ploeh blog 2024-07-26T11:27:16+00:00 Mark Seemann danish software design https://blog.ploeh.dk Three data architectures for the server https://blog.ploeh.dk/2024/07/25/three-data-architectures-for-the-server 2024-07-25T18:30:00+00:00 Mark Seemann <div id="post"> <p> <em>A comparison, for educational purposes.</em> </p> <p> <em>Use the right tool for the job.</em> How often have you encountered that phrase when discussing software architecture? </p> <p> There's nothing wrong with the sentiment per se, but it's almost devoid of meaning. It doesn't pass the 'not test'. Try to negate it and imagine if anyone would seriously hold that belief: <em>Don't use the right tool for the job,</em> said no-one ever. </p> <p> Even so, the underlying idea is that there are better and worse ways to solve problems. In software architecture too. It follows that you should choose the better solution. </p> <p> How to do that requires skill and experience. When planning a good software architecture, an important consideration is how it'll handle future requirements. This seems to indicate that an architect should be able to predict the future in order to pick the best architecture. Which is, in general, not possible. Predicting the future is not the topic of this article. </p> <p> There is, however, a more practical issue related to the notion of using the right tool for the job. One that we <em>can</em> address. </p> <h3 id="19b9dea780d2475fb4e0311dc1cc6893"> Choice <a href="#19b9dea780d2475fb4e0311dc1cc6893">#</a> </h3> <p> In order to choose the better solution, you need to be aware of alternatives. You can't choose if there's nothing to choose from. This seems obvious, but a flowchart may drive home the point in an even stronger fashion. </p> <p> <img src="/content/binary/flowchart-without-choice.png" alt="A flowchart diagram, but without any choice at the decision shape."> </p> <p> On the other hand, if you have options, you're now in a position to choose. </p> <p> <img src="/content/binary/flowchart-with-choice.png" alt="A flowchart diagram, now with three options available from the decision shape."> </p> <p> In order to make a decision, you must be able to identify alternatives. This is hardly earth-shattering, but perhaps a bit abstract. To make it concrete, in this article, I'll look at a particular example. </p> <h3 id="4b9045825d9a47c3a6d8f0af1de89a2c"> Default data architecture <a href="#4b9045825d9a47c3a6d8f0af1de89a2c">#</a> </h3> <p> Many applications need some sort of persistent storage. Particularly when it comes to (relational) database-based systems, I've seen more than one organization defaulting to a single data architecture: A presentation layer with View Models, a business logic layer with Domain Models, and a data access layer with ORM objects. A few decades ago, you'd typically see that model illustrated with horizontal layers. This is no longer en vogue. Today, most organizations that I consult with will tell me that they've decided on Ports and Adapters. Even so, if you do it right, <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">it's the same architecture</a>. </p> <p> Reusing a diagram from <a href="/2024/07/08/should-interfaces-be-asynchronous">a recent article</a>, we may draw it like this: </p> <p> <img src="/content/binary/ports-and-adapters-dependency-graph-2.png" alt="Ports and Adapters diagram, with arrows pointing inward."> </p> <p> The architect or senior developer who made that decision is obviously aware of some of the lore in the industry. He or she can often name that data architecture as either Ports and Adapters, <a href="https://alistair.cockburn.us/hexagonal-architecture/">Hexagonal Architecture</a>, <a href="/ref/clean-architecture">Clean Architecture</a>, or, more rarely, <a href="https://jeffreypalermo.com/2008/07/the-onion-architecture-part-1/">Onion Architecture</a>. </p> <p> I still get the impression that this way of arranging code was chosen by default, without much deliberation. I see it so often that it strikes me as a 'default architecture'. Are architects aware of alternatives? Can they compare the benefits and drawbacks of each alternative? </p> <h3 id="38ef737999f04f2d8c8d9fe7a44b47be"> Three alternatives <a href="#38ef737999f04f2d8c8d9fe7a44b47be">#</a> </h3> <p> As an example, I'll explore three alternative data architectures, one of them being Ports and Adapters. My goal with this is only to raise awareness. Since I rarely (if ever) see my customers use anything other than Ports and Adapters, I think some readers may benefit from seeing some alternatives. </p> <p> I'll show three ways to organize data with code, but that doesn't imply that these are the only three options. At the very least, some hybrid combinations are also possible. It's also possible that a fourth or fifth alternative exists, and I'm just not aware of it. </p> <p> In three articles, you'll see each data architecture explored in more detail. </p> <ul> <li>Using Ports and Adapters to persist restaurant table configurations</li> <li>Using a Shared Data Model to persist restaurant table configurations</li> <li>Using only a Domain Model to persist restaurant table configurations</li> </ul> <p> As the titles suggest, all three examples will attempt to address the same problem: How to persist restaurant table configuration for a restaurant. The scenario is the same as already outlined in the article <a href="/2023/12/04/serialization-with-and-without-reflection">Serialization with and without Reflection</a>, and the example code base also attempts to follow the external data format of those articles. </p> <h3 id="0b2358ba517444eeb990d1ff72613b82"> Data formats <a href="#0b2358ba517444eeb990d1ff72613b82">#</a> </h3> <p> In JSON, a table may be represented like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;singleTable&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;16, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;minimalReservation&quot;</span>:&nbsp;10 &nbsp;&nbsp;} }</pre> </p> <p> Or like this: </p> <p> <pre>{&nbsp;<span style="color:#2e75b6;">&quot;communalTable&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;10&nbsp;}&nbsp;}</pre> </p> <p> But I'll also explore what happens if you need to support multiple external formats, such as <a href="https://en.wikipedia.org/wiki/XML">XML</a>. Generally speaking, a given XML specification may lean towards favouring a verbose style based on elements, or a terser style based on attributes. An example of the former could be: </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:#a31515;">communal-table</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">capacity</span><span style="color:blue;">&gt;</span>12<span style="color:blue;">&lt;/</span><span style="color:#a31515;">capacity</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">communal-table</span><span style="color:blue;">&gt;</span></pre> </p> <p> or </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:#a31515;">single-table</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">capacity</span><span style="color:blue;">&gt;</span>4<span style="color:blue;">&lt;/</span><span style="color:#a31515;">capacity</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">minimal-reservation</span><span style="color:blue;">&gt;</span>3<span style="color:blue;">&lt;/</span><span style="color:#a31515;">minimal-reservation</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">single-table</span><span style="color:blue;">&gt;</span></pre> </p> <p> while examples of the latter style include </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:#a31515;">communal-table</span><span style="color:blue;">&nbsp;</span><span style="color:red;">capacity</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">12</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span></pre> </p> <p> and </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:#a31515;">single-table</span><span style="color:blue;">&nbsp;</span><span style="color:red;">capacity</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">4</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">minimal-reservation</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">3</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span></pre> </p> <p> As it turns out, only one of the three data architectures is flexible enough to fully address such requirements. </p> <h3 id="76af298edac94997a28a92b865b2e508"> Comparisons <a href="#76af298edac94997a28a92b865b2e508">#</a> </h3> <p> A <a href="https://en.wikipedia.org/wiki/REST">REST</a> API is the kind of application where data representation flexibility is most likely to be an issue. Thus, that only one of the three alternative architectures is able to exhibit enough expressive power in that dimension doesn't disqualify the other two. Each come with their own benefits and drawbacks. </p> <table> <thead> <tr> <td></td> <td>Ports and Adapters</td> <td>Shared Data Model</td> <td>Domain Model only</td> </tr> </thead> <tbody> <tr> <td>Advantages</td> <td> <ul> <li>Separation of concerns</li> <li>Well-described</li> </ul> </td> <td> <ul> <li>Simple</li> <li>No mapping</li> </ul> </td> <td> <ul> <li>Flexible</li> <li>Congruent with reality</li> </ul> </td> </tr> <tr> <td>Disadvantages</td> <td> <ul> <li>Much mapping</li> <li>Easy to get wrong</li> </ul> </td> <td> <ul> <li>Inflexible</li> <li>God Class attractor</li> </ul> </td> <td> <ul> <li>Requires non-opinionated framework</li> <li>Requires more testing</li> </ul> </td> </tr> </tbody> </table> <p> I'll discuss each alternative's benefits and drawback in their individual articles. </p> <p> An important point of all this is that none of these articles are meant to prescriptive. While I do have favourites, my biases are shaped by the kind of work I typically do. In other contexts, another alternative may prevail. </p> <h3 id="3db82a1056ff4f4fbcb9bb2dd9c4643c"> Example code <a href="#3db82a1056ff4f4fbcb9bb2dd9c4643c">#</a> </h3> <p> As usual, example code is in C#. Of the three languages in which I'm most proficient (the other two being <a href="https://fsharp.org/">F#</a> and <a href="https://www.haskell.org/">Haskell</a>), this is the most easily digestible for a larger audience. </p> <p> All three alternatives are written with ASP.NET 8.0, and it's unavoidable that there will be some framework-specific details. In <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, I made it an explicit point that while the examples in the book are in C#, the book (and the code in it) should be understandable by developers who normally use <a href="https://www.java.com/">Java</a>, <a href="https://en.wikipedia.org/wiki/C%2B%2B">C++</a>, <a href="https://www.typescriptlang.org/">TypeScript</a>, or similar C-based languages. </p> <p> The book is, for that reason, light on .NET-specific details. Instead, I published <a href="/2021/06/14/new-book-code-that-fits-in-your-head">an article</a> that collects all the interesting .NET things I ran into while writing the book. </p> <p> Not so here. The three articles cover enough ASP.NET particulars that readers who don't care about that framework are encouraged to skim-read. </p> <p> I've developed the three examples as three branches of the same Git repository. The code is available upon request against a small <a href="/support">support donation</a> of 10 USD (or more). If you're one of my regular supporters, you have my gratitude and can get the code without further donation. <a href="/about#contact">Send me an email</a> in both cases. </p> <h3 id="83a76525d22a49d898609fc6c1963acf"> Conclusion <a href="#83a76525d22a49d898609fc6c1963acf">#</a> </h3> <p> There's more than one way to organize a code base to deal with data. Depending on context, one may be a better choice than another. Thus, it pays to be aware of alternatives. </p> <p> In the remaining articles in this series, you'll see three examples of how to deal with persistent data from a database. In order to establish a baseline, the first covers the well-known Ports and Adapters architecture. </p> <p> <strong>Next:</strong> Using Ports and Adapters to persist restaurant table configurations. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The end of trust? https://blog.ploeh.dk/2024/07/15/the-end-of-trust 2024-07-15T19:07:00+00:00 Mark Seemann <div id="post"> <p> <em>Software development in a globalized, hostile world.</em> </p> <p> Imagine that you're perusing the thriller section in an airport book store and come across a book with the following back cover blurb: </p> <blockquote> <p> Programmers are dying. </p> <p> Holly-Ann Kerr works as a data scientist for an NGO that fights workplace discrimination. While scrubbing input, she discovers an unusual pattern in the data. Some employees seem to have an unusually high fatal accident rate. Programmers are dying in traffic accidents, falling on stairs, defect electrical wiring, smoking in bed. They work for a variety of companies. Some for Big Tech, others for specialized component vendors, some for IT-related NGOs, others again for utility companies. The deaths seem to have nothing in common, until H-A uncovers a disturbing pattern. </p> <p> All victims had recently started in a new position. And all were of Iranian descent. </p> <p> Is a racist killer on the loose? But if so, why is he only targeting new hires? And why only software developers? </p> <p> When H-A shares her discovery with the wrong people, she soon discovers that she'll be the next victim. </p> </blockquote> <p> Okay, I'm not a professional editor, so this could probably do with a bit of polish. Does it sound like an exiting piece of fiction, though? </p> <p> <img src="/content/binary/the-long-game-cover.jpg" alt="Cover of the imaginary thriller, The Long Game."> </p> <p> I'm going to spoil the plot, since the book doesn't exist anyway. </p> <h3 id="269cc12b04c24fadb740f64ef4045625"> An international plot <a href="#269cc12b04c24fadb740f64ef4045625">#</a> </h3> <p> (Apologies to Iranian readers. I have nothing against Iranians, but find the regime despicable. In any case, nothing in the following hinges on the <a href="https://en.wikipedia.org/wiki/Council_for_Intelligence_Coordination">ICC</a>. You can replace it with another adversarial intelligence agency that you don't like, including, but not limited to <a href="https://en.wikipedia.org/wiki/Reconnaissance_General_Bureau">RGB</a>, <a href="https://en.wikipedia.org/wiki/Federal_Security_Service">FSB</a>, or a clandestine Chinese intelligence organization. You could probably even swap the roles and make <a href="https://en.wikipedia.org/wiki/Central_Intelligence_Agency">CIA</a>, <a href="https://en.wikipedia.org/wiki/MI5">MI5</a>, or <a href="https://en.wikipedia.org/wiki/Mossad">Mossad</a> be the bad guys, if your loyalties lie elsewhere.) </p> <p> In the story, it turns out that clandestine Iranian special operations are attempting to recruit <a href="https://en.wikipedia.org/wiki/Mole_(espionage)">moles</a> in software organizations that constitute the supply chain of Western digital infrastructure. </p> <p> Intelligence bureaus and software organizations that directly develop sensitive software tend to have good security measures. Planting a mole in such an organization is difficult. The entire supply chain of software dependencies, on the other hand, is much more vulnerable. If you can get an employee to install a <a href="https://en.wikipedia.org/wiki/Backdoor_(computing)">backdoor</a> in <a href="https://en.wikipedia.org/wiki/Npm_left-pad_incident">left-pad</a>, chances are that you may attain <a href="https://en.wikipedia.org/wiki/Arbitrary_code_execution">remote execution</a> capabilities on an ostensibly secure system. </p> <p> In my hypothetical thriller, the Iranians kill those software developers that they <em>fail</em> to recruit. After all, one can't run a clandestine operation if people notify the police that they've been approached by a foreign power. </p> <h3 id="8979c9d3d6484a9b8356b887220a594f"> Long game <a href="#8979c9d3d6484a9b8356b887220a594f">#</a> </h3> <p> Does that plot sound far-fetched? </p> <p> I admit that I did <a href="https://en.wikipedia.org/wiki/Up_to_eleven">turn to 11</a> some plot elements. This is, after all, supposed to be a thriller. </p> <p> The story is, however, 'loosely based on real events'. Earlier this year, <a href="https://arstechnica.com/security/2024/04/what-we-know-about-the-xz-utils-backdoor-that-almost-infected-the-world/">a Microsoft developer revealed a backdoor that someone had intentionally planted in xz Utils</a>. That version of the software was close to being merged into <a href="https://www.debian.org/">Debian</a> and <a href="https://www.redhat.com/">Red Hat</a> Linux distributions. It would have enabled an attacker to execute arbitrary code on an infected machine. </p> <p> The attack was singularly sophisticated. It also looks as though it was initiated years ago by one or more persons who contributed real, useful work to an open-source project, apparently (in hindsight) with the sole intention of gaining the trust of the rest of the community. </p> <p> This is such a long game that it reeks of an adversarial state actor. The linked article speculates on which foreign power may be behind the attack. No, not the Iranians, after all. </p> <p> If you think about it, it's an entirely rational gambit for a foreign intelligence agency to make. It's not that the <a href="https://en.wikipedia.org/wiki/Stuxnet">NSA hasn't already tried something comparable</a>. If anything, the xz hack mostly seems far-fetched because it's so unnecessarily sophisticated. </p> <p> Usually, the most effective hacking techniques utilize human trust or gullibility. Why spend enormous effort developing sophisticated buffer overrun exploits if you can get a (perhaps unwitting) insider to run arbitrary code for you? </p> <p> It'd be much cheaper, and much more reliable, to recruit moles on the inside of software companies, and get them to add the backdoors you need. It doesn't necessary have to be new hires, but perhaps (I'm speculating) it's easier to recruit people before they've developed any loyalty to their new team mates. </p> <h3 id="3a6d30419c8d4e869309502db610dfd6"> The soft underbelly <a href="#3a6d30419c8d4e869309502db610dfd6">#</a> </h3> <p> Which software organizations are the most promising targets? If it were me, I'd particularly try to go after various component vendors. One category may be companies that produce <a href="https://en.wikipedia.org/wiki/Rapid_application_development">RAD</a> tools such as grid <a href="https://en.wikipedia.org/wiki/Graphical_user_interface">GUIs</a>, but also service providers that offer free <a href="https://en.wikipedia.org/wiki/Software_development_kit">SDKs</a> to, say, send email, generate invoices, send SMS, charge credit cards, etc. </p> <p> I'm <em>not</em> implying that any such company has ill intent, but since such software run on many machines, it's a juicy target if you can sneak a backdoor into one. </p> <p> Why not open-source software (OSS)? Many OSS libraries run on even more machines, so wouldn't that be an even more attractive target for an adversary? Yes, but on the other hand, most popular open-source code is also scrutinized by many independent agents, so it's harder to sneak in a backdoor. As the attempted xz hack demonstrates, even a year-long sophisticated attack is at risk of being discovered. </p> <p> Doesn't commercial or closed-source code receive the same level of scrutiny? </p> <p> In my experience, not always. Of course, some development organizations use proper shared-code-ownership techniques like code reviews or pair programming, but others rely on siloed solo development. Programmers just check in code that no-one else ever looks at. </p> <p> In such an organization, imagine how easy it'd be for a mole to add a backdoor to a widely-distributed library. He or she wouldn't even have to resort to sophisticated ways to obscure the backdoor, because no colleague would be likely to look at the code. Particularly not if you bury it in seven levels of nested <code>for</code> loops and call the class <code>MonitorManager</code> or similar. As long as the reusable library ships as compiled code, it's unlikely that customers will discover the backdoor before its too late. </p> <h3 id="2987e2669c4c46c29a45281d3a6b3adc"> Trust <a href="#2987e2669c4c46c29a45281d3a6b3adc">#</a> </h3> <p> Last year I published an article <a href="/2023/03/20/on-trust-in-software-development">on trust in software development</a>. The point of that piece wasn't that you should suspect your colleagues of ill intent, but rather that you can trust neither yourself nor your co-workers for the simple reason that people make mistakes. </p> <p> Since then, I've been doing some work in the digital security space, and I've been forced to think about concerns like <a href="https://en.wikipedia.org/wiki/Supply_chain_attack">supply-chain attacks</a>. The implications are, unfortunately, that you can't automatically trust that your colleague has benign intentions. </p> <p> This, obviously, will vary with context. If you're only writing a small web site for your HR department to use, it's hard to imagine how an adversarial state actor could take advantage of a backdoor in <em>your</em> code. If so, it's unlikely that anyone will go to the trouble of planting a mole in your organization. </p> <p> On the other hand, if you're writing any kind of reusable library or framework, you just might be an interesting target. If so, you can no longer entirely trust your team mates. </p> <p> As a Dane, that bothers me deeply. Denmark, along with the other Nordic countries, exhibit <a href="https://ourworldindata.org/trust">the highest levels of inter-societal trust in the world</a>. I was raised to trust strangers, and so far, it's worked well enough for me. A business transaction in Denmark is often just a short email exchange. It's a great benefit to the business environment, and the economy in general, that we don't have to waste a lot of resources filling out formulas, contracts, agreements, etc. Trust is grease that makes society run smoother. </p> <p> Even so, Scandinavians aren't <em>naive</em>. We don't believe that we can trust everyone. To a large degree, we rely on a lot of subtle social cues to assess a given situation. Some people shouldn't be trusted, and we're able to identify those situations, too. </p> <p> What remains is that insisting that you can trust your colleague, just because he or she is your colleague, would be descending into teleology. I'm not a proponent of wishful thinking if good arguments suggest the contrary. </p> <h3 id="295deb8a2c1041678830fcf173f7abf4"> Shared code ownership <a href="#295deb8a2c1041678830fcf173f7abf4">#</a> </h3> <p> Perhaps you shouldn't trust your colleagues. How does that impact software development? </p> <p> The good news is that this is yet another argument to practice the beneficial practices of shared code ownership. Crucially, what this should entail is not just that everyone is allowed to edit any line of code, but rather that all team members take responsibility for the entire code base. No-one should be allowed to write code in splendid isolation. </p> <p> There are several ways to address this concern. I often phrase it as follows: <em>There should be at least two pair of eyes on every line of code before a merge to master</em>. </p> <p> As I describe in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, you can achieve that goal with pair programming, ensemble programming, or code reviews (including <a href="/2021/06/21/agile-pull-requests">agile pull request</a> reviews). That's a broad enough palette that it should be possible for every developer in every organization to find a modus vivendi that fits any personality and context. </p> <p> Just looking at each others' code could significantly raise the bar for a would-be mole to add a backdoor to the code base. As an added benefit, it might also raise the general code quality. </p> <p> What this <em>does</em> suggest to me, however, is that a too simplistic notion of <em>running on trunk</em> may be dangerous. Letting everyone commit to <em>master</em> and trusting that everyone means well no longer strikes me as a good idea (again, given the context, and all that). </p> <p> Or, if you do, you should consider having some sort of systematic <ins datetime="2024-07-26T08:09Z">posterior</ins> <del datetime="2024-07-26T08:09Z">post mortem</del> review process. I've read of organizations that do that, but specific sources escape me at the moment. With Git, however, it's absolutely within the realms of the possible to make a diff of all change since the last ex-post review, and then go through those changes. </p> <h3 id="be306c291a644cd09762335becd1291e"> Conclusion <a href="#be306c291a644cd09762335becd1291e">#</a> </h3> <p> The world is changed. I feel it in the <a href="https://owasp.org/www-project-top-ten/">OWASP top 10</a>. I sense it in the shifting geopolitical climate. I smell it on the code I review. </p> <p> Much that once was, is lost. The dream of a global computer network with boundless trust is no more. There are countries whose interests no longer align with ours. Who pay full-time salaries to people whose job it is to wage 'cyber warfare' against us. We can't rule out that parts of such campaigns include planting moles in our midsts. Moles whose task it is to weaken the foundations of our digital infrastructure. </p> <p> In that light, should you always trust your colleagues? </p> <p> Despite the depressing thought that I probably shouldn't, I'm likely to bounce back to my usual Danish most-people-are-to-be-trusted attitude tomorrow. On the other hand, I'll still insist that more than one person is involved with every line of code. Not only because every other person may be a foreign agent, but mostly, still, because humans are fallible, and two brains think better than one. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="95fab96f981647a9a852c8d960b7f824"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#95fab96f981647a9a852c8d960b7f824">#</a></div> <div class="comment-content"> <blockquote> Or, if you do, you should consider having some sort of systematic post mortem review process. I've read of organizations that do that, but specific sources escape me at the moment. </blockquote> <p> My company has a Google Docs template for postmortem analysis that we use when something goes especially wrong. The primary focus is stating what went wrong according to the "five whys technique". Our template links to <a href="http://www.startuplessonslearned.com/2008/11/five-whys.html">this post by Eric Ries</a>. There is also<a href="https://en.wikipedia.org/wiki/Five_whys">this Wikipedia article on the subject</a>. The section heading are "What happened" (one sentence), "Impact on Customers" (duration and severity), "What went wrong (5 Whys)", "What went right (optional)", "Corrective Actions" (and all of the content so far should be short enough to fit on one page), "Timeline" (a bulleted list asking for "Event beginning", "Time to Detect (monitoring)", "Time to Notify (alerting)", "Time to Respond (devops)", "Time to Troubleshoot (devops)", "Time to Mitigate (devops)", "Event end"), "Logs (optional)". </p> </div> <div class="comment-date">2024-07-21 15:37 UTC</div> </div> <div class="comment" id="0c1f8083882c4de8a11be963869cc098"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0c1f8083882c4de8a11be963869cc098">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I now realize that 'post mortem' was a poor choice of words on my part, since it implies that something went wrong. I should have written 'posterior' instead. I'll update the article. </p> <p> I've been digging around a bit to see if I can find the article that originally made me aware of that option. I'm fairly sure that it wasn't <a href="https://itnext.io/optimizing-the-software-development-process-for-continuous-integration-and-flow-of-work-56cf614b3f59">Optimizing the Software development process for continuous integration and flow of work</a>, but that article, on the other hand, seems to be the source that other articles cite. It's fairly long, and also discusses other concepts; the relevant section here is the one called <em>Non-blocking reviews</em>. </p> <p> An shorter overview of this kind of process can be found in <a href="https://thinkinglabs.io/articles/2023/05/02/non-blocking-continuous-code-reviews-a-case-study.html">Non-Blocking, Continuous Code Reviews - a case study</a>. </p> </div> <div class="comment-date">2024-07-26 08:04 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Should interfaces be asynchronous? https://blog.ploeh.dk/2024/07/08/should-interfaces-be-asynchronous 2024-07-08T13:52:00+00:00 Mark Seemann <div id="post"> <p> <em>Async and await are notorious for being contagious. Must all interfaces be Task-based, just in case?</em> </p> <p> I recently came across this question on Mastodon: </p> <blockquote> <p> "To async or not to async? </p> <p> "How would you define a library interface for a service that probably will be implemented with an in memory procedure - let's say returning a mapped value to a key you registered programmatically - and a user of your API might want to implement a decorator that needs a 'long running task' - for example you want to log a msg into your db or load additional mapping from a file? </p> <p> "Would you define the interface to return a Task&lt;string&gt; or just a string?" </p> <footer><cite><a href="https://fosstodon.org/@Fandermill/112613967801632197">Fandermill</a></cite></footer> </blockquote> <p> While seemingly a simple question, it's both fundamental and turns out to have deeper implications than you may at first realize. </p> <h3 id="e4c03ad0436340b4b510d51e14acd794"> Interpretation <a href="#e4c03ad0436340b4b510d51e14acd794">#</a> </h3> <p> Before I proceed, I'll make my interpretation of the question more concrete. This is just how I <em>interpret</em> the question, so doesn't necessarily reflect the original poster's views. </p> <p> The post itself doesn't explicitly mention a particular language, and since several languages now have <code>async</code> and <code>await</code> features, the question may be of more general interest that a question constrained to a single language. On the other hand, in order to have something concrete to discuss, it'll be useful with some real code examples. From perusing the discussion surrounding the original post, I get the impression that the language in question may be C#. That suits me well, since it's one of the languages with which I'm most familiar, and is also a language where programmers of other C-based languages should still be able to follow along. </p> <p> My interpretation of the implementation, then, is this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NameMap</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;knownIds&nbsp;=&nbsp;<span style="color:blue;">new</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;4778CA3D-FB1B-4665-AAC1-6649CEFA4F05&quot;</span>),&nbsp;<span style="color:#a31515;">&quot;Bob&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;8D3B9093-7D43-4DD2-B317-DCEE4C72D845&quot;</span>),&nbsp;<span style="color:#a31515;">&quot;Alice&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetName</span>(<span style="color:#2b91af;">Guid</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">guid</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;knownIds.<span style="font-weight:bold;color:#74531f;">TryGetValue</span>(<span style="font-weight:bold;color:#1f377f;">guid</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>)&nbsp;?&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>&nbsp;:&nbsp;<span style="color:#a31515;">&quot;Trudy&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Nothing fancy, but, as <a href="https://fosstodon.org/@Fandermill">Fandermill</a> writes in a follow-up post: </p> <blockquote> <p> "Used examples that first came into mind, but it could be anything really." </p> <footer><cite><a href="https://fosstodon.org/@Fandermill/112613968890232099">Fandermill</a></cite></footer> </blockquote> <p> The point, as I understand it, is that the intended implementation doesn't require asynchrony. A <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a>, on the other hand, may. </p> <p> Should we, then, declare an interface like the following? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">INameMap</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetName</span>(<span style="color:#2b91af;">Guid</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">guid</span>); }</pre> </p> <p> If we do, the <code>NameMap</code> class can't automatically implement that interface because the return types of the two <code>GetName</code> methods don't match. What are the options? </p> <h3 id="9da822948cd049c0a625bb9a2c013d7b"> Conform <a href="#9da822948cd049c0a625bb9a2c013d7b">#</a> </h3> <p> While the following may not be the 'best' answer, let's get the obvious solution out of the way first. Let the implementation conform to the interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NameMap</span>&nbsp;:&nbsp;<span style="color:#2b91af;">INameMap</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;knownIds&nbsp;=&nbsp;<span style="color:blue;">new</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;4778CA3D-FB1B-4665-AAC1-6649CEFA4F05&quot;</span>),&nbsp;<span style="color:#a31515;">&quot;Bob&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;8D3B9093-7D43-4DD2-B317-DCEE4C72D845&quot;</span>),&nbsp;<span style="color:#a31515;">&quot;Alice&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetName</span>(<span style="color:#2b91af;">Guid</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">guid</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Task</span>.<span style="color:#74531f;">FromResult</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;knownIds.<span style="font-weight:bold;color:#74531f;">TryGetValue</span>(<span style="font-weight:bold;color:#1f377f;">guid</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>)&nbsp;?&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>&nbsp;:&nbsp;<span style="color:#a31515;">&quot;Trudy&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This variation of the <code>NameMap</code> class conforms to the interface by making the <code>GetName</code> method look asynchronous. </p> <p> We may even keep the synchronous implementation around as a public method if some client code might need it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NameMap</span>&nbsp;:&nbsp;<span style="color:#2b91af;">INameMap</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;knownIds&nbsp;=&nbsp;<span style="color:blue;">new</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;4778CA3D-FB1B-4665-AAC1-6649CEFA4F05&quot;</span>),&nbsp;<span style="color:#a31515;">&quot;Bob&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;8D3B9093-7D43-4DD2-B317-DCEE4C72D845&quot;</span>),&nbsp;<span style="color:#a31515;">&quot;Alice&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetName</span>(<span style="color:#2b91af;">Guid</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">guid</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Task</span>.<span style="color:#74531f;">FromResult</span>(<span style="font-weight:bold;color:#74531f;">GetNameSync</span>(<span style="font-weight:bold;color:#1f377f;">guid</span>)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetNameSync</span>(<span style="color:#2b91af;">Guid</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">guid</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;knownIds.<span style="font-weight:bold;color:#74531f;">TryGetValue</span>(<span style="font-weight:bold;color:#1f377f;">guid</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>)&nbsp;?&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>&nbsp;:&nbsp;<span style="color:#a31515;">&quot;Trudy&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since C# doesn't support return-type-based overloading, we need to distinguish these two methods by giving them different names. In C# it might be more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> to name the asynchronous method <code>GetNameAsync</code> and the synchronous method just <code>GetName</code>, but for reasons that would be too much of a digression now, I've never much liked that naming convention. In any case, I'm not going to go in this direction for much longer, so it hardly matters how we name these two methods. </p> <h3 id="821f1bb35dac41eeade7c5d7b28a3b18"> Kinds of interfaces <a href="#821f1bb35dac41eeade7c5d7b28a3b18">#</a> </h3> <p> Another digression is, however, quite important. Before we can look at some more code, I'm afraid that we have to perform a bit of practical ontology, as it were. It starts with the question: <em>Why do we even need interfaces?</em> </p> <p> I should also make clear, as a digression within a digression, that by 'interface' in this context, I'm really interested in any kind of mechanism that enables you to achieve polymorphism. In languages like C# or <a href="https://www.java.com/">Java</a>, we may in fact avail ourselves of the <code>interface</code> keyword, as in the above <code>INameMap</code> example, but we may equally well use a base class or perhaps just what C# calls a <a href="https://learn.microsoft.com/dotnet/csharp/programming-guide/delegates/">delegate</a>. In other languages, we may use function or action types, or even <a href="https://en.wikipedia.org/wiki/Function_pointer">function pointers</a>. </p> <p> Regardless of specific language constructs, there are, as far as I can tell, two kinds of interfaces: </p> <ul> <li>Interfaces that enable variability or extensibility in behaviour.</li> <li>Interfaces that mostly or exclusively exist to support automated testing.</li> </ul> <p> While there may be some overlap between these two kinds, in my experience, the intersection between the two tends to be surprisingly small. Interfaces tend to mostly belong to one of those two categories. </p> <h3 id="d990222c075942ab851c1455c8efcb95"> Strategies and higher-order functions <a href="#d990222c075942ab851c1455c8efcb95">#</a> </h3> <p> In design-patterns parlance, examples of the first kind are <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder</a>, <a href="https://en.wikipedia.org/wiki/State_pattern">State</a>, <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">Chain of Responsibility</a>, <a href="https://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a>, and perhaps most starkly represented by the <a href="https://en.wikipedia.org/wiki/Strategy_pattern">Strategy</a> pattern. A Strategy is an encapsulated piece of behaviour that you pass around as a single 'thing' (an <em>object</em>). </p> <p> And granted, you could also use a Strategy to access a database or make a web-service call, but that's not how the pattern was <a href="/ref/dp">originally described</a>. We'll return to that use case in the next section. </p> <p> Rather, the first kind of interface exists to enable extensibility or variability in algorithms. Typical examples (from Design Patterns) include page layout, user interface component rendering, building a maze, finding the most appropriate help topic for a given application context, and so on. If we wish to relate this kind of interface to the <a href="https://en.wikipedia.org/wiki/SOLID">SOLID</a> principles, it mostly exists to support the <a href="https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle">Open-closed principle</a>. </p> <p> A good heuristics for identifying such interfaces is to consider the Reused Abstractions Principle (Jason Gorman, 2010, I'd link to it, but the page has fallen off the internet. Use your favourite web archive to read it.). If your code base contains <em>multiple</em> production-ready implementations of the same interface, you're reusing the interface, most likely to vary the behaviour of a general-purpose data structure. </p> <p> And before the functional-programming (FP) crowd becomes too smug: FP uses this kind of interface <em>all the time</em>. In the FP jargon, however, we rather talk about <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order functions</a> and the interfaces we use to modify behaviour are typically modelled as functions and passed as <a href="https://en.wikipedia.org/wiki/Anonymous_function">lambda expressions</a>. So when you write <code>Cata((_,&nbsp;xs)&nbsp;=&gt;&nbsp;xs.Sum(),&nbsp;_&nbsp;=&gt;&nbsp;1)</code> (<a href="/2019/08/05/rose-tree-catamorphism">as one does</a>), you <a href="/2018/06/25/visitor-as-a-sum-type">might as well</a> just have passed a <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a> implementation to an <code>Accept</code> method. </p> <p> This hints at a more quantifiable distinction: If the interface models something that's intended to be a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, it'd typically be part of a higher-order API in FP, while we in object-oriented design (once again) lack the terminology to distinguish these interfaces from the other kind. </p> <p> These days, in C# <a href="/2023/09/04/decomposing-ctfiyhs-sample-code-base">I mostly use these kinds of interfaces for the Visitor pattern</a>. </p> <h3 id="36e2464953344fa2a0b173f98bb260c9"> Seams <a href="#36e2464953344fa2a0b173f98bb260c9">#</a> </h3> <p> The other kind of interface exists to afford automated testing. In <a href="/ref/wewlc">Working Effectively with Legacy Code</a>, Michael Feathers calls such interfaces <em>Seams</em>. Modern object-oriented code bases often use <a href="/dippp">Dependency Injection</a> (DI) to control which Strategies are in use in a given context. The production system may use an object that communicates with a relational database, while an automated test environment might replace that with a <a href="https://martinfowler.com/bliki/TestDouble.html">Test Double</a>. </p> <p> Yes, I wrote <em>Strategies</em>. As I suggested above, a Strategy is really a replaceable object in its purest form. When you use DI you may call all those interfaces <code>IUserRepository</code>, <code>ICommandHandler</code>, <code>IEmailGateway</code>, and so on, but they're really all Strategies. </p> <p> Contrary to the first kind of interface, you typically only find a single production implementation of each of these interfaces. If you find more that one, the rest are usually <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a> (one that logs, one that caches, one that works as a <a href="https://martinfowler.com/bliki/CircuitBreaker.html">Circuit Breaker</a>, etc.). All other implementations will be defined in the test code as dynamic mocks or <a href="http://xunitpatterns.com/Fake%20Object.html">Fakes</a>. </p> <p> Code bases that rely heavily on DI in order to support testing rub many people the wrong way. In 2014 <a href="https://en.wikipedia.org/wiki/David_Heinemeier_Hansson">David Heinemeier Hansson</a> published a serious criticism of such <a href="https://dhh.dk/2014/test-induced-design-damage.html">test-induced damage</a>. For the record, I agree with the criticism, but <a href="/2020/08/17/unit-testing-is-fine">not with the conclusion</a>. While I still practice test-driven development, I <a href="https://stackoverflow.blog/2022/01/03/favor-real-dependencies-for-unit-testing/">only define interfaces for true architectural dependencies</a>. So, yes, my code bases may have an <code>IReservationsRepository</code> or <code>IEmailGateway</code>, but no <code>ICommandHandler</code> or <code>IUserManager</code>. </p> <p> The bottom line, though, is that some interfaces exist to support testing. If there's a better way to make inherently non-deterministic systems behave deterministically in a test context, I've yet to discover it. </p> <p> (As an aside, it's worth looking into tests that adopt non-deterministic behaviour as a driving principle, or at least an unavoidable state of affairs. Property-based testing is one such approach, but I also found the article <a href="https://arialdomartini.github.io/when-im-done-i-dont-clean-up">When I'm done, I don't clean up</a> by <a href="https://arialdomartini.github.io/">Arialdo Martini</a> interesting. You may also want to refer to my article <a href="/2021/01/11/waiting-to-happen">Waiting to happen</a> for a discussion of how to make tests independent of system time.) </p> <h3 id="f8bd1ea29b2a4ddbaf6538c238ec6979"> Where to define interfaces <a href="#f8bd1ea29b2a4ddbaf6538c238ec6979">#</a> </h3> <p> The reason the above distinction is important is that it fundamentally determines where interfaces should be defined. In short, the first kind of interface is part of an object model's API, and should be defined together with that API. The second kind, on the other hand, is part of a particular application's architecture, and should be defined by the client code that talks to the interface. </p> <p> As an example of the first kind, consider <a href="/2024/06/24/a-mutable-priority-collection">this recent example</a>, where the <code><span style="color:#2b91af;">IPriorityEditor</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> interface is part of the <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> API. You <em>must</em> ship the interface together with the class, because the <code>Edit</code> method takes an interface implementation as an argument. It's how client code interacts with the API. </p> <p> Another example is <a href="/2023/12/25/serializing-restaurant-tables-in-c">this Table class</a> that comes with an <code>ITableVisitor&lt;T&gt;</code> interface. In both cases, we'd expect interface implementations to be deterministic. These interfaces don't exist to support automated testing, but rather to afford a flexible programming model. </p> <p> For the sake of argument, imagine that you package such APIs in reusable libraries that you publish via a package manager. In that case, it's obvious that the interface is as much part of the package as the class. </p> <p> Contrast this with the other kind of interface, as described in the article <a href="/2023/09/04/decomposing-ctfiyhs-sample-code-base">Decomposing CTFiYH's sample code base</a> or showcased in the article <a href="/2019/04/01/an-example-of-state-based-testing-in-c">An example of state-based testing in C#</a>. In the latter example, the interfaces <code>IUserReader</code> and <code>IUserRepository</code> are <em>not</em> part of any pre-packaged library. Rather, they are defined by the application code to support application-specific needs. </p> <p> This may be even more evident if you contemplate the diagram in <a href="/2023/09/04/decomposing-ctfiyhs-sample-code-base">Decomposing CTFiYH's sample code base</a>. Interfaces like <code>IPostOffice</code> and <code>IReservationsRepository</code> only exist to support the application. Following the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> </p> <blockquote> <p> "clients [...] own the abstract interfaces" </p> <footer><cite>Robert C. Martin, <a href="/ref/appp">APPP</a>, chapter 11</cite></footer> </blockquote> <p> In these code bases, only the Controllers (or rather the tests that exercise them) need these interfaces, so the Controllers get to define them. </p> <h3 id="8674154df1bf4c22b59f1fd3baf5996e"> Should it be asynchronous, then? <a href="#8674154df1bf4c22b59f1fd3baf5996e">#</a> </h3> <p> Okay, so should <code>INameMap.GetName</code> return <code><span style="color:blue;">string</span></code> or <code><span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;</code>, then? </p> <p> Hopefully, at this point, it should be clear that the answer depends on what kind of interface it is. </p> <p> If it's the first kind, the return type should support the requirements of the API. If the object model doesn't need the return type to be asynchronous, it shouldn't be. </p> <p> If it's the second kind of interface, the application code decides what <em>it</em> needs, and defines the interface accordingly. </p> <p> In neither case, however, is it the concrete class' responsibility to second-guess what client code might need. </p> <p> <em>But client code may need the method to be asynchronous. What's the harm of returning <code><span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;</code>, just in case?</em> </p> <p> The problem, as you may well be aware, is that <a href="https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/">the asynchronous programming model is contagious</a>. Once you've made an API asynchronous, you can't easily make it synchronous, whereas if you have a synchronous API, you can easily make it asynchronous. This follows from <a href="https://en.wikipedia.org/wiki/Postel%27s_law">Postel's law</a>, in this case: Be conservative with what you send. </p> <h3 id="17aebf480ad4468cac7eb1045dfa6041"> Library API <a href="#17aebf480ad4468cac7eb1045dfa6041">#</a> </h3> <p> Imagine, for the sake of argument, that the <code>NameMap</code> class is defined in a reusable library, wrapped in a package and imported into your code base via a package manager (NuGet, Maven, pip, NPM, Hackage, RubyGems, etc.). </p> <p> Clearly it shouldn't implement any interface in order to 'support unit testing', since such interfaces should be defined by application code. </p> <p> It <em>could</em> implement one or more 'extensibility' interfaces, if such interfaces are part of the wider API offered by the library. In the case of the <code>NameMap</code> class, we don't really know if that's the case. To complete this part of the argument, then, I'd just leave it as shown in the first code example, shown above. It doesn't need to implement any interface, and <code>GetName</code> can just return <code>string</code>. </p> <h3 id="66d0c20621ef43e5808f589a30314d6f"> Domain Model <a href="#66d0c20621ef43e5808f589a30314d6f">#</a> </h3> <p> What if, instead of an external library, the <code>NameMap</code> class is part of an application's Domain Model? </p> <p> In that case, you <em>could</em> define application-level interfaces as part of the Domain Model. In fact, most people do. Even so, I'd recommend that you don't, at least if you're aiming for a <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">Functional Core, Imperative Shell</a> architecture, a <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a>, or even a <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">Ports and Adapters</a> or, if you will, <a href="/ref/clean-architecture">Clean Architecture</a>. The interfaces that exist only to support testing are application concerns, so keep them out of the Domain Model and instead define them in the Application Model. </p> <p> <img src="/content/binary/ports-and-adapters-dependency-graph-2.png" alt="Ports and Adapters diagram, with arrows pointing inward."> </p> <p> You don't have to follow my advice. If you want to define interfaces in the Domain Model, I can't stop you. But what if, as I recommend, you define application-specific interfaces in the Application Model? If you do that, your <code>NameMap</code> Domain Model can't implement your <code>INameMap</code> interface, because the dependencies point the other way, and most languages will not allow circular dependencies. </p> <p> In that case, what do you do if, as the original toot suggested, you need to Decorate the <code>GetName</code> method with some asynchronous behaviour? </p> <p> You can always introduce an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NameMapAdapter</span>&nbsp;:&nbsp;<span style="color:#2b91af;">INameMap</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">NameMap</span>&nbsp;imp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">NameMapAdapter</span>(<span style="color:#2b91af;">NameMap</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">imp</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.imp&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">imp</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetName</span>(<span style="color:#2b91af;">Guid</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">guid</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Task</span>.<span style="color:#74531f;">FromResult</span>(imp.<span style="font-weight:bold;color:#74531f;">GetName</span>(<span style="font-weight:bold;color:#1f377f;">guid</span>)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Now any <code>NameMap</code> object can look like an <code>INameMap</code>. This is exactly the kind of problem that the Adapter pattern addresses. </p> <p> <em>But,</em> you say, <em>that's too much trouble! I don't want to have to maintain two classes that are almost identical.</em> </p> <p> I understand the concern, and it may even be appropriate. Maybe you're right. As usual, I don't really intend this article to be prescriptive. Rather, I'm offering ideas for your consideration, and you can choose to adopt them or ignore them as it best fits your context. </p> <p> When it comes to whether or not an Adapter is an unwarranted extra complication, I'll return to that topic later in this article. </p> <h3 id="f51b8986514046e989556923bc19a063"> Application Model <a href="#f51b8986514046e989556923bc19a063">#</a> </h3> <p> The final architectural option is when the concrete <code>NameMap</code> class is part of the Application Model, where you'd also define the application-specific <code>INameMap</code> interface. In that case, we must assume that the <code>NameMap</code> class implements some application-specific concern. If you want it to implement an interface so that you can wrap it in a Decorator, then do that. This means that the <code>GetName</code> method must conform to the interface, and if that means that it must be asynchronous, then so be it. </p> <p> As <a href="https://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a> wrote in a Facebook article that used to be accessible without a Facebook account (but isn't any longer): </p> <blockquote> <p> "Things that change at the same rate belong together. Things that change at different rates belong apart." </p> <footer><cite><a href="https://www.facebook.com/notes/kent-beck/naming-from-the-outside-in/464270190272517">Naming From the Outside In</a>, Kent Beck, Facebook, 2012</cite></footer> </blockquote> <p> If the concrete <code>NameMap</code> class and the <code>INameMap</code> interface are both part of the application model, it's not unreasonable to guess that they may change together. (Do be mindful of <a href="https://en.wikipedia.org/wiki/Shotgun_surgery">Shotgun Surgery</a>, though. If you expect the interface and the concrete class to frequently change, then perhaps another design might be more appropriate.) </p> <h3 id="9cf2a82474ea4af49b7f5b556a1e7fce"> Easier Adapters <a href="#9cf2a82474ea4af49b7f5b556a1e7fce">#</a> </h3> <p> Before concluding this article, let's revisit the topic of introducing an Adapter for the sole purpose of 'architectural purity'. Should you really go to such lengths only to 'do it right'? You decide, but </p> <blockquote> <p> You can only be pragmatic if you know how to be dogmatic. </p> <footer><cite><a href="/2018/11/12/what-to-test-and-not-to-test">What to test and not to test</a></cite>, me</footer> </blockquote> <p> I'm presenting a dogmatic solution for your consideration, so that you know what it might look like. Would I follow my own 'dogmatic' advice? Yes, I usually do, but then, <a href="/2020/03/23/repeatable-execution">I wouldn't log the return value of a pure function</a>, so I wouldn't introduce an interface for <em>that</em> purpose, at least. To be fair to Fandermill, he or she also wrote: "or load additional mapping from a file", which could be an appropriate motivation for introducing an interface. I'd probably go with an Adapter in that case. </p> <p> Whether or not an Adapter is an unwarranted complication depends, however, on language specifics. In high-<a href="/2019/12/16/zone-of-ceremony">ceremony</a> languages like C#, Java, or <a href="https://en.wikipedia.org/wiki/C%2B%2B">C++</a>, adding an Adapter involves at least one new file, and dozens of lines of code. </p> <p> Consider, on the other hand, a low-ceremony language like <a href="https://www.haskell.org/">Haskell</a>. The corresponding <code>getName</code> function might close over a statically defined map and have the type <code><span style="color:#2b91af;">getName</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">UUID</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span></code>. </p> <p> How do you adapt such a pure function to an API that returns <a href="/2020/06/08/the-io-container">IO</a> (which is <a href="/2020/07/27/task-asynchronous-programming-as-an-io-surrogate"><em>roughly</em> comparable to task-based programming</a>)? Trivially: </p> <p> <pre><span style="color:#2b91af;">getNameM</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">UUID</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;<span style="color:#2b91af;">String</span> getNameM&nbsp;=&nbsp;<span style="color:blue;">return</span>&nbsp;.&nbsp;getName</pre> </p> <p> For didactic purposes I have here shown the 'Adapter' as an explicit function, but in idiomatic Haskell I'd consider this below the <a href="https://wiki.haskell.org/Fairbairn_threshold">Fairbairn threshold</a>; I'd usually just inline the composition <code><span style="color:blue;">return</span>&nbsp;.&nbsp;getName</code> if I needed to adapt the <code>getName</code> function to the <a href="/2022/04/04/kleisli-composition">Kleisli</a> category. </p> <p> You can do the same in <a href="https://fsharp.org/">F#</a>, where the composition would be <code><span style="color:#74531f;">getName</span> &gt;&gt; <span style="color:#2b91af;">Task</span>.<span style="color:#74531f;">FromResult</span></code>. F# compositions usually go in the (for Westerners) intuitive left-to-right directions, whereas Haskell compositions follow the mathematical right-to-left convention. </p> <p> The point, however, is that there's nothing conceptually complicated about an Adapter. Unfortunately, however, some languages require substantial ceremony to implement them. </p> <h3 id="39dae93245f141a09754ff56305fe805"> Conclusion <a href="#39dae93245f141a09754ff56305fe805">#</a> </h3> <p> Should an API return a Task-based (asynchronous) value 'just in case'? In general: No. </p> <p> You can't predict all possible use cases, so don't make an API more complicated than it has to be. If you need to implement an application-specific interface, use the Adapter design pattern. </p> <p> A possible exception to this rule is if the entire API (the concrete implementation <em>and</em> the interface) only exists to support a specific application. If the interface and its concrete implementation are both part of the Application Model, you may as well skip the Adapter step and consider the concrete implementation as its own Adapter. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An immutable priority collection https://blog.ploeh.dk/2024/07/01/an-immutable-priority-collection 2024-07-01T17:28:00+00:00 Mark Seemann <div id="post"> <p> <em>With examples in C# and F#.</em> </p> <p> This article is part of a <a href="/2024/06/12/simpler-encapsulation-with-immutability">series about encapsulation and immutability</a>. After two attempts at an object-oriented, mutable implementation, I now turn toward immutability. As already suggested in the introductory article, immutability makes it easier to maintain invariants. </p> <p> In the introductory article, I described the example problem in more details, but in short, the exercise is to develop a class that holds a collection of prioritized items, with the invariant that the priorities must always sum to 100. It should be impossible to leave the object in a state where that's not true. It's quite an illuminating exercise, so if you have the time, you should try it for yourself before reading on. </p> <h3 id="8543ed3d0ac44d3a8d75145da7e10626"> Initialization <a href="#8543ed3d0ac44d3a8d75145da7e10626">#</a> </h3> <p> Once again, I begin by figuring out how to initialize the object, and how to model it. Since it's a kind of collection, and since I now plan to keep it immutable, it seems natural to implement <a href="https://learn.microsoft.com/dotnet/api/system.collections.generic.ireadonlycollection-1">IReadOnlyCollection&lt;T&gt;</a>. </p> <p> In this, the third attempt, I'll reintroduce <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code>, with one important difference. It's now an immutable record: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">record</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">Item</span>,&nbsp;<span style="color:blue;">byte</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">Priority</span>);</pre> </p> <p> If you're not on a version of C# that supports <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/record">records</a> (which is also trivially true if you're not using C# at all), you can always define an immutable class by hand. It just requires more <a href="https://buttondown.email/hillelwayne/archive/why-do-we-call-it-boilerplate-code/">boilerplate</a> code. </p> <p> <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> is going to be the <code>T</code> in the <code>IReadOnlyCollection&lt;T&gt;</code> implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;</pre> </p> <p> Since an invariant should always hold, it should also hold at initialization, so the <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> constructor must check that all is as it should be: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;[]&nbsp;priorities; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>(<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;[]&nbsp;<span style="font-weight:bold;color:#1f377f;">priorities</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">priorities</span>.<span style="font-weight:bold;color:#74531f;">Sum</span>(<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>.Priority)&nbsp;!=&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;The&nbsp;sum&nbsp;of&nbsp;all&nbsp;priorities&nbsp;must&nbsp;be&nbsp;100.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">priorities</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.priorities&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">priorities</span>; }</pre> </p> <p> The rest of the class is just the <code>IReadOnlyCollection&lt;T&gt;</code> implementation, which just delegates to the <code>priorities</code> field. </p> <p> That's it, really. That's the API. We're done. </p> <h3 id="8b7c8d67ea144bcfbdc10823bee1e770"> Projection <a href="#8b7c8d67ea144bcfbdc10823bee1e770">#</a> </h3> <p> <em>But,</em> you may ask, <em>how does one edit such a collection?</em> </p> <p> <img src="/content/binary/immutable-edit-comic.jpg" alt="Bob: How do I edit an immutable object Other man: You don't, because it's a persistent data structure. Bob: Fine, it's persist. How do I edit it? Other man: You make it a monomorphic functor and compose it with projections. Bob: Did you just tell me to go fuck myself? Other man: I believe I did, Bob."> </p> <p> (Comic originally by John Muellerleile.) </p> <p> Humour aside, you don't edit an immutable object, but rather make a new object from a previous one. Most modern languages now come with built-in collection-projection APIs; in .NET, it's called <a href="https://learn.microsoft.com/dotnet/csharp/linq/">LINQ</a>. Here's an example. You begin with a collection with two items: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">pc</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:blue;">string</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;60), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;40));</pre> </p> <p> You'd now like to add a third item with priority 20: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newPriority</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;baz&quot;</span>,&nbsp;20);</pre> </p> <p> How should you make room for this new item? One option is to evenly reduce each of the existing priorities: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reduction</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">newPriority</span>.Priority&nbsp;/&nbsp;<span style="font-weight:bold;color:#1f377f;">pc</span>.Count; <span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reduced</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">pc</span> &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Select</span>(<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;<span style="color:blue;">with</span>&nbsp;{&nbsp;Priority&nbsp;=&nbsp;(<span style="color:blue;">byte</span>)(<span style="font-weight:bold;color:#1f377f;">p</span>.Priority&nbsp;-&nbsp;<span style="font-weight:bold;color:#1f377f;">reduction</span>)&nbsp;});</pre> </p> <p> Notice that while the sum of priorities in <code>reduced</code> no longer sum to 100, it's okay, because <code>reduced</code> isn't a <code>PriorityCollection</code> object. It's just an <code><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:blue;">string</span>&gt;&gt;</code>. </p> <p> You can now <a href="https://learn.microsoft.com/dotnet/api/system.linq.enumerable.append">Append</a> the <code>newPriority</code> to the <code>reduced</code> sequence and repackage that in a <code>PriorityCollection</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">adjusted</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:blue;">string</span>&gt;(<span style="font-weight:bold;color:#1f377f;">reduced</span>.<span style="font-weight:bold;color:#74531f;">Append</span>(<span style="font-weight:bold;color:#1f377f;">newPriority</span>).<span style="font-weight:bold;color:#74531f;">ToArray</span>());</pre> </p> <p> Like the original <code>pc</code> object, the <code>adjusted</code> object is valid upon construction, and since its immutable, it'll remain valid. </p> <h3 id="53475fec07ce46929951858e3d5be5ba"> Edit <a href="#53475fec07ce46929951858e3d5be5ba">#</a> </h3> <p> If you think this process of unwrapping and rewrapping seems cumbersome, we can make it a bit more palatable by defining a wrapping <code>Edit</code> function, similar to the one in the <a href="/2024/06/24/a-mutable-priority-collection">previous article</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Edit</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">edit</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="font-weight:bold;color:#1f377f;">edit</span>(<span style="color:blue;">this</span>).<span style="font-weight:bold;color:#74531f;">ToArray</span>()); }</pre> </p> <p> You can now write code equivalent to the above example like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">adjusted</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">pc</span>.<span style="font-weight:bold;color:#74531f;">Edit</span>(<span style="font-weight:bold;color:#1f377f;">col</span>&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reduced</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">col</span>.<span style="font-weight:bold;color:#74531f;">Select</span>(<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;<span style="color:blue;">with</span>&nbsp;{&nbsp;Priority&nbsp;=&nbsp;(<span style="color:blue;">byte</span>)(<span style="font-weight:bold;color:#1f377f;">p</span>.Priority&nbsp;-&nbsp;<span style="font-weight:bold;color:#1f377f;">reduction</span>)&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reduced</span>.<span style="font-weight:bold;color:#74531f;">Append</span>(<span style="font-weight:bold;color:#1f377f;">newPriority</span>); });</pre> </p> <p> I'm not sure it's much of an improvement, though. </p> <h3 id="12f521756ae949f1bafc8294074446b4"> Using the right tool for the job <a href="#12f521756ae949f1bafc8294074446b4">#</a> </h3> <p> While C# over the years has gained some functional-programming features, it's originally an object-oriented language, and working with immutable values may still seem a bit cumbersome. If so, consider using a language natively designed for this style of programming. On .NET, <a href="https://fsharp.org/">F#</a> is the obvious choice. </p> <p> First, you define the required types: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">&#39;a</span>&gt;&nbsp;=&nbsp;{&nbsp;Item:&nbsp;<span style="color:#2b91af;">&#39;a</span>;&nbsp;Priority:&nbsp;<span style="color:#2b91af;">byte</span>&nbsp;} <span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">PriorityList</span>&nbsp;=&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">PriorityList</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">string</span>&gt;&nbsp;<span style="color:#2b91af;">list</span></pre> </p> <p> Notice that <code>PriorityList</code> has a <code>private</code> constructor, so that client code can't just create any value. The type should protect its invariants, since <a href="/2022/10/24/encapsulation-in-functional-programming">encapsulation is also relevant in functional programming</a>. Since client code can't directly create <code>PriorityList</code> objects, you instead supply a function for that purpose: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:#2b91af;">PriorityList</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:#74531f;">tryCreate</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">priorities</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">priorities</span>&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.<span style="color:#74531f;">sumBy</span>&nbsp;(_.Priority)&nbsp;=&nbsp;100uy &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:#2b91af;">Some</span>&nbsp;(<span style="color:#2b91af;">PriorityList</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">priorities</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:#2b91af;">None</span></pre> </p> <p> That's really it, although you also need a way to work with the data. We supply two alternatives that correspond to the above C#: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;edit&nbsp;f&nbsp;(PriorityList&nbsp;priorities)&nbsp;=&nbsp;f&nbsp;priorities&nbsp;|&gt;&nbsp;tryCreate <span style="color:blue;">let</span>&nbsp;toList&nbsp;(PriorityList&nbsp;priorities)&nbsp;=&nbsp;priorities</pre> </p> <p> These functions are also defined on the <code>PriorityList</code> module. </p> <p> Here's the same adjustment example as shown above in C#: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;pl&nbsp;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;{&nbsp;Item&nbsp;=&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>;&nbsp;Priority&nbsp;=&nbsp;60uy&nbsp;};&nbsp;{&nbsp;Item&nbsp;=&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>;&nbsp;Priority&nbsp;=&nbsp;40uy&nbsp;}&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">PriorityList</span>.<span style="color:#74531f;">tryCreate</span> <span style="color:blue;">let</span>&nbsp;newPriority&nbsp;=&nbsp;{&nbsp;Item&nbsp;=&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>;&nbsp;Priority&nbsp;=&nbsp;20uy&nbsp;} <span style="color:blue;">let</span>&nbsp;adjusted&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;pl &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Option</span>.<span style="color:#74531f;">bind</span>&nbsp;(<span style="color:#2b91af;">PriorityList</span>.<span style="color:#74531f;">edit</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.<span style="color:#74531f;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;<span style="color:blue;">with</span>&nbsp;Priority&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>.Priority&nbsp;-&nbsp;(newPriority.Priority&nbsp;/&nbsp;<span style="color:#74531f;">byte</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>.Length)&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.<span style="color:#74531f;">append</span>&nbsp;[&nbsp;newPriority&nbsp;]))</pre> </p> <p> The entire F# definition is 15 lines of code, including namespace declaration and blank lines. </p> <h3 id="653bf17ab4cc4353b165638d497b74f1"> Conclusion <a href="#653bf17ab4cc4353b165638d497b74f1">#</a> </h3> <p> With an immutable data structure, you only need to check the invariants upon creation. Invariants therefore become preconditions. Once a value is created in a valid state, it stays valid because it never changes state. </p> <p> If you're having trouble maintaining invariants in an object-oriented design, try making the object immutable. It's likely to make it easier to attain good encapsulation. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A mutable priority collection https://blog.ploeh.dk/2024/06/24/a-mutable-priority-collection 2024-06-24T17:59:00+00:00 Mark Seemann <div id="post"> <p> <em>An encapsulated, albeit overly complicated, implementation.</em> </p> <p> This is the second in a <a href="/2024/06/12/simpler-encapsulation-with-immutability">series of articles about encapsulation and immutability</a>. In the next article, you'll see how immutability makes encapsulation easier, but in order to appreciate that, you should see the alternative. This article, then, shows a working, albeit overly complicated, implementation that does maintain its invariants. </p> <p> In the introductory article, I described the example problem in more details, but in short, the exercise is to develop a class that holds a collection of prioritized items, with the invariant that the priorities must always sum to 100. It should be impossible to leave the object in a state where that's not true. It's quite an illuminating exercise, so if you have the time, you should try it for yourself before reading on. </p> <h3 id="9f40c96077664ab7acbc2705d9e0d2ea"> Initialization <a href="#9f40c96077664ab7acbc2705d9e0d2ea">#</a> </h3> <p> As the <a href="/2024/06/17/a-failed-attempt-at-priority-collection-with-inheritance">previous article</a> demonstrated, inheriting directly from a base class seems like a dead end. Once you see the direction that I go in this article, you may argue that it'd be possible to also make that design work with an inherited collection. It may be, but I'm not convinced that it would improve anything. Thus, for this iteration, I decided to eschew inheritance. </p> <p> On the other hand, we need an API to <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">query</a> the object about its state, and I found that it made sense to implement the <a href="https://learn.microsoft.com/dotnet/api/system.collections.generic.ireadonlydictionary-2">IReadOnlyDictionary</a> interface. </p> <p> As before, invariants are statements that are always true about an object, and that includes a newly initialized object. Thus, the <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class should require enough information to safely initialize. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReadOnlyDictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;<span style="color:blue;">where</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;:&nbsp;<span style="color:blue;">notnull</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;dict; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">initial</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;{&nbsp;{&nbsp;<span style="font-weight:bold;color:#1f377f;">initial</span>,&nbsp;100&nbsp;}&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;IReadOnlyDictionary&nbsp;implemented&nbsp;by&nbsp;delegating&nbsp;to&nbsp;dict&nbsp;field...</span> }</pre> </p> <p> Several design decisions are different from the previous article. This design has no <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class. Instead it treats the item (of type <code>T</code>) as a dictionary key, and the priority as the value. The most important motivation for this design decision was that this enables me to avoid the 'leaf node mutation' problem that I demonstrated in the previous article. Notice how, while the general design in this iteration will be object-oriented and mutable, I already take advantage of a bit of immutability to make the design simpler and safer. </p> <p> Another difference is that you can't initialize a <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> object with a list. Instead, you only need to tell the constructor what the <code>initial</code> item is. The constructor will then infer that, since this is the only item so far, its priority must be 100. It can't be anything else, because that would violate the invariant. Thus, no assertion is required in the constructor. </p> <h3 id="f3c5abe45ba949e69c349dc8e21e959a"> Mutation API <a href="#f3c5abe45ba949e69c349dc8e21e959a">#</a> </h3> <p> So far, the code only implements the <code>IReadOnlyDictionary</code> API, so we need to add some methods that will enable us to add new items and so on. As a start, we can add methods to add, remove, or update items: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">key</span>,&nbsp;<span style="color:blue;">byte</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(dict.<span style="font-weight:bold;color:#74531f;">Append</span>(<span style="color:#2b91af;">KeyValuePair</span>.<span style="color:#74531f;">Create</span>(<span style="font-weight:bold;color:#1f377f;">key</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>))); &nbsp;&nbsp;&nbsp;&nbsp;dict.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">key</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">key</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(dict.<span style="font-weight:bold;color:#74531f;">Where</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;=&gt;&nbsp;!<span style="font-weight:bold;color:#1f377f;">kvp</span>.Key.<span style="font-weight:bold;color:#74531f;">Equals</span>(<span style="font-weight:bold;color:#1f377f;">key</span>))); &nbsp;&nbsp;&nbsp;&nbsp;dict.<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="font-weight:bold;color:#1f377f;">key</span>); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;<span style="color:blue;">this</span>[<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">key</span>] { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;dict[<span style="font-weight:bold;color:#1f377f;">key</span>];&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">set</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&nbsp;dict.<span style="font-weight:bold;color:#74531f;">ToDictionary</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Key,&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>[<span style="font-weight:bold;color:#1f377f;">key</span>]&nbsp;=&nbsp;<span style="color:blue;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(<span style="font-weight:bold;color:#1f377f;">l</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict[<span style="font-weight:bold;color:#1f377f;">key</span>]&nbsp;=&nbsp;<span style="color:blue;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I'm not going to show the <code>AssertInvariants</code> helper method yet, since it's going to change anyway. </p> <p> At this point, the implementation suffers from the same problem as the example in the previous article. While you can add new items, you can only add an item with priority 0. You can only remove items if they have priority 0. And you can only 'update' an item if you set the priority to the same value as it already had. </p> <p> We need to be able to add new items, change their priorities, and so on. How do we get around the above problem, without breaking the invariant? </p> <h3 id="f9400ef096f94b15b4c32ae2f35ba000"> Edit mode <a href="#f9400ef096f94b15b4c32ae2f35ba000">#</a> </h3> <p> One way out of this conundrum is introduce a kind of 'edit mode'. The idea is to temporarily turn off the maintenance of the invariant for long enough to allow edits. </p> <p> Af first glance, such an idea seems to go against the very definition of an invariant. After all, an invariant is a statement about the object that is <em>always</em> true. If you allow a client developer to turn off that guarantee, then, clearly, the guarantee is gone. Guarantees only work if you can trust them, and you can't trust them if they can be cancelled. </p> <p> That idea in itself doesn't work, but if we can somehow encapsulate such an 'edit action' in an isolated scope that either succeeds or fails in its entirety, we may be getting somewhere. It's an idea similar to <a href="https://en.wikipedia.org/wiki/Unit_of_work">Unit of Work</a>, although here we're not involving an actual database. Still, an 'edit action' is a kind of in-memory transaction. </p> <p> For didactic reasons, I'll move toward that design in a series of step, where the intermediate steps fail to maintain the invariant. We'll get there eventually. The first step is to introduce 'edit mode'. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;isEditing;</pre> </p> <p> While I could have made that flag <code>public</code>, I found it more natural to wrap access to it in two methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BeginEdit</span>() { &nbsp;&nbsp;&nbsp;&nbsp;isEditing&nbsp;=&nbsp;<span style="color:blue;">true</span>; } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">EndEdit</span>() { &nbsp;&nbsp;&nbsp;&nbsp;isEditing&nbsp;=&nbsp;<span style="color:blue;">false</span>; }</pre> </p> <p> This still doesn't accomplishes anything in itself, but the final change in this step is to change the assertion so that it respects the flag: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!isEditing&nbsp;&amp;&amp;&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>.<span style="font-weight:bold;color:#74531f;">Sum</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Value)&nbsp;!=&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvalidOperationException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;The&nbsp;sum&nbsp;of&nbsp;all&nbsp;values&nbsp;must&nbsp;be&nbsp;100.&quot;</span>); }</pre> </p> <p> Finally, you can add or change priorities, as this little <a href="https://fsharp.org/">F#</a> example shows: </p> <p> <pre>sut.BeginEdit&nbsp;() sut[<span style="color:#a31515;">&quot;foo&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;50uy sut[<span style="color:#a31515;">&quot;bar&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;50uy sut.EndEdit&nbsp;()</pre> </p> <p> Even if you nominally 'don't read F#', this little example is almost like C# without semicolons. The <code><span style="color:blue;">&lt;-</span></code> arrow is F#'s mutation or assignment operator, which in C# would be <code>=</code>, and the <code>uy</code> suffix is <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/literals">the F# way of stating that the literal is a <code>byte</code></a>. </p> <p> The above example is well-behaved because the final state of the object is valid. The priorities sum to 100. Even so, no code in <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> actually checks that, so we could trivially leave the object in an invalid state. </p> <h3 id="bcd53701e2214e99adcb1090acf3536a"> Assert invariant at end of edit <a href="#bcd53701e2214e99adcb1090acf3536a">#</a> </h3> <p> The first step toward remedying that problem is to add a check to the <code>EndEdit</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">EndEdit</span>() { &nbsp;&nbsp;&nbsp;&nbsp;isEditing&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(dict); }</pre> </p> <p> The class is still not effectively protecting its invariants, because a client developer could forget to call <code>EndEdit</code>, or client code might pass around a collection in edit mode. Other code, receiving such an object as an argument, may not know whether or not it's in edit mode, so again, doesn't know if it can trust it. </p> <p> We'll return to that problem shortly, but first, there's another, perhaps more pressing issue that we should attend to. </p> <h3 id="45cbf95701e14f08974cb5f1a585ad79"> Edit dictionary <a href="#45cbf95701e14f08974cb5f1a585ad79">#</a> </h3> <p> The current implementation directly edits the collection, and even if a client developer remembers to call <code>EndEdit</code>, other code, higher up in the call stack could circumvent the check and leave the object in an invalid state. Not that I expect client developers to be deliberately malicious, but the notion that someone might wrap a method call in a <code>try-catch</code> block seems realistic. </p> <p> The following F# unit test demonstrates the issue: </p> <p> <pre>[&lt;<span style="color:#2b91af;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:#74531f;">``Attempt&nbsp;to&nbsp;circumvent``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">string</span>&gt;&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.<span style="font-weight:bold;color:#74531f;">BeginEdit</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>[<span style="color:#a31515;">&quot;foo&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;50uy &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>[<span style="color:#a31515;">&quot;bar&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;48uy &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.<span style="font-weight:bold;color:#74531f;">EndEdit</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">with</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;100uy&nbsp;=!&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>[<span style="color:#a31515;">&quot;foo&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">test</span>&nbsp;&lt;@&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.<span style="font-weight:bold;color:#74531f;">ContainsKey</span>&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>&nbsp;|&gt;&nbsp;<span style="color:#74531f;">not</span>&nbsp;@&gt;</pre> </p> <p> Again, let me walk you through it in case you're unfamiliar with F#. </p> <p> The <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/exception-handling/the-try-with-expression">try-with</a> block works just like C# <code>try-catch</code> blocks. Inside of that <code>try-with</code> block, the test enters edit mode, changes the values in such a way that the sum of them is 98, and then calls <code>EndEdit</code>. While <code>EndEdit</code> throws an exception, those four lines of code are wrapped in a <code>try-with</code> block that suppresses all exceptions. </p> <p> The test attempts to verify that, since the edit failed, the <code>"foo"</code> value should be 100, and there should be no <code>"bar"</code> value. This turns out not to be the case. The test fails. The edits persist, even though <code>EndEdit</code> throws an exception, because there's no roll-back. </p> <p> You could probably resolve that defect in various ways, but I chose to address it by introducing two, instead of one, backing dictionaries. One holds the data that always maintains the invariant, and the other is a temporary dictionary for editing. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;current; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;encapsulated; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;editable; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;isEditing; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">initial</span>) { &nbsp;&nbsp;&nbsp;&nbsp;encapsulated&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;{&nbsp;{&nbsp;<span style="font-weight:bold;color:#1f377f;">initial</span>,&nbsp;100&nbsp;}&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;editable&nbsp;=&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;encapsulated; }</pre> </p> <p> There are two dictionaries: <code>encapsulated</code> holds the always-valid list of priorities, while <code>editable</code> is the dictionary that client code will be editing when in edit mode. Finally, <code>current</code> is either of these: <code>editable</code> when the object is in edit mode, and <code>encapsulated</code> when it's not. Most of the existing code shown so far now uses <code>current</code>, which before was called <code>dict</code>. The important changes are in <code>BeginEdit</code> and <code>EndEdit</code>. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BeginEdit</span>() { &nbsp;&nbsp;&nbsp;&nbsp;isEditing&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;editable.<span style="font-weight:bold;color:#74531f;">Clear</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;current) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;editable.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>.Key,&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Value); &nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;editable; }</pre> </p> <p> Besides setting the <code>isEditing</code> flag, <code>BeginEdit</code> now copies all data from <code>current</code> to <code>editable</code>, and then sets <code>current</code> to <code>editable</code>. Keep in mind that <code>encapsulated</code> still holds the original, valid values. </p> <p> Now that I'm writing this, I'm not even sure if this method is re-entrant, in the following sense: What happens if client code calls <code>BeginEdit</code>, makes some changes, and then calls <code>BeginEdit</code> again? It's questions like these that I don't feel intelligent enough to feel safe that I always answer correctly. That's why I like functional programming better. I don't have to think so hard. </p> <p> Anyway, this will soon become irrelevant, since <code>BeginEdit</code> and <code>EndEdit</code> will eventually become <code>private</code> methods. </p> <p> The <code>EndEdit</code> method performs the inverse manoeuvre: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">EndEdit</span>() { &nbsp;&nbsp;&nbsp;&nbsp;isEditing&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(current); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encapsulated.<span style="font-weight:bold;color:#74531f;">Clear</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;current) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encapsulated.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>.Key,&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;encapsulated; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">catch</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;encapsulated; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> It first checks the invariant, and only copies the edited values to the <code>encapsulated</code> dictionary if the invariant still holds. Otherwise, it restores the original <code>encapsulated</code> values and rethrows the exception. </p> <p> This helps to make the nature of editing 'transactional' in nature, but it doesn't address the issue that the collection is in an invalid state during editing, or that a client developer may forget to call <code>EndEdit</code>. </p> <h3 id="d53f9fe83e944d1bbd58d478f9fc27bc"> Edit action <a href="#d53f9fe83e944d1bbd58d478f9fc27bc">#</a> </h3> <p> As the next step towards addressing that problem, we may now introduce a 'wrapper method' for that little object protocol: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Edit</span>(<span style="color:#2b91af;">Action</span>&lt;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">editAction</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">BeginEdit</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">editAction</span>(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">EndEdit</span>(); }</pre> </p> <p> As you can see, it just wraps that little call sequence so that you don't have to remember to call <code>BeginEdit</code> and <code>EndEdit</code>. My F# test code comes with this example: </p> <p> <pre>sut.Edit&nbsp;(<span style="color:blue;">fun</span>&nbsp;col&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;col[<span style="color:#a31515;">&quot;bar&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;55uy &nbsp;&nbsp;&nbsp;&nbsp;col[<span style="color:#a31515;">&quot;baz&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;45uy &nbsp;&nbsp;&nbsp;&nbsp;col.Remove&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span> )</pre> </p> <p> The <code><span style="color:blue;">fun</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">col</span>&nbsp;<span style="color:blue;">-&gt;</span></code> part is just F# syntax for a lambda expression. In C#, you'd write it as <code>col =&gt;</code>. </p> <p> We're close to a solution. What remains is to make <code>BeginEdit</code> and <code>EndEdit</code> <code>private</code>. This means that client code can only edit a <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> object through the <code>Edit</code> method. </p> <h3 id="b04cc69a08c749f39d8e9276e444046a"> Replace action with interface <a href="#b04cc69a08c749f39d8e9276e444046a">#</a> </h3> <p> You may complain that this solution isn't properly object-oriented, since it makes use of <a href="https://learn.microsoft.com/dotnet/api/system.action-1">Action&lt;T&gt;</a> and requires that client code uses lambda expressions. </p> <p> We can easily fix that. </p> <p> Instead of the action, you can introduce a <a href="https://en.wikipedia.org/wiki/Command_pattern">Command</a> interface with the same signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPriorityEditor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="color:blue;">where</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;:&nbsp;<span style="color:blue;">notnull</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">EditPriorities</span>(<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">priorities</span>); }</pre> </p> <p> Next, change the <code>Edit</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Edit</span>(<span style="color:#2b91af;">IPriorityEditor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">editor</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">BeginEdit</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">editor</span>.<span style="font-weight:bold;color:#74531f;">EditPriorities</span>(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">EndEdit</span>(); }</pre> </p> <p> Now you have a nice, object-oriented design, with no lambda expressions in sight. </p> <h3 id="e15b63b0a3ef4f229caedc15a2b64f39"> Full code dump <a href="#e15b63b0a3ef4f229caedc15a2b64f39">#</a> </h3> <p> The final code is complex enough that it's easy to lose track of what it looks like, as I walk through my process. To make it easer, here's the full code for the collection class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReadOnlyDictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;:&nbsp;<span style="color:blue;">notnull</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;current; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;encapsulated; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;editable; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;isEditing; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">initial</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encapsulated&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;{&nbsp;{&nbsp;<span style="font-weight:bold;color:#1f377f;">initial</span>,&nbsp;100&nbsp;}&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;editable&nbsp;=&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;encapsulated; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">key</span>,&nbsp;<span style="color:blue;">byte</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(current.<span style="font-weight:bold;color:#74531f;">Append</span>(<span style="color:#2b91af;">KeyValuePair</span>.<span style="color:#74531f;">Create</span>(<span style="font-weight:bold;color:#1f377f;">key</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">key</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">key</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(current.<span style="font-weight:bold;color:#74531f;">Where</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;=&gt;&nbsp;!<span style="font-weight:bold;color:#1f377f;">kvp</span>.Key.<span style="font-weight:bold;color:#74531f;">Equals</span>(<span style="font-weight:bold;color:#1f377f;">key</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current.<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="font-weight:bold;color:#1f377f;">key</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;<span style="color:blue;">this</span>[<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">key</span>] &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;current[<span style="font-weight:bold;color:#1f377f;">key</span>];&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">set</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&nbsp;current.<span style="font-weight:bold;color:#74531f;">ToDictionary</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Key,&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>[<span style="font-weight:bold;color:#1f377f;">key</span>]&nbsp;=&nbsp;<span style="color:blue;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(<span style="font-weight:bold;color:#1f377f;">l</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current[<span style="font-weight:bold;color:#1f377f;">key</span>]&nbsp;=&nbsp;<span style="color:blue;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Edit</span>(<span style="color:#2b91af;">IPriorityEditor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">editor</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">BeginEdit</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">editor</span>.<span style="font-weight:bold;color:#74531f;">EditPriorities</span>(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">EndEdit</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BeginEdit</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isEditing&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;editable.<span style="font-weight:bold;color:#74531f;">Clear</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;current) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;editable.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>.Key,&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;editable; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">EndEdit</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isEditing&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(current); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encapsulated.<span style="font-weight:bold;color:#74531f;">Clear</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;current) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encapsulated.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>.Key,&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;encapsulated; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">catch</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;encapsulated; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AssertInvariants</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!isEditing&nbsp;&amp;&amp;&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>.<span style="font-weight:bold;color:#74531f;">Sum</span>(<span style="font-weight:bold;color:#1f377f;">kvp</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">kvp</span>.Value)&nbsp;!=&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvalidOperationException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;The&nbsp;sum&nbsp;of&nbsp;all&nbsp;values&nbsp;must&nbsp;be&nbsp;100.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Keys &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;current.Keys;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">byte</span>&gt;&nbsp;Values &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;current.Values;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Count &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;current.Count;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ContainsKey</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">key</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;current.<span style="font-weight:bold;color:#74531f;">ContainsKey</span>(<span style="font-weight:bold;color:#1f377f;">key</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerator</span>&lt;<span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetEnumerator</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;current.<span style="font-weight:bold;color:#74531f;">GetEnumerator</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryGetValue</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">key</span>,&nbsp;[<span style="color:#2b91af;">MaybeNullWhen</span>(<span style="color:blue;">false</span>)]&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;current.<span style="font-weight:bold;color:#74531f;">TryGetValue</span>(<span style="font-weight:bold;color:#1f377f;">key</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerator</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>.<span style="font-weight:bold;color:#74531f;">GetEnumerator</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetEnumerator</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code><span style="color:#2b91af;">IPriorityEditor</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> interface remains as shown above. </p> <h3 id="7994e307ceaf4857901378fe711f3ceb"> Conclusion <a href="#7994e307ceaf4857901378fe711f3ceb">#</a> </h3> <p> Given how simple the problem is, this solution is surprisingly complicated, and I'm fairly sure that it's not even thread-safe. </p> <p> At least it does, as far as I can tell, protect the invariant that the sum of priorities must always be exactly 100. Even so, it's just complicated enough that I wouldn't be surprised if a bug is lurking somewhere. It'd be nice if a simpler design existed. </p> <p> <strong>Next:</strong> <a href="/2024/07/01/an-immutable-priority-collection">An immutable priority collection</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="667d3faaebff4009bfffcd15612280ac"> <div class="comment-author">Joker_vD <a href="#667d3faaebff4009bfffcd15612280ac">#</a></div> <div class="comment-content"> <p> Where does the notion come that a data structure invariant has to be true <i>at all times</i>? I am fairly certain that it's only required to be true at "quiescent" points of executions. That is, just as the loop invariant is only required to hold before and after each loop step but not inside the loop step, so is the data structure invariant is only required to hold before and after each invocation of its public methods. </p> <p> This definition actually has an interesting quirk which is absent in the loop invariant: a data structure's method can't, generally speaking, call other public methods of the very same data structure because the invariant might not hold at this particular point of execution! I've been personally bitten by this a couple of times, and I've seen others tripping over this subtle point as well. You yourself notice it when you muse about the re-entrancy of the <code>BeginEdit</code> method. </p> <p> Now, this particular problem is quite similar to the problem with inner iteration, and can be solved the same way, with the outer editor, as you've done, although I would have probably provided each editor with its own, separate <code>editable</code> dictionary because right now, the editors cannot nest/compose... but that'd complicate implementation even further. </p> </div> <div class="comment-date">2024-07-03 22:19 UTC</div> </div> <div class="comment" id="6a880573a3424f37b74bc78a8276d441"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6a880573a3424f37b74bc78a8276d441">#</a></div> <div class="comment-content"> <p> Thank you for writing. As so many other areas of knowledge, the wider field of software development suffers from the problem of overlapping or overloaded terminology. The word <em>invariant</em> is just one of them. In this context, <em>invariant</em> doesn't refer to loop invariants, or any other kind of invariants used in algorithmic analysis. </p> <p> As outlined in the <a href="/2024/06/12/simpler-encapsulation-with-immutability">introduction article</a>, when discussing <a href="/encapsulation-and-solid">encapsulation</a>, I follow <a href="/ref/oosc">Object-Oriented Software Construction</a> (OOSC). In that seminal work, <a href="https://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Meyer</a> proposes the notion of design-by-contract, and specifically decomposes a contract into three parts: preconditions, invariants, and postconditions. </p> <p> Having actually read the book, I'm well aware that it uses <a href="https://en.wikipedia.org/wiki/Eiffel_(programming_language)">Eiffel</a> as an exemplar of the concept. This has led many readers to conflate design-by-contract with Eiffel, and (in yet another logical derailment) conclude that it doesn't apply to, say, Java or C# programming. </p> <p> It turns out, however, to transfer easily to other languages, and it's a concept with much practical potential. </p> <p> A major problem with object-oriented design is that most ideas about good design are too 'fluffy' to be of immediate use to most developers. Take the <a href="https://en.wikipedia.org/wiki/Single-responsibility_principle">Single Responsibility Principle</a> (SRP) as an example. It's seductively easy to grasp the overall idea, but turns out to be hard to apply. Being able to identify <em>reasons to change</em> requires more programming experience than most people have. Or rather, the SRP is mostly useful to programmers who already have that experience. Being too 'fluffy', it's not a good learning tool. </p> <p> I've spent quite some time with development organizations and individual programmers eager to learn, but struggling to find useful, concrete design rules. The decomposition of encapsulation into preconditions, invariants, and postconditions works well as a concrete, almost quantifiable heuristic. </p> <p> Does it encompass everything that encapsulation means? Probably not, but it's by far the most effective heuristic that I've found so far. </p> <p> Since I'm currently travelling, I don't have my copy of OOSC with me, but as far as I remember, the notion that an invariant should be true at all times originates there. </p> <p> In any case, if an invariant doesn't always hold, then of what value is it? The whole idea behind encapsulation (as I read Meyer) is that client developers should be able to use 'objects' without having intimate knowledge of their implementation details. The use of <em>contracts</em> proposes to achieve that ideal by decoupling affordances from implementation details by condensing the legal protocol between object and client code into a contract. This means that a client developer, when making programming decisions, should be able to trust that certain guarantees stipulated by a contract always hold. If a client developer can't trust those guarantees, they aren't really guarantees. </p> <blockquote> <p> "the data structure invariant is only required to hold before and after each invocation of its public methods" </p> </blockquote> <p> I can see how a literal reading of OOSC may leave one with that impression. One must keep in mind, however, that the book was written in the eighties, at a time when multithreading wasn't much of a concern. (Incidentally, this is an omission that also mars a much later book on API design, the first edition of the .NET <a href="/ref/fdg">Framework Design Guidelines</a>.) </p> <p> In modern code, concurrent execution is a real possibility, so is at least worth keeping in mind. I'm still most familiar with the .NET ecosystem, and in it, there are plenty of classes that are documented as <em>not</em> being thread-safe. You could say that such a statement is part of the contract, in which case what you wrote is true: The invariant is only required to hold before and after each method invocation. </p> <p> If, on the other hand, you want to make the code thread-safe, you must be more rigorous than that. Then an invariant must truly always hold. </p> <p> This is, of course, a design decision one may take. Just don't bother with thread-safety if it's not important. </p> <p> Still, the overall thrust of this article series is that immutability makes encapsulation much simpler. This is also true when it comes to concurrency. Immutable data structures are automatically thread-safe. </p> </div> <div class="comment-date">2024-07-06 8:07 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A failed attempt at priority collection with inheritance https://blog.ploeh.dk/2024/06/17/a-failed-attempt-at-priority-collection-with-inheritance 2024-06-17T08:04:00+00:00 Mark Seemann <div id="post"> <p> <em>An instructive dead end.</em> </p> <p> This article is part of <a href="/2024/06/12/simpler-encapsulation-with-immutability">a short series on encapsulation and immutability</a>. As the introductory article claims, object mutation makes it difficult to maintain invariants. In order to demonstrate the problem, I deliberately set out to do it wrong, and report on the result. </p> <p> In subsequent articles in this series I will then show one way you can maintain the invariants in the face of mutation, as well as how much easier everything becomes if you choose an immutable design. </p> <p> For now, however, I'll pretend to be naive and see how far I can get with that. </p> <p> In the first article, I described the example problem in more details, but in short, the exercise is to develop a class that holds a collection of prioritized items, with the invariant that the priorities must always sum to 100. It should be impossible to leave the object in a state where that's not true. It's quite an illuminating exercise, so if you have the time, you should try it for yourself before reading on. </p> <h3 id="e5060f80472a46a7b5aac3061558f993"> Initialization <a href="#e5060f80472a46a7b5aac3061558f993">#</a> </h3> <p> In object-oriented design it's common to inherit from a base class. Since I'll try to implement a collection of prioritized items, it seems natural to inherit from <a href="https://learn.microsoft.com/dotnet/api/system.collections.objectmodel.collection-1">Collection&lt;T&gt;</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">Collection</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;</pre> </p> <p> Of course, I also had to define <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">item</span>,&nbsp;<span style="color:blue;">byte</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">priority</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">item</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Priority&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">priority</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;Priority&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> Since <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> is generic, it can be used to prioritize any kind of object. In the tests I wrote, however, I exclusively used strings. </p> <p> A priority is a number between 0 and 100, so I chose to represent that with a <code>byte</code>. Not that this strongly protects invariants, because values can still exceed 100, but on the other hand, there's no reason to use a 32-bit integer to model a number between 0 and 100. </p> <p> Now that I write this text, I realize that I could have added a Guard Clause to the <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> constructor to enforce that precondition, but as you can tell, I didn't think of doing that. This omission, however, doesn't change the conclusion, because the problems that we'll run into stems from another source. </p> <p> In any case, just inheriting from <code><span style="color:#2b91af;">Collection</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;</code> isn't enough to guarantee the invariant that the sum of priorities must be 100. An invariant must always hold, even for a newly initialized object. Thus, we need something like this ensure that this is the case: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">Collection</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PriorityCollection</span>(<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;[]&nbsp;<span style="font-weight:bold;color:#1f377f;">priorities</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<span style="color:blue;">base</span>(<span style="font-weight:bold;color:#1f377f;">priorities</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AssertSumIsOneHundred</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AssertSumIsOneHundred</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="color:blue;">this</span>.<span style="font-weight:bold;color:#74531f;">Sum</span>(<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>.Priority)&nbsp;!=&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvalidOperationException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;The&nbsp;sum&nbsp;of&nbsp;all&nbsp;priorities&nbsp;must&nbsp;be&nbsp;100.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> So far, there's no real need to have a separate <code>AssertSumIsOneHundred</code> helper method; I could have kept that check in the constructor, and that would have been simpler. I did, however, anticipate that I'd need the helper method in other parts of the code base. As it turned out, I did, but not without having to change it. </p> <h3 id="1326a8ef64124d138a084c4511f3899a"> Protecting overrides <a href="#1326a8ef64124d138a084c4511f3899a">#</a> </h3> <p> The <code>Collection&lt;T&gt;</code> base class offers normal collection methods like <a href="https://learn.microsoft.com/dotnet/api/system.collections.objectmodel.collection-1.add">Add</a>, <a href="https://learn.microsoft.com/dotnet/api/system.collections.objectmodel.collection-1.insert">Insert</a>, <a href="https://learn.microsoft.com/dotnet/api/system.collections.objectmodel.collection-1.remove">Remove</a> and so on. The default implementation allows client code to make arbitrary changes to the collection, including <a href="https://learn.microsoft.com/dotnet/api/system.collections.objectmodel.collection-1.clear">clearing it</a>. The <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class can't allow that, because such edits could easily violate the invariants. </p> <p> <code>Collection&lt;T&gt;</code> is explicitly designed to be a base class, so it offers various <code>virtual</code> methods that inheritors can override to change the behaviour. In this case, this is necessary. </p> <p> As it turned out, I quickly realized that I had to change my assertion helper method to check the invariant in various cases: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">AssertSumIsOneHundred</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">priorities</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">priorities</span>.<span style="font-weight:bold;color:#74531f;">Sum</span>(<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>.Priority)&nbsp;!=&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvalidOperationException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;The&nbsp;sum&nbsp;of&nbsp;all&nbsp;priorities&nbsp;must&nbsp;be&nbsp;100.&quot;</span>); }</pre> </p> <p> By taking the sequence of <code>priorities</code> as an input argument, this enables me to simulate what would happen if I make a change to the actual collection, for example when adding an item to the collection: </p> <p> <pre><span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">InsertItem</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">index</span>,&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">item</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">AssertSumIsOneHundred</span>(<span style="color:blue;">this</span>.<span style="font-weight:bold;color:#74531f;">Append</span>(<span style="font-weight:bold;color:#1f377f;">item</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">base</span>.<span style="font-weight:bold;color:#74531f;">InsertItem</span>(<span style="font-weight:bold;color:#1f377f;">index</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">item</span>); }</pre> </p> <p> By using <a href="https://learn.microsoft.com/dotnet/api/system.linq.enumerable.append">Append</a>, the <code>InsertItem</code> method creates a sequence of values that simulates what the collection would look like if we add the candidate <code>item</code>. The <code>Append</code> function returns a new collection, so this operation doesn't change the actual <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code>. This only happens if we get past the assertion and call <code>InsertItem</code>. </p> <p> Likewise, I can protect the invariant in the other overrides: </p> <p> <pre><span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RemoveItem</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">index</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>.RemoveAt(<span style="font-weight:bold;color:#1f377f;">index</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">AssertSumIsOneHundred</span>(<span style="font-weight:bold;color:#1f377f;">l</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">base</span>.<span style="font-weight:bold;color:#74531f;">RemoveItem</span>(<span style="font-weight:bold;color:#1f377f;">index</span>); } <span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">SetItem</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">index</span>,&nbsp;Prioritized&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">item</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>[<span style="font-weight:bold;color:#1f377f;">index</span>]&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">item</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">AssertSumIsOneHundred</span>(<span style="font-weight:bold;color:#1f377f;">l</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">base</span>.<span style="font-weight:bold;color:#74531f;">SetItem</span>(<span style="font-weight:bold;color:#1f377f;">index</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">item</span>); }</pre> </p> <p> I can even use it in the implementation of <code>ClearItems</code>, although that may seem a tad redundant: </p> <p> <pre><span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ClearItems</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">AssertSumIsOneHundred</span>([]); }</pre> </p> <p> I could also just have thrown an exception directly from this method, since it's never okay to clear the collection. This would violate the invariant, because the sum of an empty collection of priorities is zero. </p> <p> As far as I recall, the entire API of <code>Collection&lt;T&gt;</code> is (transitively) based on those four <code>virtual</code> methods, so now that I've protected the invariant in all four, the <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class maintains the invariant, right? </p> <p> Not yet. See if you can spot the problem. </p> <p> There are, in fact, at least two remaining problems. One that we can recover from, and one that is insurmountable with this design. I'll get back to the serious problem later, but see if you can spot it already. </p> <h3 id="0179acb780534b558c947b35c7f9c137"> Leaf mutation <a href="#0179acb780534b558c947b35c7f9c137">#</a> </h3> <p> In the <a href="/2024/06/12/simpler-encapsulation-with-immutability">introductory article</a> I wrote: </p> <blockquote> <p> "If the mutation happens on a leaf node in an object graph, the leaf may have to notify its parent, so that the parent can recheck the invariants." </p> </blockquote> <p> I realize that this may sound abstract, but the current code presents a simple example. What happens if you change the <code>Priority</code> of an item after you've initialized the collection? </p> <p> Consider the following example. For various reasons, I wrote the examples (that is, the unit tests) for this exercise in <a href="https://fsharp.org/">F#</a>, but even if you're not an F# developer, you can probably understand what's going on. First, we create a <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">string</span>&gt;</code> object and use it to initialize a <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">string</span>&gt;</code> object <a href="/2020/11/30/name-by-role">named sut</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">item</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">string</span>&gt;&nbsp;(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;40uy) <span style="color:blue;">let</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">string</span>&gt;&nbsp;(<span style="font-weight:bold;color:#1f377f;">item</span>,&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">string</span>&gt;&nbsp;(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;60uy))</pre> </p> <p> The <code>item</code> has a priority of <code>40</code> (the <code>uy</code> suffix is <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/literals">the F# way of stating that the literal is a <code>byte</code></a>), and the other unnamed value has a priority of <code>60</code>, so all is good so far; the sum is 100. </p> <p> Since, however, <code>item</code> is a mutable object, we can now change its <code>Priority</code>: </p> <p> <pre><span style="font-weight:bold;color:#1f377f;">item</span>.Priority&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;50uy</pre> </p> <p> This changes <code>item.Priority</code> to 50, but since none of the four <code>virtual</code> base class methods of <code>Collection&lt;T&gt;</code> are involved, the <code>sut</code> never notices, the assertion never runs, and the object is now in an invalid state. </p> <p> That's what I meant when I discussed mutations in leaf nodes. You can think of a collection as a rather flat and boring <a href="https://en.wikipedia.org/wiki/Tree_(data_structure)">tree</a>. The collection object itself is the root, and each of the items are leaves, and no further nesting is allowed. </p> <p> When you edit a leaf, the root isn't automatically aware of such an event. You explicitly have to wire the object graph up so that this happens. </p> <h3 id="2edbce9f883448e89d118233bfceefef"> Event propagation <a href="#2edbce9f883448e89d118233bfceefef">#</a> </h3> <p> One possible way to address this issue is to take advantage of .NET's event system. If you're reading along, but you normally write in another language, you can also use the <a href="https://en.wikipedia.org/wiki/Observer_pattern">Observer pattern</a>, or even <a href="https://reactivex.io/">ReactiveX</a>. </p> <p> We need to have <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> raise events, and one option is to let it implement <a href="https://learn.microsoft.com/dotnet/api/system.componentmodel.inotifypropertychanging">INotifyPropertyChanging</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">INotifyPropertyChanging</span></pre> </p> <p> A <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> object can now raise its <code>PropertyChanging</code> event before accepting an edit: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;Priority { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;=&gt;&nbsp;priority; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">set</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(PropertyChanging&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PropertyChanging( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PriorityChangingEventArgs</span>(<span style="color:blue;">value</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;priority&nbsp;=&nbsp;<span style="color:blue;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> where <code>PriorityChangingEventArgs</code> is a little helper class that carries the proposed <code>value</code> around: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PriorityChangingEventArgs</span>(<span style="color:blue;">byte</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">proposal</span>) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<span style="color:#2b91af;">PropertyChangingEventArgs</span>(<span style="color:blue;">nameof</span>(Priority)) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;Proposal&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">proposal</span>; }</pre> </p> <p> A <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> object can now subscribe to that event on each of the values it keeps track of, so that it can protect the invariant against leaf node mutations. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Priority_PropertyChanging</span>(<span style="color:blue;">object</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">sender</span>,&nbsp;<span style="color:#2b91af;">PropertyChangingEventArgs</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">e</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">sender</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">p</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">e</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;.<span style="color:#2b91af;">PriorityChangingEventArgs</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">pcea</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.<span style="font-weight:bold;color:#74531f;">ToList</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>[<span style="font-weight:bold;color:#1f377f;">l</span>.<span style="font-weight:bold;color:#74531f;">IndexOf</span>(<span style="font-weight:bold;color:#1f377f;">p</span>)]&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="font-weight:bold;color:#1f377f;">p</span>.Item,&nbsp;<span style="font-weight:bold;color:#1f377f;">pcea</span>.Proposal); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">AssertSumIsOneHundred</span>(<span style="font-weight:bold;color:#1f377f;">l</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Such a solution comes with its own built-in complexity, because the <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class must be careful to subscribe to the <code>PropertyChanging</code> event in various different places. A new <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> object may be added to the collection during initialization, or via the <code>InsertItem</code> or <code>SetItem</code> methods. Furthermore, the collection should make sure to unsubscribe from the event if an item is removed from the collection. </p> <p> To be honest, I didn't bother to implement these extra checks, because the point is moot anyway. </p> <h3 id="bebd0dc43a03466e9709a551bbd563ba"> Fatal flaw <a href="#bebd0dc43a03466e9709a551bbd563ba">#</a> </h3> <p> The design shown here comes with a fatal flaw. Can you tell what it is? </p> <p> Since the invariant is that the priorities must always sum to exactly 100, it's impossible to add, remove, or change any items after initialization. </p> <p> Or, rather, you can add new <code><span style="color:#2b91af;">Prioritized</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> objects as long as their <code>Priority</code> is 0. Any other value breaks the invariant. </p> <p> Likewise, the only item you can remove is one with a <code>Priority</code> of 0. Again, if you remove an item with any other <code>Priority</code>, you'd be violating the invariant. </p> <p> A similar situation arises with editing an existing item. While you can change the <code>Priority</code> of an item, you can only 'change' it to the same value. So you can change 0 to 0, 42 to 42, or 100 to 100, but that's it. </p> <p> <em>But</em>, I can hear you say, <em>I'll only change 60 to 40 because I intend to add a new item with a 20 priority! In the end, the sum will be 100!</em> </p> <p> Yes, but this design doesn't know that, and you have no way of telling it. </p> <p> While we may be able to rectify the situation, I consider this design so compromised that I think it better to start afresh with this realization. Thus, I'll abandon this version of <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> in favour of a fresh start in the next article. </p> <h3 id="6f81f5d6efb5476b9072f9d8e5b01031"> Conclusion <a href="#6f81f5d6efb5476b9072f9d8e5b01031">#</a> </h3> <p> While I've titled this article "A failed attempt", the actual purpose was to demonstrate how 'aggregate' requirements make it difficult to maintain class invariants. </p> <p> I've seen many code bases with poor encapsulation. As far as I can tell, a major reason for that is that the usual 'small-scale' object-oriented design techniques like Guard Clauses fall short when an invariant involves the interplay of multiple objects. And in real business logic, that's the rule rather than the exception. </p> <p> Not all is lost, however. In the next article, I'll develop an alternative object-oriented solution to the priority collection problem. </p> <p> <strong>Next:</strong> <a href="/2024/06/24/a-mutable-priority-collection">A mutable priority collection</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b86cf94255c34db29d0a6275787f18d8"> <div class="comment-author">Daniel Frost <a href="#b86cf94255c34db29d0a6275787f18d8">#</a></div> <div class="comment-content"> <p> 2 things. </p> <p> I had a difficult time getting this to work with as a mutable type and the only two things I could come with (i spent some hours on it, it was in fact hard!) was <br><br> 1. To throw an exception when the items in the collection didn't sum up to the budget. That violates the variant because you can add and remove items all you want. <br> 2. Another try, which I didn't finish, is to add some kind of result-object that could tell about the validity of the collection and not expose the collection items before the result is valid. I haven't tried this and it doesn't resemble a collection but it could perhaps be a way to go. <br> I am also leaning towards a wrapper around the item type, making it immutable, so the items cannot change afterwards. Cheating ? <br> I tried with the events approach but it is as you put yourself not a very friendly type you end up with. </p> </div> <div class="comment-date">2024-06-18 11:54 UTC</div> </div> <div class="comment" id="e9143a083d5449448e3bd69bfb8fde85"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e9143a083d5449448e3bd69bfb8fde85">#</a></div> <div class="comment-content"> <p> Daniel, thank you for writing. You'll be interested in the next articles in the series, then. </p> </div> <div class="comment-date">2024-06-18 13:55 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Simpler encapsulation with immutability https://blog.ploeh.dk/2024/06/12/simpler-encapsulation-with-immutability 2024-06-12T15:33:00+00:00 Mark Seemann <div id="post"> <p> <em>A worked example.</em> </p> <p> I've noticed that many software organizations struggle with <a href="/encapsulation-and-solid">encapsulation</a> with 'bigger' problems. It may be understandable and easily applicable to <a href="/2022/08/22/can-types-replace-validation">define a NaturalNumber type</a> or ensure that a minimum value is less than a maximum value, and so on. How do you, however, guarantee invariants once the scope of the problem becomes bigger and more complex? </p> <p> In this series of articles, I'll attempt to illustrate how and why this worthy design goal seems elusive, and what you can do to achieve it. </p> <h3 id="ff18a3c70d46435f9c301f20ce6bb830"> Contracts <a href="#ff18a3c70d46435f9c301f20ce6bb830">#</a> </h3> <p> As usual, when I discuss <em>encapsulation</em>, I first need to establish what I mean by the term. It is, after all, one of the most misunderstood concepts in software development. As regular readers will know, I follow the lead of <a href="/ref/oosc">Object-Oriented Software Construction</a>. In that perspective, encapsulation is the appropriate modelling and application of <em>preconditions</em>, <em>invariants</em>, and <em>postconditions</em>. </p> <p> Particularly when it comes to invariants, things seem to fall apart as the problem being modelled grows in complexity. Teams eventually give up guaranteeing any invariants, leaving client developers with no recourse but <a href="/2013/07/08/defensive-coding">defensive coding</a>, which again leads to code duplication, bugs, and maintenance problems. </p> <p> If you need a reminder, an <em>invariant</em> is an assertion about an object that is always true. The more invariants an object has, the better guarantees it gives, and the more you can trust it. The more you can trust it, the less defensive coding you have to write. You don't have to check if return values are null, strings empty, numbers negative, collections empty, or so on. </p> <p> <img src="/content/binary/contract-pre-post-invariant.png" alt="The three sets of preconditions, postconditions, and invariants, embedded in their common superset labeled contract."> </p> <p> All together, I usually denote the collection of invariants, pre-, and postconditions as a type's <em>contract</em>. </p> <p> For a simple example like modelling a natural number, or <a href="/2024/01/01/variations-of-the-range-kata">a range</a>, or a user name, most people are able to produce sensible and coherent designs. Once, however, the problem becomes more complex, and the invariants involve multiple interacting values, maintaining the contract becomes harder. </p> <h3 id="8d70ef6ac6054c87b920c972cf26b3a5"> Immutability to the rescue <a href="#8d70ef6ac6054c87b920c972cf26b3a5">#</a> </h3> <p> I'm not going to bury the lede any longer. It strikes me that <em>mutation</em> is a major source of complexity. It's not that hard to check a set of conditions when you create a value (or object or record). What makes it hard to maintain invariants is when objects are allowed to change. This implies that for every possible change to the object, it needs to examine its current state in order to decide whether or not it should allow the operation. </p> <p> If the mutation happens on a leaf node in an object graph, the leaf may have to notify its parent, so that the parent can recheck the invariants. If the <a href="https://en.wikipedia.org/wiki/Directed_graph">graph</a> has cycles it becomes more complicated still, and if you want to make the problem truly formidable, try making the object thread-safe. </p> <p> Making the object immutable makes most of these problems go away. You don't have to worry about thread-safety, because immutable values are automatically thread-safe; there's no state for any thread to change. </p> <p> Even better, though, is that an immutable object's contract is smaller and simpler. It still has preconditions, because there are rules that govern what has to be true before you can create such an object. Furthermore, there may also be rules that stipulate what must be true before you can call a method on it. </p> <p> Likewise, postconditions are still relevant. If you call a method on the object, it may give you guarantees about what it returns. </p> <p> There are, however, no independent invariants. </p> <p> <img src="/content/binary/contract-pre-post.png" alt="The two sets of preconditions and postconditions, embedded in their common superset labeled contract."> </p> <p> Or rather, the invariants for an immutable object entirely coincide with its preconditions. If it was valid at creation, it remains valid. </p> <h3 id="d281ea9242574c65bf85399178b2ce28"> Priority collection <a href="#d281ea9242574c65bf85399178b2ce28">#</a> </h3> <p> As promised, I'll work through a problem to demonstrate what I mean. I'll first showcase how mutation makes the problem hard, and then how trivial it becomes with an immutable design. </p> <p> The problem is this: Design and implement a class (or just a <em>data structure</em> if you don't want to do Object-Oriented programming) that models a priority list (not a <a href="https://en.wikipedia.org/wiki/Priority_queue">Priority Queue</a>) as you sometimes run into in surveys. You know, one of these survey questions that asks you to distribute 100 points on various different options: </p> <ul> <li>Option F: 30%</li> <li>Option A: 25%</li> <li>Option C: 25%</li> <li>Option E: 20%</li> <li>Option B: 0%</li> <li>Option D: 0%</li> </ul> <p> If you have the time, I suggest that you <a href="/2020/01/13/on-doing-katas">treat this problem as a kata</a>. Try to do the exercise before reading the next articles in this series. You can assume the following, which is what I did. </p> <ul> <li>The budget is 100. (You could make it configurable, but the problem is gnarly enough even with a hard-coded value.)</li> <li>You don't need to include items with priority value 0, but you should allow it.</li> <li>The sum of priorities must be exactly 100. This is the invariant.</li> </ul> <p> The difficult part is that last invariant. Let me stress this requirement: At any time, the object should be in a consistent state; i.e. at any time should the sum of priorities be exactly 100. Not 101 or 99, but 100. Good luck with that. </p> <p> The object should also be valid at initialization. </p> <p> Of course, having read this far, you understand that all you have to do is to make the object immutable, but just for the sake of argument, try designing a mutable object with this invariant. Once you've tried your hand with that, read on. </p> <h3 id="e891a12040df4d81957d50b9110a6957"> Attempts <a href="#e891a12040df4d81957d50b9110a6957">#</a> </h3> <p> There's educational value going through even failed attempts. When I thought of this example, I fairly quickly outlined in my head one approach that was unlikely to ever work, one that could work, and the nice immutable solution that trivially works. </p> <p> I'll cover each in turn: </p> <ul> <li><a href="/2024/06/17/a-failed-attempt-at-priority-collection-with-inheritance">A failed attempt at priority collection with inheritance</a></li> <li><a href="/2024/06/24/a-mutable-priority-collection">A mutable priority collection</a></li> <li><a href="/2024/07/01/an-immutable-priority-collection">An immutable priority collection</a></li> </ul> <p> It's surprising how hard even a simple exercise like this one turns out to be, if you try to do it the object-oriented way. </p> <p> In reality, business rules are much more involved than what's on display here. For only a taste of how bad it might get, read <a href="https://buttondown.email/hillelwayne/archive/making-illegal-states-unrepresentable/">Hillel Wayne's suggestions regarding a similar kind of problem</a>. </p> <h3 id="01740af0c0cc4afcac2cb2361689e209"> Conclusion <a href="#01740af0c0cc4afcac2cb2361689e209">#</a> </h3> <p> If you've lived all your programming life with mutation as an ever-present possibility, you may not realize how much easier immutability makes everything. This includes invariants. </p> <p> When you have immutable data, object graphs tend to be simpler. You can't easily define cyclic graphs (although <a href="https://www.haskell.org/">Haskell</a>, due to its laziness, surprisingly does enable this), and invariants essentially coincide with preconditions. </p> <p> In the following articles, I'll show how mutability makes even simple invariants difficult to implement, and how immutability easily addresses the issue. </p> <p> <strong>Next:</strong> <a href="/2024/06/17/a-failed-attempt-at-priority-collection-with-inheritance">A failed attempt at priority collection with inheritance</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="667d3faaebff4009beefcd15612280ac"> <div class="comment-author">Marken Foo <a href="#667d3faaebff4009beefcd15612280ac">#</a></div> <div class="comment-content"> <p> I've been enjoying going through your articles in the past couple months, and I really like the very pedagogic treatment of functional programming and adjacent topics. </p> <p> The kata here is an interesting one, but I don't think I'd link it with the concept of immutability/mutability. My immediate thought was a naïve struct that can represent illegal values and whose validity is managed through functions containing some tricky logic, but that didn't seem promising whether it was done immutably or not. </p> <p> Instead, the phrase "distribute 100 points" triggered an association with the <a href="https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)">stars and bars</a> method for similar problems. The idea is that we have N=100 points in a row, and inserting dividers to break it into (numOptions) groups. Concretely, our data structure is (dividers: int array), which is a sorted array of length (numOptions + 1) where the first element is 0 and the last element is N=100. The priorities are then exactly the differences between adjacent elements of the array. The example in the kata (A=25, B=0, C=25, D=0, E=20, F=30) is then represented by the array [| 0; 25; 25; 50; 50; 70; 100|]. </p> <p> This solution seems to respect the invariant, has a configurable budget, can work with other numerical types, and works well whether immutable or not (if mutable, just ensure the array remains sorted, has min 0, and max N). The invariant is encoded in the representation of the data, which seems to me to be the more relevant point than mutability. </p> <p> And a somewhat disjoint thought, the kata reminded me of a WinForms TableLayoutPanel (or MS Word table) whose column widths all must fit within the container's width... </p> </div> <div class="comment-date">2024-06-13 13:55 UTC</div> </div> <div class="comment" id="3b2e1954394849c4970a1ab30f692192"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3b2e1954394849c4970a1ab30f692192">#</a></div> <div class="comment-content"> <p> Thank you for writing. The danger of writing these article series is always that as soon as I've published the first one, someone comes by and puts a big hole through my premise. Well, I write this blog for a couple of independent reasons, and one of them is to learn. </p> <p> And you just taught me something. Thank you. That is, at least, an elegant implementation. </p> <p> How would you design the API encapsulating that implementation? </p> <p> Clearly, arrays already have APIs, so you could obviously define an array-like API that performs the appropriate boundary checks. That, however, doesn't seem to model the given problem. Rather, it reveals the implementation, and forces a client developer to think in terms of the data structure, rather the problem (s)he has to solve. </p> <p> Ideally, again channelling Bertrand Meyer, an object should present as an Abstract Data Structure (ADT) that doesn't require client developers to understand the implementation details. I'm curious what such an API would look like. </p> <p> You've already surprised me once, and please do so once again. I'm always happy to learn something new, and that little stars-and-bars concept I've now added to my tool belt. </p> <p> All that said, this article makes a more general claim, although its possible that the example it showcases is a tad too simple and naive to be a truly revealing one. The claim is that this kind of 'aggregate constraint' often causes so much trouble in the face of arbitrary state mutation that most programmers give up on encapsulation. </p> <p> What happens if we instead expand the requirements a bit? Let's say that we will require the user to spend at least 90% of the budget, but no more than 100%. Also, there must be at least three prioritized items, and no individual item can receive more than a third of the budget. </p> </div> <div class="comment-date">2024-06-14 14:22 UTC</div> </div> <div class="comment" id="7c5c157868fe46f3aae343e5e145a5eb"> <div class="comment-author">Marken Foo <a href="#7c5c157868fe46f3aae343e5e145a5eb">#</a></div> <div class="comment-content"> <p> Thank you for the response. Here's my thoughts - it's a bit of a wall of text, I might be wrong in any of the following, and the conclusion may be disappointing. When you ask how I'd design the API, I'd say it depends on how the priority list is going to be used. The implementation trick with stars and bars might just be a one-off trick that happens to work here, but it doesn't (shouldn't) affect the contract with the outside world. </p> <p> If we're considering survey questions or budgets, the interest is in the priority values. So I think the problem then <em>is</em> about a list of priorities with an aggregate constraint. So I would define... an array-like API that performs the appropriate boundary checks (wow), but for the item priorities. My approach would be to go for "private data, public functions", and rely on a legal starting state and preserving the legality through the public API. In pseudocode: </p> <pre> type PriorityList = { budget: int; dividers: int list } create :: numItems: int -> budget: int -> PriorityList // Returns priorities. getAll :: plist: PriorityList -> int list get :: itemIdx: int -> plist: PriorityList -> int // *Sets the priority for an item (taking the priority from other items, starting from the back). set :: itemIdx: int -> priority: int -> plist: PriorityList -> PriorityList // *Adds a new item to (the end of) the PriorityList (with priority zero). addItem :: plist: PriorityList -> PriorityList // *Removes an item from the PriorityList (and assigns its priority to the last item). removeItem :: itemIdx: int -> plist PriorityList -> PriorityList // Utility functions: see text _toPriorities :: dividers: int list -> int list _toDividers :: priorities: int list -> int list </pre> <p> Crucially: since <code>set</code>, <code>addItem</code>, and <code>removeItem</code> must maintain the invariants, they must have "side effects" of altering other priorities. I think this is unavoidable here because we have aggregate/global constraints, rather than just elementwise/local constraints. (Is this why resizing rows and columns in WinForms tableLayoutPanels and MS Word tables is so tedious?) This will manifest in the API - the client needs to know what "side effects" there are (suggested behaviour in parentheses in the pseudocode comments above). See <a href="https://gist.github.com/Marken-Foo/d1e1a32afa91790f84f151c429c042cd">my crude attempt at implementation</a>. </p> <p> You may already see where this is going. If I accept that boundary checks are needed, then my secondary goal in encapsulation is to express the constraints as clearly as possible, and hopefully not spread the checking logic all over the code. </p> <p> Whence the utility functions: it turned out to be useful to convert from a list of dividers to priorities, and vice versa. This is because the elementwise operations/invariants like the individual priority values are easier to express in terms of raw priorities, while the aggregate ones like the total budget are easier in terms of "dividers" (the <em>cumulative</em> priorities). There is a runtime cost to the conversion, but the code becomes clearer. This smells similar to feature envy... </p> <p> So why not just have the underlying implementation hold a list of priorities in the first place?! Almost everything in the implementation needs translation back to that anyway. D'oh! I refactored myself back to the naïve approach. The original representation seemed elegant, but I couldn't find a way to manipulate it that clients would find intuitive and useful in the given problem. </p> <p> But... if I approach the design from the angle "what advantages does the cumulative priority model offer?", I might come up with the following candidate API functions, which could be implemented cleanly in the "divider" space: </p> <pre> // (same type, create, get, getAll, addItem as above) // Removes the item and merges its priority with the item before it. merge :: ItemIdx: int -> PriorityList // Sets the priority of an item to zero and gives it to the item after it. collapse :: itemIdx: int -> PriorityList // Swaps the priority of an item and the one after it (e.g. to "bubble" a priority value forwards or backwards, although this is easier in the "priority" space) swap :: itemIdx: int -> PriorityList // Sets (alternative: adds to) the priority of an item, taking the priority from the items after it in sequence ("consuming" them in the forward direction) consume :: itemIdx: int -> priority: int -> PriorityList // Splits the item into 2 smaller items each with half the priority (could be generalised to n items) split :: ItemIdx: int -> PriorityList // etc. </pre> <p> And this seems like a more fitting API for that table column width example I keep bringing up. What's interesting to me is that despite the data structures of the budget/survey question and the table column widths being isomorphic, we can come up with rather different APIs depending on which view we consider. I think this is my main takeaway from this exploration, actually. </p> <p> As for the additional requirements, individually each constraint is easy to handle, but their composition is tricky. If it's easy to transform an illegal PriorityList to make it respect the invariants, we can just apply the transformation after every create/set/add/remove. Something like: </p> <pre> type PriorityList = { budget: int dividers: int list budgetCondition: int -> bool maxPriority: int minChoices: int } let _enforceBudget (predicate: int -> bool) (defaultBudget: int) (dividers: int list) : int list = if (List.last dividers |> predicate) then dividers else List.take (dividers.Length - 1) dividers @ [ defaultBudget ] let _enforceMaxPriority (maxPriority: int) (dividers: int list) : int list = _toPriorities dividers |> List.map (fun p -> min p maxPriority) |> _toDividers </pre> <p> The problem is those transforms may not preserve each others' invariant. Life would be easy if we could write a single transform to preserve everything (I haven't found one - notice that the two above are operating on different int lists so it's tricky). Otherwise, we could write validations instead of transformations, then let create/set/add/remove fail by returning Option.None (explicitly fail) or the original list (silently fail). This comes at the cost of making the API less friendly. </p> <p> Ultimately with this approach I can't see a way to make all illegal states unrepresentable without sprinkling ad-hoc checks everywhere in the code. The advantages of the "cumulative priorities" representation I can think of are (a) it makes the total budget invariant obvious, and (b) it maps nicely to a UI where you click and drag segments around. Since you might have gone down a different path in the series, I'm curious to see how that shapes up. </p> </div> <div class="comment-date">2024-06-15 14:48 UTC</div> </div> <div class="comment" id="595bcdcf19e446c7a23531b93b6d5a1c"> <div class="comment-author">Aliaksei Saladukhin <a href="#595bcdcf19e446c7a23531b93b6d5a1c">#</a></div> <div class="comment-content"> <p> Hello and thank you for your blog. It is really informative and provides great food for thought. </p> <p> What if it will be impossible to compile and run program which would lead to illegal (list) state? </p> <p> I've tried to implement priority collection in Rust, and what I've ended up with is a heterogenous priority list with compile-time priority validation. Idea behind this implementation is simple: you declare recursive generic struct, which holds current element and tail (another list or unit type). </p> <pre> struct PriorityList&lt;const B: usize, const P: usize, H, T&gt; { head: H, tail: T, } </pre> <p> If, for example, we need list of two Strings with budget 100, and 30/70 priority split, it will have the following type: <code>PriorityList&lt;100, 30, String, PriorityList&lt;100, 70, String, ()&gt;&gt;</code> Note that information about list budget and current element priority is contained in generic arguments B and P respectively. These are compile-time "variables", and will be replaced be their values in compiled program. </p> <p> Since each element of such list is a list itself, and budget is the same for each element, all elements except the first are invalid priority lists. So, in order to make it possible to create lists other than containing one element, or only one element with &gt;0 priority, validity check should be targeted and deferred. In order to target invariant validation on the first element of the list, I've included validation into list methods (except set_priority method). Every time list method is called, compiler does recursive computation of priority sum, and compares it with list budget, giving compile-time error if there is mismatch. Consider the following example, which will compile and run: </p> <pre> let list = ListBuilder::new::&lt;10, 10&gt;("Hello"); let list = list.set_priority::<5>(); </pre> <p> Seems like invariants have been violated and sum of priorities is less than the budget. But if we try to manipulate this list in any other way except to add element or change priority, program won't compile </p> <pre> // Won't compile let _ = list.pop(); // Won't compile let list = list.push::<4>("Hi"); // Will do let list = list.push::<5>("Hello there"); </pre> <p> This implementation may not be as practical as it could be due to verbose compilation error messages, but is a good showcase and exercise I've also uploaded full source code at GitLab: https://gitlab.com/studiedlist/priority-collection </p> </div> <div class="comment-date">2024-06-18 08:47 UTC</div> </div> <div class="comment" id="7f2ed4d386144b378cae2206e8269a6d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7f2ed4d386144b378cae2206e8269a6d">#</a></div> <div class="comment-content"> <p> Marken, thank you for writing. It's always interesting to learn new techniques, and, as I previously mentioned, the array-based implementation certainly seems to <a href="https://blog.janestreet.com/effective-ml-video/">make illegal states unrepresentable</a>. And then, as we'll see in the last (yet unpublished) article in this little series, if we also make the data structure immutable, we'll have a truly simple and easy-to-understand API to work with. </p> <p> I've tried experimenting with the <a href="https://fsharp.org/">F#</a> script you linked, but I must admit that I'm having trouble understanding how to use it. You did write that it was a crude attempt, so I'm not complaining, but on the other hand, it doesn't work well as an example of good encapsulation. The following may seem as though I'm moving the goalpost, so apologies for that in advance. </p> <p> Usually, when I consult development organizations about software architecture, the failure to maintain invariants is so fundamental that I usually have to start with that problem. That's the reason that this article series is so narrow-mindedly focused on contract, and seemingly not much else. We must not, though, lose sight of what ultimately motivates us to consider encapsulation beneficial. This is what I've tried to outline in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>: That the human brain is ill-suited to keep all implementation details in mind at the same time. One way we may attempt to address this problem is to hide implementation details behind an API which, additionally, comes with some guarantees. Thus (and this is where you may, reasonably, accuse me of moving the goal post), not only should an object fulfil its contract, it should also be possible to interact with its API without understanding implementation details. </p> <p> The API you propose seem to have problems, some of which may be rectifiable: </p> <ul> <li>At a fundamental level, it's not really clear to me how to use the various functions in the script file.</li> <li>The API doesn't keep track of <em>what</em> is being prioritized. This could probably be fixed.</li> <li>It's not clear whether it's possible to transition from one arbitrary valid distribution to another arbitrary valid distribution.</li> </ul> <p> I'll briefly expand on each. </p> <p> As an example of the API being less that clear to me, I can't say that I understand what's going on here: </p> <p> <pre>&gt; create 1 100 |&gt; set 1 50 |&gt; addItem |&gt; set 1 30;; val it: PriorityList = { budget = 100 dividers = [0; 50; 100] }</pre> </p> <p> As for what's being prioritized, you could probably mend that shortcoming by letting the array be an array of tuples. </p> <p> The last part I'm not sure of, but you write: </p> <blockquote> <p> "Crucially: since <code>set</code>, <code>addItem</code>, and <code>removeItem</code> must maintain the invariants, they must have "side effects" of altering other priorities." </p> </blockquote> <p> As <a href="/2024/06/24/a-mutable-priority-collection">the most recent article in this series demonstrates</a>, this isn't an overall limitation imposed by the invariant, but rather by your chosen API design. Specifically, assuming that you initially have a <em>23, 31, 46</em> distribution, how do you transition to a <em>19, 29, 43, 7, 2</em> distribution? </p> </div> <div class="comment-date">2024-06-27 6:42 UTC</div> </div> <div class="comment" id="388933ccbe5a4c71ba0b443a223e08ca"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#388933ccbe5a4c71ba0b443a223e08ca">#</a></div> <div class="comment-content"> <p> Aliaksei, thank you for writing. I've never programmed in Rust, so I didn't know it had that capability. At first I though it was dependent typing, but after reading up on it, it seems as though it's not quite that. </p> <p> An exercise like the one in this article series is useful because it can help shed light on options and their various combinations of benefits and drawbacks. Thus, there are no entirely right or wrong solutions to such an exercise. </p> <p> Since I don't know Rust, I can't easily distinguish what might be possible drawbacks here. I usually regard making illegal states unrepresentable as a benefit, but we must always be careful not to go too far in that direction. One thing is to reject invalid states, but can we still represent all valid states? What if priority distributions are run-time values? </p> </div> <div class="comment-date">2024-06-28 7:21 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. You'll regret using natural keys https://blog.ploeh.dk/2024/06/03/youll-regret-using-natural-keys 2024-06-03T19:46:00+00:00 Mark Seemann <div id="post"> <p> <em>Beating another dead horse.</em> </p> <p> Although I live in Copenhagen and mostly walk or ride my bicycle in order to get around town, I do own an old car for getting around the rest of the country. In Denmark, cars go through mandatory official inspection every other year, and I've been through a few of these in my life. A few years ago, the mechanic doing the inspection informed me that my car's <a href="https://en.wikipedia.org/wiki/Vehicle_identification_number">chassis number</a> was incorrect. </p> <p> This did make me a bit nervous, because I'd bought the car used, and I was suddenly concerned that things weren't really as I thought. Had I unwittingly bought a stolen car? </p> <p> But the mechanic just walked over to his computer in order to correct the error. That's when a different kind of unease hit me. When you've programmed for some decades, you learn to foresee various typical failure modes. Since a chassis number is an obvious candidate for a <a href="https://en.wikipedia.org/wiki/Natural_key">natural key</a>, I already predicted that changing the number would prove to be either impossible, or have all sorts of cascading effects, ultimately terminating in official records no longer recognizing that the car is mine. </p> <p> As it turned out, though, whoever made that piece of software knew what they were doing, because the mechanic just changed the chassis number, and that was that. This is now five or six years ago, and I still own the same car, and I've never had any problems with the official ownership records. </p> <h3 id="8a38dcbfae1146f8868fe8a408ffe5d8"> Uniqueness <a href="#8a38dcbfae1146f8868fe8a408ffe5d8">#</a> </h3> <p> The reason I related this story is that I'm currently following an undergraduate course in databases and information systems. Since this course is aimed at students with no real-world experience, it wisely moves forward in a pedagogical progression. In order to teach database keys, it starts with natural keys. From a didactic perspective, this makes sense, but the result, so far, is that the young people I work with now propose database designs with natural keys. </p> <p> I'm not blaming anyone. You have to learn to crawl before you can walk. </p> <p> Still, this situation made me reflect on the following question: <em>Are natural keys ever a good idea?</em> </p> <p> Let's consider an example. For a little project we're doing, we've created a database of <a href="https://www.theworlds50best.com/">the World's 50 best restaurants</a>. My fellow students suggest a table design like this: </p> <p> <pre><span style="color:blue;">CREATE</span>&nbsp;<span style="color:blue;">TABLE</span>&nbsp;Restaurants<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:magenta;">year</span>&nbsp;<span style="color:blue;">TEXT</span>&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:magenta;">rank</span>&nbsp;<span style="color:blue;">TEXT</span>&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;restaurantName&nbsp;<span style="color:blue;">TEXT</span>&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;cityName&nbsp;<span style="color:blue;">TEXT</span>&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span> <span style="color:gray;">);</span></pre> </p> <p> Granted, at this point, this table definition defines no key at all. I'm not complaining about that. After all, a month ago, the students probably hadn't seen a database table. </p> <p> From following the course curriculum, it'd be natural, however, to define a key for the <code>Restaurants</code> table as the combination of <code>restaurantName</code>, <code>cityName</code>, and <code>year</code>. The assumption is that name and city uniquely identifies a restaurant. </p> <p> In this particular example, this assumption may actually turn out to hold. So far. After all, the data set isn't that big, and it's important for restaurants in that league to have recognizable names. If I had to guess, I'd say that there's probably only one <a href="https://www.nobelhartundschmutzig.com/">Nobelhart & Schmutzig</a> in the world. </p> <p> Still, a good software architect should challenge the underlying assumptions. Is name and city a <em>natural</em> key? It's easy to imagine that it's not. What if we expand the key to include the country as well? Okay, but what if we had a restaurant named <em>China Wok</em> in Springfield, USA? Hardly unique. Add the state, you say? Probably still not unique. </p> <h3 id="778bd217e62d4ffca25f487a39d34c1a"> Identity <a href="#778bd217e62d4ffca25f487a39d34c1a">#</a> </h3> <p> Ensuring uniqueness is only the first of many problems with natural keys. You may quickly reach the conclusion that for a restaurant database, a <a href="https://en.wikipedia.org/wiki/Surrogate_key">synthetic key</a> is probably the best choice. </p> <p> But what about 'natural' natural keys, so to speak? An example may be a car's chassis number. This is already an opaque number, and it probably originates from a database somewhere. Or how about a personal identification number? In Denmark we have the <a href="https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)">CPR number</a>, and I understand that the US <a href="https://en.wikipedia.org/wiki/Social_Security_number">Social Security Number</a> is vaguely analogous. </p> <p> If you're designing a database that already includes such a personal identification number, you might be tempted to use it as a natural key. After all, it's already a key somewhere else, so it's guaranteed to be unique, right? </p> <p> Yes, the number may uniquely identify a person, but the converse may not be true. A person may have more than one identification number. At least when time is a factor. </p> <p> As an example, for technical-historical reasons, the Danish CPR number carries information (which keys shouldn't do), such as a person's date of birth and sex. Since 2014 a new law enables transsexual citizens to get a new CPR number that reflects their perceived gender. The consequence is that the same person may have more than one CPR number. Perhaps not more than one at the same time, but definitely two during a lifetime. </p> <p> Even if existing keys are guaranteed to be unique, you can't assume that the uniqueness gives rise to a <a href="https://en.wikipedia.org/wiki/Bijection">bijection</a>. If you use an external unique key, you may lose track of the entities that you're trying to keep track of. </p> <p> This is true not only for people, but cars, bicycles (which also have chassis numbers), network cards, etc. </p> <h3 id="93d43ca6c76a4c6c83e9542bb671f39c"> Clerical errors <a href="#93d43ca6c76a4c6c83e9542bb671f39c">#</a> </h3> <p> Finally, even if you've found a natural key that is guaranteed to be unique <em>and</em> track the actual entity that you want to keep track of, there's a final argument against using an externally defined key in your system: Data-entry errors. </p> <p> Take the story about my car's chassis number. The mechanic who spotted the discrepancy clearly interpreted it as a clerical error. </p> <p> After a few decades of programming, I've learned that sooner or later, there <em>will</em> be errors in your data. Either it's a clerical error, or the end-user mistyped, or there was a data conversion error when importing from an external system. Or even data conversion errors within the <em>same</em> system, as it goes through upgrades and migrations. </p> <p> Your system should be designed to allow corrections to data. This includes corrections of external keys, such as chassis numbers, government IDs, etc. This means that you can't use such keys as database keys in your own system. </p> <h3 id="ca9efd721698452f856b089fc3f69ad1"> Heuristic <a href="#ca9efd721698452f856b089fc3f69ad1">#</a> </h3> <p> Many were the times, earlier in my career, when I decided to use a 'natural key' as a key in my own database. As far as I recall, I've regretted it every single time. </p> <p> These days I follow a hard heuristic: Always use synthetic keys for database tables. </p> <h3 id="c174062e2ad14b6a9ff4056b1de80a0f"> Conclusion <a href="#c174062e2ad14b6a9ff4056b1de80a0f">#</a> </h3> <p> Is it ever a good idea to use natural keys in a database design? My experience tells me that it's not. Ultimately, regardless of how certain you can be that the natural key is stable and correctly tracks the entity that it's supposed to keep track of, data errors will occur. This includes errors in those natural keys. </p> <p> You should be able to correct such errors without losing track of the involved entities. You'll regret using natural keys. Use synthetic keys. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="63e21d2f538e4d67a30094e33ff507a7"> <div class="comment-author"><a href="http:/snape.me">James Snape</a> <a href="#63e21d2f538e4d67a30094e33ff507a7">#</a></div> <div class="comment-content"> <p> There are lots of different types of keys. I agree that using natural keys as physical primary keys is a bad idea but you really should be modelling your data logically with natural keys. Thinking about uniqueness and identity is a part of your data design. Natural keys often end up as constraints, indexes and query plans. When natural keys are not unique enough then you need to consider additional attributes in your design to ensure access to a specific record. </p> <p> Considering natural keys during design can help elicit additional requirements and business rules. "Does a social security number uniquely identify a person? If not why?" In the UK they recycle them so the natural key is a combination of national insurance number and birth year. You have to ask questions. </p> </div> <div class="comment-date">2024-06-04 15:43 UTC</div> </div> <div class="comment" id="82ee5d39a4d34539899ddaf13d1336a9"> <div class="comment-author"><a href="https://thomascastiglione.com">Thomas Castiglione</a> <a href="#82ee5d39a4d34539899ddaf13d1336a9">#</a></div> <div class="comment-content"> <img src="/content/binary/you-will-regret-this.png" border="0"> </div> <div class="comment-date">2024-06-05 9:33 UTC</div> </div> <div class="comment" id="92ee5d39a4d34539899ddaf13d1336b7"> <div class="comment-author"><a href="https://processdecision.com">Nicholas Peterson</a> <a href="#92ee5d39a4d34539899ddaf13d1336b7">#</a></div> <div class="comment-content"> <p> I largely agree with James Snape, but wanted to throw in a few other thoughts on top. Surrogates don't defend you from duplicate data, in fact they facilitate it, because the routine generating the surrogate key isn't influenced by any of the other data in the record. The concept of being unable to correct a natural key is also odd, why can't you? Start a transaction, insert a new record with the correct key, update the related records to point to the new record, then delete the old record, done. Want some crucial information about a related record but only have the surrogate to it? I guess you have to join it every time in order to get the columns the user actually wants to see. A foreign key that uses a natural key often often prevents the join entirely, because it tells the user what they wanted to know. </p> <p> I find the problem with natural keys usually comes from another source entirely. Developers write code and don't tend to prefer using SQL. They typically interact with databases through ORM libraries. ORMs are complicated and rely on conventions to uniformly deal with data. It's not uncommon for ORMs to dictate the structure of tables to some degree, or what datatypes to prefer. It's usually easier in an ORM to have a single datatype for keys (BIGINT?) and use it uniformly across all the tables. </p> </div> <div class="comment-date">2024-06-05 12:42 UTC</div> </div> <div class="comment" id="2960b65bbaec4db8ade70f551e3f5062"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2960b65bbaec4db8ade70f551e3f5062">#</a></div> <div class="comment-content"> <p> James, Nicholas, thank you for writing. I realize that there are some unstated assumptions and implied concerns that I should have made more explicit. I certainly have no problem with adding constraints and other rules to model data. For the Danish CPR number, for example, while I wouldn't make it a primary key (for the reasons outlined in the article), I'd definitely put a <code>UNIQUE</code> constraint on it. </p> <p> Another unspoken context that I had in mind is that systems often exist in a wider context where ACID guarantees fall apart. I suppose it's true that if you look at a database in isolation, you may be able to update a foreign key with the help of some cascading changes rippling through the database, but if you've ever shared the old key outside of the database, you now have orphaned data. </p> <p> A simple example could be sending out an email with a link that embeds the old key. If you change the key after sending out the email, but before the user clicks, the link no longer works. </p> <p> That's just a simple and easy-to-explain example. The more integration (particularly system-to-system integration) you have, the worse this kind of problem becomes. I briefly discussed the CPR number example with my doctor wife, and she immediately confirmed that this is a real problem in the Danish health sector, where many independent software systems need to exchange patient data. </p> <p> You can probably work around such problems in various ways, but if you had avoided using natural keys, you wouldn't have had to change the key in the first place. </p> </div> <div class="comment-date">2024-06-06 6:56 UTC</div> </div> <div class="comment" id="f7d04f04aade40d1b668c94a56c7c189"> <div class="comment-author"><a href="https://github.com/bantling">Greg Hall</a> <a href="#f7d04f04aade40d1b668c94a56c7c189">#</a></div> <div class="comment-content"> <p> I think it is best to have two separate generated keys for each row: </p> <ul> <li>A key used only for relationships between tables. I like to call this relid, and make it serialised, so it is just an increasing number. This key is the primary key and should never be exposed outside the database. </li> <li>A key used only outside the database as a unique reference to which row to update. I like to call this id, and make it a uuid, since it is well accepted to uniquely identify rows by a uuid, and to expose them to the outside world - many public APIs do this. Theoretically, the same uuid should never be generated twice, so this key doesn't necessarily have to be declared as unique. </li> </ul> <p> The relid can be used in simple foreign keys, and in bridging/join tables - tables that contain primary keys of multiple tables. Generally speaking, the relid is far more readable than a uuid - it is easier to hold in your head a simple integer, which usually is not that large, than a 36 character sequence that looks similar to other 36 character sequences. UUIDs generally look like a jumble. </p> <p> A relid can be 32-bits for tables you're confident will never need more than 2.1 billion rows, which really is 99.99% of all tables ever created by 99.99% of applications. If this turns out to be wrong, it is possible to upgrade the relids to 64-bit for a given table. It's a bit of a pain, especially if there are lots of references to it, but it can be done. </p> <p> The relid doesn't always have to be a serialised value, and you don't always have to call the column relid. Since the primary key is never exposed publicly, it doesn't matter if different column types or names are used for different use cases. For example, code tables might use one of the codes as the primary key. </p> <p> I don't think it makes sense to be religious on key usage; just like everything else, there are valid reasons for periodically varying how they work. I'm sure somebody has a valid case where a single key is better than two. I just think it generally makes sense to have a pair of internal and external keys for most cases. </p> </div> <div class="comment-date">2024-06-07 3:31 UTC</div> </div> <div class="comment" id="7a1067a9e6fb4b6293777a1408518429"> <div class="comment-author"><a href="http://snape.me">James Snape</a> <a href="#7a1067a9e6fb4b6293777a1408518429">#</a></div> <div class="comment-content"> <p> The thing with databases keys is you really need to be precise on what you mean by a key. Any combination of attributes is a candidate key. There are also logical and physical representations of keys. For example, a SQL Server primary key is a physical record locator but logically a unique key constraint. Yes, these behave poorly when you use natural keys as the primary key for all the reasons you mention. They are a complete implementation detail. Users should never see these attributes though and you shouldn't share the values outside of your implementation. Sharing integer surrogate keys in urls is a classic issue allowing enumeration attacks on your data if not secured properly. </p> <p> Foreign keys are another logical and physical dual use concept. In SQL Server a physical foreign key constrain must reference the primary key from a parent table but logically that doesn't need to happen for relational theory to work. </p> <p> Alternate keys are combinations of attributes that identify a record (or many records); these are often the natural keys you use in your user interface and where clauses etc. Alternate keys are also how systems communicate. Take your CPR number example, you cannot exchange patient data unless both systems agree on a common key. This can't be an internally generated surrogate value. </p> <p> Natural keys also serve another purpose in parent-child relationships. By sharing natural key attributes with a parent you can ensure a child is not accidentally moved to a new parent plus you can query a child table without needing to join to the parent table. </p> <p>There isn't a one-size-fits all when it comes to databases and keys. <a href="https://en.wikipedia.org/wiki/Joe_Celko">Joe Celko</a> has written extensively on the subject so maybe its better to read the following than my small commentary: <ul> <li><a href="https://www.informationweek.com/it-sectors/celko-on-sql-natural-artificial-and-surrogate-keys-explained">Celko on SQL: Natural, Artificial and Surrogate Keys Explained</a></li> <li><a href="https://www.informationweek.com/data-management/celko-on-sql-identifiers-and-the-properties-of-relational-keys">Celko On SQL: Identifiers and the Properties of Relational Keys</a></li> </ul> </p> </div> <div class="comment-date">2024-06-07 09:57 UTC</div> </div> <div class="comment" id="3ef814a896f34b5485aeeea739766fa9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3ef814a896f34b5485aeeea739766fa9">#</a></div> <div class="comment-content"> <p> Greg, thank you for writing. I agree with everything you wrote, and I've been using that kind of design for... wow, at least a decade, it looks! <a href="/2014/08/11/cqs-versus-server-generated-ids">for a slightly different reason</a>. This kind of design seems, even if motivated by a different concern, congruent with what you describe. </p> <p> Like you also imply, only a sith speaks in absolutes. The irony of the article is that I originally intended it to be more open-ended, in the sense that I was curious if there were genuinely good reasons to use natural keys. As I wrote, the article turned out more unconditional than I originally had in mind. </p> <p> I am, in reality, quite ready to consider arguments to the contrary. But really, I was curious: <em>Is it ever a good idea to use natural keys as primary keys?</em> It sounds like a rhetorical question, but I don't mind if someone furnishes a counter-example. </p> <p> As <a href="#92ee5d39a4d34539899ddaf13d1336b7">Nicholas Peterson intimated</a>, it's probably not a real problem if those keys never 'leave' the database. What I failed to make explicit in this article is that the problems I've consistently run into occur when a system has shared keys with external systems or users. </p> </div> <div class="comment-date">2024-06-14 11:26 UTC</div> </div> <div class="comment" id="7f7d8205e5174c61b1b5e3d19482c0ab"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7f7d8205e5174c61b1b5e3d19482c0ab">#</a></div> <div class="comment-content"> <p> James, thank you for writing. I think we're discussing issues at different levels of abstraction. This just underscores how difficult technical writing is. I should have made my context and assumptions more explicit. The error is mine. </p> <p> Everything you write sounds correct to me. I <em>am</em> aware of both relational calculus and relational algebra, so I'm familiar with the claims you make, and I don't dispute them. </p> <p> My focus is rather on systems architecture. Even an 'internal' system may actually be composed from multiple independent systems, and my concern is that using natural keys to exchange data between such systems ultimately turns out to make things more difficult than they could have been. The only statement of yours with which I think I disagree is that you can't exchange data between systems unless you use natural keys. You definitely can, although you need to appoint one of the systems to be a 'master key issuer'. </p> <p> In practice, <a href="#f7d04f04aade40d1b668c94a56c7c189">like Greg Hall</a>, I'd prefer using GUIDs for that purpose, rather than sequential numbers. That also addresses the concern about enumeration attacks. (Somewhat tangentially, I also <a href="/2020/10/26/fit-urls">recommend signing URLs with a private key</a> in order to prevent reverse-engineering, or 'URL-hacking'.) </p> </div> <div class="comment-date">2024-06-14 11:55 UTC</div> </div> <div class="comment" id="5b3ac2db5a7a4b8697528e757652e6af"> <div class="comment-author"><a href="http://snape.me">James Snape</a> <a href="#5b3ac2db5a7a4b8697528e757652e6af">#</a></div> <div class="comment-content"> <p>I think we are basically agreeing here because I would never use natural keys nor externally visible synthetic keys for <em>physical</em> primary keys. (I think this statement is even more restrictive than the article's main premise). Well, with a rule exception for configurable enum type tables because the overhead of joining to resolve a single column value is inefficient. I would however always use a natural key for a <em>logical</em> primary key.</p> <p>The only reason why I'm slightly pedantic about this is due the the number of clients why have used surrogate keys in a logical model and then gone on to create databases where the concept of entity identity doesn't exist. This creates many of the issues <a href="https://processdecision.com">Nicholas Peterson</a> mentioned above: duplicates, historical change tracking, etc. Frankly, it doesn't help that lots of code examples for ORMs just start with an entity that has an ID attribute.</p> <p>One final comment on sharing data based on a golden master synthetic key. The moment you do I would argue that you have now committed to maintaining that key through all types of data mergers and acquisitions. It must never collide, and always point to exactly the same record and only that record. Since users can use it to refer to an entity and it makes up part of your external API, it now meets the definition of a natural key. Whether you agree or not on my stretching the definition a little, you still should not use this attribute as the physical primary key (record locator) because we should not expose implementation details in our APIs. The first Celko article I linked to explains some of the difficulties for externally visible synthetic keys.</p> </div> <div class="comment-date">2024-06-14 13:45 UTC</div> </div> <div class="comment" id="fee47871b1494293b039b67d21187e6b"> <div class="comment-author">Julius H <a href="#fee47871b1494293b039b67d21187e6b">#</a></div> <div class="comment-content"> <p>I'd like to comment with an example where using a synthetic key came back to bite me. My system had posts and users with synthetic IDs. Now I wanted to track an unread state across them. Naively, I designed just another entity:</p> <pre> public int ID { get; set; } public int PostID { get; set; } public int UserID { get; set; } </pre> <p>And it worked flawlessly for years. One day, however, a user complained that he always got an exception "Sequence contains more than one element". Of course I used SingleOrDefault() in application code because I expected 0 or 1 record per user and post. The quick solution was deleting the spurious table row. As a permanant solution I removed the ID field (and column) so the unread state had its natural key as primary key (both columns). So if it happens again in the future, the app will error on insertin rather than querying.</p> <p>Since my application is in control of the IDs and it's just a very simple join table I think it was the best solution. If the future requirements hold different kinds of unread state, I can always add the key again.</p> </div> <div class="comment-date">2024-07-22 14:40 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Continuous delivery without a CI server https://blog.ploeh.dk/2024/05/27/continuous-delivery-without-a-ci-server 2024-05-27T13:34:00+00:00 Mark Seemann <div id="post"> <p> <em>An illustrative example.</em> </p> <p> More than a decade ago, I worked on a small project. It was a small <a href="https://en.wikipedia.org/wiki/Single-page_application">single-page application</a> (SPA) with a <a href="https://en.wikipedia.org/wiki/REST">REST</a> API backend, deployed to <a href="https://azure.microsoft.com/">Azure</a>. As far as I recall, the REST API used <a href="https://en.wikipedia.org/wiki/Object_storage">blob storage</a>, so all in all it wasn't a complex system. </p> <p> We were two developers, and although we wanted to do <a href="https://en.wikipedia.org/wiki/Continuous_delivery">continuous delivery</a> (CD), we didn't have much development infrastructure. This was a little startup, and back then, there weren't a lot of free build services available. We were using <a href="https://github.com/">GitHub</a>, but it was before it had any free services to compile your code and run tests. </p> <p> Given those constraints, we figured out a simple way to do CD, even though we didn't have a <a href="https://en.wikipedia.org/wiki/Continuous_integration">continuous integration</a> (CI) server. </p> <p> I'll tell you how we did this. </p> <h3 id="d4bf05ee06c64650a15e5dae86a6efbd"> Shining an extraordinary light on the mundane <a href="#d4bf05ee06c64650a15e5dae86a6efbd">#</a> </h3> <p> The reason I'm relating this little story isn't to convince you that you, too, should do it that way. Rather, it's a didactic device. By doing something extreme, we can sometimes learn about the ordinary. </p> <blockquote> <p> You can only be pragmatic if you know how to be dogmatic. </p> <footer><cite><a href="/2018/11/12/what-to-test-and-not-to-test">What to test and not to test</a></cite>, me</footer> </blockquote> <p> From what I hear and read, it seems that there's a lot of organizations that believe that they're doing CI (or perhaps even CD) because they have a CI server. What the following tale will hopefully highlight is that, while build servers are useful, they aren't a requirement for CI or CD. </p> <h3 id="08dc5521141f4907bc44d43852716196"> Distributed CD <a href="#08dc5521141f4907bc44d43852716196">#</a> </h3> <p> Dramatis personae: My colleague and me. Scene: One small SPA project with a REST API and blob storage, to be deployed to Azure. Code base in GitHub. Two laptops. Remote work. </p> <p> One of us (let's say me) would start on implementing a feature, or fixing a bug. I'd use test-driven development (TDD) to get feedback on API ideas, as well as to accumulate a suite of regression tests. After a few hours of effective work, I'd send a pull request to my colleague. </p> <p> Since we were only two people on the team, the responsibility was clear. It was the other person's job to review the pull request. It was also clear that the longer the reviewer dawdled, the less efficient the process would be. For that reason, we'd typically have <a href="/2021/06/21/agile-pull-requests">agile pull requests</a> with a good turnaround time. </p> <p> While we were taking advantage of GitHub as a central coordination hub for pull requests, <a href="https://git-scm.com/">Git</a> itself is famously distributed. Thus, we wondered whether it'd be possible to make the CD process distributed as well. </p> <p> Yes, apart from GitHub, what we did was already distributed. </p> <h3 id="77a1cae8e41349f8b8092aec8fa512d3"> A little more automation <a href="#77a1cae8e41349f8b8092aec8fa512d3">#</a> </h3> <p> Since we were both doing TDD, we already had automated tests. Due to the simple setup of the system, we'd already automated more than 80% of our process. It wasn't much of a stretch to automate whatever else needed automation. Such as deployment. </p> <p> We agreed on a few simple rules: </p> <ul> <li>Every part of our process should be automated.</li> <li>Reviewing a pull request included running all tests.</li> </ul> <p> When people review pull requests, they often just go to GitHub and look around before issuing an LGTM. </p> <p> But, you <em>do</em> realize that this is Git, right? You can pull down the proposed changes and <em>run them</em>. </p> <p> What if you're already in the middle of something, working on the same code base? Stash your changes and pull down the code. </p> <p> The consequence of this process was that every time a pull request was accepted, we already knew that it passed all automated tests on two physical machines. We actually didn't need a server to run the tests a third time. </p> <p> <img src="/content/binary/distributed-cd.png" alt="Two laptops, a box indicating GitHub, and another box indicating a production system."> </p> <p> After a merge, the final part of the development process mandated that the original author should deploy to production. We had <a href="https://en.wikipedia.org/wiki/Bash_(Unix_shell)">Bash</a> script that did that. </p> <h3 id="cabd161b2cb34fe79caf3172bb339948"> Simplicity <a href="#cabd161b2cb34fe79caf3172bb339948">#</a> </h3> <p> This process came with some built-in advantages. First of all, it was <em>simple</em>. There wasn't a lot of moving parts, so there weren't many steps that could break. </p> <p> Have you ever had the pleasure of troubleshooting a build? The code works on your machine, but not on the build server. </p> <p> It sometimes turns out that there's a configuration mismatch with the compiler or test tools. Thus, the problem with the build server doesn't mean that you prevented a dangerous defect from being deployed to production. No, the code just didn't compile on the build server, but would actually have run fine on the production system. </p> <p> It's much easier troubleshooting issues on your own machine than on some remote server. </p> <p> I've also seen build servers that were set up to run tests, but along the way, something had failed and the tests didn't run. And no-one was looking at logs or warning emails from the build system because that system would already be sending hundreds of warnings a day. </p> <p> By agreeing to manually(!) run the automated tests as part of the review process, we were sure that they were exercised. </p> <p> Finally, by keeping the process simple, we could focus on what mattered: Delivering value to our customer. We didn't have to waste time learning how a proprietary build system worked. </p> <h3 id="2a427dee73f44fb4ab6753cf5246cc52"> Does it scale? <a href="#2a427dee73f44fb4ab6753cf5246cc52">#</a> </h3> <p> I know what you're going to say: This may have worked because the overall requirements were so simple. This will never work in a 'real' development organization, with a 'real' code base. </p> <p> I understand. I never claimed that it would. </p> <p> The point of this story is to highlight what CI and CD is. It's a way of working where you <em>continuously</em> integrate your code with everyone else's code, and where you <em>continuously</em> deploy changes to production. </p> <p> In reality, having a dedicated build system for that can be useful. These days, such systems tend to be services that integrate with GitHub or other sites, rather than an actual server that you have to care for. Even so, having such a system doesn't mean that your organization makes use of CI or CD. </p> <p> (Oh, and for the mathematically inclined: In this context <em>continuous</em> doesn't mean actually continuous. It just means <em>arbitrarily often</em>.) </p> <h3 id="097bd9130bb144c79ee3e75b0652a99f"> Conclusion <a href="#097bd9130bb144c79ee3e75b0652a99f">#</a> </h3> <p> CI and CD are processes that describe how we work with code, and how we work together. </p> <p> Continuous integration means that you often integrate your code with everyone else's code. How often? More than once a day. </p> <p> Continuous deployment means that you often deploy code changes to production. How often? Every time new code is integrated. </p> <p> A build system can be convenient to help along such processes, but it's strictly speaking not required. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fundamentals https://blog.ploeh.dk/2024/05/20/fundamentals 2024-05-20T07:04:00+00:00 Mark Seemann <div id="post"> <p> <em>How to stay current with technology progress.</em> </p> <p> A long time ago, I landed my dream job. My new employer was a consulting company, and my role was to be the resident <a href="https://en.wikipedia.org/wiki/Microsoft_Azure">Azure</a> expert. Cloud computing was still in its infancy, and there was a good chance that I might be able to establish myself as a leading regional authority on the topic. </p> <p> As part of the role, I was supposed to write articles and give presentations showing how to solve various problems with Azure. I dug in with fervour, writing sample code bases and even <a href="http://msdn.microsoft.com/en-us/magazine/gg983487.aspx">an MSDN Magazine article</a>. To my surprise, after half a year I realized that I was bored. </p> <p> At that time I'd already spent more than a decade learning new technology, and I knew that I was good at it. For instance, I worked five years for Microsoft Consulting Services, and a dirty little secret of that kind of role is that, although you're sold as an expert in some new technology, you're often only a few weeks ahead of your customer. For example, I was once engaged as a <a href="https://en.wikipedia.org/wiki/Windows_Workflow_Foundation">Windows Workflow Foundation</a> expert at a time when it was still in beta. No-one had years of experience with that technology, but I was still expected to know much more about it than my customer. </p> <p> I had lots of engagements like that, and they usually went well. I've always been good at cramming, and as a consultant you're also unencumbered by all the daily responsibilities and politics that often occupy the time and energy of regular employees. The point being that while I'm decent at learning new stuff, the role of being a consultant also facilitates that sort of activity. </p> <p> After more then a decade of learning new frameworks, new software libraries, new programming languages, new tools, new online services, it turned out that I was ready for something else. After spending a few months learning Azure, I realized that I'd lost interest in that kind of learning. When investigating a new Azure SDK, I'd quickly come to the conclusion that, <em>oh, this is just another object-oriented library</em>. There are these objects, and you call this method to do that, etc. That's not to say that learning a specific technology is a trivial undertaking. The worse the design, the more difficult it is to learn. </p> <p> Still, after years of learning new technologies, I'd started recognizing certain patterns. Perhaps, I thought, well-designed technologies are based on some fundamental ideas that may be worth learning instead. </p> <h3 id="ac37913a2b8248e6b51d4506c2da0481"> Staying current <a href="#ac37913a2b8248e6b51d4506c2da0481">#</a> </h3> <p> A common lament among software developers is that the pace of technology is so overwhelming that they can't keep up. This is true. You can't keep up. </p> <p> There will always be something that you don't know. In fact, most things you don't know. This isn't a condition isolated only to technology. The sum total of all human knowledge is so vast that you can't know it all. What you will learn, even after a lifetime of diligent study, will be a nanoscopic fraction of all human knowledge - even of everything related to software development. You can't stay current. Get used to it. </p> <p> A more appropriate question is: <em>How do I keep my skill set relevant?</em> </p> <p> Assuming that you wish to stay employable in some capacity, it's natural to be concerned with how your mad <a href="https://en.wikipedia.org/wiki/Adobe_Flash">Flash</a> skillz will land you the next gig. </p> <p> Trying to keep abreast of all new technologies in your field is likely to lead to burnout. Rather, put yourself in a position so that you can quickly learn necessary skills, just in time. </p> <h3 id="c529c0131b284fe1bca42bec0663fc8e"> Study fundamentals, rather than specifics <a href="#c529c0131b284fe1bca42bec0663fc8e">#</a> </h3> <p> Those many years ago, I realized that it'd be a better investment of my time to study fundamentals. Often, once you have some foundational knowledge, you can apply it in many circumstances. Your general knowledge will enable you to get quickly up to speed with specific technologies. </p> <p> Success isn't guaranteed, but knowing fundamentals increases your chances. </p> <p> This may still seem too abstract. Which fundamentals should you learn? </p> <p> In the remainder of this article, I'll give you some examples. The following collection of general programmer knowledge spans software engineering, computer science, broad ideas, but also specific tools. I only intend this set of examples to serve as inspiration. The list isn't complete, nor does it constitute a minimum of what you should learn. </p> <p> If you have other interests, you may put together your own research programme. What follows here are just some examples of fundamentals that I've found useful during my career. </p> <p> A criterion, however, for constituting foundational knowledge is that you should be able to apply that knowledge in a wide variety of contexts. The fundamental should not be tied to a particular programming language, platform, or operating system. </p> <h3 id="4f474189809f4d53b447b4005cef1bfd"> Design patterns <a href="#4f474189809f4d53b447b4005cef1bfd">#</a> </h3> <p> Perhaps the first foundational notion that I personally encountered was that of <em>design patterns</em>. As the Gang of Four (GoF) wrote in <a href="https://en.wikipedia.org/wiki/Design_Patterns">the book</a>, a design pattern is an abstract description of a solution that has been observed 'in the wild', more than once, independently evolved. </p> <p> Please pay attention to the causality. A design pattern isn't prescriptive, but descriptive. It's an observation that a particular code organisation tends to solve a particular problem. </p> <p> There are lots of misconceptions related to design patterns. One of them is that the 'library of patterns' is finite, and more or less constrained to the patterns included in the original book. </p> <p> There are, however, many more patterns. To illustrate how much wider this area is, here's a list of some patterns books in my personal library: </p> <ul> <li><a href="/ref/dp">Design Patterns</a></li> <li><a href="/ref/plopd3">Pattern Languages of Program Design 3</a></li> <li><a href="/ref/peaa">Patterns of Enterprise Application Architecture</a></li> <li><a href="/ref/eip">Enterprise Integration Patterns</a></li> <li><a href="/ref/xunit-patterns">xUnit Test Patterns</a></li> <li><a href="/ref/service-design-patterns">Service Design Patterns</a></li> <li><a href="/ref/implementation-patterns">Implementation Patterns</a></li> <li><a href="/ref/rest-cookbook">RESTful Web Services Cookbook</a></li> <li><a href="/ref/antipatterns">AntiPatterns</a></li> </ul> <p> In addition to these, there are many more books in my library that are patterns-adjacent, including <a href="/dippp">one of my own</a>. The point is that software design patterns is a vast topic, and it pays to know at least the most important ones. </p> <p> A design pattern fits the criterion that you can apply the knowledge independently of technology. The original GoF book has examples in <a href="https://en.wikipedia.org/wiki/C%2B%2B">C++</a> and <a href="https://en.wikipedia.org/wiki/Smalltalk">Smalltalk</a>, but I've found that they apply well to C#. Other people employ them in their <a href="https://www.java.com/">Java</a> code. </p> <p> Knowing design patterns not only helps you design solutions. That knowledge also enables you to recognize patterns in existing libraries and frameworks. It's this fundamental knowledge that makes it easier to learn new technologies. </p> <p> Often (although not always) successful software libraries and frameworks tend to follow known patterns, so if you're aware of these patterns, it becomes easier to learn such technologies. Again, be aware of the causality involved. I'm not claiming that successful libraries are explicitly designed according to published design patterns. Rather, some libraries become successful because they offer good solutions to certain problems. It's not surprising if such a good solution falls into a pattern that other people have already observed and recorded. It's like <a href="https://en.wikipedia.org/wiki/Parallel_evolution">parallel evolution</a>. </p> <p> This was my experience when I started to learn the details of Azure. Many of those SDKs and APIs manifested various design patterns, and once I'd recognized a pattern it became much easier to learn the rest. </p> <p> The idea of design patterns, particularly object-oriented design patterns, have its detractors, too. Let's visit that as the next set of fundamental ideas. </p> <h3 id="c44e7624ea3e4cef9485522146d17a6d"> Functional programming abstractions <a href="#c44e7624ea3e4cef9485522146d17a6d">#</a> </h3> <p> As I'm writing this, yet another Twitter thread pokes fun at object-oriented design (OOD) patterns as being nothing but a published collection of workarounds for the shortcomings of object orientation. The people who most zealously pursue that agenda tends to be functional programmers. </p> <p> Well, I certainly like functional programming (FP) better than OOD too, but rather than poking fun at OOD, I'm more interested in <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">how design patterns relate to universal abstractions</a>. I also believe that FP has shortcomings of its own, but I'll have more to say about that in a future article. </p> <p> Should you learn about <a href="/2017/10/06/monoids">monoids</a>, <a href="/2018/03/22/functors">functors</a>, <a href="/2022/03/28/monads">monads</a>, <a href="/2019/04/29/catamorphisms">catamorphisms</a>, and so on? </p> <p> Yes you should, because these ideas also fit the criterion that the knowledge is technology-independent. I've used my knowledge of these topics in <a href="https://www.haskell.org/">Haskell</a> (hardly surprising) and <a href="https://fsharp.org/">F#</a>, but also in C# and <a href="https://www.python.org/">Python</a>. The various <a href="https://en.wikipedia.org/wiki/Language_Integrated_Query">LINQ</a> methods are really just well-known APIs associated with, you guessed it, functors, monads, monoids, and catamorphisms. </p> <p> Once you've learned these fundamental ideas, it becomes easier to learn new technologies. This has happened to me multiple times, for example in contexts as diverse as property-based testing and asynchronous message-passing architectures. Once I realize that an API gives rise to a monad, say, I know that certain functions must be available. I also know how I should best compose larger code blocks from smaller ones. </p> <p> Must you know all of these concepts before learning, say, F#? No, not at all. Rather, a language like F# is a great vehicle for learning such fundamentals. There's a first time for learning anything, and you need to start somewhere. Rather, the point is that once you know these concepts, it becomes easier to learn the next thing. </p> <p> If, for example, you already know what a monad is when learning F#, picking up the idea behind <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a> is easy once you realize that it's just a compiler-specific way to enable syntactic sugaring of monadic expressions. You can learn how computation expressions work without that knowledge, too; it's just harder. </p> <p> This is a recurring theme with many of these examples. You can learn a particular technology without knowing the fundamentals, but you'll have to put in more time to do that. </p> <p> On to the next example. </p> <h3 id="02c8fb3f6fe74fb9bffda719122c60a9"> SQL <a href="#02c8fb3f6fe74fb9bffda719122c60a9">#</a> </h3> <p> Which <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping">object-relational mapper</a> (ORM) should you learn? <a href="https://hibernate.org/orm/">Hibernate</a>? <a href="https://learn.microsoft.com/ef/">Entity Framework</a>? </p> <p> How about learning <a href="https://en.wikipedia.org/wiki/SQL">SQL</a>? I learned SQL in 1999, I believe, and it's served me well ever since. I <a href="/2023/09/18/do-orms-reduce-the-need-for-mapping">consider raw SQL to be more productive than using an ORM</a>. Once more, SQL is largely technology-independent. While each database typically has its own SQL dialect, the fundamentals are the same. I'm most well-versed in the <a href="https://en.wikipedia.org/wiki/Microsoft_SQL_Server">SQL Server</a> dialect, but I've also used my SQL knowledge to interact with <a href="https://www.oracle.com/database/">Oracle</a> and <a href="https://www.postgresql.org/">PostgreSQL</a>. Once you know one SQL dialect, you can quickly solve data problems in one of the other dialects. </p> <p> It doesn't matter much whether you're interacting with a database from .NET, Haskell, Python, <a href="https://www.ruby-lang.org/">Ruby</a>, or another language. SQL is not only universal, the core of the language is stable. What I learned in 1999 is still useful today. Can you say the same about your current ORM? </p> <p> Most programmers prefer learning the newest, most cutting-edge technology, but that's a risky gamble. Once upon a time <a href="https://en.wikipedia.org/wiki/Microsoft_Silverlight">Silverlight</a> was a cutting-edge technology, and more than one of my contemporaries went all-in on it. </p> <p> On the contrary, most programmers find old stuff boring. It turns out, though, that it may be worthwhile learning some old technologies like SQL. Be aware of the <a href="https://en.wikipedia.org/wiki/Lindy_effect">Lindy effect</a>. If it's been around for a long time, it's likely to still be around for a long time. This is true for the next example as well. </p> <h3 id="e4a7c033c0964420a0abbf83a0bbb773"> HTTP <a href="#e4a7c033c0964420a0abbf83a0bbb773">#</a> </h3> <p> The <a href="https://en.wikipedia.org/wiki/HTTP">HTTP protocol</a> has been around since 1991. It's an effectively text-based protocol, and you can easily engage with a web server on a near-protocol level. This is true for other older protocols as well. </p> <p> In my first IT job in the late 1990s, one of my tasks was to set up and maintain <a href="https://en.wikipedia.org/wiki/Microsoft_Exchange_Server">Exchange Servers</a>. It was also my responsibility to make sure that email could flow not only within the organization, but that we could exchange email with the rest of the internet. In order to test my mail servers, I would often just <a href="https://en.wikipedia.org/wiki/Telnet">telnet</a> into them on port 25 and type in the correct, <a href="https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol">text-based instructions to send a test email</a>. </p> <p> Granted, it's not that easy to telnet into a modern web server on port 80, but a ubiquitous tool like <a href="https://curl.se/">curl</a> accomplishes the same goal. I recently wrote how <a href="/2024/05/13/gratification">knowing curl is better</a> than knowing <a href="https://www.postman.com/">Postman</a>. While this wasn't meant as an attack on Postman specifically, neither was it meant as a facile claim that curl is the only tool useful for ad-hoc interaction with HTTP-based APIs. Sometimes you only realize an underlying truth when you write about a thing and then <a href="/2024/05/13/gratification#9efea1cadb8c4e388bfba1a2064dd59a">other people find fault with your argument</a>. The underlying truth, I think, is that it pays to understand HTTP and being able to engage with an HTTP-based web service at that level of abstraction. </p> <p> Preferably in an automatable way. </p> <h3 id="e3a250b707b243dabc6609134e864aee"> Shells and scripting <a href="#e3a250b707b243dabc6609134e864aee">#</a> </h3> <p> The reason I favour curl over other tools to interact with HTTP is that I already spend quite a bit of time at the command line. I typically have a little handful of terminal windows open on my laptop. If I need to test an HTTP server, curl is already available. </p> <p> Many years ago, an employer introduced me to <a href="https://git-scm.com/">Git</a>. Back then, there were no good graphical tools to interact with Git, so I had to learn to use it from the command line. I'm eternally grateful that it turned out that way. I still use Git from the command line. </p> <p> When you install Git, by default you also install Git Bash. Since I was already using that shell to interact with Git, it began to dawn on me that it's a full-fledged shell, and that I could do all sorts of other things with it. It also struck me that learning <a href="https://www.gnu.org/software/bash/">Bash</a> would be a better investment of my time than learning <a href="https://learn.microsoft.com/powershell/">PowerShell</a>. At the time, there was no indication that PowerShell would ever be relevant outside of Windows, while Bash was already available on most systems. Even today, knowing Bash strikes me as more useful than knowing PowerShell. </p> <p> It's not that I do much Bash-scripting, but I could. Since I'm a programmer, if I need to automate something, I naturally reach for something more robust than shell scripting. Still, it gives me confidence to know that, since I already know Bash, Git, curl, etc., I <em>could</em> automate some tasks if I needed to. </p> <p> Many a reader will probably complain that the Git CLI has horrible <a href="/2024/05/13/gratification">developer experience</a>, but I will, again, postulate that it's not that bad. It helps if you understand some fundamentals. </p> <h3 id="a511cfd8d9bf4bbda433dbf70184284a"> Algorithms and data structures <a href="#a511cfd8d9bf4bbda433dbf70184284a">#</a> </h3> <p> Git really isn't that difficult to understand once you realize that a Git repository is just a <a href="https://en.wikipedia.org/wiki/Directed_acyclic_graph">directed acyclic graph</a> (DAG), and that branches are just labels that point to nodes in the graph. There are basic data structures that it's just useful to know. DAGs, <a href="https://en.wikipedia.org/wiki/Tree_(graph_theory)">trees</a>, <a href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)">graphs</a> in general, <a href="https://en.wikipedia.org/wiki/Adjacency_list">adjacency lists</a> or <a href="https://en.wikipedia.org/wiki/Adjacency_matrix">adjacency matrices</a>. </p> <p> Knowing that such data structures exist is, however, not that useful if you don't know what you can <em>do</em> with them. If you have a graph, you can find a <a href="https://en.wikipedia.org/wiki/Minimum_spanning_tree">minimum spanning tree</a> or a <a href="https://en.wikipedia.org/wiki/Shortest-path_tree">shortest-path tree</a>, which sometimes turn out to be useful. Adjacency lists or matrices give you ways to represent graphs in code, which is why they are useful. </p> <p> Contrary to certain infamous interview practices, you don't need to know these algorithms by heart. It's usually enough to know that they exist. I can't remember <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Dijkstra's algorithm</a> off the top of my head, but if I encounter a problem where I need to find the shortest path, I can look it up. </p> <p> Or, if presented with the problem of constructing current state from an Event Store, you may realize that it's just a left <a href="https://en.wikipedia.org/wiki/Fold_(higher-order_function)">fold</a> over a <a href="https://en.wikipedia.org/wiki/Linked_list">linked list</a>. (This isn't my own realization; I first heard it from <a href="https://gotocon.com/cph-2011/presentation/Behavior!">Greg Young in 2011</a>.) </p> <p> Now we're back at one of the first examples, that of FP knowledge. A <a href="/2019/05/27/list-catamorphism">list fold is its catamorphism</a>. Again, these things are much easier to learn if you already know some fundamentals. </p> <h3 id="f109425f27014cd5bd395a74e9575355"> What to learn <a href="#f109425f27014cd5bd395a74e9575355">#</a> </h3> <p> These examples may seems overwhelming. Do you really need to know all of that before things become easier? </p> <p> No, that's not the point. I didn't start out knowing all these things, and some of them, I'm still not very good at. The point is rather that if you're wondering how to invest your limited time so that you can remain up to date, consider pursuing general-purpose knowledge rather than learning a specific technology. </p> <p> Of course, if your employer asks you to use a particular library or programming language, you need to study <em>that</em>, if you're not already good at it. If, on the other hand, you decide to better yourself, you can choose what to learn next. </p> <p> Ultimately, if your're learning for your own sake, the most important criterion may be: Choose something that interests you. If no-one forces you to study, it's too easy to give up if you lose interest. </p> <p> If, however, you have the choice between learning <a href="https://mjvl.github.io/Noun.js/">Noun.js</a> or design patterns, may I suggest the latter? </p> <h3 id="94c3f380b556403d82dd9f3cd0c1d1e9"> For life <a href="#94c3f380b556403d82dd9f3cd0c1d1e9">#</a> </h3> <p> When are you done, you ask? </p> <p> Never. There's more stuff than you can learn in a lifetime. I've met a lot of programmers who finally give up on the grind to keep up, and instead become managers. </p> <p> As if there's nothing to learn when you're a manager. I'm fortunate that, before <a href="/2011/11/08/Independency">I went solo</a>, I mainly had good managers. I'm under no illusion that they automatically became good managers. All I've heard said about management is that there's a lot to learn in that field, too. Really, it'd be surprising if that wasn't the case. </p> <p> I can understand, however, how just keep learning the next library, the next framework, the next tool becomes tiring. As I've already outlined, I hit that wall more than a decade ago. </p> <p> On the other hand, there are so many wonderful fundamentals that you can learn. You can do self-study, or you can enrol in a more formal programme if you have the opportunity. I'm currently following a course on compiler design. It's not that I expect to pivot to writing compilers for the rest of my career, but rather, </p> <blockquote> <ol type="a"> <li>"It is considered a topic that you should know in order to be "well-cultured" in computer science.</li> <li>"A good craftsman should know his tools, and compilers are important tools for programmers and computer scientists.</li> <li>"The techniques used for constructing a compiler are useful for other purposes as well.</li> <li>"There is a good chance that a programmer or computer scientist will need to write a compiler or interpreter for a domain-specific language."</li> </ol> <footer><cite><a href="/ref/introduction-to-compiler-design">Introduction to Compiler Design</a></cite> (from the introduction), Torben Ægidius Mogensen</footer> </blockquote> <p> That's good enough for me, and so far, I'm enjoying the course (although it's also hard work). </p> <p> You may not find this particular topic interesting, but then hopefully you can find something else that you fancy. 3D rendering? Machine learning? Distributed systems architecture? </p> <h3 id="7519e9b6147d49379f545c69871c381a"> Conclusion <a href="#7519e9b6147d49379f545c69871c381a">#</a> </h3> <p> Technology moves at a pace with which it's impossible to keep up. It's not just you who's falling behind. Everyone is. Even the best-paid <a href="https://en.wikipedia.org/wiki/Big_Tech">GAMMA</a> programmer knows next to nothing of all there is to know in the field. They may have superior skills in certain areas, but there will be so much other stuff that they don't know. </p> <p> You may think of me as a <a href="https://x.com/hillelogram/status/1445435617047990273">thought leader</a> if you will. If nothing else, I tend to be a prolific writer. Perhaps you even think I'm a good programmer. I should hope so. Who fancies themselves bad at something? </p> <p> You should, however, have seen me struggle with <a href="https://en.wikipedia.org/wiki/C_(programming_language)">C</a> programming during a course on computer systems programming. There's a thing I'm happy if I never have to revisit. </p> <p> You can't know it all. You can't keep up. But you can focus on learning the fundamentals. That tends to make it easier to learn specific technologies that build on those foundations. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Gratification https://blog.ploeh.dk/2024/05/13/gratification 2024-05-13T06:27:00+00:00 Mark Seemann <div id="post"> <p> <em>Some thoughts on developer experience.</em> </p> <p> Years ago, I was introduced to a concept called <em>developer ergonomics</em>. Despite the name, it's not about good chairs, standing desks, or multiple monitors. Rather, the concept was related to how easy it'd be for a developer to achieve a certain outcome. How easy is it to set up a new code base in a particular language? How much work is required to save a row in a database? How hard is it to read rows from a database and display the data on a web page? And so on. </p> <p> These days, we tend to discuss <em>developer experience</em> rather than ergonomics, and that's probably a good thing. This term more immediately conveys what it's about. </p> <p> I've recently had some discussions about developer experience (DevEx, DX) with one of my customers, and this has lead me to reflect more explicitly on this topic than previously. Most of what I'm going to write here are opinions and beliefs that go back a long time, but apparently, it's only recently that these notions have congealed in my mind under the category name <em>developer experience</em>. </p> <p> This article may look like your usual old-man-yells-at-cloud article, but I hope that I can avoid that. It's not the case that I yearn for some lost past where 'we' wrote <a href="https://en.wikipedia.org/wiki/Plankalk%C3%BCl">Plankalkül</a> in <a href="https://en.wikipedia.org/wiki/Edlin">Edlin</a>. That, in fact, sounds like a horrible developer experience. </p> <p> The point, rather, is that most attractive things come with consequences. For anyone who have been reading this blog even once in a while, this should come as no surprise. </p> <h3 id="cbc9752f754e40cc94267689f5dd87bf"> Instant gratification <a href="#cbc9752f754e40cc94267689f5dd87bf">#</a> </h3> <p> Fat foods, cakes, and wine can be wonderful, but can be detrimental to your health if you overindulge. It can, however, be hard to resist a piece of chocolate, and even if we think that we shouldn't, we often fail to restrain ourselves. The temptation of instant gratification is simply too great. </p> <p> There are other examples like this. The most obvious are the use of narcotics, lack of exercise, smoking, and dropping out of school. It may feel good in the moment, but can have long-term consequences. </p> <p> Small children are notoriously bad at delaying gratification, and we often associate the ability to delay gratification with maturity. We all, however, fall in from time to time. Food and wine are my weak spots, while I don't do drugs, and I didn't drop out of school. </p> <p> It strikes me that we often talk about ideas related to developer experience in a way where we treat developers as children. To be fair, many developers also act like children. I don't know how many times I've <ins datetime="2024-06-17T08:26Z">heard</ins> something like, <em>"I don't want to write tests/go through a code review/refactor! I just want to ship working code now!"</em> </p> <p> Fine, so do I. </p> <p> Even if wine is bad for me, it makes life worth living. As the saying goes, even if you don't smoke, don't drink, exercise rigorously, eat healthily, don't do drugs, and don't engage in dangerous activities, you're not guaranteed to live until ninety, but you're guaranteed that it's going to <em>feel</em> that long. </p> <p> Likewise, I'm aware that doing everything right can sometimes take so long that by the time we've deployed the software, it's too late. The point isn't to always or never do certain things, but rather to be aware of the consequences of our choices. </p> <h3 id="ac2969093f264da092186fa0cb7196e5"> Developer experience <a href="#ac2969093f264da092186fa0cb7196e5">#</a> </h3> <p> I've no problem with aiming to make the experience of writing software as good as possible. Some developer-experience thought leaders talk about the importance of documentation, predictability, and timeliness. Neither do I mind that a development environment looks good, completes my words, or helps me refactor. </p> <p> To return to the analogy of human vices, not everything that feels good is ultimately bad for you. While I do like wine and chocolate, I also love <a href="https://en.wikipedia.org/wiki/Sushi">sushi</a>, white <a href="https://en.wikipedia.org/wiki/Asparagus">asparagus</a>, <a href="https://en.wikipedia.org/wiki/Turbot">turbot</a>, <a href="https://en.wikipedia.org/wiki/Chanterelle">chanterelles</a>, <a href="https://en.wikipedia.org/wiki/Cyclopterus">lumpfish</a> roe <a href="https://en.wikipedia.org/wiki/Caviar">caviar</a>, <a href="https://en.wikipedia.org/wiki/Morchella">true morels</a>, <a href="https://en.wikipedia.org/wiki/Nephrops_norvegicus">Norway lobster</a>, and various other foods that tend to be categorized as healthy. </p> <p> A good <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> with refactoring support, statement completion, type information, test runner, etc. is certainly preferable to writing all code in <a href="https://en.wikipedia.org/wiki/Windows_Notepad">Notepad</a>. </p> <p> That said, there's a certain kind of developer tooling and language features that strikes me as more akin to candy. These are typically tools and technologies that tend to demo well. Recent examples include <a href="https://www.openapis.org/">OpenAPI</a>, <a href="https://github.com/features/copilot">GitHub Copilot</a>, <a href="https://learn.microsoft.com/dotnet/csharp/fundamentals/program-structure/top-level-statements">C# top-level statements</a>, code generation, and <a href="https://www.postman.com/">Postman</a>. Not all of these are unequivocally bad, but they strike me as mostly aiming at immature developers. </p> <p> The point of this article isn't to single out these particular products, standards, or language features, but on the other hand, in order to make a point, I do have to at least outline why I find them problematic. They're just examples, and I hope that by explaining what is on my mind, you can see the pattern and apply it elsewhere. </p> <h3 id="f7f676bf5a334b189b3c2baab18b1e6a"> OpenAPI <a href="#f7f676bf5a334b189b3c2baab18b1e6a">#</a> </h3> <p> A standard like OpenAPI, for example, looks attractive because it automates or standardizes much work related to developing and maintaining <a href="https://en.wikipedia.org/wiki/REST">REST APIs</a>. Frameworks and tools that leverage that standard automatically creates machine-readable <a href="/2024/04/15/services-share-schema-and-contract-not-class">schema and contract</a>, which can be used to generate client code. Furthermore, an OpenAPI-aware framework can also autogenerate an entire web-based graphical user interface, which developers can use for ad-hoc testing. </p> <p> I've worked with clients who also published these OpenAPI user interfaces to their customers, so that it was easy to get started with the APIs. Easy onboarding. </p> <p> Instant gratification. </p> <p> What's the problem with this? There are clearly enough apparent benefits that I usually have a hard time talking my clients out of pursuing this strategy. What are the disadvantages? Essentially, OpenAPI locks you into <a href="https://martinfowler.com/articles/richardsonMaturityModel.html">level 2</a> APIs. No hypermedia controls, no <a href="/2015/06/22/rest-implies-content-negotiation">smooth conneg-based versioning</a>, no <a href="https://en.wikipedia.org/wiki/HATEOAS">HATEOAS</a>. In fact, most of what makes REST flexible is lost. What remains is an ad-hoc, informally-specified, bug-ridden, slow implementation of half of <a href="https://en.wikipedia.org/wiki/SOAP">SOAP</a>. </p> <p> I've <a href="/2022/12/05/github-copilot-preliminary-experience-report">previously described my misgivings about Copilot</a>, and while I actually still use it, I don't want to repeat all of that here. Let's move on to another example. </p> <h3 id="f56e835825464650a86c557c7253f095"> Top-level statements <a href="#f56e835825464650a86c557c7253f095">#</a> </h3> <p> Among many other language features, C# 9 got <em>top-level-statements</em>. This means that you don't need to write a <code>Main</code> method in a static class. Rather, you can have a single C# code file where you can immediately start executing code. </p> <p> It's not that I consider this language feature particularly harmful, but it also solves what seems to me a non-problem. It demos well, though. If I understand the motivation right, the feature exists because 'modern' developers are used to languages like <a href="https://www.python.org/">Python</a> where you can, indeed, just create a <code>.py</code> file and start adding code statements. </p> <p> In an attempt to make C# more attractive to such an audience, it, too, got that kind of developer experience enabled. </p> <p> You may argue that this is a bid to remove some of the ceremony from the language, but I'm not convinced that this moves that needle much. The <a href="/2019/12/16/zone-of-ceremony">level of ceremony that a language like C# has is much deeper than that</a>. That's not to target C# in particular. <a href="https://www.java.com/">Java</a> is similar, and don't even get me started on <a href="https://en.wikipedia.org/wiki/C_(programming_language)">C</a> or <a href="https://en.wikipedia.org/wiki/C%2B%2B">C++</a>! Did anyone say <em>header files?</em> </p> <p> Do 'modern' developers choose Python over C# because they can't be arsed to write a <code>Main</code> method? If that's the <em>only</em> reason, it strikes me as incredibly immature. <em>I want instant gratification, and writing a <code>Main</code> method is just too much trouble!</em> </p> <p> If developers do, indeed, choose Python or JavaScript over C# and Java, I hope and believe that it's for other reasons. </p> <p> This particular C# feature doesn't bother me, but I find it symptomatic of a kind of 'innovation' where language designers target instant gratification. </p> <h3 id="b9ce02aa90074838bd7b8e2cec0189e2"> Postman <a href="#b9ce02aa90074838bd7b8e2cec0189e2">#</a> </h3> <p> Let's consider one more example. You may think that I'm now attacking a company that, for all I know, makes a decent product. I don't really care about that, though. What I do care about is the developer mentality that makes a particular tool so ubiquitous. </p> <p> I've met web service developers who would be unable to interact with the HTTP APIs that they are themselves developing if they didn't have Postman. Likewise, there are innumerable questions on <a href="https://stackoverflow.com/">Stack Overflow</a> where people ask questions about HTTP APIs and post screen shots of Postman sessions. </p> <p> It's okay if you don't know how to interact with an HTTP API. After all, there's a first time for everything, and there was a time when I didn't know how to do this either. Apparently, however, it's easier to install an application with a graphical user interface than it is to use <a href="https://curl.se/">curl</a>. </p> <p> Do yourself a favour and learn curl instead of using Postman. Curl is a command-line tool, which means that you can use it for both ad-hoc experimentation and automation. It takes five to ten minutes to learn the basics. It's also free. </p> <p> It still seems to me that many people are of a mind that it's easier to use Postman than to learn curl. Ultimately, I'd wager that for any task you do with some regularity, it's more productive to learn the text-based tool than the point-and-click tool. In a situation like this, I'd suggest that delayed gratification beats instant gratification. </p> <h3 id="50ed56effb784c95a6f6de4967e883ef"> CV-driven development <a href="#50ed56effb784c95a6f6de4967e883ef">#</a> </h3> <p> It is, perhaps, easy to get the wrong impression from the above examples. I'm not pointing fingers at just any 'cool' new technology. There are techniques, languages, frameworks, and so on, which people pick up because they're exciting for other reasons. Often, such technologies solve real problems in their niches, but are then applied for the sole reason that people want to get on the bandwagon. Examples include <a href="https://kubernetes.io/">Kubernetes</a>, mocks, <a href="/2012/11/06/WhentouseaDIContainer">DI Containers</a>, <a href="/2023/12/04/serialization-with-and-without-reflection">reflection</a>, <a href="https://en.wikipedia.org/wiki/Aspect-oriented_programming">AOP</a>, and <a href="https://en.wikipedia.org/wiki/Microservices">microservices</a>. All of these have legitimate applications, but we also hear about many examples where people use them just to use them. </p> <p> That's a different problem from the one I'm discussing in this article. Usually, learning about such advanced techniques requires delaying gratification. There's nothing wrong with learning new skills, but part of that process is also gaining the understanding of when to apply the skill, and when not to. That's a different discussion. </p> <h3 id="10cc039f39ba4c2caab34f66f17f90b2"> Innovation is fine <a href="#10cc039f39ba4c2caab34f66f17f90b2">#</a> </h3> <p> The point of this article isn't that every innovation is bad. Contrary to <a href="https://www.charlespetzold.com/">Charles Petzold</a>, I don't really believe that Visual Studio rots the mind, although I once did publish <a href="/2013/02/04/BewareofProductivityTools">an article</a> that navigated the same waters. </p> <p> Despite my misgivings, I haven't uninstalled GitHub Copilot, and I do enjoy many of the features in both Visual Studio (VS) and Visual Studio Code (VS Code). I also welcome and use many new language features in various languages. </p> <p> I can certainly appreciate how an IDE makes many things easier. Every time I have to begin a new <a href="https://www.haskell.org/">Haskell</a> code base, I long for the hand-holding offered by Visual Studio when creating a new C# project. </p> <p> And although I don't use the debugger much, the built-in debuggers in VS and VS Code sure beat <a href="https://en.wikipedia.org/wiki/GNU_Debugger">GDB</a>. It even works in Python! </p> <p> There's even tooling that <a href="https://developercommunity.visualstudio.com/t/Test-Explorer:-Better-support-for-TDD-wo/701822">I wish for</a>, but apparently never will get. </p> <h3 id="675450a5c0cf441fa433b928251de8a5"> Simple made easy <a href="#675450a5c0cf441fa433b928251de8a5">#</a> </h3> <p> In <a href="https://www.infoq.com/presentations/Simple-Made-Easy/">Simple Made Easy</a> Rich Hickey follows his usual look-up-a-word-in-the-dictionary-and-build-a-talk-around-the-definition style to contrast <em>simple</em> with <em>easy</em>. I find his distinction useful. A tool or technique that's <em>close at hand</em> is <em>easy</em>. This certainly includes many of the above instant-gratification examples. </p> <p> An <em>easy</em> technique is not, however, necessarily <em>simple</em>. It may or may not be. <a href="https://en.wikipedia.org/wiki/Rich_Hickey">Rich Hickey</a> defines <em>simple</em> as the opposite of <em>complex</em>. Something that is complex is assembled from parts, whereas a simple thing is, ideally, single and undivisible. In practice, truly simple ideas and tools may not be available, and instead we may have to settle with things that are less complex than their alternatives. </p> <p> Once you start looking for things that make simple things easy, you see them in many places. A big category that I personally favour contains all the language features and tools that make functional programming (FP) easier. FP tends to be simpler than object-oriented or procedural programming, because it <a href="/2018/11/19/functional-architecture-a-definition">explicitly distinguishes between and separates</a> predictable code from unpredictable code. This does, however, in itself tend to make some programming tasks harder. How do you generate a random number? Look up the system time? Write a record to a database? </p> <p> Several FP languages have special features that make even those difficult tasks easy. <a href="https://fsharp.org/">F#</a> has <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a> and <a href="https://www.haskell.org/">Haskell</a> has <a href="https://en.wikibooks.org/wiki/Haskell/do_notation">do notation</a>. </p> <p> Let's say you want to call a function that consumes a random number generator. In Haskell (as in .NET) random number generators are actually deterministic, as long as you give them the same seed. Generating a random seed, on the other hand, is non-deterministic, so has to happen in <a href="/2020/06/08/the-io-container">IO</a>. </p> <p> Without <code>do</code> notation, you could write the action like this: </p> <p> <pre><span style="color:#2b91af;">rndSelect</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Integral</span>&nbsp;i&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;[a] rndSelect&nbsp;xs&nbsp;count&nbsp;=&nbsp;(\rnd&nbsp;-&gt;&nbsp;rndGenSelect&nbsp;rnd&nbsp;xs&nbsp;count)&nbsp;&lt;$&gt;&nbsp;newStdGen</pre> </p> <p> (The type annotation is optional.) While terse, this is hardly readable, and the developer experience also leaves something to be desired. Fortunately, however, you can <a href="/2018/07/09/typing-and-testing-problem-23">rewrite this action</a> with <code>do</code> notation, like this: </p> <p> <pre><span style="color:#2b91af;">rndSelect</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;i&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;[a] rndSelect&nbsp;xs&nbsp;count&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;rnd&nbsp;&lt;-&nbsp;newStdGen &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;rndGenSelect&nbsp;rnd&nbsp;xs&nbsp;count </pre> </p> <p> Now we can clearly see that the action first creates the <code>rnd</code> random number generator and then passes it to <code>rndGenSelect</code>. That's what happened before, but it was buried in a lambda expression and Haskell's right-to-left causality. Most people would find the first version (without <code>do</code> notation) less readable, and more difficult to write. </p> <p> Related to <em>developer ergonomics</em>, though, <code>do</code> notation makes the simple code (i.e. code that separates predictable code from unpredictable code) easy (that is; <em>at hand</em>). </p> <p> F# computation expressions offer the same kind of syntactic sugar, making it easy to write simple code. </p> <h3 id="86e9a9bd6cfc4408bedace8acd330f64"> Delay gratification <a href="#86e9a9bd6cfc4408bedace8acd330f64">#</a> </h3> <p> While it's possible to set up a development context in such a way that it nudges you to work in a way that's ultimately good for you, temptation is everywhere. </p> <p> Not only may new language features, IDE functionality, or frameworks entice you to do something that may be disadvantageous in the long run. There may also be actions you don't take because it just feels better to move on. </p> <p> Do you take the time to write good commit messages? Not just a single-line heading, but <a href="https://github.com/GreanTech/AtomEventStore/commit/615cdee2c4d675d412e6669bcc0678655376c4d1">a proper message that explains your context and reasoning</a>? </p> <p> Most people I've observed working with source control 'just want to move on', and can't be bothered to write a useful commit message. </p> <p> I hear about the same mindset when it comes to code reviews, particularly pull request reviews. Everyone 'just wants to write code', and no-one want to review other people's code. Yet, in a shared code base, you have to live with the code that other people write. Why not review it so that you have a chance to decide what that shared code base should look like? </p> <p> Delay your own gratification a bit, and reap the awards later. </p> <h3 id="630055ed606d43289d71232dd1ef1c25"> Conclusion <a href="#630055ed606d43289d71232dd1ef1c25">#</a> </h3> <p> The only goal I have with this article is to make you think about the consequences of new and innovative tools and frameworks. Particularly if they are immediately compelling, they may be empty calories. Consider if there may be disadvantages to adopting a new way of doing things. </p> <p> Some tools and technologies give you instant gratification, but may be unhealthy in the long run. This is, like most other things, context-dependent. <a href="/2023/01/16/in-the-long-run">In the long run</a> your company may no longer be around. Sometimes, it pays to deliberately do something that you know is bad, in order to reach a goal before your competition. That was the original <em>technical debt</em> metaphor. </p> <p> Often, however, it pays to delay gratification. Learn curl instead of Postman. Learn to design proper REST APIs instead of relying on OpenAI. If you need to write ad-hoc scripts, <a href="/2024/02/05/statically-and-dynamically-typed-scripts">use a language suitable for that</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9efea1cadb8c4e388bfba1a2064dd59a"> <div class="comment-author"><a href="https://thomaslevesque.com">Thomas Levesque</a> <a href="#9efea1cadb8c4e388bfba1a2064dd59a">#</a></div> <div class="comment-content"> <p> Regarding Postman vs. curl, I have to disagree. Sure, curl is pretty easy to use. But while it's good for one-off tests, it sucks when you need to maintain a collection of requests that you can re-execute whevenever you want. In a testing session, you either need to re-type whole command, or reuse a previous command from the shell's history. Or have a file with all your commands and copy-paste to the shell. Either way, it's not a good experience. </p> <p> That being said, I'm not very fond of Postman either. It's too heavyweight for what it does, IMHO, and the import/export mechanism is terrible for sharing collections with the team. These days, I tend to use VSCode extensions like <a href="https://github.com/AnWeber/vscode-httpyac">httpYac</a> or <a href="https://github.com/Huachao/vscode-restclient">REST Client</a>, or the equivalent that is now built into Visual Studio and Rider. It's much easier to work with than Postman (it's just text), while still being interactive. And since it's just a text file, you can just add it to the Git to share it with the team. </p> </div> <div class="comment-date">2024-05-14 02:38 UTC</div> </div> <div class="comment" id="9efea1cadb8c4e388bfba1a2064dd59b"> <div class="comment-author"><a href="https://majiehong.com/">Jiehong</a> <a href="#9efea1cadb8c4e388bfba1a2064dd59b">#</a></div> <div class="comment-content"> <p> @Thomas Levesque: I agree with you, yet VSCode or Rider's extensions lock you into an editor quite quickly. </p> <p> But you can have the best of both worlds: a cli tool first, with editor extensions. Just like <a href="https://github.com/Orange-OpenSource/hurl">Hurl</a>. </p> <p> Note that you can run a <a href="https://everything.curl.dev/cmdline/configfile.html#urls">curl command from a file with</a> <code>curl --config [curl_request.file]</code>, it makes chaining requests (like with login and secrets) rather cumbersome very quickly. </p> </div> <div class="comment-date">2024-05-16 13:57 UTC</div> </div> <div class="comment" id="2a6dd3839e2e4bf9b06071221b330356"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2a6dd3839e2e4bf9b06071221b330356">#</a></div> <div class="comment-content"> <p> Thank you, both, for writing. In the end, it's up to every team to settle on technical solutions that work for them, in that context. Likewise, it's up to each developer to identify methodology and tools that work for her or him, as long as it doesn't impact the rest of the team. </p> <p> The reason I suggest curl over other alternatives is that not only is it free, it also tends to be ubiquitous. Most systems come with curl baked in - perhaps not a consumer installation of Windows, but if you have developer tools installed, it's highly likely that you have curl on your machine. It's <a href="/2024/05/20/fundamentals">a fundamental skill that may serve you well if you know it</a>. </p> <p> In addition to that, since curl is a CLI you can always script it if you need a kind of semi-automation. What prevents you from maintaining a collection of script files? They could even take command-line arguments, if you'd like. </p> <p> That said, personally, if I realize that I need to maintain a collection of requests that I can re-execute whenever I want, I'd prefer writing a 'real' program. On the other hand, I find a tool like curl useful for ad-hoc testing. </p> </div> <div class="comment-date">2024-05-21 5:36 UTC</div> </div> <div class="comment" id="be53c7b6d29a43e0aa0fdac3fcce835d"> <div class="comment-author">Johannes Egger <a href="#be53c7b6d29a43e0aa0fdac3fcce835d">#</a></div> <div class="comment-content"> <blockquote> ... maintain a collection of requests that you can re-execute whevenever you want. </blockquote> <p>@Thomas Levesque: that sounds like a proper collection of automatically executable tests would be a better fit. But yeah, it's just easier to write those simple commands than to set up a test project - instant gratification 😉</p> </div> <div class="comment-date">2024-05-28 17:02 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Conservative codomain conjecture https://blog.ploeh.dk/2024/05/06/conservative-codomain-conjecture 2024-05-06T06:35:00+00:00 Mark Seemann <div id="post"> <p> <em>An API design heuristic.</em> </p> <p> For a while now, I've been wondering whether, in the language of <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>, one should favour being liberal in what one accepts over being conservative in what one sends. Yes, according to the design principle, a protocol or API should do both, but sometimes, you can't do that. Instead, you'll have to choose. I've recently reached the tentative conclusion that it may be a good idea favouring being conservative in what one sends. </p> <p> Good API design explicitly considers <em>contracts</em>. What are the preconditions for invoking an operation? What are the postconditions? Are there any invariants? These questions are relevant far beyond object-oriented design. They are <a href="/2022/10/24/encapsulation-in-functional-programming">equally important in Functional Programming</a>, as well as <a href="/2024/04/15/services-share-schema-and-contract-not-class">in service-oriented design</a>. </p> <p> If you have a type system at your disposal, you can often model pre- and postconditions as types. In practice, however, it frequently turns out that there's more than one way of doing that. You can model an additional precondition with an input type, but you can also model potential errors as a return type. Which option is best? </p> <p> That's what this article is about, and my conjecture is that constraining the input type may be preferable, thus being conservative about what is returned. </p> <h3 id="7ef0610940fb4670b7cf12a21bdd725f"> An average example <a href="#7ef0610940fb4670b7cf12a21bdd725f">#</a> </h3> <p> That's all quite abstract, so for the rest of this article, I'll discuss this kind of problem in the context of an example. We'll revisit the <a href="/2020/02/03/non-exceptional-averages">good old example of calculating an average value</a>. This example, however, is only a placeholder for any kind of API design problem. This article is only superficially about designing an API for calculating an <a href="https://en.wikipedia.org/wiki/Average">average</a>. More generally, this is about API design. I like the <em>average</em> example because it's easy to follow, and it does exhibit some characteristics that you can hopefully extrapolate from. </p> <p> In short, what is the contract of the following method? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.Zero; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+=</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>++; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>; }</pre> </p> <p> What are the preconditions? What are the postconditions? Are there any invariants? </p> <p> Before I answer these questions, I'll offer equivalent code in two other languages. Here it is in <a href="https://fsharp.org/">F#</a>: </p> <p> <pre>let&nbsp;average&nbsp;(timeSpans&nbsp;:&nbsp;TimeSpan&nbsp;seq)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;timeSpans &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.averageBy&nbsp;(_.Ticks&nbsp;&gt;&gt;&nbsp;double) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;int64 &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;TimeSpan.FromTicks</pre> </p> <p> And in <a href="https://www.haskell.org/">Haskell</a>: </p> <p> <pre><span style="color:#2b91af;">average</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Fractional</span>&nbsp;a,&nbsp;<span style="color:blue;">Foldable</span>&nbsp;t)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;t&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a average&nbsp;xs&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;xs&nbsp;/&nbsp;<span style="color:blue;">fromIntegral</span>&nbsp;(<span style="color:blue;">length</span>&nbsp;xs)</pre> </p> <p> These three examples have somewhat different implementations, but the same externally observable behaviour. What is the contract? </p> <p> It seems straightforward: If you input a sequence of values, you get the average of all of those values. Are there any preconditions? Yes, the sequence can't be empty. Given an empty sequence, all three implementations throw an exception. (The Haskell version is a little more nuanced than that, but given an empty list of <a href="https://hackage.haskell.org/package/time/docs/Data-Time-Clock.html#t:NominalDiffTime">NominalDiffTime</a>, it does throw an exception.) </p> <p> Any other preconditions? At least one more: The sequence must be finite. All three functions allow infinite streams as input, but if given one, they will fail to return an average. </p> <p> Are there any postconditions? I can only think of a statement that relates to the preconditions: <em>If</em> the preconditions are fulfilled, the functions will return the correct average value (within the precision allowed by floating-point calculations). </p> <p> All of this, however, is just warming up. We've <a href="/2020/02/03/non-exceptional-averages">been over this ground before</a>. </p> <h3 id="7922b269c9924877abe993cb282440a8"> Modelling contracts <a href="#7922b269c9924877abe993cb282440a8">#</a> </h3> <p> Keep in mind that this <em>average</em> function is just an example. Think of it as a stand-in for a procedure that's much more complicated. Think of the most complicated operation in your code base. </p> <p> Not only do real code bases have many complicated operations. Each comes with its own contract, different from the other operations, and if the team isn't explicitly thinking in terms of contracts, these contracts may change over time, as the team adds new features and fixes bugs. </p> <p> It's difficult work to keep track of all those contracts. As I argue in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, it helps if you can automate away some of that work. One way is having good test coverage. Another is to leverage a static type system, if you're fortunate enough to work in a language that has one. As I've <em>also</em> already covered, <a href="/2022/08/22/can-types-replace-validation">you can't replace all rules with types</a>, but it doesn't mean that using the type system is ineffectual. Quite the contrary. Every part of a contract that you can offload to the type system frees up your brain to think about something else - something more important, hopefully. </p> <p> Sometimes there's no good way to to model a precondition with a type, or <a href="https://buttondown.email/hillelwayne/archive/making-illegal-states-unrepresentable/">perhaps it's just too awkward</a>. At other times, there's really only a single way to address a concern. When it comes to the precondition that you can't pass an infinite sequence to the <em>average</em> function, <a href="/2020/02/03/non-exceptional-averages">change the type so that it takes some finite collection</a> instead. That's not what this article is about, though. </p> <p> Assuming that you've already dealt with the infinite-sequence issue, how do you address the other precondition? </p> <h3 id="03c13848cbd54058a3dfed204bc85878"> Error-handling <a href="#03c13848cbd54058a3dfed204bc85878">#</a> </h3> <p> A typical object-oriented move is to introduce a Guard Clause: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.<span style="font-weight:bold;color:#74531f;">Any</span>()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentOutOfRangeException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">timeSpans</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Can&#39;t&nbsp;calculate&nbsp;the&nbsp;average&nbsp;of&nbsp;an&nbsp;empty&nbsp;collection.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.Zero; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+=</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Count; }</pre> </p> <p> You could do the same in F#: </p> <p> <pre>let&nbsp;average&nbsp;(timeSpans&nbsp;:&nbsp;TimeSpan&nbsp;seq)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Seq.isEmpty&nbsp;timeSpans&nbsp;then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof&nbsp;timeSpans, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Can&#39;t&nbsp;calculate&nbsp;the&nbsp;average&nbsp;of&nbsp;an&nbsp;empty&nbsp;collection.&quot;)) &nbsp;&nbsp;&nbsp;&nbsp;timeSpans &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.averageBy&nbsp;(_.Ticks&nbsp;&gt;&gt;&nbsp;double) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;int64 &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;TimeSpan.FromTicks</pre> </p> <p> You <em>could</em> also replicate such behaviour in Haskell, but it'd be highly unidiomatic. Instead, I'd rather discuss one <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> solution in Haskell, and then back-port it. </p> <p> While you can throw exceptions in Haskell, you typically handle <a href="/2024/01/29/error-categories-and-category-errors">predictable errors</a> with a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>. Here's a version of the Haskell function equivalent to the above C# code: </p> <p> <pre><span style="color:#2b91af;">average</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Foldable</span>&nbsp;t,&nbsp;<span style="color:blue;">Fractional</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;t&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;a average&nbsp;xs&nbsp;= &nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:blue;">null</span>&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;Left&nbsp;<span style="color:#a31515;">&quot;Can&#39;t&nbsp;calculate&nbsp;the&nbsp;average&nbsp;of&nbsp;an&nbsp;empty&nbsp;collection.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;Right&nbsp;$&nbsp;<span style="color:blue;">sum</span>&nbsp;xs&nbsp;/&nbsp;<span style="color:blue;">fromIntegral</span>&nbsp;(<span style="color:blue;">length</span>&nbsp;xs) </pre> </p> <p> For the readers that don't know the Haskell <a href="https://hackage.haskell.org/package/base">base</a> library by heart, <a href="https://hackage.haskell.org/package/base/docs/Data-List.html#v:null">null</a> is a predicate that checks whether or not a collection is empty. It has nothing to do with <a href="https://en.wikipedia.org/wiki/Null_pointer">null pointers</a>. </p> <p> This variation returns an <a href="/2018/06/11/church-encoded-either">Either</a> value. In practice you shouldn't just return a <code>String</code> as the error value, but rather a strongly-typed value that other code can deal with in a robust manner. </p> <p> On the other hand, in this particular example, there's really only one error condition that the function is able to detect, so you often see a variation where instead of a single error message, such a function just doesn't return anything: </p> <p> <pre><span style="color:#2b91af;">average</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Foldable</span>&nbsp;t,&nbsp;<span style="color:blue;">Fractional</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;t&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a average&nbsp;xs&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:blue;">null</span>&nbsp;xs&nbsp;<span style="color:blue;">then</span>&nbsp;Nothing&nbsp;<span style="color:blue;">else</span>&nbsp;Just&nbsp;$&nbsp;<span style="color:blue;">sum</span>&nbsp;xs&nbsp;/&nbsp;<span style="color:blue;">fromIntegral</span>&nbsp;(<span style="color:blue;">length</span>&nbsp;xs) </pre> </p> <p> This iteration of the function returns a <a href="/2018/03/26/the-maybe-functor">Maybe</a> value, indicating that a return value may or may not be present. </p> <h3 id="ccc6a2a1804740a8942feee3b637db90"> Liberal domain <a href="#ccc6a2a1804740a8942feee3b637db90">#</a> </h3> <p> We can back-port this design to F#, where I'd also consider it idiomatic: </p> <p> <pre>let&nbsp;average&nbsp;(timeSpans&nbsp;:&nbsp;IReadOnlyCollection&lt;TimeSpan&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;timeSpans.Count&nbsp;=&nbsp;0&nbsp;then&nbsp;None&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeSpans &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.averageBy&nbsp;(_.Ticks&nbsp;&gt;&gt;&nbsp;double) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;int64 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;TimeSpan.FromTicks &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Some</pre> </p> <p> This version returns a <code>TimeSpan option</code> rather than just a <code>TimeSpan</code>. While this may seem to put the burden of error-handling on the caller, nothing has really changed. The fundamental situation is the same. Now the function is just being more <a href="https://peps.python.org/pep-0020/">explicit</a> (more honest, you could say) about the pre- and postconditions. The type system also now insists that you deal with the possibility of error, rather than just hoping that the problem doesn't occur. </p> <p> In C# you can <a href="/2024/01/29/error-categories-and-category-errors">expand the codomain by returning a nullable TimeSpan value</a>, but such an option may not always be available at the language level. Keep in mind that the <code>Average</code> method is just an example standing in for something that may be more complicated. If the original return type is a <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/reference-types">reference type</a> rather than a <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/value-types">value type</a>, only recent versions of C# allows statically-checked <a href="https://learn.microsoft.com/dotnet/csharp/nullable-references">nullable reference types</a>. What if you're working in an older version of C#, or another language that doesn't have that feature? </p> <p> In that case, you may need to introduce an explicit <a href="/2018/03/26/the-maybe-functor">Maybe</a> class and return that: </p> <p> <pre>public&nbsp;static&nbsp;Maybe&lt;TimeSpan&gt;&nbsp;Average(this&nbsp;IReadOnlyCollection&lt;TimeSpan&gt;&nbsp;timeSpans) { &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(timeSpans.Count&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Maybe&lt;TimeSpan&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;sum&nbsp;=&nbsp;TimeSpan.Zero; &nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;ts&nbsp;in&nbsp;timeSpans) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;+=&nbsp;ts; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Maybe&lt;TimeSpan&gt;(sum&nbsp;/&nbsp;timeSpans.Count); }</pre> </p> <p> Two things are going on here; one is obvious while the other is more subtle. Clearly, all of these alternatives change the static type of the function in order to make the pre- and postconditions more explicit. So far, they've all been loosening the <a href="https://en.wikipedia.org/wiki/Codomain">codomain</a> (the return <a href="/2021/11/15/types-as-sets">type</a>). This suggests a connection with <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>: <em>be conservative in what you send, be liberal in what you accept</em>. These variations are all liberal in what they accept, but it seems that the API design pays the price by also having to widen the set of possible return values. In other words, such designs aren't conservative in what they send. </p> <p> Do we have other options? </p> <h3 id="4fb2cc5775c44f80965cacbc37825f27"> Conservative codomain <a href="#4fb2cc5775c44f80965cacbc37825f27">#</a> </h3> <p> Is it possible to instead design the API in such a way that it's conservative in what it returns? Ideally, we'd like it to guarantee that it returns a number. This is possible by making the preconditions even more explicit. I've also <a href="/2020/02/03/non-exceptional-averages">covered that alternative already</a>, so I'm just going to repeat the C# code here without further comments: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Head; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Tail) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+=</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Count; }</pre> </p> <p> This variation promotes another precondition to a type. The precondition that the input collection mustn't be empty can be explicitly modelled with a type. This enables us to be conservative about the codomain. The method now guarantees that it will return a value. </p> <p> This idea is also easily ported to F#: </p> <p> <pre>type&nbsp;NonEmpty&lt;&#39;a&gt;&nbsp;=&nbsp;{&nbsp;Head&nbsp;:&nbsp;&#39;a;&nbsp;Tail&nbsp;:&nbsp;IReadOnlyCollection&lt;&#39;a&gt;&nbsp;} let&nbsp;average&nbsp;(timeSpans&nbsp;:&nbsp;NonEmpty&lt;TimeSpan&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;timeSpans.Head&nbsp;]&nbsp;@&nbsp;List.ofSeq&nbsp;timeSpans.Tail &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.averageBy&nbsp;(_.Ticks&nbsp;&gt;&gt;&nbsp;double) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;int64 &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;TimeSpan.FromTicks</pre> </p> <p> The <code>average</code> function now takes a <code>NonEmpty</code> collection as input, and always returns a proper <code>TimeSpan</code> value. </p> <p> Haskell already comes with a built-in <a href="https://hackage.haskell.org/package/base/docs/Data-List-NonEmpty.html">NonEmpty</a> collection type, and while it oddly doesn't come with an <code>average</code> function, it's easy enough to write: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.List.NonEmpty&nbsp;<span style="color:blue;">as</span>&nbsp;NE <span style="color:#2b91af;">average</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Fractional</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">NE</span>.<span style="color:blue;">NonEmpty</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a average&nbsp;xs&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;xs&nbsp;/&nbsp;<span style="color:blue;">fromIntegral</span>&nbsp;(NE.<span style="color:blue;">length</span>&nbsp;xs) </pre> </p> <p> You can find a recent example of using a variation of that function <a href="/2024/04/08/extracting-curve-coordinates-from-a-bitmap">here</a>. </p> <h3 id="6f42a53e7c5f4ddb994e85c9d15ec37a"> Choosing between the two alternatives <a href="#6f42a53e7c5f4ddb994e85c9d15ec37a">#</a> </h3> <p> While Postel's law recommends having liberal domains and conservative codomains, in the case of the <em>average</em> API, we can't have both. If we design the API with a liberal input type, the output type has to be liberal as well. If we design with a restrictive input type, the output can be guaranteed. In my experience, you'll often find yourself in such a conundrum. The <em>average</em> API examined in this article is just an example, while the problem occurs often. </p> <p> Given such a choice, what should you choose? Is it even possible to give general guidance on this sort of problem? </p> <p> For decades, I considered such a choice a toss-up. After all, these solutions seem to be equivalent. Perhaps even isomorphic? </p> <p> When I recently began to explore this isomorphism more closely, it dawned on me that there's a small asymmetry in the isomorphism that favours the <em>conservative codomain</em> option. </p> <h3 id="976ac8645de44d51a5796be7481b1c12"> Isomorphism <a href="#976ac8645de44d51a5796be7481b1c12">#</a> </h3> <p> An <a href="https://en.wikipedia.org/wiki/Isomorphism">isomorphism</a> is a two-way translation between two representations. You can go back and forth between the two alternatives without loss of information. </p> <p> Is this possible with the two alternatives outlined above? For example, if you have the conservative version, can create the liberal alternative? Yes, you can: </p> <p> <pre><span style="color:#2b91af;">average&#39;</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Fractional</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a average&#39;&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;average&nbsp;.&nbsp;NE.nonEmpty</pre> </p> <p> Not surprisingly, this is trivial in Haskell. If you have the conservative version, you can just map it over a more liberal input. </p> <p> In F# it looks like this: </p> <p> <pre>module&nbsp;NonEmpty&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;tryOfSeq&nbsp;xs&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Seq.isEmpty&nbsp;xs&nbsp;then&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;Some&nbsp;{&nbsp;Head&nbsp;=&nbsp;Seq.head&nbsp;xs;&nbsp;Tail&nbsp;=&nbsp;Seq.tail&nbsp;xs&nbsp;|&gt;&nbsp;List.ofSeq&nbsp;} let&nbsp;average&#39;&nbsp;(timeSpans&nbsp;:&nbsp;IReadOnlyCollection&lt;TimeSpan&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;NonEmpty.tryOfSeq&nbsp;timeSpans&nbsp;|&gt;&nbsp;Option.map&nbsp;average</pre> </p> <p> In C# we can create a liberal overload that calls the conservative method: </p> <p> <pre>public&nbsp;static&nbsp;TimeSpan?&nbsp;Average(this&nbsp;IReadOnlyCollection&lt;TimeSpan&gt;&nbsp;timeSpans) { &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(timeSpans.Count&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;arr&nbsp;=&nbsp;timeSpans.ToArray(); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;NotEmptyCollection&lt;TimeSpan&gt;(arr[0],&nbsp;arr[1..]).Average(); }</pre> </p> <p> Here I just used a Guard Clause and explicit construction of the <code>NotEmptyCollection</code>. I could also have added a <code>NotEmptyCollection.TryCreate</code> method, like in the F# and Haskell examples, but I chose the above slightly more imperative style in order to demonstrate that my point isn't tightly coupled to the concept of <a href="/2018/03/22/functors">functors</a>, mapping, and other Functional Programming trappings. </p> <p> These examples highlight how you can trivially make a conservative API look like a liberal API. Is it possible to go the other way? Can you make a liberal API look like a conservative API? </p> <p> Yes and no. </p> <p> Consider the liberal Haskell version of <code>average</code>, shown above; that's the one that returns <code>Maybe a</code>. Can you make a conservative function based on that? </p> <p> <pre><span style="color:#2b91af;">average&#39;</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Fractional</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">NE</span>.<span style="color:blue;">NonEmpty</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a average&#39;&nbsp;xs&nbsp;=&nbsp;fromJust&nbsp;$&nbsp;average&nbsp;xs</pre> </p> <p> Yes, this is possible, but only by resorting to the <a href="https://wiki.haskell.org/Partial_functions">partial function</a> <a href="https://hackage.haskell.org/package/base/docs/Data-Maybe.html#v:fromJust">fromJust</a>. I'll explain why that is a problem once we've covered examples in the two other languages, such as F#: </p> <p> <pre>let&nbsp;average&#39;&nbsp;(timeSpans&nbsp;:&nbsp;NonEmpty&lt;TimeSpan&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;timeSpans.Head&nbsp;]&nbsp;@&nbsp;List.ofSeq&nbsp;timeSpans.Tail&nbsp;|&gt;&nbsp;average&nbsp;|&gt;&nbsp;Option.get</pre> </p> <p> In this variation, <code>average</code> is the liberal version shown above; the one that returns a <code>TimeSpan option</code>. In order to make a conservative version, the <code>average'</code> function can call the liberal <code>average</code> function, but has to resort to the partial function <code>Option.get</code>. </p> <p> The same issue repeats a third time in C#: </p> <p> <pre>public&nbsp;static&nbsp;TimeSpan&nbsp;Average(this&nbsp;NotEmptyCollection&lt;TimeSpan&gt;&nbsp;timeSpans) { &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;timeSpans.ToList().Average().Value; }</pre> </p> <p> This time, the partial function is the unsafe <a href="https://learn.microsoft.com/dotnet/api/system.nullable-1.value">Value</a> property, which throws an <code>InvalidOperationException</code> if there's no value. </p> <p> This even violates Microsoft's own design guidelines: </p> <blockquote> <p> "AVOID throwing exceptions from property getters." </p> <footer><cite><a href="https://learn.microsoft.com/dotnet/standard/design-guidelines/property">Krzystof Cwalina and Brad Abrams</a></cite></footer> </blockquote> <p> I've cited Cwalina and Abrams as the authors, since this rule can be found in my 2006 edition of <a href="/ref/fdg">Framework Design Guidelines</a>. This isn't a new insight. </p> <p> While the two alternatives are 'isomorphic enough' that we can translate both ways, the translations are asymmetric in the sense that one is safe, while the other has to resort to an inherently unsafe operation to make it work. </p> <h3 id="e10b4b0269b74efa9d89275644c88d8e"> Encapsulation <a href="#e10b4b0269b74efa9d89275644c88d8e">#</a> </h3> <p> I've called the operations <code>fromJust</code>, <code>Option.get</code>, and <code>Value</code> <em>partial</em>, and only just now used the word <em>unsafe</em>. You may protest that neither of the three examples are unsafe in practice, since we know that the input is never empty. Thus, we know that the liberal function will always return a value, and therefore it's safe to call a partial function, even though these operations are unsafe in the general case. </p> <p> While that's true, consider how the burden shifts. When you want to promote a conservative variant to a liberal variant, you can rely on all the operations being total. On the other hand, if you want to make a liberal variant look conservative, the onus is on you. None of the three type systems on display here can perform that analysis for you. </p> <p> This may not be so bad when the example is as simple as taking the average of a collection of numbers, but does it scale? What if the operation you're invoking is much more complicated? Can you still be sure that you safely invoke a partial function on the return value? </p> <p> As <a href="/ctfiyh">Code That Fits in Your Head</a> argues, procedures quickly become so complicated that they no longer fit in your head. If you don't have well-described and patrolled contracts, you don't know what the postconditions are. You can't trust the return values from method calls, or even the state of the objects you passed as arguments. This tend to lead to <a href="/2013/07/08/defensive-coding">defensive coding</a>, where you write code that checks the state of everything all too often. </p> <p> The remedy is, as always, good old <a href="/encapsulation-and-solid">encapsulation</a>. In this case, check the preconditions at the beginning, and capture the result of that check in an object or type that is guaranteed to be always valid. This goes beyond <a href="https://blog.janestreet.com/effective-ml-video/">making illegal states unrepresentable</a> because it also works with <a href="https://www.hillelwayne.com/post/constructive/">predicative</a> types. Once you're past the Guard Clauses, you don't have to check the preconditions <em>again</em>. </p> <p> This kind of thinking illustrates why you need a multidimensional view on API design. As useful as Postel's law sometimes is, it doesn't address all problems. In fact, it turned out to be unhelpful in this context, while another perspective proves more fruitful. Encapsulation is the art and craft of designing APIs in such a way that they suggest or even compels correct interactions. The more I think of this, the more it strikes me that a <em>ranking</em> is implied: Preconditions are more important than postconditions, because if the preconditions are unfulfilled, you can't trust the postconditions, either. </p> <h3 id="35bb4abc87b8402da82c82c5baa71235"> Mapping <a href="#35bb4abc87b8402da82c82c5baa71235">#</a> </h3> <p> What's going on here? One perspective is to view <a href="/2021/11/15/types-as-sets">types as sets</a>. In the <em>average</em> example, the function maps from one set to another: </p> <p> <img src="/content/binary/mapping-from-collections-to-reals.png" alt="Mapping from the set of collections to the set of real numbers."> </p> <p> Which sets are they? We can think of the <em>average</em> function as a mapping from the set of non-empty collections of numbers to the set of <a href="https://en.wikipedia.org/wiki/Real_number">real numbers</a>. In programming, we can't represent real numbers, so instead, the left set is going to be the set of all the non-empty collections the computer or the language can represent and hold in (virtual) memory, and the right-hand set is the set of all the possible numbers of whichever type you'd like (32-bit signed integers, <a href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format">64-bit floating-point numbers</a>, 8-bit unsigned integers, etc.). </p> <p> In reality, the left-hand set is much larger than the set to the right. </p> <p> Drawing all those arrows quickly becomes awkward , so instead, we may <a href="/2021/11/22/functions-as-pipes">draw each mapping as a pipe</a>. Such a pipe also corresponds to a function. Here's an intermediate step in such a representation: </p> <p> <img src="/content/binary/mapping-from-collections-to-reals-transparent-pipe.png" alt="Mapping from one set to the other, drawn inside a transparent pipe."> </p> <p> One common element is, however, missing from the left set. Which one? </p> <h3 id="8920c8df3b9f4f978a5c560d5c9cdcb4"> Pipes <a href="#8920c8df3b9f4f978a5c560d5c9cdcb4">#</a> </h3> <p> The above mapping corresponds to the conservative variation of the function. It's a total function that maps all values in the domain to a value in the codomain. It accomplishes this trick by explicitly constraining the domain to only those elements on which it's defined. Due to the preconditions, that excludes the empty collection, which is therefore absent from the left set. </p> <p> What if we also want to allow the empty collection to be a valid input? </p> <p> Unless we find ourselves in some special context where it makes sense to define a 'default average value', we can't map an empty collection to any meaningful number. Rather, we'll have to map it to some special value, such as <code>Nothing</code>, <code>None</code>, or <code>null</code>: </p> <p> <img src="/content/binary/mapping-with-none-channel-transparent-pipe.png" alt="Mapping the empty collection to null in a pipe separate, but on top of, the proper function pipe."> </p> <p> This extra pipe is free, because it's supplied by the <a href="/2018/03/26/the-maybe-functor">Maybe functor</a>'s mapping (<code>Select</code>, <code>map</code>, <code>fmap</code>). </p> <p> What happens if we need to go the other way? If the function is the liberal variant that also maps the empty collection to a special element that indicates a missing value? </p> <p> <img src="/content/binary/mapping-from-all-collections-to-reals-transparent-pipe.png" alt="Mapping all collections, including the empty collection, to the set of real numbers."> </p> <p> In this case, it's much harder to disentangle the mappings. If you imagine that a liquid flows through the pipes, we can try to be careful and avoid 'filling up' the pipe. </p> <p> <img src="/content/binary/pipe-partially-filled-with-liquid.png" alt="Pipe partially filled with liquid."> </p> <p> The liquid represents the data that we <em>do</em> want to transmit through the pipe. As this illustration suggests, we now have to be careful that nothing goes wrong. In order to catch just the right outputs on the right side, you need to know how high the liquid may go, and attach a an 'flat-top' pipe to it: </p> <p> <img src="/content/binary/pipe-composed-with-open-top-pipe.png" alt="Pipe composed with open-top pipe."> </p> <p> As this illustration tries to get across, this kind of composition is awkward and error-prone. What's worse is that you need to know how high the liquid is going to get on the right side. This depends on what actually goes on inside the pipe, and what kind of input goes into the left-hand side. </p> <p> This is a metaphor. The longer the pipe is, the more difficult it gets to keep track of that knowledge. The stubby little pipe in these illustrations may correspond to the <em>average</em> function, which is an operation that easily fits in our heads. It's not too hard to keep track of the preconditions, and how they map to postconditions. </p> <p> Thus, turning such a small liberal function into a conservative function is possible, but already awkward. If the operation is complicated, you can no longer keep track of all the details of how the inputs relate to the outputs. </p> <h3 id="18b682600e5a4d1baf542b0cd1dcda7f"> Additive extensibility <a href="#18b682600e5a4d1baf542b0cd1dcda7f">#</a> </h3> <p> This really shouldn't surprise us. Most programming languages come with all sorts of facilities that enable <em>extensibility</em>: The ability to <em>add</em> more functionality, more behaviour, more capabilities, to existing building blocks. Conversely, few languages come with <em>removability</em> facilities. You can't, commonly, declare that an object is an instance of a class, <em>except</em> one method, or that a function is just like another function, <em>except</em> that it doesn't accept a particular subset of input. </p> <p> This explains why we can safely make a conservative function liberal, but why it's difficult to make a liberal function conservative. This is because making a conservative function liberal <em>adds</em> functionality, while making a liberal function conservative attempts to remove functionality. </p> <h3 id="6545430a4e1f47a38e121aee1a342b40"> Conjecture <a href="#6545430a4e1f47a38e121aee1a342b40">#</a> </h3> <p> All this leads me to the following conjecture: When faced with a choice between two versions of an API, where one has a liberal domain, and the other a conservative codomain, choose the design with the conservative codomain. </p> <p> If you need the liberal version, you can create it from the conservative operation. The converse need not be true. </p> <h3 id="312f8f4df0c44390a43f4f0f92d2c9d6"> Conclusion <a href="#312f8f4df0c44390a43f4f0f92d2c9d6">#</a> </h3> <p> Postel's law encourages us to be liberal with what we accept, but conservative with what we return. This is a good design heuristic, but sometimes you're faced with mutually exclusive alternatives. If you're liberal with what you accept, you'll also need to be too loose with what you return, because there are input values that you can't handle. On the other hand, sometimes the only way to be conservative with the output is to also be restrictive when it comes to input. </p> <p> Given two such alternatives, which one should you choose? </p> <p> This article conjectures that you should choose the conservative alternative. This isn't a political statement, but simply a result of the conservative design being the smaller building block. From a small building block, you can compose something bigger, whereas from a bigger unit, you can't easily extract something smaller that's still robust and useful. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Service compatibility is determined based on policy https://blog.ploeh.dk/2024/04/29/service-compatibility-is-determined-based-on-policy 2024-04-29T11:12:00+00:00 Mark Seemann <div id="post"> <p> <em>A reading of the fourth Don Box tenet, with some commentary.</em> </p> <p> This article is part of a series titled <a href="/2024/03/04/the-four-tenets-of-soa-revisited">The four tenets of SOA revisited</a>. In each of these articles, I'll pull one of <a href="https://en.wikipedia.org/wiki/Don_Box">Don Box</a>'s <em>four tenets of service-oriented architecture</em> (SOA) out of the <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/january/a-guide-to-developing-and-running-connected-systems-with-indigo">original MSDN Magazine article</a> and add some of my own commentary. If you're curious why I do that, I cover that in the introductory article. </p> <p> In this article, I'll go over the fourth tenet, quoting from the MSDN Magazine article unless otherwise indicated. </p> <h3 id="57382e74449c40409a7d73d91bc5fd14"> Service compatibility is determined based on policy <a href="#57382e74449c40409a7d73d91bc5fd14">#</a> </h3> <p> The fourth tenet is the forgotten one. I could rarely remember exactly what it included, but it does give me an opportunity to bring up a few points about compatibility. The articles said: </p> <blockquote> <p> Object-oriented designs often confuse structural compatibility with semantic compatibility. Service-orientation deals with these two axes separately. Structural compatibility is based on contract and schema and can be validated (if not enforced) by machine-based techniques (such as packet-sniffing, validating firewalls). Semantic compatibility is based on explicit statements of capabilities and requirements in the form of policy. </p> <p> Every service advertises its capabilities and requirements in the form of a machine-readable policy expression. Policy expressions indicate which conditions and guarantees (called assertions) must hold true to enable the normal operation of the service. Policy assertions are identified by a stable and globally unique name whose meaning is consistent in time and space no matter which service the assertion is applied to. Policy assertions may also have parameters that qualify the exact interpretation of the assertion. Individual policy assertions are opaque to the system at large, which enables implementations to apply simple propositional logic to determine service compatibility. </p> </blockquote> <p> As you can tell, this description is the shortest of the four. This is also the point where I begin to suspect that my reading of <a href="/2024/04/15/services-share-schema-and-contract-not-class">the third tenet</a> may deviate from what Don Box originally had in mind. </p> <p> This tenet is also the most baffling to me. As I understand it, the motivation behind the four tenets was to describe assumptions about the kind of systems that people would develop with <a href="https://en.wikipedia.org/wiki/Windows_Communication_Foundation">Windows Communication Foundation</a> (WCF), or <a href="https://en.wikipedia.org/wiki/SOAP">SOAP</a> in general. </p> <p> While I worked with WCF for a decade, the above description doesn't ring a bell. Reading it now, the description of <em>policy</em> sounds more like a system such as <a href="https://clojure.org/about/spec">clojure.spec</a>, although that's not something I know much about either. I don't recall WCF ever having a machine-readable policy subsystem, and if it had, I never encountered it. </p> <p> It does seem, however, as though what I interpret as <em>contract</em>, Don Box called <em>policy</em>. </p> <p> Despite my confusion, the word <em>compatibility</em> is worth discussing, regardless of whether that was what Don Box meant. A well-designed service is one where you've explicitly considered forwards and backwards compatibility. </p> <h3 id="77bf7878d5304ba08f686cbfbc6cb941"> Versioning <a href="#77bf7878d5304ba08f686cbfbc6cb941">#</a> </h3> <p> Planning for forwards and backwards compatibility does <em>not</em> imply that you're expected to be able to predict the future. It's fine if you have so much experience developing and maintaining online systems that you may have enough foresight to plan for certain likely changes that you may have to make in the future, but that's not what I have in mind. </p> <p> Rather, what you <em>should</em> do is to have a system that enables you to detect breaking changes before you deploy them. Furthermore you should have a strategy for how to deal with the perceived necessity to introduce breaking changes. </p> <p> The most effective strategy that I know of is to employ explicit versioning, particularly <em>message versioning</em>. You <em>can</em> version an entire service as one indivisible block, but I often find it more useful to version at the message level. If you're designing a <a href="https://en.wikipedia.org/wiki/REST">REST</a> API, for example, you can <a href="/2015/06/22/rest-implies-content-negotiation">take advantage of Content Negotiation</a>. </p> <p> If you like, you can use <a href="https://semver.org/">Semantic Versioning</a> as a versioning scheme, but for services, the thing that mostly matters is the major version. Thus, you may simply label your messages with the version numbers <em>1</em>, <em>2</em>, etc. </p> <p> If you already have a published service without explicit message version information, then you can still retrofit versioning afterwards. <a href="/2023/12/04/serialization-with-and-without-reflection">Imagine that your existing data looks like this</a>: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;singleTable&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;16, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;minimalReservation&quot;</span>:&nbsp;10 &nbsp;&nbsp;} }</pre> </p> <p> This <a href="https://json.org/">JSON</a> document has no explicit version information, but you can interpret that as implying that the document has the 'default' version, which is always <em>1:</em> </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;singleTable&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;version&quot;</span>:&nbsp;1, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;16, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;minimalReservation&quot;</span>:&nbsp;10 &nbsp;&nbsp;} }</pre> </p> <p> If you later realize that you need to make a breaking change, you can do that by increasing the (major) version: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;singleTable&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;version&quot;</span>:&nbsp;2, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;12, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;16, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;minimalReservation&quot;</span>:&nbsp;10 &nbsp;&nbsp;} }</pre> </p> <p> Recipients can now look for the <code>version</code> property to learn how to interpret the rest of the message, and failing to find it, infer that this is version <em>1</em>. </p> <p> As Don Box wrote, in a service-oriented system, you can't just update all systems in a single coordinated release. Therefore, you must never break compatibility. Versioning enables you to move forward in a way that does break with the past, but without breaking existing clients. </p> <p> Ultimately, you <a href="/2020/06/01/retiring-old-service-versions">may attempt to retire old service versions</a>, but be ready to keep them around for a long time. </p> <p> For more of my thoughts about backwards compatibility, see <a href="/2021/12/13/backwards-compatibility-as-a-profunctor">Backwards compatibility as a profunctor</a>. </p> <h3 id="ad9cec4f54c243d08fc71d38ff13ac17"> Conclusion <a href="#ad9cec4f54c243d08fc71d38ff13ac17">#</a> </h3> <p> The fourth tenet is the most nebulous, and I wonder if it was ever implemented. If it was, I'm not aware of it. Even so, compatibility is an important component of service design, so I took the opportunity to write about that. In most cases, it pays to think explicitly about message versioning. </p> <p> I have the impression that Don Box had something in mind more akin to what I call <em>contract</em>. Whether you call it one thing or another, it stands to reason that you often need to attach extra rules to simple types. The <em>schema</em> may define an input value as a number, but the service does require that this particular number is a natural number. Or that a string is really a <a href="https://en.wikipedia.org/wiki/ISO_8601">proper encoding</a> of a date. Perhaps you call that <em>policy</em>. I call it <em>contract</em>. In any case, clearly communicating such expectations is important for systems to be compatible. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fitting a polynomial to a set of points https://blog.ploeh.dk/2024/04/22/fitting-a-polynomial-to-a-set-of-points 2024-04-22T05:35:00+00:00 Mark Seemann <div id="post"> <p> <em>The story of a fiasco.</em> </p> <p> This is the second in a small series of articles titled <a href="/2024/04/01/trying-to-fit-the-hype-cycle">Trying to fit the hype cycle</a>. In the introduction, I've described the exercise I had in mind: Determining a formula, or at least a <a href="https://en.wikipedia.org/wiki/Piecewise">piecewise</a> <a href="https://en.wikipedia.org/wiki/Function_(mathematics)">function</a>, for the <a href="https://en.wikipedia.org/wiki/Gartner_hype_cycle">Gartner hype cycle</a>. This, to be clear, is an entirely frivolous exercise with little practical application. </p> <p> In the previous article, I <a href="/2024/04/08/extracting-curve-coordinates-from-a-bitmap">extracted a set of <em>(x, y)</em> coordinates from a bitmap</a>. In this article, I'll showcase my failed attempt at fitting the data to a <a href="https://en.wikipedia.org/wiki/Polynomial">polynomial</a>. </p> <h3 id="36f71204d90b44a8b39a7d8103f46cca"> Failure <a href="#36f71204d90b44a8b39a7d8103f46cca">#</a> </h3> <p> I've already revealed that I failed to accomplish what I set out to do. Why should you read on, then? </p> <p> You don't have to, and I can't predict the many reasons my readers have for occasionally swinging by. Therefore, I can't tell you why <em>you</em> should keep reading, but I <em>can</em> tell you why I'm writing this article. </p> <p> This blog is a mix of articles that I write because readers ask me interesting questions, and partly, it's my personal research-and-development log. In that mode, I write about things that I've learned, and I write in order to learn. One can learn from failure as well as from success. </p> <p> I'm not <em>that</em> connected to 'the' research community (if such a thing exists), but I'm getting the sense that there's a general tendency in academia that researchers rarely publish their negative results. This could be a problem, because this means that the rest of us never learn about the <em>thousands of ways that don't work</em>. </p> <p> Additionally, in 'the' programming community, we also tend to boast our victories and hide our failures. More than one podcast (sorry about the <a href="https://en.wikipedia.org/wiki/Weasel_word">weasel words</a>, but I don't remember which ones) have discussed how this gives young programmers the wrong impression of what programming is like. It is, indeed, a process of much trial and error, but usually, we only publish our polished, final result. </p> <p> Well, I did manage to produce code to fit a polynomial to the Gartner hype cycle, but I never managed to get a <em>good</em> fit. </p> <h3 id="34ad323fc07f48709fb86c4045bd5892"> The big picture <a href="#34ad323fc07f48709fb86c4045bd5892">#</a> </h3> <p> I realize that I have a habit of burying the lede when I write technical articles. I don't know if I've picked up that tendency from <a href="https://fsharp.org/">F#</a>, which does demand that you define a value or function before you can use it. This, by the way, <a href="/2015/04/15/c-will-eventually-get-all-f-features-right">is a good feature</a>. </p> <p> Here, I'll try to do it the other way around, and start with the big picture: </p> <p> <pre>data&nbsp;=&nbsp;numpy.loadtxt(<span style="color:#a31515;">&#39;coords.txt&#39;</span>,&nbsp;delimiter=<span style="color:#a31515;">&#39;,&#39;</span>) x&nbsp;=&nbsp;data[:,&nbsp;0] t&nbsp;=&nbsp;data[:,&nbsp;1] w&nbsp;=&nbsp;fit_polynomial(x,&nbsp;t,&nbsp;9) plot_fit(x,&nbsp;t,&nbsp;w)</pre> </p> <p> This, by the way, is a <a href="https://www.python.org/">Python</a> script, and it opens with these imports: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;numpy <span style="color:blue;">import</span>&nbsp;matplotlib.pyplot&nbsp;<span style="color:blue;">as</span>&nbsp;plt</pre> </p> <p> The first line of code reads the <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> file into the <code>data</code> variable. The first column in that file contains all the <em>x</em> values, and the second column the <em>y</em> values. <a href="/ref/rogers-girolami">The book</a> that I've been following uses <em>t</em> for the data, rather than <em>y</em>. (Now that I think about it, I believe that this may only be because it works from an example in which the data to be fitted are <a href="https://en.wikipedia.org/wiki/100_metres">100 m dash</a> times, denoted <em>t</em>.) </p> <p> Once the script has extracted the data, it calls the <code>fit_polynomial</code> function to produce a set of weights <code>w</code>. The constant <code>9</code> is the degree of polynomial to fit, although I think that I've made an off-by-one error so that the result is only a eighth-degree polynomial. </p> <p> Finally, the code plots the original data together with the polynomial: </p> <p> <img src="/content/binary/hype-8th-degree-poly.png" alt="Gartner hype cycle and a eighth-degree fitted polynomial."> </p> <p> The green dots are the <em>(x, y)</em> coordinates that I extracted in the previous article, while the red curve is the fitted eighth-degree polynomial. Even though we're definitely in the realm of over-fitting, it doesn't reproduce the Gartner hype cycle. </p> <p> I've even arrived at the value <code>9</code> after some trial and error. After all, I wasn't trying to do any real science here, so over-fitting is definitely allowed. Even so, <code>9</code> seems to be the best fit I can achieve. With lover values, like <code>8</code>, below, the curve deviates too much: </p> <p> <img src="/content/binary/hype-7th-degree-poly.png" alt="Gartner hype cycle and a seventh-degree fitted polynomial."> </p> <p> The value <code>10</code> looks much like <code>9</code>, but above that (<code>11</code>), the curve completely disconnects from the data, it seems: </p> <p> <img src="/content/binary/hype-10th-degree-poly.png" alt="Gartner hype cycle and a tenth-degree fitted polynomial."> </p> <p> I'm not sure why it does this, to be honest. I would have thought that the more degrees you added, the more (over-)fitted the curve would be. Apparently, this is not so, or perhaps I made a mistake in my code. </p> <h3 id="183834d3c95544d9a185b5ba84bba9a1"> Calculating the weights <a href="#183834d3c95544d9a185b5ba84bba9a1">#</a> </h3> <p> The <code>fit_polynomial</code> function calculates the polynomial coefficients using a <a href="https://en.wikipedia.org/wiki/Linear_algebra">linear algebra</a> formula that I've found in at least two text books. Numpy makes it easy to invert, transpose, and multiply matrices, so the formula itself is just a one-liner. Here it is in the entire context of the function, though: </p> <p> <pre><span style="color:blue;">def</span>&nbsp;<span style="color:#2b91af;">fit_polynomial</span>(x,&nbsp;t,&nbsp;degree): &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;&quot;&quot; &nbsp;&nbsp;&nbsp;&nbsp;Fits&nbsp;a&nbsp;polynomial&nbsp;to&nbsp;the&nbsp;given&nbsp;data. &nbsp;&nbsp;&nbsp;&nbsp;Parameters &nbsp;&nbsp;&nbsp;&nbsp;---------- &nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;:&nbsp;Array&nbsp;of&nbsp;shape&nbsp;[n_samples] &nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;:&nbsp;Array&nbsp;of&nbsp;shape&nbsp;[n_samples] &nbsp;&nbsp;&nbsp;&nbsp;degree&nbsp;:&nbsp;degree&nbsp;of&nbsp;the&nbsp;polynomial &nbsp;&nbsp;&nbsp;&nbsp;Returns &nbsp;&nbsp;&nbsp;&nbsp;------- &nbsp;&nbsp;&nbsp;&nbsp;w&nbsp;:&nbsp;Array&nbsp;of&nbsp;shape&nbsp;[degree&nbsp;+&nbsp;1] &nbsp;&nbsp;&nbsp;&nbsp;&quot;&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">#&nbsp;This&nbsp;expansion&nbsp;creates&nbsp;a&nbsp;matrix,&nbsp;so&nbsp;we&nbsp;name&nbsp;that&nbsp;with&nbsp;an&nbsp;upper-case&nbsp;letter</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">#&nbsp;rather&nbsp;than&nbsp;a&nbsp;lower-case&nbsp;letter,&nbsp;which&nbsp;is&nbsp;used&nbsp;for&nbsp;vectors.</span> &nbsp;&nbsp;&nbsp;&nbsp;X&nbsp;=&nbsp;expand(x.reshape((<span style="color:blue;">len</span>(x),&nbsp;1)),&nbsp;degree) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;numpy.linalg.inv(X.T&nbsp;@&nbsp;X)&nbsp;@&nbsp;X.T&nbsp;@&nbsp;t</pre> </p> <p> This may look daunting, but is really just two lines of code. The rest is <a href="https://en.wikipedia.org/wiki/Docstring">docstring</a> and a comment. </p> <p> The above-mentioned formula is the last line of code. The one before that expands the input data <code>t</code> from a simple one-dimensional array to a matrix of those values squared, cubed, etc. That's how you use the <a href="https://en.wikipedia.org/wiki/Least_squares">least squares</a> method if you want to fit it to a polynomial of arbitrary degree. </p> <h3 id="782c5cbd64de43878eea4a3ddfcdf755"> Expansion <a href="#782c5cbd64de43878eea4a3ddfcdf755">#</a> </h3> <p> The <code>expand</code> function looks like this: </p> <p> <pre><span style="color:blue;">def</span>&nbsp;<span style="color:#2b91af;">expand</span>(x,&nbsp;degree): &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;&quot;&quot; &nbsp;&nbsp;&nbsp;&nbsp;Expands&nbsp;the&nbsp;given&nbsp;array&nbsp;to&nbsp;polynomial&nbsp;elements&nbsp;of&nbsp;the&nbsp;given&nbsp;degree. &nbsp;&nbsp;&nbsp;&nbsp;Parameters &nbsp;&nbsp;&nbsp;&nbsp;---------- &nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;:&nbsp;Array&nbsp;of&nbsp;shape&nbsp;[n_samples,&nbsp;1] &nbsp;&nbsp;&nbsp;&nbsp;degree&nbsp;:&nbsp;degree&nbsp;of&nbsp;the&nbsp;polynomial &nbsp;&nbsp;&nbsp;&nbsp;Returns &nbsp;&nbsp;&nbsp;&nbsp;------- &nbsp;&nbsp;&nbsp;&nbsp;Xp&nbsp;:&nbsp;Array&nbsp;of&nbsp;shape&nbsp;[n_samples,&nbsp;degree&nbsp;+&nbsp;1] &nbsp;&nbsp;&nbsp;&nbsp;&quot;&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;Xp&nbsp;=&nbsp;numpy.ones((<span style="color:blue;">len</span>(x),&nbsp;1)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">range</span>(1,&nbsp;degree&nbsp;+&nbsp;1): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Xp&nbsp;=&nbsp;numpy.hstack((Xp,&nbsp;numpy.power(x,&nbsp;i))) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Xp</pre> </p> <p> The function begins by creating a column vector of ones, here illustrated with only three rows: </p> <p> <pre>&gt;&gt;&gt; Xp = numpy.ones((3, 1)) &gt;&gt;&gt; Xp array([[1.], [1.], [1.]])</pre> </p> <p> It then proceeds to loop over as many degrees as you've asked it to, each time adding a column to the <code>Xp</code> matrix. Here's an example of doing that up to a power of three, on example input <code>[1,2,3]</code>: </p> <p> <pre>&gt;&gt;&gt; x = numpy.array([1,2,3]).reshape((3, 1)) &gt;&gt;&gt; x array([[1], [2], [3]]) &gt;&gt;&gt; Xp = numpy.hstack((Xp, numpy.power(x, 1))) &gt;&gt;&gt; Xp array([[1., 1.], [1., 2.], [1., 3.]]) &gt;&gt;&gt; Xp = numpy.hstack((Xp, numpy.power(x, 2))) &gt;&gt;&gt; Xp array([[1., 1., 1.], [1., 2., 4.], [1., 3., 9.]]) &gt;&gt;&gt; Xp = numpy.hstack((Xp, numpy.power(x, 3))) &gt;&gt;&gt; Xp array([[ 1., 1., 1., 1.], [ 1., 2., 4., 8.], [ 1., 3., 9., 27.]])</pre> </p> <p> Once it's done looping, the <code>expand</code> function returns the resulting <code>Xp</code> matrix. </p> <h3 id="cfb27c6067d2486c95836dc61484b2a0"> Plotting <a href="#cfb27c6067d2486c95836dc61484b2a0">#</a> </h3> <p> Finally, here's the <code>plot_fit</code> procedure: </p> <p> <pre><span style="color:blue;">def</span>&nbsp;<span style="color:#2b91af;">plot_fit</span>(x,&nbsp;t,&nbsp;w): &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;&quot;&quot; &nbsp;&nbsp;&nbsp;&nbsp;Plots&nbsp;the&nbsp;polynomial&nbsp;with&nbsp;the&nbsp;given&nbsp;weights&nbsp;and&nbsp;the&nbsp;data. &nbsp;&nbsp;&nbsp;&nbsp;Parameters &nbsp;&nbsp;&nbsp;&nbsp;---------- &nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;:&nbsp;Array&nbsp;of&nbsp;shape&nbsp;[n_samples] &nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;:&nbsp;Array&nbsp;of&nbsp;shape&nbsp;[n_samples] &nbsp;&nbsp;&nbsp;&nbsp;w&nbsp;:&nbsp;Array&nbsp;of&nbsp;shape&nbsp;[degree&nbsp;+&nbsp;1] &nbsp;&nbsp;&nbsp;&nbsp;&quot;&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;xs&nbsp;=&nbsp;numpy.linspace(x[0],&nbsp;x[0]+<span style="color:blue;">len</span>(x),&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;ys&nbsp;=&nbsp;numpy.polyval(w[::-1],&nbsp;xs) &nbsp;&nbsp;&nbsp;&nbsp;plt.plot(xs,&nbsp;ys,&nbsp;<span style="color:#a31515;">&#39;r&#39;</span>) &nbsp;&nbsp;&nbsp;&nbsp;plt.scatter(x,&nbsp;t,&nbsp;s=10,&nbsp;c=<span style="color:#a31515;">&#39;g&#39;</span>) &nbsp;&nbsp;&nbsp;&nbsp;plt.show()</pre> </p> <p> This is fairly standard pyplot code, so I don't have much to say about it. </p> <h3 id="3730027db8614b01960cf5379d8add78"> Conclusion <a href="#3730027db8614b01960cf5379d8add78">#</a> </h3> <p> When I started this exercise, I'd hoped that I could get close to the Gartner hype cycle by over-fitting the model to some ridiculous polynomial degree. This turned out not to be the case, for reasons that I don't fully understand. As I increase the degree, the curve begins to deviate from the data. </p> <p> I can't say that I'm a data scientist or a statistician of any skill, so it's possible that my understanding is still too shallow. Perhaps I'll return to this article later and marvel at the ineptitude on display here. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4bef47fad250438a94c2f1de28dc330d"> <div class="comment-author"><a href="https://www.mit.edu/~amu/">Aaron M. Ucko</a> <a href="#4bef47fad250438a94c2f1de28dc330d">#</a></div> <div class="comment-content"> <p> I suspect that increasing the degree wound up backfiring by effectively putting too much weight on the right side, whose flatness clashed with the increasingly steep powers you were trying to mix in. A vertically offset damped sinusoid might make a better starting point for modeling, though identifying its parameters wouldn't be quite as straightforward. One additional wrinkle there is that you want to level fully off after the valley; you could perhaps make that happen by plugging a scaled arctangent or something along those lines into the sinusoid. </p> <p> Incidentally, a neighboring post in my feed reader was about a new release of an open-source data analysis and curve fitting program (QSoas) that might help if you don't want to take such a DIY approach. </p> </div> <div class="comment-date">2024-05-16 02:37 UTC</div> </div> <div class="comment" id="831d9f6360da4cbaa2ab5a08315b532a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#831d9f6360da4cbaa2ab5a08315b532a">#</a></div> <div class="comment-content"> <p> Aaron, thank you for writing. In retrospect, it becomes increasingly clear to me why this doesn't work. This highlights, I think, why it's a good idea to sometimes do stupid exercises like this one. You learn something from it, even when you fail. </p> </div> <div class="comment-date">2024-05-22 6:15 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Services share schema and contract, not class https://blog.ploeh.dk/2024/04/15/services-share-schema-and-contract-not-class 2024-04-15T07:25:00+00:00 Mark Seemann <div id="post"> <p> <em>A reading of the third Don Box tenet, with some commentary.</em> </p> <p> This article is part of a series titled <a href="/2024/03/04/the-four-tenets-of-soa-revisited">The four tenets of SOA revisited</a>. In each of these articles, I'll pull one of <a href="https://en.wikipedia.org/wiki/Don_Box">Don Box</a>'s <em>four tenets of service-oriented architecture</em> (SOA) out of the <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/january/a-guide-to-developing-and-running-connected-systems-with-indigo">original MSDN Magazine article</a> and add some of my own commentary. If you're curious why I do that, I cover that in the introductory article. </p> <p> In this article, I'll go over the third tenet, quoting from the MSDN Magazine article unless otherwise indicated. </p> <h3 id="3a56e1083c454dec90a28f8c7ff44d5f"> Services share schema and contract, not class <a href="#3a56e1083c454dec90a28f8c7ff44d5f">#</a> </h3> <p> Compared to <a href="/2024/03/25/services-are-autonomous">the second tenet</a>, the following description may at first seem more dated. Here's what the article said: </p> <blockquote> <p> Object-oriented programming encourages developers to create new abstractions in the form of classes. Most modern development environments not only make it trivial to define new classes, modern IDEs do a better job guiding you through the development process as the number of classes increases (as features like IntelliSense® provide a more specific list of options for a given scenario). </p> <p> Classes are convenient abstractions as they share both structure and behavior in a single named unit. Service-oriented development has no such construct. Rather, services interact based solely on schemas (for structures) and contracts (for behaviors). Every service advertises a contract that describes the structure of messages it can send and/or receive as well as some degree of ordering constraints over those messages. This strict separation between structure and behavior vastly simplifies deployment, as distributed object concepts such as marshal-by-value require a common execution and security environment which is in direct conflict with the goals of autonomous computing. </p> <p> Services do not deal in types or classes per se; rather, only with machine readable and verifiable descriptions of the legal "ins and outs" the service supports. The emphasis on machine verifiability and validation is important given the inherently distributed nature of how a service-oriented application is developed and deployed. Unlike a traditional class library, a service must be exceedingly careful about validating the input data that arrives in each message. Basing the architecture on machine-validatible schema and contract gives both developers and infrastructure the hints they need to protect the integrity of an individual service as well as the overall application as a whole. </p> <p> Because the contract and schema for a given service are visible over broad ranges of both space and time, service-orientation requires that contracts and schema remain stable over time. In the general case, it is impossible to propagate changes in schema and/or contract to all parties who have ever encountered a service. For that reason, the contract and schema used in service-oriented designs tend to have more flexibility than traditional object-oriented interfaces. It is common for services to use features such as XML element wildcards (like xsd:any) and optional SOAP header blocks to evolve a service in ways that do not break already deployed code. </p> </blockquote> <p> With its explicit discussion of <a href="https://en.wikipedia.org/wiki/XML">XML</a>, <a href="https://en.wikipedia.org/wiki/SOAP">SOAP</a>, and <a href="https://en.wikipedia.org/wiki/XML_schema">XSD</a>, this description may seem more stuck in 2004 than the two first tenets. </p> <p> I'll cover the most obvious consequence first. </p> <h3 id="7ddbc0f966b74c499d0414de8741e454"> At the boundaries... <a href="#7ddbc0f966b74c499d0414de8741e454">#</a> </h3> <p> In the MSDN article, the four tenets guide the design of <a href="https://en.wikipedia.org/wiki/Windows_Communication_Foundation">Windows Communication Foundation</a> (WCF) - a technology that in 2004 was under development, but still not completed. While SOAP already existed as a platform-independent protocol, WCF was a .NET endeavour. Most developers using the Microsoft platform at the time were used to some sort of binary protocol, such as <a href="https://en.wikipedia.org/wiki/Distributed_Component_Object_Model">DCOM</a> or <a href="https://en.wikipedia.org/wiki/.NET_Remoting">.NET Remoting</a>. Thus, it makes sense that Don Box was deliberately explicit that this was <em>not</em> how SOA (or WCF) was supposed to work. </p> <p> In fact, since SOAP is platform-independent, you could write a web service in one language (say, <a href="https://www.java.com/">Java</a>) and consume it with a different language (e.g. <a href="https://en.wikipedia.org/wiki/C%2B%2B">C++</a>). WCF was Microsoft's SOAP technology for .NET. </p> <p> If you squint enough that you don't see the explicit references to XML or SOAP, however, the description still applies. Today, you may exchange data with <a href="https://www.json.org">JSON</a> over <a href="https://en.wikipedia.org/wiki/REST">REST</a>, <a href="https://en.wikipedia.org/wiki/Protocol_Buffers">Protocol Buffers</a> via <a href="https://en.wikipedia.org/wiki/GRPC">gRPC</a>, or something else, but it's still common to have a communications protocol that is independent of specific service implementations. A service may be written in <a href="https://www.python.org/">Python</a>, <a href="https://www.haskell.org/">Haskell</a>, <a href="https://en.wikipedia.org/wiki/C_(programming_language)">C</a>, or any other language that supports the wire format. As this little list suggests, the implementation language doesn't even have to be object-oriented. </p> <p> In fact, </p> <ul> <li><a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">At the Boundaries, Applications are Not Object-Oriented</a></li> <li><a href="/2022/05/02/at-the-boundaries-applications-arent-functional">At the boundaries, applications aren't functional</a></li> <li><a href="/2023/10/16/at-the-boundaries-static-types-are-illusory">At the boundaries, static types are illusory</a></li> </ul> <p> A formal <a href="https://en.wikipedia.org/wiki/Interface_description_language">interface definition language</a> (IDL) may enable you to automate serialization and deserialization, but these are usually constrained to defining the shape of data and operations. Don Box talks about validation, and <a href="/2022/08/22/can-types-replace-validation">types don't replace validation</a> - particularly if you allow <code>xsd:any</code>. That particular remark is quite at odds with the notion that a formal schema definition is necessary, or even desirable. </p> <p> And indeed, today we often see JSON-based REST APIs that are more loosely defined. Even so, the absence of a machine-readable IDL doesn't entail the absence of a schema. As <a href="https://lexi-lambda.github.io/">Alexis King</a> wrote related to the static-versus-dynamic-types debate, <a href="https://lexi-lambda.github.io/blog/2020/01/19/no-dynamic-type-systems-are-not-inherently-more-open/">dynamic type systems are not inherently more open</a>. A similar argument can be made about schema. Regardless of whether or not a formal specification exists, a service always has a de-facto schema. </p> <p> To be honest, though, when I try to interpret what this and the next tenet seem to imply, an IDL may have been all that Don Box had in mind. By <em>schema</em> he may only have meant XSD, and by <em>contract</em>, he may only have meant SOAP. More broadly speaking, this notion of <em>contract</em> may entail nothing more than a list of named operations, and references to schemas that indicate what input each operation takes, and what output it returns. </p> <p> What I have in mind with the rest of this article may be quite an embellishment on that notion. In fact, my usual interpretation of the word <em>contract</em> may be more aligned with what Don Box calls <em>policy</em>. Thus, if you want a very literal reading of the four tenets, what comes next may fit better with the fourth tenet, that service compatibility is determined based on policy. </p> <p> Regardless of whether you think that the following discussion belongs here, or in the next article, I'll assert that it's paramount to designing and developing useful and maintainable web services. </p> <h3 id="99146c84ab1d4d439879970bc17ca728"> Encapsulation <a href="#99146c84ab1d4d439879970bc17ca728">#</a> </h3> <p> If we, once more, ignore the particulars related to SOAP and XML, we may rephrase the notion of schema and contract as follows. Schema describes the shape of data: Is it a number, a string, a tuple, or a combination of these? Is there only one, or several? Is the data composed from smaller such definitions? Does the composition describe the combination of several such definitions, or does it describe mutually exclusive alternatives? </p> <p> Compliant data may be encoded as objects or data structures in memory, or serialized to JSON, XML, <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a>, byte streams, etc. We may choose to call a particular agglomeration of data a <em>message</em>, which we may pass from one system to another. The <a href="/2024/03/11/boundaries-are-explicit">first tenet</a> already used this metaphor. </p> <p> You can't, however, just pass arbitrary valid messages from one system to another. Certain operations allow certain data, and may promise to return other kinds of messages. In additions to the schema, we also need to describe a <em>contract</em>. </p> <p> What's a contract? If you consult <a href="/ref/oosc">Object-Oriented Software Construction</a>, a contract stipulates invariants, pre- and postconditions for various operations. </p> <p> Preconditions state what must be true before an operation can take place. This often puts the responsibility on the caller to ensure that the system is in an appropriate state, and that the message that it intends to pass to the other system is valid according to that state. </p> <p> Postconditions, on the other hand, detail what the caller can expect in return. This includes guarantees about response messages, but may also describe the posterior state of the system. </p> <p> Invariants, finally, outline what is always true about the system. </p> <p> Although such a description of a contract originates from a book about object-oriented design, it's <a href="/2022/10/24/encapsulation-in-functional-programming">useful in other areas, too, such as functional programming</a>. It strikes me that it applies equally well in the context of service-orientation. </p> <p> The combination of contract and well-described message structure is, in other words, <a href="/encapsulation-and-solid">encapsulation</a>. There's nothing wrong with that: It works. If you actually apply it as a design principle, that is. </p> <h3 id="7d42dff045a24a4c89a894f8ed5d5166"> Conclusion <a href="#7d42dff045a24a4c89a894f8ed5d5166">#</a> </h3> <p> The third SOA tenet emphasizes that only data travels over service boundaries. In order to communicate effectively, services must agree on the shape of data, and which operations are legal when. While they exchange data, however, they don't share address space, or even internal representation. </p> <p> One service may be written in <a href="https://fsharp.org/">F#</a> and the client in <a href="https://clojure.org/">Clojure</a>. Even so, it's important that they have a shared understanding of what is possible, and what is not. The more explicit you, as a service owner, can be, the better. </p> <p> <strong>Next:</strong> <a href="/2024/04/29/service-compatibility-is-determined-based-on-policy">Service compatibility is determined based on policy</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Extracting curve coordinates from a bitmap https://blog.ploeh.dk/2024/04/08/extracting-curve-coordinates-from-a-bitmap 2024-04-08T05:32:00+00:00 Mark Seemann <div id="post"> <p> <em>Another example of using Haskell as an ad-hoc scripting language.</em> </p> <p> This article is part of a short series titled <a href="/2024/04/01/trying-to-fit-the-hype-cycle">Trying to fit the hype cycle</a>. In the first article, I outlined what it is that I'm trying to do. In this article, I'll describe how I extract a set of <em>x</em> and <em>y</em> coordinates from this bitmap: </p> <p> <img src="/content/binary/hype-cycle-cleaned.png" alt="Gartner hype cycle."> </p> <p> (Actually, this is scaled-down version of the image. The file I work with is a bit larger.) </p> <p> As I already mentioned in the previous article, these days there are online tools for just about everything. Most likely, there's also an online tool that will take a bitmap like that and return a set of <em>(x, y)</em> coordinates. </p> <p> Since I'm doing this for the programming exercise, I'm not interested in that. Rather, I'd like to write a little <a href="https://www.haskell.org/">Haskell</a> script to do it for me. </p> <h3 id="2ed7ee24ae244f3688dc8a362e149c17"> Module and imports <a href="#2ed7ee24ae244f3688dc8a362e149c17">#</a> </h3> <p> Yes, I wrote Haskell <em>script</em>. As I've described before, with good type inference, <a href="/2024/02/05/statically-and-dynamically-typed-scripts">a statically typed language can be as good for scripting as a dynamic one</a>. Just as might be the case with, say, a <a href="https://www.python.org/">Python</a> script, you'll be iterating, trying things out until finally the script settles into its final form. What I present here is the result of my exercise. You should imagine that I made lots of mistakes underway, tried things that didn't work, commented out code and print statements, imported modules I eventually didn't need, etc. Just like I imagine you'd also do with a script in a dynamically typed language. At least, that's how I write Python, when I'm forced to do that. </p> <p> In other words, the following is far from the result of perfect foresight, but rather the equilibrium into which the script settled. </p> <p> I named the module <code>HypeCoords</code>, because the purpose of it is to extract the <em>(x, y)</em> coordinates from the above <a href="https://en.wikipedia.org/wiki/Gartner_hype_cycle">Gartner hype cycle</a> image. These are the imports it turned out that I ultimately needed: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;HypeCoords&nbsp;<span style="color:blue;">where</span> <span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.List.NonEmpty&nbsp;<span style="color:blue;">as</span>&nbsp;NE <span style="color:blue;">import</span>&nbsp;Data.List.NonEmpty&nbsp;(<span style="color:blue;">NonEmpty</span>((:|))) <span style="color:blue;">import</span>&nbsp;Codec.Picture <span style="color:blue;">import</span>&nbsp;Codec.Picture.Types</pre> </p> <p> The <code>Codec.Picture</code> modules come from the <a href="https://hackage.haskell.org/package/JuicyPixels">JuicyPixels</a> package. This is what enables me to read a <code>.png</code> file and extract the pixels. </p> <h3 id="e0f66bef266249ea8a9546c0edf0b15c"> Black and white <a href="#e0f66bef266249ea8a9546c0edf0b15c">#</a> </h3> <p> If you look at the above bitmap, you may notice that it has some vertical lines in a lighter grey than the curve itself. My first task, then, is to get rid of those. The easiest way to do that is to convert the image to a black-and-white bitmap, with no grey scale. </p> <p> Since this is a one-off exercise, I could easily do that with a bitmap editor, but on the other hand, I thought that this was a good first task to give myself. After all, I didn't know the JuicyPixels library <em>at all</em>, so this was an opportunity to start with a task just a notch simpler than the one that was my actual goal. </p> <p> I thought that the easiest way to convert to a black-and-white image would be to turn all pixels white if they are lighter than some threshold, and black otherwise. </p> <p> A <a href="https://en.wikipedia.org/wiki/PNG">PNG</a> file has more information than I need, so I first converted the image to an 8-bit <a href="https://en.wikipedia.org/wiki/RGB_color_model">RGB</a> bitmap. Even though the above image looks as though it's entirely grey scale, each pixel is actually composed of three colours. In order to compare a pixel with a threshold, I needed a single measure of how light or dark it is. </p> <p> That turned out to be about as simple as it sounds: Just take the average of the three colours. Later, I'd need a function to compute the average for another reason, so I made it a reusable function: </p> <p> <pre><span style="color:#2b91af;">average</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">NE</span>.<span style="color:blue;">NonEmpty</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a average&nbsp;nel&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;nel&nbsp;`div`&nbsp;<span style="color:blue;">fromIntegral</span>&nbsp;(NE.<span style="color:blue;">length</span>&nbsp;nel)</pre> </p> <p> It's a bit odd that the Haskell <a href="https://hackage.haskell.org/package/base">base</a> library doesn't come with such a function (at least to my knowledge), but anyway, this one is specialized to do integer division. Notice that this function computes only <a href="/2020/02/03/non-exceptional-averages">non-exceptional averages</a>, since it requires the input to be a <a href="https://hackage.haskell.org/package/base/docs/Data-List-NonEmpty.html">NonEmpty</a> list. No division-by-zero errors here, please! </p> <p> Once I'd computed a pixel average and compared it to a threshold value, I wanted to replace it with either black or white. In order to make the code more readable I defined two named constants: </p> <p> <pre><span style="color:#2b91af;">black</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">PixelRGB8</span> black&nbsp;=&nbsp;PixelRGB8&nbsp;<span style="color:blue;">minBound</span>&nbsp;<span style="color:blue;">minBound</span>&nbsp;<span style="color:blue;">minBound</span> <span style="color:#2b91af;">white</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">PixelRGB8</span> white&nbsp;=&nbsp;PixelRGB8&nbsp;<span style="color:blue;">maxBound</span>&nbsp;<span style="color:blue;">maxBound</span>&nbsp;<span style="color:blue;">maxBound</span></pre> </p> <p> With that in place, converting to black-and-white is only a few more lines of code: </p> <p> <pre><span style="color:#2b91af;">toBW</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">PixelRGB8</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">PixelRGB8</span> toBW&nbsp;(PixelRGB8&nbsp;r&nbsp;g&nbsp;b)&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;threshold&nbsp;=&nbsp;192&nbsp;::&nbsp;Integer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lum&nbsp;=&nbsp;average&nbsp;(<span style="color:blue;">fromIntegral</span>&nbsp;r&nbsp;:|&nbsp;[<span style="color:blue;">fromIntegral</span>&nbsp;g,&nbsp;<span style="color:blue;">fromIntegral</span>&nbsp;b]) &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">if</span>&nbsp;lum&nbsp;&lt;=&nbsp;threshold&nbsp;<span style="color:blue;">then</span>&nbsp;black&nbsp;<span style="color:blue;">else</span>&nbsp;white</pre> </p> <p> I arrived at the threshold of <code>192</code> after a bit of trial-and-error. That's dark enough that the light vertical lines fall to the <code>white</code> side, while the real curve becomes <code>black</code>. </p> <p> What remained was to glue the parts together to save the black-and-white file: </p> <p> <pre><span style="color:#2b91af;">main</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;readResult&nbsp;&lt;-&nbsp;readImage&nbsp;<span style="color:#a31515;">&quot;hype-cycle-cleaned.png&quot;</span> &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;readResult&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Left&nbsp;msg&nbsp;-&gt;&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;Right&nbsp;img&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;bwImg&nbsp;=&nbsp;pixelMap&nbsp;toBW&nbsp;$&nbsp;convertRGB8&nbsp;img &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writePng&nbsp;<span style="color:#a31515;">&quot;hype-cycle-bw.png&quot;</span>&nbsp;bwImg</pre> </p> <p> The <a href="https://hackage.haskell.org/package/JuicyPixels/docs/Codec-Picture.html#v:convertRGB8">convertRGB8</a> function comes from JuicyPixels. </p> <p> The <code>hype-cycle-bw.png</code> picture unsurprisingly looks like this: </p> <p> <img src="/content/binary/hype-cycle-bw.png" alt="Black-and-white Gartner hype cycle."> </p> <p> Ultimately, I didn't need the black-and-white bitmap <em>file</em>. I just wrote the script to create the file in order to be able to get some insights into what I was doing. Trust me, I made a lot of stupid mistakes along the way, and among other issues had some <a href="https://stackoverflow.com/q/77952762/126014">'fun' with integer overflows</a>. </p> <h3 id="2bd5b7d3dbd44e4a93594030bf5faca5"> Extracting image coordinates <a href="#2bd5b7d3dbd44e4a93594030bf5faca5">#</a> </h3> <p> Now I had a general feel for how to work with the JuicyPixels library. It still required quite a bit of spelunking through the documentation before I found a useful API to extract all the pixels from a bitmap: </p> <p> <pre><span style="color:#2b91af;">pixelCoordinates</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Pixel</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Image</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[((<span style="color:#2b91af;">Int</span>,&nbsp;<span style="color:#2b91af;">Int</span>),&nbsp;a)] pixelCoordinates&nbsp;=&nbsp;pixelFold&nbsp;(\acc&nbsp;x&nbsp;y&nbsp;px&nbsp;-&gt;&nbsp;((x,y),px):acc)&nbsp;<span style="color:blue;">[]</span></pre> </p> <p> While this is, after all, just a one-liner, I'm surprised that something like this doesn't come in the box. It returns a list of tuples, where the first element contains the pixel coordinates (another tuple), and the second element the pixel information (e.g. the RGB value). </p> <h3 id="2b2a30265e1b4577b845bb8d235a97eb"> One y value per x value <a href="#2b2a30265e1b4577b845bb8d235a97eb">#</a> </h3> <p> There were a few more issues to be addressed. The black curve in the black-and-white bitmap is thicker than a single pixel. This means that for each <em>x</em> value, there will be several black pixels. In order to do linear regression, however, we need a single <em>y</em> value per <em>x</em> value. </p> <p> One easy way to address that concern is to calculate the average <em>y</em> value for each <em>x</em> value. This may not always be the best choice, but as far as we can see in the above black-and-white image, it doesn't look as though there's any noise left in the picture. This means that we don't have to worry about outliers pulling the average value away from the curve. In other words, finding the average <em>y</em> value is an easy way to get what we need. </p> <p> <pre><span style="color:#2b91af;">averageY</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Integral</span>&nbsp;b&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">NonEmpty</span>&nbsp;(a,&nbsp;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a,&nbsp;b) averageY&nbsp;nel&nbsp;=&nbsp;(<span style="color:blue;">fst</span>&nbsp;$&nbsp;NE.<span style="color:blue;">head</span>&nbsp;nel,&nbsp;average&nbsp;$&nbsp;<span style="color:blue;">snd</span>&nbsp;&lt;$&gt;&nbsp;nel)</pre> </p> <p> The <code>averageY</code> function converts a <code>NonEmpty</code> list of tuples to a single tuple. <em>Watch out!</em> The input tuples are not the 'outer' tuples that <code>pixelCoordinates</code> returns, but rather a list of actual pixel coordinates. Each tuple is a set of coordinates, but since the function never manipulates the <em>x</em> coordinate, the type of the first element is just unconstrained <code>a</code>. It can literally be anything, but will, in practice, be an integer. </p> <p> The assumption is that the input is a small list of coordinates that all share the same <em>x</em> coordinate, such as <code>(42, 99) :| [(42, 100), (42, 102)]</code>. The function simply returns a single tuple that it creates on the fly. For the first element of the return tuple, it picks the <code>head</code> tuple from the input (<code>(42, 99)</code> in the example), and then that tuple's <code>fst</code> element (<code>42</code>). For the second element, the function averages all the <code>snd</code> elements (<code>99</code>, <code>100</code>, and <code>102</code>) to get <code>100</code> (integer division, you may recall): </p> <p> <pre>ghci&gt; averageY ((42, 99) :| [(42, 100), (42, 102)]) (42,100)</pre> </p> <p> What remains is to glue together the building blocks. </p> <h3 id="305d72ac4dd94c41aa23f6581f7aa716"> Extracting curve coordinates <a href="#305d72ac4dd94c41aa23f6581f7aa716">#</a> </h3> <p> A few more steps were required, but these I just composed <em>in situ</em>. I found no need to define them as individual functions. </p> <p> The final composition looks like this: </p> <p> <pre><span style="color:#2b91af;">main</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;readResult&nbsp;&lt;-&nbsp;readImage&nbsp;<span style="color:#a31515;">&quot;hype-cycle-cleaned.png&quot;</span> &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;readResult&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Left&nbsp;msg&nbsp;-&gt;&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;Right&nbsp;img&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;bwImg&nbsp;=&nbsp;pixelMap&nbsp;toBW&nbsp;$&nbsp;convertRGB8&nbsp;img &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;blackPixels&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">fst</span>&nbsp;&lt;$&gt;&nbsp;<span style="color:blue;">filter</span>&nbsp;((black&nbsp;==)&nbsp;.&nbsp;<span style="color:blue;">snd</span>)&nbsp;(pixelCoordinates&nbsp;bwImg) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;h&nbsp;=&nbsp;imageHeight&nbsp;bwImg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;lineCoords&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;(h&nbsp;-)&nbsp;.&nbsp;averageY&nbsp;&lt;$&gt;&nbsp;NE.groupAllWith&nbsp;<span style="color:blue;">fst</span>&nbsp;blackPixels &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">writeFile</span>&nbsp;<span style="color:#a31515;">&quot;coords.txt&quot;</span>&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">unlines</span>&nbsp;$&nbsp;(\(x,y)&nbsp;-&gt;&nbsp;<span style="color:blue;">show</span>&nbsp;x&nbsp;++&nbsp;<span style="color:#a31515;">&quot;,&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;y)&nbsp;&lt;$&gt;&nbsp;lineCoords</pre> </p> <p> The first lines of code, until and including <code>let bwImg</code>, are identical to what you've already seen. </p> <p> We're only interested in the black pixels, so the <code>main</code> action uses the standard <code>filter</code> function to keep only those that are equal to the <code>black</code> constant value. Once the white pixels are gone, we no longer need the pixel information. The expression that defines the <code>blackPixels</code> value finally (remember, you read Haskell code from right to left) throws away the pixel information by only retaining the <code>fst</code> element. That's the tuple that contains the coordinates. You may want to refer back to the type signature of <code>pixelCoordinates</code> to see what I mean. </p> <p> The <code>blackPixels</code> value has the type <code>[(Int, Int)]</code>. </p> <p> Two more things need to happen. One is to group the pixels together per <em>x</em> value so that we can use <code>averageY</code>. The other is that we want the coordinates as normal Cartesian coordinates, and right now, they're in screen coordinates. </p> <p> When working with bitmaps, it's quite common that pixels are measured out from the top left corner, instead of from the bottom left corner. It's not difficult to flip the coordinates, but we need to know the height of the image: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;h&nbsp;=&nbsp;imageHeight&nbsp;bwImg</pre> </p> <p> The <a href="https://hackage.haskell.org/package/JuicyPixels/docs/Codec-Picture.html#v:imageHeight">imageHeight</a> function is another JuicyPixels function. </p> <p> Because I sometimes get carried away, I write the code in a 'nice' compact style that could be more readable. I accomplished both of the above remaining tasks with a single line of code: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;lineCoords&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;(h&nbsp;-)&nbsp;.&nbsp;averageY&nbsp;&lt;$&gt;&nbsp;NE.groupAllWith&nbsp;<span style="color:blue;">fst</span>&nbsp;blackPixels</pre> </p> <p> This first groups the coordinates according to <em>x</em> value, so that all coordinates that share an <em>x</em> value are collected in a single <code>NonEmpty</code> list. This means that we can map all of those groups over <code>averageY</code>. Finally, the expression flips from screen coordinates to Cartesian coordinates by subtracting the <em>y</em> coordinate from the height <code>h</code>. </p> <p> The final <code>writeFile</code> expression writes the coordinates to a text file as <a href="https://en.wikipedia.org/wiki/Comma-separated_values">comma-separated values</a>. The first ten lines of that file looks like this: </p> <p> <pre>9,13 10,13 11,13 12,14 13,15 14,15 15,16 16,17 17,17 18,18 ...</pre> </p> <p> Do these points plot the Gartner hype cycle? </p> <h3 id="eed82185c8cd43dda147bce839454ca9"> Sanity checking by plotting the coordinates <a href="#eed82185c8cd43dda147bce839454ca9">#</a> </h3> <p> To check whether the coordinates look useful, we could plot them. If I wanted to use a few more hours, I could probably figure out how to do that with JuicyPixels as well, but on the other hand, I already know how to do that with Python: </p> <p> <pre>data&nbsp;=&nbsp;numpy.loadtxt(<span style="color:#a31515;">&#39;coords.txt&#39;</span>,&nbsp;delimiter=<span style="color:#a31515;">&#39;,&#39;</span>) x&nbsp;=&nbsp;data[:,&nbsp;0] t&nbsp;=&nbsp;data[:,&nbsp;1] plt.scatter(x,&nbsp;t,&nbsp;s=10,&nbsp;c=<span style="color:#a31515;">&#39;g&#39;</span>) plt.show()</pre> </p> <p> That produces this plot: </p> <p> <img src="/content/binary/hype-cycle-pyplot.png" alt="Coordinates plotted with Python."> </p> <p> LGTM. </p> <h3 id="9836c90de9ac487f9295acfb667090b7"> Conclusion <a href="#9836c90de9ac487f9295acfb667090b7">#</a> </h3> <p> In this article, you've seen how a single Haskell script can extract curve coordinates from a bitmap. The file is 41 lines all in all, including module declaration and white space. This article shows every single line in that file, apart from some blank lines. </p> <p> I loaded the file into GHCi and ran the <code>main</code> action in order to produce the CSV file. </p> <p> I did spend a few hours looking around in the JuicyPixels documentation before I'd identified the functions that I needed. All in all I used some hours on this exercise. I didn't keep track of time, but I guess that I used more than three, but probably fewer than six, hours on this. </p> <p> This was the successful part of the overall exercise. Now onto the fiasco. </p> <p> <strong>Next:</strong> <a href="/2024/04/22/fitting-a-polynomial-to-a-set-of-points">Fitting a polynomial to a set of points</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Trying to fit the hype cycle https://blog.ploeh.dk/2024/04/01/trying-to-fit-the-hype-cycle 2024-04-01T07:14:00+00:00 Mark Seemann <div id="post"> <p> <em>An amateur tries his hand at linear modelling.</em> </p> <p> About a year ago, I was contemplating a conference talk I was going to give. Although I later abandoned the idea for other reasons, for a few days I was thinking about using the <a href="https://en.wikipedia.org/wiki/Gartner_hype_cycle">Gartner hype cycle</a> for an animation. What I had in mind would require me to draw the curve in a way that would enable me to zoom in and out. Vector graphics would be much more useful for that job than a bitmap. </p> <p> <img src="/content/binary/hype-cycle-cleaned.png" alt="Gartner hype cycle."> </p> <p> Along the way, I considered if there was a <a href="https://en.wikipedia.org/wiki/Function_(mathematics)">function</a> that would enable me to draw it on the fly. A few web searches revealed the <a href="https://stats.stackexchange.com/">Cross Validated</a> question <a href="https://stats.stackexchange.com/q/268293/397132">Is there a linear/mixture function that can fit the Gartner hype curve?</a> So I wasn't the first person to have that idea, but at the time I found it, the question was effectively dismissed without a proper answer. Off topic, dontcha know? </p> <p> A web search also seems to indicate the existence of a few research papers where people have undertaken this task, but there's not a lot about it. True, the Gartner hype cycle isn't a real function, but it sounds like a relevant exercise in statistics, if one's into that kind of thing. </p> <p> Eventually, for my presentation, I went with another way to illustrate what I wanted to say, so for half I year, I didn't think more about it. </p> <h3 id="f3bfad5e6e80409e9703c80b1c98099b"> Linear regression? <a href="#f3bfad5e6e80409e9703c80b1c98099b">#</a> </h3> <p> Recently, however, I was following a course in mathematical analysis of data, and among other things, I learned how to fit a line to data. Not just a straight line, but any degree of <a href="https://en.wikipedia.org/wiki/Polynomial">polynomial</a>. So I thought that perhaps it'd be an interesting exercise to see if I could fit the hype cycle to some high-degree polynomial - even though I do realize that the hype cycle isn't a real function, and neither does it look like a straight polynomial function. </p> <p> In order to fit a polynomial to the curve, I needed some data, so my first task was to convert an image to a series of data points. </p> <p> I'm sure that there are online tools and apps that offer to do that for me, but the whole point of this was that I wanted to learn how to tackle problems like these. It's like <a href="/2020/01/13/on-doing-katas">doing katas</a>. The journey is the goal. </p> <p> This turned out to be an exercise consisting of two phases so distinct that I wrote them in two different languages. </p> <ul> <li><a href="/2024/04/08/extracting-curve-coordinates-from-a-bitmap">Extracting curve coordinates from a bitmap</a></li> <li><a href="/2024/04/22/fitting-a-polynomial-to-a-set-of-points">Fitting a polynomial to a set of points</a></li> </ul> <p> As the articles will reveal, the first part went quite well, while the other was, essentially, a fiasco. </p> <h3 id="fc418f36d6c74aa2a056b48489be7162"> Conclusion <a href="#fc418f36d6c74aa2a056b48489be7162">#</a> </h3> <p> There's not much point in finding a formula for the Gartner hype cycle, but the goal of this exercise was, for me, to tinker with some new techniques to see if I could learn from doing the exercise. And I <em>did</em> learn something. </p> <p> In the next articles in this series, I'll go over some of the details. </p> <p> <strong>Next:</strong> <a href="/2024/04/08/extracting-curve-coordinates-from-a-bitmap">Extracting curve coordinates from a bitmap</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Services are autonomous https://blog.ploeh.dk/2024/03/25/services-are-autonomous 2024-03-25T08:31:00+00:00 Mark Seemann <div id="post"> <p> <em>A reading of the second Don Box tenet, with some commentary.</em> </p> <p> This article is part of a series titled <a href="/2024/03/04/the-four-tenets-of-soa-revisited">The four tenets of SOA revisited</a>. In each of these articles, I'll pull one of <a href="https://en.wikipedia.org/wiki/Don_Box">Don Box</a>'s <em>four tenets of service-oriented architecture</em> (SOA) out of the <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/january/a-guide-to-developing-and-running-connected-systems-with-indigo">original MSDN Magazine article</a> and add some of my own commentary. If you're curious why I do that, I cover that in the introductory article. </p> <p> In this article, I'll go over the second tenet. The quotes are from the MSDN Magazine article unless otherwise indicated. </p> <h3 id="5021be8510304665ba3a8b9d9287a531"> Services are autonomous <a href="#5021be8510304665ba3a8b9d9287a531">#</a> </h3> <p> Compared with <a href="/2024/03/11/boundaries-are-explicit">the first tenet</a>, you'll see that Don Box had more to say about this one. I, conversely, have less to add. First, here's what the article said: </p> <blockquote> <p> Service-orientation mirrors the real world in that it does not assume the presence of an omniscient or omnipotent oracle that has awareness and control over all parts of a running system. This notion of service autonomy appears in several facets of development, the most obvious place being the area of deployment and versioning. </p> <p> Object-oriented programs tend to be deployed as a unit. Despite the Herculean efforts made in the 1990s to enable classes to be independently deployed, the discipline required to enable object-oriented interaction with a component proved to be impractical for most development organizations. When coupled with the complexities of versioning object-oriented interfaces, many organizations have become extremely conservative in how they roll out object-oriented code. The popularity of the XCOPY deployment and private assemblies capabilities of the .NET Framework is indicative of this trend. </p> <p> Service-oriented development departs from object-orientation by assuming that atomic deployment of an application is the exception, not the rule. While individual services are almost always deployed atomically, the aggregate deployment state of the overall system/application rarely stands still. It is common for an individual service to be deployed long before any consuming applications are even developed, let alone deployed into the wild. Amazon.com is one example of this build-it-and-they-will-come philosophy. There was no way the developers at Amazon could have known the multitude of ways their service would be used to build interesting and novel applications. </p> <p> It is common for the topology of a service-oriented application to evolve over time, sometimes without direct intervention from an administrator or developer. The degree to which new services may be introduced into a service-oriented system depends on both the complexity of the service interaction and the ubiquity of services that interact in a common way. Service-orientation encourages a model that increases ubiquity by reducing the complexity of service interactions. As service-specific assumptions leak into the public facade of a service, fewer services can reasonably mimic that facade and stand in as a reasonable substitute. </p> <p> The notion of autonomous services also impacts the way failures are handled. Objects are deployed to run in the same execution context as the consuming application. Service-oriented designs assume that this situation is the exception, not the rule. For that reason, services expect that the consuming application can fail without notice and often without any notification. To maintain system integrity, service-oriented designs use a variety of techniques to deal with partial failure modes. Techniques such as transactions, durable queues, and redundant deployment and failover are quite common in a service-oriented system. </p> <p> Because many services are deployed to function over public networks (such as the Internet), service-oriented development assumes not only that incoming message data may be malformed but also that it may have been transmitted for malicious purposes. Service-oriented architectures protect themselves by placing the burden of proof on all message senders by requiring applications to prove that all required rights and privileges have been granted. Consistent with the notion of service autonomy, service-oriented architectures invariably rely on administratively managed trust relationships in order to avoid per-service authentication mechanisms common in classic Web applications. </p> </blockquote> <p> Again, I'd like to highlight how general these ideas are. Once lifted out of the context of <a href="https://en.wikipedia.org/wiki/Windows_Communication_Foundation">Windows Communication Foundation</a>, all of this applies more broadly. </p> <p> Perhaps a few details now seem dated, but in general I find that this description holds up well. </p> <h3 id="f921c1135edd46d688729181489a9c73"> Wildlife <a href="#f921c1135edd46d688729181489a9c73">#</a> </h3> <p> It's striking that someone in 2004 observed that big, complex, coordinated releases are impractical. Even so, it doesn't seem as though adopting a network-based technology and architecture in itself solves that problem. <a href="/2012/12/18/ZookeepersmustbecomeRangers">I wrote about that in 2012</a>, and I've seen <a href="https://youtu.be/jdliXz70NtM?si=NRSHFqaVHMvWnOPF">Adam Ralph make a similar observation</a>. Many organizations inadvertently create distributed monoliths. I think that this often stems from a failure of heeding the tenet that services are autonomous. </p> <p> I've experienced the following more than once. A team of developers rely on a service. As they take on a new feature, they realize that the way things are currently modelled prevents them from moving forward. Typical examples include mismatched cardinalities. For example, a customer record has a single active address, but the new feature requires that customers may have multiple active addresses. It could be that a customer has a permanent address, but also a summerhouse. </p> <p> It is, however, the other service that defines how customer addresses are modelled, so the development team contacts the service team to discuss a breaking change. The service team agrees to the breaking change, but this means that the service and the relying client team now have to coordinate when they deploy the new versions of their software. The service is no longer autonomous. </p> <p> I've already discussed this kind of problem in <a href="/2023/11/27/synchronizing-concurrent-teams">a previous article</a>, and as Don Box also implied, this discussion is related to the question of versioning, which we'll get back to when covering the fourth tenet. </p> <h3 id="11028dabd5a540cf9160c06c3e1b283c"> Transactions <a href="#11028dabd5a540cf9160c06c3e1b283c">#</a> </h3> <p> It may be worthwhile to comment on this sentence: </p> <blockquote> <p> Techniques such as transactions, durable queues, and redundant deployment and failover are quite common in a service-oriented system. </p> </blockquote> <p> Indeed, but particularly regarding database transactions, a service may use them <em>internally</em> (typically leveraging a database engine like <a href="https://en.wikipedia.org/wiki/Microsoft_SQL_Server">SQL Server</a>, <a href="https://en.wikipedia.org/wiki/Oracle_Database">Oracle</a>, <a href="https://en.wikipedia.org/wiki/PostgreSQL">PostgreSQL</a>, etc.), but not across services. Around the time Don Box wrote the original MSDN Magazine article an extension to SOAP colloquially known as <em>WS-Death Star</em> was in the works, and it included <a href="https://en.wikipedia.org/wiki/WS-Transaction">WS Transaction</a>. </p> <p> I don't know whether Don Box had something like this in mind when he wrote the word <em>transaction</em>, but in my experience, you don't want to go there. If you need to, you can make use of database transactions to keep your own service <a href="https://en.wikipedia.org/wiki/ACID">ACID</a>-consistent, but don't presume that this is possible with multiple autonomous services. </p> <p> As always, even if a catchphrase such as <em>services are autonomous</em> sounds good, it's always illuminating to understand that there are trade-offs involved - and what they are. Here, a major trade-off is that you need to think about error-handling in a different way. If you don't already know how to address such concerns, look up <em>lock-free transactions</em> and <a href="https://en.wikipedia.org/wiki/Eventual_consistency">eventual consistency</a>. As Don Box also mentioned, durable queues are often part of such a solution, as is <a href="https://en.wikipedia.org/wiki/Idempotence">idempotence</a>. </p> <h3 id="7dc237c5f67c42c8b2c439140fc7a05b"> Validation <a href="#7dc237c5f67c42c8b2c439140fc7a05b">#</a> </h3> <p> From this discussion follows that an autonomous service should, ideally, exist independently of the software ecosystem in which it exists. While an individual service can't impose its will on its surroundings, it can, and should, behave in a consistent and correct manner. </p> <p> This does include deliberate consistency for the service itself. An autonomous service may make use of ACID or eventual consistency as the service owner deems appropriate. </p> <p> It should also treat all input as suspect, until proven otherwise. Input validation is an important part of service design. It is my belief that <a href="/2020/12/14/validation-a-solved-problem">validation is a solved problem</a>, but that doesn't mean that you don't have to put in the work. You should consider correctness, versioning, as well as <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. </p> <h3 id="2482dbc1c20248fdb61a7347abce49ef"> Security <a href="#2482dbc1c20248fdb61a7347abce49ef">#</a> </h3> <p> A similar observation relates to security. Some services (particularly read-only services) may allow for anonymous access, but if a service needs to authenticate or authorize requests, consider how this is done in an autonomous manner. Looking up account information in a centralized database isn't the autonomous way. If a service does that, it now relies on the account database, and is no longer autonomous. </p> <p> Instead, rely on <a href="https://en.wikipedia.org/wiki/Claims-based_identity">claims-based identity</a>. In my experience, <a href="https://en.wikipedia.org/wiki/OAuth">OAuth</a> with <a href="https://en.wikipedia.org/wiki/JSON_Web_Token">JWT</a> is usually fine. </p> <p> If your service needs to know something about the user that only an external source can tell it, don't look it up in an external system. Instead, demand that it's included in the JWT as a claim. Do you need to validate the age of the user? Require a <em>date-of-birth</em> or <em>age</em> claim. Do you need to know if the request is made on behalf of a system administrator? Demand a list of <em>role</em> claims. </p> <h3 id="75412f1e737a45dfaaf11c54e28013fa"> Conclusion <a href="#75412f1e737a45dfaaf11c54e28013fa">#</a> </h3> <p> The second of Don Box's four tenets of SOA state that services should be autonomous. At first glance, you may think that all this means is that a service shouldn't share its database with another service. That is, however, a minimum bar. You need to consider how a service exists in an environment that it doesn't control. Again, the <a href="/2012/12/18/RangersandZookeepers">wildlife metaphor</a> seems apt. Particularly if your service is exposed to the internet, it lives in a hostile environment. </p> <p> Not only should you consider all input belligerent, you must also take into account that friendly systems may disappear or change. Your service exists by itself, supported by itself, relying on itself. If you need to coordinate work with other service owners, that's a strong hint that your service isn't, after all, autonomous. </p> <p> <strong>Next:</strong> <a href="/2024/04/15/services-share-schema-and-contract-not-class">Services share schema and contract, not class</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Extracting data from a small CSV file with Python https://blog.ploeh.dk/2024/03/18/extracting-data-from-a-small-csv-file-with-python 2024-03-18T08:36:00+00:00 Mark Seemann <div id="post"> <p> <em>My inept adventures with a dynamically typed language.</em> </p> <p> This article is the third in <a href="/2024/02/05/statically-and-dynamically-typed-scripts">a small series about ad-hoc programming in two languages</a>. In <a href="/2024/02/19/extracting-data-from-a-small-csv-file-with-haskell">the previous article</a> you saw how I originally solved a small data extraction and analysis problem with <a href="https://www.haskell.org/">Haskell</a>, even though it was strongly implied that <a href="https://www.python.org/">Python</a> was the language for the job. </p> <p> Months after having solved the problem I'd learned a bit more Python, so I decided to return to it and do it again in Python as an exercise. In this article, I'll briefly describe what I did. </p> <h3 id="590b0c98bf064ac0b8893ae41d398daa"> Reading CSV data <a href="#590b0c98bf064ac0b8893ae41d398daa">#</a> </h3> <p> When writing Python, I feel the way I suppose a script kiddie might feel. I cobble together code based on various examples I've seen somewhere else, without a full or deep understanding of what I'm doing. There's more than a hint of <a href="/ref/pragmatic-programmer">programming by coincidence</a>, I'm afraid. One thing I've picked up along the way is that I can use <a href="https://pandas.pydata.org/">pandas</a> to read a <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV file</a>: </p> <p> <pre>data&nbsp;=&nbsp;pd.read_csv(<span style="color:#a31515;">&#39;survey_data.csv&#39;</span>,&nbsp;header=<span style="color:blue;">None</span>) grades&nbsp;=&nbsp;data.iloc[:,&nbsp;2] experiences&nbsp;=&nbsp;data.iloc[:,&nbsp;3]</pre> </p> <p> In order for this to work, I needed to import <code>pandas</code>. Ultimately, my imports looked like this: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;pandas&nbsp;<span style="color:blue;">as</span>&nbsp;pd <span style="color:blue;">from</span>&nbsp;collections&nbsp;<span style="color:blue;">import</span>&nbsp;Counter <span style="color:blue;">from</span>&nbsp;itertools&nbsp;<span style="color:blue;">import</span>&nbsp;combinations,&nbsp;combinations_with_replacement <span style="color:blue;">import</span>&nbsp;matplotlib.pyplot&nbsp;<span style="color:blue;">as</span>&nbsp;plt</pre> </p> <p> In other Python code that I've written, I've been a heavy user of <a href="https://numpy.org/">NumPy</a>, and while I several times added it to my imports, I never needed it for this task. That was a bit surprising, but I've only done Python programming for a year, and I still don't have a good feel for the ecosystem. </p> <p> The above code snippet also demonstrates how easy it is to slice a <em>dataframe</em> into columns: <code>grades</code> contains all the values in the (zero-indexed) second column, and <code>experiences</code> likewise the third column. </p> <h3 id="2a5c679e37394960acf5cf283abd41d5"> Sum of grades <a href="#2a5c679e37394960acf5cf283abd41d5">#</a> </h3> <p> All the trouble I had with binomial choice without replacement that I had with my Haskell code is handled with <code>combinations</code>, which happily handles duplicate values: </p> <p> <pre>&gt&gt&gt list(combinations('foo', 2)) [('f', 'o'), ('f', 'o'), ('o', 'o')]</pre> </p> <p> Notice that <code>combinations</code> doesn't list <code>('o', 'f')</code>, since (apparently) it doesn't consider ordering important. That's more in line with the <a href="https://en.wikipedia.org/wiki/Binomial_coefficient">binomial coefficient</a>, whereas <a href="/2024/02/19/extracting-data-from-a-small-csv-file-with-haskell">my Haskell code</a> considers a tuple like <code>('f', 'o')</code> to be distinct from <code>('o', 'f')</code>. This is completely consistent with how Haskell works, but means that all the counts I arrived at with Haskell are double what they are in this article. Ultimately, <em>6/1406</em> is equal to <em>3/703</em>, so the probabilities are the same. I'll try to call out this factor-of-two difference whenever it occurs. </p> <p> A <code>Counter</code> object counts the number of occurrences of each value, so reading, picking combinations without replacement and adding them together is just two lines of code, and one more to print them: </p> <p> <pre>sumOfGrades&nbsp;=&nbsp;Counter(<span style="color:blue;">map</span>(<span style="color:blue;">sum</span>,&nbsp;combinations(grades,&nbsp;2))) sumOfGrades&nbsp;=&nbsp;<span style="color:blue;">sorted</span>(sumOfGrades.items(),&nbsp;key=<span style="color:blue;">lambda</span>&nbsp;item:&nbsp;item[0]) <span style="color:blue;">print</span>(<span style="color:blue;">f</span><span style="color:#a31515;">&#39;Sums&nbsp;of&nbsp;grades:&nbsp;</span>{sumOfGrades}<span style="color:#a31515;">&#39;</span>)</pre> </p> <p> The output is: </p> <p> <pre>Sums of grades: [(0, 3), (2, 51), (4, 157), (6, 119), (7, 24), (8, 21), (9, 136), (10, 3), (11, 56), (12, 23), (14, 69), (16, 14), (17, 8), (19, 16), (22, 2), (24, 1)]</pre> </p> <p> (Formatting courtesy of yours truly.) </p> <p> As already mentioned, these values are off by a factor two compared to the previous Haskell code, but since I'll ultimately be dealing in ratios, it doesn't matter. What this output indicates is that the sum <em>0</em> occurs three times, the sum <em>2</em> appears <em>51</em> times, and so on. </p> <p> This is where I, in my Haskell code, dropped down to a few ephemeral <a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>-based queries that enabled me to collect enough information to paste into Excel in order to produce a figure. In Python, however, I have <a href="https://matplotlib.org/">Matplotlib</a>, which means that I can create the desired plots entirely in code. It does require that I write a bit more code, though. </p> <p> First, I need to calculate the range of the <a href="https://www.probabilitycourse.com/chapter3/3_1_3_pmf.php">Probability Mass Function</a> (PMF), since there are values that are possible, but not represented in the above data set. To calculate all possible values in the PMF's range, I use <code>combinations_with_replacement</code> against the <a href="https://en.wikipedia.org/wiki/Academic_grading_in_Denmark">Danish grading scale</a>. </p> <p> <pre>grade_scale&nbsp;=&nbsp;[-3,&nbsp;0,&nbsp;2,&nbsp;4,&nbsp;7,&nbsp;10,&nbsp;12] sumOfGradesRange&nbsp;=&nbsp;<span style="color:#2b91af;">set</span>(<span style="color:blue;">map</span>(<span style="color:blue;">sum</span>,&nbsp;combinations_with_replacement(grade_scale,&nbsp;2))) sumOfGradesRange&nbsp;=&nbsp;<span style="color:blue;">sorted</span>(sumOfGradesRange) <span style="color:blue;">print</span>(<span style="color:blue;">f</span><span style="color:#a31515;">&#39;Range&nbsp;of&nbsp;sums&nbsp;of&nbsp;grades:&nbsp;</span>{sumOfGradesRange}<span style="color:#a31515;">&#39;</span>)</pre> </p> <p> The output is this: </p> <p> <pre>Range of sums of grades: [-6, -3, -1, 0, 1, 2, 4, 6, 7, 8, 9, 10, 11, 12, 14, 16, 17, 19, 20, 22, 24]</pre> </p> <p> Next, I create a dictionary of all possible grades, initializing all entries to zero, but then updating that dictionary with the observed values, where they are present: </p> <p> <pre>probs&nbsp;=&nbsp;<span style="color:#2b91af;">dict</span>.fromkeys(sumOfGradesRange,&nbsp;0) probs.update(<span style="color:#2b91af;">dict</span>(sumOfGrades))</pre> </p> <p> Finally, I recompute the dictionary entries to probabilities. </p> <p> <pre>total&nbsp;=&nbsp;<span style="color:blue;">sum</span>(x[1]&nbsp;<span style="color:blue;">for</span>&nbsp;x&nbsp;<span style="color:blue;">in</span>&nbsp;sumOfGrades) <span style="color:blue;">for</span>&nbsp;k,&nbsp;v&nbsp;<span style="color:blue;">in</span>&nbsp;probs.items(): &nbsp;&nbsp;&nbsp;&nbsp;probs[k]&nbsp;=&nbsp;v&nbsp;/&nbsp;total</pre> </p> <p> Now I have all the data needed to plot the desired bar char: </p> <p> <pre>plt.bar(probs.keys(),&nbsp;probs.values()) plt.xlabel(<span style="color:#a31515;">&#39;Sum&#39;</span>) plt.ylabel(<span style="color:#a31515;">&#39;Probability&#39;</span>) plt.show()</pre> </p> <p> The result looks like this: </p> <p> <img src="/content/binary/sum-pmf-plot.png" alt="Bar chart of the sum-of-grades PMF."> </p> <p> While I'm already on line 34 in my Python file, with one more question to answer, I've written proper code in order to produce data that I only wrote ephemeral queries for in Haskell. </p> <h3 id="8831d23c67bd48e9b22db86ca3c21bd4"> Difference of experiences <a href="#8831d23c67bd48e9b22db86ca3c21bd4">#</a> </h3> <p> The next question is almost a repetition of the the first one, and I've addressed it by copying and pasting. After all, it's only <em>duplication</em>, not <em>triplication</em>, so I can always invoke the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">Rule of Three</a>. Furthermore, this is a one-off script that I don't expect to have to maintain in the future, so copy-and-paste, here we go: </p> <p> <pre>diffOfExperiances&nbsp;=&nbsp;\ &nbsp;&nbsp;&nbsp;&nbsp;Counter(<span style="color:blue;">map</span>(<span style="color:blue;">lambda</span>&nbsp;x:&nbsp;<span style="color:blue;">abs</span>(x[0]&nbsp;-&nbsp;x[1]),&nbsp;combinations(experiences,&nbsp;2))) diffOfExperiances&nbsp;=&nbsp;<span style="color:blue;">sorted</span>(diffOfExperiances.items(),&nbsp;key=<span style="color:blue;">lambda</span>&nbsp;item:&nbsp;item[0]) <span style="color:blue;">print</span>(<span style="color:blue;">f</span><span style="color:#a31515;">&#39;Differences&nbsp;of&nbsp;experiences:&nbsp;</span>{diffOfExperiances}<span style="color:#a31515;">&#39;</span>) experience_scale&nbsp;=&nbsp;<span style="color:#2b91af;">list</span>(<span style="color:blue;">range</span>(1,&nbsp;8)) diffOfExperiancesRange&nbsp;=&nbsp;<span style="color:#2b91af;">set</span>(\ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">map</span>(<span style="color:blue;">lambda</span>&nbsp;x:&nbsp;<span style="color:blue;">abs</span>(x[0]&nbsp;-&nbsp;x[1]),\ &nbsp;&nbsp;&nbsp;&nbsp;combinations_with_replacement(experience_scale,&nbsp;2))) diffOfExperiancesRange&nbsp;=&nbsp;<span style="color:blue;">sorted</span>(diffOfExperiancesRange) probs&nbsp;=&nbsp;<span style="color:#2b91af;">dict</span>.fromkeys(diffOfExperiancesRange,&nbsp;0) probs.update(<span style="color:#2b91af;">dict</span>(diffOfExperiances)) total&nbsp;=&nbsp;<span style="color:blue;">sum</span>(x[1]&nbsp;<span style="color:blue;">for</span>&nbsp;x&nbsp;<span style="color:blue;">in</span>&nbsp;diffOfExperiances) <span style="color:blue;">for</span>&nbsp;k,&nbsp;v&nbsp;<span style="color:blue;">in</span>&nbsp;probs.items(): &nbsp;&nbsp;&nbsp;&nbsp;probs[k]&nbsp;=&nbsp;v&nbsp;/&nbsp;total <span style="color:green;">#&nbsp;Plot&nbsp;the&nbsp;histogram&nbsp;of&nbsp;differences&nbsp;of&nbsp;experiences</span> plt.bar(probs.keys(),&nbsp;probs.values()) plt.xlabel(<span style="color:#a31515;">&#39;Difference&#39;</span>) plt.ylabel(<span style="color:#a31515;">&#39;Probability&#39;</span>) plt.show()</pre> </p> <p> The bar chart has the same style as before, but obviously displays different data. See the bar chart in the <a href="/2024/02/19/extracting-data-from-a-small-csv-file-with-haskell">previous article</a> for the Excel-based rendition of that data. </p> <h3 id="8d7d707edeba43c59d07b5753a4bdb2d"> Conclusion <a href="#8d7d707edeba43c59d07b5753a4bdb2d">#</a> </h3> <p> The Python code runs to 61 lines of code, compared with the 34 lines of Haskell code. The Python code, however, is much more complete than the Haskell code, since it also contains the code that computes the range of each PMF, as well as code that produces the figures. </p> <p> Like the Haskell code, it took me a couple of hours to produce this, so I can't say that I feel much more productive in Python than in Haskell. On the other hand, I also acknowledge that I have less experience writing Python code. If I had to do a lot of ad-hoc data crunching like this, I can see how Python is useful. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Boundaries are explicit https://blog.ploeh.dk/2024/03/11/boundaries-are-explicit 2024-03-11T08:03:00+00:00 Mark Seemann <div id="post"> <p> <em>A reading of the first Don Box tenet, with some commentary.</em> </p> <p> This article is part of a series titled <a href="/2024/03/04/the-four-tenets-of-soa-revisited">The four tenets of SOA revisited</a>. In each of these articles, I'll pull one of <a href="https://en.wikipedia.org/wiki/Don_Box">Don Box</a>'s <em>four tenets of service-oriented architecture</em> (SOA) out of the <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/january/a-guide-to-developing-and-running-connected-systems-with-indigo">original MSDN Magazine article</a> and add some of my own commentary. If you're curious why I do that, I cover that in the introductory article. </p> <p> In this article, I'll go over the first tenet, quoting from the MSDN Magazine article unless otherwise indicated. </p> <h3 id="3d25f37d4da8482fa846b8660823b8cd"> Boundaries are explicit <a href="#3d25f37d4da8482fa846b8660823b8cd">#</a> </h3> <p> This tenet was the one I struggled with the most. It took me a long time to come to grips with how to apply it, but I'll get back to that in a moment. First, here's what the article said: </p> <blockquote> <p> A service-oriented application often consists of services that are spread over large geographical distances, multiple trust authorities, and distinct execution environments. The cost of traversing these various boundaries is nontrivial in terms of complexity and performance. Service-oriented designs acknowledge these costs by putting a premium on boundary crossings. Because each cross-boundary communication is potentially costly, service-orientation is based on a model of explicit message passing rather than implicit method invocation. Compared to distributed objects, the service-oriented model views cross-service method invocation as a private implementation technique, not as a primitive construct—the fact that a given interaction may be implemented as a method call is a private implementation detail that is not visible outside the service boundary. </p> <p> Though service-orientation does not impose the RPC-style notion of a network-wide call stack, it can support a strong notion of causality. It is common for messages to explicitly indicate which chain(s) of messages a particular message belongs to. This indication is useful for message correlation and for implementing several common concurrency models. </p> <p> The notion that boundaries are explicit applies not only to inter-service communication but also to inter-developer communication. Even in scenarios in which all services are deployed in a single location, it is commonplace for the developers of each service to be spread across geographical, cultural, and/or organizational boundaries. Each of these boundaries increases the cost of communication between developers. Service orientation adapts to this model by reducing the number and complexity of abstractions that must be shared across service boundaries. By keeping the surface area of a service as small as possible, the interaction and communication between development organizations is reduced. One theme that is consistent in service-oriented designs is that simplicity and generality aren't a luxury but rather a critical survival skill. </p> </blockquote> <p> Notice that there's nothing here about <a href="https://en.wikipedia.org/wiki/Windows_Communication_Foundation">Windows Communication Framework</a> (WCF), or any other specific technology. This is common to all four tenets, and one of the reasons that I think they deserve to be lifted out of their original context and put on display as the general ideas that they are. </p> <p> I'm getting the vibe that the above description was written under the impression of the disenchantment with distributed objects that was setting in around that time. The year before, <a href="https://martinfowler.com/">Martin Fowler</a> had formulated his </p> <blockquote> <p> "<strong>First Law of Distributed Object Design:</strong> Don't distribute your objects!" </p> <footer><cite>Martin Fowler, <a href="/ref/peaa">Patterns of Enterprise Application Architecture</a>, (his emphasis)</cite></footer> </blockquote> <p> The way that I read the tenet then, and the way I <em>still</em> read it today, is that in contrast to distributed objects, you should treat any service invocation as a noticeable operation, <em>"putting a premium on boundary crossings"</em>, somehow distinct from normal code. </p> <p> Perhaps I read to much into that, because WCF immediately proceeded to convert any <a href="https://en.wikipedia.org/wiki/SOAP">SOAP</a> service into a lot of auto-generated C# code that would then enable you to invoke operations on a remote service using (you guessed it) a method invocation. </p> <p> Here a code snippet from the <a href="https://learn.microsoft.com/dotnet/framework/wcf/how-to-use-a-wcf-client">WCF documentation</a>: </p> <p> <pre><span style="color:blue;">double</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">value1</span>&nbsp;=&nbsp;100.00D; <span style="color:blue;">double</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">value2</span>&nbsp;=&nbsp;15.99D; <span style="color:blue;">double</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">result</span>&nbsp;=&nbsp;client.Add(value1,&nbsp;value2);</pre> </p> <p> What happens here is that <code>client.Add</code> creates and sends a SOAP message to a service, receives the response, unpacks it, and returns it as a <code>double</code> value. Even so, it looks just like any other method call. There's no <em>"premium on boundary crossings"</em> here. </p> <p> So much for the principle that boundaries are explicit. They're not, and it bothered me twenty years ago, as it bothers me today. </p> <p> I'll remind you what the problem is. When the boundary is <em>not</em> explicit, you may inadvertently write client code that makes network calls, and you may not be aware of it. This could noticeably slow down the application, particularly if you do it in a loop. </p> <h3 id="55bd772540a047a3b8db0d1aee373e87"> How do you make boundaries explicit? <a href="#55bd772540a047a3b8db0d1aee373e87">#</a> </h3> <p> This problem isn't isolated to WCF or SOAP. <a href="https://en.wikipedia.org/wiki/Fallacies_of_distributed_computing">Network calls are slow and unreliable</a>. Perhaps you're connecting to a system on the other side of the Earth. Perhaps the system is unavailable. This is true regardless of protocol. </p> <p> From the software architect's perspective, the tenet that boundaries are explicit is a really good idea. The clearer it is where in a code base network operations take place, the easier it is to troubleshot and maintain that code. This could make it easier to spot <em>n + 1</em> problems, as well as give you opportunities to <a href="/2020/03/23/repeatable-execution">add logging</a>, <a href="https://martinfowler.com/bliki/CircuitBreaker.html">Circuit Breakers</a>, etc. </p> <p> How do you make boundaries explicit? Clearly, WCF failed to do so, despite the design goal. </p> <h3 id="3c69e9213db946dc8d389c9b4bf19de2"> Only Commands <a href="#3c69e9213db946dc8d389c9b4bf19de2">#</a> </h3> <p> After having struggled with this question for years, I had an idea. This idea, however, doesn't really work, but I'll briefly cover it here. After all, if I can have that idea, other people may get it as well. It could save you some time if I explain why I believe that it doesn't address the problem. </p> <p> The idea is to mandate that all network operations are <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Commands</a>. In a C-like language, that would indicate a <code>void</code> method. </p> <p> While it turns out that it ultimately doesn't work, this isn't just some arbitrary rule that I've invented. After all, if a method doesn't return anything, the boundary does, in a sense, become explicit. You can't just 'keep dotting', <a href="https://martinfowler.com/bliki/FluentInterface.html">fluent-interface</a> style. </p> <p> <pre>channel.UpdateProduct(pc);</pre> </p> <p> This gives you the opportunity to treat network operations as fire-and-forget operations. While you could still run such Commands in a tight loop, you could at least add them to a queue and move on. Such a queue could be be an in-process data structure, or a persistent queue. Your network card also holds a small queue of network packets. </p> <p> This is essentially an asynchronous messaging architecture. It seems to correlate with Don Box's talk about messages. </p> <p> Although this may seem as though it addresses some concerns about making boundaries explicit, an obvious question arises: How do you perform queries in this model? </p> <p> You <em>could</em> keep such an architecture clean. You might, for example, implement a <a href="https://martinfowler.com/bliki/CQRS.html">CQRS</a> architecture where Commands create Events for which your application may subscribe. Such events could be handled by <em>event handlers</em> (other <code>void</code> methods) to update in-memory data as it changes. </p> <p> Even so, there are practical limitations with such a model. What's likely to happen, instead, is the following. </p> <h3 id="04a7c349122e45e38341eb0b50b877c0"> Request-Reply <a href="#04a7c349122e45e38341eb0b50b877c0">#</a> </h3> <p> It's hardly unlikely that you may need to perform some kind of Query against a remote system. If you can only communicate with services using <code>void</code> methods, such a scenario seems impossible. </p> <p> It's not. There's even a pattern for that. <a href="/ref/eip">Enterprise Integration Patterns</a> call it <a href="https://www.enterpriseintegrationpatterns.com/patterns/messaging/RequestReply.html">Request-Reply</a>. You create a Query message and give it a correlation ID, send it, and wait for the reply message to arrive at your own <em>message handler</em>. Client code might look like this: </p> <p> <pre>var&nbsp;correlationId&nbsp;=&nbsp;Guid.NewGuid(); var&nbsp;query&nbsp;=&nbsp;new&nbsp;FavouriteSongsQuery(UserId:&nbsp;123,&nbsp;correlationId); channel.Send(query); IReadOnlyCollection&lt;Song&gt;&nbsp;songs&nbsp;=&nbsp;[]; while&nbsp;(true) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;response&nbsp;=&nbsp;subscriber.GetNextResponse(correlationId); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(response&nbsp;is&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.Sleep(100); &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;songs&nbsp;=&nbsp;response; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break; }</pre> </p> <p> While this works, it's awkward to use, so it doesn't take long before someone decides to wrap it in a helpful helper method: </p> <p> <pre>public&nbsp;IReadOnlyCollection&lt;Song&gt;&nbsp;GetFavouriteSongs(int&nbsp;userId) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;correlationId&nbsp;=&nbsp;Guid.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;query&nbsp;=&nbsp;new&nbsp;FavouriteSongsQuery(userId,&nbsp;correlationId); &nbsp;&nbsp;&nbsp;&nbsp;channel.Send(query); &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;Song&gt;&nbsp;songs&nbsp;=&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(true) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;response&nbsp;=&nbsp;subscriber.GetNextResponse(correlationId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(response&nbsp;is&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.Sleep(100); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;songs&nbsp;=&nbsp;response; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;songs; }</pre> </p> <p> This now enables you to write client code like this: </p> <p> <pre>var&nbsp;songService&nbsp;=&nbsp;new&nbsp;SongService(); var&nbsp;songs&nbsp;=&nbsp;songService.GetFavouriteSongs(123);</pre> </p> <p> We're back where we started. Boundaries are no longer explicit. Equivalent to how <a href="/2020/11/23/good-names-are-skin-deep">good names are only skin-deep</a>, this attempt to make boundaries explicit can't resist programmers' natural tendency to make things easier for themselves. </p> <p> If only there was some way to make an abstraction <a href="https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/">contagious</a>... </p> <h3 id="fffc782323e746ef9a7b662132e17257"> Contagion <a href="#fffc782323e746ef9a7b662132e17257">#</a> </h3> <p> Ideally, we'd like to make boundaries explicit in such a way that they can't be hidden away. After all, </p> <blockquote> <p> "Abstraction is <em>the elimination of the irrelevant and the amplification of the essential.</em>" </p> <footer><cite>Robert C. Martin, <a href="/ref/doocautbm">Designing Object-Oriented C++ Applications Using The Booch Method</a>, chapter 00 (sic), (his emphasis)</cite></footer> </blockquote> <p> The existence of a boundary is essential, so while we might want to eliminate various other irrelevant details, this is a property that we should retain and surface in APIs. Even better, it'd be best if we could do it in such a way that it can't easily be swept under the rug, as shown above. </p> <p> In <a href="https://www.haskell.org/">Haskell</a>, this is true for all input/output - not only network requests, but also file access, and other non-deterministic actions. In Haskell this is a 'wrapper' type called <code>IO</code>; for an explanation with C# examples, see <a href="/2020/06/08/the-io-container">The IO Container</a>. </p> <p> In a more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> way, we can use <a href="/2020/07/27/task-asynchronous-programming-as-an-io-surrogate">task asynchronous programming as an IO surrogate</a>. People often complain that <code>async</code> code is contagious. By that they mean that once a piece of code is asynchronous, the caller must also be asynchronous. This effect is transitive, and while this is often lamented as a problem, this is exactly what we need. Amplify the essential. Make boundaries explicit. </p> <p> This doesn't mean that your entire code base has to be asynchronous. Only your network (and similar, non-deterministic) code should be asynchronous. Write your Domain Model and application code as pure functions, and <a href="/2019/02/11/asynchronous-injection">compose them with the asynchronous code using standard combinators</a>. </p> <h3 id="16ff65dbc4784ad1939257635d08039c"> Conclusion <a href="#16ff65dbc4784ad1939257635d08039c">#</a> </h3> <p> The first of Don Box's four tenets of SOA is that boundaries should be explicit. WCF failed to deliver on that ideal, and it took me more than a decade to figure out how to square that circle. </p> <p> Many languages now come with support for asynchronous programming, often utilizing some kind of generic <code>Task</code> or <code>Async</code> <a href="/2022/03/28/monads">monad</a>. Since such types are usually contagious, you can use them to make boundaries explicit. </p> <p> <strong>Next:</strong> <a href="/2024/03/25/services-are-autonomous">Services are autonomous</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The four tenets of SOA revisited https://blog.ploeh.dk/2024/03/04/the-four-tenets-of-soa-revisited 2024-03-04T06:39:00+00:00 Mark Seemann <div id="post"> <p> <em>Twenty years after.</em> </p> <p> In the <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/january/msdn-magazine-january-2004">January 2004 issue of MSDN Magazine</a> you can find an article by <a href="https://en.wikipedia.org/wiki/Don_Box">Don Box</a> titled <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/january/a-guide-to-developing-and-running-connected-systems-with-indigo">A Guide to Developing and Running Connected Systems with Indigo</a>. Buried within the (now dated) discussion of the technology code-named <em>Indigo</em> (later <a href="https://en.wikipedia.org/wiki/Windows_Communication_Foundation">Windows Communication Foundation</a>) you can find a general discussion of <em>four tenets of service-oriented architecture</em> (SOA). </p> <p> I remember that they resonated strongly with me back then, or that they at least prompted me to think explicitly about how to design software services. Some of these ideas have stayed with me ever since, while another has nagged at me for decades before I found a way to reconcile it with other principles of software design. </p> <p> Now that it's twenty years ago that the MSDN article was published, I find that this is as good a time as ever to revisit it. </p> <h3 id="96e92c4bccef4d5789bbb5d860e3ce3f"> Legacy <a href="#96e92c4bccef4d5789bbb5d860e3ce3f">#</a> </h3> <p> Why should we care about an old article about <a href="https://en.wikipedia.org/wiki/SOAP">SOAP</a> and SOA? Does anyone even use such things today, apart from legacy systems? </p> <p> After all, we've moved on from SOAP to <a href="https://en.wikipedia.org/wiki/REST">REST</a>, <a href="https://en.wikipedia.org/wiki/GRPC">gRPC</a>, or <a href="https://en.wikipedia.org/wiki/GraphQL">GraphQL</a>, and from SOA to <a href="https://en.wikipedia.org/wiki/Microservices">microservices</a> - that is, if we're not already swinging back towards monoliths. </p> <p> Even so, I find much of what Don Box wrote twenty years ago surprisingly prescient. If you're interested in distributed software design involving some kind of remote API design, the four tenets of service-orientation apply beyond their original context. Some of the ideas, at least. </p> <p> As is often the case in our field, various resources discuss the tenets without much regard to proper citation. Thus, I can't be sure that the MSDN article is where they first appeared, but I haven't found any earlier source. </p> <p> My motivation for writing these article is partly to rescue the four tenets from obscurity, and partly to add some of my own commentary. </p> <p> Much of the original article is about Indigo, and I'm going to skip that. On the other hand, I'm going to quote rather extensively from the article, in order to lift the more universal ideas out of their original context. </p> <p> I'll do that in a series of articles, each covering one of the tenets. </p> <ul> <li><a href="/2024/03/11/boundaries-are-explicit">Boundaries are explicit</a></li> <li><a href="/2024/03/25/services-are-autonomous">Services are autonomous</a></li> <li><a href="/2024/04/15/services-share-schema-and-contract-not-class">Services share schema and contract, not class</a></li> <li><a href="/2024/04/29/service-compatibility-is-determined-based-on-policy">Service compatibility is determined based on policy</a></li> </ul> <p> Not all of the tenets have stood the test of time equally well, so I may not add an equal amount of commentary to all four. </p> <h3 id="ad6f66b0ac954647bebf4d288939d2ab"> Conclusion <a href="#ad6f66b0ac954647bebf4d288939d2ab">#</a> </h3> <p> Ever since I first encountered the four tenets of SOA, they've stayed with me in one form or other. When helping teams to design services, even what we may today consider 'modern services', I've drawn on some of those ideas. There are insights of a general nature that are worth considering even today. </p> <p> <strong>Next:</strong> <a href="/2024/03/11/boundaries-are-explicit">Boundaries are explicit</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Testing exceptions https://blog.ploeh.dk/2024/02/26/testing-exceptions 2024-02-26T06:47:00+00:00 Mark Seemann <div id="post"> <p> <em>Some thoughts on testing past the happy path.</em> </p> <p> Test-driven development is a great development technique that enables you to get rapid feedback on design and implementation ideas. It enables you to rapidly move towards a working solution. </p> <p> The emphasis on the <em>happy path</em>, however, can make you forget about all the things that may go wrong. Sooner or later, though, you may realize that the implementation can fail for a number of reasons, and, wanting to make things more robust, you may want to also subject your error-handling code to automated testing. </p> <p> This doesn't have to be difficult, but can raise some interesting questions. In this article, I'll try to address a few. </p> <h3 id="ead73eb4bc4b45eba0bde0cf61269814"> Throwing exceptions with a dynamic mock <a href="#ead73eb4bc4b45eba0bde0cf61269814">#</a> </h3> <p> In <a href="/2023/08/14/replacing-mock-and-stub-with-a-fake#0afe67b375254fe193a3fd10234a1ce9">a question to another article</a> AmirB asks how to use a <a href="http://xunitpatterns.com/Fake%20Object.html">Fake Object</a> to test exceptions. Specifically, since <a href="/2023/11/13/fakes-are-test-doubles-with-contracts">a Fake is a Test Double with a coherent contract</a> it'll be inappropriate to let it throw exceptions that relate to different implementations. </p> <p> Egads, that was quite abstract, so let's consider a concrete example. </p> <p> <a href="/2023/08/14/replacing-mock-and-stub-with-a-fake">The original article</a> that AmirB asked about used this interface as an example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;<span style="font-weight:bold;color:#74531f;">Read</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>); }</pre> </p> <p> Granted, this interface is a little odd, but it should be good enough for the present purpose. As AmirB wrote: </p> <blockquote> <p> "In scenarios where dynamic mocks (like Moq) are employed, we can mock a method to throw an exception, allowing us to test the expected behavior of the System Under Test (SUT)." </p> <footer><cite><a href="/2023/08/14/replacing-mock-and-stub-with-a-fake#0afe67b375254fe193a3fd10234a1ce9">AmirB</a></cite></footer> </blockquote> <p> Specifically, this might look like this, using <a href="https://github.com/devlooped/moq">Moq</a>: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">CreateThrows</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">td</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IUserRepository&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;td.Setup(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Read(1234)).Returns(<span style="color:blue;">new</span>&nbsp;User&nbsp;{&nbsp;Id&nbsp;=&nbsp;0&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;td.Setup(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Create(It.IsAny&lt;<span style="color:blue;">int</span>&gt;())).Throws(MakeSqlException()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SomeController(td.Object); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.GetUser(1234); &nbsp;&nbsp;&nbsp;&nbsp;Assert.NotNull(actual); }</pre> </p> <p> It's just an example, but the point is that since you can make a dynamic mock do anything that you can define in code, you can also use it to simulate database exceptions. This test pretends that the <code>IUserRepository</code> throws a <a href="https://learn.microsoft.com/dotnet/api/system.data.sqlclient.sqlexception">SqlException</a> from the <code>Create</code> method. </p> <p> Perhaps the <code>GetUser</code> implementation now looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;User&nbsp;<span style="font-weight:bold;color:#74531f;">GetUser</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">u</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.userRepository.Read(userId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(u.Id&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.userRepository.Create(userId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">catch</span>&nbsp;(SqlException) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;u; }</pre> </p> <p> If you find the example contrived, I don't blame you. The <code>IUserRepository</code> interface, the <code>User</code> class, and the <code>GetUser</code> method that orchestrates them are all degenerate in various ways. I originally created this little code example to discuss <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">data flow verification</a>, and I'm now stretching it beyond any reason. I hope that you can look past that. The point I'm making here is more general, and doesn't hinge on particulars. </p> <h3 id="ed58e2e387234a7ebd3c97a384841d9f"> Fake <a href="#ed58e2e387234a7ebd3c97a384841d9f">#</a> </h3> <p> <a href="/2023/08/14/replacing-mock-and-stub-with-a-fake">The article</a> also suggests a <code>FakeUserRepository</code> that is small enough that I can repeat it here. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeUserRepository</span>&nbsp;:&nbsp;Collection&lt;User&gt;,&nbsp;IUserRepository { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;User&nbsp;{&nbsp;Id&nbsp;=&nbsp;userId&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;User&nbsp;<span style="font-weight:bold;color:#74531f;">Read</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">user</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.SingleOrDefault(<span style="font-weight:bold;color:#1f377f;">u</span>&nbsp;=&gt;&nbsp;u.Id&nbsp;==&nbsp;userId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(user&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;User&nbsp;{&nbsp;Id&nbsp;=&nbsp;0&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;user; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The question is how to use something like this when you want to test exceptions? It's possible that this little class may produce <a href="/2024/01/29/error-categories-and-category-errors">errors that I've failed to predict</a>, but it certainly doesn't throw any <code>SqlExceptions</code>! </p> <p> Should we inflate <code>FakeUserRepository</code> by somehow also giving it the capability to throw particular exceptions? </p> <h3 id="c116b036864348b7938dfb6805e0c2dd"> Throwing exceptions from Test Doubles <a href="#c116b036864348b7938dfb6805e0c2dd">#</a> </h3> <p> I understand why AmirB asks that question, because it doesn't seem right. As a start, it would go against the <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>. The <code>FakeUserRepository</code> would then have more than reason to change: You'd have to change it if the <code>IUserRepository</code> interface changes, but you'd also have to change it if you wanted to simulate a different error situation. </p> <p> Good coding practices apply to test code as well. Test code is code that you have to read and maintain, so all the good practices that keep production code in good shape also apply to test code. This may include <a href="https://en.wikipedia.org/wiki/SOLID">the SOLID principles</a>, unless you're of the mind that <a href="https://dannorth.net/cupid-for-joyful-coding/">SOLID ought to be a thing of the past</a>. </p> <p> If you really <em>must</em> throw exceptions from a <a href="https://martinfowler.com/bliki/TestDouble.html">Test Double</a>, perhaps a dynamic mock object as shown above is the best option. No-one says that if you use a Fake Object for most of your tests you can't use a dynamic mock library for truly one-off test cases.Or perhaps a one-off Test Double that throws the desired exception. </p> <p> I would, however, consider it a code smell if this happens too often. Not a test smell, but a code smell. </p> <h3 id="302a9e4462744a55974b8fdab6f70054"> Is the exception part of the contract? <a href="#302a9e4462744a55974b8fdab6f70054">#</a> </h3> <p> You may ask yourself whether a particular exception type is part of an object's contract. As I always do, when I use the word <em>contract</em>, I refer to a set of invariants, pre-, and postconditions, taking a cue from <a href="/ref/oosc">Object-Oriented Software Construction</a>. See also my video course <a href="/encapsulation-and-solid">Encapsulation and SOLID</a> for more details. </p> <p> You can <em>imply</em> many things about a contract when you have a static type system at your disposal, but there are always rules that you can't express that way. Parts of a contract are implicitly understood, or communicated in other ways. Code comments, <a href="https://en.wikipedia.org/wiki/Docstring">docstrings</a>, or similar, are good options. </p> <p> What may you infer from the <code>IUserRepository</code> interface? What should you <em>not</em> infer? </p> <p> I'd expect the <code>Read</code> method to return a <code>User</code> object. This code example hails us <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">from 2013</a>, before C# had <a href="https://learn.microsoft.com/dotnet/csharp/nullable-references">nullable reference types</a>. Around that time I'd begun using <a href="/2018/03/26/the-maybe-functor">Maybe</a> to signal that the return value might be missing. This is a <em>convention</em>, so the reader needs to be aware of it in order to correctly infer that part of the contract. Since the <code>Read</code> method does <em>not</em> return <code>Maybe&lt;User&gt;</code> I might infer that a non-null <code>User</code> object is guaranteed; that's a post-condition. </p> <p> These days, I'd also use <a href="/2020/07/27/task-asynchronous-programming-as-an-io-surrogate">asynchronous APIs to hint that I/O is involved</a>, but again, the example is so old and simplified that this isn't the case here. Still, regardless of how this is communicated to the reader, if an interface (or base class) is intended for I/O, we may expect it to fail at times. In most languages, such errors manifest as exceptions. </p> <p> At least two questions arise from such deliberations: </p> <ul> <li>Which exception types may the methods throw?</li> <li>Can you even handle such exceptions?</li> </ul> <p> Should <code>SqlException</code> even be part of the contract? Isn't that an implementation detail? </p> <p> The <code>FakeUserRepository</code> class neither uses SQL Server nor throws <code>SqlExceptions</code>. You may imagine other implementations that use a document database, or even just another relational database than SQL Server (Oracle, MySQL, PostgreSQL, etc.). Those wouldn't throw <code>SqlExceptions</code>, but perhaps other exception types. </p> <p> According to the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>, </p> <blockquote> <p> "Abstractions should not depend upon details. Details should depend upon abstractions." </p> <footer><cite>Robert C. Martin, <a href="/ref/appp">Agile Principles, Patterns, and Practices in C#</a></cite></footer> </blockquote> <p> If we make <code>SqlException</code> part of the contract, an implementation detail becomes part of the contract. Not only that: With an implementation like the above <code>GetUser</code> method, which catches <code>SqlException</code>, we've also violated the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>. If you injected another implementation, one that throws a different type of exception, the code would no longer work as intended. </p> <p> Loosely coupled code shouldn't look like that. </p> <p> Many specific exceptions are of <a href="/2024/01/29/error-categories-and-category-errors">a kind that you can't handle anyway</a>. On the other hand, if you do decide to handle particular error scenarios, make it part of the contract, or, as Michael Feathers puts it, <a href="https://youtu.be/AnZ0uTOerUI?si=1gJXYFoVlNTSbjEt">extend the domain</a>. </p> <h3 id="ed86f41415724219a0afbf9d669ec1b7"> Integration testing <a href="#ed86f41415724219a0afbf9d669ec1b7">#</a> </h3> <p> How should we unit test specific exception? <a href="https://en.wikipedia.org/wiki/Mu_(negative)">Mu</a>, we shouldn't. </p> <blockquote> <p> "Personally, I avoid using try-catch blocks in repositories or controllers and prefer handling exceptions in middleware (e.g., ErrorHandler). In such cases, I write separate unit tests for the middleware. Could this be a more fitting approach?" </p> <footer><cite><a href="/2023/08/14/replacing-mock-and-stub-with-a-fake#0afe67b375254fe193a3fd10234a1ce9">AmirB</a></cite></footer> </blockquote> <p> That is, I think, an excellent approach to those exceptions that that you've decided to not handle explicitly. Such middleware would typically log or otherwise notify operators that a problem has arisen. You could also write some general-purpose middleware that performs retries or implements the <a href="https://martinfowler.com/bliki/CircuitBreaker.html">Circuit Breaker</a> pattern, but reusable libraries that do that already exist. Consider using one. </p> <p> Still, you may have decided to implement a particular feature by leveraging a capability of a particular piece of technology, and the code you intent to write is complicated enough, or important enough, that you'd like to have good test coverage. How do you do that? </p> <p> I'd suggest an integration test. </p> <p> I don't have a good example lying around that involves throwing specific exceptions, but something similar may be of service. The example code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> pretends to be an online restaurant reservation system. Two concurrent clients may compete for the last table on a particular date; a typical race condition. </p> <p> There are more than one way to address such a concern. As implied in <a href="/2024/01/29/error-categories-and-category-errors">a previous article</a>, you may decide to rearchitect the entire application to be able to handle such edge cases in a robust manner. For the purposes of the book's example code base, however, I considered a <em>lock-free architecture</em> out of scope. Instead, I had in mind dealing with that issue by taking advantage of .NET and SQL Server's support for lightweight transactions via a <a href="https://learn.microsoft.com/dotnet/api/system.transactions.transactionscope">TransactionScope</a>. While this is a handy solution, it's utterly dependent on the technology stack. It's a good example of an implementation detail that I'd rather not expose to a unit test. </p> <p> Instead, I wrote a <a href="/2021/01/25/self-hosted-integration-tests-in-aspnet">self-hosted integration test</a> that runs against a real SQL Server instance (automatically deployed and configured on demand). It tests <em>behaviour</em> rather than implementation details: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">NoOverbookingRace</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">start</span>&nbsp;=&nbsp;DateTimeOffset.UtcNow; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeOut</span>&nbsp;=&nbsp;TimeSpan.FromSeconds(30); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(DateTimeOffset.UtcNow&nbsp;-&nbsp;start&nbsp;&lt;&nbsp;timeOut) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostTwoConcurrentLiminalReservations(start.DateTime.AddDays(++i)); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostTwoConcurrentLiminalReservations</span>(DateTime&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) { &nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;=&nbsp;date.Date.AddHours(18.5); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">service</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantService(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">task1</span>&nbsp;=&nbsp;service.PostReservation(<span style="color:blue;">new</span>&nbsp;ReservationDtoBuilder() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithDate(date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(10) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">task2</span>&nbsp;=&nbsp;service.PostReservation(<span style="color:blue;">new</span>&nbsp;ReservationDtoBuilder() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithDate(date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(10) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Task.WhenAll(task1,&nbsp;task2); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Single(actual,&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&gt;&nbsp;msg.IsSuccessStatusCode); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Single( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&gt;&nbsp;msg.StatusCode&nbsp;==&nbsp;HttpStatusCode.InternalServerError); }</pre> </p> <p> This test attempts to make two concurrent reservations for ten people. This is also the maximum capacity of the restaurant: It's impossible to seat twenty people. We'd like for one of the requests to win that race, while the server should reject the loser. </p> <p> This test is only concerned with the behaviour that clients can observe, and since this code base contains hundreds of other tests that inspect HTTP response messages, this one only looks at the status codes. </p> <p> The implementation handles the potential overbooking scenario like this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">TryCreate</span>(Restaurant&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>,&nbsp;Reservation&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">scope</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TransactionScope(TransactionScopeAsyncFlowOption.Enabled); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(restaurant.Id,&nbsp;reservation.At); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">now</span>&nbsp;=&nbsp;Clock.GetCurrentDateTime(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!restaurant.MaitreD.WillAccept(now,&nbsp;reservations,&nbsp;reservation)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;NoTables500InternalServerError(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(restaurant.Id,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;scope.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Reservation201Created(restaurant.Id,&nbsp;reservation); }</pre> </p> <p> Notice the <code>TransactionScope</code>. </p> <p> I'm under the illusion that I could radically change this implementation detail without breaking the above test. Granted, unlike <a href="/2023/09/04/decomposing-ctfiyhs-sample-code-base">another experiment</a>, this hypothesis isn't one I've put to the test. </p> <h3 id="9659f21863e74c288d5c2d36534eaa37"> Conclusion <a href="#9659f21863e74c288d5c2d36534eaa37">#</a> </h3> <p> How does one automatically test error branches? Most unit testing frameworks come with APIs that makes it easy to verify that specific exceptions were thrown, so that's not the hard part. If a particular exception is part of the System Under Test's contract, just test it like that. </p> <p> On the other hand, when it comes to objects composed with other objects, implementation details may easily leak through in the shape of specific exception types. I'd think twice before writing a test that verifies whether a piece of client code (such as the above <code>SomeController</code>) handles a particular exception type (such as <code>SqlException</code>). </p> <p> If such a test is difficult to write because all you have is a Fake Object (e.g. <code>FakeUserRepository</code>), that's only good. The rapid feedback furnished by test-driven development strikes again. Listen to your tests. </p> <p> You should probably not write that test at all, because there seems to be an issue with the planned structure of the code. Address <em>that</em> problem instead. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Extracting data from a small CSV file with Haskell https://blog.ploeh.dk/2024/02/19/extracting-data-from-a-small-csv-file-with-haskell 2024-02-19T12:57:00+00:00 Mark Seemann <div id="post"> <p> <em>Statically typed languages are also good for ad-hoc scripting.</em> </p> <p> This article is part of a <a href="/2024/02/05/statically-and-dynamically-typed-scripts">short series of articles</a> that compares ad-hoc scripting in <a href="https://www.haskell.org/">Haskell</a> with solving the same problem in <a href="https://www.python.org/">Python</a>. The <a href="/2024/02/05/statically-and-dynamically-typed-scripts">introductory article</a> describes the problem to be solved, so here I'll jump straight into the Haskell code. In the next article I'll give a similar walkthrough of my Python script. </p> <h3 id="0a705367eb2f4080ac168eb1bbe9b2ec"> Getting started <a href="#0a705367eb2f4080ac168eb1bbe9b2ec">#</a> </h3> <p> When working with Haskell for more than a few true one-off expressions that I can type into GHCi (the Haskell <a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>), I usually create a module file. Since I'd been asked to crunch some data, and I wasn't feeling very imaginative that day, I just named the module (and the file) <code>Crunch</code>. After some iterative exploration of the problem, I also arrived at a set of imports: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Crunch&nbsp;<span style="color:blue;">where</span> <span style="color:blue;">import</span>&nbsp;Data.List&nbsp;(<span style="color:#2b91af;">sort</span>) <span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.List.NonEmpty&nbsp;<span style="color:blue;">as</span>&nbsp;NE <span style="color:blue;">import</span>&nbsp;Data.List.Split <span style="color:blue;">import</span>&nbsp;Control.Applicative <span style="color:blue;">import</span>&nbsp;Control.Monad <span style="color:blue;">import</span>&nbsp;Data.Foldable</pre> </p> <p> As we go along, you'll see where some of these fit in. </p> <p> Reading the actual data file, however, can be done with just the Haskell <code>Prelude</code>: </p> <p> <pre>inputLines&nbsp;=&nbsp;<span style="color:blue;">words</span>&nbsp;&lt;$&gt;&nbsp;<span style="color:blue;">readFile</span>&nbsp;<span style="color:#a31515;">&quot;survey_data.csv&quot;</span></pre> </p> <p> Already now, it's possible to load the module in GHCi and start examining the data: </p> <p> <pre>ghci&gt; :l Crunch.hs [1 of 1] Compiling Crunch ( Crunch.hs, interpreted ) Ok, one module loaded. ghci&gt; length &lt;$&gt; inputLines 38</pre> </p> <p> Looks good, but reading a text file is hardly the difficult part. The first obstacle, surprisingly, is to split comma-separated values into individual parts. For some reason that I've never understood, the Haskell base library doesn't even include something as basic as <a href="https://learn.microsoft.com/dotnet/api/system.string.split">String.Split</a> from .NET. I could probably hack together a function that does that, but on the other hand, it's available in the <a href="https://hackage.haskell.org/package/split/docs/Data-List-Split.html">split</a> package; that explains the <code>Data.List.Split</code> import. It's just a bit of a bother that one has to pull in another package only to do that. </p> <h3 id="a70030690c1645a2b0923ad354fa665b"> Grades <a href="#a70030690c1645a2b0923ad354fa665b">#</a> </h3> <p> Extracting all the grades are now relatively easy. This function extracts and parses a grade from a single line: </p> <p> <pre><span style="color:#2b91af;">grade</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Read</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a grade&nbsp;line&nbsp;=&nbsp;<span style="color:blue;">read</span>&nbsp;$&nbsp;splitOn&nbsp;<span style="color:#a31515;">&quot;,&quot;</span>&nbsp;line&nbsp;!!&nbsp;2</pre> </p> <p> It splits the line on commas, picks the third element (zero-indexed, of course, so element <code>2</code>), and finally parses it. </p> <p> One may experiment with it in GHCi to get an impression that it works: </p> <p> <pre>ghci&gt; fmap grade &lt;$&gt; inputLines :: IO [Int] [2,2,12,10,4,12,2,7,2,2,2,7,2,7,2,4,2,7,4,7,0,4,0,7,2,2,2,2,2,2,4,4,2,7,4,0,7,2]</pre> </p> <p> This lists all 38 expected grades found in the data file. </p> <p> In the <a href="/2024/02/05/statically-and-dynamically-typed-scripts">introduction article</a> I spent some time explaining how languages with strong type inference don't need type declarations. This makes iterative development easier, because you can fiddle with an expression until it does what you'd like it to do. When you change an expression, often the inferred type changes as well, but there's no programmer overhead involved with that. The compiler figures that out for you. </p> <p> Even so, the above <code>grade</code> function does have a type annotation. How does that gel with what I just wrote? </p> <p> It doesn't, on the surface, but when I was fiddling with the code, there was no type annotation. The Haskell compiler is perfectly happy to infer the type of an expression like </p> <p> <pre>grade&nbsp;line&nbsp;=&nbsp;<span style="color:blue;">read</span>&nbsp;$&nbsp;splitOn&nbsp;<span style="color:#a31515;">&quot;,&quot;</span>&nbsp;line&nbsp;!!&nbsp;2</pre> </p> <p> The human reader, however, is not so clever (I'm not, at least), so once a particular expression settles, and I'm fairly sure that it's not going to change further, I sometimes add the type annotation to aid myself. </p> <p> When writing this, I was debating the didactics of showing the function <em>with</em> the type annotation, against showing it without it. Eventually I decided to include it, because it's more understandable that way. That decision, however, prompted this explanation. </p> <h3 id="754ea5fced264c439f8784705176851b"> Binomial choice <a href="#754ea5fced264c439f8784705176851b">#</a> </h3> <p> The next thing I needed to do was to list all pairs from the data file. Usually, <a href="/2022/01/17/enumerate-wordle-combinations-with-an-applicative-functor">when I run into a problem related to combinations, I reach for applicative composition</a>. For example, to list all possible combinations of the first three primes, I might do this: </p> <p> <pre>ghci&gt; liftA2 (,) [2,3,5] [2,3,5] [(2,2),(2,3),(2,5),(3,2),(3,3),(3,5),(5,2),(5,3),(5,5)]</pre> </p> <p> You may now protest that this is sampling with replacement, whereas the task is to pick two <em>different</em> rows from the data file. Usually, when I run into that requirement, I just remove the ones that pick the same value twice: </p> <p> <pre>ghci&gt; filter (uncurry (/=)) $ liftA2 (,) [2,3,5] [2,3,5] [(2,3),(2,5),(3,2),(3,5),(5,2),(5,3)]</pre> </p> <p> That works great as long as the values are unique, but what if that's not the case? </p> <p> <pre>ghci&gt; liftA2 (,) "foo" "foo" [('f','f'),('f','o'),('f','o'),('o','f'),('o','o'),('o','o'),('o','f'),('o','o'),('o','o')] ghci&gt; filter (uncurry (/=)) $ liftA2 (,) "foo" "foo" [('f','o'),('f','o'),('o','f'),('o','f')]</pre> </p> <p> This removes too many values! We don't want the combinations where the first <code>o</code> is paired with itself, or when the second <code>o</code> is paired with itself, but we <em>do</em> want the combination where the first <code>o</code> is paired with the second, and vice versa. </p> <p> This is relevant because the data set turns out to contain identical rows. Thus, I needed something that would deal with that issue. </p> <p> Now, bear with me, because it's quite possible that what i did do isn't the simplest solution to the problem. On the other hand, I'm reporting what I did, and how I used Haskell to solve a one-off problem. If you have a simpler solution, please <a href="https://github.com/ploeh/ploeh.github.com?tab=readme-ov-file#comments">leave a comment</a>. </p> <p> You often reach for the tool that you already know, so I used a variation of the above. Instead of combining values, I decided to combine row indices instead. This meant that I needed a function that would produce the indices for a particular list: </p> <p> <pre><span style="color:#2b91af;">indices</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;t&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;t&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:#2b91af;">Int</span>] indices&nbsp;f&nbsp;=&nbsp;[0&nbsp;..&nbsp;<span style="color:blue;">length</span>&nbsp;f&nbsp;-&nbsp;1]</pre> </p> <p> Again, the type annotation came later. This just produces sequential numbers, starting from zero: </p> <p> <pre>ghci&gt; indices &lt;$&gt; inputLines [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37]</pre> </p> <p> Such a function hovers just around the <a href="https://wiki.haskell.org/Fairbairn_threshold">Fairbairn threshold</a>; some experienced Haskellers would probably just inline it. </p> <p> Since row numbers (indices) are unique, the above approach to binomial choice works, so I also added a function for that: </p> <p> <pre><span style="color:#2b91af;">choices</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Eq</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[(a,&nbsp;a)] choices&nbsp;=&nbsp;<span style="color:blue;">filter</span>&nbsp;(<span style="color:blue;">uncurry</span>&nbsp;<span style="color:#2b91af;">(/=)</span>)&nbsp;.&nbsp;join&nbsp;(liftA2&nbsp;<span style="color:#2b91af;">(,)</span>)</pre> </p> <p> Combined with <code>indices</code> I can now enumerate all combinations of two rows in the data set: </p> <p> <pre>ghci&gt; choices . indices &lt;$&gt; inputLines [(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),(0,7),(0,8),(0,9),...</pre> </p> <p> I'm only showing the first ten results here, because in reality, there are <em>1406</em> such pairs. </p> <p> Perhaps you think that all of this seems quite elaborate, but so far it's only four lines of code. The reason it looks like more is because I've gone to some lengths to explain what the code does. </p> <h3 id="bf2d1d52fb3c4a29b6987840b2d46530"> Sum of grades <a href="#bf2d1d52fb3c4a29b6987840b2d46530">#</a> </h3> <p> The above combinations are pairs of <em>indices</em>, not values. What I need is to use each index to look up the row, from the row get the grade, and then sum the two grades. The first parts of that I can accomplish with the <code>grade</code> function, but I need to do if for every row, and for both elements of each pair. </p> <p> While tuples are <code>Functor</code> instances, they only map over the second element, and that's not what I need: </p> <p> <pre>ghci&gt; rows = ["foo", "bar", "baz"] ghci&gt; fmap (rows!!) &lt;$&gt; [(0,1),(0,2)] [(0,"bar"),(0,"baz")]</pre> </p> <p> While this is just a simple example that maps over the two pairs <code>(0,1)</code> and <code>(0,2)</code>, it illustrates the problem: It only finds the row for each tuple's second element, but I need it for both. </p> <p> On the other hand, a type like <code>(a, a)</code> gives rise to a <a href="/2018/03/22/functors">functor</a>, and while a wrapper type like that is not readily available in the <em>base</em> library, defining one is a one-liner: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;Pair&nbsp;a&nbsp;=&nbsp;Pair&nbsp;{&nbsp;unPair&nbsp;::&nbsp;(a,&nbsp;a)&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Functor</span>)</pre> </p> <p> This enables me to map over pairs in one go: </p> <p> <pre>ghci&gt; unPair &lt;$&gt; fmap (rows!!) &lt;$&gt; Pair <$&gt; [(0,1),(0,2)] [("foo","bar"),("foo","baz")]</pre> </p> <p> This makes things a little easier. What remains is to use the <code>grade</code> function to look up the grade value for each row, then add the two numbers together, and finally count how many occurrences there are of each: </p> <p> <pre>sumGrades&nbsp;ls&nbsp;= &nbsp;&nbsp;liftA2&nbsp;<span style="color:#2b91af;">(,)</span>&nbsp;NE.<span style="color:blue;">head</span>&nbsp;<span style="color:blue;">length</span>&nbsp;&lt;$&gt;&nbsp;NE.group &nbsp;&nbsp;&nbsp;&nbsp;(sort&nbsp;(<span style="color:blue;">uncurry</span>&nbsp;<span style="color:#2b91af;">(+)</span>&nbsp;.&nbsp;unPair&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(grade&nbsp;.&nbsp;(ls&nbsp;!!))&nbsp;.&nbsp;Pair&nbsp;&lt;$&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;choices&nbsp;(indices&nbsp;ls)))</pre> </p> <p> You'll notice that this function doesn't have a type annotation, but we can ask GHCi if we're curious: </p> <p> <pre>ghci&gt; :t sumGrades sumGrades :: (Ord a, Num a, Read a) =&gt; [String] -&gt; [(a, Int)]</pre> </p> <p> This enabled me to get a count of each sum of grades: </p> <p> <pre>ghci&gt; sumGrades &lt;$&gt; inputLines [(0,6),(2,102),(4,314),(6,238),(7,48),(8,42),(9,272),(10,6), (11,112),(12,46),(14,138),(16,28),(17,16),(19,32),(22,4),(24,2)]</pre> </p> <p> The way to read this is that the sum <em>0</em> occurs six times, <em>2</em> appears <em>102</em> times, etc. </p> <p> There's one remaining task to accomplish before we can produce a PMF of the sum of grades: We need to enumerate the range, because, as it turns out, there are sums that are possible, but that don't appear in the data set. Can you spot which ones? </p> <p> Using tools already covered, it's easy to enumerate all possible sums: </p> <p> <pre>ghci&gt; import Data.List ghci&gt; sort $ nub $ (uncurry (+)) &lt;$&gt; join (liftA2 (,)) [-3,0,2,4,7,10,12] [-6,-3,-1,0,1,2,4,6,7,8,9,10,11,12,14,16,17,19,20,22,24]</pre> </p> <p> The sums <em>-6</em>, <em>-3</em>, <em>-1</em>, and more, are possible, but don't appear in the data set. Thus, in the PMF for two randomly picked grades, the probability that the sum is <em>-6</em> is <em>0</em>. On the other hand, the probability that the sum is <em>0</em> is <em>6/1406 ~ 0.004267</em>, and so on. </p> <h3 id="ddcac27fb3ff468cb9312f0fcc333865"> Difference of experience levels <a href="#ddcac27fb3ff468cb9312f0fcc333865">#</a> </h3> <p> The other question posed in the assignment was to produce the PMF for the absolute difference between two randomly selected students' experience levels. </p> <p> Answering that question follows the same mould as above. First, extract experience level from each data row, instead of the grade: </p> <p> <pre><span style="color:#2b91af;">experience</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Read</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a experience&nbsp;line&nbsp;=&nbsp;<span style="color:blue;">read</span>&nbsp;$&nbsp;splitOn&nbsp;<span style="color:#a31515;">&quot;,&quot;</span>&nbsp;line&nbsp;!!&nbsp;3</pre> </p> <p> Since I was doing an ad-hoc script, I just copied the <code>grade</code> function and changed the index from <code>2</code> to <code>3</code>. Enumerating the experience differences were also a close copy of <code>sumGrades</code>: </p> <p> <pre>diffExp&nbsp;ls&nbsp;= &nbsp;&nbsp;liftA2&nbsp;<span style="color:#2b91af;">(,)</span>&nbsp;NE.<span style="color:blue;">head</span>&nbsp;<span style="color:blue;">length</span>&nbsp;&lt;$&gt;&nbsp;NE.group &nbsp;&nbsp;&nbsp;&nbsp;(sort&nbsp;(<span style="color:blue;">abs</span>&nbsp;.&nbsp;<span style="color:blue;">uncurry</span>&nbsp;<span style="color:#2b91af;">(-)</span>&nbsp;.&nbsp;unPair&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(experience&nbsp;.&nbsp;(ls&nbsp;!!))&nbsp;.&nbsp;Pair&nbsp;&lt;$&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;choices&nbsp;(indices&nbsp;ls)))</pre> </p> <p> Running it in the REPL produces some other numbers, to be interpreted the same way as above: </p> <p> <pre>ghci&gt diffExp &lt;$&gt inputLines [(0,246),(1,472),(2,352),(3,224),(4,82),(5,24),(6,6)]</pre> </p> <p> This means that the difference <em>0</em> occurs <em>246</em> times, <em>1</em> appears <em>472</em> times, and so on. From those numbers, it's fairly simple to set up the PMF. </p> <h3 id="948660097f7748f2844ebfd91371b2a2"> Figures <a href="#948660097f7748f2844ebfd91371b2a2">#</a> </h3> <p> Another part of the assignment was to produce plots of both PMFs. I don't know how to produce figures with Haskell, and since the final results are just a handful of numbers each, I just copied them into a text editor to align them, and then pasted them into Excel to produce the figures there. </p> <p> Here's the PMF for the differences: </p> <p> <img src="/content/binary/difference-pmf-plot.png" alt="Bar chart of the differences PMF."> </p> <p> I originally created the figure with Danish labels. I'm sure that you can guess what <em>differens</em> means, and <em>sandsynlighed</em> means <em>probability</em>. </p> <h3 id="9b225242b63e4616b25954ad9141e273"> Conclusion <a href="#9b225242b63e4616b25954ad9141e273">#</a> </h3> <p> In this article you've seen the artefacts of an ad-hoc script to extract and analyze a small data set. While I've spent quite a few words to explain what's going on, the entire <code>Crunch</code> module is only 34 lines of code. Add to that a few ephemeral queries done directly in GHCi, but never saved to a file. It's been some months since I wrote the code, but as far as I recall, it took me a few hours all in all. </p> <p> If you do stuff like this every day, you probably find that appalling, but data crunching isn't really my main thing. </p> <p> Is it quicker to do it in Python? Not for me, it turns out. It also took me a couple of hours to repeat the exercise in Python. </p> <p> <strong>Next:</strong> <a href="/2024/03/18/extracting-data-from-a-small-csv-file-with-python">Extracting data from a small CSV file with Python</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Range as a functor https://blog.ploeh.dk/2024/02/12/range-as-a-functor 2024-02-12T06:59:00+00:00 Mark Seemann <div id="post"> <p> <em>With examples in C#, F#, and Haskell.</em> </p> <p> This article is an instalment in <a href="/2024/01/01/variations-of-the-range-kata">a short series of articles on the Range kata</a>. In the previous three articles you've seen <a href="https://codingdojo.org/kata/Range/">the Range kata</a> implemented <a href="/2024/01/08/a-range-kata-implementation-in-haskell">in Haskell</a>, <a href="/2024/01/15/a-range-kata-implementation-in-f">in F#</a>, and <a href="/2024/01/22/a-range-kata-implementation-in-c">in C#</a>. </p> <p> The reason I engaged with this kata was that I find that it provides a credible example of a how a pair of <a href="/2018/03/22/functors">functors</a> itself forms a functor. In this article, you'll see how that works out in three languages. If you don't care about one or two of those languages, just skip that section. </p> <h3 id="f8b28f239ca6444c8f32c768e725fad7"> Haskell perspective <a href="#f8b28f239ca6444c8f32c768e725fad7">#</a> </h3> <p> If you've done any <a href="https://www.haskell.org/">Haskell</a> programming, you may be thinking that I have in mind the default <code>Functor</code> instances for tuples. As part of the <a href="https://hackage.haskell.org/package/base">base</a> library, tuples (pairs, triples, quadruples, etc.) are already <code>Functor</code> instances. Specifically for pairs, we have this instance: </p> <p> <pre>instance Functor ((,) a)</pre> </p> <p> Those are not the functor instances I have in mind. To a degree, I find these default <code>Functor</code> instances unfortunate, or at least arbitrary. Let's briefly explore the above instance to see why that is. </p> <p> Haskell is a notoriously terse language, but if we expand the above instance to (invalid) pseudocode, it says something like this: </p> <p> <pre>instance Functor ((a,b) b)</pre> </p> <p> What I'm trying to get across here is that the <code>a</code> type argument is fixed, and only the second type argument <code>b</code> can be mapped. Thus, you can map a <code>(Bool, String)</code> pair to a <code>(Bool, Int)</code> pair: </p> <p> <pre>ghci&gt; fmap length (True, "foo") (True,3)</pre> </p> <p> but the first element (<code>Bool</code>, in this example) is fixed, and you can't map that. To be clear, the first element can be any type, but once you've fixed it, you can't change it (within the constraints of the <code>Functor</code> API, mind): </p> <p> <pre>ghci&gt; fmap (replicate 3) (42, 'f') (42,"fff") ghci&gt; fmap ($ 3) ("bar", (* 2)) ("bar",6)</pre> </p> <p> The reason I find these default instances arbitrary is that this isn't the only possible <code>Functor</code> instance. Pairs, in particular, are also <a href="https://hackage.haskell.org/package/base/docs/Data-Bifunctor.html">Bifunctor</a> instances, so you can easily map over the first element, instead of the second: </p> <p> <pre>ghci&gt; first show (42, 'f') ("42",'f')</pre> </p> <p> Similarly, one can easily imagine a <code>Functor</code> instance for triples (three-tuples) that map the middle element. The default instance, however, maps the third (i.e. last) element only. </p> <p> There are some hand-wavy rationalizations out there that argue that in Haskell, application and reduction is usually done from the right, so therefore it's most appropriate to map over the rightmost element of tuples. I admit that it at least argues from a position of consistency, and it does make it easier to remember, but from a didactic perspective I still find it a bit unfortunate. It suggests that a tuple functor only maps the last element. </p> <p> What I had in mind for <em>ranges</em> however, wasn't to map only the first or the last element. Neither did I wish to treat ranges as <a href="/2018/12/24/bifunctors">bifunctors</a>. What I really wanted was the ability to project an entire range. </p> <p> In my Haskell Range implementation, I'd simply treated ranges as tuples of <code>Endpoint</code> values, and although I didn't show that in the article, I ultimately declared <code>Endpoint</code> as a <code>Functor</code> instance: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Endpoint&nbsp;a&nbsp;=&nbsp;Open&nbsp;a&nbsp;|&nbsp;Closed&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Functor</span>)</pre> </p> <p> This enables you to map a single <code>Endpoint</code> value: </p> <p> <pre>ghci&gt; fmap length $ Closed "foo" Closed 3</pre> </p> <p> That's just a single value, but the Range kata API operates with pairs of <code>Endpoint</code> value. For example, the <code>contains</code> function has this type: </p> <p> <pre><span style="color:#2b91af;">contains</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Foldable</span>&nbsp;t,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(<span style="color:blue;">Endpoint</span>&nbsp;a,&nbsp;<span style="color:blue;">Endpoint</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;t&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span></pre> </p> <p> Notice the <code>(Endpoint a, Endpoint a)</code> input type. </p> <p> Is it possible to treat such a pair as a functor? Yes, indeed, just import <a href="https://hackage.haskell.org/package/base/docs/Data-Functor-Product.html">Data.Functor.Product</a>, which enables you to package two functor values in a single wrapper: </p> <p> <pre>ghci&gt; import Data.Functor.Product ghci&gt; Pair (Closed "foo") (Open "corge") Pair (Closed "foo") (Open "corge")</pre> </p> <p> Now, granted, the <code>Pair</code> data constructor doesn't wrap a <em>tuple</em>, but that's easily fixed: </p> <p> <pre>ghci&gt; uncurry Pair (Closed "foo", Open "corge") Pair (Closed "foo") (Open "corge")</pre> </p> <p> The resulting <code>Pair</code> value is a <code>Functor</code> instance, which means that you can project it: </p> <p> <pre>ghci&gt; fmap length $ uncurry Pair (Closed "foo", Open "corge") Pair (Closed 3) (Open 5)</pre> </p> <p> Now, granted, I find the <code>Data.Functor.Product</code> API a bit lacking in convenience. For instance, there's no <code>getPair</code> function to retrieve the underlying values; you'd have to use pattern matching for that. </p> <p> In any case, my motivation for covering this ground wasn't to argue that <code>Data.Functor.Product</code> is all we need. The point was rather to observe that when you have two functors, you can combine them, and the combination is also a functor. </p> <p> This is one of the many reasons I get so much value out of Haskell. Its abstraction level is so high that it substantiates relationships that may also exist in other code bases, written in other programming languages. Even if a language like <a href="https://fsharp.org/">F#</a> or C# can't formally express some of those abstraction, you can still make use of them as 'design patterns' (for lack of a better term). </p> <h3 id="ba94968ed2bc4780b995639212f8371b"> F# functor <a href="#ba94968ed2bc4780b995639212f8371b">#</a> </h3> <p> What we've learned from Haskell is that if we have two functors we can combine them into one. Specifically, I made <code>Endpoint</code> a <code>Functor</code> instance, and from that followed automatically that a <code>Pair</code> of those was also a <code>Functor</code> instance. </p> <p> I can do the same in F#, starting with <code>Endpoint</code>. In F# I've unsurprisingly defined the type like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Endpoint&lt;&#39;a&gt;&nbsp;=&nbsp;Open&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a&nbsp;|&nbsp;Closed&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> That's just a standard <a href="https://en.wikipedia.org/wiki/Tagged_union">discriminated union</a>. In order to make it a functor, you'll have to add a <code>map</code> function: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Endpoint&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Open&nbsp;&nbsp;&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Open&nbsp;&nbsp;&nbsp;(f&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Closed&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Closed&nbsp;(f&nbsp;x)</pre> </p> <p> The function alone, however, isn't enough to give rise to a functor. We must also convince ourselves that the <code>map</code> function obeys the functor laws. One way to do that is to write tests. While tests aren't <em>proofs</em>, we may still be sufficiently reassured by the tests that that's good enough for us. While I could, I'm not going to <em>prove</em> that <code>Endpoint.map</code> satisfies the functor laws. I will, later, do just that with the pair, but I'll leave this one as an exercise for the interested reader. </p> <p> Since I was already using <a href="https://hedgehog.qa/">Hedgehog</a> for property-based testing in my F# code, it was obvious to write properties for the functor laws as well. </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``First&nbsp;functor&nbsp;law``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;genInt32&nbsp;=&nbsp;Gen.int32&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;expected&nbsp;=&nbsp;Gen.choice&nbsp;[Gen.map&nbsp;Open&nbsp;genInt32;&nbsp;Gen.map&nbsp;Closed&nbsp;genInt32] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Endpoint.map&nbsp;id&nbsp;expected &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual&nbsp;}</pre> </p> <p> This property exercises the first functor law for integer endpoints. Recall that this law states that if you map a value with the <a href="https://en.wikipedia.org/wiki/Identity_function">identity function</a>, nothing really happens. </p> <p> The second functor law is more interesting. </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Second&nbsp;functor&nbsp;law``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;genInt32&nbsp;=&nbsp;Gen.int32&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;endpoint&nbsp;=&nbsp;Gen.choice&nbsp;[Gen.map&nbsp;Open&nbsp;genInt32;&nbsp;Gen.map&nbsp;Closed&nbsp;genInt32] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;f&nbsp;=&nbsp;Gen.item&nbsp;[id;&nbsp;((+)&nbsp;1);&nbsp;((*)&nbsp;2)] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;g&nbsp;=&nbsp;Gen.item&nbsp;[id;&nbsp;((+)&nbsp;1);&nbsp;((*)&nbsp;2)] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Endpoint.map&nbsp;(f&nbsp;&lt;&lt;&nbsp;g)&nbsp;endpoint &nbsp;&nbsp;&nbsp;&nbsp;Endpoint.map&nbsp;f&nbsp;(Endpoint.map&nbsp;g&nbsp;endpoint)&nbsp;=!&nbsp;actual&nbsp;}</pre> </p> <p> This property again exercises the property for integer endpoints. Not only does the property pick a random integer and varies whether the <code>Endpoint</code> is <code>Open</code> or <code>Closed</code>, it also picks two random functions from a small list of functions: The identity function (again), a function that increments by one, and a function that doubles the input. These two functions, <code>f</code> and <code>g</code>, might then be the same, but might also be different from each other. Thus, the composition <code>f&nbsp;&lt;&lt;&nbsp;g</code> <em>might</em> be <code>id &lt;&lt; id</code> or <code>((+) 1) &lt;&lt; ((+) 1)</code>, but might just as well be <code>((+) 1) &lt;&lt; ((*) 2)</code>, or one of the other possible combinations. </p> <p> The law states that the result should be the same regardless of whether you first compose the functions and then map them, or map them one after the other. </p> <p> Which is the case. </p> <p> A <code>Range</code> is defined like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Range&lt;&#39;a&gt;&nbsp;=&nbsp;{&nbsp;LowerBound&nbsp;:&nbsp;Endpoint&lt;&#39;a&gt;;&nbsp;UpperBound&nbsp;:&nbsp;Endpoint&lt;&#39;a&gt;&nbsp;}</pre> </p> <p> This record type also gives rise to a functor: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Range&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;{&nbsp;LowerBound&nbsp;=&nbsp;lowerBound;&nbsp;UpperBound&nbsp;=&nbsp;upperBound&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;LowerBound&nbsp;=&nbsp;Endpoint.map&nbsp;f&nbsp;lowerBound &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UpperBound&nbsp;=&nbsp;Endpoint.map&nbsp;f&nbsp;upperBound&nbsp;}</pre> </p> <p> This <code>map</code> function uses the projection <code>f</code> on both the <code>lowerBound</code> and the <code>upperBound</code>. It, too, obeys the functor laws: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``First&nbsp;functor&nbsp;law``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;genInt64&nbsp;=&nbsp;Gen.int64&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;genEndpoint&nbsp;=&nbsp;Gen.choice&nbsp;[Gen.map&nbsp;Open&nbsp;genInt64;&nbsp;Gen.map&nbsp;Closed&nbsp;genInt64] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;expected&nbsp;=&nbsp;Gen.tuple&nbsp;genEndpoint&nbsp;|&gt;&nbsp;Gen.map&nbsp;Range.ofEndpoints &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;expected&nbsp;|&gt;&nbsp;Ploeh.Katas.Range.map&nbsp;id &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual&nbsp;} [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Second&nbsp;functor&nbsp;law``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;genInt16&nbsp;=&nbsp;Gen.int16&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;genEndpoint&nbsp;=&nbsp;Gen.choice&nbsp;[Gen.map&nbsp;Open&nbsp;genInt16;&nbsp;Gen.map&nbsp;Closed&nbsp;genInt16] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;range&nbsp;=&nbsp;Gen.tuple&nbsp;genEndpoint&nbsp;|&gt;&nbsp;Gen.map&nbsp;Range.ofEndpoints &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;f&nbsp;=&nbsp;Gen.item&nbsp;[id;&nbsp;((+)&nbsp;1s);&nbsp;((*)&nbsp;2s)] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;g&nbsp;=&nbsp;Gen.item&nbsp;[id;&nbsp;((+)&nbsp;1s);&nbsp;((*)&nbsp;2s)] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;range&nbsp;|&gt;&nbsp;Ploeh.Katas.Range.map&nbsp;(f&nbsp;&lt;&lt;&nbsp;g) &nbsp;&nbsp;&nbsp;&nbsp;Ploeh.Katas.Range.map&nbsp;f&nbsp;(Ploeh.Katas.Range.map&nbsp;g&nbsp;range)&nbsp;=!&nbsp;actual&nbsp;}</pre> </p> <p> These two Hedgehog properties are cast in the same mould as the <code>Endpoint</code> properties, only they create 64-bit and 16-bit ranges for variation's sake. </p> <h3 id="c91ec56a7b22445b85ac4253f81c5c74"> C# functor <a href="#c91ec56a7b22445b85ac4253f81c5c74">#</a> </h3> <p> As I wrote about the Haskell result, it teaches us which abstractions are possible, even if we can't formalise them to the same degree in, say, C# as we can in Haskell. It should come as no surprise, then, that we can also make <code><span style="color:#2b91af;">Range</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> a functor in C#. </p> <p> In C# we <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatically</a> do that by giving a class a <code>Select</code> method. Again, we'll have to begin with <code>Endpoint</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Endpoint&lt;TResult&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(Func&lt;T,&nbsp;TResult&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whenClosed:&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;Endpoint.Closed(selector(x)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whenOpen:&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;Endpoint.Open(selector(x))); }</pre> </p> <p> Does that <code>Select</code> method obey the functor laws? Yes, as we can demonstrate (not prove) with a few properties: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">FirstFunctorLaw</span>() { &nbsp;&nbsp;&nbsp;&nbsp;Gen.OneOf( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Int.Select(Endpoint.Open), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Int.Select(Endpoint.Closed)) &nbsp;&nbsp;&nbsp;&nbsp;.Sample(<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;expected.Select(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;}); } [Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ScondFunctorLaw</span>() { &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;endpoint&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.OneOf( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Int.Select(Endpoint.Open), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Int.Select(Endpoint.Closed)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;f&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.OneOfConst&lt;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;1,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;*&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;g&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.OneOfConst&lt;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;1,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;*&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(endpoint,&nbsp;f,&nbsp;g)) &nbsp;&nbsp;&nbsp;&nbsp;.Sample(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;t.endpoint.Select(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;t.g(t.f(x))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.endpoint.Select(t.f).Select(t.g), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> These two tests follow the scheme laid out by the above F# properties, and they both pass. </p> <p> The <code>Range</code> class gets the same treatment. First, a <code>Select</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Range&lt;TResult&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(Func&lt;T,&nbsp;TResult&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">selector</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;TResult&nbsp;:&nbsp;IComparable&lt;TResult&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Range&lt;TResult&gt;(min.Select(selector),&nbsp;max.Select(selector)); }</pre> </p> <p> which, again, can be demonstrated with two properties that exercise the functor laws: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">FirstFunctorLaw</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">genEndpoint</span>&nbsp;=&nbsp;Gen.OneOf( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Int.Select(Endpoint.Closed), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Int.Select(Endpoint.Open)); &nbsp;&nbsp;&nbsp;&nbsp;genEndpoint.SelectMany(<span style="font-weight:bold;color:#1f377f;">min</span>&nbsp;=&gt;&nbsp;genEndpoint &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="font-weight:bold;color:#1f377f;">max</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Range&lt;<span style="color:blue;">int</span>&gt;(min,&nbsp;max))) &nbsp;&nbsp;&nbsp;&nbsp;.Sample(<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.Select(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(sut,&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;}); } [Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">SecondFunctorLaw</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">genEndpoint</span>&nbsp;=&nbsp;Gen.OneOf( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Int.Select(Endpoint.Closed), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Int.Select(Endpoint.Open)); &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;min&nbsp;<span style="color:blue;">in</span>&nbsp;genEndpoint &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;max&nbsp;<span style="color:blue;">in</span>&nbsp;genEndpoint &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;f&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.OneOfConst&lt;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;1,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;*&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;g&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.OneOfConst&lt;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;1,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;*&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(sut&nbsp;:&nbsp;<span style="color:blue;">new</span>&nbsp;Range&lt;<span style="color:blue;">int</span>&gt;(min,&nbsp;max),&nbsp;f,&nbsp;g)) &nbsp;&nbsp;&nbsp;&nbsp;.Sample(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;t.sut.Select(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;t.g(t.f(x))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.sut.Select(t.f).Select(t.g), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> These tests also pass. </p> <h3 id="222d65b253b145679994d1b9336069c7"> Laws <a href="#222d65b253b145679994d1b9336069c7">#</a> </h3> <p> Exercising a pair of properties can give us a good warm feeling that the data structures and functions defined above are proper functors. Sometimes, tests are all we have, but in this case we can do better. We can prove that the functor laws always hold. </p> <p> The various above incarnations of a <code>Range</code> type are all <a href="https://en.wikipedia.org/wiki/Product_type">product types</a>, and the canonical form of a product type is a tuple (see e.g. <a href="https://thinkingwithtypes.com/">Thinking with Types</a> for a clear explanation of why that is). That's the reason I stuck with a tuple in my Haskell code. </p> <p> Consider the implementation of the <code>fmap</code> implementation of <code>Pair</code>: </p> <p> <pre>fmap f (Pair x y) = Pair (fmap f x) (fmap f y)</pre> </p> <p> We can use equational reasoning, and as always I'll use the <a href="https://bartoszmilewski.com/2015/01/20/functors/">the notation that Bartosz Milewski uses</a>. It's only natural to begin with the first functor law, using <code>F</code> and <code>G</code> as placeholders for two arbitrary <code>Functor</code> data constructors. </p> <p> <pre> fmap id (Pair (F x) (G y)) = { definition of fmap } Pair (fmap id (F x)) (fmap id (G y)) = { first functor law } Pair (F x) (G y) = { definition of id } id (Pair (F x) (G y))</pre> </p> <p> Keep in mind that in this notation, the equal signs are true equalities, going both ways. Thus, you can read this proof from the top to the bottom, or from the bottom to the top. The equality holds both ways, as should be the case for a true equality. </p> <p> We can proceed in the same vein to prove the second functor law, being careful to distinguish between <code>Functor</code> instances (<code>F</code> and <code>G</code>) and functions (<code>f</code> and <code>g</code>): </p> <p> <pre> fmap (g . f) (Pair (F x) (G y)) = { definition of fmap } Pair (fmap (g . f) (F x)) (fmap (g . f) (G y)) = { second functor law } Pair ((fmap g . fmap f) (F x)) ((fmap g . fmap f) (G y)) = { definition of composition } Pair (fmap g (fmap f (F x))) (fmap g (fmap f (G y))) = { definition of fmap } fmap g (Pair (fmap f (F x)) (fmap f (G y))) = { definition of fmap } fmap g (fmap f (Pair (F x) (G y))) = { definition of composition } (fmap g . fmap f) (Pair (F x) (G y))</pre> </p> <p> Notice that both proofs make use of the functor laws. This may seem self-referential, but is rather recursive. When the proofs refer to the functor laws, they refer to the functors <code>F</code> and <code>G</code>, which are both assumed to be lawful. </p> <p> This is how we know that the product of two lawful functors is itself a functor. </p> <h3 id="54ac7f2fadef46c4a295333a2037656e"> Negations <a href="#54ac7f2fadef46c4a295333a2037656e">#</a> </h3> <p> During all of this, you may have thought: <em>What happens if we project a range with a negation?</em> </p> <p> As a simple example, let's consider the range from <em>-1</em> to <em>2:</em> </p> <p> <pre>ghci&gt; uncurry Pair (Closed (-1), Closed 2) Pair (Closed (-1)) (Closed 2)</pre> </p> <p> We may draw this range on the number line like this: </p> <p> <img src="/content/binary/single-range-on-number-line.png" alt="The range from -1 to 2 drawn on the number line."> </p> <p> What happens if we map that range by multiplying with <em>-1?</em> </p> <p> <pre>ghci&gt; fmap negate $ uncurry Pair (Closed (-1), Closed 2) Pair (Closed 1) (Closed (-2))</pre> </p> <p> We get a range from <em>1</em> to <em>-2!</em> </p> <p> <em>Aha!</em> you say, <em>clearly that's wrong!</em> We've just found a counterexample. After all, <em>range</em> isn't a functor. </p> <p> Not so. The functor laws say nothing about the interpretation of projections (but I'll get back to that in a moment). Rather, they say something about composition, so let's consider an example that reaches a similar, seemingly wrong result: </p> <p> <pre>ghci&gt; fmap ((+1) . negate) $ uncurry Pair (Closed (-1), Closed 2) Pair (Closed 2) (Closed (-1))</pre> </p> <p> This is a range from <em>2</em> to <em>-1</em>, so just as problematic as before. </p> <p> The second functor law states that the outcome should be the same if we map piecewise: </p> <p> <pre>ghci&gt; (fmap (+ 1) . fmap negate) $ uncurry Pair (Closed (-1), Closed 2) Pair (Closed 2) (Closed (-1))</pre> </p> <p> Still a range from <em>2</em> to <em>-1</em>. The second functor law holds. </p> <p> <em>But,</em> you protest, <em>that's doesn't make any sense!</em> </p> <p> I disagree. It could make sense in at least three different ways. </p> <p> What does a range from <em>2</em> to <em>-1</em> mean? I can think of three interpretations: </p> <ul> <li>It's the empty set</li> <li>It's the range from <em>-1</em> to <em>2</em></li> <li>It's the set of numbers that are either less than or equal to <em>-1</em> or greater than or equal to <em>2</em></li> </ul> <p> We may illustrate those three interpretations, together with the original range, like this: </p> <p> <img src="/content/binary/three-ranges-on-number-lines.png" alt="Four number lines, each with a range interpretation drawn in."> </p> <p> According to the first interpretation, we consider the range as the Boolean <em>and</em> of two predicates. In this interpretation the initial range is really the Boolean expression <em>-1 ≤ x ∧ x ≤ 2</em>. The projected range then becomes the expression <em>2 ≤ x ∧ x ≤ -1</em>, which is not possible. This is how I've chosen to implement the <code>contains</code> function: </p> <p> <pre>ghci&gt; Pair x y = fmap ((+1) . negate) $ uncurry Pair (Closed (-1), Closed 2) ghci&gt; contains (x, y) [0] False ghci&gt; contains (x, y) [-3] False ghci&gt; contains (x, y) [4] False</pre> </p> <p> In this interpretation, the result is the empty set. The range isn't impossible; it's just empty. That's the second number line from the top in the above illustration. </p> <p> This isn't, however, the only interpretation. Instead, we may choose to <a href="https://en.wikipedia.org/wiki/Robustness_principle">be liberal in what we accept</a> and interpret the range from <em>2</em> to <em>-1</em> as a 'programmer mistake': <em>What you asked me to do is formally wrong, but I think that I understand that you meant the range from </em>-1<em> to </em>2. </p> <p> That's the third number line in the above illustration. </p> <p> The fourth interpretation is that when the first element of the range is greater than the second, the range represents the <a href="https://en.wikipedia.org/wiki/Complement_(set_theory)">complement</a> of the range. That's the fourth number line in the above illustration. </p> <p> The reason I spent some time on this is that it's easy to confuse the functor laws with other properties that you may associate with a data structure. This may lead you to falsely conclude that a functor isn't a functor, because you feel that it violates some other invariant. </p> <p> If this happens, consider instead whether you could possibly expand the interpretation of the data structure in question. </p> <h3 id="859ece7acfdb415da1ba52578189e9ca"> Conclusion <a href="#859ece7acfdb415da1ba52578189e9ca">#</a> </h3> <p> You can model a <em>range</em> as a functor, which enables you to project ranges, either moving them around on an imaginary number line, or changing the type of the range. This might for example enable you to map a date range to an integer range, or vice versa. </p> <p> A functor enables mapping or projection, and some maps may produce results that you find odd or counter-intuitive. In this article you saw an example of that in the shape of a negated range where the first element (the 'minimum', in one interpretation) becomes greater than the second element (the 'maximum'). You may take that as an indication that the functor isn't, after all, a functor. </p> <p> This isn't the case. A data structure and its <em>map</em> function is a functor if the the mapping obeys the functor laws, which is the case for the range structures you've seen here. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Statically and dynamically typed scripts https://blog.ploeh.dk/2024/02/05/statically-and-dynamically-typed-scripts 2024-02-05T07:53:00+00:00 Mark Seemann <div id="post"> <p> <em>Extracting and analysing data in Haskell and Python.</em> </p> <p> I was recently following a course in mathematical analysis and probability for computer scientists. One assignment asked to analyze a small <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV file</a> with data collected in a student survey. The course contained a mix of pure maths and practical application, and the official programming language to be used was <a href="https://www.python.org/">Python</a>. It was understood that one was to do the work in Python, but it wasn't an explicit requirement, and I was so tired that didn't have the energy for it. </p> <p> I can get by in Python, but it's not a language I'm actually comfortable with. For small experiments, ad-hoc scripting, etc. I reach for <a href="https://www.haskell.org/">Haskell</a>, so that's what I did. </p> <p> This was a few months ago, and I've since followed another course that required more intense use of Python. With a few more months of Python programming under my belt, I decided to revisit that old problem and do it in Python with the explicit purpose of comparing and contrasting the two. </p> <h3 id="ae9c59e5fd0744f98841c6f864b20e33"> Static or dynamic types for scripting <a href="#ae9c59e5fd0744f98841c6f864b20e33">#</a> </h3> <p> I'd like to make one point with these articles, and that is that dynamically typed languages aren't inherently better suited for scripting than statically typed languages. From this, it does not, however, follow that statically typed languages are better, either. Rather, I increasingly believe that whether you find one or the other more productive is a question of personality, past experiences, programming background, etc. I've been over this ground before. <a href="/2021/08/09/am-i-stuck-in-a-local-maximum">Many of my heroes seem to favour dynamically typed languages</a>, while I keep returning to statically typed languages. </p> <p> For more than a decade I've preferred <a href="https://fsharp.org/">F#</a> or Haskell for ad-hoc scripting. Note that while these languages are statically typed, they are <a href="/2019/12/16/zone-of-ceremony">low on ceremony</a>. Types are <em>inferred</em> rather than declared. This means that for scripts, you can experiment with small code blocks, iteratively move closer to what you need, just as you would with a language like Python. Change a line of code, and the inferred type changes with it; there are no type declarations that you also need to fix. </p> <p> When I talk about writing scripts in statically typed languages, I have such languages in mind. I wouldn't write a script in C#, <a href="https://en.wikipedia.org/wiki/C_(programming_language)">C</a>, or <a href="https://www.java.com/">Java</a>. </p> <blockquote> <p> "Let me stop you right there: I don't think there is a real dynamic typing versus static typing debate. </p> <p> "What such debates normally are is language X vs language Y debates (where X happens to be dynamic and Y happens to be static)." </p> <footer><cite><a href="https://twitter.com/KevlinHenney/status/1425513161252278280">Kevlin Henney</a></cite></footer> </blockquote> <p> The present articles compare Haskell and Python, so be careful that you don't extrapolate and draw any conclusions about, say, <a href="https://en.wikipedia.org/wiki/C%2B%2B">C++</a> versus <a href="https://www.erlang.org/">Erlang</a>. </p> <p> When writing an ad-hoc script to extract data from a file, it's important to be able to experiment and iterate. Load the file, inspect the data, figure out how to extract subsets of it (particular columns, for example), calculate totals, averages, etc. A <a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a> is indispensable in such situations. The Haskell REPL (called <em><a href="https://en.wikipedia.org/wiki/Glasgow_Haskell_Compiler">Glasgow Haskell Compiler</a> interactive</em>, or just <em>GHCi</em>) is the best one I've encountered. </p> <p> I imagine that a Python expert would start by reading the data to slice and dice it various ways. We may label this a <em>data-first</em> approach, but be careful not to read too much into this, as I don't really know what I'm talking about. That's not how my mind works. Instead, I tend to take a <em>types-first</em> approach. I'll look at the data and start with the types. </p> <h3 id="6454710fbb5644ae979af4aa247dce96"> The assignment <a href="#6454710fbb5644ae979af4aa247dce96">#</a> </h3> <p> The actual task is the following. At the beginning of the course, the professors asked students to fill out a survey. Among the questions asked was which grade the student expected to receive, and how much experience with programming he or she already had. </p> <p> Grades are given according to the <a href="https://en.wikipedia.org/wiki/Academic_grading_in_Denmark">Danish academic scale</a>: -3, 00, 02, 4, 7, 10, and 12, and experience level on a simple numeric scale from 1 to 7, with 1 indicating no experience and 7 indicating expert-level experience. </p> <p> Here's a small sample of the data: </p> <p> <pre>No,3,2,6,6 No,4,2,3,7 No,1,12,6,2 No,4,10,4,3 No,3,4,4,6</pre> </p> <p> The expected grade is in the third column (i.e. <em>2, 2, 12, 10, 4</em>) and the experience level is in the fourth column (<em>6,3,6,4,4</em>). The other columns are answers to different survey questions. The full data set contains 38 rows. </p> <p> The assignment poses the following questions: Two rows from the survey data are randomly selected. What is the <a href="https://www.probabilitycourse.com/chapter3/3_1_3_pmf.php">probability mass function</a> (PMF) of the sum of their expected grades, and what is the PMF of the absolute difference between their programming experience levels? </p> <p> In both cases I was also asked to plot the PMFs. </p> <h3 id="331b2ed3198f4a59872eb0e9d2f4ebd9"> Comparisons <a href="#331b2ed3198f4a59872eb0e9d2f4ebd9">#</a> </h3> <p> As outlined above, I originally wrote a Haskell script to answer the questions, and only months later returned to the problem to give it a go in Python. When reading my detailed walkthroughs, keep in mind that I have 8-9 years of Haskell experience, and that I tend to 'think in Haskell', while I have only about a year of experience with Python. I don't consider myself proficient with Python, so the competition is rigged from the outset. </p> <ul> <li><a href="/2024/02/19/extracting-data-from-a-small-csv-file-with-haskell">Extracting data from a small CSV file with Haskell</a></li> <li><a href="/2024/03/18/extracting-data-from-a-small-csv-file-with-python">Extracting data from a small CSV file with Python</a></li> </ul> <p> For this small task, I don't think that there's a clear winner. I still like my Haskell code the best, but I'm sure someone better at Python could write a much cleaner script. I also have to admit that <a href="https://matplotlib.org/">Matplotlib</a> makes it a breeze to produce nice-looking plots with Python, whereas I don't even know where to start with that with Haskell. </p> <p> Recently I've done some more advanced data analysis with Python, such as random forest classification, principal component analysis, KNN-classification, etc. While I understand that I'm only scratching the surface of data science and machine learning, it's obvious that there's a rich Python ecosystem for that kind of work. </p> <h3 id="dcf63d011f02487eb051e3a75cbe59f7"> Conclusion <a href="#dcf63d011f02487eb051e3a75cbe59f7">#</a> </h3> <p> This lays the foundations for comparing a small Haskell script with an equivalent Python script. There's no scientific method to the comparison; it's just me doing the same exercise twice, a bit like I'd <a href="/2020/01/13/on-doing-katas">do katas</a> with multiple variations in order to learn. </p> <p> While I still like Haskell better than Python, that's only a personal preference. I'm deliberately not declaring a winner. </p> <p> One point I'd like to make, however, is that there's nothing inherently better about a dynamically typed language when it comes to ad-hoc scripting. Languages with strong type inference work well, too. </p> <p> <strong>Next:</strong> <a href="/2024/02/19/extracting-data-from-a-small-csv-file-with-haskell">Extracting data from a small CSV file with Haskell</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Error categories and category errors https://blog.ploeh.dk/2024/01/29/error-categories-and-category-errors 2024-01-29T16:05:00+00:00 Mark Seemann <div id="post"> <p> <em>How I currently think about errors in programming.</em> </p> <p> A reader <a href="/2023/08/14/replacing-mock-and-stub-with-a-fake#0afe67b375254fe193a3fd10234a1ce9">recently asked a question</a> that caused me to reflect on the way I think about errors in software. While my approach to error handling has remained largely the same for years, I don't think I've described it in an organized way. I'll try to present those thoughts here. </p> <p> This article is, for lack of a better term, a <em>think piece</em>. I don't pretend that it represents any fundamental truth, or that this is the only way to tackle problems. Rather, I write this article for two reasons. </p> <ul> <li>Writing things down often helps clarifying your thoughts. While I already feel that my thinking on the topic of error handling is fairly clear, I've written enough articles that I know that by writing this one, I'll learn something new.</li> <li>Publishing this article enables the exchange of ideas. By sharing my thoughts, I enable readers to point out errors in my thinking, or to improve on my work. Again, I may learn something. Perhaps others will, too.</li> </ul> <p> Although I don't claim that the following is universal, I've found it useful for years. </p> <h3 id="43e58fcae3184b0597def9a4ec5629d7"> Error categories <a href="#43e58fcae3184b0597def9a4ec5629d7">#</a> </h3> <p> Almost all software is at risk of failing for a myriad of reasons: User input, malformed data, network partitions, cosmic rays, race conditions, bugs, etc. Even so, we may categorize errors like this: </p> <ul> <li>Predictable errors we can handle</li> <li>Predictable errors we can't handle</li> <li>Errors we've failed to predict</li> </ul> <p> This distinction is hardly original. I believe I've picked it up from Michael Feathers, but although I've searched, I can't find the source, so perhaps I'm remembering it wrong. </p> <p> You may find these three error categories underwhelming, but I find it useful to first consider what may be done about an error. Plenty of error situations are predictable. For example, all input should be considered suspect. This includes user input, but also data you receive from other systems. This kind of potential error you can typically solve with input validation, which I believe is <a href="/2020/12/14/validation-a-solved-problem">a solved problem</a>. Another predictable kind of error is unavailable services. Many systems store data in databases. You can easily predict that the database <em>will</em>, sooner or later, be unreachable. Potential causes include network partitions, a misconfigured connection string, logs running full, a crashed server, denial-of-service attacks, etc. </p> <p> With some experience with software development, it's not that hard producing a list of things that could go wrong. The next step is to decide what to do about it. </p> <p> There are scenarios that are so likely to happen, and where the solution is so well-known, that they fall into the category of predictable errors that you can handle. User input belongs here. You examine the input and inform the user if it's invalid. </p> <p> Even with input, however, other scenarios may lead you down different paths. What if, instead of a system with a user interface, you're developing a batch job that receives a big data file every night? How do you deal with invalid input in that scenario? Do you reject the entire data set, or do you filter it so that you only handle the valid input? Do you raise a notification to asynchronously inform the sender that input was malformed? </p> <p> Notice how categorization is context-dependent. It would be a (category?) error to interpret the above model as fixed and universal. Rather, it's an analysis framework that helps identifying how to categorize various fault scenarios in a particular application context. </p> <p> Another example may be in order. If your system depends on a database, a predictable error is that the database will be unavailable. Can you handle that situation? </p> <p> A common reaction is that there's really not a lot one can do about that. You may retry the operation, log the problem, or notify an on-call engineer, but ultimately the system <em>depends</em> on the database. If the database is unreachable, the system can't work. You can't handle that problem, so this falls in the category of predictable errors that you can't handle. </p> <p> Or does it? </p> <h3 id="b703b2c68f3b4656a1cf1f4042974ab7"> Trade-offs of error handling <a href="#b703b2c68f3b4656a1cf1f4042974ab7">#</a> </h3> <p> The example of an unreachable database is useful to explore in order to demonstrate that error handling isn't writ in stone, but rather an architectural design decision. Consider <a href="/2014/08/11/cqs-versus-server-generated-ids">a common API design</a> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRepository</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;Create(T&nbsp;item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;other&nbsp;members</span> }</pre> </p> <p> What happens if client code calls <code>Create</code> but the database is unreachable? This is C# code, but the problem generalizes. With most implementations, the <code>Create</code> method will throw an exception. </p> <p> Can you handle that situation? You may retry a couple of times, but if you have a user waiting for a response, you can't retry for too long. Once time is up, you'll have to accept that the operation failed. In a language like C#, the most robust implementation is to <em>not</em> handle the specific exception, but instead let it bubble up to be handled by a global exception handler that usually can't do much else than showing the user a generic error message, and then log the exception. </p> <p> This isn't your only option, though. You may find yourself in a context where this kind of attitude towards errors is unacceptable. If you're working with <a href="https://twitter.com/ploeh/status/530320252790669313">BLOBAs</a> it's probably fine, but if you're working with medical life-support systems, or deep-space probes, or in other high-value contexts, the overall error-tolerance may be lower. Then what do you do? </p> <p> You may try to address the concern with IT operations: Configure failover systems for the database, installing two network cards in every machine, and so on. This may (also) be a way to address the problem, but isn't your only option. You may also consider changing the software architecture. </p> <p> One option may be to switch to an asynchronous message-based system where messages are transmitted via durable queues. Granted, durables queues may fail as well (everything may fail), but when done right, they tend to be more robust. Even a machine that has lost all network connectivity may queue messages on its local disk until the network returns. Yes, the disk may run full, etc. but it's <em>less</em> likely to happen than a network partition or an unreachable database. </p> <p> Notice that an unreachable database now goes into the category of errors that you've predicted, and that you can handle. On the other hand, failing to send an asynchronous message is now a new kind of error in your system: One that you can predict, but can't handle. </p> <p> Making this change, however, impacts your software architecture. You can no longer have an interface method like the above <code>Create</code> method, because you can't rely on it returning an <code>int</code> in reasonable time. During error scenarios, messages may sit in queues for hours, if not days, so you can't block on such code. </p> <p> As <a href="/2014/08/11/cqs-versus-server-generated-ids">I've explained elsewhere</a> you can instead model a <code>Create</code> method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRepository</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Create(<span style="color:#2b91af;">Guid</span>&nbsp;id,&nbsp;T&nbsp;item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;other&nbsp;members</span> }</pre> </p> <p> Not only does this follow the <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> principle, it also makes it easier for you to adopt an asynchronous message-based architecture. Done consistently, however, this requires that you approach application design in a way different from a design where you assume that the database is reachable. </p> <p> It may even impact a user interface, because it'd be a good idea to design user experience in such a way that it helps the user have a congruent mental model of how the system works. This may include making the concept of an <em>outbox</em> explicit in the user interface, as it may help users realize that writes happen asynchronously. Most users understand that email works that way, so it's not inconceivable that they may be able to adopt a similar mental model of other applications. </p> <p> The point is that this is an <em>option</em> that you may consider as an architect. Should you always design systems that way? I wouldn't. There's much extra complexity that you have to deal with in order to make asynchronous messaging work: UX, out-of-order messages, dead-letter queues, message versioning, etc. Getting to <a href="https://en.wikipedia.org/wiki/High_availability">five nines</a> is expensive, and often not warranted. </p> <p> The point is rather that what goes in the <em>predictable errors we can't handle</em> category isn't fixed, but context-dependent. Perhaps we should rather name the category <em>predictable errors we've decided not to handle</em>. </p> <h3 id="529bcc700301441aa3337bdb1911f74d"> Bugs <a href="#529bcc700301441aa3337bdb1911f74d">#</a> </h3> <p> How about the third category of errors, those we've failed to predict? We also call these <em>bugs</em> or <em>defects</em>. By definition, we only learn about them when they manifest. As soon as they become apparent, however, they fall into one of the other categories. If an error occurs once, it may occur again. It is now up to you to decide what to do about it. </p> <p> I usually consider <a href="/2023/01/23/agilean">errors as stop-the-line-issues</a>, so I'd be inclined to immediately address them. On they other hand, if you don't do that, you've implicitly decided to put them in the category of predictable errors that you've decided not to handle. </p> <p> We don't intentionally write bugs; there will always be some of those around. On the other hand, various practices help reducing them: Test-driven development, code reviews, property-based testing, but also up-front design. </p> <h3 id="4f0b84e839d54f16af37d0295140dc17"> Error-free code <a href="#4f0b84e839d54f16af37d0295140dc17">#</a> </h3> <p> Do consider explicitly how code may fail. </p> <p> Despite the title of this section, there's no such thing as error-free code. Still, you can explicitly think about edge cases. For example, how might the following function fail? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.Zero; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+=</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>++; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>; }</pre> </p> <p> In at least two ways: The input collection may be empty or infinite. I've <a href="/2020/02/03/non-exceptional-averages">already suggested a few ways to address those problems</a>. Some of them are similar to what Michael Feathers calls <a href="https://youtu.be/AnZ0uTOerUI?si=1gJXYFoVlNTSbjEt">unconditional code</a>, in that we may change the <a href="https://en.wikipedia.org/wiki/Domain_of_a_function">domain</a>. Another option, that I didn't cover in the linked article, is to expand the <a href="https://en.wikipedia.org/wiki/Codomain">codomain</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;TimeSpan?&nbsp;<span style="font-weight:bold;color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;IReadOnlyCollection&lt;TimeSpan&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!timeSpans.Any()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;TimeSpan.Zero; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;timeSpans) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;+=&nbsp;ts; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;sum&nbsp;/&nbsp;timeSpans.Count; }</pre> </p> <p> Now, instead of diminishing the domain, we expand the codomain by allowing the return value to be null. (Interestingly, this is the inverse of <a href="/2021/12/06/the-liskov-substitution-principle-as-a-profunctor">my profunctor description of the Liskov Substitution Principle</a>. I don't yet know what to make of that. See: Just by writing things down, I learn something I hadn't realized before.) </p> <p> This is beneficial in a statically typed language, because such a change makes hidden knowledge explicit. It makes it so explicit that a type checker can point out when we make mistakes. <a href="https://blog.janestreet.com/effective-ml-video/">Make illegal states unrepresentable</a>. <a href="https://en.wikipedia.org/wiki/Poka-yoke">Poka-yoke</a>. A potential run-time exception is now a compile-time error, and it's firmly in the category of errors that we've predicted and decided to handle. </p> <p> In the above example, we could use the built-in .NET <a href="https://learn.microsoft.com/dotnet/api/system.nullable-1">Nullable&lt;T&gt;</a> (with the <code>?</code> syntactic-sugar alias). In other cases, you may resort to returning a <a href="/2018/03/26/the-maybe-functor">Maybe</a> (AKA <em>option</em>). </p> <h3 id="0855ccbcd46f4c29980a219993293279"> Modelling errors <a href="#0855ccbcd46f4c29980a219993293279">#</a> </h3> <p> Explicitly expanding the codomain of functions to signal potential errors is beneficial if you expect the caller to be able to handle the problem. If callers can't handle an error, forcing them to deal with it is just going to make things more difficult. I've never done any professional <a href="https://www.java.com/">Java</a> programming, but I've heard plenty of Java developers complain about checked exceptions. As far as I can tell, the problem in Java isn't so much with the language feature per se, but rather with the exception types that APIs force you to handle. </p> <p> As an example, imagine that every time you call a database API, the compiler forces you to handle an <a href="https://learn.microsoft.com/dotnet/api/system.io.ioexception">IOException</a>. Unless you explicitly architect around it (as outlined above), this is likely to be one of the errors you can predict, but decide not to handle. But if the compiler forces you to handle it, then what do you do? You probably find some workaround that involves re-throwing the exception, or, as I understand that some Java developers do, declare that their own APIs may throw <em>any</em> exception, and by that means just pass the buck. Not helpful. </p> <p> As far as I can tell, (checked) exceptions are equivalent to the <a href="/2018/06/11/church-encoded-either">Either</a> container, also known as <em>Result</em>. We may imagine that instead of throwing exceptions, a function may return an Either value: <em>Right</em> for a right result (explicit mnemonic, there!), and left for an error. </p> <p> It might be tempting to model all error-producing operations as Either-returning, but <a href="https://eiriktsarpalis.wordpress.com/2017/02/19/youre-better-off-using-exceptions/">you're often better off using exceptions</a>. Throw exceptions in those situations that you expect most clients can't recover from. Return <em>left</em> (or <em>error</em>) cases in those situations that you expect that a typical client would want to handle. </p> <p> Again, it's context-specific, so if you're developing a reusable library, there's a balance to strike in API design (or overloads to supply). </p> <h3 id="4ecf10fdb9a842e88aeecf1024cd466a"> Most errors are just branches <a href="#4ecf10fdb9a842e88aeecf1024cd466a">#</a> </h3> <p> In many languages, errors are somehow special. Most modern languages include a facility to model errors as exceptions, and special syntax to throw or catch them. (The odd man out may be C, with its reliance on error codes as return values, but that is incredible awkward for other reasons. You may also reasonably argue that C is hardly a modern language.) </p> <p> Even Haskell has exceptions, even though it also has deep language support for <code>Maybe</code> and <code>Either</code>. Fortunately, Haskell APIs <em>tend</em> to only throw exceptions in those cases where average clients are unlikely to handle them: Timeouts, I/O failures, and so on. </p> <p> It's unfortunate that languages treat errors as something exceptional, because this nudges us to make a proper category error: That errors are somehow special, and that we can't use normal coding constructs or API design practices to model them. </p> <p> But you can. That's what <a href="https://youtu.be/AnZ0uTOerUI?si=1gJXYFoVlNTSbjEt">Michael Feathers' presentation is about</a>, and that's what you can do by <a href="https://blog.janestreet.com/effective-ml-video/">making illegal states unrepresentable</a>, or by returning Maybe or Either values. </p> <p> Most errors are just branches in your code; where it diverges from the happy path in order to do something else. </p> <h3 id="a0e442d5e5c842108555236745f23155"> Conclusion <a href="#a0e442d5e5c842108555236745f23155">#</a> </h3> <p> This article presents a framework for thinking about software errors. There are those you can predict may happen, and you choose to handle; those you predict may happen, but you choose to ignore; and those that you have not yet predicted: bugs. </p> <p> A little up-front thinking will often help you predict some errors, but I'm not advocating that you foresee all errors. Some errors are programmer errors, and we make those errors because we're human, exactly because we're failing to predict the behaviour of a particular state of the code. Once you discover a bug, however, you have a choice: Do you address it or ignore it? </p> <p> There are error conditions that you may deliberately choose to ignore. This doesn't necessarily make you an irresponsible programmer, but may rather be the result of a deliberate feasibility study. For example, every network operation may fail. How important is it that your application can keep running without the network? Is it worthwhile to make the code so robust that it can handle that situation? Or can you rather live with a few hours of downtime per quarter? If the latter, it may be best to let a human deal with network partitions when they occur. </p> <p> The three error categories I suggest here are context-dependent. You decide which problems to deal with, and which ones to ignore, but apart from that, error-handling doesn't have to be difficult. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A Range kata implementation in C# https://blog.ploeh.dk/2024/01/22/a-range-kata-implementation-in-c 2024-01-22T07:05:00+00:00 Mark Seemann <div id="post"> <p> <em>A port of the corresponding F# code.</em> </p> <p> This article is an instalment in <a href="/2024/01/01/variations-of-the-range-kata">a short series of articles on the Range kata</a>. In the <a href="/2024/01/15/a-range-kata-implementation-in-f">previous article</a> I made a pass at <a href="https://codingdojo.org/kata/Range/">the kata</a> in <a href="https://fsharp.org/">F#</a>, using property-based testing with <a href="https://hedgehog.qa/">Hedgehog</a> to generate test data. </p> <p> In the conclusion I mused about the properties I was able to come up with. Is it possible to describe open, closed, and mixed ranges in a way that's less coupled to the implementation? To be honest, I still don't have an answer to that question. Instead, in this article, I describe a straight port of the F# code to C#. There's value in that, too, for people who wonder <a href="/2015/04/15/c-will-eventually-get-all-f-features-right">how to reap the benefits of F# in C#</a>. </p> <p> The code is <a href="https://github.com/ploeh/RangeCSharp">available on GitHub</a>. </p> <h3 id="2b05848a3b494ec99cc0e50da22bdd15"> First property <a href="#2b05848a3b494ec99cc0e50da22bdd15">#</a> </h3> <p> Both F# and C# are .NET languages. They run in the same substrate, and are interoperable. While Hedgehog is written in F#, it's possible to consume F# libraries from C#, and vice versa. I've done this multiple times with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a>, but I admit to never having tried it with Hedgehog. </p> <p> If you want to try property-based testing in C#, a third alternative is available: <a href="https://github.com/AnthonyLloyd/CsCheck">CsCheck</a>. It's written in C# and is more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> in that context. While I sometimes <a href="/2021/02/15/when-properties-are-easier-than-examples">still use FsCheck from C#</a>, I often choose CsCheck for didactic reasons. </p> <p> The first property I wrote was a direct port of the idea of the first property I wrote in F#: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ClosedRangeContainsList</span>() { &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;xs&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Short.Enumerable.Nonempty &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;min&nbsp;=&nbsp;xs.Min() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;max&nbsp;=&nbsp;xs.Max() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(xs,&nbsp;min,&nbsp;max)) &nbsp;&nbsp;&nbsp;&nbsp;.Sample(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Range&lt;<span style="color:blue;">short</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ClosedEndpoint&lt;<span style="color:blue;">short</span>&gt;(t.min), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ClosedEndpoint&lt;<span style="color:blue;">short</span>&gt;(t.max)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.Contains(t.xs); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.True(actual,&nbsp;<span style="color:#a31515;">$&quot;</span><span style="color:#a31515;">Expected&nbsp;</span>{t.xs}<span style="color:#a31515;">&nbsp;to&nbsp;be&nbsp;contained&nbsp;in&nbsp;</span>{sut}<span style="color:#a31515;">.</span><span style="color:#a31515;">&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> This test (or property, if you will) uses a technique that I often use with property-based testing. I'm still searching for a catchy name for this, but here we may call it something like <em>reverse test-case assembly</em>. My <em>goal</em> is to test a predicate, and this particular property should verify that for a given <a href="https://en.wikipedia.org/wiki/Equivalence_class">Equivalence Class</a>, the predicate is always true. </p> <p> While we may think of an Equivalence Class as a set from which we pick test cases, I don't actually have a full enumeration of such a set. I can't have that, since that set is infinitely big. Instead of randomly picking values from a set that I can't fully populate, I instead carefully pick test case values in such a way that they would all belong to the same <a href="https://en.wikipedia.org/wiki/Partition_of_a_set">set partition</a> (Equivalence Class). </p> <p> The <a href="/2022/06/13/some-thoughts-on-naming-tests">test name suggests the test case</a>: I'd like to verify that given I have a closed range, when I ask it whether a list <em>within</em> that range is contained, then the answer is <em>true</em>. How do I pick such a test case? </p> <p> I do it in reverse. You can say that the sampling is the dual of the test. I start with a list (<code>xs</code>) and only then do I create a range that contains it. Since the first test case is for a closed range, the <code>min</code> and <code>max</code> values are sufficient to define such a range. </p> <p> How do I pass that property? </p> <p> Degenerately, as is often the case with TDD beginnings: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Contains</span>(IEnumerable&lt;T&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">candidates</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> Even though the <code>ClosedRangeContainsList</code> property effectively executes a hundred test cases, the <a href="/2019/10/07/devils-advocate">Devil's Advocate</a> can easily ignore that and instead return hard-coded <code>true</code>. </p> <h3 id="61c1050202934baa99baeaa4dfed40e1"> Endpoint sum type <a href="#61c1050202934baa99baeaa4dfed40e1">#</a> </h3> <p> I'm not going to bore you with the remaining properties. The repository is available on GitHub if you're interested in those details. </p> <p> If you've programmed in F# for some time, you typically miss <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> when forced to return to C#. A language like C# does have <a href="https://en.wikipedia.org/wiki/Product_type">product types</a>, but lack native <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a>. Even so, not all is lost. I've previously demonstrated that <a href="/2018/06/25/visitor-as-a-sum-type">you can employ the Visitor pattern to encode a sum type</a>. Another option is to use <a href="/2018/05/22/church-encoding">Church encoding</a>, which I've decided to do here. </p> <p> When choosing between Church encoding and the <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a> pattern, Visitor is more object-oriented (after all, it's an original <a href="/ref/dp">GoF</a> design pattern), but Church encoding has fewer moving parts. Since I was just doing an exercise, I went for the simpler implementation. </p> <p> An <code>Endpoint</code> object should allow one of two cases: <code>Open</code> or <code>Closed</code>. To avoid <a href="https://wiki.c2.com/?PrimitiveObsession">primitive obsession</a> I gave the class a <code>private</code> constructor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Endpoint</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;T&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;isClosed; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Endpoint</span>(T&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>,&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">isClosed</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.isClosed&nbsp;=&nbsp;isClosed; &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Since the constructor is <code>private</code> you need another way to create <code>Endpoint</code> objects. Two factory methods provide that affordance: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Endpoint&lt;T&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Closed</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Endpoint&lt;T&gt;.Closed(value); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Endpoint&lt;T&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Open</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Endpoint&lt;T&gt;.Open(value); }</pre> </p> <p> The heart of the Church encoding is the <code>Match</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;TResult&nbsp;<span style="font-weight:bold;color:#74531f;">Match</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;TResult&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">whenClosed</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;TResult&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">whenOpen</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(isClosed) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;whenClosed(value); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;whenOpen(value); }</pre> </p> <p> Such an API is an example of <a href="https://en.wikipedia.org/wiki/Poka-yoke">poka-yoke</a> because it obliges you to deal with both cases. The compiler will keep you honest: <em>Did you remember to deal with both the open and the closed case?</em> When calling the <code>Match</code> method, you must supply both arguments, or your code doesn't compile. <a href="https://blog.janestreet.com/effective-ml-video/">Make illegal states unrepresentable</a>. </p> <h3 id="484d1f121cc44050b76f23d92bca429c"> Containment <a href="#484d1f121cc44050b76f23d92bca429c">#</a> </h3> <p> With the <code>Endpoint</code> class in place, you can implement a <code>Range</code> class. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Range</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="color:blue;">where</span>&nbsp;T&nbsp;:&nbsp;IComparable&lt;T&gt;</pre> </p> <p> It made sense to me to constrain the <code>T</code> type argument to <code>IComparable&lt;T&gt;</code>, although it's possible that I could have deferred that constraint to the actual <code>Contains</code> method, like I did with <a href="/2024/01/08/a-range-kata-implementation-in-haskell">my Haskell implementation</a>. </p> <p> A <code>Range</code> holds two <code>Endpoint</code> values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Range</span>(Endpoint&lt;T&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">min</span>,&nbsp;Endpoint&lt;T&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">max</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.min&nbsp;=&nbsp;min; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.max&nbsp;=&nbsp;max; }</pre> </p> <p> The <code>Contains</code> method makes use of the built-in <a href="https://learn.microsoft.com/dotnet/api/system.linq.enumerable.all">All</a> method, using a <code>private</code> helper function as the predicate: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">IsInRange</span>(T&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;min.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whenClosed:&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&gt;&nbsp;max.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whenClosed:&nbsp;<span style="font-weight:bold;color:#1f377f;">h</span>&nbsp;=&gt;&nbsp;l.CompareTo(candidate)&nbsp;&lt;=&nbsp;0&nbsp;&amp;&amp;&nbsp;candidate.CompareTo(h)&nbsp;&lt;=&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whenOpen:&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">h</span>&nbsp;=&gt;&nbsp;l.CompareTo(candidate)&nbsp;&lt;=&nbsp;0&nbsp;&amp;&amp;&nbsp;candidate.CompareTo(h)&nbsp;&lt;&nbsp;&nbsp;0), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whenOpen:&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&gt;&nbsp;max.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whenClosed:&nbsp;<span style="font-weight:bold;color:#1f377f;">h</span>&nbsp;=&gt;&nbsp;l.CompareTo(candidate)&nbsp;&lt;&nbsp;&nbsp;0&nbsp;&amp;&amp;&nbsp;candidate.CompareTo(h)&nbsp;&lt;=&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whenOpen:&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">h</span>&nbsp;=&gt;&nbsp;l.CompareTo(candidate)&nbsp;&lt;&nbsp;&nbsp;0&nbsp;&amp;&amp;&nbsp;candidate.CompareTo(h)&nbsp;&lt;&nbsp;&nbsp;0)); }</pre> </p> <p> This implementation performs a nested <code>Match</code> to arrive at the appropriate answer. The code isn't as elegant or readable as its F# counterpart, but it comes with comparable compile-time safety. You can't forget a combination, because if you do, your code isn't going to compile. </p> <p> Still, you can't deny that C# involves more <a href="/2019/12/16/zone-of-ceremony">ceremony</a>. </p> <h3 id="54fd635d237d4f57891bda8ef5d13623"> Conclusion <a href="#54fd635d237d4f57891bda8ef5d13623">#</a> </h3> <p> Once you know how, it's not that difficult to port a functional design from F# or <a href="https://www.haskell.org/">Haskell</a> to a language like C#. The resulting code tends to be more complicated, but to a large degree, it's possible to retain the type safety. </p> <p> In this article you saw a sketch of how to make that transition, using the Range kata as an example. The resulting C# API is perfectly serviceable, as the test code demonstrates. </p> <p> Now that we have covered the fundamentals of the Range kata we have learned enough about it to go beyond the exercise and examine some more abstract properties. </p> <p> <strong>Next:</strong> <a href="/2024/02/12/range-as-a-functor">Range as a functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A Range kata implementation in F# https://blog.ploeh.dk/2024/01/15/a-range-kata-implementation-in-f 2024-01-15T07:20:00+00:00 Mark Seemann <div id="post"> <p> <em>This time with some property-based testing.</em> </p> <p> This article is an instalment in <a href="/2024/01/01/variations-of-the-range-kata">a short series of articles on the Range kata</a>. In the <a href="/2024/01/08/a-range-kata-implementation-in-haskell">previous article</a> I described my first attempt at the kata, and also complained that I had to think of test cases myself. When I find it tedious coming up with new test cases, I usually start to wonder if it'd be easier to use property-based testing. </p> <p> Thus, when I decided to revisit <a href="https://codingdojo.org/kata/Range/">the kata</a>, the variation that I was most interested in pursuing was to explore whether it would make sense to use property-based testing instead of a set of existing examples. </p> <p> Since I also wanted to do the second attempt in <a href="https://fsharp.org/">F#</a>, I had a choice between <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> and <a href="https://hedgehog.qa/">Hedgehog</a>. Each have their strengths and weaknesses, but since I already know FsCheck so well, I decided to go with Hedgehog. </p> <p> I also soon discovered that I had no interest in developing the full suite of capabilities implied by the kata. Instead, I decided to focus on just the data structure itself, as well as the <code>contains</code> function. As in the previous article, this function can also be used to cover the kata's <em>ContainsRange</em> feature. </p> <h3 id="81447639b1a54c8c9437b81f3856018f"> Getting started <a href="#81447639b1a54c8c9437b81f3856018f">#</a> </h3> <p> There's no rule that you can't combine property-based testing with test-driven development (TDD). On the contrary, that's how I often do it. In this exercise, I first wrote this test: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Closed&nbsp;range&nbsp;contains&nbsp;list``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;xs&nbsp;=&nbsp;Gen.int16&nbsp;(Range.linearBounded&nbsp;())&nbsp;|&gt;&nbsp;Gen.list&nbsp;(Range.linear&nbsp;1&nbsp;99) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;min&nbsp;=&nbsp;List.min&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;max&nbsp;=&nbsp;List.max&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;(Closed&nbsp;min,&nbsp;Closed&nbsp;max)&nbsp;|&gt;&nbsp;Range.contains&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;Assert.True&nbsp;(actual,&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;Range&nbsp;[%i,&nbsp;%i]&nbsp;expected&nbsp;to&nbsp;contain&nbsp;list.&quot;</span>&nbsp;min&nbsp;max)&nbsp;}</pre> </p> <p> We have to be careful when reading and understanding this code: There are two <code>Range</code> modules in action here! </p> <p> Hedgehog comes with a <code>Range</code> module that you must use to define how it samples values from <a href="https://en.wikipedia.org/wiki/Domain_of_a_function">domains</a>. Examples of that here are <code>Range.linearBounded</code> and <code>Range.linear</code>. </p> <p> On the other hand, I've defined <em>my</em> <code>contains</code> function in a <code>Range</code> module, too. As long as there's no ambiguity, the F# compiler doesn't have a problem with that. Since there's no <code>contains</code> function in the Hedgehog <code>Range</code> module, the F# compiler isn't confused. </p> <p> We humans, on the other hand, might be confused, and had this been a code base that I had to maintain for years, I might seriously consider whether I should rename my own <code>Range</code> module to something else, like <code>Interval</code>, perhaps. </p> <p> In any case, the first test (or property, if you will) uses a technique that I often use with property-based testing. I'm still searching for a catchy name for this, but here we may call it something like <em>reverse test-case assembly</em>. My <em>goal</em> is to test a predicate, and this particular property should verify that for a given <a href="https://en.wikipedia.org/wiki/Equivalence_class">Equivalence Class</a>, the predicate is always true. </p> <p> While we may think of an Equivalence Class as a set from which we pick test cases, I don't actually have a full enumeration of such a set. I can't have that, since that set is infinitely big. Instead of randomly picking values from a set that I can't fully populate, I instead carefully pick test case values in such a way that they would all belong to the same <a href="https://en.wikipedia.org/wiki/Partition_of_a_set">set partition</a> (Equivalence Class). </p> <p> The <a href="/2022/06/13/some-thoughts-on-naming-tests">test name suggests the test case</a>: I'd like to verify that given I have a closed range, when I ask it whether a list <em>within</em> that range is contained, then the answer is <em>true</em>. How do I pick such a test case? </p> <p> I do it in reverse. You can say that the sampling is the dual of the test. I start with a list (<code>xs</code>) and only then do I create a range that contains it. Since the first test case is for a closed range, the <code>min</code> and <code>max</code> values are sufficient to define such a range. </p> <p> How do I pass that property? </p> <p> Degenerately, as is often the case with TDD beginnings: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Range&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contains&nbsp;_&nbsp;_&nbsp;=&nbsp;<span style="color:blue;">true</span></pre> </p> <p> Even though the <code>Closed range contains list</code> property effectively executes a hundred test cases, the <a href="/2019/10/07/devils-advocate">Devil's Advocate</a> can easily ignore that and instead return hard-coded <code>true</code>. </p> <p> More properties are required to flesh out the behaviour of the function. </p> <h3 id="50564e684f074ae1956b106333320c9b"> Open range <a href="#50564e684f074ae1956b106333320c9b">#</a> </h3> <p> While I do keep the <a href="https://blog.cleancoder.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html">transformation priority premise</a> in mind when picking the next test (or, here, <em>property</em>), I'm rarely particularly analytic about it. Since the first property tests that a closed range barely contains a list of values from its minimum to its maximum, it seemed like a promising next step to consider the case where the range consisted of open endpoints. That was the second test I wrote, then: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Open&nbsp;range&nbsp;doesn&#39;t&nbsp;contain&nbsp;endpoints``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;min&nbsp;=&nbsp;Gen.int32&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;max&nbsp;=&nbsp;Gen.int32&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;(Open&nbsp;min,&nbsp;Open&nbsp;max)&nbsp;|&gt;&nbsp;Range.contains&nbsp;[min;&nbsp;max] &nbsp;&nbsp;&nbsp;&nbsp;Assert.False&nbsp;(actual,&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;Range&nbsp;(%i,&nbsp;%i)&nbsp;expected&nbsp;not&nbsp;to&nbsp;contain&nbsp;list.&quot;</span>&nbsp;min&nbsp;max)&nbsp;}</pre> </p> <p> This property simply states that if you query the <code>contains</code> predicate about a list that only contains the endpoints of an open range, then the answer is <code>false</code> because the endpoints are <code>Open</code>. </p> <p> One implementation that passes both tests is this one: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Range&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contains&nbsp;_&nbsp;endpoints&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;endpoints&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Open&nbsp;_,&nbsp;Open&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span></pre> </p> <p> This implementation is obviously still incorrect, but we have reason to believe that we're moving closer to something that will eventually work. </p> <h3 id="f01d9a05b57d470fac8f48bcfc85df4d"> Tick-tock <a href="#f01d9a05b57d470fac8f48bcfc85df4d">#</a> </h3> <p> In the spirit of the transformation priority premise, I've often found that when test-driving a predicate, I seem to fall into a tick-tock pattern where I alternate between tests for a <code>true</code> return value, followed by a test for a <code>false</code> return value, or the other way around. This was also the case here. The previous test was for a <code>false</code> value, so the third test requires <code>true</code> to be returned: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Open&nbsp;range&nbsp;contains&nbsp;list``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;xs&nbsp;=&nbsp;Gen.int64&nbsp;(Range.linearBounded&nbsp;())&nbsp;|&gt;&nbsp;Gen.list&nbsp;(Range.linear&nbsp;1&nbsp;99) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;min&nbsp;=&nbsp;List.min&nbsp;xs&nbsp;-&nbsp;1L &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;max&nbsp;=&nbsp;List.max&nbsp;xs&nbsp;+&nbsp;1L &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;(Open&nbsp;min,&nbsp;Open&nbsp;max)&nbsp;|&gt;&nbsp;Range.contains&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;Assert.True&nbsp;(actual,&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;Range&nbsp;(%i,&nbsp;%i)&nbsp;expected&nbsp;to&nbsp;contain&nbsp;list.&quot;</span>&nbsp;min&nbsp;max)&nbsp;}</pre> </p> <p> This then led to this implementation of the <code>contains</code> function: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Range&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contains&nbsp;ys&nbsp;endpoints&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;endpoints&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Open&nbsp;x,&nbsp;Open&nbsp;z&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ys&nbsp;|&gt;&nbsp;List.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;&lt;&nbsp;y&nbsp;&amp;&amp;&nbsp;y&nbsp;&lt;&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span></pre> </p> <p> Following up on the above <code>true</code>-demanding test, I added one that tested a <code>false</code> scenario: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Open-closed&nbsp;range&nbsp;doesn&#39;t&nbsp;contain&nbsp;endpoints``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;min&nbsp;=&nbsp;Gen.int16&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;max&nbsp;=&nbsp;Gen.int16&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;(Open&nbsp;min,&nbsp;Closed&nbsp;max)&nbsp;|&gt;&nbsp;Range.contains&nbsp;[min;&nbsp;max] &nbsp;&nbsp;&nbsp;&nbsp;Assert.False&nbsp;(actual,&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;Range&nbsp;(%i,&nbsp;%i]&nbsp;expected&nbsp;not&nbsp;to&nbsp;contain&nbsp;list.&quot;</span>&nbsp;min&nbsp;max)&nbsp;}</pre> </p> <p> This again led to this implementation: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Range&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contains&nbsp;ys&nbsp;endpoints&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;endpoints&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Open&nbsp;x,&nbsp;Open&nbsp;z&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ys&nbsp;|&gt;&nbsp;List.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;&lt;&nbsp;y&nbsp;&amp;&amp;&nbsp;y&nbsp;&lt;&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Open&nbsp;x,&nbsp;Closed&nbsp;z&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span></pre> </p> <p> I had to add four more tests before I felt confident that I had the right implementation. I'm not going to show them all here, but you can look at the <a href="https://github.com/ploeh/RangeFSharp">repository on GitHub</a> if you're interested in the interim steps. </p> <h3 id="0cd66550fe5a43b0a7e8a6d3a2b0ea32"> Types and functionality <a href="#0cd66550fe5a43b0a7e8a6d3a2b0ea32">#</a> </h3> <p> So far I had treated a range as a pair (two-tuple), just as I had done with the code in <a href="/2024/01/08/a-range-kata-implementation-in-haskell">my first attempt</a>. I did, however, have a few other things planned for this code base, so I introduced a set of explicit types: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Endpoint&lt;&#39;a&gt;&nbsp;=&nbsp;Open&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a&nbsp;|&nbsp;Closed&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a <span style="color:blue;">type</span>&nbsp;Range&lt;&#39;a&gt;&nbsp;=&nbsp;{&nbsp;LowerBound&nbsp;:&nbsp;Endpoint&lt;&#39;a&gt;;&nbsp;UpperBound&nbsp;:&nbsp;Endpoint&lt;&#39;a&gt;&nbsp;}</pre> </p> <p> The <code>Range</code> record type is isomorphic to a pair of <code>Endpoint</code> values, so it's not strictly required, but does make things <a href="https://peps.python.org/pep-0020/">more explicit</a>. </p> <p> To support the new type, I added an <code>ofEndpoints</code> function, and finalized the implementation of <code>contains</code>: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Range&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ofEndpoints&nbsp;(lowerBound,&nbsp;upperBound)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;LowerBound&nbsp;=&nbsp;lowerBound;&nbsp;UpperBound&nbsp;=&nbsp;upperBound&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contains&nbsp;ys&nbsp;r&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;r.LowerBound,&nbsp;r.UpperBound&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Open&nbsp;x,&nbsp;&nbsp;&nbsp;Open&nbsp;z&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ys&nbsp;|&gt;&nbsp;List.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;&nbsp;&lt;&nbsp;y&nbsp;&amp;&amp;&nbsp;y&nbsp;&nbsp;&lt;&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Open&nbsp;x,&nbsp;Closed&nbsp;z&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ys&nbsp;|&gt;&nbsp;List.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;&nbsp;&lt;&nbsp;y&nbsp;&amp;&amp;&nbsp;y&nbsp;&lt;=&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Closed&nbsp;x,&nbsp;&nbsp;&nbsp;Open&nbsp;z&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ys&nbsp;|&gt;&nbsp;List.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;&lt;=&nbsp;y&nbsp;&amp;&amp;&nbsp;y&nbsp;&nbsp;&lt;&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Closed&nbsp;x,&nbsp;Closed&nbsp;z&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ys&nbsp;|&gt;&nbsp;List.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;&lt;=&nbsp;y&nbsp;&amp;&amp;&nbsp;y&nbsp;&lt;=&nbsp;z)</pre> </p> <p> As is so often the case in F#, pattern matching makes such functions a pleasure to implement. </p> <h3 id="c00252811495433987c37f7bcfc751a5"> Conclusion <a href="#c00252811495433987c37f7bcfc751a5">#</a> </h3> <p> I was curious whether using property-based testing would make the development process of the Range kata simpler. While each property was simple, I still had to write eight of them before I felt I'd fully described the problem. This doesn't seem like much of an improvement over the example-driven approach I took the first time around. It seems to be a comparable amount of code, and on one hand a property is more abstract than an example, but on the hand usually also covers more ground. I feel more confident that this implementation works, because I know that it's being exercised more rigorously. </p> <p> When I find myself writing a property per branch, so to speak, I always feel that I missed a better way to describe the problem. As an example, for years <a href="https://youtu.be/2oN9caQflJ8?si=em1VvFqYFA_AjDlk">I would demonstrate</a> how to test <a href="https://codingdojo.org/kata/FizzBuzz/">the FizzBuzz kata</a> with property-based testing by dividing the problem into Equivalence Classes and then writing a property for each partition. Just as I've done here. This is usually possible, but smells of being too coupled to the implementation. </p> <p> Sometimes, if you think about the problem long enough, you may be able to produce an alternative set of properties that describe the problem in a way that's entirely decoupled from the implementation. After years, <a href="/2021/06/28/property-based-testing-is-not-the-same-as-partition-testing">I finally managed to do that with the FizzBuzz kata</a>. </p> <p> I didn't succeed doing that with the Range kata this time around, but maybe later. </p> <p> <strong>Next:</strong> <a href="/2024/01/22/a-range-kata-implementation-in-c">A Range kata implementation in C#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A Range kata implementation in Haskell https://blog.ploeh.dk/2024/01/08/a-range-kata-implementation-in-haskell 2024-01-08T07:06:00+00:00 Mark Seemann <div id="post"> <p> <em>A first crack at the exercise.</em> </p> <p> This article is an instalment in <a href="/2024/01/01/variations-of-the-range-kata">a short series of articles on the Range kata</a>. Here I describe my first attempt at the exercise. As I usually advise people <a href="/2020/01/13/on-doing-katas">on doing katas</a>, the first time you try your hand at a kata, use the language with which you're most comfortable. To be honest, I may be most habituated to C#, having programmed in it since 2002, but on the other hand, I currently 'think in <a href="https://www.haskell.org/">Haskell</a>', and am often frustrated with C#'s lack of structural equality, higher-order abstractions, and support for functional expressions. </p> <p> Thus, I usually start with Haskell even though I always find myself struggling with the ecosystem. If you do, too, the source code is <a href="https://github.com/ploeh/RangeHaskell">available on GitHub</a>. </p> <p> I took my own advice by setting out with the explicit intent to follow <a href="https://codingdojo.org/kata/Range/">the Range kata description</a> as closely as possible. This kata doesn't beat about the bush, but instead just dumps a set of test cases on you. It wasn't clear if this is the most useful set of tests, or whether the order in which they're represented is the one most conducive to a good experience of test-driven development, but there was only one way to find out. </p> <p> I quickly learned, however, that the suggested test cases were insufficient in describing the behaviour in enough details. </p> <h3 id="287256d6f0fe412585cce5f16fcf5363"> Containment <a href="#287256d6f0fe412585cce5f16fcf5363">#</a> </h3> <p> I started by adding the first two test cases as <a href="/2018/05/07/inlined-hunit-test-lists">inlined HUnit test lists</a>: </p> <p> <pre><span style="color:#a31515;">&quot;integer&nbsp;range&nbsp;contains&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(r,&nbsp;candidate,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Open&nbsp;6),&nbsp;[2,4],&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Open&nbsp;6),&nbsp;[-1,1,6,10],&nbsp;False) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;r&nbsp;`contains`&nbsp;candidate &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> I wasn't particularly keen on going full <a href="/2019/10/07/devils-advocate">Devil's Advocate</a> on the exercise. I could, on the other hand, trivially pass both tests with this obviously degenerate implementation: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;Data.List <span style="color:blue;">data</span>&nbsp;Endpoint&nbsp;a&nbsp;=&nbsp;Open&nbsp;a&nbsp;|&nbsp;Closed&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) contains&nbsp;_&nbsp;candidate&nbsp;=&nbsp;[2]&nbsp;`isPrefixOf`&nbsp;candidate</pre> </p> <p> Reluctantly, I had to invent some additional test cases: </p> <p> <pre><span style="color:#a31515;">&quot;integer&nbsp;range&nbsp;contains&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(r,&nbsp;candidate,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;&nbsp;&nbsp;2&nbsp;,&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;6),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,4],&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;&nbsp;&nbsp;2&nbsp;,&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;6),&nbsp;[-1,1,6,10],&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;(-1),&nbsp;Closed&nbsp;10),&nbsp;[-1,1,6,10],&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;(-1),&nbsp;&nbsp;&nbsp;Open&nbsp;10),&nbsp;[-1,1,6,10],&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;(-1),&nbsp;&nbsp;&nbsp;Open&nbsp;10),&nbsp;&nbsp;[-1,1,6,9],&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;&nbsp;&nbsp;2,&nbsp;&nbsp;Closed&nbsp;&nbsp;6),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3,5,6],&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;&nbsp;&nbsp;2,&nbsp;&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;6),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,5],&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;&nbsp;&nbsp;2,&nbsp;&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;6),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">[]</span>,&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;&nbsp;&nbsp;2,&nbsp;&nbsp;Closed&nbsp;&nbsp;6),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3,7,4],&nbsp;False) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;r&nbsp;`contains`&nbsp;candidate &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> This was when I began to wonder whether it would have been easier to use property-based testing. That would entail, however, a departure from the kata's suggested test cases, so I decided to stick to the plan and then perhaps return to property-based testing when repeating the exercise. </p> <p> Ultimately I implemented the <code>contains</code> function this way: </p> <p> <pre><span style="color:#2b91af;">contains</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Foldable</span>&nbsp;t,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(<span style="color:blue;">Endpoint</span>&nbsp;a,&nbsp;<span style="color:blue;">Endpoint</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;t&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> contains&nbsp;(lowerBound,&nbsp;upperBound)&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;isHighEnough&nbsp;=&nbsp;<span style="color:blue;">case</span>&nbsp;lowerBound&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Closed&nbsp;x&nbsp;-&gt;&nbsp;(x&nbsp;&lt;=) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;&nbsp;x&nbsp;-&gt;&nbsp;(x&nbsp;&lt;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isLowEnough&nbsp;=&nbsp;<span style="color:blue;">case</span>&nbsp;upperBound&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Closed&nbsp;y&nbsp;-&gt;&nbsp;(&lt;=&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;&nbsp;y&nbsp;-&gt;&nbsp;&nbsp;(&lt;&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isContained&nbsp;x&nbsp;=&nbsp;isHighEnough&nbsp;x&nbsp;&amp;&amp;&nbsp;isLowEnough&nbsp;x &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">all</span>&nbsp;isContained</pre> </p> <p> In some ways it seems a bit verbose to me, but I couldn't easily think of a simpler implementation. </p> <p> One of the features I find so fascinating about Haskell is how <em>general</em> it enables me to be. While the tests use integers for concision, the <code>contains</code> function works with any <code>Ord</code> instance; not only <code>Integer</code>, but also <code>Double</code>, <code>Word</code>, <code>Day</code>, <code>TimeOfDay</code>, or some new type I can't even predict. </p> <h3 id="67c8d29aeb5d4c2ca18b4a6664cf6af8"> All points <a href="#67c8d29aeb5d4c2ca18b4a6664cf6af8">#</a> </h3> <p> The next function suggested by the kata is a function to enumerate all points in a range. There's only a single test case, so again I added some more: </p> <p> <pre><span style="color:#a31515;">&quot;getAllPoints&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(r,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;&nbsp;&nbsp;Open&nbsp;6),&nbsp;[2..5]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;4,&nbsp;&nbsp;&nbsp;Open&nbsp;8),&nbsp;[4..7]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Closed&nbsp;6),&nbsp;[2..6]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;4,&nbsp;Closed&nbsp;8),&nbsp;[4..8]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;2,&nbsp;Closed&nbsp;6),&nbsp;[3..6]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;4,&nbsp;Closed&nbsp;8),&nbsp;[5..8]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;2,&nbsp;&nbsp;&nbsp;Open&nbsp;6),&nbsp;[3..5]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;4,&nbsp;&nbsp;&nbsp;Open&nbsp;8),&nbsp;[5..7]) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;allPoints&nbsp;r &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> Ultimately, after I'd implemented the <em>next</em> feature, I refactored the <code>allPoints</code> function to make use of it, and it became a simple one-liner: </p> <p> <pre><span style="color:#2b91af;">allPoints</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Enum</span>&nbsp;a,&nbsp;<span style="color:blue;">Num</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(<span style="color:blue;">Endpoint</span>&nbsp;a,&nbsp;<span style="color:blue;">Endpoint</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] allPoints&nbsp;=&nbsp;<span style="color:blue;">uncurry</span>&nbsp;<span style="color:blue;">enumFromTo</span>&nbsp;.&nbsp;endpoints</pre> </p> <p> The <code>allPoints</code> function also enabled me to express the kata's <em>ContainsRange</em> test cases without introducing a new API: </p> <p> <pre><span style="color:#a31515;">&quot;ContainsRange&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(r,&nbsp;candidate,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;5),&nbsp;allPoints&nbsp;(Closed&nbsp;7,&nbsp;Open&nbsp;&nbsp;&nbsp;10),&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;5),&nbsp;allPoints&nbsp;(Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;&nbsp;10),&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;3,&nbsp;&nbsp;&nbsp;Open&nbsp;&nbsp;5),&nbsp;allPoints&nbsp;(Closed&nbsp;2,&nbsp;Open&nbsp;&nbsp;&nbsp;10),&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;&nbsp;&nbsp;Open&nbsp;10),&nbsp;allPoints&nbsp;(Closed&nbsp;3,&nbsp;Closed&nbsp;&nbsp;5),&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;3,&nbsp;Closed&nbsp;&nbsp;5),&nbsp;allPoints&nbsp;(Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;&nbsp;&nbsp;5),&nbsp;&nbsp;True) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;r&nbsp;`contains`&nbsp;candidate &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> As I've already mentioned, the above implementation of <code>allPoints</code> is based on the next feature, <code>endpoints</code>. </p> <h3 id="a16cc4c45e614bb9a726c14ef19afc8f"> Endpoints <a href="#a16cc4c45e614bb9a726c14ef19afc8f">#</a> </h3> <p> The kata also suggests a function to return the two endpoints of a range, as well as some test cases to describe it. Once more, I had to add more test cases to adequately describe the desired functionality: </p> <p> <pre><span style="color:#a31515;">&quot;endPoints&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(r,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;&nbsp;&nbsp;Open&nbsp;6),&nbsp;(2,&nbsp;5)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;1,&nbsp;&nbsp;&nbsp;Open&nbsp;7),&nbsp;(1,&nbsp;6)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Closed&nbsp;6),&nbsp;(2,&nbsp;6)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;1,&nbsp;Closed&nbsp;7),&nbsp;(1,&nbsp;7)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;2,&nbsp;&nbsp;&nbsp;Open&nbsp;6),&nbsp;(3,&nbsp;5)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;1,&nbsp;&nbsp;&nbsp;Open&nbsp;7),&nbsp;(2,&nbsp;6)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;2,&nbsp;Closed&nbsp;6),&nbsp;(3,&nbsp;6)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&nbsp;&nbsp;Open&nbsp;1,&nbsp;Closed&nbsp;7),&nbsp;(2,&nbsp;7)) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;endpoints&nbsp;r &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> The implementation is fairly trivial: </p> <p> <pre><span style="color:#2b91af;">endpoints</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Num</span>&nbsp;a1,&nbsp;<span style="color:blue;">Num</span>&nbsp;a2)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(<span style="color:blue;">Endpoint</span>&nbsp;a2,&nbsp;<span style="color:blue;">Endpoint</span>&nbsp;a1)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a2,&nbsp;a1) endpoints&nbsp;(Closed&nbsp;x,&nbsp;Closed&nbsp;y)&nbsp;=&nbsp;(x&nbsp;&nbsp;,&nbsp;y) endpoints&nbsp;(Closed&nbsp;x,&nbsp;&nbsp;&nbsp;Open&nbsp;y)&nbsp;=&nbsp;(x&nbsp;&nbsp;,&nbsp;y-1) endpoints&nbsp;(&nbsp;&nbsp;Open&nbsp;x,&nbsp;Closed&nbsp;y)&nbsp;=&nbsp;(x+1,&nbsp;y) endpoints&nbsp;(&nbsp;&nbsp;Open&nbsp;x,&nbsp;&nbsp;&nbsp;Open&nbsp;y)&nbsp;=&nbsp;(x+1,&nbsp;y-1)</pre> </p> <p> One attractive quality of <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> is that the 'algebra' of the type(s) tell you how many cases you need to pattern-match against. Since I'm treating a range as a pair of <code>Endpoint</code> values, and since each <code>Endpoint</code> can be one of two cases (<code>Open</code> or <code>Closed</code>), there's exactly 2 * 2 = 4 possible combinations (since a tuple is a <a href="https://en.wikipedia.org/wiki/Product_type">product type</a>). </p> <p> That fits with the number of pattern-matches required to implement the function. </p> <h3 id="c2b611a0c9ba494b9ccc30c5cd3ec4e8"> Overlapping ranges <a href="#c2b611a0c9ba494b9ccc30c5cd3ec4e8">#</a> </h3> <p> The final interesting feature is a predicate to determine whether one range overlaps another. As has become a refrain by now, I didn't find the suggested test cases sufficient to describe the desired behaviour, so I had to add a few more: </p> <p> <pre><span style="color:#a31515;">&quot;overlapsRange&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(r,&nbsp;candidate,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;7,&nbsp;Open&nbsp;10),&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Open&nbsp;10),&nbsp;(Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;3,&nbsp;Open&nbsp;10),&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;2,&nbsp;Open&nbsp;10),&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;1,&nbsp;Open&nbsp;&nbsp;3),&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;5,&nbsp;Open&nbsp;&nbsp;7),&nbsp;False) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;r&nbsp;`overlaps`&nbsp;candidate &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> I'm not entirely happy with the implementation: </p> <p> <pre><span style="color:#2b91af;">overlaps</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Ord</span>&nbsp;a1,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a2)&nbsp;<span style="color:blue;">=&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Endpoint&nbsp;a1,&nbsp;Endpoint&nbsp;a2)&nbsp;-&gt;&nbsp;(Endpoint&nbsp;a2,&nbsp;Endpoint&nbsp;a1)&nbsp;-&gt;&nbsp;Bool overlaps&nbsp;(l1,&nbsp;h1)&nbsp;(l2,&nbsp;h2)&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;less&nbsp;(Closed&nbsp;x)&nbsp;(Closed&nbsp;y)&nbsp;=&nbsp;x&nbsp;&lt;=&nbsp;y &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;less&nbsp;(Closed&nbsp;x)&nbsp;&nbsp;&nbsp;(Open&nbsp;y)&nbsp;=&nbsp;x&nbsp;&lt;&nbsp;&nbsp;y &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;less&nbsp;&nbsp;&nbsp;(Open&nbsp;x)&nbsp;(Closed&nbsp;y)&nbsp;=&nbsp;x&nbsp;&lt;&nbsp;&nbsp;y &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;less&nbsp;&nbsp;&nbsp;(Open&nbsp;x)&nbsp;&nbsp;&nbsp;(Open&nbsp;y)&nbsp;=&nbsp;x&nbsp;&lt;&nbsp;&nbsp;y &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;l1&nbsp;`less`&nbsp;h2&nbsp;&amp;&amp;&nbsp;l2&nbsp;`less`&nbsp;h1</pre> </p> <p> Noth that the code presented here is problematic in isolation, but if you compare it to the above <code>contains</code> function, there seems to be some repetition going on. Still, it's not <em>quite</em> the same, but the code looks similar enough that it bothers me. I feel that some kind of abstraction is sitting there, right before my nose, mocking me because I can't see it. Still, the code isn't completely duplicated, and even if it was, I can always invoke the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">rule of three</a> and let it remain as it is. </p> <p> Which is ultimately what I did. </p> <h3 id="c09e7c817ecc48c097d6660a5438f5e0"> Equality <a href="#c09e7c817ecc48c097d6660a5438f5e0">#</a> </h3> <p> The kata also suggests some test cases to verify that it's possible to compare two ranges for equality. Dutifully I added those test cases to the code base, even though I knew that they'd automatically pass. </p> <p> <pre><span style="color:#a31515;">&quot;Equals&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(x,&nbsp;y,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;&nbsp;True), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Open&nbsp;10),&nbsp;(Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;2,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;3,&nbsp;Open&nbsp;10),&nbsp;False), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((Closed&nbsp;3,&nbsp;Open&nbsp;&nbsp;5),&nbsp;(Closed&nbsp;2,&nbsp;Open&nbsp;10),&nbsp;False) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;x&nbsp;==&nbsp;y &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> In the beginning of this article, I called attention to C#'s regrettable lack of structural equality. Here's an example of what I mean. In Haskell, these tests automatically pass because <code>Endpoint</code> is an <code>Eq</code> instance (by declaration), and all pairs of <code>Eq</code> instances are themselves <code>Eq</code> instances. Simple, elegant, powerful. </p> <h3 id="094e6dd4c07e40739d6fca4945dc7018"> Conclusion <a href="#094e6dd4c07e40739d6fca4945dc7018">#</a> </h3> <p> As a first pass at the (admittedly uncomplicated) Range kata, I tried to follow the 'plan' implied by the kata description's test cases. I quickly became frustrated with their lack of completion. They were adequate in indicating to a human (me) what the desired behaviour should be, but insufficient to satisfactorily describe the desired behaviour. </p> <p> I could, of course, have stuck with only those test cases, and instead of employing the Devil's Advocate technique (which I actively tried to avoid) made an honest effort to implement the functionality. </p> <p> The things is, however, that <a href="/2023/03/20/on-trust-in-software-development">I don't trust myself</a>. At its essence, the Range kata is all about edge cases, which are where most bugs tend to lurk. Thus, these are exactly the cases that should be covered by tests. </p> <p> Having made enough 'dumb' programming mistakes during my career, I didn't trust myself to be able to write correct implementations without more test coverage than originally suggested. That's the reason I added more tests. </p> <p> On the other hand, I more than once speculated whether property-based testing would make this work easier. I decided to pursue that idea during my second pass at the kata. </p> <p> <strong>Next:</strong> <a href="/2024/01/15/a-range-kata-implementation-in-f">A Range kata implementation in F#</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f768e9d0ec73603ff40542ae07a1a9bd"> <div class="comment-author"><a href="https://github.com/mormegil-cz">Petr Kadlec</a> <a href="#f768e9d0ec73603ff40542ae07a1a9bd">#</a></div> <div class="comment-content"> <p> I’d have another test case for the Equality function: <code>((Open 2, Open 6), (Closed 3, Closed 5), True)</code>. While it is nice Haskell provides (automatic) structural equality, I don’t think we want to say that the (2, 6) range (on integers!) is something else than the [3, 5] range. </p> <p> But yes, this opens a can of worms: While (2, 6) = [3, 5] on integers, (2.0, 6.0) is obviously different than [3.0, 5.0] (on reals/Doubles/…). I have no idea: In Haskell, could you write an implementation of a function which would behave differently depending on whether the type argument belongs to a typeclass or not? </p> </div> <div class="comment-date">2024-01-09 13:38 UTC</div> </div> <div class="comment" id="9d0f60b0a2654424b10d264cfd8b6c96"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9d0f60b0a2654424b10d264cfd8b6c96">#</a></div> <div class="comment-content"> <p> Petr, thank you for writing. I don't think I'd add that (or similar) test cases, but it's a judgment call, and it's partly language-specific. What you're suggesting is to consider things that are <em>equivalent</em> equal. I agree that for integers this would be the case, but it wouldn't be for rational numbers, or floating points (or real numbers, if we had those in programming). </p> <p> In Haskell it wouldn't really be idiomatic, because equality is defined by the <code>Eq</code> type class, and most types just go with the default implementation. What you suggest requires writing an explicit <code>Eq</code> instance for <code>Endpoint</code>. It'd be possible, but then you'd have to deal explicitly with the various integer representations separately from other representations that use floating points. </p> <p> The distinction between <em>equivalence</em> and <em>equality</em> is largely artificial or a convenient hand wave. To explain what I mean, consider mathematical expressions. Obviously, <em>3 + 1</em> is equal to <em>2 + 2</em> when evaluated, but they're different <em>expressions</em>. Thus, on an expression level, we don't consider those two expressions equal. I think of the integer ranges <em>(2, 6)</em> and <em>[3, 6]</em> the same way. They evaluate to the same, but there aren't equal. </p> <p> I don't think that this is a strong argument, mind. In other programming languages, I might arrive at a different decision. It also matters what client code needs to <em>do</em> with the API. In any case, the decision to not consider <em>equivalence</em> the same as <em>equality</em> is congruent with how Haskell works. </p> <p> The existence of floating points and rational numbers, however, opens another can of worms that I happily glossed over, since I had a completely different goal with the kata than producing a reusable library. </p> <p> Haskell actually supports rational numbers with the <code>%</code> operator: </p> <p> <pre>ghci&gt; 1%2 1 % 2</pre> </p> <p> This value represents ½, to be explicit. </p> <p> Unfortunately, according to the specification (or, at least, <a href="https://hackage.haskell.org/package/base/docs/GHC-Enum.html#v:succ">the documentation</a>) of the <code>Enum</code> type class, the two 'movement' operations <code>succ</code> and <code>pred</code> jump by increments of <em>1</em>: </p> <p> <pre>ghci&gt; succ $ 1%2 3 % 2 ghci&gt; succ $ succ $ 1%2 5 % 2</pre> </p> <p> The same is the case with floating points: </p> <p> <pre>ghci&gt; succ 1.5 2.5 ghci&gt; succ $ succ 1.5 3.5</pre> </p> <p> This is unfortunate when it comes to floating points, since it would be possible to enumerate all floating points in a range. (For example, if a <a href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format">single-precision floating point</a> occupies 32 bits, there's a finite number of them, and you can enumerate them.) </p> <p> As <a href="https://twitter.com/sonatsuer/status/1744326173524394372">Sonat Süer points out</a>, this means that the <code>allPoints</code> function is fundamentally broken for floating points and rational numbers (and possibly other types as well). </p> <p> One way around that in Haskell would be to introduce a <em>new</em> type class for the purpose of truly enumerating ranges, and either implement it correctly for floating points, or explicitly avoid making <code>Float</code> and <code>Double</code> instances of that new type class. This, on the other hand, would have the downside that all of a sudden, the <code>allPoints</code> function wouldn't support any custom type of which I, as the implementer, is unaware. </p> <p> If this was a library that I'd actually have to ship as a reusable API, I think I'd start by <em>not</em> including the <code>allPoints</code> function, and then see if anyone asks for it. If or when that happens, I'd begin a process to chart why people need it, and what could be done to serve those needs in a useful and mathematically consistent manner. </p> </div> <div class="comment-date">2024-01-13 19:51 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Variations of the Range kata https://blog.ploeh.dk/2024/01/01/variations-of-the-range-kata 2024-01-01T17:00:00+00:00 Mark Seemann <div id="post"> <p> <em>In the languages I usually employ.</em> </p> <p> The <a href="https://codingdojo.org/kata/Range/">Range kata</a> is succinct, bordering on the spartan in both description and requirements. To be honest, it's hardly the most inspiring kata available, and yet it may help showcase a few interesting points about software design in general. It's what it demonstrates about <a href="/2018/03/22/functors">functors</a> that makes it marginally interesting. </p> <p> In this short article series I first cover a few incarnations of the kata in my usual programming languages, and then conclude by looking at <em>range</em> as a functor. </p> <p> The article series contains the following articles: </p> <ul> <li><a href="/2024/01/08/a-range-kata-implementation-in-haskell">A Range kata implementation in Haskell</a></li> <li><a href="/2024/01/15/a-range-kata-implementation-in-f">A Range kata implementation in F#</a></li> <li><a href="/2024/01/22/a-range-kata-implementation-in-c">A Range kata implementation in C#</a></li> <li><a href="/2024/02/12/range-as-a-functor">Range as a functor</a></li> </ul> <p> I didn't take the same approaches through all three exercises. An important point about <a href="/2020/01/13/on-doing-katas">doing katas</a> is to learn something, and when you've done the kata once, you've already gained some knowledge that can't easily be unlearned. Thus, on the second, or third time through, it's only natural to apply that knowledge, but then try different tactics to solve the problem in a different way. That's what I did here, starting with <a href="https://www.haskell.org/">Haskell</a>, proceeding with <a href="https://fsharp.org/">F#</a>, and concluding with C#. </p> <p> <strong>Next:</strong> <a href="/2024/01/08/a-range-kata-implementation-in-haskell">A Range kata implementation in Haskell</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Serializing restaurant tables in C# https://blog.ploeh.dk/2023/12/25/serializing-restaurant-tables-in-c 2023-12-25T11:42:00+00:00 Mark Seemann <div id="post"> <p> <em>Using System.Text.Json, with and without Reflection.</em> </p> <p> This article is part of a short series of articles about <a href="/2023/12/04/serialization-with-and-without-reflection">serialization with and without Reflection</a>. In this instalment I'll explore some options for serializing <a href="https://en.wikipedia.org/wiki/JSON">JSON</a> with C# using the API built into .NET: <a href="https://learn.microsoft.com/dotnet/api/system.text.json">System.Text.Json</a>. I'm not going use <a href="https://www.newtonsoft.com/json">Json.NET</a> in this article, but I've <a href="/2022/01/03/to-id-or-not-to-id">done similar things with that library</a> in the past, so what's here is, at least, somewhat generalizable. </p> <p> Since the API is the same, the only difference from <a href="/2023/12/18/serializing-restaurant-tables-in-f">the previous article</a> is the language syntax. </p> <h3 id="e949466d51f647bfbee9016d551d9b78"> Natural numbers <a href="#e949466d51f647bfbee9016d551d9b78">#</a> </h3> <p> Before we start investigating how to serialize to and from JSON, we must have something to serialize. As described in the <a href="/2023/12/04/serialization-with-and-without-reflection">introductory article</a> we'd like to parse and write restaurant table configurations like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;singleTable&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;16, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;minimalReservation&quot;</span>:&nbsp;10 &nbsp;&nbsp;} }</pre> </p> <p> On the other hand, I'd like to represent the Domain Model in a way that <a href="/encapsulation-and-solid">encapsulates the rules</a> governing the model, <a href="https://blog.janestreet.com/effective-ml-video/">making illegal states unrepresentable</a>. Even though that's a catchphrase associated with functional programming, it applies equally well to a statically typed object-oriented language like C#. </p> <p> As the first step, we observe that the numbers involved are all <a href="https://en.wikipedia.org/wiki/Natural_number">natural numbers</a>. In C# it's rarer to define <a href="https://www.hillelwayne.com/post/constructive/">predicative data types</a> than in a language like <a href="https://fsharp.org/">F#</a>, but people should do it more. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span>&nbsp;:&nbsp;IEquatable&lt;NaturalNumber&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(value&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(value), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Value&nbsp;must&nbsp;be&nbsp;a&nbsp;natural&nbsp;number&nbsp;greater&nbsp;than&nbsp;zero.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;NaturalNumber?&nbsp;<span style="font-weight:bold;color:#74531f;">TryCreate</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(candidate&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;NaturalNumber(candidate); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;&lt;(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">left</span>,&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">right</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;left.value&nbsp;&lt;&nbsp;right.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;&gt;(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">left</span>,&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">right</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;left.value&nbsp;&gt;&nbsp;right.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;&lt;=(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">left</span>,&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">right</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;left.value&nbsp;&lt;=&nbsp;right.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;&gt;=(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">left</span>,&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">right</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;left.value&nbsp;&gt;=&nbsp;right.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;==(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">left</span>,&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">right</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;left.value&nbsp;==&nbsp;right.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;!=(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">left</span>,&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">right</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;left.value&nbsp;!=&nbsp;right.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">explicit</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;<span style="color:blue;">int</span>(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">number</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;number.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Equals</span>(<span style="color:blue;">object</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">number</span>&nbsp;&amp;&amp;&nbsp;Equals(number); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Equals</span>(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">other</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;value&nbsp;==&nbsp;other.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetHashCode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;HashCode.Combine(value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> When comparing all that boilerplate code to the <a href="/2023/12/18/serializing-restaurant-tables-in-f">three lines required to achieve the same result in F#</a>, it seems, at first glance, understandable that C# developers rarely reach for that option. Still, <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">typing is not a programming bottleneck</a>, and most of that code was generated by a combination of Visual Studio and <a href="https://github.com/features/copilot">GitHub Copilot</a>. </p> <p> The <code>TryCreate</code> method may not be <em>strictly</em> necessary, but I consider it good practice to give client code a way to perform a fault-prone operation in a safe manner, without having to resort to a <code>try/catch</code> construct. </p> <p> That's it for natural numbers. 72 lines of code. Compare that to <a href="/2023/12/18/serializing-restaurant-tables-in-f">the F# implementation</a>, which required three lines of code. Syntax does matter. </p> <h3 id="542d19e6713f46d79cfc013fc577980a"> Domain Model <a href="#542d19e6713f46d79cfc013fc577980a">#</a> </h3> <p> Modelling a restaurant table follows in the same vein. One invariant I would like to enforce is that for a 'single' table, the minimal reservation should be a <code>NaturalNumber</code> less than or equal to the table's capacity. It doesn't make sense to configure a table for four with a minimum reservation of six. </p> <p> In the same spirit as above, then, define this type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Table</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;NaturalNumber&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;NaturalNumber?&nbsp;minimalReservation; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Table</span>(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>,&nbsp;NaturalNumber?&nbsp;<span style="font-weight:bold;color:#1f377f;">minimalReservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.minimalReservation&nbsp;=&nbsp;minimalReservation; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table?&nbsp;<span style="font-weight:bold;color:#74531f;">TryCreateSingle</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">minimalReservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">cap</span>&nbsp;=&nbsp;NaturalNumber.TryCreate(capacity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(cap&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">min</span>&nbsp;=&nbsp;NaturalNumber.TryCreate(minimalReservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(min&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(cap&nbsp;&lt;&nbsp;min) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Table(cap.Value,&nbsp;min.Value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table?&nbsp;<span style="font-weight:bold;color:#74531f;">TryCreateCommunal</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">cap</span>&nbsp;=&nbsp;NaturalNumber.TryCreate(capacity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(cap&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Table(cap.Value,&nbsp;<span style="color:blue;">null</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="font-weight:bold;color:#74531f;">Accept</span>&lt;<span style="color:#2b91af;">T</span>&gt;(ITableVisitor&lt;T&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">visitor</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(minimalReservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;visitor.VisitCommunal(capacity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;visitor.VisitSingle(capacity,&nbsp;minimalReservation.Value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Here I've <a href="/2018/06/25/visitor-as-a-sum-type">Visitor-encoded</a> the <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> that <code>Table</code> is. It can either be a 'single' table or a communal table. </p> <p> Notice that <code>TryCreateSingle</code> checks the invariant that the <code>capacity</code> must be greater than or equal to the <code>minimalReservation</code>. </p> <p> The point of this little exercise, so far, is that it <em>encapsulates</em> the contract implied by the Domain Model. It does this by using the static type system to its advantage. </p> <h3 id="13ac203cee494ec18420959fbad03003"> JSON serialization by hand <a href="#13ac203cee494ec18420959fbad03003">#</a> </h3> <p> At the boundaries of applications, however, <a href="/2023/10/16/at-the-boundaries-static-types-are-illusory">there are no static types</a>. Is the static type system still useful in that situation? </p> <p> For a long time, the most popular .NET library for JSON serialization was <a href="https://www.newtonsoft.com/json">Json.NET</a>, but these days I find the built-in API offered in the <a href="https://learn.microsoft.com/dotnet/api/system.text.json">System.Text.Json</a> namespace adequate. This is also the case here. </p> <p> The original rationale for this article series was to demonstrate how serialization can be done without Reflection, so I'll start there and return to Reflection later. </p> <p> In this article series, I consider the JSON format fixed. A single table should be rendered as shown above, and a communal table should be rendered like this: </p> <p> <pre>{&nbsp;<span style="color:#2e75b6;">&quot;communalTable&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;42&nbsp;}&nbsp;}</pre> </p> <p> Often in the real world you'll have to conform to a particular protocol format, or, even if that's not the case, being able to control the shape of the wire format is important to deal with backwards compatibility. </p> <p> As I outlined in the <a href="/2023/12/04/serialization-with-and-without-reflection">introduction article</a> you can usually find a more weakly typed API to get the job done. For serializing <code>Table</code> to JSON it looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Serialize</span>(<span style="color:blue;">this</span>&nbsp;Table&nbsp;<span style="font-weight:bold;color:#1f377f;">table</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;table.Accept(<span style="color:blue;">new</span>&nbsp;TableVisitor()); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TableVisitor</span>&nbsp;:&nbsp;ITableVisitor&lt;<span style="color:blue;">string</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">VisitCommunal</span>(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">j</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;JsonObject &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&quot;communalTable&quot;</span>]&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;JsonObject &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&quot;capacity&quot;</span>]&nbsp;=&nbsp;(<span style="color:blue;">int</span>)capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;j.ToJsonString(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">VisitSingle</span>(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>,&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">j</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;JsonObject &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&quot;singleTable&quot;</span>]&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;JsonObject &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&quot;capacity&quot;</span>]&nbsp;=&nbsp;(<span style="color:blue;">int</span>)capacity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&quot;minimalReservation&quot;</span>]&nbsp;=&nbsp;(<span style="color:blue;">int</span>)value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;j.ToJsonString(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In order to separate concerns, I've defined this functionality in a new static class that references the Domain Model. The <code>Serialize</code> extension method uses a <code>private</code> Visitor to write two different <a href="https://learn.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">JsonObject</a> objects, using the JSON API's underlying Document Object Model (DOM). </p> <h3 id="53c250b548c64292b2d704be12c91aa5"> JSON deserialization by hand <a href="#53c250b548c64292b2d704be12c91aa5">#</a> </h3> <p> You can also go the other way, and when it looks more complicated, it's because it is. When serializing an encapsulated value, not a lot can go wrong because the value is already valid. When deserializing a JSON string, on the other hand, all sorts of things can go wrong: It might not even be a valid string, or the string may not be valid JSON, or the JSON may not be a valid <code>Table</code> representation, or the values may be illegal, etc. </p> <p> Since there are several values that explicitly must be integers, it makes sense to define a helper method to try to parse an integer: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>?&nbsp;<span style="font-weight:bold;color:#74531f;">TryInt</span>(<span style="color:blue;">this</span>&nbsp;JsonNode?&nbsp;<span style="font-weight:bold;color:#1f377f;">node</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(node&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(node.GetValueKind()&nbsp;!=&nbsp;JsonValueKind.Number) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;(<span style="color:blue;">int</span>)node; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">catch</span>&nbsp;(FormatException) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I'm surprised that there's no built-in way to do that, but if there is, I couldn't find it. </p> <p> With a helper method like that you can now implement the <code>Deserialize</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table?&nbsp;<span style="font-weight:bold;color:#74531f;">Deserialize</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">json</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">node</span>&nbsp;=&nbsp;JsonNode.Parse(json); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">cnode</span>&nbsp;=&nbsp;node?[<span style="color:#a31515;">&quot;communalTable&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(cnode&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>&nbsp;=&nbsp;cnode[<span style="color:#a31515;">&quot;capacity&quot;</span>].TryInt(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(capacity&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Table.TryCreateCommunal(capacity.Value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">snode</span>&nbsp;=&nbsp;node?[<span style="color:#a31515;">&quot;singleTable&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(snode&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>&nbsp;=&nbsp;snode[<span style="color:#a31515;">&quot;capacity&quot;</span>].TryInt(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">minimalReservation</span>&nbsp;=&nbsp;snode[<span style="color:#a31515;">&quot;minimalReservation&quot;</span>].TryInt(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(capacity&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>&nbsp;||&nbsp;minimalReservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Table.TryCreateSingle( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;capacity.Value, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minimalReservation.Value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">catch</span>&nbsp;(JsonException) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since both serialisation and deserialization is based on string values, you should write automated tests that verify that the code works, and in fact, I did. Here are a few examples: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">DeserializeSingleTableFor4</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;&quot;{&quot;singleTable&quot;:{&quot;capacity&quot;:4,&quot;minimalReservation&quot;:3}}&quot;&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;TableJson.Deserialize(json); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Table.TryCreateSingle(4,&nbsp;3),&nbsp;actual); } [Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">DeserializeNonTable</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;&quot;{&quot;foo&quot;:42}&quot;&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;TableJson.Deserialize(json); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Null(actual); }</pre> </p> <p> Apart from using directives and namespace declaration this hand-written JSON capability requires 87 lines of code, although, to be fair, <code>TryInt</code> is a general-purpose method that ought to be part of the <code>System.Text.Json</code> API. Can we do better with static types and Reflection? </p> <h3 id="fc6832fe72874427b32a0ee062d4fbf6"> JSON serialisation based on types <a href="#fc6832fe72874427b32a0ee062d4fbf6">#</a> </h3> <p> The static <a href="https://learn.microsoft.com/dotnet/api/system.text.json.jsonserializer">JsonSerializer</a> class comes with <code>Serialize&lt;T&gt;</code> and <code>Deserialize&lt;T&gt;</code> methods that use Reflection to convert a statically typed object to and from JSON. You can define a type (a <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a> (DTO) if you will) and let Reflection do the hard work. </p> <p> In <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> I explain how you're usually better off separating the role of serialization from the role of Domain Model. One way to do that is exactly by defining a DTO for serialisation, and let the Domain Model remain exclusively to model the rules of the application. The above <code>Table</code> type plays the latter role, so we need new DTO types: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TableDto</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;CommunalTableDto?&nbsp;CommunalTable&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SingleTableDto?&nbsp;SingleTable&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CommunalTableDto</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SingleTableDto</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;MinimalReservation&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> One way to model a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> with a DTO is to declare both cases as nullable fields. While it does allow illegal states to be representable (i.e. both kinds of tables defined at the same time, or none of them present) this is only par for the course at the application boundary. </p> <p> While you can serialize values of that type, by default the generated JSON doesn't have the right format. Instead, a serialized communal table looks like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;CommunalTable&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;Capacity&quot;</span>:&nbsp;42&nbsp;}, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;SingleTable&quot;</span>:&nbsp;<span style="color:blue;">null</span> }</pre> </p> <p> There are two problems with the generated JSON document: </p> <ul> <li>The casing is wrong</li> <li>The null value shouldn't be there</li> </ul> <p> None of those are too hard to address, but it does make the API a bit more awkward to use, as this test demonstrates: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">SerializeCommunalTableViaReflection</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TableDto &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CommunalTable&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CommunalTableDto&nbsp;{&nbsp;Capacity&nbsp;=&nbsp;42&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;JsonSerializer.Serialize( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;JsonSerializerOptions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PropertyNamingPolicy&nbsp;=&nbsp;JsonNamingPolicy.CamelCase, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DefaultIgnoreCondition&nbsp;=&nbsp;JsonIgnoreCondition.WhenWritingNull &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:#a31515;">&quot;&quot;&quot;{&quot;communalTable&quot;:{&quot;capacity&quot;:42}}&quot;&quot;&quot;</span>,&nbsp;actual); }</pre> </p> <p> You can, of course, define this particular serialization behaviour as a reusable method, so it's not a problem that you can't address. I just wanted to include this, since it's part of the overall work that you have to do in order to make this work. </p> <h3 id="213e3959eb49407ab3cdf59a4d2aed06"> JSON deserialisation based on types <a href="#213e3959eb49407ab3cdf59a4d2aed06">#</a> </h3> <p> To allow parsing of JSON into the above DTO the Reflection-based <code>Deserialize</code> method pretty much works out of the box, although again, it needs to be configured. Here's a passing test that demonstrates how that works: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">DeserializeSingleTableViaReflection</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;&quot;{&quot;singleTable&quot;:{&quot;capacity&quot;:4,&quot;minimalReservation&quot;:2}}&quot;&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;JsonSerializer.Deserialize&lt;TableDto&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;JsonSerializerOptions&nbsp;{&nbsp;PropertyNamingPolicy&nbsp;=&nbsp;JsonNamingPolicy.CamelCase&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Null(actual?.CommunalTable); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(4,&nbsp;actual?.SingleTable?.Capacity); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(2,&nbsp;actual?.SingleTable?.MinimalReservation); }</pre> </p> <p> There's only difference in casing, so you'd expect the <code>Deserialize</code> method to be a <a href="https://martinfowler.com/bliki/TolerantReader.html">Tolerant Reader</a>, but no. It's very particular about that, so the <code>JsonNamingPolicy.CamelCase</code> configuration is necessary. Perhaps the API designers found that <a href="https://peps.python.org/pep-0020/">explicit is better than implicit</a>. </p> <p> In any case, you could package that in a reusable <code>Deserialize</code> function that has all the options that are appropriate in a particular code context, so not a big deal. That takes care of actually writing and parsing JSON, but that's only half the battle. This only gives you a way to parse and serialize the DTO. What you ultimately want is to persist or dehydrate <code>Table</code> data. </p> <h3 id="ef08c05a3ae84141b2e8b20238af83df"> Converting DTO to Domain Model, and vice versa <a href="#ef08c05a3ae84141b2e8b20238af83df">#</a> </h3> <p> As usual, converting a nice, encapsulated value to a more relaxed format is safe and trivial: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;TableDto&nbsp;<span style="font-weight:bold;color:#74531f;">ToDto</span>(<span style="color:blue;">this</span>&nbsp;Table&nbsp;<span style="font-weight:bold;color:#1f377f;">table</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;table.Accept(<span style="color:blue;">new</span>&nbsp;TableDtoVisitor()); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TableDtoVisitor</span>&nbsp;:&nbsp;ITableVisitor&lt;TableDto&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TableDto&nbsp;<span style="font-weight:bold;color:#74531f;">VisitCommunal</span>(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;TableDto &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CommunalTable&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CommunalTableDto &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;(<span style="color:blue;">int</span>)capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TableDto&nbsp;<span style="font-weight:bold;color:#74531f;">VisitSingle</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;TableDto &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SingleTable&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SingleTableDto &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;(<span style="color:blue;">int</span>)capacity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MinimalReservation&nbsp;=&nbsp;(<span style="color:blue;">int</span>)value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Going the other way is <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">fundamentally a parsing exercise</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Table?&nbsp;<span style="font-weight:bold;color:#74531f;">TryParse</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(CommunalTable&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Table.TryCreateCommunal(CommunalTable.Capacity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(SingleTable&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Table.TryCreateSingle( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SingleTable.Capacity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SingleTable.MinimalReservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; }</pre> </p> <p> Here, like in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, I've made that conversion an instance method on <code>TableDto</code>. </p> <p> Such an operation may fail, so the result is a nullable <code>Table</code> object. </p> <p> Let's take stock of the type-based alternative. It requires 58 lines of code, distributed over three DTO types and the two conversions <code>ToDto</code> and <code>TryParse</code>, but here I haven't counted configuration of <code>Serialize</code> and <code>Deserialize</code>, since I left that to each test case that I wrote. Since all of this code generally stays within 80 characters in line width, that would realistically add another 10 lines of code, for a total around 68 lines. </p> <p> This is smaller than the DOM-based code, but not by much. </p> <h3 id="bfac4d5d5ca940a2a61f964c4336adcf"> Conclusion <a href="#bfac4d5d5ca940a2a61f964c4336adcf">#</a> </h3> <p> In this article I've explored two alternatives for converting a well-encapsulated Domain Model to and from JSON. One option is to directly manipulate the DOM. Another option is take a more declarative approach and define <em>types</em> that model the shape of the JSON data, and then leverage type-based automation (here, Reflection) to automatically parse and write the JSON. </p> <p> I've deliberately chosen a Domain Model with some constraints, in order to demonstrate how persisting a non-trivial data model might work. With that setup, writing 'loosely coupled' code directly against the DOM requires 87 lines of code, while taking advantage of type-based automation requires 68 lines of code. Again, Reflection seems 'easier' if you count lines of code, but the difference is marginal. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="467ce74e29064c60bfa9559140710e51"> <div class="comment-author"><a href="https://blog.oakular.xyz">Callum Warrilow</a> <a href="#467ce74e29064c60bfa9559140710e51">#</a></div> <div class="comment-content"> <p> Great piece as ever Mark. Always enjoy reading about alternatives to methods that have become unquestioned convention. </p> <p> I generally try to avoid reflection, especially within business code, and mainly use it for application bootstrapping, such as to discover services for dependency injection by convention. I also don't like attributes muddying model definitions, even on DTOs, so I would happily take an alternative to <code>System.Text.Json</code>. It is however increasingly integrated into other System libraries in ways that make it almost too useful to pass up. For example, the <code><a href="https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent?view=net-8.0">System.Net.Http.HttpContent</a></code> class has the <code><a href="https://learn.microsoft.com/en-us/dotnet/api/system.net.http.json.httpcontentjsonextensions.readfromjsonasync?view=net-8.0">ReadFromJsonAsync</a></code> extension method, which makes it trivial to deserialize a response body. Analogous methods exist for <code><a href="https://learn.microsoft.com/en-us/dotnet/api/system.binarydata?view=dotnet-plat-ext-8.0">BinaryData</a></code>. I'm not normally a sucker for convenience, but it is difficult to turn down strong integration like this. </p> </div> <div class="comment-date">2024-01-05 21:13 UTC</div> </div> <div class="comment" id="b9a5340eec9c45f49a438a37c7499520"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b9a5340eec9c45f49a438a37c7499520">#</a></div> <div class="comment-content"> <p> Callum, thank you for writing. You are correct that the people who design and develop .NET put a lot of effort into making things convenient. Some of that convenience, however, comes with a price. You have to buy into a certain way of doing things, and that certain way can sometimes be at odds with other good software practices, such as the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> or test-driven development. </p> <p> My goal with this (and other) article(s) isn't, however, to say that you mustn't take advantage of convenient integrations, but rather to highlight that alternatives exist. </p> <p> The many 'convenient' ways that a framework gives you to solve various problems comes with the risk that you may paint yourself into a corner, if you aren't careful. You've invested heavily in the framework's way of doing things, but there's just this small edge case that you can't get right. So you write a bit of custom code, after having figured out the correct extensibility point to hook into. Until the framework changes 'how things are done' in the next iteration. </p> <p> This is what I call <a href="/2023/10/02/dependency-whac-a-mole">Framework Whac-A-Mole</a> - a syndrome that I'm becoming increasingly wary of the more experience I gain. Of the examples linked to in that article, <a href="/2022/08/15/aspnet-validation-revisited">ASP.NET validation revisited</a> may be the most relevant to this discussion. </p> <p> As a final note, I'd be remiss if I entered into a discussion about programmer convenience without drawing on <a href="https://en.wikipedia.org/wiki/Rich_Hickey">Rich Hickey</a>'s excellent presentation <a href="https://www.infoq.com/presentations/Simple-Made-Easy/">Simple Made Easy</a>, where he goes to great length distinguishing between what is <em>easy</em> (i.e. close at hand) and what is <em>simple</em> (i.e. not complex). The sweet spot, of course, is the intersection, where things are both simple and easy. </p> <p> Most 'convenient' framework features do not, in my opinion, check that box. </p> </div> <div class="comment-date">2024-01-10 13:37 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Serializing restaurant tables in F# https://blog.ploeh.dk/2023/12/18/serializing-restaurant-tables-in-f 2023-12-18T13:59:00+00:00 Mark Seemann <div id="post"> <p> <em>Using System.Text.Json, with and without Reflection.</em> </p> <p> This article is part of a short series of articles about <a href="/2023/12/04/serialization-with-and-without-reflection">serialization with and without Reflection</a>. In this instalment I'll explore some options for serializing <a href="https://en.wikipedia.org/wiki/JSON">JSON</a> with <a href="https://fsharp.org/">F#</a> using the API built into .NET: <a href="https://learn.microsoft.com/dotnet/api/system.text.json">System.Text.Json</a>. I'm not going use <a href="https://www.newtonsoft.com/json">Json.NET</a> in this article, but I've <a href="/2022/01/03/to-id-or-not-to-id">done similar things with that library</a> in the past, so what's here is, at least, somewhat generalizable. </p> <h3 id="64e7a2f8c5634026ae4ffd1497dd58f9"> Natural numbers <a href="#64e7a2f8c5634026ae4ffd1497dd58f9">#</a> </h3> <p> Before we start investigating how to serialize to and from JSON, we must have something to serialize. As described in the <a href="/2023/12/04/serialization-with-and-without-reflection">introductory article</a> we'd like to parse and write restaurant table configurations like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;singleTable&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;16, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;minimalReservation&quot;</span>:&nbsp;10 &nbsp;&nbsp;} }</pre> </p> <p> On the other hand, I'd like to represent the Domain Model in a way that <a href="/2022/10/24/encapsulation-in-functional-programming">encapsulates the rules</a> governing the model, <a href="https://blog.janestreet.com/effective-ml-video/">making illegal states unrepresentable</a>. </p> <p> As the first step, we observe that the numbers involved are all <a href="https://en.wikipedia.org/wiki/Natural_number">natural numbers</a>. In F# it's both <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> and easy to define a <a href="https://www.hillelwayne.com/post/constructive/">predicative data type</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;NaturalNumber&nbsp;=&nbsp;<span style="color:blue;">private</span>&nbsp;NaturalNumber&nbsp;<span style="color:blue;">of</span>&nbsp;int</pre> </p> <p> Since it's defined with a <code>private</code> constructor we need to also supply a way to create valid values of the type: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;NaturalNumber&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;tryCreate&nbsp;n&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;n&nbsp;&lt;&nbsp;1&nbsp;<span style="color:blue;">then</span>&nbsp;None&nbsp;<span style="color:blue;">else</span>&nbsp;Some&nbsp;(NaturalNumber&nbsp;n)</pre> </p> <p> In this, as well as the other articles in this series, I've chosen to model the potential for errors with <code>Option</code> values. I could also have chosen to use <code>Result</code> if I wanted to communicate information along the 'error channel', but sticking with <code>Option</code> makes the code a bit simpler. Not so much in F# or <a href="https://www.haskell.org/">Haskell</a>, but once we reach C#, <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">applicative validation</a> becomes complicated. </p> <p> There's no loss of generality in this decision, since both <code>Option</code> and <code>Result</code> are <a href="/2018/10/01/applicative-functors">applicative functors</a>. </p> <p> <pre>&gt; NaturalNumber.tryCreate&nbsp;-1;; val it: NaturalNumber option = None &gt; <span style="color:blue;">let</span>&nbsp;x&nbsp;=&nbsp;NaturalNumber.tryCreate&nbsp;42;; val x: NaturalNumber option = Some NaturalNumber 42</pre> </p> <p> The <code>tryCreate</code> function enables client developers to create <code>NaturalNumber</code> values, and due to the F#'s default equality and comparison implementation, you can even compare them: </p> <p> <pre>&gt; <span style="color:blue;">let</span>&nbsp;y = NaturalNumber.tryCreate 2112;; val y: NaturalNumber option = Some NaturalNumber 2112 &gt; x &lt; y;; val it: bool = true</pre> </p> <p> That's it for natural numbers. Three lines of code. Compare that to <a href="/2023/12/11/serializing-restaurant-tables-in-haskell">the Haskell implementation</a>, which required eight lines of code. This is mostly due to F#'s <code>private</code> keyword, which Haskell doesn't have. </p> <h3 id="8957ab6a606a4279a654e040d0788051"> Domain Model <a href="#8957ab6a606a4279a654e040d0788051">#</a> </h3> <p> Modelling a restaurant table follows in the same vein. One invariant I would like to enforce is that for a 'single' table, the minimal reservation should be a <code>NaturalNumber</code> less than or equal to the table's capacity. It doesn't make sense to configure a table for four with a minimum reservation of six. </p> <p> In the same spirit as above, then, define this type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Table&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;SingleTable&nbsp;<span style="color:blue;">of</span>&nbsp;NaturalNumber&nbsp;*&nbsp;NaturalNumber &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;CommunalTable&nbsp;<span style="color:blue;">of</span>&nbsp;NaturalNumber</pre> </p> <p> Once more the <code>private</code> keyword makes it impossible for client code to create instances directly, so we need a pair of functions to create values: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Table&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;trySingle&nbsp;capacity&nbsp;minimalReservation&nbsp;=&nbsp;option&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;cap&nbsp;=&nbsp;NaturalNumber.tryCreate&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;min&nbsp;=&nbsp;NaturalNumber.tryCreate&nbsp;minimalReservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;cap&nbsp;&lt;&nbsp;min&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">return!</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">return</span>&nbsp;SingleTable&nbsp;(cap,&nbsp;min)&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;tryCommunal&nbsp;=&nbsp;NaturalNumber.tryCreate&nbsp;&gt;&gt;&nbsp;Option.map&nbsp;CommunalTable</pre> </p> <p> Notice that <code>trySingle</code> checks the invariant that the <code>capacity</code> must be greater than or equal to the <code>minimalReservation</code>. </p> <p> Again, notice how much easier it is to define a predicative type in F#, compared to Haskell. </p> <p> This isn't a competition between languages, and while F# certainly scores a couple of points here, Haskell has other advantages. </p> <p> The point of this little exercise, so far, is that it <em>encapsulates</em> the contract implied by the Domain Model. It does this by using the static type system to its advantage. </p> <h3 id="560a74686ec64d36858a893c7c63cbb4"> JSON serialization by hand <a href="#560a74686ec64d36858a893c7c63cbb4">#</a> </h3> <p> At the boundaries of applications, however, <a href="/2023/10/16/at-the-boundaries-static-types-are-illusory">there are no static types</a>. Is the static type system still useful in that situation? </p> <p> For a long time, the most popular .NET library for JSON serialization was <a href="https://www.newtonsoft.com/json">Json.NET</a>, but these days I find the built-in API offered in the <a href="https://learn.microsoft.com/dotnet/api/system.text.json">System.Text.Json</a> namespace adequate. This is also the case here. </p> <p> The original rationale for this article series was to demonstrate how serialization can be done without Reflection, so I'll start there and return to Reflection later. </p> <p> In this article series, I consider the JSON format fixed. A single table should be rendered as shown above, and a communal table should be rendered like this: </p> <p> <pre>{&nbsp;<span style="color:#2e75b6;">&quot;communalTable&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;42&nbsp;}&nbsp;}</pre> </p> <p> Often in the real world you'll have to conform to a particular protocol format, or, even if that's not the case, being able to control the shape of the wire format is important to deal with backwards compatibility. </p> <p> As I outlined in the <a href="/2023/12/04/serialization-with-and-without-reflection">introduction article</a> you can usually find a more weakly typed API to get the job done. For serializing <code>Table</code> to JSON it looks like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;serializeTable&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;SingleTable&nbsp;(NaturalNumber&nbsp;capacity,&nbsp;NaturalNumber&nbsp;minimalReservation)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;j&nbsp;=&nbsp;JsonObject&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j[<span style="color:#a31515;">&quot;singleTable&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;JsonObject&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j[<span style="color:#a31515;">&quot;singleTable&quot;</span>][<span style="color:#a31515;">&quot;capacity&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j[<span style="color:#a31515;">&quot;singleTable&quot;</span>][<span style="color:#a31515;">&quot;minimalReservation&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;minimalReservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j.ToJsonString&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;CommunalTable&nbsp;(NaturalNumber&nbsp;capacity)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;j&nbsp;=&nbsp;JsonObject&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j[<span style="color:#a31515;">&quot;communalTable&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;JsonObject&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j[<span style="color:#a31515;">&quot;communalTable&quot;</span>][<span style="color:#a31515;">&quot;capacity&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j.ToJsonString&nbsp;()</pre> </p> <p> In order to separate concerns, I've defined this functionality in a new module that references the module that defines the Domain Model. The <code>serializeTable</code> function pattern-matches on <code>SingleTable</code> and <code>CommunalTable</code> to write two different <a href="https://learn.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">JsonObject</a> objects, using the JSON API's underlying Document Object Model (DOM). </p> <h3 id="7fb5251937ac4f86b016f7c782db7680"> JSON deserialization by hand <a href="#7fb5251937ac4f86b016f7c782db7680">#</a> </h3> <p> You can also go the other way, and when it looks more complicated, it's because it is. When serializing an encapsulated value, not a lot can go wrong because the value is already valid. When deserializing a JSON string, on the other hand, all sorts of things can go wrong: It might not even be a valid string, or the string may not be valid JSON, or the JSON may not be a valid <code>Table</code> representation, or the values may be illegal, etc. </p> <p> Here I found it appropriate to first define a small API of parsing functions, mostly in order to make the object-oriented API more composable. First, I need some code that looks at the root JSON object to determine which kind of table it is (if it's a table at all). I found it appropriate to do that as a pair of <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/active-patterns">active patterns</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;(|Single|_|)&nbsp;(node&nbsp;:&nbsp;JsonNode)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;node[<span style="color:#a31515;">&quot;singleTable&quot;</span>]&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">null</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;tn&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Some&nbsp;tn <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;(|Communal|_|)&nbsp;(node&nbsp;:&nbsp;JsonNode)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;node[<span style="color:#a31515;">&quot;communalTable&quot;</span>]&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">null</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;tn&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Some&nbsp;tn</pre> </p> <p> It turned out that I also needed a function to even check if a string is a valid JSON document: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;tryParseJson&nbsp;(candidate&nbsp;:&nbsp;string)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">try</span>&nbsp;JsonNode.Parse&nbsp;candidate&nbsp;|&gt;&nbsp;Some &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">with</span>&nbsp;|&nbsp;:?&nbsp;System.Text.Json.JsonException&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None</pre> </p> <p> If there's a way to do that without a <code>try/with</code> expression, I couldn't find it. Likewise, trying to parse an integer turns out to be surprisingly complicated: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;tryParseInt&nbsp;(node&nbsp;:&nbsp;JsonNode)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;node&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">null</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;node.GetValueKind&nbsp;()&nbsp;=&nbsp;JsonValueKind.Number &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">try</span>&nbsp;node&nbsp;|&gt;&nbsp;int&nbsp;|&gt;&nbsp;Some &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">with</span>&nbsp;|&nbsp;:?&nbsp;FormatException&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None&nbsp;<span style="color:green;">//&nbsp;Thrown&nbsp;on&nbsp;decimal&nbsp;numbers</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;None</pre> </p> <p> Both <code>tryParseJson</code> and <code>tryParseInt</code> are, however, general-purpose functions, so if you have a lot of JSON you need to parse, you can put them in a reusable library. </p> <p> With those building blocks you can now define a function to parse a <code>Table</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;tryDeserializeTable&nbsp;(candidate&nbsp;:&nbsp;string)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;tryParseJson&nbsp;candidate&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;(Single&nbsp;node)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;option&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;capacity&nbsp;=&nbsp;node[<span style="color:#a31515;">&quot;capacity&quot;</span>]&nbsp;|&gt;&nbsp;tryParseInt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;minimalReservation&nbsp;=&nbsp;node[<span style="color:#a31515;">&quot;minimalReservation&quot;</span>]&nbsp;|&gt;&nbsp;tryParseInt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;Table.trySingle&nbsp;capacity&nbsp;minimalReservation&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;(Communal&nbsp;node)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;option&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;capacity&nbsp;=&nbsp;node[<span style="color:#a31515;">&quot;capacity&quot;</span>]&nbsp;|&gt;&nbsp;tryParseInt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;Table.tryCommunal&nbsp;capacity&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None</pre> </p> <p> Since both serialisation and deserialization is based on string values, you should write automated tests that verify that the code works, and in fact, I did. Here are a few examples: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Deserialize&nbsp;single&nbsp;table&nbsp;for&nbsp;4``&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;json&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;&quot;{&quot;singleTable&quot;:{&quot;capacity&quot;:4,&quot;minimalReservation&quot;:3}}&quot;&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;tryDeserializeTable&nbsp;json &nbsp;&nbsp;&nbsp;&nbsp;Table.trySingle&nbsp;4&nbsp;3&nbsp;=!&nbsp;actual [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Deserialize&nbsp;non-table``&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;json&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;&quot;{&quot;foo&quot;:42}&quot;&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;tryDeserializeTable&nbsp;json &nbsp;&nbsp;&nbsp;&nbsp;None&nbsp;=!&nbsp;actual</pre> </p> <p> Apart from module declaration and imports etc. this hand-written JSON capability requires 46 lines of code, although, to be fair, some of that code (<code>tryParseJson</code> and <code>tryParseInt</code>) are general-purpose functions that belong in a reusable library. Can we do better with static types and Reflection? </p> <h3 id="08491ec39df4485e83cd0b5cf80cdb7e"> JSON serialisation based on types <a href="#08491ec39df4485e83cd0b5cf80cdb7e">#</a> </h3> <p> The static <a href="https://learn.microsoft.com/dotnet/api/system.text.json.jsonserializer">JsonSerializer</a> class comes with <code>Serialize&lt;T&gt;</code> and <code>Deserialize&lt;T&gt;</code> methods that use Reflection to convert a statically typed object to and from JSON. You can define a type (a <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a> (DTO) if you will) and let Reflection do the hard work. </p> <p> In <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> I explain how you're usually better off separating the role of serialization from the role of Domain Model. One way to do that is exactly by defining a DTO for serialisation, and let the Domain Model remain exclusively to model the rules of the application. The above <code>Table</code> type plays the latter role, so we need new DTO types: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;CommunalTableDto&nbsp;=&nbsp;{&nbsp;Capacity&nbsp;:&nbsp;int&nbsp;} <span style="color:blue;">type</span>&nbsp;SingleTableDto&nbsp;=&nbsp;{&nbsp;Capacity&nbsp;:&nbsp;int;&nbsp;MinimalReservation&nbsp;:&nbsp;int&nbsp;} <span style="color:blue;">type</span>&nbsp;TableDto&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;CommunalTable&nbsp;:&nbsp;CommunalTableDto&nbsp;option &nbsp;&nbsp;&nbsp;&nbsp;SingleTable&nbsp;:&nbsp;SingleTableDto&nbsp;option&nbsp;}</pre> </p> <p> One way to model a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> with a DTO is to declare both cases as <code>option</code> fields. While it does allow illegal states to be representable (i.e. both kinds of tables defined at the same time, or none of them present) this is only par for the course at the application boundary. </p> <p> While you can serialize values of that type, by default the generated JSON doesn't have the right format: </p> <p> <pre>&gt; val dto: TableDto = { CommunalTable = Some { Capacity = 42 } SingleTable = None } &gt; JsonSerializer.Serialize dto;; val it: string = "{"CommunalTable":{"Capacity":42},"SingleTable":null}"</pre> </p> <p> There are two problems with the generated JSON document: </p> <ul> <li>The casing is wrong</li> <li>The null value shouldn't be there</li> </ul> <p> None of those are too hard to address, but it does make the API a bit more awkward to use, as this test demonstrates: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Serialize&nbsp;communal&nbsp;table&nbsp;via&nbsp;reflection``&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;dto&nbsp;=&nbsp;{&nbsp;CommunalTable&nbsp;=&nbsp;Some&nbsp;{&nbsp;Capacity&nbsp;=&nbsp;42&nbsp;};&nbsp;SingleTable&nbsp;=&nbsp;None&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JsonSerializer.Serialize&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JsonSerializerOptions&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PropertyNamingPolicy&nbsp;=&nbsp;JsonNamingPolicy.CamelCase, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IgnoreNullValues&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;&quot;&quot;{&quot;communalTable&quot;:{&quot;capacity&quot;:42}}&quot;&quot;&quot;</span>&nbsp;=!&nbsp;actual</pre> </p> <p> You can, of course, define this particular serialization behaviour as a reusable function, so it's not a problem that you can't address. I just wanted to include this, since it's part of the overall work that you have to do in order to make this work. </p> <h3 id="a215bb56b64446afbe2ca6861f724126"> JSON deserialisation based on types <a href="#a215bb56b64446afbe2ca6861f724126">#</a> </h3> <p> To allow parsing of JSON into the above DTO the Reflection-based <code>Deserialize</code> method pretty much works out of the box, although again, it needs to be configured. Here's a passing test that demonstrates how that works: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Deserialize&nbsp;single&nbsp;table&nbsp;via&nbsp;reflection``&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;json&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;&quot;{&quot;singleTable&quot;:{&quot;capacity&quot;:4,&quot;minimalReservation&quot;:2}}&quot;&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JsonSerializer.Deserialize&lt;TableDto&gt;&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JsonSerializerOptions&nbsp;(&nbsp;PropertyNamingPolicy&nbsp;=&nbsp;JsonNamingPolicy.CamelCase&nbsp;)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CommunalTable&nbsp;=&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SingleTable&nbsp;=&nbsp;Some&nbsp;{&nbsp;Capacity&nbsp;=&nbsp;4;&nbsp;MinimalReservation&nbsp;=&nbsp;2&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;=!&nbsp;actual</pre> </p> <p> There's only difference in casing, so you'd expect the <code>Deserialize</code> method to be a <a href="https://martinfowler.com/bliki/TolerantReader.html">Tolerant Reader</a>, but no. It's very particular about that, so the <code>JsonNamingPolicy.CamelCase</code> configuration is necessary. Perhaps the API designers found that <a href="https://peps.python.org/pep-0020/">explicit is better than implicit</a>. </p> <p> In any case, you could package that in a reusable <code>Deserialize</code> function that has all the options that are appropriate in a particular code context, so not a big deal. That takes care of actually writing and parsing JSON, but that's only half the battle. This only gives you a way to parse and serialize the DTO. What you ultimately want is to persist or dehydrate <code>Table</code> data. </p> <h3 id="8faed9f5f0b149d68ec5e1a457046e59"> Converting DTO to Domain Model, and vice versa <a href="#8faed9f5f0b149d68ec5e1a457046e59">#</a> </h3> <p> As usual, converting a nice, encapsulated value to a more relaxed format is safe and trivial: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;toTableDto&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;SingleTable&nbsp;(NaturalNumber&nbsp;capacity,&nbsp;NaturalNumber&nbsp;minimalReservation)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CommunalTable&nbsp;=&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SingleTable&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Some &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MinimalReservation&nbsp;=&nbsp;minimalReservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;CommunalTable&nbsp;(NaturalNumber&nbsp;capacity)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;CommunalTable&nbsp;=&nbsp;Some&nbsp;{&nbsp;Capacity&nbsp;=&nbsp;capacity&nbsp;};&nbsp;SingleTable&nbsp;=&nbsp;None&nbsp;}</pre> </p> <p> Going the other way is <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">fundamentally a parsing exercise</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;tryParseTableDto&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;candidate.CommunalTable,&nbsp;candidate.SingleTable&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;{&nbsp;Capacity&nbsp;=&nbsp;capacity&nbsp;},&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Table.tryCommunal&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None,&nbsp;Some&nbsp;{&nbsp;Capacity&nbsp;=&nbsp;capacity;&nbsp;MinimalReservation&nbsp;=&nbsp;minimalReservation&nbsp;}&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Table.trySingle&nbsp;capacity&nbsp;minimalReservation &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None</pre> </p> <p> Such an operation may fail, so the result is a <code>Table option</code>. It could also have been a <code>Result&lt;Table, 'something&gt;</code>, if you wanted to return information about errors when things go wrong. It makes the code marginally more complex, but doesn't change the overall thrust of this exploration. </p> <p> Ironically, while <code>tryParseTableDto</code> is actually more complex than <code>toTableDto</code> it looks smaller, or at least denser. </p> <p> Let's take stock of the type-based alternative. It requires 26 lines of code, distributed over three DTO types and the two conversions <code>tryParseTableDto</code> and <code>toTableDto</code>, but here I haven't counted configuration of <code>Serialize</code> and <code>Deserialize</code>, since I left that to each test case that I wrote. Since all of this code generally stays within 80 characters in line width, that would realistically add another 10 lines of code, for a total around 36 lines. </p> <p> This is smaller than the DOM-based code, although at the same magnitude. </p> <h3 id="2292c83546d441159ece864f3636cd41"> Conclusion <a href="#2292c83546d441159ece864f3636cd41">#</a> </h3> <p> In this article I've explored two alternatives for converting a well-encapsulated Domain Model to and from JSON. One option is to directly manipulate the DOM. Another option is take a more declarative approach and define <em>types</em> that model the shape of the JSON data, and then leverage type-based automation (here, Reflection) to automatically parse and write the JSON. </p> <p> I've deliberately chosen a Domain Model with some constraints, in order to demonstrate how persisting a non-trivial data model might work. With that setup, writing 'loosely coupled' code directly against the DOM requires 46 lines of code, while taking advantage of type-based automation requires 36 lines of code. Contrary to <a href="/2023/12/11/serializing-restaurant-tables-in-haskell">the Haskell example</a>, Reflection does seem to edge out a win this round. </p> <p> <strong>Next:</strong> <a href="/2023/12/25/serializing-restaurant-tables-in-c">Serializing restaurant tables in C#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Serializing restaurant tables in Haskell https://blog.ploeh.dk/2023/12/11/serializing-restaurant-tables-in-haskell 2023-12-11T07:35:00+00:00 Mark Seemann <div id="post"> <p> <em>Using Aeson, with and without generics.</em> </p> <p> This article is part of a short series of articles about <a href="/2023/12/04/serialization-with-and-without-reflection">serialization with and without Reflection</a>. In this instalment I'll explore some options for serializing <a href="https://en.wikipedia.org/wiki/JSON">JSON</a> using <a href="https://hackage.haskell.org/package/aeson">Aeson</a>. </p> <p> The source code is <a href="https://github.com/ploeh/HaskellJSONSerialization">available on GitHub</a>. </p> <h3 id="013a78e039024c81a204ca10f1a7af69"> Natural numbers <a href="#013a78e039024c81a204ca10f1a7af69">#</a> </h3> <p> Before we start investigating how to serialize to and from JSON, we must have something to serialize. As described in the <a href="/2023/12/04/serialization-with-and-without-reflection">introductory article</a> we'd like to parse and write restaurant table configurations like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;singleTable&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;16, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;minimalReservation&quot;</span>:&nbsp;10 &nbsp;&nbsp;} }</pre> </p> <p> On the other hand, I'd like to represent the Domain Model in a way that <a href="/2022/10/24/encapsulation-in-functional-programming">encapsulates the rules</a> governing the model, <a href="https://blog.janestreet.com/effective-ml-video/">making illegal states unrepresentable</a>. </p> <p> As the first step, we observe that the numbers involved are all <a href="https://en.wikipedia.org/wiki/Natural_number">natural numbers</a>. While <a href="/2022/01/24/type-level-di-container-prototype">I'm aware</a> that <a href="https://www.haskell.org/">Haskell</a> has built-in <a href="https://hackage.haskell.org/package/base/docs/GHC-TypeLits.html#t:Nat">Nat</a> type, I choose not to use it here, for a couple of reasons. One is that <code>Nat</code> is intended for type-level programming, and while this <em>might</em> be useful here, I don't want to pull in more exotic language features than are required. Another reason is that, in this domain, I want to model natural numbers as excluding zero (and I honestly don't remember if <code>Nat</code> allows zero, but I <em>think</em> that it does..?). </p> <p> Another option is to use <a href="/2019/05/13/peano-catamorphism">Peano numbers</a>, but again, for didactic reasons, I'll stick with something a bit more idiomatic. </p> <p> You can easily introduce a wrapper over, say, <code>Integer</code>, to model natural numbers: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;Natural&nbsp;=&nbsp;Natural&nbsp;Integer&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Ord</span>,&nbsp;<span style="color:#2b91af;">Show</span>)</pre> </p> <p> This, however, doesn't prevent you from writing <code>Natural (-1)</code>, so we need to make this a <a href="https://www.hillelwayne.com/post/constructive/">predicative data type</a>. The first step is to only export the type, but <em>not</em> its data constructor: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Restaurants&nbsp;( &nbsp;&nbsp;<span style="color:blue;">Natural</span>, &nbsp;&nbsp;<span style="color:green;">--&nbsp;More&nbsp;exports&nbsp;here...</span> &nbsp;&nbsp;)&nbsp;<span style="color:blue;">where</span></pre> </p> <p> But this makes it impossible for client code to create values of the type, so we need to supply a <a href="https://wiki.haskell.org/Smart_constructors">smart constructor</a>: </p> <p> <pre><span style="color:#2b91af;">tryNatural</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:#2b91af;">Integer</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:blue;">Natural</span> tryNatural&nbsp;n &nbsp;&nbsp;|&nbsp;n&nbsp;&lt;&nbsp;1&nbsp;=&nbsp;Nothing &nbsp;&nbsp;|&nbsp;<span style="color:blue;">otherwise</span>&nbsp;=&nbsp;Just&nbsp;(Natural&nbsp;n)</pre> </p> <p> In this, as well as the other articles in this series, I've chosen to model the potential for errors with <code>Maybe</code> values. I could also have chosen to use <code>Either</code> if I wanted to communicate information along the 'error channel', but sticking with <code>Maybe</code> makes the code a bit simpler. Not so much in Haskell or <a href="https://fsharp.org/">F#</a>, but once we reach C#, <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">applicative validation</a> becomes complicated. </p> <p> There's no loss of generality in this decision, since both <code>Maybe</code> and <code>Either</code> are <code>Applicative</code> instances. </p> <p> With the <code>tryNatural</code> function you can now (attempt to) create <code>Natural</code> values: </p> <p> <pre>ghci&gt; tryNatural (-1) Nothing ghci&gt; x = tryNatural 42 ghci&gt; x Just (Natural 42)</pre> </p> <p> This enables client developers to create <code>Natural</code> values, and due to the type's <code>Ord</code> instance, you can even compare them: </p> <p> <pre>ghci&gt; y = tryNatural 2112 ghci&gt; x &lt; y True</pre> </p> <p> Even so, there will be cases when you need to extract the underlying <code>Integer</code> from a <code>Natural</code> value. You could supply a normal function for that purpose, but in order to make some of the following code a little more elegant, I chose to do it with pattern synonyms: </p> <p> <pre>{-#&nbsp;COMPLETE&nbsp;N&nbsp;#-} pattern&nbsp;N&nbsp;::&nbsp;Integer&nbsp;-&gt;&nbsp;Natural pattern&nbsp;N&nbsp;i&nbsp;&lt;-&nbsp;Natural&nbsp;i</pre> </p> <p> That needs to be exported as well. </p> <p> So, eight lines of code to declare a predicative type that models a natural number. Incidentally, this'll be 2-3 lines of code in F#. </p> <h3 id="1e5a116c8a6f4a928cbea3f88eed42ad"> Domain Model <a href="#1e5a116c8a6f4a928cbea3f88eed42ad">#</a> </h3> <p> Modelling a restaurant table follows in the same vein. One invariant I would like to enforce is that for a 'single' table, the minimal reservation should be a <code>Natural</code> number less than or equal to the table's capacity. It doesn't make sense to configure a table for four with a minimum reservation of six. </p> <p> In the same spirit as above, then, define this type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;SingleTable&nbsp;=&nbsp;SingleTable &nbsp;&nbsp;{&nbsp;singleCapacity&nbsp;::&nbsp;Natural &nbsp;&nbsp;,&nbsp;minimalReservation&nbsp;::&nbsp;Natural &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Ord</span>,&nbsp;<span style="color:#2b91af;">Show</span>)</pre> </p> <p> Again, only export the type, but not its data constructor. In order to extract values, then, supply another pattern synonym: </p> <p> <pre>{-#&nbsp;COMPLETE&nbsp;SingleT&nbsp;#-} pattern&nbsp;SingleT&nbsp;::&nbsp;Natural&nbsp;-&gt;&nbsp;Natural&nbsp;-&gt;&nbsp;SingleTable pattern&nbsp;SingleT&nbsp;c&nbsp;m&nbsp;&lt;-&nbsp;SingleTable&nbsp;c&nbsp;m</pre> </p> <p> Finally, define a <code>Table</code> type and two smart constructors: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Table&nbsp;=&nbsp;Single&nbsp;SingleTable&nbsp;|&nbsp;Communal&nbsp;Natural&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) <span style="color:#2b91af;">trySingleTable</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:#2b91af;">Integer</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Integer</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:blue;">Table</span> trySingleTable&nbsp;capacity&nbsp;minimal&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;c&nbsp;&lt;-&nbsp;tryNatural&nbsp;capacity &nbsp;&nbsp;m&nbsp;&lt;-&nbsp;tryNatural&nbsp;minimal &nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;c&nbsp;&lt;&nbsp;m&nbsp;<span style="color:blue;">then</span>&nbsp;Nothing&nbsp;<span style="color:blue;">else</span>&nbsp;Just&nbsp;(Single&nbsp;(SingleTable&nbsp;c&nbsp;m)) <span style="color:#2b91af;">tryCommunalTable</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:#2b91af;">Integer</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:blue;">Table</span> tryCommunalTable&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;Communal&nbsp;.&nbsp;tryNatural</pre> </p> <p> Notice that <code>trySingleTable</code> checks the invariant that the <code>capacity</code> must be greater than or equal to the minimal reservation. </p> <p> The point of this little exercise, so far, is that it <em>encapsulates</em> the contract implied by the Domain Model. It does this by using the static type system to its advantage. </p> <h3 id="a0f3606b82374c349dc0bf116db14493"> JSON serialization by hand <a href="#a0f3606b82374c349dc0bf116db14493">#</a> </h3> <p> At the boundaries of applications, however, <a href="/2023/10/16/at-the-boundaries-static-types-are-illusory">there are no static types</a>. Is the static type system still useful in that situation? </p> <p> For Haskell, the most common JSON library is Aeson, and I admit that I'm no expert. Thus, it's possible that there's an easier way to serialize to and deserialize from JSON. If so, please leave a comment explaining the alternative. </p> <p> The original rationale for this article series was to demonstrate how serialization can be done without Reflection, or, in the case of Haskell, <a href="https://hackage.haskell.org/package/base/docs/GHC-Generics.html">Generics</a> (not to be confused with .NET generics, which in Haskell usually is called <em>parametric polymorphism</em>). We'll return to Generics later in this article. </p> <p> In this article series, I consider the JSON format fixed. A single table should be rendered as shown above, and a communal table should be rendered like this: </p> <p> <pre>{&nbsp;<span style="color:#2e75b6;">&quot;communalTable&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;42&nbsp;}&nbsp;}</pre> </p> <p> Often in the real world you'll have to conform to a particular protocol format, or, even if that's not the case, being able to control the shape of the wire format is important to deal with backwards compatibility. </p> <p> As I outlined in the <a href="/2023/12/04/serialization-with-and-without-reflection">introduction article</a> you can usually find a more weakly typed API to get the job done. For serializing <code>Table</code> to JSON it looks like this: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;JSONTable&nbsp;=&nbsp;JSONTable&nbsp;Table&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">ToJSON</span>&nbsp;<span style="color:blue;">JSONTable</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;toJSON&nbsp;(JSONTable&nbsp;(Single&nbsp;(SingleT&nbsp;(N&nbsp;c)&nbsp;(N&nbsp;m))))&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;object&nbsp;[<span style="color:#a31515;">&quot;singleTable&quot;</span>&nbsp;.=&nbsp;object&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;capacity&quot;</span>&nbsp;.=&nbsp;c, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;minimalReservation&quot;</span>&nbsp;.=&nbsp;m]] &nbsp;&nbsp;toJSON&nbsp;(JSONTable&nbsp;(Communal&nbsp;(N&nbsp;c)))&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;object&nbsp;[<span style="color:#a31515;">&quot;communalTable&quot;</span>&nbsp;.=&nbsp;object&nbsp;[<span style="color:#a31515;">&quot;capacity&quot;</span>&nbsp;.=&nbsp;c]]</pre> </p> <p> In order to separate concerns, I've defined this functionality in a new module that references the module that defines the Domain Model. Thus, to avoid orphan instances, I've defined a <code>JSONTable</code> <code>newtype</code> wrapper that I then make a <code>ToJSON</code> instance. </p> <p> The <code>toJSON</code> function pattern-matches on <code>Single</code> and <code>Communal</code> to write two different <a href="https://hackage.haskell.org/package/aeson/docs/Data-Aeson.html#t:Value">Values</a>, using Aeson's underlying Document Object Model (DOM). </p> <h3 id="9c3c05517701474d8ac88c59e6fd11e7"> JSON deserialization by hand <a href="#9c3c05517701474d8ac88c59e6fd11e7">#</a> </h3> <p> You can also go the other way, and when it looks more complicated, it's because it is. When serializing an encapsulated value, not a lot can go wrong because the value is already valid. When deserializing a JSON string, on the other hand, all sorts of things can go wrong: It might not even be a valid string, or the string may not be valid JSON, or the JSON may not be a valid <code>Table</code> representation, or the values may be illegal, etc. </p> <p> It's no surprise, then, that the <code>FromJSON</code> instance is bigger: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">FromJSON</span>&nbsp;<span style="color:blue;">JSONTable</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;parseJSON&nbsp;(Object&nbsp;v)&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;single&nbsp;&lt;-&nbsp;v&nbsp;.:?&nbsp;<span style="color:#a31515;">&quot;singleTable&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;communal&nbsp;&lt;-&nbsp;v&nbsp;.:?&nbsp;<span style="color:#a31515;">&quot;communalTable&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;(single,&nbsp;communal)&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Just&nbsp;s,&nbsp;Nothing)&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;capacity&nbsp;&lt;-&nbsp;s&nbsp;.:&nbsp;<span style="color:#a31515;">&quot;capacity&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minimal&nbsp;&lt;-&nbsp;s&nbsp;.:&nbsp;<span style="color:#a31515;">&quot;minimalReservation&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;trySingleTable&nbsp;capacity&nbsp;minimal&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;-&gt;&nbsp;<span style="color:blue;">fail</span>&nbsp;<span style="color:#a31515;">&quot;Expected&nbsp;natural&nbsp;numbers.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;t&nbsp;-&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;JSONTable&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Nothing,&nbsp;Just&nbsp;c)&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;capacity&nbsp;&lt;-&nbsp;c&nbsp;.:&nbsp;<span style="color:#a31515;">&quot;capacity&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;tryCommunalTable&nbsp;capacity&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;-&gt;&nbsp;<span style="color:blue;">fail</span>&nbsp;<span style="color:#a31515;">&quot;Expected&nbsp;a&nbsp;natural&nbsp;number.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;t&nbsp;-&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;JSONTable&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;-&gt;&nbsp;<span style="color:blue;">fail</span>&nbsp;<span style="color:#a31515;">&quot;Expected&nbsp;exactly&nbsp;one&nbsp;of&nbsp;singleTable&nbsp;or&nbsp;communalTable.&quot;</span> &nbsp;&nbsp;parseJSON&nbsp;_&nbsp;=&nbsp;<span style="color:blue;">fail</span>&nbsp;<span style="color:#a31515;">&quot;Expected&nbsp;an&nbsp;object.&quot;</span></pre> </p> <p> I could probably have done this more succinctly if I'd spent even more time on it than I already did, but it gets the job done and demonstrates the point. Instead of relying on run-time Reflection, the <code>FromJSON</code> instance is, unsurprisingly, a parser, composed from Aeson's specialised parser combinator API. </p> <p> Since both serialisation and deserialization is based on string values, you should write automated tests that verify that the code works. </p> <p> Apart from module declaration and imports etc. this hand-written JSON capability requires 27 lines of code. Can we do better with static types and Generics? </p> <h3 id="ddac03fba5134f0da9e613c29888ce83"> JSON serialisation based on types <a href="#ddac03fba5134f0da9e613c29888ce83">#</a> </h3> <p> The intent with the Aeson library is that you define a type (a <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a> (DTO) if you will), and then let 'compiler magic' do the rest. In Haskell, it's not run-time Reflection, but a compilation technology called Generics. As I understand it, it automatically 'writes' the serialization and parsing code and turns it into machine code as part of normal compilation. </p> <p> You're supposed to first turn on the </p> <p> <pre>{-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;DeriveGeneric&nbsp;#-}</pre> </p> <p> language pragma and then tell the compiler to automatically derive <code>Generic</code> for the DTO in question. You'll see an example of that shortly. </p> <p> It's a fairly flexible system that you can tweak in various ways, but if it's possible to do it directly with the above <code>Table</code> type, please leave a comment explaining how. I tried, but couldn't make it work. To be clear, I <em>could</em> make it serializable, but not to the above JSON format. <ins datetime="2023-12-11T11:06Z">After enough <a href="/2023/10/02/dependency-whac-a-mole">Aeson Whac-A-Mole</a> I decided to change tactics.</ins> </p> <p> In <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> I explain how you're usually better off separating the role of serialization from the role of Domain Model. The way to do that is exactly by defining a DTO for serialisation, and let the Domain Model remain exclusively to model the rules of the application. The above <code>Table</code> type plays the latter role, so we need new DTO types. </p> <p> We may start with the building blocks: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;CommunalDTO&nbsp;=&nbsp;CommunalDTO &nbsp;&nbsp;{&nbsp;communalCapacity&nbsp;::&nbsp;Integer &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Generic</span>)</pre> </p> <p> Notice how it declaratively derives <code>Generic</code>, which works because of the <code>DeriveGeneric</code> language pragma. </p> <p> From here, in principle, all that you need is just a single declaration to make it serializable: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">ToJSON</span>&nbsp;<span style="color:blue;">CommunalDTO</span></pre> </p> <p> While it does serialize to JSON, it doesn't have the right format: </p> <p> <pre>{&nbsp;<span style="color:#2e75b6;">&quot;communalCapacity&quot;</span>:&nbsp;42&nbsp;}</pre> </p> <p> The property name should be <code>capacity</code>, not <code>communalCapacity</code>. Why did I call the record field <code>communalCapacity</code> instead of <code>capacity</code>? Can't I just fix my <code>CommunalDTO</code> record? </p> <p> Unfortunately, I can't just do that, because I also need a <code>capacity</code> JSON property for the single-table case, and Haskell isn't happy about duplicated field names in the same module. (This language feature truly is one of the weak points of Haskell.) </p> <p> Instead, I can tweak the Aeson rules by supplying an <code>Options</code> value to the instance definition: </p> <p> <pre><span style="color:#2b91af;">communalJSONOptions</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Options</span> communalJSONOptions&nbsp;= &nbsp;&nbsp;defaultOptions&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;fieldLabelModifier&nbsp;=&nbsp;\s&nbsp;-&gt;&nbsp;<span style="color:blue;">case</span>&nbsp;s&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;communalCapacity&quot;</span>&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;capacity&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;-&gt;&nbsp;s&nbsp;} <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">ToJSON</span>&nbsp;<span style="color:blue;">CommunalDTO</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;toJSON&nbsp;=&nbsp;genericToJSON&nbsp;communalJSONOptions &nbsp;&nbsp;toEncoding&nbsp;=&nbsp;genericToEncoding&nbsp;communalJSONOptions</pre> </p> <p> This instructs the compiler to modify how it generates the serialization code, and the generated JSON fragment is now correct. </p> <p> We can do the same with the single-table case: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;SingleDTO&nbsp;=&nbsp;SingleDTO &nbsp;&nbsp;{&nbsp;singleCapacity&nbsp;::&nbsp;Integer &nbsp;&nbsp;,&nbsp;minimalReservation&nbsp;::&nbsp;Integer &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Generic</span>) <span style="color:#2b91af;">singleJSONOptions</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Options</span> singleJSONOptions&nbsp;= &nbsp;&nbsp;defaultOptions&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;fieldLabelModifier&nbsp;=&nbsp;\s&nbsp;-&gt;&nbsp;<span style="color:blue;">case</span>&nbsp;s&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;singleCapacity&quot;</span>&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;capacity&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;minimalReservation&quot;</span>&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;minimalReservation&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;-&gt;&nbsp;s&nbsp;} <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">ToJSON</span>&nbsp;<span style="color:blue;">SingleDTO</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;toJSON&nbsp;=&nbsp;genericToJSON&nbsp;singleJSONOptions &nbsp;&nbsp;toEncoding&nbsp;=&nbsp;genericToEncoding&nbsp;singleJSONOptions</pre> </p> <p> This takes care of that case, but we still need a container type that will hold either one or the other: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;TableDTO&nbsp;=&nbsp;TableDTO &nbsp;&nbsp;{&nbsp;singleTable&nbsp;::&nbsp;Maybe&nbsp;SingleDTO &nbsp;&nbsp;,&nbsp;communalTable&nbsp;::&nbsp;Maybe&nbsp;CommunalDTO &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Generic</span>) <span style="color:#2b91af;">tableJSONOptions</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Options</span> tableJSONOptions&nbsp;= &nbsp;&nbsp;defaultOptions&nbsp;{&nbsp;omitNothingFields&nbsp;=&nbsp;True&nbsp;} <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">ToJSON</span>&nbsp;<span style="color:blue;">TableDTO</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;toJSON&nbsp;=&nbsp;genericToJSON&nbsp;tableJSONOptions &nbsp;&nbsp;toEncoding&nbsp;=&nbsp;genericToEncoding&nbsp;tableJSONOptions</pre> </p> <p> One way to model a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> with a DTO is to declare both cases as <code>Maybe</code> fields. While it does allow illegal states to be representable (i.e. both kinds of tables defined at the same time, or none of them present) this is only par for the course at the application boundary. </p> <p> That's quite a bit of infrastructure to stand up, but at least most of it can be reused for parsing. </p> <h3 id="0a507081076c4afe9e2197f013f6f107"> JSON deserialisation based on types <a href="#0a507081076c4afe9e2197f013f6f107">#</a> </h3> <p> To allow parsing of JSON into the above DTO we can make them all <code>FromJSON</code> instances, e.g.: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">FromJSON</span>&nbsp;<span style="color:blue;">CommunalDTO</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;parseJSON&nbsp;=&nbsp;genericParseJSON&nbsp;communalJSONOptions</pre> </p> <p> Notice that you can reuse the same <code>communalJSONOptions</code> used for the <code>ToJSON</code> instance. Repeat that exercise for the two other record types. </p> <p> That's only half the battle, though, since this only gives you a way to parse and serialize the DTO. What you ultimately want is to persist or dehydrate <code>Table</code> data. </p> <h3 id="7e234848ee2d48b8a1da42c0e05bb088"> Converting DTO to Domain Model, and vice versa <a href="#7e234848ee2d48b8a1da42c0e05bb088">#</a> </h3> <p> As usual, converting a nice, encapsulated value to a more relaxed format is safe and trivial: </p> <p> <pre><span style="color:#2b91af;">toTableDTO</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Table</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">TableDTO</span> toTableDTO&nbsp;(Single&nbsp;(SingleT&nbsp;(N&nbsp;c)&nbsp;(N&nbsp;m)))&nbsp;=&nbsp;TableDTO&nbsp;(Just&nbsp;(SingleDTO&nbsp;c&nbsp;m))&nbsp;Nothing toTableDTO&nbsp;(Communal&nbsp;(N&nbsp;c))&nbsp;=&nbsp;TableDTO&nbsp;Nothing&nbsp;(Just&nbsp;(CommunalDTO&nbsp;c))</pre> </p> <p> Going the other way is fundamentally a parsing exercise: </p> <p> <pre><span style="color:#2b91af;">tryParseTable</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">TableDTO</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:blue;">Table</span> tryParseTable&nbsp;(TableDTO&nbsp;(Just&nbsp;(SingleDTO&nbsp;c&nbsp;m))&nbsp;Nothing)&nbsp;=&nbsp;trySingleTable&nbsp;c&nbsp;m tryParseTable&nbsp;(TableDTO&nbsp;Nothing&nbsp;(Just&nbsp;(CommunalDTO&nbsp;c)))&nbsp;=&nbsp;tryCommunalTable&nbsp;c tryParseTable&nbsp;_&nbsp;=&nbsp;Nothing</pre> </p> <p> Such an operation may fail, so the result is a <code>Maybe Table</code>. It could also have been an <code>Either something Table</code>, if you wanted to return information about errors when things go wrong. It makes the code marginally more complex, but doesn't change the overall thrust of this exploration. </p> <p> Let's take stock of the type-based alternative. It requires 62 lines of code, distributed over three DTO types, their <code>Options</code>, their <code>ToJSON</code> and <code>FromJSON</code> instances, and finally the two conversions <code>tryParseTable</code> and <code>toTableDTO</code>. </p> <h3 id="473295252dbf46bd93fc31b1d2f505e5"> Conclusion <a href="#473295252dbf46bd93fc31b1d2f505e5">#</a> </h3> <p> In this article I've explored two alternatives for converting a well-encapsulated Domain Model to and from JSON. One option is to directly manipulate the DOM. Another option is take a more declarative approach and define <em>types</em> that model the shape of the JSON data, and then leverage type-based automation (here, Generics) to automatically produce the code that parses and writes the JSON. </p> <p> I've deliberately chosen a Domain Model with some constraints, in order to demonstrate how persisting a non-trivial data model might work. With that setup, writing 'loosely coupled' code directly against the DOM requires 27 lines of code, while 'taking advantage' of type-based automation requires 62 lines of code. </p> <p> To be fair, the dice don't always land that way. You can't infer a general rule from a single example, and it's possible that I could have done something clever with Aeson to reduce the code. Even so, I think that there's a conclusion to be drawn, and it's this: </p> <p> Type-based automation (Generics, or run-time Reflection) may seem simple at first glance. Just declare a type and let some automation library do the rest. It may happen, however, that you need to tweak the defaults so much that it would be easier skipping the type-based approach and instead directly manipulating the DOM. </p> <p> I love static type systems, but I'm also watchful of their limitations. There's likely to be an inflection point where, on the one side, a type-based declarative API is best, while on the other side of that point, a more 'lightweight' approach is better. </p> <p> The position of such an inflection point will vary from context to context. Just be aware of the possibility, and explore alternatives if things begin to feel awkward. </p> <p> <strong>Next:</strong> <a href="/2023/12/18/serializing-restaurant-tables-in-f">Serializing restaurant tables in F#</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Serialization with and without Reflection https://blog.ploeh.dk/2023/12/04/serialization-with-and-without-reflection 2023-12-04T20:53:00+00:00 Mark Seemann <div id="post"> <p> <em>An investigation of alternatives.</em> </p> <p> I recently wrote a tweet that caused more responses than usual: </p> <blockquote> <p> "A decade ago, I used .NET Reflection so often that I know most the the API by heart. </p> <p> "Since then, I've learned better ways to solve my problems. I can't remember when was the last time I used .NET Reflection. I never need it. </p> <p> "Do you?" </p> <footer><cite><a href="https://twitter.com/ploeh/status/1727280699051495857">me</a></cite></footer> </blockquote> <p> Most people who read my tweets are programmers, and some are, perhaps, not entirely neurotypical, but I intended the last paragraph to be a <a href="https://en.wikipedia.org/wiki/Rhetorical_question">rhetorical question</a>. My point, really, was to point out that if I tell you it's possible to do without Reflection, one or two readers might keep that in mind and at least explore options the next time the urge to use Reflection arises. </p> <p> A common response was that Reflection is useful for (de)serialization of data. These days, the most common case is going to and from <a href="https://en.wikipedia.org/wiki/JSON">JSON</a>, but the problem is similar if the format is <a href="https://en.wikipedia.org/wiki/XML">XML</a>, <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a>, or another format. In a sense, even <a href="/2023/10/16/at-the-boundaries-static-types-are-illusory">reading to and from a database is a kind of serialization</a>. </p> <p> In this little series of articles, I'm going to explore some alternatives to Reflection. I'll use the same example throughout, and I'll stick to JSON, but you can easily extrapolate to other serialization formats. </p> <h3 id="4b1340f8fad4452f950c3cfdde275365"> Table layouts <a href="#4b1340f8fad4452f950c3cfdde275365">#</a> </h3> <p> As always, I find the example domain of online restaurant reservation systems to be so rich as to furnish a useful example. Imagine a multi-tenant service that enables restaurants to take and manage reservations. </p> <p> When a new reservation request arrives, the system has to make a decision on whether to accept or reject the request. The layout, or configuration, of <a href="/2020/01/27/the-maitre-d-kata">tables plays a role in that decision</a>. </p> <p> Such a multi-tenant system may have an API for configuring the restaurant; essentially, entering data into the system about the size and policies regarding tables in a particular restaurant. </p> <p> Most restaurants have 'normal' tables where, if you reserve a table for three, you'll have the entire table for a duration. Some restaurants also have one or more communal tables, typically bar seating where you may get a view of the kitchen. Quite a few high-end restaurants have tables like these, because it enables them to cater to single diners without reserving an entire table that could instead have served two paying customers. </p> <p> <img src="/content/binary/ernst.jpg" alt="Bar seating at Ernst, Berlin."> </p> <p> In Copenhagen, on the other hand, it's also not uncommon to have a special room for larger parties. I think this has something to do with the general age of the buildings in the city. Most establishments are situated in older buildings, with all the trappings, including load-bearing walls, cellars, etc. As part of a restaurant's location, there may be a big cellar room, second-story room, or other room that's not practical for the daily operation of the place, but which works for parties of, say, 15-30 people. Such 'private dining' rooms can be used for private occasions or company outings. </p> <p> A <a href="https://en.wikipedia.org/wiki/Ma%C3%AEtre_d%27h%C3%B4tel">maître d'hôtel</a> may wish to configure the system with a variety of tables, including communal tables, and private dining tables as described above. </p> <p> One way to model such requirements is to distinguish between two kinds of tables: Communal tables, and 'single' tables, and where single tables come with an additional property that models the minimal reservation required to reserve that table. A JSON representation might look like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;singleTable&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;16, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;minimalReservation&quot;</span>:&nbsp;10 &nbsp;&nbsp;} }</pre> </p> <p> This may represent a private dining table that seats up to sixteen people, and where the maître d'hôtel has decided to only accept reservations for at least ten guests. </p> <p> A <code>singleTable</code> can also be used to model 'normal' tables without special limits. If the restaurant has a table for four, but is ready to accept a reservation for one person, you can configure a table for four, with a minimum reservation of one. </p> <p> Communal tables are different, though: </p> <p> <pre>{&nbsp;<span style="color:#2e75b6;">&quot;communalTable&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;capacity&quot;</span>:&nbsp;10&nbsp;}&nbsp;}</pre> </p> <p> Why not just model that as ten single tables that each seat one? </p> <p> You don't want to do that because you want to make sure that parties can eat together. Some restaurants have more than one communal table. Imagine that you only have two communal tables of ten seats each. What happens if you model this as twenty single-person tables? </p> <p> If you do that, you may accept reservations for parties of six, six, and six, because <em>6&nbsp;+&nbsp;6&nbsp;+&nbsp;6&nbsp;=&nbsp;18&nbsp;&lt;&nbsp;20</em>. When those three groups arrive, however, you discover that you have to split one of the parties! The party getting separated may not like that at all, and you are, after all, in the hospitality business. </p> <h3 id="368ebde4f05e434b938327469aba1640"> Exploration <a href="#368ebde4f05e434b938327469aba1640">#</a> </h3> <p> In each article in this short series, I'll explore serialization with and without Reflection in a few languages. I'll start with <a href="https://www.haskell.org/">Haskell</a>, since that language doesn't have run-time Reflection. It does have a related facility called <em>generics</em>, not to be confused with .NET or Java generics, which in Haskell are called <em>parametric polymorphism</em>. It's confusing, I know. </p> <p> Haskell generics look a bit like .NET Reflection, and there's some overlap, but it's not quite the same. The main difference is that Haskell generic programming all 'resolves' at compile time, so there's no run-time Reflection in Haskell. </p> <p> If you don't care about Haskell, you can skip that article. </p> <ul> <li><a href="/2023/12/11/serializing-restaurant-tables-in-haskell">Serializing restaurant tables in Haskell</a></li> <li><a href="/2023/12/18/serializing-restaurant-tables-in-f">Serializing restaurant tables in F#</a></li> <li><a href="/2023/12/25/serializing-restaurant-tables-in-c">Serializing restaurant tables in C#</a></li> </ul> <p> As you can see, the next article repeats the exercise in <a href="https://fsharp.org/">F#</a>, and if you also don't care about that language, you can skip that article as well. </p> <p> The C# article, on the other hand, should be readable to not only C# programmers, but also developers who work in sufficiently equivalent languages. </p> <h3 id="f367d5adccbe4d3792acc8f3828add85"> Descriptive, not prescriptive <a href="#f367d5adccbe4d3792acc8f3828add85">#</a> </h3> <p> The purpose of this article series is only to showcase alternatives. Based on the reactions my tweet elicited I take it that some people can't imagine how serialisation might look without Reflection. </p> <p> It is <em>not</em> my intent that you should eschew the Reflection-based APIs available in your languages. In .NET, for example, a framework like ASP.NET MVC expects you to model JSON or XML as <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects</a>. This gives you an <a href="/2023/10/16/at-the-boundaries-static-types-are-illusory">illusion of static types at the boundary</a>. </p> <p> Even a Haskell web library like <a href="https://www.servant.dev/">Servant</a> expects you to model web APIs with static types. </p> <p> When working with such a framework, it doesn't always pay to fight against its paradigm. When I work with ASP.NET, I define DTOs just like everyone else. On the other hand, if communicating with a backend system, I <a href="/2022/01/03/to-id-or-not-to-id">sometimes choose to skip static types and instead working directly with a JSON Document Object Model</a> (DOM). </p> <p> I occasionally find that it better fits my use case, but it's not the majority of times. </p> <h3 id="9af8390d4b1b4c50ae9148ea2eb841ad"> Conclusion <a href="#9af8390d4b1b4c50ae9148ea2eb841ad">#</a> </h3> <p> While some sort of Reflection or metadata-driven mechanism is often used to implement serialisation, it often turns out that such convenient language capabilities are programmed on top of an ordinary object model. Even isolated to .NET, I think I'm on my third JSON library, and most (all?) turned out to have an underlying DOM that you can manipulate. </p> <p> In this article I've set the stage for exploring how serialisation can work, with or (mostly) without Reflection. </p> <p> If you're interested in the philosophy of science and <a href="https://en.wikipedia.org/wiki/Epistemology">epistemology</a>, you may have noticed a recurring discussion in academia: A wider society benefits not only from learning what works, but also from learning what doesn't work. It would be useful if researchers published their failures along with their successes, yet few do (for fairly obvious reasons). </p> <p> Well, I depend neither on research grants nor salary, so I'm free to publish negative results, such as they are. </p> <p> Not that I want to go so far as to categorize what I present in the present articles as useless, but they're probably best applied in special circumstances. On the other hand, I don't know <em>your context</em>, and perhaps you're doing something I can't even imagine, and what I present here is just what you need. </p> <p> <strong>Next:</strong> <a href="/2023/12/11/serializing-restaurant-tables-in-haskell">Serializing restaurant tables in Haskell</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="db4a9a94452a1237bf71989561dfd947"> <div class="comment-author">gdifolco <a href="#db4a9a94452a1237bf71989561dfd947">#</a></div> <div class="comment-content"> <q> <i> I'll start with <a href="https://www.haskell.org/">Haskell</a>, since that language doesn't have run-time Reflection. </i> </q> <p> Haskell (the language) does not provide primitives to access to data representation <i>per-se</i>, during the compilation GHC (the compiler) erase a lot of information (more or less depending on the profiling flags) in order to provide to the run-time system (RTS) a minimal "bytecode". </p> <p> That being said, provide three ways to deal structurally with values: <ul> <li><a href="https://wiki.haskell.org/Template_Haskell">TemplateHaskell</a>: give the ability to rewrite the AST at compile-time</li> <li><a href="https://hackage.haskell.org/package/base-4.19.0.0/docs/GHC-Generics.html#t:Generic">Generics</a>: give the ability to have a type-level representation of a type structure</li> <li><a href="https://hackage.haskell.org/package/base-4.19.0.0/docs/Type-Reflection.html#t:Typeable">Typeable</a>: give the ability to have a value-level representation of a type structure</li> </ul> </p> <p> <i>Template Haskell</i> is low-level as it implies to deal with the AST, it is also harder to debug as, in order to be evaluated, the main compilation phase is stopped, then the <i>Template Haskell</i> code is ran, and finally the main compilation phase continue. It also causes compilation cache issues. </p> <p> <i>Generics</i> take type's structure and generate a representation at type-level, the main idea is to be able to go back and forth between the type and its representation, so you can define so behavior over a structure, the good thing being that, since the representation is known at compile-time, many optimizations can be done. On complex types it tends to slow-down compilation and produce larger-than-usual binaries, it is generraly the way libraries are implemented. </p> <p> <i>Typeable</i> is a purely value-level type representation, you get only on type whatever the type structure is, it is generally used when you have "dynamic" types, it provides safe ways to do coercion. </p> <p> Haskell tends to push as much things as possible in compile-time, this may explain this tendency. </p> </div> <div class="comment-date">2023-12-05 21:47 UTC</div> </div> <div class="comment" id="9cf7ca25c1364eb39c489d9883d90cea"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9cf7ca25c1364eb39c489d9883d90cea">#</a></div> <div class="comment-content"> <p> Thank you for writing. I was already aware of Template Haskell and Generics, but Typeable is new to me. I don't consider the first two equivalent to Reflection, since they resolve at compile time. They're more akin to automated code generation, I think. Reflection, as I'm used to it from .NET, is a run-time feature where you can inspect and interact with types and values as the code is executing. </p> <p> I admit that I haven't had the time to more than browse the documentation of Typeable, and it's so abstract that it's not clear to me what it does, how it works, or whether it's actually comparable to Reflection. The first question that comes to my mind regards the type class <code>Typeable</code> itself. It has no instances. Is it one of those special type classes (like <a href="https://hackage.haskell.org/package/base/docs/Data-Coerce.html#t:Coercible">Coercible</a>) that one doesn't have to explicitly implement? </p> </div> <div class="comment-date">2023-12-08 17:11 UTC</div> </div> <div class="comment" id="d3ad8ff86d11b285e69adc3ebbc74165"> <div class="comment-author">gdifolco <a href="#d3ad8ff86d11b285e69adc3ebbc74165">#</a></div> <div class="comment-content"> <blockquote> I don't consider the first two equivalent to Reflection, since they resolve at compile time. They're more akin to automated code generation, I think. Reflection, as I'm used to it from .NET, is a run-time feature where you can inspect and interact with types and values as the code is executing. </blockquote> <p> I don't know the .NET ecosystem well, but I guess you can borrow information at run-time we have at compile-time with TemplateHaskell and Generics, I think you are right then. </p> <blockquote> I admit that I haven't had the time to more than browse the documentation of Typeable, and it's so abstract that it's not clear to me what it does, how it works, or whether it's actually comparable to Reflection. The first question that comes to my mind regards the type class <code>Typeable</code> itself. It has no instances. Is it one of those special type classes (like <a href="https://hackage.haskell.org/package/base/docs/Data-Coerce.html#t:Coercible">Coercible</a>) that one doesn't have to explicitly implement? </blockquote> <p> You can derive <code>Typeable</code> as any othe type classes: </p> <pre> data MyType = MyType { myString :: String, myInt :: Int } deriving stock (Eq, Show, Typeable) </pre> <p> It's pretty "low-level", <code>typeRep</code> gives you a <code>TypeRep a</code> (<code>a</code> being the type represented) which is <a href="https://hackage.haskell.org/package/base-4.19.0.0/docs/Type-Reflection.html#v:App">a representation of the type with primitive elements</a> (<a href="https://www.youtube.com/watch?v=uR_VzYxvbxg">More details here</a>). </p> <p> Then, you'll be able to either pattern match on it, or <a href="https://hackage.haskell.org/package/base-4.19.0.0/docs/Data-Typeable.html#v:cast">cast it</a> (which it not like casting in Java for example, because you are just proving to the compiler that two types are equivalent). </p> </div> <div class="comment-date">2023-12-11 17:11 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Synchronizing concurrent teams https://blog.ploeh.dk/2023/11/27/synchronizing-concurrent-teams 2023-11-27T08:43:00+00:00 Mark Seemann <div id="post"> <p> <em>Or, rather: Try not to.</em> </p> <p> A few months ago I visited a customer and as the day was winding down we got to talk more informally. One of the architects mentioned, in an almost off-hand manner, "we've embarked on a <a href="https://en.wikipedia.org/wiki/Scaled_agile_framework">SAFe</a> journey..." </p> <p> "Yes..?" I responded, hoping that my inflection would sound enough like a question that he'd elaborate. </p> <p> Unfortunately, I'm apparently sometimes too subtle when dealing with people face-to-face, so I never got to hear just how that 'SAFe journey' was going. Instead, the conversation shifted to the adjacent topic of how to coordinate independent teams. </p> <p> I told them that, in my opinion, the best way to coordinate independent teams is to <em>not</em> coordinate them. I don't remember exactly how I proceeded from there, but I probably said something along the lines that I consider coordination meetings between teams to be an 'architecture smell'. That the need to talk to other teams was a symptom that teams were too tightly coupled. </p> <p> I don't remember if I said exactly that, but it would have been in character. </p> <p> The architect responded: "I don't like silos." </p> <p> How do you respond to that? </p> <h3 id="c5fdd5e722994d8fb75bb3f32f1cf86b"> Autonomous teams <a href="#c5fdd5e722994d8fb75bb3f32f1cf86b">#</a> </h3> <p> I couldn't very well respond that <em>silos are great</em>. First, it doesn't sound very convincing. Second, it'd be an argument suitable only in a kindergarten. <em>Are not! -Are too! -Not! -Too!</em> etc. </p> <p> After feeling momentarily <a href="https://en.wikipedia.org/wiki/Check_(chess)">checked</a>, for once I managed to think on my feet, so I replied, "I don't suggest that your teams should be isolated from each other. I do encourage people to talk to each other, but I don't think that teams should <em>coordinate</em> much. Rather, think of each team as an organism on the savannah. They interact, and what they do impact others, but in the end they're autonomous life forms. I believe an architect's job is like a ranger's. You can't control the plants or animals, but you can nurture the ecosystem, herding it in a beneficial direction." </p> <p> <img src="/content/binary/samburu.jpg" alt="Gazelles and warthogs in Samburu National Reserve, Kenya."> </p> <p> That ranger metaphor is an old pet peeve of mine, originating from what I consider one of my most under-appreciated articles: <a href="/2012/12/18/ZookeepersmustbecomeRangers">Zookeepers must become Rangers</a>. It's closely related to the more popular metaphor of software architecture as gardening, but I like the wildlife variation because it emphasizes an even more hands-off approach. It removes the illusion that you can control a fundamentally unpredictable process, but replaces it with the hopeful emphasis on stewardship. </p> <p> How do ecosystems thrive? A software architect (or ranger) should nurture resilience in each subsystem, just like evolution has promoted plants' and animals' ability to survive a variety of unforeseen circumstances: Flood, draught, fire, predators, lack of prey, disease, etc. </p> <p> You want teams to work independently. This doesn't mean that they work in isolation, but rather they they are free to act according to their abilities and understanding of the situation. An architect can help them understand the wider ecosystem and predict tomorrow's weather, so to speak, but the team should remain autonomous. </p> <h3 id="d0637b2597964cfb9bf6ffd3f0559fd0"> Concurrent work <a href="#d0637b2597964cfb9bf6ffd3f0559fd0">#</a> </h3> <p> I'm assuming that an organisation has multiple teams because they're supposed to work concurrently. While team A is off doing one thing, team B is doing something else. You can attempt to herd them in the same general direction, but beware of tight coordination. </p> <p> What's the problem with coordination? Isn't it a kind of collaboration? Don't we consider that beneficial? </p> <p> I'm not arguing that teams should be antagonistic. Like all metaphors, we should be careful not to take the savannah metaphor too far. I'm not imagining that one team consists of lions, apex predators, killing and devouring other teams. </p> <p> Rather, the reason I'm wary of coordination is because it seems synonymous with <em>synchronisation</em>. </p> <p> In <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> I've already discussed how good practices for Continuous Integration are similar to earlier lessons about <a href="https://en.wikipedia.org/wiki/Optimistic_concurrency_control">optimistic concurrency</a>. It recently struck me that we can draw a similar parallel between concurrent team work and parallel computing. </p> <p> For decades we've known that the less synchronization, the faster parallel code is. Synchronization is costly. </p> <p> In team work, coordination is like thread synchronization. Instead of doing work, you stop in order to coordinate. This implies that one thread or team has to wait for the other to catch up. </p> <p> <img src="/content/binary/sync-wait.png" alt="Two horizontal bars presenting two processes, A and B. A is shorter than B, indicating that it finishes first."> </p> <p> Unless work is perfectly evenly divided, team A may finish before team B. In order to coordinate, team A must sit idle for a while, waiting for B to catch up. (In development organizations, <em>idleness</em> is rarely allowed, so in practice, team A embarks on some other work, with <a href="/2012/12/18/ZookeepersmustbecomeRangers">consequences that I've already outlined</a>.) </p> <p> If you have more than two teams, this phenomenon only becomes worse. You'll have more idle time. This reminds me of <a href="https://en.wikipedia.org/wiki/Amdahl%27s_law">Amdahl's law</a>, which briefly put expresses that there's a limit to how much of a speed improvement you can get from concurrent work. The limit is related to the percentage of the work that can <em>not</em> be parallelized. The greater the need to synchronize work, the lower the ceiling. Conversely, the more you can let concurrent processes run without coordination, the more you gain from parallelization. </p> <p> It seems to me that there's a direct counterpart in team organization. The more teams need to coordinate, the less is gained from having multiple teams. </p> <p> But really, <a href="/ref/mythical-man-month">Fred Brooks could you have told you so in 1975</a>. </p> <h3 id="11f48be968f94f67b42d0934a4d08501"> Versioning <a href="#11f48be968f94f67b42d0934a4d08501">#</a> </h3> <p> A small development team may organize work informally. Work may be divided along 'natural' lines, each developer taking on tasks best suited to his or her abilities. If working in a code base with shared ownership, one developer doesn't <em>have</em> to wait on the work done by another developer. Instead, a programmer may complete the required work individually, or working together with a colleague. Coordination happens, but is both informal and frequent. </p> <p> As development organizations grow, teams are formed. Separate teams are supposed to work independently, but may in practice often depend on each other. Team A may need team B to make a change before they can proceed with their own work. The (felt) need to coordinate team activities arise. </p> <p> In my experience, this happens for a number of reasons. One is that teams may be divided along wrong lines; this is a socio-technical problem. Another, more technical, reason is that <a href="/2012/12/18/RangersandZookeepers">zookeepers</a> rarely think explicitly about versioning or avoiding breaking changes. Imagine that team A needs team B to develop a new capability. This new capability <em>implies</em> a breaking change, so the teams will now need to coordinate. </p> <p> Instead, team B should develop the new feature in such a way that it doesn't break existing clients. If all else fails, the new feature must exist side-by-side with the old way of doing things. With <a href="https://en.wikipedia.org/wiki/Continuous_deployment">Continuous Deployment</a> the new feature becomes available when it's ready. Team A still has to <em>wait</em> for the feature to become available, but no <em>synchronization</em> is required. </p> <h3 id="def545737be9487da7c4b01dcc3eb106"> Conclusion <a href="#def545737be9487da7c4b01dcc3eb106">#</a> </h3> <p> Yet another lesson about thread-safety and concurrent transactions seems to apply to people and processes. Parallel processes should be autonomous, with as little synchronization as possible. The more you coordinate development teams, the more you limit the speed of overall work. This seems to suggest that something akin to Amdahl's law also applies to development organizations. </p> <p> Instead of coordinating teams, encourage them to exist as autonomous entities, but set things up so that <em>not breaking compatibility</em> is a major goal for each team. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Trimming a Fake Object https://blog.ploeh.dk/2023/11/20/trimming-a-fake-object 2023-11-20T06:44:00+00:00 Mark Seemann <div id="post"> <p> <em>A refactoring example.</em> </p> <p> When I introduce the <a href="http://xunitpatterns.com/Fake%20Object.html">Fake Object</a> testing pattern to people, a common concern is the maintenance burden of it. The point of the pattern is that you write some 'working' code only for test purposes. At a glance, it seems as though it'd be more work than using a dynamic mock library like <a href="https://www.devlooped.com/moq/">Moq</a> or <a href="https://site.mockito.org/">Mockito</a>. </p> <p> This article isn't really about that, but the benefit of a Fake Object is that it has a <em>lower</em> maintenance footprint because it gives you a single class to maintain when you change interfaces or base classes. Dynamic mock objects, on the contrary, leads to <a href="https://en.wikipedia.org/wiki/Shotgun_surgery">Shotgun surgery</a> because every time you change an interface or base class, you have to revisit multiple tests. </p> <p> In a <a href="/2023/11/13/fakes-are-test-doubles-with-contracts">recent article</a> I presented a Fake Object that may have looked bigger than most people would find comfortable for test code. In this article I discuss how to trim it via a set of refactorings. </p> <h3 id="22e6648934bb4ae59aa1a181940172ae"> Original Fake read registry <a href="#22e6648934bb4ae59aa1a181940172ae">#</a> </h3> <p> The article presented this <code>FakeReadRegistry</code>, repeated here for your convenience: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeReadRegistry</span>&nbsp;:&nbsp;IReadRegistry { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IDictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;&nbsp;views; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FakeReadRegistry</span>(<span style="color:blue;">params</span>&nbsp;Room[]&nbsp;<span style="font-weight:bold;color:#1f377f;">rooms</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.rooms&nbsp;=&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Dictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;EnumerateDates(arrival,&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(GetView) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate(rooms.AsEnumerable(),&nbsp;Enumerable.Intersect) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;EnumerateDates(booking.Arrival,&nbsp;booking.Departure)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>&nbsp;=&nbsp;GetView(d); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newView</span>&nbsp;=&nbsp;QueryService.Reserve(booking,&nbsp;view); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views[d]&nbsp;=&nbsp;newView; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;DateOnly&gt;&nbsp;<span style="color:#74531f;">EnumerateDates</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&nbsp;arrival; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(d&nbsp;&lt;&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">yield</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;d; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&nbsp;d.AddDays(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(views.TryGetValue(date,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is 47 lines of code, spread over five members (including the constructor). Three of the methods have a <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> (CC) of <em>2</em>, which is the maximum for this class. The remaining two have a CC of <em>1</em>. </p> <p> While you <em>can</em> play some <a href="/2023/11/14/cc-golf">CC golf</a> with those CC-2 methods, that tends to pull the code in a direction of being less <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a>. For that reason, I chose to present the code as above. Perhaps more importantly, it doesn't save that many lines of code. </p> <p> Had this been a piece of production code, no-one would bat an eye at size or complexity, but this is test code. To add spite to injury, those 47 lines of code implement this two-method interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReadRegistry</span> { &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>); }</pre> </p> <p> Can we improve the situation? </p> <h3 id="49f78ebabf3d4875b469e935151c064a"> Root cause analysis <a href="#49f78ebabf3d4875b469e935151c064a">#</a> </h3> <p> Before you rush to 'improve' code, it pays to understand why it looks the way it looks. </p> <p> Code is a wonderfully malleable medium, so you should regard nothing as set in stone. On the other hand, there's often a reason it looks like it does. It <em>may</em> be that the previous programmers were incompetent ogres for hire, but often there's a better explanation. </p> <p> I've outlined my thinking process in <a href="/2023/11/13/fakes-are-test-doubles-with-contracts">the previous article</a>, and I'm not going to repeat it all here. To summarise, though, I've applied the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>. </p> <blockquote> <p> "clients [...] own the abstract interfaces" </p> <footer><cite>Robert C. Martin, <a href="/ref/appp">APPP</a>, chapter 11</cite></footer> </blockquote> <p> In other words, I let the needs of the clients guide the design of the <code>IReadRegistry</code> interface, and then the implementation (<code>FakeReadRegistry</code>) had to conform. </p> <p> But that's not the whole truth. </p> <p> I was doing a programming exercise - the <a href="https://codingdojo.org/kata/CQRS_Booking/">CQRS booking</a> kata - and I was following the instructions given in the description. They quite explicitly outline the two dependencies and their methods. </p> <p> When trying a new exercise, it's a good idea to follow instructions closely, so that's what I did. Once you get a sense of a kata, though, there's no law saying that you have to stick to the original rules. After all, the purpose of an exercise is to train, and in programming, <a href="/2020/01/13/on-doing-katas">trying new things is training</a>. </p> <h3 id="740cb249aff74666af7af4784cc166b8"> Test code that wants to be production code <a href="#740cb249aff74666af7af4784cc166b8">#</a> </h3> <p> A major benefit of test-driven development (TDD) is that it provides feedback. It pays to be tuned in to that channel. The above <code>FakeReadRegistry</code> seems to be trying to tell us something. </p> <p> Consider the <code>GetFreeRooms</code> method. I'll repeat the single-expression body here for your convenience: </p> <p> <pre><span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;EnumerateDates(arrival,&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;.Select(GetView) &nbsp;&nbsp;&nbsp;&nbsp;.Aggregate(rooms.AsEnumerable(),&nbsp;Enumerable.Intersect) &nbsp;&nbsp;&nbsp;&nbsp;.ToList();</pre> </p> <p> Why is that the implementation? Why does it need to first enumerate the dates in the requested interval? Why does it need to call <code>GetView</code> for each date? </p> <p> Why don't I just do the following and be done with it? </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeStorage</span>&nbsp;:&nbsp;Collection&lt;Booking&gt;,&nbsp;IWriteRegistry,&nbsp;IReadRegistry { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FakeStorage</span>(<span style="color:blue;">params</span>&nbsp;Room[]&nbsp;<span style="font-weight:bold;color:#1f377f;">rooms</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.rooms&nbsp;=&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">booked</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.Where(<span style="font-weight:bold;color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;b.Overlaps(arrival,&nbsp;departure)).ToList(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;rooms &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;!booked.Any(<span style="font-weight:bold;color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;b.RoomName&nbsp;==&nbsp;r.Name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Save</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(booking); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> To be honest, that's what I did <em>first</em>. </p> <p> While there are two interfaces, there's only one Fake Object implementing both. That's often an easy way to address the <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> and still keeping the Fake Object simple. </p> <p> This is much simpler than <code>FakeReadRegistry</code>, so why didn't I just keep that? </p> <p> I didn't feel it was an honest attempt at CQRS. In CQRS you typically write the data changes to one system, and then you have another logical process that propagates the information about the data modification to the <em>read</em> subsystem. There's none of that here. Instead of being based on one or more 'materialised views', the query is just that: A query. </p> <p> That was what I attempted to address with <code>FakeReadRegistry</code>, and I think it's a much more faithful CQRS implementation. It's also more complex, as CQRS tends to be. </p> <p> In both cases, however, it seems that there's some production logic trapped in the test code. Shouldn't <code>EnumerateDates</code> be production code? And how about the general 'algorithm' of <code>RoomBooked</code>: </p> <ul> <li>Enumerate the relevant dates</li> <li>Get the 'materialised' view for each date</li> <li>Calculate the new view for that date</li> <li>Update the collection of views for that date</li> </ul> <p> That seems like just enough code to warrant moving it to the production code. </p> <p> A word of caution before we proceed. When deciding to pull some of that test code into the production code, I'm making a decision about architecture. </p> <p> Until now, I'd been following the Dependency Inversion Principle closely. The interfaces exist because the client code needs them. Those interfaces could be implemented in various ways: You could use a relational database, a document database, files, blobs, etc. </p> <p> Once I decide to pull the above algorithm into the production code, I'm choosing a particular persistent data structure. This now locks the data storage system into a design where there's a persistent view per date, and another database of bookings. </p> <p> Now that I'd learned some more about the exercise, I felt confident making that decision. </p> <h3 id="d1515fe093394daf8cf9e4a9ec687770"> Template Method <a href="#d1515fe093394daf8cf9e4a9ec687770">#</a> </h3> <p> The first move I made was to create a superclass so that I could employ the <a href="https://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a> pattern: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReadRegistry</span>&nbsp;:&nbsp;IReadRegistry { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;EnumerateDates(arrival,&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(GetView) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate(Rooms.AsEnumerable(),&nbsp;Enumerable.Intersect) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;EnumerateDates(booking.Arrival,&nbsp;booking.Departure)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>&nbsp;=&nbsp;GetView(d); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newView</span>&nbsp;=&nbsp;QueryService.Reserve(booking,&nbsp;view); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UpdateView(d,&nbsp;newView); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">UpdateView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;Rooms&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryGetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;DateOnly&gt;&nbsp;<span style="color:#74531f;">EnumerateDates</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&nbsp;arrival; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(d&nbsp;&lt;&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">yield</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;d; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&nbsp;d.AddDays(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(TryGetView(date,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Rooms; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This looks similar to <code>FakeReadRegistry</code>, so how is this an improvement? </p> <p> The new <code>ReadRegistry</code> class is production code. It can, and should, be tested. (Due to the history of how we got here, <a href="/2023/11/13/fakes-are-test-doubles-with-contracts">it's already covered by tests</a>, so I'm not going to repeat that effort here.) </p> <p> True to the <a href="https://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a> pattern, three <code>abstract</code> members await a child class' implementation. These are the <code>UpdateView</code> and <code>TryGetView</code> methods, as well as the <code>Rooms</code> read-only property (glorified getter method). </p> <p> Imagine that in the production code, these are implemented based on file/document/blob storage - one per date. <code>TryGetView</code> would attempt to read the document from storage, <code>UpdateView</code> would create or modify the document, while <code>Rooms</code> returns a default set of rooms. </p> <p> A Test Double, however, can still use an in-memory dictionary: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeReadRegistry</span>&nbsp;:&nbsp;ReadRegistry { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IDictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;&nbsp;views; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;Rooms&nbsp;=&gt;&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FakeReadRegistry</span>(<span style="color:blue;">params</span>&nbsp;Room[]&nbsp;<span style="font-weight:bold;color:#1f377f;">rooms</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.rooms&nbsp;=&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Dictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">UpdateView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views[date]&nbsp;=&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryGetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;views.TryGetValue(date,&nbsp;<span style="color:blue;">out</span>&nbsp;view); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Each <code>override</code> is a one-liner with cyclomatic complexity <em>1</em>. </p> <h3 id="91153011891b4b7791acfe0edc65f997"> First round of clean-up <a href="#91153011891b4b7791acfe0edc65f997">#</a> </h3> <p> An abstract class is already a polymorphic object, so we no longer need the <code>IReadRegistry</code> interface. Delete that, and update all code accordingly. Particularly, the <code>QueryService</code> now depends on <code>ReadRegistry</code> rather than <code>IReadRegistry</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;ReadRegistry&nbsp;readRegistry; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">QueryService</span>(ReadRegistry&nbsp;<span style="font-weight:bold;color:#1f377f;">readRegistry</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.readRegistry&nbsp;=&nbsp;readRegistry; }</pre> </p> <p> Now move the <code>Reserve</code> function from <code>QueryService</code> to <code>ReadRegistry</code>. Once this is done, the <code>QueryService</code> looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">QueryService</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;ReadRegistry&nbsp;readRegistry; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">QueryService</span>(ReadRegistry&nbsp;<span style="font-weight:bold;color:#1f377f;">readRegistry</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.readRegistry&nbsp;=&nbsp;readRegistry; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;readRegistry.GetFreeRooms(arrival,&nbsp;departure); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> That class is only passing method calls along, so clearly no longer serving any purpose. Delete it. </p> <p> This is a not uncommon in CQRS. One might even argue that if CQRS is done right, there's almost no code on the query side, since all the data view update happens as events propagate. </p> <h3 id="640e19ed6f904d71b925672028aeee45"> From abstract class to Dependency Injection <a href="#640e19ed6f904d71b925672028aeee45">#</a> </h3> <p> While the current state of the code is based on an abstract base class, the overall architecture of the system doesn't hinge on inheritance. From <a href="/2018/02/19/abstract-class-isomorphism">Abstract class isomorphism</a> we know that it's possible to refactor an abstract class to Constructor Injection. Let's do that. </p> <p> First add an <code>IViewStorage</code> interface that mirrors the three <code>abstract</code> methods defined by <code>ReadRegistry</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IViewStorage</span> { &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;Rooms&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">UpdateView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryGetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>); }</pre> </p> <p> Then implement it with a Fake Object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeViewStorage</span>&nbsp;:&nbsp;IViewStorage { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IDictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;&nbsp;views; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;Rooms&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FakeViewStorage</span>(<span style="color:blue;">params</span>&nbsp;Room[]&nbsp;<span style="font-weight:bold;color:#1f377f;">rooms</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rooms&nbsp;=&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Dictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">UpdateView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views[date]&nbsp;=&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryGetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;views.TryGetValue(date,&nbsp;<span style="color:blue;">out</span>&nbsp;view); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice the similarity to <code>FakeReadRegistry</code>, which we'll get rid of shortly. </p> <p> Now inject <code>IViewStorage</code> into <code>ReadRegistry</code>, and make <code>ReadRegistry</code> a regular (<code>sealed</code>) class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReadRegistry</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IViewStorage&nbsp;viewStorage; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReadRegistry</span>(IViewStorage&nbsp;<span style="font-weight:bold;color:#1f377f;">viewStorage</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.viewStorage&nbsp;=&nbsp;viewStorage; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;EnumerateDates(arrival,&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(GetView) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate(viewStorage.Rooms.AsEnumerable(),&nbsp;Enumerable.Intersect) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;EnumerateDates(booking.Arrival,&nbsp;booking.Departure)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>&nbsp;=&nbsp;GetView(d); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newView</span>&nbsp;=&nbsp;Reserve(booking,&nbsp;view); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;viewStorage.UpdateView(d,&nbsp;newView); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="color:#74531f;">Reserve</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">existingView</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;existingView &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Name&nbsp;!=&nbsp;booking.RoomName) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;DateOnly&gt;&nbsp;<span style="color:#74531f;">EnumerateDates</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&nbsp;arrival; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(d&nbsp;&lt;&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">yield</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;d; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&nbsp;d.AddDays(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(viewStorage.TryGetView(date,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;viewStorage.Rooms; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can now delete the <code>FakeReadRegistry</code> Test Double, since <code>FakeViewStorage</code> has now taken its place. </p> <p> Finally, we may consider if we can make <code>FakeViewStorage</code> even slimmer. While I usually favour composition over inheritance, I've found that deriving Fake Objects from collection base classes is often an efficient way to get a lot of mileage out of a few lines of code. <code>FakeReadRegistry</code>, however, had to inherit from <code>ReadRegistry</code>, so it couldn't derive from any other class. </p> <p> <code>FakeViewStorage</code> isn't constrained in that way, so it's free to inherit from <code>Dictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeViewStorage</span>&nbsp;:&nbsp;Dictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;,&nbsp;IViewStorage { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;Rooms&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FakeViewStorage</span>(<span style="color:blue;">params</span>&nbsp;Room[]&nbsp;<span style="font-weight:bold;color:#1f377f;">rooms</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rooms&nbsp;=&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">UpdateView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>[date]&nbsp;=&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryGetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;TryGetValue(date,&nbsp;<span style="color:blue;">out</span>&nbsp;view); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This last move isn't strictly necessary, but I found it worth at least mentioning. </p> <p> I hope you'll agree that this is a Fake Object that looks maintainable. </p> <h3 id="cd86ac335816431aa39a6538fd9ce95c"> Conclusion <a href="#cd86ac335816431aa39a6538fd9ce95c">#</a> </h3> <p> Test-driven development is a feedback mechanism. If something is difficult to test, it tells you something about your System Under Test (SUT). If your test code looks bloated, that tells you something too. Perhaps part of the test code really belongs in the production code. </p> <p> In this article, we started with a Fake Object that looked like it contained too much production code. Via a series of refactorings I moved the relevant parts to the production code, leaving me with a more idiomatic and conforming implementation. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. CC golf https://blog.ploeh.dk/2023/11/14/cc-golf 2023-11-14T14:44:00+00:00 Mark Seemann <div id="post"> <p> <em>Noun. Game in which the goal is to minimise cyclomatic complexity.</em> </p> <p> <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic complexity</a> (CC) is a rare code metric since it <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">can be actually useful</a>. In general, it's a good idea to minimise it as much as possible. </p> <p> In short, CC measures looping and branching in code, and this is often where bugs lurk. While it's only a rough measure, I nonetheless find the metric useful as a general guideline. Lower is better. </p> <h3 id="0a1e55fd6ebf422aac1f6441e4e34f99"> Golf <a href="#0a1e55fd6ebf422aac1f6441e4e34f99">#</a> </h3> <p> I'd like to propose the term "CC golf" for the activity of minimising cyclomatic complexity in an area of code. The name derives from <a href="https://en.wikipedia.org/wiki/Code_golf">code golf</a>, in which you have to implement some behaviour (typically an algorithm) in fewest possible characters. </p> <p> Such games can be useful because they enable you to explore different ways to express yourself in code. It's always a good <a href="/2020/01/13/on-doing-katas">kata constraint</a>. The <a href="/2011/05/16/TennisKatawithimmutabletypesandacyclomaticcomplexityof1">first time I tried that was in 2011</a>, and when looking back on that code today, I'm not that impressed. Still, it taught me a valuable lesson about the <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor pattern</a> that I never forgot, and that later enabled me to <a href="/2018/06/25/visitor-as-a-sum-type">connect some important dots</a>. </p> <p> But don't limit CC golf to katas and the like. Try it in your production code too. Most production code I've seen could benefit from some CC golf, and if you <a href="https://stackoverflow.blog/2022/12/19/use-git-tactically/">use Git tactically</a> you can always stash the changes if they're no good. </p> <h3 id="f7e54cb8b9954fbd9ed8022ad09f5d7f"> Idiomatic tension <a href="#f7e54cb8b9954fbd9ed8022ad09f5d7f">#</a> </h3> <p> Alternative expressions with lower cyclomatic complexity may not always be idiomatic. Let's look at a few examples. In my <a href="/2023/11/13/fakes-are-test-doubles-with-contracts">previous article</a>, I listed some test code where some helper methods had a CC of <em>2</em>. Here's one of them: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;DateOnly&gt;&nbsp;<span style="color:#74531f;">EnumerateDates</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&nbsp;arrival; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(d&nbsp;&lt;&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">yield</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;d; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&nbsp;d.AddDays(1); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Can you express this functionality with a CC of <em>1?</em> In <a href="https://www.haskell.org/">Haskell</a> it's essentially built in as <code>(. pred) . enumFromTo</code>, and in <a href="https://fsharp.org/">F#</a> it's also idiomatic, although more verbose: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;enumerateDates&nbsp;(arrival&nbsp;:&nbsp;DateOnly)&nbsp;departure&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;Seq.initInfinite&nbsp;id&nbsp;|&gt;&nbsp;Seq.map&nbsp;arrival.AddDays&nbsp;|&gt;&nbsp;Seq.takeWhile&nbsp;(<span style="color:blue;">fun</span>&nbsp;d&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;d&nbsp;&lt;&nbsp;departure)</pre> </p> <p> Can we do the same in C#? </p> <p> If there's a general API in .NET that corresponds to the F#-specific <code>Seq.initInfinite</code> I haven't found it, but we can do something like this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;DateOnly&gt;&nbsp;<span style="color:#74531f;">EnumerateDates</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">int</span>&nbsp;infinity&nbsp;=&nbsp;<span style="color:blue;">int</span>.MaxValue;&nbsp;<span style="color:green;">//&nbsp;As&nbsp;close&nbsp;as&nbsp;int&nbsp;gets,&nbsp;at&nbsp;least</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Enumerable.Range(0,&nbsp;infinity).Select(arrival.AddDays).TakeWhile(<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;d&nbsp;&lt;&nbsp;departure); }</pre> </p> <p> In C# infinite sequences are generally unusual, but <em>if</em> you were to create one, a combination of <code>while true</code> and <code>yield return</code> would be the most idiomatic. The problem with that, though, is that such a construct has a cyclomatic complexity of <em>2</em>. </p> <p> The above suggestion gets around that problem by pretending that <code>int.MaxValue</code> is infinity. Practically, at least, a 32-bit signed integer can't get larger than that anyway. I haven't tried to let F#'s <a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-seqmodule.html#initInfinite">Seq.initInfinite</a> run out, but by its type it seems <code>int</code>-bound as well, so in practice it, too, probably isn't infinite. (Or, if it is, the index that it supplies will have to overflow and wrap around to a negative value.) </p> <p> Is this alternative C# code better than the first? You be the judge of that. It has a lower cyclomatic complexity, but is less idiomatic. This isn't uncommon. In languages with a procedural background, there's often tension between lower cyclomatic complexity and how 'things are usually done'. </p> <h3 id="d24ea9b2a10f482693071d7dbe1c6604"> Checking for null <a href="#d24ea9b2a10f482693071d7dbe1c6604">#</a> </h3> <p> Is there a way to reduce the cyclomatic complexity of the <code>GetView</code> helper method? </p> <p> <pre><span style="color:blue;">private</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(views.TryGetValue(date,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;rooms; }</pre> </p> <p> This is an example of the built-in API being in the way. In F#, you naturally write the same behaviour with a CC of <em>1:</em> </p> <p> <pre><span style="color:blue;">let</span>&nbsp;getView&nbsp;(date&nbsp;:&nbsp;DateOnly)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;|&gt;&nbsp;Map.tryFind&nbsp;date&nbsp;|&gt;&nbsp;Option.defaultValue&nbsp;rooms&nbsp;|&gt;&nbsp;Set.ofSeq</pre> </p> <p> That <code>TryGet</code> idiom is in the way for further CC reduction, it seems. It <em>is</em> possible to reach a CC of <em>1</em>, though, but it's neither pretty nor idiomatic: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) { &nbsp;&nbsp;&nbsp;&nbsp;views.TryGetValue(date,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;view,&nbsp;rooms&nbsp;}.Where(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}).First()!; }</pre> </p> <p> Perhaps there's a better way, but if so, it escapes me. Here, I use my knowledge that <code>view</code> is going to remain <code>null</code> if <code>TryGetValue</code> doesn't find the dictionary entry. Thus, I can put it in front of an array where I put the fallback value <code>rooms</code> as the second element. Then I filter the array by only keeping the elements that are <em>not</em> <code>null</code> (that's what the <code>x is { }</code> pun means; I usually read it as <em>x is something</em>). Finally, I return the first of these elements. </p> <p> I know that <code>rooms</code> is never <code>null</code>, but apparently the compiler can't tell. Thus, I have to suppress its anxiety with the <code>!</code> operator, telling it that this <em>will</em> result in a non-null value. </p> <p> I would never use such a code construct in a professional C# code base. </p> <h3 id="b1c8693ac29a43a1812e4b9ba9f86e6e"> Side effects <a href="#b1c8693ac29a43a1812e4b9ba9f86e6e">#</a> </h3> <p> The third helper method suggests another kind of problem that you may run into: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;EnumerateDates(booking.Arrival,&nbsp;booking.Departure)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>&nbsp;=&nbsp;GetView(d); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newView</span>&nbsp;=&nbsp;QueryService.Reserve(booking,&nbsp;view); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views[d]&nbsp;=&nbsp;newView; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Here the higher-than-one CC stems from the need to loop through dates in order to produce a side effect for each. Even in F# I do that: </p> <p> <pre><span style="color:blue;">member</span>&nbsp;this.RoomBooked&nbsp;booking&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;d&nbsp;<span style="color:blue;">in</span>&nbsp;enumerateDates&nbsp;booking.Arrival&nbsp;booking.Departure&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newView&nbsp;=&nbsp;getView&nbsp;d&nbsp;|&gt;&nbsp;QueryService.reserve&nbsp;booking&nbsp;|&gt;&nbsp;Seq.toList &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;Map.add&nbsp;d&nbsp;newView&nbsp;views</pre> </p> <p> This also has a cyclomatic complexity of <em>2</em>. You could do something like this: </p> <p> <pre><span style="color:blue;">member</span>&nbsp;this.RoomBooked&nbsp;booking&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;enumerateDates&nbsp;booking.Arrival&nbsp;booking.Departure &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.iter&nbsp;(<span style="color:blue;">fun</span>&nbsp;d&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newView&nbsp;=&nbsp;getView&nbsp;d&nbsp;|&gt;&nbsp;QueryService.reserve&nbsp;booking&nbsp;|&gt;&nbsp;Seq.toList&nbsp;<span style="color:blue;">in</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;Map.add&nbsp;d&nbsp;newView&nbsp;views)</pre> </p> <p> but while that nominally has a CC of <em>1</em>, it has the same level of indentation as the previous attempt. This seems to indicate, at least, that it doesn't <em>really</em> address any complexity issue. </p> <p> You could also try something like this: </p> <p> <pre><span style="color:blue;">member</span>&nbsp;this.RoomBooked&nbsp;booking&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;enumerateDates&nbsp;booking.Arrival&nbsp;booking.Departure &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;d&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;d,&nbsp;getView&nbsp;d&nbsp;|&gt;&nbsp;QueryService.reserve&nbsp;booking&nbsp;|&gt;&nbsp;Seq.toList) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.iter&nbsp;(<span style="color:blue;">fun</span>&nbsp;(d,&nbsp;newView)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;views&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;Map.add&nbsp;d&nbsp;newView&nbsp;views)</pre> </p> <p> which, again, may be nominally better, but forced me to wrap the <code>map</code> output in a tuple so that both <code>d</code> and <code>newView</code> is available to <code>Seq.iter</code>. I tend to regard that as a code smell. </p> <p> This latter version is, however, fairly easily translated to C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) { &nbsp;&nbsp;&nbsp;&nbsp;EnumerateDates(booking.Arrival,&nbsp;booking.Departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;(d,&nbsp;view:&nbsp;QueryService.Reserve(booking,&nbsp;GetView(d)))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ForEach(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;views[x.d]&nbsp;=&nbsp;x.view); }</pre> </p> <p> The standard .NET API doesn't have something equivalent to <code>Seq.iter</code> (although you could trivially write such an action), but <a href="https://stackoverflow.com/a/1509450/126014">you can convert any sequence to a <code>List&lt;T&gt;</code> and use its <code>ForEach</code> method</a>. </p> <p> In practice, though, I tend to <a href="https://ericlippert.com/2009/05/18/foreach-vs-foreach/">agree with Eric Lippert</a>. There's already an idiomatic way to iterate over each item in a collection, and <a href="https://peps.python.org/pep-0020/">being explicit</a> is generally helpful to the reader. </p> <h3 id="5f060ce8557043dfb0f374ef254cc922"> Church encoding <a href="#5f060ce8557043dfb0f374ef254cc922">#</a> </h3> <p> There's a general solution to most of CC golf: Whenever you need to make a decision and branch between two or more pathways, you can model that with a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>. In C# you can mechanically model that with <a href="/2018/05/22/church-encoding">Church encoding</a> or <a href="/2018/06/25/visitor-as-a-sum-type">the Visitor pattern</a>. If you haven't tried that, I recommend it for the exercise, but once you've done it enough times, you realise that it requires little creativity. </p> <p> As an example, in 2021 I <a href="/2021/08/03/the-tennis-kata-revisited">revisited the Tennis kata</a> with the explicit purpose of translating <a href="/2016/02/10/types-properties-software">my usual F# approach to the exercise</a> to C# using Church encoding and the Visitor pattern. </p> <p> Once you've got a sense for how Church encoding enables you to simulate pattern matching in C#, there are few surprises. You may also rightfully question what is gained from such an exercise: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IScore&nbsp;<span style="font-weight:bold;color:#74531f;">VisitPoints</span>(IPoint&nbsp;<span style="font-weight:bold;color:#1f377f;">playerOnePoint</span>,&nbsp;IPoint&nbsp;<span style="font-weight:bold;color:#1f377f;">playerTwoPoint</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;playerWhoWinsBall.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerOne:&nbsp;playerOnePoint.Match&lt;IScore&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;love:&nbsp;<span style="color:blue;">new</span>&nbsp;Points(<span style="color:blue;">new</span>&nbsp;Fifteen(),&nbsp;playerTwoPoint), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fifteen:&nbsp;<span style="color:blue;">new</span>&nbsp;Points(<span style="color:blue;">new</span>&nbsp;Thirty(),&nbsp;playerTwoPoint), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thirty:&nbsp;<span style="color:blue;">new</span>&nbsp;Forty(playerWhoWinsBall,&nbsp;playerTwoPoint)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerTwo:&nbsp;playerTwoPoint.Match&lt;IScore&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;love:&nbsp;<span style="color:blue;">new</span>&nbsp;Points(playerOnePoint,&nbsp;<span style="color:blue;">new</span>&nbsp;Fifteen()), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fifteen:&nbsp;<span style="color:blue;">new</span>&nbsp;Points(playerOnePoint,&nbsp;<span style="color:blue;">new</span>&nbsp;Thirty()), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thirty:&nbsp;<span style="color:blue;">new</span>&nbsp;Forty(playerWhoWinsBall,&nbsp;playerOnePoint))); }</pre> </p> <p> Believe it or not, but that method has a CC of <em>1</em> despite the double indentation strongly suggesting that there's some branching going on. To a degree, this also highlights the limitations of the cyclomatic complexity metric. Conversely, <a href="/2021/03/29/table-driven-tennis-scoring">stupidly simple code may have a high CC rating</a>. </p> <p> Most of the examples in this article border on the pathological, and I don't recommend that you write code <em>like</em> that. I recommend that you do the exercise. In less pathological scenarios, there are real benefits to be reaped. </p> <h3 id="931c0946572041449fcce50da5f5219b"> Idioms <a href="#931c0946572041449fcce50da5f5219b">#</a> </h3> <p> In 2015 I published an article titled <a href="/2015/08/03/idiomatic-or-idiosyncratic">Idiomatic or idiosyncratic?</a> In it, I tried to explore the idea that the notion of idiomatic code can sometimes hold you back. I revisited that idea in 2021 in an article called <a href="/2021/05/17/against-consistency">Against consistency</a>. The point in both cases is that just because something looks unfamiliar, it doesn't mean that it's bad. </p> <p> Coding idioms somehow arose. If you believe that there's a portion of natural selection involved in the development of coding idioms, you may assume by default that idioms represent good ways of doing things. </p> <p> To a degree I believe this to be true. Many idioms represent the best way of doing things at the time they settled into the shape that we now know them. Languages and contexts change, however. Just look at <a href="/2019/07/15/tester-doer-isomorphisms">the many approaches to data lookups</a> there have been over the years. For many years now, C# has settled into the so-called <em>TryParse</em> idiom to solve that problem. In my opinion this represents a local maximum. </p> <p> Languages that provide <a href="/2018/06/04/church-encoded-maybe">Maybe</a> (AKA <code>option</code>) and <a href="/2018/06/11/church-encoded-either">Either</a> (AKA <code>Result</code>) types offer a superior alternative. These types naturally compose into <em>CC 1</em> pipelines, whereas <em>TryParse</em> requires you to stop what you're doing in order to check a return value. How very <a href="https://en.wikipedia.org/wiki/C_(programming_language)">C</a>-like. </p> <p> All that said, I still think you should write idiomatic code by default, but don't be a slave by what's considered idiomatic, just as you shouldn't be a slave to consistency. If there's a better way of doing things, choose the better way. </p> <h3 id="8bf3cb23fa5a4f3aa532a40b01dbefb1"> Conclusion <a href="#8bf3cb23fa5a4f3aa532a40b01dbefb1">#</a> </h3> <p> While cyclomatic complexity is a rough measure, it's one of the few useful programming metrics I know of. It should be as low as possible. </p> <p> Most professional code I encounter implements decisions almost exclusively with language primitives: <code>if</code>, <code>for</code>, <code>switch</code>, <code>while</code>, etc. Once, an organisation hired me to give a one-day <em>anti-if</em> workshop. There are other ways to make decisions in code. Most of those alternatives reduce cyclomatic complexity. </p> <p> That's not really a goal by itself, but reducing cyclomatic complexity tends to produce the beneficial side effect of structuring the code in a more sustainable way. It becomes easier to understand and change. </p> <p> As the cliché goes: <em>Choose the right tool for the job.</em> You can't, however, do that if you have nothing to choose from. If you only know of one way to do a thing, you have no choice. </p> <p> Play a little CC golf with your code from time to time. It may improve the code, or it may not. If it didn't, just <a href="https://git-scm.com/docs/git-stash">stash</a> those changes. Either way, you've probably <em>learned</em> something. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fakes are Test Doubles with contracts https://blog.ploeh.dk/2023/11/13/fakes-are-test-doubles-with-contracts 2023-11-13T17:11:00+00:00 Mark Seemann <div id="post"> <p> <em>Contracts of Fake Objects can be described by properties.</em> </p> <p> The first time I tried my hand with the <a href="https://codingdojo.org/kata/CQRS_Booking/">CQRS Booking kata</a>, I abandoned it after 45 minutes because I found that I had little to learn from it. After all, I've already done umpteen variations of (restaurant) booking code examples, in several programming languages. The code example that accompanies my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> is only the largest and most complete of those. </p> <p> I also wrote <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2011/april/azure-development-cqrs-on-microsoft-azure">an MSDN Magazine article</a> in 2011 about <a href="https://en.wikipedia.org/wiki/Command_Query_Responsibility_Segregation">CQRS</a>, so I think I have that angle covered as well. </p> <p> Still, while at first glance the kata seemed to have little to offer me, I've found myself coming back to it a few times. It does enable me to focus on something else than the 'production code'. In fact, it turns out that even if (or perhaps particularly <em>when</em>) you use test-driven development (TDD), there's precious little production code. Let's get that out of the way first. </p> <h3 id="b1192f76c2ef4f31b6bddcbc944664c7"> Production code <a href="#b1192f76c2ef4f31b6bddcbc944664c7">#</a> </h3> <p> The few times I've now done the kata, there's almost no 'production code'. The implied <code>CommandService</code> has two lines of effective code: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CommandService</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IWriteRegistry&nbsp;writeRegistry; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IReadRegistry&nbsp;readRegistry; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CommandService</span>(IWriteRegistry&nbsp;<span style="font-weight:bold;color:#1f377f;">writeRegistry</span>,&nbsp;IReadRegistry&nbsp;<span style="font-weight:bold;color:#1f377f;">readRegistry</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.writeRegistry&nbsp;=&nbsp;writeRegistry; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.readRegistry&nbsp;=&nbsp;readRegistry; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BookARoom</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeRegistry.Save(booking); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readRegistry.RoomBooked(booking); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>QueryService</code> class isn't much more exciting: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">QueryService</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IReadRegistry&nbsp;readRegistry; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">QueryService</span>(IReadRegistry&nbsp;<span style="font-weight:bold;color:#1f377f;">readRegistry</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.readRegistry&nbsp;=&nbsp;readRegistry; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="color:#74531f;">Reserve</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">existingView</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;existingView.Where(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Name&nbsp;!=&nbsp;booking.RoomName).ToList(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;readRegistry.GetFreeRooms(arrival,&nbsp;departure); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The kata only suggests the <code>GetFreeRooms</code> method, which is only a single line. The only reason the <code>Reserve</code> function also exists is to pull a bit of testable logic back from the below <a href="http://xunitpatterns.com/Fake%20Object.html">Fake object</a>. I'll return to that shortly. </p> <p> I've also done the exercise in <a href="https://fsharp.org/">F#</a>, essentially porting the C# implementation, which only highlights how simple it all is: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;CommandService&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;bookARoom&nbsp;(writeRegistry&nbsp;:&nbsp;IWriteRegistry)&nbsp;(readRegistry&nbsp;:&nbsp;IReadRegistry)&nbsp;booking&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeRegistry.Save&nbsp;booking &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readRegistry.RoomBooked&nbsp;booking <span style="color:blue;">module</span>&nbsp;QueryService&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reserve&nbsp;booking&nbsp;existingView&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;existingView&nbsp;|&gt;&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Name&nbsp;&lt;&gt;&nbsp;booking.RoomName) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;getFreeRooms&nbsp;(readRegistry&nbsp;:&nbsp;IReadRegistry)&nbsp;arrival&nbsp;departure&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readRegistry.GetFreeRooms&nbsp;arrival&nbsp;departure</pre> </p> <p> That's <em>both</em> the Command side and the Query side! </p> <p> This represents my honest interpretation of the kata. Really, there's nothing to it. </p> <p> The reason I still find the exercise interesting is that it explores other aspects of TDD than most katas. The most common katas require you to write a little algorithm: <a href="https://codingdojo.org/kata/Bowling/">Bowling</a>, <a href="https://codingdojo.org/kata/WordWrap/">Word wrap</a>, <a href="https://codingdojo.org/kata/RomanNumerals/">Roman Numerals</a>, <a href="https://codingdojo.org/kata/Diamond/">Diamond</a>, <a href="https://codingdojo.org/kata/Tennis/">Tennis</a>, etc. </p> <p> The CQRS Booking kata suggests no interesting algorithm, but rather teaches some important lessons about software architecture, separation of concerns, and, if you approach it with TDD, real-world test automation. In contrast to all those algorithmic exercises, this one strongly suggests the use of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>. </p> <h3 id="6d8d7717cfef428e91418b319c4fe971"> Fakes <a href="#6d8d7717cfef428e91418b319c4fe971">#</a> </h3> <p> You could attempt the kata with a dynamic 'mocking' library such as <a href="https://devlooped.com/moq">Moq</a> or <a href="https://site.mockito.org/">Mockito</a>, but I haven't tried. Since <a href="/2022/10/17/stubs-and-mocks-break-encapsulation">Stubs and Mocks break encapsulation</a> I favour Fake Objects instead. </p> <p> Creating a Fake write registry is trivial: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeWriteRegistry</span>&nbsp;:&nbsp;Collection&lt;Booking&gt;,&nbsp;IWriteRegistry { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Save</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(booking); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Its counterpart, the Fake read registry, turns out to be much more involved: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeReadRegistry</span>&nbsp;:&nbsp;IReadRegistry { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IDictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;&nbsp;views; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FakeReadRegistry</span>(<span style="color:blue;">params</span>&nbsp;Room[]&nbsp;<span style="font-weight:bold;color:#1f377f;">rooms</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.rooms&nbsp;=&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Dictionary&lt;DateOnly,&nbsp;IReadOnlyCollection&lt;Room&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;EnumerateDates(arrival,&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(GetView) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate(rooms.AsEnumerable(),&nbsp;Enumerable.Intersect) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;EnumerateDates(booking.Arrival,&nbsp;booking.Departure)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>&nbsp;=&nbsp;GetView(d); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newView</span>&nbsp;=&nbsp;QueryService.Reserve(booking,&nbsp;view); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views[d]&nbsp;=&nbsp;newView; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;DateOnly&gt;&nbsp;<span style="color:#74531f;">EnumerateDates</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&nbsp;arrival; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(d&nbsp;&lt;&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">yield</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;d; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&nbsp;d.AddDays(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetView</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(views.TryGetValue(date,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="font-weight:bold;color:#1f377f;">view</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;rooms; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I think I can predict the most common reaction: <em>That's much more code than the System Under Test!</em> Indeed. For this particular exercise, this may indicate that a 'dynamic mock' library may have been a better choice. I do, however, also think that it's an artefact of the kata description's lack of requirements. </p> <p> As is evident from the restaurant sample code that accompanies <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, once you add <a href="/2020/01/27/the-maitre-d-kata">realistic business rules</a> the production code grows, and the ratio of test code to production code becomes better balanced. </p> <p> The size of the <code>FakeReadRegistry</code> class also stems from the way the .NET base class library API is designed. The <code>GetView</code> helper method demonstrates that it requires four lines of code to look up an entry in a dictionary but return a default value if the entry isn't found. That's a one-liner in F#: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;getView&nbsp;(date&nbsp;:&nbsp;DateOnly)&nbsp;=&nbsp;views&nbsp;|&gt;&nbsp;Map.tryFind&nbsp;date&nbsp;|&gt;&nbsp;Option.defaultValue&nbsp;rooms&nbsp;|&gt;&nbsp;Set.ofSeq</pre> </p> <p> I'll show the entire F# Fake later, but you could also play some <a href="/2023/11/14/cc-golf">CC golf</a> with the C# code. That's a bit besides the point, though. </p> <h3 id="1f5b34534edd4b72947c8d6b4c8921bf"> Command service design <a href="#1f5b34534edd4b72947c8d6b4c8921bf">#</a> </h3> <p> Why does <code>FakeReadRegistry</code> look like it does? It's a combination of the kata description and my prior experience with CQRS. When adopting an asynchronous message-based architecture, I would usually not implement the write side exactly like that. Notice how the <code>CommandService</code> class' <code>BookARoom</code> method seems to repeat itself: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BookARoom</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) { &nbsp;&nbsp;&nbsp;&nbsp;writeRegistry.Save(booking); &nbsp;&nbsp;&nbsp;&nbsp;readRegistry.RoomBooked(booking); }</pre> </p> <p> While semantically it seems to be making two different statements, structurally they're identical. If you rename the methods, you could wrap both method calls in a single <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a>. In a more typical CQRS architecture, you'd post a Command on bus: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BookARoom</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>) { &nbsp;&nbsp;&nbsp;&nbsp;bus.BookRoom(booking); }</pre> </p> <p> This makes that particular <code>BookARoom</code> method, and perhaps the entire <code>CommandService</code> class, look redundant. Why do we need it? </p> <p> As presented here, we don't, but in a real application, the Command service would likely perform some pre- and post-processing. For example, if this was a web application, the Command service might instead be a Controller concerned with validating and translating HTTP- or Web-based input to a Domain Object before posting to the bus. </p> <p> A realistic code base would also be asynchronous, which, on .NET, would imply the use of the <code>async</code> and <code>await</code> keywords, etc. </p> <h3 id="4dd49e22fe4c479381285eb1b886457e"> Read registry design <a href="#4dd49e22fe4c479381285eb1b886457e">#</a> </h3> <p> A central point of CQRS is that you can optimise the read side for the specific tasks that it needs to perform. Instead of performing a dynamic query every time a client requests a view, you can update and persist a view. Imagine having a JSON or HTML file that the system can serve upon request. </p> <p> Part of handling a Command or Event is that the system background processes update persistent views once per event. </p> <p> For the particular hotel booking system, I imagine that the read registry has a set of files, blobs, documents, or denormalised database rows. When it receives notification of a booking, it'll need to remove that room from the dates of the booking. </p> <p> While a booking may stretch over several days, I found it simplest to think of the storage system as subdivided into single dates, instead of ranges. Indeed, the <code>GetFreeRooms</code> method is a ranged query, so if you really wanted to denormalise the views, you could create a persistent view per range. This would, however, require that you precalculate and persist a view for October 2 to October 4, and another one for October 2 to October 5, and so on. The combinatorial explosion suggests that this isn't a good idea, so instead I imagine keeping a persistent view per date, and then perform a bit of on-the-fly calculation per query. </p> <p> That's what <code>FakeReadRegistry</code> does. It also falls back to a default collection of <code>rooms</code> for all the dates that are yet untouched by a booking. This is, again, because I imagine that I might implement a real system like that. </p> <p> You may still protest that the <code>FakeReadRegistry</code> duplicates production code. True, perhaps, but if this really is a concern, you could <a href="/2023/11/20/trimming-a-fake-object">refactor it to the Template Method pattern</a>. </p> <p> Still, it's not really that complicated; it only looks that way because C# and the Dictionary API is too heavy on <a href="/2019/12/16/zone-of-ceremony">ceremony</a>. The Fake looks much simpler in F#: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;FakeReadRegistry&nbsp;(rooms&nbsp;:&nbsp;IReadOnlyCollection&lt;Room&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;views&nbsp;=&nbsp;Map.empty &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;enumerateDates&nbsp;(arrival&nbsp;:&nbsp;DateOnly)&nbsp;departure&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.initInfinite&nbsp;id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.map&nbsp;arrival.AddDays &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.takeWhile&nbsp;(<span style="color:blue;">fun</span>&nbsp;d&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;d&nbsp;&lt;&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;getView&nbsp;(date&nbsp;:&nbsp;DateOnly)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;|&gt;&nbsp;Map.tryFind&nbsp;date&nbsp;|&gt;&nbsp;Option.defaultValue&nbsp;rooms&nbsp;|&gt;&nbsp;Set.ofSeq &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">interface</span>&nbsp;IReadRegistry&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.GetFreeRooms&nbsp;arrival&nbsp;departure&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enumerateDates&nbsp;arrival&nbsp;departure &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.map&nbsp;getView &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.fold&nbsp;Set.intersect&nbsp;(Set.ofSeq&nbsp;rooms) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Set.toList&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.RoomBooked&nbsp;booking&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;d&nbsp;<span style="color:blue;">in</span>&nbsp;enumerateDates&nbsp;booking.Arrival&nbsp;booking.Departure&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newView&nbsp;=&nbsp;getView&nbsp;d&nbsp;|&gt;&nbsp;QueryService.reserve&nbsp;booking&nbsp;|&gt;&nbsp;Seq.toList &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;views&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;Map.add&nbsp;d&nbsp;newView&nbsp;views </pre> </p> <p> This isn't just more dense than the corresponding C# code, as F# tends to be, it also has a lower <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a>. Both the <code>EnumerateDates</code> and <code>GetView</code> C# methods have a cyclomatic complexity of <em>2</em>, while their F# counterparts rate only <em>1</em>. </p> <p> For production code, cyclomatic complexity of <em>2</em> is fine if the code is covered by automatic tests. In test code, however, we should be wary of any branching or looping, since there are (typically) no tests of the test code. </p> <p> While I <em>am</em> going to show some tests of that code in what follows, I do that for a different reason. </p> <h3 id="da4d51ff041c4cf6b4007d53a67f2d76"> Contract <a href="#da4d51ff041c4cf6b4007d53a67f2d76">#</a> </h3> <p> When explaining Fake Objects to people, I've begun to use a particular phrase: </p> <blockquote> <p> A Fake Object is a polymorphic implementation of a dependency that fulfils the contract, but lacks some of the <em>ilities</em>. </p> </blockquote> <p> It's funny how you can arrive at something that strikes you as profound, only to discover that it was part of the definition all along: </p> <blockquote> <p> "We acquire or build a very lightweight implementation of the same functionality as provided by a component on which the SUT [System Under Test] depends and instruct the SUT to use it instead of the real DOC [Depended-On Component]. This implementation need not have any of the "-ilities" that the real DOC needs to have" </p> <footer><cite>Gerard Meszaros, <a href="/ref/xunit-patterns">xUnit Test Patterns</a></cite></footer> </blockquote> <p> A common example is a Fake Repository object that pretends to be a database, often by leveraging a built-in collection API. The above <code>FakeWriteRegistry</code> is as simple an example as you could have. A slightly more compelling example is <a href="/2023/08/14/replacing-mock-and-stub-with-a-fake">the FakeUserRepository shown in another article</a>. Such an 'in-memory database' fulfils the implied contract, because if you 'save' something in the 'database' you can later retrieve it again with a query. As long as the object remains in memory. </p> <p> The <em>ilities</em> that such a Fake database lacks are </p> <ul> <li>data persistence</li> <li>thread safety</li> <li>transaction support</li> </ul> <p> and perhaps others. Such qualities are clearly required in a real production environment, but are in the way in an automated testing context. The implied contract, however, is satisfied: What you save you can later retrieve. </p> <p> Now consider the <code>IReadRegistry</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReadRegistry</span> { &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;Room&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>(DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;DateOnly&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>(Booking&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>); }</pre> </p> <p> Which contract does it imply, given what you know about the <em>CQRS Booking</em> kata? </p> <p> I would suggest the following: </p> <ul> <li><em>Precondition:</em> <code>arrival</code> should be less than (or equal?) to <code>departure</code>.</li> <li><em>Postcondition:</em> <code>GetFreeRooms</code> should always return a result. Null isn't a valid return value.</li> <li><em>Invariant:</em> After calling <code>RoomBooked</code>, <code>GetFreeRooms</code> should exclude that room when queried on overlapping dates.</li> </ul> <p> There may be other parts of the contract than this, but I find the third one most interesting. This is exactly what you would expect from a real system: If you reserve a room, you'd be surprised to see <code>GetFreeRooms</code> indicating that this room is free if queried about dates that overlap the reservation. </p> <p> This is the sort of implied interaction that <a href="/2022/10/17/stubs-and-mocks-break-encapsulation">Stubs and Mocks break</a>, but that <code>FakeReadRegistry</code> guarantees. </p> <h3 id="6ab8206598ab4bb990d93e5472d36054"> Properties <a href="#6ab8206598ab4bb990d93e5472d36054">#</a> </h3> <p> There's a close relationship between contracts and properties. Once you can list preconditions, invariants, and postconditions for an object, there's a good chance that you can write code that exercises those qualities. Indeed, why not use property-based testing to do so? </p> <p> I don't wish to imply that you should (normally) write tests of your test code. The following rather serves as a concretisation of the notion that a Fake Object is a Test Double that implements the 'proper' behaviour. In the following, I'll subject the <code>FakeReadRegistry</code> class to that exercise. To do that, I'll use <a href="https://github.com/AnthonyLloyd/CsCheck">CsCheck</a> 2.14.1 with <a href="https://xunit.net/">xUnit.net</a> 2.5.3. </p> <p> Before tackling the above invariant, there's a simpler invariant specific to the <code>FakeReadRegistry</code> class. A <code>FakeReadRegistry</code> object takes a collection of <code>rooms</code> via its constructor, so for this particular implementation, we may wish to establish the reasonable invariant that <code>GetFreeRooms</code> doesn't 'invent' rooms on its own: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Gen&lt;Room&gt;&nbsp;GenRoom&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;name&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.String &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Room(name); [Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetFreeRooms</span>() { &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;rooms&nbsp;<span style="color:blue;">in</span>&nbsp;GenRoom.ArrayUnique &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;arrival&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Date.Select(DateOnly.FromDateTime) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Int[1,&nbsp;1_000] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;departure&nbsp;=&nbsp;arrival.AddDays(i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(rooms,&nbsp;arrival,&nbsp;departure)) &nbsp;&nbsp;&nbsp;&nbsp;.Sample((<span style="font-weight:bold;color:#1f377f;">rooms</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">arrival</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">departure</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeReadRegistry(rooms); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.GetFreeRooms(arrival,&nbsp;departure); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Subset(<span style="color:blue;">new</span>&nbsp;HashSet&lt;Room&gt;(rooms),&nbsp;<span style="color:blue;">new</span>&nbsp;HashSet&lt;Room&gt;(actual)); &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> This property asserts that the <code>actual</code> value returned from <code>GetFreeRooms</code> is a subset of the <code>rooms</code> used to initialise the <code>sut</code>. Recall that the subset relation is <a href="https://en.wikipedia.org/wiki/Reflexive_relation">reflexive</a>; i.e. a set is a subset of itself. </p> <p> The same property written in F# with <a href="https://hedgehog.qa/">Hedgehog</a> 0.13.0 and <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> 6.1.0 may look like this: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Gen&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;room&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.alphaNum &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Gen.array&nbsp;(Range.linear&nbsp;1&nbsp;10) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Gen.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;chars&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;Name&nbsp;=&nbsp;String&nbsp;chars&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;dateOnly&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;min&nbsp;=&nbsp;DateOnly(2000,&nbsp;1,&nbsp;1).DayNumber &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;max&nbsp;=&nbsp;DateOnly(2100,&nbsp;1,&nbsp;1).DayNumber &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Range.linear&nbsp;min&nbsp;max&nbsp;|&gt;&nbsp;Gen.int32&nbsp;|&gt;&nbsp;Gen.map&nbsp;DateOnly.FromDayNumber [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;GetFreeRooms&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;rooms&nbsp;=&nbsp;Gen.room&nbsp;|&gt;&nbsp;Gen.list&nbsp;(Range.linear&nbsp;0&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;arrival&nbsp;=&nbsp;Gen.dateOnly &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;i&nbsp;=&nbsp;Gen.int32&nbsp;(Range.linear&nbsp;1&nbsp;1_000) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;departure&nbsp;=&nbsp;arrival.AddDays&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut&nbsp;=&nbsp;FakeReadRegistry&nbsp;rooms&nbsp;:&gt;&nbsp;IReadRegistry &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut.GetFreeRooms&nbsp;arrival&nbsp;departure &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;Set.isSubset&nbsp;(Set.ofSeq&nbsp;rooms)&nbsp;(Set.ofSeq&nbsp;actual)&nbsp;@&gt;&nbsp;}</pre> </p> <p> Simpler syntax, same idea. </p> <p> Likewise, we can express the contract that describes the relationship between <code>RoomBooked</code> and <code>GetFreeRooms</code> like this: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RoomBooked</span>() { &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;rooms&nbsp;<span style="color:blue;">in</span>&nbsp;GenRoom.ArrayUnique.Nonempty &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;arrival&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Date.Select(DateOnly.FromDateTime) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Int[1,&nbsp;1_000] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;departure&nbsp;=&nbsp;arrival.AddDays(i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;room&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.OneOfConst(rooms) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;id&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Guid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;booking&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Booking(id,&nbsp;room.Name,&nbsp;arrival,&nbsp;departure) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(rooms,&nbsp;booking)) &nbsp;&nbsp;&nbsp;&nbsp;.Sample((<span style="font-weight:bold;color:#1f377f;">rooms</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">booking</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeReadRegistry(rooms); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sut.RoomBooked(booking); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.GetFreeRooms(booking.Arrival,&nbsp;booking.Departure); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.DoesNotContain(booking.RoomName,&nbsp;actual.Select(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Name)); &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> or, in F#: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;RoomBooked&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;rooms&nbsp;=&nbsp;Gen.room&nbsp;|&gt;&nbsp;Gen.list&nbsp;(Range.linear&nbsp;1&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;arrival&nbsp;=&nbsp;Gen.dateOnly &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;i&nbsp;=&nbsp;Gen.int32&nbsp;(Range.linear&nbsp;1&nbsp;1_000) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;departure&nbsp;=&nbsp;arrival.AddDays&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;room&nbsp;=&nbsp;Gen.item&nbsp;rooms &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;id&nbsp;=&nbsp;Gen.guid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;booking&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ClientId&nbsp;=&nbsp;id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RoomName&nbsp;=&nbsp;room.Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arrival&nbsp;=&nbsp;arrival &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Departure&nbsp;=&nbsp;departure&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut&nbsp;=&nbsp;FakeReadRegistry&nbsp;rooms&nbsp;:&gt;&nbsp;IReadRegistry &nbsp;&nbsp;&nbsp;&nbsp;sut.RoomBooked&nbsp;booking &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut.GetFreeRooms&nbsp;arrival&nbsp;departure &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;(Seq.contains&nbsp;room&nbsp;actual)&nbsp;@&gt;&nbsp;}</pre> </p> <p> In both cases, the property books a room and then proceeds to query <code>GetFreeRooms</code> to see which rooms are free. Since the query is exactly in the range from <code>booking.Arrival</code> to <code>booking.Departure</code>, we expect <em>not</em> to see the name of the booked room among the free rooms. </p> <p> (As I'm writing this, I think that there may be a subtle bug in the F# property. Can you spot it?) </p> <h3 id="fa249347697b49699b7ea62336746651"> Conclusion <a href="#fa249347697b49699b7ea62336746651">#</a> </h3> <p> A Fake Object isn't like other Test Doubles. While <a href="/2022/10/17/stubs-and-mocks-break-encapsulation">Stubs and Mocks break encapsulation</a>, a Fake Object not only stays encapsulated, but it also fulfils the contract implied by a polymorphic API (interface or base class). </p> <p> Or, put another way: When is a Fake Object the right Test Double? When you can describe the contract of the dependency. </p> <p> But if you <em>can't</em> describe the contract of a dependency, you should seriously consider if the design is right. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A C# port of validation with partial round trip https://blog.ploeh.dk/2023/10/30/a-c-port-of-validation-with-partial-round-trip 2023-10-30T11:52:00+00:00 Mark Seemann <div id="post"> <p> <em>A raw port of the previous F# demo code.</em> </p> <p> This article is part of <a href="/2020/12/14/validation-a-solved-problem">a short article series</a> on <a href="/2018/11/05/applicative-validation">applicative validation</a> with a twist. The twist is that validation, when it fails, should return not only a list of error messages; it should also retain that part of the input that <em>was</em> valid. </p> <p> In the <a href="/2020/12/28/an-f-demo-of-validation-with-partial-data-round-trip">previous article</a> I showed <a href="https://fsharp.org/">F#</a> demo code, and since <a href="https://forums.fsharp.org/t/thoughts-on-input-validation-pattern-from-a-noob/1541">the original forum question</a> that prompted the article series was about F# code, for a long time, I left it there. </p> <p> Recently, however, I've found myself writing about validation in a broader context: </p> <ul> <li><a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">An applicative reservation validation example in C#</a></li> <li><a href="/2022/08/15/aspnet-validation-revisited">ASP.NET validation revisited</a></li> <li><a href="/2022/08/22/can-types-replace-validation">Can types replace validation?</a></li> <li><a href="/2023/06/26/validation-and-business-rules">Validation and business rules</a></li> <li><a href="/2023/07/03/validating-or-verifying-emails">Validating or verifying emails</a></li> </ul> <p> Perhaps I should consider adding a <em>validation</em> tag to the blog... </p> <p> In that light I thought that it might be illustrative to continue <a href="/2020/12/14/validation-a-solved-problem">this article series</a> with a port to C#. </p> <p> Here, I use techniques already described on this site to perform the translation. Follow the links for details. </p> <p> The translation given here is direct so produces some fairly non-idiomatic C# code. </p> <h3 id="5cee653b6148484fb782d92fea2ca415"> Building blocks <a href="#5cee653b6148484fb782d92fea2ca415">#</a> </h3> <p> The original problem is succinctly stated, and I follow it as closely as possible. This includes potential errors that may be present in the original post. </p> <p> The task is to translate some input to a Domain Model with <a href="/2022/10/24/encapsulation-in-functional-programming">good encapsulation</a>. The input type looks like this, translated to a <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/record">C# record</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">record</span>&nbsp;<span style="color:#2b91af;">Input</span>(<span style="color:blue;">string</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">Name</span>,&nbsp;DateTime?&nbsp;<span style="font-weight:bold;color:#1f377f;">DoB</span>,&nbsp;<span style="color:blue;">string</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">Address</span>)</pre> </p> <p> Notice that every input may be null. This indicates poor encapsulation, but is symptomatic of most input. <a href="/2023/10/16/at-the-boundaries-static-types-are-illusory">At the boundaries, static types are illusory</a>. Perhaps it would have been more idiomatic to model such input as a <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a>, but it makes little difference to what comes next. </p> <p> I consider <a href="/2020/12/14/validation-a-solved-problem">validation a solved problem</a>, because it's possible to model the process as an <a href="/2018/10/01/applicative-functors">applicative functor</a>. Really, <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">validation is a parsing problem</a>. </p> <p> Since my main intent with this article is to demonstrate a technique, I will allow myself a few shortcuts. Like I did <a href="/2023/08/28/a-first-crack-at-the-args-kata">when I first encountered the Args kata</a>, I start by copying the <code>Validated</code> code from <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">An applicative reservation validation example in C#</a>; you can go there if you're interested in it. I'm not going to repeat it here. </p> <p> The target type looks similar to the above <code>Input</code> record, but doesn't allow null values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">record</span>&nbsp;<span style="color:#2b91af;">ValidInput</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">Name</span>,&nbsp;DateTime&nbsp;<span style="font-weight:bold;color:#1f377f;">DoB</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">Address</span>);</pre> </p> <p> This could also have been a 'proper' class. The following code doesn't depend on that. </p> <h3 id="7af5ab9c8fca4dc193fc5854c2806ff4"> Validating names <a href="#7af5ab9c8fca4dc193fc5854c2806ff4">#</a> </h3> <p> Since I'm now working in an ostensibly object-oriented language, I can make the various validation functions methods on the <code>Input</code> record. Since I'm treating validation as a parsing problem, I'm going to name those methods with the <code>TryParse</code> prefix: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;Validated&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;<span style="color:blue;">string</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">TryParseName</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(Name&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;<span style="color:blue;">string</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;name&nbsp;is&nbsp;required&quot;</span>&nbsp;})); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(Name.Length&nbsp;&lt;=&nbsp;3) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;<span style="color:blue;">string</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;i&nbsp;<span style="color:blue;">with</span>&nbsp;{&nbsp;Name&nbsp;=&nbsp;<span style="color:blue;">null</span>&nbsp;},&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;no&nbsp;bob&nbsp;and&nbsp;toms&nbsp;allowed&quot;</span>&nbsp;})); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;<span style="color:blue;">string</span>&gt;(Name); }</pre> </p> <p> As the two previous articles have explained, the result of trying to parse input is a type isomorphic to <a href="/2019/01/14/an-either-functor">Either</a>, but here called <code><span style="color:#2b91af;">Validated</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;</code>. (The reason for this distinction is that we <em>don't</em> want the <a href="/2022/05/09/an-either-monad">monadic behaviour of Either</a>, because monads short-circuit.) </p> <p> When parsing succeeds, the <code>TryParseName</code> method returns the <code>Name</code> wrapped in a <code>Success</code> case. </p> <p> Parsing the name may fail in two different ways. If the name is missing, the method returns the input and the error message <em>"name is required"</em>. If the name is present, but too short, <code>TryParseName</code> returns another error message, and also resets <code>Name</code> to <code>null</code>. </p> <p> Compare the C# code with <a href="/2020/12/28/an-f-demo-of-validation-with-partial-data-round-trip">the corresponding F#</a> or <a href="/2020/12/21/a-haskell-proof-of-concept-of-validation-with-partial-data-round-trip">Haskell code</a> and notice how much more verbose the C# has to be. </p> <p> While it's possible to translate many functional programming concepts to a language like C#, syntax does matter, because it affects readability. </p> <h3 id="2113a955061341ab9e2dba711aaf8457"> Validating date of birth <a href="#2113a955061341ab9e2dba711aaf8457">#</a> </h3> <p> From here, the port is direct, if awkward. Here's how to validate the date-of-birth field: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;Validated&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;DateTime&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">TryParseDoB</span>(DateTime&nbsp;<span style="font-weight:bold;color:#1f377f;">now</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!DoB.HasValue) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;DateTime&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;dob&nbsp;is&nbsp;required&quot;</span>&nbsp;})); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(DoB.Value&nbsp;&lt;=&nbsp;now.AddYears(-12)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;DateTime&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;i&nbsp;<span style="color:blue;">with</span>&nbsp;{&nbsp;DoB&nbsp;=&nbsp;<span style="color:blue;">null</span>&nbsp;},&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;get&nbsp;off&nbsp;my&nbsp;lawn&quot;</span>&nbsp;})); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;DateTime&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoB.Value); }</pre> </p> <p> I suspect that the age check should really have been a greater-than relation, but I'm only reproducing the original code. </p> <h3 id="e1fc6b98e4fb4dad81ee5e354032acb8"> Validating addresses <a href="#e1fc6b98e4fb4dad81ee5e354032acb8">#</a> </h3> <p> The final building block is to parse the input address: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;Validated&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;<span style="color:blue;">string</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">TryParseAddress</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(Address&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;<span style="color:blue;">string</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;add1&nbsp;is&nbsp;required&quot;</span>&nbsp;})); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;<span style="color:blue;">string</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Address); }</pre> </p> <p> The <code>TryParseAddress</code> only checks whether or not the <code>Address</code> field is present. </p> <h3 id="b11153a62fa945568e880cf771a7cb19"> Composition <a href="#b11153a62fa945568e880cf771a7cb19">#</a> </h3> <p> The above methods are <code>private</code> because the entire problem is simple enough that I can test the composition as a whole. Had I wanted to, however, I could easily have made them <code>public</code> and tested them individually. </p> <p> You can now use applicative composition to produce a single validation method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Validated&lt;(Input,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;ValidInput&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">TryParse</span>(DateTime&nbsp;<span style="font-weight:bold;color:#1f377f;">now</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>&nbsp;=&nbsp;TryParseName(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dob</span>&nbsp;=&nbsp;TryParseDoB(now); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">address</span>&nbsp;=&nbsp;TryParseAddress(); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;DateTime,&nbsp;<span style="color:blue;">string</span>,&nbsp;ValidInput&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">createValid</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="font-weight:bold;color:#1f377f;">n</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">a</span>)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;ValidInput(n,&nbsp;d,&nbsp;a); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;(Func&lt;Input,&nbsp;Input&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;)&nbsp;<span style="color:#74531f;">combineErrors</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Func&lt;Input,&nbsp;Input&gt;&nbsp;f,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;&nbsp;es)&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Func&lt;Input,&nbsp;Input&gt;&nbsp;g,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;&nbsp;es)&nbsp;<span style="font-weight:bold;color:#1f377f;">y</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">z</span>&nbsp;=&gt;&nbsp;y.g(x.f(z)),&nbsp;y.es.Concat(x.es).ToArray()); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;createValid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(name,&nbsp;combineErrors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(dob,&nbsp;combineErrors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(address,&nbsp;combineErrors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectFailure(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;(x.Item1(<span style="color:blue;">this</span>),&nbsp;x.Item2)); }</pre> </p> <p> This is where the <code>Validated</code> API is still awkward. You need to explicitly define a function to compose error cases. In this case, <code>combineErrors</code> composes the <a href="/2017/11/13/endomorphism-monoid">endomorphisms</a> and concatenates the collections. </p> <p> The final step 'runs' the endomorphism. <code>x.Item1</code> is the endomorphism, and <code>this</code> is the <code>Input</code> value being validated. Again, this isn't readable in C#, but it's where the endomorphism removes the invalid values from the input. </p> <h3 id="8aa59e20c1924002ae0d4e951df71619"> Tests <a href="#8aa59e20c1924002ae0d4e951df71619">#</a> </h3> <p> Since <a href="/2018/11/05/applicative-validation">applicative validation</a> is a functional technique, it's <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. </p> <p> Testing a successful validation is as easy as this: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ValidationSucceeds</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">now</span>&nbsp;=&nbsp;DateTime.Now; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">eightYearsAgo</span>&nbsp;=&nbsp;now.AddYears(-8); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">input</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Alice&quot;</span>,&nbsp;eightYearsAgo,&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;input.TryParse(now); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;Validated.Succeed&lt;(Input,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;),&nbsp;ValidInput&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ValidInput(<span style="color:#a31515;">&quot;Alice&quot;</span>,&nbsp;eightYearsAgo,&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> As is often the case, the error conditions are more numerous, or more interesting, if you will, than the success case, so this requires a parametrised test: </p> <p> <pre>[Theory,&nbsp;ClassData(<span style="color:blue;">typeof</span>(ValidationFailureTestCases))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ValidationFails</span>( &nbsp;&nbsp;&nbsp;&nbsp;Input&nbsp;<span style="font-weight:bold;color:#1f377f;">input</span>, &nbsp;&nbsp;&nbsp;&nbsp;Input&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>, &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">expectedMessages</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">now</span>&nbsp;=&nbsp;DateTime.Now; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;input.TryParse(now); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(<span style="font-weight:bold;color:#1f377f;">inp</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">msgs</span>)&nbsp;=&nbsp;Assert.Single(actual.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onFailure:&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;x&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onSuccess:&nbsp;<span style="font-weight:bold;color:#1f377f;">_</span>&nbsp;=&gt;&nbsp;Array.Empty&lt;(Input,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;)&gt;())); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;inp); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expectedMessages,&nbsp;msgs); }</pre> </p> <p> I also had to take <code>actual</code> apart in order to inspects its individual elements. When working with a pure and immutable data structure, I consider that a test smell. Rather, one should be able to use <a href="/2021/05/03/structural-equality-for-better-tests">structural equality for better tests</a>. Unfortunately, .NET collections don't have structural equality, so the test has to pull the message collection out of <code>actual</code> in order to verify it. </p> <p> Again, in F# or <a href="https://www.haskell.org/">Haskell</a> you don't have that problem, and the tests are much more succinct and robust. </p> <p> The test cases are implemented by this nested <code>ValidationFailureTestCases</code> class: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ValidationFailureTestCases</span>&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;TheoryData&lt;Input,&nbsp;Input,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ValidationFailureTestCases</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Input(<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Input(<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;add1&nbsp;is&nbsp;required&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;dob&nbsp;is&nbsp;required&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;name&nbsp;is&nbsp;required&quot;</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Bob&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Input(<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;add1&nbsp;is&nbsp;required&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;dob&nbsp;is&nbsp;required&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;no&nbsp;bob&nbsp;and&nbsp;toms&nbsp;allowed&quot;</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Alice&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Alice&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;add1&nbsp;is&nbsp;required&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;dob&nbsp;is&nbsp;required&quot;</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">eightYearsAgo</span>&nbsp;=&nbsp;DateTime.Now.AddYears(-8); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Alice&quot;</span>,&nbsp;eightYearsAgo,&nbsp;<span style="color:blue;">null</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Alice&quot;</span>,&nbsp;eightYearsAgo,&nbsp;<span style="color:blue;">null</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;add1&nbsp;is&nbsp;required&quot;</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">fortyYearsAgo</span>&nbsp;=&nbsp;DateTime.Now.AddYears(-40); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Alice&quot;</span>,&nbsp;fortyYearsAgo,&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Alice&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;get&nbsp;off&nbsp;my&nbsp;lawn&quot;</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Tom&quot;</span>,&nbsp;fortyYearsAgo,&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Input(<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;get&nbsp;off&nbsp;my&nbsp;lawn&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;no&nbsp;bob&nbsp;and&nbsp;toms&nbsp;allowed&quot;</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Input(<span style="color:#a31515;">&quot;Tom&quot;</span>,&nbsp;eightYearsAgo,&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Input(<span style="color:blue;">null</span>,&nbsp;eightYearsAgo,&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;no&nbsp;bob&nbsp;and&nbsp;toms&nbsp;allowed&quot;</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> All eight tests pass. </p> <h3 id="96596b52720f4a2688c216701f48d559"> Conclusion <a href="#96596b52720f4a2688c216701f48d559">#</a> </h3> <p> Once you know <a href="/2018/05/22/church-encoding">how to model sum types (discriminated unions) in C#</a>, translating something like applicative validation isn't difficult per se. It's a fairly automatic process. </p> <p> The code is hardly <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> C#, and the type annotations are particularly annoying. Things work as expected though, and it isn't difficult to imagine how one could refactor some of this code to a more idiomatic form. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Domain Model first https://blog.ploeh.dk/2023/10/23/domain-model-first 2023-10-23T06:09:00+00:00 Mark Seemann <div id="post"> <p> <em>Persistence concerns second.</em> </p> <p> A few weeks ago, I published an article with the title <a href="/2023/09/18/do-orms-reduce-the-need-for-mapping">Do ORMs reduce the need for mapping?</a> Not surprisingly, this elicited more than one reaction. In this article, I'll respond to a particular kind of reaction. </p> <p> First, however, I'd like to reiterate the message of the previous article, which is almost revealed by the title: <em>Do <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping">object-relational mappers</a> (ORMs) reduce the need for mapping?</em> To which the article answers a tentative <em>no</em>. </p> <p> Do pay attention to the question. It doesn't ask whether ORMs are bad in general, or in all cases. It mainly analyses whether the use of ORMs reduces the need to write code that maps between different representations of data: From database to objects, from objects to <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects</a> (DTOs), etc. </p> <p> Granted, the article looks at a wider context, which I think is only a responsible thing to do. This could lead some readers to extrapolate from the article's specific focus to draw a wider conclusion. </p> <h3 id="951d538881fd4464a081ba3cd09162b0"> Encapsulation-first <a href="#951d538881fd4464a081ba3cd09162b0">#</a> </h3> <p> Most of the systems I work with aren't <a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> systems, but rather systems where correctness is important. As an example, one of my clients does security-heavy digital infrastructure. Earlier in my career, I helped write web shops when these kinds of systems were new. Let me tell you: System owners were quite concerned that prices were correct, and that orders were taken and handled without error. </p> <p> In my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> I've tried to capture the essence of those kinds of system with the accompanying sample code, which pretends to be an online restaurant reservation system. While this may sound like a trivial CRUD system, <a href="/2020/01/27/the-maitre-d-kata">the business logic isn't entirely straightforward</a>. </p> <p> The point I was making in <a href="/2023/09/18/do-orms-reduce-the-need-for-mapping">the previous article</a> is that I consider <a href="/encapsulation-and-solid">encapsulation</a> to be more important than 'easy' persistence. I don't mind writing a bit of mapping code, since <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">typing isn't a programming bottleneck</a> anyway. </p> <p> When prioritising encapsulation you should be able to make use of any design pattern, run-time assertion, as well as static type systems (if you're working in such a language) to guard correctness. You should be able to compose objects, define <a href="https://en.wikipedia.org/wiki/Value_object">Value Objects</a>, <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">wrap single values to avoid primitive obsession</a>, make constructors private, leverage polymorphism and effectively use any trick your language, <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiom</a>, and platform has on offer. If you want to use <a href="/2018/05/22/church-encoding">Church encoding</a> or the <a href="/2018/06/25/visitor-as-a-sum-type">Visitor pattern to represent a sum type</a>, you should be able to do that. </p> <p> When writing these kinds of systems, I start with the Domain Model without any thought of how to persist or retrieve data. </p> <p> In my experience, once the Domain Model starts to congeal, the persistence question tends to answer itself. There's usually one or two obvious ways to store and read data. </p> <p> Usually, a relational database isn't the most obvious choice. </p> <h3 id="1b562dd9077e4b27b912d782bdca14fb"> Persistence ignorance <a href="#1b562dd9077e4b27b912d782bdca14fb">#</a> </h3> <p> Write the best API you can to solve the problem, and then figure out how to store data. This is the allegedly elusive ideal of <em>persistence ignorance</em>, which turns out to be easier than rumour has it, once you cast a wider net than relational databases. </p> <p> It seems to me, though, that more than one person who has commented on my previous article have a hard time considering alternatives. And granted, I've consulted with clients who knew how to operate a particular database system, but nothing else, and who didn't want to consider adopting another technology. I do understand that such constraints are real, too. Thus, if you need to compromise for reasons such as these, you aren't doing anything wrong. You may still, however, try to get the best out of the situation. </p> <p> One client of mine, for example, didn't want to operate anything else than <a href="https://en.wikipedia.org/wiki/Microsoft_SQL_Server">SQL Server</a>, which they already know. For an asynchronous message-based system, then, we chose <a href="https://particular.net/nservicebus">NServiceBus</a> and configured it to use SQL Server as a persistent queue. </p> <p> Several comments still seem to assume that persistence must look in a particular way. </p> <blockquote> <p> "So having a Order, OrderLine, Person, Address and City, all the rows needed to be loaded in advance, mapped to objects and references set to create the object graph to be able to, say, display shipping costs based on person's address." </p> <footer><cite><a href="/2023/09/18/do-orms-reduce-the-need-for-mapping#75ca5755d2a4445ba4836fc3f6922a5c">Vlad</a></cite></footer> </blockquote> <p> I don't wish to single out Vlad, but this is both the first comment, and it captures the essence of other comments well. I imagine that what he has in mind is something like this: </p> <p> <img src="/content/binary/orders-db-diagram.png" alt="Database diagram with five tables: Orders, OrderLines, Persons, Addresses, and Cities."> </p> <p> I've probably simplified things a bit too much. In a more realistic model, each person may have a collection of addresses, instead of just one. If so, it only strengthens Vlad's point, because that would imply even more tables to read. </p> <p> The unstated assumption, however, is that a fully <a href="https://en.wikipedia.org/wiki/Database_normalization">normalised</a> relational data model is the correct way to store such data. </p> <p> It's not. As I already mentioned, I spent the first four years of my programming career developing web shops. Orders were an integral part of that work. </p> <p> An order is a <em>document</em>. You don't want the customer's address to be updatable after the fact. With a normalised relational model, if you change the customer's address row in the future, it's going to look as though the order went to that address instead of the address it actually went to. </p> <p> This also explains why the order lines should <em>not</em> point to the actually product entries in the product catalogue. Trust me, I almost shipped such a system once, when I was young and inexperienced. </p> <p> You should, at the very least, denormalise the database model. To a degree, this has already happened here, since the implied order has order lines, that, I hope, are copies of the relevant product data, rather than linked to the product catalogue. </p> <p> Such insights, however, suggest that other storage mechanisms may be more appropriate. </p> <p> Putting that aside for a moment, though, how would a persistence-ignorant Domain Model look? </p> <p> I'd probably start with something like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">order</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Order( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Person(<span style="color:#a31515;">&quot;Olive&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Hoyle&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Address(<span style="color:#a31515;">&quot;Green&nbsp;Street&nbsp;15&quot;</span>,&nbsp;<span style="color:blue;">new</span>&nbsp;City(<span style="color:#a31515;">&quot;Oakville&quot;</span>),&nbsp;<span style="color:#a31515;">&quot;90125&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;OrderLine(123,&nbsp;1), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;OrderLine(456,&nbsp;3), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;OrderLine(789,&nbsp;2));</pre> </p> <p> (As <a href="/ref/90125">the ZIP code</a> implies, I'm more of a <a href="https://en.wikipedia.org/wiki/Yes_(band)">Yes</a> fan, but still can't help but relish writing <code>new Order</code> in code.) </p> <p> With code like this, many a <a href="/ref/ddd">DDD</a>'er would start talking about Aggregate Roots, but that is, frankly, a concept that never made much sense to me. Rather, the above <code>order</code> is a <a href="https://en.wikipedia.org/wiki/Tree_(graph_theory)">tree</a> composed of immutable data structures. </p> <p> It trivially serializes to e.g. JSON: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;customer&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;firstName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Olive&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;lastName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Hoyle&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;address&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;street&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Green&nbsp;Street&nbsp;15&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;city&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Oakville&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;zipCode&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;90125&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;orderLines&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;sku&quot;</span>:&nbsp;123, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;sku&quot;</span>:&nbsp;456, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;3 &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;sku&quot;</span>:&nbsp;789, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> All of this strongly suggests that this kind of data would be <em>much easier</em> to store and retrieve with a document database instead of a relational database. </p> <p> While that's just one example, it strikes me as a common theme when discussing persistence. For most online transaction processing systems, relational database aren't necessarily the best fit. </p> <h3 id="3643959a545940f88001fb82297a286e"> The cart before the horse <a href="#3643959a545940f88001fb82297a286e">#</a> </h3> <p> <a href="/2023/09/18/do-orms-reduce-the-need-for-mapping#359a7bb0d2c14b8eb2dcb2ac6de4897d">Another comment</a> also starts with the premise that a data model is fundamentally relational. This one purports to model the relationship between sheikhs, their wives, and supercars. While I understand that the example is supposed to be tongue-in-cheek, the comment launches straight into problems with how to read and persist such data without relying on an ORM. </p> <p> Again, I don't intend to point fingers at anyone, but on the other hand, I can't suggest alternatives when a problem is presented like that. </p> <p> The whole point of developing a Domain Model <em>first</em> is to find a good way to represent the business problem in a way that encourages correctness and ease of use. </p> <p> If you present me with a relational model without describing the business goals you're trying to achieve, I don't have much to work with. </p> <p> It may be that your business problem is truly relational, in which case an ORM probably is a good solution. I wrote as much in the previous article. </p> <p> In many cases, however, it looks to me as though programmers start with a relational model, only to proceed to complain that it's difficult to work with in object-oriented (or functional) code. </p> <p> If you, on the other hand, start with the business problem and figure out how to model it in code, the best way to store the data may suggest itself. Document databases are often a good fit, as are event stores. I've never had need for a graph database, but perhaps that would be a better fit for the <em>sheikh</em> domain suggested by <em>qfilip</em>. </p> <h3 id="8c32485e1ffd42f4ace9b83c98ae3184"> Reporting <a href="#8c32485e1ffd42f4ace9b83c98ae3184">#</a> </h3> <p> While I no longer feel that relational databases are particularly well-suited for online transaction processing, they are really good at one thing: Ad-hoc querying. Because it's such a rich and mature type of technology, and because <a href="https://en.wikipedia.org/wiki/SQL">SQL</a> is a powerful language, you can slice and dice data in multiple ways. </p> <p> This makes relational databases useful for reporting and other kinds of data extraction tasks. </p> <p> You may have business stakeholders who insist on a relational database for that particular reason. It may even be a good reason. </p> <p> If, however, the sole purpose of having a relational database is to support reporting, you may consider setting it up as a secondary system. Keep your online transactional data in another system, but regularly synchronize it to a relational database. If the only purpose of the relational database is to support reporting, you can treat it as a read-only system. This makes synchronization manageable. In general, you should avoid two-way synchronization if at all possible, but one-way synchronization is usually less of a problem. </p> <p> Isn't that going to be more work, or more expensive? </p> <p> That question, again, has no single answer. Of course setting up and maintaining two systems is more work at the outset. On the other hand, there's a perpetual cost to be paid if you come up with the wrong architecture. If development is slow, and you have many bugs in production, or similar problems, the cause could be that you've chosen the wrong architecture and you're now fighting a losing battle. </p> <p> On the other hand, if you relegate relational databases exclusively to a reporting role, chances are that there's a lot of off-the-shelf software that can support your business users. Perhaps you can even hire a paratechnical power user to take care of that part of the system, freeing you to focus on the 'actual' system. </p> <p> All of this is only meant as inspiration. If you don't want to, or can't, do it that way, then this article doesn't help you. </p> <h3 id="1b0ce932168349f8abb9887f9ed219c8"> Conclusion <a href="#1b0ce932168349f8abb9887f9ed219c8">#</a> </h3> <p> When discussing databases, and particularly ORMs, some people approach the topic with the unspoken assumption that a relational database is the only option for storing data. Many programmers are so skilled in relational data design that they naturally use those skills when thinking new problems over. </p> <p> Sometimes problems are just relational in nature, and that's fine. More often than not, however, that's not the case. </p> <p> Try to model a business problem without concern for storage and see where that leads you. Test-driven development is often a great technique for such a task. Then, once you have a good API, consider how to store the data. The Domain Model that you develop in that way may naturally suggest a good way to store and retrieve the data. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="db4a9a94452a4cc7bf71989561dfd947"> <div class="comment-author"><a href="#db4a9a94452a4cc7bf71989561dfd947">qfilip</a></div> <div class="comment-content"> <q> <i> Again, I don't intend to point fingers at anyone, but on the other hand, I can't suggest alternatives when a problem is presented like that. </i> </q> <p> Heh, that's fair criticism, not finger pointing. I wanted to give a better example here, but I gave up halfway through writing it. You raised some good points. I'll have to rethink my approach on domain modeling further, before asking any meaningful questions. </p> <p> Years of working with EF-Core in a specific way got me... indoctrinated. Not all things are bad ofcourse, but I have missed the bigger picture in some areas, as far as I can tell. </p> <p> Thanks for dedicating so many articles to the subject. </p> </div> <div class="comment-date">2023-10-23 18:05 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. At the boundaries, static types are illusory https://blog.ploeh.dk/2023/10/16/at-the-boundaries-static-types-are-illusory 2023-10-16T08:07:00+00:00 Mark Seemann <div id="post"> <p> <em>Static types are useful, but have limitations.</em> </p> <p> Regular readers of this blog may have noticed that I like static type systems. Not the kind of static types offered by <a href="https://en.wikipedia.org/wiki/C_(programming_language)">C</a>, which strikes me as mostly being able to distinguish between way too many types of integers and pointers. <a href="/2020/01/20/algebraic-data-types-arent-numbers-on-steroids">A good type system is more than just numbers on steroids</a>. A type system like C#'s is <a href="/2019/12/16/zone-of-ceremony">workable, but verbose</a>. The kind of type system I find most useful is when it has <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> and good type inference. The examples that I know best are the type systems of <a href="https://fsharp.org/">F#</a> and <a href="https://www.haskell.org/">Haskell</a>. </p> <p> As great as static type systems can be, they have limitations. <a href="https://www.hillelwayne.com/post/constructive/">Hillel Wayne has already outlined one kind of distinction</a>, but here I'd like to focus on another constraint. </p> <h3 id="ab0d595d35304a9ea9302197b4f796d3"> Application boundaries <a href="#ab0d595d35304a9ea9302197b4f796d3">#</a> </h3> <p> Any piece of software interacts with the 'rest of the world'; effectively everything outside its own process. Sometimes (but increasingly rarely) such interaction is exclusively by way of some user interface, but more and more, an application interacts with other software in some way. </p> <p> <img src="/content/binary/application-boundary.png" alt="A application depicted as an opaque disk with a circle emphasising its boundary. Also included are arrows in and out, with some common communication artefacts: Messages, HTTP traffic, and a database."> </p> <p> Here I've drawn the application as an opaque disc in order to emphasise that what happens inside the process isn't pertinent to the following discussion. The diagram also includes some common kinds of traffic. Many applications rely on some kind of database or send messages (email, SMS, Commands, Events, etc.). We can think of such traffic as the interactions that the application initiates, but many systems also receive and react to incoming data: HTTP traffic or messages that arrive on a queue, and so on. </p> <p> When I talk about application <em>boundaries</em>, I have in mind what goes on in that interface layer. </p> <p> An application can talk to the outside world in multiple ways: It may read or write a file, access shared memory, call operating-system APIs, send or receive network packets, etc. Usually you get to program against higher-level abstractions, but ultimately the application is dealing with various binary protocols. </p> <h3 id="4991578e222e408bb08e261dce6454f1"> Protocols <a href="#4991578e222e408bb08e261dce6454f1">#</a> </h3> <p> The bottom line is that at a sufficiently low level of abstraction, what goes in and out of your application has no static type stronger than an array of bytes. </p> <p> You may counter-argue that higher-level APIs deal with that to present the input and output as static types. When you interact with a text file, you'll typically deal with a list of strings: One for each line in the file. Or you may manipulate <a href="https://en.wikipedia.org/wiki/JSON">JSON</a>, <a href="https://en.wikipedia.org/wiki/XML">XML</a>, <a href="https://en.wikipedia.org/wiki/Protocol_Buffers">Protocol Buffers</a>, or another wire format using a serializer/deserializer API. Sometime, as is often the case with <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a>, you may need to write a very simple parser yourself. Or perhaps <a href="/2023/08/28/a-first-crack-at-the-args-kata">something slightly more involved</a>. </p> <p> To demonstrate what I mean, there's no shortage of APIs like <a href="https://learn.microsoft.com/dotnet/api/system.text.json.jsonserializer.deserialize">JsonSerializer.Deserialize</a>, which enables you to write <a href="/2022/05/02/at-the-boundaries-applications-arent-functional">code like this</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;n&nbsp;=&nbsp;JsonSerializer.Deserialize&lt;Name&gt;&nbsp;(json,&nbsp;opts)</pre> </p> <p> and you may say: <em><code>n</code> is statically typed, and its type is <code>Name</code>! Hooray!</em> But you do realise that that's only half a truth, don't you? </p> <p> An interaction at the application boundary is expected to follow some kind of <em>protocol</em>. This is even true if you're reading a text file. In these modern times, you may expect a text file to contain <a href="https://unicode.org/">Unicode</a>, but have you ever received a file from a legacy system and have to deal with its <a href="https://en.wikipedia.org/wiki/EBCDIC">EBCDIC</a> encoding? Or an <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> file with a <a href="https://en.wikipedia.org/wiki/Code_page">code page</a> different from the one you expect? Or even just a file written on a Unix system, if you're on Windows, or vice versa? </p> <p> In order to correctly interpret or transmit such data, you need to follow a <em>protocol</em>. </p> <p> Such a protocol can be low-level, as the character-encoding examples I just listed, but it may also be much more high-level. You may, for example, consider an HTTP request like this: </p> <p> <pre>POST /restaurants/90125/reservations?sig=aco7VV%2Bh5sA3RBtrN8zI8Y9kLKGC60Gm3SioZGosXVE%3D HTTP/1.1 Content-Type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-08&nbsp;20:30&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;snomob@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Snow&nbsp;Moe&nbsp;Beal&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 }</pre> </p> <p> Such an interaction implies a protocol. Part of such a protocol is that the HTTP request's body is a valid JSON document, that it has an <code>at</code> property, that that property encodes a valid date and time, that <code>quantity</code> is a natural number, that <code>email</code> <a href="/2023/07/03/validating-or-verifying-emails">is present</a>, and so on. </p> <p> You can model the expected input as a <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a> (DTO): </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;At&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> and even set up your 'protocol handlers' (here, an ASP.NET Core <a href="https://learn.microsoft.com/aspnet/core/mvc/controllers/actions">action method</a>) to use such a DTO: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Post</span>(ReservationDto&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>)</pre> </p> <p> While this may look statically typed, it assumes a particular protocol. What happens when the bytes on the wire don't follow the protocol? </p> <p> Well, we've already been <a href="/2022/08/15/aspnet-validation-revisited">around that block</a> <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">more than once</a>. </p> <p> The point is that there's always an implied protocol at the application boundary, and you can choose to model it more or less explicitly. </p> <h3 id="41f3b4ad7a4b4429bba3f619c2af55d1"> Types as short-hands for protocols <a href="#41f3b4ad7a4b4429bba3f619c2af55d1">#</a> </h3> <p> In the above example, I've relied on <em>some</em> static typing to deal with the problem. After all, I did define a DTO to model the expected shape of input. I could have chosen other alternatives: Perhaps I could have used a JSON parser to explicitly <a href="https://learn.microsoft.com/dotnet/standard/serialization/system-text-json/use-dom">use the JSON DOM</a>, or even more low-level <a href="https://learn.microsoft.com/dotnet/standard/serialization/system-text-json/use-utf8jsonreader">used Utf8JsonReader</a>. Ultimately, I could have decided to write my own JSON parser. </p> <p> I'd rarely (or never?) choose to implement a JSON parser from scratch, so that's not what I'm advocating. Rather, my point is that you can leverage existing APIs to deal with input and output, and some of those APIs offer a convincing illusion that what happens at the boundary is statically typed. </p> <p> This illusion is partly API-specific, and partly language-specific. In .NET, for example, <code>JsonSerializer.Deserialize</code> <em>looks</em> like it'll always deserialize <em>any</em> JSON string into the desired model. Obviously, that's a lie, because the function will throw an exception if the operation is impossible (i.e. when the input is malformed). In .NET (and many other languages or platforms), you can't tell from an API's type what the failure modes might be. In contrast, aeson's <a href="https://hackage.haskell.org/package/aeson/docs/Data-Aeson.html#v:fromJSON">fromJSON</a> function returns a type that explicitly indicates that deserialization may fail. Even in Haskell, however, this is mostly an <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> convention, because Haskell also 'supports' exceptions. </p> <p> At the boundary, a static type can be a useful shorthand for a protocol. You declare a static type (e.g. a DTO) and rely on built-in machinery to handle malformed input. You give up some fine-grained control in exchange for a more declarative model. </p> <p> I often choose to do that because I find such a trade-off beneficial, but I'm under no illusion that my static types fully model what goes 'on the wire'. </p> <h3 id="792212e79feb46889e71a6c08dedb88e"> Reversed roles <a href="#792212e79feb46889e71a6c08dedb88e">#</a> </h3> <p> So far, I've mostly discussed input validation. <a href="/2022/08/22/can-types-replace-validation">Can types replace validation?</a> No, but they can make most common validation scenarios easier. What happens when you return data? </p> <p> You may decide to return a statically typed value. A serializer can faithfully convert such a value to a proper wire format (JSON, XML, or similar). The recipient may not care about that type. After all, you may return a Haskell value, but the system receiving the data is written in <a href="https://www.python.org/">Python</a>. Or you return a C# object, but the recipient is <a href="https://en.wikipedia.org/wiki/JavaScript">JavaScript</a>. </p> <p> Should we conclude, then, that there's no reason to model return data with static types? Not at all, because by modelling output with static types, you are being <a href="https://en.wikipedia.org/wiki/Robustness_principle">conservative with what you send</a>. Since static types are typically more rigid than 'just code', there may be corner cases that a type can't easily express. While this may pose a problem when it comes to input, it's only a benefit when it comes to output. This means that you're <a href="/2021/11/29/postels-law-as-a-profunctor">narrowing the output funnel</a> and thus making your system easier to work with. </p> <p> <img src="/content/binary/liberal-conservative-at-boundary.png" alt="Funnels labelled 'liberal' and 'conservative' to the left of an line indicating an application boundary."> </p> <p> Now consider another role-reversal: When your application <em>initiates</em> an interaction, it starts by producing output and receives input as a result. This includes any database interaction. When you create, update, or delete a row in a database, you <em>send</em> data, and receive a response. </p> <p> Should you not consider <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> in that case? </p> <p> <img src="/content/binary/conservative-liberal-at-boundary.png" alt="Funnels labelled 'conservative' and 'liberal' to the right of an line indicating an application boundary."> </p> <p> Most people don't, particularly if they rely on <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping">object-relational mappers</a> (ORMs). After all, if you have a static type (class) that models a database row, what's the harm using that when updating the database? </p> <p> Probably none. After all, based on what I've just written, using a static type is a good way to be conservative with what you send. Here's an example using <a href="https://en.wikipedia.org/wiki/Entity_Framework">Entity Framework</a>: </p> <p> <pre><span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantsContext(ConnectionString); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dbReservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation { &nbsp;&nbsp;&nbsp;&nbsp;PublicId&nbsp;=&nbsp;reservation.Id, &nbsp;&nbsp;&nbsp;&nbsp;RestaurantId&nbsp;=&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;reservation.At, &nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;reservation.Name.ToString(), &nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;reservation.Email.ToString(), &nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;reservation.Quantity }; <span style="color:blue;">await</span>&nbsp;db.Reservations.AddAsync(dbReservation); <span style="color:blue;">await</span>&nbsp;db.SaveChangesAsync();</pre> </p> <p> Here we send a statically typed <code>Reservation</code> 'Entity' to the database, and since we use a static type, we're being conservative with what we send. That's only good. </p> <p> What happens when we query a database? Here's a typical example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;Restaurants.Reservation?&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">ReadReservation</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurantId</span>,&nbsp;Guid&nbsp;<span style="font-weight:bold;color:#1f377f;">id</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantsContext(ConnectionString); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;db.Reservations.FirstOrDefaultAsync(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x.PublicId&nbsp;==&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(r&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Restaurants.Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.PublicId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.At, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(r.Email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(r.Name), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.Quantity); }</pre> </p> <p> Here I read a database row <code>r</code> and unquestioning translate it to my domain model. Should I do that? What if the database schema has diverged from my application code? </p> <p> I suspect that much grief and trouble with relational databases, and particularly with ORMs, stem from the illusion that an ORM 'Entity' is a statically-typed view of the database schema. Typically, you can either use an ORM like Entity Framework in a code-first or a database-first fashion, but regardless of what you choose, you have two competing 'truths' about the database: The database schema and the Entity Classes. </p> <p> You need to be disciplined to keep those two views in synch, and I'm not asserting that it's impossible. I'm only suggesting that it may pay to explicitly acknowledge that static types may not represent any truth about what's actually on the other side of the application boundary. </p> <h3 id="1ab46f8e48a74b94ad9aa92cce2d915f"> Types are an illusion <a href="#1ab46f8e48a74b94ad9aa92cce2d915f">#</a> </h3> <p> Given that I usually find myself firmly in the static-types-are-great camp, it may seem odd that I now spend an entire article trashing them. Perhaps it looks as though I've had a revelation and made an about-face, but that's not the case. Rather, I'm fond of making the implicit explicit. This often helps improve understanding, because it helps delineate conceptual boundaries. </p> <p> This, too, is the case here. <a href="https://en.wikipedia.org/wiki/All_models_are_wrong">All models are wrong, but some models are useful</a>. So are static types, I believe. </p> <p> A static type system is a useful tool that enables you to model how your application should behave. The types don't really exist at run time. Even though .NET code (just to point out an example) compiles to <a href="https://en.wikipedia.org/wiki/Common_Intermediate_Language">a binary representation that includes type information</a>, once it runs, it <a href="https://en.wikipedia.org/wiki/Just-in-time_compilation">JITs</a> to machine code. In the end, it's just registers and memory addresses, or, if you want to be even more nihilistic, electrons moving around on a circuit board. </p> <p> Even at a higher level of abstraction, you may say: <em>But at least, a static type system can help you encapsulate rules and assumptions.</em> In a language like C#, for example, consider a <a href="https://www.hillelwayne.com/post/constructive/">predicative type</a> like <a href="/2022/08/22/can-types-replace-validation">this NaturalNumber</a> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span>&nbsp;:&nbsp;IEquatable&lt;NaturalNumber&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span>(<span style="color:blue;">int</span>&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(candidate&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(candidate), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;The&nbsp;value&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;(non-zero)&nbsp;number,&nbsp;but&nbsp;was:&nbsp;</span>{candidate}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.i&nbsp;=&nbsp;candidate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Various&nbsp;other&nbsp;members&nbsp;follow...</span></pre> </p> <p> Such a class effectively protects the invariant that a <a href="https://en.wikipedia.org/wiki/Natural_number">natural number</a> is always a positive integer. Yes, that works well until someone does this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">n</span>&nbsp;=&nbsp;(NaturalNumber)FormatterServices.GetUninitializedObject(<span style="color:blue;">typeof</span>(NaturalNumber));</pre> </p> <p> This <code>n</code> value has the internal value <code>0</code>. Yes, <a href="https://learn.microsoft.com/dotnet/api/system.runtime.serialization.formatterservices.getuninitializedobject">FormatterServices.GetUninitializedObject</a> bypasses the constructor. This thing is evil, but it exists, and at least in the current discussion serves to illustrate the point that types are illusions. </p> <p> This isn't just a flaw in C#. Other languages have similar backdoors. One of the most famously statically-typed languages, Haskell, comes with <a href="https://hackage.haskell.org/package/base/docs/System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a>, which enables you to pretend that nothing untoward is going on even if you've written some impure code. </p> <p> You may (and should) institute policies to not use such backdoors in your normal code bases. You don't need them. </p> <h3 id="de28c90a44e14b299f6eb30c09b08821"> Types are useful models <a href="#de28c90a44e14b299f6eb30c09b08821">#</a> </h3> <p> All this may seem like an argument that types are useless. That would, however, be to draw the wrong conclusion. Types don't exist at run time to the same degree that Python objects or JavaScript functions don't exist at run time. Any language (except <a href="https://en.wikipedia.org/wiki/Assembly_language">assembler</a>) is an abstraction: A way to model computer instructions so that programming becomes easier (one would hope, <a href="/2023/09/11/a-first-stab-at-the-brainfuck-kata">but then...</a>). This is true even for C, as low-level and detail-oriented as it may seem. </p> <p> If you grant that high-level programming languages (i.e. any language that is <em>not</em> machine code or assembler) are useful, you must also grant that you can't rule out the usefulness of types. Notice that this argument is one of logic, rather than of preference. The only claim I make here is that programming is based on useful illusions. That the abstractions are illusions don't prevent them from being useful. </p> <p> In statically typed languages, we effectively need to pretend that the type system is good enough, strong enough, generally trustworthy enough that it's safe to ignore the underlying reality. We work with, if you will, a provisional truth that serves as a user interface to the computer. </p> <p> Even though a computer program eventually executes on a processor where types don't exist, a good compiler can still check that our models look sensible. We say that it <em>type-checks</em>. I find that indispensable when modelling the internal behaviour of a program. Even in a large code base, a compiler can type-check whether all the various components look like they may compose correctly. That a program compiles is no guarantee that it works correctly, but if it doesn't type-check, it's strong evidence that the code's model is <em>internally</em> inconsistent. </p> <p> In other words, that a statically-typed program type-checks is a necessary, but not a sufficient condition for it to work. </p> <p> This holds as long as we're considering program internals. Some language platforms allow us to take this notion further, because we can link software components together and still type-check them. The .NET platform is a good example of this, since the IL code retains type information. This means that the C#, F#, or <a href="https://en.wikipedia.org/wiki/Visual_Basic_(.NET)">Visual Basic .NET</a> compiler can type-check your code against the APIs exposed by external libraries. </p> <p> On the other hand, you can't extend that line of reasoning to the boundary of an application. What happens at the boundary is ultimately untyped. </p> <p> Are types useless at the boundary, then? Not at all. <a href="https://lexi-lambda.github.io/blog/2020/01/19/no-dynamic-type-systems-are-not-inherently-more-open/">Alexis King has already dealt with this topic better than I could</a>, but the point is that types remain an effective way to capture the result of <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">parsing input</a>. You can view receiving, handling, parsing, or validating input as implementing a protocol, as I've already discussed above. Such protocols are application-specific or domain-specific rather than general-purpose protocols, but they are still protocols. </p> <p> When I decide to write <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">input validation for my restaurant sample code base as a set of composable parsers</a>, I'm implementing a protocol. My starting point isn't raw bits, but rather a loose static type: A DTO. In other cases, I may decide to use a different level of abstraction. </p> <p> One of the (many) reasons I have for <a href="/2023/09/18/do-orms-reduce-the-need-for-mapping">finding ORMs unhelpful</a> is exactly because they insist on an illusion past its usefulness. Rather, I prefer implementing the protocol that talks to my database with a lower-level API, such as ADO.NET: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Reservation&nbsp;<span style="color:#74531f;">ReadReservationRow</span>(SqlDataReader&nbsp;<span style="font-weight:bold;color:#1f377f;">rdr</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Guid)rdr[<span style="color:#a31515;">&quot;PublicId&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(DateTime)rdr[<span style="color:#a31515;">&quot;At&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email((<span style="color:blue;">string</span>)rdr[<span style="color:#a31515;">&quot;Email&quot;</span>]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name((<span style="color:blue;">string</span>)rdr[<span style="color:#a31515;">&quot;Name&quot;</span>]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;NaturalNumber((<span style="color:blue;">int</span>)rdr[<span style="color:#a31515;">&quot;Quantity&quot;</span>])); }</pre> </p> <p> This actually isn't a particular good protocol implementation, because it fails to take Postel's law into account. Really, this code should be a <a href="https://martinfowler.com/bliki/TolerantReader.html">Tolerant Reader</a>. In practice, not that much input contravariance is possible, but perhaps, at least, this code ought to gracefully handle if the <code>Name</code> field was missing. </p> <p> The point of this particular example isn't that it's perfect, because it's not, but rather that it's possible to drop down to a lower level of abstraction, and sometimes, this may be a more honest representation of reality. </p> <h3 id="ce2a1d57f63e4f39a28e801fd23164cf"> Conclusion <a href="#ce2a1d57f63e4f39a28e801fd23164cf">#</a> </h3> <p> It may be helpful to acknowledge that static types don't really exist. Even so, internally in a code base, a static type system can be a powerful tool. A good type system enables a compiler to check whether various parts of your code looks internally consistent. Are you calling a procedure with the correct arguments? Have you implemented all methods defined by an interface? Have you handled all cases defined by a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>? Have you correctly initialized an object? </p> <p> As useful type systems are for this kind of work, you should also be aware of their limitations. A compiler can check whether a code base's internal model makes sense, but it can't verify what happens at run time. </p> <p> As long as one part of your code base sends data to another part of your code base, your type system can still perform a helpful sanity check, but for data that enters (or leaves) your application at run time, bets are off. You may attempt to model what input <em>should</em> look like, and it may even be useful to do that, but it's important to acknowledge that reality may not look like your model. </p> <p> You can write statically-typed, composable parsers. Some of them are quite elegant, but the good ones explicitly model that parsing of input is error-prone. When input is well-formed, the result may be a nicely <a href="/2022/10/24/encapsulation-in-functional-programming">encapsulated</a>, statically-typed value, but when it's malformed, the result is one or more error values. </p> <p> Perhaps the most important message is that databases, other web services, file systems, etc. involve input and output, too. Even if <em>you</em> write code that initiates a database query, or a web service request, should you implicitly trust the data that comes back? </p> <p> This question of trust doesn't have to imply security concerns. Rather, systems evolve and errors happen. Every time you interact with an external system, there's a risk that it has become misaligned with yours. Static types can't protect you against that. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. What's a sandwich? https://blog.ploeh.dk/2023/10/09/whats-a-sandwich 2023-10-09T20:20:00+00:00 Mark Seemann <div id="post"> <p> <em>Ultimately, it's more about programming than food.</em> </p> <p> The <a href="https://en.wikipedia.org/wiki/Sandwich">Sandwich</a> was named after <a href="https://en.wikipedia.org/wiki/John_Montagu,_4th_Earl_of_Sandwich">John Montagu, 4th Earl of Sandwich</a> because of his fondness for this kind of food. As popular story has it, he found it practical because it enabled him to eat without greasing the cards he often played. </p> <p> A few years ago, a corner of the internet erupted in good-natured discussion about exactly what constitutes a sandwich. For instance, is the Danish <a href="https://en.wikipedia.org/wiki/Sm%C3%B8rrebr%C3%B8d">smørrebrød</a> a sandwich? It comes in two incarnations: <em>Højtbelagt</em>, the luxury version which is only consumable with knife and fork and the more modest, everyday <em>håndmad</em> (literally <em>hand food</em>), which, while open-faced, can usually be consumed without cutlery. </p> <p> <img src="/content/binary/bjoernekaelderen-hoejtbelagt.jpg" alt="A picture of elaborate Danish smørrebrød."> </p> <p> If we consider the 4th Earl of Sandwich's motivation as a yardstick, then the depicted <em>højtbelagte smørrebrød</em> is hardly a sandwich, while I believe a case can be made that a <em>håndmad</em> is: </p> <p> <img src="/content/binary/haandmadder.jpg" alt="Two håndmadder a half of a sliced apple."> </p> <p> Obviously, you need a different grip on a <em>håndmad</em> than on a sandwich. The bread (<em>rugbrød</em>) is much denser than wheat bread, and structurally more rigid. You eat it with your thumb and index finger on each side, and remaining fingers supporting it from below. The bottom line is this: A single piece of bread with something on top can also solve the original problem. </p> <p> What if we go in the other direction? How about a combo consisting of bread, meat, bread, meat, and bread? I believe that I've seen burgers like that. Can you eat that with one hand? I think that this depends more on how greasy and overfilled it is, than on the structure. </p> <p> What if you had five layers of meat and six layers of bread? This is unlikely to work with traditional Western leavened bread which, being a foam, will lose structural integrity when cut too thin. Imagining other kinds of bread, though, and thin slices of meat (or other 'content'), I don't see why it couldn't work. </p> <h3 id="00d495b0703a45a98f36607e99799c62"> FP sandwiches <a href="#00d495b0703a45a98f36607e99799c62">#</a> </h3> <p> As regular readers may have picked up over the years, I do like food, but this is, after all, a programming blog. </p> <p> A few years ago I presented a functional-programming design pattern named <a href="/2020/03/02/impureim-sandwich">Impureim sandwich</a>. It argues that it's often beneficial to structure a code base according to the <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">functional core, imperative shell</a> architecture. </p> <p> The idea, in a nutshell, is that at every entry point (<code>Main</code> method, message handler, Controller action, etcetera) you first perform all impure actions necessary to collect input data for a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, then you call that pure function (which may be composed by many smaller functions), and finally you perform one or more impure actions based on the function's return value. That's the <a href="/2020/03/02/impureim-sandwich">impure-pure-impure sandwich</a>. </p> <p> My experience with this pattern is that it's surprisingly often possible to apply it. Not always, but more often than you think. </p> <p> Sometimes, however, it demands a looser interpretation of the word <em>sandwich</em>. </p> <p> Even the examples from <a href="/2020/03/02/impureim-sandwich">the article</a> aren't standard sandwiches, once you dissect them. Consider, first, the <a href="https://www.haskell.org/">Haskell</a> example, here recoloured: </p> <p> <pre><span style="color:#600277;">tryAcceptComposition</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;(Maybe&nbsp;Int) tryAcceptComposition&nbsp;reservation&nbsp;<span style="color:#666666;">=</span>&nbsp;runMaybeT&nbsp;<span style="color:#666666;">$</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;liftIO&nbsp;(<span style="color:#dd0000;">DB</span><span style="color:#666666;">.</span>readReservations&nbsp;connectionString</span><span style="background-color: palegreen;">&nbsp;<span style="color:#666666;">$</span>&nbsp;date&nbsp;reservation</span><span style="background-color: lightsalmon;">)</span> <span style="background-color: palegreen;">&nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;<span style="color:#dd0000;">MaybeT</span>&nbsp;<span style="color:#666666;">.</span>&nbsp;return&nbsp;<span style="color:#666666;">.</span>&nbsp;flip&nbsp;(tryAccept&nbsp;<span style="color:#09885a;">10</span>)&nbsp;reservation</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;liftIO&nbsp;<span style="color:#666666;">.</span>&nbsp;<span style="color:#dd0000;">DB</span><span style="color:#666666;">.</span>createReservation&nbsp;connectionString</span></pre> </p> <p> The <code>date</code> function is a pure accessor that retrieves the date and time of the <code>reservation</code>. In C#, it's typically a read-only property: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(</span><span style="background-color: palegreen;">reservation.Date</span><span style="background-color: lightsalmon;">)</span> <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(rs&nbsp;=&gt;&nbsp;maîtreD.TryAccept(rs,&nbsp;reservation))</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(m&nbsp;=&gt;&nbsp;m.Traverse(Repository.Create)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>),&nbsp;Ok);</span> }</pre> </p> <p> Perhaps you don't think of a C# property as a function. After all, it's just an idiomatic grouping of language keywords: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;DateTimeOffset&nbsp;Date&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}</pre> </p> <p> Besides, a function takes input and returns output. What's the input in this case? </p> <p> Keep in mind that a C# read-only property like this is only syntactic sugar for a getter method. In Java it would have been a method called <code>getDate()</code>. From <a href="/2018/01/22/function-isomorphisms">Function isomorphisms</a> we know that an instance method is isomorphic to a function that takes the object as input: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;DateTimeOffset&nbsp;GetDate(Reservation&nbsp;reservation)</pre> </p> <p> In other words, the <code>Date</code> property is an operation that takes the object itself as input and returns <code>DateTimeOffset</code> as output. The operation has no side effects, and will always return the same output for the same input. In other words, it's a pure function, and that's the reason I've now coloured it green in the above code examples. </p> <p> The layering indicated by the examples may, however, be deceiving. The green colour of <code>reservation.Date</code> is adjacent to the green colour of the <code>Select</code> expression below it. You might interpret this as though the pure middle part of the sandwich partially expands to the upper impure phase. </p> <p> That's not the case. The <code>reservation.Date</code> expression executes <em>before</em> <code>Repository.ReadReservations</code>, and only then does the pure <code>Select</code> expression execute. Perhaps this, then, is a more honest depiction of the sandwich: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;IActionResult&gt;&nbsp;Post(Reservation&nbsp;reservation) { <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;date&nbsp;=&nbsp;reservation.Date;</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(date)</span> <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(rs&nbsp;=&gt;&nbsp;maîtreD.TryAccept(rs,&nbsp;reservation))</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(m&nbsp;=&gt;&nbsp;m.Traverse(Repository.Create)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>),&nbsp;Ok);</span> }</pre> </p> <p> The corresponding 'sandwich diagram' looks like this: </p> <p> <img src="/content/binary/pure-impure-pure-impure-box.png" alt="A box with green, red, green, and red horizontal tiers."> </p> <p> If you want to interpret the word <em>sandwich</em> narrowly, this is no longer a sandwich, since there's 'content' on top. That's the reason I started this article discussing Danish <em>smørrebrød</em>, also sometimes called <em>open-faced sandwiches</em>. Granted, I've never seen a <em>håndmad</em> with two slices of bread with meat both between and on top. On the other hand, I don't think that having a smidgen of 'content' on top is a showstopper. </p> <h3 id="c3a4d1243ee540af95571141c0dd500e"> Initial and eventual purity <a href="#c3a4d1243ee540af95571141c0dd500e">#</a> </h3> <p> Why is this important? Whether or not <code>reservation.Date</code> is a little light of purity in the otherwise impure first slice of the sandwich actually doesn't concern me that much. After all, my concern is mostly cognitive load, and there's hardly much gained by extracting the <code>reservation.Date</code> expression to a separate line, as I did above. </p> <p> The reason this interests me is that in many cases, the first step you may take is to validate input, and <a href="/2023/06/26/validation-and-business-rules">validation is a composed set of pure functions</a>. While pure, and <a href="/2020/12/14/validation-a-solved-problem">a solved problem</a>, validation may be a sufficiently significant step that it warrants explicit acknowledgement. It's not just a property getter, but complex enough that bugs could hide there. </p> <p> Even if you follow the <em>functional core, imperative shell</em> architecture, you'll often find that the first step is pure validation. </p> <p> Likewise, once you've performed impure actions in the second impure phase, you can easily have a final thin pure translation slice. In fact, the above C# example contains an example of just that: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IActionResult&nbsp;Ok(<span style="color:blue;">int</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;OkActionResult(value); } <span style="color:blue;">public</span>&nbsp;IActionResult&nbsp;InternalServerError(<span style="color:blue;">string</span>&nbsp;msg) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;InternalServerErrorActionResult(msg); }</pre> </p> <p> These are two tiny pure functions used as the final translation in the sandwich: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;IActionResult&gt;&nbsp;Post(Reservation&nbsp;reservation) { <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;date&nbsp;=&nbsp;reservation.Date;</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(date)</span> <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(rs&nbsp;=&gt;&nbsp;maîtreD.TryAccept(rs,&nbsp;reservation))</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(m&nbsp;=&gt;&nbsp;m.Traverse(Repository.Create)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match(</span><span style="background-color: palegreen;">InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>),&nbsp;Ok</span><span style="background-color: lightsalmon;">);</span></span> }</pre> </p> <p> On the other hand, I didn't want to paint the <code>Match</code> operation green, since it's essentially a continuation of a <a href="https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1">Task</a>, and if we consider <a href="/2020/07/27/task-asynchronous-programming-as-an-io-surrogate">task asynchronous programming as an IO surrogate</a>, we should, at least, regard it with scepticism. While it might be pure, it probably isn't. </p> <p> Still, we may be left with an inverted 'sandwich' that looks like this: </p> <p> <img src="/content/binary/pure-impure-pure-impure-pure-box.png" alt="A box with green, red, green, red, and green horizontal tiers."> </p> <p> Can we still claim that this is a sandwich? </p> <h3 id="4d14e6795066473e95d1e5cdbcef6c2d"> At the metaphor's limits <a href="#4d14e6795066473e95d1e5cdbcef6c2d">#</a> </h3> <p> This latest development seems to strain the sandwich metaphor. Can we maintain it, or does it fall apart? </p> <p> What seems clear to me, at least, is that this ought to be the limit of how much we can stretch the allegory. If we add more tiers we get a <a href="https://en.wikipedia.org/wiki/Dagwood_sandwich">Dagwood sandwich</a> which is clearly a gimmick of little practicality. </p> <p> But again, I'm appealing to a dubious metaphor, so instead, let's analyse what's going on. </p> <p> In practice, it seems that you can rarely avoid the initial (pure) validation step. Why not? Couldn't you move validation to the functional core and do the impure steps without validation? </p> <p> The short answer is <em>no</em>, because <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">validation done right is actually parsing</a>. At the entry point, you don't even know if the input makes sense. </p> <p> A more realistic example is warranted, so I now turn to the example code base from my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. One blog post shows <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">how to implement applicative validation for posting a reservation</a>. </p> <p> A typical HTTP <code>POST</code> may include a JSON document like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;bf4e84130dac451b9c94049da8ea8c17&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2024-11-07T20:30&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;snomob@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Snow&nbsp;Moe&nbsp;Beal&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 }</pre> </p> <p> In order to handle even such a simple request, the system has to perform a set of impure actions. One of them is to query its data store for existing reservations. After all, the restaurant may not have any remaining tables for that day. </p> <p> Which day, you ask? I'm glad you asked. The data access API comes with this method: </p> <p> <pre>Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;DateTime&nbsp;min,&nbsp;DateTime&nbsp;max);</pre> </p> <p> You can supply <code>min</code> and <code>max</code> values to indicate the range of dates you need. How do you determine that range? You need the desired date of the reservation. In the above example it's 20:30 on November 7 2024. We're in luck, the data is there, and understandable. </p> <p> Notice, however, that due to limitations of wire formats such as JSON, the date is a string. The value might be anything. If it's sufficiently malformed, you can't even perform the impure action of querying the database, because you don't know what to query it about. </p> <p> If keeping the sandwich metaphor untarnished, you might decide to push the parsing responsibility to an impure action, but why make something impure that has a well-known pure solution? </p> <p> A similar argument applies when performing a final, pure translation step in the other direction. </p> <p> So it seems that we're stuck with implementations that don't quite fit the ideal of the sandwich metaphor. Is that enough to abandon the metaphor, or should we keep it? </p> <p> The layers in layered application architecture aren't really layers, and neither are vertical slices really slices. <a href="https://en.wikipedia.org/wiki/All_models_are_wrong">All models are wrong, but some are useful</a>. This is the case here, I believe. You should still keep the <a href="/2020/03/02/impureim-sandwich">Impureim sandwich</a> in mind when structuring code: Keep impure actions at the application boundary - in the 'Controllers', if you will; have only two phases of impurity - the initial and the ultimate; and maximise use of pure functions for everything else. Keep most of the pure execution between the two impure phases, but realistically, you're going to need a pure validation phase in front, and a slim translation layer at the end. </p> <h3 id="5b191dfc434149bab1d9d6bea029a4d4"> Conclusion <a href="#5b191dfc434149bab1d9d6bea029a4d4">#</a> </h3> <p> Despite the prevalence of food imagery, this article about functional programming architecture has eluded any mention of <a href="https://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/">burritos</a>. Instead, it examines the tension between an ideal, the <a href="/2020/03/02/impureim-sandwich">Impureim sandwich</a>, with real-world implementation details. When you have to deal with concerns such as input validation or translation to egress data, it's practical to add one or two more thin slices of purity. </p> <p> In <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a> you want to maximise the proportion of pure functions. Adding more pure code is hardly a problem. </p> <p> The opposite is not the case. We shouldn't be cavalier about adding more impure slices to the sandwich. Thus, the adjusted definition of the Impureim sandwich seems to be that it may have at most two impure phases, but from one to three pure slices. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="aa4031dbe9a7467ba087c2731596f420"> <div class="comment-author">qfilip <a href="#aa4031dbe9a7467ba087c2731596f420">#</a></div> <div class="comment-content"> <p> Hello again... </p> <p> In one of your excellent talks (<a href="https://youtu.be/F9bznonKc64?feature=shared&t=3392">here</a>), you ended up refactoring maitreD kata using the <pre>traverse</pre> function. Since this step is crucial for "sandwich" to work, any post detailing it's implementation would be nice. </p> <p> Thanks </p> </div> <div class="comment-date">2023-11-16 10:56 UTC</div> </div> <div class="comment" id="7ea7f0f5f3a24a939be3a1cb5b23e2f5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7ea7f0f5f3a24a939be3a1cb5b23e2f5">#</a></div> <div class="comment-content"> <p> qfilip, thank you for writing. That particular talk fortunately comes with a set of companion articles: </p> <ul> <li><a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">How to get the value out of the monad</a></li> <li><a href="/2019/02/11/asynchronous-injection">Asynchronous Injection</a></li> </ul> <p> The latter of the two comes with a link to <a href="https://github.com/ploeh/asynchronous-injection">a GitHub repository with all the sample code</a>, including the <code>Traverse</code> implementation. </p> <p> That said, a more formal description of traversals has long been on my to-do list, as you can infer from <a href="/2022/07/11/functor-relationships">this (currently inactive) table of contents</a>. </p> </div> <div class="comment-date">2023-11-16 11:18 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency Whac-A-Mole https://blog.ploeh.dk/2023/10/02/dependency-whac-a-mole 2023-10-02T07:52:00+00:00 Mark Seemann <div id="post"> <p> <em>AKA Framework Whac-A-Mole, Library Whac-A-Mole.</em> </p> <p> I have now three times used the name <a href="https://en.wikipedia.org/wiki/Whac-A-Mole">Whac-A-Mole</a> about a particular kind of relationship that may evolve with some dependencies. According to the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">rule of three</a>, I can now extract the explanation to a separate article. This is that article. </p> <h3 id="f9a98473c3ed40eda1f6288eec631795"> Architecture smell <a href="#f9a98473c3ed40eda1f6288eec631795">#</a> </h3> <p> <em>Dependency Whac-A-Mole</em> describes the situation when you're spending too much time investigating, learning, troubleshooting, and overall satisfying the needs of a dependency (i.e. library or framework) instead of delivering value to users. </p> <p> Examples include Dependency Injection containers, <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping">object-relational mappers</a>, validation frameworks, dynamic mock libraries, and perhaps the Gherkin language. </p> <p> From the above list it does <em>not</em> follow that those examples are universally bad. I can think of situations where some of them make sense. I might even use them myself. </p> <p> Rather, the Dependency Whac-A-Mole architecture smell occurs when a given dependency causes more trouble than the benefit it was supposed to provide. </p> <h3 id="9ae83d04788d4d4c9582ba02aa11b19b"> Causes <a href="#9ae83d04788d4d4c9582ba02aa11b19b">#</a> </h3> <p> We rarely set out to do the wrong thing, but we often make mistakes in good faith. You may decide to take a dependency on a library or framework because </p> <ul> <li>it worked well for you in a previous context</li> <li>it looks as though it'll address a major problem you had in a previous context</li> <li>you've heard good things about it</li> <li>you saw a convincing demo</li> <li>you heard about it in a podcast, conference talk, YouTube video, etc.</li> <li>a FAANG company uses it</li> <li>it's the latest tech</li> <li>you want it on your CV</li> </ul> <p> There could be other motivations as well, and granted, some of those I listed aren't really <em>good</em> reasons. Even so, I don't think anyone chooses a dependency with ill intent. </p> <p> And what might work in one context may turn out to not work in another. You can't always predict such consequences, so I imply no judgement on those who choose the 'wrong' dependency. I've done it, too. </p> <p> It is, however, important to be aware that this risk is always there. You picked a library with the best of intentions, but it turns out to slow you down. If so, acknowledge the mistake and kill your darlings. </p> <h3 id="02aa21e2bdc645f1b769c5a8412323f9"> Background <a href="#02aa21e2bdc645f1b769c5a8412323f9">#</a> </h3> <p> Whenever you use a library or framework, you need to learn how to use it effectively. You have to learn its concepts, abstractions, APIs, pitfalls, etc. Not only that, but you need to stay abreast of changes and improvements. </p> <p> Microsoft, for example, is usually good at maintaining backwards compatibility, but even so, things don't stand still. They evolve libraries and frameworks the same way I would do it: Don't introduce breaking changes, but do introduce new, better APIs going forward. This is essentially the <a href=https://martinfowler.com/bliki/StranglerFigApplication.html>Strangler pattern</a> that I also write about in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> While it's a good way to evolve a library or framework, the point remains: Even if you trust a supplier to prioritise backwards compatibility, it doesn't mean that you can stop learning. You have to stay up to date with all your dependencies. If you don't, sooner or later, the way that you use something like, say, <a href="https://en.wikipedia.org/wiki/Entity_Framework">Entity Framework</a> is 'the old way', and it's not really supported any longer. </p> <p> In order to be able to move forward, you'll have to rewrite those parts of your code that depend on that old way of doing things. </p> <p> Each dependency comes with benefits and costs. As long as the benefits outweigh the costs, it makes sense to keep it around. If, on the other hand, you spend more time dealing with it than it would take you to do the work yourself, consider getting rid of it. </p> <h3 id="439ea4466014446a9ddfc2e264c86fba"> Symptoms <a href="#439ea4466014446a9ddfc2e264c86fba">#</a> </h3> <p> Perhaps the infamous <em>left-pad</em> incident is too easy an example, but it does highlight the essence of this tension. Do you really need a third-party package to pad a string, or could you have done it yourself? </p> <p> You can spend much time figuring out how to fit a general-purpose library or framework to your particular needs. How do you make your object-relational mapper (ORM) fit a special database schema? How do you annotate a class so that it produces validation messages according to the requirements in your jurisdiction? How do you configure an automatic mapping library so that it correctly projects data? How do you tell a Dependency Injection (DI) Container how to compose a <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">Chain of Responsibility</a> where some objects also take strings or integers in their constructors? </p> <p> Do such libraries or frameworks save time, or could you have written the corresponding code quicker? To be clear, I'm not talking about writing your own ORM, your own DI Container, your own auto-mapper. Rather, instead of using a DI Container, <a href="/2014/06/10/pure-di">Pure DI</a> is likely easier. As an alternative to an ORM, what's the cost of just writing <a href="https://en.wikipedia.org/wiki/SQL">SQL</a>? Instead of an <a href="https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule">ad-hoc, informally-specified, bug-ridden</a> validation framework, have you considered <a href="/2018/11/05/applicative-validation">applicative validation</a>? </p> <p> Things become really insidious if your chosen library never really solves all problems. Every time you figure out how to use it for one exotic corner case, your 'solution' causes a new problem to arise. </p> <p> A symptom of <em>Dependency Whac-A-Mole</em> is when you have to advertise after people skilled in a particular technology. </p> <p> Again, it's not necessarily a problem. If you're getting tremendous value out of, say, Entity Framework, it makes sense to list expertise as a job requirement. If, on the other hand, you have to list a litany of libraries and frameworks as necessary skills, it might pay to stop and reconsider. You can call it your 'tech stack' all you will, but is it really an inadvertent case of <a href="https://en.wikipedia.org/wiki/Vendor_lock-in">vendor lock-in</a>? </p> <h3 id="381db0b94f094be2be2b95841e248669"> Anecdotal evidence <a href="#381db0b94f094be2be2b95841e248669">#</a> </h3> <p> I've used the term <em>Whac-A-Mole</em> a couple of times to describe the kind of situation where you feel that you're fighting a technology more than it's helping you. It seems to resonate with other people than me. </p> <p> Here are the original articles where I used the term: </p> <ul> <li><a href="/2022/08/15/aspnet-validation-revisited">ASP.NET validation revisited</a></li> <li><a href="/2022/08/22/can-types-replace-validation">Can types replace validation?</a></li> <li><a href="/2023/09/18/do-orms-reduce-the-need-for-mapping">Do ORMs reduce the need for mapping?</a></li> </ul> <p> These are only the articles where I explicitly use the term. I do, however, think that the phenomenon is more common. I'm particularly sensitive to it when it comes to Dependency Injection, where I generally believe that DI Containers make the technique harder that it has to be. Composing object graphs is easily done with code. </p> <h3 id="2ef657b607cd49408ced7110e28e2321"> Conclusion <a href="#2ef657b607cd49408ced7110e28e2321">#</a> </h3> <p> Sometimes a framework or library makes it more difficult to get things done. You spend much time kowtowing to its needs, researching how to do things 'the xyz way', learning its intricate extensibility points, keeping up to date with its evolving API, and engaging with its community to lobby for new features. </p> <p> Still, you feel that it makes you compromise. You might have liked to organise your code in a different way, but unfortunately you can't, because it doesn't fit the way the dependency works. As you solve issues with it, new ones appear. </p> <p> These are symptoms of <em>Dependency Whac-A-Mole</em>, an architecture smell that indicates that you're using the wrong tool for the job. If so, get rid of the dependency in favour of something better. Often, the better alternative is just plain vanilla code. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9235995516070545f7cc3ee83d37023d"> <div class="comment-author"><a href="https://github.com/thomaslevesque">Thomas Levesque</a> <a href="#9235995516070545f7cc3ee83d37023d">#</a></div> <div class="comment-content"> <p> The most obvious example of this for me is definitely AutoMapper. I used to think it was great and saved so much time, but more often than not, the mapping configuration ended up being more complex (and fragile) than just mapping the properties manually. </p> </div> <div class="comment-date">2023-10-02 13:27 UTC</div> </div> <div class="comment" id="93b32bb03ee14d298b0d9b7cf65ddcae"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#93b32bb03ee14d298b0d9b7cf65ddcae">#</a></div> <div class="comment-content"> <p> I could imagine. AutoMapper is not, however, a library I've used enough to evaluate. </p> </div> <div class="comment-date">2023-10-02 13:58 UTC</div> </div> <div class="comment" id="3e81ff9e535743148d8898e84ff69595"> <div class="comment-author"><a href="https://blog.oakular.xyz">Callum Warrilow</a> <a href="#3e81ff9e535743148d8898e84ff69595">#</a></div> <div class="comment-content"> <p> The moment I lost any faith in AutoMapper was after trying to debug a mapping that was silently failing on a single property. Three of us were looking at it for a good amount of time before one of us noticed a single character typo on the destination property. As the names did not match, no mapping occurred. It is unfortunately a black box, and obfuscated a problem that a manual mapping would have handled gracefully. <hr /> Mark, it is interesting that you mention Gherkin as potentially one of these moles. It is something I've been evaluating in the hopes of making our tests more business focused, but considering it again now, you can achieve a lot of what Gherkin offers with well defined namespaces, classes and methods in your test assemblies, something like: <ul> <li>Namespace: GivenSomePrecondition</li> <li>TestClass: WhenCarryingOutAnAction</li> <li>TestMethod: ThenTheExpectedPostConditionResults</li> </ul> To get away from playing Whac-a-Mole, it would seem to require changing the question being asked, from <i>what product do I need to solve this problem?</i>, to <i>what tools and patterns can do I have around me to solve this problem?</i>. </p> </div> <div class="comment-date">2023-10-11 15:54 UTC</div> </div> <div class="comment" id="eef76159a60b4ee482238b1cd990ab94"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#eef76159a60b4ee482238b1cd990ab94">#</a></div> <div class="comment-content"> <p> Callum, I was expecting someone to comment on including Gherkin on the list. </p> <p> I don't consider all my examples as universally problematic. Rather, they often pop up in contexts where people seem to be struggling with a concept or a piece of technology with no apparent benefit. </p> <p> I'm sure that when <a href="https://dannorth.net/">Dan North</a> came up with the idea of BDD and Gherkin, he actually <em>used</em> it. When used in the way it was originally intended, I can see it providing value. </p> <p> Apart from Dan himself, however, I'm not aware that I've ever met anyone who has used BDD and Gherkin in that way. On the contrary, I've had more than one discussion that went like this: </p> <p> <em>Interlocutor:</em> "We use BDD and Gherkin. It's great! You should try it." </p> <p> <em>Me:</em> "Why?" </p> <p> <em>Interlocutor:</em> "It enables us to <em>organise</em> our tests." </p> <p> <em>Me:</em> "Can't you do that with the <a href="https://wiki.c2.com/?ArrangeActAssert">AAA</a> pattern?" </p> <p> <em>Interlocutor:</em> "..." </p> <p> <em>Me:</em> "Do any non-programmers ever look at your tests?" </p> <p> <em>Interlocutor:</em> "No..." </p> <p> If only programmers look at the test code, then why impose an artificial constraint? <em>Given-when-then</em> is just <em>arrange-act-assert</em> with different names, but free of Gherkin and the tooling that typically comes with it, you're free to write test code that follows normal good coding practices. </p> <p> (As an aside, yes: Sometimes <a href="https://www.dotnetrocks.com/?show=1542">constraints liberate</a>, but what I've seen of Gherkin-based test code, this doesn't seem to be one of those cases.) </p> <p> Finally, to be quite clear, although I may be repeating myself: If you're using Gherkin to interact with non-programmers on a regular basis, it may be beneficial. I've just never been in that situation, or met anyone other than Dan North who have. </p> </div> <div class="comment-date">2023-10-15 14:35 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The case of the mysterious comparison https://blog.ploeh.dk/2023/09/25/the-case-of-the-mysterious-comparison 2023-09-25T05:58:00+00:00 Mark Seemann <div id="post"> <p> <em>A ploeh mystery.</em> </p> <p> I was <a href="/2023/09/18/do-orms-reduce-the-need-for-mapping">recently playing around</a> with the example code from my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, refactoring the <code>Table</code> class to use <a href="/2022/08/22/can-types-replace-validation">a predicative NaturalNumber wrapper</a> to represent a table's seating capacity. </p> <p> Originally, the <code>Table</code> constructor and corresponding read-only data looked like this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;isStandard; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Reservation[]&nbsp;reservations; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} <span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Table</span>(<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">isStandard</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>,&nbsp;<span style="color:blue;">params</span>&nbsp;Reservation[]&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.isStandard&nbsp;=&nbsp;isStandard; &nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.reservations&nbsp;=&nbsp;reservations; }</pre> </p> <p> Since I wanted to show an example of how wrapper types can help make preconditions explicit, I changed it to this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;isStandard; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Reservation[]&nbsp;reservations; <span style="color:blue;">public</span>&nbsp;NaturalNumber&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} <span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Table</span>(<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">isStandard</span>,&nbsp;NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>,&nbsp;<span style="color:blue;">params</span>&nbsp;Reservation[]&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.isStandard&nbsp;=&nbsp;isStandard; &nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.reservations&nbsp;=&nbsp;reservations; }</pre> </p> <p> The only thing I changed was the type of <code>Capacity</code> and <code>capacity</code>. </p> <p> As I did that, two tests failed. </p> <h3 id="5942663d531c41c491e2b79116008c5e"> Evidence <a href="#5942663d531c41c491e2b79116008c5e">#</a> </h3> <p> Both tests failed in the same way, so I only show one of the failures: </p> <p> <pre>Ploeh.Samples.Restaurants.RestApi.Tests.MaitreDScheduleTests.Schedule &nbsp;&nbsp;Source: MaitreDScheduleTests.cs line 16 &nbsp;&nbsp;Duration: 340 ms &nbsp;&nbsp;Message: &nbsp;&nbsp;&nbsp;&nbsp;FsCheck.Xunit.PropertyFailedException : &nbsp;&nbsp;&nbsp;&nbsp;Falsifiable, after 2 tests (0 shrinks) (StdGen (48558275,297233133)): &nbsp;&nbsp;&nbsp;&nbsp;Original: &nbsp;&nbsp;&nbsp;&nbsp;&lt;null&gt; &nbsp;&nbsp;&nbsp;&nbsp;(Ploeh.Samples.Restaurants.RestApi.MaitreD, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[|Ploeh.Samples.Restaurants.RestApi.Reservation|]) &nbsp;&nbsp;&nbsp;&nbsp;---- System.InvalidOperationException : Failed to compare two elements in the array. &nbsp;&nbsp;&nbsp;&nbsp;-------- System.ArgumentException : At least one object must implement IComparable. &nbsp;&nbsp;Stack Trace: &nbsp;&nbsp;&nbsp;&nbsp;----- Inner Stack Trace ----- &nbsp;&nbsp;&nbsp;&nbsp;GenericArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer) &nbsp;&nbsp;&nbsp;&nbsp;Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer) &nbsp;&nbsp;&nbsp;&nbsp;EnumerableSorter`2.QuickSort(Int32[] keys, Int32 lo, Int32 hi) &nbsp;&nbsp;&nbsp;&nbsp;EnumerableSorter`1.Sort(TElement[] elements, Int32 count) &nbsp;&nbsp;&nbsp;&nbsp;OrderedEnumerable`1.ToList() &nbsp;&nbsp;&nbsp;&nbsp;Enumerable.ToList[TSource](IEnumerable`1 source) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red;">MaitreD.Allocate(IEnumerable`1 reservations)</span> line 91 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red;">&lt;&gt;c__DisplayClass21_0.&lt;Schedule&gt;b__4(&lt;&gt;f__AnonymousType7`2 &lt;&gt;h__TransparentIdentifier1)</span> line 114 &nbsp;&nbsp;&nbsp;&nbsp;&lt;&gt;c__DisplayClass2_0`3.&lt;CombineSelectors&gt;b__0(TSource x) &nbsp;&nbsp;&nbsp;&nbsp;SelectIPartitionIterator`2.GetCount(Boolean onlyIfCheap) &nbsp;&nbsp;&nbsp;&nbsp;Enumerable.Count[TSource](IEnumerable`1 source) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red;">MaitreDScheduleTests.ScheduleImp(MaitreD sut, Reservation[] reservations)</span> line 31 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red;">&lt;&gt;c.&lt;Schedule&gt;b__0_2(ValueTuple`2 t)</span> line 22 &nbsp;&nbsp;&nbsp;&nbsp;ForAll@15.Invoke(Value arg00) &nbsp;&nbsp;&nbsp;&nbsp;Testable.evaluate[a,b](FSharpFunc`2 body, a a) &nbsp;&nbsp;&nbsp;&nbsp;----- Inner Stack Trace ----- &nbsp;&nbsp;&nbsp;&nbsp;Comparer.Compare(Object a, Object b) &nbsp;&nbsp;&nbsp;&nbsp;ObjectComparer`1.Compare(T x, T y) &nbsp;&nbsp;&nbsp;&nbsp;EnumerableSorter`2.CompareAnyKeys(Int32 index1, Int32 index2) &nbsp;&nbsp;&nbsp;&nbsp;ComparisonComparer`1.Compare(T x, T y) &nbsp;&nbsp;&nbsp;&nbsp;ArraySortHelper`1.SwapIfGreater(T[] keys, Comparison`1 comparer, Int32 a, Int32 b) &nbsp;&nbsp;&nbsp;&nbsp;ArraySortHelper`1.IntroSort(T[] keys, Int32 lo, Int32 hi, Int32 depthLimit, Comparison`1 comparer) &nbsp;&nbsp;&nbsp;&nbsp;GenericArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)</pre> </p> <p> The code highlighted with red is user code (i.e. my code). The rest comes from .NET or <a href="https://fscheck.github.io/FsCheck/">FsCheck</a>. </p> <p> While a stack trace like that can look intimidating, I usually navigate to the top stack frame of my own code. As I reproduce my investigation, see if you can spot the problem before I did. </p> <h3 id="fded951b0b3a4cac848941153e84eaa6"> Understand before resolving <a href="#fded951b0b3a4cac848941153e84eaa6">#</a> </h3> <p> Before starting the investigation proper, we might as well acknowledge what seems evident. I had a fully passing test suite, then I edited two lines of code, which caused the above error. The two nested exception messages contain obvious clues: <em>Failed to compare two elements in the array,</em> and <em>At least one object must implement IComparable</em>. </p> <p> The only edit I made was to change an <code>int</code> to a <code>NaturalNumber</code>, and <code>NaturalNumber</code> didn't implement <code>IComparable</code>. It seems straightforward to just make <code>NaturalNumber</code> implement that interface and move on, and as it turns out, that <em>is</em> the solution. </p> <p> As I describe in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, when troubleshooting, first seek to understand the problem. I've seen too many people go immediately into 'action mode' when faced with a problem. It's often a suboptimal strategy. </p> <p> First, if the immediate solution turns out not to work, you can waste much time trashing, trying various 'fixes' without understanding the problem. </p> <p> Second, even if the resolution is easy, as is the case here, if you don't understand the underlying cause and effect, you can easily build a <a href="https://en.wikipedia.org/wiki/Cargo_cult">cargo cult</a>-like 'understanding' of programming. This could become one such experience: <em>All wrapper types must implement <code>IComparable</code></em>, or some nonsense like that. </p> <p> Unless people are getting hurt or you are bleeding money because of the error, seek first to understand, and only then fix the problem. </p> <h3 id="f881a7f048144dd2a2521e336675d052"> First clue <a href="#f881a7f048144dd2a2521e336675d052">#</a> </h3> <p> The top user stack frame is the <code>Allocate</code> method: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;IEnumerable&lt;Table&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Allocate</span>( &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Table&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">allocation</span>&nbsp;=&nbsp;Tables.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;reservations) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">table</span>&nbsp;=&nbsp;allocation.Find(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Fits(r.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(table&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;allocation.Remove(table); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;allocation.Add(table.Reserve(r)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;allocation; }</pre> </p> <p> The stack trace points to line 91, which is the first line of code; where it calls <code>Tables.ToList()</code>. This is also consistent with the stack trace, which indicates that the exception is thrown from <a href="https://learn.microsoft.com/dotnet/api/system.linq.enumerable.tolist">ToList</a>. </p> <p> I am, however, not used to <code>ToList</code> throwing exceptions, so I admit that I was nonplussed. Why would <code>ToList</code> try to sort the input? It usually doesn't do that. </p> <p> Now, I <em>did</em> notice the <code>OrderedEnumerable`1</code> on the stack frame above <code>Enumerable.ToList</code>, but this early in the investigation, I failed to connect the dots. </p> <p> What does the caller look like? It's that scary <code>DisplayClass21</code>... </p> <h3 id="5250f81716324ff1918bea2e57d08ef4"> Immediate caller <a href="#5250f81716324ff1918bea2e57d08ef4">#</a> </h3> <p> The code that calls <code>Allocate</code> is the <code>Schedule</code> method, the System Under Test: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IEnumerable&lt;TimeSlot&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Schedule</span>( &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;r&nbsp;<span style="color:blue;">by</span>&nbsp;r.At&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">orderby</span>&nbsp;g.Key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;seating&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Seating(SeatingDuration,&nbsp;g.Key) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;overlapping&nbsp;=&nbsp;reservations.Where(seating.Overlaps) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSlot(g.Key,&nbsp;Allocate(overlapping).ToList()); }</pre> </p> <p> While it does <code>orderby</code>, it doesn't seem to be sorting the input to <code>Allocate</code>. While <code>overlapping</code> is a filtered subset of <code>reservations</code>, the code doesn't sort <code>reservations</code>. </p> <p> Okay, moving on, what does the caller of that method look like? </p> <h3 id="0db463ec75d64f93a1b188af9fe731f3"> Test implementation <a href="#0db463ec75d64f93a1b188af9fe731f3">#</a> </h3> <p> The caller of the <code>Schedule</code> method is this test implementation: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ScheduleImp</span>( &nbsp;&nbsp;&nbsp;&nbsp;MaitreD&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>, &nbsp;&nbsp;&nbsp;&nbsp;Reservation[]&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.Schedule(reservations); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservations.Select(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.At).Distinct().Count(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual.Count()); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual.Select(<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;ts.At).OrderBy(<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;d), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual.Select(<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;ts.At)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.All(actual,&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;AssertTables(sut.Tables,&nbsp;ts.Tables)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.All( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;AssertRelevance(reservations,&nbsp;sut.SeatingDuration,&nbsp;ts)); }</pre> </p> <p> Notice how the first line of code calls <code>Schedule</code>, while the rest is 'just' assertions. </p> <p> Because I had noticed that <code>OrderedEnumerable`1</code> on the stack, I was on the lookout for an expression that would sort an <code>IEnumerable&lt;T&gt;</code>. The <code>ScheduleImp</code> method surprised me, though, because the <code>reservations</code> parameter is an array. If there was any problem sorting it, it should have blown up much earlier. </p> <p> I really should be paying more attention, but despite my best resolution to proceed methodically, I was chasing the wrong clue. </p> <p> Which line of code throws the exception? The stack trace says line 31. That's not the <code>sut.Schedule(reservations)</code> call. It's the first assertion following it. I failed to notice that. </p> <h3 id="5116edf953f1438b9cb4b37c5b043bda"> Property <a href="#5116edf953f1438b9cb4b37c5b043bda">#</a> </h3> <p> I was stumped, and not knowing what to do, I looked at the fourth and final piece of user code in that stack trace: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;Property&nbsp;<span style="font-weight:bold;color:#74531f;">Schedule</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;rs&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;&nbsp;m&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.MaitreD(rs) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(m,&nbsp;rs)).ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;ScheduleImp(t.m,&nbsp;t.rs)); }</pre> </p> <p> No sorting there. What's going on? </p> <p> In retrospect, I'm struggling to understand what was going on in my mind. Perhaps you're about to lose patience with me. I was chasing the wrong 'clue', just as I said above that 'other' people do, but surely, it's understood, that I don't. </p> <h3 id="70ad2e90704d4d31bc0d045fff16a011"> WYSIATI <a href="#70ad2e90704d4d31bc0d045fff16a011">#</a> </h3> <p> In <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> I spend some time discussing how code relates to human cognition. I'm no neuroscientist, but I try to read books on other topics than programming. I was partially inspired by <a href="/ref/thinking-fast-and-slow">Thinking, Fast and Slow</a> in which <a href="https://en.wikipedia.org/wiki/Daniel_Kahneman">Daniel Kahneman</a> (among many other topics) presents how <em>System 1</em> (the inaccurate <em>fast</em> thinking process) mostly works with what's right in front of it: <em>What You See Is All There Is</em>, or WYSIATI. </p> <p> That <code>OrderedEnumerable`1</code> in the stack trace had made me look for an <code>IEnumerable&lt;T&gt;</code> as the culprit, and in the source code of the <code>Allocate</code> method, one parameter is clearly what I was looking for. I'll repeat that code here for your benefit: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;IEnumerable&lt;Table&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">Allocate</span>( &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Table&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">allocation</span>&nbsp;=&nbsp;Tables.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;reservations) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">table</span>&nbsp;=&nbsp;allocation.Find(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Fits(r.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(table&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;allocation.Remove(table); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;allocation.Add(table.Reserve(r)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;allocation; }</pre> </p> <p> Where's the <code>IEnumerable&lt;T&gt;</code> in that code? </p> <p> <code>reservations</code>, right? </p> <h3 id="c99f8e1238284cc88868d5fe39f43f2a"> Revelation <a href="#c99f8e1238284cc88868d5fe39f43f2a">#</a> </h3> <p> As WYSIATI 'predicts', the brain gloms on to what's prominent. I was looking for <code>IEnumerable&lt;T&gt;</code>, and it's right there in the method declaration as the parameter <code>IEnumerable&lt;Reservation&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span></code>. </p> <p> As covered in multiple places (<a href="/code-that-fits-in-your-head">my book</a>, <a href="/ref/programmers-brain">The Programmer's Brain</a>), the human brain has limited short-term memory. Apparently, while chasing the <code>IEnumerable&lt;T&gt;</code> clue, I'd already managed to forget another important datum. </p> <p> Which line of code throws the exception? This one: </p> <p> <pre>List&lt;Table&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">allocation</span>&nbsp;=&nbsp;Tables.ToList();</pre> </p> <p> The <code>IEnumerable&lt;T&gt;</code> isn't <code>reservations</code>, but <code>Tables</code>. </p> <p> While the code doesn't explicitly say <code>IEnumerable&lt;Table&gt;&nbsp;Tables</code>, that's just what it is. </p> <p> Yes, it took me way too long to notice that I'd been barking up the wrong tree all along. Perhaps you immediately noticed that, but have pity with me. I don't think this kind of human error is uncommon. </p> <h3 id="288f57cb1a4648a1926164e64aebfbe2"> The culprit <a href="#288f57cb1a4648a1926164e64aebfbe2">#</a> </h3> <p> Where do <code>Tables</code> come from? It's a read-only property originally injected via the constructor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;TimeOfDay&nbsp;<span style="font-weight:bold;color:#1f377f;">opensAt</span>, &nbsp;&nbsp;&nbsp;&nbsp;TimeOfDay&nbsp;<span style="font-weight:bold;color:#1f377f;">lastSeating</span>, &nbsp;&nbsp;&nbsp;&nbsp;TimeSpan&nbsp;<span style="font-weight:bold;color:#1f377f;">seatingDuration</span>, &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Table&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">tables</span>) { &nbsp;&nbsp;&nbsp;&nbsp;OpensAt&nbsp;=&nbsp;opensAt; &nbsp;&nbsp;&nbsp;&nbsp;LastSeating&nbsp;=&nbsp;lastSeating; &nbsp;&nbsp;&nbsp;&nbsp;SeatingDuration&nbsp;=&nbsp;seatingDuration; &nbsp;&nbsp;&nbsp;&nbsp;Tables&nbsp;=&nbsp;tables; }</pre> </p> <p> Okay, in the test then, where does it come from? That's the <code>m</code> in the above property, repeated here for your convenience: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;Property&nbsp;<span style="font-weight:bold;color:#74531f;">Schedule</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;rs&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;&nbsp;m&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.MaitreD(rs) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(m,&nbsp;rs)).ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;ScheduleImp(t.m,&nbsp;t.rs)); }</pre> </p> <p> The <code>m</code> variable is generated by <code>Gens.MaitreD</code>, so let's follow that clue: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Gen&lt;MaitreD&gt;&nbsp;<span style="color:#74531f;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;seatingDuration&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(1,&nbsp;6) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;tables&nbsp;<span style="color:blue;">in</span>&nbsp;Tables(reservations) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;MaitreD( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromHours(18), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromHours(21), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromHours(seatingDuration), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tables); }</pre> </p> <p> We're not there yet, but close. The <code>tables</code> variable is generated by this <code>Tables</code> helper function: </p> <p> <pre><span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">summary</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;Generate&nbsp;a&nbsp;table&nbsp;configuration&nbsp;that&nbsp;can&nbsp;at&nbsp;minimum&nbsp;accomodate&nbsp;all</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;reservations.</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;/</span><span style="color:gray;">summary</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">param</span>&nbsp;<span style="color:gray;">name</span><span style="color:gray;">=</span><span style="color:gray;">&quot;</span>reservations<span style="color:gray;">&quot;</span><span style="color:gray;">&gt;</span><span style="color:green;">The&nbsp;reservations&nbsp;to&nbsp;accommodate</span><span style="color:gray;">&lt;/</span><span style="color:gray;">param</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">returns</span><span style="color:gray;">&gt;</span><span style="color:green;">A&nbsp;generator&nbsp;of&nbsp;valid&nbsp;table&nbsp;configurations.</span><span style="color:gray;">&lt;/</span><span style="color:gray;">returns</span><span style="color:gray;">&gt;</span> <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Gen&lt;IEnumerable&lt;Table&gt;&gt;&nbsp;<span style="color:#74531f;">Tables</span>( &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Create&nbsp;a&nbsp;table&nbsp;for&nbsp;each&nbsp;reservation,&nbsp;to&nbsp;ensure&nbsp;that&nbsp;all</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;reservations&nbsp;can&nbsp;be&nbsp;allotted&nbsp;a&nbsp;table.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">tables</span>&nbsp;=&nbsp;reservations.Select(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;Table.Standard(r.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;moreTables&nbsp;<span style="color:blue;">in</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gen.Choose(1,&nbsp;12).Select( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Table.Standard(<span style="color:blue;">new</span>&nbsp;NaturalNumber(i))).ArrayOf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;allTables&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tables.Concat(moreTables).OrderBy(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Capacity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;allTables.AsEnumerable(); }</pre> </p> <p> And there you have it: <code>OrderBy(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Capacity)</code>! </p> <p> The <code>Capacity</code> property was exactly the property I changed from <code>int</code> to <code>NaturalNumber</code> - the change that made the test fail. </p> <p> As expected, the fix was to let <code>NaturalNumber</code> implement <code>IComparable&lt;NaturalNumber&gt;</code>. </p> <h3 id="534a507621b840dfb566cdb359261840"> Conclusion <a href="#534a507621b840dfb566cdb359261840">#</a> </h3> <p> I thought this little troubleshooting session was interesting enough to write down. I spent perhaps twenty minutes on it before I understood what was going on. Not disastrously long, but enough time that I was relieved when I figured it out. </p> <p> Apart from the obvious (look for the problem where it is), there is one other useful lesson to be learned, I think. </p> <p> <a href="https://learn.microsoft.com/dotnet/standard/linq/deferred-execution-lazy-evaluation">Deferred execution</a> can confuse even the most experienced programmer. It took me some time before it dawned on me that even though the the <code>MaitreD</code> constructor had run and the object was 'safely' initialised, it actually wasn't. </p> <p> The implication is that there's a 'disconnect' between the constructor and the <code>Allocate</code> method. The error actually happens during initialisation (i.e. in the caller of the constructor), but it only manifests when you run the method. </p> <p> Ever since <a href="/2013/07/20/linq-versus-the-lsp">I discovered the IReadOnlyCollection&lt;T&gt; interface in 2013</a> I've resolved to favour it over <code>IEnumerable&lt;T&gt;</code>. This is one example of why that's a good idea. </p> <p> Despite my best intentions, I, too, cut corners from time to time. I've done it here, by accepting <code>IEnumerable&lt;Table&gt;</code> instead of <code>IReadOnlyCollection&lt;Table&gt;</code> as a constructor parameter. I really should have known better, and now I've paid the price. </p> <p> This is particularly ironic because I also love <a href="https://www.haskell.org/">Haskell</a> so much. Haskell is lazy by default, so you'd think that I run into such issues all the time. An expression like <code>OrderBy(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Capacity)</code>, however, wouldn't have compiled in Haskell unless the sort key implemented the <a href="https://hackage.haskell.org/package/base/docs/Data-Ord.html#t:Ord">Ord</a> type class. Even C#'s type system can express that a generic type must implement an interface, but <a href="https://learn.microsoft.com/dotnet/api/system.linq.enumerable.orderby">OrderBy</a> doesn't do that. </p> <p> This problem could have been caught at compile-time, but unfortunately it wasn't. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="7207e4dc0287435facea31fc9ce49d36"> <div class="comment-author"><a href="https://github.com/JesHansen">Jes Hansen</a> <a href="#7207e4dc0287435facea31fc9ce49d36">#</a></div> <div class="comment-content"> <p> I made a <a href="https://github.com/dotnet/runtime/issues/92691">pull request</a> describing the issue. </p> <p> As this is likely a breaking change I don't have high hopes for it to be fixed, though&hellip; </p> </div> <div class="comment-date">2023-09-27 09:40 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Do ORMs reduce the need for mapping? https://blog.ploeh.dk/2023/09/18/do-orms-reduce-the-need-for-mapping 2023-09-18T14:40:00+00:00 Mark Seemann <div id="post"> <p> <em>With some Entity Framework examples in C#.</em> </p> <p> In a recent comment, a reader <a href="/2023/07/17/works-on-most-machines#4012c2cddcb64a068c0b06b7989a676e">asked me to expand on my position</a> on <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping">object-relational mappers</a> (ORMs), which is that I'm not a fan: </p> <blockquote> <p> I consider ORMs a waste of time: they create more problems than they solve. </p> <footer><cite><a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, subsection 12.2.2, footnote</cite></footer> </blockquote> <p> While I acknowledge that only a Sith deals in absolutes, I favour clear assertions over guarded language. I don't really mean it that categorically, but I do stand by the general sentiment. In this article I'll attempt to describe why I don't reach for ORMs when querying or writing to a relational database. </p> <p> As always, any exploration of such a kind is made in a <em>context</em>, and this article is no exception. Before proceeding, allow me to delineate the scope. If your context differs from mine, what I write may not apply to your situation. </p> <h3 id="a29a6dfd90604a358c5e2f8e76941f80"> Scope <a href="#a29a6dfd90604a358c5e2f8e76941f80">#</a> </h3> <p> It's been decades since I last worked on a system where the database 'came first'. The last time that happened, the database was hidden behind an XML-based <a href="https://en.wikipedia.org/wiki/Remote_procedure_call">RPC</a> API that tunnelled through HTTP. Not a <a href="https://en.wikipedia.org/wiki/REST">REST</a> API by a long shot. </p> <p> Since then, I've worked on various systems. Some used relational databases, some document databases, some worked with CSV, or really old legacy APIs, etc. Common to these systems was that they were <em>not</em> designed around a database. Rather, they were developed with an eye to the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>, keeping storage details out of the Domain Model. Many were developed with test-driven development (TDD). </p> <p> When I evaluate whether or not to use an ORM in situations like these, the core application logic is my main design driver. As I describe in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, I usually develop (vertical) feature slices one at a time, utilising an <a href="/outside-in-tdd">outside-in TDD</a> process, during which I also figure out how to save or retrieve data from persistent storage. </p> <p> Thus, in systems like these, storage implementation is an artefact of the software architecture. If a relational database is involved, the schema must adhere to the needs of the code; not the other way around. </p> <p> To be clear, then, this article doesn't discuss typical <a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a>-heavy applications that are mostly forms over relational data, with little or no application logic. If you're working with such a code base, an ORM might be useful. I can't really tell, since I last worked with such systems at a time when ORMs didn't exist. </p> <h3 id="b6446ab3f8b8410da2679b4fb915a69e"> The usual suspects <a href="#b6446ab3f8b8410da2679b4fb915a69e">#</a> </h3> <p> The most common criticism of ORMs (that I've come across) is typically related to the queries they generate. People who are skilled in writing <a href="https://en.wikipedia.org/wiki/SQL">SQL</a> by hand, or who are concerned about performance, may look at the SQL that an ORM generates and dislike it for that reason. </p> <p> It's my impression that ORMs have come a long way over the decades, but frankly, the generated SQL is not really what concerns me. It never was. </p> <p> In the abstract, Ted Neward already outlined the problems in the seminal article <a href="https://blogs.newardassociates.com/blog/2006/the-vietnam-of-computer-science.html">The Vietnam of Computer Science</a>. That problem description may, however, be too theoretical to connect with most programmers, so I'll try a more example-driven angle. </p> <h3 id="6908e1b735ee41068baeeb9482a15953"> Database operations without an ORM <a href="#6908e1b735ee41068baeeb9482a15953">#</a> </h3> <p> Once more I turn to the trusty example code base that accompanies <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. In it, I used <a href="https://en.wikipedia.org/wiki/Microsoft_SQL_Server">SQL Server</a> as the example database, and ADO.NET as the data access technology. </p> <p> I considered this more than adequate for saving and reading restaurant reservations. Here, for example, is the code that creates a new reservation row in the database: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(reservation)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">conn</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlConnection(ConnectionString); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlCommand(createReservationSql,&nbsp;conn); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Id&quot;</span>,&nbsp;reservation.Id); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@RestaurantId&quot;</span>,&nbsp;restaurantId); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@At&quot;</span>,&nbsp;reservation.At); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;reservation.Name.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Email&quot;</span>,&nbsp;reservation.Email.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Quantity&quot;</span>,&nbsp;reservation.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;conn.OpenAsync().ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;cmd.ExecuteNonQueryAsync().ConfigureAwait(<span style="color:blue;">false</span>); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;createReservationSql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;INSERT&nbsp;INTO&nbsp;[dbo].[Reservations]&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[PublicId],&nbsp;[RestaurantId],&nbsp;[At],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity]) &nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(@Id,&nbsp;@RestaurantId,&nbsp;@At,&nbsp;@Name,&nbsp;@Email,&nbsp;@Quantity)&quot;</span>;</pre> </p> <p> Yes, there's mapping, even if it's 'only' from a Domain Object to command parameter strings. As I'll argue later, if there's a way to escape such mapping, I'm not aware of it. ORMs don't seem to solve that problem. </p> <p> This, however, seems to be the reader's main concern: </p> <blockquote> <p> "I can work with raw SQL ofcourse... but the mapping... oh the mapping..." </p> <footer><cite><a href="/2023/07/17/works-on-most-machines#4012c2cddcb64a068c0b06b7989a676e">qfilip</a></cite></footer> </blockquote> <p> It's not a concern that I share, but again I'll remind you that if your context differs substantially from mine, what doesn't concern me could reasonably concern you. </p> <p> You may argue that the above example isn't representative, since it only involves a single table. No foreign key relationships are involved, so perhaps the example is artificially easy. </p> <p> In order to work with a slightly more complex schema, I decided to port the read-only in-memory restaurant database (the one that keeps track of the restaurants - the <em>tenants</em> - of the system) to SQL Server. </p> <h3 id="ef3d04206a20442dbd2c01336c48fd28"> Restaurants schema <a href="#ef3d04206a20442dbd2c01336c48fd28">#</a> </h3> <p> In the book's sample code base, I'd only stored restaurant configurations as JSON config files, since I considered it out of scope to include an online tenant management system. Converting to a relational model wasn't hard, though. Here's the database schema: </p> <p> <pre><span style="color:blue;">CREATE</span>&nbsp;<span style="color:blue;">TABLE</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Restaurants]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span> &nbsp;&nbsp;&nbsp;&nbsp;[Id]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Name]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">NVARCHAR&nbsp;</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span>&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">UNIQUE</span><span style="color:gray;">,</span> &nbsp;&nbsp;&nbsp;&nbsp;[OpensAt]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">TIME</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[LastSeating]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">TIME</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[SeatingDuration]&nbsp;&nbsp;<span style="color:blue;">TIME</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">PRIMARY</span>&nbsp;<span style="color:blue;">KEY</span>&nbsp;<span style="color:blue;">CLUSTERED&nbsp;</span><span style="color:gray;">(</span>[Id]&nbsp;<span style="color:blue;">ASC</span><span style="color:gray;">)</span> <span style="color:gray;">)</span> <span style="color:blue;">CREATE</span>&nbsp;<span style="color:blue;">TABLE</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Tables]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span> &nbsp;&nbsp;&nbsp;&nbsp;[Id]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">IDENTITY</span><span style="color:gray;">,</span> &nbsp;&nbsp;&nbsp;&nbsp;[RestaurantId]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">REFERENCES</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Restaurants]<span style="color:gray;">(</span>Id<span style="color:gray;">),</span> &nbsp;&nbsp;&nbsp;&nbsp;[Capacity]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[IsCommunal]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">BIT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">PRIMARY</span>&nbsp;<span style="color:blue;">KEY</span>&nbsp;<span style="color:blue;">CLUSTERED&nbsp;</span><span style="color:gray;">(</span>[Id]&nbsp;<span style="color:blue;">ASC</span><span style="color:gray;">)</span> <span style="color:gray;">)</span></pre> </p> <p> This little subsystem requires two database tables: One that keeps track of the overall restaurant configuration, such as name, opening and closing times, and another database table that lists all a restaurant's physical tables. </p> <p> You may argue that this is still too simple to realistically capture the intricacies of existing database systems, but conversely I'll remind you that the scope of this article is the sort of system where you develop and design the application first; not a system where you're given a relational database upon which you must create an application. </p> <p> Had I been given this assignment in a realistic setting, a relational database probably wouldn't have been my first choice. Some kind of document database, or even blob storage, strikes me as a better fit. Still, this article is about ORMs, so I'll pretend that there are external circumstances that dictate a relational database. </p> <p> To test the system, I also created a script to populate these tables. Here's part of it: </p> <p> <pre><span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Restaurants]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[Id]<span style="color:gray;">,</span>&nbsp;[Name]<span style="color:gray;">,</span>&nbsp;[OpensAt]<span style="color:gray;">,</span>&nbsp;[LastSeating]<span style="color:gray;">,</span>&nbsp;[SeatingDuration]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>1<span style="color:gray;">,</span>&nbsp;<span style="color:red;">N&#39;Hipgnosta&#39;</span><span style="color:gray;">,</span>&nbsp;<span style="color:red;">&#39;18:00&#39;</span><span style="color:gray;">,</span>&nbsp;<span style="color:red;">&#39;21:00&#39;</span><span style="color:gray;">,</span>&nbsp;<span style="color:red;">&#39;6:00&#39;</span><span style="color:gray;">)</span> <span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Tables]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[RestaurantId]<span style="color:gray;">,</span>&nbsp;[Capacity]<span style="color:gray;">,</span>&nbsp;[IsCommunal]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>1<span style="color:gray;">,</span>&nbsp;10<span style="color:gray;">,</span>&nbsp;1<span style="color:gray;">)</span> <span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Restaurants]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[Id]<span style="color:gray;">,</span>&nbsp;[Name]<span style="color:gray;">,</span>&nbsp;[OpensAt]<span style="color:gray;">,</span>&nbsp;[LastSeating]<span style="color:gray;">,</span>&nbsp;[SeatingDuration]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>2112<span style="color:gray;">,</span>&nbsp;<span style="color:red;">N&#39;Nono&#39;</span><span style="color:gray;">,</span>&nbsp;<span style="color:red;">&#39;18:00&#39;</span><span style="color:gray;">,</span>&nbsp;<span style="color:red;">&#39;21:00&#39;</span><span style="color:gray;">,</span>&nbsp;<span style="color:red;">&#39;6:00&#39;</span><span style="color:gray;">)</span> <span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Tables]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[RestaurantId]<span style="color:gray;">,</span>&nbsp;[Capacity]<span style="color:gray;">,</span>&nbsp;[IsCommunal]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>2112<span style="color:gray;">,</span>&nbsp;6<span style="color:gray;">,</span>&nbsp;1<span style="color:gray;">)</span> <span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Tables]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[RestaurantId]<span style="color:gray;">,</span>&nbsp;[Capacity]<span style="color:gray;">,</span>&nbsp;[IsCommunal]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>2112<span style="color:gray;">,</span>&nbsp;4<span style="color:gray;">,</span>&nbsp;1<span style="color:gray;">)</span> <span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Tables]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[RestaurantId]<span style="color:gray;">,</span>&nbsp;[Capacity]<span style="color:gray;">,</span>&nbsp;[IsCommunal]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>2112<span style="color:gray;">,</span>&nbsp;2<span style="color:gray;">,</span>&nbsp;0<span style="color:gray;">)</span> <span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Tables]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[RestaurantId]<span style="color:gray;">,</span>&nbsp;[Capacity]<span style="color:gray;">,</span>&nbsp;[IsCommunal]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>2112<span style="color:gray;">,</span>&nbsp;2<span style="color:gray;">,</span>&nbsp;0<span style="color:gray;">)</span> <span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Tables]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[RestaurantId]<span style="color:gray;">,</span>&nbsp;[Capacity]<span style="color:gray;">,</span>&nbsp;[IsCommunal]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>2112<span style="color:gray;">,</span>&nbsp;4<span style="color:gray;">,</span>&nbsp;0<span style="color:gray;">)</span> <span style="color:blue;">INSERT</span>&nbsp;<span style="color:blue;">INTO</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Tables]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span>[RestaurantId]<span style="color:gray;">,</span>&nbsp;[Capacity]<span style="color:gray;">,</span>&nbsp;[IsCommunal]<span style="color:gray;">)</span> <span style="color:blue;">VALUES&nbsp;</span><span style="color:gray;">(</span>2112<span style="color:gray;">,</span>&nbsp;4<span style="color:gray;">,</span>&nbsp;0<span style="color:gray;">)</span></pre> </p> <p> There are more rows than this, but this should give you an idea of what data looks like. </p> <h3 id="ba5d810c332945398ab2a870711357f1"> Reading restaurant data without an ORM <a href="#ba5d810c332945398ab2a870711357f1">#</a> </h3> <p> Due to the foreign key relationship, reading restaurant data from the database is a little more involved than reading from a single table. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;Restaurant?&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetRestaurant</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlCommand(readByNameSql); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;name); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurants</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;ReadRestaurants(cmd); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;restaurants.SingleOrDefault(); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;readByNameSql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;[Id],&nbsp;[Name],&nbsp;[OpensAt],&nbsp;[LastSeating],&nbsp;[SeatingDuration] &nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;[dbo].[Restaurants] &nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;[Name]&nbsp;=&nbsp;@Name &nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;[RestaurantId],&nbsp;[Capacity],&nbsp;[IsCommunal] &nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;[dbo].[Tables] &nbsp;&nbsp;&nbsp;&nbsp;JOIN&nbsp;[dbo].[Restaurants] &nbsp;&nbsp;&nbsp;&nbsp;ON&nbsp;[dbo].[Tables].[RestaurantId]&nbsp;=&nbsp;[dbo].[Restaurants].[Id] &nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;[Name]&nbsp;=&nbsp;@Name&quot;</span>;</pre> </p> <p> There are more than one option when deciding how to construct the query. You could make one query with a join, in which case you'd get rows with repeated data, and you'd then need to detect duplicates, or you could do as I've done here: Query each table to get multiple result sets. </p> <p> I'm not claiming that this is better in any way. I only chose this option because I found the code that I had to write less offensive. </p> <p> Since the <code>IRestaurantDatabase</code> interface defines three different kinds of queries (<code>GetAll()</code>, <code>GetRestaurant(int id)</code>, and <code>GetRestaurant(string name)</code>), I invoked the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">rule of three</a> and extracted a helper method: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;IEnumerable&lt;Restaurant&gt;&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">ReadRestaurants</span>(SqlCommand&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">conn</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlConnection(ConnectionString); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Connection&nbsp;=&nbsp;conn; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;conn.OpenAsync(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">rdr</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;cmd.ExecuteReaderAsync(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurants</span>&nbsp;=&nbsp;Enumerable.Empty&lt;Restaurant&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(<span style="color:blue;">await</span>&nbsp;rdr.ReadAsync()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;restaurants&nbsp;=&nbsp;restaurants.Append(ReadRestaurantRow(rdr)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="color:blue;">await</span>&nbsp;rdr.NextResultAsync()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(<span style="color:blue;">await</span>&nbsp;rdr.ReadAsync()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;restaurants&nbsp;=&nbsp;ReadTableRow(rdr,&nbsp;restaurants); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;restaurants; }</pre> </p> <p> The <code>ReadRestaurants</code> method does the overall work of opening the database connection, executing the query, and moving through rows and result sets. Again, we'll find mapping code hidden in helper methods: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Restaurant&nbsp;<span style="color:#74531f;">ReadRestaurantRow</span>(SqlDataReader&nbsp;<span style="font-weight:bold;color:#1f377f;">rdr</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Restaurant( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">int</span>)rdr[<span style="color:#a31515;">&quot;Id&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">string</span>)rdr[<span style="color:#a31515;">&quot;Name&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;MaitreD( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeOfDay((TimeSpan)rdr[<span style="color:#a31515;">&quot;OpensAt&quot;</span>]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeOfDay((TimeSpan)rdr[<span style="color:#a31515;">&quot;LastSeating&quot;</span>]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(TimeSpan)rdr[<span style="color:#a31515;">&quot;SeatingDuration&quot;</span>])); }</pre> </p> <p> As the name suggests, <code>ReadRestaurantRow</code> reads a row from the <code>Restaurants</code> table and converts it into a <code>Restaurant</code> object. At this time, however, it creates each <code>MaitreD</code> object without any tables. This is possible because one of the <code>MaitreD</code> constructors takes a <code>params</code> array as the last parameter: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;TimeOfDay&nbsp;<span style="font-weight:bold;color:#1f377f;">opensAt</span>, &nbsp;&nbsp;&nbsp;&nbsp;TimeOfDay&nbsp;<span style="font-weight:bold;color:#1f377f;">lastSeating</span>, &nbsp;&nbsp;&nbsp;&nbsp;TimeSpan&nbsp;<span style="font-weight:bold;color:#1f377f;">seatingDuration</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">params</span>&nbsp;Table[]&nbsp;<span style="font-weight:bold;color:#1f377f;">tables</span>)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>(opensAt,&nbsp;lastSeating,&nbsp;seatingDuration,&nbsp;tables.AsEnumerable()) { }</pre> </p> <p> Only when the <code>ReadRestaurants</code> method moves on to the next result set can it add tables to each restaurant: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;Restaurant&gt;&nbsp;<span style="color:#74531f;">ReadTableRow</span>( &nbsp;&nbsp;&nbsp;&nbsp;SqlDataReader&nbsp;<span style="font-weight:bold;color:#1f377f;">rdr</span>, &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Restaurant&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurants</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurantId</span>&nbsp;=&nbsp;(<span style="color:blue;">int</span>)rdr[<span style="color:#a31515;">&quot;RestaurantId&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>&nbsp;=&nbsp;(<span style="color:blue;">int</span>)rdr[<span style="color:#a31515;">&quot;Capacity&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">isCommunal</span>&nbsp;=&nbsp;(<span style="color:blue;">bool</span>)rdr[<span style="color:#a31515;">&quot;IsCommunal&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">table</span>&nbsp;=&nbsp;isCommunal&nbsp;?&nbsp;Table.Communal(capacity)&nbsp;:&nbsp;Table.Standard(capacity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;restaurants.Select(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Id&nbsp;==&nbsp;restaurantId&nbsp;?&nbsp;AddTable(r,&nbsp;table)&nbsp;:&nbsp;r); }</pre> </p> <p> As was also the case in <code>ReadRestaurantRow</code>, this method uses string-based indexers on the <code>rdr</code> to extract the data. I'm no fan of stringly-typed code, but at least I have automated tests that exercise these methods. </p> <p> Could an ORM help by creating strongly-typed classes that model database tables? To a degree; I'll discuss that later. </p> <p> In any case, since the entire code base follows the <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">Functional Core, Imperative Shell</a> architecture, the entire Domain Model is made of immutable data types with pure functions. Thus, <code>ReadTableRow</code> has to iterate over all <code>restaurants</code> and add the table when the <code>Id</code> matches. <code>AddTable</code> does that: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Restaurant&nbsp;<span style="color:#74531f;">AddTable</span>(Restaurant&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>,&nbsp;Table&nbsp;<span style="font-weight:bold;color:#1f377f;">table</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;restaurant.Select(<span style="font-weight:bold;color:#1f377f;">m</span>&nbsp;=&gt;&nbsp;m.WithTables(m.Tables.Append(table).ToArray())); }</pre> </p> <p> I can think of other ways to solve the overall mapping task when using ADO.NET, but this was what made most sense to me. </p> <h3 id="3eda5ebf7165478698c756d59af43a1e"> Reading restaurants with Entity Framework <a href="#3eda5ebf7165478698c756d59af43a1e">#</a> </h3> <p> Does an ORM like <a href="https://en.wikipedia.org/wiki/Entity_Framework">Entity Framework</a> (EF) improve things? To a degree, but not enough to outweigh the disadvantages it also brings. </p> <p> In order to investigate, I followed <a href="https://learn.microsoft.com/ef/core/managing-schemas/scaffolding">the EF documentation to scaffold</a> code from a database I'd set up for only that purpose. For the <code>Tables</code> table it created the following <code>Table</code> class and a similar <code>Restaurant</code> class. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">partial</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Table</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;RestaurantId&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsCommunal&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;Restaurant&nbsp;Restaurant&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;}&nbsp;=&nbsp;<span style="color:blue;">null</span>!; }</pre> </p> <p> Hardly surprising. Also, hardly object-oriented, but more about that later, too. </p> <p> Entity Framework didn't, by itself, add a <code>Tables</code> collection to the <code>Restaurant</code> class, so I had to do that by hand, as well as modify the <a href="https://learn.microsoft.com/dotnet/api/microsoft.entityframeworkcore.dbcontext">DbContext</a>-derived class to tell it about this relationship: </p> <p> <pre>entity.OwnsMany(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Tables,&nbsp;<span style="font-weight:bold;color:#1f377f;">b</span>&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;b.Property&lt;<span style="color:blue;">int</span>&gt;(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Id).ValueGeneratedOnAdd(); &nbsp;&nbsp;&nbsp;&nbsp;b.HasKey(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Id); });</pre> </p> <p> I thought that such a simple foreign key relationship would be something an ORM would help with, but apparently not. </p> <p> With that in place, I could now rewrite the above <code>GetRestaurant</code> method to use Entity Framework instead of ADO.NET: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;Restaurants.Restaurant?&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">GetRestaurant</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantsContext(ConnectionString); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dbRestaurant</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;db.Restaurants.FirstOrDefaultAsync(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Name&nbsp;==&nbsp;name); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(dbRestaurant&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;ToDomainModel(dbRestaurant); }</pre> </p> <p> The method now queries the database, and EF automatically returns a populated object. This would be nice if it was the right kind of object, but alas, it isn't. <code>GetRestaurant</code> still has to call a helper method to convert to the correct Domain Object: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Restaurants.Restaurant&nbsp;<span style="color:#74531f;">ToDomainModel</span>(Restaurant&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Restaurants.Restaurant( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;restaurant.Id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;restaurant.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;MaitreD( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeOfDay(restaurant.OpensAt), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeOfDay(restaurant.LastSeating), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;restaurant.SeatingDuration, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;restaurant.Tables.Select(ToDomainModel).ToList())); }</pre> </p> <p> While this helper method converts an EF <code>Restaurant</code> object to a proper Domain Object (<code>Restaurants.Restaurant</code>), it also needs another helper to convert the <code>table</code> objects: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Restaurants.Table&nbsp;<span style="color:#74531f;">ToDomainModel</span>(Table&nbsp;<span style="font-weight:bold;color:#1f377f;">table</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(table.IsCommunal) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Restaurants.Table.Communal(table.Capacity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Restaurants.Table.Standard(table.Capacity); }</pre> </p> <p> As should be clear by now, using vanilla EF doesn't reduce the need for mapping. </p> <p> Granted, the mapping code is a bit simpler, but you still need to remember to map <code>restaurant.Name</code> to the right constructor parameter, <code>restaurant.OpensAt</code> and <code>restaurant.LastSeating</code> to <em>their</em> correct places, <code>table.Capacity</code> to a constructor argument, and so on. If you make changes to the database schema or the Domain Model, you'll need to edit this code. </p> <h3 id="b85eb83e5acc4d66baaf9ec51c3be02d"> Encapsulation <a href="#b85eb83e5acc4d66baaf9ec51c3be02d">#</a> </h3> <p> This is the point where more than one reader wonders: <em>Can't you just..?</em> </p> <p> In short, no, I can't just. </p> <p> The most common reaction is most likely that I'm doing this all wrong. I'm supposed to use the EF classes as my Domain Model. </p> <p> But I can't, and I won't. I can't because I already have classes in place that serve that purpose. I also will not, because it would violate the Dependency Inversion Principle. As I recently described, <a href="/2023/09/04/decomposing-ctfiyhs-sample-code-base">the architecture is Ports and Adapters</a>, or, if you will, <a href="/ref/clean-architecture">Clean Architecture</a>. The database <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> should depend on the Domain Model; the Domain Model shouldn't depend on the database implementation. </p> <p> Okay, but couldn't I have generated the EF classes in the Domain Model? After all, a class like the above <code>Table</code> is just a <a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object">POCO</a> Entity. It doesn't depend on the Entity Framework. I could have those classes in my Domain Model, put my <code>DbContext</code> in the data access layer, and have the best of both worlds. Right? </p> <p> The code shown so far hints at a particular API afforded by the Domain Model. If you've read <a href="/code-that-fits-in-your-head">my book</a>, you already know what comes next. Here's the <code>Table</code> Domain Model's API: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Table</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table&nbsp;<span style="color:#74531f;">Standard</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seats</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table&nbsp;<span style="color:#74531f;">Communal</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seats</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;RemainingSeats&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Table&nbsp;<span style="font-weight:bold;color:#74531f;">Reserve</span>(Reservation&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="font-weight:bold;color:#74531f;">Accept</span>&lt;<span style="color:#2b91af;">T</span>&gt;(ITableVisitor&lt;T&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">visitor</span>) }</pre> </p> <p> A couple of qualities of this design should be striking: There's <em>no</em> visible constructor - not even one that takes parameters. Instead, the type affords two static creation functions. One creates a standard table, the other a communal table. My book describes the difference between these types, and so does <a href="/2020/01/27/the-maitre-d-kata">the Maître d' kata</a>. </p> <p> This isn't some frivolous design choice of mine, but rather quite deliberate. That <code>Table</code> class is a <a href="/2018/06/25/visitor-as-a-sum-type">Visitor-encoded sum type</a>. You can debate whether I should have modelled a table as a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> or a polymorphic object, but now that I've chosen a sum type, it should be explicit in the API design. </p> <blockquote> <p> "Explicit is better than implicit." </p> <footer><cite><a href="https://peps.python.org/pep-0020/">The Zen of Python</a></cite></footer> </blockquote> <p> When we program, we make many mistakes. It's important to discover the mistakes as soon as possible. With a compiled language, <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">the first feedback you get is from the compiler</a>. I favour leveraging the compiler, and its type system, to prevent as many mistakes as possible. That's what <a href="https://buttondown.email/hillelwayne/archive/making-illegal-states-unrepresentable/">Hillel Wayne calls <em>constructive</em> data</a>. <a href="https://blog.janestreet.com/effective-ml-video/">Make illegal states unrepresentable</a>. </p> <p> I could, had I thought of it at the time, have introduced <a href="/2022/08/22/can-types-replace-validation">a predicative natural-number wrapper of integers</a>, in which case I could have strengthened the contract of <code>Table</code> even further: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Table</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table&nbsp;<span style="color:#74531f;">Standard</span>(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table&nbsp;<span style="color:#74531f;">Communal</span>(NaturalNumber&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;NaturalNumber&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;RemainingSeats&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Table&nbsp;<span style="font-weight:bold;color:#74531f;">Reserve</span>(Reservation&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="font-weight:bold;color:#74531f;">Accept</span>&lt;<span style="color:#2b91af;">T</span>&gt;(ITableVisitor&lt;T&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">visitor</span>) }</pre> </p> <p> The point is that I take <a href="/encapsulation-and-solid">encapsulation</a> seriously, and my interpretation of the concept is heavily inspired by <a href="https://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Meyer</a>'s <a href="/ref/oosc">Object-Oriented Software Construction</a>. The view of encapsulation emphasises <em>contracts</em> (preconditions, invariants, postconditions) rather than information hiding. </p> <p> As I described in a previous article, <a href="/2022/08/22/can-types-replace-validation">you can't model all preconditions and invariants with types</a>, but you can still let the type system do much heavy lifting. </p> <p> This principle applies to all classes that are part of the Domain Model; not only <code>Table</code>, but also <code>Restaurant</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Restaurant</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">id</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>,&nbsp;MaitreD&nbsp;<span style="font-weight:bold;color:#1f377f;">maitreD</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaitreD&nbsp;MaitreD&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Restaurant&nbsp;<span style="font-weight:bold;color:#74531f;">WithId</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newId</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Restaurant&nbsp;<span style="font-weight:bold;color:#74531f;">WithName</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newName</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Restaurant&nbsp;<span style="font-weight:bold;color:#74531f;">WithMaitreD</span>(MaitreD&nbsp;<span style="font-weight:bold;color:#1f377f;">newMaitreD</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Restaurant&nbsp;<span style="font-weight:bold;color:#74531f;">Select</span>(Func&lt;MaitreD,&nbsp;MaitreD&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">selector</span>) }</pre> </p> <p> While this class does have a public constructor, it makes use of another design choice that Entity Framework doesn't support: It nests one rich object (<code>MaitreD</code>) inside another. Why does it do that? </p> <p> Again, this is far from a frivolous design choice I made just to be difficult. Rather, it's a result of a need-to-know principle (which strikes me as closely related to the <a href="https://en.wikipedia.org/wiki/Single-responsibility_principle">Single Responsibility Principle</a>): A class should only contain the information it needs in order to perform its job. </p> <p> The <code>MaitreD</code> class does all the heavy lifting when it comes to deciding whether or not to accept reservations, how to allocate tables, etc. It doesn't, however, need to know the <code>id</code> or <code>name</code> of the restaurant in order to do that. Keeping that information out of <code>MaitreD</code>, and instead in the <code>Restaurant</code> wrapper, makes the code simpler and easier to use. </p> <p> The bottom line of all this is that I value encapsulation over 'easy' database mapping. </p> <h3 id="3cfc364c9a87463aaac98bc358d062d5"> Limitations of Entity Framework <a href="#3cfc364c9a87463aaac98bc358d062d5">#</a> </h3> <p> The promise of an object-relational mapper is that it automates mapping between objects and database. Is that promise realised? </p> <p> In its current incarnation, <a href="https://stackoverflow.com/q/77039584/126014">it doesn't look as though Entity Framework supports mapping to and from the Domain Model</a>. With the above tweaks, it supports the database schema that I've described, but only via 'Entity classes'. I still have to map to and from the 'Entity objects' and the actual Domain Model. Not much is gained. </p> <p> One should, of course, be careful not drawing too strong inferences from this example. First, proving anything impossible is generally difficult. Just because <em>I</em> can't find a way to do what I want, I can't conclude that it's impossible. That a few other people tell me, too, that it's impossible still doesn't constitute strong evidence. </p> <p> Second, even if it's impossible today, it doesn't follow that it will be impossible forever. Perhaps Entity Framework will support my Domain Model in the future. </p> <p> Third, we can't conclude that just because Entity Framework (currently) doesn't support my Domain Model it follows that no object-relational mapper (ORM) does. There might be another ORM out there that perfectly supports my design, but I'm just not aware of it. </p> <p> Based on my experience and what I see, read, and hear, I don't think any of that likely. Things might change, though. </p> <h3 id="4002bb8f9ec64b14958233b16b93ec10"> Net benefit or drawback? <a href="#4002bb8f9ec64b14958233b16b93ec10">#</a> </h3> <p> Perhaps, despite all of this, you still prefer ORMs. You may compare my ADO.NET code to my Entity Framework code and conclude that the EF code still looks simpler. After all, when using ADO.NET I have to jump through some hoops to load the correct tables associated with each restaurant, whereas EF automatically handles that for me. The EF version requires fewer lines of code. </p> <p> In isolation, the fewer lines of code the better. This seems like an argument for using an ORM after all, even if the original promise remains elusive. Take what you can get. </p> <p> On the other hand, when you take on a dependency, there's usually a cost that comes along. A library like Entity Framework isn't free. While you don't pay a licence fee for it, it comes with other costs. You have to learn how to use it, and so do your colleagues. You also have to keep up to date with changes. </p> <p> Every time some exotic requirement comes up, you'll have to spend time investigating how to address it with that ORM's API. This may lead to a game of <a href="https://en.wikipedia.org/wiki/Whac-A-Mole">Whac-A-Mole</a> where every tweak to the ORM leads you further down the rabbit hole, and couples your code tighter with it. </p> <p> You can only keep up with so many things. What's the best investment of your time, and the time of your team mates? Learning and knowing <a href="https://en.wikipedia.org/wiki/SQL">SQL</a>, or learning and keeping up to date with a particular ORM? </p> <p> I learned SQL decades ago, and that knowledge is still useful. On the other hand, I don't even know how many library and framework APIs that I've both learned and forgotten about. </p> <p> As things currently stand, it looks to me as though the net benefit of using a library like Entity Framework is negative. Yes, it might save me a few lines of code, but I'm not ready to pay the costs just outlined. </p> <p> This balance could tip in the future, or my context may change. </p> <h3 id="e76ec6ffdf3f44949bab3d86786b26ae"> Conclusion <a href="#e76ec6ffdf3f44949bab3d86786b26ae">#</a> </h3> <p> For the kind of applications that I tend to become involved with, I don't find object-relational mappers particularly useful. When you have a rich Domain Model where the first design priority is encapsulation, assisted by the type system, it looks as though mapping is unavoidable. </p> <p> While you can ask automated tools to generate code that mirrors a database schema (or the other way around), only classes with poor encapsulation are supported. As soon as you do something out of the ordinary like static factory methods or nested objects, apparently Entity Framework gives up. </p> <p> Can we extrapolate from Entity Framework to other ORMs? Can we infer that Entity Framework will never be able to support objects with proper encapsulation, just because it currently doesn't? </p> <p> I can't say, but I'd be surprised if things change soon, if at all. If, on the other hand, it eventually turns out that I can have my cake and eat it too, then why shouldn't I? </p> <p> Until then, however, I don't find that the benefits of ORMs trump the costs of using them. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="75ca5755d2a4445ba4836fc3f6922a5c"> <div class="comment-author">Vlad <a href="#75ca5755d2a4445ba4836fc3f6922a5c">#</a></div> <div class="comment-content"> <p> One project I worked on was (among other things) mapping data from database to rich domain objects in the way similar to what is described in this article. These object knew how to do a lot of things but were dependant on related objects and so everything neded to be loaded in advance from the database in order to ensure correctness. So having a Order, OrderLine, Person, Address and City, all the rows needed to be loaded in advance, mapped to objects and references set to create the object graph to be able to, say, display shipping costs based on person's address. </p> <p> The mapping step involved cumbersome manual coding and was error prone because it was easy to forget to load some list or set some reference. Reflecting on that experience, it seems to me that sacrificing a bit of purity wrt domain modelling and leaning on an ORM to lazily load the references would have been much more efficient <strong>and</strong> correct. </p> <p> But I guess it all depends on the context..? </p> </div> <div class="comment-date">2023-09-19 13:17 UTC</div> </div> <div class="comment" id="58265df2f91c434696a3e0d21fe1b3b1"> <div class="comment-author">qfilip <a href="#58265df2f91c434696a3e0d21fe1b3b1">#</a></div> <div class="comment-content"> <!-- comment here --> <p> Thanks. I've been following recent posts, but I was too lazy to go through the whole PRing things to reply. Maybe that's a good thing, since it forces you to think how to reply, instead of throwing a bunch of words together quickly. Anyways, back to business. </p> <p> I'm not trying to sell one way or the other, because I'm seriously conflicted with both. Since most people on the web tend to fall into ORM category (in .NET world at least), I was simply looking for other perspective, from someone more knowledgable than me. </p> <p> The following is just my thinking out loud... </p> <p> You've used DB-first approach and scaffolding classes from DB schema. With EF core, the usual thing to do, is the opposite. Write classes to scaffold DB schema. Now, this doesn't save us from writing those "relational properties", but it allows us to generate DB update scripts. So if you have a class like: <pre> class SomeTable { public int Id; public string Name; } </pre> and you add a field: <pre> class SomeTable { public int Id; public string Name; public DateTime Birthday; } </pre> you can run <pre> add-migration MyMigration // generate migration file update-database // execute it </pre> </p> <p> This gives you a nice way to track DB chages via Git, but it can also introduce conflicts. Two devs cannot edit the same class/table. You have to be really careful when making commits. Another painful thing to do this way is creating DB views and stored procedures. I've honestly never saw a good solution for it. Maybe trying to do these things is a futile effort in the first place. </p> <p> The whole <pre> readByNameSql = @"SELECT [Id], [Name], [OpensAt], [LastSeating], [SeatingDuration]... </pre> is giving me heebie jeebies. It is easy to change some column name, and introduce a bug. It might be possible to do stuff with string interpolation, but at that point, I'm thinking about creating my own framework... </p> <p> <blockquote> The most common reaction is most likely that I'm doing this all wrong. I'm supposed to use the EF classes as my Domain Model. - Mark Seemann </blockquote> One of the first things that I was taught on my first job, was to never expose my domain model to the outside world. The domain model being EF Core classes... These days, I'm thinking quite the opposite. EF Core classes are DTOs for the database (with some boilerplate in order for framework to do it's magic). I also <b>want</b> to expose my domain model to the outside world. Why not? That's the contract after all. But the problem with this, is that it adds another layer of mapping. Since my domain model validation is done in class constructor, deserialization of requests becomes a problem. Ideally, it should sit in a static method. But in that case I have: jsonDto -> domainModel -> dbDto. The No-ORM approach also still requires me to map domainModel to command parameters manually. All of this is a tedious, and very error prone process. Especially if you have the case like <a href="#75ca5755d2a4445ba4836fc3f6922a5c">vlad</a> mentioned above. </p> <p> Minor observation on your code. People rarely map things from DB data to domain models when using EF Core. This is a horrible thing to do. Anyone can run a script against a DB, and corrupt the data. It is something I intend to enforce in future projects, if possible. Thank you F# community. </p> <p> I can't think of anything more to say at the moment. Thanks again for a fully-fledged-article reply :). I also recommend <a href="https://www.youtube.com/watch?v=ZYfdjszs8sU">this video</a>. I haven't had the time to try things he is talking about yet. </p> </div> <div class="comment-date">2023-09-21 19:27 UTC</div> </div> <div class="comment" id="b3a8702fe15b416fa20f78d1351c8ca4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b3a8702fe15b416fa20f78d1351c8ca4">#</a></div> <div class="comment-content"> <p> Vlad, qfilip, thank you for writing. </p> <p> I think your comments warrant another article. I'll post an update here later. </p> </div> <div class="comment-date">2023-09-24 15:57 UTC</div> </div> <div class="comment" id="359a7bb0d2c14b8eb2dcb2ac6de4897d"> <div class="comment-author">qfilip <a href="#359a7bb0d2c14b8eb2dcb2ac6de4897d">#</a></div> <div class="comment-content"> <p> Quick update from me again. I've been thinking and experimenting with several approaches to solve issues I've written about above. How idealized world works and where do we make compromises. Forgive me for the sample types, I couldn't think of anything else. Let's assume we have this table: <pre> type Sheikh = { // db entity data Id: Guid CreatedAt: DateTime ModifiedAt: DateTime Deleted: bool // domain data Name: string Email: string // unique constraint here // relational data Wives: Wife list Supercars: Supercar list } </pre> </p> <p> I've named first 3 fields as "entity data". Why would my domain model contain an ID? It shouldn't care about persistence. I may save it to the DB, write it to a text file, or print it on a piece of paper. Don't care. We put IDs because data usually ends up in a DB. I could have used Email here to serve as an ID, because it should be unique, but we also like to standardize these stuff. All IDs shall be uuids. </p> <p> There are also these "CreatedAt", "ModifiedAt" and "Deleted" columns. This is something I usually do, when I want soft-delete functionality. Denoramalize the data to gain performance. Otherwise, I would need to make... say... EntityStatus table to keep that data, forcing me to do a JOIN for every read operation and additional UPDATE EntityStatus for every write operation. So I kinda sidestep "the good practices" to avoid very real complications. </p> <p> Domain data part is what it is, so I can safely skip that part. </p> <p> Relational data part is the most interesting bit. I think this is what keeps me falling back to EntityFramework and why using "relational properties" are unavoidable. Either that, or I'm missing something. </p> <p> Focusing attention on <code>Sheikh</code> table here, with just 2 relations, there are 4 potential scenarios. I don't want to load stuff from the DB, unless they are required, so the scenarios are: <ul> <li>Load <code>Sheikh</code> without relational data</li> <li>Load <code>Sheikh</code> with <code>Wives</code></li> <li>Load <code>Sheikh</code> with <code>Supercars</code></li> <li>Load <code>Sheikh</code> with <code>Wives</code> and <code>Supercars</code></li> </ul> </p> <p> 2<sup>NRelations</sup> I guess? I'm three beers in on this, with only six hours left until corporate clock starts ticking, so my math is probably off. </p> <p> God forbid if any of these relations have their own "inner relations" you may or may not need to load. This is where the (magic mapping/to SQL translations) really start getting useful. There will be some code repetition, but you'll just need to add <code>ThenInclude(x => ...)</code> and you're done. </p> <p> Now the flip side. Focusing attention on <code>Supercar</code> table: <pre> type Supercar = { // db entity data ... // domain data Vendor: string Model: string HorsePower: int // relational data Owner: Sheikh OwnerId: Guid } </pre> </p> <p> Pretty much same as before. Sometimes I'll need <code>Sheikh</code> info, sometimes I won't. One of F# specific problems I'm having is that, records require all fields to be populated. What if I need just SheikhID to perform some domain logic? <pre> let tuneSheikhCars (sheikhId) (hpIncrement) (cars) = cars |> List.filter (fun x -> x.Owner.Id = sheikhId) |> List.map (fun x -> x with { HorsePower = x.HorsePower + hpIncrement }) </pre> </p> <p> Similar goes for inserting new <code>Supercar</code>. I want to query-check first if <code>Owner/Sheikh</code> exists, before attempting insertion. You can pass it as a separate parameter, but code gets messier and messier. </p> <p> No matter how I twist and turn things around, in the real world, I'm not only concerned by current steps I need to take to complete a task, but also with possible future steps. Now, I could define a record that only contains relevant data per each request. But, as seen above, I'd be eventually forced to make ~ 2<sup>NRelations</sup> of such records, instead of one. A reusable one, that serves like a bucket for a preferred persistence mechanism, allowing me to load relations later on, because nothing lives in memory long term. </p> <p> I strayed away slightly here from ORM vs no-ORM discussion that I've started earlier. Because, now I realize that this problem isn't just about mapping things from type <code>A</code> to type <code>B</code>. </p> </div> <div class="comment-date">2023-10-08 23:24 UTC</div> </div> <div class="comment" id="f8dc3a0d9ca44cbc88de5b773f4679d0"> <div class="comment-author">opcoder <a href="#f8dc3a0d9ca44cbc88de5b773f4679d0">#</a></div> <div class="comment-content"> I wonder if EF not having all the features we want isn't a false problem. I feel like we try to use the domain entities as DTOs and viceversa, breaking the SRP principle. But if we start writing DTOs and use them with EF, we would need a layer to map between the DTOs and the entities (AutoMapper might help with this?). I'm sure this has been discussed before. </div> <div class="comment-date">2023-10-09 6:56 UTC</div> </div> <div class="comment" id="9af5c8eda58f44fcaa24c22515286be8"> <div class="comment-author">qfilip <a href="#9af5c8eda58f44fcaa24c22515286be8">#</a></div> <div class="comment-content"> <a href="#f8dc3a0d9ca44cbc88de5b773f4679d0">opcoder</a> not really, no... Automapper(s) should only be used for mapping between two "dumb objects" (DTOs). I wouldn't drag in a library even for that, however, as it's relatively simple to write this thing yourself (with tests) and have full control / zero configuration when you come to a point to create some special projections. As for storing domain models in objects, proper OOP objects, with both data and behaviour, I don't like that either. Single reason for that is: constructors. This is where you pass the data to be validated into a domain model, and this is where OOP has a fatal flaw for me. Constructors can only throw exceptions, giving me no room to maneuver. You can use static methods with <code>ValidationResult<T></code> as a return type, but now we're entering a territory where C#, as a language, is totally unprepared for. </div> <div class="comment-date"><time>2023-10-13 17:00 UTC</time></div> </div> <div class="comment" id="84a111412d674526b05226f903e81af3"> <div class="comment-author">Iker <a href="#84a111412d674526b05226f903e81af3">#</a></div> <div class="comment-content"> <p>Just my two cents:</p> <p>Yes, it is possible to map the <code>NaturalNumber</code> object to an E.F class property using <a href="https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions">ValueConverters</a>. Here are a couple of articles talking about this:</p> <ul> <li><a href="https://andrewlock.net/strongly-typed-ids-in-ef-core-using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-4/">Andrew Lock: Using strongly-typed entity IDs to avoid primitive obsession.</a></li> <li><a href="https://thomaslevesque.com/2020/12/23/csharp-9-records-as-strongly-typed-ids-part-4-entity-framework-core-integration/">Thomas Levesque: Using C# 9 records as strongly-typed ids.</a></li> </ul> <p>But even though you can use this, you may still encounter another use cases that you cannot tackle. E.F is just a tool with its limitations, and there will be things you can do with simple C# that you can not do with E.F.</p> <p>I think you need to consider why you want to use E.F, understand its strengths and weaknesses, and then decide if it suits your project.</p> <p>Do you want to use EF solely as a data access layer, or do you want it to be your domain layer?. Maybe for a big project you can use only E.F as a data access layer and use old plain C# files for domain layer. In a [small | medium | quick & dirty] project use as your domain layer.</p> <p>There are bad thing we already know:</p> <ul> <li>Increased complexity.</li> <li>There will be things you can not do. So you must be carefull you will not need something E.F can not give you.</li> <li>You need to know how it works. For example, know that accessing <code>myRestaurant.MaitreD</code> implies a new database access (if you have not loaded it previously).</li> </ul> <p>But sometimes E.F shines, for example:</p> <ul> <li>You are programing against the E.F model, not against a specific database, so it is easier to migrate to another database.</li> <li>Maybe migrate to another database is rare, but it is very convenient to run tests against an in-memory SQLite database. Tests against a real database can be run in the CD/CI environment, for example.</li> <li>Having a centralized point to process changes (<code>SaveChanges</code>) allows you to easily do interesting things: save "CreatedBy," "CreatedDate," "ModifiedBy," and "ModifiedDate" fields for all tables or create historical tables (if you do not have access to the<a href="https://learn.microsoft.com/en-us/sql/relational-databases/tables/temporal-tables?view=sql-server-ver16"> SQL Server temporal tables</a>).</li> <li>Global query filters allow you to make your application multi-tenant with very little code: all tables implement <code>IByClient</code>, a global filter for theses tables... and voilà, your application becomes multi-client with just a few lines.</li> </ul> <p>I am not a E.F defender, in fact I have a love & hate reletaionship with it. But I believe it is a powerful tool for certain projects. As always, the important thing is to think whether it is the right tool for your specific project :)</p> </div> <div class="comment-date">2023-10-15 16:43 UTC</div> </div> <div class="comment" id="0c76c456b47e42ec872603996ba1cfc0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0c76c456b47e42ec872603996ba1cfc0">#</a></div> <div class="comment-content"> <p> Thank you, all, for writing. There's more content in your comments than I can address in one piece, but I've written a follow-up article that engages with some of your points: <a href="/2023/10/23/domain-model-first">Domain Model first</a>. </p> <p> Specifically regarding the point of having to hand-write a lot of code to deal with multiple tables joined in various fashions, I grant that while <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">typing isn't a bottleneck</a>, the more code you add, the greater the risk of bugs. I'm not trying to be dismissive of ORMs as a general tool. If you truly, inescapably, have a relational model, then an ORM seems like a good choice. If so, however, I don't see that you can get good encapsulation at the same time. </p> <p> And indeed, an important responsibility of a software architect is to consider trade-offs to find a good solution for a particular problem. Sometimes such a solution involves an ORM, but sometimes, it doesn't. In my world, it usually doesn't. </p> <p> Do I breathe rarefied air, dealing with esoteric problems that mere mortals can never hope to encounter? I don't think so. Rather, I offer the interpretation that I sometimes approach problems in a different way. All I really try to do with these articles is to present to the public the ways I think about problems. I hope, then, that it may inspire other people to consider problems from more than one angle. </p> <p> Finally, from my various customer engagements I get the impression that people also like ORMs because 'entity classes' look strongly typed. As a counter-argument, I suggest that <a href="/2023/10/16/at-the-boundaries-static-types-are-illusory">this may be an illusion</a>. </p> </div> <div class="comment-date">2023-10-23 06:45 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A first stab at the Brainfuck kata https://blog.ploeh.dk/2023/09/11/a-first-stab-at-the-brainfuck-kata 2023-09-11T08:07:00+00:00 Mark Seemann <div id="post"> <p> <em>I almost gave up, but persevered and managed to produce something that works.</em> </p> <p> As I've <a href="/2023/08/28/a-first-crack-at-the-args-kata">previously mentioned</a>, a customer hired me to swing by to demonstrate test-driven development and <a href="https://stackoverflow.blog/2022/12/19/use-git-tactically/">tactical Git</a>. To make things interesting, we agreed that they'd give me a <a href="https://en.wikipedia.org/wiki/Kata_(programming)">kata</a> at the beginning of the session. I didn't know which problem they'd give me, so I thought it'd be a good idea to come prepared. I decided to seek out katas that I hadn't done before. </p> <p> The demonstration session was supposed to be two hours in front of a participating audience. In order to make my preparation aligned to that situation, I decided to impose a two-hour time limit to see how far I could get. At the same time, I'd also keep an eye on didactics, so preferably proceeding in an order that would be explainable to an audience. </p> <p> Some katas are more complicated than others, so I'm under no illusion that I can complete any, to me unknown, kata in under two hours. My success criterion for the time limit is that I'd like to reach a point that would satisfy an audience. Even if, after two hours, I don't reach a complete solution, I should leave a creative and intelligent audience with a good idea of how to proceed. </p> <p> After a few other katas, I ran into the <a href="https://codingdojo.org/kata/Brainfuck/">Brainfuck</a> kata one Thursday. In this article, I'll describe some of the most interesting things that happened along the way. If you want all the details, the code is <a href="https://github.com/ploeh/BrainfuckCSharp">available on GitHub</a>. </p> <h3 id="24034bdadc7c4f9798ada181f99cd46a"> Understanding the problem <a href="#24034bdadc7c4f9798ada181f99cd46a">#</a> </h3> <p> I had heard about <a href="https://en.wikipedia.org/wiki/Brainfuck">Brainfuck</a> before, but never tried to write an interpreter (or a program, for that matter). </p> <p> The <a href="https://codingdojo.org/kata/Brainfuck/">kata description</a> lacks examples, so I decided to search for them elsewhere. The <a href="https://en.wikipedia.org/wiki/Brainfuck">wikipedia article</a> comes with some examples of small programs (including <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">Hello, World</a>), so ultimately I used that for reference instead of the kata page. </p> <p> I'm happy I wasn't making my first pass on this problem in front of an audience. I spent the first 45 minutes just trying to understand the examples. </p> <p> You might find me slow, since the rules of the language aren't that complicated. I was, however, confused by the way the examples were presented. </p> <p> As the wikipedia article explains, in order to add two numbers together, one can use this idiom: </p> <p> <pre>[-&gt;+&lt;]</pre> </p> <p> The article then proceeds to list a small, complete program that adds two numbers. This program adds numbers this way: </p> <p> <pre>[ Start your loops with your cell pointer on the loop counter (c1 in our case) &lt; + Add 1 to c0 &gt; - Subtract 1 from c1 ] End your loops with the cell pointer on the loop counter</pre> </p> <p> I couldn't understand why this annotated 'walkthrough' explained the idiom in reverse. Several times, I was on the verge of giving up, feeling that I made absolutely no progress. Finally, it dawned on me that the second example is <em>not</em> an explanation of the first example, but rather a separate example that makes use of the same idea, but expresses it in a different way. </p> <p> Most programming languages have more than one way to do things, and this is also the case here. <code>[-&gt;+&lt;]</code> adds two numbers together, but so does <code>[&lt;+&gt-]</code>. </p> <p> Once you understand something, it can be difficult to recall why you ever found it confusing. Now that I get this, I'm having trouble explaining what I was originally thinking, and why it confused me. </p> <p> This experience does, however, drive home a point for educators: When you introduce a concept and then provide examples, the first example should be a direct continuation of the introduction, and not some variation. Variations are fine, too, but should follow later and be clearly labelled. </p> <p> After 45 minutes I had finally cracked the code and was ready to get programming. </p> <h3 id="4903839fcc6a4d75917b97026359cdc6"> Getting started <a href="#4903839fcc6a4d75917b97026359cdc6">#</a> </h3> <p> The <a href="https://codingdojo.org/kata/Brainfuck/">kata description</a> suggests starting with the <code>+</code>, <code>-</code>, <code>&gt;</code>, and <code>&lt;</code> instructions to manage memory. I briefly considered that, but on the other hand, I wanted to have some test coverage. Usually, I take advantage of test-driven development, and I while I wasn't sure how to proceed, I wanted to have some tests. </p> <p> If I were to exclusively start with memory management, I would need some way to inspect the memory in order to write assertions. This struck me as violating <a href="/encapsulation-and-solid">encapsulation</a>. </p> <p> Instead, I thought that I'd write the simplest program that would produce some output, because if I had output, I would have something to verify. </p> <p> That, on the other hand, meant that I had to consider how to model input and output. The Wikipedia article describes these as </p> <blockquote> <p> "two streams of bytes for input and output (most often connected to a keyboard and a monitor respectively, and using the ASCII character encoding)." </p> <footer><cite><a href="https://en.wikipedia.org/wiki/Brainfuck">Wikipedia</a></cite></footer> </blockquote> <p> Knowing that <a href="https://learn.microsoft.com/archive/blogs/ploeh/console-unit-testing">you can model the console's input and output streams as polymorphic objects</a>, I decided to model the output as a <a href="https://learn.microsoft.com/dotnet/api/system.io.textwriter">TextWriter</a>. The lowest-valued printable <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> character is space, which has the byte value <code>32</code>, so I wrote this test: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;++++++++++++++++++++++++++++++++.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;32&nbsp;increments;&nbsp;ASCII&nbsp;32&nbsp;is&nbsp;space</span> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;StringWriter(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;BrainfuckInterpreter(output); &nbsp;&nbsp;&nbsp;&nbsp;sut.Run(program); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;output.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> As you can see, I wrote the test as a <code>[Theory]</code> (parametrised test) from the outset, since I predicted that I'd add more test cases soon. Strictly speaking, when following the <a href="/2019/10/21/a-red-green-refactor-checklist">red-green-refactor checklist</a>, you shouldn't write more code than absolutely necessary. According to <a href="https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it">YAGNI</a>, you should avoid <a href="https://wiki.c2.com/?SpeculativeGenerality">speculative generality</a>. </p> <p> Sometimes, however, you've gone through a process so many times that you know, with near certainty, what happens next. I've done test-driven development for decades, so I occasionally allow my experience to trump the rules. </p> <p> The Brainfuck program in the <code>[InlineData]</code> attribute increments the same data cell 32 times (you can count the plusses) and then outputs its value. The <code>expected</code> output is the space character, since it has the ASCII code <code>32</code>. </p> <p> What's the simplest thing that could possibly work? Something like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">BrainfuckInterpreter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;StringWriter&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">BrainfuckInterpreter</span>(StringWriter&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.output&nbsp;=&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.Write(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As is typical with test-driven development (TDD), the first few tests help you design the API, but not the implementation, which, here, is deliberately naive. </p> <p> Since I felt pressed for time, having already spent 45 minutes of my two-hour time limit getting to grips with the problem, I suppose I lingered less on the <em>refactoring</em> phase than perhaps I should have. You'll notice, at least, that the <code>BrainfuckInterpreter</code> class depends on <a href="https://learn.microsoft.com/dotnet/api/system.io.stringwriter">StringWriter</a> rather than its abstract parent class <code>TextWriter</code>, which was the original plan. </p> <p> It's not a disastrous mistake, so when I later discovered it, I easily rectified it. </p> <h3 id="301838c8419640358d52babb6d8e04b8"> Implementation outline <a href="#301838c8419640358d52babb6d8e04b8">#</a> </h3> <p> To move on, I added another test case: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;++++++++++++++++++++++++++++++++.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;32&nbsp;increments;&nbsp;ASCII&nbsp;32&nbsp;is&nbsp;space</span> [InlineData(<span style="color:#a31515;">&quot;+++++++++++++++++++++++++++++++++.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;33&nbsp;increments;&nbsp;ASCII&nbsp;32&nbsp;is&nbsp;!</span> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;StringWriter(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;BrainfuckInterpreter(output); &nbsp;&nbsp;&nbsp;&nbsp;sut.Run(program); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;output.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> The only change is the addition of the second <code>[InlineData]</code> attribute, which supplies a slightly different Brainfuck program. This one has 33 increments, which corresponds to the ASCII character code for an exclamation mark. </p> <p> Notice that I clearly copied and pasted the comment, but forgot to change the last <code>32</code> to <code>33</code>. </p> <p> In my eagerness to pass both tests, and because I felt the clock ticking, I made another classic TDD mistake: I took too big a step. At this point, it would have been enough to iterate over the program's characters, count the number of plusses, and convert that number to a character. What I did instead was this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">BrainfuckInterpreter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;StringWriter&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">BrainfuckInterpreter</span>(StringWriter&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.output&nbsp;=&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">imp</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;InterpreterImp(program,&nbsp;output); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imp.Run(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InterpreterImp</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">int</span>&nbsp;programPointer; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;data; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;program; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;StringWriter&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">InterpreterImp</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>,&nbsp;StringWriter&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[30_000]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.program&nbsp;=&nbsp;program; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.output&nbsp;=&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(!IsDone) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InterpretInstruction(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsDone&nbsp;=&gt;&nbsp;program.Length&nbsp;&lt;=&nbsp;programPointer; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">InterpretInstruction</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">instruction</span>&nbsp;=&nbsp;program[programPointer]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">switch</span>&nbsp;(instruction) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;+&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[0]++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;.&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.Write((<span style="color:blue;">char</span>)data[0]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">default</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> With only two test cases, all that code isn't warranted, but I was more focused on implementing an interpreter than on moving in small steps. Even with decades of TDD experience, discipline sometimes slips. Or maybe exactly because of it. </p> <p> Once again, I was fortunate enough that this implementation structure turned out to work all the way, but the point of the TDD process is that you can't always know that. </p> <p> You may wonder why I decided to delegate the work to an inner class. I did that because I expected to have to maintain a <code>programPointer</code> over the actual <code>program</code>, and having a class that interprets <em>one</em> program has better encapsulation. I'll remind the reader than when I use the word <em>encapsulation</em>, I don't necessarily mean <em>information hiding</em>. Usually, I think in terms of <em>contracts</em>: Invariants, pre-, and postconditions. </p> <p> With this design, the <code>program</code> is guaranteed to be present as a class field, since it's <code>readonly</code> and assigned upon initialisation. No <a href="/2013/07/08/defensive-coding">defensive coding</a> is required. </p> <h3 id="630304784a59460e836bd9d773e56b60"> Remaining memory-management instructions <a href="#630304784a59460e836bd9d773e56b60">#</a> </h3> <p> While I wasn't planning on making use of the <a href="/2019/10/07/devils-advocate">Devil's advocate</a> technique, I did leave one little deliberate mistake in the above implementation: I'd hardcoded the data pointer as <code>0</code>. </p> <p> This made it easy to choose the next test case, and the next one after that, and so on. </p> <p> At the two-hour mark, I had these test cases: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;++++++++++++++++++++++++++++++++.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;32&nbsp;increments;&nbsp;ASCII&nbsp;32&nbsp;is&nbsp;space</span> [InlineData(<span style="color:#a31515;">&quot;+++++++++++++++++++++++++++++++++.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;33&nbsp;increments;&nbsp;ASCII&nbsp;32&nbsp;is&nbsp;!</span> [InlineData(<span style="color:#a31515;">&quot;+&gt;++++++++++++++++++++++++++++++++.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;32&nbsp;increments&nbsp;after&nbsp;&gt;;&nbsp;ASCII&nbsp;32&nbsp;is&nbsp;space</span> [InlineData(<span style="color:#a31515;">&quot;+++++++++++++++++++++++++++++++++-.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;33&nbsp;increments&nbsp;and&nbsp;1&nbsp;decrement;&nbsp;ASCII&nbsp;32</span> [InlineData(<span style="color:#a31515;">&quot;&gt;+&lt;++++++++++++++++++++++++++++++++.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;32&nbsp;increments&nbsp;after&nbsp;movement;&nbsp;ASCII&nbsp;32</span> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;StringWriter(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;BrainfuckInterpreter(output); &nbsp;&nbsp;&nbsp;&nbsp;sut.Run(program); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;output.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> And this implementation: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InterpreterImp</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">int</span>&nbsp;programPointer; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">int</span>&nbsp;dataPointer; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;data; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;program; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;StringWriter&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">InterpreterImp</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>,&nbsp;StringWriter&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[30_000]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.program&nbsp;=&nbsp;program; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.output&nbsp;=&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(!IsDone) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InterpretInstruction(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsDone&nbsp;=&gt;&nbsp;program.Length&nbsp;&lt;=&nbsp;programPointer; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">InterpretInstruction</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">instruction</span>&nbsp;=&nbsp;program[programPointer]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">switch</span>&nbsp;(instruction) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;&gt;&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;&lt;&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataPointer--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;+&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[dataPointer]++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;-&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[dataPointer]--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;.&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.Write((<span style="color:blue;">char</span>)data[dataPointer]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">default</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;programPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I'm only showing the inner <code>InterpreterImp</code> class, since I didn't change the outer <code>BrainfuckInterpreter</code> class. </p> <p> At this point, I had used my two hours, but I think that I managed to leave my imaginary audience with a sketch of a possible solution. </p> <h3 id="ff7d802a-14bb-4c1d-bb39-1ecaa9059f03"> Jumps <a href="#ff7d802a-14bb-4c1d-bb39-1ecaa9059f03">#</a> </h3> <p> What remained was the jumping instructions <code>[</code> and <code>]</code>, as well as input. </p> <p> Perhaps I could have kept adding small <code>[InlineData]</code> test cases to my single test method, but I thought I was ready to take on some of the small example programs on the Wikipedia page. I started with the addition example in this manner: </p> <p> <pre>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Copied&nbsp;from&nbsp;https://en.wikipedia.org/wiki/Brainfuck</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;addTwoProgram&nbsp;=&nbsp;<span style="color:maroon;">@&quot; ++&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cell&nbsp;c0&nbsp;=&nbsp;2 &gt;&nbsp;+++++&nbsp;&nbsp;Cell&nbsp;c1&nbsp;=&nbsp;5 [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Start&nbsp;your&nbsp;loops&nbsp;with&nbsp;your&nbsp;cell&nbsp;pointer&nbsp;on&nbsp;the&nbsp;loop&nbsp;counter&nbsp;(c1&nbsp;in&nbsp;our&nbsp;case) &lt;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add&nbsp;1&nbsp;to&nbsp;c0 &gt;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subtract&nbsp;1&nbsp;from&nbsp;c1 ]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End&nbsp;your&nbsp;loops&nbsp;with&nbsp;the&nbsp;cell&nbsp;pointer&nbsp;on&nbsp;the&nbsp;loop&nbsp;counter At&nbsp;this&nbsp;point&nbsp;our&nbsp;program&nbsp;has&nbsp;added&nbsp;5&nbsp;to&nbsp;2&nbsp;leaving&nbsp;7&nbsp;in&nbsp;c0&nbsp;and&nbsp;0&nbsp;in&nbsp;c1 but&nbsp;we&nbsp;cannot&nbsp;output&nbsp;this&nbsp;value&nbsp;to&nbsp;the&nbsp;terminal&nbsp;since&nbsp;it&nbsp;is&nbsp;not&nbsp;ASCII&nbsp;encoded To&nbsp;display&nbsp;the&nbsp;ASCII&nbsp;character&nbsp;&quot;&quot;7&quot;&quot;&nbsp;we&nbsp;must&nbsp;add&nbsp;48&nbsp;to&nbsp;the&nbsp;value&nbsp;7 We&nbsp;use&nbsp;a&nbsp;loop&nbsp;to&nbsp;compute&nbsp;48&nbsp;=&nbsp;6&nbsp;*&nbsp;8 ++++&nbsp;++++&nbsp;&nbsp;c1&nbsp;=&nbsp;8&nbsp;and&nbsp;this&nbsp;will&nbsp;be&nbsp;our&nbsp;loop&nbsp;counter&nbsp;again [ &lt;&nbsp;+++&nbsp;+++&nbsp;&nbsp;Add&nbsp;6&nbsp;to&nbsp;c0 &gt;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subtract&nbsp;1&nbsp;from&nbsp;c1 ] &lt;&nbsp;.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Print&nbsp;out&nbsp;c0&nbsp;which&nbsp;has&nbsp;the&nbsp;value&nbsp;55&nbsp;which&nbsp;translates&nbsp;to&nbsp;&quot;&quot;7&quot;&quot;!&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;[Fact] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddTwoValues</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">input</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;StringReader(<span style="color:#a31515;">&quot;&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;StringWriter(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;BrainfuckInterpreter(input,&nbsp;output); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sut.Run(addTwoProgram); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;output.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:#a31515;">&quot;7&quot;</span>,&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> I got that test passing, added the next example, got that passing, and so on. My final implementation looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">BrainfuckInterpreter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;TextReader&nbsp;input; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;TextWriter&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">BrainfuckInterpreter</span>(TextReader&nbsp;<span style="font-weight:bold;color:#1f377f;">input</span>,&nbsp;TextWriter&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.input&nbsp;=&nbsp;input; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.output&nbsp;=&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">imp</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;InterpreterImp(program,&nbsp;input,&nbsp;output); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imp.Run(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InterpreterImp</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">int</span>&nbsp;instructionPointer; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">int</span>&nbsp;dataPointer; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;data; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;program; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;TextReader&nbsp;input; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;TextWriter&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">InterpreterImp</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">program</span>,&nbsp;TextReader&nbsp;<span style="font-weight:bold;color:#1f377f;">input</span>,&nbsp;TextWriter&nbsp;<span style="font-weight:bold;color:#1f377f;">output</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[30_000]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.program&nbsp;=&nbsp;program; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.input&nbsp;=&nbsp;input; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.output&nbsp;=&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(!IsDone) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InterpretInstruction(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsDone&nbsp;=&gt;&nbsp;program.Length&nbsp;&lt;=&nbsp;instructionPointer; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">InterpretInstruction</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WrapDataPointer(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">instruction</span>&nbsp;=&nbsp;program[instructionPointer]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">switch</span>&nbsp;(instruction) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;&gt;&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;&lt;&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataPointer--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;+&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[dataPointer]++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;-&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[dataPointer]--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;.&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.Write((<span style="color:blue;">char</span>)data[dataPointer]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;,&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[dataPointer]&nbsp;=&nbsp;(<span style="color:blue;">byte</span>)input.Read(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;[&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(data[dataPointer]&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MoveToMatchingClose(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">case</span>&nbsp;<span style="color:#a31515;">&#39;]&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(data[dataPointer]&nbsp;!=&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MoveToMatchingOpen(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">default</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WrapDataPointer</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(dataPointer&nbsp;==&nbsp;-1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataPointer&nbsp;=&nbsp;data.Length&nbsp;-&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(dataPointer&nbsp;==&nbsp;data.Length) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataPointer&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">MoveToMatchingClose</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">nestingLevel</span>&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(0&nbsp;&lt;&nbsp;nestingLevel) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(program[instructionPointer]&nbsp;==&nbsp;<span style="color:#a31515;">&#39;[&#39;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nestingLevel++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(program[instructionPointer]&nbsp;==&nbsp;<span style="color:#a31515;">&#39;]&#39;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nestingLevel--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">MoveToMatchingOpen</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">nestingLevel</span>&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(0&nbsp;&lt;&nbsp;nestingLevel) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(program[instructionPointer]&nbsp;==&nbsp;<span style="color:#a31515;">&#39;]&#39;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nestingLevel++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(program[instructionPointer]&nbsp;==&nbsp;<span style="color:#a31515;">&#39;[&#39;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nestingLevel--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instructionPointer++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As you can see, I finally discovered that I'd been too concrete when using <code>StringWriter</code>. Now, <code>input</code> is defined as a <a href="https://learn.microsoft.com/dotnet/api/system.io.textreader">TextReader</a>, and <code>output</code> as a <code>TextWriter</code>. </p> <p> When <a href="https://learn.microsoft.com/dotnet/api/system.io.textreader.read">TextReader.Read</a> encounters the end of the input stream, it returns <code>-1</code>, and when you cast that to <code>byte</code>, it becomes <code>255</code>. I admit that I haven't read through the Wikipedia article's <em>ROT13</em> example code to a degree that I understand how it decides to stop processing, but the test passes. </p> <p> I also realised that the Wikipedia article used the term <em>instruction pointer</em>, so I renamed <code>programPointer</code> to <code>instructionPointer</code>. </p> <h3 id="868485959d2a4adfbdaa5e34de5c0484"> Assessment <a href="#868485959d2a4adfbdaa5e34de5c0484">#</a> </h3> <p> Due to the <code>switch/case</code> structure, the <code>InterpretInstruction</code> method has a <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of <em>12</em>, which is more than I recommend in my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> It's not uncommon that <code>switch/case</code> code has high cyclomatic complexity, and this is also a common criticism of the measure. When each <code>case</code> block is as simple as it is here, or delegates to helper methods such as <code>MoveToMatchingClose</code>, you could reasonably argue that the code is still maintainable. </p> <p> <a href="/ref/refactoring">Refactoring</a> lists switch statements as a code smell and suggests better alternatives. Had I followed the kata description's <em>additional constraints</em> to the letter, I should also have made it easy to add new instructions, or rename existing ones. This might suggest that one of <a href="https://martinfowler.com/">Martin Fowler</a>'s refactorings might be in order. </p> <p> That is, however, an entirely different kind of exercise, and I thought that I'd already gotten what I wanted out of the kata. </p> <h3 id="9ed50e79927541a18a53a37d3c810442"> Conclusion <a href="#9ed50e79927541a18a53a37d3c810442">#</a> </h3> <p> At first glance, the Brainfuck language isn't difficult to understand (but onerous to read). Even so, it took me so long time to understand the example code that I almost gave up more than once. Still, once I understood how it worked, the interpreter actually wasn't that hard to write. </p> <p> In retrospect, perhaps I should have structured my code differently. Perhaps I should have used polymorphism instead of a switch statement. Perhaps I should have written the code in a more functional style. Regular readers will at least recognise that the code shown here is uncharacteristically imperative for me. I do, however, try to vary my approach to fit the problem at hand (<em>use the right tool for the job</em>, as the old saw goes), and the Brainfuck language is described in so imperative terms that imperative code seemed like the most fitting style. </p> <p> Now that I understand how Brainfuck works, I might later try to <a href="/2020/01/13/on-doing-katas">do the kata with some other constraints</a>. It might prove interesting. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Decomposing CTFiYH's sample code base https://blog.ploeh.dk/2023/09/04/decomposing-ctfiyhs-sample-code-base 2023-09-04T06:00:00+00:00 Mark Seemann <div id="post"> <p> <em>An experience report.</em> </p> <p> In my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> (CTFiYH) I write in the last chapter: </p> <blockquote> <p> If you've looked at the book's sample code base, you may have noticed that it looks disconcertingly monolithic. If you consider the full code base that includes the integration tests, as [the following figure] illustrates, there are all of three packages[...]. Of those, only one is production code. </p> <p> <img src="/content/binary/ctfiyh-monolith-architecture.png" alt="Three boxes labelled unit tests, integration tests, and REST API."> </p> <p> [Figure caption:] The packages that make up the sample code base. With only a single production package, it reeks of a monolith. </p> <footer><a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, subsection 16.2.1.</footer> </blockquote> <p> Later, after discussing dependency cycles, I conclude: </p> <blockquote> <p> I've been writing F# and Haskell for enough years that I naturally follow the beneficial rules that they enforce. I'm confident that the sample code is nicely decoupled, even though it's packaged as a monolith. But unless you have a similar experience, I recommend that you separate your code base into multiple packages. </p> <footer><a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, subsection 16.2.2.</footer> </blockquote> <p> Usually, you can't write something that cocksure without it backfiring, but I was really, really confident that this was the case. Still, it's always nagged me, because I believe that I should walk the walk rather than talk the talk. And I do admit that this was one of the few claims in the book that I actually didn't have code to back up. </p> <p> So I decided to spend part of a weekend to verify that what I wrote was true. You won't believe what happened next. </p> <h3 id="850effbb523248579dd4c382ed75f923"> Decomposition <a href="#850effbb523248579dd4c382ed75f923">#</a> </h3> <p> Reader, I was right all along. </p> <p> I stopped my experiment when my package graph looked like this: </p> <p> <img src="/content/binary/ctfiyh-decomposed-architecture.png" alt="Ports-and-adapters architecture diagram."> </p> <p> Does that look familiar? It should; it's a poster example of <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">Ports and Adapters</a>, or, if you will, <a href="/ref/clean-architecture">Clean Architecture</a>. Notice how all dependencies point inward, following the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>. </p> <p> The Domain Model has no dependencies, while the HTTP Model (Application Layer) only depends on the Domain Model. The outer layer contains the ports and adapters, as well as the <a href="/2011/07/28/CompositionRoot">Composition Root</a>. The Web Host is a small web project that composes everything else. In order to do that, it must reference everything else, either directly (SMTP, SQL, HTTP Model) or transitively (Domain Model). </p> <p> The <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapters</a>, on the other hand, depend on the HTTP Model and (not shown) the SDKs that they adapt. The <code>SqlReservationsRepository</code> class, for example, is implemented in the SQL library, adapting the <code>System.Data.SqlClient</code> SDK to look like an <code>IReservationsRepository</code>, which is defined in the HTTP Model. </p> <p> The SMTP library is similar. It contains a concrete implementation called <code>SmtpPostOffice</code> that adapts the <code>System.Net.Mail</code> API to look like an <code>IPostOffice</code> object. Once again, the <code>IPostOffice</code> interface is defined in the HTTP Model. </p> <p> The above figure is not to scale. In reality, the outer ring is quite thin. The SQL library contains only <code>SqlReservationsRepository</code> and some supporting text files with SQL <a href="https://en.wikipedia.org/wiki/Data_definition_language">DDL</a> definitions. The SMTP library contains only the <code>SmtpPostOffice</code> class. And the Web Host contains <code>Program</code>, <code>Startup</code>, and a few configuration file <a href="https://en.wikipedia.org/wiki/Data_transfer_object">DTOs</a> (<em>options</em>, in ASP.NET parlance). </p> <h3 id="813610f5bf6446cfb1daaa22423df14c"> Application layer <a href="#813610f5bf6446cfb1daaa22423df14c">#</a> </h3> <p> The majority of code, at least if I use a rough proxy measure like number of files, is in the HTTP Model. I often think of this as the <em>application layer</em>, because it's all the logic that's specific to to the application, in contrast to the Domain Model, which ought to contain code that can be used in a variety of application contexts (REST API, web site, batch job, etc.). </p> <p> In this particular, case the application is a REST API, and it turns out that while the Domain Model isn't trivial, more goes on making sure that the REST API behaves correctly: That it returns correctly formatted data, that it <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">validates input</a>, that it <a href="/2020/11/09/checking-signed-urls-with-aspnet">detects attempts at tampering with URLs</a>, that it <a href="/2020/11/16/redirect-legacy-urls">redirects legacy URLs</a>, etc. </p> <p> This layer also contains the <a href="https://stackoverflow.blog/2022/01/03/favor-real-dependencies-for-unit-testing/">interfaces for the application's real dependencies</a>: <code>IReservationsRepository</code>, <code>IPostOffice</code>, <code>IRestaurantDatabase</code>, and <code>IClock</code>. This explains why the SQL and SMTP packages need to reference the HTTP Model. </p> <p> If you have bought the book, you have access to its example code base, and while it's a Git repository, this particular experiment isn't included. After all, I just made it, two years after finishing the book. Thus, if you want to compare with the code that comes with the book, here's a list of all the files I moved to the HTTP Model package: </p> <ul> <li>AccessControlList.cs</li> <li>CalendarController.cs</li> <li>CalendarDto.cs</li> <li>Day.cs</li> <li>DayDto.cs</li> <li>DtoConversions.cs</li> <li>EmailingReservationsRepository.cs</li> <li>Grandfather.cs</li> <li>HomeController.cs</li> <li>HomeDto.cs</li> <li>Hypertext.cs</li> <li>IClock.cs</li> <li>InMemoryRestaurantDatabase.cs</li> <li>IPeriod.cs</li> <li>IPeriodVisitor.cs</li> <li>IPostOffice.cs</li> <li>IReservationsRepository.cs</li> <li>IRestaurantDatabase.cs</li> <li>Iso8601.cs</li> <li>LinkDto.cs</li> <li>LinksFilter.cs</li> <li>LoggingClock.cs</li> <li>LoggingPostOffice.cs</li> <li>LoggingReservationsRepository.cs</li> <li>Month.cs</li> <li>NullPostOffice.cs</li> <li>Period.cs</li> <li>ReservationDto.cs</li> <li>ReservationsController.cs</li> <li>ReservationsRepository.cs</li> <li>RestaurantDto.cs</li> <li>RestaurantsController.cs</li> <li>ScheduleController.cs</li> <li>SigningUrlHelper.cs</li> <li>SigningUrlHelperFactory.cs</li> <li>SystemClock.cs</li> <li>TimeDto.cs</li> <li>UrlBuilder.cs</li> <li>UrlIntegrityFilter.cs</li> <li>Year.cs</li> </ul> <p> As you can see, this package contains the Controllers, the DTOs, the interfaces, and some REST- and HTTP-specific code such as <a href="/2020/11/02/signing-urls-with-aspnet">SigningUrlHelper</a>, <a href="/2020/11/09/checking-signed-urls-with-aspnet">UrlIntegrityFilter</a>, <a href="/2020/08/24/adding-rest-links-as-a-cross-cutting-concern">LinksFilter</a>, security, <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> formatters, etc. </p> <h3 id="eace4360f69240c0b8077a3c1e236473"> Domain Model <a href="#eace4360f69240c0b8077a3c1e236473">#</a> </h3> <p> The Domain Model is small, but not insignificant. Perhaps the most striking quality of it is that (with a single, inconsequential exception) it contains no interfaces. There are no polymorphic types that model application dependencies such as databases, web services, messaging systems, or the system clock. Those are all the purview of the application layer. </p> <p> As the book describes, the architecture is <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">functional core, imperative shell</a>, and since <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">dependencies make everything impure</a>, you can't have those in your functional core. While, with a language like C#, <a href="/2020/02/24/discerning-and-maintaining-purity">you can never be sure that a function truly is pure</a>, I believe that the entire Domain Model is <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>. </p> <p> For those readers who have the book's sample code base, here's a list of the files I moved to the Domain Model: </p> <ul> <li>Email.cs</li> <li>ITableVisitor.cs</li> <li>MaitreD.cs</li> <li>Name.cs</li> <li>Reservation.cs</li> <li>ReservationsVisitor.cs</li> <li>Restaurant.cs</li> <li>Seating.cs</li> <li>Table.cs</li> <li>TimeOfDay.cs</li> <li>TimeSlot.cs</li> </ul> <p> If the entire Domain Model consists of immutable values and <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>, and if impure dependencies make everything impure, what's the <code>ITableVisitor</code> interface doing there? </p> <p> This interface doesn't model any external application dependency, but rather represents <a href="/2018/06/25/visitor-as-a-sum-type">a sum type with the Visitor pattern</a>. The interface looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ITableVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="font-weight:bold;color:#74531f;">VisitStandard</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seats</span>,&nbsp;Reservation?&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="font-weight:bold;color:#74531f;">VisitCommunal</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seats</span>,&nbsp;IReadOnlyCollection&lt;Reservation&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>); }</pre> </p> <p> Restaurant tables are modelled this way because the Domain Model distinguishes between two fundamentally different kinds of tables: Normal restaurant tables, and communal or shared tables. In <a href="https://fsharp.org/">F#</a> or <a href="https://www.haskell.org/">Haskell</a> such a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> would be a one-liner, but in C# you need to use either the <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor pattern</a> or a <a href="/2018/05/22/church-encoding">Church encoding</a>. For the book, I chose the Visitor pattern in order to keep the code base as object-oriented as possible. </p> <h3 id="c76f92f5413246958cfd31a6edc44845"> Circular dependencies <a href="#c76f92f5413246958cfd31a6edc44845">#</a> </h3> <p> In the book I wrote: </p> <blockquote> <p> The passive prevention of cycles [that comes from separate packages] is worth the extra complexity. Unless team members have extensive experience with a language that prevents cycles, I recommend this style of architecture. </p> <p> Such languages do exist, though. F# famously prevents cycles. In it, you can't use a piece of code unless it's already defined above. Newcomers to the language see this as a terrible flaw, but it's actually one of its <a href="https://fsharpforfunandprofit.com/posts/cycles-and-modularity-in-the-wild/">best</a> <a href="http://evelinag.com/blog/2014/06-09-comparing-dependency-networks/">features</a>. </p> <p> Haskell takes a different approach, but ultimately, its explicit treatment of side effects at the type level <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">steers you towards a ports-and-adapters-style architecture</a>. Your code simply doesn't compile otherwise! </p> <p> I've been writing F# and Haskell for enough years that I naturally follow the beneficial rules that they enforce. I'm confident that the sample code is nicely decoupled, even though it's packaged as a monolith. But unless you have a similar experience, I recommend that you separate your code base into multiple packages. </p> <footer><a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, subsection 16.2.2.</footer> </blockquote> <p> As so far demonstrated in this article, I'm now sufficiently conditioned to be aware of side effects and non-determinism that I know to avoid them and push them to be boundaries of the application. Even so, it turns out that it's insidiously easy to introduce small cycles when the language doesn't stop you. </p> <p> This wasn't much of a problem in the Domain Model, but one small example may still illustrate how easy it is to let your guard down. In the Domain Model, I'd added a class called <code>TimeOfDay</code> (since this code base predates <a href="https://learn.microsoft.com/dotnet/api/system.timeonly">TimeOnly</a>), but without thinking much of it, I'd added this method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ToIso8601TimeString</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;durationSinceMidnight.ToIso8601TimeString(); }</pre> </p> <p> While this doesn't look like much, formatting a time value as an ISO 8601 value isn't a Domain concern. It's an application boundary concern, so belongs in the HTTP Model. And sure enough, once I moved the file that contained the ISO 8601 conversions to the HTTP Model, the <code>TimeOfDay</code> class no longer compiled. </p> <p> In this case, the fix was easy. I removed the method from the <code>TimeOfDay</code> class, but added an extension method to the other ISO 8601 conversions: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ToIso8601TimeString</span>(<span style="color:blue;">this</span>&nbsp;TimeOfDay&nbsp;<span style="font-weight:bold;color:#1f377f;">timeOfDay</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;((TimeSpan)timeOfDay).ToIso8601TimeString(); }</pre> </p> <p> While I had little trouble moving the files to the Domain Model one-by-one, once I started moving files to the HTTP Model, it turned out that this part of the code base contained more coupling. </p> <p> Since I had made many classes and interfaces <a href="/2021/03/01/pendulum-swing-internal-by-default">internal by default</a>, once I started moving types to the HTTP Model, I was faced with either making them public, or move them en block. Ultimately, I decided to move eighteen files that were transitively linked to each other in one go. I could have moved them in smaller chunks, but that would have made the <code>internal</code> types invisible to the code that (temporarily) stayed behind. I decided to move them all at once. After all, while I prefer to <a href="https://stackoverflow.blog/2022/12/19/use-git-tactically/">move in small, controlled steps</a>, even moving eighteen files isn't that big an operation. </p> <p> In the end, I still had to make <code>LinksFilter</code>, <code>UrlIntegrityFilter</code>, <code>SigningUrlHelperFactory</code>, and <code>AccessControlList.FromUser</code> public, because I needed to reference them from the Web Host. </p> <h3 id="f3da2544f60b4e7f8abc441e3ddaaa7f"> Test dependencies <a href="#f3da2544f60b4e7f8abc441e3ddaaa7f">#</a> </h3> <p> You may have noticed that in the above diagram, it doesn't look as though I separated the two test packages into more packages, and that is, indeed, the case. I've recently described <a href="/2023/07/31/test-driving-the-pyramids-top">how I think about distinguishing kinds of tests</a>, and I don't really care too much whether an automated test exercises only a single function, or a whole bundle of objects. What I do care about is whether a test is simple or complex, fast or slow. That kind of thing. </p> <p> The package labelled "Integration tests" on the diagram is really a small test library that exercises some SQL Server-specific behaviour. Some of the tests in it verify that certain race conditions don't occur. They do that by keep trying to make the race condition occur, until they time out. Since the timeout is 30 seconds per test, this test suite is <em>slow</em>. That's the reason it's a separate library, even though it contains only eight tests. The book contains more details. </p> <p> The "Unit tests" package contains the bulk of the tests: 176 tests, <a href="/2021/02/15/when-properties-are-easier-than-examples">some of which</a> are <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> properties that each run a hundred test cases. </p> <p> Some tests are <a href="/2021/01/25/self-hosted-integration-tests-in-aspnet">self-hosted integration tests</a> that rely on the Web Host, and some of them are more 'traditional' unit tests. Dependencies are transitive, so I drew an arrow from the "Unit tests" package to the Web Host. Some unit tests exercise objects in the HTTP Model, and some exercise the Domain Model. </p> <p> You may have another question: If the Integration tests reference the SQL package, then why not the SMTP package? Why is it okay that the unit tests reference the SMTP package? </p> <p> Again, I want to reiterate that the reason I distinguished between these two test packages were related to execution speed rather than architecture. The few SMTP tests are fast enough, so there's no reason to keep them in a separate package. </p> <p> In fact, the SMTP tests don't exercise that the <code>SmtpPostOffice</code> can send email. Rather, I treat that class as a <a href="http://xunitpatterns.com/Humble%20Object.html">Humble Object</a>. The few tests that I do have only verify that the system can parse configuration settings: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;m.example.net&quot;</span>,&nbsp;587,&nbsp;<span style="color:#a31515;">&quot;grault&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;garply&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;g@example.org&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;n.example.net&quot;</span>,&nbsp;465,&nbsp;<span style="color:#a31515;">&quot;corge&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;waldo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;c@example.org&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ToPostOfficeReturnsSmtpOffice</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">host</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">port</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userName</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">password</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">from</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;Create.SmtpOptions(host,&nbsp;port,&nbsp;userName,&nbsp;password,&nbsp;from); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.ToPostOffice(db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SmtpPostOffice(host,&nbsp;port,&nbsp;userName,&nbsp;password,&nbsp;from,&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> Notice, by the way, the <a href="/2021/05/03/structural-equality-for-better-tests">use of structural equality on a service</a>. Consider doing that more often. </p> <p> In any case, the separation of automated tests into two packages may not be the final iteration. It's worked well so far, in this context, but it's possible that had things been different, I would have chosen to have more test packages. </p> <h3 id="d61ec0b4390b470ba9b3340be8f902f4"> Conclusion <a href="#d61ec0b4390b470ba9b3340be8f902f4">#</a> </h3> <p> In the book, I made a bold claim: Although I had developed the example code as a monolith, I asserted that I'd been so careful that I could easily tease it apart into multiple packages should I chose to do so. </p> <p> This sounded so much like <a href="https://en.wikipedia.org/wiki/Hubris">hubris</a> that I was trepidatious writing it. I wrote it anyway, because, while I hadn't tried, I was convinced that I was right. </p> <p> Still, it nagged me ever since. What if, after all, I was wrong? I've been wrong before. </p> <p> So I decided to finally make the experiment, and to my relief it turned out that I'd been right. </p> <p> Don't try this at home, kids. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A first crack at the Args kata https://blog.ploeh.dk/2023/08/28/a-first-crack-at-the-args-kata 2023-08-28T07:28:00+00:00 Mark Seemann <div id="post"> <p> <em>Test-driven development in C#.</em> </p> <p> A customer hired me to swing by to demonstrate test-driven development and <a href="https://stackoverflow.blog/2022/12/19/use-git-tactically/">tactical Git</a>. To make things interesting, we agreed that they'd give me a <a href="https://en.wikipedia.org/wiki/Kata_(programming)">kata</a> at the beginning of the session. I didn't know which problem they'd give me, so I thought it'd be a good idea to come prepared. I decided to seek out katas that I hadn't done before. </p> <p> The demonstration session was supposed to be two hours in front of a participating audience. In order to make my preparation aligned to that situation, I decided to impose a two-hour time limit to see how far I could get. At the same time, I'd also keep an eye on didactics, so preferably proceeding in an order that would be explainable to an audience. </p> <p> Some katas are more complicated than others, so I'm under no illusion that I can complete any, to me unknown, kata in under two hours. My success criterion for the time limit is that I'd like to reach a point that would satisfy an audience. Even if, after two hours, I don't reach a complete solution, I should leave a creative and intelligent audience with a good idea of how to proceed. </p> <p> The first kata I decided to try was the <a href="https://codingdojo.org/kata/Args/">Args kata</a>. In this article, I'll describe some of the most interesting things that happened along the way. If you want all the details, the code is <a href="https://github.com/ploeh/ArgsCSharp">available on GitHub</a>. </p> <h3 id="e51a29225774493ca6b20b6dde4c0f3e"> Boolean parser <a href="#e51a29225774493ca6b20b6dde4c0f3e">#</a> </h3> <p> In short, the goal of the Args kata is to develop an API for parsing command-line arguments. </p> <p> When you encounter a new problem, it's common to have a few false starts until you develop a promising plan. This happened to me as well, but after a few attempts that I quickly stashed, I realised that this is really a validation problem - as in <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">parse, don't validate</a>. </p> <p> The first thing I did after that realisation was to copy verbatim the <code>Validated</code> code from <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">An applicative reservation validation example in C#</a>. I consider it fair game to reuse general-purpose code like this for a kata. </p> <p> With that basic building block available, I decided to start with a parser that would handle Boolean flags. My reasoning was that this might be the simplest parser, since it doesn't have many failure modes. If the flag is present, the value should be interpreted to be <code>true</code>; otherwise, <code>false</code>. </p> <p> Over a series of iterations, I developed this parametrised <a href="https://xunit.net/">xUnit.net</a> test: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&#39;l&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;-l&quot;</span>,&nbsp;<span style="color:blue;">true</span>)] [InlineData(<span style="color:#a31515;">&#39;l&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;&nbsp;-l&nbsp;&quot;</span>,&nbsp;<span style="color:blue;">true</span>)] [InlineData(<span style="color:#a31515;">&#39;l&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;-l&nbsp;-p&nbsp;8080&nbsp;-d&nbsp;/usr/logs&quot;</span>,&nbsp;<span style="color:blue;">true</span>)] [InlineData(<span style="color:#a31515;">&#39;l&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;-p&nbsp;8080&nbsp;-l&nbsp;-d&nbsp;/usr/logs&quot;</span>,&nbsp;<span style="color:blue;">true</span>)] [InlineData(<span style="color:#a31515;">&#39;l&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;-p&nbsp;8080&nbsp;-d&nbsp;/usr/logs&quot;</span>,&nbsp;<span style="color:blue;">false</span>)] [InlineData(<span style="color:#a31515;">&#39;l&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;-l&nbsp;true&quot;</span>,&nbsp;<span style="color:blue;">true</span>)] [InlineData(<span style="color:#a31515;">&#39;l&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;-l&nbsp;false&quot;</span>,&nbsp;<span style="color:blue;">false</span>)] [InlineData(<span style="color:#a31515;">&#39;l&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;nonsense&quot;</span>,&nbsp;<span style="color:blue;">false</span>)] [InlineData(<span style="color:#a31515;">&#39;f&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;-f&quot;</span>,&nbsp;<span style="color:blue;">true</span>)] [InlineData(<span style="color:#a31515;">&#39;f&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:blue;">false</span>)] [InlineData(<span style="color:#a31515;">&#39;f&#39;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:blue;">false</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">TryParseSuccess</span>(<span style="color:blue;">char</span>&nbsp;<span style="color:#1f377f;">flagName</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">candidate</span>,&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;BoolParser(flagName); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.TryParse(candidate); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Validated.Succeed&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;(expected),&nbsp;actual); }</pre> </p> <p> To be clear, this test started as a <code>[Fact]</code> (single, non-parametrised test) that I subsequently converted to a parametrised test, and then added more and more test cases to. </p> <p> The final implementation of <code>BoolParser</code> looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">BoolParser</span>&nbsp;:&nbsp;IParser&lt;<span style="color:blue;">bool</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">char</span>&nbsp;flagName; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">BoolParser</span>(<span style="color:blue;">char</span>&nbsp;<span style="color:#1f377f;">flagName</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.flagName&nbsp;=&nbsp;flagName; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Validated&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#74531f;">TryParse</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">idx</span>&nbsp;=&nbsp;candidate.IndexOf(<span style="color:#a31515;">$&quot;-</span>{flagName}<span style="color:#a31515;">&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(idx&nbsp;&lt;&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">nextFlagIdx</span>&nbsp;=&nbsp;candidate[(idx&nbsp;+&nbsp;2)..].IndexOf(<span style="color:#a31515;">&#39;-&#39;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">bFlag</span>&nbsp;=&nbsp;nextFlagIdx&nbsp;&lt;&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;candidate[(idx&nbsp;+&nbsp;2)..] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;candidate.Substring(idx&nbsp;+&nbsp;2,&nbsp;nextFlagIdx); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(<span style="color:blue;">bool</span>.TryParse(bFlag,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">b</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;(b); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;(<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This may not be the most elegant solution, but it passes all tests. Since I was under time pressure, I didn't want to spend too much time polishing the implementation details. As longs as I'm comfortable with the API design and the test cases, I can always refactor later. (I usually say that <em>later is never</em>, which also turned out to be true this time. On the other hand, it's not that the implementation code is <em>awful</em> in any way. It has a cyclomatic complexity of <em>4</em> and fits within a <a href="/2019/11/04/the-80-24-rule">80 x 20 box</a>. It could be much worse.) </p> <p> The <code>IParser</code> interface came afterwards. It wasn't driven by the above test, but by later developments. </p> <h3 id="2d94e2103d6f405f89e4bd0b4e1f2ff3"> Rough proof of concept <a href="#2d94e2103d6f405f89e4bd0b4e1f2ff3">#</a> </h3> <p> Once I had a passable implementation of <code>BoolParser</code>, I developed a similar <code>IntParser</code> to a degree where it supported a happy path. With two parsers, I had enough building blocks to demonstrate how to combine them. At that point, I also had some 40 minutes left, so it was time to produce something that might look useful. </p> <p> At first, I wanted to demonstrate that it's possible to combine the two parsers, so I wrote this test: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ParseBoolAndIntProofOfConceptRaw</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">args</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;-l&nbsp;-p&nbsp;8080&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">l</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;BoolParser(<span style="color:#a31515;">&#39;l&#39;</span>).TryParse(args).SelectFailure(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;s&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">p</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;IntParser(<span style="color:#a31515;">&#39;p&#39;</span>).TryParse(args).SelectFailure(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;s&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;(<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>)&gt;&nbsp;<span style="color:#1f377f;">createTuple</span>&nbsp;=&nbsp;(<span style="color:#1f377f;">b</span>,&nbsp;<span style="color:#1f377f;">i</span>)&nbsp;=&gt;&nbsp;(b,&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>[]&nbsp;<span style="color:#74531f;">combineErrors</span>(<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">s1</span>,&nbsp;<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">s2</span>)&nbsp;=&gt;&nbsp;s1.Concat(s2).ToArray(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;createTuple.Apply(l,&nbsp;combineErrors).Apply(p,&nbsp;combineErrors); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Validated.Succeed&lt;<span style="color:blue;">string</span>[],&nbsp;(<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>)&gt;((<span style="color:blue;">true</span>,&nbsp;8080)),&nbsp;actual); }</pre> </p> <p> That's really not pretty, and I wouldn't expect an unsuspecting audience to understand what's going on. It doesn't help that C# is inadequate for <a href="/2018/10/01/applicative-functors">applicative functors</a>. While it's possible to implement <a href="/2018/11/05/applicative-validation">applicative validation</a>, the C# API is awkward. (There are ways to make it better than what's on display here, but keep in mind that I came into this exercise unprepared, and had to grab what was closest at hand.) </p> <p> The main point of the above test was only to demonstrate that it's possible to combine two parsers into one. That took me roughly 15 minutes. </p> <p> Armed with that knowledge, I then proceeded to define this base class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ArgsParser</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IParser&lt;T1&gt;&nbsp;parser1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IParser&lt;T2&gt;&nbsp;parser2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ArgsParser</span>(IParser&lt;T1&gt;&nbsp;<span style="color:#1f377f;">parser1</span>,&nbsp;IParser&lt;T2&gt;&nbsp;<span style="color:#1f377f;">parser2</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.parser1&nbsp;=&nbsp;parser1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.parser2&nbsp;=&nbsp;parser2; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Validated&lt;<span style="color:blue;">string</span>[],&nbsp;T&gt;&nbsp;<span style="color:#74531f;">TryParse</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">l</span>&nbsp;=&nbsp;parser1.TryParse(candidate).SelectFailure(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;s&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">p</span>&nbsp;=&nbsp;parser2.TryParse(candidate).SelectFailure(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;s&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;T2,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">create</span>&nbsp;=&nbsp;Create; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;create.Apply(l,&nbsp;CombineErrors).Apply(p,&nbsp;CombineErrors); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;T&nbsp;<span style="color:#74531f;">Create</span>(T1&nbsp;<span style="color:#1f377f;">x1</span>,&nbsp;T2&nbsp;<span style="color:#1f377f;">x2</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>[]&nbsp;<span style="color:#74531f;">CombineErrors</span>(<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">s1</span>,&nbsp;<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">s2</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;s1.Concat(s2).ToArray(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While I'm not a fan of inheritance, this seemed the fasted way to expand on the proof of concept. The class encapsulates the ugly details of the <code>ParseBoolAndIntProofOfConceptRaw</code> test, while leaving just enough room for a derived class: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ProofOfConceptParser</span>&nbsp;:&nbsp;ArgsParser&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;(<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>)&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ProofOfConceptParser</span>()&nbsp;:&nbsp;<span style="color:blue;">base</span>(<span style="color:blue;">new</span>&nbsp;BoolParser(<span style="color:#a31515;">&#39;l&#39;</span>),&nbsp;<span style="color:blue;">new</span>&nbsp;IntParser(<span style="color:#a31515;">&#39;p&#39;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;(<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>)&nbsp;<span style="color:#74531f;">Create</span>(<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">x1</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">x2</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;(x1,&nbsp;x2); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class only defines which parsers to use and how to translate successful results to a single object. Here, because this is still a proof of concept, the resulting object is just a tuple. </p> <p> The corresponding test looks like this: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ParseBoolAndIntProofOfConcept</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ProofOfConceptParser(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.TryParse(<span style="color:#a31515;">&quot;-l&nbsp;-p&nbsp;8080&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Validated.Succeed&lt;<span style="color:blue;">string</span>[],&nbsp;(<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>)&gt;((<span style="color:blue;">true</span>,&nbsp;8080)),&nbsp;actual); }</pre> </p> <p> At this point, I hit the two-hour mark, but I think I managed to produce enough code to convince a hypothetical audience that a complete solution is within grasp. </p> <p> What remained was to </p> <ul> <li>add proper error handling to <code>IntParser</code></li> <li>add a corresponding <code>StringParser</code></li> <li>improve the <code>ArgsParser</code> API</li> <li>add better demo examples of the improved <code>ArgsParser</code> API</li> </ul> <p> While I could leave this as an exercise to the reader, I couldn't just leave the code like that. </p> <h3 id="0a1a987c02f8421785382a6973eccd47"> Finishing the kata <a href="#0a1a987c02f8421785382a6973eccd47">#</a> </h3> <p> For my own satisfaction, I decided to complete the kata, which I did in another hour. </p> <p> Although I had started with an abstract base class, I know <a href="/2018/02/19/abstract-class-isomorphism">how to refactor it to a <code>sealed</code> class with an injected Strategy</a>. I did that for the existing class, and also added one that supports three parsers instead of two: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ArgsParser</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IParser&lt;T1&gt;&nbsp;parser1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IParser&lt;T2&gt;&nbsp;parser2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IParser&lt;T3&gt;&nbsp;parser3; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;T&gt;&nbsp;create; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ArgsParser</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IParser&lt;T1&gt;&nbsp;<span style="color:#1f377f;">parser1</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IParser&lt;T2&gt;&nbsp;<span style="color:#1f377f;">parser2</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IParser&lt;T3&gt;&nbsp;<span style="color:#1f377f;">parser3</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">create</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.parser1&nbsp;=&nbsp;parser1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.parser2&nbsp;=&nbsp;parser2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.parser3&nbsp;=&nbsp;parser3; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.create&nbsp;=&nbsp;create; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Validated&lt;<span style="color:blue;">string</span>[],&nbsp;T&gt;&nbsp;<span style="color:#74531f;">TryParse</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">x1</span>&nbsp;=&nbsp;parser1.TryParse(candidate).SelectFailure(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;s&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">x2</span>&nbsp;=&nbsp;parser2.TryParse(candidate).SelectFailure(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;s&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">x3</span>&nbsp;=&nbsp;parser3.TryParse(candidate).SelectFailure(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;s&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;create &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(x1,&nbsp;CombineErrors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(x2,&nbsp;CombineErrors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(x3,&nbsp;CombineErrors); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>[]&nbsp;<span style="color:#74531f;">CombineErrors</span>(<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">s1</span>,&nbsp;<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">s2</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;s1.Concat(s2).ToArray(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Granted, that's a bit of boilerplate, but if you imagine this as supplied by a reusable library, you only have to write this once. </p> <p> I was now ready to parse the kata's central example, <code>"-l -p 8080 -d /usr/logs"</code>, to a strongly typed value: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">record</span>&nbsp;<span style="color:#2b91af;">TestConfig</span>(<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">DoLog</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">Port</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">Directory</span>); [Theory] [InlineData(<span style="color:#a31515;">&quot;-l&nbsp;-p&nbsp;8080&nbsp;-d&nbsp;/usr/logs&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;-p&nbsp;8080&nbsp;-l&nbsp;-d&nbsp;/usr/logs&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;-d&nbsp;/usr/logs&nbsp;-l&nbsp;-p&nbsp;8080&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;&nbsp;-d&nbsp;&nbsp;/usr/logs&nbsp;&nbsp;-l&nbsp;&nbsp;-p&nbsp;8080&nbsp;&nbsp;&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ParseConfig</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">args</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ArgsParser&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;TestConfig&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;BoolParser(<span style="color:#a31515;">&#39;l&#39;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;IntParser(<span style="color:#a31515;">&#39;p&#39;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;StringParser(<span style="color:#a31515;">&#39;d&#39;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#1f377f;">b</span>,&nbsp;<span style="color:#1f377f;">i</span>,&nbsp;<span style="color:#1f377f;">s</span>)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TestConfig(b,&nbsp;i,&nbsp;s)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.TryParse(args); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Validated.Succeed&lt;<span style="color:blue;">string</span>[],&nbsp;TestConfig&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;TestConfig(<span style="color:blue;">true</span>,&nbsp;8080,&nbsp;<span style="color:#a31515;">&quot;/usr/logs&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual); }</pre> </p> <p> This test parses some variations of the example input into an immutable <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/record">record</a>. </p> <p> What happens if the input is malformed? Here's an example of that: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FailToParseConfig</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ArgsParser&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;TestConfig&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;BoolParser(<span style="color:#a31515;">&#39;l&#39;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;IntParser(<span style="color:#a31515;">&#39;p&#39;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;StringParser(<span style="color:#a31515;">&#39;d&#39;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#1f377f;">b</span>,&nbsp;<span style="color:#1f377f;">i</span>,&nbsp;<span style="color:#1f377f;">s</span>)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TestConfig(b,&nbsp;i,&nbsp;s)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.TryParse(<span style="color:#a31515;">&quot;-p&nbsp;aityaity&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Assert.True(actual.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onFailure:&nbsp;<span style="color:#1f377f;">ss</span>&nbsp;=&gt;&nbsp;ss.Contains(<span style="color:#a31515;">&quot;Expected&nbsp;integer&nbsp;for&nbsp;flag&nbsp;&#39;-p&#39;,&nbsp;but&nbsp;got&nbsp;\&quot;aityaity\&quot;.&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onSuccess:&nbsp;<span style="color:#1f377f;">_</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">false</span>)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.True(actual.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onFailure:&nbsp;<span style="color:#1f377f;">ss</span>&nbsp;=&gt;&nbsp;ss.Contains(<span style="color:#a31515;">&quot;Missing&nbsp;value&nbsp;for&nbsp;flag&nbsp;&#39;-d&#39;.&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onSuccess:&nbsp;<span style="color:#1f377f;">_</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">false</span>)); }</pre> </p> <p> Of particular interest is that, as promised by applicative validation, parsing failures don't short-circuit. The input value <code>"-p aityaity"</code> has two problems, and both are reported by <code>TryParse</code>. </p> <p> At this point I was happy that I had sufficiently demonstrated the viability of the design. I decided to call it a day. </p> <h3 id="0d715ed2918d48c3840fe2f3ac7a6966"> Conclusion <a href="#0d715ed2918d48c3840fe2f3ac7a6966">#</a> </h3> <p> As I did the Args kata, I found it interesting enough to warrant an article. Once I realised that I could use applicative parsing as the basis for the API, the rest followed. </p> <p> There's room for improvement, but while <a href="/2020/01/13/on-doing-katas">doing katas</a> is valuable, there are marginal returns in perfecting the code. Get the main functionality working, learn from it, and move on to another exercise. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Compile-time type-checked truth tables https://blog.ploeh.dk/2023/08/21/compile-time-type-checked-truth-tables 2023-08-21T08:07:00+00:00 Mark Seemann <div id="post"> <p> <em>With simple and easy-to-understand examples in F# and Haskell.</em> </p> <p> <a href="https://blog.testdouble.com/authors/eve-ragins/">Eve Ragins</a> recently published an article called <a href="https://blog.testdouble.com/posts/2023-08-14-using-truth-tables/">Why you should use truth tables in your job</a>. It's a good article. You should read it. </p> <p> In it, she outlines how creating a <a href="https://en.wikipedia.org/wiki/Truth_table">Truth Table</a> can help you smoke out edge cases or unclear requirements. </p> <p> I agree, and it also beautifully explains why I find <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> so useful. </p> <p> With languages like <a href="https://fsharp.org/">F#</a> or <a href="https://www.haskell.org/">Haskell</a>, this kind of modelling is part of the language, and you even get statically-typed compile-time checking that tells you whether you've handled all combinations. </p> <p> Eve Ragins points out that there are other, socio-technical benefits from drawing up a truth table that you can, perhaps, print out, or otherwise share with non-technical stakeholders. Thus, the following is in no way meant as a full replacement, but rather as examples of how certain languages have affordances that enable you to think like this while programming. </p> <h3 id="9e4397fd77a043db8043dac6aff81c4a"> F# <a href="#9e4397fd77a043db8043dac6aff81c4a">#</a> </h3> <p> I'm not going to go through Eve Ragins' blow-by-blow walkthrough, explaining how you construct a truth table. Rather, I'm just briefly going to show how simple it is to do the same in F#. </p> <p> Most of the inputs in her example are Boolean values, which already exist in the language, but we need a type for the item status: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ItemStatus&nbsp;=&nbsp;NotAvailable&nbsp;|&nbsp;Available&nbsp;|&nbsp;InUse</pre> </p> <p> As is typical in F#, a type declaration is just a one-liner. </p> <p> Now for something a little more interesting. In Eve Ragins' final table, there's a footnote that says that the dash/minus symbol indicates that the value is irrelevant. If you look a little closer, it turns out that the <code>should_field_be_editable</code> value is irrelevant whenever the <code>should_field_show</code> value is <code>FALSE</code>. </p> <p> So instead of a <code>bool * bool</code> tuple, you really have a three-state type like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;FieldState&nbsp;=&nbsp;Hidden&nbsp;|&nbsp;ReadOnly&nbsp;|&nbsp;ReadWrite</pre> </p> <p> It would probably have taken a few iterations to learn this if you'd jumped straight into pattern matching in F#, but since F# requires you to define types and functions before you can use them, I list the type now. </p> <p> That's all you need to produce a truth table in F#: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;decide&nbsp;requiresApproval&nbsp;canUserApprove&nbsp;itemStatus&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;requiresApproval,&nbsp;canUserApprove,&nbsp;itemStatus&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;NotAvailable&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Hidden &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;NotAvailable&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Hidden &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;NotAvailable&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Hidden &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;NotAvailable&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Hidden &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;&nbsp;&nbsp;Available&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ReadWrite &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;&nbsp;&nbsp;Available&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Hidden &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;&nbsp;&nbsp;&nbsp;Available&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ReadOnly &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;&nbsp;&nbsp;&nbsp;Available&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Hidden &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InUse&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ReadOnly &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InUse&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Hidden &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InUse&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ReadOnly &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InUse&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Hidden </pre> </p> <p> I've called the function <code>decide</code> because it wasn't clear to me what else to call it. </p> <p> What's so nice about F# pattern matching is that the compiler can tell if you've missed a combination. If you forget a combination, you get a helpful <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/compiler-messages/fs0025">Incomplete pattern match</a> compiler warning that points out the combination that you missed. </p> <p> And as I argue in my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, you should turn warnings into errors. This would also be helpful in a case like this, since you'd be prevented from forgetting an edge case. </p> <h3 id="e8bce005200f4f3394eed71cffe955d0"> Haskell <a href="#e8bce005200f4f3394eed71cffe955d0">#</a> </h3> <p> You can do the same exercise in Haskell, and the result is strikingly similar: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;ItemStatus&nbsp;=&nbsp;NotAvailable&nbsp;|&nbsp;Available&nbsp;|&nbsp;InUse&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) <span style="color:blue;">data</span>&nbsp;FieldState&nbsp;=&nbsp;Hidden&nbsp;|&nbsp;ReadOnly&nbsp;|&nbsp;ReadWrite&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) <span style="color:#2b91af;">decide</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:#2b91af;">Bool</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ItemStatus</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FieldState</span> decide&nbsp;&nbsp;True&nbsp;&nbsp;True&nbsp;NotAvailable&nbsp;=&nbsp;Hidden decide&nbsp;False&nbsp;&nbsp;True&nbsp;NotAvailable&nbsp;=&nbsp;Hidden decide&nbsp;&nbsp;True&nbsp;False&nbsp;NotAvailable&nbsp;=&nbsp;Hidden decide&nbsp;False&nbsp;False&nbsp;NotAvailable&nbsp;=&nbsp;Hidden decide&nbsp;&nbsp;True&nbsp;&nbsp;True&nbsp;&nbsp;&nbsp;&nbsp;Available&nbsp;=&nbsp;ReadWrite decide&nbsp;False&nbsp;&nbsp;True&nbsp;&nbsp;&nbsp;&nbsp;Available&nbsp;=&nbsp;Hidden decide&nbsp;&nbsp;True&nbsp;False&nbsp;&nbsp;&nbsp;&nbsp;Available&nbsp;=&nbsp;ReadOnly decide&nbsp;False&nbsp;False&nbsp;&nbsp;&nbsp;&nbsp;Available&nbsp;=&nbsp;Hidden decide&nbsp;&nbsp;True&nbsp;&nbsp;True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InUse&nbsp;=&nbsp;ReadOnly decide&nbsp;False&nbsp;&nbsp;True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InUse&nbsp;=&nbsp;Hidden decide&nbsp;&nbsp;True&nbsp;False&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InUse&nbsp;=&nbsp;ReadOnly decide&nbsp;False&nbsp;False&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InUse&nbsp;=&nbsp;Hidden</pre> </p> <p> Just like in F#, if you forget a combination, the compiler will tell you: </p> <p> <pre>LibrarySystem.hs:8:1: <span style="color:red;">warning:</span> [<span style="color:red;">-Wincomplete-patterns</span>] Pattern match(es) are non-exhaustive In an equation for `decide': Patterns of type `Bool', `Bool', `ItemStatus' not matched: False False NotAvailable <span style="color:blue;">|</span> <span style="color:blue;">8 |</span> <span style="color:red;">decide True True NotAvailable = Hidden</span> <span style="color:blue;">|</span> <span style="color:red;">^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...</span></pre> </p> <p> To be clear, that combination is <em>not</em> missing from the above code example. This compiler warning was one I subsequently caused by commenting out a line. </p> <p> It's also possible to turn warnings into errors in Haskell. </p> <h3 id="52ba967712374cd9aa91b21bc61aa8a1"> Conclusion <a href="#52ba967712374cd9aa91b21bc61aa8a1">#</a> </h3> <p> I love languages with algebraic data types because they don't just enable modelling like this, they <em>encourage</em> it. This makes it much easier to write code that handles various special cases that I'd easily overlook in other languages. In languages like F# and Haskell, the compiler will tell you if you forgot to deal with a combination. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Replacing Mock and Stub with a Fake https://blog.ploeh.dk/2023/08/14/replacing-mock-and-stub-with-a-fake 2023-08-14T07:23:00+00:00 Mark Seemann <div id="post"> <p> <em>A simple C# example.</em> </p> <p> A reader recently wrote me about my 2013 article <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Mocks for Commands, Stubs for Queries</a>, commenting that the 'final' code looks suspect. Since it looks like the following, that's hardly an overstatement. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;User&nbsp;<span style="font-weight:bold;color:#74531f;">GetUser</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">u</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.userRepository.Read(userId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(u.Id&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.userRepository.Create(1234); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;u; }</pre> </p> <p> Can you spot what's wrong? </p> <h3 id="a1c674ca5ff049ddac08a7618f95e57b"> Missing test cases <a href="#a1c674ca5ff049ddac08a7618f95e57b">#</a> </h3> <p> You might point out that this example seems to violate <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a>, and probably other design principles as well. I agree that the example is a bit odd, but that's not what I have in mind. </p> <p> The problem with the above example is that while it correctly calls the <code>Read</code> method with the <code>userId</code> parameter, it calls <code>Create</code> with the hardcoded constant <code>1234</code>. It really ought to call <code>Create</code> with <code>userId</code>. </p> <p> Does this mean that the technique that I described in 2013 is wrong? I don't think so. Rather, I left the code in a rather unhelpful state. What I had in mind with that article was the technique I called <em>data flow verification</em>. As soon as I had delivered that message, I was, according to my own goals, done. I wrapped up the article, leaving the code as shown above. </p> <p> As the reader remarked, it's noteworthy that an article about better unit testing leaves the System Under Test (SUT) in an obviously defect state. </p> <p> The short response is that at least one test case is missing. Since this was only demo code to show an example, the entire test suite is this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SomeControllerTests</span> { &nbsp;&nbsp;&nbsp;&nbsp;[Theory] &nbsp;&nbsp;&nbsp;&nbsp;[InlineData(1234)] &nbsp;&nbsp;&nbsp;&nbsp;[InlineData(9876)] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetUserReturnsCorrectValue</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;User(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">td</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IUserRepository&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;td.Setup(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Read(userId)).Returns(expected); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SomeController(td.Object); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.GetUser(userId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[Fact] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">UserIsSavedIfItDoesNotExist</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">td</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IUserRepository&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;td.Setup(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Read(1234)).Returns(<span style="color:blue;">new</span>&nbsp;User&nbsp;{&nbsp;Id&nbsp;=&nbsp;0&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SomeController(td.Object); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sut.GetUser(1234); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;td.Verify(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Create(1234)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> There are three test cases: Two for the parametrised <code>GetUserReturnsCorrectValue</code> method and one test case for the <code>UserIsSavedIfItDoesNotExist</code> test. Since the latter only verifies the hardcoded value <code>1234</code> the <a href="/2019/10/07/devils-advocate">Devil's advocate</a> can get by with using that hardcoded value as well. </p> <h3 id="f0bc9ac185d345c29e61efec2ee7e6b9"> Adding a test case <a href="#f0bc9ac185d345c29e61efec2ee7e6b9">#</a> </h3> <p> The solution to that problem is simple enough. Add another test case by converting <code>UserIsSavedIfItDoesNotExist</code> to a parametrised test: </p> <p> <pre>[Theory] [InlineData(1234)] [InlineData(9876)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">UserIsSavedIfItDoesNotExist</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">td</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IUserRepository&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;td.Setup(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Read(userId)).Returns(<span style="color:blue;">new</span>&nbsp;User&nbsp;{&nbsp;Id&nbsp;=&nbsp;0&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SomeController(td.Object); &nbsp;&nbsp;&nbsp;&nbsp;sut.GetUser(userId); &nbsp;&nbsp;&nbsp;&nbsp;td.Verify(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Create(userId)); }</pre> </p> <p> There's no reason to edit the other test method; this should be enough to elicit a change to the SUT: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;User&nbsp;<span style="font-weight:bold;color:#74531f;">GetUser</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">u</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.userRepository.Read(userId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(u.Id&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.userRepository.Create(userId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;u; }</pre> </p> <p> When you use <a href="http://xunitpatterns.com/Mock%20Object.html">Mocks</a> (or, rather, <a href="http://xunitpatterns.com/Test%20Spy.html">Spies</a>) and <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a> the Data Flow Verification technique is useful. </p> <p> On the other hand, I no longer use Spies or Stubs since <a href="/2022/10/17/stubs-and-mocks-break-encapsulation">they tend to break encapsulation</a>. </p> <h3 id="0864ba24b8a841828c3aaba0bc84877c"> Fake <a href="#0864ba24b8a841828c3aaba0bc84877c">#</a> </h3> <p> These days, I tend to <a href="https://stackoverflow.blog/2022/01/03/favor-real-dependencies-for-unit-testing/">only model real application dependencies as Test Doubles</a>, and when I do, I use <a href="http://xunitpatterns.com/Fake%20Object.html">Fakes</a>. </p> <p> <img src="/content/binary/dos-equis-fakes.jpg" alt="Dos Equis meme with the text: I don't always use Test Doubles, but when I do, I use Fakes."> </p> <p> While the article series <a href="/2019/02/18/from-interaction-based-to-state-based-testing">From interaction-based to state-based testing</a> goes into more details, I think that this small example is a good opportunity to demonstrate the technique. </p> <p> The <code>IUserRepository</code> interface is defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;<span style="font-weight:bold;color:#74531f;">Read</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>); }</pre> </p> <p> A typical Fake is an in-memory collection: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeUserRepository</span>&nbsp;:&nbsp;Collection&lt;User&gt;,&nbsp;IUserRepository { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;User&nbsp;{&nbsp;Id&nbsp;=&nbsp;userId&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;User&nbsp;<span style="font-weight:bold;color:#74531f;">Read</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">user</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.SingleOrDefault(<span style="font-weight:bold;color:#1f377f;">u</span>&nbsp;=&gt;&nbsp;u.Id&nbsp;==&nbsp;userId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(user&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;User&nbsp;{&nbsp;Id&nbsp;=&nbsp;0&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;user; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In my experience, they're typically easy to implement by inheriting from a collection base class. Such an object exhibits typical traits of a Fake object: It fulfils the implied contract, but it lacks some of the 'ilities'. </p> <p> The contract of a Repository is typically that if you add an Entity, you'd expect to be able to retrieve it later. If the Repository offers a <code>Delete</code> method (this one doesn't), you'd expect the deleted Entity to be gone, so that you <em>can't</em> retrieve it. And so on. The <code>FakeUserRepository</code> class fulfils such a contract. </p> <p> On the other hand, you'd also expect a proper Repository implementation to support more than that: </p> <ul> <li>You'd expect a proper implementation to persist data so that you can reboot or change computers without losing data.</li> <li>You'd expect a proper implementation to correctly handle multiple threads.</li> <li>You <em>may</em> expect a proper implementation to support <a href="https://en.wikipedia.org/wiki/ACID">ACID</a> transactions.</li> </ul> <p> The <code>FakeUserRepository</code> does none of that, but in the context of a unit test, it doesn't matter. The data exists as long as the object exists, and that's until it goes out of scope. As long as a test needs the Repository, it remains in scope, and the data is there. </p> <p> Likewise, each test runs in a single thread. Even when tests run in parallel, each test has its own Fake object, so there's no shared state. Therefore, even though <code>FakeUserRepository</code> isn't thread-safe, it doesn't have to be. </p> <h3 id="a479805231124c1e8d74856a2cce2762"> Testing with the Fake <a href="#a479805231124c1e8d74856a2cce2762">#</a> </h3> <p> You can now rewrite the tests to use <code>FakeUserRepository</code>: </p> <p> <pre>[Theory] [InlineData(1234)] [InlineData(9876)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetUserReturnsCorrectValue</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;User&nbsp;{&nbsp;Id&nbsp;=&nbsp;userId&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeUserRepository&nbsp;{&nbsp;expected&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SomeController(db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.GetUser(userId); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); } [Theory] [InlineData(1234)] [InlineData(9876)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">UserIsSavedIfItDoesNotExist</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userId</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeUserRepository(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SomeController(db); &nbsp;&nbsp;&nbsp;&nbsp;sut.GetUser(userId); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Single(db,&nbsp;<span style="font-weight:bold;color:#1f377f;">u</span>&nbsp;=&gt;&nbsp;u.Id&nbsp;==&nbsp;userId); }</pre> </p> <p> Instead of asking a Spy whether or not a particular method was called (which is an implementation detail), the <code>UserIsSavedIfItDoesNotExist</code> test verifies the posterior state of the database. </p> <h3 id="5ce6d826519d44ec905096a2098513ca"> Conclusion <a href="#5ce6d826519d44ec905096a2098513ca">#</a> </h3> <p> In my experience, using Fakes simplifies unit tests. While you may have to edit the Fake implementation from time to time, you edit that code in a single place. The alternative is to edit <em>all</em> affected tests, every time you change something about a dependency. This is also known as <a href="https://en.wikipedia.org/wiki/Shotgun_surgery">Shotgun Surgery</a> and considered an antipattern. </p> <p> The code base that accompanies my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> has more realistic examples of this technique, and much else. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0afe67b375254fe193a3fd10234a1ce9"> <div class="comment-author"><a href="https://github.com/AmiradelBeyg">AmirB</a> <a href="#0afe67b375254fe193a3fd10234a1ce9">#</a></div> <div class="comment-content"> <p> <p> Hi Mark, </p> <p> Firstly, thank you for another insightful article. </p> <p> I'm curious about using Fakes and testing exceptions. In scenarios where dynamic mocks (like Moq) are employed, we can mock a method to throw an exception, allowing us to test the expected behavior of the System Under Test (SUT). In your example, if we were using Moq, we could create a test to mock the UserRepository's Read method to throw a specific exception (e.g., SqlException). This way, we could ensure that the controller responds appropriately, perhaps with an internal server response. However, I'm unsure about how to achieve a similar test using Fakes. Is this type of test suitable for Fakes, or do such tests not align with the intended use of Fakes? Personally, I avoid using try-catch blocks in repositories or controllers and prefer handling exceptions in middleware (e.g., ErrorHandler). In such cases, I write separate unit tests for the middleware. Could this be a more fitting approach? Your guidance would be much appreciated. </p> <p> (And yes, I remember your advice about framing questions —it's in your 'Code that Fits in Your Head' book! :D ) </p> <p> Thanks </p> </p> </div> <div class="comment-date">2024-01-15 03:10 UTC</div> </div> <div class="comment" id="d0ac4c5ecf444fb2a18b993ca16822de"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d0ac4c5ecf444fb2a18b993ca16822de">#</a></div> <div class="comment-content"> <p> Thank you for writing. That's a question that warrants an article or two. I've now published an article titled <a href="/2024/01/29/error-categories-and-category-errors">Error categories and category errors</a>. It's not a direct answer to your question, but I found it useful to first outline my thinking on errors in general. </p> <p> I'll post an update here when I also have an answer to your specific question. </p> </div> <div class="comment-date">2024-01-30 7:13 UTC</div> </div> <div class="comment" id="5844fd8b3ca94d318c5295bc1b3e2c80"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5844fd8b3ca94d318c5295bc1b3e2c80">#</a></div> <div class="comment-content"> <p> AmirB, once again, thank you for writing. I've now published an article titled <a href="/2024/02/26/testing-exceptions">Testing exceptions</a> that attempt to answer your question. </p> </div> <div class="comment-date">2024-02-26 6:57 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. NonEmpty catamorphism https://blog.ploeh.dk/2023/08/07/nonempty-catamorphism 2023-08-07T11:40:00+00:00 Mark Seemann <div id="post"> <p> <em>The universal API for generic non-empty collections, with examples in C# and Haskell.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> I was recently doing some work that required a data structure like a collection, but with the additional constraint that it should be guaranteed to have at least one element. I've known about <a href="https://www.haskell.org/">Haskell</a>'s <a href="https://hackage.haskell.org/package/base/docs/Data-List-NonEmpty.html">NonEmpty</a> type, and <a href="/2017/12/11/semigroups-accumulate">how to port it to C#</a> for years. This time I needed to implement it in a third language, and since I had a little extra time available, I thought it'd be interesting to pursue a conjecture of mine: It seems as though you can implement most (all?) of a generic data structure's API based on its catamorphism. </p> <p> While I could make a guess as to how a catamorphism might look for a non-empty collection, I wasn't sure. A quick web search revealed nothing conclusive, so I decided to deduce it from first principles. As this article series demonstrates, you can derive the catamorphism from a type's isomorphic <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">F-algebra</a>. </p> <p> The beginning of this article presents the catamorphism in C#, with an example. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in Haskell. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <h3 id="1e7c622eea7d4a7bad2edc9958d865ce"> C# catamorphism <a href="#1e7c622eea7d4a7bad2edc9958d865ce">#</a> </h3> <p> This article will use a custom C# class called <code><span style="color:#2b91af;">NonEmptyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code>, which is near-identical to the <code><span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> originally introduced in the article <a href="/2017/12/11/semigroups-accumulate">Semigroups accumulate</a>. </p> <p> I don't know why I originally chose to name the class <code>NotEmptyCollection</code> instead of <code>NonEmptyCollection</code>, but it's annoyed me ever since. I've finally decided to rectify that mistake, so from now on, the name is <code>NonEmptyCollection</code>. </p> <p> The catamorphism for <code>NonEmptyCollection</code> is this instance method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;TResult&nbsp;Aggregate&lt;<span style="color:#2b91af;">TResult</span>&gt;(Func&lt;T,&nbsp;IReadOnlyCollection&lt;T&gt;,&nbsp;TResult&gt;&nbsp;algebra) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;algebra(Head,&nbsp;Tail); }</pre> </p> <p> Because the <code>NonEmptyCollection</code> class is really just a glorified tuple, the <code>algebra</code> is any function which produces a single value from the two constituent values. </p> <p> It's easy to fall into the trap of thinking of the catamorphism as 'reducing' the data structure to a more compact form. While this is a common kind of operation, loss of data is not inevitable. You can, for example, return a new collection, essentially doing nothing: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;nec&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;NonEmptyCollection&lt;<span style="color:blue;">int</span>&gt;(42,&nbsp;1337,&nbsp;2112,&nbsp;666); <span style="color:blue;">var</span>&nbsp;same&nbsp;=&nbsp;nec.Aggregate((x,&nbsp;xs)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;NonEmptyCollection&lt;<span style="color:blue;">int</span>&gt;(x,&nbsp;xs.ToArray()));</pre> </p> <p> This <code>Aggregate</code> method enables you to safely find a maximum value: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;nec&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;NonEmptyCollection&lt;<span style="color:blue;">int</span>&gt;(42,&nbsp;1337,&nbsp;2112,&nbsp;666); <span style="color:blue;">var</span>&nbsp;max&nbsp;=&nbsp;nec.Aggregate((x,&nbsp;xs)&nbsp;=&gt;&nbsp;xs.Aggregate(x,&nbsp;Math.Max));</pre> </p> <p> or to <a href="/2020/02/03/non-exceptional-averages">safely calculate an average</a>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;nec&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;NonEmptyCollection&lt;<span style="color:blue;">int</span>&gt;(42,&nbsp;1337,&nbsp;2112,&nbsp;666); <span style="color:blue;">var</span>&nbsp;average&nbsp;=&nbsp;nec.Aggregate((x,&nbsp;xs)&nbsp;=&gt;&nbsp;xs.Aggregate(x,&nbsp;(a,&nbsp;b)&nbsp;=&gt;&nbsp;a&nbsp;+&nbsp;b)&nbsp;/&nbsp;(xs.Count&nbsp;+&nbsp;1.0));</pre> </p> <p> Both of these two last examples use the built-in <a href="https://learn.microsoft.com/dotnet/api/system.linq.enumerable.aggregate">Aggregate</a> function to accumulate the <code>xs</code>. It uses the overload that takes a seed, for which it supplies <code>x</code>. This means that there's guaranteed to be at least that one value. </p> <p> The catamorphism given here is not unique. You can create a trivial variation by swapping the two function arguments, so that <code>x</code> comes after <code>xs</code>. </p> <h3 id="3b188484128d4cef935a13426d8fb51b"> NonEmpty F-algebra <a href="#3b188484128d4cef935a13426d8fb51b">#</a> </h3> <p> As in the <a href="/2019/05/27/list-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-algebras</a>. </p> <p> As always, start with the underlying endofunctor: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;NonEmptyF&nbsp;a&nbsp;c&nbsp;=&nbsp;NonEmptyF&nbsp;{&nbsp;<span style="color:blue;">head</span>&nbsp;::&nbsp;a,&nbsp;<span style="color:blue;">tail</span>&nbsp;::&nbsp;ListFix&nbsp;a&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">NonEmptyF</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;(NonEmptyF&nbsp;x&nbsp;xs)&nbsp;=&nbsp;NonEmptyF&nbsp;x&nbsp;xs</pre> </p> <p> Instead of using Haskell's standard list (<code>[]</code>) for the tail, I've used <code>ListFix</code> from <a href="/2019/05/27/list-catamorphism">the article on list catamorphism</a>. This should, hopefully, demonstrate how you can build on already established definitions derived from first principles. </p> <p> Since a non-empty collection is really just a glorified tuple of <em>head</em> and <em>tail</em>, there's no recursion, and thus, the carrier type <code>c</code> is not used. You could argue that going through all of these motions is overkill, but it still provides some insights. This is similar to the <a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a> and <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>. </p> <p> The <code>fmap</code> function ignores the mapping argument (often called <code>f</code>), since the <code>Functor</code> instance maps <code>NonEmptyF a c</code> to <code>NonEmptyF a c1</code>, but the <code>c</code> or <code>c1</code> type is not used. </p> <p> As was the case when deducing the recent catamorphisms, Haskell isn't too happy about defining instances for a type like <code>Fix (NonEmptyF a)</code>. To address that problem, you can introduce a <code>newtype</code> wrapper: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;NonEmptyFix&nbsp;a&nbsp;= &nbsp;&nbsp;NonEmptyFix&nbsp;{&nbsp;unNonEmptyFix&nbsp;::&nbsp;Fix&nbsp;(NonEmptyF&nbsp;a)&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> You can define <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, etc. instances for this type without resorting to any funky GHC extensions. Keep in mind that, ultimately, the purpose of all this code is just to figure out what the catamorphism looks like. This code isn't intended for actual use. </p> <p> A helper function makes it easier to define <code>NonEmptyFix</code> values: </p> <p> <pre><span style="color:#2b91af;">createNonEmptyF</span>&nbsp;<span style="color:blue;">::</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;a createNonEmptyF&nbsp;x&nbsp;xs&nbsp;=&nbsp;NonEmptyFix&nbsp;$&nbsp;Fix&nbsp;$&nbsp;NonEmptyF&nbsp;x&nbsp;xs</pre> </p> <p> Here's how to use it: </p> <p> <pre>ghci&gt; createNonEmptyF 42 $ consF 1337 $ consF 2112 nilF NonEmptyFix { unNonEmptyFix = Fix (NonEmptyF 42 (ListFix (Fix (ConsF 1337 (Fix (ConsF 2112 (Fix NilF)))))))}</pre> </p> <p> While this is quite verbose, keep in mind that the code shown here isn't meant to be used in practice. The goal is only to deduce catamorphisms from more basic universal abstractions, and you now have all you need to do that. </p> <h3 id="ccc6b13fcf794ec39f98fdd3e0c61460"> Haskell catamorphism <a href="#ccc6b13fcf794ec39f98fdd3e0c61460">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>NonEmptyF a</code>), and an object <code>c</code>, but you still need to find a morphism <code>NonEmptyF a c -&gt; c</code>. Notice that the algebra you have to find is the function that reduces the functor to its <em>carrier type</em> <code>c</code>, not the 'data type' <code>a</code>. This takes some time to get used to, but that's how catamorphisms work. This doesn't mean, however, that you get to ignore <code>a</code>, as you'll see. </p> <p> As in the previous articles, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>nonEmptyF&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unNonEmptyFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(NonEmptyF&nbsp;x&nbsp;xs)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementation of <code>alg</code>, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from <code>alg</code>? You could pass a function argument to the <code>nonEmptyF</code> function and use it with <code>x</code> and <code>xs</code>: </p> <p> <pre><span style="color:#2b91af;">nonEmptyF</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c nonEmptyF&nbsp;f&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unNonEmptyFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(NonEmptyF&nbsp;x&nbsp;xs)&nbsp;=&nbsp;f&nbsp;x&nbsp;xs</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>NonEmptyF</code>, the compiler infers that the <code>alg</code> function has the type <code>NonEmptyF a c -&gt; c1</code>, which fits the bill, since <code>c</code> may be the same type as <code>c1</code>. </p> <p> This, then, is the catamorphism for a non-empty collection. This one is just a single function. It's still not the only possible catamorphism, since you could trivially flip the arguments to <code>f</code>. </p> <p> I've chosen this representation because the arguments <code>x</code> and <code>xs</code> are defined in the same order as the order of <code>head</code> before <code>tail</code>. Notice how this is the same order as the above C# <code>Aggregate</code> method. </p> <h3 id="2ecc4634c63e40e4a9d47be4bffa4d5f"> Basis <a href="#2ecc4634c63e40e4a9d47be4bffa4d5f">#</a> </h3> <p> You can implement most other useful functionality with <code>nonEmptyF</code>. Here's the <code>Semigroup</code> instance and a useful helper function: </p> <p> <pre><span style="color:#2b91af;">toListFix</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a toListFix&nbsp;=&nbsp;nonEmptyF&nbsp;consF <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;(<span style="color:blue;">NonEmptyFix</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;xs&nbsp;&lt;&gt;&nbsp;ys&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;nonEmptyF&nbsp;(\x&nbsp;xs&#39;&nbsp;-&gt;&nbsp;createNonEmptyF&nbsp;x&nbsp;$&nbsp;xs&#39;&nbsp;&lt;&gt;&nbsp;toListFix&nbsp;ys)&nbsp;xs</pre> </p> <p> The implementation uses <code>nonEmptyF</code> to operate on <code>xs</code>. Inside the lambda expression, it converts <code>ys</code> to a list, and uses <a href="/2019/05/27/list-catamorphism">the <code>ListFix</code> <code>Semigroup</code> instance</a> to concatenate <code>xs</code> with it. </p> <p> Here's the <code>Functor</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;=&nbsp;nonEmptyF&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;createNonEmptyF&nbsp;(f&nbsp;x)&nbsp;$&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;xs)</pre> </p> <p> Like the <code>Semigroup</code> instance, this <code>fmap</code> implementation uses <code>fmap</code> on <code>xs</code>, which is the <code>ListFix</code> <code>Functor</code> instance. </p> <p> The <code>Applicative</code> instance is much harder to write from scratch (or, at least, I couldn't come up with a simpler way): </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;x&nbsp;=&nbsp;createNonEmptyF&nbsp;x&nbsp;nilF &nbsp;&nbsp;liftA2&nbsp;f&nbsp;xs&nbsp;ys&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;nonEmptyF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(\x&nbsp;xs&#39;&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nonEmptyF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(\y&nbsp;ys&#39;&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createNonEmptyF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(f&nbsp;x&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(liftA2&nbsp;f&nbsp;(consF&nbsp;x&nbsp;nilF)&nbsp;ys&#39;&nbsp;&lt;&gt;&nbsp;liftA2&nbsp;f&nbsp;xs&#39;&nbsp;(consF&nbsp;y&nbsp;ys&#39;))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ys) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xs</pre> </p> <p> While that looks complicated, it's not <em>that</em> bad. It uses <code>nonEmptyF</code> to 'loop' over the <code>xs</code>, and then a nested call to <code>nonEmptyF</code> to 'loop' over the <code>ys</code>. The inner lambda expression uses <code>f x y</code> to calculate the head, but it also needs to calculate all other combinations of values in <code>xs</code> and <code>ys</code>. </p> <p> <img src="/content/binary/non-empty-applicative-x-y.png" alt="Boxes labelled x, x1, x2, x3 over other boxes labelled y, y1, y2, y3. The x and y box are connected by an arrow labelled f."> </p> <p> First, it keeps <code>x</code> fixed and 'loops' through all the remaining <code>ys'</code>; that's the <code>liftA2&nbsp;f&nbsp;(consF&nbsp;x&nbsp;nilF)&nbsp;ys&#39;</code> part: </p> <p> <img src="/content/binary/non-empty-applicative-x-ys.png" alt="Boxes labelled x, x1, x2, x3 over other boxes labelled y, y1, y2, y3. The x and y1, y2, y3 boxes are connected by three arrows labelled with a single f."> </p> <p> Then it 'loops' over all the remaining <code>xs'</code> and all the <code>ys</code>; that is, <code>liftA2&nbsp;f&nbsp;xs&#39;&nbsp;(consF&nbsp;y&nbsp;ys&#39;)</code>. </p> <p> <img src="/content/binary/non-empty-applicative-xs-ys.png" alt="Boxes labelled x, x1, x2, x3 over other boxes labelled y, y1, y2, y3. The x1, x2, x3 boxes are connected to the y, y1, y2, y3 boxes by arrows labelled with a single f."> </p> <p> The two <code>liftA2</code> functions apply to the <code>ListFix</code> <code>Applicative</code> instance. </p> <p> You'll be happy to see, I think, that the <code>Monad</code> instance is simpler: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;xs&nbsp;&gt;&gt;=&nbsp;f&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;nonEmptyF&nbsp;(\x&nbsp;xs&#39;&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nonEmptyF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(\y&nbsp;ys&nbsp;-&gt;&nbsp;createNonEmptyF&nbsp;y&nbsp;$&nbsp;ys&nbsp;&lt;&gt;&nbsp;(xs&#39;&nbsp;&gt;&gt;=&nbsp;toListFix&nbsp;.&nbsp;f))&nbsp;(f&nbsp;x))&nbsp;xs</pre> </p> <p> And fortunately, <code>Foldable</code> and <code>Traversable</code> are even simpler: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">foldr</span>&nbsp;f&nbsp;seed&nbsp;=&nbsp;nonEmptyF&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;f&nbsp;x&nbsp;$&nbsp;<span style="color:blue;">foldr</span>&nbsp;f&nbsp;seed&nbsp;xs) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;traverse&nbsp;f&nbsp;=&nbsp;nonEmptyF&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;liftA2&nbsp;createNonEmptyF&nbsp;(f&nbsp;x)&nbsp;(traverse&nbsp;f&nbsp;xs))</pre> </p> <p> Finally, you can implement conversions to and from the <code>NonEmpty</code> type from <code>Data.List.NonEmpty</code>: </p> <p> <pre><span style="color:#2b91af;">toNonEmpty</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">NonEmpty</span>&nbsp;a toNonEmpty&nbsp;=&nbsp;nonEmptyF&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;x&nbsp;:|&nbsp;toList&nbsp;xs) <span style="color:#2b91af;">fromNonEmpty</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">NonEmpty</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">NonEmptyFix</span>&nbsp;a fromNonEmpty&nbsp;(x&nbsp;:|&nbsp;xs)&nbsp;=&nbsp;createNonEmptyF&nbsp;x&nbsp;$&nbsp;fromList&nbsp;xs</pre> </p> <p> This demonstrates that <code>NonEmptyFix</code> is isomorphic to <code>NonEmpty</code>. </p> <h3 id="f57599f8fcbb4b02af85816dff99a790"> Conclusion <a href="#f57599f8fcbb4b02af85816dff99a790">#</a> </h3> <p> The catamorphism for a non-empty collection is a single function that produces a single value from the head and the tail of the collection. While it's possible to implement a 'standard fold' (<code>foldr</code> in Haskell), the non-empty catamorphism doesn't require a seed to get started. The data structure guarantees that there's always at least one value available, and this value can then be use to 'kick off' a fold. </p> <p> In C# one can define the catamorphism as the above <code>Aggregate</code> method. You could then define all other instance functions based on <code>Aggregate</code>. </p> <p> <strong>Next:</strong> <a href="/2019/06/03/either-catamorphism">Either catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Test-driving the pyramid's top https://blog.ploeh.dk/2023/07/31/test-driving-the-pyramids-top 2023-07-31T07:00:00+00:00 Mark Seemann <div id="post"> <p> <em>Some thoughts on TDD related to integration and systems testing.</em> </p> <p> My recent article <a href="/2023/07/17/works-on-most-machines">Works on most machines</a> elicited some responses. Upon reflection, it seems that most of the responses relate to the top of the <a href="https://martinfowler.com/bliki/TestPyramid.html">Test Pyramid</a>. </p> <p> While I don't have an one-shot solution that addresses all concerns, I hope that nonetheless I can suggest some ideas and hopefully inspire a reader or two. That's all. I intend nothing of the following to be prescriptive. I describe my own professional experience: What has worked for me. Perhaps it could also work for you. Use the ideas if they inspire you. Ignore them if you find them impractical. </p> <h3 id="5031910a0b37420bbcea753fc9a31dd0"> The Test Pyramid <a href="#5031910a0b37420bbcea753fc9a31dd0">#</a> </h3> <p> The Test Pyramid is often depicted like this: </p> <p> <img src="/content/binary/standard-test-pyramid.png" alt="Standard Test Pyramid, which is really a triangle with three layers: Unit tests, integration tests, and UI tests."> </p> <p> This seems to indicate that while the majority of tests should be unit tests, you should also have a substantial number of integration tests, and quite a few UI tests. </p> <p> Perhaps the following is obvious, but the Test Pyramid is an idea; it's a way to communicate a concept in a compelling way. What one should take away from it, I think, is only this: The number of tests in each category should form a <a href="https://en.wikipedia.org/wiki/Total_order">total order</a>, where the <em>unit test</em> category is the maximum. In other words, you should have more unit tests than you have tests in the next category, and so on. </p> <p> No-one says that you can only have three levels, or that they have to have the same height. Finally, the above figure isn't even a <a href="https://en.wikipedia.org/wiki/Pyramid_(geometry)">pyramid</a>, but rather a <a href="https://en.wikipedia.org/wiki/Triangle">triangle</a>. </p> <p> I sometimes think of the Test Pyramid like this: </p> <p> <img src="/content/binary/test-pyramid-perspective.png" alt="Test pyramid in perspective."> </p> <p> To be honest, it's not so much whether or not the pyramid is shown in perspective, but rather that the <em>unit test</em> base is significantly more voluminous than the other levels, and that the top is quite small. </p> <h3 id="7b385ee20e7e4054afc625a15525285f"> Levels <a href="#7b385ee20e7e4054afc625a15525285f">#</a> </h3> <p> In order to keep the above discussion as recognisable as possible, I've used the labels <em>unit tests</em>, <em>integration tests</em>, and <em>UI tests</em>. It's easy to get caught up in a discussion about how these terms are defined. Exactly what is a <em>unit test?</em> How does it differ from an <em>integration test?</em> </p> <p> There's no universally accepted definition of a <em>unit test</em>, so it tends to be counter-productive to spend too much time debating the finer points of what to call the tests in each layer. </p> <p> Instead, I find the following criteria useful: </p> <ol> <li>In-process tests</li> <li>Tests that involve more than one process</li> <li>Tests that can only be performed in production</li> </ol> <p> I'll describe each in a little more detail. Along the way, I'll address some of the reactions to <a href="/2023/07/17/works-on-most-machines">Works on most machines</a>. </p> <h3 id="7501caecf9d74c26aa15661fdc7982d7"> In-process tests <a href="#7501caecf9d74c26aa15661fdc7982d7">#</a> </h3> <p> The <em>in-process</em> category corresponds roughly to the Test Pyramid's <em>unit test</em> level. It includes 'traditional' unit tests such as tests of stand-alone functions or methods on objects, but also <a href="/2012/06/27/FacadeTest">Facade Tests</a>. The latter may involve multiple modules or objects, perhaps even from multiple libraries. Many people may call them <em>integration tests</em> because they integrate more than one module. </p> <p> As long as an automated test runs in a single process, in memory, it tends to be fast and leave no persistent state behind. This is almost exclusively the kind of test I tend to test-drive. I often follow an <a href="/outside-in-tdd">outside-in TDD</a> process, an example of which is shown in my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> Consider an example from the source code that accompanies the book: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;ReserveTableAtTheVaticanCellar() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;api&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SelfHostedApi(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;client&nbsp;=&nbsp;api.CreateClient(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;timeOfDayLaterThanLastSeatingAtTheOtherRestaurants&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromHours(21.5); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;=&nbsp;DateTime.Today.AddDays(433).Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeOfDayLaterThanLastSeatingAtTheOtherRestaurants); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;Some.Reservation.WithDate(at).ToDto(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;response&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;client.PostReservation(<span style="color:#a31515;">&quot;The&nbsp;Vatican&nbsp;Cellar&quot;</span>,&nbsp;dto); &nbsp;&nbsp;&nbsp;&nbsp;response.EnsureSuccessStatusCode(); }</pre> </p> <p> I think of a test like this as an automated acceptance test. It uses an internal test-specific domain-specific language (<a href="http://xunitpatterns.com/Test%20Utility%20Method.html">test utilities</a>) to exercise the REST service's API. It uses <a href="/2021/01/25/self-hosted-integration-tests-in-aspnet">ASP.NET self-hosting</a> to run both the service and the HTTP client in the same process. </p> <p> Even though this may, at first glance, look like an integration test, it's an artefact of test-driven development. Since it does cut across both HTTP layer and domain model, some readers may think of it as an integration test. It uses a <a href="/2019/02/18/from-interaction-based-to-state-based-testing">stateful in-memory data store</a>, so it doesn't involve more than a single process. </p> <h3 id="2f036c4726d74c6285b1b0a759a54269"> Tests that span processes <a href="#2f036c4726d74c6285b1b0a759a54269">#</a> </h3> <p> There are aspects of software that you can't easily drive with tests. I'll return to some really gnarly examples in the third category, but in between, we find concerns that are hard, but still possible to test. The reason that they are hard is often because they involve more than one process. </p> <p> The most common example is data access. Many software systems save or retrieve data. With test-driven development, you're supposed to let the tests inform your API design decisions in such a way that everything that involves difficult, error-prone code is factored out of the data access layer, and into another part of the code that <em>can</em> be tested in process. This development technique ought to drain the hard-to-test components of logic, leaving behind a <a href="http://xunitpatterns.com/Humble%20Object.html">Humble Object</a>. </p> <p> One reaction to <a href="/2023/07/17/works-on-most-machines">Works on most machines</a> concerns exactly that idea: </p> <blockquote> <p> "As a developer, you need to test HumbleObject's behavior." </p> <footer><cite><a href="https://twitter.com/ladeak87/status/1680915766764351489">ladeak</a></cite></footer> </blockquote> <p> It's almost tautologically part of the definition of a Humble Object that you're <em>not</em> supposed to test it. Still, realistically, ladeak has a point. </p> <p> When I wrote the example code to <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, I applied the Humble Object pattern to the data access component. For a good while, I had a <code>SqlReservationsRepository</code> class that was so simple, so drained of logic, that it couldn't possibly fail. </p> <p> Until, of course, the inevitable happened: There was a bug in the <code>SqlReservationsRepository</code> code. Not to make a long story out of it, but even with a really low <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a>, I'd accidentally swapped two database columns when reading from a table. </p> <p> Whenever possible, when I discover a bug, I first write an automated test that exposes that bug, and only then do I fix the problem. This is congruent with <a href="/2023/01/23/agilean">my lean bias</a>. If a defect can occur once, it can occur again in the future, so it's better to have a regression test. </p> <p> The problem with this bug is that it was in a Humble Object. So, ladeak is right. Sooner or later, you'll have to test the Humble Object, too. </p> <p> That's when I had to bite the bullet and add a test library that tests against the database. </p> <p> One such test looks like this: </p> <p> <pre>[Theory] [InlineData(Grandfather.Id,&nbsp;<span style="color:#a31515;">&quot;2022-06-29&nbsp;12:00&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;e@example.gov&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Enigma&quot;</span>,&nbsp;1)] [InlineData(Grandfather.Id,&nbsp;<span style="color:#a31515;">&quot;2022-07-27&nbsp;11:40&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;c@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Carlie&quot;</span>,&nbsp;2)] [InlineData(2,&nbsp;<span style="color:#a31515;">&quot;2021-09-03&nbsp;14:32&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bon@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Jovi&quot;</span>,&nbsp;4)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;CreateAndReadRoundTrip( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Guid.NewGuid(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime.Parse(at,&nbsp;CultureInfo.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connectionString&nbsp;=&nbsp;ConnectionStrings.Reservations; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlReservationsRepository(connectionString); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> The entire test runs in a special context where a database is automatically created before the test runs, and torn down once the test has completed. </p> <blockquote> <p> "When building such behavior, you can test against a shared instance of the service in your dev team or run that service on your dev machine in a container." </p> <footer><cite><a href="https://twitter.com/ladeak87/status/1680915837782224905">ladeak</a></cite></footer> </blockquote> <p> Yes, those are two options. A third, in the spirit of <a href="/ref/goos">GOOS</a>, is to strongly favour technologies that support automation. Believe it or not, you can automate <a href="https://en.wikipedia.org/wiki/Microsoft_SQL_Server">SQL Server</a>. You don't need a Docker container for it. That's what I did in the above test. </p> <p> I can see how a Docker container with an external dependency can be useful too, so I'm not trying to dismiss that technology. The point is, however, that simpler alternatives may exist. I, and others, did test-driven development for more than a decade before Docker existed. </p> <h3 id="4884dac317d84c70ac4824a5ee3fe922"> Tests that can only be performed in production <a href="#4884dac317d84c70ac4824a5ee3fe922">#</a> </h3> <p> The last category of tests are those that you can only perform on a production system. What might be examples of that? </p> <p> I've run into a few over the years. One such test is what I call a <a href="https://en.wikipedia.org/wiki/Smoke_testing_(software)">Smoke Test</a>: Metaphorically speaking, turn it on and see if it develops smoke. These kinds of tests are good at catching configuration errors. Does the web server have the right connection string to the database? A test can verify whether that's the case, but it makes no sense to run such a test on a development machine, or against a test system, or a staging environment. You want to verify that the production system is correctly configured. Only a test against the production system can do that. </p> <p> For every configuration value, you may want to consider a Smoke Test. </p> <p> There are other kinds of tests you can only perform in production. Sometimes, it's not technical concerns, but rather legal or financial constraints, that dictate circumstances. </p> <p> A few years ago I worked with a software organisation that, among other things, integrated with the Danish <a href="https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)">personal identification number system (CPR)</a>. Things may have changed since, but back then, an organisation had to have a legal agreement with CPR before being granted access to its integration services. It's an old system (originally from 1968) with a proprietary data integration protocol. </p> <p> We test-drove a parser of the data format, but that still left behind a Humble Object that would actually perform the data transfers. How do we test that Humble Object? </p> <p> Back then, at least, there was no test system for the CPR service, and it was illegal to query the live system unless you had a business reason. And software testing did not constitute a legal reason. </p> <p> The only <em>legal</em> option was to make the Humble Object as simple and foolproof as possible, and then observe how it worked in actual production situations. Containers wouldn't help in such a situation. </p> <p> It's possible to write automated tests against production systems, but unless you're careful, they're difficult to write and maintain. At least, go easy on the assertions, since you can't assume much about the run-time data and behaviour of a live system. Smoke tests are mostly just 'pings', so can be written to be fairly maintenance-free, but you shouldn't need many of them. </p> <p> Other kinds of tests against production are likely to be fragile, so it pays to minimise their number. That's the top of the pyramid. </p> <h3 id="32214fa81f2f4dc188b0990d4308bded"> User interfaces <a href="#32214fa81f2f4dc188b0990d4308bded">#</a> </h3> <p> I no longer develop user interfaces, so take the following with a pinch of salt. </p> <p> The 'original' Test Pyramid that I've depicted above has <em>UI tests</em> at the pyramid's top. That doesn't necessarily match the categories I've outlined here; don't assume parity. </p> <p> A UI test may or may not involve more than one process, but they are often difficult to maintain for other reasons. Perhaps this is where the pyramid metaphor starts to break down. <a href="https://en.wikipedia.org/wiki/All_models_are_wrong">All models are wrong, but some are useful</a>. </p> <p> Back when I still programmed user interfaces, I'd usually test-drive them via a <a href="https://martinfowler.com/bliki/SubcutaneousTest.html">subcutaneous API</a>, and rely on some variation of <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC</a> to keep the rendered controls in sync. Still, once in a while, you need to verify that the user interface looks as it's supposed to. Often, the best tool for that job is the good old Mark I Eyeball. </p> <p> This still means that you need to run the application from time to time. </p> <blockquote> <p> "Docker is also very useful for enabling others to run your software on their machines. Recently, we've been exploring some apps that consisted of ~4 services (web servers) and a database. All of them written in different technologies (PHP, Java, C#). You don't have to setup environment variables. You don't need to have relevant SDKs to build projects etc. Just run docker command, and spin them instantly on your PC." </p> <footer><cite><a href="/2023/07/17/works-on-most-machines#4012c2cddcb64a068c0b06b7989a676e">qfilip</a></cite></footer> </blockquote> <p> That sounds like a reasonable use case. I've never found myself in such circumstances, but I can imagine the utility that containers offer in a situation like that. Here's how I envision the scenario: </p> <p> <img src="/content/binary/app-with-services-in-containers.png" alt="A box with arrows to three other boxes, which again have arrows to a database symbol."> </p> <p> The boxes with rounded corners symbolise containers. </p> <p> Again, my original goal with the <a href="/2023/07/17/works-on-most-machines">previous article</a> wasn't to convince you that container technologies are unequivocally bad. Rather, it was to suggest that test-driven development (TDD) solves many of the problems that people seem to think can only be solved with containers. Since TDD has many other beneficial side effects, it's worth considering instead of mindlessly reaching for containers, which may represent only a local maximum. </p> <p> How could TDD address qfilip's concern? </p> <p> When I test-drive software, I <a href="https://stackoverflow.blog/2022/01/03/favor-real-dependencies-for-unit-testing/">favour real dependencies</a>, and I <a href="/2019/02/18/from-interaction-based-to-state-based-testing">favour Fake objects over Mocks and Stubs</a>. Were I to return to user-interface programming today, I'd define its external dependencies as one or more interfaces, and implement a <a href="http://xunitpatterns.com/Fake%20Object.html">Fake Object</a> for each. </p> <p> Not only will this enable me to simulate the external dependencies with the Fakes. If I implement the Fakes as part of the production code, I'd even be able to spin up the system, using the Fakes instead of the real system. </p> <p> <img src="/content/binary/app-with-fake-dependencies.png" alt="App box with arrows pointing to itself."> </p> <p> A Fake is an implementation that 'almost works'. A common example is an in-memory collection instead of a database. It's neither persistent nor thread-safe, but it's internally consistent. What you add, you can retrieve, until you delete it again. For the purposes of starting the app in order to verify that the user interface looks correct, that should be good enough. </p> <p> Another related example is <a href="https://particular.net/nservicebus">NServiceBus</a>, which comes with a <a href="https://docs.particular.net/transports/learning/">file transport that is clearly labeled as not for production use</a>. While it's called the <em>Learning Transport</em>, it's also useful for exploratory testing on a development machine. While this example clearly makes use of an external resource (the file system), it illustrates how a Fake implementation can alleviate the need for a container. </p> <h3 id="687363d13cf24a569b1dea6a45f8771e"> Uses for containers <a href="#687363d13cf24a569b1dea6a45f8771e">#</a> </h3> <p> Ultimately, it's still useful to be able to stand up an entire system, as qfilip suggests, and if containers is a good way to do that, it doesn't bother me. At the risk of sounding like a broken record, I never intended to say that containers are useless. </p> <p> When I worked as a Software Development Engineer in Microsoft, I had two computers: A laptop and a rather beefy <a href="https://en.wikipedia.org/wiki/Computer_tower">tower PC</a>. I've always done all programming on laptops, so I repurposed the tower as a <a href="https://en.wikipedia.org/wiki/Microsoft_Virtual_Server">virtual server</a> with all my system's components on separate virtual machines (VM). The database in one VM, the application server in another, and so on. I no longer remember what all the components were, but I seem to recall that I had four VMs running on that one box. </p> <p> While I didn't use it much, I found it valuable to occasionally verify that all components could talk to each other on a realistic network topology. This was in 2008, and <a href="https://en.wikipedia.org/wiki/Docker_(software)">Docker wasn't around then</a>, but I could imagine it would have made that task easier. </p> <p> I don't dispute that Docker and <a href="https://en.wikipedia.org/wiki/Kubernetes">Kubernetes</a> are useful, but the job of a software architect is to carefully identify the technologies on which a system should be based. The more technology dependencies you take on, the more rigid the design. </p> <p> After a few decades of programming, my experience is that as a programmer and architect, I can find better alternatives than depending on container technologies. If testers and IT operators find containers useful to do their jobs, then that's fine by me. Since my code <a href="/2023/07/17/works-on-most-machines">works on most machines</a>, it works in containers, too. </p> <h3 id="be02c64c095e474eaa54ab1750a2d471"> Truly Humble Objects <a href="#be02c64c095e474eaa54ab1750a2d471">#</a> </h3> <p> One last response, and I'll wrap this up. </p> <blockquote> <p> "As a developer, you need to test HumbleObject's behavior. What if a DatabaseConnection or a TCP conn to a message queue is down?" </p> <footer><cite><a href="https://twitter.com/ladeak87/status/1680915766764351489">ladeak</a></cite></footer> </blockquote> <p> How should such situations be handled? There may always be special cases, but in general, I can think of two reactions: </p> <ul> <li>Log the error</li> <li>Retry the operation</li> </ul> <p> Assuming that the Humble Object is a polymorphic type (i.e. inherits a base class or implements an interface), you should be able to extract each of these behaviours to general-purpose components. </p> <p> In order to log errors, you can either use a <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> or a global exception handler. Most frameworks provide a way to catch (otherwise) unhandled exceptions, exactly for this purpose, so you don't have to add such functionality to a Humble Object. </p> <p> Retry logic can also be delegated to a third-party component. For .NET I'd start looking at <a href="https://www.thepollyproject.org/">Polly</a>, but I'd be surprised if other platforms don't have similar libraries that implement the stability patterns from <a href="/ref/release-it">Release It</a>. </p> <p> Something more specialised, like a fail-over mechanism, sounds like a good reason to wheel out the <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">Chain of Responsibility</a> pattern. </p> <p> All of these can be tested independently of any Humble Object. </p> <h3 id="ea544f519e0b4114a21bb094d9798c6c"> Conclusion <a href="#ea544f519e0b4114a21bb094d9798c6c">#</a> </h3> <p> In a recent article I reflected on my experience with TDD and speculated that a side effect of that process is code flexible enough to work on most machines. Thus, I've never encountered the need for a containers. </p> <p> Readers responded with comments that struck me as mostly related to the upper levels of the Test Pyramid. In this article, I've attempted to address some of those concerns. I still get by without containers. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Is software getting worse? https://blog.ploeh.dk/2023/07/24/is-software-getting-worse 2023-07-24T06:02:00+00:00 Mark Seemann <div id="post"> <p> <em>A rant, with some examples.</em> </p> <p> I've been a software user for thirty years. </p> <p> My first PC was <a href="https://en.wikipedia.org/wiki/DOS">DOS</a>-based. In my first job, I used <a href="https://en.wikipedia.org/wiki/OS/2">OS/2</a>, in the next, <a href="https://en.wikipedia.org/wiki/Windows_3.1x">Windows 3.11</a>, <a href="https://en.wikipedia.org/wiki/Windows_NT">NT</a>, and later incarnations of Windows. </p> <p> I wrote my first web sites in <a href="https://en.wikipedia.org/wiki/Arachnophilia">Arachnophilia</a>, and my first professional software in Visual Basic, Visual C++, and <a href="https://en.wikipedia.org/wiki/Visual_InterDev">Visual InterDev</a>. </p> <p> I used <a href="https://en.wikipedia.org/wiki/Terminate_(software)">Terminate</a> with my first modem. If I recall correctly, it had a built-in email downloader and offline reader. Later, I switched to Outlook for email. I've used <a href="https://en.wikipedia.org/wiki/Netscape_Navigator">Netscape Navigator</a>, <a href="https://en.wikipedia.org/wiki/Internet_Explorer">Internet Explorer</a>, Firefox, and Chrome to surf the web. </p> <p> I've written theses, articles, reports, etc. in <a href="https://en.wikipedia.org/wiki/WordPerfect">Word Perfect</a> for DOS and MS Word for Windows. I wrote my new book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits In your Head</a> in <a href="https://www.texstudio.org/">TexStudio</a>. Yes, it was written entirely in <a href="https://en.wikipedia.org/wiki/LaTeX">LaTeX</a>. </p> <h3 id="c3ee042288d94295bec33308840d075f"> Updates <a href="#c3ee042288d94295bec33308840d075f">#</a> </h3> <p> For the first fifteen years, new software version were rare. You'd get a version of AutoCAD, Windows, or Visual C++, and you'd use it for years. After a few years, a new version would come out, and that would be a big deal. </p> <p> Interim service releases were rare, too, since there was no network-based delivery mechanism. Software came on <a href="https://en.wikipedia.org/wiki/Floppy_disk">floppy disks</a>, and later on <a href="https://en.wikipedia.org/wiki/Compact_disc">CD</a>s. </p> <p> Even if a bug fix was easy to make, it was difficult for a software vendor to <em>distribute</em> it, so most software releases were well-tested. Granted, software had bugs back then, and some of them you learned to work around. </p> <p> When a new version came out, the same forces were at work. The new version had to be as solid and stable as the previous one. Again, I grant that once in a while, even in those days, this wasn't always the case. Usually, a bad release spelled the demise of a company, because release times were so long that competitors could take advantage of a bad software release. </p> <p> Usually, however, software updates were <em>improvements</em>, and you looked forward to them. </p> <h3 id="865a6ab3b3c64982bbeb31ccf64db6b5"> Decay <a href="#865a6ab3b3c64982bbeb31ccf64db6b5">#</a> </h3> <p> I no longer look forward to updates. These days, software is delivered over the internet, and some applications update automatically. </p> <p> From a security perspective it can be a good idea to stay up-to-date, and for years, I diligently did that. Lately, however, I've become more conservative. Particularly when it comes to Windows, I ignore all suggestions to update it until it literally forces the update on me. </p> <p> Just like <a href="https://tvtropes.org/pmwiki/pmwiki.php/Main/StarTrekMovieCurse">even-numbered Star Trek movies don't suck</a> the same pattern seems to be true for Windows: <a href="https://en.wikipedia.org/wiki/Windows_XP">Windows XP</a> was good, <a href="https://en.wikipedia.org/wiki/Windows_7">Windows 7</a> was good, and <a href="https://en.wikipedia.org/wiki/Windows_10">Windows 10</a> wasn't bad either. I kept putting off Windows 11 for as long as possible, but now I use it, and I can't say that I'm surprised that I don't like it. </p> <p> This article, however, isn't a rant about Windows in particular. This seems to be a general trend, and it's been noticeable for years. </p> <h3 id="f3bd0d4e750b4d9aa53ee9a6c0f59ddc"> Examples <a href="#f3bd0d4e750b4d9aa53ee9a6c0f59ddc">#</a> </h3> <p> I think that the first time I noticed a particular application degrading was <a href="https://en.wikipedia.org/wiki/Vivino">Vivino</a>. It started out as a local company here in Copenhagen, and I was a fairly early adopter. Initially, it was great: If you like wine, but don't know that much about it, you could photograph a bottle's label, and it'd automatically recognise the wine and register it in your 'wine library'. I found it useful that I could look up my notes about a wine I'd had a year ago to remind me what I thought of it. As time went on, however, I started to notice errors in my wine library. It might be double entries, or wines that were silently changed to another vintage, etc. Eventually it got so bad that I lost trust in the application and uninstalled it. </p> <p> Another example is <a href="https://www.sublimetext.com/">Sublime Text</a>, which I used for writing articles for this blog. I even bought a licence for it. Version 3 was great, but version 4 was weird from the outset. One thing was that they changed how they indicated which parts of a file I'd edited after opening it, and I never understood the idea behind the visuals. Worse was that auto-closing of HTML stopped working. Since I'm that <a href="https://rakhim.org/honestly-undefined/19/">weird dude who writes raw HTML</a>, such a feature is quite important to me. If I write an HTML tag, I expect the editor to automatically add the closing tag, and place my cursor between the two. Sublime Text stopped doing that consistently, and eventually it became annoying enough that I though: <em>Why bother?</em> Now I write in <a href="https://code.visualstudio.com/">Visual Studio Code</a>. </p> <p> Microsoft is almost a chapter in itself, but to be fair, I don't consider Microsoft products <em>worse</em> than others. There's just so much of it, and since I've always been working in the Microsoft tech stack, I use a lot of it. Thus, <a href="https://en.wikipedia.org/wiki/Selection_bias">selection bias</a> clearly is at work here. Still, while I don't think Microsoft is worse than the competition, it seems to be part of the trend. </p> <p> For years, my login screen was stuck on the same mountain lake, even though I tried every remedy suggested on the internet. Eventually, however, a new version of Windows fixed the issue. So, granted, sometimes new versions improve things. </p> <p> Now, however, I have another problem with <a href="https://en.wikipedia.org/wiki/Windows_spotlight">Windows Spotlight</a>. It shows nice pictures, and there used to be an option to see where the picture was taken. Since I repaved my machine, this option is gone. Again, I've scoured the internet for resolutions to this problem, but neither rebooting, regedit changes, etc. has so far solved the problem. </p> <p> That sounds like small problems, so let's consider something more serious. Half a year ago, Outlook used to be able to detect whether I was writing an email in English or Danish. It could even handle the hybrid scenario where parts of an email was in English, and parts in Danish. Since I repaved my machine, this feature no longer works. Outlook doesn't recognise Danish when I write it. One thing are the red squiggly lines under most words, but that's not even the worst. The worst part of this is that even though I'm writing in Danish, outlook thinks I'm writing in English, so it silently auto-corrects Danish words to whatever looks adjacent in English. </p> <p> <img src="/content/binary/outlook-language-bug.png" alt="Screen shot of Outlook language bug."> </p> <p> This became so annoying that I contacted Microsoft support about it, but while they had me try a number of things, nothing worked. They eventually had to give up and suggested that I reinstalled my machine - which, at that point, I'd done two weeks before. </p> <p> This used to work, but now it doesn't. </p> <h3 id="b314b021b97f455184e44c52ab584afa"> It's not all bad <a href="#b314b021b97f455184e44c52ab584afa">#</a> </h3> <p> I could go on with other examples, but I think that this suffices. After all, I don't think it makes for a compelling read. </p> <p> Of course, not everything is bad. While it looks as though I'm particularly harping on Microsoft, I rarely detect problems with <a href="https://visualstudio.microsoft.com/">Visual Studio</a> or Code, and I usually install updates as soon as they are available. The same is true for much other software I use. <a href="https://www.getpaint.net/">Paint.NET</a> is awesome, <a href="https://www.getmusicbee.com/">MusicBee</a> is solid, and even the <a href="https://www.sonos.com/">Sonos</a> Windows app, while horrific, is at least consistently so. </p> <h3 id="708ec0a2be1d42f083c1c2d37c24221d"> Conclusion <a href="#708ec0a2be1d42f083c1c2d37c24221d">#</a> </h3> <p> It seems to me that some software is actually getting worse, and that this is a more recent trend. </p> <p> The point isn't that some software is bad. This has always been the case. What seems new to me is that software that <em>used to be good</em> deteriorates. While this wasn't entirely unheard of in the nineties (I'm looking at you, WordPerfect), this is becoming much more noticeable. </p> <p> Perhaps it's just <a href="https://en.wikipedia.org/wiki/Frequency_illusion">frequency illusion</a>, or perhaps it's because I use software much more than I did in the nineties. Still, I can't shake the feeling that some software is deteriorating. </p> <p> Why does this happen? I don't know, but my own bias suggests that it's because there's less focus on regression testing. Many of the problems I see look like regression bugs to me. A good engineering team could have caught them with automated regression tests, but these days, it seems as though many teams rely on releasing often and then letting users do the testing. </p> <p> The problem with that approach, however, is that if you don't have good automated tests, fixing one regression may resurrect another. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="16748f443dbe46f6b9f039e3de5165bf"> <div class="comment-author"><a href="https://carlosschults.net">Carlos Schults</a> <a href="#16748f443dbe46f6b9f039e3de5165bf">#</a></div> <div class="comment-content"> <p>I don't think your perception that software is getting worse is wrong, Mark. I've been an Evernote user since 2011. And, for a good portion of those years, I've been a paid customer.</p> <p> I'm certainly not alone in my perception that the application is becoming worse with each new release, to the point of, at times, becoming unusable. Syncing problems with the mobile version, weird changes in the UI that don't accomplish anything, general sluggishness, and, above all, not listening to the users regarding long-needed features. </p> </div> <div class="comment-date">2023-07-29 18:05 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Works on most machines https://blog.ploeh.dk/2023/07/17/works-on-most-machines 2023-07-17T08:01:00+00:00 Mark Seemann <div id="post"> <p> <em>TDD encourages deployment flexibility. Functional programming also helps.</em> </p> <p> Recently several of the podcasts I subscribe to have had episodes about various container technologies, of which <a href="https://en.wikipedia.org/wiki/Kubernetes">Kubernetes</a> dominates. I tune out of such content, since it has nothing to do with me. </p> <p> I've never found containerisation relevant. I remember being fascinated when I first heard of <a href="https://en.wikipedia.org/wiki/Docker_(software)">Docker</a>, and for a while, I awaited a reason to use it. It never materialised. </p> <p> I'd test-drive whatever system I was working on, and deploy it to production. Usually, it'd just work. </p> <p> Since my process already produced good results, why make it more complicated? </p> <p> Occasionally, I would become briefly aware of the lack of containers in my life, but then I'd forget about it again. Until now, I haven't thought much about it, and it's probably only the random coincidence of a few podcast episodes back-to-back that made me think more about it. </p> <h3 id="98b5a360a5ba413ca4dbccce86cbe331"> Be liberal with what system you run on <a href="#98b5a360a5ba413ca4dbccce86cbe331">#</a> </h3> <p> When I was a beginner programmer a few years ago, things were different. I'd write code that <a href="https://blog.codinghorror.com/the-works-on-my-machine-certification-program/">worked on my machine</a>, but not always on the test server. </p> <p> As I gained experience, this tended to happen less often. This doubtlessly have multiple causes, and increased experience is likely one of them, but I also think that my interest in loose coupling and test-driven development plays a role. </p> <p> Increasingly I developed an ethos of writing software that would work on most machines, instead of only my own. It seems reminiscent of <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>: Be liberal with what system you run on. </p> <p> Test-driven development helps in that regard, because you write code that must be able to execute in at least two contexts: The test context, and the actual system context. These two contexts both exist on your machine. </p> <p> A colleague once taught me: <em>The most difficult generalisation step is going from one to two</em>. Once you've generalised to two cases, it's much easier to generalise to three, four, or <em>n</em> cases. </p> <p> It seems to me that such from-one-to-two-cases generalisation is an inadvertent by-product of test-driven development. Once your code already matches two different contexts, making it even more flexible isn't that much extra work. It's not even <a href="https://wiki.c2.com/?SpeculativeGenerality">speculative generality</a> because you also need to make it work on the production system and (one hopes) on a build server or continuous delivery pipeline. That's 3-4 contexts. Odds are that software that runs successfully in four separate contexts runs successfully on many more systems. </p> <h3 id="a0315ee333ff454fb1bd6814f5806121"> General-purpose modules <a href="#a0315ee333ff454fb1bd6814f5806121">#</a> </h3> <p> In <a href="/ref/a-philosophy-of-software-design">A Philosophy of Software Design</a> John Ousterhout argues that one should aim for designing general-purpose objects or modules, rather than specialised APIs. He calls them <em>deep modules</em> and their counterparts <em>shallow modules</em>. On the surface, this seems to go against the grain of <a href="https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it">YAGNI</a>, but the way I understand the book, the point is rather that general-purpose solutions also solve special cases, and, when done right, the code doesn't have to be more complicated than the one that handles the special case. </p> <p> As I write in <a href="https://www.goodreads.com/review/show/5498011140">my review of the book</a>, I think that there's a connection with test-driven development. General-purpose code is code that works in more than one situation, including automated testing environments. This is almost tautological. If it doesn't work in an automated test, an argument could be made that it's insufficiently general. </p> <p> Likewise, general-purpose software should be able to work when deployed to more than one machine. It should even work on machines where other versions of that software already exist. </p> <p> When you have general-purpose software, though, do you really need containers? </p> <h3 id="960f22bd15fb48b1a3d7dfddc9f60408"> Isolation <a href="#960f22bd15fb48b1a3d7dfddc9f60408">#</a> </h3> <p> While I've routinely made use of test-driven development since 2003, I started my shift towards functional programming around ten years later. I think that this has amplified my code's flexibility. </p> <p> As <a href="https://jessitron.com/">Jessica Kerr</a> <a href="http://www.functionalgeekery.com/episode-8-jessica-kerr">pointed out years ago</a>, a corollary of <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a> is that <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> are <em>isolated</em> from their environment. Only input arguments affect the output of a pure function. </p> <p> Ultimately, you may need to query the environment about various things, but in functional programming, querying the environment is impure, so you <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">push it to the boundary of the system</a>. Functional programming encourages you to <a href="/2018/11/19/functional-architecture-a-definition">explicitly consider and separate impure actions from pure functions</a>. This implies that the environment-specific code is small, cohesive, and easy to review. </p> <h3 id="4775b8ab484c4833a6a5a86bac3b8b8e"> Conclusion <a href="#4775b8ab484c4833a6a5a86bac3b8b8e">#</a> </h3> <p> For a while, when Docker was new, I expected it to be a technology that I'd eventually pick up and make part of my tool belt. As the years went by, that never happened. As a programmer, I've never had the need. </p> <p> I think that a major contributor to that is that since I mostly develop software with test-driven development, the resulting software is already robust or flexible enough to run in multiple environments. Adding functional programming to the mix helps to achieve isolation from the run-time environment. </p> <p> All of this seems to collaborate to enable code to work not just on my machine, but on most machines. Including containers. </p> <p> Perhaps there are other reasons to use containers and Kubernetes. In a devops context, I could imagine that it makes deployment and operations easier. I don't know much about that, but I also don't mind. If someone wants to take the code I've written and run it in a container, that's fine. It's going to run there too. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4012c2cddcb64a068c0b06b7989a676e"> <div class="comment-author">qfilip <a href="#4012c2cddcb64a068c0b06b7989a676e">#</a></div> <div class="comment-content"> <p> Commenting for the first time. I hope I made these changes in proper manner. Anyway... </p> <p> Kubernetes usually also means the usage of cloud infrastructure, and as such, it can be automated (and change-tracked) in various interesting ways. Is it worth it? Well, that depends as always... Docker isn't the only container technology supported by k8s, but since it's the most popular one... they go hand in hand. </p> <p> Docker is also very useful for enabling others to run your software on their machines. Recently, we've been exploring some apps that consisted of ~4 services (web servers) and a database. All of them written in different technologies (PHP, Java, C#). You don't have to setup environment variables. You don't need to have relevant SDKs to build projects etc. Just run docker command, and spin them instantly on your PC. </p> <p>So there's that...</p> <p> Unrelated to the topic above, I'd like to ask you, if you could write an article on the specific subject. Or, if the answer is short, comment me back. As an F# enthusiast, I find yours and <a href="https://fsharpforfunandprofit.com">Scott's</a> blog very valuable. One thing I've failed to find here is why you don't like ORMs. I think the words were <i>they solve a problem that we shouldn't have in the first place</i>. Since F# doesn't play too well with Entity Framework, and I pretty much can't live without it... I'm curious if I'm missing something. A different approach, way of thinking. I can work with raw SQL ofcourse... but the mapping... oh the mapping... </p> </div> <div class="comment-date">2023-07-18 22:30 UTC</div> </div> <div class="comment" id="04d9d2a2e9884b0ba2a0049898b98e5f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#04d9d2a2e9884b0ba2a0049898b98e5f">#</a></div> <div class="comment-content"> <p> I'm contemplating turning my response into a new article, but it may take some time before I get to it. I'll post here once I have a more thorough response. </p> </div> <div class="comment-date">2023-07-23 13:56 UTC</div> </div> <div class="comment" id="2adbe12cf0e541e7b76cf39037c6a96c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2adbe12cf0e541e7b76cf39037c6a96c">#</a></div> <div class="comment-content"> <p> qfilip, thank you for writing. I've now published <a href="/2023/07/31/test-driving-the-pyramids-top">the article</a> that, among many other things, respond to your comment about containers. </p> <p> I'll get back to your question about <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping">ORMs</a> as soon as possible. </p> </div> <div class="comment-date">2023-07-31 07:01 UTC</div> </div> <div class="comment" id="1702d8d84d024d3b83b683e2589460f5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1702d8d84d024d3b83b683e2589460f5">#</a></div> <div class="comment-content"> <p> I'm still considering how to best address the question about ORMs, but in the meanwhile, I'd like to point interested readers to Ted Neward's famous article <a href="https://blogs.newardassociates.com/blog/2006/the-vietnam-of-computer-science.html">The Vietnam of Computer Science</a>. </p> </div> <div class="comment-date">2023-08-14 20:01 UTC</div> </div> <div class="comment" id="74654595140446239d66bcb85fc51234"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#74654595140446239d66bcb85fc51234">#</a></div> <div class="comment-content"> <p> Finally, I'm happy to announce that I've written an article trying to explain my position: <a href="/2023/09/18/do-orms-reduce-the-need-for-mapping">Do ORMs reduce the need for mapping?</a>. </p> </div> <div class="comment-date">2023-09-18 14:50 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AI for doc comments https://blog.ploeh.dk/2023/07/10/ai-for-doc-comments 2023-07-10T06:02:00+00:00 Mark Seemann <div id="post"> <p> <em>A solution in search of a problem?</em> </p> <p> I was recently listening to <a href="https://www.dotnetrocks.com/details/1850">a podcast episode</a> where the guest (among other things) enthused about how advances in <a href="https://en.wikipedia.org/wiki/Large_language_model">large language models</a> mean that you can now get these systems to write <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/xmldoc/">XML doc comments</a>. </p> <p> You know, these things: </p> <p> <pre><span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">summary</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;Scorbles&nbsp;a&nbsp;dybliad.</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;/</span><span style="color:gray;">summary</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">param</span>&nbsp;<span style="color:gray;">name</span><span style="color:gray;">=</span><span style="color:gray;">&quot;</span>dybliad<span style="color:gray;">&quot;</span><span style="color:gray;">&gt;</span><span style="color:green;">The&nbsp;dybliad&nbsp;to&nbsp;scorble.</span><span style="color:gray;">&lt;/</span><span style="color:gray;">param</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">param</span>&nbsp;<span style="color:gray;">name</span><span style="color:gray;">=</span><span style="color:gray;">&quot;</span>flag<span style="color:gray;">&quot;</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;A&nbsp;flag&nbsp;that&nbsp;controls&nbsp;wether&nbsp;scorbling&nbsp;is&nbsp;done&nbsp;pre-&nbsp;or&nbsp;postvotraid.</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;/</span><span style="color:gray;">param</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">returns</span><span style="color:gray;">&gt;</span><span style="color:green;">The&nbsp;scorbled&nbsp;dybliad.</span><span style="color:gray;">&lt;/</span><span style="color:gray;">returns</span><span style="color:gray;">&gt;</span> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Scorble(<span style="color:blue;">string</span>&nbsp;dybliad,&nbsp;<span style="color:blue;">bool</span>&nbsp;flag)</pre> </p> <p> And it struck me how that's not the first time I've encountered that notion. Finally, you no longer need to write those tedious documentation comments in your code. Instead, you can get <a href="https://github.com/features/copilot">Github Copilot</a> or <a href="https://en.wikipedia.org/wiki/ChatGPT">ChatGPT</a> to write them for you. </p> <p> When was the last time you wrote such comments? </p> <p> I'm sure that there are readers who wrote some just yesterday, but generally, I rarely encounter them in the wild. </p> <p> As a rule, I only write them when my modelling skills fail me so badly that I need to <a href="http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode">apologise in code</a>. Whenever I run into such a situation, I may as well take advantage of the format already in place for such things, but it's not taking up a big chunk of my time. </p> <p> It's been a decade since I ran into a code base where doc comments were mandatory. When I had to write comments, I'd use <a href="https://submain.com/ghostdoc/">GhostDoc</a>, which used heuristics to produce 'documentation' on par with modern AI tools. </p> <p> Whether you use GhostDoc, Github Copilot, or write the comments yourself, most of them tend to be equally inane and vacuous. Good design only amplifies this quality. The better names you use, and the more you leverage the type system to <a href="https://blog.janestreet.com/effective-ml-video">make illegal states unrepresentable</a>, the less you need the kind of documentation furnished by doc comments. </p> <p> I find it striking that more than one person wax poetic about AI's ability to produce doc comments. </p> <p> Is that, ultimately, the only thing we'll entrust to large language models? </p> <p> I <a href="/2022/12/05/github-copilot-preliminary-experience-report">know that that they can do more than that</a>, but are we going to let them? Or is automatic doc comments a solution in search of a problem? </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Validating or verifying emails https://blog.ploeh.dk/2023/07/03/validating-or-verifying-emails 2023-07-03T05:41:00+00:00 Mark Seemann <div id="post"> <p> <em>On separating preconditions from business rules.</em> </p> <p> My recent article <a href="/2023/06/26/validation-and-business-rules">Validation and business rules</a> elicited this question: </p> <blockquote> <p> "Regarding validation should be pure function, lets have user registration as an example, is checking the email address uniqueness a validation or a business rule? It may not be pure since the check involves persistence mechanism." </p> <footer><cite><a href="https://twitter.com/Cherif_b/status/1673906245172969473">Cherif BOUCHELAGHEM</a></cite></footer> </blockquote> <p> This is a great opportunity to examine some heuristics in greater detail. As always, this mostly presents how I think about problems like this, and so doesn't represent any rigid universal truth. </p> <p> The specific question is easily answered, but when the topic is email addresses and validation, I foresee several follow-up questions that I also find interesting. </p> <h3 id="579eacb264e94f3bb80aa6d5020df26f"> Uniqueness constraint <a href="#579eacb264e94f3bb80aa6d5020df26f">#</a> </h3> <p> A new user signs up for a system, and as part of the registration process, you want to verify that the email address is unique. Is that validation or a business rule? </p> <p> Again, I'm going to put the cart before the horse and first use the definition to answer the question. </p> <blockquote> <p> Validation is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> that decides whether data is acceptable. </p> <footer><cite><a href="/2023/06/26/validation-and-business-rules">Validation and business rules</a></cite></footer> </blockquote> <p> Can you implement the uniqueness constraint with a pure function? Not easily. What most systems would do, I'm assuming, is to keep track of users in some sort of data store. This means that in order to check whether or not a email address is unique, you'd have to query that database. </p> <p> Querying a database is non-deterministic because you could be making multiple subsequent queries with the same input, yet receive differing responses. In this particular example, imagine that you ask the database whether <em>ann.siebel@example.com</em> is already registered, and the answer is <em>no, that address is new to us</em>. </p> <p> Database queries are snapshots in time. All that answer tells you is that at the time of the query, the address would be unique in your database. While that answer travels over the network back to your code, a concurrent process might add that very address to the database. Thus, the next time you ask the same question: <em>Is ann.siebel@example.com already registered?</em> the answer would be: <em>Yes, we already know of that address</em>. </p> <p> Verifying that the address is unique (most likely) involves an impure action, and so according to the above definition isn't a validation step. By the <a href="https://en.wikipedia.org/wiki/Law_of_excluded_middle">law of the the excluded middle</a>, then, it must be a business rule. </p> <p> Using a different rule of thumb, <a href="https://en.wikipedia.org/wiki/Robert_C._Martin">Robert C. Martin</a> arrives at the same conclusion: </p> <blockquote> <p> "Uniqueness is semantic not syntactic, so I vote that uniqueness is a business rule not a validation rule." </p> <footer><cite><a href="https://twitter.com/unclebobmartin/status/1674023070611263493">Robert C. Martin</a></cite></footer> </blockquote> <p> This highlights a point about this kind of analysis. Using functional purity is a heuristic shortcut to sorting verification problems. Those that are deterministic and have no side effects are validation problems, and those that are either non-deterministic or have side effects are not. </p> <p> Being able to sort problems in this way is useful because it enables you to choose the right tool for the job, and to avoid the wrong tool. In this case, trying to address the uniqueness constraint with validation is likely to cause trouble. </p> <p> Why is that? Because of what I already described. A database query is a snapshot in time. If you make a decision based on that snapshot, it may be the wrong decision once you reach a conclusion. Granted, when discussing user registration, the risk of several processes concurrently trying to register the same email address probably isn't that big, but in other domains, contention may be a substantial problem. </p> <p> Being able to identify a uniqueness constraint as something that <em>isn't</em> validation enables you to avoid that kind of attempted solution. Instead, you may contemplate other designs. If you keep users in a relational database, the easiest solution is to put a uniqueness constraint on the <code>Email</code> column and let the database deal with the problem. Just be prepared to handle the exception that the <code>INSERT</code> statement may generate. </p> <p> If you have another kind of data store, there are other ways to model the constraint. You can even do so using lock-free architectures, but that's out of scope for this article. </p> <h3 id="4ebc57fb6a4d40d4a168b454f63804fb"> Validation checks preconditions <a href="#4ebc57fb6a4d40d4a168b454f63804fb">#</a> </h3> <p> <a href="/encapsulation-and-solid">Encapsulation</a> is an important part of object-oriented programming (<a href="/2022/10/24/encapsulation-in-functional-programming">and functional programming as well</a>). As I've often outlined, I base my understanding of encapsulation on <a href="/ref/oosc">Object-Oriented Software Construction</a>. I consider <em>contract</em> (preconditions, invariants, and postconditions) essential to encapsulation. </p> <p> I'll borrow a figure from my article <a href="/2022/08/22/can-types-replace-validation">Can types replace validation?</a>: </p> <p> <img src="/content/binary/validation-as-a-function-from-data-to-type.png" alt="An arrow labelled 'validation' pointing from a document to the left labelled 'Data' to a box to the right labelled 'Type'."> </p> <p> The role of validation is to answer the question: <em>Does the data make sense?</em> </p> <p> This question, and its answer, is typically context-dependent. What 'makes sense' means may differ. This is even true for email addresses. </p> <p> When I wrote the example code for my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, I had to contemplate how to model email addresses. Here's an excerpt from the book: </p> <blockquote> <p> Email addresses are <a href="https://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/">notoriously difficult to validate</a>, and even if you had a full implementation of the SMTP specification, what good would it do you? </p> <p> Users can easily give you a bogus email address that fits the spec. The only way to really validate an email address is to send a message to it and see if that provokes a response (such as the user clicking on a validation link). That would be a long-running asynchronous process, so even if you'd want to do that, you can't do it as a blocking method call. </p> <p> The bottom line is that it makes little sense to validate the email address, apart from checking that it isn't null. For that reason, I'm not going to validate it more than I've already done. </p> <footer><cite><a href="/ctfiyh">Code That Fits in Your Head</a>, p. 102</cite></footer> </blockquote> <p> In this example, I decided that the only precondition I would need to demand was that the email address isn't null. This was motivated by the operations I needed to perform with the email address - or rather, in this case, the operations I didn't need to perform. The only thing I needed to do with the address was to save it in a database and send emails: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;EmailReservationCreated(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Reservation&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(reservation)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;RestaurantDatabase.GetRestaurant(restaurantId).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;subject&nbsp;=&nbsp;<span style="color:#a31515;">$&quot;Your&nbsp;reservation&nbsp;for&nbsp;</span>{r?.Name}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;body&nbsp;=&nbsp;CreateBodyForCreated(reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;email&nbsp;=&nbsp;reservation.Email.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Send(subject,&nbsp;body,&nbsp;email).ConfigureAwait(<span style="color:blue;">false</span>); }</pre> </p> <p> This code example suggests why I made it a precondition that <code>Email</code> mustn't be null. Had null be allowed, I would have had to resort to <a href="/2013/07/08/defensive-coding">defensive coding, which is exactly what encapsulation makes redundant</a>. </p> <p> Validation is a process that determines whether data is useful in a particular context. In this particular case, all it takes is to check the <code>Email</code> property on the <a href="https://en.wikipedia.org/wiki/Data_transfer_object">DTO</a>. The sample code that comes with <a href="/ctfiyh">Code That Fits in Your Head</a> shows the basics, while <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">An applicative reservation validation example in C#</a> contains a more advanced solution. </p> <h3 id="55a584c6f15d4899beac8e40190068d5"> Preconditions are context-dependent <a href="#55a584c6f15d4899beac8e40190068d5">#</a> </h3> <p> I would assume that a normal user registration process has little need to validate an ostensible email address. A system may want to verify the address, but that's a completely different problem. It usually involves sending an email to the address in question and have some asynchronous process register if the user verifies that email. For an article related to this problem, see <a href="/2019/12/02/refactoring-registration-flow-to-functional-architecture">Refactoring registration flow to functional architecture</a>. </p> <p> Perhaps you've been reading this with mounting frustration: <em>How about validating the address according to the SMTP spec?</em> </p> <p> Indeed, that sounds like something one should do, but turns out to be rarely necessary. As already outlined, users can easily supply a bogus address like <code>foo@bar.com</code>. It's valid according to the spec, and so what? How does that information help you? </p> <p> In most contexts I've found myself, validating according to the SMTP specification is a distraction. One might, however, imagine scenarios where it might be required. If, for example, you need to sort addresses according to user name or host name, or perform some filtering on those parts, etc. it might be warranted to actually <em>require</em> that the address is valid. </p> <p> This would imply a <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">validation step that attempts to parse</a> the address. Once again, parsing here implies translating less-structured data (a string) to more-structured data. On .NET, I'd consider using the <a href="https://learn.microsoft.com/dotnet/api/system.net.mail.mailaddress">MailAddress</a> class which already comes with <a href="https://learn.microsoft.com/dotnet/api/system.net.mail.mailaddress.trycreate">built-in parser functions</a>. </p> <p> The point being that your needs determine your preconditions, which again determine what validation should do. The preconditions are context-dependent, and so is validation. </p> <h3 id="6e66e8eb91f742109a02def661115656"> Conclusion <a href="#6e66e8eb91f742109a02def661115656">#</a> </h3> <p> Email addresses offer a welcome opportunity to discuss the difference between validation and verification in a way that is specific, but still, I hope, easy to extrapolate from. </p> <p> Validation is a translation from one (less-structured) data format to another. Typically, the more-structured data format is an object, a record, or a hash map (depending on language). Thus, validation is determined by two forces: What the input data looks like, and what the desired object requires; that is, its preconditions. </p> <p> Validation is always a translation with the potential for error. Some input, being less-structured, can't be represented by the more-structured format. In addition to parsing, a validation function must also be able to fail in a composable matter. That is, fortunately, <a href="/2020/12/14/validation-a-solved-problem">a solved problem</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Validation and business rules https://blog.ploeh.dk/2023/06/26/validation-and-business-rules 2023-06-26T06:05:00+00:00 Mark Seemann <div id="post"> <p> <em>A definition of validation as distinguished from business rules.</em> </p> <p> This article suggests a definition of <em>validation</em> in software development. <em>A</em> definition, not <em>the</em> definition. It presents how I currently distinguish between validation and business rules. I find the distinction useful, although perhaps it's a case of reversed causality. The following definition of <em>validation</em> is useful because, if defined like that, <a href="/2020/12/14/validation-a-solved-problem">it's a solved problem</a>. </p> <p> My definition is this: </p> <p> <em>Validation is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> that decides whether data is acceptable.</em> </p> <p> I've used the word <em>acceptable</em> because it suggests a link to <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. When validating, you may want to allow for some flexibility in input, even if, strictly speaking, it's not entirely on spec. </p> <p> That's not, however, the key ingredient in my definition. The key is that validation should be a pure function. </p> <p> While this may sound like an arbitrary requirement, there's a method to my madness. </p> <h3 id="8b91bccdf17f42fa9cfc93599e35bd6c"> Business rules <a href="#8b91bccdf17f42fa9cfc93599e35bd6c">#</a> </h3> <p> Before I explain the benefits of the above definition, I think it'll be useful to outline typical problems that developers face. My thesis in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> is that understanding limits of human cognition is a major factor in making a code base sustainable. This again explains why <a href="/encapsulation-and-solid">encapsulation</a> is such an important idea. You want to <em>confine</em> knowledge in small containers that fit in your head. Information shouldn't leak out of these containers, because that would require you to keep track of too much stuff when you try to understand other code. </p> <p> When discussing encapsulation, I emphasise <em>contract</em> over information hiding. A contract, in the spirit of <a href="/ref/oosc">Object-Oriented Software Construction</a>, is a set of preconditions, invariants, and postconditions. Preconditions are particularly relevant to the topic of validation, but I've often experienced that some developers struggle to identify where validation ends and business rules begin. </p> <p> Consider an online restaurant reservation system as an example. We'd like to implement a feature that enables users to make reservations. In order to meet that end, we decide to introduce a <code>Reservation</code> class. What are the preconditions for creating a valid instance of such a class? </p> <p> When I go through such an exercise, people quickly identify requirement such as these: </p> <ul> <li>The reservation should have a date and time.</li> <li>The reservation should contain the number of guests.</li> <li>The reservation should contain the name or email (or other data) about the person making the reservation.</li> </ul> <p> A common suggestion is that the restaurant should also be able to accommodate the reservation; that is, it shouldn't be fully booked, it should have an available table at the desired time of an appropriate size, etc. </p> <p> That, however, isn't a precondition for creating a valid <code>Reservation</code> object. That's a business rule. </p> <h3 id="5c9983d374e84212bbd37e7cd2476287"> Preconditions are self-contained <a href="#5c9983d374e84212bbd37e7cd2476287">#</a> </h3> <p> How do you distinguish between a precondition and a business rule? And what does that have to do with input validation? </p> <p> Notice that in the above examples, the three preconditions I've listed are self-contained. They are statements about the object or value's constituent parts. On the other hand, the requirement that the restaurant should be able to accommodate the reservation deals with a wider context: The table layout of the restaurant, prior reservations, opening and closing times, and other business rules as well. </p> <p> Validation is, as <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">Alexis King points out</a>, a parsing problem. You receive less-structured data (<a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a>, <a href="https://en.wikipedia.org/wiki/JSON">JSON</a>, <a href="https://en.wikipedia.org/wiki/XML">XML</a>, etc.) and attempt to project it to a more-structured format (C# objects, <a href="https://fsharp.org/">F#</a> records, <a href="https://clojure.org/">Clojure</a> maps, etc.). This succeeds when the input satisfies the preconditions, and fails otherwise. </p> <p> Why can't we add more preconditions than required? Consider <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. An operation (and that includes object constructors) should be liberal in what it accepts. While you have to draw the line somewhere (you can't really work with a reservation if the date is missing), an object shouldn't require <em>more</em> than it needs. </p> <p> In general we observe that the fewer pre-conditions, the easier it is to create an object (or equivalent functional data structure). As a counter-example, this explains why <a href="https://en.wikipedia.org/wiki/Active_record_pattern">Active Record</a> is antithetical to unit testing. One precondition is that there's a database available, and while not impossible to automate in tests, it's quite the hassle. It's easier to work with <a href="https://en.wikipedia.org/wiki/Plain_old_Java_object">POJOs</a> in tests. And unit tests, being <a href="/2011/11/10/TDDimprovesreusability">the first clients of an API</a>, tell you how easy it is to use that API. </p> <h3 id="56b5624f416c4648aab2b27216c10bb1"> Contracts with third parties <a href="#56b5624f416c4648aab2b27216c10bb1">#</a> </h3> <p> If validation is fundamentally parsing, it seems reasonable that operations should be pure functions. After all, a parser operates on unchanging (less-structured) data. A programming-language parser takes contents of text files as input. There's little need for more input than that, and the output is expected to be deterministic. Not surprisingly, <a href="https://www.haskell.org/">Haskell</a> is well-suited for writing parsers. </p> <p> You don't, however, have to buy the argument that validation is essentially parsing, so consider another perspective. </p> <p> Validation is a data transformation step you perform to deal with input. Data comes from a source external to your system. It can be a user filling in a form, another program making an HTTP request, or a batch job that receives files over <a href="https://en.wikipedia.org/wiki/File_Transfer_Protocol">FTP</a>. </p> <p> Even if you don't have a formal agreement with any third party, <a href="https://www.hyrumslaw.com/">Hyrum's law</a> implies that a contract does exist. It behoves you to pay attention to that, and make it as explicit as possible. </p> <p> Such a contract should be stable. Third parties should be able to rely on deterministic behaviour. If they supply data one day, and you accept it, you can't reject the same data the next days on grounds that it was malformed. At best, you may <a href="/2021/12/13/backwards-compatibility-as-a-profunctor">be contravariant in input as time passes</a>; in other words, you may accept things tomorrow that you didn't accept today, but you may not reject tomorrow what you accepted today. </p> <p> Likewise, you can't have validation rules that erratically accept data one minute, reject the same data the next minute, only to accept it later. This implies that validation must, at least, be deterministic: The same input should always produce the same output. </p> <p> That's half of the way to <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>. Do you need side effects in your validation logic? Hardly, so you might as well implement it as pure functions. </p> <h3 id="45e45eee8b8947dc9eb2743de5adac13"> Putting the cart before the horse <a href="#45e45eee8b8947dc9eb2743de5adac13">#</a> </h3> <p> You may still think that my definition smells of a solution in search of a problem. Yes, pure functions are convenient, but does it naturally follow that validation should be implemented as pure functions? Isn't this a case of poor <a href="https://en.wikipedia.org/wiki/Retroactive_continuity">retconning</a>? </p> <p> <img src="/content/binary/distinguishing-between-validation-and-business-rules.png" alt="Two buckets with a 'lid' labeled 'applicative validation' conveniently fitting over the validation bucket."> </p> <p> When faced with the question: <em>What is validation, and what are business rules?</em> it's almost as though I've conveniently sized the <em>Validation</em> sorting bucket so that it perfectly aligns with <a href="/2018/11/05/applicative-validation">applicative validation</a>. Then, the <em>Business rules</em> bucket fits whatever is left. (In the figure, the two buckets are of equal size, which hardly reflects reality. I estimate that the <em>Business rules</em> bucket is much larger, but had I tried to illustrate that, too, in the figure, it would have looked akilter.) </p> <p> This is suspiciously convenient, but consider this: My experience is that this perspective on validation works well. To a great degree, this is because <a href="/2020/12/14/validation-a-solved-problem">I consider validation a solved problem</a>. It's productive to be able to take a chunk of a larger problem and put it aside: <em>We know how to deal with this. There are no risks there.</em> </p> <p> Definitions do, I believe, rarely spring fully formed from some <a href="https://en.wikipedia.org/wiki/Theory_of_forms">Platonic ideal</a>. Rather, people observe what works and eventually extract a condensed description and call it a definition. That's what I've attempted to do here. </p> <h3 id="62140574219942c9b34e6b84fe53bfb2"> Business rules change <a href="#62140574219942c9b34e6b84fe53bfb2">#</a> </h3> <p> Let's return to the perspective of validation as a technical contract between your system and a third party. While that contract should be as stable as possible, business rules change. </p> <p> Consider the online restaurant reservation example. Imagine that you're the third-party programmer, and that you've developed a client that can make reservations on behalf of users. When a user wants to make a reservation, there's always a risk that it's not possible. Your client should be able to handle that scenario. </p> <p> Now the restaurant becomes so popular that it decides to change a rule. Earlier, you could make reservations for one, three, or five people, even though the restaurant only has tables for two, four, or six people. Based on its new-found popularity, the restaurant decides that it only accepts reservations for entire tables. Unless it's on the same day and they still have a free table. </p> <p> This changes the <em>behaviour</em> of the system, but not the contract. A reservation for three is still <em>valid</em>, but will be declined because of the new rule. </p> <blockquote> <p> "Things that change at the same rate belong together. Things that change at different rates belong apart." </p> <footer><cite><a href="https://www.facebook.com/notes/kent-beck/naming-from-the-outside-in/464270190272517">Kent Beck</a></cite></footer> </blockquote> <p> Business rules change at different rates than preconditions, so it makes sense to decouple those concerns. </p> <h3 id="ac220ae336c24be88aea366e64de39b4"> Conclusion <a href="#ac220ae336c24be88aea366e64de39b4">#</a> </h3> <p> Since validation is a solved problem, it's useful to be able to identify what is validation, and what is something else. As long as an 'input rule' is self-contained (or parametrisable), deterministic, and has no side-effects, you can model it with applicative validation. </p> <p> Equally useful is it to be able to spot when applicative validation isn't a good fit. While I'm sure that someone has published a <code>ValidationT</code> monad transformer for Haskell, I'm not sure I would recommend going that route. In other words, if some business operation involves impure actions, it's not going to fit the mold of applicative validation. </p> <p> This doesn't mean that you can't implement business rules with pure functions. You can, but in my experience, abstractions other than applicative validation are more useful in those cases. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. When is an implementation detail an implementation detail? https://blog.ploeh.dk/2023/06/19/when-is-an-implementation-detail-an-implementation-detail 2023-06-19T06:10:00+00:00 Mark Seemann <div id="post"> <p> <em>On the tension between encapsulation and testability.</em> </p> <p> This article is part of a series called <a href="/2023/02/13/epistemology-of-interaction-testing">Epistemology of interaction testing</a>. A <a href="/2023/03/13/confidence-from-facade-tests">previous article in the series</a> elicited this question: </p> <blockquote> <p> "following your suggestion, aren’t we testing implementation details?" </p> <footer><cite><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a></cite></footer> </blockquote> <p> This frequently-asked question reminds me of an old joke. I think that I first heard it in the eighties, a time when phones had <a href="https://en.wikipedia.org/wiki/Rotary_dial">rotary dials</a>, everyone smoked, you'd receive mail through your apartment door's <a href="https://en.wikipedia.org/wiki/Letter_box">letter slot</a>, and unemployment was high. It goes like this: </p> <p> <em>A painter gets a helper from the unemployment office. A few days later the lady from the office calls the painter and apologizes deeply for the mistake.</em> </p> <p> <em>"What mistake?"</em> </p> <p> <em>"I'm so sorry, instead of a painter we sent you a gynaecologist. Please just let him go, we'll send you a..."</em> </p> <p> <em>"Let him go? Are you nuts, he's my best worker! At the last job, they forgot to leave us the keys, and the guy painted the whole room through the letter slot!"</em> </p> <p> I always think of this joke when the topic is testability. Should you test everything through a system's public API, or do you choose to expose some internal APIs in order to make the code more testable? </p> <h3 id="815100cf9c3b492f8abf392528fc5b1e"> Letter slots <a href="#815100cf9c3b492f8abf392528fc5b1e">#</a> </h3> <p> Consider the simplest kind of program you could write: <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">Hello world</a>. If you didn't consider automated testing, then an <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> C# implementation might look like this: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Program</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console<span style="color:#b4b4b4;">.</span>WriteLine(<span style="color:#a31515;">&quot;Hello,&nbsp;World!&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> (Yes, I know that with modern C# you can write such a program using a single <a href="https://learn.microsoft.com/dotnet/csharp/fundamentals/program-structure/top-level-statements">top-level statement</a>, but I'm writing for a broader audience, and only use C# as an example language.) </p> <p> How do we test a program like that? Of course, no-one seriously suggests that we <em>really</em> need to test something that simple, but what if we make it a little more complex? What if we make it possible to supply a name as a command-line argument? What if we want to internationalise the program? What if we want to add a <em>help</em> feature? What if we want to add a feature so that we can send a <em>hello</em> to another recipient, on another machine? When does the behaviour become sufficiently complex to warrant automated testing, and how do we achieve that goal? </p> <p> For now, I wish to focus on <em>how</em> to achieve the goal of testing software. For the sake of argument, then, assume that we want to test the above <em>hello world</em> program. </p> <p> As given, we can run the program and verify that it prints <em>Hello, World!</em> to the console. This is easy to do as a manual test, but harder if you want to automate it. </p> <p> You could write a test framework that automatically starts a new operating-system process (the program) and waits until it exits. This framework should be able to handle processes that exit with success and failure status codes, as well as processes that hang, or never start, or keep restarting... Such a framework also requires a way to capture the standard output stream in order to verify that the expected text is written to it. </p> <p> I'm sure such frameworks exist for various operating systems and programming languages. There is, however, a simpler solution if you can live with the trade-off: You could open the API of your source code a bit: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Program</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<span style="color:#a31515;">&quot;Hello,&nbsp;World!&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While I haven't changed the structure or the layout of the source code, I've made both class and method <code>public</code>. This means that I can now write a normal C# unit test that calls <code>Program.Main</code>. </p> <p> I still need a way to observe the behaviour of the program, but <a href="https://stackoverflow.com/a/2139303/126014">there are known ways of redirecting the Console output in .NET</a> (and I'd be surprised if that wasn't the case on other platforms and programming languages). </p> <p> As we add more and more features to the command-line program, we may be able to keep testing by calling <code>Program.Main</code> and asserting against the redirected <a href="https://learn.microsoft.com/dotnet/api/system.console">Console</a>. As the complexity of the program grows, however, this starts to look like painting a room through the letter slot. </p> <h3 id="4681beea02be47b89c9a5d7c2618fad7"> Adding new APIs <a href="#4681beea02be47b89c9a5d7c2618fad7">#</a> </h3> <p> Real programs are usually more than just a command-line utility. They may be smartphone apps that react to user input or network events, or web services that respond to HTTP requests, or complex asynchronous systems that react to, and send messages over durable queues. Even good old batch jobs are likely to pull data from files in order to write to a database, or the other way around. Thus, the interface to the rest of the world is likely larger than just a single <code>Main</code> method. </p> <p> Smartphone apps or message-based systems have event handlers. Web sites or services have classes, methods, or functions that handle incoming HTTP requests. These are essentially event handlers, too. This increases the size of the 'test surface': There are more than a single method you can invoke in order to exercise the system. </p> <p> Even so, a real program will soon grow to a size where testing entirely through the real-world-facing API becomes reminiscent of painting through a letter slot. <a href="https://www.infoq.com/presentations/integration-tests-scam/">J.B. Rainsberger explains that one major problem is the combinatorial explosion of required test cases</a>. </p> <p> Another problem is that the system may produce side effects that you care about. As a basic example, consider a system that, as part of its operation, sends emails. When testing this system, you want to verify that under certain circumstances, the system sends certain emails. How do you do that? </p> <p> If the system has <em>absolutely no concessions to testability</em>, I can think of two options: </p> <ul> <li>You contact the person to whom the system sends the email, and ask him or her to verify receipt of the email. You do that <em>every time</em> you test.</li> <li>You deploy the System Under Test in an environment with an <a href="https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol">SMTP</a> gateway that redirects all email to another address.</li> </ul> <p> Clearly the first option is unrealistic. The second option is a little better, but you still have to open an email inbox and look for the expected message. Doing so programmatically is, again, technically possible, and I'm sure that there are <a href="https://en.wikipedia.org/wiki/Post_Office_Protocol">POP3</a> or <a href="https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol">IMAP</a> assertion libraries out there. Still, this seems complicated, error-prone, and slow. </p> <p> What could we do instead? I would usually introduce a polymorphic interface such as <code>IPostOffice</code> as a way to substitute the real <code>SmtpPostOffice</code> with a <a href="https://martinfowler.com/bliki/TestDouble.html">Test Double</a>. </p> <p> Notice what happens in these cases: We introduce (or make public) new APIs in order to facilitate automated testing. </p> <h3 id="0cc85d98c7ae437aa1156e72fc25dcf6"> Application-boundary API and internal APIs <a href="#0cc85d98c7ae437aa1156e72fc25dcf6">#</a> </h3> <p> It's helpful to distinguish between the real-world-facing API and everything else. In this diagram, I've indicated the public-facing API as a thin green slice facing upwards (assuming that external stimulus - button clicks, HTTP requests, etc. - arrives from above). </p> <p> <img src="/content/binary/public-and-internal-system-apis.png" alt="A box depicting a program, with a small green slice indicating public-facing APIs, and internal blue slices indicating internal APIs."> </p> <p> The real-world-facing API is the code that <em>must</em> be present for the software to work. It could be a button-click handler or an ASP.NET <em>action method</em>: </p> <p> <pre>[HttpPost(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;Post(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;ReservationDto&nbsp;dto)</pre> </p> <p> Of course, if you're using another web framework or another programming language, the details differ, but the application <em>has</em> to have code that handles an HTTP <em>POST</em> request on matching addresses. Or a button click, or a message that arrives on a message bus. You get the point. </p> <p> These APIs are fairly fixed. If you change them, you change the externally observable behaviour of the system. Such changes are likely breaking changes. </p> <p> Based on which framework and programming language you're using, the shape of these APIs will be given. Like I did with the above <code>Main</code> method, you can make it <code>public</code> and use it for testing. </p> <p> A software system of even middling complexity will usually also be decomposed into smaller components. In the figure, I've indicated such subdivisions as boxes with gray outlines. Each of these may present an API to other parts of the system. I've indicated these APIs with light blue. </p> <p> The total size of internal APIs is likely to be larger than the public-facing API. On the other hand, you can (theoretically) change these internal interfaces without breaking the observable behaviour of the system. This is called <a href="/ref/refactoring">refactoring</a>. </p> <p> These internal APIs will often have <code>public</code> access modifiers. That doesn't make them real-world-facing. Be careful not to confuse programming-language <a href="https://en.wikipedia.org/wiki/Access_modifiers">access modifiers</a> with architectural concerns. Objects or their members can have <code>public</code> access modifiers even if the object plays an exclusively internal role. <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">At the boundaries, applications aren't object-oriented</a>. And <a href="/2022/05/02/at-the-boundaries-applications-arent-functional">neither are they functional</a>. </p> <p> Likewise, as the original <code>Main</code> method example shows, public APIs may be implemented with a <code>private</code> access modifier. </p> <p> Why do such internal APIs exist? Is it only to support automated testing? </p> <h3 id="e6bf8d9b8617435f914d976c05cc9731"> Decomposition <a href="#e6bf8d9b8617435f914d976c05cc9731">#</a> </h3> <p> If we introduce new code, such as the above <code>IPostOffice</code> interface, in order to facilitate testing, we have to be careful that it doesn't lead to <a href="https://dhh.dk/2014/test-induced-design-damage.html">test-induced design damage</a>. The idea that one might introduce an API exclusively to support automated testing rubs some people the wrong way. </p> <p> On the other hand, we do introduce (or make public) APIs for other reasons, too. One common reason is that we want to decompose an application's source code so that parallel development is possible. One person (or team) works on one part, and other people work on other parts. If those parts need to communicate, we need to agree on a contract. </p> <p> Such a contract exists for purely internal reasons. End users don't care, and never know of it. You can change it without impacting users, but you may need to coordinate with other teams. </p> <p> What remains, though, is that we do decompose systems into internal parts, and we've done this since before <a href="https://en.wikipedia.org/wiki/David_Parnas">Parnas</a> wrote <em>On the Criteria to Be Used in Decomposing Systems into Modules</em>. </p> <p> Successful test-driven development introduces <a href="http://wiki.c2.com/?SoftwareSeam">seams</a> where they ought to be in any case. </p> <h3 id="0df7cb8e24734176b20f4d61e7e24264"> Testing implementation details <a href="#0df7cb8e24734176b20f4d61e7e24264">#</a> </h3> <p> An internal seam is an implementation detail. Even so, when designed with care, it can serve multiple purposes. It enables teams to develop in parallel, and it enables automated testing. </p> <p> Consider the example from <a href="/2023/04/03/an-abstract-example-of-refactoring-from-interaction-based-to-property-based-testing">a previous article in this series</a>. I'll repeat one of the tests here: </p> <p> <pre>[Theory] [AutoData] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HappyPath</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>,&nbsp;(<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;Uri)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>) { &nbsp;&nbsp;&nbsp;&nbsp;_repository.Add(state,&nbsp;knownState); &nbsp;&nbsp;&nbsp;&nbsp;_stateValidator &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">validator</span>&nbsp;=&gt;&nbsp;validator.Validate(code,&nbsp;knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;_renderer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">renderer</span>&nbsp;=&gt;&nbsp;renderer.Success(knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(response); &nbsp;&nbsp;&nbsp;&nbsp;_target &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Complete(state,&nbsp;code) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Should().Be(response); }</pre> </p> <p> This test exercises a <em>happy-path</em> case by manipulating <code>IStateValidator</code> and <code>IRenderer</code> Test Doubles. It's a common approach to testability, and what <a href="https://en.wikipedia.org/wiki/David_Heinemeier_Hansson">dhh</a> would label test-induced design damage. While I'm sympathetic to that position, that's not my point. My point is that I consider <code>IStateValidator</code> and <code>IRenderer</code> internal APIs. End users (who probably don't even know what C# is) don't care about these interfaces. </p> <p> Tests like these test against implementation details. </p> <p> This need not be a problem. If you've designed good, stable seams then these tests can serve you for a long time. Testing against implementation details become a problem if those details change. Since it's hard to predict how things change in the future, it behoves us to decouple tests from implementation details as much as possible. </p> <p> The alternative, however, is mail-slot testing, which comes with its own set of problems. Thus, judicious introduction of seams is helpful, even if it couples tests to implementation details. </p> <p> Actually, in the question I quoted above, Christer van der Meeren asked whether my proposed alternative isn't testing implementation details. And, yes, that style of testing <em>also</em> relies on implementation details for testing. It's just a different way to design seams. Instead of designing seams around polymorphic objects, we design them around pure functions and immutable data. </p> <p> There are, I think, advantages to functional programming, but when it comes to relying on implementation details, it's only on par with object-oriented design. Not worse, not better, but the same. </p> <h3 id="d5e57741df0247d5a75879a75ca588dc"> Conclusion <a href="#d5e57741df0247d5a75879a75ca588dc">#</a> </h3> <p> Every API in use carries a cost. You need to keep the API stable so that users can use it tomorrow like they did yesterday. This can make it difficult to evolve or improve an API, because you risk introducing a breaking change. </p> <p> There are APIs that a system <em>must</em> have. Software exists to be used, and whether that entails a user clicking on a button or another computer system sending a message to your system, your code must handle such stimulus. This is your real-world-facing contract, and you need to be careful to keep it consistent. The smaller that surface area is, the simpler that task is. </p> <p> The same line of reasoning applies to internal APIs. While end users aren't impacted by changes in internal seams, other code is. If you change an implementation detail, this could cost maintenance work somewhere else. (Modern IDEs can handle some changes like that automatically, such as method renames. In those cases, the cost of change is low.) Therefore, it pays to minimise the internal seams as much as possible. One way to do this is by <a href="/2022/11/21/decouple-to-delete">decoupling to delete code</a>. </p> <p> Still, some internal APIs are warranted. They help you decompose a large system into smaller subparts. While there's a potential maintenance cost with every internal API, there's also the advantage of working with smaller, independent units of code. Often, the benefits are larger than the cost. </p> <p> When done well, such internal seams are useful testing APIs as well. They're still implementation details, though. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Collatz sequences by function composition https://blog.ploeh.dk/2023/06/12/collatz-sequences-by-function-composition 2023-06-12T05:27:00+00:00 Mark Seemann <div id="post"> <p> <em>Mostly in C#, with a few lines of Haskell code.</em> </p> <p> A <a href="/2023/05/08/is-cyclomatic-complexity-really-related-to-branch-coverage">recent article</a> elicited more comments than usual, and I've been so unusually buried in work that only now do I have a little time to respond to some of them. In <a href="/2023/05/08/is-cyclomatic-complexity-really-related-to-branch-coverage#02568f995d91432da540858644b61e89">one comment</a> <a href="http://github.com/neongraal">Struan Judd</a> offers a refactored version of my <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">Collatz sequence</a> in order to shed light on the relationship between <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> and test case coverage. </p> <p> Struan Judd's agenda is different from what I have in mind in this article, but the comment inspired me to refactor my own code. I wanted to see what it would look like with this constraint: It should be possible to test odd input numbers without exercising the code branches related to even numbers. </p> <p> The problem with more naive implementations of Collatz sequence generators is that (apart from when the input is <em>1</em>) the sequence ends with a tail of even numbers halving down to <em>1</em>. I'll start with a simple example to show what I mean. </p> <h3 id="ccd16365f7ce4842870f90e43267c33f"> Standard recursion <a href="#ccd16365f7ce4842870f90e43267c33f">#</a> </h3> <p> At first I thought that my confusion originated from the imperative structure of the original example. For more than a decade, I've preferred functional programming (FP), and even when I write object-oriented code, I tend to use concepts and patterns from FP. Thus I, naively, rewrote my Collatz generator as a recursive function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&nbsp;Sequence(<span style="color:blue;">int</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(n&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(n), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Only&nbsp;natural&nbsp;numbers&nbsp;allowed,&nbsp;but&nbsp;given&nbsp;</span>{n}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(n&nbsp;==&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;n&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(n&nbsp;%&nbsp;2&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;n&nbsp;}.Concat(Sequence(n&nbsp;/&nbsp;2)).ToArray(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;n&nbsp;}.Concat(Sequence(n&nbsp;*&nbsp;3&nbsp;+&nbsp;1)).ToArray(); }</pre> </p> <p> Recursion is usually not recommended in C#, because a sufficiently long sequence could blow the call stack. I wouldn't write production C# code like this, but you could do something like this in <a href="https://fsharp.org/">F#</a> or <a href="https://www.haskell.org/">Haskell</a> where the languages offer solutions to that problem. In other words, the above example is only for educational purposes. </p> <p> It doesn't, however, solve the problem that confused me: If you want to test the branch that deals with odd numbers, you can't avoid also exercising the branch that deals with even numbers. </p> <h3 id="4587120e2d9e483aba8cf7297704eb28"> Calculating the next value <a href="#4587120e2d9e483aba8cf7297704eb28">#</a> </h3> <p> In functional programming, you solve most problems by decomposing them into smaller problems and then compose the smaller <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">Lego bricks</a> with standard combinators. It seemed like a natural refactoring step to first pull the calculation of the next value into an independent function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Next(<span style="color:blue;">int</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;((n&nbsp;%&nbsp;2)&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n&nbsp;/&nbsp;2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n&nbsp;*&nbsp;3&nbsp;+&nbsp;1; }</pre> </p> <p> This function has a cyclomatic complexity of <em>2</em> and no loops or recursion. Test cases that exercise the even branch never touch the odd branch, and vice versa. </p> <p> A parametrised test might look like this: </p> <p> <pre>[Theory] [InlineData(&nbsp;2,&nbsp;&nbsp;1)] [InlineData(&nbsp;3,&nbsp;10)] [InlineData(&nbsp;4,&nbsp;&nbsp;2)] [InlineData(&nbsp;5,&nbsp;16)] [InlineData(&nbsp;6,&nbsp;&nbsp;3)] [InlineData(&nbsp;7,&nbsp;22)] [InlineData(&nbsp;8,&nbsp;&nbsp;4)] [InlineData(&nbsp;9,&nbsp;28)] [InlineData(10,&nbsp;&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;NextExamples(<span style="color:blue;">int</span>&nbsp;n,&nbsp;<span style="color:blue;">int</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;actual&nbsp;=&nbsp;Collatz.Next(n); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> The <code>NextExamples</code> test obviously defines more than the two test cases that are required to cover the <code>Next</code> function, but since <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">code coverage shouldn't be used as a target measure</a>, I felt that more than two test cases were warranted. This often happens, and should be considered normal. </p> <h3 id="e46d3353db8b4d50bcbe595dbbe3dbd0"> A Haskell proof of concept <a href="#e46d3353db8b4d50bcbe595dbbe3dbd0">#</a> </h3> <p> While I had a general idea about the direction in which I wanted to go, I felt that I lacked some standard functional building blocks in C#: Most notably an infinite, lazy sequence generator. Before moving on with the C# code, I threw together a proof of concept in Haskell. </p> <p> The <code>next</code> function is just a one-liner (if you ignore the optional type declaration): </p> <p> <pre><span style="color:#2b91af;">next</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a next&nbsp;n&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:blue;">even</span>&nbsp;n&nbsp;<span style="color:blue;">then</span>&nbsp;n&nbsp;`div`&nbsp;2&nbsp;<span style="color:blue;">else</span>&nbsp;n&nbsp;*&nbsp;3&nbsp;+&nbsp;1</pre> </p> <p> A few examples in GHCi suggest that it works as intended: </p> <p> <pre>ghci&gt; next 2 1 ghci&gt; next 3 10 ghci&gt; next 4 2 ghci&gt; next 5 16</pre> </p> <p> Haskell comes with enough built-in functions that that was all I needed to implement a Colaltz-sequence generator: </p> <p> <pre><span style="color:#2b91af;">collatz</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] collatz&nbsp;n&nbsp;=&nbsp;(<span style="color:blue;">takeWhile</span>&nbsp;(1&nbsp;&lt;)&nbsp;$&nbsp;<span style="color:blue;">iterate</span>&nbsp;next&nbsp;n)&nbsp;++&nbsp;[1]</pre> </p> <p> Again, a few examples suggest that it works as intended: </p> <p> <pre>ghci&gt; collatz 1 [1] ghci&gt; collatz 2 [2,1] ghci&gt; collatz 3 [3,10,5,16,8,4,2,1] ghci&gt; collatz 4 [4,2,1] ghci&gt; collatz 5 [5,16,8,4,2,1]</pre> </p> <p> I should point out, for good measure, that since this is a proof of concept I didn't add a Guard Clause against zero or negative numbers. I'll keep that in the C# code. </p> <h3 id="3c46180459334e86a278f9934fa4b032"> Generator <a href="#3c46180459334e86a278f9934fa4b032">#</a> </h3> <p> While C# does come with a <a href="https://learn.microsoft.com/dotnet/api/system.linq.enumerable.takewhile">TakeWhile</a> function, there's no direct equivalent to Haskell's <a href="https://hackage.haskell.org/package/base/docs/Prelude.html#v:iterate">iterate</a> function. It's not difficult to implement, though: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;Iterate&lt;<span style="color:#2b91af;">T</span>&gt;(Func&lt;T,&nbsp;T&gt;&nbsp;f,&nbsp;T&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;current&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;(<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;current; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;f(current); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While this <code>Iterate</code> implementation has a cyclomatic complexity of only <em>2</em>, it exhibits the same kind of problem as the previous attempts at a Collatz-sequence generator: You can't test one branch without testing the other. Here, it even seems as though it's impossible to test the branch that skips the loop. </p> <p> In Haskell the <code>iterate</code> function is simply a lazily-evaluated recursive function, but that's not going to solve the problem in the C# case. On the other hand, it helps to know that the <code>yield</code> keyword in C# is just syntactic sugar over a compiler-generated <a href="https://en.wikipedia.org/wiki/Iterator_pattern">Iterator</a>. </p> <p> Just for the exercise, then, I decided to write an explicit Iterator instead. </p> <h3 id="a834d92c5d864109aff31cb270c06b83"> Iterator <a href="#a834d92c5d864109aff31cb270c06b83">#</a> </h3> <p> For the sole purpose of demonstrating that it's possible to refactor the code so that branches are independent of each other, I rewrote the <code>Iterate</code> function to return an explicit <code>IEnumerable&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;Iterate&lt;<span style="color:#2b91af;">T</span>&gt;(Func&lt;T,&nbsp;T&gt;&nbsp;f,&nbsp;T&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Iterable&lt;T&gt;(f,&nbsp;x); }</pre> </p> <p> The <code><span style="color:#2b91af;">Iterable</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class is a private helper class, and only exists to return an <code>IEnumerator&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Iterable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;IEnumerable&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;T,&nbsp;T&gt;&nbsp;f; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;T&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Iterable</span>(Func&lt;T,&nbsp;T&gt;&nbsp;f,&nbsp;T&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.f&nbsp;=&nbsp;f; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.x&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IEnumerator&lt;T&gt;&nbsp;GetEnumerator() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Iterator&lt;T&gt;(f,&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;IEnumerator&nbsp;IEnumerable.GetEnumerator() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;GetEnumerator(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code><span style="color:#2b91af;">Iterator</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class does the heavy lifting: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Iterator</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;IEnumerator&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;T,&nbsp;T&gt;&nbsp;f; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;T&nbsp;original; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;iterating; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">Iterator</span>(Func&lt;T,&nbsp;T&gt;&nbsp;f,&nbsp;T&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.f&nbsp;=&nbsp;f; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;original&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Current&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;Current&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[MaybeNull] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>&nbsp;IEnumerator.Current&nbsp;=&gt;&nbsp;Current; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Dispose() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;MoveNext() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(iterating) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Current&nbsp;=&nbsp;f(Current); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iterating&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Reset() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Current&nbsp;=&nbsp;original; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iterating&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I can't think of a situation where I would write code like this in a real production code base. Again, I want to stress that this is only an exploration of what's possible. What this does show is that all members have low cyclomatic complexity, and none of them involve looping or recursion. Only one method, <code>MoveNext</code>, has a cyclomatic complexity greater than one, and its branches are independent. </p> <h3 id="f8030072341448bd80c8c0af055abc10"> Composition <a href="#f8030072341448bd80c8c0af055abc10">#</a> </h3> <p> All Lego bricks are now in place, enabling me to compose the <code>Sequence</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&nbsp;Sequence(<span style="color:blue;">int</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(n&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(n), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Only&nbsp;natural&nbsp;numbers&nbsp;allowed,&nbsp;but&nbsp;given&nbsp;</span>{n}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Generator.Iterate(Next,&nbsp;n).TakeWhile(i&nbsp;=&gt;&nbsp;1&nbsp;&lt;&nbsp;i).Append(1).ToList(); }</pre> </p> <p> This function has a cyclomatic complexity of <em>2</em>, and each branch can be exercised independently of the other. </p> <p> Which is what I wanted to accomplish. </p> <h3 id="e32a8549e23446ba8c9cffd5739d62d6"> Conclusion <a href="#e32a8549e23446ba8c9cffd5739d62d6">#</a> </h3> <p> I'm still re-orienting myself when it comes to understanding the relationship between cyclomatic complexity and test coverage. As part of that work, I wanted to refactor the Collatz code I originally showed. This article shows one way to decompose and reassemble the function in such a way that all branches are independent of each other, so that each can be covered by test cases without exercising the other branch. </p> <p> I don't know if this is useful to anyone else, but I found the hours well-spent. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b43aefd2fa5f4e7a916a31587fa4886e"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#b43aefd2fa5f4e7a916a31587fa4886e">#</a></div> <div class="comment-content"> <p> I really like this article. So much so that I tried to implement this approach for a recursive function at my work. However, I realized that there are some required conditions. </p> <p> First, the recusrive funciton must be tail recursive. Second, the recursive function must be closed (i.e. the output is a subset/subtype of the input). Neither of those were true for my function at work. An example of a function that doesn't satisfy either of these conditions is the function that computes the depth of a tree. </p> <p> A less serious issue is that your code, as currently implemented, requires that there only be one base case value. The issue is that you have duplicated code: the unique base case value appears both in the call to TakeWhile and in the subsequent call to Append. Instead of repeating yourself, I recommend defining an extension method on Enumerable called TakeUntil that works like TakeWhile but also returns the first value on which the predicate returned false. <a href="https://stackoverflow.com/questions/2242318/how-could-i-take-1-more-item-from-linqs-takewhile/6817553#6817553">Here</a> is an implementation of that extension method. </p> </div> <div class="comment-date">2023-06-22 13:45 UTC</div> </div> <div class="comment" id="420c5aef12504e048d5f8c6d2691f0fa"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#420c5aef12504e048d5f8c6d2691f0fa">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I suppose that you can't share the function that you mention, so I'll have to discuss it in general terms. </p> <p> As far as I can tell you can always(?) <a href="/2015/12/22/tail-recurse">refactor non-tail-recursive functions to tail-recursive implementations</a>. In practice, however, there's rarely need for that, since you can usually separate the problem into a general-purpose library function on the one hand, and your special function on the other. Examples of general-purpose functions are the various maps and folds. If none of the standard functions do the trick, the type's associated <a href="/2019/04/29/catamorphisms">catamorphism</a> ought to. </p> <p> One example of that is computing the depth of a tree, which we've <a href="/2019/08/05/rose-tree-catamorphism">already discussed</a>. </p> <p> I don't insist that any of this is universally true, so if you have another counter-example, I'd be keen to see it. </p> <p> You are, of course, right about using a <code>TakeUntil</code> extension instead. I was, however, trying to use as many built-in components as possible, so as to not unduly confuse casual readers. </p> </div> <div class="comment-date">2023-06-27 12:35 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Git repository that vanished https://blog.ploeh.dk/2023/06/05/the-git-repository-that-vanished 2023-06-05T06:38:00+00:00 Mark Seemann <div id="post"> <p> <em>A pair of simple operations resurrected it.</em> </p> <p> The other day I had an 'interesting' experience. I was about to create a small pull request, so I checked out a new branch in Git and switched to my editor in order to start coding when the battery on my laptop died. </p> <p> Clearly, when this happens, the computer immediately stops, without any graceful shutdown. </p> <p> I plugged in the laptop and booted it. When I navigated to the source code folder I was working on, the files where there, but it was no longer a Git repository! </p> <h3 id="25e1ded964e041ba82394682a7ce046b"> Git is fixable <a href="#25e1ded964e041ba82394682a7ce046b">#</a> </h3> <p> Git is more complex, and more powerful, than most developers care to deal with. Over the years, I've observed hundreds of people interact with Git in various ways, and most tend to give up at the first sign of trouble. </p> <p> The point of this article isn't to point fingers at anyone, but rather to serve as a gentle reminder that Git tends to be eminently fixable. </p> <p> Often, when people run into problems with Git, their only recourse is to delete the repository and clone it again. I've seen people do that enough times to realise that it might be helpful to point out: <em>You may not have to do that.</em> </p> <h3 id="c7afb00a22cd49f0a86b3c2e9f560d91"> Corruption <a href="#c7afb00a22cd49f0a86b3c2e9f560d91">#</a> </h3> <p> Since I <a href="https://stackoverflow.blog/2022/12/19/use-git-tactically/">use Git tactically</a> I have many repositories on my machine that have no remotes. In those cases, deleting the entire directory and cloning it from the remote isn't an option. I do take backups, though. </p> <p> Still, in this story, the repository I was working with <em>did</em> have a remote. Even so, I was reluctant to delete everything and start over, since I had multiple branches and stashes I'd used for various experiments. Many of those I'd never pushed to the remote, so starting over would mean that I'd lose all of that. It was, perhaps, not a catastrophe, but I would certainly prefer to restore my local repository, if possible. </p> <p> The symptoms were these: When you work with Git in Git Bash, the prompt will indicate which branch you're on. That information was absent, so I was already worried. A quick query confirmed my fears: </p> <p> <pre>$ git status fatal: not a git repository (or any of the parent directories): .git</pre> </p> <p> All the source code was there, but it looked as though the Git repository was gone. The code still compiled, but there was no source history. </p> <p> Since all code files were there, I had hope. It helps knowing that Git, too, is file-based, and all files are in a hidden directory called <code>.git</code>. If all the source code was still there, perhaps the <code>.git</code> files were there, too. Why wouldn't they be? </p> <p> <pre>$ ls .git COMMIT_EDITMSG description gitk.cache hooks/ info/ modules/ objects/ packed-refs config FETCH_HEAD HEAD index logs/ ms-persist.xml ORIG_HEAD refs/</pre> </p> <p> Jolly good! The <code>.git</code> files were still there. </p> <p> I now had a hypothesis: The unexpected shutdown of my machine had left some 'dangling pointers' in <code>.git</code>. A modern operating system may delay writes to disk, so perhaps my <code>git checkout</code> command had never made it all the way to disk - or, at least, not all of it. </p> <p> If the repository was 'merely' corrupted in the sense that a few of the reference pointers had gone missing, perhaps it was fixable. </p> <h3 id="a875078b3cbf466cbc8bf3396f9f8ce2"> Empty-headed <a href="#a875078b3cbf466cbc8bf3396f9f8ce2">#</a> </h3> <p> A few web searches indicated that the problem might be with the <code>HEAD</code> file, so I investigated its contents: </p> <p> <pre>$ cat .git/HEAD </pre> </p> <p> That was all. No output. The <code>HEAD</code> file was empty. </p> <p> That file is not supposed to be empty. It's supposed to contain a commit ID or a reference that tells the Git CLI what the current <em>head</em> is - that is, which commit is currently checked out. </p> <p> While I had checked out a new branch when my computer shut down, I hadn't written any code yet. Thus, the easiest remedy would be to restore the head to <code>master</code>. So I opened the <code>HEAD</code> file in Vim and added this to it: </p> <p> <pre>ref: refs/heads/master</pre> </p> <p> And just like that, the entire Git repository returned! </p> <h3 id="ce1dfafa82dd4ea0b4c6d5603f0111cf"> Bad object <a href="#ce1dfafa82dd4ea0b4c6d5603f0111cf">#</a> </h3> <p> The branches, the history, everything looked as though it was restored. A little more investigation, however, revealed one more problem: </p> <p> <pre>$ git log --oneline --all fatal: bad object refs/heads/some-branch</pre> </p> <p> While a normal <code>git log</code> command worked fine, as soon as I added the <code>--all</code> switch, I got that <code>bad object</code> error message, with the name of the branch I had just created before the computer shut down. (The name of that branch wasn't <code>some-branch</code> - that's just a surrogate I'm using for this article.) </p> <p> Perhaps this was the same kind of problem, so I explored the <code>.git</code> directory further and soon discovered a <code>some-branch</code> file in <code>.git/refs/heads/</code>. What did the contents look like? </p> <p> <pre>$ cat .git/refs/heads/some-branch </pre> </p> <p> Another empty file! </p> <p> Since I had never committed any work to that branch, the easiest fix was to simply delete the file: </p> <p> <pre>$ rm .git/refs/heads/some-branch</pre> </p> <p> That solved that problem as well. No more <code>fatal: bad object</code> error when using the <code>--all</code> switch with <code>git log</code>. </p> <p> No more problems have shown up since then. </p> <h3 id="018956b830c74f3797fff92449045b37"> Conclusion <a href="#018956b830c74f3797fff92449045b37">#</a> </h3> <p> My experience with Git is that it's so powerful that you can often run into trouble. On the other hand, it's also so powerful that you can also use it to extricate yourself from trouble. Learning how to do that will teach you how to use Git to your advantage. </p> <p> The problem that I ran into here wasn't fixable with the Git CLI itself, but turned out to still be easily remedied. A Git guru like <a href="https://megakemp.com/">Enrico Campidoglio</a> could most likely have solved my problems without even searching the web. The details of how to solve the problems were new to me, but it took me a few web searches and perhaps five-ten minutes to fix them. </p> <p> The point of this article, then, isn't in the details. It's that it pays to do a little investigation when you run into problems with Git. I already knew that, but I thought that this little story was a good occasion to share that knowledge. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Favour flat code file folders https://blog.ploeh.dk/2023/05/29/favour-flat-code-file-folders 2023-05-29T19:20:00+00:00 Mark Seemann <div id="post"> <p> <em>How code files are organised is hardly related to sustainability of code bases.</em> </p> <p> My recent article <a href="/2023/05/15/folders-versus-namespaces">Folders versus namespaces</a> prompted some reactions. A few kind people <a href="https://twitter.com/Savlambda/status/1658453377489960960">shared how they organise code bases</a>, both on Twitter and in the comments. Most reactions, however, carry the (subliminal?) subtext that organising code in file folders is how things are done. </p> <p> I'd like to challenge that notion. </p> <p> As is usually my habit, I mostly do this to make you think. I don't insist that I'm universally right in all contexts, and that everyone else are wrong. I only write to suggest that alternatives exist. </p> <p> The <a href="/2023/05/15/folders-versus-namespaces">previous article</a> wasn't a recommendation; it's was only an exploration of an idea. As I describe in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, I recommend flat folder structures. Put most code files in the same directory. </p> <h3 id="58c997618e794a26bc366859b216e226"> Finding files <a href="#58c997618e794a26bc366859b216e226">#</a> </h3> <p> People usually dislike that advice. <em>How can I find anything?!</em> </p> <p> Let's start with a counter-question: How can you find anything if you have a deep file hierarchy? Usually, if you've organised code files in subfolders of subfolders of folders, you typically start with a collapsed view of the tree. </p> <p> <img src="/content/binary/mostly-collapsed-solution-explorer-tree.png" alt="Mostly-collapsed Solution Explorer tree."> </p> <p> Those of my readers who know a little about search algorithms will point out that a <a href="https://en.wikipedia.org/wiki/Search_tree">search tree</a> is an efficient data structure for locating content. The assumption, however, is that you already know (or can easily construct) the <em>path</em> you should follow. </p> <p> In a view like the above, <em>most</em> files are hidden in one of the collapsed folders. If you want to find, say, the <code>Iso8601.cs</code> file, where do you look for it? Which path through the tree do you take? </p> <p> <em>Unfair!</em>, you protest. You don't know what the <code>Iso8601.cs</code> file does. Let me enlighten you: That file contains functions that render dates and times in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> formats. These are used to transmit dates and times between systems in a platform-neutral way. </p> <p> So where do you look for it? </p> <p> It's probably not in the <code>Controllers</code> or <code>DataAccess</code> directories. Could it be in the <code>Dtos</code> folder? <code>Rest</code>? <code>Models</code>? </p> <p> Unless your first guess is correct, you'll have to open more than one folder before you find what you're looking for. If each of these folders have subfolders of their own, that only exacerbates the problem. </p> <p> If you're curious, some programmer (me) decided to put the <code>Iso8601.cs</code> file in the <code>Dtos</code> directory, and perhaps you already guessed that. That's not the point, though. The point is this: 'Organising' code files in folders is only efficient if you can unerringly predict the correct path through the tree. You'll have to get it right the first time, every time. If you don't, it's not the most efficient way. </p> <p> Most modern code editors come with features that help you locate files. In <a href="https://visualstudio.microsoft.com/">Visual Studio</a>, for example, you just hit <kbd>Ctrl</kbd>+<kbd>,</kbd> and type a bit of the file name: <em>iso</em>: </p> <p> <img src="/content/binary/visual-studio-go-to-all.png" alt="Visual Studio Go To All dialog."> </p> <p> Then hit <kbd>Enter</kbd> to open the file. In <a href="https://code.visualstudio.com/">Visual Studio Code</a>, the corresponding keyboard shortcut is <kbd>Ctrl</kbd>+<kbd>p</kbd>, and I'd be highly surprised if other editors didn't have a similar feature. </p> <p> To conclude, so far: Organising files in a folder hierarchy is <em>at best</em> on par with your editor's built-in search feature, but is likely to be less productive. </p> <h3 id="f3dffaf6e73e42a6a10e1f403b8bf37b"> Navigating a code base <a href="#f3dffaf6e73e42a6a10e1f403b8bf37b">#</a> </h3> <p> What if you don't quite know the name of the file you're looking for? In such cases, the file system is even less helpful. </p> <p> I've seen people work like this: </p> <ol> <li>Look at some code. Identify another code item they'd like to view. (Examples may include: Looking at a unit test and wanting to see the <a href="https://en.wikipedia.org/wiki/System_under_test">SUT</a>, or looking at a class and wanting to see the base class.)</li> <li>Move focus to the editor's folder view (in Visual Studio called the <em>Solution Explorer</em>).</li> <li>Scroll to find the file in question.</li> <li>Double-click said file.</li> </ol> <p> Regardless of how the files are organised, you could, instead, <em>go to definition</em> (<kbd>F12</kbd> with my Visual Studio keyboard layout) in a single action. Granted, how well this works varies with editor and language. Still, even when editor support is less optimal (e.g. a code base with a mix of <a href="https://fsharp.org/">F#</a> and C#, or a <a href="https://www.haskell.org/">Haskell</a> code base), I can often find things faster with a search (<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>f</kbd>) than via the file system. </p> <p> A modern editor has efficient tools that can help you find what you're looking for. Looking through the file system is often the least efficient way to find the code you're looking for. </p> <h3 id="b050833e38ab4ee7a1ed3429979d8405"> Large code bases <a href="#b050833e38ab4ee7a1ed3429979d8405">#</a> </h3> <p> Do I recommend that you dump thousands of code files in a single directory, then? </p> <p> Hardly, but a question like that presupposes that code bases have thousands of code files. Or more, even. And I've seen such code bases. </p> <p> Likewise, it's a common complaint that Visual Studio is slow when opening solutions with hundreds of projects. And the day Microsoft fixes that problem, people are going to complain that it's slow when opening a solution with thousands of projects. </p> <p> Again, there's an underlying assumption: That a 'real' code base <em>must</em> be so big. </p> <p> Consider alternatives: Could you decompose the code base into multiple smaller code bases? Could you extract subsystems of the code base and package them as reusable packages? Yes, you can do all those things. </p> <p> Usually, I'd pull code bases apart long before they hit a thousand files. Extract modules, libraries, utilities, etc. and put them in separate code bases. Use existing package managers to distribute these smaller pieces of code. Keep the code bases small, and you don't need to organise the files. </p> <h3 id="64b5d272a18d41778a021540cd710fd1"> Maintenance <a href="#64b5d272a18d41778a021540cd710fd1">#</a> </h3> <p> <em>But, if all files are mixed together in a single folder, how do we keep the code maintainable?</em> </p> <p> Once more, implicit (but false) assumptions underlie such questions. The assumption is that 'neatly' organising files in hierarchies somehow makes the code easier to maintain. Really, though, it's more akin to a teenager who 'cleans' his room by sweeping everything off the floor only to throw it into his cupboard. It does enable hoovering the floor, but it doesn't make it easier to find anything. The benefit is mostly superficial. </p> <p> Still, consider a tree. </p> <p> <img src="/content/binary/file-tree.png" alt="A tree of folders with files."> </p> <p> This may not be the way you're used to see files and folders rendered, but this diagram emphases the tree structure and makes what happens next starker. </p> <p> The way that most languages work, putting code files in folders makes little difference to the compiler. If the classes in my <code>Controllers</code> folder need some classes from the <code>Dtos</code> folder, you just use them. You may need to import the corresponding namespace, but modern editors make that a breeze. </p> <p> <img src="/content/binary/two-files-coupled-across-tree-branches.png" alt="A tree of folders with files. Two files connect across the tree's branches."> </p> <p> In the above tree, the two files who now communicate are coloured orange. Notice that they span across two main branches of the tree. </p> <p> Thus, even though the files are organised in a tree, it has no impact on the maintainability of the code base. Code can reference other code in other parts of the tree. You can <a href="http://evelinag.com/blog/2014/06-09-comparing-dependency-networks/">easily create cycles in a language like C#</a>, and organising files in trees makes no difference. </p> <p> Most languages, however, enforce that library dependencies form a <a href="https://en.wikipedia.org/wiki/Directed_acyclic_graph">directed acyclic graph</a> (i.e. <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">if the data access library references the domain model, the domain model can't reference the data access library</a>). The C# (and most other languages) compiler enforces what <a href="/ref/appp">Robert C. Martin calls the Acyclic Dependencies Principle</a>. Preventing cycles prevents <a href="https://en.wikipedia.org/wiki/Spaghetti_code">spaghetti code</a>, which is <a href="/2022/11/21/decouple-to-delete">key to a maintainable code base</a>. </p> <p> (Ironically, <a href="/2015/04/15/c-will-eventually-get-all-f-features-right">one of the more controversial features of F# is actually one of its greatest strengths: It doesn't allow cycles</a>.) </p> <h3 id="ffe0131017254522acd40d2445929f24"> Tidiness <a href="#ffe0131017254522acd40d2445929f24">#</a> </h3> <p> Even so, I do understand the lure of organising code files in an elaborate hierarchy. It looks so <em>neat</em>. </p> <p> Previously, I've <a href="/2021/05/17/against-consistency">touched on the related topic of consistency</a>, and while I'm a bit of a neat freak myself, I have to realise that tidiness seems to be largely unrelated to the sustainability of a code base. </p> <p> As another example in this category, I've seen more than one code base with consistently beautiful documentation. Every method was adorned with formal <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/">XML documentation</a> with every input parameter as well as output described. </p> <p> Every new phase in a method was delineated with another neat comment, nicely adorned with a 'comment frame' and aligned with other comments. </p> <p> It was glorious. </p> <p> Alas, that documentation sat on top of 750-line methods with a cyclomatic complexity above 50. The methods were so long that <a href="/2019/12/23/the-case-of-the-mysterious-curly-bracket">developers had to introduce artificial variable scopes to avoid naming collisions</a>. </p> <p> The reason I was invited to look at that code in the first place was that the organisation had trouble with maintainability, and they asked me to help. </p> <p> It was neat, yet unmaintainable. </p> <p> This discussion about tidiness may seem like a digression, but I think it's important to make the implicit explicit. If I'm not much mistaken, preference for order is a major reason that so many developers want to organise code files into hierarchies. </p> <h3 id="47227afb0a674330bb1b3556f751799d"> Organising principles <a href="#47227afb0a674330bb1b3556f751799d">#</a> </h3> <p> What other motivations for file hierarchies could there be? How about the directory structure as an organising principle? </p> <p> The two most common organising principles are <a href="/2023/05/15/folders-versus-namespaces">those that I experimented with in the previous article</a>: </p> <ol> <li>By technical role (Controller, View Model, DTO, etc.)</li> <li>By feature</li> </ol> <p> A technical leader might hope that, by presenting a directory structure to team members, it imparts an organising principle on the code to be. </p> <p> It may even do so, but is that actually a benefit? </p> <p> It might subtly discourage developers from introducing code that doesn't fit into the predefined structure. If you organise code by technical role, developers might put most code in Controllers, producing mostly procedural <a href="https://martinfowler.com/eaaCatalog/transactionScript.html">Transaction Scripts</a>. If you organise by feature, this might encourage duplication because developers don't have a natural place to put general-purpose code. </p> <p> <em>You can put truly shared code in the root folder,</em> the counter-argument might be. This is true, but: </p> <ol> <li>This seems to be implicitly discouraged by the folder structure. After all, the hierarchy is there for a reason, right? Thus, any file you place in the root seems to suggest a failure of organisation.</li> <li>On the other hand, if you flaunt that not-so-subtle hint and put many code files in the root, what advantage does the hierarchy furnish?</li> </ol> <p> In <em>Information Distribution Aspects of Design Methodology</em> <a href="https://en.wikipedia.org/wiki/David_Parnas">David Parnas</a> writes about documentation standards: </p> <blockquote> <p> "standards tend to force system structure into a standard mold. A standard [..] makes some assumptions about the system. [...] If those assumptions are violated, the [...] organization fits poorly and the vocabulary must be stretched or misused." </p> <footer><cite><a href="https://en.wikipedia.org/wiki/David_Parnas">David Parnas</a>, <em>Information Distribution Aspects of Design Methodology</em></cite></footer> </blockquote> <p> (The above quote is on the surface about documentation standards, and I've deliberately butchered it a bit (clearly marked) to make it easier to spot the more general mechanism.) </p> <p> In the same paper, Parnas describes the danger of making hard-to-change decisions too early. Applied to directory structure, the lesson is that you should postpone designing a file hierarchy until you know more about the problem. Start with a flat directory structure and add folders later, if at all. </p> <h3 id="426c5128ef804c6abfe6005d267cb624"> Beyond files? <a href="#426c5128ef804c6abfe6005d267cb624">#</a> </h3> <p> My claim is that you don't need <em>much</em> in way of directory hierarchy. From this doesn't follow, however, that we may never leverage such options. Even though I left most of the example code for <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> in a single folder, I did add a specialised folder as an <a href="/ref/ddd">anti-corruption layer</a>. Folders do have their uses. </p> <blockquote> <p> "Why not take it to the extreme and place most code in a single file? If we navigate by "namespace view" and search, do we need all those files?" </p> <footer><cite><a href="https://twitter.com/ronnieholm/status/1662219232652963840">Ronnie Holm</a></cite></footer> </blockquote> <p> Following a thought to its extreme end can shed light on a topic. Why not, indeed, put all code in a single file? </p> <p> Curious thought, but possibly not new. I've never programmed in <a href="https://en.wikipedia.org/wiki/Smalltalk">SmallTalk</a>, but as I understand it, the language came with tooling that was both <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> and execution environment. Programmers would write source code in the editor, but although the code was persisted to disk, it may not have been as text files. </p> <p> Even if I completely misunderstand how SmallTalk worked, it's not inconceivable that you could have a development environment based directly on a database. Not that I think that this sounds like a good idea, but it sounds technically possible. </p> <p> Whether we do it one way or another seems mostly to be a question of tooling. What problems would you have if you wrote an entire C# (Java, Python, F#, or similar) code base as a single file? It becomes more difficult to look at two or more parts of the code base at the same time. Still, Visual Studio can actually give you split windows of the same file, but I don't know how it scales if you need multiple views over the same huge file. </p> <h3 id="fd3145a641ad4de18dbab9616e2ed4b7"> Conclusion <a href="#fd3145a641ad4de18dbab9616e2ed4b7">#</a> </h3> <p> I recommend flat directory structures for code files. Put most code files in the root of a library or app. Of course, if your system is composed from multiple libraries (dependencies), each library has its own directory. </p> <p> Subfolders aren't <em>prohibited</em>, only generally discouraged. Legitimate reasons to create subfolders may emerge as the code base evolves. </p> <p> My misgivings about code file directory hierarchies mostly stem from the impact they have on developers' minds. This may manifest as <a href="https://en.wikipedia.org/wiki/Magical_thinking">magical thinking</a> or <a href="https://en.wikipedia.org/wiki/Cargo_cult">cargo-cult programming</a>: Erect elaborate directory structures to keep out the evil spirits of spaghetti code. </p> <p> It doesn't work that way. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Visual Studio Code snippet to make URLs relative https://blog.ploeh.dk/2023/05/23/visual-studio-code-snippet-to-make-urls-relative 2023-05-23T19:23:00+00:00 Mark Seemann <div id="post"> <p> <em>Yes, it involves JSON and regular expressions.</em> </p> <p> Ever since I <a href="/2013/03/03/moving-the-blog-to-jekyll">migrated the blog off dasBlog</a> I've been <a href="https://rakhim.org/honestly-undefined/19/">writing the articles in raw HTML</a>. The reason is mostly a historical artefact: Originally, I used <a href="https://en.wikipedia.org/wiki/Windows_Live_Writer">Windows Live Writer</a>, but <a href="https://jekyllrb.com/">Jekyll</a> had no support for that, and since I'd been doing web development for more than a decade already, raw HTML seemed like a reliable and durable alternative. I increasingly find that relying on skill and knowledge is a far more durable strategy than relying on technology. </p> <p> For a decade I used <a href="https://www.sublimetext.com/">Sublime Text</a> to write articles, but over the years, I found it degrading in quality. I only used Sublime Text to author blog posts, so when I recently repaved my machine, I decided to see if I could do without it. </p> <p> Since I was already using <a href="https://code.visualstudio.com/">Visual Studio Code</a> for much of my programming, I decided to give it a go for articles as well. It always takes time when you decide to move off a tool you've been used for a decade, but after some initial frustrations, I quickly found a new modus operandi. </p> <p> One benefit of rocking the boat is that it prompts you to reassess the way you do things. Naturally, this happened here as well. </p> <h3 id="28218d2cd10945e0886bd528cf2d792f"> My quest for relative URLs <a href="#28218d2cd10945e0886bd528cf2d792f">#</a> </h3> <p> I'd been using a few Sublime Text snippets to automate a few things, like the markup for the section heading you see above this paragraph. Figuring out how to replicate that snippet in Visual Studio Code wasn't too hard, but as I was already perusing <a href="https://code.visualstudio.com/docs/editor/userdefinedsnippets">the snippet documentation</a>, I started investigating other options. </p> <p> One little annoyance I'd lived with for years was adding links to other articles on the blog. </p> <p> While I write an article, I run the site on my local machine. When linking to other articles, I sometimes use the existing page address off the public site, and sometimes I just copy the page address from <code>localhost</code>. In both cases, I want the URL to be relative so that I can navigate the site even if I'm offline. I've written enough articles on planes or while travelling without internet that this is an important use case for me. </p> <p> For example, if I want to link to the article <a href="/2023/01/02/adding-nuget-packages-when-offline">Adding NuGet packages when offline</a>, I want the URL to be <code>/2023/01/02/adding-nuget-packages-when-offline</code>, but that's not the address I get when I copy from the browser's address bar. Here, I get the full URL, with either <code>http://localhost:4000/</code> or <code>https://blog.ploeh.dk/</code> as the origin. </p> <p> For years, I've been manually stripping the origin away, as well as the trailing <code>/</code>. Looking through the Visual Studio Code snippet documentation, however, I eyed an opportunity to automate that workflow. </p> <h3 id="f13d4bb7acf84183b9ac18088206f0ca"> Snippet <a href="#f13d4bb7acf84183b9ac18088206f0ca">#</a> </h3> <p> I wanted a piece of editor automation that could modify a URL after I'd pasted it into the article. After a few iterations, I've settled on a <em>surround-with</em> snippet that works pretty well. It looks like this: </p> <p> <pre><span style="color:#2e75b6;">&quot;Make&nbsp;URL&nbsp;relative&quot;</span>:&nbsp;{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;prefix&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urlrel&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;body&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;${TM_SELECTED_TEXT/^(?:http(?:s?):\\/\\/(?:[^\\/]+))(.+)\\//$1/}&quot;</span>&nbsp;], &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;description&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Make&nbsp;URL&nbsp;relative.&quot;</span> }</pre> </p> <p> Don't you just love regular expressions? Write once, scrutinise forever. </p> <p> I don't want to go over all the details, because I've already forgotten most of them, but essentially this expression strips away the URL origin starting with either <code>http</code> or <code>https</code> until it finds the first slash <code>/</code>. </p> <p> The thing that makes it useful, though, is the <code>TM_SELECTED_TEXT</code> variable that tells Visual Studio Code that this snippet works on <em>selected</em> text. </p> <p> When I paste a URL into an <code>a</code> tag, at first nothing happens because no text is selected. I can then use <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>&rarr;</kbd> to expand the selection, at which point the Visual Studio Code lightbulb (<em>Code Action</em>) appears: </p> <p> <img src="/content/binary/make-url-relative-screen-shot.png" alt="Screen shot of the make-URL-relative code snippet in action."> </p> <p> Running the snippet removes the URL's origin, as well as the trailing slash, and I can move on to write the link text. </p> <h3 id="4e72443f63fe42e382e854fdc9a8d07a"> Conclusion <a href="#4e72443f63fe42e382e854fdc9a8d07a">#</a> </h3> <p> After I started using Visual Studio Code to write blog posts, I've created a few custom snippets to support my authoring workflow. Most of them are fairly mundane, but the <em>make-URLs-relative</em> snippet took me a few iterations to get right. </p> <p> I'm not expecting many of my readers to have this particular need, but I hope that this outline showcases the capabilities of Visual Studio Code snippets, and perhaps inspires you to look into creating custom snippets for your own purposes. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8d84d3c52d134dcda81f7b63faccb58b"> <div class="comment-author"><a href="https://chamook.lol">Adam Guest</a> <a href="#8d84d3c52d134dcda81f7b63faccb58b">#</a></div> <div class="comment-content"> <p> Seems like a useful function to have, so I naturally wondered if I could <del>make it worse</del> <ins>implement a similar function in Emacs</ins>. </p> <p> Emacs lisp has support for regular expressions, only typically with a bunch of extra slashes included, so I needed to figure out how to work with the currently selected text to get this to work. The currently selected text is referred to as the "region" and by specifying <code>"r"</code> as a parameter for the <code>interactive</code> call we can pass the start and end positions for the region directly to the function. </p> <p> I came up with this rather basic function: </p> <p> <pre> (defun make-url-relative (start end) "Converts the selected uri from an absolute url and converts it to a relative one. This is very simple and relies on the url starting with http/https, and removes each character to the first slash in the path" (interactive "r") (replace-regexp-in-region "http[s?]:\/\/.+\/" "" start end)) </pre> </p> <p> With this function included in config somewhere: it can be called by selecting a url, and using <kbd>M-x</kbd> <code>make-url-relative</code> (or assigned to a key binding as required) </p> <p> I'm not sure if there's an already existing package for this functionality, but I hadn't really thought to look for it before so thanks for the idea 😊 </p> </div> <div class="comment-date">2023-05-24 11:20 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Folders versus namespaces https://blog.ploeh.dk/2023/05/15/folders-versus-namespaces 2023-05-15T06:01:00+00:00 Mark Seemann <div id="post"> <p> <em>What if you allow folder and namespace structure to diverge?</em> </p> <p> I'm currently writing C# code with some first-year computer-science students. Since most things are new to them, they sometimes do things in a way that are 'not the way we usually do things'. As an example, teachers have instructed them to use namespaces, but apparently no-one have told them that the file folder structure has to mirror the namespace structure. </p> <p> The compiler doesn't care, but as long as I've been programming in C#, it's been idiomatic to do it that way. There's even <a href="https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130">a static code analysis rule</a> about it. </p> <p> The first couple of times they'd introduce a namespace without a corresponding directory, I'd point out that they are supposed to keep those things in sync. One day, however, it struck me: What happens if you flout that convention? </p> <h3 id="dac7601856e94a4e88315cb2e80f74e5"> A common way to organise code files <a href="#dac7601856e94a4e88315cb2e80f74e5">#</a> </h3> <p> Code scaffolding tools and wizards will often nudge you to organise your code according to technical concerns: Controllers, models, views, etc. I'm sure you've encountered more than one code base organised like this: </p> <p> <img src="/content/binary/code-organised-by-tech-responsibility.png" alt="Code organised into folders like Controllers, Models, DataAccess, etc."> </p> <p> You'll put all your Controller classes in the <em>Controllers</em> directory, and make sure that the namespace matches. Thus, in such a code base, the full name of the <code>ReservationsController</code> might be <code>Ploeh.Samples.Restaurants.RestApi.Controllers.ReservationsController</code>. </p> <p> A common criticism is that this is the <em>wrong</em> way to organise the code. </p> <h3 id="ea3a6df18fa641638f18e0b554c1ee7c"> The problem with trees <a href="#ea3a6df18fa641638f18e0b554c1ee7c">#</a> </h3> <p> The complaint that this is the wrong way to organise code implies that a correct way exists. I write about this in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>: </p> <blockquote> <p> Should you create a subdirectory for Controllers, another for Models, one for Filters, and so on? Or should you create a subdirectory for each feature? </p> <p> Few people like my answer: <em>Just put all files in one directory.</em> Be wary of creating subdirectories just for the sake of 'organising' the code. </p> <p> File systems are <em>hierarchies</em>; they are trees: a specialised kind of acyclic graph in which any two vertices are connected by exactly one path. Put another way, each vertex can have at most one parent. Even more bluntly: If you put a file in a hypothetical <code>Controllers</code> directory, you can't <em>also</em> put it in a <code>Calendar</code> directory. </p> </blockquote> <p> But what if you could? </p> <h3 id="bcdc2397846c45a9bcb3cea0e71cfa7a"> Namespaces disconnected from directory hierarchy <a href="#bcdc2397846c45a9bcb3cea0e71cfa7a">#</a> </h3> <p> The code that accompanies <em>Code That Fits in Your Head</em> is organised as advertised: 65 files in a single directory. (Tests go in separate directories, though, as they belong to separate libraries.) </p> <p> If you decide to ignore the convention that namespace structure should mirror folder structure, however, you now have a second axis of variability. </p> <p> As an experiment, I decided to try that idea with the book's code base. The above screen shot shows the stereotypical organisation according to technical responsibility, after I moved things around. To be clear: This isn't how the book's example code is organised, but an experiment I only now carried out. </p> <p> If you open the <code>ReservationsController.cs</code> file, however, I've now declared that it belongs to a namespace called <code>Ploeh.Samples.Restaurants.RestApi.Reservations</code>. Using Visual Studio's <em>Class View</em>, things look different from the <em>Solution Explorer:</em> </p> <p> <img src="/content/binary/code-organised-by-namespace.png" alt="Code organised into namespaces according to feature: Calandar, Reservations, etc."> </p> <p> Here I've organised the namespaces according to feature, rather than technical role. The screen shot shows the <em>Reservations</em> feature opened, while other features remain closed. </p> <h3 id="19763ef496cc424f8fd419bc4ad699d6"> Initial reactions <a href="#19763ef496cc424f8fd419bc4ad699d6">#</a> </h3> <p> This article isn't a recommendation. It's nothing but an initial exploration of an idea. </p> <p> Do I like it? So far, I think I still prefer flat directory structures. Even though this idea gives two axes of variability, you still have to make judgment calls. It's easy enough with Controllers, but where do you put cross-cutting concerns? Where do you put domain logic that seems to encompass everything else? </p> <p> As an example, the code base that accompanies <em>Code That Fits in Your Head</em> is a multi-tenant system. Each restaurant is a separate tenant, but I've modelled restaurants as part of the domain model, and I've put that 'feature' in its own namespace. Perhaps that's a mistake; at least, I now have the code wart that I have to import the <code>Ploeh.Samples.Restaurants.RestApi.Restaurants</code> namespace to implement the <code>ReservationsController</code>, because its constructor looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;IClock&nbsp;<span style="font-weight:bold;color:#1f377f;">clock</span>, &nbsp;&nbsp;&nbsp;&nbsp;IRestaurantDatabase&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurantDatabase</span>, &nbsp;&nbsp;&nbsp;&nbsp;IReservationsRepository&nbsp;<span style="font-weight:bold;color:#1f377f;">repository</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Clock&nbsp;=&nbsp;clock; &nbsp;&nbsp;&nbsp;&nbsp;RestaurantDatabase&nbsp;=&nbsp;restaurantDatabase; &nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; }</pre> </p> <p> The <code>IRestaurantDatabase</code> interface is defined in the <code>Restaurants</code> namespace, but the Controller needs it in order to look up the restaurant (i.e. tenant) in question. </p> <p> You could argue that this isn't a problem with namespaces, but rather a code smell indicating that I should have organised the code in a different way. </p> <p> That may be so, but then implies a deeper problem: Assigning files to hierarchies may not, after all, help much. It looks as though things are organised, but if the assignment of things to buckets is done without a predictable system, then what benefit does it provide? Does it make things easier to find, or is the sense of order mostly illusory? </p> <p> I tend to still believe that this is the case. This isn't a nihilistic or defeatist position, but rather a realisation that order must arise from other origins. </p> <h3 id="305d423972e0422fa5ecdd04326cd132"> Conclusion <a href="#305d423972e0422fa5ecdd04326cd132">#</a> </h3> <p> I was recently repeatedly encountering student code with a disregard for the convention that namespace structure should follow directory structure (or the other way around). Taking a cue from <a href="https://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a> I decided to investigate what happens if you forget about the rules and instead pursue what that new freedom might bring. </p> <p> In this article, I briefly show an example where I reorganised a code base so that the file structure is according to implementation detail, but the namespace hierarchy is according to feature. Clearly, I could also have done it the other way around. </p> <p> What if, instead of two, you have three organising principles? I don't know. I can't think of a third kind of hierarchy in a language like C#. </p> <p> After a few hours reorganising the code, I'm not scared away from this idea. It might be worth to revisit in a larger code base. On the other hand, I'm still not convinced that forcing a hierarchy over a sophisticated software design is particularly beneficial. </p> <p> <ins datetime="2023-05-30T12:22Z"><strong>P.S. 2023-05-30.</strong> This article is only a report on an experiment. For my general recommendation regarding code file organisation, see <a href="/2023/05/29/favour-flat-code-file-folders">Favour flat code file folders</a>.</ins> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="6c209fa61ad34ef3aa8290b06a964aaf"> <div class="comment-author"><a href="http://github.com/m4rsh">Markus Schmits</a> <a href="#6c209fa61ad34ef3aa8290b06a964aaf">#</a></div> <div class="comment-content"> <p> Hi Mark, <br> While reading your book "Code That Fits in Your Head", your latest blog entry caught my attention, as I am struggling in software development with similar issues. <br> I find it hard, to put all classes into one project directory, as it feels overwhelming, when the number of classes increases. <br> In the following, I would like to specify possible organising principles in my own words. <p> <b> Postulations </b> <br>- Folders should help the programmer (and reader) to keep the code organised <br> - Namespaces should reflect the hierarchical organisation of the code base <br> - Cross-cutting concerns should be addressed by modularity. </p> <p> <b> Definitions </b> <br> 1. Folders <br> - the allocation of classes in a project with similar technical concerns into folders should help the programmer in the first place, by visualising this similarity <br> - the benefit lies just in the organisation, i.e. storage of code, not in the expression of hierarchy </p> <p> 2. Namespaces <br> - expression of hierarchy can be achieved by namespaces, which indicate the relationship between allocated classes <br> - classes can be organised in folders with same designation <br> - the namespace designation could vary by concerns, although the classes are placed in same folders, as the technical concern of the class shouldn't affect the hierarchical organisation </p> <p> 3. Cross-cutting concerns <br> - classes, which aren't related to a single task, could be indicated by a special namespace <br> - they could be placed in a different folder, to signalize different affiliations <br> - or even placed in a different assembly </p> <p> <b> Summing up </b> <br> A hierarchy should come by design. The organisation of code in folders should help the programmer or reader to grasp the file structure, not necessarily the program hierarchy. <br>Folders should be a means, not an expression of design. Folders and their designations could change (or disappear) over time in development. Thus, explicit connection of namespace to folder designation seems not desirable, but it's not forbidden. </p> All views above are my own. Please let me know, what you think. <p> Best regards, <br>Markus </p> </p> </div> <div class="comment-date">2023-05-18 19:13 UTC</div> </div> <div class="comment" id="3178e0d2d3494f7db7188ed455b78103"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3178e0d2d3494f7db7188ed455b78103">#</a></div> <div class="comment-content"> <p> Markus, thank you for writing. You can, of course, organise code according to various principles, and what works in one case may not be the best fit in another case. The main point of this article was to suggest, as an idea, that folder hierarchy and namespace hierarchy doesn't <em>have</em> to match. </p> <p> Based on reader reactions, however, I realised that I may have failed to clearly communicate my fundamental position, so I wrote <a href="/2023/05/29/favour-flat-code-file-folders">another article about that</a>. I do, indeed, favour flat folder hierarchies. </p> <p> That is not to say that you can't have any directories in your code base, but rather that I'm sceptical that any such hierarchy addresses real problems. </p> <p> For instance, you write that </p> <blockquote> <p> "Folders should help the programmer (and reader) to keep the code organised" </p> </blockquote> <p> If I focus on the word <em>should</em>, then I agree: Folders <em>should</em> help the programmer keep the code organised. In my view, then, it follows that if a tree structure does <em>not</em> assist in doing that, then that structure is of no use and should not be implemented (or abandoned if already in place). </p> <p> I do get the impression from many people that they consider a directory tree vital to be able to navigate and understand a code base. What I've tried to outline in <a href="/2023/05/29/favour-flat-code-file-folders">my more recent article</a> is that I don't accept that as an undisputable axiom. </p> <p> What I <em>do</em> find helpful as an organising principle is focusing on dependencies as a directed acyclic graph. Cyclic dependencies between objects is a main source of complexity. Keep dependency graphs directed and <a href="/2022/11/21/decouple-to-delete">make code easy to delete</a>. </p> <p> Organising code files in a tree structure doesn't help achieve that goal. This is the reason I consider code folder hierarchies a red herring: Perhaps not explicitly detrimental to sustainability, but usually nothing but a distraction. </p> <p> How, then, do you organise a large code base? I hope that I answer that question, too, in my more recent article <a href="/2023/05/29/favour-flat-code-file-folders">Favour flat code file folders</a>. </p> </div> <div class="comment-date">2023-06-13 6:11 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Is cyclomatic complexity really related to branch coverage? https://blog.ploeh.dk/2023/05/08/is-cyclomatic-complexity-really-related-to-branch-coverage 2023-05-08T05:38:00+00:00 Mark Seemann <div id="post"> <p> <em>A genuine case of doubt and bewilderment.</em> </p> <p> Regular readers of this blog may be used to its confident and opinionated tone. I write that way, not because I'm always convinced that I'm right, but because prose with too many caveats and qualifications tends to bury the message in verbose and circumlocutory ambiguity. </p> <p> This time, however, I write to solicit feedback, and because I'm surprised to the edge of bemusement by a recent experience. </p> <h3 id="6ebe2eae2250441c918433ba40ce8e86"> Collatz sequence <a href="#6ebe2eae2250441c918433ba40ce8e86">#</a> </h3> <p> Consider the following code: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Collatz</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">Sequence</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">n</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(n&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(n), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Only&nbsp;natural&nbsp;numbers&nbsp;allowed,&nbsp;but&nbsp;given&nbsp;</span>{n}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sequence</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;List&lt;<span style="color:blue;">int</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">current</span>&nbsp;=&nbsp;n; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(current&nbsp;!=&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sequence.Add(current); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(current&nbsp;%&nbsp;2&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;current&nbsp;/&nbsp;2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;current&nbsp;*&nbsp;3&nbsp;+&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sequence.Add(current); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;sequence; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As the names imply, the <code>Sequence</code> function calculates the <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">Collatz sequence</a> for a given natural number. </p> <p> Please don't tune out if that sounds mathematical and difficult, because it really isn't. While the Collatz conjecture still evades mathematical proof, the sequence is easy to calculate and understand. Given a number, produce a sequence starting with that number and stop when you arrive at 1. Every new number in the sequence is based on the previous number. If the input is even, divide it by two. If it's odd, multiply it by three and add one. Repeat until you arrive at one. </p> <p> The conjecture is that any natural number will produce a finite sequence. That's the unproven part, but that doesn't concern us. In this article, I'm only interested in the above code, which computes such sequences. </p> <p> Here are few examples: </p> <p> <pre>&gt; Collatz.Sequence(1) List&lt;<span style="color:blue;">int</span>&gt;(1)&nbsp;{&nbsp;1&nbsp;} &gt; Collatz.Sequence(2) List&lt;<span style="color:blue;">int</span>&gt;(2)&nbsp;{&nbsp;2,&nbsp;1&nbsp;} &gt; Collatz.Sequence(3) List&lt;<span style="color:blue;">int</span>&gt;(8)&nbsp;{&nbsp;3,&nbsp;10,&nbsp;5,&nbsp;16,&nbsp;8,&nbsp;4,&nbsp;2,&nbsp;1&nbsp;} &gt; Collatz.Sequence(4) List&lt;<span style="color:blue;">int</span>&gt;(3)&nbsp;{&nbsp;4,&nbsp;2,&nbsp;1&nbsp;}</pre> </p> <p> While there seems to be a general tendency for the sequence to grow as the input gets larger, that's clearly not a rule. The examples show that the sequence for <code>3</code> is longer than the sequence for <code>4</code>. </p> <p> All this, however, just sets the stage. The problem doesn't really have anything to do with Collatz sequences. I only ran into it while working with a Collatz sequence implementation that looked a lot like the above. </p> <h3 id="08c0cb2794184e9da8b9f72e6c9ce985"> Cyclomatic complexity <a href="#08c0cb2794184e9da8b9f72e6c9ce985">#</a> </h3> <p> What is the <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of the above <code>Sequence</code> function? If you need a reminder of how to count cyclomatic complexity, this is a good opportunity to take a moment to refresh your memory, count the number, and compare it with my answer. </p> <p> Apart from the opportunity for exercise, it was a rhetorical question. The answer is <em>4</em>. </p> <p> This means that we'd need <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">at least four unit test to cover all branches</a>. Right? Right? </p> <p> Okay, let's try. </p> <h3 id="d10688e0a13241c7b7124a5ce8f063ef"> Branch coverage <a href="#d10688e0a13241c7b7124a5ce8f063ef">#</a> </h3> <p> Before we start, let's make the ritual <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">denouncement of code coverage as a target metric</a>. The point isn't to reach 100% code coverage as such, but to <a href="/2018/11/12/what-to-test-and-not-to-test">gain confidence that you've added tests that cover whatever is important to you</a>. Also, the best way to do that is usually with TDD, which isn't the situation I'm discussing here. </p> <p> The first branch that we might want to cover is the Guard Clause. This is easily addressed with an <a href="https://xunit.net/">xUnit.net</a> test: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ThrowOnInvalidInput</span>() { &nbsp;&nbsp;&nbsp;&nbsp;Assert.Throws&lt;ArgumentOutOfRangeException&gt;(()&nbsp;=&gt;&nbsp;Collatz.Sequence(0)); }</pre> </p> <p> This test calls the <code>Sequence</code> function with <code>0</code>, which (in this context, at least) isn't a <a href="https://en.wikipedia.org/wiki/Natural_number">natural number</a>. </p> <p> If you measure test coverage (or, in this case, just think it through), there are no surprises yet. One branch is covered, the rest aren't. That's 25%. </p> <p> (If you use the <a href="https://learn.microsoft.com/dotnet/core/testing/unit-testing-code-coverage">free code coverage option for .NET</a>, it will surprisingly tell you that you're only at 16% branch coverage. It deems the cyclomatic complexity of the <code>Sequence</code> function to be 6, not 4, and 1/6 is 16.67%. Why it thinks it's 6 is not entirely clear to me, but Visual Studio agrees with me that the cyclomatic complexity is 4. In this particular case, it doesn't matter anyway. The conclusion that follows remains the same.) </p> <p> Let's add another test case, and perhaps one that gives the algorithm a good exercise. </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Example</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;Collatz.Sequence(5); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;5,&nbsp;16,&nbsp;8,&nbsp;4,&nbsp;2,&nbsp;1&nbsp;},&nbsp;actual); }</pre> </p> <p> As expected, the test passes. What's the branch coverage now? </p> <p> Try to think it through instead of relying exclusively on a tool. The algorithm isn't more complicated that you can emulate execution in your head, or perhaps with the assistance of a notepad. How many branches does it execute when the input is <code>5</code>? </p> <p> Branch coverage is now 100%. (Even the <em>dotnet</em> coverage tool agrees, despite its weird cyclomatic complexity value.) All branches are exercised. </p> <p> Two tests produce 100% branch coverage of a function with a cyclomatic complexity of 4. </p> <h3 id="7a4d9c5fbb8e4e94a52c61990565e38f"> Surprise <a href="#7a4d9c5fbb8e4e94a52c61990565e38f">#</a> </h3> <p> That's what befuddles me. I thought that cyclomatic complexity and branch coverage were related. I thought, that the number of branches was a good indicator of the number of tests you'd need to cover all branches. I even wrote <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">an article to that effect</a>, and no-one contradicted me. </p> <p> That, in itself, is no proof of anything, but the notion that the article presents seems to be widely accepted. I never considered it controversial, and the only reason I didn't cite anyone is that this seems to be 'common knowledge'. I wasn't aware of a particular source I could cite. </p> <p> Now, however, it seems that it's wrong. Is it wrong, or am I missing something? </p> <p> To be clear, I completely understand why the above two tests are sufficient to fully cover the function. I also believe that I fully understand why the cyclomatic complexity is 4. </p> <p> I am also painfully aware that the above two tests in no way fully specify the Collatz sequence. That's not the point. </p> <p> The point is that it's possible to cover this function with only two tests, despite the cyclomatic complexity being 4. That surprises me. </p> <p> Is this a known thing? </p> <p> I'm sure it is. I've long since given up discovering anything new in programming. </p> <h3 id="829b5f9b3e9449d1a023d2bacff5b58c"> Conclusion <a href="#829b5f9b3e9449d1a023d2bacff5b58c">#</a> </h3> <p> I recently encountered a function that performed a Collatz calculation similar to the one I've shown here. It exhibited the same trait, and since it had no Guard Clause, I could fully cover it with a single test case. That function even had a cyclomatic complexity of 6, so you can perhaps imagine my befuddlement. </p> <p> Is it wrong, then, that cyclomatic complexity suggests a minimum number of test cases in order to cover all branches? </p> <p> It seems so, but that's new to me. I don't mind being wrong on occasion. It's usually an opportunity to learn something new. If you have any insights, please <a href="https://github.com/ploeh/ploeh.github.com#comments">leave a comment</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="02568f995d91432da540858644b61e89"> <div class="comment-author"><a href="http://github.com/neongraal">Struan Judd</a> <a href="#02568f995d91432da540858644b61e89">#</a></div> <div class="comment-content"> <p> My first thought is that the code looks like an unrolled recursive function, so perhaps if it's refactored into a driver function and a "continuation passing style" it might make the cyclomatic complexity match the covering tests. </p> <p> So given the following: <pre>public delegate void ResultFunc(IEnumerable&lt;int&gt; result); public delegate void ContFunc(int n, ResultFunc result, ContFunc cont); public static void Cont(int n, ResultFunc result, ContFunc cont) { &nbsp;&nbsp;if (n == 1) { &nbsp;&nbsp;&nbsp;&nbsp;result(new[] { n }); &nbsp;&nbsp;&nbsp;&nbsp;return; &nbsp;&nbsp;} &nbsp;&nbsp;void Result(IEnumerable&lt;int&gt; list) => result(list.Prepend(n)); &nbsp;&nbsp;if (n % 2 == 0) &nbsp;&nbsp;&nbsp;&nbsp;cont(n / 2, Result, cont); &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;cont(n * 3 + 1, Result, cont); } public static IReadOnlyCollection&lt;int&gt; Continuation(int n) { &nbsp;&nbsp;if (n &lt; 1) &nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(n), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$"Only natural numbers allowed, but given {n}."); &nbsp;&nbsp;var output = new List&lt;int&gt;(); &nbsp;&nbsp;void Output(IEnumerable&lt;int&gt; list) => output = list.ToList(); &nbsp;&nbsp;Cont(n, Output, Cont); &nbsp;&nbsp;return output; }</pre> </p> <p> I calculate the Cyclomatic complexity of <code>Continuation</code> to be <em>2</em> and <code>Step</code> to be <em>3</em>. </p> <p> And it would seem you need 5 tests to properly cover the code, 3 for <code>Step</code> and 2 for <code>Continuation</code>. </p> <p> But however you write the "n >=1" case for <code>Continuation</code> you will have to cover some of <code>Step</code>. </p> </div> <div class="comment-date">2023-05-08 10:11 UTC</div> </div> <div class="comment" id="896f7e7c979144438a6e7f1a66dd72ea"> <div class="comment-author">Jeroen Heijmans <a href="#896f7e7c979144438a6e7f1a66dd72ea">#</a></div> <div class="comment-content"> <p> There is a relation between cyclomatic complexity and branches to cover, but it's not one of equality, cyclomatic complexity is an upper bound for the number of branches. There's a nice example illustrating this in the <a href="https://en.wikipedia.org/w/index.php?title=Cyclomatic_complexity#Implications_for_software_testing">Wikipedia article on cyclomatic complexity</a> that explains this, as well as the relation with path coverage (for which cyclomatic complexity is a lower bound). </p> </div> <div class="comment-date">2023-05-08 15:03 UTC</div> </div> <div class="comment" id="b683f78855f8440389b973e24c88c253"> <div class="comment-author"><a href="https://github.com/bretthall">Brett Hall</a> <a href="#b683f78855f8440389b973e24c88c253">#</a></div> <div class="comment-content"> <p> I find cyclomatic complexity to be overly pedantic at times, and you will need four tests if you get really pedantic. First, test the guard clause as you already did. Then, test with 1 in order to test the <pre>while</pre> loop body not being run. Then, test with 2 in order to test that the <pre>while</pre> is executed, but we only hit the <pre>if</pre> part of the <pre>if/else</pre>. Finally, test with 3 in order to hit the <pre>else</pre> inside of the <pre>while</pre>. That's four tests where each test is only testing one of the branches (some tests hit more than one branch, but the "extra branch" is already covered by another test). Again, this is being really pedantic and I wouldn't test this function as laid out above (I'd probaby put in the test with 1, since it's an edge case, but otherwise test as you did). </p> <p> I don't think there's a rigorous relationship between cyclomatic complexity and number of tests. In simple cases, treating things as though the relationship exists can be helpful. But once you start having iterrelated branches in a function, things get murky, and you may have to go to pedantic lengths in order to maintain the relationship. The same thing goes for code coverage, which can be 100% even though you haven't actually tested all paths through your code if there are multiple branches in the function that depend on each other. </p> </div> <div class="comment-date">2023-05-08 15:30 UTC</div> </div> <div class="comment" id="61939b516c0e4c2caab7c6e8a3302595"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#61939b516c0e4c2caab7c6e8a3302595">#</a></div> <div class="comment-content"> <p> Thank you, all, for writing. I'm extraordinarily busy at the moment, so it'll take me longer than usual to respond. Rest assured, however, that I haven't forgotten. </p> </div> <div class="comment-date">2023-05-11 12:42 UTC</div> </div> <div class="comment" id="e91eeb8bd09f446ab863f51ae30afad9"> <div class="comment-author"><a href="https://www.nikolamilekic.com">Nikola Milekic</a> <a href="#e91eeb8bd09f446ab863f51ae30afad9">#</a></div> <div class="comment-content"> <p> If we agree to the definition of cyclomatic complexity as the number of independent paths through a section of code, then the number of tests needed to cover that section <strong>must be</strong> the same per definition, <strong>if those tests are also independent</strong>. Independence is crucial here, and is also the main source of confusion. Both the <code>while</code> and <code>if</code> forks depend on the same variable (<code>current</code>), and so they are not independent. </p> <p> The second test you wrote is similarly not independent, as it ends up tracing multiple paths through through <code>if</code>: odd for 5, and even for 16, 8, etc, and so ends up covering all paths. Had you picked 2 instead of 5 for the test, that would have been more independent, as it would not have traced the <code>else</code> path, requiring one additional test. </p> <p> The standard way of computing cyclomatic complexity assumes independence, which simply is not possible in this case. </p> </div> <div class="comment-date">2023-06-02 00:38 UTC</div> </div> <div class="comment" id="1fafb3fa289a415f9102dc8d6defc464"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1fafb3fa289a415f9102dc8d6defc464">#</a></div> <div class="comment-content"> <p> Struan, thank you for writing, and please accept my apologies for the time it took me to respond. I agree with your calculations of cyclomatic complexity of your refactored code. </p> <p> I agree with what you write, but you can't write a sentence like "however you write the "n >=1" case for [...] you will have to cover some of [..]" and expect me to just ignore it. To be clear, I agree with you in the particular case of the methods you provided, but you inspired me to refactor my code with that rule as a specific constraint. You can see the results in my new article <a href="/2023/06/12/collatz-sequences-by-function-composition">Collatz sequences by function composition</a>. </p> <p> Thank you for the inspiration. </p> </div> <div class="comment-date">2023-06-12 5:46 UTC</div> </div> <div class="comment" id="2878d9f87f90405aa64ed1d1400d8d2b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2878d9f87f90405aa64ed1d1400d8d2b">#</a></div> <div class="comment-content"> <p> Jeroen, thank you for writing, and please accept my apologies for the time it took me to respond. I should have read that Wikipedia article more closely, instead of just linking to it. </p> <p> What still puzzles me is that I've been aware of, and actively used, cyclomatic complexity for more than a decade, and this distinction has never come up, and no-one has called me out on it. </p> <p> As <a href="https://en.wikipedia.org/wiki/Ward_Cunningham#%22Cunningham's_Law%22">Cunningham's law</a> says, <em>the best way to get the right answer on the Internet is not to ask a question; it's to post the wrong answer.</em> Even so, I posted <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">Put cyclomatic complexity to good use</a> in 2019, and no-one contradicted it. </p> <p> I don't mention this as an argument that I'm right. Obviously, I was wrong, but no-one told me. Have I had something in my teeth all these years, too? </p> </div> <div class="comment-date">2023-06-12 6:35 UTC</div> </div> <div class="comment" id="da62194dabc947d0b3ecd7c4258b0e86"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#da62194dabc947d0b3ecd7c4258b0e86">#</a></div> <div class="comment-content"> <p> Brett, thank you for writing, and please accept my apologies for the time it took me to respond. I suppose that I failed to make my overall motivation clear. When doing proper test-driven development (TDD), one doesn't need cyclomatic complexity in order to think about coverage. When following the <a href="/2019/10/21/a-red-green-refactor-checklist">red-green-refactor checklist</a>, you only add enough code to pass all tests. With that process, cyclomatic complexity is rarely useful, and I tend to ignore it. </p> <p> I do, however, often coach programmers in unit testing and TDD, and people new to the technique often struggle with basics. They add too much code, instead of the simplest thing that could possibly work, or they can't think of a good next test case to write. </p> <p> When teaching TDD I sometimes suggest cyclomatic complexity as a metric to help decision-making. <em>Did we add more code to the System Under Test than warranted by tests? Is it okay to forgo writing a test of a one-liner with cyclomatic complexity of one?</em> </p> <p> The metric is also useful in hybrid scenarios where you already have production code, and now you want to add <a href="https://en.wikipedia.org/wiki/Characterization_test">characterisation tests</a>: Which test cases should you <em>at least</em> write? </p> <p> Another way to answer such questions is to run a code-coverage tool, but that often takes time. I find it useful to teach people about cyclomatic complexity, because it's a lightweight heuristic always at hand. </p> </div> <div class="comment-date">2023-06-12 7:24 UTC</div> </div> <div class="comment" id="01b5af5ccab04843911cde37104c4a7c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#01b5af5ccab04843911cde37104c4a7c">#</a></div> <div class="comment-content"> <p> Nikola, thank you for writing. The emphasis on independence is useful; I used compatible thinking in my new article <a href="/2023/06/12/collatz-sequences-by-function-composition">Collatz sequences by function composition</a>. By now, including the other comments to this article, it seems that we've been able to cover the problem better, and I, at least, feel that I've learned something. </p> <p> I don't think, however, that the standard way of computing cyclomatic complexity assumes independence. You can easily compute the cyclomatic complexity of the above <code>Sequence</code> function, even though its branches aren't independent. Tooling such as Visual Studio seems to agree with me. </p> </div> <div class="comment-date">2023-06-13 5:32 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Refactoring pure function composition without breaking existing tests https://blog.ploeh.dk/2023/05/01/refactoring-pure-function-composition-without-breaking-existing-tests 2023-05-01T06:44:00+00:00 Mark Seemann <div id="post"> <p> <em>An example modifying a Haskell Gossiping Bus Drivers implementation.</em> </p> <p> This is an article in an series of articles about the <a href="/2023/02/13/epistemology-of-interaction-testing">epistemology of interaction testing</a>. In short, this collection of articles discusses how to test the composition of <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. While a pure function is <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>, how do you test the composition of pure functions? As the introductory article outlines, I consider it mostly a matter of establishing confidence. With <a href="/2018/11/12/what-to-test-and-not-to-test">enough test coverage</a> you can be confident that the composition produces the desired outputs. </p> <p> Keep in mind that if you compose pure functions into a larger pure function, the composition is still pure. This implies that you can still test it by supplying input and verifying that the output is correct. </p> <p> Tests that exercise the composition do so by verifying observable behaviour. This makes them more robust to refactoring. You'll see an example of that later in this article. </p> <h3 id="32b583422c354d0f8468406f0486a762"> Gossiping bus drivers <a href="#32b583422c354d0f8468406f0486a762">#</a> </h3> <p> I recently did the <a href="https://kata-log.rocks/gossiping-bus-drivers-kata">Gossiping Bus Drivers</a> kata in <a href="https://www.haskell.org/">Haskell</a>. At first, I added the tests suggested in the kata description. </p> <p> <pre>{-#&nbsp;OPTIONS_GHC&nbsp;-Wno-type-defaults&nbsp;#-} <span style="color:blue;">module</span>&nbsp;Main&nbsp;<span style="color:blue;">where</span> <span style="color:blue;">import</span>&nbsp;GossipingBusDrivers <span style="color:blue;">import</span>&nbsp;Test.HUnit <span style="color:blue;">import</span>&nbsp;Test.Framework.Providers.HUnit&nbsp;(<span style="color:#2b91af;">hUnitTestToTests</span>) <span style="color:blue;">import</span>&nbsp;Test.Framework&nbsp;(<span style="color:#2b91af;">defaultMain</span>) <span style="color:#2b91af;">main</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;=&nbsp;defaultMain&nbsp;$&nbsp;hUnitTestToTests&nbsp;$&nbsp;TestList&nbsp;[ &nbsp;&nbsp;<span style="color:#a31515;">&quot;Kata&nbsp;examples&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;(routes,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[3,&nbsp;1,&nbsp;2,&nbsp;3], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3,&nbsp;2,&nbsp;3,&nbsp;1], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[4,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;5), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[2,&nbsp;1,&nbsp;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[5,&nbsp;2,&nbsp;8]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;drive&nbsp;routes &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual &nbsp;&nbsp;]</pre> </p> <p> As I prefer them, these tests are <a href="/2018/04/30/parametrised-unit-tests-in-haskell">parametrised HUnit tests</a>. </p> <p> The problem with those suggested test cases is that they don't provide enough confidence that an implementation is correct. In fact, I wrote this implementation to pass them: </p> <p> <pre>drive&nbsp;routes&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:blue;">length</span>&nbsp;routes&nbsp;==&nbsp;3&nbsp;<span style="color:blue;">then</span>&nbsp;Just&nbsp;5&nbsp;<span style="color:blue;">else</span>&nbsp;Nothing</pre> </p> <p> This is clearly incorrect. It just looks at the number of routes and returns a fixed value for each count. It doesn't look at the contents of the routes. </p> <p> Even if you don't <a href="/2019/10/07/devils-advocate">try to deliberately cheat</a> I'm not convinced that these two tests are enough. You could <em>try</em> to write the correct implementation, but how do you know that you've correctly dealt with various edge cases? </p> <h3 id="0e40e3994772481f855b266452685852"> Helper function <a href="#0e40e3994772481f855b266452685852">#</a> </h3> <p> The kata description isn't hard to understand, so while the suggested test cases seem insufficient, I knew what was required. Perhaps I could write a proper implementation without additional tests. After all, I was convinced that it'd be possible to do it with a <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of <em>1</em>, and since <a href="/2013/04/02/why-trust-tests">a test function also has a cyclomatic complexity of <em>1</em></a>, there's always that tension in test-driven development: Why write test code to exercise code with a cyclomatic complexity of <em>1?</em>. </p> <p> To be clear: There are often good reasons to write tests even in this case, and this seems like one of them. <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">Cyclomatic complexity indicates a minimum number of test cases</a>, not necessarily a sufficient number. </p> <p> Even though Haskell's type system is expressive, I soon found myself second-guessing the behaviour of various expressions that I'd experimented with. Sometimes I find GHCi (the Haskell <a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>) sufficiently edifying, but in this case I thought that I might want to keep some test cases around for a helper function that I was developing: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;Data.List <span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.Map.Strict&nbsp;<span style="color:blue;">as</span>&nbsp;Map <span style="color:blue;">import</span>&nbsp;Data.Map.Strict&nbsp;(<span style="color:#2b91af;">(!)</span>) <span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.Set&nbsp;<span style="color:blue;">as</span>&nbsp;Set <span style="color:blue;">import</span>&nbsp;Data.Set&nbsp;(<span style="color:blue;">Set</span>) <span style="color:#2b91af;">evaluateStop</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Functor</span>&nbsp;f,&nbsp;<span style="color:blue;">Foldable</span>&nbsp;f,&nbsp;<span style="color:blue;">Ord</span>&nbsp;k,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;f&nbsp;(k,&nbsp;Set&nbsp;a)&nbsp;-&gt;&nbsp;f&nbsp;(k,&nbsp;Set&nbsp;a) evaluateStop&nbsp;stopsAndDrivers&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;gossip&nbsp;(stop,&nbsp;driver)&nbsp;=&nbsp;Map.insertWith&nbsp;Set.union&nbsp;stop&nbsp;driver &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gossipAtStops&nbsp;=&nbsp;<span style="color:blue;">foldl</span>&#39;&nbsp;(<span style="color:blue;">flip</span>&nbsp;gossip)&nbsp;Map.empty&nbsp;stopsAndDrivers &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">fmap</span>&nbsp;(\(stop,&nbsp;_)&nbsp;-&gt;&nbsp;(stop,&nbsp;gossipAtStops&nbsp;!&nbsp;stop))&nbsp;stopsAndDrivers</pre> </p> <p> I was fairly confident that this function worked as I intended, but I wanted to be sure. I needed some examples, so I added these tests: </p> <p> <pre><span style="color:#a31515;">&quot;evaluateStop&nbsp;examples&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(stopsAndDrivers,&nbsp;expected)&nbsp;&lt;-&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([(1,&nbsp;fromList&nbsp;[1]),&nbsp;(2,&nbsp;fromList&nbsp;[2]),&nbsp;(1,&nbsp;fromList&nbsp;[1])], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(1,&nbsp;fromList&nbsp;[1]),&nbsp;(2,&nbsp;fromList&nbsp;[2]),&nbsp;(1,&nbsp;fromList&nbsp;[1])]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([(1,&nbsp;fromList&nbsp;[1]),&nbsp;(2,&nbsp;fromList&nbsp;[2]),&nbsp;(1,&nbsp;fromList&nbsp;[2])], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(1,&nbsp;fromList&nbsp;[1,&nbsp;2]),&nbsp;(2,&nbsp;fromList&nbsp;[2]),&nbsp;(1,&nbsp;fromList&nbsp;[1,&nbsp;2])]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([(1,&nbsp;fromList&nbsp;[1,&nbsp;2,&nbsp;3]),&nbsp;(1,&nbsp;fromList&nbsp;[2,&nbsp;3,&nbsp;4])], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(1,&nbsp;fromList&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4]),&nbsp;(1,&nbsp;fromList&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4])]) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;evaluateStop&nbsp;stopsAndDrivers &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;fromList&nbsp;expected&nbsp;~=?&nbsp;fromList&nbsp;actual</pre> </p> <p> They do, indeed, pass. </p> <p> The idea behind that <code>evaluateStop</code> function is to evaluate the state at each 'minute' of the simulation. The first line of each test case is the state before the drivers meet, and the second line is the <em>expected</em> state after all drivers have gossiped. </p> <p> My plan was to use some sort of left fold to keep evaluating states until all information has disseminated to all drivers. </p> <h3 id="06888628b5da4b4bb76fc59812c019da"> Property <a href="#06888628b5da4b4bb76fc59812c019da">#</a> </h3> <p> Since I have already extolled the virtues of property-based testing in this article series, I wondered whether I could add some properties instead of relying on examples. Well, I did manage to add one <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a> property: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;drive&nbsp;image&quot;</span>&nbsp;$&nbsp;\&nbsp;(routes&nbsp;::&nbsp;[NonEmptyList&nbsp;Int])&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;drive&nbsp;$&nbsp;<span style="color:blue;">fmap</span>&nbsp;getNonEmpty&nbsp;routes &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;isJust&nbsp;actual&nbsp;==&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">all</span>&nbsp;(\i&nbsp;-&gt;&nbsp;0&nbsp;&lt;=&nbsp;i&nbsp;&amp;&amp;&nbsp;i&nbsp;&lt;=&nbsp;480)&nbsp;actual</pre> </p> <p> There's not much to talk about here. The property only states that the result of the <code>drive</code> function must be between <code>0</code> and <code>480</code>, if it exists. </p> <p> Such a property could vacuously pass if <code>drive</code> always returns <code>Nothing</code>, so I used the <code>==&gt;</code> QuickCheck combinator to make sure that the property is actually exercising only the <code>Just</code> cases. </p> <p> Since the <code>drive</code> function only returns a number, apart from verifying its <a href="https://en.wikipedia.org/wiki/Image_(mathematics)">image</a> I couldn't think of any other general property to add. </p> <p> You can always come up with more specific properties that explicitly set up more constrained test scenarios, but is it worth it? </p> <p> It's always worthwhile to stop and think. If you're writing a 'normal' example-based test, consider whether a property would be better. Likewise, if you're about to write a property, consider whether an example would be better. </p> <p> 'Better' can mean more than one thing. Preventing regressions is one thing, but making the code maintainable is another. If you're writing a property that is too complicated, it might be better to write a simpler example-based test. </p> <p> I could definitely think of some complicated properties, but I found that more examples might make the test code easier to understand. </p> <h3 id="64eac896c8ea4127bacccb8ca01cf2fb"> More examples <a href="#64eac896c8ea4127bacccb8ca01cf2fb">#</a> </h3> <p> After all that angst and soul-searching, I added a few more examples to the first parametrised test: </p> <p> <pre><span style="color:#a31515;">&quot;Kata&nbsp;examples&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(routes,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[3,&nbsp;1,&nbsp;2,&nbsp;3], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3,&nbsp;2,&nbsp;3,&nbsp;1], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[4,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;5), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[2,&nbsp;1,&nbsp;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[5,&nbsp;2,&nbsp;8]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[5,&nbsp;6,&nbsp;7,&nbsp;8], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3,&nbsp;9,&nbsp;6]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;13), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[1,&nbsp;2,&nbsp;3], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,&nbsp;1,&nbsp;3], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,&nbsp;4,&nbsp;5,&nbsp;3]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;5), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[1,&nbsp;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,&nbsp;1]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[1]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;0), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([[2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;drive&nbsp;routes &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> The first two test cases are the same as before, and the last two are some edge cases I added myself. The middle three I adopted from <a href="https://dodona.ugent.be/en/activities/1792896126/">another page about the kata</a>. Since those examples turned out to be off by one, I did those examples on paper to verify that I understood what the expected value was. Then I adjusted them to my one-indexed results. </p> <h3 id="52417ab56528457891928ea551017f75"> Drive <a href="#52417ab56528457891928ea551017f75">#</a> </h3> <p> The <code>drive</code> function now correctly implements the kata, I hope. At least it passes all the tests. </p> <p> <pre><span style="color:#2b91af;">drive</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Num</span>&nbsp;b,&nbsp;<span style="color:blue;">Enum</span>&nbsp;b,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[[a]]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;b drive&nbsp;routes&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;Each&nbsp;driver&nbsp;starts&nbsp;with&nbsp;a&nbsp;single&nbsp;gossip.&nbsp;Any&nbsp;kind&nbsp;of&nbsp;value&nbsp;will&nbsp;do,&nbsp;as </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;long&nbsp;as&nbsp;each&nbsp;is&nbsp;unique.&nbsp;Here&nbsp;I&nbsp;use&nbsp;the&nbsp;one-based&nbsp;index&nbsp;of&nbsp;each&nbsp;route, </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;since&nbsp;it&nbsp;fulfills&nbsp;the&nbsp;requirements. </span>&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;drivers&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;Set.singleton&nbsp;[1&nbsp;..&nbsp;<span style="color:blue;">length</span>&nbsp;routes] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goal&nbsp;=&nbsp;Set.unions&nbsp;drivers &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stops&nbsp;=&nbsp;transpose&nbsp;$&nbsp;<span style="color:blue;">fmap</span>&nbsp;(<span style="color:blue;">take</span>&nbsp;480&nbsp;.&nbsp;<span style="color:blue;">cycle</span>)&nbsp;routes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propagation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">scanl</span>&nbsp;(\ds&nbsp;ss&nbsp;-&gt;&nbsp;<span style="color:blue;">snd</span>&nbsp;&lt;$&gt;&nbsp;evaluateStop&nbsp;(<span style="color:blue;">zip</span>&nbsp;ss&nbsp;ds))&nbsp;drivers&nbsp;stops &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">fmap</span>&nbsp;<span style="color:blue;">fst</span>&nbsp;$&nbsp;find&nbsp;(<span style="color:blue;">all</span>&nbsp;(==&nbsp;goal)&nbsp;.&nbsp;<span style="color:blue;">snd</span>)&nbsp;$&nbsp;<span style="color:blue;">zip</span>&nbsp;[0&nbsp;..]&nbsp;propagation</pre> </p> <p> Haskell code can be information-dense, and if you don't have an integrated development environment (IDE) around, this may be hard to read. </p> <p> <code>drivers</code> is a list of sets. Each set represents the gossip that a driver knows. At the beginning, each only knows one piece of gossip. The expression initialises each driver with a <code>singleton</code> set. Each piece of gossip is represented by a number, simply going from <code>1</code> to the number of routes. Incidentally, this is also the number of drivers, so you can consider the number <code>1</code> as a placeholder for the gossip that driver <em>1</em> knows, and so on. </p> <p> The <code>goal</code> is the union of all the gossip. Once every driver's knowledge is equal to the <code>goal</code> the simulation can stop. </p> <p> Since <code>evaluateStop</code> simulates one stop, the <code>drive</code> function needs a list of stops to fold. That's the <code>stops</code> value. In the very first example, you have three routes: <code>[3, 1, 2, 3]</code>, <code>[3, 2, 3, 1]</code>, and <code>[4, 2, 3, 4, 5]</code>. The first time the drivers stop (after one minute), the stops are <code>3</code>, <code>3</code>, and <code>4</code>. That is, the first element in <code>stops</code> would be the list <code>[3, 3, 4]</code>. The next one would be <code>[1, 2, 2]</code>, then <code>[2, 3, 3]</code>, and so on. </p> <p> My plan all along was to use some sort of left fold to repeatedly run <code>evaluateStop</code> over each minute. Since I need to produce a list of states, <code>scanl</code> was an appropriate choice. The lambda expression that I have to pass to it, though, is more complicated than I appreciate. We'll return to that in a moment. </p> <p> The <code>drive</code> function can now index the <code>propagation</code> list by zipping it with the infinite list <code>[0 ..]</code>, <code>find</code> the first element where <code>all</code> sets are equal to the <code>goal</code> set, and then return that index. That produces the correct results. </p> <h3 id="2b84136e78154ebdb81654efabf3d987"> The need for a better helper function <a href="#2b84136e78154ebdb81654efabf3d987">#</a> </h3> <p> As I already warned, I wasn't happy with the lambda expression passed to <code>scanl</code>. It looks complicated and arcane. Is there a better way to express the same behaviour? Usually, when confronted with a nasty lambda expression like that, in Haskell my first instinct is to see if <a href="https://pointfree.io/">pointfree.io</a> has a better option. Alas, <code>(((<span style="color:blue;">snd</span>&nbsp;&lt;$&gt;)&nbsp;.&nbsp;evaluateStop)&nbsp;.)&nbsp;.&nbsp;<span style="color:blue;">flip</span>&nbsp;<span style="color:blue;">zip</span></code> hardly seems an improvement. That <code>flip zip</code> expression to the right, however, suggests that it might help flipping the arguments to <code>evaluateStop</code>. </p> <p> When I developed the <code>evaluateStop</code> helper function, I found it intuitive to define it over a list of tuples, where the first element in the tuple is the stop, and the second element is the set of gossip that the driver at that stop knows. </p> <p> The tuples don't <em>have</em> to be in that order, though. Perhaps if I flip the tuples that would make the lambda expression more readable. It was worth a try. </p> <h3 id="ca808b5a5fa3473c9046704e4bcc7357"> Confidence <a href="#ca808b5a5fa3473c9046704e4bcc7357">#</a> </h3> <p> Since this article is part of a small series about the epistemology of testing composed functions, let's take a moment to reflect on the confidence we may have in the <code>drive</code> function. </p> <p> Keep in mind the goal of the kata: Calculate the number of minutes it takes for all gossip to spread to all drivers. There's a few tests that verify that; seven examples and a fairly vacuous QuickCheck property. Is that enough to be confident that the function is correct? </p> <p> If it isn't, I think the best option you have is to add more examples. For the sake of argument, however, let's assume that the tests are good enough. </p> <p> When summarising the tests that cover the <code>drive</code> function, I didn't count the three examples that exercise <code>evaluateStop</code>. Do these three test cases improve your confidence in the <code>drive</code> function? A bit, perhaps, but keep in mind that <em>the kata description doesn't mandate that function.</em> It's just a helper function I created in order to decompose the problem. </p> <p> Granted, having tests that cover a helper function does, to a degree, increase my confidence in the code. I have confidence in the function itself, but that is largely irrelevant, because the problem I'm trying to solve is <em>not</em> implementing this particular function. On the other hand, my confidence in <code>evaluateStop</code> means that I have increased confidence in the code that calls it. </p> <p> Compared to interaction-based testing, I'm not <em>testing</em> that <code>drive</code> calls <code>evaluateStop</code>, but I can still verify that this happens. I can just look at the code. </p> <p> The composition is already there in the code. What do I gain from replicating that composition with <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a> and <a href="http://xunitpatterns.com/Test%20Spy.html">Spies</a>? </p> <p> It's not a breaking change if I decide to implement <code>drive</code> in a different way. </p> <p> What gives me confidence when composing pure functions isn't that I've subjected the composition to an interaction-based test. Rather, it's that the function is composed from trustworthy components. </p> <h3 id="a414a1e6b9e947d2b4632fcea7b916cf"> Strangler <a href="#a414a1e6b9e947d2b4632fcea7b916cf">#</a> </h3> <p> My main grievance with Stubs and Spies is that <a href="/2022/10/17/stubs-and-mocks-break-encapsulation">they break encapsulation</a>. This may sound abstract, but is a real problem. This is the underlying reason that so many tests break when you refactor code. </p> <p> This example code base, as other functional code that I write, avoids interaction-based testing. This makes it easier to refactor the code, as I will now demonstrate. </p> <p> My goal is to change the <code>evaluateStop</code> helper function by flipping the tuples. If I just edit it, however, I'm going to (temporarily) break the <code>drive</code> function. </p> <p> Katas typically result in small code bases where you can get away with a lot of bad practices that wouldn't work in a larger code base. To be honest, the refactoring I have in mind can be completed in a few minutes with a brute-force approach. Imagine, however, that we can't break compatibility of the <code>evaluateStop</code> function for the time being. Perhaps, had we had a larger code base, there were other code that depended on this function. At the very least, the tests do. </p> <p> Instead of brute-force changing the function, I'm going to make use of the <a href="https://martinfowler.com/bliki/StranglerFigApplication.html">Strangler</a> pattern, as I've also described in my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> Leave the existing function alone, and add a new one. You can typically copy and paste the existing code and then make the necessary changes. In that way, you break neither client code nor tests, because there are none. </p> <p> <pre><span style="color:#2b91af;">evaluateStop&#39;</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Functor</span>&nbsp;f,&nbsp;<span style="color:blue;">Foldable</span>&nbsp;f,&nbsp;<span style="color:blue;">Ord</span>&nbsp;k,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;f&nbsp;(Set&nbsp;a,&nbsp;k)&nbsp;-&gt;&nbsp;f&nbsp;(Set&nbsp;a,&nbsp;k) evaluateStop&#39;&nbsp;driversAndStops&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;gossip&nbsp;(driver,&nbsp;stop)&nbsp;=&nbsp;Map.insertWith&nbsp;Set.union&nbsp;stop&nbsp;driver &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gossipAtStops&nbsp;=&nbsp;<span style="color:blue;">foldl</span>&#39;&nbsp;(<span style="color:blue;">flip</span>&nbsp;gossip)&nbsp;Map.empty&nbsp;driversAndStops &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">fmap</span>&nbsp;(\(_,&nbsp;stop)&nbsp;-&gt;&nbsp;(gossipAtStops&nbsp;!&nbsp;stop,&nbsp;stop))&nbsp;driversAndStops</pre> </p> <p> In a language like C# you can often get away with overloading a method name, but Haskell doesn't have overloading. Since I consider this side-by-side situation to be temporary, I've appended a prime after the function name. This is a fairly normal convention in Haskell, I gather. </p> <p> The only change this function represents is that I've swapped the tuple order. </p> <p> Once you've added the new function, you may want to copy, paste and edit the tests. Or perhaps you want to do the tests first. During this process, make <a href="https://www.industriallogic.com/blog/whats-this-about-micro-commits/">micro-commits</a> so that you can easily suspend your 'refactoring' activity if something more important comes up. </p> <p> Once everything is in place, you can change the <code>drive</code> function: </p> <p> <pre><span style="color:#2b91af;">drive</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Num</span>&nbsp;b,&nbsp;<span style="color:blue;">Enum</span>&nbsp;b,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[[a]]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;b drive&nbsp;routes&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;Each&nbsp;driver&nbsp;starts&nbsp;with&nbsp;a&nbsp;single&nbsp;gossip.&nbsp;Any&nbsp;kind&nbsp;of&nbsp;value&nbsp;will&nbsp;do,&nbsp;as </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;long&nbsp;as&nbsp;each&nbsp;is&nbsp;unique.&nbsp;Here&nbsp;I&nbsp;use&nbsp;the&nbsp;one-based&nbsp;index&nbsp;of&nbsp;each&nbsp;route, </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;since&nbsp;it&nbsp;fulfills&nbsp;the&nbsp;requirements. </span>&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;drivers&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;Set.singleton&nbsp;[1&nbsp;..&nbsp;<span style="color:blue;">length</span>&nbsp;routes] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goal&nbsp;=&nbsp;Set.unions&nbsp;drivers &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stops&nbsp;=&nbsp;transpose&nbsp;$&nbsp;<span style="color:blue;">fmap</span>&nbsp;(<span style="color:blue;">take</span>&nbsp;480&nbsp;.&nbsp;<span style="color:blue;">cycle</span>)&nbsp;routes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propagation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">scanl</span>&nbsp;(\ds&nbsp;ss&nbsp;-&gt;&nbsp;<span style="color:blue;">fst</span>&nbsp;&lt;$&gt;&nbsp;evaluateStop&#39;&nbsp;(<span style="color:blue;">zip</span>&nbsp;ds&nbsp;ss))&nbsp;drivers&nbsp;stops &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">fmap</span>&nbsp;<span style="color:blue;">fst</span>&nbsp;$&nbsp;find&nbsp;(<span style="color:blue;">all</span>&nbsp;(==&nbsp;goal)&nbsp;.&nbsp;<span style="color:blue;">snd</span>)&nbsp;$&nbsp;<span style="color:blue;">zip</span>&nbsp;[0&nbsp;..]&nbsp;propagation</pre> </p> <p> Notice that the type of <code>drive</code> hasn't change, and neither has the behaviour. This means that although I've changed the composition (the <em>interaction</em>) no tests broke. </p> <p> Finally, once I moved all code over, I deleted the old function and renamed the new one to take its place. </p> <h3 id="6be4ec66c14c4adbbb8bcb60307c7ba3"> Was it all worth it? <a href="#6be4ec66c14c4adbbb8bcb60307c7ba3">#</a> </h3> <p> At first glance, it doesn't look as though much was gained. What happens if I eta-reduce the new lambda expression? </p> <p> <pre><span style="color:#2b91af;">drive</span>&nbsp;<span style="color:blue;">::</span>&nbsp;(<span style="color:blue;">Num</span>&nbsp;b,&nbsp;<span style="color:blue;">Enum</span>&nbsp;b,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[[a]]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;b drive&nbsp;routes&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;Each&nbsp;driver&nbsp;starts&nbsp;with&nbsp;a&nbsp;single&nbsp;gossip.&nbsp;Any&nbsp;kind&nbsp;of&nbsp;value&nbsp;will&nbsp;do,&nbsp;as </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;long&nbsp;as&nbsp;each&nbsp;is&nbsp;unique.&nbsp;Here&nbsp;I&nbsp;use&nbsp;the&nbsp;one-based&nbsp;index&nbsp;of&nbsp;each&nbsp;route, </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;since&nbsp;it&nbsp;fulfills&nbsp;the&nbsp;requirements. </span>&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;drivers&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;Set.singleton&nbsp;[1&nbsp;..&nbsp;<span style="color:blue;">length</span>&nbsp;routes] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goal&nbsp;=&nbsp;Set.unions&nbsp;drivers &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stops&nbsp;=&nbsp;transpose&nbsp;$&nbsp;<span style="color:blue;">fmap</span>&nbsp;(<span style="color:blue;">take</span>&nbsp;480&nbsp;.&nbsp;<span style="color:blue;">cycle</span>)&nbsp;routes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propagation&nbsp;=&nbsp;<span style="color:blue;">scanl</span>&nbsp;(((<span style="color:blue;">fmap</span>&nbsp;<span style="color:blue;">fst</span>&nbsp;.&nbsp;evaluateStop)&nbsp;.)&nbsp;.&nbsp;<span style="color:blue;">zip</span>)&nbsp;drivers&nbsp;stops &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">fmap</span>&nbsp;<span style="color:blue;">fst</span>&nbsp;$&nbsp;find&nbsp;(<span style="color:blue;">all</span>&nbsp;(==&nbsp;goal)&nbsp;.&nbsp;<span style="color:blue;">snd</span>)&nbsp;$&nbsp;<span style="color:blue;">zip</span>&nbsp;[0&nbsp;..]&nbsp;propagation</pre> </p> <p> Not much better. I can now fit the <code>propagation</code> expression on a single line of code and still stay within a <a href="/2019/11/04/the-80-24-rule">80x24 box</a>, but that's about it. Is <code>((<span style="color:blue;">fmap</span>&nbsp;<span style="color:blue;">fst</span>&nbsp;.&nbsp;evaluateStop)&nbsp;.)&nbsp;.&nbsp;<span style="color:blue;">zip</span></code> more readable than what we had before? </p> <p> Hardly, I admit. I might consider reverting, and since I've been <a href="https://stackoverflow.blog/2022/12/19/use-git-tactically/">using Git tactically</a>, I have that option. </p> <p> If I hadn't tried, though, I wouldn't have known. </p> <h3 id="5bf92cabf46e4ec9a6a41b51dd29e876"> Conclusion <a href="#5bf92cabf46e4ec9a6a41b51dd29e876">#</a> </h3> <p> When composing one pure function with another, how can you test that the outer function correctly calls the inner function? </p> <p> By the same way that you test any other pure function. The only way you can observe whether a pure function works as intended is to compare its actual output to the output you expect its input to produce. How it arrives at that output is irrelevant. It could be looking up all results in a big table. As long as the result is correct, the function is correct. </p> <p> In this article, you saw an example of how to test a composed function, as well as how to refactor it without breaking tests. </p> <p> <strong>Next:</strong> <a href="/2023/06/19/when-is-an-implementation-detail-an-implementation-detail">When is an implementation detail an implementation detail?</a> </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Are pull requests bad because they originate from open-source development? https://blog.ploeh.dk/2023/04/24/are-pull-requests-bad-because-they-originate-from-open-source-development 2023-04-24T06:08:00+00:00 Mark Seemann <div id="post"> <p> <em>I don't think so, and at least find the argument flawed.</em> </p> <p> Increasingly I come across a quote that goes like this: </p> <blockquote> <p> Pull requests were invented for open source projects where you want to gatekeep changes from people you don't know and don't trust to change the code safely. </p> </blockquote> <p> If you're wondering where that 'quote' comes from, then read on. I'm not trying to stand up a straw man, but I had to do a bit of digging in order to find the source of what almost seems like a <a href="https://en.wikipedia.org/wiki/Meme">meme</a>. </p> <h3 id="c347774c419941a9987c74c95b6f91cd"> Quote investigation <a href="#c347774c419941a9987c74c95b6f91cd">#</a> </h3> <p> The quote is usually attributed to <a href="https://www.davefarley.net/">Dave Farley</a>, who is a software luminary that <a href="https://www.goodreads.com/review/show/4812673890">I respect tremendously</a>. Even with the attribution, the source is typically missing, but after asking around, <a href="https://twitter.com/MitjaBezensek/status/1626165418296590336">Mitja Bezenšek pointed me in the right direction</a>. </p> <p> The source is most likely a video, from which I've transcribed a longer passage: </p> <blockquote> <p> "Pull requests were invented to gatekeep access to open-source projects. In open source, it's very common that not everyone is given free access to changing the code, so contributors will issue a pull request so that a trusted person can then approve the change. </p> <p> "I think this is really bad way to organise a development team. </p> <p> "If you can't trust your team mates to make changes carefully, then your version control system is not going to fix that for you." </p> <footer><cite><a href="https://youtu.be/UQrlEXU6RM8">Dave Farley</a></cite></footer> </blockquote> <p> I've made an effort to transcribe as faithfully as possible, but if you really want to be sure what Dave Farley said, watch the video. The quote comes twelve minutes in. </p> <h3 id="60fb5776c68b464d9ae77ad601e8c99b"> My biases <a href="#60fb5776c68b464d9ae77ad601e8c99b">#</a> </h3> <p> I agree that the argument sounds compelling, but I find it flawed. Before I proceed to put forward my arguments I want to make my own biases clear. Arguing against someone like Dave Farley is not something I take lightly. As far as I can tell, he's worked on systems more impressive than any I can showcase. I also think he has more industry experience than I have. </p> <p> That doesn't necessarily make him right, but on the other hand, why should you side with me, with my less impressive résumé? </p> <p> My objective is not to attack Dave Farley, or any other person for that matter. My agenda is the argument itself. I do, however, find it intellectually honest to cite sources, with the associated risk that my argument may look like a personal attack. To steelman my opponent, then, I'll try to put my own biases on display. To the degree I'm aware of them. </p> <p> I prefer pull requests over pair and ensemble programming. I've tried all three, and I do admit that real-time collaboration has obvious advantages, but I find pairing or ensemble programming exhausting. </p> <p> Since <a href="https://www.goodreads.com/review/show/440837121">I read <em>Quiet</em></a> a decade ago, I've been alert to the introspective side of my personality. Although I agree with <a href="http://www.exampler.com/about/">Brian Marick</a> that one should <a href="https://podcast.oddly-influenced.dev/episodes/not-a-ted-talk-relevant-results-from-psychology">be wary of understanding personality traits as destiny</a>, I mostly prefer solo activities. </p> <p> Increasingly, since I became self-employed, I've arranged my life to maximise the time I can work from home. The exercise regimen I've chosen for myself is independent of other people: I run, and lift weights at home. You may have noticed that I like writing. I like reading as well. And, hardly surprising, I prefer writing code in splendid isolation. </p> <p> Even so, I find it perfectly possible to have meaningful relationships with other people. After all, I've been married to the same woman for decades, my (mostly) grown kids haven't fled from home, and I have friends that I've known for decades. </p> <p> In a toot that I can no longer find, Brian Marick asked (and I paraphrase from memory): <em>If you've tried a technique and didn't like it, what would it take to make you like it?</em> </p> <p> As a self-professed introvert, social interaction <em>does</em> tire me, but I still enjoy hanging out with friends or family. What makes those interactions different? Well, often, there's good food and wine involved. Perhaps ensemble programming would work better for me with a bottle of Champagne. </p> <p> Other forces influence my preferences as well. I like the <a href="/2023/02/20/a-thought-on-workplace-flexibility-and-asynchrony">flexibility provided by asynchrony</a>, and similarly dislike having to be somewhere at a specific time. </p> <p> Having to be somewhere also involves transporting myself there, which I also don't appreciate. </p> <p> In short, I prefer pull requests over pairing and ensemble programming. All of that, however, is just my subjective opinion, and <a href="/2020/10/12/subjectivity">that's not an argument</a>. </p> <h3 id="c83cc60f53e049edbdae29dca4402563"> Counter-examples <a href="#c83cc60f53e049edbdae29dca4402563">#</a> </h3> <p> The above tirade about my biases is <em>not</em> a refutation of Dave Farley's argument. Rather, I wanted to put my own blind spots on display. If you suspect me of <a href="https://en.wikipedia.org/wiki/Motivated_reasoning">motivated reasoning</a>, that just might be the case. </p> <p> All that said, I want to challenge the argument. </p> <p> First, it includes an appeal to <em>trust</em>, which is <a href="/2023/03/20/on-trust-in-software-development">a line of reasoning with which I don't agree</a>. You can't trust your colleagues, just like you can't trust yourself. A code review serves more purposes than keeping malicious actors out of the code base. It also helps catch mistakes, security issues, or misunderstandings. It can also improve shared understanding of common goals and standards. Yes, this is <em>also</em> possible with other means, such as pair or ensemble programming, but from that, it doesn't follow that code reviews <em>can't</em> do that. They can. I've lived that dream. </p> <p> If you take away the appeal to trust, though, there isn't much left of the argument. What remains is essentially: <em>Pull requests were invented to solve a particular problem in open-source development. Internal software development is not open source. Pull requests are bad for internal software development.</em> </p> <p> That an invention was done in one context, however, doesn't preclude it from being useful in another. Git was invented to address an open-source problem. Should we stop using Git for internal software development? </p> <p> <a href="https://en.wikipedia.org/wiki/Solar_cell">Solar panels were originally developed for satellites and space probes</a>. Does that mean that we shouldn't use them on Earth? </p> <p> <a href="https://en.wikipedia.org/wiki/Global_Positioning_System">GPS was invented for use by the US military</a>. Does that make civilian use wrong? </p> <h3 id="a30f67d73f0e488aac23dccb723370f8"> Are pull requests bad? <a href="#a30f67d73f0e488aac23dccb723370f8">#</a> </h3> <p> I find the original <em>argument</em> logically flawed, but if I insist on logic, I'm also obliged to admit that my <a href="/ref/predicate-logic">possible-world counter-examples</a> don't prove that pull requests are good. </p> <p> Dave Farley's claim may still turn out to be true. Not because of the argument he gives, but perhaps for other reasons. </p> <p> I think I understand where the dislike of pull requests come from. As they are often practised, pull requests can sit for days with no-one looking at them. This creates unnecessary delays. If this is the only way you know of working with pull requests, no wonder you don't like them. </p> <p> <a href="/2021/06/21/agile-pull-requests">I advocate a more agile workflow for pull requests</a>. I consider that congruent with <a href="/2023/01/23/agilean">my view on agile development</a>. </p> <h3 id="e0d3bb23dda34ae98241ff2bad442794"> Conclusion <a href="#e0d3bb23dda34ae98241ff2bad442794">#</a> </h3> <p> Pull requests are often misused, but they don't have to be. On the other hand, that's just my experience and subjective preference. </p> <p> Dave Farley has argued that pull requests are a bad way to organise a development team. I've argued that the argument is logically flawed. </p> <p> The question remains unsettled. I've attempted to refute one particular argument, and even if you accept my counter-examples, pull requests may still be bad. Or good. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="bdd051fb26464bdbbc056ddea07712d5"> <div class="comment-author"><a href="https://cwb.dk/">Casper Weiss Bang</a> <a href="#bdd051fb26464bdbbc056ddea07712d5">#</a></div> <div class="comment-content"> <p> Another important angle, for me, is that pull requests are not merely code review. It can also be a way of enforcing a variety of automated checks, i.e. running tests or linting etc. This enforces quality too - so I'd argue to use pull requests even if you don't do peer review (I do on my hobby projects atleast, for the exact reasons you mentioned in <a href="https://blog.ploeh.dk/2023/03/20/on-trust-in-software-development/">On trust in software development</a> - I don't trust myself to be perfect.) </p> </div> <div class="comment-date">2023-04-26 10:26 UTC</div> </div> <div class="comment" id="9b022dd663d34feba170006de5b66af4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9b022dd663d34feba170006de5b66af4">#</a></div> <div class="comment-content"> <p> Casper, thank you for writing. Indeed, other readers have made similar observations on other channels (Twitter, Mastodon). That, too, can be a benefit. </p> <p> In order to once more steel-man 'the other side', they'd probably say that you can run automated checks in your Continuous Delivery pipeline, and halt it if automated checks fail. </p> <p> When done this way, it's useful to be able to also run the same tests on your dev box. I consider that a good practice anyway. </p> </div> <div class="comment-date">2023-04-28 14:49 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A restaurant example of refactoring from example-based to property-based testing https://blog.ploeh.dk/2023/04/17/a-restaurant-example-of-refactoring-from-example-based-to-property-based-testing 2023-04-17T06:37:00+00:00 Mark Seemann <div id="post"> <p> <em>A C# example with xUnit.net and FsCheck.</em> </p> <p> This is the second comprehensive example that accompanies the article <a href="/2023/02/13/epistemology-of-interaction-testing">Epistemology of interaction testing</a>. In that article, I argue that in a code base that leans toward functional programming (FP), property-based testing is a better fit than interaction-based testing. In this example, I will show how to refactor realistic <a href="/2019/02/18/from-interaction-based-to-state-based-testing">state-based tests</a> into (state-based) property-based tests. </p> <p> The <a href="/2023/04/03/an-abstract-example-of-refactoring-from-interaction-based-to-property-based-testing">previous article</a> showed a <a href="https://en.wikipedia.org/wiki/Minimal_reproducible_example">minimal and self-contained example</a> that had the advantage of being simple, but the disadvantage of being perhaps too abstract and unrelatable. In this article, then, I will attempt to show a more realistic and concrete example. It actually doesn't start with interaction-based testing, since it's already written in the style of <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">Functional Core, Imperative Shell</a>. On the other hand, it shows how to refactor from concrete example-based tests to property-based tests. </p> <p> I'll use the online restaurant reservation code base that accompanies my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <h3 id="e7aaa6310292411ab830de17f5906777"> Smoke test <a href="#e7aaa6310292411ab830de17f5906777">#</a> </h3> <p> I'll start with a simple test which was, if I remember correctly, the second test I wrote for this code base. It was a smoke test that I wrote to drive a <a href="https://wiki.c2.com/?WalkingSkeleton">walking skeleton</a>. It verifies that if you post a valid reservation request to the system, you receive an HTTP response in the <code>200</code> range. </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservation</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">api</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;LegacyApi(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDto &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;DateTime.Today.AddDays(778).At(19,&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToIso8601DateTimeString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;katinka@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Katinka&nbsp;Ingabogovinanana&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.PostReservation(expected); &nbsp;&nbsp;&nbsp;&nbsp;response.EnsureSuccessStatusCode(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;response.ParseJsonContent&lt;ReservationDto&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual,&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDtoComparer()); }</pre> </p> <p> Over the lifetime of the code base, I embellished and edited the test to reflect the evolution of the system as well as my understanding of it. Thus, when I wrote it, it may not have looked exactly like this. Even so, I kept it around even though other, more detailed tests eventually superseded it. </p> <p> One characteristic of this test is that it's quite concrete. When I originally wrote it, I hard-coded the date and time as well. Later, however, <a href="/2021/01/11/waiting-to-happen">I discovered that I had to make the time relative to the system clock</a>. Thus, as you can see, the <code>At</code> property isn't a literal value, but all other properties (<code>Email</code>, <code>Name</code>, and <code>Quantity</code>) are. </p> <p> This test is far from abstract or data-driven. Is it possible to turn such a test into a property-based test? Yes, I'll show you how. </p> <p> A word of warning before we proceed: Tests with concrete, literal, easy-to-understand examples are valuable as programmer documentation. A person new to the code base can peruse such tests and learn about the system. Thus, this test is <em>already quite valuable as it is</em>. In a real, living code base, I'd prefer leaving it as it is, instead of turning it into a property-based test. </p> <p> Since it's a simple and concrete test, on the other hand, it's easy to understand, and thus also a a good place to start. Thus, I'm going to refactor it into a property-based test; not because I think that you should (I don't), but because I think it'll be easy for you, the reader, to follow along. In other words, it's a good introduction to the process of turning a concrete test into a property-based test. </p> <h3 id="9dabcfae9e284a0ab9748cf817f4b2f9"> Adding parameters <a href="#9dabcfae9e284a0ab9748cf817f4b2f9">#</a> </h3> <p> This code base already uses <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> so it makes sense to stick to that framework for property-based testing. While it's written in <a href="https://fsharp.org/">F#</a> you can use it from C# as well. The easiest way to use it is as a parametrised test. This is possible with the <a href="https://www.nuget.org/packages/FsCheck.Xunit">FsCheck.Xunit</a> glue library. In fact, as I refactor the <code>PostValidReservation</code> test, it'll look much like the <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>-driven tests from <a href="/2023/04/03/an-abstract-example-of-refactoring-from-interaction-based-to-property-based-testing">the previous article</a>. </p> <p> When turning concrete examples into properties, it helps to consider whether literal values are representative of an equivalence class. In other words, is that particular value important, or is there a wider set of values that would be just as good? For example, why is the test making a reservation 778 days in the future? Why not 777 or 779? Is the value <em>778</em> important? Not really. What's important is that the reservation is in the future. How far in the future actually isn't important. Thus, we can replace the literal value <code>778</code> with a parameter: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservation</span>(PositiveInt&nbsp;<span style="font-weight:bold;color:#1f377f;">days</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">api</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;LegacyApi(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDto &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;DateTime.Today.AddDays((<span style="color:blue;">int</span>)days).At(19,&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToIso8601DateTimeString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;The&nbsp;rest&nbsp;of&nbsp;the&nbsp;test...</span></pre> </p> <p> Notice that I've replaced the literal value <code>778</code> with the method parameter <code>days</code>. The <code>PositiveInt</code> type is a type from FsCheck. It's a wrapper around <code>int</code> that guarantees that the value is positive. This is important because we don't want to make a reservation in the past. The <code>PositiveInt</code> type is a good choice because it's a type that's already available with FsCheck, and the framework knows how to generate valid values. Since it's a wrapper, though, the test needs to unwrap the value before using it. This is done with the <code>(int)days</code> cast. </p> <p> Notice, also, that I've replaced the <code>[Fact]</code> attribute with the <code>[Property]</code> attribute that comes with FsCheck.Xunit. This is what enables FsCheck to automatically generate test cases and feed them to the test method. You can't always do this, as you'll see later, but when you can, it's a nice and succinct way to express a property-based test. </p> <p> Already, the <code>PostValidReservation</code> test method is 100 test cases (the FsCheck default), rather than one. </p> <p> What about <code>Email</code> and <code>Name</code>? Is it important for the test that these values are exactly <em>katinka@example.com</em> and <em>Katinka Ingabogovinanana</em> or might other values do? The answer is that it's not important. What's important is that the values are valid, and essentially any non-null string is. Thus, we can replace the literal values with parameters: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;PositiveInt&nbsp;<span style="font-weight:bold;color:#1f377f;">days</span>, &nbsp;&nbsp;&nbsp;&nbsp;StringNoNulls&nbsp;<span style="font-weight:bold;color:#1f377f;">email</span>, &nbsp;&nbsp;&nbsp;&nbsp;StringNoNulls&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">api</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;LegacyApi(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDto &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;DateTime.Today.AddDays((<span style="color:blue;">int</span>)days).At(19,&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToIso8601DateTimeString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email.Item, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name.Item, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.PostReservation(expected); &nbsp;&nbsp;&nbsp;&nbsp;response.EnsureSuccessStatusCode(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;response.ParseJsonContent&lt;ReservationDto&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual,&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDtoComparer()); }</pre> </p> <p> The <code>StringNoNulls</code> type is another FsCheck wrapper, this time around <code>string</code>. It ensures that FsCheck will generate no null strings. This time, however, a cast isn't possible, so instead I had to pull the wrapped string out of the value with the <code>Item</code> property. </p> <p> That's enough conversion to illustrate the process. </p> <p> What about the literal values <em>19</em>, <em>0</em>, or <em>2?</em> Shouldn't we parametrise those as well? While we could, that takes a bit more effort. The problem is that with these values, any old positive integer isn't going to work. For example, the number <em>19</em> is the hour component of the reservation time; that is, the reservation is for 19:00. Clearly, we can't just let FsCheck generate any positive integer, because most integers aren't going to work. For example, <em>5</em> doesn't work because it's in the early morning, and the restaurant isn't open at that time. </p> <p> Like other property-based testing frameworks FsCheck has an API that enables you to constrain value generation, but it doesn't work with the type-based approach I've used so far. Unlike <code>PositiveInt</code> there's no <code>TimeBetween16And21</code> wrapper type. </p> <p> You'll see what you can do to control how FsCheck generates values, but I'll use another test for that. </p> <h3 id="80844424b40a48f4931c78d91d865323"> Parametrised unit test <a href="#80844424b40a48f4931c78d91d865323">#</a> </h3> <p> The <code>PostValidReservation</code> test is a high-level smoke test that gives you an idea about how the system works. It doesn't, however, reveal much about the possible variations in input. To drive such behaviour, I wrote and evolved the following state-based test: </p> <p> <pre>[Theory] [InlineData(1049,&nbsp;19,&nbsp;00,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [InlineData(1130,&nbsp;18,&nbsp;15,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] [InlineData(&nbsp;956,&nbsp;16,&nbsp;55,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] [InlineData(&nbsp;433,&nbsp;17,&nbsp;30,&nbsp;<span style="color:#a31515;">&quot;shli@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Shanghai&nbsp;Li&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservationWhenDatabaseIsEmpty</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">days</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">hours</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">minutes</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">email</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">at</span>&nbsp;=&nbsp;DateTime.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Guid(<span style="color:#a31515;">&quot;B50DF5B1-F484-4D99-88F9-1915087AF568&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Contains(expected,&nbsp;db.Grandfather); }</pre> </p> <p> This test gives more details, without exercising all possible code paths of the system. It's still a <a href="/2012/06/27/FacadeTest">Facade Test</a> that covers 'just enough' of the integration with underlying components to provide confidence that things work as they should. All the business logic is implemented by a class called <code>MaitreD</code>, which is covered by its own set of targeted unit tests. </p> <p> While parametrised, this is still only four test cases, so perhaps you don't have sufficient confidence that everything works as it should. Perhaps, as I've outlined in <a href="/2023/02/13/epistemology-of-interaction-testing">the introductory article</a>, it would help if we converted it to an FsCheck property. </p> <h3 id="707d58026e914b708e6394b5d1d2abad"> Parametrised property <a href="#707d58026e914b708e6394b5d1d2abad">#</a> </h3> <p> I find it safest to refactor this parametrised test to a property in a series of small steps. This implies that I need to keep the <code>[InlineData]</code> attributes around for a while longer, removing one or two literal values at a time, turning them into randomly generated values. </p> <p> From the previous test we know that the <code>Email</code> and <code>Name</code> values are almost unconstrained. This means that they are trivial in themselves to have FsCheck generate. That change, in itself, is easy, which is good, because combining an <code>[InlineData]</code>-driven <code>[Theory]</code> with an FsCheck property is enough of a mouthful for one refactoring step: </p> <p> <pre>[Theory] [InlineData(1049,&nbsp;19,&nbsp;00,&nbsp;5)] [InlineData(1130,&nbsp;18,&nbsp;15,&nbsp;9)] [InlineData(&nbsp;956,&nbsp;16,&nbsp;55,&nbsp;2)] [InlineData(&nbsp;433,&nbsp;17,&nbsp;30,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservationWhenDatabaseIsEmpty</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">days</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">hours</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">minutes</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Reservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;r).ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">at</span>&nbsp;=&nbsp;DateTime.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithDate(at); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Contains(expected,&nbsp;db.Grandfather); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}).QuickCheckThrowOnFailure(); }</pre> </p> <p> I've now managed to get rid of the <code>email</code> and <code>name</code> parameters, so I've also removed those values from the <code>[InlineData]</code> attributes. Instead, I've asked FsCheck to generate a valid reservation <code>r</code>, which comes with both valid <code>Email</code> and <code>Name</code>. </p> <p> It turned out that this code base already had some custom generators in a static class called <code>Gens</code>, so I reused those: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Gen&lt;Email&gt;&nbsp;Email&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;NonWhiteSpaceString&gt;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Email(s.Item); <span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Gen&lt;Name&gt;&nbsp;Name&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;StringNoNulls&gt;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Name(s.Item); <span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Gen&lt;Reservation&gt;&nbsp;Reservation&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;id&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;Guid&gt;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;d&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;DateTime&gt;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;e&nbsp;<span style="color:blue;">in</span>&nbsp;Email &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;n&nbsp;<span style="color:blue;">in</span>&nbsp;Name &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;q&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;PositiveInt&gt;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation(id,&nbsp;d,&nbsp;e,&nbsp;n,&nbsp;q.Item);</pre> </p> <p> As was also the case with <a href="https://github.com/AnthonyLloyd/CsCheck">CsCheck</a> you typically use <a href="/2022/03/28/monads">syntactic sugar for monads</a> (which in C# is query syntax) to compose complex <a href="/2023/02/27/test-data-generator-monad">test data generators</a> from simpler generators. This enables me to generate an entire <code>Reservation</code> object with a single expression. </p> <h3 id="05d4d9e8c07b4162bd1b65347200456f"> Time of day <a href="#05d4d9e8c07b4162bd1b65347200456f">#</a> </h3> <p> Some of the values (such as the reservation's name and email address) that are involved in the <code>PostValidReservationWhenDatabaseIsEmpty</code> test don't really matter. Other values are constrained in some way. Even for the reservation <code>r</code> the above version of the test has to override the arbitrarily generated <code>r</code> value with a specific <code>quantity</code> and a specific <code>at</code> value. This is because you can't just reserve any quantity at any time of day. The restaurant has opening hours and actual tables. Most likely, it doesn't have a table for 100 people at 3 in the morning. </p> <p> This particular test actually exercises a particular restaurant called <code>Grandfather.Restaurant</code> (because it was the original restaurant that was <a href="https://en.wikipedia.org/wiki/Grandfather_clause">grandfathered in</a> when the system was expanded to a multi-tenant system). It opens at 16 and has the last seating at 21. This means that the <code>at</code> value has to be between 16 and 21. What's the best way to generate a <code>DateTime</code> value that satisfies this constraint? </p> <p> You could, naively, ask FsCheck to generate an integer between these two values. You'll see how to do that when we get to the <code>quantity</code>. While that would work for the <code>at</code> value, it would only generate the whole hours <em>16:00</em>, <em>17:00</em>, <em>18:00</em>, etcetera. It would be nice if the test could also exercise times such as <em>18:30</em>, <em>20:45</em>, and so on. On the other hand, perhaps we don't want weird reservation times such as <em>17:09:23.282</em>. How do we tell FsCheck to generate a <code>DateTime</code> value like that? </p> <p> It's definitely possible to do from scratch, but I chose to do something else. The following shows how test code and production code can co-exist in a symbiotic relationship. The main business logic component that deals with reservations in the system is a class called <code>MaitreD</code>. One of its methods is used to generate a list of time slots for every day. A user interface can use that list to populate a drop-down list of available times. The method is called <code>Segment</code> and can also be used as a data source for an FsCheck test data generator: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Gen&lt;TimeSpan&gt;&nbsp;<span style="color:#74531f;">ReservationTime</span>( &nbsp;&nbsp;&nbsp;&nbsp;Restaurant&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">slots</span>&nbsp;=&nbsp;restaurant.MaitreD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Segment(date,&nbsp;Enumerable.Empty&lt;Reservation&gt;()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;ts.At.TimeOfDay); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Gen.Elements(slots); }</pre> </p> <p> The <code>Gen.Elements</code> function is an FsCheck combinator that randomly picks a value from a collection. This one, then, picks one of the <code>DataTime</code> values generated by <code>MaitreD.Segment</code>. </p> <p> The <code>PostValidReservationWhenDatabaseIsEmpty</code> test can now use the <code>ReservationTime</code> generator to produce a time of day: </p> <p> <pre>[Theory] [InlineData(5)] [InlineData(9)] [InlineData(2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservationWhenDatabaseIsEmpty</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">today</span>&nbsp;=&nbsp;DateTime.Now.Date; &nbsp;&nbsp;&nbsp;&nbsp;Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;days&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;PositiveInt&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;t&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.ReservationTime(Grandfather.Restaurant,&nbsp;today) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;offset&nbsp;=&nbsp;TimeSpan.FromDays((<span style="color:blue;">int</span>)days)&nbsp;+&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Reservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(r,&nbsp;offset)).ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">at</span>&nbsp;=&nbsp;today&nbsp;+&nbsp;t.offset; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;t.r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithDate(at); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Contains(expected,&nbsp;db.Grandfather); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}).QuickCheckThrowOnFailure(); }</pre> </p> <p> Granted, the test code is getting more and more busy, but there's room for improvement. Before I simplify it, though, I think that it's more prudent to deal with the remaining literal values. </p> <p> Notice that the <code>InlineData</code> attributes now only supply a single value each: The <code>quantity</code>. </p> <h3 id="e551e156b8344bc0bd5379084bd8a7ed"> Quantity <a href="#e551e156b8344bc0bd5379084bd8a7ed">#</a> </h3> <p> Like the <code>at</code> value, the <code>quantity</code> is constrained. It must be a positive integer, but it can't be larger than the largest table in the restaurant. That number, however, isn't that hard to find: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">maxCapacity</span>&nbsp;=&nbsp;restaurant.MaitreD.Tables.Max(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Capacity);</pre> </p> <p> The FsCheck API includes a function that generates a random number within a given range. It's called <code>Gen.Choose</code>, and now that we know the range, we can use it to generate the <code>quantity</code> value. Here, I'm only showing the test-data-generator part of the test, since the rest doesn't change that much. You'll see the full test again after a few more refactorings. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">today</span>&nbsp;=&nbsp;DateTime.Now.Date; <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>&nbsp;=&nbsp;Grandfather.Restaurant; <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">maxCapacity</span>&nbsp;=&nbsp;restaurant.MaitreD.Tables.Max(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Capacity); Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;days&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;PositiveInt&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;t&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.ReservationTime(restaurant,&nbsp;today) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;offset&nbsp;=&nbsp;TimeSpan.FromDays((<span style="color:blue;">int</span>)days)&nbsp;+&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;quantity&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(1,&nbsp;maxCapacity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Reservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(r.WithQuantity(quantity),&nbsp;offset)).ToArbitrary(),</pre> </p> <p> There are now no more literal values in the test. In a sense, the refactoring from parametrised test to property-based test is complete. It could do with a bit of cleanup, though. </p> <h3 id="53494d59981c4c32b0dbbd93aa857874"> Simplification <a href="#53494d59981c4c32b0dbbd93aa857874">#</a> </h3> <p> There's no longer any need to pass along the <code>offset</code> variable, and the explicit <code>QuickCheckThrowOnFailure</code> also seems a bit redundant. I can use the <code>[Property]</code> attribute from FsCheck.Xunit instead. </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;Property&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservationWhenDatabaseIsEmpty</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">today</span>&nbsp;=&nbsp;DateTime.Now.Date; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>&nbsp;=&nbsp;Grandfather.Restaurant; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">maxCapacity</span>&nbsp;=&nbsp;restaurant.MaitreD.Tables.Max(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Capacity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;days&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;PositiveInt&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;t&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.ReservationTime(restaurant,&nbsp;today) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;at&nbsp;=&nbsp;today&nbsp;+&nbsp;TimeSpan.FromDays((<span style="color:blue;">int</span>)days)&nbsp;+&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;quantity&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(1,&nbsp;maxCapacity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Reservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;r.WithQuantity(quantity).WithDate(at)).ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Contains(expected,&nbsp;db.Grandfather); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> Compared to the initial version of the test, it has become more top-heavy. It's about the same size, though. The original version was 30 lines of code. This version is only 26 lines of code, but it is admittedly more information-dense. The original version had more 'noise' interleaved with the 'signal'. The new variation actually has a better separation of data generation and the test itself. Consider the 'actual' test code: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(restaurant), &nbsp;&nbsp;&nbsp;&nbsp;db); <span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); Assert.Contains(expected,&nbsp;db.Grandfather);</pre> </p> <p> If we could somehow separate the data generation from the test itself, we might have something that was quite readable. </p> <h3 id="a23c68b065d140c588e30bd1db228879"> Extract test data generator <a href="#a23c68b065d140c588e30bd1db228879">#</a> </h3> <p> The above data generation consists of a bit of initialisation and a query expression. Like all <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> it's easy to extract: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Gen&lt;(Restaurant,&nbsp;Reservation)&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">GenValidReservationForEmptyDatabase</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">today</span>&nbsp;=&nbsp;DateTime.Now.Date; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>&nbsp;=&nbsp;Grandfather.Restaurant; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>&nbsp;=&nbsp;restaurant.MaitreD.Tables.Max(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Capacity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">from</span>&nbsp;days&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;PositiveInt&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;t&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.ReservationTime(restaurant,&nbsp;today) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;at&nbsp;=&nbsp;today&nbsp;+&nbsp;TimeSpan.FromDays((<span style="color:blue;">int</span>)days)&nbsp;+&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;quantity&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(1,&nbsp;capacity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Reservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(restaurant,&nbsp;r.WithQuantity(quantity).WithDate(at)); }</pre> </p> <p> While it's quite specialised, it leaves the test itself small and readable: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;Property&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservationWhenDatabaseIsEmpty</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenValidReservationForEmptyDatabase().ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(<span style="font-weight:bold;color:#1f377f;">restaurant</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>)&nbsp;=&nbsp;t; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.Contains(expected,&nbsp;db[restaurant.Id]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> That's not the only way to separate test and data generation. </p> <h3 id="39343db1a22c4d0c93dbfb74e3af6689"> Test as implementation detail <a href="#39343db1a22c4d0c93dbfb74e3af6689">#</a> </h3> <p> The above separation refactors the data-generating expression to a private helper function. Alternatively you can keep all that FsCheck infrastructure code in the public test method and extract the test body itself to a private helper method: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;Property&nbsp;<span style="font-weight:bold;color:#74531f;">PostValidReservationWhenDatabaseIsEmpty</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">today</span>&nbsp;=&nbsp;DateTime.Now.Date; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>&nbsp;=&nbsp;Grandfather.Restaurant; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>&nbsp;=&nbsp;restaurant.MaitreD.Tables.Max(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Capacity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;days&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;PositiveInt&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;t&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.ReservationTime(restaurant,&nbsp;today) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;at&nbsp;=&nbsp;today&nbsp;+&nbsp;TimeSpan.FromDays((<span style="color:blue;">int</span>)days)&nbsp;+&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;quantity&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(1,&nbsp;capacity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Reservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(restaurant,&nbsp;r.WithQuantity(quantity).WithDate(at)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;PostValidReservationWhenDatabaseIsEmptyImp( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.restaurant, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item2)); }</pre> </p> <p> At first glance, that doesn't look like an improvement, but it has the advantage that the actual test method is now devoid of FsCheck details. If we use that as a yardstick for how decoupled the test is from FsCheck, this seems cleaner. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">PostValidReservationWhenDatabaseIsEmptyImp</span>( &nbsp;&nbsp;&nbsp;&nbsp;Restaurant&nbsp;<span style="font-weight:bold;color:#1f377f;">restaurant</span>,&nbsp;Reservation&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Contains(expected,&nbsp;db[restaurant.Id]); }</pre> </p> <p> Using a property-based testing framework in C# is still more awkward than in a language with better support for monadic composition and pattern matching. That said, more recent versions of C# do have better pattern matching on tuples, but this code base is still on C# 8. </p> <p> If you still think that this looks more complicated than the initial version of the test, then I agree. Property-based testing isn't free, but you get something in return. We started with four test cases and ended with 100. And that's just the default. If you want to increase the number of test cases, that's just an API call away. You could run 1,000 or 10,000 test cases if you wanted to. The only real downside is that the tests take longer to run. </p> <h3 id="624f8d06db274e54b76af869f1a790c5"> Unhappy paths <a href="#624f8d06db274e54b76af869f1a790c5">#</a> </h3> <p> The tests above all test the happy path. A valid request arrives and the system is in a state where it can accept it. This small article series is, you may recall, a response to an email from Sergei Rogovtsev. In his email, he mentioned the need to test both happy path and various error scenarios. Let's cover a few before wrapping up. </p> <p> As I was developing the system and fleshing out its behaviour, I evolved this parametrised test: </p> <p> <pre>[Theory] [InlineData(<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;j@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Jay&nbsp;Xerxes&quot;</span>,&nbsp;1)] [InlineData(<span style="color:#a31515;">&quot;not&nbsp;a&nbsp;date&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;w@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Wk&nbsp;Hd&quot;</span>,&nbsp;8)] [InlineData(<span style="color:#a31515;">&quot;2023-11-30&nbsp;20:01&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;Thora&quot;</span>,&nbsp;19)] [InlineData(<span style="color:#a31515;">&quot;2022-01-02&nbsp;12:10&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3&nbsp;Beard&quot;</span>,&nbsp;0)] [InlineData(<span style="color:#a31515;">&quot;2045-12-31&nbsp;11:45&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;git@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Gil&nbsp;Tan&quot;</span>,&nbsp;-1)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostInvalidReservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">at</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">email</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">api</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;LegacyApi(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.PostReservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;at,&nbsp;email,&nbsp;name,&nbsp;quantity&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(HttpStatusCode.BadRequest,&nbsp;response.StatusCode); }</pre> </p> <p> The test body itself is about as minimal as it can be. There are four test cases that I added one or two at a time. </p> <ul> <li>The first test case covers what happens if the <code>at</code> value is missing (i.e. null)</li> <li>The next test case covers a malformed <code>at</code> value</li> <li>The third test case covers a missing email address</li> <li>The two last test cases covers non-positive quantities, both <em>0</em> and a negative number</li> </ul> <p> It's possible to combine FsCheck generators that deal with each of these cases, but here I want to demonstrate how it's still possible to keep each error case separate, if that's what you need. First, separate the test body from its data source, like I did above: </p> <p> <pre>[Theory] [InlineData(<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;j@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Jay&nbsp;Xerxes&quot;</span>,&nbsp;1)] [InlineData(<span style="color:#a31515;">&quot;not&nbsp;a&nbsp;date&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;w@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Wk&nbsp;Hd&quot;</span>,&nbsp;8)] [InlineData(<span style="color:#a31515;">&quot;2023-11-30&nbsp;20:01&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;Thora&quot;</span>,&nbsp;19)] [InlineData(<span style="color:#a31515;">&quot;2022-01-02&nbsp;12:10&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3&nbsp;Beard&quot;</span>,&nbsp;0)] [InlineData(<span style="color:#a31515;">&quot;2045-12-31&nbsp;11:45&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;git@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Gil&nbsp;Tan&quot;</span>,&nbsp;-1)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostInvalidReservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">at</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">email</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostInvalidReservationImp(at,&nbsp;email,&nbsp;name,&nbsp;quantity); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">PostInvalidReservationImp</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">at</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">email</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">api</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;LegacyApi(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.PostReservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;at,&nbsp;email,&nbsp;name,&nbsp;quantity&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(HttpStatusCode.BadRequest,&nbsp;response.StatusCode); }</pre> </p> <p> If you consider this refactoring in isolation, it seems frivolous, but it's just preparation for further work. In each subsequent refactoring I'll convert each of the above error cases to a property. </p> <h3 id="d1d2ea6091c7440991e24e213c9154b6"> Missing date and time <a href="#d1d2ea6091c7440991e24e213c9154b6">#</a> </h3> <p> Starting from the top, convert the reservation-at-null test case to a property: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostReservationAtNull</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">email</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>,&nbsp;PositiveInt&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostInvalidReservationImp(<span style="color:blue;">null</span>,&nbsp;email,&nbsp;name,&nbsp;(<span style="color:blue;">int</span>)quantity); }</pre> </p> <p> I've left the parametrised <code>PostInvalidReservation</code> test in place, but removed the <code>[InlineData]</code> attribute with the <code>null</code> value for the <code>at</code> parameter: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;not&nbsp;a&nbsp;date&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;w@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Wk&nbsp;Hd&quot;</span>,&nbsp;8)] [InlineData(<span style="color:#a31515;">&quot;2023-11-30&nbsp;20:01&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;Thora&quot;</span>,&nbsp;19)] [InlineData(<span style="color:#a31515;">&quot;2022-01-02&nbsp;12:10&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3&nbsp;Beard&quot;</span>,&nbsp;0)] [InlineData(<span style="color:#a31515;">&quot;2045-12-31&nbsp;11:45&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;git@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Gil&nbsp;Tan&quot;</span>,&nbsp;-1)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostInvalidReservation</span>(</pre> </p> <p> The <code>PostReservationAtNull</code> property can use the FsCheck.Xunit <code>[Property]</code> attribute, because any <code>string</code> can be used for <code>email</code> and <code>name</code>. </p> <p> To be honest, it is, perhaps, cheating a bit to post any positive quantity, because a number like, say, <em>1837</em> would be a problem even if the posted representation was well-formed and valid, since no table of the restaurant has that capacity. </p> <p> Validation does, however, happen before evaluating business rules and application state, so the way the system is currently implemented, the test never fails because of that. The service never gets to that part of handling the request. </p> <p> One might argue that this is relying on (and thereby coupling to) an implementation detail, but honestly, it seems unlikely that the service would begin processing an invalid request - 'invalid' implying that the request makes no sense. Concretely, if the date and time is missing from a reservation, how can the service begin to process it? On which date? At what time? </p> <p> Thus, it's not that likely that this behaviour would change in the future, and therefore unlikely that the test would fail because of a policy change. It is, however, worth considering. </p> <h3 id="a93a77fa6f9f42b9810d37a300c1989a"> Malformed date and time <a href="#a93a77fa6f9f42b9810d37a300c1989a">#</a> </h3> <p> The next error case is when the <code>at</code> value is present, but malformed. You can also convert that case to a property: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;Property&nbsp;<span style="font-weight:bold;color:#74531f;">PostMalformedDateAndTime</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;at&nbsp;<span style="color:blue;">in</span>&nbsp;ArbMap.Default.GeneratorFor&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(<span style="font-weight:bold;color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;!DateTime.TryParse(s,&nbsp;<span style="color:blue;">out</span>&nbsp;_)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;email&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;name&nbsp;<span style="color:blue;">in</span>&nbsp;Gens.Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;quantity&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(1,&nbsp;10) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email:&nbsp;email.ToString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;name.ToString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Prop.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;PostInvalidReservationImp(t.at,&nbsp;t.email,&nbsp;t.name,&nbsp;t.quantity)); }</pre> </p> <p> Given how simple <code>PostReservationAtNull</code> turned out to be, you may be surprised that this case takes so much code to express. There's not that much going on, though. I reuse the generators I already have for <code>email</code> and <code>name</code>, and FsCheck's built-in <code>Gen.Choose</code> to pick a <code>quantity</code> between <code>1</code> and <code>10</code>. The only slightly tricky expression is for the <code>at</code> value. </p> <p> The distinguishing part of this test is that the <code>at</code> value should be malformed. A randomly generated <code>string</code> is a good starting point. After all, most strings aren't well-formed date-and-time values. Still, <a href="/2016/01/18/make-pre-conditions-explicit-in-property-based-tests">a random string <em>could</em> be interpreted as a date or time, so it's better to explicitly disallow such values</a>. This is possible with the <code>Where</code> function. It's a filter that only allows values through that are <em>not</em> understandable as dates or times - which is the vast majority of them. </p> <h3 id="3b130aa436c44e25b0b4b9d9ccf87e9a"> Null email <a href="#3b130aa436c44e25b0b4b9d9ccf87e9a">#</a> </h3> <p> The penultimate error case is when the email address is missing. That one is as easy to express as the missing <code>at</code> value. </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostNullEmail</span>(DateTime&nbsp;<span style="font-weight:bold;color:#1f377f;">at</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>,&nbsp;PositiveInt&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostInvalidReservationImp(at.ToIso8601DateTimeString(),&nbsp;<span style="color:blue;">null</span>,&nbsp;name,&nbsp;(<span style="color:blue;">int</span>)quantity); }</pre> </p> <p> Again, with the addition of this specific property, I've removed the corresponding <code>[InlineData]</code> attribute from the <code>PostInvalidReservation</code> test. It only has two remaining test cases, both about non-positive quantities. </p> <h3 id="c1897ba360a74189a3c49a1bf05fb46a"> Non-positive quantity <a href="#c1897ba360a74189a3c49a1bf05fb46a">#</a> </h3> <p> Finally, we can add a property that checks what happens if the quantity isn't positive: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="font-weight:bold;color:#74531f;">PostNonPositiveQuantity</span>( &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="font-weight:bold;color:#1f377f;">at</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">email</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;NonNegativeInt&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostInvalidReservationImp(at.ToIso8601DateTimeString(),&nbsp;email,&nbsp;name,&nbsp;-(<span style="color:blue;">int</span>)quantity); }</pre> </p> <p> FsCheck doesn't have a wrapper for non-positive integers, but I can use <code>NonNegativeInt</code> and negate it. The point is that I want to include <em>0</em>, which <code>NonNegativeInt</code> does. That wrapper generates integers greater than or equal to zero. </p> <p> Since I've now modelled each error case as a separate FsCheck property, I can remove the <code>PostInvalidReservation</code> method. </p> <h3 id="1da5bfff9de24adba8fe5f3005f35e69"> Conclusion <a href="#1da5bfff9de24adba8fe5f3005f35e69">#</a> </h3> <p> To be honest, I think that turning these parametrised tests into FsCheck properties is overkill. After all, when I wrote the code base, I found the parametrised tests adequate. I used test-driven development all the way through, and while I also kept the <a href="/2019/10/07/devils-advocate">Devil's Advocate</a> in mind, the tests that I wrote gave me sufficient confidence that the system works as it should. </p> <p> The main point of this article is to show how you <em>can</em> convert example-based tests to property-based tests. After all, just because I felt confident in my test suite it doesn't follow that a few parametrised tests does it for you. <a href="/2018/11/12/what-to-test-and-not-to-test">How much testing you need depends on a variety of factors</a>, so you may need the extra confidence that thousands of test cases can give you. </p> <p> The previous article in this series showed an abstract, but minimal example. This one is more realistic, but also more involved. </p> <p> <strong>Next:</strong> <a href="/2023/05/01/refactoring-pure-function-composition-without-breaking-existing-tests">Refactoring pure function composition without breaking existing tests</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="d7c5114a287c479db91235ded264bd55"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#d7c5114a287c479db91235ded264bd55">#</a></div> <div class="comment-content"> <p>In the section "Missing date and time", you mention that it could be worth considering the coupling of the test to the implementation details regarding validation order and possible false positive test results. Given that you already have a test data generator that produces valid reservations (GenValidReservationForEmptyDatabase), wouldn't it be more or less trivial to just generate valid test data and modify it to make it invalid in the single specific way you want to test?</p> </div> <div class="comment-date">2023-04-18 14:00 UTC</div> </div> <div class="comment" id="114d5ded264bd7c5a912355287c479db"> <div class="comment-author"><a href="https://github.com/AnthonyLloyd">Anthony Lloyd</a> <a href="#114d5ded264bd7c5a912355287c479db">#</a></div> <div class="comment-content"> <p>Am I right in thinking shrinking doesn't work in FsCheck with the query syntax? I've just tried with two ints. How would you make it work?</p> <pre><code style="background-color: #eee;border: 1px solid #999;display:block;padding:5px;">[Fact] public void ShrinkingTest() { Prop.ForAll( (from a1 in Arb.Default.Int32().Generator from a2 in Arb.Default.Int32().Generator select (a1, a2)).ToArbitrary(), t => { if (t.a2 &gt; 10) throw new System.Exception(); }) .QuickCheckThrowOnFailure(); }</code></pre> </div> <div class="comment-date">2023-04-18 19:15 UTC</div> </div> <div class="comment" id="a784010d2340448ea69d4b30f82074c2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a784010d2340448ea69d4b30f82074c2">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. It wouldn't be impossible to address that concern, but I haven't found a good way of doing it without introducing other problems. So, it's a trade-off. </p> <p> What I meant by my remark in the article is that in order to make an (otherwise) valid request, the test needs to know the maximum valid quantity, which varies from restaurant to restaurant. The problem, in a nutshell, is that the test in question operates exclusively against the REST API of the service, and that API doesn't expose any functionality that enable clients to query the configuration of tables for a given restaurant. There's no way to obtain that information. </p> <p> The only two options I can think of are: </p> <ul> <li>Add such a query API to the REST API. In this case, that seems unwarranted.</li> <li>Add a <a href="http://xunitpatterns.com/Back%20Door%20Manipulation.html">backdoor API</a> to the self-host (<code>LegacyApi</code>).</li> </ul> <p> If I had to, I'd prefer the second option, but it would still require me to add more (test) code to the code base. There's a cost to every line of code. </p> <p> Here, I'm making a bet that the grandfathered restaurant isn't going to change its configuration. The tests are then written with the implicit knowledge that that particular restaurant has a maximum table size of 10, and also particular opening and closing times. </p> <p> This makes those tests more concrete, which makes them more readable. They serve as easy-to-understand examples of how the system works (once the reader has gained the implicit knowledge I just described). </p> <p> It's not perfect. The tests are, perhaps, too obscure for that reason, and they <em>are</em> vulnerable to configuration changes. Even so, the remedies I can think of come with their own disadvantages. </p> <p> So far, I've decided that the trade-offs are best leaving things as you see them here. That doesn't mean that I wouldn't change that decision in the future if it turns out that these tests are too brittle. </p> </div> <div class="comment-date">2023-04-19 8:18 UTC</div> </div> <div class="comment" id="1690c8861d8d44cfbe23b89613825250"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1690c8861d8d44cfbe23b89613825250">#</a></div> <div class="comment-content"> <p> Anthony, thank you for writing. You're correct that in FsCheck shrinking doesn't work with query syntax; at least in the versions I've used. I'm not sure if that's planned for a future release. </p> <p> As far as I can tell, this is a consequence of the maturity of the library. You have the same issue with <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a>, which also distinguishes between <code>Gen</code> and <code>Arbitrary</code>. While <code>Gen</code> is a monad, <code>Arbitrary</code>'s <code>shrink</code> function is <a href="/2022/08/01/invariant-functors">invariant</a>, which prevents it from being a functor (and hence, also from being a monad). </p> <p> FsCheck is a mature port of QuickCheck, so it has the same limitation. No functor, no query syntax. </p> <p> Later, this limitation was solved by modelling shrinking based on a lazily evaluated shrink tree, which does allow for a monad. The first time I saw that in effect was in <a href="https://hedgehog.qa/">Hedgehog</a>. </p> </div> <div class="comment-date">2023-04-21 6:17 UTC</div> </div> <div class="comment" id="774e5ded214bd7c5a912355287c479db"> <div class="comment-author"><a href="https://github.com/AnthonyLloyd">Anthony Lloyd</a> <a href="#774e5ded214bd7c5a912355287c479db">#</a></div> <div class="comment-content"> <p>Hedgehog does a little better than FsCheck but it doesn't shrink well when the variables are dependent.</p> <pre><code style="background-color: #eee;border: 1px solid #999;display:block;padding:5px;">[Fact] public void ShrinkingTest_Hedgehog() { Property.ForAll( from a1 in Gen.Int32(Range.ConstantBoundedInt32()) from a2 in Gen.Int32(Range.ConstantBoundedInt32()) where a1 > a2 select (a1, a2)) .Select(t => { if (t.a2 &gt; 10) throw new System.Exception(); }) .Check(PropertyConfig.Default.WithTests(1_000_000).WithShrinks(1_000_000)); } [Fact] public void ShrinkingTest_Hedgehog2() { Property.ForAll( from a1 in Gen.Int32(Range.ConstantBoundedInt32()) from a2 in Gen.Int32(Range.Constant(0, a1)) select (a1, a2)) .Select(t => { if (t.a2 &gt; 10) throw new System.Exception(); }) .Check(PropertyConfig.Default.WithTests(1_000_000).WithShrinks(1_000_000)); } [Fact] public void ShrinkingTest_CsCheck() { (from a1 in Gen.Int from a2 in Gen.Int where a1 > a2 select (a1, a2)) .Sample((_, a2) => { if (a2 &gt; 10) throw new Exception(); }, iter: 1_000_000); } [Fact] public void ShrinkingTest_CsCheck2() { (from a1 in Gen.Int.Positive from a2 in Gen.Int[0, a1] select (a1, a2)) .Sample((_, a2) => { if (a2 &gt; 10) throw new Exception(); }, iter: 1_000_000); }</code></pre> <p>This and the syntax complexity I mentioned in the previous post were the reasons I developed CsCheck. Random shrinking is the key innovation that makes it simpler.</p> </div> <div class="comment-date">2023-04-21 16:38 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Anagrams kata as a one-liner https://blog.ploeh.dk/2023/04/10/anagrams-kata-as-a-one-liner 2023-04-10T08:08:00+00:00 Mark Seemann <div id="post"> <p> <em>A futile exercise in code compaction.</em> </p> <p> Recently I was doing the <a href="http://codekata.com/kata/kata06-anagrams/">Anagrams kata</a> in <a href="https://fsharp.org/">F#</a> with <a href="https://github.com/gdziadkiewicz">Grzegorz Dziadkiewicz</a>, and along the way realised that the implementation is essentially a one-liner. I thought it would be fun to redo the exercise in <a href="https://www.haskell.org/">Haskell</a> and see how compact code I could get away with. </p> <p> In short, in the exercise, you're given a list of words, and you need to find all the <a href="https://en.wikipedia.org/wiki/Anagram">anagrams</a> in the list. For example, given the list <em>bar, foo, bra</em>, the result should be <em>bar, bra</em>, and <em>foo</em> shouldn't be part of the output, since it's not an anagram of any other word in the list. </p> <h3 id="99c78f1d41a540128c07eb8912e4ca92"> A pipeline of transformations <a href="#99c78f1d41a540128c07eb8912e4ca92">#</a> </h3> <p> My idea was to collect all the words in a <a href="https://hackage.haskell.org/package/containers/docs/Data-Map-Strict.html">Map</a> (dictionary) keyed by the string, but sorted. Even if the sorted string is a nonsense word, all anagrams sort to the same sequence of letters: </p> <p> <pre>ghci&gt; sort "bar" "abr" ghci&gt; sort "bra" "abr"</pre> </p> <p> Each of the keys should contain a <a href="https://hackage.haskell.org/package/containers-0.6.7/docs/Data-Set.html">Set</a> of words, since I don't care about the order. </p> <p> Once I have that map of sets, I can throw away the singleton sets, and then the keys. Or perhaps first throw away the keys, and then the singleton sets. The order of those two steps doesn't matter. </p> <p> The reason I don't want the singleton sets is that a set with only one word means that no anagrams were found. </p> <h3 id="53831677170c455cb4624971c8e82249"> Creating the map <a href="#53831677170c455cb4624971c8e82249">#</a> </h3> <p> How to create the desired map? The <code>Map</code> module exports the <a href="https://hackage.haskell.org/package/containers/docs/Data-Map-Strict.html#v:fromListWith">fromListWith</a> function that enables you to go through an <a href="https://en.wikipedia.org/wiki/Association_list">association list</a> and combine values along the way, in case you encounter the key more than once. That sounds useful, but means that first I have to convert the list of words to an association list. </p> <p> Importing <a href="https://hackage.haskell.org/package/base/docs/Control-Arrow.html">Control.Arrow</a>, I can do it like this: </p> <p> <pre>ghci&gt; fmap (sort &amp;&amp;&amp; Set.singleton) ["bar", "foo", "bra"] [("abr",fromList ["bar"]),("foo",fromList ["foo"]),("abr",fromList ["bra"])]</pre> </p> <p> Each element in the list is a pair of a key, and a set containing a single word. Notice that the set containing <code>"bar"</code> has the same key as the set containing <code>"bra"</code>. When using <code>fromListWith</code>, the function will have to unite these two sets whenever it encounters the same key. </p> <p> <pre>ghci&gt; Map.fromListWith Set.union $ fmap (sort &amp;&amp;&amp; Set.singleton) ["bar", "foo", "bra"] fromList [("abr",fromList ["bar","bra"]),("foo",fromList ["foo"])]</pre> </p> <p> The two anagrams <code>"bar"</code> and <code>"bra"</code> now belong to the same set, while <code>"foo"</code> is still solitary. </p> <h3 id="bbd82971c2324018973182c27d613a64"> Finding the anagrams <a href="#bbd82971c2324018973182c27d613a64">#</a> </h3> <p> Now that we've grouped sets according to key, we no longer need the keys: </p> <p> <pre>ghci&gt; Map.elems $ Map.fromListWith Set.union $ fmap (sort &amp;&amp;&amp; Set.singleton) ["bar", "foo", "bra"] [fromList ["bar","bra"],fromList ["foo"]]</pre> </p> <p> The anagrams are those sets that have more than one element, so we can throw away those that are smaller. </p> <p> <pre>ghci&gt; filter ((1 &lt;) . Set.size) $ Map.elems $ Map.fromListWith Set.union $ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmap (sort &amp;&amp;&amp; Set.singleton) ["bar", "foo", "bra"] [fromList ["bar","bra"]]</pre> </p> <p> The expression has now grown to such a width that I've broken it into two lines to make it more readable. It really is just one line, though. </p> <h3 id="203f41225d944d1689cf1df0d3f8118f"> Function <a href="#203f41225d944d1689cf1df0d3f8118f">#</a> </h3> <p> To save a bit of space, I eta-reduced the expression before I made it a function: </p> <p> <pre><span style="color:#2b91af;">anagrams</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Ord</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[[a]]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Set</span>&nbsp;(<span style="color:blue;">Set</span>&nbsp;[a]) anagrams&nbsp;= &nbsp;&nbsp;Set.fromList&nbsp;.&nbsp;<span style="color:blue;">filter</span>&nbsp;((1&nbsp;&lt;)&nbsp;.&nbsp;Set.size)&nbsp;.&nbsp;Map.elems&nbsp;.&nbsp;Map.fromListWith&nbsp;Set.union &nbsp;&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(sort&nbsp;&amp;&amp;&amp;&nbsp;Set.singleton)</pre> </p> <p> The leftmost <code>Set.fromList</code> converts the list of anagrams to a <code>Set</code> of anagrams, since I didn't think that it was a postcondition that the anagrams should be returned in a specific order. </p> <p> Unfortunately the expression is still so wide that I found it necessary to break it into two lines. </p> <p> Just for the hell of it, I tried to fix the situation by changing the imports: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;Control.Arrow <span style="color:blue;">import</span>&nbsp;Data.List&nbsp;(<span style="color:#2b91af;">sort</span>) <span style="color:blue;">import</span>&nbsp;Data.Map.Strict&nbsp;(<span style="color:#2b91af;">fromListWith</span>,&nbsp;<span style="color:#2b91af;">elems</span>) <span style="color:blue;">import</span>&nbsp;Data.Set&nbsp;(<span style="color:blue;">Set</span>,&nbsp;<span style="color:#2b91af;">fromList</span>,&nbsp;<span style="color:#2b91af;">singleton</span>)</pre> </p> <p> With this very specific set of imports, the expression now barely fits on a single line: </p> <p> <pre><span style="color:#2b91af;">anagrams</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:blue;">Ord</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[[a]]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Set</span>&nbsp;(<span style="color:blue;">Set</span>&nbsp;[a]) anagrams&nbsp;=&nbsp;fromList&nbsp;.&nbsp;<span style="color:blue;">filter</span>&nbsp;((1&nbsp;&lt;)&nbsp;.&nbsp;<span style="color:blue;">length</span>)&nbsp;.&nbsp;elems&nbsp;.&nbsp;fromListWith&nbsp;<span style="color:#2b91af;">(&lt;&gt;)</span>&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(sort&nbsp;&amp;&amp;&amp;&nbsp;singleton)</pre> </p> <p> Here, I also took advantage of <code>Semigroup</code> <em>append</em> (<code>&lt;&gt;</code>) being equal to <code>Set.union</code> for <code>Set</code>. </p> <p> Is it readable? Hardly. </p> <p> My main goal with the exercise was to implement the desired functionality as a single expression. Perhaps I was inspired by Dave Thomas, who wrote: </p> <blockquote> <p> "I hacked a solution together in 25 lines of Ruby." </p> <footer><cite><a href="http://codekata.com/kata/kata06-anagrams/">Dave Thomas</a></cite></footer> </blockquote> <p> 25 lines of Ruby? I can do it in one line of Haskell! </p> <p> Is that interesting? Does it make sense to compare two languages? Why not? By trying out different languages you learn the strengths and weaknesses of each. There's no denying that Haskell is <em>expressive</em>. On the other hand, what you can't see in this blog post is that compilation takes forever. Not for this code in particular, but in general. </p> <p> I'm sure Dave Thomas was done with his Ruby implementation before my machine had even finished compiling the empty, scaffolded Haskell code. </p> <h3 id="3ee6ff0610174f3ea5293baaefbc9ee3"> Performance <a href="#3ee6ff0610174f3ea5293baaefbc9ee3">#</a> </h3> <p> Dave Thomas also wrote: </p> <blockquote> <p> "It runs on <a href="http://codekata.com/data/wordlist.txt">this wordlist</a> in 1.8s on a 1.7GHz i7." </p> </blockquote> <p> Usually I don't care that much about performance as long as it's adequate. Or rather, I find that good software architecture with poor algorithms usually beats bad architecture with good algorithms. But I digress. </p> <p> How fares my one-liner against Dave Thomas' implementation? </p> <p> <pre>ghci&gt; :set +s ghci&gt; length . anagrams . lines &lt;$&gt; readFile "wordlist.txt" 20683 (3.56 secs, 1,749,984,448 bytes)</pre> </p> <p> Oh, 3.56 seconds isn't particularly g <em>Holy thunk, Batman! 1.7 gigabytes!</em> </p> <p> That's actually disappointing, I admit. If only I could excuse this by running on a weaker machine, but mine is a 1.9 GHz i7. Nominally faster than Dave Thomas' machine. </p> <p> At least, the time it takes to run through that 3.7 MB file is the same order of magnitude. </p> <h3 id="224925799e714bb1922f9c41a68207a3"> Tests <a href="#224925799e714bb1922f9c41a68207a3">#</a> </h3> <p> Since I had a good idea about the kind of implementation I was aiming for, I didn't write that many tests. Only three, actually. </p> <p> <pre><span style="color:#2b91af;">main</span>&nbsp;<span style="color:blue;">::</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;=&nbsp;defaultMain&nbsp;$&nbsp;hUnitTestToTests&nbsp;(TestList&nbsp;[ &nbsp;&nbsp;<span style="color:#a31515;">&quot;Examples&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">words</span>,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>],&nbsp;Set.empty), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bra&quot;</span>],&nbsp;Set.fromList&nbsp;[Set.fromList&nbsp;[<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bra&quot;</span>]]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bra&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;oof&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set.fromList&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set.fromList&nbsp;[<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;oof&quot;</span>],&nbsp;Set.fromList&nbsp;[<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bra&quot;</span>]]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;anagrams&nbsp;<span style="color:blue;">words</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual &nbsp;&nbsp;])</pre> </p> <p> As I usually do it in Haskell, these are <a href="/2018/05/07/inlined-hunit-test-lists">inlined</a> <a href="/2018/04/30/parametrised-unit-tests-in-haskell">parametrised HUnit tests</a>. </p> <h3 id="3b4f477f18954c5095d48f450f935092"> Conclusion <a href="#3b4f477f18954c5095d48f450f935092">#</a> </h3> <p> <a href="/2020/01/13/on-doing-katas">Doing katas</a> is a good way to try out new ideas, dumb or otherwise. Implementing the Anagrams kata as a one-liner was fun, but the final code shown here is sufficiently unreadable that I wouldn't recommend it. </p> <p> You could still write the <code>anagrams</code> function based on the idea presented here, but in a shared code base with an indefinite life span, I'd break it up into multiple expressions with descriptive names. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An abstract example of refactoring from interaction-based to property-based testing https://blog.ploeh.dk/2023/04/03/an-abstract-example-of-refactoring-from-interaction-based-to-property-based-testing 2023-04-03T06:02:00+00:00 Mark Seemann <div id="post"> <p> <em>A C# example with xUnit.net and CsCheck</em> </p> <p> This is the first comprehensive example that accompanies the article <a href="/2023/02/13/epistemology-of-interaction-testing">Epistemology of interaction testing</a>. In that article, I argue that in a code base that leans toward functional programming (FP), property-based testing is a better fit than interaction-based testing. In this example, I will show how to refactor simple interaction-based tests into a property-based tests. </p> <p> This small article series was prompted by an email from Sergei Rogovtsev, who was kind enough to furnish <a href="https://github.com/srogovtsev/mocks-in-tests">example code</a>. I'll use his code as a starting point for this example, so I've <a href="https://github.com/ploeh/mocks-in-tests">forked the repository</a>. If you want to follow along, all my work is in a branch called <em>no-mocks</em>. That branch simply continues off the <em>master</em> branch. </p> <h3 id="5fe789d2b85b449fa2677bbcbf095bfc"> Interaction-based testing <a href="#5fe789d2b85b449fa2677bbcbf095bfc">#</a> </h3> <p> Sergei Rogovtsev writes: </p> <blockquote> <p> "A major thing to point out here is that I'm not following TDD here not by my own choice, but because my original question arose in a context of a legacy system devoid of tests, so I choose to present it to you in the same way. I imagine that working from tests would avoid a lot of questions." </p> </blockquote> <p> Even when using test-driven development (TDD), most code bases I've seen make use of <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a> and <a href="http://xunitpatterns.com/Mock%20Object.html">Mocks</a> (or, rather, <a href="http://xunitpatterns.com/Test%20Spy.html">Spies</a>). In an object-oriented context this can make much sense. After all, a catch phrase of object-oriented programming is <em>tell, don't ask</em>. </p> <p> If you base API design on that principle, you're modelling side effects, and it makes sense that tests use Spies to verify those side effects. The book <a href="/ref/goos">Growing Object-Oriented Software, Guided by Tests</a> is a good example of this approach. Thus, even if you follow established good TDD practice, you could easily arrive at a code base reminiscent of Sergei Rogovtsev's example. I've written plenty of such code bases myself. </p> <p> Sergei Rogovtsev then extracts a couple of components, leaving him with a <code>Controller</code> class looking like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Complete</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>&nbsp;=&nbsp;_repository.GetState(state); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(_stateValidator.Validate(code,&nbsp;knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;_renderer.Success(knownState); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;_renderer.Failure(knownState); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">catch</span>&nbsp;(Exception&nbsp;<span style="font-weight:bold;color:#1f377f;">e</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;_renderer.Error(knownState,&nbsp;e); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This code snippet doesn't show the entire class, but only its solitary action method. Keep in mind that the entire repository is available on GitHub if you want to see the surrounding code. </p> <p> The <code>Complete</code> method orchestrates three injected dependencies: <code>_repository</code>, <code>_stateValidator</code>, and <code>_renderer</code>. The question that Sergei Rogovtsev asks is how to test this method. You may think that it's so simple that you don't need to test it, but keep in mind that this is a <a href="https://en.wikipedia.org/wiki/Minimal_reproducible_example">minimal and self-contained example</a> that stands in for something more complicated. </p> <p> The method has a cyclomatic complexity of <em>3</em>, so <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">you need at least three test cases</a>. That's also what Sergei Rogovtsev's code contains. I'll show each test case in turn, while I refactor them. </p> <p> The overall question is still this: Both <code>IStateValidator</code> and <code>IRenderer</code> interfaces have only a single production implementation, and in both cases the implementations are <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. If interaction-based testing is suboptimal, is there a better way to test this code? </p> <p> As I outlined in <a href="/2023/02/13/epistemology-of-interaction-testing">the introductory article</a>, I consider property-based testing a good alternative. In the following, I'll refactor the tests. Since the tests already use <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>, most of the preliminary work can be done without choosing a property-based testing framework. I'll postpone that decision until I need it. </p> <h3 id="ca095000df004ef9b7cb7c022071f76b"> State validator <a href="#ca095000df004ef9b7cb7c022071f76b">#</a> </h3> <p> The <code>IStateValidator</code> interface has a single implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">StateValidator</span>&nbsp;:&nbsp;IStateValidator { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Validate</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>,&nbsp;(<span style="color:blue;">string</span>&nbsp;expectedCode,&nbsp;<span style="color:blue;">bool</span>&nbsp;isMobile,&nbsp;Uri&nbsp;redirect)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;code&nbsp;==&nbsp;knownState.expectedCode; }</pre> </p> <p> The <code>Validate</code> method is a pure function, so it's completely deterministic. It means that you don't have to hide it behind an interface and replace it with a <a href="https://martinfowler.com/bliki/TestDouble.html">Test Double</a> in order to control it. Rather, just feed it proper data. Still, that's not what the interaction-based tests do: </p> <p> <pre>[Theory] [AutoData] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HappyPath</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>,&nbsp;(<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;Uri)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>) { &nbsp;&nbsp;&nbsp;&nbsp;_repository.Add(state,&nbsp;knownState); &nbsp;&nbsp;&nbsp;&nbsp;_stateValidator &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">validator</span>&nbsp;=&gt;&nbsp;validator.Validate(code,&nbsp;knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;_renderer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">renderer</span>&nbsp;=&gt;&nbsp;renderer.Success(knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(response); &nbsp;&nbsp;&nbsp;&nbsp;_target &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Complete(state,&nbsp;code) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Should().Be(response); }</pre> </p> <p> These tests use AutoFixture, which will make it a bit easier to refactor them to properties. It also makes the test a bit more abstract, since you don't get to see concrete test data. In short, the <code>[AutoData]</code> attribute will generate a random <code>state</code> string, a random <code>code</code> string, and so on. If you want to see an example with concrete test data, the next article shows that variation. </p> <p> The test uses <a href="https://github.com/moq/moq4">Moq</a> to control the behaviour of the Test Doubles. It states that the <code>Validate</code> method will return <code>true</code> when called with certain arguments. This is possible because you can redefine its behaviour, but as far as executable specifications go, this test doesn't reflect reality. There's only one <code>Validate</code> implementation, and it doesn't behave like that. Rather, it'll return <code>true</code> when <code>code</code> is equal to <code>knownState.expectedCode</code>. The test poorly communicates that behaviour. </p> <p> Even before I replace AutoFixture with CsCheck, I'll prepare the test by making it more honest. I'll replace the <code>code</code> parameter with a <a href="http://xunitpatterns.com/Derived%20Value.html">Derived Value</a>: </p> <p> <pre>[Theory] [AutoData] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HappyPath</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;(<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;Uri)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(<span style="font-weight:bold;color:#1f377f;">expectedCode</span>,&nbsp;<span style="color:blue;">_</span>,&nbsp;<span style="color:blue;">_</span>)&nbsp;=&nbsp;knownState; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>&nbsp;=&nbsp;expectedCode; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;The&nbsp;rest&nbsp;of&nbsp;the&nbsp;test...</span></pre> </p> <p> I've removed the <code>code</code> parameter to replace it with a variable derived from <code>knownState</code>. Notice how this <em>documents</em> the overall behaviour of the (sub-)system. </p> <p> This also means that I can now replace the <code>IStateValidator</code> Test Double with the real, pure implementation: </p> <p> <pre>[Theory] [AutoData] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HappyPath</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;(<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;Uri)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(<span style="font-weight:bold;color:#1f377f;">expectedCode</span>,&nbsp;<span style="color:blue;">_</span>,&nbsp;<span style="color:blue;">_</span>)&nbsp;=&nbsp;knownState; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>&nbsp;=&nbsp;expectedCode; &nbsp;&nbsp;&nbsp;&nbsp;_repository.Add(state,&nbsp;knownState); &nbsp;&nbsp;&nbsp;&nbsp;_renderer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">renderer</span>&nbsp;=&gt;&nbsp;renderer.Success(knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(response); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Controller(_repository,&nbsp;<span style="color:blue;">new</span>&nbsp;StateValidator(),&nbsp;_renderer.Object); &nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Complete(state,&nbsp;code) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Should().Be(response); }</pre> </p> <p> I give the <code>Failure</code> test case the same treatment: </p> <p> <pre>[Theory] [AutoData] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Failure</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;(<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;Uri)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(<span style="font-weight:bold;color:#1f377f;">expectedCode</span>,&nbsp;<span style="color:blue;">_</span>,&nbsp;<span style="color:blue;">_</span>)&nbsp;=&nbsp;knownState; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>&nbsp;=&nbsp;expectedCode&nbsp;+&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>;&nbsp;<span style="color:green;">//&nbsp;Any&nbsp;extra&nbsp;string&nbsp;will&nbsp;do</span> &nbsp;&nbsp;&nbsp;&nbsp;_repository.Add(state,&nbsp;knownState); &nbsp;&nbsp;&nbsp;&nbsp;_renderer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">renderer</span>&nbsp;=&gt;&nbsp;renderer.Failure(knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(response); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Controller(_repository,&nbsp;<span style="color:blue;">new</span>&nbsp;StateValidator(),&nbsp;_renderer.Object); &nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Complete(state,&nbsp;code) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Should().Be(response); }</pre> </p> <p> The third test case is a bit more interesting. </p> <h3 id="aa150cf1b817416ea49e30cdc8b27ac7"> An impossible case <a href="#aa150cf1b817416ea49e30cdc8b27ac7">#</a> </h3> <p> Before I make any changes to it, the third test case is this: </p> <p> <pre>[Theory] [AutoData] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Error</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>, &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;Uri)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>, &nbsp;&nbsp;&nbsp;&nbsp;Exception&nbsp;<span style="font-weight:bold;color:#1f377f;">e</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>) { &nbsp;&nbsp;&nbsp;&nbsp;_repository.Add(state,&nbsp;knownState); &nbsp;&nbsp;&nbsp;&nbsp;_stateValidator &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">validator</span>&nbsp;=&gt;&nbsp;validator.Validate(code,&nbsp;knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Throws(e); &nbsp;&nbsp;&nbsp;&nbsp;_renderer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">renderer</span>&nbsp;=&gt;&nbsp;renderer.Error(knownState,&nbsp;e)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(response); &nbsp;&nbsp;&nbsp;&nbsp;_target &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Complete(state,&nbsp;code) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Should().Be(response); }</pre> </p> <p> This test case verifies the behaviour of the <code>Controller</code> class when the <code>Validate</code> method throws an exception. If we want to instead use the real, pure implementation, how can we get it to throw an exception? Consider it again: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Validate</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>,&nbsp;(<span style="color:blue;">string</span>&nbsp;expectedCode,&nbsp;<span style="color:blue;">bool</span>&nbsp;isMobile,&nbsp;Uri&nbsp;redirect)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>) &nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;code&nbsp;==&nbsp;knownState.expectedCode;</pre> </p> <p> As far as I can tell, there's no way to get this method to throw an exception. You might suggest passing <code>null</code> as the <code>knownState</code> parameter, but that's not possible. This is a new version of C# and the <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/nullable-reference-types">nullable reference types</a> feature is turned on. I spent some fifteen minutes trying to convince the compiler to pass a <code>null</code> argument in place of <code>knownState</code>, but I couldn't make it work in a unit test. </p> <p> That's interesting. The <code>Error</code> test is exercising a code path that's impossible in production. Is it redundant? </p> <p> It might be, but here I think that it's more an artefact of the process. Sergei Rogovtsev has provided a minimal example, and as it sometimes happens, perhaps it's a bit too minimal. He did write, however, that he considered it essential for the example that the logic involved more that an Boolean <em>true/false</em> condition. In order to keep with the spirit of the example, then, I'm going to modify the <code>Validate</code> method so that it's also possible to make it throw an exception: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Validate</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>,&nbsp;(<span style="color:blue;">string</span>&nbsp;expectedCode,&nbsp;<span style="color:blue;">bool</span>&nbsp;isMobile,&nbsp;Uri&nbsp;redirect)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(knownState&nbsp;==&nbsp;<span style="color:blue;">default</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(knownState)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;code&nbsp;==&nbsp;knownState.expectedCode; }</pre> </p> <p> The method now throws an exception if you pass it a <code>default</code> value for <code>knownState</code>. From an implementation standpoint, there's no reason to do this, so it's only for the sake of the example. You can now test how the <code>Controller</code> handles an exception: </p> <p> <pre>[Theory] [AutoData] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Error</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>) { &nbsp;&nbsp;&nbsp;&nbsp;_repository.Add(state,&nbsp;<span style="color:blue;">default</span>); &nbsp;&nbsp;&nbsp;&nbsp;_renderer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(<span style="font-weight:bold;color:#1f377f;">renderer</span>&nbsp;=&gt;&nbsp;renderer.Error(<span style="color:blue;">default</span>,&nbsp;It.IsAny&lt;Exception&gt;())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(response); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Controller(_repository,&nbsp;<span style="color:blue;">new</span>&nbsp;StateValidator(),&nbsp;_renderer.Object); &nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Complete(state,&nbsp;code) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Should().Be(response); }</pre> </p> <p> The test no longer has a reference to the specific <code>Exception</code> object that <code>Validate</code> is going to throw, so instead it has to use Moq's <code>It.IsAny</code> API to configure the <code>_renderer</code>. This is, however, only an interim step, since it's now time to treat that dependency in the same way as the validator. </p> <h3 id="389a212d80a4445c99917cbd15b043eb"> Renderer <a href="#389a212d80a4445c99917cbd15b043eb">#</a> </h3> <p> The <code>Renderer</code> class has three methods, and they are all pure functions: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Renderer</span>&nbsp;:&nbsp;IRenderer { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Success</span>((<span style="color:blue;">string</span>&nbsp;expectedCode,&nbsp;<span style="color:blue;">bool</span>&nbsp;isMobile,&nbsp;Uri&nbsp;redirect)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(knownState.isMobile) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;{\&quot;success\&quot;:&nbsp;true,&nbsp;\&quot;redirect\&quot;:&nbsp;\&quot;&quot;</span>&nbsp;+&nbsp;knownState.redirect&nbsp;+&nbsp;<span style="color:#a31515;">&quot;\&quot;}&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;302&nbsp;Location:&nbsp;&quot;</span>&nbsp;+&nbsp;knownState.redirect; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Failure</span>((<span style="color:blue;">string</span>&nbsp;expectedCode,&nbsp;<span style="color:blue;">bool</span>&nbsp;isMobile,&nbsp;Uri&nbsp;redirect)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(knownState.isMobile) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;{\&quot;success\&quot;:&nbsp;false,&nbsp;\&quot;redirect\&quot;:&nbsp;\&quot;login\&quot;}&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;302&nbsp;Location:&nbsp;login&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Error</span>((<span style="color:blue;">string</span>&nbsp;expectedCode,&nbsp;<span style="color:blue;">bool</span>&nbsp;isMobile,&nbsp;Uri&nbsp;redirect)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>,&nbsp;Exception&nbsp;<span style="font-weight:bold;color:#1f377f;">e</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(knownState.isMobile) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;{\&quot;error\&quot;:&nbsp;\&quot;&quot;</span>&nbsp;+&nbsp;e.Message&nbsp;+&nbsp;<span style="color:#a31515;">&quot;\&quot;}&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;500&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since all three methods are deterministic, automated tests can control their behaviour simply by passing in the appropriate arguments: </p> <p> <pre>[Theory] [AutoData] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HappyPath</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;(<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;Uri)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>) { &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(<span style="font-weight:bold;color:#1f377f;">expectedCode</span>,&nbsp;<span style="color:blue;">_</span>,&nbsp;<span style="color:blue;">_</span>)&nbsp;=&nbsp;knownState; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>&nbsp;=&nbsp;expectedCode; &nbsp;&nbsp;&nbsp;&nbsp;_repository.Add(state,&nbsp;knownState); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">renderer</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Renderer(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Controller(_repository,&nbsp;renderer); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;renderer.Success(knownState); &nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Complete(state,&nbsp;code) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Should().Be(expected); }</pre> </p> <p> Instead of configuring an <code>IRenderer</code> Stub, the test can state the <code>expected</code> output: That the output is equal to the output that <code>renderer.Success</code> would return. </p> <p> Notice that the test doesn't <em>require</em> that the implementation calls <code>renderer.Success</code>. It only requires that the output is equal to the output that <code>renderer.Success</code> would return. Thus, it has less of an opinion about the implementation, which means that it's marginally less coupled to it. </p> <p> You might protest that the test now duplicates the implementation code. This is partially true, but no more than the previous incarnation of it. Before, the test used Moq to explicitly <em>require</em> that <code>renderer.Success</code> gets called. Now, there's still coupling, but this refactoring reduces it. </p> <p> As a side note, this may partially be an artefact of the process. Here I'm refactoring tests while keeping the implementation intact. Had I started with a property, perhaps the test would have turned out differently, and less coupled to the implementation. If you're interested in a successful exercise in using property-based TDD, you may find my article <a href="/2021/06/28/property-based-testing-is-not-the-same-as-partition-testing">Property-based testing is not the same as partition testing</a> interesting. </p> <h3 id="81bbcfc59f5c48df8ee15f9d1bad47cf"> Simplification <a href="#81bbcfc59f5c48df8ee15f9d1bad47cf">#</a> </h3> <p> Once you've refactored the tests to use the pure functions as dependencies, you no longer need the interfaces. The interfaces <code>IStateValidator</code> and <code>IRenderer</code> only existed to support testing. Now that the tests no longer use the interfaces, you can delete them. </p> <p> Furthermore, once you've removed those interfaces, there's no reason for the classes to support instantiation. Instead, make them <code>static</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">StateValidator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Validate</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">string</span>&nbsp;expectedCode,&nbsp;<span style="color:blue;">bool</span>&nbsp;isMobile,&nbsp;Uri&nbsp;redirect)&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(knownState&nbsp;==&nbsp;<span style="color:blue;">default</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(knownState)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;code&nbsp;==&nbsp;knownState.expectedCode; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can do the same for the <code>Renderer</code> class. </p> <p> This doesn't change the overall <em>flow</em> of the <code>Controller</code> class' <code>Complete</code> method, although the implementation details have changed a bit: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Complete</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>&nbsp;=&nbsp;_repository.GetState(state); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(StateValidator.Validate(code,&nbsp;knownState)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Renderer.Success(knownState); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Renderer.Failure(knownState); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">catch</span>&nbsp;(Exception&nbsp;<span style="font-weight:bold;color:#1f377f;">e</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Renderer.Error(knownState,&nbsp;e); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> <code>StateValidator</code> and <code>Renderer</code> are no longer injected dependencies, but rather 'modules' that affords pure functions. </p> <p> Both the <code>Controller</code> class and the tests that cover it are simpler. </p> <h3 id="984cd44e8f6545ddafa673a411e188f1"> Properties <a href="#984cd44e8f6545ddafa673a411e188f1">#</a> </h3> <p> So far I've been able to make all these changes without introducing a property-based testing framework. This was possible because the tests already used AutoFixture, which, while not a property-based testing framework, already strongly encourages you to write tests without literal test data. </p> <p> This makes it easy to make the final change to property-based testing. On the other hand, it's a bit unfortunate from a pedagogical perspective. This means that you didn't get to see how to refactor a 'traditional' unit test to a property. The next article in this series will plug that hole, as well as show a more realistic example. </p> <p> It's now time to pick a property-based testing framework. On .NET you have a few choices. Since this code base is C#, you may consider a framework written in C#. I'm not convinced that this is necessarily better, but it's a worthwhile experiment. Here I've used <a href="https://github.com/AnthonyLloyd/CsCheck">CsCheck</a>. </p> <p> Since the tests already used randomly generated test data, the conversion to CsCheck is relatively straightforward. I'm only going to show one of the tests. You can always find the rest of the code in the Git repository. </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HappyPath</span>() { &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;state&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.String &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;expectedCode&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.String &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;isMobile&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Bool &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;urls&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;https://example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;https://example.org&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;redirect&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.OneOfConst(urls).Select(<span style="font-weight:bold;color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Uri(s)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(state,&nbsp;(expectedCode,&nbsp;isMobile,&nbsp;redirect))) &nbsp;&nbsp;&nbsp;&nbsp;.Sample((<span style="font-weight:bold;color:#1f377f;">state</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">knownState</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(<span style="font-weight:bold;color:#1f377f;">expectedCode</span>,&nbsp;<span style="color:blue;">_</span>,&nbsp;<span style="color:blue;">_</span>)&nbsp;=&nbsp;knownState; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">code</span>&nbsp;=&nbsp;expectedCode; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">repository</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;RepositoryStub(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repository.Add(state,&nbsp;knownState); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Controller(repository); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;Renderer.Success(knownState); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Complete(state,&nbsp;code) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Should().Be(expected); &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> Compared to the AutoFixture version of the test, this looks more complicated. Part of it is that CsCheck (as far as I know) doesn't have the same <a href="https://www.nuget.org/packages/AutoFixture.Xunit2/">integration with xUnit.net that AutoFixture has</a>. That might be an issue that someone could address; after all, <a href="https://fscheck.github.io/FsCheck/RunningTests.html">FsCheck has framework integration</a>, to name an example. </p> <p> <a href="/2023/02/13/epistemology-of-interaction-testing">Test data generators are monads</a> so you typically leverage whatever syntactic sugar a language offers to simplify monadic composition. <a href="/2022/03/28/monads">In C# that syntactic sugar is query syntax</a>, which explains that initial <code>from</code> block. </p> <p> The test does look too top-heavy for my taste. An equivalent problem appears in the next article, where I also try to address it. In general, the better monad support a language offers, the more elegantly you can address this kind of problem. C# isn't really there yet, whereas languages like <a href="https://fsharp.org/">F#</a> and <a href="https://www.haskell.org/">Haskell</a> offer superior alternatives. </p> <h3 id="b0e7738d8b0440bba33caa470fd440fb"> Conclusion <a href="#b0e7738d8b0440bba33caa470fd440fb">#</a> </h3> <p> In this article I've tried to demonstrate how property-based testing is a viable alternative to using Stubs and Mocks for verification of composition. You can try to sabotage the <code>Controller.Complete</code> method in the <em>no-mocks</em> branch and see that one or more properties will fail. </p> <p> While the example code base that I've used for this article has the strength of being small and self-contained, it also suffers from a few weaknesses. It's perhaps a bit too abstract to truly resonate. It also uses AutoFixture to generate test data, which already takes it halfway towards property-based testing. While that makes the refactoring easier, it also means that it may not fully demonstrate how to refactor an example-based test to a property. I'll try to address these shortcomings in the next article. </p> <p> <strong>Next:</strong> <a href="/2023/04/17/a-restaurant-example-of-refactoring-from-example-based-to-property-based-testing">A restaurant example of refactoring from example-based to property-based testing</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0510331b953a4c308c3b01a918d2b65c"> <div class="comment-author"><a href="https://github.com/srogovtsev">Sergei Rogovtcev</a> <a href="#0510331b953a4c308c3b01a918d2b65c">#</a></div> <div class="comment-content"> <p>First of all, thanks again for continuing to explore this matter. This was very enlightening, but in the end, I was left with is a sense of subtle wrongness, which is very hard to pin down, and even harder to tell apart between "this is actually not right for me" and "this is something new I'm not accustomed to".</p> <p>I suppose that my main question would center around difference between your tests for <code>IStateValidator</code> and <code>IRenderer</code>. Let's start with the latter:</p> <blockquote><p>Instead of configuring an <code>IRenderer</code> Stub, the test can state the expected output: That the output is equal to the output that <code>renderer.Success</code> would return.</p></blockquote> <p>Coupled with the explanation ("[the test] has less of an opinion about the implementation, which means that it's marginally less coupled to it"), this makes a lot of sense, with the only caveat that in more production-like cases comparing the output would be harder (e.g., if <code>IRenderer</code> operates on <code>HttpContext</code> to produce a full HTTP response), but that's a technicality that can be sorted out with proper assertion library. But let's now look at the <code>IStateValidator</code> part:</p> <blockquote><p>as far as executable specifications go, this test doesn't reflect reality. There's only one <code>Validate</code> implementation, and it doesn't behave like that. Rather, it'll return true when code is equal to <code>knownState.expectedCode</code>. The test poorly communicates that behaviour. </p></blockquote> <p>Here you act with the opposite intent: you want the test to communicate the specification, and thus be explicitly tied to the logic in the implementation (if not the actual code of it). There are two thing about that that bother me. First of all, it's somewhat inconsistent, so it makes it harder for me to choose which path to follow when testing the next code I'd write (or articulating to another developer how they should do it). But what's more important - and that comes from my example being <em>minimal</em>, as you've already noted - is that the validation logic might be more complicated, and thus the setup would be complicated as well. And as you've already mentioned on Twitter, when chaging the code in the validator implementation, you might be forced to change the implementation in the test, even if the test is more about the controller itself.</p> <p>There's also another frame for the same issue: the original test read as (at least for me): "if the state is valid, we return successful response based on this state". It didn't matter what is "valid" not did it matter what is "successful response". The new tests reads as "if state in the repository matches passed code, we return successful response for the state". It still doesn't matter what is "successful response", but the definition of validity <em>does</em> matter. For me, this is a change of test meaning, and I'm not sure I understand where that leads me.</p> <p>Let's consider the following scenario: we need to add another validity criteria, such as "state in repository has an expiration date, and this date should be in the future". We obviously need to add a couple of tests for this (negative and positive). Where do we add them? I'd say we add them into the tests for the validator itself (which are "not shown" for the purposes of brevity), but then it feels weird that we <em>also</em> need to change this "happy path" test...</p> </div> <div class="comment-date">2023-04-03 21:24 UTC</div> </div> <div class="comment" id="1a4c308c3b01a85c1b953d2051033b69"> <div class="comment-author"><a href="https://github.com/AnthonyLloyd">Anthony Lloyd</a> <a href="#1a4c308c3b01a85c1b953d2051033b69">#</a></div> <div class="comment-content"> <p>Thanks for showing CsCheck. I've put in a PR to show how I would refactor the CsCheck tests and will attempt to explain some of the design choices of CsCheck.</p> <p>First of all, it may be a personal opinion but I don't really tend to use query syntax for CsCheck. I prefer to see the SelectManys and there are a number of additional overloads that simplify ranging and composing Gens.</p> <p>On the design of CsCheck, I build it to not use reflection, attributes or target test frameworks. I've seen the very difficult problems these lead to (for author and user) when you try to move past simple examples.</p> <p>I wanted the user to be able to move from simple general type generators to ranged complex types easily in a fluent style. No Arbs, attributes, PositiveInt type etc.</p> <p>CsCheck has automatic shrinking even for the most complex types that just comes out of composing Gens.</p> <p>I think some of the reason it was so easy to extend the library to areas such as concurrency testing was because of this simplicity (as well as the random shrinking insight).</p> <pre><code style="background-color: #eee;border: 1px solid #999;display:block;padding:5px;">Gen&lt;Uri&gt; _genRedirect = Gen.OneOfConst(new Uri("https://example.com"), new Uri("https://example.org")); [Fact] public void HappyPath() { Gen.Select(Gen.String, Gen.String, Gen.Bool, _genRedirect) .Sample((state, expectedCode, isMobile, redirect) => { var code = expectedCode; var knownState = (expectedCode, isMobile, redirect); var repository = new RepositoryStub { { state, knownState } }; var sut = new Controller(repository); var actual = sut.Complete(state, code); var expected = Renderer.Success(knownState); return actual == expected; }); }</code></pre> </div> <div class="comment-date">2023-04-04 23:56 UTC</div> </div> <div class="comment" id="076f05eef3f649eab07392dc2ac83023"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#076f05eef3f649eab07392dc2ac83023">#</a></div> <div class="comment-content"> <p> Sergei, thank you for writing. I'm afraid all of this is context-dependent, and I seem to constantly fail giving enough context. It's a fair criticism that I seem to employ inconsistent heuristics when making technical decisions. Part of it is caused by my lack of context. The code base is deliberately stripped of context, which has many other benefits, but gives me little to navigate after. I'm flying blind, so to speak. I've had to (implicitly) imagine some forces acting on the software in order to make technical decisions. Since we haven't explicitly outlined such forces, I've had to make them up as I went. It's quite possible, even, that I've imagined one set of forces in one place, and another set somewhere else. If so, no wonder the decisions are inconsistent. </p> <p> What do I mean by <em>forces?</em> I'm thinking of the surrounding context that you have to take into a account when making technical decisions about code: Is the current feature urgent? Is it a bug already in production? Is this a new system with a small code base? Or is it an old system with a large code base? Do you have good automated tests? Do you have a Continuous Delivery pipeline? Are you experiencing problems with code quality? What does the team makeup look like? Do you have mostly seasoned veterans who've worked in that code base for years? Or do you have many newcomers? Is the system public-facing or internal? Is it a system, even, or a library or framework? What sort of organisation owns the software? Is it a product group? Or a cost centre? What is the organization's goals? How are you incentivized? How are other stakeholders incentivized? </p> <p> As you can imagine, I can keep going, asking questions like these, and they may all be relevant. Clearly, we can't expect a self-contained minimal example to also include all such superstructure, so that's what I (inconsistently) have to imagine, on the fly. </p> <p> I admit that the decisions I describe seem inconsistent, and the explanation may simply be what is already implied above: I may have had a different context in mind when I made one, and a variation in mind when I made the other. </p> <p> That's hardly the whole story, though. I didn't start my answer with the above litany of forces only to make a bad excuse for myself. Rather, what I had in mind was to argue that I use a wider context when making decisions. That context is not just technical, but includes, among many other considerations, the team structure. </p> <p> As an example, I was recently working with some students in a university setting. These are people in their early twenties, with only a few months of academic programming under their belt, as well as, perhaps, a few years of hobby programming. They'd just been introduced to Git and GitHub a few weeks earlier, C# a month before that. I was trying to teach them how to use Git and GitHub, how to structure decent C# code, and many other things. During our project, they did send me a few pull requests I would have immediately rejected from a professional programmer. In this particular context, however, that would have been counter-productive. These students were doing a good job, based on their level of experience, and they needed the sense of accomplishment that I would (often, but not always) accept their code. </p> <p> I could have insisted on a higher code quality, and I would also have been able to teach it to anyone patient enough to listen. One thing I've learned all too slowly in my decades of working with other people is that most people aren't as patient with me as I'd like them to be. I need to explicitly consider how to motivate my collaborators. </p> <p> Here's another example: Years ago, I worked with a rag-tag team hastily assembled via word-of-mouth of some fine European freelancers. My challenge here was another. These people were used to be on top of their game - usually the ones brought in to an organisation because they were the best. I needed them to work together, and among other things, it meant showing them that even though they might think that their way was the best way, other ways exist. I wanted them to be able to work together and produce code with shared ownership. At the beginning, I was rather strict with my standards, clearly bruising a few egos, but ultimately several members have told what a positive transformative experience it was for them. It was a positive transformative experience for me, too. </p> <p> I discuss all of this because you, among various points, mention the need to be able to articulate to other developers how to make technical decisions about tests. The point is that there's a lot of context that goes into making decisions, and hardly a one-size-fits-all heuristic. </p> <p> What <em>usually</em> guides me is an emphasis on <em>coupling</em>, and that's also, I believe, what ultimately motivated me here. There's always going to be <em>some</em> coupling between tests and production code, but the less the better. For example, when considering whether how to write an assertion, I consider whether a change in production code's behaviour would cause a test to break. </p> <p> Consider, for example, the <code>renderer</code> in the present example. How important is the exact output? What happens if I change a character in the string that is being returned? </p> <p> That's a good example of context being important. If that output is part of an implementation of a network protocol or some other technical spec, just one character change could, indeed, imply that your implementation is off spec. In that case, we do want to test the exact output, and we do want the test to fail if the output changes. </p> <p> On the other hand, if the output is a piece of UI, or perhaps an error message, then the exact wording is likely to change over time. Since this doesn't really imply a change in <em>behaviour</em>, changing such a string output shouldn't break a test. </p> <p> You need that wider context in order to make decisions like that: If we change the System Under Test in this way, will the test break? Should it? What if we change it in another way? </p> <p> This is relevant in order to address your final concern: What if you now decide that the expiration date should be in the future? The way you describe it, it sounds like this strengthens the preconditions of the system - in other words, it <a href="/2021/12/13/backwards-compatibility-as-a-profunctor">breaks backwards compatibility</a>. So yes, making that change may break existing tests, but this could be an indication that it's also going to break existing clients. </p> <p> <em>If</em> you have any clients, that is. Again, you know your context better than I do, so only you can decide whether making such a change is okay. I can think of situations where is, but I usually find myself in contexts where it isn't, so I tend to err on the side of avoiding breaking changes. </p> </div> <div class="comment-date">2023-04-09 14:28 UTC</div> </div> <div class="comment" id="f2fe826964404792954ddaa4155c7be6"> <div class="comment-author"><a href="https://github.com/srogovtsev">Sergei Rogovtcev</a> <a href="#f2fe826964404792954ddaa4155c7be6">#</a></div> <div class="comment-content"> <p>Mark, thank you for taking the time to discuss this.</p> <p>Having the magic word "architect" somewhere in my title, I know the importance of context, and in fact that would usually be the first counter-question that I have for somebody coming at me with a question: "what is your context?". So here we are, me being contractually obliged to strip as much context from my code as possible, and you having to reinvent it back from your experience. On the other hand, this allows us to point out which decisions are actually context-driven, and how different contexts affect them.</p> <p>With that in mind, I can actually propose two different contexts to reframe the decisions above, so that we could arrive at more insights.</p> <p>The first would be an imaginary context, which I had in mind when writing the code, but haven't thought of communicating: the <code>renderer</code> is as important as the <code>validator</code>. In case of "mobile" state the consumer is actually a mobile application, so we need to know we've produced the right JSON it will consume, and in case of non-"mobile" state the consumer is the browser, which again needs to be properly redirected. In my mind, this is no less important than the validation logic itself, because breaking it will break at least one consumer (mobile), and more likely both of them. Thus, according to the logic above, this is a compatibility issue, and as such we need to explicitly spell this behavior in the tests. Which gives us six outcome branches... six tests? Or something more complicated? This is especially interesting, considering the fact that we can test the <code>renderer</code> in isolation, so we'd be either duplicating our tests... or just just discarding the isolated tests for the <code>renderer</code>?</p> <p>And then here's the actual real context, which I can thankfully divulge to this extent: this is, in fact, a <em>migration</em> problem, when we move from one externally-facing framework (i.e. ASP.NET Web API) to another (i.e. ASP.NET Core). So I am not, in fact, concerned about the validation at all - I'm concerned about the data being properly <em>passed</em> to the validator (because the validator already existed, and worked properly, I'm just calling it from another framework), its result properly handled in the controller (which I am replacing), and then I'm concerned that despite the heavy changes between ASP.NET versions I'm still rendering the output in the exactly same way.</p> <p>Now that I'm thinking about it, it seems grossly unfair that I've hidden this context beforehand, but then, I didn't see how it was affecting the decisions in test design. So hopefully we can still find some use in this.</p> </div> <div class="comment-date">2023-04-10 18:03 UTC</div> </div> <div class="comment" id="82b07a802bd54507afb0aa90bfd827c8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#82b07a802bd54507afb0aa90bfd827c8">#</a></div> <div class="comment-content"> <p> Anthony, thank you for writing. I didn't intend to disparage CsCheck. Having once run <a href="https://github.com/AutoFixture/AutoFixture/">a moderately successful open source project</a>, I've learned that it's a good idea to limit the scope to a functional minimum. I'm not complaining about the design decisions made for CsCheck. </p> <p> I was a bit concerned that a casual reader might compare the CsCheck example with the previous code and be put off by the apparent complexity. I admit that your example looks less complex than mine, so to address that particular concern, I could have used code similar to yours. </p> <p> Whether one prefers the syntactic sugar of query syntax or the more explicit use of <code>SelectMany</code> is, to a degree, subjective. There are, on the other hand, some cases where one is objectively better than the other. One day I should write an article about that. </p> <p> I agree that the test-framework integration that exists in FsCheck is less than ideal. I'm not wishing for anything like that. Something declarative, however, would be nice. Contrary to you, I consider wrapper types like <code>PositiveInt</code> to be a boon, but perhaps not like they're implemented in FsCheck. (And this, by the way, isn't to harp on FsCheck either; I only bring up those examples because FsCheck has more of that kind of API than Hedgehog does.) The Haskell <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a> approach is nice: While a wrapper like <a href="https://hackage.haskell.org/package/QuickCheck/docs/Test-QuickCheck.html#t:Positive">Positive</a> is predefined in the package, it's just an <code>Arbitrary</code> instance. There's really nothing special about it, and you can easily define domain-specific wrappers for your own testing purposes. Here's an example: <a href="/2019/09/02/naming-newtypes-for-quickcheck-arbitraries">Naming newtypes for QuickCheck Arbitraries</a>. I'm wondering if something similar wouldn't be possible with interfaces in .NET. </p> </div> <div class="comment-date">2023-04-13 8:46 UTC</div> </div> <div class="comment" id="ee233e2d67954197afcaa98d7637fda1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ee233e2d67954197afcaa98d7637fda1">#</a></div> <div class="comment-content"> <p> Sergei, thank you for writing. I'm sorry if I came across as condescending or implying that you weren't aware that context matters. Of course you do. I was mostly trying to reconstruct my own decision process, which is far less explicit than you might like. </p> <p> Regarding the renderer component, I understand that testing such a thing in reality may be more involved than the toy example we're looking at. My first concern is to avoid duplicating efforts too much. Again, however, the external behaviour should be the primary concern. I'm increasingly <a href="/2021/03/01/pendulum-swing-internal-by-default">shifting towards making more and more things internal</a>, as long as I can still test them via the application boundary. As I understand it, this is the same concern that <a href="https://dannorth.net/introducing-bdd/">made Dan North come up with behaviour-driven development</a>. I'm also concerned primarily with testing the behaviour of systems, just without all the Gherkin overhead. </p> <p> There comes a time, however, where testing everything through an external API becomes too awkward. I'm not adverse to introduce or make public classes or functions to enable testing at a different abstraction level. Doing that, however, represents a bet that it's possible to keep the new component's API stable enough that it isn't going to cause too much test churn. Still, if we imagine that we've already made such a decision, and that we now have some renderer component, then it's only natural to test it thoroughly. Then, in order to avoid duplicating assertions, we can state, as I did in this article, that the overall system should expect to see whatever the renderer component returns. </p> <p> That was, perhaps, too wordy an explanation. Perhaps this is more helpful: Don't repeat yourself. What has been asserted in one place shouldn't be asserted in another place. </p> <p> The other example you mention, about migrating to another framework, reminds me of two things. </p> <p> The first is that we shouldn't forget about other ways of verifying code correctness. We've mostly been discussing black-box testing, and while it can be <a href="/2019/10/07/devils-advocate">an interesting exercise to imagine an adversary developer</a>, in general <a href="/2023/03/20/on-trust-in-software-development">that's rarely the reality</a>. Are there other ways to verify that methods are called with the correct values? How about <em>looking</em> at the code? Consistent code reviews are good at detecting bugs. </p> <p> The second observation is that if you already have two working components (validator and renderer) you can treat them as <a href="https://en.wikipedia.org/wiki/Test_oracle">Test Oracles</a>. This still works well with property-based testing. Write tests based on equivalence classes and get a property-based testing framework to sample from those classes. Then use the Test Oracles to define the expected output. That's essentially what I have done in this article. </p> <p> Does it <em>prove</em> that your framework-based code calls the components with the correct arguments? No, not like if you'd used a Test Spy. Property-based testing produces knowledge reminiscent of the kind of knowledge produced by experimental physics; not the kind of axiomatic knowledge produced by mathematics. That's why I named this article series <em>Epistemology of interaction testing.</em> </p> <p> Is it wrong to test with Stubs or Spies in a case like this? Not necessarily. Ultimately, what I try to do with this blog is to investigate and present alternatives. Only once we have alternatives do we have choices. </p> </div> <div class="comment-date">2023-04-15 15:53 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. More functional pits of success https://blog.ploeh.dk/2023/03/27/more-functional-pits-of-success 2023-03-27T05:36:00+00:00 Mark Seemann <div id="post"> <p> <em>FAQ: What are the other pits of successes of functional programming?</em> </p> <p> People who have seen my presentation <a href="https://youtu.be/US8QG9I1XW0">Functional architecture: the pits of success</a> occasionally write to ask: <em>What are the other pits?</em> </p> <p> The talk is about some of the design goals that we often struggle with in object-oriented programming, but which tend to happen automatically in functional programming (FP). In the presentation I cover three pits of success, but I also mention that there are more. In a one-hour conference presentation, I simply didn't have time to discuss more than three. </p> <p> It's a natural question, then, to ask what are the pits of success that I don't cover in the talk? </p> <p> I've been digging through my notes and found the following: </p> <ul> <li>Parallelism</li> <li>Ports and adapters</li> <li>Services, Entities, Value Objects</li> <li>Testability</li> <li>Composition</li> <li>Package and component principles</li> <li>CQS</li> <li>Encapsulation</li> </ul> <p> Finding a lost list like this, more than six years after I jotted it down, presents a bit of a puzzle to me, too. In this post, I'll see if I can reconstruct some of the points. </p> <h3 id="08cc3545b4324d0ab76e8f38d6367324"> Parallelism <a href="#08cc3545b4324d0ab76e8f38d6367324" title="permalink">#</a> </h3> <p> When most things are immutable you don't have to worry about multiple threads updating the same shared resource. Much has already been said and written about this in the context of functional programming, to the degree that for some people, it's the main (or only?) reason to adopt FP. </p> <p> Even so, I had (and still have) a version of the presentation that included this advantage. When I realised that I had to cut some content for time, it was easy to cut this topic in favour of other benefits. After all, this one was already well known in 2016. </p> <h3 id="243b258ac0ad4d8fa517bcbc7f2b5e7a"> Ports and adapters <a href="#243b258ac0ad4d8fa517bcbc7f2b5e7a" title="permalink">#</a> </h3> <p> This was one of the three benefits I kept in the talk. I've also covered it on my blog in the article <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">Functional architecture is Ports and Adapters</a>. </p> <h3 id="18060ca17da341148f9027a290f06652"> Services, Entities, Value Objects <a href="#18060ca17da341148f9027a290f06652" title="permalink">#</a> </h3> <p> This was the second topic I included in the talk. I don't think that there's an explicit article here on the blog that deals with this particular subject matter, so if you want the details, you'll have to view the recording. </p> <p> In short, though, what I had in mind was that <a href="/ref/ddd">Domain-Driven Design</a> explicitly distinguishes between Services, Entities, and Value Objects, which often seems to pull in the opposite direction of the object-oriented notion of <em>data with behaviour</em>. In FP, on the contrary, it's natural to separate data from behaviour. Since behaviour often implements business logic, and since business logic tends to change at a different rate than data, it's a good idea to keep them apart. </p> <h3 id="17d84bcee48a42a989760327b779bf59"> Testability <a href="#17d84bcee48a42a989760327b779bf59" title="permalink">#</a> </h3> <p> The third pit of success I covered in the talk was testability. I've also covered this here on the blog: <a href="/2015/05/07/functional-design-is-intrinsically-testable">Functional design is intrinsically testable</a>. <a href="https://en.wikipedia.org/wiki/Pure_function">Pure functions</a> are trivial to test: Supply some input and verify the output. </p> <h3 id="d2ac6cdd43794ade93ad664d72ed1ea1"> Composition <a href="#d2ac6cdd43794ade93ad664d72ed1ea1" title="permalink">#</a> </h3> <p> Pure functions compose. In the simplest case, use the return value from one function as input for another function. In more complex cases, you may need various combinators in order to be able to 'click' functions together. </p> <p> I don't have a single article about this. Rather, I have scores: <a href="/2017/10/04/from-design-patterns-to-category-theory">From design patterns to category theory</a>. </p> <h3 id="c3137baaeb50413e9caa4ebf2acde8e3"> Package and component principles <a href="#c3137baaeb50413e9caa4ebf2acde8e3" title="permalink">#</a> </h3> <p> When it comes to this one, I admit, I no longer remember what I had in mind. Perhaps I was thinking about <a href="https://fsharpforfunandprofit.com/posts/cycles-and-modularity-in-the-wild/">Scott Wlaschin's studies of cycles and modularity</a>. Perhaps I did, again, have <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">my article about Ports and Adapters</a> in mind, or perhaps it was my later articles on <a href="/2017/02/02/dependency-rejection">dependency rejection</a> that already stirred. </p> <h3 id="3554fe1eca684683bb793df1b1d23a87"> CQS <a href="#3554fe1eca684683bb793df1b1d23a87" title="permalink">#</a> </h3> <p> The <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> principle states that an operation (i.e. a method) should be either a Command or a Query, but not both. In most programming languages, the onus is on you to maintain that discipline. It's not a design principle that comes easy to most object-oriented programmers. </p> <p> Functional programming, on the other hand, emphasises pure functions, which are all Queries. Commands have side effects, and pure functions can't have side effects. <a href="https://www.haskell.org/">Haskell</a> even makes sure to type-check that pure functions don't perform side effects. If you're interested in a C# explanation of how that works, see <a href="/2020/06/08/the-io-container">The IO Container</a>. </p> <p> What impure actions remain after you've implemented most of your code base as pure functions can violate or follow CQS as you see fit. I usually still follow that principle, but since the impure parts of a functional code base tends to be fairly small and isolated to the edges of the application, even if you decide to violate CQS there, it probably makes little difference. </p> <h3 id="da267445bdae46bd97249acb0ea12246"> Encapsulation <a href="#da267445bdae46bd97249acb0ea12246" title="permalink">#</a> </h3> <p> Functional programmers don't talk much about <em>encapsulation</em>, but you'll often hear them say that we should <a href="https://blog.janestreet.com/effective-ml-video/">make illegal states unrepresentable</a>. I recently wrote an article that explains that this tends to originate from the same motivation: <a href="/2022/10/24/encapsulation-in-functional-programming">Encapsulation in Functional Programming</a>. </p> <p> In languages like <a href="https://fsharp.org/">F#</a> and Haskell, most type definitions require a single line of code, in contrast to object-oriented programming where types are normally classes, which take up a whole file each. This makes it much easier and succinct to define <a href="https://www.hillelwayne.com/post/constructive/">constructive data</a> in proper FP languages. </p> <p> Furthermore, perhaps most importantly, pure functions are <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>, and <a href="/2021/07/28/referential-transparency-fits-in-your-head">referential transparency fits in your head</a>. </p> <h3 id="f89e19457a344cfc9bd2c38e5d5c5f2b"> Conclusion <a href="#f89e19457a344cfc9bd2c38e5d5c5f2b" title="permalink">#</a> </h3> <p> In a recording of the talk titled <em>Functional architecture: the pits of success</em> I explain that the presentation only discusses three pits of success, but that there are more. Consulting my notes, I found five more that I didn't cover. I've now tried to remedy this lapse. </p> <p> I don't, however, believe that this list is exhaustive. Why should it be? </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. On trust in software development https://blog.ploeh.dk/2023/03/20/on-trust-in-software-development 2023-03-20T08:55:00+00:00 Mark Seemann <div id="post"> <p> <em>Can you trust your colleagues to write good code? Can you trust yourself?</em> </p> <p> I've recently noticed a trend among some agile thought leaders. They talk about <em>trust</em> and <em>gatekeeping</em>. It goes something like this: </p> <blockquote> <p> Why put up barriers to prevent people from committing code? Don't you trust your colleagues? </p> <p> Gated check-ins, pull requests, reviews are a sign of a dysfunctional organisation. </p> </blockquote> <p> I'm deliberately paraphrasing. While I could cite multiple examples, I wish to engage with the idea rather than the people who propose it. Thus, I apologise for the seeming use of <a href="https://en.wikipedia.org/wiki/Weasel_word">weasel words</a> in the above paragraph, but my agenda is the opposite of appealing to anonymous authority. </p> <p> If someone asks me: "Don't you trust your colleagues?", my answer is: </p> <p> No, I don't trust my colleagues, as I don't trust myself. </p> <h3 id="25501f07623946a1925a83af5b5198a3"> Framing <a href="#25501f07623946a1925a83af5b5198a3">#</a> </h3> <p> I don't trust myself to write defect-free code. I don't trust that I've always correctly understood the requirements. I don't trust that I've written the code in the best possible way. Why should I trust my colleagues to be superhumanly perfect? </p> <p> The <em>trust</em> framing is powerful because few people like to be labeled as <em>mistrusting</em>. When asked <em>"don't you trust your colleagues?"</em> you don't want to answer in the affirmative. You don't want to come across as suspicious or paranoid. You want to <em>belong</em>. </p> <p> <a href="https://en.wikipedia.org/wiki/Belongingness">The need to belong is fundamental to human nature</a>. When asked if you trust your colleagues, saying "no" implicitly disassociates you from the group. </p> <p> Sometimes the trust framing goes one step further and labels processes such as code reviews or pull requests as <em>gatekeeping</em>. This is still the same framing, but now turns the group dynamics around. Now the question isn't whether <em>you</em> belong, but whether you're excluding others from the group. Most people (me included) want to be nice people, and excluding other people is bullying. Since you don't want to be a bully, you don't want to be a gatekeeper. </p> <p> Framing a discussion about software engineering as one of trust and belonging is powerful and seductive. You're inclined to accept arguments made from that position, and you may not discover the sleight of hand. It's subliminal. </p> <p> Most likely, it's such a fundamental and subconscious part of human psychology that the thought leaders who make the argument don't realise what they are doing. Many of them are professionals that I highly respect; people with more merit, experience, and education than I have. I don't think they're deliberately trying to put one over you. </p> <p> I do think, on the other hand, that this is an argument to be challenged. </p> <h3 id="1a98f4b52b824194b23b5bb836efacb7"> Two kinds of trust <a href="#1a98f4b52b824194b23b5bb836efacb7">#</a> </h3> <p> On the surface, the trust framing seems to be about belonging, or its opposite, exclusion. It implies that if you don't trust your co-workers, you suspect them of malign intent. Organisational dysfunction, it follows, is a Hobbesian state of nature where everyone is out for themselves: Expect your colleague to be a back-stabbing liar out to get you. </p> <p> Indeed, the word <em>trust</em> implies that, too, but that's usually not the reason to introduce guardrails and checks to a software engineering process. </p> <p> Rather, another fundamental human characteristic is <em>fallibility</em>. We make mistakes in all sorts of way, and we don't make them from malign intent. We make them because we're human. </p> <p> Do we trust our colleagues to make no mistakes? Do we trust that our colleagues have perfect knowledge of requirement, goals, architecture, coding standards, and so on? I don't, just as I don't trust myself to have those qualities. </p> <p> This interpretation of <em>trust</em> is, I believe, better aligned with software engineering. If we institute formal sign-offs, code reviews, and other guardrails, it's not that we suspect co-workers of ill intent. Rather, we're trying to prevent mistakes. </p> <h3 id="020b7202b31d421baa9e612ca168f767"> Two wrongs... <a href="#020b7202b31d421baa9e612ca168f767">#</a> </h3> <p> That's not to say that all guardrails are necessary all of the time. The thought leaders I so vaguely refer to will often present alternatives: Pair programming instead of pull requests. Indeed, that can be an efficient and confidence-inducing way to work, in certain contexts. I describe advantages as well as disadvantages in my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> I've warned about the <em>trust framing</em>, but that doesn't mean that pull requests, code reviews, gated check-ins, or feature branches are always a good idea. Just because one argument is flawed it does't mean that the message is wrong. It could be correct for other reasons. </p> <p> I agree with the likes of <a href="https://martinfowler.com/">Martin Fowler</a> and <a href="https://www.davefarley.net/">Dave Farley</a> that feature branching is a bad idea, and that you should adopt <a href="https://en.wikipedia.org/wiki/Continuous_delivery">Continuous Delivery</a>. <a href="/ref/accelerate">Accelerate</a> strongly suggests that. </p> <p> I also agree that pull requests and formal reviews with sign-offs, <em>as they're usually practised</em>, is at odds with even <a href="https://en.wikipedia.org/wiki/Continuous_integration">Continuous Integration</a>. Again, be aware of common pitfalls in logic. Just because one way to do reviews is counter-productive, it doesn't follow that all reviews are bad. </p> <p> As I have outlined in another article, under the right circumstances, <a href="/2021/06/21/agile-pull-requests">agile pull requests</a> are possible. I've had good result with pull requests like that. Reviewing was everyone's job, and we integrated multiple times a day. </p> <p> Is that way to work always possible? Is it always the best way to work? No, of course not. Context matters. I've worked with another team where it was evident that that process had little chance of working. On the other hand, that team wasn't keen on pair programming either. Then what do you do? </p> <h3 id="1c3ff2a877214b76934d3817f24c1318"> Mistakes were made <a href="#1c3ff2a877214b76934d3817f24c1318">#</a> </h3> <p> I rarely have reason to believe that co-workers have malign intent. When we are working together towards a common goal, I trust that they have as much interest in reaching that goal as I have. </p> <p> Does that trust mean that everyone is free to do whatever they want? Of course not. Even with the best of intentions, we make mistakes, there are misunderstandings, or we have incomplete information. </p> <p> This is one among several reasons I practice test-driven development (TDD). Writing a test before implementation code catches many mistakes early in the process. In this context, the point is that I don't trust myself to be perfect. </p> <p> Even with TDD and the best of intentions, there are other reasons to look at other people's work. </p> <p> Last year, I did some freelance programming for a customer, and sometimes I would receive feedback that a function I'd included in a pull request already existed in the code base. I didn't have that knowledge, but the review caught it. </p> <p> Could we have caught that with pair or ensemble programming? Yes, that would work too. There's more than one way to make things work, and they tend to be context-dependent. </p> <p> If everyone on a team have the luxury of being able to work together, then pair or ensemble programming is an efficient way to coordinate work. Little extra process may be required, because everyone is already on the same page. </p> <p> If team members are less fortunate, or have different preferences, they may need to rely on the <a href="/2023/02/20/a-thought-on-workplace-flexibility-and-asynchrony">flexibility offered by asynchrony</a>. This doesn't mean that you can't do Continuous Delivery, even with pull requests and code reviews, but <a href="/2020/03/16/conways-law-latency-versus-throughput">the trade-offs are different</a>. </p> <h3 id="8cf2f27540de4461826bfe2b9ef88405"> Conclusion <a href="#8cf2f27540de4461826bfe2b9ef88405">#</a> </h3> <p> There are many good reasons to be critical of code reviews, pull requests, and other processes that seem to slow things down. The lack of trust in co-workers is, however, not one of them. </p> <p> You can easily be swayed by that argument because it touches something deep in our psyche. We want to be trusted, and we want to trust our colleagues. We want to <em>belong</em>. </p> <p> The argument is visceral, but it misrepresents the motivation for process. We don't review code because we believe that all co-workers are really North Korean agents looking to sneak in security breaches if we look away. </p> <p> We look at each other's work because it's human to make mistakes. If we can't all be in the same office at the same time, fast but asynchronous reviews also work. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f2faef409ab74134938a6ae77404ec43"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#f2faef409ab74134938a6ae77404ec43">#</a></div> <div class="comment-content"> <p> This reminds me of <a href="https://en.wikipedia.org/wiki/Hanlon%27s_razor">Hanlon's razor</a>. </p> <blockquote> Never attribute to malice that which is adequately explained by stupidity. </blockquote> </div> <div class="comment-date">2023-03-23 23:29 UTC</div> </div> <div class="comment" id="c46f7f22b22941f6a24858a40093424c"> <div class="comment-author"><a href="mailto:danielfrostdk@outlook.com">Daniel Frost</a> <a href="#c46f7f22b22941f6a24858a40093424c">#</a></div> <div class="comment-content"> <p> Too much technical attibution is still being presented on a whole, into a field which is far more of a social and psycological construct. What's even worse is the evidence around organisation and team capabilities is pretty clear towards what makes a good team and organisation. <br><br> The technical solutions for reaching trust or beloningness is somewhat a menas to an end. They can't stand alone because it only takes two humans to side track them. <br><br> therefore I still absolutely believe that the technical parts of software engineering is by far the less demanding. As technical work most often still are done as individual contributions based on loose requirements, equally loose leadership and often non-existing enforcement from tooling and scattered human ownership. Even worse subjective perceptions. That I believe on the other hand has everything to do with trust, gatekeeping, belonging and other human psycology needs. </p> </div> <div class="comment-date">2023-03-27 11:29 UTC</div> </div> <div class="comment" id="ebc68487f13d470a92260d91a890c5ad"> <div class="comment-author"><a href="https://www.linkedin.com/in/jakub-kwasniewski/">Jakub Kwaśniewski</a> <a href="#ebc68487f13d470a92260d91a890c5ad">#</a></div> <div class="comment-content"> <p> I like to look at it from the other side - I ask my colleagues for reviewing my code, because I trust them. I trust they can help me make it better and it's in all of us interests to make it better - as soon as the code gets in to the main branch, it's not my code anymore - it's teams code. </p> <p> Also, not tightly connected to the topic of code reviews and pull requests, but still about trust in software development, I had this general thought about trusting yourself in software programming: <em>Don't trust your past self. Do trust your future self.</em><br> Don't trust your past self in the meaning: don't hesitate to refactor your code. If you've written the code a while ago, even with the best intentions of making it readable, as you normally have, but now you read it and you don't understand it - don't make too much effort into trying to understand it. If you don't understand it, others won't as well. Rewrite it to make it better.<br> Do trust your future self in the meaning: don't try to be smarter than you are now. If you don't know all the requirements or answers to all the questions, don't guess them when you don't have to. Don't create an abstraction from one use case you have - in future you will have more use cases and you will be able to create a better abstraction - trust me, or actually trust yourself. I guess it's just YAGNI rephrased ;) </p> </div> <div class="comment-date">2023-05-16 21:22 UTC</div> </div> <div class="comment" id="b234787e00144b28ad897fc039820ef4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b234787e00144b28ad897fc039820ef4">#</a></div> <div class="comment-content"> <p> Jakub, thank you for writing. I don't think that I have much to add. More people should use refactoring as a tool for enhancing understanding when they run into incomprehensible code. Regardless of who originally wrote that code. </p> </div> <div class="comment-date">2023-06-13 7:36 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Confidence from Facade Tests https://blog.ploeh.dk/2023/03/13/confidence-from-facade-tests 2023-03-13T07:15:00+00:00 Mark Seemann <div id="post"> <p> <em>Recycling an old neologism of mine, I try to illustrate a point about the epistemology of testing function composition.</em> </p> <p> This article continues the introduction of a series on the <a href="/2023/02/13/epistemology-of-interaction-testing">epistemology of interaction testing</a>. In the first article, I attempted to explain how to test the composition of functions. Despite my best efforts, I felt that that article somehow fell short of its potential. Particularly, I felt that I ought to have been able to provide some illustrations. </p> <p> After publishing the first article, I finally found a way to illustrate what I'd been trying to communicate. That's this article. Better late than never. </p> <h3 id="0c1034fd05cb4b14a3e6ed985e98d6a3"> Previously, on epistemology of interaction testing <a href="#0c1034fd05cb4b14a3e6ed985e98d6a3">#</a> </h3> <p> A brief summary of the previous article may be in order. The question this article series tries to address is how to unit test composition of functions - particularly <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> <p> Consider the illustration from the previous article, repeated here for your convenience: </p> <p> <img src="/content/binary/component-graph.png" alt="Example component graph with four leaves."> </p> <p> When the leaves are pure functions they are <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. That's not the hard part, but how do we test the internal nodes or the root? </p> <p> While most people would reach for <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a> and <a href="http://xunitpatterns.com/Test%20Spy.html">Spies</a>, those kinds of <a href="https://martinfowler.com/bliki/TestDouble.html">Test Doubles</a> tend to <a href="/2022/10/17/stubs-and-mocks-break-encapsulation">break encapsulation</a>. </p> <p> What are the alternatives? </p> <p> An alternative I find useful is to test groups of functions composed together. Particularly when they are pure functions, you have no problem with non-deterministic behaviour. On the other hand, this approach seems to run afoul of the problem with combinatorial explosion of integration testing <a href="https://www.infoq.com/presentations/integration-tests-scam/">so eloquently explained by J.B. Rainsberger</a>. </p> <p> What I suggest, however, isn't quite integration testing. </p> <h3 id="bec86a649bfb420abbf0a333dd663b57"> Neologism <a href="#bec86a649bfb420abbf0a333dd663b57">#</a> </h3> <p> If it isn't integration testing, then what is it? What do we call it? </p> <p> I'm going to resurrect and recycle an old term of mine: <a href="/2012/06/27/FacadeTest">Facade Tests</a>. Ten years ago I had a more narrow view of a term like 'unit test' than I do today, but the overall idea seems apt in this new context. A Facade Test is a test that exercises a <a href="https://en.wikipedia.org/wiki/Facade_pattern">Facade</a>. </p> <p> These days, I don't find it productive to distinguish narrowly between different kinds of tests. At least not to the the degree that I wish to fight over terminology. On the other hand, occasionally it's useful to have a name for a thing, in order to be able to differentiate it from some other thing. </p> <p> The term <em>Facade Tests</em> is my attempt at a <a href="https://martinfowler.com/bliki/Neologism.html">neologism</a>. I hope it helps. </p> <h3 id="11713bd3f4bc48d8a9c82b3f955066ad"> Code coverage as a proxy for confidence <a href="#11713bd3f4bc48d8a9c82b3f955066ad">#</a> </h3> <p> The question I'm trying to address is how to test functions that compose other functions - the internal nodes or the root in the above graph. As I tried to explain in the previous article, you need to build confidence that various parts of the composition work. How do you gain confidence in the leaves? </p> <p> One way is to test each leaf individually. </p> <p> <img src="/content/binary/test-and-leaf-no-test.png" alt="A single leaf node and a test module pointing to it."> </p> <p> The first test or two may exercise a tiny slice of the System Under Test (SUT): </p> <p> <img src="/content/binary/test-and-leaf-one-test.png" alt="A single leaf node with a thin slice of space filled in green, driven by a test."> </p> <p> The next few tests may exercise another part of the SUT: </p> <p> <img src="/content/binary/test-and-leaf-two-tests.png" alt="A single leaf node with two thin slices of space filled in green, driven by tests."> </p> <p> Keep adding more tests: </p> <p> <img src="/content/binary/test-and-leaf-several-tests.png" alt="A single leaf node with a triangle of space filled in green, driven by tests."> </p> <p> Stop when you have good confidence that the SUT works as intended: </p> <p> <img src="/content/binary/test-and-leaf-full-coverage.png" alt="A single leaf node fully filled in green, indicating full code coverage by tests."> </p> <p> If you're now thinking of code coverage, I can't blame you. To be clear, I haven't changed my position about code coverage. <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">Code coverage is a useless target measure</a>. On the other hand, there's no harm in having a high degree of code coverage. It still might give you confidence that the SUT works as intended. </p> <p> You may think of the amount of green in the above diagrams as a proxy for confidence. The more green, the more confident you are in the SUT. </p> <p> None of the arguments here hinge on <em>code coverage</em> per se. What matters is confidence. </p> <h3 id="1b6d2872c49c4f22aa3f84e541989be7"> Facade testing confidence <a href="#1b6d2872c49c4f22aa3f84e541989be7">#</a> </h3> <p> With all the leaves covered, you can move on to the internal nodes. This is the actual problem that I'm trying to address. We would like to test an internal node, but it has dependencies. Fortunately, the context of this article is that the dependencies are pure functions, so we don't have a problem with non-deterministic behaviour. No need for Test Doubles. </p> <p> It's really simple, then. Just test the internal node until you're confident that it works: </p> <p> <img src="/content/binary/test-and-internal-node-full-coverage.png" alt="An internal node fully filled in blue, indicating full code coverage by tests. Dependencies are depicted as boxes below the internal node, but with only slivers of coverage."> </p> <p> The goal is to build confidence in the internal node, the new SUT. While it has dependencies, covering those with tests is no longer the goal. This is the key difference between Facade Testing and Integration Testing. You're not trying to cover all combinations of code paths in the integrated set of components. You're still just trying to test the new SUT. </p> <p> Whether or not these tests exercise the leaves is irrelevant. The leaves are already covered by other tests. What 'coverage' you get of the leaves is incidental. </p> <p> Once you've built confidence in internal nodes, you can repeat the process with the root node: </p> <p> <img src="/content/binary/test-and-root-full-coverage.png" alt="A root node fully filled in blue, indicating full code coverage by tests. Dependencies are depicted as trees below the root node, but with only slivers of coverage."> </p> <p> The test covers enough of the root node to give you confidence in it. Some of the dependencies are also partially exercised by the tests, but this is still secondary. The way I've drawn the diagram, the left internal node is exercised in such a way that <em>its</em> dependencies (the leaves) are partially exercised. The test apparently also exercises the right internal node, but none of that activity makes it interact with the leaves. </p> <p> These aren't integration tests, so they avoid the problem of combinatorial explosion. </p> <h3 id="8eeade5dc4f0433cbb2506dc50973e48"> Conclusion <a href="#8eeade5dc4f0433cbb2506dc50973e48">#</a> </h3> <p> This article was an attempt to illustrate the prose in the previous article. You can unit test functions that compose other functions by first unit testing the leaf functions and then the compositions. While these tests exercise an 'integration' of components, the purpose is <em>not</em> to test the integration. Thus, they aren't integration tests. They're facade tests. </p> <p> <strong>Next:</strong> <a href="/2023/04/03/an-abstract-example-of-refactoring-from-interaction-based-to-property-based-testing">An abstract example of refactoring from interaction-based to property-based testing</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="3d4420c5b9f9425384a480be778993db"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#3d4420c5b9f9425384a480be778993db">#</a></div> <div class="comment-content"> <p>I really appreciate that you are writing about testing compositions of pure functions. As an F# dev who tries to adhere to the <a href='https://blog.ploeh.dk/2020/03/02/impureim-sandwich/'>impureim sandwich</a> (which, indeed, you <a href='https://blog.ploeh.dk/2022/02/14/a-conditional-sandwich-example/'>helped me with before</a>), this is something I have also been struggling with, and failing to find good answers to.</p> <p><strong>But following your suggestion, aren’t we testing implementation details?</strong></p> <p>Using the terminology in this article, I often have a root function that is public, which composes and delegates work to private helper functions. Compared to having all the logic directly in the root function, the code is, unsurprisingly, easier to read and maintain this way. However, all the private helper functions (internal nodes and leaves) as well as the particularities of how the root and the internal nodes compose their “children”, are very much just implementation details of the root function.</p> <p>I occasionally need to change such code in a way that does not change the public API (at least not significantly enough to cause excessive test maintenance), but which significantly restructures the internal helpers. If I were to test as suggested in this article, I would have many broken tests on my hands. These would be tests of the internal nodes and leaves (which may not exist at all after the refactor, having been replaced with completely different functions) as well as tests of how the root node composes the other functions (which, presumably, would still pass but may not actually test anything useful anymore).</p> <p>In short, testing in the way suggested here would act as a force to avoid refactoring, which seems counter-productive.</p> <p>One would also need to use <code>InternalsVisibleTo</code> or similar in order to test those helpers. I’m not very concerned about that on its own (though I’d like to keep the helpers <code>private</code>), but it always smells of testing implementation details, which, as I argue, is what I think we’re doing. (One could alternatively make the helpers public – they’re pure, after all, so presumably no harm done – but that would expose a public API that no-one should actually use, and doesn’t avoid the main problem anyway.)</p> <p>As a motivating example from my world, consider a system for sending email notifications. The root function accepts a list of notifications that should be sent, together with any auxiliary data (names and other data from all users referenced by the notifications; translated strings; any environment-specific data such as base URLs for links; etc.), and returns the email HTML (or at least a structure that maps trivially to HTML). In doing this, the code has to group notifications in several levels, sort them in various ways, merge similar consecutive notifications in non-trivial ways, hide notifications that the user has not asked to receive (but which must still be passed to the root function since they are needed for other logic), and so on. All in all, I have almost 600 lines of pure code that does this. (In addition, I have 150 lines that fetches everything from the DB and creates necessary lookup maps of auxiliary data to pass to the root function. I consider this code “too boring to fail”.)</p> <p>The pure part of the code was recently significantly revamped. Had I had tests for private/internal helpers, the refactor would likely have been much more painful.</p> <p>I expect there is no perfect way to make the code both testable and easy to refactor. But I am still eager to hear your thoughts on my concern: <strong>Following your suggestion, aren’t we testing implementation details?</strong></p> </div> <div class="comment-date">2023-03-13 14:00 UTC</div> </div> <div class="comment" id="af0abf6dad214bfbb452488ca09c8a27"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#af0abf6dad214bfbb452488ca09c8a27">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. The short answer is: <em>yes</em>. </p> <p> Isn't this a separate problem, though? If you're using Stubs and Spies to test interaction, and other tests to verify your implementations, then isn't that a similar problem? </p> <p> I'm going to graze this topic in the future article in this series tentatively titled <em>Refactoring pure function composition without breaking existing tests</em>, but I should probably write another article more specifically about this topic... </p> </div> <div class="comment-date">2023-03-14 9:06 UTC</div> </div> <div class="comment" id="ccd772ca4c0946adbd7500d928eda8e0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ccd772ca4c0946adbd7500d928eda8e0">#</a></div> <div class="comment-content"> <p> Christer (and everyone who may be interested), I've long been wanting to expand on my <a href="#af0abf6dad214bfbb452488ca09c8a27">previous answer</a>, and I finally found the time to write an <a href="/2023/06/19/when-is-an-implementation-detail-an-implementation-detail">article that discusses the implementation-detail question</a>. </p> <p> Apart from that I also want to remind readers that the article <a href="/2023/05/01/refactoring-pure-function-composition-without-breaking-existing-tests">Refactoring pure function composition without breaking existing tests</a> has been available since May 1st, 2023. It shows one example of using the <a href="https://martinfowler.com/bliki/StranglerFigApplication.html">Strangler pattern</a> to refactor pure Facade Tests without breaking them. </p> <p> This doesn't imply that one should irresponsibly make every pure function public. These days, I make things <a href="/2021/03/01/pendulum-swing-internal-by-default">internal by default</a>, but make them public if I think they'd be good <a href="http://wiki.c2.com/?SoftwareSeam">seams</a>. Particularly when following test-driven development, it's possible to <a href="/2021/09/13/unit-testing-private-helper-methods">unit test private helpers</a> via a <code>public</code> API. This does, indeed, have the benefit that you're free to refactor those helpers without impacting test code. </p> <p> The point of this article series isn't that you <em>should</em> make pure functions public and test interactions with property-based testing. The point is that if you <em>already</em> have pure functions and you wish to test how they interact, then property-based testing is a good way to achieve that goal. </p> <p> If, on the other hand, you have a pure function for composing emails, and you can keep all helper functions <code>private</code>, <em>still</em> <a href="/2018/11/12/what-to-test-and-not-to-test">cover it enough to be confident that it works</a>, and do that by only exercising a single <code>public</code> root function (mail-slot testing) then that's preferable. That's what I would aim for as well. </p> </div> <div class="comment-date">2023-06-19 7:04 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Warnings-as-errors friction https://blog.ploeh.dk/2023/03/06/warnings-as-errors-friction 2023-03-06T07:17:00+00:00 Mark Seemann <div id="post"> <p> <em>TDD friction. Surely that's a bad thing(?)</em> </p> <p> <a href="https://furlough.merecomplexities.com/">Paul Wilson</a> recently wrote on Mastodon: </p> <blockquote> <p> Software development opinion (warnings as errors) </p> <p> Just seen this via Elixir Radar, <a href="https://curiosum.com/til/warnings-as-errors-elixir-mix-compile">https://curiosum.com/til/warnings-as-errors-elixir-mix-compile</a> on on treating warnings as errors, and yeah don't integrate code with warnings. But .... </p> <p> Having worked on projects with this switched on in dev, it's an annoying bit of friction when Test Driving code. Yes, have it switched on in CI, but don't make me fix all the warnings before I can run my failing test. </p> <p> (Using an env variable for the switch is a good compromise here, imo). </p> <footer><cite><a href="https://mastodon.social/@paulwilson/109433259807695275">Paul Wilson</a></cite></footer> </blockquote> <p> This made me reflect on similar experiences I've had. I thought perhaps I should write them down. </p> <p> To be clear, <em>this article is not an attack on Paul Wilson</em>. He's right, but since he got me thinking, I only find it honest and respectful to acknowledge that. </p> <p> The remark does, I think, invite more reflection. </p> <h3 id="512ad9e57e7b4c3c849cfc5e78c8b977"> Test friction example <a href="#512ad9e57e7b4c3c849cfc5e78c8b977" title="permalink">#</a> </h3> <p> <a href="http://www.exampler.com/">An example would be handy right about now</a>. </p> <p> As I was writing the example code base for <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, I was following the advice of the book: </p> <ul> <li>Turn on <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/nullable-reference-types">Nullable reference types</a> (only relevant for C#)</li> <li>Turn on static code analysis or <a href="https://en.wikipedia.org/wiki/Lint_(software)">linters</a></li> <li>Treat warnings as errors. Yes, also the warnings produced by the two above steps</li> </ul> <p> As Paul Wilson points out, this tends to create friction with test-driven development (TDD). When I started the code base, this was the first TDD test I wrote: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;PostValidReservation() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;response&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;PostReservation(<span style="color:blue;">new</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2023-03-10&nbsp;19:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;katinka@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Katinka&nbsp;Ingabogovinanana&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity&nbsp;=&nbsp;2&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;Assert.True( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{response.StatusCode}<span style="color:#a31515;">.&quot;</span>); }</pre> </p> <p> Looks good so far, doesn't it? Are any of the warnings-as-errors settings causing friction? Not directly, but now regard the <code>PostReservation</code> helper method: </p> <p> <pre>[SuppressMessage( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Usage&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;CA2234:Pass&nbsp;system&nbsp;uri&nbsp;objects&nbsp;instead&nbsp;of&nbsp;strings&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;Justification&nbsp;=&nbsp;<span style="color:#a31515;">&quot;URL&nbsp;isn&#39;t&nbsp;passed&nbsp;as&nbsp;variable,&nbsp;but&nbsp;as&nbsp;literal.&quot;</span>)] <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;HttpResponseMessage&gt;&nbsp;PostReservation( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;factory&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;WebApplicationFactory&lt;Startup&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;client&nbsp;=&nbsp;factory.CreateClient(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;json&nbsp;=&nbsp;JsonSerializer.Serialize(reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;content&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;StringContent(json); &nbsp;&nbsp;&nbsp;&nbsp;content.Headers.ContentType.MediaType&nbsp;=&nbsp;<span style="color:#a31515;">&quot;application/json&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;client.PostAsync(<span style="color:#a31515;">&quot;reservations&quot;</span>,&nbsp;content); }</pre> </p> <p> Notice the <code>[SuppressMessage]</code> attribute. Without it, the compiler emits this error: </p> <blockquote> error CA2234: Modify 'ReservationsTests.PostReservation(object)' to call 'HttpClient.PostAsync(Uri, HttpContent)' instead of 'HttpClient.PostAsync(string, HttpContent)'. </blockquote> <p> That's an example of friction in TDD. I could have fixed the problem by changing the last line to: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;client.PostAsync(<span style="color:blue;">new</span>&nbsp;Uri(<span style="color:#a31515;">&quot;reservations&quot;</span>,&nbsp;UriKind.Relative),&nbsp;content);</pre> </p> <p> This makes the actual code more obscure, which is the reason I didn't like that option. Instead, I chose to add the <code>[SuppressMessage]</code> attribute and write a <code>Justification</code>. It is, perhaps, not much of an explanation, but my position is that, in general, I consider <a href="https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2234">CA2234</a> a good and proper rule. It's a specific example of favouring stronger types over <a href="https://blog.codinghorror.com/new-programming-jargon/">stringly typed code</a>. I'm <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">all for it</a>. </p> <p> If you <a href="/ref/stranger-in-a-strange-land">grok</a> the motivation for the rule (which, evidently, the <a href="https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2234">documentation</a> code-example writer didn't) you also know when to safely ignore it. Types are useful because they enable you to encapsulate knowledge and guarantees about data in a way that strings and ints typically don't. Indeed, if you are passing URLs around, pass them around as <a href="https://learn.microsoft.com/dotnet/api/system.uri">Uri</a> objects rather than strings. This prevents simple bugs, such as accidentally swapping the place of two variables because they're both strings. </p> <p> In the above example, however, a URL isn't being passed around as a variable. <em>The value is hard-coded right there in the code.</em> Wrapping it in a <code>Uri</code> object doesn't change that. </p> <p> But I digress... </p> <p> This is an example of friction in TDD. Instead of being able to just plough through, I had to stop and deal with a Code Analysis rule. </p> <h3 id="3265182ae4114b9da00d91fe0763f36e"> SUT friction example <a href="#3265182ae4114b9da00d91fe0763f36e" title="permalink">#</a> </h3> <p> But wait! There's more. </p> <p> To pass the test, I had to add this class: </p> <p> <pre>&nbsp;&nbsp;&nbsp;&nbsp;[Route(<span style="color:#a31515;">&quot;[controller]&quot;</span>)] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span> &nbsp;&nbsp;&nbsp;&nbsp;{ <span style="color:gray;">#pragma</span>&nbsp;<span style="color:gray;">warning</span>&nbsp;<span style="color:gray;">disable</span>&nbsp;CA1822&nbsp;<span style="color:green;">//&nbsp;Mark&nbsp;members&nbsp;as&nbsp;static</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Post()&nbsp;{&nbsp;} <span style="color:gray;">#pragma</span>&nbsp;<span style="color:gray;">warning</span>&nbsp;<span style="color:gray;">restore</span>&nbsp;CA1822&nbsp;<span style="color:green;">//&nbsp;Mark&nbsp;members&nbsp;as&nbsp;static</span> &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> I had to suppress <a href="https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822">CA1822</a> as well, because it generated this error: </p> <blockquote> error CA1822: Member Post does not access instance data and can be marked as static (Shared in VisualBasic) </blockquote> <p> Keep in mind that because of my settings, it's an <em>error</em>. The code doesn't compile. </p> <p> You can try to fix it by making the method <code>static</code>, but this then triggers another error: </p> <blockquote> error CA1052: Type 'ReservationsController' is a static holder type but is neither static nor NotInheritable </blockquote> <p> In other words, the class should be static as well: </p> <p> <pre>[Route(<span style="color:#a31515;">&quot;[controller]&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Post()&nbsp;{&nbsp;} }</pre> </p> <p> This compiles. What's not to like? Those Code Analysis rules are there for a reason, aren't they? Yes, but they are general rules that can't predict every corner case. While the code compiles, the test fails. </p> <p> Out of the box, that's just not how that version of ASP.NET works. The MVC model of ASP.NET expects <em>action methods</em> to be instance members. </p> <p> (I'm sure that there's a way to tweak ASP.NET so that it allows static HTTP handlers as well, but I wasn't interested in researching that option. After all, the above code only represents an interim stage during a longer TDD session. Subsequent tests would prompt me to give the <code>Post</code> method some proper behaviour that would make it an instance method anyway.) </p> <p> So I kept the method as an instance method and suppressed the Code Analysis rule. </p> <p> Friction? Demonstrably. </p> <h3 id="06c8473479aa404486a88a99fd90cea9"> Opt in <a href="#06c8473479aa404486a88a99fd90cea9" title="permalink">#</a> </h3> <p> Is there a way to avoid the friction? Paul Wilson mentions a couple of options: Using an environment variable, or only turning warnings into errors in your deployment pipeline. A variation on using an environment variable is to only turn on errors for Release builds (for languages where that distinction exists). </p> <p> In general, if you have a useful tool that unfortunately takes a long time to run, making it a scheduled or opt-in tool may be the way to go. A <a href="https://en.wikipedia.org/wiki/Mutation_testing">mutation testing</a> tool like <a href="https://stryker-mutator.io/">Stryker</a> can easily run for hours, so it's not something you want to do for every change you make. </p> <p> Another example is dependency analysis. One of my recent clients had a tool that scanned their code dependencies (NuGet, npm) for versions with known vulnerabilities. This tool would also take its time before delivering a verdict. </p> <p> Making tools opt-in is definitely an option. </p> <p> You may be concerned that this requires discipline that perhaps not all developers have. If a tool is opt-in, will anyone remember to run it? </p> <p> As I also describe in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, you could address that issue with a checklist. </p> <p> Yeah, but <em>do we then need a checklist to remind us to look at the checklist?</em> Right, <a href="https://en.wikipedia.org/wiki/Quis_custodiet_ipsos_custodes%3F">quis custodiet ipsos custodes?</a> Is it going to be <a href="https://en.wikipedia.org/wiki/Turtles_all_the_way_down">turtles all the way down</a>? </p> <p> Well, if no-one in your organisation can be trusted to follow <em>any</em> commonly-agreed-on rules on a regular basis, you're in trouble anyway. </p> <h3 id="cad76f35e04f46b9b2703759c03aa3eb"> Good friction? <a href="#cad76f35e04f46b9b2703759c03aa3eb" title="permalink">#</a> </h3> <p> So far, I've spent some time describing the problem. When encountering resistance your natural reaction is to find it disagreeable. You want to accomplish something, and then this rule/technique/tool gets in the way! </p> <p> Despite this, is it possible that this particular kind of friction is beneficial? </p> <p> By (subconsciously, I'm sure) picking a word like 'friction', you've already chosen sides. That word, in general, has a negative connotation. Is it the only word that describes the situation? What if we talked about it instead in terms of safety, assistance, or predictability? </p> <p> Ironically, <em>friction</em> was a main complaint about TDD when it was first introduced. </p> <p> <em>"What do you mean? I have to write a test</em> before <em>I write the implementation? That's going to slow me down!"</em> </p> <p> The TDD and agile movement developed a whole set of standard responses to such objections. <em>Brakes enable you to go faster. If it hurts, do it more often.</em> </p> <p> Try those on for size, only now applied to warnings as errors. Friction is what makes brakes work. </p> <h3 id="456884d28f9b484f961f0b142993e042"> Additive mindset <a href="#456884d28f9b484f961f0b142993e042" title="permalink">#</a> </h3> <p> As I age, I'm becoming increasingly aware of a tendency in the software industry. Let's call it the <em>additive mindset</em>. </p> <p> It's a reflex to consider <em>addition</em> a good thing. An API with a wide array of options is better than a narrow API. Software with more features is better than software with few features. More log data provides better insight. </p> <p> More code is better than less code. </p> <p> Obviously, that's not true, but we. keep. behaving as though it is. Just look at the recent hubbub about <a href="https://openai.com/blog/chatgpt/">ChatGPT</a>, or <a href="https://github.com/features/copilot">GitHub Copilot</a>, which I <a href="/2022/12/05/github-copilot-preliminary-experience-report">recently wrote about</a>. Everyone reflexively view them as productivity tools because the can help us produce more code faster. </p> <p> I had a cup of coffee with my wife as I took a break from writing this article, and I told her about it. Her immediate reaction when told about friction is that it's a benefit. She's a doctor, and naturally view procedure, practice, regulation, etcetera as occasionally annoying, but essential to the practice of medicine. Without procedures, patients would die from preventable mistakes and doctors would prescribe morphine to themselves. Checking boxes and signing off on decisions slow you down, and that's half the point. Making you slow down can give you the opportunity to realise that you're about to do something stupid. </p> <blockquote> <p> Worried that TDD will slow down your programmers? Don't. They probably need slowing down. </p> <footer><cite><a href="https://twitter.com/jbrains/status/167297606698008576">J. B. Rainsberger</a></cite></footer> </blockquote> <p> But if TDD is already being touted as a process to make us slow down and think, is it a good idea, then, to slow down TDD with warnings as errors? Are we not interfering with a beneficial and essential process? </p> <h3 id="efed5514b66e4da5ab36690d3288ca84"> Alternatives to TDD <a href="#efed5514b66e4da5ab36690d3288ca84" title="permalink">#</a> </h3> <p> I don't have a confident answer to that question. What follows is tentative. I've been doing TDD since 2003 and while I was also an <a href="/2010/12/22/TheTDDApostate">early critic</a>, it's still central to how I write code. </p> <p> When I began doing TDD with all the errors <a href="https://en.wikipedia.org/wiki/Up_to_eleven">dialled to 11</a> I was concerned about the friction, too. While I also believe in linters, the two seem to work at cross purposes. The rule about static members in the above example seems clearly counterproductive. After all, a few commits later I'd written enough code for the <code>Post</code> method that it <em>had</em> to be an instance method after all. The degenerate state was temporary, an artefact of the TDD process, but the rule triggered anyway. </p> <p> What should I think of that? </p> <p> I don't <em>like</em> having to deal with such <a href="https://en.wikipedia.org/wiki/False_positives_and_false_negatives">false positives</a>. The question is whether treating warnings as errors is a net positive or a net negative? </p> <p> It may help to recall why TDD is a useful practice. A major reason is that it provides rapid feedback. There are, however, <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">other ways to produce rapid feedback</a>. Static types, compiler warnings, and static code analysis are other ways. </p> <p> I don't think of these as alternatives to TDD, but rather as complementary. Tests can produce feedback about some implementation details. <a href="https://www.hillelwayne.com/post/constructive/">Constructive data</a> is another option. Compiler warnings and linters enter that mix as well. </p> <p> Here I again speak with some hesitation, but it looks to me as though the TDD practice originated in dynamically typed tradition (<a href="https://en.wikipedia.org/wiki/Smalltalk">Smalltalk</a>), and even though some Java programmers were early adopters as well, from my perspective it's always looked stronger among the dynamic languages than the compiled languages. The unadulterated TDD tradition still seems to largely ignore the existence of other forms of feedback. Everything must be tested. </p> <p> At the risk of repeating myself, I find TDD invaluable, but I'm happy to receive rapid feedback from heterogeneous sources: Tests, type checkers, compilers, linters, fellow ensemble programmers. </p> <p> This suggests that TDD isn't the only game in town. This may also imply that the friction to TDD caused by treating warnings as errors may not be as costly as first perceived. After all, slowing down something that you rely on 75% of the time isn't quite as bad as slowing down something you rely on 100% of the time. </p> <p> While it's a cost, perhaps it went down... </p> <h3 id="235a1aa10437465b945242f8b6a29873"> Simplicity <a href="#235a1aa10437465b945242f8b6a29873" title="permalink">#</a> </h3> <p> As always, circumstances matter. Is it always a good idea to treat warnings as errors? </p> <p> Not really. To be honest, treating warnings as errors is another case of treating a symptom. The reason I recommend it is that I've seen enough code bases where compiler warnings (not errors) have accumulated. In a setting where that happens, treating (new) warnings as errors can help get the situation under control. </p> <p> When I work alone, I don't allow warnings to build up. I rarely tell the compiler to treat warnings as errors in my personal code bases. There's no need. I have zero tolerance for compiler warnings, and I do spot them. </p> <p> If you have a team that never allows compiler warnings to accumulate, is there any reason to treat them as errors? Probably not. </p> <p> This underlines an important point about productivity: A good team without strict process can outperform a poor team with a clearly defined process. Mindset beats tooling. Sometimes. </p> <p> Which mindset is that? Not the additive mindset. Rather, I believe in focusing on simplicity. The alternative to adding things isn't to blindly remove things. You can't add features to a program <em>only</em> by deleting code. Rather, add code, but keep it simple. <a href="/2022/11/21/decouple-to-delete">Decouple to delete</a>. </p> <blockquote> <p> perfection is attained not when there is nothing more to add, but when there is nothing more to remove. </p> <footer><cite>Antoine de Saint Exupéry, <a href="/ref/wind-sand-stars">Wind, Sand And Stars</a></cite></footer> </blockquote> <p> Simple code. Simple tests. Be warned, however, that code simplicity does not imply naive code understandable by everyone. I'll refer you to <a href="https://en.wikipedia.org/wiki/Rich_Hickey">Rich Hickey</a>'s wonderful talk <a href="https://www.infoq.com/presentations/Simple-Made-Easy/">Simple Made Easy</a> and remind you that this was the line of thinking that lead to <a href="https://clojure.org/">Clojure</a>. </p> <p> Along the same lines, I tend to consider <a href="https://www.haskell.org/">Haskell</a> to be a vehicle for expressing my thoughts in a <em>simpler</em> way than I can do in <a href="https://fsharp.org/">F#</a>, which again enables simplicity not available in C#. Simpler, not easier. </p> <h3 id="eb224aa293984475bf9b105e191b362c"> Conclusion <a href="#eb224aa293984475bf9b105e191b362c" title="permalink">#</a> </h3> <p> Does treating warnings as errors imply TDD friction? It certainly looks that way. </p> <p> Is it worth it, nonetheless? Possibly. It depends on why you need to turn warnings into errors in the first place. In some settings, the benefits of treating warnings as errors may be greater than the cost. If that's the only way you can keep compiler warnings down, then do treat warnings as errors. Such a situation, however, is likely to be a symptom of a more fundamental mindset problem. </p> <p> This almost sounds like a moral judgement, I realise, but that's not my intent. Mindset is shaped by personal preference, but also by organisational and peer pressure, as well as knowledge. If you only know of one way to achieve a goal, you have no choice. Only if you know of more than one way can you choose. </p> <p> Choose the way that leaves the code simpler than the other. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Test Data Generator monad https://blog.ploeh.dk/2023/02/27/test-data-generator-monad 2023-02-27T07:10:00+00:00 Mark Seemann <div id="post"> <p> <em>With examples in C# and F#.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. In other related series previous articles described <a href="/2017/09/18/the-test-data-generator-functor">Test Data Generator as a functor</a>, as well as <a href="/2018/11/26/the-test-data-generator-applicative-functor">Test Data Generator as an applicative functor</a>. As is the case with many (but not all) <a href="/2018/03/22/functors">functors</a>, this one also forms a monad. </p> <p> This article expands on the code from the above-mentioned articles about Test Data Generators. Keep in mind that the code is a simplified version of what you'll find in a real property-based testing framework. It lacks shrinking and referentially transparent (pseudo-)random value generation. Probably more things than that, too. </p> <h3 id="0855f8f4840c4efc96f102e3913e7357"> SelectMany <a href="#0855f8f4840c4efc96f102e3913e7357">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function. In C#, monadic bind is called <code>SelectMany</code>. For the <code><span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class, you can implement it as an instance method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Generator&lt;TResult&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(Func&lt;T,&nbsp;Generator&lt;TResult&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;Random,&nbsp;TResult&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">newGenerator</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Generator&lt;TResult&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">g</span>&nbsp;=&nbsp;selector(generate(r)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;g.Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Generator&lt;TResult&gt;(newGenerator); }</pre> </p> <p> <code>SelectMany</code> enables you to chain generators together. You'll see an example later in the article. </p> <h3 id="36a3fb07ddc9428cb334939251deea36"> Query syntax <a href="#36a3fb07ddc9428cb334939251deea36">#</a> </h3> <p> As the <a href="/2022/03/28/monads">monad article</a> explains, you can enable C# query syntax by adding a special <code>SelectMany</code> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Generator&lt;TResult&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Generator&lt;U&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;U,&nbsp;TResult&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;SelectMany(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="font-weight:bold;color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> The implementation body always looks the same; only the method signature varies from monad to monad. Again, I'll show you an example of using query syntax later in the article. </p> <h3 id="3486b1debaf34921b03fd307d03fadce"> Flatten <a href="#3486b1debaf34921b03fd307d03fadce">#</a> </h3> <p> In <a href="/2022/03/28/monads">the introduction</a> you learned that if you have a <code>Flatten</code> or <code>Join</code> function, you can implement <code>SelectMany</code>, and the other way around. Since we've already defined <code>SelectMany</code> for <code><span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code>, we can use that to implement <code>Flatten</code>. In this article I use the name <code>Flatten</code> rather than <code>Join</code>. This is an arbitrary choice that doesn't impact behaviour. Perhaps you find it confusing that I'm inconsistent, but I do it in order to demonstrate that the behaviour is the same even if the name is different. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Generator&lt;T&gt;&nbsp;<span style="color:#74531f;">Flatten</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Generator&lt;Generator&lt;T&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">generator</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;generator.SelectMany(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> As you can tell, this function has to be an extension method, since we can't have a class typed <code>Generator&lt;Generator&lt;T&gt;&gt;</code>. As usual, when you already have <code>SelectMany</code>, the body of <code>Flatten</code> (or <code>Join</code>) is always the same. </p> <h3 id="347da42ffc7a4c8296743de68cf6a622"> Return <a href="#347da42ffc7a4c8296743de68cf6a622">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that <a href="https://www.haskell.org/">Haskell</a> uses): </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Generator&lt;T&gt;&nbsp;<span style="color:#74531f;">Return</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="font-weight:bold;color:#1f377f;">value</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Generator&lt;T&gt;(<span style="font-weight:bold;color:#1f377f;">_</span>&nbsp;=&gt;&nbsp;value); }</pre> </p> <p> This function ignores the random number generator and always returns <code>value</code>. </p> <h3 id="60fdd02680034ebe8ec21cff4fdb47e6"> Left identity <a href="#60fdd02680034ebe8ec21cff4fdb47e6">#</a> </h3> <p> We needed to identify the <em>return</em> function in order to examine <a href="/2022/04/11/monad-laws">the monad laws</a>. Let's see what they look like for the Test Data Generator monad, starting with the left identity law. </p> <p> <pre>[Theory] [InlineData(17,&nbsp;0)] [InlineData(17,&nbsp;8)] [InlineData(42,&nbsp;0)] [InlineData(42,&nbsp;1)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">LeftIdentityLaw</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seed</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Generator&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Generator&lt;<span style="color:blue;">string</span>&gt;(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Next(i).ToString()); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Generator.Return(x).SelectMany(h).Generate(<span style="color:blue;">new</span>&nbsp;Random(seed)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h(x).Generate(<span style="color:blue;">new</span>&nbsp;Random(seed))); }</pre> </p> <p> Notice that the test can't directly compare the two generators, because equality isn't clearly defined for that class. Instead, the test has to call <code>Generate</code> in order to produce comparable values; in this case, strings. </p> <p> Since <code>Generate</code> is non-deterministic, the test has to <code>seed</code> the random number generator argument in order to get reproducible results. It can't even declare one <code>Random</code> object and share it across both method calls, since generating values changes the state of the object. Instead, the test has to generate two separate <code>Random</code> objects, one for each call to <code>Generate</code>, but with the same <code>seed</code>. </p> <h3 id="eed0615f4fce4cd79ecc57a734a358ea"> Right identity <a href="#eed0615f4fce4cd79ecc57a734a358ea">#</a> </h3> <p> In a manner similar to above, we can showcase the right identity law as a test. </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&#39;a&#39;</span>,&nbsp;0)] [InlineData(<span style="color:#a31515;">&#39;a&#39;</span>,&nbsp;8)] [InlineData(<span style="color:#a31515;">&#39;j&#39;</span>,&nbsp;0)] [InlineData(<span style="color:#a31515;">&#39;j&#39;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">RightIdentityLaw</span>(<span style="color:blue;">char</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">letter</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seed</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">char</span>,&nbsp;Generator&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">c</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Generator&lt;<span style="color:blue;">string</span>&gt;(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(c,&nbsp;r.Next(100))); &nbsp;&nbsp;&nbsp;&nbsp;Generator&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">m</span>&nbsp;=&nbsp;f(letter); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.SelectMany(Generator.Return).Generate(<span style="color:blue;">new</span>&nbsp;Random(seed)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.Generate(<span style="color:blue;">new</span>&nbsp;Random(seed))); }</pre> </p> <p> As always, even a parametrised test constitutes no <em>proof</em> that the law holds. I show the tests to illustrate what the laws look like in 'real' code. </p> <h3 id="bca718938dd84e3f9d650e42094768f9"> Associativity <a href="#bca718938dd84e3f9d650e42094768f9">#</a> </h3> <p> The last monad law is the associativity law that describes how (at least) three functions compose. We're going to need three functions. For the demonstration test I'm going to conjure three nonsense functions. While this may not be as intuitive, it on the other hand reduces the noise that more realistic code tends to produce. Later in the article you'll see a more realistic example. </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&#39;t&#39;</span>,&nbsp;&nbsp;0)] [InlineData(<span style="color:#a31515;">&#39;t&#39;</span>,&nbsp;28)] [InlineData(<span style="color:#a31515;">&#39;u&#39;</span>,&nbsp;&nbsp;0)] [InlineData(<span style="color:#a31515;">&#39;u&#39;</span>,&nbsp;98)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AssociativityLaw</span>(<span style="color:blue;">char</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">a</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seed</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">char</span>,&nbsp;Generator&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">c</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Generator&lt;<span style="color:blue;">string</span>&gt;(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(c,&nbsp;r.Next(100))); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Generator&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Generator&lt;<span style="color:blue;">int</span>&gt;(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Next(s.Length)); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Generator&lt;TimeSpan&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">h</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Generator&lt;TimeSpan&gt;(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;TimeSpan.FromDays(r.Next(i))); &nbsp;&nbsp;&nbsp;&nbsp;Generator&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.SelectMany(g).SelectMany(h).Generate(<span style="color:blue;">new</span>&nbsp;Random(seed)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.SelectMany(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h)).Generate(<span style="color:blue;">new</span>&nbsp;Random(seed))); }</pre> </p> <p> All tests pass. </p> <h3 id="3bcc2327700747008fbc5b00865fffc3"> CPR example <a href="#3bcc2327700747008fbc5b00865fffc3">#</a> </h3> <p> Formalities out of the way, let's look at a more realistic example. In the article about the <a href="/2018/11/26/the-test-data-generator-applicative-functor">Test Data Generator applicative functor</a> you saw an example of parsing a <a href="https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)">Danish personal identification number</a>, in Danish called <em>CPR-nummer</em> (<em>CPR number</em>) for <em>Central Person Register</em>. (It's not a register of central persons, but rather the central register of persons. Danish works slightly differently than English.) </p> <p> CPR numbers have a simple format: <code>DDMMYY-SSSS</code>, where the first six digits indicate a person's birth date, and the last four digits are a sequence number. An example could be <code>010203-1234</code>, which indicates a woman born February 1, 1903. </p> <p> In C# you might model a CPR number as a class with a constructor like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CprNumber</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">day</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">month</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">year</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sequenceNumber</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(year&nbsp;&lt;&nbsp;0&nbsp;||&nbsp;99&nbsp;&lt;&nbsp;year) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(year), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Year&nbsp;must&nbsp;be&nbsp;between&nbsp;0&nbsp;and&nbsp;99,&nbsp;inclusive.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(month&nbsp;&lt;&nbsp;1&nbsp;||&nbsp;12&nbsp;&lt;&nbsp;month) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(month), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Month&nbsp;must&nbsp;be&nbsp;between&nbsp;1&nbsp;and&nbsp;12,&nbsp;inclusive.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(sequenceNumber&nbsp;&lt;&nbsp;0&nbsp;||&nbsp;9999&nbsp;&lt;&nbsp;sequenceNumber) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(sequenceNumber), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Sequence&nbsp;number&nbsp;must&nbsp;be&nbsp;between&nbsp;0&nbsp;and&nbsp;9999,&nbsp;inclusive.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">fourDigitYear</span>&nbsp;=&nbsp;CalculateFourDigitYear(year,&nbsp;sequenceNumber); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">daysInMonth</span>&nbsp;=&nbsp;DateTime.DaysInMonth(fourDigitYear,&nbsp;month); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(day&nbsp;&lt;&nbsp;1&nbsp;||&nbsp;daysInMonth&nbsp;&lt;&nbsp;day) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(day), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Day&nbsp;must&nbsp;be&nbsp;between&nbsp;1&nbsp;and&nbsp;</span>{daysInMonth}<span style="color:#a31515;">,&nbsp;inclusive.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.day&nbsp;=&nbsp;day; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.month&nbsp;=&nbsp;month; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.year&nbsp;=&nbsp;year; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.sequenceNumber&nbsp;=&nbsp;sequenceNumber; }</pre> </p> <p> The system has been around since 1968 so clearly suffers from a <a href="https://en.wikipedia.org/wiki/Year_2000_problem">Y2k problem</a>, as years are encoded with only two digits. The workaround for this is that the most significant digit of the sequence number encodes the century. At the time I'm writing this, <a href="https://da.wikipedia.org/wiki/CPR-nummer">the Danish-language wikipedia entry for CPR-nummer</a> still includes a table that shows how one can derive the century from the sequence number. This enables the CPR system to handle birth dates between 1858 and 2057. </p> <p> The <code>CprNumber</code> constructor has to consult that table in order to determine the century. It uses the <code>CalculateFourDigitYear</code> function for that. Once it has the four-digit year, it can use the <a href="https://learn.microsoft.com/dotnet/api/system.datetime.daysinmonth">DateTime.DaysInMonth</a> method to determine the number of days in the given month. This is used to validate the day parameter. </p> <p> The <a href="/2018/11/26/the-test-data-generator-applicative-functor">previous article</a> showed a test that made use of a Test Data Generator for the <code>CprNumber</code> class. The generator was referenced as <code>Gen.CprNumber</code>, but how do you define such a generator? </p> <h3 id="02e0bc1c345c4c2b910abd5fa338b30c"> CPR number generator <a href="#02e0bc1c345c4c2b910abd5fa338b30c">#</a> </h3> <p> The constructor arguments for <code>month</code>, <code>year</code>, and <code>sequenceNumber</code> are easy to generate. You need a basic generator that produces values between two boundaries. Both <a href="https://en.wikipedia.org/wiki/QuickCheck">QuickCheck</a> and <a href="https://fscheck.github.io/FsCheck">FsCheck</a> call it <code>choose</code>, so I'll reuse that name: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Generator&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">Choose</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">min</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">max</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Generator&lt;<span style="color:blue;">int</span>&gt;(<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Next(min,&nbsp;max&nbsp;+&nbsp;1)); }</pre> </p> <p> The <code>choose</code> functions of QuickCheck and FsCheck consider both boundaries to be inclusive, so I've done the same. That explains the <code>+ 1</code>, since <a href="https://learn.microsoft.com/dotnet/api/system.random.next">Random.Next</a> excludes the upper boundary. </p> <p> You can now combine <code>choose</code> with <code>DateTime.DaysInMonth</code> to generate a valid day: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Generator&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">Day</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">year</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">month</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">daysInMonth</span>&nbsp;=&nbsp;DateTime.DaysInMonth(year,&nbsp;month); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;Gen.Choose(1,&nbsp;daysInMonth); }</pre> </p> <p> Let's pause and consider the implications. The point of this example is to demonstrate why it's practically useful that Test Data Generators are monads. Keep in mind that monads are functors you can flatten. When do you need to flatten a functor? Specifically, when do you need to flatten a Test Data Generator? Right now, as it turns out. </p> <p> The <code>Day</code> method returns a <code>Generator&lt;<span style="color:blue;">int</span>&gt;</code>, but where do the <code>year</code> and <code>month</code> arguments come from? They'll typically be produced by another Test Data Generator such as <code>choose</code>. Thus, if you only <em>map</em> (<code>Select</code>) over previous Test Data Generators, you'll produce a <code>Generator&lt;Generator&lt;<span style="color:blue;">int</span>&gt;&gt;</code>: </p> <p> <pre>Generator&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">genYear</span>&nbsp;=&nbsp;Gen.Choose(1970,&nbsp;2050); Generator&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">genMonth</span>&nbsp;=&nbsp;Gen.Choose(1,&nbsp;12); Generator&lt;(<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>)&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">genYearAndMonth</span>&nbsp;=&nbsp;genYear.Apply(genMonth); Generator&lt;Generator&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">genDay</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;genYearAndMonth.Select(<span style="font-weight:bold;color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;Gen.Choose(1,&nbsp;DateTime.DaysInMonth(t.Item1,&nbsp;t.Item2)));</pre> </p> <p> This example uses an <code>Apply</code> overload to combine <code>genYear</code> and <code>genMonth</code>. As long as the two generators are independent of each other, you can use the <a href="/2018/10/01/applicative-functors">applicative functor</a> capability to combine them. When, however, you need to produce a new generator from a value produced by a previous generator, the functor or applicative functor capabilities are insufficient. If you try to use <code>Select</code>, as in the above example, you'll produce a nested generator. </p> <p> Since it's a monad, however, you can <code>Flatten</code> it: </p> <p> <pre>Generator&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">flattened</span>&nbsp;=&nbsp;genDay.Flatten();</pre> </p> <p> Or you can use <code>SelectMany</code> (monadic <em>bind</em>) to flatten as you go. The <code>CprNumber</code> generator does that, although it uses query syntax syntactic sugar to make the code more readable: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Generator&lt;CprNumber&gt;&nbsp;CprNumber&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;sequenceNumber&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(0,&nbsp;9999) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;year&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(0,&nbsp;99) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;month&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Choose(1,&nbsp;12) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fourDigitYear&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestDataBuilderFunctor.CprNumber.CalculateFourDigitYear(year,&nbsp;sequenceNumber) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;day&nbsp;<span style="color:blue;">in</span>&nbsp;Gen.Day(fourDigitYear,&nbsp;month) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;CprNumber(day,&nbsp;month,&nbsp;year,&nbsp;sequenceNumber);</pre> </p> <p> The expression first uses <code>Gen.Choose</code> to produce three independent <code>int</code> values: <code>sequenceNumber</code>, <code>year</code>, and <code>month</code>. It then uses the <code>CalculateFourDigitYear</code> function to look up the proper century based on the two-digit <code>year</code> and the <code>sequenceNumber</code>. With that information it can call <code>Gen.Day</code>, and since the expression uses monadic composition, it's flattening as it goes. Thus <code>day</code> is an <code>int</code> value rather than a generator. </p> <p> Finally, the entire expression can compose the four <code>int</code> values into a valid <code>CprNumber</code> object. </p> <p> You can consult the <a href="/2018/11/26/the-test-data-generator-applicative-functor">previous article</a> to see <code>Gen.CprNumber</code> in use. </p> <h3 id="f3947d3a3c364fb28fb264aff59f7fe7"> Hedgehog CPR generator <a href="#f3947d3a3c364fb28fb264aff59f7fe7">#</a> </h3> <p> You can reproduce the CPR example in F# using one of several property-based testing frameworks. In this example, I'll continue the example from the <a href="/2018/11/26/the-test-data-generator-applicative-functor">previous article</a> as well as the article <a href="/2018/12/10/danish-cpr-numbers-in-f">Danish CPR numbers in F#</a>. You can see a couple of tests in these articles. They use the <code>cprNumber</code> generator, but never show the code. </p> <p> In all the property-based testing frameworks I've seen, generators are called <code>Gen</code>. This is also the case for Hedgehog. The <code>Gen</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> is a monad, and there's a <code>gen</code> <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/computation-expressions">computation expression</a> that supplies syntactic sugar. </p> <p> You can translate the above example to a Hedgehog <code>Gen</code> value like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;cprNumber&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;gen&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;sequenceNumber&nbsp;=&nbsp;Range.linear&nbsp;0&nbsp;9999&nbsp;|&gt;&nbsp;Gen.int32 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;Range.linear&nbsp;0&nbsp;&nbsp;&nbsp;99&nbsp;|&gt;&nbsp;Gen.int32 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;month&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;Range.linear&nbsp;1&nbsp;&nbsp;&nbsp;12&nbsp;|&gt;&nbsp;Gen.int32 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fourDigitYear&nbsp;&nbsp;&nbsp;=&nbsp;Cpr.calculateFourDigitYear&nbsp;year&nbsp;sequenceNumber &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;daysInMonth&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;DateTime.DaysInMonth&nbsp;(fourDigitYear,&nbsp;month) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;day&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;Range.linear&nbsp;1&nbsp;daysInMonth&nbsp;|&gt;&nbsp;Gen.int32 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Cpr.tryCreate&nbsp;day&nbsp;month&nbsp;year&nbsp;sequenceNumber&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Gen.some</pre> </p> <p> To keep the example simple, I haven't defined an explicit <code>day</code> generator, but instead just inlined <code>DateTime.DaysInMonth</code>. </p> <p> Consult the articles that I linked above to see the <code>Gen.cprNumber</code> generator in use. </p> <h3 id="15d1feac289e426ebd5845fc59d3f33e"> Conclusion <a href="#15d1feac289e426ebd5845fc59d3f33e">#</a> </h3> <p> Test Data Generators form monads. This is useful when you need to generate test data that depend on other generated test data. Monadic <em>bind</em> (<code>SelectMany</code> in C#) can flatten the generator functor as you go. This article showed examples in both C# and F#. </p> <p> The same abstraction also exists in the Haskell QuickCheck library, but I haven't shown any Haskell examples. If you've taken the trouble to learn Haskell (which you should), you already know what a monad is. </p> <p> <strong>Next:</strong> <a href="/2022/07/11/functor-relationships">Functor relationships</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="771023c319fb4f03bce6e3beb79294e6"> <div class="comment-author"><a href="https://github.com/AnthonyLloyd">Anthony Lloyd</a> <a href="#771023c319fb4f03bce6e3beb79294e6">#</a></div> <div class="comment-content"> <p> <a href="https://github.com/AnthonyLloyd/CsCheck">CsCheck</a> is a full implementation of something along these lines. It uses the same random sample generation in the shrinking step always reducing a Size measure. It turns out to be a better way of shrinking than the QuickCheck way. </p> </div> <div class="comment-date">2023-02-27 22:51 UTC</div> </div> <div class="comment" id="21f1a20a1a5944fbbbdd300c8f0e89b0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#21f1a20a1a5944fbbbdd300c8f0e89b0">#</a></div> <div class="comment-content"> <p> Anthony, thank you for writing. You'll be pleased to learn, I take it, that the next article in the series about the <a href="/2023/02/13/epistemology-of-interaction-testing">epistemology of interaction testing</a> uses CsCheck as the example framework. </p> </div> <div class="comment-date">2023-02-28 7:33 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A thought on workplace flexibility and asynchrony https://blog.ploeh.dk/2023/02/20/a-thought-on-workplace-flexibility-and-asynchrony 2023-02-20T07:03:00+00:00 Mark Seemann <div id="post"> <p> <em>Is an inclusive workplace one that enables people to work at different hours?</em> </p> <p> In the early <a href="https://en.wikipedia.org/wiki/Aughts">noughties</a> I worked for Microsoft Consulting Service in Denmark. In some sense it was quite the competitive working environment with an unhealthy focus on billable hours, customer satisfaction surveys, and stack ranking. On the other hand, since I was mostly on my own on customer engagements, my managers didn't care when and how I worked. As long as I billed and customers were happy, they were happy. </p> <p> That sometimes allowed me great flexibility. </p> <p> At one time I was on a project for a customer in another part of Denmark, and while Denmark isn't that big, it was still understood that I would do most of my work remotely. The main deliverable was the code base for a software system, and while I might email and otherwise communicate with the customer and a few colleagues during the day, we didn't have any fixed schedules. In other words, I could work whenever I wanted, as long as I got the work done. </p> <p> My daughter was a toddler at the time, and as is the norm in Denmark, already in day nursery. My wife is a doctor and was, at that time, working in hospitals - some of the most inflexible workplaces I can think of. She had to leave early in the morning because the hospitals run on fixed schedules. </p> <p> I'd get up to have breakfast with her. After she left for work, I'd work until my daughter woke up. She typically woke up between 8 and 9, so I'd already be 1-2 hours into my work day. I'd stop working, make her breakfast, and take her to day care. We'd typically arrive between 10 and 11 in good spirits. I'd then bicycle home and work until my wife came home with our daughter. Perhaps I'd get a few more hours of work done in the evening. </p> <p> I worked odd hours, and I loved the flexibility. My customers expected me to deliver iterations of the software and generally stay in touch, but they were perfectly happy with mostly asynchronous communication. Back then, it mostly meant email. </p> <p> During the normal work day, I might be unavailable for hours, taking care of my daughter, exercising, grocery shopping, etc. Yet, I still billed more hours than most of my colleagues, and ultimately received an award for my work. </p> <p> In the decades that followed, I haven't always had such flexibility, but that early experience gave me a strong appreciation for asynchronous work. </p> <h3 id="3d5f041e6a4d46d5997c67e7693c3006"> Lockdown work wasn't flexible <a href="#3d5f041e6a4d46d5997c67e7693c3006">#</a> </h3> <p> When COVID-19 hit and most countries went into lockdown, many office workers got their first taste of remote work. Many struggled, for a variety of reasons. Some of those reasons are quite real. If you don't have a well-equipped home office, spending eight hours a day on a kitchen chair is hardly ideal working conditions. And no, the sofa isn't a good long-term solution either. </p> <p> Another problem during lockdown is that your entire family may be home, too. If you have kids, you'll have to attend to them. To be clear, if you've only experienced working from home during COVID-19 lockdown, you may have suffered from many of these problems without realising the benefits of flexibility. </p> <p> To add spite to injury, many workplaces tried to carry on as if nothing had changed, apart from the physical location of people. Office hours were still in effect, and work now took place over video calls. If you spent eight hours on Teams or Zoom, that's not flexible working conditions. Rather, it's the worst of both worlds. The only benefit is that you avoid the commute. </p> <h3 id="ac7c2c6f7cf54f0ab882275944262f71"> Remote compared to asynchronous work <a href="#ac7c2c6f7cf54f0ab882275944262f71">#</a> </h3> <p> As outlined above, remote work isn't necessarily flexible. Flexibility comes from asynchronous work processes more than physical location. The flexibility is a result of the freedom to chose <em>when</em> to work, more than <em>where</em> to work. </p> <p> Based on my decades of experience working asynchronously from home, I published <a href="/2020/03/16/conways-law-latency-versus-throughput">an article about the trade-off between latency and throughput</a>, comparing working together in an office with working asynchronously from home. The point is that you can make both work, but the way you organise work matters. In-office work is efficient if everyone is at the office at the same time. Remote work is efficient if people can work asynchronously. </p> <p> As is usually the case, there are trade-offs. The disadvantage of working together is that you must all be present simultaneously. Thus, you don't get the flexibility of choosing when to work. The benefits of working asynchronously is exactly that flexibility, but on the other hand, you lose the advantage of the efficient, high-bandwidth communication that comes from being physically in the same room as others. </p> <h3 id="219d3cd5d3c94996b5d3b2993b8a5512"> Inclusion through flexibility? <a href="#219d3cd5d3c94996b5d3b2993b8a5512">#</a> </h3> <p> I was recently listening to an episode of the <a href="https://freakonomics.com/series/freakonomics-radio/">Freakonomics Radio</a> podcast. As a side remark, someone mentioned that for women an important workplace criterion is <em>flexibility</em>. This, clearly, has some implications for this discussion. </p> <p> There's a strong statistical tendency for women to have work-life priorities different from men. For example, <a href="https://ec.europa.eu/eurostat/web/products-eurostat-news/-/EDN-20200306-1">Eurostat reports that women are more likely to work part-time</a>. That may be a signifier that although women want to work, they may want to work less than men. Or perhaps with more flexible hours. </p> <p> If that's true, what does it mean for software development? </p> <p> If you want to include people who value flexibility highly (e.g. some women, but also me) then work should be structured to enable people to engage with it when they have the time. That might include early in the morning, late in the evening, or during the weekend. </p> <p> Two workers who value flexibility may not be on the same schedule. When collaborating, they may have to do so asynchronously. Emails, work item trackers, pull requests. </p> <h3 id="554f4dbddb6746fe9ca2224df4714049"> Inclusive collaboration <a href="#554f4dbddb6746fe9ca2224df4714049">#</a> </h3> <p> Most software development takes place in teams. Various team members have different skills, which is good, because a modern software system comprises more components than most people can overcome. Unless you're one of those rainbow unicorns who master modern front-end development, back-end development, DevOps, database design and administration, graphical design, security concerns, cloud computing platforms, reporting and analytics, etc. you'll need to collaborate with team members. </p> <p> You can do so with short-lived Git branches, <a href="/2021/06/21/agile-pull-requests">agile pull requests</a>, and generally well-written communication. No, pull requests and asynchronous reviews don't have to be slow. </p> <p> Recently, I've noticed an increased tendency among some software development thought leaders to extol the virtues of pair- and ensemble programming. These are great collaboration techniques. I've used them with great success in specific contexts. I also write about their advantages in my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> Pair- and ensemble programming are synchronous collaboration techniques. There are clear advantages to them, but it's a requirement that team members participate at the same time. </p> <p> I'm sure it's fun and stimulating if you're already mostly extravert, but it doesn't strike me as particularly inclusive, time-wise. </p> <p> If you can't be at the office 9-17 you can't participate. Sorry, we can't use you then. </p> <p> What's that, you say? You can work <em>some</em> hours during the day, evenings, and sometimes weekends? But only twenty-five hours a week? Sorry, that doesn't fit our process. </p> <h3 id="cd6fdd9f987041e18e35f699952124b2"> A high-throughput alternative <a href="#cd6fdd9f987041e18e35f699952124b2">#</a> </h3> <p> Pair- and ensemble programming are great collaboration techniques, but I've noticed an increased tendency to contrast them to a particular style of siloed, slow solo work with which I'm honestly not familiar. I do, however, consider that a false dichotomy. </p> <p> The alternative to ensemble programming doesn't <em>have</em> to be slow, waterfall-like, feature-branch-based solo work heavy on misunderstandings, integration problems, and rework. It can be asynchronous, pull-based work. <a href="/2023/01/23/agilean">Lean</a>. </p> <p> I've lived that dream. I know that it <em>can</em> work. Is it easy? No. Does it require discipline? Yes. But it's possible, and it's <em>flexible</em>. It enables people to work when they have the time. </p> <h3 id="ebbf4488a29a44569e92c491886c3e83"> Conclusion <a href="#ebbf4488a29a44569e92c491886c3e83">#</a> </h3> <p> There are people who would like to work, just not 9-17. Perhaps they <em>can't</em> (for all sorts of socio-economic reasons), or perhaps that just doesn't align with their life choices. Perhaps they're just <a href="/2015/12/04/the-rules-of-attraction-location">not in your time zone</a>. </p> <p> Do you want to include these people, or exclude them? </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Epistemology of interaction testing https://blog.ploeh.dk/2023/02/13/epistemology-of-interaction-testing 2023-02-13T06:48:00+00:00 Mark Seemann <div id="post"> <p> <em>How do we know that components interact correctly?</em> </p> <p> Most software systems are composed as a graph of components. To be clear, I use the word <em>component</em> loosely to mean a collection of functionality - it may be an object, a module, a function, a data type, or perhaps something else I haven't thought of. Some components deal with the bigger picture and will typically coordinate other components that perform more specific tasks. If we think of a component graph as a tree, then some components are leaves. </p> <p> <img src="/content/binary/component-graph.png" alt="Example component graph with four leaves."> </p> <p> Leaf components, being self-contained and without dependencies, are typically the easiest to test. Most test-driven development (TDD) katas focus on these kinds of components: <a href="https://codingdojo.org/kata/Tennis/">Tennis</a>, <a href="https://codingdojo.org/kata/Bowling/">bowling</a>, <a href="http://claysnow.co.uk/recycling-tests-in-tdd/">diamond</a>, <a href="https://codingdojo.org/kata/RomanNumerals/">Roman numerals</a>, <a href="https://kata-log.rocks/gossiping-bus-drivers-kata">gossiping bus drivers</a>, and so on. Even the <a href="https://www.devjoy.com/blog/legacy-code-katas/">legacy security manager kata</a> is simple and quite self-contained. There's nothing wrong with that, and there's good reason to keep such exercises simple. After all, you want to be able to <a href="/2020/01/13/on-doing-katas">complete a kata</a> in a few hours. You can hardly do that if the exercise is to develop an entire web site with user interface, persistent data storage, security, data validation, business logic, third-party integration, emails, instrumentation and logging, and so on. </p> <p> This means that even if you get good at TDD against 'leaf' functionality, you may be struggling when it comes to higher-level components. How does one unit test code that has dependencies? </p> <h3 id="66efa9d72a62458e93ff8490ef7585e9"> Interaction-based testing <a href="#66efa9d72a62458e93ff8490ef7585e9">#</a> </h3> <p> A common solution is to <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">invert the dependencies</a>. You can, for example, use <a href="/dippp">Dependency Injection</a> to inject <a href="https://martinfowler.com/bliki/TestDouble.html">Test Doubles</a> into the System Under Test (SUT). This enables you to control the behaviour of the dependencies and to verify that the SUT behaves as expected. Not only that, but you can also verify that the SUT interacts with the dependencies as expected. This is called <em>interaction-based testing</em>. It is, perhaps, the most common form of unit testing in the industry, and exemplary explained in <a href="/ref/goos">Growing Object-Oriented Software, Guided by Tests</a>. </p> <p> The kinds of Test Doubles most useful with interaction-based testing are <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a> and <a href="http://xunitpatterns.com/Mock%20Object.html">Mocks</a>. They are, however, problematic because <a href="/2022/10/17/stubs-and-mocks-break-encapsulation">they break encapsulation</a>. And encapsulation, to be clear, is <a href="/2022/10/24/encapsulation-in-functional-programming">also a concern in functional programming</a>. </p> <p> I have already described how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based to state-based testing</a>, and why <a href="/2015/05/07/functional-design-is-intrinsically-testable">functional programming is intrinsically more testable</a>. </p> <h3 id="db90b714ddc24393b4340cdd98a19082"> How to test composition of pure functions? <a href="#db90b714ddc24393b4340cdd98a19082">#</a> </h3> <p> When you adopt functional programming (FP) you'll sooner or later need to compose or orchestrate pure functions. How do you test that the composition of pure functions is correct? That's what you can test with a Mock or <a href="http://xunitpatterns.com/Test%20Spy.html">Spy</a>. </p> <p> You've developed component <em>A</em>, perhaps as a <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order function</a>, that depends on another component <em>B</em>. You want to test that <em>A</em> correctly interacts with <em>B</em>, but if interaction-based testing is no longer 'allowed' (because it breaks encapsulation), then what do you do? </p> <p> For a long time, I pondered that question myself, while I was busy enjoying FP making most things easier. It took me some time to understand that the answer, as is often the case, is <a href="https://en.wikipedia.org/wiki/Mu_(negative)">mu</a>. I'll get back to that later. </p> <p> I'm not the only one struggling with this question. Sergei Rogovtcev writes and asks what I interpret as the same question: </p> <blockquote> <p> "I do have a component A, which is, frankly, some controller doing some checks and processing around a fairly complex state. This process can have several outcomes, let's call them Success, Fail, and Missing (the actual states are not important, but I'd like to have more than two). Then we have a component B, which is responsible for the rendering of the result. Of course, three different states lead to three different renderings, but the renderings are also influenced by state (let's say we have browser, mobile and native clients, and we need to provide different renderings). Originally the components are objects, B having three separate methods, but I can express them as pure functions, at least for the purpose of this discussion - A, and then BSuccess, BFail and BMissing. I can easily test each part of B in isolation; the problem comes when I need to test A, which calls different parts of B. If I use mocks, the solution is simple - I inject a mock of B to A, and then verify that A calls appropriate parts according to the process result. This requires knowing the innards of A, but otherwise it is a well-known and well-understood approach. But if I want to avoid mocks, what do I do? I cannot test A without relying on some code path in B, and this to me means that I'm losing the benefits of unit testing and entering the realm of integration testing." </p> </blockquote> <p> In his email Sergei Rogovtcev has explicitly given me permission to quote him and engage with this question. As I've outlined, I've grappled with that question myself, so I find the question worthwhile. I can't, however, work with it without questioning the premise. This is not an attack on Sergei Rogovtcev; after all, I had that question myself, so any critique I make is directed as much at my former self as at him. </p> <h3 id="034c99ad20644781af4f28db0f45b2dd"> Axiomatic versus scientific knowledge <a href="#034c99ad20644781af4f28db0f45b2dd">#</a> </h3> <p> It may be helpful to elevate the discussion. How do we know that software (or a subsystem thereof) works? You could say that one answer to that is: <em>Passing tests</em>. If all tests are passing, we may have high confidence that the system works. </p> <p> In the parlance of Sergei Rogovtcev, we can easily unit test component <em>B</em> because it's composed from <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> <p> How do we unit test component <em>A</em>, though? With Mocks and Stubs, you can prove that the interaction works as intended. The keyword here is <em>prove</em>. If you assume that component <em>B</em> works correctly, 'all' you have to do is to demonstrate that component <em>A</em> correctly interacts with component <em>B</em>. I used to do that all the time and called it <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">data-flow verification</a> or <a href="/2013/04/04/structural-inspection">structural inspection</a>. The idea was that if you could demonstrate that component <em>A</em> correctly interacts with <em>any</em> <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">LSP</a>-compliant implementation of component <em>B</em>, and then also demonstrate that in reality (when composed in the <a href="/2011/07/28/CompositionRoot">Composition Root</a>) component <em>A</em> is composed with a component <em>B</em> that has also been demonstrated to work correctly, then the (sub-)system works correctly. </p> <p> This is almost like a mathematical proof. First prove <em>lemma B</em>, then prove <em>theorem A</em> using <em>lemma B</em>. Finally, state <em>corollary C</em>: <em>b</em> is a special case handled by <em>lemma B</em>, so therefore <em>a</em> is covered by <em>theorem A</em>. <a href="https://en.wikipedia.org/wiki/Q.E.D.">Q.E.D.</a> </p> <p> It's a logical and deductive approach to the problem of verifying the composition of the whole from verified parts. It's almost mathematical in the sense that it tries to erect an <a href="https://en.wikipedia.org/wiki/Axiomatic_system">axiomatic system</a>. </p> <p> It's also fundamentally flawed. </p> <p> I didn't understand that a decade ago, and in practice, the method worked well enough - apart from all the problems stemming from poor encapsulation. The problem with that approach is that an axiomatic system is only as strong as its <a href="https://en.wikipedia.org/wiki/Axiom">axioms</a>. What are the axioms in this system? The axioms, or premises, are that each of the components (<em>A</em> and <em>B</em>) are already correct. Based on these premises, this testing approach then proves that the composition is also correct. </p> <p> How do we know that the components work correctly? </p> <p> In this context, the answer is that they pass all tests. This, however, doesn't constitute any kind of <em>proof</em>. Rather, this is experimental knowledge, more reminiscent of science than of mathematics. </p> <p> Why are we trying to <em>prove</em>, then, that composition works correctly? Why not just <em>test</em> it? </p> <p> This observation cuts to the heart of the epistemology of testing. How do we know that software works? Typically not by <em>proving</em> it correct, but by subjecting it to experiments. As I've also outlined in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, we can regard automated tests as scientific experiments that we repeat over and over. </p> <h3 id="604436572933477d86d64349357d84ae"> Integration testing <a href="#604436572933477d86d64349357d84ae">#</a> </h3> <p> To outline the argument so far: While you <em>can</em> use Mocks and Spies to verify that a component correctly interacts with another component, this may be overkill. You're essentially trying to prove a conjecture based on doubtful evidence. </p> <p> Does it really matter that two components <em>interact</em> correctly? Aren't the components implementation details? Do users care? </p> <p> Users and other stakeholders care about the <em>behaviour</em> of the software system. Why not test that? </p> <p> This is, unfortunately, easier said than done. Sergei Rogovtcev strongly implies that he isn't keen on integration testing. While he doesn't explicitly state why, there are good reasons to be wary of integration testing. <a href="https://www.infoq.com/presentations/integration-tests-scam/">As J.B. Rainsberger eloquently explained</a>, a major problem with integration testing is the combinatorial explosion of test cases. If you ought to write 53,000 test cases to cover all combinations of pathways through integrated components, which test cases do you write? Surely not all 53,000. </p> <p> J.B. Rainsberger's argument is that if you're going to write no more than a dozen unit tests, you're unlikely to cover enough test cases to be confident that the system works. </p> <p> What if, however, you could write hundreds or thousands of test cases? </p> <h3 id="3d43f77a5480418780816b3d6a8b9a0f"> Property-based testing <a href="#3d43f77a5480418780816b3d6a8b9a0f">#</a> </h3> <p> You may recall that the premise of this article is functional programming (FP), where <em>property-based testing</em> is a common testing technique. While you can, to a degree, also use this technique in object-oriented programming (OOP), it's often difficult because of side effects and non-deterministic behaviour. </p> <p> When you write a property-based test, you write a single piece of code that evaluates a <em>property</em> of the SUT. The property looks like a parametrised unit test; the difference is that the input is generated randomly, but in a fashion you can control. This enables you to write hundreds or thousands of test cases without having to write them explicitly. </p> <p> Thus, epistemologically, you can use property-based testing with integrated components to produce confidence that the (sub-)system works. In practice, I find that the confidence I get from this technique is at least as high as the one I used to get from unit testing with Stubs and Spies. </p> <h3 id="9edd8d0a18cd4e1abbfaad2d7a3a650a"> Examples <a href="#9edd8d0a18cd4e1abbfaad2d7a3a650a">#</a> </h3> <p> All of this is abstract and theoretical, I realise. <a href="http://www.exampler.com/">An example would be handy right about now</a>. Such examples, however, are complex enough to warrant their own articles: </p> <ul> <li><a href="/2023/03/13/confidence-from-facade-tests">Confidence from Facade Tests</a></li> <li><a href="/2023/04/03/an-abstract-example-of-refactoring-from-interaction-based-to-property-based-testing">An abstract example of refactoring from interaction-based to property-based testing</a></li> <li><a href="/2023/04/17/a-restaurant-example-of-refactoring-from-example-based-to-property-based-testing">A restaurant example of refactoring from example-based to property-based testing</a></li> <li><a href="/2023/05/01/refactoring-pure-function-composition-without-breaking-existing-tests">Refactoring pure function composition without breaking existing tests</a></li> <li><a href="/2023/06/19/when-is-an-implementation-detail-an-implementation-detail">When is an implementation detail an implementation detail?</a></li> </ul> <p> Sergei Rogovtcev was kind enough to furnish a rather abstract, but <a href="https://en.wikipedia.org/wiki/Minimal_reproducible_example">minimal and self-contained</a>, example. I'll go through that first, and then follow up with a more realistic example. </p> <h3 id="2908ce4d26244c90bf20db829888205e"> Conclusion <a href="#2908ce4d26244c90bf20db829888205e">#</a> </h3> <p> How do you know that a software system works correctly? Ultimately, if it behaves in the way it's supposed to, it works correctly. Testing an entire system from the outside, however, is rarely viable in itself. The number of possible test cases is just too large. </p> <p> You can partially address that problem by decomposing the system into components. You can then test the components individually, and verify that they interact correctly. This last part is the topic of this article. A common way to to address this problem is to use Mocks and Spies to prove interactions correct. It does solve the problem of correctness quite neatly, but has the undesirable side effect of making the tests brittle. </p> <p> An alternative is to use property-based testing to verify that the components integrate correctly. Rather than something that looks like a proof, this is a question of numbers. Throw enough random test cases at the system, and you'll be confident that it works. How many? <a href="/2018/11/12/what-to-test-and-not-to-test">Enough</a>. </p> <p> <strong>Next:</strong> <a href="/2023/03/13/confidence-from-facade-tests">Confidence from Facade Tests</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f30e0d110f6c42feb75100e08c78beab"> <div class="comment-author"><a href="https://github.com/srogovtsev">Sergei Rogovtcev</a> <a href="#f30e0d110f6c42feb75100e08c78beab">#</a></div> <div class="comment-content"> <p>First of all, let me thank you for taking time and effort to discuss this.</p> <p>There's a minor point about integration testing:</p> <blockquote><p>[SR] strongly implies that he isn't keen on integration testing. While he doesn't explicitly state why...</p></blockquote> <p>The situation is somewhat more complicated: in fact, I tend to have at least a few integration tests for a feature I'm involved with, starting the coverage from the happy paths (the minimum requirement being to verify that we've wired correctly as many components as can be verified), and then, if possible, extending to error paths, edge cases and so on. Even the code from my email originally had integration tests covering all the outcomes for a single rendering (browser). The problem that I've faced then, and which prompted my question, was exactly the one that you quote from J.B. Rainsberger: combinatorial explosion. As soon as I decided to cover a second rendering (mobile), I saw that I needed to replicate the setups for outcomes (success/fail/missing), but modify the asserts for their rendering. And then again the same for the native client. Unit tests, even with their ungainly break in encapsulation, gave the simple appeal of writing less code...</p> <p>Hopefully, this seem to be the very same premise that you explore towards the end of your post, leading to the property-based testing - which I was trying to incorporate into my toolset for quite some time, but was always somewhat baffled at how it should work and integrate into object-oriented (and C#-based) code. So I'm very much looking forward for your next installment in this series.</p> <p>And again, thank you for exploring these matters.</p> </div> <div class="comment-date">2023-02-21 13:52 UTC</div> </div> <div class="comment" id="73cb2b8d15bf4e10a9e20a89dbad4374"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#73cb2b8d15bf4e10a9e20a89dbad4374">#</a></div> <div class="comment-content"> <p> Sergei, thank you for writing. I hope that this small series of articles will be able to at least give you some ideas. I am, however, concerned that I may miss the mark. </p> <p> When discussing problems like this, there's always a risk that the examples we look at are too simple; that they don't adequately represent the real world. For instance, we may look at the example code in the next few articles and calculate how well we've covered all combinations. </p> <p> Perhaps we may find that the combinatorial 'explosion' is only in the ten-thousands, which is within reasonable reach of well-written properties. </p> <p> Then, when we come back to our 'real' problems, the combinatorial explosion may be orders of magnitudes larger. You can easily ask a property-based framework to run a property millions of time, but it'll take time. Perhaps this makes the tests so slow that it's not a practical solution. </p> <p> All that said, I think that not all is lost. Part of the solution, however, may be found elsewhere. </p> <p> The more I learn about functional programming (FP), the more I'm amazed at the alternative mindset it offers. Solutions that look in one way in object-oriented programming (OOP) may look completely different in FP. You've probably noticed this yourself. Often, you have to turn a problem on its head to see it 'the FP way'. </p> <p> The following is something that I've not yet thought through rigorously, so perhaps there are flaws in my thinking. I offer it for peer review here. </p> <p> OOP composition tends to be 'deep'. If we think of object composition as a directed (acyclic, hopefully!) graph, typical OOP composition might resemble a graph where each node has only few children, but the distance from the root to each leaf is great. Since, every time you compose two objects, you have to multiply the number of pathways, this gives you this combinatorial explosion we've discussed. The deeper the graph, the worse it is. </p> <p> In FP I typically find myself composing functions in a more shallow fashion. Instead of having functions that call other functions that call other functions, etc. I tend to have functions that return values that I then pass to other functions, and so on. This produces a shallower and wider composition graph. Doesn't it also reduce the combinations that we need to consider for testing? </p> <p> I haven't subjected this idea to a more formal analysis yet, so this may be wrong. If I'm right, though, this could mean that property-based testing is still a viable solution to the problem. </p> <p> Identifying useful properties is another problem that you also bring up, particularly in the context of OOP. So far, property-based testing is more prevalent in FP, and perhaps there's a reason for that. </p> <p> It seems to me that there's a connection between property-based testing and encapsulation. Essentially, a property is an executable description of some invariant, or pre- or post-condition. Most real-world object-oriented code I've seen, however, isn't encapsulated. If you have poor encapsulation, it's no wonder that it's hard to identify useful properties. </p> <p> Even so, Identifying good properties is a skill that you have to learn. It's fairly easy to construct properties that, in a sense, 'reproduce the implementation'. The challenge is to avoid that, and that's not always easy. As an example, it took me years before I found <a href="/2021/06/28/property-based-testing-is-not-the-same-as-partition-testing">a good way to express properties of FizzBuzz without repeating the implementation</a>. </p> </div> <div class="comment-date">2023-02-22 8:00 UTC</div> </div> <div class="comment" id="6a9753ec6d54462c9d500112a55105b6"> <div class="comment-author"><a href="https://github.com/srogovtsev">Sergei Rogovtcev</a> <a href="#6a9753ec6d54462c9d500112a55105b6">#</a></div> <div class="comment-content"> <blockquote><p>This produces a shallower and wider composition graph. Doesn't it also reduce the combinations that we need to consider for testing?</p></blockquote> <p>Intuitively I'd say that it shouldn't (reduce), because in the end the number of combinations that we consider for testing is the number states our SUT can be in, which is defined as a combination of all its inputs. But I may, of course, miss something important here.</p> <p>My own opinion on this, coming from a short-ish brush with FP, is that FP, or, more precisely, more expressive type systems, reduce the number of combinations by reducing the number of possible inputs by the virtue of more expressive types. My favorite example is that even less expressive type system, one with simple <em>int</em> and <em>string</em> instead of all-encompassing <em>var</em>/<em>object</em>, allows us to get rid off all the tests where we pass "foo" to a function that only works on numbers. Explicit nullability gets rid of all the <em>null</em>-related test-cases (and we get an indication where we lack such cases for <em>null</em>-accepting functions). This can be continued by adding more and more cases until we arrive at the (in)famous "if it compiles, is works".</p> <p>I don't remember whether I've included this guard case in my original email, but I definitely remember thinking of mentioning that I'm confined to a less-expressive type system of C#. Even comparing to F# (as I remember it from my side studies), I can see how some tests can be made redundant by, for example, introducing a sum type and then relying on compiler to check for exhaustive match. Sometimes I wonder what would a more expressive type system do to these problems...</p> </div> <div class="comment-date">2023-02-22 14:40 UTC</div> </div> <div class="comment" id="9fa9a41fd827458aa0a87d854e5e8228"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9fa9a41fd827458aa0a87d854e5e8228">#</a></div> <div class="comment-content"> <p> Sergei, thank you for writing. A more expressive type system certainly does reduce the amount of testing required. While I prefer <a href="https://fsharp.org/">F#</a>, the good news is that most of what F# can do, C# can do, too. Everything is just more verbose in C#. The main stumbling block that people usually complain about is the lack of <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a>, but you can use <a href="/2018/06/25/visitor-as-a-sum-type">Visitors as sum types</a>. You get the same benefits as with F# discriminated unions, except with much more <a href="/2019/12/16/zone-of-ceremony">ceremony</a>. </p> </div> <div class="comment-date">2023-02-25 17:50 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Contravariant functors as invariant functors https://blog.ploeh.dk/2023/02/06/contravariant-functors-as-invariant-functors 2023-02-06T06:42:00+00:00 Mark Seemann <div id="post"> <p> <em>Another most likely useless set of invariant functors that nonetheless exist.</em> </p> <p> This article is part of <a href="/2022/08/01/invariant-functors">a series of articles about invariant functors</a>. An invariant functor is a <a href="/2018/03/22/functors">functor</a> that is neither covariant nor contravariant. See the series introduction for more details. </p> <p> It turns out that all <a href="/2021/09/02/contravariant-functors">contravariant functors</a> are also invariant functors. </p> <p> Is this useful? Let me, like in <a href="/2022/12/26/functors-as-invariant-functors">the previous article</a>, be honest and say that if it is, I'm not aware of it. Thus, if you're interested in practical applications, you can stop reading here. This article contains nothing of practical use - as far as I can tell. </p> <h3 id="fda7d35329c74cf9ab9753d0b25d1f08"> Because it's there <a href="#fda7d35329c74cf9ab9753d0b25d1f08" title="permalink">#</a> </h3> <p> Why describe something of no practical use? </p> <p> Why do some people climb <a href="https://en.wikipedia.org/wiki/Mount_Everest">Mount Everest</a>? <em>Because it's there</em>, or for other irrational reasons. Which is fine. I've no personal goals that involve climbing mountains, but <a href="/2020/10/12/subjectivity">I happily engage in other irrational and subjective activities</a>. </p> <p> One of them, apparently, is to write articles of software constructs of no practical use, <em>because it's there</em>. </p> <p> All contravariant functors are also invariant functors, even if that's of no practical use. That's just the way it is. This article explains how, and shows a few (useless) examples. </p> <p> I'll start with a few <a href="https://www.haskell.org/">Haskell</a> examples and then move on to showing the equivalent examples in C#. If you're unfamiliar with Haskell, you can skip that section. </p> <h3 id="61d5b82994f941db98cb933e6311d396"> Haskell package <a href="#61d5b82994f941db98cb933e6311d396" title="permalink">#</a> </h3> <p> For Haskell you can find an existing definition and implementations in the <a href="https://hackage.haskell.org/package/invariant">invariant</a> package. It already makes most 'common' contravariant functors <code>Invariant</code> instances, including <code>Predicate</code>, <code>Comparison</code>, and <code>Equivalence</code>. Here's an example of using <code>invmap</code> with a predicate. </p> <p> First, we need a predicate. Consider a function that evaluates whether a number is divisible by three: </p> <p> <pre>isDivisbleBy3 :: Integral a =&gt; a -&gt; Bool isDivisbleBy3 = (0 ==) . (`mod` 3)</pre> </p> <p> While this is already <a href="/2021/09/09/the-specification-contravariant-functor">conceptually a contravariant functor</a>, in order to make it an <code>Invariant</code> instance, we have to enclose it in the <code>Predicate</code> wrapper: </p> <p> <pre>ghci&gt; :t Predicate isDivisbleBy3 Predicate isDivisbleBy3 :: Integral a =&gt; Predicate a</pre> </p> <p> This is a predicate of some kind of integer. What if we wanted to know if a given duration represented a number of picoseconds divisible by three? Silly example, I know, but in order to demonstrate invariant mapping, we need types that are isomorphic, and <a href="https://hackage.haskell.org/package/time/docs/Data-Time-Clock.html#t:NominalDiffTime">NominalDiffTime</a> is isomorphic to a number of picoseconds via its <code>Enum</code> instance. </p> <p> <pre>p :: Enum a => Predicate a p = invmap toEnum fromEnum $ Predicate isDivisbleBy3</pre> </p> <p> In other words, it's possible to map the <code>Integral</code> predicate to an <code>Enum</code> predicate, and since <code>NominalDiffTime</code> is an <code>Enum</code> instance, you can now evaluate various durations: </p> <p> <pre>ghci&gt; (getPredicate p) $ secondsToNominalDiffTime 60 True ghci&gt; (getPredicate p) $ secondsToNominalDiffTime 61 False</pre> </p> <p> This is, as I've already announced, hardly useful, but it's still possible. Unless you have an API that <em>requires</em> an <code>Invariant</code> instance, it's also redundant, because you could just have used <code>contramap</code> with the predicate: </p> <p> <pre>ghci&gt; (getPredicate $ contramap fromEnum $ Predicate isDivisbleBy3) $ secondsToNominalDiffTime 60 True ghci&gt; (getPredicate $ contramap fromEnum $ Predicate isDivisbleBy3) $ secondsToNominalDiffTime 61 False</pre> </p> <p> When mapping a contravariant functor, only the contravariant mapping argument is required. The <code>Invariant</code> instances for <code>Contravariant</code> simply ignores the covariant mapping argument. </p> <h3 id="5a36c7d729f54ce98f21cd714f050d55"> Specification as an invariant functor in C# <a href="#5a36c7d729f54ce98f21cd714f050d55" title="permalink">#</a> </h3> <p> My earlier article <a href="/2021/09/09/the-specification-contravariant-functor">The Specification contravariant functor</a> takes a more object-oriented view on predicates by examining the <a href="https://en.wikipedia.org/wiki/Specification_pattern">Specification pattern</a>. </p> <p> As outlined in <a href="/2022/08/01/invariant-functors">the introduction</a>, while it's possible to add a method called <code>InvMap</code>, it'd be more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> to add a non-standard <code>Select</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;ISpecification&lt;T1&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;ISpecification&lt;T&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;T1&gt;&nbsp;tToT1, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;t1ToT) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.ContraMap(t1ToT); }</pre> </p> <p> This implementation ignores <code>tToT1</code> and delegates to the existing <code>ContraMap</code> method. </p> <p> Here's a unit test that demonstrates an example equivalent to the above Haskell example: </p> <p> <pre>[Theory] [InlineData(60,&nbsp;&nbsp;<span style="color:blue;">true</span>)] [InlineData(61,&nbsp;<span style="color:blue;">false</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;InvariantMappingExample(<span style="color:blue;">long</span>&nbsp;seconds,&nbsp;<span style="color:blue;">bool</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;ISpecification&lt;<span style="color:blue;">long</span>&gt;&nbsp;spec&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;IsDivisibleBy3Specification(); &nbsp;&nbsp;&nbsp;&nbsp;ISpecification&lt;TimeSpan&gt;&nbsp;mappedSpec&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spec.Select(ticks&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(ticks),&nbsp;ts&nbsp;=&gt;&nbsp;ts.Ticks); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expected, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mappedSpec.IsSatisfiedBy(TimeSpan.FromSeconds(seconds))); }</pre> </p> <p> Again, while this is hardly useful, it's possible. </p> <h3 id="d016726d91834c73998a7615aeab6c3a"> Conclusion <a href="#d016726d91834c73998a7615aeab6c3a" title="permalink">#</a> </h3> <p> All contravariant functors are invariant functors. You simply use the 'normal' contravariant mapping function (<code>contramap</code> in Haskell). This enables you to add an invariant mapping (<code>invmap</code>) that only uses the contravariant argument (<code>b -&gt; a</code>) and ignores the covariant argument (<code>a -&gt; b</code>). </p> <p> Invariant functors are, however, not particularly useful, so neither is this result. Still, it's there, so deserves a mention. Enough of that, though. </p> <p> <strong>Next:</strong> <a href="/2022/03/28/monads">Monads</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Built-in alternatives to applicative assertions https://blog.ploeh.dk/2023/01/30/built-in-alternatives-to-applicative-assertions 2023-01-30T08:08:00+00:00 Mark Seemann <div id="post"> <p> <em>Why make things so complicated?</em> </p> <p> Several readers reacted to my small article series on <a href="/2022/11/07/applicative-assertions">applicative assertions</a>, pointing out that error-collecting assertions are already supported in more than one unit-testing framework. </p> <blockquote> <p> "In the Java world this seems similar to the result gained by Soft Assertions in AssertJ. <a href="https://assertj.github.io/doc/#assertj-core-soft-assertions">https://assertj.github.io/doc/#assertj-c...</a> if you’re after a target for functionality (without the adventures through monad land)" </p> <footer><cite><a href="https://twitter.com/joshuamck/status/1597190184134590464">Josh McK</a></cite></footer> </blockquote> <p> While I'm not familiar with the details of Java unit-testing frameworks, the situation is similar in .NET, it turns out. </p> <blockquote> <p> "Did you know there is Assert.Multiple in NUnit and now also in xUnit .Net? It seems to have quite an overlap with what you're doing here. </p> <p> "For a quick overview, I found this blogpost helpful: <a href="https://www.thomasbogholm.net/2021/11/25/xunit-2-4-2-pre-multiple-asserts-in-one-test/">https://www.thomasbogholm.net/2021/11/25/xunit-2-4-2-pre-multiple-asserts-in-one-test/</a>" </p> <footer><cite><a href="https://twitter.com/DoCh_Dev/status/1597158737357459456">DoCh_Dev</a></cite></footer> </blockquote> <p> I'm not surprised to learn that something like this exists, but let's take a quick look. </p> <h3 id="d7c0c78093084c08aa22ccaa7b86cb8a"> NUnit Assert.Multiple <a href="#d7c0c78093084c08aa22ccaa7b86cb8a" title="permalink">#</a> </h3> <p> Let's begin with <a href="https://nunit.org/">NUnit</a>, as this seems to be the first .NET unit-testing framework to support error-collecting assertions. As a beginning, the <a href="https://docs.nunit.org/articles/nunit/writing-tests/assertions/multiple-asserts.html">documentation example</a> works as it's supposed to: </p> <p> <pre>[Test] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ComplexNumberTest() { &nbsp;&nbsp;&nbsp;&nbsp;ComplexNumber&nbsp;result&nbsp;=&nbsp;SomeCalculation(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Multiple(()&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.AreEqual(5.2,&nbsp;result.RealPart,&nbsp;<span style="color:#a31515;">&quot;Real&nbsp;part&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.AreEqual(3.9,&nbsp;result.ImaginaryPart,&nbsp;<span style="color:#a31515;">&quot;Imaginary&nbsp;part&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> When you run the test, it fails (as expected) with this error message: </p> <p> <pre>Message: &nbsp;&nbsp;Multiple failures or warnings in test: &nbsp;&nbsp;&nbsp;&nbsp;1) Real part &nbsp;&nbsp;&nbsp;&nbsp;Expected: 5.2000000000000002d &nbsp;&nbsp;&nbsp;&nbsp;But was: 5.0999999999999996d &nbsp;&nbsp;&nbsp;&nbsp;2) Imaginary part &nbsp;&nbsp;&nbsp;&nbsp;Expected: 3.8999999999999999d &nbsp;&nbsp;&nbsp;&nbsp;But was: 4.0d</pre> </p> <p> That seems to work well enough, but how does it actually work? I'm not interested in reading the NUnit source code - after all, the concept of <a href="/encapsulation-and-solid">encapsulation</a> is that one should be able to make use of the capabilities of an object without knowing all implementation details. Instead, I'll guess: Perhaps <code>Assert.Multiple</code> executes the code block in a <code>try/catch</code> block and collects the various exceptions thrown by the nested assertions. </p> <p> Does it catch all exception types, or only a subset? </p> <p> Let's try with the kind of composed assertion that I <a href="/2022/11/28/an-initial-proof-of-concept-of-applicative-assertions-in-c">previously investigated</a>: </p> <p> <pre>[Test] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;HttpExample() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;deleteResp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpResponseMessage(HttpStatusCode.BadRequest); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;getResp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpResponseMessage(HttpStatusCode.OK); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Multiple(()&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deleteResp.EnsureSuccessStatusCode(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.That(getResp.StatusCode,&nbsp;Is.EqualTo(HttpStatusCode.NotFound)); &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> This test fails (again, as expected). What's the error message? </p> <p> <pre>Message: &nbsp;&nbsp;System.Net.Http.HttpRequestException :↩ &nbsp;&nbsp;&nbsp;&nbsp;Response status code does not indicate success: 400 (Bad Request).</pre> </p> <p> (I've wrapped the result over multiple lines for readability. The <code>↩</code> symbol indicates where I've wrapped the text. I'll do that again later in this article.) </p> <p> Notice that I'm using <a href="/2020/09/28/ensuresuccessstatuscode-as-an-assertion">EnsureSuccessStatusCode as an assertion</a>. This seems to spoil the behaviour of <code>Assert.Multiple</code>. It only reports the first status code error, but not the second one. </p> <p> I admit that I don't fully understand what's going on here. In fact, I <em>have</em> taken a cursory glance at the relevant NUnit source code without being enlightened. </p> <p> One hypothesis might be that NUnit assertions throw special <code>Exception</code> sub-types that <code>Assert.Multiple</code> catch. In order to test that, I wrote a few more tests in <a href="https://fsharp.org/">F#</a> with <a href="http://www.swensensoftware.com/unquote/">Unquote</a>, assuming that, since Unquote hardly throws NUnit exceptions, the behaviour might be similar to above. </p> <p> <pre>[&lt;Test&gt;] <span style="color:blue;">let</span>&nbsp;Test4&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;x&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;y&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;z&nbsp;=&nbsp;3 &nbsp;&nbsp;&nbsp;&nbsp;Assert.Multiple&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=!&nbsp;y &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;=!&nbsp;z)</pre> </p> <p> The <code>=!</code> operator is an Unquote operator that I usually read as <em>must equal</em>. How does that error message look? </p> <p> <pre>Message: &nbsp;&nbsp;Multiple failures or warnings in test: &nbsp;&nbsp;&nbsp;&nbsp;1) &nbsp;&nbsp;1 = 2 &nbsp;&nbsp;false &nbsp;&nbsp;&nbsp;&nbsp;2) &nbsp;&nbsp;2 = 3 &nbsp;&nbsp;false</pre> </p> <p> Somehow, <code>Assert.Multiple</code> understands Unquote error messages, but not <code>HttpRequestException</code>. As I wrote, I don't fully understand why it behaves this way. To a degree, I'm intellectually curious enough that I'd like to know. On the other hand, from a maintainability perspective, as a user of NUnit, I shouldn't have to understand such details. </p> <h3 id="1f28e534b0f94e93bb12cbb951fa663f"> xUnit.net Assert.Multiple <a href="#1f28e534b0f94e93bb12cbb951fa663f" title="permalink">#</a> </h3> <p> How fares the <a href="https://xunit.net/">xUnit.net</a> port of <code>Assert.Multiple</code>? </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;HttpExample() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;deleteResp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpResponseMessage(HttpStatusCode.BadRequest); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;getResp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpResponseMessage(HttpStatusCode.OK); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Multiple( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;()&nbsp;=&gt;&nbsp;deleteResp.EnsureSuccessStatusCode(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;()&nbsp;=&gt;&nbsp;Assert.Equal(HttpStatusCode.NotFound,&nbsp;getResp.StatusCode)); }</pre> </p> <p> The API is, you'll notice, not quite identical. Where the NUnit <code>Assert.Multiple</code> method takes a single delegate as input, the xUnit.net method takes an array of actions. The difference is not only at the level of API; the behaviour is different, too: </p> <p> <pre>Message: &nbsp;&nbsp;Multiple failures were encountered: &nbsp;&nbsp;---- System.Net.Http.HttpRequestException :↩ &nbsp;&nbsp;Response status code does not indicate success: 400 (Bad Request). &nbsp;&nbsp;---- Assert.Equal() Failure &nbsp;&nbsp;Expected: NotFound &nbsp;&nbsp;Actual: OK</pre> </p> <p> This error message reports both problems, as we'd like it to do. </p> <p> I also tried writing equivalent tests in F#, with and without Unquote, and they behave consistently with this result. </p> <p> If I had to use something like <code>Assert.Multiple</code>, I'd trust the xUnit.net variant more than NUnit's implementation. </p> <h3 id="c51fb932618c4464a2e9be05869005d4"> Assertion scopes <a href="#c51fb932618c4464a2e9be05869005d4" title="permalink">#</a> </h3> <p> Apparently, <a href="https://fluentassertions.com/">Fluent Assertions</a> offers yet another alternative. </p> <blockquote> <p> "Hey @ploeh, been reading your applicative assertion series. I recently discovered Assertion Scopes, so I'm wondering what is your take on them since it seems to me they are solving this problem in C# already. <a href="https://fluentassertions.com/introduction#assertion-scopes">https://fluentassertions.com/introduction#assertion-scopes</a>" </p> <footer><cite><a href="https://twitter.com/JernejGoricki/status/1597704973839904768">Jernej Gorički</a></cite></footer> </blockquote> <p> The linked documentation contains this example: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;DocExample() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">new</span>&nbsp;AssertionScope()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.Should().Be(10); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Actual&quot;</span>.Should().Be(<span style="color:#a31515;">&quot;Expected&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> It fails in the expected manner: </p> <p> <pre>Message: &nbsp;&nbsp;Expected value to be 10, but found 5 (difference of -5). &nbsp;&nbsp;Expected string to be "Expected" with a length of 8, but "Actual" has a length of 6,↩ &nbsp;&nbsp;&nbsp;&nbsp;differs near "Act" (index 0).</pre> </p> <p> How does it fare when subjected to the <code>EnsureSuccessStatusCode</code> test? </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;HttpExample() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;deleteResp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpResponseMessage(HttpStatusCode.BadRequest); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;getResp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpResponseMessage(HttpStatusCode.OK); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">new</span>&nbsp;AssertionScope()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deleteResp.EnsureSuccessStatusCode(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getResp.StatusCode.Should().Be(HttpStatusCode.NotFound); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> That test produces this error output: </p> <p> <pre>Message: &nbsp;&nbsp;System.Net.Http.HttpRequestException :↩ &nbsp;&nbsp;&nbsp;&nbsp;Response status code does not indicate success: 400 (Bad Request).</pre> </p> <p> Again, <code>EnsureSuccessStatusCode</code> prevents further assertions from being evaluated. I can't say that I'm that surprised. </p> <h3 id="2f325809027243889d703e927a115efc"> Implicit or explicit <a href="#2f325809027243889d703e927a115efc" title="permalink">#</a> </h3> <p> You might protest that using <code>EnsureSuccessStatusCode</code> and treating the resulting <code>HttpRequestException</code> as an assertion is unfair and unrealistic. Possibly. As usual, such considerations are subject to a multitude of considerations, and there's no one-size-fits-all answer. </p> <p> My intent with this article isn't to attack or belittle the APIs I've examined. Rather, I wanted to explore their boundaries by stress-testing them. That's one way to gain a better understanding. Being aware of an API's limitations and quirks can prevent subtle bugs. </p> <p> Even if you'd <em>never</em> use <code>EnsureSuccessStatusCode</code> as an assertion, perhaps you or a colleague might inadvertently do something to the same effect. </p> <p> I'm not surprised that both NUnit's <code>Assert.Multiple</code> and Fluent Assertions' <code>AssertionScope</code> behaves in a less consistent manner than xUnit.net's <code>Assert.Multiple</code>. The clue is in the API. </p> <p> The xUnit.net API looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Multiple</span>(<span style="color:blue;">params</span>&nbsp;Action[]&nbsp;<span style="color:#1f377f;">checks</span>)</pre> </p> <p> Notice that each assertion is explicitly a separate action. This enables the implementation to isolate it and treat it independently of other actions. </p> <p> Neither the NUnit nor the Fluent Assertions API is that explicit. Instead, you can write arbitrary code inside the 'scope' of multiple assertions. For <code>AssertionScope</code>, the notion of a 'scope' is plain to see. For the NUnit API it's more implicit, but the scope is effectively the extent of the method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Multiple</span>(TestDelegate&nbsp;<span style="color:#1f377f;">testDelegate</span>)</pre> </p> <p> That <code>testDelegate</code> can have as many (nested, even) assertions as you'd like, so the <code>Multiple</code> implementation needs to somehow demarcate when it begins and when it ends. </p> <p> The <code>testDelegate</code> can be implemented in a different file, or even in a different library, and it has no way to communicate or coordinate with its surrounding scope. This reminds me of an Ambient Context, an idiom that <a href="/2019/01/21/some-thoughts-on-anti-patterns">Steven van Deursen convinced me was an anti-pattern</a>. The surrounding context changes the behaviour of the code block it surrounds, and it's quite implicit. </p> <blockquote> <p> Explicit is better than implicit. </p> <footer><cite>Tim Peters, <a href="https://peps.python.org/pep-0020/">The Zen of Python</a></cite></footer> </blockquote> <p> The xUnit.net API, at least, looks a bit saner. Still, this kind of API is quirky enough that it reminds me of <a href="https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule">Greenspun's tenth rule</a>; that these APIs are ad-hoc, informally-specified, bug-ridden, slow implementations of half of <a href="/2018/10/01/applicative-functors">applicative functors</a>. </p> <h3 id="95b723f05a7f4b4c88b5e716f5f82989"> Conclusion <a href="#95b723f05a7f4b4c88b5e716f5f82989" title="permalink">#</a> </h3> <p> Not surprisingly, popular unit-testing and assertion libraries come with facilities to compose assertions. Also, not surprisingly, these APIs are crude and require you to learn their implementation details. </p> <p> Would I use them if I had to? I probably would. As <a href="https://www.infoq.com/presentations/Simple-Made-Easy/">Rich Hickey put it</a>, they're already <em>at hand</em>. That makes them easy, but not necessarily simple. APIs that compel you to learn their internal implementation details aren't simple. </p> <p> <a href="/2017/10/04/from-design-patterns-to-category-theory">Universal abstractions</a>, on the other hand, you only have to learn one time. Once you understand what an applicative functor is, you know what to expect from it, and which capabilities it has. </p> <p> In languages with good support for applicative functors, I would favour an assertion API based on that abstraction, if given a choice. At the moment, though, that's not much of an option. Even <a href="https://hackage.haskell.org/package/HUnit/docs/Test-HUnit-Base.html#t:Assertion">HUnit assertions</a> are based on side effects. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e3269279066146f985c8405f6d3ad286"> <div class="comment-author">Joker_vD <a href="#e3269279066146f985c8405f6d3ad286">#</a></div> <div class="comment-content"> <p> Just a reminder: in .NET, method's execution <b>cannot</b> be resumed after an exception is thrown, there is just simply no way to do this, at all. Which means that NUnit's Assert.Multiple absolutely cannot work the way you guess it probably does, by running the delegate and resuming its execution after it throws an exception until the delegate returns. </p> <p> How could it work then? Well, considering that documentation to almost every Assert's method has "Returns without throwing an exception when inside a multiple assert block" line in it, I would assume that Assert.Multiple sets a global flag which makes actual assertions to store the failures in some global hidden context instead on throwing them, then runs the delegate and after it finishes or throws, collects and clears all those failures from the context and resets the global flag. </p> <p> Cursory inspection of NUnit's source code supports this idea, except that apparently it's not just a boolean flag but a "depth" counter; and assertions report the failures <a href="https://github.com/nunit/nunit/blob/62059054137de84b711353765d474779db95f731/src/NUnitFramework/framework/Assert.cs#L371">just the way I've speculated</a>. I personally hate such side-channels but you have to admit, they allow for some nifty, seemingly impossible magical tricks (a.k.a. "spooky action at the distance"). </p> <p> Also, why do you assume that Unquote would not throw NUnit's assertions? It literally has "Unquote integrates configuration-free with all exception-based unit testing frameworks including xUnit.net, NUnit, MbUnit, Fuchu, and MSTest" in its README, and indeed, if you look at <a href="https://github.com/SwensenSoftware/unquote/blob/78b071043c42372f3693a07e5562520046873ebc/src/Unquote/Assertions.fs">its source code</a>, you'll see that at runtime it tries to locate any testing framework it's aware of and use its assertions. More funny party tricks, this time with reflection! </p> <p> I understand that after working in more pure/functional programming environments one does start to slowly forget about those terrible things, but: those horrorterrors <i>still</i> exist, and people <i>keep making</i> more of them. Now, if you can, have a good night :) </p> </div> <div class="comment-date">2023-01-31 03:00 UTC</div> </div> <div class="comment" id="e0e7c5b258d54c30b87f157e8746150d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e0e7c5b258d54c30b87f157e8746150d">#</a></div> <div class="comment-content"> <p> Joker_vD, thank you for explaining those details. I admit that I hadn't thought too deeply about implementation details, for the reasons I briefly mentioned in the post. </p> <blockquote> <p> "I understand that after working in more pure/functional programming environments one does start to slowly forget about those terrible things" </p> </blockquote> <p> Yes, that summarises my current thinking well, I'm afraid. </p> </div> <div class="comment-date">2023-01-30 6:49 UTC</div> </div> <div class="comment" id="821541be129a4ea7976ab33f71d3637a"> <div class="comment-author"><a href="https://github.com/MaxKot">Max Kiselev</a> <a href="#821541be129a4ea7976ab33f71d3637a">#</a></div> <div class="comment-content"> <p> NUnit has <a href="https://docs.nunit.org/articles/nunit/writing-tests/assertions/classic-assertions/Assert.DoesNotThrow.html">Assert.DoesNotThrow</a> and Fluent Assertions has <a href="https://fluentassertions.com/exceptions/">.Should().NotThrow()</a>. I did not check Fluent Assertions, but NUnit does gather failures of Assert.DoesNotThrow inside Assert.Multiple into a multi-error report. One might argue that asserting that a delegate should not throw is another application of the "explicit is better than implicit" philosophy. Here's what Fluent Assertions has to say on that matter: </p> <blockquote> <p> "We know that a unit test will fail anyhow if an exception was thrown, but this syntax returns a clearer description of the exception that was thrown and fits better to the AAA syntax." </p> </blockquote> <p> As a side note, you might also want to take a look on NUnits Assert.That syntax. It allows to construct complex conditions tested against a single actual value: </p> <p> <pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;3; Assert.That&nbsp;(actual,&nbsp;Is.GreaterThan&nbsp;(0).And.LessThanOrEqualTo&nbsp;(2).And.Matches&nbsp;(Has.Property&nbsp;(<span style="color:#a31515;">&quot;P&quot;</span>).EqualTo&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>)));</pre> </p> <p> A failure is then reported like this: </p> <p> <pre>Expected: greater than 0 and less than or equal to 2 and property P equal to "a" But was: 3</pre> </p> </div> <div class="comment-date">2023-01-31 18:35 UTC</div> </div> <div class="comment" id="815b4f0e18284dccb3ce38dbb476eb4d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#815b4f0e18284dccb3ce38dbb476eb4d">#</a></div> <div class="comment-content"> <p> Max, thank you for writing. I have to admit that I never understood the point of <a href="https://docs.nunit.org/articles/nunit/writing-tests/assertions/assertion-models/constraint.html">NUnit's constraint model</a>, but your example clearly illustrates how it may be useful. It enables you to compose assertions. </p> <p> It's interesting to try to understand the underlying reason for that. I took a cursory glance at that <code>IResolveConstraint</code> API, and as far as I can tell, it may form a <a href="/2017/10/06/monoids">monoid</a> (I'm not entirely sure about the <code>ConstraintStatus</code> enum, but even so, it may be 'close enough' to be composable). </p> <p> I can see how that may be useful when making assertions against complex objects (i.e. object composed from other objects). </p> <p> In xUnit.net you'd typically address that problem with custom <a href="https://learn.microsoft.com/dotnet/api/system.collections.generic.iequalitycomparer-1">IEqualityComparers</a>. This is more verbose, but also strikes me as more reusable. One disadvantage of that approach, however, is that when tests fail, the assertion message is typically useless. </p> <p> This is the reason I favour Unquote: Instead of inventing a Boolean algebra(?) from scratch, it uses the existing language and still gives you good error messages. Alas, that only works in F#. </p> <p> In general, though, I'm inclined to think that all of these APIs address symptoms rather than solve real problems. Granted, they're useful whenever you need to make assertions against values that you don't control, but for your own APIs, <a href="/2021/05/03/structural-equality-for-better-tests">a simpler solution is to model values as immutable data with structural equality</a>. </p> <p> Another question is whether aiming for clear assertion messages is optimising for the right concern. At least with TDD, <a href="/2022/12/12/when-do-tests-fail">I don't think that it is</a>. </p> </div> <div class="comment-date">2023-02-02 7:53 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Agilean https://blog.ploeh.dk/2023/01/23/agilean 2023-01-23T07:55:00+00:00 Mark Seemann <div id="post"> <p> <em>There are other agile methodologies than scrum.</em> </p> <p> More than twenty years after <a href="https://agilemanifesto.org/">the Agile Manifesto</a> it looks as though there's only one kind of agile process left: <a href="https://en.wikipedia.org/wiki/Scrum_(software_development)">Scrum</a>. </p> <p> I recently held a workshop and as a side remark I mentioned that I don't consider scrum the best development process. This surprised some attendees, who politely inquired about my reasoning. </p> <h3 id="8837c4f67d694f93ad0b708cc1739705"> My experience with scrum <a href="#8837c4f67d694f93ad0b708cc1739705" title="permalink">#</a> </h3> <p> The first nine years I worked as a professional programmer, the companies I worked in used various <a href="https://en.wikipedia.org/wiki/Waterfall_model">waterfall</a> processes. When I joined the Microsoft Dynamics Mobile team in 2008 they were already using scrum. That was my first exposure to it, and I liked it. Looking back on it today, we weren't particular dogmatic about the process, being more interested in getting things done. </p> <p> One telling fact is that we took turns being Scrum Master. Every sprint we'd rotate that role. </p> <p> We did test-driven development, and had two-week sprints. This being a Microsoft development organisation, we had a dedicated build master, tech writers, specialised testers, and security reviews. </p> <p> I liked it. It's easily one of the most professional software organisations I've worked in. I think it was a good place to work for many reasons. Scrum may have been a contributing factor, but hardly the only reason. </p> <p> I have no issues with scrum as we practised it then. I recall later attending a presentation by <a href="https://en.wikipedia.org/wiki/Mike_Cohn">Mike Cohn</a> where he outlined four quadrants of team maturity. You'd start with scrum, but use retrospectives to evaluate what worked and what didn't. Then you'd adjust. A mature, self-organising team would arrive at its own process, perhaps initiated with scrum, but now having little resemblance with it. </p> <p> I like scrum when viewed like that. When it becomes rigid and empty ceremony, I don't. If all you do is daily stand-ups, sprints, and backlogs, you may be doing scrum, but probably not agile. </p> <h3 id="1bb8e521e78341768ca7f5942f3ace4a"> Continuous deployment <a href="#1bb8e521e78341768ca7f5942f3ace4a" title="permalink">#</a> </h3> <p> After Microsoft I joined a startup so small that formal process was unnecessary. Around that time I also became interested in <a href="https://en.wikipedia.org/wiki/Lean_software_development">lean software development</a>. In the beginning, I learned a lot from <a href="https://www.linkedin.com/in/martin-jul-39a12/">Martin Jul</a> who seemed to use the now-defunct <a href="https://ative.dk/">Ative</a> blog as a public notepad as he was reading works of <a href="https://en.wikipedia.org/wiki/W._Edwards_Deming">Deming</a>. I suppose, if you want a more canonical introduction to the topic, that you might start with one of <a href="http://www.poppendieck.com/">the Poppendiecks'</a> books, but since I've only read <a href="/ref/implementing-lean">Implementing Lean Software Development</a>, that's the only one I can recommend. </p> <p> Around 2014 I returned to a regular customer. The team had, in my absence, been busy implementing <a href="https://en.wikipedia.org/wiki/Continuous_deployment">continuous deployment</a>. Instead of artificial periods like 'sprints' we had a <a href="https://en.wikipedia.org/wiki/Kanban_board">kanban board</a> to keep track of our work. We used a variation of <a href="https://en.wikipedia.org/wiki/Feature_toggle">feature flags</a> and marked features as done when they were complete and in production. </p> <p> Why wait until <em>next</em> Friday if the feature is <em>done, done</em> on a Wednesday? Why wait until the <em>next</em> Monday to identify what to work on next, if you're ready to take on new work on a Thursday? Why not move towards <em>one-piece flow?</em> </p> <p> An effective self-organising team typically already knows what it's doing. Much process is introduced in order to give external stakeholders visibility into what a team is doing. </p> <p> I found, in that organisation, that continuous deployment eliminated most of that need. At one time I asked a stakeholder what he thought of the feature I'd deployed a week before - a feature that <em>he had requested</em>. He replied that he hadn't had time to look at it yet. </p> <p> The usual inquires about status (<em>Is it done yet? When is it done?</em>) were gone. The team moved faster than the stakeholders could keep up. That also gave us <a href="/2022/09/19/when-to-refactor">enough slack to keep the code base in good order</a>. We also used test-driven development throughout (TDD). </p> <p> TDD with continuous deployment and a kanban board strikes me as congenial with the ideas of lean software development, but that's not all. </p> <h3 id="9f5e725164f54d32b5d56a28d3b1619e"> Stop-the-line issues <a href="#9f5e725164f54d32b5d56a28d3b1619e" title="permalink">#</a> </h3> <p> An <a href="https://en.wikipedia.org/wiki/Andon_(manufacturing)">andon cord</a> is a central concept in <a href="https://en.wikipedia.org/wiki/Lean_manufacturing">lean manufactoring</a>. If a worker (or anyone, really) discovers a problem during production, he or she pulls the andon cord and <em>stops the production line</em>. Then everyone investigates and determines what to do about the problem. Errors are not allowed to accumulate. </p> <p> I think that I've internalised this notion to such a degree that I only recently connected it to lean software development. </p> <p> In <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, I recommend turning compiler warnings into errors at the beginning of a code base. Don't allow warnings to pile up. Do the same with static code analysis and linters. </p> <p> When discussing software engineering with developers, I'm beginning to realise that this runs even deeper. </p> <ul> <li>Turn warnings into errors. Don't allow warnings to accumulate.</li> <li>The correct number of unhandled exceptions in production is zero. If you observe an unhandled exception in your production logs, fix it. Don't let them accumulate.</li> <li>The correct number of known bugs is zero. Don't let bugs accumulate.</li> </ul> <p> If you're used to working on a code base with hundreds of known bugs, and frequent exceptions in production, this may sound unrealistic. If you deal with issues as soon as they arise, however, this is not only possible - it's faster. </p> <p> In lean software development, bugs are stop-the-line issues. When something unexpected happens, you stop what you're doing and make fixing the problem the top priority. You build quality in. </p> <p> This has been my modus operandi for years, but I only recently connected the dots to realise that this is a typical lean practice. I may have picked it up from there. Or perhaps it's just common sense. </p> <h3 id="e2513b8e32b148cbab8dbd6b75c67348"> Conclusion <a href="#e2513b8e32b148cbab8dbd6b75c67348" title="permalink">#</a> </h3> <p> When Agile was new and exciting, there were <a href="https://en.wikipedia.org/wiki/Extreme_programming">extreme programming</a> and scrum, and possibly some lesser known techniques. Lean was around the corner, but didn't come to my attention, at least, until around 2010. Then it seems to have faded away again. </p> <p> Today, agile looks synonymous with scrum, but I find lean software development more efficient. Why divide work into artificial time periods when you can release continuously? Why <em>plan</em> bug fixing when it's more efficient to stop the line and deal with the problem as it arises? </p> <p> That may sound counter-intuitive, but it works because it prevents technical debt from accumulating. </p> <p> Lean software development is, in my experience, a better agile methodology than scrum. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. In the long run https://blog.ploeh.dk/2023/01/16/in-the-long-run 2023-01-16T08:28:00+00:00 Mark Seemann <div id="post"> <p> <em>Software design decisions should be time-aware.</em> </p> <p> A common criticism of modern capitalism is that maximising <a href="https://en.wikipedia.org/wiki/Shareholder_value">shareholder value</a> leads to various detrimental outcomes, both societal, but possibly also for the maximising organisation itself. One major problem is when company leadership is incentivised to optimise stock market price for the next quarter, or other short terms. When considering only the short term, decision makers may (rationally) decide to sacrifice long-term benefits for short-term gains. </p> <p> We often see similar behaviour in democracies. Politicians tend to optimise within a time frame that coincides with the election period. Getting re-elected is more important than good policy in the next period. </p> <p> These observations are crude generalisations. Some democratic politicians and CEOs take longer views. Inherent in the context, however, is an incentive to short-term thinking. </p> <p> This, it strikes me, is frequently the case in software development. </p> <p> Particularly in the context of <a href="https://en.wikipedia.org/wiki/Scrum_(software_development)">scrum</a> there's a focus on delivering at the end of every sprint. I've observed developers and other stakeholders together engage in short-term thinking in order to meet those arbitrary and fictitious deadlines. </p> <p> Even when deadlines are more remote than two weeks, project members rarely think beyond some perceived end date. As I describe in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, a <em>project</em> is rarely is good way to organise software development work. Projects end. Successful software doesn't. </p> <p> Regardless of the specific circumstances, a too myopic focus on near-term goals gives you an incentive to cut corners. To not care about code quality. </p> <h3 id="5cd528d178504f2ba849019e5d6d425e"> ...we're all dead <a href="#5cd528d178504f2ba849019e5d6d425e" title="permalink">#</a> </h3> <p> As <a href="https://en.wikipedia.org/wiki/John_Maynard_Keynes">Keynes</a> once quipped: </p> <blockquote> <p> "In the long run we are all dead." </p> <footer><cite>John Maynard Keynes</cite></footer> </blockquote> <p> Clearly, while you can be too short-sighted, you can also take too long a view. Sometimes deadlines matter, and software not used makes no-one happy. </p> <p> Working software remains the ultimate test of value, but as I've tried to express many times before, this does <em>not</em> imply that anything else is worthless. </p> <p> You can't measure code quality. <a href="/2019/03/04/code-quality-is-not-software-quality">Code quality isn't software quality</a>. Low code quality <a href="https://martinfowler.com/articles/is-quality-worth-cost.html">slows you down</a>, and that, eventually, costs you money, blood, sweat, and tears. </p> <p> This is, however, not difficult to predict. All it takes is a slightly wider time horizon. Consider the impact of your decisions past the next deadline. </p> <h3 id="b0f0f05bd00142379c2d713adde6eb77"> Conclusion <a href="#b0f0f05bd00142379c2d713adde6eb77" title="permalink">#</a> </h3> <p> Don't be too short-sighted, but don't forget the immediate value of what you do. Your <a href="/2019/03/18/the-programmer-as-decision-maker">decisions</a> matter. The impact is not always immediate. Consider what consequences short-term optimisations may have in a longer perspective. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The IO monad https://blog.ploeh.dk/2023/01/09/the-io-monad 2023-01-09T07:39:00+00:00 Mark Seemann <div id="post"> <p> <em>The IO container forms a monad. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. A previous article described <a href="/2020/06/22/the-io-functor">the IO functor</a>. As is the case with many (but not all) <a href="/2018/03/22/functors">functors</a>, this one also forms a monad. </p> <h3 id="bee075f5bf474b099146cbc7c96f4888"> SelectMany <a href="#bee075f5bf474b099146cbc7c96f4888" title="permalink">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function. In C#, monadic bind is called <code>SelectMany</code>. In a recent article, I gave an example of <a href="/2020/06/15/io-container-in-a-parallel-c-universe">what <em>IO</em> might look like in C#</a>. Notice that it already comes with a <code>SelectMany</code> function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IO&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(Func&lt;T,&nbsp;IO&lt;TResult&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>)</pre> </p> <p> Unlike other monads, the IO implementation is considered a black box, but if you're interested in a prototypical implementation, I already posted <a href="/2020/07/13/implementation-of-the-c-io-container">a sketch</a> in 2020. </p> <h3 id="1ba6a4e6da014fc3a511e71bdba4f0a9"> Query syntax <a href="#1ba6a4e6da014fc3a511e71bdba4f0a9" title="permalink">#</a> </h3> <p> I have also, already, demonstrated <a href="/2020/06/29/syntactic-sugar-for-io">syntactic sugar for IO</a>. In that article, however, I used an implementation of the required <code>SelectMany</code> overload that is more explicit than it has to be. The <a href="/2022/03/28/monads">monad introduction</a> makes the prediction that you can always implement that overload in the same way, and yet here I didn't. </p> <p> That's an oversight on my part. You can implement it like this instead: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IO&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IO&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;IO&lt;U&gt;&gt;&nbsp;<span style="color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;U,&nbsp;TResult&gt;&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> Indeed, the conjecture from the introduction still holds. </p> <h3 id="f234f9e9d3724a30b8465d176a0d98a6"> Join <a href="#f234f9e9d3724a30b8465d176a0d98a6" title="permalink">#</a> </h3> <p> In <a href="/2022/03/28/monads">the introduction</a> you learned that if you have a <code>Flatten</code> or <code>Join</code> function, you can implement <code>SelectMany</code>, and the other way around. Since we've already defined <code>SelectMany</code> for <code>IO&lt;T&gt;</code>, we can use that to implement <code>Join</code>. In this article I use the name <code>Join</code> rather than <code>Flatten</code>. This is an arbitrary choice that doesn't impact behaviour. Perhaps you find it confusing that I'm inconsistent, but I do it in order to demonstrate that the behaviour is the same even if the name is different. </p> <p> The concept of a monad is universal, but the names used to describe its components differ from language to language. What C# calls <code>SelectMany</code>, Scala calls <code>flatMap</code>, and what <a href="https://www.haskell.org/">Haskell</a> calls <code>join</code>, other languages may call <code>Flatten</code>. </p> <p> You can always implement <code>Join</code> by using <code>SelectMany</code> with the identity function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IO&lt;T&gt;&nbsp;<span style="color:#74531f;">Join</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;IO&lt;IO&lt;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> In C# the identity function is <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatically</a> given as the lambda expression <code><span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x</code> since C# doesn't come with a built-in identity function. </p> <h3 id="c57bcf1e74144046bc7191280d78519b"> Return <a href="#c57bcf1e74144046bc7191280d78519b" title="permalink">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that Haskell uses). In <a href="/2020/06/22/the-io-functor">the IO functor</a> article, I wrote that the <code>IO&lt;T&gt;</code> constructor corresponds to <em>return</em>. That's not strictly true, though, since the constructor takes a <code>Func&lt;T&gt;</code> and not a <code>T</code>. </p> <p> This issue is, however, trivially addressed: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IO&lt;T&gt;&nbsp;<span style="color:#74531f;">Return</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;IO&lt;T&gt;(()&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> Take the value <code>x</code> and wrap it in a lazily-evaluated function. </p> <h3 id="e0548aa855224081a5c1c3ec80f23951"> Laws <a href="#e0548aa855224081a5c1c3ec80f23951" title="permalink">#</a> </h3> <p> While <a href="/2020/07/06/referential-transparency-of-io">IO values are referentially transparent</a> you can't compare them. You also can't 'run' them by other means than running a program. This makes it hard to talk meaningfully about the <a href="/2022/04/11/monad-laws">monad laws</a>. </p> <p> For example, the left identity law is: </p> <p> <pre>return &gt;=&gt; h ≡ h</pre> </p> <p> Note the implied equality. The composition of <code>return</code> and <code>h</code> should be equal to <code>h</code>, for some reasonable definition of equality. How do we define that? </p> <p> Somehow we must imagine that two alternative compositions would produce the same observable effects <a href="https://en.wikipedia.org/wiki/Ceteris_paribus">ceteris paribus</a>. If you somehow imagine that you have two parallel universes, one with one composition (say <code>return &gt;=&gt; h</code>) and one with another (<code>h</code>), if all else in those two universes were equal, then you would observe no difference in behaviour. </p> <p> That may be useful as a thought experiment, but isn't particularly practical. Unfortunately, due to side effects, things <em>do</em> change when non-deterministic behaviour and side effects are involved. As a simple example, consider an IO action that gets the current time and prints it to the console. That involves both non-determinism and a side effect. </p> <p> In Haskell, that's a straightforward composition of two <code>IO</code> actions: </p> <p> <pre>&gt; h () = getCurrentTime &gt;>= print</pre> </p> <p> How do we compare two compositions? By running them? </p> <p> <pre>&gt; return () &gt;&gt;= h 2022-06-25 16:47:30.6540847 UTC &gt; h () 2022-06-25 16:47:37.5281265 UTC</pre> </p> <p> The outputs are not the same, because time goes by. Can we thereby conclude that the monad laws don't hold for IO? Not quite. </p> <p> The IO Container is referentially transparent, but evaluation isn't. Thus, we have to pretend that two alternatives will lead to the same evaluation behaviour, all things being equal. </p> <p> This property seems to hold for both the identity and associativity laws. Whether or not you compose with <em>return</em>, or in which evaluation order you compose actions, it doesn't affect the outcome. </p> <p> For completeness sake, the <a href="/2020/07/13/implementation-of-the-c-io-container">C# implementation sketch</a> is just a wrapper over a <code>Func&lt;T&gt;</code>. We can also think of such a function as a function from <a href="/2018/01/15/unit-isomorphisms">unit</a> to <code>T</code> - in pseudo-C# <code>() =&gt; T</code>. That's a function; in other words: <a href="/2022/11/14/the-reader-monad">The Reader monad</a>. We already know that the Reader monad obeys the monad laws, so the C# implementation, at least, should be okay. </p> <h3 id="74154d1a95a44a91a8d534fbfda59d8d"> Conclusion <a href="#74154d1a95a44a91a8d534fbfda59d8d" title="permalink">#</a> </h3> <p> IO forms a monad, among other abstractions. This is what enables Haskell programmers to compose an arbitrary number of impure actions with monadic bind without ever having to force evaluation. In C# <a href="/2020/06/15/io-container-in-a-parallel-c-universe">it might have looked the same</a>, except that it doesn't. </p> <p> <strong>Next:</strong> <a href="/2023/02/27/test-data-generator-monad">Test Data Generator monad</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Adding NuGet packages when offline https://blog.ploeh.dk/2023/01/02/adding-nuget-packages-when-offline 2023-01-02T05:41:00+00:00 Mark Seemann <div id="post"> <p> <em>A fairly trivial technical detective story.</em> </p> <p> I was recently in an air plane, writing code, when I realised that I needed to add a couple of NuGet packages to my code base. I was on one of those less-travelled flights in Europe, on board an <a href="https://en.wikipedia.org/wiki/Embraer_E-Jet_family">Embraer E190</a>, and as is usually the case on those 1½-hour flights, there was no WiFi. </p> <p> Adding a NuGet package typically requires that you're online so that the tools can query the relevant NuGet repository. You'll need to download the package, so if you're offline, you're just out of luck, right? </p> <p> Fortunately, I'd previously used the packages I needed in other projects, on the same laptop. While <a href="/2014/01/29/nuget-package-restore-considered-harmful">I'm no fan of package restore</a>, I know that the local NuGet tools cache packages somewhere on the local machine. </p> <p> So, perhaps I could entice the tools to reuse a cached package... </p> <p> First, I simply tried adding a package that I needed: </p> <p> <pre>$ dotnet add package unquote Determining projects to restore... Writing C:\Users\mark\AppData\Local\Temp\tmpF3C.tmp info : X.509 certificate chain validation will use the default trust store selected by .NET. info : Adding PackageReference for package 'unquote' into project '[redacted]'. error: Unable to load the service index for source https://api.nuget.org/v3/index.json. error: No such host is known. (api.nuget.org:443) error: No such host is known.</pre> </p> <p> Fine plan, but no success. </p> <p> Clearly the <code>dotnet</code> tool was trying to access <code>api.nuget.org</code>, which, obviously, couldn't be reached because my laptop was in flight mode. It occurred to me, though, that the reason that the tool was querying <code>api.nuget.org</code> was that it wanted to see which version of the package was the most recent. After all, I hadn't specified a version. </p> <p> What if I were to specify a version? Would the tool use the cached version of the package? </p> <p> That seemed worth a try, but which versions did I already have on my laptop? </p> <p> I don't go around remembering which version numbers I've used of various NuGet packages, but I expected the NuGet tooling to have that information available, somewhere. </p> <p> But where? Keep in mind that I was offline, so couldn't easily look this up. </p> <p> On the other hand, I knew that these days, most Windows applications keep data of that kind somewhere in <code>AppData</code>, so I started spelunking around there, looking for something that might be promising. </p> <p> After looking around a bit, I found a subdirectory named <code>AppData\Local\NuGet\v3-cache</code>. This directory contained a handful of subdirectories obviously named with GUIDs. Each of these contained a multitude of <code>.dat</code> files. The names of those files, however, looked promising: </p> <p> <pre>list_antlr_index.dat list_autofac.dat list_autofac.extensions.dependencyinjection.dat list_autofixture.automoq.dat list_autofixture.automoq_index.dat list_autofixture.automoq_range_2.0.0-3.6.7.dat list_autofixture.automoq_range_3.30.3-3.50.5.dat list_autofixture.automoq_range_3.50.6-4.17.0.dat list_autofixture.automoq_range_3.6.8-3.30.2.dat list_autofixture.dat ...</pre> </p> <p> and so on. </p> <p> These names were clearly(?) named <code>list_[package-name].dat</code> or <code>list_[package-name]_index.dat</code>, so I started looking around for one named after the package I was looking for (<a href="https://www.nuget.org/packages/Unquote/">Unquote</a>). </p> <p> Often, both files are present, which was also the case for Unquote. </p> <p> <pre>$ ls list_unquote* -l -rw-r--r-- 1 mark 197609 348 Oct 1 18:38 list_unquote.dat -rw-r--r-- 1 mark 197609 42167 Sep 23 21:29 list_unquote_index.dat</pre> </p> <p> As you can tell, <code>list_unquote_index.dat</code> is much larger than <code>list_unquote.dat</code>. Since I didn't know what the format of these files were, I decided to look at the smallest one first. It had this content: </p> <p> <pre>{ "versions": [ "1.3.0", "2.0.0", "2.0.1", "2.0.2", "2.0.3", "2.1.0", "2.1.1", "2.2.0", "2.2.1", "2.2.2", "3.0.0", "3.1.0", "3.1.1", "3.1.2", "3.2.0", "4.0.0", "5.0.0", "6.0.0-rc.1", "6.0.0-rc.2", "6.0.0-rc.3", "6.0.0", "6.1.0" ] }</pre> </p> <p> A list of versions. Sterling. It looked as though version 6.1.0 was the most recent one on my machine, so I tried to add that one to my code base: </p> <p> <pre>$ dotnet add package unquote --version 6.1.0 Determining projects to restore... Writing C:\Users\mark\AppData\Local\Temp\tmp815D.tmp info : X.509 certificate chain validation will use the default trust store selected by .NET. info : Adding PackageReference for package 'unquote' into project '[redacted]'. info : Restoring packages for [redacted]... info : Package 'unquote' is compatible with all the specified frameworks in project '[redacted]'. info : PackageReference for package 'unquote' version '6.1.0' added to file '[redacted]'. info : Generating MSBuild file [redacted]. info : Writing assets file to disk. Path: [redacted] log : Restored [redacted] (in 397 ms).</pre> </p> <p> Jolly good! That worked. </p> <p> This way I managed to install all the NuGet packages I needed. This was fortunate, because I had so little time to transfer to my connecting flight that I never got to open the laptop before I was airborne again - in another E190 without WiFi, and another session of offline programming. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="7387859cda784fdf8612ddb34666d49b"> <div class="comment-author"><a href="https://github.com/asherber">Aaron Sherber</a> <a href="#7387859cda784fdf8612ddb34666d49b">#</a></div> <div class="comment-content"> <p> A postscript to your detective story might note that the primary NuGet cache lives at <code>%userprofile%\.nuget\packages</code> on Windows and <code>~/.nuget/packages</code> on Mac and Linux. The folder names there are much easier to decipher than the folders and files in the http cache. </p> </div> <div class="comment-date">2023-01-02 13:46 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functors as invariant functors https://blog.ploeh.dk/2022/12/26/functors-as-invariant-functors 2022-12-26T13:05:00+00:00 Mark Seemann <div id="post"> <p> <em>A most likely useless set of invariant functors that nonetheless exist.</em> </p> <p> This article is part of <a href="/2022/08/01/invariant-functors">a series of articles about invariant functors</a>. An invariant functor is a <a href="/2018/03/22/functors">functor</a> that is neither covariant nor contravariant. See the series introduction for more details. </p> <p> It turns out that all <a href="/2018/03/22/functors">functors</a> are also invariant functors. </p> <p> Is this useful? Let me be honest and say that if it is, I'm not aware of it. Thus, if you're interested in practical applications, you can stop reading here. This article contains nothing of practical use - as far as I can tell. </p> <h3 id="d748d07afe5342e9847e858849053911"> Because it's there <a href="#d748d07afe5342e9847e858849053911" title="permalink">#</a> </h3> <p> Why describe something of no practical use? </p> <p> Why do some people climb <a href="https://en.wikipedia.org/wiki/Mount_Everest">Mount Everest</a>? <em>Because it's there</em>, or for other irrational reasons. Which is fine. I've no personal goals that involve climbing mountains, but <a href="/2020/10/12/subjectivity">I happily engage in other irrational and subjective activities</a>. </p> <p> One of them, apparently, is to write articles of software constructs of no practical use, <em>because it's there</em>. </p> <p> All functors are also invariant functors, even if that's of no practical use. That's just the way it is. This article explains how, and shows a few (useless) examples. </p> <p> I'll start with a few <a href="https://www.haskell.org/">Haskell</a> examples and then move on to showing the equivalent examples in C#. If you're unfamiliar with Haskell, you can skip that section. </p> <h3 id="268965f94deb48d79335afea6e9b85e4"> Haskell package <a href="#268965f94deb48d79335afea6e9b85e4" title="permalink">#</a> </h3> <p> For Haskell you can find an existing definition and implementations in the <a href="https://hackage.haskell.org/package/invariant">invariant</a> package. It already makes most common functors <code>Invariant</code> instances, including <code>[]</code> (list), <code>Maybe</code>, and <code>Either</code>. Here's an example of using <code>invmap</code> with a small list: </p> <p> <pre>ghci&gt; invmap secondsToNominalDiffTime nominalDiffTimeToSeconds [0.1, 60] [0.1s,60s]</pre> </p> <p> Here I'm using the <a href="https://hackage.haskell.org/package/time">time</a> package to convert fixed-point decimals into <code>NominalDiffTime</code> values. </p> <p> How is this different from normal functor mapping with <code>fmap</code>? In observable behaviour, it's not: </p> <p> <pre>ghci&gt; fmap secondsToNominalDiffTime [0.1, 60] [0.1s,60s]</pre> </p> <p> When invariantly mapping a functor, only the covariant mapping function <code>a -&gt; b</code> is used. Here, that's <code>secondsToNominalDiffTime</code>. The contravariant mapping function <code>b -&gt; a</code> (<code>nominalDiffTimeToSeconds</code>) is simply ignored. </p> <p> While the <em>invariant</em> package already defines certain common functors as <code>Invariant</code> instances, every <code>Functor</code> instance can be converted to an <code>Invariant</code> instance. There are two ways to do that: <code>invmapFunctor</code> and <code>WrappedFunctor</code>. </p> <p> In order to demonstrate, we need a custom <code>Functor</code> instance. This one should do: </p> <p> <pre>data Pair a = Pair (a, a) deriving (Eq, Show, Functor)</pre> </p> <p> If you just want to perform an ad-hoc invariant mapping, you can use <code>invmapFunctor</code>: </p> <p> <pre>ghci&gt; invmapFunctor secondsToNominalDiffTime nominalDiffTimeToSeconds $ Pair (0.1, 60) Pair (0.1s,60s)</pre> </p> <p> I can't think of any reason to do this, but it's possible. </p> <p> <code>WrappedFunctor</code> is perhaps marginally more relevant. If you run into a function that takes an <code>Invariant</code> argument, you can convert any <code>Functor</code> to an <code>Invariant</code> instance by wrapping it in <code>WrappedFunctor</code>: </p> <p> <pre>ghci&gt; invmap secondsToNominalDiffTime nominalDiffTimeToSeconds $ WrapFunctor $ Pair (0.1, 60) WrapFunctor {unwrapFunctor = Pair (0.1s,60s)}</pre> </p> <p> A realistic, useful example still escapes me, but there it is. </p> <h3 id="2578032e937f4a68997f10b0e2412ffb"> Pair as an invariant functor in C# <a href="#2578032e937f4a68997f10b0e2412ffb" title="permalink">#</a> </h3> <p> What would the above Haskell example look like in C#? First, we're going to need a <code>Pair</code> data structure: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Pair</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Pair</span>(T&nbsp;x,&nbsp;T&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Y&nbsp;=&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;X&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;Y&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members&nbsp;follow...</span></pre> </p> <p> Making <code>Pair&lt;T&gt;</code> a functor is so easy that Haskell can do it automatically with the <code>DeriveFunctor</code> extension. In C# you must explicitly write the function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Pair&lt;T1&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T1</span>&gt;(Func&lt;T,&nbsp;T1&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Pair&lt;T1&gt;(selector(X),&nbsp;selector(Y)); }</pre> </p> <p> An example equivalent to the above <code>fmap</code> example might be this, here expressed as a unit test: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;FunctorExample() { &nbsp;&nbsp;&nbsp;&nbsp;Pair&lt;<span style="color:blue;">long</span>&gt;&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Pair&lt;<span style="color:blue;">long</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.TicksPerSecond&nbsp;/&nbsp;10, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.TicksPerSecond&nbsp;*&nbsp;60); &nbsp;&nbsp;&nbsp;&nbsp;Pair&lt;TimeSpan&gt;&nbsp;actual&nbsp;=&nbsp;sut.Select(ticks&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(ticks)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Pair&lt;TimeSpan&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromSeconds(.1), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromSeconds(60)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual); }</pre> </p> <p> You can trivially make <code>Pair&lt;T&gt;</code> an invariant functor by giving it a function equivalent to <code>invmap</code>. As I outlined in <a href="/2022/08/01/invariant-functors">the introduction</a> it's possible to add an <code>InvMap</code> method to the class, but it might be more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> to instead add a <code>Select</code> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Pair&lt;T1&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T1</span>&gt;(Func&lt;T,&nbsp;T1&gt;&nbsp;tToT1,&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;t1ToT) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Select(tToT1); }</pre> </p> <p> Notice that this overload simply ignores the <code>t1ToT</code> argument and delegates to the normal <code>Select</code> overload. That's consistent with the Haskell package. This unit test shows an examples: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;InvariantFunctorExample() { &nbsp;&nbsp;&nbsp;&nbsp;Pair&lt;<span style="color:blue;">long</span>&gt;&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Pair&lt;<span style="color:blue;">long</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.TicksPerSecond&nbsp;/&nbsp;10, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.TicksPerSecond&nbsp;*&nbsp;60); &nbsp;&nbsp;&nbsp;&nbsp;Pair&lt;TimeSpan&gt;&nbsp;actual&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sut.Select(ticks&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(ticks),&nbsp;ts&nbsp;=&gt;&nbsp;ts.Ticks); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Pair&lt;TimeSpan&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromSeconds(.1), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromSeconds(60)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual); }</pre> </p> <p> I can't think of a reason to do this in C#. In Haskell, at least, you have enough power of abstraction to describe something as simply an <code>Invariant</code> functor, and then let client code decide whether to use <code>Maybe</code>, <code>[]</code>, <code>Endo</code>, or a custom type like <code>Pair</code>. You can't do that in C#, so the abstraction is even less useful here. </p> <h3 id="8dbd7bd0f9c64fa5a0ac7c397617521b"> Conclusion <a href="#8dbd7bd0f9c64fa5a0ac7c397617521b" title="permalink">#</a> </h3> <p> All functors are invariant functors. You simply use the normal functor mapping function (<code>fmap</code> in Haskell, <code>map</code> in many other languages, <code>Select</code> in C#). This enables you to add an invariant mapping (<code>invmap</code>) that only uses the covariant argument (<code>a -&gt; b</code>) and ignores the contravariant argument (<code>b -&gt; a</code>). </p> <p> Invariant functors are, however, not particularly useful, so neither is this result. Still, it's there, so deserves a mention. The situation is similar for the next article. </p> <p> <strong>Next:</strong> <a href="/2023/02/06/contravariant-functors-as-invariant-functors">Contravariant functors as invariant functors</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Error-accumulating composable assertions in C# https://blog.ploeh.dk/2022/12/19/error-accumulating-composable-assertions-in-c 2022-12-19T08:39:00+00:00 Mark Seemann <div id="post"> <p> <em>Perhaps the list monoid is all you need for non-short-circuiting assertions.</em> </p> <p> This article is the second instalment in a small articles series about <a href="/2022/11/07/applicative-assertions">applicative assertions</a>. It explores a way to compose assertions in such a way that failure messages accumulate rather than short-circuit. It assumes that you've read the <a href="/2022/11/07/applicative-assertions">article series introduction</a> and the <a href="/2022/11/28/an-initial-proof-of-concept-of-applicative-assertions-in-c">previous article</a>. </p> <p> Unsurprisingly, the previous article showed that you can use an <a href="/2018/10/01/applicative-functors">applicative functor</a> to create composable assertions that don't short-circuit. It also concluded that, in C# at least, the API is awkward. </p> <p> This article explores a simpler API. </p> <h3 id="dc146cd5b792473db7f7e64cac3c4607"> A clue left by the proof of concept <a href="#dc146cd5b792473db7f7e64cac3c4607" title="permalink">#</a> </h3> <p> The previous article's proof of concept left a clue suggesting a simpler API. Consider, again, how the rather horrible <code>RunAssertions</code> method decides whether or not to throw an exception: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;errors&nbsp;=&nbsp;composition.Match( &nbsp;&nbsp;&nbsp;&nbsp;onFailure:&nbsp;f&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.Join(Environment.NewLine,&nbsp;f), &nbsp;&nbsp;&nbsp;&nbsp;onSuccess:&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.Empty); <span style="color:blue;">if</span>&nbsp;(!<span style="color:blue;">string</span>.IsNullOrEmpty(errors)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Exception(errors);</pre> </p> <p> Even though <code><span style="color:#2b91af;">Validated</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;</code> is a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>, the <code>RunAssertions</code> method declines to take advantage of that. Instead, it reduces <code>composition</code> to a simple type: A <code>string</code>. It then decides to throw an exception if the <code>errors</code> value is not null or empty. </p> <p> This suggests that using a sum type may not be necessary to distinguish between the success and the failure case. Rather, an empty error string is all it takes to indicate success. </p> <h3 id="801897c3ffc0455e8e7a7ac7531f2be3"> Non-empty errors <a href="#801897c3ffc0455e8e7a7ac7531f2be3" title="permalink">#</a> </h3> <p> The proof-of-concept assertion type is currently defined as <code>Validated</code> with a particular combination of type arguments: <code>Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;</code>. Consider, again, this <code>Match</code> expression: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;errors&nbsp;=&nbsp;composition.Match( &nbsp;&nbsp;&nbsp;&nbsp;onFailure:&nbsp;f&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.Join(Environment.NewLine,&nbsp;f), &nbsp;&nbsp;&nbsp;&nbsp;onSuccess:&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.Empty);</pre> </p> <p> Does an empty string unambiguously indicate success? Or is it possible to arrive at an empty string even if <code>composition</code> actually represents a failure case? </p> <p> You can arrive at an empty string from a failure case if the collection of error messages is empty. Consider the type argument that takes the place of the <code>F</code> generic type: <code>IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;</code>. A collection of this type can be empty, which would also cause the above <code>Match</code> to produce an empty string. </p> <p> Even so, the proof-of-concept works in practice. The reason it works is that failure cases will never have empty assertion messages. We know this because (in the proof-of-concept code) only two functions produce assertions, and they each populate the error message collection with a string. You may want to revisit the <code>AssertTrue</code> and <code>AssertEqual</code> functions in the <a href="/2022/11/28/an-initial-proof-of-concept-of-applicative-assertions-in-c">previous article</a> to convince yourself that this is true. </p> <p> This is a good example of knowledge that 'we' as developers know, but the code currently doesn't capture. Having to deal with such knowledge taxes your working memory, so why not <a href="/encapsulation-and-solid">encapsulate</a> such information in the type itself? </p> <p> How do you encapsulate the knowledge that a collection is never empty? Introduce a <code>NotEmptyCollection</code> collection. I'll reuse the class from the article <a href="/2017/12/11/semigroups-accumulate">Semigroups accumulate</a> and add a <code>Concat</code> instance method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;NotEmptyCollection&lt;T&gt;&nbsp;Concat(NotEmptyCollection&lt;T&gt;&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;NotEmptyCollection&lt;T&gt;(Head,&nbsp;Tail.Concat(other).ToArray()); }</pre> </p> <p> Since the two assertion-producing functions both supply an error message in the failure case, it's trivial to change them to return <code>Validated&lt;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;</code> - just change the types used: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;&nbsp;AssertTrue( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;condition, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;message) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;condition &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;Succeed&lt;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;(Unit.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Fail&lt;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;(<span style="color:blue;">new</span>&nbsp;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;(message)); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;&nbsp;AssertEqual&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;expected, &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;actual) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(expected,&nbsp;actual) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;Succeed&lt;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;(Unit.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Fail&lt;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">$&quot;Expected&nbsp;</span>{expected}<span style="color:#a31515;">,&nbsp;but&nbsp;got&nbsp;</span>{actual}<span style="color:#a31515;">.&quot;</span>)); }</pre> </p> <p> This change guarantees that the <code>RunAssertions</code> method only produces an empty <code>errors</code> string in success cases. </p> <h3 id="e5097dc5d94f4a1480eeaf483ac08336"> Error collection isomorphism <a href="#e5097dc5d94f4a1480eeaf483ac08336" title="permalink">#</a> </h3> <p> Assertions are still defined by the <code>Validated</code> sum type, but the <em>success</em> case carries no information: <code>Validated&lt;NotEmptyCollection&lt;T&gt;,&nbsp;Unit&gt;</code>, and the <em>failure</em> case is always guaranteed to contain at least one error message. </p> <p> This suggests that a simpler representation is possible: One that uses a normal collection of errors, and where an empty collection indicates an absence of errors: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Asserted</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Asserted</span>()&nbsp;:&nbsp;<span style="color:blue;">this</span>(Array.Empty&lt;T&gt;()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Asserted</span>(T&nbsp;error)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;error&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Asserted</span>(IReadOnlyCollection&lt;T&gt;&nbsp;errors) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Errors&nbsp;=&nbsp;errors; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Asserted&lt;T&gt;&nbsp;And(Asserted&lt;T&gt;&nbsp;other) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(other)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Asserted&lt;T&gt;(Errors.Concat(other.Errors).ToList()); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;T&gt;&nbsp;Errors&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> The <code><span style="color:#2b91af;">Asserted</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class is scarcely more than a glorified wrapper around a normal collection, but it's isomorphic to <code>Validated&lt;NotEmptyCollection&lt;T&gt;,&nbsp;Unit&gt;</code>, which the following two functions prove: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Asserted&lt;T&gt;&nbsp;FromValidated&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Validated&lt;NotEmptyCollection&lt;T&gt;,&nbsp;Unit&gt;&nbsp;v) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;v.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;failures&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Asserted&lt;T&gt;(failures), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Asserted&lt;T&gt;()); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;NotEmptyCollection&lt;T&gt;,&nbsp;Unit&gt;&nbsp;ToValidated&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Asserted&lt;T&gt;&nbsp;a) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(a.Errors.Any()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;errors&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;NotEmptyCollection&lt;T&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.Errors.First(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.Errors.Skip(1).ToArray()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Validated.Fail&lt;NotEmptyCollection&lt;T&gt;,&nbsp;Unit&gt;(errors); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Validated.Succeed&lt;NotEmptyCollection&lt;T&gt;,&nbsp;Unit&gt;(Unit.Value); }</pre> </p> <p> You can translate back and forth between <code>Validated&lt;NotEmptyCollection&lt;T&gt;,&nbsp;Unit&gt;</code> and <code>Asserted&lt;T&gt;</code> without loss of information. </p> <p> A collection, however, gives rise to a <a href="/2017/10/06/monoids">monoid</a>, which suggests a much simpler way to compose assertions than using an applicative functor. </p> <h3 id="0ccfc89ad23f4b95932637723ff3c29a"> Asserted truth <a href="#0ccfc89ad23f4b95932637723ff3c29a" title="permalink">#</a> </h3> <p> You can now rewrite the assertion-producing functions to return <code>Asserted&lt;<span style="color:blue;">string</span>&gt;</code> instead of <code>Validated&lt;NotEmptyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;</code>. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Asserted&lt;<span style="color:blue;">string</span>&gt;&nbsp;True(<span style="color:blue;">bool</span>&nbsp;condition,&nbsp;<span style="color:blue;">string</span>&nbsp;message) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;condition&nbsp;?&nbsp;<span style="color:blue;">new</span>&nbsp;Asserted&lt;<span style="color:blue;">string</span>&gt;()&nbsp;:&nbsp;<span style="color:blue;">new</span>&nbsp;Asserted&lt;<span style="color:blue;">string</span>&gt;(message); }</pre> </p> <p> This <code>Asserted.True</code> function returns no error messages when <code>condition</code> is <code>true</code>, but a collection with the single element <code>message</code> when it's <code>false</code>. </p> <p> You can use it in a unit test like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;assertResponse&nbsp;=&nbsp;Asserted.True( &nbsp;&nbsp;&nbsp;&nbsp;deleteResp.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{deleteResp.StatusCode}<span style="color:#a31515;">.&quot;</span>);</pre> </p> <p> You'll see how <code>assertResponse</code> composes with another assertion later in this article. The example continues from <a href="/2022/11/28/an-initial-proof-of-concept-of-applicative-assertions-in-c">the previous article</a>. It's the same test from the same code base. </p> <h3 id="519d7e0d757f4dceb6a2833c09d019d8"> Asserted equality <a href="#519d7e0d757f4dceb6a2833c09d019d8" title="permalink">#</a> </h3> <p> You can also rewrite the other assertion-producing function in the same way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Asserted&lt;<span style="color:blue;">string</span>&gt;&nbsp;Equal(<span style="color:blue;">object</span>&nbsp;expected,&nbsp;<span style="color:blue;">object</span>&nbsp;actual) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Equals(expected,&nbsp;actual)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Asserted&lt;<span style="color:blue;">string</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Asserted&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">$&quot;Expected&nbsp;</span>{expected}<span style="color:#a31515;">,&nbsp;but&nbsp;got&nbsp;</span>{actual}<span style="color:#a31515;">.&quot;</span>); }</pre> </p> <p> Again, when the assertion passes, it returns no errors; otherwise, it returns a collection with a single error message. </p> <p> Using it may look like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;getResp&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateClient().GetAsync(address); <span style="color:blue;">var</span>&nbsp;assertState&nbsp;=&nbsp;Asserted.Equal(HttpStatusCode.NotFound,&nbsp;getResp.StatusCode);</pre> </p> <p> At this point, each of the assertions are objects that represent a verification step. By themselves, they neither pass nor fail the test. You have to execute them to reach a verdict. </p> <h3 id="29825aed26124471803324863ea7d897"> Evaluating assertions <a href="#29825aed26124471803324863ea7d897" title="permalink">#</a> </h3> <p> The above code listing of the <code><span style="color:#2b91af;">Asserted</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class already shows how to combine two <code><span style="color:#2b91af;">Asserted</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> objects into one. The <code>And</code> instance method is a binary operation that, together with the parameterless constructor, makes <code><span style="color:#2b91af;">Asserted</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> a <a href="/2017/10/06/monoids">monoid</a>. </p> <p> Once you've combined all assertions into a single <code><span style="color:#2b91af;">Asserted</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> object, you need to <code>Run</code> it to produce a test outcome: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Run(<span style="color:blue;">this</span>&nbsp;Asserted&lt;<span style="color:blue;">string</span>&gt;&nbsp;assertions) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(assertions?.Errors.Any()&nbsp;??&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;messages&nbsp;=&nbsp;<span style="color:blue;">string</span>.Join(Environment.NewLine,&nbsp;assertions.Errors); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Exception(messages); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If there are no errors, <code>Run</code> does nothing; otherwise it combines all the error messages together and throws an exception. As was also the case in the previous article, I've allowed myself a few proof-of-concept shortcuts. <a href="https://learn.microsoft.com/dotnet/standard/design-guidelines/using-standard-exception-types">The framework design guidelines admonishes against throwing System.Exception</a>. It might be more appropriate to introduce a new <code>Exception</code> type that also allows enumerating the error messages. </p> <p> The entire <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">assertion phase</a> of the test looks like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;assertResponse&nbsp;=&nbsp;Asserted.True( &nbsp;&nbsp;&nbsp;&nbsp;deleteResp.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{deleteResp.StatusCode}<span style="color:#a31515;">.&quot;</span>); <span style="color:blue;">var</span>&nbsp;getResp&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateClient().GetAsync(address); <span style="color:blue;">var</span>&nbsp;assertState&nbsp;=&nbsp;Asserted.Equal(HttpStatusCode.NotFound,&nbsp;getResp.StatusCode); assertResponse.And(assertState).Run();</pre> </p> <p> You can see the entire test in the previous article. Notice how the two assertion objects are first combined into one with the <code>And</code> binary operation. The result is a single <code>Asserted&lt;<span style="color:blue;">string</span>&gt;</code> object on which you can call <code>Run</code>. </p> <p> Like the previous proof of concept, this assertion passes and fails in the same way. It's possible to compose assertions and collect error messages, instead of short-circuiting on the first failure, even without an applicative functor. </p> <h3 id="f3d9baf1ad564c05be99b32f0b4a1017"> Method chaining <a href="#f3d9baf1ad564c05be99b32f0b4a1017" title="permalink">#</a> </h3> <p> If you don't like to come up with variable names just to make assertions, it's also possible to use the <code>Asserted</code> API's <a href="https://martinfowler.com/bliki/FluentInterface.html">fluent interface</a>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;getResp&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateClient().GetAsync(address); Asserted &nbsp;&nbsp;&nbsp;&nbsp;.True( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deleteResp.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{deleteResp.StatusCode}<span style="color:#a31515;">.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;.And(Asserted.Equal(HttpStatusCode.NotFound,&nbsp;getResp.StatusCode)) &nbsp;&nbsp;&nbsp;&nbsp;.Run();</pre> </p> <p> This isn't necessarily better, but it's an option. </p> <h3 id="ecabe91c2b9949edbcb4cfed5213ec72"> Conclusion <a href="#ecabe91c2b9949edbcb4cfed5213ec72" title="permalink">#</a> </h3> <p> While it's possible to design non-short-circuiting composable assertions using an applicative functor, it looks as though a simpler solution might solve the same problem. Collect error messages. If none were collected, interpret that as a success. </p> <p> As I wrote in the <a href="/2022/11/07/applicative-assertions">introduction article</a>, however, this may not be the last word. Some assertions return values that can be used for other assertions. That's a scenario that I have not yet investigated in this light, and it may change the conclusion. If so, I'll add more articles to this small article series. As I'm writing this, though, I have no such plans. </p> <p> Did I just, in a roundabout way, write that <em>more research is needed?</em> </p> <p> <strong>Next:</strong> <a href="/2023/01/30/built-in-alternatives-to-applicative-assertions">Built-in alternatives to applicative assertions</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2aae9a7f4c1145aa92b487925f4b46ba"> <div class="comment-author"><a href="https://ptupitsyn.github.io/">Pavel Tupitsyn</a> <a href="#2aae9a7f4c1145aa92b487925f4b46ba">#</a></div> <div class="comment-content"> <p> I think NUnit's <a href="https://docs.nunit.org/articles/nunit/writing-tests/assertions/multiple-asserts.html">Assert.Multiple</a> is worth mentioning in this series. It does not require any complicated APIs, just wrap your existing test with multiple asserts into a delegate. </p> </div> <div class="comment-date">2022-12-20 08:02 UTC</div> </div> <div class="comment" id="5b34eb2d997f417bac27738ee7cbb16d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5b34eb2d997f417bac27738ee7cbb16d">#</a></div> <div class="comment-content"> <p> Pavel, thank you for writing. I'm aware of both that API and similar ones for other testing frameworks. As is usually the case, there are trade-offs to consider. I'm currently working on some material that may turn into another article about that. </p> </div> <div class="comment-date">2022-12-21 20:17 UTC</div> </div> <div class="comment" id="f244b81042414b98be67a937cf652648"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f244b81042414b98be67a937cf652648">#</a></div> <div class="comment-content"> <p> A new article is now available: <a href="/2023/01/30/built-in-alternatives-to-applicative-assertions">Built-in alternatives to applicative assertions</a>. </p> </div> <div class="comment-date">2023-01-30 12:31 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. When do tests fail? https://blog.ploeh.dk/2022/12/12/when-do-tests-fail 2022-12-12T08:33:00+00:00 Mark Seemann <div id="post"> <p> <em>Optimise for the common scenario.</em> </p> <p> Unit tests occasionally fail. When does that happen? How often? What triggers it? What information is important when tests fail? </p> <p> Regularly I encounter the viewpoint that it should be easy to understand the purpose of a test <em>when it fails</em>. Some people consider test names important, a topic that <a href="/2022/06/13/some-thoughts-on-naming-tests">I've previously discussed</a>. Recently I discussed the <a href="http://xunitpatterns.com/Assertion%20Roulette.html">Assertion Roulette</a> test smell on Twitter, and again I learned some surprising things about what people value in unit tests. </p> <h3 id="6673d3ef3f374f849fe7fe38a7b96985"> The importance of clear assertion messages <a href="#6673d3ef3f374f849fe7fe38a7b96985" title="permalink">#</a> </h3> <p> The Assertion Roulette test smell is often simplified to degeneracy, but it really describes situations where it may be a problem if you can't tell which of several assertions actually caused a test to fail. </p> <p> <a href="https://www.joshka.net">Josh McKinney</a> gave a more detailed example than Gerard Meszaros does in <a href="/ref/xunit-patterns">the book</a>: </p> <blockquote> <p> "Background. In a legacy product, we saw some tests start failing intermittently. They weren’t just flakey, but also failed without providing enough info to fix. One of things which caused time to fix to increase was multiple ways of a single test to fail." </p> <footer><cite><a href="https://twitter.com/joshuamck/status/1572528796125003777">Josh McK</a></cite></footer> </blockquote> <p> He goes on: </p> <blockquote> <p> "I.e. if you fix the first assertion and you know there still could be flakiness, or long cycle times to see the failure. Multiple assertions makes any test problem worse. In an ideal state, they are fine, but every assertion doubles the amount of failures a test catches." </p> <footer><cite><a href="https://twitter.com/joshuamck/status/1572529894361534464">Josh McK</a></cite></footer> </blockquote> <p> and concludes: </p> <blockquote> <p> "the other main way (unrelated) was things like: </p> <p> assertTrue(someListResult.isRmpty()) </p> <p> Which tells you what failed, but nothing about how. </p> <p> But the following is worse. You must run the test twice to fix: </p> <p> assertFalse(someList.isEmpty());<br> assertEqual(expected, list.get(0));" </p> <footer><cite><a href="https://twitter.com/joshuamck/status/1572534297403469824">Josh McK</a></cite></footer> </blockquote> <p> The final point is due to the short-circuiting nature of most assertion libraries. That, however, is <a href="/2022/11/07/applicative-assertions">a solvable problem</a>. </p> <p> I find the above a compelling example of why Assertion Roulette may be problematic. </p> <p> It did give me pause, though. How common is this scenario? </p> <h3 id="5ae99f4515444bd6ba483beffb819557"> Out of the blue <a href="#5ae99f4515444bd6ba483beffb819557" title="permalink">#</a> </h3> <p> The situation described by Josh McKinney comes with more than a single warning flag. I hope that it's okay to point some of them out. I didn't get the impression from my interaction with Josh McKinney that he considered the situation ideal in any way. </p> <p> First, of course, there's the lack of information about the problem. Here, that's a real problem. As I understand it, it makes it harder to reproduce the problem in a development environment. </p> <p> Next, there's long cycle times, which I interpret as significant time may pass from when you attempt a fix until you can actually observe whether or not it worked. Josh McKinney doesn't say how long, but I wouldn't surprised if it was measured in days. At least, if the cycle time is measured in days, I can see how this is a problem. </p> <p> Finally, there's the observation that "some tests start failing intermittently". This was the remark that caught my attention. How often does that happen? </p> <p> Tests shouldn't do that. Tests should be deterministic. If they're not, you should work to <a href="https://martinfowler.com/articles/nonDeterminism.html">eradicate non-determinism in tests</a>. </p> <p> I'll be the first to admit that that I also write non-deterministic tests. Not by design, but because I make mistakes. I've written many <a href="http://xunitpatterns.com/Erratic%20Test.html">Erratic Tests</a> in my career, and I've documented a few of them here: </p> <ul> <li><a href="/2021/01/11/waiting-to-happen">Waiting to happen</a></li> <li><a href="/2022/05/23/waiting-to-never-happen">Waiting to never happen</a></li> <li><a href="/2020/10/05/fortunately-i-dont-squash-my-commits">Fortunately, I don't squash my commits</a></li> <li><a href="/2016/01/18/make-pre-conditions-explicit-in-property-based-tests">Make pre-conditions explicit in Property-Based Tests</a></li> </ul> <p> While it <em>can</em> happen, it shouldn't be the norm. When it nonetheless happens, eradicating that source of non-determinism should be top priority. Pull the <a href="https://en.wikipedia.org/wiki/Andon_(manufacturing)">andon cord</a>. </p> <h3 id="5fd251665441473ca3048a9137f9bc2b"> When tests fail <a href="#5fd251665441473ca3048a9137f9bc2b" title="permalink">#</a> </h3> <p> Ideally, tests should rarely fail. As examined above, you may have Erratic Tests in your test suite, and if you do, these tests will occasionally (or often) fail. As Martin Fowler writes, this is a problem and you should do something about it. He also outlines strategies for it. </p> <p> Once you've eradicated non-determinism in unit tests, then when do tests fail? </p> <p> I can think of a couple of situations. </p> <p> Tests routinely fail as part of the <a href="/2019/10/21/a-red-green-refactor-checklist">red-green-refactor cycle</a>. This is by design. If no test is failing in the <em>red</em> phase, you probably made a mistake (which also regularly <a href="/2019/10/14/tautological-assertion">happens to me</a>), or you may not really be doing test-driven development (TDD). </p> <p> Another situation that may cause a test to fail is if you changed some code and triggered a regression test. </p> <p> In both cases, tests don't just fail <a href="https://amzn.to/3SPdHAO">out of the blue</a>. They fail as an immediate consequence of something you did. </p> <h3 id="aaeebfa1a96b47398219817bc3327a9c"> Optimise for the common scenario <a href="#aaeebfa1a96b47398219817bc3327a9c" title="permalink">#</a> </h3> <p> In both cases you're (hopefully) in a tight feedback loop. If you're in a tight feedback loop, then how important is the assertion message really? How important is the test name? </p> <p> You work on the code base, make some changes, run the tests. If one or more tests fail, it's correlated to the change you just made. You should have a good idea of what went wrong. Are code forensics and elaborate documentation really necessary to understand a test that failed because you just did something a few minutes before? </p> <p> The reason I don't care much about test names or whether there's one or more assertion in a unit test is exactly that: When tests fail, it's usually because of something I just did. I don't need diagnostics tools to find the root cause. The root cause is the change that I just made. </p> <p> That's my common scenario, and I try to optimise my processes for the common scenarios. </p> <h3 id="21b16c9b2fb649ec859e53b0a1ab431a"> Fast feedback <a href="#21b16c9b2fb649ec859e53b0a1ab431a" title="permalink">#</a> </h3> <p> There's an implied way of working that affects such attitudes. Since I learned about TDD in 2003 I've always relished the fast feedback I get from a test suite. Since I tried continuous deployment around 2014, I consider it central to <a href="/ref/modern-software-engineering">modern software engineering</a> (and <a href="/ref/accelerate">Accelerate</a> strongly suggests so, too). </p> <p> The modus operandi I outline above is one of fast feedback. If you're sitting on a feature branch for weeks before integrating into master, or if you can only deploy two times a year, this influences what works and what doesn't. </p> <p> Both <em>Modern Software Engineering</em> and <em>Accelerate</em> make a strong case that short feedback cycles are pivotal for successful software development organisations. </p> <p> I also understand that that's not the reality for everyone. When faced with long cycle times, a multitude of Erratic Tests, a legacy code base, and so on, other things become important. In those circumstances, tests may fail for different reasons. </p> <p> When you work with TDD, continuous integration (CI), and continuous deployment (CD), then when do tests fail? They fail because you made them fail, only minutes earlier. Fix your code and move forward. </p> <h3 id="53cc6ce19bad4bdea3f40fd752f6338d"> Conclusion <a href="#53cc6ce19bad4bdea3f40fd752f6338d" title="permalink">#</a> </h3> <p> When discussing test names and assertion messages, I've been surprised by the emphasis some people put on what I consider to be of secondary importance. I think the explanation is that circumstances differ. </p> <p> With TDD and CI/CD you mostly look at a unit test when you write it, or if some regression test fails because you changed some code (perhaps in response to a test you just wrote). Your test suite may have hundreds or thousands of tests. Most of these pass every time you run the test suite. That's the normal state of affairs. </p> <p> In other circumstances, you may have Erratic Tests that fail unpredictably. You should make it a priority to stop that, but as part of that process, you may need good assertion messages and good test names. </p> <p> Different circumstances call for different reactions, so what works well in one situation may be a liability in other situations. I hope that this article has shed a little light on the forces you may want to consider. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. GitHub Copilot preliminary experience report https://blog.ploeh.dk/2022/12/05/github-copilot-preliminary-experience-report 2022-12-05T08:37:00+00:00 Mark Seemann <div id="post"> <p> <em>Based on a few months of use.</em> </p> <p> I've been evaluating <a href="https://github.com/features/copilot">GitHub Copilot</a> since August 2022. Perhaps it's time to collect my thoughts so far. </p> <p> In short, it's surprisingly good, but also gets a lot of things wrong. It does seem helpful to the experienced programmer, but I don't see it replacing all programmers yet. </p> <h3 id="19d8478cc2d5429f9c62138574a30a45"> Not only for boilerplate code <a href="#19d8478cc2d5429f9c62138574a30a45" title="permalink">#</a> </h3> <p> I was initially doubtful. I'd seen some demos where Copilot created fifteen to twenty lines of code to, say, make a REST API request. These examples mostly struck me as auto-generation of something that ought to be a proper abstraction: A method in a reusable library. </p> <p> In general <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">I don't think that typing is a productivity bottleneck</a>, and I'm <a href="/2013/02/04/BewareofProductivityTools">sceptical of productivity tools</a>, and particularly code generators. The more code a code base contains, the more code there is to read. Accelerating code production doesn't strike me as a goal in itself. </p> <p> On the other hand, I'm past fifty and aware that in my age, one often tends to resist new ideas. Perhaps the above justifications are nothing but <a href="https://en.wikipedia.org/wiki/Motivated_reasoning">motivated reasoning</a>. I decided to install the plug-ins and go about my business to see what would happen. </p> <p> It has surprised and delighted me more than once. It's not only for boilerplate code. Here's a recent example. </p> <p> I was doing an exercise in <a href="https://fsharp.org">F#</a> and wrote this type definition: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Instruction&nbsp;=&nbsp;Enqueue&nbsp;<span style="color:blue;">of</span>&nbsp;int&nbsp;|&nbsp;Dequeue</pre> </p> <p> I wanted to write a property-based test with this type, and since I was using <a href="https://hedgehog.qa/">Hedgehog</a> I needed a generator for the type. While <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> can automatically generate values of custom <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/discriminated-unions">discriminated unions</a>, Hedgehog has a more <a href="https://peps.python.org/pep-0020/">explicit-is-better-than-implicit</a> design philosophy. In other words, if there's a way to make Hedgehog automatically generate values of a type like the above <code>Instruction</code>, it has so far escaped me. </p> <p> So I started typing <code><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;genInstruction&nbsp;=</code> after which Copilot suggested the following: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;genInstruction&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;Gen.frequency &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;1,&nbsp;Gen.constant&nbsp;Dequeue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1,&nbsp;Gen.int32&nbsp;(Range.constant&nbsp;0&nbsp;10_000)&nbsp;|&gt;&nbsp;Gen.map&nbsp;Enqueue&nbsp;]</pre> </p> <p> I was, frankly, stunned. While I do understand that Copilot doesn't actually understand the code it suggests, Hedgehog is sufficiently esoteric that I didn't expect Copilot to have enough training data to enable it to make a useful contribution in this niche. I was wrong. I've repeatedly seen Copilot make suggestions to my F# and <a href="https://www.haskell.org/">Haskell</a> code. It's not just for C#, <a href="https://www.javascript.com">JavaScript</a>, or <a href="https://www.python.org/">python</a> code. </p> <p> The above suggestion was, to be clear, absolutely appropriate and compiled right away. The only detail I decided to change was the <code>Range</code>, which I decided to change to <code>Range.linear</code>. That's not, however, a significant change. </p> <p> Perhaps you're not impressed by three lines of auto-generated code. How much of a productivity improvement is that? Quite a bit, in my case. </p> <p> It wouldn't have taken me long to type those three lines of code, but as I already mentioned, <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">typing isn't a bottleneck</a>. On the other hand, looking up an unfamiliar API can take some time. <a href="/ref/programmers-brain">The Programmer's Brain</a> discusses this kind of problem and suggests exercises to address it. Does Copilot offer a shortcut? </p> <p> While I couldn't remember the details of Hedgehog's API, once I saw the suggestion, I recognised <code>Gen.frequency</code>, so I understood it as an appropriate code suggestion. The productivity gain, if there is one, may come from saving you the effort of looking up unfamiliar APIs, rather than saving you some keystrokes. </p> <p> In this example, I already knew of the <code>Gen.frequency</code> function - I just couldn't recall the exact name and type. This enabled me to evaluate Copilot's suggestion and deem it correct. If I hadn't known that API already, how could I have known whether to trust Copilot? </p> <h3 id="424f6a4928d945d7bcb13f2de8f3a98e"> Detectably wrong suggestions <a href="#424f6a4928d945d7bcb13f2de8f3a98e" title="permalink">#</a> </h3> <p> As amazing as Copilot can be, it's hardly faultless. It makes many erroneous suggestions. Sometimes the suggestion is obviously wrong. If you accept it, it doesn't compile. Sometimes, the compilation error is only a little edit from being correct, but at least in such situations you'll be explicitly aware that the suggestion couldn't be used verbatim. </p> <p> Other suggestions are wrong, but less conspicuously so. Here's an example. </p> <p> I was recently subjecting the code base that accompanies <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> to the <a href="https://en.wikipedia.org/wiki/Mutation_testing">mutation testing</a> tool <a href="https://stryker-mutator.io/">Stryker</a>. Since it did point out a few possible mutations, I decided to add a few tests. One was of a wrapper class called <code>TimeOfDay</code>. Because of static code analysis rules, it came with conversions to and from <a href="https://learn.microsoft.com/dotnet/api/system.timespan">TimeSpan</a>, but these methods weren't covered by any tests. </p> <p> In order to remedy that situation, I started writing an FsCheck property and came as far as: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ConversionsRoundTrip</span>(TimeSpan&nbsp;<span style="color:#1f377f;">timeSpan</span>)</pre> </p> <p> At that point Copilot suggested the following, which I accepted: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ConversionsRoundTrip</span>(TimeSpan&nbsp;<span style="color:#1f377f;">timeSpan</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">timeOfDay</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TimeOfDay(timeSpan); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;(TimeSpan)timeOfDay; &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(timeSpan,&nbsp;actual); }</pre> </p> <p> Looks good, doesn't it? Again, I was impressed. It compiled, and it even looks as though Copilot had picked up one of my naming conventions: <a href="/2020/11/30/name-by-role">naming variables by role</a>, in this case <code>actual</code>. </p> <p> While I tend to be on guard, I immediately ran the test suite instead of thinking it through. It failed. Keep in mind that this is a <a href="https://en.wikipedia.org/wiki/Characterization_test">characterisation test</a>, so it was supposed to pass. </p> <p> The <code>TimeOfDay</code> constructor reveals why: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeOfDay</span>(TimeSpan&nbsp;<span style="color:#1f377f;">durationSinceMidnight</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(durationSinceMidnight&nbsp;&lt;&nbsp;TimeSpan.Zero&nbsp;|| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromHours(24)&nbsp;&lt;&nbsp;durationSinceMidnight) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(durationSinceMidnight), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;supply&nbsp;a&nbsp;TimeSpan&nbsp;between&nbsp;0&nbsp;and&nbsp;24&nbsp;hours.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.durationSinceMidnight&nbsp;=&nbsp;durationSinceMidnight; }</pre> </p> <p> While FsCheck knows how to generate <code>TimeSpan</code> values, it'll generate arbitrary durations, including negative values and spans much longer than 24 hours. That explains why the test fails. </p> <p> Granted, this is hardly a searing indictment against Copilot. After all, I could have made this mistake myself. </p> <p> Still, that prompted me to look for more issues with the code that Copilot had suggested. Another problem with the code is that it tests the wrong API. The suggested test tries to round-trip via the <code>TimeOfDay</code> class' explicit cast operators, which were already covered by tests. Well, I might eventually have discovered that, too. Keep in mind that I was adding this test to improve the code base's Stryker score. After running the tool again, I would probably eventually have discovered that the score didn't improve. It takes Stryker around 25 minutes to test this code base, though, so it wouldn't have been rapid feedback. </p> <p> Since, however, I examined the code with a critical eye, I noticed this by myself. This would clearly require changing the test code as well. </p> <p> In the end, I wrote this test: </p> <p> <pre>[Property] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ConversionsRoundTrip</span>(TimeSpan&nbsp;<span style="color:#1f377f;">timeSpan</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;ScaleToTimeOfDay(timeSpan); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;TimeOfDay.ToTimeOfDay(expected); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;TimeOfDay.ToTimeSpan(sut); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;TimeSpan&nbsp;<span style="color:#74531f;">ScaleToTimeOfDay</span>(TimeSpan&nbsp;<span style="color:#1f377f;">timeSpan</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Convert&nbsp;an&nbsp;arbitrary&nbsp;TimeSpan&nbsp;to&nbsp;a&nbsp;24-hour&nbsp;TimeSpan.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;The&nbsp;data&nbsp;structure&nbsp;that&nbsp;underlies&nbsp;TimeSpan&nbsp;is&nbsp;a&nbsp;64-bit&nbsp;integer,</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;so&nbsp;first&nbsp;we&nbsp;need&nbsp;to&nbsp;identify&nbsp;the&nbsp;range&nbsp;of&nbsp;possible&nbsp;TimeSpan</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;values.&nbsp;It&nbsp;might&nbsp;be&nbsp;easier&nbsp;to&nbsp;understand&nbsp;to&nbsp;calculate</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;TimeSpan.MaxValue&nbsp;-&nbsp;TimeSpan.MinValue,&nbsp;but&nbsp;that&nbsp;underflows.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Instead,&nbsp;the&nbsp;number&nbsp;of&nbsp;possible&nbsp;64-bit&nbsp;integer&nbsp;values&nbsp;is&nbsp;the&nbsp;same</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;as&nbsp;the&nbsp;number&nbsp;of&nbsp;possible&nbsp;unsigned&nbsp;64-bit&nbsp;integer&nbsp;values.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">range</span>&nbsp;=&nbsp;<span style="color:blue;">ulong</span>.MaxValue; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">domain</span>&nbsp;=&nbsp;TimeSpan.FromHours(24).Ticks; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">scale</span>&nbsp;=&nbsp;(<span style="color:blue;">ulong</span>)domain&nbsp;/&nbsp;range; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;timeSpan&nbsp;*&nbsp;scale; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;expected; }</pre> </p> <p> In this case, Copilot didn't improve my productivity. It may actually have slowed me down a bit. </p> <p> This time, it wasn't too difficult to spot issues with the suggested code. What if the problems are more subtle? </p> <h3 id="02cb23fc57394a1c963a2f4ebe75ec48"> Errors that are difficult to detect <a href="#02cb23fc57394a1c963a2f4ebe75ec48" title="permalink">#</a> </h3> <p> How do bugs appear? We write them, thinking that our code is going to do one thing, while the compiler decides otherwise. Even when we actively engage with the code, we may be looking at a bug and still fail to see it. </p> <p> Why should we trust Copilot to produce bug-free code? </p> <p> Here's another example. I was going through the <a href="https://codingdojo.org/kata/Range/">Range kata</a> in F# and made my <code>Endpoint</code> discriminated union a <a href="/2018/03/22/functors">functor</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Endpoint&lt;&#39;a&gt;&nbsp;=&nbsp;Open&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a&nbsp;|&nbsp;Closed&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a <span style="color:blue;">module</span>&nbsp;Endpoint&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Open&nbsp;&nbsp;&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Open&nbsp;&nbsp;&nbsp;(f&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Closed&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Closed&nbsp;(f&nbsp;x)</pre> </p> <p> Making a type a functor is such a well-understood process that <a href="https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/compiler/derive-functor">Haskell can automate it</a>. I knew how to write the above <code>map</code> function as well, so I wrote it without testing it. </p> <p> After I'd written the <code>map</code> function, I decided to add tests of the two functor laws. Not because I doubted the implementation, but because I might need that as example code later. </p> <p> Copilot suggested this test after I named it: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;Xunit <span style="color:blue;">open</span>&nbsp;Hedgehog [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``First&nbsp;functor&nbsp;law``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&nbsp;=&nbsp;Gen.int32&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;f&nbsp;=&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;+&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;g&nbsp;=&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;*&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Endpoint.map&nbsp;f&nbsp;(Endpoint.map&nbsp;g&nbsp;(Open&nbsp;x)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;Endpoint.map&nbsp;(f&nbsp;&gt;&gt;&nbsp;g)&nbsp;(Open&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual)&nbsp;}</pre> </p> <p> It compiles. It also fails. </p> <p> What's wrong with it? </p> <p> If you know the functor laws by heart, you'll be able to spot that this is clearly not the first functor law. On the other hand, it looks convincingly like the second functor law. Should I just change the name and move on? </p> <p> I can't, though, since the test fails. Could there be a bug in my <code>map</code> function, after all? </p> <p> No, there's an error in the test. I invite you to spot it. </p> <p> In terms of keystrokes, it's easy to fix the problem: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;Xunit <span style="color:blue;">open</span>&nbsp;Hedgehog [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``First&nbsp;functor&nbsp;law``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&nbsp;=&nbsp;Gen.int32&nbsp;(Range.linearBounded&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;f&nbsp;=&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;+&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;g&nbsp;=&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;*&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Endpoint.map&nbsp;f&nbsp;(Endpoint.map&nbsp;g&nbsp;(Open&nbsp;x)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;Endpoint.map&nbsp;(f&nbsp;&lt;&lt;&nbsp;g)&nbsp;(Open&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual)&nbsp;}</pre> </p> <p> Spot the edit. I bet it'll take you longer to find it than it took me to type it. </p> <p> The test now passes, but for one who has spent less time worrying over functor laws than I have, troubleshooting this could have taken a long time. </p> <p> These almost-right suggestions from Copilot both worry me and give me hope. </p> <h3 id="7c90d2bd03054906a5f2505baaf78a31"> Copilot for experienced programmers <a href="#7c90d2bd03054906a5f2505baaf78a31" title="permalink">#</a> </h3> <p> When a new technology like Copilot appears, it's natural to speculate on the consequences. <em>Does this mean that programmers will lose their jobs?</em> </p> <p> This is just a preliminary evaluation after a few months, so I could be wrong, but I think we programmers are safe. If you're experienced, you'll be able to tell most of Copilot's hits from its misses. Perhaps you'll get a productivity improvement out of, but it could also slow you down. </p> <p> The tool is likely to improve over time, so I'm hopeful that this could become a net productivity gain. Still, with this high an error rate, I'm not too worried yet. </p> <p> <a href="/ref/pragmatic-programmer">The Pragmatic Programmer</a> describes a programming style named <em>Programming by Coincidence</em>. People who develop software this way have only a partial understanding of the code they write. </p> <blockquote> <p> "Fred doesn't know why the code is failing because <em>he didn't know why it worked in the first place.</em>" </p> <footer><cite>Andy Hunt and Dave Thomas, <a href="/ref/pragmatic-programmer">The Pragmatic Programmer</a></cite></footer> </blockquote> <p> I've encountered my fair share of these people. When editing code, they make small adjustments and do cursory manual testing until 'it looks like it works'. If they have to start a new feature or are otherwise faced with a metaphorical blank page, they'll copy some code from somewhere else and use that as a starting point. </p> <p> You'd think that Copilot could enhance the productivity of such people, but I'm not sure. It might actually slow them down. These people don't fully understand the code they themselves 'write', so why should we expect them to understand the code that Copilot suggests? </p> <p> If faced with a Copilot suggestion that 'almost works', will they be able to spot if it's a genuinely good suggestion, or whether it's off, like I've described above? If the Copilot code doesn't work, how much time will they waste thrashing? </p> <h3 id="f3e17cbf9dbc41f19ae46974d2f28a90"> Conclusion <a href="#f3e17cbf9dbc41f19ae46974d2f28a90" title="permalink">#</a> </h3> <p> GitHub Copilot has the potential to be a revolutionary technology, but it's not, yet. So far, I'm not too worried. It's an assistant, like a pairing partner, but it's up to you to evaluate whether the code that Copilot suggests is useful, correct, and safe. How can you do that unless you already know what you're doing? </p> <p> If you don't have the qualifications to evaluate the suggested code, I fail to see how it's going to help you. Granted, it does have potential to help you move on in less time that you would otherwise have spent. In this article, I showed one example where I would have had to spend significant time looking up API documentation. Instead, Copilot suggested the correct code to use. </p> <p> Pulling in the other direction are the many <a href="https://en.wikipedia.org/wiki/False_positives_and_false_negatives">false positives</a>. Copilot makes many suggestions, and many of them are poor. The ones that are recognisably bad are unlikely to slow you down. I'm more concerned with those that are subtly wrong. They have the potential to waste much time. </p> <p> Which of these forces are strongest? The potential for wasting time is infinite, while the maximum productivity gain you can achieve is 100 percent. That's an asymmetric distribution. There's a long tail of time wasters, but there's no equivalent long tail of improvement. </p> <p> I'm not, however, trying to be pessimistic. I expect to keep Copilot around for the time being. It could very well be here to stay. Used correctly, it seems useful. </p> <p> Is it going to replace programmers? Hardly. Rather, it may enable poor developers to make such a mess of things that you need even more good programmers to subsequently fix things. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An initial proof of concept of applicative assertions in C# https://blog.ploeh.dk/2022/11/28/an-initial-proof-of-concept-of-applicative-assertions-in-c 2022-11-28T06:47:00+00:00 Mark Seemann <div id="post"> <p> <em>Worthwhile? Not obviously.</em> </p> <p> This article is the first instalment in a small articles series about <a href="/2022/11/07/applicative-assertions">applicative assertions</a>. It explores a way to compose assertions in such a way that failure messages accumulate rather than short-circuit. It assumes that you've read the <a href="/2022/11/07/applicative-assertions">article series introduction</a>. </p> <p> Assertions are typically based on throwing exceptions. As soon as one assertion fails, an exception is thrown and no further assertions are evaluated. This is normal short-circuiting behaviour of exceptions. In some cases, however, it'd be useful to keep evaluating other assertions and collect error messages. </p> <p> This article series explores <a href="https://twitter.com/lucasdicioccio/status/1572264819109003265">an intriguing idea</a> to address such issues: Use an <a href="/2018/10/01/applicative-functors">applicative functor</a> to collect multiple assertion messages. I started experimenting with the idea to see where it would lead. The article series serves as a report of what I found. It is neither a recommendation nor a caution. I still find the idea interesting, but I'm not sure whether the complexity is warranted. </p> <h3 id="877c6f5ee5b34ecf990d1126b8139720"> Example scenario <a href="#877c6f5ee5b34ecf990d1126b8139720" title="permalink">#</a> </h3> <p> A realistic example is often illustrative, although there's a risk that the realism carries with it some noise that detracts from the core of the matter. I'll reuse an example that I've <a href="https://stackoverflow.blog/2022/11/03/multiple-assertions-per-test-are-fine/">already discussed and explained in greater detail</a>. The code is from the code base that accompanies my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> This test has two independent assertions: </p> <p> <pre>[Theory] [InlineData(884,&nbsp;18,&nbsp;47,&nbsp;<span style="color:#a31515;">&quot;c@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Nick&nbsp;Klimenko&quot;</span>,&nbsp;2)] [InlineData(902,&nbsp;18,&nbsp;50,&nbsp;<span style="color:#a31515;">&quot;emot@example.gov&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Emma&nbsp;Otting&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;DeleteReservation( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;days,&nbsp;<span style="color:blue;">int</span>&nbsp;hours,&nbsp;<span style="color:blue;">int</span>&nbsp;minutes,&nbsp;<span style="color:blue;">string</span>&nbsp;email,&nbsp;<span style="color:blue;">string</span>&nbsp;name,&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;api&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;LegacyApi(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;=&nbsp;DateTime.Today.AddDays(days).At(hours,&nbsp;minutes) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToIso8601DateTimeString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;Create.ReservationDto(at,&nbsp;email,&nbsp;name,&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;postResp&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.PostReservation(dto); &nbsp;&nbsp;&nbsp;&nbsp;Uri&nbsp;address&nbsp;=&nbsp;FindReservationAddress(postResp); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;deleteResp&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateClient().DeleteAsync(address); &nbsp;&nbsp;&nbsp;&nbsp;Assert.True( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deleteResp.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{deleteResp.StatusCode}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;getResp&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateClient().GetAsync(address); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(HttpStatusCode.NotFound,&nbsp;getResp.StatusCode); }</pre> </p> <p> The test exercises the REST API to first create a reservation, then delete it, and finally check that the reservation no longer exists. Two independent postconditions must be true for the test to pass: </p> <ul> <li>The <code>DELETE</code> request must result in a status code that indicates success.</li> <li>The resource must no longer exist.</li> </ul> <p> It's conceivable that a bug might fail one of these without invalidating the other. </p> <p> As the test is currently written, it uses <a href="https://xunit.net/">xUnit.net</a>'s standard assertion library. If the <code>Assert.True</code> verification fails, the <code>Assert.Equal</code> statement isn't evaluated. </p> <h3 id="cd132aaee5484068a890cc4b7995bed3"> Assertions as validations <a href="#cd132aaee5484068a890cc4b7995bed3" title="permalink">#</a> </h3> <p> Is it possible to evaluate the <code>Assert.Equal</code> postcondition even if the first assertion fails? You could use a <code>try/catch</code> block, but is there a more composable and elegant option? How about an applicative functor? </p> <p> Since I was interested in exploring this question as a proof of concept, I decided to reuse the machinery that I'd already put in place for the article <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">An applicative reservation validation example in C#</a>: The <code>Validated</code> class and its associated functions. In a sense, you can think of an assertion as a validation of a postcondition. </p> <p> This is not a resemblance I intend to carry too far. What I learn by experimenting with <code>Validated</code> I can apply to a more appropriately-named class like <code>Asserted</code>. </p> <p> Neither of the two above assertions return a value; they are one-stop assertions. If they succeed, they return nothing; if they fail, they produce an error. </p> <p> It's possible to model this kind of behaviour with <code>Validated</code>. You can model a collection of errors with, well, a collection. To keep the proof of concept simple, I decided to use a collection of strings: <code>IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;</code>. To model 'nothing' I had to add a <a href="/2018/01/15/unit-isomorphisms">unit</a> type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Unit</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Unit</span>()&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Unit&nbsp;Value&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Unit(); }</pre> </p> <p> This enabled me to define assertions as <code>Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;</code> values: Either a collection of error messages, or nothing. </p> <h3 id="5ac18fb532c04876b00f7de080905a16"> Asserting truth <a href="#5ac18fb532c04876b00f7de080905a16" title="permalink">#</a> </h3> <p> Instead of xUnit.net's <code>Assert.True</code>, you can now define an equivalent function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;&nbsp;AssertTrue( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;condition, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;message) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;condition &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;Succeed&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;(Unit.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Fail&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;message&nbsp;}); }</pre> </p> <p> It simply returns a <code>Success</code> value containing nothing when <code>condition</code> is <code>true</code>, and otherwise a <code>Failure</code> value containing the error <code>message</code>. </p> <p> You can use it like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;assertResponse&nbsp;=&nbsp;Validated.AssertTrue( &nbsp;&nbsp;&nbsp;&nbsp;deleteResp.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{deleteResp.StatusCode}<span style="color:#a31515;">.&quot;</span>);</pre> </p> <p> Later in the article you'll see how this assertion combines with another assertion. </p> <h3 id="adc98673e4734918ac983a5bbb520e15"> Asserting equality <a href="#adc98673e4734918ac983a5bbb520e15" title="permalink">#</a> </h3> <p> Instead of xUnit.net's <code>Assert.Equal</code>, you can also define a function that works the same way but returns a <code>Validated</code> value: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;&nbsp;AssertEqual&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;expected, &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;actual) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(expected,&nbsp;actual) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;Succeed&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;(Unit.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Fail&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;(<span style="color:blue;">new</span>[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#a31515;">$&quot;Expected&nbsp;</span>{expected}<span style="color:#a31515;">,&nbsp;but&nbsp;got&nbsp;</span>{actual}<span style="color:#a31515;">.&quot;</span>&nbsp;}); }</pre> </p> <p> The <code>AssertEqual</code> function first uses <a href="https://learn.microsoft.com/dotnet/api/system.object.equals">Equals</a> to compare <code>expected</code> with <code>actual</code>. If the result is <code>true</code>, the function returns a <code>Success</code> value containing nothing; otherwise, it returns a <code>Failure</code> value containing a failure message. Since this is only a proof of concept, the failure message is useful, but minimal. </p> <p> Notice that this function returns a value of the same type (<code>Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;</code>) as <code>AssertTrue</code>. </p> <p> You can use the function like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;assertState&nbsp;=&nbsp;Validated.AssertEqual(HttpStatusCode.NotFound,&nbsp;getResp.StatusCode);</pre> </p> <p> Again, you'll see how to combine this assertion with the above <code>assertResponse</code> value later in this article. </p> <h3 id="1d29ecff5ea64911918168cb985ed6b5"> Evaluating assertions <a href="#1d29ecff5ea64911918168cb985ed6b5" title="permalink">#</a> </h3> <p> The <code>DeleteReservation</code> test only has two independent assertions, so in my proof of concept, all I needed to do was to figure out a way to combine two applicative assertions into one, and then evaluate it. This rather horrible method does that: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;RunAssertions( &nbsp;&nbsp;&nbsp;&nbsp;Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;&nbsp;assertion1, &nbsp;&nbsp;&nbsp;&nbsp;Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;&nbsp;assertion2) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;f&nbsp;=&nbsp;Succeed&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Func&lt;Unit,&nbsp;Unit,&nbsp;Unit&gt;&gt;((_,&nbsp;__)&nbsp;=&gt;&nbsp;Unit.Value); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine&nbsp;=&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;x.Concat(y).ToArray(); &nbsp;&nbsp;&nbsp;&nbsp;Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;&nbsp;composition&nbsp;=&nbsp;f &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(assertion1,&nbsp;combine) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(assertion2,&nbsp;combine); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;errors&nbsp;=&nbsp;composition.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onFailure:&nbsp;f&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.Join(Environment.NewLine,&nbsp;f), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onSuccess:&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.Empty); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:blue;">string</span>.IsNullOrEmpty(errors)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Exception(errors);</pre> </p> <p> C# doesn't have good language features for applicative functors the same way that <a href="https://fsharp.org/">F#</a> and <a href="https://www.haskell.org/">Haskell</a> do, and although you can use various tricks to make the programming experience better that what is on display here, I was still doing a proof of concept. If it turns out that this approach is useful and warranted, we can introduce some of the facilities to make the API more palatable. For now, though, we're dealing with all the rough edges. </p> <p> The way that applicative functors work, you typically use a 'lifted' function to combine two (or more) 'lifted' values. Here, 'lifted' means 'being inside the <code>Validated</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers/">container</a>'. </p> <p> Each of the assertions that I want to combine has the same type: <code>Validated&lt;IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;,&nbsp;Unit&gt;</code>. Notice that the <code>S</code> (<em>success</em>) generic type argument is <code>Unit</code> in both cases. While it seems redundant, formally I needed a 'lifted' function to combine two <code>Unit</code> values into a single value. This single value can (in principle) have any type I'd like it to have, but since you can't extract any information out of a <code>Unit</code> value, it makes sense to use the <a href="/2018/01/15/unit-isomorphisms#4657dbd4b5fc4abda6e8dee2cac67ea9">monoidal nature of unit</a> to combine two into one. </p> <p> Basically, you just ignore the <code>Unit</code> input values because they carry no information. Also, they're all the same value anyway, since the type is a <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a>. In its 'naked' form, the function might be implemented like this: <code>(_,&nbsp;__)&nbsp;=&gt;&nbsp;Unit.Value</code>. Due to the <a href="/2019/12/16/zone-of-ceremony">ceremony</a> required by the combination of C# and applicative functors, however, this <a href="/2017/10/06/monoids">monoidal</a> binary operation has to be 'lifted' to a <code>Validated</code> value. That's the <code>f</code> value in the <code>RunAssertions</code> function body. </p> <p> The <code>Validated.Apply</code> function requires as an argument a function that combines the generic <code>F</code> (<em>failure</em>) values into one, in order to deal with the case where there's multiple failures. In this case <code>F</code> is <code>IReadOnlyCollection&lt;<span style="color:blue;">string</span>&gt;</code>. Since declarations of <code>Func</code> values in C# requires explicit type declaration, that's a bit of a mouthful, but the <code>combine</code> function just concatenates two collections into one. </p> <p> The <code>RunAssertions</code> method can now <code>Apply</code> both <code>assertion1</code> and <code>assertion2</code> to <code>f</code>, which produces a combined <code>Validated</code> value, <code>composition</code>. It then matches on the combined value to produce a <code>string</code> value. If there are no assertion messages, the result is the empty string; otherwise, the function combines the assertion messages with a <code>NewLine</code> between each. Again, this is proof-of-concept code. A more robust and flexible API (if warranted) might keep the errors around as a collection of strongly typed <a href="https://martinfowler.com/bliki/ValueObject.html">Value Objects</a>. </p> <p> Finally, if the resulting <code>errors</code> string is not null or empty, the <code>RunAssertions</code> method throws an exception with the combined error message(s). Here I once more invoked my proof-of-concept privilege to throw an <a href="https://learn.microsoft.com/dotnet/api/system.exception">Exception</a>, even though <a href="https://learn.microsoft.com/dotnet/standard/design-guidelines/using-standard-exception-types">the framework design guidelines admonishes against doing so</a>. </p> <p> Ultimately, then, the <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">assert phase</a> of the test looks like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;assertResponse&nbsp;=&nbsp;Validated.AssertTrue( &nbsp;&nbsp;&nbsp;&nbsp;deleteResp.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{deleteResp.StatusCode}<span style="color:#a31515;">.&quot;</span>); <span style="color:blue;">var</span>&nbsp;getResp&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateClient().GetAsync(address); <span style="color:blue;">var</span>&nbsp;assertState&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;Validated.AssertEqual(HttpStatusCode.NotFound,&nbsp;getResp.StatusCode); Validated.RunAssertions(assertResponse,&nbsp;assertState);</pre> </p> <p> The rest of the test hasn't changed. </p> <h3 id="42e68063b7a3472e960031fde5e3f40d"> Outcomes <a href="#42e68063b7a3472e960031fde5e3f40d" title="permalink">#</a> </h3> <p> Running the test with the applicative assertions passes, as expected. In order to verify that it works as it's supposed to, I tried to sabotage the System Under Test (SUT) in various ways. First, I made the <code>Delete</code> method that handles <code>DELETE</code> requests a <a href="https://en.wikipedia.org/wiki/NOP_(code)">no-op</a>, while still returning <code>200 OK</code>. As you'd expect, the result is a test failure with this message: </p> <p> <pre>Message: System.Exception : Expected NotFound, but got OK.</pre> </p> <p> This is the assertion that verifies that <code>getResp.StatusCode</code> is <code>404 Not Found</code>. It fails because the sabotaged <code>Delete</code> method doesn't delete the reservation. </p> <p> Then I further sabotaged the SUT to also return an incorrect status code (<code>400 Bad Request</code>), which produced this failure message: </p> <p> <pre>Message: System.Exception : Actual status code: BadRequest. Expected NotFound, but got OK.</pre> </p> <p> Notice that the message contains information about both failure conditions. </p> <p> Finally, I re-enabled the correct behaviour (deleting the reservation from the data store) while still returning <code>400 Bad Request</code>: </p> <p> <pre>Message: System.Exception : Actual status code: BadRequest.</pre> </p> <p> As desired, the assertions collect all relevant failure messages. </p> <h3 id="4a2c496da999408ea6c6cddd6a4f33d4"> Conclusion <a href="#4a2c496da999408ea6c6cddd6a4f33d4" title="permalink">#</a> </h3> <p> Not surprisingly, it's possible to design a composable assertion API that collects multiple failure messages using an applicative functor. Anyone who knows how <a href="/2018/11/05/applicative-validation">applicative validation</a> works would have been able to predict that outcome. That's not what the above proof of concept was about. What I wanted to see was rather how it would play out in a realistic scenario, and whether using an applicative functor is warranted. </p> <p> Applicative functors don't gel well with C#, so unsurprisingly the API is awkward. It's likely possible to smooth much of the friction, but without good language support and syntactic sugar, it's unlikely to become <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> C#. </p> <p> Rather than taking the edge off the unwieldy API, the implementation of <code>RunAssertions</code> suggests another alternative. </p> <p> <strong>Next:</strong> <a href="/2022/12/19/error-accumulating-composable-assertions-in-c">Error-accumulating composable assertions in C#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Decouple to delete https://blog.ploeh.dk/2022/11/21/decouple-to-delete 2022-11-21T08:46:00+00:00 Mark Seemann <div id="post"> <p> <em>Don't try to predict the future.</em> </p> <p> Do you know why it's called <a href="https://en.wikipedia.org/wiki/Spaghetti_code">spaghetti code</a>? It's a palatable metaphor. You may start with a single spaghetto, but usually, as you wind your fork around it, the whole dish follows along. Unless you're careful, eating spaghetti can be a mess. </p> <p> <img src="/content/binary/spaghetti-le-calandre.jpg" alt="A small spaghetti serving."> </p> <p> Spaghetti code is tangled and everything is directly or transitively connected to everything else. As you try to edit the code, every change you make affects other code. Fix one thing and another thing breaks, cascading through the code base. </p> <p> I was recently <a href="https://www.goodreads.com/review/show/4913194780">reading Clean Architecture</a>, and as <a href="https://en.wikipedia.org/wiki/Robert_C._Martin">Robert C. Martin</a> was explaining the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> for the umpteenth time, my brain made a new connection. To be clear: Connecting (coupling) code is bad, but connecting ideas is good. </p> <h3 id="51ca1981115f493bb24ac9338419fe91"> What a tangled web we weave <a href="#51ca1981115f493bb24ac9338419fe91" title="permalink">#</a> </h3> <p> It's impractical to write code that depends on nothing else. Most code will call other code, which again calls other code. It behoves us, though, to be careful that the web of dependencies don't get too tangled. </p> <p> Imagine a code base where the dependency graph looks like this: </p> <p> <img src="/content/binary/tangled-dependency-graph.png" alt="A connected graph."> </p> <p> Think of each node as a unit of code; a class or a module. While a dependency graph is a <a href="https://en.wikipedia.org/wiki/Directed_graph">directed graph</a>, I didn't indicate the directions. Imagine that most edges point both ways, so that the nodes are interdependent. In other ways, the graph has <a href="https://en.wikipedia.org/wiki/Cycle_(graph_theory)">cycles</a>. This is <a href="http://evelinag.com/blog/2014/06-09-comparing-dependency-networks/">not uncommon in C# code</a>. </p> <p> Pick any node in such a graph, and chances are that other nodes depend on it. This makes it hard to make changes to the code in that node, because a change may affect the code that depends on it. As you try to fix the depending code, that change, too, ripples through the network. </p> <p> This already explains why tight coupling is problematic. </p> <h3 id="61e4982dad794d0085dd7240508f73b7"> It is difficult to make predictions, especially about the future <a href="#61e4982dad794d0085dd7240508f73b7" title="permalink">#</a> </h3> <p> When you write source code, you might be tempted to try to take into account future needs and requirements. There may be a historical explanation for that tendency. </p> <blockquote> <p> "That is, once it was a sign of failure to change product code. You should have gotten it right the first time." </p> <footer><cite><a href="https://twitter.com/marick/status/1566564277573525507">Brian Marick</a></cite></footer> </blockquote> <p> In the days of punchcards, you had to schedule time to use a computer. If you made a mistake in your program, you typically didn't have time to fix it during your timeslot. A mistake could easily cost you days as you scrambled to schedule a new time. Not surprisingly, emphasis was on correctness. </p> <p> With this mindset, it's natural to attempt to future-proof code. </p> <h3 id="0f12f362efaf461bbbdf9bddd2e01361"> YAGNI <a href="#0f12f362efaf461bbbdf9bddd2e01361" title="permalink">#</a> </h3> <p> With interactive development environments you can get rapid feedback. If you make a mistake, change the code and observe the outcome. Don't add code because you think that you might need it later. <a href="https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it">You probably will not</a>. </p> <p> While you should avoid <a href="https://wiki.c2.com/?SpeculativeGenerality">speculative generality</a>, that alone is no guarantee of clean code. Unless you're careful, you can easily make a mess by tightly coupling different parts of your code base. </p> <p> How do produce a code base that is as easy to change as possible? </p> <h3 id="89bda4d0fd774c7c969c30e1562aa11b"> Write code that is easy to delete <a href="#89bda4d0fd774c7c969c30e1562aa11b" title="permalink">#</a> </h3> <p> Write code that is easy to change. The ultimate change you can make is to delete code. After that, you can write something else that better does what you need. </p> <blockquote> <p> "A system where you can delete parts without rewriting others is often called loosely coupled" </p> <footer><cite><a href="https://programmingisterrible.com/post/139222674273/how-to-write-disposable-code-in-large-systems">tef</a></cite></footer> </blockquote> <p> I don't mean that you should always delete code in order to make changes, but often, looking at extremes can provide insights into less extreme cases. </p> <p> When you have a tangled web as shown above, most of the code is coupled to other parts. If you delete a node, then you break something else. You'd think that deleting code is the easiest thing in the world, but it's not. </p> <p> What if, on the other hand, you have smaller clusters of nodes that are independent? </p> <p> <img src="/content/binary/less-coupled-dependency-graph.png" alt="A disconnected graph with small islands of connected graphs."> </p> <p> If your dependency graph looks like this, you can at least delete each of the 'islands' without impacting the other sub-graphs. </p> <p> <img src="/content/binary/dependency-graph-without-deleted-subgraph.png" alt="The graph from the previous figure, less one sub-graph."> </p> <p> <a href="https://programmingisterrible.com/post/139222674273/how-to-write-disposable-code-in-large-systems">Writing code that is easy to delete</a> may be a good idea, but even <em>that</em> is easier said that done. Loose coupling is, once more, key to good architecture. </p> <h3 id="88c387fb34bc48b382d1eefdc3ee6367"> Add something better <a href="#88c387fb34bc48b382d1eefdc3ee6367" title="permalink">#</a> </h3> <p> Once you've deleted a cluster of code, you have the opportunity to add something that is even less coupled than the island you deleted. </p> <p> <img src="/content/binary/dependency-graph-with-new-subgraphs.png" alt="The graph from the previous figure, with new small graphs added."> </p> <p> If you add new code that is less coupled than the code you deleted, it's even easier to delete again. </p> <h3 id="8a7a8d9547eb4d3881a1cced603f8422"> Conclusion <a href="#8a7a8d9547eb4d3881a1cced603f8422" title="permalink">#</a> </h3> <p> Coupling is a key factor in code organisation. Tightly coupled code is difficult to change. Loosely coupled code is easier to change. As a thought experiment, consider how difficult it would be to delete a particular piece of code. The easier it is to delete the code, the less coupled it is. </p> <p> Deleting a small piece of code to add new code in its stead is the ultimate change. You can often get by with a less radical edit, but if all else fails, delete part of your code base and start over. The less coupled the code is, the easier it is to change. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Reader monad https://blog.ploeh.dk/2022/11/14/the-reader-monad 2022-11-14T06:50:00+00:00 Mark Seemann <div id="post"> <p> <em>Normal functions form monads. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. A previous article described <a href="/2021/08/30/the-reader-functor">the Reader functor</a>. As is the case with many (but not all) <a href="/2018/03/22/functors">functors</a>, Readers also form monads. </p> <p> This article continues where the Reader functor article stopped. It uses the same code base. </p> <h3 id="d54d83f22d854e94853271e1a559a1d8"> Flatten <a href="#d54d83f22d854e94853271e1a559a1d8" title="permalink">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function, although you can use other names for both of these functions. <code>Flatten</code> is in my opinion a more intuitive name than <code>join</code>, since a monad is really just a functor that you can flatten. Flattening is relevant if you have a nested functor; in this case a Reader within a Reader. You can flatten such a nested Reader with a <code>Flatten</code> function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReader&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#74531f;">Flatten</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IReader&lt;R,&nbsp;IReader&lt;R,&nbsp;A&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;FlattenReader&lt;R,&nbsp;A&gt;(source); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FlattenReader</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt;&nbsp;:&nbsp;IReader&lt;R,&nbsp;A&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IReader&lt;R,&nbsp;IReader&lt;R,&nbsp;A&gt;&gt;&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FlattenReader</span>(IReader&lt;R,&nbsp;IReader&lt;R,&nbsp;A&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.source&nbsp;=&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;A&nbsp;<span style="color:#74531f;">Run</span>(R&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">newReader</span>&nbsp;=&nbsp;source.Run(environment); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;newReader.Run(environment); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since the <code>source</code> Reader is nested, calling its <code>Run</code> method once returns a <code>newReader</code>. You can <code>Run</code> that <code>newReader</code> one more time to get an <code>A</code> value to return. </p> <p> You could easily chain the two calls to <code>Run</code> together, one after the other. That would make the code terser, but here I chose to do it in two explicit steps in order to show what's going on. </p> <p> Like the previous article about <a href="/2022/06/20/the-state-monad">the State monad</a>, a lot of <a href="/2019/12/16/zone-of-ceremony">ceremony</a> is required because this variation of the Reader monad is defined with an interface. You could also define the Reader monad on a 'raw' function of the type <code>Func&lt;R, A&gt;</code>, in which case <code>Flatten</code> would be simpler: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#74531f;">Flatten</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt;(<span style="color:blue;">this</span>&nbsp;Func&lt;R,&nbsp;Func&lt;R,&nbsp;A&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">environment</span>&nbsp;=&gt;&nbsp;source(environment)(environment); }</pre> </p> <p> In this variation <code>source</code> is a function, so you can call it with <code>environment</code>, which returns another function that you can again call with <code>environment</code>. This produces an <code>A</code> value for the function to return. </p> <h3 id="e2f72c66681d45949a23a7e574ae5ae7"> SelectMany <a href="#e2f72c66681d45949a23a7e574ae5ae7" title="permalink">#</a> </h3> <p> When you have <code>Flatten</code> you can always define <code>SelectMany</code> (<em>monadic bind</em>) like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReader&lt;R,&nbsp;B&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">B</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IReader&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;A,&nbsp;IReader&lt;R,&nbsp;B&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.Select(selector).Flatten(); }</pre> </p> <p> First use functor-based mapping. Since the <code>selector</code> returns a Reader, this mapping produces a Reader within a Reader. That's exactly the situation that <code>Flatten</code> addresses. </p> <p> The above <code>SelectMany</code> example works with the <code>IReader&lt;R,&nbsp;A&gt;</code> interface, but the 'raw' function version has the exact same implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;R,&nbsp;B&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">B</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Func&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;A,&nbsp;Func&lt;R,&nbsp;B&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.Select(selector).Flatten(); }</pre> </p> <p> Only the method declaration differs. </p> <h3 id="8c5f94f7395040a7bcfdb2561c8e3ed3"> Query syntax <a href="#8c5f94f7395040a7bcfdb2561c8e3ed3" title="permalink">#</a> </h3> <p> Monads also enable query syntax in C# (just like they enable other kinds of syntactic sugar in languages like <a href="https://fsharp.org/">F#</a> and <a href="https://www.haskell.org">Haskell</a>). As outlined in the <a href="/2022/03/28/monads">monad introduction</a>, however, you must add a special <code>SelectMany</code> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReader&lt;R,&nbsp;T1&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IReader&lt;R,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;IReader&lt;R,&nbsp;U&gt;&gt;&nbsp;<span style="color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;U,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> As already predicted in the monad introduction, this boilerplate overload is always implemented in the same way. Only the signature changes. With it, you could write an expression like this nonsense: </p> <p> <pre>IReader&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">r</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;dur&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">new</span>&nbsp;MinutesReader() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;b&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Thingy(dur) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;b;</pre> </p> <p> Where <code>MinutesReader</code> was already shown in the article <a href="/2021/10/04/reader-as-a-contravariant-functor">Reader as a contravariant functor</a>. I couldn't come up with a good name for another reader, so I went with <a href="https://dannorth.net">Dan North</a>'s naming convention that if you don't yet know what to call a class, method, or function, don't <em>pretend</em> that you know. Be explicit that you don't know. </p> <p> Here it is, for the sake of completion: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Thingy</span>&nbsp;:&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;TimeSpan&nbsp;timeSpan; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Thingy</span>(TimeSpan&nbsp;<span style="color:#1f377f;">timeSpan</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.timeSpan&nbsp;=&nbsp;timeSpan; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Run</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(timeSpan.Ticks&nbsp;*&nbsp;environment).TotalDays&nbsp;&lt;&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I'm not claiming that this class makes sense. These articles are deliberate kept abstract in order to focus on structure and behaviour, rather than on practical application. </p> <h3 id="d5d827cf83d242e6baecbda622ca5cb9"> Return <a href="#d5d827cf83d242e6baecbda622ca5cb9" title="permalink">#</a> </h3> <p> Apart from flattening or monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that Haskell uses): </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReader&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#74531f;">Return</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt;(A&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ReturnReader&lt;R,&nbsp;A&gt;(a); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReturnReader</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt;&nbsp;:&nbsp;IReader&lt;R,&nbsp;A&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;A&nbsp;a; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReturnReader</span>(A&nbsp;<span style="color:#1f377f;">a</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.a&nbsp;=&nbsp;a; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;A&nbsp;<span style="color:#74531f;">Run</span>(R&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;a; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation returns the <code>a</code> value and completely ignores the <code>environment</code>. You can do the same with a 'naked' function. </p> <h3 id="bd79fe3d97644f0f9edb12f08a0b5d01"> Left identity <a href="#bd79fe3d97644f0f9edb12f08a0b5d01" title="permalink">#</a> </h3> <p> We need to identify the <em>return</em> function in order to examine <a href="/2022/04/11/monad-laws">the monad laws</a>. Now that this is accomplished, let's see what the laws look like for the Reader monad, starting with the left identity law. </p> <p> <pre>[Theory] [InlineData(UriPartial.Authority,&nbsp;<span style="color:#a31515;">&quot;https://example.com/f?o=o&quot;</span>)] [InlineData(UriPartial.Path,&nbsp;<span style="color:#a31515;">&quot;https://example.net/b?a=r&quot;</span>)] [InlineData(UriPartial.Query,&nbsp;<span style="color:#a31515;">&quot;https://example.org/b?a=z&quot;</span>)] [InlineData(UriPartial.Scheme,&nbsp;<span style="color:#a31515;">&quot;https://example.gov/q?u=x&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LeftIdentity</span>(UriPartial&nbsp;<span style="color:#1f377f;">a</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">u</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;UriPartial,&nbsp;IReader&lt;Uri,&nbsp;UriPartial&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">up</span>&nbsp;=&gt;&nbsp;Reader.Return&lt;Uri,&nbsp;UriPartial&gt;(up); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;UriPartial,&nbsp;IReader&lt;Uri,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">up</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;UriPartReader(up); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@return(a).SelectMany(h).Run(<span style="color:blue;">new</span>&nbsp;Uri(u)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h(a).Run(<span style="color:blue;">new</span>&nbsp;Uri(u))); }</pre> </p> <p> In order to compare the two Reader values, the test has to <code>Run</code> them and then compare the return values. </p> <p> This test and the next uses a Reader implementation called <code>UriPartReader</code>, which almost makes sense: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">UriPartReader</span>&nbsp;:&nbsp;IReader&lt;Uri,&nbsp;<span style="color:blue;">string</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;UriPartial&nbsp;part; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">UriPartReader</span>(UriPartial&nbsp;<span style="color:#1f377f;">part</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.part&nbsp;=&nbsp;part; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Run</span>(Uri&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;environment.GetLeftPart(part); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Almost. </p> <h3 id="ad9edb1097be4eeb96170809272851eb"> Right identity <a href="#ad9edb1097be4eeb96170809272851eb" title="permalink">#</a> </h3> <p> In a similar manner, we can showcase the right identity law as a test. </p> <p> <pre>[Theory] [InlineData(UriPartial.Authority,&nbsp;<span style="color:#a31515;">&quot;https://example.com/q?u=ux&quot;</span>)] [InlineData(UriPartial.Path,&nbsp;<span style="color:#a31515;">&quot;https://example.net/q?u=uuz&quot;</span>)] [InlineData(UriPartial.Query,&nbsp;<span style="color:#a31515;">&quot;https://example.org/c?o=rge&quot;</span>)] [InlineData(UriPartial.Scheme,&nbsp;<span style="color:#a31515;">&quot;https://example.gov/g?a=rply&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">RightIdentity</span>(UriPartial&nbsp;<span style="color:#1f377f;">a</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">u</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;UriPartial,&nbsp;IReader&lt;Uri,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">up</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;UriPartReader(up); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IReader&lt;Uri,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Reader.Return&lt;Uri,&nbsp;<span style="color:blue;">string</span>&gt;(s); &nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;Uri,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.SelectMany(@return).Run(<span style="color:blue;">new</span>&nbsp;Uri(u)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.Run(<span style="color:blue;">new</span>&nbsp;Uri(u))); }</pre> </p> <p> As always, even a parametrised test constitutes no <em>proof</em> that the law holds. I show the tests to illustrate what the laws look like in 'real' code. </p> <h3 id="67a2f225bd8f432dbeed30c2ba2b623a"> Associativity <a href="#67a2f225bd8f432dbeed30c2ba2b623a" title="permalink">#</a> </h3> <p> The last monad law is the associativity law that describes how (at least) three functions compose. We're going to need three functions. For the purpose of demonstrating the law, any three pure functions will do. While the following functions are silly and not at all 'realistic', they have the virtue of being as simple as they can be (while still providing a bit of variety). They don't 'mean' anything, so don't worry too much about their behaviour. It is, as far as I can tell, nonsensical. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">F</span>&nbsp;:&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">char</span>&nbsp;c; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">F</span>(<span style="color:blue;">char</span>&nbsp;<span style="color:#1f377f;">c</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.c&nbsp;=&nbsp;c; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Run</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(c,&nbsp;environment); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">G</span>&nbsp;:&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">G</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.s&nbsp;=&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Run</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;environment&nbsp;&lt;&nbsp;42&nbsp;||&nbsp;s.Contains(<span style="color:#a31515;">&quot;a&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">H</span>&nbsp;:&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;TimeSpan&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;b; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">H</span>(<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">b</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.b&nbsp;=&nbsp;b; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TimeSpan&nbsp;<span style="color:#74531f;">Run</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;b&nbsp;? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromMinutes(environment)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromSeconds(environment); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Armed with these three classes, we can now demonstrate the Associativity law: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&#39;a&#39;</span>,&nbsp;0)] [InlineData(<span style="color:#a31515;">&#39;b&#39;</span>,&nbsp;1)] [InlineData(<span style="color:#a31515;">&#39;c&#39;</span>,&nbsp;42)] [InlineData(<span style="color:#a31515;">&#39;d&#39;</span>,&nbsp;2112)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Associativity</span>(<span style="color:blue;">char</span>&nbsp;<span style="color:#1f377f;">a</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">char</span>,&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">c</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;F(c); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;G(s); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">bool</span>,&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;TimeSpan&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;H(b); &nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.SelectMany(g).SelectMany(h).Run(i), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h)).Run(i)); }</pre> </p> <p> In case you're wondering, the four test cases produce the outputs <code>00:00:00</code>, <code>00:01:00</code>, <code>00:00:42</code>, and <code>00:35:12</code>. You can see that reproduced below: </p> <h3 id="a46f77dddd914f0b9c8926dd2c06e9d6"> Haskell <a href="#a46f77dddd914f0b9c8926dd2c06e9d6" title="permalink">#</a> </h3> <p> In Haskell, normal functions <code>a -&gt; b</code> are already <code>Monad</code> instances, which means that you can easily replicate the functions from the <code>Associativity</code> test: </p> <p> <pre>&gt; f c = \env -&gt; replicate env c &gt; g s = \env -&gt; env &lt; 42 || 'a' `elem` s &gt; h b = \env -&gt; if b then secondsToDiffTime (toEnum env * 60) else secondsToDiffTime (toEnum env)</pre> </p> <p> I've chosen to write the <code>f</code>, <code>g</code>, and <code>h</code> as functions that return lambda expressions in order to emphasise that each of these functions return Readers. Since Haskell functions are already curried, I could also have written them in the more normal function style with two normal parameters, but that might have obscured the Reader aspect of each. </p> <p> Here's the composition in action: </p> <p> <pre>&gt; f 'a' &gt;&gt;= g &gt;&gt;= h $ 0 0s &gt; f 'b' &gt;&gt;= g &gt;&gt;= h $ 1 60s &gt; f 'c' &gt;&gt;= g &gt;&gt;= h $ 42 42s &gt; f 'd' &gt;&gt;= g &gt;&gt;= h $ 2112 2112s</pre> </p> <p> In case you are wondering, 2,112 seconds is 35 minutes and 12 seconds, so all outputs fit with the results reported for the C# example. </p> <p> What the above Haskell GHCi (<a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>) session demonstrates is that it's possible to compose functions with Haskell's monadic bind operator <code>&gt;&gt;=</code> operator exactly because all functions are (Reader) monads. </p> <h3 id="fb5e50be9b66464ca61cd4a45eb7e756"> Conclusion <a href="#fb5e50be9b66464ca61cd4a45eb7e756" title="permalink">#</a> </h3> <p> In Haskell, it can occasionally be useful that a function can be used when a <code>Monad</code> is required. Some Haskell libraries are defined in very general terms. Their APIs may enable you to call functions with any monadic input value. You can, say, pass a <a href="/2022/04/25/the-maybe-monad">Maybe</a>, a <a href="/2022/04/19/the-list-monad">List</a>, an <a href="/2022/05/09/an-either-monad">Either</a>, a State, but you can also pass a function. </p> <p> C# and most other languages (F# included) doesn't come with that level of abstraction, so the fact that a function forms a monad is less useful there. In fact, I can't recall having made explicit use of this knowledge in C#, but one never knows if that day arrives. </p> <p> In a similar vein, knowing that <a href="/2018/04/16/endomorphic-composite-as-a-monoid">endomorphisms form monoids</a> (and thereby also <a href="/2017/11/27/semigroups">semigroups</a>) enabled me to <a href="/2020/12/14/validation-a-solved-problem">quickly identify the correct design for a validation problem</a>. </p> <p> Who knows? One day the knowledge that functions are monads may come in handy. </p> <p> <strong>Next:</strong> <a href="/2023/01/09/the-io-monad">The IO monad</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative assertions https://blog.ploeh.dk/2022/11/07/applicative-assertions 2022-11-07T06:56:00+00:00 Mark Seemann <div id="post"> <p> <em>An exploration.</em> </p> <p> In a recent Twitter exchange, <a href="https://lucasdicioccio.github.io/">Lucas DiCioccio</a> made an interesting observation: </p> <blockquote> <p> "Imho the properties you want of an assertion-framework are really close (the same as?) applicative-validation: one assertion failure with multiple bullet points composed mainly from combinators." </p> <footer><cite><a href="https://twitter.com/lucasdicioccio/status/1572264819109003265">Lucas DiCioccio</a></cite></footer> </blockquote> <p> In another branch off my initial tweet <a href="https://www.joshka.net/">Josh McKinney</a> pointed out the short-circuiting nature of standard assertions: </p> <blockquote> <p> "short circuiting often causes weaker error messages in failing tests than running compound assertions. E.g. </p> <p> <pre>TransferTest { a.transfer(b,50); a.shouldEqual(50); b.shouldEqual(150); // never reached? }</pre> </p> <footer><cite><a href="https://twitter.com/joshuamck/status/1572232484884217864">Josh McK</a></cite></footer> </blockquote> <p> Most standard assertion libraries work by throwing exceptions when an assertion fails. Once you throw an exception, remaining code doesn't execute. This means that you only get the first assertion message. Further assertions are not evaluated. </p> <p> Josh McKinney <a href="https://twitter.com/joshuamck/status/1572528796125003777">later gave more details about a particular scenario</a>. Although in the general case I don't consider the short-circuiting nature of assertions to be a problem, I grant that there are cases where proper assertion composition would be useful. </p> <p> Lucas DiCioccio's suggestion seems worthy of investigation. </p> <h3 id="54fc71d7459d4251a79dc16f58bd79b3"> Ongoing exploration <a href="#54fc71d7459d4251a79dc16f58bd79b3" title="permalink">#</a> </h3> <p> <a href="https://twitter.com/ploeh/status/1572282314402721805">I asked</a> Lucas DiCioccio whether he'd done any work with his idea, and the day after <a href="https://twitter.com/lucasdicioccio/status/1572639255582867456">he replied</a> with a <a href="https://www.haskell.org">Haskell</a> proof of concept. </p> <p> I found the idea so interesting that I also wanted to carry out a few proofs of concept myself, perhaps within a more realistic setting. </p> <p> As I'm writing this, I've reached some preliminary conclusions, but I'm also aware that they may not hold in more general cases. I'm posting what I have so far, but you should expect this exploration to evolve over time. If I find out more, I'll update this post with more articles. </p> <ul> <li><a href="/2022/11/28/an-initial-proof-of-concept-of-applicative-assertions-in-c">An initial proof of concept of applicative assertions in C#</a></li> <li><a href="/2022/12/19/error-accumulating-composable-assertions-in-c">Error-accumulating composable assertions in C#</a></li> <li><a href="/2023/01/30/built-in-alternatives-to-applicative-assertions">Built-in alternatives to applicative assertions</a></li> </ul> <p> A preliminary summary is in order. Based on the first two articles, applicative assertions look like overkill. I think, however, that it's because of the degenerate nature of the example. Some assertions are essentially one-stop verifications: Evaluate a predicate, and throw an exception if the result is <em>false</em>. These assertions return <a href="/2018/01/15/unit-isomorphisms">unit or void</a>. Examples from <a href="https://xunit.net/">xUnit</a> include <code>Assert.Equal</code>, <code>Assert.True</code>, <code>Assert.False</code>, <code>Assert.All</code>, and <code>Assert.DoesNotContain</code>. </p> <p> These are the kinds of assertions that the initial two articles explore. </p> <p> There are other kinds of assertions that return a value in case of success. xUnit.net examples include <code>Assert.Throws</code>, <code>Assert.Single</code>, <code>Assert.IsAssignableFrom</code>, and some overloads of <code>Assert.Contains</code>. <code>Assert.Single</code>, for example, verifies that a collection contains only a single element. While it throws an exception if the collection is either empty or has more than one element, in the success case it returns the single value. This can be useful if you want to add more assertions based on that value. </p> <p> I haven't experimented with this yet, but as far as can tell, you'll run into the following problem: If you make such an assertion return an <a href="/2018/10/01/applicative-functors">applicative functor</a>, you'll need some way to handle the success case. Combining it with another assertion-producing function, such as <code>a -> Asserted e b</code> (pseudocode) is possible with <a href="/2018/03/22/functors">functor</a> mapping, but will leave you with a nested functor. </p> <p> You'll probably want to flatten the nested functor, which is exactly what <a href="/2022/03/28/monads">monads</a> do. Monads, on the other hand, short circuit, so you don't want to make your applicative assertion type a monad. Instead, you'll need to use an isomorphic monad container (<a href="/2022/05/09/an-either-monad">Either</a> should do) to move in and out of. Doable, but is the complexity warranted? </p> <p> I realise that the above musings are abstract, and that I really should show rather than tell. I'll add some more content here if I ever collect something worthy of an article. if you ask me now, though, I consider that a bit of a toss-up. </p> <p> The first two examples also suffer from being written in C#, which doesn't have good syntactic support for applicative functors. Perhaps I'll add some articles that use <a href="https://fsharp.org/">F#</a> or Haskell. </p> <h3 id="676b3bf45f0841bc9a51d3510d917a6a"> Conclusion <a href="#676b3bf45f0841bc9a51d3510d917a6a" title="permalink">#</a> </h3> <p> There's the occasional need for composable assertions. You can achieve that with an applicative functor, but the question is whether it's warranted. Could you make something simpler based on the <a href="/2022/04/19/the-list-monad">list monad</a>? </p> <p> As I'm writing this, I don't consider that question settled. Even so, you may want to read on. </p> <p> <strong>Next:</strong> <a href="/2022/11/28/an-initial-proof-of-concept-of-applicative-assertions-in-c">An initial proof of concept of applicative assertions in C#</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e3269279056146f985c8405f6d3ad286"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#e3269279056146f985c8405f6d3ad286">#</a></div> <div class="comment-content"> <blockquote> Monads, on the other hand, short circuit, so you don't want to make your applicative assertion type a monad. </blockquote> <p> I want my assertion type to be both applicative and monadic. So does Paul Loath, the creator of Language Ext, which is most clearly seen via <a href="https://github.com/louthy/language-ext/blob/main/LanguageExt.Tests/ValidationTests.cs#L267-L277">this Validation test code</a>. So does Alexis King (as you pointed out to me) in her Haskell Validation package, which violiates Hakell's monad type class, and which she defends <a href="https://hackage.haskell.org/package/monad-validate-1.2.0.1/docs/Control-Monad-Validate.html#:~:text=ValidateT%20and%20the%20Monad%20laws">here</a>. </p> <p> When I want (or typically need) short-circuiting behavior, then I use the type's monadic API. When I want "error-collecting behavior", then I use the type's applicative API. </p> <blockquote> The first two examples also suffer from being written in C#, which doesn't have good syntactic support for applicative functors. </blockquote> <p> The best syntactic support for applicative functors in C# that I have seen is in Langauge Ext. <a href="https://github.com/louthy/language-ext/blob/15cd875ea40925e2ca9cd702c84f9142918dbb77/LanguageExt.Tests/ValidationTests.cs#L272-L277">A comment explains</a> in that same Validation test how it works, and the line after the comment shows it in action. </p> </div> <div class="comment-date">2023-01-16 21:13 UTC</div> </div> <div class="comment" id="a1516515c1b84cb2b8473f2c0321562e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a1516515c1b84cb2b8473f2c0321562e">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Whether or not you want to enable monadic short-circuiting for assertions or validations depends, I think, on 'developer ergonomics'. It's a trade-off mainly between <em>ease</em> and <em>simplicity</em> as <a href="https://www.infoq.com/presentations/Simple-Made-Easy/">outlined by Rich Hickey</a>. Enabling a monadic API for something that isn't naturally monadic does indeed provide ease of use, in that the compositional capabilities of a monad are readily 'at hand'. </p> <p> If you don't have that capability you'll have to map back and forth between, say, <code>Validation</code> and <code>Either</code> (if using the <a href="https://hackage.haskell.org/package/validation">validation</a> package). This is tedious, but <a href="https://peps.python.org/pep-0020/">explicit</a>. </p> <p> Making validation or assertions monadic makes it easier to compose nested values, but also (in my experience) makes it easier to make mistakes, in the sense that you (or a colleague) may <em>think</em> that the behaviour is error-collecting, whereas in reality it's short-circuiting. </p> <p> In the end, the trade-off may reduce to how much you trust yourself (and co-workers) to steer clear of mistakes, and how important it is to avoid errors. In this case, how important is it to collect the errors, rather than short-circuiting? </p> <p> You can choose one alternative or the other by weighing such concerns. </p> </div> <div class="comment-date">2023-01-19 8:30 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A regular grid emerges https://blog.ploeh.dk/2022/10/31/a-regular-grid-emerges 2022-10-31T06:44:00+00:00 Mark Seemann <div id="post"> <p> <em>The code behind a lecture animation.</em> </p> <p> If you've seen my presentation <a href="https://youtu.be/FPEEiX5unWI">Fractal Architecture</a>, you may have wondered how I made the animation where a regular(ish) hexagonal grid emerges from adding more and more blobs to an area. </p> <p> <img src="/content/binary/a-regular-grid-emerges.png" alt="A grid-like structure starting to emerge from tightly packing blobs."> </p> <p> Like <a href="/2021/04/05/mazes-on-voronoi-tesselations">a few</a> <a href="/2021/07/05/fractal-hex-flowers">previous</a> blog posts, today's article appears on <a href="https://observablehq.com">Observable</a>, which is where the animation and the code that creates it lives. <a href="https://observablehq.com/@ploeh/a-regular-grid-emerges">Go there to read it</a>. </p> <p> If you have time, watch the animation evolve. Personally I find it quite mesmerising. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Encapsulation in Functional Programming https://blog.ploeh.dk/2022/10/24/encapsulation-in-functional-programming 2022-10-24T05:54:00+00:00 Mark Seemann <div id="post"> <p> <em>Encapsulation is only relevant for object-oriented programming, right?</em> </p> <p> The concept of <em>encapsulation</em> is closely related to object-oriented programming (OOP), and you rarely hear the word in discussions about (statically-typed) functional programming (FP). I will argue, however, that the notion is relevant in FP as well. Typically, it just appears with a different catchphrase. </p> <h3 id="f0f64bfaaa6f4d22b990fae4775a8b89"> Contracts <a href="#f0f64bfaaa6f4d22b990fae4775a8b89" title="permalink">#</a> </h3> <p> I base my understanding of encapsulation on <a href="/ref/oosc">Object-Oriented Software Construction</a>. I've tried to distil it in my Pluralsight course <a href="/encapsulation-and-solid">Encapsulation and SOLID</a>. </p> <p> In short, encapsulation denotes the distinction between an object's contract and its implementation. An object should fulfil its contract in such a way that client code doesn't need to know about its implementation. </p> <p> Contracts, according to <a href="https://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Meyer</a>, describe three properties of objects: </p> <ul> <li>Preconditions: What client code must fulfil in order to successfully interact with the object.</li> <li>Invariants: Statements about the object that are always true.</li> <li>Postconditions: Statements that are guaranteed to be true after a successful interaction between client code and object.</li> </ul> <p> You can replace <em>object</em> with <em>value</em> and I'd argue that the same concerns are relevant in FP. </p> <p> In OOP <em>invariants</em> often point to the properties of an object that are guaranteed to remain even in the face of state mutation. As you change the state of an object, the object should guarantee that its state remains valid. These are the properties (i.e. <em>qualities</em>, <em>traits</em>, <em>attributes</em>) that don't vary - i.e. are <em>invariant</em>. </p> <p> An example would be helpful around here. </p> <h3 id="72a37691c0c14d4a8673d52f25e7c3e2"> Table mutation <a href="#72a37691c0c14d4a8673d52f25e7c3e2" title="permalink">#</a> </h3> <p> Consider an object that models a table in a restaurant. You may, for example, be working on <a href="/2020/01/27/the-maitre-d-kata">the Maître d' kata</a>. In short, you may decide to model a table as being one of two kinds: Standard tables and communal tables. You can reserve seats at communal tables, but you still share the table with other people. </p> <p> You may decide to model the problem in such a way that when you reserve the table, you change the state of the object. You may decide to describe the contract of <code>Table</code> objects like this: </p> <ul> <li>Preconditions <ul> <li>To create a <code>Table</code> object, you must supply a type (standard or communal).</li> <li>To create a <code>Table</code> object, you must supply the size of the table, which is a measure of its capacity; i.e. how many people can sit at it.</li> <li>The capacity must be a natural number. <em>One</em> (1) is the smallest valid capacity.</li> <li>When reserving a table, you must supply a valid reservation.</li> <li>When reserving a table, the reservation quantity must be less than or equal to the table's remaining capacity.</li> </ul> </li> <li>Invariants <ul> <li>The table capacity doesn't change.</li> <li>The table type doesn't change.</li> <li>The number of remaining seats is never negative.</li> <li>The number of remaining seats is never greater than the table's capacity.</li> </ul> </li> <li>Postconditions <ul> <li>After reserving a table, the number of remaining seats can't be greater than the previous number of remaining seats minus the reservation quantity.</li> </ul> </li> </ul> <p> This list may be incomplete, and if you add more operations, you may have to elaborate on what that means to the contract. </p> <p> In C# you may implement a <code>Table</code> class like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Table</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;List&lt;Reservation&gt;&nbsp;reservations; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Table</span>(<span style="color:blue;">int</span>&nbsp;capacity,&nbsp;TableType&nbsp;type) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(capacity&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(capacity), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Capacity&nbsp;must&nbsp;be&nbsp;greater&nbsp;than&nbsp;zero,&nbsp;but&nbsp;was:&nbsp;</span>{capacity}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservations&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;List&lt;Reservation&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type&nbsp;=&nbsp;type; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RemaingSeats&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TableType&nbsp;Type&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;RemaingSeats&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Reserve(Reservation&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(RemaingSeats&nbsp;&lt;&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;InvalidOperationException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;The&nbsp;table&nbsp;has&nbsp;no&nbsp;remaining&nbsp;seats.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Type&nbsp;==&nbsp;TableType.Communal) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RemaingSeats&nbsp;-=&nbsp;reservation.Quantity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RemaingSeats&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservations.Add(reservation); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class has good encapsulation because it makes sure to fulfil the contract. You can't put it in an invalid state. </p> <h3 id="b37e173371f249d99f80d12d64b2bee2"> Immutable Table <a href="#b37e173371f249d99f80d12d64b2bee2" title="permalink">#</a> </h3> <p> Notice that two of the invariants for the above <code>Table</code> class is that the table can't change type or capacity. While OOP often revolves around state mutation, it seems reasonable that some data is immutable. A table doesn't all of a sudden change size. </p> <p> In FP data is immutable. Data doesn't change. Thus, data has that invariant property. </p> <p> If you consider the above contract, it still applies to FP. The specifics change, though. You'll no longer be dealing with <code>Table</code> objects, but rather <code>Table</code> data, and to make reservations, you call a function that returns a new <code>Table</code> value. </p> <p> In <a href="https://fsharp.org/">F#</a> you could model a <code>Table</code> like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Table&nbsp;=&nbsp;<span style="color:blue;">private</span>&nbsp;Standard&nbsp;<span style="color:blue;">of</span>&nbsp;int&nbsp;*&nbsp;Reservation&nbsp;list&nbsp;|&nbsp;Communal&nbsp;<span style="color:blue;">of</span>&nbsp;int&nbsp;*&nbsp;Reservation&nbsp;list <span style="color:blue;">module</span>&nbsp;Table&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;standard&nbsp;capacity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;0&nbsp;&lt;&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;Some&nbsp;(Standard&nbsp;(capacity,&nbsp;[])) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;communal&nbsp;capacity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;0&nbsp;&lt;&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;Some&nbsp;(Communal&nbsp;(capacity,&nbsp;[])) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;remainingSeats&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Standard&nbsp;(capacity,&nbsp;[])&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Standard&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Communal&nbsp;(capacity,&nbsp;rs)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;capacity&nbsp;-&nbsp;List.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reserve&nbsp;r&nbsp;t&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;t&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Standard&nbsp;(capacity,&nbsp;[])&nbsp;<span style="color:blue;">when</span>&nbsp;r.Quantity&nbsp;&lt;=&nbsp;remainingSeats&nbsp;t&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Some&nbsp;(Standard&nbsp;(capacity,&nbsp;[r])) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Communal&nbsp;(capacity,&nbsp;rs)&nbsp;<span style="color:blue;">when</span>&nbsp;r.Quantity&nbsp;&lt;=&nbsp;remainingSeats&nbsp;t&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Some&nbsp;(Communal&nbsp;(capacity,&nbsp;r&nbsp;::&nbsp;rs)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None</pre> </p> <p> While you'll often hear fsharpers say that one should <a href="https://blog.janestreet.com/effective-ml-video/">make illegal states unrepresentable</a>, in practice you often have to rely on <a href="https://www.hillelwayne.com/post/constructive/">predicative</a> data to enforce contracts. I've done this here by making the <code>Table</code> cases <code>private</code>. Code outside the module can't directly create <code>Table</code> data. Instead, it'll have to use one of two functions: <code>Table.standard</code> or <code>Table.communal</code>. These are functions that return <code>Table option</code> values. </p> <p> That's the idiomatic way to model predicative data in statically typed FP. In <a href="https://www.haskell.org/">Haskell</a> such functions are called <a href="https://wiki.haskell.org/Smart_constructors">smart constructors</a>. </p> <p> Statically typed FP typically use <a href="/2022/04/25/the-maybe-monad">Maybe</a> (<code>Option</code>) or <a href="/2022/05/09/an-either-monad">Either</a> (<code>Result</code>) values to communicate failure, rather than throwing exceptions, but apart from that a smart constructor is just an object constructor. </p> <p> The above F# <code>Table</code> API implements the same contract as the OOP version. </p> <p> If you want to see a more elaborate example of modelling table and reservations in F#, see <a href="/2020/04/27/an-f-implementation-of-the-maitre-d-kata">An F# implementation of the Maître d' kata</a>. </p> <h3 id="78027bc1d1414c2fa3604a68c9df6418"> Functional contracts in OOP languages <a href="#78027bc1d1414c2fa3604a68c9df6418" title="permalink">#</a> </h3> <p> You can adopt many FP concepts in OOP languages. My book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> contains sample code in C# that implements an online restaurant reservation system. It includes a <code>Table</code> class that, at first glance, looks like the above C# class. </p> <p> While it has the same contract, the book's <code>Table</code> class is implemented with the FP design principles in mind. Thus, it's an immutable class with this API: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Table</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table&nbsp;Standard(<span style="color:blue;">int</span>&nbsp;seats) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Table&nbsp;Communal(<span style="color:blue;">int</span>&nbsp;seats) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;RemainingSeats&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Table&nbsp;Reserve(Reservation&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;Accept&lt;<span style="color:#2b91af;">T</span>&gt;(ITableVisitor&lt;T&gt;&nbsp;visitor) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>?&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() }</pre> </p> <p> Notice that the <code>Reserve</code> method returns a <code>Table</code> object. That's the table with the reservation associated. The original <code>Table</code> instance remains unchanged. </p> <p> The entire book is written in the <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">Functional Core, Imperative Shell</a> architecture, so all domain models are immutable objects with <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> as methods. </p> <p> The objects still have contracts. They have proper encapsulation. </p> <h3 id="ca2409555a5b4efe9b98e1c65e77256d"> Conclusion <a href="#ca2409555a5b4efe9b98e1c65e77256d" title="permalink">#</a> </h3> <p> Functional programmers may not use the term <em>encapsulation</em> much, but that doesn't mean that they don't share that kind of concern. They often throw around the phrase <em>make illegal states unrepresentable</em> or talk about smart constructors or <a href="https://en.wikipedia.org/wiki/Partial_function">partial versus total functions</a>. It's clear that they care about data modelling that prevents mistakes. </p> <p> The object-oriented notion of <em>encapsulation</em> is ultimately about separating the affordances of an API from its implementation details. An object's contract is an abstract description of the properties (i.e. <em>qualities</em>, <em>traits</em>, or <em>attributes</em>) of the object. </p> <p> Functional programmers care so much about the properties of data and functions that <em>property-based testing</em> is often the preferred way to perform automated testing. </p> <p> Perhaps you can find a functional programmer who might be slightly offended if you suggest that he or she should consider encapsulation. If so, suggest instead that he or she considers the properties of functions and data. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="bb616acbc1ac41cb8f937fe7175ce061"> <div class="comment-author"><a href="http://www.raboof.com/">Atif Aziz</a> <a href="#bb616acbc1ac41cb8f937fe7175ce061">#</a></div> <div class="comment-content"> <p> I wonder what's the goal of illustrating OOP-ish examples exclusively in C# and FP-ish ones in F# when you could stick to just one language for the reader. It might not always be as effective depending on the topic, but for encapsulation and the examples shown in this article, a C# version would read just as effective as an F# one. I mean when you get round to making your points in the <strong>Immutable Table</strong> section of your article, you could demonstrate the ideas with a C# version that's nearly identical to and reads as succinct as the F# version: </p> <pre><span style="color:gray;">#nullable</span>&nbsp;<span style="color:gray;">enable</span> <span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">record</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:darkblue;">Reservation</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">Quantity</span>); <span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">record</span>&nbsp;<span style="color:darkblue;">Table</span>; <span style="color:blue;">record</span>&nbsp;<span style="color:darkblue;">StandardTable</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">Capacity</span>,&nbsp;<span style="color:darkblue;">Reservation</span>?&nbsp;<span style="color:#1f377f;">Reservation</span>):&nbsp;<span style="color:darkblue;">Table</span>; <span style="color:blue;">record</span>&nbsp;<span style="color:darkblue;">CommunalTable</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">Capacity</span>,&nbsp;<span style="color:darkblue;">ImmutableArray</span>&lt;<span style="color:darkblue;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">Reservations</span>):&nbsp;<span style="color:darkblue;">Table</span>; <span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:darkblue;">TableModule</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:darkblue;">StandardTable</span>?&nbsp;<span style="color:darkcyan;">Standard</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">capacity</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&lt;&nbsp;capacity&nbsp;?&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:darkblue;">StandardTable</span>(capacity,&nbsp;<span style="color:blue;">null</span>)&nbsp;:&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:darkblue;">CommunalTable</span>?&nbsp;<span style="color:darkcyan;">Communal</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">capacity</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&lt;&nbsp;capacity&nbsp;?&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:darkblue;">CommunalTable</span>(capacity,&nbsp;<span style="color:darkblue;">ImmutableArray</span>&lt;<span style="color:darkblue;">Reservation</span>&gt;.Empty)&nbsp;:&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:darkcyan;">RemainingSeats</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:darkblue;">Table</span>&nbsp;<span style="color:#1f377f;">table</span>)&nbsp;=&gt;&nbsp;table&nbsp;<span style="color:#8f08c4;">switch</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:darkblue;">StandardTable</span>&nbsp;{&nbsp;<span style="color:purple;">Reservation</span>:&nbsp;<span style="color:blue;">null</span>&nbsp;}&nbsp;t&nbsp;=&gt;&nbsp;t.<span style="color:purple;">Capacity</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:darkblue;">StandardTable</span>&nbsp;=&gt;&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:darkblue;">CommunalTable</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.<span style="color:purple;">Capacity</span>&nbsp;-&nbsp;t.<span style="color:purple;">Reservations</span>.Sum(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:darkblue;">Table</span>?&nbsp;<span style="color:darkcyan;">Reserve</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:darkblue;">Table</span>&nbsp;<span style="color:#1f377f;">table</span>,&nbsp;<span style="color:darkblue;">Reservation</span>&nbsp;<span style="color:#1f377f;">r</span>)&nbsp;=&gt;&nbsp;table&nbsp;<span style="color:#8f08c4;">switch</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:darkblue;">StandardTable</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;<span style="color:#8f08c4;">when</span>&nbsp;r.<span style="color:purple;">Quantity</span>&nbsp;&lt;=&nbsp;t.<span style="color:darkcyan;">RemainingSeats</span>()&nbsp;=&gt;&nbsp;t&nbsp;<span style="color:blue;">with</span>&nbsp;{&nbsp;<span style="color:purple;">Reservation</span>&nbsp;=&nbsp;r&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:darkblue;">CommunalTable</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;<span style="color:#8f08c4;">when</span>&nbsp;r.<span style="color:purple;">Quantity</span>&nbsp;&lt;=&nbsp;t.<span style="color:darkcyan;">RemainingSeats</span>()&nbsp;=&gt;&nbsp;t&nbsp;<span style="color:blue;">with</span>&nbsp;{&nbsp;<span style="color:purple;">Reservations</span>&nbsp;=&nbsp;t.<span style="color:purple;">Reservations</span>.Add(r)&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">_</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">null</span>, &nbsp;&nbsp;&nbsp;&nbsp;}; } </pre> <p> This way, I can just point someone to your article for enlightenment, &#x1F609; but not leave them feeling frustrated that they need F# to (practice and) model around data instead of state mutating objects. It might still be worthwhile to show an F# version to draw the similarities and also call out some differences; like <code>Table</code> being a true discriminated union in F#, and while it appears to be emulated in C#, they desugar to the same thing in terms of CLR types and hierarchies. </p> <p> By the way, in the C# example above, I modeled the standard table variant differently because if it can hold only one reservation at a time then the model should reflect that. </p> </div> <div class="comment-date">2022-10-27 16:09 UTC</div> </div> <div class="comment" id="7069ea2b33a64a1caf7247c3a1543bac"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7069ea2b33a64a1caf7247c3a1543bac">#</a></div> <div class="comment-content"> <p> Atif, thank you for supplying and example of an immutable C# implementation. </p> <p> I already have an example of an immutable, functional C# implementation in <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>, so I wanted to supply something else here. I also tend to find it interesting to compare how to model similar ideas in different languages, and it felt natural to supply an F# example to show how a 'natural' FP implementation might look. </p> <p> Your point is valid, though, so I'm not insisting that this was the right decision. </p> </div> <div class="comment-date">2022-10-28 8:50 UTC</div> </div> <div class="comment" id="a7b4d4d0dcc8432fb3b49cb7189d8123"> <div class="comment-author"><a href="https://github.com/sebastianfrelle">Sebastian Frelle Koch</a> <a href="#a7b4d4d0dcc8432fb3b49cb7189d8123">#</a></div> <div class="comment-content"> <p>I took your idea, Atif, and wrote something that I think is more congruent with the example <a href="#78027bc1d1414c2fa3604a68c9df6418">here</a>. In short, I’m</p> <ul> <li>using polymorphism to avoid having to switch over the Table type</li> <li>hiding subtypes of Table to simplify the interface.</li> </ul> <p>Here's the code:</p> <div class="language-cs highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="err">#</span><span class="n">nullable</span> <span class="n">enable</span> <span class="k">using</span> <span class="nn">System.Collections.Immutable</span><span class="p">;</span> <span class="k">readonly</span> <span class="n">record</span> <span class="k">struct</span> <span class="nc">Reservation</span><span class="p">(</span><span class="kt">int</span> <span class="n">Quantity</span><span class="p">);</span> <span class="k">abstract</span> <span class="n">record</span> <span class="n">Table</span> <span class="p">{</span> <span class="k">public</span> <span class="k">abstract</span> <span class="n">Table</span><span class="p">?</span> <span class="nf">Reserve</span><span class="p">(</span><span class="n">Reservation</span> <span class="n">r</span><span class="p">);</span> <span class="k">public</span> <span class="k">abstract</span> <span class="kt">int</span> <span class="nf">RemainingSeats</span><span class="p">();</span> <span class="k">public</span> <span class="k">static</span> <span class="n">Table</span><span class="p">?</span> <span class="nf">Standard</span><span class="p">(</span><span class="kt">int</span> <span class="n">capacity</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">capacity</span> <span class="p">&gt;</span> <span class="m">0</span> <span class="p">?</span> <span class="k">new</span> <span class="nf">StandardTable</span><span class="p">(</span><span class="n">capacity</span><span class="p">,</span> <span class="k">null</span><span class="p">)</span> <span class="p">:</span> <span class="k">null</span><span class="p">;</span> <span class="k">public</span> <span class="k">static</span> <span class="n">Table</span><span class="p">?</span> <span class="nf">Communal</span><span class="p">(</span><span class="kt">int</span> <span class="n">capacity</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">capacity</span> <span class="p">&gt;</span> <span class="m">0</span> <span class="p">?</span> <span class="k">new</span> <span class="nf">CommunalTable</span><span class="p">(</span> <span class="n">capacity</span><span class="p">,</span> <span class="n">ImmutableArray</span><span class="p">&lt;</span><span class="n">Reservation</span><span class="p">&gt;.</span><span class="n">Empty</span><span class="p">)</span> <span class="p">:</span> <span class="k">null</span><span class="p">;</span> <span class="k">private</span> <span class="n">record</span> <span class="nf">StandardTable</span><span class="p">(</span><span class="kt">int</span> <span class="n">Capacity</span><span class="p">,</span> <span class="n">Reservation</span><span class="p">?</span> <span class="n">Reservation</span><span class="p">)</span> <span class="p">:</span> <span class="n">Table</span> <span class="p">{</span> <span class="k">public</span> <span class="k">override</span> <span class="n">Table</span><span class="p">?</span> <span class="nf">Reserve</span><span class="p">(</span><span class="n">Reservation</span> <span class="n">r</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nf">RemainingSeats</span><span class="p">()</span> <span class="k">switch</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">seats</span> <span class="n">when</span> <span class="n">seats</span> <span class="p">&gt;=</span> <span class="n">r</span><span class="p">.</span><span class="n">Quantity</span> <span class="p">=&gt;</span> <span class="k">this</span> <span class="n">with</span> <span class="p">{</span> <span class="n">Reservation</span> <span class="p">=</span> <span class="n">r</span> <span class="p">},</span> <span class="n">_</span> <span class="p">=&gt;</span> <span class="k">null</span><span class="p">,</span> <span class="p">};</span> <span class="k">public</span> <span class="k">override</span> <span class="kt">int</span> <span class="nf">RemainingSeats</span><span class="p">()</span> <span class="p">=&gt;</span> <span class="n">Reservation</span> <span class="k">switch</span> <span class="p">{</span> <span class="k">null</span> <span class="p">=&gt;</span> <span class="n">Capacity</span><span class="p">,</span> <span class="n">_</span> <span class="p">=&gt;</span> <span class="m">0</span><span class="p">,</span> <span class="p">};</span> <span class="p">}</span> <span class="k">private</span> <span class="n">record</span> <span class="nf">CommunalTable</span><span class="p">(</span> <span class="kt">int</span> <span class="n">Capacity</span><span class="p">,</span> <span class="n">ImmutableArray</span><span class="p">&lt;</span><span class="n">Reservation</span><span class="p">&gt;</span> <span class="n">Reservations</span><span class="p">)</span> <span class="p">:</span> <span class="n">Table</span> <span class="p">{</span> <span class="k">public</span> <span class="k">override</span> <span class="n">Table</span><span class="p">?</span> <span class="nf">Reserve</span><span class="p">(</span><span class="n">Reservation</span> <span class="n">r</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nf">RemainingSeats</span><span class="p">()</span> <span class="k">switch</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">seats</span> <span class="n">when</span> <span class="n">seats</span> <span class="p">&gt;=</span> <span class="n">r</span><span class="p">.</span><span class="n">Quantity</span> <span class="p">=&gt;</span> <span class="k">this</span> <span class="n">with</span> <span class="p">{</span> <span class="n">Reservations</span> <span class="p">=</span> <span class="n">Reservations</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="p">},</span> <span class="n">_</span> <span class="p">=&gt;</span> <span class="k">null</span><span class="p">,</span> <span class="p">};</span> <span class="k">public</span> <span class="k">override</span> <span class="kt">int</span> <span class="nf">RemainingSeats</span><span class="p">()</span> <span class="p">=&gt;</span> <span class="n">Capacity</span> <span class="p">-</span> <span class="n">Reservations</span><span class="p">.</span><span class="nf">Sum</span><span class="p">(</span><span class="n">r</span> <span class="p">=&gt;</span> <span class="n">r</span><span class="p">.</span><span class="n">Quantity</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>I’d love to hear your thoughts on this approach. I think that one of its weaknesses is that calls to <code class="language-plaintext highlighter-rouge">Table.Standard()</code> and <code class="language-plaintext highlighter-rouge">Table.Communal()</code> will yield two instances of <code class="language-plaintext highlighter-rouge">Table</code> that can never be equal. For instance, <code class="language-plaintext highlighter-rouge">Table.Standard(4) != Table.Communal(4)</code>, even though they’re both of type <code class="language-plaintext highlighter-rouge">Table?</code> and have the same number of seats. </p> <p> Calling <code class="language-plaintext highlighter-rouge">GetType()</code> on each of the instances reveals that their types are actually <code class="language-plaintext highlighter-rouge">Table+StandardTable</code> and <code class="language-plaintext highlighter-rouge">Table+CommunalTable</code> respectively; however, this isn't transparent to callers. Another solution might be to expose the <code class="language-plaintext highlighter-rouge">Table</code> subtypes and give them private constructors &ndash; I just like the simplicity of not exposing the individual types of tables the same way you’re doing <a href="#b37e173371f249d99f80d12d64b2bee2">here</a>, Mark.</p> </div> <div class="comment-date">2022-11-29 11:28 UTC</div> </div> <div class="comment" id="f925c18ec3a746c393cfae319200baac"> <div class="comment-author"><a href="https://github.com/alexmurari">Alexandre Murari Jr</a> <a href="#f925c18ec3a746c393cfae319200baac">#</a></div> <div class="comment-content"> <p> Mark, <p> How do you differentiate encapsulation from abstraction? </p> <p> Here's an excerpt from your book Dependency Injection: Principles, Practices, and Patterns. </p> <p> Section: 1.3 - What to inject and what not to inject Subsection: 1.3.1 - Stable Dependencies </p> <blockquote> "Other examples [of libraries that do not require to be injected] may include specialized libraries that encapsulate alogorithms relevant to your application". </blockquote> <p> In that section, you and Steven were giving examples of stable dependencies that do not require to be injected to keep modularity. You define a library that "encapsulates an algorithm" as an example. </p> <p> Now, to me, encapsulation is "protecting data integrity", plain and simple. A class is encapsulated as long as it's impossible or nearly impossible to bring it to an invalid or inconsistent state. </p> <p> Protection of invariants, implementation hiding, bundling data and operations together, pre- and postconditions, Postel's Law all come into play to achieve this goal. </p> <p> Thus, a class, to be "encapsulatable", has to have a state that can be initialized and/or modified by the client code. </p> <p> Now I ask: most of the time when we say that something is encapsulating another, don't we really mean abstracting? </p> <p> Why is it relevant to know that the hypothetical algorithm library protects it's invariants by using the term "encapsulate"? </p> <p> Abstraction, under the light of Robert C. Martin's definition of it, makes much more sense in that context: "a specialized library that abstracts algorithms relevant to your application". It amplifies the essential (by providing a clear API), but eliminates the irrelevant (by hiding the alogirthm's implementation details). </p> <p> Granted, there is some overlap between encapsulation and abstraction, specially when you bundle data and operations together (rich domain models), but they are not the same thing, you just use one to achieve another sometimes. </p> <p> Would it be correct to say that the .NET Framework encapsulates math algorithms in the System.Math class? Is there any state there to be preserved? They're all static methods and constants. On the other hand, they're surely eliminating some pretty irrelevant (from a consumer POV) trigonometric algorithms. </p> <p> Thanks. </p> </div> <div class="comment-date">2022-12-04 02:35 UTC</div> </div> <div class="comment" id="5af1535933bf4b28bb5c2fc14ce0a01a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5af1535933bf4b28bb5c2fc14ce0a01a">#</a></div> <div class="comment-content"> <p> Alexandre, thank you for writing. How do I distinguish between abstraction and encapsulation? </p> <p> There's much overlap, to be sure. </p> <p> As I write, my view on encapsulation is influenced by Bertrand Meyer's notion of contract. Likewise, I do use Robert C. Martin's notion of amplifying the essentials while hiding the irrelevant details as a guiding light when discussing abstraction. </p> <p> While these concepts may seem synonymous, they're not quite the same. I can't say that I've spent too much time considering how these two words relate, but shooting from the hip I think that <em>abstraction</em> is a wider concept. </p> <p> You don't need to read much of Robert C. Martin before he'll tell you that the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> is an important part of abstraction: </p> <blockquote> <p> "Abstractions should not depend on details. Details should depend on abstractions." </p> <footer><cite>Robert C. Martin, <a href="/ref/appp">Agile Principles, Patterns, and Practices in C#</a></cite></footer> </blockquote> <p> It's possible to implement a code base where this isn't true, even if classes have good encapsulation. You could imagine a domain model that depends on database details like a particular ORM. I've seen plenty of those in my career, although I grant that most of them have had poor encapsulation as well. It is not, however, impossible to imagine such a system with good encapsulation, but suboptimal abstraction. </p> <p> Does it go the other way as well? Can we have good abstraction, but poor encapsulation? </p> <p> An example doesn't come immediately to mind, but as I wrote, it's not an ontology that I've given much thought. </p> </div> <div class="comment-date">2022-12-06 22:11 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Stubs and mocks break encapsulation https://blog.ploeh.dk/2022/10/17/stubs-and-mocks-break-encapsulation 2022-10-17T08:47:00+00:00 Mark Seemann <div id="post"> <p> <em>Favour Fakes over dynamic mocks.</em> </p> <p> For a while now, I've <a href="/2019/02/18/from-interaction-based-to-state-based-testing">favoured Fakes over Stubs and Mocks</a>. Using <a href="http://xunitpatterns.com/Fake%20Object.html">Fake Objects</a> over other <a href="https://martinfowler.com/bliki/TestDouble.html">Test Doubles</a> makes test suites more robust. I wrote the code base for my book <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a> entirely with Fakes and the occasional <a href="http://xunitpatterns.com/Test%20Spy.html">Test Spy</a>, and I rarely had to fix broken tests. No <a href="https://moq.github.io/moq4/">Moq</a>, <a href="https://fakeiteasy.github.io/">FakeItEasy</a>, <a href="https://nsubstitute.github.io/">NSubstitute</a>, nor <a href="https://hibernatingrhinos.com/oss/rhino-mocks">Rhino Mocks</a>. Just hand-written Test Doubles. </p> <p> It recently occurred to me that a way to explain the problem with <a href="http://xunitpatterns.com/Mock%20Object.html">Mocks</a> and <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a> is that they break encapsulation. </p> <p> You'll see some examples soon, but first it's important to be explicit about terminology. </p> <h3 id="bde25ebecc664e99b529755c3f0829fb"> Terminology <a href="#bde25ebecc664e99b529755c3f0829fb" title="permalink">#</a> </h3> <p> Words like <em>Mocks</em>, <em>Stubs</em>, as well as <em>encapsulation</em>, have different meanings to different people. They've fallen victim to <a href="https://martinfowler.com/bliki/SemanticDiffusion.html">semantic diffusion</a>, if ever they were well-defined to begin with. </p> <p> When I use the words <em>Test Double</em>, <em>Fake</em>, <em>Mock</em>, and <em>Stub</em>, I use them as they are defined in <a href="/ref/xunit-patterns">xUnit Test Patterns</a>. I usually try to avoid the terms <em>Mock</em> and <em>Stub</em> since people use them vaguely and inconsistently. The terms <em>Test Double</em> and <em>Fake</em> fare better. </p> <p> We do need, however, a name for those libraries that generate Test Doubles on the fly. In .NET, they are libraries like Moq, FakeItEasy, and so on, as listed above. Java has <a href="https://site.mockito.org/">Mockito</a>, <a href="https://easymock.org/">EasyMock</a>, <a href="https://jmockit.github.io/">JMockit</a>, and possibly more like that. </p> <p> What do we call such libraries? Most people call them <em>mock libraries</em> or <em>dynamic mock libraries</em>. Perhaps <em>dynamic Test Double library</em> would be more consistent with the <em>xUnit Test Patterns</em> vocabulary, but nobody calls them that. I'll call them <em>dynamic mock libraries</em> to at least emphasise the dynamic, on-the-fly object generation these libraries typically use. </p> <p> Finally, it's important to define <em>encapsulation</em>. This is another concept where people may use the same word and yet mean different things. </p> <p> I base my understanding of encapsulation on <a href="/ref/oosc">Object-Oriented Software Construction</a>. I've tried to distil it in my Pluralsight course <a href="/encapsulation-and-solid">Encapsulation and SOLID</a>. </p> <p> In short, encapsulation denotes the distinction between an object's contract and its implementation. An object should fulfil its contract in such a way that client code doesn't need to know about its implementation. </p> <p> Contracts, according to Meyer, describe three properties of objects: </p> <ul> <li>Preconditions: What client code must fulfil in order to successfully interact with the object.</li> <li>Invariants: Statements about the object that are always true.</li> <li>Postconditions: Statements that are guaranteed to be true after a successful interaction between client code and object.</li> </ul> <p> As I'll demonstrate in this article, objects generated by dynamic mock libraries often break their contracts. </p> <h3 id="2d05b5926474448589c3953f6796c2a9"> Create-and-read round-trip <a href="#2d05b5926474448589c3953f6796c2a9" title="permalink">#</a> </h3> <p> Consider the <code>IReservationsRepository</code> interface from <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;Create(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Reservation&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;DateTime&nbsp;min,&nbsp;DateTime&nbsp;max); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;Reservation?&gt;&nbsp;ReadReservation(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Guid&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;Update(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Reservation&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;Delete(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Guid&nbsp;id); }</pre> </p> <p> I already discussed some of the contract properties of this interface in <a href="/2021/12/06/the-liskov-substitution-principle-as-a-profunctor">an earlier article</a>. Here, I want to highlight a certain interaction. </p> <p> What is the contract of the <code>Create</code> method? </p> <p> There are a few preconditions: </p> <ul> <li>The client must have a properly initialised <code>IReservationsRepository</code> object.</li> <li>The client must have a valid <code>restaurantId</code>.</li> <li>The client must have a valid <code>reservation</code>.</li> </ul> <p> A client that fulfils these preconditions can successfully call and await the <code>Create</code> method. What are the invariants and postconditions? </p> <p> I'll skip the invariants because they aren't relevant to the line of reasoning that I'm pursuing. One postcondition, however, is that the <code>reservation</code> passed to <code>Create</code> must now be 'in' the repository. </p> <p> How does that manifest as part of the object's contract? </p> <p> This implies that a client should be able to retrieve the <code>reservation</code>, either with <code>ReadReservation</code> or <code>ReadReservations</code>. This suggests a kind of property that Scott Wlaschin calls <a href="https://fsharpforfunandprofit.com/posts/property-based-testing-2/">There and back again</a>. </p> <p> Picking <code>ReadReservation</code> for the verification step we now have a property: If client code successfully calls and awaits <code>Create</code> it should be able to use <code>ReadReservation</code> to retrieve the reservation it just saved. That's implied by the <code>IReservationsRepository</code> contract. </p> <h3 id="f787d89599a04573a2c9b7afc28b2315"> SQL implementation <a href="#f787d89599a04573a2c9b7afc28b2315" title="permalink">#</a> </h3> <p> The 'real' implementation of <code>IReservationsRepository</code> used in production is an implementation that stores reservations in SQL Server. This class should obey the contract. </p> <p> While it might be possible to write a true property-based test, running hundreds of randomly generated test cases against a real database is going to take time. Instead, I chose to only write a parametrised test: </p> <p> <pre>[Theory] [InlineData(Grandfather.Id,&nbsp;<span style="color:#a31515;">&quot;2022-06-29&nbsp;12:00&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;e@example.gov&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Enigma&quot;</span>,&nbsp;1)] [InlineData(Grandfather.Id,&nbsp;<span style="color:#a31515;">&quot;2022-07-27&nbsp;11:40&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;c@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Carlie&quot;</span>,&nbsp;2)] [InlineData(2,&nbsp;<span style="color:#a31515;">&quot;2021-09-03&nbsp;14:32&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bon@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Jovi&quot;</span>,&nbsp;4)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;CreateAndReadRoundTrip( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Guid.NewGuid(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime.Parse(at,&nbsp;CultureInfo.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connectionString&nbsp;=&nbsp;ConnectionStrings.Reservations; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlReservationsRepository(connectionString); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> The part that we care about is the three last lines: </p> <p> <pre><span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); Assert.Equal(expected,&nbsp;actual);</pre> </p> <p> First call <code>Create</code> and subsequently <code>ReadReservation</code>. The value created should equal the value retrieved, which is also the case. All tests pass. </p> <h3 id="dc7d9e2730364cf8895d73d2adcd37b0"> Fake <a href="#dc7d9e2730364cf8895d73d2adcd37b0" title="permalink">#</a> </h3> <p> The Fake implementation is effectively an in-memory database, so we expect it to also fulfil the same contract. We can test it with an almost identical test: </p> <p> <pre>[Theory] [InlineData(RestApi.Grandfather.Id,&nbsp;<span style="color:#a31515;">&quot;2022-06-29&nbsp;12:00&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;e@example.gov&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Enigma&quot;</span>,&nbsp;1)] [InlineData(RestApi.Grandfather.Id,&nbsp;<span style="color:#a31515;">&quot;2022-07-27&nbsp;11:40&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;c@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Carlie&quot;</span>,&nbsp;2)] [InlineData(2,&nbsp;<span style="color:#a31515;">&quot;2021-09-03&nbsp;14:32&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bon@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Jovi&quot;</span>,&nbsp;4)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;CreateAndReadRoundTrip( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Guid.NewGuid(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime.Parse(at,&nbsp;CultureInfo.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> The only difference is that the <code>sut</code> is a different class instance. These test cases also all pass. </p> <p> How is <code>FakeDatabase</code> implemented? That's not important, because it obeys the contract. <code>FakeDatabase</code> has good encapsulation, which makes it possible to use it without knowing anything about its internal implementation details. That, after all, is the point of encapsulation. </p> <h3 id="a13bc3a894914ce59b1c11ea9702b3f8"> Dynamic mock <a href="#a13bc3a894914ce59b1c11ea9702b3f8" title="permalink">#</a> </h3> <p> How does a dynamic mock fare if subjected to the same test? Let's try with Moq 4.18.2 (and I'm not choosing Moq to single it out - I chose Moq because it's the dynamic mock library I used to love the most): </p> <p> <pre>[Theory] [InlineData(RestApi.Grandfather.Id,&nbsp;<span style="color:#a31515;">&quot;2022-06-29&nbsp;12:00&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;e@example.gov&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Enigma&quot;</span>,&nbsp;1)] [InlineData(RestApi.Grandfather.Id,&nbsp;<span style="color:#a31515;">&quot;2022-07-27&nbsp;11:40&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;c@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Carlie&quot;</span>,&nbsp;2)] [InlineData(2,&nbsp;<span style="color:#a31515;">&quot;2021-09-03&nbsp;14:32&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bon@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Jovi&quot;</span>,&nbsp;4)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;CreateAndReadRoundTrip( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Guid.NewGuid(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime.Parse(at,&nbsp;CultureInfo.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IReservationsRepository&gt;().Object; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> If you've worked a little with dynamic mock libraries, you will not be surprised to learn that all three tests fail. Here's one of the failure messages: </p> <p> <pre>Ploeh.Samples.Restaurants.RestApi.Tests.MoqRepositoryTests.CreateAndReadRoundTrip(↩ &nbsp;&nbsp;&nbsp;&nbsp;restaurantId:&nbsp;1,&nbsp;at:&nbsp;&quot;2022-06-29&nbsp;12:00&quot;,&nbsp;email:&nbsp;&quot;e@example.gov&quot;,&nbsp;name:&nbsp;&quot;Enigma&quot;,&nbsp;quantity:&nbsp;1) &nbsp;Source:&nbsp;<span style="color:blue;">MoqRepositoryTests.cs</span>&nbsp;line&nbsp;17 &nbsp;Duration:&nbsp;1&nbsp;ms Message:&nbsp; &nbsp;&nbsp;Assert.Equal()&nbsp;Failure &nbsp;&nbsp;Expected:&nbsp;Reservation↩ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{↩ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;2022-06-29T12:00:00.0000000,↩ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;e@example.gov,↩ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;c9de4f95-3255-4e1f-a1d6-63591b58ff0c,↩ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;Enigma,↩ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;1↩ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;Actual:&nbsp;&nbsp;&nbsp;(null) Stack&nbsp;Trace:&nbsp; &nbsp;&nbsp;<span style="color:red;">MoqRepositoryTests.CreateAndReadRoundTrip(↩ &nbsp;&nbsp;&nbsp;&nbsp;Int32&nbsp;restaurantId,&nbsp;String&nbsp;at,&nbsp;String&nbsp;email,&nbsp;String&nbsp;name,&nbsp;Int32&nbsp;quantity)</span>&nbsp;line&nbsp;35 &nbsp;&nbsp;---&nbsp;End&nbsp;of&nbsp;stack&nbsp;trace&nbsp;from&nbsp;previous&nbsp;location&nbsp;where&nbsp;exception&nbsp;was&nbsp;thrown&nbsp;---</pre> </p> <p> (I've introduced line breaks and indicated them with the ↩ symbol to make the output more readable. I'll do that again later in the article.) </p> <p> Not surprisingly, the return value of <code>Create</code> is null. You typically have to configure a dynamic mock in order to give it any sort of behaviour, and I didn't do that here. In that case, the dynamic mock returns the default value for the return type, which in this case correctly is null. </p> <p> You may object that the above example is unfair. How can a dynamic mock know what to do? You have to configure it. That's the whole point of it. </p> <h3 id="307eb992e4c54aae8a2f331c05d9d7e4"> Retrieval without creation <a href="#307eb992e4c54aae8a2f331c05d9d7e4" title="permalink">#</a> </h3> <p> Okay, let's set up the dynamic mock: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;dm&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IReservationsRepository&gt;(); dm.Setup(r&nbsp;=&gt;&nbsp;r.ReadReservation(restaurantId,&nbsp;expected.Id)).ReturnsAsync(expected); <span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;dm.Object;</pre> </p> <p> These are the only lines I've changed from the previous listing of the test, which now passes. </p> <p> A common criticism of dynamic-mock-heavy tests is that they mostly 'just test the mocks', and this is exactly what happens here. </p> <p> You can make that more explicit by deleting the <code>Create</code> method call: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;dm&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IReservationsRepository&gt;(); dm.Setup(r&nbsp;=&gt;&nbsp;r.ReadReservation(restaurantId,&nbsp;expected.Id)).ReturnsAsync(expected); <span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;dm.Object; <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); Assert.Equal(expected,&nbsp;actual);</pre> </p> <p> The test still passes. Clearly it only tests the dynamic mock. </p> <p> You may, again, demur that this is expected, and it doesn't demonstrate that dynamic mocks break encapsulation. Keep in mind, however, the nature of the contract: Upon successful completion of <code>Create</code>, the reservation is 'in' the repository and can later be retrieved, either with <code>ReadReservation</code> or <code>ReadReservations</code>. </p> <p> This variation of the test no longer calls <code>Create</code>, yet <code>ReadReservation</code> still returns the <code>expected</code> value. </p> <p> Do <code>SqlReservationsRepository</code> or <code>FakeDatabase</code> behave like that? No, they don't. </p> <p> Try to delete the <code>Create</code> call from the test that exercises <code>SqlReservationsRepository</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlReservationsRepository(connectionString); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); Assert.Equal(expected,&nbsp;actual);</pre> </p> <p> Hardly surprising, the test now fails because <code>actual</code> is null. The same happens if you delete the <code>Create</code> call from the test that exercises <code>FakeDatabase</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); Assert.Equal(expected,&nbsp;actual);</pre> </p> <p> Again, the assertion fails because <code>actual</code> is null. </p> <p> The classes <code>SqlReservationsRepository</code> and <code>FakeDatabase</code> behave according to contract, while the dynamic mock doesn't. </p> <h3 id="81c7041024ad4c5d99055494c5b0bdf0"> Alternative retrieval <a href="#81c7041024ad4c5d99055494c5b0bdf0" title="permalink">#</a> </h3> <p> There's another way in which the dynamic mock breaks encapsulation. Recall what the contract states: Upon successful completion of <code>Create</code>, the reservation is 'in' the repository and can later be retrieved, either with <code>ReadReservation</code> or <code>ReadReservations</code>. </p> <p> In other words, it should be possible to change the interaction from <code>Create</code> followed by <code>ReadReservation</code> to <code>Create</code> followed by <code>ReadReservations</code>. </p> <p> First, try it with <code>SqlReservationsRepository</code>: </p> <p> <pre><span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); <span style="color:blue;">var</span>&nbsp;min&nbsp;=&nbsp;expected.At.Date; <span style="color:blue;">var</span>&nbsp;max&nbsp;=&nbsp;min.AddDays(1); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservations(restaurantId,&nbsp;min,&nbsp;max); Assert.Contains(expected,&nbsp;actual);</pre> </p> <p> The test still passes, as expected. </p> <p> Second, try the same change with <code>FakeDatabase</code>: </p> <p> <pre><span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); <span style="color:blue;">var</span>&nbsp;min&nbsp;=&nbsp;expected.At.Date; <span style="color:blue;">var</span>&nbsp;max&nbsp;=&nbsp;min.AddDays(1); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservations(restaurantId,&nbsp;min,&nbsp;max); Assert.Contains(expected,&nbsp;actual);</pre> </p> <p> Notice that this is the exact same code as in the <code>SqlReservationsRepository</code> test. That test also passes, as expected. </p> <p> Third, try it with the dynamic mock: </p> <p> <pre><span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); <span style="color:blue;">var</span>&nbsp;min&nbsp;=&nbsp;expected.At.Date; <span style="color:blue;">var</span>&nbsp;max&nbsp;=&nbsp;min.AddDays(1); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservations(restaurantId,&nbsp;min,&nbsp;max); Assert.Contains(expected,&nbsp;actual);</pre> </p> <p> Same code, different <code>sut</code>, and the test fails. The dynamic mock breaks encapsulation. You'll have to go and fix the <code>Setup</code> of it to make the test pass again. That's not the case with <code>SqlReservationsRepository</code> or <code>FakeDatabase</code>. </p> <h3 id="09410dcfb758412d991a47f08be885b5"> Dynamic mocks break the SUT, not the tests <a href="#09410dcfb758412d991a47f08be885b5" title="permalink">#</a> </h3> <p> Perhaps you're still not convinced that this is of practical interest. After all, <a href="https://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Meyer</a> had limited success getting mainstream adoption of his thought on contract-based programming. </p> <p> That dynamic mocks break encapsulation does, however, have real implications. </p> <p> What if, instead of using <code>FakeDatabase</code>, I'd used dynamic mocks when testing my online restaurant reservation system? A test might have looked like this: </p> <p> <pre>[Theory] [InlineData(1049,&nbsp;19,&nbsp;00,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [InlineData(1130,&nbsp;18,&nbsp;15,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] [InlineData(&nbsp;956,&nbsp;16,&nbsp;55,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] [InlineData(&nbsp;433,&nbsp;17,&nbsp;30,&nbsp;<span style="color:#a31515;">&quot;shli@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Shanghai&nbsp;Li&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;days, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;hours, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;minutes, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;=&nbsp;DateTime.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dm&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IReservationsRepository&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;dm.Setup(r&nbsp;=&gt;&nbsp;r.ReadReservations(Grandfather.Id,&nbsp;at.Date,&nbsp;at.Date.AddDays(1).AddTicks(-1))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReturnsAsync(Array.Empty&lt;Reservation&gt;()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dm.Object); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Guid(<span style="color:#a31515;">&quot;B50DF5B1-F484-4D99-88F9-1915087AF568&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); &nbsp;&nbsp;&nbsp;&nbsp;dm.Verify(r&nbsp;=&gt;&nbsp;r.Create(Grandfather.Id,&nbsp;expected)); }</pre> </p> <p> This is yet another riff on the <code>PostValidReservationWhenDatabaseIsEmpty</code> test - the gift that keeps giving. I've previously discussed this test in other articles: </p> <ul> <li><a href="/2020/12/07/branching-tests">Branching tests</a></li> <li><a href="/2021/01/11/waiting-to-happen">Waiting to happen</a></li> <li><a href="/2021/01/18/parametrised-test-primitive-obsession-code-smell">Parametrised test primitive obsession code smell</a></li> <li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li> </ul> <p> Here I've replaced the <code>FakeDatabase</code> Test Double with a dynamic mock. (I am, again, using Moq, but keep in mind that the fallout of using a dynamic mock is unrelated to specific libraries.) </p> <p> To go 'full dynamic mock' I should also have replaced <code>SystemClock</code> and <code>InMemoryRestaurantDatabase</code> with dynamic mocks, but that's not necessary to illustrate the point I wish to make. </p> <p> This, and other tests, describe the desired outcome of making a reservation against the REST API. It's an interaction that looks like this: </p> <p> <pre>POST /restaurants/90125/reservations?sig=aco7VV%2Bh5sA3RBtrN8zI8Y9kLKGC60Gm3SioZGosXVE%3D HTTP/1.1 content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-12-12T20:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Pearl&nbsp;Yvonne&nbsp;Gates&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;pearlygates@example.net&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 } HTTP/1.1 201 Created Content-Length: 151 Content-Type: application/json; charset=utf-8 Location: [...]/restaurants/90125/reservations/82e550b1690742368ea62d76e103b232?sig=fPY1fSr[...] { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;82e550b1690742368ea62d76e103b232&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-12-12T20:00:00.0000000&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;pearlygates@example.net&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Pearl&nbsp;Yvonne&nbsp;Gates&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 }</pre> </p> <p> What's of interest here is that the response includes the JSON representation of the resource that the interaction created. It's mostly a copy of the posted data, but enriched with a server-generated ID. </p> <p> The code responsible for the database interaction looks like this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;TryCreate(Restaurant&nbsp;restaurant,&nbsp;Reservation&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;scope&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TransactionScope(TransactionScopeAsyncFlowOption.Enabled); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(restaurant.Id,&nbsp;reservation.At) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;now&nbsp;=&nbsp;Clock.GetCurrentDateTime(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!restaurant.MaitreD.WillAccept(now,&nbsp;reservations,&nbsp;reservation)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;NoTables500InternalServerError(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(restaurant.Id,&nbsp;reservation).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;scope.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Reservation201Created(restaurant.Id,&nbsp;reservation); }</pre> </p> <p> The last line of code creates a <code>201 Created</code> response with the <code>reservation</code> as content. Not shown in this snippet is the origin of the <code>reservation</code> parameter, but it's <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">the input JSON document parsed to a <code>Reservation</code> object</a>. Each <code>Reservation</code> object has <a href="/2022/09/12/coalescing-dtos">an ID that the server creates when it's not supplied by the client</a>. </p> <p> The above <code>TryCreate</code> helper method contains all the database interaction code related to creating a new reservation. It first calls <code>ReadReservations</code> to retrieve the existing reservations. Subsequently, it calls <code>Create</code> if it decides to accept the reservation. The <code>ReadReservations</code> method is actually an <code>internal</code> extension method: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IReservationsRepository&nbsp;repository, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;date) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;min&nbsp;=&nbsp;date.Date; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;max&nbsp;=&nbsp;min.AddDays(1).AddTicks(-1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;repository.ReadReservations(restaurantId,&nbsp;min,&nbsp;max); }</pre> </p> <p> Notice how the dynamic-mock-based test has to replicate this <code>internal</code> implementation detail to the <a href="https://docs.microsoft.com/dotnet/api/system.datetime.ticks">tick</a>. If I ever decide to change this just one tick, the test is going to fail. That's already bad enough (and something that <code>FakeDatabase</code> gracefully handles), but not what I'm driving towards. </p> <p> At the moment the <code>TryCreate</code> method echoes back the <code>reservation</code>. What if, however, you instead want to query the database and return the record that you got from the database? In this particular case, there's no reason to do that, but perhaps in other cases, something happens in the data layer that either enriches or normalises the data. So you make an innocuous change: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;TryCreate(Restaurant&nbsp;restaurant,&nbsp;Reservation&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;scope&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TransactionScope(TransactionScopeAsyncFlowOption.Enabled); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(restaurant.Id,&nbsp;reservation.At) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;now&nbsp;=&nbsp;Clock.GetCurrentDateTime(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!restaurant.MaitreD.WillAccept(now,&nbsp;reservations,&nbsp;reservation)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;NoTables500InternalServerError(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(restaurant.Id,&nbsp;reservation).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;storedReservation&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservation(restaurant.Id,&nbsp;reservation.Id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;scope.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Reservation201Created(restaurant.Id,&nbsp;storedReservation!); }</pre> </p> <p> Now, instead of echoing back <code>reservation</code>, the method calls <code>ReadReservation</code> to retrieve the (possibly enriched or normalised) <code>storedReservation</code> and returns that value. Since this value could, conceivably, be null, for now the method uses the <code>!</code> operator to insist that this is not the case. A new test case might be warranted to cover the scenario where the query returns null. </p> <p> This is perhaps a little less efficient because it implies an extra round-trip to the database, but <em>it shouldn't change the behaviour of the system!</em> </p> <p> But when you run the test suite, that <code>PostValidReservationWhenDatabaseIsEmpty</code> test fails: </p> <p> <pre>Ploeh.Samples.Restaurants.RestApi.Tests.ReservationsTests.PostValidReservationWhenDatabaseIsEmpty(↩ days: 433, hours: 17, minutes: 30, email: "shli@example.org", name: "Shanghai Li", quantity: 5)↩ [FAIL] System.NullReferenceException : Object reference not set to an instance of an object. Stack Trace: [...]\Restaurant.RestApi\ReservationsController.cs(94,0): at↩ [...].RestApi.ReservationsController.Reservation201Created↩ (Int32 restaurantId, Reservation r) [...]\Restaurant.RestApi\ReservationsController.cs(79,0): at↩ [...].RestApi.ReservationsController.TryCreate↩ (Restaurant restaurant, Reservation reservation) [...]\Restaurant.RestApi\ReservationsController.cs(57,0): at↩ [...].RestApi.ReservationsController.Post↩ (Int32 restaurantId, ReservationDto dto) [...]\Restaurant.RestApi.Tests\ReservationsTests.cs(73,0): at↩ [...].RestApi.Tests.ReservationsTests.PostValidReservationWhenDatabaseIsEmpty↩ (Int32 days, Int32 hours, Int32 minutes, String email, String name, Int32 quantity) --- End of stack trace from previous location where exception was thrown ---</pre> </p> <p> Oh, the dreaded <code>NullReferenceException</code>! This happens because <code>ReadReservation</code> returns null, since the dynamic mock isn't configured. </p> <p> The typical reaction that most people have is: <em>Oh no, the tests broke!</em> </p> <p> I think, though, that this is the wrong perspective. The dynamic mock broke the System Under Test (SUT) because it passed an implementation of <code>IReservationsRepository</code> that breaks the contract. The test didn't 'break', because it was never correct from the outset. </p> <h3 id="f720608a0e3b45aa88f1831a4bb1f4e8"> Shotgun surgery <a href="#f720608a0e3b45aa88f1831a4bb1f4e8" title="permalink">#</a> </h3> <p> When a test code base uses dynamic mocks, it tends to do so pervasively. Most tests create one or more dynamic mocks that they pass to their SUT. Most of these dynamic mocks break encapsulation, so when you refactor, the dynamic mocks break the SUT. </p> <p> You'll typically need to revisit and 'fix' all the failing tests to accommodate the refactoring: </p> <p> <pre>[Theory] [InlineData(1049,&nbsp;19,&nbsp;00,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [InlineData(1130,&nbsp;18,&nbsp;15,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] [InlineData(&nbsp;956,&nbsp;16,&nbsp;55,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] [InlineData(&nbsp;433,&nbsp;17,&nbsp;30,&nbsp;<span style="color:#a31515;">&quot;shli@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Shanghai&nbsp;Li&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;days, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;hours, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;minutes, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;=&nbsp;DateTime.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Guid(<span style="color:#a31515;">&quot;B50DF5B1-F484-4D99-88F9-1915087AF568&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dm&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Mock&lt;IReservationsRepository&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;dm.Setup(r&nbsp;=&gt;&nbsp;r.ReadReservations(Grandfather.Id,&nbsp;at.Date,&nbsp;at.Date.AddDays(1).AddTicks(-1))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReturnsAsync(Array.Empty&lt;Reservation&gt;()); &nbsp;&nbsp;&nbsp;&nbsp;dm.Setup(r&nbsp;=&gt;&nbsp;r.ReadReservation(Grandfather.Id,&nbsp;expected.Id)).ReturnsAsync(expected); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dm.Object); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(expected.ToDto()); &nbsp;&nbsp;&nbsp;&nbsp;dm.Verify(r&nbsp;=&gt;&nbsp;r.Create(Grandfather.Id,&nbsp;expected)); }</pre> </p> <p> The test now passes (until the next change in the SUT), but notice how top-heavy it becomes. That's a test code smell when using dynamic mocks. Everything has to happen in <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">the Arrange phase</a>. </p> <p> You typically have many such tests that you need to edit. The name of this antipattern is <a href="https://en.wikipedia.org/wiki/Shotgun_surgery">Shotgun Surgery</a>. </p> <p> The implication is that <em>refactoring</em> by definition is impossible: </p> <blockquote> <p> "to refactor, the essential precondition is [...] solid tests" </p> <footer><cite>Martin Fowler, <a href="/ref/refactoring">Refactoring</a></cite></footer> </blockquote> <p> You need tests that don't break when you refactor. When you use dynamic mocks, tests tend to fail whenever you make changes in SUTs. Even though you have tests, they don't enable refactoring. </p> <p> To add spite to injury, <a href="/2013/04/02/why-trust-tests">every time you edit existing tests, they become less trustworthy</a>. </p> <p> To address these problems, use Fakes instead of Mocks and Stubs. With the <code>FakeDatabase</code> the entire sample test suite for the online restaurant reservation system gracefully handles the change described above. No tests fail. </p> <h3 id="8b30b068b4a4412ca3a797de2760b2be"> Spies <a href="#8b30b068b4a4412ca3a797de2760b2be" title="permalink">#</a> </h3> <p> If you spelunk the test code base for the book, you may also find this Test Double: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SpyPostOffice</span>&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;Collection&lt;SpyPostOffice.Observation&gt;,&nbsp;IPostOffice { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;EmailReservationCreated( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Observation(Event.Created,&nbsp;restaurantId,&nbsp;reservation)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;EmailReservationDeleted( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Observation(Event.Deleted,&nbsp;restaurantId,&nbsp;reservation)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;EmailReservationUpdating( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Observation(Event.Updating,&nbsp;restaurantId,&nbsp;reservation)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;EmailReservationUpdated( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;Observation(Event.Updated,&nbsp;restaurantId,&nbsp;reservation)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">enum</span>&nbsp;<span style="color:#2b91af;">Event</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Created&nbsp;=&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Updating, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Updated, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Deleted &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Observation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Observation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Event&nbsp;@event, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Event&nbsp;=&nbsp;@event; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RestaurantId&nbsp;=&nbsp;restaurantId; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;=&nbsp;reservation; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Event&nbsp;Event&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;RestaurantId&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Reservation&nbsp;Reservation&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>?&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;Observation&nbsp;observation&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Event&nbsp;==&nbsp;observation.Event&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RestaurantId&nbsp;==&nbsp;observation.RestaurantId&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;Reservation&gt;.Default.Equals(Reservation,&nbsp;observation.Reservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;HashCode.Combine(Event,&nbsp;RestaurantId,&nbsp;Reservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As you can see, I've chosen to name this class with the <em>Spy</em> prefix, indicating that this is a Test Spy rather than a Fake Object. A Spy is a Test Double whose main purpose is to observe and record interactions. Does that break or realise encapsulation? </p> <p> While I favour Fakes whenever possible, consider the interface that <code>SpyPostOffice</code> implements: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPostOffice</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;EmailReservationCreated(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Reservation&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;EmailReservationDeleted(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Reservation&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;EmailReservationUpdating(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Reservation&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;EmailReservationUpdated(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;Reservation&nbsp;reservation); }</pre> </p> <p> This interface consist entirely of <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Commands</a>. There's no way to query the interface to examine the state of the object. Thus, you can't check that postconditions hold <em>exclusively via the interface</em>. Instead, you need an additional <a href="http://xunitpatterns.com/Test%20Spy.html">retrieval interface</a> to examine the posterior state of the object. The <code>SpyPostOffice</code> concrete class exposes such an interface. </p> <p> In a sense, you can view <code>SpyPostOffice</code> as an in-memory message sink. It fulfils the contract. </p> <h3 id="b36b228b2b4f4187918c8c224c9aed04"> Concurrency <a href="#b36b228b2b4f4187918c8c224c9aed04" title="permalink">#</a> </h3> <p> Perhaps you're still not convinced. You may argue, for example, that the (partial) contract that I stated is naive. Consider, again, the implications expressed as code: </p> <p> <pre><span style="color:blue;">await</span>&nbsp;sut.Create(restaurantId,&nbsp;expected); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.ReadReservation(restaurantId,&nbsp;expected.Id); Assert.Equal(expected,&nbsp;actual);</pre> </p> <p> You may argue that in the face of concurrency, another thread or process could be making changes to the reservation <em>after</em> <code>Create</code>, but <em>before</em> <code>ReadReservation</code>. Thus, you may argue, the contract I've stipulated is false. In a real system, we can't expect that to be the case. </p> <p> I agree. </p> <p> Concurrency makes things much harder. Even in that light, I think the above line of reasoning is appropriate, for two reasons. </p> <p> First, I chose to model <code>IReservationsRepository</code> like I did because I didn't expect high contention on individual reservations. In other words, I don't expect two or more concurrent processes to attempt to modify <em>the same reservation</em> at the same time. Thus, I found it appropriate to model the Repository as </p> <blockquote> <p> "a collection-like interface for accessing domain objects." </p> <footer><cite>Edward Hieatt and Rob Mee, in Martin Fowler, <a href="/ref/peaa">Patterns of Enterprise Application Architecture</a>, <em>Repository</em> pattern</cite></footer> </blockquote> <p> A <em>collection-like interface</em> implies both data retrieval and collection manipulation members. In low-contention scenarios like the reservation system, this turns out to be a useful model. As the aphorism goes, <a href="https://en.wikipedia.org/wiki/All_models_are_wrong">all models are wrong, but some models are useful</a>. Treating <code>IReservationsRepository</code> as a collection accessed in a non-concurrent manner turned out to be useful in this code base. </p> <p> Had I been more worried about data contention, a move towards <a href="https://martinfowler.com/bliki/CQRS.html">CQRS</a> seems promising. This leads to another object model, with different contracts. </p> <p> Second, even in the face of concurrency, most unit test cases are implicitly running on a single thread. While they may run in parallel, each unit test exercises the SUT on a single thread. This implies that reads and writes against Test Doubles are serialised. </p> <p> Even if concurrency is a real concern, you'd still expect that <em>if only one thread is manipulating the Repository object</em>, then what you <code>Create</code> you should be able to retrieve. The contract may be a little looser, but it'd still be a violation of the <a href="https://en.wikipedia.org/wiki/Principle_of_least_astonishment">principle of least surprise</a> if it was any different. </p> <h3 id="7db1c826945e417db7eacf65ac5207bb"> Conclusion <a href="#7db1c826945e417db7eacf65ac5207bb" title="permalink">#</a> </h3> <p> In object-oriented programming, encapsulation is the notion of separating the affordances of an object from its implementation details. I find it most practical to think about this in terms of contracts, which again can be subdivided into sets of preconditions, invariants, and postconditions. </p> <p> Polymorphic objects (like interfaces and base classes) come with contracts as well. When you replace 'real' implementations with Test Doubles, the Test Doubles should also fulfil the contracts. Fake objects do that; Test Spies may also fit that description. </p> <p> When Test Doubles obey their contracts, you can refactor your SUT without breaking your test suite. </p> <p> By default, however, dynamic mocks break encapsulation because they don't fulfil the objects' contracts. This leads to fragile tests. </p> <p> Favour Fakes over dynamic mocks. You can read more about this way to write tests by following many of the links in this article, or by reading my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="6b55998427954772bed6da4ed8be8694"> <div class="comment-author">Matthew Wiemer <a href="#6b55998427954772bed6da4ed8be8694">#</a></div> <div class="comment-content"> <p> Excellent article exploring the nuances of encapsulation as it relates to testing. That said, the examples here left me with one big question: what exactly is covered by the tests using `FakeDatabase`? </p> <p>This line in particular is confusing me (as to its practical use in a "real-world" setting): `var sut = new FakeDatabase();`</p> <p> How can I claim to have tested the real system's implementation when the "system under test" is, in this approach, explicitly _not_ my real system? It appears the same criticism of dynamic mocks surfaces: "you're only testing the fake database". Does this approach align with any claim you are testing the "real database"? </p> <p> When testing the data-layer, I have historically written (heavier) tests that integrate with a real database to exercise a system's data-layer (as you describe with `SqlReservationsRepository`). I find myself reaching for dynamic mocks in the context of exercising an application's domain layer -- where the data-layer is a dependency providing indirect input/output. Does this use of mocks violate encapsulation in the way this article describes? I _think_ not, because in that case a dynamic mock is used to represent states that are valid "according to the contract", but I'm hoping you could shed a bit more light on the topic. Am I putting the pieces together correctly? </p> <p> Rephrasing the question using your Reservations example code, I would typically inject `IReservationsRepository` into `MaitreD` (which you opt not to do) and outline the posssible database return values (or commands) using dynamic mocks in a test suite of `MaitreD`. What drawbacks, if any, would that approach lead to with respect to encapsulation and test fragility? </p> </div> <div class="comment-date">2022-11-02 20:11 UTC</div> </div> <div class="comment" id="98ef29aec08e408382205b49bea697b6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#98ef29aec08e408382205b49bea697b6">#</a></div> <div class="comment-content"> <p> Matthew, thank you for writing. I apologise if the article is unclear about this, but nowhere in the <em>real</em> code base do I have a test of <code>FakeDatabase</code>. I only wrote the tests that exercise the Test Doubles to illustrate the point I was trying to make. These tests only exist for the benefit of this article. </p> <p> The first <code>CreateAndReadRoundTrip</code> test in the article shows a real integration test. The System Under Test (SUT) is the <code>SqlReservationsRepository</code> class, which is part of the production code - not a Test Double. </p> <p> That class implements the <code>IReservationsRepository</code> interface. The point I was trying to make is that the <code>CreateAndReadRoundTrip</code> test already exercises a particular subset of the contract of the interface. Thus, if one replaces one implementation of the interface with another implementation, according to the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> (LSP) the test should still pass. </p> <p> This is true for <code>FakeDatabase</code>. While the behaviour is different (it doesn't persist data), it still fulfils the contract. Dynamic mocks, on the other hand, don't automatically follow the LSP. Unless one is careful and explicit, dynamic mocks tend to weaken postconditions. For example, a dynamic mock doesn't automatically return the added reservation when you call <code>ReadReservation</code>. </p> <p> This is an essential flaw of dynamic mock objects that is independent of where you use them. My article <a href="#09410dcfb758412d991a47f08be885b5">already describes</a> how a fairly innocuous change in the production code will cause a dynamic mock to break the test. </p> <p> I no longer inject dependencies into domain models, since doing so <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">makes the domain model impure</a>. Even if I did, however, I'd still have the same problem with dynamic mocks breaking encapsulation. </p> </div> <div class="comment-date">2022-11-04 7:06 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Refactoring a saga from the State pattern to the State monad https://blog.ploeh.dk/2022/10/10/refactoring-a-saga-from-the-state-pattern-to-the-state-monad 2022-10-10T06:27:00+00:00 Mark Seemann <div id="post"> <p> <em>A slightly less unrealistic example in C#.</em> </p> <p> This article is one of the examples that I promised in the earlier article <a href="/2022/09/05/the-state-pattern-and-the-state-monad">The State pattern and the State monad</a>. That article examines the relationship between the <a href="https://en.wikipedia.org/wiki/State_pattern">State design pattern</a> and the <a href="/2022/06/20/the-state-monad">State monad</a>. It's deliberately abstract, so one or more examples are in order. </p> <p> In the <a href="/2022/09/26/refactoring-the-tcp-state-pattern-example-to-pure-functions">previous example</a> you saw how to refactor <a href="/ref/dp">Design Patterns</a>' <em>TCP connection</em> example. That example is, unfortunately, hardly illuminating due to its nature, so a second example is warranted. </p> <p> This second example shows how to refactor a stateful asynchronous message handler from the State pattern to the State monad. </p> <h3 id="20b0274c10b14a84a07ebd2086ab1fa0"> Shipping policy <a href="#20b0274c10b14a84a07ebd2086ab1fa0" title="permalink">#</a> </h3> <p> Instead of inventing an example from scratch, I decided to use <a href="https://docs.particular.net/tutorials/nservicebus-sagas/1-saga-basics/">an NServiceBus saga tutorial</a> as a foundation. Read on even if you don't know <a href="https://particular.net/nservicebus">NServiceBus</a>. You don't have to know anything about NServiceBus in order to follow along. I just thought that I'd embed the example code in a context that actually executes and does something, instead of faking it with a bunch of unit tests. Hopefully this will help make the example a bit more realistic and relatable. </p> <p> The example is a simple demo of asynchronous message handling. In a web store shipping department, you should only ship an item once you've received the order and a billing confirmation. When working with asynchronous messaging, you can't, however, rely on message ordering, so perhaps the <code>OrderBilled</code> message arrives before the <code>OrderPlaced</code> message, and sometimes it's the other way around. </p> <p> <img src="/content/binary/shipping-policy-state-diagram.png" alt="Shipping policy state diagram."> </p> <p> Only when you've received both messages may you ship the item. </p> <p> It's a simple workflow, and you don't <em>really</em> need the State pattern. So much is clear from the sample code implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ShippingPolicy</span>&nbsp;:&nbsp;Saga&lt;ShippingPolicyData&gt;, &nbsp;&nbsp;&nbsp;&nbsp;IAmStartedByMessages&lt;OrderBilled&gt;, &nbsp;&nbsp;&nbsp;&nbsp;IAmStartedByMessages&lt;OrderPlaced&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;ILog&nbsp;log&nbsp;=&nbsp;LogManager.GetLogger&lt;ShippingPolicy&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConfigureHowToFindSaga(SagaPropertyMapper&lt;ShippingPolicyData&gt;&nbsp;mapper) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mapper.MapSaga(sagaData&nbsp;=&gt;&nbsp;sagaData.OrderId) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToMessage&lt;OrderPlaced&gt;(message&nbsp;=&gt;&nbsp;message.OrderId) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToMessage&lt;OrderBilled&gt;(message&nbsp;=&gt;&nbsp;message.OrderId); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;Handle(OrderPlaced&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.Info(<span style="color:#a31515;">$&quot;OrderPlaced&nbsp;message&nbsp;received.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProcessOrder(context); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;Handle(OrderBilled&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.Info(<span style="color:#a31515;">$&quot;OrderBilled&nbsp;message&nbsp;received.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProcessOrder(context); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;ProcessOrder(IMessageHandlerContext&nbsp;context) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Data.IsOrderPlaced&nbsp;&amp;&amp;&nbsp;Data.IsOrderBilled) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;context.SendLocal(<span style="color:blue;">new</span>&nbsp;ShipOrder()&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;Data.OrderId&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MarkAsComplete(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I don't expect you to be familiar with the NServiceBus API, so don't worry about the base class, the interfaces, or the <code>ConfigureHowToFindSaga</code> method. What you need to know is that this class handles two types of messages: <code>OrderPlaced</code> and <code>OrderBilled</code>. What the base class and the framework does is handling message correlation, hydration and dehydration, and so on. </p> <p> For the purposes of this demo, all you need to know about the <code>context</code> object is that it enables you to send and publish messages. The code sample uses <code>context.SendLocal</code> to send a new <code>ShipOrder</code> Command. </p> <p> Messages arrive asynchronously and conceptually with long wait times between them. You can't just rely on in-memory object state because a <code>ShippingPolicy</code> instance may receive one message and then risk that the server it's running on shuts down before the next message arrives. The NServiceBus framework handles message correlation and hydration and dehydration of state data. The latter is modelled by the <code>ShippingPolicyData</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ShippingPolicyData</span>&nbsp;:&nbsp;ContainSagaData { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;OrderId&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsOrderPlaced&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsOrderBilled&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> Notice that the above sample code inspects and manipulates the <code>Data</code> property defined by the <code>Saga&lt;ShippingPolicyData&gt;</code> base class. </p> <p> When the <code>ShippingPolicy</code> methods are called by the NServiceBus framework, the <code>Data</code> is automatically populated. When you modify the <code>Data</code>, the state data is automatically persisted when the message handler shuts down to wait for the next message. </p> <h3 id="0edda65734e64a6a99a865c43d6de5be"> Characterisation tests <a href="#0edda65734e64a6a99a865c43d6de5be" title="permalink">#</a> </h3> <p> While you can draw an explicit state diagram like the one above, the sample code doesn't explicitly model the various states as objects. Instead, it relies on reading and writing two Boolean values. </p> <p> There's nothing wrong with this implementation. It's the simplest thing that could possibly work, so why make it more complicated? </p> <p> In this article, I <em>am</em> going to make it more complicated. First, I'm going to refactor the above sample code to use the State design pattern, and then I'm going to refactor that code to use the State monad. From a perspective of maintainability, this isn't warranted, but on the other hand, I hope it's educational. The sample code is just complex enough to showcase the structures of the State pattern and the State monad, yet simple enough that the implementation logic doesn't get in the way. </p> <p> Simplicity can be deceiving, however, and no refactoring is without risk. </p> <blockquote> <p> "to refactor, the essential precondition is [...] solid tests" </p> <footer><cite><a href="https://martinfowler.com/">Martin Fowler</a>, <a href="/ref/refactoring">Refactoring</a></cite></footer> </blockquote> <p> I found it safest to first add a few <a href="https://en.wikipedia.org/wiki/Characterization_test">Characterisation Tests</a> to make sure I didn't introduce any errors as I changed the code. It did catch a few copy-paste goofs that I made, so adding tests turned out to be a good idea. </p> <p> Testing NServiceBus message handlers isn't too hard. All the tests I wrote look similar, so one should be enough to give you an idea. </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;1337&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;baz&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;OrderPlacedAndBilled(<span style="color:blue;">string</span>&nbsp;orderId) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingPolicy&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingPolicyData&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;orderId&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ctx&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TestableMessageHandlerContext(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Handle(<span style="color:blue;">new</span>&nbsp;OrderPlaced&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;orderId&nbsp;},&nbsp;ctx); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Handle(<span style="color:blue;">new</span>&nbsp;OrderBilled&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;orderId&nbsp;},&nbsp;ctx); &nbsp;&nbsp;&nbsp;&nbsp;Assert.True(sut.Completed); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;msg&nbsp;=&nbsp;Assert.Single(ctx.SentMessages.Containing&lt;ShipOrder&gt;()); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(orderId,&nbsp;msg.Message.OrderId); }</pre> </p> <p> The tests use <a href="https://xunit.net/">xUnit.net</a> 2.4.2. When I downloaded the <a href="https://docs.particular.net/tutorials/nservicebus-sagas/1-saga-basics/">NServiceBus saga sample code</a> it targeted .NET Framework 4.8, and I didn't bother to change the version. </p> <p> While the NServiceBus framework will automatically hydrate and populate <code>Data</code>, in a unit test you have to remember to explicitly populate it. The <code>TestableMessageHandlerContext</code> class is a <a href="http://xunitpatterns.com/Test%20Spy.html">Test Spy</a> that is part of <a href="https://docs.particular.net/nservicebus/testing/">NServiceBus testing API</a>. </p> <p> You'd think I was paid by <a href="https://particular.net/">Particular Software</a> to write this article, but I'm not. All this is really just the introduction. You're excused if you've forgotten the topic of this article, but my goal is to show a State pattern example. Only now can we begin in earnest. </p> <h3 id="7b1878c85e134ac5a24e9ce13ba064d2"> State pattern implementation <a href="#7b1878c85e134ac5a24e9ce13ba064d2" title="permalink">#</a> </h3> <p> Refactoring to the State pattern, I chose to let the <code>ShippingPolicy</code> class fill the role of the pattern's <code>Context</code>. Instead of a base class with virtual method, I used an interface to define the <code>State</code> object, as that's more <a href="/2015/08/03/idiomatic-or-idiosyncratic">Idiomatic</a> in C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IShippingState</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;OrderPlaced(OrderPlaced&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context,&nbsp;ShippingPolicy&nbsp;policy); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;OrderBilled(OrderBilled&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context,&nbsp;ShippingPolicy&nbsp;policy); }</pre> </p> <p> The State pattern only shows examples where the <code>State</code> methods take a single argument: The <code>Context</code>. In this case, that's the <code>ShippingPolicy</code>. Careful! There's also a parameter called <code>context</code>! That's the NServiceBus context, and is an artefact of the original example. The two other parameters, <code>message</code> and <code>context</code>, are run-time values passed on from the <code>ShippingPolicy</code>'s <code>Handle</code> methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IShippingState&nbsp;State&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;Handle(OrderPlaced&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;log.Info(<span style="color:#a31515;">$&quot;OrderPlaced&nbsp;message&nbsp;received.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Hydrate(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;State.OrderPlaced(message,&nbsp;context,&nbsp;<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;Dehydrate(); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;Handle(OrderBilled&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;log.Info(<span style="color:#a31515;">$&quot;OrderBilled&nbsp;message&nbsp;received.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Hydrate(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;State.OrderBilled(message,&nbsp;context,&nbsp;<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;Dehydrate(); }</pre> </p> <p> The <code>Hydrate</code> method isn't part of the State pattern, but finds an appropriate state based on <code>Data</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Hydrate() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!Data.IsOrderPlaced&nbsp;&amp;&amp;&nbsp;!Data.IsOrderBilled) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;InitialShippingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">if</span>&nbsp;(Data.IsOrderPlaced&nbsp;&amp;&amp;&nbsp;!Data.IsOrderBilled) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;AwaitingBillingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">if</span>&nbsp;(!Data.IsOrderPlaced&nbsp;&amp;&amp;&nbsp;Data.IsOrderBilled) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;AwaitingPlacementState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;CompletedShippingState.Instance; }</pre> </p> <p> In more recent versions of C# you'd be able to use more succinct pattern matching, but since this code base is on .NET Framework 4.8 I'm constrained to C# 7.3 and this is as good as I cared to make it. It's not important to the topic of the State pattern, but I'm showing it in case you where wondering. It's typical that you need to translate between data that exists in the 'external world' and your object-oriented, polymorphic code, since <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">at the boundaries, applications aren't object-oriented</a>. </p> <p> Likewise, the <code>Dehydrate</code> method translates the other way: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Dehydrate() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(State&nbsp;<span style="color:blue;">is</span>&nbsp;AwaitingBillingState) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(State&nbsp;<span style="color:blue;">is</span>&nbsp;AwaitingPlacementState) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(State&nbsp;<span style="color:blue;">is</span>&nbsp;CompletedShippingState) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">false</span>; }</pre> </p> <p> In any case, <code>Hydrate</code> and <code>Dehydrate</code> are distractions. The important part is that the <code>ShippingPolicy</code> (the State <em>Context</em>) now delegates execution to its <code>State</code>, which performs the actual work and updates the <code>State</code>. </p> <h3 id="0ad9f36d329d45cda95fc92630e65eed"> Initial state <a href="#0ad9f36d329d45cda95fc92630e65eed" title="permalink">#</a> </h3> <p> The first time the saga runs, both <code>Data.IsOrderPlaced</code> and <code>Data.IsOrderBilled</code> are <code>false</code>, which means that the <code>State</code> is <code>InitialShippingState</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InitialShippingState</span>&nbsp;:&nbsp;IShippingState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;InitialShippingState&nbsp;Instance&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InitialShippingState(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">InitialShippingState</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;OrderPlaced( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderPlaced&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;policy.State&nbsp;=&nbsp;AwaitingBillingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;OrderBilled( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderBilled&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;policy.State&nbsp;=&nbsp;AwaitingPlacementState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As the above state transition diagram indicates, the only thing that each of the methods do is that they transition to the next appropriate state: <code>AwaitingBillingState</code> if the first event was <code>OrderPlaced</code>, and <code>AwaitingPlacementState</code> when the event was <code>OrderBilled</code>. </p> <blockquote> <p> "State object are often Singletons" </p> <footer><cite><a href="/ref/dp">Design Patterns</a></cite></footer> </blockquote> <p> Like in the <a href="/2022/09/26/refactoring-the-tcp-state-pattern-example-to-pure-functions">previous example</a> I've made all the State objects <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singletons</a>. It's not that important, but since they are all stateless, we might as well. At least, it's in the spirit of the book. </p> <h3 id="1a3c6882c82d444fb4dff0cb9ffee2e5"> Awaiting billing <a href="#1a3c6882c82d444fb4dff0cb9ffee2e5" title="permalink">#</a> </h3> <p> <code>AwaitingBillingState</code> is another <code>IShippingState</code> implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AwaitingBillingState</span>&nbsp;:&nbsp;IShippingState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IShippingState&nbsp;Instance&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;AwaitingBillingState(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">AwaitingBillingState</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;OrderPlaced( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderPlaced&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;OrderBilled( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderBilled&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;context.SendLocal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ShipOrder()&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;policy.Data.OrderId&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;policy.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;policy.State&nbsp;=&nbsp;CompletedShippingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This State doesn't react to <code>OrderPlaced</code> because it assumes that an order has already been placed. It only reacts to an <code>OrderBilled</code> event. When that happens, all requirements have been fulfilled to ship the item, so it sends a <code>ShipOrder</code> Command, marks the saga as completed, and changes the <code>State</code> to <code>CompletedShippingState</code>. </p> <p> The <code>Complete</code> method is a little wrapper method I had to add to the <code>ShippingPolicy</code> class, since <code>MarkAsComplete</code> is a <code>protected</code> method: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Complete() { &nbsp;&nbsp;&nbsp;&nbsp;MarkAsComplete(); }</pre> </p> <p> The <code>AwaitingPlacementState</code> class is similar to <code>AwaitingBillingState</code>, except that it reacts to <code>OrderPlaced</code> rather than <code>OrderBilled</code>. </p> <h3 id="503262f1d69a4042bb3f93b16aa04d44"> Terminal state <a href="#503262f1d69a4042bb3f93b16aa04d44" title="permalink">#</a> </h3> <p> The fourth and final state is the <code>CompletedShippingState</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CompletedShippingState</span>&nbsp;:&nbsp;IShippingState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IShippingState&nbsp;Instance&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;CompletedShippingState(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">CompletedShippingState</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;OrderPlaced( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderPlaced&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Task&nbsp;OrderBilled( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderBilled&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Task.CompletedTask; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In this state, the saga is completed, so it ignores both events. </p> <h3 id="3769c2c7504548c884cdc38d788a8f46"> Move Commands to output <a href="#3769c2c7504548c884cdc38d788a8f46" title="permalink">#</a> </h3> <p> The saga now uses the State pattern to manage state-specific behaviour as well as state transitions. To be clear, this complexity isn't warranted for the simple requirements. This is, after all, an example. All tests still pass, and smoke testing also indicates that everything still works as it's supposed to. </p> <p> The goal of this article is now to refactor the State pattern implementation to <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. When the saga runs it has an observable side effect: It eventually sends a <code>ShipOrder</code> Command. During processing it also updates its internal state. Both of these are sources of impurity that we have to <a href="/2016/09/26/decoupling-decisions-from-effects">decouple from the decision logic</a>. </p> <p> I'll do this in several steps. The first impure action I'll address is the externally observable message transmission. A common functional-programming trick is to turn a side effect into a return value. So far, the <code>IShippingState</code> methods don't return anything. (This is strictly not true; they each return <a href="https://docs.microsoft.com/dotnet/api/system.threading.tasks.task">Task</a>, but we can regard <code>Task</code> as 'asynchronous <code>void</code>'.) Thus, return values are still available as a communications channel. </p> <p> Refactor the <code>IShippingState</code> methods to return Commands instead of actually sending them. Each method may send an arbitrary number of Commands, including none, so the return type has to be a collection: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IShippingState</span> { &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;OrderPlaced( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderPlaced&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy); &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;OrderBilled( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderBilled&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy); }</pre> </p> <p> When you change the interface you also have to change all the implementing classes, including <code>AwaitingBillingState</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AwaitingBillingState</span>&nbsp;:&nbsp;IShippingState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IShippingState&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;AwaitingBillingState(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">AwaitingBillingState</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;OrderPlaced( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderPlaced&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Array.Empty&lt;ICommand&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;OrderBilled( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderBilled&nbsp;message, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;policy.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;policy.State&nbsp;=&nbsp;CompletedShippingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;ShipOrder()&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;policy.Data.OrderId&nbsp;}&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In order to do nothing a method like <code>OrderPlaced</code> now has to return an empty collection of Commands. In order to 'send' a Command, <code>OrderBilled</code> now returns it instead of using the <code>context</code> to send it. The <code>context</code> is already redundant, but since I prefer to <a href="https://stackoverflow.blog/2022/04/06/use-git-tactically/">move in small steps</a>, I'll remove it in a separate step. </p> <p> It's now the responsibility of the <code>ShippingPolicy</code> class to do something with the Commands returned by the <code>State</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;Handle(OrderBilled&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;log.Info(<span style="color:#a31515;">$&quot;OrderBilled&nbsp;message&nbsp;received.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Hydrate(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;State.OrderBilled(message,&nbsp;context,&nbsp;<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Interpret(result,&nbsp;context); &nbsp;&nbsp;&nbsp;&nbsp;Dehydrate(); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;Interpret( &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;commands, &nbsp;&nbsp;&nbsp;&nbsp;IMessageHandlerContext&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(var&nbsp;cmd&nbsp;<span style="color:blue;">in</span>&nbsp;commands) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;context.SendLocal(cmd); }</pre> </p> <p> In functional programming, you often run an interpreter over the instructions returned by a pure function. Here the interpreter is just a private helper method. </p> <p> The <code>IShippingState</code> methods are no longer asynchronous. Now they just return collections. I consider that a simplification. </p> <h3 id="31ef1f01413143359d91e067da9d9a35"> Remove context parameter <a href="#31ef1f01413143359d91e067da9d9a35" title="permalink">#</a> </h3> <p> The <code>context</code> parameter is now redundant, so remove it from the <code>IShippingState</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IShippingState</span> { &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;OrderPlaced(OrderPlaced&nbsp;message,&nbsp;ShippingPolicy&nbsp;policy); &nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;OrderBilled(OrderBilled&nbsp;message,&nbsp;ShippingPolicy&nbsp;policy); }</pre> </p> <p> I used Visual Studio's built-in refactoring tools to remove the parameter, which automatically removed it from all the call sites and implementations. </p> <p> This takes us part of the way towards implementing the states as pure functions, but there's still work to be done. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;OrderBilled(OrderBilled&nbsp;message,&nbsp;ShippingPolicy&nbsp;policy) { &nbsp;&nbsp;&nbsp;&nbsp;policy.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;policy.State&nbsp;=&nbsp;CompletedShippingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;ShipOrder()&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;policy.Data.OrderId&nbsp;}&nbsp;}; }</pre> </p> <p> The above <code>OrderBilled</code> implementation calls <code>policy.Complete</code> to indicate that the saga has completed. That's another state mutation that must be eliminated to make this a pure function. </p> <h3 id="51ae5d89697b47fe8611b58b4540163e"> Return complex result <a href="#51ae5d89697b47fe8611b58b4540163e" title="permalink">#</a> </h3> <p> How do you refactor from state mutation to pure function? You turn the mutation statement into an instruction, which is a value that you return. In this case you might want to return a Boolean value: True to complete the saga. False otherwise. </p> <p> There seems to be a problem, though. The <code>IShippingState</code> methods already return data: They return a collection of Commands. How do we get around this conundrum? </p> <p> Introduce a complex object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ShippingStateResult</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ShippingStateResult</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;commands, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;completeSaga) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Commands&nbsp;=&nbsp;commands; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompleteSaga&nbsp;=&nbsp;completeSaga; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReadOnlyCollection&lt;ICommand&gt;&nbsp;Commands&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;CompleteSaga&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;ShippingStateResult&nbsp;result&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;IReadOnlyCollection&lt;ICommand&gt;&gt;.Default &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Equals(Commands,&nbsp;result.Commands)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompleteSaga&nbsp;==&nbsp;result.CompleteSaga; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;hashCode&nbsp;=&nbsp;-1668187231; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hashCode&nbsp;=&nbsp;hashCode&nbsp;*&nbsp;-1521134295&nbsp;+&nbsp;EqualityComparer&lt;IReadOnlyCollection&lt;ICommand&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Default.GetHashCode(Commands); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hashCode&nbsp;=&nbsp;hashCode&nbsp;*&nbsp;-1521134295&nbsp;+&nbsp;CompleteSaga.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;hashCode; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> That looks rather horrible, but most of the code is generated by Visual Studio. The only thing I wrote myself was the class declaration and the two read-only properties. I then used Visual Studio's <em>Generate constructor</em> and <em>Generate Equals and GetHashCode</em> Quick Actions to produce the rest of the code. </p> <p> With more modern versions of C# I could have used a <a href="https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/record">record</a>, but as I've already mentioned, I'm on C# 7.3 here. </p> <p> The <code>IShippingState</code> interface can now define its methods with this new return type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IShippingState</span> { &nbsp;&nbsp;&nbsp;&nbsp;ShippingStateResult&nbsp;OrderPlaced(OrderPlaced&nbsp;message,&nbsp;ShippingPolicy&nbsp;policy); &nbsp;&nbsp;&nbsp;&nbsp;ShippingStateResult&nbsp;OrderBilled(OrderBilled&nbsp;message,&nbsp;ShippingPolicy&nbsp;policy); }</pre> </p> <p> This change reminds me of the <a href="https://refactoring.com/catalog/introduceParameterObject.html">Introduce Parameter Object</a> refactoring, but instead applied to the return value instead of input. </p> <p> Implementers now have to return values of this new type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AwaitingBillingState</span>&nbsp;:&nbsp;IShippingState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IShippingState&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;AwaitingBillingState(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">AwaitingBillingState</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ShippingStateResult&nbsp;OrderPlaced(OrderPlaced&nbsp;message,&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingStateResult(Array.Empty&lt;ICommand&gt;(),&nbsp;<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ShippingStateResult&nbsp;OrderBilled(OrderBilled&nbsp;message,&nbsp;ShippingPolicy&nbsp;policy) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;policy.State&nbsp;=&nbsp;CompletedShippingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingStateResult( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;ShipOrder()&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;policy.Data.OrderId&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Moving a statement to an output value implies that the effect must happen somewhere else. It seems natural to put it in the <code>ShippingPolicy</code> class' <code>Interpret</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;Handle(OrderBilled&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;log.Info(<span style="color:#a31515;">$&quot;OrderBilled&nbsp;message&nbsp;received.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Hydrate(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;State.OrderBilled(message,&nbsp;<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Interpret(result,&nbsp;context); &nbsp;&nbsp;&nbsp;&nbsp;Dehydrate(); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;Interpret(ShippingStateResult&nbsp;result,&nbsp;IMessageHandlerContext&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(var&nbsp;cmd&nbsp;<span style="color:blue;">in</span>&nbsp;result.Commands) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;context.SendLocal(cmd); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(result.CompleteSaga) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MarkAsComplete(); }</pre> </p> <p> Since <code>Interpret</code> is an instance method on the <code>ShippingPolicy</code> class I can now also delete the internal <code>Complete</code> method, since <code>MarkAsComplete</code> is already callable (it's a <code>protected</code> method defined by the <code>Saga</code> base class). </p> <h3 id="ce2f9545c94b4c6ab9536050ed030b8b"> Use message data <a href="#ce2f9545c94b4c6ab9536050ed030b8b" title="permalink">#</a> </h3> <p> Have you noticed an odd thing about the code so far? It doesn't use any of the <code>message</code> data! </p> <p> This is an artefact of the original code example. Refer back to the original <code>ProcessOrder</code> helper method. It uses neither <code>OrderPlaced</code> nor <code>OrderBilled</code> for anything. Instead, it pulls the <code>OrderId</code> from the saga's <code>Data</code> property. It can do that because NServiceBus makes sure that all <code>OrderId</code> values are correlated. It'll only instantiate a saga for which <code>Data.OrderId</code> matches <code>OrderPlaced.OrderId</code> or <code>OrderBilled.OrderId</code>. Thus, these values are guaranteed to be the same, and that's why <code>ProcessOrder</code> can get away with using <code>Data.OrderId</code> instead of the <code>message</code> data. </p> <p> So far, through all refactorings, I've retained this detail, but it seems odd. It also couples the implementation methods to the <code>ShippingPolicy</code> class rather than the message classes. For these reasons, refactor the methods to use the message data instead. Here's the <code>AwaitingBillingState</code> implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;ShippingStateResult&nbsp;OrderBilled(OrderBilled&nbsp;message,&nbsp;ShippingPolicy&nbsp;policy) { &nbsp;&nbsp;&nbsp;&nbsp;policy.State&nbsp;=&nbsp;CompletedShippingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingStateResult( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;ShipOrder()&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;message.OrderId&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>); }</pre> </p> <p> Compare this version with the previous iteration, where it used <code>policy.Data.OrderId</code> instead of <code>message.OrderId</code>. </p> <p> Now, the only reason to pass <code>ShippingPolicy</code> as a method parameter is to mutate <code>policy.State</code>. We'll get to that in due time, but first, there's another issue I'd like to address. </p> <h3 id="800453851c1944e3835a05f8ff2e3272"> Immutable arguments <a href="#800453851c1944e3835a05f8ff2e3272" title="permalink">#</a> </h3> <p> Keep in mind that the overall goal of the exercise is to refactor the state machine to pure functions. For good measure, method parameters should be immutable as well. Consider a method like <code>OrderBilled</code> shown above in its most recent iteration. It mutates <code>policy</code> by setting <code>policy.State</code>. The long-term goal is to get rid of that statement. </p> <p> The method doesn't mutate the other argument, <code>message</code>, but the <code>OrderBilled</code> class is actually mutable: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">OrderBilled</span>&nbsp;:&nbsp;IEvent { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;OrderId&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> The same is true for the other message type, <code>OrderPlaced</code>. </p> <p> For good measure, pure functions shouldn't take mutable arguments. You could argue that, since none of the implementation methods actually mutate the messages, it doesn't really matter. I am, however, enough of a neat freak that I don't like to leave such a loose strand dangling. I'd like to refactor the <code>IShippingState</code> API so that only immutable message data is passed as arguments. </p> <p> In a situation like this, there are (at least) three options: </p> <ul> <li> Make the message types immutable. This would mean making <code>OrderBilled</code> and <code>OrderPlaced</code> immutable. These message types are by default mutable <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects</a> (DTO), because NServiceBus needs to serialise and deserialise them to transmit them over durable queues. There are ways you can configure NServiceBus to use serialisation mechanisms that enable immutable records as messages, but for an example code base like this, I might be inclined to reach for an easier solution if one presents itself. </li> <li> Add an immutable 'mirror' class. This may often be a good idea if you have a rich domain model that you'd like to represent. You can see an example of that in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, where there's both a mutable <code>ReservationDto</code> class and an immutable <code>Reservation</code> <a href="https://www.martinfowler.com/bliki/ValueObject.html">Value Object</a>. This makes sense if the invariants of the domain model are sufficiently stronger than the DTO. That hardly seems to be the case here, since both messages only contain an <code>OrderId</code>. </li> <li> Dissolve the DTO into its constituents and pass each as an argument. This doesn't work if the DTO is complex and nested, but here there's only a single constituent element, and that's the <code>OrderId</code> property. </li> </ul> <p> The third option seems like the simplest solution, so refactor the <code>IShippingState</code> methods to take an <code>orderId</code> parameter instead of a message: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IShippingState</span> { &nbsp;&nbsp;&nbsp;&nbsp;ShippingStateResult&nbsp;OrderPlaced(<span style="color:blue;">string</span>&nbsp;orderId,&nbsp;ShippingPolicy&nbsp;policy); &nbsp;&nbsp;&nbsp;&nbsp;ShippingStateResult&nbsp;OrderBilled(<span style="color:blue;">string</span>&nbsp;orderId,&nbsp;ShippingPolicy&nbsp;policy); }</pre> </p> <p> While this is the easiest of the three options given above, the refactoring doesn't hinge on this. It would work just as well with one of the two other options. </p> <p> Implementations now look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;ShippingStateResult&nbsp;OrderBilled(<span style="color:blue;">string</span>&nbsp;orderId,&nbsp;ShippingPolicy&nbsp;policy) { &nbsp;&nbsp;&nbsp;&nbsp;policy.State&nbsp;=&nbsp;CompletedShippingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingStateResult( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;ShipOrder()&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;orderId&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>); }</pre> </p> <p> The only impure action still lingering is the mutation of <code>policy.State</code>. Once we're rid of that, the API consists of pure functions. </p> <h3 id="1034a4283bb44e639805778d5ae4504a"> Return state <a href="#1034a4283bb44e639805778d5ae4504a" title="permalink">#</a> </h3> <p> As outlined by the <a href="/2022/09/05/the-state-pattern-and-the-state-monad">parent article</a>, instead of mutating the caller's state, you can return the state as part of a tuple. This means that you no longer need to pass <code>ShippingPolicy </code> as a parameter: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IShippingState</span> { &nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;ShippingStateResult,&nbsp;IShippingState&gt;&nbsp;OrderPlaced(<span style="color:blue;">string</span>&nbsp;orderId); &nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;ShippingStateResult,&nbsp;IShippingState&gt;&nbsp;OrderBilled(<span style="color:blue;">string</span>&nbsp;orderId); }</pre> </p> <p> Why not expand the <code>ShippingStateResult</code> class, or conversely, dissolve that class and instead return a triple (a three-tuple)? All of these are possible as alternatives, as they'd be isomorphic to this particular design. The reason I've chosen this particular return type is that it's the idiomatic implementation of the State monad: The result is the first element of a tuple, and the state is the second element. This means that you can use a standard, reusable State monad library to manipulate the values, as you'll see later. </p> <p> An implementation now looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AwaitingBillingState</span>&nbsp;:&nbsp;IShippingState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IShippingState&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;AwaitingBillingState(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">AwaitingBillingState</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;ShippingStateResult,&nbsp;IShippingState&gt;&nbsp;OrderPlaced(<span style="color:blue;">string</span>&nbsp;orderId) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Tuple.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingStateResult(Array.Empty&lt;ICommand&gt;(),&nbsp;<span style="color:blue;">false</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(IShippingState)<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;ShippingStateResult,&nbsp;IShippingState&gt;&nbsp;OrderBilled(<span style="color:blue;">string</span>&nbsp;orderId) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Tuple.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingStateResult( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;ShipOrder()&nbsp;{&nbsp;OrderId&nbsp;=&nbsp;orderId&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompletedShippingState.Instance); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since the <code>ShippingPolicy</code> class that calls these methods now directly receives the state as part of the output, it no longer needs a mutable <code>State</code> property. Instead, it immediately handles the return value: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;Handle(OrderPlaced&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;log.Info(<span style="color:#a31515;">$&quot;OrderPlaced&nbsp;message&nbsp;received.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;state&nbsp;=&nbsp;Hydrate(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;state.OrderPlaced(message.OrderId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Interpret(result.Item1,&nbsp;context); &nbsp;&nbsp;&nbsp;&nbsp;Dehydrate(result.Item2); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;Handle(OrderBilled&nbsp;message,&nbsp;IMessageHandlerContext&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;log.Info(<span style="color:#a31515;">$&quot;OrderBilled&nbsp;message&nbsp;received.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;state&nbsp;=&nbsp;Hydrate(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;state.OrderBilled(message.OrderId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Interpret(result.Item1,&nbsp;context); &nbsp;&nbsp;&nbsp;&nbsp;Dehydrate(result.Item2); }</pre> </p> <p> Each <code>Handle</code> method is now an <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>. </p> <p> Since the <code>result</code> is now a tuple, the <code>Handle</code> methods now have to pass the first element (<code>result.Item1</code>) to the <code>Interpret</code> helper method, and the second element (<code>result.Item2</code>) - the state - to <code>Dehydrate</code>. It's also possible to pattern match (or <em>destructure</em>) each of the elements directly; you'll see an example of that later. </p> <p> Since the mutable <code>State</code> property is now gone, the <code>Hydrate</code> method returns the hydrated state: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;IShippingState&nbsp;Hydrate() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!Data.IsOrderPlaced&nbsp;&amp;&amp;&nbsp;!Data.IsOrderBilled) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;InitialShippingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">if</span>&nbsp;(Data.IsOrderPlaced&nbsp;&amp;&amp;&nbsp;!Data.IsOrderBilled) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;AwaitingBillingState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">if</span>&nbsp;(!Data.IsOrderPlaced&nbsp;&amp;&amp;&nbsp;Data.IsOrderBilled) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;AwaitingPlacementState.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;CompletedShippingState.Instance; }</pre> </p> <p> Likewise, the <code>Dehydrate</code> method takes the new state as an input parameter: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Dehydrate(IShippingState&nbsp;state) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(state&nbsp;<span style="color:blue;">is</span>&nbsp;AwaitingBillingState) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(state&nbsp;<span style="color:blue;">is</span>&nbsp;AwaitingPlacementState) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(state&nbsp;<span style="color:blue;">is</span>&nbsp;CompletedShippingState) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderPlaced&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;Data.IsOrderBilled&nbsp;=&nbsp;<span style="color:blue;">false</span>; }</pre> </p> <p> Since each <code>Handle</code> method only calls a single State-valued method, they don't need the State monad machinery. This only becomes useful when you need to compose multiple State-based operations. </p> <p> This might be useful in unit tests, so let's examine that next. </p> <h3 id="8559bfcefc6e49459fa16fce5977cc58"> State monad <a href="#8559bfcefc6e49459fa16fce5977cc58" title="permalink">#</a> </h3> <p> In <a href="/2022/06/20/the-state-monad">previous articles about the State monad</a> you've seen it implemented based on an <code>IState</code> interface. I've also dropped hints here and there that you don't <em>need</em> the interface. Instead, you can implement the monad functions directly on State-valued functions. That's what I'm going to do here: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;S,&nbsp;Tuple&lt;T1,&nbsp;S&gt;&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Func&lt;S,&nbsp;Tuple&lt;T,&nbsp;S&gt;&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Func&lt;S,&nbsp;Tuple&lt;T1,&nbsp;S&gt;&gt;&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;s&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;tuple&nbsp;=&nbsp;source(s); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;f&nbsp;=&nbsp;selector(tuple.Item1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f(tuple.Item2); &nbsp;&nbsp;&nbsp;&nbsp;}; }</pre> </p> <p> This <code>SelectMany</code> implementation works directly on another function, <code>source</code>. This function takes a state of type <code>S</code> as input and returns a tuple as a result. The first element is the result of type <code>T</code>, and the second element is the new state, still of type <code>S</code>. Compare that to <a href="/2021/07/19/the-state-functor">the IState interface</a> to convince yourself that these are just two representations of the same idea. </p> <p> The return value is a new function with the same shape, but where the result type is <code>T1</code> rather than <code>T</code>. </p> <p> You can implement the special <code>SelectMany</code> overload that enables query syntax in <a href="/2022/03/28/monads">the standard way</a>. </p> <p> The <em>return</em> function also mirrors the previous interface-based implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;S,&nbsp;Tuple&lt;T,&nbsp;S&gt;&gt;&nbsp;Return&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;s&nbsp;=&gt;&nbsp;Tuple.Create(x,&nbsp;s); }</pre> </p> <p> You can also implement <a href="/2022/07/04/get-and-put-state">the standard Get, Put, and Modify functions</a>, but we are not going to need them here. Try it as an exercise. </p> <h3 id="4fe0da2976e34c8eaf8490186f7d022a"> State-valued event handlers <a href="#4fe0da2976e34c8eaf8490186f7d022a" title="permalink">#</a> </h3> <p> The <code>IShippingState</code> methods almost look like State values, but the arguments are in the wrong order. A State value is a function that takes state as input and returns a tuple. The methods on <code>IShippingState</code>, however, take <code>orderId</code> as input and return a tuple. The state is also present, but as the instance that exposes the methods. We have to flip the arguments: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;IShippingState,&nbsp;Tuple&lt;ShippingStateResult,&nbsp;IShippingState&gt;&gt;&nbsp;Billed( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:blue;">string</span>&nbsp;orderId) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;s&nbsp;=&gt;&nbsp;s.OrderBilled(orderId); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;IShippingState,&nbsp;Tuple&lt;ShippingStateResult,&nbsp;IShippingState&gt;&gt;&nbsp;Placed( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:blue;">string</span>&nbsp;orderId) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;s&nbsp;=&gt;&nbsp;s.OrderPlaced(orderId); }</pre> </p> <p> This is a typical example of how you have to turn things on their heads in functional programming, compared to object-oriented programming. These two methods convert <code>OrderBilled</code> and <code>OrderPlaced</code> to State monad values. </p> <h3 id="2aa26c7863bc47f6a37ad50f54d97d24"> Testing state results <a href="#2aa26c7863bc47f6a37ad50f54d97d24" title="permalink">#</a> </h3> <p> A unit test demonstrates how this enables you to compose multiple stateful operations using query syntax: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;90125&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;quux&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;StateResultExample(<span style="color:blue;">string</span>&nbsp;orderId) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sf&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;x&nbsp;<span style="color:blue;">in</span>&nbsp;orderId.Placed() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;y&nbsp;<span style="color:blue;">in</span>&nbsp;orderId.Billed() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;x,&nbsp;y&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(results,&nbsp;finalState)&nbsp;=&nbsp;sf(InitialShippingState.Instance); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:blue;">true</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results.Select(r&nbsp;=&gt;&nbsp;r.CompleteSaga)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Single( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(r&nbsp;=&gt;&nbsp;r.Commands) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OfType&lt;ShipOrder&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(msg&nbsp;=&gt;&nbsp;msg.OrderId), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;orderId); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(CompletedShippingState.Instance,&nbsp;finalState); }</pre> </p> <p> Keep in mind that a State monad value is a function. That's the reason I called the composition <code>sf</code> - for <em>State Function</em>. When you execute it with <code>InitialShippingState</code> as input it returns a tuple that the test immediately pattern matches (destructures) into its constituent elements. </p> <p> The test then asserts that the <code>results</code> and <code>finalState</code> are as expected. The assertions against <code>results</code> are a bit awkward, since C# collections don't have structural equality. These assertions would have been simpler in <a href="https://fsharp.org/">F#</a> or <a href="https://www.haskell.org/">Haskell</a>. </p> <h3 id="c899bd7d1b8f4bcea29a5ad946b0f3b8"> Testing with an interpreter <a href="#c899bd7d1b8f4bcea29a5ad946b0f3b8" title="permalink">#</a> </h3> <p> While <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">the Arrange and Act phases of the above test</a> are simple, the Assertion phase seems awkward. Another testing strategy is to run a test-specific interpreter over the instructions returned as the State computation result: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;1984&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;quuz&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;StateInterpretationExample(<span style="color:blue;">string</span>&nbsp;orderId) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sf&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;x&nbsp;<span style="color:blue;">in</span>&nbsp;orderId.Placed() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;y&nbsp;<span style="color:blue;">in</span>&nbsp;orderId.Billed() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;x,&nbsp;y&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;(results,&nbsp;finalState)&nbsp;=&nbsp;sf(InitialShippingState.Instance); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(CompletedShippingState.Instance,&nbsp;finalState); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;Interpret(results); &nbsp;&nbsp;&nbsp;&nbsp;Assert.True(result.CompleteSaga); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Single( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.Commands.OfType&lt;ShipOrder&gt;().Select(msg&nbsp;=&gt;&nbsp;msg.OrderId), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;orderId); }</pre> </p> <p> It helps a little, but the assertions still have to work around the lack of structural equality of <code>result.Commands</code>. </p> <h3 id="80405400d4ed4cfd9c89bf539df21b3b"> Monoid <a href="#80405400d4ed4cfd9c89bf539df21b3b" title="permalink">#</a> </h3> <p> The test-specific <code>Interpret</code> helper method is interesting in its own right, though: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;ShippingStateResult&nbsp;Interpret(IEnumerable&lt;ShippingStateResult&gt;&nbsp;results) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;identity&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingStateResult(Array.Empty&lt;ICommand&gt;(),&nbsp;<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;ShippingStateResult&nbsp;Combine(ShippingStateResult&nbsp;x,&nbsp;ShippingStateResult&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ShippingStateResult( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Commands.Concat(y.Commands).ToArray(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.CompleteSaga&nbsp;||&nbsp;y.CompleteSaga); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;results.Aggregate(identity,&nbsp;Combine); }</pre> </p> <p> It wasn't until I started implementing this helper method that I realised that <code>ShippingStateResult</code> gives rise to a <a href="/2017/10/06/monoids">monoid</a>! Since <a href="/2017/11/20/monoids-accumulate">monoids accumulate</a>, you can start with the <code>identity</code> and use the binary operation (here called <code>Combine</code>) to <code>Aggregate</code> an arbitrary number of <code>ShippingStateResult</code> values into one. </p> <p> The <code>ShippingStateResult</code> class is composed of two constituent values (a collection and a Boolean value), and since both of these give rise to one or more monoids, a <a href="/2017/10/30/tuple-monoids">tuple of those monoids itself gives rise to one or more monoids</a>. The <code>ShippingStateResult</code> is isomorphic to a tuple, so this result carries over. </p> <p> Should you move the <code>Combine</code> method and the <code>identity</code> value to the <code>ShippingStateResult</code> class itself. After all, putting them in a test-specific helper method smells a bit of <a href="https://wiki.c2.com/?FeatureEnvySmell">Feature Envy</a>. </p> <p> This seems compelling, but it's not clear that arbitrary client code might need this particular monoid. After all, there are four monoids over Boolean values, and at least two over collections. That's eight possible combinations. Which one should <code>ShippingStateResult</code> expose as members? </p> <p> The monoid used in <code>Interpret</code> combines the normal <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">collection monoid</a> with the <em>any</em> monoid. That seems appropriate in this case, but other clients might rather need the <em>all</em> monoid. </p> <p> Without more usage examples, I decided to leave the code as an <code>Interpret</code> implementation detail for now. </p> <p> In any case, I find it worth noting that by decoupling the state logic from the NServiceBus framework, it's possible to test it <a href="/2019/02/11/asynchronous-injection">without running asynchronous workflows</a>. </p> <h3 id="a1bc6585723044a9b795aa23099d9d36"> Conclusion <a href="#a1bc6585723044a9b795aa23099d9d36" title="permalink">#</a> </h3> <p> In this article you saw how to implement an asynchronous messaging saga in three different ways. First, as a simple ad-hoc solution, second using the State pattern, and third implemented with the State monad. Both the State pattern and State monad implementations are meant exclusively to showcase these two techniques. The first solution using two Boolean flags is by far the simplest solution, and the one I'd use in a production system. </p> <p> The point is that you can use the State monad if you need to write stateful computations. This may include finite state machines, as otherwise addressed by the State design pattern, but could also include other algorithms where you need to keep track of state. </p> <p> <strong>Next:</strong> <a href="/2021/11/29/postels-law-as-a-profunctor">Postel's law as a profunctor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Some thoughts on the economics of programming https://blog.ploeh.dk/2022/10/03/some-thoughts-on-the-economics-of-programming 2022-10-03T05:53:00+00:00 Mark Seemann <div id="post"> <p> <em>On the net value of process and code quality.</em> </p> <p> Once upon a time there was a software company that had a special way of doing things. No other company had ever done things quite like that before, but the company had much success. In short time it rose to dominance in the market, outcompeting all serious competition. Some people disliked the company because of its business tactics and sheer size, but others admired it. </p> <p> Even more wanted to be like it. </p> <p> How did the company achieve its indisputable success? It looked as though it was really, really good at making software. How did they write such good software? </p> <p> It turned out that the company had a special software development process. </p> <p> Other software organisations, hoping to be able to be as successful, tried to copy the special process. The company was willing to share. Its employees wrote about the process. They gave conference presentations on their special sauce. </p> <p> Which company do I have in mind, and what was the trick that made it so much better than its competition? Was it <a href="https://en.wikipedia.org/wiki/Microservices">microservices</a>? <a href="https://en.wikipedia.org/wiki/Monorepo">Monorepos</a>? <a href="https://en.wikipedia.org/wiki/Kubernetes">Kubernetes</a>? <a href="https://en.wikipedia.org/wiki/DevOps">DevOps</a>? <a href="https://en.wikipedia.org/wiki/Serverless_computing">Serverless</a>? </p> <p> No, the company was <a href="https://microsoft.com">Microsoft</a> and the development process was called <a href="https://en.wikipedia.org/wiki/Microsoft_Solutions_Framework">Microsoft Solutions Framework</a> (MSF). </p> <p> <em>What?!</em> do you say. </p> <p> You've never heard of MSF? </p> <p> That's hardly surprising. I doubt that MSF was in any way related to Microsoft's success. </p> <h3 id="9333d3993dbb4125a7d40faf5221ea02"> Net profits <a href="#9333d3993dbb4125a7d40faf5221ea02" title="permalink">#</a> </h3> <p> These days, many people in technology consider Microsoft an embarrassing dinosaur. While you know that it's still around, does it really <em>matter</em>, these days? </p> <p> You can't deny, however, that Microsoft made a lot of money in the Nineties. They still do. </p> <p> What's the key to making a lot of money? Have a revenue larger than your costs. </p> <p> I'm too lazy to look up the actual numbers, but clearly Microsoft had (and still has) a revenue vastly larger than its costs: </p> <p> <img src="/content/binary/great-net-value-chart.png" alt="Revenue and cost line chart. The revenue is visibly and significantly greater than the cost over the entire time line."> </p> <p> Compared to real, historic numbers, this may be exaggerated, but I'm trying to make a general point - not one that hinges on actual profit numbers of Microsoft, <a href="https://apple.com">Apple</a>, <a href="https://amzn.to/3QLHkly">Amazon</a>, <a href="http://google.com">Google</a>, or any other tremendously profitable company. I'm also aware that real companies have costs that aren't directly related to software development: Marketing, operations, buildings, sales, etcetera. They also make money in other ways than from their software, mainly from investments of the profits. </p> <p> The difference between the revenue and the cost is the profit or net value. </p> <p> If the graph looks like the above, is <em>managing cost</em> the main cause of success? Hardly. The cost is almost a rounding error on the profits. </p> <p> If so, is the technology or process key to such a company's success? Was it MSF that made Microsoft the wealthiest company in the world? Are two-pizza teams the only explanation of Amazon's success? Is Google the dominant search engine because the source code is organised in a monorepo? </p> <p> I'd be surprised were that the case. Rather, I think that these companies were at the right place at the right time. While there were search engines before Google, Google was so much better that users quickly migrated. Google was also better at making money than earlier search engines like <a href="https://en.wikipedia.org/wiki/AltaVista">AltaVista</a> or <a href="https://en.wikipedia.org/wiki/Yahoo!">Yahoo!</a> Likewise, Microsoft made more successful PC operating systems than the competition (which in the early Windows era consisted exclusively of <a href="https://en.wikipedia.org/wiki/OS/2">OS/2</a>) and better professional software (word processor, spreadsheet, etcetera). Amazon made a large-scale international web shop before anyone else. Apple made affordable computers with graphical user interfaces before other companies. Later, they introduced a smartphone at the right time. </p> <p> All of this is simplified. For example, it's not really true that Apple made the first smartphone. When the iPhone was introduced, I already carried a <a href="https://en.wikipedia.org/wiki/Pocket_PC">Pocket PC Phone Edition</a> device that could browse the internet, had email, phone, SMS, and so on. There were other precursors even earlier. </p> <p> I'm not trying to explain away <em>excellence of execution</em>. These companies succeeded for a variety of reasons, including that they were good at what they were doing. Lots of companies, however, are good at what they are doing, and still they fail. Being at the right place at the right time matters. Once in a while, a company finds itself in such favourable circumstances that success is served on a silver platter. While good execution is important, it doesn't explain the magnitude of the success. </p> <p> Bad execution is likely to eliminate you in the long run, but it doesn't follow logically that good execution guarantees success. </p> <p> Perhaps the successful companies succeeded because of circumstances, and <em>despite</em> mediocre execution. As usual, you should be wary not to mistake correlation for causation. </p> <h3 id="d1ab697df716465ab7eb8c841d3a1615"> Legacy code <a href="#d1ab697df716465ab7eb8c841d3a1615" title="permalink">#</a> </h3> <p> You should be sceptical of adopting processes or technology just because a <a href="https://en.wikipedia.org/wiki/Big_Tech">Big Tech</a> company uses it. Still, if that was all I had in mind, I could probably had said that shorter. I have another point to make. </p> <p> I often encounter resistance to ideas about better software development on the grounds that the status quo is good enough. Put bluntly, </p> <blockquote> <p>""legacy," [...] is condescending-engineer-speak for "actually makes money.""</p> <footer><cite><a href="https://www.lastweekinaws.com/blog/right-sizing-your-instances-is-nonsense">Corey Quinn</a></cite></footer> </blockquote> <p> To be clear, I have nothing against the author or the cited article, which discusses something (right-sizing VMs) that I know nothing about. The phrase, or variations thereof, however, is such a fit <a href="https://en.wikipedia.org/wiki/Meme">meme</a> that it spreads. It strongly indicates that people who discuss code quality are wankers, while 'real programmers' produce code that makes money. I consider that a <a href="https://en.wikipedia.org/wiki/False_dilemma">false dichotomy</a>. </p> <p> Most software organisations aren't in the fortunate situation that revenues are orders of magnitude greater than costs. Most software organisations can make a decent profit if they find a market and execute on a good idea. Perhaps the revenue starts at 'only' double the cost. </p> <p> <img src="/content/binary/decreasing-profit-margin-from-increased-cost.png" alt="Revenue and cost line chart. The revenue starts at about double that of the cost. The cost line, however, grows by a steeper rater and eventually overtakes the revenue."> </p> <p> If you can consistently make the double of your costs, you'll be in business for a long time. As the above line chart indicates, however, is that if the costs rise faster than the revenue, you'll eventually hit a point when you start losing money. </p> <p> The Big Tech companies aren't likely to run into that situation because their profit margins are so great, but normal companies are very much at risk. </p> <p> The area between the revenue and the cost represents the profit. Thus, looking back, it may be true that a software system has been making money. This doesn't mean, however, that it will keep making money. </p> <p> In the above chart, the cost eventually exceeds the revenue. If this cost is mainly driven by rising software development costs, then the company is in deep trouble. </p> <p> I've worked with such a company. When I started with it, it was a thriving company with many employees, most of them developers or IT professionals. In the previous decade, it had turned a nice profit every year. </p> <p> This all started to change around the time that I arrived. (I will, again, remind the reader that correlation does not imply causation.) One reason I was engaged was that the developers were stuck. Due to external market pressures they had to deliver a tremendous amount of new features, and they were stuck in <a href="https://en.wikipedia.org/wiki/Analysis_paralysis">analysis paralysis</a>. </p> <p> I helped them get unstuck, but as we started working on the new features, we discovered the size of the mess of the legacy code base. </p> <p> I recall a conversation I later had with the CEO. He told me, after having discussed the situation with several key people: <em>"I knew that we had a legacy code base... but I didn't know it was </em>this<em> bad!"</em> </p> <p> Revenue remained constant, but costs kept rising. Today, the company is no longer around. </p> <p> This was a 100% digital service company. All revenue was ultimately based on software. The business idea was good, but the company couldn't keep up with competitors. As far as I can tell, it was undone by its legacy code base. </p> <h3 id="33ded378d771431fa00f37c8b3e85bfc"> Conclusion <a href="#33ded378d771431fa00f37c8b3e85bfc" title="permalink">#</a> </h3> <p> Software should provide some kind of value. Usually profits, but sometimes savings, and occasionally wider concerns are in scope. It's reasonable and professional to consider value as you produce software. You should, however, be aware of a too myopic focus on immediate and past value. </p> <p> Finding safety in past value is indulging in complacency. Legacy software can make money from day one, but that doesn't mean that it'll keep making money. The main problem with legacy code is that costs keep rising. When non-technical business stakeholders start to notice this, it may be too late. </p> <p> The is one of many reasons I believe that we, software developers, have a <em>responsibility</em> to combat the mess. I don't think there's anything condescending about that attitude. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Refactoring the TCP State pattern example to pure functions https://blog.ploeh.dk/2022/09/26/refactoring-the-tcp-state-pattern-example-to-pure-functions 2022-09-26T05:50:00+00:00 Mark Seemann <div id="post"> <p> <em>A C# example.</em> </p> <p> This article is one of the examples that I promised in the earlier article <a href="/2022/09/05/the-state-pattern-and-the-state-monad">The State pattern and the State monad</a>. That article examines the relationship between the <a href="https://en.wikipedia.org/wiki/State_pattern">State design pattern</a> and the <a href="/2022/06/20/the-state-monad">State monad</a>. That article is deliberately abstract, so one or more examples are in order. </p> <p> In this article, I show you how to start with the example from <a href="/ref/dp">Design Patterns</a> and refactor it to an immutable solution using <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> <p> The code shown here is <a href="https://github.com/ploeh/TCPStateCSharp">available on GitHub</a>. </p> <h3 id="f70ca28a361241e5a352399f8cf912d7"> TCP connection <a href="#f70ca28a361241e5a352399f8cf912d7" title="permalink">#</a> </h3> <p> The example is a class that handles <a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol">TCP</a> connections. The book's example is in C++, while I'll show my C# interpretation. </p> <p> A TCP connection can be in one of several states, so the <code>TcpConnection</code> class keeps an instance of the polymorphic <code>TcpState</code>, which implements the state and transitions between them. </p> <p> <code>TcpConnection</code> plays the role of the State pattern's <code>Context</code>, and <code>TcpState</code> of the <code>State</code>. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TcpConnection</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TcpState&nbsp;State&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TcpConnection</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;TcpClosed.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ActiveOpen() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State.ActiveOpen(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PassiveOpen() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State.PassiveOpen(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members&nbsp;that&nbsp;delegate&nbsp;to&nbsp;State&nbsp;follows...</span></pre> </p> <p> The <code>TcpConnection</code> class' methods delegate to a corresponding method on <code>TcpState</code>, passing itself an argument. This gives the <code>TcpState</code> implementation an opportunity to change the <code>TcpConnection</code>'s <code>State</code> property, which has an <code>internal</code> setter. </p> <h3 id="ffea200499f94145a0686e2481aebf2c"> State <a href="#ffea200499f94145a0686e2481aebf2c" title="permalink">#</a> </h3> <p> This is the <code>TcpState</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TcpState</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Transmit(TcpConnection&nbsp;connection,&nbsp;TcpOctetStream&nbsp;stream) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ActiveOpen(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PassiveOpen(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Close(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Synchronize(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Acknowledge(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Send(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I don't consider this entirely <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> C# code, but it seems closer to the book's C++ example. (It's been a couple of decades since I wrote C++, so I could be mistaken.) It doesn't matter in practice, but instead of a concrete class with <a href="https://en.wikipedia.org/wiki/NOP_(code)">no-op</a> <code>virtual</code> methods, I would usually define an interface. I'll do that in the next example article. </p> <p> The methods have the same names as the methods on <code>TcpConnection</code>, but the signatures are different. All the <code>TcpState</code> methods take a <code>TcpConnection</code> parameter, whereas the <code>TcpConnection</code> methods take no arguments. </p> <p> While the <code>TcpState</code> methods don't do anything, various classes can inherit from the class and override some or all of them. </p> <h3 id="d68a32e30a314c9ea7f45004482a7d98"> Connection closed <a href="#d68a32e30a314c9ea7f45004482a7d98" title="permalink">#</a> </h3> <p> The book shows implementations of three classes that inherit from <code>TcpState</code>, starting with <code>TcpClosed</code>. Here's my translation to C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TcpClosed</span>&nbsp;:&nbsp;TcpState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;TcpState&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TcpClosed(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TcpClosed</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ActiveOpen(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Send&nbsp;SYN,&nbsp;receive&nbsp;SYN,&nbsp;Ack,&nbsp;etc.</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.State&nbsp;=&nbsp;TcpEstablished.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PassiveOpen(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.State&nbsp;=&nbsp;TcpListen.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation overrides <code>ActiveOpen</code> and <code>PassiveOpen</code>. In both cases, after performing some work, they change <code>connection.State</code>. </p> <blockquote> <p> "<code>TCPState</code> subclasses maintain no local state, so they can be shared, and only one instance of each is required. The unique instance of <code>TCPState</code> subclass is obtained by the static <code>Instance</code> operation. [...] </p> <p> "This make each <code>TCPState</code> subclass a Singleton [...]." </p> <footer><cite><a href="/ref/dp">Design Patterns</a></cite></footer> </blockquote> <p> I've maintained that property of each subclass in my C# code, even though it has no impact on the structure of the State pattern. </p> <h3 id="8e21a702cffe4ef09455a29c2981ab39"> The other subclasses <a href="#8e21a702cffe4ef09455a29c2981ab39" title="permalink">#</a> </h3> <p> The next subclass, <code>TcpEstablished</code>, is cast in the same mould: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TcpEstablished</span>&nbsp;:&nbsp;TcpState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;TcpState&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TcpEstablished(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TcpEstablished</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Close(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;send&nbsp;FIN,&nbsp;receive&nbsp;ACK&nbsp;of&nbsp;FIN</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.State&nbsp;=&nbsp;TcpListen.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Transmit( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TcpConnection&nbsp;connection, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TcpOctetStream&nbsp;stream) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.ProcessOctet(stream); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As is <code>TcpListen</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TcpListen</span>&nbsp;:&nbsp;TcpState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;TcpState&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TcpListen(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TcpListen</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Send(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Send&nbsp;SYN,&nbsp;receive&nbsp;SYN,&nbsp;ACK,&nbsp;etc.</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.State&nbsp;=&nbsp;TcpEstablished.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I admit that I find these examples a bit anaemic, since there's really no logic going on. None of the overrides change state <em>conditionally</em>, which would be possible and make the examples a little more interesting. If you're interested in an example where this happens, see my article <a href="/2021/05/24/tennis-kata-using-the-state-pattern">Tennis kata using the State pattern</a>. </p> <h3 id="68a957ca3dcd40e9986b078223f0763e"> Refactor to pure functions <a href="#68a957ca3dcd40e9986b078223f0763e" title="permalink">#</a> </h3> <p> There's only one obvious source of impurity in the example: The literal <code>State</code> mutation of <code>TcpConnection</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;TcpState&nbsp;State&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;}</pre> </p> <p> While client code can't <code>set</code> the <code>State</code> property, subclasses can, and they do. After all, it's how the State pattern works. </p> <p> It's quite a stretch to claim that if we can only get rid of that property setter then all else will be pure. After all, who knows what all those comments actually imply: </p> <p> <pre><span style="color:green;">//&nbsp;Send&nbsp;SYN,&nbsp;receive&nbsp;SYN,&nbsp;ACK,&nbsp;etc.</span></pre> </p> <p> To be honest, we must imagine that <a href="https://en.wikipedia.org/wiki/Input/output">I/O</a> takes place here. This means that even though it's possible to refactor away from mutating the <code>State</code> property, these implementations are not really going to be pure functions. </p> <p> I could try to imagine what that <code>SYN</code> and <code>ACK</code> would look like, but it would be unfounded and hypothetical. I'm not going to do that here. Instead, that's the reason I'm going to publish a second article with a more realistic and complex example. When it comes to the present example, I'm going to proceed with the unreasonable assumption that the comments hide no nondeterministic behaviour or side effects. </p> <p> As outlined in the <a href="/2022/09/05/the-state-pattern-and-the-state-monad">article that compares the State pattern and the State monad</a>, you can refactor state mutation to a pure function by instead returning the new state. Usually, you'd have to return a tuple, because you'd also need to return the 'original' return value. Here, however, the 'return type' of all methods is <code>void</code>, so this isn't necessary. </p> <p> <code>void</code> is <a href="/2018/01/15/unit-isomorphisms">isomorphic to unit</a>, so strictly speaking you could refactor to a return type like <code>Tuple&lt;Unit, TcpConnection&gt;</code>, but that is isomorphic to <code>TcpConnection</code>. (If you need to understand why that is, try writing two functions: One that converts a <code>Tuple&lt;Unit, TcpConnection&gt;</code> to a <code>TcpConnection</code>, and another that converts a <code>TcpConnection</code> to a <code>Tuple&lt;Unit, TcpConnection&gt;</code>.) </p> <p> There's no reason to make things more complicated than they have to be, so I'm going to use the simplest representation: <code>TcpConnection</code>. Thus, you can get rid of the <code>State</code> mutation by instead returning a new <code>TcpConnection</code> from all methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TcpConnection</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TcpState&nbsp;State&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TcpConnection</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;TcpClosed.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TcpConnection</span>(TcpState&nbsp;state) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;state; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TcpConnection&nbsp;ActiveOpen() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;TcpConnection(State.ActiveOpen(<span style="color:blue;">this</span>)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TcpConnection&nbsp;PassiveOpen() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;TcpConnection(State.PassiveOpen(<span style="color:blue;">this</span>)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members&nbsp;that&nbsp;delegate&nbsp;to&nbsp;State&nbsp;follows...</span></pre> </p> <p> The <code>State</code> property no longer has a setter; there's only a public getter. In order to 'change' the state, code must return a new <code>TcpConnection</code> object with the new state. To facilitate that, you'll need to add a constructor overload that takes the new state as an input. Here I made it <code>private</code>, but making it more accessible is not prohibited. </p> <p> This implies, however, that the <code>TcpState</code> methods <em>also</em> return values instead of mutating state. The base class now looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TcpState</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;TcpState&nbsp;Transmit(TcpConnection&nbsp;connection,&nbsp;TcpOctetStream&nbsp;stream) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;TcpState&nbsp;ActiveOpen(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;TcpState&nbsp;PassiveOpen(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;And&nbsp;so&nbsp;on...</span></pre> </p> <p> Again, all the methods previously 'returned' <code>void</code>, so while, according to the State monad, you should strictly speaking return <code>Tuple&lt;Unit, TcpState&gt;</code>, this simplifies to <code>TcpState</code>. </p> <p> Individual subclasses now do their work and return other <code>TcpState</code> implementations. I'm not going to tire you with all the example subclasses, so here's just <code>TcpEstablished</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TcpEstablished</span>&nbsp;:&nbsp;TcpState { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;TcpState&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TcpEstablished(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TcpEstablished</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;TcpState&nbsp;Close(TcpConnection&nbsp;connection) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;send&nbsp;FIN,&nbsp;receive&nbsp;ACK&nbsp;of&nbsp;FIN</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;TcpListen.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;TcpState&nbsp;Transmit( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TcpConnection&nbsp;connection, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TcpOctetStream&nbsp;stream) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TcpConnection&nbsp;newConnection&nbsp;=&nbsp;connection.ProcessOctet(stream); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;newConnection.State; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The trickiest implementation is <code>Transmit</code>, since <code>ProcessOctet</code> returns a <code>TcpConnection</code> while the <code>Transmit</code> method has to return a <code>TcpState</code>. Fortunately, the <code>Transmit</code> method can achieve that goal by returning <code>newConnection.State</code>. It feels a bit roundabout, but highlights a point I made in the <a href="/2022/09/05/the-state-pattern-and-the-state-monad">previous article</a>: The <code>TcpConnection</code> and <code>TcpState</code> classes are isomorphic - or, they would be if we made the <code>TcpConnection</code> constructor overload public. Thus, the <code>TcpConnection</code> class is redundant and might be deleted. </p> <h3 id="a94a0e101f6b49d3b4caea30060ae1e1"> Conclusion <a href="#a94a0e101f6b49d3b4caea30060ae1e1" title="permalink">#</a> </h3> <p> This article shows how to refactor the <em>TCP connection</em> sample code from <a href="/ref/dp">Design Patterns</a> to pure functions. </p> <p> If it feels as though something's missing there's a good reason for that. The example, as given, is degenerate because all methods 'return' <code>void</code>, and we don't really know what the actual implementation code (all that <em>Send SYN, receive SYN, ACK, etc.</em>) looks like. This means that we actually don't have to make use of the State monad, because we can get away with <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphisms</a>. All methods on <code>TcpConnection</code> are really functions that take <code>TcpConnection</code> as input (the instance itself) and return <code>TcpConnection</code>. If you want to see a more realistic example showcasing that perspective, see my article <a href="/2021/05/31/from-state-tennis-to-endomorphism">From State tennis to endomorphism</a>. </p> <p> Even though the example is degenerate, I wanted to show it because otherwise you might wonder how the book's example code fares when exposed to the State monad. To be clear, because of the nature of the example, the State monad never becomes necessary. Thus, we need a second example. </p> <p> <strong>Next:</strong> <a href="/2022/10/10/refactoring-a-saga-from-the-state-pattern-to-the-state-monad">Refactoring a saga from the State pattern to the State monad</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. When to refactor https://blog.ploeh.dk/2022/09/19/when-to-refactor 2022-09-19T06:36:00+00:00 Mark Seemann <div id="post"> <p> <em>FAQ: How do I convince my manager to let me refactor?</em> </p> <p> This question frequently comes up. Developers want to refactor, but are under the impression that managers or other stakeholders will not let them. </p> <p> Sometimes people ask me how to convince their managers to get permission to refactor. I can't answer that. <a href="/2021/03/22/the-dispassionate-developer">I don't know how to convince other people</a>. That's not my métier. </p> <p> I also believe that professional programmers <a href="/2019/03/18/the-programmer-as-decision-maker">should make their own decisions</a>. You don't ask permission to add three lines to a file, or create a new class. Why do you feel that you have to ask permission to refactor? </p> <h3 id="4af4356cd706457ebf8968e92850b140"> Does refactoring take time? <a href="#4af4356cd706457ebf8968e92850b140" title="permalink">#</a> </h3> <p> In <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> I tell the following story: </p> <blockquote> <p> "I once led an effort to <a href="/ref/ddd">refactor towards deeper insight</a>. My colleague and I had identified that the key to implementing a new feature would require changing a fundamental class in our code base. </p> <p> "While such an insight rarely arrives at an opportune time, we wanted to make the change, and our manager allowed it. </p> <p> "A week later, our code still didn’t compile. </p> <p> "I’d hoped that I could make the change to the class in question and then <a href="/ref/welc">lean on the compiler</a> to identify the call sites that needed modification. The problem was that there was an abundance of compilation errors, and fixing them wasn’t a simple question of search-and-replace. </p> <p> "My manager finally took me aside to let me know that he wasn’t satisfied with the situation. I could only concur. </p> <p> "After a mild dressing down, he allowed me to continue the work, and a few more days of heroic effort saw the work completed. </p> <p> "That’s a failure I don’t intend to repeat." </p> <footer><cite><a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a></cite></footer> </blockquote> <p> There's a couple of points to this story. Yes, I <em>did</em> ask for permission before refactoring. I expected the process to take time, and I felt that making such a choice of prioritisation should involve my manager. While this manager trusted me, I felt a moral obligation to be transparent about the work I was doing. I didn't consider it professional to take a week out of the calendar and work on one thing while the rest of the organisation was expecting me to be working on something else. </p> <p> So I can understand why developers feel that they have to ask permission to refactor. After all, refactoring takes time... Doesn't it? </p> <h3 id="ddb8e36130fe475da5ea914ccf06ae45"> Small steps <a href="#ddb8e36130fe475da5ea914ccf06ae45" title="permalink">#</a> </h3> <p> This may unearth the underlying assumption that prevents developers from refactoring: The notion that refactoring takes time. </p> <p> As I wrote in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, that was a failure I didn't intend to repeat. I've never again asked permission to refactor, because I've never since allowed myself to be in a situation where refactoring would take significant time. </p> <p> The reason I tell the story in the book is that I use it to motivate using the <a href="https://martinfowler.com/bliki/StranglerFigApplication.html">Strangler pattern</a> at the code level. The book proceeds to show an example of that. </p> <p> Migrating code to a new API by allowing the old and the new to coexist for a while is only one of many techniques for taking smaller steps. Another is the use of <a href="https://en.wikipedia.org/wiki/Feature_toggle">feature flags</a>, a technique that I also show in the book. <a href="https://martinfowler.com/">Martin Fowler</a>'s <a href="/ref/refactoring">Refactoring</a> is literally an entire book about how to improve code bases in small, controlled steps. </p> <p> Follow the <a href="/2019/10/21/a-red-green-refactor-checklist">red-green-refactor checklist</a> and commit after each <em>green</em> and <em>refactor</em> step. Move in small steps and <a href="https://stackoverflow.blog/2022/04/06/use-git-tactically/">use Git tactically</a>. </p> <p> I'm beginning to realise, though, that <em>moving in small steps</em> is a skill that must be explicitly learned. This may seem obvious once posited, but it may also be helpful to explicitly state it. </p> <p> Whenever I've had a chance to talk to other software professionals and <a href="https://twitter.com/hillelogram/status/1445435617047990273">thought leaders</a>, they agree. As far as I can tell, universities and coding boot camps don't teach this skill, and if (like me) you're autodidact, you probably haven't learned it either. After all, few people insist that this is an important skill. It may, however, be one of the most important programming skills you can learn. </p> <h3 id="11afa6718a3c4a6f8e50aa5066474ac8"> Make it work, then make it right <a href="#11afa6718a3c4a6f8e50aa5066474ac8" title="permalink">#</a> </h3> <p> When should you refactor? As <a href="https://wiki.c2.com/?BoyScoutRule">the boy scout rule</a> suggests: All the time. </p> <p> You can, specifically, do it after implementing a new feature. As <a href="https://wiki.c2.com/?MakeItWorkMakeItRightMakeItFast">Kent Beck perhaps said or wrote</a>: <em>Make it work, then make it right</em>. </p> <p> How long does it take to make it right? </p> <p> Perhaps you think that it takes as much time as it does to make it work. </p> <p> <img src="/content/binary/make-it-work-then-right-50-50.png" alt="A timeline with two sections: 'make it work' and 'make it right'. Each section has the same size."> </p> <p> Perhaps you think that making it right takes even more time. </p> <p> <img src="/content/binary/make-it-work-then-right-20-80.png" alt="A timeline with two sections: 'make it work' and 'make it right'. The 'make it right' section is substantially larger than the 'make it work' section."> </p> <p> If this is how much time making the code right takes, I can understand why you feel that you need to ask your manager. That's what I did, those many years ago. But what if the proportions are more like this? </p> <p> <img src="/content/binary/make-it-work-then-right-80-20.png" alt="A timeline with two sections: 'make it work' and 'make it right'. The 'make it right' section is substantially smaller than the 'make it work' section."> </p> <p> Do you still feel that you need to ask for permission to refactor? </p> <p> Writing code so that the team can keep a sustainable pace is your job. It's not something you should have to ask for permission to do. </p> <blockquote> <p> "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." </p> <footer><cite>Martin Fowler, <a href="/ref/refactoring">Refactoring</a></cite></footer> </blockquote> <p> Making the code right is not always a huge endeavour. It can be, if you've already made a mess of it, but if it's in good condition, keeping it that way doesn't have to take much extra effort. It's part of the ongoing design process that programming is. </p> <p> How do you know what <em>right</em> is? Doesn't this make-it-work-make-it-right mentality lead to <a href="https://wiki.c2.com/?SpeculativeGenerality">speculative generality</a>? </p> <p> No-one expects you to be able to predict the future, so don't try. Making it right means making the code good in the current context. Use good names, remove duplication, get rid of code smells, keep methods small and complexity low. <a href="/2020/04/13/curb-code-rot-with-thresholds">Refactor if you exceed a threshold</a>. </p> <h3 id="1a2eb1f23d70440c84e518babf7dc373"> Make code easy to change <a href="#1a2eb1f23d70440c84e518babf7dc373" title="permalink">#</a> </h3> <p> The purpose of keeping code in a good condition is to make future changes as easy as possible. If you can't predict the future, however, then how do you know how to factor the code? </p> <p> Another <a href="https://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a> aphorism suggests a tactic: </p> <blockquote> <p> "for each desired change, make the change easy (warning: this may be hard), then make the easy change" </p> <footer><cite><a href="https://twitter.com/KentBeck/status/250733358307500032">Kent Beck</a></cite></footer> </blockquote> <p> In other words, when you know what you need to accomplish, first refactor the code so that it becomes easier to achieve the goal, and only then write the code to do that. </p> <p> <img src="/content/binary/refactor-implement-40-60.png" alt="A timeline with two sections: Refactor and Implement. The Implement section is visibly larger than the Refactor section."> </p> <p> Should you ask permission to refactor in such a case? Only if you sincerely believe that you can complete the entire task significantly faster without first improving the code. How likely is that? If the code base is already a mess, how easy is it to make changes? Not easy, and granted: That will also be true for refactoring. The difference between first refactoring and <em>not</em> refactoring, however, is that if you refactor, you leave the code in a better state. If you don't, you leave it in a worse state. </p> <p> These decisions compound. </p> <p> But what if, as Kent Beck implies, refactoring is hard? Then the situation might look like this: </p> <p> <img src="/content/binary/refactor-implement-80-20.png" alt="A timeline with two sections: Refactor and Implement. The Refactor section is significantly larger than the Implement section."> </p> <p> Should you ask for permission to refactor? I don't think so. While refactoring in this diagram is most of the work, it makes the change easy. Thus, once you're done refactoring, you make the easy change. The total amount of time this takes may turn out to be quicker than if you hadn't refactored (compare this figure to the previous figure: they're to scale). You also leave the code base in a better state so that future changes may be easier. </p> <h3 id="ff4ce77459af4e64a9e3df797fc01d8d"> Conclusion <a href="#ff4ce77459af4e64a9e3df797fc01d8d" title="permalink">#</a> </h3> <p> There are lots of opportunities for refactoring. Every time you see something that could be improved, why not improve it? The fact that you're already looking at a piece of code suggests that it's somehow relevant to your current task. If it takes ten, fifteen minutes to improve it, why not do it? What if it takes an hour? </p> <p> Most people think nothing of spending hours in meetings without asking their managers. If this is true, you can also decide to use a couple of hours improving code. They're likely as well spent as the meeting hours. </p> <p> The key, however, is to be able to perform opportunistic refactoring. You can't do that if you can only move in day-long iterations; if hours, or days, go by when you can't compile, or when most tests fail. </p> <p> On the other hand, if you're able to incrementally improve the code base in one-minute, or fifteen-minute, steps, then you can improve the code base every time an occasion arrives. </p> <p> This is a skill that you need to learn. You're not born with the ability to improve in small steps. You'll have to practice - for example by <a href="/2020/01/13/on-doing-katas">doing katas</a>. One customer of mine told me that they found Kent Beck's <a href="https://medium.com/@kentbeck_7670/test-commit-revert-870bbd756864">TCR</a> a great way to teach that skill. </p> <p> You can refactor in small steps. It's part of software engineering. Usually, you don't need to ask for permission. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4dc52487bcee4459857d6fb6741dbcad"> <div class="comment-author"><a href="https://danielt1263.medium.com">Daniel Tartaglia</a> <a href="#4dc52487bcee4459857d6fb6741dbcad">#</a></div> <div class="comment-content"> <p> I've always had a problem with the notion of "red, green, refactor" and "first get it working, then make it right." I think the order is completely wrong. </p> <p> As an explanation, I refer you to the first chapter of the first edition of Martin Fowler's Refactoring book. In that chapter is an example of a working system and we are presented with a change request. </p> <p> In the example, the first thing that Fowler points out and does is the refactoring. And one of the highlighted ideas in the chapter says: <blockquote>When you find you have to add a feature to a program, and the program's code is not structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature.</blockquote> </p> <p> In other words, the refactoring <i>comes first</i>. You refactor as part of adding the feature, not as a separate thing that is done after you have working code. It may not trip off the tongue as nicely, but the saying should be "refactor, red, green." </p> <p> Once you have working code, you are done, and when you are estimating the time it will take to add the feature, <i>you include the refactoring time</i>. Lastly, you never refactor "just because," you refactor in order to make a new feature easy to add. </p> <p> This mode of working makes much more sense to me. I feel that refactoring with no clear goal in mind ("improve the design" is not a clear goal) just leads to an over-designed/unnecessarily complex system. What do you think of this idea? </p> </div> <div class="comment-date">2022-09-24 02:28 UTC</div> </div> <div class="comment" id="f9df3a46ce5f4cb8a386304f64030137"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f9df3a46ce5f4cb8a386304f64030137">#</a></div> <div class="comment-content"> <p> Daniel, thank you for writing. You make some good points. </p> <p> The red-green-refactor cycle is useful as a feedback cycle for new development work. It's not the only way to work. Particularly, as you point out, when you have existing code, first refactoring and then adding new code is a useful order. </p> <p> Typically, though, when you're adding a new feature, you can rarely implement a new feature <em>only</em> by refactoring existing code. Normally you also need to add some new code. I still find the red-green-refactor cycle useful for that kind of work. I don't view it as an <em>either-or</em> proposition, but rather as a <em>both-this-and-that</em> way of working. </p> <blockquote> <p> "you never refactor "just because," you refactor in order to make a new feature easy to add." </p> </blockquote> <p> Never say never. I don't agree with that point of view. There are more than one reason for refactoring, and making room for a new feature is certainly one of them. This does not, however, rule out other reasons. I can easily think of a handful of other reasons that I consider warranted, but I don't want to derail the discussion by listing all of them. The list is not going to be complete anyway. I'll just outline one: </p> <p> Sometimes, you read existing code because you need to understand what's going on. If the code is badly structured, it can take significant time and effort to reach such understanding. If, at that point you can see a simpler way to achieve the same behaviour, why not refactor the code? In that way, you make it easier for future readers of the code to understand what's going on. If you've already spent (wasted) significant time understanding something, why let other readers suffer and waste time if you can simplify the code? </p> <p> This is essentially the boy scout rule, but as I claimed, there are other reasons to refactor as well. </p> <p> Finally, thank you for the quote from <em>Refactoring</em>. I've usually been using this Kent Beck quote: </p> <blockquote> <p> "for each desired change, make the change easy (warning: this may be hard), then make the easy change" </p> <footer><cite><a href="https://twitter.com/kentbeck/status/250733358307500032">Kent Beck</a></cite></footer> </blockquote> <p> but that's from 2012, and <em>Refactoring</em> is from 1999. It's such a Kent Beck thing to say, though, and Kent is a coauthor of <em>Refactoring</em>, so who knows who came up with that. I'm happy to know of the earlier quote, though. </p> </div> <div class="comment-date">2022-10-02 18:19 UTC</div> </div> <div class="comment" id="d2f99d2cf0d2471ea591103b9eda7e9a"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#d2f99d2cf0d2471ea591103b9eda7e9a">#</a></div> <div class="comment-content"> <blockquote> I don't view it as an either-or proposition, but rather as a both-this-and-that way of working. </blockquote> <p> I think it is worth elaborating on this. I think am correct in saying that Mark believes that type-driven development and test-driven development are a both-this-and-that way of working instead of an either-or way of working. He did exactly this in his <a href="https://www.pluralsight.com/courses/fsharp-type-driven-development">Pluralsight course titled Type-Driven Development with F#</a> by first obtaining an implementation using type-driven development and then <i>deleting his implementation</i> but keeping his types and obtaining a second implementation using test-driven development. </p> <p> When implementing a new feature, it is important to as quickly as possible derisk by discovering any surprises (aka unknown unknowns) and analyze all challenges (aka known unknowns). The reason for this is to make sure the intended approach is feasible. During this phase of work, we are in the "green" step of test-driven development. Anything goes. There are no rules. The code can horribly ugly or unmaintainable. Just get the failing test to pass. </p> <p> After the test passes, you have proved that the approach is sound. Now you need to share your solution with others. Here is where refactoring first occurs. Just like in Mark's course, I often find it helpful to <i>start over</i>. Now that I know where I am going, I can first refactor the code to make the functional change, which I know will make the test pass. In this way, I know that all my refactors have a clear goal. </p> <blockquote> You refactor as part of adding the feature, not as a separate thing that is done after you have working code. </blockquote> <p> I agree that refactoring should be done as part of the feature, but I disagree that it should (always) be done before you have working code. It is often done after you have working code. </p> <blockquote> Once you have working code, you are done, and when you are estimating the time it will take to add the feature, <i>you include the refactoring time</i>. </blockquote> <p> I agree that estimating should include the refactoring time, but I disagree that you are done when you have working code. When you have working code, you are approximately halfway done. Your code is currently optimized for writing. You still need to optimize it for reading. </p> </div> <div class="comment-date">2022-10-08 17:42 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Coalescing DTOs https://blog.ploeh.dk/2022/09/12/coalescing-dtos 2022-09-12T07:35:00+00:00 Mark Seemann <div id="post"> <p> <em>Refactoring to a universal abstraction.</em> </p> <p> Despite my best efforts, no code base I write is perfect. This is also true for the code base that accompanies <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> One (among several) warts that has annoyed me for long is this: </p> <p> <pre>[HttpPost(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;Post( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;ReservationDto&nbsp;dto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dto&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(dto)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;id&nbsp;=&nbsp;dto.ParseId()&nbsp;??&nbsp;Guid.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;Reservation?&nbsp;reservation&nbsp;=&nbsp;dto.Validate(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;BadRequestResult(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;code&nbsp;follows...</span></pre> </p> <p> Passing <code>id</code> to <code>Validate</code> annoys me. Why does <code>Validate</code> need an <code>id</code>? </p> <p> When you see it in context, it <em>may</em> makes some sort of sense, but in isolation, it seems arbitrary: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;Reservation?&nbsp;Validate(Guid&nbsp;id)</pre> </p> <p> Why does the method need an <code>id</code>? Doesn't <code>ReservationDto</code> have an <code>Id</code>? </p> <h3 id="90765fa49a8149d0bf54d8d9de19ffee"> Abstraction, broken <a href="#90765fa49a8149d0bf54d8d9de19ffee" title="permalink">#</a> </h3> <p> Yes, indeed, <code>ReservationDto</code> <em>has</em> an <code>Id</code> property: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;}</pre> </p> <p> Then why do callers have to pass an <code>id</code> argument? Doesn't <code>Validate</code> use the <code>Id</code> property? It's almost as though the <code>Validate</code> method <em>begs</em> you to read the implementing code: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;Reservation?&nbsp;Validate(Guid&nbsp;id) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!DateTime.TryParse(At,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;d)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Email&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Quantity&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(Email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity); }</pre> </p> <p> Indeed, the method doesn't use the <code>Id</code> property. Reading the code may not be of much help, but at least we learn that <code>id</code> is passed to the <code>Reservation</code> constructor. It's still not clear why the method isn't trying to parse the <code>Id</code> property, like it's doing with <code>At</code>. </p> <p> I'll return to the motivation in a moment, but first I'd like to dwell on the problems of this design. </p> <p> It's a typical example of ad-hoc design. I had a set of behaviours I needed to implement, and in order to avoid code duplication, I came up with a method that seemed to solve the problem. </p> <p> And indeed, the <code>Validate</code> method does solve the problem of code duplication. It also passes all tests. It could be worse. </p> <p> It could also be better. </p> <p> The problem with an ad-hoc design like this is that the motivation is unclear. As a reader, you feel that you're missing the full picture. Perhaps you feel compelled to read the implementation code to gain a better understanding. Perhaps you look for other call sites. Perhaps you search the Git history to find a helpful comment. Perhaps you ask a colleague. </p> <p> It slows you down. Worst of all, it may leave you apprehensive of refactoring. If you feel that there's something you don't fully understand, you may decide to leave the API alone, instead of improving it. </p> <p> It's one of the many ways that code slowly rots. </p> <p> What's missing here is a proper abstraction. </p> <h3 id="c6b50930baf541eb9243611e2090b1e6"> Motivation <a href="#c6b50930baf541eb9243611e2090b1e6" title="permalink">#</a> </h3> <p> I recently hit upon a design that I like better. Before I describe it, however, you need to understand the problem I was trying to solve. </p> <p> The code base for the book is a restaurant reservation REST API, and I was evolving the code as I wrote it. I wanted the code base (and its Git history) to be as realistic as possible. In a real-world situation, you don't always know all requirements up front, or even if you do, they may change. </p> <p> At one point I decided that a REST client could supply a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">GUID</a> when making a new reservation. On the other hand, I had lots of existing tests (and a deployed system) that accepted reservations <em>without</em> IDs. In order to not break compatibility, I decided to use the ID if it was supplied with the <a href="https://en.wikipedia.org/wiki/Data_transfer_object">DTO</a>, and otherwise create one. (I later explored <a href="/2021/09/20/keep-ids-internal-with-rest">an API without explicit IDs</a>, but that's a different story.) </p> <p> The <code>id</code> is a JSON property, however, so there's no guarantee that it's properly formatted. Thus, the need to first parse it: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;id&nbsp;=&nbsp;dto.ParseId()&nbsp;??&nbsp;Guid.NewGuid();</pre> </p> <p> To make matters even more complicated, when you <code>PUT</code> a reservation, the ID is actually part of the resource address, which means that even if it's present in the JSON document, that value should be ignored: </p> <p> <pre>[HttpPut(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations/{id}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;Put( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;ReservationDto&nbsp;dto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dto&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(dto)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!Guid.TryParse(id,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;rid)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;NotFoundResult(); &nbsp;&nbsp;&nbsp;&nbsp;Reservation?&nbsp;reservation&nbsp;=&nbsp;dto.Validate(rid); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;BadRequestResult(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;code&nbsp;follows...</span></pre> </p> <p> Notice that this <code>Put</code> implementation exclusively considers the resource address <code>id</code> parameter. Recall that the <code>Validate</code> method ignores the <code>dto</code>'s <code>Id</code> property. </p> <p> This is knowledge about implementation details that leaks through to the calling code. As a client developer, you need to know and keep this knowledge in your head while you write your own code. That's not really code that fits in your head. </p> <p> As I usually put it: If you have to read the code, it implies that encapsulation is broken. </p> <p> At the time, however, I couldn't think of a better alternative, and since the problem is still fairly small and localised, I decided to move on. After all, <a href="https://en.wikipedia.org/wiki/Perfect_is_the_enemy_of_good">perfect is the enemy of good</a>. </p> <h3 id="7e10ecc024e94a20817f0054bde56c29"> Why don't you just..? <a href="#7e10ecc024e94a20817f0054bde56c29" title="permalink">#</a> </h3> <p> Is there a better way? Perhaps you think that you've spotted an obvious improvement. Why don't I just try to parse <code>dto.Id</code> and then create a <code>Guid.NewGuid()</code> if parsing fails? Like this: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;Reservation?&nbsp;<span style="color:#74531f;">Validate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!Guid.TryParse(Id,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">id</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;=&nbsp;Guid.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!DateTime.TryParse(At,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">d</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Email&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Quantity&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(Email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity); }</pre> </p> <p> The short answer is: Because it doesn't work. </p> <p> It may work for <code>Get</code>, but then <code>Put</code> doesn't have a way to tell the <code>Validate</code> method which ID to use. </p> <p> Or rather: That's not entirely true, because this is possible: </p> <p> <pre>dto.Id&nbsp;=&nbsp;id; Reservation?&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;dto.Validate();</pre> </p> <p> This does suggest an even better way. Before we go there, however, there's another reason I don't like this particular variation: It makes <code>Validate</code> impure. </p> <p> <em>Why care?</em> you may ask. </p> <p> I always end up regretting making an otherwise potentially <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> non-deterministic. Sooner or later, it turns out to have been a bad decision, regardless of how alluring it initially looked. <a href="/2022/05/23/waiting-to-never-happen">I recently gave an example of that</a>. </p> <p> When weighing the advantages and disadvantages, I preferred passing <code>id</code> explicitly rather than relying on <code>Guid.NewGuid()</code> inside <code>Validate</code>. </p> <h3 id="cb79dc6987854f8b9a897285af649360"> First monoid <a href="#cb79dc6987854f8b9a897285af649360" title="permalink">#</a> </h3> <p> One of the reasons I find <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstractions</a> beneficial is that you only have to learn them once. As Felienne Hermans writes in <a href="/ref/programmers-brain">The Programmer's Brain</a> our working memory juggles a combination of ephemeral data and knowledge from our long-term memory. The better you can leverage existing knowledge, the easier it is to read code. </p> <p> Which universal abstraction enables you to choose from a prioritised list of candidates? The <a href="/2018/04/03/maybe-monoids">First monoid</a>! </p> <p> In C# with <a href="https://docs.microsoft.com/dotnet/csharp/nullable-references">nullable reference types</a> the <a href="https://docs.microsoft.com/dotnet/csharp/language-reference/operators/null-coalescing-operator">null-coalescing operator</a> <code>??</code> already implements the desired functionality. (If you're using another language or an older version of C#, you can instead use <a href="/2018/06/04/church-encoded-maybe">Maybe</a>.) </p> <p> Once I got that idea I was able to simplify the API. </p> <h3 id="ad2f0a4d20da4c9c8006e9587b011911"> Parsing and coalescing DTOs <a href="#ad2f0a4d20da4c9c8006e9587b011911" title="permalink">#</a> </h3> <p> Instead of that odd <code>Validate</code> method which isn't quite a validator and not quite a parser, this insight suggests to <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">parse, don't validate</a>: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;Reservation?&nbsp;<span style="color:#74531f;">TryParse</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!Guid.TryParse(Id,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">id</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!DateTime.TryParse(At,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">d</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Email&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Quantity&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(Email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity); }</pre> </p> <p> This function only returns a parsed <code>Reservation</code> object when the <code>Id</code> is present and well-formed. What about the cases where the <code>Id</code> is absent? </p> <p> The calling <code>ReservationsController</code> can deal with that: </p> <p> <pre>Reservation?&nbsp;<span style="color:#1f377f;">candidate1</span>&nbsp;=&nbsp;dto.TryParse(); dto.Id&nbsp;=&nbsp;Guid.NewGuid().ToString(<span style="color:#a31515;">&quot;N&quot;</span>); Reservation?&nbsp;<span style="color:#1f377f;">candidate2</span>&nbsp;=&nbsp;dto.TryParse(); Reservation?&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;candidate1&nbsp;??&nbsp;candidate2; <span style="color:#8f08c4;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;BadRequestResult();</pre> </p> <p> First try to parse the <code>dto</code>, then explicitly overwrite its <code>Id</code> property with a new <code>Guid</code>, and then try to parse it again. Finally, pick the first of these that aren't null, using the null-coalescing <code>??</code> operator. </p> <p> This API also works consistently in the <code>Put</code> method: </p> <p> <pre>dto.Id&nbsp;=&nbsp;id; Reservation?&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;dto.TryParse(); <span style="color:#8f08c4;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;BadRequestResult();</pre> </p> <p> Why is this better? I consider it better because the <code>TryParse</code> function should be a familiar abstraction. Once you've seen a couple of those, you know that a well-behaved parser either returns a valid object, or nothing. You don't have to go and read the implementation of <code>TryParse</code> to (correctly) guess that. Thus, encapsulation is maintained. </p> <h3 id="72d1eececc3f4166ba426d6a39cd6721"> Where does mutation go? <a href="#72d1eececc3f4166ba426d6a39cd6721" title="permalink">#</a> </h3> <p> The <code>ReservationsController</code> mutates the <code>dto</code> and relies on the impure <code>Guid.NewGuid()</code> method. Why is that okay when it wasn't okay to do this inside of <code>Validate</code>? </p> <p> This is because the code base follows the <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">functional core, imperative shell</a> architecture. Specifically, Controllers make up the imperative shell, so I consider it appropriate to put impure actions there. After all, they have to go somewhere. </p> <p> This means that the <code>TryParse</code> function remains pure. </p> <h3 id="85ce3840fad04a0c8cb106f6d36459d5"> Conclusion <a href="#85ce3840fad04a0c8cb106f6d36459d5" title="permalink">#</a> </h3> <p> Sometimes a good API design can elude you for a long time. When that happens, I move on with the best solution I can think of in the moment. As it often happens, though, ad-hoc abstractions leave me unsatisfied, so I'm always happy to improve such code later, if possible. </p> <p> In this article, you saw an example of an ad-hoc API design that represented the best I could produce at the time. Later, it dawned on me that an implementation based on a universal abstraction would be possible. In this case, the universal abstraction was null coalescing (which is a specialisation of the <em>monoid</em> abstraction). </p> <p> I like universal abstractions because, once you know them, you can trust that they work in well-understood ways. You don't have to waste time reading implementation code in order to learn whether it's safe to call a method in a particular way. </p> <p> This saves time when you have to work with the code, because, after all, we spend more time reading code than writing it. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="abb3e1c91878423a94835a798fc8b32d"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#abb3e1c91878423a94835a798fc8b32d">#</a></div> <div class="comment-content"> <p> After the refactor in this article, is the entirety of your Post method (including the part you didn't show in this article) an <a href="https://blog.ploeh.dk/2020/03/02/impureim-sandwich/">impureim sandwich</a>? </p> </div> <div class="comment-date">2022-09-17 18:37 UTC</div> </div> <div class="comment" id="2352255931b1499184b5ba3782436d61"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2352255931b1499184b5ba3782436d61">#</a></div> <div class="comment-content"> <p> Not yet. There's a lot of (preliminary) interleaving of impure actions and pure functions remaining in the controller, even after this refactoring. </p> <p> A future article will tackle that question. One of the reasons I even started writing about monads, functor relationships, etcetera was to establish the foundations for what this requires. If it can be done without monads and traversals I don't know how. </p> <p> Even though the <code>Post</code> method isn't an impureim sandwich, I still consider the architecture <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">functional core, imperative shell</a>, since I've kept all impure actions in the controllers. </p> <p> The reason I didn't go all the way to impureim sandwiches with the book's code is didactic. For complex logic, you'll need traversals, monads, sum types, and so on, and none of those things were in scope for the book. </p> </div> <div class="comment-date">2022-09-18 19:28 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The State pattern and the State monad https://blog.ploeh.dk/2022/09/05/the-state-pattern-and-the-state-monad 2022-09-05T12:48:00+00:00 Mark Seemann <div id="post"> <p> <em>The names are the same. Is there a connection? An article for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">a series of articles about specific design patterns and their category theory counterparts</a>. In this article I compare the <a href="https://en.wikipedia.org/wiki/State_pattern">State design pattern</a> to the <a href="/2022/06/20/the-state-monad">State monad</a>. </p> <p> Since the design pattern and the monad share the name <em>State</em> you'd think that they might be <a href="/2018/01/08/software-design-isomorphisms">isomorphic</a>, but it's not quite that easy. I find it more likely that the name is an example of <a href="https://en.wikipedia.org/wiki/Parallel_evolution">parallel evolution</a>. Monads were discovered by <a href="https://en.wikipedia.org/wiki/Eugenio_Moggi">Eugenio Moggi</a> in the early nineties, and <a href="/ref/dp">Design Patterns</a> is from 1994. That's close enough in time that I find it more likely that whoever came up with the names found them independently. <em>State</em>, after all, is hardly an exotic word. </p> <p> Thus, it's possible that the choice of the same name is coincidental. If this is true (which is only my conjecture), does the State pattern have anything in common with the State monad? I find that the answer is a tentative <em>yes</em>. The State design pattern describes an open polymorphic stateful computation. That kind of computation can also be described with the State monad. </p> <p> This article contains a significant amount of code, and it's all quite abstract. It examines the abstract shape of the pattern, so there's little prior intuition on which to build an understanding. While later articles will show more concrete examples, if you want to follow along, you can use the <a href="https://github.com/ploeh/StatePatternAndMonad">GitHub repository</a>. </p> <h3 id="320763be103b49018debc64b45069e3c"> Shape <a href="#320763be103b49018debc64b45069e3c" title="permalink">#</a> </h3> <p> <a href="/ref/dp">Design Patterns</a> is a little vague when it comes to representing the essential form of the pattern. What one can deduce from the diagram in the <em>Structure</em> section describing the pattern, you have an abstract <code>State</code> class with a <code>Handle</code> method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Handle</span>(Context&nbsp;<span style="color:#1f377f;">context</span>) { }</pre> </p> <p> This, however, doesn't capture all scenarios. What if you need to pass more arguments to the method? What if the method returns a result? What if there's more than one method? </p> <p> Taking into account all those concerns, you might arrive at a more generalised description of the State pattern where an abstract <code>State</code> class might define methods like these: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;Out1&nbsp;<span style="color:#74531f;">Handle1</span>(Context&nbsp;<span style="color:#1f377f;">context</span>,&nbsp;In1&nbsp;<span style="color:#1f377f;">in1</span>); <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;Out2&nbsp;<span style="color:#74531f;">Handle2</span>(Context&nbsp;<span style="color:#1f377f;">context</span>,&nbsp;In2&nbsp;<span style="color:#1f377f;">in2</span>);</pre> </p> <p> There might be an arbitrary number of <code>Handle</code> methods, from <code>Handle1</code> to <code>HandleN</code>, each with their own input and return types. </p> <p> The idea behind the State pattern is that clients don't interact directly with <code>State</code> objects. Instead, they interact with a <code>Context</code> object that delegates operations to a <code>State</code> object, passing itself as an argument: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Out1&nbsp;<span style="color:#74531f;">Request1</span>(In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;State.Handle1(<span style="color:blue;">this</span>,&nbsp;in1); } <span style="color:blue;">public</span>&nbsp;Out2&nbsp;<span style="color:#74531f;">Request2</span>(In2&nbsp;<span style="color:#1f377f;">in2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;State.Handle2(<span style="color:blue;">this</span>,&nbsp;in2); }</pre> </p> <p> Classes that derive from the abstract <code>State</code> may then mutate <code>context.State</code>. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;Out2&nbsp;<span style="color:#74531f;">Handle2</span>(Context&nbsp;<span style="color:#1f377f;">context</span>,&nbsp;In2&nbsp;<span style="color:#1f377f;">in2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(in2&nbsp;==&nbsp;In2.Epsilon) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context.State&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ConcreteStateB(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Out2.Eta; }</pre> </p> <p> Clients interact with the <code>Context</code> object and aren't aware of this internal machinery: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;ctx.Request2(in2);</pre> </p> <p> With such state mutation going on, is it possible to refactor to a design that uses immutable data and <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>? </p> <h3 id="463f29ae1f8240ec8de7ed18cd429b9f"> State pair <a href="#463f29ae1f8240ec8de7ed18cd429b9f" title="permalink">#</a> </h3> <p> When you have a <code>void</code> method that mutates state, you can refactor it to a pure function by leaving the existing state unchanged and instead returning the new state. What do you do, however, when the method in question already returns a value? </p> <p> This is the case with the generalised <code>HandleN</code> methods, above. </p> <p> One way to resolve this problem is to introduce a more complex type to return. To avoid too much duplication or boilerplate code, you could make it a generic type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">StatePair</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">StatePair</span>(T&nbsp;<span style="color:#1f377f;">value</span>,&nbsp;State&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;state; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;Value&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;State&nbsp;State&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;StatePair&lt;T&gt;&nbsp;<span style="color:#1f377f;">result</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;T&gt;.Default.Equals(Value,&nbsp;result.Value)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;State&gt;.Default.Equals(State,&nbsp;result.State); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;HashCode.Combine(Value,&nbsp;State); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This enables you to change the signatures of the <code>Handle</code> methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;StatePair&lt;Out1&gt;&nbsp;<span style="color:#74531f;">Handle1</span>(Context&nbsp;<span style="color:#1f377f;">context</span>,&nbsp;In1&nbsp;<span style="color:#1f377f;">in1</span>); <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;StatePair&lt;Out2&gt;&nbsp;<span style="color:#74531f;">Handle2</span>(Context&nbsp;<span style="color:#1f377f;">context</span>,&nbsp;In2&nbsp;<span style="color:#1f377f;">in2</span>);</pre> </p> <p> This refactoring is always possible. Even if the original return type of a method was <code>void</code>, you can <a href="/2018/01/15/unit-isomorphisms">use a <em>unit</em> type as a replacement for <em>void</em></a>. While redundant but consistent, a method could return <code>StatePair&lt;Unit&gt;</code>. </p> <h3 id="e2ca8d320e684cf89407880d06256b64"> Generic pair <a href="#e2ca8d320e684cf89407880d06256b64" title="permalink">#</a> </h3> <p> The above <code>StatePair</code> type is so coupled to a particular <code>State</code> class that it's not reusable. If you had more than one implementation of the State pattern in your code base, you'd have to duplicate that effort. That seems wasteful, so why not make the type generic in the state dimension as well? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">StatePair</span>&lt;<span style="color:#2b91af;">TState</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">StatePair</span>(T&nbsp;<span style="color:#1f377f;">value</span>,&nbsp;TState&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;state; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;Value&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TState&nbsp;State&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;StatePair&lt;TState,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">pair</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;T&gt;.Default.Equals(Value,&nbsp;pair.Value)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;TState&gt;.Default.Equals(State,&nbsp;pair.State); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;HashCode.Combine(Value,&nbsp;State); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> When you do that then clearly you'd also need to modify the <code>Handle</code> methods accordingly: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;StatePair&lt;State,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Handle1</span>(Context&nbsp;<span style="color:#1f377f;">context</span>,&nbsp;In1&nbsp;<span style="color:#1f377f;">in1</span>); <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;StatePair&lt;State,&nbsp;Out2&gt;&nbsp;<span style="color:#74531f;">Handle2</span>(Context&nbsp;<span style="color:#1f377f;">context</span>,&nbsp;In2&nbsp;<span style="color:#1f377f;">in2</span>);</pre> </p> <p> Notice that, as is the case with <a href="/2021/07/19/the-state-functor">the State functor</a>, the <em>type</em> declares the type with <code>TState</code> before <code>T</code>, while the <em>constructor</em> takes <code>T</code> before <code>TState</code>. While odd and potentially confusing, I've done this to stay consistent with my previous articles, which again do this to stay consistent with prior art (mainly <a href="https://www.haskell.org/">Haskell</a>). </p> <p> With <code>StatePair</code> you can make the methods pure. </p> <h3 id="cacad33b630d4529b4f703337b9b0a61"> Pure functions <a href="#cacad33b630d4529b4f703337b9b0a61" title="permalink">#</a> </h3> <p> Since <code>Handle</code> methods can now return a new state instead of mutating objects, they can be pure functions. Here's an example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;StatePair&lt;State,&nbsp;Out2&gt;&nbsp;<span style="color:#74531f;">Handle2</span>(Context&nbsp;<span style="color:#1f377f;">context</span>,&nbsp;In2&nbsp;<span style="color:#1f377f;">in2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(in2&nbsp;==&nbsp;In2.Epsilon) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;StatePair&lt;State,&nbsp;Out2&gt;(Out2.Eta,&nbsp;<span style="color:blue;">new</span>&nbsp;ConcreteStateB()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;StatePair&lt;State,&nbsp;Out2&gt;(Out2.Eta,&nbsp;<span style="color:blue;">this</span>); }</pre> </p> <p> The same is true for <code>Context</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;StatePair&lt;Context,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Request1</span>(In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">pair</span>&nbsp;=&nbsp;State.Handle1(<span style="color:blue;">this</span>,&nbsp;in1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;StatePair&lt;Context,&nbsp;Out1&gt;(pair.Value,&nbsp;<span style="color:blue;">new</span>&nbsp;Context(pair.State)); } <span style="color:blue;">public</span>&nbsp;StatePair&lt;Context,&nbsp;Out2&gt;&nbsp;<span style="color:#74531f;">Request2</span>(In2&nbsp;<span style="color:#1f377f;">in2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">pair</span>&nbsp;=&nbsp;State.Handle2(<span style="color:blue;">this</span>,&nbsp;in2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;StatePair&lt;Context,&nbsp;Out2&gt;(pair.Value,&nbsp;<span style="color:blue;">new</span>&nbsp;Context(pair.State)); }</pre> </p> <p> Does this begin to look familiar? </p> <h3 id="bf25630a0d324f628f400c73ddaa78fa"> Monad <a href="#bf25630a0d324f628f400c73ddaa78fa" title="permalink">#</a> </h3> <p> The <code>StatePair</code> class is nothing but a glorified tuple. Armed with that knowledge, you can introduce a variation of <a href="/2021/07/19/the-state-functor">the IState interface I used to introduce the State functor</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IState</span>&lt;<span style="color:#2b91af;">TState</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;StatePair&lt;TState,&nbsp;T&gt;&nbsp;<span style="color:#74531f;">Run</span>(TState&nbsp;<span style="color:#1f377f;">state</span>); }</pre> </p> <p> This variation uses the explicit <code>StatePair</code> class as the return type of <code>Run</code>, rather than a more anonymous tuple. These representations are isomorphic. (That might be a good exercise: Write functions that convert from one to the other, and vice versa.) </p> <p> You can write the usual <code>Select</code> and <code>SelectMany</code> implementations to make <code>IState</code> a functor and monad. Since I have already shown these in previous articles, I'm also going to skip those. (Again, it might be a good exercise to implement them if you're in doubt of how they work.) </p> <p> You can now, for example, use C# query syntax to run the same computation multiple times: </p> <p> <pre>IState&lt;Context,&nbsp;(Out1&nbsp;a,&nbsp;Out1&nbsp;b)&gt;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;a&nbsp;<span style="color:blue;">in</span>&nbsp;in1.Request1() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;b&nbsp;<span style="color:blue;">in</span>&nbsp;in1.Request1() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(a,&nbsp;b); StatePair&lt;Context,&nbsp;(Out1&nbsp;a,&nbsp;Out1&nbsp;b)&gt;&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;s.Run(ctx);</pre> </p> <p> This example calls <code>Request1</code> twice, and collects both return values in a tuple. Running the computation with a <code>Context</code> will produce both a result (the two outputs <code>a</code> and <code>b</code>) as well as the 'current' <code>Context</code> (state). </p> <p> <code>Request1</code> is a State-valued extension method on <code>In1</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;Context,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Request1</span>(<span style="color:blue;">this</span>&nbsp;In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;ctx&nbsp;<span style="color:blue;">in</span>&nbsp;Get&lt;Context&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;p&nbsp;=&nbsp;ctx.Request1(in1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;_&nbsp;<span style="color:blue;">in</span>&nbsp;Put(p.State) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;p.Value; }</pre> </p> <p> Notice the abstraction level in play. This extension method doesn't return a <code>StatePair</code>, but rather an <code>IState</code> computation, defined by using <a href="/2022/07/04/get-and-put-state">the State monad's Get and Put functions</a>. Since the computation is running with a <code>Context</code> state, the computation can <code>Get</code> a <code>ctx</code> object and call its <code>Request1</code> method. This method returns a pair <code>p</code>. The computation can then <code>Put</code> the pair's <code>State</code> (here, a <code>Context</code> object) and return the pair's <code>Value</code>. </p> <p> This stateful computation is composed from the building blocks of the State monad, including query syntax supported by <code>SelectMany</code>, <code>Get</code>, and <code>Put</code>. </p> <p> This does, however, still feel unsatisfactory. After all, you have to know enough of the details of the State monad to know that <code>ctx.Request1</code> returns a pair of which you must remember to <code>Put</code> the <code>State</code>. Would it be possible to also express the underlying <code>Handle</code> methods as stateful computations? </p> <h3 id="f8c4147c58924cd19f1d0bdae8fc05a5"> StatePair bifunctor <a href="#f8c4147c58924cd19f1d0bdae8fc05a5" title="permalink">#</a> </h3> <p> The <code>StatePair</code> class is isomorphic to a <em>pair</em> (a <em>two-tuple</em>), and we know that <a href="/2018/12/31/tuple-bifunctor">a pair gives rise to a bifunctor</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;StatePair&lt;TState1,&nbsp;T1&gt;&nbsp;<span style="color:#74531f;">SelectBoth</span>&lt;<span style="color:#2b91af;">TState1</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">selectValue</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;TState,&nbsp;TState1&gt;&nbsp;<span style="color:#1f377f;">selectState</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;StatePair&lt;TState1,&nbsp;T1&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectValue(Value), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectState(State)); }</pre> </p> <p> You can use <code>SelectBoth</code> to implement both <code>Select</code> and <code>SelectState</code>. In the following we're only going to need <code>SelectState</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;StatePair&lt;TState1,&nbsp;T&gt;&nbsp;<span style="color:#74531f;">SelectState</span>&lt;<span style="color:#2b91af;">TState1</span>&gt;(Func&lt;TState,&nbsp;TState1&gt;&nbsp;<span style="color:#1f377f;">selectState</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;SelectBoth(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;selectState); }</pre> </p> <p> This enables us to slightly simplify the <code>Context</code> methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;StatePair&lt;Context,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Request1</span>(In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;State.Handle1(<span style="color:blue;">this</span>,&nbsp;in1).SelectState(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Context(s)); } <span style="color:blue;">public</span>&nbsp;StatePair&lt;Context,&nbsp;Out2&gt;&nbsp;<span style="color:#74531f;">Request2</span>(In2&nbsp;<span style="color:#1f377f;">in2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;State.Handle2(<span style="color:blue;">this</span>,&nbsp;in2).SelectState(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Context(s)); }</pre> </p> <p> Keep in mind that <code>Handle1</code> returns a <code>StatePair&lt;State,&nbsp;Out1&gt;</code>, <code>Handle2</code> returns <code>StatePair&lt;State,&nbsp;Out2&gt;</code>, and so on. While <code>Request1</code> calls <code>Handle1</code>, it must return a <code>StatePair&lt;Context,&nbsp;Out1&gt;</code> rather than a <code>StatePair&lt;State,&nbsp;Out1&gt;</code>. Since <code>StatePair</code> is a bifunctor, the <code>Request1</code> method can use <code>SelectState</code> to map the <code>State</code> to a <code>Context</code>. </p> <p> Unfortunately, this doesn't seem to move us much closer to being able to express the underlying functions as stateful computations. It does, however, set up the code so that the next change is a little easier to follow. </p> <h3 id="21f08903569d4c1a898914092f2bf3f9"> State computations <a href="#21f08903569d4c1a898914092f2bf3f9" title="permalink">#</a> </h3> <p> Is it possible to express the <code>Handle</code> methods on <code>State</code> as <code>IState</code> computations? One option is to write another extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;State,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Request1S</span>(<span style="color:blue;">this</span>&nbsp;In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;Get&lt;State&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ctx&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Context(s) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;p&nbsp;=&nbsp;s.Handle1(ctx,&nbsp;in1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;_&nbsp;<span style="color:blue;">in</span>&nbsp;Put(p.State) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;p.Value; }</pre> </p> <p> I had to add an <code>S</code> suffix to the name, since it only differs from the above <code>Request1</code> extension method on its return type, and C# doesn't allow method overloading on return types. </p> <p> You can add a similar <code>Request2S</code> extension method. It feels like boilerplate code, but enables us to express the <code>Context</code> methods in terms of running stateful computations: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;StatePair&lt;Context,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Request1</span>(In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;in1.Request1S().Run(State).SelectState(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Context(s)); } <span style="color:blue;">public</span>&nbsp;StatePair&lt;Context,&nbsp;Out2&gt;&nbsp;<span style="color:#74531f;">Request2</span>(In2&nbsp;<span style="color:#1f377f;">in2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;in2.Request2S().Run(State).SelectState(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Context(s)); }</pre> </p> <p> This still isn't entirely satisfactory, since the return types of these <code>Request</code> methods are state pairs, and not <code>IState</code> values. The above <code>Request1S</code> function, however, contains a clue about how to proceed. Notice how it can create a <code>Context</code> object from the underlying <code>State</code>, and convert that <code>Context</code> object back to a <code>State</code> object. That's a generalizable idea. </p> <h3 id="6ec1667fe3354e8bb9b5f7b3540b74f9"> Invariant functor <a href="#6ec1667fe3354e8bb9b5f7b3540b74f9" title="permalink">#</a> </h3> <p> While it's possible to map the <code>TState</code> dimension of the state pair, it seems harder to do it on <code><span style="color:#2b91af;">IState</span>&lt;<span style="color:#2b91af;">TState</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;</code>. A tuple, after all, is covariant in both dimensions. The State monad, on the other hand, is neither co- nor contravariant in the state dimension. You can deduce this with positional variance analysis (which I've learned from <a href="https://thinkingwithtypes.com/">Thinking with Types</a>). In short, this is because <code>TState</code> appears as both input and output in <code>StatePair&lt;TState,&nbsp;T&gt;&nbsp;<span style="color:#74531f;">Run</span>(TState&nbsp;<span style="color:#1f377f;">state</span>)</code> - it's neither co- nor contravariant, but rather <em>invariant</em>. </p> <p> What little option is left us, then, is to make <code>IState</code> an <a href="/2022/08/01/invariant-functors">invariant functor</a> in the state dimension: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;TState1,&nbsp;T&gt;&nbsp;<span style="color:#74531f;">SelectState</span>&lt;<span style="color:#2b91af;">TState</span>,&nbsp;<span style="color:#2b91af;">TState1</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IState&lt;TState,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">state</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;TState,&nbsp;TState1&gt;&nbsp;<span style="color:#1f377f;">forward</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;TState1,&nbsp;TState&gt;&nbsp;<span style="color:#1f377f;">back</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s1&nbsp;<span style="color:blue;">in</span>&nbsp;Get&lt;TState1&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;s&nbsp;=&nbsp;back(s1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;p&nbsp;=&nbsp;state.Run(s) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;_&nbsp;<span style="color:blue;">in</span>&nbsp;Put(forward(p.State)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;p.Value; }</pre> </p> <p> Given an <code>IState&lt;TState,&nbsp;T&gt;</code> the <code>SelectState</code> function enables us to turn it into a <code>IState&lt;TState1,&nbsp;T&gt;</code>. This is, however, only possible if you can translate both <code>forward</code> and <code>back</code> between two representations. When we have two such translations, we can produce a new computation that runs in <code>TState1</code> by first using <code>Get</code> to retrieve a <code>TState1</code> value from the new environment, translate it <code>back</code> to <code>TState</code>, which enables the expression to <code>Run</code> the <code>state</code>. Then translate the resulting <code>p.State</code> <code>forward</code> and <code>Put</code> it. Finally, return the <code>Value</code>. </p> <p> As <a href="https://reasonablypolymorphic.com/">Sandy Maguire</a> explains: </p> <blockquote> <p> "... an invariant type <code>T</code> allows you to map from <code>a</code> to <code>b</code> if and only if <code>a</code> and <code>b</code> are isomorphic. [...] an isomorphism between <code>a</code> and <code>b</code> means they're already the same thing to begin with." </p> <footer><cite>Sandy Maguire, <a href="https://thinkingwithtypes.com/">Thinking with Types</a></cite></footer> </blockquote> <p> This may seem limiting, but is enough in this case. The <code>Context</code> class is only a wrapper of a <code>State</code> object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Context</span>(State&nbsp;<span style="color:#1f377f;">state</span>) { &nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;=&nbsp;state; } <span style="color:blue;">public</span>&nbsp;State&nbsp;State&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}</pre> </p> <p> If you have a <code>State</code> object, you can create a <code>Context</code> object via the <code>Context</code> constructor. On the other hand, if you have a <code>Context</code> object, you can get the wrapped <code>State</code> object by reading the <code>State</code> property. </p> <p> The first improvement this offers is simplification of the <code>Request1</code> extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;Context,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Request1</span>(<span style="color:blue;">this</span>&nbsp;In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;in1.Request1S().SelectState(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Context(s),&nbsp;<span style="color:#1f377f;">ctx</span>&nbsp;=&gt;&nbsp;ctx.State); }</pre> </p> <p> Recall that <code>Request1S</code> returns a <code>IState&lt;State,&nbsp;Out1&gt;</code>. Since a two-way translation between <code>State</code> and <code>Context</code> exists, <code>SelectState</code> can translate <code>IState&lt;State,&nbsp;Out1&gt;</code> to <code>IState&lt;Context,&nbsp;Out1&gt;</code>. </p> <p> The same applies to the equivalent <code>Request2</code> extension method. </p> <p> This, again, enables us to rewrite the <code>Context</code> methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;StatePair&lt;Context,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Request1</span>(In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;in1.Request1().Run(<span style="color:blue;">this</span>); } <span style="color:blue;">public</span>&nbsp;StatePair&lt;Context,&nbsp;Out2&gt;&nbsp;<span style="color:#74531f;">Request2</span>(In2&nbsp;<span style="color:#1f377f;">in2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;in2.Request2().Run(<span style="color:blue;">this</span>); }</pre> </p> <p> While this may seem like an insignificant change, one result has been gained: This last refactoring pushed the <code>Run</code> call to the right. It's now clear that each expression is a stateful computation, and that the only role that the <code>Request</code> methods play is to <code>Run</code> the computations. </p> <p> This illustrates that the <code>Request</code> methods can be decomposed into two decoupled steps: <ol> <li>A stateful computation expression</li> <li>Running the expression</li> </ol> The question now becomes: How useful is the <code>Context</code> wrapper class now? </p> <h3 id="4fc39c9d7bd647cb9773344082468051"> Eliminating the Context <a href="#4fc39c9d7bd647cb9773344082468051" title="permalink">#</a> </h3> <p> A reasonable next refactoring might be to remove the <code>context</code> parameter from each of the <code>Handle</code> methods. After all, this parameter is a remnant of the State design pattern. Its original purpose was to enable <code>State</code> implementers to mutate the <code>context</code> by changing its <code>State</code>. </p> <p> After refactoring to immutable functions, the <code>context</code> parameter no longer needs to be there - for that reason. Do we need it for other reasons? Does it carry other information that a <code>State</code> implementer might need? </p> <p> In the form that the code now has, it doesn't. Even if it did, we could consider moving that data to the other input parameter: <code>In1</code>, <code>In2</code>, etcetera. </p> <p> Therefore, it seems sensible to remove the <code>context</code> parameter from the <code>State</code> methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;StatePair&lt;State,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Handle1</span>(In1&nbsp;<span style="color:#1f377f;">in1</span>); <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;StatePair&lt;State,&nbsp;Out2&gt;&nbsp;<span style="color:#74531f;">Handle2</span>(In2&nbsp;<span style="color:#1f377f;">in2</span>);</pre> </p> <p> This also means that a function like <code>Request1S</code> becomes simpler: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;State,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Request1S</span>(<span style="color:blue;">this</span>&nbsp;In1&nbsp;<span style="color:#1f377f;">in1</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;Get&lt;State&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;p&nbsp;=&nbsp;s.Handle1(in1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;_&nbsp;<span style="color:blue;">in</span>&nbsp;Put(p.State) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;p.Value; }</pre> </p> <p> Since <code>Context</code> and <code>State</code> are isomorphic, you can rewrite all callers of <code>Context</code> to instead use <code>State</code>, like the above example: </p> <p> <pre>IState&lt;State,&nbsp;(Out1&nbsp;a,&nbsp;Out1&nbsp;b)&gt;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;a&nbsp;<span style="color:blue;">in</span>&nbsp;in1.Request1() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;b&nbsp;<span style="color:blue;">in</span>&nbsp;in1.Request1() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(a,&nbsp;b); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;s.Run(csa);</pre> </p> <p> Do this consistently, and you can eventually delete the <code>Context</code> class. </p> <h3 id="c92292ad071c4b468fa223d764d89ab8"> Further possible refactorings <a href="#c92292ad071c4b468fa223d764d89ab8" title="permalink">#</a> </h3> <p> With the <code>Context</code> class gone, you're left with the abstract <code>State</code> class and its implementers: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">State</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;StatePair&lt;State,&nbsp;Out1&gt;&nbsp;<span style="color:#74531f;">Handle1</span>(In1&nbsp;<span style="color:#1f377f;">in1</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;StatePair&lt;State,&nbsp;Out2&gt;&nbsp;<span style="color:#74531f;">Handle2</span>(In2&nbsp;<span style="color:#1f377f;">in2</span>); }</pre> </p> <p> One further change worth considering might be to change the abstract base class to an interface. </p> <p> In this article, I've considered the general case where the <code>State</code> class supports an arbitrary number of independent state transitions, symbolised by the methods <code>Handle1</code> and <code>Handle2</code>. With an arbitrary number of such state transitions, you would have additional methods up to <code>HandleN</code> for <em>N</em> independent state transitions. </p> <p> At the other extreme, you may have just a single polymorphic state transition function. My intuition tells me that that's more likely to be the case than one would think at first. </p> <h3 id="c53b386a92b04575b675272d2dc85365"> Relationship between pattern and monad <a href="#c53b386a92b04575b675272d2dc85365" title="permalink">#</a> </h3> <p> You can view the State design pattern as a combination of two common practices in object-oriented programming: Mutation and polymorphism. </p> <p> <img src="/content/binary/state-pattern-venn.png" alt="Venn diagram with the two sets mutation and polymorphism. The intersection is labelled state."> </p> <p> The patterns in <em>Design Patterns</em> rely heavily on mutation of object state. Most other 'good' object-oriented code tends to do likewise. </p> <p> Proper object-oriented code also makes good use of polymorphism. Again, refer to <em>Design Patterns</em> or a book like <a href="https://blog.ploeh.dk/ref/refactoring">Refactoring</a> for copious examples. </p> <p> I view the State pattern as the intersection of these two common practices. The problem to solve is this: </p> <blockquote> <p> "Allow an object to alter its behavior when its internal state changes." </p> <footer><cite><a href="/ref/dp">Design Patterns</a></cite></footer> </blockquote> <p> The State pattern achieves that goal by having an inner polymorphic object (<code>State</code>) wrapped by an container object (<code>Context</code>). The <code>State</code> objects can mutate the <code>Context</code>, which enables them to replace themselves with other states. </p> <p> While functional programming also has notions of polymorphism, a pure function can't mutate state. Instead, a pure function must return a new state, leaving the old state unmodified. If there's nothing else to return, you can model such state-changing behaviour as an <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a>. The article <a href="/2021/05/31/from-state-tennis-to-endomorphism">From State tennis to endomorphism</a> gives a quite literal example of that. </p> <p> Sometimes, however, an object-oriented method does more than one thing: It both mutates state and returns a value. (This, by the way, violates <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">the Command Query Separation principle</a>.) The State monad is the functional way of doing that: Return both the result and the new state. </p> <p> Essentially, you replace mutation with the State monad. </p> <p> <img src="/content/binary/state-monad-venn.png" alt="Venn diagram with the two sets state monad and polymorphism. The intersection is labelled state."> </p> <p> From a functional perspective, then, we can view the State pattern as the intersection of polymorphism and the State monad. </p> <h3 id="d31601e4146d4f48ae30228f697de0a0"> Examples <a href="#d31601e4146d4f48ae30228f697de0a0" title="permalink">#</a> </h3> <p> This article is both long and abstract. Some examples might be helpful, so I'll give a few in separate articles: <ul> <li><a href="/2022/09/26/refactoring-the-tcp-state-pattern-example-to-pure-functions">Refactoring the TCP State pattern example to pure functions</a></li> <li><a href="/2022/10/10/refactoring-a-saga-from-the-state-pattern-to-the-state-monad">Refactoring a saga from the State pattern to the State monad</a></li> </ul> The first one uses the example from <em>Design Patterns</em>. That example is, unfortunately not that illuminating, since none of the book's methods return data. Thus, the endomorphism-based refactoring is enough, and you don't need the State monad. Therefore, another example is warranted. </p> <h3 id="1b5dead7f5c84f3896d639837314f17d"> Conclusion <a href="#1b5dead7f5c84f3896d639837314f17d" title="permalink">#</a> </h3> <p> You can view the State design pattern as the intersection of polymorphism and mutation. Both are object-oriented staples. The pattern uses polymorphism to model state, and mutation to change from one polymorphic state to another. </p> <p> In functional programming pure functions can't mutate state. You can often design around that problem, but if all else fails, the State monad offers a general-purpose alternative to both return a value and change object state. Thus, you can view the functional equivalent of the State pattern as the intersection of polymorphism and the State monad. </p> <p> <strong>Next:</strong> <a href="/2022/09/26/refactoring-the-tcp-state-pattern-example-to-pure-functions">Refactoring the TCP State pattern example to pure functions</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Natural transformations as invariant functors https://blog.ploeh.dk/2022/08/29/natural-transformations-as-invariant-functors 2022-08-29T06:12:00+00:00 Mark Seemann <div id="post"> <p> <em>An article (also) for object-oriented programmers.</em> </p> <ins datetime="2022-09-04T06:40Z"> <p> <strong>Update 2022-09-04:</strong> <em>This article is most likely partially incorrect. What it describes works, but may not be a natural transformation. See <a href="#9336107b26fd48a8971e617d8ebd5159">the below comment</a> for more details.</em> </p> </ins> <p> This article is part of <a href="/2022/08/01/invariant-functors">a series of articles about invariant functors</a>. An invariant functor is a <a href="/2018/03/22/functors">functor</a> that is neither covariant nor contravariant. See the series introduction for more details. The <a href="/2022/08/08/endomorphism-as-an-invariant-functor">previous article</a> described how you can view an <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a> as an invariant functor. This article generalises that result. </p> <h3 id="f3a2cab08b004fe2bfa8e4b307317d88"> Endomorphism as a natural transformation <a href="#f3a2cab08b004fe2bfa8e4b307317d88" title="permalink">#</a> </h3> <p> An endomorphism is a function whose <a href="https://en.wikipedia.org/wiki/Domain_of_a_function">domain</a> and <a href="https://en.wikipedia.org/wiki/Codomain">codomain</a> is the same. In C# you'd denote the type as <code>Func&lt;T, T&gt;</code>, in <a href="https://fsharp.org/">F#</a> as <code>'a -&gt; 'a</code>, and in <a href="https://www.haskell.org/">Haskell</a> as <code>a -&gt; a</code>. <code>T</code>, <code>'a</code>, and <code>a</code> all symbolise generic types - the notation is just different, depending on the language. </p> <p> A 'naked' value is isomorphic to <a href="/2018/09/03/the-identity-functor">the Identity functor</a>. You can wrap a value of the type <code>a</code> in <code>Identity a</code>, and if you have an <code>Identity a</code>, you can extract the <code>a</code> value. </p> <p> An endomorphism is thus isomorphic to a function from Identity to Identity. In C#, you might denote that as <code>Func&lt;Identity&lt;T&gt;, Identity&lt;T&gt;&gt;</code>, and in Haskell as <code>Identity a -&gt; Identity a</code>. </p> <p> In fact, you can lift any function to an Identity-valued function: </p> <p> <pre>Prelude Data.Functor.Identity&gt; :t \f -&gt; Identity . f . runIdentity \f -&gt; Identity . f . runIdentity :: (b -&gt; a) -&gt; Identity b -&gt; Identity a</pre> </p> <p> While this is a general result that allows <code>a</code> and <code>b</code> to differ, when <code>a ~ b</code> this describes an endomorphism. </p> <p> Since Identity is a functor, a function <code>Identity a -&gt; Identity a</code> is a <a href="/2022/07/18/natural-transformations">natural transformation</a>. </p> <p> The identity function (<code>id</code> in F# and Haskell; <code>x =&gt; x</code> in C#) is the only one possible entirely general endomorphism. You can use the <a href="https://hackage.haskell.org/package/natural-transformation">natural-transformation</a> package to make it explicit that this is a natural transformation: </p> <p> <pre><span style="color:#2b91af;">idNT</span>&nbsp;::&nbsp;<span style="color:blue;">Identity</span>&nbsp;:~&gt;&nbsp;<span style="color:blue;">Identity</span> idNT&nbsp;=&nbsp;NT&nbsp;$&nbsp;Identity&nbsp;.&nbsp;<span style="color:blue;">id</span>&nbsp;.&nbsp;runIdentity</pre> </p> <p> The point, so far, is that you can view an endomorphism as a natural transformation. </p> <p> Since an endomorphism forms an invariant functor, this suggests a promising line of inquiry. </p> <h3 id="88842564359c40978adb687e27cc460d"> Natural transformations as invariant functors <a href="#88842564359c40978adb687e27cc460d" title="permalink">#</a> </h3> <p> Are <em>all</em> natural transformations invariant functors? </p> <p> Yes, they are. In Haskell, you can implement it like this: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;(<span style="color:blue;">Functor</span>&nbsp;f,&nbsp;<span style="color:blue;">Functor</span>&nbsp;g)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Invariant</span>&nbsp;(<span style="color:blue;">NT</span>&nbsp;f&nbsp;g)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;invmap&nbsp;f&nbsp;g&nbsp;(NT&nbsp;h)&nbsp;=&nbsp;NT&nbsp;$&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;.&nbsp;h&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;g</pre> </p> <p> Here, I chose to define <code>NT</code> from scratch, rather than relying on the <em>natural-transformation</em> package. </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;NT&nbsp;f&nbsp;g&nbsp;a&nbsp;=&nbsp;NT&nbsp;{&nbsp;unNT&nbsp;::&nbsp;f&nbsp;a&nbsp;-&gt;&nbsp;g&nbsp;a&nbsp;}</pre> </p> <p> Notice how the implementation (<code><span style="color:blue;">fmap</span>&nbsp;f&nbsp;.&nbsp;h&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;g</code>) looks like a generalisation of the endomorphism implementation of <code>invmap</code> (<code>f . h . g</code>). Instead of pre-composing with <code>g</code>, the generalisation pre-composes with <code>fmap g</code>, and instead of post-composing with <code>f</code>, it post-composes with <code>fmap f</code>. </p> <p> Using the same kind of diagram as in the previous article, this composition now looks like this: </p> <p> <img src="/content/binary/invariant-natural-transformation-map-diagram.png" alt="Arrow diagram showing the mapping from a natural transformation in a to a natural transformation in b."> </p> <p> I've used thicker arrows to indicate that each one potentially involves 'more work'. Each is a mapping from a functor to a functor. For the <a href="/2022/04/19/the-list-monad">List functor</a>, for example, the arrow implies zero to many values being mapped. Thus, 'more data' moves 'through' each arrow, and for that reason I thought it made sense to depict them as being thicker. This 'more data' view is not always correct. For example, for <a href="/2018/03/26/the-maybe-functor">the Maybe functor</a>, the amount of data transported though each arrow is zero or one, which rather suggests a thinner arrow. For something like <a href="/2021/07/19/the-state-functor">the State functor</a> or <a href="/2021/08/30/the-reader-functor">the Reader functor</a>, there's really no <em>data</em> in the strictest sense moving through the arrows, but rather functions (which are also, however, a kind of data). Thus, don't take this metaphor of the thicker arrows literally. I did, however, wish to highlight that there's something 'more' going on. </p> <p> The diagram shows a natural transformation <code>h</code> from some functor <code>F</code> to another functor <code>G</code>. It transports objects of the type <code>a</code>. If <code>a</code> and <code>b</code> are isomorphic, you can map that natural transformation to one that transports objects of the type <code>b</code>. </p> <p> Compared to endomorphisms, where you need to, say, map <code>b</code> to <code>a</code>, you now need to map <code>F b</code> to <code>F a</code>. If <code>g</code> maps <code>b</code> to <code>a</code>, then <code>fmap g</code> maps <code>F b</code> to <code>F a</code>. The same line of argument applies to <code>fmap f</code>. </p> <p> In C# you can implement the same behaviour as follows. Assume that you have a natural transformation <code>H</code> from the functor <code>F</code> to the functor <code>G</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Func&lt;F&lt;A&gt;,&nbsp;G&lt;A&gt;&gt;&nbsp;H&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}</pre> </p> <p> You can now implement a non-standard <code>Select</code> overload (as described in the introductory article) that maps a natural transformation <code>FToG&lt;A&gt;</code> to a natural transformation <code>FToG&lt;B&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;FToG&lt;B&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">B</span>&gt;(Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">aToB</span>,&nbsp;Func&lt;B,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">bToA</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;FToG&lt;B&gt;(<span style="color:#1f377f;">fb</span>&nbsp;=&gt;&nbsp;H(fb.Select(bToA)).Select(aToB)); }</pre> </p> <p> The implementation looks more imperative than in Haskell, but the idea is the same. First it uses <code>Select</code> on <code>F</code> in order to translate <code>fb</code> (of the type <code>F&lt;B&gt;</code>) to an <code>F&lt;A&gt;</code>. It then uses <code>H</code> to transform the <code>F&lt;A&gt;</code> to an <code>G&lt;A&gt;</code>. Finally, now that it has a <code>G&lt;A&gt;</code>, it can use <code>Select</code> on <em>that</em> functor to map to a <code>G&lt;B&gt;</code>. </p> <p> Note that there's two different functors (<code>F</code> and <code>G</code>) in play, so the two <code>Select</code> methods are different. This is also true in the Haskell code. <code>fmap g</code> need not be the same as <code>fmap f</code>. </p> <h3 id="70c39c1999864a82a73a4ce22f252110"> Identity law <a href="#70c39c1999864a82a73a4ce22f252110" title="permalink">#</a> </h3> <p> As in the previous article, I'll set out to <em>prove</em> the two laws for invariant functors, starting with the identity law. Again, I'll use equational reasoning with <a href="https://bartoszmilewski.com/2015/01/20/functors/">the notation that Bartosz Milewski uses</a>. Here's the proof that the <code>invmap</code> instance obeys the identity law: </p> <p> <pre>&nbsp;&nbsp;invmap&nbsp;id&nbsp;id&nbsp;(NT&nbsp;h) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;invmap&nbsp;} &nbsp;&nbsp;NT&nbsp;$&nbsp;fmap&nbsp;id&nbsp;.&nbsp;h&nbsp;.&nbsp;fmap&nbsp;id =&nbsp;{&nbsp;first&nbsp;functor&nbsp;law&nbsp;} &nbsp;&nbsp;NT&nbsp;$&nbsp;id&nbsp;.&nbsp;h&nbsp;.&nbsp;id =&nbsp;{&nbsp;eta&nbsp;expansion&nbsp;} &nbsp;&nbsp;NT&nbsp;$&nbsp;(\x&nbsp;-&gt;&nbsp;(id&nbsp;.&nbsp;h&nbsp;.&nbsp;id)&nbsp;x) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;(.)&nbsp;} &nbsp;&nbsp;NT&nbsp;$&nbsp;(\x&nbsp;-&gt;&nbsp;id(h(id(x)))) =&nbsp;{&nbsp;defintion&nbsp;of&nbsp;id&nbsp;} &nbsp;&nbsp;NT&nbsp;$&nbsp;(\x&nbsp;-&gt;&nbsp;h(x)) =&nbsp;{&nbsp;eta&nbsp;reduction&nbsp;} &nbsp;&nbsp;NT&nbsp;h =&nbsp;{&nbsp;definition&nbsp;of&nbsp;id&nbsp;} &nbsp;&nbsp;id&nbsp;(NT&nbsp;h)</pre> </p> <p> I'll leave it here without further comment. The Haskell type system is so expressive and abstract that it makes little sense to try to translate these findings to C# or F# in the abstract. Instead, you'll see some more concrete examples later. </p> <h3 id="d54ff79951db44559583a200d20cf6d9"> Composition law <a href="#d54ff79951db44559583a200d20cf6d9" title="permalink">#</a> </h3> <p> As with the identity law, I'll offer a proof for the composition law for the Haskell instance: </p> <p> <pre>&nbsp;&nbsp;invmap&nbsp;f2&nbsp;f2&#39;&nbsp;$&nbsp;invmap&nbsp;f1&nbsp;f1&#39;&nbsp;(NT&nbsp;h) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;invmap&nbsp;} &nbsp;&nbsp;invmap&nbsp;f2&nbsp;f2&#39;&nbsp;$&nbsp;NT&nbsp;$&nbsp;fmap&nbsp;f1&nbsp;.&nbsp;h&nbsp;.&nbsp;fmap&nbsp;f1&#39; =&nbsp;{&nbsp;defintion&nbsp;of&nbsp;($)&nbsp;} &nbsp;&nbsp;invmap&nbsp;f2&nbsp;f2&#39;&nbsp;(NT&nbsp;(fmap&nbsp;f1&nbsp;.&nbsp;h&nbsp;.&nbsp;fmap&nbsp;f1&#39;)) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;invmap&nbsp;} &nbsp;&nbsp;NT&nbsp;$&nbsp;fmap&nbsp;f2&nbsp;.&nbsp;(fmap&nbsp;f1&nbsp;.&nbsp;h&nbsp;.&nbsp;fmap&nbsp;f1&#39;)&nbsp;.&nbsp;fmap&nbsp;f2&#39; =&nbsp;{&nbsp;associativity&nbsp;of&nbsp;composition&nbsp;(.)&nbsp;} &nbsp;&nbsp;NT&nbsp;$&nbsp;(fmap&nbsp;f2&nbsp;.&nbsp;fmap&nbsp;f1)&nbsp;.&nbsp;h&nbsp;.&nbsp;(fmap&nbsp;f1&#39;&nbsp;.&nbsp;fmap&nbsp;f2&#39;) =&nbsp;{&nbsp;second&nbsp;functor&nbsp;law&nbsp;} &nbsp;&nbsp;NT&nbsp;$&nbsp;fmap&nbsp;(f2&nbsp;.&nbsp;f1)&nbsp;.&nbsp;h&nbsp;.&nbsp;fmap&nbsp;(f1&#39;&nbsp;.&nbsp;f2&#39;) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;invmap&nbsp;} &nbsp;&nbsp;invmap&nbsp;(f2&nbsp;.&nbsp;f1)&nbsp;(f1&#39;&nbsp;.&nbsp;f2&#39;)&nbsp;(NT&nbsp;h)</pre> </p> <p> Unless I've made a mistake, these two proofs should demonstrate that all natural transformations can be turned into an invariant functor - in Haskell, at least, but I'll conjecture that that result carries over to other languages like F# and C# as long as one stays within the confines of <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> <h3 id="2afc7c35819c4d499f531a7229102404"> The State functor as a natural transformation <a href="#2afc7c35819c4d499f531a7229102404" title="permalink">#</a> </h3> <p> I'll be honest and admit that my motivation for embarking on this exegesis was because I'd come to the realisation that you can think about <a href="/2021/07/19/the-state-functor">the State functor</a> as a natural transformation. Recall that <code>State</code> is usually defined like this: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;State&nbsp;s&nbsp;a&nbsp;=&nbsp;State&nbsp;{&nbsp;runState&nbsp;::&nbsp;s&nbsp;-&gt;&nbsp;(a,&nbsp;s)&nbsp;}</pre> </p> <p> You can easily establish that this definition of <code>State</code> is isomorphic with a natural transformation from <a href="/2018/09/03/the-identity-functor">the Identity functor</a> to <a href="/2018/12/31/tuple-bifunctor">the tuple functor</a>: </p> <p> <pre><span style="color:#2b91af;">stateToNT</span>&nbsp;::&nbsp;<span style="color:blue;">State</span>&nbsp;s&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">NT</span>&nbsp;<span style="color:blue;">Identity</span>&nbsp;((,)&nbsp;a)&nbsp;s stateToNT&nbsp;(State&nbsp;h)&nbsp;=&nbsp;NT&nbsp;$&nbsp;h&nbsp;.&nbsp;runIdentity <span style="color:#2b91af;">ntToState</span>&nbsp;::&nbsp;<span style="color:blue;">NT</span>&nbsp;<span style="color:blue;">Identity</span>&nbsp;((,)&nbsp;a)&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">State</span>&nbsp;s&nbsp;a ntToState&nbsp;(NT&nbsp;h)&nbsp;=&nbsp;State&nbsp;$&nbsp;h&nbsp;.&nbsp;Identity</pre> </p> <p> Notice that this is a natural transformation in <code>s</code> - not in <code>a</code>. </p> <p> Since I've already established that natural transformations form invariant functors, this also applies to the State monad. </p> <h3 id="6c0025936a9949eca6191989f4d1b8c6"> State mapping <a href="#6c0025936a9949eca6191989f4d1b8c6" title="permalink">#</a> </h3> <p> My point with all of this isn't really to insist that anyone makes actual use of all this machinery, but rather that this line of reasoning helps to <em>identify a capability</em>. We now know that it's possible to translate a <code>State s a</code> value to a <code>State t a</code> value if <code>s</code> is isomorphic to <code>t</code>. </p> <p> As an example, imagine that you have some State-valued function that attempts to find the maximum value based on various criteria. Such a <code>pickMax</code> function may have the type <code><span style="color:blue;">State</span>&nbsp;(<span style="color:blue;">Max</span>&nbsp;<span style="color:#2b91af;">Integer</span>)&nbsp;<span style="color:#2b91af;">String</span></code> where the state type (<code><span style="color:blue;">Max</span>&nbsp;<span style="color:#2b91af;">Integer</span></code>) is used to keep track of the maximum value found while examining candidates. </p> <p> You could conceivably turn such a function around to instead look for the minimum by mapping the state to a <code>Min</code> value instead: </p> <p> <pre><span style="color:#2b91af;">pickMin</span>&nbsp;::&nbsp;<span style="color:blue;">State</span>&nbsp;(<span style="color:blue;">Min</span>&nbsp;<span style="color:#2b91af;">Integer</span>)&nbsp;<span style="color:#2b91af;">String</span> pickMin&nbsp;=&nbsp;ntToState&nbsp;$&nbsp;invmap&nbsp;(Min&nbsp;.&nbsp;getMax)&nbsp;(Max&nbsp;.&nbsp;getMin)&nbsp;$&nbsp;stateToNT&nbsp;pickMax</pre> </p> <p> You can use <code>getMax</code> to extract the underlying <code>Integer</code> from the <code><span style="color:blue;">Max</span>&nbsp;<span style="color:#2b91af;">Integer</span></code> and then <code>Min</code> to turn it into a <code><span style="color:blue;">Min</span>&nbsp;<span style="color:#2b91af;">Integer</span></code> value, and vice versa. <code><span style="color:blue;">Max</span>&nbsp;<span style="color:#2b91af;">Integer</span></code> and <code><span style="color:blue;">Min</span>&nbsp;<span style="color:#2b91af;">Integer</span></code> are isomorphic. </p> <p> In C#, you can implement a similar method. The code shown here extends the code shown in <a href="/2021/07/19/the-state-functor">The State functor</a>. I chose to call the method <code>SelectState</code> so as to not make things too confusing. The State functor already comes with a <code>Select</code> method that maps <code>T</code> to <code>T1</code> - that's the 'normal', covariant functor implementation. The new method is the invariant functor implementation that maps the state <code>S</code> to <code>S1</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S1,&nbsp;T&gt;&nbsp;<span style="color:#74531f;">SelectState</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">S1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">state</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S,&nbsp;S1&gt;&nbsp;<span style="color:#1f377f;">sToS1</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S1,&nbsp;S&gt;&nbsp;<span style="color:#1f377f;">s1ToS</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;InvariantStateMapper&lt;T,&nbsp;S,&nbsp;S1&gt;(state,&nbsp;sToS1,&nbsp;s1ToS); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InvariantStateMapper</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">S1</span>&gt;&nbsp;:&nbsp;IState&lt;S1,&nbsp;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;state; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;S,&nbsp;S1&gt;&nbsp;sToS1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;S1,&nbsp;S&gt;&nbsp;s1ToS; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">InvariantStateMapper</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">state</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S,&nbsp;S1&gt;&nbsp;<span style="color:#1f377f;">sToS1</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S1,&nbsp;S&gt;&nbsp;<span style="color:#1f377f;">s1ToS</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.state&nbsp;=&nbsp;state; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.sToS1&nbsp;=&nbsp;sToS1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.s1ToS&nbsp;=&nbsp;s1ToS; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;T,&nbsp;S1&gt;&nbsp;<span style="color:#74531f;">Run</span>(S1&nbsp;<span style="color:#1f377f;">s1</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;state.Run(s1ToS(s1)).Select(sToS1); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As usual when working in C# with interfaces instead of higher-order functions, <a href="/2019/12/16/zone-of-ceremony">there's some ceremony to be expected</a>. The only interesting line of code is the <code>Run</code> implementation. </p> <p> It starts by calling <code>s1ToS</code> in order to translate the <code>s1</code> parameter into an <code>S</code> value. This enables it to call <code>Run</code> on <code>state</code>. The result is a tuple with the type <code>Tuple&lt;T,&nbsp;S&gt;</code>. It's necessary to translate the <code>S</code> to <code>S1</code> with <code>sToS1</code>. You could do that by extracting the value from the tuple, mapping it, and returning a new tuple. Since a tuple gives rise to a functor (<a href="/2018/12/31/tuple-bifunctor">two, actually</a>) I instead used the <code>Select</code> method I'd already defined on it. </p> <p> Notice how similar the implementation is to the implementation of <a href="/2022/08/08/endomorphism-as-an-invariant-functor">the endomorphism invariant functor</a>. The only difference is that when translating back from <code>S</code> to <code>S1</code>, this happens inside a <code>Select</code> mapping. This is as predicted by the general implementation of invariant functors for natural transformations. </p> <p> In a future article, you'll see an example of <code>SelectState</code> in action. </p> <h3 id="5a148244488b4391be257181108072b3"> Other natural transformations <a href="#5a148244488b4391be257181108072b3" title="permalink">#</a> </h3> <p> As the <a href="/2022/07/18/natural-transformations">natural transformations</a> article outlines, there are infinitely many natural transformations. Each one gives rise to an invariant functor. </p> <p> It might be a good exercise to try to implement a few of them as invariant functors. If you want to do it in C#, you could, for example, start with the <em>safe head</em> natural transformation. </p> <p> If you want to stick to interfaces, you could define one like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ISafeHead</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;T&gt;&nbsp;<span style="color:#74531f;">TryFirst</span>(IEnumerable&lt;T&gt;&nbsp;<span style="color:#1f377f;">ts</span>); }</pre> </p> <p> The exercise is now to define and implement a method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;ISafeHead&lt;T1&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;ISafeHead&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">tToT1</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">t1ToT</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Implementation&nbsp;goes&nbsp;here...</span> }</pre> </p> <p> The implementation, once you get the handle of it, is entirely automatable. After all, in Haskell it's possible to do it once and for all, as shown above. </p> <h3 id="2c86c2f77e0a404da80858d855898843"> Conclusion <a href="#2c86c2f77e0a404da80858d855898843" title="permalink">#</a> </h3> <p> A natural transformation forms an invariant functor. This may not be the most exciting result ever, because invariant functors are limited in use. They only work when translating between types that are already isomorphic. Still, I did <a href="/2022/09/05/the-state-pattern-and-the-state-monad">find a use for this result</a> when I was working with the relationship between the State design pattern and the <a href="/2022/06/20/the-state-monad">State monad</a>. </p> <p> <strong>Next:</strong> <a href="/2022/12/26/functors-as-invariant-functors">Functors as invariant functors</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9336107b26fd48a8971e617d8ebd5159"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9336107b26fd48a8971e617d8ebd5159">#</a></div> <div class="comment-content"> <p> Due to feedback that I've received, I have to face evidence that this article may be partially incorrect. While I've added that proviso at the top of the article, I've decided to use a comment to expand on the issue. </p> <p> On Twitter, the user <a href="https://twitter.com/Savlambda">@Savlambda</a> (<em>borar</em>) argued that my <code>newtype</code> isn't a natural transformation: </p> <blockquote> <p> "The newtype 'NT' in the article is not a natural transformation though. Quantification over 'a' is at the "wrong place": it is not allowed for a client module to instantiate the container element type of a natural transformation." </p> <footer><cite><a href="https://twitter.com/Savlambda/status/1564175654845030400">@Savlambda</a></cite></footer> </blockquote> <p> While I engaged with the tweet, I have to admit that it took me a while to understand the core of the criticism. Of course I'm not happy about being wrong, but initially I genuinely didn't understand what was the problem. On the other hand, it's not the first time @Savlambda has provided valuable insights, so I knew it'd behove me to pay attention. </p> <p> After a few tweets back and forth, @Savlambda finally supplied a counter-argument that I understood. </p> <blockquote> <p> "This is not being overly pedantic. Here is one practical implication:" </p> <footer><cite><a href="https://twitter.com/Savlambda/status/1564970422981890048">@Savlambda</a></cite></footer> </blockquote> <p> The practical implication shown in the tweet is a screen shot (in order to get around Twitter's character limitation), but I'll reproduce it as code here in order to <a href="https://meta.stackoverflow.com/q/285551/126014">not show images of code</a>. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">(~&gt;)</span>&nbsp;f&nbsp;g&nbsp;=&nbsp;forall&nbsp;a.&nbsp;f&nbsp;a&nbsp;-&gt;&nbsp;g&nbsp;a <span style="color:green;">--&nbsp;Use&nbsp;the&nbsp;natural&nbsp;transformation&nbsp;twice,&nbsp;for&nbsp;different&nbsp;types </span><span style="color:#2b91af;">convertLists</span>&nbsp;::&nbsp;([]&nbsp;~&gt;&nbsp;g)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(g&nbsp;<span style="color:#2b91af;">Int</span>,&nbsp;g&nbsp;<span style="color:#2b91af;">Bool</span>) convertLists&nbsp;nt&nbsp;=&nbsp;(nt&nbsp;[1,2],&nbsp;nt&nbsp;[True]) <span style="color:blue;">newtype</span>&nbsp;NT&nbsp;f&nbsp;g&nbsp;a&nbsp;=&nbsp;NT&nbsp;(f&nbsp;a&nbsp;-&gt;&nbsp;g&nbsp;a) <span style="color:green;">--&nbsp;Does&nbsp;not&nbsp;type&nbsp;check,&nbsp;does&nbsp;not&nbsp;work;&nbsp;not&nbsp;a&nbsp;natural&nbsp;transformation </span><span style="color:#2b91af;">convertLists2</span>&nbsp;::&nbsp;<span style="color:blue;">NT</span>&nbsp;[]&nbsp;g&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(g&nbsp;<span style="color:#2b91af;">Int</span>,&nbsp;g&nbsp;<span style="color:#2b91af;">Bool</span>) convertLists2&nbsp;(NT&nbsp;f)&nbsp;=&nbsp;(f&nbsp;[1,2],&nbsp;f&nbsp;[True])</pre> </p> <p> I've moved the code comments to prevent horizontal scrolling, but otherwise tried to stay faithful to @Savlambda's screen shot. </p> <p> This was the example that finally hit the nail on the head for me. A natural transformation is a mapping from one functor (<code>f</code>) to another functor (<code>g</code>). I knew that already, but hadn't realised the implications. In Haskell (and other languages with <a href="https://en.wikipedia.org/wiki/Parametric_polymorphism">parametric polymorphism</a>) a <code>Functor</code> is defined for all <code>a</code>. </p> <p> A natural transformation is a higher level of abstraction, mapping one functor to another. That mapping must be defined for all <code>a</code>, and it must be <em>reusable</em>. The second example provided by @Savlambda demonstrates that the function wrapped by <code>NT</code> isn't reusable for different contained types. </p> <p> If you try to compile that example, GHC emits this compiler error: </p> <p> <pre>* Couldn't match type `a' with `Int' `a' is a rigid type variable bound by the type signature for: convertLists2 :: forall (g :: * -&gt; *) a. NT [] g a -&gt; (g Int, g Bool) Expected type: g Int Actual type: g a * In the expression: f [1, 2] In the expression: (f [1, 2], f [True]) In an equation for `convertLists2': convertLists2 (NT f) = (f [1, 2], f [True])</pre> </p> <p> Even though it's never fun to be proven wrong, I want to thank @Savlambda for educating me. One reason I write blog posts like this one is that writing is a way to learn. By writing about topics like these, I educate myself. Occasionally, it turns out that I make a mistake, and <a href="/2018/12/03/set-is-not-a-functor">this isn't the first time that's happened</a>. I also wish to apologise if this article has now left any readers more confused. </p> <p> A remaining question is what practical implications this has? Only rarely do you need a programming construct like <code>convertLists2</code>. On the other hand, had I wanted a function with the type <code>NT [] g Int -&gt; (g Int, g Int)</code>, it would have type-checked just fine. </p> <p> I'm not claiming that this is generally useful either, but I actually wrote this article because I <em>did</em> have use for the result that <code>NT</code> (whatever it is) is an invariant functor. As far as I can tell, that result still holds. </p> <p> I could be wrong about that, too. If you think so, please leave a comment. </p> </div> <div class="comment-date">2022-09-04 7:53 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Can types replace validation? https://blog.ploeh.dk/2022/08/22/can-types-replace-validation 2022-08-22T05:57:00+00:00 Mark Seemann <div id="post"> <p> <em>With some examples in C#.</em> </p> <p> In a comment to my article on <a href="/2022/08/15/aspnet-validation-revisited">ASP.NET validation revisited</a> Maurice Johnson asks: </p> <blockquote> <p> "I was just wondering, is it possible to use the type system to do the validation instead ? </p> <p> "What I mean is, for example, to make all the ReservationDto's field a type with validation in the constructor (like a class name, a class email, and so on). Normally, when the framework will build ReservationDto, it will try to construct the fields using the type constructor, and if there is an explicit error thrown during the construction, the framework will send us back the error with the provided message. </p> <p> "Plus, I think types like "email", "name" and "at" are reusable. And I feel like we have more possibilities for validation with that way of doing than with the validation attributes. </p> <p> "What do you think ?" </p> <footer><cite><a href="/2022/08/15/aspnet-validation-revisited#f6deac18851d47c3b066f82a8be3847d">Maurice Johnson</a></cite></footer> </blockquote> <p> I started writing a response below the question, but it grew and grew so I decided to turn it into a separate article. I think the question is of general interest. </p> <h3 id="2492a444e49b49a09a4f7c03f38b29d5"> The halting problem <a href="#2492a444e49b49a09a4f7c03f38b29d5" title="permalink">#</a> </h3> <p> I'm all in favour of using the type system for encapsulation, but there are limits to what it can do. We know this because it follows from the <a href="https://en.wikipedia.org/wiki/Halting_problem">halting problem</a>. </p> <p> I'm basing my understanding of the halting problem on <a href="https://www.goodreads.com/review/show/1731926050">my reading</a> of <a href="/ref/annotated-turing">The Annotated Turing</a>. In short, given an arbitrary computer program in a Turing-complete language, there's no general algorithm that will determine whether or not the program will finish running. </p> <p> A compiler that performs type-checking is a program, but typical type systems aren't Turing-complete. It's possible to write type checkers that always finish, because the 'programming language' they are running on - the type system - isn't Turing-complete. </p> <p> Normal type systems (like C#'s) aren't Turing-complete. You expect the C# compiler to always arrive at a result (either compiled code or error) in finite time. As a counter-example, consider <a href="https://www.haskell.org/">Haskell</a>'s type system. By default it, too, isn't Turing-complete, but with sufficient language extensions, you <em>can</em> make it Turing-complete. Here's a fun example: <a href="https://aphyr.com/posts/342-typing-the-technical-interview">Typing the technical interview</a> by Kyle Kingsbury (Aphyr). When you make the type system Turing-complete, however, termination is no longer guaranteed. A program may now compile forever or, practically, until it times out or runs out of memory. That's what happened to me when I tried to compile Kyle Kingsbury's code example. </p> <p> How is this relevant? </p> <p> This matters because understanding that a normal type system is <em>not</em> Turing-complete means that there are truths it <em>can't</em> express. Thus, we shouldn't be surprised if we run into rules or policies that we can't express with the type system we're given. What exactly is inexpressible depends on the type system. There are policies you can express in Haskell that are impossible to express in C#, and so on. Let's stick with C#, though. Here are some examples of rules that are practically inexpressible: </p> <ul> <li>An integer must be positive.</li> <li>A string must be at most 100 characters long.</li> <li>A maximum value must be greater than a minimum value.</li> <li>A value must be a valid email address.</li> </ul> <p> <a href="https://www.hillelwayne.com/">Hillel Wayne</a> provides more compelling examples in the article <a href="https://buttondown.email/hillelwayne/archive/making-illegal-states-unrepresentable/">Making Illegal States Unrepresentable</a>. </p> <h3 id="34f9d2e5570842adbb79a20972f458a0"> Encapsulation <a href="#34f9d2e5570842adbb79a20972f458a0" title="permalink">#</a> </h3> <p> Depending on how many times you've been around the block, you may find the above list naive. You may, for example, say that it's possible to express that an integer is positive like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span>&nbsp;:&nbsp;IEquatable&lt;NaturalNumber&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span>(<span style="color:blue;">int</span>&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(candidate&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(candidate), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;The&nbsp;value&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;(non-zero)&nbsp;number,&nbsp;but&nbsp;was:&nbsp;</span>{candidate}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.i&nbsp;=&nbsp;candidate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Various&nbsp;other&nbsp;members&nbsp;follow...</span></pre> </p> <p> I like introducing wrapper types like this. To the inexperienced developer this may seem redundant, but using a wrapper like this has several advantages. For one, it makes preconditions explicit. Consider a constructor like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;Guid&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;NaturalNumber&nbsp;quantity)</pre> </p> <p> What are the preconditions that you, as a client developer, has to fulfil before you can create a valid <code>Reservation</code> object? First, you must supply five arguments: <code>id</code>, <code>at</code>, <code>email</code>, <code>name</code>, and <code>quantity</code>. There is, however, more information than that. </p> <p> Consider, as an alternative, a constructor like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;Guid&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity)</pre> </p> <p> This constructor requires you to supply the same five arguments. There is, however, less explicit information available. If that was the only available constructor, you might be wondering: <em>Can I pass zero as <code>quantity</code>? Can I pass <code>-1</code>?</em> </p> <p> When the only constructor available is the first of these two alternatives, you already have the answer: No, the <code>quantity</code> must be a natural number. </p> <p> Another advantage of creating wrapper types like <code>NaturalNumber</code> is that you centralise run-time checks in one place. Instead of sprinkling defensive code all over the code base, you have it in one place. Any code that receives a <code>NaturalNumber</code> object knows that the check has already been performed. </p> <p> There's a word for this: <a href="/encapsulation-and-solid">Encapsulation</a>. </p> <p> You gather a coherent set of invariants and collect it in a single type, making sure that the type always guarantees its invariants. Note that <a href="/2022/10/24/encapsulation-in-functional-programming">this is an important design technique in functional programming</a> too. While you may not have to worry about state mutation preserving invariants, it's still important to guarantee that all values of a type are <em>valid</em>. </p> <h3 id="8c48a55a5db74686ab7a0c8fc40dd650"> Predicative and constructive data <a href="#8c48a55a5db74686ab7a0c8fc40dd650" title="permalink">#</a> </h3> <p> It's debatable whether the above <code>NaturalNumber</code> class <em>really</em> uses the type system to model what constitutes valid data. Since it relies on a run-time predicate, it falls in the category of types Hillel Wayne <a href="https://www.hillelwayne.com/post/constructive/">calls <em>predicative</em></a>. Such types are easy to create and compose well, but on the other hand fail to take full advantage of the type system. </p> <p> It's often worthwhile considering if a <em>constructive</em> design is possible and practical. In other words, is it possible to <a href="https://blog.janestreet.com/effective-ml-video/">make illegal states unrepresentable</a> (MISU)? </p> <p> What's wrong with <code>NaturalNumber</code>? Doesn't it do that? No, it doesn't, because this compiles: </p> <p> <pre><span style="color:blue;">new</span>&nbsp;NaturalNumber(-1)</pre> </p> <p> Surely it <em>will</em> fail at run time, but it compiles. Thus, it's <em>representable</em>. </p> <p> <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">The compiler gives you feedback faster than tests</a>. Considering MISU is worthwhile. </p> <p> Can we model natural numbers in a constructive way? Yes, with <a href="https://en.wikipedia.org/wiki/Peano_axioms">Peano numbers</a>. This is even <a href="/2018/05/28/church-encoded-natural-numbers">possible in C#</a>, but I wouldn't consider it practical. On the other hand, while it's possible to represent any natural number, there is <em>no way</em> to express -1 as a Peano number. </p> <p> As Hillel Wayne describes, constructive data types are much harder and requires a considerable measure of creativity. Often, a constructive model can seem impossible until you get a good idea. </p> <blockquote> <p> "a list can only be of even length. Most languages will not be able to express such a thing in a reasonable way in the data type." </p> <footer><cite><a href="https://note89.github.io/state-of-emergency/">Nils Eriksson</a></cite></footer> </blockquote> <p> Such a requirement may look difficult until inspiration hits. Then one day you may realise that it'd be as simple as a list of pairs (two-tuples). In Haskell, it could be as simple as this: </p> <p> <pre>newtype EvenList a = EvenList [(a,a)] deriving (Eq, Show)</pre> </p> <p> With such a constructive data model, lists of uneven length are unrepresentable. This is a simple example of the kind of creative thinking you may need to engage in with constructive data modelling. </p> <p> If you feel the need to object that Haskell isn't 'most languages', then here's the same idea expressed in C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">EvenCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;IEnumerable&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IEnumerable&lt;Tuple&lt;T,&nbsp;T&gt;&gt;&nbsp;values; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">EvenCollection</span>(IEnumerable&lt;Tuple&lt;T,&nbsp;T&gt;&gt;&nbsp;<span style="color:#1f377f;">values</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.values&nbsp;=&nbsp;values; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IEnumerator&lt;T&gt;&nbsp;<span style="color:#74531f;">GetEnumerator</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="color:#1f377f;">x</span>&nbsp;<span style="color:#8f08c4;">in</span>&nbsp;values) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;x.Item1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;x.Item2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;IEnumerator&nbsp;IEnumerable.<span style="color:#74531f;">GetEnumerator</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;GetEnumerator(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can create such a list like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">list</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;EvenCollection&lt;<span style="color:blue;">string</span>&gt;(<span style="color:blue;">new</span>[] { &nbsp;&nbsp;&nbsp;&nbsp;Tuple.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;Tuple.Create(<span style="color:#a31515;">&quot;baz&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>) });</pre> </p> <p> On the other hand, this doesn't compile: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">list</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;EvenCollection&lt;<span style="color:blue;">string</span>&gt;(<span style="color:blue;">new</span>[] { &nbsp;&nbsp;&nbsp;&nbsp;Tuple.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;Tuple.Create(<span style="color:#a31515;">&quot;baz&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;quux&quot;</span>) });</pre> </p> <p> Despite this digression, the point remains: Constructive data modelling may be impossible, unimagined, or impractical. </p> <p> Often, in languages like C# we resort to predicative data modelling. That's also what I did in the article <a href="/2022/08/15/aspnet-validation-revisited">ASP.NET validation revisited</a>. </p> <h3 id="830f3f7a663b459994cc585ef9604d11"> Validation as functions <a href="#830f3f7a663b459994cc585ef9604d11" title="permalink">#</a> </h3> <p> That was a long rambling detour inspired by a simple question: Is it possible to use types instead of validation? </p> <p> In order to address that question, it's only proper to explicitly state assumptions and definitions. What's the definition of <em>validation?</em> </p> <p> I'm not aware of a ubiquitous definition. While I could draw from <a href="https://en.wikipedia.org/wiki/Data_validation">the Wikipedia article on the topic</a>, at the time of writing it doesn't cite any sources when it sets out to define what it is. So I may as well paraphrase. It seems fair, though, to consider the stem of the word: <em>Valid</em>. </p> <p> Validation is the process of examining input to determine whether or not it's valid. I consider this a (mostly) self-contained operation: Given the data, is it well-formed and according to specification? If you have to query a database before making a decision, you're not validating the input. In that case, you're applying a business rule. As a rule of thumb I expect validations to be <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> <p> Validation, then, seems to imply a process. Before you execute the process, you don't know if data is valid. After executing the process, you do know. </p> <p> Data types, whether predicative like <code>NaturalNumber</code> or constructive like <code>EvenCollection&lt;T&gt;</code>, aren't processes or functions. They are results. </p> <p> <img src="/content/binary/validation-as-a-function-from-data-to-type.png" alt="An arrow labelled 'validation' pointing from a document to the left labelled 'Data' to a box to the right labelled 'Type'."> </p> <p> Sometimes an algorithm can use a type to <em>infer</em> the validation function. This is common in statically typed languages, from C# over <a href="https://fsharp.org/">F#</a> to Haskell (which are the languages with which I'm most familiar). </p> <h3 id="4cc324c1919e45958542bb82c2ca73d4"> Data Transfer Object as a validation DSL <a href="#4cc324c1919e45958542bb82c2ca73d4" title="permalink">#</a> </h3> <p> In a way you can think of the type system as a <a href="https://en.wikipedia.org/wiki/Domain-specific_language">domain-specific language</a> (DSL) for defining validation functions. It's not perfectly suited for that task, but often good enough that many developers reach for it. </p> <p> Consider the <code>ReservationDto</code> class from the <a href="/2022/08/15/aspnet-validation-revisited">ASP.NET validation revisited</a> article where I eventually gave up on it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;LinkDto[]?&nbsp;Links&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Guid?&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[Required,&nbsp;NotNull] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;DateTime?&nbsp;At&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[Required,&nbsp;NotNull] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[NaturalNumber] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> It actually tries to do what Maurice Johnson suggests. Particularly, it defines <code>At</code> as a <code>DateTime?</code> value. </p> <p> <pre>&gt; <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;{&nbsp;\&quot;At\&quot;:&nbsp;\&quot;2022-10-11T19:30\&quot;,&nbsp;\&quot;Email\&quot;:&nbsp;\&quot;z@example.com\&quot;,&nbsp;\&quot;Quantity\&quot;:&nbsp;1}&quot;</span>; &gt; JsonSerializer.Deserialize&lt;ReservationDto&gt;(json) ReservationDto { At=[11.10.2022 19:30:00], Email="z@example.com", Id=null, Name=null, Quantity=1 }</pre> </p> <p> A JSON deserializer like this one uses run-time reflection to examine the type in question and then maps the incoming data onto an instance. Many XML deserializers work the same way. </p> <p> What happens if you supply malformed input? </p> <p> <pre>&gt; <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;{&nbsp;\&quot;At\&quot;:&nbsp;\&quot;foo\&quot;,&nbsp;\&quot;Email\&quot;:&nbsp;\&quot;z@example.com\&quot;,&nbsp;\&quot;Quantity\&quot;:&nbsp;1}&quot;</span>; &gt; JsonSerializer.Deserialize&lt;ReservationDto&gt;(json) <span style="color:red">System.Text.Json.JsonException:↩ The JSON value could not be converted to System.Nullable`1[System.DateTime].↩ Path: $.At | LineNumber: 0 | BytePositionInLine: 26.↩ [...]</span></pre> </p> <p> (I've wrapped the result over multiple lines for readability. The <code>↩</code> symbol indicates where I've wrapped the text. I've also omitted a stack trace, indicated by <code>[...]</code>. I'll do that repeatedly throughout this article.) </p> <p> What happens if we try to define <code>ReservationDto.Quantity</code> with <code>NaturalNumber</code>? </p> <p> <pre>&gt; var json = "{ \"At\": \"2022-10-11T19:30\", \"Email\": \"z@example.com\", \"Quantity\": 1}"; &gt; JsonSerializer.Deserialize&lt;ReservationDto&gt;(json) <span style="color:red">System.Text.Json.JsonException:↩ The JSON value could not be converted to NaturalNumber.↩ Path: $.Quantity | LineNumber: 0 | BytePositionInLine: 67.↩ [...]</span></pre> </p> <p> While <a href="https://docs.microsoft.com/dotnet/api/system.text.json.jsonserializer">JsonSerializer</a> is a sophisticated piece of software, it's not so sophisticated that it can automatically map <code>1</code> to a <code>NaturalNumber</code> value. </p> <p> I'm sure that you can configure the behaviour with one or more <a href="https://docs.microsoft.com/dotnet/api/system.text.json.serialization.jsonconverter">JsonConverter</a> objects, but this is exactly the kind of framework <a href="https://en.wikipedia.org/wiki/Whac-A-Mole">Whack-a-mole</a> that I consider costly. It also suggests a wider problem. </p> <h3 id="bdad63c97ad44f1199f4f06e2b2e3fff"> Error handling <a href="#bdad63c97ad44f1199f4f06e2b2e3fff" title="permalink">#</a> </h3> <p> What happens if input to a validation function is malformed? You may want to report the errors to the caller, and you may want to report all errors in one go. Consider the user experience if you don't: A user types in a big form and submits it. The system informs him or her that there's an error in the third field. Okay, correct the error and submit again. Now there's an error in the fifth field, and so on. </p> <p> It's often better to return all errors as one collection. </p> <p> The problem is that type-based validation doesn't <em>compose</em> well. What do I mean by that? </p> <p> It's fairly clear that if you take a <em>simple</em> (i.e. non-complex) type like <code>NaturalNumber</code>, if you fail to initialize a value it's because the input is at fault: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;NaturalNumber(-1) <span style="color:red">System.ArgumentOutOfRangeException: The value must be a positive (non-zero) number, but was: -1.↩ (Parameter 'candidate') + NaturalNumber..ctor(int)</span></pre> </p> <p> The problem is that for complex types (i.e. types made from other types), exceptions short-circuit. As soon as one exception is thrown, further data validation stops. The <a href="/2022/08/15/aspnet-validation-revisited">ASP.NET validation revisited</a> article shows examples of that particular problem. </p> <p> This happens when validation functions have no composable way to communicate errors. When throwing exceptions, you can return an exception message, but exceptions short-circuit rather than compose. The same is true for the <a href="/2022/05/09/an-either-monad">Either monad</a>: It short-circuits. Once you're on <a href="https://fsharpforfunandprofit.com/posts/recipe-part2/">the failure track</a> you stay there and no further processing takes place. Errors don't compose. </p> <h3 id="7a85a7339fc24eda8e922ba12fdb3d89"> Monoidal versus applicative validation <a href="#7a85a7339fc24eda8e922ba12fdb3d89" title="permalink">#</a> </h3> <p> The naive take on validation is to answer the question: <em>Is that data valid or invalid?</em> Notice the binary nature of the question. It's either-or. </p> <p> This is true for both predicative data and constructive data. </p> <p> For constructive data, the question is: Is a candidate value representable? For example, can you represent <em>-1</em> as a Peano number? The answer is either yes or no; true or false. </p> <p> This is even clearer for predicative data, which is defined by a <em>predicate</em>. (Here's another <a href="/2021/09/09/the-specification-contravariant-functor">example of a natural number specification</a>.) A predicate is a function that returns a Boolean value: True or false. </p> <p> It's possible to compose Boolean values. The composition that we need in this case is Boolean <em>and</em>, which is also known as the <em>all</em> <a href="/2017/10/06/monoids">monoid</a>: If all values are <em>true</em>, the composed value is <em>true</em>; if just one value is <em>false</em>, the composed value is <em>false</em>. </p> <p> The problem is that during composition, we lose information. While a single <em>false</em> value causes the entire aggregated value to be <em>false</em>, we don't know why. And we don't know if there was only a single <em>false</em> value, or if there were more than one. Boolean <em>all</em> short-circuits on the first <em>false</em> value it encounters, and stops processing subsequent predicates. </p> <p> In logic, that's all you need, but in data validation you often want to know <em>what's wrong with the data</em>. </p> <p> Fortunately, this is <a href="/2020/12/14/validation-a-solved-problem">a solved problem</a>. Use <a href="/2018/11/05/applicative-validation">applicative validation</a>, an example of which I supplied in the article <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">An applicative reservation validation example in C#</a>. </p> <p> This changes focus on validation. No longer is validation a <em>true/false</em> question. Validation is a function from less-structured data to more-structured data. <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">Parse, don't validate</a>. </p> <h3 id="96d5b6c4df424f79b9eec8b186a2b3de"> Conclusion <a href="#96d5b6c4df424f79b9eec8b186a2b3de" title="permalink">#</a> </h3> <p> Can types replace validation? </p> <p> In some cases they can, but I think that the general answer is <em>no</em>. Granted, this answer is partially based on capabilities of current deserialisers. <a href="https://docs.microsoft.com/dotnet/api/system.text.json.jsonserializer.deserialize">JsonSerializer.Deserialize</a> short-circuits on the first error it encounters, and the same does <a href="https://hackage.haskell.org/package/aeson/docs/Data-Aeson.html">aeson</a>'s <a href="https://hackage.haskell.org/package/aeson/docs/Data-Aeson.html#v:eitherDecode">eitherDecode</a>. </p> <p> While that's the current state of affairs, it may not have to stay like that forever. One might be able to derive an applicative parser from a desired destination type, but I haven't seen that done yet. </p> <p> It sounds like a worthwhile research project. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4e4498fcf878443e96fad490569a8e4f"> <div class="comment-author"><a href="https://www.lloydatkinson.net">Lloyd Atkinson</a> <a href="#4e4498fcf878443e96fad490569a8e4f">#</a></div> <div class="comment-content"> <p> This slightly reminds me of <a href="https://github.com/colinhacks/zod">Zod</a> which is described as "TypeScript-first schema validation with static type inference". </p> <p> The library automatically infers a type that matches the validation - in a way it blurs this line between types and validation by making them become one. </p> <p> Of course, once you have that infered type there is nothing stopping you using it without the library, but that's something code reviews could catch. It's quite interesting though. </p> <pre> <code> import { z } from 'zod'; const User = z.object({ username: z.string(), age: z.number().positive({ message: 'Your age must be positive!', }), }); User.parse({ username: 'Ludwig', age: -1 }); // extract the inferred type type User = z.infer&lt;typeof User&gt;; // { username: string, age: number } </code> </pre> </div> <div class="comment-date">2022-08-28 00:53 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. ASP.NET validation revisited https://blog.ploeh.dk/2022/08/15/aspnet-validation-revisited 2022-08-15T05:48:00+00:00 Mark Seemann <div id="post"> <p> <em>Is the built-in validation framework better than applicative validation?</em> </p> <p> I recently published an article called <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">An applicative reservation validation example in C#</a> in which I describe how to use the universal abstractions of <a href="/2018/10/01/applicative-functors">applicative functors</a> and <a href="/2017/11/27/semigroups">semigroups</a> to implement reusable, composable validation. </p> <p> One reader reaction made me stop and think: </p> <blockquote> <p> "An exercise on how to reject 90% of the framework's existing services (*Validation) only to re implement them more poorly, by renouncing standardization, interoperability and globalization all for the glory of FP." </p> <footer><cite><a href="https://twitter.com/PopCatalin/status/1551478523981881349">PopCatalin</a></cite></footer> </blockquote> <p> (At the time of posting, the <a href="https://twitter.com/PopCatalin">PopCatalin Twitter account</a>'s display name was <em>Prime minister of truth™ カタリンポップ🇺🇦</em>, which I find unhelpful. The linked <a href="https://github.com/popcatalin81">GitHub account</a> locates the user in <a href="https://en.wikipedia.org/wiki/Cluj-Napoca">Cluj-Napoca</a>, a city I've <a href="/schedule">repeatedly visited for conferences</a> - the last time as recent as June 2022. I wouldn't be surprised if we've interacted, but if so, I'm sorry to say that I can't connect these accounts with one of the many wonderful people I've met there. In general, I'm getting a strong sarcastic vibe from that account, and I'm not sure whether or not to take <em>Pronouns kucf/fof</em> seriously. As the possibly clueless 51-year white male that I am, I will proceed with good intentions and to the best of my abilities.) </p> <p> That reply is an important reminder that I should once in a while check my assumptions. I'm aware that the ASP.NET framework comes with validation features, but I many years ago dismissed them because I found them inadequate. Perhaps, in the meantime, these built-in services have improved to the point that they are to be preferred over <a href="/2018/11/05/applicative-validation">applicative validation</a>. </p> <p> I decided to attempt to refactor the code to take advantage of the built-in ASP.NET validation to be able to compare the two approaches. This article is an experience report. </p> <h3 id="d4b2e6bdae494d0397866f683ddd64e9"> Requirements <a href="#d4b2e6bdae494d0397866f683ddd64e9" title="permalink">#</a> </h3> <p> In order to compare the two approaches, the ASP.NET-based validation should support the same validation features as the applicative validation example: </p> <ul> <li>The <code>At</code> property is required and should be a valid date and time. If it isn't, the validation message should report the problem and the offending input.</li> <li>The <code>Email</code> property should be required. If it's missing, the validation message should state so.</li> <li>The <code>Quantity</code> property is required and should be a natural number. If it isn't, the validation message should report the problem and the offending input.</li> </ul> <p> The <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">previous article</a> includes an interaction example that I'll repeat here for convenience: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 Content-Type: application/json {&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;large&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;-1&nbsp;} HTTP/1.1 400 Bad Request Invalid date or time: large. Email address is missing. Quantity must be a positive integer, but was: -1.</pre> </p> <p> ASP.NET validation formats the errors differently, as you'll see later in this article. That's not much of a concern, though: <a href="/2014/12/23/exception-messages-are-for-programmers">Error <em>messages</em> are for other developers</a>. They don't really have to be machine-readable or have a strict shape (as opposed to error <em>types</em>, which should be machine-readable). </p> <p> Reporting the offending values, as in <em>"Quantity must be a positive integer, but was: -1."</em> is part of the requirements. A REST API can make no assumptions about its clients. Perhaps one client is an unattended batch job that only logs errors. Logging offending values may be helpful to maintenance developers of such a batch job. </p> <h3 id="74d95aed3a5a40eb8f32ace05b0410a0"> Framework API <a href="#74d95aed3a5a40eb8f32ace05b0410a0" title="permalink">#</a> </h3> <p> The first observation to make about the ASP.NET validation API is that it's specific to ASP.NET. It's not a general-purpose API that you can use for other purposes. </p> <p> If, instead, you need to validate input to a console application, a background message handler, a batch job, or a desktop or phone app, you can't use that API. </p> <p> Perhaps each of these styles of software come with their own validation APIs, but even if so, that's a different API you'll have to learn. And in cases where there's no built-in validation API, then what do you do? </p> <p> The beauty and practicality of applicative validation is that it's <em>universal</em>. Since it's based on mathematical foundations, it's not tied to a particular framework, platform, or language. These concepts exist independently of technology. Once you understand the concepts, they're always there for you. </p> <p> The code example from the previous article, as well as here, build upon the code base that accompanies <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. An example code base has to be written in <em>some</em> language, and I chose C# because I'm more familiar with it than I am with <a href="https://www.java.com/">Java</a>, <a href="https://isocpp.org/">C++</a>, or <a href="https://www.typescriptlang.org/">TypeScript</a>. While I wanted the code base to be realistic, I tried hard to include only coding techniques and patterns that you could use in more than one language. </p> <p> As I wrote the book, I ran into many interesting problems and solutions that were specific to C# and ASP.NET. While I found them too specific to include in the book, I wrote <a href="/2021/06/14/new-book-code-that-fits-in-your-head">a series of blog posts</a> about them. This article is now becoming one of those. </p> <p> The point about the previous article on <a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">applicative reservation validation in C#</a> was to demonstrate how the general technique works. Not specifically in ASP.NET, or even C#, but in general. </p> <p> It just so happens that this example is situated in a context where an alternative solution presents itself. This is not always the case. Sometimes you have to solve this problem yourself, and when this happens, it's useful to know that <a href="/2020/12/14/validation-a-solved-problem">validation is a solved problem</a>. Even so, while a universal solution exists, it doesn't follow that the universal solution is the best. Perhaps there are specialised solutions that are better, each within their constrained contexts. </p> <p> Perhaps ASP.NET validation is an example of that. </p> <h3 id="0b80f4aa36954378be9278761b2f27ba"> Email validation <a href="#0b80f4aa36954378be9278761b2f27ba" title="permalink">#</a> </h3> <p> The following is a report on my experience refactoring validation to use the built-in ASP.NET validation API. </p> <p> I decided to start with the <code>Email</code> property, since the only requirement is that this value should be present. That seemed like an easy way to get started. </p> <p> I added the <a href="https://docs.microsoft.com/dotnet/api/system.componentmodel.dataannotations.requiredattribute">[Required]</a> attribute to the <code>ReservationDto</code> class' <code>Email</code> property. Since this code base also uses <a href="https://docs.microsoft.com/dotnet/csharp/nullable-references">nullable reference types</a>, it was necessary to also annotate the property with the <a href="https://docs.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.notnullattribute">[NotNull]</a> attribute: </p> <p> <pre>[Required,&nbsp;NotNull] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;}</pre> </p> <p> That's not too difficult, and seems to be working satisfactorily: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 > content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-11-21&nbsp;19:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 } HTTP/1.1 400 Bad Request Content-Type: application/problem+json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;type&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;title&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;One&nbsp;or&nbsp;more&nbsp;validation&nbsp;errors&nbsp;occurred.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;status&quot;</span>:&nbsp;400, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;traceId&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;|552ab5ff-494e1d1a9d4c6355.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;errors&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;Email&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;Email&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;]&nbsp;} }</pre> </p> <p> As discussed above, the response body is formatted differently than in the applicative validation example, but I consider that inconsequential for the reasons I gave. </p> <p> So far, so good. </p> <h3 id="a10d6da09d064a96bc0022e9657b62e5"> Quantity validation <a href="#a10d6da09d064a96bc0022e9657b62e5" title="permalink">#</a> </h3> <p> The next property I decided to migrate was <code>Quantity</code>. This must be a natural number; that is, an integer greater than zero. </p> <p> Disappointingly, no such built-in validation attribute seems to exist. One <a href="https://stackoverflow.com/a/7419330/126014">highly voted Stack Overflow answer</a> suggested using the <a href="https://docs.microsoft.com/dotnet/api/system.componentmodel.dataannotations.rangeattribute">[Range]</a> attribute, so I tried that: </p> <p> <pre>[Range(1,&nbsp;<span style="color:blue;">int</span>.MaxValue,&nbsp;ErrorMessage&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Quantity&nbsp;must&nbsp;be&nbsp;a&nbsp;natural&nbsp;number.&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;}</pre> </p> <p> As a <em>declarative</em> approach to validation goes, I don't think this is off to a good start. I like declarative programming, but I'd prefer to be able to declare that <code>Quantity</code> must be a <em>natural number</em>, rather than in the range of <code>1</code> and <code>int.MaxValue</code>. </p> <p> Does it work, though? </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-11-21&nbsp;19:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;0 } HTTP/1.1 400 Bad Request Content-Type: application/problem+json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;type&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;title&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;One&nbsp;or&nbsp;more&nbsp;validation&nbsp;errors&nbsp;occurred.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;status&quot;</span>:&nbsp;400, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;traceId&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;|d9a6be38-4be82ede7c525913.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;errors&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Email&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;Email&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Quantity&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;Quantity&nbsp;must&nbsp;be&nbsp;a&nbsp;natural&nbsp;number.&quot;</span>&nbsp;] &nbsp;&nbsp;} }</pre> </p> <p> While it does capture the intent that <code>Quantity</code> must be one or greater, it fails to echo back the offending value. </p> <p> In order to address that concern, I tried reading the documentation to find a way forward. Instead I found this: </p> <blockquote> <p> "Internally, the attributes call <a href="https://docs.microsoft.com/en-us/dotnet/api/system.string.format">String.Format</a> with a placeholder for the field name and sometimes additional placeholders. [...]" </p> <p> "To find out which parameters are passed to <code>String.Format</code> for a particular attribute's error message, see the <a href="https://github.com/dotnet/runtime/tree/main/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations">DataAnnotations source code</a>." </p> <footer><cite><a href="https://docs.microsoft.com/aspnet/core/mvc/models/validation">ASP.NET validation documentation</a></cite></footer> </blockquote> <p> Really?! </p> <p> If you have to read implementation code, <a href="/encapsulation-and-solid">encapsulation</a> is broken. </p> <p> Hardly impressed, I nonetheless found <a href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/RangeAttribute.cs">the RangeAttribute source code</a>. Alas, it only passes the property <code>name</code>, <code>Minimum</code>, and <code>Maximum</code> to <code>string.Format</code>, but not the offending value: </p> <p> <pre>return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, Minimum, Maximum);</pre> </p> <p> This looked like a dead end, but at least it's possible to extend the ASP.NET validation API: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NaturalNumberAttribute</span>&nbsp;:&nbsp;ValidationAttribute { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;ValidationResult&nbsp;IsValid( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>&nbsp;value, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ValidationContext&nbsp;validationContext) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(validationContext&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(validationContext)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;i&nbsp;=&nbsp;value&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:blue;">int</span>?; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(i.HasValue&nbsp;&amp;&amp;&nbsp;0&nbsp;&lt;&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ValidationResult.Success; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ValidationResult( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;</span>{validationContext.MemberName}<span style="color:#a31515;">&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;integer,&nbsp;but&nbsp;was:&nbsp;</span>{value}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Adding this <code>NaturalNumberAttribute</code> class enabled me to change the annotation of the <code>Quantity</code> property: </p> <p> <pre>[NaturalNumber] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;}</pre> </p> <p> This seems to get the job done: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-11-21&nbsp;19:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;0 } HTTP/1.1 400 Bad Request Content-Type: application/problem+json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;type&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;title&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;One&nbsp;or&nbsp;more&nbsp;validation&nbsp;errors&nbsp;occurred.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;status&quot;</span>:&nbsp;400, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;traceId&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;|bb45b60d-4bd255194871157d.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;errors&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Email&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;Email&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Quantity&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;Quantity&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;integer,&nbsp;but&nbsp;was:&nbsp;0.&quot;</span>&nbsp;] &nbsp;&nbsp;} }</pre> </p> <p> The <code>[NaturalNumber]</code> attribute now correctly reports the offending value together with a useful error message. </p> <p> Compare, however, the above <code>NaturalNumberAttribute</code> class to the <code>TryParseQuantity</code> function, repeated here for convenience: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;Validated&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">TryParseQuantity</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Quantity&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Quantity&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;integer,&nbsp;but&nbsp;was:&nbsp;</span>{Quantity}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(Quantity); }</pre> </p> <p> <code>TryParseQuantity</code> is shorter and has half the <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of <code>NaturalNumberAttribute</code>. In isolation, at least, I'd prefer the shorter, simpler alternative. </p> <h3 id="3c90c9ded0af42fd9017593c9a387e97"> Date and time validation <a href="#3c90c9ded0af42fd9017593c9a387e97" title="permalink">#</a> </h3> <p> Remaining is validation of the <code>At</code> property. As a first step, I converted the property to a <code>DateTime</code> value and added attributes: </p> <p> <pre>[Required,&nbsp;NotNull] <span style="color:blue;">public</span>&nbsp;DateTime?&nbsp;At&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;}</pre> </p> <p> I'd been a little apprehensive doing that, fearing that it'd break a lot of code (particularly tests), but that turned out not to be the case. In fact, it actually simplified a few of the tests. </p> <p> On the other hand, this doesn't really work as required: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-11-21&nbsp;19:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;0 } HTTP/1.1 400 Bad Request Content-Type: application/problem+json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;type&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;title&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;One&nbsp;or&nbsp;more&nbsp;validation&nbsp;errors&nbsp;occurred.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;status&quot;</span>:&nbsp;400, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;traceId&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;|1e1d600e-4098fb36635642f6.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;errors&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;dto&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;dto&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;$.at&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;JSON&nbsp;value&nbsp;could&nbsp;not&nbsp;be&nbsp;converted&nbsp;to&nbsp;System.Nullable`1[System.DateTime].↩ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path:&nbsp;$.at&nbsp;|&nbsp;LineNumber:&nbsp;0&nbsp;|&nbsp;BytePositionInLine:&nbsp;26.&quot;</span>&nbsp;] &nbsp;&nbsp;} }</pre> </p> <p> (I've wrapped the last error message over two lines for readability. The <code>↩</code> symbol indicates where I've wrapped the text.) </p> <p> There are several problems with this response. First, in addition to complaining about the missing <code>at</code> property, it should also have reported that there are problems with the <code>Quantity</code> and that the <code>Email</code> property is missing. Instead, the response implies that the <code>dto</code> field is missing. That's likely confusing to client developers, because <code>dto</code> is an implementation detail; it's the name of the C# parameter of the method that handles the request. Client developers can't and shouldn't know this. Instead, it looks as though the REST API somehow failed to receive the JSON document that the client posted. </p> <p> Second, the error message exposes other implementation details, here that the <code>at</code> field has the type <code>System.Nullable`1[System.DateTime]</code>. This is, at best, irrelevant. At worst, it could be a security issue, because it reveals to a would-be attacker that the system is implemented on .NET. </p> <p> Third, the framework rejects what looks like a perfectly good date and time: <code>2022-11-21 19:00</code>. This is a breaking change, since the API used to accept such values. </p> <p> What's wrong with <code>2022-11-21 19:00</code>? It's not a valid <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> string. According to the ISO 8601 standard, the date and time must be separated by <code>T</code>: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-11-21T19:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;0 } HTTP/1.1 400 Bad Request Content-Type: application/problem+json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;type&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;title&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;One&nbsp;or&nbsp;more&nbsp;validation&nbsp;errors&nbsp;occurred.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;status&quot;</span>:&nbsp;400, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;traceId&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;|1e1d600f-4098fb36635642f6.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;errors&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Email&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;Email&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Quantity&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;Quantity&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;integer,&nbsp;but&nbsp;was:&nbsp;0.&quot;</span>&nbsp;] &nbsp;&nbsp;} }</pre> </p> <p> Posting a valid ISO 8601 string does, indeed, enable the client to proceed - only to receive a new set of error messages. After I converted <code>At</code> to <code>DateTime?</code>, the ASP.NET validation framework fails to collect and report all errors. Instead it stops if it can't parse the <code>At</code> property. It doesn't report any other errors that might also be present. </p> <p> That is exactly the requirement that applicative validation so elegantly solves. </p> <h3 id="e9986e66f63c476ca529788afaa863e4"> Tolerant Reader <a href="#e9986e66f63c476ca529788afaa863e4" title="permalink">#</a> </h3> <p> While it's true that <code>2022-11-21 19:00</code> isn't valid ISO 8601, it's unambiguous. According to <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> an API should be a <a href="https://martinfowler.com/bliki/TolerantReader.html">Tolerant Reader</a>. It's not. </p> <p> This problem, however, is solvable. First, add the Tolerant Reader: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DateTimeConverter</span>&nbsp;:&nbsp;JsonConverter&lt;DateTime&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;DateTime&nbsp;Read( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">ref</span>&nbsp;Utf8JsonReader&nbsp;reader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type&nbsp;typeToConvert, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JsonSerializerOptions&nbsp;options) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;DateTime.Parse( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.GetString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CultureInfo.InvariantCulture); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Write( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Utf8JsonWriter&nbsp;writer, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;value, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JsonSerializerOptions&nbsp;options) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(writer&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(writer)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.WriteStringValue(value.ToString(<span style="color:#a31515;">&quot;s&quot;</span>)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Then add it to the JSON serialiser's <a href="https://docs.microsoft.com/dotnet/api/system.text.json.jsonserializeroptions.converters">Converters</a>: </p> <p> <pre>opts.JsonSerializerOptions.Converters.Add(<span style="color:blue;">new</span>&nbsp;DateTimeConverter());</pre> </p> <p> This, at least, addresses the Tolerant Reader concern: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-11-21&nbsp;19:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;0 } HTTP/1.1 400 Bad Request Content-Type: application/problem+json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;type&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;title&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;One&nbsp;or&nbsp;more&nbsp;validation&nbsp;errors&nbsp;occurred.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;status&quot;</span>:&nbsp;400, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;traceId&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;|11576943-400dafd4b489c282.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;errors&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Email&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;Email&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Quantity&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;Quantity&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;integer,&nbsp;but&nbsp;was:&nbsp;0.&quot;</span>&nbsp;] &nbsp;&nbsp;} }</pre> </p> <p> The API now accepts the slightly malformed <code>at</code> field. It also correctly handles if the field is entirely missing: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;0 } HTTP/1.1 400 Bad Request Content-Type: application/problem+json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;type&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;title&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;One&nbsp;or&nbsp;more&nbsp;validation&nbsp;errors&nbsp;occurred.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;status&quot;</span>:&nbsp;400, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;traceId&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;|11576944-400dafd4b489c282.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;errors&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;At&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;At&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Email&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;Email&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Quantity&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;Quantity&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;integer,&nbsp;but&nbsp;was:&nbsp;0.&quot;</span>&nbsp;] &nbsp;&nbsp;} }</pre> </p> <p> On the other hand, it <em>still</em> doesn't gracefully handle the case when the <code>at</code> field is unrecoverably malformed: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;0 } HTTP/1.1 400 Bad Request Content-Type: application/problem+json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;type&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;title&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;One&nbsp;or&nbsp;more&nbsp;validation&nbsp;errors&nbsp;occurred.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;status&quot;</span>:&nbsp;400, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;traceId&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;|11576945-400dafd4b489c282.&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;errors&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;supplied&nbsp;value&nbsp;is&nbsp;invalid.&quot;</span>&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;dto&quot;</span>:&nbsp;[&nbsp;<span style="color:#a31515;">&quot;The&nbsp;dto&nbsp;field&nbsp;is&nbsp;required.&quot;</span>&nbsp;] &nbsp;&nbsp;} }</pre> </p> <p> <code>The supplied value is invalid.</code> and <code>The dto field is required.</code>? That's not really helpful. And what happened to <code>The Email field is required.</code> and <code>Quantity must be a positive integer, but was: 0.</code>? </p> <p> If there's a way to address this problem, I don't know how. I've tried adding another custom attribute, similar to the above <code>NaturalNumberAttribute</code> class, but that doesn't solve it - probably because the model binder (that deserialises the JSON document to a <code>ReservationDto</code> instance) runs before the validation. </p> <p> Perhaps there's a way to address this problem with yet another class that derives from a base class, but I think that I've already played enough <a href="https://en.wikipedia.org/wiki/Whac-A-Mole">Whack-a-mole</a> to arrive at a conclusion. </p> <h3 id="fc3e2119ed1c4a7a8cff0c0992a8b071"> Conclusion <a href="#fc3e2119ed1c4a7a8cff0c0992a8b071" title="permalink">#</a> </h3> <p> Your context may differ from mine, so the conclusion that I arrive at may not apply in your situation. For example, I'm given to understand that one benefit that the ASP.NET validation framework provides is that when used with ASP.NET MVC (instead of as a Web API), (some of) the validation logic can also run in <a href="https://www.javascript.com/">JavaScript</a> in browsers. This, ostensibly, reduces code duplication. </p> <blockquote> <p> "Yet in the case of validation, a Declarative model is far superior to a FP one. The declarative model allows various environments to implement validation as they need it (IE: Client side validation) while the FP one is strictly limited to the environment executing the code." </p> <footer><cite><a href="https://twitter.com/PopCatalin/status/1551478926005911553">PopCatalin</a></cite></footer> </blockquote> <p> On the other hand, using the ASP.NET validation framework requires more code, and more complex code, than with applicative validation. It's a particular set of APIs that you have to learn, and that knowledge doesn't transfer to other frameworks, platforms, or languages. </p> <p> Apart from client-side validation, I fail to see how applicative validation <em>"re implement[s validation] more poorly, by renouncing standardization, interoperability and globalization"</em>. </p> <p> I'm not aware that there's any <em>standard</em> for validation as such, so I think that @PopCatalin has the 'standard' ASP.NET validation API in mind. If so, I consider applicative validation a much more standardised solution than a specialised API. </p> <p> If by <em>interoperability</em> @PopCatalin means the transfer of logic from server side to client side, then it's true that the applicative validation I showed in the previous article runs exclusively on the server. I wonder, however, how much of such custom validation as <code>NaturalNumberAttribute</code> automatically transfers to the client side. </p> <p> When it comes to globalisation, I fail to see how applicative validation is less globalisable than the ASP.NET validation framework. One could easily replace the hard-coded strings in my examples with resource strings. </p> <p> It would seem, again, that <a href="https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule">any sufficiently complicated custom validation framework contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of applicative validation</a>. </p> <blockquote> <p> "I must admit I really liked the declarative OOP model using annotations when I first saw it in Java (EJB3.0, almost 20yrs ago) until I saw FP way of doing things. FP way is so much simpler and powerful, because it's just function composition, nothing more, no hidden "magic"." </p> <footer><cite><a href="https://twitter.com/witoldsz/status/1552429555503493120">Witold Szczerba</a></cite></footer> </blockquote> <p> I still find myself in the same camp as Witold Szczerba. It's easy to get started using validation annotations, but it doesn't follow that it's simpler or better in the long run. As <a href="https://en.wikipedia.org/wiki/Rich_Hickey">Rich Hickey</a> points out in <a href="https://www.infoq.com/presentations/Simple-Made-Easy/">Simple Made Easy</a>, <em>simple</em> and <em>easy</em> isn't the same. If I have to maintain code, I'll usually choose the simple solution over the easy solution. That means choosing applicative validation over a framework-specific validation API. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f6deac18851d47c3b066f82a8be3847d"> <div class="comment-author">Maurice Johnson <a href="#f6deac18851d47c3b066f82a8be3847d">#</a></div> <div class="comment-content"> <p> Hello Mark. I was just wondering, is it possible to use the type system to do the validation instead ? </p> <p> What I mean is, for example, to make all the ReservationDto's field a type with validation in the constructor (like a class name, a class email, and so on). Normally, when the framework will build ReservationDto, it will try to construct the fields using the type constructor, and if there is an explicit error thrown during the construction, the framework will send us back the error with the provided message. </p> <p> Plus, I think types like "email", "name" and "at" are reusable. And I feel like we have more possibilities for validation with that way of doing than with the validation attributes. </p> <p> What do you think ? </p> <p> Regards. </p> </div> <div class="comment-date">2022-08-16 08:30 UTC</div> </div> <div class="comment" id="89a9ef6d57f645d7a1e848aaf20b8b72"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#89a9ef6d57f645d7a1e848aaf20b8b72">#</a></div> <div class="comment-content"> <p> Maurice, thank you for writing. I started writing a reply, but it grew, so I'm going to turn it into a blog post. I'll post an update here once I've published it, but expect it to take a few weeks. </p> </div> <div class="comment-date">2022-08-18 7:50 UTC</div> </div> <div class="comment" id="e932eb2fd0804a049314872d5fe6a358"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e932eb2fd0804a049314872d5fe6a358">#</a></div> <div class="comment-content"> <p> I've published the article: <a href="/2022/08/22/can-types-replace-validation">Can types replace validation?</a>. </p> </div> <div class="comment-date">2022-08-22 6:00 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Endomorphism as an invariant functor https://blog.ploeh.dk/2022/08/08/endomorphism-as-an-invariant-functor 2022-08-08T04:43:00+00:00 Mark Seemann <div id="post"> <p> <em>An article (also) for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2022/08/01/invariant-functors">a series of articles about invariant functors</a>. An invariant functor is a <a href="/2018/03/22/functors">functor</a> that is neither covariant nor contravariant. See the series introduction for more details. </p> <p> An <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a> is a function where the return type is the same as the input type. </p> <p> In <a href="https://www.haskell.org/">Haskell</a> we denote an endomorphism as <code>a -&gt; a</code>, in <a href="http://fsharp.org/">F#</a> we have to add an apostrophe: <code>'a -&gt; 'a</code>, while in C# such a function corresponds to the delegate <code>Func&lt;T, T&gt;</code> or, alternatively, to a method that has the same return type as input type. </p> <p> In Haskell you can treat an endomorphism like a <a href="/2017/10/06/monoids">monoid</a> by wrapping it in a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> called <a href="https://hackage.haskell.org/package/base/docs/Data-Monoid.html#t:Endo">Endo</a>: <code>Endo a</code>. In C#, we <a href="/2021/05/31/from-state-tennis-to-endomorphism">might model it as an interface</a> called <code><span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code>. </p> <p> That looks enough like a functor that you might wonder if it is one, but it turns out that it's neither co- nor contravariant. You can deduce this with positional variance analysis (which I've learned from <a href="https://thinkingwithtypes.com/">Thinking with Types</a>). In short, this is because <code>T</code> appears as both input and output - it's neither co- nor contravariant, but rather <em>invariant</em>. </p> <h3 id="39fa705060f143aba4c18dd2c8ff7f2d"> Explicit endomorphism interface in C# <a href="#39fa705060f143aba4c18dd2c8ff7f2d" title="permalink">#</a> </h3> <p> Consider an <code><span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> interface in C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">Run</span>(T&nbsp;<span style="color:#1f377f;">x</span>); }</pre> </p> <p> I've borrowed this interface from the article <a href="/2021/05/31/from-state-tennis-to-endomorphism">From State tennis to endomorphism</a>. In that article I explain that I only introduce this interface for educational reasons. I don't expect you to use something like this in production code bases. On the other hand, everything that applies to <code><span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> also applies to 'naked' functions, as you'll see later in the article. </p> <p> As outlined in the introduction, you can make a container an invariant functor by implementing a non-standard version of <code>Select</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEndomorphism&lt;B&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">B</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IEndomorphism&lt;A&gt;&nbsp;<span style="color:#1f377f;">endomorphism</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">aToB</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;B,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">bToA</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;SelectEndomorphism&lt;A,&nbsp;B&gt;(endomorphism,&nbsp;aToB,&nbsp;bToA); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SelectEndomorphism</span>&lt;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">B</span>&gt;&nbsp;:&nbsp;IEndomorphism&lt;B&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IEndomorphism&lt;A&gt;&nbsp;endomorphism; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;A,&nbsp;B&gt;&nbsp;aToB; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;B,&nbsp;A&gt;&nbsp;bToA; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">SelectEndomorphism</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;A&gt;&nbsp;<span style="color:#1f377f;">endomorphism</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">aToB</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;B,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">bToA</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.endomorphism&nbsp;=&nbsp;endomorphism; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.aToB&nbsp;=&nbsp;aToB; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.bToA&nbsp;=&nbsp;bToA; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;B&nbsp;<span style="color:#74531f;">Run</span>(B&nbsp;<span style="color:#1f377f;">x</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;aToB(endomorphism.Run(bToA(x))); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since the <code>Select</code> method has to return an <code>IEndomorphism&lt;B&gt;</code> implementation, one option is to use a private, nested class. Most of this is <a href="/2019/12/16/zone-of-ceremony">ceremony</a> required because it's working with interfaces. The interesting part is the nested class' <code>Run</code> implementation. </p> <p> In order to translate an <code>IEndomorphism&lt;A&gt;</code> to an <code>IEndomorphism&lt;B&gt;</code>, the <code>Run</code> method first uses <code>bToA</code> to translate <code>x</code> to an <code>A</code> value. Once it has the <code>A</code> value, it can <code>Run</code> the <code>endomorphism</code>, which returns another <code>A</code> value. Finally, the method can use <code>aToB</code> to convert the returned <code>A</code> value to a <code>B</code> value that it can return. </p> <p> Here's a simple example. Imagine that you have an endomorphism like this one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Incrementer</span>&nbsp;:&nbsp;IEndomorphism&lt;BigInteger&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;BigInteger&nbsp;<span style="color:#74531f;">Run</span>(BigInteger&nbsp;<span style="color:#1f377f;">x</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;x&nbsp;+&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This one simply increments a <a href="https://docs.microsoft.com/dotnet/api/system.numerics.biginteger">BigInteger</a> value. Since <code>BigInteger</code> is isomorphic to a byte array, it's possible to transform this <code>BigInteger</code> endomorphism to a byte array endomorphism: </p> <p> <pre>[Theory] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[0],&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;{&nbsp;1&nbsp;})] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;{&nbsp;1&nbsp;},&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;{&nbsp;2&nbsp;})] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;{&nbsp;255,&nbsp;0&nbsp;},&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;{&nbsp;0,&nbsp;1&nbsp;})] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">InvariantSelection</span>(<span style="color:blue;">byte</span>[]&nbsp;<span style="color:#1f377f;">bs</span>,&nbsp;<span style="color:blue;">byte</span>[]&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;BigInteger&gt;&nbsp;<span style="color:#1f377f;">source</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Incrementer(); &nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;<span style="color:blue;">byte</span>[]&gt;&nbsp;<span style="color:#1f377f;">destination</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.Select(<span style="color:#1f377f;">bi</span>&nbsp;=&gt;&nbsp;bi.ToByteArray(),&nbsp;<span style="color:#1f377f;">arr</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;BigInteger(arr)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;destination.Run(bs)); }</pre> </p> <p> You can convert a <code>BigInteger</code> to a byte array with the <code>ToByteArray</code> method, and convert such a byte array back to a <code>BigInteger</code> using one of its constructor overloads. Since this is possible, the example test can convert this <code>IEndomorphism&lt;BigInteger&gt;</code> to an <code>IEndomorphism&lt;<span style="color:blue;">byte</span>[]&gt;</code> and later <code>Run</code> it. </p> <h3 id="f9afa75c0b014df68d29ddf1244f329f"> Mapping functions in F# <a href="#f9afa75c0b014df68d29ddf1244f329f" title="permalink">#</a> </h3> <p> You don't need an interface in order to turn an endomorphism into an invariant functor. An endomorphism is just a function that has the same input and output type. In C# such a function has the type <code>Func&lt;T, T&gt;</code>, while in F# it's written <code>&#39;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a</code>. </p> <p> You could write an F# module that defines an <code>invmap</code> function, which would be equivalent to the above <code>Select</code> method: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Endo&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(&#39;b&nbsp;-&gt;&nbsp;&#39;a)&nbsp;-&gt;&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;a)&nbsp;-&gt;&nbsp;(&#39;b&nbsp;-&gt;&nbsp;&#39;b)</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;invmap&nbsp;(f&nbsp;:&nbsp;&#39;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;b)&nbsp;(g&nbsp;:&nbsp;&#39;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a)&nbsp;(h&nbsp;:&nbsp;&#39;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a)&nbsp;=&nbsp;g&nbsp;&gt;&gt;&nbsp;h&nbsp;&gt;&gt;&nbsp;f</pre> </p> <p> Since this function doesn't have to deal with the ceremony of interfaces, the implementation is simple function composition: For any input, first apply it to the <code>g</code> function, then apply the output to the <code>h</code> function, and again apply the output of that function to the <code>f</code> function. </p> <p> Here's the same example as above: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;increment&nbsp;(bi&nbsp;:&nbsp;BigInteger)&nbsp;=&nbsp;bi&nbsp;+&nbsp;BigInteger.One <span style="color:green;">//&nbsp;byte&nbsp;[]&nbsp;-&gt;&nbsp;byte&nbsp;[]</span> <span style="color:blue;">let</span>&nbsp;bArrInc&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;Endo.invmap&nbsp;(<span style="color:blue;">fun</span>&nbsp;(bi&nbsp;:&nbsp;BigInteger)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;bi.ToByteArray&nbsp;())&nbsp;BigInteger&nbsp;increment</pre> </p> <p> Here's a simple sanity check of the <code>bArrInc</code> function executed in F# Interactive: </p> <p> <pre>&gt; let bArr = bArrInc [| 255uy; 255uy; 0uy |];; val bArr : byte [] = [|0uy; 0uy; 1uy|]</pre> </p> <p> If you are wondering about that particular output value, I'll refer you to <a href="https://docs.microsoft.com/dotnet/api/system.numerics.biginteger">the BigInteger documentation</a>. </p> <h3 id="76c32ad7688b41fc9e083a51584e4a6a"> Function composition <a href="#76c32ad7688b41fc9e083a51584e4a6a" title="permalink">#</a> </h3> <p> The F# implementation of <code>invmap</code> (<code>g&nbsp;&gt;&gt;&nbsp;h&nbsp;&gt;&gt;&nbsp;f</code>) makes it apparent that an endomorphism is an invariant functor via function composition. In F#, though, that fact almost disappears in all the type declaration ceremony. In the Haskell instance from the <a href="https://hackage.haskell.org/package/invariant">invariant</a> package it's even clearer: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Invariant</span>&nbsp;<span style="color:blue;">Endo</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;invmap&nbsp;f&nbsp;g&nbsp;(Endo&nbsp;h)&nbsp;=&nbsp;Endo&nbsp;(f&nbsp;.&nbsp;h&nbsp;.&nbsp;g)</pre> </p> <p> Perhaps a diagram is helpful: </p> <p> <img src="/content/binary/invariant-map-diagram.png" alt="Arrow diagram showing the mapping from an endomorphism in a to an endomorphism in b."> </p> <p> If you have a function <code>h</code> from the type <code>a</code> to <code>a</code> and you need a function <code>b -&gt; b</code>, you can produce it by putting <code>g</code> in front of <code>h</code>, and <code>f</code> after. That's also what the above C# implementation does. In F#, you can express such a composition as <code>g&nbsp;&gt;&gt;&nbsp;h&nbsp;&gt;&gt;&nbsp;f</code>, which seems natural to most westerners, since it goes from left to right. In Haskell, most expressions are instead expressed from right to left, so it becomes: <code>f&nbsp;.&nbsp;h&nbsp;.&nbsp;g</code>. In any case, the result is the desired function that takes a <code>b</code> value as input and returns a <code>b</code> value as output. That composed function is indicated by a dashed arrow in the above diagram. </p> <h3 id="3934e941d51f47c698d8b803b7adb97c"> Identity law <a href="#3934e941d51f47c698d8b803b7adb97c" title="permalink">#</a> </h3> <p> Contrary to my usual habit, I'm going to <em>prove</em> that both invariant functor laws hold for this implementation. I'll use equational reasoning with <a href="https://bartoszmilewski.com/2015/01/20/functors/">the notation that Bartosz Milewski uses</a>. Here's the proof that the <code>invmap</code> instance obeys the identity law: </p> <p> <pre>&nbsp;&nbsp;invmap&nbsp;id&nbsp;id&nbsp;(Endo&nbsp;h) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;invmap&nbsp;} &nbsp;&nbsp;Endo&nbsp;(id&nbsp;.&nbsp;h&nbsp;.&nbsp;id) =&nbsp;{&nbsp;eta&nbsp;expansion&nbsp;} &nbsp;&nbsp;Endo&nbsp;(\x&nbsp;-&gt;&nbsp;(id&nbsp;.&nbsp;h&nbsp;.&nbsp;id)&nbsp;x) =&nbsp;{&nbsp;defintion&nbsp;of&nbsp;composition&nbsp;(.)&nbsp;} &nbsp;&nbsp;Endo&nbsp;(\x&nbsp;-&gt;&nbsp;id&nbsp;(h&nbsp;(id&nbsp;x))) =&nbsp;{&nbsp;defintion&nbsp;of&nbsp;id&nbsp;} &nbsp;&nbsp;Endo&nbsp;(\x&nbsp;-&gt;&nbsp;h&nbsp;x) =&nbsp;{&nbsp;eta&nbsp;reduction&nbsp;} &nbsp;&nbsp;Endo&nbsp;h =&nbsp;{&nbsp;definition&nbsp;of&nbsp;id&nbsp;} &nbsp;&nbsp;id&nbsp;(Endo&nbsp;h)</pre> </p> <p> While I'm not going to comment further on that, I can show you what the identity law looks like in C#: </p> <p> <pre>[Theory] [InlineData(0)] [InlineData(1)] [InlineData(9)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IdentityLaw</span>(<span style="color:blue;">long</span>&nbsp;<span style="color:#1f377f;">l</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;BigInteger&gt;&nbsp;<span style="color:#1f377f;">e</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Incrementer(); &nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;BigInteger&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;e.Select(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(e.Run(l),&nbsp;actual.Run(l)); }</pre> </p> <p> In C#, you typically write the identity function (<code>id</code> in F# and Haskell) as the lambda expression <code><span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x</code>, since the identity function isn't 'built in' for C# like it is for F# and Haskell. (You can define it yourself, but it's not as <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a>.) </p> <h3 id="e47f375aafe441949e057787a5d35178"> Composition law <a href="#e47f375aafe441949e057787a5d35178" title="permalink">#</a> </h3> <p> As with the identity law, I'll start by suggesting a proof for the composition law for the Haskell instance: </p> <p> <pre>&nbsp;&nbsp;invmap&nbsp;f2&nbsp;f2&#39;&nbsp;$&nbsp;invmap&nbsp;f1&nbsp;f1&#39;&nbsp;(Endo&nbsp;h) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;invmap&nbsp;} &nbsp;&nbsp;invmap&nbsp;f2&nbsp;f2&#39;&nbsp;$&nbsp;Endo&nbsp;(f1&nbsp;.&nbsp;h&nbsp;.&nbsp;f1&#39;) =&nbsp;{&nbsp;defintion&nbsp;of&nbsp;($)&nbsp;} &nbsp;&nbsp;invmap&nbsp;f2&nbsp;f2&#39;&nbsp;(Endo&nbsp;(f1&nbsp;.&nbsp;h&nbsp;.&nbsp;f1&#39;)) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;invmap&nbsp;} &nbsp;&nbsp;Endo&nbsp;(f2&nbsp;.&nbsp;(f1&nbsp;.&nbsp;h&nbsp;.&nbsp;f1&#39;)&nbsp;.&nbsp;f2&#39;) =&nbsp;{&nbsp;associativity&nbsp;of&nbsp;composition&nbsp;(.)&nbsp;} &nbsp;&nbsp;Endo&nbsp;((f2&nbsp;.&nbsp;f1)&nbsp;.&nbsp;h&nbsp;.&nbsp;(f1&#39;&nbsp;.&nbsp;f2&#39;)) =&nbsp;{&nbsp;definition&nbsp;of&nbsp;invmap&nbsp;} &nbsp;&nbsp;invmap&nbsp;(f2&nbsp;.&nbsp;f1)&nbsp;(f1&#39;&nbsp;.&nbsp;f2&#39;)&nbsp;(Endo&nbsp;h)</pre> </p> <p> As above, a C# example may also help. First, assume that you have some endomorphism like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SecondIncrementer</span>&nbsp;:&nbsp;IEndomorphism&lt;TimeSpan&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TimeSpan&nbsp;<span style="color:#74531f;">Run</span>(TimeSpan&nbsp;<span style="color:#1f377f;">x</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;x&nbsp;+&nbsp;TimeSpan.FromSeconds(1); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> A test then demonstrates the composition law in action: </p> <p> <pre>[Theory] [InlineData(-3)] [InlineData(0)] [InlineData(11)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CompositionLaw</span>(<span style="color:blue;">long</span>&nbsp;<span style="color:#1f377f;">x</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SecondIncrementer(); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;TimeSpan,&nbsp;<span style="color:blue;">long</span>&gt;&nbsp;<span style="color:#1f377f;">f1</span>&nbsp;=&nbsp;<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;ts.Ticks; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">long</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">f1p</span>&nbsp;=&nbsp;<span style="color:#1f377f;">l</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(l); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">long</span>,&nbsp;IntPtr&gt;&nbsp;<span style="color:#1f377f;">f2</span>&nbsp;=&nbsp;<span style="color:#1f377f;">l</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;IntPtr(l); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;IntPtr,&nbsp;<span style="color:blue;">long</span>&gt;&nbsp;<span style="color:#1f377f;">f2p</span>&nbsp;=&nbsp;<span style="color:#1f377f;">ip</span>&nbsp;=&gt;&nbsp;ip.ToInt64(); &nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;IntPtr&gt;&nbsp;<span style="color:#1f377f;">left</span>&nbsp;=&nbsp;i.Select(f1,&nbsp;f1p).Select(f2,&nbsp;f2p); &nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;IntPtr&gt;&nbsp;<span style="color:#1f377f;">right</span>&nbsp;=&nbsp;i.Select(<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;f2(f1(ts)),&nbsp;<span style="color:#1f377f;">ip</span>&nbsp;=&gt;&nbsp;f1p(f2p(ip))); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(left.Run(<span style="color:blue;">new</span>&nbsp;IntPtr(x)),&nbsp;right.Run(<span style="color:blue;">new</span>&nbsp;IntPtr(x))); }</pre> </p> <p> Don't try to make any sense of this. As outlined in the introductory article, in order to use an invariant functor, you're going to need an isomorphism. In order to demonstrate the composition law, you need <em>three</em> types that are isomorphic. Since you can convert back and forth between <code>TimeSpan</code> and <code>IntPtr</code> via <code>long</code>, this requirement is formally fulfilled. It doesn't make any sense to add a second to a value and then turn it into a function that changes a pointer. It sounds more like a security problem waiting to happen... Don't try this at home, kids. </p> <h3 id="ac1e8d3a75b54f9691c616755750ff77"> Conclusion <a href="#ac1e8d3a75b54f9691c616755750ff77" title="permalink">#</a> </h3> <p> Since an endomorphism can be modelled as a 'generic type', it may look like a candidate for a functor or <a href="/2021/09/02/contravariant-functors">contravariant functor</a>, but alas, neither is possible. The best we can get (apart from <a href="/2017/11/13/endomorphism-monoid">a monoid instance</a>) is an invariant functor. </p> <p> The invariant functor instance for an endomorphism turns out to be simple function composition. That's not how all invariant functors, work, though. </p> <p> <strong>Next:</strong> <a href="/2022/08/29/natural-transformations-as-invariant-functors">Natural transformations as invariant functors</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Invariant functors https://blog.ploeh.dk/2022/08/01/invariant-functors 2022-08-01T05:49:00+00:00 Mark Seemann <div id="post"> <p> <em>Containers that support mapping isomorphic values.</em> </p> <p> This article series is part of <a href="/2018/03/19/functors-applicatives-and-friends">a larger series of articles about functors, applicatives, and other mappable containers</a>. So far, you've seen examples of both co- and <a href="/2021/09/02/contravariant-functors">contravariant functors</a>, including <a href="/2021/11/01/profunctors">profunctors</a>. You've also seen a few examples of <a href="/2020/10/19/monomorphic-functors">monomorphic functors</a> - mappable containers where there's no variance at all. </p> <p> What happens, on the other hand, if you have a container of (generic) values, but it's neither co- nor contravariant? An <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a> is an example - it's neither co- nor contravariant. You'll see a treatment of that in a later article. </p> <p> Even if neither co- nor contravariant mappings exists for a container, all may not be lost. It may still be an <em>invariant functor</em>. </p> <h3 id="40d4c4a6deed4af593b3cf002563f085"> Invariance <a href="#40d4c4a6deed4af593b3cf002563f085" title="permalink">#</a> </h3> <p> Consider a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> <code>f</code> (for <em>functor</em>). Depending on its variance, we call it <em>covariant</em>, <em>contravariant</em>, or <em>invariant</em>: </p> <ul> <li><em>Covariance</em> means that any function <code>a -&gt; b</code> can be lifted into a function <code>f a -&gt; f b</code>.</li> <li><em>Contravariance</em> means that any function <code>a -&gt; b</code> can be lifted into a function <code>f b -&gt; f a</code>.</li> <li><em>Invariance</em> means that in general, no function <code>a -&gt; b</code> can be lifted into a function over <code>f a</code>.</li> </ul> <p> <em>In general</em>, that is. A limited escape hatch exists: </p> <blockquote> <p> "an invariant type [...] allows you to map from <code>a</code> to <code>b</code> if and only if <code>a</code> and <code>b</code> are isomorphic. In a very real sense, this isn't an interesting property - an isomorphism between <code>a</code> and <code>b</code> means they're already the same thing to begin with." </p> <footer><cite>Sandy Maguire, <a href="https://thinkingwithtypes.com/">Thinking with Types</a></cite></footer> </blockquote> <p> In <a href="https://www.haskell.org/">Haskell</a> we may define an invariant functor (AKA <a href="http://comonad.com/reader/2008/rotten-bananas/">exponential functor</a>) as in the <a href="https://hackage.haskell.org/package/invariant">invariant</a> package: </p> <p> <pre><span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Invariant</span>&nbsp;f&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:#2b91af;">invmap</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;b</pre> </p> <p> This means that an <em>invariant functor</em> <code>f</code> is a container of values where a translation from <code>f a</code> to <code>f b</code> exists if it's possible to translate contained values both ways: From <code>a</code> to <code>b</code>, and from <code>b</code> to <code>a</code>. Callers of the <code>invmap</code> function must supply translations that go both ways. </p> <h3 id="36e114d8871c46c9addb2e38936a2872"> Invariant functor in C# <a href="#36e114d8871c46c9addb2e38936a2872" title="permalink">#</a> </h3> <p> It's possible to translate the concept to a language like C#. Since C# doesn't have higher-kinded types, we have to examine the abstraction as a set of patterns or templates. For <a href="/2018/03/22/functors">functors</a> and <a href="/2022/03/28/monads">monads</a>, the C# compiler can perform 'compile-time duck typing' to recognise these motifs to enable query syntax. For more advanced or exotic universal abstractions, such as <a href="/2018/12/24/bifunctors">bifunctors</a>, <a href="/2021/11/01/profunctors">profunctors</a>, or invariant functors, we have to use a concrete container type as a stand-in for 'any' functor. In this article, I'll call it <code><span style="color:#2b91af;">Invariant</span>&lt;<span style="color:#2b91af;">A</span>&gt;</code>. </p> <p> Such a generic class must have a mapping function that corresponds to the above <code>invmap</code>. In C# it has this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Invariant&lt;B&gt;&nbsp;<span style="color:#74531f;">InvMap</span>&lt;<span style="color:#2b91af;">B</span>&gt;(Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">aToB</span>,&nbsp;Func&lt;B,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">bToA</span>)</pre> </p> <p> In this example, <code>InvMap</code> is an instance method on <code><span style="color:#2b91af;">Invariant</span>&lt;<span style="color:#2b91af;">A</span>&gt;</code>. You may use it like this: </p> <p> <pre>Invariant&lt;<span style="color:blue;">long</span>&gt;&nbsp;<span style="color:#1f377f;">il</span>&nbsp;=&nbsp;createInvariant(); Invariant&lt;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">its</span>&nbsp;=&nbsp;il.InvMap(<span style="color:#1f377f;">l</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(l),&nbsp;<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;ts.Ticks);</pre> </p> <p> It's not that easy to find good examples of truly isomorphic primitives, but <a href="https://docs.microsoft.com/dotnet/api/system.timespan">TimeSpan</a> is just a useful wrapper of <code>long</code>, so it's possible to translate back and forth without loss of information. To create a <code>TimeSpan</code> from a <code>long</code>, you can use the suitable constructor overload. To get a <code>long</code> from a <code>TimeSpan</code>, you can read the <a href="https://docs.microsoft.com/dotnet/api/system.timespan.ticks">Ticks</a> property. </p> <p> Perhaps you find a method name like <code>InvMap</code> non-idiomatic in C#. Perhaps a more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> name might be <code>Select</code>? That's not a problem: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Invariant&lt;B&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">B</span>&gt;(Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">aToB</span>,&nbsp;Func&lt;B,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">bToA</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;InvMap(aToB,&nbsp;bToA); }</pre> </p> <p> In that case, usage would look like this: </p> <p> <pre>Invariant&lt;<span style="color:blue;">long</span>&gt;&nbsp;<span style="color:#1f377f;">il</span>&nbsp;=&nbsp;createInvariant(); Invariant&lt;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">its</span>&nbsp;=&nbsp;il.Select(<span style="color:#1f377f;">l</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(l),&nbsp;<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;ts.Ticks);</pre> </p> <p> In this article, I'll use <code>Select</code> in order to be consistent with C# naming conventions. Using that name, however, will not make query syntax light up. While the name is fine, the signature is not one that the C# compiler will recognise as enabling special syntax. The name does, however, suggest a kinship with a normal functor, where the mapping in C# is called <code>Select</code>. </p> <h3 id="4a5c0a55258d4e398931d2880df059fb"> Laws <a href="#4a5c0a55258d4e398931d2880df059fb" title="permalink">#</a> </h3> <p> As is usual with these kinds of universal abstractions, an invariant functor must satisfy a few laws. </p> <p> The first one we might call the <em>identity law:</em> </p> <p> <pre>invmap id id = id</pre> </p> <p> This law corresponds to the first functor law. When performing the mapping operation, if the values in the invariant functor are mapped to themselves, the result will be an unmodified functor. </p> <p> In C# such a mapping might look like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;i.Select(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x);</pre> </p> <p> The law then says that <code>actual</code> should be equal to <code>i</code>. </p> <p> The second law we might call the <em>composition law:</em> </p> <p> <pre>invmap f2 f2' . invmap f1 f1' = invmap (f2 . f1) (f1' . f2')</pre> </p> <p> Granted, this looks more complicated, but also directly corresponds to the second functor law. If two sequential mapping operations are performed one after the other, the result should be the same as a single mapping operation where the functions are composed. </p> <p> In C# the left-hand side might look like this: </p> <p> <pre>Invariant&lt;IntPtr&gt;&nbsp;<span style="color:#1f377f;">left</span>&nbsp;=&nbsp;i.Select(f1,&nbsp;f1p).Select(f2,&nbsp;f2p);</pre> </p> <p> In C# you can't name functions or variables with a quotation mark (like the Haskell code's <code>f1'</code> and <code>f2'</code>), so instead I named them <code>f1p</code> and <code>f2p</code> (with a <em>p</em> for <em>prime</em>). </p> <p> Likewise, the right-hand side might look like this: </p> <p> <pre>Invariant&lt;IntPtr&gt;&nbsp;<span style="color:#1f377f;">right</span>&nbsp;=&nbsp;i.Select(<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;f2(f1(ts)),&nbsp;<span style="color:#1f377f;">ip</span>&nbsp;=&gt;&nbsp;f1p(f2p(ip)));</pre> </p> <p> The composition law says that the <code>left</code> and <code>right</code> values must be equal. </p> <p> You'll see some more detailed examples in later articles. </p> <h3 id="14c58dc57f9744dfbc955015d03b871e"> Examples <a href="#14c58dc57f9744dfbc955015d03b871e" title="permalink">#</a> </h3> <p> This is all too abstract to seem useful in itself, so example are warranted. You'll be able to peruse examples of specific invariant functors in separate articles: </p> <ul> <li><a href="/2022/08/08/endomorphism-as-an-invariant-functor">Endomorphism as an invariant functor</a></li> <li><a href="/2022/08/29/natural-transformations-as-invariant-functors">Natural transformations as invariant functors</a></li> <li><a href="/2022/12/26/functors-as-invariant-functors">Functors as invariant functors</a></li> <li><a href="/2023/02/06/contravariant-functors-as-invariant-functors">Contravariant functors as invariant functors</a></li> </ul> <p> As two of the titles suggest, all functors are also invariant functors, and the same goes for contravariant functors: </p> <p> <img src="/content/binary/invariant-functor-set-diagram.png" alt="Set diagram. The biggest set labelled invariant functos contains two other sets labelled functors and invariant functors."> </p> <p> To be honest, invariant functors are exotic, and you are unlikely to need them in all but the rarest cases. Still, I <em>did</em> run into a scenario where I needed an invariant functor instance to be able to perform <a href="/2022/09/05/the-state-pattern-and-the-state-monad">a particular sleight of hand</a>. The rabbit holes we sometimes fall into... </p> <h3 id="ffa0e597f09c40e7bcd1cabe808aa0d5"> Conclusion <a href="#ffa0e597f09c40e7bcd1cabe808aa0d5" title="permalink">#</a> </h3> <p> Invariant functors form a set that contains both co- and contravariant functors, as well as some data structures that are neither. This is an exotic abstraction that you may never need. It did, however, get me out of a bind at one time. </p> <strong>Next:</strong> <a href="/2022/08/08/endomorphism-as-an-invariant-functor">Endomorphism as an invariant functor</a>. </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="faae4a8cbe294be8ac7596488d675483"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#faae4a8cbe294be8ac7596488d675483">#</a></div> <div class="comment-content"> <blockquote> For <a href="/2018/03/22/functors">functors</a> and <a href="/2022/03/28/monads">monads</a>, the C# compiler can perform 'compile-time duck typing' to recognise these motifs to enable query syntax. </blockquote> <p> Instead of 'compile-time duck typing', I think a better phrase to describe this is <a href="https://en.wikipedia.org/wiki/Structural_type_system">structural typing</a>. </p> </div> <div class="comment-date">2022-09-17 16:20 UTC</div> </div> <div class="comment" id="98be00b677484b11a51d8e583c25baa5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#98be00b677484b11a51d8e583c25baa5">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I wasn't aware of the term <em>structural typing</em>, so thank you for the link. I've now read that Wikipedia article, but all I know is what's there. Based on it, though, it looks as though <a href="https://fsharp.org">F#</a>'s <a href="https://learn.microsoft.com/dotnet/fsharp/language-reference/generics/statically-resolved-type-parameters">Statically Resolved Type Parameters</a> are another example of structural typing, in addition to the <a href="https://ocaml.org/">OCaml</a> example given in the article. </p> <p> IIRC, <a href="https://www.purescript.org/">PureScript</a>'s <em>row polymorphism</em> may be another example, but it's been many years since <a href="/2017/06/06/fractal-trees-with-purescript">I played with it</a>. In other words, I could be mistaken. </p> <p> Based on the Wikipedia article, it looks as though structural typing is more concerned with polymorphism, but granted, so is duck typing. Given how wrong 'compile-time duck typing' actually is in the above context, 'structural typing' seems more correct. </p> <p> I may still stick with 'compile-time duck typing' as a loose metaphor, though, because most people know what duck typing is, whereas I'm not sure as many people know of structural typing. The purpose of the metaphor is, after all, to be helpful. </p> </div> <div class="comment-date">2022-09-19 14:46 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An applicative reservation validation example in C# https://blog.ploeh.dk/2022/07/25/an-applicative-reservation-validation-example-in-c 2022-07-25T06:56:00+00:00 Mark Seemann <div id="post"> <p> <em>How to return all relevant error messages in a composable way.</em> </p> <p> I've previously suggested that <a href="/2020/12/14/validation-a-solved-problem">I consider validation a solved problem</a>. I still do, until someone disproves me with a counterexample. Here's a fairly straightforward <a href="/2018/11/05/applicative-validation">applicative validation</a> example in C#. </p> <p> After corresponding and speaking with readers of <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> I've learned that some readers have objections to the following lines of code: </p> <p> <pre>Reservation?&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;dto.Validate(id); <span style="color:#8f08c4;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;BadRequestResult();</pre> </p> <p> This code snippet demonstrates how to <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">parse, not validate</a>, an incoming Data Transfer Object (DTO). This code base uses C#'s <a href="https://docs.microsoft.com/dotnet/csharp/nullable-references">nullable reference types</a> feature to distinguish between null and non-null objects. Other languages (and earlier versions of C#) can instead use <a href="/2022/04/25/the-maybe-monad">the Maybe monad</a>. Nothing in this article or the book hinges on the <em>nullable reference types</em> feature. </p> <p> If the <code>Validate</code> method (which I really should have called <code>TryParse</code> instead) returns a null value, the Controller from which this code snippet is taken returns a <code>400 Bad Request</code> response. </p> <p> The <code>Validate</code> method is an instance method on the DTO class: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;Reservation?&nbsp;<span style="color:#74531f;">Validate</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!DateTime.TryParse(At,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">d</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Email&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Quantity&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(Email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity); }</pre> </p> <p> What irks some readers is the loss of information. While <code>Validate</code> 'knows' why it's rejecting a candidate, that information is lost and no error message is communicated to unfortunate HTTP clients. </p> <p> One email from a reader went on about this for quite some time and I got the impression that the sender considered this such a grave flaw that it invalidates the entire book. </p> <p> That's not the case. </p> <h3 id="26803ee560dd42be825ec266694d0211"> Rabbit hole, evaded <a href="#26803ee560dd42be825ec266694d0211" title="permalink">#</a> </h3> <p> When I wrote the code like above, I was fully aware of trade-offs and priorities. I understood that this particular design would mean that clients get no information about <em>why</em> a particular reservation JSON document is rejected - only that it is. </p> <p> This was a simplification that I explicitly decided to make for educational reasons. </p> <p> The above design is based on something as simple as a null check. I expect all my readers to be able to follow that code. As hinted above, you could also model a method like <code>Validate</code> with the Maybe monad, but while Maybe preserves success cases, it throws away all information about errors. In a production system, this is rarely acceptable, but I found it acceptable for the example code in the book, since this isn't the main topic. </p> <p> Instead of basing the design on nullable reference types or the Maybe monad, you can instead base parsing on applicative validation. In order to explain that, I'd first need to explain <a href="/2018/03/22/functors">functors</a>, <a href="/2018/10/01/applicative-functors">applicative functors</a>, and applicative validation. It might also prove helpful to the reader to explain <a href="/2018/05/22/church-encoding">Church encodings</a>, <a href="/2018/12/24/bifunctors">bifunctors</a>, and <a href="/2017/11/27/semigroups">semigroups</a>. That's quite a rabbit hole to fall into, and I felt that it would be such a big digression from the themes of the book that I decided not to go there. </p> <p> On this blog, however, I have all the space and time I'd like. I can digress as much as I'd like. Most of that digression has already happened. Those articles are already on the blog. I'm going to assume that you've read all of the articles I just linked, or that you understand these concepts. </p> <p> In this article, I'm going to rewrite the DTO parser to also return error messages. It's an entirely local change that breaks no existing tests. </p> <h3 id="5f6a7326fb484c1d8b4402b24e49d13b"> Validated <a href="#5f6a7326fb484c1d8b4402b24e49d13b" title="permalink">#</a> </h3> <p> Most functional programmers are already aware of the <a href="/2022/05/09/an-either-monad">Either monad</a>. They often reach for it when they need to expand the Maybe monad with <a href="https://fsharpforfunandprofit.com/posts/recipe-part2/">an error track</a>. </p> <p> The problem with the Either monad is, however, that it short-circuits error handling. It's like throwing exceptions. As soon as an Either composition hits the first error, it stops processing the rest of the data. As a caller, you only get one error message, even if there's more than one thing wrong with your input value. </p> <p> In a distributed system where a client posts a document to a service, you'd like to respond with a collection of errors. </p> <p> You can do this with a data type that's isomorphic with Either, but behaves differently as an applicative functor. Instead of short-circuiting on the first error, it collects them. This, however, turns out to be incompatible to the Either monad's short-circuiting behaviour, so this data structure is usually not given monadic features. </p> <p> This data type is usually called <code>Validation</code>, but when I translated that to C# various static code analysis rules lit up, claiming that there was already a referenced namespace called <code>Validation</code>. Instead, I decided to call the type <code><span style="color:#2b91af;">Validated</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;</code>, which I like better anyway. </p> <p> The type arguments are <code>F</code> for <em>failure</em> and <code>S</code> for <em>success</em>. I've put <code>F</code> before <code>S</code> because by convention that's how Either works. </p> <p> I'm using an encapsulated variation of a Church encoding and a series of <code>Apply</code> overloads as described in the article <a href="/2018/10/15/an-applicative-password-list">An applicative password list</a>. There's quite a bit of boilerplate, so I'll just dump the entire contents of the file here instead of tiring you with a detailed walk-through: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Validated</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IValidation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">Match</span>&lt;<span style="color:#2b91af;">T</span>&gt;(Func&lt;F,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">onFailure</span>,&nbsp;Func&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">onSuccess</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IValidation&nbsp;imp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Validated</span>(IValidation&nbsp;<span style="color:#1f377f;">imp</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.imp&nbsp;=&nbsp;imp; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;F,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Succeed</span>(S&nbsp;<span style="color:#1f377f;">success</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Validated&lt;F,&nbsp;S&gt;(<span style="color:blue;">new</span>&nbsp;Success(success)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;F,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Fail</span>(F&nbsp;<span style="color:#1f377f;">failure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Validated&lt;F,&nbsp;S&gt;(<span style="color:blue;">new</span>&nbsp;Failure(failure)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="color:#74531f;">Match</span>&lt;<span style="color:#2b91af;">T</span>&gt;(Func&lt;F,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">onFailure</span>,&nbsp;Func&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">onSuccess</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;imp.Match(onFailure,&nbsp;onSuccess); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Validated&lt;F1,&nbsp;S1&gt;&nbsp;<span style="color:#74531f;">SelectBoth</span>&lt;<span style="color:#2b91af;">F1</span>,&nbsp;<span style="color:#2b91af;">S1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;F,&nbsp;F1&gt;&nbsp;<span style="color:#1f377f;">selectFailure</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S,&nbsp;S1&gt;&nbsp;<span style="color:#1f377f;">selectSuccess</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&gt;&nbsp;Validated.Fail&lt;F1,&nbsp;S1&gt;(selectFailure(f)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Validated.Succeed&lt;F1,&nbsp;S1&gt;(selectSuccess(s))); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Validated&lt;F1,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">SelectFailure</span>&lt;<span style="color:#2b91af;">F1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;F,&nbsp;F1&gt;&nbsp;<span style="color:#1f377f;">selectFailure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;SelectBoth(selectFailure,&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Validated&lt;F,&nbsp;S1&gt;&nbsp;<span style="color:#74531f;">SelectSuccess</span>&lt;<span style="color:#2b91af;">S1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S,&nbsp;S1&gt;&nbsp;<span style="color:#1f377f;">selectSuccess</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;SelectBoth(<span style="color:#1f377f;">f</span>&nbsp;=&gt;&nbsp;f,&nbsp;selectSuccess); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Validated&lt;F,&nbsp;S1&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">S1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S,&nbsp;S1&gt;&nbsp;<span style="color:#1f377f;">selector</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;SelectSuccess(selector); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Success</span>&nbsp;:&nbsp;IValidation &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;S&nbsp;success; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Success</span>(S&nbsp;<span style="color:#1f377f;">success</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.success&nbsp;=&nbsp;success; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="color:#74531f;">Match</span>&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;F,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">onFailure</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">onSuccess</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;onSuccess(success); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Failure</span>&nbsp;:&nbsp;IValidation &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;F&nbsp;failure; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Failure</span>(F&nbsp;<span style="color:#1f377f;">failure</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.failure&nbsp;=&nbsp;failure; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="color:#74531f;">Match</span>&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;F,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">onFailure</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">onSuccess</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;onFailure(failure); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Validated</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;F,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Succeed</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;S&nbsp;<span style="color:#1f377f;">success</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated&lt;F,&nbsp;S&gt;.Succeed(success); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;F,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Fail</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F&nbsp;<span style="color:#1f377f;">failure</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated&lt;F,&nbsp;S&gt;.Fail(failure); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;F,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Apply</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Validated&lt;F,&nbsp;Func&lt;T,&nbsp;S&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Validated&lt;F,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;F,&nbsp;F,&nbsp;F&gt;&nbsp;<span style="color:#1f377f;">combine</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(selector&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(selector)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;selector.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f1</span>&nbsp;=&gt;&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f2</span>&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;S&gt;(combine(f1,&nbsp;f2)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">_</span>&nbsp;&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;S&gt;(f1)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">map</span>&nbsp;=&gt;&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f2</span>&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;S&gt;(f2), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">x</span>&nbsp;&nbsp;=&gt;&nbsp;Succeed&lt;F,&nbsp;S&gt;(map(x)))); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;F,&nbsp;Func&lt;T2,&nbsp;S&gt;&gt;&nbsp;<span style="color:#74531f;">Apply</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Validated&lt;F,&nbsp;Func&lt;T1,&nbsp;T2,&nbsp;S&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Validated&lt;F,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;F,&nbsp;F,&nbsp;F&gt;&nbsp;<span style="color:#1f377f;">combine</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(selector&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(selector)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;selector.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f1</span>&nbsp;=&gt;&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f2</span>&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;Func&lt;T2,&nbsp;S&gt;&gt;(combine(f1,&nbsp;f2)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">_</span>&nbsp;&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;Func&lt;T2,&nbsp;S&gt;&gt;(f1)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">map</span>&nbsp;=&gt;&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f2</span>&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;Func&lt;T2,&nbsp;S&gt;&gt;(f2), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">x</span>&nbsp;&nbsp;=&gt;&nbsp;Succeed&lt;F,&nbsp;Func&lt;T2,&nbsp;S&gt;&gt;(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;map(x,&nbsp;y)))); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;F,&nbsp;Func&lt;T2,&nbsp;T3,&nbsp;S&gt;&gt;&nbsp;<span style="color:#74531f;">Apply</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Validated&lt;F,&nbsp;Func&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;S&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Validated&lt;F,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;F,&nbsp;F,&nbsp;F&gt;&nbsp;<span style="color:#1f377f;">combine</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(selector&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(selector)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;selector.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f1</span>&nbsp;=&gt;&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f2</span>&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;Func&lt;T2,&nbsp;T3,&nbsp;S&gt;&gt;(combine(f1,&nbsp;f2)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">_</span>&nbsp;&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;Func&lt;T2,&nbsp;T3,&nbsp;S&gt;&gt;(f1)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">map</span>&nbsp;=&gt;&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">f2</span>&nbsp;=&gt;&nbsp;Fail&lt;F,&nbsp;Func&lt;T2,&nbsp;T3,&nbsp;S&gt;&gt;(f2), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">x</span>&nbsp;&nbsp;=&gt;&nbsp;Succeed&lt;F,&nbsp;Func&lt;T2,&nbsp;T3,&nbsp;S&gt;&gt;((<span style="color:#1f377f;">y</span>,&nbsp;<span style="color:#1f377f;">z</span>)&nbsp;=&gt;&nbsp;map(x,&nbsp;y,&nbsp;z)))); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Validated&lt;F,&nbsp;Func&lt;T2,&nbsp;T3,&nbsp;S&gt;&gt;&nbsp;<span style="color:#74531f;">Apply</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Func&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;S&gt;&nbsp;<span style="color:#1f377f;">map</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Validated&lt;F,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;F,&nbsp;F,&nbsp;F&gt;&nbsp;<span style="color:#1f377f;">combine</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Apply( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Succeed&lt;F,&nbsp;Func&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;S&gt;&gt;((<span style="color:#1f377f;">x</span>,&nbsp;<span style="color:#1f377f;">y</span>,&nbsp;<span style="color:#1f377f;">z</span>)&nbsp;=&gt;&nbsp;map(x,&nbsp;y,&nbsp;z)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I only added the <code>Apply</code> overloads that I needed for the following demo code. As stated above, I'm not going to launch into a detailed walk-through, since the code follows the concepts lined out in the various articles I've already mentioned. If there's something that you'd like me to explain then please <a href="https://github.com/ploeh/ploeh.github.com#comments">leave a comment</a>. </p> <p> Notice that <code><span style="color:#2b91af;">Validated</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;</code> has no <code>SelectMany</code> method. It's deliberately not a <a href="/2022/03/28/monads">monad</a>, because monadic <em>bind</em> (<code>SelectMany</code>) would conflict with the applicative functor implementation. </p> <h3 id="533e728019834b22b323ffab10f4cae8"> Individual parsers <a href="#533e728019834b22b323ffab10f4cae8" title="permalink">#</a> </h3> <p> An essential quality of applicative validation is that it's composable. This means that you can compose a larger, more complex parser from smaller ones. Parsing a <code><span style="color:#2b91af;">ReservationDto</span></code> object, for example, involves parsing the date and time of the reservation, the email address, and the quantity. Here's how to parse the date and time: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;Validated&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#74531f;">TryParseAt</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!DateTime.TryParse(At,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">d</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;(<span style="color:#a31515;">$&quot;Invalid&nbsp;date&nbsp;or&nbsp;time:&nbsp;</span>{At}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;(d); }</pre> </p> <p> In order to keep things simple I'm going to use strings for error messages. You could instead decide to encode error conditions as a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> or other polymorphic type. This would be appropriate if you also need to be able to make programmatic decisions based on individual error conditions, or if you need to translate the error messages to more than one language. </p> <p> The <code><span style="color:#74531f;">TryParseAt</span></code> function only attempts to parse the <code>At</code> property to a <code>DateTime</code> value. If parsing fails, it returns a <code><span style="color:#2b91af;">Failure</span></code> value with a helpful error message; otherwise, it wraps the parsed date and time in a <code><span style="color:#2b91af;">Success</span></code> value. </p> <p> Parsing the email address is similar: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;Validated&lt;<span style="color:blue;">string</span>,&nbsp;Email&gt;&nbsp;<span style="color:#74531f;">TryParseEmail</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Email&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;<span style="color:blue;">string</span>,&nbsp;Email&gt;(<span style="color:#a31515;">$&quot;Email&nbsp;address&nbsp;is&nbsp;missing.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;<span style="color:blue;">string</span>,&nbsp;Email&gt;(<span style="color:blue;">new</span>&nbsp;Email(Email)); }</pre> </p> <p> As is parsing the quantity: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;Validated&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">TryParseQuantity</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Quantity&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Fail&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Quantity&nbsp;must&nbsp;be&nbsp;a&nbsp;positive&nbsp;integer,&nbsp;but&nbsp;was:&nbsp;</span>{Quantity}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Validated.Succeed&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(Quantity); }</pre> </p> <p> There's no reason to create a parser for the reservation name, because if the name doesn't exist, instead use the empty string. That operation can't fail. </p> <h3 id="48f0650702f1490392838bf3c5c88cd1"> Composition <a href="#48f0650702f1490392838bf3c5c88cd1" title="permalink">#</a> </h3> <p> You can now use applicative composition to reuse those individual parsers in a more complex parser: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;Validated&lt;<span style="color:blue;">string</span>,&nbsp;Reservation&gt;&nbsp;<span style="color:#74531f;">TryParse</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;DateTime,&nbsp;Email,&nbsp;<span style="color:blue;">int</span>,&nbsp;Reservation&gt;&nbsp;<span style="color:#1f377f;">createReservation</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#1f377f;">at</span>,&nbsp;<span style="color:#1f377f;">email</span>,&nbsp;<span style="color:#1f377f;">quantity</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation(id,&nbsp;at,&nbsp;email,&nbsp;<span style="color:blue;">new</span>&nbsp;Name(Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>),&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">combine</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#1f377f;">x</span>,&nbsp;<span style="color:#1f377f;">y</span>)&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.Join(Environment.NewLine,&nbsp;x,&nbsp;y); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;createReservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(TryParseAt(),&nbsp;combine) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(TryParseEmail(),&nbsp;combine) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Apply(TryParseQuantity(),&nbsp;combine); }</pre> </p> <p> <code><span style="color:#1f377f;">createReservation</span></code> is a local function that closes over <code>id</code> and <code>Name</code>. Specifically, it uses the null coalescing operator (<code>??</code>) to turn a null name into the empty string. On the other hand, it takes <code>at</code>, <code>email</code>, and <code>quantity</code> as inputs, since these are the values that must first be parsed. </p> <p> A type like <code><span style="color:#2b91af;">Validated</span>&lt;<span style="color:#2b91af;">F</span>,&nbsp;<span style="color:#2b91af;">S</span>&gt;</code> is only an applicative functor when the failure dimension (<code>F</code>) gives rise to a semigroup. The way I've modelled it here is as a binary operation that you need to pass as a parameter to each <code>Apply</code> overload. This seems awkward, but is good enough for a proof of concept. </p> <p> The <code><span style="color:#1f377f;">combine</span></code> function joins two strings together, separated by a line break. </p> <p> The <code><span style="color:#74531f;">TryParse</span></code> function composes <code><span style="color:#1f377f;">createReservation</span></code> with <code>TryParseAt</code>, <code>TryParseEmail</code>, and <code>TryParseQuantity</code> using the various <code>Apply</code> overloads. The combination is a <code>Validated</code> value that's either a failure string or a properly encapsulated <code>Reservation</code> object. </p> <ins datetime="2023-06-25T20:06Z"> <p> One thing that I still don't like about this function is that it takes an <code>id</code> parameter. For an article about why that is a problem, and what to do about it, see <a href="/2022/09/12/coalescing-dtos">Coalescing DTOs</a>. </p> </ins> <h3 id="04091776345e4f4b8ba4d446d8d80376"> Using the parser <a href="#04091776345e4f4b8ba4d446d8d80376" title="permalink">#</a> </h3> <p> Client code can now invoke the <code>TryParse</code> function on the DTO. Here is the code inside the <code><span style="color:#74531f;">Post</span></code> method on the <code><span style="color:#2b91af;">ReservationsController</span></code> class: </p> <p> <pre>[HttpPost(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations&quot;</span>)] <span style="color:blue;">public</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;<span style="color:#74531f;">Post</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;ReservationDto&nbsp;<span style="color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(dto&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(dto)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">id</span>&nbsp;=&nbsp;dto.ParseId()&nbsp;??&nbsp;Guid.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">parseResult</span>&nbsp;=&nbsp;dto.TryParse(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;parseResult.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">msgs</span>&nbsp;=&gt;&nbsp;Task.FromResult&lt;ActionResult&gt;(<span style="color:blue;">new</span>&nbsp;BadRequestObjectResult(msgs)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&gt;&nbsp;TryCreate(restaurantId,&nbsp;reservation)); }</pre> </p> <p> When the <code>parseResult</code> matches a failure, it returns a <code><span style="color:blue;">new</span>&nbsp;BadRequestObjectResult</code> with all collected error messages. When, on the other hand, it matches a success, it invokes the <code><span style="color:#74531f;">TryCreate</span></code> helper method with the parsed <code>reservation</code>. </p> <h3 id="d260c38d1dd444e291868e377732fd24"> HTTP request and response <a href="#d260c38d1dd444e291868e377732fd24" title="permalink">#</a> </h3> <p> A client will now receive all relevant error messages if it posts a malformed reservation: </p> <p> <pre>POST /restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D HTTP/1.1 Content-Type: application/json {&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;large&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kerry&nbsp;Onn&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;-1&nbsp;} HTTP/1.1 400 Bad Request Invalid date or time: large. Email address is missing. Quantity must be a positive integer, but was: -1.</pre> </p> <p> Of course, if only a single element is wrong, only that error message will appear. </p> <h3 id="fada4ef8e64648b5a2d90e47082acfde"> Conclusion <a href="#fada4ef8e64648b5a2d90e47082acfde" title="permalink">#</a> </h3> <p> The changes described in this article were entirely local to the two involved types: <code><span style="color:#2b91af;">ReservationsController</span></code> and <code><span style="color:#2b91af;">ReservationDto</span></code>. Once I'd expanded <code><span style="color:#2b91af;">ReservationDto</span></code> with the <code><span style="color:#74531f;">TryParse</span></code> function and its helper functions, and changed <code><span style="color:#2b91af;">ReservationsController</span></code> accordingly, the rest of the code base compiled and all tests passed. The point is that this isn't a big change, and that's why I believe that the original design (returning null or non-null) doesn't invalidate anything else I had to say in the book. </p> <p> The change did, however, take quite a bit of boilerplate code, as witnessed by the <code>Validated</code> code dump. That API is, on the other hand, completely reusable, and you can find packages on the internet that already implement this functionality. It's not much of a burden in terms of extra code, but it would have taken a couple of extra chapters to explain in the book. It could easily have been double the size if I had to include material about functors, applicative functors, semigroups, Church encoding, etcetera. </p> <p> To fix two lines of code, I didn't think that was warranted. After all, it's not a major blocker. On the contrary, validation is a solved problem. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="22b961d6ecb84e6f8af7232b09a445fc"> <div class="comment-author">Dan Carter <a href="#22b961d6ecb84e6f8af7232b09a445fc">#</a></div> <div class="comment-content"> <blockquote>you can find packages on the internet that already implement this functionality</blockquote> <p> Do you have any recommendations for a library that implements the <code>Validated&lt;F, S&gt;</code> type? </p> </div> <div class="comment-date">2022-08-15 11:15 UTC</div> </div> <div class="comment" id="03273eecd6e749188106e8caef68d82e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#03273eecd6e749188106e8caef68d82e">#</a></div> <div class="comment-content"> <p> Dan, thank you for writing. The following is not a recommendation, but the most comprehensive C# library for functional programming currently seems to be <a href="https://github.com/louthy/language-ext">LanguageExt</a>, which includes a <a href="https://louthy.github.io/language-ext/LanguageExt.Core/Monads/Alternative%20Value%20Monads/Validation/index.html">Validation</a> functor. </p> <p> I'm neither recommending nor arguing against LanguageExt. </p> <ul> <li>I've never used it in a real-world code base.</li> <li>I've been answering questions about it on <a href="https://stackoverflow.com">Stack Overflow</a>. In general, it seems to stump C# developers, since it's very Haskellish and quite advanced.</li> <li>Today is just a point in time. Libraries come and go.</li> </ul> <p> Since all the ideas presented in these articles are universal abstractions, you can safely and easily implement them yourself, instead of taking a dependency on a third-party library. If you stick with lawful implementations, the only variation possible is with naming. Do you call a functor like this one <code>Validation</code>, <code>Validated</code>, or something else? Do you call monadic <em>bind</em> <code>SelectMany</code> or <code>Bind</code>? Will you have a <code>Flatten</code> or a <code>Join</code> function? </p> <p> When working with teams that are new to these things, I usually start by adding these concepts as source code as they become useful. If a type like <code>Maybe</code> or <code>Validated</code> starts to proliferate, sooner or later you'll need to move it to a shared library so that multiple in-house libraries can use the type to communicate results across library boundaries. Eventually, you may decide to move such a dependency to a NuGet package. You can, at such time, decide to use an existing library instead of your own. </p> <p> The maintenance burden for these kinds of libraries is low, since the APIs and behaviour are defined and locked in advance by mathematics. </p> </div> <div class="comment-date">2022-08-16 5:54 UTC</div> </div> <div class="comment" id="fa7fc6662aee4889bcc9f84bc5db1b39"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#fa7fc6662aee4889bcc9f84bc5db1b39">#</a></div> <div class="comment-content"> <blockquote> If you stick with lawful implementations, the only variation possible is with naming. </blockquote> <p> There are also language-specific choices that can vary. </p> <p> One example involves applicative functors in C#. The "standard" API for applicative functors works well in <a href="https://blog.ploeh.dk/2018/10/01/applicative-functors/#6173a96aedaa4e97ad868c159e6d06fa">Haskell</a> and <a href="https://blog.ploeh.dk/2018/10/01/applicative-functors/#6f9b2f13951e475d8c4fc9682ad96a94">F#</a> because it is designed to be used with curried functions, and both of those languages curry their functions by default. In contrast, <a href="https://blog.ploeh.dk/2018/10/01/applicative-functors/#cef395ee19644f30bfd1ad7a84b6f912:~:text=Applicative%20functors%20push%20the%20limits%20of%20what%20you%20can%20express%20in%20C%23">applicative functors push the limits of what you can express in C#</a>. I am impressed with <a href="https://github.com/louthy/language-ext/blob/ba16d97d4909067222c8b134a80bfd6b7e54c424/LanguageExt.Tests/ValidationTests.cs#L277">the design that Language Ext uses for applicative functors</a>, which is an extension method on a (value) tuple of applicative functor instances that accepts a lambda expression that is given all the "unwrapped" values "inside" the applicative functors. </p> <p> Another example involves monads in TypeScript. To avoid the <a href="https://en.wikipedia.org/wiki/Pyramid_of_doom_(programming)">Pyramid of doom</a> when performing a sequence of monadic operations, Haskell has <a href="https://en.wikibooks.org/wiki/Haskell/do_notation">do notation</a> and F# has <a href="https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a>. There is no equivalent language feature in TypeScript, but it has <a href="https://en.wikipedia.org/wiki/Row_polymorphism">row polymorphism</a>, which <a href="https://gcanti.github.io/fp-ts/guides/do-notation.html">pf-ts uses to effectively implement do notation</a>. </p> <p> A related dimension is how to approximate high-kinded types in a language that lacks them. Language Ext passes in the monad as a type parameter as well as the "lower-kinded" type parameter and then constrains the monad type parameter to implement a monad interface parametereized by the lower type parameter as well as being a struct. I find that second constraint very intersting. Since the type parameter has a struct constraint, it has a default constructor that can be used to get an instance, which then implements methods according to the interface constraint. For more infomration, see <a href="https://github.com/louthy/language-ext/wiki/Does-C%23-Dream-Of-Electric-Monads%3F">this wiki article</a> for a gentle introduction and <a href="https://github.com/louthy/language-ext/blob/ba16d97d4909067222c8b134a80bfd6b7e54c424/LanguageExt.Core/Class%20Instances/Trans/Trans.cs#L74-L80">Trans.cs</a> for how Language Ext uses this approach to only implement traverse once. Similarly, F#+ has a feature called <a href="https://fsprojects.github.io/FSharpPlus/generic-doc.html">generic functions</a> that enable one to write F# like <code>map aFoo</code> instead of the typical <code>Foo.map aFoo</code>. </p> </div> <div class="comment-date">2022-09-20 02:00 UTC</div> </div> <div class="comment" id="5619079f7c994fc0bd0dbbdbfeec9e4a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5619079f7c994fc0bd0dbbdbfeec9e4a">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I agree that details differ. Clearly, this is true across languages, where, say, <a href="https://www.haskell.org/">Haskell</a>'s <code>fmap</code> has a name different from C#'s <code>SelectMany</code>. To state the obvious, the syntax is also different. </p> <p> Even within the same language, you can have variations. Functor mapping in Haskell is generally called <code>fmap</code>, but you can also use <code>map</code> explicitly for lists. The same could be true in C#. I've seen functor and monad implementations in C# that use method names like <code>Map</code> and <code>Bind</code> rather than <code>Select</code> and <code>SelectMany</code>. </p> <p> To expand on this idea, one may also observe that what one language calls <em>Option</em>, another language calls <em>Maybe</em>. The same goes for <code>Result</code> versus <code>Either</code>. </p> <p> As you know, the names <code>Select</code> and <code>SelectMany</code> are special because they enable C# query syntax. While methods named <code>Map</code> and <code>Bind</code> are 'the same' functions, they don't light up that language feature. Another way to enable syntactic sugar for monads in C# is via <code>async</code> and <code>await</code>, as <a href="https://eiriktsarpalis.wordpress.com/2020/07/20/effect-programming-in-csharp/">shown by Eirik Tsarpalis and Nick Palladinos</a>. </p> <p> I do agree with you that there are various options available to an implementer. The point I was trying to make is that while implementation details differ, the concepts are the same. Thus, as a <em>user</em> of one of these APIs (monads, monoids, etc.) you only have to learn the mental model once. You still have to learn the implementation details. </p> <p> I recently heard a professor at <a href="https://en.wikipedia.org/wiki/UCPH_Department_of_Computer_Science">DIKU</a> state that once you know one programming language, you should be able to learn another one in a week. That's the same general idea. </p> <p> (I do, however, have issues with that statement about programming languages as a universal assertion, but I agree that it tends to hold for mainstream languages. When I read <a href="/ref/mazes-for-programmers">Mazes for Programmers</a> I'd never programmed in <a href="https://www.ruby-lang.org/en/">Ruby</a> before, but I had little trouble picking it up for the exercises. On the other hand, most people don't learn Haskell in a week.) </p> </div> <div class="comment-date">2022-09-20 17:42 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Natural transformations https://blog.ploeh.dk/2022/07/18/natural-transformations 2022-07-18T08:12:00+00:00 Mark Seemann <div id="post"> <p> <em>Mappings between functors, with some examples in C#.</em> </p> <p> This article is part of <a href="/2022/07/11/functor-relationships">a series of articles about functor relationships</a>. In this one you'll learn about natural transformations, which are simply mappings between two <a href="/2018/03/22/functors">functors</a>. It's probably the easiest relationship to understand. In fact, it may be so obvious that your reaction is: <em>Is that it?</em> </p> <p> In programming, a natural transformation is just a function from one functor to another. A common example is a function that tries to extract a value from a collection. You'll see specific examples a little later in this article. </p> <h3 id="05a07e6563da4ba39c83bd92250d3056"> Laws <a href="#05a07e6563da4ba39c83bd92250d3056" title="permalink">#</a> </h3> <p> In this, the dreaded section on <em>laws</em>, I have a nice surprise for you: There aren't any (that we need worry about)! </p> <p> In the broader context of <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a> there are, in fact, rules that a natural transformation must follow. </p> <blockquote> <p> "Haskell's parametric polymorphism has an unexpected consequence: any polymorphic function of the type: </p> <p> <pre>alpha :: F a -&gt; G a</pre> </p> <p> "where <code>F</code> and <code>G</code> are functors, automatically satisfies the naturality condition." </p> <footer><cite><a href="https://bartoszmilewski.com/2015/04/07/natural-transformations/">Natural Transformations</a>, Bartosz Milewski</cite></footer> </blockquote> <p> While C# isn't <a href="https://www.haskell.org">Haskell</a>, .NET generics are similar enough to Haskell <a href="https://en.wikipedia.org/wiki/Parametric_polymorphism">parametric polymorphism</a> that the result, as far as I can tell, carry over. (Again, however, we have to keep in mind that C# doesn't distinguish between <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> and impure actions. The knowledge that I infer translates for pure functions. For impure actions, there are no guarantees.) </p> <p> The C# equivalent of the above <code>alpha</code> function would be a method like this: </p> <p> <pre>G&lt;T&gt;&nbsp;<span style="color:#74531f;">Alpha</span>&lt;<span style="color:#2b91af;">T</span>&gt;(F&lt;T&gt;&nbsp;<span style="color:#1f377f;">f</span>)</pre> </p> <p> where both <code>F</code> and <code>G</code> are functors. </p> <h3 id="643a53f5758f485f8a7000f9e7825c5c"> Safe head <a href="#643a53f5758f485f8a7000f9e7825c5c" title="permalink">#</a> </h3> <p> Natural transformations easily occur in normal programming. You've probably written some yourself, without being aware of it. Here are some examples. </p> <p> It's common to attempt to get the first element of a collection. Collections, however, may be empty, so this is not always possible. In Haskell, you'd model that as a function that takes a list as input and returns a <code>Maybe</code> as output: </p> <p> <pre>Prelude Data.Maybe&gt; :t listToMaybe listToMaybe :: [a] -&gt; Maybe a Prelude Data.Maybe&gt; listToMaybe [] Nothing Prelude Data.Maybe&gt; listToMaybe [7] Just 7 Prelude Data.Maybe&gt; listToMaybe [3,9] Just 3 Prelude Data.Maybe&gt; listToMaybe [5,9,2,4,4] Just 5</pre> </p> <p> In many tutorials such a function is often called <code>safeHead</code>, because it returns the <em>head</em> of a list (i.e. the first item) in a safe manner. It returns <code>Nothing</code> if the list is empty. In <a href="https://fsharp.org">F#</a> this function is called <a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-seqmodule.html#tryHead">tryHead</a>. </p> <p> In C# you could write a similar function like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Maybe&lt;T&gt;&nbsp;<span style="color:#74531f;">TryFirst</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(source.Any()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;T&gt;(source.First()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Maybe.Empty&lt;T&gt;(); }</pre> </p> <p> This extension method (which is really a pure function) is a natural transformation between two functors. The source functor is the <a href="/2022/04/19/the-list-monad">list functor</a> and the destination is <a href="/2018/03/26/the-maybe-functor">the Maybe functor</a>. </p> <p> Here are some unit tests that demonstrate how it works: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">TryFirstWhenEmpty</span>() { &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;Guid&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;Enumerable.Empty&lt;Guid&gt;().TryFirst(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Maybe.Empty&lt;Guid&gt;(),&nbsp;actual); } [Theory] [InlineData(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>&nbsp;},&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>&nbsp;},&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>)] [InlineData(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;quux&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;quuz&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;corge&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;corge&quot;</span>&nbsp;},&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">TryFirstWhenNotEmpty</span>(<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">arr</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;arr.TryFirst(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">string</span>&gt;(expected),&nbsp;actual); }</pre> </p> <p> All these tests pass. </p> <h3 id="6d899e8f2a7948ed898eaaefc3064dc6"> Safe index <a href="#6d899e8f2a7948ed898eaaefc3064dc6" title="permalink">#</a> </h3> <p> The above <em>safe head</em> natural transformation is just one example. Even for a particular combination of functors like <em>List to Maybe</em> many natural transformations may exist. For this particular combination, there are infinitely many natural transformations. </p> <p> You can view the <em>safe head</em> example as a special case of a more general set of <em>safe indexing</em>. With a collection of values, you can attempt to retrieve the value at a particular index. Since a collection can contain an arbitrary number of elements, however, there's no guarantee that there's an element at the requested index. </p> <p> In order to avoid exceptions, then, you can try to retrieve the value at an index, getting a Maybe value as a result. </p> <p> The F# <code>Seq</code> module defines a function called <a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-seqmodule.html#tryItem">tryItem</a>. This function takes an index and a sequence (<code>IEnumerable&lt;T&gt;</code>) and returns an <code>option</code> (F#'s name for Maybe): </p> <p> <pre>&gt; Seq.tryItem 2 [2;5;3;5];; val it : int option = Some 3</pre> </p> <p> The <code>tryItem</code> function itself is <em>not</em> a natural transformation, but because of currying, it's a function that <em>returns</em> a natural transformation. When you partially apply it with an index, it becomes a natural transformation: <code>Seq.tryItem 3</code> is a natural transformation <code>seq&lt;'a&gt; -&gt; 'a option</code>, as is <code>Seq.tryItem 4</code>, <code>Seq.tryItem 5</code>, and so on ad infinitum. Thus, there are infinitely many natural transformations from the List functor to the Maybe functor, and <em>safe head</em> is simply <code>Seq.tryItem 0</code>. </p> <p> In C# you can use the various <code>Func</code> delegates to implement currying, but if you want something that looks a little more object-oriented, you could write code like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Index</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;index; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Index</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">index</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.index&nbsp;=&nbsp;index; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Maybe&lt;T&gt;&nbsp;<span style="color:#74531f;">TryItem</span>&lt;<span style="color:#2b91af;">T</span>&gt;(IEnumerable&lt;T&gt;&nbsp;<span style="color:#1f377f;">values</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">candidate</span>&nbsp;=&nbsp;values.Skip(index).Take(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(candidate.Any()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;T&gt;(candidate.First()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Maybe.Empty&lt;T&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This <code>Index</code> class captures an index value for potential use against any <code>IEnumerable&lt;T&gt;</code>. Thus, the <code>TryItem</code> method is a natural transformation from the List functor to the Maybe functor. Here are some examples: </p> <p> <pre>[Theory] [InlineData(0,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>[0])] [InlineData(1,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;bee&quot;</span>&nbsp;})] [InlineData(2,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;nig&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;fev&quot;</span>&nbsp;})] [InlineData(4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;sta&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;ali&quot;</span>&nbsp;})] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">MissItem</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>,&nbsp;<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">xs</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">idx</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Index(i); &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;idx.TryItem(xs); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Maybe.Empty&lt;<span style="color:blue;">string</span>&gt;(),&nbsp;actual); } [Theory] [InlineData(0,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>&nbsp;},&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(1,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>&nbsp;},&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>)] [InlineData(1,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;quux&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;quuz&quot;</span>&nbsp;},&nbsp;<span style="color:#a31515;">&quot;quux&quot;</span>)] [InlineData(2,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;corge&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;grault&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;fred&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;garply&quot;</span>&nbsp;},&nbsp;<span style="color:#a31515;">&quot;fred&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FindItem</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>,&nbsp;<span style="color:blue;">string</span>[]&nbsp;<span style="color:#1f377f;">xs</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">idx</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Index(i); &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;idx.TryItem(xs); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">string</span>&gt;(expected),&nbsp;actual); }</pre> </p> <p> Since there are infinitely many integers, there are infinitely many such natural transformations. (This is strictly not true for the above code, since there's a finite number of 32-bit integers. Exercise: Is it possible to rewrite the above <code>Index</code> class to instead work with <a href="https://docs.microsoft.com/dotnet/api/system.numerics.biginteger">BigInteger</a>?) </p> <p> The Haskell <a href="https://hackage.haskell.org/package/natural-transformation">natural-transformation</a> package offers an even more explicit way to present the same example: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;Control.Natural <span style="color:#2b91af;">tryItem</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Num</span>&nbsp;a,&nbsp;<span style="color:blue;">Enum</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[]&nbsp;:~&gt;&nbsp;<span style="color:#2b91af;">Maybe</span> tryItem&nbsp;i&nbsp;=&nbsp;NT&nbsp;$&nbsp;<span style="color:blue;">lookup</span>&nbsp;i&nbsp;.&nbsp;<span style="color:blue;">zip</span>&nbsp;[0..]</pre> </p> <p> You can view this <code>tryItem</code> function as a function that takes a number and returns a particular natural transformation. For example you can define a value called <code>tryThird</code>, which is a natural transformation from <code>[]</code> to <code>Maybe</code>: </p> <p> <pre>λ tryThird = tryItem 2 λ :t tryThird tryThird :: [] :~&gt; Maybe</pre> </p> <p> Here are some usage examples: </p> <p> <pre>λ tryThird # [] Nothing λ tryThird # [1] Nothing λ tryThird # [2,3] Nothing λ tryThird # [4,5,6] Just 6 λ tryThird # [7,8,9,10] Just 9</pre> </p> <p> In all three languages (F#, C#, Haskell), <em>safe head</em> is really just a special case of <em>safe index</em>: <code>Seq.tryItem 0</code> in F#, <code><span style="color:blue;">new</span>&nbsp;Index(0)</code> in C#, and <code>tryItem 0</code> in Haskell. </p> <h3 id="a8e50cd940974b88b5564c2939900702"> Maybe to List <a href="#a8e50cd940974b88b5564c2939900702" title="permalink">#</a> </h3> <p> You can also move in the opposite direction: From Maybe to List. In F#, I can't find a function that translates from <code>option 'a</code> to <code>seq 'a</code> (<code>IEnumerable&lt;T&gt;</code>), but there are both <a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-optionmodule.html#toArray">Option.toArray</a> and <a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-optionmodule.html#toList">Option.toList</a>. I'll use <code>Option.toList</code> for a few examples: </p> <p> <pre>&gt; Option.toList (None : string option);; val it : string list = [] &gt; Option.toList (Some "foo");; val it : string list = ["foo"]</pre> </p> <p> Contrary to translating from List to Maybe, going the other way there aren't a lot of options: <code>None</code> translates to an empty list, and <code>Some</code> translates to a singleton list. </p> <p> Using <a href="/2018/06/25/visitor-as-a-sum-type">a Visitor-based</a> Maybe in C#, you can implement the natural transformation like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;<span style="color:#74531f;">ToList</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;IMaybe&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.Accept(<span style="color:blue;">new</span>&nbsp;ToListVisitor&lt;T&gt;()); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ToListVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;IMaybeVisitor&lt;T,&nbsp;IEnumerable&lt;T&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;VisitNothing &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Enumerable.Empty&lt;T&gt;();&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;<span style="color:#74531f;">VisitJust</span>(T&nbsp;<span style="color:#1f377f;">just</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;just&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Here are some examples: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">NothingToList</span>() { &nbsp;&nbsp;&nbsp;&nbsp;IMaybe&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">maybe</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Nothing&lt;<span style="color:blue;">double</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;maybe.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Empty(actual); } [Theory] [InlineData(-1)] [InlineData(&nbsp;0)] [InlineData(15)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">JustToList</span>(<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">d</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IMaybe&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">maybe</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Just&lt;<span style="color:blue;">double</span>&gt;(d); &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;maybe.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Single(actual,&nbsp;d); }</pre> </p> <p> In Haskell this natural transformation is called <a href="https://hackage.haskell.org/package/base/docs/Data-Maybe.html#v:maybeToList">maybeToList</a> - just when you think that Haskell names are always <a href="/2021/06/07/abstruse-nomenclature">abstruse</a>, you learn that some are very explicit and self-explanatory. </p> <p> If we wanted, we could use the <em>natural-transformation</em> package to demonstrate that this is, indeed, a natural transformation: </p> <p> <pre>λ :t NT maybeToList NT maybeToList :: Maybe :~&gt; []</pre> </p> <p> There would be little point in doing so, since we'd need to unwrap it again to use it. Using the function directly, on the other hand, looks like this: </p> <p> <pre>λ maybeToList Nothing [] λ maybeToList $ Just 2 [2] λ maybeToList $ Just "fon" ["fon"]</pre> </p> <p> A <code>Nothing</code> value is always translated to the empty list, and a <code>Just</code> value to a singleton list, exactly as in the other languages. </p> <p> Exercise: Is this the only possible natural transformation from Maybe to List? </p> <h3 id="5dfc02dd10514c2288721ace174c7229"> Maybe-Either relationships <a href="#5dfc02dd10514c2288721ace174c7229" title="permalink">#</a> </h3> <p> The Maybe functor is isomorphic to <a href="/2019/01/14/an-either-functor">Either</a> where the <em>left</em> (or <em>error</em>) dimension is <a href="/2018/01/15/unit-isomorphisms">unit</a>. Here are the two natural transformations in F#: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Option&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;&#39;a&nbsp;option&nbsp;-&gt;&nbsp;Result&lt;&#39;a,unit&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;toResult&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Result&lt;&#39;a,unit&gt;&nbsp;-&gt;&nbsp;&#39;a&nbsp;option</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ofResult&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Ok&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Some&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Error&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None</pre> </p> <p> In F#, Maybe is called <code>option</code> and Either is called <code>Result</code>. Be aware that the F# <code>Result</code> discriminated union puts the <code>Error</code> dimension to the right of the <code>Ok</code>, which is opposite of Either, where <em>left</em> is usually used for errors, and <em>right</em> for successes (because what is correct is right). </p> <p> Here are some examples: </p> <p> <pre>&gt; Some "epi" |&gt; Option.toResult;; val it : Result&lt;string,unit&gt; = Ok "epi" &gt; Ok "epi" |&gt; Option.ofResult;; val it : string option = Some "epi"</pre> </p> <p> Notice that the natural transformation from <code>Result</code> to <code>Option</code> is only defined for <code>Result</code> values where the <code>Error</code> type is <code>unit</code>. You could also define a natural transformation from <em>any</em> <code>Result</code> to <code>option</code>: </p> <p> <pre><span style="color:green;">//&nbsp;Result&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;&#39;a&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;ignoreErrorValue&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Ok&nbsp;x&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Some&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Error&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None</pre> </p> <p> That's still a natural transformation, but no longer part of an isomorphism due to the loss of information: </p> <p> <pre>&gt; (Error "Catastrophic failure" |&gt; ignoreErrorValue : int option);; val it : int option = None</pre> </p> <p> Just like above, when examining the infinitely many natural transformations from List to Maybe, we can use the Haskell <em>natural-transformation</em> package to make this more explicit: </p> <p> <pre><span style="color:#2b91af;">ignoreLeft</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;b&nbsp;:~&gt;&nbsp;<span style="color:#2b91af;">Maybe</span> ignoreLeft&nbsp;=&nbsp;NT&nbsp;$&nbsp;either&nbsp;(<span style="color:blue;">const</span>&nbsp;Nothing)&nbsp;Just</pre> </p> <p> <code>ignoreLeft</code> is a natural transformation from the <code>Either b</code> functor to the <code>Maybe</code> functor. </p> <p> Using a Visitor-based Either implementation (refactored from <a href="/2018/06/11/church-encoded-either">Church-encoded Either</a>), you can implement an equivalent <code>IgnoreLeft</code> natural transformation in C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IMaybe&lt;R&gt;&nbsp;<span style="color:#74531f;">IgnoreLeft</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:blue;">this</span>&nbsp;IEither&lt;L,&nbsp;R&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.Accept(<span style="color:blue;">new</span>&nbsp;IgnoreLeftVisitor&lt;L,&nbsp;R&gt;()); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IgnoreLeftVisitor</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;:&nbsp;IEitherVisitor&lt;L,&nbsp;R,&nbsp;IMaybe&lt;R&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IMaybe&lt;R&gt;&nbsp;<span style="color:#74531f;">VisitLeft</span>(L&nbsp;<span style="color:#1f377f;">left</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Nothing&lt;R&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IMaybe&lt;R&gt;&nbsp;<span style="color:#74531f;">VisitRight</span>(R&nbsp;<span style="color:#1f377f;">right</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Just&lt;R&gt;(right); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Here are some examples: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;OMG!&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;Catastrophic&nbsp;failure&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;Important&nbsp;information!&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IgnoreLeftOfLeft</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">msg</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">e</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Left&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(msg); &nbsp;&nbsp;&nbsp;&nbsp;IMaybe&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;e.IgnoreLeft(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>&nbsp;Nothing&lt;<span style="color:blue;">int</span>&gt;(),&nbsp;actual); } [Theory] [InlineData(0)] [InlineData(1)] [InlineData(2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IgnoreLeftOfRight</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">e</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Right&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;IMaybe&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;e.IgnoreLeft(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>&nbsp;Just&lt;<span style="color:blue;">int</span>&gt;(i),&nbsp;actual); }</pre> </p> <p> I'm not insisting that this natural transformation is always useful, but I've occasionally found myself in situations were it came in handy. </p> <h3 id="cc17fdc2940948978ff8bea222503965"> Natural transformations to or from Identity <a href="#cc17fdc2940948978ff8bea222503965" title="permalink">#</a> </h3> <p> Some natural transformations are a little less obvious. If you have a <code><span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> class as shown in my article <a href="/2017/12/11/semigroups-accumulate">Semigroups accumulate</a>, you could consider the <code>Head</code> property a natural transformation. It translates a <code><span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> object to a <code>T</code> object. </p> <p> This function also exists in Haskell, where it's simply called <a href="https://hackage.haskell.org/package/base/docs/Data-List-NonEmpty.html#v:head">head</a>. </p> <p> The input type (<code><span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> in C#, <code>NonEmpty a</code> in Haskell) is a functor, but the return type is a 'naked' value. That doesn't look like a functor. </p> <p> True, a naked value isn't a functor, but it's isomorphic to <a href="/2018/09/03/the-identity-functor">the Identity functor</a>. In Haskell, you can make that relationship quite explicit: </p> <p> <pre><span style="color:#2b91af;">headNT</span>&nbsp;::&nbsp;<span style="color:blue;">NonEmpty</span>&nbsp;:~&gt;&nbsp;<span style="color:blue;">Identity</span> headNT&nbsp;=&nbsp;NT&nbsp;$&nbsp;Identity&nbsp;.&nbsp;NonEmpty.<span style="color:blue;">head</span></pre> </p> <p> While not particularly useful in itself, this demonstrates that it's possible to think of the <code>head</code> function as a natural transformation from <code>NonEmpty</code> to <code>Identity</code>. </p> <p> Can you go the other way, too? </p> <p> Yes, indeed. Consider <a href="/2022/03/28/monads">monadic return</a>. This is a function that takes a 'naked' value and wraps it in a particular monad (which is also, always, a functor). Again, you may consider the 'naked' value as isomorphic with the Identity functor, and thus <em>return</em> as a natural transformation: </p> <p> <pre><span style="color:#2b91af;">returnNT</span>&nbsp;::&nbsp;<span style="color:blue;">Monad</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Identity</span>&nbsp;:~&gt;&nbsp;m returnNT&nbsp;=&nbsp;NT&nbsp;$&nbsp;<span style="color:blue;">return</span>&nbsp;.&nbsp;runIdentity</pre> </p> <p> We might even consider if a function <code>a -&gt; a</code> (in Haskell syntax) or <code>Func&lt;T, T&gt;</code> (in C# syntax) might actually be a natural transformation from Identity to Identity... (It is, but only one such function exists.) </p> <h3 id="335f008f8f65439e8253bf96968af169"> Not all natural transformations are useful <a href="#335f008f8f65439e8253bf96968af169" title="permalink">#</a> </h3> <p> Are are all functor combinations possible as natural transformations? Can you take any two functors and define one or more natural transformations? I'm not sure, but it seems clear that even if it is so, not all natural transformations are useful. </p> <p> Famously, for example, you can't <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">get the value out of</a> the <a href="/2020/06/22/the-io-functor">IO functor</a>. Thus, at first glance it seems impossible to define a natural transformation <em>from</em> <code>IO</code> to some other functor. After all, how would you implement a natural transformation from <code>IO</code> to, say, the Identity functor. That seems impossible. </p> <p> On the other hand, <em>this</em> is possible: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;<span style="color:#74531f;">Collapse</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;IO&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">break</span>; }</pre> </p> <p> That's a natural transformation from <code>IO&lt;T&gt;</code> to <code>IEnumerable&lt;T&gt;</code>. It's possible to ignore the input value and <em>always</em> return an empty sequence. This natural transformation collapses all values to a single return value. </p> <p> You can repeat this exercise with the Haskell <em>natural-transformation</em> package: </p> <p> <pre><span style="color:#2b91af;">collapse</span>&nbsp;::&nbsp;f&nbsp;:~&gt;&nbsp;[] collapse&nbsp;=&nbsp;NT&nbsp;$&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">[]</span></pre> </p> <p> This one collapses <em>any</em> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers/">container</a> <code>f</code> to a List (<code>[]</code>), including <code>IO</code>: </p> <p> <pre>λ collapse # (return 10 :: IO Integer) [] λ collapse # putStrLn "ploeh" []</pre> </p> <p> Notice that in the second example, the <code>IO</code> action is <code>putStrLn "ploeh"</code>, which ought to produce the side effect of writing to the console. This is effectively prevented - instead the <code>collapse</code> natural transformation simply produces the empty list as output. </p> <p> You can define a similar natural transformation from any functor (including <code>IO</code>) to Maybe. Try it as an exercise, in either C#, Haskell, or another language. If you want a Haskell-specific exercise, also define a natural transformation of this type: <code><span style="color:blue;">Alternative</span>&nbsp;g&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;f&nbsp;:~&gt;&nbsp;g</code>. </p> <p> These natural transformations are possible, but hardly useful. </p> <h3 id="103675f9845b4aa1b32f59a510280a53"> Conclusion <a href="#103675f9845b4aa1b32f59a510280a53" title="permalink">#</a> </h3> <p> A natural transformation is a function that translates one functor into another. Useful examples are safe or total collection indexing, including retrieving the first element from a collection. These natural transformations return a populated Maybe value if the element exists, and an empty Maybe value otherwise. </p> <p> Other examples include translating Maybe values into Either values or Lists. </p> <p> A natural transformation can easily involve loss of information. Even if you're able to retrieve the first element in a collection, the return value includes only that value, and not the rest of the collection. </p> <p> A few natural transformations may be true isomorphisms, but in general, being able to go in both directions isn't required. In degenerate cases, a natural transformation may throw away all information and map to a general empty value like the empty List or an empty Maybe value. </p> <p> <strong>Next:</strong> Traversals. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functor relationships https://blog.ploeh.dk/2022/07/11/functor-relationships 2022-07-11T08:09:00+00:00 Mark Seemann <div id="post"> <p> <em>Sometimes you need to use more than one functor together.</em> </p> <p> This article series is part of <a href="/2018/03/19/functors-applicatives-and-friends">a larger series of articles about functors, applicatives, and other mappable containers</a>. Particularly, you've seen examples of both <a href="/2018/03/22/functors">functors</a> and <a href="/2018/10/01/applicative-functors">applicative functors</a>. </p> <p> There are situations where you can get by with a single functor. Many languages come with list comprehensions or other features to work with collections of values (C#, for instance, has <em>language-integrated query</em>, or: LINQ). The <a href="/2022/04/19/the-list-monad">list functor (and monad)</a> gives you a comprehensive API to manipulate multiple values. Likewise, you may write some parsing (<a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate">or validation</a>) that exclusively uses the <a href="/2019/01/14/an-either-functor">Either functor</a>. </p> <p> At other times, however, you may find yourself having to juggle more than one functor at once. Perhaps you are working with Either values, but one existing API returns <a href="/2018/03/26/the-maybe-functor">Maybe</a> values instead. Or perhaps you need to deal with Either values, but you're already working within <a href="/2018/09/24/asynchronous-functors">an asynchronous functor</a>. </p> <p> There are several standard ways you can combine or transform combinations of functors. </p> <h3 id="70383da35af346f6b2dc3095a1bf7273"> A partial catalogue <a href="#70383da35af346f6b2dc3095a1bf7273" title="permalink">#</a> </h3> <p> The following relationships often come in handy - particularly those that top this list: </p> <ul> <li><a href="/2022/07/18/natural-transformations">Natural transformations</a></li> <li>Traversals</li> <li>Functor compositions</li> <li>Functor products</li> <li>Functor sums</li> </ul> <p> This list is hardly complete, and I may add to it in the future. Compared to some of the other subtopics of <a href="/2017/10/04/from-design-patterns-to-category-theory">the larger articles series on universal abstractions</a>, this catalogue is more heterogeneous. It collects various ways that functors can relate to each other, but uses disparate concepts and abstractions, rather than a single general idea (like a <a href="/2018/12/24/bifunctors">bifunctor</a>, <a href="/2017/10/06/monoids">monoid</a>, or <a href="/2019/04/29/catamorphisms">catamorphism</a>). </p> <p> Keep in mind when reading these articles that all <a href="/2022/03/28/monads">monads</a> are also functors and applicative functors, so what applies to functors also applies to monads. </p> <h3 id="1a4f0850f8974b59ab44eed352b0daf7"> Conclusion <a href="#1a4f0850f8974b59ab44eed352b0daf7" title="permalink">#</a> </h3> <p> You can use a single functor in isolation, or you can combine more than one. Most of the relationships described in this articles series work for all (lawful) functors, but traversals require applicative functors and functors that are 'foldable' (i.e. a catamorphism exists). </p> <p> <strong>Next:</strong> <a href="/2022/07/18/natural-transformations">Natural transformations</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Get and Put State https://blog.ploeh.dk/2022/07/04/get-and-put-state 2022-07-04T09:15:00+00:00 Mark Seemann <div id="post"> <p> <em>A pair of standard helper functions for the State monad. An article for object-oriented programmers.</em> </p> <p> The <a href="/2022/06/20/the-state-monad">State monad</a> is completely defined by <a href="/2022/03/28/monads">its two defining functions</a> (<code>SelectMany</code> and <code>Return</code>). While you can get by without them, two additional helper functions (<em>get</em> and <em>put</em>) are so convenient that they're typically included. To be clear, they're not part of the State <em>monad</em> - rather, you can consider them part of what we may term a <em>standard State API</em>. </p> <p> In short, <em>get</em> is a function that, as the name implies, gets the state while inside the State monad, and <em>put</em> replaces the state with a new value. </p> <p> Later in this article, I'll show how to implement these two functions, as well as a usage example. Before we get to that, however, I want to show a motivating example. In other words, an example that doesn't use <em>get</em> and <em>put</em>. </p> <p> The code shown in this article uses the C# State implementation from <a href="/2022/06/20/the-state-monad">the State monad article</a>. </p> <h3 id="62936af6492e474cb1f6afd919bc2cde"> Aggregator <a href="#62936af6492e474cb1f6afd919bc2cde" title="permalink">#</a> </h3> <p> Imagine that you have to implement a simple <a href="https://www.enterpriseintegrationpatterns.com/Aggregator.html">Aggregator</a>. </p> <blockquote> <p> "How do we combine the results of individual but related messages so that they can be processed as a whole?" </p> <p> [...] "Use a stateful filter, an <em>Aggregator</em>, to collect and store individual messages until it receives a complete set of related messages. Then, the <em>Aggregator</em> publishes a single message distilled from the individual messages." </p> <footer><cite><a href="/ref/eip">Enterprise Integration Patterns</a></cite></footer> </blockquote> <p> The example that I'll give here is simplified and mostly focuses on how to use the State monad to implement the desired behaviour. The book Enterprise Integration Patterns starts with a simple example where messages arrive with a <a href="https://www.enterpriseintegrationpatterns.com/CorrelationIdentifier.html">correlation ID</a> as an integer. The message payload is also a an integer, just to keep things simple. The Aggregator should only publish an aggregated message once it has received three correlated messages. </p> <p> Using the State monad, you could implement an Aggregator like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Aggregator</span>&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;IReadOnlyDictionary&lt;<span style="color:blue;">int</span>,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&gt;,&nbsp;Maybe&lt;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;correlationId; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Aggregator</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">correlationId</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.correlationId&nbsp;=&nbsp;correlationId; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;Maybe&lt;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;,&nbsp;IReadOnlyDictionary&lt;<span style="color:blue;">int</span>,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Run</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IReadOnlyDictionary&lt;<span style="color:blue;">int</span>,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(state.TryGetValue(correlationId,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">coll</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(coll.Count&nbsp;==&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">retVal</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tuple.Create(coll.ElementAt(0),&nbsp;coll.ElementAt(1),&nbsp;value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newState</span>&nbsp;=&nbsp;state.Remove(correlationId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(retVal.ToMaybe(),&nbsp;newState); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newColl</span>&nbsp;=&nbsp;coll.Append(value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newState</span>&nbsp;=&nbsp;state.Replace(correlationId,&nbsp;newColl); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(<span style="color:blue;">new</span>&nbsp;Maybe&lt;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;(),&nbsp;newState); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newColl</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;value&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newState</span>&nbsp;=&nbsp;state.Add(correlationId,&nbsp;newColl); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(<span style="color:blue;">new</span>&nbsp;Maybe&lt;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;(),&nbsp;newState); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Aggregator</code> class implements the <code>IState&lt;S, T&gt;</code> interface. The full generic type is something of a mouthful, though. </p> <p> The state type (<code>S</code>) is <code>IReadOnlyDictionary&lt;<span style="color:blue;">int</span>,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&gt;</code> - in other words, a dictionary of collections. Each entry in the dictionary is keyed by a correlation ID. Each value is a collection of messages that belong to that ID. Keep in mind that, in order to keep the example simple, each message is just a number (an <code>int</code>). </p> <p> The value to produce (<code>T</code>) is <code>Maybe&lt;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;</code>. This code example uses <a href="/2022/04/25/the-maybe-monad">this implementation of the Maybe monad</a>. The value produced may or may not be empty, depending on whether the Aggregator has received all three required messages in order to produce an aggregated message. Again, for simplicity, the aggregated message is just a triple (a three-tuple). </p> <p> The <code>Run</code> method starts by querying the <code>state</code> dictionary for an entry that corresponds to the <code>correlationId</code>. This entry may or may not be present. If the message is the first in a series of three, there will be no entry, but if it's the second or third message, the entry will be present. </p> <p> In that case, the <code>Run</code> method checks the <code>Count</code> of the collection. If the <code>Count</code> is <code>2</code>, it means that two other messages with the same <code>correlationId</code> was already received. This means that the <code>Aggregator</code> is now handling the third and final message. Thus, it creates the <code>retVal</code> tuple, removes the entry from the dictionary to create the <code>newState</code>, and returns both. </p> <p> If the <code>state</code> contains an entry for the <code>correlationId</code>, but the <code>Count</code> isn't <code>2</code>, the <code>Run</code> method updates the entry by appending the <code>value</code>, updating the state to <code>newState</code>, and returns that together with an empty Maybe value. </p> <p> Finally, if there is no entry for the <code>correlationId</code>, the <code>Run</code> method creates a new collection containing only the <code>value</code>, adds it to the <code>state</code> dictionary, and returns the <code>newState</code> together with an empty Maybe value. </p> <h3 id="f8ed5ee8f1eb4f1499e283f2383962e9"> Message handler <a href="#f8ed5ee8f1eb4f1499e283f2383962e9" title="permalink">#</a> </h3> <p> A message handler could be a background service that receives messages from a durable queue, a REST endpoint, or based on some other technology. </p> <p> After it receives a message, a message handler would create a new instance of the <code>Aggregator</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">a</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Aggregator(msg.CorrelationId,&nbsp;msg.Value);</pre> </p> <p> Since <code>Aggregator</code> implements the <code>IState&lt;S, T&gt;</code> interface, the object <code>a</code> represents a stateful computation. A message handler might keep the current state in memory, or rehydrate it from some persistent storage system. Keep in mind that the state must be of the type <code>IReadOnlyDictionary&lt;<span style="color:blue;">int</span>,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&gt;</code>. Wherever it comes from, assume that this state is a variable called <code>s</code> (for <em>state</em>). </p> <p> The message handler can now <code>Run</code> the stateful computation by supplying <code>s</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;a.Run(s);</pre> </p> <p> The result is a tuple where the first item is a Maybe value, and the second item is the new state. </p> <p> The message handler can now publish the triple if the Maybe value is populated. In any case, it can update the 'current' state with the new state. That's a nice little <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>. </p> <p> Notice how this design is different from a typical object-oriented solution. In object-oriented programming, you'd typically have an object than contains the state and then receives the run-time value as input to a method that might then mutate the state. <em>Data with behaviour</em>, as it's sometimes characterised. </p> <p> The State-based computation turns such a design on its head. The computation closes over the run-time values, and the state is supplied as an argument to the <code>Run</code> method. This is an example of the shift of perspective often required to think functionally, rather than object-oriented. <em>That's</em> why it takes time learning Functional Programming (FP); it's not about syntax. It's a different way to think. </p> <p> An object like the above <code>a</code> seems almost frivolous, since it's going to have a short lifetime. Calling code will create it only to call its <code>Run</code> method and then let it go out of scope to be garbage-collected. </p> <p> Of course, in a language more attuned to FP like <a href="https://www.haskell.org">Haskell</a>, it's a different story: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;h&nbsp;=&nbsp;handle&nbsp;(corrId&nbsp;msg)&nbsp;(val&nbsp;msg)</pre> </p> <p> Instead of creating an object using a constructor, you only pass the message values to a function called <code>handle</code>. The return value <code>h</code> is a State value that an overall message handler can then later run with a state <code>s</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;(m,&nbsp;ns)&nbsp;=&nbsp;runState&nbsp;h&nbsp;s</pre> </p> <p> The return value is a tuple where <code>m</code> is the Maybe value that may or may not contain the aggregated message; <code>ns</code> is the new state. </p> <h3 id="6904a24cf4a242c98b6732b5fcb8a3d1"> Is this better? <a href="#6904a24cf4a242c98b6732b5fcb8a3d1" title="permalink">#</a> </h3> <p> Is this approach to state mutation better than the default kind of state mutation possible with most languages (including C#)? Why make things so complicated? </p> <p> There's more than one answer. First, in a language like Haskell, state mutation is in general not possible. While you <em>can</em> do state mutation with <a href="/2020/06/08/the-io-container">the IO container</a> in Haskell, this sets you completely free. You don't want to be free, because with freedom comes innumerable ways to shoot yourself in the foot. <a href="https://www.dotnetrocks.com/?show=1542">Constraints liberate</a>. </p> <p> While the IO monad allows uncontrolled state mutation (together with all sorts of other impure actions), the State monad constrains itself and callers to only one type of apparent mutation. The type of the state being 'mutated' is visible in the type system, and that's the only type of value you can 'mutate' (in Haskell, that is). </p> <p> The State monad uses the type system to clearly communicate what the type of state is. Given a language like Haskell, or otherwise given sufficient programming discipline, you can tell from an object's type exactly what to expect. </p> <p> This also goes a long way to explain why monads are such an important concept in Functional Programming. When discussing FP, a common question is: <em>How do you perform side effects?</em> The answer, as may be already implied by this article, is that you use <a href="/2022/03/28/monads">monads</a>. The State monad for local state mutation, and the IO monad for 'global' side effects. </p> <h3 id="598094a7efc64e2685cf6196415bca01"> Get <a href="#598094a7efc64e2685cf6196415bca01" title="permalink">#</a> </h3> <p> Clearly you can write an implementation of <code>IState&lt;S, T&gt;</code> like the above <code>Aggregator</code> class. Must we always write a class that implements the interface in order to work within the State monad? </p> <p> Monads are all about composition. Usually, you can compose even complex behaviour from smaller building blocks. Just consider the <a href="/2022/04/19/the-list-monad">list monad</a>, which in C# is epitomised by the <code>IEnumerable&lt;T&gt;</code> interface. You can write quite complex logic using the building blocks of <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.where">Where</a>, <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.select">Select</a>, <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.aggregate">Aggregate</a>, <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.zip">Zip</a>, etcetera. </p> <p> Likewise, we should expect that to be the case with the State monad, and it is so. The useful extra combinators are <em>get</em> and <em>put</em>. </p> <p> The <em>get</em> function enables a composition to retrieve the current state. Given the <code>IState&lt;S, T&gt;</code> interface, you can implement it like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Get</span>&lt;<span style="color:#2b91af;">S</span>&gt;() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;GetState&lt;S&gt;(); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">GetState</span>&lt;<span style="color:#2b91af;">S</span>&gt;&nbsp;:&nbsp;IState&lt;S,&nbsp;S&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;S,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Run</span>(S&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(state,&nbsp;state); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Get</code> function represents a stateful computation that copies the <code>state</code> over to the 'value' dimension, so to speak. Notice that the return type is <code>IState&lt;S,&nbsp;S&gt;</code>. Copying the state over to the position of the <code>T</code> generic type means that it becomes accessible to the expressions that run inside of <code>Select</code> and <code>SelectMany</code>. </p> <p> You'll see an example once I rewrite the above <code>Aggregator</code> to be entirely based on composition, but in order to do that, I also need the <em>put</em> function. </p> <h3 id="224229b040ea417097a9de2ed1247953"> Put <a href="#224229b040ea417097a9de2ed1247953" title="permalink">#</a> </h3> <p> The <em>put</em> function enables you to write a new state value to the underlying state dimension. The implementation in the current code base looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;Unit&gt;&nbsp;<span style="color:#74531f;">Put</span>&lt;<span style="color:#2b91af;">S</span>&gt;(S&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;PutState&lt;S&gt;(s); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PutState</span>&lt;<span style="color:#2b91af;">S</span>&gt;&nbsp;:&nbsp;IState&lt;S,&nbsp;Unit&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;S&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PutState</span>(S&nbsp;<span style="color:#1f377f;">s</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.s&nbsp;=&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;Unit,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Run</span>(S&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(Unit.Default,&nbsp;s); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation uses a <code>Unit</code> value to represent <code>void</code>. As usual, we have the problem in C-based languages that <code>void</code> isn't a value, but fortunately, <a href="/2018/01/15/unit-isomorphisms">unit is isomorphic to void</a>. </p> <p> Notice that the <code>Run</code> method ignores the current <code>state</code> and instead replaces it with the new state <code>s</code>. </p> <h3 id="c347a208b9524a1b85756a66cf53999c"> Look, no classes! <a href="#c347a208b9524a1b85756a66cf53999c" title="permalink">#</a> </h3> <p> The <code>Get</code> and <code>Put</code> functions are enough that we can now rewrite the functionality currently locked up in the <code>Aggregator</code> class. Instead of having to define a new <code>class</code> for that purpose, it's possible to compose our way to the same functionality by writing a function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;IReadOnlyDictionary&lt;<span style="color:blue;">int</span>,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&gt;,&nbsp;Maybe&lt;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">Aggregate</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">correlationId</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">value</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;state&nbsp;<span style="color:blue;">in</span>&nbsp;State.Get&lt;IReadOnlyDictionary&lt;<span style="color:blue;">int</span>,&nbsp;IReadOnlyCollection&lt;<span style="color:blue;">int</span>&gt;&gt;&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;mcoll&nbsp;=&nbsp;state.TryGetValue(correlationId) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;retVal&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;coll&nbsp;<span style="color:blue;">in</span>&nbsp;mcoll.Where(<span style="color:#1f377f;">c</span>&nbsp;=&gt;&nbsp;c.Count&nbsp;==&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;Tuple.Create(coll.ElementAt(0),&nbsp;coll.ElementAt(1),&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newState&nbsp;=&nbsp;retVal &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:#1f377f;">_</span>&nbsp;=&gt;&nbsp;state.Remove(correlationId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GetValueOrFallback( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state.Replace( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mcoll &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:#1f377f;">coll</span>&nbsp;=&gt;&nbsp;coll.Append(value)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GetValueOrFallback(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;value&nbsp;}))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;_&nbsp;<span style="color:blue;">in</span>&nbsp;State.Put(newState) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;retVal; }</pre> </p> <p> Okay, I admit that there's a hint of <a href="https://en.wikipedia.org/wiki/Code_golf">code golf</a> over this. It's certainly not <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> C#. To be clear, I'm not endorsing this style of C#; I'm only showing it to explain the abstraction offered by the State monad. <a href="/2019/03/18/the-programmer-as-decision-maker">Adopt such code at your own peril</a>. </p> <p> The first observation to be made about this code example is that it's written entirely in query syntax. There's a good reason for that. Query syntax is syntactic sugar on top of <code>SelectMany</code>, so you could, conceivably, also write the above expression using method call syntax. However, in order to make early values available to later expressions, you'd have to pass a lot of tuples around. For example, the above expression makes repeated use of <code>mcoll</code>, so had you been using method call syntax instead of query syntax, you would have had to pass that value on to subsequent computations as one item in a tuple. Not impossible, but awkward. With query syntax, all values remain in scope so that you can refer to them later. </p> <p> The expression starts by using <code>Get</code> to get the current state. The <code>state</code> variable is now available in the rest of the expression. </p> <p> The <code>state</code> is a dictionary, so the next step is to query it for an entry that corresponds to the <code>correlationId</code>. I've used an overload of <code>TryGetValue</code> that returns a Maybe value, which also explains (I hope) the <code>m</code> prefix of <code>mcoll</code>. </p> <p> Next, the expression filters <code>mcoll</code> and creates a triple if the <code>coll</code> has a <code>Count</code> of two. Notice that the nested query syntax expression (<code>from...select</code>) isn't running in the State monad, but rather in the <a href="/2022/04/25/the-maybe-monad">Maybe monad</a>. The result, <code>retVal</code>, is another Maybe value. </p> <p> That takes care of the 'return value', but we also need to calculate the new state. This happens in a somewhat roundabout way. The reason that it's not more straightforward is that C# query syntax doesn't allow branching (apart from the ternary <code>?..:</code> operator) and (this version of the language, at least) has weak pattern-matching abilities. </p> <p> Instead, it uses <code>retVal</code> and <code>mcoll</code> as indicators of how to update the state. If <code>retVal</code> is populated, it means that the <code>Aggregate</code> computation will return a triple, in which case it must <code>Remove</code> the entry from the state dictionary. On the other hand, if that's not the case, it must update the entry. Again, there are two options. If <code>mcoll</code> was already populated, it should be updated by appending the <code>value</code>. If not, a new entry containing only the <code>value</code> should be added. </p> <p> Finally, the expression uses <code>Put</code> to save the new state, after which it returns <code>retVal</code>. </p> <p> While this is far from idiomatic C# code, the point is that you can <em>compose</em> your way to the desired behaviour. You don't have to write a new class. Not that this is necessarily an improvement in C#. I'm mostly stating this to highlight a difference in philosophy. </p> <p> Of course, this is all much more elegant in Haskell, where the same functionality is as terse as this: </p> <p> <pre><span style="color:#2b91af;">handle</span>&nbsp;::&nbsp;(<span style="color:blue;">Ord</span>&nbsp;k,&nbsp;<span style="color:blue;">MonadState</span>&nbsp;(<span style="color:blue;">Map</span>&nbsp;k&nbsp;[a])&nbsp;m)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;k&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;(<span style="color:#2b91af;">Maybe</span>&nbsp;(a,&nbsp;a,&nbsp;a)) handle&nbsp;correlationId&nbsp;value&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;m&nbsp;&lt;-&nbsp;get &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(retVal,&nbsp;newState)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;Map.<span style="color:blue;">lookup</span>&nbsp;correlationId&nbsp;m&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;[x,&nbsp;y]&nbsp;-&gt;&nbsp;(Just&nbsp;(x,&nbsp;y,&nbsp;value),&nbsp;Map.delete&nbsp;correlationId&nbsp;m) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;&nbsp;_&nbsp;-&gt;&nbsp;(Nothing,&nbsp;Map.adjust&nbsp;(++&nbsp;[value])&nbsp;correlationId&nbsp;m) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;-&gt;&nbsp;(Nothing,&nbsp;Map.insert&nbsp;correlationId&nbsp;[value]&nbsp;m) &nbsp;&nbsp;put&nbsp;newState &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;retVal</pre> </p> <p> Notice that this implementation also makes use of <code>get</code> and <code>put</code>. </p> <h3 id="2263fd914af84e0082c77a5ad2b9afe6"> Modify <a href="#2263fd914af84e0082c77a5ad2b9afe6" title="permalink">#</a> </h3> <p> The <code>Get</code> and <code>Put</code> functions are basic functions based on the State monad abstraction. These two functions, again, can be used to define some secondary helper functions, whereof <code>Modify</code> is one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;Unit&gt;&nbsp;<span style="color:#74531f;">Modify</span>&lt;<span style="color:#2b91af;">S</span>&gt;(Func&lt;S,&nbsp;S&gt;&nbsp;<span style="color:#1f377f;">modify</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Get&lt;S&gt;().SelectMany(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Put(modify(s))); }</pre> </p> <p> It wasn't required for the above <code>Aggregate</code> function, but here's a basic unit test that demonstrates how it works: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ModifyExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&nbsp;State.Modify((<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>)&nbsp;=&gt;&nbsp;i&nbsp;+&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;x.Run(1); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(2,&nbsp;actual.Item2); }</pre> </p> <p> It can be useful if you need to perform an 'atomic' state modification. For a realistic Haskell example, you may want to refer to my article <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">An example of state-based testing in Haskell</a>. </p> <h3 id="b1e1ba440a4f4b6aad12bc6721ba6f8b"> Gets <a href="#b1e1ba440a4f4b6aad12bc6721ba6f8b" title="permalink">#</a> </h3> <p> Another occasionally useful second-order helper function is <code>Gets</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#74531f;">Gets</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(Func&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Get&lt;S&gt;().Select(selector); }</pre> </p> <p> This function can be useful as a combinator if you need just a projection of the current state, instead of the whole state. </p> <p> Here's another basic unit test as an example: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">GetsExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&nbsp;State.Gets((<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>)&nbsp;=&gt;&nbsp;s.Length); &nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;x.Run(<span style="color:#a31515;">&quot;bar&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Tuple.Create(3,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>),&nbsp;actual); }</pre> </p> <p> While the above Aggregator example didn't require <code>Modify</code> or <code>Gets</code>, I wanted to include them here for completeness sake. </p> <h3 id="05a2046a71e94b85a3f5445df9669800"> F# <a href="#05a2046a71e94b85a3f5445df9669800" title="permalink">#</a> </h3> <p> Most of the code shown in this article has been C#, with the occasional Haskell code. You can also implement the State monad, as well as the helper methods, in <a href="https://fsharp.org">F#</a>, where it'd feel more natural to dispense with interfaces and instead work directly with functions. To make things a little clearer, you may want to define a type alias: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;State&lt;&#39;a,&nbsp;&#39;s&gt;&nbsp;=&nbsp;(&#39;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a&nbsp;*&nbsp;&#39;s)</pre> </p> <p> You can now define a <code>State</code> module that works directly with that kind of function: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;State&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;run&nbsp;state&nbsp;(f&nbsp;:&nbsp;State&lt;_,&nbsp;_&gt;)&nbsp;=&nbsp;f&nbsp;state &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;lift&nbsp;x&nbsp;state&nbsp;=&nbsp;x,&nbsp;state &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;x&nbsp;state&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;x&#39;,&nbsp;newState&nbsp;=&nbsp;run&nbsp;state&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;x&#39;,&nbsp;newState &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;bind&nbsp;(f&nbsp;:&nbsp;&#39;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;State&lt;&#39;b,&nbsp;&#39;s&gt;)&nbsp;(x&nbsp;:&nbsp;State&lt;&#39;a,&nbsp;&#39;s&gt;)&nbsp;state&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;x&#39;,&nbsp;newState&nbsp;=&nbsp;run&nbsp;state&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run&nbsp;newState&nbsp;(f&nbsp;x&#39;) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;get&nbsp;state&nbsp;=&nbsp;state,&nbsp;state &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;put&nbsp;newState&nbsp;_&nbsp;=&nbsp;(),&nbsp;newState &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;modify&nbsp;f&nbsp;=&nbsp;get&nbsp;|&gt;&nbsp;map&nbsp;f&nbsp;|&gt;&nbsp;bind&nbsp;put</pre> </p> <p> This is code I originally wrote for <a href="https://codereview.stackexchange.com/a/139652/3878">a Code Review answer</a>. You can go there to see all the details, as well as a motivating example. </p> <p> I see that I never got around to add a <code>gets</code> function... I'll leave that as an exercise. </p> <p> In C#, I've based the example on an interface (<code>IState&lt;S, T&gt;</code>), but it would also be possible to implement the State monad as extension methods on <code>Func&lt;S, Tuple&lt;T, S&gt;&gt;</code>. Try it! It might be another good exercise. </p> <h3 id="994529b38af241b9ba6090f5d670bcc8"> Conclusion <a href="#994529b38af241b9ba6090f5d670bcc8" title="permalink">#</a> </h3> <p> The State monad usually comes with a few helper functions: <em>get</em>, <em>put</em>, <em>modify</em>, and <em>gets</em>. They can be useful as combinators you can use to compose a stateful combination from smaller building blocks, just like you can use LINQ to compose complex queries over data. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Test Double clocks https://blog.ploeh.dk/2022/06/27/test-double-clocks 2022-06-27T05:44:00+00:00 Mark Seemann <div id="post"> <p> <em>A short exploration of replacing the system clock with Test Doubles.</em> </p> <p> In a comment to my article <a href="/2022/05/23/waiting-to-never-happen">Waiting to never happen</a>, <a href="https://github.com/ladeak">Laszlo</a> asks: </p> <blockquote> <p> "Why have you decided to make the date of the reservation relative to the SystemClock, and not the other way around? Would it be more deterministic to use a faked system clock instead?" </p> <footer><cite><a href="/2022/05/23/waiting-to-never-happen#c0b2e0bd555b5d5c5555c60bf11bff69">Laszlo</a></cite></footer> </blockquote> <p> The short answer is that I hadn't thought of the alternative. Not in this context, at least. </p> <p> It's a question worth exploring, which I will now proceed to do. </p> <h3 id="d7283861d9494c478b2bd8bb90218208"> Why IClock? <a href="#d7283861d9494c478b2bd8bb90218208" title="permalink">#</a> </h3> <p> The article in question discusses a unit test, which ultimately arrives at this: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">ChangeDateToSoldOutDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r1</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Some.Reservation.WithDate(DateTime.Now.AddDays(8).At(20,&nbsp;15)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r2</span>&nbsp;=&nbsp;r1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithId(Guid.NewGuid()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.TheDayAfter() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r1); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto</span>&nbsp;=&nbsp;r1.WithDate(r2.At).ToDto(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Put(r1.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>),&nbsp;dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">oRes</span>&nbsp;=&nbsp;Assert.IsAssignableFrom&lt;ObjectResult&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StatusCodes.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oRes.StatusCode); }</pre> </p> <p> The keen reader may notice that the test passes a <code><span style="color:blue;">new</span>&nbsp;SystemClock()</code> to the <code>sut</code>. In case you're wondering what that is, here's the definition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SystemClock</span>&nbsp;:&nbsp;IClock { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;DateTime&nbsp;<span style="color:#74531f;">GetCurrentDateTime</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;DateTime.Now; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While it should be possible to extrapolate the <code>IClock</code> interface from this code snippet, here it is for the sake of completeness: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IClock</span> { &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#74531f;">GetCurrentDateTime</span>(); }</pre> </p> <p> Since such an interface exists, why not use it in unit tests? </p> <p> That's possible, but I think it's worth highlighting what motivated this interface in the first place. If you're used to a certain style of test-driven development (TDD), you may think that interfaces exist in order to support TDD. They may. That's how I did TDD 15 years ago, but <a href="/2019/02/18/from-interaction-based-to-state-based-testing">not how I do it today</a>. </p> <p> The motivation for the <code>IClock</code> interface is another. It's there because the system clock is a source of impurity, just like random number generators, database queries, and web service invocations. In order to support <a href="/2020/03/23/repeatable-execution">repeatable execution</a>, it's useful to log the inputs and outputs of impure actions. This includes the system clock. </p> <p> The <code>IClock</code> interface doesn't exist in order to support unit testing, but in order to enable logging via the <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> pattern: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">LoggingClock</span>&nbsp;:&nbsp;IClock { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">LoggingClock</span>(ILogger&lt;LoggingClock&gt;&nbsp;<span style="color:#1f377f;">logger</span>,&nbsp;IClock&nbsp;<span style="color:#1f377f;">inner</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Logger&nbsp;=&nbsp;logger; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inner&nbsp;=&nbsp;inner; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ILogger&lt;LoggingClock&gt;&nbsp;Logger&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IClock&nbsp;Inner&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;DateTime&nbsp;<span style="color:#74531f;">GetCurrentDateTime</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">output</span>&nbsp;=&nbsp;Inner.GetCurrentDateTime(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Logger.LogInformation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;{method}()&nbsp;=&gt;&nbsp;{output}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(GetCurrentDateTime), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;output; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> All code in this article originates from the code base that accompanies <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> The web application is configured to decorate the <code>SystemClock</code> with the <code>LoggingClock</code>: </p> <p> <pre>services.AddSingleton&lt;IClock&gt;(<span style="color:#1f377f;">sp</span>&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">logger</span>&nbsp;=&nbsp;sp.GetService&lt;ILogger&lt;LoggingClock&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;LoggingClock(logger,&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock()); });</pre> </p> <p> While the motivation for the <code>IClock</code> interface wasn't to support testing, now that it exists, would it be useful for unit testing as well? </p> <h3 id="3bf52db0c892409dbc372a83293a8992"> A Stub clock <a href="#3bf52db0c892409dbc372a83293a8992" title="permalink">#</a> </h3> <p> As a first effort, we might try to add a <a href="http://xunitpatterns.com/Test%20Stub.html">Stub</a> clock: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ConstantClock</span>&nbsp;:&nbsp;IClock { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;DateTime&nbsp;dateTime; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ConstantClock</span>(DateTime&nbsp;<span style="color:#1f377f;">dateTime</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.dateTime&nbsp;=&nbsp;dateTime; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;This&nbsp;default&nbsp;value&nbsp;is&nbsp;more&nbsp;or&nbsp;less&nbsp;arbitrary.&nbsp;I&nbsp;chose&nbsp;it&nbsp;as&nbsp;the&nbsp;date</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;and&nbsp;time&nbsp;I&nbsp;wrote&nbsp;these&nbsp;lines&nbsp;of&nbsp;code,&nbsp;which&nbsp;also&nbsp;has&nbsp;the&nbsp;implication</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;that&nbsp;it&nbsp;was&nbsp;immediately&nbsp;a&nbsp;time&nbsp;in&nbsp;the&nbsp;past.&nbsp;The&nbsp;actual&nbsp;value&nbsp;is,</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;however,&nbsp;irrelevant.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IClock&nbsp;Default&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ConstantClock(<span style="color:blue;">new</span>&nbsp;DateTime(2022,&nbsp;6,&nbsp;19,&nbsp;9,&nbsp;25,&nbsp;0)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;DateTime&nbsp;<span style="color:#74531f;">GetCurrentDateTime</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;dateTime; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation always returns the same date and time. I called it <code>ConstantClock</code> for that reason. </p> <p> It's trivial to replace the <code>SystemClock</code> with a <code>ConstantClock</code> in the above test: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">ChangeDateToSoldOutDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">clock</span>&nbsp;=&nbsp;ConstantClock.Default; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r1</span>&nbsp;=&nbsp;Some.Reservation.WithDate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clock.GetCurrentDateTime().AddDays(8).At(20,&nbsp;15)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r2</span>&nbsp;=&nbsp;r1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithId(Guid.NewGuid()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.TheDayAfter() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r1); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clock, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto</span>&nbsp;=&nbsp;r1.WithDate(r2.At).ToDto(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Put(r1.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>),&nbsp;dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">oRes</span>&nbsp;=&nbsp;Assert.IsAssignableFrom&lt;ObjectResult&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StatusCodes.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oRes.StatusCode); }</pre> </p> <p> As you can see, however, it doesn't seem to be enabling any simplification of the test. It still needs to establish that <code>r1</code> and <code>r2</code> relates to each other as required by the test case, as well as establish that they are valid reservations in the future. </p> <p> You may protest that this is straw man argument, and that it would make the test both simpler and more readable if it would, instead, use explicit, hard-coded values. That's a fair criticism, so I'll get back to that later. </p> <h3 id="58fe168e8266401990d62641020db9ac"> Fragility <a href="#58fe168e8266401990d62641020db9ac" title="permalink">#</a> </h3> <p> Before examining the above criticism, there's something more fundamental that I want to get out of the way. I find a Stub clock icky. </p> <p> It works in this case, but may lead to fragile tests. What happens, for example, if another programmer comes by and adds code like this to the System Under Test (SUT)? </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">now</span>&nbsp;=&nbsp;Clock.GetCurrentDateTime(); <span style="color:green;">//&nbsp;Sabotage:</span> <span style="color:#8f08c4;">while</span>&nbsp;(Clock.GetCurrentDateTime()&nbsp;-&nbsp;now&nbsp;&lt;&nbsp;TimeSpan.FromMilliseconds(1)) {&nbsp;}</pre> </p> <p> As the comment suggests, in this case it's pure sabotage. I don't think that anyone would deliberately do something like this. This code snippet even sits in an asynchronous method, and in .NET 'everyone' knows that if you want to suspend execution in an asynchronous method, you should use <a href="https://docs.microsoft.com/dotnet/api/system.threading.tasks.task.delay">Task.Delay</a>. I rather intend this code snippet to indicate that keeping time constant, as <code>ConstantClock</code> does, can be fatal. </p> <p> If someone comes by and attempts to implement any kind of time-sensitive logic based on an injected <code>IClock</code>, the consequences could be dire. With the above sabotage, for example, the test hangs forever. </p> <p> When I originally <a href="/2021/01/11/waiting-to-happen">refactored time-sensitive tests</a>, it was because I didn't appreciate having such ticking bombs lying around. A <code>ConstantClock</code> isn't <em>ticking</em> (that's the problem), but it still seems like a booby trap. </p> <h3 id="d58309147ef6450589ed0b5c69b33b30"> Offset clock <a href="#d58309147ef6450589ed0b5c69b33b30" title="permalink">#</a> </h3> <p> It seems intuitive that a clock that doesn't go isn't very useful. Perhaps we can address that problem by setting the clock back. Not just a few hours, but days or years: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">OffsetClock</span>&nbsp;:&nbsp;IClock { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;TimeSpan&nbsp;offset; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">OffsetClock</span>(DateTime&nbsp;<span style="color:#1f377f;">origin</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset&nbsp;=&nbsp;DateTime.Now&nbsp;-&nbsp;origin; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IClock&nbsp;<span style="color:#74531f;">Start</span>(DateTime&nbsp;<span style="color:#1f377f;">at</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;OffsetClock(at); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;This&nbsp;default&nbsp;value&nbsp;is&nbsp;more&nbsp;or&nbsp;less&nbsp;arbitrary.&nbsp;I&nbsp;just&nbsp;picked&nbsp;the&nbsp;same</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;date&nbsp;and&nbsp;time&nbsp;as&nbsp;ConstantClock&nbsp;(which&nbsp;see).</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IClock&nbsp;Default&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Start(at:&nbsp;<span style="color:blue;">new</span>&nbsp;DateTime(2022,&nbsp;6,&nbsp;19,&nbsp;9,&nbsp;25,&nbsp;0)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;DateTime&nbsp;<span style="color:#74531f;">GetCurrentDateTime</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;DateTime.Now&nbsp;-&nbsp;offset; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> An <code>OffsetClock</code> object starts ticking as soon as it's created, but it ticks at the same pace as the system clock. Time still passes. Rather than a Stub, I think that this implementation qualifies as a <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a>. </p> <p> Using it in a test is as easy as using the <code>ConstantClock</code>: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">ChangeDateToSoldOutDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">clock</span>&nbsp;=&nbsp;OffsetClock.Default; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r1</span>&nbsp;=&nbsp;Some.Reservation.WithDate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clock.GetCurrentDateTime().AddDays(8).At(20,&nbsp;15)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r2</span>&nbsp;=&nbsp;r1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithId(Guid.NewGuid()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.TheDayAfter() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r1); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clock, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto</span>&nbsp;=&nbsp;r1.WithDate(r2.At).ToDto(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Put(r1.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>),&nbsp;dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">oRes</span>&nbsp;=&nbsp;Assert.IsAssignableFrom&lt;ObjectResult&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StatusCodes.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oRes.StatusCode); }</pre> </p> <p> The only change from the version that uses <code>ConstantClock</code> is the definition of the <code>clock</code> variable. </p> <p> This test can withstand the above sabotage, because time still passes at normal pace. </p> <h3 id="06f0ace3806e41139e30106aeb77e0e1"> Explicit dates <a href="#06f0ace3806e41139e30106aeb77e0e1" title="permalink">#</a> </h3> <p> Above, I promised to return to the criticism that the test is overly abstract. Now that it's possible to directly control time, perhaps it'd simplify the test if we could use hard-coded dates and times, instead of all that relative-time machinery: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">ChangeDateToSoldOutDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r1</span>&nbsp;=&nbsp;Some.Reservation.WithDate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;DateTime(2022,&nbsp;6,&nbsp;27,&nbsp;20,&nbsp;15,&nbsp;0)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r2</span>&nbsp;=&nbsp;r1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithId(Guid.NewGuid()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithDate(<span style="color:blue;">new</span>&nbsp;DateTime(2022,&nbsp;6,&nbsp;28,&nbsp;20,&nbsp;15,&nbsp;0)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r1); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OffsetClock.Start(at:&nbsp;<span style="color:blue;">new</span>&nbsp;DateTime(2022,&nbsp;6,&nbsp;19,&nbsp;13,&nbsp;43,&nbsp;0)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto</span>&nbsp;=&nbsp;r1.WithDate(r2.At).ToDto(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Put(r1.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>),&nbsp;dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">oRes</span>&nbsp;=&nbsp;Assert.IsAssignableFrom&lt;ObjectResult&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StatusCodes.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oRes.StatusCode); }</pre> </p> <p> Yeah, not really. This isn't worse, but neither is it better. It's the same size of code, and while the dates are now explicit (<a href="https://peps.python.org/pep-0020/">which, ostensibly, is better</a>), the reader now has to deduce the relationship between the clock offset, <code>r1</code>, and <code>r2</code>. I'm not convinced that this is an improvement. </p> <h3 id="62ae3d8257b64496ae7e82930438daa5"> Determinism <a href="#62ae3d8257b64496ae7e82930438daa5" title="permalink">#</a> </h3> <p> In the original comment, Laszlo asked if it would be more deterministic to use a Fake system clock instead. This seems to imply that using the system clock is nondeterministic. Granted, it is when not used with care. </p> <p> On the other hand, when used as shown in the initial test, it's <em>almost</em> deterministic. What time-related circumstances would have to come around for the test to fail? </p> <p> The important precondition is that both reservations are in the future. The test picks a date eight days in the future. How might that precondition fail? </p> <p> The only failure condition I can think of is if test execution somehow gets suspended <em>after</em> <code>r1</code> and <code>r2</code> are initialised, but <em>before</em> calling <code>sut.Put</code>. If you run the test on a laptop and put it to sleep for more than eight days, you may be so extremely lucky (or unlucky, depending on how you look at it) that this turns out to be the case. When execution resumes, the reservations are now in the past, and <code>sut.Put</code> will fail because of that. </p> <p> I'm not convinced that this is at all likely, and it's not a scenario that I'm inclined to take into account. </p> <p> And in any case, the test variation that uses <code>OffsetClock</code> is as 'vulnerable' to that scenario as the <code>SystemClock</code>. The only <a href="https://martinfowler.com/bliki/TestDouble.html">Test Double</a> not susceptible to such a scenario is <code>ConstantClock</code>, but as you have seen, this has more immediate problems. </p> <h3 id="0d09375b62d94fc287cb70568e0b7f1a"> Conclusion <a href="#0d09375b62d94fc287cb70568e0b7f1a" title="permalink">#</a> </h3> <p> If you've read or seen a sufficient amount of time-travel science fiction, you know that it's not a good idea to try to change time. This also seems to be the case here. At least, I can see a few disadvantages to using Test Double clocks, but no clear advantages. </p> <p> The above is, of course, only one example, but the concern of how to control the passing of time in unit testing isn't new to me. This is something that have been an issue on and off since I started with TDD in 2003. I keep coming back to the notion that the simplest solution is to use as many <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> as possible, combined with a few impure actions that may require explicit use of dates and times relative to the system clock, as shown in previous articles. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1d4fb68f3b5542e9897c82ad5c342a8a"> <div class="comment-author"><a href="https://github.com/ladeak">Laszlo</a> <a href="#1d4fb68f3b5542e9897c82ad5c342a8a">#</a></div> <div class="comment-content"> <p> I agree to most described in this post. However, I still find StubClock as my 'default' approach. I summarized the my reasons in this <a href="https://gist.github.com/ladeak/4f6ece31e941e28bafa1fca844d9fe3b">gist reply</a>. </p> </div> <div class="comment-date">2022-06-30 7:43 UTC</div> </div> <div class="comment" id="5aa51b5af530409290c0944b07ce9b10"> <div class="comment-author"><a href="https://github.com/c-vetter">C. Vetter</a> <a href="#5aa51b5af530409290c0944b07ce9b10">#</a></div> <div class="comment-content"> <figure class="quote"> <blockquote> Yeah, not really. This isn't worse, but neither is it better. It's the same size of code, and while the dates are now explicit (<a href="https://peps.python.org/pep-0020/">which, ostensibly, is better</a>), the reader now has to deduce the relationship between the clock offset, <code>r1</code>, and <code>r2</code>. I'm not convinced that this is an improvement. </blockquote> </figure> <p>I think, you overlook an important fact here: It depends™.</p> <p> As <a href="https://youtu.be/pSOBeD1GC_Y">Obi Wan taught us</a>, the point of view is often quite important. In this case, yes it's true, the changed code is more explicit, from a certain point of view, because <em>the dates are now explicit</em>. But: in the previous version, the <em>relationships were explicit</em>, whereas they have been rendered implicit now. Which is better depends on the context, and in this context, I think the change is for the worse. </p> <p> In this context, I think we care neither about the specific dates nor the relationship between both reservation dates. All we care about is their relationship to the present and that they are different from each other. With that in mind, I'd suggest to extend your <code>Some</code> container with more datetimes, in addition to <code>Now</code>, like <code>FutureDate</code> and <code>OtherFutureDate</code>. </p> <p> How those are constructed is generally of no relevance to the current test. After all, if we wanted to be 100% sure about every piece, we'd basically have re-write our entire runtime for each test case, which would just be madness. And yes, I'd just construct them based on the current system time. </p> <p> Regarding the overall argument, I'll say that dealing with time issues is generally a pain, but most of the time, we don't really need to deal with what happens at specific times. In those rare cases, yes, it makes sense to fix the test's time, but I'd leave that as a rare exception. Partly because such tests tend to require some kind of wide-ranging mock that messes with a lot of things. </p> <p> If we're talking about stuff like Y2k-proofing (if you're too young to remember, look it up, kids), it bears thinking about actually creating a whole test machine (virtual or physical) with an appropriate system time and just running your test suite on there. In times of docker, I'll bet that that will be less pain in many cases than adding time-fixing mock stuff. </p> <p> If passage of time is important, that's another bag of pain right there, but I'd look into segregating that as much as possible from everything else. If, for example, you need things to happen after 100 minutes have passed, I'd prefer having a single time-based event system that all other code can subscribe to and be called back when the interesting time arrives. That way, I can test the consumers without actually travelling through time, while testing the timer service will be reduced to making sure that events are fired at the appropriate times. <small>The latter could even happen on a persistent test machine that just keeps running, giving you insight on long-time behavior (just an idea, not a prescription 😉).</small> </p> </div> <div class="comment-date">2024-05-08 11:13 UTC</div> </div> <div class="comment" id="eddb383b90f8416aaecbee862b808df9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#eddb383b90f8416aaecbee862b808df9">#</a></div> <div class="comment-content"> <p> Thank you for writing. It's a good point that the two alternatives that I compare really only represent different perspectives. As one part becomes more explicit, the other becomes more implicit, and vice versa. I hadn't though of that, so thank you for pointing that out. </p> <p> Perhaps, as you suggest, a better API might be in order. I'm sure this isn't my last round around that block. I don't, however, want to add <code>Now</code>, <code>FutureDate</code>, etc. to the <code>Some</code> API. This module contains a collection of <a href="https://en.wikipedia.org/wiki/Equivalence_class">representative values of various equivalence classes</a>, and in order to ensure test repeatability, <a href="/2017/09/11/test-data-without-builders">they should be immutable</a> and deterministic. This rules out hiding a call to <code>DateTime.Now</code> behind such an API. </p> <p> That doesn't, however, rule out other types of APIs. If you move to <a href="/2017/09/18/the-test-data-generator-functor">test data generators</a> instead, it might make sense to define a 'future date' generator. </p> <p> All that said, I agree that the best way to test time-sensitive code is to model it in such a way that it's deterministic. I've <a href="/2020/02/24/discerning-and-maintaining-purity">touched on this topic before</a>, and most of the tests in the sample code base that accompanies <a href="/ctfiyh">Code That Fits in Your Head</a> takes that approach. </p> <p> The test discussed in this article, however, sits <a href="/2023/07/31/test-driving-the-pyramids-top">higher in the Test Pyramid</a>, and for such <a href="/2012/06/27/FacadeTest">Facade Tests</a>, I'd like to exercise them in as realistic a context as possible. That's why I run them on the real system clock. </p> </div> <div class="comment-date">2024-05-18 8:45 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The State monad https://blog.ploeh.dk/2022/06/20/the-state-monad 2022-06-20T21:52:00+00:00 Mark Seemann <div id="post"> <p> <em>Stateful computations as a monad. An example for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. A previous article described <a href="/2021/07/19/the-state-functor">the State functor</a>. As is the case with many (but not all) <a href="/2018/03/22/functors">functors</a>, this one also forms a monad. </p> <p> This article continues where the State functor article stopped. It uses the same code base. </p> <h3 id="0309f41b7a434781a1640f18ac7cea30"> SelectMany <a href="#0309f41b7a434781a1640f18ac7cea30" title="permalink">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function. In C#, monadic bind is called <code>SelectMany</code>. Given the <code>IState&lt;S, T&gt;</code> interface defined in the State functor article, you can implement <code>SelectMany</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;T1&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;IState&lt;S,&nbsp;T1&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;SelectManyState&lt;S,&nbsp;T,&nbsp;T1&gt;(source,&nbsp;selector); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SelectManyState</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;:&nbsp;IState&lt;S,&nbsp;T1&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;T,&nbsp;IState&lt;S,&nbsp;T1&gt;&gt;&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">SelectManyState</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;IState&lt;S,&nbsp;T1&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.source&nbsp;=&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.selector&nbsp;=&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;T1,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Run</span>(S&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;T,&nbsp;S&gt;&nbsp;<span style="color:#1f377f;">tuple</span>&nbsp;=&nbsp;source.Run(state); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IState&lt;S,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;selector(tuple.Item1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;projection.Run(tuple.Item2); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As <code>SelectMany</code> implementations go, this is easily the most complex so far in this article series. While it looks complex, it really isn't. It's only complicated. </p> <p> The three lines of code in the <code>Run</code> method does most of the work. The rest is essentially <a href="/2019/12/16/zone-of-ceremony">ceremony</a> required because C# doesn't have language features like object expressions. </p> <p> To be fair, part of the boilerplate is also caused by using an interface instead of functions. In <a href="https://fsharp.org">F#</a> you could get by with as little as this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;bind&nbsp;(f&nbsp;:&nbsp;&#39;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;State&lt;&#39;b,&nbsp;&#39;s&gt;)&nbsp;(x&nbsp;:&nbsp;State&lt;&#39;a,&nbsp;&#39;s&gt;)&nbsp;state&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;x&#39;,&nbsp;newState&nbsp;=&nbsp;run&nbsp;state&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;run&nbsp;newState&nbsp;(f&nbsp;x&#39;)</pre> </p> <p> I found an F# State implementation on my hard drive that turned out to originate from <a href="https://codereview.stackexchange.com/a/139652/3878">this Code Review answer</a>. You can go there to see it in context. </p> <p> The <code>SelectMany</code> method first runs the <code>source</code> with the supplied <code>state</code>. This produces a tuple with a value and a new state. The value is <code>tuple.Item1</code>, which has the type <code>T</code>. The method proceeds to use that value to call the <code>selector</code>, which produces a new State value. Finally, the method runs the <code>projection</code> with the new state (<code>tuple.Item2</code>). </p> <p> Monadic bind becomes useful when you have more than one function that returns a monadic value. Consider a code snippet like this: </p> <p> <pre>IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Switch(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>).SelectMany(<span style="color:#1f377f;">txt</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;VowelExpander(txt));</pre> </p> <p> This uses the silly <code>VowelExpander</code> class from <a href="/2021/07/19/the-state-functor">the State functor article</a>, as well as this new frivolous State implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Switch</span>&nbsp;:&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;option1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;option2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Switch</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">option1</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">option2</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.option1&nbsp;=&nbsp;option1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.option2&nbsp;=&nbsp;option2; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">Run</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(0&nbsp;&lt;=&nbsp;state) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(option1,&nbsp;state); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newState</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(option2,&nbsp;newState); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Both <code>Switch</code> and <code>VowelExpander</code> are State objects. If <code>SelectMany</code> didn't flatten as it goes, composition would have resulted in a nested State value. You'll see an example later in this article. </p> <h3 id="edc7d11f7fbd4b72b6b16858617fbbab"> Query syntax <a href="#edc7d11f7fbd4b72b6b16858617fbbab" title="permalink">#</a> </h3> <p> Monads also enable query syntax in C# (just like they enable other kinds of syntactic sugar in languages like F# and <a href="https://www.haskell.org">Haskell</a>). As outlined in the <a href="/2022/03/28/monads">monad introduction</a>, however, you must add a special <code>SelectMany</code> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;T1&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;IState&lt;S,&nbsp;U&gt;&gt;&nbsp;<span style="color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;U,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> As already predicted in the <a href="/2022/03/28/monads">monad introduction</a>, this boilerplate overload is always implemented in the same way. Only the signature changes. With it, you could instead write the above composition of <code>Switch</code> and <code>VowelExpander</code> like this: </p> <p> <pre>IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;txt&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Switch(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;txt1&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">new</span>&nbsp;VowelExpander(txt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;txt1;</pre> </p> <p> That example requires a new variable (<code>txt1</code>). Given that it's often difficult to come up with good variable names, this doesn't look like much of an improvement. Still, it's possible. </p> <h3 id="048b4a03e28a4f7baca996319ca54627"> Join <a href="#048b4a03e28a4f7baca996319ca54627" title="permalink">#</a> </h3> <p> In <a href="/2022/03/28/monads">the introduction</a> you learned that if you have a <code>Flatten</code> or <code>Join</code> function, you can implement <code>SelectMany</code>, and the other way around. Since we've already defined <code>SelectMany</code> for <code>IState&lt;S, T&gt;</code>, we can use that to implement <code>Join</code>. In this article I use the name <code>Join</code> rather than <code>Flatten</code>. This is an arbitrary choice that doesn't impact behaviour. Perhaps you find it confusing that I'm inconsistent, but I do it in order to demonstrate that the behaviour is the same even if the name is different. </p> <p> The concept of a monad is universal, but the names used to describe its components differ from language to language. What C# calls <code>SelectMany</code>, Scala calls <code>flatMap</code>, and what Haskell calls <code>join</code>, other languages may call <code>Flatten</code>. </p> <p> You can always implement <code>Join</code> by using <code>SelectMany</code> with the identity function. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#74531f;">Join</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;IState&lt;S,&nbsp;IState&lt;S,&nbsp;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> Here's a way you can use it: </p> <p> <pre>IState&lt;<span style="color:blue;">int</span>,&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">nested</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Switch(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>).Select(<span style="color:#1f377f;">txt</span>&nbsp;=&gt;&nbsp;(IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;)<span style="color:blue;">new</span>&nbsp;VowelExpander(txt)); IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">flattened</span>&nbsp;=&nbsp;nested.Join();</pre> </p> <p> Of the three examples involving <code>Switch</code> and <code>VowelExpander</code>, this one most clearly emphasises the idea that a monad is a functor you can flatten. Using <code>Select</code> (instead of <code>SelectMany</code>) creates a nested State value when you try to compose the two together. With <code>Join</code> you can flatten them. </p> <p> Not that doing it this way is better in any way. In practice, you'll mostly use either <code>SelectMany</code> or query syntax. It's a rare case when I use something like <code>Join</code>. </p> <h3 id="57fff60c94da4624965da2eb87e46f17"> Return <a href="#57fff60c94da4624965da2eb87e46f17" title="permalink">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that Haskell uses): </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#74531f;">Return</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ReturnState&lt;S,&nbsp;T&gt;(x); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReturnState</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;IState&lt;S,&nbsp;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;T&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReturnState</span>(T&nbsp;<span style="color:#1f377f;">x</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.x&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;T,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Run</span>(S&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(x,&nbsp;state); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Like the above <code>SelectMany</code> implementation, this is easily the most complicated <code>Return</code> implementation so far shown in this article series. Again, however, most of it is just boilerplate necessitated by C#'s lack of certain language features (most notably object expressions). And again, this is also somewhat unfair because I could have chosen to demonstrate the State monad using <code>Func&lt;S, Tuple&lt;T, S&gt;&gt;</code> instead of an interface. (This would actually be a good exercise; try it!) </p> <p> If you strip away all the boilerplate, the implementation is a trivial one-liner (the <code>Run</code> method), as also witnessed by this equivalent F# function that just returns a tuple: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;lift&nbsp;x&nbsp;state&nbsp;=&nbsp;x,&nbsp;state</pre> </p> <p> When partially applied (<code>State.lift&nbsp;x</code>) that function returns a State value (i.e. a <code>&#39;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a&nbsp;*&nbsp;&#39;s</code> function). </p> <p> Again, you can see that F# code in context in <a href="https://codereview.stackexchange.com/a/139652/3878">this Code Review answer</a>. </p> <h3 id="7cff668e401e40a4b7ee005683e341f6"> Left identity <a href="#7cff668e401e40a4b7ee005683e341f6" title="permalink">#</a> </h3> <p> We need to identify the <em>return</em> function in order to examine <a href="/2022/04/11/monad-laws">the monad laws</a>. Now that this is done, let's see what the laws look like for the State monad, starting with the left identity law. </p> <p> <pre>[Theory] [InlineData(DayOfWeek.Monday,&nbsp;2)] [InlineData(DayOfWeek.Tuesday,&nbsp;0)] [InlineData(DayOfWeek.Wednesday,&nbsp;19)] [InlineData(DayOfWeek.Thursday,&nbsp;42)] [InlineData(DayOfWeek.Friday,&nbsp;2112)] [InlineData(DayOfWeek.Saturday,&nbsp;90)] [InlineData(DayOfWeek.Sunday,&nbsp;210)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LeftIdentity</span>(DayOfWeek&nbsp;<span style="color:#1f377f;">a</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">state</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;DayOfWeek,&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;DayOfWeek&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;State.Return&lt;<span style="color:blue;">int</span>,&nbsp;DayOfWeek&gt;; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;DayOfWeek,&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">dow</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;VowelExpander(dow.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(@return(a).SelectMany(h).Run(state),&nbsp;h(a).Run(state)); }</pre> </p> <p> In order to compare the two State values, the test has to <code>Run</code> them and then compare the return values. </p> <h3 id="e47c76d943214e0399bf1ab4ad1e843c"> Right identity <a href="#e47c76d943214e0399bf1ab4ad1e843c" title="permalink">#</a> </h3> <p> In a similar manner, we can showcase the right identity law as a test. </p> <p> <pre>[Theory] [InlineData(&nbsp;<span style="color:blue;">true</span>,&nbsp;0)] [InlineData(&nbsp;<span style="color:blue;">true</span>,&nbsp;1)] [InlineData(&nbsp;<span style="color:blue;">true</span>,&nbsp;8)] [InlineData(<span style="color:blue;">false</span>,&nbsp;0)] [InlineData(<span style="color:blue;">false</span>,&nbsp;2)] [InlineData(<span style="color:blue;">false</span>,&nbsp;7)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">RightIdentity</span>(<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">a</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">state</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">bool</span>,&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;VowelExpander(b.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;State.Return&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;; &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(@return).Run(state),&nbsp;m.Run(state)); }</pre> </p> <p> As always, even a parametrised test constitutes no <em>proof</em> that the law holds. I show the tests to illustrate what the laws look like in 'real' code. </p> <h3 id="4a3a29ab66304776b7bc6676d1675762"> Associativity <a href="#4a3a29ab66304776b7bc6676d1675762" title="permalink">#</a> </h3> <p> The last monad law is the associativity law that describes how (at least) three functions compose. We're going to need three functions. For the purpose of demonstrating the law, any three pure functions will do. While the following functions are silly and not at all 'realistic', they have the virtue of being as simple as they can be (while still providing a bit of variety). They don't 'mean' anything, so don't worry too much about their behaviour. It is, as far as I can tell, nonsensical. Later articles will show some more realistic examples of the State monad in action. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">F</span>&nbsp;:&nbsp;IState&lt;DateTime,&nbsp;<span style="color:blue;">int</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">F</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.s&nbsp;=&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#74531f;">Run</span>(DateTime&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newState</span>&nbsp;=&nbsp;state.AddDays(i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newValue</span>&nbsp;=&nbsp;i&nbsp;+&nbsp;state.Month; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(newValue,&nbsp;newState); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">G</span>&nbsp;:&nbsp;IState&lt;DateTime,&nbsp;TimeSpan&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">G</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.i&nbsp;=&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;TimeSpan,&nbsp;DateTime&gt;&nbsp;<span style="color:#74531f;">Run</span>(DateTime&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newState</span>&nbsp;=&nbsp;state.AddYears(i&nbsp;-&nbsp;state.Year); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newValue</span>&nbsp;=&nbsp;TimeSpan.FromMinutes(i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(newValue,&nbsp;newState); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">H</span>&nbsp;:&nbsp;IState&lt;DateTime,&nbsp;<span style="color:blue;">bool</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;TimeSpan&nbsp;duration; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">H</span>(TimeSpan&nbsp;<span style="color:#1f377f;">duration</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.duration&nbsp;=&nbsp;duration; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;<span style="color:blue;">bool</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#74531f;">Run</span>(DateTime&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newState</span>&nbsp;=&nbsp;state&nbsp;-&nbsp;duration; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">newValue</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newState.DayOfWeek&nbsp;==&nbsp;DayOfWeek.Saturday&nbsp;||&nbsp;newState.DayOfWeek&nbsp;==&nbsp;DayOfWeek.Sunday; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(newValue,&nbsp;newState); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Armed with these three classes, we can now demonstrate the Associativity law: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2022-03-23&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2021-12-23T18:05&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;baz&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;1984-01-06T00:33&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Associativity</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">state</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IState&lt;DateTime,&nbsp;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;F(s); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;IState&lt;DateTime,&nbsp;TimeSpan&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;G(i); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;TimeSpan,&nbsp;IState&lt;DateTime,&nbsp;<span style="color:blue;">bool</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;H(ts); &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;DateTime,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.SelectMany(g).SelectMany(h).Run(state), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h)).Run(state)); }</pre> </p> <p> The version of <a href="https://xunit.net">xUnit.net</a> I'm using for these examples (xUnit.net 2.2.0 on .NET Framework 4.6.1 - I may already have hinted that this is an old code base I had lying around) comes with a converter between <code>string</code> and <code>DateTime</code>, which explains why the <code>[InlineData]</code> can supply <code>DateTime</code> values as <code>string</code>s. </p> <h3 id="bf812a647a5c412da53f52f63989ca1d"> Conclusion <a href="#bf812a647a5c412da53f52f63989ca1d" title="permalink">#</a> </h3> <p> For people coming from an imperative or object-oriented background, it can often be difficult to learn how to think 'functionally'. It took me years before I felt that I was on firm ground, and even so, I'm still learning new techniques today. As an imperative programmer, one often thinks in terms of state mutation. </p> <p> In Functional Programming, there are often other ways to solve problems than in object-oriented programming, but if you can't think of a way, you can often reach for the fairly blunt hammer than the State monad is. It enables you to implement ostensibly state-based algorithms in a functional way. </p> <p> This article was abstract, because I wanted to focus on the monad nature itself, rather than on practical applications. Future articles will provide more useful examples. </p> <p> <strong>Next:</strong> <a href="/2022/11/14/the-reader-monad">The Reader monad</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Some thoughts on naming tests https://blog.ploeh.dk/2022/06/13/some-thoughts-on-naming-tests 2022-06-13T07:51:00+00:00 Mark Seemann <div id="post"> <p> <em>What is the purpose of a test name?</em> </p> <p> Years ago I was participating in a coding event where we <a href="/2020/01/13/on-doing-katas">did katas</a>. My pairing partner and I was doing <em>silent ping pong</em>. Ping-pong style pair programming is when one programmer writes a test and passes the keyboard to the partner, who writes enough code to pass the test. He or she then writes a new test and passes control back to the first person. In the <em>silent</em> variety, you're not allowed to talk. This is an exercise in communicating via code. </p> <p> My partner wrote a test and I made it pass. After the exercise was over, we were allowed to talk to evaluate how it went, and my partner remarked that he'd been surprised that I'd implemented the opposite behaviour of what he'd intended. (It was something where there was a fork in the logic depending on a number being less than or greater to zero; I don't recall the exact details.) </p> <p> We looked at the test that he had written, and sure enough: He'd named the test by clearly indicating one behaviour, but then he'd written an assertion that looked for the opposite behaviour. </p> <p> I hadn't even noticed. </p> <p> I didn't read the test name. I only considered the test body, because that's the executable specification. </p> <h3 id="211621375fca4b7590abdfe87a27542b"> How tests are named <a href="#211621375fca4b7590abdfe87a27542b" title="permalink">#</a> </h3> <p> I've been thinking about test names ever since. What is the role of a test name? </p> <p> In some languages, you write unit tests as methods or functions. That's how you do it in C#, Java, and many other languages: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;Home&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;Calendar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;Reservations&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">WithControllerHandlesSuffix</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">name</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;UrlBuilder(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.WithController(name&nbsp;+&nbsp;<span style="color:#a31515;">&quot;Controller&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;sut.WithController(name); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> Usually, when we define new class methods, we've learned that naming is important. Truly, this applies to test methods, too? </p> <p> Yet, other languages don't use class methods to define tests. The most common JavaScript frameworks don't, and <a href="/2018/05/07/inlined-hunit-test-lists">neither does Haskell HUnit</a>. Instead, tests are simply values with labels. </p> <p> This hints at something that may be important. </p> <h3 id="03845212a1284263a6cdf452e7fd0bea"> The role of test names <a href="#03845212a1284263a6cdf452e7fd0bea" title="permalink">#</a> </h3> <p> If tests aren't necessarily class methods, then what role do names play? </p> <p> Usually, when considering method names, it's important to provide a descriptive name in order to help client developers. A client developer writing calling code must figure out which methods to call on an object. Good names help with that. </p> <p> Automated tests, on the other hand, have no explicit callers. There's no client developer to communicate with. Instead, a test framework such as <a href="https://xunit.net">xUnit.net</a> scans the public API of a test suite and automatically finds the test methods to execute. </p> <p> The most prominent motivation for writing good method names doesn't apply here. We must reevaluate the role of test names, also keeping in mind that with some frameworks, in some languages, tests aren't even methods. </p> <h3 id="06e15a7c7d90430685afd0182b878546"> Mere anarchy is loosed upon the world <a href="#06e15a7c7d90430685afd0182b878546" title="permalink">#</a> </h3> <p> The story that introduces this article has a point. When considering a test, I tend to go straight to the test body. I only read the test name if I find the test body unclear. </p> <p> Does this mean that the test name is irrelevant? Should we simply number the tests: <code>Test1</code>, <code>Test212</code>, and so on? </p> <p> That hardly seems like a good idea - not even to a person like me who considers the test name secondary to the test definition. </p> <p> This begs the question, though: If <code>Test42</code> isn't a good name, then what does a good test name look like? </p> <h3 id="bf895999bc034985ab10e839a69be5d4"> Naming schemes <a href="#bf895999bc034985ab10e839a69be5d4" title="permalink">#</a> </h3> <p> Various people suggest naming schemes. In the .NET world many people like <a href="https://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html">Roy Osherove's naming standard for unit tests</a>: <code>[UnitOfWork_StateUnderTest_ExpectedBehavior]</code>. I find it too verbose to my tastes, but my point isn't to attack this particular naming scheme. In my <a href="/2016/02/10/types-properties-software">Types + Properties = Software</a> article series, I experimented with using a poor man's version of <em>Given When Then:</em> </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;deuce&nbsp;when&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span> &nbsp;&nbsp;&nbsp;&nbsp;(winner&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;=&nbsp;<span style="color:navy;">scoreWhenDeuce</span>&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:navy;">Advantage</span>&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> It was a worthwhile experiment, but I don't think I ever used that style again. After all, <em>Given When Then</em> is just another way of saying <em>Arrange Act Assert</em>, and I already <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">organise my test code according to the AAA pattern</a>. </p> <p> These days, I don't follow any particular naming scheme, but I do keep a guiding principle in mind. </p> <h3 id="4db32b0268064abda82e32fb5724d7b6"> Information channel <a href="#4db32b0268064abda82e32fb5724d7b6" title="permalink">#</a> </h3> <p> A test name, whether it's a method name or a label, is an opportunity to communicate with the reader of the code. You can communicate via code, via names, via comments, and so on. A test name is more like a mandatory comment than a normal method name. </p> <p> Books like <a href="/ref/clean-code">Clean Code</a> make a compelling case that comments should be secondary to good names. The point isn't that all comments are bad, but that some are: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">z</span>&nbsp;=&nbsp;x&nbsp;+&nbsp;y;&nbsp;<span style="color:green;">//&nbsp;Add&nbsp;x&nbsp;and&nbsp;y</span></pre> </p> <p> It's rarely a good idea to add a comment that describes what the code <em>does</em>. This should already be clear from the code itself. </p> <p> A comment can still provide important information that code can't easily do. It may explain the <em>purpose</em> of the code. I try to take this into account when naming tests: Not repeat what the code does, but suggest a hint about its raison d'être. </p> <p> I try to strike a balance between <code>Test2112</code> and <code>Given&nbsp;deuce&nbsp;when&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct</code>. I view the task of naming tests as equivalent to producing section headings in an article like this one. They offer a hint at the kind of information that might be available in the section (<em>The role of test names</em>, <em>How tests are named</em>, or <em>Information channel</em>), but sometimes they're more tongue-in-cheek than helpful (<em>Mere anarchy is loosed upon the world</em>). I tend to name tests with a similar degree of precision (or lack thereof): <code>HomeReturnsJson</code>, <code>NoHackingOfUrlsAllowed</code>, <code>GetPreviousYear</code>, etcetera. </p> <p> These names, in isolation, hardly tell you what the tests are about. I'm okay with that, because I don't think that they have to. </p> <h3 id="cd5ccd57ea8a41dbb78f8b5c32985311"> What do you use test names for? <a href="#cd5ccd57ea8a41dbb78f8b5c32985311" title="permalink">#</a> </h3> <p> I occasionally discuss this question with other people. It seems to me that it's one of the topics where Socratic questioning breaks down: </p> <p> Them: <em>How do you name tests?</em> </p> <p> Me: <em>I try to strike a balance between information and not repeating myself.</em> </p> <p> Them: <em>How do you like this particular naming scheme?</em> </p> <p> Me: <em>It looks verbose to me. It seems to be repeating what's already in the test code.</em> </p> <p> Them: <em>I like to read the test name to see what the test does.</em> </p> <p> Me: <em>If the name and test code disagree, which one is right?</em> </p> <p> Them: <em>The test name should follow the naming scheme.</em> </p> <p> Me: <em>Why do you find that important?</em> </p> <p> Them: <em>It's got... electrolytes.</em> </p> <p> Okay, I admit that <a href="https://en.wikipedia.org/wiki/Satire">I'm a being uncharitable</a>, but the point that I'm after is that test names are <em>different</em>, yet most people seem to reflect little on this. </p> <p> When do you read test names? </p> <p> Personally, I rarely read or otherwise <em>use</em> test names. When I'm writing a test, I also write the name, but at that point I don't really <em>need</em> the name. Sometimes I start with a placeholder name (<code>Foo</code>), write the test, and change the name once I understand what the test does. </p> <p> Once a test is written, ideally it should just be sitting there as a regression test. <a href="/2013/04/02/why-trust-tests">The less you touch it, the better you can trust it</a>. </p> <p> You may have hundreds or thousands of tests. When you run your test suite, you care about the outcome. Did it pass or fail? The outcome is the result of a Boolean <em>and</em> operation. The test suite only passes when all tests pass, but you don't have to look at each test result. The aggregate result is enough as long as the test suite passes. </p> <p> You only need to look at a test when it fails. When this happens, most tools enable you to go straight to the failing test by clicking on it. (And if this isn't possible, I usually find it easier to navigate to the failing test either by line number or by copying the test name and navigating to it by pasting the name into my editor's navigation UI.) You don't really need the name to find a failing test. If the test was named <code>Test1337</code> it would be as easy to find as if it was named <code>Given&nbsp;deuce&nbsp;when&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct</code>. </p> <p> Once I look at a failing test, I start by looking at the test code and comparing that to the assertion message. </p> <p> Usually, when a test fails, it breaks for a reason. A code change caused the test to fail. Often, the offending change was one you did ten seconds earlier. Armed with an assertion message and the test code, I usually understand the problem right away. </p> <p> In rare cases the test is one that I've never seen before, and I'm confused about its purpose. This is when I read the test name. At that point, I appreciate if the name is helpful. </p> <h3 id="62f1324f399243c393c77a3cbb46f173"> Conclusion <a href="#62f1324f399243c393c77a3cbb46f173" title="permalink">#</a> </h3> <p> I'm puzzled that people are so <a href="/2021/03/22/the-dispassionate-developer">passionate</a> about test names. I consider them the least important part of a test. A name isn't irrelevant, but I find the test code more important. The code is an executable specification. It expresses the desired truth about a system. </p> <p> Test code is code that has the same lifetime as the production code. It pays to structure it as well as the production code. If a test is well-written, you should be able to understand it without reading its name. </p> <p> That's an ideal, and in reality we are fallible. Thus, providing a helpful name gives the reader a second chance to understand a test. The name shouldn't, however, be your first priority. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8e8c4bda169848e988926028933189d0"> <div class="comment-author"><a href="http://github.com/neongraal">Struan Judd</a> <a href="#8e8c4bda169848e988926028933189d0">#</a></div> <div class="comment-content"> <p> I often want run selected tests from the command line and thus use the test runner's abilty to filter all available tests. Where the set of tests I want to run is all the tests below some point in the heirarchy of tests I can filter by the common prefix, or the test class name. </p> <p> But I also often find myself wanting to run a set of tests that meet some functional criteria, e.g Validation approval tests, or All the tests for a particular feature across all the levels of the code base. In this case if the tests follow a naming convention where such test attributes are included in the test name, either via the method or class name, then such test filtering is possible. </p> </div> <div class="comment-date">2022-06-13 10:21 UTC</div> </div> <div class="comment" id="d1a000d5eb8349a586e19dbb231ce744"> <div class="comment-author"><a href="https://github.com/flakey-bit">Eddie Stanley</a> <a href="#d1a000d5eb8349a586e19dbb231ce744">#</a></div> <div class="comment-content"> <p> Mark, are you a <a href="https://www.thoughtworks.com/insights/blog/mockists-are-dead-long-live-classicists" target="_blank">Classicist or a Mockist</a>? I'm going to go out on a limb here and say you're probably a classicist. Test code written in a classicist style probably conveys the intent well already. I think code written in a Mockist style may not convey the intent as well, hence the test name (or a comment) becomes more useful to convey that information. </p> </div> <div class="comment-date">2022-06-13 23:40 UTC</div> </div> <div class="comment" id="63217ba97aed47cb877c9dd56e53ae31"> <div class="comment-author"><a href="https://www.chriskrycho.com">Chris Krycho</a> <a href="#63217ba97aed47cb877c9dd56e53ae31">#</a></div> <div class="comment-content"> <p> There are (at least) two ways of using test names (as well as test <em>module</em> names, as suggested by Struan Judd) that we make extensive use of in the LinkedIn code base and which I have used in every code base I have ever written tests for: </p> <ul> <li> <p> <strong>To indicate the <em>intent</em> of the test.</strong> It is well and good to say that the assertions should convey the conditions, but often it is not clear <em>why</em> a condition is intended to hold. Test names (and descriptive strings on the assertions) can go a very long way, especially when working in a large and/or unfamiliar code base, to understand whether the assertion remains relevant, or <em>how</em> it is relevant. </p> <p> Now, granted: it is quite possible for those to get out of date, much as comments do. However, just as good comments remain valuable even though there is a risk of stale comments, good test names can be valuable even though they can also become stale. </p> <p> The key, for me, is exactly the same as good comments—and you could argue that comments therefore obviate the need for test names. If we only cared about tests from the POV of reading the code, I would actually agree! However, because we often read the tests as a suite of assertions presented in some other UI (a terminal, a web view, etc.), the names and assertion descriptions themselves serve as the explanation when reading. </p> </li> <li> <p> <strong>To provide structure and organization to the test suite.</strong> This is the same point Struan Judd was getting at: having useful test names lets you filter down to relevant chunks of the suite easily. This is valuable even on a small code base (like <a href="https://github.com/true-myth/true-myth">the <code>Maybe</code> and <code>Result</code> library in TypeScript</a> a friend and I maintain), but it becomes <em>invaluable</em> when you have tens of thousands of tests to filter or search through, as in the main LinkedIn app! </p> <p> For that reason, we (and the Ember.js community more broadly) make extensive use of QUnit's <code>module()</code> hook to name the <em>set</em> of modules under test (<code>module('Rendering | SomeComponent', function () { ... }</code> or <code>module('Unit | some-utility', function () { ... }</code>) as well as naming <code>test()</code> (<code>test('returns `null` if condition X does not hold', function (assert) { ... }</code>) and indeed providing descriptive strings for <code>assert()</code> calls. We might even nest <code>module()</code> calls to make it easy to see and filter from how our test UI presents things: <strong>Rendering | SomeComponent > someMethod > a test description</strong>. </p> </li> </ul> <p> Now, how that plays out varies library to library. The aforementioned TS library just names the test with a decent description of what is under test (<a href="https://github.com/true-myth/true-myth/blob/cc2883e63748705272ff423d958525b65524c640/test/result.test.ts#L10-L31">here</a>, for example) as well as grouping them sensibly with overarching descriptions, and never uses assertion descriptions because they wouldn’t add anything. A couple of the libraries I wrote internally at LinkedIn, by contrast, make extensive use of both. It is, as usual, a tool to be employed as, and only as, it is <em>useful</em>. But it is indeed quite useful sometimes! </p> </div> <div class="comment-date">2022-06-14 00:57 UTC</div> </div> <div class="comment" id="81fdb10f87804872b24acae33ea14ecd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#81fdb10f87804872b24acae33ea14ecd">#</a></div> <div class="comment-content"> <p> Struan, thank you for writing. I can't say that I've given much thought to the need to run subsets of a test suite. You have a point, though, that if that's a requirement, you need something on which to filter. </p> <p> Is the name the appropriate criterion for that, though? It sounds brittle to me, but I grant that it depends on which alternatives are available. In <a href="https://xunit.net/">xUnit.net</a>, for example, you can use the <code>[Trait]</code> attribute to annotate tests with arbitrary metadata. I think that <a href="https://nunit.org/">NUnit</a> has a similar feature, but there's no guarantee that every unit testing framework on any platform or language supports such a feature. </p> <p> Whenever a framework supports such metadata-based filtering, I'd favour relying on that instead of naming conventions. Naming conventions are vulnerable to misspellings and other programmer errors. That may also be true of metadata-based categorisation, but hopefully to a lesser degree, as these might enable you to use ordinary language features to keep the categories DRY. </p> <p> Using names also sounds restrictive to me. Doesn't this mean that you have to be able to predict your filtering requirements when you decide on a naming scheme? </p> <p> What if, later, you find that you need to filter on a different dimension? With metadata annotations, you should be able to add a new category to the affected tests, but how will you do that with an established naming scheme? </p> <p> Overall, though, the reason that I haven't given this much thought is that I've never had the need to filter tests in arbitrary ways. You must be doing something different from how I work with tests. Why do you need to filter tests? </p> </div> <div class="comment-date">2022-06-19 21:59 UTC</div> </div> <div class="comment" id="b8c145c78a3649ada137f8c997e10b4a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b8c145c78a3649ada137f8c997e10b4a">#</a></div> <div class="comment-content"> <p> Eddie, thank you for writing. I don't find the linked article illuminating if one hasn't already heard about the terms <em>mockist</em> and <em>classicist</em>. I rather prefer the terms <em>interaction-based</em> and <em>state-based</em> testing. In any case, I started out doing interaction-based testing, but have since <a href="/2019/02/18/from-interaction-based-to-state-based-testing">moved away from that</a>. Even when I mainly wrote interaction-based tests, though, I didn't like rigid naming schemes. I don't see how that makes much of a difference. </p> <p> I agree that a test name is a fine opportunity to convey intent. Did that not come across in the article? </p> </div> <div class="comment-date">2022-06-22 1:37 UTC</div> </div> <div class="comment" id="b74a945cce364621b0f80d247d7153f3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b74a945cce364621b0f80d247d7153f3">#</a></div> <div class="comment-content"> <p> Chris, thank you for writing. As I also <a href="#b8c145c78a3649ada137f8c997e10b4a">responded to Eddie Stanley</a>, I agree that a test name is a fine opportunity to convey intent. Did that not come across in the article? </p> <p> To your second point, I'll refer you to <a href="#81fdb10f87804872b24acae33ea14ecd">my answer to Struan Judd</a>. I'm still curious to learn <em>why</em> you find it necessary to categorise and filter tests. </p> </div> <div class="comment-date">2022-06-22 23:03 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Asynchronous monads https://blog.ploeh.dk/2022/06/06/asynchronous-monads 2022-06-06T07:33:00+00:00 Mark Seemann <div id="post"> <p> <em>Asynchronous computations form monads. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. A previous article described how <a href="/2018/09/24/asynchronous-functors">asynchronous computations form functors</a>. In this article, you'll see that asynchronous computations also form monads. You'll learn about closely related monads: .NET Tasks and <a href="https://fsharp.org">F#</a> <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/async-expressions">asynchronous workflows</a>. </p> <p> Before we start, I'm going to repeat the warning from the article about asynchronous functors. .NET Tasks aren't <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>, whereas F# asynchronous computations are. You could argue, then, that .NET Tasks aren't proper monads, but you mostly observe the difference when you perform impure operations. As a general observation, when impure operations are allowed, the conclusions of <a href="/2017/10/04/from-design-patterns-to-category-theory">this overall article series</a> are precarious. We can't radically change how the .NET languages work, so we'll have to soldier on, pretending that impure operations are delegated to other parts of our system. Under this undue assumption, we can pretend that <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1">Task&lt;T&gt;</a> forms a monad. Also, while there are differences, <a href="/2020/07/27/task-asynchronous-programming-as-an-io-surrogate">it sometimes helps to think of <code>Task&lt;T&gt;</code> as a sort of poor man's IO monad</a>. </p> <h3 id="1ab27adfb0194a10a2fa02690b49343b"> Monadic bind <a href="#1ab27adfb0194a10a2fa02690b49343b" title="permalink">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function. In C#, monadic bind is called <code>SelectMany</code>. You can define one as an extension method on the <a href="https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1">Task&lt;T&gt;</a> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Task&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Task&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Task&lt;TResult&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;selector(x); }</pre> </p> <p> With <code>SelectMany</code>, you can compose various tasks and flatten as you go: </p> <p> <pre>Task&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&nbsp;AsyncValue(&nbsp;&nbsp;42); Task&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">y</span>&nbsp;=&nbsp;AsyncValue(1337); Task&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">z</span>&nbsp;=&nbsp;x.SelectMany(<span style="color:blue;">async</span>&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;i&nbsp;+&nbsp;<span style="color:blue;">await</span>&nbsp;y);</pre> </p> <p> If you're wondering how this is useful, since C# already has <code>async</code> and <code>await</code> keywords for that purpose, I can understand you. Had you not had that language feature, monadic bind would have been helpful, but now it feels a little odd. (I haven't been deep into the bowels of how that language feature works, but from what little I've seen, monads play a central role - just as they do in the LINQ language feature.) </p> <p> In F# you can define a <code>bind</code> function that works on <code>Async&lt;'a&gt;</code> values: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;bind&nbsp;f&nbsp;x&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;f&nbsp;x&#39;&nbsp;}</pre> </p> <p> For both the C# and the F# examples, the exercise seems a little redundant, since they're both based on language features. The C# <code>SelectMany</code> implementation uses the <code>async</code> and <code>await</code> keywords, and the F# <code>bind</code> function uses the built-in <code>async</code> <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/computation-expressions">computation expression</a>. For that reason, I'll also skip the section on syntactic sugar that I've included in the previous articles in this article series. Syntactic sugar is already built into the languages. </p> <h3 id="e27cbc4c22ac4f66a311cb41584f971c"> Flatten <a href="#e27cbc4c22ac4f66a311cb41584f971c" title="permalink">#</a> </h3> <p> In <a href="/2022/03/28/monads">the introduction</a> you learned that if you have a <code>Flatten</code> or <code>Join</code> function, you can implement <code>SelectMany</code>, and the other way around. Since we've already defined <code>SelectMany</code> for <code>Task&lt;T&gt;</code>, we can use that to implement <code>Flatten</code>. In this article I use the name <code>Flatten</code> rather than <code>Join</code>. This is an arbitrary choice that doesn't impact behaviour. Perhaps you find it confusing that I'm inconsistent, but I do it in order to demonstrate that the behaviour is the same even if the name is different. </p> <p> The concept of a monad is universal, but the names used to describe its components differ from language to language. What C# calls <code>SelectMany</code>, Scala calls <code>flatMap</code>, and what Haskell calls <code>join</code>, other languages may call <code>Flatten</code>. </p> <p> You can always implement <code>Flatten</code> by using <code>SelectMany</code> with the identity function. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Task&lt;T&gt;&nbsp;<span style="color:#74531f;">Flatten</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Task&lt;Task&lt;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> The F# version uses the same implementation - it's just a bit terser: </p> <p> <pre><span style="color:green;">//&nbsp;Async&lt;Async&lt;&#39;a&gt;&gt;&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;flatten&nbsp;x&nbsp;=&nbsp;bind&nbsp;id&nbsp;x</pre> </p> <p> In F#, <code>id</code> is a built-in function. </p> <h3 id="99a8068b063e416daf4e8383fda97331"> Return <a href="#99a8068b063e416daf4e8383fda97331" title="permalink">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that Haskell uses). You don't, however, have to define a static method called <code>Return</code>. What's of importance is that the capability exists. For <code>Task&lt;T&gt;</code> this function already exists: It's called <a href="https://docs.microsoft.com/dotnet/api/system.threading.tasks.task.fromresult">FromResult</a>. </p> <p> In F#, it's not built-in, but easy to implement: </p> <p> <pre><span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;fromValue&nbsp;x&nbsp;=&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;x&nbsp;}</pre> </p> <p> I called it <code>fromValue</code> inspired by the C# method name (and also because <code>return</code> is a reserved keyword in F#). </p> <h3 id="f6b42483d4454e8a8e3e6d580b0595db"> Left identity <a href="#f6b42483d4454e8a8e3e6d580b0595db" title="permalink">#</a> </h3> <p> We need to identify the <em>return</em> function in order to examine <a href="/2022/04/11/monad-laws">the monad laws</a>. Now that this is done, let's see what the laws look like for the asynchronous monads, starting with the left identity law. </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">TaskHasLeftIdentity</span>(Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">h_</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Task&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Task.FromResult; &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#74531f;">h</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;Task.FromResult(h_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(@return(a).SelectMany(h).Result,&nbsp;h(a).Result); }</pre> </p> <p> Like in <a href="/2022/05/30/the-lazy-monad">the previous article</a> the test uses <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> 2.11.0 and <a href="https://xunit.net/">xUnit.net</a> 2.4.0. FScheck can generate arbitrary functions in addition to arbitrary values, but it unfortunately, it can't generate asynchronous computations. Instead, I've asked FsCheck to generate a function that I then convert to an asynchronous computation. </p> <p> The code I'm using for this article is quite old, and neither FsCheck 2.11.0 nor xUnit.net 2.4.0 can handle asynchronous unit tests (a capability that later versions do have). Thus, the assertion has to force the computations to run by accessing the <code>Result</code> property. Not modern best practice, but it gets the point across, I hope. </p> <p> In F# I wrote monad law examples using <a href="/2022/04/04/kleisli-composition">Kleisli composition</a>. I first defined a function called <code>fish</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;(&#39;b&nbsp;-&gt;&nbsp;Async&lt;&#39;c&gt;)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;c&gt;</span> <span style="color:blue;">let</span>&nbsp;fish&nbsp;f&nbsp;g&nbsp;x&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;f&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;g&nbsp;x&#39;&nbsp;}</pre> </p> <p> Keep in mind that <em>fish</em> is also a verb, so that's okay for a function name. The function then implements the <em>fish operator:</em> </p> <p> <pre><span style="color:blue;">let</span>&nbsp;(&gt;=&gt;)&nbsp;=&nbsp;Async.fish</pre> </p> <p> This enables us to give an example of the left identity law using Kleisli composition: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Async&nbsp;fish&nbsp;has&nbsp;left&nbsp;identity``&nbsp;(h&#39;&nbsp;:&nbsp;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string)&nbsp;a&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;h&nbsp;x&nbsp;=&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;h&#39;&nbsp;x&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;left&nbsp;=&nbsp;Async.fromValue&nbsp;&gt;=&gt;&nbsp;h &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;right&nbsp;=&nbsp;h &nbsp;&nbsp;&nbsp;&nbsp;Async.RunSynchronously&nbsp;(left&nbsp;a)&nbsp;=!&nbsp;Async.RunSynchronously&nbsp;(right&nbsp;a)</pre> </p> <p> The <code>=!</code> operator is an <a href="https://github.com/SwensenSoftware/Unquote">Unquote</a> operator that I usually read as <em>must equal</em>. It's a test assertion that'll throw an exception if the left and right sides aren't equal. </p> <h3 id="ef0d98230ec6429c855f15a7a876fdad"> Right identity <a href="#ef0d98230ec6429c855f15a7a876fdad" title="permalink">#</a> </h3> <p> In a similar manner, we can showcase the right identity law as a test - first in C#: </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">TaskHasRightIdentity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Task&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Task.FromResult; &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;Task.FromResult(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(@return).Result,&nbsp;m.Result); }</pre> </p> <p> Here's a Kleisli-composition-based F# property that demonstrates the right identity law for asynchronous workflows: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Async&nbsp;fish&nbsp;has&nbsp;right&nbsp;identity``&nbsp;(f&#39;&nbsp;:&nbsp;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string)&nbsp;a&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;f&nbsp;x&nbsp;=&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;f&#39;&nbsp;x&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;left&nbsp;=&nbsp;f &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;right&nbsp;=&nbsp;f&nbsp;&gt;=&gt;&nbsp;Async.fromValue &nbsp;&nbsp;&nbsp;&nbsp;Async.RunSynchronously&nbsp;(left&nbsp;a)&nbsp;=!&nbsp;Async.RunSynchronously&nbsp;(right&nbsp;a)</pre> </p> <p> As always, even a property-based test constitutes no <em>proof</em> that the law holds. I show it only to illustrate what the laws look like in 'real' code. </p> <h3 id="cc6f13f397574584b0c3dbb55cb15726"> Associativity <a href="#cc6f13f397574584b0c3dbb55cb15726" title="permalink">#</a> </h3> <p> The last monad law is the associativity law that describes how (at least) three functions compose. </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">TaskIsAssociative</span>( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;DateTime,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">f_</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">g_</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;<span style="color:#1f377f;">h_</span>, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">f</span>(DateTime&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;Task.FromResult(f_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#74531f;">g</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;Task.FromResult(g_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;<span style="color:blue;">byte</span>&gt;&nbsp;<span style="color:#74531f;">h</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;Task.FromResult(h_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(g).SelectMany(h).Result,&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h)).Result); }</pre> </p> <p> This property once more relies on FsCheck's ability to generate arbitrary <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>, which it then converts to asynchronous computations. The same does the Kleisli-composition-based F# property: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Async&nbsp;fish&nbsp;is&nbsp;associative``&nbsp;(f&#39;&nbsp;:&nbsp;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string)&nbsp;(g&#39;&nbsp;:&nbsp;string&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;byte)&nbsp;(h&#39;&nbsp;:&nbsp;byte&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;bool)&nbsp;a&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;f&nbsp;x&nbsp;=&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;f&#39;&nbsp;x&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;g&nbsp;x&nbsp;=&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;g&#39;&nbsp;x&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;h&nbsp;x&nbsp;=&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;h&#39;&nbsp;x&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;left&nbsp;=&nbsp;(f&nbsp;&gt;=&gt;&nbsp;&nbsp;g)&nbsp;&gt;=&gt;&nbsp;h &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;right&nbsp;=&nbsp;&nbsp;f&nbsp;&gt;=&gt;&nbsp;(g&nbsp;&nbsp;&gt;=&gt;&nbsp;h) &nbsp;&nbsp;&nbsp;&nbsp;Async.RunSynchronously&nbsp;(left&nbsp;a)&nbsp;=!&nbsp;Async.RunSynchronously&nbsp;(right&nbsp;a)</pre> </p> <p> It's easier to see the associativity that the law is named after when using Kleisli composition, but as <a href="/2022/04/11/monad-laws">the article about the monad laws</a> explained, the two variations are equivalent. </p> <h3 id="c7e119135d3243e9b29dae38ab6969bd"> Conclusion <a href="#c7e119135d3243e9b29dae38ab6969bd" title="permalink">#</a> </h3> <p> Whether you do asynchronous programming with <code>Task&lt;T&gt;</code> or <code>Async&lt;'a&gt;</code>, asynchronous computations form monads. This enables you to piecemeal compose asynchronous workflows. </p> <p> <strong>Next:</strong> <a href="/2022/06/20/the-state-monad">The State monad</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Lazy monad https://blog.ploeh.dk/2022/05/30/the-lazy-monad 2022-05-30T05:34:00+00:00 Mark Seemann <div id="post"> <p> <em>Lazy computations form a monad. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. A previous article described how <a href="/2018/09/10/the-lazy-functor">lazy computations form a functor</a>. In this article, you'll see that lazy computations also form a monad. </p> <h3 id="749d0adfe50046dbaf3fdb63c1f28ebd"> SelectMany <a href="#749d0adfe50046dbaf3fdb63c1f28ebd" title="permalink">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function. In C#, monadic bind is called <code>SelectMany</code>. You can define one as an extension method on the <a href="https://docs.microsoft.com/dotnet/api/system.lazy-1">Lazy&lt;T&gt;</a> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Lazy&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Lazy&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Lazy&lt;TResult&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Lazy&lt;TResult&gt;(()&nbsp;=&gt;&nbsp;selector(source.Value).Value); }</pre> </p> <p> While the implementation seemingly forces evaluation by accessing the <code>Value</code> property, this all happens inside a lambda expression that defers execution. </p> <p> If <code>x</code> is a <code>Lazy&lt;int&gt;</code> and <code>SlowToString</code> is a function that takes an <code>int</code> as input and returns a <code>Lazy&lt;string&gt;</code> you can compose them like this: </p> <p> <pre>Lazy&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">y</span>&nbsp;=&nbsp;x.SelectMany(SlowToString);</pre> </p> <p> The result is another lazy computation that, when forced, will produce a <code>string</code>. </p> <h3 id="e84186c0a20546e393b2d803c1af01b1"> Query syntax <a href="#e84186c0a20546e393b2d803c1af01b1" title="permalink">#</a> </h3> <p> Monads also enable query syntax in C# (just like they enable other kinds of syntactic sugar in languages like <a href="https://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>). As outlined in the <a href="/2022/03/28/monads">monad introduction</a>, however, you must add a special <code>SelectMany</code> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Lazy&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Lazy&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Lazy&lt;U&gt;&gt;&nbsp;<span style="color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;U,&nbsp;TResult&gt;&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> This would enable you to rewrite the above example like this: </p> <p> <pre>Lazy&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">y</span>&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;SlowToString(i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;s;</pre> </p> <p> The behaviour is the same as above. It's just two different ways of writing the same expression. The C# compiler desugars the query-syntax expression to one that composes with <code>SelectMany</code>. </p> <h3 id="8170cd0d693c455fa02a44bd293100a8"> Flatten <a href="#8170cd0d693c455fa02a44bd293100a8" title="permalink">#</a> </h3> <p> In <a href="/2022/03/28/monads">the introduction</a> you learned that if you have a <code>Flatten</code> or <code>Join</code> function, you can implement <code>SelectMany</code>, and the other way around. Since we've already defined <code>SelectMany</code> for <code>Lazy&lt;T&gt;</code>, we can use that to implement <code>Flatten</code>. In this article I use the name <code>Flatten</code> rather than <code>Join</code>. This is an arbitrary choice that doesn't impact behaviour. Perhaps you find it confusing that I'm inconsistent, but I do it in order to demonstrate that the behaviour is the same even if the name is different. </p> <p> The concept of a monad is universal, but the names used to describe its components differ from language to language. What C# calls <code>SelectMany</code>, Scala calls <code>flatMap</code>, and what Haskell calls <code>join</code>, other languages may call <code>Flatten</code>. </p> <p> You can always implement <code>Flatten</code> by using <code>SelectMany</code> with the identity function. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Lazy&lt;T&gt;&nbsp;<span style="color:#74531f;">Flatten</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Lazy&lt;Lazy&lt;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> You could also compose the above <code>x</code> and <code>SlowToString</code> with <code>Select</code> and <code>Flatten</code>, like this: </p> <p> <pre>Lazy&lt;Lazy&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">nested</span>&nbsp;=&nbsp;x.Select(SlowToString); Lazy&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">flattened</span>&nbsp;=&nbsp;nested.Flatten();</pre> </p> <p> The <code>flattened</code> value remains deferred until you force execution. </p> <h3 id="e1c48063295e4a4ea8f91dc2b5539219"> Return <a href="#e1c48063295e4a4ea8f91dc2b5539219" title="permalink">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that Haskell uses). You don't, however, have to define a static method called <code>Return</code>. What's of importance is that the capability exists. For <code>Lazy&lt;T&gt;</code> in C# the idiomatic way would be to use a constructor, but the version of .NET I'm using for this code (this is actually code I wrote years ago) doesn't have such a constructor (newer versions do). Instead, I'll define a function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Lazy&lt;T&gt;&nbsp;<span style="color:#74531f;">Return</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Lazy&lt;T&gt;(()&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> In other words, <code>Return</code> wraps a pre-existing value in a lazy computation. </p> <h3 id="078ba13f5e33436a915b386b5826c05d"> Left identity <a href="#078ba13f5e33436a915b386b5826c05d" title="permalink">#</a> </h3> <p> We need to identify the <em>return</em> function in order to examine <a href="/2022/04/11/monad-laws">the monad laws</a>. Now that this is done, let's see what the laws look like for the Lazy monad, starting with the left identity law. </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LazyHasLeftIdentity</span>(Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">h_</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Lazy&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Lazy.Return; &nbsp;&nbsp;&nbsp;&nbsp;Lazy&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#74531f;">h</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;Lazy.Return(h_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(@return(a).SelectMany(h).Value,&nbsp;h(a).Value); }</pre> </p> <p> Like in <a href="/2022/05/16/the-identity-monad">the previous article</a> the test uses <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> 2.11.0 and <a href="https://xunit.net/">xUnit.net</a> 2.4.0. FScheck can generate arbitrary functions in addition to arbitrary values, but it unfortunately, it can't generate lazy computations. Instead, I've asked FsCheck to generate a function that I then convert to a lazy computation. </p> <p> In order to compare the values, the assertion has to force evaluation by reading the <code>Value</code> properties. </p> <h3 id="e3c13eb7e8ee4bd789f7b477d3cb5945"> Right identity <a href="#e3c13eb7e8ee4bd789f7b477d3cb5945" title="permalink">#</a> </h3> <p> In a similar manner, we can showcase the right identity law as a test. </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LazyHasRightIdentity</span>(Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">f_</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Lazy&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;Lazy.Return(f_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Lazy&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Lazy.Return; &nbsp;&nbsp;&nbsp;&nbsp;Lazy&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(@return).Value,&nbsp;m.Value); }</pre> </p> <p> As always, even a property-based test constitutes no <em>proof</em> that the law holds. I show it only to illustrate what the laws look like in 'real' code. </p> <h3 id="9cd90607b2974086a9194fd5e9c1e712"> Associativity <a href="#9cd90607b2974086a9194fd5e9c1e712" title="permalink">#</a> </h3> <p> The last monad law is the associativity law that describes how (at least) three functions compose. </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LazyIsAssociative</span>( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">f_</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;<span style="color:#1f377f;">g_</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">byte</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">h_</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Lazy&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#74531f;">f</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;Lazy.Return(f_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Lazy&lt;<span style="color:blue;">byte</span>&gt;&nbsp;<span style="color:#74531f;">g</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;Lazy.Return(g_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Lazy&lt;TimeSpan&gt;&nbsp;<span style="color:#74531f;">h</span>(<span style="color:blue;">byte</span>&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;Lazy.Return(h_(x)); &nbsp;&nbsp;&nbsp;&nbsp;Lazy&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(g).SelectMany(h).Value,&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h)).Value); }</pre> </p> <p> This property once more relies on FsCheck's ability to generate arbitrary <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>, which it then converts to lazy computations. </p> <h3 id="5c5416d137e444baa298182dbf315235"> Conclusion <a href="#5c5416d137e444baa298182dbf315235" title="permalink">#</a> </h3> <p> The Lazy functor (which <a href="/2018/12/17/the-lazy-applicative-functor">is also an applicative functor</a>) is also a monad. This can be used to combine multiple lazily computed values into a single lazily computed value. </p> <p> <strong>Next:</strong> <a href="/2022/06/06/asynchronous-monads">Asynchronous monads</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Waiting to never happen https://blog.ploeh.dk/2022/05/23/waiting-to-never-happen 2022-05-23T05:55:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of how non-deterministic code can cause test cases to wither.</em> </p> <p> It seems to me that when people discuss functional programming, they spend much time discussing side effects and how to avoid them. Sometimes, they almost forget that non-deterministic behaviour is also something to avoid. </p> <p> On the other hand, we've known for a long time that we should <a href="https://martinfowler.com/articles/nonDeterminism.html">eradicate non-determinism in tests</a>. Martin Fowler's article, however, mostly discusses <a href="https://en.wikipedia.org/wiki/False_positives_and_false_negatives">false positives</a>: Tests that fail even when no defect is manifest. </p> <p> Unit tests may also exhibit <a href="http://xunitpatterns.com/false%20negative.html">false negatives</a>. This can happen for a variety of reason. In my article <a href="/2019/10/14/tautological-assertion">Tautological assertion</a> I describe one example. </p> <p> The passing of time, however, has a tendency to introduce decay. This may apply to test suites as well. If a test or the System Under Test depends on the current time, a test may over time transition from a proper test to one that still passes, but for the wrong reason. </p> <h3 id="68269f84da2845d2994b378001326bac"> A test example <a href="#68269f84da2845d2994b378001326bac" title="permalink">#</a> </h3> <p> I was recently looking at this test: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">ChangeDateToSoldOutDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r1</span>&nbsp;=&nbsp;Some.Reservation; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r2</span>&nbsp;=&nbsp;Some.Reservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithId(Guid.NewGuid()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.TheDayAfter() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r1); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto</span>&nbsp;=&nbsp;r1.WithDate(r2.At).ToDto(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Put(r1.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>),&nbsp;dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">oRes</span>&nbsp;=&nbsp;Assert.IsAssignableFrom&lt;ObjectResult&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StatusCodes.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oRes.StatusCode); }</pre> </p> <p> It's from the code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. It's a false negative waiting to happen. What's the problem? </p> <p> Before I start describing the problem, I think that I owe you a short explanation of what you're looking at. The book has a single example code base that it uses to showcase various heuristics and techniques. The code base implements an online restaurant reservation REST API. I've <a href="/2021/06/14/new-book-code-that-fits-in-your-head">previously discussed that code base on this blog</a>. </p> <p> This particular test exercises the following test case: A client can use the REST API to change an existing reservation. When it does that, the API should check that all business rules apply. Particularly, in this case, if you have an existing reservation (<code>r1</code>), but try to change it to another date, and that other date is already sold out, the update should be refused. </p> <p> The test uses <a href="http://xunitpatterns.com/Back%20Door%20Manipulation.html">backdoor manipulation</a> to establish the initial state. It's a <a href="/2019/02/18/from-interaction-based-to-state-based-testing">state-based integration test</a> that uses an in-memory <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> database. It adds two reservations (<code>r1</code> and <code>r2</code>) to the database, on two different days. </p> <p> What's not at all clear from the above code is that that <code>10</code> is the <code>Grandfather.Restaurant</code>'s maximum capacity. (This is a fair criticism against this test, but not what this article is about.) It effectively means that the restaurant is sold out that day. </p> <p> The test then, in <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">the act phase</a>, tries to change <code>r1</code> to the date that's sold out and <code>Put</code> that change. </p> <p> The assertion checks that this isn't possible. </p> <h3 id="994d6756ca4f444bbeba236980914a23"> A false negative waiting to happen <a href="#994d6756ca4f444bbeba236980914a23" title="permalink">#</a> </h3> <p> What's the problem with the above test? </p> <p> The problem is that <em>when I wrote it</em>, it exercised the above test case, but now it no longer does. </p> <p> At this point, you're probably wondering. The test case is supposed to change the date of <code>r1</code> to the date of <code>r2</code>, but no dates are visible in the test. Which dates does the test use? </p> <p> The answer may be found in the <code>Some</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Reservation&nbsp;Reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Guid(0x81416928,&nbsp;0xC236,&nbsp;0x4EBF,&nbsp;0xA4,&nbsp;0x50,&nbsp;0x24,&nbsp;0x95,&nbsp;0xA4,&nbsp;0xDA,&nbsp;0x92,&nbsp;0x30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Now, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(<span style="color:#a31515;">&quot;x@example.net&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1);</pre> </p> <p> <code>Some.Reservation</code> is an <a href="/2017/09/11/test-data-without-builders">immutable test datum</a>. It's intended to be used as a representative example of a <code>Reservation</code> value. The reservation date and time (the <code>At</code> property) is another immutable test datum: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;DateTime&nbsp;Now&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;DateTime(2022,&nbsp;4,&nbsp;1,&nbsp;20,&nbsp;15,&nbsp;0);</pre> </p> <p> I wrote most of that code base in 2020. April 1st 2022 seemed far in the future, and I needed a value that could represent 'approximately' the current time. It didn't have to be the day the code would run, but (as it turns out) it's important that this date isn't in the past. Which it now is. </p> <p> What happens now that <code>Some.Reservation</code> is in the past? </p> <p> Nothing, and that's exactly what is the problem. </p> <h3 id="af00b9fef7124afd93b3b80bac4efbf5"> A new path <a href="#af00b9fef7124afd93b3b80bac4efbf5" title="permalink">#</a> </h3> <p> Once the clock rolled over from 2022-04-02 20:14:59 to 2022-04-02 20:15:00, the pathway through the code changed. Why April 2 and not April 1? Because the date of importance in the test is <code>TheDayAfter</code>, and <code>TheDayAfter</code> is defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Reservation&nbsp;<span style="color:#74531f;">TheDayAfter</span>(<span style="color:blue;">this</span>&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;reservation.AddDate(TimeSpan.FromDays(1)); }</pre> </p> <p> So <code>r2.At</code> is 20:15 April 2, 2022. </p> <p> Before 2022-04-02 20:15:00 the test would exercise the test case it was supposed to. After all, I did write the test <em>before</em> I wrote the implementation, and I did <a href="/2019/10/21/a-red-green-refactor-checklist">see it fail before I saw it pass</a>. </p> <p> Once, however, <code>dto</code> is in the past, another pathway takes over. The <a href="/2020/01/27/the-maitre-d-kata">Maître d's</a> core decision logic (<a href="/2020/11/30/name-by-role">WillAccept</a>) contains this Guard Clause: </p> <p> <pre><span style="color:#8f08c4;">if</span>&nbsp;(candidate.At&nbsp;&lt;&nbsp;now) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>;</pre> </p> <p> Long before the domain model investigates whether it can accommodate a reservation, it rejects all reservations in the past. The calling code, however, only sees that the return value is <code>false</code>. In other words, the externally observable behaviour is the same. This also means that test's assertion still passes. From the outside, you can't tell the difference. </p> <h3 id="8a3330c5362c47cbb5431a20a90f1278"> Coverage decay <a href="#8a3330c5362c47cbb5431a20a90f1278" title="permalink">#</a> </h3> <p> If the behaviour is stable, then why is this a problem? </p> <p> This is a problem because this decreases test coverage. While <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">code coverage is a useless target measure</a>, it's not a completely useless metric. The absolute percentage is less relevant than the trend. If code coverage decreases, one should at least investigate. </p> <p> Here, specifically, it means that I thought I had a particular test case covered, and in reality it turns out that that's no longer the case. Now the test just verifies that you can't update past reservations, regardless of application state. Other tests already verify that, however, so this test now became redundant. </p> <p> As I've <a href="/2018/11/12/what-to-test-and-not-to-test">described before</a>, each test case exercises a particular path through the System Under Test: </p> <p> <img src="/content/binary/one-test-path-through-complex-unit.png" alt="Diagram that shows a unit test exercising one path through a unit."> </p> <p> If you have high code coverage, an aggregate diagram might look like this: </p> <p> <img src="/content/binary/most-test-paths-through-complex-unit.png" alt="Diagram that shows several unit tests exercising most paths through a unit."> </p> <p> Notice that the top box now says <em>Unit tests</em> in plural. Together, the tests exercise most pathways through the code. There's one pathway not exercised: The path from the light blue node to the orange node. The point is that this diagram illustrates <em>high</em> code coverage, not complete code coverage. The following argument applies to that general situation. </p> <p> What happened with the above test is that coverage <em>decayed</em>. </p> <p> <img src="/content/binary/some-test-paths-through-complex-unit.png" alt="Diagram that shows several unit tests exercising one fewer paths through a unit that before."> </p> <p> In this figure, the green pathway is no longer exercised. </p> <p> Not only did coverage decrease: Unless you're monitoring code coverage, you probably didn't notice. The code didn't change. Time just passed. </p> <h3 id="c8a7facb117247baaddceca8ae45ffc6"> Threat to refactoring <a href="#c8a7facb117247baaddceca8ae45ffc6" title="permalink">#</a> </h3> <p> Why is this a problem? After all, code coverage isn't a goal; it's just a measure. </p> <p> As <a href="https://martinfowler.com">Martin Fowler</a> writes: </p> <blockquote> <p> "to refactor, the essential precondition is [...] solid tests" </p> <footer><cite>Martin Fowler, <a href="/ref/refactoring">Refactoring</a></cite></footer> </blockquote> <p> A good test suite verifies that the behaviour of your software doesn't change as you reorganise the code. The underlying assumption is that the test suite exercises important use cases and protects against regressions. </p> <p> The above test is an example of a test case that silently disappears. If you think that your tests cover all important use cases, you may feel confident refactoring. After all, if all tests pass, you didn't break any existing behaviour. </p> <p> Unbeknownst to you, however, the test suite may become less trustworthy. As time passes, test cases may disappear, as exemplified by the above test. While the test suite may be passing, a refactoring could introduce a regression - of behaviour you believed to be already covered. </p> <h3 id="ad442fa0b7dd4c7a9d46a71771d75184"> Under the radar <a href="#ad442fa0b7dd4c7a9d46a71771d75184" title="permalink">#</a> </h3> <p> Why didn't I catch this earlier? After all, <a href="/2021/01/11/waiting-to-happen">I'd already identified the problem</a> in the code base. Once I realised the problem, I took steps to remedy it. I loaded the code base on a spare computer and changed the machine's year to a future year (<a href="https://en.wikipedia.org/wiki/Year_2038_problem">2038</a>, if I recall correctly). Then I ran the tests to see which ones failed. </p> <p> I examined each failing test to verify that it actually failed because of the time and date. Then I corrected the test to make it relative to the system clock. </p> <p> That took care of all the tests that would eventually break. On the other hand, it didn't unearth any of the tests that over time silently would become false negatives. </p> <h3 id="c5bcb89e54714544b48c5cfbd7974e47"> Make it relative <a href="#c5bcb89e54714544b48c5cfbd7974e47" title="permalink">#</a> </h3> <p> Fixing the above test was easy: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">ChangeDateToSoldOutDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r1</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Some.Reservation.WithDate(DateTime.Now.AddDays(8).At(20,&nbsp;15)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r2</span>&nbsp;=&nbsp;r1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithId(Guid.NewGuid()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.TheDayAfter() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithQuantity(10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r1); &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather.Add(r2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto</span>&nbsp;=&nbsp;r1.WithDate(r2.At).ToDto(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Put(r1.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>),&nbsp;dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">oRes</span>&nbsp;=&nbsp;Assert.IsAssignableFrom&lt;ObjectResult&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StatusCodes.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oRes.StatusCode); }</pre> </p> <p> The only change is to the creation of <code>r1</code> and <code>r2</code>. The test now explicitly sets the <code>r1</code> date eight days in the future. It also derives <code>r2</code> from <code>r1</code>, which makes the relationship between the two values more explicit. This in itself is a small improvement, I think. </p> <h3 id="7caaa6f5a1874d23a4712af241a2b87d"> Deterministic behaviour <a href="#7caaa6f5a1874d23a4712af241a2b87d" title="permalink">#</a> </h3> <p> More than one person has told me that I obsess over details to a degree that can't be expected of most developers. If that's true, problems like this are likely to go unnoticed. After all, I discovered the problem because I was carefully reviewing each test more than a year after I originally wrote it. While I had a reason to do that, we can't expect developers to do that. </p> <p> Are we doomed to suffer this kind of error, then? I don't have a <a href="/2019/07/01/yes-silver-bullet">silver bullet</a> for you, but I don't think that all is lost. A little discipline may help. As I write in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, pair programming or code reviews can help raise awareness of issues. </p> <p> On the tactical level, you might want to consider a policy that discourages absolute dates and times in unit tests. </p> <p> On the strategic level, you may want to adopt the <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">Functional Core, Imperative Shell</a> architecture. After all, <a href="/2015/05/07/functional-design-is-intrinsically-testable">pure functions are intrinsically testable</a>. </p> <p> As I wrote in the beginning of this article, most people tend to focus on how to avoid side effects when discussing functional programming. A <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, however, must be <em>both</em> side-effect-free and deterministic. </p> <p> This means that instead of learning many specific rules (such as not using absolute time in unit tests), you may focus on one general rule: Write and test pure functions. </p> <p> To be honest, though, this rule doesn't solve all problems. Even in a functional-core-imperative-shell architecture, the imperative shell isn't pure. The above integration test is, unfortunately, a test of the imperative shell. It's already using the <code>SystemClock</code>, and that's no oversight on my part. While I do favour the <a href="https://martinfowler.com/bliki/TestPyramid.html">test pyramid</a>, I also write integration tests - just as the test pyramid encourages me to do. I find it valuable that integration tests integrate as many components as possible. The more real components an integration test uses, the better the chance that it uncovers problems. </p> <p> With integration tests, then, diligence is required to keep tests deterministic. In that context you can't rely on a universal rule like <em>just write pure functions</em>, because <a href="/2022/05/02/at-the-boundaries-applications-arent-functional">at the boundaries, programs aren't functional</a>. Instead, some diligence and discipline is required. At least, I don't see other alternatives, but perhaps you do? </p> <h3 id="d92d984d1c064aa1ac11d5bc01854e57"> Conclusion <a href="#d92d984d1c064aa1ac11d5bc01854e57" title="permalink">#</a> </h3> <p> If you use absolute dates in unit tests, what was once a future date may some day become a past date. This can change which pathways of the System Under Test an automated test exercises. </p> <p> Is this important? It's probably not the most pressing problem you may run into. Even if test coverage silently decays, it may not be a problem if you never refactor the code in question. It's only if, during a refactoring, you introduce a defect that goes unnoticed that this may become a problem. It doesn't seem like the most likely of errors, but on the other hand, these kinds of unexpected problems have a tendency to occur at the most inopportune moments. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c0b2e0bd555b5d5c5555c60bf11bff69"> <div class="comment-author"><a href="https://github.com/ladeak">Laszlo</a> <a href="#c0b2e0bd555b5d5c5555c60bf11bff69">#</a></div> <div class="comment-content"> <p> Why have you decided to make the date of the reservation relative to the SystemClock, and not the other way around? Would it be more deterministic to use a faked system clock instead? </p> </div> <div class="comment-date">2022-05-30 17:12 UTC</div> </div> <div class="comment" id="552d192249594844937724b2b2f86efe"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#552d192249594844937724b2b2f86efe">#</a></div> <div class="comment-content"> <p> Laszlo, thank you for asking. I wrote <a href="/2022/06/27/test-double-clocks">a new article</a> to answer your question. </p> </div> <div class="comment-date">2022-06-27 5:48 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Identity monad https://blog.ploeh.dk/2022/05/16/the-identity-monad 2022-05-16T05:49:00+00:00 Mark Seemann <div id="post"> <p> <em>Probably one of the least useful monads. An article for object-oriented developers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. </p> <p> <a href="/2018/09/03/the-identity-functor">The Identity functor</a> is another example of a functor that also forms a monad. Contrary to useful monads like <a href="/2022/04/19/the-list-monad">list</a>, <a href="/2022/04/25/the-maybe-monad">Maybe</a>, and <a href="/2022/05/09/an-either-monad">Either</a>, the Identity monad isn't particularly useful. In <a href="https://www.haskell.org">Haskell</a> it serves a few niche positions, for example when it comes to monad transformers. It's possible to define abstractions at a higher level in Haskell than it is in many other languages. Some of those abstractions involve monads. The Identity monad can plug into these abstractions as a 'do-nothing' monad, a bit like the <a href="https://en.wikipedia.org/wiki/Null_object_pattern">Null Object pattern</a> can supply a 'do-nothing' implementation of an interface. </p> <p> Other languages with which I'm proficient (C#, <a href="https://fsharp.org">F#</a>) don't enable that kind of abstraction, and I've never felt the need to actually <em>use</em> the Identity monad in those languages. Still, the Identity monad exists mathematically and conceptually, and it can be useful to know of that existence. This implies, like the Null Object pattern, that some abstractions can be simplified by 'plugging in' the Identity monad. The alternative would have had to involve some hand-waving about special cases. </p> <p> The code in this article continues the implementation shown in the article about <a href="/2018/09/03/the-identity-functor">the Identity functor</a>. </p> <h3 id="db0c7e97a3ee4477a96078c22cf89330"> SelectMany <a href="#db0c7e97a3ee4477a96078c22cf89330" title="permalink">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function. In C#, monadic bind is called <code>SelectMany</code>. For <code>Identity&lt;T&gt;</code> the implementation is trivial: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Identity&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(Func&lt;T,&nbsp;Identity&lt;TResult&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;selector(Item); }</pre> </p> <p> This is an instance method on <code>Identity&lt;T&gt;</code>, which means that the <code>Item</code> property has the type <code>T</code>. Since the <code>selector</code> return a new <code>Identity&lt;TResult&gt;</code>, the method can simply return that value. </p> <h3 id="9e4f558b6a7542148090044ab8e19060"> Query syntax <a href="#9e4f558b6a7542148090044ab8e19060" title="permalink">#</a> </h3> <p> As usual, you can implement C# query syntax with a boilerplate overload of <code>SelectMany</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Identity&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Identity&lt;U&gt;&gt;&nbsp;<span style="color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;U,&nbsp;TResult&gt;&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> The implementation is the same as in the previous articles in this article series. If it looks a little different than, say, the implementation for <a href="/2022/04/25/the-maybe-monad">the Maybe monad</a>, it's only because this <code>SelectMany</code> overload is an instance method, whereas the Maybe implementation is an extension method. </p> <p> Query syntax enables syntactic sugar like this: </p> <p> <pre>Identity&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;x&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Identity&lt;<span style="color:blue;">int</span>&gt;(39) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;y&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Identity&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;x&nbsp;+&nbsp;y.Length;</pre> </p> <p> The C# compiler infers that <code>x</code> is an <code>int</code> and <code>y</code> is a <code>string</code>. </p> <h3 id="9ee27fde0adc488b82f4c5ec358bc17c"> Flatten <a href="#9ee27fde0adc488b82f4c5ec358bc17c" title="permalink">#</a> </h3> <p> As <a href="/2022/03/28/monads">the monad introduction</a> also points out, if you have monadic bind (<code>SelectMany</code>) you can always implement a function to flatten a nested <a href="/2018/03/22/functors">functor</a>. This function is sometimes called <em>join</em> and sometimes (perhaps more intuitively) <em>flatten</em>. Here I've chosen to call it <code>Flatten</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Identity&lt;T&gt;&nbsp;<span style="color:#74531f;">Flatten</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Identity&lt;Identity&lt;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> Even though the other methods shown so far have been instance methods, <code>Flatten</code> has to be an extension method, since the source is a nested <code>Identity&lt;Identity&lt;T&gt;&gt;</code>. There's no class of that specific type - only <code>Identity&lt;T&gt;</code>. </p> <p> If there's one useful aspect of the Identity monad, it may be that it reveals the concept of a nested functor in all its triviality: </p> <p> <pre>Identity&lt;Identity&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">nested</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Identity&lt;Identity&lt;<span style="color:blue;">string</span>&gt;&gt;(<span style="color:blue;">new</span>&nbsp;Identity&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>)); Identity&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">flattened</span>&nbsp;=&nbsp;nested.Flatten();</pre> </p> <p> Recall: A monad is a functor you can flatten. </p> <h3 id="be7d9a35a21a48498f469ae3a3357bc0"> Return <a href="#be7d9a35a21a48498f469ae3a3357bc0" title="permalink">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that Haskell uses). You don't, however, have to define a static method called <code>Return</code>. What's of importance is that the capability exists. For <code>Identity&lt;T&gt;</code> in C# the idiomatic way would be to use a constructor. This constructor does double duty as monadic <em>return</em>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Identity</span>(T&nbsp;<span style="color:#1f377f;">item</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; }</pre> </p> <p> In other words, <em>return</em> just wraps a value in the <code>Identity&lt;T&gt;</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a>. </p> <h3 id="6bc314d479564086a2acd46dbccf44dd"> Left identity <a href="#6bc314d479564086a2acd46dbccf44dd" title="permalink">#</a> </h3> <p> We need to identify the <em>return</em> function in order to examine <a href="/2022/04/11/monad-laws">the monad laws</a>. Let's see what they look like for the Identity monad, starting with the left identity law. </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IdentityHasLeftIdentity</span>(Func&lt;<span style="color:blue;">int</span>,&nbsp;Identity&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Identity&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Identity&lt;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(@return(a).SelectMany(h),&nbsp;h(a)); }</pre> </p> <p> This example is a bit different compared to the previous examples, since it uses <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> 2.11.0 and <a href="https://xunit.net/">xUnit.net</a> 2.4.0. FScheck can generate arbitrary functions in addition to arbitrary values, so I've simply asked it to generate some arbitrary <code>h</code> functions. </p> <h3 id="b97ac93ff7d74d0ba165dc847f604d0d"> Right identity <a href="#b97ac93ff7d74d0ba165dc847f604d0d" title="permalink">#</a> </h3> <p> In a similar manner, we can showcase the right identity law as a test. </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IdentityHasRightIdentity</span>(Func&lt;<span style="color:blue;">string</span>,&nbsp;Identity&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Identity&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Identity&lt;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;Identity&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(@return),&nbsp;m); }</pre> </p> <p> As always, even a property-based test constitutes no <em>proof</em> that the law holds. I show it only to illustrate what the laws look like in 'real' code. </p> <h3 id="fc12cf871df6428fb9e14906e9c65707"> Associativity <a href="#fc12cf871df6428fb9e14906e9c65707" title="permalink">#</a> </h3> <p> The last monad law is the associativity law that describes how (at least) three functions compose. </p> <p> <pre>[Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IdentityIsAssociative</span>( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Identity&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Identity&lt;<span style="color:blue;">byte</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">byte</span>,&nbsp;Identity&lt;TimeSpan&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Identity&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(g).SelectMany(h),&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h))); }</pre> </p> <p> This property once more relies on FsCheck's ability to generate arbitrary <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> <h3 id="33f53ea7db964a62a542244b9de58499"> F# <a href="#33f53ea7db964a62a542244b9de58499" title="permalink">#</a> </h3> <p> While the <code>Identity</code> monad is built into the Haskell standard library, there's no Identity monad in F#. While it can be occasionally useful in Haskell, Identity is as useless in F# as it is in C#. Again, that doesn't imply that you can't define it. You can: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Identity&lt;&#39;a&gt;&nbsp;=&nbsp;Identity&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a <span style="color:blue;">module</span>&nbsp;Identity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Identity&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;get&nbsp;(Identity&nbsp;x)&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Identity&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Identity&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;(Identity&nbsp;x)&nbsp;=&nbsp;Identity&nbsp;(f&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Identity&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;Identity&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Identity&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;bind&nbsp;f&nbsp;(Identity&nbsp;x)&nbsp;:&nbsp;Identity&lt;_&gt;&nbsp;=&nbsp;f&nbsp;x</pre> </p> <p> Here I've repeated the functor implementation from <a href="/2018/09/03/the-identity-functor">the article about the Identity functor</a>, but now also included the <code>bind</code> function. </p> <p> You can use such a module to support syntactic sugar in the form of <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;IdentityBuilder()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;_.Bind(x,&nbsp;f)&nbsp;=&nbsp;Identity.bind&nbsp;f&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;_.Return&nbsp;x&nbsp;=&nbsp;Identity&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;_.ReturnFrom&nbsp;x&nbsp;=&nbsp;x <span style="color:blue;">let</span>&nbsp;identity&nbsp;=&nbsp;IdentityBuilder&nbsp;()</pre> </p> <p> This enables you to write code like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;identity&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&nbsp;=&nbsp;Identity&nbsp;<span style="color:#a31515;">&quot;corge&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;y&nbsp;=&nbsp;Identity&nbsp;47 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y&nbsp;-&nbsp;x.Length&nbsp;}</pre> </p> <p> Here, <code>i</code> is a value of the type <code>Identity&lt;int&gt;</code>. </p> <h3 id="7b30db7a0912447c8b3aa6d9da8691e9"> Conclusion <a href="#7b30db7a0912447c8b3aa6d9da8691e9" title="permalink">#</a> </h3> <p> If the Identity monad is useful in languages like C# and F#, I have yet to encounter a use case. Even so, it exists. It is also, perhaps, the most naked illustration of what a functor and monad is. As such, I think it may be helpful to study. </p> <p> <strong>Next:</strong> <a href="/2022/05/30/the-lazy-monad">The Lazy monad</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An Either monad https://blog.ploeh.dk/2022/05/09/an-either-monad 2022-05-09T06:30:00+00:00 Mark Seemann <div id="post"> <p> <em>The Either sum type forms a monad. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. </p> <p> The Either <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> is a useful data type that <a href="/2019/01/14/an-either-functor">forms a functor</a>. Actually, <a href="/2019/01/07/either-bifunctor">it forms two functors</a>. Like many other useful functors, it also forms a monad. Again, more than one monad is possible. In this article I'll focus on the 'usual' monad instance where mapping takes place over the right side. If you swap left and right, however, you can also define a monad that maps over the left side. Try it; it's a good exercise. </p> <p> <em>Either</em> is also known as <em>Result</em>, in which case <em>right</em> is usually called <em>success</em> or <em>OK</em>, while <em>left</em> is called <em>error</em> or <em>failure</em>. </p> <p> In this article, I'll base the C# implementation on the code from <a href="/2018/06/11/church-encoded-either">Church-encoded Either</a>. </p> <h3 id="d098a362e231405fb5a3b65cbc59e5f4"> Join <a href="#d098a362e231405fb5a3b65cbc59e5f4" title="permalink">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function. As outlined in <a href="/2022/03/28/monads">the introduction to monads</a>, if you have one, you can use it to implement the other. For variety's sake, in this article series I do both. I also vary the names when there's no clear <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> naming convention. For this version of Either, I've chosen to start with a method called <code>Join</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEither&lt;L,&nbsp;R&gt;&nbsp;<span style="color:#74531f;">Join</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:blue;">this</span>&nbsp;IEither&lt;L,&nbsp;IEither&lt;L,&nbsp;R&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onLeft:&nbsp;&nbsp;<span style="color:#1f377f;">l</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Left&lt;L,&nbsp;R&gt;(l), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onRight:&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r); }</pre> </p> <p> This method flattens a nested Either value. If the outer Either is a <em>left</em> case, the method returns a <code>new Left&lt;L, R&gt;(l)</code> value, which fits the required return type. If the outer Either is a <em>right</em> case, it contains another, nested Either value that it can return. </p> <p> As already described in the article <a href="/2018/06/11/church-encoded-either">Church-encoded Either</a>, you can use Either values instead of exceptions. While that article already gives an example, the Either in that value isn't nested. This, however, can easily happen when you try to compose calculations that may fail. Another example may involve parsing. Here are two simple parsers: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#74531f;">TryParseDate</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">candidate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(DateTime.TryParse(candidate,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">d</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Right&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;(d); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Left&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;(candidate); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#74531f;">TryParseDuration</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">candidate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(TimeSpan.TryParse(candidate,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">ts</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Right&lt;<span style="color:blue;">string</span>,&nbsp;TimeSpan&gt;(ts); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Left&lt;<span style="color:blue;">string</span>,&nbsp;TimeSpan&gt;(candidate); }</pre> </p> <p> These functions try to <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">parse candidate values to more structured data types</a>. If parsing fails, they return the <a href="/2020/11/30/name-by-role">candidate value</a> so that calling code may get a chance to inspect and report the string that caused a failure. What happens if you try to compose these two functions? </p> <p> <pre>IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#1f377f;">dt</span>&nbsp;=&nbsp;TryParseDate(<span style="color:#a31515;">&quot;2022-03-21&quot;</span>); IEither&lt;<span style="color:blue;">string</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">ts</span>&nbsp;=&nbsp;TryParseDuration(<span style="color:#a31515;">&quot;2&quot;</span>); IEither&lt;<span style="color:blue;">string</span>,&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&gt;&nbsp;<span style="color:#1f377f;">nested</span>&nbsp;=&nbsp;dt.Select(<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;ts.Select(<span style="color:#1f377f;">dur</span>&nbsp;=&gt;&nbsp;d&nbsp;+&nbsp;dur));</pre> </p> <p> Composing the results of these two functions produces a nested result that you can flatten with <code>Join</code>: </p> <p> <pre>IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#1f377f;">flattened</span>&nbsp;=&nbsp;nested.Join();</pre> </p> <p> Using <code>Join</code> for that purpose is, however, often awkward, so a better option is to flatten as you go. </p> <h3 id="2ae2ab4c8dea429cae52de32dd5c3a2d"> SelectMany <a href="#2ae2ab4c8dea429cae52de32dd5c3a2d" title="permalink">#</a> </h3> <p> Monadic bind (in C# called <code>SelectMany</code>) enables you to flatten as you go. As also shown in <a href="/2022/03/28/monads">the monad introduction</a>, if you already have <code>Join</code> you can always implement <code>SelectMany</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEither&lt;L,&nbsp;R1&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IEither&lt;L,&nbsp;R&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;R,&nbsp;IEither&lt;L,&nbsp;R1&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.Select(selector).Join(); }</pre> </p> <p> The <code>SelectMany</code> method makes the above composition a little easier: </p> <p> <pre>IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#1f377f;">result</span>&nbsp;=&nbsp;dt.SelectMany(<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;ts.Select(<span style="color:#1f377f;">dur</span>&nbsp;=&gt;&nbsp;d&nbsp;+&nbsp;dur));</pre> </p> <p> Instead of having to flatten a nested result, you can flatten as you go. I think that <a href="https://www.scala-lang.org/">Scala</a>'s <code>flatMap</code> is a better name for that operation than <code>SelectMany</code>, but the behaviour is the same. If you're wondering why it's called <code>SelectMany</code>, see my attempt at an explanation in the article about the <a href="/2022/04/19/the-list-monad">list monad</a>. The name makes modest sense in that context, but in the context of Either, that meaning is lost. </p> <h3 id="cfe7b3b6f8104ec5b766407eacaaa458"> Query syntax <a href="#cfe7b3b6f8104ec5b766407eacaaa458" title="permalink">#</a> </h3> <p> Perhaps you still find the above composition awkward. After all, you have to nest a <code>Select</code> inside a <code>SelectMany</code>. Is there a simpler way? </p> <p> One option is to use query syntax, but in order to do that, you must (again as outlined in the <a href="/2022/03/28/monads">monad introduction</a>) add a special boilerplate <code>SelectMany</code> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEither&lt;L,&nbsp;R1&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>,&nbsp;<span style="color:#2b91af;">U</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IEither&lt;L,&nbsp;R&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;R,&nbsp;IEither&lt;L,&nbsp;U&gt;&gt;&nbsp;<span style="color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;R,&nbsp;U,&nbsp;R1&gt;&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> This enables you to rewrite the above composition like this: </p> <p> <pre>IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#1f377f;">result</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;d&nbsp;<span style="color:blue;">in</span>&nbsp;dt &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;dur&nbsp;<span style="color:blue;">in</span>&nbsp;ts &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;d&nbsp;+&nbsp;dur;</pre> </p> <p> It's often a question of <a href="/2020/10/12/subjectivity">subjectivity</a> whether you like one alternative over the other, but the behaviour is the same. </p> <h3 id="064ab5098d3944b1be8bc24247592fb0"> Return <a href="#064ab5098d3944b1be8bc24247592fb0" title="permalink">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that Haskell uses). You don't, however, have to define a static method called <code>Return</code>. What's of importance is that the capability exists. For <code>Either&lt;L, R&gt;</code> in C# the idiomatic way would be to use a constructor. This constructor does double duty as monadic <em>return</em>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Right</span>(R&nbsp;<span style="color:#1f377f;">right</span>)</pre> </p> <p> In other words, <em>return</em> wraps a value in a <em>right</em> case. </p> <p> Why not the <em>left</em> case? Because it wouldn't type-check. A <em>return</em> function should take a value of the type <code>R</code> and return an <code>IEither&lt;L, R&gt;</code>. The <code>Left</code> constructor, however, doesn't take an <code>R</code> value - it takes an <code>L</code> value. </p> <h3 id="f0fb650204c043229df4967c22c4d6ec"> Left identity <a href="#f0fb650204c043229df4967c22c4d6ec" title="permalink">#</a> </h3> <p> We need to identify the <em>return</em> function in order to examine <a href="/2022/04/11/monad-laws">the monad laws</a>. Let's see what they look like for the Either monad, starting with the left identity law. </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;2&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;2.3:00&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;4.5:30&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;0:33:44&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LeftIdentityLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Right&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(s); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;TimeSpan&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;TryParseDuration; &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(@return(a).SelectMany(h),&nbsp;h(a)); }</pre> </p> <p> As always, even a parametrised test constitutes no <em>proof</em> that the law holds. I show the tests to illustrate what the laws look like in 'real' code. </p> <h3 id="d158a758e9dc40149c7676a83398d17d"> Right identity <a href="#d158a758e9dc40149c7676a83398d17d" title="permalink">#</a> </h3> <p> In a similar manner, we can showcase the right identity law as a test. </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;2022-03-22&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;2022-03-21T16:57&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">RightIdentityLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;TryParseDate; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;DateTime,&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Right&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;(d); &nbsp;&nbsp;&nbsp;&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(@return),&nbsp;m); }</pre> </p> <p> The parameters supplied as <code>a</code> will cause <code>m</code> to be both <code>Left</code> and <code>Right</code> values, but the test demonstrates that the law holds in both cases. </p> <h3 id="ff7794ca964b4dfdb588251f264c7c11"> Associativity <a href="#ff7794ca964b4dfdb588251f264c7c11" title="permalink">#</a> </h3> <p> The last monad law is the associativity law that describes how (at least) three functions compose. We're going to need three functions. I'm going to reuse <code>TryParseDuration</code> and add two new Either-valued functions: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#74531f;">DaysForward</span>(TimeSpan&nbsp;<span style="color:#1f377f;">ts</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(ts&nbsp;&lt;&nbsp;TimeSpan.Zero) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Left&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">double</span>&gt;(<span style="color:#a31515;">$&quot;Negative&nbsp;durations&nbsp;not&nbsp;allowed:&nbsp;</span>{ts}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Right&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">double</span>&gt;(ts.TotalDays); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">Nat</span>(<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">d</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(d&nbsp;%&nbsp;1&nbsp;!=&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Left&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">$&quot;Non-integers&nbsp;not&nbsp;allowed:&nbsp;</span>{d}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(d&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Left&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">$&quot;Non-positive&nbsp;numbers&nbsp;not&nbsp;allowed:&nbsp;</span>{d}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Right&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;((<span style="color:blue;">int</span>)d); }</pre> </p> <p> These silly functions are only here for purposes of demonstration. Don't try to infer any deep meaning from them. </p> <p> Designating them <code>f</code>, <code>g</code>, and <code>h</code>, we can use them to express the monad associativity law: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;2&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;-2.3:00&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;4.5:30&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;0:33:44&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;0&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">AssociativityLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;TimeSpan&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;TryParseDuration; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;TimeSpan,&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">double</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;DaysForward; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;IEither&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;Nat; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(g).SelectMany(h),&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h))); }</pre> </p> <p> The test cases supplied via the <code>[InlineData]</code> attributes comprise a set of values that make their way through the composition to varying degrees. <code>"2"</code> makes it all the way through to a <code>Right</code> value, because it encodes a two-day duration. It's both a positive value, greater than zero, and an integer. The other test cases become <code>Left</code> values at various stages in the composition, being either a non-number, fractional, zero, or negative. </p> <h3 id="b409ce7af135433eb275832d2c45ffde"> Conclusion <a href="#b409ce7af135433eb275832d2c45ffde" title="permalink">#</a> </h3> <p> Either (AKA Result) is another useful data structure that forms a monad. You can use it instead of throwing exceptions. Its monadic nature enables you to compose multiple Either-valued functions in a sane and elegant way. </p> <p> <strong>Next:</strong> <a href="/2022/05/16/the-identity-monad">The Identity monad</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. At the boundaries, applications aren't functional https://blog.ploeh.dk/2022/05/02/at-the-boundaries-applications-arent-functional 2022-05-02T05:29:00+00:00 Mark Seemann <div id="post"> <p> <em>Ten years later.</em> </p> <p> In 2011 I published an article titled <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">At the Boundaries, Applications are Not Object-Oriented</a>. It made the observation that at the edges of systems, where software interacts with either humans or other software, interactions aren't object-oriented. The data exchanged is exactly that: data. </p> <p> A common catchphrase about object-oriented design is that <em>objects are data with behaviour</em>. In a user interface, a REST API, at the command line, etcetera, data is transmitted, but behaviour is not. </p> <p> The article was one of those unfortunately unsatisfying articles that point out a problem without offering a solution. While I don't like to leave readers hanging like that, I on the other hand think that it's important sometimes to ask open-ended questions. <em>Here's a problem. Can anyone offer a solution?</em> </p> <p> People occasionally come by that old article and ask me if I've changed my mind since then. The short answer is that I believe that the problem persists. I may, however, today have another approach to it. </p> <h3 id="a438ebbc5a964854906bf0ac0991e0ba"> Functional programming <a href="#a438ebbc5a964854906bf0ac0991e0ba" title="permalink">#</a> </h3> <p> Starting around the time of the original article, I became interested in functional programming (FP). The more I've learned about it, the better it seems to address my concerns of eleven years ago. </p> <p> I don't know if FP is a silver bullet (although <a href="/2019/07/01/yes-silver-bullet">I'd like to think so</a>), but at least I find that I have fewer impedance mismatch issues than I did with object-oriented programming (OOP). In FP, after all, data is just data. </p> <p> Except when it isn't, but more about that later. </p> <p> It's easier to reconcile the FP notion of data with what actually goes on at the boundaries of a system. </p> <p> Perhaps I should clarify what I mean by the word <em>boundary</em>. This is where a software system interacts with the rest of the world. This is where it receives HTTP requests and answers with HTTP responses. This is where it queries its database. This is where it sends emails. This is where it reads files from disk. This is where it repaints user interfaces. In short, the outermost circle in <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">ports and adapters architecture</a>. </p> <p> Notice that what crosses the boundary tends to be data. JSON or XML documents, tabular data streams, flat files, etcetera. In OOP, your training tells you that you should associate some <em>behaviour</em> with data, and that's what creates dissonance. We introduce <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects</a> (DTO) even though a DTO </p> <blockquote> <p> "is one of those objects our mothers told us never to write." </p> <footer><cite>Marin Fowler, <a href="/ref/peaa">Patterns of Enterprise Application Architecture</a></cite></footer> </blockquote> <p> We almost have to do violence to ourselves to introduce a DTO in an object-oriented code base, yet grudgingly we accept that this is probably better than the alternative. </p> <p> In FP, on the other hand, that's just natural. </p> <p> To paraphrase the simple example from the old article, imagine that you receive data like this as input: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;firstName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Mark&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;lastName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Seemann&quot;</span> }</pre> </p> <p> If we wanted to capture that structure as a type, in <a href="https://fsharp.org">F#</a> you'd write a type like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Name&nbsp;=&nbsp;{&nbsp;FirstName&nbsp;:&nbsp;string;&nbsp;LastName&nbsp;:&nbsp;string&nbsp;}</pre> </p> <p> And while you might feel icky defining such a DTO-like type in OOP, it's perfectly natural and expected in FP. </p> <p> That type even supports JSON serialization and deserialization without further <a href="/2019/12/16/zone-of-ceremony">ceremony</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;opts&nbsp;=&nbsp;JsonSerializerOptions&nbsp;(PropertyNamingPolicy&nbsp;=&nbsp;JsonNamingPolicy.CamelCase) <span style="color:blue;">let</span>&nbsp;n&nbsp;=&nbsp;JsonSerializer.Deserialize&lt;Name&gt;&nbsp;(json,&nbsp;opts)</pre> </p> <p> where <code>json</code> might contain the above JSON fragment. </p> <p> To summarise so far: <em>Data crosses the boundaries of systems - not objects</em>. Since FP tends to separate data from behaviour, that paradigm feels more natural in that context. </p> <h3 id="3f60d44d38f24e8080eb4d8123ed5530"> Not all peaches and cream <a href="#3f60d44d38f24e8080eb4d8123ed5530" title="permalink">#</a> </h3> <p> FP is, however, not a perfect fit either. Let's paraphrase the above medial summary: <em>Data crosses the boundaries of systems - not functions</em>. </p> <p> FP is more than just functions <em>and</em> data. It's also about functions as first-class concepts, including functions as values, and <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order functions</a>. While an object is <em>data with behaviour</em>, <a href="/2018/02/12/object-isomorphisms">closures <em>are behaviour with data</em></a>. Such first-class values cross system boundaries with the same ease as objects do: Not very well. </p> <p> I was recently helping a client defining a domain-specific language (DSL). I wrote a proof of concept in <a href="https://www.haskell.org">Haskell</a> based on Abstract Syntax Trees (AST). Each AST would typically be defined with copious use of lambda expressions. There was also an existential type involved. </p> <p> At one time, the question of persistence came up. At first I thought that you could easily serialize those ASTs, since, after all, they're just immutable values. Then I realised that while that's true, most of the constituent values were <em>functions</em>. There's no clear way to serialize a function, other than in code. </p> <p> Functions don't easily cross boundaries, for the same reasons that objects don't. </p> <p> So while FP seems like a better fit when it comes to modelling data, <em>at the boundaries, applications aren't functional</em>. </p> <h3 id="7ab8590765114bcf91c0e505e6b34271"> Weak or strong typing? <a href="#7ab8590765114bcf91c0e505e6b34271" title="permalink">#</a> </h3> <p> In my original article, I attempted to look ahead. One option I considered was dynamic typing. Instead of defining a DTO that mirrors a particular shape of data (e.g. a JSON document), would it be easier to parse or create a document on the fly, without a static type? </p> <p> I occasionally find myself doing that, as you can see in my article <a href="/2022/01/03/to-id-or-not-to-id">To ID or not to ID</a>. The code base already used <a href="https://www.newtonsoft.com/json">Json.NET</a>, and I found it easier to use the <code>JToken</code> API to parse documents, rather than having to maintain a static type whose only raison d'être was to mirror a JSON document. I repeat an example here for your convenience: </p> <p> <pre><span style="color:green;">//&nbsp;JToken&nbsp;-&gt;&nbsp;UserData&lt;string&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;parseUser&nbsp;(jobj&nbsp;:&nbsp;JToken)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uid&nbsp;=&nbsp;jobj.[<span style="color:#a31515;">&quot;id&quot;</span>]&nbsp;|&gt;&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fn&nbsp;=&nbsp;jobj.[<span style="color:#a31515;">&quot;firstName&quot;</span>]&nbsp;|&gt;&nbsp;Option.ofObj&nbsp;|&gt;&nbsp;Option.map&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ln&nbsp;=&nbsp;jobj.[<span style="color:#a31515;">&quot;lastName&quot;</span>]&nbsp;|&gt;&nbsp;Option.ofObj&nbsp;|&gt;&nbsp;Option.map&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;email&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jobj.[<span style="color:#a31515;">&quot;primaryEmail&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.ofObj &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.map&nbsp;(string&nbsp;&gt;&gt;&nbsp;MailAddress) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;uid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FirstName&nbsp;=&nbsp;fn &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastName&nbsp;=&nbsp;ln &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Identities&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Permissions&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Likewise, I also used <code>JObject</code> to create and serialize new JSON documents. The <em>To ID or not to ID</em> article also contains an example of that. </p> <p> F# types are typically one-liners, like that above <code>Name</code> example. If that's true, why do I shy away from defining a static type that mirrors the desired JSON document structure? </p> <p> Because <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">typing (the other kind of typing) isn't a programming bottleneck</a>. The cost of another type isn't the time it takes to write the code. It's the maintenance burden it adds. </p> <p> One problem you'll commonly have when you define DTOs as static types is that the code base will contain more than one type that represents the same concept. If these types are too similar, it becomes difficult for programmers to distinguish between them. As a result, functionality may inadvertently be coupled to the wrong type. </p> <p> Still, there can be good reasons to introduce a DTO type and a domain model. As <a href="https://lexi-lambda.github.io/">Alexis King</a> writes, </p> <blockquote> <p> "Consider: what is a parser? Really, a parser is just a function that consumes less-structured input and produces more-structured output." </p> <footer><cite>Alexis King, <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">Parse, don’t validate</a></cite></footer> </blockquote> <p> This is the approach I took in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. A <code>ReservationDto</code> class has almost no invariants. It's just a bag of mutable and nullable properties; it's less-structured data. </p> <p> The corresponding domain model <code>Reservation</code> is more-structured. It's an immutable <a href="https://martinfowler.com/bliki/ValueObject.html">Value Object</a> that guarantees various invariants: All required data is present, quantity is a natural number, you can't mutate the object, and it has structural equality. </p> <p> In that case, <code>ReservationDto</code> and <code>Reservation</code> are sufficiently different. I consider their separate roles in the code base clear and distinct. </p> <h3 id="f416242d15a24f41a925182f180b4c2e"> Conclusion <a href="#f416242d15a24f41a925182f180b4c2e" title="permalink">#</a> </h3> <p> In 2011 I observed that at the boundaries, applications aren't object-oriented. For years, I've thought it a natural corollary that likewise, <em>at the boundaries, applications aren't functional</em>, either. On the other hand, I don't think I've explicitly made that statement before. </p> <p> A decade after 2011, I still find object-oriented programming incongruent with how software must work in reality. Functional programming offers an alternative that, while also not perfectly aligned with all data, seems better aligned with the needs of working software. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Maybe monad https://blog.ploeh.dk/2022/04/25/the-maybe-monad 2022-04-25T06:50:00+00:00 Mark Seemann <div id="post"> <p> <em>The Maybe sum type forms a monad. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. </p> <p> The Maybe <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> is a useful data type that <a href="/2018/03/26/the-maybe-functor">forms a functor</a>. Like many other useful functors, it also forms a monad. </p> <p> It can be useful when querying another data structure (like a list or a tree) for a value that may or may not be present. It's also useful when performing a calculation that may not be possible, such as taking the square root of of a number, or calculating the average of a collection of values. A third common use case is when parsing a value, although usually, the Either sum type is often a better option for that task. </p> <h3 id="20497728035845fd9e7be0bb1ffae94c"> Bind <a href="#20497728035845fd9e7be0bb1ffae94c" title="permalink">#</a> </h3> <p> A monad must define either a <em>bind</em> or <em>join</em> function. In C#, monadic bind is called <code>SelectMany</code>. Depending on how you've implemented <code>Maybe&lt;T&gt;</code> the details of the implementation may vary, but the observable behaviour must always be the same. In this article I'm going to continue to use the <code>Maybe&lt;T&gt;</code> class first shown in <a href="/2018/03/26/the-maybe-functor">the article about the Maybe functor</a>. (That's not really my favourite implementation, but it doesn't matter.) </p> <p> In that code base, I chose to implement <code>SelectMany</code> as an extension method. Why I didn't use an instance method I no longer remember. Again, this doesn't matter. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Maybe&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Maybe&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Maybe&lt;TResult&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(source.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;selector(source.Item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;TResult&gt;(); }</pre> </p> <p> If the <code>source</code> has an item, <code>SelectMany</code> calls <code>selector</code> with the value and returns the result. If not, the result is an empty <code>Maybe&lt;TResult&gt;</code>. </p> <p> Monadic bind becomes useful when you have more than one function that returns a monadic value. Consider, for example, this variation of <code>Average</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#74531f;">Average</span>(IEnumerable&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">values</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(values.Any()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;(values.Average()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;(); }</pre> </p> <p> You can't calculate the average of an empty collection, so if <code>values</code> is empty, this function returns an empty <code>Maybe&lt;double&gt;</code>. </p> <p> If you only needed this <code>Average</code> function, then you could use Maybe's <a href="/2018/03/22/functors">functor</a> affordance to map the result. On the other hand, imagine that you'd like to pass the <code>Average</code> result to this <code>Sqrt</code> function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#74531f;">Sqrt</span>(<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">d</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">result</span>&nbsp;=&nbsp;Math.Sqrt(d); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">switch</span>&nbsp;(result) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">case</span>&nbsp;<span style="color:blue;">double</span>.NaN: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">case</span>&nbsp;<span style="color:blue;">double</span>.PositiveInfinity:&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">default</span>:&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;(result); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is another calculation that may not be possible. If you try to compose <code>Average</code> and <code>Sqrt</code> with <code>Select</code> (the functor's <em>map</em> function), you'd get a <code>Maybe&lt;Maybe&lt;double&gt;&gt;</code>. Fortunately, however, monads are functors you can flatten: </p> <p> <pre>[Theory] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[0],&nbsp;-100)] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[]&nbsp;{&nbsp;5,&nbsp;3&nbsp;},&nbsp;2)] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[]&nbsp;{&nbsp;1,&nbsp;-2&nbsp;},&nbsp;-100)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ComposeAverageAndSqrt</span>(<span style="color:blue;">double</span>[]&nbsp;<span style="color:#1f377f;">values</span>,&nbsp;<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;Average(values).SelectMany(Sqrt); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual.GetValueOrFallback(-100)); }</pre> </p> <p> The <code>SelectMany</code> method flattens as it goes. Viewed in this way, <a href="https://www.scala-lang.org/">Scala</a>'s <code>flatMap</code> seems like a more appropriate name. </p> <p> The above test demonstrates how <code>SelectMany</code> returns a flattened <code>Maybe&lt;double&gt;</code>. The first test case has zero elements, so the <code>Average</code> method is going to return an empty <code>Maybe&lt;double&gt;</code>. <code>SelectMany</code> handles that gracefully by returning another empty value. </p> <p> In the second test case <code>Average</code> returns a populated value that contains the number <code>4</code>. <code>SelectMany</code> passes <code>4</code> to <code>Sqrt</code>, which returns <code>2</code> wrapped in a <code>Maybe&lt;double&gt;</code>. </p> <p> In the third test case, the average is <code>-.5</code>, wrapped in a <code>Maybe&lt;double&gt;</code>. <code>SelectMany</code> passes <code>-.5</code> on to <code>Sqrt</code>, which returns an empty value. </p> <h3 id="c9bb381e6f834ba2b955e3ea13efba7d"> Query syntax <a href="#c9bb381e6f834ba2b955e3ea13efba7d" title="permalink">#</a> </h3> <p> Monads also enable query syntax in C# (just like they enable other kinds of syntactic sugar in languages like <a href="https://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>). As outlined in the <a href="/2022/03/28/monads">monad introduction</a>, however, you must add a special <code>SelectMany</code> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Maybe&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Maybe&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Maybe&lt;U&gt;&gt;&nbsp;<span style="color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;U,&nbsp;TResult&gt;&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> As I also mentioned in the monad introduction, it seems to me that you can always implement this overload with the above expression. Once this overload is in place, you can rewrite the above composition in query syntax, if that's more to your liking: </p> <p> <pre>[Theory] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[0],&nbsp;-100)] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[]&nbsp;{&nbsp;5,&nbsp;3&nbsp;},&nbsp;2)] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[]&nbsp;{&nbsp;1,&nbsp;-2&nbsp;},&nbsp;-100)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ComposeAverageAndSqrtWithQuerySyntax</span>(<span style="color:blue;">double</span>[]&nbsp;<span style="color:#1f377f;">values</span>,&nbsp;<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;a&nbsp;<span style="color:blue;">in</span>&nbsp;Average(values) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;Sqrt(a) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual.GetValueOrFallback(-100)); }</pre> </p> <p> In this case, query syntax is more verbose, but the behaviour is the same. It's just two different ways of writing the same expression. The C# compiler desugars the query-syntax expression to one that composes with <code>SelectMany</code>. </p> <h3 id="09cb0abf4e7d4c21bce8796a57a5df50"> Join <a href="#09cb0abf4e7d4c21bce8796a57a5df50" title="permalink">#</a> </h3> <p> In <a href="/2022/03/28/monads">the introduction</a> you learned that if you have a <code>Flatten</code> or <code>Join</code> function, you can implement <code>SelectMany</code>, and the other way around. Since we've already defined <code>SelectMany</code> for <code>Maybe&lt;T&gt;</code>, we can use that to implement <code>Join</code>. In this article I use the name <code>Join</code> rather than <code>Flatten</code>. This is an arbitrary choice that doesn't impact behaviour. Perhaps you find it confusing that I'm inconsistent, but I do it in order to demonstrate that the behaviour is the same even if the name is different. </p> <p> The concept of a monad is universal, but the names used to describe its components differ from language to language. What C# calls <code>SelectMany</code>, Scala calls <code>flatMap</code>, and what Haskell calls <code>join</code>, other languages may call <code>Flatten</code>. </p> <p> You can always implement <code>Join</code> by using <code>SelectMany</code> with the identity function. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Maybe&lt;T&gt;&nbsp;<span style="color:#74531f;">Join</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Maybe&lt;Maybe&lt;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> Instead of flattening as you go with <code>SelectMany</code>, you could also use <code>Select</code> and <code>Join</code> to compose <code>Average</code> and <code>Sqrt</code>: </p> <p> <pre>[Theory] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[0],&nbsp;-100)] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[]&nbsp;{&nbsp;5,&nbsp;3&nbsp;},&nbsp;2)] [InlineData(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">double</span>[]&nbsp;{&nbsp;1,&nbsp;-2&nbsp;},&nbsp;-100)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">JoinAverageAndSqrt</span>(<span style="color:blue;">double</span>[]&nbsp;<span style="color:#1f377f;">values</span>,&nbsp;<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;Average(values).Select(Sqrt).Join(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual.GetValueOrFallback(-100)); }</pre> </p> <p> You'd typically not write the composition like this, as it seems more convenient to use <code>SelectMany</code>, but you could. The behaviour is the same. </p> <h3 id="770215f4b3da49dd976dc67e2416481e"> Return <a href="#770215f4b3da49dd976dc67e2416481e" title="permalink">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. Conceptually, I call this function <em>return</em> (because that's the name that Haskell uses). You don't, however, have to define a static method called <code>Return</code>. What's of importance is that the capability exists. For <code>Maybe&lt;T&gt;</code> in C# the <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> way would be to use a constructor. This constructor overload does double duty as monadic <em>return</em>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>(T&nbsp;<span style="color:#1f377f;">item</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(item)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.HasItem&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Item&nbsp;=&nbsp;item; }</pre> </p> <p> Why is it that overload, and not the other one? Because if you try to use <code>new Maybe&lt;T&gt;()</code> as <em>return</em>, you'll find that it breaks the <a href="/2022/04/11/monad-laws">monad laws</a>. Try it. It's a good exercise. </p> <h3 id="393bd508a0b549ca908c8950589bd910"> Left identity <a href="#393bd508a0b549ca908c8950589bd910" title="permalink">#</a> </h3> <p> Now that we're on the topic of the monad laws, let's see what they look like for the Maybe monad, starting with the left identity law. </p> <p> <pre>[Theory] [InlineData(0)] [InlineData(1)] [InlineData(2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LeftIdentity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;i&nbsp;!=&nbsp;0&nbsp;?&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;(1.0&nbsp;/&nbsp;i)&nbsp;:&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(@return(a).SelectMany(h),&nbsp;h(a)); }</pre> </p> <p> To provide some variety, <code>h</code> is another Maybe-valued function - this time one that performs a safe <a href="https://en.wikipedia.org/wiki/Multiplicative_inverse">multiplicative inverse</a>. If <code>i</code> is zero, it returns an empty <code>Maybe&lt;double&gt;</code> because you can't devide by zero; otherwise, it returns one divided by <code>i</code> (wrapped in a <code>Maybe&lt;double&gt;</code>). </p> <h3 id="fcd0a8fe00784ec1a606d867fbc52384"> Right identity <a href="#fcd0a8fe00784ec1a606d867fbc52384" title="permalink">#</a> </h3> <p> In a similar manner, we can showcase the right identity law as a test. </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;42&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;1337&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">RightIdentity</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">f</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(<span style="color:blue;">int</span>.TryParse(s,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">i</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(@return),&nbsp;m); }</pre> </p> <p> Again, for variety's sake, for <code>f</code> I've chosen a function that should probably be named <code>TryParseInt</code> if used in another context. </p> <h3 id="52f8e7ce874942d2999e647c3a354869"> Associativity <a href="#52f8e7ce874942d2999e647c3a354869" title="permalink">#</a> </h3> <p> The last monad law is the associativity law that we can demonstrate like this: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;-1&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;0&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;4&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Associativity</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">f</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(<span style="color:blue;">int</span>.TryParse(s,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">i</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">int</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Sqrt(i); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;d&nbsp;==&nbsp;0&nbsp;?&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;()&nbsp;:&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;<span style="color:blue;">double</span>&gt;(1&nbsp;/&nbsp;d); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(g).SelectMany(h),&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h))); }</pre> </p> <p> Here, I've copy-and-pasted the <em>TryParseInt</em> function I also used in the right identity example, and combined it with the <code>Sqrt</code> function and a safe multiplicative inverse. The law, however, should hold for all <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> that type-check. The functions shown here are just examples. </p> <p> As usual, a parametrised test is no proof that the law holds. I provide the tests only as examples of what the laws looks like. </p> <h3 id="7a49fae4e6fc4ba3850c323cf22d62f6"> Conclusion <a href="#7a49fae4e6fc4ba3850c323cf22d62f6" title="permalink">#</a> </h3> <p> The Maybe sum type is a versatile and useful data structure that forms a monad. This enables you to compose disparate functions that each may err by failing to return a value. </p> <p> <strong>Next: </strong> <a href="/2022/05/09/an-either-monad">An Either monad</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The List monad https://blog.ploeh.dk/2022/04/19/the-list-monad 2022-04-19T05:45:00+00:00 Mark Seemann <div id="post"> <p> <em>Lists, collections, deterministic Iterators form a monad. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. </p> <p> In the article series about <a href="/2018/03/22/functors">functors</a> I never included an explicit article about collections. Instead, I wrote: </p> <blockquote> <p> "Perhaps the most well-known of all functors is List, a.k.a. Sequence. C# query syntax can handle any functor, but most people only think of it as a language feature related to <code>IEnumerable&lt;T&gt;</code>. Since the combination of <code>IEnumerable&lt;T&gt;</code> and query syntax is already well-described, I'm not going to cover it explicitly here." </p> <footer><cite><a href="/2018/03/22/functors">Functors</a>, Mark Seemann, 2018</cite></footer> </blockquote> <p> Like many other useful functors, a list also forms a monad. In this article, I call it <em>list</em>. In <a href="https://www.haskell.org">Haskell</a> the most readily available equivalent is the built-in linked list (<code>[]</code>). Likewise, in <a href="https://fsharp.org">F#</a>, the fundamental collection types are linked lists and arrays. In C#, any <code>IEnumerable&lt;T&gt;</code> forms a monad. The .NET <code>IEnumerable&lt;T&gt;</code> interface is an application of the <a href="https://en.wikipedia.org/wiki/Iterator_pattern">Iterator design pattern</a>. There are many possible names for this monad: <em>list</em>, <em>sequence</em>, <em>collection</em>, <em>iterator</em>, etcetera. I've arbitrarily chosen to mostly use <em>list</em>. </p> <h3 id="13be436e595c411baf581dcfb31ab86f"> SelectMany <a href="#13be436e595c411baf581dcfb31ab86f" title="permalink">#</a> </h3> <p> In the <a href="/2022/03/28/monads">introduction to monads</a> you learned that monads are characterised by having either a <em>join</em> (or <em>flatten</em>) function, or a <em>bind</em> function. While in Haskell, monadic bind is the <code>&gt;&gt;=</code> operator, in C# it's a method called <code>SelectMany</code>. Perhaps you wonder about the name. </p> <p> The name <code>SelectMany</code> makes most sense in the context of lists. An example would be useful around here. </p> <p> Imagine that you have to parse a <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> file. You've already read the file from disc and split the contents into lines. Now you need to split each line on commas. </p> <p> <pre>&gt; <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">lines</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;foo,bar,baz&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;qux,quux,quuz&quot;</span>&nbsp;}; &gt; lines.Select(l =&gt; l.Split(',')).ToArray() string[2][] { string[3] { "foo", "bar", "baz" }, string[3] { "qux", "quux", "quuz" } }</pre> </p> <p> When you use <code>Select</code> the result is a nested list. You've already learned that a monad is a functor you can flatten. In C#, you can 'flatten as you go' with <code>SelectMany</code>. </p> <p> The above scenario offers a hint at why the method is named <code>SelectMany</code>. You use it instead of <code>Select</code> when the selector returns <em>many</em> values. In the above example, the <code>Split</code> method returns many values. </p> <p> So, instead of <code>Select</code>, use <code>SelectMany</code> if you need a flattened list: </p> <p> <pre>&gt; lines.SelectMany(l =&gt; l.Split(',')).ToArray() string[6] { "foo", "bar", "baz", "qux", "quux", "quuz" }</pre> </p> <p> I've never had the opportunity to ask any of the LINQ designers about that naming choice, but this explanation makes sense to me. Even if it turns out that they had something else in mind when they named the method, I think that my explanation at least serves as a mnemonic device. </p> <p> The name is still unfortunate, since it mostly makes sense for the list monad. Already when you use <code>SelectMany</code> with the Maybe monad, the name makes less sense. </p> <h3 id="4f2a54b64a744c5f855c2baa9f1c8d9a"> Flatten <a href="#4f2a54b64a744c5f855c2baa9f1c8d9a" title="permalink">#</a> </h3> <p> In <a href="/2022/03/28/monads">the introduction</a> you learned that if you have a <code>Flatten</code> or <code>Join</code> function, you can implement <code>SelectMany</code>, and the other way around. In C#, LINQ already comes with <code>SelectMany</code>, but surprisingly, <code>Flatten</code> isn't built in. </p> <p> You can always implement <code>Flatten</code> by using <code>SelectMany</code> with the identity function. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;<span style="color:#74531f;">Flatten</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;IEnumerable&lt;IEnumerable&lt;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> Recall that the <code>selector</code> passed to <code>SelectMany</code> must have the type <code>Func&lt;TSource,&nbsp;IEnumerable&lt;TResult&gt;&gt;</code>. There's no rule, however, that says that <code>TSource</code> can't be <code>IEnumerable&lt;TResult&gt;</code>. If <code>x</code> in <code>x =&gt; x</code> has the type <code>IEnumerable&lt;TResult&gt;</code>, then <code>x =&gt; x</code> has the type <code>Func&lt;IEnumerable&lt;TResult&gt;,&nbsp;IEnumerable&lt;TResult&gt;&gt;</code> and it all type-checks. </p> <p> Here's a test that demonstrates how <code>Flatten</code> works: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FlattenExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>[][]&nbsp;<span style="color:#1f377f;">nested</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>&nbsp;},&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>&nbsp;}&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">flattened</span>&nbsp;=&nbsp;nested.Flatten(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>&nbsp;},&nbsp;flattened); }</pre> </p> <p> In Haskell <code>Flatten</code> is called <code>join</code> and is implemented this way: </p> <p> <pre>join :: (Monad m) =&gt; m (m a) -&gt; m a join x = x &gt;&gt;= id</pre> </p> <p> It's the same implementation as the above C# method: monadic bind (<code>SelectMany</code> in C#; <code>&gt;&gt;=</code> in Haskell) with the identity function. The Haskell implementation works for all monads. </p> <h3 id="f54cad2a32b341e1a11420fe53c0a7e1"> Return <a href="#f54cad2a32b341e1a11420fe53c0a7e1" title="permalink">#</a> </h3> <p> Apart from monadic bind, a monad must also define a way to put a normal value into the monad. For the list monad, this implies a single value promoted to a list. Surprisingly, the .NET base class library doesn't come with such a built-in function, but you can easily implement it yourself: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;<span style="color:#74531f;">Return</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;x; }</pre> </p> <p> In practice, however, you'd normally instead create a singleton array, like this: <code>new[] { 2112 }</code>. Since arrays implement <code>IEnumerable&lt;T&gt;</code>, this works just as well. </p> <p> Why is this the correct implementation of <em>return?</em> </p> <p> Wouldn't this work? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;<span style="color:#74531f;">ReturnZero</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">break</span>; }</pre> </p> <p> Or this? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;T&gt;&nbsp;<span style="color:#74531f;">ReturnTwo</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;x; }</pre> </p> <p> None of these are appropriate because they break the <a href="/2022/04/11/monad-laws">monad laws</a>. Try it, as an exercise! </p> <h3 id="0795515a923a47ad95417dc77a826c24"> Left identity <a href="#0795515a923a47ad95417dc77a826c24" title="permalink">#</a> </h3> <p> Now that we're on the topic of the monad laws, let's see what they look like for the list monad, starting with the left identity law. </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo,bar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo,bar,baz&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LeftIdentity</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;List.Return; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s.Split(<span style="color:#a31515;">&#39;,&#39;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(@return(a).SelectMany(h),&nbsp;h(a)); }</pre> </p> <p> As usual, a parametrised test is no proof that the law holds. I provide it only as an example of what the law looks like. </p> <h3 id="09b17be2537842a3994b697e723ada34"> Right identity <a href="#09b17be2537842a3994b697e723ada34" title="permalink">#</a> </h3> <p> Likewise, we can showcase the right identity law as a test: </p> <p> <pre>[Theory] [InlineData(0)] [InlineData(1)] [InlineData(9)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">RightIdentity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Enumerable.Repeat(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;List.Return; &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(@return),&nbsp;m); }</pre> </p> <p> These tests all pass. </p> <h3 id="c8898fa040ce4c138c0beadbf5a045d0"> Associativity <a href="#c8898fa040ce4c138c0beadbf5a045d0" title="permalink">#</a> </h3> <p> The last monad law is the associativity law that we can demonstrate like this: </p> <p> <pre>[Theory] [InlineData(0)] [InlineData(3)] [InlineData(8)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Associativity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Enumerable.Repeat(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;IEnumerable&lt;<span style="color:blue;">char</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">char</span>,&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">c</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;c.ToString().ToUpper(),&nbsp;c.ToString().ToLower()&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(g).SelectMany(h),&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h))); }</pre> </p> <p> The three functions <code>f</code>, <code>g</code>, and <code>h</code> are just silly functions I chose for variety's sake. </p> <h3 id="e15e1dfc86b94747ab7f7dc66faa02d2"> Conclusion <a href="#e15e1dfc86b94747ab7f7dc66faa02d2" title="permalink">#</a> </h3> <p> Lists, arrays, collections, Iterators are various names for a common idea: that of an ordered sequence of values. In this article, I've called it <em>list</em> for simplicity's sake. It's possible to flatten nested lists in a well-behaved manner, which makes <em>list</em> a monad. </p> <p> <strong>Next:</strong> <a href="/2022/04/25/the-maybe-monad">The Maybe monad</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Monad laws https://blog.ploeh.dk/2022/04/11/monad-laws 2022-04-11T06:57:00+00:00 Mark Seemann <div id="post"> <p> <em>An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. </p> <p> Just as <a href="/2018/03/22/functors">functors</a> must obey the functor laws, monads must follow the monad laws. When looked at from the right angle, these are intuitive and reasonable rules: <a href="https://en.wikipedia.org/wiki/Identity_element">identity</a> and <a href="https://en.wikipedia.org/wiki/Associative_property">associativity</a>. </p> <p> That sounds almost like a <a href="/2017/10/06/monoids">monoid</a>, but the rules are more relaxed. For monoids, we require a <a href="https://en.wikipedia.org/wiki/Binary_operation">binary operation</a>. That's not the case for functors and monads. Instead of binary operations, for monads we're going to use <a href="/2022/04/04/kleisli-composition">Kleisli composition</a>. </p> <p> This article draws heavily on the <a href="https://wiki.haskell.org/Monad_laws">Haskell wiki on monad laws</a>. </p> <p> In short, the monad laws comprise three rules: </p> <ul> <li>Left identity</li> <li>Right identity</li> <li>Associativity</li> </ul> <p> The article describes each in turn, starting with left identity. </p> <h3 id="2589cde8994b4e3db81ef821d8f346ba"> Left identity <a href="#2589cde8994b4e3db81ef821d8f346ba" title="permalink">#</a> </h3> <p> A left identity is an element that, when applied to the left of an operator, doesn't change the other element. When used with the <a href="/2022/04/04/kleisli-composition">fish operator</a> it looks like this: </p> <p> <pre>id &gt;=&gt; h ≡ h</pre> </p> <p> For monads, <code>id</code> turns out to be <code>return</code>, so more specifically: </p> <p> <pre>return &gt;=&gt; h ≡ h</pre> </p> <p> In C# we can illustrate the law as a parametrised test: </p> <p> <pre>[Theory] [InlineData(-1)] [InlineData(42)] [InlineData(87)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LeftIdentityKleisli</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Monad.Return; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Monad.Return(i.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">composed</span>&nbsp;=&nbsp;@return.Compose(h); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(composed(a),&nbsp;h(a)); }</pre> </p> <p> Recall that we can implement the fish operators as <code>Compose</code> overloads. The above test demonstrates the notion that <code>@return</code> composed with <code>h</code> should be indistinguishable from <code>h</code>. Such a test should pass. </p> <p> Keep in mind, however, that Kleisli composition is <em>derived</em> from the definition of a monad. The direct definition of monads is that they come with a <code>return</code> and <code>bind</code> functions (or, alternatively <code>return</code> and <code>join</code>). If we <a href="https://refactoring.com/catalog/inlineFunction.html">inline</a> the implementation of <code>Compose</code>, the test instead looks like this: </p> <p> <pre>[Theory] [InlineData(-1)] [InlineData(42)] [InlineData(87)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">LeftIdentityInlined</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Monad.Return; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Monad.Return(i.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">composed</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;@return(x).SelectMany(h); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(composed(a),&nbsp;h(a)); }</pre> </p> <p> or, more succinctly: </p> <p> <pre>Assert.Equal(@return(a).SelectMany(h),&nbsp;h(a));</pre> </p> <p> This is also the way the left identity law is usually presented in <a href="https://www.haskell.org">Haskell</a>: </p> <p> <pre>return a &gt;&gt;= h = h a</pre> </p> <p> In Haskell, <em>monadic bind</em> is represented by the <code>&gt;&gt;=</code> operator, while in C# it's the <code>SelectMany</code> method. Thus, the Haskell and the C# representations are equivalent. </p> <h3 id="b74cccb66945471d8b28e40e4e90516d"> Right identity <a href="#b74cccb66945471d8b28e40e4e90516d" title="permalink">#</a> </h3> <p> Right identity starts out similar to left identity. Using the fish operator, the Haskell wiki describes it as: </p> <p> <pre>f &gt;=&gt; return ≡ f</pre> </p> <p> Translated to a C# test, an example of that law might look like this: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;ploeh&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;lawful&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">RightIdentityKleisli</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Monad.Return(s.Length); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Monad.Return; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">composed</span>&nbsp;=&nbsp;f.Compose(@return); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(composed(a),&nbsp;f(a)); }</pre> </p> <p> Again, if we inline <code>Compose</code>, the test instead looks like this: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;ploeh&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;lawful&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">RightIdentityInlined</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Monad.Return(s.Length); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Monad.Return; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">composed</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;f(x).SelectMany(@return); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(composed(a),&nbsp;f(a)); }</pre> </p> <p> Now explicitly call the <code>f</code> function and assign it to a variable <code>m</code>: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;ploeh&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;lawful&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">RightIdentityInlinedIntoAssertion</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Monad.Return(s.Length); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">@return</span>&nbsp;=&nbsp;Monad.Return; &nbsp;&nbsp;&nbsp;&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(@return),&nbsp;m); }</pre> </p> <p> The assertion is now the C# version of the normal Haskell definition of the right identity law: </p> <p> <pre>m &gt;&gt;= return = m</pre> </p> <p> In other words, <code>return</code> is the identity element for monadic composition. It doesn't really 'do' anything in itself. </p> <h3 id="3d323da500cc4405ac4768b1108cfdf9"> Associativity <a href="#3d323da500cc4405ac4768b1108cfdf9" title="permalink">#</a> </h3> <p> The third monad law is the associativity law. Keep in mind that an operator like <code>+</code> is associative if it's true that </p> <p> <pre>(x + y) + z = x + (y + z)</pre> </p> <p> Instead of <code>+</code>, the operator in question is the fish operator, and since values are functions, we typically call them <code>f</code>, <code>g</code>, and <code>h</code> rather than <code>x</code>, <code>y</code>, and <code>z</code>: </p> <p> <pre>(f &gt;=&gt; g) &gt;=&gt; h ≡ f &gt;=&gt; (g &gt;=&gt; h)</pre> </p> <p> Translated to a C# test as an example, the law looks like this: </p> <p> <pre>[Theory] [InlineData(0)] [InlineData(1)] [InlineData(2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">AssociativityKleisli</span>(<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Monad&lt;<span style="color:blue;">bool</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Monad.Return(i&nbsp;%&nbsp;2&nbsp;==&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">bool</span>,&nbsp;Monad&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;Monad.Return(b.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Monad.Return(s.Length); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">left</span>&nbsp;=&nbsp;(f.Compose(g)).Compose(h); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">right</span>&nbsp;=&nbsp;f.Compose(g.Compose(h)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(left(a),&nbsp;right(a)); }</pre> </p> <p> The outer brackets around <code>(f.Compose(g))</code> are actually redundant; we should remove them. Also, inline <code>Compose</code>: </p> <p> <pre>[Theory] [InlineData(0)] [InlineData(1)] [InlineData(2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">AssociativityInlined</span>(<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Monad&lt;<span style="color:blue;">bool</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Monad.Return(i&nbsp;%&nbsp;2&nbsp;==&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">bool</span>,&nbsp;Monad&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;Monad.Return(b.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Monad.Return(s.Length); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Monad&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">fg</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;f(x).SelectMany(g); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">left</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;fg(x).SelectMany(h); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">bool</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">gh</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">right</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;f(x).SelectMany(gh); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(left(a),&nbsp;right(a)); }</pre> </p> <p> If there's a way in C# to inline the local compositions <code>fg</code> and <code>gh</code> directly into <code>left</code> and <code>right</code>, respectively, I don't know how. In any case, the above is just an intermediate step. </p> <p> For both <code>left</code> and <code>right</code>, notice that the first step involves invoking <code>f</code> with <code>x</code>. If, instead, we call that value <code>m</code>, we can rewrite the test like this: </p> <p> <pre>[Theory] [InlineData(0)] [InlineData(1)] [InlineData(2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">AssociativityInlinedIntoAssertion</span>(<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">a</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;Monad&lt;<span style="color:blue;">bool</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Monad.Return(i&nbsp;%&nbsp;2&nbsp;==&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">bool</span>,&nbsp;Monad&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;Monad.Return(b.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Monad.Return(s.Length); &nbsp;&nbsp;&nbsp;&nbsp;Monad&lt;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">m</span>&nbsp;=&nbsp;f(a); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(m.SelectMany(g).SelectMany(h),&nbsp;m.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(x).SelectMany(h))); }</pre> </p> <p> The above assertion corresponds to the way the associativity law is usually represented in Haskell: </p> <p> <pre>(m &gt;&gt;= g) &gt;&gt;= h = m &gt;&gt;= (\x -&gt; g x &gt;&gt;= h)</pre> </p> <p> Once more, keep in mind that <code>&gt;&gt;=</code> in Haskell corresponds to <code>SelectMany</code> in C#. </p> <h3 id="d43cb529320e4dc2ab2003e68918ae92"> Conclusion <a href="#d43cb529320e4dc2ab2003e68918ae92" title="permalink">#</a> </h3> <p> A proper monad must satisfy the three monad laws: left identity, right identity, and associativity. Together, left identity and right identity are know as simply <em>identity</em>. What's so special about identity and associativity? </p> <p> As <a href="https://bartoszmilewski.com/2014/11/04/category-the-essence-of-composition/">Bartosz Milewski explains</a>, a <em>category</em> must satisfy associativity and identity. When it comes to monads, the category in question is the <a href="https://bartoszmilewski.com/2014/12/23/kleisli-categories/">Kleisli category</a>. Or, as the Haskell wiki puts it: </p> <blockquote> <p>Monad axioms:<br> Kleisli composition forms<br> a category.</p> <footer><cite><a href="https://wiki.haskell.org/Monad_laws">Haskell wiki</a></cite></footer> </blockquote> <p> Subsequent articles in this article series will now proceed to give examples of concrete monads, and what the laws look like for each of these. </p> <p> <strong>Next:</strong> <a href="/2022/04/19/the-list-monad">The List monad</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8e777ae4b3c64f5ab244b0e4d0f0d263"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#8e777ae4b3c64f5ab244b0e4d0f0d263">#</a></div> <div class="comment-content"> <blockquote> That sounds almost like a <a href="/2017/10/06/monoids">monoid</a>, but the rules are more relaxed. For monoids, we require a <a href="https://en.wikipedia.org/wiki/Binary_operation">binary operation</a>. That's not the case for functors and monads. Instead of binary operations, for monads we're going to use <a href="/2022/04/04/kleisli-composition">Kleisli composition</a>. </blockquote> <p> I am confused by this paragraph. A monoid requries a binary operation (that is also closed), and Kleisli composition is (closed) binary operation. </p> <p> As you said in the <a href="https://blog.ploeh.dk/2022/03/28/monads/">introductory article to this monad series</a>, there are three pieces to a monad: a generic type <code>M&lt;&gt;</code>, a function with type <code>a -&gt; M&lt;a&gt;</code> (typically called "return"), and a function with signature <code>(a -&gt; M&lt;b&gt;) -&gt; M&lt;a&gt; -&gt; M&lt;b&gt;</code> (sometimes called "bind"). Then <code>(M&lt;&gt;, return, bind)</code> is a monad if those three items satisfy the monad laws. </p> <p> I think the following is true. Let <code>M&lt;&gt;</code> be a generic type, and let <code>S</code> be the set of all functions with signature <code>a -&gt; M&lt;a&gt;</code>. Then <code>(M&lt;&gt;, return, bind)</code> is a monad if only only if <code>(S, &gt;=&gt;, return)</code> is a monoid. </p> </div> <div class="comment-date">2022-04-18 16:17 UTC</div> </div> <div class="comment" id="2bf9e517fa744a89928ae3d9f08fedd2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2bf9e517fa744a89928ae3d9f08fedd2">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. To address the elephant in the room first: A monad <em>is</em> a monoid (<a href="https://bartoszmilewski.com/2016/12/27/monads-categorically/">in the category of endofunctors</a>), but as far as I can tell, you have to look more abstractly at it than allowed by the static type systems we normally employ. </p> <p> <a href="https://bartoszmilewski.com/2016/12/27/monads-categorically/">Bartosz Milewski describes</a> a <a href="/2022/03/28/monads">nested functor</a> as the tensor product of two monadic types: <code>M ⊗ M</code>. The binary operation in question is then <code>M ⊗ M -> M</code>, or <em>join/flatten</em> (from here on just <em>join</em>). In this very abstract view, monads <em>are</em> monoids. It does require an abstract view of functors as objects in a category. At that level of abstraction, so many details are lost that <em>join</em> looks like a binary operation. </p> <p> So, to be clear, if you want to look at a monad as a monoid, it's not the monadic <em>bind</em> function that's the binary operation; it's <em>join</em>. (That's another reason I prefer explaining monads in terms of <em>join</em> rather than leading with <em>bind</em>.) </p> <p> When you zoom in on the details, however, <a href="https://hackage.haskell.org/package/base/docs/Control-Monad.html#v:join">join</a> doesn't look like a regular binary operation: <code>m (m a) -&gt; m a</code>. A binary operation, on the other hand, should look like this: <code>m a -&gt; m a -&gt; m a</code>. </p> <p> The Kleisli composition operator unfortunately doesn't fix this. Let's look at the C# method signature, since it may offer a way out: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;T,&nbsp;Monad&lt;T2&gt;&gt;&nbsp;<span style="color:#74531f;">Compose</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Monad&lt;T1&gt;&gt;&nbsp;<span style="color:#1f377f;">action1</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;Monad&lt;T2&gt;&gt;&nbsp;<span style="color:#1f377f;">action2</span>) </pre> </p> <p> Notice that the types don't match: <code>action1</code> has a type different from <code>action2</code>, which is again different from the return type. Thus, it's not a binary operation, because the elements don't belong to the same <a href="/2021/11/15/types-as-sets">set</a>. </p> <p> As Bartosz Milewski wrote, though, if we squint a little, we may make it work. C# allows us to 'squint' because of its object hierarchy: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;object,&nbsp;Monad&lt;object&gt;&gt;&nbsp;<span style="color:#74531f;">Compose</span>( &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;object,&nbsp;Monad&lt;object&gt;&gt;&nbsp;<span style="color:#1f377f;">action1</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;object,&nbsp;Monad&lt;object&gt;&gt;&nbsp;<span style="color:#1f377f;">action2</span>) </pre> </p> <p> I'm not sure how helpful that is, though, so I didn't want to get into these weeds in the above article itself. It's fine here in the comments, because I expect only very dedicated readers to consult them. This, thus, serves more like a footnote or appendix. </p> </div> <div class="comment-date">2022-04-23 11:29 UTC</div> </div> <div class="comment" id="2b7a3b85cfec410c8213adbfa3b7d33f"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#2b7a3b85cfec410c8213adbfa3b7d33f">#</a></div> <div class="comment-content"> <p> Thanks very much for you reply. It helped me to see where I was wrong. <code>(S, &gt;=&gt;, return)</code> is not a monoid because <code>&gt;=&gt;</code> is not total over <code>S</code>, which is another condition the binary operation must satisfy, but I overlooked that. Furthremore, I meant to define <code>S</code> as the set of all functions with signature <code>a -&gt; M&lt;b&gt;</code>. The same problem still exists though. The second code block in your comment has this same problem. There is not a natural choice for the implementation unless the types of the objects satisfy the constraints implied by the use of type parameters in the first code block in your comment. I will keep thinking about this. </p> <p> In the meantime, keep up the great work. I am loving your monad content. </p> </div> <div class="comment-date">2022-04-23 19:47 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Kleisli composition https://blog.ploeh.dk/2022/04/04/kleisli-composition 2022-04-04T06:08:00+00:00 Mark Seemann <div id="post"> <p> <em>A way to compose two monadic functions. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2022/03/28/monads">an article series about monads</a>. </p> <p> There's a bit of groundwork which will be useful before we get to the <a href="/2022/04/11/monad-laws">monad laws</a>. There are two ways to present the monad laws. One is quite unintuitive, but uses only the concepts already discussed in the <a href="/2022/03/28/monads">introductory article</a>. The other way to present the laws is much more intuitive, but builds on a derived concept: Kleisli composition. </p> <p> I find it preferable to take the intuitive route, even if it requires this little detour around the <a href="https://bartoszmilewski.com/2014/12/23/kleisli-categories/">Kleisli category</a>. </p> <h3 id="751e600750344be6bce3b19a3353ac9b"> Monadic functions <a href="#751e600750344be6bce3b19a3353ac9b" title="permalink">#</a> </h3> <p> Imagine some monad - any monad. It could be <em>Maybe</em> (AKA <em>Option</em>), <em>Either</em> (AKA <em>Result</em>), <em>Task</em>, <em>State</em>, etcetera. In <a href="https://www.haskell.org">Haskell</a> notation we denote it <code>m</code>. (Don't worry if you don't know Haskell. The type notation is fairly intuitive, and I'll explain enough of it along the way.) </p> <p> You may have a function that takes some input and returns a monadic value. Perhaps it's a parser that takes a string as input and returns a <em>Maybe</em> or <em>Either</em> as output: </p> <p> <pre>String -&gt; Maybe Int</pre> </p> <p> Perhaps it's a database query that takes an ID as input and returns a <em>Task</em> or <em>IO</em> value: </p> <p> <pre>UUID -&gt; IO Reservation</pre> </p> <p> Notice that in Haskell, you write the input to the left of the output. (This is also the case in <a href="https://fsharp.org/">F#</a>.) Functions looks like arrows that point from the input to the output, which is a common depiction that <a href="/2021/11/22/functions-as-pipes">I've also used before</a>. </p> <p> If we generalise this notation, we would write a monadic function like this: </p> <p> <pre>a -&gt; m b</pre> </p> <p> <code>a</code> is a generic type that denotes input. It could be <code>String</code> or <code>UUID</code> as shown above, or anything else. Likewise, <code>b</code> is another generic type that could be <code>Int</code>, <code>Reservation</code>, or anything else. And <code>m</code>, as already explained, may be any monad. The corresponding C# method signature would be: </p> <p> <pre>Func&lt;T,&nbsp;Monad&lt;T1&gt;&gt;</pre> </p> <p> In C#, it's <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> to name generic type arguments <code>T</code> and <code>T1</code> rather than <code>a</code> and <code>b</code>, but the idea is the same. </p> <p> The previous article used <code>Functor&lt;T&gt;</code> as a stand-in for any functor. Likewise, now that you know what a monad is, the type <code>Monad&lt;T&gt;</code> is a similar pretend type. It symbolises 'any monad'. </p> <h3 id="9581579bd43f47cf9973222d0bbec20c"> Kleisli composition <a href="#9581579bd43f47cf9973222d0bbec20c" title="permalink">#</a> </h3> <p> In <a href="/2021/11/22/functions-as-pipes">Functions as pipes</a> you got a visual illustration of how functions compose. If you have a function that returns a Boolean value, you can use that Boolean value as input to a function that accepts Boolean values as input. Generalised, if one function returns <code>a</code> and another function accepts <code>a</code> as input, you can compose these two functions: </p> <p> <pre>(a -&gt; b) -&gt; (b -&gt; c) -&gt; (a -&gt; c)</pre> </p> <p> A combinator of this type takes two functions as input: <code>a -&gt; b</code> (a function from <code>a</code> to <code>b</code>) and <code>(b -&gt; c)</code>, and returns a new function that 'hides' the middle step: <code>a -&gt; c</code>. </p> <p> If this is possible, it seems intuitive that you can also compose monadic functions: </p> <p> <pre>(&gt;=&gt;) :: Monad m =&gt; (a -&gt; m b) -&gt; (b -&gt; m c) -&gt; a -&gt; m c</pre> </p> <p> The <a href="https://hackage.haskell.org/package/base/docs/Control-Monad.html#v:-62--61--62-">&gt;=&gt;</a> operator is known as the <em>fish</em> operator - or more precisely the <em>right fish</em> operator, since there's also a <em>left fish</em> operator <a href="https://hackage.haskell.org/package/base/docs/Control-Monad.html#v:-60--61--60-">&lt;=&lt;</a>. </p> <p> This is known as <em>Kleisli composition</em>. In C# you could write it like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;T,&nbsp;Monad&lt;T2&gt;&gt;&nbsp;<span style="color:#74531f;">Compose</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Func&lt;T,&nbsp;Monad&lt;T1&gt;&gt;&nbsp;<span style="color:#1f377f;">action1</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;Monad&lt;T2&gt;&gt;&nbsp;<span style="color:#1f377f;">action2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;action1(x).SelectMany(action2); }</pre> </p> <p> Notice that Kleisli composition works for any monad. You don't need any new abilities in order to compose a monad in this way. The composition exclusively relies on <code>SelectMany</code> (monadic <em>bind</em>). </p> <p> Here's a usage example: </p> <p> <pre>Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;Monad.Return(s.Length); Func&lt;<span style="color:blue;">int</span>,&nbsp;Monad&lt;<span style="color:blue;">bool</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;Monad.Return(i&nbsp;%&nbsp;2&nbsp;==&nbsp;0); Func&lt;<span style="color:blue;">string</span>,&nbsp;Monad&lt;<span style="color:blue;">bool</span>&gt;&gt;&nbsp;<span style="color:#1f377f;">composed</span>&nbsp;=&nbsp;f.Compose(g);</pre> </p> <p> The <code>Compose</code> method corresponds to the right fish operator, but obviously you could define an overload that flips the arguments. That would correspond to the left fish operator. </p> <h3 id="6c831b646c374287a07c3918097818e2"> Universality <a href="#6c831b646c374287a07c3918097818e2" title="permalink">#</a> </h3> <p> Kleisli composition seems compelling, but in reality I find that I rarely use it. If it isn't that useful, then why dedicate an entire article to it? </p> <p> As I wrote in the introduction, it's mostly because it gives us an intuitive way to describe the monad laws. </p> <p> Kleisli arrows form a <a href="https://en.wikipedia.org/wiki/Category_theory">category</a>. As such, it's a universal abstraction. I admit that <a href="/2017/10/04/from-design-patterns-to-category-theory">this article series</a> has strayed away from its initial vision of discussing category theory, but I still find it assuring when I encounter some bedrock of theory behind a programming concept. </p> <h3 id="afc020e8bbe7436b891782b8d469c111"> Conclusion <a href="#afc020e8bbe7436b891782b8d469c111" title="permalink">#</a> </h3> <p> Kleisli composition lets you compose two (or more) compatible monadic functions into a single monadic function. </p> <p> <strong>Next:</strong> <a href="/2022/04/11/monad-laws">Monad laws</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Monads https://blog.ploeh.dk/2022/03/28/monads 2022-03-28T06:12:00+00:00 Mark Seemann <div id="post"> <p> <em>A monad is a common abstraction. While typically associated with Haskell, monads also exist in C# and other languages.</em> </p> <p> This article series is part of <a href="/2018/03/19/functors-applicatives-and-friends">a larger series of articles about functors, applicatives, and other mappable containers</a>. </p> <p> If you understand what a <a href="/2018/03/22/functors">functor</a> is, it should be easy to grasp the idea of a monad. <em>It's a functor you can flatten.</em> </p> <p> There's a little more to it than that, and you also need to learn what I mean by <em>flatten</em>, but that's the essence of it. </p> <p> In this article-series-within-an-article series, I'll explain this disproportionally dreaded concept, giving you plenty of examples as well as a more formal definition. Examples will be in both C#, <a href="https://fsharp.org">F#</a>, and <a href="https://www.haskell.org">Haskell</a>, although I plan to emphasise C#. I expect that there's little need of explaining monads to people already familiar with Haskell. I'll show examples as separate articles in this series. These articles are mostly aimed at object-oriented programmers curious about monads. </p> <ul> <li><a href="/2022/04/04/kleisli-composition">Kleisli composition</a></li> <li><a href="/2022/04/11/monad-laws">Monad laws</a></li> <li><a href="/2022/04/19/the-list-monad">The List monad</a></li> <li><a href="/2022/04/25/the-maybe-monad">The Maybe monad</a></li> <li><a href="/2022/05/09/an-either-monad">An Either monad</a></li> <li><a href="/2022/05/16/the-identity-monad">The Identity monad</a></li> <li><a href="/2022/05/30/the-lazy-monad">The Lazy monad</a></li> <li><a href="/2022/06/06/asynchronous-monads">Asynchronous monads</a></li> <li><a href="/2022/06/20/the-state-monad">The State monad</a></li> <li><a href="/2022/11/14/the-reader-monad">The Reader monad</a></li> <li><a href="/2023/01/09/the-io-monad">The IO monad</a></li> <li><a href="/2023/02/27/test-data-generator-monad">Test Data Generator monad</a></li> </ul> <p> This list is far from exhaustive; more monads exist. </p> <p> <img src="/content/binary/functors-applicatives-monads.png" alt="Set diagram with monads shown as a subset of applicatives, which is shown as a subset of functors, again a subset of generic types."> </p> <p> All monads are <a href="/2018/10/01/applicative-functors">applicative functors</a>, but not all applicative functors are monads. As the set diagram reiterates, applicative functors are functors, but again: not necessarily the other way around. </p> <h3 id="a342785804ef4d5ebb6e88cef8140432"> Flattening <a href="#a342785804ef4d5ebb6e88cef8140432" title="permalink">#</a> </h3> <p> A monad is a functor you can flatten. What do I mean by <em>flatten?</em> </p> <p> As in <a href="/2018/03/22/functors">the article that introduces functors</a>, imagine that you have some <code>Functor&lt;T&gt;</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a>. More concretely, this could be <code>IEnumerable&lt;T&gt;</code>, <code>Maybe&lt;T&gt;</code>, <code>Task&lt;T&gt;</code>, etcetera. </p> <p> When working with functors, you sometimes run into situations where the containers nest inside each other. Instead of a <code>Functor&lt;decimal&gt;</code>, you may find yourself with a <code>Functor&lt;Functor&lt;decimal&gt;&gt;</code>, or maybe even an object that's thrice or quadruply nested. </p> <p> If you can flatten such nested functor objects to just <code>Functor&lt;T&gt;</code>, then that functor also forms a monad. </p> <p> In C#, you need a method with this kind of signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Functor&lt;T&gt;&nbsp;<span style="color:#74531f;">Flatten</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Functor&lt;Functor&lt;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>)</pre> </p> <p> Notice that this method takes a <code>Functor&lt;Functor&lt;T&gt;&gt;</code> as input and returns a <code>Functor&lt;T&gt;</code> as output. I'm not showing the actual implementation, since <code>Functor&lt;T&gt;</code> is really just a 'pretend' functor (and monad). In the example articles that follow, you'll see actual implementation code. </p> <p> With <code>Flatten</code> you can write code like this: </p> <p> <pre>Functor&lt;<span style="color:blue;">decimal</span>&gt;&nbsp;<span style="color:#1f377f;">dest</span>&nbsp;=&nbsp;source.Flatten();</pre> </p> <p> Here, <code>source</code> is a <code>Functor&lt;Functor&lt;<span style="color:blue;">decimal</span>&gt;&gt;</code>. </p> <p> This function also exists in Haskell, although there, it's called <a href="https://hackage.haskell.org/package/base/docs/Control-Monad.html#v:join">join</a>: </p> <p> <pre>join :: Monad m =&gt; m (m a) -&gt; m a</pre> </p> <p> For any <code>Monad m</code> you can flatten or join a nested monad <code>m (m a)</code> to a 'normal' monad <code>m a</code>. </p> <p> This is the same function as <code>Flatten</code>, so in C# we could define an alias like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Functor&lt;T&gt;&nbsp;<span style="color:#74531f;">Join</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Functor&lt;Functor&lt;T&gt;&gt;&nbsp;<span style="color:#1f377f;">source</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.Flatten(); }</pre> </p> <p> Usually, however, you don't see monads introduced via <code>join</code> or <code>Flatten</code>. It's a shame, really, because it's the most straightforward description. </p> <h3 id="948d267819284e61af68d575c9d294e6"> Bind <a href="#948d267819284e61af68d575c9d294e6" title="permalink">#</a> </h3> <p> Most languages that come with a notion of monads define them via a function colloquially referred to as <em>monadic bind</em> or just <em>bind</em>. In C# it's called <code>SelectMany</code>, <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/computation-expressions">F# computation expressions</a> call it <code>Bind</code>, and Scala calls it <code>flatMap</code> (with a vestige of the concept of flattening). Haskell just defines it as an operator: </p> <p> <pre>(&gt;&gt;=) :: Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b</pre> </p> <p> When talking about it, I tend to call the <code>&gt;&gt;=</code> operator <em>bind</em>. </p> <p> Translated to C# it has this kind of signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Functor&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Functor&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Functor&lt;TResult&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>)</pre> </p> <p> It looks almost like a functor's <code>Select</code> method (AKA <code>map</code> or <code>fmap</code> function). The difference is that this <code>selector</code> returns a functor value (<code>Functor&lt;TResult&gt;</code>) rather than a 'naked' <code>TResult</code> value. </p> <p> When a <code>Flatten</code> (or <code>Join</code>) method already exists, you can always implement <code>SelectMany</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Functor&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Functor&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Functor&lt;TResult&gt;&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Functor&lt;Functor&lt;TResult&gt;&gt;&nbsp;<span style="color:#1f377f;">nest</span>&nbsp;=&nbsp;source.Select(selector); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;nest.Flatten(); }</pre> </p> <p> I've split the implementation over two lines of code to make it clearer what's going on. That's also the reason I've used an explicit type declaration rather than <code>var</code>. </p> <p> The <code>Select</code> method exists because the type is already a functor. When you call <code>Select</code> with <code>selector</code> you get a <code>Functor&lt;Functor&lt;TResult&gt;&gt;</code> because <code>selector</code> itself returns a <code>Functor&lt;TResult&gt;</code>. That's a nested functor, which you can <code>Flatten</code>. </p> <p> You can use <code>SelectMany</code> like this: </p> <p> <pre>Functor&lt;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">source</span>&nbsp;=&nbsp;Functor.Return(TimeSpan.FromMinutes(32)); Functor&lt;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">dest</span>&nbsp;=&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;Functor.Return(x.TotalSeconds&nbsp;+&nbsp;192));</pre> </p> <p> The lambda expression returns a <code>Functor&lt;double&gt;</code>. Had you called <code>Select</code> with this lambda expression, the result would have been a nested functor. <code>SelectMany</code>, however, flattens while it maps (it <em>flatMaps</em>). (This example is rather inane, since it would be simpler to use <code>Select</code> and omit wrapping the output of the lambda in the functor. The later articles in this series will show some more compelling examples.) </p> <p> If you already have <code>Flatten</code> (or <code>Join</code>) you can always implement <code>SelectMany</code> like this. Usually, however, languages use monadic bind (e.g. <code>SelectMany</code>) as the foundation. Both C#, Haskell, and F# do that. In that case, it's also possible to go the other way and implement <code>Flatten</code> with <code>SelectMany</code>. You'll see examples of that in the subsequent articles. </p> <h3 id="ed8ec161f2f3424089c5c0cee07d5fbd"> Syntactic sugar <a href="#ed8ec161f2f3424089c5c0cee07d5fbd" title="permalink">#</a> </h3> <p> Several languages understand monads well enough to provide some degree of syntactic sugar. While you can always use <code>SelectMany</code> in C# by simply calling the method, there are situations where that becomes awkward. The same is true with <code>bind</code> functions in F# and <code>&gt;&gt;=</code> in Haskell. </p> <p> In C# you can use <em>query syntax</em>: </p> <p> <pre>Functor&lt;DateTime&gt;&nbsp;<span style="color:#1f377f;">fdt</span>&nbsp;=&nbsp;Functor.Return(<span style="color:blue;">new</span>&nbsp;DateTime(2001,&nbsp;8,&nbsp;3)); Functor&lt;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">fts</span>&nbsp;=&nbsp;Functor.Return(TimeSpan.FromDays(4)); Functor&lt;DateTime&gt;&nbsp;<span style="color:#1f377f;">dest</span>&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;dt&nbsp;<span style="color:blue;">in</span>&nbsp;fdt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;ts&nbsp;<span style="color:blue;">in</span>&nbsp;fts &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;dt&nbsp;-&nbsp;ts;</pre> </p> <p> In order to support more than a single <code>from</code> expression, however, you must supply a special <code>SelectMany</code> overload, or the code doesn't compile: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Functor&lt;TResult&gt;&nbsp;<span style="color:#74531f;">SelectMany</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Functor&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;Functor&lt;U&gt;&gt;&nbsp;<span style="color:#1f377f;">k</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;U,&nbsp;TResult&gt;&nbsp;<span style="color:#1f377f;">s</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.SelectMany(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;k(x).Select(<span style="color:#1f377f;">y</span>&nbsp;=&gt;&nbsp;s(x,&nbsp;y))); }</pre> </p> <p> This is an odd quirk of C# that I've yet to encounter in other languages, and I consider it <a href="/2019/12/16/zone-of-ceremony">ceremony</a> that I have to perform to satiate the C# compiler. It's not part of the concept of what a monad is. As far as I can tell, you can always implement it like shown here. </p> <p> Haskell's syntactic sugar is called <em>do notation</em> and works for all monads: </p> <p> <pre><span style="color:#2b91af;">example</span>&nbsp;::&nbsp;(<span style="color:blue;">Monad</span>&nbsp;m,&nbsp;<span style="color:blue;">Num</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;m&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a example&nbsp;fdt&nbsp;fts&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;dt&nbsp;&lt;-&nbsp;fdt &nbsp;&nbsp;ts&nbsp;&lt;-&nbsp;fts &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;dt&nbsp;-&nbsp;ts</pre> </p> <p> In F#, <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a> drive syntactic sugar, but still based (among other things) on the concept of a monad: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;example&nbsp;(fdt&nbsp;:&nbsp;Monad&lt;DateTime&gt;)&nbsp;(fts&nbsp;:&nbsp;Monad&lt;TimeSpan&gt;)&nbsp;=&nbsp;monad&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;dt&nbsp;=&nbsp;fdt &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;ts&nbsp;=&nbsp;fts &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;dt&nbsp;-&nbsp;ts&nbsp;}</pre> </p> <p> Here, we again have to pretend that a <code>Monad&lt;'a&gt;</code> type exists, with a companion <code>monad</code> computation expression. </p> <h3 id="440abd098e1144c78229eb0ee2d0ad4f"> Return <a href="#440abd098e1144c78229eb0ee2d0ad4f" title="permalink">#</a> </h3> <p> The ability to flatten nested functors is the essence of what a monad is. There are, however, also some formal requirements. As is the case with functors, there are laws that a monad should obey. I'll dedicate <a href="/2022/04/11/monad-laws">a separate article</a> for those, so we'll skip them for now. </p> <p> Another requirement is that in addition to <code>SelectMany</code>/<code>bind</code>/<code>flatMap</code>/<code>&gt;&gt;=</code> a monad must also provide a method to 'elevate' a value to the monad. This function is usually called <em>return</em>. It should have a type like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Functor&lt;T&gt;&nbsp;<span style="color:#74531f;">Return</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>)</pre> </p> <p> You can see it in use in the above code snippet, and with the relevant parts repeated here: </p> <p> <pre>Functor&lt;DateTime&gt;&nbsp;<span style="color:#1f377f;">fdt</span>&nbsp;=&nbsp;Functor.Return(<span style="color:blue;">new</span>&nbsp;DateTime(2001,&nbsp;8,&nbsp;3)); Functor&lt;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">fts</span>&nbsp;=&nbsp;Functor.Return(TimeSpan.FromDays(4));</pre> </p> <p> This code example lifts the value <code>DateTime(2001,&nbsp;8,&nbsp;3)</code> to a <code>Functor&lt;DateTime&gt;</code> value, and the value <code>TimeSpan.FromDays(4)</code> to a <code>Functor&lt;TimeSpan&gt;</code> value. </p> <p> Usually, in a language like C# a concrete monad type may either afford a constructor or factory method for this purpose. You'll see examples in the articles that give specific monad examples. </p> <h3 id="558c849b4c644f9fb28c788aa43cb263"> Conclusion <a href="#558c849b4c644f9fb28c788aa43cb263" title="permalink">#</a> </h3> <p> A monad is a functor you can flatten. That's the simplest way I can put it. </p> <p> In this article you learned what <em>flattening</em> means. You also learned that a monad must obey some laws and have a <em>return</em> function. The essence, however, is flattening. The laws just ensure that the flattening is well-behaved. </p> <p> It may not yet be clear to you why some functors would need flattening in the first place, but subsequent articles will show examples. </p> <p> The word <em>monad</em> has its own mystique in software development culture. If you've ever encountered some <a href="/2021/06/07/abstruse-nomenclature">abstruse nomenclature</a> about monads, then try to forget about it. The concept is really not hard to grasp, and it usually clicks once you've seen enough examples, and perhaps done some exercises. </p> <p> <strong>Next:</strong> <a href="/2022/04/04/kleisli-composition">Kleisli composition</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="86bf8188e50f4dee891bde7f2b7a8225"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#86bf8188e50f4dee891bde7f2b7a8225">#</a></div> <div class="comment-content"> <p>Quoting image in words:</p> <blockquote> <ul> <li>Every Monad is an applicative functor</li> <li>Every applicative functor is a functor</li> <li>Every functor is a generic type</li> </ul> </blockquote> <p> As your aricle about <a href="https://blog.ploeh.dk/2020/10/19/monomorphic-functors/">Monomorphic functors</a> shows, not every functor is a generic type. However, it is true that every applicative functor is a generic (or polymorphic) functor. </p> </div> <div class="comment-date">2022-04-18 15:28 UTC</div> </div> <div class="comment" id="2971857cfe8d4766bb48b37ce92eef4b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2971857cfe8d4766bb48b37ce92eef4b">#</a></div> <div class="comment-content"> <p> True, the illustration is a simplification. </p> </div> <div class="comment-date">2022-04-18 20:38 UTC</div> </div> <div class="comment" id="adddc3fdc50ac979899dfbe272794544"> <div class="comment-author"><a href="https://github.com/mormegil-cz">Petr Kadlec</a> <a href="#adddc3fdc50ac979899dfbe272794544">#</a></div> <div class="comment-content"> <p> I guess you might know that already but the reason for the strange <code>SelectMany</code> ceremonial version is compiler performance. The complicated form is needed so that the overload resolution does not take exponential amount of time. See <a href="https://ericlippert.com/2013/04/02/monads-part-twelve/">a 2013 article by Eric Lippert covering this</a>. </p> </div> <div class="comment-date">2022-05-02 11:50 UTC</div> </div> <div class="comment" id="f7409894f143485683c9862221f3a903"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f7409894f143485683c9862221f3a903">#</a></div> <div class="comment-content"> <p> Petr, thank you for writing. I remember reading about it before, but it's good to have a proper source. </p> <p> If I understand the article correctly, the problem seems to originate from overload resolution. This would, at least, explain why neither F# nor Haskell needs that extra overload. I wonder if a language like Java would have a similar problem, but I don't think that Java has syntactic sugar for monads. </p> <p> It still seems a little strange that the C# compiler <em>requires</em> the presence of that overload. Eric Lippert seems to strongly imply that it's always possible to derive that extra overload from any monad's fundamental functions (as is also my conjecture). If this is so, the C# compiler should be able to do that work by itself. Monads that wish to supply a more efficient implement (e.g. <code>IEnumerable&lt;T&gt;</code>) could still do so: The C# compiler could look for the overload and use it if it finds it, and still fall back to the default implementation if none is available. </p> <p> But I suppose that since the query syntax feature only really exists to support <code>IEnumerable&lt;T&gt;</code> this degree of sophistication wasn't warranted. </p> </div> <div class="comment-date">2022-05-03 6:12 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Contravariant Dependency Injection https://blog.ploeh.dk/2022/03/21/contravariant-dependency-injection 2022-03-21T06:46:00+00:00 Mark Seemann <div id="post"> <p> <em>How to use a DI Container in FP... or not.</em> </p> <p> This article is an instalment in <a href="/2021/09/02/contravariant-functors">an article series about contravariant functors</a>. It assumes that you've read the introduction, and a few of the examples. </p> <p> People sometimes ask me how to do Dependency Injection (DI) in Functional Programming (FP). The short answer is that <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">you don't, because dependencies make functions impure</a>. If you're having the discussion on Twitter, you may get some drive-by comments to the effect that <em>DI is just a contravariant functor</em>. (I've seen such interactions take place, but currently can't find the specific examples.) </p> <p> What might people mean by that? </p> <p> In this article, I'll explain. You'll also see how this doesn't address the underlying problem that DI makes everything impure. Ultimately, then, it doesn't really matter. It's still not functional. </p> <h3 id="43f01ded1bbd42e28a90f398152518ff"> Partial application as Dependency Injection <a href="#43f01ded1bbd42e28a90f398152518ff" title="permalink">#</a> </h3> <p> As example code, let's revisit the code from the article <a href="/2017/01/30/partial-application-is-dependency-injection">Partial application is dependency injection</a>. You may have an impure action like this: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;(DateTimeOffset&nbsp;-&gt;&nbsp;Reservation&nbsp;list)&nbsp;-&gt;&nbsp;(Reservation&nbsp;-&gt;&nbsp;int)&nbsp;-&gt;&nbsp;Reservation</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;tryAccept&nbsp;capacity&nbsp;readReservations&nbsp;createReservation&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations&nbsp;reservation.Date&nbsp;|&gt;&nbsp;List.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;createReservation&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;|&gt;&nbsp;Some &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;None</pre> </p> <p> The <code>readReservations</code> and <code>createReservation</code> 'functions' will in reality be impure actions that read from and write to a database. Since they are impure, the entire <code>tryAccept</code> action is impure. That's the result from that article. </p> <p> In this example the 'dependencies' are pushed to the left. This enables you to partially apply the 'function' with the dependencies: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;=&nbsp;tryAccept&nbsp;capacity&nbsp;readReservations&nbsp;createReservation</pre> </p> <p> This code example is from a unit test, which is the reason that I've <a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/naming-sut-test-variables">named the partially applied 'function' <code>sut</code></a>. It has the type <code>Reservation -&gt; int option</code>. Keep in mind that while it looks like a regular function, in practice it'll be an impure action. </p> <p> You can invoke <code>sut</code> like another other function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut&nbsp;reservation</pre> </p> <p> When you do this, <code>tryAccept</code> will execute, interact with the partially applied dependencies, and return a result. </p> <p> Partial application is a standard practice in FP, and in itself it's useful. Using it for Dependency Injection, however, doesn't magically make DI functional. DI remains impure, which also means that the entire <code>sut</code> composition is impure. </p> <h3 id="88c2633d1dd34a979aaca1a223ff665b"> Moving dependencies to the right <a href="#88c2633d1dd34a979aaca1a223ff665b" title="permalink">#</a> </h3> <p> Pushing dependencies to the left enables partial application. Dependencies are typically more long-lived than run-time values like <code>reservation</code>. Thus, partially applying the dependencies as shown above makes sense. If all dependencies are thread-safe, you can let an action like <code>sut</code>, above, have Singleton lifetime. In other words, just keep a single, stateless <code>Reservation -&gt; int option</code> action around for the lifetime of the application, and pass it <code>Reservation</code> values as they arrive. </p> <p> Moving dependencies to the right seems, then, counterproductive. </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;(DateTimeOffset&nbsp;-&gt;&nbsp;Reservation&nbsp;list)&nbsp;-&gt;&nbsp;(Reservation&nbsp;-&gt;&nbsp;int)</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;tryAccept&nbsp;reservation&nbsp;capacity&nbsp;readReservations&nbsp;createReservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations&nbsp;reservation.Date&nbsp;|&gt;&nbsp;List.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;createReservation&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;|&gt;&nbsp;Some &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;None</pre> </p> <p> If you move the dependencies to the right, you can no longer partially apply them, so what would be the point of that? </p> <p> As given above, it seems that you'll have to supply all arguments in one go: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;tryAccept&nbsp;reservation&nbsp;capacity&nbsp;readReservations&nbsp;createReservation</pre> </p> <p> If, on the other hand, you turn all the dependencies into a tuple, some new options arise: </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;(int&nbsp;*&nbsp;(DateTimeOffset&nbsp;-&gt;&nbsp;Reservation&nbsp;list)&nbsp;*&nbsp;(Reservation&nbsp;-&gt;&nbsp;int))</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;tryAccept&nbsp;reservation&nbsp;(capacity,&nbsp;readReservations,&nbsp;createReservation)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations&nbsp;reservation.Date&nbsp;|&gt;&nbsp;List.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;createReservation&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;|&gt;&nbsp;Some &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;None</pre> </p> <p> I decided to count <code>capacity</code> as a dependency, since this is most likely a value that originates from a configuration setting somewhere. Thus, it'd tend to have a singleton lifetime equivalent to <code>readReservations</code> and <code>createReservation</code>. </p> <p> It still seems that you have to supply all the arguments, though: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;tryAccept&nbsp;reservation&nbsp;(capacity,&nbsp;readReservations,&nbsp;createReservation)</pre> </p> <p> What's the point, then? </p> <h3 id="8bf311e8b5184d1396e7f49b5b9d9b48"> Reader <a href="#8bf311e8b5184d1396e7f49b5b9d9b48" title="permalink">#</a> </h3> <p> You can view all functions as values of the <a href="/2021/11/08/reader-as-a-profunctor">Reader profunctor</a>. This, then, is also true for <code>tryAccept</code>. It's an (impure) function that takes a <code>Reservation</code> as input, and returns a Reader value as output. The 'Reader environment' is a tuple of dependencies, and the output is <code>int option</code>. </p> <p> First, add an explicit <code>Reader</code> module: </p> <p> <pre><span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;&#39;b</span> <span style="color:blue;">let</span>&nbsp;run&nbsp;env&nbsp;rdr&nbsp;=&nbsp;rdr&nbsp;env <span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;d)&nbsp;-&gt;&nbsp;(&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;d</span> <span style="color:blue;">let</span>&nbsp;dimap&nbsp;f&nbsp;g&nbsp;rdr&nbsp;=&nbsp;<span style="color:blue;">fun</span>&nbsp;env&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;g&nbsp;(rdr&nbsp;(f&nbsp;env)) <span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;a)&nbsp;-&gt;&nbsp;&#39;c&nbsp;-&gt;&nbsp;&#39;b</span> <span style="color:blue;">let</span>&nbsp;map&nbsp;g&nbsp;=&nbsp;dimap&nbsp;id&nbsp;g <span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;contraMap&nbsp;f&nbsp;=&nbsp;dimap&nbsp;f&nbsp;id</pre> </p> <p> The <code>run</code> function is mostly there to make the Reader concept more explicit. The <code>dimap</code> implementation is what makes Reader a <a href="/2021/11/01/profunctors">profunctor</a>. You can use <code>dimap</code> to implement <code>map</code> and <code>contraMap</code>. In the following, we're not going to need <code>map</code>, but I still included it for good measure. </p> <p> You can partially apply the last version of <code>tryAccept</code> with a <code>reservation</code>: </p> <p> <pre><span style="color:green;">//&nbsp;sut&nbsp;:&nbsp;(int&nbsp;*&nbsp;(DateTimeOffset&nbsp;-&gt;&nbsp;Reservation&nbsp;list)&nbsp;*&nbsp;(Reservation&nbsp;-&gt;&nbsp;int))&nbsp;-&gt;&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;sut&nbsp;=&nbsp;tryAccept&nbsp;reservation</pre> </p> <p> You can view the <code>sut</code> value as a Reader. The 'environment' is the tuple of dependencies, and the output is still <code>int option</code>. Since we can <a href="/2022/03/07/a-type-safe-di-container-as-a-tuple">view a tuple as a type-safe DI Container</a>, we've now introduced a DI Container to the 'function'. We can say that the action is a Reader of the DI Container. </p> <p> You can <code>run</code> the action by supplying a <code>container</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;container&nbsp;=&nbsp;(capacity,&nbsp;readReservations,&nbsp;createReservation) <span style="color:blue;">let</span>&nbsp;sut&nbsp;=&nbsp;tryAccept&nbsp;reservation <span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut&nbsp;|&gt;&nbsp;Reader.run&nbsp;container</pre> </p> <p> This <code>container</code>, however, is specialised to fit <code>tryAccept</code>. What if other actions require other dependencies? </p> <h3 id="ada91148e72f45e3b274d89e19644f50"> Contravariant mapping <a href="#ada91148e72f45e3b274d89e19644f50" title="permalink">#</a> </h3> <p> In a larger system, you're likely to have more than the three dependencies shown above. Other actions require other dependencies, and may not need <code>capacity</code>, <code>readReservations</code>, or <code>createReservation</code>. </p> <p> To make the point, perhaps the <code>container</code> looks like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;container&nbsp;=&nbsp;(capacity,&nbsp;readReservations,&nbsp;createReservation,&nbsp;someOtherService,&nbsp;yetAnotherService)</pre> </p> <p> If you try to <code>run</code> a partially applied Reader like <code>sut</code> with this action, your code doesn't compile. <code>sut</code> needs a triple of a specific type, but <code>container</code> is now a pentuple. Should you change <code>tryAccept</code> so that it takes the bigger pentuple as a parameter? </p> <p> That would compile, but would be a leaky abstraction. It would also violate the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> and the <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> because <code>tryAccept</code> would require callers to supply a bigger argument than is needed. </p> <p> Instead, you can <code>contraMap</code> the pentuple: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;container&nbsp;=&nbsp;(capacity,&nbsp;readReservations,&nbsp;createReservation,&nbsp;someOtherService,&nbsp;yetAnotherService) <span style="color:blue;">let</span>&nbsp;sut&nbsp;=&nbsp;tryAccept&nbsp;reservation&nbsp;|&gt;&nbsp;Reader.contraMap&nbsp;(<span style="color:blue;">fun</span>&nbsp;(c,&nbsp;rr,&nbsp;cr,&nbsp;_,&nbsp;_)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(c,&nbsp;rr,&nbsp;cr)) <span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut&nbsp;|&gt;&nbsp;Reader.run&nbsp;container</pre> </p> <p> This turns <code>sut</code> into a Reader of the pentuple, instead of a Reader of the triple. You can now <code>run</code> it with the pentuple <code>container</code>. <code>tryAccept</code> remains a Reader of the triple. </p> <h3 id="e8cb20a7be2342f7802f6522fa5edd5a"> Conclusion <a href="#e8cb20a7be2342f7802f6522fa5edd5a" title="permalink">#</a> </h3> <p> Keep in mind that <code>tryAccept</code> remains impure. While this may look impressively functional, it really isn't. It doesn't address the original problem that <a href="/2021/07/28/referential-transparency-fits-in-your-head">composition of impure actions creates larger impure actions that don't fit in your head</a>. </p> <p> Why, then, make things more complicated than they have to be? </p> <p> In FP, I'd look long and hard for a better alternative - one that obeys <a href="/2018/11/19/functional-architecture-a-definition">the functional architecture law</a>. If, for various reasons, I was unable to produce a purely functional design, I'd prefer composing impure actions with partial application. It's simpler. </p> <p> This article isn't an endorsement of using contravariant DI in FP. It's only meant as an explanation of a phrase like <em>DI is just a contravariant functor</em>. </p> <p> <strong>Next:</strong> <a href="/2021/11/01/profunctors">Profunctors</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Type-safe DI Containers are redundant https://blog.ploeh.dk/2022/03/14/type-safe-di-containers-are-redundant 2022-03-14T05:27:00+00:00 Mark Seemann <div id="post"> <p> <em>Just use Pure DI.</em> </p> <p> This article is part of a series called <a href="/2022/01/10/type-safe-di-composition">Type-safe DI composition</a>. In the previous article, you saw <a href="/2022/03/07/a-type-safe-di-container-as-a-tuple">how tuples are adequate DI Containers</a>. In case it's not clear from the article that introduces the series, there's really no point to any of this. My motivation for writing the article is that readers sometimes ask me about topics such as DI Containers versus type safety, or DI Containers in functional programming. The goal of these articles is to make it painfully clear why I find such ideas moot. </p> <h3 id="5cc2827dd110479c980af791313d03da"> Tuples as DI Containers <a href="#5cc2827dd110479c980af791313d03da" title="permalink">#</a> </h3> <p> In the <a href="/2022/03/07/a-type-safe-di-container-as-a-tuple">previous article</a> you saw how tuples make adequate DI Containers. Consider, however, this constructor: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&nbsp;container; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CompositionRoot</span>( &nbsp;&nbsp;&nbsp;&nbsp;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&nbsp;<span style="color:#1f377f;">container</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.container&nbsp;=&nbsp;container; }</pre> </p> <p> One question, though: Why pass a tuple of three objects as a single argument? Wouldn't it be more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> to use a normal argument list? </p> <h3 id="1612596b46e045bbae911e2868a4bbfe"> Argument list <a href="#1612596b46e045bbae911e2868a4bbfe" title="permalink">#</a> </h3> <p> Just remove the brackets around the tuple argument and dissolve the elements into separate class fields: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IRestaurantDatabase&nbsp;rdb; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IClock&nbsp;clock; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IReservationsRepository&nbsp;repo; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CompositionRoot</span>(IRestaurantDatabase&nbsp;<span style="color:#1f377f;">rdb</span>,&nbsp;IClock&nbsp;<span style="color:#1f377f;">clock</span>,&nbsp;IReservationsRepository&nbsp;<span style="color:#1f377f;">repo</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.rdb&nbsp;=&nbsp;rdb; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.clock&nbsp;=&nbsp;clock; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repo&nbsp;=&nbsp;repo; }</pre> </p> <p> You no longer need to <a href="/2010/09/29/TheRegisterResolveReleasepattern">resolve</a> services from a DI Container. You can simply use the class fields directly: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;<span style="color:#74531f;">Create</span>(ControllerContext&nbsp;<span style="color:#1f377f;">context</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(context&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(context)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;context.ActionDescriptor.ControllerTypeInfo; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(CalendarController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;CalendarController(rdb,&nbsp;repo); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(HomeController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;HomeController(rdb); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ReservationsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController(clock,&nbsp;rdb,&nbsp;repo); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(RestaurantsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantsController(rdb); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ScheduleController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ScheduleController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rdb, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repo, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AccessControlList.FromUser(context.HttpContext.User)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Unexpected&nbsp;controller&nbsp;type:&nbsp;</span>{t}<span style="color:#a31515;">.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(context)); }</pre> </p> <p> Instead of <code>container.rdb</code>, <code>container.repo</code>, and <code>container.clock</code>, you can simply access <code>rdb</code>, <code>repo</code>, and <code>clock</code>. </p> <p> The <a href="/2010/09/29/TheRegisterResolveReleasepattern">registration phase</a> (such as it now remains) also becomes simpler: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">rdb</span>&nbsp;=&nbsp;CompositionRoot.CreateRestaurantDatabase(Configuration); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">po</span>&nbsp;=&nbsp;CompositionRoot.CreatePostOffice(Configuration,&nbsp;rdb); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">clock</span>&nbsp;=&nbsp;CompositionRoot.CreateClock(); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">repo</span>&nbsp;=&nbsp;CompositionRoot.CreateRepository(Configuration,&nbsp;po); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">compositionRoot</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompositionRoot(rdb,&nbsp;clock,&nbsp;repo); services.AddSingleton&lt;IControllerActivator&gt;(compositionRoot);</pre> </p> <p> There's no longer any remnant of a DI Container. This is <a href="/2014/06/10/pure-di">Pure DI</a>. </p> <h3 id="b7d5b475bb88437ca9a39c6d2fbda35a"> Conclusion <a href="#b7d5b475bb88437ca9a39c6d2fbda35a" title="permalink">#</a> </h3> <p> Type-safe DI Containers are redundant. The simplest way to compose dependencies in a type-safe fashion is to write normal code, i.e. Pure DI. </p> <p> What about 'normal' DI Containers, though? These aren't type-safe, but <a href="/2012/11/06/WhentouseaDIContainer">may have their uses</a>. They represent a trade-off. For sufficiently complicated code bases, they may offer some benefits. </p> <p> I typically don't consider the trade-off worthwhile. The keywords in the above paragraph are 'sufficiently complicated'. Instead of writing complicated code, I favour simplicity: <a href="/code-that-fits-in-your-head">code that fits in my head</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A type-safe DI Container as a tuple https://blog.ploeh.dk/2022/03/07/a-type-safe-di-container-as-a-tuple 2022-03-07T07:42:00+00:00 Mark Seemann <div id="post"> <p> <em>Tuples are DI Containers. With example code in C#.</em> </p> <p> This article is part of a series called <a href="/2022/01/10/type-safe-di-composition">Type-safe DI composition</a>. In the previous article, you saw <a href="/2022/02/21/a-type-safe-di-container-as-a-functor">a C# example of a type-safe DI Container as a functor</a>. In case it's not clear from the article that introduces the series, there's really no point to any of this. My motivation for writing the article is that readers sometimes ask me about topics such as DI Containers versus type safety, or DI Containers in functional programming. The goal of these articles is to make it painfully clear why I find such ideas moot. </p> <p> The previous article demonstrated how you can view a type-safe DI Container as a <a href="/2018/03/22/functors">functor</a>. The payload is a registry of services. You could introduce a specialised Parameter Object with each service given a name, but really, since <a href="https://ayende.com/blog/2886/building-an-ioc-container-in-15-lines-of-code">a DI container is little more than a dictionary keyed by type</a>, if you <a href="/2022/01/24/type-level-di-container-prototype">give each element of a tuple a unique type</a>, then a tuple of services is all you need. </p> <h3 id="47a88147593b4ef9a8721f4045b553ca"> Identity <a href="#47a88147593b4ef9a8721f4045b553ca" title="permalink">#</a> </h3> <p> In <a href="/2022/02/21/a-type-safe-di-container-as-a-functor">the previous article</a>, I asked the question: <em>Does <code>Container&lt;T&gt;</code> and its <code>Select</code> method form a lawful functor?</em> In other words, does it obey the functor laws? </p> <p> In the article, I just left the question dangling, so you might wonder if there's something fishy going on. In a sense, there is. </p> <p> The Container functor is simply the <a href="/2018/09/03/the-identity-functor">Identity functor</a> in disguise. Just imagine renaming <code>Container&lt;T&gt;</code> to <code>Identity&lt;T&gt;</code> and this should be clear. Since the Identity functor is lawful, the Container functor is also lawful. </p> <p> On the other hand, the Identity functor is redundant. It doesn't offer any additional capabilities over its payload. Anything you can do with the Identity functor's payload, you can do directly to the payload. An even stronger statement is that the Identity functor is isomorphic to its underlying payload. This is true also for <code>Container&lt;T&gt;</code>, which is isomorphic to <code>T</code>. This is clear from the definition of the class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Container</span>(T&nbsp;<span style="color:#1f377f;">item</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members&nbsp;to&nbsp;follow...</span></pre> </p> <p> You can convert <em>any</em> <code>T</code> to a <code>Container&lt;T&gt;</code> by calling the constructor, and you can convert <em>any</em> <code>Container&lt;T&gt;</code> to a <code>T</code> value by reading the <code>Item</code> property. </p> <p> Then why have the Container at all? </p> <h3 id="2ab798ff8d8c452d92d4f0c4da12516a"> Tuple as a DI Container <a href="#2ab798ff8d8c452d92d4f0c4da12516a" title="permalink">#</a> </h3> <p> In the previous article, we left off with a container defined as <code>Container&lt;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&gt;</code>. It follows that the type of the Container's <code>Item</code> property is <code>(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)</code> - a tuple with three elements. </p> <p> This code example makes use of C# <a href="https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/value-tuples">tuple types</a>, which enables you to give each element in the tuple a name. For the tuple in question, the first element is called <code>rdb</code>, the next element <code>clock</code>, and the third <code>repo</code>. This makes the code that accesses these elements more readable, but is structurally unimportant. If you work in a language without this tuple feature, it doesn't change the conclusion in this article series. </p> <p> Instead of relying on <code>Container&lt;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&gt;</code>, the <code>CompositionRoot</code> class can rely on the unwrapped tuple: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&nbsp;container; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CompositionRoot</span>( &nbsp;&nbsp;&nbsp;&nbsp;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&nbsp;<span style="color:#1f377f;">container</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.container&nbsp;=&nbsp;container; }</pre> </p> <p> Its <code>Create</code> method directly accesses the tuple elements to create Controllers: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;<span style="color:#74531f;">Create</span>(ControllerContext&nbsp;<span style="color:#1f377f;">context</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(context&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(context)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;context.ActionDescriptor.ControllerTypeInfo; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(CalendarController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;CalendarController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.rdb, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.repo); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(HomeController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;HomeController(container.rdb); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ReservationsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.clock, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.rdb, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.repo); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(RestaurantsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantsController(container.rdb); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ScheduleController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ScheduleController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.rdb, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.repo, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AccessControlList.FromUser(context.HttpContext.User)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Unexpected&nbsp;controller&nbsp;type:&nbsp;</span>{t}<span style="color:#a31515;">.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(context)); }</pre> </p> <p> Notice that instead of <code>container.Item.rdb</code>, <code>container.Item.repo</code>, and <code>container.Item.clock</code>, there's no redundant <code>Item</code> property. The <code>Create</code> method can directly access <code>container.rdb</code>, <code>container.repo</code>, and <code>container.clock</code>. </p> <h3 id="1714e2353b3349f7babbeea715058d5f"> Registration <a href="#1714e2353b3349f7babbeea715058d5f" title="permalink">#</a> </h3> <p> Not only did it become easier to <a href="/2010/09/29/TheRegisterResolveReleasepattern">resolve services</a> from the Container (i.e. the tuple) - registering services is also simpler: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">rdb</span>&nbsp;=&nbsp;CompositionRoot.CreateRestaurantDatabase(Configuration); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">po</span>&nbsp;=&nbsp;CompositionRoot.CreatePostOffice(Configuration,&nbsp;rdb); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">clock</span>&nbsp;=&nbsp;CompositionRoot.CreateClock(); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">repo</span>&nbsp;=&nbsp;CompositionRoot.CreateRepository(Configuration,&nbsp;po); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">container</span>&nbsp;=&nbsp;(rdb,&nbsp;clock,&nbsp;repo); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">compositionRoot</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompositionRoot(container); services.AddSingleton&lt;IControllerActivator&gt;(compositionRoot);</pre> </p> <p> Just create the services and put them in a tuple of the desired shape. No <em>query syntax</em> or other compiler tricks are required. Just write normal code. </p> <h3 id="ba588510ef1841aa8591f5885f4b04c5"> Conclusion <a href="#ba588510ef1841aa8591f5885f4b04c5" title="permalink">#</a> </h3> <p> The <code>Container&lt;T&gt;</code> class is just the Identity functor in disguise. It's redundant, so you can delete it and instead use a tuple, a record type, or a Parameter Object as your service registry. </p> <p> Are we done now, or can we simplify things even further? Read on. </p> <p> <strong>Next:</strong> <a href="/2022/03/14/type-safe-di-containers-are-redundant">Type-safe DI Containers are redundant</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Can one flatten a statically typed list? https://blog.ploeh.dk/2022/02/28/can-one-flatten-a-statically-typed-list 2022-02-28T06:58:00+00:00 Mark Seemann <div id="post"> <p> <em>What would be the type of a heterogeneously nested list?</em> </p> <p> Once again, I (inadvertently) managed to start a discussion on Twitter about static versus dynamic typing. I've <a href="/2021/08/09/am-i-stuck-in-a-local-maximum">touched on the topic before</a>, but my agenda is not to fan any flames - quite the contrary. I'm trying (nay, <em>struggling</em>) to <em>understand</em> the other side of the debate. </p> <p> This post isn't really about that, though, but in one of the threads spawned by my <a href="https://twitter.com/ploeh/status/1497261540445241350">original tweet</a>, <a href="https://twitter.com/tom_a_r_johnson">Tom Johnson</a> wrote: </p> <blockquote> <p> "One of my favourite examples: </p> <p> "What's the type of [[[3, 2], 7, [2, 4], 1], 0]? </p> <p> "What's the type of flatten, which takes that value and yields [3, 2, 7, 2, 4, 1, 0]?" </p> <footer><cite><a href="https://twitter.com/tom_a_r_johnson/status/1497384367156609027">Tom Johnson</a></cite></footer> </blockquote> <p> The type, it turns out, is straightforward. </p> <p> I'd like to be explicit about my agenda when writing this article. I'm not doing this to prove Tom Johnson wrong. In fact, I get the sense that he already knows the answer. Rather, I found it an interesting little exercise. </p> <h3 id="c37c2ce8c0154b8f9058ae47d8697ec8"> Syntax <a href="#c37c2ce8c0154b8f9058ae47d8697ec8" title="permalink">#</a> </h3> <p> First, we need to get one thing out of the way. In many languages, <code>[[[3, 2], 7, [2, 4], 1], 0]</code> isn't valid syntax. Even as a 'normal' list, syntax like <code>[3, 2, 7, 2, 4, 1, 0]</code> wouldn't be valid syntax in some languages. </p> <p> In <a href="https://www.haskell.org">Haskell</a> <code>[3, 2, 7, 2, 4, 1, 0]</code> would be valid, but it wouldn't be valid syntax in C#. And while it'd be valid syntax in <a href="https://fsharp.org">F#</a>, it wouldn't mean what it means in Haskell. The F# equivalent would be <code>[3; 2; 7; 2; 4; 1; 0]</code>. </p> <p> The point is that that I don't consider myself bound by the specific syntax <code>[[[3, 2], 7, [2, 4], 1], 0]</code>. I'm going to answer the question using Haskell, and in Haskell, that code doesn't parse. </p> <p> Instead, I'll use a data structure that represents the same data, but in a more verbose manner. </p> <h3 id="24df61de8afb498ab9d10f397e8d24f8"> Tree <a href="#24df61de8afb498ab9d10f397e8d24f8" title="permalink">#</a> </h3> <p> In one of the Twitter threads, <a href="https://lexi-lambda.github.io/">Alexis King</a> already gave the answer: </p> <blockquote> <p> "`flatten` is perfectly possible in Haskell, you just have to write a rose tree datatype, not use `[a]`." </p> <footer><cite><a href="https://twitter.com/lexi_lambda/status/1497339055629025283">Alexis King</a></cite></footer> </blockquote> <p> The standard <code>Tree a</code> in <a href="https://hackage.haskell.org/package/containers/docs/Data-Tree.html">Data.Tree</a>, however, doesn't seem to quite fit. Instead, we can use a <a href="https://en.wikipedia.org/wiki/Rose_tree">rose tree</a>. As Meertens wrote: </p> <blockquote> <p> "We consider trees whose internal nodes may fork into an arbitrary (natural) number of sub-trees. (If such a node has zero descendants, we still consider it internal.) Each external node carries a data item. No further information is stored in the tree; in particular, internal nodes are unlabelled." </p> <footer><cite><em>First Steps towards the Theory of Rose Trees</em>, Lambert Meertens, 1988</cite></footer> </blockquote> <p> With <em>unlabelled</em> nodes, we might draw Tom Johnson's 'list' like this: </p> <p> <img src="/content/binary/nested-list-as-tree.png" alt="A nested 'list' drawn as a tree."> </p> <p> I'll use the <code>Tree</code> data type introduced in the article <a href="/2019/09/09/picture-archivist-in-haskell">Picture archivist in Haskell</a>: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Tree&nbsp;a&nbsp;b&nbsp;=&nbsp;Node&nbsp;a&nbsp;[Tree&nbsp;a&nbsp;b]&nbsp;|&nbsp;Leaf&nbsp;b&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> If you would, you could define a type alias that has unlabelled internal nodes: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;MeertensTree&nbsp;=&nbsp;Tree&nbsp;<span style="color:blue;">()</span></pre> </p> <p> I'm not really going to bother doing that, though. I'm just going to create a <code>Tree () b</code> value. </p> <p> Here we go: </p> <p> <pre>&gt; l = Node () [Node () [Node () [Leaf 3, Leaf 2], Leaf 7, Node () [Leaf 2, Leaf 4], Leaf 1], Leaf 0] &gt; :t l l :: Num b =&gt; Tree () b</pre> </p> <p> You could probably come up with a more succinct syntax to create trees like this in code, but I'm assuming that in 'reality', if you have the need to flatten nested lists, it'll be a values that arrive at the boundary of the application... </p> <h3 id="9f83ac5fc78e47d0be64cadfe5d43f4c"> Flattening <a href="#9f83ac5fc78e47d0be64cadfe5d43f4c" title="permalink">#</a> </h3> <p> Can you flatten the 'list' <code>l</code>? </p> <p> Yes, no problem. The <code>Tree a b</code> data type is an instance of various type classes, among these <code>Foldable</code>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;(<span style="color:blue;">Tree</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;foldMap&nbsp;=&nbsp;bifoldMap&nbsp;mempty</pre> </p> <p> You can see the full definition of <code>bifoldMap</code> and its dependencies in <a href="/2019/09/09/picture-archivist-in-haskell">the article that introduces the type</a>. </p> <p> One of the functions available via <code>Foldable</code> is <code>toList</code>, which is just what you need: </p> <p> <pre>&gt; toList l [3,2,7,2,4,1,0]</pre> </p> <p> This is the result required by Tom Johnson. </p> <h3 id="4bf861c4bd5a49fbab9426fe55e8ec3f"> Summary <a href="#4bf861c4bd5a49fbab9426fe55e8ec3f" title="permalink">#</a> </h3> <p> In short, the answer to the question: <em>"What's the type of [[[3, 2], 7, [2, 4], 1], 0]?"</em> is: <code>Num b =&gt; Tree () b</code>, an example of which might be <code>Tree () Integer</code>. You could also call it <code>MeertensTree Integer</code>. </p> <p> Likewise, the type of the requested <code>flatten</code> function is <code>toList :: Foldable t =&gt; t a -&gt; [a]</code>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="c3ed1ef693ce4c5a85c612cdbbc85cd8"> <div class="comment-author">Michael Hensler <a href="#c3ed1ef693ce4c5a85c612cdbbc85cd8">#</a></div> <div class="comment-content"> <p> He appears to be using JavaScript and the solution is trivial in TypeScript, so he must not have looked too hard before claiming that statically typed languages cannot handle this scenario. He also kept moving the goalposts (It needs to use list syntax! It needs to support heterogeneous lists!), but even the most naïve attempt in TypeScript handles those as well. </p> <pre>type Nested&lt;T&gt; = (Nested&lt;T&gt; | T)[]; const flatten = &lt;T&gt;(val: Nested&lt;T&gt;): T[] => val.reduce( (r: T[], v) =&gt; [...r, ...(Array.isArray(v) ? flatten(v) : [v])], [] ); const nested = [[[3, 2], 7, [2, 4], 1], 0]; const result = flatten(nested); console.log(result); // [3, 2, 7, 2, 4, 1, 0] const silly: Nested&lt;number | string | { name: string }&gt; = [ [[3, 2], 1], "cheese", { name: "Fred" }, ]; const sillyResult = flatten(silly); console.log(sillyResult); // [3, 2, 1, "cheese", {"name": "Fred"}]</pre> <p> TypeScript types can also maintain the order of the types of elements in the flattened list. <a href="https://catchts.com/flatten">How to flatten a tuple type in TypeScript?</a> gives a solution capable of handling that, though it is certainly more complex than the version above. </p> </div> <div class="comment-date">2022-03-01 03:48 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A type-safe DI Container as a functor https://blog.ploeh.dk/2022/02/21/a-type-safe-di-container-as-a-functor 2022-02-21T06:37:00+00:00 Mark Seemann <div id="post"> <p> <em>Decoupling the registry of services from the container. An ongoing C# example.</em> </p> <p> This article is part of a series called <a href="/2022/01/10/type-safe-di-composition">Type-safe DI composition</a>. In the previous article, you saw <a href="/2022/02/07/nested-type-safe-di-containers">a C# example of a type-safe, nested DI Container</a>. In case it's not clear from the article that introduces the series, there's really no point to any of this. My motivation for writing the article is that readers sometimes ask me about topics such as DI Containers versus type safety, or DI Containers in functional programming. The goal of these articles is to make it painfully clear why I find such ideas moot. </p> <p> In the previous article, you saw how you can use container nesting to implement a type-safe container of arbitrary arity. You only need these generic containers: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>&gt; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;</pre> </p> <p> You can achieve arities higher than two by nesting containers. For example, you can <a href="/2010/09/29/TheRegisterResolveReleasepattern">register</a> three services by nesting one container inside another: <code>Container&lt;T1,&nbsp;Container&lt;T2,&nbsp;T3&gt;&gt;</code>. Higher arities require more nesting. See the <a href="/2022/02/07/nested-type-safe-di-containers">previous article</a> for an example with five services. </p> <h3 id="c0c1df1b436c49a79f1ea5aef2a9d251"> Decoupling the container from the registry <a href="#c0c1df1b436c49a79f1ea5aef2a9d251" title="permalink">#</a> </h3> <p> How did I get the idea of nesting the containers? </p> <p> It started as I was examining the constructor of the <code>CompositionRoot</code> class: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Container&lt;IConfiguration,&nbsp;Container&lt;Container&lt;Container&lt;IRestaurantDatabase,&nbsp;IPostOffice&gt;,&nbsp;IClock&gt;,&nbsp;IReservationsRepository&gt;&gt;&nbsp;container; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CompositionRoot</span>(Container&lt;IConfiguration,&nbsp;Container&lt;Container&lt;Container&lt;IRestaurantDatabase,&nbsp;IPostOffice&gt;,&nbsp;IClock&gt;,&nbsp;IReservationsRepository&gt;&gt;&nbsp;<span style="color:#1f377f;">container</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.container&nbsp;=&nbsp;container; }</pre> </p> <p> Those are, indeed, wide lines of code. No <a href="/2019/11/04/the-80-24-rule">80x24 box</a> here. If you ignore all the container nesting, you can tell that the container contains five services: <ul> <li><code>IConfiguration</code></li> <li><code>IRestaurantDatabase</code></li> <li><code>IPostOffice</code></li> <li><code>IClock</code></li> <li><code>IReservationsRepository</code></li> </ul> What may not be immediately clear, however, is that only three of those are required by <code>CompositionRoot</code>: <ul> <li><code>IRestaurantDatabase</code></li> <li><code>IClock</code></li> <li><code>IReservationsRepository</code></li> </ul> The <code>IConfiguration</code> and <code>IPostOffice</code> services are only present because they were required to create some of the other services. <code>CompositionRoot</code> never directly uses these two services. In other words, there's a leaky abstraction somewhere. </p> <p> It'd be more in spirit with the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> (DIP) and the <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> (ISP) if the constructor required only the services it needs. </p> <p> On the other hand, it's easiest to create the container from its constituent elements if you can pass already-registered services along when registering other services: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">container</span>&nbsp;=&nbsp;Container.Empty &nbsp;&nbsp;&nbsp;&nbsp;.Register(Configuration) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreateRestaurantDatabase) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreatePostOffice) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreateClock()) &nbsp;&nbsp;&nbsp;&nbsp;.Register((<span style="color:#1f377f;">conf</span>,&nbsp;<span style="color:#1f377f;">cont</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompositionRoot.CreateRepository(conf,&nbsp;cont.Item1.Item2)); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">compositionRoot</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompositionRoot(container); services.AddSingleton&lt;IControllerActivator&gt;(compositionRoot);</pre> </p> <p> I realised that the container needed a way to remove services from the public API. And it hit me that it'd be easier if I separated the container from its registry of services. If I did that, I could define a <em>single</em> generic <code>Container&lt;T&gt;</code> and even make it a <a href="/2018/03/22/functors">functor</a>. The payload could be a custom Parameter Object, but could also just be a tuple of services. </p> <p> From <a href="https://thinkingwithtypes.com">Thinking with Types</a> I knew that you can reduce all <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> to canonical representations of <a href="/2018/06/11/church-encoded-either">Eithers</a> and tuples. </p> <p> A triple, for example, can be modelled as a <code>Tuple&lt;T1, Tuple&lt;T2, T3&gt;&gt;</code> or <code>Tuple&lt;Tuple&lt;T1, T2&gt;, T3&gt;</code>, a quadruple as e.g. <code>Tuple&lt;Tuple&lt;T1, T2&gt;, Tuple&lt;T3, T4&gt;&gt;</code>, etcetera. </p> <p> Connecting those dots, I also realised that as an intermediary step I could nest the containers. It's really unnecessary, though, so let's proceed to simplify the code. </p> <h3 id="6e6880e3ca98497d87473981d084d43f"> A Container functor <a href="#6e6880e3ca98497d87473981d084d43f" title="permalink">#</a> </h3> <p> Instead of a <code>Container&lt;T1, T2&gt;</code> in addition to a <code>Container&lt;T1&gt;</code>, you only need the single-arity container: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Container</span>(T&nbsp;<span style="color:#1f377f;">item</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members&nbsp;to&nbsp;follow...</span></pre> </p> <p> Apart from overriding <code>Equals</code> and <code>GetHashCode</code>, the only member is <code>Select</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Container&lt;TResult&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(Func&lt;T,&nbsp;TResult&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(selector&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(selector)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Container&lt;TResult&gt;(selector(Item)); }</pre> </p> <p> This is a straightforward <code>Select</code> implementation, making <code>Container&lt;T&gt;</code> a functor. </p> <h3 id="140bf408620c4c9aa3bd2d4480f333a2"> Usage <a href="#140bf408620c4c9aa3bd2d4480f333a2" title="permalink">#</a> </h3> <p> You can use the <code>Select</code> method to incrementally build the desired container: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">container</span>&nbsp;=&nbsp;Container.Empty.Register(Configuration) &nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:#1f377f;">conf</span>&nbsp;=&gt;&nbsp;(conf,&nbsp;rdb:&nbsp;CompositionRoot.CreateRestaurantDatabase(conf))) &nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;(t.conf,&nbsp;t.rdb,&nbsp;po:&nbsp;CompositionRoot.CreatePostOffice(t.conf,&nbsp;t.rdb))) &nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;(t.conf,&nbsp;t.rdb,&nbsp;t.po,&nbsp;clock:&nbsp;CompositionRoot.CreateClock())) &nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;(t.conf,&nbsp;t.rdb,&nbsp;t.po,&nbsp;t.clock,&nbsp;repo:&nbsp;CompositionRoot.CreateRepository(t.conf,&nbsp;t.po))); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">compositionRoot</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompositionRoot(container); services.AddSingleton&lt;IControllerActivator&gt;(compositionRoot);</pre> </p> <p> The syntax is awkward, since you need to pass a tuple along to the next step, in which you need to access each of the tuple's elements, only to create a bigger tuple, and so on. </p> <p> While it does get the job done, we can do better. </p> <h3 id="176a52591fd8439e90e747ee96edc695"> Query syntax <a href="#176a52591fd8439e90e747ee96edc695" title="permalink">#</a> </h3> <p> Instead of using method-call syntax to chain all these service registrations together, you can take advantage of C#'s <em>query syntax</em> which lights up for any functor: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">container</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;conf&nbsp;<span style="color:blue;">in</span>&nbsp;Container.Empty.Register(Configuration) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rdb&nbsp;=&nbsp;CompositionRoot.CreateRestaurantDatabase(conf) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;po&nbsp;=&nbsp;CompositionRoot.CreatePostOffice(conf,&nbsp;rdb) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;clock&nbsp;=&nbsp;CompositionRoot.CreateClock() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;repo&nbsp;=&nbsp;CompositionRoot.CreateRepository(conf,&nbsp;po) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(conf,&nbsp;rdb,&nbsp;po,&nbsp;clock,&nbsp;repo); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">compositionRoot</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompositionRoot(container); services.AddSingleton&lt;IControllerActivator&gt;(compositionRoot);</pre> </p> <p> In both cases, the <code>container</code> object has the type <code>Container&lt;(IConfiguration&nbsp;conf,&nbsp;IRestaurantDatabase&nbsp;rdb,&nbsp;IPostOffice&nbsp;po,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&gt;</code>, which is still quite the mouthful. Now, however, we can do something about it. </p> <h3 id="dea37fc7697046bc8d8c6ba881d34890"> DIP and ISP applied <a href="#dea37fc7697046bc8d8c6ba881d34890" title="permalink">#</a> </h3> <p> The <code>CompositionRoot</code> class only needs three services, so its constructor should ask for those, and no more: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Container&lt;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&gt; &nbsp;&nbsp;&nbsp;&nbsp;container; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CompositionRoot</span>( &nbsp;&nbsp;&nbsp;&nbsp;Container&lt;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&gt;&nbsp;<span style="color:#1f377f;">container</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.container&nbsp;=&nbsp;container; }</pre> </p> <p> With that injected <code>container</code> it can implement <code>Create</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;<span style="color:#74531f;">Create</span>(ControllerContext&nbsp;<span style="color:#1f377f;">context</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(context&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(context)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;context.ActionDescriptor.ControllerTypeInfo; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(CalendarController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;CalendarController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item.rdb, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item.repo); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(HomeController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;HomeController(container.Item.rdb); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ReservationsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item.clock, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item.rdb, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item.repo); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(RestaurantsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantsController(container.Item.rdb); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ScheduleController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ScheduleController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item.rdb, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item.repo, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AccessControlList.FromUser(context.HttpContext.User)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Unexpected&nbsp;controller&nbsp;type:&nbsp;</span>{t}<span style="color:#a31515;">.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(context)); }</pre> </p> <p> That's more readable, although the intermediary <code>Item</code> object doesn't seem to do much work... </p> <p> You can create a <code>container</code> with the desired type like this: </p> <p> <pre>Container&lt;(IRestaurantDatabase&nbsp;rdb,&nbsp;IClock&nbsp;clock,&nbsp;IReservationsRepository&nbsp;repo)&gt;&nbsp;<span style="color:#1f377f;">container</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;conf&nbsp;<span style="color:blue;">in</span>&nbsp;Container.Empty.Register(Configuration) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rdb&nbsp;=&nbsp;CompositionRoot.CreateRestaurantDatabase(conf) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;po&nbsp;=&nbsp;CompositionRoot.CreatePostOffice(conf,&nbsp;rdb) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;clock&nbsp;=&nbsp;CompositionRoot.CreateClock() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;repo&nbsp;=&nbsp;CompositionRoot.CreateRepository(conf,&nbsp;po) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;(rdb,&nbsp;clock,&nbsp;repo); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">compositionRoot</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompositionRoot(container); services.AddSingleton&lt;IControllerActivator&gt;(compositionRoot);</pre> </p> <p> Notice that the only difference compared to earlier is that the <code>select</code> expression only involves the three required services: <code>rdb</code>, <code>clock</code>, and <code>repo</code>. </p> <h3 id="0cf421420e044b6fb66292689f987d13"> Conclusion <a href="#0cf421420e044b6fb66292689f987d13" title="permalink">#</a> </h3> <p> This seems cleaner than before, but perhaps you're left with a nagging doubt: Why is the <code>Container&lt;T&gt;</code> class even required? What value does its <code>Item</code> property provide? </p> <p> And perhaps another question is also in order: Does the <code>Select</code> method shown even define a lawful functor? </p> <p> Read on. </p> <p> <strong>Next:</strong> <a href="/2022/03/07/a-type-safe-di-container-as-a-tuple">A type-safe DI Container as a tuple</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A conditional sandwich example https://blog.ploeh.dk/2022/02/14/a-conditional-sandwich-example 2022-02-14T07:44:00+00:00 Mark Seemann <div id="post"> <p> <em>An F# example of reducing a workflow to an impureim sandwich.</em> </p> <p> The most common reaction to the <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a> architecture (also known as <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">functional core, imperative shell</a>) is one of incredulity. How does this way of organising code generalise to arbitrary complexity? </p> <p> The short answer is that it doesn't. Given <em>sufficient complexity</em>, you may not be able to 1. gather all data with impure queries, 2. call a pure function, and 3. apply the return value via impure actions. The question is: How much complexity is required before you have to give up on the impureim sandwich? </p> <p> <img src="/content/binary/sandwich-transition-free.png" alt="Axis with sandwich to the left, free monad to the right, and a tentative transition zone in between, but biased to the right."> </p> <p> There's probably a fuzzy transition zone where the sandwich may still apply, but where it begins to be questionable whether it's beneficial. In my experience, this transition seems to lie further to the right than most people think. </p> <p> Once you have to give up on the impureim sandwich, in functional programming you may resort to using <a href="/2017/08/07/f-free-monad-recipe">free monads</a>. In object-oriented programming, you may use <a href="/dippp">Dependency Injection</a>. Depending on language and paradigm, still more options may be available. </p> <p> My experience is mostly with web-based systems, but in that context, I find that a surprisingly large proportion of problems can be rephrased and organised in such a way that the impureim sandwich architecture applies. Actually, I surmise that <em>most</em> problems can be addressed in that way. </p> <p> I am, however, often looking for good examples. As I wrote in <a href="/2017/02/02/dependency-rejection#36c724b49f614104842c47909cd9c916">a comment</a> to <a href="/2017/02/02/dependency-rejection">Dependency rejection</a>: </p> <blockquote> <p> "I'd welcome a simplified, but still concrete example where the impure/pure/impure sandwich described here isn't going to be possible." </p> </blockquote> <p> Such examples are, unfortunately, rare. While real production code may seem like an endless supply of examples, production code often contains irrelevant details that obscure the essence of the case. Additionally, production code is often proprietary, so I can't share it. </p> <p> In 2019 <a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> kindly supplied an example problem that <a href="/2019/12/02/refactoring-registration-flow-to-functional-architecture">I could refactor</a>. Since then, there's been a dearth of more examples. Until now. </p> <p> I recently ran into another fine example of a decision flow that at first glance seemed a poor fit for the <em>functional core, imperative shell</em> architecture. What follows is, actually, production code, here reproduced with the kind permission of <a href="https://www.criipto.com/">Criipto</a>. </p> <h3 id="e90c1b9373854c988f5c7d4fe21f90ee"> Create a user if it doesn't exist <a href="#e90c1b9373854c988f5c7d4fe21f90ee" title="permalink">#</a> </h3> <p> As I've <a href="/2022/01/03/to-id-or-not-to-id">previously touched on</a>, I'm helping Criipto integrate with the <a href="https://fusebit.io/docs/reference/fusebit-http-api/">Fusebit API</a>. This API has a user model where you can create users in the Fusebit services. Once you've created a user, a client can log on as that user and access the resources available to her, him, or it. There's an underlying security model that controls all of that. </p> <p> Criipto's users may not all be provisioned as users in the Fusebit API. If they need to use the Fusebit system, we'll provision them just in time. On the other hand, there's no reason to create the user if it already exists. </p> <p> But it gets more complicated than that. To fit our requirements, the user must have an associated <em>issuer</em>. This is another Fusebit resource that we may have to provision if it doesn't already exist. </p> <p> The desired logic may be easier to follow if visualised as a flowchart. </p> <p> <img src="/content/binary/flowchart-to-create-fusebit-user.png" alt="Flowchart that illustrates how to provision a Fusebit user."> </p> <p> The user must have an <em>issuer</em>, but an appropriate issuer may already exist. If it doesn't, we must create the issuer before we create the user. </p> <p> At first blush this seems like a workflow that doesn't fit the impureim sandwich architecture. After all, you should only check for the existence of the issuer if you find that the user doesn't exist. There's a decision between the first and second impure query. </p> <p> Can we resolve this problem and implement the functionality as an impureim sandwich? </p> <h3 id="26f6a40a12534d349f556fb67c41e945"> Speculative prefetching <a href="#26f6a40a12534d349f556fb67c41e945" title="permalink">#</a> </h3> <p> When looking for ways to apply the <em>functional core, imperative shell</em> architecture, it often pays to take a step back and look at a slightly larger picture. Another way to put it is that you should think less procedurally, and more declaratively. A flowchart like the above is essentially procedural. It may prevent you from seeing other opportunities. </p> <p> One of the reasons I like functional programming is that it forces me to think in a more declarative way. This helps me identify better abstractions than I might otherwise be able to think of. </p> <p> The above flowchart is representative of the most common counterargument I hear: The impureim sandwich doesn't work if the code has to make a decision about a secondary query based on the result of an initial query. This is also what's at stake here. The result of the <em>user exists</em> query determines whether the program should query about the issuer. </p> <p> The assumption is that since the user is supposed to have an issuer, if the user exists, the issuer must also exist. </p> <p> Even so, would it hurt so much to query the Fusebit API up front about the issuer? </p> <p> Perhaps you react to such a suggestion with distaste. After all, it seems wasteful. Why query a web service if you don't need the result? And what about performance? </p> <p> Whether or not this is wasteful depends on what kind of waste you measure. If you measure bits transmitted over the network, then yes, you may see this measure increase. </p> <p> It may not be as bad as you think, though. Perhaps the HTTP <code>GET</code> request you're about to make has a cacheable result. Perhaps the result is already waiting in your proxy server's RAM. </p> <p> Neither the Fusebit HTTP API's <em>user</em> resources nor its <em>issuer</em> resources, however, come with cache headers, so this last argument doesn't apply here. I still included it above because it's worth taking into account. </p> <p> Another typical performance consideration is that this kind of potentially redundant traffic will degrade performance. Perhaps. As usual, if that's a concern: measure. </p> <p> Querying the API whether a user exists is independent of the query to check if an issuer exists. This means that you could perform the two queries in parallel. Depending on the total load on the system, the difference between one HTTP request and two concurrent requests may be negligible. (It could still impact overall system performance if the system is already running close to capacity, so this isn't always a good strategy. Often, however, it's not really an issue.) </p> <p> A third consideration is the statistical distribution of pathways through the system. If you consider the flowchart above, it indicates a <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of <em>3</em>; there are three distinct pathways. </p> <p> If, however, it turns out that in 95 percent of cases the user <em>doesn't</em> exist, you're going to have to perform the second query (for issuer) anyway, then the difference between prefetching and conditional querying is minimal. </p> <p> While some would consider this 'cheating', when aiming for the impureim sandwich architecture, these are all relevant questions to ponder. It often turns out that you <em>can</em> fetch all the data before passing them to a pure function. It may entail 'wasting' some electrons on queries that turn out to be unnecessary, but it may still be worth doing. </p> <p> There's another kind of waste worth considering. This is the waste in <em>developer hours</em> if you write code that's harder to maintain than it has to be. As I recently described in an article titled <a href="https://stackoverflow.blog/2022/01/03/favor-real-dependencies-for-unit-testing/">Favor real dependencies for unit testing</a>, the more you use functional programming, the less test maintenance you'll have. </p> <p> Keep in mind that the scenario in this article is a server-to-server interaction. How much would a bit of extra bandwidth cost, versus wasted programmer hours? </p> <p> If you can substantially simplify the code at the cost of a few dollars of hardware or network infrastructure, it's often a good trade-off. </p> <h3 id="284c71bc67ef4958b855f57b892fd842"> Referential integrity <a href="#284c71bc67ef4958b855f57b892fd842" title="permalink">#</a> </h3> <p> The above flowchart implies a more subtle assumption that turns out to not hold in practice. The assumption is that all users in the system have been created the same: that all users are associated with an issuer. Thus, according to this assumption, if the user exists, then so must the issuer. </p> <p> This turns out to be a false assumption. The Fusebit HTTP API doesn't enforce <a href="https://en.wikipedia.org/wiki/Referential_integrity">referential integrity</a>. You can create a user with an issuer that doesn't exist. When creating a user, you supply only the issuer ID (a string), but the API doesn't check that an issuer with that ID exists. </p> <p> Thus, just because a user exists you can't be sure that its associated issuer exists. To be sure, you'd have to check. </p> <p> But this means that you'll have to perform two queries after all. The angst from the previous section turns out to be irrelevant. The flowchart is wrong. </p> <p> Instead, you have two independent, but potentially parallelisable, processes: </p> <p> <img src="/content/binary/parallel-flowchart-to-create-fusebit-user-and-issuer.png" alt="Flowchart showing the actual parallel nature of Fusebit user provisioning."> </p> <p> You can't always be that lucky when you consider how to make requirements fit the impureim sandwich mould, but this is beginning to look really promising. Each of these two processes is near-trivial. </p> <h3 id="6622f662e19243a198475bde42d624bd"> Idempotence <a href="#6622f662e19243a198475bde42d624bd" title="permalink">#</a> </h3> <p> Really, what's actually required is an <a href="https://en.wikipedia.org/wiki/Idempotence">idempotent</a> <em>create</em> action. As the <a href="/ref/rest-cookbook">RESTful Web Services Cookbook</a> describes, all HTTP verbs <em>except</em> <code>POST</code> should be regarded as idempotent in a well-designed API. Alas, creating users and issuers are (naturally) done with <code>POST</code> requests, so these operations aren't naturally idempotent. </p> <p> In functional programming you often <a href="/2016/09/26/decoupling-decisions-from-effects">decouple decisions from effects</a>. In order to be able to do that here, I created this <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/discriminated-unions">discriminated union</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Idempotent&lt;&#39;a&gt;&nbsp;=&nbsp;UpToDate&nbsp;|&nbsp;Update&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> This type is isomorphic to <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/options">option</a>, but I found it worthwhile to introduce a distinct type for this particular purpose. Usually, if a query returns, say <code>UserData option</code>, you'd interpret the <code>Some</code> case as indicating that the user exists, and the <code>None</code> case as indicating that the user doesn't exist. </p> <p> Here, I wanted the 'populated' case to indicate that an <code>Update</code> action is required. If I'd used <code>option</code>, then I would have had to map the user-doesn't-exist case to a <code>Some</code> value, and the user-exists case to a <code>None</code> value. I though that this might be confusing to other programmers, since it'd go against the usual <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> use of the type. </p> <p> That's the reason I created a custom type. </p> <p> The <code>UpToDate</code> case indicates that the value exists and is up to date. The <code>Update</code> case is worded in the imperative to indicate that the value (of type <code>'a</code>) should be updated. </p> <h3 id="6c8043d3335840919df68562d57a91e5"> Establish <a href="#6c8043d3335840919df68562d57a91e5" title="permalink">#</a> </h3> <p> The purpose of this entire exercise is to <em>establish</em> that a user (and issuer) exists. It's okay if the user already exists, but if it doesn't, we should create it. </p> <p> I mulled over the terminology and liked the verb <em>establish</em>, to the consternation of many Twitter users. </p> <blockquote> <p> CreateUserIfNotExists is a crude name 🤢 </p> <p> How about EstablishUser instead? </p> <p> "Establish" can both mean to "set up on a firm or permanent basis" and "show (something) to be true or certain by determining the facts". That seems to say the same in a more succinct way 👌 </p> <footer><cite><a href="https://twitter.com/ploeh/status/1471123230203199491">Me, on Twitter</a></cite></footer> </blockquote> <p> Just read the comments to see how divisive that little idea is. Still, I decided to define a function called <code>establish</code> to convert a Boolean and an <code>'a</code> value to an <code>Idempotent&lt;&#39;a&gt;</code> value. </p> <p> Don't forget the purpose of this entire exercise. The benefit that the impureim sandwich architecture can bring is that it enables you to drain the impure parts of the sandwich of logic. <a href="/2015/05/07/functional-design-is-intrinsically-testable">Pure functions are intrinsically testable</a>, so the more you define decisions and algorithms as pure functions, the more testable the code will be. </p> <p> It's even better when you can make the testable functions generic, because reusable functions has the potential to reduce cognitive load. Once a reader learns and understands an abstraction, it stops being much of a cognitive load. </p> <p> The functions to create and manipulate <code>Idempotent&lt;&#39;a&gt;</code> values should be covered by automated tests. The behaviour is quite trivial, though, so to increase coverage we can write the tests as properties. The code base in question already uses <a href="https://fscheck.github.io/FsCheck/">FsCheck</a>, so I just went with that: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Idempotent.establish&nbsp;returns&nbsp;UpToDate``&nbsp;(x&nbsp;:&nbsp;int)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Idempotent.establish&nbsp;x&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;UpToDate&nbsp;=!&nbsp;actual [&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Idempotent.establish&nbsp;returns&nbsp;Update``&nbsp;(x&nbsp;:&nbsp;string)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Idempotent.establish&nbsp;x&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;Update&nbsp;x&nbsp;=!&nbsp;actual</pre> </p> <p> These two properties also use <a href="https://github.com/SwensenSoftware/Unquote">Unquote</a> for assertions. The <code>=!</code> operator means <em>should equal</em>, so you can read an expression like <code>UpToDate&nbsp;=!&nbsp;actual</code> as <em>UpToDate should equal actual</em>. </p> <p> This describes the entire behaviour of the <code>establish</code> function, which is implemented this way: </p> <p> <pre><span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;bool&nbsp;-&gt;&nbsp;Idempotent&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;establish&nbsp;x&nbsp;isUpToDate&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;isUpToDate&nbsp;<span style="color:blue;">then</span>&nbsp;UpToDate&nbsp;<span style="color:blue;">else</span>&nbsp;Update&nbsp;x</pre> </p> <p> About as trivial as it can be. Unsurprising code is good. </p> <h3 id="af0781e5c14f4044bb46a885c7960870"> Fold <a href="#af0781e5c14f4044bb46a885c7960870" title="permalink">#</a> </h3> <p> The <code>establish</code> function affords a way to create <code>Idempotent</code> values. It'll also be useful with a function to <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">get the value out of the container</a>, so to speak. While you can always <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/pattern-matching">pattern match</a> on an <code>Idempotent</code> value, that'd introduce decision logic into the code that does that. </p> <p> The goal is to cover as much decision logic as possible by tests so that we can leave the overall impureim sandwich as an untested declarative composition - a <a href="http://xunitpatterns.com/Humble%20Object.html">Humble Object</a>, if you will. It'd be appropriate to introduce a reusable function (covered by tests) that can fulfil that role. </p> <p> We need the so-called <em>case analysis</em> of <code>Idempotent&lt;&#39;a&gt;</code>. In other terminology, this is also known as the <a href="/2019/04/29/catamorphisms">catamorphism</a>. Since <code>Idempotent&lt;&#39;a&gt;</code> is isomorphic to <code>option</code> (also known as <em>Maybe</em>), the catamorphism is also isomorphic to the <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>. While we expect no surprises, we can still cover the function with automated tests: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Idempotent.fold&nbsp;when&nbsp;up-to-date``&nbsp;(expected&nbsp;:&nbsp;DateTimeOffset)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Idempotent.fold&nbsp;(<span style="color:blue;">fun</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;DateTimeOffset.MinValue)&nbsp;expected&nbsp;UpToDate &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual [&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Idempotent.fold&nbsp;when&nbsp;update&nbsp;required``&nbsp;(x&nbsp;:&nbsp;TimeSpan)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;f&nbsp;(ts&nbsp;:&nbsp;TimeSpan)&nbsp;=&nbsp;ts.TotalHours&nbsp;+&nbsp;float&nbsp;ts.Minutes &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Update&nbsp;x&nbsp;|&gt;&nbsp;Idempotent.fold&nbsp;f&nbsp;1.1 &nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;x&nbsp;=!&nbsp;actual</pre> </p> <p> The most common catamorphisms are idiomatically called <code>fold</code> in F#, so that's what I called it as well. </p> <p> The first property states that when the <code>Idempotent</code> value is already <code>UpToDate</code>, <code>fold</code> simply returns the 'fallback value' (here called <code>expected</code>) and the function doesn't run. </p> <p> When the <code>Idempotent</code> is an <code>Update</code> value, the function <code>f</code> runs over the contained value <code>x</code>. </p> <p> The implementation hardly comes as a surprise: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;Idempotent&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;b</span> <span style="color:blue;">let</span>&nbsp;fold&nbsp;f&nbsp;onUpToDate&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;UpToDate&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;onUpToDate &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Update&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;x</pre> </p> <p> Both <code>establish</code> and <code>fold</code> are general-purpose functions. I needed one more specialised function before I could compose a workflow to create a Fusebit user if it doesn't exist. </p> <h3 id="d0dada3f342f42df93197f777ac94712"> Checking whether an issuer exists <a href="#d0dada3f342f42df93197f777ac94712" title="permalink">#</a> </h3> <p> As I've <a href="/2022/01/03/to-id-or-not-to-id">previously mentioned</a>, I'd already developed a set of modules to interact with the Fusebit API. One of these was a function to read an issuer. This <code>Issuer.get</code> action returns a <code>Task&lt;Result&lt;IssuerData, HttpResponseMessage&gt;&gt;</code>. </p> <p> The <code>Result</code> value will only be an <code>Ok</code> value if the issuer exists, but we can't conclude that any <code>Error</code> value indicates a missing resource. An <code>Error</code> may also indicate a genuine HTTP error. </p> <p> A function to translate a <code>Result&lt;IssuerData, HttpResponseMessage&gt;</code> value to a <code>Result&lt;bool, HttpResponseMessage&gt;</code> by examining the <code>HttpResponseMessage</code> is just complex enough (cyclomatic complexity <em>3</em>) to warrant unit test coverage. Here I just went with some parametrised tests rather than FsCheck properties. </p> <p> The first test asserts that when the result is <code>Ok</code> it translates to <code>Ok true</code>: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData&nbsp;(<span style="color:#a31515;">&quot;https://example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;DN&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;https://example.net&quot;</span>)&gt;] [&lt;InlineData&nbsp;(<span style="color:#a31515;">&quot;https://example.org/id&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;lga&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;https://example.gov/jwks&quot;</span>)&gt;] [&lt;InlineData&nbsp;(<span style="color:#a31515;">&quot;https://example.com/id&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;https://example.org/.jwks&quot;</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Issuer&nbsp;exists``&nbsp;iid&nbsp;dn&nbsp;jwks&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;issuer&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;Uri&nbsp;iid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DisplayName&nbsp;=&nbsp;dn&nbsp;|&gt;&nbsp;Option.ofObj &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PKA&nbsp;=&nbsp;JwksEndpoint&nbsp;(Uri&nbsp;jwks) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;result&nbsp;=&nbsp;Ok&nbsp;issuer &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Fusebit.issuerExists&nbsp;result &nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;<span style="color:blue;">true</span>&nbsp;=!&nbsp;actual</pre> </p> <p> All tests here are structured according to the <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">AAA formatting heuristic</a>. This particular test may seem so obvious that you may wonder how there's actually any logic to test. Perhaps the next test throws a little more light on that question: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Issuer&nbsp;doesn&#39;t&nbsp;exist``&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;resp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpResponseMessage&nbsp;(HttpStatusCode.NotFound) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;result&nbsp;=&nbsp;Error&nbsp;resp &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Fusebit.issuerExists&nbsp;result &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;<span style="color:blue;">false</span>&nbsp;=!&nbsp;actual</pre> </p> <p> How do we know that the requested issuer doesn't exist? It's not just any <code>Error</code> result that indicates that, but a particular <code>404 Not Found</code> result. Notice that this particular <code>Error</code> result translates to an <code>Ok</code> result: <code>Ok false</code>. </p> <p> All other kinds of <code>Error</code> results, on the other hand, should remain <code>Error</code> values: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData&nbsp;(HttpStatusCode.BadRequest)&gt;] [&lt;InlineData&nbsp;(HttpStatusCode.Unauthorized)&gt;] [&lt;InlineData&nbsp;(HttpStatusCode.Forbidden)&gt;] [&lt;InlineData&nbsp;(HttpStatusCode.InternalServerError)&gt;] <span style="color:blue;">let</span>&nbsp;``Issuer&nbsp;error``&nbsp;statusCode&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;resp&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpResponseMessage&nbsp;(statusCode) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;Error&nbsp;resp &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Fusebit.issuerExists&nbsp;expected &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> All together, these tests indicate an implementation like this: </p> <p> <pre><span style="color:green;">//&nbsp;Result&lt;&#39;a,&nbsp;HttpResponseMessage&gt;&nbsp;-&gt;&nbsp;Result&lt;bool,&nbsp;HttpResponseMessage&gt;</span> <span style="color:blue;">let</span>&nbsp;issuerExists&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Ok&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Error&nbsp;(resp&nbsp;:&nbsp;HttpResponseMessage)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;resp.StatusCode&nbsp;=&nbsp;HttpStatusCode.NotFound &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;Ok&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;Error&nbsp;resp</pre> </p> <p> Once again, I've managed to write a function more generic than its name implies. This seems to happen to me a lot. </p> <p> In this context, what matters more is that this is another <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> - which also explains why it was so easy to unit test. </p> <h3 id="ea0ca01fa88143e3adb23c5d883b6125"> Composition <a href="#ea0ca01fa88143e3adb23c5d883b6125" title="permalink">#</a> </h3> <p> It turned out that I'd now managed to extract all complexity to pure, testable functions. What remained was composing them together. </p> <p> First, a couple of private helper functions: </p> <p> <pre><span style="color:green;">//&nbsp;Task&lt;Result&lt;&#39;a,&nbsp;&#39;b&gt;&gt;&nbsp;-&gt;&nbsp;Task&lt;Result&lt;unit,&nbsp;&#39;b&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;ignoreOk&nbsp;x&nbsp;=&nbsp;TaskResult.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;())&nbsp;x <span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Task&lt;Result&lt;&#39;b,&nbsp;&#39;c&gt;&gt;)&nbsp;-&gt;&nbsp;Idempotent&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Task&lt;Result&lt;unit,&nbsp;&#39;c&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;whenMissing&nbsp;f&nbsp;=&nbsp;Idempotent.fold&nbsp;(f&nbsp;&gt;&gt;&nbsp;ignoreOk)&nbsp;(task&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;Ok&nbsp;()&nbsp;})</pre> </p> <p> These only exist to make the ensuing composition more readable. Since they both have a cyclomatic complexity of <em>1</em>, I found that <a href="/2018/11/12/what-to-test-and-not-to-test">it was okay to skip unit testing</a>. </p> <p> The same is true for the final composition: </p> <p> <pre><span style="color:blue;">let!</span>&nbsp;comp&nbsp;=&nbsp;taskResult&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(issuer,&nbsp;identity,&nbsp;user)&nbsp;=&nbsp;gatherData&nbsp;dto &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;issuerExists&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Issuer.get&nbsp;client&nbsp;issuer.Id&nbsp;|&gt;&nbsp;Task.map&nbsp;Fusebit.issuerExists &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;userExists&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User.find&nbsp;client&nbsp;(IdentityCriterion&nbsp;identity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;TaskResult.map&nbsp;(not&nbsp;&lt;&lt;&nbsp;List.isEmpty) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;Idempotent.establish&nbsp;issuer&nbsp;issuerExists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;whenMissing&nbsp;(Issuer.create&nbsp;client) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;Idempotent.establish&nbsp;user&nbsp;userExists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;whenMissing&nbsp;(User.create&nbsp;client)&nbsp;}</pre> </p> <p> The <code>comp</code> composition starts by gathering data from an incoming <code>dto</code> value. This code snippet is part of a slightly larger Controller Action that I'm not showing here. The rest of the surrounding method is irrelevant to the present example, since it only deals with translation of the input <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a> and from <code>comp</code> back to an <code>IHttpActionResult</code> object. </p> <p> After a little pure hors d'œuvre the sandwich arrives with the first impure actions: Retrieving the <code>issuerExists</code> and <code>userExists</code> values from the Fusebit API. After that, the sandwich does fall apart a bit, I admit. Perhaps it's more like a piece of <a href="https://en.wikipedia.org/wiki/Sm%C3%B8rrebr%C3%B8d">smørrebrød</a>... </p> <p> I could have written this composition with a more explicit sandwich structure, starting by exclusively calling <code>Issuer.get</code> and <code>User.find</code>. That would have been the first impure layer of the sandwich. </p> <p> As the pure centre, I could then have composed a pure function from <code>Fusebit.issuerExists</code>, <code>not &lt;&lt; List.isEmpty</code> and <code>Idempotent.establish</code>. </p> <p> Finally, I could have completed the sandwich with the second impure layer that'd call <code>whenMissing</code>. </p> <p> I admit that I didn't actually structure the code exactly like that. I mixed some of the pure functions (<code>Fusebit.issuerExists</code> and <code>not &lt;&lt; List.isEmpty</code>) with the initial queries by adding them as continuations with <code>Task.map</code> and <code>TaskResult.map</code>. Likewise, I decided to immediately pipe the results of <code>Idempotent.establish</code> to <code>whenMissing</code>. My motivation was that this made the code more readable, since I wanted to highlight the symmetry of the two actions. That mattered more to me, as a writer, than highlighting any sandwich structure. </p> <p> I'm not insisting I was right in making that choice. Perhaps I was; perhaps not. I'm only reporting what motivated me. </p> <p> Could the code be further improved? I wouldn't be surprised, but at this time I felt that it was good enough to submit to a code review, which it survived. </p> <p> One possible improvement, however, might be to parallelise the two actions, so that they could execute concurrently. I'm not sure it's worth the (small?) effort, though. </p> <h3 id="328d685313fd4141b3c9ea8f03e4c944"> Conclusion <a href="#328d685313fd4141b3c9ea8f03e4c944" title="permalink">#</a> </h3> <p> I'm always keen on examples that challenge the notion of the impureim sandwich architecture. Usually, I find that by taking a slightly more holistic view of what has to happen, I can make most problems fit the pattern. </p> <p> The most common counterargument is that subsequent impure queries may depend on decisions taken earlier. Thus, the argument goes, you can't gather all impure data up front. </p> <p> I'm sure that such situations genuinely exist, but I also think that they are rarer than most people think. In most cases I've experienced, even when I initially think that I've encountered such a situation, after a bit of reflection I find that I can structure the code to fit the <em>functional core, imperative shell</em> architecture. Not only that, but the code becomes simpler in the process. </p> <p> This happened in the example I've covered in this article. Initially, I though that ensuring that a Fusebit user exists would involve a process as illustrated in the first of the above flowcharts. Then, after thinking it over, I realised that defining a simple discriminated union would simplify matters <em>and</em> make the code testable. </p> <p> I thought it was worthwhile sharing that journey of discovery with others. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Nested type-safe DI Containers https://blog.ploeh.dk/2022/02/07/nested-type-safe-di-containers 2022-02-07T07:01:00+00:00 Mark Seemann <div id="post"> <p> <em>How to address the arity problem with type-safe DI Container prototypes.</em> </p> <p> This article is part of a series called <a href="/2022/01/10/type-safe-di-composition">Type-safe DI composition</a>. In the previous article, you saw <a href="/2022/01/31/a-type-safe-di-container-c-example">a C# example of a type-safe DI Container</a>. In case it's not clear from the article that introduces the series, there's really no point to any of this. My motivation for writing the article is that readers sometimes ask me about topics such as DI Containers versus type safety, or DI Containers in functional programming. The goal of these articles is to make it painfully clear why I find such ideas moot. </p> <h3 id="c261b81e550743df99709ff721e08807"> N+1 arity <a href="#c261b81e550743df99709ff721e08807" title="permalink">#</a> </h3> <p> The <a href="/2022/01/31/a-type-safe-di-container-c-example">previous article</a> suggested a series of generic containers in order to support type-safe Dependency Injection (DI) composition. For example, to support five services, you need five generic containers: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>&gt; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>&gt; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>&gt; </pre> </p> <p> As the above listing suggests, there's also an (effectively redundant) empty, non-generic <code>Container</code> class. Thus, in order to support five services, you need 5 + 1 = 6 <code>Container</code> classes. In order to support ten services, you'll need eleven classes, and so on. </p> <p> While these classes are all boilerplate and completely generic, you may still consider this a design flaw. If so, there's a workaround. </p> <h3 id="3ece4148c1d2449896e2f3bad3eb31e4"> Nested containers <a href="#3ece4148c1d2449896e2f3bad3eb31e4" title="permalink">#</a> </h3> <p> The key to avoid the <em>n + 1</em> arity problem is to nest the containers. First, we can delete <code><span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;</code>, <code><span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>&gt;</code>, and <code><span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>&gt;</code>, while leaving <code><span style="color:#2b91af;">Container</span></code> and <code><span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>&gt;</code> alone. </p> <p> <code><span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;</code> needs a few changes to its <code>Register</code> methods. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Container&lt;T1,&nbsp;Container&lt;T2,&nbsp;T3&gt;&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T3</span>&gt;(T3&nbsp;<span style="color:#1f377f;">item3</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Container&lt;T1,&nbsp;Container&lt;T2,&nbsp;T3&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Container&lt;T2,&nbsp;T3&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item3)); }</pre> </p> <p> Instead of returning a <code>Container&lt;T1,&nbsp;T2,&nbsp;T3&gt;</code>, this version of <code>Register</code> returns a <code>Container&lt;T1,&nbsp;Container&lt;T2,&nbsp;T3&gt;&gt;</code>. Notice how <code>Item2</code> is a new container. A <code>Container&lt;T2, T3&gt;</code> is nested inside an outer container whose <code>Item1</code> remains of the type <code>T1</code>, but whose <code>Item2</code> is of the type <code>Container&lt;T2, T3&gt;</code>. </p> <p> The other <code>Register</code> overload follows suit: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Container&lt;T1,&nbsp;Container&lt;T2,&nbsp;T3&gt;&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T3</span>&gt;(Func&lt;T1,&nbsp;T2,&nbsp;T3&gt;&nbsp;<span style="color:#1f377f;">create</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(create&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(create)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">item3</span>&nbsp;=&nbsp;create(Item1,&nbsp;Item2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Register(item3); }</pre> </p> <p> The only change to this method, compared to the <a href="/2022/01/31/a-type-safe-di-container-c-example">previous article</a>, is to the return type. </p> <h3 id="45e07d03149b44e7aabf566aa14e49f4"> Usage <a href="#45e07d03149b44e7aabf566aa14e49f4" title="permalink">#</a> </h3> <p> Since the input parameter types didn't change, composition still looks much the same: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">container</span>&nbsp;=&nbsp;Container.Empty &nbsp;&nbsp;&nbsp;&nbsp;.Register(Configuration) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreateRestaurantDatabase) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreatePostOffice) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreateClock()) &nbsp;&nbsp;&nbsp;&nbsp;.Register((<span style="color:#1f377f;">conf</span>,&nbsp;<span style="color:#1f377f;">cont</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompositionRoot.CreateRepository(conf,&nbsp;cont.Item1.Item2)); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">compositionRoot</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompositionRoot(container); services.AddSingleton&lt;IControllerActivator&gt;(compositionRoot);</pre> </p> <p> Only the last <code>Register</code> call is different. Instead of a lambda expression taking <em>four</em> arguments (<code>(c, _, po, __)</code>), this one only takes two: <code>(conf, cont)</code>. <code>conf</code> is an <code>IConfiguration</code> object, while <code>cont</code> is a nested container of the type <code>Container&lt;Container&lt;IRestaurantDatabase,&nbsp;IPostOffice&gt;,&nbsp;IClock&gt;</code>. </p> <p> Recall that the <code>CreateRepository</code> method has this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReservationsRepository&nbsp;<span style="color:#74531f;">CreateRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;IConfiguration&nbsp;<span style="color:#1f377f;">configuration</span>, &nbsp;&nbsp;&nbsp;&nbsp;IPostOffice&nbsp;<span style="color:#1f377f;">postOffice</span>)</pre> </p> <p> In order to produce the required <code>IPostOffice</code> object, the lambda expression must first read <code>Item1</code>, which has the type <code>Container&lt;IRestaurantDatabase,&nbsp;IPostOffice&gt;</code>. It can then read <em>that</em> container's <code>Item2</code> to get the <code>IPostOffice</code>. </p> <p> Not particularly readable, but type-safe. </p> <p> The entire <code>container</code> object passed into <code>CompositionRoot</code> has the type <code>Container&lt;IConfiguration,&nbsp;Container&lt;Container&lt;Container&lt;IRestaurantDatabase,&nbsp;IPostOffice&gt;,&nbsp;IClock&gt;,&nbsp;IReservationsRepository&gt;&gt;</code>. </p> <p> Equivalently, the <code>CompositionRoot</code>'s <code>Create</code> method has to <a href="https://en.wikipedia.org/wiki/Law_of_Demeter">train-wreck</a> its way to each service: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;<span style="color:#74531f;">Create</span>(ControllerContext&nbsp;<span style="color:#1f377f;">context</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(context&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(context)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;context.ActionDescriptor.ControllerTypeInfo; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(CalendarController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;CalendarController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2.Item1.Item1.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2.Item2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(HomeController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;HomeController(container.Item2.Item1.Item1.Item1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ReservationsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2.Item1.Item2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2.Item1.Item1.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2.Item2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(RestaurantsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantsController(container.Item2.Item1.Item1.Item1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ScheduleController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ScheduleController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2.Item1.Item1.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2.Item2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AccessControlList.FromUser(context.HttpContext.User)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Unexpected&nbsp;controller&nbsp;type:&nbsp;</span>{t}<span style="color:#a31515;">.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(context)); }</pre> </p> <p> Notice how most of the services depend on <code>container.Item2.Item1.Item1.Item1</code>. If you hover over that code in an <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a>, you'll see that this is an <code>IRestaurantDatabase</code> service. Again, type-safe, but hardly readable. </p> <h3 id="d311f951726146d39bc7402d756b8032"> Conclusion <a href="#d311f951726146d39bc7402d756b8032" title="permalink">#</a> </h3> <p> You can address the <em>n + 1 arity</em> problem by nesting generic containers inside each other. How did I think of this solution? And can we simplify things even more? </p> <p> Read on. </p> <p> <strong>Next:</strong> <a href="/2022/02/21/a-type-safe-di-container-as-a-functor">A type-safe DI Container as a functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A type-safe DI Container C# example https://blog.ploeh.dk/2022/01/31/a-type-safe-di-container-c-example 2022-01-31T06:42:00+00:00 Mark Seemann <div id="post"> <p> <em>An ultimately pointless exploration of options.</em> </p> <p> This article is part of a series called <a href="/2022/01/10/type-safe-di-composition">Type-safe DI composition</a>. In the previous article, you saw <a href="/2022/01/24/type-level-di-container-prototype">a type-level prototype</a> written in <a href="https://www.haskell.org">Haskell</a>. If you don't read Haskell code, then it's okay to skip that article and instead start reading here. I'm not going to assume that you've read and understood the previous article. </p> <p> In this article, I'll begin exploration of a type-safe Dependency Injection (DI) Container prototype written in C#. In order to demonstrate that it works in a realistic environment, I'm going to use it in the code base that accompanies <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <h3 id="1d6113f811d348f593851f8e54896105"> Empty container <a href="#1d6113f811d348f593851f8e54896105" title="permalink">#</a> </h3> <p> Like the previous article, we can start with an empty container: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Container&nbsp;Empty&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Container(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Container</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Container&lt;T1&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T1</span>&gt;(T1&nbsp;<span style="color:#1f377f;">item1</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Container&lt;T1&gt;(item1); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The only API this class affords is the <code>Empty</code> <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a> instance and the <code>Register</code> method. As you can tell from the signature, this method returns a different container type. </p> <h3 id="a566b84277eb413ea29ce0ab9094ff51"> Generic container with one item <a href="#a566b84277eb413ea29ce0ab9094ff51" title="permalink">#</a> </h3> <p> The above <code>Register</code> method returns a <code>Container&lt;T1&gt;</code> instance. This class is defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Container</span>(T1&nbsp;<span style="color:#1f377f;">item1</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item1&nbsp;=&nbsp;item1; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T1&nbsp;Item1&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members&nbsp;here...</span></pre> </p> <p> This enables you to add a single service of any type <code>T1</code>. For example, if you <code>Register</code> an <code>IConfiguration</code> instance, you'll have a <code>Container&lt;IConfiguration&gt;</code>: </p> <p> <pre>Container&lt;IConfiguration&gt;&nbsp;<span style="color:#1f377f;">container</span>&nbsp;=&nbsp;Container.Empty.Register(Configuration);</pre> </p> <p> The static type system tells you that <code>Item1</code> contains an <code>IConfiguration</code> object - not a collection of <code>IConfiguration</code> objects, or one that may or may not be there. There's guaranteed to be one and only one. No <a href="/2013/07/08/defensive-coding">defensive coding</a> is required: </p> <p> <pre>IConfiguration&nbsp;<span style="color:#1f377f;">conf</span>&nbsp;=&nbsp;container.Item1;</pre> </p> <p> A container that contains only a single service is, however, hardly interesting. How do we add more services? </p> <h3 id="a43d9f12300d45c68b4b7fad248010a2"> Registration <a href="#a43d9f12300d45c68b4b7fad248010a2" title="permalink">#</a> </h3> <p> The <code>Container&lt;T1&gt;</code> class affords a <code>Register</code> method of its own: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Container&lt;T1,&nbsp;T2&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T2</span>&gt;(T2&nbsp;<span style="color:#1f377f;">item2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Container&lt;T1,&nbsp;T2&gt;(Item1,&nbsp;item2); }</pre> </p> <p> This method return a new container that contains both <code>Item1</code> and <code>item2</code>. </p> <p> There's also a convenient overload that, in some scenarios, better support method chaining: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Container&lt;T1,&nbsp;T2&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T2</span>&gt;(Func&lt;T1,&nbsp;T2&gt;&nbsp;<span style="color:#1f377f;">create</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(create&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(create)); &nbsp;&nbsp;&nbsp;&nbsp;T2&nbsp;<span style="color:#1f377f;">item2</span>&nbsp;=&nbsp;create(Item1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Register(item2); }</pre> </p> <p> This method runs a <code>create</code> function to produce an object of the type <code>T2</code> given the already-registered service <code>Item1</code>. </p> <p> As an example, the code base's <a href="/2011/07/28/CompositionRoot">Composition Root</a> defines a method for creating an <code>IRestaurantDatabase</code> object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IRestaurantDatabase&nbsp;<span style="color:#74531f;">CreateRestaurantDatabase</span>( &nbsp;&nbsp;&nbsp;IConfiguration&nbsp;<span style="color:#1f377f;">configuration</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(configuration&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(configuration)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">restaurantsOptions</span>&nbsp;=&nbsp;configuration.GetSection(<span style="color:#a31515;">&quot;Restaurants&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Get&lt;RestaurantOptions[]&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(restaurantsOptions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.ToRestaurant()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OfType&lt;Restaurant&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToArray()); }</pre> </p> <p> Notice that this method takes an <code>IConfiguration</code> object as input. You can now use the <code>Register</code> overload to add the <code>IRestaurantDatabase</code> service to the container: </p> <p> <pre>Container&lt;IConfiguration,&nbsp;IRestaurantDatabase&gt;&nbsp;<span style="color:#1f377f;">container</span>&nbsp;=&nbsp;Container.Empty &nbsp;&nbsp;&nbsp;&nbsp;.Register(Configuration) &nbsp;&nbsp;&nbsp;&nbsp;.Register(<span style="color:#1f377f;">conf</span>&nbsp;=&gt;&nbsp;CompositionRoot.CreateRestaurantDatabase(conf));</pre> </p> <p> Or, via eta reduction: </p> <p> <pre>Container&lt;IConfiguration,&nbsp;IRestaurantDatabase&gt;&nbsp;<span style="color:#1f377f;">container</span>&nbsp;=&nbsp;Container.Empty &nbsp;&nbsp;&nbsp;&nbsp;.Register(Configuration) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreateRestaurantDatabase);</pre> </p> <p> You've probably noticed that this container is one with <em>two</em> generic type arguments. </p> <h3 id="4cc0437bb0e14543a63d650df7f07e32"> Generic container with two items <a href="#4cc0437bb0e14543a63d650df7f07e32" title="permalink">#</a> </h3> <p> This new class is, not surprisingly, defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Container</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Container</span>(T1&nbsp;<span style="color:#1f377f;">item1</span>,&nbsp;T2&nbsp;<span style="color:#1f377f;">item2</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item1&nbsp;=&nbsp;item1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item2&nbsp;=&nbsp;item2; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T1&nbsp;Item1&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T2&nbsp;Item2&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Container&lt;T1,&nbsp;T2,&nbsp;T3&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T3</span>&gt;(T3&nbsp;<span style="color:#1f377f;">item3</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Container&lt;T1,&nbsp;T2,&nbsp;T3&gt;(Item1,&nbsp;Item2,&nbsp;item3); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Container&lt;T1,&nbsp;T2,&nbsp;T3&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T3</span>&gt;(Func&lt;T1,&nbsp;T2,&nbsp;T3&gt;&nbsp;<span style="color:#1f377f;">create</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(create&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(create)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">item3</span>&nbsp;=&nbsp;create(Item1,&nbsp;Item2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Register(item3); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>?&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;Container&lt;T1,&nbsp;T2&gt;&nbsp;<span style="color:#1f377f;">container</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;T1&gt;.Default.Equals(Item1,&nbsp;container.Item1)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;T2&gt;.Default.Equals(Item2,&nbsp;container.Item2); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;HashCode.Combine(Item1,&nbsp;Item2); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Like <code>Container&lt;T1&gt;</code> it also defines two <code>Register</code> overloads that enable you to add yet another service. </p> <p> If you're on C# 9 or later, you could dispense with much of the boilerplate code by defining the type as a <code>record</code> instead of a <code>class</code>. </p> <h3 id="b2771d4cd9a04cf7932195fe9a8b478c"> Containers of higher arity <a href="#b2771d4cd9a04cf7932195fe9a8b478c" title="permalink">#</a> </h3> <p> You've probably noticed a pattern. Each <code>Register</code> method just returns a container with incremented arity: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Container&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;T4&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T4</span>&gt;(T4&nbsp;<span style="color:#1f377f;">item4</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Container&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;T4&gt;(Item1,&nbsp;Item2,&nbsp;Item3,&nbsp;item4); }</pre> </p> <p> and </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Container&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;T4,&nbsp;T5&gt;&nbsp;<span style="color:#74531f;">Register</span>&lt;<span style="color:#2b91af;">T5</span>&gt;(T5&nbsp;<span style="color:#1f377f;">item5</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Container&lt;T1,&nbsp;T2,&nbsp;T3,&nbsp;T4,&nbsp;T5&gt;(Item1,&nbsp;Item2,&nbsp;Item3,&nbsp;Item4,&nbsp;item5); }</pre> </p> <p> and so on. </p> <p> Wait! Does every new service require a new class? What if you have 143 services to <a href="/2010/09/29/TheRegisterResolveReleasepattern">register</a>? </p> <p> Well, yes, as presented here, you'll need 144 classes (one for each service, plus the empty container). They'd all be generic, so you could imagine making a reusable library that defines all these classes. It is, however, pointless for several reasons: <ul> <li>You shouldn't have that many services. Do yourself a favour and <a href="https://stackoverflow.blog/2022/01/03/favor-real-dependencies-for-unit-testing/">have a service for each 'real' architectural dependency</a>: 1. Database, 2. Email gateway, 3. Third-party HTTP service, etc. Also, add a service for anything else that's not <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>, such as clocks and random number generators. For the example restaurant reservation system, the greatest arity I needed was 5: <code>Container&lt;T1, T2, T3, T4, T5&gt;</code>.</li> <li>You don't need a container for each arity after all, as the next article will demonstrate.</li> <li>This is all pointless anyway, as already predicted in the <a href="/2022/01/10/type-safe-di-composition">introduction article</a>.</li> </ul> For now, then, I'll continue with the example. There are five generic container classes, as well as the empty one (which really is redundant, but so is all of this). </p> <h3 id="0323fc5a24fc4397b67203f4c71e64cf"> Usage <a href="#0323fc5a24fc4397b67203f4c71e64cf" title="permalink">#</a> </h3> <p> You can create a statically typed container by registering all the required services: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">container</span>&nbsp;=&nbsp;Container.Empty &nbsp;&nbsp;&nbsp;&nbsp;.Register(Configuration) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreateRestaurantDatabase) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreatePostOffice) &nbsp;&nbsp;&nbsp;&nbsp;.Register(CompositionRoot.CreateClock()) &nbsp;&nbsp;&nbsp;&nbsp;.Register( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#1f377f;">c</span>,&nbsp;<span style="color:#1f377f;">_</span>,&nbsp;<span style="color:#1f377f;">po</span>,&nbsp;<span style="color:#1f377f;">__</span>)&nbsp;=&gt;&nbsp;CompositionRoot.CreateRepository(c,&nbsp;po));</pre> </p> <p> The <code>container</code> object has the type <code>Container&lt;IConfiguration,&nbsp;IRestaurantDatabase,&nbsp;IPostOffice,&nbsp;IClock,&nbsp;IReservationsRepository&gt; </code>, which is quite a mouthful. You can, however, pass it to the <code>CompositionRoot</code> and register it as the application's <code>IControllerActivator</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">compositionRoot</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompositionRoot(container); services.AddSingleton&lt;IControllerActivator&gt;(compositionRoot);</pre> </p> <p> <code>CompositionRoot.Create</code> uses the injected <code>container</code> to create Controllers: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;<span style="color:#74531f;">Create</span>(ControllerContext&nbsp;<span style="color:#1f377f;">context</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(context&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(context)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;context.ActionDescriptor.ControllerTypeInfo; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(CalendarController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;CalendarController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item5); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(HomeController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;HomeController(container.Item2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ReservationsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item5); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(RestaurantsController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;RestaurantsController(container.Item2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(t&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(ScheduleController)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ScheduleController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;container.Item5, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AccessControlList.FromUser(context.HttpContext.User)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Unexpected&nbsp;controller&nbsp;type:&nbsp;</span>{t}<span style="color:#a31515;">.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(context)); }</pre> </p> <p> Having to refer to <code>Item2</code>, <code>Item5</code>, etc. instead of named services leaves better readability to be desired, but <a href="https://amzn.to/2PzDpJu">in the end, it doesn't even matter</a>, because, as you'll see as this article series progresses, this is all moot. </p> <h3 id="230326b6b3f147709ade44982feffbb5"> Conclusion <a href="#230326b6b3f147709ade44982feffbb5" title="permalink">#</a> </h3> <p> You can define a type-safe DI Container with a series of generic containers. Each registered service has a generic type, so if you need a single <code>IFoo</code>, you register a single <code>IFoo</code> object. If you need a collection of <code>IBar</code> objects, you register an <code>IReadOnlyCollection&lt;IBar&gt;</code>, and so on. This means that you don't have to waste brain capacity remembering the configuration of all services. </p> <p> Compared to the initial Haskell prototype, the C# example shown here doesn't prevent you from registering the same type more than once. I don't know of a way to do this at the type level in C#, and while you could make a run-time check to prevent this, I didn't implement it. After all, as this article series will demonstrate, none of this is particularly useful, because <a href="/2014/06/10/pure-di">Pure Di</a> is simpler without being less powerful. </p> <p> If you were concerned about the proliferation of generic classes with increasing type argument arity, then rest assured that this isn't a problem. The next article in the series will demonstrate how to get around that issue. </p> <p> <strong>Next:</strong> <a href="/2022/02/07/nested-type-safe-di-containers">Nested type-safe DI Containers</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Type-level DI Container prototype https://blog.ploeh.dk/2022/01/24/type-level-di-container-prototype 2022-01-24T06:48:00+00:00 Mark Seemann <div id="post"> <p> <em>A fairly pointless Haskell exercise.</em> </p> <p> This article is part of a series called <a href="/2022/01/10/type-safe-di-composition">Type-safe DI composition</a>. </p> <p> People sometimes ask me how to do Dependency Injection (DI) in Functional Programming, and the short answer is that <a href="/2017/02/02/dependency-rejection">you don't</a>. DI makes everything impure, while the entire <a href="/2018/11/19/functional-architecture-a-definition">point of FP is to write as much referentially transparent code as possible</a>. Instead, you should aim for the <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">Functional Core, Imperative Shell</a> style of architecture (AKA <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>). </p> <p> Occasionally, someone might point out that you can use the <a href="/2021/10/04/reader-as-a-contravariant-functor">contravariant Reader functor</a> with a 'registry' of services to <a href="/2022/03/21/contravariant-dependency-injection">emulate a DI Container in FP</a>. </p> <p> Not really, because even if you make the dependencies implicitly available as the Reader 'environment', they're still impure. More on that in a future article, though. </p> <p> Still, what's a DI Container but <a href="https://ayende.com/blog/2886/building-an-ioc-container-in-15-lines-of-code">a dictionary of objects, keyed by type</a>? After I read <a href="https://thinkingwithtypes.com/">Thinking with Types</a> I thought I'd do the exercise and write a type-level container of values in Haskell. </p> <h3 id="e60305d1f0914fd689209c6aa2da5f1e"> Module <a href="#e60305d1f0914fd689209c6aa2da5f1e" title="permalink">#</a> </h3> <p> The <code>TLContainer</code> module requires a smorgasbord of extensions and a few imports: </p> <p> <pre>{-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;AllowAmbiguousTypes&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;ConstraintKinds&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;DataKinds&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;PolyKinds&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;GADTs&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;ScopedTypeVariables&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;TypeApplications&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;TypeOperators&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;TypeFamilies&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;FlexibleContexts&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;RankNTypes&nbsp;#-} {-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;UndecidableInstances&nbsp;#-} <span style="color:blue;">module</span>&nbsp;TLContainer&nbsp;(<span style="color:blue;">Container</span>&nbsp;(),&nbsp;<span style="color:#2b91af;">nil</span>,&nbsp;<span style="color:#2b91af;">insert</span>,&nbsp;<span style="color:#2b91af;">get</span>)&nbsp;<span style="color:blue;">where</span> <span style="color:blue;">import</span>&nbsp;Data.Kind <span style="color:blue;">import</span>&nbsp;Data.Proxy <span style="color:blue;">import</span>&nbsp;GHC.TypeLits <span style="color:blue;">import</span>&nbsp;Fcf <span style="color:blue;">import</span>&nbsp;Unsafe.Coerce&nbsp;(<span style="color:#2b91af;">unsafeCoerce</span>) </pre> </p> <p> Notice that the module only exports the <code>Container</code> type, but not its data constructor. You'll have to use <code>nil</code> and <code>insert</code> to create values of the type. </p> <h3 id="ff97b224b8e04299a8ee100fbc944adf"> Data types <a href="#ff97b224b8e04299a8ee100fbc944adf" title="permalink">#</a> </h3> <p> The <code>Container</code> should be able to store an arbitrary number of services of arbitrary types. This doesn't sound like a good fit for a statically typed language like Haskell, but it's possible to do this with existential types. Define an existential type that models a container registration: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Reg&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;Reg&nbsp;::&nbsp;a&nbsp;-&gt;&nbsp;Reg </pre> </p> <p> The problem with existential types is that the type argument <code>a</code> is lost at compile time. If you have a <code>Reg</code> value, it contains a value (e.g. a service) of a particular type, but you don't know what it is. </p> <p> You can solve this by keeping track of the types at the type level of the container itself. The <code>Container</code> data type is basically just a wrapper around a list of <code>Reg</code> values: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Container&nbsp;(ts&nbsp;::&nbsp;[k])&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;UnsafeContainer&nbsp;::&nbsp;[Reg]&nbsp;-&gt;&nbsp;Container&nbsp;ts </pre> </p> <p> The name of the data constructor is <code>UnsafeContainer</code> because it's unsafe. It would enable you to add multiple registrations of the same type. The container shouldn't allow that, so the module doesn't export <code>UnsafeContainer</code>. Instead, it defines sufficient type-level constraints to guarantee that if you try to add two registrations of the same type, your code isn't going to compile. </p> <p> This is the principal feature that distinguishes <code>Container</code> from the set of tuples that Haskell already defines. </p> <h3 id="9299405418b44b7dbe35310f11543e7a"> Registration <a href="#9299405418b44b7dbe35310f11543e7a" title="permalink">#</a> </h3> <p> The module exports an empty <code>Container</code>: </p> <p> <pre><span style="color:#2b91af;">nil</span>&nbsp;::&nbsp;<span style="color:blue;">Container</span>&nbsp;&#39;[] nil&nbsp;=&nbsp;UnsafeContainer&nbsp;<span style="color:blue;">[]</span> </pre> </p> <p> Not only is this container empty, it's also statically typed that way. The type <code>Container '[]</code> is isomorphic to <code>()</code>. </p> <p> The <code>nil</code> container gives you a container so that you can get started. You can add a registration to <code>nil</code>, and that's going to return a new container. You can add another registration to that container, and so on. </p> <p> The distinguishing feature of <code>Container</code>, however, is that you can only add one registration of a given type. If you want to register multiple services of the same type, register a list of them. </p> <p> Code like <code>insert readReservations $ insert readReservations nil</code> shouldn't compile, because it tries to <code>insert</code> the same service (<code>readReservations</code>) twice. To enable that feature, the module must be able to detect type uniqueness at the type level. This is possible with the help from <a href="https://hackage.haskell.org/package/first-class-families">the first-class-families package</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;UniqueType&nbsp;(t&nbsp;::&nbsp;k)&nbsp;(ts&nbsp;::&nbsp;[k])&nbsp;=&nbsp;Null&nbsp;=&lt;&lt;&nbsp;Filter&nbsp;(TyEq&nbsp;t)&nbsp;ts </pre> </p> <p> This type models the condition that the type <code>t</code> must not be in the list of types <code>ts</code>. It almost looks like regular Haskell code at the term level, but it works at the type level. <code>Null</code> is a type that can be evaluated to Boolean types at compile-time, depending on whether a list is empty or not. </p> <p> This enables you to define a closed type family that will produce a compile-time error if a candidate type isn't unique: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;family&nbsp;RequireUniqueType&nbsp;(result&nbsp;::&nbsp;Bool)&nbsp;(t&nbsp;::&nbsp;k)&nbsp;::&nbsp;Constraint&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;RequireUniqueType&nbsp;&nbsp;&#39;True&nbsp;t&nbsp;=&nbsp;<span style="color:blue;">()</span> &nbsp;&nbsp;RequireUniqueType&nbsp;&#39;False&nbsp;t&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;TypeError&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;Text&nbsp;<span style="color:#a31515;">&quot;Attempting&nbsp;to&nbsp;add&nbsp;the&nbsp;type&nbsp;&quot;</span>&nbsp;&#39;:&lt;&gt;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;ShowType&nbsp;t&nbsp;&#39;:&lt;&gt;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;Text&nbsp;<span style="color:#a31515;">&quot;&nbsp;to&nbsp;the&nbsp;container,&nbsp;but&nbsp;this&nbsp;container&nbsp;already&nbsp;contains&nbsp;that&nbsp;type.&quot;</span>) </pre> </p> <p> Combined with the <code>UniqueType</code> alias, you can now define the <code>insert</code> function: </p> <p> <pre><span style="color:#2b91af;">insert</span>&nbsp;::&nbsp;<span style="color:blue;">RequireUniqueType</span>&nbsp;(<span style="color:blue;">Eval</span>&nbsp;(<span style="color:blue;">UniqueType</span>&nbsp;t&nbsp;ts))&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;t&nbsp;-&gt;&nbsp;Container&nbsp;ts&nbsp;-&gt;&nbsp;Container&nbsp;(t&nbsp;&#39;:&nbsp;ts) insert&nbsp;x&nbsp;(UnsafeContainer&nbsp;xs)&nbsp;=&nbsp;UnsafeContainer&nbsp;(Reg&nbsp;x&nbsp;:&nbsp;xs) </pre> </p> <p> This function enables you to register multiple services, like this: </p> <p> <pre><span style="color:#2b91af;">container</span>&nbsp;::&nbsp;<span style="color:blue;">Container</span>&nbsp;&#39;[<span style="color:blue;">LocalTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;[<span style="color:blue;">Reservation</span>],&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;()] container&nbsp;=&nbsp;insert&nbsp;readReservations&nbsp;$&nbsp;insert&nbsp;createReservation&nbsp;nil</pre> </p> <p> If, on the other hand, you attempt to register the same service multiple times, your code doesn't compile. You might, for example, attempt to do something like this: </p> <p> <pre>container&#39;&nbsp;=&nbsp;insert&nbsp;readReservations&nbsp;container</pre> </p> <p> When you try to compile your code, however, it doesn't: </p> <p> <pre> * Attempting to add the type LocalTime -> IO [Reservation] to the container, but this container already contains that type. * In the expression: insert readReservations container In an equation for container': container' = insert readReservations container <span style="color: blue">|</span> <span style="color: blue">36 |</span> container' = <span style="color: red">insert readReservations container</span> <span style="color: blue">|</span> <span style="color: red">^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</span></pre> </p> <p> As a proof of concept, that's good enough for me. A type-safe set of uniquely typed registrations. </p> <h3 id="fb31959c8c8545879c5266466de2ce1e"> Retrieval <a href="#fb31959c8c8545879c5266466de2ce1e" title="permalink">#</a> </h3> <p> Given that <code>Container</code> is a wrapper over a list of existential types, it seems as though the type information is lost. It isn't, though. Consider the type of the above <code>container</code> value. At the type level, you can see that it contains two services: one with the type <code>LocalTime -&gt; IO [Reservation]</code>, and another with the type <code>Reservation -&gt; IO ()</code>. Not only that, but the compiler can see the position of each of those types. Due to the way <code>insert</code> is implemented, that order corresponds to the order of <code>Reg</code> values. </p> <p> First, define a type alias to find the index of a type <code>t</code> in a list of types <code>ts</code>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;FindElem&nbsp;(t&nbsp;::&nbsp;k)&nbsp;(ts&nbsp;::&nbsp;[k])&nbsp;= &nbsp;&nbsp;FromMaybe&nbsp;Stuck&nbsp;=&lt;&lt;&nbsp;FindIndex&nbsp;(TyEq&nbsp;t)&nbsp;ts </pre> </p> <p> This is again the <em>first-class-families</em> package in action. <code>FindIndex</code> finds a <code>Nat</code> that represents the index if the type is there. If it isn't there, the type is equivalent to <code>Stuck</code>, which is the type-level equivalent of <code>undefined</code>. <code>Nat</code> is a <code>KnownNat</code> instance, whereas <code>Stuck</code> isn't, which now enables you to define a constraint: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;IsMember&nbsp;t&nbsp;ts&nbsp;=&nbsp;KnownNat&nbsp;(Eval&nbsp;(FindElem&nbsp;t&nbsp;ts))</pre> </p> <p> The <code>IsMember</code> constraint limits <code>t</code> to belong to <code>ts</code>. With it, you can now define a helper function to find the index of a type <code>t</code> in a list of types <code>ts</code>: </p> <p> <pre><span style="color:#2b91af;">findElem</span>&nbsp;::&nbsp;forall&nbsp;t&nbsp;ts.&nbsp;<span style="color:blue;">IsMember</span>&nbsp;t&nbsp;ts&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:#2b91af;">Int</span> findElem&nbsp;=&nbsp;<span style="color:blue;">fromIntegral</span>&nbsp;.&nbsp;natVal&nbsp;$&nbsp;Proxy&nbsp;@(Eval&nbsp;(FindElem&nbsp;t&nbsp;ts)) </pre> </p> <p> Because of the <code>IsMember</code> constraint, we know that <code>t</code> must be a member of <code>ts</code>. You can't call <code>findElem</code> if that's not the case; your code wouldn't compile. </p> <p> You can now define a function to retrieve a service from a <code>Container</code>: </p> <p> <pre><span style="color:#2b91af;">get</span>&nbsp;::&nbsp;forall&nbsp;t&nbsp;ts.&nbsp;<span style="color:blue;">IsMember</span>&nbsp;t&nbsp;ts&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Container</span>&nbsp;ts&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;t get&nbsp;(UnsafeContainer&nbsp;xs)&nbsp;=&nbsp;unReg&nbsp;$&nbsp;xs&nbsp;!!&nbsp;findElem&nbsp;@t&nbsp;@ts &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;unReg&nbsp;(Reg&nbsp;x)&nbsp;=&nbsp;unsafeCoerce&nbsp;x </pre> </p> <p> The <code>get</code> function first finds the index of the type <code>t</code> in <code>ts</code>. It then uses the (unsafe) list index operator <code>!!</code> to get the correct <code>Reg</code> value out of <code>x</code>. While the use of <code>!!</code> is generally considered unsafe (or, at least, partial) in Haskell, we <em>know</em> that the element is there because of the <code>IsMember</code> constraint. </p> <p> Furthermore, because of the way <code>insert</code> builds up the container, we know that the service inside the existential type <code>Reg</code> must be of the type <code>t</code>. Thus, it's safe to use <code>unsafeCoerce</code>. </p> <h3 id="243221d6a21442b78c2fe54c942d33e3"> Example <a href="#243221d6a21442b78c2fe54c942d33e3" title="permalink">#</a> </h3> <p> Imagine that you've created the above <code>container</code>. You can now retrieve services from it as necessary. </p> <p> For example, to implement a HTTP GET resource that returns a list of reservations for a given date, you could do something like this: </p> <p> <pre><span style="color:#2b91af;">getReservations</span>&nbsp;::&nbsp;<span style="color:blue;">LocalTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;(<span style="color:blue;">HTTPResult</span>&nbsp;[<span style="color:blue;">Reservation</span>]) getReservations&nbsp;date&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;svc&nbsp;=&nbsp;get&nbsp;container&nbsp;::&nbsp;LocalTime&nbsp;-&gt;&nbsp;IO&nbsp;[Reservation] &nbsp;&nbsp;reservations&nbsp;&lt;-&nbsp;svc&nbsp;date &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;OK&nbsp;reservations</pre> </p> <p> Nothing much happens here. You could imagine that proper input validation of <code>date</code> is done before the service is invoked, or that some mapping operation is done on <code>reservations</code> before the function returns them. I omitted those steps, since they're not what matters. What matters is that that you can use <code>get</code> to safely get a service of the type <code>LocalTime -&gt; IO [Reservation]</code>. </p> <p> Likewise, you could implement an HTTP POST resource that clients can use use to create new reservations: </p> <p> <pre><span style="color:#2b91af;">postReservation</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;(<span style="color:blue;">HTTPResult</span>&nbsp;()) postReservation&nbsp;dto&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;svc&nbsp;=&nbsp;get&nbsp;container&nbsp;::&nbsp;Reservation&nbsp;-&gt;&nbsp;IO&nbsp;<span style="color:blue;">()</span> &nbsp;&nbsp;svc&nbsp;dto &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;OK&nbsp;<span style="color:blue;">()</span></pre> </p> <p> Since the compiler knows that <code>container</code> also contains a service of the type <code>Reservation -&gt; IO ()</code>, this still compiles. </p> <p> If, on the other hand, you attempted to implement a single HTTP GET resource, the following wouldn't compile: </p> <p> <pre><span style="color:#2b91af;">getSingleReservation</span>&nbsp;::&nbsp;<span style="color:blue;">LocalTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;(<span style="color:blue;">HTTPResult</span>&nbsp;<span style="color:blue;">Reservation</span>) getSingleReservation&nbsp;date&nbsp;email&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;svc&nbsp;=&nbsp;get&nbsp;container&nbsp;::&nbsp;LocalTime&nbsp;-&gt;&nbsp;String&nbsp;-&gt;&nbsp;IO&nbsp;(Maybe&nbsp;Reservation) &nbsp;&nbsp;mres&nbsp;&lt;-&nbsp;svc&nbsp;date&nbsp;email &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;mres&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;r&nbsp;-&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;OK&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;-&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;NotFound</pre> </p> <p> The <code>get container</code> line doesn't compile because <code>container</code> doesn't contain a service of the type <code>LocalTime -&gt; String -&gt; IO (Maybe Reservation)</code>, and the compiler can tell. </p> <p> If you truly want to add that feature, you'll have to first register that service with the container: </p> <p> <pre><span style="color:#2b91af;">container</span>&nbsp;::&nbsp;<span style="color:blue;">Container</span>&nbsp;&#39;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LocalTime&nbsp;-&gt;&nbsp;String&nbsp;-&gt;&nbsp;IO&nbsp;(Maybe&nbsp;Reservation), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LocalTime&nbsp;-&gt;&nbsp;IO&nbsp;[Reservation], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;-&gt;&nbsp;IO&nbsp;<span style="color:blue;">()</span>] container&nbsp;= &nbsp;&nbsp;insert&nbsp;readReservation&nbsp;$ &nbsp;&nbsp;insert&nbsp;readReservations&nbsp;$ &nbsp;&nbsp;insert&nbsp;createReservation&nbsp;nil</pre> </p> <p> Notice that the type of <code>container</code> has now changed. It now contains three services instead of two. The <code>getSingleReservation</code> action now compiles. </p> <h3 id="25a8d28be45a4ef1a5eb9732427c7ef0"> Uniqueness <a href="#25a8d28be45a4ef1a5eb9732427c7ef0" title="permalink">#</a> </h3> <p> The <code>Container</code> shown here is essentially just a glorified tuple. The only distinguishing trait is that you can define a tuple where two or more elements have the same type, such as <code>(String, Bool, String)</code>, whereas this isn't possible with <code>Container</code>. You can define a <code>Container '[String, Bool]</code>, but not <code>Container '[String, Bool, String]</code>. </p> <p> Why is this desirable? </p> <p> This stems from <a href="https://twitter.com/bogdangaliceanu/status/1252151352119132160">a short and (friendly, I hope) Twitter discussion</a> initiated by <a href="https://twitter.com/bogdangaliceanu">Bogdan Galiceanu</a>. We were discussing whether it'd be more appropriate to use <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.singleordefault">SingleOrDefault</a> to manipulate a service in a DI Container, or <code>foreach</code>. </p> <blockquote> <p> "Yeah, I wasn't explicit and clarified in a second tweet. I didn't mean in the services example, but in general where it helps if the reader's mental model of the code has 1 item from the collection, because that's how it is in real life. SingleOrDefault would enforce this." </p> <footer><cite><a href="https://twitter.com/bogdangaliceanu/status/1252210213211582477">Bogdan Galiceanu</a></cite></footer> </blockquote> <p> The point being made here is that while you have a dictionary of <em>collections</em>, you expect certain (if not all) of these collections to be <a href="https://en.wikipedia.org/wiki/Singleton_(mathematics)">singleton sets</a>. </p> <p> I'm so happy that people like Bogdan Galiceanu share their thoughts with me, because it gives me a wider perspective on how programmers interact with APIs. If you take the API of <a href="https://docs.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">the .NET Core DI Container</a> as given, you almost <em>have</em> to think of its entries in this way. </p> <p> I think, on the other hand, that this indicates a missed opportunity of API design. I replied: </p> <blockquote> <p> "Yes, this could be a requirement. I think, though, that if that's the case, you've unearthed another invariant. That's what object-oriented design is about. </p> <p> "Different invariants imply a change of type. If there can only be one of each element, then a set is more appropriate." </p> <footer><cite><a href="https://twitter.com/ploeh/status/1252226486507110400">(me)</a></cite></footer> </blockquote> <p> Twitter isn't a medium that makes it easy to elaborate on ideas, but what I meant was that if a container should contain only a single instance of, say, <code>IFoo</code>, it'd be clearer if the type system reflected that. Thus, when <a href="/2010/09/29/TheRegisterResolveReleasepattern">resolving</a> <code>IFoo</code>, the return type should be <code>IFoo</code>, and not <code>IEnumerable&lt;IFoo&gt;</code>. </p> <p> On the other hand, if you meant to <a href="/2010/09/29/TheRegisterResolveReleasepattern">register</a> a collection of <code>IBar</code> services, when resolving <code>IBar</code>, the return type should be <code>IEnumerable&lt;IBar&gt;</code> (or, <a href="/2013/07/20/linq-versus-the-lsp">even better</a>, <code>IReadOnlyCollection&lt;IBar&gt;</code>). </p> <p> The <code>Container</code> shown here has this desired property: You can't <code>insert</code> the same type more than once. If you want to <code>insert</code> multiple <code>IBar</code> values, you must <code>insert</code> a <code>[IBar]</code> (<em>list of <code>IBar</code></em>). Thus, you can't <code>get</code> a single <code>IBar</code>, but you can <code>get</code> a list: <code>[IBar]</code>. </p> <p> That was my motivation for the rule that each type can only appear once. In Haskell it's possible to implement such a rule at the type level. I don't think it'd be possible in a language like C# or <a href="https://fsharp.org">F#</a>, but you <em>could</em> implement it as a run-time check. </p> <h3 id="64add25aee0444b086b10bbfe0d48db3"> Conclusion <a href="#64add25aee0444b086b10bbfe0d48db3" title="permalink">#</a> </h3> <p> You can implement a type-level container of values in Haskell. The contents are completely parametrically polymorphic, so while you can insert pure values like <code>String</code>, <code>Bool</code>, or <code>Reservation</code> into it, you can also add functions and impure actions like <code>Reservation -&gt; IO ()</code>. </p> <p> Why is this interesting? It really isn't, apart from that I found it an interesting exercise in type-level programming. </p> <p> The idea of a type-safe DI Container is, however, marginally more compelling, so I'll return to that topic in a future article. </p> <p> <strong>Next:</strong> <a href="/2022/01/31/a-type-safe-di-container-c-example">A type-safe DI Container C# example</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Enumerate Wordle combinations with an applicative functor https://blog.ploeh.dk/2022/01/17/enumerate-wordle-combinations-with-an-applicative-functor 2022-01-17T16:39:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of ad-hoc programming.</em> </p> <p> Like so many others, I recently started solving the daily <a href="https://www.powerlanguage.co.uk/wordle/">Wordle</a> puzzle. As is normal when one is a beginner, I initially struggled a bit. One day, I couldn't find a good word to proceed. </p> <p> To be clear, this article isn't really about Wordle strategies or tools. Rather, it's an example of ad-hoc programming. Particularly, it's an example of how the <a href="/2018/10/01/applicative-functors">applicative</a> nature of lists can be useful when you need to enumerate combinations. While I've <a href="/2018/10/15/an-applicative-password-list">previously shown a similar example</a>, I think one more is warranted. </p> <h3 id="aa6304fd821b47f5a67ec6ea9e7e5665"> Inured from tears <a href="#aa6304fd821b47f5a67ec6ea9e7e5665" title="permalink">#</a> </h3> <p> Last Monday, I'd started with the word <em>TEARS</em>. (I've subsequently learned that better starting words exist.) The puzzle responded with a yellow <em>E</em> and a green <em>R:</em> </p> <p> <img src="/content/binary/wordle-tears.png" alt="Wordle after entering TEARS."> </p> <p> In case you haven't played the game, this means that the fourth letter of the hidden word <em>is</em> an <em>R</em>, and that the word also contains an <em>E</em>. The second letter, however, is <em>not</em> an <em>E</em>. Also, the hidden word contains neither <em>T</em>, <em>A</em>, nor <em>S</em>. </p> <p> While the green and yellow letters may be repeated, you only have six guesses, so it's a good idea to attempt to exhaust as many letters as possible. The first compatible word with five distinct letters that I could think of was <em>INURE</em>. </p> <p> <img src="/content/binary/wordle-tears-inure.png" alt="Wordle after entering TEARS and INURE."> </p> <p> This gave me a bit of new information. The hidden word also contains a <em>U</em>, but not as the third letter. Furthermore, the <em>E</em> isn't the last letter either. Keep in mind that from <em>TEARS</em> we also know that <em>E</em> isn't the second letter. </p> <p> While I believe myself to have a decent English vocabulary, at this point I was stuck. While I knew that the <em>E</em> had to be in either first or third position, I couldn't think of a single word that fit all observations. </p> <p> After trying to come up with a word for ten minutes, I decided that, instead of giving up, I'd use the applicative nature of lists to generate all possible combinations. I was hoping that with the observations already in place, there wouldn't be too many to sift through. </p> <h3 id="c0745354a6364beaba9ac1a4423e1b9d"> Combinations <a href="#c0745354a6364beaba9ac1a4423e1b9d" title="permalink">#</a> </h3> <p> While you could do this in other languages (such as <a href="https://fsharp.org">F#</a> or C#), it's natural to use <a href="https://www.haskell.org">Haskell</a> because it natively understands applicative functors. Thus, I launched GHCi (<em>Glasgow Haskell Compiler interactive</em> - the Haskell <a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>). </p> <p> Wordle is kind enough to show a keyboard with colour-coded keys: </p> <p> <img src="/content/binary/wordle-keyboard.png" alt="Wordle keyboard."> </p> <p> All letters except the dark ones remain valid, so I first defined a list of all available letters: </p> <p> <pre>&gt; avail = "QWERYUOPDFGHJKLZXCVBM"</pre> </p> <p> The variable <code>avail</code> is a <code>String</code>, but in Haskell, a <code>String</code> is a type synonym for a (linked) list of <code>Char</code> values (characters). Since lists form an applicative functor, that'll work. </p> <p> Most of the letters are still in play - only five letters are already out of the picture: <em>T</em>, <em>I</em>, <em>A</em>, <em>S</em>, and <em>N</em>. Thus, <code>avail</code>still spells out most of the alphabet. </p> <p> Next, I wrote an expression that enumerates all five-letter combinations of these letters, with one constraint: The fourth letter must be an <em>R:</em> </p> <p> <pre>&gt; candidates = (\x y z æ ø -&gt; [x,y,z,æ,ø]) &lt;$&gt; avail &lt;*&gt; avail &lt;*&gt; avail &lt;*&gt; "R" &lt;*&gt; avail</pre> </p> <p> The leftmost expression (<code>(\x y z æ ø -&gt; [x,y,z,æ,ø])</code>) is a lambda expression that takes five values (one from each list of available letters) and combines them to a single list. Each value (<code>x</code>, <code>y</code>, and so on) is a <code>Char</code> value, and since <code>String</code> in Haskell is the same as <code>[Char]</code>, the expression <code>[x,y,z,æ,ø]</code> is a <code>String</code>. In Danish we have three more letters after <code>z</code>, so I after I ran out of the the usual Latin letters, I just continued with the Danish <code>æ</code> and <code>ø</code>. </p> <p> Notice that between each of the <code>&lt;*&gt;</code> operators (<em>apply</em>) I've supplied the list of available letters. In the fourth position there's no choice, so there the list contains only a single letter. Recall that a <code>String</code> is a list of characters, so <code>"R"</code> is the same as <code>['R']</code>. </p> <p> How many combinations are there? Let's ask GHCi: </p> <p> <pre>&gt; length candidates 194481</pre> </p> <p> Almost 200,000. That's a bit much to look through, but we can peek at the first ten as a sanity check: </p> <p> <pre>&gt; take 10 candidates ["QQQRQ","QQQRW","QQQRE","QQQRR","QQQRY","QQQRU","QQQRO","QQQRP","QQQRD","QQQRF"]</pre> </p> <p> There are no promising words in this little list, but I never expected that. </p> <p> I needed to narrow down the options. </p> <h3 id="78f6d90a9b3a43649fa97f525b52f980"> Filtering <a href="#78f6d90a9b3a43649fa97f525b52f980" title="permalink">#</a> </h3> <p> How do you make a collection smaller? You could filter it. </p> <p> <code>candidates</code> contains illegal values. For example, the third value in the above list (of the ten first candidates) is <code>"QQQRE"</code>. Yet, we know (from the <em>INURE</em> attempt) that the last letter isn't <em>E</em>. We can filter out all strings that end with <code>E</code>: </p> <p> <pre>&gt; take 10 $ filter (\s -&gt; s!!4 /= 'E') candidates ["QQQRQ","QQQRW","QQQRR","QQQRY","QQQRU","QQQRO","QQQRP","QQQRD","QQQRF","QQQRG"]</pre> </p> <p> In Haskell, <code>!!</code> is the indexing operator, so <code>s!!4</code> means the (zero-based) fourth element of the string <code>s</code>. <code>/=</code> is the inequality operator, so the lambda expression <code>(\s -&gt; s!!4 /= 'E')</code> identifies all strings where the fifth element (or fourth element, when starting from zero) is different from <code>E</code>. </p> <p> We know more than this, though. We also know that the second element can't be <em>E</em>, and that the third element isn't <em>U</em>, so add those predicates to the <code>filter</code>: </p> <p> <pre>&gt; take 10 $ filter (\s -&gt; s!!1 /= 'E' && s!!2 /= 'U' && s!!4 /= 'E') candidates ["QQQRQ","QQQRW","QQQRR","QQQRY","QQQRU","QQQRO","QQQRP","QQQRD","QQQRF","QQQRG"]</pre> </p> <p> How many are left? </p> <p> <pre>&gt; length $ filter (\s -&gt; s!!1 /= 'E' && s!!2 /= 'U' && s!!4 /= 'E') candidates 168000</pre> </p> <p> Still too many, but we aren't done yet. </p> <p> Notice that all of the first ten values shown above are invalid. Why? Because the word must contain at least one <em>E</em>, and none of them do. Let's add that predicate: </p> <p> <pre>&gt; take 10 $ filter (\s -&gt; s!!1 /= 'E' && s!!2 /= 'U' && s!!4 /= 'E' && 'E' `elem` s) candidates ["QQERQ","QQERW","QQERR","QQERY","QQERU","QQERO","QQERP","QQERD","QQERF","QQERG"]</pre> </p> <p> The Boolean expression <code>'E' `elem` s</code> means that the character <code>'E'</code> must be an element of the string (list) <code>s</code>. </p> <p> The same rule applies for <em>U:</em> </p> <p> <pre>&gt; take 10 $ filter (\s -&gt; s!!1 /= 'E' && s!!2 /= 'U' && s!!4 /= 'E' && 'E' `elem` s && 'U' `elem` s) candidates ["QQERU","QWERU","QRERU","QYERU","QUERQ","QUERW","QUERR","QUERY","QUERU","QUERO"]</pre> </p> <p> There's a great suggestion already! The eighth entry spells <em>QUERY!</em> Let's try it: </p> <p> <img src="/content/binary/wordle-tears-inure-query.png" alt="Wordle after entering TEARS, INURE and QUERY."> </p> <p> <em>QUERY</em> was the word of the day! </p> <p> A bit embarrassing that I couldn't think of <em>query</em>, given that I often discuss <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a>. </p> <p> Was that just pure luck? How many suggestions are left in the filtered list? </p> <p> <pre>&gt; length $ filter (\s -&gt; s!!1 /= 'E' && s!!2 /= 'U' && s!!4 /= 'E' && 'E' `elem` s && 'U' `elem` s) candidates 1921</pre> </p> <p> Okay, a bit lucky. If I ask GHCi to display the filtered list in its entirety, no other word jumps out at me, looking like a real word. </p> <h3 id="8c5fd8c64959478bb7834274657af406"> Conclusion <a href="#8c5fd8c64959478bb7834274657af406" title="permalink">#</a> </h3> <p> While I admit that I was a bit lucky that <em>QUERY</em> was among the first ten of 1,921 possible combinations, I still find that applicative combinations are handy as an ad-hoc tool. I'm sure there are more elegant ways to solve a problem like this one, but for me, this approach had <a href="/2019/12/16/zone-of-ceremony">low ceremony</a>. It was a few lines of code in a terminal. Once I had the answer, I could just close the terminal and no further clean-up was required. </p> <p> I'm sure other people have other tool preferences, and perhaps you'd like to <a href="https://github.com/ploeh/ploeh.github.com#comments">leave a comment</a> to the effect that you have a better way with Bash, Python, or APL. That's OK, and I don't mind learning new tricks. </p> <p> I do find this capability of applicative functors to do combinatorics occasionally useful, though. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="22eb88afa50e4193b8f04e82c55df1cb"> <div class="comment-author"><a href="https://github.com/debonte">Erik De Bonte</a> <a href="#22eb88afa50e4193b8f04e82c55df1cb">#</a></div> <div class="comment-content"> <p> It's not "better", but here's a similar approach in Python. </p> <p> <pre>from operator import contains avail = "QWERYUOPDFGHJKLZXCVBM" candidates = ((x,y,z,æ,ø) for x in avail for y in avail for z in avail for æ in "R" for ø in avail) filtered = [s for s in candidates if s[1] != "E" and s[2] != "U" and s[4] != "E" and contains(s, "E") and contains(s, "U")] for candidate in filtered[:10]: print(*candidate, sep="")</pre> </p> </div> <div class="comment-date">2022-01-21 19:08 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Type-safe DI composition https://blog.ploeh.dk/2022/01/10/type-safe-di-composition 2022-01-10T06:41:00+00:00 Mark Seemann <div id="post"> <p> <em>DI Containers aren't type-safe. What are the alternatives?</em> </p> <p> In April 2020 I published an article called <a href="/2020/04/20/unit-bias-against-collections">Unit bias against collections</a>. My goal with the article was to point out a common <a href="https://en.wikipedia.org/wiki/Cognitive_bias">cognitive bias</a>. I just happened to use .NET's built-in DI Container as an example because I'd recently encountered a piece of documentation that exhibited the kind of bias I wanted to write about. </p> <p> This lead to a discussion about the mental model of the DI Container: </p> <blockquote> <p> "Yeah, I wasn't explicit and clarified in a second tweet. I didn't mean in the services example, but in general where it helps if the reader's mental model of the code has 1 item from the collection, because that's how it is in real life. SingleOrDefault would enforce this." </p> <footer><cite><a href="https://twitter.com/bogdangaliceanu/status/1252210213211582477">Bogdan Galiceanu</a></cite></footer> </blockquote> <p> The point made by Bogdan Galiceanu highlights the incongruity between the container's API and the mental model required to work with it. </p> <h3 id="56c89a345b534a7d9cc4f90cd1779b89"> IServiceCollection recap <a href="#56c89a345b534a7d9cc4f90cd1779b89" title="permalink">#</a> </h3> <p> The API in case belongs to <a href="https://docs.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a>, which is little more than a collection of <a href="https://docs.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.servicedescriptor">ServiceDescriptor</a> objects. Each <code>ServiceDescriptor</code> describes a service, as the name implies. </p> <p> Given an <code>IServiceCollection</code> you can, for example, <a href="/2010/09/29/TheRegisterResolveReleasepattern">register</a> an <code>IReservationsRepository</code> instance: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">connStr</span>&nbsp;=&nbsp;Configuration.GetConnectionString(<span style="color:#a31515;">&quot;Restaurant&quot;</span>); services.AddSingleton&lt;IReservationsRepository&gt;(<span style="color:#1f377f;">sp</span>&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">logger</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sp.GetService&lt;ILogger&lt;LoggingReservationsRepository&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">postOffice</span>&nbsp;=&nbsp;sp.GetService&lt;IPostOffice&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;EmailingReservationsRepository( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postOffice, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;LoggingReservationsRepository( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SqlReservationsRepository(connStr))); });</pre> </p> <p> This adds a <code>ServiceDescriptor</code> entry to the collection. (Code examples are from <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.) </p> <p> Later, you can <a href="/2020/04/20/unit-bias-against-collections">remove and replace the service</a> for test purposes: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SelfHostedApi</span>&nbsp;:&nbsp;WebApplicationFactory&lt;Startup&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ConfigureWebHost</span>(IWebHostBuilder&nbsp;<span style="color:#1f377f;">builder</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder.ConfigureServices(<span style="color:#1f377f;">services</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;services.RemoveAll&lt;IReservationsRepository&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;services.AddSingleton&lt;IReservationsRepository&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Here I use <code>RemoveAll</code>, even though I 'know' there's only one service of that type. Bogdan Galiceanu's argument, if I understand it correctly, is that it'd be more honest to use <code>SingleOrDefault</code>, since we 'know' that there's only one such service. </p> <p> I don't bring this up to bash on either Bogdan Galiceanu or the <code>IServiceCollection</code> API, but this exchange of ideas provided another example that <a href="/2012/11/06/WhentouseaDIContainer">DI Containers aren't as helpful as you'd think</a>. While they do provide some services, they require significant mental overhead. You have to 'know' that this service has only one instance, while another service may have two implementations, and so on. As the size of both code base and team grows, keeping all such knowledge in your head becomes increasingly difficult. </p> <p> The promise of object-orientation was always that you <em>shouldn't</em> have to remember implementation details. </p> <p> Particularly with statically typed programming languages you should be able to surface such knowledge as static type information. What would a more honest, statically typed DI Container look like? </p> <h3 id="5c487718a2f84c4c9f68f71871bf15e1"> Statically typed containers <a href="#5c487718a2f84c4c9f68f71871bf15e1" title="permalink">#</a> </h3> <p> Over a series of articles I'll explore how a statically typed DI Container might look: </p> <ul> <li><a href="/2022/01/24/type-level-di-container-prototype">Type-level DI Container prototype</a></li> <li><a href="/2022/01/31/a-type-safe-di-container-c-example">A type-safe DI Container C# example</a></li> <li><a href="/2022/02/07/nested-type-safe-di-containers">Nested type-safe DI Containers</a></li> <li><a href="/2022/02/21/a-type-safe-di-container-as-a-functor">A type-safe DI Container as a functor</a></li> <li><a href="/2022/03/07/a-type-safe-di-container-as-a-tuple">A type-safe DI Container as a tuple</a></li> <li><a href="/2022/03/14/type-safe-di-containers-are-redundant">Type-safe DI Containers are redundant</a></li> </ul> <p> The first of these articles show a <a href="https://www.haskell.org">Haskell</a> prototype, while the rest of the articles use C#. If you don't care about Haskell, you can skip the first article. </p> <p> As the title of the last article implies, this exploration only concludes that type-safe DI Containers are isomorphic to <a href="/2014/06/10/pure-di">Pure DI</a>. I consider Pure DI the simplest of these approaches, suggesting that there's no point in type-safe DI Containers of the kinds shown here. </p> <h3 id="10a5db047d7d4bc1b397214ba6db6137"> Conclusion <a href="#10a5db047d7d4bc1b397214ba6db6137" title="permalink">#</a> </h3> <p> Some people like DI Containers. I don't, because they take away type-safety without providing much benefit to warrant the trade-off. A commonly suggested benefit of DI Containers is lifetime management, but you can <a href="/2014/06/03/compile-time-lifetime-matching">trivially implement type-safe lifetime management with Pure DI</a>. I don't find that argument compelling. </p> <p> This article series examines if it's possible to create a 'better' DI Container by making it more type-safe, but I ultimately conclude that there's no point in doing so. </p> <p> <strong>Next:</strong> <a href="/2022/01/24/type-level-di-container-prototype">Type-level DI Container prototype</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. To ID or not to ID https://blog.ploeh.dk/2022/01/03/to-id-or-not-to-id 2022-01-03T08:57:00+00:00 Mark Seemann <div id="post"> <p> <em>How to model an ID that sometimes must be present, and sometimes not.</em> </p> <p> I'm currently writing a client library for <a href="https://www.criipto.com">Criipto</a> that partially implements the actions available on the <a href="https://fusebit.io/docs/reference/fusebit-http-api/">Fusebit API</a>. This article, however, isn't about the Fusebit API, so even if you don't know what that is, read on. The Fusebit API is just an example. </p> <p> This article, rather, is about how to model the absence or presence of an <code>Id</code> property. </p> <h3 id="76bfa54c15f346689e7210a9e5dc9852"> User example <a href="#76bfa54c15f346689e7210a9e5dc9852" title="permalink">#</a> </h3> <p> The Fusebit API is an HTTP API that, as these things usually do, enables you to create, read, update, and delete resources. One of these is a <em>user</em>. When you create a user, you supply such data as <code>firstName</code>, <code>lastName</code>, and <code>primaryEmail</code>: </p> <p> <pre>POST /v1/account/acc-123/user HTTP/2 authorization: Bearer 938[...] content-type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;firstName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Rhea&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;lastName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Curran&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;primaryEmail&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;recurring@example.net&quot;</span> } HTTP/2 200 content-type: application/json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;usr-8babf0cb95d94e6f&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;firstName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Rhea&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;lastName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Curran&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;primaryEmail&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;recurring@example.net&quot;</span> }</pre> </p> <p> Notice that you're supposed to <code>POST</code> the user representation <em>without</em> an ID. The response, however, contains an updated representation of the resource that now includes an <code>id</code>. The <code>id</code> (in this example <code>usr-8babf0cb95d94e6f</code>) was created by the service. </p> <p> To summarise: when you create a new user, you can't supply an ID, but once the user is created, it does have an ID. </p> <p> I wanted to capture this rule with the <a href="https://fsharp.org">F#</a> type system. </p> <h3 id="2f67ed9643b74c11b12054176540f9ea"> Inheritance <a href="#2f67ed9643b74c11b12054176540f9ea" title="permalink">#</a> </h3> <p> Before we get to the F# code, let's take a detour around some typical C# code. </p> <p> At times, I've seen people address this kind of problem by having two types: <code>UserForCreation</code> and <code>CreatedUser</code>, or something like that. The only difference would be that <code>CreatedUser</code> would have an <code>Id</code> property, whereas <code>UserForCreation</code> wouldn't. While, at this time, the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">rule of three</a> doesn't apply yet, such duplication still seems frivolous. </p> <p> How does an object-oriented programmer address such a problem? By deriving <code>CreatedUser</code> from <code>UserForCreation</code>, of course! </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CreatedUser</span>&nbsp;:&nbsp;UserForCreation { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> I'm not too fond of inheritance, and such a design also comes with a built-in flaw: Imagine a method with the signature <code>public CreatedUser Create(UserForCreation user)</code>. While such an API design clearly indicates that you don't <em>have</em> to supply an ID, you still can. You can call such a <code>Create</code> method with a <code>CreatedUser</code> object, since <code>CreatedUser</code> derives from <code>UserForCreation</code>. </p> <p> <pre>CreatedUser&nbsp;<span style="color:#1f377f;">user</span>&nbsp;=&nbsp;resource.Create(<span style="color:blue;">new</span>&nbsp;CreatedUser { &nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;<span style="color:#a31515;">&quot;123&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;FirstName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Sue&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;LastName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Flay&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;suoffle@example.org&quot;</span> });</pre> </p> <p> Since <code>CreatedUser</code> contains an ID, this seems to suggest that you can call the <code>Create</code> method with a user with an ID. What would you expected from such a possibility? In the above code example, what would you expect the value of <code>user.Id</code> to be? </p> <p> It'd be reasonable to expect <code>user.Id</code> to be <code>"123"</code>. This seems to indicate that it'd be possible to supply a client-generated user ID, which would then be used instead of a server-generated user ID. The HTTP API, however, doesn't allow that. </p> <p> Such a design is misleading. It suggests that <code>CreatedUser</code> can be used where <code>UserForCreation</code> is required. This isn't true. </p> <h3 id="cf4f25d849d7436686852f177cff2578"> Generic user <a href="#cf4f25d849d7436686852f177cff2578" title="permalink">#</a> </h3> <p> I was aware of the above problem, so I didn't even attempt to go there. Besides, I was writing the library in F#, not C#, and while F# enables inheritance as well, it's not the first modelling option you'd typically reach for. </p> <p> Instead, my first attempt was to define user data as a generic record type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;UserData&lt;&#39;a&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;:&nbsp;&#39;a &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FirstName&nbsp;:&nbsp;string&nbsp;option &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastName&nbsp;:&nbsp;string&nbsp;option &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;:&nbsp;MailAddress&nbsp;option &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Identities&nbsp;:&nbsp;Identity&nbsp;list &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Permissions&nbsp;:&nbsp;Permission&nbsp;list &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> (The Fusebit API also enables you to supply <code>Identities</code> and <code>Permissions</code> when creating a user. I omitted them from the above C# example code because this detail is irrelevant to the example.) </p> <p> This enabled me to define an impure action to create a user: </p> <p> <pre><span style="color:green;">//&nbsp;ApiClient&nbsp;-&gt;&nbsp;UserData&lt;unit&gt;&nbsp;-&gt;&nbsp;Task&lt;Result&lt;UserData&lt;string&gt;,&nbsp;HttpResponseMessage&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;create&nbsp;client&nbsp;(userData&nbsp;:&nbsp;UserData&lt;unit&gt;)&nbsp;=&nbsp;task&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;jobj&nbsp;=&nbsp;JObject&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;userData.FirstName &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.iter&nbsp;(<span style="color:blue;">fun</span>&nbsp;fn&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;jobj.[<span style="color:#a31515;">&quot;firstName&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;JValue&nbsp;fn) &nbsp;&nbsp;&nbsp;&nbsp;userData.LastName &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.iter&nbsp;(<span style="color:blue;">fun</span>&nbsp;ln&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;jobj.[<span style="color:#a31515;">&quot;lastName&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;JValue&nbsp;ln) &nbsp;&nbsp;&nbsp;&nbsp;userData.Email &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.iter &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">fun</span>&nbsp;email&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;jobj.[<span style="color:#a31515;">&quot;primaryEmail&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;email&nbsp;|&gt;&nbsp;string&nbsp;|&gt;&nbsp;JValue) &nbsp;&nbsp;&nbsp;&nbsp;jobj.[<span style="color:#a31515;">&quot;identities&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userData.Identities &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.map&nbsp;Identity.toJToken &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.toArray &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;JArray &nbsp;&nbsp;&nbsp;&nbsp;jobj.[<span style="color:#a31515;">&quot;access&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;aobj&nbsp;=&nbsp;JObject&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aobj.[<span style="color:#a31515;">&quot;allow&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userData.Permissions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.map&nbsp;Permission.toJToken &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.toArray &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;JArray &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aobj &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;json&nbsp;=&nbsp;jobj.ToString&nbsp;Formatting.None &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;relativeUrl&nbsp;=&nbsp;Uri&nbsp;(<span style="color:#a31515;">&quot;user&quot;</span>,&nbsp;UriKind.Relative) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;resp&nbsp;=&nbsp;Api.post&nbsp;client&nbsp;relativeUrl&nbsp;json &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;resp.IsSuccessStatusCode &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;content&nbsp;=&nbsp;resp.Content.ReadAsStringAsync&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;jtok&nbsp;=&nbsp;JToken.Parse&nbsp;content &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;createdUser&nbsp;=&nbsp;parseUser&nbsp;jtok &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok&nbsp;createdUser &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">return</span>&nbsp;Error&nbsp;resp&nbsp;}</pre> </p> <p> Where <code>parseUser</code> is defined like this: </p> <p> <pre><span style="color:green;">//&nbsp;JToken&nbsp;-&gt;&nbsp;UserData&lt;string&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;parseUser&nbsp;(jobj&nbsp;:&nbsp;JToken)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uid&nbsp;=&nbsp;jobj.[<span style="color:#a31515;">&quot;id&quot;</span>]&nbsp;|&gt;&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fn&nbsp;=&nbsp;jobj.[<span style="color:#a31515;">&quot;firstName&quot;</span>]&nbsp;|&gt;&nbsp;Option.ofObj&nbsp;|&gt;&nbsp;Option.map&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ln&nbsp;=&nbsp;jobj.[<span style="color:#a31515;">&quot;lastName&quot;</span>]&nbsp;|&gt;&nbsp;Option.ofObj&nbsp;|&gt;&nbsp;Option.map&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;email&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jobj.[<span style="color:#a31515;">&quot;primaryEmail&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.ofObj &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.map&nbsp;(string&nbsp;&gt;&gt;&nbsp;MailAddress) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;uid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FirstName&nbsp;=&nbsp;fn &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastName&nbsp;=&nbsp;ln &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Identities&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Permissions&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Notice that, if we strip away all the noise from the <code>User.create</code> action, it takes a <code>UserData&lt;unit&gt;</code> as input and returns a <code>UserData&lt;string&gt;</code> as output. </p> <p> Creating a value of a type like <code>UserData&lt;unit&gt;</code> seems a little odd: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;user&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FirstName&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;Helen&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastName&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;Back&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;Some&nbsp;(MailAddress&nbsp;<span style="color:#a31515;">&quot;hellnback@example.net&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Identities&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Permissions&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> It gets the point across, though. In order to call <code>User.create</code> you must supply a <code>UserData&lt;unit&gt;</code>, and the only way you can do that is by setting <code>Id</code> to <code>()</code>. </p> <h3 id="6b269340404e4dbeaba049d65886edf2"> Not quite there... <a href="#6b269340404e4dbeaba049d65886edf2" title="permalink">#</a> </h3> <p> In the Fusebit API, however, the <em>user</em> resource isn't the only resource that exhibits the pattern of requiring no ID on creation, but having an ID after creation. Another example is a resource called a <em>client</em>. Adopting the above design as a template, I also defined <code>ClientData</code> as a generic record type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ClientData&lt;&#39;a&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;:&nbsp;&#39;a &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DisplayName&nbsp;:&nbsp;string&nbsp;option &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Identities&nbsp;:&nbsp;Identity&nbsp;list &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Permissions&nbsp;:&nbsp;Permission&nbsp;list &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> In both cases, I also realised that the record types gave rise to <a href="/2018/03/22/functors">functors</a>. A <code>map</code> function turned out to be useful in certain unit tests, so I added such functions as well: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Client&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;c&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;f&nbsp;c.Id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DisplayName&nbsp;=&nbsp;c.DisplayName &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Identities&nbsp;=&nbsp;c.Identities &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Permissions&nbsp;=&nbsp;c.Permissions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> The corresponding <code>User.map</code> function was similar, so I began to realise that I had some boilerplate on my hands. </p> <p> Besides, a type like <code>UserData&lt;'a&gt;</code> seems to indicate that the type argument <code>'a</code> could be anything. The <code>map</code> function implies that as well. In reality, though, the only constructed types you'd be likely to run into are <code>UserData&lt;unit&gt;</code> and <code>UserData&lt;string&gt;</code>. </p> <p> I wasn't quite happy with this design, after all... </p> <h3 id="c74641d5b7fc49849d671f233f77bf37"> Identifiable <a href="#c74641d5b7fc49849d671f233f77bf37" title="permalink">#</a> </h3> <p> After thinking about this, I decided to move the generics around. Instead of making the ID generic, I instead made the payload generic by introducing this container type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Identifiable&lt;&#39;a&gt;&nbsp;=&nbsp;{&nbsp;Id&nbsp;:&nbsp;string;&nbsp;Item&nbsp;:&nbsp;&#39;a&nbsp;}</pre> </p> <p> The <code>User.create</code> action now looks like this: </p> <p> <pre><span style="color:green;">//&nbsp;ApiClient&nbsp;-&gt;&nbsp;UserData&nbsp;-&gt;&nbsp;Task&lt;Result&lt;Identifiable&lt;UserData&gt;,&nbsp;HttpResponseMessage&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;create&nbsp;client&nbsp;userData&nbsp;=&nbsp;task&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;jobj&nbsp;=&nbsp;JObject&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;userData.FirstName &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.iter&nbsp;(<span style="color:blue;">fun</span>&nbsp;fn&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;jobj.[<span style="color:#a31515;">&quot;firstName&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;JValue&nbsp;fn) &nbsp;&nbsp;&nbsp;&nbsp;userData.LastName &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.iter&nbsp;(<span style="color:blue;">fun</span>&nbsp;ln&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;jobj.[<span style="color:#a31515;">&quot;lastName&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;JValue&nbsp;ln) &nbsp;&nbsp;&nbsp;&nbsp;userData.Email &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.iter &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">fun</span>&nbsp;email&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;jobj.[<span style="color:#a31515;">&quot;primaryEmail&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;email&nbsp;|&gt;&nbsp;string&nbsp;|&gt;&nbsp;JValue) &nbsp;&nbsp;&nbsp;&nbsp;jobj.[<span style="color:#a31515;">&quot;identities&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userData.Identities &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.map&nbsp;Identity.toJToken &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.toArray &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;JArray &nbsp;&nbsp;&nbsp;&nbsp;jobj.[<span style="color:#a31515;">&quot;access&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;aobj&nbsp;=&nbsp;JObject&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aobj.[<span style="color:#a31515;">&quot;allow&quot;</span>]&nbsp;<span style="color:blue;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userData.Permissions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.map&nbsp;Permission.toJToken &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.toArray &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;JArray &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aobj &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;json&nbsp;=&nbsp;jobj.ToString&nbsp;Formatting.None &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;relativeUrl&nbsp;=&nbsp;Uri&nbsp;(<span style="color:#a31515;">&quot;user&quot;</span>,&nbsp;UriKind.Relative) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;resp&nbsp;=&nbsp;Api.post&nbsp;client&nbsp;relativeUrl&nbsp;json &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;resp.IsSuccessStatusCode &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;content&nbsp;=&nbsp;resp.Content.ReadAsStringAsync&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;jtok&nbsp;=&nbsp;JToken.Parse&nbsp;content &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;createdUser&nbsp;=&nbsp;parseUser&nbsp;jtok &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok&nbsp;createdUser &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">return</span>&nbsp;Error&nbsp;resp&nbsp;}</pre> </p> <p> Where <code>parseUser</code> is defined as: </p> <p> <pre><span style="color:green;">//&nbsp;JToken&nbsp;-&gt;&nbsp;Identifiable&lt;UserData&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;parseUser&nbsp;(jtok&nbsp;:&nbsp;JToken)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uid&nbsp;=&nbsp;jtok.[<span style="color:#a31515;">&quot;id&quot;</span>]&nbsp;|&gt;&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fn&nbsp;=&nbsp;jtok.[<span style="color:#a31515;">&quot;firstName&quot;</span>]&nbsp;|&gt;&nbsp;Option.ofObj&nbsp;|&gt;&nbsp;Option.map&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ln&nbsp;=&nbsp;jtok.[<span style="color:#a31515;">&quot;lastName&quot;</span>]&nbsp;|&gt;&nbsp;Option.ofObj&nbsp;|&gt;&nbsp;Option.map&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;email&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jtok.[<span style="color:#a31515;">&quot;primaryEmail&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.ofObj &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.map&nbsp;(string&nbsp;&gt;&gt;&nbsp;MailAddress) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ids&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;jtok.[<span style="color:#a31515;">&quot;identities&quot;</span>]&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">null</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;:?&gt;&nbsp;JArray&nbsp;|&gt;&nbsp;Seq.map&nbsp;Identity.parse&nbsp;|&gt;&nbsp;Seq.toList &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;perms&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jtok.[<span style="color:#a31515;">&quot;access&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.ofObj &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.toList &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.collect&nbsp;(<span style="color:blue;">fun</span>&nbsp;j&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j.[<span style="color:#a31515;">&quot;allow&quot;</span>]&nbsp;:?&gt;&nbsp;JArray &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.choose&nbsp;Permission.tryParse &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Seq.toList) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;uid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FirstName&nbsp;=&nbsp;fn &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastName&nbsp;=&nbsp;ln &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Identities&nbsp;=&nbsp;ids &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Permissions&nbsp;=&nbsp;perms &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> The required input to <code>User.create</code> is now a simple, non-generic <code>UserData</code> value, and the successful return value an <code>Identifiable&lt;UserData&gt;</code>. There's no more arbitrary ID data types. The ID is either present as a <code>string</code> or it's absent. </p> <p> You could also turn the <code>Identifiable</code> container into a functor if you need it, but I've found no need for it so far. Wrapping and unwrapping the payload from the container is easy without supporting machinery like that. </p> <p> This design is still reusable. The equivalent <code>Client.create</code> action takes a non-generic <code>ClientData</code> value as input and returns an <code>Identifiable&lt;ClientData&gt;</code> value when successful. </p> <h3 id="9ff91e0a956147c796329551aa91bd1d"> C# translation <a href="#9ff91e0a956147c796329551aa91bd1d" title="permalink">#</a> </h3> <p> There's nothing F#-specific about the above solution. You can easily define <code>Identifiable</code> in C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Identifiable</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Identifiable</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">id</span>,&nbsp;T&nbsp;<span style="color:#1f377f;">item</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;Identifiable&lt;T&gt;&nbsp;<span style="color:#1f377f;">identifiable</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;==&nbsp;identifiable.Id&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;T&gt;.Default.Equals(Item,&nbsp;identifiable.Item); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;HashCode.Combine(Id,&nbsp;Item); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I've here used the explicit <code>class</code>-based syntax to define an immutable class. In C# 9 and later, you can simplify this quite a bit using <code>record</code> syntax instead (which gets you closer to the F# example), but I chose to use the more verbose syntax for the benefit of readers who may encounter this example and wish to understand how it relates to a less specific C-based language. </p> <h3 id="8df883ff44924387aafe519c5e83a178"> Conclusion <a href="#8df883ff44924387aafe519c5e83a178" title="permalink">#</a> </h3> <p> When you need to model interactions where you <em>must not</em> supply an ID on create, but representations have IDs when you query the resources, don't reach for inheritance. Wrap the data in a generic container that contains the ID and a generic payload. You can do this in languages that support parametric polymorphism (AKA <em>generics</em>). </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Label persistent test data with deletion dates https://blog.ploeh.dk/2021/12/27/label-persistent-test-data-with-deletion-dates 2021-12-27T06:34:00+00:00 Mark Seemann <div id="post"> <p> <em>If you don't clean up after yourself, at least enable others to do so.</em> </p> <p> I'm currently developing a software library that interacts with a third-party HTTP API to automate creation of various resources at that third party. While I use automated testing to verify that my code works, I still need to run my automation code against the real service once in while. After all, I'd like to verify that I've correctly interpreted the third party's documentation. </p> <p> I run my tests against a staging environment. The entire purpose of the library is to create resources, so all successful tests leave behind new 'things' in that staging environment. </p> <p> I'm not the only person who's testing against that environment, so all sorts of test entries accumulate. </p> <h3 id="f5fe2c5bcb104f65ad2f2e23ec5eea04"> Test data accretion <a href="#f5fe2c5bcb104f65ad2f2e23ec5eea04" title="permalink">#</a> </h3> <p> More than one developer is working with the third-party staging environment. They create various records in the system for test purposes. Often, they forget about these items once the test is complete. </p> <p> After a few weeks, you have various entries like these: </p> <ul> <li>Foo test Permit Client</li> <li>Fo Permit test client</li> <li>Paul Fo client from ..id</li> <li>Paul Verify Bar Test Client</li> <li>Pauls test</li> <li>SomeClient</li> <li>michael-template-client</li> </ul> <p> Some of these may be used for sustained testing. Others look suspiciously like abandoned objects. </p> <p> Does it matter that stuff like this builds up? </p> <p> Perhaps not, but it bothers my sense of order. If enough stuff builds up, it may also make it harder to find the item you actually need, and rarely, there may be real storage costs associated with the jetsam. But realistically, it just offends my proclivity for tidiness. </p> <h3 id="f83b07e2a587427db7d48e160ec09478"> Label ephemeral objects explicitly <a href="#f83b07e2a587427db7d48e160ec09478" title="permalink">#</a> </h3> <p> While I was testing my library's ability to create new resources, it dawned on me that I could use the records' display names to explicitly label them as temporary. </p> <p> At first, I named the objects like this: </p> <blockquote> Test by Mark Seemann. Delete if older than 10 minutes. </blockquote> <p> While browsing the objects via a web UI (instead of the HTTP API), however, I realised that the creation date wasn't visible in the UI. That makes it hard to identify the actual age. </p> <p> So, instead, I began labelling the items with a absolute time of safe deletion: </p> <blockquote> Test by Mark Seemann. Delete after 2021-11-23T13:13:00Z. </blockquote> <p> I chose to use <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> Zulu time because it's unambiguous. </p> <h3 id="deec7068de60499ab54fe3b9f011b5ed"> Author name <a href="#deec7068de60499ab54fe3b9f011b5ed" title="permalink">#</a> </h3> <p> As you can tell from the above examples, I explicitly named the object <em>Test by Mark Seemann</em>. The word <em>Test</em> indicates to everyone else that this is a test resource. The reason I decided to include my own name was to make it clear to other readers who to contact in case of doubt. </p> <p> While I find a message like <em>Delete after 2021-11-23T13:13:00Z</em> quite clear, you can never predict how other readers will interpret a text. Thus, I left my name in the title to give other people an opportunity to contact me if they have questions about the record. </p> <h3 id="81ea8d84900049a3b7155e22f5dfe06c"> Conclusion <a href="#81ea8d84900049a3b7155e22f5dfe06c" title="permalink">#</a> </h3> <p> This is just a little pleasantry you can use to make life for a development team a little more agreeable. </p> <p> You may not always be able to explicitly label a test item. Some records don't have display names, or the name field is too short to allow a detailed, explicit label. </p> <p> You may also feel that this isn't worth the trouble, and perhaps it isn't. </p> <p> I usually clean up after having added test data, but sometimes one forgets. When working in a shared environment, I find it considerate to clearly label test data to indicate to others when it's safe to delete it. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Changing your organisation https://blog.ploeh.dk/2021/12/20/changing-your-organisation 2021-12-20T06:41:00+00:00 Mark Seemann <div id="post"> <p> <em>Or: How do I convince X to adopt Y in software development?</em> </p> <p> In January 2012 a customer asked for my help with software architecture. The CTO needed to formulate a strategy to deal with increasing demand for bring-your-own-device (BYOD) access to internal systems. Executives brought their iPhones and iPads to work and expected to be able to access and interact with custom-developed and bespoke internal line-of-business applications. </p> <p> Quite a few of these were running on the Microsoft stack, which was the reason Microsoft Denmark had recommended me. </p> <p> I had several meetings with the developers responsible for enabling BYOD. One guy, in particular, kept suggesting a <a href="https://en.wikipedia.org/wiki/Microsoft_Silverlight">Silverlight</a> solution. I pointed out that Silverlight wouldn't run on Apple devices, but he wouldn't be dissuaded. He was <em>certain</em> that this was the correct solution, and I could tell that he became increasingly frustrated with me because he couldn't convince me. </p> <p> We'll return to this story later in this essay. </p> <h3 id="2346cc0e812841c483afd6483bb4b081"> Please convince my manager <a href="#2346cc0e812841c483afd6483bb4b081" title="permalink">#</a> </h3> <p> Sometimes people ask me questions like these: </p> <ul> <li>How do I convince my manager to let me use <a href="https://fsharp.org">F#</a>?</li> <li>How do I convince my team mates to adopt test-driven development?</li> <li>How do I convince the entire team that code quality matters?</li> </ul> <p> Sometimes I receive such questions via email. Sometimes people ask me at conferences or user groups. </p> <p> To quote <a href="https://ericlippert.com">Eric Lippert</a>: <a href="https://ericlippert.com/2012/12/17/performance-rant">Why are you even asking me?</a> </p> <p> To be fair, I do understand why people ask me, but I have few good answers. </p> <h3 id="8e3d3323c2c14cd2b592854140d2bb92"> Why ask me? <a href="#8e3d3323c2c14cd2b592854140d2bb92" title="permalink">#</a> </h3> <p> I suppose people ask me because I've been writing about software improvement for decades. <a href="/">This blog</a> dates back to January 2009, and <a href="http://blogs.msdn.com/ploeh">my previous MSDN blog</a> goes back to January 2006. These two resources alone contain more than 700 posts, the majority of which are about some kind of suggested improvement. Granted, there's also <a href="/2013/02/04/BewareofProductivityTools">the occasional rant</a>, by I think it's fair to say that my main motivation for writing is to share with readers what I think is right and good. </p> <p> I do write a lot about potential improvements to software development. </p> <p> As most readers have discovered, <a href="/dippp">my book about Dependency Injection</a> (with <a href="https://blogs.cuttingedge.it/steven/about/">Steven van Deursen</a>) is more than just a manual to a few Dependency Injection Containers, and my new book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> is full of suggested improvements. </p> <p> Add to that <a href="/pluralsight-courses">my Pluralsight courses</a>, <a href="https://cleancoders.com/videos/humane-code-real">my Clean Coders videos</a>, and <a href="/schedule">my conference talks</a>, and there's a clear pattern to most of my content. </p> <h3 id="b7902ef70c88416886f79138d5e4ee2a"> Maven <a href="#b7902ef70c88416886f79138d5e4ee2a" title="permalink">#</a> </h3> <p> In <a href="http://amzn.to/18sHRmt">The Tipping Point</a> Malcolm Gladwell presents a model for information dissemination, containing three types of people required for an idea to take hold: Connectors, mavens, and salesmen. </p> <p> In that model, a <em>maven</em> is an 'information specialist' - someone who accumulates knowledge and shares it with others. If I resemble any of these three types of people, I'm a maven. I like learning and discovering new ways of doing things, and obviously I like sharing that information. I wouldn't have blogged consistently for sixteen years if I didn't <a href="/2021/03/22/the-dispassionate-developer">feel compelled</a> to do so. </p> <p> My role, as I see it, is to find and discover better ways of writing software. Much of what I write about is something that I've picked up somewhere else, but I try to present it in my own way, and I'm diligent about citing sources. </p> <p> When people ask me concrete questions, like <em>how do I refactor this piece of code?</em> or <em>how do write this in a more functional style?</em>, I present an answer (if I can). </p> <p> The suggestions I make are just that: It's a buffet of possible solutions to certain problems. If you encounter one of my articles and find the proposed technique useful, then that makes me happy and proud. </p> <p> Notice the order of events: Someone has a problem, finds my content, and decides that it looks like a solution. Perhaps (s)he remembers my name. Perhaps (s)he feels that I helped solve a problem. </p> <p> Audience members that come to my conference talks, or readers who buy my books, may not have a concrete problem to solve, but they still voluntarily seeks me out - perhaps because of previous exposure to my content. </p> <p> To reiterate: </p> <ul> <li>You have a problem (concrete or vague)</li> <li>Perhaps you come across my content</li> <li>Perhaps you find it useful</li> </ul> <p> Perhaps you think that I convinced you that 'my way' is best. I didn't. You were already looking for a solution. You were ready for a change. You were open. </p> <h3 id="332746ebf9b249118eaf61d8a52cb2ec"> Salesman <a href="#332746ebf9b249118eaf61d8a52cb2ec" title="permalink">#</a> </h3> <p> When you ask me about how to convince your manager, or your team mates, you're no longer asking me a technical question. Now you're asking about <a href="https://amzn.to/32xc9X5">how to win friends and influence people</a>. You don't need a maven for that; you need a <em>salesman</em>. That's not me. </p> <p> I've had some success applying changes to software organisations, but in all cases, the reason I was there in the first place was because the organisation itself (or members thereof) had asked me to come and help them. When people <em>want</em> your help changing things, convincing them to try something new isn't a hard sell. </p> <p> When I consult development teams, I'm not there to sell them new processes; I'm there to help them use appropriate solutions at opportune moments. </p> <p> I'm not a salesman. Just because I convinced <em>you</em> that, say, property-based testing is a good idea doesn't mean I can convince anyone else. Keep in mind that I probably convinced you because you were ready for a change. </p> <p> Your manager or your team mates may not be. </p> <h3 id="2ce7188e30d64d318293fa17bd6da8dc"> Bide your time <a href="#2ce7188e30d64d318293fa17bd6da8dc" title="permalink">#</a> </h3> <p> While I'm no salesman, I've managed to turn people around from time to time. The best strategy, I've found, is to wait for an opportunity. </p> <p> As long as everything is going well, people aren't ready to change. </p> <blockquote> <p> "Only a crisis - actual or perceived - produces real change. When that crisis occurs, the actions that are taken depend on the ideas that are lying around. That, I believe, is our basic function: to develop alternatives to existing policies, to keep them alive and available until the politically impossible becomes the politically inevitable" </p> <footer><a href="https://amzn.to/3BsIqLG">Milton Friedman</a></footer> </blockquote> <p> If you have solutions at the ready, you may be able to convince your manager or your colleagues to try something new if a crisis occurs. Don't gloat - just present your suggestion: <em>What if we did this instead?</em> </p> <h3 id="3980ee0c37454d00b749c6a3ae51ab21"> Vocabulary <a href="#3980ee0c37454d00b749c6a3ae51ab21" title="permalink">#</a> </h3> <p> Indeed, I'm not a salesman, and while I can't tell you how to <em>sell</em> an idea to an unwilling audience, I <em>can</em> tell you how you can weaken your position: Make it all about you. </p> <p> Notice how questions like the above are often phrased: <em>my manager will not let me...</em> or <em>how do I convince my colleagues?</em> </p> <p> <a href="https://www.goodreads.com/review/show/1313490263">I actually didn't much like <em>How to Win Friends and Influence People</em></a>, but it does present the insight that in order to sway people, you have to consider what's in it for them. </p> <p> I had to be explicitly told this before I learned that lesson. </p> <p> In the second half of the 2000s I was attached to a software development project at a large Danish company. After a code review, I realised that the architecture of the code was <em>all wrong!</em> </p> <p> In order to make the project manager aware of the issue, I wrote an eight-page internal 'white paper' and emailed it to the project manager (let's call him Henk). </p> <p> Nothing happened. No one addressed the problem. </p> <p> A few weeks later, I had a scheduled one-on-one meeting with my own manager in Microsoft, and when asked if I had any issues, I started to complain about this problem I'd identified. </p> <p> My manager looked at me for a moment and asked me: <em>How do you think receiving that email made Henk feel?</em> </p> <p> It had never crossed my mind to think about that. It was <em>my</em> problem! <em>I</em> discovered it! I viewed myself as a great programmer and architect because I had perceived such a complex issue and was able to describe it clearly. It was all about me, me, me. </p> <p> When we programmers ask how to convince our managers to 'let us' use TDD, or FP, or some other 'cool' practice, we're still focused on <em>us</em>. What's in it for the manager? </p> <p> When we talk about code quality and lack thereof, 'ugly code', refactoring, and other such language, a non-coding manager is likely to see us as primadonnas out of touch with reality: <em>We have features to ship, but the programmers only care about making the code 'pretty'.</em> </p> <p> I offer no silver bullet to convince other people that certain techniques are superior, but do consider introducing suggestions by describing the benefits they bring: <em>Wouldn't it be cool if we could decrease our testing phase from three weeks to three days?</em>, <em>Wouldn't it be nice if we could deploy every week?</em>, <em>Wouldn't it be nice if we could decrease the number of errors our users encounter?</em> </p> <h3 id="b98c57c52d06414d8d74d40cfea69056"> You could be wrong <a href="#b98c57c52d06414d8d74d40cfea69056" title="permalink">#</a> </h3> <p> Have you ever felt frustrated that you couldn't convince other people to do it your way, despite <em>knowing</em> that you were right? </p> <p> Recall the developer from the introduction, the one who kept insisting that Silverlight was the right solution to the organisation's BYOD problem. He was clearly convinced that he had the solution, and he was frustrated that I kept rejecting it. </p> <p> We may scoff at such obvious ignorance of facts, but he had clearly drunk the Microsoft Kool-Aid. I could tell, because I'd been there myself. When you're a young programmer, you may easily buy into a compelling narrative. Microsoft evangelists were quite good at their game back then, as I suppose their Apple and Linux counterparts were and are. As an inexperienced developer, you can easily be convinced that a particular technology will solve all problems. </p> <p> When you're young and inexperienced, you can easily feel that you're absolutely right and still be unequivocally wrong. </p> <p> Consider this the next time you push your agenda. Perhaps your manager and colleagues reject your ideas because they're actually bad. A bit of metacognition is often appropriate. </p> <h3 id="bce39fbfe8964742af9e45be2d3752e8"> Conclusion <a href="#bce39fbfe8964742af9e45be2d3752e8" title="permalink">#</a> </h3> <p> How do you convince your manager or team mates to do things better? </p> <p> I don't know; I'm not a salesman, but in this essay, I've nonetheless tried to reflect on the question. I think it helps to consider what the other party gains from accepting a change, but I also think it's a waiting game. You have to be patient. </p> <p> As an economist, I could also say much about incentives, but this essay is already long enough as it is. Still, when you consider how your counterparts react to your suggestions, reflect on how they are incentivised. </p> <p> Even if you do everything right, make the best suggestions at the most opportune times, you may find yourself in a situation systemically rigged against doing the right thing. Ultimately, as <a href="https://martinfowler.com">Martin Fowler</a> quipped, <a href="https://wiki.c2.com/?ChangeYourOrganization">either change your organisation, or change your organisation</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Backwards compatibility as a profunctor https://blog.ploeh.dk/2021/12/13/backwards-compatibility-as-a-profunctor 2021-12-13T07:01:00+00:00 Mark Seemann <div id="post"> <p> <em>In order to keep backwards compatibility, you can weaken preconditions or strengthen postconditions.</em> </p> <p> Like the previous articles on <a href="/2021/11/29/postels-law-as-a-profunctor">Postel's law as a profunctor</a> and <a href="/2021/12/06/the-liskov-substitution-principle-as-a-profunctor">the Liskov Substitution Principle as a profunctor</a>, this article is part of a series titled <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">Some design patterns as universal abstractions</a>. And like the previous articles, it's a bit of a stretch including the present article in that series, since <a href="https://en.wikipedia.org/wiki/Backward_compatibility">backwards compatibility</a> isn't a <a href="https://en.wikipedia.org/wiki/Software_design_pattern">design pattern</a>, but rather a software design principle or heuristic. I still think, however, that the article fits the spirit of the article series, if not the letter. </p> <p> Backwards compatibility is often (but not always) a desirable property of a system. Even in <a href="/2012/12/18/ZookeepersmustbecomeRangers">Zoo software, it pays to explicitly consider versioning</a>. In order to support <a href="https://en.wikipedia.org/wiki/Continuous_delivery">Continuous Delivery</a>, you must be able to evolve a system in such a way that it's always in a working state. </p> <p> When other systems depend on a system, it's important to maintain backwards compatibility. Evolve the system, but support legacy dependents as well. </p> <h3 id="eb3409db1d7f42109355387c2a72c4c4"> Adding new test code <a href="#eb3409db1d7f42109355387c2a72c4c4" title="permalink">#</a> </h3> <p> In <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, chapter 11 contains a subsection on adding new test code. Despite admonitions to the contrary, I often experience that programmers treat test code as a second-class citizen. They casually change test code in a more undisciplined way than they'd edit production code. Sometimes, that may be in order, but I wanted to show that you <em>can</em> approach the task of editing test code in a more disciplined way. After all, <a href="/2013/04/02/why-trust-tests">the more you edit tests, the less you can trust them</a>. </p> <p> After a preliminary discussion about adding entirely new test code, the book goes on to say: </p> <blockquote> <p> You can also append test cases to a parametrised test. If, for example, you have the test cases shown in listing 11.1, you can add another line of code, as shown in listing 11.2. That’s hardly dangerous. </p> <p> <strong>Listing 11.1</strong> A parametrised test method with three test cases. Listing 11.2 shows the updated code after I added a new test case. <em>(Restaurant/b789ef1/Restaurant.RestApi.Tests/ReservationsTests.cs)</em> </p> <p> <pre>[Theory] [InlineData(<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;j@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Jay&nbsp;Xerxes&quot;</span>,&nbsp;1)] [InlineData(<span style="color:#a31515;">&quot;not&nbsp;a&nbsp;date&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;w@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Wk&nbsp;Hd&quot;</span>,&nbsp;8)] [InlineData(<span style="color:#a31515;">&quot;2023-11-30&nbsp;20:01&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;Thora&quot;</span>,&nbsp;19)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">PostInvalidReservation</span>(</pre> </p> <p> <strong>Listing 11.2</strong> A test method with a new test case appended, compared to listing 11.1. The line added is highlighted. <em>(Restaurant/745dbf5/Restaurant.RestApi.Tests/ReservationsTests.cs)</em> </p> <p> <pre>[Theory] [InlineData(<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;j@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Jay&nbsp;Xerxes&quot;</span>,&nbsp;1)] [InlineData(<span style="color:#a31515;">&quot;not&nbsp;a&nbsp;date&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;w@example.edu&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Wk&nbsp;Hd&quot;</span>,&nbsp;8)] [InlineData(<span style="color:#a31515;">&quot;2023-11-30&nbsp;20:01&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:#a31515;">&quot;Thora&quot;</span>,&nbsp;19)] <strong>[InlineData(<span style="color:#a31515;">&quot;2022-01-02&nbsp;12:10&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3&nbsp;Beard&quot;</span>,&nbsp;0)]</strong> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">PostInvalidReservation</span>(</pre> </p> <p> You can also add assertions to existing tests. Listing 11.3 shows a single assertion in a unit test, while listing 11.4 shows the same test after I added two more assertions. </p> <p> <strong>Listing 11.3</strong> A single assertion in a test method. Listing 11.4 shows the updated code after I added more assertions. <em>(Restaurant/36f8e0f/Restaurant.RestApi.Tests/ReservationsTests.cs)</em> </p> <p> <pre>Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;HttpStatusCode.InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;response.StatusCode);</pre> </p> <p> <strong>Listing 11.4</strong> Verification phase after I added two more assertions, compared to listing 11.3. The lines added are highlighted. <em>(Restaurant/0ab2792/Restaurant.RestApi.Tests/ReservationsTests.cs)</em> </p> <p> <pre>Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;HttpStatusCode.InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;response.StatusCode); <strong>Assert.NotNull(response.Content); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">content</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;response.Content.ReadAsStringAsync(); Assert.Contains( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;tables&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;content, &nbsp;&nbsp;&nbsp;&nbsp;StringComparison.OrdinalIgnoreCase);</strong></pre> </p> <p> These two examples are taken from a test case that verifies what happens if you try to overbook the restaurant. In listing 11.3, the test only verifies that the HTTP response is <code>500 Internal Server Error</code>. The two new assertions verify that the HTTP response includes a clue to what might be wrong, such as the message <code>No tables available</code>. </p> <p> I often run into programmers who’ve learned that a test method may only contain a single assertion; that having multiple assertions is called Assertion Roulette. I find that too simplistic. You can view appending new assertions as a strengthening of postconditions. With the assertion in listing 11.3 any <code>500 Internal Server Error</code> response would pass the test. That would include a 'real' error, such as a missing connection string. This could lead to false negatives, since a general error could go unnoticed. </p> <p> Adding more assertions strengthens the postconditions. Any old <code>500 Internal Server Error</code> will no longer do. The HTTP response must also come with content, and that content must, at least, contain the string <code>"tables"</code>. </p> <p> This strikes me as reminiscent of the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>. There are many ways to express it, but in one variation, we say that subtypes may weaken preconditions and strengthen postconditions, but not the other way around. You can think of of subtyping as an ordering, and you can think of time in the same way, as illustrated by figure 11.1. Just like a subtype depends on its supertype, a point in time 'depends' on previous points in time. Going forward in time, you’re allowed to strengthen the postconditions of a system, just like a subtype is allowed to strengthen the postcondition of a supertype. </p> <p> <img src="/content/binary/type-hierarchy-and-time.png" alt="A type hierarchy forms a directed graph, as indicated by the arrow from subtype to supertype. Time, too, forms a directed graph, as indicated by the arrow from t2 to t1. Both present a way to order elements."> </p> <p> <strong>Figure 11.1</strong> A type hierarchy forms a directed graph, as indicated by the arrow from <em>subtype</em> to <em>supertype</em>. Time, too, forms a directed graph, as indicated by the arrow from <em>t<sub>2</sub></em> to <em>t<sub>1</sub></em>. Both present a way to order elements. </p> <p> Think of it another way, adding new tests or assertions is fine; deleting tests or assertions would weaken the guarantees of the system. You probably don’t want that; herein lie regression bugs and breaking changes. </p> </blockquote> <p> The book leaves it there, but I find it worthwhile to expand on that thought. </p> <h3 id="a1d529a2fbd04af4a9a2a08bc3c17bed"> Function evolution over time <a href="#a1d529a2fbd04af4a9a2a08bc3c17bed" title="permalink">#</a> </h3> <p> As in the previous articles about <em>x as a profunctor</em>, let's first view 'a system' as a function. As I've repeatedly suggested, <a href="/2018/01/22/function-isomorphisms">with sufficient imagination, every operation looks like a function</a>. Even an HTTP <code>POST</code> request, as suggested in the above test snippets, can be considered a function, albeit one with <a href="/2020/06/08/the-io-container">the IO effect</a>. </p> <p> You can <a href="/2021/11/22/functions-as-pipes">envision a function as a pipe</a>. In previous articles, I've drawn horizontal pipes, with data flowing from left to right, but we can also rotate them 90° and place them on a timeline: </p> <p> <img src="/content/binary/function-pipes-on-timeline.png" alt="Function pipes on a timeline."> </p> <p> As usually depicted in Western culture, time moves from left to right. In a stable system, functions don't change: The function at <em>t<sub>1</sub></em> is equal to the function at <em>t<sub>2</sub></em>. </p> <p> The function in the illustration takes values belonging to the set <em>a</em> as input and returns values belonging to the set <em>b</em> as output. A bit more formally, we can denote the function as having the type <code>a -&gt; b</code>. </p> <p> We can view the passing of time as a translation of the function <code>a -&gt; b</code> at <em>t<sub>1</sub></em> to <code>a -&gt; b</code> at <em>t<sub>2</sub></em>. If we just leave the function alone (as implied by the above figure), it corresponds to mapping the function with the identity function. </p> <p> Clients that rely on the function are calling it by supplying input values from the set <em>a</em>. In return, they receive values from the set <em>b</em>. As already discussed in the article about <a href="/2021/11/29/postels-law-as-a-profunctor">Postel's law as a profunctor</a>, we can illustrate such a fit between client and function as snugly fitting pipes: </p> <p> <img src="/content/binary/function-pipes-and-clients-on-timeline.png" alt="Function pipes and clients on a timeline."> </p> <p> As long as the clients keep supplying elements from <em>a</em> and expecting elements from <em>b</em> in return, the function remains compatible. </p> <p> If we have to change the function, which kind of change will preserve compatibility? </p> <p> We can make the function accept a wider set of input, and let it return narrower set of output: </p> <p> <img src="/content/binary/function-pipe-flanged-pipe-and-clients-on-timeline.png" alt="Function pipe, flanged pipe, and clients on a timeline."> </p> <p> This will not break any existing clients, because they'll keep calling the function with <em>a</em> input and expecting <em>b</em> output values. The drawing is similar to the drawings from the articles on <a href="/2021/11/29/postels-law-as-a-profunctor">Postel's law as a profunctor</a> and <a href="/2021/12/06/the-liskov-substitution-principle-as-a-profunctor">The Liskov Substitution Principle as a profunctor</a>. It seems reasonable to consider backwards compatibility in the same light. </p> <h3 id="29913e188d87465c897cfaf96b3de349"> Profunctor <a href="#29913e188d87465c897cfaf96b3de349" title="permalink">#</a> </h3> <p> Consider backwards compatible function evolution as a mapping of a function <code>a -&gt; b</code> at <em>t<sub>1</sub></em> to <code>a' -&gt; b'</code> at <em>t<sub>2</sub></em>. </p> <p> What rules should we institute for this mapping? </p> <p> In order for this translation to be backwards compatible, we must be able to translate the larger input set <code>a'</code> to <code>a</code>; that is: <code>a' -&gt; a</code>. That's the top flange in the above figure. </p> <p> Likewise, we must be able to translate the original output set <code>b</code> to the smaller <code>b'</code>: <code>b -&gt; b'</code>. That's the bottom nozzle in the above figure. </p> <p> Thus, armed with the two functions <code>a' -&gt; a</code> and <code>b -&gt; b'</code>, we can translate <code>a -&gt; b</code> at <em>t<sub>1</sub></em> to <code>a' -&gt; b'</code> at <em>t<sub>2</sub></em> in a way that preserves backwards compatibility. More formally: </p> <p> <pre>(a' -&gt; a) -&gt; (b -&gt; b') -&gt; (a -&gt; b) -&gt; (a' -&gt; b')</pre> </p> <p> This is exactly the definition of <code>dimap</code> for <a href="/2021/11/08/reader-as-a-profunctor">the Reader profunctor</a>! </p> <h3 id="f9fb3a037ed34eeeaea489cb45b830f6"> Arrow directions <a href="#f9fb3a037ed34eeeaea489cb45b830f6" title="permalink">#</a> </h3> <p> That's why I wrote as I did in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. The direction of the arrows in the book's <em>figure 11.1</em> may seem counter-intuitive, but I had them point in that direction because that's how most readers are used to see supertypes and subtypes depicted. </p> <p> When thinking of concepts such as <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>, it may be more intuitive to think of the <a href="/2021/11/01/profunctors">profunctor</a> as a mapping from a formal specification <code>a -&gt; b</code> to the more robust implementation <code>a' -&gt; b'</code>. That is, the arrow would point in the other direction. </p> <p> Likewise, when we think of <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">the Liskov Substitution Principle</a> as rule about how to lawfully derive subtypes from supertypes, again we have a mapping from the supertype <code>a -&gt; b</code> to the subtype <code>a' -&gt; b'</code>. Again, the arrow direction goes from supertype to subtype - that is, in the opposite direction from the book's <em>figure 11.1</em>. </p> <p> This now also better matches how we intuitively think about time, as flowing from left to right. The arrow, again, goes from <em>t<sub>1</sub></em> to <em>t<sub>2</sub></em>. </p> <p> Most of the time, the function doesn't change as time goes by. This corresponds to the mapping <code>dimap id id</code> - that is, applying the identity function to the mapping. </p> <h3 id="ff48c88ef49b438f8a0085c16ce11442"> Implications for tests <a href="#ff48c88ef49b438f8a0085c16ce11442" title="permalink">#</a> </h3> <p> Consider the test snippets shown at the start of the article. When you add test cases to an existing test, you increase the size of the input set. Granted, unit test inputs are only samples of the entire input set, but it's still clear that adding a test case increases the input set. Thus, we can view such an edit as a mapping <code>a -&gt; a'</code>, where <code>a ⊂ a'</code>. </p> <p> Likewise, when you add more assertions to an existing set of assertions, you add extra constraints. Adding an assertion implies that the test must pass <em>all</em> of the previous assertions, <em>as well as</em> the new one. That's a Boolean <em>and</em>, which implies a narrowing of the allowed result set (unless the new assertion is a <a href="/2019/10/14/tautological-assertion">tautological assertion</a>). Thus, we can view adding an assertion as a mapping <code>b -&gt; b'</code>, where <code>b' ⊂ b</code>. </p> <p> This is why it's okay to add more test cases, and more assertions, to an existing test, whereas you should be weary of the opposite: It may imply (or at least allow) a breaking change. </p> <h3 id="f8cfe2b42dff43a388b3cd5da9746f09"> Conclusion <a href="#f8cfe2b42dff43a388b3cd5da9746f09" title="permalink">#</a> </h3> <p> As <a href="https://michaelfeathers.silvrback.com/the-universality-of-postel-s-law">Michael Feathers observed</a>, Postel's law seems universal. That's one way to put it. </p> <p> Another way to view it is that Postel's law is a formulation of a kind of profunctor. And it certainly seems as though profunctors pop up here, there, and everywhere, once you start looking for that idea. </p> <p> We can think of the Liskov Substitution Principle as a profunctor, and backwards compatibility as well. It seems reasonable enough: In order to stay backwards compatible, a function can become more tolerant of input, or more conservative in what it returns. Put another way: Contravariant in input, and covariant in output. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Liskov Substitution Principle as a profunctor https://blog.ploeh.dk/2021/12/06/the-liskov-substitution-principle-as-a-profunctor 2021-12-06T06:52:00+00:00 Mark Seemann <div id="post"> <p> <em>With a realistic example in C#.</em> </p> <p> Like the previous article on <a href="/2021/11/29/postels-law-as-a-profunctor">Postel's law as a profunctor</a>, this article is part of a series titled <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">Some design patterns as universal abstractions</a>. And like the previous article, it's a bit of a stretch to include the present article in that series, since the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> (LSP) isn't a <a href="https://en.wikipedia.org/wiki/Software_design_pattern">design pattern</a>, but rather a software design principle or heuristic. I still think, however, that the article fits the spirit of the article series, if not the letter. </p> <p> As was also the case for the previous article, I don't claim that any of this is new. <a href="https://twitter.com/runarorama/status/1425946258234032130">Michael Feathers and Rúnar Bjarnason blazed that trail long before me</a>. </p> <h3 id="95f8226160af4ba7a62e5d8f7d4da5c5"> LSP distilled <a href="#95f8226160af4ba7a62e5d8f7d4da5c5" title="permalink">#</a> </h3> <p> In more or less natural language, the LSP states that subtypes must preserve correctness. A subtype isn't allowed to change behaviour in such a way that client code breaks. </p> <p> Note that subtypes <em>are</em> allowed to change behaviour. That's often the point of subtyping. By providing a subtype, you can change the behaviour of a system. You can, for example, override how messages are sent, so that an SMS becomes a Slack notification or a Tweet. </p> <p> If client code 'originally' supplied correct input for sending an SMS, this input should also be valid for posting a Tweet. </p> <p> Specifically (paraphrasing <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">the Wikipedia entry as of early November 2021</a>): </p> <p> <ul> <li>Subtypes must be contravariant in input</li> <li>Subtypes must be covariant in output</li> <li>Preconditions must not be strengthened in the subtype</li> <li>Postconditions must not be weakened in the subtype</li> </ul> </p> <p> There's a bit more, but in this article, I'll focus on those rules. The first two we already know. Since <a href="/2021/11/08/reader-as-a-profunctor">any function is already a profunctor</a>, we know that functions are contravariant in input and covariant in output. </p> <p> The LSP, however, isn't a rule about a single function. Rather, it's a rule about a family of functions. Think about a function <code>a -&gt; b</code> <a href="/2021/11/22/functions-as-pipes">as a pipe</a>. You can replace the pipe segment with another pipe segment that has exactly the same shape, but replacing it with a flanged pipe also works, as long as the input flange is wider, and the nozzle narrower than the original pipe shape. </p> <p> <img src="/content/binary/lsp-pipes.png" alt="The Liskov Substitution Principle illustrated with flanged pipes."> </p> <p> On the other hand, if you narrow the pipe at the input and widen it at the output, spillage will happen. That's essentially what the LSP states: The upper, green, flanged pipe is a good replacement for the supertype (the blue pipe in the middle), while the lower, orange, flanged pipe is not useful. </p> <p> The previous article already described that visual metaphor when it comes to co- and contravariance, so this article will focus on pre- and postconditions. My conjecture is that this is just another take on co- and contravariance. </p> <h3 id="73b14971495b4701a322f95ff3f332a0"> Supertype example <a href="#73b14971495b4701a322f95ff3f332a0" title="permalink">#</a> </h3> <p> When encountering statements about subtypes and supertypes, most people tend to think about object inheritance, but that's just one example. As I've <a href="/2021/10/25/functor-variance-compared-to-cs-notion-of-variance">previously outlined</a>, anything that can 'act as' something else is a subtype of that something else. Specifically, an interface implementation is a subtype of the interface, and the interface itself the supertype. </p> <p> Consider, as a supertype example, this interface from my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;Reservation?&gt;&nbsp;<span style="color:#74531f;">ReadReservation</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Guid&nbsp;<span style="color:#1f377f;">id</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Update</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Delete</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Guid&nbsp;<span style="color:#1f377f;">id</span>); }</pre> </p> <p> Specifically, in this article, I'll focus exclusively on the <code>ReadReservations</code> method. You can imagine that there's an interface with only that method, or that when subtyping the interface in the following, we vary only that method and keep everything else fixed. </p> <p> What might be the pre- and postconditions of the <code>ReadReservations</code> method? </p> <h3 id="18ffff3d0ba7474086053e283d3bd77b"> ReadReservations preconditions <a href="#18ffff3d0ba7474086053e283d3bd77b" title="permalink">#</a> </h3> <p> The most basic kind of precondition is captured by the parameter types. In order to be able to call the method, you must supply an <code>int</code> and two <code>DateTime</code> instances. You can't omit any of them or replace one of the <code>DateTime</code> values with a <code>Guid</code>. In a statically typed language, this is obvious, and the compiler will take care of that. </p> <p> Both <code>int</code> and <code>DateTime</code> are value types (<code>struct</code>s), so they can't be null. Had one of the parameters been a reference type, it'd be appropriate to consider whether or not null constitutes valid input. </p> <p> So far, we've only discussed static types. Of course a subtype must satisfy the compiler, but what other pre-conditions might be implied by <code>ReadReservations</code>? </p> <p> The purpose of the method is to enable client code to query a data store and retrieve the reservations for a particular restaurant and in a particular time interval. </p> <p> Is any <code>restaurantId</code> acceptable? <code>1</code>? <code>0</code>? <code>-235</code>? </p> <p> It's probably a distraction that the restaurant ID is even an <code>int</code>. After all, you don't add IDs together, or multiply one with another one. That an ID is an integer is really just a leaky implementation detail - databases like it best when IDs are integers. I should actually have defined the restaurant ID as an opaque object with <a href="https://martinfowler.com/bliki/ValueObject.html">Value Object</a> semantics, but I didn't (like other humans, I'm imperfect and lazy). The bottom line is that any number is as good as any other number. No precondition there. </p> <p> What about the two <code>DateTime</code> parameters? Are <code>DateTime.MinValue</code> or <code>DateTime.MaxValue</code> valid values? Probably: If you'd like to retrieve all reservations in the past, you could ask for <code>DateTime.MinValue</code> as <code>min</code> and <code>DateTime.Now</code> as <code>max</code>. On the other hand, it'd be reasonable to require that <code>min</code> should be less than (or equal to?) <code>max</code>. That sounds like a proper precondition, and one that's difficult to express as a type. </p> <p> We may also consider it a precondition that the object that implements the <code>ReadReservations</code> method is properly initialised, but I'll take that as a given. Making sure of that is the responsibility of the constructor, not the <code>ReadReservations</code> method. </p> <p> To summarise, apart from the types and other things handled by the compiler, there's only one additional pre-condition: that <code>min</code> must be less than <code>max</code>. </p> <p> Are there any postconditions? </p> <h3 id="e1588e7f3cb7406f8a15b286edfd570e"> ReadReservations postconditions <a href="#e1588e7f3cb7406f8a15b286edfd570e" title="permalink">#</a> </h3> <p> The code base for the book obeys <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command-Query Separation</a>. Since <code>ReadReservations</code> returns data, it must be a Query. Thus, we can assume that calling it isn't supposed to change state. Thus, postconditions can only be statements about the return value. </p> <p> Again, static typing takes care of the most fundamental postconditions. An implementation can't return a <code>double</code> or an <code>UriBuilder</code>. It must be a <code>Task</code>, and that task must compute an <code>IReadOnlyCollection&lt;Reservation&gt;</code>. </p> <p> Why <code>IReadOnlyCollection</code>, by the way? That's my attempt at describing a particular postcondition as a type. </p> <p> The <a href="https://docs.microsoft.com/dotnet/api/system.collections.generic.ireadonlycollection-1">IReadOnlyCollection&lt;T&gt;</a> interface is a restriction of <code>IEnumerable&lt;T&gt;</code> that adds a <code>Count</code>. By adding the <code>Count</code> property, the interface strongly suggests that the collection is finite. </p> <p> <code>IEnumerable&lt;T&gt;</code> implementations can be infinite sequences. These can be useful as functional alternatives to infinite loops, but are clearly not appropriate when retrieving reservations from a database. </p> <p> The use of <code>IReadOnlyCollection</code> tells us about a postcondition: The collection of reservations is finite. This is, however, captured by the type. Any valid implementation of the interface ought to make that guarantee. </p> <p> Is there anything else? Is it okay if the collection is empty? Yes, that could easily happen, if you have no reservations in the requested interval. </p> <p> What else? Not much comes to mind, only that we'd expect the collection to be 'stable'. Technically, you could implement the <code>GetEnumerator</code> method so that it generates <code>Count</code> random <code>Reservation</code> objects every time you enumerate it, but none of the built-in implementations do that; that's quite a low bar, as postconditions go. </p> <p> To summarise the postconditions: None, apart from a well-behaved implementation of <code>IReadOnlyCollection&lt;Reservation&gt;</code>. </p> <h3 id="1b02cb07547e405b86e2d42d889b7bef"> SQL implementation <a href="#1b02cb07547e405b86e2d42d889b7bef" title="permalink">#</a> </h3> <p> According to the LSP, a subtype should be allowed to weaken preconditions. Keep in mind that I consider an interface implementation a subtype, so every implementation of <code>ReadReservations</code> constitutes a subtype. Consider the SQL Server-based implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">result</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;List&lt;Reservation&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">conn</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlConnection(ConnectionString); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">cmd</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlCommand(readByRangeSql,&nbsp;conn); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@RestaurantId&quot;</span>,&nbsp;restaurantId); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Min&quot;</span>,&nbsp;min); &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Max&quot;</span>,&nbsp;max); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;conn.OpenAsync().ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">rdr</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;cmd.ExecuteReaderAsync().ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">while</span>&nbsp;(<span style="color:blue;">await</span>&nbsp;rdr.ReadAsync().ConfigureAwait(<span style="color:blue;">false</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.Add(ReadReservationRow(rdr)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;result.AsReadOnly(); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;readByRangeSql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;[PublicId],&nbsp;[At],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity] &nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;[dbo].[Reservations] &nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;[RestaurantId]&nbsp;=&nbsp;@RestaurantId&nbsp;AND &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Min&nbsp;&lt;=&nbsp;[At]&nbsp;AND&nbsp;[At]&nbsp;&lt;=&nbsp;@Max&quot;</span>;</pre> </p> <p> This implementation actually doesn't enforce the precondition that <code>min</code> ought to be less than <code>max</code>. It doesn't have to, since the code will run even if that's not the case - the result set, if <code>min</code> is greater than <code>max</code>, will always be empty. </p> <p> While perhaps not useful, weakening this precondition doesn't adversely affect well-behaved clients, and buggy clients are always going to receive empty results. If this implementation also fulfils all postconditions, it's already LSP-compliant. </p> <p> Still, could it weaken preconditions even more, or in a different way? </p> <h3 id="a7427e15a9614179939911f25959e093"> Weakening of preconditions <a href="#a7427e15a9614179939911f25959e093" title="permalink">#</a> </h3> <p> As <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> suggests, a method should be <em>liberal in what it accepts</em>. If it understands 'what the caller meant', it should perform the desired operation instead of insisting on the letter of the law. </p> <p> Imagine that you receive a call where <code>min</code> is midnight June 6 and <code>max</code> is midnight June 5. While wrong, what do you think that the caller 'meant'? </p> <p> The caller probably wanted to retrieve the reservations for June 5. </p> <p> You could weaken that precondition by swapping back <code>min</code> and <code>max</code> if you detect that they've been swapped. </p> <p> Let's assume, for the sake of argument, that we make the above <code>ReadReservations</code> implementation <code>virtual</code>. This enables us to inherit from <code>SqlReservationsRepository</code> and override the method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(max&nbsp;&lt;&nbsp;min) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">base</span>.ReadReservations(restaurantId,&nbsp;max,&nbsp;min); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">base</span>.ReadReservations(restaurantId,&nbsp;min,&nbsp;max); }</pre> </p> <p> While this weakens preconditions, it breaks no existing clients because they all 'know' that they must pass the lesser value before the greater value. </p> <h3 id="1e73ce9509ed4004a9c26c23ea186e7c"> Strengthening of postconditions <a href="#1e73ce9509ed4004a9c26c23ea186e7c" title="permalink">#</a> </h3> <p> Postel's law also suggests that a method should be <em>conservative in what it sends</em>. What could that mean? </p> <p> In the case of <code>ReadReservations</code>, you may notice that the result set isn't explicitly sorted. Perhaps we'd like to also sort it on the date and time: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>, &nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">query</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min&nbsp;&lt;&nbsp;max&nbsp;? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">base</span>.ReadReservations(restaurantId,&nbsp;min,&nbsp;max)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">base</span>.ReadReservations(restaurantId,&nbsp;max,&nbsp;min); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;query.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;reservations.OrderBy(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.At).ToList(); }</pre> </p> <p> This implementation retains the weakened precondition from before, but now it also explicitly sorts the reservations on <code>At</code>. </p> <p> Since no client code relies on sorting, this breaks no existing clients. </p> <p> While the behaviour changes, it does so in a way that doesn't violate the original contract. </p> <h3 id="e254e40ed8634bdeb144ac4f6711409c"> Profunctor <a href="#e254e40ed8634bdeb144ac4f6711409c" title="permalink">#</a> </h3> <p> While we've used terms such as <em>weaken preconditions</em> and <em>strengthen postconditions</em>, doesn't this look an awful lot like co- and contravariance? </p> <p> I think it does, so let's rewrite the above implementation using the <a href="/2021/11/08/reader-as-a-profunctor">Reader profunctor</a>. </p> <p> First, we'll need to express the original method in the shape of a function like <code>a -&gt; b</code> - that is: a function that takes a single input and returns a single output. While <code>ReadReservations</code> return a single value (a <code>Task</code>), it takes three input arguments. To make it fit the <code>a -&gt; b</code> mould, we have to <a href="/2018/01/29/argument-list-isomorphisms">convert those three parameters to a Parameter Object</a>. </p> <p> This enables us to write the original implementation as a function: </p> <p> <pre>Func&lt;QueryParameters,&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&gt;&nbsp;<span style="color:#1f377f;">imp</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">q</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">base</span>.ReadReservations(q.RestaurantId,&nbsp;q.Min,&nbsp;q.Max);</pre> </p> <p> If we didn't want to weaken any preconditions or strengthen any postconditions, we could simply call <code>imp</code> and return its output. </p> <p> The above weakened precondition can be expressed like this: </p> <p> <pre>Func&lt;QueryParameters,&nbsp;QueryParameters&gt;&nbsp;<span style="color:#1f377f;">pre</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">q</span>&nbsp;=&gt;&nbsp;q.Min&nbsp;&lt;&nbsp;q.Max&nbsp;? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;QueryParameters(q.RestaurantId,&nbsp;q.Min,&nbsp;q.Max)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;QueryParameters(q.RestaurantId,&nbsp;q.Max,&nbsp;q.Min);</pre> </p> <p> Notice that this is a function from <code>QueryParameters</code> to <code>QueryParameters</code>. As above, it simply swaps <code>Min</code> and <code>Max</code> if required. </p> <p> Likewise, we can express the strengthened postcondition as a function: </p> <p> <pre>Func&lt;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;,&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&gt;&nbsp;<span style="color:#1f377f;">post</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;t.Select(<span style="color:#1f377f;">rs</span>&nbsp;=&gt;&nbsp;rs.OrderBy(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;r.At).ToReadOnly());</pre> </p> <p> The <code>Select</code> method exists because <code>Task</code> forms an <a href="/2018/09/24/asynchronous-functors">asynchronous functor</a>. </p> <p> It's now possible to compose <code>imp</code> with <code>pre</code> and <code>post</code> using <code>DiMap</code>: </p> <p> <pre>Func&lt;QueryParameters,&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&gt;&nbsp;<span style="color:#1f377f;">composition</span>&nbsp;=&nbsp;imp.DiMap(pre,&nbsp;post);</pre> </p> <p> You can now call <code>composition</code> with the original arguments: </p> <p> <pre>composition(<span style="color:blue;">new</span>&nbsp;QueryParameters(restaurantId,&nbsp;min,&nbsp;max))</pre> </p> <p> The output of such a function call is entirely equivalent to the above, subtyped <code>ReadReservations</code> implementation. </p> <p> In case you've forgotten, the presence of a lawful <code>DiMap</code> function is what makes something a profunctor. We <a href="/2021/11/08/reader-as-a-profunctor">already knew that functions are profunctors</a>, but now we've also seen that we can use this knowledge to weaken preconditions and strengthen postconditions. </p> <p> It seems reasonable to conjecture that the LSP actually describes a profunctor. </p> <p> It seems to me that the profunctor composition involved with the LSP always takes the specialised form where, for a function <code>a -&gt; b</code>, the preprocessor (the contravariant mapping) always takes the form <code>a -&gt; a</code>, and the postprocessor (the covariant mapping) always takes the form <code>b -&gt; b</code>. This is because polymorphism must preserve the shape of the original function (<code>a -&gt; b</code>). </p> <h3 id="bca3373857e246a280bce24c83f2e8ac"> Conclusion <a href="#bca3373857e246a280bce24c83f2e8ac" title="permalink">#</a> </h3> <p> We already know that something contravariant in input and covariant in output is a profunctor candidate, but the Liskov Substitution Principle is usually expressed in terms of pre- and postconditions. Subtypes may weaken preconditions and strengthen postconditions, but not the other way around. </p> <p> Evidence suggests that you can phrase these rules as a profunctor specialisation. </p> <p> <strong>Next:</strong> <a href="/2021/12/13/backwards-compatibility-as-a-profunctor">Backwards compatibility as a profunctor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="79c6999370fd4c1f955c6f2c243d23b0"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#79c6999370fd4c1f955c6f2c243d23b0">#</a></div> <div class="comment-content"> <blockquote> It seems to me that the profunctor composition involved with the LSP always takes the specialised form where, for a function <code>a -&gt; b</code>, the preprocessor (the contravariant mapping) always takes the form <code>a -&gt; a</code>, and the postprocessor (the covariant mapping) always takes the form <code>b -&gt; b</code>. This is because polymorphism must preserve the shape of the original function (<code>a -&gt; b</code>). </blockquote> <p> I think this depends on the language and your perspective on subtype polymorphism. In Java, a subclass <code>Y</code> of a superclass <code>X</code> can override a method <code>m</code> on <code>X</code> that has return type <code>b</code> with a method on <code>Y</code> that has return type <code>c</code> provided <code>c</code> is a subtype of <code>b</code>. To be clear, I am not saying that the instance returned from <code>Y::m</code> can have runtime-type <code>c</code>, though that is also true. I am saying that the compile-time return type of <code>Y::m</code> can be <code>c</code>. </p> <p> With this in mind, I am willing to argue that the postprocessor can take the form <code>b -&gt; c</code> provided <code>c</code> is a subtype of <code>b</code>. As the programmer, that is how I think of <code>Y::m</code>. And yet, if someone has a instance of <code>Y</code> with compile-time type <code>X</code>, then calling <code>m</code> returns something with compile-time type <code>b</code> by composing my <code>b -&gt; c</code> function with the natural <code>c -&gt; b</code> function from subtype polymorphism to get the <code>b -&gt; b</code> function that you suggested is required. </p> </div> <div class="comment-date">2021-12-10 05:28 UTC</div> </div> <div class="comment" id="c4279163948e4fda98899fa54a6cfc16"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c4279163948e4fda98899fa54a6cfc16">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Yes, true, that nuance may exist. This implies that the LSP is recursive, which really isn't surprising. </p> <p> A function <code>a -&gt; b</code> may be defined for types <code>a</code> and <code>b</code>, where both are, themselves, polymorphic. So, if <code>a</code> is <a href="https://docs.microsoft.com/dotnet/api/system.httpstyleuriparser">HttpStyleUriParser</a> and <code>b</code> is <a href="https://docs.microsoft.com/dotnet/api/system.reflection.memberinfo">MemberInfo</a>, we'd have a function <code>Func&lt;HttpStyleUriParser, MemberInfo&gt;</code> (or <code>HttpStyleUriParser -&gt; MemberInfo</code> if we want to stick with an ML-style type signature - we can imagine that it's an <a href="https://fsharp.org">F#</a> function). </p> <p> If <code>Func&lt;HttpStyleUriParser, MemberInfo&gt;</code> is our polymorphic target signature, according to <a href="/2021/10/25/functor-variance-compared-to-cs-notion-of-variance">C# co- and contravariance</a>, we're allowed to vary both input and output. We can, for example, widen the input type to <a href="https://docs.microsoft.com/dotnet/api/system.uriparser">UriParser</a> and restrict the output type to <a href="https://docs.microsoft.com/dotnet/api/system.type">Type</a>. Thus, a <code>specific</code> function of the type <code>Func&lt;UriParser, Type&gt;</code> is compatible with a general function of the type <code>Func&lt;HttpStyleUriParser, MemberInfo&gt;</code>: </p> <p> <pre>Func&lt;HttpStyleUriParser,&nbsp;MemberInfo&gt;&nbsp;<span style="color:#1f377f;">general</span>&nbsp;=&nbsp;specific;</pre> </p> <p> Thus, you're correct that both pre- and postprocessor may take the form <code>a' -> a</code> and <code>b -> b'</code>, where <code>a'</code> is a supertype of <code>a</code>, and <code>b</code> is a supertype of <code>b'</code>. This is true for C# function delegates, since <a href="/2021/10/25/functor-variance-compared-to-cs-notion-of-variance">they're defined with the <code>in</code> and <code>out</code> keywords</a>. You can put <code>in</code> and <code>out</code> on your own interface definitions as well, but most people don't bother (I rarely do, either). </p> <p> As with all practical recursion you need base cases to stop the recursion. While you can define polymorphic functions (or classes) where input parameters and return types are themselves polymorphic, etc., these should still obey the LSP. </p> </div> <div class="comment-date">2021-12-14 07:12 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Postel's law as a profunctor https://blog.ploeh.dk/2021/11/29/postels-law-as-a-profunctor 2021-11-29T07:10:00+00:00 Mark Seemann <div id="post"> <p> <em>When viewing inputs and outputs as sets, Postel's law looks like a profunctor.</em> </p> <p> This article is part of a series titled <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">Some design patterns as universal abstractions</a>. Including the present article in that series is a bit of a stretch, since <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> isn't really a <a href="https://en.wikipedia.org/wiki/Software_design_pattern">design pattern</a>, but rather a software design principle or heuristic. I still think, however, that the article fits the spirit of the article series, if not the letter. </p> <p> This article is heavily inspired by <a href="https://www.r7krecon.com/">Michael Feathers'</a> article <a href="https://michaelfeathers.silvrback.com/the-universality-of-postel-s-law">The Universality of Postel's Law</a>, in which he writes: </p> <blockquote> <p> [Postel's law] has been paraphrased over the years as “Be liberal in what you accept, and conservative in what you send” and for people who are mathematically inclined: “be contravariant in your inputs and covariant in your outputs.” </p> <footer><cite><a href="https://michaelfeathers.silvrback.com/the-universality-of-postel-s-law">Michael Feathers</a></cite></footer> </blockquote> <p> A thing contravariant in input and covariant in output sounds like a <a href="/2021/11/01/profunctors">profunctor</a>, but why does Michael Feathers write that about Postel's law? </p> <p> In this article, I'll try to explain. </p> <h3 id="31fb7a0153d24cd9bdc5d7f467488726"> Perfect fit <a href="#31fb7a0153d24cd9bdc5d7f467488726" title="permalink">#</a> </h3> <p> Postel's law is a statement about functions, methods, procedures, or whatever else you'd like to call them. As I've previously outlined, with sufficient squinting, <a href="/2018/01/22/function-isomorphisms">we can think about methods and other operations as functions</a>, so in this article I'll focus on functions. </p> <p> Functions don't stand alone. Functions have callers. Some other entity, usually <em>client code</em>, passes some input data to the function, which then performs its work and returns output data. When viewed with a set-based perspective, we can <a href="/2021/11/22/functions-as-pipes">depict a function as a pipe</a>: </p> <p> <img src="/content/binary/horizontal-pipe.png" alt="Horizontal pipe."> </p> <p> Client code often use the output of one function as input for another: </p> <p> <pre>Int3&nbsp;<span style="color:#1f377f;">isEven</span>&nbsp;=&nbsp;EncodeEven(number); Int3&nbsp;<span style="color:#1f377f;">decremented</span>&nbsp;=&nbsp;Decrement(isEven);</pre> </p> <p> Even though this code example uses an explicit intermediary variable (<code>isEven</code>), it's equivalent to function composition: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">composition</span>&nbsp;=&nbsp;EncodeEven.Compose(Decrement);</pre> </p> <p> where <code>Compose</code> can be implemented as: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;A,&nbsp;C&gt;&nbsp;<span style="color:#74531f;">Compose</span>&lt;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">B</span>,&nbsp;<span style="color:#2b91af;">C</span>&gt;(<span style="color:blue;">this</span>&nbsp;Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">f</span>,&nbsp;Func&lt;B,&nbsp;C&gt;&nbsp;<span style="color:#1f377f;">g</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;g(f(x)); }</pre> </p> <p> Such a composition we can depict by appending one pipe after another: </p> <p> <img src="/content/binary/two-composed-horizontal-pipes.png" alt="Two horizontal pipes composed one after the other."> </p> <p> This works, but is brittle. It's a close fit. The output set of the first function has to exactly fit the input set of the second function. What happens if the pipes don't perfectly align? </p> <h3 id="8e059f4a27d4496887a7b35399dbdcc1"> Misalignment <a href="#8e059f4a27d4496887a7b35399dbdcc1" title="permalink">#</a> </h3> <p> Functions compose when they fit perfectly, but in the real world, that's rarely the case. For example, it may turn out that <code>Decrement</code> is defined like this: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;Int3&nbsp;<span style="color:#74531f;">Decrement</span>(Int3&nbsp;<span style="color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(i&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(i), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Can&#39;t&nbsp;decrement&nbsp;0.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;i&nbsp;-&nbsp;(Int3)1; }</pre> </p> <p> This function is undefined for <code>0</code>. If we wanted to peek at the set diagram 'inside' the pipe, we might depict the function like this: </p> <p> <img src="/content/binary/partial-decrement-set-diagram.png" alt="Decrement function set diagram."> </p> <p> In a sense, it's still a mapping from <a href="/2021/11/22/functions-as-pipes">our hypothetical 3-bit integer</a> to 3-bit integer, but it's a partial function. </p> <p> Another way to depict the mapping, however, is to constrain the <a href="https://en.wikipedia.org/wiki/Domain_of_a_function">domain</a> to <code>[1..7]</code>, and narrow the <a href="https://en.wikipedia.org/wiki/Codomain">codomain</a> to the function's <a href="https://en.wikipedia.org/wiki/Image_(mathematics)">image</a>, producing a <a href="https://en.wikipedia.org/wiki/Bijection">bijection</a>: </p> <p> <img src="/content/binary/total-decrement-set-diagram.png" alt="Total decrement function set diagram."> </p> <p> Such sets are a little harder to express in code, because how do you represent a set with seven elements? Often, you'd stick with an implementation like the above <code>Decrement</code> function. </p> <p> This turns out to be unfortunate, however, because <code>EncodeEven</code> is defined like this: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;Int3&nbsp;<span style="color:#74531f;">EncodeEven</span>(Int3&nbsp;<span style="color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;i.IsEven&nbsp;?&nbsp;(Int3)1&nbsp;:&nbsp;(Int3)0; }</pre> </p> <p> As a set diagram, we might depict it like this: </p> <p> <img src="/content/binary/encode-even-set-diagram.png" alt="Set diagram for the EncodeEven function."> </p> <p> It turns out that half the inputs into the above composition don't work! It's almost as though the pipes are misaligned: </p> <p> <img src="/content/binary/misaligned-pipes.png" alt="Misaligned pipes."> </p> <p> This can easily happen, also in the real world: </p> <p> <img src="/content/binary/misaligned-drain-pipes.jpg" alt="Misaligned drain pipes."> </p> <p> This is also why Michael Feathers writes: </p> <blockquote> <p> We can see Postel in the physical world too. Every time you see a PVC pipe with a flanged end, you’re seeing something that serves as a decent visual metaphor for Postel’s Law. Those pipes fit well together because one end is more accepting. </p> <footer><cite><a href="https://michaelfeathers.silvrback.com/the-universality-of-postel-s-law">Michael Feathers</a></cite></footer> </blockquote> <p> In other words, there's nothing new in any of the above. I've just been supplying the illustrations. </p> <h3 id="fc4420f7ca184f7ca90224ace0db59c3"> Flanges <a href="#fc4420f7ca184f7ca90224ace0db59c3" title="permalink">#</a> </h3> <p> How should we interpret the idea of <em>flanges?</em> How do we illustrate them? Here's a way: </p> <p> <img src="/content/binary/flanged-pipe.png" alt="Flanged pipe."> </p> <p> Given our set-based interpretation of things, how should we interpret a flange? Let's isolate one of them. It doesn't matter which one, but lets consider the left flange. If we attempt to make it transparent, we could also draw it like this: </p> <p> <img src="/content/binary/transparent-flange.png" alt="Transparent flange."> </p> <p> What does that look like? It looks like a mapping from one set to another. </p> <p> The left-hand set is slightly larger than the right-hand set, but the illustration includes neither the elements of each set nor the arrows that connect them. </p> <p> If we think of the 'original' function as a function from the set <em>A</em> to the set <em>B</em> we can also write it in pseudo-code as <code>A -&gt; B</code>. In <a href="https://www.haskell.org">Haskell</a> you'd exactly write <code>A -&gt; B</code> if <code>A</code> and <code>B</code> were two concrete types. Polymorphically, though, you'd write any function as <code>a -&gt; b</code>, or in C# as <code>Func&lt;A, B&gt;</code>. </p> <p> Let's think of any function <code>a -&gt; b</code> as the 'perfect fit' case. While such a function composes with, say, a function <code>b -&gt; c</code>, the composition is brittle. It can easily become misaligned. </p> <p> How do we add flanges to the function <code>a -&gt; b</code>? </p> <p> As the above illustration of the flange implies, we can think of the flange as another function. Perhaps we should call the slightly larger set to the left <code>a<sup>+</sup></code> (since it's 'like' <code>a</code>, just larger - that is, more liberal). With that nomenclature, the flange would be a function <code>a<sup>+</sup> -&gt; a</code>. </p> <p> Likewise, the right flange would be a function <code>b -&gt; b<sup>-</sup></code>. Here, I've called the narrower set of the flange <code>b<sup>-</sup></code> because it's smaller (more conservative) than <code>b</code>. </p> <p> Thus, the flanged pipe is just the composition of these three functions: <code>a<sup>+</sup> -&gt; a</code>, <code>a -&gt; b</code>, and <code>b -&gt; b<sup>-</sup></code>: </p> <p> <img src="/content/binary/flange-composition.png" alt="Flange composition."> </p> <p> That's exactly how <code>dimap</code> is defined in Haskell: </p> <p> <code>dimap ab cd bc = cd . bc . ab</code> </p> <p> The implementation code uses other letters, and recall that Haskell is typically read from right to left. As its name implies, <code>ab</code> is a function <code>a -&gt; b</code>, <code>bc</code> is a function <code>b -&gt; c</code>, and <code>cd</code> is a function <code>c -&gt; d</code>. </p> <p> In other words, Postel's law is a description of <a href="/2021/11/08/reader-as-a-profunctor">the Reader profunctor</a>, or, as Michael Feathers put it: <em>Be contravariant in your inputs and covariant in your outputs.</em> </p> <h3 id="2886607b42c14f8484ff36a41d339549"> Conclusion <a href="#2886607b42c14f8484ff36a41d339549" title="permalink">#</a> </h3> <p> Postel's law is a useful design principle to keep in mind. Intuitively, it makes sense to think of it as making sure that pipes are flanged. The bigger the receiving flange is, and the smaller the nozzle is, the easier it is to compose the flanged pipe with other (flanged) pipes. </p> <p> Using mostly visual metaphor, this article demonstrates that this is equivalent with being contravariant in input and covariant in output, and thus that the principle describes a profunctor. </p> <p> Postel's law, however, isn't the only design principle describing a profunctor. </p> <p> <strong>Next:</strong> <a href="/2021/12/06/the-liskov-substitution-principle-as-a-profunctor">The Liskov Substitution Principle as a profunctor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functions as pipes https://blog.ploeh.dk/2021/11/22/functions-as-pipes 2021-11-22T06:30:00+00:00 Mark Seemann <div id="post"> <p> <em>A visual metaphor.</em> </p> <p> A recent article on <a href="/2021/11/15/types-as-sets">types as sets</a> briefly touched on functions as sets. For example, you can think of Boolean negation as a set of two arrows: </p> <p> <img src="/content/binary/not-function-set-diagram.png" alt="Boolean negation set diagram."> </p> <p> Here the arrows stay within the set, because the function is a function from the set of Boolean values to the set of Boolean values. </p> <p> In general, however, functions aren't always <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphisms</a>. They often map one set of values to another set. How can we think of this as sets? </p> <p> As was also the case with the article on types as sets, I'd like to point out that this article isn't necessarily mathematically rigorous. I'm neither a computer scientist nor a mathematician, and while I try to be as correct as possible, some hand-waving may occur. My purpose with this article isn't to prove a mathematical theorem, but rather to suggest what I, myself, find to be a useful metaphor. </p> <h3 id="ce1ac3b7b2bd49b1a7bf60591606017e"> Boolean negation visualised with domain and codomain <a href="#ce1ac3b7b2bd49b1a7bf60591606017e" title="permalink">#</a> </h3> <p> Instead of visualising Boolean negation within a single set, we can also visualise it as a mapping of elements from an input set (the <em>domain</em>) to an output set (the <em>codomain</em>): </p> <p> <img src="/content/binary/not-function-from-domain-to-codomain.png" alt="Boolean negation illustrated as arrows from the set on the left to the set on the right."> </p> <p> In this figure, the <a href="https://en.wikipedia.org/wiki/Domain_of_a_function">domain</a> is to the left and the <a href="https://en.wikipedia.org/wiki/Codomain">codomain</a> is on the right. </p> <p> How do we visualise more complex functions? What if the domain isn't the same as the codomain? </p> <h3 id="eb9ee0e778134e2d8e2c39a09ab76300"> Boolean and <a href="#eb9ee0e778134e2d8e2c39a09ab76300" title="permalink">#</a> </h3> <p> Let's proceed slowly. Let's consider Boolean <em>and</em> next. While this function still involves only Boolean values, it combines two Boolean values into a single Boolean value. It'd seem, then, that in order to visualise this mapping, we'd need two sets to the left, and one to the right. But perhaps a better option is to visualise the domain as pairs of Boolean values: </p> <p> <img src="/content/binary/boolean-and-function-with-domain-and-codomain.png" alt="Boolean and visualised as arrows between sets."> </p> <p> To the left, we have four pairs that each map to the Boolean values on the right. </p> <h3 id="19cc86cf04ae461f936f9f492fe972bd"> Even numbers <a href="#19cc86cf04ae461f936f9f492fe972bd" title="permalink">#</a> </h3> <p> Perhaps using only Boolean values is misleading. Even when dealing with pairs, the above example may fail to illustrate that we can think of any function as a mapping from a domain to a codomain. </p> <p> Imagine that there's such a thing as a 3-bit number. Such a data structure would be able to represent eight different numbers. To be clear, I'm only 'inventing' such a thing as a 3-bit number because it's still manageable to draw a set of eight elements. </p> <p> How would we illustrate a function that returns <em>true</em> if the input is an even 3-bit number, and <em>false</em> otherwise? We could make a diagram like this: </p> <p> <img src="/content/binary/even-function-diagram-with-domain-and-codomain.png" alt="Set diagram of a function to determine whether a 3-bit number is even."> </p> <p> On the left-hand side, I've only labelled two of the numbers, but I'm sure you can imagine that the rest of the elements are ordered from top to bottom: <em>0, 1, 2, 3, 4, 5, 6, 7</em>. To the right, the two elements are <em>true</em> (top) and <em>false</em> (bottom). </p> <p> Given this illustration, I'm sure you can extrapolate to 32-bit integers and so on. It's impractical to draw, but the concept is the same. </p> <h3 id="15f67395fdf24c79945cb1e2b08c1625"> Encoding <a href="#15f67395fdf24c79945cb1e2b08c1625" title="permalink">#</a> </h3> <p> So far, we've only looked at functions where the codomain is the set of Boolean values. How does it look with other codomains? </p> <p> We could, for example, imagine a function that 'encodes' Boolean values as 3-bit numbers, <em>false</em> corresponding (arbitrarily) to <em>5</em> and <em>true</em> to <em>2</em>. The diagram for that function looks like this: </p> <p> <img src="/content/binary/boolean-encoding-as-3-bit-number.png" alt="Set diagram of encoding Boolean values as 3-bit numbers."> </p> <p> Now the set of Boolean values is on the left, with <em>true</em> on top. </p> <h3 id="d51e56d3e298439c95abb398a5183efc"> A function as a pipe <a href="#d51e56d3e298439c95abb398a5183efc" title="permalink">#</a> </h3> <p> In all examples, we have the domain to the left and the codomain on the right, connected with arrows. Sometimes, however, we may want to think about a function as just a single thing, without all the details. After all, the diagrams in this article are simple only because the examples are toy examples. Even a simple function like this one would require a huge diagram: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">IsPrime</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">candidate</span>)</pre> </p> <p> An input type of <code>int</code> corresponds to a set with 4,294,967,296 elements. That's a big set to draw, and a lot of arrows, too! </p> <p> So perhaps, instead, we'd like to have a way to illustrate a function without all the details, yet still retaining this set-based way of thinking. </p> <p> Let's return to one of the earlier examples, but add a shape around it all: </p> <p> <img src="/content/binary/even-function-diagram-in-a-pipe.png" alt="Set diagram of even function enclosed in pipe."> </p> <p> This seems like a fair addition to make. It starts to look like the function is enclosed in a pipe. Let's make the pipe opaque: </p> <p> <img src="/content/binary/horizontal-pipe.png" alt="Horizontal pipe."> </p> <p> In architecture diagrams, a horizontal pipe is a common way to illustrate that some sort of data processing takes place, so this figure should hardly be surprising. </p> <h3 id="81976b18ccaa40c4af2866681cd2c3bb"> Composition <a href="#81976b18ccaa40c4af2866681cd2c3bb" title="permalink">#</a> </h3> <p> You may say that I've been cheating. After all, in a figure like the one that illustrates an <code>isEven</code> function, the domain is larger than the codomain, yet I've kept both ovals of the same size. Wouldn't the following be a fairer depiction? </p> <p> <img src="/content/binary/even-function-diagram-with-smaller-codomain.png" alt="Set diagram where the codomain is drawn smaller."> </p> <p> If we try to enclose this diagram in an opaque pipe, it'd look like this: </p> <p> <img src="/content/binary/horizontal-cone.png" alt="Horizontal cone."> </p> <p> This mostly looks like a (bad) perspective drawing of a pipe, but it does start to suggest how functions fit together. For example, the output of this <code>isEven</code> function is the Boolean set, which is also the input of, for example the Boolean negation function (<code>!</code> or <code>not</code>). This means that if the shapes fit together, we can compose the pipes: </p> <p> <img src="/content/binary/horizontal-cone-continued-with-pipe.png" alt="Horizontal cone composed with horizontal pipe."> </p> <p> Continuing this line of thinking, we can keep on composing the shapes as long as the output fits the input of the next function. For example, the output of Boolean negation is still the Boolean set, which is also the domain of the above 'encoding' function: </p> <p> <img src="/content/binary/cone-pipe-cone.png" alt="Narrowing cone composed with pipe and widening cone."> </p> <p> We can even, if we'd like to peek into the composition, make the pipes transparent again, to illustrate what's going on: </p> <p> <img src="/content/binary/transparent-cone-pipe-cone.png" alt="Transparent narrowing cone composed with transparent pipe and transparent widening cone."> </p> <p> As long as the right-hand side of one pipe fits the left-hand side of another pipe, it indicates that you can compose these two functions. </p> <h3 id="ec35e44891d24e87bb57e38225ba3f5a"> Haskell translation <a href="#ec35e44891d24e87bb57e38225ba3f5a" title="permalink">#</a> </h3> <p> For completeness' sake, let's try to express these three functions, as well as their composition, in a programming language. Since <a href="https://www.haskell.org">Haskell</a> already comes with a composition operator (<code>.</code>), it fits nicely. It also already comes with two of the three functions we'll need: </p> <p> <pre><span style="color:#2b91af;">even</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> <span style="color:#2b91af;">not</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Bool</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span></pre> </p> <p> Thanks to Haskell's type-class feature, <code>even</code> works for any <code>Integral</code> instance, so if we imagine that our hypothetical 3-bit number is an <code>Integral</code> instance, it'll work for that type of input as well. </p> <p> The remaining function is trivial to implement: </p> <p> <pre><span style="color:#2b91af;">encode</span>&nbsp;::&nbsp;<span style="color:blue;">Num</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a encode&nbsp;&nbsp;True&nbsp;=&nbsp;2 encode&nbsp;False&nbsp;=&nbsp;5</pre> </p> <p> Since <code>Integral</code> is a supertype of <code>Num</code>, if our 3-bit number is an <code>Integral</code> instance, it's also a <code>Num</code> instance. </p> <p> The composition implied by the above figure is this: </p> <p> <pre><span style="color:#2b91af;">composition</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a composition&nbsp;=&nbsp;encode&nbsp;.&nbsp;<span style="color:blue;">not</span>&nbsp;.&nbsp;<span style="color:blue;">even</span></pre> </p> <p> Haskell is typically read from right to left, so this composition starts with <code>even</code>, continues with <code>not</code>, and concludes with <code>encode</code>. </p> <p> Let's call it: </p> <p> <pre>&gt; composition 3 2 &gt; composition 4 5</pre> </p> <p> <code>composition 3</code> first passes through <code>even</code>, which returns <code>False</code>. <code>False</code> then passes through <code>not</code>, which returns <code>True</code>. Finally, <code>True</code> passes through <code>encode</code>, which returns <code>2</code>. </p> <p> I'll leave the exegesis of <code>composition 4</code> as an exercise for the reader. </p> <h3 id="6ece689713714da8adacda590fa3dc7d"> C# translation <a href="#6ece689713714da8adacda590fa3dc7d" title="permalink">#</a> </h3> <p> In C#, imagine that an <code>Int3</code> data type exists. You can now define the three functions like this: </p> <p> <pre>Func&lt;Int3,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">isEven</span>&nbsp;=&nbsp;<span style="color:#1f377f;">number</span>&nbsp;=&gt;&nbsp;number.IsEven; Func&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">not</span>&nbsp;=&nbsp;<span style="color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;!b; Func&lt;<span style="color:blue;">bool</span>,&nbsp;Int3&gt;&nbsp;<span style="color:#1f377f;">encode</span>&nbsp;=&nbsp;<span style="color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;(Int3)2&nbsp;:&nbsp;(Int3)5;</pre> </p> <p> Given a <code>Compose</code> extension method on <code>Func&lt;A, B&gt;</code>, you can now compose the functions like this: </p> <p> <pre>Func&lt;Int3,&nbsp;Int3&gt;&nbsp;<span style="color:#1f377f;">composition</span>&nbsp;=&nbsp;isEven.Compose(not).Compose(encode);</pre> </p> <p> This composition works just like the above Haskell example, and produces the same results. </p> <h3 id="82b6205678464b8489e661774fb2412f"> Conclusion <a href="#82b6205678464b8489e661774fb2412f" title="permalink">#</a> </h3> <p> A function takes input and returns output. Even if the function takes multiple arguments, we <a href="/2018/01/29/argument-list-isomorphisms">can think of an argument as a single object</a>: a tuple or Parameter Object. </p> <p> Thus, we can think of a function as a mapping from one set to another. While we can illustrate a specific mapping (such as <code>even</code>, <code>not</code>, and <code>encode</code>), it's often <a href="/2021/07/28/referential-transparency-fits-in-your-head">useful to think of a function as a single abstract thing</a>. When we think of functions as mappings from sets to sets, it makes intuitive sense to visualise the abstraction as a pipe. </p> <p> This visual metaphor works for object-oriented programming as well. With sufficient mental gymnastics, <a href="/2018/01/22/function-isomorphisms">functions are isomorphic to methods</a>, so the pipe metaphor works beyond <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types as sets https://blog.ploeh.dk/2021/11/15/types-as-sets 2021-11-15T06:37:00+00:00 Mark Seemann <div id="post"> <p> <em>To a certain degree, you can think of static types as descriptions of sets.</em> </p> <p> If you've ever worked with C#, Java, <a href="https://fsharp.org">F#</a>, <a href="https://www.haskell.org">Haskell</a>, or other compiled languages, you've encountered <em>static types</em> in programming. In school, you've probably encountered basic <a href="https://en.wikipedia.org/wiki/Set_theory">set theory</a>. The two relate to each other in illuminating ways. </p> <p> To be clear, I'm neither a mathematician nor a computer scientist, so I'm only basing this article on my layman's understanding of these topics. Still, I find some of the correspondences to be useful when thinking about certain programming topics. </p> <h3 id="3d5852c69bd04cea82693033796f7a59"> Two elements <a href="#3d5852c69bd04cea82693033796f7a59" title="permalink">#</a> </h3> <p> What's the simplest possible set? For various definitions of <em>simple</em>, probably the empty set. After that? The singleton set. We'll skip these, and instead start with a set with two elements: </p> <p> <img src="/content/binary/two-element-set.png" alt="Set with two elements."> </p> <p> If you had to represent such a set in code, how would you do it? </p> <p> First, you'd have to figure out how to distinguish the two elements from each other. Giving each a label seems appropriate. What do you call them? Yin and yang? Church and state? Alice and Bob? </p> <p> And then, how do you represent the labels in code, keeping in mind that they somehow must 'belong to the same set'? Perhaps as an <code>enum</code>? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">enum</span>&nbsp;<span style="color:#2b91af;">Dualism</span> { &nbsp;&nbsp;&nbsp;&nbsp;Yin, &nbsp;&nbsp;&nbsp;&nbsp;Yang }</pre> </p> <p> As a data definition, though, an <code>enum</code> is a poor choise because the underlying data type is an integer (<code>int</code> by default). Thus, a method like this compiles and executes just fine: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Dualism&nbsp;<span style="color:#74531f;">YinOrYang</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;(Dualism)42; }</pre> </p> <p> The <code>Dualism</code> returned by the function is neither <code>Yin</code> nor <code>Yang</code>. </p> <p> So, how do you represent a set with two elements in code? One option would be to <a href="/2018/05/22/church-encoding">Church encode</a> it (try it! It's a good exercise), but perhaps you find something like the following simpler: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Dualism</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Dualism&nbsp;&nbsp;Yin&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Dualism(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Dualism&nbsp;Yang&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Dualism(&nbsp;<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Dualism</span>(<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">isYang</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsYang&nbsp;=&nbsp;isYang; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsYang&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsYin&nbsp;=&gt;&nbsp;!IsYang; }</pre> </p> <p> With this design, a method like <code>YinOrYang</code> can't cheat, but must return either <code>Yin</code> or <code>Yang</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Dualism&nbsp;<span style="color:#74531f;">YinOrYang</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Dualism.Yin; }</pre> </p> <p> Notice that this variation is based entirely off a single member: <code>IsYang</code>, which is a Boolean value. </p> <p> In fact, this implementation is isomorphic to <code>bool</code>. You can create a <code>Dualism</code> instance from a <code>bool</code>, and you can convert that instance back to a Boolean value via <code>IsYang</code>, without loss of information. </p> <p> This holds for any two-element set: it's isomorphic to <code>bool</code>. You could say that the data type <code>bool</code> is equivalent to 'the' two-element set. </p> <h3 id="dc3052aa0c574a3684a7a295b765ce80"> More, but still few, elements <a href="#dc3052aa0c574a3684a7a295b765ce80" title="permalink">#</a> </h3> <p> Is there a data type that corresponds to a three-element set? Again, you can always use Church encoding to describe a data type with three cases, but in C#, the easiest backing type would probably be <code>bool?</code> (<code>Nullable&lt;bool&gt;</code>). When viewed as a set, it's a set inhabited by the three values <code>false</code>, <code>true</code>, and <code>null</code>. </p> <p> How about a set with four elements? A pair of Boolean values seems appropriate: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Direction</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Direction&nbsp;North&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Direction(<span style="color:blue;">false</span>,&nbsp;<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Direction&nbsp;South&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Direction(<span style="color:blue;">false</span>,&nbsp;&nbsp;<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Direction&nbsp;&nbsp;East&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Direction(&nbsp;<span style="color:blue;">true</span>,&nbsp;<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Direction&nbsp;&nbsp;West&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Direction(&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Direction</span>(<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">isEastOrWest</span>,&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">isLatter</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsEastOrWest&nbsp;=&nbsp;isEastOrWest; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsLatter&nbsp;=&nbsp;isLatter; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsEastOrWest&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsLatter&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> The <code>Direction</code> class is backed by two Boolean values. We can say that a four-element set is isomorphic to a pair of Boolean values. </p> <p> How about a five-element set? If a four-element set corresponds to a pair of Boolean values, then perhaps a pair of one Boolean value and one <code>Nullable&lt;bool&gt;</code>? </p> <p> Alas, that doesn't work. When combining types, the number of possible combinations is the product, not the sum, of individual types. So, a pair of <code>bool</code> and <code>bool?</code> would support 2&nbsp;×&nbsp;3&nbsp;=&nbsp;6 combinations: <code>(false, null)</code>, <code>(false, false)</code>, <code>(false, true)</code>, <code>(true, null)</code>, <code>(true, false)</code>, and <code>(true, true)</code>. </p> <p> Again, a Church encoding (try it!) is an option, but you could also do something like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Spice</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Spice&nbsp;&nbsp;&nbsp;Posh&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Spice(0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Spice&nbsp;&nbsp;Scary&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Spice(1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Spice&nbsp;&nbsp;&nbsp;Baby&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Spice(2); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Spice&nbsp;Sporty&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Spice(3); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Spice&nbsp;Ginger&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Spice(4); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Spice</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">id</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.id&nbsp;=&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;Spice&nbsp;<span style="color:#1f377f;">spice</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;==&nbsp;spice.id; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;HashCode.Combine(id); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This seems like cheating, since it uses a much larger underlying data type (<code>int</code>), but it still captures the essence of a set with five elements. You can't add more elements, or initialise the class with an out-of-range <code>id</code> since the constructor is private. </p> <p> The point isn't so much how to implement particular classes, but rather that if you can enumerate all possible values of a type, you can map a type to a set, and vice versa. </p> <h3 id="fa41b9eb292947728ad3bd7d1ff3ff3c"> More elements <a href="#fa41b9eb292947728ad3bd7d1ff3ff3c" title="permalink">#</a> </h3> <p> Which type corresponds to this set? </p> <p> <img src="/content/binary/256-element-set.png" alt="A 256-element set."> </p> <p> I hope that by now, it's clear that this set could correspond to infinitely many types. It's really only a matter of what we call the elements. </p> <p> If we call the first element <code>0</code>, the next one <code>1</code>, and then <code>2</code>, <code>3</code>, and so on up to <code>255</code>, the set corresponds to an unsigned byte. If we call the elements <code>-128</code>, <code>-127</code>, <code>-126</code> up to <code>127</code>, the set corresponds to a signed byte. They are, however, isomorphic. </p> <p> Likewise, a set with 65,536 elements corresponds to a 16-bit integer, and so on. This also holds for 32-bit integers, 64-bit integers, and even floating point types like <code>float</code> and <code>double</code>. These sets are just too large to draw. </p> <h3 id="f3ebcbb8b5844dcaaa0dbff01b68a571"> Infinite sets <a href="#f3ebcbb8b5844dcaaa0dbff01b68a571" title="permalink">#</a> </h3> <p> While most languages have built-in number types based on a fixed number of bytes, some languages also come with number types that support arbitrarily large numbers. .NET has <a href="https://docs.microsoft.com/dotnet/api/system.numerics.biginteger">BigInteger</a>, <a href="https://www.haskell.org">Haskell</a> comes with <code>Integer</code>, and so on. These numbers aren't truly infinite, but are limited by machine capacity rather than the data structure used to represent them in memory. </p> <p> Another example of a type with arbitrary size is the ubiquitous <code>string</code>. There's no strict upper limit to how large strings you can create, although, again, your machine will ultimately run out of memory or disk space. </p> <p> Theoretically, there are infinitely many strings, so, like <code>BigInteger</code>, <code>string</code> corresponds to an infinite set. This also implies that <code>string</code> and <code>BigInteger</code> are isomorphic, but that shouldn't really be that surprising, since everything that's happening on a computer is already encoded as (binary) numbers - including strings. </p> <p> Any class that contains a <code>string</code> field is therefore also isomorphic to an (or <em>the?</em>) infinite set. Two <code>string</code> fields also correspond to infinity, as does a <code>string</code> field paired with a <code>bool</code> field, and so on. As soon as you have just one 'infinite type', the corresponding set is infinite. </p> <h3 id="233bc54732794692b4cb877ff92c30a8"> Constrained types <a href="#233bc54732794692b4cb877ff92c30a8" title="permalink">#</a> </h3> <p> How about static types (classes) that use one or more built-in types as backing fields, but on top of that impose 'business rules'? </p> <p> This one, for example, uses a <code>byte</code> as a backing field, but prohibits some values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DesByte</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;b; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DesByte</span>(<span style="color:blue;">byte</span>&nbsp;<span style="color:#1f377f;">b</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(12&nbsp;&lt;=&nbsp;b&nbsp;&amp;&amp;&nbsp;b&nbsp;&lt;=&nbsp;19) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException(nameof(b),&nbsp;<span style="color:#a31515;">&quot;[12,&nbsp;19]&nbsp;not&nbsp;allowed.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.b&nbsp;=&nbsp;b; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While this class doesn't correspond to the above 256-element set, you can still enumerate all possible values: </p> <p> <img src="/content/binary/248-element-set.png" alt="A 248-element set."> </p> <p> But then what about a class like this one? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Gadsby</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;manuscript; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Gadsby</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">manuscript</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(manuscript.Contains(<span style="color:#a31515;">&#39;e&#39;</span>,&nbsp;StringComparison.OrdinalIgnoreCase)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;The&nbsp;manuscript&nbsp;may&nbsp;not&nbsp;contain&nbsp;the&nbsp;letter&nbsp;&#39;e&#39;.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(manuscript)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.manuscript&nbsp;=&nbsp;manuscript; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While the constructor prohibits any string that contains the letter <em>e</em>, you can still create infinitely many <code>string</code> values even with that constraint. </p> <p> You can, however, still conceivably enumerate all possible <code>Gadsby</code> values, although the corresponding set would be infinitely large. </p> <p> Obviously, this isn't practical, but the point isn't one of practicality. The point is that you can think of types as sets. </p> <h3 id="0c33ef61fa3e4758b49b7c055ea07c65"> Function types <a href="#0c33ef61fa3e4758b49b7c055ea07c65" title="permalink">#</a> </h3> <p> So far we've only covered 'values', even though it's trivial to create types that correspond to infinitely large sets. </p> <p> In type systems, functions also have types. In Haskell, for example, the <em>type</em> <code>Int -&gt; Bool</code> indicates a function that takes an <code>Int</code> as input and return a <code>Bool</code>. This might for example be a function that checks whether the number is even. </p> <p> Likewise, we can write the type of <code>not</code> (Boolean negation) as <code>Bool -&gt; Bool</code>. In a set diagram, we can illustrate it like this: </p> <p> <img src="/content/binary/not-function-set-diagram.png" alt="Boolean negation set diagram."> </p> <p> Each element points to the other one: <em>true</em> points to <em>false</em>, and vice versa. Together, the two arrows completely describe the <code>not</code> function (the <code>!</code> operator in C#). </p> <p> <img src="/content/binary/not-function-arrow-set.png" alt="The two arrows describing Boolean negation themselves form a set."> </p> <p> If we remove the original elements (<em>true</em> and <em>false</em>) of the set, it becomes clearer that these two arrows also form a set. </p> <p> In general, we can think of functions as sets of arrows, but still sets. Many of these sets are infinite. </p> <h3 id="3dfcc401e1d248888bd2032bfa05f4f4"> Conclusion <a href="#3dfcc401e1d248888bd2032bfa05f4f4" title="permalink">#</a> </h3> <p> Set theory is a branch of mathematics, and so is type theory. Having no formal education in either, I don't claim that types and sets are the same. A quick web search implies that while there are many similarities between types and sets, there are also differences. In this article, I've highlighted some similarities. </p> <p> Thinking about types as sets can be helpful in everyday programming. In test-driven development, for example, <a href="https://en.wikipedia.org/wiki/Equivalence_partitioning">equivalence partitioning</a> provides insights into which test inputs to use. Being able to consider a <em>system under test's</em> inputs as sets (rather than types) makes this easier. </p> <p> As future articles will cover, it also becomes easier to think about <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> and the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov substitution principle</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="846a603e9bb84bf69c3f0327db148737"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#846a603e9bb84bf69c3f0327db148737">#</a></div> <div class="comment-content"> <blockquote> Any class that contains a <code>string</code> field is therefore also isomorphic to an (or <em>the?</em>) infinite set. </blockquote> <p> It is isomorphic to <em>an</em> infinite set. More generally, any class that contains a field of type <code>T</code> is isomorphic to a set with cardinality at least at big as the cardinality of the inhabitants of <code>T</code>. There are infinite sets with different cardinalities, and <code>string</code> is isomorphic to the smallest of these, which is known as countably infinite or <a href="https://en.wikipedia.org/wiki/Aleph_number#Aleph-nought">Aleph-nought</a>. Any class that contains a field of type <code>string -&gt; bool</code> is isomorphic to a set with cardinality at least <a href="https://en.wikipedia.org/wiki/Aleph_number#Aleph-one">Aleph-one</a> since that function type is isomorphic to a set with that cardinality. </p> </div> <div class="comment-date">2021-11-15 14:39 UTC</div> </div> <div class="comment" id="ff1df88a06aa42349fb56e243299499c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ff1df88a06aa42349fb56e243299499c">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I wasn't kidding when I wrote that I'm not a mathematician. Given, however, that <a href="https://www.goodreads.com/review/show/1731926050">I've read and more or less understood</a> <a href="http://amzn.to/2n9MFGh">The Annotated Turing</a>, I should have known better. That book begins with a lucid explanation of Cantor's theorem. </p> <p> Does this practically impact the substance of the present article? </p> </div> <div class="comment-date">2021-11-16 06:51 UTC</div> </div> <div class="comment" id="13a45f6dddad4d73a1b54459c16337a0"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#13a45f6dddad4d73a1b54459c16337a0">#</a></div> <div class="comment-content"> <blockquote> Does this practically impact the substance of the present article? </blockquote> <p> Nope. I think the article is good. I think everything you said is correct. I just wanted to elaborate a bit at the one point where you conveyed some hesitation. </p> </div> <div class="comment-date">2021-11-20 15:52 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Reader as a profunctor https://blog.ploeh.dk/2021/11/08/reader-as-a-profunctor 2021-11-08T07:01:00+00:00 Mark Seemann <div id="post"> <p> <em>Any function gives rise to a profunctor. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2021/11/01/profunctors">a short article series about profunctors</a>. It assumes that you've read the introduction. </p> <p> Previous articles in <a href="/2018/03/19/functors-applicatives-and-friends">the overall article series on functors and similar abstractions</a> discussed <a href="/2021/08/30/the-reader-functor">Reader as both a covariant functor</a>, as well as <a href="/2021/10/04/reader-as-a-contravariant-functor">a contravariant functor</a>. As the profunctor introduction intimated, if you combine the properties of both co- and contravariance, you'll have a profunctor. </p> <p> There's <a href="/2018/03/22/functors">a wide selection of well-known functors</a> and <a href="/2021/09/02/contravariant-functors">a smaller selection of contravariant functors</a>. Of all those that I've covered so far, only one appears in both lists: Reader. </p> <h3 id="3f3f21d3e3024108801a73605ea1194e"> Reader <a href="#3f3f21d3e3024108801a73605ea1194e" title="permalink">#</a> </h3> <p> Consider, again, this <code>IReader</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReader</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;<span style="color:#74531f;">Run</span>(R&nbsp;<span style="color:#1f377f;">environment</span>); }</pre> </p> <p> When discussing <code>IReader</code> as a covariant functor, we'd fix <code>R</code> and let <code>A</code> vary. When discussing the same interface as a contravariant functor, we'd fix <code>A</code> and let <code>R</code> vary. If you allow both to vary freely, you have a profunctor. </p> <p> As the profunctor overview article asserted, you can implement <code>ContraMap</code> and <code>Select</code> with <code>DiMap</code>, or you can implement <code>DiMap</code> with <code>ContraMap</code> and <code>Select</code>. Since previous articles have supplied both <code>Select</code> and <code>ContraMap</code> for <code>IReader</code>, it's a natural opportunity to see how to implement <code>DiMap</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReader&lt;R1,&nbsp;A1&gt;&nbsp;<span style="color:#74531f;">DiMap</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>,&nbsp;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">A1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IReader&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">reader</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;R1,&nbsp;R&gt;&nbsp;<span style="color:#1f377f;">contraSelector</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;A,&nbsp;A1&gt;&nbsp;<span style="color:#1f377f;">coSelector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;reader.ContraMap(contraSelector).Select(coSelector); }</pre> </p> <p> You simply pass <code>contraSelector</code> to <code>ContraMap</code> and <code>coSelector</code> to <code>Select</code>, while chaining the two method calls. You can also flip the order in which you call these two functions - as long as they are both <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>, it'll make no difference. You'll shortly see an example of flipping the order. </p> <p> First, though, an example of using <code>DiMap</code>. Imagine that you have this <code>IReader</code> implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TotalSecondsReader</span>&nbsp;:&nbsp;IReader&lt;TimeSpan,&nbsp;<span style="color:blue;">double</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">double</span>&nbsp;<span style="color:#74531f;">Run</span>(TimeSpan&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;environment.TotalSeconds; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class converts a <code>TimeSpan</code> value into the total number of seconds represented by that duration. You can project this Reader in both directions using <code>DiMap</code>: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ReaderDiMapExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;TimeSpan,&nbsp;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">reader</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TotalSecondsReader(); &nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;DateTime,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.DiMap((DateTime&nbsp;<span style="color:#1f377f;">dt</span>)&nbsp;=&gt;&nbsp;dt.TimeOfDay,&nbsp;<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;d&nbsp;%&nbsp;2&nbsp;==&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;Assert.True(projection.Run(<span style="color:blue;">new</span>&nbsp;DateTime(3271,&nbsp;12,&nbsp;11,&nbsp;2,&nbsp;3,&nbsp;4))); }</pre> </p> <p> This example maps the Reader from <code>TimeSpan</code> to <code>DateTime</code> by mapping in the opposite direction: The lambda expression <code>(DateTime dt) =&gt; dt.TimeOfDay</code> returns the time of day of a given <code>DateTime</code>. This value is a <code>TimeSpan</code> value representing the time passed since midnight on that date. </p> <p> The example also checks whether or not a <code>double</code> value is an even number. </p> <p> When the resulting <code>projection</code> is executed, the expected result is <code>true</code> because the input date and time is first converted to a time of day (<em>02:03:04</em>) by the <code>contraSelector</code>. Then <code>TotalSecondsReader</code> converts that duration to the total number of seconds: <em>2 * 60 * 60 + 3 * 60 + 4 = 7,384</em>. Finally, <em>7,384</em> is an even number, so the output is <code>true</code>. </p> <h3 id="536f69bb15094519becd4066266c1088"> Raw functions <a href="#536f69bb15094519becd4066266c1088" title="permalink">#</a> </h3> <p> As already explained in the previous Reader articles, the <code>IReader</code> interface is mostly a teaching device. In a language where you can treat functions as values, you don't need the interface. In C#, for example, the standard function delegates suffice. You can implement <code>DiMap</code> directly on <code>Func&lt;A, B&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;R1,&nbsp;A1&gt;&nbsp;<span style="color:#74531f;">DiMap</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>,&nbsp;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">A1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;Func&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">func</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;R1,&nbsp;R&gt;&nbsp;<span style="color:#1f377f;">contraSelector</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;A,&nbsp;A1&gt;&nbsp;<span style="color:#1f377f;">coSelector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;func.Select(coSelector).ContraMap(contraSelector); }</pre> </p> <p> As promised, I've here flipped the order of methods in the chain, so that the implementation first calls <code>Select</code> and then <code>ContraMap</code>. This is entirely arbitrary, and I only did it to demonstrate that the order doesn't matter. </p> <p> Here's another usage example: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">AreaOfDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;Version,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">func</span>&nbsp;=&nbsp;<span style="color:#1f377f;">v</span>&nbsp;=&gt;&nbsp;v.Major&nbsp;+&nbsp;v.Minor&nbsp;*&nbsp;v.Build; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;DateTime,&nbsp;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;func.DiMap( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(DateTime&nbsp;<span style="color:#1f377f;">dt</span>)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;Version(dt.Year,&nbsp;dt.Month,&nbsp;dt.Day), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;i&nbsp;*&nbsp;i&nbsp;*&nbsp;Math.PI); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expected:&nbsp;16662407.390443427686297314140028, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual:&nbsp;projection(<span style="color:blue;">new</span>&nbsp;DateTime(1991,&nbsp;12,&nbsp;26))); }</pre> </p> <p> This example starts with a nonsensical function that calculates a number from a <code>Version</code> value. Using <code>DiMap</code> the example then transforms <code>func</code> into a function that produces a <code>Version</code> from a <code>DateTime</code> and also calculates the area of a circle with the radius <code>i</code>. </p> <p> Clearly, this isn't a <em>useful</em> piece of code - it only demonstrates how <code>DiMap</code> works. </p> <h3 id="e060b80958e44aa3a88d5bcfad34335b"> Identity law <a href="#e060b80958e44aa3a88d5bcfad34335b" title="permalink">#</a> </h3> <p> As stated in the profunctor introduction, I don't intend to make much of the profunctor laws, since they are only reiterations of the (covariant) functor and contravariant functor laws. Still, an example (not a proof) of the profunctor identity law may be in order: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ProfunctorIdentityLaw</span>() { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;Guid,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">byteRange</span>&nbsp;=&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&gt;&nbsp;g.ToByteArray().Max()&nbsp;-&nbsp;g.ToByteArray().Min(); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">id</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;Guid,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">projected</span>&nbsp;=&nbsp;byteRange.DiMap&lt;Guid,&nbsp;Guid,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;(id,&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">guid</span>&nbsp;=&nbsp;Guid.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(byteRange(guid),&nbsp;projected(guid)); }</pre> </p> <p> This example uses another silly function. Given any <code>Guid</code>, the <code>byteRange</code> function calculates the difference between the largest and smallest byte in the value. Projecting this function with the identity function <code>id</code> along both axes should yield a function with the same behaviour. The <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">assertion phase</a> generates an arbitrary <code>Guid</code> and verifies that both <code>byteRange</code> and <code>projected</code> produce the same resulting value. </p> <h3 id="531348d1ee0f40bca3b6a18d1d0665ec"> Haskell <a href="#531348d1ee0f40bca3b6a18d1d0665ec" title="permalink">#</a> </h3> <p> As usual, I've adopted many of the concepts and ideas from <a href="https://www.haskell.org">Haskell</a>. The notion of a profunctor is so exotic that, unlike <a href="https://hackage.haskell.org/package/base/docs/Data-Functor-Contravariant.html">the Contravariant type class</a>, it's not (to my knowledge) part of the <em>base</em> library. Not that I've ever felt the need to import it, but if I did, I would probably use <a href="https://hackage.haskell.org/package/profunctors/docs/Data-Profunctor.html">Data.Profunctor</a>. This module defines a <code>Profunctor</code> type class, of which a normal function <code>(-&gt;)</code> is an instance. The type class defines the <code>dimap</code> function. </p> <p> We can replicate the above <code>AreaOfDate</code> example using the <code>Profunctor</code> type class, and the types and functions in <a href="https://hackage.haskell.org/package/time/docs/Data-Time.html">the time library</a>. </p> <p> First, I'll implement <code>func</code> like this: </p> <p> <pre><span style="color:#2b91af;">func</span>&nbsp;::&nbsp;<span style="color:blue;">Num</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(a,&nbsp;a,&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a func&nbsp;(maj,&nbsp;<span style="color:blue;">min</span>,&nbsp;bld)&nbsp;=&nbsp;maj&nbsp;+&nbsp;<span style="color:blue;">min</span>&nbsp;*&nbsp;bld</pre> </p> <p> Instead of using a <code>Version</code> type (which I'm not sure exists in the 'standard' Haskell libraries) this function just uses a triple (three-tuple). </p> <p> The projection is a bit more involved: </p> <p> <pre><span style="color:#2b91af;">projection</span>&nbsp;::&nbsp;<span style="color:blue;">Day</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Double</span> projection&nbsp;= &nbsp;&nbsp;dimap &nbsp;&nbsp;&nbsp;&nbsp;((\(maj,&nbsp;<span style="color:blue;">min</span>,&nbsp;bld)&nbsp;-&gt;&nbsp;(<span style="color:blue;">fromInteger</span>&nbsp;maj,&nbsp;<span style="color:blue;">min</span>,&nbsp;bld))&nbsp;.&nbsp;toGregorian) &nbsp;&nbsp;&nbsp;&nbsp;(\i&nbsp;-&gt;&nbsp;<span style="color:blue;">toEnum</span>&nbsp;(i&nbsp;*&nbsp;i)&nbsp;*&nbsp;<span style="color:blue;">pi</span>) &nbsp;&nbsp;&nbsp;&nbsp;func</pre> </p> <p> It basically does the same as the <code>AreaOfDate</code>, but the lambda expressions look more scary because of all the brackets and conversions. Haskell isn't always more succinct than C#. </p> <p> <pre>&gt; projection $ fromGregorian 1991 12 26 1.6662407390443427e7</pre> </p> <p> Notice that GHCi returns the result in scientific notation, so while the decimal separator seems to be oddly placed, the result is the same as in the C# example. </p> <h3 id="91777248e4394855a60f725dbd3c91d3"> Conclusion <a href="#91777248e4394855a60f725dbd3c91d3" title="permalink">#</a> </h3> <p> The Reader functor is not only a (covariant) functor and a contravariant functor. Since it's both, it's also a profunctor. And so what? </p> <p> This knowledge doesn't seem immediately applicable, but shines an interesting light on the fabric of code. If you squint hard enough, <a href="/2018/01/08/software-design-isomorphisms">most programming constructs look like functions</a>, and functions are profunctors. I don't intent to go so far as to claim that 'everything is a profunctor', but the Reader profunctor is ubiquitous. </p> <p> I'll return to this insight in a future article. </p> <p> <strong>Next:</strong> <a href="/2022/08/01/invariant-functors">Invariant functors</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Profunctors https://blog.ploeh.dk/2021/11/01/profunctors 2021-11-01T06:59:00+00:00 Mark Seemann <div id="post"> <p> <em>Functors that are both co- and contravariant. An article for C# programmers.</em> </p> <p> This article series is part of <a href="/2018/03/19/functors-applicatives-and-friends">a larger series of articles about functors, applicatives, and other mappable containers</a>. Particularly, you've seen examples of both <a href="/2018/03/22/functors">functors</a> and <a href="/2021/09/02/contravariant-functors">contravariant functors</a>. </p> <p> What happens if you, so to speak, combine those two? </p> <h3 id="20df8ada70644ca2a4a5363f05f89216"> Mapping in both directions <a href="#20df8ada70644ca2a4a5363f05f89216" title="permalink">#</a> </h3> <p> A <em>profunctor</em> is like a <a href="/2018/12/24/bifunctors">bifunctor</a>, except that it's contravariant in one of its arguments (and covariant in the other). Usually, you'd list the contravariant argument first, and the covariant argument second. By that convention, a hypothetical <code>Profunctor&lt;A, B&gt;</code> would be contravariant in <code>A</code> and covariant in <code>B</code>. </p> <p> In order to support such mapping, you could give the class a <code>DiMap</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Profunctor</span>&lt;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">B</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Profunctor&lt;A1,&nbsp;B1&gt;&nbsp;<span style="color:#74531f;">DiMap</span>&lt;<span style="color:#2b91af;">A1</span>,&nbsp;<span style="color:#2b91af;">B1</span>&gt;(Func&lt;A1,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">contraSelector</span>,&nbsp;Func&lt;B,&nbsp;B1&gt;&nbsp;<span style="color:#1f377f;">coSelector</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span></pre> </p> <p> Contrary to (covariant) functors, where C# will light up some extra compiler features if you name the mapping method <code>Select</code>, there's no extra language support for profunctors. Thus, you can call the method whatever you like, but here I've chosen the name <code>DiMap</code> just because that's what the <a href="https://www.haskell.org">Haskell</a> <a href="https://hackage.haskell.org/package/profunctors">Profunctors package</a> calls the corresponding function. </p> <p> Notice that in order to map the contravariant type argument <code>A</code> to <code>A1</code>, you must supply a selector that moves in the contrary direction: from <code>A1</code> to <code>A</code>. Mapping the covariant type argument <code>B</code> to <code>B1</code>, on the other hand, goes in the same direction: from <code>B</code> to <code>B1</code>. </p> <p> An example might look like this: </p> <p> <pre>Profunctor&lt;TimeSpan,&nbsp;<span style="color:blue;">double</span>&gt;&nbsp;<span style="color:#1f377f;">profunctor</span>&nbsp;=&nbsp;CreateAProfunctor(); Profunctor&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;profunctor.DiMap&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;(TimeSpan.Parse,&nbsp;<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;d&nbsp;%&nbsp;1&nbsp;==&nbsp;0);</pre> </p> <p> This example starts with a profunctor where the contravariant type is <code>TimeSpan</code> and the covariant type is <code>double</code>. Using <code>DiMap</code> you can map it to a <code>Profunctor&lt;string, bool&gt;</code>. In order to map the <code>profunctor</code> value's <code>TimeSpan</code> to the <code>projection</code> value's <code>string</code>, the method call supplies <code>TimeSpan.Parse</code>: a (partial) function that maps <code>string</code> to <code>TimeSpan</code>. </p> <p> The second argument maps the <code>profunctor</code> value's <code>double</code> to the <code>projection</code> value's <code>bool</code> by checking if <code>d</code> is an integer. The lambda expression <code><span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;d&nbsp;%&nbsp;1&nbsp;==&nbsp;0</code> implements a function from <code>double</code> to <code>bool</code>. That is, the profunctor <em>covaries</em> with that function. </p> <h3 id="3252cf7678234d0697ea0223b5659f5e"> Covariant mapping <a href="#3252cf7678234d0697ea0223b5659f5e" title="permalink">#</a> </h3> <p> Given <code>DiMap</code> you can implement the standard <code>Select</code> method for functors. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Profunctor&lt;A,&nbsp;B1&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">B1</span>&gt;(Func&lt;B,&nbsp;B1&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;DiMap&lt;A,&nbsp;B1&gt;(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x,&nbsp;selector); }</pre> </p> <p> <a href="/2019/01/07/either-bifunctor">Equivalently to bifunctors</a>, when you have a function that maps both dimensions, you can map one dimension by using the identity function for the dimension you don't need to map. Here I've used the lambda expression <code>x =&gt; x</code> as the identity function. </p> <p> You can use this <code>Select</code> method with standard method-call syntax: </p> <p> <pre>Profunctor&lt;DateTime,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">profunctor</span>&nbsp;=&nbsp;CreateAProfunctor(); Profunctor&lt;DateTime,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;profunctor.Select(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s.Length);</pre> </p> <p> or with query syntax: </p> <p> <pre>Profunctor&lt;DateTime,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">profunctor</span>&nbsp;=&nbsp;CreateAProfunctor(); Profunctor&lt;DateTime,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;ts&nbsp;<span style="color:blue;">in</span>&nbsp;profunctor &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;ts.Minutes;</pre> </p> <p> All profunctors are also covariant functors. </p> <h3 id="171bd982cd5c48a69690ea10d9dbb7d4"> Contravariant mapping <a href="#171bd982cd5c48a69690ea10d9dbb7d4" title="permalink">#</a> </h3> <p> Likewise, given <code>DiMap</code> you can implement a <code>ContraMap</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Profunctor&lt;A1,&nbsp;B&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">A1</span>&gt;(Func&lt;A1,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;DiMap&lt;A1,&nbsp;B&gt;(selector,&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> Using <code>ContraMap</code> is only possible with normal method-call syntax, since C# has no special understanding of contravariant functors: </p> <p> <pre>Profunctor&lt;<span style="color:blue;">long</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#1f377f;">profunctor</span>&nbsp;=&nbsp;CreateAProfunctor(); Profunctor&lt;<span style="color:blue;">string</span>,&nbsp;DateTime&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;profunctor.ContraMap&lt;<span style="color:blue;">string</span>&gt;(<span style="color:blue;">long</span>.Parse);</pre> </p> <p> All profunctors are also contravariant functors. </p> <p> While you can implement <code>Select</code> and <code>ContraMap</code> from <code>DiMap</code>, it's also possible to go the other way. If you have <code>Select</code> and <code>ContraMap</code> you can implement <code>DiMap</code>. </p> <h3 id="db28768129484d5d8fcf6591f164c2ad"> Laws <a href="#db28768129484d5d8fcf6591f164c2ad" title="permalink">#</a> </h3> <p> In the overall article series, I've focused on the laws that govern various universal abstractions. In this article, I'm going to treat this topic lightly, since it'd mostly be a reiteration of the laws that govern co- and contravariant functors. </p> <p> The only law I'll highlight is the profunctor identity law, which intuitively is a generalisation of the identity laws for co- and contravariant functors. If you map a profunctor in both dimensions, but use the identity function in both directions, nothing should change: </p> <p> <pre>Profunctor&lt;Guid,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">profunctor</span>&nbsp;=&nbsp;CreateAProfunctor(); Profunctor&lt;Guid,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;profunctor.DiMap((Guid&nbsp;<span style="color:#1f377f;">g</span>)&nbsp;=&gt;&nbsp;g,&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s);</pre> </p> <p> Here I've used two lambda expressions to implement the identity function. While they're two different lambda expressions, they both 'implement' the general identity function. If you aren't convinced, we can demonstrate the idea like this instead: </p> <p> <pre>T&nbsp;<span style="color:#74531f;">id</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;x; Profunctor&lt;Guid,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">profunctor</span>&nbsp;=&nbsp;CreateAProfunctor(); Profunctor&lt;Guid,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;profunctor.DiMap&lt;Guid,&nbsp;<span style="color:blue;">string</span>&gt;(id,&nbsp;id);</pre> </p> <p> This alternative representation defines a <a href="https://docs.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/local-functions">local function</a> called <code>id</code>. Since it's generic, you can use it as both arguments to <code>DiMap</code>. </p> <p> The point of the identity law is that in both cases, <code>projection</code> should be equal to <code>profunctor</code>. </p> <h3 id="7b00d6198531450b86fa49d9f3133806"> Usefulness <a href="#7b00d6198531450b86fa49d9f3133806" title="permalink">#</a> </h3> <p> Are profunctors useful in everyday programming? So far, I've found no particular use for them. This mirrors my experience with contravariant functors, which I also find little use for. Why should we care, then? </p> <p> It turns out that, while we rarely work explicitly with profunctors, they're everywhere. Normal functions are profunctors. </p> <ul> <li><a href="/2021/11/08/reader-as-a-profunctor">Reader as a profunctor</a></li> </ul> <p> In addition to normal functions (which <a href="/2021/08/30/the-reader-functor">are both covariant</a> and <a href="/2021/10/04/reader-as-a-contravariant-functor">contravariant</a>) other profunctors exist. At the time that I'm writing this article, I've no particular plans to add articles about any other profunctors, but if I do, I'll add them to the above list. Examples include Kleisli arrows and profunctor optics. </p> <p> The reason I find it worthwhile to learn about profunctors is that this way of looking at well-behaved functions shines an interesting light on the fabric of computation, so to speak. In <a href="/2021/11/29/postels-law-as-a-profunctor">a future article</a>, I'll expand on that topic. </p> <h3 id="da045552d1064ff88ac2eb7fb4ce18d7"> Conclusion <a href="#da045552d1064ff88ac2eb7fb4ce18d7" title="permalink">#</a> </h3> <p> A <em>profunctor</em> is a functor that is covariant in one dimension and contravariant in another dimension. While various exotic examples exist, the only example that you'd tend to encounter in mainstream programming is the Reader profunctor, also known simply as <em>functions</em>. </p> <p> <strong>Next:</strong> <a href="/2021/11/08/reader-as-a-profunctor">Reader as a profunctor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="bb0bb9d1bf094c1897f9dd42fb49712c"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#bb0bb9d1bf094c1897f9dd42fb49712c">#</a></div> <div class="comment-content"> <blockquote> Are profunctors useful in everyday programming? So far, I've found no particular use for them. </blockquote> <p> I have not found the concept of a profunctor useful in everyday programming, but I have found one very big use for them. </p> <p> I am the maintainer of <a href="https://github.com/elmish/Elmish.WPF">Elmish.WPF</a>. This project makes it easy to create a WPF application in the style of the Model-View-Update / MVU / <a href="https://guide.elm-lang.org/architecture/">Elm Architecture</a>. In a traditional WPF application, data goes between a view model and WPF via properties on that view model. Elmish.WPF makes that relationship a first-class concept via the type <code>Binding&lt;'model, 'msg&gt;</code>. This type is a profunctor; contravariant in <code>'model</code> and covariant in <code>'msg</code>. The individual mapping functions are <a href="https://github.com/elmish/Elmish.WPF/blob/1a4f4384c469ae22f281ce489a811d772ceba5f7/src/Elmish.WPF/Binding.fs#L724-L725"><code>Binding.mapModel</code></a> and <a href="https://github.com/elmish/Elmish.WPF/blob/1a4f4384c469ae22f281ce489a811d772ceba5f7/src/Elmish.WPF/Binding.fs#L730-L731"><code>Binding.mapMsg</code></a> respectively. </p> <p> This type was not always a profunctor. Recall that a profunctor is not really just a type but a type along with its mapping functions (or a single combined function). In versions 1, 2, and 3 of Elmish.WPF, this type is not a profunctor due to the missing mapping function(s). I added the mapping functions in version 4 (currently a prelease) that makes <code>Binding&lt;'model, 'msg&gt;</code> (along with those functions) a profunctor. This abstraction has made it possible to significantly improve both the internal implementation as well as the public API. </p> <p> As you stated, a single function <code>a -&gt; b</code> is a profunctor; contravariant in <code>a</code> and covariant in <code>b</code>. I think of this as the canonical profunctor, or maybe "the original" profunctor. I think it is interesting to compare each profunctor to this one. In the case of <code>Binding&lt;'model, 'msg&gt;</code>, is implemented by two functions: one from the (view) model to WPF with type <code>'model -> obj</code> that we could call <code>toWpf</code> and one from WPF with type <code>obj -> 'msg</code> that we could call <code>fromWpf</code>. Of course, composing these two functions results in a single function that is a profunctor in exactly the way we expect. </p> <p> Now here is something that I don't understand. In addition to <code>Binding.mapModel</code> and <code>Binding.mapMsg</code>, I have discovered another function with useful behavior in the function <a href="https://github.com/elmish/Elmish.WPF/blob/1a4f4384c469ae22f281ce489a811d772ceba5f7/src/Elmish.WPF/Binding.fs#L727-L728"><code>Binding.mapMsgWithModel</code></a>. Recall that a function <code>a -&gt; b</code> is not just profunctor in (contravariant) <code>a</code> and (covariant) <code>b</code>, but it is also a monad in <code>b</code> (with <code>a</code> fixed). The composed function <code>toWpf &gt;&gt; fromWpf</code> is such a monad and <code>Binding.mapMsgWithModel</code> is its "<code>bind</code>" function. The leads one to think that <code>Binding&lt;'model, 'msg&gt;</code> could be a monad in <code>'msg</code> (with <code>'model</code> fixed). My intuition is that this is not the case, but maybe I am wrong. </p> </div> <div class="comment-date">2021-11-02 01:38 UTC</div> </div> <div class="comment" id="5a203c4b4570460d86a960552dbaada8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5a203c4b4570460d86a960552dbaada8">#</a></div> <div class="comment-content"> <p> Tyson, it's great to learn that there are other examples of useful profunctors in the wild. It might be useful to add it to a 'catalogue' of profunctor examples, if someone ever compiles such a list - but perhaps it's a little to specific for a general-purpose catalogue... </p> <p> After much digging around in the source code you linked, I managed to locate the <a href="https://github.com/elmish/Elmish.WPF/blob/1a4f4384c469ae22f281ce489a811d772ceba5f7/src/Elmish.WPF/Binding.fs#L139-L142">definition of Binding&lt;'model, 'msg&gt;</a>, which, however, turns out to be too complex for me to do a full analysis on a Saturday morning. </p> <p> One of the cases, however, the <a href="https://github.com/elmish/Elmish.WPF/blob/1a4f4384c469ae22f281ce489a811d772ceba5f7/src/Elmish.WPF/Binding.fs#L36-L38">TwoWayData&lt;'model, 'msg&gt;</a> type looks much like a lens - another profunctor example. </p> <p> Might <code>Binding&lt;'model, 'msg&gt;</code> also form a monad? </p> <p> One simple test I've found useful for answering such a question is to consider whether a lawful <code>join</code> function exists. In Haskell, the <code>join</code> function has the type <code>Monad m =&gt; m (m a) -&gt; m a</code>. The intuitive interpretation is that if you can 'flatten' a nested functor, then that functor is also a monad. </p> <p> So the question is: Can you reasonably write a function with the type <code>Binding&lt;'model, Binding&lt;'model, 'msg&gt;&gt; -&gt; Binding&lt;'model, 'msg&gt;</code>? </p> <p> If you can write a lawful implementation of this <code>join</code> function, the <code>Binding&lt;'model, 'msg&gt;</code> forms a monad. </p> </div> <div class="comment-date">2021-11-06 10:56 UTC</div> </div> <div class="comment" id="26dfc57cd0f14444898a53b0a0d6f977"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#26dfc57cd0f14444898a53b0a0d6f977">#</a></div> <div class="comment-content"> <p> Thank you for linking to the deinition of <code>Binding&lt;'model, 'msg&gt;</code>. I should have done that. Yes, the <code>TwoWayData&lt;'model, 'msg&gt;</code> case is probably the simplest. Good job finding it. I have thought about implementing such a <code>join</code> function, but it doesn't seem possible to me. </p> </div> <div class="comment-date">2021-11-15 14:13 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functor variance compared to C#'s notion of variance https://blog.ploeh.dk/2021/10/25/functor-variance-compared-to-cs-notion-of-variance 2021-10-25T05:53:00+00:00 Mark Seemann <div id="post"> <p> <em>A note on C# co- and contravariance, and how it relates to functors.</em> </p> <p> This article is an instalment in <a href="/2021/09/02/contravariant-functors">an article series about contravariant functors</a>. It assumes that you've read the introduction, and a few of the examples. </p> <p> If you know your way around C# you may know that the language has its own notion of co- and contravariance. Perhaps you're wondering how it fits with contravariant functors. </p> <p> Quite well, fortunately. </p> <h3 id="1469d9fdd00f4a68b4c30a1bf81b7295"> Assignment compatibility <a href="#1469d9fdd00f4a68b4c30a1bf81b7295" title="permalink">#</a> </h3> <p> For the C# perspective on co- and contravariance, <a href="https://docs.microsoft.com/dotnet/csharp/programming-guide/concepts/covariance-contravariance">the official documentation</a> is already quite good. It starts with this example of <em>assignment compatibility</em>: </p> <blockquote> <p> <pre><span style="color:green;">//&nbsp;Assignment&nbsp;compatibility.</span> <span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">str</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;test&quot;</span>; <span style="color:green;">//&nbsp;An&nbsp;object&nbsp;of&nbsp;a&nbsp;more&nbsp;derived&nbsp;type&nbsp;is&nbsp;assigned&nbsp;to&nbsp;an&nbsp;object&nbsp;of&nbsp;a&nbsp;less&nbsp;derived&nbsp;type.</span> <span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">obj</span>&nbsp;=&nbsp;str;</pre> </p> </blockquote> <p> This kind of assignment is always possible, because a <code>string</code> <em>is also already</em> an <code>object</code>. An upcast within the inheritance hierarchy is <em>always</em> possible, so C# automatically allows it. </p> <p> <a href="https://fsharp.org">F#</a>, on the other hand, doesn't. If you try to do something similar in F#, it doesn't compile: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;str&nbsp;=&nbsp;<span style="color:#a31515;">&quot;test&quot;</span> <span style="color:blue;">let</span>&nbsp;obj&nbsp;:&nbsp;obj&nbsp;=&nbsp;str&nbsp;<span style="color:green;">//&nbsp;Doesn&#39;t&nbsp;compile</span></pre> </p> <p> The compiler error is: </p> <blockquote> This expression was expected to have type 'obj' but here has type 'string' </blockquote> <p> You have to explicitly use the upcast operator: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;str&nbsp;=&nbsp;<span style="color:#a31515;">&quot;test&quot;</span> <span style="color:blue;">let</span>&nbsp;obj&nbsp;=&nbsp;str&nbsp;:&gt;&nbsp;obj</pre> </p> <p> When you do that, the explicit type declaration of the value is redundant, so I removed it. </p> <p> In this example, you can think of <code>:&gt;</code> as a function from <code>string</code> to <code>obj</code>: <code>string -&gt; obj</code>. In C#, the equivalent function would be <code>Func&lt;string, object&gt;</code>. </p> <p> These functions always exist for types that are properly related to each other, upcast-wise. You can think of it as a generic function <code>'a -&gt; 'b</code> (or <code>Func&lt;A, B&gt;</code>), with the proviso that <code>A</code> must be 'upcastable' to <code>B</code>: </p> <p> <img src="/content/binary/a-as-a-subtype-of-b.png" alt="Two boxes labeled 'A' and 'B with an arrow pointing from A to B."> </p> <p> In my head, I'd usually think about this as <em>A being a subtype of B</em>, but unless I explain what I mean by <em>subtyping</em>, it usually confuses people. I consider anything that can 'act as' something else a subtype. So <code>string</code> is a subtype of <code>object</code>, but I also consider <code>TimeSpan</code> a subtype of <code>IComparable</code>, because that cast is also always possible: </p> <p> <pre>TimeSpan&nbsp;<span style="color:#1f377f;">twoMinutes</span>&nbsp;=&nbsp;TimeSpan.FromMinutes(2); IComparable&nbsp;<span style="color:#1f377f;">comp</span>&nbsp;=&nbsp;twoMinutes;</pre> </p> <p> Once again, F# is only happy if you explicitly use the <code>:&gt;</code> operator: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;twoMinutes&nbsp;=&nbsp;TimeSpan.FromMinutes&nbsp;2. <span style="color:blue;">let</span>&nbsp;comp&nbsp;=&nbsp;twoMinutes&nbsp;:&gt;&nbsp;IComparable</pre> </p> <p> All this is surely old hat to any .NET developer with a few months of programming under his or her belt. All the same, I want to drive home one last point (that you already know): Automatic upcast conversions are <a href="https://en.wikipedia.org/wiki/Transitive_relation">transitive</a>. Consider a class like <a href="https://docs.microsoft.com/dotnet/api/system.httpstyleuriparser">HttpStyleUriParser</a>, which is part of a small inheritance hierarchy: <code>object -&gt; UriParser -&gt; HttpStyleUriParser</code> (sic, that's how the documentation denotes the inheritance hierarchy; be careful about the arrow direction!). You can upcast an <code>HttpStyleUriParser</code> to both <code>UriParser </code> and <code>object</code>: </p> <p> <pre>HttpStyleUriParser&nbsp;<span style="color:#1f377f;">httParser</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;HttpStyleUriParser(); UriParser&nbsp;<span style="color:#1f377f;">parser</span>&nbsp;=&nbsp;httParser; <span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">op</span>&nbsp;=&nbsp;httParser;</pre> </p> <p> Again, the same is true in F#, but you have to explicitly use the <code>:&gt;</code> operator. </p> <p> To recapitulate: C# has a built-in <em>automatic</em> conversion that upcasts. It's also built into F#, but here as an operator that you explicitly have to use. It's like an automatic function from subtype to supertype. </p> <h3 id="7d69802639da4e13aef41faf2a740c19"> Covariance <a href="#7d69802639da4e13aef41faf2a740c19" title="permalink">#</a> </h3> <p> The C# documentation continues with an example of covariance: </p> <blockquote> <p> <pre><span style="color:green;">//&nbsp;Covariance.</span> IEnumerable&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">strings</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;List&lt;<span style="color:blue;">string</span>&gt;(); <span style="color:green;">//&nbsp;An&nbsp;object&nbsp;that&nbsp;is&nbsp;instantiated&nbsp;with&nbsp;a&nbsp;more&nbsp;derived&nbsp;type&nbsp;argument</span> <span style="color:green;">//&nbsp;is&nbsp;assigned&nbsp;to&nbsp;an&nbsp;object&nbsp;instantiated&nbsp;with&nbsp;a&nbsp;less&nbsp;derived&nbsp;type&nbsp;argument.</span> <span style="color:green;">//&nbsp;Assignment&nbsp;compatibility&nbsp;is&nbsp;preserved.</span> IEnumerable&lt;<span style="color:blue;">object</span>&gt;&nbsp;<span style="color:#1f377f;">objects</span>&nbsp;=&nbsp;strings;</pre> </p> </blockquote> <p> Since <code>IEnumerable&lt;T&gt;</code> forms a <a href="/2018/03/22/functors">(covariant) functor</a> you can lift a function <code>Func&lt;A, B&gt;</code> to a function from <code>IEnumerable&lt;A&gt;</code> to <code>IEnumerable&lt;B&gt;</code>. Consider the above example that goes from <code>IEnumerable&lt;string&gt;</code> to <code>IEnumerable&lt;object&gt;</code>. Let's modify the diagram from the functor article: </p> <p> <img src="/content/binary/functor-diagram-for-ienumerable.png" alt="Functor diagram."> </p> <p> Since the C# compiler already knows that an automatic function (<code>:&gt;</code>) exists that converts <code>string</code> to <code>object</code>, it can <em>automatically</em> convert <code>IEnumerable&lt;string&gt;</code> to <code>IEnumerable&lt;object&gt;</code>. You don't have to call <code>Select</code> to do this. The compiler does it for you. </p> <p> How does it do that? </p> <p> It looks for a little annotation on the generic type argument. For covariant types, <a href="https://docs.microsoft.com/dotnet/csharp/programming-guide/concepts/covariance-contravariance/creating-variant-generic-interfaces">the relevant keyword</a> is <code>out</code>. And, as expected, the <code>T</code> in <a href="https://docs.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1">IEnumerable&lt;T&gt;</a> is annotated with <code>out</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">T</span>&gt;</pre> </p> <p> The same is true for <a href="https://docs.microsoft.com/dotnet/api/system.func-2">Func&lt;T, TResult&gt;</a>, which <a href="/2021/08/30/the-reader-functor">is both covariant</a> and <a href="/2021/10/04/reader-as-a-contravariant-functor">contravariant</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">delegate</span>&nbsp;TResult&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">TResult</span>&gt;(T&nbsp;<span style="color:#1f377f;">arg</span>);</pre> </p> <p> The <code>in</code> keyword denotes contravariance, but we'll get to that shortly. </p> <p> The reason that covariance is annotated with the <code>out</code> keyword is that covariant type arguments usually sit in the return-type position. The rule is actually a little more nuanced than that, but I'll again refer you to <a href="https://reasonablypolymorphic.com">Sandy Maguire</a>'s <a href="https://www.goodreads.com/review/show/3406773267">excellent</a> book <a href="https://thinkingwithtypes.com">Thinking with Types</a> if you're interested in the details. </p> <h3 id="e409b7a8a3964385bcdffb1fb72e4251"> Contravariance <a href="#e409b7a8a3964385bcdffb1fb72e4251" title="permalink">#</a> </h3> <p> So far, so good. What about contravariance? The C# documentation continues its example: </p> <blockquote> <p> <pre><span style="color:green;">//&nbsp;Contravariance.</span> <span style="color:green;">//&nbsp;Assume&nbsp;that&nbsp;the&nbsp;following&nbsp;method&nbsp;is&nbsp;in&nbsp;the&nbsp;class:</span> <span style="color:green;">//&nbsp;static&nbsp;void&nbsp;SetObject(object&nbsp;o)&nbsp;{&nbsp;}</span> Action&lt;<span style="color:blue;">object</span>&gt;&nbsp;<span style="color:#1f377f;">actObject</span>&nbsp;=&nbsp;SetObject; <span style="color:green;">//&nbsp;An&nbsp;object&nbsp;that&nbsp;is&nbsp;instantiated&nbsp;with&nbsp;a&nbsp;less&nbsp;derived&nbsp;type&nbsp;argument</span> <span style="color:green;">//&nbsp;is&nbsp;assigned&nbsp;to&nbsp;an&nbsp;object&nbsp;instantiated&nbsp;with&nbsp;a&nbsp;more&nbsp;derived&nbsp;type&nbsp;argument.</span> <span style="color:green;">//&nbsp;Assignment&nbsp;compatibility&nbsp;is&nbsp;reversed.</span> Action&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">actString</span>&nbsp;=&nbsp;actObject;</pre> </p> </blockquote> <p> The <a href="https://docs.microsoft.com/dotnet/api/system.action-1">Action&lt;T&gt;</a> delegate <a href="/2021/09/06/the-command-handler-contravariant-functor">gives rise to a contravariant functor</a>. The <code>T</code> is also annotated with the <code>in</code> keyword, since the type argument sits in the input position: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">delegate</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">obj</span>)</pre> </p> <p> Again, let's modify the diagram from <a href="/2021/09/02/contravariant-functors">the article about contravariant functors</a>: </p> <p> <img src="/content/binary/contravariant-functor-diagram-for-action.png" alt="Contravariant functor diagram."> </p> <p> Again, since the C# compiler already knows that an automatic function exists that converts <code>string</code> to <code>object</code>, it can <em>automatically</em> convert <code>Action&lt;object&gt;</code> to <code>Action&lt;string&gt;</code>. You don't have to call <code>Contramap</code> to do this. The compiler does it for you. </p> <p> It knows that <code>Action&lt;T&gt;</code> is contravariant because it's annotated with the <code>in</code> keyword. Thus, it allows contravariant assignment. </p> <p> It all checks out. </p> <h3 id="660eb0eaa995410d891e07ec4b978ba6"> Conclusion <a href="#660eb0eaa995410d891e07ec4b978ba6" title="permalink">#</a> </h3> <p> The C# compiler understands co- and contravariance, but while it automatically supports it, it only deals with automatic conversion from subtype to supertype. Thus, for those kinds of conversions, you don't need a <code>Select</code> or <code>ContraMap</code> method. </p> <p> The functor notion of co- and contravariance is a generalisation of how the C# compiler works. Instead of relying on automatic conversions, the <code>Select</code> and <code>ContraMap</code> methods enable you to supply arbitrary conversion functions. </p> <p> <strong>Next:</strong> <a href="/2022/03/21/contravariant-dependency-injection">Contravariant Dependency Injection</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Readability verification https://blog.ploeh.dk/2021/10/18/readability-verification 2021-10-18T07:37:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you know whether the code you wrote is readable?</em> </p> <p> In a recent Twitter thread about pair and mob programming, <a href="https://dannorth.net">Dan North</a> observes: </p> <blockquote> <p> "That’s the tricky problem I was referring to. If you think you can write code that other humans can understand, without collaborating or calibrating with other humans, assuming that an after-the-fact check will always be affirmative, then you are a better programmer than me." </p> <footer><cite><a href="https://twitter.com/tastapod/status/1448184718122487811">Dan North</a></cite></footer> </blockquote> <p> I neither think that I'm a better programmer than Dan nor that, without collaboration, I can write code that other humans can understand. That's why I'd like someone else to <em>review</em> my code. Not write it together with me, but <em>read it</em> after I've written it. </p> <h3 id="b9a6a0093d0c4c2193c4e39d519ef582"> Advantages of pair and ensemble programming <a href="#b9a6a0093d0c4c2193c4e39d519ef582" title="permalink">#</a> </h3> <p> Pair programming and ensemble (AKA <em>mob</em>) programming is an efficient way to develop software. It works for lots of people. I'm not insisting otherwise. </p> <p> By working together, you can pool skills. Imagine working on a feature for a typical web application. This involves user interface, business logic, data access, and possibly other things as well. Few people are experts in all those areas. Personally, I'm comfortable around business logic and data access, but know little about web front-end development. It's great to have someone else's expertise to draw on. </p> <p> By working together in real time, you avoid hand-offs. If I had to help implementing a feature in an asynchronous manner, I'd typically implement domain logic and data access in a REST API, then tell a front-end expert that the API is ready. This way of working <a href="/2020/03/16/conways-law-latency-versus-throughput">introduces wait times into the process</a>, and may also cause rework if it turns out that the way I designed the API doesn't meet the requirements of the front end. </p> <p> Real-time collaboration addresses some of these concerns. It also improves code ownership. In <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, I quote <a href="https://twitter.com/birgitta410">Birgitta Böckeler</a> and <a href="https://sssggr.net">Nina Siessegger</a>: </p> <blockquote> <p> "Consistent pairing makes sure that every line of code was touched or seen by at least 2 people. This increases the chances that anyone on the team feels comfortable changing the code almost anywhere. It also makes the codebase more consistent than it would be with single coders only. </p> <p> "Pair programming alone does not guarantee you achieve collective code ownership. You need to make sure that you also rotate people through different pairs and areas of the code, to prevent knowledge silos." </p> <footer><cite>Birgitta Böckeler and Nina Siessegger, <a href="https://martinfowler.com/articles/on-pair-programming.html">On Pair Programming</a></cite></footer> </blockquote> <p> With mob programming, you take many of these advantages to the next level. If you include a domain expert in the group, you can learn about what the organisation actually needs as you're developing a feature. If you include specialised testers, they may see edge cases or error modes you didn't think of. If you include UX experts, you'll have a chance to develop software that users can actually figure out how to use. </p> <p> There are lots of benefits to be had from pair and ensemble programming. In <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> I recommend that you try it. I've recommended it to my customers. I've had good experiences with it myself: </p> <blockquote> <p> "I’ve used [mob programming] with great success as a programming coach. In one engagement, I spent two to three days a week with a few other programmers, helping them apply test-driven development practices to their production code bases. After a few months of that, I went on vacation. Meanwhile those programmers kept going with test-driven development. Mob programming is great for knowledge transfer." </p> <footer><cite><a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a></cite></footer> </blockquote> <p> <a href="/2021/10/11/serendipity-driven-development">I don't, however, think that it's a one-size-fits-all solution</a>. </p> <h3 id="c57bfb2342d2437596ff90a13fb29c44"> The curse of knowledge <a href="#c57bfb2342d2437596ff90a13fb29c44" title="permalink">#</a> </h3> <p> While outlining the advantages of pair and ensemble programming, I didn't mention readability. I don't see how those ways of working address the problem of writing readable code. </p> <p> I've reviewed code written by pairs, and it was neither more nor less readable than code written by a single programmer. I think that there's an easy-to-understand reason for this. It relates to the <em>curse of knowledge:</em> </p> <blockquote> <p> "In 1990, Elizabeth Newton earned a Ph.D. in psychology at Stanford by studying a simple game in which she assigned people to one of two roles: “tappers” or “listeners.” Tappers received a list of twenty-five well-known songs, such as “Happy Birthday to You” and “The Star-Spangled Banner.” Each tapper was asked to pick a song and tap out the rhythm to a listener (by knocking on a table). The listener’s job was to guess the song, based on the rhythm being tapped. (By the way, this experiment is fun to try at home if there’s a good “listener” candidate nearby.) </p> <p> "The listener’s job in this game is quite difficult. Over the course of Newton’s experiment, 120 songs were tapped out. Listeners guessed only 2.5 percent of the songs: 3 out of 120. </p> <p> "But here’s what made the result worthy of a dissertation in psychology. Before the listeners guessed the name of the song, Newton asked the tappers to predict the odds that the listeners would guess correctly. They predicted that the odds were 50 percent. </p> <p> "The tappers got their message across 1 time in 40, but they thought they were getting their message across 1 time in 2. Why? </p> <p> "When a tapper taps, she is <em>hearing the song in her head</em>. Go ahead and try it for yourself—tap out “The Star-Spangled Banner.” It’s impossible to avoid hearing the tune in your head. Meanwhile, the listeners can’t hear that tune—all they can hear is a bunch of disconnected taps, like a kind of bizarre Morse Code. </p> <p> "In the experiment, tappers are flabbergasted at how hard the listeners seem to be working to pick up the tune. <em>Isn’t the song obvious?</em> The tappers’ expressions, when a listener guesses “Happy Birthday to You” for “The Star-Spangled Banner,” are priceless: <em>How could you be so stupid?</em> </p> <p> "It’s hard to be a tapper. The problem is that tappers have been given knowledge (the song title) that makes it impossible for them to imagine what it’s like to <em>lack</em> that knowledge. When they’re tapping, they can’t imagine what it’s like for the listeners to hear isolated taps rather than a song. This is the Curse of Knowledge. Once we know something, we find it hard to imagine what it was like not to know it. Our knowledge has “cursed” us. And it becomes difficult for us to share our knowledge with others, because we can’t readily re-create our listeners’ state of mind. </p> <p> "The tapper/listener experiment is reenacted every day across the world. The tappers and listeners are CEOs and frontline employees, teachers and students, politicians and voters, marketers and customers, writers and readers. All of these groups rely on ongoing communication, but, like the tappers and listeners, they suffer from enormous information imbalances. When a CEO discusses “unlocking shareholder value,” there is a tune playing in her head that the employees can’t hear." </p> <footer><cite>Chip Heath and Dan Heath, <a href="https://amzn.to/3viu26j">Made to Stick</a></cite></footer> </blockquote> <p> When you're writing code, you're a <em>tapper</em>. As you're writing the code, you know <em>why</em> you are writing it the way you do, you know what you've already tried that didn't work, the informal requirements that someone told you about over the water cooler, etc. </p> <p> Why should pair or ensemble programming change that? </p> <blockquote> <p> "One of the roles of a PR is to verify that someone who didn't write the new code can understand it. </p> <p> "The constant communication of pair programming can result in code only that pair understands. Does a book with two authors not need an editor?" </p> <footer><cite><a href="https://twitter.com/laurence/status/1448485971289260039">Laurence Gonsalves</a></cite></footer> </blockquote> <p> So, how do you verify that code is readable? </p> <h3 id="77efc0b4e41a4f5597ff6e072187698f"> Readability <a href="#77efc0b4e41a4f5597ff6e072187698f" title="permalink">#</a> </h3> <p> I often forget to remind the reader that discussions like this one, about software productivity, mostly rely on <a href="https://martinfowler.com/bliki/AnecdotalEvidence.html">anecdotal evidence</a>. There's <a href="/2020/05/25/wheres-the-science">little scientific evidence about these topics</a>. The ensuing discussions <a href="/2020/10/12/subjectivity">tend to rely on subjectivity</a>, and so, ultimately, does this one. </p> <p> In <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, I suggest heuristics for writing readable code, but ultimately, the only reliable test of readability that I can think of is simple: </p> <p> Ask someone else to <em>read</em> the code. </p> <p> That's what a code review ought to do. Anyone who took part in writing the code is a <em>tapper</em>. After I've written code, I'm a <em>tapper</em>. I'm in no position to evaluate whether the code I just wrote is readable. </p> <p> You need a <em>listener</em> (or, here: a <em>reader</em>) to evaluate whether or not sufficient information came across. </p> <p> I agree with Dan North that I need other humans to collaborate and calibrate. I just disagree that people who write code are in a position to evaluate whether the code is readable (and thereby can sustain the business in the long run). </p> <h3 id="8655f1951b6a414f82d38435aa317e5c"> Rejection <a href="#8655f1951b6a414f82d38435aa317e5c" title="permalink">#</a> </h3> <p> What happens, then, if I submit a pull request that the reviewer finds unreadable? </p> <p> The reviewer should either suggest improvements or decline the pull request. </p> <p> I can tell from Dan's tweet that he's harbouring a common misconception about the pull request review process: </p> <blockquote> <p> "assuming that an after-the-fact check will always be affirmative" </p> <footer><cite><a href="https://twitter.com/tastapod/status/1448184718122487811">Dan North</a></cite></footer> </blockquote> <p> No, I don't assume that my pull requests always pass muster. That's also the reason that <a href="/2015/01/15/10-tips-for-better-pull-requests">pull requests should be small</a>. They should be small enough that you can afford to have them rejected. </p> <p> I'm currently helping one of my clients with some code. I add some code and send an <a href="/2021/06/21/agile-pull-requests">agile pull request</a>. </p> <p> Several times in the last month, my pull requests have remained unmerged. In none of the cases, actually, have the reviewer outright rejected the pull request. He just started asking questions, then we had a short debate over GitHub, and then I came to the conclusion that I should close the pull request myself. </p> <p> No drama, just feedback. </p> <h3 id="ad0609e523b8401fbab92dc0e90c44d5"> Conclusion <a href="#ad0609e523b8401fbab92dc0e90c44d5" title="permalink">#</a> </h3> <p> How do you verify that code is readable? </p> <p> I can't think of anything better than asking someone else to read the code. </p> <p> Obviously, we shouldn't ask random strangers about readability. We should ask team members to review code. One implication of collective code ownership is that when a team member accepts a pull request, he or she is also taking on the shared responsibility of maintaining that code. As I write in <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, a fundamental criterion for evaluating a pull request is: <em>Will I be okay maintaining this?</em> </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Serendipity-driven development https://blog.ploeh.dk/2021/10/11/serendipity-driven-development 2021-10-11T05:54:00+00:00 Mark Seemann <div id="post"> <p> <em>How much does providence drive thought leadership?</em> </p> <p> I regularly listen to podcasts. Many podcast episodes are structured around an interview with a guest. A common interview technique (and icebreaker, I suppose) is to ask the guest how he or she became a voice in the field that's the topic for the episode. Surprisingly often, the answer is that it's basically a happy coincidence. He or she was young, had no specific plans, but tried a few things until eventually becoming enamoured with a particular topic. </p> <p> That's not just technology podcasts. I also listen to interviews with scientists and artists on a variety of topics. </p> <p> A <em>few</em> people are driven from an early age to study something specific. Most, it seems, are not. I'm no exception. I had a false start as an economist, but was so extremely fortunate that the 1990's were such a boom decade of IT that you could get a job in the field if you could spell HTML. </p> <p> It seems to me that it's a common (Western) experience for a young person to start adult life without much of a plan, but an unrealised responsiveness to certain stimuli. As a young person, you may have a general notion of your own inclinations, so you seek out certain activities and avoid others. Still, you may not really know yourself. </p> <p> I didn't know myself at 18. After <a href="https://en.wikipedia.org/wiki/Gymnasium_(school)">gymnasium</a> (~ high school) in 1989 my best friend started at computer science at the University of Copenhagen. I had no experience with computers and thought it sounded incredibly dry. I wanted to be a rock star or a comic book artist in the French-Belgian style. </p> <p> In order to get an education, though, I started at economics at the University of Copenhagen. Talk about a dry subject. </p> <p> Well, at least I learned game theory, n-dimensional topology, optimal control theory, chaos theory, and some other branches of mathematics, so perhaps the years weren't entirely wasted... </p> <p> Computers weren't on my radar, but I soon realised that it'd be practical to buy a PC in order to write my thesis. </p> <p> So, I bought one and soon found the computer much more interesting than economics. </p> <p> You may not realise that you'll love something until you try it. </p> <h3 id="1909691de650477189ee7d41a49cf22e"> Thought leadership <a href="#1909691de650477189ee7d41a49cf22e" title="permalink">#</a> </h3> <p> I recently wrote <a href="/2021/08/09/am-i-stuck-in-a-local-maximum">an article about the cognitive dissonance I felt</a> when interacting with many of my heroes. The <a href="https://twitter.com/ploeh/status/1424624134030602241">ensuing Twitter discussion</a> was enlightening. </p> <p> Many of my heroes balk at being called heroes or thought leaders, but I agree with <a href="https://hillelwayne.com/">Hillel Wayne</a>: </p> <blockquote> <p> "That's why, incidentally, "thought leaders" have so much weight in our industry. We like to make fun of them, but fact of the matter is that the Thought Leaders are the ones actually trying to communicate their techniques. </p> <p> "(That's why I want to unironically be a Thought Leader)" </p> <footer><cite><a href="https://twitter.com/hillelogram/status/1445435617047990273">Hillel Wayne</a></cite></footer> </blockquote> <p> I've been called a <em>though leader</em> a few times already, and like Hillel Wayne, I gratefully accept the label. </p> <p> There's <a href="/2020/05/25/wheres-the-science">little scientific evidence about what works in software development</a>, and most innovation happens behind closed doors. Thought leaders are those that observe and share the innovation with the rest of the world. </p> <p> I follow though leaders on Twitter, listen to podcasts on which they are guests, and <a href="https://www.goodreads.com/author/show/4383188.Mark_Seemann">read books</a>. </p> <p> I learned a lot from the discussion related to <a href="/2021/08/09/am-i-stuck-in-a-local-maximum">my article about feeling stuck</a>. I feel that I better understand why opposing views exist. Much has to do with context and nuance, two important factors easily lost on Twitter. </p> <p> I also think that personal experience plays a big role. Thought leaders share <a href="https://martinfowler.com/bliki/AnecdotalEvidence.html">anecdotal evidence</a>. As is also the case in science and business, we tend to share our successes. </p> <p> What feels like a success is what resonates with us. </p> <p> It's like the serendipity when you're young and finally encounter something that feels compatible with you. Should we call it serendipity-driven development? </p> <p> A couple of examples may be in order. </p> <h3 id="d4fb5cf60f584d7e8de3e0fcbcb902a7"> Pair programming <a href="#d4fb5cf60f584d7e8de3e0fcbcb902a7" title="permalink">#</a> </h3> <p> While I now have an increased awareness of what motivates other thought leaders, I still often disagree. It wouldn't be unnatural if our personal experiences with particular practices influence our positions. </p> <p> <img src="/content/binary/pair-programming.jpg" alt="Pair programming."> </p> <p> One such example is pair programming. In an interview (sorry, can't remember which) Robert C. Martin told how he found test-driven development dumb until either Kent Beck or Ward Cunningham paired with him to show him the light. Recently, <a href="http://www.exampler.com/">Brian Marick</a> shared a similar experience: </p> <blockquote> <p> "When I first heard of XP, I thought pair programming was the *second* stupidest idea I'd ever heard. The stupidest was everyone working in the same team room (*not* an "open office"). But..." </p> <footer><a href="https://twitter.com/marick/status/1446618423312715779">Brian Marick</a></footer> </blockquote> <p> This seems to be a common experience with pair programming. Most people dislike it until they have actually tried it. </p> <p> Well, I've tried both pair programming and ensemble (AKA mob) programming, and I don't <em>like</em> it. </p> <p> That's all: It's my <em>preference</em> - not any sort of objective truth. What little scientific evidence we can find in our field does seem to indicate that pair programming is efficient. In my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> I've put aside my dislike to instead endorse pair and ensemble programming as something you should consider. </p> <p> There's enough evidence (anecdotal and otherwise) that it works well for many people, so give it a try. </p> <p> I also use it myself. While I find it exhausting, I find ensemble programming incredibly well-suited to knowledge transfer. I've used it to great success when teaching development organisations new ways of doing things. </p> <p> Even with the nicest people in the room, however, the process drains me. One reason is probably that I've a strong introvert side to my personality. </p> <p> Another perspective to consider is the role you assume. </p> <p> A common theme when people share stories of how they saw the light of pair programming is that they learned it from luminaries. If Kent Beck or Ward Cunningham personally tutors you, it's easy to see how it could feel like a revelation. </p> <p> On the other hand, <a href="https://en.wikipedia.org/wiki/Survivorship_bias">survivorship bias</a> could be at work. Perhaps Kent Beck showed pair programming and test-driven development to many people who never caught the bug, and thus never discussed it in public. </p> <p> In my own experience, I mostly taught myself test-driven development long before I tried pair programming, and I'd heard about pair programming long before I tried it. When I did try it, I wasn't the person being taught. I was in the role of the teacher. </p> <p> Teaching is both a satisfying and exhausting activity. I do consider myself a teacher of sorts, but I prefer to write. Whenever I've taught a workshop, given a lecture, or consulted, I'm always left happy but exhausted. It's really hard work. </p> <p> So is pair programming, in my experience. Efficient, most likely, but hard work. I can't muster much enthusiasm about it. </p> <h3 id="4b17192456614ca28a4e69cb5f86e23b"> REST <a href="#4b17192456614ca28a4e69cb5f86e23b" title="permalink">#</a> </h3> <p> Another topic about which I regularly disagree with others is <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a>. Just try to find some of my articles <a href="/tags/#REST-ref">tagged REST</a> and read the comments. </p> <p> For the record, the crowd who disagrees with me is a completely different set of people than those with whom I disagree about pair programming and other agile practices. </p> <p> The people who disagree with me about REST may be right, and I could be wrong. My views on REST are strongly influenced by early experience. Do be aware of the pattern. </p> <p> In early 2012 a client asked for my help designing a stable API. The customer didn't ask me to design a REST API - in fact, I think he had a <a href="https://en.wikipedia.org/wiki/SOAP">SOAP</a> API in mind, but he was open to other options. One requirement was clear, though: The API had to be exceptionally stable and backwards compatible. There was a reason for this. </p> <p> My customer's business was to provide a consumer-grade online service. They were currently talking to a hardware producer who'd include support for the service in consumer hardware. Imagine thousands (perhaps millions) of devices sitting in people's homes, using the online service via the API we were about to design. </p> <p> Even if the hardware producer were to enable firmware upgrades of the devices, there'd be no way we could roll out new versions of client software in a controlled way. This meant that backwards compatibility was a top priority. </p> <p> I'd recently learned enough about REST to see the opportunity, so I suggested it as a principle for designing APIs that could evolve without breaking backwards compatibility. </p> <p> The resulting REST API was a success, and I worked with that client for many years on other projects. </p> <p> This experience clearly shaped my view on REST. To me, the major benefit of REST is the ability to design evolvable APIs without breaking changes. It does work best, however, if you design <a href="https://martinfowler.com/articles/richardsonMaturityModel.html">level 3 REST APIs</a>. </p> <p> People use HTTP APIs for all sorts of other reasons. Perhaps the driving factor isn't evolvability, but rather interoperability. Perhaps they're developing <a href="https://samnewman.io/patterns/architectural/bff/">backends for frontends</a> or APIs strictly for internal use in an organisation. In some scenarios you can easier schedule updates of clients to coincide with updates to the API, in which case backwards compatibility is less of a concern. </p> <p> Another concern about API design is <a href="https://thoughtbot.com/blog/who-is-empowered-by-your-design">who's empowered by your design</a>. It seems fair to say that a level 2 REST API is an easier sell. To many client developers, that's all they've ever encountered - they've never seen a level 3 REST API. </p> <p> I readily admit that a level 3 REST API puts an additional up-front burden on client developers. Such a design is a long game. If the API is active for many years, such investments are likely to pay off, while it may not be worthwhile in the short term. It could even hurt initial adoption, so it's not a one-size-fits-all architectural choice. </p> <p> In the context of thought leadership, however, my point is that I acknowledge that my view on REST, too, is flavoured by initial personal success. </p> <h3 id="f6e7da07a61340ef9f1617cb5fb19071"> Conclusion <a href="#f6e7da07a61340ef9f1617cb5fb19071" title="permalink">#</a> </h3> <p> I think it's natural to latch on to certain practices via serendipity, You go through life without being aware of a thing that turns out to be highly compatible with your preferences in a given context. Until you one day <em>do</em> encounter it, and it changes your life. </p> <p> I consider this only human. It's certainly happened to me multiple times, and I'd be surprised if it doesn't happen to others. </p> <p> Perhaps the people extolling the virtues of pair programming had great initial experiences that they've managed to carry forward. For me, the experience has been another. </p> <p> Likewise, I had an initial positive experience with REST that surely influenced my position on its virtues. Other people could have had a negative experience, and naturally protest against my ideas. There's nothing wrong with that. </p> <blockquote> <p> "Only a crisis - actual or perceived - produces real change. When that crisis occurs, the actions that are taken depend on the ideas that are lying around. That, I believe, is our basic function: to develop alternatives to existing policies, to keep them alive and available until the politically impossible becomes the politically inevitable" </p> <footer><a href="https://amzn.to/3BsIqLG">Milton Friedman</a></footer> </blockquote> <p> Thought leadership strikes me as similar to Friedman's ideas on policy alternatives. I don't see my role as an <em>enforcer</em> of ideas. I write in order to keep certain ideas alive, in the hope that one day, someone picks them up and uses them. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Reader as a contravariant functor https://blog.ploeh.dk/2021/10/04/reader-as-a-contravariant-functor 2021-10-04T05:47:00+00:00 Mark Seemann <div id="post"> <p> <em>Any function gives rise to a contravariant functor. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2021/09/02/contravariant-functors">an article series about contravariant functors</a>. It assumes that you've read the introduction. In the <a href="/2021/09/06/the-command-handler-contravariant-functor">first example article</a>, you saw how the Command Handler pattern gives rise to a contravariant functor. <a href="/2021/09/09/the-specification-contravariant-functor">The next article</a> gave another example based on predicates. </p> <p> In the <a href="/2021/09/02/contravariant-functors">overview article</a> I also mentioned that equivalence and comparison form contravariant functors. Each can be described with an interface, or just function syntax. Let's put them in a table to compare them: </p> <p> <table border="1"> <thead> <tr> <th><em>Name</em></th> <th><em>C# method signature</em></th> <th><em>C# delegate(s)</em></th> <th><em>Haskell type(s)</em></th> </tr> </thead> <tbody> <tr> <td>Command Handler</td> <td><code><span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(TCommand&nbsp;<span style="color:#1f377f;">command</span>);</code></td> <td><code>Action&lt;TCommand&gt;</code></td> <td><code>a -&gt; ()</code><br><code>a -&gt; IO ()</code></td> </tr> <tr> <td>Specification</td> <td><code><span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">IsSatisfiedBy</span>(T&nbsp;<span style="color:#1f377f;">candidate</span>);</code></td> <td><code>Predicate&lt;T&gt;</code><br><code>Func&lt;T, bool&gt;</code></td> <td><code>a -&gt; Bool</code></td> </tr> <tr> <td>Equivalence</td> <td><code><span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(T&nbsp;<span style="color:#1f377f;">x</span>,&nbsp;T&nbsp;<span style="color:#1f377f;">y</span>);</code></td> <td><code>Func&lt;T, T, bool&gt;</code></td> <td><code>a -&gt; a -&gt; Bool</code></td> </tr> <tr> <td>Comparison</td> <td><code><span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">Compare</span>(T&nbsp;<span style="color:#1f377f;">x</span>,&nbsp;T&nbsp;<span style="color:#1f377f;">y</span>);</code></td> <td><code>Func&lt;T, T, int&gt;</code></td> <td><code>a -&gt; a -&gt; Ordering</code></td> </tr> </tbody> </table> </p> <p> In some cases, there's more than one possible representation. For example, in C# <a href="https://docs.microsoft.com/dotnet/api/system.predicate-1">Predicate</a> is isomorphic to <code>Func&lt;T, bool&gt;</code>. When it comes to the <a href="https://www.haskell.org">Haskell</a> representation of a Command Handler, the 'direct' translation of <code>Action&lt;T&gt;</code> is <code>a -&gt; ()</code>. In (Safe) Haskell, however, a function with that type is always a no-op. More realistically, a 'handler' function would have the type <code>a -&gt; IO ()</code> in order to allow side effects to happen. </p> <p> Do you notice a pattern? </p> <h3 id="920ac139cb1449789dac402d3e05c368"> Input variance <a href="#920ac139cb1449789dac402d3e05c368" title="permalink">#</a> </h3> <p> There's a pattern emerging from the above table. Notice that in all the examples, the function types are generic (AKA <em>parametrically polymorphic</em>) in their <em>input</em> types. </p> <p> This turns out to be part of a general rule. The actual rule is a little more complicated than that. I'll recommend <a href="https://reasonablypolymorphic.com">Sandy Maguire</a>'s <a href="https://www.goodreads.com/review/show/3406773267">excellent</a> book <a href="https://thinkingwithtypes.com">Thinking with Types</a> if you're interested in the details. </p> <p> For first-order functions, you can pick and fix <em>any</em> type as the return type and let the input type(s) vary: that function will give rise to a contravariant functor. </p> <p> In the above table, various handlers fix <code>void</code> (which is <a href="/2018/01/15/unit-isomorphisms">isomorphic to <em>unit</em></a> (<code>()</code>) as the output type and let the input type vary. Both Specification and Equivalence fix <code>bool</code> as the output type, and Comparison fix <code>int</code> (or, in Haskell, the more sane type <code>Ordering</code>), and allow the input type to vary. </p> <p> You can pick any other type. If you fix it as <em>the</em> output type for a function and let the input vary, you have the basis for a contravariant functor. </p> <h3 id="11651dbe49d04a6989c45fc52744ebf8"> Reader <a href="#11651dbe49d04a6989c45fc52744ebf8" title="permalink">#</a> </h3> <p> Consider this <code>IReader</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReader</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;<span style="color:#74531f;">Run</span>(R&nbsp;<span style="color:#1f377f;">environment</span>); }</pre> </p> <p> If you fix the environment type <code>R</code> and let the output type <code>A</code> vary, <a href="/2021/08/30/the-reader-functor">you have a (covariant) functor</a>. If, on the other hand, you fix the <em>output</em> type <code>A</code> and allow the input type <code>R</code> to vary, you can have yourself a contravariant functor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReader&lt;R1,&nbsp;A&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IReader&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">reader</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;R1,&nbsp;R&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;FuncReader&lt;R1,&nbsp;A&gt;(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;reader.Run(selector(r))); }</pre> </p> <p> As an example, you may have this (rather unwarranted) interface implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MinutesReader</span>&nbsp;:&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;TimeSpan&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TimeSpan&nbsp;<span style="color:#74531f;">Run</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;TimeSpan.FromMinutes(environment); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can fix the output type to <code>TimeSpan</code> and let the input type vary using the <code>ContraMap</code> functions: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">WrappedContravariantExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;<span style="color:blue;">int</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;MinutesReader(); &nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;<span style="color:blue;">string</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">projected</span>&nbsp;=&nbsp;r.ContraMap((<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>)&nbsp;=&gt;&nbsp;<span style="color:blue;">int</span>.Parse(s)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>&nbsp;TimeSpan(0,&nbsp;21,&nbsp;0),&nbsp;projected.Run(<span style="color:#a31515;">&quot;21&quot;</span>)); }</pre> </p> <p> When you <code>Run</code> the <code>projected</code> reader with the input string <code>"21"</code>, the <code>ContraMap</code> function first calls the <code>selector</code>, which (in this case) parses <code>"21"</code> to the integer <code>21</code>. It then calls <code>Run</code> on the 'original' <code>reader</code> with the value <code>21</code>. Since the 'original' <code>reader</code> is a <code>MinutesReader</code>, the output is a <code>TimeSpan</code> value that represents 21 minutes. </p> <h3 id="d147d424c6de412d9ad83cf659812ef6"> Raw functions <a href="#d147d424c6de412d9ad83cf659812ef6" title="permalink">#</a> </h3> <p> As was also the case when I introduced the Reader (covariant) functor, the <code>IReader</code> interface is just a teaching device. You don't need the interface in order to turn first-order functions into contravariant functors. It works on raw functions too: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;R1,&nbsp;A&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt;(<span style="color:blue;">this</span>&nbsp;Func&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">func</span>,&nbsp;Func&lt;R1,&nbsp;R&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;func(selector(r)); }</pre> </p> <p> In the following I'm going to dispense with the <code>IReader</code> interface and instead work with raw functions. </p> <h3 id="ad09f6dd9b3542b4ba8c01a01baa6afa"> Identity law <a href="#ad09f6dd9b3542b4ba8c01a01baa6afa" title="permalink">#</a> </h3> <p> A <code>ContraMap</code> method with the right signature isn't enough to be a contravariant functor. It must also obey the contravariant functor laws. As usual, it's proper computer-science work to actually prove this, but you can write some tests to demonstrate the identity law for functions. In this article, you'll see parametrised tests written with <a href="https://xunit.net">xUnit.net</a>. First, the identity law: </p> <p> <pre>[Theory] [InlineData(42)] [InlineData(1337)] [InlineData(2112)] [InlineData(90125)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ContravariantIdentityLaw</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">input</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;i.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;f.ContraMap((<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>)&nbsp;=&gt;&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(f(input),&nbsp;actual(input)); }</pre> </p> <p> Here I'm using the <code>(int i) =&gt; i</code> lambda expression as the identity function. As usual, you can't easily compare functions for equality, so you'll have to call them to verify that they produce the same output, which they do. </p> <h3 id="2e1d05c3d61c4773820dbbaec5967565"> Composition law <a href="#2e1d05c3d61c4773820dbbaec5967565" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that <code>ContraMap</code> obeys the composition law for contravariant functors: </p> <p> <pre>[Theory] [InlineData(4.2)] [InlineData(13.37)] [InlineData(21.12)] [InlineData(901.25)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ContravariantCompositionLaw</span>(<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">input</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">double</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;TimeSpan.FromSeconds(i); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;TimeSpan,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;ts.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h.ContraMap((<span style="color:blue;">double</span>&nbsp;<span style="color:#1f377f;">d</span>)&nbsp;=&gt;&nbsp;g(f(d)))(input), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h.ContraMap(g).ContraMap(f)(input)); }</pre> </p> <p> This test defines two local functions, <code>f</code> and <code>g</code>. Once more, you can't directly compare methods for equality, so instead you have to invoke both compositions to verify that they return the same <code>int</code> value. </p> <p> They do. </p> <h3 id="392a90e5e3204d459dae673d23be92e1"> Isomorphisms <a href="#392a90e5e3204d459dae673d23be92e1" title="permalink">#</a> </h3> <p> Now that we understand that any first-order function is contravariant, we can see that the previous examples of predicates, handlers, comparisons, and equivalences are really just special cases of the Reader contravariant functor. </p> <p> For example, <code>Predicate&lt;T&gt;</code> is trivially isomorphic to <code>Func&lt;T, bool&gt;</code>. Still, it might be worthwhile to flesh out how other translations might work: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;ISpecification&lt;T&gt;&nbsp;<span style="color:#74531f;">AsSpecification</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Predicate&lt;T&gt;&nbsp;<span style="color:#1f377f;">predicate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;DelegateSpecificationAdapter&lt;T&gt;(predicate); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;ISpecification&lt;T&gt;&nbsp;<span style="color:#74531f;">AsSpecification</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;Func&lt;T,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">predicate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;DelegateSpecificationAdapter&lt;T&gt;(predicate); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DelegateSpecificationAdapter</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;ISpecification&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Predicate&lt;T&gt;&nbsp;predicate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DelegateSpecificationAdapter</span>(Predicate&lt;T&gt;&nbsp;<span style="color:#1f377f;">predicate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.predicate&nbsp;=&nbsp;predicate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DelegateSpecificationAdapter</span>(Func&lt;T,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">predicate</span>)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>((Predicate&lt;T&gt;)(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;predicate(x))) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">IsSatisfiedBy</span>(T&nbsp;<span style="color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;predicate(candidate); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Predicate&lt;T&gt;&nbsp;<span style="color:#74531f;">AsPredicate</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;ISpecification&lt;T&gt;&nbsp;<span style="color:#1f377f;">specification</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">candidate</span>&nbsp;=&gt;&nbsp;specification.IsSatisfiedBy(candidate); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;T, bool&gt;&nbsp;<span style="color:#74531f;">AsFunc</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;ISpecification&lt;T&gt;&nbsp;<span style="color:#1f377f;">specification</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">candidate</span>&nbsp;=&gt;&nbsp;specification.IsSatisfiedBy(candidate); }</pre> </p> <p> Above are conversions between <code>ISpecification&lt;T&gt;</code> on the one hand, and <code>Predicate&lt;T&gt;</code> and <code>Func&lt;T, bool&gt;</code> on the other. Not shown are the conversions between <code>Predicate&lt;T&gt;</code> and <code>Func&lt;T, bool&gt;</code>, since they are already built into C#. </p> <p> Most saliently in this context is that it's possible to convert both <code>ISpecification&lt;T&gt;</code> and <code>Predicate&lt;T&gt;</code> to <code>Func&lt;T, bool&gt;</code>, and <code>Func&lt;T, bool&gt;</code> to <code>ISpecification&lt;T&gt;</code> or <code>Predicate&lt;T&gt;</code> without any loss of information. Specifications and predicates are isomorphic to an open constructed <code>Func</code> - that is, a Reader. </p> <p> I'll leave the other isomorphisms as exercises, with the following hints: </p> <ul> <li>You can only convert an <code>ICommandHandler&lt;T&gt;</code> to a <code>Func</code> if you introduce a <code>Unit</code> value, but you could also try to use <code>Action&lt;T&gt;</code>.</li> <li>For Equivalence, you'll need to translate the two input arguments to a single object or value.</li> <li>The same goes for Comparison.</li> </ul> <p> All the contravariant functor examples shown so far in this article series are isomorphic to the Reader contravariant functor. </p> <p> Particularly, this also explains why <a href="/2021/09/27/the-equivalence-contravariant-functor">it was possible to make <code>IEqualityComparer.GetHashCode</code> contravariant</a>. </p> <h3 id="fa845ae308634182bf6e82298205c9a8"> Haskell <a href="#fa845ae308634182bf6e82298205c9a8" title="permalink">#</a> </h3> <p> The Haskell <em>base</em> package comes with a <a href="https://hackage.haskell.org/package/base/docs/Data-Functor-Contravariant.html">Contravariant type class</a> and various instances. </p> <p> In order to replicate the above <code>MinutesReader</code> example, we can start by implementing a function with equivalent behaviour: </p> <p> <pre>Prelude Data.Functor.Contravariant Data.Time&gt; minutes m = secondsToDiffTime (60 * m) Prelude Data.Functor.Contravariant Data.Time&gt; :t minutes minutes :: Integer -&gt; DiffTime</pre> </p> <p> As GHCi reports, the <code>minutes</code> function has the type <code>Integer -&gt; DiffTime</code> (<code>DiffTime</code> corresponds to .NET's <code>TimeSpan</code>). </p> <p> The above C# example contramaps a <code>MinutesReader</code> with a function that parses a <code>string</code> to an <code>int</code>. In Haskell, we can use the built-in <code>read</code> function to equivalent effect. </p> <p> Here's where Haskell gets a little odd. In order to fit the <code>Contravariant</code> type class, we need to flip the type arguments of a function. A normal function is usually written as having the type <code>a -&gt; b</code>, but we can also write it as the type <code>(-&gt;) a b</code>. With this notation, <code>minutes</code> has the type <code>(-&gt;) Integer DiffTime</code>. </p> <p> In order to make <code>minutes</code> a contravariant instance, we need to fix <code>DiffTime</code> and let the input vary. What we'd like to have is something like this: <code>(-&gt;) a DiffTime</code>. Alas, that's not how you define a legal type class instance in Haskell. We have to flip the types around so that we can partially apply the type. The built-in <code>newtype Op</code> does that: </p> <p> <pre>Prelude Data.Functor.Contravariant Data.Time&gt; :t Op minutes Op minutes :: Op DiffTime Integer</pre> </p> <p> Since the general, partially applied type <code>Op a</code> is a <code>Contravariant</code> instance, it follows that the specific type <code>Op DiffTime</code> is. This means that we can <code>contramap</code> <code>Op minutes</code> with <code>read</code>: </p> <p> <pre>Prelude Data.Functor.Contravariant Data.Time&gt; :t contramap read (Op minutes) contramap read (Op minutes) :: Op DiffTime String</pre> </p> <p> Notice that this maps an <code>Op DiffTime Integer</code> to an <code>Op DiffTime String</code>. </p> <p> How do you use it? </p> <p> You can retrieve the function wrapped in <code>Op</code> with the <code>getOp</code> function: </p> <p> <pre>Prelude Data.Functor.Contravariant Data.Time&gt; :t getOp (contramap read (Op minutes)) getOp (contramap read (Op minutes)) :: String -&gt; DiffTime</pre> </p> <p> As you can tell, this expression indicates a <code>String -&gt; DiffTime</code> function. This means that if you call it with a string representation of an integer, you should get a <code>DiffTime</code> value back: </p> <p> <pre>Prelude Data.Functor.Contravariant Data.Time&gt; getOp (contramap read (Op minutes)) "21" 1260s</pre> </p> <p> As usual, this is way too complicated to be immediately useful, but it once again demonstrates that contravariant functors are ubiquitous. </p> <h3 id="c5952616e8b24528900fcdc9b2b75535"> Conclusion <a href="#c5952616e8b24528900fcdc9b2b75535" title="permalink">#</a> </h3> <p> Normal first-order functions give rise to contravariant functors. With sufficiently tinted glasses, <a href="/2018/01/08/software-design-isomorphisms">most programming constructs look like functions</a>. To me, at least, this indicates that a contravariant functor is a fundamental abstraction in programming. </p> <p> This result looks quite abstract, but <a href="/2021/11/29/postels-law-as-a-profunctor">future articles</a> will build on it to arrive at a (to me) fascinating conclusion. Until then, though... </p> <p> <strong>Next:</strong> <a href="/2021/10/25/functor-variance-compared-to-cs-notion-of-variance">Functor variance compared to C#'s notion of variance</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Equivalence contravariant functor https://blog.ploeh.dk/2021/09/27/the-equivalence-contravariant-functor 2021-09-27T06:08:00+00:00 Mark Seemann <div id="post"> <p> <em>An introduction to the Equivalence contravariant functor for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2021/09/02/contravariant-functors">an article series about contravariant functors</a>. It assumes that you've read the introduction. In previous articles, you saw examples of <a href="/2021/09/06/the-command-handler-contravariant-functor">the Command Handler</a> and <a href="/2021/09/09/the-specification-contravariant-functor">Specification</a> contravariant functors. This article presents another example. </p> <p> In <a href="/2021/09/20/keep-ids-internal-with-rest">a recent article</a> I described how I experimented with removing the <code>id</code> property from a JSON representation in a REST API. I also mentioned that doing that made one test fail. In this article you'll see the failing test and how the Equivalence contravariant functor can improve the situation. </p> <h3 id="be01e1b437a94b2689f5fdd3a68ae33f"> Baseline <a href="#be01e1b437a94b2689f5fdd3a68ae33f" title="permalink">#</a> </h3> <p> Before I made the change, the test in question looked like this: </p> <p> <pre>[Theory] [InlineData(1049,&nbsp;19,&nbsp;00,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [InlineData(1130,&nbsp;18,&nbsp;15,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] [InlineData(&nbsp;956,&nbsp;16,&nbsp;55,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] [InlineData(&nbsp;433,&nbsp;17,&nbsp;30,&nbsp;<span style="color:#a31515;">&quot;shli@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Shanghai&nbsp;Li&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">PostValidReservationWhenDatabaseIsEmpty</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">days</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">hours</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">minutes</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">email</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">at</span>&nbsp;=&nbsp;DateTime.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;TimeSpan(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">db</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;FakeDatabase(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;SystemClock(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;InMemoryRestaurantDatabase(Grandfather.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDto &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;<span style="color:#a31515;">&quot;B50DF5B1-F484-4D99-88F9-1915087AF568&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at.ToString(<span style="color:#a31515;">&quot;O&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Guid.Parse(dto.Id), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Contains(expected,&nbsp;db.Grandfather); }</pre> </p> <p> You can find this test in the code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>, although I've slightly simplified the initialisation of <code>expected</code> since I froze the code for the manuscript. I've already discussed this particular test in the articles <a href="/2020/12/07/branching-tests">Branching tests</a>, <a href="/2021/01/11/waiting-to-happen">Waiting to happen</a>, and <a href="/2021/01/18/parametrised-test-primitive-obsession-code-smell">Parametrised test primitive obsession code smell</a>. It's the gift that keeps giving. </p> <p> It's a <a href="/2019/04/01/an-example-of-state-based-testing-in-c">state-based integration test</a> that verifies the state of the <code>FakeDatabase</code> after 'posting' a reservation to 'the REST API'. I'm using quotes because the test doesn't really perform an HTTP POST request (it's not <a href="/2021/01/25/self-hosted-integration-tests-in-aspnet">a self-hosted integration test</a>). Rather, it directly calls the <code>Post</code> method on the <code>sut</code>. In the assertion phase, it uses Back Door Manipulation (as <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> terms it) to verify the state of the <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> <code>db</code>. </p> <p> If you're wondering about the <code>Grandfather</code> property, <a href="/2020/11/16/redirect-legacy-urls">it represents the original restaurant that was grandfathered in</a> when I expanded the REST API to a multi-tenant service. </p> <p> Notice, particularly, the use of <code>dto.Id</code> when defining the <code>expected</code> reservation. </p> <h3 id="be8ae690e33a4e7fb3b5d5f15d6ddce9"> Brittle assertion <a href="#be8ae690e33a4e7fb3b5d5f15d6ddce9" title="permalink">#</a> </h3> <p> When I <a href="/2021/09/20/keep-ids-internal-with-rest">made the <code>Id</code> property <code>internal</code></a>, this test no longer compiled. I had to delete the assignment of <code>Id</code>, which also meant that I couldn't use a deterministic <code>Guid</code> to define the <code>expected</code> value. While I could create an arbitrary <code>Guid</code>, that would never pass the test, since the <code>Post</code> method also generates a new <code>Guid</code>. </p> <p> In order to <a href="/2019/10/21/a-red-green-refactor-checklist">get to green</a> as quickly as possible, I rewrote the assertion: </p> <p> <pre>Assert.Contains( &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;&nbsp;&nbsp;DateTime.Parse(dto.At,&nbsp;CultureInfo.InvariantCulture)&nbsp;==&nbsp;r.At &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(dto.Email)&nbsp;==&nbsp;r.Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(dto.Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>)&nbsp;==&nbsp;r.Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;dto.Quantity&nbsp;==&nbsp;r.Quantity);</pre> </p> <p> This passed the test so that I could move on. It may even be the simplest thing that could possibly work, but it's brittle and noisy. </p> <p> It's brittle because it explicitly considers the four properties <code>At</code>, <code>Email</code>, <code>Name</code>, and <code>Quantity</code> of the <code>Reservation</code> class. What happens if you add a new property to <code>Reservation</code>? What happens if you have similar assertions scattered over the code base? </p> <p> This is one reason that <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> also applies to unit tests. You want to have as few places as possible that you have to edit when making changes. Otherwise, the risk increases that you forget one or more. </p> <p> Not only is the assertion brittle - it's also noisy, because it's hard to read. There's parsing, null coalescing and object initialisation going on in those four lines of Boolean operations. Perhaps it'd be better to extract a well-named helper method, but while I'm often in favour of doing that, I'm also a little concerned that too many ad-hoc helper methods obscure something essential. After all: </p> <blockquote> <p> "Abstraction is the elimination of the irrelevant and the amplification of the essential" </p> <footer><cite>Robert C. Martin, <a href="http://amzn.to/19W4JHk">APPP</a></cite></footer> </blockquote> <p> The hardest part of abstraction is striking the right balance. Does a well-named helper method effectively communicate the essentials while eliminating <em>only</em> the irrelevant. While I favour good names over bad names, I'm also aware that <a href="/2020/11/23/good-names-are-skin-deep">good names are skin-deep</a>. If I can draw on a universal abstraction rather than coming up with an ad-hoc name, I prefer doing that. </p> <p> Which universal abstraction might be useful in this situation? </p> <h3 id="724d8609add4401cae58a14c259c5698"> Relaxed comparison <a href="#724d8609add4401cae58a14c259c5698" title="permalink">#</a> </h3> <p> The baseline version of the test relied on the structural equality of the <code>Reservation</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>?&nbsp;<span style="color:#1f377f;">obj</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id.Equals(reservation.Id)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;==&nbsp;reservation.At&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;Email&gt;.Default.Equals(Email,&nbsp;reservation.Email)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;Name&gt;.Default.Equals(Name,&nbsp;reservation.Name)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;==&nbsp;reservation.Quantity; }</pre> </p> <p> This implementation was auto-generated by a Visual Studio <em>Quick Action</em>. From C# 9, I could also have made <code>Reservation</code> a <a href="https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/record">record</a>, in which case the compiler would be taking care of implementing <code>Equals</code>. </p> <p> The <code>Reservation</code> class already defines the canonical way to compare two reservations for equality. Why can't we use that? </p> <p> The <code>PostValidReservationWhenDatabaseIsEmpty</code> test can no longer use the <code>Reservation</code> class' structural equality because it doesn't know what the <code>Id</code> is going to be. </p> <p> One way to address this problem is to inject a hypothetical <code>IGuidGenerator</code> dependency into <code>ReservationsController</code>. I consider this a valid alternative, since the Controller already takes an <code>IClock</code> dependency. I might be inclined towards such a course of action <a href="/2020/03/23/repeatable-execution">for other reasons</a>, but here I wanted to explore other options. </p> <p> Can we somehow reuse the <code>Equals</code> implementation of <code>Reservation</code>, but relax its behaviour so that it doesn't consider the <code>Id</code>? </p> <p> This would be <a href="https://docs.microsoft.com/archive/msdn-magazine/2010/october/msdn-magazine-the-working-programmer-multiparadigmatic-net-part-2">what Ted Neward called <em>negative variability</em></a> - the ability to subtract from an existing feature. As he implied in 2010, normal programming languages don't have that capability. That strikes me as true in 2021 as well. </p> <p> The best we can hope for, then, is to put the required custom comparison somewhere central, so that at least it's not scattered across the entire code base. Since the test uses <a href="https://xunit.net">xUnit.net</a>, a class that implements <code>IEqualityComparer&lt;Reservation&gt;</code> sounds like just the right solution. </p> <p> This is definitely doable, but it's odd having to define a custom equality comparer for a class that already has structural equality. In the context of the <code>PostValidReservationWhenDatabaseIsEmpty</code> test, we understand the reason, but for a future team member who may encounter the class out of context, it might be confusing. </p> <p> Are there other options? </p> <h3 id="df0297c4c0274368843e9e0f5bb98cba"> Reuse <a href="#df0297c4c0274368843e9e0f5bb98cba" title="permalink">#</a> </h3> <p> It turns out that, by lucky accident, the code base already contains an equality comparer that almost fits: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationDtoComparer</span>&nbsp;:&nbsp;IEqualityComparer&lt;ReservationDto&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(ReservationDto?&nbsp;<span style="color:#1f377f;">x</span>,&nbsp;ReservationDto?&nbsp;<span style="color:#1f377f;">y</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">datesAreEqual</span>&nbsp;=&nbsp;Equals(x?.At,&nbsp;y?.At); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!datesAreEqual&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime.TryParse(x?.At,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">xDate</span>)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime.TryParse(y?.At,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">yDate</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datesAreEqual&nbsp;=&nbsp;Equals(xDate,&nbsp;yDate); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;datesAreEqual &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(x?.Email,&nbsp;y?.Email) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(x?.Name,&nbsp;y?.Name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(x?.Quantity,&nbsp;y?.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>(ReservationDto&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dateHash</span>&nbsp;=&nbsp;obj.At?.GetHashCode(StringComparison.InvariantCulture); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(DateTime.TryParse(obj.At,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">dt</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dateHash&nbsp;=&nbsp;dt.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;HashCode.Combine(dateHash,&nbsp;obj.Email,&nbsp;obj.Name,&nbsp;obj.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class already compares two reservations' dates, emails, names, and quantities, while ignoring any IDs. Just what we need? </p> <p> There's only one problem. <code>ReservationDtoComparer</code> compares <code>ReservationDto</code> objects - not <code>Reservation</code> objects. </p> <p> Would it be possible to somehow, on the spot, without writing a new class, transform <code>ReservationDtoComparer</code> to an <code>IEqualityComparer&lt;Reservation&gt;</code>? </p> <p> Well, yes it is. </p> <h3 id="affeb3f683624666a9c1bb3b0bae342a"> Contravariant functor <a href="#affeb3f683624666a9c1bb3b0bae342a" title="permalink">#</a> </h3> <p> We can contramap an <code>IEqualityComparer&lt;ReservationDto&gt;</code> to a <code>IEqualityComparer&lt;Reservation&gt;</code> because <em>equivalence</em> gives rise to a contravariant functor. </p> <p> In order to enable contravariant mapping, you must add a <code>ContraMap</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Equivalance</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEqualityComparer&lt;T1&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IEqualityComparer&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>)&nbsp;<span style="color:blue;">where</span>&nbsp;T&nbsp;:&nbsp;<span style="color:blue;">notnull</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ContraMapComparer&lt;T,&nbsp;T1&gt;(source,&nbsp;selector); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ContraMapComparer</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;:&nbsp;IEqualityComparer&lt;T1&gt;&nbsp;<span style="color:blue;">where</span>&nbsp;T&nbsp;:&nbsp;<span style="color:blue;">notnull</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IEqualityComparer&lt;T&gt;&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ContraMapComparer</span>(IEqualityComparer&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>,&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.source&nbsp;=&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.selector&nbsp;=&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>([AllowNull]&nbsp;T1&nbsp;<span style="color:#1f377f;">x</span>,&nbsp;[AllowNull]&nbsp;T1&nbsp;<span style="color:#1f377f;">y</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(x&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>&nbsp;&amp;&amp;&nbsp;y&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(x&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>&nbsp;||&nbsp;y&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.Equals(selector(x),&nbsp;selector(y)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>(T1&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.GetHashCode(selector(obj)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since the <code>IEqualityComparer&lt;T&gt;</code> interface defines <em>two</em> methods, the <code>selector</code> must contramap the behaviour of both <code>Equals</code> and <code>GetHashCode</code>. Fortunately, that's possible. </p> <p> Notice that, as explained in the overview article, in order to map from an <code>IEqualityComparer&lt;T&gt;</code> to an <code>IEqualityComparer&lt;T1&gt;</code>, the <code>selector</code> has to go the other way: from <code>T1</code> to <code>T</code>. How this is possible will become more apparent with an example, which will follow later in the article. </p> <h3 id="911357268ea14f09ac03bac6a781333e"> Identity law <a href="#911357268ea14f09ac03bac6a781333e" title="permalink">#</a> </h3> <p> A <code>ContraMap</code> method with the right signature isn't enough to be a contravariant functor. It must also obey the contravariant functor laws. As usual, it's proper computer-science work to actually prove this, but you can write some tests to demonstrate the identity law for the <code>IEqualityComparer&lt;T&gt;</code> interface. In this article, you'll see parametrised tests written with xUnit.net. First, the identity law: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;18:30&quot;</span>,&nbsp;1,&nbsp;<span style="color:#a31515;">&quot;18:30&quot;</span>,&nbsp;1)] [InlineData(<span style="color:#a31515;">&quot;18:30&quot;</span>,&nbsp;2,&nbsp;<span style="color:#a31515;">&quot;18:30&quot;</span>,&nbsp;2)] [InlineData(<span style="color:#a31515;">&quot;19:00&quot;</span>,&nbsp;1,&nbsp;<span style="color:#a31515;">&quot;19:00&quot;</span>,&nbsp;1)] [InlineData(<span style="color:#a31515;">&quot;18:30&quot;</span>,&nbsp;1,&nbsp;<span style="color:#a31515;">&quot;19:00&quot;</span>,&nbsp;1)] [InlineData(<span style="color:#a31515;">&quot;18:30&quot;</span>,&nbsp;2,&nbsp;<span style="color:#a31515;">&quot;18:30&quot;</span>,&nbsp;1)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IdentityLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">time1</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">size1</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">time2</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">size2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TimeDtoComparer(); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">id</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;IEqualityComparer&lt;TimeDto&gt;?&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.ContraMap&lt;TimeDto,&nbsp;TimeDto&gt;(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto1</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TimeDto&nbsp;{&nbsp;Time&nbsp;=&nbsp;time1,&nbsp;MaximumPartySize&nbsp;=&nbsp;size1&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto2</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TimeDto&nbsp;{&nbsp;Time&nbsp;=&nbsp;time2,&nbsp;MaximumPartySize&nbsp;=&nbsp;size2&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(sut.Equals(dto1,&nbsp;dto2),&nbsp;actual.Equals(dto1,&nbsp;dto2)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(sut.GetHashCode(dto1),&nbsp;actual.GetHashCode(dto1)); }</pre> </p> <p> In order to observe that the two comparers have identical behaviours, the test must invoke both the <code>Equals</code> and the <code>GetHashCode</code> methods on both <code>sut</code> and <code>actual</code> to assert that the two different objects produce the same output. </p> <p> All test cases pass. </p> <h3 id="c2228ba4c0044dc8b90ad92354850a22"> Composition law <a href="#c2228ba4c0044dc8b90ad92354850a22" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that <code>ContraMap</code> obeys the composition law for contravariant functors: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;&nbsp;7:45&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;18:13&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;18:13&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;18:13&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;22&quot;</span>&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;22&quot;</span>&nbsp;&nbsp;&nbsp;)] [InlineData(<span style="color:#a31515;">&quot;22:32&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;22&quot;</span>&nbsp;&nbsp;&nbsp;)] [InlineData(&nbsp;<span style="color:#a31515;">&quot;9&quot;</span>&nbsp;&nbsp;&nbsp;,&nbsp;&nbsp;<span style="color:#a31515;">&quot;9&quot;</span>&nbsp;&nbsp;&nbsp;)] [InlineData(&nbsp;<span style="color:#a31515;">&quot;9&quot;</span>&nbsp;&nbsp;&nbsp;,&nbsp;&nbsp;<span style="color:#a31515;">&quot;8&quot;</span>&nbsp;&nbsp;&nbsp;)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CompositionLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">time1</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">time2</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IEqualityComparer&lt;TimeDto&gt;&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TimeDtoComparer(); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;(<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>)&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;(s,&nbsp;s.Length); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;(<span style="color:blue;">string</span>&nbsp;s,&nbsp;<span style="color:blue;">int</span>&nbsp;i),&nbsp;TimeDto&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;TimeDto&nbsp;{&nbsp;Time&nbsp;=&nbsp;t.s,&nbsp;MaximumPartySize&nbsp;=&nbsp;t.i&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;IEqualityComparer&lt;<span style="color:blue;">string</span>&gt;?&nbsp;<span style="color:#1f377f;">projection1</span>&nbsp;=&nbsp;sut.ContraMap((<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>)&nbsp;=&gt;&nbsp;g(f(s))); &nbsp;&nbsp;&nbsp;&nbsp;IEqualityComparer&lt;<span style="color:blue;">string</span>&gt;?&nbsp;<span style="color:#1f377f;">projection2</span>&nbsp;=&nbsp;sut.ContraMap(g).ContraMap(f); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;projection1.Equals(time1,&nbsp;time2), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;projection2.Equals(time1,&nbsp;time2)); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;projection1.GetHashCode(time1), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;projection2.GetHashCode(time1)); }</pre> </p> <p> This test defines two local functions, <code>f</code> and <code>g</code>. Once more, you can't directly compare methods for equality, so instead you have to call both <code>Equals</code> and <code>GetHashCode</code> on <code>projection1</code> and <code>projection2</code> to verify that they return the same values. </p> <p> They do. </p> <h3 id="857e86fbf0944fe4b985cae73a9edb4b"> Relaxed assertion <a href="#857e86fbf0944fe4b985cae73a9edb4b" title="permalink">#</a> </h3> <p> The code base already contains a function that converts <code>Reservation</code> values to <code>ReservationDto</code> objects: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;ReservationDto&nbsp;<span style="color:#74531f;">ToDto</span>(<span style="color:blue;">this</span>&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(reservation)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDto &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;reservation.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;reservation.At.ToIso8601DateTimeString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;reservation.Email.ToString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;reservation.Name.ToString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;reservation.Quantity &nbsp;&nbsp;&nbsp;&nbsp;}; }</pre> </p> <p> Given that it's possible to map from <code>Reservation</code> to <code>ReservationDto</code>, it's also possible to map equality comparers in the contrary direction: from <code>IEqualityComparer&lt;ReservationDto&gt;</code> to <code>IEqualityComparer&lt;Reservation&gt;</code>. That's just what the <code>PostValidReservationWhenDatabaseIsEmpty</code> test needs! </p> <p> Most of the test stays the same, but you can now write the assertion as: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;Guid.NewGuid(), &nbsp;&nbsp;&nbsp;&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Email(email), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Name(name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;quantity); Assert.Contains( &nbsp;&nbsp;&nbsp;&nbsp;expected, &nbsp;&nbsp;&nbsp;&nbsp;db.Grandfather, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDtoComparer().ContraMap((Reservation&nbsp;<span style="color:#1f377f;">r</span>)&nbsp;=&gt;&nbsp;r.ToDto()));</pre> </p> <p> Instead of using the too-strict equality comparison of <code>Reservation</code>, the assertion now takes advantage of the relaxed, test-specific comparison of <code>ReservationDto</code> objects. </p> <p> What's not to like? </p> <p> To be truthful, this probably isn't a trick I'll perform often. I think it's fair to consider contravariant functors an advanced programming concept. On a team, I'd be concerned that colleagues wouldn't understand what's going on here. </p> <p> The purpose of this article series isn't to advocate for this style of programming. It's to show some realistic examples of contravariant functors. </p> <p> Even in <a href="https://www.haskell.org">Haskell</a>, where contravariant functors are en explicit part of <a href="https://hackage.haskell.org/package/base/docs/index.html">the <em>base</em> package</a>, I can't recall having availed myself of this functionality. </p> <h3 id="50a1d26e56054e8f896af110d176e3a5"> Equivalence in Haskell <a href="#50a1d26e56054e8f896af110d176e3a5" title="permalink">#</a> </h3> <p> The <a href="https://hackage.haskell.org/package/base/docs/Data-Functor-Contravariant.html">Haskell <em>Data.Functor.Contravariant</em> module</a> defines a <code>Contravariant</code> type class and some instances to go with it. One of these is a <code>newtype</code> called <code>Equivalence</code>, which is just a wrapper around <code>a -&gt; a -&gt; Bool</code>. </p> <p> In Haskell, equality is normally defined by the <code>Eq</code> type class. You can trivially 'promote' any <code>Eq</code> instance to an <code>Equivalence</code> instance using the <code>defaultEquivalence</code> value. </p> <p> To illustrate how this works in Haskell, you can reproduce the two reservation types: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Reservation&nbsp;=&nbsp;Reservation&nbsp;{ &nbsp;&nbsp;<span style="color:#2b91af;">reservationID</span>&nbsp;::&nbsp;<span style="color:blue;">UUID</span>, &nbsp;&nbsp;<span style="color:#2b91af;">reservationAt</span>&nbsp;::&nbsp;<span style="color:blue;">LocalTime</span>, &nbsp;&nbsp;<span style="color:#2b91af;">reservationEmail</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>, &nbsp;&nbsp;<span style="color:#2b91af;">reservationName</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>, &nbsp;&nbsp;<span style="color:#2b91af;">reservationQuantity</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) <span style="color:blue;">data</span>&nbsp;ReservationJson&nbsp;=&nbsp;ReservationJson&nbsp;{ &nbsp;&nbsp;<span style="color:#2b91af;">jsonAt</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>, &nbsp;&nbsp;<span style="color:#2b91af;">jsonEmail</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>, &nbsp;&nbsp;<span style="color:#2b91af;">jsonName</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>, &nbsp;&nbsp;<span style="color:#2b91af;">jsonQuantity</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Double</span>&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>,&nbsp;<span style="color:#2b91af;">Generic</span>)</pre> </p> <p> The <code>ReservationJson</code> type doesn't have an ID, whereas <code>Reservation</code> does. Still, you can easily convert from <code>Reservation</code> to <code>ReservationJson</code>: </p> <p> <pre><span style="color:#2b91af;">reservationToJson</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationJson</span> reservationToJson&nbsp;(Reservation&nbsp;_&nbsp;at&nbsp;email&nbsp;name&nbsp;q)&nbsp;= &nbsp;&nbsp;ReservationJson&nbsp;(<span style="color:blue;">show</span>&nbsp;at)&nbsp;email&nbsp;name&nbsp;(<span style="color:blue;">fromIntegral</span>&nbsp;q)</pre> </p> <p> Now imagine that you have two reservations that differ only on <code>reservationID</code>: </p> <p> <pre><span style="color:#2b91af;">reservation1</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span> reservation1&nbsp;= &nbsp;&nbsp;Reservation &nbsp;&nbsp;&nbsp;&nbsp;(fromWords&nbsp;3822151499&nbsp;288494060&nbsp;2147588346&nbsp;2611157519) &nbsp;&nbsp;&nbsp;&nbsp;(LocalTime&nbsp;(fromGregorian&nbsp;2021&nbsp;11&nbsp;11)&nbsp;(TimeOfDay&nbsp;12&nbsp;30&nbsp;0)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;just.inhale@example.net&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Justin&nbsp;Hale&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;2 <span style="color:#2b91af;">reservation2</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span> reservation2&nbsp;= &nbsp;&nbsp;Reservation &nbsp;&nbsp;&nbsp;&nbsp;(fromWords&nbsp;1263859666&nbsp;288625132&nbsp;2147588346&nbsp;2611157519) &nbsp;&nbsp;&nbsp;&nbsp;(LocalTime&nbsp;(fromGregorian&nbsp;2021&nbsp;11&nbsp;11)&nbsp;(TimeOfDay&nbsp;12&nbsp;30&nbsp;0)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;just.inhale@example.net&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Justin&nbsp;Hale&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;2</pre> </p> <p> If you compare these two values using the standard equality operator, they're (not surprisingly) not the same: </p> <p> <pre>&gt; reservation1 == reservation2 False</pre> </p> <p> Attempting to compare them using the default <code>Equivalence</code> value doesn't help, either: </p> <p> <pre>&gt; (getEquivalence $ defaultEquivalence) reservation1 reservation2 False</pre> </p> <p> But if you promote the comparison to <code>Equivalence</code> and then <code>contramap</code> it with <code>reservationToJson</code>, they do look the same: </p> <p> <pre>&gt; (getEquivalence $ contramap reservationToJson $ defaultEquivalence) reservation1 reservation2 True</pre> </p> <p> This Haskell example is equivalent in spirit to the above C# assertion. </p> <p> Notice that <code>Equivalence</code> is only a wrapper around any function of the type <code>a -&gt; a -&gt; Bool</code>. This corresponds to the <code>IEqualityComparer</code> interface's <code>Equals</code> method. On the other hand, <code>Equivalence</code> has no counterpart to <code>GetHashCode</code> - that's a .NETism. </p> <p> When using Haskell as inspiration for identifying universal abstractions, it's not entirely clear how <code>Equivalence</code> is similar to <code>IEqualityComparer&lt;T&gt;</code>. While <code>a -&gt; a -&gt; Bool</code> is isomorphic to its <code>Equals</code> method, and thus gives rise to a contravariant functor, what about the <code>GetHashCode</code> method? </p> <p> As this article has demonstrated, it turned out that it's possible to also contramap the <code>GetHashCode</code> method, but was that just a fortunate accident, or is there something more fundamental going on? </p> <h3 id="d51dbd1743e14420ab8c38b25bc02369"> Conclusion <a href="#d51dbd1743e14420ab8c38b25bc02369" title="permalink">#</a> </h3> <p> Equivalence relations give rise to a contravariant functor. In this article, you saw how this property can be used to relax assertions in unit tests. </p> <p> Strictly speaking, an equivalence relation is exclusively a function that compares two values to return a Boolean value. No <code>GetHashCode</code> method is required. That's a .NET-specific implementation detail that, unfortunately, has been allowed to leak into the <code>object</code> base class. It's not part of the concept of an equivalence relation, but still, it's possible to form a contravariant functor from <code>IEqualityComparer&lt;T&gt;</code>. Is this just a happy coincidence, or could there be something more fundamental going on? </p> <p> Read on. </p> <p> <strong>Next:</strong> <a href="/2021/10/04/reader-as-a-contravariant-functor">Reader as a contravariant functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Keep IDs internal with REST https://blog.ploeh.dk/2021/09/20/keep-ids-internal-with-rest 2021-09-20T06:21:00+00:00 Mark Seemann <div id="post"> <p> <em>Instead of relying on entity IDs, use hypermedia to identify resources.</em> </p> <p> Whenever I've helped teams design HTTP APIs, sooner or later one request comes up - typically from client developers: <em>Please add the entity ID to the representation.</em> </p> <p> In this article I'll show an alternative, but first: the normal state of affairs. </p> <h3 id="24703d08faec4fe6a650599e54e8dbf8"> Business as usual <a href="#24703d08faec4fe6a650599e54e8dbf8" title="permalink">#</a> </h3> <p> It's such a common requirement that, despite admonitions not to expose IDs, I did it myself in the code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. This code base is a <a href="https://martinfowler.com/articles/richardsonMaturityModel.html">level 3 REST API</a>, and still, I'd included the ID in the JSON representation of a reservation: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;bf4e84130dac451b9c94049da8ea8c17&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-08T20:30:00.0000000&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;snomob@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Snow&nbsp;Moe&nbsp;Beal&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 }</pre> </p> <p> At least the ID is a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">GUID</a>, so I'm <a href="/2014/08/11/cqs-versus-server-generated-ids">not exposing internal database IDs</a>. </p> <p> After having written the book, the <code>id</code> property kept nagging me, and I wondered if it'd be possible to get rid of it. After all, in a true REST API, clients aren't supposed to construct URLs from templates. They're supposed to follow links. So why do you need the ID? </p> <h3 id="e480deb4e2b3470a847fc2995bdf406d"> Following links <a href="#e480deb4e2b3470a847fc2995bdf406d" title="permalink">#</a> </h3> <p> Early on in the system's lifetime, I began <a href="/2020/10/26/fit-urls">signing all URLs</a> to prevent clients from <a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls">retro-engineering URLs</a>. This also meant that most of my <a href="/2021/01/25/self-hosted-integration-tests-in-aspnet">self-hosted integration tests</a> were already following links: </p> <p> <pre>[Theory] [InlineData(867,&nbsp;19,&nbsp;10,&nbsp;<span style="color:#a31515;">&quot;adur@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Adrienne&nbsp;Ursa&quot;</span>,&nbsp;2)] [InlineData(901,&nbsp;18,&nbsp;55,&nbsp;<span style="color:#a31515;">&quot;emol@example.gov&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Emma&nbsp;Olsen&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">ReadSuccessfulReservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">days</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">hours</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">minutes</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">email</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">api</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;LegacyApi(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">at</span>&nbsp;=&nbsp;DateTime.Today.AddDays(days).At(hours,&nbsp;minutes) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToIso8601DateTimeString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;Create.ReservationDto(at,&nbsp;email,&nbsp;name,&nbsp;quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">postResp</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.PostReservation(expected); &nbsp;&nbsp;&nbsp;&nbsp;Uri&nbsp;<span style="color:#1f377f;">address</span>&nbsp;=&nbsp;FindReservationAddress(postResp); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">getResp</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateClient().GetAsync(address); &nbsp;&nbsp;&nbsp;&nbsp;getResp.EnsureSuccessStatusCode(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;getResp.ParseJsonContent&lt;ReservationDto&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual,&nbsp;<span style="color:blue;">new</span>&nbsp;ReservationDtoComparer()); &nbsp;&nbsp;&nbsp;&nbsp;AssertUrlFormatIsIdiomatic(address); }</pre> </p> <p> This parametrised test uses <a href="https://xunit.net">xUnit.net</a> 2.4.1 to first post a new reservation to the system, and then following the link provided in the response's <code>Location</code> header to verify that this resource contains a representation compatible with the reservation that was posted. </p> <p> A corresponding plaintext HTTP session would start like this: </p> <p> <pre>POST /restaurants/90125/reservations?sig=aco7VV%2Bh5sA3RBtrN8zI8Y9kLKGC60Gm3SioZGosXVE%3D HTTP/1.1 Content-Type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-08&nbsp;20:30&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;snomob@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Snow&nbsp;Moe&nbsp;Beal&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 } HTTP/1.1 201 Created Content-Type: application/json; charset=utf-8 Location: example.com/restaurants/90125/reservations/bf4e84130dac451b9c94049da8ea8c17?sig=ZVM%2[...] { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;bf4e84130dac451b9c94049da8ea8c17&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-08T20:30:00.0000000&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;snomob@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Snow&nbsp;Moe&nbsp;Beal&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 }</pre> </p> <p> That's the first request and response. Clients can now examine the response's headers to find the <code>Location</code> header. That URL is the actual, external ID of the resource, not the <code>id</code> property in the JSON representation. </p> <p> The client can save that URL and request it whenever it needs the reservation: </p> <p> <pre>GET /restaurants/90125/reservations/bf4e84130dac451b9c94049da8ea8c17?sig=ZVM%2[...] HTTP/1.1 HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;bf4e84130dac451b9c94049da8ea8c17&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-08T20:30:00.0000000&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;snomob@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Snow&nbsp;Moe&nbsp;Beal&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 }</pre> </p> <p> The actual, intended use of the API doesn't rely on the <code>id</code> property, neither do the tests. </p> <p> Based on this consistent design principle, I had reason to hope that I'd be able to remove the <code>id</code> property. </p> <h3 id="f4fc880aa8434cc2ac1675fcdf68bc18"> Breaking change <a href="#f4fc880aa8434cc2ac1675fcdf68bc18" title="permalink">#</a> </h3> <p> My motivation for making this change was to educate myself. I wanted to see if it would be possible to design a REST API that doesn't expose IDs in their JSON (or XML) representations. Usually I'm having trouble doing this in practice because when I'm consulting, I'm typically present to help the organisation with test-driven development and how to organise their code. It's always hard to learn new ways of doing things, and I don't wish to overwhelm my clients with too many changes all at once. </p> <p> So I usually let them do <a href="https://martinfowler.com/articles/richardsonMaturityModel.html">level 2 APIs</a> because that's what they're comfortable with. With that style of HTTP API design, it's hard to avoid <code>id</code> fields. </p> <p> This wasn't a constraint for the book's code, so I'd gone full REST on that API, and I'm happy that I did. By habit, though, I'd exposed the <code>id</code> property in JSON, and I now wanted to perform an experiment: Could I remove the field? </p> <p> A word of warning: You can't just remove a JSON property from a production API. That would constitute a breaking change, and even though clients aren't supposed to use the <code>id</code>, <a href="https://www.hyrumslaw.com">Hyrum's law</a> says that someone somewhere probably already is. </p> <p> This is just an experiment that I carried out on a separate Git branch, for my own edification. </p> <h3 id="3cf78b876e2a4f91b3e49b91f89bde02"> Leaning on the compiler <a href="#3cf78b876e2a4f91b3e49b91f89bde02" title="permalink">#</a> </h3> <p> As outlined, I had relatively strong faith in my test suite, so I decided to modify the <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a> (DTO) in question. Before the change, it looked like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;LinkDto[]?&nbsp;Links&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;At&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> At first, I simply tried to delete the <code>Id</code> property, but while it turned out to be not too bad in general, it did break one feature: The ability of <a href="/2020/08/24/adding-rest-links-as-a-cross-cutting-concern">the LinksFilter</a> to generate links to reservations. Instead, I changed the <code>Id</code> property to be internal: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;LinkDto[]?&nbsp;Links&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;At&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> This enables the <code>LinksFilter</code> and other internal code to still access the <code>Id</code> property, while the unit tests no longer could. As expected, this change caused some compiler errors. That was expected, and my plan was to <em>lean on the compiler</em>, as <a href="https://michaelfeathers.silvrback.com">Michael Feathers</a> describes in <a href="http://bit.ly/working-effectively-with-legacy-code">Working Effectively with Legacy Code</a>. </p> <p> As I had hoped, relatively few things broke, and they were fixed in 5-10 minutes. Once everything compiled, I ran the tests. Only a single test failed, and this was a unit test that used some Back Door Manipulation, as <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> terms it. I'll return to that test in <a href="/2021/09/27/the-equivalence-contravariant-functor">a future article</a>. </p> <p> None of my self-hosted integration tests failed. </p> <h3 id="d65ef3b7816c4f73b1060af4532fe1dc"> ID-free interaction <a href="#d65ef3b7816c4f73b1060af4532fe1dc" title="permalink">#</a> </h3> <p> Since clients are supposed to follow links, they can still do so. For example, a <a href="https://en.wikipedia.org/wiki/Maître_d%27hôtel">maître d'hôtel</a> might request the day's schedule: </p> <p> <pre>GET /restaurants/90125/schedule/2021/12/8?sig=82fosBYsE9zSKkA4Biy5t%2BFMxl71XiLlFKaI2E[...] HTTP/1.1 Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXN0YXVyYW50IjpbIjEiLCIyMTEyIiwi[...] HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;The&nbsp;Vatican&nbsp;Cellar&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;year&quot;</span>:&nbsp;2021, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;month&quot;</span>:&nbsp;12, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;day&quot;</span>:&nbsp;8, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;days&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-08&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;entries&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;20:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;reservations&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:reservation&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://example.com/restaurants/90125/reservations/bf4e84130dac4[...]&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-08T20:30:00.0000000&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;snomob@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Snow&nbsp;Moe&nbsp;Beal&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> I've edited the response quite heavily by removing other links, and so on. </p> <p> Clients that wish to navigate to Snow Moe Beal's reservation must locate its <code>urn:reservation</code> link and use the corresponding <code>href</code> value. This is an opaque URL that clients can use to make requests: </p> <p> <pre>GET /restaurants/90125/reservations/bf4e84130dac451b9c94049da8ea8c17?sig=vxkBT1g1GHRmx[...] HTTP/1.1 HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-08T20:30:00.0000000&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;snomob@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Snow&nbsp;Moe&nbsp;Beal&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;1 }</pre> </p> <p> In none of these interactions do clients rely on the <code>id</code> property - which is also gone now. It's gone because the <code>Id</code> property on the C# DTO is <code>internal</code>, which means that it's not being rendered. </p> <p> Mission accomplished. </p> <h3 id="849eb1540d7145c78415976e8c662597"> Conclusion <a href="#849eb1540d7145c78415976e8c662597" title="permalink">#</a> </h3> <p> It always grates on me when I have to add an <code>id</code> property to a representation in an HTTP API. It's often necessary when working with a level 2 API, but with a proper hypermedia-driven REST API, it may not be necessary. </p> <p> At least, the experiment I performed with the code base from my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> indicates that this may be so. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="cd972bab11b74bf382c5796f5b0bec7a"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#cd972bab11b74bf382c5796f5b0bec7a">#</a></div> <div class="comment-content"> <p>It seems to me that this approach will cause problems if 3rd parties need to integrate with your API in a way where they themselves need to store references to entities in your system. For example, they may expose your entities to their users with additional data in their systems/integrations. Sure, it is <em>possible</em> for them to use the URI as a primary key (<em>if</em> you guarantee a sensible max URI length; another can of worms), but if you internally use INT or UNIQUEIDENTIFIER as your primary key, I would not want to force them to use VARCHAR(whatever) as primary key.</p> <p>Therefore, in all our APIs, we document in the API specification that the IDs, though required by <a target="_blank" href="https://jsonapi.org/">JSON:API</a> (which we follow) to be passed as string values for consistency, can be safely assumed to be integers (or GUIDs, if relevant). We even document that they are <em>32-bit</em> ints, so any clients know they can safely use INT fields instead of BIGINT.</p> <p>JSON:API requires all entities to have a single ID. For obvious reasons, IDs should be stable. Therefore, for entities that represent an association between two other entities and do not have a separate, persisted ID, we have a need to have API IDs that contain information about the associated entities. To combat Hyrum's law, we typically concatenate the associated IDs using a known delimiter and encode the resulting string using a non-standard, URL-friendly encoding (i.e., not Base64, which may contain non-URL-friendly characters and is often obvious). This way, the IDs appear opaque to API clients. Of course, the format of these IDs are not documented in our API specifications, as they are not intended to be stored. Instead, the actual association is documented and the related entities retrievable (of course, since this information inherent to the entity's very nature), and the associated IDs may be used by clients in a multi-column primary key, just like we do.</p> <p>All of the above assumes that the integrating clients use a SQL database or similar. Let's face it; many do. If you have (or may hve in the future) a single client that do this, you have to take the above into account.</p> </div> <div class="comment-date">2021-09-20 9:07 UTC</div> </div> <div class="comment" id="2fa9b78a83b64b33ae16182ed7db450c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2fa9b78a83b64b33ae16182ed7db450c">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. I think that one of the problems with discussions about REST APIs, or just HTTP APIs in general, is that people use them for all sorts of things. At one extreme, you have <a href="https://samnewman.io/patterns/architectural/bff/">Backends For Frontends</a>, where, if you aren't writing the API with the single client in mind, you're doing something wrong. At the other extreme, you have APIs that may have uncountable and unknown clients. When I write about REST, I mostly have the latter kind in mind. </p> <p> When designing APIs for many unknown clients, it makes little sense to take 'special needs' into account. Different clients may present mutually exclusive requirements. </p> <p> Clients that need to 'bookmark' REST resources in a database can do that by defining two columns: one an ordinary primary key column on which the table defines its clustered index, and another column for the link value itself, with a <code>UNIQUE</code> constraint. Something like this (in T-SQL dialect): </p> <p> <pre><span style="color:blue;">CREATE</span>&nbsp;<span style="color:blue;">TABLE</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Foo]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span> &nbsp;&nbsp;&nbsp;&nbsp;[Id]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">IDENTITY</span>&nbsp;<span style="color:blue;">PRIMARY</span>&nbsp;<span style="color:blue;">KEY</span>&nbsp;<span style="color:blue;">CLUSTERED</span><span style="color:gray;">,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Address]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">NVARCHAR&nbsp;</span><span style="color:gray;">(</span>850<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">UNIQUE</span>&nbsp;<span style="color:blue;">NONCLUSTERED</span><span style="color:gray;">,</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">--&nbsp;Other&nbsp;columns&nbsp;go&nbsp;here...</span> <span style="color:gray;">);</span></pre> </p> <p> Client code can look up an API resource on internal key, or on address, as required. </p> </div> <div class="comment-date">2021-09-21 16:27 UTC</div> </div> <div class="comment" id="6a7b06cf97e9453bafe5ddafa5156093"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#6a7b06cf97e9453bafe5ddafa5156093">#</a></div> <div class="comment-content"> <p>Your URLs include a signature, which changes if you need to switch signing keys. Furthermore, the base URL for your API may change. The entities are still the same; the restaurant previously at old.domain/restaurants/1?sig=abc is the same as the restaurant now at new.domain/restaurants/1?sig=123. With your proposed bookmark-based solution, the API clients would effectively lose the associations in their system.</p> <p>Also, indexing a very long varchar column probably works fine for tables that are fairly small and not overly busy. But for large and/or busy tables containing entities that are created every second of every day (say, passages through gates at hundreds of large construction sites, which is one of the domains I work with), I think that the performance would suffer unreasonably. (Admittedly, I have of course not measured this; this is just speculation, and anyway not my main point.)</p> <p>You say you write APIs with arbitrary clients in mind. I do, too. That is one of the reasons I design my APIs at REST level 2 instead of 3. (JSON:API does offer some possibility of just "following links" if the client wishes to do that, though it is does not allow for APIs that are fully level 3/HATEOAS.) Having stable IDs with well-known formats and being able to construct URLs seems pragmatically like a good solution that keeps client developers happy. I do not have decades of experience, but I have never encountered clients who have been unhappy with my decision to go for level 2 instead of level 3. (I imagine I would have encountered some resistance in the opposite case, though that is pure speculation on my part.) Furthermore, I have never encountered the need for breaking changes that would be non-breaking by level 3 standards.</p> <p>You say it makes little sense to take "special needs" into account. Idealistically, I agree. Pragmatically, 1) SQL databases are so ubiquitous and have been for such a long time that making life better for those developers by including an ID with a guaranteed format seems like a fair decision, and 2) our APIs (and many others, I assume) are created not just for 3rd party integration but also for one or more 1st party front-ends, which naturally tends to receive some preferential treatment (e.g. features and designs that probably aren't useful to other clients).</p> </div> <div class="comment-date">2021-09-21 20:56 UTC</div> </div> <div class="comment" id="e1a154854b014ab8ad218b076579db4c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e1a154854b014ab8ad218b076579db4c">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. It's possible that I'm going about this the wrong way. I only report on what's been working for me, but that said, while I <em>do</em> have decades of general programming experience, I don't have decades of REST experience. I designed my first REST API in 2012. </p> <p> Additionally, just because one style of API design works well, that doesn't rule out that other types of design also work. </p> <p> Finally, this particular article is an experiment. I've never done something like this in the wild, so it's possible that it does have unforeseen issues. </p> <p> A couple of answers to your various points, though: </p> <p> I don't foresee having to change signing keys, but if that happens, it'd be a breaking change to remove support for old keys. One might have to, instead, retire old signing keys in the same way one can <a href="/2020/06/01/retiring-old-service-versions">retire old service versions</a>. Even if a key gets 'compromised', it's not an immediate issue. It only means that any client that possesses the leaked key <em>can</em> construct URLs directly by <a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls">retro-engineering implied URL templates</a>. This would still be undocumented and unsupported use of the API, which means that ultimately, it'd be against the client developers' own self-interest in doing that. </p> <p> <a href="/2020/10/26/fit-urls">Signing the URLs isn't a security measure</a>; it's more like a <a href="https://en.wikipedia.org/wiki/Nudge_theory">nudge</a>. </p> <blockquote> <p> "our APIs (and many others, I assume) are created not just for 3rd party integration but also for one or more 1st party front-ends, which naturally tends to receive some preferential treatment" </p> </blockquote> <p> I've written APIs like that as well, and if there's one thing I've learned from doing that is that if I'm ever again put in charge of such an API, I'll strongly resist giving preferential treatment to any clients. If a particular client needs a particular feature, the client team can develop and maintain a Backend for Frontend, which bases its own implementation on the general-purpose API. </p> <p> My experience with supporting particular clients is that client needs evolve much faster than APIs. This makes sense. Someone wants to do <a href="https://en.wikipedia.org/wiki/A/B_testing">A/B testing</a> on the client's user interface. Depending on the outcome of such a test, at least one of the supporting features will now be obsolete. I'm not much inclined having to support such features in an API where backwards compatibility is critical. </p> <p> But again, these things are never clear-cut. Much depends on the overall goals of the organisation - and these may also change over time. I'm not claiming that my way is <em>best</em> - only that it's possible. </p> </div> <div class="comment-date">2021-09-23 8:07 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Unit testing private helper methods https://blog.ploeh.dk/2021/09/13/unit-testing-private-helper-methods 2021-09-13T05:25:00+00:00 Mark Seemann <div id="post"> <p> <em>Evolving a private helper method, guided by tests.</em> </p> <p> A frequently asked question about unit testing and test-driven development (TDD) is how to test private helper methods. I've <a href="/2015/09/22/unit-testing-internals">already attempted to answer that question: <em>through the public API</em></a>, but a recent comment to <a href="https://stackoverflow.com/a/69141655/126014">a Stack Overflow question</a> made me realise that I've failed to supply a code example. </p> <p> Show, don't tell. </p> <p> In this article I'll show a code example that outlines how a private helper method can evolve under TDD. </p> <h3 id="03e1e533e1514945812c557b006883c3"> Threshold <a href="#03e1e533e1514945812c557b006883c3" title="permalink">#</a> </h3> <p> The code example in this article comes from my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. When you buy the book, you get not only the finished code examples, but the entire Git repository, with detailed commit messages. </p> <p> A central part of the code base is a method that decides whether or not to accept a reservation attempt. It's essentially a solution to <a href="/2020/01/27/the-maitre-d-kata/">the Maître d' kata</a>. I wrote most of the book's code with TDD, and after commit <code>fa12fd69c158168178f3a75bcd900e5caa7e7dec</code> I decided that I ought to refactor the implementation. As I wrote in the commit message: </p> <p> <pre>Filter later reservations based on date The line count of the willAccept method has now risen to 28. Cyclomatic complexity is still at 7. It's ripe for refactoring.</pre> </p> <p> I think, by the way, that I made a small mistake. As far as I can tell, the <code>WillAccept</code> line count in this commit is <em>26</em> - not <em>28:</em> </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">WillAccept</span>( &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="color:#1f377f;">existingReservations</span>, &nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;<span style="color:#1f377f;">candidate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(existingReservations&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(existingReservations)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(candidate&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">relevantReservations</span>&nbsp;=&nbsp;existingReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;candidate.At.Date&nbsp;==&nbsp;r.At.Date); &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Table&gt;&nbsp;<span style="color:#1f377f;">availableTables</span>&nbsp;=&nbsp;Tables.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="color:#1f377f;">r</span>&nbsp;<span style="color:#8f08c4;">in</span>&nbsp;relevantReservations) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">table</span>&nbsp;=&nbsp;availableTables.Find(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;r.Quantity&nbsp;&lt;=&nbsp;t.Seats); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(table&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;availableTables.Remove(table); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(table.IsCommunal) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;availableTables.Add(table.Reserve(r.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;availableTables.Any(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;candidate.Quantity&nbsp;&lt;=&nbsp;t.Seats); }</pre> </p> <p> Still, I knew that it wasn't done - that I'd be adding more tests that would increase both the size and complexity of the method. It was brushing against more than one <a href="/2020/04/13/curb-code-rot-with-thresholds">threshold</a>. I decided that it was time for a prophylactic refactoring. </p> <p> Notice that the <a href="/2019/10/21/a-red-green-refactor-checklist">red-green-refactor checklist</a> explicitly states that refactoring is part of the process. It doesn't, however, mandate that refactoring must be done in the same commit as the green phase. Here, I did <em>red-green-commit-refactor-commit</em>. </p> <p> While I decided to refactor, I also knew that I still had some way to go before <code>WillAccept</code> would be complete. With the code still in flux, I didn't want to couple tests to a new method, so I chose to extract a <code>private</code> helper method. </p> <h3 id="8db39405ecce4ee9b647866eed0e1383"> Helper method <a href="#8db39405ecce4ee9b647866eed0e1383" title="permalink">#</a> </h3> <p> After the refactoring, the code looked like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">WillAccept</span>( &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="color:#1f377f;">existingReservations</span>, &nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;<span style="color:#1f377f;">candidate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(existingReservations&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(existingReservations)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(candidate&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">relevantReservations</span>&nbsp;=&nbsp;existingReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;candidate.At.Date&nbsp;==&nbsp;r.At.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">availableTables</span>&nbsp;=&nbsp;Allocate(relevantReservations); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;availableTables.Any(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;candidate.Quantity&nbsp;&lt;=&nbsp;t.Seats); } <span style="color:blue;">private</span>&nbsp;IEnumerable&lt;Table&gt;&nbsp;<span style="color:#74531f;">Allocate</span>( &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Table&gt;&nbsp;<span style="color:#1f377f;">availableTables</span>&nbsp;=&nbsp;Tables.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">foreach</span>&nbsp;(var&nbsp;<span style="color:#1f377f;">r</span>&nbsp;<span style="color:#8f08c4;">in</span>&nbsp;reservations) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">table</span>&nbsp;=&nbsp;availableTables.Find(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;r.Quantity&nbsp;&lt;=&nbsp;t.Seats); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(table&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;availableTables.Remove(table); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(table.IsCommunal) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;availableTables.Add(table.Reserve(r.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;availableTables; }</pre> </p> <p> I committed the change, and wrote in the commit message: </p> <p> <pre>Extract helper method from WillAccept This quite improves the complexity of the method, which is now 4, and at 18 lines of code. The new helper method also has a cyclomatic complexity of 4, and 17 lines of code. A remaining issue with the WillAccept method is that the code operates on different levels of abstraction. The call to Allocate represents an abstraction, while the filter on date is as low-level as it can get.</pre> </p> <p> As you can tell, I was well aware that there were remaining issues with the code. </p> <p> Since the new <code>Allocate</code> helper method is <code>private</code>, unit tests can't reach it directly. It's still <em>covered</em> by tests, though, just as that code block was before I extracted it. </p> <h3 id="b8928ae201444a78b7ca1715e486fb92"> More tests <a href="#b8928ae201444a78b7ca1715e486fb92" title="permalink">#</a> </h3> <p> I wasn't done with the <code>WillAccept</code> method, and after a bout of other refactorings, I added more test cases covering it. </p> <p> While the method ultimately grew to exhibit moderately complex behaviour, I had only two test methods covering it: one (not shown) for the rejection case, and another for the accept (<code>true</code>) case: </p> <p> <pre>[Theory,&nbsp;ClassData(<span style="color:blue;">typeof</span>(AcceptTestCases))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Accept</span>( &nbsp;&nbsp;&nbsp;&nbsp;TimeSpan&nbsp;<span style="color:#1f377f;">seatingDuration</span>, &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Table&gt;&nbsp;<span style="color:#1f377f;">tables</span>, &nbsp;&nbsp;&nbsp;&nbsp;IEnumerable&lt;Reservation&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;MaitreD(seatingDuration,&nbsp;tables); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&nbsp;Some.Reservation.WithQuantity(11); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.WillAccept(reservations,&nbsp;r); &nbsp;&nbsp;&nbsp;&nbsp;Assert.True(actual); }</pre> </p> <p> I based the example code on the <a href="/2020/03/02/impureim-sandwich">impureim sandwich architecture</a>, which meant that domain logic (including the <code>WillAccept</code> method) is all <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. The nice thing about pure functions is that <a href="/2015/05/07/functional-design-is-intrinsically-testable">they're easy to unit test</a>. </p> <p> The <code>Accept</code> test method uses an object data source (see the article <a href="/2021/01/18/parametrised-test-primitive-obsession-code-smell">Parametrised test primitive obsession code smell</a> for another example of the motivation behind using objects for test parametrisation), so adding more test cases were simply a matter of adding them to the data source: </p> <p> <pre><span style="color:silver;">Add(TimeSpan.FromHours(6), &nbsp;&nbsp;&nbsp;&nbsp;new[]&nbsp;{&nbsp;Table.Communal(11)&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;new[]&nbsp;{&nbsp;Some.Reservation.WithQuantity(11).TheDayAfter()&nbsp;});</span> Add(TimeSpan.FromHours(2.5), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;Table.Standard(12)&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;Some.Reservation.WithQuantity(11).AddDate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromHours(-2.5))&nbsp;}); Add(TimeSpan.FromHours(1), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;Table.Standard(14)&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;Some.Reservation.WithQuantity(9).AddDate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromHours(1))&nbsp;});</pre> </p> <p> The bottom two test cases are new additions. In that way, by adding new test cases, I could keep evolving <code>WillAccept</code> and its various private helper methods (of which I added more). While no tests directly exercise the private helper methods, the unit tests still <a href="https://en.wikipedia.org/wiki/Transitive_relation">transitively</a> exercise the private parts of the code base. </p> <p> Since I followed TDD, no private helper methods sprang into existence untested. I didn't have to jump through hoops in order to be able to unit test private helper methods. Rather, the private helper methods were a natural by-product of the red-green-refactor process - particularly, the <em>refactor</em> phase. </p> <h3 id="6c5f4e12a3cb4318a3331cf4f2485028"> Conclusion <a href="#6c5f4e12a3cb4318a3331cf4f2485028" title="permalink">#</a> </h3> <p> Following TDD doesn't preclude the creation of private helper methods. In fact, private helper methods can (and should?) emerge during the refactoring phase of the red-green-refactoring cycle. </p> <p> For long-time practitioners of TDD, there's nothing new in this, but people new to TDD are still learning. This question keeps coming up, so I hope that this example is useful. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Specification contravariant functor https://blog.ploeh.dk/2021/09/09/the-specification-contravariant-functor 2021-09-09T09:12:00+00:00 Mark Seemann <div id="post"> <p> <em>An introduction for object-oriented programmers to the Specification contravariant functor.</em> </p> <p> This article is an instalment in <a href="/2021/09/02/contravariant-functors">an article series about contravariant functors</a>. It assumes that you've read the introduction. In the <a href="/2021/09/06/the-command-handler-contravariant-functor">previous article</a>, you saw an example of a contravariant functor based on the Command Handler pattern. This article gives another example. </p> <p> <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> discusses the benefits of the <a href="https://en.wikipedia.org/wiki/Specification_pattern">Specification pattern</a>. In its generic incarnation this pattern gives rise to a contravariant functor. </p> <h3 id="2d6538ee35d84c47808f6b162f61eeb3"> Interface <a href="#2d6538ee35d84c47808f6b162f61eeb3" title="permalink">#</a> </h3> <p> <a href="http://amzn.to/WBCwx7">DDD</a> introduces the pattern with a non-generic <code>InvoiceSpecification</code> interface. The book also shows other examples, and it quickly becomes clear that with generics, you can generalise the pattern to this interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ISpecification</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">IsSatisfiedBy</span>(T&nbsp;<span style="color:#1f377f;">candidate</span>); }</pre> </p> <p> Given such an interface, you can implement standard reusable Boolean logic such as <em>and</em>, <em>or</em>, and <em>not</em>. (Exercise: consider how implementations of <em>and</em> and <em>or</em> correspond to well-known <a href="/2017/10/06/monoids">monoids</a>. Do the implementations look like <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composites</a>? <a href="/2018/03/12/composite-as-a-monoid">Is that a coincidence?</a>) </p> <p> The <code>ISpecification&lt;T&gt;</code> interface is really just a glorified predicate. These days the Specification pattern may seem somewhat exotic in languages with first-class functions. C#, for example, defines both a <a href="https://docs.microsoft.com/dotnet/api/system.predicate-1">specialised Predicate delegate</a>, as well as the more general <code>Func&lt;T, bool&gt;</code> delegate. Since you can pass those around as objects, that's often good enough, and you don't need an <code>ISpecification</code> interface. </p> <p> Still, for the sake of argument, in this article I'll start with the Specification pattern and demonstrate how that gives rise to a contravariant functor. </p> <h3 id="a13ce5becb564762847a6876f87aa16f"> Natural number specification <a href="#a13ce5becb564762847a6876f87aa16f" title="permalink">#</a> </h3> <p> Consider the <code>AdjustInventoryService</code> class from <a href="/2021/09/06/the-command-handler-contravariant-functor">the previous article</a>. I'll repeat the 'original' <code>Execute</code> method here: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(AdjustInventory&nbsp;<span style="color:#1f377f;">command</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">productInventory</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.repository.GetByIdOrNull(command.ProductId) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantityAdjustment</span>&nbsp;=&nbsp;command.Quantity&nbsp;*&nbsp;(command.Decrease&nbsp;?&nbsp;-1&nbsp;:&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;productInventory&nbsp;=&nbsp;productInventory.AdjustQuantity(quantityAdjustment); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(productInventory.Quantity&nbsp;&lt;&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;InvalidOperationException(<span style="color:#a31515;">&quot;Can&#39;t&nbsp;decrease&nbsp;below&nbsp;0.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository.Save(productInventory); }</pre> </p> <p> Notice the Guard Clause: </p> <p> <pre><span style="color:#8f08c4;">if</span>&nbsp;(productInventory.Quantity&nbsp;&lt;&nbsp;0)</pre> </p> <p> Image that we'd like to introduce some flexibility here. It's admittedly a silly example, but just come along for the edification. Imagine that we'd like to use an injected <code>ISpecification&lt;ProductInventory&gt;</code> instead: </p> <p> <pre><span style="color:#8f08c4;">if</span>&nbsp;(!specification.IsSatisfiedBy(productInventory))</pre> </p> <p> That doesn't sound too difficult, but what if you only have an <code>ISpecification</code> implementation like the following? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span>&nbsp;:&nbsp;ISpecification&lt;<span style="color:blue;">int</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;ISpecification&lt;<span style="color:blue;">int</span>&gt;&nbsp;Specification&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;NaturalNumber(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">IsSatisfiedBy</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;0&nbsp;&lt;=&nbsp;candidate; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> That's essentially what you need, but alas, it only implements <code>ISpecification&lt;int&gt;</code>, not <code>ISpecification&lt;ProductInventory&gt;</code>. Do you <em>really</em> have to write a new <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> just to implement the right interface? </p> <p> No, you don't. </p> <h3 id="069f9ff7ee4d41dab4cf9243804dc873"> Contravariant functor <a href="#069f9ff7ee4d41dab4cf9243804dc873" title="permalink">#</a> </h3> <p> Fortunately, an interface like <code>ISpecification&lt;T&gt;</code> gives rise to a contravariant functor. This will enable you to compose an <code>ISpecification&lt;ProductInventory&gt;</code> object from the <code>NaturalNumber</code> specification. </p> <p> In order to enable contravariant mapping, you must add a <code>ContraMap</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;ISpecification&lt;T1&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;ISpecification&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ContraSpecification&lt;T,&nbsp;T1&gt;(source,&nbsp;selector); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ContraSpecification</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;:&nbsp;ISpecification&lt;T1&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;ISpecification&lt;T&gt;&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ContraSpecification</span>(ISpecification&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>,&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.source&nbsp;=&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.selector&nbsp;=&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">IsSatisfiedBy</span>(T1&nbsp;<span style="color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;source.IsSatisfiedBy(selector(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that, as explained in the overview article, in order to map from an <code>ISpecification&lt;T&gt;</code> to an <code>ISpecification&lt;T1&gt;</code>, the <code>selector</code> has to go the other way: from <code>T1</code> to <code>T</code>. How this is possible will become more apparent with an example, which will follow later in the article. </p> <h3 id="f3211a80973c4d85b63a564704e67028"> Identity law <a href="#f3211a80973c4d85b63a564704e67028" title="permalink">#</a> </h3> <p> A <code>ContraMap</code> method with the right signature isn't enough to be a contravariant functor. It must also obey the contravariant functor laws. As usual, it's proper computer-science work to actually prove this, but you can write some tests to demonstrate the identity law for the <code>ISpecification&lt;T&gt;</code> interface. In this article, you'll see parametrised tests written with <a href="https://xunit.net">xUnit.net</a>. First, the identity law: </p> <p> <pre>[Theory] [InlineData(-102)] [InlineData(&nbsp;&nbsp;-3)] [InlineData(&nbsp;&nbsp;-1)] [InlineData(&nbsp;&nbsp;&nbsp;0)] [InlineData(&nbsp;&nbsp;&nbsp;1)] [InlineData(&nbsp;&nbsp;32)] [InlineData(&nbsp;283)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IdentityLaw</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">input</span>) { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">id</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;ISpecification&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NaturalNumber.Specification.ContraMap&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;(id); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NaturalNumber.Specification.IsSatisfiedBy(input), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;projection.IsSatisfiedBy(input)); }</pre> </p> <p> In order to observe that the two Specifications have identical behaviours, the test has to invoke <code>IsSatisfiedBy</code> on both of them to verify that the return values are the same. </p> <p> All test cases pass. </p> <h3 id="84d4761adecd427fbd9e463a05e8cc67"> Composition law <a href="#84d4761adecd427fbd9e463a05e8cc67" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that <code>ContraMap</code> obeys the composition law for contravariant functors: </p> <p> <pre>[Theory] [InlineData(&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;0:05&quot;</span>)] [InlineData(&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;1:20&quot;</span>)] [InlineData(&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;0:12:10&quot;</span>)] [InlineData(&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;1:00:12&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;1.13:14:34&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CompositionLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">input</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;TimeSpan.Parse; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;TimeSpan,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;(<span style="color:blue;">int</span>)ts.TotalMinutes; &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NaturalNumber.Specification.ContraMap((<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>)&nbsp;=&gt;&nbsp;g(f(s))).IsSatisfiedBy(input), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NaturalNumber.Specification.ContraMap(g).ContraMap(f).IsSatisfiedBy(input)); }</pre> </p> <p> This test defines two local functions, <code>f</code> and <code>g</code>. Once more, you can't directly compare methods for equality, so instead you have to call <code>IsSatisfiedBy</code> on both compositions to verify that they return the same Boolean value. </p> <p> They do. </p> <h3 id="94ecc00ea3064f86b48c45d0e9627cc8"> Product inventory specification <a href="#94ecc00ea3064f86b48c45d0e9627cc8" title="permalink">#</a> </h3> <p> You can now produce the desired <code>ISpecification&lt;ProductInventory&gt;</code> from the <code>NaturalNumber</code> Specification without having to add a new class: </p> <p> <pre>ISpecification&lt;ProductInventory&gt;&nbsp;<span style="color:#1f377f;">specification</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;NaturalNumber.Specification.ContraMap((ProductInventory&nbsp;<span style="color:#1f377f;">inv</span>)&nbsp;=&gt;&nbsp;inv.Quantity);</pre> </p> <p> Granted, it is, once more, a silly example, but the purpose of this article isn't to convince you that this is better (it probably isn't). The purpose of the article is to show an example of a contravariant functor, and how it can be used. </p> <h3 id="376573ff36c64561b5fc231589f6922f"> Predicates <a href="#376573ff36c64561b5fc231589f6922f" title="permalink">#</a> </h3> <p> For good measure, any predicate forms a contravariant functor. You don't need the <code>ISpecification</code> interface. Here are <code>ContraMap</code> overloads for <code>Predicate&lt;T&gt;</code> and <code>Func&lt;T, bool&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Predicate&lt;T1&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;(<span style="color:blue;">this</span>&nbsp;Predicate&lt;T&gt;&nbsp;<span style="color:#1f377f;">predicate</span>,&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;predicate(selector(x)); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;T1,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;(<span style="color:blue;">this</span>&nbsp;Func&lt;T,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">predicate</span>,&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;predicate(selector(x)); }</pre> </p> <p> Notice that the lambda expressions are identical in both implementations. </p> <h3 id="4c2e7471713547d5a24a20dacce49806"> Conclusion <a href="#4c2e7471713547d5a24a20dacce49806" title="permalink">#</a> </h3> <p> Like Command Handlers and Event Handlers, generic predicates give rise to a contravariant functor. This includes both the Specification pattern, <code>Predicate&lt;T&gt;</code>, and <code>Func&lt;T, bool&gt;</code>. </p> <p> Are you noticing a pattern? </p> <p> <strong>Next:</strong> <a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Command Handler contravariant functor https://blog.ploeh.dk/2021/09/06/the-command-handler-contravariant-functor 2021-09-06T05:46:00+00:00 Mark Seemann <div id="post"> <p> <em>An introduction to the Command Handler contravariant functor for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2021/09/02/contravariant-functors">an article series about contravariant functors</a>. It assumes that you've read the introduction. </p> <p> Asynchronous software architectures, such as those described in <a href="http://bit.ly/eipatterns">Enterprise Integration Patterns</a>, often make good use of a pattern where <em>Commands</em> are (preferably immutable) <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects</a> (DTOs) that are often placed on a persistent queue and later handled by a background process. </p> <p> Even if you don't use asynchronous processing, separating command data from command handling can be beneficial for your software's granular architecture. In perhaps his most remarkable contribution to <a href="/dippp">our book</a>, <a href="https://blogs.cuttingedge.it/steven">Steven van Deursen</a> describes how this pattern can greatly simplify how you deal with cross-cutting concerns. </p> <h3 id="3d338bea6fa74d149b239067a283967c"> Interface <a href="#3d338bea6fa74d149b239067a283967c" title="permalink">#</a> </h3> <p> In <a href="/dippp">DIPPP</a> the interface is called <code>ICommandService</code>, but in this article I'll instead call it <code>ICommandHandler</code>. It's a generic interface with a single method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ICommandHandler</span>&lt;<span style="color:#2b91af;">TCommand</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(TCommand&nbsp;<span style="color:#1f377f;">command</span>); }</pre> </p> <p> The book explains how this interface enables you to gracefully handle cross-cutting concerns without any reflection magic. You can also peruse <a href="https://github.com/DependencyInjection-2nd-edition/codesamples">its example code base on GitHub</a>. In this article, however, I'm using <a href="https://github.com/ploeh/codesamples">a fork of that code</a> because I wanted to make the properties of contravariant functors stand out more clearly. </p> <p> In the sample code base, an ASP.NET Controller delegates work to an injected <code>ICommandHandler&lt;AdjustInventory&gt;</code> called <code>inventoryAdjuster</code>. </p> <p> <pre>[Route(<span style="color:#a31515;">&quot;inventory/adjustinventory&quot;</span>)] <span style="color:blue;">public</span>&nbsp;ActionResult&nbsp;<span style="color:#74531f;">AdjustInventory</span>(AdjustInventoryViewModel&nbsp;<span style="color:#1f377f;">viewModel</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!<span style="color:blue;">this</span>.ModelState.IsValid) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">this</span>.View(nameof(Index),&nbsp;<span style="color:blue;">this</span>.Populate(viewModel)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;AdjustInventory&nbsp;<span style="color:#1f377f;">command</span>&nbsp;=&nbsp;viewModel.Command; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.inventoryAdjuster.Execute(command); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.TempData[<span style="color:#a31515;">&quot;SuccessMessage&quot;</span>]&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Inventory&nbsp;successfully&nbsp;adjusted.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">this</span>.RedirectToAction(nameof(HomeController.Index),&nbsp;<span style="color:#a31515;">&quot;Home&quot;</span>); }</pre> </p> <p> There's a single implementation of <code>ICommandHandler&lt;AdjustInventory&gt;</code>, which is a class called <code>AdjustInventoryService</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AdjustInventoryService</span>&nbsp;:&nbsp;ICommandHandler&lt;AdjustInventory&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IInventoryRepository&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AdjustInventoryService</span>(IInventoryRepository&nbsp;<span style="color:#1f377f;">repository</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(repository&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(repository)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(AdjustInventory&nbsp;<span style="color:#1f377f;">command</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">productInventory</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.repository.GetByIdOrNull(command.ProductId) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantityAdjustment</span>&nbsp;=&nbsp;command.Quantity&nbsp;*&nbsp;(command.Decrease&nbsp;?&nbsp;-1&nbsp;:&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;productInventory&nbsp;=&nbsp;productInventory.AdjustQuantity(quantityAdjustment); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(productInventory.Quantity&nbsp;&lt;&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;InvalidOperationException(<span style="color:#a31515;">&quot;Can&#39;t&nbsp;decrease&nbsp;below&nbsp;0.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository.Save(productInventory); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Execute</code> method first loads the inventory data from the database, calculates how to adjust it, and saves it. This is all fine and good object-oriented design, and my intent with the present article isn't to point fingers at it. My intent is only to demonstrate how the <code>ICommandHandler</code> interface gives rise to a contravariant functor. </p> <p> I'm using this particular code base because it provides a good setting for a realistic example. </p> <h3 id="24bc8cde80ca4234b4eea11c7617a784"> Towards Domain-Driven Design <a href="#24bc8cde80ca4234b4eea11c7617a784" title="permalink">#</a> </h3> <p> Consider these two lines of code from <code>AdjustInventoryService</code>: </p> <p> <pre><span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantityAdjustment</span>&nbsp;=&nbsp;command.Quantity&nbsp;*&nbsp;(command.Decrease&nbsp;?&nbsp;-1&nbsp;:&nbsp;1); productInventory&nbsp;=&nbsp;productInventory.AdjustQuantity(quantityAdjustment);</pre> </p> <p> Doesn't that look like a case of <a href="https://wiki.c2.com/?FeatureEnvySmell">Feature Envy</a>? Doesn't this calculation belong better on another class? Which one? The <code>AdjustInventory</code> Command? That's one option, but in this style of architecture Commands are supposed to be dumb DTOs, so that may not be the best fit. <code>ProductInventory</code>? That may be more promising. </p> <p> Before making that change, however, let's consider the current state of the class. </p> <p> One of the changes I made in my fork of the code was to turn the <code>ProductInventory</code> class into an immutable <a href="https://martinfowler.com/bliki/ValueObject.html">Value Object</a>, as recommended in <a href="http://amzn.to/WBCwx7">DDD</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ProductInventory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ProductInventory</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(id,&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ProductInventory</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Guid&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ProductInventory&nbsp;<span style="color:#74531f;">WithQuantity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">newQuantity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(Id,&nbsp;newQuantity); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ProductInventory&nbsp;<span style="color:#74531f;">AdjustQuantity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">adjustment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;WithQuantity(Quantity&nbsp;+&nbsp;adjustment); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;ProductInventory&nbsp;<span style="color:#1f377f;">inventory</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id.Equals(inventory.Id)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;==&nbsp;inventory.Quantity; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;HashCode.Combine(Id,&nbsp;Quantity); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> That looks like a lot of code, but keep in mind that <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">typing isn't the bottleneck</a> - and besides, most of that code was written by various Visual Studio <em>Quick Actions</em>. </p> <p> Let's try to add a <code>Handle</code> method to <code>ProductInventory</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;ProductInventory&nbsp;<span style="color:#74531f;">Handle</span>(AdjustInventory&nbsp;<span style="color:#1f377f;">command</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">adjustment</span>&nbsp;=&nbsp;command.Quantity&nbsp;*&nbsp;(command.Decrease&nbsp;?&nbsp;-1&nbsp;:&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;AdjustQuantity(adjustment); }</pre> </p> <p> While <code>AdjustInventoryService</code> isn't too difficult to unit test, it still does require setting up and configuring some <a href="https://martinfowler.com/bliki/TestDouble.html">Test Doubles</a>. The new method, on the other hand, is actually a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, which means that <a href="/2015/05/07/functional-design-is-intrinsically-testable">it's trivial to unit test</a>: </p> <p> <pre>[Theory] [InlineData(0,&nbsp;<span style="color:blue;">false</span>,&nbsp;0,&nbsp;0)] [InlineData(0,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;0,&nbsp;0)] [InlineData(0,&nbsp;<span style="color:blue;">false</span>,&nbsp;1,&nbsp;1)] [InlineData(0,&nbsp;<span style="color:blue;">false</span>,&nbsp;2,&nbsp;2)] [InlineData(1,&nbsp;<span style="color:blue;">false</span>,&nbsp;1,&nbsp;2)] [InlineData(2,&nbsp;<span style="color:blue;">false</span>,&nbsp;3,&nbsp;5)] [InlineData(5,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;2,&nbsp;3)] [InlineData(5,&nbsp;&nbsp;<span style="color:blue;">true</span>,&nbsp;5,&nbsp;0)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Handle</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">initial</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#1f377f;">decrease</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">adjustment</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(Guid.NewGuid(),&nbsp;initial); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">command</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;AdjustInventory &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ProductId&nbsp;=&nbsp;sut.Id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Decrease&nbsp;=&nbsp;decrease, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;adjustment &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.Handle(command); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(sut.WithQuantity(expected),&nbsp;actual); }</pre> </p> <p> Now that the new function is available on <code>ProductInventory</code>, you can use it in <code>AdjustInventoryService</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(AdjustInventory&nbsp;<span style="color:#1f377f;">command</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">productInventory</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.repository.GetByIdOrNull(command.ProductId) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId); &nbsp;&nbsp;&nbsp;&nbsp;productInventory&nbsp;=&nbsp;productInventory.Handle(command); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(productInventory.Quantity&nbsp;&lt;&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;InvalidOperationException(<span style="color:#a31515;">&quot;Can&#39;t&nbsp;decrease&nbsp;below&nbsp;0.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository.Save(productInventory); }</pre> </p> <p> The <code>Execute</code> method now delegates its central logic to <code>ProductInventory.Handle</code>. </p> <h3 id="ee1b47388e1340a9ac8ced8eb3d7a181"> Encapsulation <a href="#ee1b47388e1340a9ac8ced8eb3d7a181" title="permalink">#</a> </h3> <p> If you consider the <code>Execute</code> method in its current incarnation, you may wonder why it checks whether the <code>Quantity</code> is negative. Shouldn't that be the responsibility of <code>ProductInventory</code>? Why do we even allow <code>ProductInventory</code> to enter an invalid state? </p> <p> This breaks encapsulation. Encapsulation is one of the most misunderstood concepts in programming, but as <a href="/encapsulation-and-solid">I explain in my PluralSight course</a>, as a minimum requirement, an object should not allow itself to be put into an invalid state. </p> <p> How to better encapsulate <code>ProductInventory</code>? Add a Guard Clause to the constructor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ProductInventory</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(quantity&nbsp;&lt;&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentOutOfRangeException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameof(quantity), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Negative&nbsp;quantity&nbsp;not&nbsp;allowed.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity; }</pre> </p> <p> Again, such behaviour is trivial to drive with a unit test: </p> <p> <pre>[Theory] [InlineData(&nbsp;-1)] [InlineData(&nbsp;-2)] [InlineData(-19)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">SetNegativeQuantity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">negative</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">id</span>&nbsp;=&nbsp;Guid.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;<span style="color:#1f377f;">action</span>&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(id,&nbsp;negative); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Throws&lt;ArgumentOutOfRangeException&gt;(action); }</pre> </p> <p> With those changes in place, <code>AdjustInventoryService</code> becomes even simpler: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(AdjustInventory&nbsp;<span style="color:#1f377f;">command</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">productInventory</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.repository.GetByIdOrNull(command.ProductId) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId); &nbsp;&nbsp;&nbsp;&nbsp;productInventory&nbsp;=&nbsp;productInventory.Handle(command); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository.Save(productInventory); }</pre> </p> <p> Perhaps even so simple that the class begins to seem unwarranted. </p> <h3 id="6f41a860c47741d7b2eb3b4b720d1d7f"> Sandwich <a href="#6f41a860c47741d7b2eb3b4b720d1d7f" title="permalink">#</a> </h3> <p> It's just a database Query, a single pure function call, and another database Command. In fact, it looks a lot like an <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(AdjustInventory&nbsp;<span style="color:#1f377f;">command</span>) { <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">productInventory</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.repository.GetByIdOrNull(command.ProductId)</span> <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId); &nbsp;&nbsp;&nbsp;&nbsp;productInventory&nbsp;=&nbsp;productInventory.Handle(command);</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository.Save(productInventory);</span> }</pre> </p> <p> In fact, it'd probably be more appropriate to move the null-handling closer to the other <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a> code: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(AdjustInventory&nbsp;<span style="color:#1f377f;">command</span>) { <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">productInventory</span>&nbsp;=&nbsp;<span style="color:blue;">this</span>.repository.GetByIdOrNull(command.ProductId);</span> <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;productInventory&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(productInventory&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId)).Handle(command);</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository.Save(productInventory);</span> }</pre> </p> <p> Why do we need the <code>AdjustInventoryService</code> class, again? </p> <p> Can't we move those three lines of code to the Controller? We could, but that might make testing the above <code>AdjustInventory</code> Controller action more difficult. After all, at the moment, the Controller has an injected <code>ICommandHandler&lt;AdjustInventory&gt;</code>, which is easy to replace with a Test Double. </p> <p> If only we could somehow <em>compose</em> an <code>ICommandHandler&lt;AdjustInventory&gt;</code> from the above sandwich <em>without having to define a class</em>... </p> <h3 id="535cefbfe8564f888f92fa4edd1fe7c8"> Contravariant functor <a href="#535cefbfe8564f888f92fa4edd1fe7c8" title="permalink">#</a> </h3> <p> Fortunately, an interface like <code>ICommandHandler&lt;T&gt;</code> gives rise to a contravariant functor. This will enable you to compose an <code>ICommandHandler&lt;AdjustInventory&gt;</code> object from the above constituent parts. </p> <p> In order to enable contravariant mapping, you must add a <code>ContraMap</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;ICommandHandler&lt;T1&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;ICommandHandler&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Action&lt;T1&gt;&nbsp;<span style="color:#1f377f;">action</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;source.Execute(selector(x)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;DelegatingCommandHandler&lt;T1&gt;(action); }</pre> </p> <p> Notice that, as explained in the overview article, in order to map from an <code>ICommandHandler&lt;T&gt;</code> to an <code>ICommandHandler&lt;T1&gt;</code>, the <code>selector</code> has to go the other way: from <code>T1</code> to <code>T</code>. How this is possible will become more apparent with an example, which will follow later in the article. </p> <p> The <code>ContraMap</code> method uses a <code>DelegatingCommandHandler</code> that wraps any <code>Action&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DelegatingCommandHandler</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;ICommandHandler&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Action&lt;T&gt;&nbsp;action; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DelegatingCommandHandler</span>(Action&lt;T&gt;&nbsp;<span style="color:#1f377f;">action</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.action&nbsp;=&nbsp;action; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Execute</span>(T&nbsp;<span style="color:#1f377f;">command</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action(command); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If you're now wondering whether <code>Action&lt;T&gt;</code> itself gives rise to a contravariant functor, then yes it does. </p> <h3 id="cab26cece052462890c8e04363a907ec"> Identity law <a href="#cab26cece052462890c8e04363a907ec" title="permalink">#</a> </h3> <p> A <code>ContraMap</code> method with the right signature isn't enough to be a contravariant functor. It must also obey the contravariant functor laws. As usual, it's proper computer-science work to actually prove this, but you can write some tests to demonstrate the identity law for the <code>ICommandHandler&lt;T&gt;</code> interface. In this article, you'll see parametrised tests written with <a href="https://xunit.net">xUnit.net</a>. First, the identity law: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;baz&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;qux&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;quux&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;quuz&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;corge&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;grault&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;garply&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">IdentityLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">input</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">observations</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;List&lt;<span style="color:blue;">string</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;ICommandHandler&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;DelegatingCommandHandler&lt;<span style="color:blue;">string</span>&gt;(observations.Add); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">id</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;ICommandHandler&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;sut.ContraMap&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Run&nbsp;both&nbsp;handlers</span> &nbsp;&nbsp;&nbsp;&nbsp;sut.Execute(input); &nbsp;&nbsp;&nbsp;&nbsp;projection.Execute(input); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(2,&nbsp;observations.Count); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Single(observations.Distinct()); }</pre> </p> <p> In order to observe that the two handlers have identical behaviours, the test has to <code>Execute</code> both of them to verify that both observations are the same. </p> <p> All test cases pass. </p> <h3 id="b4000e7978f949d8817590b0779d0afe"> Composition law <a href="#b4000e7978f949d8817590b0779d0afe" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that <code>ContraMap</code> obeys the composition law for contravariant functors: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;baz&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;qux&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;quux&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;quuz&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;corge&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;grault&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;garply&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CompositionLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">input</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">observations</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;List&lt;TimeSpan&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;ICommandHandler&lt;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;DelegatingCommandHandler&lt;TimeSpan&gt;(observations.Add); &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;TimeSpan.FromDays(i); &nbsp;&nbsp;&nbsp;&nbsp;ICommandHandler&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">projection1</span>&nbsp;=&nbsp;sut.ContraMap((<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>)&nbsp;=&gt;&nbsp;g(f(s))); &nbsp;&nbsp;&nbsp;&nbsp;ICommandHandler&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">projection2</span>&nbsp;=&nbsp;sut.ContraMap(g).ContraMap(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Run&nbsp;both&nbsp;handlers</span> &nbsp;&nbsp;&nbsp;&nbsp;projection1.Execute(input); &nbsp;&nbsp;&nbsp;&nbsp;projection2.Execute(input); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(2,&nbsp;observations.Count); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Single(observations.Distinct()); }</pre> </p> <p> This test defines two local functions, <code>f</code> and <code>g</code>. Once more, you can't directly compare methods for equality, so instead you have to <code>Execute</code> them to verify that they produce the same observable effect. </p> <p> They do. </p> <h3 id="a068d2c54eda4315a4c592aa30b998f4"> Composed inventory adjustment handler <a href="#a068d2c54eda4315a4c592aa30b998f4" title="permalink">#</a> </h3> <p> We can now return to the inventory adjustment example. You may recall that the Controller would <code>Execute</code> a <code>command</code> on an injected <code>ICommandHandler&lt;AdjustInventory&gt;</code>: </p> <p> <pre><span style="color:blue;">this</span>.inventoryAdjuster.Execute(command);</pre> </p> <p> As a first step, we can attempt to compose <code>inventoryAdjuster</code> on the fly: </p> <p> <pre>ICommandHandler&lt;AdjustInventory&gt;&nbsp;<span style="color:#1f377f;">inventoryAdjuster</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;DelegatingCommandHandler&lt;ProductInventory&gt;(repository.Save) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ContraMap((ProductInventory&nbsp;<span style="color:#1f377f;">inv</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(inv&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId)).Handle(command)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ContraMap((AdjustInventory&nbsp;<span style="color:#1f377f;">cmd</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repository.GetByIdOrNull(cmd.ProductId)); inventoryAdjuster.Execute(command);</pre> </p> <p> Contra-mapping is hard to get one's head around, and to make matters worse, you have to read it from the bottom towards the top to understand what it does. It really is contrarian. </p> <p> How do you arrive at something like this? </p> <p> You start by looking at what you have. The Controller may already have an injected <code>repository</code> with various methods. <code>repository.Save</code>, for example, has this signature: </p> <p> <pre><span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Save</span>(ProductInventory&nbsp;<span style="color:#1f377f;">productInventory</span>);</pre> </p> <p> Since it has a <code>void</code> return type, you can treat <code>repository.Save</code> as an <code>Action&lt;ProductInventory&gt;</code>. Wrap it in a <code>DelegatingCommandHandler</code> and you have an <code>ICommandHandler&lt;ProductInventory&gt;</code>: </p> <p> <pre>ICommandHandler&lt;ProductInventory&gt;&nbsp;<span style="color:#1f377f;">inventoryAdjuster</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;DelegatingCommandHandler&lt;ProductInventory&gt;(repository.Save);</pre> </p> <p> That's not what you need, though. You need an <code>ICommandHandler&lt;AdjustInventory&gt;</code>. How do you get closer to that? </p> <p> You already know from the <code>AdjustInventoryService</code> class that you can use a pure function as the core of the impureim sandwich. Try that and see what it gives you: </p> <p> <pre>ICommandHandler&lt;ProductInventory&gt;&nbsp;<span style="color:#1f377f;">inventoryAdjuster</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;DelegatingCommandHandler&lt;ProductInventory&gt;(repository.Save) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ContraMap((ProductInventory&nbsp;<span style="color:#1f377f;">inv</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(inv&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId)).Handle(command));</pre> </p> <p> That doesn't change the type of the handler, but implements the desired functionality. </p> <p> You have an <code>ICommandHandler&lt;ProductInventory&gt;</code> that you need to somehow map to an <code>ICommandHandler&lt;AdjustInventory&gt;</code>. How do you do that? </p> <p> By supplying a function that goes the other way: from <code>AdjustInventory</code> to <code>ProductInventory</code>. Does such a method exist? Yes, it does, on the repository: </p> <p> <pre>ProductInventory&nbsp;<span style="color:#74531f;">GetByIdOrNull</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>);</pre> </p> <p> Or, close enough. While <code>AdjustInventory</code> is not a <code>Guid</code>, it <em>comes with</em> a <code>Guid</code>: </p> <p> <pre>ICommandHandler&lt;AdjustInventory&gt;&nbsp;<span style="color:#1f377f;">inventoryAdjuster</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;DelegatingCommandHandler&lt;ProductInventory&gt;(repository.Save) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ContraMap((ProductInventory&nbsp;<span style="color:#1f377f;">inv</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(inv&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId)).Handle(command)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ContraMap((AdjustInventory&nbsp;<span style="color:#1f377f;">cmd</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repository.GetByIdOrNull(cmd.ProductId));</pre> </p> <p> That's cool, but unfortunately, this composition cheats. It closes over <code>command</code>, which is a run-time variable only available inside the <code>AdjustInventory</code> Controller action. </p> <p> If we're allowed to compose the Command Handler <em>inside</em> the <code>AdjustInventory</code> method, we might as well just have written: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">inv</span>&nbsp;=&nbsp;repository.GetByIdOrNull(command.ProductId); inv&nbsp;=&nbsp;(inv&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(command.ProductId)).Handle(command); repository.Save(inv);</pre> </p> <p> This is clearly much simpler, so why don't we do that? </p> <p> In this particular example, that's probably a better idea overall, but I'm trying to explain what is possible with contravariant functors. The goal here is to decouple the caller (the Controller) from the handler. We want to be able to define the handler outside of the Controller action. </p> <p> That's what the <code>AdjustInventory</code> class does, but can we leverage the contravariant functor to compose an <code>ICommandHandler&lt;AdjustInventory&gt;</code> <em>without</em> adding a new class? </p> <h3 id="01c3971cdf1d43ca8fd5db6005477304"> Composition without closures <a href="#01c3971cdf1d43ca8fd5db6005477304" title="permalink">#</a> </h3> <p> The use of a closure in the above composition is what disqualifies it. Is it possible to compose an <code>ICommandHandler&lt;AdjustInventory&gt;</code> when the <code>command</code> object is unavailable to close over? </p> <p> Yes, but it isn't pretty: </p> <p> <pre>ICommandHandler&lt;AdjustInventory&gt;&nbsp;<span style="color:#1f377f;">inventoryAdjuster</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;DelegatingCommandHandler&lt;ProductInventory&gt;(repository.Save) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ContraMap((ValueTuple&lt;AdjustInventory,&nbsp;ProductInventory&gt;&nbsp;<span style="color:#1f377f;">t</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(t.Item2&nbsp;??&nbsp;<span style="color:blue;">new</span>&nbsp;ProductInventory(t.Item1.ProductId)).Handle(t.Item1)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ContraMap((AdjustInventory&nbsp;<span style="color:#1f377f;">cmd</span>)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(cmd,&nbsp;repository.GetByIdOrNull(cmd.ProductId)));</pre> </p> <p> You can let the composing function return a tuple of the original input value <em>and</em> the projected value. That's what the lowest <code>ContraMap</code> does. This means that the upper <code>ContraMap</code> receives this tuple to map. Not pretty, but possible. </p> <p> I never said that this was the <em>best</em> way to address some of the concerns I've hinted at in this article. The purpose of the article was mainly to give you a sense of what a contravariant functor can do. </p> <h3 id="0a960dd6e8f24121a5b9229b9cca1e7a"> Action as a contravariant functor <a href="#0a960dd6e8f24121a5b9229b9cca1e7a" title="permalink">#</a> </h3> <p> Wrapping an <code>Action&lt;T&gt;</code> in a <code>DelegatingCommandHandler</code> isn't necessary in order to form the contravariant functor. I only used the <code>ICommandHandler</code> interface as an object-oriented-friendly introduction to the example. In fact, any <code>Action&lt;T&gt;</code> gives rise to a contravariant functor with this <code>ContraMap</code> function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Action&lt;T1&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;(<span style="color:blue;">this</span>&nbsp;Action&lt;T&gt;&nbsp;<span style="color:#1f377f;">source</span>,&nbsp;Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;source(selector(x)); }</pre> </p> <p> As you can tell, the function being returned is similar to the lambda expression used to implement <code>ContraMap</code> for <code>ICommandHandler&lt;T&gt;</code>. </p> <p> This turns out to make little difference in the context of the examples shown here, so I'm not going to tire you with more example code. </p> <h3 id="9c7dc6d88a164a0b8bd7ac06eea22dc4"> Conclusion <a href="#9c7dc6d88a164a0b8bd7ac06eea22dc4" title="permalink">#</a> </h3> <p> Any generic polymorphic interface or abstract method with a <code>void</code> return type gives rise to a contravariant functor. This includes the <code>ICommandHandler&lt;T&gt;</code> (originally <code>ICommandService&lt;T&gt;</code>) interface, but also another interface discussed in <a href="/dippp">DIPPP</a>: <code>IEventHandler&lt;TEvent&gt;</code>. </p> <p> The utility of this insight may not be immediately apparent. Contrary to its built-in support for functors, C# doesn't have any language features that light up if you implement a <code>ContraMap</code> function. Even in <a href="https://www.haskell.org">Haskell</a> where <a href="https://hackage.haskell.org/package/base/docs/Data-Functor-Contravariant.html">the Contravariant functor is available in the <em>base</em> library</a>, I can't recall having ever used it. </p> <p> Still, even if not a <em>practical</em> insight, the ubiquitous presence of contravariant functors in everyday programming 'building blocks' tells us something profound about the fabric of programming abstraction and polymorphism. </p> <p> <strong>Next:</strong> <a href="/2021/09/09/the-specification-contravariant-functor">The Specification contravariant functor</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Contravariant functors https://blog.ploeh.dk/2021/09/02/contravariant-functors 2021-09-02T06:49:00+00:00 Mark Seemann <div id="post"> <p> <em>A more exotic kind of universal abstraction.</em> </p> <p> This article series is part of <a href="/2018/03/19/functors-applicatives-and-friends">a larger series of articles about functors, applicatives, and other mappable containers</a>. </p> <p> So far in the article series, you've seen examples of mappable containers that map in the same direction of projections, so to speak. Let's unpack that. </p> <h3 id="272009219c12482cba296186e0e0a645"> Covariance recap <a href="#272009219c12482cba296186e0e0a645" title="permalink">#</a> </h3> <p> <a href="/2018/03/22/functors">Functors</a>, <a href="/2018/10/01/applicative-functors">applicative functors</a>, and <a href="/2018/12/24/bifunctors">bifunctors</a> all follow the direction of projections. Consider the illustration from <a href="/2018/03/22/functors">the article about functors</a>: </p> <p> <img src="/content/binary/functor-diagram.png" alt="Functor diagram."> </p> <p> The function <code>f</code> maps from <code>a</code> to <code>b</code>. You can think of <code>a</code> and <code>b</code> as two types, or two sets. For example, if <code>a</code> is the set of all strings, it might correspond to the type <code>String</code>. Likewise, if <code>b</code> is the set of all integers, then it corresponds to a type called <code>Int</code>. The function <code>f</code> would, in that case, have the type <code>String -&gt; Int</code>; that is: it maps strings to integers. The most natural such function seems to be one that counts the number of characters in a string: </p> <p> <pre>&gt; f = length &gt; f "foo" 3 &gt; f "ploeh" 5</pre> </p> <p> This little interactive session uses <a href="https://www.haskell.org">Haskell</a>, but even if you've never heard about Haskell before, you should still be able to understand what's going on. </p> <p> A functor is a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> of values, for example a collection, a <a href="/2018/03/26/the-maybe-functor">Maybe</a>, a <a href="/2018/09/10/the-lazy-functor">lazy computation</a>, or many other things. If <code>f</code> maps from <code>a</code> to <code>b</code>, then lifting it to the functor <code>F</code> retains the direction. That's what the above figure illustrates. Not only does the functor project <code>a</code> to <code>F a</code> and <code>b</code> to <code>F b</code>, it also maps <code>f</code> to <code>F f</code>, which is <code>F a -&gt; F b</code>. </p> <p> For lists it might look like this: </p> <p> <pre>&gt; fmap f ["bar", "fnaah", "Gauguin"] [3,5,7]</pre> </p> <p> Here <code>fmap</code> lifts the function <code>String -&gt; Int</code> to <code>[String] -&gt; [Int]</code>. Notice that the types 'go in the same direction' when you lift a function to the functor. The types vary <em>with</em> the function - they <em>co</em>-vary; hence <em>covariance</em>. </p> <p> While applicative functors and bifunctors are more complex, they are still covariant. Consult, for example, the diagrams in my <a href="/2018/12/24/bifunctors">bifunctor article</a> to get an intuitive sense that this still holds. </p> <h3 id="187c2160ddf94c119c341534189d3eab"> Contravariance <a href="#187c2160ddf94c119c341534189d3eab" title="permalink">#</a> </h3> <p> What happens if we change the direction of <em>only one</em> arrow? For example, we could change the direction of the <code>f</code> arrow, so that the function is now a function from <code>b</code> to <code>a</code>: <code>b -&gt; a</code>. The figure would look like this: </p> <p> <img src="/content/binary/contravariant-functor-diagram.png" alt="Contravariant functor diagram."> </p> <p> This looks <em>almost</em> like the first figure, with one crucial difference: The lower arrow now goes from right to left. Notice that the upper arrow still goes from left to right: <code>F a -&gt; F b</code>. In other words, the functor varies in the contrary direction than the projected function. It's <em>contravariant</em>. </p> <p> This seems really odd. Why would anyone do that? </p> <p> As is so often the case with universal abstractions, it's not so much a question of coming up with an odd concept and see what comes of it. It's actually an abstract description of some common programming constructs. In this series of articles, you'll see examples of some contravariant functors: </p> <ul> <li><a href="/2021/09/06/the-command-handler-contravariant-functor">The Command Handler contravariant functor</a></li> <li><a href="/2021/09/09/the-specification-contravariant-functor">The Specification contravariant functor</a></li> <li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li> <li><a href="/2021/10/04/reader-as-a-contravariant-functor">Reader as a contravariant functor</a></li> <li><a href="/2021/10/25/functor-variance-compared-to-cs-notion-of-variance">Functor variance compared to C#'s notion of variance</a></li> <li><a href="/2022/03/21/contravariant-dependency-injection">Contravariant Dependency Injection</a></li> </ul> <p> These aren't the only examples, but they should be enough to get the point across. Other examples include equivalence and comparison. </p> <h3 id="b7b1780cc7a74ffaace2a42f06301561"> Lifting <a href="#b7b1780cc7a74ffaace2a42f06301561" title="permalink">#</a> </h3> <p> How do you lift a function <code>f</code> to a contravariant functor? For covariant functors (normally just called <em>functors</em>), Haskell has the <code>fmap</code> function, while in C# you'd be writing a family of <code>Select</code> methods. Let's compare. In Haskell, <code>fmap</code> has this type: </p> <p> <pre>fmap :: Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b</pre> </p> <p> You can read it like this: For any <code>Functor f</code>, <code>fmap</code> lifts a function of the type <code>a -&gt; b</code> to a function of the type <code>f a -&gt; f b</code>. Another way to read this is that given a function <code>a -&gt; b</code> and a container of type <code>f a</code>, you can produce a container of type <code>f b</code>. Due to <a href="https://en.wikipedia.org/wiki/Currying">currying</a>, these two interpretations are both correct. </p> <p> In C#, you'd be writing a <a href="/2018/03/22/functors">method on <code>Functor&lt;T&gt;</code> that looks like this</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector)</pre> </p> <p> This fits the later interpretation of <code>fmap</code>: Given an instance of <code>Functor&lt;T&gt;</code>, you can call <code>Select</code> with a <code>Func&lt;T, TResult&gt;</code> to produce a <code>Functor&lt;TResult&gt;</code>. </p> <p> What does the equivalent function look like for contravariant functors? Haskell <a href="https://hackage.haskell.org/package/base/docs/Data-Functor-Contravariant.html">defines it</a> as: </p> <p> <pre>contramap :: Contravariant f =&gt; (b -&gt; a) -&gt; f a -&gt; f b</pre> </p> <p> You can read it like this: For any <code>Contravariant</code> functor <code>f</code>, <code>contramap</code> lifts a function <code>(b -&gt; a)</code> to a function from <code>f a</code> to <code>f b</code>. Or, in the alternative (but equally valid) interpretation that matches C# better, given a function <code>(b -&gt; a)</code> and an <code>f a</code>, you can produce an <code>f b</code>. </p> <p> In C#, you'd be writing a method on <code>Contravariant&lt;T&gt;</code> that looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Contravariant&lt;T1&gt;&nbsp;<span style="color:#74531f;">ContraMap</span>&lt;<span style="color:#2b91af;">T1</span>&gt;(Func&lt;T1,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">selector</span>)</pre> </p> <p> The actual generic type (here exemplified by <code>Contravariant&lt;T&gt;</code>) will differ, but the shape of the method will be the same. In order to map from <code>Contravariant&lt;T&gt;</code> to <code>Contravariant&lt;T1&gt;</code>, you need a function that <em>goes the other way</em>: <code>Func&lt;T1, T&gt;</code> goes from <code>T1</code> to <code>T</code>. </p> <p> In C#, the function name doesn't have to be <code>ContraMap</code>, since C# doesn't have any built-in understanding of contravariant functors - as opposed to functors, where a method called <code>Select</code> will light up some language features. In this article series I'll stick with <code>ContraMap</code> since I couldn't think of a better name. </p> <h3 id="72905e6e48a343e1bf78deae36be0abf"> Laws <a href="#72905e6e48a343e1bf78deae36be0abf" title="permalink">#</a> </h3> <p> Like functors, applicative functors, <a href="/2017/10/06/monoids">monoids</a>, and other universal abstractions, contravariant functors are characterised by simple laws. The contravariant functor laws are equivalent to the (covariant) <a href="/2018/03/22/functors">functor laws</a>: <em>identity</em> and <em>composition</em>. </p> <p> In pseudo-Haskell, we can express the <em>identity</em> law as: </p> <p> <pre>contramap id = id</pre> </p> <p> and the <em>composition</em> law as: </p> <p> <pre>contramap (g . f) = contramap f . contramap g</pre> </p> <p> The <em>identity</em> law is equivalent to the first functor law. It states that mapping a contravariant functor with the identity function is equivalent to a no-op. The <em>identity function</em> is a function that returns all input unchanged. (It's called the <em>identity function</em> because it's the <em>identity</em> for the <a href="/2017/11/13/endomorphism-monoid">endomorphism monoid</a>.) In <a href="https://fsharp.org">F#</a> and Haskell, this is simply a built-in function called <code>id</code>. </p> <p> In C#, you can write a demonstration of the law as a unit test. Here's the essential part of such a test: </p> <p> <pre>Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">id</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x; Contravariant&lt;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;createContravariant(); Assert.Equal(sut,&nbsp;sut.ContraMap(id),&nbsp;comparer);</pre> </p> <p> The <code>ContraMap</code> method <em>does</em> return a new object, so a custom <code>comparer</code> is required to evaluate whether <code>sut</code> is equal to <code>sut.ContraMap(id)</code>. </p> <p> The <em>composition</em> law governs how composition works. Again, notice how lifting reverses the order of functions. In C#, the relevant unit test code might look like this: </p> <p> <pre>Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s.Length; Func&lt;<span style="color:blue;">int</span>,&nbsp;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;TimeSpan.FromDays(i); Contravariant&lt;TimeSpan&gt;&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;createContravariant(); Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;sut.ContraMap((<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">s</span>)&nbsp;=&gt;&nbsp;g(f(s))), &nbsp;&nbsp;&nbsp;&nbsp;sut.ContraMap(g).ContraMap(f), &nbsp;&nbsp;&nbsp;&nbsp;comparer);</pre> </p> <p> This may actually look less surprising in C# than it does in Haskell. Here the lifted composition doesn't look reversed, but that's because C# doesn't have a composition operator for raw functions, so I instead wrote it as a lambda expression: <code>(string&nbsp;s)&nbsp;=&gt;&nbsp;g(f(s))</code>. If you contrast this C# example with the equivalent assertion of the (covariant) second functor law, you can see that the function order is flipped: <code>f(g(i))</code>. </p> <p> <pre><span style="color:#2b91af;">Assert</span>.Equal(sut.Select(g).Select(f),&nbsp;sut.Select(i&nbsp;=&gt;&nbsp;f(g(i))));</pre> </p> <p> It can be difficult to get your head around the order of contravariant composition without some examples. I'll provide examples in the following articles, but I wanted to leave the definition of the two contravariant functor laws here for reference. </p> <h3 id="68b342bda0ff41f5aec6fb7c08b299d3"> Conclusion <a href="#68b342bda0ff41f5aec6fb7c08b299d3" title="permalink">#</a> </h3> <p> Contravariant functors are functors that map in the opposite direction of an underlying function. This seems counter-intuitive but describes the actual behaviour of quite normal functions. </p> <p> This is hardly illuminating without some examples, so without further ado, let's proceed to the first one. </p> <p> <strong>Next:</strong> <a href="/2021/09/06/the-command-handler-contravariant-functor">The Command Handler contravariant functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Reader functor https://blog.ploeh.dk/2021/08/30/the-reader-functor 2021-08-30T05:42:00+00:00 Mark Seemann <div id="post"> <p> <em>Normal functions form functors. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. In a <a href="/2018/03/26/the-maybe-functor">previous article</a> you saw, for example, how to implement the Maybe functor in C#. In this article, you'll see another functor example: <em>Reader</em>. </p> <p> The Reader functor is similar to the <a href="/2018/09/03/the-identity-functor">Identity functor</a> in the sense that it seems practically useless. If that's the case, then why care about it? </p> <p> As I wrote about the Identity functor: </p> <blockquote> "The inutility of Identity doesn't mean that it doesn't exist. The Identity functor exists, whether it's useful or not. You can ignore it, but it still exists. In C# or <a href="https://fsharp.org">F#</a> I've never had any use for it (although I've <a href="/2017/09/04/builder-as-identity">described it before</a>), while it turns out to be occasionally useful in <a href="https://www.haskell.org">Haskell</a>, where it's built-in. The value of Identity is language-dependent." </blockquote> <p> The same holds for Reader. It exists. Furthermore, it teaches us something important about ordinary functions. </p> <h3 id="1539ecc9bc504aa1a0cc090255777b1d"> Reader interface <a href="#1539ecc9bc504aa1a0cc090255777b1d" title="permalink">#</a> </h3> <p> Imagine the following interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReader</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;<span style="color:#74531f;">Run</span>(R&nbsp;<span style="color:#1f377f;">environment</span>); }</pre> </p> <p> An <code>IReader</code> object can produce a value of the type <code>A</code> when given a value of the type <code>R</code>. The input is typically called the <code>environment</code>. A Reader reads the environment and produces a value. A possible (although not particularly useful) implementation might be: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">GuidToStringReader</span>&nbsp;:&nbsp;IReader&lt;Guid,&nbsp;<span style="color:blue;">string</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;format; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">GuidToStringReader</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">format</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.format&nbsp;=&nbsp;format; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Run</span>(Guid&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;environment.ToString(format); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This may be a silly example, but it illustrates that a a simple class can implement a constructed version of the interface: <code>IReader&lt;Guid,&nbsp;string&gt;</code>. It also demonstrates that a class can take further arguments via its constructor. </p> <p> While the <code>IReader</code> interface only takes a single input argument, <a href="/2018/01/29/argument-list-isomorphisms">we know that an argument list is isomorphic to a parameter object or tuple</a>. Thus, <code>IReader</code> is equivalent to <em>every</em> possible function type - up to isomorphism, assuming that <a href="/2018/01/15/unit-isomorphisms">unit is also a value</a>. </p> <p> While the practical utility of the Reader functor may not be immediately apparent, it's hard to argue that it isn't ubiquitous. Every method is (with a bit of hand-waving) a Reader. </p> <h3 id="23b5c83bffef42a0ab0c95919fef0166"> Functor <a href="#23b5c83bffef42a0ab0c95919fef0166" title="permalink">#</a> </h3> <p> You can turn the <code>IReader</code> interface into a functor by adding an appropriate <code>Select</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IReader&lt;R,&nbsp;B&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">B</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:blue;">this</span>&nbsp;IReader&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">reader</span>,&nbsp;Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;FuncReader&lt;R,&nbsp;B&gt;(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;selector(reader.Run(r))); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FuncReader</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">A</span>&gt;&nbsp;:&nbsp;IReader&lt;R,&nbsp;A&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Func&lt;R,&nbsp;A&gt;&nbsp;func; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FuncReader</span>(Func&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">func</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.func&nbsp;=&nbsp;func; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;A&nbsp;<span style="color:#74531f;">Run</span>(R&nbsp;<span style="color:#1f377f;">environment</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;func(environment); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The implementation of <code>Select</code> requires a private class to capture the projected function. <code>FuncReader</code> is, however, an implementation detail. </p> <p> When you <code>Run</code> a Reader, the output is a value of the type <code>A</code>, and since <code>selector</code> is a function that takes an <code>A</code> value as input, you can use the output of <code>Run</code> as input to <code>selector</code>. Thus, the return type of the lambda expression <code>r =&gt; selector(reader.Run(r))</code> is <code>B</code>. Therefore, <code>Select</code> returns an <code>IReader&lt;R, B&gt;</code>. </p> <p> Here's an example of using the <code>Select</code> method to project an <code>IReader&lt;Guid, string&gt;</code> to <code>IReader&lt;Guid, int&gt;</code>: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">WrappedFunctorExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;Guid,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;GuidToStringReader(<span style="color:#a31515;">&quot;N&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;IReader&lt;Guid,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">projected</span>&nbsp;=&nbsp;r.Select(<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s.Count(<span style="color:#1f377f;">c</span>&nbsp;=&gt;&nbsp;c.IsDigit())); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">input</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Guid(<span style="color:#a31515;">&quot;{CAB5397D-3CF9-40BB-8CBD-B3243B7FDC23}&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(16,&nbsp;projected.Run(input)); }</pre> </p> <p> The expected result is <code>16</code> because the <code>input</code> <code>Guid</code> contains 16 digits (the numbers from 0 to 9). Count them if you don't believe me. </p> <p> As usual, you can also use query syntax: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">QuerySyntaxFunctorExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">projected</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">new</span>&nbsp;GuidToStringReader(<span style="color:#a31515;">&quot;N&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;TimeSpan.FromMinutes(s.Length); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">input</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Guid(<span style="color:#a31515;">&quot;{FE2AB9C6-DDB1-466C-8AAA-C70E02F964B9}&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(32,&nbsp;projected.Run(input).TotalMinutes); }</pre> </p> <p> The actual computation shown here makes little sense, since the result will always be <code>32</code>, but it illustrates that arbitrary projections are possible. </p> <h3 id="ad9b3abef16c4ec8a70a1263c17eecd6"> Raw functions <a href="#ad9b3abef16c4ec8a70a1263c17eecd6" title="permalink">#</a> </h3> <p> The <code>IReader&lt;R, A&gt;</code> interface isn't really necessary. It was just meant as an introduction to make things a bit easier for object-oriented programmers. You can write a similar <code>Select</code> extension method for any <code>Func&lt;R, A&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Func&lt;R,&nbsp;B&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">A</span>,&nbsp;<span style="color:#2b91af;">B</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:blue;">this</span>&nbsp;Func&lt;R,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">func</span>,&nbsp;Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;selector(func(r)); }</pre> </p> <p> Compare this implementation to the one above. It's essentially the same lambda expression, but now <code>Select</code> returns the raw function instead of wrapping it in a class. </p> <p> In the following, I'll use raw functions instead of the <code>IReader</code> interface. </p> <h3 id="de5e5a6801ab49d4ae993181a05f7bab"> First functor law <a href="#de5e5a6801ab49d4ae993181a05f7bab" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual, it's proper computer-science work to actually prove this, but you can write some tests to demonstrate the first functor law for the <code>IReader&lt;R, A&gt;</code> interface. In this article, you'll see parametrised tests written with <a href="https://xunit.net">xUnit.net</a>. First, the first functor law: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;corge&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;antidisestablishmentarianism&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FirstFunctorLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">input</span>) { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">id</span>&lt;<span style="color:#2b91af;">T</span>&gt;(T&nbsp;<span style="color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;f.Select(id); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(f(input),&nbsp;actual(input)); }</pre> </p> <p> The 'original' Reader <code>f</code> (for <em>function</em>) takes a <code>string</code> as input and returns its length. The <code>id</code> function (which isn't built-in in C#) is implemented as a <a href="https://docs.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/local-functions">local function</a>. It returns whichever input it's given. </p> <p> Since <code>id</code> returns any input without modifying it, it'll also return any number produced by <code>f</code> without modification. </p> <p> To evaluate whether <code>f</code> is equal to <code>f.Select(id)</code>, the assertion calls both functions with the same input. If the functions have equal behaviour, they ought to return the same output. </p> <p> The above test cases all pass. </p> <h3 id="1dc17b41888b420d8b8161f9ef9fb84c"> Second functor law <a href="#1dc17b41888b420d8b8161f9ef9fb84c" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that a function (Reader) obeys the second functor law: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;corge&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;antidisestablishmentarianism&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">SecondFunctorLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">input</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">h</span>&nbsp;=&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">b</span>&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;t&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;f&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h.Select(g).Select(f)(input), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h.Select(<span style="color:#1f377f;">i</span>&nbsp;=&gt;&nbsp;f(g(i)))(input)); }</pre> </p> <p> You can't easily compare two different functions for equality, so, like above, this test defines equality as the functions producing the same result when you invoke them. </p> <p> Again, while the test doesn't <em>prove</em> anything, it demonstrates that for the five test cases, it doesn't matter if you project the 'original' Reader <code>h</code> in one or two steps. </p> <h3 id="76a34d3dfde1438abd080f02499bc344"> Haskell <a href="#76a34d3dfde1438abd080f02499bc344" title="permalink">#</a> </h3> <p> In Haskell, normal functions <code>a -&gt; b</code> are already <code>Functor</code> instances, which means that you can easily replicate the functions from the <code>SecondFunctorLaw</code> test: </p> <p> <pre>&gt; h = length &gt; g i = i `mod` 2 == 0 &gt; f b = if b then 't' else 'f' &gt; (fmap f $ fmap g $ h) "ploeh" 'f'</pre> </p> <p> Here <code>f</code>, <code>g</code>, and <code>h</code> are equivalent to their above C# namesakes, while the last line composes the functions stepwise and calls the composition with the input string <code>"ploeh"</code>. In Haskell you generally read code from right to left, so this composition corresponds to <code>h.Select(g).Select(f)</code>. </p> <h3 id="9d3006be0e904994b2285ae791fd287a"> Conclusion <a href="#9d3006be0e904994b2285ae791fd287a" title="permalink">#</a> </h3> <p> Functions give rise to functors, usually known collectively as the Reader functor. Even in Haskell where this fact is ingrained into the fabric of the language, I rarely make use of it. It just is. In C#, it's likely to be even less useful for practical programming purposes. </p> <p> That a function <code>a -&gt; b</code> forms a functor, however, is an important insight into just what a function actually is. It describes an essential property of functions. In itself this may still seem underwhelming, but mixed with some other properties (that I'll describe in <a href="/2021/10/04/reader-as-a-contravariant-functor">a future article</a>) it can produce some profound insights. So stay tuned. </p> <p> <strong>Next:</strong> <a href="/2020/06/22/the-io-functor">The IO functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Am I stuck in a local maximum? https://blog.ploeh.dk/2021/08/09/am-i-stuck-in-a-local-maximum 2021-08-09T05:56:00+00:00 Mark Seemann <div id="post"> <p> <em>On long-standing controversies, biases, and failures of communication.</em> </p> <p> If you can stay out of politics, Twitter can be a great place to engage in robust discussions. I mostly follow and engage with people in the programming community, and every so often find myself involved in a discussion about one of several long-standing controversies. No, not the tabs-versus-spaces debate, but other debates such as functional versus object-oriented programming, dynamic versus static typing, or <a href="/2020/03/16/conways-law-latency-versus-throughput">oral versus written collaboration</a>. </p> <p> It happened again the past week, but while this article is a reaction, it's not about the specific debacle. Thus, I'm not going to link to the tweets in question. </p> <p> These discussion usually leave me wondering why people with decades of industry experience seem to have such profound disagreements. </p> <h3 id="13c8dc8fbc9b4e26ba08bfeef9b61fd9"> I might be wrong <a href="#13c8dc8fbc9b4e26ba08bfeef9b61fd9" title="permalink">#</a> </h3> <p> Increasingly, I find myself disagreeing with my heroes. This isn't a comfortable position. Could I be wrong? </p> <p> I've definitely been wrong before. For example, in my article <a href="/2016/02/10/types-properties-software">Types + Properties = Software</a>, I wrote about type systems: </p> <blockquote> <p>"To the far right, we have a hypothetical language with such a strong type system that, indeed, <em>if it compiles, it works.</em>"</p> </blockquote> <p> <em>To the right</em>, in this context, means <em>more statically typed</em>. While the notion is natural, the sentence is uninformed. When I wrote the article, I <a href="https://www.goodreads.com/review/show/1731926050">hadn't yet read</a> Charles Petzold's excellent <a href="http://amzn.to/2n9MFGh">Annotated Turing</a>. Although I had heard about the <a href="https://en.wikipedia.org/wiki/Halting_problem">halting problem</a> before reading the book, I hadn't internalised it. I wasn't able to draw inferences based on that labelled concept. </p> <p> After I read the book, I've come to understand that general-purpose static type system can never prove unequivocally that a generic program works. That's what <a href="https://en.wikipedia.org/wiki/Alonzo_Church">Church</a>, <a href="https://en.wikipedia.org/wiki/Alan_Turing">Turing</a>, and <a href="https://en.wikipedia.org/wiki/Kurt_Gödel">Gödel</a> proved. </p> <p> I've been writing articles on this blog <a href="/2009/01/28/LivingInInterestingTimes">since January 2009</a>. To date, I've published 582 posts. Some are bound to be misinformed, outdated, or just plain wrong. Due to the sheer volume, I make no consistent effort to retroactively monitor and correct my past self. (I'm happy to engage with specific posts. If you feel that an old post is misleading, erroneous, or the like, please <a href="https://github.com/ploeh/ploeh.github.com#comments">leave a comment</a>.) </p> <p> For good measure, despite my failure to understand the implications of the halting problem, I'm otherwise happy with the article series <a href="/2016/02/10/types-properties-software">Types + Properties = Software</a>. You shouldn't consider this particular example a general condemnation of it. It's just an example of a mistake I made. This time, I'm aware of it, but there are bound to be plenty of other examples where I don't even realise it. </p> <h3 id="d7843f0140164eaf97a7724413d70d76"> Heroes <a href="#d7843f0140164eaf97a7724413d70d76" title="permalink">#</a> </h3> <p> I don't have a formal degree in computer science. As so many others of my age, I began my software career by tinkering with computers and (later) programming. The first five years of my professional career, there wasn't much methodology to the way I approached software development. Self-taught often means that you have to learn everything the hard way. </p> <p> This changed when I first heard about test-driven development (TDD). I credit <a href="https://martinfowler.com">Martin Fowler</a> with that. Around the same time I also read <a href="http://amzn.to/XBYukB">Design Patterns</a>. Armed with those two techniques, I was able to rescue a failed software project and bring it to successful completion. I even received an (internal) award for it. </p> <p> While there's more to skilled programming than test-driven development and patterns, it wasn't a bad place to start. Where, before, I had nothing that even resembled a methodology, now I had a set of practices I could use. This gave me an opportunity to experiment and observe. A few years later, <a href="/2010/12/03/Towardsbetterabstractions">I'd already started to notice some recurring beneficial patterns</a> in the code that I wrote, as well as <a href="/2010/12/22/TheTDDApostate">some limits of TDD</a>. </p> <p> Still, that was a decade where I voraciously read, attended conferences, and tried to learn from my heroes. I hope that they won't mind that I list them here: <ul> <li><a href="https://martinfowler.com">Martin Fowler</a></li> <li><a href="https://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a></li> <li><a href="https://en.wikipedia.org/wiki/Robert_C._Martin">Robert C. Martin</a></li> <li><a href="https://michaelfeathers.silvrback.com">Michael Feathers</a></li> <li><a href="https://dannorth.net">Dan North</a></li> </ul> Surely, there were others. I remember being a big fan of <a href="https://en.wikipedia.org/wiki/Don_Box">Don Box</a>, but he seems to have withdrawn from the public long time ago. There were also .NET trailblazers that I admired and tried to emulate. Later, I learned much from the early luminaries of <a href="https://fsharp.org">F#</a>. I'm not going to list all the people I admire here, because the list could never be complete, and I don't wish to leave anyone feeling left out. Related to the point I'm trying to make, all these other wonderful people give me less pause. </p> <p> There's a reason I list those particular heroes. I should include a few more of whom I wasn't aware in my formative years, but whom I've since come to highly respect: <a href="https://twitter.com/marick">Brian Marick</a> and <a href="https://jessitron.com">Jessica Kerr</a>. </p> <p> Why do I mention these heroes of mine? </p> <h3 id="ae81dde11de94f9fb223cf06377a22c4"> Bias <a href="#ae81dde11de94f9fb223cf06377a22c4" title="permalink">#</a> </h3> <p> Humans aren't as rational as we'd like to think. We all have plenty of <a href="https://en.wikipedia.org/wiki/Cognitive_bias">cognitive biases</a>. I'm aware of a few of mine, but I expect most of them to be hidden from me. Sometimes, it's easier to spot the bias in others. Perhaps, by spotting the bias in others, it reveals something about oneself? </p> <p> I increasingly find myself disagreeing with my heroes. One example is the long-standing controversy about static versus dynamic typing. </p> <p> I hope I'm not misrepresenting anyone, but the heroes I enumerate above seem to favour dynamic typing over static typing - some more strongly than others. This worries me. </p> <p> These are people who've taught me a lot; whose opinion I deeply respect, and yet I fail to grasp the benefit of dynamic typing. What are the benefits they gain from their preferred languages that I'm blind to? What am I missing? </p> <p> Whenever I find myself disagreeing with my heroes, I can't help question my own judgment. Am I biased? Yes, obviously, but in which way? What bias prohibits me from seeing the benefits that are so obvious to them? </p> <p> It's too easy to jump to conclusions - to erect a dichotomy: <ul> <li>My heroes are right, and I'm wrong</li> <li>My heroes are <em>all</em> wrong, and I'm right</li> </ul> The evidence doesn't seem to support the latter conclusion, but if the first is true, I still fail to understand <em>why</em> I'm wrong. </p> <p> I'm hoping that there's a more nuanced position to take - that the above is a <a href="https://en.wikipedia.org/wiki/False_dilemma">false dichotomy</a>. </p> <h3 id="1dc21ee0532e4555ab621dfd32f31f12"> What's the problem? <a href="#1dc21ee0532e4555ab621dfd32f31f12" title="permalink">#</a> </h3> <p> Perhaps we're really talking past each other. Perhaps we're trying to solve different problems, and thereby arrive at different solutions. </p> <p> I can only guess at the kinds of problems that my heroes think of when they prefer dynamic languages, and I don't want to misrepresent them. What I <em>can</em> do, however, is outline the kind of problem that I typically have in mind. </p> <p> I've spent much of my career trying to balance <a href="/2019/03/04/code-quality-is-not-software-quality">sustainability</a> with correctness. I consider correctness as a prerequisite for all code. As <a href="http://amzn.to/1jos26M">Gerald Weinberg implies</a>, if a program doesn't have to work, anything goes. Thus, sustainability is a major focus for me: how do we develop software that can sustain our organisation now <em>and</em> in the future? How do we structure and organise code so that future change is possible? </p> <p> Whenever I get into debates, that's implicitly the problem on my mind. It'd probably improve communication if I stated this explicitly going into every debate, but sometimes, I get dragged sideways into a debacle... I do, however, speculate that much disagreement may stem from such implicit assumptions. I bring my biases and implicit problem statements into any discussion. I consider it only human if my interlocutors do the same, but their biases and implicit problem understanding may easily be different than mine. What are they, I wonder? </p> <p> This seems to happen not only in the debate about dynamic versus static types. I get a similar sense when I discuss collaboration. Most of my heroes seem to advocate for high-band face-to-face collaboration, while <a href="/2020/03/16/conways-law-latency-versus-throughput">I favour asynchronous, written communication</a>. Indeed, I admit that my bias is speaking. I self-identify as a contrarian introvert (although, again, we should be careful not turning introversion versus extroversion into binary classification). </p> <p> Still, even when I try to account for my bias, I get the sense that my opponents and I may actually try to accomplish a common goal, but by addressing two contrasting problems. </p> <p> <img src="/content/binary/same-goal-different-starting-points.png" alt="Two arrows pointing to the same problem from different directions."> </p> <p> I think and hope that, ultimately, we're trying to accomplish the same goal: to deliver and sustain business capability. </p> <p> I do get the sense that the proponents of more team co-location, more face-to-face collaboration are coming at the problem from a different direction than I am. Perhaps the problem they're trying to solve is micro-management, red tape, overly bureaucratic processes, and a lack of developer autonomy. I can certainly see that if that's the problem, talking to each other is likely to improve the situation. I'd recommend that too, in such a situation. </p> <p> Perhaps it's a local Danish (or perhaps Northern European) phenomenon, but that's not the kind of problem I normally encounter. Most organisations who ask for my help essentially have <em>no</em> process. Everything is ad hoc, nothing is written down, deployment is a manual process, and there are meetings and interruptions all the time. Since nothing is written down, decisions aren't recorded, so team members and stakeholders keep having the same meetings over and over. Again, little gets done, but for an entirely different reason than too much bureaucracy. I see this more frequently than too much red tape, so I tend to recommend that people start leaving behind some sort of written trail of what they've been doing. Pull request reviews, for example, are great for that, and I see <a href="/2021/06/21/agile-pull-requests">no incongruity between agile and pull requests</a>. </p> <h3 id="37232ffbc5cf48e58ec773ca83046807"> Shaped by scars <a href="#37232ffbc5cf48e58ec773ca83046807" title="permalink">#</a> </h3> <p> The inimitable <a href="https://twitter.com/richcampbell">Richard Campbell</a> has phrased our biases as the scars we collect during our careers. If you've deleted the production database one too many times, you develop routines and practices to avoid doing that in the future. If you've mostly worked in an organisation that stifled progress by subjecting you to <a href="https://en.wikipedia.org/wiki/Brazil_(1985_film)">Brazil</a>-levels of bureaucracy, it's understandable if you develop a preference for less formal methods. And if, like me, you've mostly seen dysfunction manifest as a <em>lack</em> of beneficial practices, you develop a taste for written over oral communication. </p> <p> Does it go further than that? Are we also shaped by our successes, just like we are shaped by our scars? </p> <p> The first time I had professional success <em>with a methodology</em> was when I discovered TDD. This made me a true believer in TDD. I'm still a big proponent of TDD, but since I learned what <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> <a href="/2016/11/28/easy-domain-modelling-with-types">can do</a> in terms of modelling, I see <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">no reason to write a run-time test if I instead can get the compiler to enforce a rule</a>. </p> <p> In a recent discussion, some of my heroes expressed the opinion that they don't need <a href="/2021/06/07/abstruse-nomenclature">fancy</a> functional-programming concepts and features to write good code. I'm sure that they don't. </p> <p> My heroes have written code for decades. While I <em>have</em> met bad programmers with decades of experience, most programmers who last that long ultimately become good programmers. I'm not so worried about them. </p> <p> The people who need my help are typically younger teams. Statistically, there just aren't that many <a href="/2020/09/14/we-need-young-programmers-we-need-old-programmers">older programmers</a> around. </p> <p> When I recommend certain practices or programming techniques, those recommendations are aimed at anyone who care to listen. Usually, I find that the audience who engage with me is predominantly programmers with five to ten years of professional experience. </p> <h3 id="0d6d6bee68644d158deb5fa8cf478be7"> Anecdotal evidence <a href="#0d6d6bee68644d158deb5fa8cf478be7" title="permalink">#</a> </h3> <p> This is a difficult discussion to have. I think that another reason that we keep returning to the same controversies is that we mostly rely on <a href="https://martinfowler.com/bliki/AnecdotalEvidence.html">anecdotal evidence</a>. As we progress through our careers, we observe what works and what doesn't, but it's likely that <a href="https://en.wikipedia.org/wiki/Confirmation_bias">confirmation bias</a> makes us remember the results that we already favour, whereas we conveniently forget about the outcomes that don't fit our preferred narrative. </p> <p> Could we settle these discussions with more science? Alas, <a href="/2020/05/25/wheres-the-science">that's difficult</a>. </p> <p> I can't think of anything better, then, than keep having the same discussions over and over. I try hard to overcome my biases to understand the other side, and now and then, I learn something that I find illuminating. It doesn't seem to be a particularly efficient way to address these difficult questions, but I don't know what else to do. What's the alternative to discussion? To <em>not</em> discuss? To <em>not</em> exchange ideas? </p> <p> That seems worse. </p> <h3 id="ea7ed446c9094c1985f585223fbddb26"> Conclusion <a href="#ea7ed446c9094c1985f585223fbddb26" title="permalink">#</a> </h3> <p> In this essay, I've tried to come to grips with an increasing cognitive incongruity that I'm experiencing. I find myself disagreeing with my heroes on a regular basis, and that makes me uncomfortable. Could it be that I'm erecting an echo chamber for myself? </p> <p> The practices that I follow and endorse work well for me, but could I be stuck in a local maximum? </p> <p> This essay has been difficult to write. I'm not sure that I've managed to convey my doubts and motivations. Should I have named my heroes, only to describe how I disagree with them? Will it be seen as aggressive? </p> <p> I hope not. I'm writing about my heroes with reverence and gratitude for all that they've taught me. I mean no harm. </p> <p> At the same time, I'm struggling with reconciling that they rarely seem to agree with me these days. Perhaps they never did. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0de8cf1ba14ad74a440eb0b29f2e437b"> <div class="comment-author"><a href="https://news.ycombinator.com/tylerhou">tylerhou</a> <a href="#0de8cf1ba14ad74a440eb0b29f2e437b">#</a></div> <div class="comment-content"> <p> Re: type systems. I think you are wrong about being wrong. You *can* create languages with type systems that are strong enough to guarantee that if a program compiles, it "works." Languages like this exist. The most well known is <a href="https://en.wikipedia.org/wiki/Coq">Coq</a>, a programming language/proof-assistant which provides tools for proving that properties of your program hold. So literally, if it compiles, your properties are proven true (assuming the proof assistant does not have bugs). </p> <p> Why doesn't halting problem apply here? The halting problem does not conclude that "all programs cannot be determined to halt [for a given input]." It says something much weaker -- there is no algorithm to determine whether an *arbitrary program* will halt given an arbitrary input. But there are still (large) subsets of programs where there *does* exist such a halting algorithm, and practically, these programs are common. In particular, if a language disallows certain types of recursion (or iteration), you can easily prove that programs written in that language will halt. </p> <p> For example, if you used a subset of C that did not allow recursion, goto, or loops, clearly every program written in that subset would terminate. If you expanded your subset to allow loops with a statically-known iteration bound you would know that any program in that new subset would also terminate. Then, instead of only statically bounded loops, you could choose to allow any loop with a finite bound (e.g. iterating over a finite list), and termination would still be guaranteed. </p> <p> I am not as familiar with Coq, so I will discuss a similar language (<a href="https://www.idris-lang.org/">Idris</a>) that implements "substructual recursion," a generalization of the above intuition. In functional languages, the main barrier to proving termination is unbounded recusion. (In imperative languages, loops are also an issue, but loops are equivalent to recursion.) So <a href="https://docs.idris-lang.org/en/latest/tutorial/theorems.html#totality-checking">Idris provides a language subset</a> that only allows substructural recursion -- i.e. if a function <code>f</code> eventually calls itself, it must only call itself with arguments that are "smaller" than the first call. (E.g. for natural numbers smaller means less than, and for lists smaller means a list with a smaller size.) This property is checked at compile time. Since all function cases must be covered the recursive calls must eventually terminate. </p> <p> In practice, most programs don't need unbounded recursion. Most useful algorithms<sup>[citation needed]</sup> have some bound on the number of iterations needed. To modify any arbitrary algorithm to fit a language like Idris it is simple to introduce another natural number parameter <code>N</code> that always decreases by one on recursive calls. When that parameter reaches zero, the function should return an error. Now it is simple for Idris to prove that any call to that function must terminate within <code>N</code> recursions. On the initial call you can set this number to be the upper bound on your algorithm, and since you know that your algorithm will never need that many iterations you know you will always get an answer (the error is just there to satisfy the totality checker). </p> </div> <div class="comment-date">2021-08-11 05:40 UTC</div> </div> <div class="comment" id="02f4fd84d77c4006adc696457fe407dd"> <div class="comment-author"><a href="https://twitter.com/_rchaves_">_rchaves_</a> <a href="#02f4fd84d77c4006adc696457fe407dd">#</a></div> <div class="comment-content"> <p> Hello there! </p> <p> That is a very nice write up, impeccable choice of words. I could see myself a lot in it, except that at some point I was not in disagreement with the dynamic or static types approach, but that somehow I enjoyed both, and couldn’t reconcile this contradiction until some months ago, so I hope my experience can help you too. </p> <p> Going through a recent experience of moving from a consulting company (the one Martin Fowler works on) with good practices and TDD all the way, strong types and no runtime exceptions (Elm) to a Perl shop with no tests, no pull requests, nothing, I can tell you it felt like a roller coaster. </p> <p> At first I thought everything was wrong, and wanted to change it all, to play the consultant. But I decided not to stress out too much about work, and with time I actually started seeing the beauty in all that too, and the advantages this "move fast and break things" culture brought. Meanwhile, on the side, I was still having fun building Elm apps, learning about Haskell, curious about TLA+, writing tests for everything, etc. I felt conflicted, how is it that one can see beauty in both approaches? Which one do I think is the best? </p> <p> Luckily for me, I was also very interested in data, statistics, causality, etc, and somehow I think that lead me to read all the books by Nassim Taleb (one of my heroes now), but it was finally Kent Back (on top of Nassim's insights) that made it all click in place, from a similar experience at Facebook, with his 3X theory, which I really recommend you to watch if you haven't already: <a href="https://www.youtube.com/watch?v=FlJN6_4yI2A">https://www.youtube.com/watch?v=FlJN6_4yI2A</a>. </p> <p> I realised then that my views were actually not in contradiction at all, it was an obvious case of Consulting 101: "it depends". None of the approaches is the best one alone, they are conditional on the environment you are, the risks you face, what are you trying to achieve. You said "we are shaped by our scars", but I think we can (with a lot of effort) actually take a step back and acknowledge our scars as you did, and be mindful of what environments our experiences are conditional too, to try to see different paths that different people went, so you can experience that too and find other "local maxima". Having the two (or more) local maxima in your toolbelt in a way gets you closer to the global maxima :) </p> <p> I also wrote a blogpost, and happy to talk more about it if you are interested: <a href="https://rchavesferna.medium.com/designed-v-evolutionary-code-d52a54408c8f">https://rchavesferna.medium.com/designed-v-evolutionary-code-d52a54408c8f</a> </p> <p> Good luck on your journey <br> Cheers! </p> </div> <div class="comment-date">2021-08-11 09:44 UTC</div> </div> <div class="comment" id="51a93388e4fe499e9d953afb494ee43a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#51a93388e4fe499e9d953afb494ee43a">#</a></div> <div class="comment-content"> <p> tylerhou, thank you for writing. Yes, you're correct. I should have more explicitly stated that there's no algorithm to determine whether an arbitrary program in a Turing-complete language halts. This does highlight the importance of explicitly stating assumptions and preconditions, which I failed to fully do here. </p> </div> <div class="comment-date">2021-08-12 7:33 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Tennis kata revisited https://blog.ploeh.dk/2021/08/03/the-tennis-kata-revisited 2021-08-03T10:45:00+00:00 Mark Seemann <div id="post"> <p> <em>When you need discriminated unions, but all you have are objects.</em> </p> <p> After I learned that <a href="/2018/06/25/visitor-as-a-sum-type">the Visitor design pattern is isomorphic to sum types</a> (AKA <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/discriminated-unions">discriminated unions</a>), I wanted to try how easy it is to carry out a translation in practice. For that reason, I decided to translate my go-to <a href="https://fsharp.org">F#</a> implementation of the <a href="https://codingdojo.org/kata/Tennis">Tennis kata</a> to C#, using the <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor design pattern</a> everywhere I'd use a discriminated union in F#. </p> <p> The resulting C# code shows that it is, indeed, possible to 'mechanically' translate discriminated unions to the Visitor design pattern. Given that the Visitor pattern requires multiple interfaces and classes to model just a single discriminated union, it's no surprise that the resulting code is much more complex. As a solution to the Tennis kata itself, all this complexity is unwarranted. On the other hand, as an exercise in getting by with the available tools, it's quite illustrative. If all you have is C# (or a similar language), but you really need discriminated unions, the solution is ready at hand. It'll be complex, but not complicated. </p> <p> The main insight of this exercise is that translating any discriminated union to a Visitor is, indeed, possible. You can best internalise such insights, however, if you actually do the work yourself. Thus, in this article, I'll only show a few highlights from my own exercise. I highly recommend that you try it yourself. </p> <h3 id="5667e0a2df95411da703aab3df3ddcb3"> Typical F# solution <a href="#5667e0a2df95411da703aab3df3ddcb3" title="permalink">#</a> </h3> <p> You can see my typical F# solution in great detail in my article series <a href="/2016/02/10/types-properties-software">Types + Properties = Software</a>. To be clear, there are many ways to implement the Tennis kata, even in F#, and the one shown in the articles is neither overly clever nor <a href="/2021/03/29/table-driven-tennis-scoring">too boring</a>. As implementations go, this one is quite pedestrian. </p> <p> My emphasis with that kind of solution to the Tennis kata is on readability, correctness, and testability. Going for <a href="/2018/11/19/functional-architecture-a-definition">a functional architecture</a> automatically <a href="/2015/05/07/functional-design-is-intrinsically-testable">addresses the testability concern</a>. In F#, I endeavour to use the excellent type system to <a href="https://blog.janestreet.com/effective-ml-video">make illegal states unrepresentable</a>. Discriminated unions are essential ingredients in that kind of design. </p> <p> In F#, I'd typically <a href="/2016/02/10/types-properties-software-designing-with-types">model a Tennis game score as a discriminated union</a> like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;= |&nbsp;<span style="color:navy;">Points</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">PointsData</span> |&nbsp;<span style="color:navy;">Forty</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">FortyData</span> |&nbsp;<span style="color:navy;">Deuce</span> |&nbsp;<span style="color:navy;">Advantage</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">Player</span> |&nbsp;<span style="color:navy;">Game</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">Player</span></pre> </p> <p> That's not the only discriminated union involved in the implementation. <code>Player</code> is also a discriminated union, and both <code>PointsData</code> and <code>FortyData</code> compose a third discriminated union called <code>Point</code>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;=&nbsp;<span style="color:navy;">Love</span>&nbsp;|&nbsp;<span style="color:navy;">Fifteen</span>&nbsp;|&nbsp;<span style="color:navy;">Thirty</span></pre> </p> <p> Please refer to <a href="/2016/02/10/types-properties-software-designing-with-types">the older article</a> for full details of the F# 'domain model'. </p> <p> This was the sort of design I wanted to try to translate to C#, using the Visitor design pattern in place of discriminated unions. </p> <h3 id="90107f78b2a64ad288c62905aca58011"> Player <a href="#90107f78b2a64ad288c62905aca58011" title="permalink">#</a> </h3> <p> In F# you must declare all types and functions before you can use them. To newcomers, this often looks like a constraint, but is <a href="/2015/04/15/c-will-eventually-get-all-f-features-right">actually one of F#'s greatest strengths</a>. Since other types transitively use the <code>Player</code> discriminated union, this is the first type you have to define in an F# code file: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Player</span>&nbsp;=&nbsp;<span style="color:navy;">PlayerOne</span>&nbsp;|&nbsp;<span style="color:navy;">PlayerTwo</span></pre> </p> <p> This one is fairly straightforward to translate to C#. You might reach for an <code>enum</code>, but those aren't really type-safe in C#, since they're little more than glorified integers. Using a discriminated union is safer, so define a Visitor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPlayerVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">VisitPlayerOne</span>(); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">VisitPlayerTwo</span>(); }</pre> </p> <p> A Visitor interface is where you enumerate the cases of the discriminated union - in this example <em>player one</em> and <em>player two</em>. You can <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatically</a> prefix each method with <code>Visit</code> as I've done here, but that's optional. </p> <p> Once you've defined the Visitor, you can declare the 'actual' type you're modelling: the player: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPlayer</span> { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">Accept</span>&lt;<span style="color:#2b91af;">T</span>&gt;(IPlayerVisitor&lt;T&gt;&nbsp;<span style="color:#1f377f;">visitor</span>); }</pre> </p> <p> Those are only the polymorphic types required to model the discriminated union. You also need to add concrete classes for each of the cases. Here's <code>PlayerOne</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">PlayerOne</span>&nbsp;:&nbsp;IPlayer { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="color:#74531f;">Accept</span>&lt;<span style="color:#2b91af;">T</span>&gt;(IPlayerVisitor&lt;T&gt;&nbsp;<span style="color:#1f377f;">visitor</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;visitor.VisitPlayerOne(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is an example of the Visitor pattern's central <em>double dispatch</em> mechanism. Clients of the <code>IPlayer</code> interface will dispatch execution to the <code>Accept</code> method, which again dispatches execution to the <code>visitor</code>. </p> <p> I decided to make <code>PlayerOne</code> a <code>struct</code> because it holds no data. Perhaps I <a href="/2021/03/08/pendulum-swing-sealed-by-default">also ought to have sealed it</a>, or, as <a href="http://amzn.to/XBYukB">Design Patterns</a> likes to suggest, make it a <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a>. </p> <p> Hardly surprising, <code>PlayerTwo</code> looks almost identical to <code>PlayerOne</code>. </p> <p> Apart from a <a href="https://fsharpforfunandprofit.com/posts/designing-with-types-single-case-dus">single-case discriminated union</a> (which is degenerate), a discriminated union doesn't get any simpler than <code>Player</code>. It has only two cases and carries no data. Even so, it takes <em>four</em> types to translate it to a Visitor: two interfaces and two concrete classes. This highlights how the Visitor pattern adds significant complexity. </p> <p> And it only gets worse with more complex discriminated unions. </p> <h3 id="1ce1c4cb87894ae183799a140a9001fe"> Score <a href="#1ce1c4cb87894ae183799a140a9001fe" title="permalink">#</a> </h3> <p> I'm going to leave the translation of <code>Point</code> as an exercise. It's similar to the translation of <code>Player</code>, but instead of two cases, it enumerates three cases. Instead, consider how to enumerate the cases of <code>Score</code>. First, add a Visitor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IScoreVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">VisitPoints</span>(IPoint&nbsp;<span style="color:#1f377f;">playerOnePoint</span>,&nbsp;IPoint&nbsp;<span style="color:#1f377f;">playerTwoPoint</span>); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">VisitForty</span>(IPlayer&nbsp;<span style="color:#1f377f;">player</span>,&nbsp;IPoint&nbsp;<span style="color:#1f377f;">otherPlayerPoint</span>); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">VisitDeuce</span>(); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">VisitAdvantage</span>(IPlayer&nbsp;<span style="color:#1f377f;">advantagedPlayer</span>); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">VisitGame</span>(IPlayer&nbsp;<span style="color:#1f377f;">playerWhoWonGame</span>); }</pre> </p> <p> Notice that these methods take arguments, apart from <code>VisitDeuce</code>. I could have made that member a read-only property instead, but for consistency's sake, I kept it as a method. </p> <p> All the other methods take arguments that are, in their own right, Visitors. </p> <p> In addition to the Visitor interface, you also need an interface to model the score itself: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IScore</span> { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">Accept</span>&lt;<span style="color:#2b91af;">T</span>&gt;(IScoreVisitor&lt;T&gt;&nbsp;<span style="color:#1f377f;">visitor</span>); }</pre> </p> <p> This one defines, as usual, just a single <code>Accept</code> method. </p> <p> Since <code>IScoreVisitor</code> enumerates five distinct cases, you must also add five concrete implementations of <code>IScore</code>. Here's <code>Forty</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Forty</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IPlayer&nbsp;player; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IPoint&nbsp;otherPlayerPoint; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Forty</span>(IPlayer&nbsp;<span style="color:#1f377f;">player</span>,&nbsp;IPoint&nbsp;<span style="color:#1f377f;">otherPlayerPoint</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.player&nbsp;=&nbsp;player; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.otherPlayerPoint&nbsp;=&nbsp;otherPlayerPoint; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="color:#74531f;">Accept</span>&lt;<span style="color:#2b91af;">T</span>&gt;(IScoreVisitor&lt;T&gt;&nbsp;<span style="color:#1f377f;">visitor</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;visitor.VisitForty(player,&nbsp;otherPlayerPoint); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I'm leaving other concrete classes as an exercise to the reader. All of them are similar, though, in that they all implement <code>IScore</code> and unconditionally dispatch to 'their' method on <code>IScoreVisitor</code> - <code>Forty</code> calls <code>VisitForty</code>, <code>Points</code> calls <code>VisitPoints</code>, and so on. Each concrete implementation has a distinct constructor, though, since what they need to dispatch to the Visitor differs. </p> <p> <code>Deuce</code>, being degenerate, doesn't have to explicitly define a constructor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Deuce</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="color:#74531f;">Accept</span>&lt;<span style="color:#2b91af;">T</span>&gt;(IScoreVisitor&lt;T&gt;&nbsp;<span style="color:#1f377f;">visitor</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;visitor.VisitDeuce(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The C# compiler automatically adds a parameterless constructor when none is defined. An alternative implementation would, again, be to make <code>Deuce</code> a Singleton. </p> <p> In all, it takes seven types (two interfaces and five concrete classes) to model <code>Score</code> - a type that requires only a few lines of code in F# (six lines in my code, but you could format it more densely if you want to compromise readability). </p> <h3 id="6ac8c03643eb454f9dfcc76365d7ef2e"> Keeping score <a href="#6ac8c03643eb454f9dfcc76365d7ef2e" title="permalink">#</a> </h3> <p> In order to calculate the score of a game, I also translated the <code>score</code> function. I put that in an <a href="https://docs.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/extension-methods">extension method</a> so as to not 'pollute' the <code>IScore</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IScore&nbsp;<span style="color:#74531f;">BallTo</span>(<span style="color:blue;">this</span>&nbsp;IScore&nbsp;<span style="color:#1f377f;">score</span>,&nbsp;IPlayer&nbsp;<span style="color:#1f377f;">playerWhoWinsBall</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;score.Accept(<span style="color:blue;">new</span>&nbsp;ScoreVisitor(playerWhoWinsBall)); }</pre> </p> <p> Given an <code>IScore</code> value, there's little you can do with it, apart from calling its <code>Accept</code> method. In order to do that, you'll need an <code>IScoreVisitor</code>, which I defined as a private nested class: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ScoreVisitor</span>&nbsp;:&nbsp;IScoreVisitor&lt;IScore&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IPlayer&nbsp;playerWhoWinsBall; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ScoreVisitor</span>(IPlayer&nbsp;<span style="color:#1f377f;">playerWhoWinsBall</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.playerWhoWinsBall&nbsp;=&nbsp;playerWhoWinsBall; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Implementation&nbsp;goes&nbsp;here...</span></pre> </p> <p> Some of the methods are trivial to implement, like <code>VisitDeuce</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IScore&nbsp;<span style="color:#74531f;">VisitDeuce</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Advantage(playerWhoWinsBall); }</pre> </p> <p> Most others are more complicated. Keep in mind that the method arguments (<code>IPlayer</code>, <code>IPoint</code>) are Visitors in their own right, so in order to do anything useful with them, you'll have to call their <code>Accept</code> methods with a corresponding, specialised Visitor. </p> <h3 id="054ae9485c294ba2a9b9029271f540fa"> Pattern matching <a href="#054ae9485c294ba2a9b9029271f540fa" title="permalink">#</a> </h3> <p> I quickly realised that this would become too tedious, even for an exercise, so I leveraged my knowledge that the Visitor pattern is isomorphic to a <a href="/2018/05/22/church-encoding">Church encoding</a>. Instead of defining umpteen specialised Visitors, I just defined a generic <code>Match</code> method for each Visitor-based object. I put those in extension methods as well. Here's the <code>Match</code> method for <code>IPlayer</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;T&nbsp;<span style="color:#74531f;">Match</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;IPlayer&nbsp;<span style="color:#1f377f;">player</span>,&nbsp;T&nbsp;<span style="color:#1f377f;">playerOne</span>,&nbsp;T&nbsp;<span style="color:#1f377f;">playerTwo</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;player.Accept(<span style="color:blue;">new</span>&nbsp;MatchVisitor&lt;T&gt;(playerOne,&nbsp;playerTwo)); }</pre> </p> <p> The implementation is based on a private nested <code>MatchVisitor</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MatchVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;IPlayerVisitor&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;T&nbsp;playerOneValue; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;T&nbsp;playerTwoValue; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">MatchVisitor</span>(T&nbsp;<span style="color:#1f377f;">playerOneValue</span>,&nbsp;T&nbsp;<span style="color:#1f377f;">playerTwoValue</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.playerOneValue&nbsp;=&nbsp;playerOneValue; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.playerTwoValue&nbsp;=&nbsp;playerTwoValue; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="color:#74531f;">VisitPlayerOne</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;playerOneValue; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;T&nbsp;<span style="color:#74531f;">VisitPlayerTwo</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;playerTwoValue; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This enables pattern matching, upon which you can implement other reusable methods, such as <code>Other</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IPlayer&nbsp;<span style="color:#74531f;">Other</span>(<span style="color:blue;">this</span>&nbsp;IPlayer&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;player.Match&lt;IPlayer&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerOne:&nbsp;<span style="color:blue;">new</span>&nbsp;PlayerTwo(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerTwo:&nbsp;<span style="color:blue;">new</span>&nbsp;PlayerOne()); }</pre> </p> <p> It's also useful to be able to compare two players and return two alternative values depending on the result: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;T&nbsp;<span style="color:#74531f;">Equals</span>&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IPlayer&nbsp;<span style="color:#1f377f;">p1</span>, &nbsp;&nbsp;&nbsp;&nbsp;IPlayer&nbsp;<span style="color:#1f377f;">p2</span>, &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#1f377f;">same</span>, &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#1f377f;">different</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;p1.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerOne:&nbsp;p2.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerOne:&nbsp;same, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerTwo:&nbsp;different), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerTwo:&nbsp;p2.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerOne:&nbsp;different, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerTwo:&nbsp;same)); }</pre> </p> <p> You can add similar <code>Match</code> and <code>Equals</code> extension methods for <code>IPoint</code>, which enables you to implement all the methods of the <code>ScoreVisitor</code> class. Here's <code>VisitForty</code> as an example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IScore&nbsp;<span style="color:#74531f;">VisitForty</span>(IPlayer&nbsp;<span style="color:#1f377f;">player</span>,&nbsp;IPoint&nbsp;<span style="color:#1f377f;">otherPlayerPoint</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;playerWhoWinsBall.Equals(player, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;same:&nbsp;<span style="color:blue;">new</span>&nbsp;Game(player), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;different:&nbsp;otherPlayerPoint.Match&lt;IScore&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;love:&nbsp;<span style="color:blue;">new</span>&nbsp;Forty(player,&nbsp;<span style="color:blue;">new</span>&nbsp;Fifteen()), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fifteen:&nbsp;<span style="color:blue;">new</span>&nbsp;Forty(player,&nbsp;<span style="color:blue;">new</span>&nbsp;Thirty()), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thirty:&nbsp;<span style="color:blue;">new</span>&nbsp;Deuce())); }</pre> </p> <p> If <code>playerWhoWinsBall.Equals(player</code> the implementation matches on <code>same</code>, and returns <code>new Game(player)</code>. Otherwise, it matches on <code>different</code>, in which case it then has to <code>Match</code> on <code>otherPlayerPoint</code> to figure out what to return. </p> <p> Again, I'll leave the remaining code as an exercise to the reader. </p> <h3 id="f476c043ca9548b58f874fb0b87aaf70"> Tests <a href="#f476c043ca9548b58f874fb0b87aaf70" title="permalink">#</a> </h3> <p> While all this code is written in C#, it's all <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. Thus, <a href="/2015/05/07/functional-design-is-intrinsically-testable">it's intrinsically testable</a>. Knowing that, I could have added tests after writing the 'production code', but that's so boring, so I still developed this code base using test-driven development. Here's an example of a test: </p> <p> <pre>[Theory,&nbsp;MemberData(nameof(Player))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">TransitionFromDeuce</span>(IPlayer&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Deuce(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.BallTo(player); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Advantage(player); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> I wrote all tests as <a href="/2020/08/31/properties-for-all">properties for all</a>. The above test uses <a href="https://xunit.net">xUnit.net</a> 2.4.0. The <code>Player</code> data source is a private <code>MemberData</code> defined as a read-only property: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEnumerable&lt;<span style="color:blue;">object</span>[]&gt;&nbsp;Player { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">object</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;PlayerOne()&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">object</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;PlayerTwo()&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Other tests are defined in a likewise manner. </p> <h3 id="720181cad99a4cd79c41435d622a949b"> Conclusion <a href="#720181cad99a4cd79c41435d622a949b" title="permalink">#</a> </h3> <p> Once you've tried to solve problems with <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> it can be frustrating to return to languages that don't have <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a> (discriminated unions). There's no need to despair, though. Sum types are isomorphic to the Visitor design pattern, so it <em>is</em> possible to solve problems in the same way. </p> <p> The resulting code can easily <em>seem</em> complex because a simple discriminated union translates to multiple C# files. Another option is to use Church encoding, but many developers who consider themselves object-oriented often balk at such constructs. When it comes to policy, the Visitor design pattern offers the advantage that it's described in <a href="http://amzn.to/XBYukB">Design Patterns</a>. While it may be a bit of an underhanded tactic, it effectively deals with some teams' resistance to ideas originating in functional programming. </p> <p> The exercise outlined in this article demonstrates that translating from algebraic data types to patterns-based object-oriented code is not only possible, but (once you get the hang of it) unthinking and routine. It's entirely automatable, so you could even imagine defining a <a href="https://en.wikipedia.org/wiki/Domain-specific_language">DSL</a> for defining sum types and transpiling them to C#. </p> <p> But really, you don't have to, because <a href="https://fsharp.org">such a DSL already exists</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="efdd9051556447b8be4831a793c1bbbf"> <div class="comment-author"><a href="https://github.com/Joker-vD">Joker_vD</a> <a href="#efdd9051556447b8be4831a793c1bbbf">#</a></div> <div class="comment-content"> <p> The most annoying thing about the visitor pattern is that you can't get around declaring <code>interface IMyLovelyADT { R Accept&lt;R&gt;(IMyLovelyADTVisitor&lt;R&gt; visitor); }</code> and a separate class implementing this interface for each data variant. One may think that this interface is isomorphic to <code>Func&lt;IMyLovelyADTVisitor&lt;R&gt;, R&gt;</code> but it actually is not, look: </p> <p><pre>// type Bool = True | False interface IBoolVisitor&lt;R&gt; { R True(); R False(); } static class BoolUtils { public static Func&lt;IBoolVisitor&lt;R&gt;, R&gt; True&lt;R&gt;() =&gt; sym =&gt; sym.True(); public static Func&lt;IBoolVisitor&lt;R&gt;, R&gt; False&lt;R&gt;() =&gt; sym =&gt; sym.False(); private class BoolMatcher&lt;R&gt; : IBoolVisitor&lt;R&gt; { Func&lt;R&gt; OnTrue { get; } Func&lt;R&gt; OnFalse { get; } public BoolMatcher(Func&lt;R&gt; onTrue, Func&lt;R&gt; onFalse) { OnTrue = onTrue; OnFalse = onFalse; } public R True() =&gt; OnTrue(); public R False() =&gt; OnFalse(); } public static R Match&lt;R&gt;(this Func&lt;IBoolVisitor&lt;R&gt;, R&gt; value, Func&lt;R&gt; onTrue, Func&lt;R&gt; onFalse) =&gt; value(new BoolMatcher&lt;R&gt;(onTrue, onFalse)); }</pre></p> <p> Yes, all this typechecks and compiles, and you can even write <code>ShowBool</code> and <code>BoolToInt</code> methods, but! You can't combine them: </p> <p><pre>static class FunctionsAttempt1 { public static string ShowBool(Func&lt;IBoolVisitor&lt;string&gt;, string&gt; value) { return value.Match(() =&gt; "True", () =&gt; "False"); } public static int BoolToInt(Func&lt;IBoolVisitor&lt;int&gt;, int&gt; value) { return value.Match(() =&gt; 1, () =&gt; 0); } public static string CombineBoolFunctions&lt;R&gt;(Func&lt;IBoolVisitor&lt;R&gt;, R&gt; value) { return ShowBool(value) + ": " + BoolToInt(value).ToString(); } }</pre></p> <p> The first two methods are fine, but <code>CombineBoolFunctions</code> doesn't compile, because you can't pass <code>Func&lt;IBoolVisitor&lt;R&gt;, R&gt;</code> to a method that expects <code>Func&lt;IBoolVisitor&lt;string&gt;, string&gt;</code>. What if you try to make <code>ShowBool</code> and <code>BoolToInt</code> accept <code>Func&lt;IBoolVisitor&lt;R&gt;, R&gt;</code>? </p> <p><pre>static class FunctionsAttempt2 { public static string ShowBool<r>(Func<><r>, R> value) { return value.Match<string>(() => "True", () => "False"); } public static int BoolToInt<r>(Func<><r>, R> value) { return value.Match<int>(() => 1, () => 0); } public static string CombineBoolFunctions<r>(Func<><r>, R> value) { return ShowBool(value) + ": " + BoolToInt(value).ToString(); } }</r></r></int></r></r></string></r></r></pre></p> <p> That also doesn't work, for pretty much the same reason: <code>CombineBoolFunctions</code> compiles now, but not <code>ShowBool</code> or <code>BoolToInt</code>. That's why you need a <em>non-generic</em> wrapper interface <code>IMyLovelyADT</code>: it essentially does the same job as Haskell's <code>forall</code>, since generic types are not quite proper types in C#'s type system. Interestingly enough, upcoming Go 2's generics will <em>not</em> support this scenario: a method inside an interface will be able to use only the generic parameters that the interface itself declares. </p> </div> <div class="comment-date">2021-08-06 20:47 UTC</div> </div> <div class="comment" id="e9b335c980b649ccb18a015868b55763"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e9b335c980b649ccb18a015868b55763">#</a></div> <div class="comment-content"> <p> Joker_vD, thank you for writing; you explained that well. </p> </div> <div class="comment-date">2021-08-07 20:25 UTC</div> </div> <div class="comment" id="d385b2cd13834e19b587a5d80048794e"> <div class="comment-author">Tyrie Vella <a href="#d385b2cd13834e19b587a5d80048794e">#</a></div> <div class="comment-content"> <p> I've recently been using C# 9's <code>record</code> feature to implement ADTs. For example: </p> <p><pre> public abstract record Player { private Player() {} public sealed record One : Player; public sealed record Two : Player; } public abstract record Point { private Point() {} public sealed record Love: Point; public sealed record Fifteen: Point; public sealed record Thirty: Point; } public abstract record Score { private Score() {} public sealed record Points(Point PlayerOnePoints, Point PlayerTwoPoints); public sealed record Forty(Player Player, Point OtherPlayersPoint); public sealed record Deuce; public sealed record Advantage(Player Player); public sealed record Game(Player Player); } </pre></p> <p> It's not as good as F# ADTs, but it's more concise than Visitors, works with <code>switch</code> pattern matching, has structural equality semantics, and I haven't found the downside yet. </p> </div> <div class="comment-date">2021-08-17 00:16 UTC</div> </div> <div class="comment" id="33ae4341e1b44975b0171c0f99bbe263"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#33ae4341e1b44975b0171c0f99bbe263">#</a></div> <div class="comment-content"> <p> Tyrie, thank you for writing. I haven't tried that yet, but I agree that if this works, it's simpler. </p> <p> Does <code>switch</code> pattern matching check exhaustiveness? What I means is: With sum types/discriminated unions, as well as with the Visitor pattern, the compiler can tell you if you haven't covered all cases. Does the C# compiler also do that with <code>switch</code> pattern matching? </p> <p> And do you need to include a <code>default</code> label? </p> </div> <div class="comment-date">2021-08-17 5:39 UTC</div> </div> <div class="comment" id="9bd1ef73f41243f6bb02f0825ccc245b"> <div class="comment-author">Tyrie Vella <a href="#9bd1ef73f41243f6bb02f0825ccc245b">#</a></div> <div class="comment-content"> <p> Looks like you found the flaw. The C# compiler tries, and it will block invalid cases, but it always wants a default case (either <code>_</code> or both <code>"null"</code> and <code>"not null"</code>) when switching on one of these. It can't suggest the actually missing cases. </p> <p> It's also only a warning by default at compile time. </p> </div> <div class="comment-date">2021-08-17 15:43 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Referential transparency fits in your head https://blog.ploeh.dk/2021/07/28/referential-transparency-fits-in-your-head 2021-07-28T12:13:00+00:00 Mark Seemann <div id="post"> <p> <em>Why functional programming matters.</em> </p> <p> This article is mostly excerpts from my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. The overall message is too important to exclusively hide away in a book, though, which is the reason I also publish it here. </p> <p> The illustrations are preliminary. While writing the manuscript, I experimented with hand-drawn figures, but Addison-Wesley prefers 'professional' illustrations. In the published book, the illustrations shown here will be replaced by cleaner, more readable, but also more boring figures. </p> <blockquote> <h3 id="7a0e7dacbe4048cc8d22583258e39267"> Nested composition <a href="#7a0e7dacbe4048cc8d22583258e39267" title="permalink">#</a> </h3> <p> Ultimately, software interacts with the real world. It paints pixels on the screen, saves data in databases, sends emails, posts on social media, controls industrial robots, etcetera. All of these are what we in the context of <a href="https://en.wikipedia.org/wiki/Command-query_separation">Command Query Separation</a> call <em>side effects</em>. </p> <p> Since side effects are software's raison d'être, it seems only natural to model composition around them. This is how most people tend to approach object-oriented design. You model <em>actions</em>. </p> <p> Object-oriented composition tends to focus on composing side effects together. The <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a> design pattern may be the paragon of this style of composition, but most of the patterns in <a href="http://amzn.to/XBYukB">Design Patterns</a> rely heavily on composition of side effects. </p> <p> As illustrated in [the following figure] this style of composition relies on nesting objects in other objects, or side effects in other side effects. Since your goal should be code that fits in your head, this is a problem. </p> <p> <img src="/content/binary/nested-composition.jpg" alt="Objects nested within other objects."> </p> <p> [Figure caption:] The typical composition of objects (or, rather, methods on objects) is nesting. The more you compose, the less the composition fits in your brain. In this figure, each star indicates a side effect that you care about. Object <em>A</em> encapsulates one side effect, and object <em>B</em> two. Object <em>C</em> composes <em>A</em> and <em>B</em>, but also adds a fourth side effect. That's already four side effects that you need to keep in mind when trying to understand the code. This easily gets out of hand: object <em>E</em> composes a total of eight side effects, and <em>F</em> nine. Those don't fit well in your brain. </p> </blockquote> <p> I should add here that one of the book's central tenets is that <a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two">the human short-term memory can only hold a limited amount of information</a>. Code that fits in your head is code that respects that limitation. This is a topic I've already addressed in <a href="https://cleancoders.com/episode/humane-code-real-episode-1">my Humane Code video</a>. </p> <p> In the book, I use the number <em>seven</em> as a symbol of the this cognitive limit. Nothing I argue, however, relies on this exact number. The point is only that short-term memory is quite limited. <em>Seven</em> is only a shorthand for that. </p> <p> The book proceeds to provide a code example that illustrates how fast nested composition accumulates complexity that exceeds the capacity of your short-term memory. You can see the code example in the book, or in the article <a href="/2020/11/23/good-names-are-skin-deep">Good names are skin-deep</a>, which makes a slightly different criticism than the one argued in the book. </p> <p> The section on nested composition goes on: </p> <blockquote> <blockquote> <p> "Abstraction is the elimination of the irrelevant and the amplification of the essential" </p> <footer><cite>Robert C. Martin, <a href="http://amzn.to/19W4JHk">APPP</a></cite></footer> </blockquote> <p> By hiding a side effect in a Query, I've <em>eliminated</em> something essential. In other words, more is going on in [the book's code listing] than meets the eye. The <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> may be as low as <em>4</em>, but there's a hidden fifth action that you ought to be aware of. </p> <p> Granted, five chunks still fit in your brain, but that single hidden interaction is an extra 14% towards the budget of seven. It doesn't take many hidden side effects before the code no longer fits in your head </p> <h3 id="3e2c16e5e5d34e9790fbfe13f3e1f974"> Sequential composition <a href="#3e2c16e5e5d34e9790fbfe13f3e1f974" title="permalink">#</a> </h3> <p> While nested composition is problematic, it isn't the only way to compose things. You can also compose behaviour by chaining it together, as illustrated in [the following figure]. </p> <p> <img src="/content/binary/sequential-composition.jpg" alt="Two composed arrows - one is pointing to the other, thereby creating one arrow composed from two."> </p> <p> [Figure caption:] Sequential composition of two functions. The output from <code>Where</code> becomes the input to <code>Allocate</code>. </p> <p> In the terminology of Command Query Separation, Commands cause trouble. Queries, on the other hand, tend to cause little trouble. They return data which you can use as input for other Queries. </p> </blockquote> <p> Again, the book proceeds to show code examples. You can, of course, see the code in the book, but the methods discussed are the <code>WillAccept</code> function <a href="/2020/11/30/name-by-role">shown here</a>, the <code>Overlaps</code> function <a href="/2021/03/01/pendulum-swing-internal-by-default">shown here</a>, as well as a few other functions that I don't think that I've shown on the blog. </p> <blockquote> <p> The entire restaurant example code base is written with that principle in mind. Consider the <code>WillAccept</code> method [...]. After all the Guard Clauses it first creates a new instance of the <code>Seating</code> class. You can think of a constructor as a Query under the condition that it has no side effects. </p> <p> The next line of code filters the <code>existingReservations</code> using the <code>Overlaps</code> method [...] as a predicate. The built-in <code>Where</code> method is a Query. </p> <p> Finally, the <code>WillAccept</code> method returns whether there's <code>Any</code> table among the <code>availableTables</code> that <code>Fits</code> the <code>candidate.Quantity</code>. The <code>Any</code> method is another built-in Query, and <code>Fits</code> is a predicate. </p> <p> Compared to [the sequential composition figure], you can say that the <code>Seating</code> constructor, <code>seating.Overlaps</code>, <code>Allocate</code>, and <code>Fits</code> are sequentially composed. </p> <p> None of these methods have side effects, which means that once <code>WillAccept</code> returns its Boolean value, you can forget about how it reached that result. It truly eliminates the irrelevant and amplifies the essential </p> <h3 id="e15e5142ede7460bbf52bebf9102783f"> Referential transparency <a href="#e15e5142ede7460bbf52bebf9102783f" title="permalink">#</a> </h3> <p> There's a remaining issue that Command Query Separation fails to address: predictability. While a Query has no side effects that your brain has to keep track of, it could still surprise you if you get a new return value every time you call it - even with the same input. </p> <p> This may not be quite as bad as side effects, but it'll still tax your brain. What happens if we institute an extra rule on top of Command Query Separation: that Queries must be deterministic? </p> <p> This would mean that a Query can't rely on random number generators, GUID creation, the time of day, day of the month, or any other data from the environment. That would include the contents of files and databases. That sounds restrictive, so what's the benefit? </p> <p> A <em>deterministic</em> method without side effects is <em>referentially transparent</em>. It's also known as a <em>pure function</em>. Such functions have some very desirable qualities. </p> <p> One of these qualities is that pure functions readily compose. If the output of one function fits as the input for another, you can sequentially compose them. Always. There are <a href="https://bartoszmilewski.com/2014/11/04/category-the-essence-of-composition">deep mathematical reasons</a> for that, but suffice it to say that composition is ingrained into the fabric that pure functions are made of. </p> <p> Another quality is that you can replace a pure function call with its result. The function call is <em>equal</em> to the output. The only difference between the result and the function call is the time it takes to get it. </p> <p> Think about that in terms of Robert C. Martin's definition of abstraction. Once a pure function returns, the result is all you have to care about. How the function arrived at the result is an implementation detail. Referentially transparent functions eliminate the irrelevant and amplify the essential. As [the following figure] illustrates, they collapse arbitrary complexity to a single result; a single chunk that fits in your brain. </p> <p> <img src="/content/binary/pure-function-collapsing-to-its-result.jpg" alt="A pure function illustrated as circle with internal cogs, with arrows pointing to a single point to its right."> </p> <p> [Figure caption:] Pure function (left) collapsing to its result (right). Regardless of complexity, a referentially transparent function call can be replaced by its output. Thus, once you know what the output is, that's the only thing you need to keep track of as you read and interpret the calling code. </p> <p> On the other hand, if you want to know how the function works, you zoom in on its implementation, in the spirit of fractal architecture. That might be the <code>WillAccept</code> method [...]. This method is, in fact, not just Query, it's a pure function. When you look at the source code of that function, you've zoomed in on it, and the surrounding context is irrelevant. It operates exclusively on its input arguments and immutable class fields. </p> <p> When you zoom out again, the entire function collapses into its result. It's the only thing your brain needs to keep track of. </p> </blockquote> <p> Regardless of complexity, a referentially transparent function reduces to a single chunk: the result that it produces. Thus, referentially transparent code is code that fits in your head. </p> <h3 id="271a289f1907493ead7064542e22b490"> Conclusion <a href="#271a289f1907493ead7064542e22b490" title="permalink">#</a> </h3> <p> <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> isn't a book about functional programming, but it does advocate for the <em>functional core, imperative shell</em> (also known as an <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>) architecture, among many other techniques, practices, and heuristics that it presents. </p> <p> I hope that you found the excerpt so inspiring that you consider buying the book. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b02363b9e7e64f0db23d2459b57332f5"> <div class="comment-author">Gonzalo Waszczuk <a href="#b02363b9e7e64f0db23d2459b57332f5">#</a></div> <div class="comment-content"> <p>Absolutely agree. The problem of "Code that fits in your head" is not about code either, it's about <i>all human reasoning</i>. It particularly happens in math. In math, why do we prove "theorems", and then prove new theorems by referencing those previous theorems or lemmas? Why can't mathematicians just prove everything every single time? Because math can't fit in someone's head, so we also need these tools.</p> <p>Or rather, math <i>is</i> the tool for "X that fits in your head". Math is the language of human reasoning, anything that can be reasoned about can be modelled in math and talked about in math, which is a language we all know about and have used for thousands of years.</p> <p>It can help us developers a lot to make use of this shared language. There already exists a language to help us figure out how to Fit Code In Our Heads, it would be detrimental to ourselves to ignore it. You link to category theory, and there's also algebra. These are tools for such endeavor, I think developers should be more open towards them and not just deride them as "hard" or "ivory-tower-esque", they should be the opposite of that. And in fact, whne you properly understand algebra, there is nothing really hard about it, it's actually so simple and easy you can't understand why you didn't pay attention to it sooner.</p> </div> <div class="comment-date">2021-08-02 02:07 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The State functor https://blog.ploeh.dk/2021/07/19/the-state-functor 2021-07-19T15:00:00+00:00 Mark Seemann <div id="post"> <p> <em>Stateful computations as a functor. An example for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. In a <a href="/2018/03/26/the-maybe-functor">previous article</a>, you saw how to implement the Maybe functor in C#. In this article, you'll see another functor example: <em>State</em>. </p> <p> In functional programming, sooner or later a particular question comes up: How do you implement a stateful computation without mutating state? </p> <p> You use a polymorphic function that takes the current state as input and returns the new state and a result as output. In a C-based language like C#, you can model it as an interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IState</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;T,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Run</span>(S&nbsp;<span style="color:#1f377f;">state</span>); }</pre> </p> <p> The interface is generic in both the type of state and the type of return value. Notice that the type declaration lists the state type <code>S</code> before the type of the value <code>T</code>, whereas the returned tuple lists <code>T</code> before <code>S</code>. This is quite confusing, but is how <a href="https://www.haskell.org">Haskell</a> does it. Not ideal, but I've chosen to keep that convention for the benefit of readers who'd like to compare the various implementations. </p> <p> This article introduces the implementation and machinery of the type. In a later article I'll show an example. </p> <h3 id="9fb14b122a344d1c92760bea12097a70"> A nonsense example <a href="#9fb14b122a344d1c92760bea12097a70" title="permalink">#</a> </h3> <p> You can implement the interface by doing something useful, or, as in the following example, something fatuous like expanding (or contracting) all vowels in a word according to an integer state: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">VowelExpander</span>&nbsp;:&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;text; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">VowelExpander</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">text</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.text&nbsp;=&nbsp;text; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">Run</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;vowels&nbsp;=&nbsp;<span style="color:#a31515;">&quot;aeiouy&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expanded</span>&nbsp;=&nbsp;text.SelectMany(<span style="color:#1f377f;">c</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vowels.Contains(c)&nbsp;? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enumerable.Repeat(c,&nbsp;state)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;c&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">newState</span>&nbsp;=&nbsp;state&nbsp;+&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(expanded.ToArray()),&nbsp;newState); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class repeats each vowel in a string by the number indicated by the current state. It also increments the state. Here's a parametrised test that shows how various input produces different outputs: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;0,&nbsp;<span style="color:#a31515;">&quot;f&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;1,&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;2,&nbsp;<span style="color:#a31515;">&quot;foooo&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;0,&nbsp;<span style="color:#a31515;">&quot;br&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;2,&nbsp;<span style="color:#a31515;">&quot;baar&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BasicUsageExample</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">txt</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">count</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;VowelExpander(txt); &nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;s.Run(count); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Tuple.Create(expected,&nbsp;count&nbsp;+&nbsp;1),&nbsp;t); }</pre> </p> <p> That's just one, simple stateful computation. It's a silly example, but it's <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>. </p> <h3 id="c8f53e5b19e64458a4f7b42de60991d0"> Functor <a href="#c8f53e5b19e64458a4f7b42de60991d0" title="permalink">#</a> </h3> <p> You can turn the <code>IState</code> interface into a functor by adding an appropriate <code>Select</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IState&lt;S,&nbsp;T1&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;T,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">selector</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;SelectState&lt;S,&nbsp;T,&nbsp;T1&gt;(source,&nbsp;selector); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SelectState</span>&lt;<span style="color:#2b91af;">S</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;:&nbsp;IState&lt;S,&nbsp;T1&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;IState&lt;S,&nbsp;T&gt;&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Func&lt;T,&nbsp;T1&gt;&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">SelectState</span>(IState&lt;S,&nbsp;T&gt;&nbsp;<span style="color:#1f377f;">source</span>,&nbsp;Func&lt;T,&nbsp;T1&gt;&nbsp;<span style="color:#1f377f;">selector</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.source&nbsp;=&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.selector&nbsp;=&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;T1,&nbsp;S&gt;&nbsp;<span style="color:#74531f;">Run</span>(S&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;T,&nbsp;S&gt;&nbsp;<span style="color:#1f377f;">tuple</span>&nbsp;=&nbsp;source.Run(state); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T1&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;selector(tuple.Item1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(projection,&nbsp;tuple.Item2); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> A functor maps from one contained type to another, in this case from <code>T</code> to <code>T1</code>, while the state type <code>S</code> remains the same. Notice that it's possible to change the value of the state, but not the type. Even though the State functor has two generic type arguments, it's <em>not</em> a <a href="/2018/12/24/bifunctors">bifunctor</a>. You can pick any type you'd like for <code>S</code>, such as <code>int</code> in the above <code>VowelExpander</code>, but once you've picked a type for the state, you can't project it. It's possible to prove that you can't implement a lawful mapping for the <code>S</code> dimension of State, but if you'd like to understand it intuitively, it's a great exercise to try to implement a function from <code>IState&lt;S, T&gt;</code> to <code>IState&lt;S1, T&gt;</code>. Try it, and you'll soon learn why this is impossible. </p> <p> Here's an example of using the <code>Select</code> method to project an <code>IState&lt;int, string&gt;</code> to <code>IState&lt;int, int&gt;</code>: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BasicSelectExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;VowelExpander(<span style="color:#a31515;">&quot;bar&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;=&nbsp;s.Select(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x.Length); &nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;projection.Run(2); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Tuple.Create(4,&nbsp;3),&nbsp;t); }</pre> </p> <p> As usual, you can also use query syntax: </p> <p> <pre>[Fact] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">QuerySyntaxExample</span>() { &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;VowelExpander(<span style="color:#a31515;">&quot;baz&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;<span style="color:blue;">int</span>,&nbsp;DayOfWeek&gt;&nbsp;<span style="color:#1f377f;">projection</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;txt&nbsp;<span style="color:blue;">in</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;txt.Length&nbsp;%&nbsp;2&nbsp;==&nbsp;0&nbsp;?&nbsp;DayOfWeek.Friday&nbsp;:&nbsp;DayOfWeek.Sunday; &nbsp;&nbsp;&nbsp;&nbsp;Tuple&lt;DayOfWeek,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&nbsp;projection.Run(3); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Tuple.Create(DayOfWeek.Sunday,&nbsp;4),&nbsp;t); }</pre> </p> <p> This is, once again, a nonsensical function that only exists to show that arbitrary projections are possible. </p> <h3 id="c39f632f07d345c98940a7c65ffeead1"> First functor law <a href="#c39f632f07d345c98940a7c65ffeead1" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual, it's proper computer-science work to actually prove this, but you can write some tests to demonstrate the first functor law for the <code>IState&lt;S, T&gt;</code> interface. In this article, you'll see parametrised tests written with <a href="https://xunit.net">xUnit.net</a>. First, the first functor law: </p> <p> <pre>[Theory] [InlineData(DayOfWeek.Monday)] [InlineData(DayOfWeek.Tuesday)] [InlineData(DayOfWeek.Wednesday)] [InlineData(DayOfWeek.Thursday)] [InlineData(DayOfWeek.Friday)] [InlineData(DayOfWeek.Saturday)] [InlineData(DayOfWeek.Sunday)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FirstFunctorLaw</span>(DayOfWeek&nbsp;<span style="color:#1f377f;">day</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;Guid,&nbsp;Guid&gt;&nbsp;<span style="color:#1f377f;">id</span>&nbsp;=&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&gt;&nbsp;g; &nbsp;&nbsp;&nbsp;&nbsp;IState&lt;DayOfWeek,&nbsp;Guid&gt;&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;DayIdentifier(); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(s.Run(day),&nbsp;s.Select(id).Run(day)); }</pre> </p> <p> This test uses another frivolous <code>IState</code> implementation: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DayIdentifier</span>&nbsp;:&nbsp;IState&lt;DayOfWeek,&nbsp;Guid&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Guid&nbsp;Monday&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Guid(<span style="color:#a31515;">&quot;5AB18569-29C7-4041-9719-5255266B808D&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;Guid&nbsp;OtherDays&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;Guid(<span style="color:#a31515;">&quot;00553FC8-82C9-40B2-9FAA-F9ADFFD4EE66&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tuple&lt;Guid,&nbsp;DayOfWeek&gt;&nbsp;<span style="color:#74531f;">Run</span>(DayOfWeek&nbsp;<span style="color:#1f377f;">state</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(state&nbsp;==&nbsp;DayOfWeek.Monday) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(Monday,&nbsp;DayOfWeek.Tuesday); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Tuple.Create(OtherDays,&nbsp;DayOfWeek.Monday); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I only chose to write another implementation of <code>IState</code> to show a bit of variation, and to demonstrate that both <code>S</code> and <code>T</code> can be whichever type you need them to be. </p> <p> The above test cases pass. </p> <h3 id="9126274332ae4a319d8db277fef66a68"> Second functor law <a href="#9126274332ae4a319d8db277fef66a68" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that <code>IState</code> obeys the second functor law: </p> <p> <pre>[Theory] [InlineData(&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;0)] [InlineData(&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1)] [InlineData(&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>,&nbsp;2)] [InlineData(<span style="color:#a31515;">&quot;quux&quot;</span>,&nbsp;3)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">SecondFunctorLaw</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">txt</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#1f377f;">g</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x.Length; &nbsp;&nbsp;&nbsp;&nbsp;Func&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;<span style="color:#1f377f;">f</span>&nbsp;=&nbsp;<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">s</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;VowelExpander(txt); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.Select(g).Select(f).Run(i), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.Select(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;f(g(x))).Run(i)); }</pre> </p> <p> This test defines two local functions, <code>f</code> and <code>g</code>. <del datetime="2021-08-27T20:01:10Z">Instead of explicitly declaring the functions as <code>Func</code> variables, this test uses a (relatively) new C# feature called <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions">local functions</a>.</del> </p> <p> You can't easily compare two different functions for equality, so this test defines equality as the functions producing the same result when you <code>Run</code> them. </p> <p> Again, while the test doesn't prove anything, it demonstrates that for the five test cases, it doesn't matter if you project the state <code>s</code> in one or two steps. </p> <h3 id="584dea4732f54b749bd60d36c0b2dec0"> Haskell <a href="#584dea4732f54b749bd60d36c0b2dec0" title="permalink">#</a> </h3> <p> In Haskell, State is <a href="https://hackage.haskell.org/package/mtl/docs/Control-Monad-State.html">available in the mtl package</a>. You can implement the behaviour from <code>VowelExpander</code> like this: </p> <p> <pre><span style="color:#2b91af;">expandVowels</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(<span style="color:#2b91af;">String</span>,&nbsp;<span style="color:#2b91af;">Int</span>) expandVowels&nbsp;text&nbsp;s&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;vowels&nbsp;=&nbsp;<span style="color:#a31515;">&quot;aeiouy&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expanded&nbsp;=&nbsp;text&nbsp;&gt;&gt;=&nbsp;(\c&nbsp;-&gt;&nbsp;<span style="color:blue;">if</span>&nbsp;c&nbsp;`elem`&nbsp;vowels&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">replicate</span>&nbsp;s&nbsp;c&nbsp;<span style="color:blue;">else</span>&nbsp;[c]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newState&nbsp;=&nbsp;s&nbsp;+&nbsp;1 &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;(expanded,&nbsp;newState)</pre> </p> <p> Instead of defining an interface, you can use any function <code>s -&gt; (a, s)</code>, which you can elevate to the State functor using a function called <code>state</code>. You can then use <code>fmap</code> or <code>&lt;$&gt;</code> to map the value: </p> <p> <pre>&gt; runState (length &lt;$&gt; state (expandVowels "bar")) 2 (4,3)</pre> </p> <p> You can see a more useful example of the Haskell State functor in use in the article <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">An example of state-based testing in Haskell</a>. </p> <h3 id="eea12540db2a4c1cb33c5f005134cde4"> Conclusion <a href="#eea12540db2a4c1cb33c5f005134cde4" title="permalink">#</a> </h3> <p> A function that takes a state value as input and returns a value and a (potentially new) state value as output is a functor known as <em>State</em>. It can be used as a convenient way to express stateful computations as pure functions. </p> <p> <strong>Next:</strong> <a href="/2021/08/30/the-reader-functor">The Reader functor</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A reading of Extensibility for the Masses https://blog.ploeh.dk/2021/07/12/a-reading-of-extensibility-for-the-masses 2021-07-12T05:36:00+00:00 Mark Seemann <div id="post"> <p> <em>A paper read and translated to C#.</em> </p> <p> When I have the time (and I do make this a priority) I set aside an hour every day to study. Lately I've been using these time slots to read and reproduce the code in the 2012 paper <em>"Extensibility for the Masses. Practical Extensibility with Object Algebras"</em> by Bruno C. d. S. Oliveira and William R. Cook. As is often common with academic papers, they don't have a single, authoritative address on the internet. You can find the paper in various places. I've used <a href="https://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf">a copy hosted by University of Texas</a>, which is the institution with which William R. Cook is associated. </p> <p> While the example code in the paper is in Java, the authors claim that it translates easily to C#. I decided to give this a try, and found it to be true. </p> <h3 id="673345bb9c48466790a40e4c5e239240"> Git repository <a href="#673345bb9c48466790a40e4c5e239240" title="permalink">#</a> </h3> <p> From the beginning I created <a href="https://github.com/ploeh/ExtensibilityForMasses">a Git repository</a> with an eye to publishing it in case anyone was interested in looking over my shoulder. Not only can you see the 'final' translation, but you can also follow along with each commit. </p> <p> I committed each time I had something that seemed to work. When I struggled to understand how to interpret some of the code, I left detailed commit messages describing my doubts, and explaining why I had chosen to interpret things in a particular way. </p> <p> Along the way I also added automated tests, because I found that the paper lacked examples. Those tests represent my own interpretation of the code in the paper, and how one is supposed to use it. In total, I wrote 75 test cases. </p> <h3 id="05a8b46089be4fed9ead17648df68bef"> Help from one of the authors <a href="#05a8b46089be4fed9ead17648df68bef" title="permalink">#</a> </h3> <p> At one time I hit a snag that I couldn't readily resolve. After searching the web in vain, I posted <a href="https://stackoverflow.com/q/67818254/126014">a question on Stack Overflow</a>. After a few days, I got an answer from Bruno C. d. S. Oliveira, one of the authors of the paper! </p> <p> It turns out that some of my confusion stemmed from an otherwise inconsequential error in the paper. We shouldn't be shocked that an academic paper contains errors. One of many things I learned from reading Charles Petzold's excellent book <a href="http://amzn.to/2n9MFGh">The Annotated Turing</a> was that later researchers found several errors in Turing's 1936 paper, but none that changed the overall conclusion. So it seems to be here as well. There's at least one confirmed error (and another one that I only suspect), but it's inconsequential and easily corrected. </p> <p> It does, however, raise a point about scientific papers in general: Even if they're peer-reviewed they may contain errors. I'm <a href="/2020/05/25/wheres-the-science">much in favour of scientific knowledge, but also sceptical about some claims about computer science and software engineering</a>. </p> <h3 id="1cd1086ddbe84c83910dddad451efdbc"> Readability <a href="#1cd1086ddbe84c83910dddad451efdbc" title="permalink">#</a> </h3> <p> The paper's title claims to give extensibility to the masses, but will 'the masses' be able to read the paper? As papers go, I found this one quite readable. While other papers present their examples in <a href="https://www.haskell.org">Haskell</a>, this one uses Java. If you're comfortable with Java (or C#), you should be able to follow the code examples (or my C# translation). </p> <p> You won't entirely escape Greek letters or other <a href="/2021/06/07/abstruse-nomenclature">abstruse nomenclature</a>. This is, after all, an academic paper, so it can't be lucid all the way through. There's a section called <em>Algebraic Signatures, F-Algebras, and Church Encodings</em> that is definitely not for 'the masses'. I understand enough about <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">F-algebras</a> and <a href="/2018/05/22/church-encoding">Church encodings</a> to follow the outline of this section, but I didn't find it helpful. </p> <p> If you're interested in the overall article, but don't know what these words mean, I suggest you skim those parts and pay as much attention to the details as when <a href="https://en.wikipedia.org/wiki/Geordi_La_Forge">Geordi La Forge</a> spews <a href="https://en.wikipedia.org/wiki/Technobabble">technobabble</a>. In other words, I think you can follow the rest of the article just was well, even if <em>Church<sub>Σ</sub> = ∀A.(T<sub>1</sub> → A) × ... × (T<sub>n</sub> → A) → A</em> makes no sense to you. </p> <h3 id="c42c389e557a47848d4c66af477297ca"> Conclusion <a href="#c42c389e557a47848d4c66af477297ca" title="permalink">#</a> </h3> <p> Does the paper deliver on its promise? Yes and no. Formally, I think that it does. In the beginning, it establishes some criteria for a successful solution, and as far as I can tell, it can check off all of them. </p> <p> It's also true that the proposed solution requires only intermediary language features. Generics and inheritance, as they're available in C# and Java, is all that's required. </p> <p> On the other hand, I don't find the paper's examples compelling. Few 'mass developers' are tasked with implementing a simple expression-based language. I can't shake the feeling that most of what the paper accomplishes could be handled with tasty application of composition and the <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter pattern</a>. </p> <p> Still, I'll keep this paper in mind if I ever have to publish a reusable and extensible, type-safe software library. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="eddd9051446447b8ae4831a703c1ccbf"> <div class="comment-author"><a href="https://github.com/Joker-vD">Joker_vD</a> <a href="#eddd9051446447b8ae4831a703c1ccbf">#</a></div> <div class="comment-content"> <p> I was intrigued by the paper's abstract, but then it turned out it's just about implementing Oleg Kiselyov's final (typed, tagless) encoding in plain boring Java/C# &mdash; the most surprising thing is that it doesn't really mention Kiselyov's work which predates this paper by 3-4 years. And Kiselyov in his writings moslty used really basic Haskell features so translating it to Java/C# is pretty straightforward, I actually did with it the last Autumn, toying with the idea, so I feel this paper could very well have been a (small) blog post, really. Anyway, let's talk about the proposed solution itself. </p> <p> And the solution is actually pretty ingenious! The conventional approach to representing AST and writing interpreters (in broad sense) for it is to represent AST as a tree of objects, and interpret it by walking this tree over, doing the work in the process. The problem is that to add a new kind of node to AST you need to patch all existing intepreters, and writing an interpreter involves either manual dynamic dispatch on the node types in the interpreter's code, or trying to shift it onto the nodes itself somehow (cf. Visitor pattern). </p> <p> The proposed solution neatly side steps the whole problem of representing an AST by simply not representing it as a piece of data <b>at all</b>, it instead interprets a (would-be) AST right at the moment of the construction. And so the interpreters &mdash; instead of being written as vistors &mdash; are written as builders that build the resulting value. </p> <p> As I said, it's very ingenious but it is also, sadly, largely pointless. You see, it all largely started with the Haskell programmers trying to make ASTs more statically typed to get rid of as many runtime checks and "this case is impossible" in switches in the interpreters: you have to check that e.g. the <code>if</code>'s condition (which is usually just a plain <code>Expr</code>) must evaluate to boolean, but if you make its type some sort of <code>Expr&lt;BoolType&rt;</code>, the Haskell's typechecker won't allow you to build a malformed AST in the first place! It led to introduction of GADTs, then to extending GADTs even further, and I guess at some point some people started feeling kinda uneasy about going this close to the full-blown dependent types, and so the tagless final encoding was born: as I said in the beginning, it uses very boring and straightforward Haskell features &mdash; parametric polymorphism and typeclasses &mdash; or, as they're more widely known, generics and interfaces. But then again, as it evolved, it too started require language extensions although not as radical as in previous cases, and at some point waded into the esoteric type-level meta-programming territory. </p> <p> So here's the pointless part: the initial goal was to push type-checking of the (mini-)language being implemented onto the Haskell's typechecker, and it makes implementing small, typed mini-languages that are essentially Haskell's subset <i>very</i> easy, but... if you program in Haskell, what do you need this toy of a language for? And do its types/semantics really align that well with Haskell's? If they don't, this whole ordeal becomes very difficult: imagine a language with three variables (<code>A</code>, <code>B</code>, and <code>C</code>) that can hold either integers or booleans, constants, assignments, basic arithmetic and comparison operators, <code>if-then-else</code> statement and <code>while</code> loop. Trying to encode it in Haskell type-safely (so that variables would have consistent types after all branches/loops' ends) is pretty non-trivial, whether you use GADTs or the approach from the paper. I've seen a blogpost where this exact excercise was done, and it was done in Coq, with essential use of dependent typing. </p> <p> And trying to pull this off in a language like Java/C# with much more limited type-system is, I would argue, fundamentally misguided. Eric Lippert has summed it quite well in his "Wizards and warriors, part five": </p> <quote> We have no reason to believe that the C# type system was designed to have sufficient generality to encode the rules of Dungeons &amp; Dragons, so why are we even trying? </quote> <p> Then, of course, there is a problem that in this approach, AST does not actually exist as an object: it's represented as a function/method. If the interpreter you write needs multiple passes over AST, I am afraid you'll have to materialize your AST and AFAIK you can't really fine-tune or optimize the representation of closures in Java/C#. Of course, if you <i>don't</i> need multiple passes, then this approach is perfectly fine, and in fact, that's actually how one-pass compilers are usually structured: the parser straight up calls the code-generating hooks, and when it's done, the code-gen is done. </p> <p> And when it comes down to actual extensibility, that is, the case when a new node type is added to AST, this approach really doesn't win much compared to conventional visitors: ideally, since interfaces should be immutable, such addition means that a new visitor interface is declared (an extension of the old one) which can be implemented by inheriting from the old interpreter, or by Adapting it &mdash; just the same that would be done in the proposed approach. </p> <p> So, my conclusion: this paper tries to solve the problem of AST representation/interpretation by telling us to essentially write one-pass compilers, in the name of not writing semantic checking code for AST ourselves but instead of shifting it onto the shoulders of Java/C# type checker. No, sorry, but that's not a solution to the actual problem. </p> </div> <div class="comment-date">2021-07-14 01:21 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fractal hex flowers https://blog.ploeh.dk/2021/07/05/fractal-hex-flowers 2021-07-05T08:51:00+00:00 Mark Seemann <div id="post"> <p> <em>The code behind the cover of Code That Fits in Your Head</em> </p> <p> A book needs a cover, but how do you choose a cover illustration for a programming book? Software development tends to be abstract, so how to pick a compelling picture? </p> <p> Occasionally, a book manages to do everything right, such as the wonderful <a href="https://amzn.to/3fEB938">Mazes for Programmers</a>, which also happens to have a cover appropriate for its contents. Most publishers, however, resort to pictures of bridges, outer space, animals, or folk costumes. </p> <p> For <a href="/code-that-fits-in-your-head">my new book</a>, I wanted a cover that, like <em>Mazes for Programmers</em>, relates to its contents. Fortunately, the book contains a few concepts that easily visualise, including a concept I call <em>fractal architecture</em>. You can create some compelling drawings based on <em>hex flowers</em> nested within the petals of other hex flowers. I chose to render some of those for the book cover: </p> <p> <img src="/content/binary/ctfiyh.jpg" alt="Book cover."> </p> <p> I used <a href="https://observablehq.com">Observable</a> to host the code that renders the fractal hex flowers. This enabled me to experiment with various colour schemes and effects. Here's one example: </p> <p> <img src="/content/binary/decayed-purple-lace.png" alt="Fractal hex flower rendering example."> </p> <p> Not only did I write the program to render the figures on Observable, I turned the notebook into <a href="https://observablehq.com/@ploeh/fractal-hex-flowers">a comprehensive article</a> explaining not only how to draw the figures, but also the geometry behind it. </p> <p> The present blog post is really only meant as a placeholder. The real article is over at Observable. <a href="https://observablehq.com/@ploeh/fractal-hex-flowers">Go there to read it</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Property-based testing is not the same as partition testing https://blog.ploeh.dk/2021/06/28/property-based-testing-is-not-the-same-as-partition-testing 2021-06-28T06:45:00+00:00 Mark Seemann <div id="post"> <p> <em>Including an example of property-based testing without much partitioning.</em> </p> <p> A <a href="https://twitter.com/marick/status/1375601495916285952">tweet from Brian Marick</a> induced me to read a paper by Dick Hamlet and Ross Taylor called <em>Partition Testing Does Not Inspire Confidence</em>. In general, I find the conclusion fairly intuitive, but on the other hand hardly an argument against <a href="/property-based-testing-intro">property-based testing</a>. </p> <p> I'll later return to why I find the conclusion intuitive, but first, I'd like to address the implied connection between <a href="https://en.wikipedia.org/wiki/Equivalence_partitioning">partition testing</a> and property-based testing. I'll also show a detailed example. </p> <p> The source code used in this article is <a href="https://github.com/ploeh/FizzBuzzHaskellPropertyBased">available on GitHub</a>. </p> <h3 id="3cb59648be0044c88e5525583013c822"> Not the same <a href="#3cb59648be0044c88e5525583013c822" title="permalink">#</a> </h3> <p> The Hamlet & Taylor paper is exclusively concerned with partition testing, which makes sense, since it's from 1990. As far as I'm aware, property-based testing wasn't invented until later. </p> <p> Brian Marick extends its conclusions to property-based testing: <blockquote> <p> "I've been a grump about property-based testing because I bought into the conclusion of Hamlet&Taylor's 1990 "Partition testing does not inspire confidence"" </p> <footer><cite><a href="https://twitter.com/marick/status/1375601495916285952">Brian Marick</a></cite></footer> </blockquote> This seems to imply that property-based testing isn't effective, because (if you accept the paper's conclusions) partition testing isn't effective. </p> <p> There's certainly overlap between partition testing and property-based testing, but it's not complete. Some property-based testing isn't partition testing, or the other way around: </p> <p> <img src="/content/binary/partition-property-based-testing-venn.png" alt="Venn diagram of partition testing and property-based testing."> </p> <p> To be fair, the overlap may easily be larger than the figure implies, but you can certainly describes properties without having to partition a function's domain. </p> <p> In fact, the canonical example of property-based testing (that reversing a list twice yields the original list: <code>reverse (reverse xs) == xs</code>) does <em>not</em> rely on partitioning. It works for all finite lists. </p> <p> You may think that this is only because the case is so simple, but that's not the case. You can also <a href="/2015/01/10/diamond-kata-with-fscheck">avoid partitioning on the slightly more complex problem presented by the Diamond kata</a>. In fact, <a href="/2015/02/23/property-based-testing-without-a-property-based-testing-framework">the domain for that problem is so small that you don't need a property-based framework</a>. </p> <p> You may argue that the Diamond kata is another toy problem, but I've also <a href="/2021/02/15/when-properties-are-easier-than-examples">solved a realistic, complex business problem with property-based testing without relying on partitioning</a>. Granted, the property shown in that article doesn't sample uniformly from the entire domain of the System Under Test, but the property (there's only one) doesn't rely on partitioning. Instead, it relies on incremental tightening of preconditions to tease out the desired behaviour. </p> <p> I'll show another example next. </p> <h3 id="1c81c19841b74fe7af2726b20689291c"> FizzBuzz via partitioning <a href="#1c81c19841b74fe7af2726b20689291c" title="permalink">#</a> </h3> <p> When introducing equivalence classes and property-based testing in workshops, I sometimes use the <a href="https://codingdojo.org/kata/FizzBuzz">FizzBuzz kata</a> as an example. When I do this, I first introduce the concept of equivalence classes and then proceed to explain how instead of manually picking values from each partition, you can randomly sample from them: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``FizzBuzz.transform&nbsp;returns&nbsp;Buzz``&nbsp;(number&nbsp;:&nbsp;int)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;(number&nbsp;%&nbsp;5&nbsp;=&nbsp;0&nbsp;&amp;&amp;&nbsp;number&nbsp;%&nbsp;3&nbsp;&lt;&gt;&nbsp;0)&nbsp;==&gt;&nbsp;<span style="color:blue;">lazy</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;FizzBuzz.transform&nbsp;number &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=&nbsp;actual</pre> </p> <p> (That's <a href="https://fsharp.org">F#</a> code, but the rest of the code in this article is going to be Haskell.) </p> <p> While this gently introduces the concept of testing based on randomly sampling inputs, it relies heavily on partitioning. The above example filters inputs so that it only runs with numbers that are divisible by five, but not by three. </p> <p> As at least one workshop attendee objected, it's getting perilously close to reproducing the implementation logic in the test. It always hurts when someone directs valid criticism at you, but he was right. That's not a problem with property-based testing, though, but rather with the way I presented it. </p> <p> We can do better. </p> <h3 id="a26215089b75410da8da386a3b29b25c"> FizzBuzz with proper properties <a href="#a26215089b75410da8da386a3b29b25c" title="permalink">#</a> </h3> <p> The trick to 'true' property-based testing is identifying proper properties for the problem being solved. Granted, this can be difficult and often requires some creative thinking (which is also why I find it so enjoyable). Fortunately, certain patterns tend to recur; for example, <a href="https://fsharpforfunandprofit.com/">Scott Wlaschin</a> has a <a href="https://fsharpforfunandprofit.com/posts/property-based-testing-2/">small collection of property-based testing patterns</a>. </p> <p> As the FizzBuzz kata is described, the domain for a <code>fizzBuzz</code> function is only the numbers from one to 100. Let's be generous, however, and expand it to all integers, since it makes no practical difference. </p> <p> In <a href="https://www.haskell.org">Haskell</a>, for example, we might aim for a function with this API: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;(<span style="color:blue;">Integral</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span></pre> </p> <p> Is it possible to test-drive the correct implementation with <a href="https://en.wikipedia.org/wiki/QuickCheck">QuickCheck</a> without relying on partitioning? </p> <p> I must admit that I can't figure out how to entirely avoid partitioning, but it's possible to bootstrap the process using only a single partition. If you know of a way to entirely do without partitioning, <a href="https://github.com/ploeh/ploeh.github.com#readme">leave a comment</a>. </p> <h3 id="e2bddf57cd8247f799ef6b9bd6e7c726"> FizzBuzz <a href="#e2bddf57cd8247f799ef6b9bd6e7c726" title="permalink">#</a> </h3> <p> In order to anchor the behaviour, we have to describe how at least a single value translates to a string, for example that all multiples of both three and five translate to "FizzBuzz". It might be enough to simply state that a small number like <code>0</code> or <code>15</code> translates to <code>"FizzBuzz"</code>, but we might as well exercise that entire partition: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Divisible&nbsp;by&nbsp;both&nbsp;3&nbsp;and&nbsp;5&quot;</span>&nbsp;$&nbsp;\&nbsp;(seed&nbsp;::&nbsp;Int)&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;seed&nbsp;*&nbsp;3&nbsp;*&nbsp;5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;fizzBuzz&nbsp;i &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span>&nbsp;===&nbsp;actual</pre> </p> <p> Here I take <em>any</em> integer <code>seed</code> and use it to produce an integer <code>i</code> which is guaranteed to belong to the partition that always produces the output <code>"FizzBuzz"</code>. </p> <p> Certainly, this tests only a single partition, but as <a href="https://twitter.com/johanneslink/status/1375681159166881793">Johannes Link points out</a>, property-based testing still performs randomised testing <em>within</em> the partition. </p> <p> The simplest implementation that passes the test is this: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> fizzBuzz&nbsp;_&nbsp;=&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span></pre> </p> <p> From here, however, it's possible to describe the rest of the problem without relying on partition testing. </p> <h3 id="961b66d9d9124f279d1f080ff9cb9044"> At least one number in three consecutive values <a href="#961b66d9d9124f279d1f080ff9cb9044" title="permalink">#</a> </h3> <p> How to proceed from there long eluded me. Then it dawned on me that while it's hard to test <em>a single call</em> to the <code>fizzBuzz</code> function without relying on partitioning, you can examine the output from projecting a small range of inputs to outputs. </p> <p> What happens if we pick a random number, use it as an origin to enumerate three numbers in total (that is: two more numbers), and then call <code>fizzBuzz</code> with each of them? </p> <p> Imagine what happens if we randomly pick <em>10</em>. In that case, we're going to enumerate three numbers, starting with <em>10: 10, 11, 12</em>. What's the expected output of applying these three numbers to <code>fizzBuzz</code>? It's <em>Buzz, 11, Fizz</em>. Let's try a few more, and make a table of it: </p> <table border="1"> <thead> <tr> <th><em>i</em></th> <th><em>i+1</em></th> <th><em>i+2</em></th> </tr> </thead> <tbody> <tr> <td><em>10 → Buzz</em></td> <td><em>11 → 11</em></td> <td><em>12 → Fizz</em></td> </tr> <tr> <td><em>11 → 11</em></td> <td><em>12 → Fizz</em></td> <td><em>13 → 13</em></td> </tr> <tr> <td><em>12 → Fizz</em></td> <td><em>13 → 13</em></td> <td><em>14 → 14</em></td> </tr> <tr> <td><em>13 → 13</em></td> <td><em>14 → 14</em></td> <td><em>15 → FizzBuzz</em></td> </tr> <tr> <td><em>14 → 14</em></td> <td><em>15 → FizzBuzz</em></td> <td><em>16 → 16</em></td> </tr> </tbody> </table> <p> Do you notice a pattern? </p> <p> There's more than a single pattern, but one is that there's <em>always at least one number</em> among the three results. Even if you have both a <em>Fizz</em> and a <em>Buzz</em>, as is the case with <em>10, 11, 12</em>, at least one of the results (<em>11</em>) remains a number. Think about it some more to convince yourself that this should always be the case for three consecutive numbers. </p> <p> That's a <em>property</em> of <code>fizzBuzz</code>, and it holds universally (also for negative integers). </p> <p> You can turn it into a QuickCheck property like this: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;At&nbsp;least&nbsp;one&nbsp;number&nbsp;in&nbsp;3&nbsp;consecutive&nbsp;values&quot;</span>&nbsp;$&nbsp;\&nbsp;(i&nbsp;::&nbsp;Int)&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;range&nbsp;=&nbsp;[i..i+2] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;fizzBuzz&nbsp;&lt;$&gt;&nbsp;range &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;counterexample &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">show</span>&nbsp;range&nbsp;++&nbsp;<span style="color:#a31515;">&quot;-&gt;&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;actual)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">any</span>&nbsp;(\x&nbsp;-&gt;&nbsp;isJust&nbsp;(readMaybe&nbsp;x&nbsp;::&nbsp;Maybe&nbsp;Int))&nbsp;actual</pre> </p> <p> This test doesn't rely on input partitioning. It works for <em>all</em> integers. </p> <p> In this test I used QuickCheck's <code>counterexample</code> function to provide a helpful message in case of failure. Running the test suite against the above version of <code>fizzBuzz</code> yields a failure like this: </p> <p><pre>At least one number in 3 consecutive values: [<span style="color:red;">Failed</span>] *** Failed! Falsified (after 1 test): 0 [0,1,2]-&gt;["FizzBuzz","FizzBuzz","FizzBuzz"] (used seed -6204080625786338123)</pre> </p> <p> Here we see that the sequence <code>[0,1,2]</code> produces the output <code>["FizzBuzz","FizzBuzz","FizzBuzz"]</code>, which is not only wrong, but is specifically incorrect in the sense that none of the values can be parsed as an integer. </p> <p> Given the current implementation, that's hardly surprising. </p> <p> Using <a href="/2019/10/07/devils-advocate">the Devil's Advocate</a> technique, I chose to pass both tests with this implementation: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> fizzBuzz&nbsp;i&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;i&nbsp;`mod`&nbsp;15&nbsp;==&nbsp;0&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span>&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:#a31515;">&quot;2112&quot;</span></pre> </p> <p> The property I just added doesn't check whether the number is one of the input numbers, so the implementation can get away with returning the hard-coded string <code>"2112"</code>. </p> <h3 id="00d606085bea441abecad26711655016"> At least one Fizz in three consecutive values <a href="#00d606085bea441abecad26711655016" title="permalink">#</a> </h3> <p> Take a look at the above table. Do you notice any other patterns? </p> <p> Of each set of three results, there's always a string that <em>starts with Fizz</em>. Sometimes, as we see with the input <em>15</em>, the output is <em>FizzBuzz</em>, so it's not always just <em>Fizz</em>, but there's always a string that starts with <em>Fizz</em>. </p> <p> This is another universal property of the <code>fizzBuzz</code> function, which we can express as a test: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;At&nbsp;least&nbsp;one&nbsp;Fizz&nbsp;in&nbsp;3&nbsp;consecutive&nbsp;values&quot;</span>&nbsp;$&nbsp;\&nbsp;(i&nbsp;::&nbsp;Int)&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;range&nbsp;=&nbsp;[i..i+2] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;fizzBuzz&nbsp;&lt;$&gt;&nbsp;range &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;counterexample &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">show</span>&nbsp;range&nbsp;++&nbsp;<span style="color:#a31515;">&quot;-&gt;&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;actual)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">any</span>&nbsp;(<span style="color:#a31515;">&quot;Fizz&quot;</span>&nbsp;`isPrefixOf`)&nbsp;actual</pre> </p> <p> Again, no partitioning is required to express this property. The arbitrary parameter <code>i</code> is unconstrained. </p> <p> To pass all tests, I implemented <code>fizzBuzz</code> like this: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> fizzBuzz&nbsp;i&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;i&nbsp;`mod`&nbsp;3&nbsp;==&nbsp;0&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span>&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:#a31515;">&quot;2112&quot;</span></pre> </p> <p> It doesn't look as though much changed, but notice that the modulo check changed from <em>modulo 15</em> to <em>modulo 3</em>. While incorrect, it passes all tests. </p> <h3 id="f08b2d56249b4390808ee727273825a9"> Only one Buzz in five consecutive values <a href="#f08b2d56249b4390808ee727273825a9" title="permalink">#</a> </h3> <p> Using the same reasoning as above, another property emerges. If, instead of looking at sequences of three input arguments, you create five values, only one of them should result in a <em>Buzz</em> result; e.g. <em>8, 9, 10, 11, 12</em> should result in <em>8, Fizz, Buzz, 11, Fizz</em>. Sometimes, however, the <em>Buzz</em> value is <em>FizzBuzz</em>, for example when the origin is <em>11: 11, 12, 13, 14, 15</em> should produce <em>11, Fizz, 13, 14, FizzBuzz</em>. </p> <p> Like the above property, there's only one <em>Buzz</em>, but sometimes it's part of a compound word. What's clear, though, is that there should be only one result that ends with <em>Buzz</em>. </p> <p> Not only is the idea similar to the one above, so is the test: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Only&nbsp;one&nbsp;Buzz&nbsp;in&nbsp;5&nbsp;consecutive&nbsp;values&quot;</span>&nbsp;$&nbsp;\&nbsp;(i&nbsp;::&nbsp;Int)&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;range&nbsp;=&nbsp;[i..i+4] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;fizzBuzz&nbsp;&lt;$&gt;&nbsp;range &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;counterexample &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">show</span>&nbsp;range&nbsp;++&nbsp;<span style="color:#a31515;">&quot;-&gt;&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;actual)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;==&nbsp;<span style="color:blue;">length</span>&nbsp;(<span style="color:blue;">filter</span>&nbsp;(<span style="color:#a31515;">&quot;Buzz&quot;</span>&nbsp;`isSuffixOf`)&nbsp;actual)</pre> </p> <p> Again, no partitioning is required to express this property. </p> <p> This version of <code>fizzBuzz</code> passes all tests: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;5&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;3&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span> fizzBuzz&nbsp;_&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2112&quot;</span></pre> </p> <p> We're not quite there yet, but we're getting closer. </p> <h3 id="13c4e94f5b38409fb8bf99ac268f40cf"> At least one literal Buzz in ten consecutive values <a href="#13c4e94f5b38409fb8bf99ac268f40cf" title="permalink">#</a> </h3> <p> What's wrong with the above implementation? </p> <p> It never returns <em>Buzz</em>. How can we express a property that forces it to do that? </p> <p> We can keep going in the same vein. We know that if we sample a sufficiently large sequence of numbers, it might produce a <em>FizzBuzz</em> value, but even if it does, there's going to be a <em>Buzz</em> value five positions before and after. For example, if the input sequence contains <em>30</em> (producing <em>FizzBuzz</em>) then both <em>25</em> and <em>35</em> should produce <em>Buzz</em>. </p> <p> How big a range should we sample to be certain that there's at least one <em>Buzz</em>? </p> <p> If it's not immediately clear, try setting up a table similar to the one above: </p> <table border="1"> <thead> <tr> <th><em>i</em></th> <th><em>i+1</em></th> <th><em>i+2</em></th> <th><em>i+3</em></th> <th><em>i+4</em></th> <th><em>i+5</em></th> <th><em>i+6</em></th> <th><em>i+7</em></th> <th><em>i+8</em></th> <th><em>i+9</em></th> </tr> </thead> <tbody> <tr> <td><em>10 →<br>Buzz</em></td> <td><em>11 →<br>11</em></td> <td><em>12 →<br>Fizz</em></td> <td><em>13 →<br>13</em></td> <td><em>14 →<br>14</em></td> <td><em>15 →<br>FizzBuzz</em></td> <td><em>16 →<br>16</em></td> <td><em>17 →<br>17</em></td> <td><em>18 →<br>Fizz</em></td> <td><em>19 →<br>19</em></td> </tr> <tr> <td><em>11 →<br>11</em></td> <td><em>12 →<br>Fizz</em></td> <td><em>13 →<br>13</em></td> <td><em>14 →<br>14</em></td> <td><em>15 →<br>FizzBuzz</em></td> <td><em>16 →<br>16</em></td> <td><em>17 →<br>17</em></td> <td><em>18 →<br>Fizz</em></td> <td><em>19 →<br>19</em></td> <td><em>20 →<br>Buzz</em></td> </tr> <tr> <td><em>17 →<br>17</em></td> <td><em>18 →<br>Fizz</em></td> <td><em>19 →<br>19</em></td> <td><em>20 →<br>Buzz</em></td> <td><em>21 →<br>Fizz</em></td> <td><em>22 →<br>22</em></td> <td><em>23 →<br>23</em></td> <td><em>24 →<br>Fizz</em></td> <td><em>25 →<br>Buzz</em></td> <td><em>26 →<br>26</em></td> </tr> </tbody> </table> <p> Notice that as one <em>Buzz</em> drops off to the left, a new one appears to the right. Additionally, there may be more than one literal <em>Buzz</em>, but there's always at least one (that is, one that's exactly <em>Buzz</em>, and not just ending in <em>Buzz</em>). </p> <p> That's another universal property: for any consecutive sequence of numbers of length ten, there's at least one exact <em>Buzz</em>. Here's how to express that as a QuickCheck property: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;At&nbsp;least&nbsp;one&nbsp;literal&nbsp;Buzz&nbsp;in&nbsp;10&nbsp;values&quot;</span>&nbsp;$&nbsp;\&nbsp;(i&nbsp;::&nbsp;Int)&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;range&nbsp;=&nbsp;[i..i+9] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;fizzBuzz&nbsp;&lt;$&gt;&nbsp;range &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;counterexample &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">show</span>&nbsp;range&nbsp;++&nbsp;<span style="color:#a31515;">&quot;-&gt;&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;actual)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">elem</span>&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span>&nbsp;actual</pre> </p> <p> Again, no partitioning is required to express this property. </p> <p> This version of <code>fizzBuzz</code> passes all tests: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;15&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;&nbsp;3&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;&nbsp;5&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span> fizzBuzz&nbsp;_&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2112&quot;</span></pre> </p> <p> What's left? Only that the number is still hard-coded. </p> <h3 id="a8d4a2c885c04aba85b33cfbb592dca5"> Numbers round-trip <a href="#a8d4a2c885c04aba85b33cfbb592dca5" title="permalink">#</a> </h3> <p> How to get rid of the hard-coded number? </p> <p> From one of the above properties, we know that if we pick an arbitrary consecutive sequence of three numbers, at least one of the results will be a string representation of the input number. </p> <p> It's not guaranteed to be the origin, though. If the origin is, say, <em>3</em>, the input sequence is <em>3, 4, 5</em>, which should yield the resulting sequence <em>Fizz, 4, Buzz</em>. </p> <p> Since we don't know which number(s) will remain, how can we check that it translates correctly? We can use a variation of a common property-based testing pattern - the one that Scott Wlaschin calls <em>There and back again</em>. </p> <p> We can take any sequence of three outputs and try to parse them back to integers. All successfully parsed numbers must belong to the input sequence. </p> <p> That's another universal property. Here's how to express that as a QuickCheck property: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Numbers&nbsp;round-trip&quot;</span>&nbsp;$&nbsp;\&nbsp;(i&nbsp;::&nbsp;Int)&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;range&nbsp;=&nbsp;[i..i+2] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;fizzBuzz&nbsp;&lt;$&gt;&nbsp;range &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numbers&nbsp;=&nbsp;catMaybes&nbsp;$&nbsp;readMaybe&nbsp;&lt;$&gt;&nbsp;actual &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;counterexample &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">show</span>&nbsp;range&nbsp;++&nbsp;<span style="color:#a31515;">&quot;-&gt;&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;actual)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">all</span>&nbsp;(`elem`&nbsp;range)&nbsp;numbers</pre> </p> <p> The parsed <code>numbers</code> may contain one or two elements, but in both cases, all of them must be an element of <code>range</code>. </p> <p> Again, no partitioning is required to express this property. </p> <p> This version of <code>fizzBuzz</code> passes all tests: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;(<span style="color:blue;">Integral</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;15&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;&nbsp;3&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;&nbsp;5&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span> fizzBuzz&nbsp;i&nbsp;=&nbsp;<span style="color:blue;">show</span>&nbsp;i</pre> </p> <p> This looks good. Let's call it a day. </p> <p> Not so fast, though. </p> <h3 id="03a0a3395a0c40deb33404f342985102"> Redundant property? <a href="#03a0a3395a0c40deb33404f342985102" title="permalink">#</a> </h3> <p> With the new round-trip property, isn't the property titled <em>At least one number in 3 consecutive values</em> redundant? </p> <p> You might think so, but it's not. What happens if we remove it? </p> <p> If you remove the <em>At least one number in 3 consecutive values</em> property, the Devil's Advocate can corrupt <code>fizzBuzz</code> like this: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;(<span style="color:blue;">Integral</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;15&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;&nbsp;3&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;&nbsp;5&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span> fizzBuzz&nbsp;_&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Pop&quot;</span></pre> </p> <p> This passes all tests if you remove <em>At least one number in 3 consecutive values</em>. Why doesn't the <em>Numbers round-trip</em> property fail? </p> <p> It doesn't fail because with the above implementation of <code>fizzBuzz</code> its <em>numbers</em> list is always empty. This property doesn't require <em>numbers</em> to be non-empty. It doesn't have to, because that's the job of the <em>At least one number in 3 consecutive values</em> property. Thus, that property isn't redundant. Leave it in. </p> <h3 id="5efa5b0c3ed9457fb9914e94c4e9690c"> Intuition behind the paper <a href="#5efa5b0c3ed9457fb9914e94c4e9690c" title="permalink">#</a> </h3> <p> What about the results from the Hamlet & Taylor paper? Are the conclusions in the paper wrong? </p> <p> They may be, but that's not my take. Rather, the way I understand the paper, it says that partition testing isn't much more efficient at detecting errors than pure random sampling. </p> <p> I've been using the rather schizophrenic version of the Devil's Advocate technique (the one that <a href="/outside-in-tdd">I call Gollum style</a>) for so long that this conclusion rings true for me. </p> <p> Consider a truly adversarial <a href="https://fsharpforfunandprofit.com/posts/property-based-testing/">developer from Hell</a>. He or she could subvert <code>fizzBuzz</code> like this: </p> <p> <pre><span style="color:#2b91af;">fizzBuzz</span>&nbsp;::&nbsp;(<span style="color:blue;">Integral</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> fizzBuzz&nbsp;18641&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Pwnd&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;15&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;&nbsp;3&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span> fizzBuzz&nbsp;i&nbsp;|&nbsp;i&nbsp;`mod`&nbsp;&nbsp;5&nbsp;==&nbsp;0&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span> fizzBuzz&nbsp;i&nbsp;=&nbsp;<span style="color:blue;">show</span>&nbsp;i</pre> </p> <p> The test suite is <em>very unlikely</em> to detect the error - even if you ask it to run each property a million times: </p> <p> <pre>$ stack test --ta "-a 1000000" FizzBuzzHaskellPropertyBased&gt; test (suite: FizzBuzz-test, args: -a 1000000) Divisible by both 3 and 5: [<span style="color:green;">OK, passed 1000000 tests</span>] At least one number in 3 consecutive values: [<span style="color:green;">OK, passed 1000000 tests</span>] At least one Fizz in 3 consecutive values: [<span style="color:green;">OK, passed 1000000 tests</span>] Only one Buzz in 5 consecutive values: [<span style="color:green;">OK, passed 1000000 tests</span>] At least one literal Buzz in 10 values: [<span style="color:green;">OK, passed 1000000 tests</span>] Numbers round-trip: [<span style="color:green;">OK, passed 1000000 tests</span>] Properties Total Passed <span style="color:green;">6</span> <span style="color:green;">6</span> Failed 0 0 Total <span style="color:green;">6</span> <span style="color:green;">6</span> FizzBuzzHaskellPropertyBased&gt; Test suite FizzBuzz-test passed</pre> </p> <p> How many times should we run these properties before we'd expect the <em>At least one number in 3 consecutive values</em> property to detect the error? </p> <p> In Haskell, <code>Int</code> is an <a href="https://hackage.haskell.org/package/base/docs/Data-Int.html">integer type with at least the range [-2^29 .. 2^29-1]</a> - that is, from -536,870,912 to 536,870,911, for a total range of 1,073,741,823 numbers. </p> <p> In order to detect the error, the <em>At least one number in 3 consecutive values</em> property needs to hit <em>18,641</em>, which it can only do if QuickCheck supplies an <code>i</code> value of <em>18,639</em>, <em>18,640</em>, or <em><em>18,641</em></em>. That's three values out of 1,073,741,823. </p> <p> If we assume a uniform distribution, the chance of detecting the error is <em>3 / 1,073,741,823</em>, or approximately one in 333 million. </p> <p> Neither property-based testing nor randomised testing is likely to detect this kind of error. That's basically the intuition that makes sense to me when reading the Hamlet & Taylor paper. If you don't know where to look, partition testing isn't going to help you detect errors like the above. </p> <p> I can live with that. After all, the value I get out of property-based testing is as a variation on test-driven development, rather than only quality assurance. It <a href="/2021/02/15/when-properties-are-easier-than-examples">enables me to incrementally flesh out a problem in a way that example-based testing sometimes can't</a>. </p> <h3 id="8ada6d6c1deb4a39a71876b49e5b8278"> Conclusion <a href="#8ada6d6c1deb4a39a71876b49e5b8278" title="permalink">#</a> </h3> <p> There's a big overlap between partition testing and property-based testing. Often, identifying equivalence classes is the first step to expressing a property. A conspicuous example can be seen in my article series <a href="/2016/02/10/types-properties-software">Types + Properties = Software</a>, which shows a detailed walk-through of the <a href="https://codingdojo.org/kata/Tennis/">Tennis kata</a> done with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a>. For a hybrid approach, see <a href="/2016/06/28/roman-numerals-via-property-based-tdd">Roman numerals via property-based TDD</a>. </p> <p> In my experience, it's much easier to partition a domain into equivalence classes than it is to describe universal properties of a system. Thus, many properties I write tend to be a kind of partition testing. On the other hand, it's more satisfying when you can express universal properties. I'm not sure that it's always possible, but I find that when it is, it better decouples the tests from implementation details. </p> <p> Based on the FizzBuzz example shown here, you may find it unappealing that there's more test code than 'production code'. Clearly, for a problem like FizzBuzz, this is unwarranted. That, however, wasn't the point with the example. The point was to show an easily digestible example of universal properties. For a more realistic example, I'll refer you to <a href="/2021/02/15/when-properties-are-easier-than-examples">the scheduling problem I also linked to earlier</a>. While the production code ultimately turned out to be compact, it's far from trivial. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Agile pull requests https://blog.ploeh.dk/2021/06/21/agile-pull-requests 2021-06-21T05:44:00+00:00 Mark Seemann <div id="post"> <p> <em>If it hurts, do it more often.</em> </p> <p> The agile software development movement has been instrumental in <a href="https://agilemanifesto.org">uncovering better ways of developing software</a>. Under that umbrella term you find such seminal ideas as test-driven development, continuous delivery, responding to change, and so on. Yet, as we're entering the third decade of agile software development, most organisations still struggle to take in the ethos and adopt its practices. </p> <p> Change is hard. A typical reaction from a development organisation would be: </p> <p> <em>"We tried it, but it doesn't work for us."</em> </p> <p> The usual agile response to such foot-draggery is: <a href="https://martinfowler.com/bliki/FrequencyReducesDifficulty.html">if it hurts, do it more often</a>. </p> <p> I'd like to suggest exactly that remedy for a perceived problem that many agile proponents seem to believe exist. </p> <h3 id="5f207852a69a424fb5ad9625c0e5c1c4"> Pull request problems and solutions <a href="#5f207852a69a424fb5ad9625c0e5c1c4" title="permalink">#</a> </h3> <p> I like collaborating with other developers via pull requests, but I also admit that my personality may influence that preference. I like deep contemplation; I like doing things on my own time, to my own schedule; I like having the freedom to self-organise. Yes, I fit <a href="http://amzn.to/1FWUzrM">the description of an introvert</a>. Pull requests do enable contemplation, they do let me self-organise. </p> <p> Yet, I'm quite aware that there are plenty of problems with pull requests. First, many pull requests are poorly done. They are too big, bundle together unrelated things and introduce noise. I wrote an article titled <a href="/2015/01/15/10-tips-for-better-pull-requests">10 tips for better Pull Requests</a> to help developers create better pull requests. With a little practice, you can write small, focused pull requests. </p> <p> If it hurts, do it more often. </p> <p> Another problem with pull requests is in the review process. The most common criticism of the pull request process with its review phase is that it takes too long. I discuss this problem, and a remedy, in my coming book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>: <blockquote> <p> "The problem with typical approaches is illustrated by [the figure below]. A developer submits a piece of work for review. Then much time passes before the review takes place. </p> <p> <img src="/content/binary/timeline-with-long-wait-time.jpg" alt="Timeline with a long wait time between a feature is submitted for review and the review actually takes place."> </p> <p> "[Figure caption:] How not to do code reviews: let much time pass between completion of a feature and the review. (The smaller boxes to the right of the review indicates improvements based on the initial review, and a subsequent review of the improvements.) </p> <p> "[The figure below] illustrates an obvious solution to the problem. Reduce the wait time. Make code reviews part of the daily rhythm of the organisation. </p> <p> <img src="/content/binary/timeline-with-minimal-wait-time.jpg" alt="Timeline with short wait time between a feature is submitted for and the review actually takes place."> </p> <p> "[Figure caption:] Reduce the wait time between feature completion and code review. A review will typically spark some improvements, and a smaller review of those improvements. These activities are indicated by the smaller boxes to the right of the review. </p> <p> "Most people already have a routine that they follow. You should make code reviews part of that routine. You can do that on an individual level, or you can structure your team around a daily rhythm. Many teams already have a daily stand-up. Such a regularly occurring event creates an anchor around which the day revolves. Typically, lunchtime is another natural break in work. </p> <p> "Consider, for example, setting aside half an hour each morning, as well as half an hour after lunch, for reviews. </p> <p> "Keep in mind that you should make only small sets of changes. Sets that represent less than half a day's work. If you do that, and all team members review those small changes twice a day, the maximum wait time will be around four hours." </p> </blockquote> I've tried this in a small organisation, and it <em>can</em> work. I'm not claiming that it's easy, but it's hardly impossible. </p> <p> If it hurts, do it more often. </p> <p> An underlying problem is that people often feel that they don't have the time to review their colleagues' code. This is a real problem. If you are being measured (formally or informally) on your 'individual contribution', then anything you do to help your team looks like a waste of time. This is, in my opinion, an organisational problem, rather than a problem with doing reviews. </p> <p> It's also a problem that pair programming <em>doesn't</em> solve. </p> <h3 id="0b6cf5764f084c22adb075e197249e3d"> Pull requests versus pair programming <a href="#0b6cf5764f084c22adb075e197249e3d" title="permalink">#</a> </h3> <p> You <em>can</em> make the pull request process work, but should you? Isn't pair (or ensemble) programming better? </p> <p> Pair programming can also be effective. I discuss that too in <a href="/code-that-fits-in-your-head">my new book</a>. What works best, I believe, is a question of trade-offs. What's more important to you? <a href="/2020/03/16/conways-law-latency-versus-throughput">Latency or throughput</a>? </p> <p> In other words, while I have a personality-based preference for the contemplative, asynchronous pull request process, I readily admit that pair or ensemble programming may be better in many situations. </p> <p> I suspect, however, that many proponents of pair programming are as driven by their personality-based preference as I am, but that since they might be extroverts, they favour close personal interaction over contemplation. </p> <p> In any case, use what works for you, but be wary of unequivocal claims that one way is clearly better than the other. We have <a href="/2020/05/25/wheres-the-science">scant scientific knowledge about software development</a>. Most of what I write, and what industry luminaries write, is based on <a href="https://martinfowler.com/bliki/AnecdotalEvidence.html">anecdotal evidence</a>. This also applies to this discussion of pull requests versus pair programming. </p> <h3 id="3bfdb00387e64164a634722325f9710e"> Conclusion <a href="#3bfdb00387e64164a634722325f9710e" title="permalink">#</a> </h3> <p> I have anecdotal evidence that pull requests can work in an 'agile' setting. One team had implemented continuous deployment and used pull requests because more than half the team members were working from remote (this was before the COVID-19 pandemic). Pull requests were small and reviews could typically be done in five-ten minutes. Knowing this, reviews were prompt and frequent. Turnaround-time was good. </p> <p> I also have anecdotal evidence that ensemble programming works well. To me, it solves a completely different problem, but I've used it to great effect for knowledge transfer. </p> <p> Programmers more extrovert than me report anecdotal evidence that pair programming is best, and I accept that this is true - for them. I do not, however, accept it as a universal truth. Neither do I claim that my personal preference for pull request is incontrovertibly best. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b91ee20d71f840b9901d1150ba7e9fc0"> <div class="comment-author">Timothée <a href="#b91ee20d71f840b9901d1150ba7e9fc0">#</a></div> <div class="comment-content"> <p> Hi. Thanks for this insight. And thank you for the emphasize about 'anecdotal' vs 'scientific proof'. </p> <p> About this topic, do you have any information regarding <a href="http://www.knosof.co.uk/ESEUR/">Evidence-based Software Engineering (free ebook)</a> ? If so, is it worth reading ? (Yep, I have total confidence about your knowledge and your judgment) </p> </div> <div class="comment-date">2021-06-21 20:36 UTC</div> </div> <div class="comment" id="89eeaad6eded422b859e901e1157a589"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#89eeaad6eded422b859e901e1157a589">#</a></div> <div class="comment-content"> <p> Timothée, thank you for writing. I haven't read or heard about <em>Evidence-based Software Engineering</em>. After having read both <a href="http://bit.ly/leprechauns-of-software-engineering">The Leprechauns of Software Engineering</a> (<a href="https://www.goodreads.com/review/show/1956107880">wonderful book</a>) and <a href="https://amzn.to/2OBNBoY">Making Software: What Really Works, and Why We Believe It</a> (<a href="https://www.goodreads.com/review/show/3718825945">not so much</a>), I don't feel like reading another one. </p> </div> <div class="comment-date">2021-06-23 5:23 UTC</div> </div> <div class="comment" id="51b23b2d7a974445a5c80fee82407a93"> <div class="comment-author">Gonzalo Waszczuk <a href="#51b23b2d7a974445a5c80fee82407a93">#</a></div> <div class="comment-content"> <p> You mention the adage "If it hurts, do it more often", and I 100% agree. But couldn't it also apply to those personality traits you mention? If you like doing things in your own time, to your own schedule, having the freedom to self-organize, then it means that doing the opposite "hurts", as an introvert. Couldn't that mean that it would be a good idea to actually try it out, and "do it more often"? I am an introvert too, and the urge to do things on my own and in peace is strong. But over the past years I've realized that perhaps the #1 most important aspect in our work is communication. I try to go against my introvert nature and talk to others, and be involved in face-to-face instances as much as I can. </p> <p> In regards to code reviews, I found that a face-to-face code review works well, and is a sensible middle point between pair programming and pull-request-based code review. You get the benefits of pair programming where you can transfer knowledge to someone else; have a face to face discussion on design decisions; it's easy to have a back and forth of ideas; it's easier to develop a shared coding standard; etc. On the other hand it's easier to apply this to every feature/development since it takes "less time" from developers than pair programming (which could be harder to apply or convince management to do, since it theoretically halves developer throughput). You can also forego the back and forths that would be done via comments in a pull request, having them occur in the moment, instead of over a span of a few days/hours. The author can answer questions and doubts from the reviewer much more quickly; the author can provide context and a "story" of the feature so the reviewer has it easier to review it. I found that is provides a lot of benefits. However, I must admit I have had more experience with this face-to-face style of code review than the pull-request style of code review. What do you think? </p> </div> <div class="comment-date">2021-06-23 14:43 UTC</div> </div> <div class="comment" id="d13706d1689c405fbf9c0dc579a22237"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d13706d1689c405fbf9c0dc579a22237">#</a></div> <div class="comment-content"> <p> Gonzalo, thank you for writing. You're right, it goes both ways. For what it's worth, I've done quite a bit of ensemble programming the last few years, and I find it quite effective for knowledge transfer. </p> <p> In general, there's definitely a case to be made for face-to-face communication. Earlier in my career, once or twice I've fallen into the trap of maintaining a written discussion for weeks. Then, when someone finally called a meeting, we could amiably resolve the issues within an hour. </p> <p> What concerns me about face-to-face code reviews, however, is the following: When you, as a reviewer, encounter something that you don't understand, you can immediately ask about it. What happens when you receive an answer? Do you then accept the answer and move on? </p> <p> If so, haven't you collectively failed to address an underlying problem with the code? If there's something you don't understand, and you have to ask the original programmer, what happens if that colleague is no longer around? Or what happens when that team member has also forgotten the answer? </p> <p> The code is the only artefact that contains the truth about how the software is defined. The code <em>is</em> the specification. If the code is difficult to understand, aren't you relying on collective tacit knowledge? </p> <p> That's my concern, but it may be misplaced. I haven't worked in a team that does code reviews as you describe, so I have no experience with it. </p> </div> <div class="comment-date">2021-06-26 4:18 UTC</div> </div> <div class="comment" id="d2ce7f6191214e97a8f1ed70063f4d28"> <div class="comment-author">Gonzalo Waszczuk <a href="#d2ce7f6191214e97a8f1ed70063f4d28">#</a></div> <div class="comment-content"> <p>Hi Mark. If, in the code review, I find something I don't understand, the author responds, and I agree with his response, how is it any different than making a comment in the pull request, the author responding, and me taking that comment/change request back?</p> <p>If he responds and I still find it confusing, or I still believe it should be changed, then I would ask him to change it, and the feature should not be merged until he makes those changes. I don't see what could be different from the pull-request based approach here</p> </div> <div class="comment-date">2021-07-01 14:41 UTC</div> </div> <div class="comment" id="7c5640fbc0a8418799e4b7a6266a01b2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7c5640fbc0a8418799e4b7a6266a01b2">#</a></div> <div class="comment-content"> <p> Gonzalo, thank you for writing. I suppose you can make it work with an oral review as well, but doesn't it require more discipline to stick to protocol? </p> <p> As a reviewer, whether or not <em>I</em>, personally, understand the proposed code is a local concern. The broader responsibility is to ensure that the code, itself, is understandable. <a href="https://en.wikipedia.org/wiki/Immanuel_Kant">Kant's</a> <a href="https://en.wikipedia.org/wiki/Categorical_imperative">categorical imperative</a> comes to mind: <blockquote> <p> "Act only according to that maxim whereby you can, at the same time, will that it should become a universal law." </p> <footer><cite><a href="https://www.goodreads.com/review/show/1892459700">Immanuel Kant</a></cite></footer> </blockquote> A code review should be conducted in such a manner that the implied protocol can be raised to a universal rule to the benefit of all team members. As a team member, I'd like the code review process to benefit <em>me</em>, even when I'm taking no part in it. </p> <p> Assume that I'm not the person doing the review. Instead, assume that another team member performs the review. If she runs into something she doesn't understand, it doesn't help <em>me</em> that she receives an answer that satisfies her. If I have to maintain the code, I'm not aware of the exchange that took place during the review. </p> <p> If there's an issue with the proposed code, it's a symptom. You can relieve the symptom by answering an immediate query, or you can address the underlying problem. I prefer the latter. </p> <p> When doing a written pull request review, most online services (GitHub, Azure DevOps) keep track of issues and require you to actively mark ongoing discussions as resolved. When I perform a review, I usually don't consider an <em>answer</em> as a resolution to the issue. An answer doesn't change the code, which means that the issue remains for the next reader to struggle with. </p> <p> Instead, I will request that the contributor amends the proposed code to address the problem. This may include refactoring, renaming a method, or just adding a comment to the code. In its most basic form, if I had a question, other team members may have the same question. If the contributor can satisfactorily answer the question, then the least he or she can do is to add the answer as a comment to the code base so that it's readily available to all readers of it. </p> <p> This turns <a href="https://en.wikipedia.org/wiki/Tacit_knowledge">tacit knowledge</a> into explicit knowledge. </p> <p> In my new book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> I propose a <em>hierarchy of communication:</em> <ol> <li>Guide the reader by giving APIs distinct types.</li> <li>Guide the reader by giving methods helpful names.</li> <li>Guide the reader by writing good comments.</li> <li>Guide the reader by providing illustrative examples as automated tests.</li> <li>Guide the reader by writing helpful commit messages in Git.</li> <li>Guide the reader by writing good documentation</li> </ol> I admit that I, like everyone else, am biased by my experience. The above suggested heuristic arose in a context. Most development organisations I've worked with has a major problem with tacit knowledge. I'm biased toward combating that problem by encouraging team members to capture knowledge in writing, and put it where it's discoverable. </p> <p> If you don't have a problem with tacit knowledge, I suppose that most of the above doesn't apply. </p> <p> What concerns me about an oral code review is that knowledge remains tacit. I suppose that with a sufficient rigorous review protocol, you could still address that problem. You could keep a log of the questions you ask, so that even if the reviewer receives a satisfactory answer, the log still states that the question was asked. The log indicates that there are unresolved issues with the proposed code. After the review, the contributor would have to take the log and address the questions by updating the pull request. </p> <p> I suppose I'm not convinced that most people have the discipline to follow such a protocol, which is why I favour the nudging provided by review tools like those offered by GitHub and Azure DevOps. </p> <p> Perhaps I'm painting myself into a corner here. Perhaps your concerns are completely different. Are you addressing a different problem? </p> </div> <div class="comment-date">2021-07-02 6:35 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. New book: Code That Fits in Your Head https://blog.ploeh.dk/2021/06/14/new-book-code-that-fits-in-your-head 2021-06-14T11:00:00+00:00 Mark Seemann <div id="post"> <p> <em>The expanded universe.</em> </p> <p> It gives me great pleasure to announce that my new book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> will be out in September 2021. The subtitle is <em>Heuristics for Software Engineering</em>. </p> <p> <img src="/content/binary/ctfiyh.jpg" alt="Book cover."> </p> <p> Many regular readers have been expecting me to write a book about functional programming, and while that may also some day happen, this book is neither about object-oriented nor functional programming per se. Rather, it takes a step back and looks at various software development techniques and practices that I've found myself teaching for years. It covers both coding, troubleshooting, software design, team work, refactoring, and architecture. </p> <p> The target audience is all the hard-working <a href="/2012/12/18/RangersandZookeepers">enterprise developers</a> in our industry. I estimate that there's great overlap with the general readership of this blog. In other words, if you find this blog useful, I hope that you'll also find the book useful. </p> <p> As the title suggests, the theme is working effectively with code in a way that acknowledges the limitations of the human brain. This is a theme I've already explored in my <a href="https://cleancoders.com/episode/humane-code-real-episode-1">Humane Code</a> video, but in the book I expand the scope. </p> <h3 id="b73fe8b865384190a031e05915227d9a"> Expanded universe <a href="#b73fe8b865384190a031e05915227d9a" title="permalink">#</a> </h3> <p> I've structured the book around a realistic sample application. You'll see how to bootstrap a code base, but also how to work effectively with existing code. Along with the book, you'll get access to a complete Git repository with more than 500 commits and more than 6,000 lines of lovingly crafted code. </p> <p> While I was developing the sample code, I solved many interesting problems. The best and most universal examples I used in the book, but many didn't make the cut. The book aims broadly at programmers comfortable with a C-based programming language: Java, C#, JavaScript, C++, and so on. Some of the problems I solved along the way were specific to .NET, so I found them a poor fit for the book. I didn't want these great lessons to go to waste, so instead I've been blogging about them. </p> <p> These are the articles based on the code base from the book: <ul> <li><a href="/2020/07/20/closing-database-connections-during-test-teardown">Closing database connections during test teardown</a></li> <li><a href="/2020/08/03/using-the-nameof-c-keyword-with-aspnet-3-iurlhelper">Using the nameof C# keyword with ASP.NET 3 IUrlHelper</a></li> <li><a href="/2020/08/10/an-aspnet-core-url-builder">An ASP.NET Core URL Builder</a></li> <li><a href="/2020/08/24/adding-rest-links-as-a-cross-cutting-concern">Adding REST links as a cross-cutting concern</a></li> <li><a href="/2020/09/28/ensuresuccessstatuscode-as-an-assertion">EnsureSuccessStatusCode as an assertion</a></li> <li><a href="/2020/10/05/fortunately-i-dont-squash-my-commits">Fortunately, I don't squash my commits</a></li> <li><a href="/2020/10/19/monomorphic-functors">Monomorphic functors</a></li> <li><a href="/2020/11/02/signing-urls-with-aspnet">Signing URLs with ASP.NET</a></li> <li><a href="/2020/11/09/checking-signed-urls-with-aspnet">Checking signed URLs with ASP.NET</a></li> <li><a href="/2020/11/16/redirect-legacy-urls">Redirect legacy URLs</a></li> <li><a href="/2020/11/30/name-by-role">Name by role</a></li> <li><a href="/2020/12/07/branching-tests">Branching tests</a></li> <li><a href="/2021/01/11/waiting-to-happen">Waiting to happen</a></li> <li><a href="/2021/01/18/parametrised-test-primitive-obsession-code-smell">Parametrised test primitive obsession code smell</a></li> <li><a href="/2021/01/25/self-hosted-integration-tests-in-aspnet">Self-hosted integration tests in ASP.NET</a></li> <li><a href="/2021/02/01/aspnet-poco-controllers-an-experience-report">ASP.NET POCO Controllers: an experience report</a></li> <li><a href="/2021/02/15/when-properties-are-easier-than-examples">When properties are easier than examples</a></li> <li><a href="/2021/03/01/pendulum-swing-internal-by-default">Pendulum swing: internal by default</a></li> <li><a href="/2021/03/08/pendulum-swing-sealed-by-default">Pendulum swing: sealed by default</a></li> <li><a href="/2021/04/19/consider-including-identity-in-urls">Consider including identity in URLs</a></li> <li><a href="/2021/04/26/leaky-abstraction-by-omission">Leaky abstraction by omission</a></li> <li><a href="/2021/05/03/structural-equality-for-better-tests">Structural equality for better tests</a></li> <li><a href="/2021/05/10/simplifying-code-with-decorated-commands">Simplifying code with Decorated Commands</a></li> <li><a href="/2021/09/13/unit-testing-private-helper-methods">Unit testing private helper methods</a></li> <li><a href="/2021/09/20/keep-ids-internal-with-rest">Keep IDs internal with REST</a></li> <li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li> <li><a href="/2022/05/23/waiting-to-never-happen">Waiting to never happen</a></li> <li><a href="/2022/06/27/test-double-clocks">Test Double clocks</a></li> <li><a href="/2022/07/25/an-applicative-reservation-validation-example-in-c">An applicative reservation validation example in C#</a></li> <li><a href="/2022/08/15/aspnet-validation-revisited">ASP.NET validation revisited</a></li> <li><a href="/2022/09/12/coalescing-dtos">Coalescing DTOs</a></li> <li><a href="/2022/10/17/stubs-and-mocks-break-encapsulation">Stubs and mocks break encapsulation</a></li> <li><a href="/2022/11/28/an-initial-proof-of-concept-of-applicative-assertions-in-c">An initial proof of concept of applicative assertions in C#</a></li> <li><a href="/2023/03/06/warnings-as-errors-friction">Warnings-as-errors friction</a></li> <li><a href="/2023/04/17/a-restaurant-example-of-refactoring-from-example-based-to-property-based-testing">A restaurant example of refactoring from example-based to property-based testing</a></li> <li><a href="/2023/09/04/decomposing-ctfiyhs-sample-code-base">Decomposing CTFiYH's sample code base</a></li> <li><a href="/2023/09/18/do-orms-reduce-the-need-for-mapping">Do ORMs reduce the need for mapping?</a></li> <li><a href="/2023/09/25/the-case-of-the-mysterious-comparison">The case of the mysterious comparison</a></li> </ul> Some of these articles also use code examples from other sources, or code written specifically for that post, but whenever you see a code example from the <em>restaurant reservation</em> domain, it's from the book's code base. </p> <p> That the above list represents the <em>outtakes</em> from the book's example code base should give you an idea of the richness of it. </p> <p> I may add to the list in the future if I write more articles that use the book's example code base. </p> <h3 id="736024dff4af41659a6396dc8e773554"> Conclusion <a href="#736024dff4af41659a6396dc8e773554" title="permalink">#</a> </h3> <p> A decade after <a href="https://amzn.to/36xLycs">my first book</a>, I've finally written a completely new book. Over the years, I had a few false starts. This book wasn't the book I thought that I'd be writing if you'd asked me five years ago, but when it finally dawned on me that the topic ought to be <em>Code That Fits in Your Head: Heuristics for Software Engineering</em>, the book almost wrote itself. </p> <p> This one is for all the software developers out there who aspire to improve their practical skills. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e019d48894ed466689e92b95022785a7"> <!-- Just some random GUID? --> <div class="comment-author"><a href="https://ericvruder.dk">Eric V. Ruder</a> <a href="#e019d48894ed466689e92b95022785a7">#</a></div> <div class="comment-content"> <p> When will the book be available for pre-purchase in Europe/Denmark? Looking forward to reading it! </p> </div> <div class="comment-date">2021-06-23 12:26 UTC</div> </div> <div class="comment" id="7cfe04d6068f49a88db5e7317f0fe11d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7cfe04d6068f49a88db5e7317f0fe11d">#</a></div> <div class="comment-content"> <p> Eric, thank you for writing. The book has already made its way to <a href="https://www.amazon.de/Code-That-Fits-Your-Head/dp/0137464401">amazon.de</a> and <a href="https://www.amazon.fr/Code-That-Fits-Your-Head/dp/0137464401">amazon.fr</a>. On the other hand, it seems to be available neither on amazon.co.uk nor amazon.se. </p> <p> Granted, it doesn't look as though you can pre-order on those two sites yet. </p> <p> If you wish to buy the book directly in Denmark, I think that your best bet is to contact the book store of your preference and ask if and when they're going to carry it. </p> <p> When and how to offer a book for sale is ultimately the purview of book sellers, so not something I can accurately answer. That said, you can already today <a href="https://amzn.to/3pMPw8S">pre-order the book on amazon.com</a>, but it's probably going to cost you a little extra in shipping cost. </p> <p> I'd expect that when the book is finally published, many of the above sites will also sell it. For what it's worth, the manuscript has been done for months. The book is currently 'in production', being proofed and set. As I understand it, this is a fairly predictable process, so I don't expect significant delays relative to the late September 2021 timeline. </p> </div> <div class="comment-date">2021-06-25 5:40 UTC</div> </div> <div class="comment" id="eddd9051446447b8ae4831a703c1ccbd"> <div class="comment-author"><a href="https://github.com/srogovtsev">Serg Rogovtsev</a> <a href="#eddd9051446447b8ae4831a703c1ccbd">#</a></div> <div class="comment-content"> <p> Will it be available as an eBook? Unexpectedly, Google shows no results. </p> </div> <div class="comment-date">2021-06-28 14:13 UTC</div> </div> <div class="comment" id="6ef395bd0b3b48299861798582d2e492"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6ef395bd0b3b48299861798582d2e492">#</a></div> <div class="comment-content"> <p> Serg, thank you for writing. Yes, the book will be available as both PDF and for Kindle. </p> </div> <div class="comment-date">2021-06-29 8:58 UTC</div> </div> <div class="comment" id="22afd730631d4217a73aa2882c45bbc5"> <div class="comment-author"><a href="https://github.com/jwbridges">John Bridges</a> <a href="#22afd730631d4217a73aa2882c45bbc5">#</a></div> <div class="comment-content"> <p> Excellent book! I highly recommend it to new and experienced software developers. </p> </div> <div class="comment-date">2021-12-20 23:56 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Abstruse nomenclature https://blog.ploeh.dk/2021/06/07/abstruse-nomenclature 2021-06-07T05:36:00+00:00 Mark Seemann <div id="post"> <p> <em>Some thoughts on programming terminology.</em> </p> <p> Functional programming has a reputation for <a href="https://en.wikipedia.org/wiki/Fumblerules">abstruse nomenclature</a>: <blockquote> <p> "Functional programmer: (noun) One who names variables "x", names functions "f", and names code patterns "zygohistomorphic prepromorphism"" </p> <footer><cite><a href="https://twitter.com/jamesiry/status/598547781515485184">James Iry</a></cite></footer> </blockquote> I've already discussed <a href="/2015/08/17/when-x-y-and-z-are-great-variable-names">when x, y, and z are great variable names</a>, and I don't intend to say more about that sort of naming here. (And to be clear, <a href="https://twitter.com/kmett/status/1144981306654318594">zygohistomorphic prepromorphisms are a joke</a>.) </p> <p> What I <em>would</em> like to talk about is the contrast to the impenetrable jargon of functional programming: the crystal-clear vocabulary of object-oriented design. Particularly, I'd like to talk about <em>polymorphism</em>. </p> <h3 id="5bf4938cae564d3984e2a77b8f6a017e"> Etymology <a href="#5bf4938cae564d3984e2a77b8f6a017e" title="permalink">#</a> </h3> <p> As <a href="https://en.wikipedia.org/wiki/Polymorphism_(computer_science)">Wikipedia puts it</a> (retrieved 2021-06-04), <em>polymorphism is the provision of a single interface to entities of different types</em>. This doesn't quite fit with the actual meaning of the word, though. </p> <p> The word <em>polymorphism</em> is derived from Greek. <em>Poly</em> means <em>many</em>, and <em>morphism</em> stems from <em>μορφή</em> (<em>morphḗ</em>), which means <em>shape</em>. Putting all of this together, <em>polymorphism</em> means <em>many-shape</em>. </p> <p> How does that fit with the idea of having a single interface? Not very well, I think. </p> <h3 id="21df55548c334577bcde7bf988a73ae6"> A matter of perspective? <a href="#21df55548c334577bcde7bf988a73ae6" title="permalink">#</a> </h3> <p> I suppose that if you view the idea of object-oriented polymorphism from the implementer's perspective, talking about many shapes makes marginal sense. Consider, for example, two classes from <a href="/2021/05/24/tennis-kata-using-the-state-pattern">a recent article</a>. Imagine, for example, that we replace every character in the <code>Advantage</code> code with an <code>x</code>: </p> <p> <pre>xxxxxx xxxxxx xxxxxxxxx x xxxxxx x xxxxxx xxxxxxxxxxxxxxxx xxxxxxx x xxxxxx x xxxxxxx x xxxxxx xxxxxx xxxxxx x xxxx xxxx x xxxxxx xxxx xxxxxxxxxxxxx xxxxxxx xxxx xxxxx x xx xxxxxxx xx xxxxxxx xxxxxxxxxx x xxx xxxxxxxxxxxxxxxxxxxxxx xxxx xxxxxxxxxx x xxxxxxxxxxxxxxx x x</pre> </p> <p> This trick of replacing all characters with <code>x</code> to see the shape of code is one I picked up from <a href="https://en.wikipedia.org/wiki/Kevlin_Henney">Kevlin Henney</a>. Try to do the same with the <code>Deuce</code> struct from the same article: </p> <p> <pre>xxxxxx xxxxxx xxxxx x xxxxxx x xxxxxx xxxxxxxx xxxxxx xxxxxx xxxxxxxx x xxx xxxxxxxx xxxxxx xxxx xxxxxxxxxxxxx xxxxxxx xxxx xxxxx x xxxxxxxxxx x xxx xxxxxxxxxxxxxxxxxx x x</pre> </p> <p> Clearly, these two classes have different shapes. </p> <p> You could argue that all classes have different shapes, but what unites <code>Advantage</code> with <code>Deuce</code> (and three other classes) is that they implement a common interface called <code>IScore</code>. In a sense you can view an <code>IScore</code> object as an object that can have multiple shapes; i.e. a <em>polymorphism</em>. </p> <p> While there's some soundness to this view, as terminology goes, the most important part is only implicitly understood. Yes, all objects have different shapes (<em>poly-morphic</em>), but in order to be a polymorphism, they must <em>present as one</em>. </p> <p> In practice, most of us understand what the other means if one of us says <em>polymorphism</em>, but this is only because we've learned what the word means in the context of object-oriented programming. It's not because the word itself is particularly communicative, even if you pick up the Greek roots. </p> <h3 id="fe0c4a7d14284b10b243c7fa6751aa3d"> Common interface <a href="#fe0c4a7d14284b10b243c7fa6751aa3d" title="permalink">#</a> </h3> <p> The above outline doesn't present how I usually think about polymorphism. I've deliberately tried to steelman it. </p> <p> When I think of polymorphism, I usually focus on what two or more classes may have in common. Instead of replacing every character with an <code>x</code>, try instead to reduce the <code>Advantage</code> and <code>Deuce</code> structs to their public interfaces. First, <code>Advantage</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Advantage</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Advantage</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Player&nbsp;Player&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) }</pre> </p> <p> Now do the same with <code>Deuce</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Deuce</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IScore&nbsp;Instance &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) }</pre> </p> <p> These two APIs are clearly different, yet they have something in common: the <code>BallTo</code> method. In fact, you can draw a <a href="https://en.wikipedia.org/wiki/Venn_diagram">Venn diagram</a> of the public members of all five <code>IScore</code> classes: </p> <p> <img src="/content/binary/venn-diagram-of-score-classes.png" alt="Venn diagram of the members of five classes."> </p> <p> Incidentally, three of the five classes (<code>Forty</code>, <code>Advantage</code>, and <code>CompletedGame</code>) also share a <code>Player</code> property, but all five share the <code>BallTo</code> method. Singling out that method yields the <code>IScore</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IScore</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>); }</pre> </p> <p> Such a (statically-typed) common API is what I usually think of when I think of polymorphism. It's the <em>shape</em> that all five classes have in common. When viewed through the lens of the <code>IScore</code> interface, all five classes <em>have the same form!</em> </p> <p> The term <em>polymorphism</em> (<em>many shapes</em>) makes little sense in this light. Really, it ought to have been called <em>isomorphism</em> (<em>equal shape</em>), but unfortunately, <a href="https://en.wikipedia.org/wiki/Isomorphism">that word already means something else</a>. </p> <p> Sometimes, when you discover that the Greek word for a term is already taken, you can use Latin instead. Let's see, what would <em>one shape</em> be in Latin? <em>Uniform?</em> Yeah, that's also taken. </p> <p> Okay, I'm cheating by deliberately picking words that are already taken. Perhaps a better option might be <em>idiomorphism</em>, from Greek, <em>ἴδιος</em> (<em>idios</em>, “own, personal, distinct”). </p> <h3 id="1bee363b4c554063933820905bee76c0"> Opacity <a href="#1bee363b4c554063933820905bee76c0" title="permalink">#</a> </h3> <p> The point of all of this really isn't to harp on <em>polymorphism</em> in particular. This term is well understood in our industry, so there's no pragmatic reason to change it. </p> <p> Rather, I wish to point out the following: <ul> <li>Object-oriented design also includes Greek terms</li> <li>Even if you can decipher a Greek term, the name may not be helpful</li> <li>In fact, the name may be outright misleading</li> </ul> Ultimately, learning any jargon involves learning how particular words - even <em>normal</em> words - are understood in a particular context (what in <a href="http://amzn.to/WBCwx7">DDD</a> may be know as a <em>bounded context</em>). For example, the word <em>capital</em> means something completely different in architecture and finance. </p> <p> This is true also in programming. Without a context, <a href="https://en.wikipedia.org/wiki/Polymorphism">polymorphism can mean many things</a>. In biology, for example, <a href="https://en.wikipedia.org/wiki/Polymorphism_(biology)">it means the occurrence of two or more clearly different forms within a species</a>, for example light and black <a href="https://en.wikipedia.org/wiki/Jaguar">jaguars</a> (the animal, not <a href="https://en.wikipedia.org/wiki/Jaguar_Cars">the car</a> - another example that a word belongs in a context). </p> <p> This type of polymorphism in biology reminds me more of <a href="https://martinfowler.com/bliki/RoleInterface.html">role interfaces</a>, where a single class can implement several interfaces, but perhaps that's just me. </p> <p> Ultimately, industry terminology is opaque until you learn it. Some words may be easier to learn than others, but looks can be deceiving. <em>Inheritance</em> may sound straightforward, but in object-oriented design, inheritance doesn't entail the death of someone else. Additionally, in programming languages with single inheritance, descendants can only inherit once. As a metaphor, <em>inheritance</em> is mediocre at best. </p> <p> Another friendly-sounding piece of terminology is <em>encapsulation</em> - despite the fact that it's essentially Latin, just warped by two millennia of slight linguistic drift. Even so, this most fundamental concept of object-oriented design <a href="/encapsulation-and-solid">is also the most misunderstood</a>. The word itself doesn't much help communicating the concept. </p> <p> Finally, I wish to remind my English-speaking readers that not all programmers have English as their native language. For many programmers, words like <em>class</em>, <em>object</em>, or <em>encapsulation</em> may be words that they only know via programming. These could be words that have no prior, intrinsic meaning to a speaker of Hungarian or Japanese. </p> <h3 id="457f568704e347d790ed01e28c4c10eb"> Functional programming terminology <a href="#457f568704e347d790ed01e28c4c10eb" title="permalink">#</a> </h3> <p> Is functional programming terminology harder than object-oriented jargon? I don't think so. </p> <p> A nice little word like <em>monoid</em>, for example, is <a href="https://cleancoders.com/episode/humane-code-real-episode-2">Greek for <em>one-like</em></a>. Again, it's not self-explanatory, but once <a href="/2017/10/06/monoids">the concept of a monoid</a> is explained, it makes sense: it's an abstraction that enables you to treat many things as though they are a single thing (with possible loss of fidelity, though). As names go, I find this more accurate than <em>polymorphism</em>. </p> <p> Granted, there's more Greek in functional programming than in object-oriented design, but (Latin) English is still present: <em>recursion</em>, <em>fold</em>, and <em>traversal</em> are common terms. </p> <p> And then there's the occasional nonsense word, like <a href="/2018/03/22/functors">functor</a>. Despite some of digging, <a href="https://cleancoders.com/episode/humane-code-real-episode-5">I've only managed to learn that <em>functor</em> is a compaction of <em>function</em> and <em>factor</em></a> - that is, a <em>function factor</em>, but what does that tell you? </p> <p> In many ways, I prefer nonsense words like <em>functor</em>, because at least, they aren't misleading. When you learn that word, you have no preconception that you think you already know what it means. <a href="https://michaelfeathers.silvrback.com/a-book-of-form">Michael Feathers is experimenting with a similar idea, but in another context</a>, inventing words like <em>exot</em>, <em>lavin</em>, <em>endot</em>, <em>parzo</em>, <em>duon</em>, and <em>tojon</em>. </p> <h3 id="cc8da4b8866149a5ab6421afa9b395d3"> Conclusion <a href="#cc8da4b8866149a5ab6421afa9b395d3" title="permalink">#</a> </h3> <p> It's easy to dismiss the alien as incomprehensible. This often happens in programming. <a href="/2015/08/03/idiomatic-or-idiosyncratic">New ideas are dismissed as <em>non-idiomatic</em></a>. Alternative paradigms like functional programming are rejected because some words aren't immediately forthcoming. </p> <p> This, to me, says more about the person spurning new knowledge than it says about the ideas themselves. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. From State tennis to endomorphism https://blog.ploeh.dk/2021/05/31/from-state-tennis-to-endomorphism 2021-05-31T06:29:00+00:00 Mark Seemann <div id="post"> <p> <em>You can refactor the State pattern to pure functions.</em> </p> <p> In a previous article you saw how to do <a href="/2021/05/24/tennis-kata-using-the-state-pattern">the Tennis kata with the State design pattern</a>. Like most other patterns in <a href="http://amzn.to/XBYukB">Design Patterns</a>, the <a href="https://en.wikipedia.org/wiki/State_pattern">State pattern</a> relies on mutation. If you favour functional programming and immutable data, you may not like that. Fortunately, converting the API to immutable data and <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> is plain sailing. </p> <p> In this post I'll show you how I did it. </p> <h3 id="aa55df271b85405da98ba72ce27cf573"> Return Score <a href="#aa55df271b85405da98ba72ce27cf573" title="permalink">#</a> </h3> <p> Recall from the previous article that the <code>IScore</code> interface defined a single method, <code>BallTo</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IScore</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>); }</pre> </p> <p> With its <code>void</code> return type, it clearly indicate that <code>BallTo</code> mutates the state of <em>something</em> - although it's less clear whether it's the object itself, <code>game</code>, or both. </p> <p> As a first step towards turning the method into a pure function, then, you can change the return type so that it returns an <code>IScore</code> object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IScore</span> { &nbsp;&nbsp;&nbsp;&nbsp;IScore&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>); }</pre> </p> <p> In itself, <a href="/2020/02/24/discerning-and-maintaining-purity">this doesn't guarantee that the function is pure</a>. In fact, after this small step, none of the implementations are. Here, for example, is the updated <code>Advantage</code> implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IScore&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(winner&nbsp;==&nbsp;Player) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompletedGame(winner); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;Deuce.Instance; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;game.Score; }</pre> </p> <p> This implementation still modifies <code>game.Score</code> before returning it. All the other <code>IScore</code> implementations do the same. </p> <h3 id="cd54ec4b37dd4b7c86dcefb4bb29e2f9"> Use the returned score <a href="#cd54ec4b37dd4b7c86dcefb4bb29e2f9" title="permalink">#</a> </h3> <p> Now that the <code>BallTo</code> method returns an <code>IScore</code> object, you can edit the <code>Game</code> class' <code>BallTo</code> method so that it <em>uses</em> the returned value: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Score&nbsp;=&nbsp;Score.BallTo(player,&nbsp;<span style="color:blue;">this</span>); }</pre> </p> <p> Given that all the <code>IScore</code> implementations currently mutate <code>game.Score</code>, this seems redundant, but sets you up for the next refactoring step. </p> <h3 id="dad8032221d14d31b07a64c08284a78d"> Remove State mutation <a href="#dad8032221d14d31b07a64c08284a78d" title="permalink">#</a> </h3> <p> You can now remove the mutation of <code>game.Score</code> from all the implementations of <code>IScore</code>. Here's <code>Advantage</code> after the refactoring: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IScore&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(winner&nbsp;==&nbsp;Player) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;CompletedGame(winner); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Deuce.Instance; }</pre> </p> <p> Notice that this implementation no longer uses the <code>game</code> parameter. </p> <p> The other <code>IScore</code> implementations get a similar treatment. </p> <h3 id="03d390895456430e87eb609245596458"> Remove game parameter <a href="#03d390895456430e87eb609245596458" title="permalink">#</a> </h3> <p> Since no implementations use the <code>game</code> parameter you can remove it from the interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IScore</span> { &nbsp;&nbsp;&nbsp;&nbsp;IScore&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>); }</pre> </p> <p> and, of course, from each of the implementations: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IScore&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(winner&nbsp;==&nbsp;Player) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;CompletedGame(winner); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Deuce.Instance; }</pre> </p> <p> The above method, again, is the implementation of <code>Advantage</code>. </p> <h3 id="145be3db73ff40339eaef927342ca1e5"> Return Game <a href="#145be3db73ff40339eaef927342ca1e5" title="permalink">#</a> </h3> <p> You can now make the same sequence of changes to the <code>Game</code> class itself. Recall from above that its <code>BallTo</code> method returns <code>void</code>. As a the first refactoring step towards turning that method into a pure function, then, change the return type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Game&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Score&nbsp;=&nbsp;Score.BallTo(player); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">this</span>; }</pre> </p> <p> The mutation remains a little while longer, but the method looks like something that <em>could</em> be a pure function. </p> <h3 id="5bead23264dd4833b64731b1b8f8563e"> Return new Game <a href="#5bead23264dd4833b64731b1b8f8563e" title="permalink">#</a> </h3> <p> The next refactoring step is to return a <em>new</em> <code>Game</code> instance instead of the same (mutated) instance: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Game&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Score&nbsp;=&nbsp;Score.BallTo(player); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Game(Score); }</pre> </p> <p> The first line still mutates <code>Score</code>, but now you're only one step away from an immutable implementation. </p> <h3 id="162337d4c9c44a1a93bcddb4e60fc7c0"> Remove Game mutation <a href="#162337d4c9c44a1a93bcddb4e60fc7c0" title="permalink">#</a> </h3> <p> Finally, you can remove the mutation of the <code>Game</code> class. First, remove the <code>internal</code> setter from the <code>Score</code> property: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;IScore&nbsp;Score&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}</pre> </p> <p> You can now <em>lean on the compiler</em>, as Michael Feathers explains in <a href="http://bit.ly/working-effectively-with-legacy-code">Working Effectively with Legacy Code</a>. This forces you to fix the the <code>BallTo</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Game&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Game(Score.BallTo(player)); }</pre> </p> <p> This is also the only refactoring that requires you to edit the unit tests. Here a few methods as examples: </p> <p> <pre>[Theory] [InlineData(Player.One,&nbsp;Point.Love)] [InlineData(Player.One,&nbsp;Point.Fifteen)] [InlineData(Player.One,&nbsp;Point.Thirty)] [InlineData(Player.Two,&nbsp;Point.Love)] [InlineData(Player.Two,&nbsp;Point.Fifteen)] [InlineData(Player.Two,&nbsp;Point.Thirty)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FortyWins</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Point&nbsp;<span style="color:#1f377f;">otherPlayerPoint</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Game(<span style="color:blue;">new</span>&nbsp;Forty(winner,&nbsp;otherPlayerPoint)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.BallTo(winner); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>&nbsp;CompletedGame(winner),&nbsp;actual.Score); } [Theory] [InlineData(Player.One)] [InlineData(Player.Two)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FortyThirty</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Game(<span style="color:blue;">new</span>&nbsp;Forty(player,&nbsp;Point.Thirty)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.BallTo(player.Other()); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Deuce.Instance,&nbsp;actual.Score); }</pre> </p> <p> These are the same test methods as shown in the previous article. The changes are: the introduction of <a href="/2020/11/30/name-by-role">the <code>actual</code> variable</a>, and that the assertion now compares the expected value to <code>actual.Score</code> rather than <code>sut.Score</code>. </p> <p> Both variations of <code>BallTo</code> are now <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphisms</a>. </p> <h3 id="81d185ebdb8c4febaf079dc667edb0c4"> Explicit endomorphism <a href="#81d185ebdb8c4febaf079dc667edb0c4" title="permalink">#</a> </h3> <p> If you're not convinced that the refactored <code>IScore</code> interface describes an endomorphism, you can make it explicit - strictly for illustrative purposes. First, introduce an explicit <code>IEndomorphism</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;<span style="color:#74531f;">Run</span>(T&nbsp;<span style="color:#1f377f;">x</span>); }</pre> </p> <p> This is the same interface as already introduced in the article <a href="/2020/02/17/builder-as-a-monoid">Builder as a monoid</a>. To be clear, I wouldn't use such an interface in normal C# code. I only use it here to illustrate how the <code>BallTo</code> method describes an endomorphism. </p> <p> You can turn a <code>Player</code> into an endomorphism with an extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IEndomorphism&lt;IScore&gt;&nbsp;<span style="color:#74531f;">ToEndomorphism</span>(<span style="color:blue;">this</span>&nbsp;Player&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ScoreEndomorphism(player); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">ScoreEndomorphism</span>&nbsp;:&nbsp;IEndomorphism&lt;IScore&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ScoreEndomorphism</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Player&nbsp;=&nbsp;player; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Player&nbsp;Player&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IScore&nbsp;<span style="color:#74531f;">Run</span>(IScore&nbsp;<span style="color:#1f377f;">score</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;score.BallTo(Player); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is equivalent to <a href="https://en.wikipedia.org/wiki/Partial_application">partial function application</a>. It applies the <code>player</code>, and by doing that returns an <code>IEndomorphism&lt;IScore&gt;</code>. </p> <p> The <code>Game</code> class' <code>BallTo</code> implementation can now <code>Run</code> the endomorphism: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Game&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;IEndomorphism&lt;IScore&gt;&nbsp;<span style="color:#1f377f;">endo</span>&nbsp;=&nbsp;player.ToEndomorphism(); &nbsp;&nbsp;&nbsp;&nbsp;IScore&nbsp;<span style="color:#1f377f;">newScore</span>&nbsp;=&nbsp;endo.Run(Score); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Game(newScore); }</pre> </p> <p> Again, I'm not recommending this style of C# programming. I'm only showing this to illustrate how the object playing the State role now describes an endomorphism. </p> <p> You could subject the <code>Game</code> class' <code>BallTo</code> method to the same treatment, but if you did, you'd have to call the extension method something that would distinguish it from the above <code>ToEndomorphism</code> extension method, since C# doesn't allow overloading exclusively on return type. </p> <h3 id="66f6b83887e9435792187f761596b95c"> Conclusion <a href="#66f6b83887e9435792187f761596b95c" title="permalink">#</a> </h3> <p> Like many of the other patterns in <a href="http://amzn.to/XBYukB">Design Patterns</a>, the State pattern relies on mutation. It's straightforward, however, to refactor it to a set of pure functions. For what it's worth, these are all endomorphisms. </p> <p> This article used a take on the <a href="https://codingdojo.org/kata/Tennis">tennis kata</a> as an example. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tennis kata using the State pattern https://blog.ploeh.dk/2021/05/24/tennis-kata-using-the-state-pattern 2021-05-24T07:03:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of using the State design pattern.</em> </p> <p> Regular readers of this blog will know that I keep coming back to the <a href="https://codingdojo.org/kata/Tennis">tennis kata</a>. It's an interesting little problem to attack from various angles. </p> <p> I don't think you have to <a href="/2020/01/13/on-doing-katas">do the kata</a> that many times before you realise that you're <a href="/2021/03/29/table-driven-tennis-scoring">describing a simple state machine</a>. A few years ago I decided to use that insight to get reacquainted with the <a href="https://en.wikipedia.org/wiki/State_pattern">State design pattern</a>. </p> <p> In this article I'll show you what the code looks like. </p> <h3 id="132c5bdbc1984e0ea87a6ac2f9279600"> Context <a href="#132c5bdbc1984e0ea87a6ac2f9279600" title="permalink">#</a> </h3> <p> As part of the exercise, I decided to stay close to the pattern description in <a href="http://amzn.to/XBYukB">Design Patterns</a>. The public API should be exposed as a single class that hides all the internal state machinery. In the general pattern description, this class is called <code>Context</code>. The TCP example given in the book, however, calls the example class <code>TCPConnection</code>. This indicates that you don't have to use the word <em>context</em> when naming the class. I chose to simply call it <code>Game</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Game</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Game</span>()&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;Points(Point.Love,&nbsp;Point.Love)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Game</span>(IScore&nbsp;<span style="color:#1f377f;">score</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Score&nbsp;=&nbsp;score; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IScore&nbsp;Score&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Score.BallTo(player,&nbsp;<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since the <code>Game</code> class delegates all behaviour to its <code>Score</code> property, it's basically redundant. This may be a degenerate example, but as an exercise of staying true to the pattern, I decided to keep it. It's the class that all tests work through. </p> <h3 id="1d796d3f77b84a6dbcc1cf312d0fafd7"> Test <a href="#1d796d3f77b84a6dbcc1cf312d0fafd7" title="permalink">#</a> </h3> <p> All tests look similar. This parametrised test verifies what happens after <em>deuce:</em> </p> <p> <pre>[Theory] [InlineData(Player.One)] [InlineData(Player.Two)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ScoreDeuce</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Game(Deuce.Instance); &nbsp;&nbsp;&nbsp;&nbsp;sut.BallTo(winner); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>&nbsp;Advantage(winner),&nbsp;sut.Score); }</pre> </p> <p> This is code that I wrote years ago, so it uses <a href="https://xunit.net">xUnit.net</a> 2.3.1 and runs on .NET Framework 4.6.1, but I don't think it'd have looked different today. It follows my <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">heuristic for formatting unit tests</a>. </p> <h3 id="c58be115b320472cb4348058d8a111dd"> Structural equality <a href="#c58be115b320472cb4348058d8a111dd" title="permalink">#</a> </h3> <p> The <a href="/2021/05/03/structural-equality-for-better-tests">equality assertion works because <code>Advantage</code> has structural equality</a>. In this exercise, I found it simpler to declare types as <a href="https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/value-types">value types</a> instead of overriding <code>Equals</code> and <code>GetHashCode</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Advantage</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Advantage</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Player&nbsp;=&nbsp;player; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Player&nbsp;Player&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(winner&nbsp;==&nbsp;Player) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompletedGame(winner); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;Deuce.Instance; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This turned out to be possible throughout, since all types emerged as mere compositions of other value types. The above <code>Advantage</code> struct, for example, <a href="https://en.wikipedia.org/wiki/Adapter_pattern">adapts</a> a <code>Player</code>, which, unsurprisingly, is an <code>enum</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">enum</span>&nbsp;<span style="color:#2b91af;">Player</span> { &nbsp;&nbsp;&nbsp;&nbsp;One&nbsp;=&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;Two }</pre> </p> <p> One of the states holds no data at all, so I made it a <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a>, as suggested in <a href="http://amzn.to/XBYukB">the book</a>. (Contrary to popular belief, <a href="https://stackoverflow.com/a/67331100/126014">I don't consider Singleton an anti-pattern</a>.) </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Deuce</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;IScore&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Deuce(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Advantage(winner); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since it's a Singleton, from an equality perspective it doesn't matter whether it's a value or reference type, but I made it a <code>struct</code> <a href="/2021/05/17/against-consistency">for consistency's sake</a>. </p> <h3 id="f94a31a78e4741139adb53cde96eb927"> State <a href="#f94a31a78e4741139adb53cde96eb927" title="permalink">#</a> </h3> <p> In the State design pattern's formal structure, the <code>Context</code> delegates all behaviour to an abstract <code>State</code> class. Since I consider inheritance harmful (as well as <a href="/2018/02/19/abstract-class-isomorphism">redundant</a>), I instead chose to model the state as an interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IScore</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>); }</pre> </p> <p> As the pattern suggests, the State object exposes methods that take the Context as an extra parameter. This enables concrete State implementation to change the state of the Context, as both the above structs (<code>Advantage</code> and <code>Deuce</code>) demonstrate. They both implement the interface. </p> <p> When I do the kata, I always seem to arrive at five distinct states. I'm not sure if this reflects the underlying properties of the problem, or if it's just because that's what worked for me years ago, and I'm now stuck in some cognitive local maximum. In any case, that's what happened here as well. Apart from the above <code>Advantage</code> and <code>Deuce</code> there's also a <code>Forty</code> implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Forty</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Forty</span>(Player&nbsp;<span style="color:#1f377f;">player</span>,&nbsp;Point&nbsp;<span style="color:#1f377f;">otherPlayerPoint</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Player&nbsp;=&nbsp;player; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OtherPlayerPoint&nbsp;=&nbsp;otherPlayerPoint; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Player&nbsp;Player&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Point&nbsp;OtherPlayerPoint&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Player&nbsp;==&nbsp;winner) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;CompletedGame(winner); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(OtherPlayerPoint&nbsp;==&nbsp;Point.Thirty) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;Deuce.Instance; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(OtherPlayerPoint&nbsp;==&nbsp;Point.Fifteen) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Forty(Player,&nbsp;Point.Thirty); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Forty(Player,&nbsp;Point.Fifteen); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Another thing that I've also noticed when doing the Tennis kata is that the state logic for <em>advantage</em> and <em>deuce</em> is simple, whereas the state transitions involving <em>points</em> is more complicated. If you think <code>Forty</code> looks complicated, then consider <code>Points</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Points</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Points</span>(Point&nbsp;<span style="color:#1f377f;">playerOnePoint</span>,&nbsp;Point&nbsp;<span style="color:#1f377f;">playerTwoPoint</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PlayerOnePoint&nbsp;=&nbsp;playerOnePoint; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PlayerTwoPoint&nbsp;=&nbsp;playerTwoPoint; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Point&nbsp;PlayerOnePoint&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Point&nbsp;PlayerTwoPoint&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">pp</span>&nbsp;=&nbsp;PlayerPoint(winner); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">opp</span>&nbsp;=&nbsp;PlayerPoint(winner.Other()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(pp&nbsp;==&nbsp;Point.Thirty) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Forty(winner,&nbsp;opp); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span>&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(winner&nbsp;==&nbsp;Player.One) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Points(Increment(PlayerOnePoint),&nbsp;PlayerTwoPoint); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;game.Score&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Points(PlayerOnePoint,&nbsp;Increment(PlayerTwoPoint)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Point&nbsp;<span style="color:#74531f;">PlayerPoint</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(player&nbsp;==&nbsp;Player.One) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;PlayerOnePoint; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;PlayerTwoPoint; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Point&nbsp;<span style="color:#74531f;">Increment</span>(Point&nbsp;<span style="color:#1f377f;">point</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(point&nbsp;==&nbsp;Point.Love) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Point.Fifteen; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Point.Thirty; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The last <code>IScore</code> implementation represents a completed game: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">CompletedGame</span>&nbsp;:&nbsp;IScore { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CompletedGame</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Player&nbsp;=&nbsp;player; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Player&nbsp;Player&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">BallTo</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Game&nbsp;<span style="color:#1f377f;">game</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In a completed game, the <code>BallTo</code> implementation is a no-op, because <code>Player</code> has already won the game. </p> <h3 id="9a1fbf4d7d774ebea12bbe93173a5a71"> Miscellany <a href="#9a1fbf4d7d774ebea12bbe93173a5a71" title="permalink">#</a> </h3> <p> Here's a few more tests, just to back up my claim that all tests look similar: </p> <p> <pre>[Theory] [InlineData(Player.One,&nbsp;Point.Love)] [InlineData(Player.One,&nbsp;Point.Fifteen)] [InlineData(Player.One,&nbsp;Point.Thirty)] [InlineData(Player.Two,&nbsp;Point.Love)] [InlineData(Player.Two,&nbsp;Point.Fifteen)] [InlineData(Player.Two,&nbsp;Point.Thirty)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FortyWins</span>(Player&nbsp;<span style="color:#1f377f;">winner</span>,&nbsp;Point&nbsp;<span style="color:#1f377f;">otherPlayerPoint</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Game(<span style="color:blue;">new</span>&nbsp;Forty(winner,&nbsp;otherPlayerPoint)); &nbsp;&nbsp;&nbsp;&nbsp;sut.BallTo(winner); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(<span style="color:blue;">new</span>&nbsp;CompletedGame(winner),&nbsp;sut.Score); } [Theory] [InlineData(Player.One)] [InlineData(Player.Two)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">FortyThirty</span>(Player&nbsp;<span style="color:#1f377f;">player</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;Game(<span style="color:blue;">new</span>&nbsp;Forty(player,&nbsp;Point.Thirty)); &nbsp;&nbsp;&nbsp;&nbsp;sut.BallTo(player.Other()); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(Deuce.Instance,&nbsp;sut.Score); }</pre> </p> <p> The second of these test methods uses an extension method called <code>Other</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PlayerEnvy</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Player&nbsp;<span style="color:#74531f;">Other</span>(<span style="color:blue;">this</span>&nbsp;Player&nbsp;<span style="color:#1f377f;">player</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(player&nbsp;==&nbsp;Player.One) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Player.Two; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Player.One; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As is my custom, I named the class containing the extension method with the <code>Envy</code> suffix, because I often consider this kind of extension method a sign of Feature Envy. Alas, in C# you can't add methods to an <code>enum</code>. </p> <h3 id="a196bacd7b7041629b15ff6bd5ada849"> Conclusion <a href="#a196bacd7b7041629b15ff6bd5ada849" title="permalink">#</a> </h3> <p> Implementing the tennis kata with the classic State pattern is straightforward. </p> <p> After having spent the majority of the last decade with functional programming, I've come to realise that many problems are really just state machines waiting to be revealed as such. Implementing a finite state machine in a language with <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> is so easy that you often reach for that kind of modelling. </p> <p> Before I learned functional programming, when all I knew was procedural and object-oriented code, I rarely thought of problems in terms of finite state machines. Now I see them everywhere. It's an example of how learning a completely different thing can feed back on everyday programming. </p> <p> Once you recognise that a problem can be modelled as a finite state machine, you have new options. If you're in a conservative context where colleagues aren't keen on fancy FP shenanigans, you can always reach for the State design pattern. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ee5552352ccc46c3a73451f899462d26"> <div class="comment-author"><a href="https://github.com/JimCooperFromGmailAccount">Jim Cooper</a> <a href="#ee5552352ccc46c3a73451f899462d26">#</a></div> <div class="comment-content"> <p> Do you think that perhaps you are at risk of making too many problems look like nails for your state machine hammer? :-) Actually, you just want to convert a pair of points into a tennis score. That doesn't require a state machine, I don't think: </p> <p><pre> using NUnit.Framework; namespace TennisKata { public class Tests { private TennisGame tennisGame; [SetUp] public void Setup() { tennisGame = new TennisGame(); } [TestCase(0, 0, ExpectedResult = "Love All")] [TestCase(1, 1, ExpectedResult = "Fifteen All")] [TestCase(2, 2, ExpectedResult = "Thirty All")] [TestCase(3, 3, ExpectedResult = "Deuce")] [TestCase(4, 4, ExpectedResult = "Deuce")] [TestCase(1, 0, ExpectedResult = "Fifteen - Love")] [TestCase(2, 1, ExpectedResult = "Thirty - Fifteen")] [TestCase(3, 2, ExpectedResult = "Forty - Thirty")] [TestCase(4, 0, ExpectedResult = "Game Server")] [TestCase(0, 1, ExpectedResult = "Love - Fifteen")] [TestCase(1, 2, ExpectedResult = "Fifteen - Thirty")] [TestCase(2, 3, ExpectedResult = "Thirty - Forty")] [TestCase(0, 4, ExpectedResult = "Game Receiver")] [TestCase(4, 3, ExpectedResult = "Advantage Server")] [TestCase(3, 4, ExpectedResult = "Advantage Receiver")] [TestCase(5, 4, ExpectedResult = "Advantage Server")] [TestCase(4, 5, ExpectedResult = "Advantage Receiver")] [TestCase(5, 3, ExpectedResult = "Game Server")] [TestCase(3, 5, ExpectedResult = "Game Receiver")] [TestCase(5, 2, ExpectedResult = "Invalid score")] [TestCase(2, 5, ExpectedResult = "Invalid score")] public string ShouldConvertPointsToTennisStyleScore(int serverPoints, int receiverPoints) { SetServerPointsTo(serverPoints); SetReceiverPointsTo(receiverPoints); return tennisGame.Score; } private void SetServerPointsTo(int serverPoints) { for (var i = 0; i < serverPoints; i++) { tennisGame.PointToServer(); } } private void SetReceiverPointsTo(int serverPoints) { for (var i = 0; i < serverPoints; i++) { tennisGame.PointToReceiver(); } } } public class TennisGame { private int serverPoints; private int receiverPoints; public string Score => serverPoints switch { _ when serverPoints == receiverPoints && serverPoints >= 3 => "Deuce", _ when serverPoints == receiverPoints => $"{PointsAsWord(serverPoints)} All", _ when serverPoints >= 4 && serverPoints > receiverPoints => GetGameOrAdvantage(serverPoints, receiverPoints, "Server"), _ when receiverPoints >= 4 => GetGameOrAdvantage(receiverPoints, serverPoints, "Receiver"), _ => $"{PointsAsWord(serverPoints)} - {PointsAsWord(receiverPoints)}" }; public void PointToServer() { serverPoints++; } public void PointToReceiver() { receiverPoints++; } private static string GetGameOrAdvantage(int highScore, int lowScore, string highScorerName) { var scoreDifference = highScore - lowScore; return scoreDifference switch { 1 => $"Advantage {highScorerName}", _ when highScore > 4 && scoreDifference > 2 => "Invalid score", _ => $"Game {highScorerName}" }; } private string PointsAsWord(int points) { var pointNames = new [] { "Love", "Fifteen", "Thirty", "Forty"}; return pointNames[points]; } } }</pre> </p> </div> <div class="comment-date">2021-05-27 7:56 UTC</div> </div> <div class="comment" id="37f19a75fa454aae91a67b4b2a573ada"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#37f19a75fa454aae91a67b4b2a573ada">#</a></div> <div class="comment-content"> <p> Jim, thank you for writing. You're right: a state machine isn't <em>required</em>. It's a nice judo trick to keep track of the server and receiver points as two different numbers. That does simplify the code. </p> <p> I tried something similar many years ago (after all, the <a href="https://codingdojo.org/kata/Tennis/">kata description itself strongly hints at that alternative perspective</a>), but for various reasons ended with an implementation that wasn't as nice as yours. I never published it. I've done this exercise many times, and I've only published the ones that I find can be used to highlight some interesting point. </p> <p> The <a href="/2020/01/13/on-doing-katas">point of doing a coding kata is to experiment with variation</a>. The goal isn't always to reach the fewest lines of code, or complete the exercise as fast as possible. These can be interesting exercises in their own rights, but by doing a kata with other constraints can be illuminating as well. </p> <p> My goal with this variation was mainly to get reacquainted with the State pattern. Actually 'solving' the problem is a requirement, but not the goal. </p> <p> Modelling the problem with the State pattern has advantages and disadvantages. A major advantage is that it offers an API that enables client code to programmatically distinguish between the various states. When I did the exercise similar to your code, asserting against a string is easy. However, basing an API on a returned string may not be an adequate design. It's okay for an exercise, but imagine that you were asked to translate the scores. For example, in Danish, <em>advantage</em> is instead called <em>fordel</em>. Another requirement might be that you report players by name. So, for example, a Danish score might instead require something like <em>fordel Serena Williams</em>. </p> <p> Don't take this as a criticism of your code. Sometimes, you don't need more than what you've done, and in such cases, doing more would be over-engineering. </p> <p> On the other hand, if you find yourself in situations where e.g. translation is required, it can be helpful to be aware that other ways to model a problem are available. That's the underlying benefit of doing katas. The more variations you do, the better prepared you become to 'choose the right tool for the job.' </p> <p> All that said, though, with the tennis kata, you can <a href="/2021/03/29/table-driven-tennis-scoring">make it trivially simple modelling it as a finite state automaton</a>. </p> </div> <div class="comment-date">2021-05-30 9:09 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Against consistency https://blog.ploeh.dk/2021/05/17/against-consistency 2021-05-17T06:34:00+00:00 Mark Seemann <div id="post"> <p> <em>A one-sided argument against imposing a uniform coding style.</em> </p> <p> I want to credit <a href="http://www.natpryce.com">Nat Pryce</a> for planting the seed for the following line of thinking <a href="https://www.infoq.com/presentations/Stop-Refactoring">at GOTO Copenhagen 2012</a>. I'd also like to relieve him of any responsibility for what follows. The blame is all mine. </p> <p> I'd also like to point out that I'm not categorically against consistency in code. There are plenty of good arguments <em>for</em> having a consistent coding style, but as regular readers may have observed, I have a contrarian streak to my personality. If you're only aware of one side of an argument, I believe that you're unequipped to make informed decisions. Thus, I make the following case <em>against</em> imposing coding styles, not because I'm dead-set opposed to consistent code, but because I believe you should be aware of the disadvantages. </p> <h3 id="ab42cf8b7ede4b00895acba034f9a7b1"> TL;DR <a href="#ab42cf8b7ede4b00895acba034f9a7b1" title="permalink">#</a> </h3> <p> In this essay, I use the term <em>coding style</em> to indicate a set of rules that governs how code should be formatted. This may include rules about where you put brackets, whether to use tabs or spaces, which <a href="https://en.wikipedia.org/wiki/Naming_convention_(programming)">naming conventions</a> to use, <a href="/2019/11/04/the-80-24-rule">maximum line width</a>, in C# whether you should use the <code>var</code> keyword or explicit variable declaration, and so on. </p> <p> As already stated, I can appreciate consistency in code as much as the next programmer. I've seen more than one code base, however, where a formal coding style contributed to ossification. </p> <p> I've consulted a few development organisations with an eye to improving processes and code quality. Sometimes my suggestions are met with hesitation. When I investigate what causes developers to resist, it turns out that my advice goes against 'how things are done around here.' It might even go against the company's formal coding style guidelines. </p> <p> Coding styles may impede progress. </p> <p> Below, I'll discuss a few examples. </p> <h3 id="682fe4a54ba44844889f03ca33fac209"> Class fields <a href="#682fe4a54ba44844889f03ca33fac209" title="permalink">#</a> </h3> <p> A typical example of a coding style regulates naming of class fields. While it seems to be on retreat now, at one time many C# developers would name class fields with a leading underscore: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;_action; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;_controller; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">object</span>?&nbsp;_values;</pre> </p> <p> I never liked that naming convention because it meant that I always had to type an underscore <em>and then at least one other letter</em> before I could make good use of my IDE. For example, in order to take advantage of auto-completion when using the <code>_action</code> field, I'd have to type <code>_a</code>, instead of just <code>a</code>. </p> <p> <img src="/content/binary/intellisense-upon-underscore.png" alt="Screen shot of Intellisense drop-down after typing a single underscore."> </p> <p> Yes, I know that <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">typing isn't a bottleneck</a>, but it still annoyed me because it seemed redundant. </p> <p> A variation of this coding style is to mandate an <code>m_</code> prefix, which only exacerbates the problem. </p> <p> Many years ago, I came across a 'solution': Put the underscore <em>after</em> the field name, instead of in front of it: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;action_; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;controller_; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">object</span>?&nbsp;values_;</pre> </p> <p> Problem solved - I thought for some years. </p> <p> Then someone pointed out to me that if distinguishing a class field from a local variable is the goal, you can use the <code>this</code> qualifier. That made sense to me. Why invent some controversial naming rule when you can use a language keyword instead? </p> <p> So, for years, I'd always interact with class fields like <code>this.action</code>, <code>this.controller</code>, and so on. </p> <p> Then someone else point out to me that this ostensible need to be able to distinguish class fields from local variables, or static from instance fields, was really a symptom of either poor naming or too big classes. While that hurt a bit, I couldn't really defend against the argument. </p> <p> This is all many years ago. These days, I name class fields like I name variables, and I don't qualify access. </p> <p> The point of this little story is to highlight how you can institute a naming convention with the best of intentions. As experience accumulates, however, you may realise that you've become wiser. Perhaps that naming convention wasn't such a good idea after all. </p> <p> When that happens, change the convention. Don't worry that this is going to make the code base inconsistent. An improvement is an improvement, while consistency might only imply that the code base is consistently bad. </p> <h3 id="0246a2c2c5754c48a008a613c91e6dab"> Explicit variable declaration versus var <a href="#0246a2c2c5754c48a008a613c91e6dab" title="permalink">#</a> </h3> <p> In late 2007, more than a decade ago, C# 3 introduced the <code>var</code> keyword to the language. This tells the compiler to automatically <a href="https://en.wikipedia.org/wiki/Type_inference">infer</a> the static type of a variable. Before that, you'd have to explicitly declare the type of all variables: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">href</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;UrlBuilder() &nbsp;&nbsp;&nbsp;&nbsp;.WithAction(nameof(CalendarController.Get)) &nbsp;&nbsp;&nbsp;&nbsp;.WithController(nameof(CalendarController)) &nbsp;&nbsp;&nbsp;&nbsp;.WithValues(<span style="color:blue;">new</span>&nbsp;{&nbsp;year&nbsp;=&nbsp;DateTime.Now.Year&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.BuildAbsolute(Url);</pre> </p> <p> In the above example, the variable <code>href</code> is explicitly declared as a <code>string</code>. With the <code>var</code> keyword you can alternatively write the expression like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">href</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;UrlBuilder() &nbsp;&nbsp;&nbsp;&nbsp;.WithAction(nameof(CalendarController.Get)) &nbsp;&nbsp;&nbsp;&nbsp;.WithController(nameof(CalendarController)) &nbsp;&nbsp;&nbsp;&nbsp;.WithValues(<span style="color:blue;">new</span>&nbsp;{&nbsp;year&nbsp;=&nbsp;DateTime.Now.Year&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.BuildAbsolute(Url);</pre> </p> <p> The <code>href</code> variable is still statically typed as a <code>string</code>. The compiler figures that out for you, in this case because the <code>BuildAbsolute</code> method returns a <code>string</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">BuildAbsolute</span>(IUrlHelper&nbsp;<span style="color:#1f377f;">url</span>)</pre> </p> <p> These two alternatives are interchangeable. They compile to the same <a href="https://en.wikipedia.org/wiki/Common_Intermediate_Language">IL code</a>. </p> <p> When C# introduced this language feature, a year-long controversy erupted. Opponents felt that using <code>var</code> made code less readable. This isn't an entirely unreasonable argument, but most C# programmers subsequently figured that the advantages of using <code>var</code> outweigh the disadvantages. </p> <p> A major advantage is that using <code>var</code> better facilitates refactoring. Sometimes, for example, you decide to change the return type of a method. What happens if you change the return type of <a href="/2020/08/10/an-aspnet-core-url-builder">UrlBuilder</a>? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Uri&nbsp;<span style="color:#74531f;">BuildAbsolute</span>(IUrlHelper&nbsp;<span style="color:#1f377f;">url</span>)</pre> </p> <p> If you've used the <code>var</code> keyword, the compiler just infers a different type. If, on the other hand, you've explicitly declared <code>href</code> as a <code>string</code>, that piece of code no longer compiles. </p> <p> Using the <code>var</code> keyword makes refactoring easier. You'll still need to edit some call sites when you make a change like this, because <code>Uri</code> affords a different API than <code>string</code>. The point, however, is that when you use <code>var</code>, the cost of making a change is lower. Less <a href="/2019/12/16/zone-of-ceremony">ceremony</a> means that you can spend your effort where it matters. </p> <p> In the context of coding styles, I still, more than a decade after the <code>var</code> keyword was introduced, encounter code bases that use explicit variable declaration. </p> <p> When I explain the advantages of using the <code>var</code> keyword to the team responsible for the code base, they may agree in principle, but still oppose using it in practice. The reason? Using <code>var</code> would make the code base inconsistent. </p> <p> Aiming for a consistent coding style is fine, but only as long as it doesn't prohibit improvements. Don't let it stand in the way of progress. </p> <h3 id="83bc13f15fe04b829b25002f39575bb2"> Habitability <a href="#83bc13f15fe04b829b25002f39575bb2" title="permalink">#</a> </h3> <p> I don't mind consistency; in fact, I find it quite attractive. It must not, however, become a barrier to improvement. </p> <p> I've met programmers who so strongly favour consistency that they feel that, in order to change coding style, they'd have to go through the entire code base and retroactively update it all to fit the new rule. This is obviously prohibitively expensive to do, so practically it prevents change. </p> <p> Consistency is fine, but learn to accept inconsistency. As Nat Pryce said, we should learn to love the mess, to adopt a philosophy akin to <a href="https://en.wikipedia.org/wiki/Wabi-sabi">wabi-sabi</a>. </p> <p> I think this view on inconsistent code helped me come to grips with my own desire for neatness. An inconsistent code base looks <em>inhabited</em>. I don't mind looking around in a code base and immediately being able to tell: <em>oh, Anna wrote this</em>, or <em>Nader is the author of this method</em>. </p> <p> What's more important is that the code is comprehensible. </p> <h3 id="8382ebd1ba1d4c60abb984f87dc9c317"> Conclusion <a href="#8382ebd1ba1d4c60abb984f87dc9c317" title="permalink">#</a> </h3> <p> Consistency in code isn't a bad thing. Coding styles can help encourage a degree of consistency. I think that's fine. </p> <p> On the other hand, consistency shouldn't be the highest goal of a code base. If improving the code makes a code base inconsistent, I think that the improvement should trump consistency every time. </p> <p> Let the old code be as it is, until you need to work with it. When you do, you can apply Robert C. Martin's boy scout rule: <em>Always leave the code cleaner than you found it</em>. Code perfection is like eventual consistency; it's something that you should constantly move towards, yet may never attain. </p> <p> Learn to appreciate the 'lived-in' quality of an active code base. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Simplifying code with Decorated Commands https://blog.ploeh.dk/2021/05/10/simplifying-code-with-decorated-commands 2021-05-10T05:37:00+00:00 Mark Seemann <div id="post"> <p> <em>Consider modelling many side effects as a single Command.</em> </p> <p> In a <a href="/2021/04/26/leaky-abstraction-by-omission">previous article I discussed how an abstraction can sometimes be leaky by omission</a>. In this article, you'll see how removing the leak enables some beneficial refactoring. I'm going to assume that you've read the previous article. </p> <p> <ins datetime="2021-06-15T06:14Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="7d89e80085aa44edaf169962232a078a"> The relative cost of the four CRUD operations <a href="#7d89e80085aa44edaf169962232a078a" title="permalink">#</a> </h3> <p> In this article, you'll see code that implements an ASP.NET Controller action. It enables a <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> client to update an existing reservation via a <code>PUT</code> request. </p> <p> I chose to show you the <code>Put</code> method because it's the worst, and thereby the one where refactoring is most warranted. This seems to follow a pattern I've noticed over the years: data updates are always the worst. </p> <p> Before I show you the code, I'd like to take a little detour to discuss this observation. </p> <p> Consider the four <a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> operations. Which one is the easiest to implement and maintain, and which one gives you most grief? </p> <p> <em>Deletes</em> are typically straightforward: A unique identifier is all it takes. The only small complication you may have to consider is <a href="https://en.wikipedia.org/wiki/Idempotence">idempotence</a>. If you delete an entity once, it's gone. What should happen if you 'delete' it again? To be clear, I don't consider this a trick question. A delete operation should be idempotent, but sometimes, depending on the underlying storage technology, you may have to write a few lines of code to make that happen. </p> <p> <em>Reads</em> are a little more complicated. I'm actually not sure if reads are more or less involved than <em>create</em> operations. The complexity is probably about the same. Reading a single document from a document database is easy, as is reading a single row from a database. Relational databases can make this a bit harder when you have to join tables, but when you get the hang of it, it's not that hard. </p> <p> <em>Create</em> operations tend to be about as easy or difficult as reads. Adding a new document to a document database or BLOB storage is easy. Adding a complex entity with foreign key relationships in a relational database is a bit more complicated, but still doable. </p> <p> <em>Updates</em>, though, are evil. In a document database, it may be easy enough if you can just replace the document wholesale. Often, however, updates involves delta detection. Particularly in databases, when foreign keys are involved, you may have to recursively track down all the related rows and either update those as well, or delete and recreate them. </p> <p> As you'll see in the upcoming code example, an update typically also involves complicated auxiliary logic to determine what changed, and how to react to it. </p> <p> For that reason, if possible, I prefer modelling data without supporting updates. Create/read/delete is fine, but if you don't support updates, you may not need deletes either. There's a reason I like Event Sourcing. </p> <h3 id="530a01b1e4e24a5e9af0d8e87d8cacf6"> A complicated Put method <a href="#530a01b1e4e24a5e9af0d8e87d8cacf6" title="permalink">#</a> </h3> <p> My restaurant reservation API included this method that enabled REST clients to update reservations: </p> <p> <pre>[HttpPut(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations/{id}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;<span style="color:#74531f;">Put</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">id</span>, &nbsp;&nbsp;&nbsp;&nbsp;ReservationDto&nbsp;<span style="color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(dto&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(dto)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!Guid.TryParse(id,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">rid</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;NotFoundResult(); &nbsp;&nbsp;&nbsp;&nbsp;Reservation?&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;dto.Validate(rid); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;BadRequestResult(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">restaurant</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;RestaurantDatabase &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GetRestaurant(restaurantId).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(restaurant&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;NotFoundResult(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;TryUpdate(restaurant,&nbsp;reservation).ConfigureAwait(<span style="color:blue;">false</span>); }</pre> </p> <p> Had I written this code exclusively for myself, I'd written in a more functional style, as an <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>. (Actually, had I written this code exclusively for myself, I'd written it in <a href="https://fsharp.org">F#</a> or <a href="https://www.haskell.org">Haskell</a>.) This code, however, is written for another audience, so I didn't want to assume that the reader knows about impureim sandwiches. </p> <p> I still wanted to decompose the functionality into <a href="/2019/11/04/the-80-24-rule">small blocks</a>. There's still an echo of the impureim sandwich architecture in the <code>Put</code> method, because it handles most of the impure preparation - the top of the sandwich, so to speak. </p> <p> The rest - any functional core there might be, as well as impure post-processing - it delegates to the <code>TryUpdate</code> method. </p> <h3 id="fbbac35a68604f0ca221b0d4cc0f6743"> TryUpdate <a href="#fbbac35a68604f0ca221b0d4cc0f6743" title="permalink">#</a> </h3> <p> Here's the <code>TryUpdate</code> method: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;<span style="color:#74531f;">TryUpdate</span>( &nbsp;&nbsp;&nbsp;&nbsp;Restaurant&nbsp;<span style="color:#1f377f;">restaurant</span>, &nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">scope</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TransactionScope( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionScopeAsyncFlowOption.Enabled); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">existing</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservation(reservation.Id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(existing&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;NotFoundResult(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">ok</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;WillAcceptUpdate(restaurant,&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!ok) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;NoTables500InternalServerError(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Update(restaurant,&nbsp;reservation,&nbsp;existing) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;scope.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;OkObjectResult(reservation.ToDto()); }</pre> </p> <p> To be honest, this is mostly just more impure pre-processing. The functional core is hidden away inside the (impure) <code>WillAcceptUpdate</code> method, but I'm not going to show you that one. It's not important in this context. </p> <p> If, however, the method decides that the update is possible, it'll make one more delegation, to the <code>Update</code> method. </p> <p> I admit it: This isn't the prettiest code I've ever written. I did warn you, though. I chose this method as an example because it could really do with some refactoring. One problem I have with it is the naming. You have a <code>Put</code> method, which calls a <code>TryUpdate</code> method, which again calls an <code>Update</code> method. </p> <p> Even though the <em>Try</em> prefix is a .NET <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiom</a>, I still feel that a regular reader could be easily confused, having to choose between <code>TryUpdate</code> and <code>Update</code>. </p> <p> Still, let's soldier on and review the <code>Update</code> method as well. It's the last one, I promise. </p> <h3 id="4f28399e4ca64ab796446932136c5d7e"> Update <a href="#4f28399e4ca64ab796446932136c5d7e" title="permalink">#</a> </h3> <p> Here's the <code>Update</code> method: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">Update</span>( &nbsp;&nbsp;&nbsp;&nbsp;Restaurant&nbsp;<span style="color:#1f377f;">restaurant</span>, &nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>, &nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;<span style="color:#1f377f;">existing</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(existing.Email&nbsp;!=&nbsp;reservation.Email) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostOffice &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.EmailReservationUpdating(restaurant.Id,&nbsp;existing) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Update(reservation).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostOffice &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.EmailReservationUpdated(restaurant.Id,&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); }</pre> </p> <p> The method perfectly illustrates what I meant when I wrote that you often have to do various kinds of delta analysis when implementing an update - even if delta analysis isn't required by the data store. </p> <p> This method does two things: <ul> <li>It sends emails</li> <li>It updates the repository</li> </ul> Notice that if the email address changes, <code>Update</code> sends an email to the old address. This is an example of delta analysis. This only happens on a changing email address. It doesn't happen if the name or quantity changes. </p> <p> The motivation is that it may serve to warn the user if someone tries to change the reservation. Only when the email address changes is it necessary to send an email to the old address. </p> <p> In all cases, the method sends an email to the 'current' address. </p> <p> This seems ripe for refactoring. </p> <h3 id="7d446d5e2d1647a49a22b4be7953e1c7"> Plugging the leak <a href="#7d446d5e2d1647a49a22b4be7953e1c7" title="permalink">#</a> </h3> <p> The <code>Update</code> method is an asynchronous <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command</a>. It exclusively produces side effects, but it doesn't return anything (we'll regard <code>Task</code> as 'asynchronous <a href="/2018/01/15/unit-isomorphisms">unit</a>'). </p> <p> I've <a href="/2011/03/22/CommandsareComposable">known since 2011 that Commands are composable</a>. Later, I also figured out <a href="/2019/01/28/better-abstractions-revisited">the fundamental reason for that</a>. </p> <p> The <code>Update</code> method composes three other Commands - one conditional and two unconditional. This seems to call for some sort of composition: <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">Chain of Responsibility</a>, <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a>, or <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a>. Common to these patterns, however, is that the object that they compose must share an API. In a language like C# it means that they must share a polymorphic type. </p> <p> Which type might that be? Let's list the three method signatures in action, one after the other: <ul> <li> <code>Task&nbsp;<span style="color:#74531f;">EmailReservationUpdating</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>)</code> </li> <li> <code>Task&nbsp;<span style="color:#74531f;">Update</span>(Reservation&nbsp;<span style="color:#1f377f;">reservation</span>)</code> </li> <li> <code>Task&nbsp;<span style="color:#74531f;">EmailReservationUpdated</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>)</code> </li> </ul> Do these three methods have anything in common? </p> <p> The commonality might be easier to spot if we X out the names (<a href="/2020/11/23/good-names-are-skin-deep">which are only skin-deep</a>, anyway): <ul> <li> <code>Task&nbsp;<span style="color:#74531f;">Xxx</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>)</code> </li> <li> <code>Task&nbsp;<span style="color:#74531f;">Xxx</span>(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>)</code> </li> <li> <code>Task&nbsp;<span style="color:#74531f;">Xxx</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>)</code> </li> </ul> They <em>almost</em> look like each other! </p> <p> The only deviation is that the middle method (originally the <code>Update</code> method) lacks a <code>restaurantId</code> parameter. </p> <p> As the previous article explained, though, this is a <a href="/2021/04/26/leaky-abstraction-by-omission">leaky abstraction by omission</a>. Will plugging the leak enable a refactoring? </p> <p> Let's try. Make <code>restaurantId</code> a parameter for all methods defined by the interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;Reservation?&gt;&nbsp;<span style="color:#74531f;">ReadReservation</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Guid&nbsp;<span style="color:#1f377f;">id</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Update</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Delete</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Guid&nbsp;<span style="color:#1f377f;">id</span>); }</pre> </p> <p> This is the suggested remedy from the previous article, so I put it here solely as a reminder. </p> <h3 id="e5963702434c4674a39a99944d599522"> An emailing Decorator <a href="#e5963702434c4674a39a99944d599522" title="permalink">#</a> </h3> <p> There's a sequence to the actions in the <code>Update</code> method: <ol> <li>It emails the old address about a changing address</li> <li>It updates the reservation</li> <li>It emails the current address about the update</li> </ol> It's easiest to preserve this order of actions if you implement a <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> around the new version of <code>IReservationsRepository</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">EmailingReservationsRepository</span>&nbsp;:&nbsp;IReservationsRepository { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">EmailingReservationsRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IPostOffice&nbsp;<span style="color:#1f377f;">postOffice</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IReservationsRepository&nbsp;<span style="color:#1f377f;">inner</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PostOffice&nbsp;=&nbsp;postOffice; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inner&nbsp;=&nbsp;inner; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IPostOffice&nbsp;PostOffice&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IReservationsRepository&nbsp;Inner&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">Update</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;ArgumentNullException(nameof(reservation)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">existing</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Inner.ReadReservation(restaurantId,&nbsp;reservation.Id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(existing&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}&nbsp;&amp;&amp;&nbsp;existing.Email&nbsp;!=&nbsp;reservation.Email) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostOffice &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.EmailReservationUpdating(restaurantId,&nbsp;existing) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Inner.Update(restaurantId,&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostOffice.EmailReservationUpdated(restaurantId,&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Other&nbsp;members&nbsp;go&nbsp;here...</span> }</pre> </p> <p> You may think that it seems odd to have a 'repository' that also sends emails. I think that this is mostly an artefact of unfortunate naming. Perhaps a follow-up change should be to rename both the interface and the Controller's <code>Repository</code> property. I'm open to suggestions, but for now, I'll leave the names as they are. </p> <p> If you're still not convinced, consider an alternative architecture based on asynchronous message passing (e.g. CQRS). In such architectures, you'd put Commands on a message bus and think no more of it. A background process would then asynchronously perform all the actions, including sending emails and updating the data store. I think that people used to that kind of architecture wouldn't bat an eyelid by <code>bus.Send(new UpdateReservation(/**/))</code>. </p> <p> This would also be close to the kind of design that <a href="https://blogs.cuttingedge.it/steven">Steven van Deursen</a> and I describe in chapter 10 of <a href="/dippp">our book</a>. </p> <h3 id="23ba3b2153ee43dabc4a0fa964627dc0"> Simplification <a href="#23ba3b2153ee43dabc4a0fa964627dc0" title="permalink">#</a> </h3> <p> This greatly simplifies things. The above <code>Update</code> method now becomes redundant and can be deleted. Instead, <code>TryUpdate</code> can now directly call <code>Repository.Update</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&lt;ActionResult&gt;&nbsp;<span style="color:#74531f;">TryUpdate</span>( &nbsp;&nbsp;&nbsp;&nbsp;Restaurant&nbsp;<span style="color:#1f377f;">restaurant</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">scope</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;TransactionScope( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionScopeAsyncFlowOption.Enabled); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">existing</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservation(restaurant.Id,&nbsp;reservation.Id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(existing&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;NotFoundResult(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">ok</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;WillAcceptUpdate(restaurant,&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(!ok) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;NoTables500InternalServerError(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Update(restaurant.Id,&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;scope.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;OkObjectResult(reservation.ToDto()); }</pre> </p> <p> This also means that you can remove the <code>PostOffice</code> dependency from the Controller. Lots of things becomes simpler by this refactoring. It better separates concerns, so tests become simpler as well. </p> <h3 id="90227dd1059f432f9573cfe3c2553507"> Conclusion <a href="#90227dd1059f432f9573cfe3c2553507" title="permalink">#</a> </h3> <p> You can simplify code by composing Commands. Candidate patterns for this are Chain of Responsibility, Decorator, and Composite. These patterns, however, require a common polymorphic type. Key to refactoring to these patterns is to identify such a common interface. In this article, I used the refactored <code>IReservationsRepository</code> interface. </p> <p> Whenever a client calls a method on the repository, a change of state now automatically also sends emails. The client doesn't have to worry about that. </p> <p> Consider modelling many related side-effects as a single composed Command. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Structural equality for better tests https://blog.ploeh.dk/2021/05/03/structural-equality-for-better-tests 2021-05-03T05:45:00+00:00 Mark Seemann <div id="post"> <p> <em>A Fluent Builder as a Value Object?</em> </p> <p> If you've read a bit about unit testing, test-driven development, or other kinds of developer testing, you've probably come across a phrase like this: <blockquote> Test behaviour, not implementation. </blockquote> It's often taken to mean something like <a href="https://en.wikipedia.org/wiki/Behavior-driven_development">behaviour-driven development</a> (BDD), and that's certainly one interpretation. I've no problem with that. My own Pluralsight course <a href="/outside-in-tdd">Outside-In Test-Driven Development</a> shows a similar technique. </p> <p> It'd be a logical fallacy, however, to thereby conclude that you can only apply that ideal in the large, but not in the small. That it's only possible to do it with coarse-grained tests at the boundary of the system, but not with unit testing. </p> <p> It may be harder to do at the unit level, since when writing unit tests, you're closer to the implementation, so to speak. Writing the test before the implementation may, however, help </p> <p> <ins datetime="2021-06-15T06:14Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="fc448a62f581457392f12bd1fe2d273d"> An example test <a href="#fc448a62f581457392f12bd1fe2d273d" title="permalink">#</a> </h3> <p> Here's a test (using <a href="https://xunit.net">xUnit.net</a> 2.4.1) I wrote before the implementation: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;Home&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;Calendar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;Reservations&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">WithControllerHandlesSuffix</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">name</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;UrlBuilder(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.WithController(name&nbsp;+&nbsp;<span style="color:#a31515;">&quot;Controller&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;sut.WithController(name); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected,&nbsp;actual); }</pre> </p> <p> It tests an <a href="/2020/08/10/an-aspnet-core-url-builder">ASP.NET Core URL Builder</a>; particular how it deals with the <code>Controller</code> <a href="/2020/08/03/using-the-nameof-c-keyword-with-aspnet-3-iurlhelper">suffix issue I ran into last year</a>. </p> <p> Do you notice something odd about this test? </p> <p> It describes an equality relation between two individual projections of an initial <code>UrlBuilder</code> object (<a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/naming-sut-test-variables">sut</a>). </p> <p> First of all, with a <a href="/2020/02/10/builder-isomorphisms">Mutable Fluent Builder</a> the test would produce a <a href="http://xunitpatterns.com/false%20negative.html">false negative</a> because <a href="https://en.wikipedia.org/wiki/Aliasing_(computing)">aliasing</a> would make the assertion a <a href="/2019/10/14/tautological-assertion">tautological assertion</a>. Using an <a href="/2020/02/10/builder-isomorphisms">Immutable Fluent Builder</a>, however, elegantly dodges that bullet: <code>expected</code> and <code>actual</code> are two separate objects. </p> <p> Yet, it's possible to compare them. How? </p> <h3 id="ef79a890e2c3494d88f0202b687a8633"> Assertions <a href="#ef79a890e2c3494d88f0202b687a8633" title="permalink">#</a> </h3> <p> I think that most people would have written the above test like this: </p> <p> <pre>[Theory] [InlineData(<span style="color:#a31515;">&quot;Home&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;Calendar&quot;</span>)] [InlineData(<span style="color:#a31515;">&quot;Reservations&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">WithControllerHandlesSuffix</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">name</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;UrlBuilder(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;sut.WithController(name&nbsp;+&nbsp;<span style="color:#a31515;">&quot;Controller&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">expected</span>&nbsp;=&nbsp;sut.WithController(name); &nbsp;&nbsp;&nbsp;&nbsp;Assert.Equal(expected.Controller,&nbsp;actual.Controller); }</pre> </p> <p> Instead of comparing two whole objects, this variation compares the <code>Controller</code> property values from two objects. In order for this to compile, you have to expose an implementation detail: that the class has a class field (here exposed as an <a href="/2011/05/26/CodeSmellAutomaticProperty">automatic property</a>) that keeps track of the Controller name. </p> <p> I think that most object-oriented programmers' default habit is to write assertions that compare properties or class fields because in both C# and Java, objects by default only have reference equality. This leads to <a href="/2011/05/25/DesignSmellPrimitiveObsession">primitive obsession</a>, this time in the context of test assertions. </p> <p> Structural equality, on the other hand, makes it much easier to write concise and meaningful assertions. Just compare <code>expected</code> with <code>actual</code>. </p> <h3 id="b140911476e341dc83c421dc6eed70b3"> Structural equality on a Builder? <a href="#b140911476e341dc83c421dc6eed70b3" title="permalink">#</a> </h3> <p> The <code>UrlBuilder</code> class has structural equality by overriding <code>Equals</code> and <code>GetHashCode</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>?&nbsp;<span style="color:#1f377f;">obj</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;UrlBuilder&nbsp;<span style="color:#1f377f;">builder</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action&nbsp;==&nbsp;builder.action&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controller&nbsp;==&nbsp;builder.controller&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EqualityComparer&lt;<span style="color:blue;">object</span>?&gt;.Default.Equals(values,&nbsp;builder.values); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;HashCode.Combine(action,&nbsp;controller,&nbsp;values); }</pre> </p> <p> That's why the above <code>Assert.Equal</code> statement works. </p> <p> You may think that it's an odd choice to give a Fluent Builder structural equality, but why not? Since it's immutable, it's perfectly safe, and it makes things like testing much easier. </p> <p> I rarely see people do this. Even programmers experienced with functional programming often seem to categorise structural equality as something associated exclusively with <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> (ADTs). The <code>UrlBuilder</code> class, on the other hand, doesn't look like an ADT. After all, its public API exposes only behaviour, but no data: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;UrlBuilder&nbsp;<span style="color:#74531f;">WithAction</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">newAction</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;UrlBuilder&nbsp;<span style="color:#74531f;">WithController</span>(<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">newController</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;UrlBuilder&nbsp;<span style="color:#74531f;">WithValues</span>(<span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">newValues</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Uri&nbsp;<span style="color:#74531f;">BuildAbsolute</span>(IUrlHelper&nbsp;<span style="color:#1f377f;">url</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>?&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() }</pre> </p> <p> On the other hand, my threshold for when I give an immutable class structural equality is <a href="https://en.wikipedia.org/wiki/Monotonic_function">monotonically decreasing</a>. Structural equality just makes things easier. The above test is just one example. Structural equality enables you to test behaviour instead of implementation details. In this example, the behaviour can be expressed as an equality relation between two different inputs. </p> <h3 id="3843708f89fe46769d7c49af2977bf19"> UrlBuilder as an algebraic data type <a href="#3843708f89fe46769d7c49af2977bf19" title="permalink">#</a> </h3> <p> While it may seem odd or surprising to give a Fluent Builder structural equality, it's really isomorphic to a simple record type equipped with a few <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphisms</a>. (After all, we already know that <a href="/2020/02/17/builder-as-a-monoid">the Builder pattern is isomorphic to the endomorphism monoid</a>.) Let's make this explicit with <a href="https://fsharp.org">F#</a>. </p> <p> Start by declaring a record type with a <code>private</code> definition: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;UrlBuilder&nbsp;=&nbsp;<span style="color:blue;">private</span>&nbsp;{&nbsp;Action&nbsp;:&nbsp;string&nbsp;option;&nbsp;Controller&nbsp;:&nbsp;string&nbsp;option;&nbsp;Values&nbsp;:&nbsp;obj&nbsp;option&nbsp;}</pre> </p> <p> While its definition is <code>private</code>, it's still an algebraic data type. Records in F# automatically have structural equality, and so does this one. </p> <p> Since it's <code>private</code>, client code can't use the normal language constructs to create instances. Instead, the module that defines the type must supply an API that client code can use: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;emptyUrlBuilder&nbsp;=&nbsp;{&nbsp;Action&nbsp;=&nbsp;None;&nbsp;Controller&nbsp;=&nbsp;None;&nbsp;Values&nbsp;=&nbsp;None&nbsp;} <span style="color:blue;">let</span>&nbsp;withAction&nbsp;action&nbsp;ub&nbsp;=&nbsp;{&nbsp;ub&nbsp;<span style="color:blue;">with</span>&nbsp;Action&nbsp;=&nbsp;Some&nbsp;action&nbsp;} <span style="color:blue;">let</span>&nbsp;withController&nbsp;(controller&nbsp;:&nbsp;string)&nbsp;ub&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;index&nbsp;=&nbsp;controller.LastIndexOf&nbsp;(<span style="color:#a31515;">&quot;controller&quot;</span>,&nbsp;StringComparison.OrdinalIgnoreCase) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newController&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;0&nbsp;&lt;=&nbsp;index&nbsp;<span style="color:blue;">then</span>&nbsp;controller.Remove(index)&nbsp;<span style="color:blue;">else</span>&nbsp;controller &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;ub&nbsp;<span style="color:blue;">with</span>&nbsp;Controller&nbsp;=&nbsp;Some&nbsp;newController&nbsp;} <span style="color:blue;">let</span>&nbsp;withValues&nbsp;values&nbsp;ub&nbsp;=&nbsp;{&nbsp;ub&nbsp;<span style="color:blue;">with</span>&nbsp;Values&nbsp;=&nbsp;Some&nbsp;values&nbsp;}</pre> </p> <p> Without further <a href="/2019/12/16/zone-of-ceremony">ceremony</a> you can port the initial test to F# as well: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData(<span style="color:#a31515;">&quot;Home&quot;</span>)&gt;] [&lt;InlineData(<span style="color:#a31515;">&quot;Calendar&quot;</span>)&gt;] [&lt;InlineData(<span style="color:#a31515;">&quot;Reservations&quot;</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``withController&nbsp;handles&nbsp;suffix``&nbsp;name&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut&nbsp;=&nbsp;emptyUrlBuilder &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut&nbsp;|&gt;&nbsp;withController&nbsp;(name&nbsp;+&nbsp;<span style="color:#a31515;">&quot;Controller&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;sut&nbsp;|&gt;&nbsp;withController&nbsp;name &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> In addition to xUnit.net this test also uses <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> 6.0.0. </p> <p> Even though <code>UrlBuilder</code> has no externally visible data, it automatically has structural equality. <a href="/2015/05/07/functional-design-is-intrinsically-testable">Functional programming is, indeed, more test-friendly</a> than object-oriented programming. </p> <p> This F# implementation is equivalent to the <a href="/2020/08/10/an-aspnet-core-url-builder">C# UrlBuilder class</a>. </p> <h3 id="a59f8b304431456d94ef47e4a4481542"> Conclusion <a href="#a59f8b304431456d94ef47e4a4481542" title="permalink">#</a> </h3> <p> You can safely give immutable objects structural equality. Besides other advantages, it makes it easier to write tests. With structural equality, you can express a relationship between the expected and actual outcome using high-level language. </p> <p> These days, I don't really care if the type in question is a 'proper' algebraic data type. If it's immutable, I don't have to think much about it before giving it structural equality. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f71a94b456de4458bf99690bdf3c7376"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#f71a94b456de4458bf99690bdf3c7376">#</a></div> <div class="comment-content"> <blockquote> Records in F# automatically have structural equality, and so does this one. </blockquote> <p> That is mostly true but not compeltely so. Consider the type </p> <p> <code>type MyRecord = { MyField: int -> bool }</code> </p> <p> If you try to compare two instances with F#'s <code>=</code> operator, then you will get this compilier error. </p> <blockquote> Error FS0001: The type 'MyRecord' does not support the 'equality' constraint because it is a record, union or struct with one or more structural element types which do not support the 'equality' constraint. Either avoid the use of equality with this type, or add the 'StructuralEquality' attribute to the type to determine which field type does not support equality. </blockquote> <p> Adding the <code>StructuralEquality</code> attribute results in this compiler error. </p> <blockquote> Error FS1180: The struct, record or union type 'MyRecord' has the 'StructuralEquality' attribute but the component type '(int -> bool)' does not satisfy the 'equality' constraint. </blockquote> <p> I learned all this the hard way. I had added some F# functions to some of my models in my MVU architecture. Later when I tried to test my root model for structual equality, I ran into this issue. Taking the suggestion in the compiler error, I fixed the problem by adding the <code>StructuralEquality</code> attribute (as well as the <code>NoComparison</code> attribute) to my root model and refactored the code to fix the resulting compiler errors. </p> <p> During this time, I also realized that F#'s structual equality delegates to <code>object.Equals(object)</code> for types that extend <code>object</code>, which of course defaults to reference equality. For example, the following code compiles. </p> <p> <code>[&lt;StructuralEquality&gt;] [&lt;NoComparison&gt;] type MyRecord = { MyField: IDisposable }</code> </p> </div> <div class="comment-date">2021-05-04 11:49 UTC</div> </div> <div class="comment" id="ffedaf2b35744c439311d04f2adc0631"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ffedaf2b35744c439311d04f2adc0631">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Yes, you're right. Language is imprecise. F# records automatically have structural equality, when possible. </p> </div> <div class="comment-date">2021-05-05 4:48 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Leaky abstraction by omission https://blog.ploeh.dk/2021/04/26/leaky-abstraction-by-omission 2021-04-26T15:10:00+00:00 Mark Seemann <div id="post"> <p> <em>Sometimes, an abstraction can be leaky because it leaves something out.</em> </p> <p> Consider the following interface definition. What's wrong with it? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;Reservation?&gt;&nbsp;<span style="color:#74531f;">ReadReservation</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Update</span>(Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Delete</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>); }</pre> </p> <p> Perhaps you think that the name is incorrect; that this really isn't an example of the Repository design pattern, as it's described in <a href="http://bit.ly/patternsofeaa">Patterns of Enterprise Application Architecture</a>. Ironically, of all patterns, it may be the one most affected by <a href="https://martinfowler.com/bliki/SemanticDiffusion.html">semantic diffusion</a>. </p> <p> That's not what I have in mind, though. There's something else with that interface. </p> <p> It's not its <a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> design, either. You could consider that a leaky abstraction, since it strongly implies a sort of persistent data store. That's a worthwhile discussion, but not what I have in mind today. There's something else wrong with the interface. </p> <h3 id="998668fec2d9488db5a31a99c0c81774"> Consistency <a href="#998668fec2d9488db5a31a99c0c81774" title="permalink">#</a> </h3> <p> Look closer at the parameters for the various methods. The <code>Create</code> and <code>ReadReservations</code> methods take a <code>restaurantId</code> parameter, but the other three don't. </p> <p> Why does the <code>ReadReservations</code> method take a <code>restaurantId</code> while <code>ReadReservation</code> doesn't? Why is that parameter required for <code>Create</code>, but not for <code>Update</code>? That doesn't seem consistent. </p> <p> The reason is that each reservation has an ID (a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">GUID</a>). Once the reservation exists, you can uniquely identify it to read, update, or delete it. </p> <p> As the <code>restaurantId</code> parameter suggests, however, this interface is part of a multi-tenant code base. This code base implements an online restaurant reservation system as a <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> API. It's an online service where each restaurant is a separate tenant. </p> <p> While each reservation has a unique ID, the system still needs to associate it with a restaurant. Thus, the <code>Create</code> method must take a <code>restaurantId</code> parameter in order to associate the reservation with a restaurant. </p> <p> Once the reservation is stored, however, it's possible to uniquely identify it with the ID. The <code>ReadReservation</code>, <code>Update</code>, and <code>Delete</code> methods need only the <code>id</code> to work. </p> <p> On the other hand, when you're <em>not</em> querying on reservation ID, you'll need to identify the restaurant, as with the <code>ReadReservations</code> methods. If you didn't identify the restaurant in that method, you'd get all reservations in the requested range, from all tenants. That's not what you want. Therefore, you must supply the <code>restaurantId</code> to limit the query. </p> <p> The interface is inconsistent, but also allows the underlying implementation to leak through. </p> <h3 id="d270b5a98c7644389bc662287d8aa37e"> Implied implementation detail <a href="#d270b5a98c7644389bc662287d8aa37e" title="permalink">#</a> </h3> <p> If I told you that the implementation of <code>IReservationsRepository</code> is based on a relational database, can you imagine the design? You may want to stop reading and see if you can predict what the database looks like. </p> <p> The interface strongly implies a design like this: </p> <p> <pre><span style="color:blue;">CREATE</span>&nbsp;<span style="color:blue;">TABLE</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Reservations]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span> &nbsp;&nbsp;&nbsp;&nbsp;[Id]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">IDENTITY&nbsp;</span><span style="color:gray;">(</span>1<span style="color:gray;">,</span>&nbsp;1<span style="color:gray;">)</span>&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[At]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">DATETIME2&nbsp;</span><span style="color:gray;">(</span>7<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Name]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">NVARCHAR&nbsp;</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Email]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">NVARCHAR&nbsp;</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Quantity]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[PublicId]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">UNIQUEIDENTIFIER</span>&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[RestaurantId]&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">PRIMARY</span>&nbsp;<span style="color:blue;">KEY</span>&nbsp;<span style="color:blue;">CLUSTERED&nbsp;</span><span style="color:gray;">(</span>[Id]&nbsp;<span style="color:blue;">ASC</span><span style="color:gray;">),</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">CONSTRAINT</span>&nbsp;[AK_PublicId]&nbsp;<span style="color:blue;">UNIQUE</span>&nbsp;<span style="color:blue;">NONCLUSTERED&nbsp;</span><span style="color:gray;">(</span>[PublicId]&nbsp;<span style="color:blue;">ASC</span><span style="color:gray;">)</span> <span style="color:gray;">);</span></pre> </p> <p> What I wrote above is even clearer now. You can't create a row in that table without supplying a <code>RestaurantId</code>, since that column has a <code>NOT NULL</code> constraint. </p> <p> The <code>PublicId</code> column has a <code>UNIQUE</code> constraint, which means that you can uniquely read and manipulate a single row when you have an ID. </p> <p> Since all reservations are in a single table, any query <em>not</em> based on <code>PublicId</code> should also filter on <code>RestaurantId</code>. If it doesn't, the result set could include reservations from all restaurants. </p> <h3 id="646f0fb2adf147c6bca42d9aada6cf43"> Other interpretations <a href="#646f0fb2adf147c6bca42d9aada6cf43" title="permalink">#</a> </h3> <p> Is the above relational database design the only possible implementation? Perhaps not. You could implement the interface based on a document database as well. It'd be natural to store each reservation as a separate document with a unique ID. Again, once you have the ID, you can directly retrieve and manipulate the document. </p> <p> Other implementations become harder, though. Imagine, for example, that you want to <a href="https://en.wikipedia.org/wiki/Shard_(database_architecture)">shard</a> the database design: Each restaurant gets a separate database. Or perhaps, more realistically, you distribute tenants over a handful of databases, perhaps partitioned on physical location, or some other criterion. </p> <p> With such a design, the <code>ReadReservation</code>, <code>Update</code>, and <code>Delete</code> methods become more inefficient. While you should be able to identify the correct shard if you have a restaurant ID, <em>you don't have that information.</em> Instead, you'll have to attempt the operation on all databases, thereby eliminating most sharding benefits. </p> <p> In other words, the <em>absence</em> of the <code>restaurantId</code> parameter from some of the methods suggests certain implementation details. </p> <h3 id="5edc25ddf2c8439096577e8eaf45806c"> Leak by omission <a href="#5edc25ddf2c8439096577e8eaf45806c" title="permalink">#</a> </h3> <p> I admit that I rarely run into this sort of problem. Usually, a leaky abstraction manifests by a language construct that contains <em>too much</em> information. This is typically an interface or base class that exposes implementation details by either requiring too specific inputs, or by returning data that reveals implementation details. </p> <p> For a data access abstraction like the above 'repository', this most frequently happens when people design such an interface around an <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping">object-relational mapper</a> (ORM). A class like <code>Reservation</code> would then typically carry ORM details around. Perhaps it inherits from an ORM base class, or perhaps (this is very common) it has a parameterless constructor or getters and setters that model the relationships of the database (these are often called <em>navigation properties</em>). </p> <p> Another common examples of a leaky abstraction might be the presence of <code>Connect</code> and <code>Disconnect</code> methods. The <code>Connect</code> method may even take a <code>connectionString</code> parameter, clearly leaking that some sort of database is involved. </p> <p> Yet another example is <a href="/2014/08/11/cqs-versus-server-generated-ids">CQS-violating designs where a <code>Create</code> method returns a database ID</a>. </p> <p> All such leaky abstractions are leaky because they expose or require too much information. </p> <p> The example in this article, on the contrary, is leaky because of a lack of detail. </p> <h3 id="d6391eb7487d4b3ca38e4e5b1e868bd5"> Dependency Inversion Principle <a href="#d6391eb7487d4b3ca38e4e5b1e868bd5" title="permalink">#</a> </h3> <p> Ironically, I originally arrived at the above design because I followed the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> (DIP). The clients of <code>IReservationsRepository</code> are ASP.NET Controller actions, like this <code>Delete</code> method: </p> <p> <pre>[HttpDelete(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations/{id}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">Delete</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">id</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Guid.TryParse(id,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">rid</span>)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservation(rid) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Delete(rid).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(r&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostOffice.EmailReservationDeleted(restaurantId,&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As Robert C. Martin explains about the Dependency Inversion Principle: <blockquote> <p> "clients [...] own the abstract interfaces" </p> <footer><cite>Robert C. Martin, <a href="http://amzn.to/19W4JHk">APPP</a>, chapter 11</cite></footer> </blockquote> From that principle, it follows that the <code>Delete</code> method decides what <code>IReservationsRepository.Delete</code> looks like. It seems that the Controller action doesn't <em>need</em> to tell the <code>Repository</code> about the <code>restaurantId</code> when calling its <code>Delete</code> method. Supplying the reservation ID (<code>rid</code>) is enough. </p> <p> There are, however, various problems with the above code. If the DIP suggests that the <code>restaurantId</code> is redundant when calling <code>Repository.Delete</code>, then why is it required when calling <code>PostOffice.EmailReservationDeleted</code>? This seems inconsistent. </p> <p> Indeed it is. </p> <p> As I often do, I arrived at the above <code>Delete</code> method via <a href="/outside-in-tdd">outside-in TDD</a>, but as I observed a decade ago, <a href="/2010/12/22/TheTDDApostate">TDD alone doesn't guarantee good design</a>. Even when following the <a href="/2019/10/21/a-red-green-refactor-checklist">red-green-refactor checklist</a>, I often fail to spot problems right away. </p> <p> That's okay. TDD doesn't guarantee perfection, but done well it should set you up so that you can easily make changes. </p> <h3 id="b19d4f16b2ee48759c0e4d698a83cfdc"> Possible remedies <a href="#b19d4f16b2ee48759c0e4d698a83cfdc" title="permalink">#</a> </h3> <p> I can think of two ways to address the problem. The simplest solution is to make the interface consistent by adding a <code>restaurantId</code> parameter to all methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;Reservation?&gt;&nbsp;<span style="color:#74531f;">ReadReservation</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Guid&nbsp;<span style="color:#1f377f;">id</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Update</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Delete</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;Guid&nbsp;<span style="color:#1f377f;">id</span>); }</pre> </p> <p> This is the simplest solution, and the one that I prefer. In a future article, I'll show how it enabled me to significantly simplify the code base. </p> <p> For good measure, though, I should also mention the opposite solution. Completely drain the interface of <code>restaurantId</code> parameters: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Create</span>(Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;Reservation?&gt;&nbsp;<span style="color:#74531f;">ReadReservation</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Update</span>(Reservation&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Task&nbsp;<span style="color:#74531f;">Delete</span>(Guid&nbsp;<span style="color:#1f377f;">id</span>); }</pre> </p> <p> How can that work in practice? After all, an implementation <em>must</em> have a restaurant ID in order to create a new row in the database. </p> <p> It's possible to solve that problem by making the <code>restaurantId</code> an implementation detail. You could make it a constructor parameter for the concrete class, but this gives you another problem. Your <a href="/2011/07/28/CompositionRoot">Composition Root</a> doesn't know the restaurant ID - after all, it's a run-time argument. </p> <p> In a method like the above <code>Delete</code> Controller action, you'd have to translate the <code>restaurantId</code> run-time argument to an <code>IReservationsRepository</code> instance. There are various ways around that kind of problem, but they typically involve some kind of <em>factory</em>. That'd be yet another interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepositoryFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;IReservationsRepository&nbsp;<span style="color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>); }</pre> </p> <p> That just makes the API more complicated. Factories give <a href="/dippp">Dependency Injection</a> a bad reputation. For that reason, I don't like this second alternative. </p> <h3 id="ae975d7eff014d31b26ec78fa055f343"> Conclusion <a href="#ae975d7eff014d31b26ec78fa055f343" title="permalink">#</a> </h3> <p> Leaky abstractions usually express themselves as APIs that expose too many details; the implementation details leak through. </p> <p> In this example, however, a leaky abstraction manifested as a lack of consistency. Some methods require a <code>restaurantId</code> argument, while others don't - because one particular implementation doesn't need that information. </p> <p> It turned out, though, that when I was trying to simplify the overall code, this API design held me back. Consistently adding <code>restaurantId</code> parameters to all repository methods solved the problem. <a href="/2021/05/10/simplifying-code-with-decorated-commands">A future article tells that tale</a>. </p> <p> <ins datetime="2021-06-15T06:14Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c081a58ac3ea4c63a481f07c1933f325"> <div class="comment-author"><a href="https://tobias.rocks">Tobias</a> <a href="#c081a58ac3ea4c63a481f07c1933f325">#</a></div> <div class="comment-content"> <p>Thank you for the article Mark.</p> <p>I was wondering whether another solution would be including restaurantId as a member of Reservation? That way it’s not needed by the Create method. </p> <p>That just leaves ReadReservations as the last method that requires a restaurant ID, but one could argue a specialized read method such as this one doesn’t belong on a repository anyway. I personally tend to interpret these kinds of methods on a repository as a code smell on projects of a certain size. </p> <p>I might just be missing the point of your article, but I would love to hear your thoughts. :)</p> </div> <div class="comment-date">2021-05-01 8:59 UTC</div> </div> <div class="comment" id="c30f63e0ba234d15acecb40800efc0ec"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c30f63e0ba234d15acecb40800efc0ec">#</a></div> <div class="comment-content"> <p> Tobias, thank you for writing. You raise a good point, and it might be an appropriate way to model the problem. While the thought had already crossed my mind, I must admit that I hadn't given it much thought. </p> <p> For the individual CRUD operations, I admit that it might be an appropriate design option. You do, however, also point to the <code>ReadReservations</code> method as the odd man out. I applaud the intellectual honesty you exhibit by bring this up yourself, and I don't intend to misuse it by shooting down your idea. The fact that this method is somehow different might be an indication that it doesn't belong as a member of the same interface as the other four methods. </p> <p> If that's the case, though, then where does it belong? One option would be to <a href="/2014/03/10/solid-the-next-step-is-functional">define all interfaces with only a single method</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsDateRangeQuery</span> { &nbsp;&nbsp;&nbsp;&nbsp;Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">min</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>); }</pre> </p> <p> How should we deal with the <code>restaurantId</code> parameter in such an interface? Should it be included, as is the case here, or should we exclude it from the interface definition, like the following? </p> <p> <pre> Task&lt;IReadOnlyCollection&lt;Reservation&gt;&gt;&nbsp;<span style="color:#74531f;">ReadReservations</span>(DateTime&nbsp;<span style="color:#1f377f;">min</span>,&nbsp;DateTime&nbsp;<span style="color:#1f377f;">max</span>);</pre> </p> <p> If we choose to exclude the <code>restaurantId</code> parameter from the interface, it'd be consistent with the CRUD interface that you imply. On the other hand, wouldn't it require some sort of factory, as <a href="#b19d4f16b2ee48759c0e4d698a83cfdc">I outlined above</a>? </p> <p> Conversely, if we decide to keep the <code>restaurantId</code> parameter as part of the interface definition, it seems inconsistent with the design your suggestion implies. </p> <p> I'm not writing this to shoot down your suggestion. I find it a real conundrum. </p> <p> I do think, though, that this might be an indication that there's some kind of abstraction that I've failed to make explicit. Some kind of <em>Restaurant</em> or <em>Tenant</em> type seems most likely. </p> <p> My problem is that <a href="/2020/10/19/monomorphic-functors">I actually do have a <code>Restaurant</code> class</a> in my code base. That one, however, is a <a href="https://en.wikipedia.org/wiki/Value_object">Value Object</a>, so I'm loath to add impure methods to it. </p> <p> For what it's worth, it's deliberation like this that makes software design interesting. You need to balance opposing forces. A good design is one that does so in a stable way. I'm not claiming that the code shown here does that. You've clearly put your finger on a sore spot, which suggests to me that there's more work to be done. Thank you for the inspiring input! </p> </div> <div class="comment-date">2021-05-02 11:04 UTC</div> </div> <div class="comment" id="d4ed2be3914e46b0a1cc3e2f986854d2"> <div class="comment-author">Thibaut <a href="#d4ed2be3914e46b0a1cc3e2f986854d2">#</a></div> <div class="comment-content"> <p> Hi Mark, thank you for another great article! </p> <p> I have worked on several small multi-tenant systems and I faced the same problem as you with the repository interface methods and the "tenant id" being mixed. </p> <p> After several attempts and API iteration, my final design was to use what <em>Steven van Deursen</em> calls <a href="https://blogs.cuttingedge.it/steven/posts/2019/ambient-composition-model/">The Ambient Composition Model</a>. </p> <p> The idea is to inject an <code>ITenantContext</code> in the <code>IReservationsRepository</code> implementation and to use it as needed : </p> <p> <pre> public class ReservationsRepository : IReservationsRepository { private readonly ITenantContext _tenantContext; public ReservationRepository(ITenantContext tenantContext) { _tenantContext = tenantContex; } public Task Create(Reservation reservation) { var restaurantId = _tenantContext.RestaurantId; // ... } } </pre> </p> <p> In my case the implementation of the <code>ITenantContext</code> was retrieving the tenant from the route of the method. I think it could be the same for resolving the <code>restaurantId</code>. </p> <p> This solution seems similar to your Factory solution but I'm not totally sure. In any case, like the Factory solution, this solution is heavier that the first you proposed. </p> <p> Nonetheless I find some elegance in this solution with the tenant being injected by request in the implementation. What do you think? Did you have the same idea with the Factory solution? </p> </div> <div class="comment-date">2021-05-02 19:20 UTC</div> </div> <div class="comment" id="3104d83e33524adc9f4d349fa4df20ed"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3104d83e33524adc9f4d349fa4df20ed">#</a></div> <div class="comment-content"> <p> Thibaut, thank you for writing. Yes, that's another option. I've done something similar to that in the past. </p> <p> In a sense, the concept of a <em>tenant</em> seems almost like a cross-cutting concern, so it makes sense to let it fade into the background, so to speak. </p> <p> The reason I'm not too keen on that any longer is that it seems a bit too 'clever' in most settings. Consider the <code>Delete</code> Controller action <a href="#d6391eb7487d4b3ca38e4e5b1e868bd5">shown above</a>. Imagine that we inject <code>restaurantId</code> into all services - not only <code>IReservationsRepository </code>, but also into <code>IPostOffice</code>. The <code>Delete</code> method might look like this, then: </p> <p> <pre>[HttpDelete(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations/{id}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">Delete</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">id</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Guid.TryParse(id,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">rid</span>)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservation(rid) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Delete(rid).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(r&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostOffice.EmailReservationDeleted(r) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>restaurantId</code> parameter still has to be present, even though it's unused. This is likely to be puzzling to any developer not intimately familiar with the code base. </p> <p> It's possible that you can pull some trick with the ASP.NET framework so that the parameter doesn't have to be there, but it'll still be present in the URL, and again, I'm concerned that most developers would be confused about this. </p> <p> There's also another thing that bothers me about design like this: You can pull the restaurant ID out of the method's routing data, but this implies that you can do the same with the reservation ID. What makes the restaurant ID special, that it ought to be an injected dependency, while the reservation ID isn't? </p> <p> I'm concerned that the answer to that question might be 'hand-wavy'. And if we can't defend making one ID a dependency and the other not, then we might take this to the logical conclusion and inject all routing values into services. If we do that, the <code>Delete</code> method might now look like this: </p> <pre>[HttpDelete(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations/{id}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;Task&nbsp;<span style="color:#74531f;">Delete</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">id</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Repository.IsIdValid) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservation() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Delete().ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(r&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;PostOffice.EmailReservationDeleted(r) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> <p> (I haven't tried to compile this, so there may be syntax errors.) </p> <p> This seems really odd, although it's possible that we can salvage it by calling the dependency something else than <code>Repository</code>. It's not really a Unit of Work, but seems closer to that sort of design. </p> <p> I agree that a tenant feels like something that ought to be 'automatically handled', but I wonder whether it's possible to do that without making the code 'too clever'. </p> </div> <div class="comment-date">2021-05-04 8:26 UTC</div> </div> <div class="comment" id="76478b77b62e436aa2cbc96de85a5ff6"> <div class="comment-author"><a href="https://github.com/gonzaw">Gonzalo Waszczuk</a> <a href="#76478b77b62e436aa2cbc96de85a5ff6">#</a></div> <div class="comment-content"> <p> How would YAGNI come into play here? For instance, imagine your "client" code wasn't the Delete endpoint, but it was another app or endpoint that only had a "Guid reservationId", but <i>not</i> an "int restaurantId". In such case, wouldn't you be forced to add the restaurantId to the client code? What if this client code doesn't have an easy way to obtain such restaurantId? The reservation id is a global identifier, thus it makes sense that some application (be it a service, console, etc) would just get hold of the guid and nothing else, it's universally identifiable after all, it <i>should</i> be able to identify the reservation uniquely. This may require more roundtrips to the database, or forcing another client one-level above to provide the restaurantId (and this may even require politics and management to get in). </p> <p> Wouldn't YAGNI say that you shouldn't add the restaurantId to the API, since you ain't gonna need it? I.e, you likely won't change your data access implementation or shard the database in a way that would require that additional restaurantId, and even if you did, perhaps the development effort to add the restaurantId would be the same in that future timeline as it would be right now, so it would be the same to make this change now or afterwards (and in such case, wouldn't it make much sense to make the change afterwards, when you actually need it?). </p> </div> <div class="comment-date">2021-05-09 23:54 UTC</div> </div> <div class="comment" id="ccd61a106c8646dc950cf08bb5ad0b27"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ccd61a106c8646dc950cf08bb5ad0b27">#</a></div> <div class="comment-content"> <p> Gonzalo, thank you for writing. The short answer is that I only 'discovered' the leaky abstraction because I did, in fact, need the restaurant ID. As part of creating, modifying, or deleting reservations, I <a href="/2021/05/10/simplifying-code-with-decorated-commands">also wanted to send email updates</a>. For example, when updating a reservation, the system should send an email with a subject line like "Your reservation for Nono changed." </p> <p> This meant that I had to figure out which name to put in the subject line. Given the restaurant ID, this is trivial, but without it, the system would first have to make a 'reverse lookup' to find the restaurant associated with the reservation ID. While it's technically possible, it seems overly complicated given that the <code>restaurantId</code> was already available at the entry point. </p> <p> It's true that since the reservation ID is a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">GUID</a>, it's globally unique. This, however, is an implementation detail. The system <a href="/2020/10/26/fit-urls">doesn't allow external client to refer to a reservation exclusively by a GUID</a>. Rather, from the perspective of an external client, the ID of a reservation looks like <code>https://api.example.com/restaurants/90125/reservations/667fd3ca5d6943b993860809adc99ad5?sig=aqwnNXj28J547upBELNf5I6yPIrQ%2BG9DG2QIAlOpgOE%3D</code>. While both restaurant and reservation IDs are <em>visible</em> within that string, a client can't use those IDs. The external reservation ID is the full (conceptually opaque) string. </p> <p> I agree, though, that YAGNI is relevant in this context, too. If it's any consolation, I didn't change the code 'just in case' - I did, in fact, change it because I realised that I needed the modified design. But I admit that I didn't explicitly state that in this article. </p> <p> You may also find the <a href="#3104d83e33524adc9f4d349fa4df20ed">above discussion</a> relevant. </p> </div> <div class="comment-date">2021-05-11 6:56 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Consider including identity in URLs https://blog.ploeh.dk/2021/04/19/consider-including-identity-in-urls 2021-04-19T06:29:00+00:00 Mark Seemann <div id="post"> <p> <em>Automatically enable act-on-behalf-of capabilities to REST APIs.</em> </p> <p> In 2013 I published a series of API design tips called <a href="/2013/04/29/rest-lessons-learned">REST lessons learned</a>. Eight years have passed, but why not add another entry? </p> <p> This one I've known about for years, but never written down. I often use it when I consult teams, and each time I'm reminded that since this seems like a recurring piece of advice, I ought to write it down. </p> <h3 id="923c9c41ad0a40e7a76f1909a4aae8cc"> Nutshell <a href="#923c9c41ad0a40e7a76f1909a4aae8cc" title="permalink">#</a> </h3> <p> The problem, in a nutshell, relates to secured resources in a REST API. This could be any resource where the client must be authenticated before being able to access it. This design tip, however, seems to be mostly applicable when the resource in question itself represents an 'identity'. </p> <p> To scope the problem, API designers rarely falter when modelling resources that seems unrelated to security or identity. For example, if you're modelling a product catalogue and you want to enable some clients to edit the catalogue, it's clear to most people that a product is unrelated to the identity of the client. Thus, people naturally design URL schemes like <code>products/1234</code>, and that's fine. You can make a <code>PUT</code> request against <code>products/1234</code> to edit the resource, but you must supply credentials in order to do so. </p> <p> What if, however, you want to edit your own profile information? There might be a REST resource that exposes your user name, address, bio, avatar, etc. You want to make profile information editable. How do you design the API? </p> <p> API designers often design such an API based on a URL like <code>profile</code>, without any identifer in the URL. After all, a client must be authenticated in order to edit the resource, so the user ID will somehow be in the HTTP header (e.g. as a <a href="https://en.wikipedia.org/wiki/JSON_Web_Token">JSON Web Token</a> (JWT)). </p> <p> Consider, nonetheless, to include the identity in the URL. </p> <p> A profile resource, then, would follow a scheme like <code>profiles/1234</code>. Consider identifying tenant IDs in a multi-tenant system in the same way: <code>tenants/2345</code>. Do this even when other IDs follow: <code>tenants/2345/products/9876</code>. </p> <h3 id="211d2bcb8aea4f718df724e191afd92d"> Typical approach, not recommended <a href="#211d2bcb8aea4f718df724e191afd92d" title="permalink">#</a> </h3> <p> As outlined above, a typical design is to design an 'identity' resource without including the identification in the URL. If, for example, a client wants to change the avatar via a REST API, it might have to do it like this: </p> <p> <pre>PUT /users HTTP/1.1 Content-Type: application/json Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c[...] { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;bio&quot;</span>:&nbsp;&nbsp;<span style="color:#a31515;">&quot;Danish&nbsp;software&nbsp;design&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;avatar&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;ploeh.png&quot;</span> }</pre> </p> <p> The server-side code can extract the user ID and other authentication information from the <code>Bearer</code> token in the HTTP header. It can use this information to find the user ID and update its database. Technically, this gets the job done. </p> <p> I'll outline some potential problems with such a design in a moment, but first I'll show a second example. This one is more subtle. </p> <p> Imagine an online restaurant reservation system. The system enables guests to make reservations, edit them, and so on. When a potential guest attempts to make a reservation, the API should check if it can accept it. See <a href="/2020/01/27/the-maitre-d-kata">The Maître d' kata</a> for various conditions that may cause the restaurant to reject the reservation. One case might be that the reservation attempt is outside of the restaurant's opening hours. </p> <p> Perhaps the API should expose a management API that enables the restaurant's <a href="https://en.wikipedia.org/wiki/Ma%C3%AEtre_d%27h%C3%B4tel">maître d'hôtel</a> to change the opening hours. Perhaps you decide to design the API to look like this: </p> <p> <pre>PUT /restaurant HTTP/1.1 Content-Type: application/json Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c[...] { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;opensAt&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;18:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;lastSeating&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;21:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;seatingDuration&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;6:00&quot;</span> } </pre> </p> <p> Again, the <code>Bearer</code> token is supposed to contain enough information about the user to enable authentication and authorisation. This also gets the job done, but might paint you into a corner. </p> <h3 id="d57b7b148c824427b99ea26437425fd2"> Separation of concerns <a href="#d57b7b148c824427b99ea26437425fd2" title="permalink">#</a> </h3> <p> The problem with the above approach is that it fails to separate concerns. When modelling identity, it's easy to conflate the identity of the resource with the identity of the client interacting with it. Those are two separate concerns. </p> <p> What happens, for example, if you have so much success with the above restaurant reservation system that you decide to offer it as a multi-tenant service? </p> <p> I often see a 'solution' to such a requirement where API designers now require clients to supply a 'tenant ID' in the HTTP header. To make it secure, you should probably make it a claim in the JWT supplied via the <code>Authorization</code> header, or something to that effect. </p> <p> What's wrong with that? It conflates the identity of the client with the identity of the resource. This means that you can't easily enable capabilities where a client can act on behalf of someone else. </p> <p> Imagine, for example, that you have three restaurants, each a tenant: <em>Hipgnosta</em>, <em>Nono</em>, and <em>The Vatican Cellar</em>. It turns out, however, that <em>Hipgnosta</em> and <em>Nono</em> have the same owners, and share a single administrative employee. These restaurants wish to let that employee manage both restaurants. </p> <p> With the design outlined above, the employee would have to authenticate twice in order to make changes to both restaurants. That may not be a big deal for occasional edits to two restaurants, but imagine an employee who has to manage hundreds of franchises, and the situation becomes untenable. </p> <p> You should enable act-on-behalf-of capabilities. This may sound like speculative generality, but it's such a low-hanging fruit that I think you should enable it even if you don't need it right now. Just put the resource identity in the URL: <code>restaurants/456</code> and <code>users/1234</code>. </p> <p> Even for user profiles, putting the user ID in the URL enables one client to <em>view</em> (if not edit) other user profiles, which may or may not be desirable. </p> <p> The API should still demand that clients authenticate, but now you can distinguish the resource from the client making the request. This makes it possible for a client to act on behalf of others, given the right credentials. </p> <h3 id="4a51078a9a4745f8aef02993ebe52599"> Restaurant schedule example <a href="#4a51078a9a4745f8aef02993ebe52599" title="permalink">#</a> </h3> <p> I'll show you a slightly different example. Instead of editing a restaurant's opening or closing hours, I'll show you how the maître d' can view the schedule for a day. A <a href="/2020/10/05/fortunately-i-dont-squash-my-commits">previous article</a> already suggested that such a resource might exist in a code base I've recently written. A request and its response might look like this: </p> <p> <pre>GET /restaurants/1/schedule/2022/8/21 HTTP/1.1 Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXN0YXVyYW5[...] HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Hipgnosta&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;year&quot;</span>:&nbsp;2022, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;month&quot;</span>:&nbsp;8, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;day&quot;</span>:&nbsp;21, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;days&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-08-21&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;entries&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;19:45:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;reservations&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;0cced578fa21489bb0e3b5eb6be6825a&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-08-21T19:45:00.0000000&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;annekoicchamber@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Anne&nbsp;Kowics&nbsp;Chambers&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> I've simplified the example response by removing all links to make it more readable. After all, the shape of the response is irrelevant for this discussion. The point is the interaction between the request URL and the JWT. </p> <p> The request is against a URL that identifies the restaurant in question. The <code>1</code> after <code>restaurants</code> in <code>/restaurants/1/schedule/2022/8/21</code> identifies the restaurant as <em>Hipgnosta</em> to the API. (In reality, clients are expected to <a href="/2020/10/26/fit-urls">follow links. URLs are signed with HMACs</a>, but I've trimmed those off as well to simplify the example.) </p> <p> In this multi-tenant API, each restaurant is a separate tenant. Thus, the restaurant ID is really a tenant ID. The resource is fully identified via the URL. </p> <p> What about the client identity? It's supplied via the JWT, which decoded contains these claims: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;restaurant&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;2112&quot;</span> &nbsp;&nbsp;], &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;role&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;MaitreD&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;nbf&quot;</span>:&nbsp;1618301674, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;exp&quot;</span>:&nbsp;1618906474, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;iat&quot;</span>:&nbsp;1618301674 }</pre> </p> <p> Notice that the <code>restaurant</code> array contains a list of IDs that identify the tenants that the JWT can access. This particular JWT can access both restaurants <code>1</code> and <code>2112</code>, which correspond to <em>Hipgnosta</em> and <em>Nono</em>. This represents the shared employee who can act on behalf of both restaurants. </p> <h3 id="94a738f65941431a92072ee5d5f3af1f"> Access control <a href="#94a738f65941431a92072ee5d5f3af1f" title="permalink">#</a> </h3> <p> The API checks the that the incoming JWT has a <code>restaurant</code> claim that matches the incoming restaurant ID. Only if that's the case will it let the request through. </p> <p> <pre>[<span style="color:#2b91af;">HttpGet</span>(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/schedule/{year}/{month}/{day}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Get(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month,&nbsp;<span style="color:blue;">int</span>&nbsp;day) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!AccessControlList.Authorize(restaurantId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ForbidResult</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;the&nbsp;real&nbsp;work&nbsp;here...</span></pre> </p> <p> The above code fragment is a copy from <a href="/2021/02/01/aspnet-poco-controllers-an-experience-report">another article</a> where I already shared some of the server-side authorisation code. Here I'll show some of the code that I didn't show in the other article. <ins datetime="2021-06-15T06:14Z">The code is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <p> In the other article, you can see how the <code>AccessControlList</code> is populated from <code>HttpContext.User</code>, but I didn't show the implementation of the <code>FromUser</code> function. Here it is: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;AccessControlList&nbsp;<span style="color:#74531f;">FromUser</span>(ClaimsPrincipal&nbsp;<span style="color:#1f377f;">user</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">restaurantIds</span>&nbsp;=&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.FindAll(<span style="color:#a31515;">&quot;restaurant&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(<span style="color:#1f377f;">c</span>&nbsp;=&gt;&nbsp;ClaimToRestaurantId(c)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;AccessControlList(restaurantIds); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>[]&nbsp;<span style="color:#74531f;">ClaimToRestaurantId</span>(Claim&nbsp;<span style="color:#1f377f;">claim</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(<span style="color:blue;">int</span>.TryParse(claim.Value,&nbsp;<span style="color:blue;">out</span>&nbsp;var&nbsp;<span style="color:#1f377f;">i</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;i&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;Array.Empty&lt;<span style="color:blue;">int</span>&gt;(); }</pre> </p> <p> What you need to notice is just that the <code>FromUser</code> function finds and parses all the <code>"restaurant"</code> claims it can find. The <code>Authorize</code> method, subsequently, just looks for the incoming <code>restaurantId</code> among them: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Authorize</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;restaurantIds.Contains(restaurantId); }</pre> </p> <p> Thus, the identity of the resource is decoupled from the identity of the client. In this example, the client acts on behalf of two tenants, but since an array can hold an arbitrary number of values, there's no hard limit to how many tenants a single client could act on behalf of. </p> <h3 id="e753a5ee92e142ac8acfe096d1381b7f"> Conclusion <a href="#e753a5ee92e142ac8acfe096d1381b7f" title="permalink">#</a> </h3> <p> You don't always need act-on-behalf-of security features, but you never know if such a need might emerge in the future. You're going to need to check client credentials anyway, so the only extra step to avoid painting yourself into a corner is to put the resource identity in the URL - even if you believe that the resource identity and the client identity is the same. Such assumptions have a tendency to be proven wrong over time. </p> <p> I'm not usually a proponent of speculative generality, but I also think that it's prudent to consider overall return of investment. The cost of adding the resource identity to the URL is low, while having to change URL schemes later may carry a higher cost (even if you <a href="/2020/10/26/fit-urls">force clients to follow links</a>). </p> <p> This fits one view on software architecture: Make it as easy to make reactive changes to the system, but identify the areas where change will be hard; make good ex-ante decisions about those. </p> <p> Finally, I think that there's something fundamentally correct and consistent in putting user or tenant IDs in the URLs. After all, you put all other resource IDs (such as product IDs or customer IDs) in URLs. </p> <p> Notice, in the above schedule example, how the restaurant ID isn't the only ID. The URL also carries information about year, month, and date. These further identify the schedule resource. </p> <p> Putting user or tenant IDs in the URL effectively separates concerns. It enables you to discern the tenant or user from the client making the request. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Threading context through a catamorphism https://blog.ploeh.dk/2021/04/12/threading-context-through-a-catamorphism 2021-04-12T11:09:00+00:00 Mark Seemann <div id="post"> <p> <em>A problem solved after 1½ years.</em> </p> <p> You've probably noticed that it's easier to learn something new if it looks or sounds like something you already know. As a native Dane, I've found it easier to learn English and German than Russian and Japanese. If you originally were a Java or C# developer, you probably find <a href="https://en.wikipedia.org/wiki/JavaScript">JavaScript</a> more approachable than <a href="https://clojure.org">Clojure</a> or <a href="https://en.wikipedia.org/wiki/APL_(programming_language)">APL</a>. </p> <p> I believe that this extends to <a href="/2017/10/04/from-design-patterns-to-category-theory">design patterns and universal abstractions</a> as well. If code new to you follows well-known abstractions, it may be easier to learn than if it's structured in an entirely ad-hoc manner. This is my motivation for learning such universal abstractions as <a href="/2017/10/06/monoids">monoids</a>, <a href="/2018/03/22/functors">functors</a>, and <a href="/2019/04/29/catamorphisms">catamorphisms</a>. </p> <p> I particularly enjoy when it's possible to apply such abstractions to a proper problem. This occasionally happens. One example is my small article series on a <a href="/2019/08/26/functional-file-system">functional file system</a>. </p> <h3 id="2aa7c4b2f26840e7bfed2a42064853a7"> A fly in the ointment <a href="#2aa7c4b2f26840e7bfed2a42064853a7" title="permalink">#</a> </h3> <p> In those articles, I described how you could base most of the code on the <a href="/2019/08/05/rose-tree-catamorphism">rose tree catamorphism</a>. There was just one snag. There was one function, <code>calculateMoves</code>, that I was unable to implement with the catamorphism. In the article, I acknowledged my failure: <blockquote> "Earlier, I wrote that you can implement desired <code>Tree</code> functionality with the <code>foldTree</code> function, but that was a simplification. If you can implement the functionality of <code>calculateMoves</code> with <code>foldTree</code>, I don't know how." </blockquote> This was true for both <a href="/2019/09/09/picture-archivist-in-haskell">the Haskell proof of concept</a> as well as <a href="/2019/09/16/picture-archivist-in-f">the F# port</a>. </p> <p> <a href="https://about.me/tysonwilliams">Tyson Williams</a> and I <a href="/2019/09/16/picture-archivist-in-f#68b26807cc424856b8f762f214389826">discussed this wart</a> without getting closer to a solution. </p> <p> As the idiom goes, perfect is the enemy of good, so I decided to move on, although it nagged me. </p> <h3 id="641a9df4d9a24cb7bf687b041f8c305e"> Problem, condensed <a href="#641a9df4d9a24cb7bf687b041f8c305e" title="permalink">#</a> </h3> <p> The problem with the <code>calculateMoves</code> function was that it needed to thread a 'context' recursively through the entire data structure. In this case, the context was a file path. </p> <p> When <code>calculateMoves</code> runs over the input tree, it needs to thread a relative <code>path</code> through the function, building it up as it traverses the data structure. </p> <p> For example, if a leaf node named <code>1</code> is in a directory named <code>b</code>, which itself is a subdirectory of <code>a</code>, the relative path should be <code>a/b/1</code>. This example is straight from the test cases shown in both articles. You can also find the tests in the <a href="https://github.com/ploeh/picture-archivist">GitHub repository</a>. </p> <p> Each time <code>calculateMoves</code> visits a <code>Node</code> or <code>Leaf</code> it needs to know the parent <code>path</code> to calculate the destination path. As the articles show, this isn't too hard to do with regular pattern matching and recursion. </p> <p> I couldn't figure out, however, how to thread the <code>path</code> through the function when I tried to implement it with the catamorphism. </p> <h3 id="2461821e89084c5f8da43b2199d77d33"> Breakthrough <a href="#2461821e89084c5f8da43b2199d77d33" title="permalink">#</a> </h3> <p> While I'm ready to walk away from problems when I'm stuck, I tend to remember them. Sometimes, I run into a solution much later. </p> <p> This happened to me yesterday. I was trying to answer <a href="https://stackoverflow.com/q/67037663/126014">a Stack Overflow question</a> which was explicitly about the application of universal abstractions. Once more, I was stuck by being unable to thread a 'context' through a catamorphism. This time, instead of a <code>path</code>, the context was an indentation depth. Basically, the question was how to render a tree with proper indentation. </p> <p> Again, this isn't hard if you resort to explicit pattern matching and recursion, but I couldn't figure out how to do it via the data structure's catamorphism. </p> <p> Fortunately, <a href="https://stackoverflow.com/users/1364288/danidiaz">the user danidiaz</a> posted <a href="https://stackoverflow.com/a/67042881/126014">an awesome answer</a> while I was struggling. The answer uses a trick that I hadn't noticed before: It threads the indentation depth through the data structure by using the catamorphism to produce a structure map with <em>a function</em> as the carrier type. Specifically, danidiaz defines the algebra <code>Todo' (Int -&gt; String) -&gt; Int -&gt; String</code> to reduce a <code>Todo' (Int -&gt; String)</code> to an <code>Int -&gt; String</code> function. This function then gets initialised with the depth <code>0</code>. </p> <p> While I've been doing functional programming for years, I sometimes still tend to forget that functions are first-class values... </p> <p> This trick, though, seems to be universally applicable. If you need to thread a context through a catamorphism, define the algebra to work on <em>functions</em> that take the context as an argument. </p> <p> If this is a universally applicable trick, it also ought to work with the <code>calculateMoves</code> function. </p> <h3 id="f3e4908437ea4ab7bf0dd01d82ec470e"> Haskell re-implementation <a href="#f3e4908437ea4ab7bf0dd01d82ec470e" title="permalink">#</a> </h3> <p> In my <a href="https://www.haskell.org">Haskell</a> proof of concept, the <code>calculateMoves</code> function originally looked like this: </p> <p> <pre><span style="color:#2b91af;">calculateMoves</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">Move</span> calculateMoves&nbsp;=&nbsp;imp&nbsp;<span style="color:#a31515;">&quot;&quot;</span> &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;imp&nbsp;path&nbsp;&nbsp;&nbsp;&nbsp;(Leaf&nbsp;x)&nbsp;=&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;x&nbsp;$&nbsp;replaceDirectory&nbsp;x&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imp&nbsp;path&nbsp;(Node&nbsp;x&nbsp;xs)&nbsp;=&nbsp;Node&nbsp;(path&nbsp;&lt;/&gt;&nbsp;x)&nbsp;$&nbsp;imp&nbsp;(path&nbsp;&lt;/&gt;&nbsp;x)&nbsp;&lt;$&gt;&nbsp;xs</pre> </p> <p> It uses an <code>imp</code> (for <em>implementation</em>) function to explicitly <a href="/2015/12/01/recurse">recurse</a> over a <code>Tree FilePath FilePath</code>. Until yesterday, I couldn't come up with a better solution to thread the <code>path</code> through the data structure. </p> <p> The new trick suggests that it'd be possible to implement the function on <code>foldTree</code> (the catamorphism) by using <em>a function</em> as the carrier type. Since the context to be threaded through the catamorphism is a <code>String</code> (the <code>path</code>), the catamorphism should produce a function that takes a <code>String</code> as argument. In other words, the carrier type of the <code>Tree</code> should be <code>String -&gt; Tree FilePath Move</code>. </p> <p> Let's expand on this: The type of <code>foldTree</code> is <code>foldTree :: (a -&gt; [c] -&gt; c) -&gt; (b -&gt; c) -&gt; Tree a b -&gt; c</code>. Usually, I tend to think of the type parameter <code>c</code> as the type of some value, but since it's unconstrained, it can also be <em>a function</em>. That's what we need here: <code>c</code> should be <code>String -&gt; Tree FilePath Move</code>. </p> <p> That's not too hard to do, because of currying. Just write functions that take an extra <code>String</code> argument and pass them to <code>foldTree</code>: </p> <p> <pre><span style="color:#2b91af;">calculateMoves</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">Move</span> calculateMoves&nbsp;t&nbsp;=&nbsp;foldTree&nbsp;fNode&nbsp;fLeaf&nbsp;t&nbsp;<span style="color:#a31515;">&quot;&quot;</span> &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">fLeaf</span>&nbsp;::&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">Move</span> &nbsp;&nbsp;&nbsp;&nbsp;fLeaf&nbsp;x&nbsp;&nbsp;&nbsp;&nbsp;path&nbsp;=&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;x&nbsp;$&nbsp;replaceDirectory&nbsp;x&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">fNode</span>&nbsp;::&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">Move</span>]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">Move</span> &nbsp;&nbsp;&nbsp;&nbsp;fNode&nbsp;x&nbsp;fs&nbsp;path&nbsp;=&nbsp;Node&nbsp;(path&nbsp;&lt;/&gt;&nbsp;x)&nbsp;$&nbsp;($&nbsp;path&nbsp;&lt;/&gt;&nbsp;x)&nbsp;&lt;$&gt;&nbsp;fs</pre> </p> <p> Here I've used type annotations for the local functions, but that's entirely optional: </p> <p> <pre><span style="color:#2b91af;">calculateMoves</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">Move</span> calculateMoves&nbsp;t&nbsp;=&nbsp;foldTree&nbsp;fNode&nbsp;fLeaf&nbsp;t&nbsp;<span style="color:#a31515;">&quot;&quot;</span> &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;fLeaf&nbsp;x&nbsp;&nbsp;&nbsp;&nbsp;path&nbsp;=&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;x&nbsp;$&nbsp;replaceDirectory&nbsp;x&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;fNode&nbsp;x&nbsp;fs&nbsp;path&nbsp;=&nbsp;Node&nbsp;(path&nbsp;&lt;/&gt;&nbsp;x)&nbsp;$&nbsp;($&nbsp;path&nbsp;&lt;/&gt;&nbsp;x)&nbsp;&lt;$&gt;&nbsp;fs</pre> </p> <p> I included the type annotations to make it a little clearer what's going on. Recall that the type of <code>foldTree</code> is <code>foldTree :: (a -&gt; [c] -&gt; c) -&gt; (b -&gt; c) -&gt; Tree a b -&gt; c</code>. First consider the second of the two function arguments, the one I call <code>fLeaf</code> in the above code. It's the simplest of the two, so it makes sense to start with that one. </p> <p> The generic type of <code>fLeaf</code> is <code>b -&gt; c</code>. How does that map to the type of <code>fLeaf</code>, which is <code>FilePath -&gt; String -&gt; Tree FilePath Move</code>? </p> <p> Well, the <code>Tree</code> that the catamorphism runs on is a <code>Tree FilePath FilePath</code>. Mapped to the parametrically polymorphic type of <code>foldTree</code> that's <code>Tree a b</code>. In other words, <code>b</code> maps to <code>FilePath</code>. Thus, in order to fit the type of <code>b -&gt; c</code>, the type corresponding to <code>b</code> in <code>fLeaf</code> must be <code>FilePath</code>. What's left? <code>String -&gt; Tree FilePath Move</code> is what's left. The function takes a <code>FilePath</code> as input and returns a <code>String -&gt; Tree FilePath Move</code>. In other words, <code>c ~ String -&gt; Tree FilePath Move</code>. </p> <p> How does that fit with <code>fNode</code>? </p> <p> Generically, this function must have the type <code>a -&gt; [c] -&gt; c</code>. We've already established that <code>c</code> must be <code>String -&gt; Tree FilePath Move</code>. Since the catamorphism runs on a <code>Tree FilePath FilePath</code>, we also know that <code>a</code> must be <code>FilePath</code>. Thus, plugging in all the types, <code>fNode</code> must have the type <code>FilePath -&gt; [String -&gt; Tree FilePath Move] -&gt; String -&gt; Tree FilePath Move</code>. Note, particularly, that the second argument is a list of functions. That's why I decided to name the parameter <code>fs</code>, for <em>functions</em>. </p> <p> The entire expression <code>foldTree fNode fLeaf t</code>, then, has the type <code>String -&gt; Tree FilePath Move</code>, since <code>c</code> is <code>String -&gt; Tree FilePath Move</code> and the return type of <code>foldTree</code> is <code>c</code>. </p> <p> The final trick is to apply this function to the initial relative path <code>""</code>, which returns a <code>Tree FilePath Move</code>. </p> <p> This compiles and passes all tests. <code>calculateMoves</code> is now implemented using the <code>Tree</code> catamorphism, a goal that eluded me for more than one and a half years. </p> <h3 id="48fe66e3bffa413c8fe15e465581cb4e"> F# re-implementation <a href="#48fe66e3bffa413c8fe15e465581cb4e" title="permalink">#</a> </h3> <p> With the Haskell proof of concept in place, it's fairly trivial to port the new implementation to the <a href="https://fsharp.org">F#</a> code base. </p> <p> The <code>calculateMoves</code> function originally looked like this: </p> <p> <pre><span style="color:green;">//&nbsp;Tree&lt;string,FileInfo&gt;&nbsp;-&gt;&nbsp;Tree&lt;string,Move&gt;</span> <span style="color:blue;">let</span>&nbsp;calculateMoves&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;replaceDirectory&nbsp;(f&nbsp;:&nbsp;FileInfo)&nbsp;d&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInfo&nbsp;(Path.Combine&nbsp;(d,&nbsp;f.Name)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;imp&nbsp;path&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Leaf&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;{&nbsp;Source&nbsp;=&nbsp;x;&nbsp;Destination&nbsp;=&nbsp;replaceDirectory&nbsp;x&nbsp;path&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Node&nbsp;(x,&nbsp;xs)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newNPath&nbsp;=&nbsp;Path.Combine&nbsp;(path,&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tree.node&nbsp;newNPath&nbsp;(List.map&nbsp;(imp&nbsp;newNPath)&nbsp;xs) &nbsp;&nbsp;&nbsp;&nbsp;imp&nbsp;<span style="color:#a31515;">&quot;&quot;</span></pre> </p> <p> In the F# code base, the catamorphism is called <code>Tree.cata</code>, but otherwise looks like the Haskell <code>foldTree</code> function. The refactoring is also similar: </p> <p> <pre><span style="color:green;">//&nbsp;Tree&lt;string,&nbsp;FileInfo&gt;&nbsp;-&gt;&nbsp;Tree&lt;string,&nbsp;Move&gt;</span> <span style="color:blue;">let</span>&nbsp;calculateMoves&nbsp;t&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;FileInfo&nbsp;-&gt;&nbsp;string&nbsp;-&gt;&nbsp;FileInfo</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;replaceDirectory&nbsp;(f&nbsp;:&nbsp;FileInfo)&nbsp;d&nbsp;=&nbsp;FileInfo&nbsp;(Path.Combine&nbsp;(d,&nbsp;f.Name)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;FileInfo&nbsp;-&gt;&nbsp;string&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&nbsp;Move&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fLeaf&nbsp;x&nbsp;path&nbsp;=&nbsp;Leaf&nbsp;{&nbsp;Source&nbsp;=&nbsp;x;&nbsp;Destination&nbsp;=&nbsp;replaceDirectory&nbsp;x&nbsp;path&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;(string&nbsp;-&gt;&nbsp;Tree&lt;string,&nbsp;&#39;a&gt;)&nbsp;list&nbsp;-&gt;&nbsp;string&nbsp;-&gt;&nbsp;Tree&lt;string,&nbsp;&#39;a&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fNode&nbsp;x&nbsp;fs&nbsp;path&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newNPath&nbsp;=&nbsp;Path.Combine&nbsp;(path,&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tree.node&nbsp;newNPath&nbsp;(List.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;f&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;newNPath)&nbsp;fs) &nbsp;&nbsp;&nbsp;&nbsp;Tree.cata&nbsp;fNode&nbsp;fLeaf&nbsp;t&nbsp;<span style="color:#a31515;">&quot;&quot;</span></pre> </p> <p> Again, the expression <code>Tree.cata fNode fLeaf t</code> has the type <code>string -&gt; Tree&lt;string, Move&gt;</code>, so applying it to <code>""</code> produces a <code>Tree&lt;string, Move&gt;</code> return value. </p> <h3 id="21524d98dcbb4593abc18c55efbfd105"> Conclusion <a href="#21524d98dcbb4593abc18c55efbfd105" title="permalink">#</a> </h3> <p> I don't recall where I read the following, but I was under the impression that a data structure's catamorphism was its 'universal API', upon which you could implement any other functionality. I'd love it if it was true, but after my 2019 failure to implement <code>calculateMoves</code> via the <code>Tree</code> catamorphism, I wasn't sure if such a conjecture would hold. </p> <p> I still don't know if that assertion holds universally, but at least one reason to doubt it has now been removed. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1301010b460845db8730e4aa617504a4"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#1301010b460845db8730e4aa617504a4">#</a></div> <div class="comment-content"> <p> Excellent work Mark! I too had not forgotten about this, and it nagged me as well. </p> <p> To some extent, I feel like your explanation of how to implement <code>calculateMoves</code> via <code>Tree.cata</code> is top-down. By top-down, I mean it might depend on discovering the key idea of having <code>Tree.cata</code> return a function and then figuring out the correct type for that function. A good thing about such top-down approaches is being immediately aware that a better solution likely exists even if it takes some time and effort to find it. </p> <p> I was curious if a bottom-up approach would work. By bottom-up, I mean applying small refacorings to the code that are motivated by the principles, conventions, or style of functional programming. I do think I found such an approach. Of course it is a bit contradictory of me to only be able to find this approach after I read your presentation of the top-down approach. However, I am thinking of it like a kata. I now know such a bottom-up approach should be possible, and I want to find it. </p> <p> My bottom-up approach is in <a href="https://github.com/TysonMN/picture-archivist/tree/calculateMoves_via_cata_slow_refactor">this branch</a>. Here is a brief summary of how I want myself to think of making those commits in that order. </p> <p> Each case of the discriminated union could be extracted to its own function. This is easy to do in the <code>Leaf</code> case (so do it now), but it is not as easy to do in the <code>Node</code> case because of recursion, so delay that change for a bit. If we did extract both functions though, both functions would include the argument that I called <code>pathToParent</code>. Since it is passed in everywhere, it should be passed in nowhere (by eta reducing). To do that, we need it to be the last parameter to <code>imp</code>. After switching this order, we now deal with the recursion by doing it as soon as possible. Then the remaining code in that case can be extracted, and <code>imp</code> is essentially <code>Tree.cata</code>. </p> <p> In this approach, I never thought about the possibility of <code>Tree.cata</code> returning a function. It just sort of fell out as a consequence of my other changes. </p> </div> <div class="comment-date">2021-04-12 17:49 UTC</div> </div> <div class="comment" id="c90c8190c0f44e60a44fc605b9a84113"> <div class="comment-author"><a href="https://github.com/gonzaw">Gonzalo Waszczuk</a> <a href="#c90c8190c0f44e60a44fc605b9a84113">#</a></div> <div class="comment-content"> <p> Very nice! </p> <p> In Haskell there is a library called <a href="https://hackage.haskell.org/package/recursion-schemes-5.2.2.1">recursion-schemes</a> that showcases these types of recursion with catamorphisms, but also with many others recursion schemes. You can check it out and see if it gives you any new ideas. </p> <p> Regarding this use of catamorphism, the library itself I believe shows a very similar example <a href="https://hackage.haskell.org/package/recursion-schemes-5.2.2.1/docs/Data-Functor-Foldable.html">here</a>, using the Reader type (which is isomorphic to the function you used in your example): </p> <pre> >>> :{ let pprint2 :: Tree Int -> String pprint2 = flip runReader 0 . cataA go where go :: TreeF Int (Reader Int String) -> Reader Int String go (NodeF i rss) = do -- rss :: [Reader Int String] -- ss :: [String] ss <- local (+ 2) $ sequence rss indent <- ask let s = replicate indent ' ' ++ "* " ++ show i pure $ intercalate "\n" (s : ss) :} </pre> <pre> >>> putStrLn $ pprint2 myTree * 0 * 1 * 2 * 3 * 31 * 311 * 3111 * 3112 </pre> </div> <div class="comment-date">2021-04-14 02:27 UTC</div> </div> <div class="comment" id="d26feeefed69421499e766dc1737ca31"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d26feeefed69421499e766dc1737ca31">#</a></div> <div class="comment-content"> <p> Gonzalo, thank you for reminding me of the <em>recursion-schemes</em> library. It's one of those tomes of knowledge of which I'm aware, but never really have gotten around to look at... </p> </div> <div class="comment-date">2021-04-16 6:29 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Mazes on Voronoi tessellations https://blog.ploeh.dk/2021/04/05/mazes-on-voronoi-tesselations 2021-04-05T09:03:00+00:00 Mark Seemann <div id="post"> <p> <em>Recursive backtracker maze generation on a Voronoi diagram.</em> </p> <p> Today's blog post <a href="https://observablehq.com/@ploeh/mazes-on-voronoi-tessellations">appears on Observable</a>. It's an interactive environment where you can play with and fork the code. <a href="https://observablehq.com/@ploeh/mazes-on-voronoi-tessellations">Go there to read it</a>. </p> <p> <img src="/content/binary/recursive-backtracker-on-voronoi-tiles.png" alt="Recursive backtracker algorithm running on a Voronoi tessellation."> </p> <p> <a href="https://observablehq.com">Observable</a> is a really neat platform which has managed to do what I thought was nigh-impossible: make me return to <a href="https://en.wikipedia.org/wiki/JavaScript">JavaScript</a>. The site's <a href="https://observablehq.com/about">been around for some years</a>, and I hope it'll be around for even more years. </p> <p> <em>ploeh blog</em>, on the other hand, has been around <a href="/2009/01/28/LivingInInterestingTimes">since 2009</a>, and I intend to keep it around for much longer. Who knows if <em>Observable</em> will outlive the blog. Enjoy the article while it's there. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Table-driven tennis scoring https://blog.ploeh.dk/2021/03/29/table-driven-tennis-scoring 2021-03-29T06:15:00+00:00 Mark Seemann <div id="post"> <p> <em>Probably the most boring implementation of the tennis kata I've ever written.</em> </p> <p> Regular readers of this blog will know that I keep coming back to the <a href="https://codingdojo.org/kata/Tennis">tennis kata</a>. It's an interesting little problem to attack from various angles. </p> <p> The tennis scoring rules essentially describe a finite state machine, and while I was thinking about the state transitions involved, I came across an article by <a href="http://blog.mikemccandless.com">Michael McCandless</a> about <a href="http://blog.mikemccandless.com/2014/08/scoring-tennis-using-finite-state.html">scoring tennis using finite-state automata</a>. </p> <p> This isn't the first time I've thought about simply enumerating all possible states in the state machine, but I decided to spend half an hour on actually doing it. While Michael McCandless shows that an optimisation is possible, his minimised version doesn't enable us to report all intermediary states with the correct labels. For example, he optimises away <em>thirty-all</em> by replacing it with <em>deuce</em>. The end result is still the same, in the sense that the minimised state machine will arrive at the same winner for the same sequence of balls, but it can't correctly report the score while the game is in progress. </p> <p> For that reason, I decided to use his non-optimised state machine as a launch pad. </p> <h3 id="d32da6b59b7448959d6bed2f10f84569"> States <a href="#d32da6b59b7448959d6bed2f10f84569" title="permalink">#</a> </h3> <p> I used <a href="https://fsharp.org">F#</a> to enumerate all twenty states: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Score&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;LoveAll &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FifteenLove &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;LoveFifteen &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ThirtyLove &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FifteenAll &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;LoveThirty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FortyLove &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ThirtyFifteen &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FifteenThirty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;LoveForty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FortyFifteen &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ThirtyAll &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FifteenForty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;GamePlayerOne &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FortyThirty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ThirtyForty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;GamePlayerTwo &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;AdvantagePlayerOne &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Deuce &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;AdvantagePlayerTwo</pre> </p> <p> Utterly boring, yes, but perhaps boring code might be good code. </p> <h3 id="1eb656d32be0461aa11c321ec478a1cf"> Table-driven methods <a href="#1eb656d32be0461aa11c321ec478a1cf" title="permalink">#</a> </h3> <p> <a href="http://amzn.to/1dLYr0r">Code Complete</a> describes a programming technique called <em>table-driven methods</em>. The idea is to replace branching instructions such as <code>if</code>, <code>else</code>, and <code>switch</code> with a lookup table. The book assumes that the table exists in memory, but in this case, we can implement the table lookup with pattern matching: </p> <p> <pre><span style="color:green;">//&nbsp;Score&nbsp;-&gt;&nbsp;Score</span> <span style="color:blue;">let</span>&nbsp;ballOne&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;LoveAll&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;FifteenLove &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FifteenLove&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ThirtyLove &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;LoveFifteen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;FifteenAll &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ThirtyLove&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;FortyLove &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FifteenAll&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ThirtyFifteen &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;LoveThirty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;FifteenThirty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FortyLove&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;GamePlayerOne &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ThirtyFifteen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;FortyFifteen &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FifteenThirty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ThirtyAll &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;LoveForty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;FifteenForty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FortyFifteen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;GamePlayerOne &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ThirtyAll&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;FortyThirty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FifteenForty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ThirtyForty &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;GamePlayerOne&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;GamePlayerOne &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;FortyThirty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;GamePlayerOne &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ThirtyForty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Deuce &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;GamePlayerTwo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;GamePlayerTwo &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;AdvantagePlayerOne&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;GamePlayerOne &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Deuce&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;AdvantagePlayerOne &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;AdvantagePlayerTwo&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Deuce</pre> </p> <p> The <code>ballOne</code> function returns the new score when player one wins a ball. It takes the old score as input. </p> <p> I'm going to leave <code>ballTwo</code> as an exercise to the reader. </p> <h3 id="47bf74d3fafd478fafa354563459f0ad"> Smoke test <a href="#47bf74d3fafd478fafa354563459f0ad" title="permalink">#</a> </h3> <p> Does it work, then? Here's a few interactions with the API in <em>F# Interactive:</em> </p> <p> <pre>&gt; ballOne LoveAll;; val it : Score = FifteenLove &gt; LoveAll |&gt; ballOne |&gt; ballTwo;; val it : Score = FifteenAll &gt; LoveAll |&gt; ballOne |&gt; ballTwo |&gt; ballTwo;; val it : Score = FifteenThirty &gt; LoveAll |&gt; ballOne |&gt; ballTwo |&gt; ballTwo |&gt; ballTwo;; val it : Score = FifteenForty &gt; LoveAll |&gt; ballOne |&gt; ballTwo |&gt; ballTwo |&gt; ballTwo |&gt; ballOne;; val it : Score = ThirtyForty &gt; LoveAll |&gt; ballOne |&gt; ballTwo |&gt; ballTwo |&gt; ballTwo |&gt; ballOne |&gt; ballTwo;; val it : Score = GamePlayerTwo</pre> </p> <p> It looks like it's working. </p> <h3 id="17290385a4a042189ef0b5b04cf200c6"> Automated tests <a href="#17290385a4a042189ef0b5b04cf200c6" title="permalink">#</a> </h3> <p> Should I be writing unit tests for this implementation? </p> <p> I don't see how a test would be anything but a duplication of the two 'transition tables'. <em>Given that the score is thirty-love, when player one wins the ball, then the new score should be forty-love</em>. Indeed, the <code>ballOne</code> function already states that. </p> <p> We <a href="/2013/04/02/why-trust-tests">trust tests because they are simple</a>. When the implementation is as simple as the test that would exercise it, then what's the benefit of the test? </p> <p> To be clear, there are still compelling reasons to write tests for some simple implementations, but that's another discussion. I don't think those reasons apply here. I'll write no tests. </p> <h3 id="fb03ecc39926472790ba2c5eaa025a91"> Code size <a href="#fb03ecc39926472790ba2c5eaa025a91" title="permalink">#</a> </h3> <p> While this code is utterly dull, it takes up some space. In all, it runs to 67 lines of code. </p> <p> For comparison, the code base that evolves throughout my <a href="/2016/02/10/types-properties-software">Types + Properties = Software</a> article series is 65 lines of code, not counting the tests. When I also count the tests, that entire code base contains around 300 lines of code. That's more than four times as much code. </p> <p> Preliminary <a href="https://amzn.to/2OBNBoY">research implies that bug count correlates linearly with line count</a>. The more lines of code, the more bugs. </p> <p> While I believe that this is probably a simplistic rule of thumb, there's much to like about smaller code bases. In total, this utterly dull implementation is actually smaller than a comparable implementation built from small functions. </p> <h3 id="5ac7b217f375464aa21320c8f0bfe0a7"> Conclusion <a href="#5ac7b217f375464aa21320c8f0bfe0a7" title="permalink">#</a> </h3> <p> Many software problems can be modelled as finite state machines. I find that this is often the case in my own field of line-of-business software and web services. </p> <p> It's not always possible to exhaustively enumerate all states, because each 'type' of state carries data that can't practically be enumerated. For example, <a href="/2017/06/27/pure-times">polling consumers</a> need to carry timing statistics. These statistics influence how the state machine transitions, but the range of possible values is so vast that it can't be enumerated as types. </p> <p> It may not happen often that you can fully enumerate all states and transitions of a finite state machine, but I think it's worthwhile to be aware of such refactoring opportunities. It might make your code dully simple. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="57354557ce724eefb675d8788c5bea94"> <div class="comment-author"><a href="https://taeguk.co.uk">Dave Shaw</a> <a href="#57354557ce724eefb675d8788c5bea94">#</a></div> <div class="comment-content"> <p>Hi Mark, I have had a similar experience whilst coding a <a href="https://en.wikipedia.org/wiki/Shut_the_box"> Shut the box</a> game, when trying to detect if it was game over or not. <br> Originally it was a complex set of loops to calculate all the discrete summands for each roll of the dice, then checking if the remaining flaps were in that set. This was done along with a suite of tests for every possible combination set of summands up to 12 (for 2 dice). <br> Then whilst explaining the pain in writing this to a friend, they simply said, <i>there's only a finite list, why not hard code them?</i>, and that's what I went with, a dictionary with each possible roll from 2 dice, and the possible values from the flaps that could be used to meet that roll. All the tests were removed; as you pointed out, they would just be a reimplmentation of the table. </p> </div> <div class="comment-date">2021-04-07 13:30 UTC</div> </div> <div class="comment" id="858ccadaae1b41268d3bf4454ab9fcb0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#858ccadaae1b41268d3bf4454ab9fcb0">#</a></div> <div class="comment-content"> <p> Dave, thank you for writing. It's good to hear that you have a similar experience. I wonder if it's constrained to game simulation, or if 'real-world' examples exist. </p> </div> <div class="comment-date">2021-04-09 6:30 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The dispassionate developer https://blog.ploeh.dk/2021/03/22/the-dispassionate-developer 2021-03-22T06:50:00+00:00 Mark Seemann <div id="post"> <p> <em>Caring for your craft is fine, but should you work for free?</em> </p> <p> I've met many passionate developers in my career. Programmers who are deeply interested in technology, programming languages, methodology, and self-improvement. I've also seen many online profiles where people present themselves as 'passionate developers'. </p> <p> These are the people who organise and speak at user groups. They write blog posts and host podcasts. They contribute to open source development in their free time. </p> <p> I suppose that I can check many of those boxes myself. In the last few years, though, I've become increasingly sceptic that this is a good idea. </p> <h3 id="8894ee52bd514210a5f8193f38634f43"> Working for free <a href="#8894ee52bd514210a5f8193f38634f43" title="permalink">#</a> </h3> <p> In the last five years or so, I've noticed what looks like a new trend. Programmers contact me to ask about paid mentorship. They offer to pay me <em>out of their own pocket</em> to mentor them. </p> <p> I find that flattering, but it also makes me increasingly disenchanted with the software development industry. To be clear, this isn't an attack on the good people who care so much about their craft that they are willing to spend their hard-earned cash on improving their skill. This is more a reflection on employers. </p> <p> For reasons that are complicated and that I don't fully understand, the software development community in the eighties and nineties developed a culture of anti-capitalism and liberal values that put technology on a pedestal for its own sake. Open source good; commercial software bad. Free software good; commercial software bad. </p> <p> I'm not entirely unsympathetic to such ideas, but it seems clear, now, that these ideas have had unintended consequences. The idea of free software, for example, has led to a software economy where you, the user, are no longer the customer, but the product. </p> <p> The idea of open source, too, seems largely defunct as a means of 'sticking it to the man'. The big tech companies now embrace open source. Despite initial enmity towards open source, <a href="https://en.wikipedia.org/wiki/Microsoft_and_open_source">Microsoft now owns GitHub and is one of the most active contributors</a>. Google and Facebook control popular front-end platforms such as <a href="https://en.wikipedia.org/wiki/Angular_(web_framework)">Angular</a> and <a href="https://en.wikipedia.org/wiki/React_(JavaScript_library)">React</a>, as well as many other technologies such as <a href="https://en.wikipedia.org/wiki/Android_(operating_system)">Android</a> or <a href="https://en.wikipedia.org/wiki/GraphQL">GraphQL</a>. Continue the list at your own leisure. </p> <p> Developing open source is seen as a way to establish credibility, not only for companies, but for individuals as well. Would you like a cool job in tech? Show me your open-source portfolio. </p> <p> Granted, the focus on open-source contributions as a replacement for a CV seems to have peaked, and good riddance. </p> <p> I deliberately chose to use the word <em>portfolio</em>, above. Like a struggling artist, you're expected to show up with such a stunning sample of your work that you amaze your potential new employer and blow away your competition. Unlike struggling artists, though, you've already given away everything in your portfolio, and so have other job applicants. Employers benefit from this. You work for free. </p> <h3 id="2d37b0297422447c9c92e2f18828f5a7"> The passion ethos <a href="#2d37b0297422447c9c92e2f18828f5a7" title="permalink">#</a> </h3> <p> You're expected to 'contribute' to open source software. Why? Because employers want employees who are <em>passionate</em> about their craft. </p> <p> As you start to ponder the implied ethos, the stranger it gets. Would you like engineers to be <em>passionate</em> as they design new bridges? Would you like a surgeon to be <em>passionate</em> as she operates on you? Would you like judges to be <em>passionate</em> as they pass sentence on your friend? </p> <p> I'd like such people to <em>care</em> about their vocation, but I'd prefer that they keep a cool head and make as rational decisions as possible. </p> <p> Why should programmers be <em>passionate?</em> </p> <p> I don't think that it's in our interest to be passionate, but it <em>is</em> in employers' interest. Not only are passionate people expected to work for free, they're also easier to manipulate. Tell a passionate person something he wants to hear, and he may turn off further critical thinking because the praise <em>feels</em> good. </p> <p> Some open-source maintainers have created crucial software that runs everywhere. Companies make millions off that free software, while maintainers are often left with an increasing support burden and no money. </p> <p> They do, however, often get a pat on the back. They get invited to speak at conferences, and can add <em>creator of Xyz</em> to their social media bios. </p> <p> Until they burn out, that is. <ins datetime="2021-03-22T11:49Z"><em>Passion</em>, after all, comes from the Latin for <em>suffering</em></ins>. </p> <h3 id="192b9f90f1e94fcf97649edc5c234f39"> Self-improvement <a href="#192b9f90f1e94fcf97649edc5c234f39" title="permalink">#</a> </h3> <p> I remember consulting with a development organisation, helping them adopt some new technology. As my engagement was winding down, I had a meeting with the manager to discuss how they should be able to carry on without me. This was back in my Microsoft days, so I suggested that they institute a training programme for the employees. To give it structure, they could, for example, study for some Microsoft certifications. </p> <p> The development manager immediately shot down that idea: <em>"If we do that, they'll leave us once they have the certification."</em> </p> <p> I was flabbergasted. </p> <p> You've probably seen quotes like this: <blockquote> <p> "What happens if we train our people and they leave?" </p> <p> "What happens if we don't and they stay?" </p> </blockquote> This is one of those bon mots that seem impossible to attribute to a particular source, but the idea is clear enough. The sentiment doesn't seem to represent mainstream behaviour, though. </p> <p> Granted, I've met more than one visionary leader willing to invest in employees' careers, but most managers don't. </p> <p> While I teach and coach internationally, I naturally have more experience with my home region of Copenhagen, and more broadly Scandinavia. Here, it's a common position that anything that relates to work should only happen during work hours. If the employer doesn't allow training on the job, then most employees don't train. </p> <p> What happens if you don't keep up to date with new methodologies, new frameworks, new programming languages? Your skill set becomes obsolete. Not overnight, but over the years. Finding a new job becomes harder and harder. </p> <p> As your marketability atrophies, your employer can treat you worse and worse. After all, where are you going to go? </p> <p> If you're tired of working with legacy code without tests, most of your suggestions for improvements will be met by a shrug. <em>We don't have time for that now. It's more important to deliver value to the customer.</em> </p> <p> You'll have to work long hours and weekends fire-fighting 'unexpected' issues in production while still meeting deadlines. </p> <p> A sufficiently cynical employer may have no qualms keeping employees busy this way. </p> <p> To be clear, I'm not saying that it's good business sense to treat skilled employees like this, and I'm not saying that this is how <em>all</em> employers conduct business, but I've seen enough development organisations that fit the description. </p> <p> As disappointing as it may be, keeping up to date with technology is <em>your</em> responsibility, and if you can't sneak in some time for self-improvement at work, you'll have to do it on your own time. </p> <p> This has little to do with passion, but much to do with self-preservation. </p> <h3 id="a40b07d82eef4ee0a657df53662b4418"> Can I help you? <a href="#a40b07d82eef4ee0a657df53662b4418" title="permalink">#</a> </h3> <p> The programmers who contact me (and others) for mentorship are the enlightened ones who've already figured this out. </p> <p> That doesn't mean that I'm comfortable taking people's hard-earned money. If I teach you something that improves your productivity, your employer benefits, too. I think that your employer should pay for that. </p> <p> I'm aware that most companies don't want to do that. It's also my experience that while most employers couldn't care less whether you pay me for mentorship, they don't want you to show me their code. This basically means that I can't really mentor you, unless you can reproduce the problems you're having as anonymised code examples. </p> <p> But if you can do that, you can ask the whole internet. You can try asking on <a href="https://stackoverflow.com">Stack Overflow</a> and then ping me. You're also welcome to ask me. If your <a href="https://en.wikipedia.org/wiki/Minimal_working_example">minimal working example</a> is interesting, I may turn it into a blog post, and you pay nothing. </p> <p> People also ask me how they can convince their managers or colleagues to do things differently. I often wonder why they don't <a href="/2019/03/18/the-programmer-as-decision-maker">make technical decisions</a> already, but this may be my cultural bias talking. In Denmark you can often get away with the ask-for-forgiveness-rather-than-permission attitude, but it may not be a good idea in your culture. </p> <p> Can I magically convince your manager to do things differently? Not magically, but I do know an effective trick: get him or her to hire me (or another expensive consultant). Most people don't heed advice given for free, but if they pay dearly for it, they tend to pay attention. </p> <p> Other than that, I can only help you as I've passionately tried to help the world-wide community for decades: by blogging, answering questions on Stack Overflow, writing books, <a href="/schedule">speaking at user groups and conferences</a>, <a href="/about#video">publishing videos</a>, and so on. </p> <h3 id="2eb915d9d2aa411884599d592a310be0"> Ticking most of the boxes <a href="#2eb915d9d2aa411884599d592a310be0" title="permalink">#</a> </h3> <p> Yes, I know that I fit the mould of the <em>passionate developer</em>. I've blogged regularly since 2006, I've <a href="https://stackoverflow.com/users/126014/mark-seemann?tab=answers">answered thousands of questions on Stack Overflow</a>, I've given more than a hundred presentations, been a podcast guest, and co-written <a href="/dippp">a book</a>, none of which has made me rich. If I don't do it for the passion, then why do I do it? </p> <p> Sometimes it's hard and tedious work, but even so, I do much of it because I can't really help it. I <em>like</em> to write and teach. I suppose that makes me passionate. </p> <p> My point with this article isn't that there's anything wrong with being passionate about software development. The point is that you might want to regard it as a <em>weakness</em> rather than an asset. If you <em>are</em> passionate, beware that someone doesn't take advantage of you. </p> <p> I realise that I didn't view the world like this when I started blogging in January 2006. I was driven by my passion. In retrospect, though, I think that I have been both privileged and fortunate. I'm not sure my career path is reproducible today. </p> <p> When I started blogging, it was a new-fangled thing. Just the fact that you blogged was enough to get a little attention. I was in the right place at the right time. </p> <p> The same is true for Stack Overflow. The site was still fairly new when I started, and a lot of frequently asked questions were only asked on my watch. I still get upvotes on answers from 2009, because these are questions that people still ask. I was just lucky enough to be around <em>the first time</em> it was asked on the site. </p> <p> I'm also privileged by being an able-bodied man born into the middle class in one of the world's richest countries. I received a free education. Denmark has free health care and generous social security. Taking some chances with your career in such an environment isn't even reckless. I've worked for more than one startup. That's not risky here. Twice, I've worked for a company that went out of business; in none of those cases did I lose money. </p> <p> Yes, I've been fortunate, but my point is that you should probably not use my career as a model for yours, just as you shouldn't use those of <a href="https://en.wikipedia.org/wiki/Robert_C._Martin">Robert C. Martin</a>, <a href="https://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a>, or <a href="https://en.wikipedia.org/wiki/Martin_Fowler_(software_engineer)">Martin Fowler</a>. It's hardly a reproducible career path. </p> <h3 id="47dbcc3e81fc408881d0a173f5226f53"> Conclusion <a href="#47dbcc3e81fc408881d0a173f5226f53" title="permalink">#</a> </h3> <p> What <em>can</em> you do, then, if you want to stand out from the crowd? How <em>do</em> you advance your software development career? </p> <p> I don't know. I never claimed that this was easy. </p> <p> Being good at something helps, but you must also make sure that the right people know what you're good at. You're probably still going to have to invest some of your 'free' time to make that happen. </p> <p> Just beware that you aren't being taken advantage of. Be dispassionate. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c86dd3a1f4814201a7d15ba3b95c67c0"> <div class="comment-author"><a href="https://github.com/Jankowski-J">Jakub Jankowski</a> <a href="#c86dd3a1f4814201a7d15ba3b95c67c0">#</a></div> <div class="comment-content"> <p> Thanks Mark for your post. </p> <p> I really relate to your comment about portfolio. I am still a young developer, not even 30 years old. A few years ago, I had an unhealthy obsession, that I should have a portfolio, otherwise I would be having a hard time finding job. </p> <p> I am not entirely sure where this thought was coming from, but it is not important in what I want to convey. I was worrying that I do not have a portfolio and that anxiety itself, prevented me from doing any real work to have anything to showcase. Kinda vicious cycle. </p> <p> Anyways, even without a portfolio, I didn't have any troubles switching jobs. I focused on presenting what I have learned in every project I worked on. What was good about it, what were the struggles. I presented myself not as a just a "mercenary" if you will. I always gave my best at jobs and at the interviews and somehow managed to prove to myself that a portfolio is not a <em>must</em>. </p> <p> Granted, everybody's experience is different and we all work in different market conditions. But my takeaway is - don't fixate on a thing, if it's not an issue. That's kinda what I was doing a few years back. </p> </div> <div class="comment-date">2021-03-28 16:25 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pendulum swing: pure by default https://blog.ploeh.dk/2021/03/15/pendulum-swing-pure-by-default 2021-03-15T06:47:00+00:00 Mark Seemann <div id="post"> <p> <em>Favour pure functions over polymorphic dependencies.</em> </p> <p> This is an article in <a href="/2021/02/22/pendulum-swings">a small series of articles about personal pendulum swings</a>. Here, I'll discuss another contemporary one-eighty. This one is older than the other two I've discussed in this article series, but I believe that it deserves to be included. </p> <p> Once upon I time, I used to consider Dependency Injection (DI) and injected interfaces an unequivocal good: the more, the merrier. These days, I tend to only model true application dependencies as injected dependencies. For the rest, I use <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> <h3 id="52438454eb1747cb81e897e1a44dfa1b"> Background <a href="#52438454eb1747cb81e897e1a44dfa1b" title="permalink">#</a> </h3> <p> When I started my programming career, I'd barely taught myself to program. I worked in both Visual Basic, VBScript, and C++ before I encountered the concept of an interface. What C++ I wrote was entirely procedural, and I don't recall being aware of inheritance. Visual Basic 6 didn't have inheritance, and I'm fairly sure that VBScript didn't, either. </p> <p> I vaguely recall first being introduced to the concept of an interface in Visual Basic. It took me some time to wrap my head around it, and while I thought it seemed clever, I couldn't find any practical use for it. </p> <p> I think that I wrote my first professional C# code base in 2002. We didn't use Dependency Injection or interfaces. I don't even recall that we used much inheritance. </p> <h3 id="cae8dc76412149dbbe4eb4854888f652"> Inject all the things <a href="#cae8dc76412149dbbe4eb4854888f652" title="permalink">#</a> </h3> <p> When I discovered test-driven development (TDD) the year after, it didn't take me too long to figure out that I'd need to isolate units from their dependencies. Based on initial successes, I even wrote <a href="https://docs.microsoft.com/archive/msdn-magazine/2004/october/unit-testing-mock-objects-to-the-rescue-test-your-net-code-with-nmock">an article about mock objects</a> for MSDN Magazine October 2004. </p> <p> At that time I'd made interfaces a part of my active technique. I still struggled with how to replace a unit's 'real' dependencies with the mock objects. Initially, I used what I in <a href="https://amzn.to/36xLycs">Dependency Injection in .NET</a> later called <em>Bastard Injection</em>. As I also described in the book, things took a dark turn for while as I discovered the <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator anti-pattern</a> - only, at that time, I didn't realise that it was an anti-pattern. Soon after, fortunately, I discovered <a href="/2014/06/10/pure-di">Pure DI</a>. </p> <p> That problem solved, I began an era of my programming career where everything became an interface. It does enable unit testing, so it's better than not being able to test, but after some years I began to sense the limits. </p> <p> Perhaps the worst problem is that you get a deluge of interfaces. Many of these interfaces have similar-sounding names like <code>IReservationsManager</code> and <code>IRestaurantManager</code>. This makes discoverability harder: Which of these interfaces should you use? One defines <a href="/2020/11/23/good-names-are-skin-deep">a <code>TrySave</code> method, the other a <code>Check</code> method</a>, and they aren't that different. </p> <p> This wasn't clear to me when I worked in teams with one or two programmers. Once I saw how this played out in larger teams, however, I began to understand that one developer's interface remained undiscovered by other team members. When existing 'abstractions' are unclear, it leads to frequent reinvention of interfaces to implement the same functionality. Duplication abounds. </p> <p> Designing with many fine-grained dependencies also has a tendency drag into existence many <em>factory interfaces</em>, a <a href="https://blogs.cuttingedge.it/steven/posts/2016/abstract-factories-are-a-code-smell">well-known design smell</a>. </p> <h3 id="b29309513d1946238cb08718f6be8903"> Have a sandwich <a href="#b29309513d1946238cb08718f6be8903" title="permalink">#</a> </h3> <p> It's remarkable how effectively you can lie to yourself. As late as 2017 <a href="https://softwareengineering.stackexchange.com/a/356822/19115">I still concluded that fine-grained dependencies were best</a>, despite most of my arguments pointing in another direction. </p> <p> I first encountered functional programming in 2010, but was off to a slow start. It took me years before I realised that <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">Dependency Injection isn't functional</a>. There are other ways to address the problem of separating pure functions from impure actions, the simplest of which is the <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>. </p> <p> Which parts of the application architecture are inherently impure? The usual suspects: the system clock, random number generators, the file system, databases, network resources. Notice how these are the dependencies that you usually need to replace with <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a> in order to make unit tests deterministic. </p> <p> It makes sense to model these as dependencies. I still define interfaces for those and use Dependency Injection to control them. I do, however, use the impureim sandwich architecture to deal with the impure actions first, so that I can then delegate all the complex decision logic to pure functions. </p> <p> <a href="/2015/05/07/functional-design-is-intrinsically-testable">Pure functions are intrinsically testable</a>, so that solves many of the problems with testability. There's still a need to test how the impure actions interact with the pure functions. Here I take a step up in the <a href="https://martinfowler.com/bliki/TestPyramid.html">Test Pyramid</a> and write just enough <a href="/2019/02/18/from-interaction-based-to-state-based-testing">state-based integration tests</a> to render it probable that the integration works as intended. You can see an example of such a test <a href="/2021/01/18/parametrised-test-primitive-obsession-code-smell">here</a>. </p> <h3 id="bb9264be0f1648b48f6572e1054fab95"> Conclusion <a href="#bb9264be0f1648b48f6572e1054fab95" title="permalink">#</a> </h3> <p> From having favoured fine-grained Dependency Injection, I now write all decision logic as pure functions by default. These only need to implement interfaces if you need the <em>logic</em> of the system to be interchangeable, which isn't that often. I do still use Dependency Injection for the impure dependencies of the system. There's usually only a handful of those. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pendulum swing: sealed by default https://blog.ploeh.dk/2021/03/08/pendulum-swing-sealed-by-default 2021-03-08T07:28:00+00:00 Mark Seemann <div id="post"> <p> <em>Inheritance is evil. Seal your classes.</em> </p> <p> This is an article in <a href="/2021/02/22/pendulum-swings">a small series of articles about personal pendulum swings</a>. Here, I document another recent change of heart that's been a long way coming. In short, I now <code>seal</code> C# classes whenever I remember to do it. </p> <p> <ins datetime="2021-06-15T06:14Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="1f7f814e8f314fd291ad5fd73b12c7bf"> Background <a href="#1f7f814e8f314fd291ad5fd73b12c7bf" title="permalink">#</a> </h3> <p> After I discovered test-driven development (TDD) (circa 2003) I embarked on a quest for proper ways to enable testability. <a href="https://martinfowler.com/articles/nonDeterminism.html">Automated tests should be deterministic</a>, but real software systems rarely are. Software depends on the system clock, random number generators, the file system, the states of databases, web services, and so on. All of these may change independently of the software, making it difficult to express an automated systems test in a deterministic manner. </p> <p> This is a known problem in TDD. In order to get the system under test (SUT) under control, you have to <a href="http://bit.ly/working-effectively-with-legacy-code">introduce what Michael Feathers calls <em>seams</em></a>. In C#, there's traditionally been two ways you could do that: <a href="/2016/06/15/sut-double">extract and override</a>, and interfaces. </p> <p> The original <a href="http://amzn.to/16E4q3z">Framework Design Guidelines</a> explicitly recommended base classes over interfaces, and I wasn't wise to how unfortunate that recommendation was. For a long time, I'd define abstractions with (abstract) base classes. I was even envious of Java, where instance members are virtual (overridable) by default. In C# you must explicitly declare a method <code>virtual</code> to make it overridable. </p> <p> Abstract base classes aren't too bad if you leave them completely empty, but I never had much success with non-abstract base classes and virtual members and the whole extract-and-override manoeuvre. I soon concluded that <a href="/2016/06/15/sut-double">Dependency Injection with interfaces</a> was a better alternative. </p> <p> Even after I changed to exclusively relying on interfaces (instead of abstract base classes), remnants of the rule stuck with me for years: <em>unsealed good; sealed bad</em>. Even today, the framework design guidelines favour unsealed classes: <blockquote> <p> "CONSIDER using unsealed classes with no added virtual or protected members as a great way to provide inexpensive yet much appreciated extensibility to a framework." </p> <footer><cite><a href="https://docs.microsoft.com/dotnet/standard/design-guidelines/unsealed-classes">Framework Design Guidelines</a></cite></footer> </blockquote> I can no longer agree with this guidance; I think it's poor advice. </p> <h3 id="ae5513333afc437fb4ac795c2af8a4a1"> You don't need inheritance <a href="#ae5513333afc437fb4ac795c2af8a4a1" title="permalink">#</a> </h3> <p> Base classes imply class inheritance as a reuse and extensibility mechanism. We've known since 1994, though, that inheritance probably isn't the best design principle. <blockquote> <p> "Favor object composition over class inheritance." </p> <footer><cite><a href="http://amzn.to/XBYukB">Design Patterns</a></cite></footer> </blockquote> In single-inheritance languages like C# and Java, inheritance is just evil. Once you decide to inherit from a base class, you exclude all other base classes. <em>Inheritance signifies a single 'yes' and an infinity of 'noes'.</em> This is particularly problematic if you rely on inheritance for reuse. You can only 'reuse' a single base class, which again leads to duplication or bloated base classes. </p> <p> It's been years (probably more than a decade) since I stopped relying on base classes for anything. You don't need inheritance. <a href="https://www.haskell.org">Haskell</a> doesn't have it at all, and I only use it in C# when a framework forces me to derive from some base class. </p> <p> There's little you can do with an abstract class that you can't do in some other way. <a href="/2018/02/19/abstract-class-isomorphism">Abstract classes are isomorphic with Dependency Injection up to accessibility.</a> </p> <h3 id="516f72a613aa4884a86448da1c256ebb"> Seal <a href="#516f72a613aa4884a86448da1c256ebb" title="permalink">#</a> </h3> <p> If I already follow a design principle of not relying on inheritance, then why keep classes unsealed? <a href="https://www.python.org/dev/peps/pep-0020">Explicit is better than implicit</a>, so why not make that principle visible? Seal classes. </p> <p> It doesn't have any immediate impact on the code, but it might make it clearer to other programmers that an explicit decision was made. </p> <p> You already saw examples in the <a href="/2021/03/01/pendulum-swing-internal-by-default">previous article</a>: Both <code>Month</code> and <code>Seating</code> are sealed classes. They're also immutable records. I seal more than record types, too: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">HomeController</span></pre> </p> <p> I seal Controllers, as well as services: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SmtpPostOffice</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IPostOffice</span></pre> </p> <p> Another example is <a href="/2020/11/09/checking-signed-urls-with-aspnet">an ASP.NET filter</a> named <code>UrlIntegrityFilter</code>. </p> <p> A common counter-argument is that 'you may need extensibility in the future': <blockquote> <p> "by using "sealed" and not virtual in libs dev says "I thought of all extension point" which seems arrogant" </p> <footer><cite><a href="https://twitter.com/dun3/status/586790512528596992">Tobias Hertkorn</a></cite></footer> </blockquote> I agree that it'd be arrogant to claim that you've thought about all extension points. Trying to predict future need is futile. </p> <p> I don't agree, however, that making everything virtual is a good idea, but it's because I disagree with the underlying premise. The presupposition is that extensibility should be enabled through inheritance. If it's not already clear, I believe that this has many undesirable consequences. There are better ways to enable extensibility than through inheritance. </p> <h3 id="3ab4e94848d742aa8e3b12377415e8d1"> Conclusion <a href="#3ab4e94848d742aa8e3b12377415e8d1" title="permalink">#</a> </h3> <p> I've begun to routinely seal new classes. I don't always remember to do it, but I think that I ought to. As I also explained in the previous article, this is only my default. If something has to be a base class, that's still an option. Likewise, just because a class starts out <code>sealed</code> doesn't mean that it has to stay <code>sealed</code> forever. While sealing an unsealed class is a breaking change, unsealing a sealed class isn't. </p> <p> I can't think of any reason why I'd do that, though. </p> <p> <strong>Next: </strong> <a href="/2021/03/15/pendulum-swing-pure-by-default">Pendulum swing: pure by default</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pendulum swing: internal by default https://blog.ploeh.dk/2021/03/01/pendulum-swing-internal-by-default 2021-03-01T08:26:00+00:00 Mark Seemann <div id="post"> <p> <em>Declare new C# classes as internal by default, and public by choice.</em> </p> <p> This is an article in <a href="/2021/02/22/pendulum-swings">a small series of articles about personal pendulum swings</a>. Here, I document a recent change of heart that's been a long way coming. In short, I now declare C# classes as <code>internal</code> unless they're driven by tests. </p> <p> <ins datetime="2021-06-15T06:14Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="199c0f3d9cdc4ee5a6e93ce54a3f39a8"> Background <a href="#199c0f3d9cdc4ee5a6e93ce54a3f39a8" title="permalink">#</a> </h3> <p> When you create a new class in Visual Studio, the default accessibility is <code>internal</code>. In fact, Visual Studio's default templates don't add an access modifier at all, but if no access modifier is present, it implies <code>internal</code>. </p> <p> When I started out programming C#, I don't recall thinking much about accessibility modifiers. By default, then, I'd be using mostly <code>internal</code> classes. What little I knew about encapsulation (<em>information hiding</em>, anyone?) led me to believe that the more <code>internal</code> my code was, the better encapsulation it had. </p> <p> It's possible that I make my past self more ignorant than I actually was. It's almost twenty years ago: I don't recall all the details. </p> <h3 id="c98c637b240f4f0c97d6fc4d0f7d10f2"> Public all the things <a href="#c98c637b240f4f0c97d6fc4d0f7d10f2" title="permalink">#</a> </h3> <p> When I discovered test-driven development (TDD) (circa 2003) all my classes became public. They had to. When tests are interacting with code in another library, they can only exercise the system under test (SUT) if they can reach it. The tests make the SUT classes public. </p> <p> Yes, it's technically possible to test <code>internal</code> classes in .NET, but <a href="/2015/09/22/unit-testing-internals">I don't believe that you should</a>. I've yet to change my mind about that; no imminent pendulum swing there. You're testing something you <em>care</em> about. If the <code>internal</code> code serves any, <em>any</em>, purpose, it must be somehow observable. If so, verify that such observable behaviour takes place; if not, delete the code. (I'm sure you can dream up some corner cases where this doesn't hold; fine: I'm painting with a broad brush, here.) </p> <p> For years, I applied TDD, but I wasn't aware of the red-green-refactor cycle. I rarely changed the public API that the tests interacted with, and when I did, I made sure to adjust the tests accordingly. If a refactoring gave rise to new classes, I'd often write tests for those new classes as well. </p> <p> Imagine, for example, invoking the <em>Extract Class</em> refactoring. The new class would be as covered by tests as before the extraction, but what happens next is typically that you need to tweak it. When that happened to me, I'd typically write completely new tests to cover it. To do that, I'd need the extracted class to be <code>public</code>. </p> <p> In this phase of my professional life, my classes were almost exclusively <code>public</code>, with <code>internal</code> classes only making a rare appearance. </p> <p> One problem this tends to cause is that it makes code bases more brittle. Every type change is a potential breaking change. When every public class is covered by tests, this makes tests brittle. </p> <p> I think that it's relevant to consider the context of the code base. At this phase of my professional life, I maintained <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>, a fairly popular open-source library. I wanted that library to be stable so that users could trust it. I considered the test suite a guard of the contract. As long as a change didn't break any test, I considered it likely that it wasn't a breaking change. Thus, I was already conservative when it came to editing tests. I <a href="/2013/04/02/why-trust-tests">considered test to be append-only</a> in principle. </p> <p> I still consider it prudent to be conservative when it comes to a library with a public API. This doesn't mean, however, that this line of thinking carries over to code bases without a public (language-level) API. This may include web sites and services, but could also include installed apps. As long as there's no public API, there's no contract to break. </p> <h3 id="af12c8cba91944a29b9c377f5c3e045f"> Internal by default <a href="#af12c8cba91944a29b9c377f5c3e045f" title="permalink">#</a> </h3> <p> In 2020 I wrote a REST API of middling complexity. I used <a href="/outside-in-tdd">outside-in TDD</a> as a major driver. In the spirit of behaviour-driven development I favour describing the observable behaviour of the system. I use <a href="/2021/01/25/self-hosted-integration-tests-in-aspnet">self-hosted</a> <a href="/2019/02/18/from-interaction-based-to-state-based-testing">state-based integration tests</a> for this purpose. Only when I find that these tests get too complex do I grudgingly drop down to the unit-test level. </p> <p> The things that I test with unit tests have to be <code>public</code>. This still leaves plenty of room for behaviour described by the integration tests to have <code>internal</code> implementation details. The code base I mentioned has several examples of that. Some of them I've already described here on the blog. </p> <p> For example, notice that the <code>LinksFilter</code> <a href="/2020/08/24/adding-rest-links-as-a-cross-cutting-concern">shown here</a> is an <code>internal</code> class. Its behaviour is covered by abundant integration tests, so I'm not afraid to refactor it if need be. Those <code>LinkToYear</code>, <code>LinkToMonth</code>, and <code>LinkToDay</code> extension methods that it uses <a href="/2020/08/10/an-aspnet-core-url-builder">are internal too</a>. </p> <p> Another example is the <code>UrlIntegrityFilter</code> <a href="/2020/11/09/checking-signed-urls-with-aspnet">seen here</a>. The class itself is <code>internal</code> and its behaviour is composed from <code>private</code> helper functions. Its <a href="/2020/11/02/signing-urls-with-aspnet">counterpart</a> <code>SigningUrlHelper</code> is also <code>internal</code>. (Its companion <code>SigningUrlHelperFactory</code>, shown in the same article, is <code>public</code>, but that's an oversight on my part. It can easily be <code>internal</code> as well.) All that URL-signing behaviour is, again, covered by tests that verify the behaviour of the REST API. </p> <p> Another example from the same code base can be found in its so-called <em>calendar</em> feature. The system is an online restaurant reservation system. It allows clients to browse a day, a month, or even a year to see if there are any free spots for a given time slot. You can <a href="/2020/08/24/adding-rest-links-as-a-cross-cutting-concern">see an example here</a>. While I test-drove the calendar feature with integration tests, it quickly dawned on me that I had three disparate cases (day, month, year) that essentially represented the same concept: a <em>period</em>. </p> <p> A period is a closed set of heterogeneous data. A year contains only a single datum: the year itself (e.g. 2021). A month contains both a month and a year, and so on. A closed set of heterogeneous data describes a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>, and since I know that in object-oriented programming, <a href="/2018/06/25/visitor-as-a-sum-type">sum types can be encoded as Visitors</a>, I introduced a Visitor API: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPeriod</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Accept&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">IPeriodVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor); } <span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPeriodVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitYear(<span style="color:blue;">int</span>&nbsp;year); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitMonth(<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitDay(<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month,&nbsp;<span style="color:blue;">int</span>&nbsp;day); }</pre> </p> <p> I decided, however, to keep this API <code>internal</code>, since this isn't the only possible way to model this feature. As is the case with the other examples I've shown here, the behaviour is covered by integration tests. I feel free to refactor. In fact, this Visitor-based API is actually the result of a refactoring from something more ad hoc that I didn't like. </p> <p> Here's one of the three <code>IPeriod</code> implementation, in case you're curious: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Month</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IPeriod</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;year; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;month; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Month</span>(<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.year&nbsp;=&nbsp;year; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.month&nbsp;=&nbsp;month; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Accept&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">IPeriodVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;visitor.VisitMonth(year,&nbsp;month); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>?&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Month</span>&nbsp;month&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;year&nbsp;==&nbsp;month.year&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.month&nbsp;==&nbsp;month.month; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">HashCode</span>.Combine(year,&nbsp;month); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class, too, is <code>internal</code>, as are its two companions <code>Day</code> and <code>Year</code>. I'll leave it as an exercise for the interested reader to implement these two classes, as well as <code>IPeriodVisitor&lt;T&gt;</code> implementations that return the next or previous period, or the first or last tick of the period, etcetera. </p> <h3 id="af9c22ec88af4e3fa5f4b5c2356b5692"> Public by choice <a href="#af9c22ec88af4e3fa5f4b5c2356b5692" title="permalink">#</a> </h3> <p> This shifted emphasis of mine isn't a return to a simpler time. It's not <em>internal all the things!</em> It's about shifting the default for classes that are <em>not</em> driven by tests. Those classes that are artefacts of TDD are still <code>public</code> since <a href="/2015/09/22/unit-testing-internals">I don't directly unit test internal classes</a>. </p> <p> Other classes may start out as <code>internal</code> and then get promoted to <code>public</code> by choice. For example, I'd introduced a <code>Seating</code> class in the code base to model how long a seating was supposed to take: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Seating</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">Seating</span>(<span style="color:#2b91af;">TimeSpan</span>&nbsp;seatingDuration,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SeatingDuration&nbsp;=&nbsp;seatingDuration; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;=&nbsp;reservation; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Members&nbsp;follow...</span></pre> </p> <p> Some restaurants have second seatings (or more). They give you a predefined duration after which you're supposed to be done so that they can reuse your table for another party. I'd used the <code>Seating</code> class to encapsulate some logic related to that, such as the <code>Overlaps</code> method: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;Start { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;Reservation.At;&nbsp;} } <span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;End { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;Start&nbsp;+&nbsp;SeatingDuration;&nbsp;} } <span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Overlaps(<span style="color:#2b91af;">Reservation</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;otherSeating&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Seating</span>(SeatingDuration,&nbsp;other); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Start&nbsp;&lt;&nbsp;otherSeating.End&nbsp;&amp;&amp;&nbsp;otherSeating.Start&nbsp;&lt;&nbsp;End; }</pre> </p> <p> While I considered this a well-designed little class with good encapsulation, I kept it <code>internal</code> simply because there was no need to make it <code>public</code>. It was indirectly covered by test cases, but it was a result of a refactoring and not directly test-driven. </p> <p> As I started to add a new feature, I realised that I'd be able to write new unit tests in a better way if I could reuse <code>Seating</code> and a variation of its <code>Overlaps</code> method. I considered it carefully and decided to make the class and its members public: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Seating</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Seating</span>(<span style="color:#2b91af;">TimeSpan</span>&nbsp;seatingDuration,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SeatingDuration&nbsp;=&nbsp;seatingDuration; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reservation&nbsp;=&nbsp;reservation; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Members&nbsp;follow...</span></pre> </p> <p> I made this decision after explicit deliberation. It didn't take long, though, but I did shortly stop to consider whether this seemed like a good idea. This code base isn't a <a href="/2012/12/18/RangersandZookeepers">reusable library in the wild</a>, so I wasn't concerned about misuse of the API. I did consider, on the other hand, how this would increase coupling between the tests and the production code base. It didn't take me long to decide that in this case, I was okay with that. </p> <p> <code>Seating</code> had already existed as an <code>internal</code> class for some time and had proven useful and stable. Putting on my <a href="http://amzn.to/WBCwx7">DDD</a> hat, I also thought that <code>Seating</code> represented a proper domain concept. </p> <h3 id="f6ef0cc965d845fe889170ee956baa4d"> Conclusion <a href="#f6ef0cc965d845fe889170ee956baa4d" title="permalink">#</a> </h3> <p> You can go back and forth on how you write code; which rules of thumb you apply. For many years, I favoured <code>public</code> classes. I think that I even, at one time, tweaked the Visual Studio templates to explicitly create new classes as <code>public</code>. </p> <p> Now, I've changed my heuristic. Classes driven into existence by tests are <code>public</code>; they have to be. Other classes I now make <em>internal by default, and public by choice</em>. </p> <p> This is going to be my rule until I change it. </p> <p> <strong>Next:</strong> <a href="/2021/03/08/pendulum-swing-sealed-by-default">Pendulum swing: sealed by default</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pendulum swings https://blog.ploeh.dk/2021/02/22/pendulum-swings 2021-02-22T08:04:00+00:00 Mark Seemann <div id="post"> <p> <em>The software development industry goes back and forth on how to do things, and so do I.</em> </p> <p> I've been working with something IT-related since 1994, and I've been a professional programmer since 1999. When you observe the software development industry over decades, you may start to notice some trends. One decade, service-oriented architecture (SOA) is cool; the next, consolidation sets in; then it's micro-services; and, as far as I can tell, monoliths are on the way in again, although I'm sure that we'll find something else to call them. </p> <p> It's as if a pendulum swings from one extreme to the other. Sooner or later, it comes back, only to then continue its swing in the other direction. If you view it over time and assume no loss to friction, a pendulum describes a sine wave. </p> <p> <img src="/content/binary/sine-wave.png" alt="A sine wave."> </p> <p> There's probably several reasons for this motion. The benign interpretation is that it's still a young industry and we're still learning. It's not uncommon to see oscillations in dynamic systems, particularly when feedback isn't immediate. </p> <p> Software architecture tends to produce slow feedback. Architecture solves more than one problem, including scalability, but a major motivation to think about architecture is to pick a way to organise the source code so that you don't have to rewrite from scratch every 2-3 years. Tautologically, then, it takes years before you know whether or not you succeeded. </p> <p> While waiting for feedback, you may continue doing what you believe is right: micro-services versus monoliths, unit tests versus acceptance tests, etcetera. Once you discover that a particular way to work has problems, you may overcompensate by going too far in the other direction. </p> <p> Once you discover the problem with that, you may begin to pull back towards the original position. Because feedback is delayed, the pendulum once more swings too far. </p> <p> If we manage to learn from our mistakes, one could hope that the oscillations we currently observe will dampen until we reach equilibrium in the future. The industry is still so young, though, that the pendulum makes wide swings. Perhaps it'll takes decades, or even centuries, before the oscillations die down. </p> <p> The more cynic interpretation is that most software developers have only a few years of professional experience, and aren't taught the experiences of past generations. <blockquote> <p> "Those who cannot remember the past are condemned to repeat it." </p> <footer><cite>George Santayana</cite></footer> </blockquote> In this light, the industry keeps regurgitating the same ideas over and over, never learning from past mistakes. </p> <p> The truth is probably a mix of both explanations. </p> <h3 id="36d029a90bfa4d35a7e8fc10048b8bcc"> Personal pendulum <a href="#36d029a90bfa4d35a7e8fc10048b8bcc" title="permalink">#</a> </h3> <p> I've noticed a similar tendency in myself. I work in a particular way until I run into the limitations of that way. Then, after a time of frustration, I change direction. </p> <p> As an example, I'm an autodidact programmer. In the beginning of my career, I'd just throw together code until I thought it worked, then launch the software with the debugger attached only to discover that it didn't, then go back and tweak some more, and so on. </p> <p> Then I discovered test-driven development (TDD) and for years, it was the only way I could conceive of working. As my experience with TDD grew, I started to notice that it wasn't the panacea that I believed when it was all new. <a href="/2010/12/22/TheTDDApostate">I wrote about that as early as late 2010</a>. Knowing myself, I'd probably started to notice problems with TDD before that. I have cognitive biases just like the next person. You can lie to yourself for years before the problems become so blatant that you can no longer ignore them. </p> <p> To be clear, I never lost faith in TDD, but I began to glimpse the contours of its limitations. It's good for many circumstances, and it's still my preferred technique for developing new production code, but I use other techniques for e.g. prototyping. </p> <p> In 2020 I wrote a code base of middling complexity, and I noticed that I'd started to change my position on some other long-standing practices. As I've tried to explain, it may look like pendulum swings, but I hope that they are, at least, dampened swings. I intend to observe what happens so that I can learn from these new directions. </p> <p> In the following, I'll be writing about these new approaches that I'm trying on, and so far like: <ul> <li><a href="/2021/03/01/pendulum-swing-internal-by-default">Pendulum swing: internal by default</a></li> <li><a href="/2021/03/08/pendulum-swing-sealed-by-default">Pendulum swing: sealed by default</a></li> <li><a href="/2021/03/15/pendulum-swing-pure-by-default">Pendulum swing: pure by default</a></li> </ul> I'd be naive if I believed these to be my final words on any of these topics. I'm currently trying them out for size; in a few decades I'll know more about how it all turns out. </p> <h3 id="fdb1ddeb6709428ca1f0e7f441085b3d"> Conclusion <a href="#fdb1ddeb6709428ca1f0e7f441085b3d" title="permalink">#</a> </h3> <p> One year TDD is all the rage; a few years later, it's BDD. One year it's SOA, then it's <a href="https://alistair.cockburn.us/hexagonal-architecture/">ports and adapters</a> (which implies consolidated deployment), then it's micro-services. One year, it's XML, then it's JSON, then it's YAML. One decade it's structured programming, then it's object-orientation, then it's functional programming, and so on ad nauseam. </p> <p> Hopefully, this is just a symptom of growing pains. Hopefully, we'll learn from all these wild swings so that we don't have to rewrite applications when older developers leave. </p> <p> The only course of action that I can see for myself here is to document how I work so that I, and others, can learn from those experiences. </p> <p> <strong>Next:</strong> <a href="/2021/03/01/pendulum-swing-internal-by-default">Pendulum swing: internal by default</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. When properties are easier than examples https://blog.ploeh.dk/2021/02/15/when-properties-are-easier-than-examples 2021-02-15T07:33:00+00:00 Mark Seemann <div id="post"> <p> <em>Sometimes, describing the properties of a function is easier than coming up with examples.</em> </p> <p> Instead of the term <em>test-driven development</em> you may occasionally encounter the phrase <em>example-driven development</em>. The idea is that each test is an example of how the system under test ought to behave. As you add more tests, you add more examples. </p> <p> I've noticed that beginners often find it difficult to come up with good examples. This is the reason I've developed the <a href="/2019/10/07/devils-advocate">Devil's advocate</a> technique. It's meant as a heuristic that may help you identify the next good example. It's particularly effective if you combine it with the <a href="https://blog.cleancoder.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html">Transformation Priority Premise</a> (TPP) and <a href="https://en.wikipedia.org/wiki/Equivalence_partitioning">equivalence partitioning</a>. </p> <p> I've noticed, however, that translating concrete examples into code is not always straightforward. In the following, I'll describe an experience I had in 2020 while developing an online restaurant reservation system. </p> <p> <ins datetime="2021-06-15T06:14Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="83a8dd13c9694ebc99b5fa4ae049b3b4"> Problem outline <a href="#83a8dd13c9694ebc99b5fa4ae049b3b4" title="permalink">#</a> </h3> <p> I'm going to start by explaining what it was that I was trying to do. I wanted to present the <a href="https://en.wikipedia.org/wiki/Ma%C3%AEtre_d%27h%C3%B4tel">maître d'</a> (or other restaurant staff) with a schedule of a day's reservations. It should take the form of a list of time entries, one entry for every time one or more new reservations would start. I also wanted to list, for each entry, all reservations that were currently ongoing, or would soon start. Here's a simple example, represented as JSON: </p> <p> <pre><span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2023-08-23&quot;</span>, <span style="color:#2e75b6;">&quot;entries&quot;</span>:&nbsp;[ &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;20:00:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;reservations&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;af5feb35f62f475cb02df2a281948829&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2023-08-23T20:00:00.0000000&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;crystalmeth@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Crystal&nbsp;Metheney&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;eae39bc5b3a7408eb2049373b2661e32&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2023-08-23T20:30:00.0000000&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;x.benedict@example.org&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Benedict&nbsp;Xavier&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;}, &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;20:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;reservations&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;af5feb35f62f475cb02df2a281948829&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2023-08-23T20:00:00.0000000&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;crystalmeth@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Crystal&nbsp;Metheney&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;eae39bc5b3a7408eb2049373b2661e32&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2023-08-23T20:30:00.0000000&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;x.benedict@example.org&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Benedict&nbsp;Xavier&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;} ]</pre> </p> <p> To keep the example simple, there are only two reservations for that particular day: one for 20:00 and one for 20:30. Since something happens at both of these times, both time has an entry. My intent isn't necessarily that a user interface should show the data in this way, but I wanted to make the relevant data available so that a user interface could show it if it needed to. </p> <p> The first entry for 20:00 shows both reservations. It shows the reservation for 20:00 for obvious reasons, and it shows the reservation for 20:30 to indicate that the staff can expect a party of four at 20:30. Since this restaurant runs with a single seating per evening, this effectively means that although the reservation hasn't started yet, it still reserves a table. This gives a user interface an opportunity to show the state of the restaurant at that time. The table for the 20:30 party isn't active yet, but it's effectively reserved. </p> <p> For restaurants with shorter seating durations, the schedule should reflect that. If the seating duration is, say, two hours, and someone has a reservation for 20:00, you can sell that table to another party at 18:00, but not at 18:30. I wanted the functionality to take such things into account. </p> <p> The other entry in the above example is for 20:30. Again, both reservations are shown because one is ongoing (and takes up a table) and the other is just starting. </p> <h3 id="63e308b1c630441e8f55fe3ecaa5a03f"> Desired API <a href="#63e308b1c630441e8f55fe3ecaa5a03f" title="permalink">#</a> </h3> <p> A major benefit of test-driven development (TDD) is that you get fast feedback on the API you intent for the system under test (SUT). You write a test against the intended API, and besides a pass-or-fail result, you also learn something about the interaction between client code and the SUT. You often learn that the original design you had in mind isn't going to work well once it meets the harsh realities of an actual programming language. </p> <p> In TDD, you often have to revise the design multiple times during the process. </p> <p> This doesn't mean that you can't have a plan. You can't write the initial test if you have no inkling of what the API should look like. For the schedule feature, I did have a plan. It turned out to hold, more or less. I wanted the API to be a method on a class called <code>MaitreD</code>, which already had these four fields and the constructors to support them: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeOfDay</span>&nbsp;OpensAt&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeOfDay</span>&nbsp;LastSeating&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;SeatingDuration&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&nbsp;Tables&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}</pre> </p> <p> I planned to implement the new feature as a new instance method on that class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>)</pre> </p> <p> This plan turned out to hold in general, although I ultimately decided to simplify the return type by getting rid of the <code>Occurrence</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a>. It's going to be present throughout this article, however, so I need to briefly introduce it. I meant to use it as a generic container of anything, but with an time-stamp associated with the value: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Occurrence</span>(<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="color:#1f377f;">at</span>,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;<span style="color:#1f377f;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;<span style="color:#1f377f;">at</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Value&nbsp;=&nbsp;<span style="color:#1f377f;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;At&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Value&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;<span style="color:#74531f;">Select</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;<span style="color:#1f377f;">selector</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(<span style="color:#1f377f;">selector</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="color:#1f377f;">selector</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(At,&nbsp;<span style="color:#1f377f;">selector</span>(Value)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">Equals</span>(<span style="color:blue;">object</span>?&nbsp;<span style="color:#1f377f;">obj</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">obj</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="color:#1f377f;">occurrence</span>&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;<span style="color:#74531f;">==</span>&nbsp;<span style="color:#1f377f;">occurrence</span>.At&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">EqualityComparer</span>&lt;<span style="color:#2b91af;">T</span>&gt;.Default.<span style="color:#74531f;">Equals</span>(Value,&nbsp;<span style="color:#1f377f;">occurrence</span>.Value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#74531f;">GetHashCode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">HashCode</span>.<span style="color:#74531f;">Combine</span>(At,&nbsp;Value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You may notice that due to the presence of the <code>Select</code> method this is a <a href="/2018/03/22/functors">functor</a>. </p> <p> There's also a little extension method that we may later encounter: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="color:#74531f;">At</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;<span style="color:#1f377f;">value</span>,&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="color:#1f377f;">at</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#1f377f;">at</span>,&nbsp;<span style="color:#1f377f;">value</span>); }</pre> </p> <p> The plan, then, is to return a collection of occurrences, each of which may contain a collection of tables that are relevant to include at that time entry. </p> <h3 id="d121920365874005a3bf46d0555bd008"> Examples <a href="#d121920365874005a3bf46d0555bd008" title="permalink">#</a> </h3> <p> When I embarked on developing this feature, I thought that it was a good fit for example-driven development. Since the input for <code>Schedule</code> requires a collection of <code>Reservation</code> objects, each of which comes with some data, I expected the test cases to become verbose. So I decided to bite the bullet right away and define test cases using <a href="https://xunit.net">xUnit.net</a>'s <code>[ClassData]</code> feature. I wrote this test: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">ClassData</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ScheduleTestCases</span>))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Schedule</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">MaitreD</span>&nbsp;<span style="color:#1f377f;">sut</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">Table</span>[]&gt;&gt;&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">Schedule</span>(<span style="color:#1f377f;">reservations</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">expected</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">o</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">ts</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">ts</span>.<span style="color:#74531f;">AsEnumerable</span>())), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> This is almost as simple as it can be: Call the method and verify that <code>expected</code> is equal to <code>actual</code>. The only slightly complicated piece is the nested projection of <code>expected</code> from <code>IEnumerable&lt;Occurrence&lt;Table[]&gt;&gt;</code> to <code>IEnumerable&lt;Occurrence&lt;IEnumerable&lt;Table&gt;&gt;&gt;</code>. There are ugly reasons for this that I don't want to discuss here, since they have no bearing on the actual topic, which is coming up with tests. </p> <p> I also added the <code>ScheduleTestCases</code> class and a single test case: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ScheduleTestCases</span>&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TheoryData</span>&lt;<span style="color:#2b91af;">MaitreD</span>,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">Table</span>[]&gt;&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ScheduleTestCases</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;No&nbsp;reservations,&nbsp;so&nbsp;no&nbsp;occurrences:</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">Add</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(18), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(21), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(6), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Communal</span>(12)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Array</span>.<span style="color:#74531f;">Empty</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Array</span>.<span style="color:#74531f;">Empty</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">Table</span>[]&gt;&gt;()); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The simplest implementation that passed that test was this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">break</span>; }</pre> </p> <p> Okay, hardly rocket science, but this was just a test case to get started. So I added another one: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">SingleReservationCommunalTable</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">table</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Communal</span>(12); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Some</span>.Reservation; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">Add</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(18), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(21), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(6), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">table</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#1f377f;">r</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#1f377f;">table</span>.<span style="color:#74531f;">Reserve</span>(<span style="color:#1f377f;">r</span>)&nbsp;}.<span style="color:#74531f;">At</span>(<span style="color:#1f377f;">r</span>.At)&nbsp;}); }</pre> </p> <p> This test case adds a single reservation to a restaurant with a single communal table. The <code>expected</code> result is now a single occurrence with that reservation. In true TDD fashion, this new test case caused a test failure, and I now had to adjust the <code>Schedule</code> method to pass all tests: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Any</span>()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">First</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Communal</span>(12).<span style="color:#74531f;">Reserve</span>(<span style="color:#1f377f;">r</span>)&nbsp;}.<span style="color:#74531f;">AsEnumerable</span>().<span style="color:#74531f;">At</span>(<span style="color:#1f377f;">r</span>.At); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">yield</span>&nbsp;<span style="color:#8f08c4;">break</span>; }</pre> </p> <p> You might have wanted to jump to something prettier right away, but I wanted to proceed according to the Devil's advocate technique. I was concerned that I was going to mess up the implementation if I moved too fast. </p> <p> And that was when I basically hit a wall. </p> <h3 id="c8fd655d50414215b04f891676a09bd8"> Property-based testing to the rescue <a href="#c8fd655d50414215b04f891676a09bd8" title="permalink">#</a> </h3> <p> I couldn't figure out how to proceed from there. Which test case ought to be the next? I wanted to follow the spirit of the TPP and pick a test case that would cause another incremental step in the right direction. The sheer number of possible combinations overwhelmed me, though. Should I adjust the reservations? The table configuration for the <code>MaitreD</code> class? The <code>SeatingDuration</code>? </p> <p> It's possible that you'd be able to conjure up the perfect next test case, but I couldn't. I actually let it stew for a couple of days before I decided to give up on the example-driven approach. While I couldn't see a clear path forward with concrete examples, I had a vivid vision of how to proceed with <a href="/property-based-testing-intro">property-based testing</a>. </p> <p> I left the above tests in place and instead added a new test class to my code base. Its only purpose: to test the <code>Schedule</code> method. The test method itself is only a composition of various data definitions and the actual test code: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Property</span>&nbsp;<span style="color:#74531f;">Schedule</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Prop</span>.<span style="color:#74531f;">ForAll</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenReservation.<span style="color:#74531f;">ArrayOf</span>().<span style="color:#74531f;">ToArbitrary</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#74531f;">ScheduleImp</span>); }</pre> </p> <p> This uses <a href="https://fscheck.github.io/FsCheck">FsCheck</a> 2.14.3, which is written in <a href="https://fsharp.org">F#</a> and composes better if you also write the tests in F#. In order to make things a little more palatable for C# developers, I decided to implement the building blocks for the property using methods and class properties. </p> <p> The <code>ScheduleImp</code> method, for example, actually implements the test. This method runs a hundred times (FsCheck's default value) with randomly generated input values: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ScheduleImp</span>(<span style="color:#2b91af;">Reservation</span>[]&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Create&nbsp;a&nbsp;table&nbsp;for&nbsp;each&nbsp;reservation,&nbsp;to&nbsp;ensure&nbsp;that&nbsp;all</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;reservations&nbsp;can&nbsp;be&nbsp;allotted&nbsp;a&nbsp;table.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">tables</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Standard</span>(<span style="color:#1f377f;">r</span>.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(18), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(21), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(6), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">tables</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">Schedule</span>(<span style="color:#1f377f;">reservations</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.At).<span style="color:#74531f;">Distinct</span>().<span style="color:#74531f;">Count</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Count</span>()); }</pre> </p> <p> The step you see in the first line of code is an example of a trick that I find myself doing often with property-based testing: instead of trying to find some good test values for a particular set of circumstances, I create a set of circumstances that fits the randomly generated test values. As the code comment explains, given a set of <code>Reservation</code> values, it creates a table that fits each reservation. In that way I ensure that all the reservations can be allocated a table. </p> <p> I'll soon return to how those random <code>Reservation</code> values are generated, but first let's discuss the rest of the test body. Given a valid <code>MaitreD</code> object it calls the <code>Schedule</code> method. In the <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">assertion phase</a>, it so far only verifies that there's as many time entries in <code>actual</code> as there are distinct <code>At</code> values in <code>reservations</code>. </p> <p> That's hardly a comprehensive description of the SUT, but it's a start. The following implementation passes both the new property, as well as the two examples above. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#1f377f;">reservations</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Communal</span>(12).<span style="color:#74531f;">Reserve</span>(r)&nbsp;<span style="color:blue;">by</span>&nbsp;r.At&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;g.<span style="color:#74531f;">AsEnumerable</span>().<span style="color:#74531f;">At</span>(g.Key); }</pre> </p> <p> I know that many C# programmers don't like query syntax, but I've always had a soft spot for it. I liked it, but wasn't sure that I'd be able to keep it up as I added more constraints to the property. </p> <h3 id="5670f47a815542bfac848645e9a3ccf4"> Generators <a href="#5670f47a815542bfac848645e9a3ccf4" title="permalink">#</a> </h3> <p> Before we get to that, though, I promised to show you how the random <code>reservations</code> are generated. FsCheck has an API for that, and it's also query-syntax-friendly: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">Email</span>&gt;&nbsp;GenEmail&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Arb</span>.<span style="color:#2b91af;">Default</span>.<span style="color:#74531f;">NonWhiteSpaceString</span>().Generator &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Email</span>(s.Item); <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">Name</span>&gt;&nbsp;GenName&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Arb</span>.<span style="color:#2b91af;">Default</span>.<span style="color:#74531f;">StringWithoutNullChars</span>().Generator &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Name</span>(s.Item); <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;GenReservation&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;id&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Arb</span>.<span style="color:#2b91af;">Default</span>.<span style="color:#74531f;">Guid</span>().Generator &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;d&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Arb</span>.<span style="color:#2b91af;">Default</span>.<span style="color:#74531f;">DateTime</span>().Generator &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;e&nbsp;<span style="color:blue;">in</span>&nbsp;GenEmail &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;n&nbsp;<span style="color:blue;">in</span>&nbsp;GenName &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;q&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Arb</span>.<span style="color:#2b91af;">Default</span>.<span style="color:#74531f;">PositiveInt</span>().Generator &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(id,&nbsp;d,&nbsp;e,&nbsp;n,&nbsp;q.Item);</pre> </p> <p> <code>GenReservation</code> is a generator of <code>Reservation</code> values (for a simplified explanation of how such a generator might work, see <a href="/2017/09/18/the-test-data-generator-functor">The Test Data Generator functor</a>). It's composed from smaller generators, among these <code>GenEmail</code> and <code>GenName</code>. The rest of the generators are general-purpose generators defined by FsCheck. </p> <p> If you refer back to the <code>Schedule</code> property above, you'll see that it uses <code>GenReservation</code> to produce an array generator. This is another general-purpose combinator provided by FsCheck. It turns any single-object generator into a generator of arrays containing such objects. Some of these arrays will be empty, which is often desirable, because it means that you'll automatically get coverage of that edge case. </p> <h3 id="ad13aac51d654a7f988b1106579f42fd"> Iterative development <a href="#ad13aac51d654a7f988b1106579f42fd" title="permalink">#</a> </h3> <p> As I already <a href="/2015/01/10/diamond-kata-with-fscheck">discovered in 2015</a> some problems are just much better suited for property-based development than example-driven development. As I expected, this one turned out to be just such a problem. (Recently, <a href="https://www.hillelwayne.com">Hillel Wayne</a> identified a set of problems with no clear properties as <a href="https://buttondown.email/hillelwayne/archive/cross-branch-testing/">rho problems</a>. I wonder if we should pick another Greek letter for this type of problems that almost ooze properties. Sigma problems? Maybe we should just call them <em>describable problems</em>...) </p> <p> For the next step, I didn't have to write a completely new property. I only had to add a new assertion, and thereby strengthening the postconditions of the <code>Schedule</code> method: </p> <p> <pre><span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">o</span>.At).<span style="color:#74531f;">OrderBy</span>(<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">d</span>), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">o</span>.At));</pre> </p> <p> I added the above assertion to <code>ScheduleImp</code> after the previous assertion. It simply states that <code>actual</code> should be sorted in ascending order. </p> <p> To pass this new requirement I added an ordering clause to the implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#1f377f;">reservations</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Communal</span>(12).<span style="color:#74531f;">Reserve</span>(r)&nbsp;<span style="color:blue;">by</span>&nbsp;r.At&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">orderby</span>&nbsp;g.Key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;g.<span style="color:#74531f;">AsEnumerable</span>().<span style="color:#74531f;">At</span>(g.Key); }</pre> </p> <p> It passes all tests. Commit to Git. Next. </p> <h3 id="040edba86dfd4bfe8a57cef15b4a9ff6"> Table configuration <a href="#040edba86dfd4bfe8a57cef15b4a9ff6" title="permalink">#</a> </h3> <p> If you consider the current implementation, there's much not to like. The worst offence, I think, is that it conjures a hard-coded communal table out of thin air. The method ought to use the table configuration passed to the <code>MaitreD</code> object. This seems like an obvious flaw to address. I therefore added this to the property: </p> <p> <pre>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">All</span>(<span style="color:#1f377f;">actual</span>,&nbsp;<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#74531f;">AssertTables</span>(<span style="color:#1f377f;">tables</span>,&nbsp;<span style="color:#1f377f;">o</span>.Value)); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">AssertTables</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&nbsp;<span style="color:#1f377f;">expected</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&nbsp;<span style="color:#1f377f;">actual</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>(<span style="color:#1f377f;">expected</span>.<span style="color:#74531f;">Count</span>(),&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Count</span>()); }</pre> </p> <p> It's just another assertion that uses the helper assertion also shown. As a first pass, it's not enough to cheat the Devil, but it sets me up for my next move. The plan is to assert that no tables are generated out of thin air. Currently, <code>AssertTables</code> only verifies that the actual count of tables in each occurrence matches the expected count. </p> <p> The Devil easily foils that plan by generating a table for each reservation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">tables</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Communal</span>(12).<span style="color:#74531f;">Reserve</span>(<span style="color:#1f377f;">r</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#1f377f;">reservations</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;r&nbsp;<span style="color:blue;">by</span>&nbsp;r.At&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">orderby</span>&nbsp;g.Key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:#1f377f;">tables</span>.<span style="color:#74531f;">At</span>(g.Key); }</pre> </p> <p> This (unfortunately) passes all tests, so commit to Git and move on. </p> <p> The next move I made was to add an assertion to <code>AssertTables</code>: </p> <p> <pre><span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">expected</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">t</span>.Capacity), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">t</span>.Capacity));</pre> </p> <p> This new requirement states that the total capacity of the actual tables should be equal to the total capacity of the allocated tables. It doesn't prevent the Devil from generating tables out of thin air, but it makes it harder. At least, it makes it so hard that I found it more reasonable to use the supplied table configuration: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">tables</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Zip</span>(Tables,&nbsp;(<span style="color:#1f377f;">r</span>,&nbsp;<span style="color:#1f377f;">t</span>)&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">t</span>.<span style="color:#74531f;">Reserve</span>(<span style="color:#1f377f;">r</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#1f377f;">reservations</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;r&nbsp;<span style="color:blue;">by</span>&nbsp;r.At&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">orderby</span>&nbsp;g.Key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:#1f377f;">tables</span>.<span style="color:#74531f;">At</span>(g.Key); }</pre> </p> <p> The implementation of <code>Schedule</code> still cheats because it 'knows' that no tests (except for the degenerate test where there are no reservations) have surplus tables in the configuration. It takes advantage of that knowledge to zip the two collections, which is really not appropriate. </p> <p> Still, it seems that things are moving in the right direction. </p> <h3 id="5891a00f231f46b68afb0b6aff43b0b0"> Generated SUT <a href="#5891a00f231f46b68afb0b6aff43b0b0" title="permalink">#</a> </h3> <p> Until now, <code>ScheduleImp</code> has been using a hard-coded <code>sut</code>. It's time to change that. </p> <p> To keep my steps as small as possible, I decided to start with the <code>SeatingDuration</code> since it was currently not being used by the implementation. This meant that I could start randomising it without affecting the SUT. Since this was a code change of middling complexity in the test code, I found it most prudent to move in such a way that I didn't have to change the SUT as well. </p> <p> I completely extracted the initialisation of the <code>sut</code> to a method argument of the <code>ScheduleImp</code> method, and adjusted it accordingly: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ScheduleImp</span>(<span style="color:#2b91af;">MaitreD</span>&nbsp;<span style="color:#1f377f;">sut</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">Schedule</span>(<span style="color:#1f377f;">reservations</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.At).<span style="color:#74531f;">Distinct</span>().<span style="color:#74531f;">Count</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Count</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">o</span>.At).<span style="color:#74531f;">OrderBy</span>(<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">d</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">o</span>.At)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">All</span>(<span style="color:#1f377f;">actual</span>,&nbsp;<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#74531f;">AssertTables</span>(<span style="color:#1f377f;">sut</span>.Tables,&nbsp;<span style="color:#1f377f;">o</span>.Value)); }</pre> </p> <p> This meant that I also had to adjust the calling property: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Property</span>&nbsp;<span style="color:#74531f;">Schedule</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Prop</span>.<span style="color:#74531f;">ForAll</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenReservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:#74531f;">ArrayOf</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:#74531f;">SelectMany</span>(<span style="color:#1f377f;">rs</span>&nbsp;=&gt;&nbsp;<span style="color:#74531f;">GenMaitreD</span>(<span style="color:#1f377f;">rs</span>).<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">m</span>&nbsp;=&gt;&nbsp;(<span style="color:#1f377f;">m</span>,&nbsp;<span style="color:#1f377f;">rs</span>))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:#74531f;">ToArbitrary</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;<span style="color:#74531f;">ScheduleImp</span>(<span style="color:#1f377f;">t</span>.m,&nbsp;<span style="color:#1f377f;">t</span>.rs)); }</pre> </p> <p> You've already seen <code>GenReservation</code>, but <code>GenMaitreD</code> is new: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">MaitreD</span>&gt;&nbsp;<span style="color:#74531f;">GenMaitreD</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Create&nbsp;a&nbsp;table&nbsp;for&nbsp;each&nbsp;reservation,&nbsp;to&nbsp;ensure&nbsp;that&nbsp;all</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;reservations&nbsp;can&nbsp;be&nbsp;allotted&nbsp;a&nbsp;table.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">tables</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Standard</span>(<span style="color:#1f377f;">r</span>.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;seatingDuration&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Choose</span>(1,&nbsp;6) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(18), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(21), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(seatingDuration), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">tables</span>); }</pre> </p> <p> The only difference from before is that the new <code>MaitreD</code> object is now initialised from within a generator expression. The duration is randomly picked from the range of one to six hours (those numbers are my arbitrary choices). </p> <p> Notice that it's possible to base one generator on values randomly generated by another generator. Here, <code>reservations</code> are randomly produced by <code>GenReservation</code> and merged to a tuple with <code>SelectMany</code>, as you can see above. </p> <p> This in itself didn't impact the SUT, but set up the code for my next move, which was to generate <em>more</em> tables than reservations, so that there'd be some free tables left after the schedule allocation. I first added a more complex table generator: </p> <p> <pre><span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">summary</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;Generate&nbsp;a&nbsp;table&nbsp;configuration&nbsp;that&nbsp;can&nbsp;at&nbsp;minimum&nbsp;accomodate&nbsp;all</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;reservations.</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;/</span><span style="color:gray;">summary</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">param</span><span style="color:gray;">&nbsp;name</span><span style="color:gray;">=</span><span style="color:gray;">&quot;</span><span style="color:#1f377f;">reservations</span><span style="color:gray;">&quot;</span><span style="color:gray;">&gt;</span><span style="color:green;">The&nbsp;reservations&nbsp;to&nbsp;accommodate</span><span style="color:gray;">&lt;/</span><span style="color:gray;">param</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">returns</span><span style="color:gray;">&gt;</span><span style="color:green;">A&nbsp;generator&nbsp;of&nbsp;valid&nbsp;table&nbsp;configurations.</span><span style="color:gray;">&lt;/</span><span style="color:gray;">returns</span><span style="color:gray;">&gt;</span> <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&nbsp;<span style="color:#74531f;">GenTables</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Create&nbsp;a&nbsp;table&nbsp;for&nbsp;each&nbsp;reservation,&nbsp;to&nbsp;ensure&nbsp;that&nbsp;all</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;reservations&nbsp;can&nbsp;be&nbsp;allotted&nbsp;a&nbsp;table.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">tables</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Standard</span>(<span style="color:#1f377f;">r</span>.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;moreTables&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Choose</span>(1,&nbsp;12).<span style="color:#74531f;">Select</span>(<span style="color:#2b91af;">Table</span>.<span style="color:#74531f;">Standard</span>).<span style="color:#74531f;">ArrayOf</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;allTables&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Shuffle</span>(<span style="color:#1f377f;">tables</span>.<span style="color:#74531f;">Concat</span>(moreTables)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;allTables.<span style="color:#74531f;">AsEnumerable</span>(); }</pre> </p> <p> This function first creates standard tables that exactly accommodate each reservation. It then generates an array of <code>moreTables</code>, each fitting between one and twelve people. It then mixes those tables together with the ones that fit a reservation and returns the sequence. Since <code>moreTables</code> can be empty, it's possible that the entire sequence of tables only just accommodates the <code>reservations</code>. </p> <p> I then modified <code>GenMaitreD</code> to use <code>GenTables</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">MaitreD</span>&gt;&nbsp;<span style="color:#74531f;">GenMaitreD</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;seatingDuration&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Choose</span>(1,&nbsp;6) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;tables&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#74531f;">GenTables</span>(<span style="color:#1f377f;">reservations</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(18), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(21), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromHours</span>(seatingDuration), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tables); }</pre> </p> <p> This provoked a change in the SUT: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#1f377f;">reservations</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;r&nbsp;<span style="color:blue;">by</span>&nbsp;r.At&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">orderby</span>&nbsp;g.Key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:#74531f;">Allocate</span>(g).<span style="color:#74531f;">At</span>(g.Key); }</pre> </p> <p> The <code>Schedule</code> method now calls a private helper method called <code>Allocate</code>. This method already existed, since it supports the algorithm used to decide whether or not to accept a reservation request. </p> <h3 id="79e43322721b45a390c68caeaa1a3dcf"> Rinse and repeat <a href="#79e43322721b45a390c68caeaa1a3dcf" title="permalink">#</a> </h3> <p> I hope that a pattern starts to emerge. I kept adding more and more randomisation to the data generators, while I also added more and more assertions to the property. Here's what it looked like after a few more iterations: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ScheduleImp</span>(<span style="color:#2b91af;">MaitreD</span>&nbsp;<span style="color:#1f377f;">sut</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">Schedule</span>(<span style="color:#1f377f;">reservations</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.At).<span style="color:#74531f;">Distinct</span>().<span style="color:#74531f;">Count</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Count</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">o</span>.At).<span style="color:#74531f;">OrderBy</span>(<span style="color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">d</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">o</span>.At)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">All</span>(<span style="color:#1f377f;">actual</span>,&nbsp;<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#74531f;">AssertTables</span>(<span style="color:#1f377f;">sut</span>.Tables,&nbsp;<span style="color:#1f377f;">o</span>.Value)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">All</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">actual</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">o</span>&nbsp;=&gt;&nbsp;<span style="color:#74531f;">AssertRelevance</span>(<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#1f377f;">sut</span>.SeatingDuration,&nbsp;<span style="color:#1f377f;">o</span>)); }</pre> </p> <p> While <code>AssertTables</code> didn't change further, I added another helper assertion called <code>AssertRelevance</code>. I'm not going to show it here, but it checks that each occurrence only contains reservations that overlaps that point in time, give or take the <code>SeatingDuration</code>. </p> <p> I also made the reservation generator more sophisticated. If you consider the one defined above, one flaw is that it generates reservations at random dates. The chance that it'll generate two reservations that are actually adjacent in time is minimal. To counter this problem, I added a function that would return a generator of adjacent reservations: </p> <p> <pre><span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">summary</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;Generate&nbsp;an&nbsp;adjacant&nbsp;reservation&nbsp;with&nbsp;a&nbsp;25%&nbsp;chance.</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;/</span><span style="color:gray;">summary</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">param</span><span style="color:gray;">&nbsp;name</span><span style="color:gray;">=</span><span style="color:gray;">&quot;</span><span style="color:#1f377f;">reservation</span><span style="color:gray;">&quot;</span><span style="color:gray;">&gt;</span><span style="color:green;">The&nbsp;candidate&nbsp;reservation</span><span style="color:gray;">&lt;/</span><span style="color:gray;">param</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">returns</span><span style="color:gray;">&gt;</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;A&nbsp;generator&nbsp;of&nbsp;an&nbsp;array&nbsp;of&nbsp;reservations.&nbsp;The&nbsp;generated&nbsp;array&nbsp;is</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;either&nbsp;a&nbsp;singleton&nbsp;or&nbsp;a&nbsp;pair.&nbsp;In&nbsp;75%&nbsp;of&nbsp;the&nbsp;cases,&nbsp;the&nbsp;input</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;</span><span style="color:gray;">paramref</span><span style="color:gray;">&nbsp;name</span><span style="color:gray;">=</span><span style="color:gray;">&quot;</span><span style="color:#1f377f;">reservation</span><span style="color:gray;">&quot;</span><span style="color:gray;">&nbsp;/&gt;</span><span style="color:green;">&nbsp;is&nbsp;returned&nbsp;as&nbsp;a&nbsp;singleton&nbsp;array.</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;In&nbsp;25%&nbsp;of&nbsp;the&nbsp;cases,&nbsp;the&nbsp;array&nbsp;contains&nbsp;two&nbsp;reservations:&nbsp;the&nbsp;input</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;reservation&nbsp;as&nbsp;well&nbsp;as&nbsp;another&nbsp;reservation&nbsp;adjacent&nbsp;to&nbsp;it.</span> <span style="color:gray;">///</span><span style="color:green;">&nbsp;</span><span style="color:gray;">&lt;/</span><span style="color:gray;">returns</span><span style="color:gray;">&gt;</span> <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">Reservation</span>[]&gt;&nbsp;<span style="color:#74531f;">GenAdjacentReservations</span>(<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;adjacent&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#74531f;">GenReservationAdjacentTo</span>(<span style="color:#1f377f;">reservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;useAdjacent&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Frequency</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">WeightAndValue</span>&lt;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:blue;">bool</span>&gt;&gt;(3,&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Constant</span>(<span style="color:blue;">false</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">WeightAndValue</span>&lt;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:blue;">bool</span>&gt;&gt;(1,&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Constant</span>(<span style="color:blue;">true</span>))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rs&nbsp;=&nbsp;useAdjacent&nbsp;? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#1f377f;">reservation</span>,&nbsp;adjacent&nbsp;}&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;rs; } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#74531f;">GenReservationAdjacentTo</span>(<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;minutes&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Choose</span>(-6&nbsp;*&nbsp;4,&nbsp;6&nbsp;*&nbsp;4)&nbsp;<span style="color:green;">//&nbsp;4:&nbsp;quarters/h</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;GenReservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;r.<span style="color:#74531f;">WithDate</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservation</span>.At&nbsp;<span style="color:#74531f;">+</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromMinutes</span>(minutes)); }</pre> </p> <p> Now that I look at it again, I wonder whether I could have expressed this in a simpler way... It gets the job done, though. </p> <p> I then defined a generator that would either create entirely random reservations, or some with some adjacent ones mixed in: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">Reservation</span>[]&gt;&nbsp;GenReservations { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">normalArrayGen</span>&nbsp;=&nbsp;GenReservation.<span style="color:#74531f;">ArrayOf</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">adjacentReservationsGen</span>&nbsp;=&nbsp;GenReservation.<span style="color:#74531f;">ArrayOf</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:#74531f;">SelectMany</span>(<span style="color:#1f377f;">rs</span>&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Gen</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:#74531f;">Sequence</span>(<span style="color:#1f377f;">rs</span>.<span style="color:#74531f;">Select</span>(<span style="color:#74531f;">GenAdjacentReservations</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:#74531f;">SelectMany</span>(<span style="color:#1f377f;">rss</span>&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">Shuffle</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">rss</span>.<span style="color:#74531f;">SelectMany</span>(<span style="color:#1f377f;">rs</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">rs</span>)))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Gen</span>.<span style="color:#74531f;">OneOf</span>(<span style="color:#1f377f;">normalArrayGen</span>,&nbsp;<span style="color:#1f377f;">adjacentReservationsGen</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I changed the property to use this generator instead: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Property</span>&nbsp;<span style="color:#74531f;">Schedule</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Prop</span>.<span style="color:#74531f;">ForAll</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:#74531f;">SelectMany</span>(<span style="color:#1f377f;">rs</span>&nbsp;=&gt;&nbsp;<span style="color:#74531f;">GenMaitreD</span>(<span style="color:#1f377f;">rs</span>).<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">m</span>&nbsp;=&gt;&nbsp;(<span style="color:#1f377f;">m</span>,&nbsp;<span style="color:#1f377f;">rs</span>))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:#74531f;">ToArbitrary</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">t</span>&nbsp;=&gt;&nbsp;<span style="color:#74531f;">ScheduleImp</span>(<span style="color:#1f377f;">t</span>.m,&nbsp;<span style="color:#1f377f;">t</span>.rs)); }</pre> </p> <p> I could have kept at it longer, but this turned out to be good enough to bring about the change in the SUT that I was looking for. </p> <h3 id="f34220b7400d464c9fce7c2b65454971"> Implementation <a href="#f34220b7400d464c9fce7c2b65454971" title="permalink">#</a> </h3> <p> These incremental changes iteratively brought me closer and closer to an implementation that I think has the correct behaviour: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Occurrence</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&gt;&gt;&nbsp;<span style="color:#74531f;">Schedule</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#1f377f;">reservations</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;r&nbsp;<span style="color:blue;">by</span>&nbsp;r.At&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">orderby</span>&nbsp;g.Key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;seating&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Seating</span>(SeatingDuration,&nbsp;g.Key) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;overlapping&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Where</span>(seating.<span style="color:#74531f;">Overlaps</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:#74531f;">Allocate</span>(overlapping).<span style="color:#74531f;">At</span>(g.Key); }</pre> </p> <p> Contrary to my initial expectations, I managed to keep the implementation to a single query expression all the way through. </p> <h3 id="c64408017700494a808fae9388341e87"> Conclusion <a href="#c64408017700494a808fae9388341e87" title="permalink">#</a> </h3> <p> This was a problem that I was stuck on for a couple of days. I could describe the properties I wanted the function to have, but I had a hard time coming up with a good set of examples for unit tests. </p> <p> You may think that using property-based testing looks even more complicated, and I admit that it's far from trivial. The problem itself, however, isn't easy, and while the property-based approach may look daunting, it turned an intractable problem into a manageable one. That's a win in my book. </p> <p> It's also worth noting that this would all have looked more elegant in F#. There's an object-oriented tax to be paid when using FsCheck from C#. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Accountability and free speech https://blog.ploeh.dk/2021/02/09/accountability-and-free-speech 2021-02-09T07:53:00+00:00 Mark Seemann <div id="post"> <p> <em>A most likely naive suggestion.</em> </p> <p> A few years ago, my mother (born 1940) went to Paris on vacation with her older sister. She was a little concerned if she'd be able to navigate the <a href="https://en.wikipedia.org/wiki/Paris_M%C3%A9tro">Métro</a>, so she asked me to print out all sorts of maps in advance. I wasn't keen on another encounter with my nemesis, the printer, so I instead showed her how she could use Google Maps to get on-demand routes. Google Maps now include timetables and line information for many metropolitan subway lines around the world. I've successfully used it to find my way around London, Paris, New York, Tokyo, Sydney, and Melbourne. </p> <p> It even works in my backwater home-town: </p> <p> <img src="/content/binary/cph-metro-map.png" alt="Snapshot of Copenhagen Metro route generated by Google Maps."> </p> <p> It's rapidly turning into indispensable digital infrastructure, and that's beginning to trouble me. </p> <p> You can still get paper maps of the various rapid transit systems around the world, but for how long? </p> <h3 id="e5296cbd33f04db3b79890c09d8e3146"> Digital infrastructure <a href="#e5296cbd33f04db3b79890c09d8e3146" title="permalink">#</a> </h3> <p> If you're old enough, you may remember <a href="https://en.wikipedia.org/wiki/Telephone_directory">phone books</a>. In the days of land lines and analogue phones, you'd look up a number and then dial it. As the internet became increasingly ubiquitous, phone directories went online as well. Paper phone books were seen as waste, and ultimately disappeared from everyday life. </p> <p> You can think of paper phone books as a piece of infrastructure that's now gone. I don't miss them, but it's worth reflecting on what impact their disappearing has. Today, if I need to find a phone number, Google is often the place I go. The physical infrastructure is gone, replaced by a digital infrastructure. </p> <p> Google, in particular, now provides important infrastructure for modern society. Not only web search, but also maps, video sharing, translation, emails, and much more. </p> <p> Other companies offer other services that verge on being infrastructure. Facebook is more than just updates to friends and families. Many organisations, including schools and universities, coordinate activities via Facebook, and the political discourse increasingly happens there and on Twitter. </p> <p> We've come to rely on these 'free' services to a degree that resembles our reliance on physical infrastructure like the power grid, running water, roads, telephones, etcetera. </p> <h3 id="bf364e0f0ca84fbbb0536b462743993b"> TANSTAAFL <a href="#bf364e0f0ca84fbbb0536b462743993b" title="permalink">#</a> </h3> <p> <a href="https://en.wikipedia.org/wiki/Robert_A._Heinlein">Robert A. Heinlein</a> coined the phrase <em>There ain't no such thing as a free lunch</em> (TANSTAAFL) in his excellent book <a href="https://amzn.to/3pWi7aM">The Moon is a Harsh Mistress</a>. Indeed, the digital infrastructure isn't free. </p> <p> Recent events have magnified some of the cost we're paying. In January, Facebook and Twitter suspended the then-president of the United States from the platforms. I've little sympathy for Donald Trump, who strikes me as an uncouth narcissist, but since I'm a Danish citizen living in Denmark, I don't think that I ought to have much of an opinion about American politics. </p> <p> I do think, on the other hand, that the suspension sets an uncomfortable precedent. </p> <p> Should we let a handful of tech billionaires control essential infrastructure? This time, the victim was someone half the world was glad to see go, but who's next? Might these companies suspend the accounts of politicians who work against them? </p> <p> Never in the history of the world has decision power over so many people been so concentrated. I'll remind my readers that Facebook, Twitter, Google, etcetera are in worldwide use. The decisions of a handful of majority shareholders can now affect billions of people. </p> <p> If you're suspended from one of these platforms, you may lose your ability to participate in school activities, or from finding your way through a foreign city, or from taking part of the democratic discussion. </p> <h3 id="23e71b25789c465e9530a51a1cceac21"> The case for regulation <a href="#23e71b25789c465e9530a51a1cceac21" title="permalink">#</a> </h3> <p> In this article, I mainly want to focus on the free-speech issue related mostly to Facebook and Twitter. These companies make money from ads. The longer you stay on the platform, the more ads they can show you, and they've discovered that nothing pulls you in like anger. These incentives are incredibly counter-productive for society. </p> <p> Users are implicitly encouraged to create or spread anger, yet few are held accountable. One reason is that you can easily create new accounts without disclosing your real-world identity. This makes it difficult to hold users accountable for their utterances. </p> <p> Instead of clear rules, users are suspended for inexplicable and arbitrary reasons. It seems that these are mostly the verdicts of algorithms, but as we've seen in the case of Donald Trump, it can also be the result of an undemocratic, but political decision. </p> <p> Everyone can lose their opportunities for self-expression for arbitrary reasons. This is a free-speech issue. </p> <p> Yes, free speech. </p> <p> I'm well aware that Facebook and Twitter are private companies, and no-one has any <em>right</em> to an account on those platforms. That's why I started this essay by discussing how these services are turning into infrastructure. </p> <p> More than a century ago, the telephone was new technology operated by private companies. I know the Danish history best, but it serves well as an example. <a href="https://da.wikipedia.org/wiki/KTAS">KTAS</a>, one of the world's first telephone companies, was a private company. Via mergers and acquisitions, it still is, but as it grew and became the backbone of the country's electronic communications network, the government introduced legislation. For decades, it and its sister companies were regional monopolies. </p> <p> The government allowed the monopoly in exchange for various obligations. Even when the monopoly ended in 1996, the original monopoly companies inherited these legal obligations. For instance, every Danish citizen has the right to get a land-line installed, even if that installation by itself is a commercial loss for the company. This could be the case on certain remote islands or other rural areas. The Danish state compensates the telephone operator for this potential loss. </p> <p> Many other utility companies run in a similar fashion. Some are semi-public, some are private, but common to water, electricity, garbage disposal, heating, and other operators is that they are regulated by government. </p> <p> When a utility becomes important to the functioning of society, a sensible government steps in to regulate it to ensure the further smooth functioning of society. </p> <p> I think that the services offered by Google, Facebook, and Twitter are fast approaching a level of significance that ought to trigger government regulation. </p> <p> I don't say that lightly. I'm actually quite libertarian in my views, but I'm no anarcho-libertarian. I do acknowledge that a state offers essential services such as protection of property rights, a judicial system, and so on. </p> <h3 id="f4c78c79e1eb46d2941611d443f89ea1"> Accountability <a href="#f4c78c79e1eb46d2941611d443f89ea1" title="permalink">#</a> </h3> <p> How should we regulate social media? I think we should start by exchanging accountability for the right to post. </p> <p> Let's take another step back for a moment. For generations, it's been possible to send a letter to the editor of a regular newspaper. If the editor deems it worthy for publication, it'll be printed in the next issue. You don't have any right to get your letter published; this happens at the discretion of the editor. </p> <p> Why does it work that way? It works that way because the editor is accountable for what's printed in the paper. Ultimately, he or she can go to jail for what's printed. </p> <p> Freedom of speech is more complicated than it looks at first glance. For instance, censorship in Denmark was abolished with the 1849 constitution. This doesn't mean that you can freely say whatever you'd like; it only means that government has no right to <em>prevent</em> you from saying or writing something. You can still be prosecuted after the fact if you say or write something libellous, or if you incite violence, or if you disclose secrets that you've agreed to keep, etcetera. </p> <p> This is accountability. It works when the person making the utterance is known and within reach of the law. </p> <p> Notice, particularly, that an editor-in-chief is accountable for a newspaper's contents. Why isn't Facebook or Twitter accountable for content? </p> <p> These companies have managed to spin a story that they're <em>platforms</em> rather than publishers. This argument might have had some legs ten years ago. When I started using Twitter in 2009, the algorithm was easy to understand: My feed showed the tweets from the accounts that I followed, in the order that they were published. This was easy to understand, and Twitter didn't, as far as I could tell, edit the feed. Since no editing took place, I find the platform argument applicable. </p> <p> Later, the social networks began editing the feeds. No humans were directly involved, but today, some posts are amplified while others get little attention. Since this is done by 'algorithms' rather than editors, the companies have managed to convince lawmakers that they're still only platforms. Let's be real, though. Apparently, the public needs a programmer to say the following, and I'm a programmer. </p> <p> Programmers, employees of those companies, wrote the algorithms. They experimented and tweaked those algorithms to maximise 'engagement'. Humans are continually involved in this process. Editing takes place. </p> <p> I think it's time to stop treating social media networks as platforms, and start treating them as publishers. </p> <h3 id="ab23d2f377db45bf99970479238dd982"> Practicality <a href="#ab23d2f377db45bf99970479238dd982" title="permalink">#</a> </h3> <p> Facebook and Twitter will protest that it's practically impossible for them to 'edit' what happens on the networks. I find this argument unconvincing, since editing already takes place. </p> <p> I'd like to suggest a partial way out, though. This brings us back to regulation. </p> <p> I think that each country or jurisdiction should make it possible for users to opt in to being accountable. Users should be able to verify their accounts as real, legal persons. If they do that, their activity on the platform should be governed by the jurisdiction in which they reside, instead of by the arbitrary and ill-defined 'community guidelines' offered by the networks. You'd be accountable for your utterances according to local law where you live. The advantage, though, is that this accountability buys you the <em>right</em> to be present on the platform. You can be sued for your posts, but you can't be kicked off. </p> <p> I'm aware that not everyone lives in a benign welfare state such as Denmark. This is why I suggest this as an option. Even if you live in a state that regulates social media as outlined above, the option to stay anonymous should remain. This is how it already works, and I imagine that it should continue to work like that for this group of users. The cost of staying anonymous, though, is that you submit to the arbitrary and despotic rules of those networks. For many people, including minorities and citizens of oppressive states, this is likely to remain a better trade-off. </p> <h3 id="84eccfad9fe649c3b38d9e4d6ddb2698"> Problems <a href="#84eccfad9fe649c3b38d9e4d6ddb2698" title="permalink">#</a> </h3> <p> This suggestion is in no way perfect. I can already identify one problem with it. </p> <p> A country could grant its citizens the right to conduct infowar on an adversary; think troll armies and the like. If these citizens are verified and 'accountable' to their local government, but this government encourages rather than punishes incitement to violence in a foreign country, then how do we prevent that? </p> <p> I have a few half-baked ideas, but I'd rather leave the problem here in the hope that it might inspire other people to start thinking about it, too. </p> <h3 id="f25f8486eed54d3d89fd607ef41e3ac7"> Conclusion <a href="#f25f8486eed54d3d89fd607ef41e3ac7" title="permalink">#</a> </h3> <p> This is a post that I've waited for a long time for someone else to write. If someone already did, I'm not aware of it. I can think of three reasons: <ul> <li>It's a really stupid idea.</li> <li>It's a common idea, but no-one talks about it because of <a href="https://en.wikipedia.org/wiki/Pluralistic_ignorance">pluralistic ignorance</a>.</li> <li>It's an original idea that no-one else have had.</li> </ul> I don't believe much in the last option, and I'm afraid that the first is the most likely. I've no desire to look stupid, but on the other hand, I can't help keep thinking about this. </p> <p> The idea is, in short, to make it optional for users to 'buy' the right to stay on a social network for the price of being held legally accountable. This requires some national as well as international regulation of the digital infrastructure. </p> <p> Could it work? I don't know, but isn't it worth discussing? </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c9bf9249f05342dcae02d9581b7e7e5c"> <div class="comment-author"><a href="https://jeremiahflaga.github.io/">Jboy Flaga</a> <a href="#c9bf9249f05342dcae02d9581b7e7e5c">#</a></div> <div class="comment-content"> <p> Hi Mark, I would just like to say that I like the idea :) </p> <p> Thank you for writing this. </p> </div> <div class="comment-date">2021-02-18 13:34 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. ASP.NET POCO Controllers: an experience report https://blog.ploeh.dk/2021/02/01/aspnet-poco-controllers-an-experience-report 2021-02-01T08:10:00+00:00 Mark Seemann <div id="post"> <p> <em>Controllers don't have to derive from a base class.</em> </p> <p> In most tutorials about ASP.NET web APIs you'll be told to let Controller classes derive from a base class. It may be convenient if you believe that productivity is measured by how fast you can get an initial version of the software into production. Granted, sometimes that's the case, but usually there's a price to be paid. Did you produce legacy code in the process? </p> <p> One <a href="http://bit.ly/working-effectively-with-legacy-code">common definition of legacy code</a> is that it's code without tests. With ASP.NET I've repeatedly found that when Controllers derive from <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.controllerbase">ControllerBase</a> they become harder to unit test. It may be convenient to have access to the <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.url#Microsoft_AspNetCore_Mvc_ControllerBase_Url">Url</a> and <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.user#Microsoft_AspNetCore_Mvc_ControllerBase_User">User</a> properties, but this tends to make the <em>arrange</em> phase of a unit test much more complex. Not impossible; just more complex than I like. In short, inheriting from <code>ControllerBase</code> just to get access to, say, <code>Url</code> or <code>User</code> violates the <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a>. That <code>ControllerBase</code> is too big a dependency for my taste. </p> <p> I already use <a href="/2021/01/25/self-hosted-integration-tests-in-aspnet">self-hosting in integration tests</a> so that I can interact with my REST APIs via HTTP. When I want to test how my API reacts to various HTTP-specific circumstances, I do that via integration tests. So, in a recent code base I decided to see if I could write an entire REST API in ASP.NET Core without inheriting from <code>ControllerBase</code>. </p> <p> The short answer is that, yes, this is possible, and I'd do it again, but you have to jump through some hoops. I consider that hoop-jumping a fine price to pay for the benefits of simpler unit tests and (it turns out) better separation of concerns. </p> <p> In this article, I'll share what I've learned. <ins datetime="2021-06-15T06:14Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="fa68860febe6482287eadc65cf3e8a46"> POCO Controllers <a href="#fa68860febe6482287eadc65cf3e8a46" title="permalink">#</a> </h3> <p> Just so that we're on the same page: A <a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object">POCO</a> Controller is a Controller class that doesn't inherit from any base class. In my code base, they're defined like this: </p> <p> <pre>[<span style="color:#2b91af;">Route</span>(<span style="color:#a31515;">&quot;&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">HomeController</span></pre> </p> <p> or </p> <p> <pre>[<span style="color:#2b91af;">Authorize</span>(Roles&nbsp;=&nbsp;<span style="color:#a31515;">&quot;MaitreD&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ScheduleController</span></pre> </p> <p> As you can tell, they don't inherit from <code>ControllerBase</code> or any other base class. They <em>are</em> annotated with attributes, like <code>[Route]</code>, <code>[Authorize]</code>, or <code>[ApiController]</code>. Strictly speaking, that may disqualify them as true POCOs, but in practice I've found that those attributes don't impact the sustainability of the code base in a negative manner. </p> <p> Dependency Injection is still possible, and in use: </p> <p> <pre>[<span style="color:#2b91af;">ApiController</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IClock</span>&nbsp;clock, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRestaurantDatabase</span>&nbsp;restaurantDatabase, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Clock&nbsp;=&nbsp;clock; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RestaurantDatabase&nbsp;=&nbsp;restaurantDatabase; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IClock</span>&nbsp;Clock&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IRestaurantDatabase</span>&nbsp;RestaurantDatabase&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Controller&nbsp;actions&nbsp;and&nbsp;other&nbsp;members&nbsp;go&nbsp;here...</span></pre> </p> <p> I consider Dependency Injection nothing but an application of polymorphism, so I think that still fits the POCO label. After all, <a href="/2017/01/27/dependency-injection-is-passing-an-argument">Dependency Injection is just argument-passing</a>. </p> <h3 id="b1672f04c9774dca9f221a301c191d95"> HTTP responses and status codes <a href="#b1672f04c9774dca9f221a301c191d95" title="permalink">#</a> </h3> <p> The first thing I had to figure out was how to return various HTTP status codes. If you just return some model object, the default status code is <code>200 OK</code>. That's fine in many situations, but if you're implementing a proper REST API, you should be using headers and status codes to communicate with the client. </p> <p> The <code>ControllerBase</code> class defines many helper methods to return various other types of responses, such as <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.badrequest#Microsoft_AspNetCore_Mvc_ControllerBase_BadRequest">BadRequest</a> or <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.notfound#Microsoft_AspNetCore_Mvc_ControllerBase_NotFound">NotFound</a>. I had to figure out how to return such responses without access to the base class. </p> <p> Fortunately, ASP.NET Core is now open source, so it isn't too hard to look at the source code for those helper methods to see what they do. It turns out that they are just discoverable methods that create and return various result objects. So, instead of calling the <code>BadRequest</code> helper method, I can just return a <code>BadRequestResult</code>: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BadRequestResult</span>();</pre> </p> <p> Some of the responses were a little more involved, so I created my own domain-specific helper methods for them: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;Reservation201Created(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;r) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CreatedAtActionResult</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actionName:&nbsp;<span style="color:blue;">nameof</span>(Get), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controllerName:&nbsp;<span style="color:blue;">null</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;routeValues:&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;restaurantId,&nbsp;id&nbsp;=&nbsp;r.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>)&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value:&nbsp;r.ToDto()); }</pre> </p> <p> Apparently, the <code>controllerName</code> argument isn't required, so should be <code>null</code>. I had to experiment with various combinations to get it right, but I have self-hosted integration tests that cover this result. If it doesn't work as intended, tests will fail. </p> <p> Returning this <code>CreatedAtActionResult</code> object produces an HTTP response like this: </p> <p> <pre>HTTP/1.1 201 Created Content-Type: application/json; charset=utf-8 Location: https://example.net:443/restaurants/2112/reservations/276d124f20cf4cc3b502f57b89433f80 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;276d124f20cf4cc3b502f57b89433f80&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2022-01-14T19:45:00.0000000&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;donkeyman@example.org&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Don&nbsp;Keyman&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 }</pre> </p> <p> I've edited and coloured the response for readability. The <code>Location</code> URL actually also <a href="/2020/10/26/fit-urls">includes a digital signature</a>, which I've removed here just to make the example look a little prettier. </p> <p> In any case, returning various HTTP headers and status codes is less discoverable when you don't have a base class with all the helper methods, but once you've figured out the objects to return, it's straightforward, and the code is simple. </p> <h3 id="ecbc913d450d49b7ab92d2e0c193230e"> Generating links <a href="#ecbc913d450d49b7ab92d2e0c193230e" title="permalink">#</a> </h3> <p> A true (<a href="https://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a>) REST API uses <a href="https://en.wikipedia.org/wiki/HATEOAS">hypermedia as the engine of application state</a>; that is, <em>links</em>. This means that a HTTP response will typically include a JSON or XML representation with several links. <a href="/2020/10/26/fit-urls">This article</a> includes several examples. </p> <p> ASP.NET provides <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.iurlhelper">IUrlHelper</a> for exactly that purpose, and if you inherit from <code>ControllerBase</code> the above-mentioned <code>Url</code> property gives you convenient access to just such an object. </p> <p> When a class <em>doesn't</em> inherit from <code>ControllerBase</code>, you don't have convenient access to an <code>IUrlHelper</code>. Then what? </p> <p> It's possible to get an <code>IUrlHelper</code> via the framework's built-in Dependency Injection engine, but if you add such a dependency to a POCO Controller, you'll have to jump through all sorts of hoops to configure it in unit tests. That was exactly the situation I wanted to avoid, so that got me thinking about design alternatives. </p> <p> That was the real, underlying reason I came up with the idea to instead <a href="/2020/08/24/adding-rest-links-as-a-cross-cutting-concern">add REST links as a cross-cutting concern</a>. The Controllers are wonderfully free of that concern, which helps keeping the complexity down of the unit tests. </p> <p> I still have test coverage of the links, but I prefer testing HTTP-related behaviour via the HTTP API instead of relying on implementation details: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;Hipgnosta&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;Nono&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;The&nbsp;Vatican&nbsp;Cellar&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;RestaurantReturnsCorrectLinks(<span style="color:blue;">string</span>&nbsp;name) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;api&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SelfHostedApi</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;client&nbsp;=&nbsp;api.CreateClient(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;response&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;client.GetRestaurant(name); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:blue;">string</span>?&gt;(<span style="color:blue;">new</span>[] &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;urn:reservations&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;urn:year&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;urn:month&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;urn:day&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;response.ParseJsonContent&lt;<span style="color:#2b91af;">RestaurantDto</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actualRels&nbsp;=&nbsp;actual.Links.Select(l&nbsp;=&gt;&nbsp;l.Rel).ToHashSet(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Superset(expected,&nbsp;actualRels); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.All(actual.Links,&nbsp;AssertHrefAbsoluteUrl); }</pre> </p> <p> This test verifies that the representation returned in <code>response</code> contains four labelled links. Granted, this particular test only verifies that each link contains an absolute URL (which could, in theory, be <code>http://www.example.com</code>), but the whole point of <a href="/2020/10/26/fit-urls">fit URLs</a> is that they should be opaque. I've other tests that follow links to verify that the API affords the desired behaviour. </p> <h3 id="cd0769917ec441f786877bba0e630f00"> Authorisation <a href="#cd0769917ec441f786877bba0e630f00" title="permalink">#</a> </h3> <p> The final kind of behaviour that caused me a bit of trouble was authorisation. It's easy enough to annotate a Controller with an <code>[Authorize]</code> attribute, which is fine as long all you need is role-based authorisation. </p> <p> I did, however, run into one situation where I needed something closer to an <a href="https://en.wikipedia.org/wiki/Access-control_list">access control list</a>. The system that includes all these code examples is a multi-tenant restaurant reservation system. There's one protected resource: a day's schedule, intended for use by the restaurant's staff. Here's a simplified example: </p> <p> <pre>GET /restaurants/2112/schedule/2021/2/23 HTTP/1.1 Authorization: Bearer eyJhbGciOiJIUzI1NiIsInCI6IkpXVCJ9.eyJ... HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Nono&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;year&quot;</span>:&nbsp;2021, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;month&quot;</span>:&nbsp;2, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;day&quot;</span>:&nbsp;23, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;days&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-02-23&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;entries&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;19:45:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;reservations&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2c7ace4bbee94553950afd60a86c530c&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-02-23T19:45:00.0000000&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;anarchi@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Ann&nbsp;Archie&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> Since this resource contains personally identifiable information (email addresses) it's protected. You have to present a valid <a href="https://en.wikipedia.org/wiki/JSON_Web_Token">JSON Web Token</a> with required claims. Role claims, however, aren't enough. A minimum bar is that the token must contain a sufficient role claim like <code>"MaitreD"</code> shown above, but that's not enough. This is, after all, a multi-tenant system, and we don't want one restaurant's <em>MaitreD</em> to be able to see another restaurant's schedule. </p> <p> If ASP.NET can address that kind of problem with annotations, I haven't figured out how. The Controller needs to check an access control list against the resource being accessed. The above-mentioned <code>User</code> property of <code>ControllerBase</code> would be <em>really</em> convenient here. </p> <p> Again, there are ways to inject an entire <a href="https://docs.microsoft.com/dotnet/api/system.security.claims.claimsprincipal">ClaimsPrincipal</a> class into the Controller that needs it, but once more I felt that that would violate the Interface Segregation Principle. I didn't need an entire <code>ClaimsPrincipal</code>; I just needed a list of restaurant IDs that a particular JSON Web Token allows access to. </p> <p> As is often my modus operandi in such situations, I started by writing the code I wished to use, and then figured out how to make it work: </p> <p> <pre>[<span style="color:#2b91af;">HttpGet</span>(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/schedule/{year}/{month}/{day}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Get(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month,&nbsp;<span style="color:blue;">int</span>&nbsp;day) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!AccessControlList.Authorize(restaurantId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ForbidResult</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;the&nbsp;real&nbsp;work&nbsp;here...</span></pre> </p> <p> <code>AccessControlList</code> is a <a href="/2012/08/31/ConcreteDependencies">Concrete Dependency</a>. It's just a wrapper around a collection of IDs: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AccessControlList</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;restaurantIds;</pre> </p> <p> I had to create a new class in order to keep the built-in ASP.NET DI Container happy. Had I been doing <a href="/2014/06/10/pure-di">Pure DI</a> I could have just injected <code>IReadOnlyCollection&lt;int&gt;</code> directly into the Controller, but in this code base I used the built-in DI Container, which I had to configure like this: </p> <p> <pre>services.AddHttpContextAccessor(); services.AddTransient(sp&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">AccessControlList</span>.FromUser( &nbsp;&nbsp;&nbsp;&nbsp;sp.GetService&lt;<span style="color:#2b91af;">IHttpContextAccessor</span>&gt;().HttpContext.User));</pre> </p> <p> Apart from having to wrap <code>IReadOnlyCollection&lt;int&gt;</code> in a new class, I found such an implementation preferable to inheriting from <code>ControllerBase</code>. The Controller in question only depends on the services it needs: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ScheduleController</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRestaurantDatabase</span>&nbsp;restaurantDatabase, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">AccessControlList</span>&nbsp;accessControlList)</pre> </p> <p> The <code>ScheduleController</code> uses its <code>restaurantDatabase</code> dependency to look up a specific restaurant based on the <code>restaurantId</code>, the <code>repository</code> to read the schedule, and <code>accessControlList</code> to implement authorisation. That's what it needs, so that's its dependencies. It follows the Interface Segregation Principle. </p> <p> The <code>ScheduleController</code> class is easy to unit test, since a test can just create a new <code>AccessControlList</code> object whenever it needs to: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;GetScheduleForAbsentRestaurant() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ScheduleController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InMemoryRestaurantDatabase</span>(<span style="color:#2b91af;">Some</span>.Restaurant.WithId(2)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AccessControlList</span>(3)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Get(3,&nbsp;2089,&nbsp;12,&nbsp;9); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">NotFoundResult</span>&gt;(actual); }</pre> </p> <p> This test requests the schedule for a restaurant with the ID <code>3</code>, and the access control list <em>does</em> include that ID. The restaurant, however, doesn't exist, so despite correct permissions, the expected result is <code>404 Not Found</code>. </p> <h3 id="882c69dacd17476d899d3910a35aa62d"> Conclusion <a href="#882c69dacd17476d899d3910a35aa62d" title="permalink">#</a> </h3> <p> ASP.NET has supported POCO Controllers for some time now, but it's clearly not a mainstream scenario. The documentation and Visual Studio tooling assumes that your Controllers inherit from one of the framework base classes. </p> <p> You do, therefore, have to jump through a few hoops to make POCO Controllers work. The result, however, is more lightweight Controllers and better separation of concerns. I find the jumping worthwhile. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2d2c4c2ddf6845bba2cfe55a513ac3f4"> <div class="comment-author"><a href="https://taeguk.co.uk">Dave Shaw</a> <a href="#2d2c4c2ddf6845bba2cfe55a513ac3f4">#</a></div> <div class="comment-content"> <p>Hi Mark, as I read the part about <code>BadRequest</code> and <code>NotFound</code> being less discoverable, I wondered if you had considered creating a <code>ControllerHelper</code> (terrible name <a href="https://blog.ploeh.dk/2020/11/23/good-names-are-skin-deep/">I know</a>) class with <code>BadRequest</code>, etc? <br> Then by adding <code>using static Resteraunt.ControllerHelper</code> as a little bit of boilerplate code to the top of each POCO controller, you would be able to do <code>return BadRequest();</code> again. </p> </div> <div class="comment-date">2021-02-03 22:00 UTC</div> </div> <div class="comment" id="99c5531b27ff4dddb98847063b7e670e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#99c5531b27ff4dddb98847063b7e670e">#</a></div> <div class="comment-content"> <p> Dave, thank you for writing. I hadn't thought of that, but it's a useful idea. Someone would have to figure out how to write such a helper class, but once it exists, it would offer the same degree of discoverability. I'd suggest a name like <code>Result</code> or <code>HttpResult</code>, so that you could write e.g. <code>Result.BadRequest()</code>. </p> <p> Wouldn't adding a static import defy the purpose, though? As I see it, <a href="/2012/05/25/Designpatternsacrossparadigms#ebe4a8c5ba664c6fb5ea07c8b7e18555">C# discoverability is enabled by 'dot-driven development'</a>. Don't you lose that by a static import? </p> </div> <div class="comment-date">2021-02-04 7:18 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Self-hosted integration tests in ASP.NET https://blog.ploeh.dk/2021/01/25/self-hosted-integration-tests-in-aspnet 2021-01-25T07:45:00+00:00 Mark Seemann <div id="post"> <p> <em>A way to self-host a REST API and test it through HTTP.</em> </p> <p> In 2020 I developed a sizeable code base for an online restaurant REST API. In the spirit of <a href="/outside-in-tdd">outside-in TDD</a>, I found it best to test the HTTP behaviour of the API by actually interacting with it via HTTP. </p> <p> Sometimes ASP.NET offers more than one way to achieve the same end result. For example, to return <code>200 OK</code>, you can use both <code>OkObjectResult</code> and <code>ObjectResult</code>. I don't want my tests to be coupled to such implementation details, so by testing an API via HTTP instead of using the ASP.NET object model, I decouple the two. </p> <p> You can easily self-host an ASP.NET web API and test it using an <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpclient">HttpClient</a>. In this article, I'll show you how I went about it. </p> <p> <ins datetime="2021-06-15T06:14Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="a070ceb1f9a84f2988aa7e4c59f38397"> Reserving a table <a href="#a070ceb1f9a84f2988aa7e4c59f38397" title="permalink">#</a> </h3> <p> In true outside-in fashion, I'll first show you the test. Then I'll break it down to show you how it works. </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;<span style="color:#74531f;">ReserveTableAtNono</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">api</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SelfHostedApi</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">client</span>&nbsp;=&nbsp;<span style="color:#1f377f;">api</span>.<span style="color:#74531f;">CreateClient</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">at</span>&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Today.<span style="color:#74531f;">AddDays</span>(434).<span style="color:#74531f;">At</span>(20,&nbsp;15); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">dto</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Some</span>.Reservation.<span style="color:#74531f;">WithDate</span>(<span style="color:#1f377f;">at</span>).<span style="color:#74531f;">WithQuantity</span>(6).<span style="color:#74531f;">ToDto</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">response</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">client</span>.<span style="color:#74531f;">PostReservation</span>(<span style="color:#a31515;">&quot;Nono&quot;</span>,&nbsp;<span style="color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#74531f;">AssertRemainingCapacity</span>(<span style="color:#1f377f;">client</span>,&nbsp;<span style="color:#1f377f;">at</span>,&nbsp;<span style="color:#a31515;">&quot;Nono&quot;</span>,&nbsp;4); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#74531f;">AssertRemainingCapacity</span>(<span style="color:#1f377f;">client</span>,&nbsp;<span style="color:#1f377f;">at</span>,&nbsp;<span style="color:#a31515;">&quot;Hipgnosta&quot;</span>,&nbsp;10); }</pre> </p> <p> This test uses <a href="https://xunit.net">xUnit.net</a> 2.4.1 to make a reservation at the restaurant named <em>Nono</em>. The first line that creates the <code>api</code> variable spins up a self-hosted instance of the REST API. The next line creates an <code>HttpClient</code> configured to communicate with the self-hosted instance. </p> <p> The test proceeds to create a Data Transfer Object that it posts to the <em>Nono</em> restaurant. It then asserts that the remaining capacity at the <em>Nono</em> and <em>Hipgnosta</em> restaurants are as expected. </p> <p> You'll see the implementation details soon, but I first want to discuss this high-level test. As is the case with most of my code, it's far from perfect. If you're not familiar with this code base, you may have plenty of questions: <ul> <li>Why does it make a reservation 434 days in the future? Why not 433, or 211, or 1?</li> <li>Is there anything significant about the quantity <em>6?</em></li> <li>Why is the expected remaining capacity at <em>Nono 4?</em></li> <li>Why is the expected remaining capacity at <em>Hipgnosta 10?</em> Why does it even verify that?</li> </ul> To answer the easiest question first: There's nothing special about 434 days. The only <a href="/2021/01/11/waiting-to-happen">requirement is that it's a positive number</a>, so that the reservation is in the future. That makes this test a great candidate for a <a href="/property-based-testing-intro">property-based test</a>. </p> <p> The three other questions are all related. A bit of background is in order. I wrote this test during a process where I turned the system into a multi-tenant system. Before that change, there was only one restaurant, which was <em>Hipgnosta</em>. I wanted to verify that if you make a reservation at another restaurant (here, <em>Nono</em>) it changes the observable state of that restaurant, and not of <em>Hipgnosta</em>. </p> <p> The way these two restaurants are configured, <em>Hipgnosta</em> has <a href="/2020/01/27/the-maitre-d-kata">a single communal table that seats ten guests</a>. This explains why the expected capacity of <em>Hipgnosta</em> is <em>10</em>. Making a reservation at <em>Nono</em> shouldn't affect <em>Hipgnosta</em>. </p> <p> <em>Nono</em> has a more complex table configuration. It has both standard and communal tables, but the largest table is a six-person communal table. There's only one table of that size. The next-largest tables are four-person tables. Thus, a reservation for six people reserves the largest table that day, after which only four-person and two-person tables are available. Therefore the remaining capacity ought to be <em>4</em>. </p> <p> The above test knows all this. You are welcome to criticise such hard-coded knowledge. There's a real risk that it might make it more difficult to maintain the test suite in the future. </p> <p> Certainly, had this been a unit test, and not an integration test, I wouldn't have accepted so much implicit knowledge - particularly because I mostly apply <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a>, and <a href="/2015/05/07/functional-design-is-intrinsically-testable">pure functions should have isolation</a>. Functions shouldn't depend on implicit global state; they should return a value based on input arguments. That's a bit of digression, though. </p> <p> These are integration tests, which I mostly use for smoke tests and to verify HTTP-specific behaviour. I have unit tests for fine-grained testing of edge cases and variations of input. While I wouldn't accept so much implicit knowledge from a unit test, I find that it so far works well with integration tests. </p> <h3 id="db1185165dc748aba67c8daa6278f523"> Self-hosting <a href="#db1185165dc748aba67c8daa6278f523" title="permalink">#</a> </h3> <p> It only takes a <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactory-1">WebApplicationFactory</a> to self-host an ASP.NET API. You can use it directly, but if you want to modify the hosted service in some way, you can also inherit from it. </p> <p> I want my self-hosted integration tests to run as <a href="/2019/02/18/from-interaction-based-to-state-based-testing">state-based tests</a> that use an in-memory database instead of SQL Server. I've defined <code>SelfHostedApi</code> for that purpose: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SelfHostedApi</span>&nbsp;:&nbsp;<span style="color:#2b91af;">WebApplicationFactory</span>&lt;<span style="color:#2b91af;">Startup</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">ConfigureWebHost</span>(<span style="color:#2b91af;">IWebHostBuilder</span>&nbsp;<span style="color:#1f377f;">builder</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">builder</span>.<span style="color:#74531f;">ConfigureServices</span>(<span style="color:#1f377f;">services</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">services</span>.<span style="color:#74531f;">RemoveAll</span>&lt;<span style="color:#2b91af;">IReservationsRepository</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">services</span>.<span style="color:#74531f;">AddSingleton</span>&lt;<span style="color:#2b91af;">IReservationsRepository</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The way that <code>WebApplicationFactory</code> works, its <code>ConfigureWebHost</code> method runs <em>after</em> the <code>Startup</code> class' <code>ConfigureServices</code> method. Thus, when <code>ConfigureWebHost</code> runs, the <code>services</code> collection is already configured to use SQL Server. As <a href="/2020/04/20/unit-bias-against-collections#e6675033a3a9dc8a21c64650dff91b8432a9a151">Julius H so kindly pointed out to me</a>, the <code>RemoveAll</code> extension method removes all existing registrations of a service. I use it to remove the SQL Server dependency from the system, after which I replace it with a test-specific in-memory implementation. </p> <p> Since the in-memory database is configured with Singleton lifetime, that instance is going to be around for the lifetime of the service. While it's only keeping track of things in memory, it'll keep state until the service shuts down, which happens when the above <code>api</code> variable goes out of scope. </p> <p> Notice that <a href="/2020/11/30/name-by-role">I named the class by the role it plays</a> rather than which base class it derives from. </p> <h3 id="af89655ac4d948f8a749aa20fffe2b12"> Posting a reservation <a href="#af89655ac4d948f8a749aa20fffe2b12" title="permalink">#</a> </h3> <p> The <code>PostReservation</code> method is an extension method on <code>HttpClient</code>: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;&nbsp;<span style="color:#74531f;">PostReservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">HttpClient</span>&nbsp;<span style="color:#1f377f;">client</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#2b91af;">JsonSerializer</span>.<span style="color:#74531f;">Serialize</span>(<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">content</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StringContent</span>(<span style="color:#1f377f;">json</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">content</span>.Headers.ContentType.MediaType&nbsp;=&nbsp;<span style="color:#a31515;">&quot;application/json&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">resp</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">client</span>.<span style="color:#74531f;">GetRestaurant</span>(<span style="color:#1f377f;">name</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">resp</span>.<span style="color:#74531f;">EnsureSuccessStatusCode</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">rest</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">resp</span>.<span style="color:#74531f;">ParseJsonContent</span>&lt;<span style="color:#2b91af;">RestaurantDto</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">address</span>&nbsp;=&nbsp;<span style="color:#1f377f;">rest</span>.Links.<span style="color:#74531f;">FindAddress</span>(<span style="color:#a31515;">&quot;urn:reservations&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">client</span>.<span style="color:#74531f;">PostAsync</span>(<span style="color:#1f377f;">address</span>,&nbsp;<span style="color:#1f377f;">content</span>); }</pre> </p> <p> It's part of a larger set of methods that enables an <code>HttpClient</code> to interact with the REST API. Three of those methods are visible here: <code>GetRestaurant</code>, <code>ParseJsonContent</code>, and <code>FindAddress</code>. These, and many other, methods form a client API for interacting with the REST API. While this is currently test code, it's ripe for being extracted to a reusable client SDK library. </p> <p> I'm not going to show all of them, but here's <code>GetRestaurant</code> to give you a sense of what's going on: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;&nbsp;<span style="color:#74531f;">GetRestaurant</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">HttpClient</span>&nbsp;<span style="color:#1f377f;">client</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">name</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">homeResponse</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">client</span>.<span style="color:#74531f;">GetAsync</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#2b91af;">UriKind</span>.Relative)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">homeResponse</span>.<span style="color:#74531f;">EnsureSuccessStatusCode</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">homeRepresentation</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">homeResponse</span>.<span style="color:#74531f;">ParseJsonContent</span>&lt;<span style="color:#2b91af;">HomeDto</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">restaurant</span>&nbsp;=&nbsp;<span style="color:#1f377f;">homeRepresentation</span>.Restaurants.<span style="color:#74531f;">First</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Name&nbsp;==&nbsp;<span style="color:#1f377f;">name</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">address</span>&nbsp;=&nbsp;<span style="color:#1f377f;">restaurant</span>.Links.<span style="color:#74531f;">FindAddress</span>(<span style="color:#a31515;">&quot;urn:restaurant&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">client</span>.<span style="color:#74531f;">GetAsync</span>(<span style="color:#1f377f;">address</span>); }</pre> </p> <p> The REST API has only a single documented address, which is the 'home' resource at the relative URL <code>""</code>; i.e. the root of the API. In this incarnation of the API, the home resource responds with a JSON array of restaurants. The <code>GetRestaurant</code> method finds the restaurant with the desired name and finds its address. It then issues another <code>GET</code> request against that address, and returns the response. </p> <h3 id="00d69af4ad6b43538b86f3e27fb7c4ee"> Verifying state <a href="#00d69af4ad6b43538b86f3e27fb7c4ee" title="permalink">#</a> </h3> <p> The verification phase of the above test calls a private helper method: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;<span style="color:#74531f;">AssertRemainingCapacity</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpClient</span>&nbsp;<span style="color:#1f377f;">client</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="color:#1f377f;">date</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">name</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">expected</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">response</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">client</span>.<span style="color:#74531f;">GetDay</span>(<span style="color:#1f377f;">name</span>,&nbsp;<span style="color:#1f377f;">date</span>.Year,&nbsp;<span style="color:#1f377f;">date</span>.Month,&nbsp;<span style="color:#1f377f;">date</span>.Day); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">day</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#1f377f;">response</span>.<span style="color:#74531f;">ParseJsonContent</span>&lt;<span style="color:#2b91af;">CalendarDto</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">All</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">day</span>.Days.<span style="color:#74531f;">Single</span>().Entries, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">e</span>&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>(<span style="color:#1f377f;">expected</span>,&nbsp;<span style="color:#1f377f;">e</span>.MaximumPartySize)); }</pre> </p> <p> It uses another of the above-mentioned client API extension methods, <code>GetDay</code>, to inspect the REST API's calendar entry for the restaurant and day in question. Each calendar contains a series of time entries that lists the largest party size the restaurant can accept at that time slot. The two restaurants in question only have single seatings, so once you've booked a six-person table, you have it for the entire evening. </p> <p> Notice that verification is done by interacting with the system itself. No <a href="http://xunitpatterns.com/Back%20Door%20Manipulation.html">Back Door Manipulation</a> is required. I favour this if at all possible, since I believe that it offers better confidence that the system behaves as it should. </p> <h3 id="e93c5eb19e1d4f358197b4a1c048f01e"> Conclusion <a href="#e93c5eb19e1d4f358197b4a1c048f01e" title="permalink">#</a> </h3> <p> It's been possible to self-host .NET REST APIs for testing purposes at least since 2012, but it's only become easier over the years. All you need to get started is the <code>WebApplicationFactory&lt;TEntryPoint&gt;</code> class, although you're probably going to need a derived class to override some of the system configuration. </p> <p> From there, you can interact with the self-hosted system using the standard <code>HttpClient</code> class. </p> <p> Since I configure these tests to run on an in-memory database, the execution time is comparable to 'normal' unit tests. I admit that I haven't measured it, but that's because I haven't felt the need to do so. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b2a9eb23454748c18b85927346d33ba9"> <div class="comment-author">Grzegorz Gałęzowski <a href="#b2a9eb23454748c18b85927346d33ba9">#</a></div> <div class="comment-content"> <p>Hi Mark, 2 questions from me: <ol> <li>What kind of integration does this test verify? The integration between the HTTP request processing part and the service logic? If so, why fake the database only and not the whole logic?</li> <li>If you fake the database here, then would you have a separate test for testing integration with DB (e.g. some kind of "adapter test", as described in the GOOS book)?</li> </ol> </p> </div> <div class="comment-date">2021-01-25 20:34 UTC</div> </div> <div class="comment" id="05077196c8394879829e61d8c08fff08"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#05077196c8394879829e61d8c08fff08">#</a></div> <div class="comment-content"> <p> Grzegorz, thank you for writing. </p> <p> 1. As I already hinted at, the motivation for this test was that I was expanding the code from a single-tenant to a multi-tenant system. I did this in small steps, using tests to drive the augmented behaviour. Before I added the above test, I'd introduced the concept of a <em>restaurant ID</em> to identify each restaurant, and initially added a method overload to my data access interface that required such an ID: </p> <p> <pre><span style="color:#2b91af;">Task</span>&nbsp;<span style="color:#74531f;">Create</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">restaurantId</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>);</pre> </p> <p> I was using the <a href="https://martinfowler.com/bliki/StranglerFigApplication.html">Strangler pattern</a> to be able to move in small steps. One step was to add overloads like the above. Subsequent steps would be to move existing callers from the 'legacy' overload to the new overload, and then finally delete the 'legacy' overload. </p> <p> When moving from a single-tenant system to a multi-tenant system, I had to <a href="https://en.wikipedia.org/wiki/Grandfather_clause">grandfather in</a> the existing restaurant, which I arbitrarily chose to give the restaurant ID <em>1</em>. During this process, I kept the existing system working so as to not break existing clients. </p> <p> I gradually introduced method overloads that took a restaurant ID as a parameter (as above), and then deleted the legacy methods that didn't take a restaurant ID. In order to not break anything, I'd initially be calling the new methods with the hard-coded restaurant ID <em>1</em>. </p> <p> The above test was one in a series of tests that followed. Their purpose was to verify that the system had become truly multi-tenant. Before I added that test, an attempt to make a reservation at <em>Nono</em> would result in a call to <code>Create(1, reservation)</code> because there was still hard-coded restaurant IDs left in the code base. </p> <p> (To be honest, the above is a simplified account of events. It was actually more complex than that, since it involved hypermedia controls. I also, for reasons that I may divulge in the future, didn't perform this work behind a feature flag, which under other circumstances would have been appropriate.) </p> <p> What the above test verifies, then, is that the reservation gets associated with the correct restaurant, instead of the restaurant that was grandfathered in (<em>Hipgnosta</em>). An in-memory (<a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a>) database was sufficient to demonstrate that. </p> <p> Why didn't I use the real, relational database? Read on. </p> <p> 2. As I've <a href="/2020/07/20/closing-database-connections-during-test-teardown">recently discussed</a>, integration tests that involve SQL Server are certainly possible. They tend to be orders-of-magnitudes slower than unit tests that run entirely in memory, so I only add them if I deem them necessary. </p> <p> I interact with databases according to the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>. The above <code>Create</code> method is an example of that. The method is defined by the needs of the client code rather than on implementation details. </p> <p> The actual implementation of the data access interface is, as you imply, an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a>. It adapts the SQL Server SDK (i.e. ADO.NET) to my data access interface. As long as I can keep such Adapters <a href="http://xunitpatterns.com/Humble%20Object.html">Humble Objects</a> I don't cover them by tests. </p> <p> Such Adapters are often just mappings: This class field maps to that table column, that field to that other column, and so on. If there's no logic, there isn't much to test. </p> <p> This doesn't mean that bugs can't appear in database Adapters. If that happens, I may introduce regression tests that involve the database. I prefer to do this in a separate test suite, however, since such tests tend to be slow, and <a href="/2012/05/24/TDDtestsuitesshouldrunin10secondsorless">TDD test suites should be fast</a>. </p> </div> <div class="comment-date">2021-01-26 8:32 UTC</div> </div> <div class="comment" id="3e56b76abc8e48e3a24174e9ba55d616"> <div class="comment-author">Brian Elgaard Bennett <a href="#3e56b76abc8e48e3a24174e9ba55d616">#</a></div> <div class="comment-content"> <p>Hi Mark, I have been a fan of "integration" style testing since I found <a href="https://andrewlock.net/should-you-unit-test-controllers-in-aspnetcore/">Andrew Locks's post on the topic</a>. In fact, I personally find that it makes sense to <a href="https://medium.com/swlh/should-you-unit-test-in-asp-net-core-793de767ac68">take that idea to one extreme</a>. </p> <p> As far as I can see, your test assumes something like "Given that no reservations have been made". Did you consider to make that "Given" explicitly recognizable in your test? </p> <p> I am asking because this is a major pet topic of mine and I would love to hear your thoughts. I am not suggesting writing Gherkin instead of C# code, just if and how you would make the "Given" assumption explicit. </p> <p> A more practical comment is on your use of services.RemoveAll&lt;IReservationsRepository&gt;(); It seems extreme to remove all, as all you want to achieve is to overwrite a few service declarations. Andrew Lock demonstrates how to keep all the ASP.NET middleware intact by keeping most of the service registrations. </p> <p> I usually use ConfigureTestServices on WebApplicationFactory, something like, <pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:#1f377f;">factory</span>.<span style="color:#74531f;">WithWebHostBuilder</span>(<span style="color:#1f377f;">builder</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">builder</span>.<span style="color:#74531f;">ConfigureTestServices</span>(<span style="color:#2b91af;">MyTestCompositionRoot</span>.<span style="color:#74531f;">Initialize</span>)); </pre> </p> </div> <div class="comment-date">2021-02-22 10:40 UTC</div> </div> <div class="comment" id="0078721da53e4aa792698fae4a6ab325"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0078721da53e4aa792698fae4a6ab325">#</a></div> <div class="comment-content"> <p> Brian, thank you for writing. You're correct that one implicit assumption is that no prior reservations have been made to <em>Nono</em> on that date. The reality is, as you suggest, that there are no reservations <em>at all</em>, but the test doesn't require that. It only relies on the weaker assumption that there are prior reservations to neither <em>Nono</em> nor <em>Hipgnosta</em> on the date in question. </p> <p> I often find it tricky to make 'negative' assumptions explicit. How much should one highlight? Is it also relevant to highlight that <code>SelfHostedApi</code> isn't going to send notification emails? That it doesn't write to a persistent log? That it doesn't have the capability to <a href="https://stackoverflow.com/q/2773004/126014">launch missiles</a>? </p> <p> I'm not saying that it's irrelevant to highlight certain assumptions, but I'm not sure that I have a clear heuristic for it. After all, it depends on what the reader cares about, and how clear the assumptions are. </p> <p> If it became clear to me that the assumption of 'no prior reservations' was important to the reader, I might consider to embed that in the name of the test, or perhaps by adding a comment. Another option is to hide the initialisation of <code>SelfHostedApi</code> behind a required factory method so that you'd have to initialise it as <code>using var api = SelfHostedApi.StartEmpty();</code> </p> <p> In this particular code base, though, that <code>SelfHostedApi</code> is used repeatedly. A programmer who regularly works with that code base will quickly internalise the assumptions embedded in that self-hosted service. </p> <p> Granted, it can be a dangerous strategy to rely too much on implicit assumptions, because it can make it more difficult to change things if those assumptions change in the future. I do, after all, favour the wisdom from <a href="https://www.python.org/dev/peps/pep-0020/">the Zen of Python</a>: <em>Explicit is better than implicit.</em> </p> <p> Making such things as we discuss here explicit does, however, tend to make the tests more verbose. Pragmatically, then, there's a balance to strike. I'm not claiming that the test shown here is perfect. In a a living code base, this would be a concern that I would keep a constant eye on. I'd fiddle with it, trying out making things more explicit, less explicit, and so on, to see what works best in context. </p> <p> Your other point about <code>services.RemoveAll&lt;IReservationsRepository&gt;()</code> I don't understand. Why is that extreme? It's literally the most minimal, precise change I can make. It removes only the <code>IReservationsRepository</code>, of which there's exactly one Singleton instance: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">connStr</span>&nbsp;=&nbsp;Configuration.<span style="color:#74531f;">GetConnectionString</span>(<span style="color:#a31515;">&quot;Restaurant&quot;</span>); <span style="color:#1f377f;">services</span>.<span style="color:#74531f;">AddSingleton</span>&lt;<span style="color:#2b91af;">IReservationsRepository</span>&gt;(<span style="color:#1f377f;">sp</span>&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">logger</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sp</span>.<span style="color:#74531f;">GetService</span>&lt;<span style="color:#2b91af;">ILogger</span>&lt;<span style="color:#2b91af;">LoggingReservationsRepository</span>&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">postOffice</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sp</span>.<span style="color:#74531f;">GetService</span>&lt;<span style="color:#2b91af;">IPostOffice</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">EmailingReservationsRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">postOffice</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">LoggingReservationsRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">logger</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlReservationsRepository</span>(<span style="color:#1f377f;">connStr</span>))); });</pre> </p> <p> The integration test throws away that Singleton object graph, but nothing else. </p> </div> <div class="comment-date">2021-02-23 9:38 UTC</div> </div> <div class="comment" id="8f23293c06234053853c0374b9200dd3"> <div class="comment-author">Brian Elgaard Bennett <a href="#8f23293c06234053853c0374b9200dd3">#</a></div> <div class="comment-content"> <p>Hi Mark, thanks for a thorough answer.</p> <p>Alas, my second question was not thought through - I somehow thought you threw away all service registrations, which you obviously do not. In fact, I do exactly what you do, usually with Replace and thereby <i>implicitly</i> assume that there is only one registration. My Bad. <p>We very much agree in the importance of favouring <i>explicit</i> assumptions.</p> <p>The solution to stating 'negative' assumptions, as long as we talk about the 'initial context' (the 'Given') is actually quite simple if your readers know that you always state assumptions explicitly. If none are stated, it's the same as saying 'the world is empty' - at least when it comes to the bounded context of whatever you are testing. I find that the process of identifying a minimal set of assumptions per test is a worthwhile exercise.</p> <p>So, IMHO it boils down to consistently stating all assumptions and making the relevant bounded context clear.</p> </div> <div class="comment-date">2021-02-23 12:18 UTC</div> </div> <div class="comment" id="60934ff25b0e45bebca4f00749a6b44c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#60934ff25b0e45bebca4f00749a6b44c">#</a></div> <div class="comment-content"> <p> Brian, thank you for writing. I agree that it's a good rule of thumb to expect the world to be empty unless explicitly stated otherwise. This is the reason that I love functional programming. </p> <p> One question, however, is whether a statement is sufficiently explicit. The integration test shown here is a good example. What's actually happening is that <code>SelfHostedApi</code> sets up a self-hosted service configured in a certain way. It contains three restaurants, of which two are relevant in this particular test. Each restaurant comes with quite a bit of configuration: opening time, closing time, seating duration, and the configuration of tables. Just consider the configuration of <em>Nono:</em> </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Id&quot;</span>:&nbsp;2112, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Nono&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;OpensAt&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;18:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;LastSeating&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;21:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;SeatingDuration&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;6:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Tables&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;TableType&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Communal&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Seats&quot;</span>:&nbsp;6 &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;TableType&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Communal&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Seats&quot;</span>:&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;TableType&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Standard&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Seats&quot;</span>:&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;TableType&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Standard&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Seats&quot;</span>:&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;TableType&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Standard&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Seats&quot;</span>:&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;TableType&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Standard&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;Seats&quot;</span>:&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> The code base also enables a test writer to express the above configuration as code instead of JSON, but the point I'm trying to make is that you probably don't want that amount of detail to appear in the <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">Arrange phase</a> of each test, regardless of how explicit it'd be. </p> <p> To work around an issue like this, you might instead define an <a href="/2017/09/11/test-data-without-builders">immutable test data variable</a> called <code>nono</code>. Then, in each test that uses these restaurants, you might include something like <code>AddRestaurant(nono)</code> or <code>AddRestaurant(hipgnosta)</code> in the Arrange phase (hypothetical API). That's not <em>quite</em> as explicit, because it hides the actual values away, but is probably still good enough. </p> <p> Then you find yourself always adding the same restaurants, so you say to yourself: <em>It'd be nice if I had a helper method like <code>AddStandardRestaurants</code></em>. So, you may add such a helper method. </p> <p> But you find that for the integration tests, you <em>always</em> call that helper method, so ultimately, you decide to just roll it into the definition of <code>SelfHostedApi</code>. That's basically what's going on here. </p> <p> I don't recall whether I explicitly stated the following in the article, but I use integration tests consistent with the <a href="https://martinfowler.com/bliki/TestPyramid.html">test pyramid</a>. The purpose of the integration tests is to verify that components integrate correctly, and that the HTTP API behaves according to contract. In my experience, I can let the 'givens' be somewhat implicit and still keep the code maintainable. For integration tests, that is. </p> <p> On the unit level, I favour pure functions, which are <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. Due to <em>isolation</em>, everything that affects the behaviour of a pure function must be explicitly passed as arguments, and as a bi-product, they become explicit as part of a test's Arrange phase. </p> <p> But, again, let me reiterate that I agree with you. I don't claim that the code shown here is perfect. It's so implicit that it makes me uncomfortable too, but on the other hand, I think that a test like the above communicates intent well. The more explicit details one adds, the more they may drown out the intent. It's a difficult balance to strike. </p> </div> <div class="comment-date">2021-02-25 12:34 UTC</div> </div> <div class="comment" id="8f23293c06234053853c0374b9200d42"> <div class="comment-author">Brian Elgaard Bennett <a href="#8f23293c06234053853c0374b9200d42">#</a></div> <div class="comment-content"> <p>Hi Mark, I'm happy that we agree. Still, this is a pet topic of mine so please allow me one additional comment.</p> <p>I wish all code had at least a functional core, as that would make testing so much easier. You could say that explicitly stating initial context resembles testing pure functions.</p> <p>I do believe that it is possible to state initial context without being overwhelmed by details. It can by no means be fully automated - this is where the mind of a tester must be used. Also, it will never be perfect, but at least it can be documented somewhat systematically and kept under version control, so the reasoning can be challenged.</p> <p>In your example, I would <i>not</i> add Nono or Hipgnosta or a standard restaurant, as those names say noting about what makes my test pass.</p> <p>Rather, I would add e.g. a "restaurant with a 6 and a 4 person table", as this seems to be what makes your test pass, if I understood it correctly. In another test I might have "restaurant which is open from 6 PM with last seating at 9 PM" if I want to test time aspects of booking. It may be the same Json file used in those two tests, because what's most important is that you document what makes each individual test pass.</p> <p>However, if it's not too much trouble (it sometimes is) I would even try to minimize the initial context. For example, I would test timing aspects with a minimal restaurant with a single table. Unless, timing and tables interact - maybe late bookings are OK if the restaurant is starved for customers? This is where the "tester mind" comes in handy.</p> <p>In your restaurant example, that would mean having quite a few restaurant definitions, but only definitions which include the needed context for each test. My experience is that minimizing the initial context will teach you a lot about the code. This is similar to unit tests which focus on a small part of code, but "minimizing initial context" allows for less fragile tests than when "minimizing code".</p> <p>We started using this approach because our tests did not give sufficient confidence. Tests were flaky and it was difficult to see what was tested and why. I can safely say that this is much better now.</p> <p>My examples of initial context above may appear like they could grow out of control, but our experience so far is that it is possible to find a relatively small set of equivalence classes of initial context items, even for fairly complex functionality.</p> </div> <div class="comment-date">2021-02-28 15:24 UTC</div> </div> <div class="comment" id="76bca252379c4f73a676ea505f16a550"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#76bca252379c4f73a676ea505f16a550">#</a></div> <div class="comment-content"> <p> Brian, I agree that in unit testing, being able to reduce test cases to minimal examples is useful. Once you have those minimal examples, being explicit about all preconditions is more attainable. This is why, when I coach teams, I try to teach them about <a href="https://en.wikipedia.org/wiki/Equivalence_partitioning">equivalence class partitioning</a>. If, within each equivalence class, you can impose some sort of (partial) ordering, you may be able to pick a canonical representation that constitutes the minimal example. </p> <p> This is basically what the shrinking process of property-based testing frameworks attempt to do. </p> <p> Thus, when my testing goal is to explore the boundaries of the system under test, I go to great lengths to ensure that the arrange phase is as explicit as possible. I <a href="/2021/02/15/when-properties-are-easier-than-examples">recently published an example of how I use property-based testing</a> to such an end. </p> <p> The goal of the integration test in the present article, however, is <em>not</em> to explore boundary cases or verify the correctness of algorithms in question. I have unit tests for that. The goal is to examine whether things may be correctly 'clicked together'. </p> <p> Expressing an explicit minimal context for the above test is more involved than you outline. Having two restaurants, with two differently sized tables, is only the beginning. You must also ensure that they are open at the same time, so that an attempted reservation would fit both. Furthermore, you must then pick the date and time of the reservation to fit within that time window. This is still not hopelessly difficult, and might make for a good exercise, but it could easily add 4-5 extra lines of code to the test. </p> <p> And again: had this been a unit test, I'd happily added those extra lines, but for an integration test, I felt that it was less important. </p> </div> <div class="comment-date">2021-03-02 11:24 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Parametrised test primitive obsession code smell https://blog.ploeh.dk/2021/01/18/parametrised-test-primitive-obsession-code-smell 2021-01-18T06:30:00+00:00 Mark Seemann <div id="post"> <p> <em>Watch out for this code smell with some unit testing frameworks.</em> </p> <p> In a <a href="/2021/01/11/waiting-to-happen">previous article</a> you saw this <a href="/2019/04/01/an-example-of-state-based-testing-in-c">state-based integration test</a>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(1049,&nbsp;19,&nbsp;00,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [<span style="color:#2b91af;">InlineData</span>(1130,&nbsp;18,&nbsp;15,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;956,&nbsp;16,&nbsp;55,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;433,&nbsp;17,&nbsp;30,&nbsp;<span style="color:#a31515;">&quot;shli@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Shanghai&nbsp;Li&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;days, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;hours, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;minutes, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SystemClock</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InMemoryRestaurantDatabase</span>(<span style="color:#2b91af;">Grandfather</span>.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;<span style="color:#a31515;">&quot;B50DF5B1-F484-4D99-88F9-1915087AF568&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at.ToString(<span style="color:#a31515;">&quot;O&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Guid</span>.Parse(dto.Id), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Parse(dto.At,&nbsp;<span style="color:#2b91af;">CultureInfo</span>.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Email</span>(dto.Email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Name</span>(dto.Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db.Grandfather); }</pre> </p> <p> This was the test <em>after</em> I improved it. Still, I wasn't satisfied with it. It has several problems. Take a few moments to consider it. Can you identify any problems? Which ones? </p> <p> <ins datetime="2021-07-09T05:39Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="6da86d1040e544a3a67f509c0f3aade7"> Size <a href="#6da86d1040e544a3a67f509c0f3aade7" title="permalink">#</a> </h3> <p> I know that you're not familiar with all the moving parts. You don't know how <code>ReservationDto</code> or <code>Reservation</code> are implemented. You don't know what <code>InMemoryRestaurantDatabase</code> is, or how <code>ReservationsController</code> behaves. Still, the issues I have in mind aren't specific to a particular code base. </p> <p> I feel that the method is verging on being too big. Quantifiably, it doesn't fit in an <a href="/2019/11/04/the-80-24-rule">80x24 box</a>, but that's just an arbitrary <a href="/2020/04/13/curb-code-rot-with-thresholds">threshold</a> anyway. Still, I think it's grown to a size that makes me uncomfortable. </p> <p> If you aren't convinced, think of this code example as a stand-in for something larger. In the above test, a reservation contains five smaller values (<code>Id</code>, <code>At</code>, <code>Email</code>, <code>Name</code>, and <code>Quantity</code>). How would a similar test look if the object in question contains ten or twenty values? </p> <p> In the decades I've been programming and consulting, I've seen plenty of code bases. Data objects made from twenty fields are hardly unusual. </p> <p> What would a similar test look like if the <code>dto</code> and the <code>expected</code> object required twenty smaller values? </p> <p> The test would be too big. </p> <h3 id="e251bee2df414c5cb0d489f31183c664"> Primitive obsession <a href="#e251bee2df414c5cb0d489f31183c664" title="permalink">#</a> </h3> <p> A test like this one contains a mix of essential behaviour and implementation details. The behaviour that it verifies is that when you <code>Post</code> a valid <code>dto</code>, the data makes it all the way to the database. </p> <p> Exactly how the <code>dto</code> or the <code>expected</code> value are constructed is less relevant for the test. Yet it's intermingled with the test of behaviour. The signal-to-noise ratio in the test isn't all that great. What can you do to improve things? </p> <p> As given, it seems difficult to do much. The problem is <a href="/2011/05/25/DesignSmellPrimitiveObsession">primitive obsession</a>. While this is a <a href="http://xunitpatterns.com/Parameterized%20Test.html">parametrised test</a>, all the method parameters are primitives: integers and strings. The makes it hard to introduce useful abstractions. </p> <p> In C# (and probably other languages as well) parametrised tests often suffer from primitive obsession. The most common data source is an attribute (AKA <em>annotation</em>), like <a href="https://xunit.net">xUnit.net</a>'s <code>[InlineData]</code> attribute. This isn't a limitation of xUnit.net, but rather of .NET attributes. You can only create attributes with primitive values and arrays. </p> <p> What <em>is</em> a limitation of xUnit.net (and the other mainstream .NET testing frameworks, as far as I know) is that tests aren't first-class values. In <a href="https://www.haskell.org">Haskell</a>, by contrast, it's <a href="/2018/04/30/parametrised-unit-tests-in-haskell">easy to write parametrised tests using the normal language constructs</a> exactly because tests are first-class values. (I hope that the next version of xUnit.net will support tests as first-class values.) </p> <p> Imagine that instead of only five constituent fields, you'd have to write a parametrised test for objects with twenty primitive values. As long as you stick with attribute-based data sources, you'll be stuck with primitive values. </p> <p> Granted, attributes like <code>[InlineData]</code> are lightweight, but over the years, my patience with them has grown shorter. They lock me into primitive obsession, and I don't appreciate that. </p> <h3 id="9342d4306e174fe79946a131b9c6894c"> Essential test <a href="#9342d4306e174fe79946a131b9c6894c" title="permalink">#</a> </h3> <p> While tests as first-class values aren't an option in xUnit.net, you can provide other data sources for the <code>[Theory]</code> attribute than <code>[InlineData]</code>. It's not as lightweight, but it breaks the primitive obsession and re-enables normal code design techniques. It enables you to reduce the test itself to its essence. You no longer have to think in primitives, but can instead express the test unshackled by constraints. As a first pass, I'd like the test to look like this: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">ClassData</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">PostValidReservationWhenDatabaseIsEmptyTestCases</span>))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;validDto,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SystemClock</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InMemoryRestaurantDatabase</span>(<span style="color:#2b91af;">Grandfather</span>.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(validDto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db.Grandfather); }</pre> </p> <p> This version of the test eliminates the noise. How <code>validDto</code> and <code>expected</code> are constructed is an implementation detail that has little bearing on the behaviour being tested. </p> <p> For a reader of the code, it's should now be clearer what's at stake here: If you <code>Post</code> a <code>validDto</code> the <code>expected</code> reservation should appear in the database. </p> <p> Reducing the test code to its essentials made me realise something that hitherto had escaped me: that I could <a href="/2020/11/30/name-by-role">name the DTO by role</a>. Instead of just <code>dto</code>, I could call the parameter <code>validDto</code>. </p> <p> Granted, I could also have done that previously, but I didn't think of it. There's was so much noise in that test that I didn't stop to consider whether <code>dto</code> sufficiently communicated the role of that variable. </p> <p> The less code, the easier it becomes to think such things through, I find. </p> <p> In any case, the test code now much more succinctly expresses the essence of the desired behaviour. Notice how I started my refactoring by writing the desired test code. I've yet to implement the data source. Now that the data source expresses test data as full objects, I'm not so concerned with whether or not that's going to be possible. Of course it's possible. </p> <h3 id="469ee6ae8f264e21a544f11be2476111"> Object data source <a href="#469ee6ae8f264e21a544f11be2476111" title="permalink">#</a> </h3> <p> You can define data sources for xUnit.net as classes or methods. In C# I usually reach for the <code>[ClassData]</code> option, since an object (in C#, that is) gives me better options for further decomposition. For example, I can define a class and delegate the details to helper methods: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PostValidReservationWhenDatabaseIsEmptyTestCases</span>&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TheoryData</span>&lt;<span style="color:#2b91af;">ReservationDto</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PostValidReservationWhenDatabaseIsEmptyTestCases</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddWithName(1049,&nbsp;19,&nbsp;00,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddWithName(1130,&nbsp;18,&nbsp;15,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddWithoutName(956,&nbsp;16,&nbsp;55,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddWithName(433,&nbsp;17,&nbsp;30,&nbsp;<span style="color:#a31515;">&quot;shli@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Shanghai&nbsp;Li&quot;</span>,&nbsp;5); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members&nbsp;here...</span></pre> </p> <p> Here, I'm taking advantage of xUnit.net's built-in <code>TheoryData&lt;T1, T2&gt;</code> base class, but that's just a convenience. All you have to do is to implement <code>IEnumerable&lt;object[]&gt;</code>. </p> <p> As you can see, the constructor adds the four test cases by calling two private helper methods. Here's the first of those: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;id&nbsp;=&nbsp;<span style="color:#a31515;">&quot;B50DF5B1-F484-4D99-88F9-1915087AF568&quot;</span>; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AddWithName( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;days, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;hours, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;minutes, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at.ToString(<span style="color:#a31515;">&quot;O&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Guid</span>.Parse(id), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Email</span>(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Name</span>(name), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity)); }</pre> </p> <p> The other helper method is almost identical, although it has a slight variation when it comes to the reservation name: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AddWithoutName( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;days, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;hours, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;minutes, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at.ToString(<span style="color:#a31515;">&quot;O&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Guid</span>.Parse(id), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Email</span>(email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Name</span>(<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity)); }</pre> </p> <p> In total, this refactoring results in <em>more</em> code, so how is this an improvement? </p> <h3 id="9722aade54194b1a87c7312d16ed7fc8"> The paradox of decomposition <a href="#9722aade54194b1a87c7312d16ed7fc8" title="permalink">#</a> </h3> <p> In object-oriented design, decomposition tends to lead to more code. If you want to isolate and make reusable a particular piece of behaviour, you'll usually introduce an interface or a base class. Even stateless functions need a static class to define them. (To be fair, functional programming isn't entirely devoid of such overhead associated with decomposition, but the cost tends to smaller.) This leads to more code, compared with the situation before decomposition. </p> <p> This is a situation you may also encounter if you attempt to refactor to design patterns, or follow the <a href="/encapsulation-and-solid">SOLID principles</a>. You'll have more code than when you started. This often leads to resistance to such 'code bloat'. </p> <p> It's fine to resist code bloat. It's also fine to dislike 'complexity for complexity's sake'. Try to evaluate each potential code change based on advantages and disadvantages. I'm not insisting that the above refactoring is objectively better. I did feel, however, that I had a problem that I ought to address, and that this was a viable alternative. The result is more code, but each piece of code is smaller and simpler. </p> <p> You can, conceivably, read the test method itself to get a feel for what it tests, even if you don't know all the implementation details. You can read the four statements in the <code>PostValidReservationWhenDatabaseIsEmptyTestCases</code> constructor without, I hope, understanding all the details about the two helper methods. And you <em>can</em> read <code>AddWithName</code> without understanding how <code>AddWithoutName</code> works, and vice versa, because these two methods don't depend on each other. </p> <h3 id="27b546f17ba44571a11ed44db9d45a09"> Conclusion <a href="#27b546f17ba44571a11ed44db9d45a09" title="permalink">#</a> </h3> <p> In this article, I've described how the use of code annotations for parametrised tests tend to pull in the direction of primitive obsession. This is a force worth keeping an eye on, I think. </p> <p> You saw how to refactor to class-based test data generation. This enables you to use objects instead of primitives, thus opening your design palette. You can now use all your usual object-oriented or functional design skills to factor the code in a way that's satisfactory. </p> <p> Was it worth it in this case? Keep in mind that the original problem was already marginal. While the code didn't fit in a 80x24 box, it was only 33 lines of code (excluding the test data). Imagine, however, that instead of a five-field reservation, you'd be dealing with a twenty-field data class, and such a refactoring begins to look more compelling. </p> <p> Is the code now perfect? It still isn't. I'm a little put off by the similarity of <code>AddWithName</code> and <code>AddWithoutName</code>. I'm also aware that there's a trace of production code duplicated in the test case, in the way that the test code duplicates how a valid <code>ReservationDto</code> relates to a <code>Reservation</code>. I'm on the fence whether I should do anything about this. </p> <p> At the moment I'm inclined to heed <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">the rule of three</a>. The duplication is still too insubstantial to warrant refactoring, but it's worth keeping an eye on. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c1994edb7ab641e4a9bbcff33a5934d8"> <div class="comment-author"><a href="https://github.com/cieciurm">Mateusz</a> <a href="#c1994edb7ab641e4a9bbcff33a5934d8">#</a></div> <div class="comment-content"> <p> Hi Mark, fair point! Indeed, sometimes <em>[InlineData]</em> is too simple and having test data creation moved to a separate class with all its advantages is a valid approach (using f.e. <em>[ClassData]</em>). </p> <p> I just wanted to clarify on <em>TheoryData</em> class, I noticed that it was added in xUnit v3. But I can't find this new version anywhere on NuGet - are you using it from some early adopter feed? Thanks! </p> </div> <div class="comment-date">2021-05-22 12:37 UTC</div> </div> <div class="comment" id="54dc98b2fa0141ff8a6ebb7b9eb0340a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#54dc98b2fa0141ff8a6ebb7b9eb0340a">#</a></div> <div class="comment-content"> <p> Mateusz, thank you for writing. The code shown in this article uses xUnit.net 2.4.1, which (apparently) includes the <code>TheoryData</code> base classes. I must admit that I don't know exactly when they were added to the library. </p> </div> <div class="comment-date">2021-05-23 18:39 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Waiting to happen https://blog.ploeh.dk/2021/01/11/waiting-to-happen 2021-01-11T06:31:00+00:00 Mark Seemann <div id="post"> <p> <em>A typical future test maintenance problem.</em> </p> <p> In <a href="/2020/12/07/branching-tests">a recent article</a> I showed a unit test and parenthetically mentioned that it might have a future maintenance problem. Here's a more recent version of the same test. Can you tell what the future issue might be? </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2023-11-24&nbsp;19:00&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2024-02-13&nbsp;18:15&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2023-08-23&nbsp;16:55&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2022-03-18&nbsp;17:30&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;shli@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Shanghai&nbsp;Li&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at,&nbsp;<span style="color:blue;">string</span>&nbsp;email,&nbsp;<span style="color:blue;">string</span>&nbsp;name,&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SystemClock</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InMemoryRestaurantDatabase</span>(<span style="color:#2b91af;">Grandfather</span>.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;<span style="color:#a31515;">&quot;B50DF5B1-F484-4D99-88F9-1915087AF568&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Guid</span>.Parse(dto.Id), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Parse(dto.At,&nbsp;<span style="color:#2b91af;">CultureInfo</span>.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Email</span>(dto.Email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Name</span>(dto.Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db.Grandfather); }</pre> </p> <p> To be honest, there's more than one problem with this test, but presently I'm going to focus on one of them. </p> <p> Since you don't know the details of the implementation, you may not be able to tell what the problem might be. It's not a trick question. On the other hand, you might still be able to guess, just from the clues available in the above code listing. </p> <p> <ins datetime="2021-07-10T11:01Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="970cada688cc4a04abbb1c11328931f1"> Sooner or later <a href="#970cada688cc4a04abbb1c11328931f1" title="permalink">#</a> </h3> <p> Here are some clues to consider: I'm writing this article in the beginning of 2021. Consider the dates supplied via the <code>[InlineData]</code> attributes. Seen from 2021, they're all in the future. </p> <p> Notice, as well, that the <code>sut</code> takes a <code>SystemClock</code> dependency. You don't know the <code>SystemClock</code> class (it's a proprietary class in this code base), but from the name I'm sure that you can imagine what it represents. </p> <p> From the perspective of early 2021, all dates are going to be in the future for more than a year. What is going to happen, though, once the test runs after March 18, 2022? </p> <p> That test case is going to fail. </p> <p> You can't tell from the above code listing, but the system under test rejects reservations in the past. Once March 18, 2022 has come and gone, the reservation at <code>"2022-03-18 17:30"</code> is going to be in the past. The <code>sut</code> will reject the reservation, and the assertion will fail. </p> <p> You have to be careful with tests that rely on the system clock. </p> <h3 id="61e0c1225dd34c40a0ceb19270ae9007"> Test Double? <a href="#61e0c1225dd34c40a0ceb19270ae9007" title="permalink">#</a> </h3> <p> The fundamental problem is that the system clock is non-deterministic. A typical reaction to non-determinism in unit testing is to introduce a <a href="https://martinfowler.com/bliki/TestDouble.html">Test Double</a> of some sort. Instead of using the system clock, you could use a <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Stub</a> as a stand-in for the real time. </p> <p> This is possible here as well. The <code>ReservationsController</code> class actually depends on an <code>IClock</code> interface that <code>SystemClock</code> implements. You could define a test-specific <code>ConstantClock</code> implementation that would always return a constant date and time. This would actually work, but would rely on an implementation detail. </p> <p> At the moment, the <code>ReservationsController</code> only calls <code>Clock.GetCurrentDateTime()</code> a <em>single time</em> to get the current time. As soon as it has that value, it passes it to a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, which implements <a href="/2020/01/27/the-maitre-d-kata">the business logic</a>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;now&nbsp;=&nbsp;Clock.GetCurrentDateTime(); <span style="color:blue;">if</span>&nbsp;(!restaurant.MaitreD.WillAccept(now,&nbsp;reservations,&nbsp;reservation)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;NoTables500InternalServerError();</pre> </p> <p> A <code>ConstantClock</code> would work, but only as long as the <code>ReservationsController</code> only calls <code>Clock.GetCurrentDateTime()</code> once. If it ever began to call this method multiple times to detect the passing of time, using a constant time value would mostly likely again break the test. This seems brittle, so I don't want to go that way. </p> <h3 id="29f9100aa1104cd2a1db18146384bc17"> Relative time <a href="#29f9100aa1104cd2a1db18146384bc17" title="permalink">#</a> </h3> <p> Working with the system clock in automated tests is easier if you deal with relative time. Instead of defining the test cases as absolute dates, express them as days into the future. Here's one way to refactor the test: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(1049,&nbsp;19,&nbsp;00,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [<span style="color:#2b91af;">InlineData</span>(1130,&nbsp;18,&nbsp;15,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;956,&nbsp;16,&nbsp;55,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;433,&nbsp;17,&nbsp;30,&nbsp;<span style="color:#a31515;">&quot;shli@example.org&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Shanghai&nbsp;Li&quot;</span>,&nbsp;5)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;days, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;hours, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;minutes, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now.Date&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>(days,&nbsp;hours,&nbsp;minutes,&nbsp;0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SystemClock</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InMemoryRestaurantDatabase</span>(<span style="color:#2b91af;">Grandfather</span>.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;<span style="color:#a31515;">&quot;B50DF5B1-F484-4D99-88F9-1915087AF568&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at.ToString(<span style="color:#a31515;">&quot;O&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Guid</span>.Parse(dto.Id), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Parse(dto.At,&nbsp;<span style="color:#2b91af;">CultureInfo</span>.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Email</span>(dto.Email), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Name</span>(dto.Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db.Grandfather); }</pre> </p> <p> The absolute dates always were fairly arbitrary, so I just took the current date and converted the dates to a number of days into the future. Now, the first test case will always be a date 1,049 days (not quite three years) into the future, instead of November 24, 2023. </p> <p> The test is no longer a failure waiting to happen. </p> <h3 id="906499c9b3d647d08d2f48eb5991cf43"> Conclusion <a href="#906499c9b3d647d08d2f48eb5991cf43" title="permalink">#</a> </h3> <p> Treating test cases that involve time and date as relative to the current time, instead of as absolute values, is usually a good idea if the system under test depends on the system clock. </p> <p> It's always a good idea to factor as much code as you can as pure functions, like the above <code>WillAccept</code> method. Pure functions don't depend on the system clock, so here you can safely pass absolute time and date values. Pure functions are <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. </p> <p> Still, as the <a href="https://martinfowler.com/bliki/TestPyramid.html">test pyramid</a> suggests, relying exclusively on unit tests isn't a good idea. The test shown in this article isn't really a unit test, but rather a <a href="/2019/04/01/an-example-of-state-based-testing-in-c">state-based integration test</a>. It relies on both the system clock and a <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> database. Expressing the test cases for this test as relative time values effectively addresses the problem introduced by the system clock. </p> <p> There are plenty of other problems with the above test. One thing that bothers me is that the 'fix' made the line count grow. It didn't quite fit into a <a href="/2019/11/04/the-80-24-rule">80x24 box</a> before, but now it's even worse! I should do something about that, but that's a topic for <a href="/2021/01/18/parametrised-test-primitive-obsession-code-smell">another article</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dynamic test oracles for rho problems https://blog.ploeh.dk/2021/01/04/dynamic-test-oracles-for-rho-problems 2021-01-04T06:26:00+00:00 Mark Seemann <div id="post"> <p> <em>A proof of concept of cross-branch testing for compiled languages.</em> </p> <p> <a href="https://www.hillelwayne.com">Hillel Wayne</a> recently published an article called <a href="https://buttondown.email/hillelwayne/archive/cross-branch-testing/">Cross-Branch Testing</a>. It outlines an approach to a class of problems that are hard to test. He mentions computer vision and simulations, among others. I can add that it's also <a href="/2015/10/19/visual-value-verification">difficult to write intuitive tests of convex hulls</a> and <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's game of life</a>. </p> <p> Hillel Wayne calls these <em>rho problems</em>, 'just because'. I'm totally going to run with that term. </p> <p> In the article, he outlines an approach where you test an iteration of rho code against a 'last known good' snapshot. He uses <code>git worktree</code> to set up a snapshot of the reference implementation. He then writes a property that compares the refactored code's behaviour against the reference. </p> <p> The example code is in <a href="https://www.python.org">Python</a>, which is a language that I don't know. As far as I can tell, it works because Python is 'lightweight' enough that you can load and execute source code directly. I found that the approach makes much sense, but I wondered how it would apply for statically typed, compiled languages. I decided to create a proof of concept in <a href="https://fsharp.org">F#</a>. </p> <h3 id="ab0c6139b2c84e148fe1173c2508ec62"> Test cases from Python <a href="#ab0c6139b2c84e148fe1173c2508ec62" title="permalink">#</a> </h3> <p> My first problem was to port Hillel Wayne's example rho problem to F#. The function <code>f</code> doesn't have any immediate mathematical properties; nor is its behaviour intuitive. While I think that I understand what each line of code in <code>f</code> means, I don't really know Python. Since one of the properties of rho problems is that bugs can be subtle, I didn't trust myself to be able to port the Python code to F# without some test cases. </p> <p> To solve that problem, I first found an online Python interpreter and pasted the <code>f</code> function into it. I then wrote code to print the output of a function call: </p> <p> <pre>print(f'1, 2, 3, { f(1, 2, 3) }')</pre> </p> <p> This line of code produces this output: </p> <p> <pre>1, 2, 3, True</pre> </p> <p> In other words, I could produce comma-separated values of input and actual output. </p> <p> Hillel Wayne wrote properties using <a href="https://hypothesis.works">Hypothesis</a>, which, <a href="https://hypothesis.works/articles/how-many-tests">it seems</a>, by default runs each property 200 times. </p> <p> In F# I'm going to use <a href="https://fscheck.github.io/FsCheck">FsCheck</a>, so I first used <em>F# Interactive</em> with FsCheck to produce 200 Python <code>print</code> statements like the above: </p> <p> <pre>&gt; Arb.Default.Int32().Generator |&gt; Gen.three |&gt; Gen.map (fun (x, y, z) -&gt; sprintf "print(f'%i, %i, %i, { f(%i, %i, %i) }')" x y z x y z) |&gt; Gen.sample 100 200 |&gt; List.iter (printfn "%s");; print(f'-77, 67, 84, { f(-77, 67, 84) }') print(f'58, -46, 3, { f(58, -46, 3) }') print(f'21, 13, 94, { f(21, 13, 94) }') ... </pre> </p> <p> This is a throwaway data pipeline that starts with an FsCheck integer generator, creates a triple from it, turns that triple into a Python <code>print</code> statement, and finally writes 200 of those to the console. The above code listing only shows the first three lines of output, while the rest are indicated by an ellipsis. </p> <p> I copied those 200 <code>print</code> statements over to the online Python interpreter and ran the code. That produced 200 comma-separated values like these: </p> <p> <pre>-77, 67, 84, False 58, -46, 3, False 21, 13, 94, True ...</pre> </p> <p> These can serve as test cases for porting the Python code to F#. </p> <h3 id="936410215a784d73ae9b5dcba9125a4c"> Port to F# <a href="#936410215a784d73ae9b5dcba9125a4c" title="permalink">#</a> </h3> <p> The next step is to write a parametrised test, using a provisional implementation of <code>f</code>: </p> <p> <pre>[&lt;Theory;&nbsp;MemberData(nameof&nbsp;fTestCases)&gt;] <span style="color:blue;">let</span>&nbsp;``test&nbsp;f``&nbsp;x&nbsp;y&nbsp;z&nbsp;expected&nbsp;=&nbsp;expected&nbsp;=!&nbsp;f&nbsp;x&nbsp;y&nbsp;z</pre> </p> <p> This test uses <a href="https://xunit.net">xUnit.net</a> 2.4.1 and <a href="https://github.com/SwensenSoftware/Unquote">Unquote</a> 5.0.0. As you can tell, apart from the annotations, it's a true one-liner. It calls the <code>f</code> function with the three supplied arguments <code>x</code>, <code>y</code>, and <code>z</code> and compares the return value with the <code>expected</code> value. </p> <p> The code uses the new <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/nameof">nameof</a> feature of F# 5. <code>fTestCases</code> is a function in the same module that holds the test: </p> <p> <pre><span style="color:green;">//&nbsp;unit&nbsp;-&gt;&nbsp;seq&lt;obj&nbsp;[]&gt;</span> <span style="color:blue;">let</span>&nbsp;fTestCases&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;strm&nbsp;=&nbsp;typeof&lt;Anchor&gt;.Assembly.GetManifestResourceStream&nbsp;streamName &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;rdr&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;StreamReader&nbsp;(strm) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;s&nbsp;=&nbsp;rdr.ReadToEnd&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;s.Split&nbsp;Environment.NewLine&nbsp;|&gt;&nbsp;Seq.map&nbsp;csvToTestCase</pre> </p> <p> It reads an embedded resource stream of test cases, like the above comma-separated values. Even though the values are in a text file, it's easier to embed the file in the test assembly, because it nicely dispenses with the problem of copying a text file to the appropriate output directory when the code compiles. That would, however, be an valid alternative. </p> <p> <code>Anchor</code> is a dummy type to support <code>typeof</code>, and <code>streamName</code> is just a string constant that identifies the name of the stream. </p> <p> The <code>csvToTestCase</code> function converts each line of comma-separated values to test cases for the <code>[&lt;Theory&gt;]</code> attribute: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;obj&nbsp;[]</span> <span style="color:blue;">let</span>&nbsp;csvToTestCase&nbsp;(csv&nbsp;:&nbsp;string)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;values&nbsp;=&nbsp;csv.Split&nbsp;<span style="color:#a31515;">&#39;,&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;[| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values.[0]&nbsp;|&gt;&nbsp;Convert.ToInt32&nbsp;|&gt;&nbsp;box &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values.[1]&nbsp;|&gt;&nbsp;Convert.ToInt32&nbsp;|&gt;&nbsp;box &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values.[2]&nbsp;|&gt;&nbsp;Convert.ToInt32&nbsp;|&gt;&nbsp;box &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values.[3]&nbsp;|&gt;&nbsp;Convert.ToBoolean&nbsp;|&gt;&nbsp;box &nbsp;&nbsp;&nbsp;&nbsp;|]</pre> </p> <p> It's not the safest code I could write, but this is, after all, a proof of concept. </p> <p> The most direct port of the Python code I could produce is this: </p> <p> <pre><span style="color:green;">//&nbsp;f&nbsp;:&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;f&nbsp;(x&nbsp;:&nbsp;int)&nbsp;(y&nbsp;:&nbsp;int)&nbsp;(z&nbsp;:&nbsp;int)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;mx&nbsp;=&nbsp;bigint&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;my&nbsp;=&nbsp;bigint&nbsp;y &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;mz&nbsp;=&nbsp;bigint&nbsp;z &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;out&nbsp;=&nbsp;0I &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;[0I..9I]&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;out&nbsp;*&nbsp;mx&nbsp;+&nbsp;abs&nbsp;(my&nbsp;*&nbsp;mz&nbsp;-&nbsp;i&nbsp;*&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;x&#39;&nbsp;=&nbsp;mx &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;y&#39;&nbsp;=&nbsp;my &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;z&#39;&nbsp;=&nbsp;mz &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mx&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;y&#39;&nbsp;+&nbsp;1I &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;z&#39; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mz&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;x&#39; &nbsp;&nbsp;&nbsp;&nbsp;abs&nbsp;out&nbsp;%&nbsp;100I&nbsp;&lt;&nbsp;10I</pre> </p> <p> As F# code goes, it's disagreeable, but it passes all 200 test cases, so this will serve as an initial implementation. The <code>out</code> variable can grow to values that overflow even 64-bit integers, so I had to convert to <a href="https://docs.microsoft.com/dotnet/api/system.numerics.biginteger">bigint</a> to get all test cases to pass. </p> <p> If I make the same mutation to the code that Hillel Wayne did (<code>abs&nbsp;out&nbsp;%&nbsp;100I&nbsp;&lt;&nbsp;9I</code>) two test cases fail. This gives me some confidence that I have a degree of problem coverage comparable to his. </p> <h3 id="289e30b4098c4e54b8390af0b672caf1"> Test oracle <a href="#289e30b4098c4e54b8390af0b672caf1" title="permalink">#</a> </h3> <p> Now that a reference implementation exists, we can use it as a <a href="https://en.wikipedia.org/wiki/Test_oracle">test oracle</a> for refactorings. You can, for example, add a little test-only utility to your program portfolio: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;Prod <span style="color:blue;">open</span>&nbsp;FsCheck [&lt;EntryPoint&gt;] <span style="color:blue;">let</span>&nbsp;main&nbsp;argv&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;Arb.Default.Int32().Generator &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Gen.three &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Gen.sample&nbsp;100&nbsp;200 &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.iter&nbsp;(<span style="color:blue;">fun</span>&nbsp;(x,&nbsp;y,&nbsp;z)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;printfn&nbsp;<span style="color:#a31515;">&quot;%i,&nbsp;%i,&nbsp;%i,&nbsp;%b&quot;</span>&nbsp;x&nbsp;y&nbsp;z&nbsp;(f&nbsp;x&nbsp;y&nbsp;z)) &nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;<span style="color:green;">//&nbsp;return&nbsp;an&nbsp;integer&nbsp;exit&nbsp;code</span></pre> </p> <p> Notice that the last step in the pipeline is to output the values of each <code>x</code>, <code>y</code>, and <code>z</code>, as well as the result of calling <code>f x y z</code>. </p> <p> This is a command-line executable that uses FsCheck to produce new test cases by calling the <code>f</code> function. It looks similar to the above one-off script that produced Python code, but this one instead just produces comma-separated values. You can run it from the command line to produce a new sample of test cases: </p> <p> <pre>$ ./foracle 29, -48, -78, false -8, -25, 13, false -74, 34, -68, true ...</pre> </p> <p> As above, I've used an ellipsis to indicate that in reality, 200 lines of comma-separated values scroll by. </p> <p> When you use Bash, you can even pipe the output straight to a file: </p> <p> <pre>$ ./foracle > csv.txt</pre> </p> <p> You can now take the new comma-separated values and update the test values that the above <code>test f</code> test uses. </p> <p> In other words, you use version <em>n</em> of <code>f</code> as a test oracle for version <em>n + 1</em>. When iteration <em>n + 1</em> is a function of iteration <em>n</em>, you have a so-called <em>dynamic system</em>, so I think that we can call this technique <em>dynamic test oracles</em>. </p> <p> The above <code>foracle</code> program is just a proof of concept. You could make it more flexible by making it take command-line arguments that would let you control the sample size and FsCheck's <code>size</code> parameter (the hard-coded <code>100</code> in the above code listing). </p> <h3 id="b7cee6f3ed874b3390276dd3852850a6"> Refactoring <a href="#b7cee6f3ed874b3390276dd3852850a6" title="permalink">#</a> </h3> <p> With the confidence instilled by the test cases, we can now refactor the <code>f</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;f&nbsp;:&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;f&nbsp;(x&nbsp;:&nbsp;int)&nbsp;(y&nbsp;:&nbsp;int)&nbsp;(z&nbsp;:&nbsp;int)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;imp&nbsp;(x,&nbsp;y,&nbsp;z,&nbsp;out)&nbsp;i&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;out&nbsp;=&nbsp;out&nbsp;*&nbsp;x&nbsp;+&nbsp;abs&nbsp;(y&nbsp;*&nbsp;z&nbsp;-&nbsp;i&nbsp;*&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;+&nbsp;1I,&nbsp;z,&nbsp;x,&nbsp;out &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(_,&nbsp;_,&nbsp;_,&nbsp;out)&nbsp;=&nbsp;List.fold&nbsp;imp&nbsp;(bigint&nbsp;x,&nbsp;bigint&nbsp;y,&nbsp;bigint&nbsp;z,&nbsp;0I)&nbsp;[0I..9I] &nbsp;&nbsp;&nbsp;&nbsp;abs&nbsp;out&nbsp;%&nbsp;100I&nbsp;&lt;&nbsp;10I</pre> </p> <p> Instead of all those mutable variables, the function is, after all, just a left fold. Phew, I feel better now. </p> <h3 id="f849e03594b448299ba4eef5a6a72b4e"> Conclusion <a href="#f849e03594b448299ba4eef5a6a72b4e" title="permalink">#</a> </h3> <p> This article demonstrated a proof of concept where you use a known good version (<em>n</em>) of the code as a test oracle for the next version (<em>n + 1</em>). In interpreted languages, you may be able to load two versions of the code base side by side, but that's rarely practical in a statically typed compiled language like F#. Instead, I used a utility program to generate test cases that can be used as a data source for a parametrised test. </p> <p> The example rho problem takes only integers as input, and returns a simple Boolean value, so in this case it's trivial to encode each test case as a line of comma-separated values. For (real) problems that may involve more complex types, it'd be better to use another serialisation format, such as JSON or XML. </p> <p> An outstanding issue is whether it's possible to implement shrinking behaviour when tests fail. Currently, the proof of concept just uses a set of serialised test cases. Normally, when a <a href="/property-based-testing-intro">property-based testing</a> framework like FsCheck detects a counter-example, it'll shrink the counter-example to values that are easier to understand than the original. This proof of concept doesn't do that. I'm not sure if a framework like FsCheck currently contains enough extensibility points to enable that sort of behaviour. I'll leave that question open for any reader interested in taking on that problem. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="bb31c6eea41f4adcacf249d39a3798d2"> <div class="comment-author"><a href="https://github.com/dharmaturtle">Alex Nguyen</a> <a href="#bb31c6eea41f4adcacf249d39a3798d2">#</a></div> <div class="comment-content"> <p> Hi Mark! Thanks for another thought provoking post. </p> <p> I believe you and Hillel are writing <a href="https://en.wikipedia.org/wiki/Characterization_test">characterization tests</a>, which <a href="https://blog.ploeh.dk/2013/04/02/why-trust-tests/">you've mentioned in the past</a>. Namely, you're both using the behavior of existing code to verify the correctness of a refactor. The novel part to me is that Hillel is using code as the test oracle. Your solution serializes the oracle to a static file. The library I use for characterization tests (<a href="https://www.nuget.org/packages/ApprovalTests">ApprovalTests</a>) does this as well. </p> <p> I believe shrinking is impossible when the oracle is a static file. However with Hillel's solution the oracle may be consulted at any time, making shrinking viable. If only there was a practical way to combine the two... </p> </div> <div class="comment-date">2021-01-06 23:01 UTC</div> </div> <div class="comment" id="4aa4188124ca4f4fadf35d6881b9452e"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#4aa4188124ca4f4fadf35d6881b9452e">#</a></div> <div class="comment-content"> <p> A thought provoking post indeed! </p> <blockquote> In F# I'm going to use <a href="https://fscheck.github.io/FsCheck">FsCheck</a>... </blockquote> <p> I think that is a fine choice given the use case laid out in this post. In general though, I think <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> is a better property-based testing library. Its killer feature is integrated shrinking, which means that all generators can also shrink and this extra power is essentially free. </p> <p> For the record (because this can be a point of confusion), Haskell has <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a> and <a href="https://hackage.haskell.org/package/hedgehog">(Haskell) Hedgehog</a> while F# has ports from Haskell called <a href="https://fscheck.github.io/FsCheck">FsCheck</a> and <a href="https://github.com/hedgehogqa/fsharp-hedgehog">(FSharp) Hedgehog</a>. </p> <p> <a href="https://twitter.com/jacobstanley">Jacob Stanley</a> gave <a href="https://www.youtube.com/watch?v=AIv_9T0xKEo">this excellent talk at YOW! Lambda Jam 2017</a> that explains the key idea that allows Hedgehog to have integrated shrinking. (Spoiler: A generic type that is invariant in its only type parameter is replaced by a different generic type that is monadic in its only type parameter. API design guided by functional programming for the win!) </p> <blockquote> Normally, when a property-based testing framework like FsCheck detects a counter-example, it'll shrink the counter-example to values that are easier to understand than the original. </blockquote> <p> In my experience, property-based tests written with QuickCheck / FsCheck do not normally shrink. I think this is because of the extra work required to enable shrinking. For an anecdotal example, consider <a href="https://frasertweedale.github.io/blog-fp/posts/2020-03-31-quickcheck-hedgehog.html">this post by Fraser Tweedale</a>. He believed that it would be faster to add (Haskell) Hedgehog as a dependency and create a generator for it than to add shrinking to his existing generator in QuickCheck. </p> <blockquote> In other words, you use version <em>n</em> of <code>f</code> as a test oracle for version <em>n + 1</em>. When iteration <em>n + 1</em> is a function of iteration <em>n</em>, you have a so-called <em>dynamic system</em>, so I think that we can call this technique <em>dynamic test oracles</em>. </blockquote> <p> I am confused by this paragraph. I interpret your word "When" at the start of the second sentence as a common-language synonym for the mathematical word "If". Here is roughly how I understand that paragraph, where <code>A</code> stands for "version / iteration <em>n</em> of <code>f</code>" and <code>B</code> stands for "version / iteration <em>n + 1</em> of <code>f</code>". "<code>A</code> depends on <code>B</code>. If <code>B</code> depends on <code>A</code>, then we have a dynamic system. Therefore, we have a dynamic system." I feel like the paragraph assumes (because it is obvious?) that version / iteration <em>n + 1</em> of <code>f</code> depends on version / iteration <em>n</em> of <code>f</code>. In what sense is that the case? </p> <blockquote> An outstanding issue is whether it's possible to implement shrinking behaviour when tests fail. [...] I'll leave that question open for any reader interested in taking on that problem. </blockquote> <p> I am interested! </p> <p> Quoting Mark and then Alex. </p> <blockquote> <p> Hillel Wayne [...] outlines an approach where you test an iteration of rho code against a 'last known good' snapshot. He uses <code>git worktree</code> to set up a snapshot of the reference implementation. He then writes a property that compares the refactored code's behaviour against the reference. </p> <p> The example code is in <a href="https://www.python.org">Python</a>, which is a language that I don't know. As far as I can tell, it works because Python is 'lightweight' enough that you can load and execute source code directly. I found that the approach makes much sense, but I wondered how it would apply for statically typed, compiled languages. I decided to create a proof of concept in <a href="https://fsharp.org">F#</a>. </p> </blockquote> <blockquote> I believe shrinking is impossible when the oracle is a static file. However with Hillel's solution the oracle may be consulted at any time, making shrinking viable. </blockquote> <p> I want to start by elaborating on this to make sure we are all on the same page. I think of shrinking as involving two parts. On the one hand, we have the "shrink tree", which contains the values to test during the shrinking process. On the other hand, for each input tested, we need to know if the output should cause the test to pass or fail. </p> <p> With Hedgehog, getting a shrink tree would not be too difficult. For a generator with type parameter <code>'a</code>, the current generator API returns a "random" shrink tree of type <code>'a</code> in which the root is an instance <code>a</code> of the type <code>'a</code> and the tree completely depends on <code>a</code>. It should be easy to expose an additional function that accepts inputs of type <code>'a Gen</code> and <code>'a</code> and returns <em>the</em> tree with the given <code>'a</code> as its root. </p> <p> The difficult part is being able to query the test oracle. As Mark said, this seems easy to do in a dynamically-typed language like Python. In contrast, the fundamental issue with a statically-typed language like F# is that the compiled code exists in an assembly and only one assembly of a given name can be loaded in a given process at the same time. </p> <p> This leads me to two ideas for workarounds. First, we could query the test oracle in a different process. I imagine an entry point could be generated that gives direct access to the test oracle. Then the test process could query the test oracle by executing this generated process. Second, we could generate a different assembly that exposes the test oracle. Then the test process could load this generated assembly to query the test oracle. The second approach seems like it would have a faster query time but be harder to implement. The first approach seems easier to implement but would probably have a slower query time. Maybe the query time would be fast enough though, especially if it was only queried when shrinking. </p> <p> But given such a solution, who wants to restrict access to the test oracle only to shrinking? If the test oracle is always available, then there is no need to store input-output pairs. Instead of always checking that the system under test works correctly for a previously selected set of inputs, the property-based test can check that the system under test has the expected behavior for a unique set of inputs each time the property-based test is executed. In my experience, this is the default behavior of a property-based test. </p> <p> One concern that some people might have is the idea of checking into the code repository the binary containing the test oracle. My first though is that <a href="https://blog.ploeh.dk/2014/01/29/nuget-package-restore-considered-harmful/">the size of this is likely so small that it does not matter</a>. My second thought is that the binary containing the test oracle does not have to be included in the repository. Instead, the workflow could be to (1) create the property-based test that uses the compiled test oracle, (2) refactor the system under test, (3) observe that the property-based test still passes, (4) commit the refactored code, and (5) discard the remaining changes, which will delete the property-based test and the compiled test oracle. </p> <p> Instead of completely removing that property-based test, it might be better to leave it there with input-output pairs stored in a file. Then the conversion from that state of the property-based test to the one that uses the compiled test oracle will be much smaller. </p> </div> <div class="comment-date">2021-01-07 19:27 UTC</div> </div> <div class="comment" id="39b559f49ecc426db7b3d9d1518556df"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#39b559f49ecc426db7b3d9d1518556df">#</a></div> <div class="comment-content"> <p> Alex, thank you for writing. Yes, I think that calling this a Characterisation Test is correct. I wasn't aware of the <em>ApprovalTests</em> library; thank you for mentioning it. </p> <p> When I originally wrote the article, I was under the impression that shrinking might still be possible. I admit, though, that I hadn't thought things through. I think that <a href="#4aa4188124ca4f4fadf35d6881b9452e">Tyson Williams argues convincingly</a> that this isn't possible. </p> </div> <div class="comment-date">2021-01-15 13:42 UTC</div> </div> <div class="comment" id="a6ea5d7cbb75431ba6adf969599c2bd3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a6ea5d7cbb75431ba6adf969599c2bd3">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I'm <a href="/2017/09/18/the-test-data-generator-functor#5bd990290ff048c2a7b55b740053831d">well aware of Hedgehog</a>, and I'm keen on the way it works. I rarely use it, however, as it so far doesn't quite seem to have the same degree of 'industrial strength' to it that FsCheck has. Additionally, I find that shrinking is less important in practice than it might seem in theory. </p> <p> I'm not sure that I understand your confusion about the term <em>dynamic</em>. You write: <blockquote> <p> "<code>A</code> depends on <code>B</code>." </p> </blockquote> Why do you write that? I don't think, in the way you've labelled iterations, that <code>A</code> depends on <code>B</code>. </p> <p> When it comes to shrinking, I think that you convincingly argues that it can't be done unless one is able to query the oracle. As long as all you have is a list of test cases, you can't do that... unless, perhaps, you were to also generate and run all the shrunk test cases when you capture the list of test cases... Again, I haven't thought this through, so there may be some obvious gotcha that I'm missing. </p> <p> I would be wary of trying to host the previous iteration in a different process. This is technically possible, but, in .NET at least, quite cumbersome. You'll have to deal with data marshalling and lifetime management of the second process. It was difficult enough in .NET framework back when <em>remoting</em> was a thing; I'm not even sure how one would go about such a problem in .NET Core - particularly if you want it to work on both Windows, Linux, and Mac. HTTP? </p> </div> <div class="comment-date">2021-01-16 13:24 UTC</div> </div> <div class="comment" id="7e4b9abea062491bb5a4040761902b38"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#7e4b9abea062491bb5a4040761902b38">#</a></div> <div class="comment-content"> <blockquote> [Hedgehog] so far doesn't quite seem to have the same degree of 'industrial strength' to it that FsCheck has. </blockquote> <p> That seems reasonable. I haven't used FsCheck, so I wouldn't know myself. Several of us are making many great improvements to F# Hedgehog right now. </p> <blockquote> <p> When it comes to shrinking, I think that you convincingly argues that it can't be done unless one is able to query the oracle. As long as all you have is a list of test cases, you can't do that... unless, perhaps, you were to also generate and run all the shrunk test cases when you capture the list of test cases... Again, I haven't thought this through, so there may be some obvious gotcha that I'm missing. </p> </blockquote> <p> That would be too many test cases. The shrinking process finds two values <code>n</code> and <code>n+1</code> such that the test passes for <code>n</code> and fails for <code>n+1</code>. This can be viewed as a constraint. The objective is to minimize the value of <code>n</code>. The only way to ensure that some value is the minimum is to test all values smaller than it. However, doing so is not practical. Property-based tests uses randomness precisely because it is not practical to test every possible value. </p> <p> Instead, the shrinking process uses binary search as a heuristic to find a value satisfying the constraint that is rather small but not necessarily the smallest. </p> <blockquote> Why do you write that? I don't think, in the way you've labelled iterations, that <code>A</code> depends on <code>B</code>. </blockquote> <p> Ok. I will go slower and ask smaller questions. </p> <blockquote> When iteration <em>n + 1</em> is a function of iteration <em>n</em> [...] </blockquote> <p> Does this phrase have the same meaning if "When" is replaced by "If"? </p> <blockquote> In other words, you use version <em>n</em> of <code>f</code> as a test oracle for version <em>n + 1</em>. When iteration <em>n + 1</em> is a function of iteration <em>n</em>, you have a so-called <em>dynamic system</em>, so I think that we can call this technique <em>dynamic test oracles</em>. </blockquote> <p> I understand how version <em>n</em> of <code>f</code> is being used as a test oracle for version <em>n + 1</em>. In this blog post, in what sense is something of iteration <em>n + 1</em> is a function of iteration <em>n</em>? </p> </div> <div class="comment-date">2021-01-30 16:36 UTC</div> </div> <div class="comment" id="078ef886b5ba4b818cf8909e9c25e83e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#078ef886b5ba4b818cf8909e9c25e83e">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. <blockquote> "Does this phrase have the same meaning if "When" is replaced by "If"?" </blockquote> I'm not sure that there's a big difference, but then, I don't know how you parse that. As Kevlin Henney puts it, <blockquote> <p> "The act of describing a program in unambiguous detail and the act of programming are one and the same." </p> <footer><cite><a href="https://twitter.com/KevlinHenney/status/3361631527">Kevlin Henney</a></cite></footer> </blockquote> It seems to me that you're attempting to parse my prose as though it was an unambiguous description, which it can't be. </p> <p> A dynamic system is a system such that <code>x<sub>t+1</sub> = f(x<sub>t</sub>)</code>, where <code>x<sub>t</sub></code> is the value of <code>x</code> at time <code>t</code>, and <code>x<sub>t+1</sub></code> is the value of <code>x</code> at time <code>t+1</code>. For simplicity, this is the definition of a dynamic system in discrete time. Mathematically, you can also express it in continuous time using calculus. </p> </div> <div class="comment-date">2021-02-02 6:46 UTC</div> </div> <div class="comment" id="3fe354d5505b4dd2aa9c2c0e80cf9002"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#3fe354d5505b4dd2aa9c2c0e80cf9002">#</a></div> <div class="comment-content"> <blockquote> It seems to me that you're attempting to parse my prose as though it was an unambiguous description, which it can't be. </blockquote> <p> Oh, yes. My mistake. I meant to phrase in slightly differently thereby changing it from essentially an impossible question to one that only you can answer. Here is what I meant to ask. </p> <blockquote> Does this phrase have the same meaning <em>to you</em> if "When" is replaced by "If"? </blockquote> <p> No matter though. I simply misunderstood your description / defintion of a dynamical system. I understand now. Thanks for your patience and willingness to explain it to me again. </p> </div> <div class="comment-date">2021-03-25 03:47 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An F# demo of validation with partial data round trip https://blog.ploeh.dk/2020/12/28/an-f-demo-of-validation-with-partial-data-round-trip 2020-12-28T09:22:00+00:00 Mark Seemann <div id="post"> <p> <em>An F# port of the previous Haskell proof of concept.</em> </p> <p> This article is part of <a href="/2020/12/14/validation-a-solved-problem">a short article series</a> on <a href="/2018/11/05/applicative-validation">applicative validation</a> with a twist. The twist is that validation, when it fails, should return not only a list of error messages; it should also retain that part of the input that <em>was</em> valid. </p> <p> In the <a href="/2020/12/21/a-haskell-proof-of-concept-of-validation-with-partial-data-round-trip">previous article</a> you saw a <a href="https://www.haskell.org">Haskell</a> proof of concept that demonstrated how to compose the appropriate <a href="/2018/10/01/applicative-functors">applicative functor</a> with a suitable <a href="/2017/11/27/semigroups">semigroup</a> to make validation work as desired. In this article, you'll see how to port that proof of concept to <a href="https://fsharp.org">F#</a>. </p> <h3 id="b2ea11fdb65343b5b60fcf2cffc62b1a"> Data definitions <a href="#b2ea11fdb65343b5b60fcf2cffc62b1a" title="permalink">#</a> </h3> <p> Like in the previous article, we're going to need some types. These are essentially direct translations of the corresponding Haskell types: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Input&nbsp;=&nbsp;{&nbsp;Name&nbsp;:&nbsp;string&nbsp;option;&nbsp;DoB&nbsp;:&nbsp;DateTime&nbsp;option;&nbsp;Address&nbsp;:&nbsp;string&nbsp;option} <span style="color:blue;">type</span>&nbsp;ValidInput&nbsp;=&nbsp;{&nbsp;Name&nbsp;:&nbsp;string;&nbsp;DoB&nbsp;:&nbsp;DateTime;&nbsp;Address&nbsp;:&nbsp;string&nbsp;}</pre> </p> <p> The <code>Input</code> type plays the role of the input we'd like to validate, while <code>ValidInput</code> presents validated data. </p> <p> If you're an F# fan, you can bask in the reality that F# records are terser than Haskell records. I like both languages, so I have mixed feelings about this. </p> <h3 id="2e1b689c57114b259b3b4019f4c3976d"> Computation expression <a href="#2e1b689c57114b259b3b4019f4c3976d" title="permalink">#</a> </h3> <p> Haskell's main workhorse is its type class system. F# doesn't have that, but it has <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a>, which in F# 5 got support for applicative functors. That's just what we need, and it turns out that there isn't a lot of code we have to write to make all of this work. </p> <p> To recap from the Haskell proof of concept: We need a <code>Result</code>-like <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> that returns a tuple for errors. One element of the tuple should be a an <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a>, which <a href="/2017/11/13/endomorphism-monoid">forms a monoid</a> (and therefore also a semigroup). The other element should be a list of error messages - <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">another monoid</a>. In F# terms we'll write it as <code>(('b -&gt; 'b) * 'c list)</code>. </p> <p> That's a tuple, and since <a href="/2017/10/30/tuple-monoids">tuples form monoids when their elements do</a> the <code>Error</code> part of <code>Result</code> <a href="/2017/11/20/monoids-accumulate">supports accumulation</a>. </p> <p> To support an applicative computation expression, we're going to need a a way to merge two results together. This is by far the most complicated piece of code in this article, all six lines of code: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Result&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Result&lt;&#39;a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,((&#39;b&nbsp;-&gt;&nbsp;&#39;b)&nbsp;*&nbsp;&#39;c&nbsp;list)&gt;&nbsp;-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Result&lt;&#39;d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,((&#39;b&nbsp;-&gt;&nbsp;&#39;b)&nbsp;*&nbsp;&#39;c&nbsp;list)&gt;&nbsp;-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Result&lt;(&#39;a&nbsp;*&nbsp;&#39;d),((&#39;b&nbsp;-&gt;&nbsp;&#39;b)&nbsp;*&nbsp;&#39;c&nbsp;list)&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;merge&nbsp;x&nbsp;y&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;x,&nbsp;y&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Ok&nbsp;xres,&nbsp;Ok&nbsp;yres&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;(xres,&nbsp;yres) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Error&nbsp;(f,&nbsp;e1s),&nbsp;Error&nbsp;(g,&nbsp;e2s)&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;(f&nbsp;&gt;&gt;&nbsp;g,&nbsp;e2s&nbsp;@&nbsp;e1s) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Error&nbsp;e,&nbsp;Ok&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;e &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Ok&nbsp;_,&nbsp;Error&nbsp;e&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;e</pre> </p> <p> The <code>merge</code> function composes two input results together. The results have <code>Ok</code> types called <code>'a</code> and <code>'d</code>, and if they're both <code>Ok</code> values, the return value is an <code>Ok</code> tuple of <code>'a</code> and <code>'d</code>. </p> <p> If one of the results is an <code>Error</code> value, it beats an <code>Ok</code> value. The only moderately complex operations is when both are <code>Error</code> values. </p> <p> Keep in mind that an <code>Error</code> value in this instance contains a tuple of the type <code>(('b -&gt; 'b) * 'c list)</code>. The first element is an endomorphism <code>'b -&gt; 'b</code> and the other element is a list. The <code>merge</code> function composes the endomorphism <code>f</code> and <code>g</code> by standard function composition (the <code>&gt;&gt;</code> operator), and concatenates the lists with the standard <code>@</code> list concatenation operator. </p> <p> Because I'm emulating how the <a href="https://forums.fsharp.org/t/thoughts-on-input-validation-pattern-from-a-noob/1541">original forum post</a>'s code behaves, I'm concatenating the two lists with the rightmost going before the leftmost. It doesn't make any other difference than determining the order of the error list. </p> <p> With the <code>merge</code> function in place, the computation expression is a simple matter: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ValidationBuilder&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;_.BindReturn&nbsp;(x,&nbsp;f)&nbsp;=&nbsp;Result.map&nbsp;f&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;_.MergeSources&nbsp;(x,&nbsp;y)&nbsp;=&nbsp;Result.merge&nbsp;x&nbsp;y</pre> </p> <p> The last piece is a <code>ValidationBuilder</code> value: </p> <p> <pre>[&lt;AutoOpen&gt;] <span style="color:blue;">module</span>&nbsp;ComputationExpressions&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;validation&nbsp;=&nbsp;ValidationBuilder&nbsp;()</pre> </p> <p> Now, whenever you use the <code>validation</code> computation expression, you get the desired functionality. </p> <h3 id="948b93fa6a1d47beac150180769731eb"> Validators <a href="#948b93fa6a1d47beac150180769731eb" title="permalink">#</a> </h3> <p> Before we can compose some validation functions, we'll need to have some validators in place. These are straightforward translations of the Haskell validation functions, starting with the name validator: </p> <p> <pre><span style="color:green;">//&nbsp;Input&nbsp;-&gt;&nbsp;Result&lt;string,((Input&nbsp;-&gt;&nbsp;Input)&nbsp;*&nbsp;string&nbsp;list)&gt;</span> <span style="color:blue;">let</span>&nbsp;validateName&nbsp;({&nbsp;Name&nbsp;=&nbsp;name&nbsp;}&nbsp;:&nbsp;Input)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;name&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;n&nbsp;<span style="color:blue;">when</span>&nbsp;n.Length&nbsp;&gt;&nbsp;3&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Error&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">fun</span>&nbsp;(args&nbsp;:&nbsp;Input)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;args&nbsp;<span style="color:blue;">with</span>&nbsp;Name&nbsp;=&nbsp;None&nbsp;}), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&quot;no&nbsp;bob&nbsp;and&nbsp;toms&nbsp;allowed&quot;</span>]) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;(id,&nbsp;[<span style="color:#a31515;">&quot;name&nbsp;is&nbsp;required&quot;</span>])</pre> </p> <p> When the name is too short, the endomorphism resets the <code>Name</code> field to <code>None</code>. </p> <p> The date-of-birth validation function works the same way: </p> <p> <pre><span style="color:green;">//&nbsp;DateTime&nbsp;-&gt;&nbsp;Input&nbsp;-&gt;&nbsp;Result&lt;DateTime,((Input&nbsp;-&gt;&nbsp;Input)&nbsp;*&nbsp;string&nbsp;list)&gt;</span> <span style="color:blue;">let</span>&nbsp;validateDoB&nbsp;(now&nbsp;:&nbsp;DateTime)&nbsp;({&nbsp;DoB&nbsp;=&nbsp;dob&nbsp;}&nbsp;:&nbsp;Input)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;dob&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;d&nbsp;<span style="color:blue;">when</span>&nbsp;d&nbsp;&gt;&nbsp;now.AddYears&nbsp;-12&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Error&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">fun</span>&nbsp;(args&nbsp;:&nbsp;Input)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;args&nbsp;<span style="color:blue;">with</span>&nbsp;DoB&nbsp;=&nbsp;None&nbsp;}), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&quot;get&nbsp;off&nbsp;my&nbsp;lawn&quot;</span>]) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;(id,&nbsp;[<span style="color:#a31515;">&quot;dob&nbsp;is&nbsp;required&quot;</span>])</pre> </p> <p> Again, like in the Haskell proof of concept, instead of calling <code>DateTime.Now</code> from within the function, I'm passing <code>now</code> as an argument to keep the function <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>. </p> <p> The address validation concludes the set of validators: </p> <p> <pre><span style="color:green;">//&nbsp;Input&nbsp;-&gt;&nbsp;Result&lt;string,((&#39;a&nbsp;-&gt;&nbsp;&#39;a)&nbsp;*&nbsp;string&nbsp;list)&gt;</span> <span style="color:blue;">let</span>&nbsp;validateAddress&nbsp;({&nbsp;Address&nbsp;=&nbsp;address&nbsp;}:&nbsp;Input)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;address&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;a &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;(id,&nbsp;[<span style="color:#a31515;">&quot;add1&nbsp;is&nbsp;required&quot;</span>])</pre> </p> <p> The inferred endomorphism type here is the more general <code>'a -&gt; 'a</code>, but it's compatible with <code>Input -&gt; Input</code>. </p> <h3 id="836690a83da24da492f70c8eb756a52d"> Composition <a href="#836690a83da24da492f70c8eb756a52d" title="permalink">#</a> </h3> <p> All three functions have compatible <code>Error</code> types, so they ought to compose with the applicative computation expression to produce the desired behaviour: </p> <p> <pre><span style="color:green;">//&nbsp;DateTime&nbsp;-&gt;&nbsp;Input&nbsp;-&gt;&nbsp;Result&lt;ValidInput,(Input&nbsp;*&nbsp;string&nbsp;list)&gt;</span> <span style="color:blue;">let</span>&nbsp;validateInput&nbsp;now&nbsp;args&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;validation&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;name&nbsp;=&nbsp;validateName&nbsp;args &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">and!</span>&nbsp;dob&nbsp;=&nbsp;validateDoB&nbsp;now&nbsp;args &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">and!</span>&nbsp;address&nbsp;=&nbsp;validateAddress&nbsp;args &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;{&nbsp;Name&nbsp;=&nbsp;name;&nbsp;DoB&nbsp;=&nbsp;dob;&nbsp;Address&nbsp;=&nbsp;address&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Result.mapError&nbsp;(<span style="color:blue;">fun</span>&nbsp;(f,&nbsp;msgs)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;args,&nbsp;msgs)</pre> </p> <p> The <code>validation</code> expression alone produces a <code>Result&lt;ValidInput,((Input -&gt; Input) * string list)&gt;</code> value. To get an <code>Input</code> value in the <code>Error</code> tuple, we need to 'run' the <code>Input -&gt; Input</code> endomorphism. The <code>validateInput</code> function does that by applying the endomorphism <code>f</code> to <code>args</code> when mapping the error with <code>Result.mapError</code>. </p> <h3 id="df2c48e822ad48ce916e29da8638563a"> Tests <a href="#df2c48e822ad48ce916e29da8638563a" title="permalink">#</a> </h3> <p> To test that the <code>validateInput</code> works as intended, I first copied all the code from the original forum post. I then wrote eight <a href="https://en.wikipedia.org/wiki/Characterization_test">characterisation tests</a> against that code to make sure that I could reproduce the desired functionality. </p> <p> I then wrote a parametrised test against the new function: </p> <p> <pre>[&lt;Theory;&nbsp;ClassData(typeof&lt;ValidationTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Validation&nbsp;works``&nbsp;input&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;now&nbsp;=&nbsp;DateTime.Now &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;validateInput&nbsp;now&nbsp;input &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> The <code>ValidationTestCases</code> class is defined like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ValidationTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;Input,&nbsp;Result&lt;ValidInput,&nbsp;Input&nbsp;*&nbsp;string&nbsp;list&gt;&gt;&nbsp;()</pre> </p> <p> This class produces a set of test cases, where each test case contains an <code>input</code> value and the <code>expected</code> output. To define the test cases, I copied the eight characterisation tests I'd already produced and adjusted them so that they fit the simpler API of the <code>validateInput</code> function. Here's a few examples: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;eightYearsAgo&nbsp;=&nbsp;DateTime.Now.AddYears&nbsp;-8 <span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Name&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;Alice&quot;</span>;&nbsp;DoB&nbsp;=&nbsp;Some&nbsp;eightYearsAgo;&nbsp;Address&nbsp;=&nbsp;None&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;Error&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Name&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;Alice&quot;</span>;&nbsp;DoB&nbsp;=&nbsp;Some&nbsp;eightYearsAgo;&nbsp;Address&nbsp;=&nbsp;None&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&quot;add1&nbsp;is&nbsp;required&quot;</span>])) <span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Name&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;Alice&quot;</span>;&nbsp;DoB&nbsp;=&nbsp;Some&nbsp;eightYearsAgo;&nbsp;Address&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;({&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Alice&quot;</span>;&nbsp;DoB&nbsp;=&nbsp;eightYearsAgo;&nbsp;Address&nbsp;=&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>&nbsp;}))</pre> </p> <p> The first case expects an <code>Error</code> value because the <code>Input</code> value has no address. The other test case expects an <code>Ok</code> value because all input is fine. </p> <p> I copied all eight characterisation tests over, so now I have those eight tests, as well as the modified eight tests for the applicative-based API shown here. All sixteen tests pass. </p> <h3 id="5a20682ce9494011b78d5a5f9c2c9bad"> Conclusion <a href="#5a20682ce9494011b78d5a5f9c2c9bad" title="permalink">#</a> </h3> <p> I find this solution to the problem elegant. It's always satisfying when you can implement what at first glance looks like custom behaviour using <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstractions</a>. </p> <p> Besides the aesthetic value, I also believe that this keeps a team more productive. These concepts of monoids, semigroups, applicative functors, and so on, are concepts that you only have to learn once. Once you know them, you'll recognise them when you run into them. This means that there's less code to understand. </p> <p> An ad-hoc implementation as the original forum post suggested (even though it looked quite decent) always puts the onus on a maintenance developer to read and understand even more one-off infrastructure code. </p> <p> With an architecture based on universal abstractions and well-documented language features, a functional programmer that knows these things will be able to pick up what's going on without much trouble. Specifically, (s)he will recognise that this is 'just' applicative validation with a twist. </p> <p> This article is the December 28 entry in the <a href="https://sergeytihon.com/2020/10/22/f-advent-calendar-in-english-2020">F# Advent Calendar in English 2020</a>. </p> <p> <strong>Next:</strong> <a href="/2023/10/30/a-c-port-of-validation-with-partial-round-trip">A C# port of validation with partial round trip</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A Haskell proof of concept of validation with partial data round trip https://blog.ploeh.dk/2020/12/21/a-haskell-proof-of-concept-of-validation-with-partial-data-round-trip 2020-12-21T06:54:00+00:00 Mark Seemann <div id="post"> <p> <em>Which Semigroup best addresses the twist in the previous article?</em> </p> <p> This article is part of <a href="/2020/12/14/validation-a-solved-problem">a short article series</a> on applicative validation with a twist. The twist is that validation, when it fails, should return not only a list of error messages; it should also retain that part of the input that <em>was</em> valid. </p> <p> In this article, I'll show how I did a quick proof of concept in <a href="https://www.haskell.org">Haskell</a>. </p> <h3 id="9417153ff45d4188a65470fa2d67ea2e"> Data definitions <a href="#9417153ff45d4188a65470fa2d67ea2e" title="permalink">#</a> </h3> <p> You can't use the regular <code>Either</code> instance of <code>Applicative</code> for validation because it short-circuits on the first error. In other words, you can't collect multiple error messages, even if the input has multiple issues. Instead, you need a custom <code>Applicative</code> instance. You can <a href="/2018/11/05/applicative-validation">easily write such an instance</a> yourself, but there are a couple of libraries that already do this. For this prototype, I chose the <a href="https://hackage.haskell.org/package/validation">validation</a> package. </p> <p> <pre><span style="color:blue;">import</span>&nbsp;Data.Bifunctor <span style="color:blue;">import</span>&nbsp;Data.Time <span style="color:blue;">import</span>&nbsp;Data.Semigroup <span style="color:blue;">import</span>&nbsp;Data.Validation </pre> </p> <p> Apart from importing <code>Data.Validation</code>, I also need a few other imports for the proof of concept. All of them are well-known. I used no language extensions. </p> <p> For the proof of concept, the input is a triple of a name, a date of birth, and an address: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Input&nbsp;=&nbsp;Input&nbsp;{ &nbsp;&nbsp;<span style="color:#2b91af;">inputName</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:#2b91af;">String</span>, &nbsp;&nbsp;<span style="color:#2b91af;">inputDoB</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:blue;">Day</span>, &nbsp;&nbsp;<span style="color:#2b91af;">inputAddress</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) </pre> </p> <p> The goal is actually to <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate">parse (not validate)</a> <code>Input</code> into a safer data type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;ValidInput&nbsp;=&nbsp;ValidInput&nbsp;{ &nbsp;&nbsp;<span style="color:#2b91af;">validName</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>, &nbsp;&nbsp;<span style="color:#2b91af;">validDoB</span>&nbsp;::&nbsp;<span style="color:blue;">Day</span>, &nbsp;&nbsp;<span style="color:#2b91af;">validAddress</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) </pre> </p> <p> If parsing/validation fails, the output should report a collection of error messages <em>and</em> return the <code>Input</code> value with any valid data retained. </p> <h3 id="4ce26b2031084212a5fbab83dd848d4b"> Looking for a Semigroup <a href="#4ce26b2031084212a5fbab83dd848d4b" title="permalink">#</a> </h3> <p> My hypothesis was that validation, even with that twist, can be implemented elegantly with an <code>Applicative</code> instance. The <em>validation</em> package defines its <code>Validation</code> data type such that it's an <code>Applicative</code> instance as long as its error type is a <code>Semigroup</code> instance: </p> <p> <pre><span style="color:blue;">Semigroup</span> err =&gt; <span style="color:blue;">Applicative</span> (<span style="color:blue;">Validation</span> err)</pre> </p> <p> The question is: which <code>Semigroup</code> can we use? </p> <p> Since we need to return <em>both</em> a list of error messages <em>and</em> a modified <code>Input</code> value, it sounds like we'll need a product type of some sorts. A tuple will do; something like <code>(Input, [String])</code>. Is that a <code>Semigroup</code> instance, though? </p> <p> Tuples only form semigroups if both elements give rise to a semigroup: </p> <p> <pre>(<span style="color:blue;">Semigroup</span> a, <span style="color:blue;">Semigroup</span> b) =&gt; <span style="color:blue;">Semigroup</span> (a, b)</pre> </p> <p> The second element of my candidate is <code>[String]</code>, which is fine. Lists are <code>Semigroup</code> instances. But what about <code>Input</code>? Can we somehow combine two <code>Input</code> values into one? It's not entirely clear how we should do that, so that doesn't seem too promising. </p> <p> What we need to do, however, is to take the original <code>Input</code> and modify it by (optionally) resetting one or more fields. In other words, a series of functions of the type <code>Input -&gt; Input</code>. Aha! There's the semigroup we need: <a href="https://hackage.haskell.org/package/base/docs/Data-Semigroup.html#t:Endo"><code>Endo Input</code></a>. </p> <p> So the <code>Semigroup</code> instance we need is <code>(<span style="color:blue;">Endo Input</span>, [<span style="color:#2b91af;">String</span>])</code>, and the validation output should be of the type <code><span style="color:blue;">Validation</span> (<span style="color:blue;">Endo Input</span>, [<span style="color:#2b91af;">String</span>]) a</code>. </p> <h3 id="5da9d89ac8414ad0bc9ebe322b831390"> Validators <a href="#5da9d89ac8414ad0bc9ebe322b831390" title="permalink">#</a> </h3> <p> Cool, we can now implement the validation logic; a function for each field, starting with the name: </p> <p> <pre><span style="color:#2b91af;">validateName</span>&nbsp;::&nbsp;<span style="color:blue;">Input</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;(<span style="color:blue;">Endo</span>&nbsp;<span style="color:blue;">Input</span>,&nbsp;[<span style="color:#2b91af;">String</span>])&nbsp;<span style="color:#2b91af;">String</span> validateName&nbsp;(Input&nbsp;(Just&nbsp;name)&nbsp;_&nbsp;_)&nbsp;|&nbsp;<span style="color:blue;">length</span>&nbsp;name&nbsp;&gt;&nbsp;3&nbsp;=&nbsp;Success&nbsp;name validateName&nbsp;(Input&nbsp;(Just&nbsp;_)&nbsp;_&nbsp;_)&nbsp;= &nbsp;&nbsp;Failure&nbsp;(Endo&nbsp;$&nbsp;\x&nbsp;-&gt;&nbsp;x&nbsp;{&nbsp;inputName&nbsp;=&nbsp;Nothing&nbsp;},&nbsp;[<span style="color:#a31515;">&quot;no&nbsp;bob&nbsp;and&nbsp;toms&nbsp;allowed&quot;</span>]) validateName&nbsp;_&nbsp;=&nbsp;Failure&nbsp;(mempty,&nbsp;[<span style="color:#a31515;">&quot;name&nbsp;is&nbsp;required&quot;</span>]) </pre> </p> <p> This function reproduces the validation logic implied by <a href="https://forums.fsharp.org/t/thoughts-on-input-validation-pattern-from-a-noob/1541">the forum question that started it all</a>. Notice, particularly, that when the name is too short, the endomorphism resets <code>inputName</code> to <code>Nothing</code>. </p> <p> The date-of-birth validation function works the same way: </p> <p> <pre><span style="color:#2b91af;">validateDoB</span>&nbsp;::&nbsp;<span style="color:blue;">Day</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Input</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;(<span style="color:blue;">Endo</span>&nbsp;<span style="color:blue;">Input</span>,&nbsp;[<span style="color:#2b91af;">String</span>])&nbsp;<span style="color:blue;">Day</span> validateDoB&nbsp;now&nbsp;(Input&nbsp;_&nbsp;(Just&nbsp;dob)&nbsp;_)&nbsp;|&nbsp;addGregorianYearsRollOver&nbsp;(-12)&nbsp;now&nbsp;&lt;&nbsp;dob&nbsp;= &nbsp;&nbsp;Success&nbsp;dob validateDoB&nbsp;_&nbsp;(Input&nbsp;_&nbsp;(Just&nbsp;_)&nbsp;_)&nbsp;= &nbsp;&nbsp;Failure&nbsp;(Endo&nbsp;$&nbsp;\x&nbsp;-&gt;&nbsp;x&nbsp;{&nbsp;inputDoB&nbsp;=&nbsp;Nothing&nbsp;},&nbsp;[<span style="color:#a31515;">&quot;get&nbsp;off&nbsp;my&nbsp;lawn&quot;</span>]) validateDoB&nbsp;_&nbsp;_&nbsp;=&nbsp;Failure&nbsp;(mempty,&nbsp;[<span style="color:#a31515;">&quot;dob&nbsp;is&nbsp;required&quot;</span>]) </pre> </p> <p> Again, the validation logic is inferred from the forum question, although I found it better keep the function pure by requiring a <code>now</code> argument. </p> <p> The address validation is the simplest of the three validators: </p> <p> <pre><span style="color:#2b91af;">validateAddress</span>&nbsp;::&nbsp;<span style="color:blue;">Monoid</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Input</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;(a,&nbsp;[<span style="color:#2b91af;">String</span>])&nbsp;<span style="color:#2b91af;">String</span> validateAddress&nbsp;(Input&nbsp;_&nbsp;_&nbsp;(Just&nbsp;a))&nbsp;=&nbsp;Success&nbsp;a validateAddress&nbsp;_&nbsp;=&nbsp;Failure&nbsp;(mempty,&nbsp;[<span style="color:#a31515;">&quot;add1&nbsp;is&nbsp;required&quot;</span>]) </pre> </p> <p> This one's return type is actually more general than required, since I used <code>mempty</code> instead of <code>Endo id</code>. This means that it actually works for any <code>Monoid a</code>, which also includes <code>Endo Input</code>. </p> <h3 id="6d5502d178f143d58a4d3c5bef7c1f05"> Composition <a href="#6d5502d178f143d58a4d3c5bef7c1f05" title="permalink">#</a> </h3> <p> All three functions return <code><span style="color:blue;">Validation</span> (<span style="color:blue;">Endo Input</span>, [<span style="color:#2b91af;">String</span>])</code>, which has an <code>Applicative</code> instance. This means that we should be able to compose them together to get the behaviour we're looking for: </p> <p> <pre><span style="color:#2b91af;">validateInput</span>&nbsp;::&nbsp;<span style="color:blue;">Day</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Input</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;(<span style="color:blue;">Input</span>,&nbsp;[<span style="color:#2b91af;">String</span>])&nbsp;<span style="color:blue;">ValidInput</span> validateInput&nbsp;now&nbsp;args&nbsp;= &nbsp;&nbsp;toEither&nbsp;$ &nbsp;&nbsp;first&nbsp;(first&nbsp;(`appEndo`&nbsp;args))&nbsp;$ &nbsp;&nbsp;ValidInput&nbsp;&lt;$&gt;&nbsp;validateName&nbsp;args&nbsp;&lt;*&gt;&nbsp;validateDoB&nbsp;now&nbsp;args&nbsp;&lt;*&gt;&nbsp;validateAddress&nbsp;args </pre> </p> <p> That compiles, so it probably works. </p> <h3 id="9ed5a5fe379244a3bd1e9206a79a1ea9"> Sanity check <a href="#9ed5a5fe379244a3bd1e9206a79a1ea9" title="permalink">#</a> </h3> <p> Still, it'd be prudent to check. Since this is only a proof of concept, I'm not going to set up a test suite. Instead, I'll just start GHCi for some ad-hoc testing: </p> <p> <pre>λ&gt; now &lt;- localDay &lt;&amp;&gt; zonedTimeToLocalTime &lt;&amp;&gt; getZonedTime λ&gt; validateInput now &amp; Input Nothing Nothing Nothing Left (Input {inputName = Nothing, inputDoB = Nothing, inputAddress = Nothing}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["name is required","dob is required","add1 is required"]) λ&gt; validateInput now &amp; Input (Just "Bob") Nothing Nothing Left (Input {inputName = Nothing, inputDoB = Nothing, inputAddress = Nothing}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["no bob and toms allowed","dob is required","add1 is required"]) λ&gt; validateInput now &amp; Input (Just "Alice") Nothing Nothing Left (Input {inputName = Just "Alice", inputDoB = Nothing, inputAddress = Nothing}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["dob is required","add1 is required"]) λ&gt; validateInput now &amp; Input (Just "Alice") (Just &amp; fromGregorian 2002 10 12) Nothing Left (Input {inputName = Just "Alice", inputDoB = Nothing, inputAddress = Nothing}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["get off my lawn","add1 is required"]) λ&gt; validateInput now &amp; Input (Just "Alice") (Just &amp; fromGregorian 2012 4 21) Nothing Left (Input {inputName = Just "Alice", inputDoB = Just 2012-04-21, inputAddress = Nothing}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["add1 is required"]) λ&gt; validateInput now &amp; Input (Just "Alice") (Just &amp; fromGregorian 2012 4 21) (Just "x") Right (ValidInput {validName = "Alice", validDoB = 2012-04-21, validAddress = "x"})</pre> </p> <p> In order to make the output more readable, I've manually edited the GHCi session by adding line breaks to the output. </p> <p> It looks like it's working like it's supposed to. Only the last line successfully parses the input and returns a <code>Right</code> value. </p> <h3 id="8bbb1d8ca355495b95e5e5ed85a924f4"> Conclusion <a href="#8bbb1d8ca355495b95e5e5ed85a924f4" title="permalink">#</a> </h3> <p> Before I started this proof of concept, I had an inkling of the way this would go. Instead of making the prototype in <a href="https://fsharp.org">F#</a>, I found it more productive to do it in Haskell, since Haskell enables me to compose things together. I particularly appreciate how a composition of types like <code>(<span style="color:blue;">Endo Input</span>, [<span style="color:#2b91af;">String</span>])</code> is automatically a <code>Semigroup</code> instance. I don't have to do anything. That makes the language great for prototyping things like this. </p> <p> Now that I've found the appropriate semigroup, I know how to convert the code to F#. That's in the next article. </p> <p> <strong>Next:</strong> <a href="/2020/12/28/an-f-demo-of-validation-with-partial-data-round-trip">An F# demo of validation with partial data round trip</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0ebea2f4d9c54072a5bb0c093a63fe14"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#0ebea2f4d9c54072a5bb0c093a63fe14">#</a></div> <div class="comment-content"> <p> Great work and excellent post. I just had a few clarification quesitons. </p> <blockquote> <p> ...But what about <code>Input</code>? Can we somehow combine two <code>Input</code> values into one? It's not entirely clear how we should do that, so that doesn't seem too promising. </p> <p> What we need to do, however, is to take the original <code>Input</code> and modify it by (optionally) resetting one or more fields. In other words, a series of functions of the type <code>Input -&gt; Input</code>. Aha! There's the semigroup we need: <code>Endo Input</code>. </p> </blockquote> <p> How rhetorical are those questions? Whatever the case, I will take the bait. </p> <p> Any product type forms a semigroup if all of its elements do. You explicitly stated this for tuples of length 2; it also holds for records such as <code>Input</code>. Each field on that record has type <code> Maybe a</code> for some <code>a</code>, so it suffices to select a semigroup involving <code>Maybe a</code>. There are few different semigropus involving <code>Maybe</code> that have different functions. </p> <p> I think the most common semigroup for <code>Maybe a</code> has the function that returns the first <code>Just _</code> if one exists or else returns <code>Nothing</code>. Combining that with <code>Nothing</code> as the identity element gives the monoid that is typically associated with <code>Maybe a</code> (and I know by the name monoidal plus). Another monoid, and therefore a semigroup, is to return the last <code>Just _</code> instead of the first. </p> <p> Instead of the having a preference for <code>Just _</code>, the function could have a preference for <code>Nothing</code>. As before, when both inputs are <code>Just _</code>, the output could be either of the inputs. </p> <p> I think either of those last two semigroups will achieved the desired behavior in the problem at hand. Your code never replaces an instace of <code>Just a</code> with a different instance, so we don't need a preference for some input when they are both <code>Just _</code>. </p> <p> In the end though, I think the semigroup you derived from <code>Endo</code> leads to simpler code. </p> <p> At the end of the type signature for <code>validateName</code> / <code>validateDoB</code> / <code>validateAddress</code>, what does <code>String</code> / <code>Day</code> / <code>String</code> mean? </p> <p> Why did you pass all three arguments into every parsing/validation function? I think it is a bit simpler to only pass in the needed argument. Maybe you thought this was good enough for prototype code. </p> <p> Why did you use <code>add1</code> in your error message instead of <code>address</code>? Was it only for prototype code to make the message a bit shorter? </p> </div> <div class="comment-date">2020-12-21 14:21 UTC</div> </div> <div class="comment" id="510b9be50c1b43c18973008b89d2da38"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#510b9be50c1b43c18973008b89d2da38">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. The semigroup you suggest, I take it, would look something like this: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;Perhaps&nbsp;a&nbsp;=&nbsp;Perhaps&nbsp;{&nbsp;runPerhaps&nbsp;::&nbsp;Maybe&nbsp;&nbsp;a&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;(<span style="color:blue;">Perhaps</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;Perhaps&nbsp;Nothing&nbsp;&lt;&gt;&nbsp;_&nbsp;=&nbsp;Perhaps&nbsp;Nothing &nbsp;&nbsp;_&nbsp;&lt;&gt;&nbsp;Perhaps&nbsp;Nothing&nbsp;=&nbsp;Perhaps&nbsp;Nothing &nbsp;&nbsp;Perhaps&nbsp;(Just&nbsp;x)&nbsp;&lt;&gt;&nbsp;_&nbsp;=&nbsp;Perhaps&nbsp;(Just&nbsp;x)</pre> </p> <p> That might work, but it's an atypical semigroup. I <em>think</em> that it's lawful - at least, I can't come up with a counterexample against associativity. It seems reminiscent of Boolean <em>and</em> (the <em>All</em> monoid), but it isn't a monoid, as far as I can tell. </p> <p> Granted, a <code>Monoid</code> constraint isn't required to make the validation code work, but following the <a href="https://en.wikipedia.org/wiki/Principle_of_least_astonishment">principle of least surprise</a>, I still think that picking a well-known semigroup such as <code>Endo</code> is preferable. </p> <p> Regarding your second question, the type signature of e.g. <code>validateName</code> is: </p> <p> <pre><span style="color:#2b91af;">validateName</span>&nbsp;::&nbsp;<span style="color:blue;">Input</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;(<span style="color:blue;">Endo</span>&nbsp;<span style="color:blue;">Input</span>,&nbsp;[<span style="color:#2b91af;">String</span>])&nbsp;<span style="color:#2b91af;">String</span></pre> </p> <p> Like <code>Either</code>, <code>Validation</code> has two type arguments: <code>err</code> and <code>a</code>; it's defined as <code>data Validation err a</code>. In the above function type, the return value is a <code>Validation</code> value where the <code>err</code> type is <code>(Endo Input, [String])</code> and <code>a</code> is <code>String</code>. </p> <p> All three validation functions share a common <code>err</code> type: <code>(Endo Input, [String])</code>. On the other hand, they return various <code>a</code> types: <code>String</code>, <code>Day</code>, and <code>String</code>, respectively. </p> <p> Regarding your third question, I could also have defined the functions so that they would only have taken the values they'd need to validate. That would better fit <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>, so I should probably have done that... </p> <p> As for the last question, I was just following the 'spec' implied by <a href="https://forums.fsharp.org/t/thoughts-on-input-validation-pattern-from-a-noob/1541">the original forum question</a>. </p> </div> <div class="comment-date">2020-12-22 15:05 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Validation, a solved problem? https://blog.ploeh.dk/2020/12/14/validation-a-solved-problem 2020-12-14T08:28:00+00:00 Mark Seemann <div id="post"> <p> <em>A validation problem with a twist.</em> </p> <p> Until recently, I thought that data validation was a solved problem: <a href="/2018/11/05/applicative-validation">Use an applicative functor</a>. I then encountered <a href="https://forums.fsharp.org/t/thoughts-on-input-validation-pattern-from-a-noob/1541">a forum question</a> that for a few minutes shook my faith. </p> <p> After brief consideration, though, I realised that all is good. Validation, even with a twist, is successfully modelled with an <a href="/2018/10/01/applicative-functors">applicative functor</a>. Faith in computer science restored. </p> <h3 id="891801e3802f49d8a51b56bebaacecb9"> The twist <a href="#891801e3802f49d8a51b56bebaacecb9" title="permalink">#</a> </h3> <p> Usually, when you see a demo of applicative validation, the result of validating is one of two: either <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate">a parsed result</a>, or a collection of error messages. </p> <p> <pre>λ&gt; validateReservation $ ReservationJson "2017-06-30 19:00:00+02:00" 4 "Jane Doe" "j@example.com" Validation (Right (Reservation { &nbsp;&nbsp;&nbsp;&nbsp;reservationDate = 2017-06-30 19:00:00 +0200, &nbsp;&nbsp;&nbsp;&nbsp;reservationQuantity = 4, &nbsp;&nbsp;&nbsp;&nbsp;reservationName = "Jane Doe", &nbsp;&nbsp;&nbsp;&nbsp;reservationEmail = "j@example.com"})) λ&gt; validateReservation $ ReservationJson "2017/14/12 6pm" 4.1 "Jane Doe" "jane.example.com" Validation (Left ["Not a date.","Not a positive integer.","Not an email address."]) λ&gt; validateReservation $ ReservationJson "2017-06-30 19:00:00+02:00" (-3) "Jane Doe" "j@example.com" Validation (Left ["Not a positive integer."])</pre> </p> <p> (Example from <a href="/2018/11/05/applicative-validation">Applicative validation</a>.) </p> <p> What if, instead, you're displaying an input form? When users enter data, you want to validate it. Imagine, for the rest of this short series of articles that the input form has three fields: <em>name</em>, <em>date of birth</em>, and <em>address</em>. Each piece of data has associated validation rules. </p> <p> If you enter a valid name, but an invalid date of birth, you want to clear the input form's date of birth, but not the name. It's such a bother for a user having to retype valid data just because a single field turned out to be invalid. </p> <p> Imagine, for example, that you want to bind the form to a data model like this <a href="https://fsharp.org">F#</a> record type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Input&nbsp;=&nbsp;{&nbsp;Name&nbsp;:&nbsp;string&nbsp;option;&nbsp;DoB&nbsp;:&nbsp;DateTime&nbsp;option;&nbsp;Address&nbsp;:&nbsp;string&nbsp;option}</pre> </p> <p> Each of these three fields is optional. We'd like validation to work in the following way: If validation fails, the function should return <em>both</em> a list of error messages, and <em>also</em> the <code>Input</code> object, with valid data retained, but invalid data cleared. </p> <p> One of the rules implied in the forum question is that names must be more than three characters long. Thus, input like this is invalid: </p> <p> <pre>{&nbsp;Name&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;Tom&quot;</span>;&nbsp;DoB&nbsp;=&nbsp;Some&nbsp;eightYearsAgo;&nbsp;Address&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>&nbsp;}</pre> </p> <p> Both the <code>DoB</code> and <code>Address</code> fields, however, are valid, so, along with error messages, we'd like our validation function to return a partially wiped <code>Input</code> value: </p> <p> <pre>{&nbsp;Name&nbsp;=&nbsp;None;&nbsp;DoB&nbsp;=&nbsp;Some&nbsp;eightYearsAgo;&nbsp;Address&nbsp;=&nbsp;Some&nbsp;<span style="color:#a31515;">&quot;x&quot;</span>&nbsp;}</pre> </p> <p> Notice that both <code>DoB</code> and <code>Address</code> field values are retained, while <code>Name</code> has been reset. </p> <p> A final requirement: If validation succeeds, the return value should be a <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate">parsed value that captures that validation took place</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ValidInput&nbsp;=&nbsp;{&nbsp;Name&nbsp;:&nbsp;string;&nbsp;DoB&nbsp;:&nbsp;DateTime;&nbsp;Address&nbsp;:&nbsp;string&nbsp;}</pre> </p> <p> That requirement is straightforward. That's how you'd usually implement application validation. It's the partial data round-trip that seems to throw a spanner in the works. </p> <p> How should we model such validation? </p> <h3 id="534fcc2d66f242a0ba10a9aca7827276"> Theory, applied <a href="#534fcc2d66f242a0ba10a9aca7827276" title="permalink">#</a> </h3> <p> There's a subculture of functional programming that draws heavily on <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>. This is most prevalent in <a href="https://www.haskell.org">Haskell</a>. I've been studying category theory in an attempt to understand what it's all about. I even wrote <a href="/2017/10/04/from-design-patterns-to-category-theory">a substantial article series</a> about some design patterns and how they relate to theory. </p> <p> One thing I learned <em>after</em> I'd named that article series is that most of the useful theoretical concepts come from <a href="https://en.wikipedia.org/wiki/Abstract_algebra">abstract algebra</a>, with the possible exception of monads. </p> <p> People often ask me: does all that theory have any practical use? </p> <p> Yes, it does, as it turns out. It did, for example, enable me to identify a solution to the above twist in five to ten minutes. </p> <p> It's a discussion that I often have, particularly with the always friendly F# community. <em>Do you have to understand <a href="/2018/03/22/functors">functors</a>, monads, etcetera to be a productive F# developer?</em> </p> <p> To anyone who wants to learn F# I'd respond: Don't worry about that at the gate. Find a good learning resource and dive right in. It's a friendly language that you can learn gradually. </p> <p> Sooner or later, though, you'll run into knotty problems that you may struggle to address. I've seen this enough times that it looks like a pattern. The present forum question is just one example. A beginner or intermediate F# programmer will typically attempt to solve the problem in an ad-hoc manner that may or may not be easy to maintain. (The solution proposed by the author of that forum question doesn't, by the way, look half bad.) </p> <p> To be clear: there's nothing wrong with being a beginner. I was once a beginner programmer, and I'm <em>still</em> a beginner in multiple ways. What I'm trying to argue here is that there <em>is</em> value in knowing theory. With my knowledge of abstract algebra and how it applies to functional programming, it didn't take me long to identify a solution. I'll get to that later. </p> <p> Before I outline a solution, I'd like to round off the discussion of applied theory. That question about monads comes up a lot. <em>Do I have to understand functors, monads, etcetera to be a good F# developer?</em> </p> <p> I think it's like asking <em>Do I have to understand polymorphism, design patterns, the <a href="https://en.wikipedia.org/wiki/SOLID">SOLID principles</a>, etcetera to be a good object-oriented programmer?</em> </p> <p> Those are typically not the first topics people are taught about OOD. I would assert, however, that understanding such topics do help. They may not be required to get started with OOP, but knowing them makes you a better programmer. </p> <p> I think the same is true for functional programming. It's just a different skill set that makes you better in that paradigm. </p> <h3 id="78fdc446770a4ec48b556e0826a59ce9"> Solution outline <a href="#78fdc446770a4ec48b556e0826a59ce9" title="permalink">#</a> </h3> <p> When you know a bit of theory, you may know that validation can be implemented with an applicative sum type like <a href="/2019/01/14/an-either-functor">Either</a> (AKA <em>Option</em>), with one extra requirement. </p> <p> Either <a href="/2019/01/07/either-bifunctor">has two dimensions</a>, <em>left</em> or <em>right</em> (<em>success</em> or <em>failure</em>, <em>ok</em> or <em>error</em>, etcetera). The applicative nature of it already supplies a way to compose the successes, but what if there's more than one validation error? </p> <p> In my <a href="/2018/11/05/applicative-validation">article about applicative validation</a> I showed how to collect multiple error messages in a list. Lists, however, <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">form a monoid</a>, so I typed the validation API to be that flexible. </p> <p> In fact, all you need is a <a href="/2017/11/27/semigroups">semigroup</a>. When I wrote the article on applicative validation, Haskell's <code>Semigroup</code> type class wasn't yet a supertype of <code>Monoid</code>, and I (perhaps without sufficient contemplation) just went with <code>Monoid</code>. </p> <p> What remains is that applicative validation can collect errors for <em>any</em> semigroup of errors. All we need to solve the above validation problem with a twist, then, is to identify a suitable semigroup. </p> <p> I don't want to give away everything in this article, so I'm going to leave you with this cliffhanger. Which semigroup solves the problem? Read on. <ul> <li><a href="/2020/12/21/a-haskell-proof-of-concept-of-validation-with-partial-data-round-trip">A Haskell proof of concept of validation with partial data round trip</a></li> <li><a href="/2020/12/28/an-f-demo-of-validation-with-partial-data-round-trip">An F# demo of validation with partial data round trip</a></li> <li><a href="/2023/10/30/a-c-port-of-validation-with-partial-round-trip">A C# port of validation with partial round trip</a></li> </ul> As is often my modus operandi, I first did a proof of concept in Haskell. With its type classes and higher-kinded polymorphism, it's much faster to prototype solutions than even in F#. In the next article, I'll describe how that turned out. </p> <p> After the Haskell article, I'll show how it translates to F#. You can skip the Haskell article if you like. </p> <h3 id="048492d078164a648dddf6c57dbaf490"> Conclusion <a href="#048492d078164a648dddf6c57dbaf490" title="permalink">#</a> </h3> <p> I still think that validation is a solved problem. It's always interesting when such a belief for a moment is challenged, and satisfying to discover that it still holds. </p> <p> This is, after all, not proof of anything. Perhaps tomorrow, someone will throw another curve ball that I can't catch. If that happens, I'll have to update my beliefs. Until then, I'll consider validation a solved problem. </p> <p> <strong>Next:</strong> <a href="/2020/12/21/a-haskell-proof-of-concept-of-validation-with-partial-data-round-trip">A Haskell proof of concept of validation with partial data round trip</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Branching tests https://blog.ploeh.dk/2020/12/07/branching-tests 2020-12-07T06:25:00+00:00 Mark Seemann <div id="post"> <p> <em>Is it ever okay to branch and loop in a unit test?</em> </p> <p> When I coach development organisations about unit testing and test-driven development, there's often a sizeable group of developers who don't see the value of unit testing. Some of the arguments they typically use are worth considering. </p> <p> A common complaint is that it's difficult to see the wisdom in writing code to prevent defects in code. That's not an unreasonable objection. </p> <p> <a href="/2020/05/25/wheres-the-science">We have scant scientific knowledge about software engineering</a>, but the little we know suggests that the number of defects is proportional to lines of code. The more lines of code, the more defects. </p> <p> If that's true, adding more code - even when it's test code - seems like a bad idea. </p> <h3 id="88c3fa7503454d9f9e329db299f70881"> Reasons to trust test code <a href="#88c3fa7503454d9f9e329db299f70881" title="permalink">#</a> </h3> <p> First, we should consider the possibility that the correlation between lines of code and defects doesn't mean that defects are <em>evenly</em> distributed. As <a href="https://www.adamtornhill.com">Adam Tornhill</a> argues in <a href="https://amzn.to/36Pd5EE">Your Code as a Crime Scene</a>, defects tend to cluster in hotspots. </p> <p> You can have a large proportion of your code base which is, for all intents and purpose, bug-free, and hotspots where defects keep spawning. </p> <p> If this is true, adding test code isn't a problem if you can keep it bug-free. </p> <p> That, however, sounds like a chicken-and-the-egg kind of problem. How can you know that test code is bug-free without tests? </p> <p> I've <a href="/2013/04/02/why-trust-tests">previously answered that question</a>. In short, you can trust a test for two reasons: <ul> <li>You've seen it fail (haven't you?)</li> <li>It's simple</li> </ul> I usually think of the simplicity criterion as a limit on <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a>: it should be <em>1</em>. This means no branching and no loops in your tests. </p> <p> That's what this article is actually about. </p> <h3 id="0494e1e3524d46b186ad7fc039dc7c17"> What's in a name? <a href="#0494e1e3524d46b186ad7fc039dc7c17" title="permalink">#</a> </h3> <p> I was working with an online restaurant reservation system (example code), and had written this test: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2023-11-24&nbsp;19:00&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2024-02-13&nbsp;18:15&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Parse(dto.At,&nbsp;<span style="color:#2b91af;">CultureInfo</span>.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db); }</pre> </p> <p> This is a <a href="/2019/02/18/from-interaction-based-to-state-based-testing">state-based test</a> that verifies that a valid reservation makes it to the database. The test has a cyclomatic complexity of <em>1</em>, and I've seen it fail, so all is good. (It may, in fact, contain a future maintenance problem, but that's a topic for <a href="/2021/01/11/waiting-to-happen">another article</a>.) </p> <p> <ins datetime="2021-07-13T06:34Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <p> What constitutes a valid reservation? At the very least, we should demand that <code>At</code> is a valid date and time, and that <code>Quantity</code> is a positive number. The restaurant would like to be able to email a confirmation to the user, so an email address is also required. Email addresses are notoriously difficult to validate, so we'll just require that the the string isn't null. </p> <p> What about the <code>Name</code>? I thought about this a bit and decided that, according to <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>, the system should accept null names. The name is only a convenience; the system doesn't need it, it's just there so that when you arrive at the restaurant, you can say <em>"I have a reservation for Julia"</em> instead of giving an email address to the <a href="https://en.wikipedia.org/wiki/Ma%C3%AEtre_d%27h%C3%B4tel">maître d'hôtel</a>. But then, if you didn't supply a name when you made the reservation, you can always state your email address when you arrive. To summarise, the name is just a convenience, not a requirement. </p> <p> This decision meant that I ought to write a test case with a null name. </p> <p> That turned out to present a problem. I'd defined the <code>Reservation</code> class so that it didn't accept <code>null</code> arguments, and I think that's the appropriate design. Null is just evil and has no place in my domain models. </p> <p> That's not a problem in itself. In this case, I think it's acceptable to convert a null name to the empty string. </p> <h3 id="f0caaae1c01d47378b0f23a8d6a95d72"> Copy and paste <a href="#f0caaae1c01d47378b0f23a8d6a95d72" title="permalink">#</a> </h3> <p> Allow me to summarise. If you consider the above unit test, I needed a third test case with a null <code>name</code>. In that case, <code>expected</code> should be a <code>Reservation</code> value with the name <code>""</code>. Not <code>null</code>, but <code>""</code>. </p> <p> As far as I can tell, you can't easily express that in <code>PostValidReservationWhenDatabaseIsEmpty</code> without increasing its cyclomatic complexity. Based on the above introduction, that seems like a no-no. </p> <p> What's the alternative? Should I copy the test and adjust the <em>single</em> line of code that differs? If I did, it would look like this: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2023-08-23&nbsp;16:55&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWithNullNameWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Parse(dto.At,&nbsp;<span style="color:#2b91af;">CultureInfo</span>.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db); }</pre> </p> <p> Apart from the values in the <code>[InlineData]</code> attribute and the method name, the <em>only</em> difference from <code>PostValidReservationWhenDatabaseIsEmpty</code> is that <code>expected</code> has a hard-coded name of <code>""</code>. </p> <p> This is not acceptable. </p> <p> There's a common misconception that the <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> principle doesn't apply to unit tests. I don't see why this should be true. The DRY principle exists because copy-and-paste code is difficult to maintain. Unit test code is also code that you have to maintain. All the rules about writing maintainable code also apply to unit test code. </p> <h3 id="1d10bd69a6364e6984cb808ab5d9a8f8"> Branching in test <a href="#1d10bd69a6364e6984cb808ab5d9a8f8" title="permalink">#</a> </h3> <p> What's the alternative? One option (that shouldn't be easily dismissed) is to introduce a <a href="http://xunitpatterns.com/Test%20Helper.html">Test Helper</a> to perform the conversion from a nullable name to a non-nullable name. Such a helper would have a cyclomatic complexity of <em>2</em>, but could be unit tested in isolation. It might even turn out that it'd be useful in the production code. </p> <p> Still, that seems like overkill, so I instead made the taboo move and added branching logic to the existing test to see how it'd look: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2023-11-24&nbsp;19:00&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2024-02-13&nbsp;18:15&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2023-08-23&nbsp;16:55&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Parse(dto.At,&nbsp;<span style="color:#2b91af;">CultureInfo</span>.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Name&nbsp;??&nbsp;<span style="color:#a31515;">&quot;&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db); }</pre> </p> <p> Notice that the <code>expected</code> name is now computed as <code>dto.Name ?? ""</code>. Perhaps you think about branching instructions as relating exclusively to keywords such as <code>if</code> or <code>switch</code>, but the <code>??</code> operator is also a branching instruction. The test now has a cyclomatic complexity of <code>2</code>. </p> <p> Is that okay? </p> <h3 id="baf81334614241e7b36bbe7f8f1af821"> To branch or not to branch <a href="#baf81334614241e7b36bbe7f8f1af821" title="permalink">#</a> </h3> <p> I think that in this case, it's okay to slightly increase the cyclomatic complexity of the test. It's not something I just pull out of my hat, though. I think it's possible to adjust the above heuristics to embrace this sort of variation. </p> <p> To be clear, I consider this an <em>advanced</em> practice. If you're just getting started with unit testing, try to keep tests simple. Keep the cyclomatic complexity at <em>1</em>. </p> <p> Had I been in the above situation a couple of years ago, I might not have considered this option. About a year ago, though, I watched <a href="https://en.wikipedia.org/wiki/John_Hughes_(computer_scientist)">John Hughes'</a> presentation <a href="https://youtu.be/NcJOiQlzlXQ">Building on developers' intuitions to create effective property-based tests</a>. When he, about 15 minutes in, wrote a test with a branching instruction, I remember becoming quite uncomfortable. This lasted for a while until I understood where he was going with it. It's truly an inspiring and illuminating talk; I highly recommend it. </p> <p> How it relates to the problem presented here is through <em>coverage</em>. While the <code>PostValidReservationWhenDatabaseIsEmpty</code> test now has a cyclomatic complexity of <em>2</em>, it's a parametrised test with three test cases. Two of these cover one branch, and the third covers the other. </p> <p> What's more important is the process that produced the test. I added one test case at a time, and for each case, <em>I saw the test fail</em>. </p> <p> Specifically, when I added the third test case with the null name, I first added the branching expression <code>dto.Name ?? ""</code> and ran the two existing tests. They still both passed, which bolstered my belief that they both exercised the left branch of that expression. I then added the third case and saw that it (and only it) failed. This supported my belief that the third case exercised the right branch of <code>??</code>. </p> <p> Branching in unit tests isn't something I do lightly. I still believe that it could make the test more vulnerable to future changes. I'm particularly worried about making a future change that might shift one or more of these test cases into false negatives in the form of <a href="/2019/10/14/tautological-assertion">tautological assertions</a>. </p> <h3 id="bfa3a3d3f82044f289568efcc675c70f"> Conclusion <a href="#bfa3a3d3f82044f289568efcc675c70f" title="permalink">#</a> </h3> <p> As you can tell, when I feel that I'm moving onto thin ice, I move deliberately. If there's one thing I've learned from decades of professional programming it's that my brain loves jumping to conclusions. Moving slowly and deliberately is my attempt at countering this tendency. I believe that it enables me to go faster in the long run. </p> <p> I don't think that branching in unit tests should be common, but I believe that it may be occasionally valid. The key, I think, is to guarantee that each branch in the test is covered by a test case. The implication is that there must be <em>at least</em> as many test cases as the cyclomatic complexity. In other words, the test <em>must</em> be a parametrised test. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="cbbf719d6e1744c9879edb6242bbdf21"> <div class="comment-author"><a href="http://www.morcs.com">James Morcom</a> <a href="#cbbf719d6e1744c9879edb6242bbdf21">#</a></div> <div class="comment-content"> <p>Hi Mark, I guess there is implicit cyclomatic complexity in the testing framework itself (For example, it loops through the <code>InlineData</code> records). That feels fine though, does this somehow have less cost than cyclomatic complexity in the test code itself? I guess, as you mentioned, it's acceptable because the alternative is violation of DRY. </p> <p> With this in mind, I wonder how you feel about adding an <code>expectedName</code> parameter to the <code>InlineData</code> attributes, instead of the conditional in the test code? Maybe it's harder to read though when the test data includes input and output. </p> </div> <div class="comment-date">2020-12-07 08:36 UTC</div> </div> <div class="comment" id="1f5efe14d22441ffac85f5f5afc9b3b1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1f5efe14d22441ffac85f5f5afc9b3b1">#</a></div> <div class="comment-content"> <p> James, thank you for writing. I consider <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use#de927bfcc95d410bbfcd0adf7a63926b">the cyclomatic complexity of a method call to be <em>1</em></a>, and Visual Studio code metrics agree with me. Whatever happens in a framework should, in my opinion, likewise be considered as encapsulated abstraction that's none of our business. </p> <p> Adding an <code>expectedName</code> parameter to the method is definitely an option. I sometimes do that, and I could have done that here, too. In this situation, I think it's a toss-up. It'd make it harder for a later reader of the code to parse the test cases, but would simplify the test code itself, so that alternative comes with both advantages and disadvantages. </p> </div> <div class="comment-date">2020-12-08 11:02 UTC</div> </div> <div class="comment" id="e5a0f70c39f411ebadc10242ac120002"> <div class="comment-author">Romain Deneau <a href="https://twitter.com/DeneauRomain">@DeneauRomain</a> <a href="#e5a0f70c39f411ebadc10242ac120002">#</a></div> <div class="comment-content"> <p> Hi Mark. To build up on the additional <code>expectedName</code> parameter, instead of keeping a single test with the 3 cases but the last being a edge case, I prefer introduce a specific test for the last case. </p> <p> Then, to remove the duplication, we can extract a common method which will take this additional <code>expectedName</code> parameter: </p> <p> <pre> [<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2023-11-24&nbsp;19:00&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2024-02-13&nbsp;18:15&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;&nbsp;&nbsp;&nbsp;9)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWithNameWhenDatabaseIsEmpty &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">string</span>&nbsp;at,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name,&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity)&nbsp;=> PostValidReservationWhenDatabaseIsEmpty(at, email, name, expectedName: name, quantity); [<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWithoutNameWhenDatabaseIsEmpty()&nbsp;=> PostValidReservationWhenDatabaseIsEmpty( at          : <span style="color:#a31515;">&quot;2023-11-24&nbsp;19:00&quot;</span>, email       : <span style="color:#a31515;">&quot;juliad@example.net&quot;</span>, name        : <span style="color:blue;">null</span>, expectedName: <span style="color:#a31515;">&quot;&quot;</span>, quantity    : 5); <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;expectedName, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>   &nbsp;quantity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;at, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity, &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Parse(dto.At,&nbsp;<span style="color:#2b91af;">CultureInfo</span>.InvariantCulture), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expectedName,&nbsp;<span style="color:green;">// /!\ Not `dto.Name`</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db); } </pre> </p> </div> <div class="comment-date">2020-12-09 8:44 UTC</div> </div> <div class="comment" id="2779280fced748b0879a4d7267f3d634"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2779280fced748b0879a4d7267f3d634">#</a></div> <div class="comment-content"> <p> Romain, thank you for writing. There are, indeed, many ways to skin that cat. If you're comfortable with distributing a test over more than one method, I instead prefer to use another data source for the <code>[Theory]</code> attribute: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PostValidReservationWhenDatabaseIsEmptyTestCases</span>&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TheoryData</span>&lt;<span style="color:#2b91af;">ReservationDto</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PostValidReservationWhenDatabaseIsEmptyTestCases</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddWithName(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2023,&nbsp;11,&nbsp;24,&nbsp;19,&nbsp;0,&nbsp;0),&nbsp;<span style="color:#a31515;">&quot;juliad@example.net&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Julia&nbsp;Domna&quot;</span>,&nbsp;5); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddWithName(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2024,&nbsp;2,&nbsp;13,&nbsp;18,&nbsp;15,&nbsp;0),&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Xenia&nbsp;Ng&quot;</span>,&nbsp;9); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddWithoutName(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2023,&nbsp;8,&nbsp;23,&nbsp;16,&nbsp;55,&nbsp;0),&nbsp;<span style="color:#a31515;">&quot;kite@example.edu&quot;</span>,&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AddWithName(<span style="color:#2b91af;">DateTime</span>&nbsp;at,&nbsp;<span style="color:blue;">string</span>&nbsp;email,&nbsp;<span style="color:blue;">string</span>&nbsp;name,&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;at.ToString(<span style="color:#a31515;">&quot;O&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(at,&nbsp;email,&nbsp;name,&nbsp;quantity)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AddWithoutName(<span style="color:#2b91af;">DateTime</span>&nbsp;at,&nbsp;<span style="color:blue;">string</span>&nbsp;email,&nbsp;<span style="color:blue;">int</span>&nbsp;quantity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;{&nbsp;At&nbsp;=&nbsp;at.ToString(<span style="color:#a31515;">&quot;O&quot;</span>),&nbsp;Email&nbsp;=&nbsp;email,&nbsp;Quantity&nbsp;=&nbsp;quantity&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(at,&nbsp;email,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;quantity)); &nbsp;&nbsp;&nbsp;&nbsp;} } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">ClassData</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">PostValidReservationWhenDatabaseIsEmptyTestCases</span>))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PostValidReservationWhenDatabaseIsEmpty( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;dto,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(expected,&nbsp;db); }</pre> </p> <p> Whether you prefer one over the other is, I think, subjective. I like my alternative, using a <code>[ClassData]</code> source, better, because I find it a bit more principled and 'pattern-based', if you will. I also like how small the actual test method becomes. </p> <p> Your solution, on the other hand, is more portable, in the sense that you could also apply it in a testing framework that doesn't have the sort of capability that xUnit.net has. That's a definite benefit with your suggestion. </p> </div> <div class="comment-date">2020-12-10 20:05 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Name by role https://blog.ploeh.dk/2020/11/30/name-by-role 2020-11-30T06:31:00+00:00 Mark Seemann <div id="post"> <p> <em>Consider naming variables according to their role, instead of their type.</em> </p> <p> My <a href="/2020/11/23/good-names-are-skin-deep">recent article on good names</a> might leave you with the impression that I consider good names unimportant. Not at all. That article was an attempt at delineating the limits of naming. Good names aren't the panacea some people seem to imply, but they're still important. </p> <p> As the cliché goes, naming is one of the hardest problems in software development. Perhaps it's hard because you have to do it so frequently. Every time you create a variable, you have to name it. It's also an opportunity to add clarity to a code base. </p> <p> A common naming strategy is to name objects after their type: </p> <p> <pre><span style="color:#2b91af;">Reservation</span>?&nbsp;reservation&nbsp;=&nbsp;dto.Validate(id);</pre> </p> <p> or: </p> <p> <pre><span style="color:#2b91af;">Restaurant</span>?&nbsp;restaurant&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;RestaurantDatabase.GetRestaurant(restaurantId);</pre> </p> <p> There's nothing inherently wrong with a naming scheme like this. It often makes sense. The <code>reservation</code> variable is a <code>Reservation</code> object, and there's not that much more to say about it. The same goes for the <code>restaurant</code> object. </p> <p> In some contexts, however, objects play specific <em>roles</em>. This is particularly prevalent with primitive types, but can happen to any type of object. It may help the reader if you name the variables according to such roles. </p> <p> In this article, I'll show you several examples. I hope these examples are so plentiful and varied that they can inspire you to come up with good names. <ins datetime="2021-07-14T06:00Z">Most of the code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="895798815b414b368058ed8640236ff9"> A variable introduced only to be named <a href="#895798815b414b368058ed8640236ff9" title="permalink">#</a> </h3> <p> In a <a href="/2020/11/09/checking-signed-urls-with-aspnet">recent article</a> I showed this code snippet: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;SignatureIsValid(<span style="color:blue;">string</span>&nbsp;candidate,&nbsp;<span style="color:#2b91af;">ActionExecutingContext</span>&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sig&nbsp;=&nbsp;context.HttpContext.Request.Query[<span style="color:#a31515;">&quot;sig&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;receivedSignature&nbsp;=&nbsp;<span style="color:#2b91af;">Convert</span>.FromBase64String(sig.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;hmac&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HMACSHA256</span>(urlSigningKey); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;computedSignature&nbsp;=&nbsp;hmac.ComputeHash(<span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;signaturesMatch&nbsp;=&nbsp;computedSignature.SequenceEqual(receivedSignature); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;signaturesMatch; }</pre> </p> <p> Did you wonder about the <code>signaturesMatch</code> variable? Why didn't I just return the result of <code>SequenceEqual</code>, like the following? </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;SignatureIsValid(<span style="color:blue;">string</span>&nbsp;candidate,&nbsp;<span style="color:#2b91af;">ActionExecutingContext</span>&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sig&nbsp;=&nbsp;context.HttpContext.Request.Query[<span style="color:#a31515;">&quot;sig&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;receivedSignature&nbsp;=&nbsp;<span style="color:#2b91af;">Convert</span>.FromBase64String(sig.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;hmac&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HMACSHA256</span>(urlSigningKey); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;computedSignature&nbsp;=&nbsp;hmac.ComputeHash(<span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;computedSignature.SequenceEqual(receivedSignature); }</pre> </p> <p> Visual Studio even offers this as a possible refactoring that it'll do for you. </p> <p> The inclusion of the <code>signaturesMatch</code> variable was a conscious decision of mine. I felt that directly returning the result of <code>SequenceEqual</code> was a bit too implicit. It forces readers to make the inference themselves: <em>Ah, the two arrays contain the same sequence of bytes; that must mean that the signatures match!</em> </p> <p> Instead of asking readers to do that work themselves, I decided to do it for them. I hope that it improves readability. It doesn't change the behaviour of the code one bit. </p> <h3 id="e5f18d0e31264b29bf11cd817b8c7bfa"> Test roles <a href="#e5f18d0e31264b29bf11cd817b8c7bfa" title="permalink">#</a> </h3> <p> When it comes to unit testing, there's plenty of inconsistent terminology. One man's <em>mock object</em> is another woman's <em>test double</em>. Most of the jargon isn't even internally consistent. Do yourself a favour and adopt a consistent pattern language. I use the one presented in <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a>. </p> <p> For instance, the thing that you're testing is the System Under Test (SUT). This can be a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> or a static method, but when it's an object, you're going to create a variable. Consider <a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/naming-sut-test-variables">naming it <em>sut</em></a>. A typical test also defines other variables. Naming one of them <code>sut</code> clearly identifies which of them is the SUT. It also protects the tests against the class in question being renamed. </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ScheduleSingleReservationCommunalTable() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;table&nbsp;=&nbsp;<span style="color:#2b91af;">Table</span>.Communal(12); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(18), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(21), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(6), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;table); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;r&nbsp;=&nbsp;<span style="color:#2b91af;">Some</span>.Reservation; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Schedule(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;r&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TimeSlot</span>(r.At,&nbsp;table.Reserve(r))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); }</pre> </p> <p> The above test follows <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">my AAA.formatting heuristic</a>. In all, it defines five variables, but there can be little doubt about which one is the <code>sut</code>. </p> <p> The <code>table</code> and <code>r</code> variables follow the mainstream practice of naming variables after their type. They play no special role, so that's okay. You may balk at such a short variable name as <code>r</code>, and that's okay. In my defence, I follow <a href="http://amzn.to/XCJi9X">Clean Code</a>'s <em>N5</em> heuristic for long and short scopes. A variable name like <code>r</code> is fine when it only spans three lines of code (four, if you also count the blank line). </p> <p> Consider also using the variable names <code>expected</code> and <code>actual</code>, as in the above example. In many unit testing frameworks, those are the argument names for the assertion. For instance, in <a href="https://xunit.net">xUnit.net</a> (which the above test uses) the <code>Assert.Equals</code> overloads are defined as <code>Equal&lt;T&gt;(T expected, T actual)</code>. Using these names for variables makes the roles clearer, I think. </p> <h3 id="bb01b7e1d37f469e8a81f288b513248a"> The other <a href="#bb01b7e1d37f469e8a81f288b513248a" title="permalink">#</a> </h3> <p> The above assertion relies on structural equality. The <code>TimeSlot</code> class is immutable, so it can safely override <code>Equals</code> (and <code>GetHashCode</code>) to implement structural equality: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>?&nbsp;obj) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">TimeSlot</span>&nbsp;other&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;==&nbsp;other.At&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tables.SequenceEqual(other.Tables); }</pre> </p> <p> I usually call the downcast variable <code>other</code> because, from the perspective of the instance, it's the other object. I usually use that convention whenever an instance interacts with another object of the same type. Among other examples, this happens when you model objects as <a href="/2017/11/27/semigroups">semigroups</a> and <a href="/2017/10/06/monoids">monoids</a>. The <a href="/2018/07/16/angular-addition-monoid">Angle struct, for example, defines this binary operation</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;Add(<span style="color:#2b91af;">Angle</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Angle</span>(<span style="color:blue;">this</span>.degrees&nbsp;+&nbsp;other.degrees); }</pre> </p> <p> Again, the method argument is in the role as the other object, so naming it <code>other</code> seems natural. </p> <p> Here's another example from a restaurant reservation code base: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Overlaps(<span style="color:#2b91af;">Seating</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(other)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Start&nbsp;&lt;&nbsp;other.End&nbsp;&amp;&amp;&nbsp;other.Start&nbsp;&lt;&nbsp;End; }</pre> </p> <p> The <code>Overlaps</code> method is an instance method on the <code>Seating</code> class. Again, <code>other</code> seems natural. </p> <h3 id="8cf3583ad0ac446fa4b4dee2272ba054"> Candidates <a href="#8cf3583ad0ac446fa4b4dee2272ba054" title="permalink">#</a> </h3> <p> The <code>Overlaps</code> method looks like a <em>predicate</em>, i.e. a function that returns a Boolean value. In the case of that method, <code>other</code> indicates the role of being the other object, but it also plays another role. It makes sense to me to call predicate input <em>candidates</em>. Typically, you have some input that you want to evaluate as either true or false. I think it makes sense to think of such a parameter as a 'truth candidate'. You can see one example of that in the above <code>SignatureIsValid</code> method. </p> <p> There, the <code>string</code> parameter is a <code>candidate</code> for having a valid signature. </p> <p> Here's another restaurant-related example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;WillAccept( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;now, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;existingReservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;candidate) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(existingReservations&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(existingReservations)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(candidate&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(candidate.At&nbsp;&lt;&nbsp;now) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsOutsideOfOpeningHours(candidate)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;seating&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Seating</span>(SeatingDuration,&nbsp;candidate.At); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;relevantReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;existingReservations.Where(seating.Overlaps); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;availableTables&nbsp;=&nbsp;Allocate(relevantReservations); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;availableTables.Any(t&nbsp;=&gt;&nbsp;t.Fits(candidate.Quantity)); }</pre> </p> <p> Here, the reservation in question is actually not yet a reservation. It might be rejected, so it's a <code>candidate</code> reservation. </p> <p> You can also use that name in <code>TryParse</code> methods, as shown in <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">this article</a>. </p> <h3 id="4301289931dc4f94abf46578ddcfd693"> Data Transfer Objects <a href="#4301289931dc4f94abf46578ddcfd693" title="permalink">#</a> </h3> <p> Another name that I like to use is <code>dto</code> for <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects</a> (DTOs). The benefit here is that as long as <code>dto</code> is unambiguous in context, it makes it easier to distinguish between a DTO and the domain model you might want to turn it into: </p> <p> <pre>[<span style="color:#2b91af;">HttpPost</span>(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/reservations&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Post(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;dto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dto&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(dto)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;id&nbsp;=&nbsp;dto.ParseId()&nbsp;??&nbsp;<span style="color:#2b91af;">Guid</span>.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>?&nbsp;reservation&nbsp;=&nbsp;dto.Validate(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BadRequestResult</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;restaurant&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;RestaurantDatabase.GetRestaurant(restaurantId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(restaurant&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotFoundResult</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;TryCreate(restaurant,&nbsp;reservation); }</pre> </p> <p> By naming the input parameter <code>dto</code>, I keep the name <code>reservation</code> free for the domain object, which ought to be the more important object of the two. <blockquote> <p> "A Data Transfer Object is one of those objects our mothers told us never to write." </p> <footer><cite><a href="http://bit.ly/patternsofeaa">Martin Fowler</a></cite></footer> </blockquote> I could have named the input parameter <code>reservationDto</code> instead of <code>dto</code>, but that would diminish the 'mental distance' between <code>reservationDto</code> and <code>reservation</code>. I like to keep that distance, so that the roles are more explicit. </p> <h3 id="b8ece382f7554141aa741fc9ded9e02a"> Time <a href="#b8ece382f7554141aa741fc9ded9e02a" title="permalink">#</a> </h3> <p> You often need to make decisions based on the current time or date. In .NET the return value from <a href="https://docs.microsoft.com/dotnet/api/system.datetime.now">DateTime.Now</a> is a <code>DateTime</code> value. Typical variable names are <code>dateTime</code>, <code>date</code>, <code>time</code>, or <code>dt</code>, but why not call it <code>now</code>? </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;TryCreate(<span style="color:#2b91af;">Restaurant</span>&nbsp;restaurant,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;scope&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TransactionScope</span>(<span style="color:#2b91af;">TransactionScopeAsyncFlowOption</span>.Enabled); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(restaurant.Id,&nbsp;reservation.At); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;now&nbsp;=&nbsp;Clock.GetCurrentDateTime(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!restaurant.MaitreD.WillAccept(now,&nbsp;reservations,&nbsp;reservation)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;NoTables500InternalServerError(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(restaurant.Id,&nbsp;reservation).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;scope.Complete(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Reservation201Created(restaurant.Id,&nbsp;reservation); }</pre> </p> <p> This is the <code>TryCreate</code> method called by the above <code>Post</code> method. Here, <code>DateTime.Now</code> is hidden behind <code>Clock.GetCurrentDateTime()</code> in order to make <a href="/2020/03/23/repeatable-execution">execution repeatable</a>, but the idea remains: the variable represents the current time or date, or, with a bit of good will, <code>now</code>. </p> <p> Notice that the <code>WillAccept</code> method (shown above) also uses <code>now</code> as a parameter name. That value's role is to represent <code>now</code> as a concept. </p> <p> When working with time, I also sometimes use the variable names <code>before</code> and <code>after</code>. This is mostly useful in integration tests: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;GetCurrentYear() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;api&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">LegacyApi</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;before&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;response&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.GetCurrentYear(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;after&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now; &nbsp;&nbsp;&nbsp;&nbsp;response.EnsureSuccessStatusCode(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;response.ParseJsonContent&lt;<span style="color:#2b91af;">CalendarDto</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;AssertOneOf(before.Year,&nbsp;after.Year,&nbsp;actual.Year); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Null(actual.Month); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Null(actual.Day); &nbsp;&nbsp;&nbsp;&nbsp;AssertLinks(actual); }</pre> </p> <p> While you can inject something like a <code>Clock</code> dependency in order to make your SUT deterministic, in integration tests you might want to see behaviour when using the system clock. You can often verify such behaviour by surrounding the test's <em>Act</em> phase with two calls to <code>DateTime.Now</code>. This gives you the time <code>before</code> and <code>after</code> the test exercised the SUT. </p> <p> When you do that, however, be careful with the assertions. If such a test runs at midnight, <code>before</code> and <code>after</code> might be two different dates. If it runs on midnight December 31, it might actually be two different years! That's the reason that the test passes as long as the <code>actual.Year</code> is either of <code>before.Year</code> and <code>after.Year</code>. </p> <h3 id="8183f4e53fdd4ceca701ef17a6509614"> Invalid values <a href="#8183f4e53fdd4ceca701ef17a6509614" title="permalink">#</a> </h3> <p> While integration tests often test happy paths, unit tests should also exercise error paths. What happens when you supply invalid input to a method? When you write such tests, you can identify the invalid values by naming the variables or parameters accordingly: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:blue;">null</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bas&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;PutInvalidId(<span style="color:blue;">string</span>&nbsp;invalidId) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;db&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SystemClock</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InMemoryRestaurantDatabase</span>(<span style="color:#2b91af;">Some</span>.Restaurant), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dummyDto&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2024-06-25&nbsp;18:19&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;colera@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Cole&nbsp;Aera&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Put(invalidId,&nbsp;dummyDto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">NotFoundResult</span>&gt;(actual); }</pre> </p> <p> Here, the invalid input represent an ID. To indicate that, I called the parameter <code>invalidId</code>. </p> <p> The system under test is the <code>Put</code> method, which takes two arguments: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Put(<span style="color:blue;">string</span>&nbsp;id,&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;dto)</pre> </p> <p> When testing an error path, it's important to keep other arguments well-behaved. In this example, I want to make sure that it's the <code>invalidId</code> that causes the <code>NotFoundResult</code> result. Thus, the <code>dto</code> argument should be as well-behaved as possible, so that it isn't going to be the source of divergence. </p> <p> Apart from being well-behaved, that object plays no role in the test. It just needs to be there to make the code compile. <em>xUnit Test Patterns</em> calls such an object a <em>Dummy Object</em>, so I named the variable <code>dummyDto</code> as information to any reader familiar with that pattern language. </p> <h3 id="441f47fab8b74787840c0237b3958316"> Derived class names <a href="#441f47fab8b74787840c0237b3958316" title="permalink">#</a> </h3> <p> The thrust of all of these examples is that you don't <em>have</em> to name variables after their types. You can extend this line of reasoning to class inheritance. Just because a base class is called <code>Foo</code> it doesn't mean that you <em>have</em> to call a derived class <code>SomethingFoo</code>. </p> <p> This is something of which I have to remind myself. For example, to support integration testing with ASP.NET you'll need a <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactory-1">WebApplicationFactory&lt;TEntryPoint&gt;</a>. To override the default DI Container configuration, you'll have to derive from this class and override its <code>ConfigureWebHost</code> method. In <a href="/2020/04/20/unit-bias-against-collections">an example I've previously published</a> I didn't spend much time thinking about the class name, so <code>RestaurantApiFactory</code> it was. </p> <p> At first, I named the variables of this type <code>factory</code>, or something equally devoid of information. That bothered me, so instead tried <code>service</code>, which I felt was an improvement, but still too vapid. I then adopted <code>api</code> as a variable name, but then realised that that also suggested a better class name. So currently, this defines my self-hosting API: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SelfHostedApi</span>&nbsp;:&nbsp;<span style="color:#2b91af;">WebApplicationFactory</span>&lt;<span style="color:#2b91af;">Startup</span>&gt;</pre> </p> <p> Here's how I use it: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;ReserveTableAtNono() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;api&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SelfHostedApi</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;client&nbsp;=&nbsp;api.CreateClient(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dto&nbsp;=&nbsp;<span style="color:#2b91af;">Some</span>.Reservation.ToDto(); &nbsp;&nbsp;&nbsp;&nbsp;dto.Quantity&nbsp;=&nbsp;6; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;response&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;client.PostReservation(<span style="color:#a31515;">&quot;Nono&quot;</span>,&nbsp;dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;at&nbsp;=&nbsp;<span style="color:#2b91af;">Some</span>.Reservation.At; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;AssertRemainingCapacity(client,&nbsp;at,&nbsp;<span style="color:#a31515;">&quot;Nono&quot;</span>,&nbsp;4); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;AssertRemainingCapacity(client,&nbsp;at,&nbsp;<span style="color:#a31515;">&quot;Hipgnosta&quot;</span>,&nbsp;10); }</pre> </p> <p> The variable is just called <code>api</code>, but the reader can tell from the initialisation that this is an instance of the <code>SelfHostedApi</code> class. I like how that communicates that this is an integration test that uses a self-hosted API. It literally says that. </p> <p> This test also uses the <code>dto</code> naming convention. Additionally, you may take note of the variable and property called <code>at</code>. That's another name for a date and time. I struggled with naming this value, until <a href="http://blog.strobaek.org">Karsten Strøbæk</a> suggested that I used the simple word <em>at:</em> <code>reservation.At</code> indicates the date and time of the reservation without being encumbered by awkward details about date and time. Should we call it <code>date</code>? <code>time</code>? <code>dateTime</code>? No, just call it <code>at</code>. I find it elegant. </p> <h3 id="6692436d37dd491fa9920a5f4ac63118"> Conclusion <a href="#6692436d37dd491fa9920a5f4ac63118" title="permalink">#</a> </h3> <p> Sometimes, a <code>Reservation</code> object is just a <code>reservation</code>, and that's okay. At other times, it's the <code>actual</code> value, or the <code>expected</code> value. If it represents an invalid reservation in a test case, it makes sense to call the variable <code>invalidResevation</code>. </p> <p> Giving variables descriptive names improves <a href="/2019/03/04/code-quality-is-not-software-quality">code quality</a>. You don't have to write <a href="http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode">comments as apologies for poor readability</a> if a better name communicates what the comment would have said. </p> <p> Consider naming variables (and classes) for the <em>roles</em> they play, rather than their types. </p> <p> On the other hand, <a href="/2016/10/25/when-variable-names-are-in-the-way">when variable names are in the way</a>, consider <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free code</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8d7dc043829244509b1a13c184f3cbbf"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#8d7dc043829244509b1a13c184f3cbbf">#</a></div> <div class="comment-content"> <p> Excellent name suggestions. Thanks for sharing them :) </p> <blockquote> I usually call the downcast variable <code>other</code> because, from the perspective of the instance, it's the other object. I usually use that convention whenever an instance interacts with another object of the same type. </blockquote> <p> The name <code>other</code> is good, but I prefer <code>that</code> because I think it is a better antonym of <code>this</code> (the keyword for the current instance) and because it has the same number of letters as <code>this</code>. </p> <blockquote> <p> It makes sense to me to call predicate input <em>candidates</em>. Typically, you have some input that you want to evaluate as either true or false. I think it makes sense to think of such a parameter as a 'truth candidate'. You can see one example of that in the above <code>SignatureIsValid</code> method. </p> <p>...</p> <p> Here, the reservation in question is actually not yet a reservation. It might be rejected, so it's a <code>candidate</code> reservation. </p> </blockquote> <p> I typically try to avoid turning some input into either true or false. In particular, I find it confusing for the syntax to say that some instance is a <code>Reservation</code> while the semantics says that it "is actually not yet a reservation". I think of this as an example of <a href="https://blog.ploeh.dk/2015/01/19/from-primitive-obsession-to-domain-modelling/">primitive obsession</a>. Strictly speaking, I think <a href="https://medium.com/the-sixt-india-blog/primitive-obsession-code-smell-that-hurt-people-the-most-5cbdd70496e9#5009:~:text=Primitive%20Obsession%20is%20when%20the%20code%20relies%20too%20much%20on%20primitives.">"Primitive Obsession is when the code relies too much on primitives."</a> (aka, on primitive types). In my mind though, I have generalized this to cover any code that relies too much on weaker types. Separate types <code>Reservation</code> and <code>bool</code> are weaker than separate types <code>Reservation</code> and <code>CandidateReservation</code>. I think Alexis King summarized this well with a blog post titled <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/">Parse, don’t validate</a>. </p> <p> And yet, my coworkers and I have engaged in friendly but serious debates for years about which of those two approaches is better. My argument, essentially as given above, is for separate types <code>Reservation</code> and <code>CandidateReservation</code>. The main counterargument is that these types are the same except for a database-generated ID, so just represent both using one type with an optional ID. </p> <p> Have you thought about this before? </p> <blockquote> <p> By naming the input parameter <code>dto</code>, I keep the name <code>reservation</code> free for the domain object, which ought to be the more important object of the two. </p> <p>...</p> <p> I could have named the input parameter <code>reservationDto</code> instead of <code>dto</code>, but that would diminish the 'mental distance' between <code>reservationDto</code> and <code>reservation</code>. I like to keep that distance, so that the roles are more explicit. </p> </blockquote> <p> I prefer to emphasize the roles even more by using the names <code>dto</code> and <code>model</code>. We are in the implementation of the (Post) route for <code>"restaurants/{restaurantId}/reservations"</code>, so I think it is clear from context that the <code>dto</code> and <code>model</code> are really a reservation DTO and a reservation model. </p> </div> <div class="comment-date">2020-11-30 20:46 UTC</div> </div> <div class="comment" id="931a54d51d3d445c9eaf6a41d8923406"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#931a54d51d3d445c9eaf6a41d8923406">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Certainly, I didn't intent my article to dictate names. As you imply, there's room for both creativity and subjectivity, and that's fine. My suggestions were meant only for inspiration. <blockquote> <p> The main counterargument is that these types are the same except for a database-generated ID, so just represent both using one type with an optional ID. </p> <p> Have you thought about this before? </p> </blockquote> Yes; I would <a href="/2014/08/11/cqs-versus-server-generated-ids">think twice before deciding to model a domain type with a database-generated ID</a>. A server-generated ID is an implementation detail that shouldn't escape the data access layer. If it does, you have a leaky abstraction at hand. Sooner or later, it's going to bite you. </p> <p> The <code>Reservation</code> class in the above examples has this sole constructor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(<span style="color:#2b91af;">Guid</span>&nbsp;id,&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;at,&nbsp;<span style="color:#2b91af;">Email</span>&nbsp;email,&nbsp;<span style="color:#2b91af;">Name</span>&nbsp;name,&nbsp;<span style="color:blue;">int</span>&nbsp;quantity)</pre> </p> <p> You can't create an instance without supplying an ID. On the other hand, any code can conjure up a GUID, so no server is required. At the type-level, there's no compelling reason to distinguish between a reservation and a candidate reservation. </p> <p> Granted, you <em>could</em> define two types, <code>Reservation</code> and <code>CandidateReservation</code>, but they'd be isomorphic. In Haskell, you'd probably use a <code>newtype</code> for one of these types, and then you're <a href="https://lexi-lambda.github.io/blog/2020/11/01/names-are-not-type-safety">back at Alexis King's blog</a>. </p> </div> <div class="comment-date">2020-12-02 7:43 UTC</div> </div> <div class="comment" id="fe3ac3a60c754340801b877666a07b65"> <div class="comment-author"><a href="https://ttulka.com">Tomas Tulka</a> <a href="#fe3ac3a60c754340801b877666a07b65">#</a></div> <div class="comment-content"> <blockquote>...naming is one of the hardest problems in software development. Perhaps it's hard because you have to do it so frequently.</blockquote> <p>Usually, doing things frequently means mastering them pretty quickly. Not so for naming. I guess, there are multiple issues:</p> <ol> <li>Words are ambiguous. The key is, not to do naming in isolation, the context matters. For example, it's difficult to come up with a good name for a method when we don't have a good name for its class, the whole component, etc. Similar with Clean Code's N5: the meaning of a short variable is clear in a small scope, closed context.</li> <li>Good naming requires deep understanding of the domain. Developers are usualy not good at the business they model. Sadly, it often means "necessary evil" for them.</li> </ol> <p>Naming variables by their roles is a great idea!</p> <p>Many thanks for another awesome post, I enjoyed reading it.</p> </div> <div class="comment-date">2020-12-11 09:11 UTC</div> </div> <div class="comment" id="e9299b251c8e4b7f9a168ff571f70950"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e9299b251c8e4b7f9a168ff571f70950">#</a></div> <div class="comment-content"> <p> Tomas, thank you for writing. <blockquote> <p> doing things frequently means mastering them pretty quickly. Not so for naming. I guess </p> </blockquote> Good point; I hadn't thought about that. I think that the reasons you list are valid. </p> <p> As an additional observation, it may be that there's a connection to the notion of <em>deliberate practice</em>. As the catch-phrase about professional experience puts it, there's a difference between 20 years of experience and one year of experience repeated 20 times. </p> <p> Doing a thing again and again generates little improvement if one does it by rote. One has to deliberately practice. In this case, it implies that a programmer should explicitly reflect on variable names, and consider more than one option. </p> <p> I haven't met many software developers who do that. </p> </div> <div class="comment-date">2020-12-15 9:19 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Good names are skin-deep https://blog.ploeh.dk/2020/11/23/good-names-are-skin-deep 2020-11-23T06:33:00+00:00 Mark Seemann <div id="post"> <p> <em>Good names are important, but insufficient, for code maintainability.</em> </p> <p> You should give the building blocks of your code bases descriptive names. It's easier to understand the purpose of a library, module, class, method, function, etcetera if the name contains a clue about the artefact's purpose. This is hardly controversial, and while naming is hard, most teams I visit agree that names are important. </p> <p> Still, despite good intentions and efforts to name things well, code bases deteriorate into unmaintainable clutter. </p> <p> Clearly, good names aren't enough. </p> <h3 id="5a20f600298844e98c89c423f5c66c5f"> Tenuousness of names <a href="#5a20f600298844e98c89c423f5c66c5f" title="permalink">#</a> </h3> <p> A good name is tenuous. First, naming is hard, so while you may have spent some effort coming up with a good name, other people may misinterpret it. Because they originate from natural language, names are as ambiguous as language. (<a href="/2018/07/02/terse-operators-make-business-code-more-readable">Terse operators, on the other hand...</a>) </p> <p> Another maintainability problem with names is that implementation may change over time, but the names remain constant. Granted, modern IDEs make it easy to rename methods, but developers rarely adjust names when they adjust behaviour. Even the best names may become misleading over time. </p> <p> These weakness aren't the worst, though. In my experience, a more fundamental problem is that all it takes is one badly named 'wrapper object' before the information in a good name is lost. </p> <p> <img src="/content/binary/vague-names-hiding-clear-names.png" alt="Object with clear names enclosed in object with vague names."> </p> <p> In the figure, the inner object is well-named. It has a clear name and descriptive method names. All it takes before this information is lost, however, is another object with vague names to 'encapsulate' it. </p> <h3 id="c0cfcf2d16a94e4c96c33c9d0359846f"> An attempt at a descriptive method name <a href="#c0cfcf2d16a94e4c96c33c9d0359846f" title="permalink">#</a> </h3> <p> Here's an example. Imagine an online restaurant reservation system. One of the features of this system is to take reservations and save them in the database. </p> <p> A restaurant, however, is a finite resource. It can only accommodate a certain number of guests at the same time. Whenever the system receives a reservation request, it'll have to retrieve the existing reservations for that time and make a decision. <a href="/2020/01/27/the-maitre-d-kata">Can it accept the reservation?</a> Only if it can should it save the reservation. </p> <p> How do you model such an interaction? How about a descriptive name? How about <code>TrySave</code>? Here's a possible implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;TrySave(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(reservation)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.At, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.At&nbsp;+&nbsp;SeatingDuration) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;availableTables&nbsp;=&nbsp;Allocate(reservations); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!availableTables.Any(t&nbsp;=&gt;&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;t.Seats)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(reservation).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> There's an implicit naming convention in .NET that methods with the <code>Try</code> prefix indicate an operation that may or may not succeed. The return value of such methods is either <code>true</code> or <code>false</code>, and they may also have <code>out</code> parameters if they optionally produce a value. That's not the case here, but I think one could make the case that <code>TrySave</code> succinctly describes what's going on. </p> <p> All is good, then? </p> <h3 id="bbf9fd5509ba4a2c823483acd40fbe22"> A vague wrapper <a href="#bbf9fd5509ba4a2c823483acd40fbe22" title="permalink">#</a> </h3> <p> After our conscientious programmer meticulously designed and named the above <code>TrySave</code> method, it turns out that it doesn't meet all requirements. Users of the system file a bug: the system accepts reservations outside the restaurant's opening hours. </p> <p> The original programmer has moved on to greener pastures, so fixing the bug falls on a poor maintenance developer with too much to do. Having recently learned about the <a href="https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle">open-closed principle</a>, our new protagonist decides to wrap the existing <code>TrySave</code> in a new method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;Check(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservation&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(reservation)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservation.At&nbsp;&lt;&nbsp;<span style="color:#2b91af;">DateTime</span>.Now) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservation.At.TimeOfDay&nbsp;&lt;&nbsp;OpensAt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(LastSeating&nbsp;&lt;&nbsp;reservation.At.TimeOfDay) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Manager.TrySave(reservation).ConfigureAwait(<span style="color:blue;">false</span>); }</pre> </p> <p> This new method first checks whether the <code>reservation</code> is within opening hours and in the future. If that's not the case, it returns <code>false</code>. Only if these preconditions are fulfilled does it delegate the decision to that <code>TrySave</code> method. </p> <p> Notice, however, the name. The bug was urgent, and our poor programmer didn't have time to think of a good name, so <code>Check</code> it is. </p> <h3 id="6056d14ac9ab4046b4aa417d3902fbf1"> Caller's perspective <a href="#6056d14ac9ab4046b4aa417d3902fbf1" title="permalink">#</a> </h3> <p> How does this look from the perspective of calling code? Here's the Controller action that handles the pertinent HTTP request: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">ReservationDto</span>&nbsp;dto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dto&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(dto)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>?&nbsp;r&nbsp;=&nbsp;dto.Validate(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(r&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BadRequestResult</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;isOk&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Manager.Check(r).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!isOk) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StatusCodeResult</span>(<span style="color:#2b91af;">StatusCodes</span>.Status500InternalServerError); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NoContentResult</span>(); }</pre> </p> <p> Try to forget the code you've just seen and imagine that you're looking at this code first. You'd be excused if you miss what's going on. It looks as though the method just does a bit of validation and then <em>checks</em> 'something' concerning the reservation. </p> <p> There's no hint that the <code>Check</code> method might perform the significant side effect of saving the reservation in the database. </p> <p> You'll only learn that if you <em>read</em> the implementation details of <code>Check</code>. As I argue in my <a href="https://cleancoders.com/episode/humane-code-real-episode-1">Humane Code video</a>, <em>if you have to read the source code of an object, encapsulation is broken.</em> </p> <p> Such code doesn't fit in your brain. You'll struggle as you try keep track of all the things that are going on in the code, all the way from the outer boundary of the application to implementation details that relate to databases, third-party services, etcetera. </p> <h3 id="59249ae122b540ca907549ade3eca649"> Straw man? <a href="#59249ae122b540ca907549ade3eca649" title="permalink">#</a> </h3> <p> You may think that this is a straw man argument. After all, wouldn't it be better to edit the original <code>TrySave</code> method? </p> <p> Perhaps, but it would make that class more complex. The <code>TrySave</code> method has a <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of only <em>3</em>, while the <code>Check</code> method has a complexity of <em>5</em>. Combining them might easily take them over some <a href="/2020/04/13/curb-code-rot-with-thresholds">threshold</a>. </p> <p> Additionally, each of these two classes have different dependencies. As the <code>TrySave</code> method implies, it relies on both <code>Repository</code> and <code>SeatingDuration</code>, and the <code>Allocate</code> helper method (not shown) uses a third dependency: the restaurant's table configuration. </p> <p> Likewise, the <code>Check</code> method relies on <code>OpensAt</code> and <code>LastSeating</code>. If you find it better to edit the original <code>TrySave</code> method, you'd have to combine these dependencies as well. Each time you do that, the class grows until it becomes a <a href="https://en.wikipedia.org/wiki/God_object">God object</a>. </p> <p> It's rational to attempt to separate things in multiple classes. It also, on the surface, seems to make unit testing easier. For example, here's a test that verifies that the <code>Check</code> method rejects reservations before the restaurant's opening time: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;RejectReservationBeforeOpeningTime() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>.Now.AddDays(10).Date.AddHours(17), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;colaera@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Cole&nbsp;Aera&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;mgrTD&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IReservationsManager</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;mgrTD.Setup(mgr&nbsp;=&gt;&nbsp;mgr.TrySave(r)).ReturnsAsync(<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RestaurantManager</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(18), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(21), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mgrTD.Object); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Check(r); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(actual); }</pre> </p> <p> By replacing the <code>TrySave</code> method by a test double, you've ostensibly decoupled the <code>Check</code> method from all the complexity of the <code>TrySave</code> method. </p> <p> To be clear, this style of programming, with lots of nested interfaces and tests with <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">mocks and stubs</a> is far from ideal, but I still find it better than a <a href="https://en.wikipedia.org/wiki/Big_ball_of_mud">big ball of mud</a>. </p> <h3 id="33cb10390fa4417f96b24e4b9102d4ed"> Alternative <a href="#33cb10390fa4417f96b24e4b9102d4ed" title="permalink">#</a> </h3> <p> A better alternative is <a href="https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell">Functional Core, Imperative Shell</a>, AKA <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>. Move all impure actions to the edge of the system, leaving only <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a> functions as the main implementers of logic. It could look like this: </p> <p> <pre>[<span style="color:#2b91af;">HttpPost</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">ReservationDto</span>&nbsp;dto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dto&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(dto)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;id&nbsp;=&nbsp;dto.ParseId()&nbsp;??&nbsp;<span style="color:#2b91af;">Guid</span>.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>?&nbsp;r&nbsp;=&nbsp;dto.Validate(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(r&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BadRequestResult</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(r.At).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!MaitreD.WillAccept(<span style="color:#2b91af;">DateTime</span>.Now,&nbsp;reservations,&nbsp;r)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;NoTables500InternalServerError(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(r).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Reservation201Created(r); }</pre> </p> <p> Nothing is swept under the rug here. <code>WillAccept</code> is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, and while it encapsulates significant complexity, the only thing you need to understand when you're trying to understand the above <code>Post</code> code is that it returns either <code>true</code> or <code>false</code>. </p> <p> Another advantage of pure functions is that they are <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. That makes unit testing and test-driven development easier. </p> <p> Even with a functional core, you'll also have an imperative shell. You can still test that, too, such as the <code>Post</code> method. It isn't referentially transparent, so you might be inclined to use mocks and stubs, but I instead recommend <a href="/2019/02/18/from-interaction-based-to-state-based-testing">state-based testing with a Fake database</a>. </p> <h3 id="6758361ac712400aae148a7dcc2a4a70"> Conclusion <a href="#6758361ac712400aae148a7dcc2a4a70" title="permalink">#</a> </h3> <p> Good names are important, but don't let good names, alone, lull you into a false sense of security. All it takes is one vaguely named wrapper object, and all the information in your meticulously named methods is lost. </p> <p> This is one of many reasons I try to design with static types instead of names. Not that I dismiss the value of good names. After all, you'll have to give your types good names as well. </p> <p> Types are more robust in the face of inadvertent changes; or, rather, they tend to resist when we try to do something stupid. I suppose that's what lovers of dynamically typed languages feel as 'friction'. In my mind, it's entirely opposite. Types keep me honest. </p> <p> Unfortunately, most type systems don't offer an adequate degree of safety. Even in <a href="https://fsharp.org">F#</a>, which has a great type system, you can introduce impure actions into what you thought was a pure function, and <a href="/2020/02/24/discerning-and-maintaining-purity">you'd be none the wiser</a>. That's one of the reasons I find <a href="https://www.haskell.org">Haskell</a> so interesting. Because of <a href="/2020/06/08/the-io-container">the way IO works</a>, you can't inadvertently sweep surprises under the rug. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9ecb24dc5f78413687547c7f74f2d8b9"> <div class="comment-author">Johannes Schmitt <a href="#9ecb24dc5f78413687547c7f74f2d8b9">#</a></div> <div class="comment-content"> <p> I find the idea of the impure/pure/impure sandwich rather interesting and I agree with the benefits that it yields. However, I was wondering about where to move synchronization logic, i.e. the reservation system should avoid double bookings. With the initial TrySave approach it would be clear for me where to put this logic: the synchonrization mechanism should be part of the TrySave method. With the impure/pure/impure sandwich, it will move out to the most outer layern (HTTP Controller) - at least this is how I'd see it. My feelings tells me that this is a bit smelly, but I can't really pin point why I think so. Can you give some advice on this? How would you solve that? </p> </div> <div class="comment-date">2020-12-12 19:08 UTC</div> </div> <div class="comment" id="ebce3b718bc84329b6979bcacf6c2573"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ebce3b718bc84329b6979bcacf6c2573">#</a></div> <div class="comment-content"> <p> Johannes, thank you for writing. There are several ways to address that issue, depending on what sort of trade-off you're looking for. There's always a trade-off. </p> <p> You can address the issue with a lock-free architecture. This typically involves expressing the desired action as a Command and putting it on a durable queue. If you combine that with a single-threaded, single-instance Actor that pulls Commands off the queue, you need no further transaction processing, because the architecture itself serialises writes. You can find plenty of examples of such an architecture on the internet, including (IIRC) my Pluralsight course <a href="/functional-architecture-with-fsharp">Functional Architecture with F#</a>. </p> <p> Another option is to simply surround the <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a> with a <a href="https://docs.microsoft.com/dotnet/api/system.transactions.transactionscope">TransactionScope</a> (if you're on .NET, that is). </p> </div> <div class="comment-date">2020-12-16 16:59 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Redirect legacy URLs https://blog.ploeh.dk/2020/11/16/redirect-legacy-urls 2020-11-16T06:47:00+00:00 Mark Seemann <div id="post"> <p> <em>Evolving REST API URLs when cool URIs don't change.</em> </p> <p> More than one reader reacted to my article on <a href="/2020/10/26/fit-urls">fit URLs</a> by asking about bookmarks and original URLs. <a href="/2020/10/26/fit-urls#b0fb43ff9aba4c14a075e7effc9fae25">Daniel Sklenitzka's question</a> is a good example: <blockquote> <p> "I see how signing the URLs prevents clients from retro-engineering the URL templates, but how does it help preventing breaking changes? If the client stores the whole URL instead of just the ID and later the URL changes because a restaurant ID is added, the original URL is still broken, isn't it?" </p> </blockquote> While I answered the question on the same page, I think that it's worthwhile to expand it. </p> <h3 id="3e1e7cd6b79e4e7b964a96a527312ab0"> The rules of HTTP <a href="#3e1e7cd6b79e4e7b964a96a527312ab0" title="permalink">#</a> </h3> <p> I agree with the implicit assumption that clients are allowed to bookmark links. It seems, then, like a breaking change if you later change your internal URL scheme. That seems to imply that the bookmarked URL is gone, breaking a tenet of the HTTP protocol: <a href="https://www.w3.org/Provider/Style/URI">Cool URIs don't change</a>. </p> <p> REST APIs are supposed to play by the rules of HTTP, so it'd seem that once you've published a URL, you can never retire it. You can, on the other hand, change its behaviour. </p> <p> Let's call such URLs <em>legacy URLs</em>. Keep them around, but change them to return <code>301 Moved Permanently</code> responses. </p> <p> The rules of REST go both ways. The API is expected to play by the rules of HTTP, and so are the clients. Clients are not only expected to follow links, but also redirects. If a legacy URL starts returning a <code>301 Moved Permanently</code> response, a well-behaved client doesn't break. </p> <h3 id="96913fb84fb344ccae75e1e2542ed51c"> Reverse proxy <a href="#96913fb84fb344ccae75e1e2542ed51c" title="permalink">#</a> </h3> <p> As I've <a href="/2020/06/01/retiring-old-service-versions#bc73620aa71141b6a74f4a4aaf395d75">previously described</a>, one of the many benefits of HTTP-based services is that you can put a <a href="https://en.wikipedia.org/wiki/Reverse_proxy">reverse proxy</a> in front of your application servers. I've no idea how to configure or operate <a href="https://en.wikipedia.org/wiki/Nginx">NGINX</a> or <a href="https://en.wikipedia.org/wiki/Varnish_(software)">Varnish</a>, but from talking to people who do know, I get the impression that they're quite scriptable. </p> <p> Since the above ideas are independent of actual service implementation or behaviour, it's a generic problem that you should seek to address with general-purpose software. </p> <p> <img src="/content/binary/reverse-proxy-based-redirect.png" alt="Sequence diagram showing a reverse proxy returning a redirect response to a request for a legacy URL."> </p> <p> Imagine that a reverse proxy is configured with a set of rules that detects legacy URLs and knows how to forward them. Clearly, the reverse proxy must know of the REST API's current URL scheme to be able to do that. You might think that this would entail leaking an implementation detail, but just as I consider any database used by the API as part of the overall system, I'd consider the reverse proxy as just another part. </p> <h3 id="410c6db0f3a54e969c1f54aa9f1ce59e"> Redirecting with ASP.NET <a href="#410c6db0f3a54e969c1f54aa9f1ce59e" title="permalink">#</a> </h3> <p> If you don't have a reverse proxy, you can also implement redirects in code. It'd be better to use something like a reverse proxy, because that would mean that you get to delete code from your code base, but sometimes that's not possible. </p> <p> <ins datetime="2021-07-15T08:02Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <p> In ASP.NET, you can return <code>301 Moved Permanently</code> responses just like any other kind of HTTP response: </p> <p> <pre>[<span style="color:#2b91af;">Obsolete</span>(<span style="color:#a31515;">&quot;Use&nbsp;Get&nbsp;method&nbsp;with&nbsp;restaurant&nbsp;ID.&quot;</span>)] [<span style="color:#2b91af;">HttpGet</span>(<span style="color:#a31515;">&quot;calendar/{year}/{month}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;LegacyGet(<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RedirectToActionResult</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(Get), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">null</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;restaurantId&nbsp;=&nbsp;<span style="color:#2b91af;">Grandfather</span>.Id,&nbsp;year,&nbsp;month&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;permanent:&nbsp;<span style="color:blue;">true</span>); }</pre> </p> <p> This <code>LegacyGet</code> method redirects to the current Controller action called <code>Get</code> by supplying the arguments that the new method requires. The <code>Get</code> method has this signature: </p> <p> <pre>[<span style="color:#2b91af;">HttpGet</span>(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/calendar/{year}/{month}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Get(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month)</pre> </p> <p> When I expanded the API from a single restaurant to a multi-tenant system, I had to <a href="https://en.wikipedia.org/wiki/Grandfather_clause">grandfather in</a> the original restaurant. I gave it a <code>restaurantId</code>, but in order to not put <a href="https://en.wikipedia.org/wiki/Magic_number_(programming)">magic constants</a> in the code, I defined it as the named constant <code>Grandfather.Id</code>. </p> <p> Notice that I also adorned the <code>LegacyGet</code> method with an <code>[Obsolete]</code> attribute to make it clear to maintenance programmers that this is legacy code. You might argue that the <em>Legacy</em> prefix already does that, but the <code>[Obsolete]</code> attribute will make the compiler emit a warning, which is <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">even better feedback</a>. </p> <h3 id="69aad25084a5452b96e14bc81a28a53c"> Regression test <a href="#69aad25084a5452b96e14bc81a28a53c" title="permalink">#</a> </h3> <p> While legacy URLs may be just that: legacy, that doesn't mean that it doesn't matter whether or not they work. You may want to add regression tests. </p> <p> If you implement redirects in code (as opposed to a reverse proxy), you should also add automated tests that verify that the redirects work: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;http://localhost/calendar/2020?sig=ePBoUg5gDw2RKMVWz8KIVzF%2Fgq74RL6ynECiPpDwVks%3D&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;http://localhost/calendar/2020/9?sig=ZgxaZqg5ubDp0Z7IUx4dkqTzS%2Fyjv6veDUc2swdysDU%3D&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;BookmarksStillWork(<span style="color:blue;">string</span>&nbsp;bookmarkedAddress) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;api&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">LegacyApi</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateDefaultClient().GetAsync(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(bookmarkedAddress)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#2b91af;">HttpStatusCode</span>.MovedPermanently,&nbsp;actual.StatusCode); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;follow&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;api.CreateClient().GetAsync(actual.Headers.Location); &nbsp;&nbsp;&nbsp;&nbsp;follow.EnsureSuccessStatusCode(); }</pre> </p> <p> This test interacts with a self-hosted service at the HTTP level. <code>LegacyApi</code> is a test-specific helper class that derives from <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactory-1">WebApplicationFactory&lt;Startup&gt;</a>. </p> <p> The test uses URLs that I 'bookmarked' before I evolved the URLs to a multi-tenant system. As you can tell from the host name (<code>localhost</code>), these are bookmarks against the self-hosted service. The test first verifies that the response is <code>301 Moved Permanently</code>. It then requests the new address and <a href="/2020/09/28/ensuresuccessstatuscode-as-an-assertion">uses EnsureSuccessStatusCode as an assertion</a>. </p> <h3 id="f16f3cd819804e54add83ad32aaff188"> Conclusion <a href="#f16f3cd819804e54add83ad32aaff188" title="permalink">#</a> </h3> <p> When you evolve fit URLs, it could break clients that may have bookmarked legacy URLs. Consider leaving <code>301 Moved Permanently</code> responses at those addresses. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Checking signed URLs with ASP.NET https://blog.ploeh.dk/2020/11/09/checking-signed-urls-with-aspnet 2020-11-09T12:19:00+00:00 Mark Seemann <div id="post"> <p> <em>Use a filter to check all requested URL signatures.</em> </p> <p> This article is part of <a href="/2020/10/26/fit-urls">a short series on fit URLs</a>. In the overview article, I argued that you should be signing URLs in order to prevent your REST APIs from becoming victims of <a href="https://www.hyrumslaw.com">Hyrum's law</a>. <a href="/2020/11/02/signing-urls-with-aspnet">In the previous article</a> you saw how to sign URLs with ASP.NET. </p> <p> In this article you'll see how to check the URLs of all HTTP requests to the API and reject those that aren't up to snuff. </p> <p> <ins datetime="2021-07-16T07:08Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="beb942e872e6470e94777e228967b58a"> Filter <a href="#beb942e872e6470e94777e228967b58a" title="permalink">#</a> </h3> <p> If you want to intercept all incoming HTTP requests in ASP.NET Core, an <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.filters.iasyncactionfilter">IAsyncActionFilter</a> is a good option. This one should look at the URL of all incoming HTTP requests and detect if the client tried to tamper with it. </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">UrlIntegrityFilter</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IAsyncActionFilter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;urlSigningKey; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">UrlIntegrityFilter</span>(<span style="color:blue;">byte</span>[]&nbsp;urlSigningKey) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.urlSigningKey&nbsp;=&nbsp;urlSigningKey; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;code&nbsp;comes&nbsp;here...</span></pre> </p> <p> The interface only defines a single method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;OnActionExecutionAsync( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ActionExecutingContext</span>&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ActionExecutionDelegate</span>&nbsp;next) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsGetHomeRequest(context)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;next().ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;strippedUrl&nbsp;=&nbsp;GetUrlWithoutSignature(context); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(SignatureIsValid(strippedUrl,&nbsp;context)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;next().ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;context.Result&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotFoundResult</span>(); }</pre> </p> <p> While the rule is to reject requests with invalid signatures, there's one exception. The 'home' resource requires no signature, as this is the only publicly documented URL for the API. Thus, if <code>IsGetHomeRequest</code> returns <code>true</code>, the filter invokes the <code>next</code> delegate and returns. </p> <p> Otherwise, it strips the signature off the URL and checks if the signature is valid. If it is, it again invokes the <code>next</code> delegate and returns. </p> <p> If the signature is invalid, on the other hand, the filter stops further execution by <em>not</em> invoking <code>next</code>. Instead, it sets the response to a <code>404 Not Found</code> result. </p> <p> It may seem odd to return <code>404 Not Found</code> if the signature is invalid. Wouldn't <code>401 Unauthorized</code> or <code>403 Forbidden</code> be more appropriate? </p> <p> Not really. Keep in mind that while this behaviour may use encryption technology, it's not a security feature. The purpose is to make it impossible for clients to 'retro-engineer' an implied interface. This protects them from breaking changes in the future. Clients are supposed to follow links, and the URLs given by the API itself are proper, existing URLs. If you try to edit a URL, then that URL doesn't work. It represents a resource that doesn't exist. While it may seem surprising at first, I find that a <code>404 Not Found</code> result is the most appropriate status code to return. </p> <h3 id="3584198282b64c85ab5872493da8149d"> Detecting a home request <a href="#3584198282b64c85ab5872493da8149d" title="permalink">#</a> </h3> <p> The <code>IsGetHomeRequest</code> helper method is straightforward: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsGetHomeRequest(<span style="color:#2b91af;">ActionExecutingContext</span>&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;context.HttpContext.Request.Path&nbsp;==&nbsp;<span style="color:#a31515;">&quot;/&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;context.HttpContext.Request.Method&nbsp;==&nbsp;<span style="color:#a31515;">&quot;GET&quot;</span>; }</pre> </p> <p> This predicate only looks at the <code>Path</code> and <code>Method</code> of the incoming request. Perhaps it also ought to check that the URL has no query string parameters, but I'm not sure if that actually matters. </p> <h3 id="c5f674b301404a2f95177d4c1dc73cc0"> Stripping off the signature <a href="#c5f674b301404a2f95177d4c1dc73cc0" title="permalink">#</a> </h3> <p> The <code>GetUrlWithoutSignature</code> method strips off the signature query string variable from the URL, but leaves everything else intact: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;GetUrlWithoutSignature(<span style="color:#2b91af;">ActionExecutingContext</span>&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;restOfQuery&nbsp;=&nbsp;<span style="color:#2b91af;">QueryString</span>.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context.HttpContext.Request.Query.Where(x&nbsp;=&gt;&nbsp;x.Key&nbsp;!=&nbsp;<span style="color:#a31515;">&quot;sig&quot;</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;url&nbsp;=&nbsp;context.HttpContext.Request.GetEncodedUrl(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ub&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UriBuilder</span>(url); &nbsp;&nbsp;&nbsp;&nbsp;ub.Query&nbsp;=&nbsp;restOfQuery.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ub.Uri.AbsoluteUri; }</pre> </p> <p> The purpose of removing only the <code>sig</code> query string parameter is that it restores the rest of the URL to the value that it had when it was signed. This enables the <code>SignatureIsValid</code> method to recalculate the <a href="https://en.wikipedia.org/wiki/HMAC">HMAC</a>. </p> <h3 id="4fa6c27d6b504f2baf4b27b2f431851a"> Validating the signature <a href="#4fa6c27d6b504f2baf4b27b2f431851a" title="permalink">#</a> </h3> <p> The <code>SignatureIsValid</code> method validates the signature: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;SignatureIsValid(<span style="color:blue;">string</span>&nbsp;candidate,&nbsp;<span style="color:#2b91af;">ActionExecutingContext</span>&nbsp;context) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sig&nbsp;=&nbsp;context.HttpContext.Request.Query[<span style="color:#a31515;">&quot;sig&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;receivedSignature&nbsp;=&nbsp;<span style="color:#2b91af;">Convert</span>.FromBase64String(sig.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;hmac&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HMACSHA256</span>(urlSigningKey); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;computedSignature&nbsp;=&nbsp;hmac.ComputeHash(<span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;signaturesMatch&nbsp;=&nbsp;computedSignature.SequenceEqual(receivedSignature); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;signaturesMatch; }</pre> </p> <p> If the <code>receivedSignature</code> equals the <code>computedSignature</code> the signature is valid. </p> <p> This prevents clients from creating URLs based on implied templates. Since clients don't have the signing key, they can't compute a valid HMAC, and therefore the URLs they'll produce will fail the integrity test. </p> <h3 id="8c8d2409a9a647b6a4d9c2970a02a9c7"> Configuration <a href="#8c8d2409a9a647b6a4d9c2970a02a9c7" title="permalink">#</a> </h3> <p> As is the case for the URL-signing feature, you'll first need to read the signing key from the configuration system. This is the same key used to sign URLs: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;urlSigningKey&nbsp;=&nbsp;<span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes( &nbsp;&nbsp;&nbsp;&nbsp;Configuration.GetValue&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;UrlSigningKey&quot;</span>));</pre> </p> <p> Next, you'll need to register the filter with the ASP.NET framework: </p> <p> <pre>services.AddControllers(opts&nbsp;=&gt;&nbsp;opts.Filters.Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UrlIntegrityFilter</span>(urlSigningKey)));</pre> </p> <p> This is typically done in the <code>ConfigureServices</code> method of the <code>Startup</code> class. </p> <h3 id="e032ab8e735b4e54a733ded5d086bee8"> Conclusion <a href="#e032ab8e735b4e54a733ded5d086bee8" title="permalink">#</a> </h3> <p> With a filter like <code>UrlIntegrityFilter</code> you can check the integrity of URLs on all incoming requests to your REST API. This prevents clients from making up URLs based on an implied interface. This may seem restrictive, but is actually for their own benefit. When they can't assemble URLs from scratch, the only remaining option is to follow the links that the API provides. </p> <p> This enables you to evolve the API without breaking existing clients. While client developers may not initially appreciate having to follow links instead of building URLs out of templates, they may value that their clients don't break as you evolve the API. </p> <p> <strong>Next:</strong> <a href="/2020/11/16/redirect-legacy-urls">Redirect legacy URLs</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Signing URLs with ASP.NET https://blog.ploeh.dk/2020/11/02/signing-urls-with-aspnet 2020-11-02T08:20:00+00:00 Mark Seemann <div id="post"> <p> <em>A few Decorators is all it takes.</em> </p> <p> This article is part of <a href="/2020/10/26/fit-urls">a short series on fit URLs</a>. In the overview article, I argued that you should be signing URLs in order to prevent your REST APIs from becoming victims of <a href="https://www.hyrumslaw.com">Hyrum's law</a>. </p> <p> In this article, you'll see how to do this with ASP.NET Core 3.1, and in the next article you'll see how to check URL integrity. </p> <p> <ins datetime="2021-07-17T07:46Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="8d83a9fd4dec48efa79455d6f3356726"> SigningUrlHelper <a href="#8d83a9fd4dec48efa79455d6f3356726" title="permalink">#</a> </h3> <p> I wanted the URL-signing functionality to slot into the ASP.NET framework, which supplies the <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.iurlhelper">IUrlHelper</a> interface for the purpose of creating URLs. (For example, the <a href="/2020/08/10/an-aspnet-core-url-builder">UrlBuilder I recently described</a> relies on that interface.) </p> <p> Since it's an interface, you can define a <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> around it: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SigningUrlHelper</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IUrlHelper</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IUrlHelper</span>&nbsp;inner; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;urlSigningKey; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">SigningUrlHelper</span>(<span style="color:#2b91af;">IUrlHelper</span>&nbsp;inner,&nbsp;<span style="color:blue;">byte</span>[]&nbsp;urlSigningKey) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.inner&nbsp;=&nbsp;inner; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.urlSigningKey&nbsp;=&nbsp;urlSigningKey; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;code&nbsp;comes&nbsp;here...</span></pre> </p> <p> As you can tell, this Decorator requires an <code>inner</code> <code>IUrlHelper</code> and a <code>urlSigningKey</code>. Most of the members just delegate to the <code>inner</code> implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsLocalUrl(<span style="color:blue;">string</span>&nbsp;url) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;inner.IsLocalUrl(url); }</pre> </p> <p> The <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.iurlhelper.action">Action</a> method creates URLs, so this is the method to modify: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Action(<span style="color:#2b91af;">UrlActionContext</span>&nbsp;actionContext) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;url&nbsp;=&nbsp;inner.Action(actionContext); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsLocalUrl(url)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;b&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UriBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ActionContext.HttpContext.Request.Scheme, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ActionContext.HttpContext.Request.Host.ToUriComponent()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(b.Uri,&nbsp;url).AbsoluteUri; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ub&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UriBuilder</span>(url); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;<span style="color:blue;">var</span>&nbsp;hmac&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HMACSHA256</span>(urlSigningKey); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sig&nbsp;=&nbsp;<span style="color:#2b91af;">Convert</span>.ToBase64String( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hmac.ComputeHash(<span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes(url))); &nbsp;&nbsp;&nbsp;&nbsp;ub.Query&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">QueryString</span>(ub.Query).Add(<span style="color:#a31515;">&quot;sig&quot;</span>,&nbsp;sig).ToString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ub.ToString(); }</pre> </p> <p> The <code>actionContext</code> may sometimes indicate a local (relative) URL, in which case I wanted to convert it to an absolute URL. Once that's taken care of, the method calculates an <a href="https://en.wikipedia.org/wiki/HMAC">HMAC</a> and adds it as a query string variable. </p> <h3 id="37abf2a288444c9cab0541f6acafda8b"> SigningUrlHelperFactory <a href="#37abf2a288444c9cab0541f6acafda8b" title="permalink">#</a> </h3> <p> While it's possible take ASP.NET's default <code>IUrlHelper</code> instance (e.g. from <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.url">ControllerBase.Url</a>) and manually decorate it with <code>SigningUrlHelper</code>, that doesn't slot seamlessly into the framework. </p> <p> For example, to add the <code>Location</code> header that you saw in <a href="/2020/10/26/fit-urls">the previous article</a>, the code is this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;Reservation201Created( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;r) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CreatedAtActionResult</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(Get), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">null</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;restaurantId,&nbsp;id&nbsp;=&nbsp;r.Id.ToString(<span style="color:#a31515;">&quot;N&quot;</span>)&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.ToDto()); }</pre> </p> <p> The method just returns a new <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.createdatactionresult">CreatedAtActionResult</a> object, and the framework takes care of the rest. No explicit <code>IUrlHelper</code> object is used, so there's nothing to manually decorate. By default, then, the URLs created from such <code>CreatedAtActionResult</code> objects aren't signed. </p> <p> It turns out that the ASP.NET framework uses an interface called <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.routing.iurlhelperfactory">IUrlHelperFactory</a> to create <code>IUrlHelper</code> objects. Decorate that as well: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SigningUrlHelperFactory</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IUrlHelperFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IUrlHelperFactory</span>&nbsp;inner; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>[]&nbsp;urlSigningKey; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">SigningUrlHelperFactory</span>(<span style="color:#2b91af;">IUrlHelperFactory</span>&nbsp;inner,&nbsp;<span style="color:blue;">byte</span>[]&nbsp;urlSigningKey) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.inner&nbsp;=&nbsp;inner; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.urlSigningKey&nbsp;=&nbsp;urlSigningKey; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUrlHelper</span>&nbsp;GetUrlHelper(<span style="color:#2b91af;">ActionContext</span>&nbsp;context) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;url&nbsp;=&nbsp;inner.GetUrlHelper(context); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SigningUrlHelper</span>(url,&nbsp;urlSigningKey); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Straightforward: use the <code>inner</code> object to get an <code>IUrlHelper</code> object, and decorate it with <code>SigningUrlHelper</code>. </p> <h3 id="92c13952cd48418c81c6925d9af15635"> Configuration <a href="#92c13952cd48418c81c6925d9af15635" title="permalink">#</a> </h3> <p> The final piece of the puzzle is to tell the framework about the <code>SigningUrlHelperFactory</code>. You can do this in the <code>Startup</code> class' <code>ConfigureServices</code> method. </p> <p> First, read the signing key from the configuration system (e.g. a configuration file): </p> <p> <pre><span style="color:blue;">var</span>&nbsp;urlSigningKey&nbsp;=&nbsp;<span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes( &nbsp;&nbsp;&nbsp;&nbsp;Configuration.GetValue&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;UrlSigningKey&quot;</span>));</pre> </p> <p> Then use the signing key to configure the <code>SigningUrlHelperFactory</code> service. Here, I wrapped that in a little helper method: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConfigureUrSigning( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IServiceCollection</span>&nbsp;services, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">byte</span>[]&nbsp;urlSigningKey) { &nbsp;&nbsp;&nbsp;&nbsp;services.RemoveAll&lt;<span style="color:#2b91af;">IUrlHelperFactory</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;services.AddSingleton&lt;<span style="color:#2b91af;">IUrlHelperFactory</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SigningUrlHelperFactory</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UrlHelperFactory</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;urlSigningKey)); }</pre> </p> <p> This method first removes the default <code>IUrlHelperFactory</code> service and then adds the <code>SigningUrlHelperFactory</code> instead. It decorates <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.routing.urlhelperfactory">UrlHelperFactory</a>, which is the default built-in implementation of the interface. </p> <h3 id="2efe02caa55742679a953a398c235bc5"> Conclusion <a href="#2efe02caa55742679a953a398c235bc5" title="permalink">#</a> </h3> <p> You can extend the ASP.NET framework to add a signature to all the URLs it generates. All it takes is two simple Decorators. </p> <p> <strong>Next:</strong> <a href="/2020/11/09/checking-signed-urls-with-aspnet">Checking signed URLs with ASP.NET</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fit URLs https://blog.ploeh.dk/2020/10/26/fit-urls 2020-10-26T06:19:00+00:00 Mark Seemann <div id="post"> <p> <em>Keep REST API URLs evolvable. A way to address Hyrum's law.</em> </p> <p> Publishing and maintaining a true (<a href="https://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a>) RESTful API is difficult. This is the style of REST design where clients are expected to <em>follow links</em> to perform the work they want to accomplish; not assemble URLs from templates. </p> <p> Have you ever designed a URL scheme and published it, only to later discover that you wished you'd come up with a different structure? If you've published a set of URL templates, changing your mind constitutes a breaking change. If clients follow links, however, URLs are opaque and you can redesign the URLs without breaking existing clients. </p> <p> You can try to document this design principle all you want, to no avail. You can <em>tell</em> client developers that they're supposed to follow links, not try to <a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls">retro-engineer the URLs</a>, and still they'll do it. </p> <p> I know; I've experienced it. When we later changed the URL structure, it didn't take long for the client developers to complain that we broke their code. </p> <h3 id="f9591f3f57534243afc883e166727635"> Hyrum's law <a href="#f9591f3f57534243afc883e166727635" title="permalink">#</a> </h3> <p> This is an example of <a href="https://www.hyrumslaw.com">Hyrum's law</a> in action, albeit on the scale of web service interactions, rather than low-level APIs. The presence of a discernible system to URLs suggests an <em>implicit</em> interface. </p> <p> Consider this 'home' resource for an online restaurant reservation system: </p> <p> <pre>GET / HTTP/1.1 HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:reservations&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/reservations&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:year&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:month&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020/10&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:day&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020/10/23&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> It doesn't take much interaction with the API before you realise that there's a system to the URLs provided in the links. If you want to see the calendar for a specific date, you can easily retro-engineer the URL template <code>/calendar/{yyyy}/{MM}/{dd}</code>, and <code>/calendar/{yyyy}/{MM}</code> for a month, and so on. </p> <p> The same is likely to happen with the <em>reservations</em> link. You can <code>POST</code> to this link to make a new reservation: </p> <p> <pre>POST /reservations HTTP/1.1 Content-Type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-12-09 19:15&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;rainboughs@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Raine&nbsp;Burroughs&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;5 } HTTP/1.1 201 Created Content-Type: application/json Location: http://localhost:53568/reservations/fabc5bf63a1a4db38b95deaa89c01178 { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;fabc5bf63a1a4db38b95deaa89c01178&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;at&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-12-09T19:15:00.0000000&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;rainboughs@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Raine&nbsp;Burroughs&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;5 }</pre> </p> <p> Notice that when the API responds, its <code>Location</code> header gives you the URL for that particular reservation. It doesn't take long to figure out that there's a template there, as well: <code>/reservations/{id}</code>. </p> <p> So client developers may just store the ID (<code>fabc5bf63a1a4db38b95deaa89c01178</code>) and use the implied template to construct URLs on the fly. And who can blame them? </p> <p> That, however, misses the point of REST. The ID of that reservation isn't <code>fabc5bf63a1a4db38b95deaa89c01178</code>, but rather <code>http://localhost:53568/reservations/fabc5bf63a1a4db38b95deaa89c01178</code>. Yes, the URL, all of it, is the ID. </p> <h3 id="fff55f373ff24a4da6cd4e50dd8b97bd"> Evolving URLs <a href="#fff55f373ff24a4da6cd4e50dd8b97bd" title="permalink">#</a> </h3> <p> Why does that matter? </p> <p> It matters because you're human, and you make mistakes. Or, rather, it's intrinsic to software development that you learn as you work. You'll make decisions at the beginning that you'll want to change as you gain more insight. </p> <p> Also, requirements change. Consider the URL template scheme implied by the above examples. Can you spot any problems? Would you want to change anything? </p> <p> Imagine, for example, that you've already deployed the first version of the API. It's a big success. Now the product owner wants to expand the market to more restaurants. She wants to make the service a multi-tenant API. How does that affect URLs? </p> <p> In that new context, perhaps URLs like <code>/restaurants/1/reservations</code> or <code>/restaurants/90125/calendar/2020/10</code> would be better. </p> <p> That, however, would be a breaking change if clients construct URLs based on implied templates. </p> <p> Couldn't you just pass the restaurant ID as an HTTP header instead of in the URL? Yes, technically you could do that, but that doesn't work well with HTTP caching. It's not a RESTful thing to do, for that, and other, reasons. </p> <h3 id="41cdd42bac1b4bd5a573070ee27e902d"> Fitness <a href="#41cdd42bac1b4bd5a573070ee27e902d" title="permalink">#</a> </h3> <p> Do we just give up in the face of Hyrum's law? Or can we keep URLs evolvable? In evolution, organisms evolve according to a 'fitness function', so to name such URLs, we could call them <em>fit URL</em>. </p> <p> To keep URLs fit, we must prevent client developers from retro-engineering the implied interface. My original thought was to give each URL an opaque ID, such as a GUID, but <a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls#424bd3f6199e422c8294e300d312ffb4">in 2015 Dan Kubb instead suggested to sign the URLs</a>. What a great idea! </p> <p> If you do that, then the above home resource might look like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:reservations&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/restaurants/1/reservations?sig=1WiLlS5705bfsffPzaFYLwntrS4FCjE5CLdaeYTHxxg%3D&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:year&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/restaurants/1/calendar/2020?sig=eIFuUkb6WprPrp%2B4HPSPaavcUdwVjeG%2BKVrIRqDs9OI%3D&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:month&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/restaurants/1/calendar/2020/10?sig=mGqkAjY7vMbC5Fr7UiRXWWnjn3pFn21MYrMagpdWaU0%3D&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:day&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/restaurants/1/calendar/2020/10/23?sig=Ua5F%2FucP6zmAy219cHa4WG7zIcCa0hgVD5ModXcNQuo%3D&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> Even if you can still figure out what the URL templates are, it doesn't avail you. Creating a new reservation may return a URL like <code>https://localhost:53568/restaurants/1/reservations/fabc5bf63a1a4db38b95deaa89c01178?sig=8e80PmVi8aSS1UH6iSJ73nHmOsCLrUMs7yggEOkvEqo%3D</code>, but you can't just replace the ID with another ID and expect it to work: </p> <p> <pre>GET /restaurants/1/reservations/79520877ef4f4acdb69838e22ad04510?sig=8e80PmVi8aSS1UH6iSJ73nHmOsCLrUMs7yggEOkvEqo%3D HTTP/1.1 HTTP/1.1 404 Not Found</pre> </p> <p> You're requesting a URL that doesn't exist, so the result is <code>404 Not Found</code>. To be clear: yes, there <em>is</em> a reservation with the ID <code>79520877ef4f4acdb69838e22ad04510</code>, but its URL isn't the above URL. </p> <h3 id="104b052da10e463297020c109dec705d"> ASP.NET implementation <a href="#104b052da10e463297020c109dec705d" title="permalink">#</a> </h3> <p> In two articles, I'll show you how to implement <em>fit URLs</em> in ASP.NET Core. <ul> <li><a href="/2020/11/02/signing-urls-with-aspnet">Signing URLs with ASP.NET</a></li> <li><a href="/2020/11/09/checking-signed-urls-with-aspnet">Checking signed URLs with ASP.NET</a></li> <li><a href="/2020/11/16/redirect-legacy-urls">Redirect legacy URLs</a></li> </ul> The ASP.NET framework comes with enough extensibility points to make this a non-intrusive operation. I implemented this using a filter and a few <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a> in a way so that you can easily turn the feature on or off. </p> <h3 id="ff0ced83b9fb49e8835fc92e08dd2859"> Conclusion <a href="#ff0ced83b9fb49e8835fc92e08dd2859" title="permalink">#</a> </h3> <p> One of the major benefits of true RESTful API design is that it's evolvable. It enables you to learn and improve as you go, without breaking existing clients. </p> <p> You have to take care, however, that clients don't retro-engineer the URL templates that you may be using for implementation purposes. You want to be able to change URLs in the future. </p> <p> Hyrum's law suggests that clients will rely on undocumented features if they can. By signing the URLs you keep them fit to evolve. </p> <p> <strong>Next:</strong> <a href="/2020/11/02/signing-urls-with-aspnet">Signing URLs with ASP.NET</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b0fb43ff9aba4c14a075e7effc9fae25"> <div class="comment-author"><a href="https://github.com/Skleni">Daniel Sklenitzka</a> <a href="#b0fb43ff9aba4c14a075e7effc9fae25">#</a></div> <div class="comment-content"> <p> I see how signing the URLs prevents clients from retro-engineering the URL templates, but how does it help preventing breaking changes? If the client stores the whole URL instead of just the ID and later the URL changes because a restaurant ID is added, the original URL is still broken, isn't it? </p> </div> <div class="comment-date">2020-10-27 07:20 UTC</div> </div> <div class="comment" id="848275a366db40e2a35810554633447f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#848275a366db40e2a35810554633447f">#</a></div> <div class="comment-content"> <p> Daniel, thank you for writing. You're correct when assuming that clients are allowed to 'bookmark' URLs. An implicit assumption that I didn't explicitly state is that clients are supposed to follow not only links, but also redirects. Thus, to avoid breaking changes, it's the API's responsibility to leave a <code>301 Moved Permanently</code> response behind at the old address. </p> <p> As a service owner, though, you have some flexibility in how to achieve this. You can code this into the service code itself, but another option might be to use a reverse proxy for such purposes. One of the many advantages of REST is that you can offload a lot HTTP-level behaviour on standard networking software; <a href="/2020/06/01/retiring-old-service-versions#bc73620aa71141b6a74f4a4aaf395d75">here's another example</a>. </p> </div> <div class="comment-date">2020-10-28 7:15 UTC</div> </div> <div class="comment" id="880c4cda8ab14b1d8de05438becac2f1"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#880c4cda8ab14b1d8de05438becac2f1">#</a></div> <div class="comment-content"> <p> I think I have the same confusion as Daniel. </p> <blockquote> <p> If you've published a set of URL templates, changing your mind constitutes a breaking change. If clients follow links, however, URLs are opaque and you can redesign the URLs without breaking existing clients. </p> <p> ... </p> <p> You're correct when assuming that clients are allowed to 'bookmark' URLs. An implicit assumption that I didn't explicitly state is that clients are supposed to follow not only links, but also redirects. Thus, to avoid breaking changes, it's the API's responsibility to leave a <code>301 Moved Permanently</code> response behind at the old address. </p> </blockquote> <p> Is there a certain kind of breaking change that exists for a level 2 API that doesn't exist for a level 3 API? </p> </div> <div class="comment-date">2020-10-28 22:55 UTC</div> </div> <div class="comment" id="aae1427ebab14c428291984efb06fd94"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#aae1427ebab14c428291984efb06fd94">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I haven't thought much about whether there are categories of errors that differ between the levels, but I think that in practice, there's a difference in mindset. </p> <p> With a level 2 API, I think most people are still caught in a mindset that's largely a projection of RPC. URL templates map easily to programming procedures (e.g. object-oriented methods). Thus, you have a mental model that includes <code>PostReservation</code>, <code>GetCalendar</code>, etc. People think of <em>that</em> as being the API. With that mindset, I don't think that many client developers configure their HTTP clients to follow redirects. Thus, one could argue that changing URLs are breaking changes for a level 2 API, even if you leave <code>301 Moved Permanently</code> responses behind. </p> <p> With level 3 APIs, you encourage client developers to think in terms of 'the web'. That includes following links and redirects. I believe that there's a difference in perception, even if there may not be any technical difference. </p> <p> I do believe, however, that the real advantage is that you impose a smaller maintenance burden on client developers with a level 3 API. Granted, a client developer may have to spend a little more effort up front to follow links, but once a compliant client is in place, it <em>needs no more maintenance</em>. It'll just keep working. </p> <p> Not so with published URL templates. Here you have to entice client developers to <em>actively</em> update their code. This may be impossible if you don't know who the clients are, or if the client software is in maintenance mode. This may make it harder to <a href="/2020/06/01/retiring-old-service-versions">retire old versions</a>. You may be stuck with these old versions forever. </p> </div> <div class="comment-date">2020-10-30 17:32 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Monomorphic functors https://blog.ploeh.dk/2020/10/19/monomorphic-functors 2020-10-19T07:36:00+00:00 Mark Seemann <div id="post"> <p> <em>Non-generic mappable containers, with a realistic example.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. Previous articles have covered <a href="/2018/03/26/the-maybe-functor">Maybe</a>, <a href="/2018/09/10/the-lazy-functor">Lazy</a>, and other functors. This article looks at what happens when you weaken one of the conditions that the article series so far has implied. </p> <p> In the <a href="/2018/03/22/functors">introductory article</a>, I wrote: <blockquote> As a rule of thumb, if you have a type with a generic type argument, it's a candidate to be a functor. </blockquote> That still holds, but then <a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="/2018/03/22/functors#dee7ec216a464248b6103ea0979948ab">asks if that's a required condition</a>. It turns out that it isn't. In this article, you'll learn about the implications of weakening this condition. </p> <p> As is my habit with many of the articles in this article series, I'll start by uncovering the structure of the concept, and only later show a more realistic example. </p> <h3 id="bea56fadde1e4e9da8389a6c31937216"> Mapping strings <a href="#bea56fadde1e4e9da8389a6c31937216" title="permalink">#</a> </h3> <p> So far in this article series, you've seen examples of <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">containers</a> with a generic type argument: <code>Tree&lt;T&gt;</code>, <code>Task&lt;T&gt;</code>, and so on. Furthermore, you've seen how a functor is a container with a <em>structure-preserving map</em>. This function has various names in different languages: <code>Select</code>, <code>fmap</code>, <code>map</code>, etcetera. </p> <p> Until now, you've only seen examples where this mapping enables you to translate from one type argument to another. You can, for example, translate the characters in a string to Boolean values, like this: </p> <p> <pre>&gt; <span style="color:#a31515;">&quot;Safe&nbsp;From&nbsp;Harm&quot;</span>.<span style="color:#2b91af;">Select</span>(c&nbsp;=&gt;&nbsp;c.IsVowel()) Enumerable.WhereSelectEnumerableIterator&lt;char, bool&gt; &nbsp;&nbsp;{ false, true, false, true, false, false, false, true, false, false, false, true, false, false }</pre> </p> <p> This works because in C#, <a href="https://docs.microsoft.com/dotnet/api/system.string">the String class</a> implements various interfaces, among these <code>IEnumerable&lt;char&gt;</code>. By treating a <code>string</code> as an <code>IEnumerable&lt;char&gt;</code>, you can map each element. That's the standard IEnumerable functor (AKA <em>the list functor</em>). </p> <p> What if you'd like to map the characters in a string to other characters? Perhaps you'd like to map vowels to upper case, and all other characters to lower case. You could try this: </p> <p> <pre>&gt; <span style="color:#a31515;">&quot;Safe&nbsp;From&nbsp;Harm&quot;</span>.<span style="color:#2b91af;">Select</span>(c&nbsp;=&gt;&nbsp;c.IsVowel()&nbsp;?&nbsp;<span style="color:blue;">char</span>.ToUpper(c)&nbsp;:&nbsp;<span style="color:blue;">char</span>.ToLower(c)) Enumerable.WhereSelectEnumerableIterator&lt;char, char&gt; &nbsp;&nbsp;{ 's', 'A', 'f', 'E', ' ', 'f', 'r', 'O', 'm', ' ', 'h', 'A', 'r', 'm' }</pre> </p> <p> That sort of works, but as you can tell, the result isn't a <code>string</code>, it's an <code>IEnumerable&lt;char&gt;</code>. </p> <p> This isn't a big problem, because one of the <code>string</code> constructor overloads take a <code>char</code> array as input, so you can do this: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>&nbsp;(<span style="color:#a31515;">&quot;Safe&nbsp;From&nbsp;Harm&quot;</span>.<span style="color:#2b91af;">Select</span>(c&nbsp;=&gt;&nbsp;c.IsVowel()&nbsp;?&nbsp;<span style="color:blue;">char</span>.ToUpper(c)&nbsp;:&nbsp;<span style="color:blue;">char</span>.ToLower(c)).<span style="color:#2b91af;">ToArray</span>()) "sAfE frOm hArm"</pre> </p> <p> It isn't the prettiest, but it gets the job done. </p> <h3 id="fd3da7dcd560415d81ab82bfb772d94c"> Monomorphic functor in C# <a href="#fd3da7dcd560415d81ab82bfb772d94c" title="permalink">#</a> </h3> <p> If you contemplate the last example, you may arrive at the conclusion that you could package some of that boilerplate code in a reusable function. Since we're already talking about functors, why not call it <code>Select</code>? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Select(<span style="color:blue;">this</span>&nbsp;<span style="color:blue;">string</span>&nbsp;source,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(source.AsEnumerable().Select(selector).ToArray()); }</pre> </p> <p> It somewhat simplifies things: </p> <p> <pre>&gt; <span style="color:#a31515;">&quot;Safe&nbsp;From&nbsp;Harm&quot;</span>.Select(c&nbsp;=&gt;&nbsp;c.IsVowel()&nbsp;?&nbsp;<span style="color:blue;">char</span>.ToUpper(c)&nbsp;:&nbsp;<span style="color:blue;">char</span>.ToLower(c)) "sAfE frOm hArm"</pre> </p> <p> Since I deliberately wrote the <code>Select</code> method in the style of other <code>Select</code> methods (apart from the generics), you may wonder if C# query syntax also works? </p> <p> <pre>&gt; <span style="color:blue;">from</span>&nbsp;c&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#a31515;">&quot;Army&nbsp;of&nbsp;Me&quot;</span> . <span style="color:blue;">select</span>&nbsp;c.IsVowel()&nbsp;?&nbsp;<span style="color:blue;">char</span>.ToUpper(c)&nbsp;:&nbsp;<span style="color:blue;">char</span>.ToLower(c) "ArmY Of mE"</pre> </p> <p> It compiles and works! The C# compiler understands monomorphic containers! </p> <p> I admit that I was quite surprised when I first tried this out. </p> <h3 id="8ef3808c11a34d9aaa1c25b94539f843"> Monomorphic functor in Haskell <a href="#8ef3808c11a34d9aaa1c25b94539f843" title="permalink">#</a> </h3> <p> Surprisingly, in this particular instance, C# comes out looking more flexible than <a href="https://www.haskell.org">Haskell</a>. This is mainly because in C#, functors are implemented as a special compiler feature, whereas in Haskell, <code>Functor</code> is defined using the general-purpose <em>type class</em> language feature. </p> <p> There's <a href="https://hackage.haskell.org/package/mono-traversable">a package</a> that defines a <code>MonoFunctor</code> type class, as well as some instances. With it, you can write code like this: </p> <p> <pre><span style="color:#2b91af;">ftg</span>&nbsp;::&nbsp;<span style="color:blue;">Text</span> ftg&nbsp;=&nbsp;omap&nbsp;(\c&nbsp;-&gt;&nbsp;<span style="color:blue;">if</span>&nbsp;isVowel&nbsp;c&nbsp;<span style="color:blue;">then</span>&nbsp;toUpper&nbsp;c&nbsp;<span style="color:blue;">else</span>&nbsp;toLower&nbsp;c)&nbsp;<span style="color:#a31515;">&quot;Fade&nbsp;to&nbsp;Grey&quot;</span></pre> </p> <p> Even though <code>Text</code> isn't a <code>Functor</code> instance, it <em>is</em> a <code>MonoFunctor</code> instance. The value of <code>ftg</code> is <code>"fAdE tO grEY"</code>. </p> <p> All the normal functors (<code>[]</code>, <code>Maybe</code>, etc.) are also <code>MonoFunctor</code> instances, since the normal <code>Functor</code> instance is more capable than a <code>MonoFunctor</code>. </p> <h3 id="30dd605a3bb34d1985e1d277e1c9b0a5"> Restaurant example <a href="#30dd605a3bb34d1985e1d277e1c9b0a5" title="permalink">#</a> </h3> <p> While I've introduced the concept of a monomorphic functor with a trivial string example, I actually discovered the C# feature when I was working on some more realistic code. As I often do, I was working on a variation of an online restaurant reservation system. <ins datetime="2021-07-18T08:45Z">The code shown in the following is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <p> The code base contained a rather complex variation of an implementation of the <a href="/2020/01/27/the-maitre-d-kata">Maître d' kata</a>. The <code>MaitreD</code> constructor looked like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeOfDay</span>&nbsp;opensAt, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeOfDay</span>&nbsp;lastSeating, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;seatingDuration, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&nbsp;tables)</pre> </p> <p> This had worked well when the system only dealt with a single restaurant, but I was now expanding it to a multi-tenant system and I needed to keep track of some more information about each restaurant, such as its name and ID. While I could have added such information to the <code>MaitreD</code> class, I didn't want to pollute that class with data it didn't need. While the restaurant's opening time and seating duration are necessary for the decision algorithm, the name isn't. </p> <p> So I introduced a wrapper class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Restaurant</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>(<span style="color:blue;">int</span>&nbsp;id,&nbsp;<span style="color:blue;">string</span>&nbsp;name,&nbsp;<span style="color:#2b91af;">MaitreD</span>&nbsp;maitreD) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id&nbsp;=&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MaitreD&nbsp;=&nbsp;maitreD; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>&nbsp;MaitreD&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;code&nbsp;follows...</span></pre> </p> <p> I also added copy-and-update methods (AKA 'withers'): </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>&nbsp;WithId(<span style="color:blue;">int</span>&nbsp;newId) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>(newId,&nbsp;Name,&nbsp;MaitreD); } <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>&nbsp;WithName(<span style="color:blue;">string</span>&nbsp;newName) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>(Id,&nbsp;newName,&nbsp;MaitreD); } <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>&nbsp;WithMaitreD(<span style="color:#2b91af;">MaitreD</span>&nbsp;newMaitreD) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>(Id,&nbsp;Name,&nbsp;newMaitreD); }</pre> </p> <p> Still, if you need to modify the <code>MaitreD</code> within a given <code>Restaurant</code> object, you first have to have a reference to the <code>Restaurant</code> so that you can read its <code>MaitreD</code> property, then you can edit the <code>MaitreD</code>, and finally call <code>WithMaitreD</code>. Doable, but awkward: </p> <p> <pre>restaurant.WithMaitreD(restaurant.MaitreD.WithSeatingDuration(<span style="color:#2b91af;">TimeSpan</span>.FromHours(.5)))</pre> </p> <p> So I got the idea that I might try to add a structure-preserving map to <code>Restaurant</code>, which I did: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Restaurant</span>&nbsp;Select(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">MaitreD</span>,&nbsp;<span style="color:#2b91af;">MaitreD</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(selector)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;WithMaitreD(selector(MaitreD)); }</pre> </p> <p> The first time around, it enabled me to rewrite the above expression as: </p> <p> <pre>restaurant.Select(m&nbsp;=&gt;&nbsp;m.WithSeatingDuration(<span style="color:#2b91af;">TimeSpan</span>.FromHours(.5)))</pre> </p> <p> That's already a little nicer. It also handles the situation where you may not have a named <code>Restaurant</code> variable you can query; e.g. if you have a method that returns a <code>Restaurant</code> object, and you just want to continue calling into a <a href="https://martinfowler.com/bliki/FluentInterface.html">Fluent API</a>. </p> <p> Then I thought, <em>I wonder if query syntax works, too...</em> </p> <p> <pre><span style="color:blue;">from</span>&nbsp;m&nbsp;<span style="color:blue;">in</span>&nbsp;restaurant <span style="color:blue;">select</span>&nbsp;m.WithSeatingDuration(<span style="color:#2b91af;">TimeSpan</span>.FromHours(.5))</pre> </p> <p> And it <em>does</em> work! </p> <p> I know that a lot of people don't like query syntax, but I think it has certain advantages. In this case, it actually isn't shorter, but it's a nice alternative. I particularly find that if I try to <a href="/2019/11/04/the-80-24-rule">fit my code into a tight box</a>, query syntax sometimes gives me an opportunity to format code over multiple lines in a way that's more natural than with method-call syntax. </p> <h3 id="c386660f74424f33921f35265e61ecf0"> Conclusion <a href="#c386660f74424f33921f35265e61ecf0" title="permalink">#</a> </h3> <p> A monomorphic functor is still a functor, only it constrains the type of mapping you can perform. It can be useful to map monomorphic containers like strings, or immutable nested data structures like the above <code>Restaurant</code> class. </p> <p> <strong>Next:</strong> <a href="/2018/12/03/set-is-not-a-functor">Set is not a functor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="bb0bb9d1bf094c1897f9dd42fb49712c"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#bb0bb9d1bf094c1897f9dd42fb49712c">#</a></div> <div class="comment-content"> <p> Excellent article! I was planning to eventually write a blog post on this topic, but now there is no need. </p> <blockquote> <p> I know that a lot of people don't like query syntax, but I think it has certain advantages. In this case, it actually isn't shorter, but it's a nice alternative. I particularly find that if I try to <a href="/2019/11/04/the-80-24-rule">fit my code into a tight box</a>, query syntax sometimes gives me an opportunity to format code over multiple lines in a way that's more natural than with method-call syntax. </p> </blockquote> <p> Said another way, one objective measure that is better in this case when using query syntax is that the maximum line width is smaller. It is objective primarily in the sense that maximum line width is objective and secondarily in the sense that we generally agree that code with a lower maximum line width is typically easier to understand. </p> <p> Another such objective measure of quality that I value is the maximum number of matching pairs of parentheses that are nested. This measure improves to two in your query syntax code from three in your previous code. The reason it was three before is because there are four levels of detail and your code decreases the level of detail three times: <code>Restaurant</code> to <code>MaitreD</code>, <code>MaitreD</code> to <code>TimeSpan</code>, and <code>TimeSpan</code> to <code>double</code>. Query syntax is one way to decrease the level of detail without having to introduce a pair of matching parentheses. </p> <p> I find more success minimizing this measure of quality when taking a bottom-up approach. Using the <a href="https://github.com/louthy/language-ext/blob/3df8c837961a502d7a03268b6cac636a57c49056/LanguageExt.Core/DataTypes/Cond/Cond.cs#L56-L70"><code>Apply</code> extension method from <code>language-ext</code></a>, which is like the <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/#table-of-symbols-and-operators:~:text=Passes%20the%20result%20of%20the%20left%20side%20to%20the%20function%20on%20the%20right%20side%20(forward%20pipe%20operator).">forward pipe operator in F#</a>, we can rewrite this code without any nested pairs of matching parentheses as </p> <p> <pre>.5.Apply(<span style="color:#2b91af;">TimeSpan</span>.FromHours).Apply(m&nbsp;=&gt;&nbsp;m.WithSeatingDuration).Apply(restaurant.Select)</pre> </p> <p> (I did not check if this code compiles. If it does not, it would be because the C# compiler is unsure how to implicitly convert some method group to its intended <code>Func&lt;,&gt;</code> delegate.) This code also has natural line-breaking points before each dot operator, which leads to a comfortable value for the maximum line width. </p> <p> Another advantage of this code that I value is that the execution happens in the same order in which I (as an English speaker) read it: left to right. Your code before using the query syntax executed from right to left as dictated by the matching parentheses. In fact, since the dot operator is left associative, the degree to which execution occurs from left to right is inversely correlated to the number of nested pairs of parentheses. (One confusing thing here is the myriad of different semantic meanings in C# to the syntactic use of a pair of parentheses.) </p> </div> <div class="comment-date">2020-10-19 14:05 UTC</div> </div> <div class="comment" id="8e6b870ce4aa44aa98a16a7969def6e9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8e6b870ce4aa44aa98a16a7969def6e9">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I've never thought about measuring complexity by nested parentheses, but I like it! It makes intuitive sense. </p> <p> I'm not sure it applies to LISP-like languages, but perhaps some reader comes by some day who can enlighten us. </p> </div> <div class="comment-date">2020-10-21 13:55 UTC</div> </div> <div class="comment" id="393e63b1e52c4e9bad31ee579d5e9c80"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#393e63b1e52c4e9bad31ee579d5e9c80">#</a></div> <div class="comment-content"> <p> While reading your articles about <a href="https://blog.ploeh.dk/2021/09/02/contravariant-functors/">contravairant functors</a>, a better name occured to me for what you called a monomorphic functor in this article. </p> <p> For a covariant functor, its mapping function accepts a function with types that vary (i.e. differ). The order in which they vary matches the order that the types vary in the corresponding elevated function. For a contravariant functor, its mapping function also accepts a function with types that vary. However, the order in which they vary is reversed compared to the order that the types vary in the corresponding elevated function. For the functor in this article that you called a monomorphic functor, its mapping function accepts a function with types that do <i>not vary</i>. As such, we should call such a functor an <i>invariant</i> functor. </p> </div> <div class="comment-date">2021-09-14 01:01 UTC</div> </div> <div class="comment" id="3462f8e2c4a44593889f4816ba41ed88"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3462f8e2c4a44593889f4816ba41ed88">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. That's a great suggestion that makes a lot of sense. </p> <p> The term <em>invariant functor</em> seems, however, to be taken by a wider definition. </p> <blockquote> <p> "an invariant type <code>T</code> allows you to map from <code>a</code> to <code>b</code> if and only if <code>a</code> and <code>b</code> are isomorphic. In a very real sense, this isn't an interesting property - an isomorphism between <code>a</code> and <code>b</code> means that they're already the same thing to begin with." </p> <footer><cite><a href="https://reasonablypolymorphic.com">Sandy Maguire</a>, <a href="https://thinkingwithtypes.com">Thinking with Types</a></cite></footer> </blockquote> <p> This seems, at first glance, to fit your description quite well, but when we try to encode this in a programming language, something surprising happens. We'd typically say that the types <code>a</code> and <code>b</code> are isomorphic if and only if there exists two functions <code>a -&gt; b</code> <em>and</em> <code>b -&gt; a</code>. Thus, we can encode this in a Haskell type class like this one: </p> <p> <pre><span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Invariant</span>&nbsp;f&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:#2b91af;">invmap</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;b</pre> </p> <p> I've taken the name of both type class and method from <em>Thinking with Types</em>, but we find the same naming in <a href="https://hackage.haskell.org/package/invariant/docs/Data-Functor-Invariant.html">Data.Functor.Invariant</a>. The <a href="https://hackage.haskell.org/package/invariant-0.1.0">first version of the invariant package</a> is from 2012, so assume that Sandy Maguire has taken the name from there (and not the other way around). </p> <p> There's <a href="https://typelevel.org/cats/typeclasses/invariant.html">a similar-looking definition in Cats</a> (although I generally don't read Scala), and in C# we might define a method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Invariant</span>&lt;<span style="color:#2b91af;">A</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Invariant&lt;B&gt;&nbsp;<span style="color:#74531f;">InvMap</span>&lt;<span style="color:#2b91af;">B</span>&gt;(Func&lt;A,&nbsp;B&gt;&nbsp;<span style="color:#1f377f;">selector1</span>,&nbsp;Func&lt;B,&nbsp;A&gt;&nbsp;<span style="color:#1f377f;">selector2</span>) }</pre> </p> <p> This seems, at first glance, to describe monomorphic functors quite well, but it turns out that it's not quite the same. Consider mapping of <code>String</code>, as discussed in this article. The <code>MonoFunctor</code> instance of <code>String</code> (or, generally, <code>[a]</code>) lifts a <code>Char -&gt; Char</code> function to <code>String -&gt; String</code>. </p> <p> In Haskell, <code>String</code> is really a type alias for <code>[Char]</code>, but what happens if we attempt to describe <code>String</code> as an <code>Invariant</code> instance? We can't, because <code>String</code> has no type argument. </p> <p> We can, however, see what happens if we try to make <code>[a]</code> an <code>Invariant</code> instance. If we can do that, we can try to experiment with <code>Char</code>'s underlying implementation as a number: </p> <blockquote> <p> "To convert a <code>Char</code> to or from the corresponding <code>Int</code> value defined by Unicode, use <code>toEnum</code> and <code>fromEnum</code> from the <code>Enum</code> class respectively (or equivalently <code>ord</code> and <code>chr</code>)." </p> <footer><cite><a href="https://hackage.haskell.org/package/base/docs/Data-Char.html#t:Char">Char documentation</a></cite></footer> </blockquote> <p> <code>ord</code> and <code>chr</code> enable us to treat <code>Char</code> and <code>Int</code> as isomorphic: </p> <p> <pre>&gt; ord 'h' 104 &gt; chr 104 'h'</pre> </p> <p> Since we have both <code>Char -&gt; Int</code> and <code>Int -&gt; Char</code>, we should be able to use <code>invmap ord chr</code>, and indeed, it works: </p> <p> <pre>&gt; invmap ord chr "foo" [102,111,111]</pre> </p> <p> When, however, we look at the <code>Invariant []</code> instance, we may be in for a surprise: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Invariant</span>&nbsp;[]&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;invmap&nbsp;f&nbsp;_&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;f</pre> </p> <p> The instance uses only the <code>a -&gt; b</code> function, while it completely ignores the <code>b -&gt; a</code> function! Since the instance uses <code>fmap</code>, rather than <code>map</code>, it also strongly suggests that all (covariant) <code>Functor</code> instances are also <code>Invariant</code> instances. That's <a href="https://hackage.haskell.org/package/invariant/docs/Data-Functor-Invariant.html#v:invmapFunctor">also the conclusion reached by the invariant package</a>. Not only that, but <a href="https://hackage.haskell.org/package/invariant/docs/Data-Functor-Invariant.html#v:invmapContravariant">every Contravariant functor is also an Invariant functor</a>. </p> <p> That seems to completely counter the idea that <code>a</code> and <code>b</code> should be isomorphic. I can't say that I've entirely <a href="/ref/stranger-in-a-strange-land">grokked</a> this disconnect yet, but it seems to me to be an artefact of instances' ability to ignore either of the functions passed to <code>invmap</code>. At least we can say that client code can't <em>call</em> <code>invmap</code> unless it supplies both functions (here I pretend that <code>undefined</code> doesn't exist). </p> <p> If we go with the Haskell definition of things, an invariant functor is a much wider concept than a monomorphic functor. <code>Invariant</code> describes a superset that contains both (covariant) <code>Functor</code> and <code>Contravariant</code> functor. This superset is more than just the union of those two sets, since it also includes containers that are neither <code>Functor</code> nor <code>Contravariant</code>, for example <a href="https://hackage.haskell.org/package/base/docs/Data-Monoid.html#t:Endo">Endo</a>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Invariant</span>&nbsp;<span style="color:blue;">Endo</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;invmap&nbsp;f&nbsp;g&nbsp;(Endo&nbsp;h)&nbsp;=&nbsp;Endo&nbsp;(f&nbsp;.&nbsp;h&nbsp;.&nbsp;g)</pre> </p> <p> For example, <a href="https://hackage.haskell.org/package/base/docs/Data-Char.html#v:toUpper">toUpper</a> is an <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a>, so we can apply it to <code>invmap</code> and evaluate it like this: </p> <p> <pre>&gt; (appEndo $ invmap ord chr $ Endo toUpper) 102 70</pre> </p> <p> This example takes the <code>Int</code> <code>102</code> and converts it to a <code>String</code> with <code>chr</code>: </p> <p> <pre>&gt; chr 102 'f'</pre> </p> <p> It then applies <code>'f'</code> to <code>toUpper</code>: </p> <p> <pre>&gt; toUpper 'f' 'F'</pre> </p> <p> Finally, it converts <code>'F'</code> with <code>ord</code>: </p> <p> <pre>&gt; ord 'F' 70</pre> </p> <p> Endomorphisms are neither covariant nor contravariant functors - they are invariant functors. They may also be monomorphic functors. The <a href="https://hackage.haskell.org/package/mono-traversable">mono-traversable</a> package doesn't define a <code>MonoFunctor</code> instance for <code>Endo</code>, but that may just be an omission. It'd be an interesting exercise to see if such an instance is possible. I don't know yet, but I think that it is... </p> <p> For now, though, I'm going to leave that as an exercise. Readers who manage to implement a lawful instance of <code>MonoFunctor</code> for <code>Endo</code> should send a pull request to the <em>mono-traversable</em> package. </p> <p> Finally, a note on words. I agree that <em>invariant functor</em> would also have been a good description of monomorphic functors. It seems to me that in both mathematics and programming, there's sometimes a 'land-grab' of terms. The first person to think of a concept gets to pick the word. If the concept is compelling, the word may stick. </p> <p> This strikes me as an unavoidable consequence of how knowledge progresses. Ideas rarely spring forth fully formed. Often, someone gets a half-baked idea and names it before fully understanding it. When later, more complete understanding emerges, it may be too late to change the terminology. </p> <p> This has happened to me in <a href="/2017/10/04/from-design-patterns-to-category-theory">this very article series</a>, which I titled <em>From design patterns to category theory</em> before fully understanding where it'd lead me. I've later come to realise that we need only <a href="https://en.wikipedia.org/wiki/Abstract_algebra">abstract algebra</a> to describe <a href="/2017/10/05/monoids-semigroups-and-friends">monoids, semigroups, et cetera</a>. Thus, the article series turned out to have little to do with category theory, but I couldn't (wouldn't) change the title and the URL of the original article, since it was already published. </p> <p> The same may be the case with the terms <em>invariant functors</em> and <em>monomorphic functors</em>. Perhaps <em>invariant functors</em> should instead have been called <em>isomorphic functors</em>... </p> </div> <div class="comment-date">2021-09-14 8:22 UTC</div> </div> <div class="comment" id="fb3ba7e051c64b8fa2606a14085205e2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fb3ba7e051c64b8fa2606a14085205e2">#</a></div> <div class="comment-content"> <p> I've started <a href="/2022/08/01/invariant-functors">a small article series on invariant functors</a>. </p> </div> <div class="comment-date">2022-08-01 5:58 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Subjectivity https://blog.ploeh.dk/2020/10/12/subjectivity 2020-10-12T07:40:00+00:00 Mark Seemann <div id="post"> <p> <em>A subjective experience can be sublime, but it isn't an argument.</em> </p> <p> I once wrote <a href="https://amzn.to/36xLycs">a book</a>. One of the most common adjectives people used about it was <em>opinionated</em>. That's okay. It is, I think, a ramification of <a href="/2020/04/13/curb-code-rot-with-thresholds#6c5e4b0817f742bf9ecc0a9050ecb7b5">the way I write</a>. I prioritise writing so that you understand what I mean, and why I mean it. You can call that opinionated if you like. </p> <p> This means that I frequently run into online arguments, mostly on Twitter. This particularly happens when I write something that readers don't like. </p> <h3 id="6a04cf26fdfc483db096de13451a7273"> The sublime <a href="#6a04cf26fdfc483db096de13451a7273" title="permalink">#</a> </h3> <p> Whether or not you <em>like</em> something is a subjective experience. I have nothing against subjective experiences. </p> <p> Subjective experiences fuel art. Before I became a programmer, I wanted to be an artist. I've loved <a href="https://en.wikipedia.org/wiki/Bande_dessin%C3%A9e">French-Belgian</a> comics for as long as I remember (my favourites have always been <a href="https://en.wikipedia.org/wiki/Hermann_Huppen">Hermann</a> and <a href="https://en.wikipedia.org/wiki/Jean_Giraud">Jean Giraud</a>), and until I gave up in my mid-twenties, I wanted to become a graphic novelist. </p> <p> <img src="/content/binary/graphic-novel-pages.jpg" alt="Spread of original pages of my graphic novel."> </p> <p> This interest in drawing also fuelled a similar interest in paintings and art in general. I've had many sublime experiences in front of a work of art in various museums around the world. These were all subjective experiences that were precious to me. </p> <p> For as long as I've been interested in graphic arts, I've also loved music. I've worn <a href="https://en.wikipedia.org/wiki/LP_record">LPs</a> thin in the 1970s and '80s. I've played guitar and wanted to be a rock star. I regularly get goosebumps from listing to outstanding music. These are all subjective experiences. </p> <p> Having an experience is part of the human condition. </p> <p> How you <em>feel</em> about something, however, isn't an argument. </p> <h3 id="c60fc277c32d4e67be30f81d7434da51"> Arguing about subjective experience <a href="#c60fc277c32d4e67be30f81d7434da51" title="permalink">#</a> </h3> <p> I rarely read music reviews. How you experience music is such a personal experience that I find it futile to argue about it. Maybe you share my feelings that <a href="https://en.wikipedia.org/wiki/Army_of_Me">Army of Me</a> is a sublime track, or maybe it leaves you cold. If you don't like that kind of music, I can't argue that you should. </p> <p> Such a discussion generally takes place at the kindergarten level: <em>"Army of Me is a great song!</em>, <em>"No it isn't,"</em> <em>"Yes it is,"</em> and so on. </p> <p> This doesn't mean that we can't talk about music (or, by extension, other subjective experiences). Talking about music can be exhilarating, particularly when you meet someone who shares your taste. Finding common ground, exploring and inspiring each other, learning about artists you didn't know about, is wonderful. It forges bonds between people. I suppose it's the same mechanism in which sports fans find common ground. </p> <p> Sharing subjective experience may not be entirely futile. You can't use your <em>feelings</em> as an argument, but explaining <em>why</em> you feel a certain way can be illuminating. This is the realm of much art criticism. A good, popular and easily digestible example is the <a href="https://strongsongspodcast.com">Strong Songs podcast</a>. The host, Kirk Hamilton, likes plenty of music that I don't. I must admit, though, that his <a href="https://strongsongspodcast.com/satisfied-from-hamilton">exigesis of <em>Satisfied</em> from Hamilton</a> left me with more appreciation for that song than I normally have for that genre. </p> <p> This is why, in a technical argument, when people say, <em>"I don't like it"</em>, I ask <em>why?</em> </p> <p> Your <em>preference</em> doesn't convince me to change my mind, but if you can explain your perspective, it may make me consider a point of which I wasn't aware. </p> <h3 id="8759a0fb5e2f496ebc7d264072906648"> It Depends™ <a href="#8759a0fb5e2f496ebc7d264072906648" title="permalink">#</a> </h3> <p> Among other roles, I consult. Consultants are infamous for saying <em>it depends</em>, and obviously, if that's all we say, it'd be useless. </p> <p> Most advice is given in a context. When a consultant starts a sentence with <em>it depends</em>, he or she must continue by explaining on what it depends. Done right, this can be tremendously valuable, because it gives the customer a framework they can use to make decisions. </p> <p> In the same family as <em>it depends</em>, I often run into arguments like <em>nothing is black or white</em>, <em>it's a trade-off</em>, or other platitudes. Okay, it probably <em>is</em> a trade-off, but between <em>what?</em> </p> <p> If it's a trade-off between my reasoned argument on the one hand, and someone's preference on the other hand, my reasoned argument wins. </p> <p> Why? Because I can always trump with <em>my</em> preference as well. </p> <h3 id="6dd1b6841a5d42c8b295432859c5187d"> Conclusion <a href="#6dd1b6841a5d42c8b295432859c5187d" title="permalink">#</a> </h3> <p> Our subjective experiences are important pieces of our lives. I hope that my detour around my love of art highlighted that I consider subjective experiences vital. </p> <p> When we discuss how to do things, however, a <em>preference</em> isn't an argument. You may prefer to do things in one way, and I another. Such assertions, alone, don't move the discussion anywhere. </p> <p> What we <em>can</em> do is to attempt to uncover the underlying causes for our preferences. There's no guarantee that we'll reach enlightenment, but it's certain that we'll get nowhere if we don't. </p> <p> That's the reason that, when someone states <em>"I prefer"</em>, I often ask <em>why?</em> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f826ff3d591f4d6ebb1e7d93c00cea3a"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#f826ff3d591f4d6ebb1e7d93c00cea3a">#</a></div> <div class="comment-content"> <blockquote> <p> Consultants are infamous for saying <em>it depends</em>, and obviously, if that's all we say, it'd be useless. </p> <p> Most advice is given in a context. When a consultant starts a sentence with <em>it depends</em>, he or she must continue by explaining on what it depends. Done right, this can be tremendously valuable, because it gives the customer a framework they can use to make decisions. </p> </blockquote> <p> I have a vague memory of learning this through some smoothly worded saying. As I recall, it went something like this. </p> <blockquote> Anyone can say "It depends", but an expert can say on what it depends. </blockquote> <p> I don't think that is the exact phrase, because I don't think it sounds very smooth. Does anyone know of a succinct and smooth mnemonic phrase that captures this idea? </p> </div> <div class="comment-date">2020-10-12 13:54 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fortunately, I don't squash my commits https://blog.ploeh.dk/2020/10/05/fortunately-i-dont-squash-my-commits 2020-10-05T06:00:00+00:00 Mark Seemann <div id="post"> <p> <em>The story of a bug, and how I addressed it.</em> </p> <p> Okay, I admit it: I could have given this article all sorts of alternative titles, each of which would have made as much sense as the one I chose. I didn't want to go with some of the other titles I had in mind, because they would give it all away up front. I didn't want to spoil the surprise. </p> <p> I recently ran into this bug, it took me hours to troubleshoot it, and I was appalled when I realised what the problem was. </p> <p> This is the story of that bug. </p> <p> There are several insights from this story, and I admit that I picked the most click-baity one for the title. </p> <h3 id="94bc204499df483897121bb0343f73f7"> Yak shaving <a href="#94bc204499df483897121bb0343f73f7" title="permalink">#</a> </h3> <p> I was working on the umpteenth variation of an online restaurant reservations system, and one of the features I'd added was a schedule only visible to the <a href="https://en.wikipedia.org/wiki/Ma%C3%AEtre_d%27h%C3%B4tel">maître d'</a>. The schedule includes a list of all reservations for a day, including guests' email addresses and so on. For that reason, I'd protected that resource by requiring a valid <a href="https://en.wikipedia.org/wiki/JSON_Web_Token">JSON Web Token</a> (JWT) with an appropriate role. </p> <p> <ins datetime="2021-07-19T09:08Z">The code discussed here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <p> I'd deployed a new version of the API and went for an ad-hoc test. To my surprise, that particular resource didn't work. When I tried to request it, I got a <code>403 Forbidden</code> response. </p> <p> "That's odd," I though, "it worked the last time I tried this." </p> <p> The system is set up with continuous deployment. I push <em>master</em> to a remote repository, and a build pipeline takes over from there. It only deploys the new version if all tests pass, so my first reaction was that I might have made a mistake with the JWT. </p> <p> I wasted significant time decoding the JWT and comparing its contents to what it was supposed to be. I couldn't find any problems. </p> <p> I also meticulously compared the encryption key I'd used to sign the JWT with the key on the server. They were identical. </p> <p> Incredulous, and running out of ideas, I tried running all tests on my development machine. Indeed, all 170 tests passed. </p> <p> Finally, I gave up and ran the API on my development machine. It takes all of a 30 seconds to configure the code to run in that environment, so you're excused if you wonder why I didn't already do that. What can I say? I've almost two decades of experience with automated test suites. Usually, if all tests pass, the problem is environmental: a network topology issue, a bad or missing connection string, a misconfigured encryption key, an invalid JWT, things like that. </p> <p> To my surprise, the problem also manifested on my machine. </p> <h3 id="b726532ae06844eb8aeaa2c27ca0d913"> Not my code <a href="#b726532ae06844eb8aeaa2c27ca0d913" title="permalink">#</a> </h3> <p> Okay, even with hundreds of tests, perhaps some edge case went unnoticed. The only problem with that hypothesis was that this was hardly an edge case. I was only making a <code>GET</code> request with a <code>Bearer</code> token. I wasn't going through some convoluted sequence of steps. </p> <p> <pre>GET /restaurants/1/schedule/2020/9/30 HTTP/1.1 Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c[...]</pre> </p> <p> I expected a successful response containing some JSON, but the result was <code>403 Forbidden</code>. That was the same behaviour I saw in the production environment. </p> <p> Now, to be clear, this is indeed a protected resource. If you present an invalid JWT, <code>403 Forbidden</code> is the expected response. That's why I wasted a few hours looking for problems with the the JWT. </p> <p> I finally, hesitatingly, concluded that the problem might be somewhere else. The JWT looked okay. So, hours into my troubleshooting I reluctantly set a breakpoint in my code and started the debugger. It isn't rational, but I tend to see it as a small personal defeat if I have to use the debugger. Even so, if used judiciously, it can be an effective way to identify problems. </p> <p> The debugger never hit my breakpoint. </p> <p> To be clear, the beginning of my Controller method looked like this: </p> <p> <pre>[<span style="color:#2b91af;">Authorize</span>(Roles&nbsp;=&nbsp;<span style="color:#a31515;">&quot;MaitreD&quot;</span>)] [<span style="color:#2b91af;">HttpGet</span>(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/schedule/{year}/{month}/{day}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Get( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;restaurantId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;year, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;month, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;day) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!AccessControlList.Authorize(restaurantId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ForbidResult</span>();</pre> </p> <p> My breakpoint was on the first line (the <code>if</code> conditional), but the debugger didn't break into it. When I issued my <code>GET</code> request, I immediately got the <code>403 Forbidden</code> response. The breakpoint just sat there in the debugger, mocking me. </p> <p> When that happens, it's natural to conclude that the problem occurs somewhere in the framework; in this case, ASP.NET. To test that hypothesis, I commented out the <code>[Authorize]</code> attribute and reissued the <code>GET</code> request. My hypothesis was that I'd get a <code>200 OK</code> response, since the attribute is what tells ASP.NET to check authorisation. </p> <p> The hypothesis held. The response was <code>200 OK</code>. </p> <h3 id="fdff404f12644a898b6a3e695fb6533f"> Test interdependency <a href="#fdff404f12644a898b6a3e695fb6533f" title="permalink">#</a> </h3> <p> I hate when that happens. It's up there with fixing other people's printers. The problem is in the framework, not in my code. I didn't have any authorisation callbacks registered, so I was fairly certain that the problem wasn't in my code. </p> <p> I rarely jump to the conclusion that there's a bug in the framework. In my experience, <a href="https://blog.codinghorror.com/the-first-rule-of-programming-its-always-your-fault">select is rarely broken</a>. My new hypothesis had to be that I'd somehow managed to misconfigure the framework. </p> <p> But where? There were automated tests that verified that a client could request that resource with a valid JWT. There were other automated tests that verified what happened if you presented an invalid JWT, or none at all. And all tests were passing. </p> <p> While I was fiddling with the tests, I eventually ran a parametrised test by itself, instead of the entire test suite: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Hipgnosta&quot;</span>,&nbsp;2024,&nbsp;11,&nbsp;&nbsp;2)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Nono&quot;</span>,&nbsp;2018,&nbsp;&nbsp;9,&nbsp;&nbsp;9)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;The&nbsp;Vatican&nbsp;Cellar&quot;</span>,&nbsp;2021,&nbsp;10,&nbsp;10)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;GetRestaurantScheduleWhileAuthorized( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;year, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;month, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;day)</pre> </p> <p> This parametrised test has three test cases. When I ran just that test method, two of the test cases passed, but one failed: the <em>Nono</em> case, for some reason I haven't yet figured out. </p> <p> I didn't understand why that test case ought to fail while the others succeeded, but I had an inkling. I commented out the <em>Nono</em> test case and ran the test method again. </p> <p> One passed and one failing test. </p> <p> Now <em>The Vatican Cellar</em> test case was failing. I commented that out and ran the test method again. The remaining test case failed. </p> <p> This reeks of some sort of test interdependency. Apparently, <em>something</em> happens during the first test run that makes succeeding tests pass, but it happens too late for the first one. But what? </p> <h3 id="c95c3e707f46467abbf8ff02a9594c4c"> Bisect <a href="#c95c3e707f46467abbf8ff02a9594c4c" title="permalink">#</a> </h3> <p> Then something occurred to me that I should have thought of sooner. This feature used to work. Not only had the tests been passing, but I'd actually interacted with the deployed service, presenting a valid JWT and received a <code>200 OK</code> response. </p> <p> Once than dawned on me, I realised that it was just a manner of performing a binary search. Since, of course, I use version control, I had a version I knew worked, and a version that didn't work. The task, then, was to find the commit that introduced the defect. </p> <p> As I've already implied, the system in question is an example code base. While I have a cloud-based production environment, none but I use it. It had been four or five days since I'd actually interacted with the real service, and I'd been busy making changes, trusting exclusively in my test suite. I tend to make frequent, small commits instead of big, infrequent commits, so I had accumulated about a hundred and fifty commits since the 'last known good' deployment. </p> <p> Searching through hundreds of commits sounds overwhelming, but using binary search, it's actually not that bad. Pick the commit halfway between the 'last known good' commit and the most recent commit, and check it out. See if the defect is present there. If it is, you know that it was introduced somewhere between the commit you're looking at, and the 'last known good' commit. If it isn't present, it was introduced later. Regardless of the outcome, you know in which half to look. You now pick a new commit in the middle of that set and repeat the exercise. Even with, say, a hundred commits, the first bisection reduces the candidate set to 50, the next bisection to 25, then 13, then 7, 4, 2, and then you have it. If you do this systematically, you should find the exact commit in less than eight iterations. </p> <p> This is, as far as I understand it, the algorithm used by <em>Git bisect</em>. You don't have to use the <code>bisect</code> command - the algorithm is easy enough to do by hand - but let's see how it works. </p> <p> You start a <code>bisect</code> session with: </p> <p> <pre><span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((93c6c35...))</span> $ git bisect start <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((93c6c35...)|BISECTING)</span></pre> </p> <p> This starts an interactive session, which you can tell from the Git integration in Git Bash (it says <code>BISECTING</code>). You now mark a commit as being bad: </p> <p> <pre>$ git bisect bad <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((93c6c35...)|BISECTING)</span></pre> </p> <p> If you don't provide a commit ID at that point, Git is going to assume that you meant that the current commit (in this case <code>93c6c35</code>) is bad. That's what I had in mind, so that's fine. </p> <p> You now tell it about a commit ID that you know is good: </p> <p> <pre>$ git bisect good 7dfdab2 Bisecting: 75 revisions left to test after this (roughly 6 steps) [1f78c9a90c2088423ab4fc145b7b2ec3859d6a9a] Use InMemoryRestaurantDatabase in a test <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((1f78c9a...)|BISECTING)</span></pre> </p> <p> Notice that Git is already telling us how many iterations we should expect. You can also see that it checked out a new commit (<code>1f78c9a</code>) for you. That's the half-way commit. </p> <p> At this point, I manually ran the test method with the three test cases. All three passed, so I marked that commit as good: </p> <p> <pre>$ git bisect good Bisecting: 37 revisions left to test after this (roughly 5 steps) [5abf65a72628efabbf05fccd1b79340bac4490bc] Delete Either API <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((5abf65a...)|BISECTING)</span></pre> </p> <p> Again, Git estimates how many more steps are left and checks out a new commit (<code>5abf65a</code>). </p> <p> I repeated the process for each step, marking the commit as either good or bad, depending on whether or not the test passed: </p> <p> <pre>$ git bisect bad Bisecting: 18 revisions left to test after this (roughly 4 steps) [fc48292b0d654f4f20522710c14d7726e6eefa70] Delete redundant Test Data Builders <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((fc48292...)|BISECTING)</span> $ git bisect good Bisecting: 9 revisions left to test after this (roughly 3 steps) [b0cb1f5c1e9e40b1dabe035c41bfb4babfbe4585] Extract WillAcceptUpdate helper method <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((b0cb1f5...)|BISECTING)</span> $ git bisect good Bisecting: 4 revisions left to test after this (roughly 2 steps) [d160c57288455377f8b0ad05985b029146228445] Extract ConfigureClock helper method <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((d160c57...)|BISECTING)</span> $ git bisect good Bisecting: 2 revisions left to test after this (roughly 1 step) [4cb73c219565d8377aa67d79024d6836f9000935] Compact code <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((4cb73c2...)|BISECTING)</span> $ git bisect good Bisecting: 0 revisions left to test after this (roughly 1 step) [34238c7d2606e9007b96b54b43e678589723520c] Extract CreateTokenValidationParameters method <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((34238c7...)|BISECTING)</span> $ git bisect bad Bisecting: 0 revisions left to test after this (roughly 0 steps) [7d6583a97ff45fbd85878cecb5af11d93213a25d] Move Configure method up <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((7d6583a...)|BISECTING)</span> $ git bisect good 34238c7d2606e9007b96b54b43e678589723520c is the first bad commit <span style="color: orange">commit 34238c7d2606e9007b96b54b43e678589723520c</span> Author: Mark Seemann &lt;mark@example.com&gt; Date: Wed Sep 16 07:15:12 2020 +0200 Extract CreateTokenValidationParameters method Restaurant.RestApi/Startup.cs | 32 <span style="color: green">+++++++++++++++++++</span><span style="color: red">-------------</span> 1 file changed, 19 insertions(+), 13 deletions(-) <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((7d6583a...)|BISECTING)</span></pre> </p> <p> Notice that the last step finds the culprit. It tells you which commit is the bad one, but surprisingly doesn't check it out for you. You can do that using a label Git has created for you: </p> <p> <pre>$ git checkout refs/bisect/bad Previous HEAD position was 7d6583a Move Configure method up HEAD is now at 34238c7 Extract CreateTokenValidationParameters method <span style="color: green">mark@Vindemiatrix</span> <span style="color: purple">MINGW64</span> <span style="color: orange">~/Documents/Redacted/Restaurant</span> <span style="color: darkturquoise">((34238c7...)|BISECTING)</span></pre> </p> <p> You've now found and checked out the offending commit. Hopefully, the changes in that commit should give you a clue about the problem. </p> <h3 id="2f3c813d0cbf47eabb52467cff0c7692"> The culprit <a href="#2f3c813d0cbf47eabb52467cff0c7692" title="permalink">#</a> </h3> <p> What's in that commit? Take a gander: </p> <p> <pre>$ git show <span style="color: orange">commit 34238c7d2606e9007b96b54b43e678589723520c (</span><span style="color: mediumturquoise">HEAD</span><span style="color: orange">,</span> refs/bisect/bad<span style="color: orange">)</span> Author: Mark Seemann &lt;mark@example.com&gt; Date: Wed Sep 16 07:15:12 2020 +0200 Extract CreateTokenValidationParameters method <strong>diff --git a/Restaurant.RestApi/Startup.cs b/Restaurant.RestApi/Startup.cs index 6c161b5..bcde861 100644 --- a/Restaurant.RestApi/Startup.cs +++ b/Restaurant.RestApi/Startup.cs</strong> <span style="color: darkturquoise">@@ -79,10 +79,6 @@</span> namespace Ploeh.Samples.Restaurants.RestApi private void ConfigureAuthorization(IServiceCollection services) { <span style="color: red">- JwtSecurityTokenHandler.DefaultMapInboundClaims = false; - - var secret = Configuration["JwtIssuerSigningKey"]; -</span> services.AddAuthentication(opts =&gt; { opts.DefaultAuthenticateScheme = <span style="color: darkturquoise">@@ -91,15 +87,8 @@</span> namespace Ploeh.Samples.Restaurants.RestApi JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(opts =&gt; { <span style="color: red">- opts.TokenValidationParameters = new TokenValidationParameters - { - ValidateIssuerSigningKey = true, - IssuerSigningKey = new SymmetricSecurityKey( - Encoding.ASCII.GetBytes(secret)), - ValidateIssuer = false, - ValidateAudience = false, - RoleClaimType = "role" - };</span> <span style="color: green">+ opts.TokenValidationParameters = + CreateTokenValidationParameters();</span> opts.RequireHttpsMetadata = false; }); <span style="color: darkturquoise">@@ -108,6 +97,23 @@</span> namespace Ploeh.Samples.Restaurants.RestApi sp.GetService&lt;IHttpContextAccessor&gt;().HttpContext.User)); } <span style="color: green">+ private TokenValidationParameters CreateTokenValidationParameters() + { + JwtSecurityTokenHandler.DefaultMapInboundClaims = false; + + var secret = Configuration["JwtIssuerSigningKey"]; + + return new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey( + Encoding.ASCII.GetBytes(secret)), + ValidateIssuer = false, + ValidateAudience = false, + RoleClaimType = "role" + }; + } +</span> private void ConfigureRepository(IServiceCollection services) {</pre> </p> <p> Can you spot the problem? </p> <p> As soon as I saw that diff, the problem almost jumped out at me. I'm so happy that I make small commits. What you see here is, I promise, the entire diff of that commit. </p> <p> Clearly, you're not familiar with this code base, but even so, you might be able to intuit the problem from the above diff. You don't need domain-specific knowledge for it, or knowledge of the rest of the code base. </p> <p> I'll give you a hint: I moved a line of code into a lambda expression. </p> <h3 id="1cfcd1adb0b24b6098aa250c73f5cba2"> Deferred execution <a href="#1cfcd1adb0b24b6098aa250c73f5cba2" title="permalink">#</a> </h3> <p> In the above commit, I extracted a helper method that I called <code>CreateTokenValidationParameters</code>. As the name communicates, it creates JWT validation parameters. In other words, it supplies the configuration values that determine how ASP.NET figures out how to authorise an HTTP request. </p> <p> Among other things, it sets the <code>RoleClaimType</code> property to <code>"role"</code>. By default, the <code>[Authorize]</code> attribute is looking for a role claim with a rather complicated identifier. Setting the <code>RoleClaimType</code> enables you to change the identifier. By setting it to <code>"role"</code>, I'm telling the ASP.NET framework that it should look for claims named <code>role</code> in the JWT, and these should be translated to role claims. </p> <p> Except that it doesn't work. </p> <p> Ostensibly, this is for backwards compatibility reasons. You have to set <a href="https://docs.microsoft.com/dotnet/api/system.identitymodel.tokens.jwt.jwtsecuritytokenhandler.defaultmapinboundclaims">JwtSecurityTokenHandler.DefaultMapInboundClaims</a> to <code>false</code>. Notice that this is a global variable. </p> <p> I thought that the line of code that does that conceptually belongs together with the other code that configures the JWT validation parameters, so I moved it in there. I didn't think much about it, and all my tests were still passing. </p> <p> What happened, though, was that I moved mutation of a global variable into a helper method that was called from a lambda expression. Keep in mind that lambda expressions represent code that may run later; that execution is deferred. </p> <p> By moving that mutation statement into the helper method, I inadvertently deferred its execution. When the application's <code>Startup</code> code runs, it configures the service. All the code that runs inside of <code>AddJwtBearer</code>, however, doesn't run immediately; it runs when needed. </p> <p> This explains why all tests were passing. My test suite has plenty of self-hosted integration tests in the style of <a href="/outside-in-tdd">outside-in test-driven development</a>. When I ran all the tests, the deferred code block would run in some other test context, flipping that global bit as a side effect. When the test suite reaches the test that fails when run in isolation, the bit is already flipped, and then it works. </p> <p> It took me hours to figure out what the problem was, and it turned out that the root cause was a global variable. </p> <p> Global variables are evil. Who knew? </p> <h3 id="cbf70c019a264e8fa861e13536db688f"> Can't reproduce <a href="#cbf70c019a264e8fa861e13536db688f" title="permalink">#</a> </h3> <p> Once you figure out what the problem is, you should reproduce it with an automated test. </p> <p> Yes, and how do I do that here? I already had tests that verify that you can <code>GET</code> the desired resource if you present a valid JWT. And <em>that test passes!</em> </p> <p> The only way I can think of to reproduce the issue with an automated test is to create a completely new, independent test library with only one test. I considered doing that, weighed the advantages against the disadvantages and decided, given the context, that it wasn't worth the effort. </p> <p> That means, though, that I had to accept that within the confines of my existing testing strategy, I can't reproduce the defect. This doesn't happen to me often. In fact, I can't recall that it's ever happened to me before, like this. </p> <h3 id="2ae70aa4524e48bf9b0190aeaa158192"> Resolution <a href="#2ae70aa4524e48bf9b0190aeaa158192" title="permalink">#</a> </h3> <p> It took me hours to find the bug, and ten seconds to fix it. I just moved the mutation of <code>JwtSecurityTokenHandler.DefaultMapInboundClaims</code> back to the <code>ConfigureAuthorization</code> method: </p> <p> <pre><strong>diff --git a/Restaurant.RestApi/Startup.cs b/Restaurant.RestApi/Startup.cs index d4917f5..e40032f 100644 --- a/Restaurant.RestApi/Startup.cs +++ b/Restaurant.RestApi/Startup.cs</strong> <span style="color: darkturquoise">@@ -79,6 +79,7 @@</span> namespace Ploeh.Samples.Restaurants.RestApi private void ConfigureAuthorization(IServiceCollection services) { <span style="color: green">+ JwtSecurityTokenHandler.DefaultMapInboundClaims = false;</span> services.AddAuthentication(opts =&gt; { opts.DefaultAuthenticateScheme = <span style="color: darkturquoise">@@ -99,8 +100,6 @@</span> namespace Ploeh.Samples.Restaurants.RestApi private TokenValidationParameters CreateTokenValidationParameters() { <span style="color: red">- JwtSecurityTokenHandler.DefaultMapInboundClaims = false; -</span> var secret = Configuration["JwtIssuerSigningKey"]; return new TokenValidationParameters</pre> </p> <p> An ad-hoc smoke test verified that this solved the problem. </p> <h3 id="55a0017f1aab47b6b9049e63d51d79ff"> Conclusion <a href="#55a0017f1aab47b6b9049e63d51d79ff" title="permalink">#</a> </h3> <p> There are multiple insights to be had from this experience: <ul> <li>Global variables are evil</li> <li>Binary search, or bisection, is useful when troubleshooting</li> <li>Not all bugs can be reproduced by an automated test</li> <li>Small commits make it easy to detect problems</li> </ul> I've always disliked Git's <em>squash</em> feature, and here's one of the many reasons to dislike it. Had this happened in a code base with a 'nice history' (as the <em>squash</em> proponents like to present it), that small commit would have been bundled with various other commits. The problem wouldn't have jumped at me if buried in dozens of other changes. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. EnsureSuccessStatusCode as an assertion https://blog.ploeh.dk/2020/09/28/ensuresuccessstatuscode-as-an-assertion 2020-09-28T05:50:00+00:00 Mark Seemann <div id="post"> <p> <em>It finally dawned on me that EnsureSuccessStatusCode is a fine test assertion.</em> </p> <p> Okay, I admit that sometimes I can be too literal and rigid in my thinking. At least as far back as 2012 I've been doing <a href="/outside-in-tdd">outside-in TDD</a> against self-hosted HTTP APIs. When you do that, you'll regularly need to verify that an HTTP response is successful. </p> <h3 id="77cb250db1ab4278bad68cf7736ae62e"> Assert equality <a href="#77cb250db1ab4278bad68cf7736ae62e" title="permalink">#</a> </h3> <p> You can, of course, write an assertion like this: </p> <p> <pre><span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#2b91af;">HttpStatusCode</span>.OK,&nbsp;response.StatusCode);</pre> </p> <p> If the assertion fails, it'll produce a comprehensible message like this: </p> <p> <pre>Assert.Equal() Failure Expected: OK Actual: BadRequest</pre> </p> <p> What's not to like? </p> <h3 id="2ba5bb17d1f8423184a50b2fa1fcb667"> Assert success <a href="#2ba5bb17d1f8423184a50b2fa1fcb667" title="permalink">#</a> </h3> <p> The problem with testing strict equality in this context is that it's often too narrow a definition of success. </p> <p> You may start by returning <code>200 OK</code> as response to a <code>POST</code> request, but then later evolve the API to return <code>201 Created</code> or <code>202 Accepted</code>. Those status codes still indicate success, and are typically accompanied by more information (e.g. a <code>Location</code> header). Thus, evolving a REST API from returning <code>200 OK</code> to <code>201 Created</code> or <code>202 Accepted</code> isn't a breaking change. </p> <p> It does, however, break the above assertion, which may now fail like this: </p> <p> <pre>Assert.Equal() Failure Expected: OK Actual: Created</pre> </p> <p> You could attempt to fix this issue by instead verifying <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpresponsemessage.issuccessstatuscode">IsSuccessStatusCode</a>: </p> <p> <pre><span style="color:#2b91af;">Assert</span>.True(response.IsSuccessStatusCode);</pre> </p> <p> That permits the API to evolve, but introduces another problem. </p> <h3 id="0389a3d633c64aec8068be619ddce54c"> Legibility <a href="#0389a3d633c64aec8068be619ddce54c" title="permalink">#</a> </h3> <p> What happens when an assertion against <code>IsSuccessStatusCode</code> fails? </p> <p> <pre>Assert.True() Failure Expected: True Actual: False</pre> </p> <p> That's not helpful. At least, you'd like to know which other status code was returned instead. You can address that concern by using an overload to <code>Assert.True</code> (most unit testing frameworks come with such an overload): </p> <p> <pre><span style="color:#2b91af;">Assert</span>.True( &nbsp;&nbsp;&nbsp;&nbsp;response.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{response.StatusCode}<span style="color:#a31515;">.&quot;</span>);</pre> </p> <p> This produces more helpful error messages: </p> <p> <pre>Actual status code: BadRequest. Expected: True Actual: False</pre> </p> <p> Granted, the <em>Expected: True, Actual: False</em> lines are redundant, but at least the information you care about is there; in this example, the status code was <code>400 Bad Request</code>. </p> <h3 id="9697ab68967948f89792b82d00028ccf"> Conciseness <a href="#9697ab68967948f89792b82d00028ccf" title="permalink">#</a> </h3> <p> That's not bad. You can use that <code>Assert.True</code> overload everywhere you want to assert success. You can even copy and paste the code, <a href="/2014/08/07/why-dry">because it's an idiom that doesn't change</a>. Still, since <a href="/2019/11/04/the-80-24-rule">I like to stay within 80 characters line width</a>, this little phrase takes up three lines of code. </p> <p> Surely, a helper method can address that problem: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AssertSuccess(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">HttpResponseMessage</span>&nbsp;response) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.IsSuccessStatusCode, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">$&quot;Actual&nbsp;status&nbsp;code:&nbsp;</span>{response.StatusCode}<span style="color:#a31515;">.&quot;</span>); }</pre> </p> <p> I can now write a one-liner as an assertion: </p> <p> <pre>response.AssertSuccess();</pre> </p> <p> What's not to like? </p> <h3 id="6990f2fbc33f4beca15c122b5b4ccc22"> Carrying coals to Newcastle <a href="#6990f2fbc33f4beca15c122b5b4ccc22" title="permalink">#</a> </h3> <p> If you're already familiar with the <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpresponsemessage">HttpResponseMessage</a> class, you may have been reading this far with some frustration. It already comes with a method called <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpresponsemessage.ensuresuccessstatuscode">EnsureSuccessStatusCode</a>. Why not just use that instead? </p> <p> <pre>response.EnsureSuccessStatusCode();</pre> </p> <p> As long as the <code>response</code> status code is in the 200 range, this method call succeeds, but if not, it throws an exception: </p> <p> <pre>Response status code does not indicate success: 400 (Bad Request).</pre> </p> <p> That's exactly the information you need. </p> <p> I admit that I can sometimes be too rigid in my thinking. I've known about this method for years, but I never thought of it as a <em>unit testing assertion</em>. It's as if there's a category in my programming brain that's labelled <em>unit testing assertions</em>, and it only contains methods that originate from unit testing. </p> <p> I even knew about <a href="https://docs.microsoft.com/dotnet/api/system.diagnostics.debug.assert">Debug.Assert</a>, so I knew that assertions also exist outside of unit testing. That assertion isn't suitable for unit testing, however, so I never included it in my category of unit testing assertions. </p> <p> The <code>EnsureSuccessStatusCode</code> method doesn't have <em>assert</em> in its name, but it behaves just like a unit testing assertion: it throws an exception with a useful error message if a criterion is not fulfilled. I just never thought of it as useful for unit testing purposes because it's part of 'production code'. </p> <h3 id="5ff49213dd304440887719d6879ff7fb"> Conclusion <a href="#5ff49213dd304440887719d6879ff7fb" title="permalink">#</a> </h3> <p> The built-in <code>EnsureSuccessStatusCode</code> method is fine for unit testing purposes. Who knows, perhaps other class libraries contain similar assertion methods, although they may be hiding behind names that don't include <em>assert</em>. Perhaps your own production code contains such methods. If so, they can double as unit testing assertions. </p> <p> I've been doing test-driven development for close to two decades now, and one of many consistent experiences is this: <ul> <li>In the beginning of a project, the unit tests I write are verbose. This is natural, because I'm still getting to know the domain.</li> <li>After some time, I begin to notice patterns in my tests, so I refactor them. I introduce helper APIs in the test code.</li> <li>Finally, I realise that some of those unit testing APIs might actually be useful in the production code too, so I move them over there.</li> </ul> Test-driven development is process that gives you feedback about your production code's APIs. Listen to your tests. </p> <p> <ins datetime="2021-07-21T19:09Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="20484094920349b7a91eebcb9b15c628"> <div class="comment-author"><a href="https://github.com/YevgeniyShunevych">Yevgeniy Shunevych</a> <a href="#20484094920349b7a91eebcb9b15c628">#</a></div> <div class="comment-content"> <p> Unfortunately, I can't agree that it's fine to use <code>EnsureSuccessStatusCode</code> or other similar methods for assertions. An exception of a kind different from assertion (<code>XunitException</code> for XUnit, <code>AssertionException</code> for NUnit) is handled in a bit different way by the testing framework. <code>EnsureSuccessStatusCode</code> method throws <code>HttpRequestException</code>. Basically, a test that fails with such exception is considered as "Failed by error". </p> <p> For example, the test result message in case of 404 status produced by the <code>EnsureSuccessStatusCode</code> method is: </p> <p> <pre>System.Net.Http.HttpRequestException : Response status code does not indicate success: 404 (Not Found).</pre> </p> <p> Where "System.Net.Http.HttpRequestException :" text looks redundant, as we do assertion. This also may make other people think that the reason for this failure is not an assertion, but an unexpected exception. In case of regular assertion exception type, that text is missing. </p> <p> Other points are related to NUnit: <ul> <li> There are 2 different failure test outcome kinds in NUnit: <ul> <li>Failure: a test assertion failed. (Status=Failed, Label=empty)</li> <li>Error: an unexpected exception occurred. (Status=Failed, Label=Error)</li> </ul> See NUnit <a href="https://docs.nunit.org/articles/nunit/writing-tests/TestContext.html#common-outcomes">Common Outcomes</a>. Thus `EnsureSuccessStatusCode` produces "Error" status instead of desirable "Failure". </li> <li>You can't use methods like <code>EnsureSuccessStatusCode</code> as assertion inside multiple asserts.</li> <li>NUnit tracks the count of assertions for each test. <code>"assertions"</code> property gets into the test results XML file and might be useful. This property increments on assertion methods, <code>EnsureSuccessStatusCode</code> - obviously doesn't increment it.</li> </ul> </p> </div> <div class="comment-date">2020-09-30 15:25 UTC</div> </div> <div class="comment" id="16adadb969f1406c9c18605c8c85ef83"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#16adadb969f1406c9c18605c8c85ef83">#</a></div> <div class="comment-content"> <p> Yevgeniy, thank you for writing. This shows, I think, that all advice is contextual. It's been more than a decade since I regularly used NUnit, but I vaguely recall that it gives special treatment to its own assertion exceptions. </p> <p> I also grant that the "System.Net.Http.HttpRequestException" part of the message is redundant. To make matters worse, even without that 'prologue' the information that we care about (the actual status code) is all the way to the right. It'll often require horizontal scrolling to see it. </p> <p> That doesn't much bother me, though. I think that one should optimise for the most frequent scenario. For example, I favour taking a bit of extra time to write readable code, because <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">the bottleneck in programming isn't typing</a>. In the same vein, I find it a good trade-off being able to avoid adding more code against an occasional need to scroll horizontally. The cost of code isn't the time it takes to write it, but the maintenance burden it imposes, as well as the cognitive load it adds. </p> <p> The point here is that while horizontal scrolling is inconvenient, the <em>most frequent scenario</em> is that tests don't fail. The inconvenience only manifests in the rare circumstance that a test fails. </p> <p> I want to thank you, however, for pointing out the issues with NUnit. It reinforces the impression I already had of it, as a framework to stay away from. That design seems to me to impose a lock-in that prevents you from mixing NUnit with improved APIs. </p> <p> In contrast, the <a href="https://www.nuget.org/packages/xunit.core">xunit.core</a> library doesn't include <a href="https://www.nuget.org/packages/xunit.assert">xunit.assert</a>. I often take advantage of that when I write unit tests in F#. While the xUnit.net assertion library is adequate, <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> is much better for F#. I appreciate that xUnit.net enables me to combine the tools that work best in a given situation. </p> </div> <div class="comment-date">2020-10-02 07:35 UTC</div> </div> <div class="comment" id="3dbfa056360b4df6a052be2776a6f930"> <div class="comment-author"><a href="https://github.com/YevgeniyShunevych">Yevgeniy Shunevych</a> <a href="#3dbfa056360b4df6a052be2776a6f930">#</a></div> <div class="comment-content"> <p> I see your point, Mark. Thanks for your reply. </p> <p> I would like to add a few words in defense of the NUnit. </p> <p> First of all, there is no problem to use an extra library for NUnit assertions too. For example, I like <a href="https://fluentassertions.com/">FluentAssertions</a> that works great for both XUnit and NUnit. </p> <p> NUnit 3 also has a set of useful features that are missing in XUnit like: <ul> <li> <a href="https://docs.nunit.org/articles/nunit/writing-tests/assertions/multiple-asserts.html">Multiple asserts</a> - quite useful for UI testing, for example. </li> <li> <a href="https://docs.nunit.org/articles/nunit/writing-tests/Warnings.html">Warnings</a>. </li> <li> <a href="https://docs.nunit.org/articles/nunit/writing-tests/TestContext.html">TestContext</a>. </li> <li> <a href="https://docs.nunit.org/articles/nunit/writing-tests/TestContext.html#addtestattachment-37">File attachments</a> - useful for UI tests or tests that verify files. For example, when the UI test fails, you can take a screenshot, save it and add to NUnit as an attachment. Or when you test PDF file, you can attach the file that doesn't pass the test. </li> </ul> </p> <p> I can recommend to reconsider the view of NUnit, as the library progressed well during its 3rd version. XUnit and NUnit seem interchangeable for unit testing. But NUnit, from my point of view, provides the features that are helpful in integration and UI testing. </p> </div> <div class="comment-date">2020-10-05 13:24 UTC</div> </div> <div class="comment" id="fded96f5f68a4d8ba1e782678dd854be"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fded96f5f68a4d8ba1e782678dd854be">#</a></div> <div class="comment-content"> <p> Yevgeniy, I haven't done UI testing since sometime in 2005, I think, so I wasn't aware of that. It's good to know that NUnit may excel at that, and the other scenarios you point out. </p> <p> To be clear, while I still think I may prefer xUnit.net, I don't consider it perfect. </p> </div> <div class="comment-date">2020-10-05 14:10 UTC</div> </div> <div class="comment" id="d7902737ca534eb89a223dbaee65df2a"> <div class="comment-author"><a href="https://github.com/adrianiftode">Adrian Iftode</a> <a href="#d7902737ca534eb89a223dbaee65df2a">#</a></div> <div class="comment-content"> <p> I would like to mention <a href="https://github.com/adrianiftode/FluentAssertions.Web">FluentAssertions.Web</a>, a library I wrote with the purpose to solve exact this problem I had while maintaining API tests. It's frustrating not to tell in two seconds why a test failed. Yeah, it is a Bad Request, but why. Maybe the response is in the respons content? <p> </p> Wouldn't be an idea to see the response content? This FluentAssertions extension outputs everything starting from the request to the response, when the test fails. </p> </div> <div class="comment-date">2022-09-26 12:32 UTC</div> </div> <div class="comment" id="5d4af38fe7dc4428a6767792176cc61d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5d4af38fe7dc4428a6767792176cc61d">#</a></div> <div class="comment-content"> <p> Adrian, thank you for writing. Your comment inspired me to write an article on when tests fail. This article is now in the publication queue. I'll try to remember to come back here and add a link once it's published. </p> </div> <div class="comment-date">2022-10-04 5:51 UTC</div> </div> <div class="comment" id="a518569044964aadb7da8b084fd35634"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a518569044964aadb7da8b084fd35634">#</a></div> <div class="comment-content"> <p> (Placeholder) </p> </div> <div class="comment-date"> UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An XPath query for long methods https://blog.ploeh.dk/2020/09/21/an-xpath-query-for-long-methods 2020-09-21T05:28:00+00:00 Mark Seemann <div id="post"> <p> <em>A filter for Visual Studio code metrics.</em> </p> <p> I consider it a good idea to <a href="/2020/04/13/curb-code-rot-with-thresholds">limit</a> the size of code blocks. Small methods are <a href="/2019/11/18/small-methods-are-easy-to-troubleshoot">easier to troubleshoot</a> and in general fit better in your head. When it comes to vertical size, however, the editors I use don't come with visual indicators like <a href="https://marketplace.visualstudio.com/items?itemName=PaulHarrington.EditorGuidelines">they do for horizontal size</a>. </p> <p> When you're in the midst of developing a feature, you don't want to be <em>prevented</em> from doing so by a tool that refuses to compile your code if methods get too long. On the other hand, it might be a good idea to regularly run a tool over your code base to identify which methods are getting too long. </p> <p> With Visual Studio you can calculate code metrics, which include a measure of the number of lines of code for each method. One option produces an XML file, but if you have a large code base, those XML files are big. </p> <p> The XML files typically look like this: </p> <p> <pre><span style="color:blue;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;">&nbsp;</span><span style="color:red;">version</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">1.0</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">encoding</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">utf-8</span>&quot;<span style="color:blue;">?&gt;</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">CodeMetricsReport</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Version</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">1.0</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Targets</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Target</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">Restaurant.RestApi.csproj</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Assembly</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">Ploeh.Samples.Restaurants.RestApi,&nbsp;...</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metrics</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">MaintainabilityIndex</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">87</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">CyclomaticComplexity</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">537</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">ClassCoupling</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">208</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">DepthOfInheritance</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">1</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">SourceLines</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">3188</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">ExecutableLines</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">711</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Metrics</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Namespaces</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Namespace</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">Ploeh.Samples.Restaurants.RestApi</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metrics</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">MaintainabilityIndex</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">87</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">CyclomaticComplexity</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">499</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">ClassCoupling</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">204</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">DepthOfInheritance</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">1</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">SourceLines</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">3100</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">ExecutableLines</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">701</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Metrics</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Types</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">NamedType</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">CalendarController</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metrics</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">MaintainabilityIndex</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">73</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">CyclomaticComplexity</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">14</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">ClassCoupling</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">34</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">DepthOfInheritance</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">1</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">SourceLines</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">190</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">ExecutableLines</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">42</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Metrics</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Members</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Method</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">Task</span><span style="color:red;">&amp;lt;</span><span style="color:blue;">ActionResult</span><span style="color:red;">&amp;gt;</span><span style="color:blue;">&nbsp;CalendarController.Get(...</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metrics</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">MaintainabilityIndex</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">64</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">CyclomaticComplexity</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">2</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">ClassCoupling</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">12</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">SourceLines</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">28</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">Metric</span><span style="color:blue;">&nbsp;</span><span style="color:red;">Name</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">ExecutableLines</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">Value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">7</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Metrics</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Method</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--</span><span style="color:green;">Much&nbsp;more&nbsp;data&nbsp;goes&nbsp;here</span><span style="color:blue;">--&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Members</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">NamedType</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Types</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Namespace</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Namespaces</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Assembly</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Target</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">Targets</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">CodeMetricsReport</span><span style="color:blue;">&gt;</span></pre> </p> <p> How can you filter such a file to find only those methods that are too long? </p> <p> That sounds like a job for <a href="https://en.wikipedia.org/wiki/XPath">XPath</a>. I admit, though, that I use XPath only rarely. While the general idea of the syntax is easy to grasp, it has enough subtle pitfalls that it's not that easy to use, either. </p> <p> Partly for my own benefit, and partly for anyone else who might need it, here's an XPath query that looks for long methods: </p> <p> <pre>//Members/child::*[Metrics/Metric[@Value &gt; 24 and @Name = "SourceLines"]]</pre> </p> <p> This query looks for <a href="/2019/11/04/the-80-24-rule">methods longer that 24 lines of code</a>. If you don't agree with that <a href="/2020/04/13/curb-code-rot-with-thresholds">threshold</a> you can always change it to another value. You can also change <code>@Name</code> to look for <code>CyclomaticComplexity</code> or one of the other metrics. </p> <p> Given the above XML metrics report, the XPath filter would select (among other members) the <code>CalendarController.Get</code> method, because it has 28 lines of source code. It turns out, though, that the filter produces some false positives. The method in question is actually fine: </p> <p> <pre><span style="color:green;">/*&nbsp;This&nbsp;method&nbsp;loads&nbsp;a&nbsp;year&#39;s&nbsp;worth&nbsp;of&nbsp;reservations&nbsp;in&nbsp;order&nbsp;to&nbsp;segment &nbsp;*&nbsp;them&nbsp;all.&nbsp;In&nbsp;a&nbsp;realistic&nbsp;system,&nbsp;this&nbsp;could&nbsp;be&nbsp;quite&nbsp;stressful&nbsp;for &nbsp;*&nbsp;both&nbsp;the&nbsp;database&nbsp;and&nbsp;the&nbsp;web&nbsp;server.&nbsp;Some&nbsp;of&nbsp;that&nbsp;concern&nbsp;can&nbsp;be &nbsp;*&nbsp;addressed&nbsp;with&nbsp;an&nbsp;appropriate&nbsp;HTTP&nbsp;cache&nbsp;header&nbsp;and&nbsp;a&nbsp;reverse&nbsp;proxy, &nbsp;*&nbsp;but&nbsp;a&nbsp;better&nbsp;solution&nbsp;would&nbsp;be&nbsp;a&nbsp;CQRS-style&nbsp;architecture&nbsp;where&nbsp;the &nbsp;*&nbsp;calendars&nbsp;get&nbsp;re-rendered&nbsp;as&nbsp;materialised&nbsp;views&nbsp;in&nbsp;a&nbsp;background &nbsp;*&nbsp;process.&nbsp;That&#39;s&nbsp;beyond&nbsp;the&nbsp;scope&nbsp;of&nbsp;this&nbsp;example&nbsp;code&nbsp;base,&nbsp;though. &nbsp;*/</span> [<span style="color:#2b91af;">ResponseCache</span>(Duration&nbsp;=&nbsp;60)] [<span style="color:#2b91af;">HttpGet</span>(<span style="color:#a31515;">&quot;restaurants/{restaurantId}/calendar/{year}&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Get(<span style="color:blue;">int</span>&nbsp;restaurantId,&nbsp;<span style="color:blue;">int</span>&nbsp;year) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;restaurant&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;RestaurantDatabase &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GetRestaurant(restaurantId).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(restaurant&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotFoundResult</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;period&nbsp;=&nbsp;<span style="color:#2b91af;">Period</span>.Year(year); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;days&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;MakeDays(restaurant,&nbsp;period) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">OkObjectResult</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CalendarDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;restaurant.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Year&nbsp;=&nbsp;year, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Days&nbsp;=&nbsp;days &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> That method only has 18 lines of actual source code, from the beginning of the method declaration to the closing bracket. Visual Studio's metrics calculator, however, also counts the attributes and the comments. </p> <p> In general, I only add comments when I want to communicate something that I can't express as a type or a method name, so in this particular code base, it's not much of an issue. If you consistently adorn every method with doc comments, on the other hand, you may need to perform some pre-processing on the source code before you calculate the metrics. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. We need young programmers; we need old programmers https://blog.ploeh.dk/2020/09/14/we-need-young-programmers-we-need-old-programmers 2020-09-14T05:46:00+00:00 Mark Seemann <div id="post"> <p> <em>The software industry loves young people, but old-timers serve an important purpose, too.</em> </p> <p> Our culture idolises youth. There's several reasons for this, I believe. Youth seems synonymous with vigour, strength, beauty, and many other desirable qualities. The cynical perspective is that young people, while rebellious, also tend to be easy to manipulate, if you know which buttons to push. A middle-aged man like me isn't susceptible to the argument that I should buy a particular pair of Nike shoes because they're named after Michael Jordan, but for a while, one pair wasn't enough for my teenage daughter. </p> <p> In intellectual pursuits (like software development), youth is often extolled as the source of innovation. You're often confronted with examples like that of <a href="https://en.wikipedia.org/wiki/%C3%89variste_Galois">Évariste Galois</a>, who made all his discoveries before turning 21. <a href="https://en.wikipedia.org/wiki/Ada_Lovelace">Ada Lovelace</a> was around 28 years when she produced what is considered the 'first computer program'. <a href="https://en.wikipedia.org/wiki/Alan_Turing">Alan Turing</a> was 24 when he wrote <a href="https://en.wikipedia.org/wiki/Turing%27s_proof">On Computable Numbers, with an Application to the Entscheidungsproblem</a>. </p> <p> Clearly, young age is no detriment to making ground-breaking contributions. It has even become folklore that everyone past the age of 35 is a has-been whose only chance at academic influence is to write a textbook. </p> <h3 id="800321a74c054ea0b75815c86f4ce18d"> The story of the five monkeys <a href="#800321a74c054ea0b75815c86f4ce18d" title="permalink">#</a> </h3> <p> You may have seen a story called <em>the five monkeys experiment</em>. It's most likely a fabrication, but it goes like this: </p> <p> A group of scientists placed five monkeys in a cage, and in the middle, a ladder with bananas on the top. Every time a monkey went up the ladder, the scientists soaked the rest of the monkeys with cold water. After a while, every time a monkey went up the ladder, the others would beat it up. </p> <p> After some time, none of the monkeys dared go up the ladder regardless of the temptation. The scientists then substituted one of the monkeys with a new one, who'd immediately go for the bananas, only to be beaten up by the others. After several beatings, the new member learned not to climb the ladder even though it never knew why. </p> <p> A second monkey was substituted and the same occurred. The first monkey participated in beating the second. A third monkey was exchanged and the story repeated. The fourth was substituted and the beating was repeated. Finally the fifth monkey was replaced. </p> <p> Left was a group of five monkeys who, even though they never received a cold shower, continued to beat up any monkey who attempted to climb the ladder. If it was possible to ask the monkeys why they would beat up all who attempted to go up the ladder, the answer would probably be: </p> <p> "That's how we do things here." </p> <p> While the story is probably just that: a story, it tells us something about the drag induced by age and experience. If you've been in the business for decades, you've seen numerous failed attempts at something you yourself tried when you were young. You know that it can't be done. </p> <p> Young people don't know that a thing can't be done. If they can avoid the monkey-beating, they'll attempt the impossible. </p> <h3 id="4add8a9af0424d7e889d3125837ed611"> Changing circumstances <a href="#4add8a9af0424d7e889d3125837ed611" title="permalink">#</a> </h3> <p> Is attempting the impossible a good idea? </p> <p> In general, no, because it's... impossible. There's a reason older people tell young people that a thing can't be done. It's not just because they're stodgy conservatives who abhor change. It's because they see the effort as wasteful. Perhaps they're even trying to be kind, guiding young people off a path where only toil and disappointment is to be found. </p> <p> What old people don't realise is that sometimes, circumstances change. </p> <p> What was impossible twenty years ago may not be impossible today. We see this happening in many fields. Producing a commercially viable electric car was impossible for decades, until, with the advances made in battery technology, it became possible. </p> <p> Technology changes rapidly in software development. People trying something previously impossible may find that it's possible today. Once, if you had lots of data, you had to store it in fully normalised form, because storage was expensive. For a decade, relational databases were the only game in town. Then circumstances changed. Storage became cheaper, and a new movement of NoSQL storage emerged. What was before impossible became possible. </p> <p> Older people often don't see the new opportunities, because they 'know' that some things are impossible. Young people push the envelope driven by a combination of zest and ignorance. Most fail, but a few succeed. </p> <h3 id="4272a069588e47f796646bd282b9de02"> Lottery of the impossible <a href="#4272a069588e47f796646bd282b9de02" title="permalink">#</a> </h3> <p> I think of this process as a lottery. Imagine that every impossible thing is a red ball in an urn. Every young person who tries the impossible draws a random ball from the urn. </p> <p> The urn contains millions of red balls, but every now and then, one of them turns green. You don't know which one, but if you draw it, it represents something that was previously impossible which has now become possible. </p> <p> This process produces growth, because once discovered, the new and better way of doing things can improve society in general. Occasionally, the young discoverer may even gain some fame and fortune. </p> <p> It seems wasteful, though. Most people who attempt the impossible will reach the predictable conclusion. What was deemed impossible was, indeed, impossible. </p> <p> When I'm in a cynical mood, I don't think that it's youth in itself that is the source of progress. It's just the <a href="https://en.wikipedia.org/wiki/Law_of_large_numbers">law of large numbers</a> applied. If there's a one in million chance that something will succeed, but ten million people attempt it, it's only a matter of time before one succeeds. </p> <p> Society at large can benefit from the success of the few, but ten million people still wasted their efforts. </p> <h3 id="016744f0ea77495c958a7914f08187db"> We need the old, too <a href="#016744f0ea77495c958a7914f08187db" title="permalink">#</a> </h3> <p> If you accept the argument that young people are more likely to try the impossible, we need the young people. Do we need the old people? </p> <p> I'm turning fifty in 2020. You may consider that old, but I expect to work for many more years. I don't know if the software industry needs fifty-year-olds, but that's not the kind of old I have in mind. I'm thinking of people who have retired, or are close to retirement. </p> <p> In our youth-glorifying culture, we tend to dismiss the opinion and experiences of old people. <em>Oh, well, it's just a codgy old man</em> (or woman), we'll say. </p> <p> We ignore the experience of the old, because we believe that they haven't been keeping up with times. Their experiences don't apply to us, because we live under new circumstance. Well, see above. </p> <p> I'm not advocating that we turn into a gerontocracy that venerates our elders solely because of their age. Again, according to the law of large numbers, some people live to old age. There need not be any correlation between survivors and wisdom. </p> <p> We need the old to tell us the truth, because they have little to lose. </p> <h3 id="8b5c613ba6c44bb4b4e6dbba7ae7d19a"> Nothing to lose <a href="#8b5c613ba6c44bb4b4e6dbba7ae7d19a" title="permalink">#</a> </h3> <p> In the last couple of years, I've noticed a trend. A book comes out, exposing the sad state of affairs in some organisation. This has happened regularly in Denmark, where I live. One book may expose the deplorable conditions of the Danish tax authorities, one may describe the situation in the ministry of defence, one criticises the groupthink associated with the climate crisis, and so on. </p> <p> Invariably, it turns out that the book is written by a professor emeritus or a retired department head. </p> <p> I don't think that these people, all of a sudden, had an epiphany after they retired. They knew all about the rot in the system they were part of, while they were part of it, but they've had too much to lose. You could argue that they should have said something before they retired, but that requires a moral backbone we can't expect most people to have. </p> <p> When people retire, the threat of getting fired disappears. Old people can speak freely to a degree most other people can't. </p> <p> Granted, many may simply use that freedom to spew bile or shout <em>Get off my lawn!</em>, but many are in the unique position to reveal truths no-one else dare speak. Many are, perhaps, just bitter, but some may possess knowledge that they are in a unique position to reveal. </p> <p> When that grumpy old guy on Twitter writes something that makes you uncomfortable, consider this: he may still be right. </p> <h3 id="2d64bd2c7ccb4b7ca2418802ed82689e"> Being unreasonable <a href="#2d64bd2c7ccb4b7ca2418802ed82689e" title="permalink">#</a> </h3> <p> In a way, you could say that we need young and old people for the same fundamental reason. Not all of them, but enough of them, are in a position to be unreasonable. <blockquote> <p> "The reasonable man adapts himself to the world: the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man." </p> <footer><cite>George Bernard Shaw</cite></footer> </blockquote> Young people and old people are unreasonable in each their own way, and we need both. </p> <h3 id="df88f595ec814e2bafcbd018ff5f5ad2"> Conclusion <a href="#df88f595ec814e2bafcbd018ff5f5ad2" title="permalink">#</a> </h3> <p> We need young people in the software development industry. Because of their vigour and inexperience, they'll push the envelope. Most will fail to do the impossible, but a few succeed. </p> <p> This may seem like a cynical view, but we've all been young, and most of us have been through such a phase. It's like a rite of passage, and even if you fail to make your mark on the world, you're still likely to have learned a lot. </p> <p> We need old people because they're in a position to speak truth to the world. Notice that I didn't make my argument about the <em>experience</em> of old-timers. Actually, I find that valuable as well, but that's the ordinary argument: <em>Listen to old people, because they have experience and wisdom.</em> </p> <p> Some of them do, at least. </p> <p> I didn't make much out of that argument, because you already know it. There'd be no reason to write this essay if that was all I had to say. Old people have less on the line, so they can speak more freely. If someone you used to admire retires and all of a sudden starts saying or writing unpleasant and surprising things, there might be a good explanation, and it might be a good idea to pay attention. </p> <p> Or maybe he or she is just bitter or going senile... </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Add null check without brackets in Visual Studio https://blog.ploeh.dk/2020/09/07/add-null-check-without-brackets-in-visual-studio 2020-09-07T06:47:00+00:00 Mark Seemann <div id="post"> <p> <em>A Visual Studio tweak.</em> </p> <p> The most recent versions of Visual Studio have included many new <em>Quick Actions</em>, accessible with <kbd>Ctrl</kbd> + <kbd>.</kbd> or <kbd>Alt</kbd> + <kbd>Enter</kbd>. The overall feature has been around for some years, but the product group has been adding features at a good pace recently, it seems to me. </p> <p> One feature has been around for at least a year: <em>Add null check</em>. In a default installation of Visual Studio, it looks like this: </p> <p> <img src="/content/binary/add-null-check-with-brackets.png" alt="Screen shot of the 'Add null check' Quick Action. By default it'll add brackets around the throw statement."> </p> <p> As the screen shot shows, it'll auto-generate a Guard Clause like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Overlaps(<span style="color:#2b91af;">Reservation</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(other)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;otherSeating&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Seating</span>(SeatingDuration,&nbsp;other.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Overlaps(otherSeating); }</pre> </p> <p> Part of my personal coding style is that I don't use brackets for one-liners. This is partially motivated by the desire to save vertical space, since I try to <a href="/2019/11/04/the-80-24-rule">keep methods as small as possible</a>. Some people worry that not using brackets for one-liners makes the code more vulnerable to defects, but I typically have automated regressions tests to keep an eye on correctness. </p> <p> The above default behaviour of the <em>Add null check</em> Quick Action annoyed me because I had to manually remove the brackets. It's one of those things that annoy you a little, but not enough that you throw aside what you're doing to figure out if you can change the situation. </p> <p> Until it annoyed me enough to investigate whether there was something I <em>could</em> do about it. It turns out to be easy to tweak the behaviour. </p> <p> In Visual Studio 2019, go to <em>Tools</em>, <em>Options</em>, <em>Text Editor</em>, <em>C#</em>, <em>Code Style</em>, <em>General</em> and change the <em>Prefer braces</em> option to <em>No</em>: </p> <p> <img src="/content/binary/visual-studio-prefer-braces-option.png" alt="Screen shot of Visual Studio Options dialog box."> </p> <p> This changes the behaviour of the <em>Add null check</em> Quick Action: </p> <p> <img src="/content/binary/add-null-check-without-brackets.png" alt="Screen shot of the 'Add null check' Quick Action after the behaviour change. It no longer adds brackets around the throw statement."> </p> <p> After applying the Quick Action, my code now looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Overlaps(<span style="color:#2b91af;">Reservation</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(other)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;otherSeating&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Seating</span>(SeatingDuration,&nbsp;other.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Overlaps(otherSeating); }</pre> </p> <p> This better fits my preference. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="bbe35bc429dc4d759fab30cfc90c9bdd"> <div class="comment-author">Daniel <a href="#bbe35bc429dc4d759fab30cfc90c9bdd">#</a></div> <div class="comment-content"> <p> The same should go for if's, switch's, try/catch and perhaps a few other things. While we are at it, spacing is also a good source for changing layout in code, e.g: decimal.Round( amount * 1.0000m, provider.Getdecimals( currency ) ) ). </p> </div> <div class="comment-date">2020-09-07 7:14 UTC</div> </div> <div class="comment" id="ec5cf23c648e4277b851685bc55e258a"> <div class="comment-author">James World <a href="#ec5cf23c648e4277b851685bc55e258a">#</a></div> <div class="comment-content"> <p> When assigning parameters to fields, I like this one-line option for a null check: </p> <p> <pre>_thing = thing ?? throw new ArgumentNullException(nameof(thing));</pre>. </p> <p> How do you feel about it? You could even use a disard in your example: <pre>_ = thing ?? throw new ArgumentNullException(nameof(thing));</pre> </p> </div> <div class="comment-date">2020-09-07 13:14 UTC</div> </div> <div class="comment" id="e456bf65fe3e426f97639b8855622a7b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e456bf65fe3e426f97639b8855622a7b">#</a></div> <div class="comment-content"> <p> James, thank you for writing. I'm aware of that option, but rarely use it in my examples. I try to write my example code in a way that they may also be helpful to Java developers, or programmers that use other C-based languages. Obviously, that concern doesn't apply if I discuss something specific to C#, but when I try to explain design patterns or low-level architecture, I try to stay away from language features exclusive to C#. </p> <p> In other contexts, I might or might not use that language feature, but I admit that it jars me. I find that it's a 'clever' solution to something that's not much of a problem. The <code>??</code> operator is intended as a short-circuiting operator one can use to simplify variable assignment. Throwing on the right-hand side never assigns a value. </p> <p> It's as though you would use the <a href="/2018/04/09/coalescing-composite-as-a-monoid">coalescing monoid</a> to pick the left-hand side, but let the program fail on the right-hand side. In Haskell it would look like this: </p> <p> <pre>x = thing &lt;&gt; undefined</pre> </p> <p> The compiler infers that <code>x</code> has the same type as <code>thing</code>, e.g. <code>Semigroup a =&gt; Maybe a</code>. If <code>thing</code>, however, is <code>Nothing</code>, the expression crashes when evaluated. </p> <p> While this compiles in Haskell, I consider it unidiomatic. It too blatantly advertises the existence of the so-called <a href="https://en.wikipedia.org/wiki/Bottom_type">bottom value</a> (⊥). We know it's there, but we prefer to pretend that it's not. </p> <p> That line of reasoning is subjective, and if I find myself in a code base that uses <code>??</code> like you've shown, I'd try to fit in with that style. It doesn't annoy me that much, after all. </p> <p> It's also shorter, I must admit that. </p> </div> <div class="comment-date">2020-09-08 05:57 UTC</div> </div> <div class="comment" id="5c16a687f8b4421fa826ef7c66656767"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#5c16a687f8b4421fa826ef7c66656767">#</a></div> <div class="comment-content"> <blockquote> <p> The above default behaviour of the <em>Add null check</em> Quick Action annoyed me because I had to manually remove the brackets. It's one of those things that annoy you a little, but not enough that you throw aside what you're doing to figure out if you can change the situation. </p> <p> Until it annoyed me enough to investigate whether there was something I <em>could</em> do about it. It turns out to be easy to tweak the behaviour. </p> <p> In Visual Studio 2019, go to <em>Tools</em>, <em>Options</em>, <em>Text Editor</em>, <em>C#</em>, <em>Code Style</em>, <em>General</em> and change the <em>Prefer braces</em> option to <em>No</em>: </p> </blockquote> <p> You can do more than that. As suggested by Visual Studio in your second screenshot, you can (after making the change to not prefer braces) generate an <code>.editorconfig</code> file containing your coding style settings. Visual Studio will prompt you to save the file alongside your solution. If you do so, then any developer that opens these files with Visual Studio with also have your setting to not prefer braces. I wrote a <a href="https://tysonwilliams.coding.blog/2020-09-12_editorconfig">short post about EditorConfig</a> that contains more information. </p> </div> <div class="comment-date">2020-09-13 01:53 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Properties for all https://blog.ploeh.dk/2020/08/31/properties-for-all 2020-08-31T08:39:00+00:00 Mark Seemann <div id="post"> <p> <em>Writing test cases for all possible input values.</em> </p> <p> I've noticed that programmers new to automated testing struggle with a fundamental task: how to come up with good test input? </p> <p> There's plenty of design patterns that address that issue, including <a href="http://www.natpryce.com/articles/000714.html">Test Data Builders</a>. Still, test-driven development, when done right, gives you good feedback on the design of your API. I don't consider having to resort to Test Data Builder as much of a compromise, but still, it's even better if you can model the API in question so that it requires no redundant data. </p> <p> Most business processes can be modelled as finite state machines. Once you've understood the problem well enough to enumerate the possible states, you can reverse-engineer an <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data type</a> from that enumeration. </p> <p> While the data carried around in the state machine may be unconstrained, the number of state and state transitions is usually limited. Model the states as enumerable values, and you can cover all input values with simple combinatorics. </p> <h3 id="44553eea5abb4ab58c73ce173a12ddde"> Tennis states <a href="#44553eea5abb4ab58c73ce173a12ddde" title="permalink">#</a> </h3> <p> The <a href="https://codingdojo.org/kata/Tennis">Tennis kata</a> is one of my favourite exercises. Just like a business process, it turns out that you can model the rules as a finite state machine. There's a state where both players have <em>love</em>, 15, 30, or 40 points (although both can't have 40 points; that state is called <em>deuce</em>); there's a state where a player has the <em>advantage</em>; and so on. </p> <p> I've <a href="https://blog.ploeh.dk/2016/02/10/types-properties-software-designing-with-types">previously written about how to design the states of the Tennis kata with types</a>, so here, I'll just limit myself to the few types that turn out to matter as test input: players and points. </p> <p> Here are both types defined as <a href="https://www.haskell.org">Haskell</a> <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a>: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Player&nbsp;=&nbsp;PlayerOne&nbsp;|&nbsp;PlayerTwo&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>,&nbsp;<span style="color:#2b91af;">Enum</span>,&nbsp;<span style="color:#2b91af;">Bounded</span>) <span style="color:blue;">data</span>&nbsp;Point&nbsp;=&nbsp;Love&nbsp;|&nbsp;Fifteen&nbsp;|&nbsp;Thirty&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>,&nbsp;<span style="color:#2b91af;">Enum</span>,&nbsp;<span style="color:#2b91af;">Bounded</span>)</pre> </p> <p> Notice that both are instances of the <code>Enum</code> and <code>Bounded</code> type classes. This means that it's possible to enumerate all values that inhabit each type. You can use that to your advantage when writing unit tests. </p> <h3 id="5a6d5d80394b4486bb17adc4cd8cf727"> Properties for every value <a href="#5a6d5d80394b4486bb17adc4cd8cf727" title="permalink">#</a> </h3> <p> Most people think of <a href="/property-based-testing-intro">property-based testing</a> as something that involves a special framework such as <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a>, <a href="https://github.com/hedgehogqa">Hedgehog</a>, or <a href="https://fscheck.github.io/FsCheck/index.html">FsCheck</a>. I often use such frameworks, but as I've <a href="/2015/02/23/property-based-testing-without-a-property-based-testing-framework">written about before</a>, sometimes enumerating all possible input values is simpler and faster than relying on randomly generated values. There's no reason to generate 100 random values of a type inhabited by only three values. </p> <p> Instead, write properties for the entire <a href="https://en.wikipedia.org/wiki/Domain_of_a_function">domain</a> of the function in question. </p> <p> In Haskell, you can define a generic enumeration like this: </p> <p> <pre><span style="color:#2b91af;">every</span>&nbsp;::&nbsp;(<span style="color:blue;">Enum</span>&nbsp;a,&nbsp;<span style="color:blue;">Bounded</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[a] every&nbsp;=&nbsp;[<span style="color:blue;">minBound</span>&nbsp;..&nbsp;<span style="color:blue;">maxBound</span>] </pre> </p> <p> I don't know why this function doesn't already exist in the standard library, but I suppose that most people just rely on the list comprehension syntax <code>[minBound .. maxBound]</code>... </p> <p> With it you can write simple properties of the Tennis game. For example: </p> <p> <pre><span style="color:#a31515;">&quot;Given&nbsp;deuce,&nbsp;when&nbsp;player&nbsp;wins,&nbsp;then&nbsp;that&nbsp;player&nbsp;has&nbsp;advantage&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;player&nbsp;&lt;-&nbsp;every &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;score&nbsp;Deuce&nbsp;player &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;Advantage&nbsp;player&nbsp;~=?&nbsp;actual </pre> </p> <p> Here I'm using <a href="/2018/05/07/inlined-hunit-test-lists">inlined HUnit test lists</a>. With the Tennis kata, it's easiest to start with the <em>deuce</em> case, because regardless of player, the new state is that the winning player has advantage. That's what that property asserts. Because of the <code>do</code> notation, the property produces a list of test cases, one for <code>every</code> player. There's only two <code>Player</code> values, so it's only two test cases. </p> <p> The beauty of <code>do</code> notation (or the list monad in general) is that you can combine enumerations, for example like this: </p> <p> <pre><span style="color:#a31515;">&quot;Given&nbsp;forty,&nbsp;when&nbsp;player&nbsp;wins&nbsp;ball,&nbsp;then&nbsp;that&nbsp;player&nbsp;wins&nbsp;the&nbsp;game&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;player&nbsp;&lt;-&nbsp;every &nbsp;&nbsp;opp&nbsp;&lt;-&nbsp;every &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;score&nbsp;(Forty&nbsp;$&nbsp;FortyData&nbsp;player&nbsp;opp)&nbsp;player &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;Game&nbsp;player&nbsp;~=?&nbsp;actual </pre> </p> <p> While <code>player</code> is a <code>Player</code> value, <code>opp</code> (<em>Other Player's Point</em>) is a <code>Point</code> value. Since there's two possible <code>Player</code> values and three possible <code>Point</code> values, the combination yields six test cases. </p> <p> I've been doing the Tennis kata a couple of times using this approach. I've also done it in C# with <a href="https://xunit.net">xUnit.net</a>, where the first test of the <em>deuce</em> state looks like this: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(Player))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;TransitionFromDeuce(<span style="color:#2b91af;">IPlayer</span>&nbsp;player) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Deuce</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.BallTo(player); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Advantage</span>(player); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); }</pre> </p> <p> <a href="/2019/12/16/zone-of-ceremony">C# takes more ceremony than Haskell</a>, but the idea is the same. The <code>Player</code> data source for the <code>[Theory]</code> is defined as an enumeration of the possible values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">object</span>[]&gt;&nbsp;Player { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">object</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PlayerOne</span>()&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">object</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PlayerTwo</span>()&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> All the times I've done the Tennis kata by fully exhausting the domain of the transition functions in question, I've arrived at 40 test cases. I wonder if that's the number of possible state transitions of the game, or if it's just an artefact of the way I've modelled it. I suspect the latter... </p> <h3 id="21ccf5bd3c3f48859bbb8c766b0a0610"> Conclusion <a href="#21ccf5bd3c3f48859bbb8c766b0a0610" title="permalink">#</a> </h3> <p> You can sometimes enumerate all possible inputs to an API. Even if a function takes a Boolean value and a byte as input, the enumeration of all possible combinations is only 2 * 256 = 512 values. You computer will tear through all of those combinations faster than you can say <em>random selection</em>. Consider writing APIs that take algebraic data types as input, and writing properties that exhaust the domain of the functions in question. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Adding REST links as a cross-cutting concern https://blog.ploeh.dk/2020/08/24/adding-rest-links-as-a-cross-cutting-concern 2020-08-24T06:47:00+00:00 Mark Seemann <div id="post"> <p> <em>Use a piece of middleware to enrich a Data Transfer Object. An ASP.NET Core example.</em> </p> <p> When developing true REST APIs, you should <a href="https://martinfowler.com/articles/richardsonMaturityModel.html">use hypermedia controls</a> (i.e. <em>links</em>) to guide clients to the resources they need. I've always felt that the code that generates these links tends to make otherwise readable Controller methods unreadable. </p> <p> I'm currently experimenting with generating links as a cross-cutting concern. So far, I like it very much. </p> <p> <ins datetime="2021-07-22T06:32Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="57785b860f604552b099e42b296f6fb6"> Links from home <a href="#57785b860f604552b099e42b296f6fb6" title="permalink">#</a> </h3> <p> Consider an online restaurant reservation system. When you make a <code>GET</code> request against the home resource (which is the only published URL for the API), you should receive a representation like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:reservations&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/reservations&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:year&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:month&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020/8&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:day&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020/8/13&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> As you can tell, my example just runs on my local development machine, but I'm sure that you can see past that. There's three calendar links that clients can use to <code>GET</code> the restaurant's calendar for the current day, month, or year. Clients can use these resources to present a user with a date picker or a similar user interface so that it's possible to pick a date for a reservation. </p> <p> When a client wants to make a reservation, it can use the URL identified by the <code>rel</code> (<em>relationship type</em>) <code>"urn:reservations"</code> to make a <code>POST</code> request. </p> <h3 id="724d54b2bb4a4071b0f965105541c792"> Link generation as a Controller responsibility <a href="#724d54b2bb4a4071b0f965105541c792" title="permalink">#</a> </h3> <p> I first wrote the code that generates these links directly in the Controller class that serves the home resource. It looked like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IActionResult</span>&nbsp;Get() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;links&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">LinkDto</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;links.Add(Url.LinkToReservations()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(enableCalendar) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;now&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;links.Add(Url.LinkToYear(now.Year)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;links.Add(Url.LinkToMonth(now.Year,&nbsp;now.Month)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;links.Add(Url.LinkToDay(now.Year,&nbsp;now.Month,&nbsp;now.Day)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HomeDto</span>&nbsp;{&nbsp;Links&nbsp;=&nbsp;links.ToArray()&nbsp;}); }</pre> </p> <p> That doesn't look too bad, but 90% of the code is exclusively concerned with generating links. (<code>enableCalendar</code>, by the way, is a <a href="https://en.wikipedia.org/wiki/Feature_toggle">feature flag</a>.) That seems acceptable in this special case, because there's really nothing else the home resource has to do. For other resources, the Controller code might contain some composition code as well, and then all the link code starts to look like noise that makes it harder to understand the actual purpose of the Controller method. You'll see an example of a non-trivial Controller method later in this article. </p> <p> It seemed to me that enriching a Data Transfer Object (DTO) with links ought to be a cross-cutting concern. </p> <h3 id="1973741c11414414bcd0ca2748bf0f0d"> LinksFilter <a href="#1973741c11414414bcd0ca2748bf0f0d" title="permalink">#</a> </h3> <p> In ASP.NET Core, you can implement cross-cutting concerns with a type of middleware called <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.filters.iasyncactionfilter">IAsyncActionFilter</a>. I added one called <code>LinksFilter</code>: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">LinksFilter</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IAsyncActionFilter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;enableCalendar; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUrlHelperFactory</span>&nbsp;UrlHelperFactory&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">LinksFilter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUrlHelperFactory</span>&nbsp;urlHelperFactory, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">CalendarFlag</span>&nbsp;calendarFlag) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UrlHelperFactory&nbsp;=&nbsp;urlHelperFactory; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enableCalendar&nbsp;=&nbsp;calendarFlag.Enabled; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;OnActionExecutionAsync( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ActionExecutingContext</span>&nbsp;context, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ActionExecutionDelegate</span>&nbsp;next) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ctxAfter&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;next().ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(ctxAfter.Result&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">OkObjectResult</span>&nbsp;ok)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;url&nbsp;=&nbsp;UrlHelperFactory.GetUrlHelper(ctxAfter); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">switch</span>&nbsp;(ok.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#2b91af;">HomeDto</span>&nbsp;homeDto: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddLinks(homeDto,&nbsp;url); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#2b91af;">CalendarDto</span>&nbsp;calendarDto: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddLinks(calendarDto,&nbsp;url); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">default</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span></pre> </p> <p> There's only one method to implement. If you want to run some code <em>after</em> the Controllers have had their chance, you invoke the <code>next</code> delegate to get the resulting context. It should contain the response to be returned. If <code>Result</code> isn't an <code>OkObjectResult</code> there's no content to enrich with links, so the method just returns. </p> <p> Otherwise, it switches on the type of the <code>ok.Value</code> and passes the DTO to an appropriate helper method. Here's the <code>AddLinks</code> overload for <code>HomeDto</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AddLinks(<span style="color:#2b91af;">HomeDto</span>&nbsp;dto,&nbsp;<span style="color:#2b91af;">IUrlHelper</span>&nbsp;url) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(enableCalendar) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;now&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Links&nbsp;=&nbsp;<span style="color:blue;">new</span>[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url.LinkToReservations(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url.LinkToYear(now.Year), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url.LinkToMonth(now.Year,&nbsp;now.Month), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url.LinkToDay(now.Year,&nbsp;now.Month,&nbsp;now.Day) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dto.Links&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;url.LinkToReservations()&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can probably recognise the implemented behaviour from before, where it was implemented in the <code>Get</code> method. That method now looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;Get() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">OkObjectResult</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HomeDto</span>()); }</pre> </p> <p> That's clearly much simpler, but you probably think that little has been achieved. After all, doesn't this just move some code from one place to another? </p> <p> Yes, that's the case in this particular example, but I wanted to start with an example that was so simple that it highlights how to move the code to a filter. Consider, then, the following example. </p> <h3 id="9156b0b69f4d485fa373a8c4b06547d5"> A calendar resource <a href="#9156b0b69f4d485fa373a8c4b06547d5" title="permalink">#</a> </h3> <p> The online reservation system enables clients to navigate its calendar to look up dates and time slots. A representation might look like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;previous&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020/8/12&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;next&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020/8/14&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;], &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;year&quot;</span>:&nbsp;2020, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;month&quot;</span>:&nbsp;8, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;day&quot;</span>:&nbsp;13, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;days&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:year&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:month&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020/8&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;urn:day&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;http://localhost:53568/calendar/2020/8/13&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-08-13&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;entries&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;18:00:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;18:15:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;18:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;18:45:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;19:00:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;19:15:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;19:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;19:45:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;20:00:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;20:15:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;20:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;20:45:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;21:00:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;maximumPartySize&quot;</span>:&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;] }</pre> </p> <p> This is a JSON representation of the calendar for August 13, 2020. The data it contains is the identification of the date, as well as a series of <code>entries</code> that lists the largest reservation the restaurant can accept for each time slot. </p> <p> Apart from the data, the representation also contains links. There's a general collection of links that currently holds only <code>next</code> and <code>previous</code>. In addition to that, each day has its own array of links. In the above example, only a single day is represented, so the <code>days</code> array contains only a single object. For a month calendar (navigatable via the <code>urn:month</code> link), there'd be between 28 and 31 <code>days</code>, each with its own <code>links</code> array. </p> <p> Generating all these links is a complex undertaking all by itself, so separation of concerns is a boon. </p> <h3 id="8f85a3a882164352aa57d600a21628be"> Calendar links <a href="#8f85a3a882164352aa57d600a21628be" title="permalink">#</a> </h3> <p> As you can see in the above <code>LinksFilter</code>, it branches on the type of value wrapped in an <code>OkObjectResult</code>. If the type is <code>CalendarDto</code>, it calls the appropriate <code>AddLinks</code> overload: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AddLinks(<span style="color:#2b91af;">CalendarDto</span>&nbsp;dto,&nbsp;<span style="color:#2b91af;">IUrlHelper</span>&nbsp;url) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;period&nbsp;=&nbsp;dto.ToPeriod(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;previous&nbsp;=&nbsp;period.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PreviousPeriodVisitor</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;next&nbsp;=&nbsp;period.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NextPeriodVisitor</span>()); &nbsp;&nbsp;&nbsp;&nbsp;dto.Links&nbsp;=&nbsp;<span style="color:blue;">new</span>[] &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url.LinkToPeriod(previous,&nbsp;<span style="color:#a31515;">&quot;previous&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url.LinkToPeriod(next,&nbsp;<span style="color:#a31515;">&quot;next&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dto.Days&nbsp;<span style="color:blue;">is</span>&nbsp;{&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;day&nbsp;<span style="color:blue;">in</span>&nbsp;dto.Days) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddLinks(day,&nbsp;url); }</pre> </p> <p> It both generates the <code>previous</code> and <code>next</code> links on the <code>dto</code>, as well as the links for each day. While I'm not going to bore you with more of that code, you can tell, I hope, that the <code>AddLinks</code> method calls other helper methods and classes. The point is that link generation involves more than just a few lines of code. </p> <p> You already saw that in the first example (related to <code>HomeDto</code>). The question is whether there's still some significant code left in the Controller class? </p> <h3 id="ab37807dfce0431b8308f86ec18a44aa"> Calendar resource <a href="#ab37807dfce0431b8308f86ec18a44aa" title="permalink">#</a> </h3> <p> The <code>CalendarController</code> class defines three overloads of <code>Get</code> - one for a single day, one for a month, and one for an entire year. Each of them looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">ActionResult</span>&gt;&nbsp;Get(<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;period&nbsp;=&nbsp;<span style="color:#2b91af;">Period</span>.Month(year,&nbsp;month); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;days&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;MakeDays(period).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">OkObjectResult</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CalendarDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Year&nbsp;=&nbsp;year, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Month&nbsp;=&nbsp;month, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Days&nbsp;=&nbsp;days &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> It doesn't look as though much is going on, but at least you can see that it returns a <code>CalendarDto</code> object. </p> <p> While the method looks simple, it's not. Significant work happens in the <code>MakeDays</code> helper method: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">DayDto</span>[]&gt;&nbsp;MakeDays(<span style="color:#2b91af;">IPeriod</span>&nbsp;period) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;firstTick&nbsp;=&nbsp;period.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FirstTickVisitor</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;lastTick&nbsp;=&nbsp;period.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">LastTickVisitor</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(firstTick,&nbsp;lastTick).ConfigureAwait(<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;days&nbsp;=&nbsp;period.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DaysVisitor</span>()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(d&nbsp;=&gt;&nbsp;MakeDay(d,&nbsp;reservations)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToArray(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;days; }</pre> </p> <p> After having read relevant <code>reservations</code> from the database, it applies complex business logic to allocate them and thereby being able to report on remaining capacity for each time slot. </p> <p> Not having to worry about link generation while doing all that work seems like a benefit. </p> <h3 id="87b164914be0450fbd523735946cf146"> Filter registration <a href="#87b164914be0450fbd523735946cf146" title="permalink">#</a> </h3> <p> You must tell the ASP.NET Core framework about any filters that you add. You can do that in the <code>Startup</code> class' <code>ConfigureServices</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConfigureServices(<span style="color:#2b91af;">IServiceCollection</span>&nbsp;services) { &nbsp;&nbsp;&nbsp;&nbsp;services.AddControllers(opts&nbsp;=&gt;&nbsp;opts.Filters.Add&lt;<span style="color:#2b91af;">LinksFilter</span>&gt;()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span></pre> </p> <p> When registered, the filter executes for each HTTP request. When the object represents a <code>200 OK</code> result, the filter populates the DTOs with links. </p> <h3 id="ced9fd76d526420aa1a847da4398e904"> Conclusion <a href="#ced9fd76d526420aa1a847da4398e904" title="permalink">#</a> </h3> <p> By treating RESTful link generation as a cross-cutting concern, you can separate if from the logic of generating the data structure that represents the resource. That's not the only way to do it. You could also write a simple function that populates DTOs, and call it directly from each Controller action. </p> <p> What I like about using a filter is that I don't have to remember to do that. Once the filter is registered, it'll populate all the DTOs it knows about, regardless of which Controller generated them. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="fff391e80e27427e8fc93959deef3768"> <div class="comment-author"><a href="https://twitter.com/MehdiFalamarzi?s=08">Mehdi Falamarzi</a> <a href="#fff391e80e27427e8fc93959deef3768">#</a></div> <div class="comment-content"> <p> Thanks for your good and insightful posts. </p> <p> Separation of REST concerns from MVC controller's concerns is a great idea, But in my opinion this solution has two problems: </p> <h3 id="52608f57b49942aaaafdab7aae22c85"> Distance between related REST implementations <a href="#52608f57b49942aaaafdab7aae22c85" title="permalink">#</a> </h3> <p> When implementing REST by MVC pattern often REST archetypes are the reasons for a MVC Controller class to be created. As long as the MVC Controller class describes the archetype, And links of a resource is a part of the response when implementing hypermedia controls, having the archetype and its related links in the place where the resource described is a big advantage in easiness and readability of the design. pulling out link implementations and putting them in separate classes causes higher readability and uniformity of the code abstranction levels in the action method at the expense of making a distance between related REST implementation. </p> <h3 id="fa1203384d3746bfadd276aae6ac860f"> Implementation scalability <a href="#fa1203384d3746bfadd276aae6ac860f" title="permalink">#</a> </h3> <p> There is a switch statement on the ActionExecutingContext's result in the LinksFilter to decide what links must be represented to the client in the response.The DTOs thre are the results of the clients's requests for URIs. If this solution generalised for every resources the API must represent there will be several cases for the switch statement to handle. Beside that every resources may have different implemetations for generating their links. Putting all this in one place leads the LinksFilter to be coupled with too many helper classes and this coupling process never stops. </p> <h3 id="4dd0703da482480abffbaffabdb27741"> Solution <a href="#4dd0703da482480abffbaffabdb27741" title="permalink">#</a> </h3> <p> LinkDescriptor and LinkSubscriber for resources links definition </p> <div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"> <pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">LinkDescriptor</span> { <span style="color: #008800; font-weight: bold">public</span> <span style="color: #333399; font-weight: bold">string</span> Rel { <span style="color: #008800; font-weight: bold">get</span>; <span style="color: #008800; font-weight: bold">set</span>; } <span style="color: #008800; font-weight: bold">public</span> <span style="color: #333399; font-weight: bold">string</span> Href { <span style="color: #008800; font-weight: bold">get</span>; <span style="color: #008800; font-weight: bold">set</span>; } <span style="color: #008800; font-weight: bold">public</span> <span style="color: #333399; font-weight: bold">string</span> Resource { <span style="color: #008800; font-weight: bold">get</span>; <span style="color: #008800; font-weight: bold">set</span>; } } </pre> </div> <p> Letting the MVC controller classes have their resource's links definitions but not in action methods. </p> <div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"> <pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> ActionResult <span style="color: #0066BB; font-weight: bold">Get</span>() { <span style="color: #008800; font-weight: bold">return</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #0066BB; font-weight: bold">OkObjectResult</span>(<span style="color: #008800; font-weight: bold">new</span> HomeDto()); } <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #008800; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">RegisterLinks</span>(LinkSubscriber subscriber) { subscriber .Add( resource: <span style="background-color: #fff0f0">&quot;/Home&quot;</span>, rel: <span style="background-color: #fff0f0">&quot;urn:reservations&quot;</span>, href: <span style="background-color: #fff0f0">&quot;/reservations&quot;</span>) .Add( resource: <span style="background-color: #fff0f0">&quot;/Home&quot;</span>, rel: <span style="background-color: #fff0f0">&quot;urn:year&quot;</span>, href: <span style="background-color: #fff0f0">&quot;/calendar/2020&quot;</span>) .Add( resource: <span style="background-color: #fff0f0">&quot;/Home&quot;</span>, rel: <span style="background-color: #fff0f0">&quot;urn:month&quot;</span>, href: <span style="background-color: #fff0f0">&quot;/calendar/2020/8&quot;</span>) .Add( resource: <span style="background-color: #fff0f0">&quot;/Home&quot;</span>, rel: <span style="background-color: #fff0f0">&quot;urn:day&quot;</span>, href: <span style="background-color: #fff0f0">&quot;/calendar/2020/8/13&quot;</span>); } </pre> </div> <p> Registering resources links by convention </p> <div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"> <pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">MvcBuilderExtensions</span> { <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> IMvcBuilder <span style="color: #0066BB; font-weight: bold">AddRestLinks</span>(<span style="color: #008800; font-weight: bold">this</span> IMvcBuilder mvcBuilder) { <span style="color: #333399; font-weight: bold">var</span> subscriber = <span style="color: #008800; font-weight: bold">new</span> LinkSubscriber(); <span style="color: #333399; font-weight: bold">var</span> linkRegistrationMethods = GetLinkRegistrationMethods(mvcBuilder.Services); PopulateLinks(subscriber, linkRegistrationMethods); mvcBuilder.Services.AddSingleton&lt;IEnumerable&lt;LinkDescriptor&gt;&gt;(subscriber.LinkDescriptors); <span style="color: #008800; font-weight: bold">return</span> mvcBuilder; } <span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">static</span> List&lt;MethodInfo&gt; GetLinkRegistrationMethods(IServiceCollection services) { <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0066BB; font-weight: bold">typeof</span>(MvcBuilderExtensions).Assembly.ExportedTypes .Where(tp =&gt; <span style="color: #008800; font-weight: bold">typeof</span>(ControllerBase).IsAssignableFrom(tp)) .Select(tp =&gt; tp.GetMethod(<span style="background-color: #fff0f0">&quot;RegisterLinks&quot;</span>, <span style="color: #008800; font-weight: bold">new</span>[] { <span style="color: #008800; font-weight: bold">typeof</span>(LinkSubscriber) })) .Where(mi =&gt; mi != <span style="color: #008800; font-weight: bold">null</span>) .ToList(); } <span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #008800; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">PopulateLinks</span>(LinkSubscriber subscriber, List&lt;MethodInfo&gt; linkRegistrationMethods) { <span style="color: #008800; font-weight: bold">foreach</span> (<span style="color: #333399; font-weight: bold">var</span> method <span style="color: #008800; font-weight: bold">in</span> linkRegistrationMethods) { method.Invoke(<span style="color: #008800; font-weight: bold">null</span>, <span style="color: #008800; font-weight: bold">new</span>[] { subscriber }); } } } </pre> </div> <p> Add dependencies and execute procedures for adding links to responses </p> <div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"> <pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">ConfigureServices</span>(IServiceCollection services) { services.AddControllers(conf =&gt; conf.Filters.Add&lt;LinksFilter&gt;()) .AddRestLinks(); } </pre> </div> <p> And Last letting the LinksFilter to dynamicaly add resources links by utilizing ExpandoObject </p> <div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"> <pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">LinksFilter</span> : IAsyncActionFilter { <span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">readonly</span> IEnumerable&lt;LinkDescriptor&gt; links; <span style="color: #008800; font-weight: bold">public</span> <span style="color: #0066BB; font-weight: bold">LinksFilter</span>(IEnumerable&lt;LinkDescriptor&gt; links) { <span style="color: #008800; font-weight: bold">this</span>.links = links; } <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">async</span> Task <span style="color: #0066BB; font-weight: bold">OnActionExecutionAsync</span>(ActionExecutingContext context, ActionExecutionDelegate next) { <span style="color: #333399; font-weight: bold">var</span> relatedLinks = links .Where(lk =&gt; context.HttpContext.Request.Path.Value.ToLower() == lk.Resource); <span style="color: #008800; font-weight: bold">if</span> (relatedLinks.Any()) <span style="color: #008800; font-weight: bold">await</span> <span style="color: #0066BB; font-weight: bold">ManipulateResponseAsync</span>(context, next, relatedLinks); } <span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">async</span> Task <span style="color: #0066BB; font-weight: bold">ManipulateResponseAsync</span>(ActionExecutingContext context, ActionExecutionDelegate next, IEnumerable&lt;LinkDescriptor&gt; relatedLinks) { <span style="color: #333399; font-weight: bold">var</span> ctxAfter = <span style="color: #008800; font-weight: bold">await</span> next().ConfigureAwait(<span style="color: #008800; font-weight: bold">false</span>); <span style="color: #008800; font-weight: bold">if</span> (!(ctxAfter.Result <span style="color: #008800; font-weight: bold">is</span> ObjectResult objRes)) <span style="color: #008800; font-weight: bold">return</span>; <span style="color: #333399; font-weight: bold">var</span> expandoResult = <span style="color: #008800; font-weight: bold">new</span> ExpandoObject(); FillExpandoWithResultProperties(expandoResult, objRes.Value); FillExpandoWithLinks(expandoResult, relatedLinks); objRes.Value = expandoResult; } <span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">FillExpandoWithResultProperties</span>(ExpandoObject resultExpando, <span style="color: #333399; font-weight: bold">object</span> <span style="color: #008800; font-weight: bold">value</span>) { <span style="color: #333399; font-weight: bold">var</span> properties = <span style="color: #008800; font-weight: bold">value</span>.GetType().GetProperties(); <span style="color: #008800; font-weight: bold">foreach</span> (<span style="color: #333399; font-weight: bold">var</span> property <span style="color: #008800; font-weight: bold">in</span> properties) { resultExpando.TryAdd(property.Name, property.GetValue(<span style="color: #008800; font-weight: bold">value</span>)); } } <span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">FillExpandoWithLinks</span>(ExpandoObject resultExpando, IEnumerable&lt;LinkDescriptor&gt; relatedLinks) { <span style="color: #333399; font-weight: bold">var</span> linksToAdd = relatedLinks.Select(lk =&gt; <span style="color: #008800; font-weight: bold">new</span> { Rel = lk.Rel, Href = lk.Href }); resultExpando.TryAdd(<span style="background-color: #fff0f0">&quot;Links&quot;</span>, linksToAdd); } } </pre> </div> <p> If absoulute URI in href field is prefered, IUriHelper can be injected in LinksFilter to create URI paths. </p> </div> <div class="comment-date">2020-08-24 23:39 UTC</div> </div> <div class="comment" id="127d2750e83811ea8dd900155d8065e1"> <div class="comment-author"><a href="https://github.com/JesHansen">Jes Hansen</a> <a href="#127d2750e83811ea8dd900155d8065e1">#</a></div> <div class="comment-content"> <p> Mark, thanks for figuring out the tricky parts so we don't have to. :-) </p> <p> I did not see a link to a repo with the completed code from this article, and a cursory look around your Github profile didn't give away any obvious clues. Is the example code in the article part of a repo we can clone? If so, could you please provide a link? </p> </div> <div class="comment-date">2020-08-27 07:45 UTC</div> </div> <div class="comment" id="13197699aaff4a90901910a1975c4de0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#13197699aaff4a90901910a1975c4de0">#</a></div> <div class="comment-content"> <p> Jes, it's part of a larger project that I'm currently working on. Eventually, I hope to publish it, but it's not yet in a state where I wish to do that. </p> <p> Did I leave out essential details that makes it hard to reproduce the idea? </p> </div> <div class="comment-date">2020-08-27 08:07 UTC</div> </div> <div class="comment" id="0e3477f6e84311eaadc700155d8065e1"> <div class="comment-author"><a href="https://github.com/JesHansen">Jes Hansen</a> <a href="#0e3477f6e84311eaadc700155d8065e1">#</a></div> <div class="comment-content"> <p> No, your presentation was fine. Looking forward to see the completed project! </p> </div> <div class="comment-date">2020-08-27 08:57 UTC</div> </div> <div class="comment" id="a5f42d7c00e947a885506abdd2a16628"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a5f42d7c00e947a885506abdd2a16628">#</a></div> <div class="comment-content"> <p> Mehdi, thank you for writing. It's true that the <code>LinksFilter</code> implementation code contains a <code>switch</code> statement, and that this is one of multiple possible designs. I do believe that this is a trade-off rather than a problem per se. </p> <p> That <code>switch</code> statement is an implementation detail of the filter, and something I might decide to change in the future. I did choose that implementation, though, because I it was the simplest design that came to my mind. As presented, the <code>switch</code> statement just calls some private helper methods (all called <code>AddLinks</code>), but if one wanted that code close to the rest of the Controller code, one could just move those helper methods to the relevant Controller classes. </p> <p> While you wouldn't need to resort to Reflection to do that, it's true that this would leave that <code>switch</code> statement as a central place where developers would have to go if they add a new resource. It's true that your proposed solution addresses that problem, but doesn't it just shift the burden somewhere else? Now, developers will have to know that they ought to add a <code>RegisterLinks</code> method with a specific signature to their Controller classes. This replaces a design with compile-time checking with something that may fail at run time. How is that an improvement? </p> <p> I think that I understand your other point about the distance of code, but it assumes a particular notion of REST that I find parochial. Most (.NET) developers I've met design REST APIs in a code-centric (if not a database-centric) way. They tend to conflate <em>representations</em> with <em>resources</em> and translate both to Controller classes. </p> <p> The idea behind <em>Representational State Transfer</em>, however, is to decouple state from representation. Resources have state, but can have multiple representations. Vice versa, many resources may share the same representation. In the code base I used for this article, not only do I have three overloaded <code>Get</code> methods on <code>CalendarController</code> that produce <code>CalendarDto</code> representations, I also have a <code>ScheduleController</code> class that does the same. </p> <p> Granted, not all REST API code bases are designed like this. I admit that what actually motivated me to do things like this was to avoid having to inherit from <code>ControllerBase</code>. Moving all the code that relies heavily on the ASP.NET infrastructure keeps the Controller classes lighter, and thus easier to test. I should probably write an article about that... </p> </div> <div class="comment-date">2020-09-01 07:56 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Unit testing is fine https://blog.ploeh.dk/2020/08/17/unit-testing-is-fine 2020-08-17T05:29:00+00:00 Mark Seemann <div id="post"> <p> <em>Unit testing considered harmful? I think not.</em> </p> <p> Once in a while, some article, video, or podcast makes the rounds on social media, arguing that unit testing is bad, overrated, harmful, or the like. I'm not going to link to any specific resources, because this post isn't an attack on any particular piece of work. Many of these are sophisticated, thoughtful, and make good points, but still arrive at the wrong conclusion. </p> <p> The line of reasoning tends to be to show examples of bad unit tests and conclude that, based on the examples, unit tests are bad. </p> <p> The power of examples is great, but clearly, this is a logical fallacy. </p> <h3 id="5dbc2a18fc7f439b9db0282f0613b2a5"> Symbolisation <a href="#5dbc2a18fc7f439b9db0282f0613b2a5" title="permalink">#</a> </h3> <p> In case it isn't clear that the argument is invalid, I'll demonstrate it using the techniques I've learned from <a href="https://bit.ly/predicate-logic-intro">Howard Pospesel's introduction to predicate logic</a>. </p> <p> We can begin by symbolising the natural-language arguments into well-formed formulas. I'll keep this as simple as possible: </p> <p> <pre>∃xBx ⊢ ∀xBx (Domain: unit tests; Bx = x is bad)</pre> </p> <p> Basically, <code>Bx</code> states that <code>x</code> is bad, where <code>x</code> is a unit test. <code>∃xBx</code> is a statement that there exists a unit test <code>x</code> for which <code>x</code> is bad; i.e. <em>bad unit tests exist</em>. The statement <code>∀xBx</code> claims that for all unit tests <code>x</code>, <code>x</code> is bad; i.e. <em>all unit tests are bad</em>. The turnstile symbol <code>⊢</code> in the middle indicates that the antecedent on the left proves the consequent to the right. </p> <p> Translated back to natural language, the claim is this: <em>Because bad unit tests exist, all unit tests are bad.</em> </p> <p> You can trivially prove this <a href="https://en.wikipedia.org/wiki/Sequent">sequent</a> invalid. </p> <h3 id="a5f5e5c8e1f94696b8ffb86a119170b4"> Logical fallacy <a href="#a5f5e5c8e1f94696b8ffb86a119170b4" title="permalink">#</a> </h3> <p> One way to prove the sequent invalid is to use a truth tree: </p> <p> <img src="/content/binary/truth-tree-open-all-unit-tests-bad.png" alt="Truth tree that disproves the sequent about all unit tests being bad."> </p> <p> Briefly, the way this works is that the statements on the left-hand side represent truth, while the ones to the right are false. By placing the antecedent on the left, but the consequent on the right, you're basically assuming the sequent to be wrong. This is is also the way you <em>prove</em> correct sequents true; if the conclusion is assumed false, a logical truth should lead to a contradiction. If it doesn't, the sequent is invalid. That's what happens here. </p> <p> The tree remains <em>open</em>, which means that the original sequent is invalid. It's a logical fallacy. </p> <h3 id="c19e880f945d4a44a40ca1ccb9b96f74"> Counter examples <a href="#c19e880f945d4a44a40ca1ccb9b96f74" title="permalink">#</a> </h3> <p> You probably already knew that. All it takes to counter a universal assertion such as <em>all unit tests are bad</em> is to produce a counter example. One is sufficient, because if just a single good unit test exists, it can't be true that <em>all</em> are bad. </p> <p> Most of the think pieces that argue that unit testing is bad do so by showing examples of bad unit tests. These tests typically involve lots of mocks and stubs; they tend to test the interaction between internal components instead of the components themselves, or the system as a whole. I agree that this often leads to <a href="http://xunitpatterns.com/Fragile%20Test.html">fragile tests</a>. </p> <p> While I still spend my testing energy according to the <a href="https://martinfowler.com/bliki/TestPyramid.html">Test Pyramid</a>, I don't write unit tests like that. I rarely use dynamic mock libraries. Instead, I <a href="/2020/03/02/impureim-sandwich">push impure actions to the boundary of the system</a> and write most of the application code as <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>, which are <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. No test-induced damage there. </p> <p> I follow up with <a href="/outside-in-tdd">boundary tests</a> that demonstrate that the <a href="/2015/12/21/integration-testing-composed-functions">functions are integrated into a working system</a>. That's just another layer in the Test Pyramid, but smaller. You don't need that many integration tests when you have a foundation of good unit tests. </p> <p> While I'm currently working on <a href="/2021/06/14/new-book-code-that-fits-in-your-head">a larger body of work</a> that showcases this approach, this blog <a href="/2019/04/01/an-example-of-state-based-testing-in-c">already has examples of this</a>. </p> <h3 id="414e235a672042a6a07e572027250df0"> Conclusion <a href="#414e235a672042a6a07e572027250df0" title="permalink">#</a> </h3> <p> You often hear or see the claim that unit tests are bad. The supporting argument is that a particular (popular, I admit) style of unit testing is bad. </p> <p> If the person making this claim only knows of that single style of unit testing, it's natural to jump to the conclusion that all unit testing must be bad. </p> <p> That's not the case. I write most of my unit tests in a style dissimilar from the interaction-heavy, mocks-and-stubs-based style that most people use. These test have a low maintenance burden and don't cause test-induced damage. </p> <p> Unit testing is fine. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An ASP.NET Core URL Builder https://blog.ploeh.dk/2020/08/10/an-aspnet-core-url-builder 2020-08-10T06:59:00+00:00 Mark Seemann <div id="post"> <p> <em>A use case for the Immutable Fluent Builder design pattern variant.</em> </p> <p> The <a href="/2020/02/10/builder-isomorphisms">Fluent Builder</a> design pattern is popular in object-oriented programming. Most programmers use the mutable variant, while I favour the immutable alternative. The advantages of Immutable Fluent Builders, however, may not be immediately clear. <blockquote> <p> "I never thought of someone reusing a configured builder (soulds like too big class/SRP violation)." </p> <footer><cite><a href="https://twitter.com/ELuciusFTW/status/1226800015047348224">Guy Buss</a></cite></footer> </blockquote> It inspires me when I encounter a differing perspective. Could I be wrong? Or did I fail to produce a compelling example? </p> <p> It's possible that I'm wrong, but in my <a href="/2020/02/10/builder-isomorphisms">my recent article on Builder isomorphisms</a> I focused on the pattern variations themselves, to the point where a convincing example wasn't my top priority. </p> <p> I recently encountered a good use case for an Immutable Fluent Builder. </p> <p> <ins datetime="2021-07-23T07:03Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <h3 id="e2761ee2b9654ad982a2ec1a6264c76f"> Build links <a href="#e2761ee2b9654ad982a2ec1a6264c76f" title="permalink">#</a> </h3> <p> I was developing a REST API and wanted to generate some links like these: </p> <p> <pre>{ "links": [ { "rel": "urn:reservations", "href": "http://localhost:53568/reservations" }, { "rel": "urn:year", "href": "http://localhost:53568/calendar/2020" }, { "rel": "urn:month", "href": "http://localhost:53568/calendar/2020/7" }, { "rel": "urn:day", "href": "http://localhost:53568/calendar/2020/7/7" } ] }</pre> </p> <p> As I recently described, <a href="/2020/08/03/using-the-nameof-c-keyword-with-aspnet-3-iurlhelper">the ASP.NET Core Action API is tricky</a>, and since there was some repetition, I was looking for a way to reduce the code duplication. At first I just thought I'd make a few private helper methods, but then it occurred to me that an Immutable Fluent Builder as an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> to the <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.urlhelperextensions.action">Action API</a> might offer a fertile alternative. </p> <h3 id="5adf09c648a54679b1b137776e4f1192"> UrlBuilder class <a href="#5adf09c648a54679b1b137776e4f1192" title="permalink">#</a> </h3> <p> The various <code>Action</code> overloads all accept null arguments, so there's effectively no clear invariants to enforce on that dimension. While I wanted an Immutable Fluent Builder, I made all the fields nullable. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;action; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;controller; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">object</span>?&nbsp;values; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>(<span style="color:blue;">string</span>?&nbsp;action,&nbsp;<span style="color:blue;">string</span>?&nbsp;controller,&nbsp;<span style="color:blue;">object</span>?&nbsp;values) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.action&nbsp;=&nbsp;action; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.controller&nbsp;=&nbsp;controller; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.values&nbsp;=&nbsp;values; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span></pre> </p> <p> I also gave the <code>UrlBuilder</code> class a public constructor and a private copy constructor. That's the standard way I implement that pattern. </p> <p> Most of the modification methods are straightforward: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>&nbsp;WithAction(<span style="color:blue;">string</span>&nbsp;newAction) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>(newAction,&nbsp;controller,&nbsp;values); } <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>&nbsp;WithValues(<span style="color:blue;">object</span>&nbsp;newValues) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>(action,&nbsp;controller,&nbsp;newValues); }</pre> </p> <p> I wanted to encapsulate <a href="/2020/08/03/using-the-nameof-c-keyword-with-aspnet-3-iurlhelper">the suffix-handling behaviour I recently described</a> in the appropriate method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>&nbsp;WithController(<span style="color:blue;">string</span>&nbsp;newController) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(newController&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(newController)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;controllerSuffix&nbsp;=&nbsp;<span style="color:#a31515;">&quot;controller&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;index&nbsp;=&nbsp;newController.LastIndexOf( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controllerSuffix, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">StringComparison</span>.OrdinalIgnoreCase); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(0&nbsp;&lt;=&nbsp;index) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newController&nbsp;=&nbsp;newController.Remove(index); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>(action,&nbsp;newController,&nbsp;values); }</pre> </p> <p> The <code>WithController</code> method handles both the case where <code>newController</code> is suffixed by <code>"Controller"</code> and the case where it isn't. I also wrote unit tests to verify that the implementation works as intended. </p> <p> Finally, a Builder should have a method to build the desired object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;BuildAbsolute(<span style="color:#2b91af;">IUrlHelper</span>&nbsp;url) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(url&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(url)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actionUrl&nbsp;=&nbsp;url.Action( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controller, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url.ActionContext.HttpContext.Request.Scheme, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url.ActionContext.HttpContext.Request.Host.ToUriComponent()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(actionUrl); }</pre> </p> <p> One could imagine also defining a <code>BuildRelative</code> method, but I didn't need it. </p> <h3 id="bec08c507ce7475eb0038723c6f6f8a8"> Generating links <a href="#bec08c507ce7475eb0038723c6f6f8a8" title="permalink">#</a> </h3> <p> Each of the objects shown above are represented by a simple Data Transfer Object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">LinkDto</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Rel&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;Href&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> My next step was to define an extension method on <code>Uri</code>, so that I could turn a URL into a link: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">LinkDto</span>&nbsp;Link(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;uri,&nbsp;<span style="color:blue;">string</span>&nbsp;rel) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">LinkDto</span>&nbsp;{&nbsp;Rel&nbsp;=&nbsp;rel,&nbsp;Href&nbsp;=&nbsp;uri.ToString()&nbsp;}; }</pre> </p> <p> With that function I could now write code like this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">LinkDto</span>&nbsp;CreateYearLink() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithAction(<span style="color:blue;">nameof</span>(<span style="color:#2b91af;">CalendarController</span>.Get)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithController(<span style="color:blue;">nameof</span>(<span style="color:#2b91af;">CalendarController</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithValues(<span style="color:blue;">new</span>&nbsp;{&nbsp;year&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now.Year&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.BuildAbsolute(Url) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Link(<span style="color:#a31515;">&quot;urn:year&quot;</span>); }</pre> </p> <p> It's acceptable, but verbose. This only creates the <code>urn:year</code> link; to create the <code>urn:month</code> and <code>urn:day</code> links, I needed similar code. Only the <code>WithValues</code> method calls differed. The calls to <code>WithAction</code> and <code>WithController</code> were identical. </p> <h3 id="e003c737cf604f83bd61dfba8045754b"> Shared Immutable Builder <a href="#e003c737cf604f83bd61dfba8045754b" title="permalink">#</a> </h3> <p> Since <code>UrlBuilder</code> is immutable, I can trivially define a shared instance: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>&nbsp;calendar&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UrlBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithAction(<span style="color:blue;">nameof</span>(<span style="color:#2b91af;">CalendarController</span>.Get)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithController(<span style="color:blue;">nameof</span>(<span style="color:#2b91af;">CalendarController</span>));</pre> </p> <p> This enabled me to write more succinct methods for each of the relationship types: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">LinkDto</span>&nbsp;LinkToYear(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IUrlHelper</span>&nbsp;url,&nbsp;<span style="color:blue;">int</span>&nbsp;year) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;calendar.WithValues(<span style="color:blue;">new</span>&nbsp;{&nbsp;year&nbsp;}).BuildAbsolute(url).Link(<span style="color:#a31515;">&quot;urn:year&quot;</span>); } <span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">LinkDto</span>&nbsp;LinkToMonth(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IUrlHelper</span>&nbsp;url,&nbsp;<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;calendar.WithValues(<span style="color:blue;">new</span>&nbsp;{&nbsp;year,&nbsp;month&nbsp;}).BuildAbsolute(url).Link(<span style="color:#a31515;">&quot;urn:month&quot;</span>); } <span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">LinkDto</span>&nbsp;LinkToDay(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IUrlHelper</span>&nbsp;url,&nbsp;<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">int</span>&nbsp;month,&nbsp;<span style="color:blue;">int</span>&nbsp;day) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;calendar.WithValues(<span style="color:blue;">new</span>&nbsp;{&nbsp;year,&nbsp;month,&nbsp;day&nbsp;}).BuildAbsolute(url).Link(<span style="color:#a31515;">&quot;urn:day&quot;</span>); }</pre> </p> <p> This is possible exactly because <code>UrlBuilder</code> is immutable. Had the Builder been mutable, such sharing would have created an <a href="https://en.wikipedia.org/wiki/Aliasing_(computing)">aliasing</a> bug, as I <a href="/2020/02/10/builder-isomorphisms">previously described</a>. Immutability enables reuse. </p> <h3 id="cc70ea76298547e893d883bcb9983f92"> Conclusion <a href="#cc70ea76298547e893d883bcb9983f92" title="permalink">#</a> </h3> <p> I got my first taste of functional programming around 2010. Since then, when I'm not programming in <a href="https://fsharp.org">F#</a> or <a href="https://www.haskell.org">Haskell</a>, I've steadily worked on identifying good ways to enjoy the benefits of functional programming in C#. </p> <p> Immutability is a fairly low-hanging fruit. It requires more boilerplate code, but apart from that, it's easy to make classes immutable in C#, and Visual Studio has plenty of refactorings that make it easier. </p> <p> Immutability is one of those features you're unlikely to realise that you're missing. When it's not there, you work around it, but when it's there, it simplifies many tasks. </p> <p> The example you've seen in this article relates to the Fluent Builder pattern. At first glance, it seems as though a mutable Fluent Builder has the same capabilities as a corresponding Immutable Fluent Builder. You can, however, build upon shared Immutable Fluent Builders, which you can't with mutable Fluent Builders. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Using the nameof C# keyword with ASP.NET 3 IUrlHelper https://blog.ploeh.dk/2020/08/03/using-the-nameof-c-keyword-with-aspnet-3-iurlhelper 2020-08-03T10:01:00+00:00 Mark Seemann <div id="post"> <p> <em>How to generate links to other resources in a refactoring-friendly way.</em> </p> <p> I recently spent a couple of hours yak-shaving, and despite much Googling couldn't find any help on the internet. I'm surprised that the following problem turned out to be so difficult to figure out, so it may just be that I'm ignorant or that my web search skills failed me that day. On the other hand, if this really is as difficult as I found it, perhaps this article can save some other poor soul an hour or two. </p> <p> <ins datetime="2021-07-24T07:53Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <p> I was developing a REST API and wanted to generate some links like these: </p> <p> <pre>{ "links": [ { "rel": "urn:reservations", "href": "http://localhost:53568/reservations" }, { "rel": "urn:year", "href": "http://localhost:53568/calendar/2020" }, { "rel": "urn:month", "href": "http://localhost:53568/calendar/2020/7" }, { "rel": "urn:day", "href": "http://localhost:53568/calendar/2020/7/7" } ] }</pre> </p> <p> Like previous incarnations of the framework, ASP.NET Core 3 has <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.urlhelperextensions.action">an API for generating links to a method on a Controller</a>. I just couldn't get it to work. </p> <h3 id="6c7ec5c13e9942098dc18cbcc43b345c"> Using nameof <a href="#6c7ec5c13e9942098dc18cbcc43b345c" title="permalink">#</a> </h3> <p> I wanted to generate a URL like <code>http://localhost:53568/calendar/2020</code> in a refactoring-friendly way. While ASP.NET wants you to define HTTP resources as methods (<em>actions</em>) on <em>Controllers</em>, the various <code>Action</code> overloads want you to identify these actions and Controllers as strings. What happens if someone renames one of those methods or Controller classes? </p> <p> That's what the C# <code>nameof</code> keyword is for. </p> <p> I naively called the <code>Action</code> method like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;href&nbsp;=&nbsp;Url.Action( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(<span style="color:#2b91af;">CalendarController</span>.Get), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(<span style="color:#2b91af;">CalendarController</span>), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;year&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now.Year&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;Url.ActionContext.HttpContext.Request.Scheme, &nbsp;&nbsp;&nbsp;&nbsp;Url.ActionContext.HttpContext.Request.Host.ToUriComponent());</pre> </p> <p> Looks good, doesn't it? </p> <p> I thought so, but it didn't work. In the time-honoured tradition of mainstream programming languages, the method just silently fails to return a value and instead returns null. That's not helpful. What might be the problem? No clue is provided. It just doesn't work. </p> <h3 id="3f7f01514e5a4c518ddd37226352970e"> Strip off the suffix <a href="#3f7f01514e5a4c518ddd37226352970e" title="permalink">#</a> </h3> <p> It turns out that the <code>Action</code> method expects the <code>controller</code> argument to <em>not</em> contain the <code>Controller</code> suffix. Not surprisingly, <code>nameof(CalendarController)</code> becomes the string <code>"CalendarController"</code>, which doesn't work. </p> <p> It took me some time to figure out that I was supposed to pass a string like <code>"Calendar"</code>. <em>That</em> works! </p> <p> As a first pass at the problem, then, I changed my code to this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;controllerName&nbsp;=&nbsp;<span style="color:blue;">nameof</span>(<span style="color:#2b91af;">CalendarController</span>); <span style="color:blue;">var</span>&nbsp;controller&nbsp;=&nbsp;controllerName.Remove( &nbsp;&nbsp;&nbsp;&nbsp;controllerName.LastIndexOf( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Controller&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">StringComparison</span>.Ordinal)); <span style="color:blue;">var</span>&nbsp;href&nbsp;=&nbsp;Url.Action( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(<span style="color:#2b91af;">CalendarController</span>.Get), &nbsp;&nbsp;&nbsp;&nbsp;controller, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;year&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Now.Year&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;Url.ActionContext.HttpContext.Request.Scheme, &nbsp;&nbsp;&nbsp;&nbsp;Url.ActionContext.HttpContext.Request.Host.ToUriComponent());</pre> </p> <p> That also works, and is more refactoring-friendly. You can rename both the Controller class and the method, and the link should still work. </p> <h3 id="8285d8ffd53f44808a899c33b8712bda"> Conclusion <a href="#8285d8ffd53f44808a899c33b8712bda" title="permalink">#</a> </h3> <p> The <code>UrlHelperExtensions.Action</code> methods expect the <code>controller</code> to be the 'semantic' name of the Controller, if you will - not the actual class name. If you're calling it with values produced with the <code>nameof</code> keyword, you'll have to strip the <code>Controller</code> suffix away. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Task asynchronous programming as an IO surrogate https://blog.ploeh.dk/2020/07/27/task-asynchronous-programming-as-an-io-surrogate 2020-07-27T08:27:00+00:00 Mark Seemann <div id="post"> <p> <em>Is task asynchronous programming a substitute for the IO container? An article for C# programmers.</em> </p> <p> This article is part of <a href="/2020/06/08/the-io-container">an article series about the IO container in C#</a>. In the previous articles, you've seen how a type like <code>IO&lt;T&gt;</code> can be used to distinguish between <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> and impure actions. While it's an effective and elegant solution to the problem, it depends on a convention: that all impure actions return <code>IO</code> objects, which are opaque to pure functions. </p> <p> In reality, .NET base class library methods don't do that, and it's unrealistic that this is ever going to happen. It'd require a breaking reset of the entire .NET ecosystem to introduce this design. </p> <p> A comparable reset did, however, happen a few years ago. </p> <h3 id="dce87c53f2fa4b1db4c05b48e8d65870"> TAP reset <a href="#dce87c53f2fa4b1db4c05b48e8d65870" title="permalink">#</a> </h3> <p> Microsoft introduced the <a href="https://docs.microsoft.com/dotnet/csharp/programming-guide/concepts/async">task asynchronous programming</a> (TAP) model some years ago. Operations that involve <a href="https://en.wikipedia.org/wiki/Input/output">I/O</a> got a new return type. Not <code>IO&lt;T&gt;</code>, but <code>Task&lt;T&gt;</code>. </p> <p> The .NET framework team began a long process of adding asynchronous alternatives to existing APIs that involve I/O. Not as breaking changes, but by adding new, asynchronous methods side-by-side with older methods. <a href="https://docs.microsoft.com/dotnet/api/system.data.sqlclient.sqlcommand.executereaderasync">ExecuteReaderAsync</a> as an alternative to <a href="https://docs.microsoft.com/dotnet/api/system.data.sqlclient.sqlcommand.executereader">ExecuteReader</a>, <a href="https://docs.microsoft.com/dotnet/api/system.io.file.readalllinesasync">ReadAllLinesAsync</a> side by side with <a href="https://docs.microsoft.com/dotnet/api/system.io.file.readalllines">ReadAllLines</a>, and so on. </p> <p> Modern APIs exclusively with asynchronous methods appeared. For example, the <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpclient">HttpClient</a> class only affords asynchronous I/O-based operations. </p> <p> The TAP reset was further strengthened by the move from .NET to .NET Core. Some frameworks, most notably ASP.NET, were redesigned on a fundamentally asynchronous core. </p> <p> In 2020, most I/O operations in .NET are easily recognisable, because they return <code>Task&lt;T&gt;</code>. </p> <h3 id="3e84ba1d588a42689d7e8741315d22b5"> Task as a surrogate IO <a href="#3e84ba1d588a42689d7e8741315d22b5" title="permalink">#</a> </h3> <p> I/O operations are impure. Either you're receiving input from outside the running process, which is consistently non-deterministic, or you're writing to an external resource, which implies a side effect. It might seem natural to think of <code>Task&lt;T&gt;</code> as a replacement for <code>IO&lt;T&gt;</code>. <a href="https://twitter.com/SzymonPobiega/status/713001580077965312">Szymon Pobiega had a similar idea in 2016</a>, and I <a href="/2016/04/11/async-as-surrogate-io">investigated his idea in an article</a>. This was based on <a href="https://fsharp.org">F#</a>'s <code>Async&lt;'a&gt;</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a>, which is <a href="/2018/09/24/asynchronous-functors">equivalent to <code>Task&lt;T&gt;</code></a> - except when it comes to <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>. </p> <p> Unfortunately, <code>Task&lt;T&gt;</code> is far from a perfect replacement of <code>IO&lt;T&gt;</code>, because the .NET base class library (BCL) still contains plenty of impure actions that 'look' pure. Examples include <a href="https://docs.microsoft.com/dotnet/api/system.console.writeline">Console.WriteLine</a>, the parameterless <a href="https://docs.microsoft.com/dotnet/api/system.random.-ctor">Random constructor</a>, <a href="https://docs.microsoft.com/dotnet/api/system.guid.newguid">Guid.NewGuid</a>, and <a href="https://docs.microsoft.com/dotnet/api/system.datetime.now">DateTime.Now</a> (arguably a candidate for the worst-designed API in the BCL). None of those methods return tasks, which they ought to if tasks should serve as easily recognisable signifiers of impurity. </p> <p> Still, you could write asynchronous <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapters</a> over such APIs. Your <code>Console</code> Adapter might present this API: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Console</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;ReadLine(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;WriteLine(<span style="color:blue;">string</span>&nbsp;value); }</pre> </p> <p> Moreover, the <code>Clock</code> API might look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Clock</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;GetLocalTime(); }</pre> </p> <p> Modern versions of C# enable you to write asynchronous <a href="https://en.wikipedia.org/wiki/Entry_point">entry points</a>, so the <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">hello world</a> example shown in this article series becomes: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;What&#39;s&nbsp;your&nbsp;name?&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;name&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#2b91af;">Console</span>.ReadLine(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;now&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#2b91af;">Clock</span>.GetLocalTime(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;greeting&nbsp;=&nbsp;<span style="color:#2b91af;">Greeter</span>.Greet(now,&nbsp;name); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(greeting); }</pre> </p> <p> That's nice <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> C# code, so what's not to like? </p> <h3 id="8a11bbfe9cbc4e72bd90ea6b787c0037"> No referential transparency <a href="#8a11bbfe9cbc4e72bd90ea6b787c0037" title="permalink">#</a> </h3> <p> The above <code>Main</code> example is probably as good as it's going to get in C#. I've nothing against that style of C# programming, but you shouldn't believe that this gives you compile-time checking of referential transparency. It doesn't. </p> <p> Consider a simple function like this, written using the <code>IO</code> container shown in previous articles: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;AmIEvil() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Side&nbsp;effect!&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&quot;No,&nbsp;I&#39;m&nbsp;not.&quot;</span>; }</pre> </p> <p> Is this method referentially transparent? Surprisingly, despite the apparent side effect, it is. The reason becomes clearer if you write the code so that it explicitly ignores the return value: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;AmIEvil() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;_&nbsp;=&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Side&nbsp;effect!&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&quot;No,&nbsp;I&#39;m&nbsp;not.&quot;</span>; }</pre> </p> <p> The <code>Console.WriteLine</code> method returns an object that represents a computation that might take place. This <code>IO&lt;Unit&gt;</code> object, however, never escapes the method, and thus never runs. The side effect never takes place, which means that the method is referentially transparent. You can replace <code>AmIEvil()</code> with its return value <code>"No, I'm not."</code>, and your program would behave exactly the same. </p> <p> Consider what happens when you replace <code>IO</code> with <code>Task</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;AmIEvil() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;_&nbsp;=&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Side&nbsp;effect!&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&quot;Yes,&nbsp;I&nbsp;am.&quot;</span>; }</pre> </p> <p> Is this method a pure function? No, it's not. The problem is that the most common way that .NET libraries return tasks is that the task is already running when it's returned. This is also the case here. As soon as you call this version of <code>Console.WriteLine</code>, the task starts running on a background thread. Even though you ignore the task and return a plain <code>string</code>, the side effect sooner or later takes place. You can't replace a call to <code>AmIEvil()</code> with its return value. If you did, the side effect wouldn't happen, and that would change the behaviour of your program. </p> <p> Contrary to <code>IO</code>, tasks don't guarantee referential transparency. </p> <h3 id="aff1e4cb382b494ea0624ef63311da81"> Conclusion <a href="#aff1e4cb382b494ea0624ef63311da81" title="permalink">#</a> </h3> <p> While it'd be technically possible to make C# distinguish between pure and impure code at compile time, it'd require such a breaking reset to the entire .NET ecosystem that it's unrealistic to hope for. It seems, though, that there's enough overlap with the design of <code>IO&lt;T&gt;</code> and task asynchronous programming that the latter might fill that role. </p> <p> Unfortunately it doesn't, because it fails to guarantee referential transparency. It's better than nothing, though. Most C# programmers have now learned that while <code>Task</code> objects come with a <a href="https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1.result">Result</a> property, you shouldn't use it. Instead, you should write your entire program using <code>async</code> and <code>await</code>. That, at least, takes you halfway towards where you want to be. </p> <p> The compiler, on the other hand, doesn't help you when it comes to those impure actions that look pure. Neither does it protect you against asynchronous side effects. Diligence, code reviews, and programming discipline are still required if you want to separate pure functions from impure actions. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="aeb6990f553842739915d980119a48d9"> <div class="comment-author">Matt Heuer <a href="#aeb6990f553842739915d980119a48d9">#</a></div> <div class="comment-content"> <p> This is a great idea. It seems like the only Problem with Tasks is that they are usually already started, either on the current or a Worker Thread. If we return Tasks that are not started yet, then Side-Effects don't happen until we await them. And we have to await them to get their Result or use them in other Tasks. I experimented with a modified GetTime() Method returning a new Task that is not run yet: </p> <pre>public static Task&lt;DateTime&gt; GetTime() => new Task&lt;DateTime&gt;(() => DateTime.Now);</pre> <p> Using a SelectMany Method that ensures that Tasks have been run, the Time is not evaluated until the resulting Task is awaited or another SelectMany is built using the Task from the first SelectMany. The Time of one such Task is also evaluated only once. On repeating Calls the same Result is returned: </p> <pre>public static async Task&lt;TResult&gt; SelectMany&lt;T, TResult&gt;(this Task&lt;T&gt; source , Func&lt;T, Task&lt;TResult&gt;&gt; project) { T t = await source.GetResultAsync(); Task&lt;TResult&gt;? ret = project(t); return await ret.GetResultAsync(); } /// &lt;summary&gt; Asynchronously runs the &lt;paramref name="task"/&gt; to Completion &lt;/summary&gt; /// &lt;returns&gt;&lt;see cref="Task{T}.Result"/&gt;&lt;/returns&gt; public static async Task&lt;T&gt; GetResultAsync&lt;T&gt;(this Task&lt;T&gt; task) { switch (task.Status) { case TaskStatus.Created: await Task.Run(task.RunSynchronously); return task.Result; case TaskStatus.WaitingForActivation: case TaskStatus.WaitingToRun: case TaskStatus.Running: case TaskStatus.WaitingForChildrenToComplete: return await task; case TaskStatus.Canceled: case TaskStatus.Faulted: case TaskStatus.RanToCompletion: return task.Result; //let the Exceptions fly... default: throw new ArgumentOutOfRangeException(); } }</pre> <p> Since I/O Operations with side-effects are usually asynchronous anyway, Tasks and I/O are a good match. <br> Consistenly not starting Tasks and using this SelectMany Method either ensures Method purity or enforces to return the Task. To avoid ambiguity with started Tasks a Wrapper-IO-Class could be constructed, that always takes and creates unstarted Tasks. Am I missing something or do you think this would not be worth the effort? Are there more idiomatic ways to start enforcing purity in C#, except e.g. using the [Pure] Attribute and StyleCop-Warnings for unused Return Values? </p> </div> <div class="comment-date">2021-02-27 22:25 UTC</div> </div> <div class="comment" id="35f7c2e8cdc84a89b79771f541530025"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#35f7c2e8cdc84a89b79771f541530025">#</a></div> <div class="comment-content"> <p> Matt, thank you for writing. That's essentially <a href="/2016/04/11/async-as-surrogate-io#093a7032a754405a80bc4382d1e2cab9">how F# asynchronous workflows work</a>. </p> </div> <div class="comment-date">2021-03-03 9:05 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Closing database connections during test teardown https://blog.ploeh.dk/2020/07/20/closing-database-connections-during-test-teardown 2020-07-20T07:20:00+00:00 Mark Seemann <div id="post"> <p> <em>How to close database connections to SQL Server during integration testing teardown.</em> </p> <p> Whenever I need to run integration tests that involve SQL Server, I have a standard approach that <a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/sqlexpressdatabaseinstaller">I've evolved since 2007</a>. It involves <ol> <li>setting up a SQL Server Express database before each test</li> <li>running the test</li> <li>tearing down the database</li> </ol> I've explained the basic process in details in my <a href="/outside-in-tdd">Outside-In Test-Driven Development</a> Pluralsight course. You can also see it in action in <a href="https://github.com/ploeh/dependency-injection-revisited">this GitHub repository</a>. <ins datetime="2021-07-25T06:27Z">The code shown here is part of the sample code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>.</ins> </p> <p> One problem with that approach is that SQL Server doesn't allow you to delete a database if it has existing connections. </p> <h3 id="7de0c4f8bd3d40e99740a39b11570593"> Turn off connection pooling <a href="#7de0c4f8bd3d40e99740a39b11570593" title="permalink">#</a> </h3> <p> I usually solve the problem by turning off connection pooling. For an integration test suite, this is fine. I usually use integration testing to verify functionality - not performance. </p> <p> Turning off connection pooling is easily done by setting the flag to <code>false</code> in the connection string: </p> <p> <code>Server=(LocalDB)\MSSQLLocalDB;Database=Booking;Integrated Security=true;Pooling=false</code> </p> <p> This means that when you get to the teardown phase of the test, you can issue a DDL statement to the <code>master</code> database: </p> <p> <pre>IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'Booking') DROP DATABASE [Booking]</pre> </p> <p> When connection pooling is turned off, no other connections are open when you attempt to do that, and the database (here named <code>Booking</code>) is deleted. </p> <h3 id="b599a9242a7d44f1b23241341db054b8"> Forcibly close other connections <a href="#b599a9242a7d44f1b23241341db054b8" title="permalink">#</a> </h3> <p> Recently, however, I ran into a testing scenario where connection pooling had to be turned on. When you turn on connection pooling, however, the above <code>DROP DATABASE</code> statement fails because at least one connection from the pool is still connected to the database. </p> <p> To solve that issue, I forcibly close other connections during teardown: </p> <p> <pre>IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'Booking') BEGIN -- This closes existing connections: ALTER DATABASE [Booking] SET SINGLE_USER WITH ROLLBACK IMMEDIATE DROP DATABASE [Booking] END</pre> </p> <p> Surprisingly, turning on connection pooling like this makes the integration tests slower. I suppose it's because throwing other connections off the database involves a bit of negotiation between the server and the clients, and that takes some time. </p> <p> While slower, it does enable you to run the integration tests with connection pooling turned on. When you need it, you need it. </p> <h3 id="6a3cf6b38c0f49a3abb1aa8ccbf50644"> Summary <a href="#6a3cf6b38c0f49a3abb1aa8ccbf50644" title="permalink">#</a> </h3> <p> You can run integration tests against a SQL Server Express database. People do it in various ways, but I've found that setting up and tearing down a pristine database for each test case is a robust and maintainable solution to the problem. </p> <p> SQL Server will not, however, allow you to delete a database if other connections exist. The easiest and fastest solution to that problem is to turn off connection pooling. </p> <p> Sometimes, you can't do that, so instead, you can expand your database teardown script so that it closes existing connections before it deletes the database. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0a9ea1fffa1c41ae96981ff4625c4985"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#0a9ea1fffa1c41ae96981ff4625c4985">#</a></div> <div class="comment-content"> <p> This sounds like a great approach. I have been on projects with tests that involved the database, but none of them were designed as well as this. I will be sure to come back to this post when we add a database to my current project. </p> <p> My understanding is that <a href="https://expressdb.io/sql-server-express-vs-localdb.html#what-is-localdb">SQL Server Express and LocalDB are not the same thing</a>. Are you using SQL Server Express or LocalDB? Do you prefer one over the other for this database testing approach of yours? </p> </div> <div class="comment-date">2020-07-25 19:58 UTC</div> </div> <div class="comment" id="4fd6901499a44fc084d58673aa762cc1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4fd6901499a44fc084d58673aa762cc1">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. It's not really my area of expertise. I use the one bundled with Visual Studio, so I suppose that's actually LocalDB, and not SQL Server Express. </div> <div class="comment-date">2020-07-26 13:33 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Implementation of the C# IO container https://blog.ploeh.dk/2020/07/13/implementation-of-the-c-io-container 2020-07-13T06:02:00+00:00 Mark Seemann <div id="post"> <p> <em>Implementation details of the C# IO container.</em> </p> <p> This article is part of <a href="/2020/06/08/the-io-container">an article series about the IO container in C#</a>. In the previous articles, you've seen how a type like <code>IO&lt;T&gt;</code> can be used to distinguish between <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> and impure actions. </p> <p> The point of the article series is to illustrate the concept that <a href="https://www.haskell.org">Haskell</a> uses to impose the <a href="/2018/11/19/functional-architecture-a-definition">functional interaction law</a> at compile time. The implementation details really aren't important. Still, I believe that I know my readership well enough that a substantial fraction would be left unsatisfied if I didn't share the implementation details. </p> <p> I consider this an appendix to the article series. It's not really what this is all about, but here it is, nonetheless. </p> <h3 id="dcf2521a1b8c40df9d45e97a43cfc546"> Constructor <a href="#dcf2521a1b8c40df9d45e97a43cfc546" title="permalink">#</a> </h3> <p> Based on the public API <a href="/2020/06/15/io-container-in-a-parallel-c-universe">already published</a>, the constructor implementation hardly comes as a surprise. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;item; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;item) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.item&nbsp;=&nbsp;item; }</pre> </p> <p> The <code>IO&lt;T&gt;</code> class is little more than a wrapper around a lazily evaluated function, with the restriction that while you can put a <code>Func&lt;T&gt;</code> object into an <code>IO</code> object, you can never get it out again. Thus, the <code>item</code> is a <code>private</code> class field instead of a public property. </p> <h3 id="50d90cc423f04f5bb4e31a0d6c9b12c8"> SelectMany <a href="#50d90cc423f04f5bb4e31a0d6c9b12c8" title="permalink">#</a> </h3> <p> The <code>SelectMany</code> method is a little more tricky: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(()&nbsp;=&gt;&nbsp;selector(item()).item()); }</pre> </p> <p> To guarantee <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>, we don't want the method to trigger evaluation of the lazy value, so the <code>selector</code> has to run inside a new lazy computation. This produces a lazy <code>IO</code> value that the method then has to unwrap inside another lazy computation. Such a translation from <code>Func&lt;IO&lt;TResult&gt;&gt;</code> to a new <code>IO</code> object with a <code>Func&lt;TResult&gt;</code> inside it is reminiscent of what in Haskell is known as a <em>traversal</em>. </p> <h3 id="b6c088710f6340689376b0ddf3b7df01"> UnsafePerformIO <a href="#b6c088710f6340689376b0ddf3b7df01" title="permalink">#</a> </h3> <p> Finally, the <code>UnsafePerformIO</code> method isn't part of the API, but as explained in the <a href="/2020/07/06/referential-transparency-of-io">previous article</a>, this is the special method that the hypothetical parallel-world framework calls on the <code>IO&lt;Unit&gt;</code> returned by <code>Main</code> methods. </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;UnsafePerformIO() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;item(); }</pre> </p> <p> Since only the framework is supposed to call this method, it's <code>internal</code> by design. The only thing it does is to force evaluation of the lazy <code>item</code>. </p> <h3 id="38830b00fbbf4911b8a7ebd723e138ea"> Conclusion <a href="#38830b00fbbf4911b8a7ebd723e138ea" title="permalink">#</a> </h3> <p> Most of the implementation of <code>IO&lt;T&gt;</code> is straightforward, with the single exception of <code>SelectMany</code>, which has to jump through a few hoops to keep the behaviour lazy until it's activated. </p> <p> Once more I want to point out that the purpose of this article series is to explain how a type system like Haskell's guarantees referential transparency. C# could do the same, but it'd require that all impure actions in all libraries in the entire .NET ecosystem were to return <code>IO</code> values. That's not going to happen, but something similar already has happened. Read on in the next article. </p> <p> <strong>Next:</strong> <a href="/2020/07/27/task-asynchronous-programming-as-an-io-surrogate">Task asynchronous programming as an IO surrogate</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8fea4b2ac22d4211bd91d352c7b4e55e"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#8fea4b2ac22d4211bd91d352c7b4e55e">#</a></div> <div class="comment-content"> <p> In your <a href="https://blog.ploeh.dk/2020/07/13/implementation-of-the-c-io-container/">previous post</a>, you said </p> <blockquote> Haskell is a lazily evaluated language. That's an important piece of the puzzle, so I've modelled the <code>IO&lt;T&gt;</code> example so that it only wraps <code>Lazy</code> values. That emulates Haskell's behaviour in C#. </blockquote> <p> After several days, I finally feel like I fully understand this. </p> <p> The concept of lazy has serveral slightly different definitions depending on the context. Haskell is a lazily evaluated language in the sense that its evaluation strategy is <a href="https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_need">call by need</a>. In C#, both <code>Lazy&lt;T&gt;</code> and <code>Func&lt;T&gt;</code> are lazy in the sense that neither actually contains a <code>T</code>, but both could produce a <code>T</code> if asked to do so. The difference is the presence or absence of caching. I remember all this by saying that <code>Lazy&lt;T&gt;</code> is lazy with caching and <code>Func&lt;T&gt;</code> is lazy without caching. So <code>Lazy&lt;T&gt;</code> is to call by need as <code>Func&lt;T&gt;</code> is to <a href="https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name">call by name</a>. </p> <p> Therefore, <code>Lazy&lt;T&gt;</code> is the correct choice if we want to model or emulate the evaluation strategy of Haskell in C#. What about Haskell's <code>IO&lt;T&gt;</code>? Is it lazy with caching or lazy without caching? My guess was lazy without caching, but I finally installed the ghc Haskell compiler and compiled Haskell on my machine for the first time in order to test this. I think this example shows that Haskell's <code>IO&lt;T&gt;</code> is lazy without caching. </p> <p> <pre>-- output: xx main = let io = putStr "x" in do { io ; io }</pre> </p> <p> I think this would be equivalent C# code in this parallel world that you have created. </p> <p> <pre>// output: x static IO&lt;Unit&gt; MainIO(string[] args) { var io = Console.Write("x"); return from _1 in io from _2 in io select Unit.Instance; }</pre> </p> <p> What makes me think that I fully understand this now is that I think I see where you are going. I think you already knew all this and decided to model Haskell's <code>IO&lt;T&gt;</code> using <code>Lazy&lt;T&gt;</code> anyway because <code>Task&lt;T&gt;</code> is also lazy with caching just like <code>Lazy&lt;T&gt;</code>, and your next post will discuss using <code>Task&lt;T&gt;</code> as a surrogate for Haskell's <code>IO&lt;T&gt;</code>. I think you want your C# implementation of <code>IO&lt;T&gt;</code> to be more like C#'s <code>Task&lt;T&gt;</code> than Haskell's <code>IO&lt;T&gt;</code>. </p> <p> Thank you for including such a gem for me to think about...and enough motivation to finally put Haskell on my machine! </p> </div> <div class="comment-date">2020-07-13 20:46 UTC</div> </div> <div class="comment" id="ba8de284a15c49529bb10390c33d2aa1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ba8de284a15c49529bb10390c33d2aa1">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. You've almost turned my blog into a peer-reviewed journal, and you've just pointed out a major blunder of mine 👍 </p> <p> I think I was mislead by the name <code>Lazy</code>, my attention was elsewhere, and I completely forgot about the memoisation that both <code>Lazy&lt;T&gt;</code> and <code>Task&lt;T&gt;</code> employ. It does turn out to be problematic in this context. Take the following example: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;getTime&nbsp;=&nbsp;<span style="color:#2b91af;">Clock</span>.GetLocalTime(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getTime.SelectMany(t1&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(t1.Ticks.ToString()).Select(u1&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Thread</span>.Sleep(2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Unit</span>.Instance; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}).SelectMany(u2&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getTime).SelectMany(t2&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(t2.Ticks.ToString()))); }</pre> </p> <p> Notice that this example <em>reuses</em> <code>getTime</code> twice. We'd like any <code>IO&lt;T&gt;</code> value to represent an impure computation, so evaluating it twice with a 2 millisecond delay in between ought to yield two different results. </p> <p> Due to the memoisation built into <code>Lazy&lt;T&gt;</code>, the first value is reused. That's not the behaviour we'd like to see. </p> <p> While this is a rather big error on my part, it's fortunately only of a technical nature (I think). As you imply, the correct implementation is to use <code>Func&lt;T&gt;</code> rather than <code>Lazy&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(()&nbsp;=&gt;&nbsp;selector(item()).item()); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;UnsafePerformIO() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;item(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This addresses the above reuse bug. With this implementation, the above <code>Main</code> method prints two different values, even though it reuses <code>getTime</code>. </p> <p> Haskell <code>IO</code> doesn't memoise values, so this <code>Func</code>-based implementation better emulates the Haskell behaviour, which is actually what I wanted to do all along. </p> <p> This mistake of mine is potentially so confusing that I think that it's best if I go back and edit the other posts in this articles series. Before I do that, though, I'd like to get your feedback. </p> <p> Does this look better to you, or do you see other problems with it? </p> </div> <div class="comment-date">2020-07-19 14:32 UTC</div> </div> <div class="comment" id="f14bcdf1d46245278ef8694fd6b73d3b"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#f14bcdf1d46245278ef8694fd6b73d3b">#</a></div> <div class="comment-content"> <blockquote> Tyson, thank you for writing. You've almost turned my blog into a peer-reviewed journal, and you've just pointed out a major blunder of mine 👍 </blockquote> <p> You're welcome. I am certainly a peer, and I benefit greatly from closly reviewing your posts. They always give me so much to think about. I am happy that we both benefit from this :) </p> <blockquote> <p> While this is a rather big error on my part, it's fortunately only of a technical nature (I think). ... </p> <p> Haskell <code>IO</code> doesn't memoise values, so this <code>Func</code>-based implementation better emulates the Haskell behaviour, which is actually what I wanted to do all along. </p> <p> This mistake of mine is potentially so confusing that I think that it's best if I go back and edit the other posts in this articles series. Before I do that, though, I'd like to get your feedback. </p> <p> Does this look better to you, or do you see other problems with it? </p> </blockquote> <p> Yes, your <code>Func&lt;T&gt;</code>-based implementation better emulates Haskell's <code>IO&lt;T&gt;</code>. My guess was that you had used <code>Lazy&lt;T&gt;</code> with its caching behavior in mind. I do think it is a minor issue. I can't think of any code that I would write on purpose that would depend on this difference. </p> <p> I think editing the previous posts depends on exactly how you want to suggesst <code>Task&lt;T&gt;</code> as an <code>IO&lt;T&gt;</code> surrogate. </p> <p> From a purely teaching perspective, I think I prefer to first implement Haskell's <code>IO&lt;T&gt;</code> in C# using <code>Func&lt;T&gt;</code>, then suggest this implemention is essentialy the same as <code>Task&lt;T&gt;</code>, then point out the caching difference for those that are still reading. It would be a shame to lose some readers eariler by pointing out the difference too soon. I wouldn't expect you lost any readres in your current presentation that includes the caching difference but without mentioning it. </p> <p> <code>Func&lt;T&gt;</code>, <code>Lazy&lt;T&gt;</code>, and <code>Task&lt;T&gt;</code> are all lazy (in the sense that none contain a <code>T</code> but all could produce one if requested. Here are their differences: <ul> <li><code>Func&lt;T&gt;</code> is synchronous without caching,</li> <li><code>Lazy&lt;T&gt;</code> is synchronous with caching, and</li> <li><code>Task&lt;T&gt;</code> is asynchronous with caching.</li> </ul> We are missing a type that is asynchronous without caching. We can create such behavior though using <code>Func&lt;T&gt;</code> and <code>Task&lt;T&gt;</code>. The nested type <code>Func&lt;Task&lt;T&gt;&gt;</code> can asynchronously produce a <code>T</code> without caching. Maybe this would be helpful in this article series. </p> <p> Overall though, I don't know of any other potential changes to consider. </p> </div> <div class="comment-date">2020-07-19 21:19 UTC</div> </div> <div class="comment" id="689c56123f7b469a9796e0793f48b294"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#689c56123f7b469a9796e0793f48b294">#</a></div> <div class="comment-content"> <p> For any reader following the discussion after today (July 24, 2020), it may be slightly confusing. Based on <a href="#8fea4b2ac22d4211bd91d352c7b4e55e">Tyson Williams' feedback</a>, I've edited the article series with <a href="#ba8de284a15c49529bb10390c33d2aa1">the above implementation</a>. The previous incarnation of the article series had this implementation of <code>IO&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>(<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;res&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;(()&nbsp;=&gt;&nbsp;selector(item.Value)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(()&nbsp;=&gt;&nbsp;res.Value.item.Value)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;UnsafePerformIO() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;item.Value; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As this discussion reveals, the memoisation performed by <code>Lazy&lt;T&gt;</code> causes problems. After thinking it through, I've decided to retroactively change the articles in the series. This is something that I rarely do, but in this case I think is the best way forward, as the <code>Lazy</code>-based implementation could be confusing to new readers. </p> <p> Readers interested in the history of these articles can peruse <a href="https://github.com/ploeh/ploeh.github.com">the Git log</a>. </p> </div> <div class="comment-date">2020-07-24 8:22 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Referential transparency of IO https://blog.ploeh.dk/2020/07/06/referential-transparency-of-io 2020-07-06T05:56:00+00:00 Mark Seemann <div id="post"> <p> <em>How the IO container guarantees referential integrity. An article for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2020/06/08/the-io-container">an article series about the IO container in C#</a>. In <a href="/2020/06/15/io-container-in-a-parallel-c-universe">a previous article</a> you got a basic introduction to the <code>IO&lt;T&gt;</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> I use to explain how a type system like <a href="https://www.haskell.org">Haskell</a>'s distinguishes between <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> and impure actions. </p> <p> The whole point of the <code>IO</code> container is to effectuate the <a href="/2018/11/19/functional-architecture-a-definition">functional interaction law</a>: <em>a pure function mustn't be able to invoke an impure activity.</em> This rule follows from <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>. </p> <p> The practical way to think about it is to consider the two rules of pure functions: <ul> <li>Pure functions must be deterministic</li> <li>Pure functions may have no side effects</li> </ul> In this article, you'll see how <code>IO&lt;T&gt;</code> imposes those rules. </p> <h3 id="3b648ec5b32a42e4b608dbc2e8861a86"> Determinism <a href="#3b648ec5b32a42e4b608dbc2e8861a86" title="permalink">#</a> </h3> <p> Like in the previous articles in this series, you must imagine that you're living in a parallel universe where all impure library functions return <code>IO&lt;T&gt;</code> objects. By elimination, then, methods that return naked values must be pure functions. </p> <p> Consider the <code>Greet</code> function from the <a href="/2020/06/15/io-container-in-a-parallel-c-universe">previous article</a>. Since it returns a plain <code>string</code>, you can infer that it must be a pure function. What prevents it from doing something impure? </p> <p> What if you thought that passing <code>now</code> as an argument is a silly design. Couldn't you just call <code>Clock.GetLocalTime</code> from the method? </p> <p> Well, yes, in fact you can: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Greet(<span style="color:#2b91af;">DateTime</span>&nbsp;now,&nbsp;<span style="color:blue;">string</span>&nbsp;name) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;now1&nbsp;=&nbsp;<span style="color:#2b91af;">Clock</span>.GetLocalTime(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Hello&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsMorning(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;morning&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsAfternoon(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;afternoon&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsEvening(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;evening&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">,&nbsp;</span>{name.Trim()}<span style="color:#a31515;">.&quot;</span>; }</pre> </p> <p> This compiles, but is only the first refactoring step you have in mind. Next, you want to extract the <code>DateTime</code> from <code>now1</code> so that you can get rid of the <code>now</code> parameter. Alas, you now run into an insuperable barrier. <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">How do you get the DateTime out of the IO?</a> </p> <p> You can't. By design. </p> <p> While you can call the <code>GetLocalTime</code> method, you can't <em>use</em> the return value. The only way you can use it is by composing it with <code>SelectMany</code>, but that still accomplishes nothing unless you return the resulting <code>IO</code> object. If you do that, though, you've now turned the entire <code>Greet</code> method into an impure action. </p> <p> You can't perform any non-deterministic behaviour inside a pure function. </p> <h3 id="9f62e432e0c74fda93aff427774c0183"> Side effects <a href="#9f62e432e0c74fda93aff427774c0183" title="permalink">#</a> </h3> <p> How does <code>IO&lt;T&gt;</code> protect against side effects? In the <code>Greet</code> method, couldn't you just write to the console, like the following? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Greet(<span style="color:#2b91af;">DateTime</span>&nbsp;now,&nbsp;<span style="color:blue;">string</span>&nbsp;name) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Side&nbsp;effect!&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Hello&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsMorning(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;morning&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsAfternoon(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;afternoon&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsEvening(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;evening&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">,&nbsp;</span>{name.Trim()}<span style="color:#a31515;">.&quot;</span>; }</pre> </p> <p> This also compiles, despite our best efforts. That's unfortunate, but, as you'll see in a moment, it doesn't violate referential transparency. </p> <p> In Haskell or <a href="https://fsharp.org">F#</a> equivalent code would make the compiler complain. Those compilers don't have special knowledge about <code>IO</code>, but they can see that an action returns a value. F# generates a compiler warning if you ignore a return value. In Haskell the story is a bit different, but the result is the same. Those compilers complain because you try to ignore the return value. </p> <p> You can get around the issue using the language's wildcard pattern. This tells the compiler that you're actively ignoring the result of the action. You can do the same in C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Greet(<span style="color:#2b91af;">DateTime</span>&nbsp;now,&nbsp;<span style="color:blue;">string</span>&nbsp;name) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;_&nbsp;=&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Side&nbsp;effect!&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Hello&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsMorning(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;morning&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsAfternoon(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;afternoon&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsEvening(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;evening&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">,&nbsp;</span>{name.Trim()}<span style="color:#a31515;">.&quot;</span>; }</pre> </p> <p> The situation is now similar to the above treatment of non-determinism. While there's no value of interest in an <code>IO&lt;Unit&gt;</code>, the fact that there's an object at all is a hint. Like <code>Lazy&lt;T&gt;</code>, that value isn't a result. It's a placeholder for a computation. </p> <p> If there's a way to make the C# compiler complain about ignored return values, I'm not aware of it, so I don't know if we can get closer to how Haskell works than this. Regardless, keep in mind that I'm not trying to encourage you to write C# like this; I'm only trying to explain how Haskell enforces referential transparency at the type level. </p> <h3 id="4f40c1e1a7a142b693d050dcab466d90"> Referential transparency <a href="#4f40c1e1a7a142b693d050dcab466d90" title="permalink">#</a> </h3> <p> While the above examples compile without warnings in C#, both are still referentially transparent! </p> <p> This may surprise you. Particularly the second example that includes <code>Console.WriteLine</code> looks like it has a side effect, and thus violates referential transparency. </p> <p> Keep in mind, though, what referential transparency means. It means that you can replace a particular function call with its value. For example, you should be able to replace the function call <code>Greeter.Greet(new DateTime(2020, 6, 4, 12, 10, 0), "Nearth")</code> with its return value <code>"Hello, Nearth."</code>, or the function call <code>Greeter.Greet(new DateTime(2020, 6, 4, 7, 30, 0), "Bru")</code> with <code>"Good morning, Bru."</code>, without changing the behaviour of the software. This property still holds. </p> <p> Even when the <code>Greet</code> method includes the <code>Console.WriteLine</code> call, that side effect never happens. </p> <p> The reason is that an <code>IO</code> object represents a potential computation that may take place (also known as a <em>thunk</em>). Notice that the <code>IO&lt;T&gt;</code> constructor takes a <code>Func</code> as argument. It's basically just a lazily evaluated function wrapped in such a way that you can't force evaluation. </p> <p> Instead, you should imagine that <em>after</em> <code>Main</code> returns its <code>IO&lt;Unit&gt;</code> object, the parallel-universe .NET framework executes it. </p> <p> <img src="/content/binary/parallel-universe-fx-calling-unsafeperformio.png" alt="Diagram showing that the parrallel-universe framework executes the IO value after Main returns."> </p> <p> The framework supplies command-line arguments to the <code>Main</code> method. Once the method returns an <code>IO&lt;Unit&gt;</code> object, the framework executes it with a special method that only the framework can invoke. Any other <code>IO</code> values that may have been created (e.g. the above <code>Console.WriteLine</code>) never gets executed, because they're not included in the return value. </p> <h3 id="d5fe0e97d9ea4442b3b4fb13ad28aab6"> Conclusion <a href="#d5fe0e97d9ea4442b3b4fb13ad28aab6" title="permalink">#</a> </h3> <p> The <code>IO</code> container makes sure that pure functions maintain referential transparency. The underlying assumption that makes all of this work is that <em>all</em> impure actions return <code>IO</code> objects. That's not how the .NET framework works, but that's how Haskell works. Since the <code>IO</code> container is opaque, pure functions can't see the contents of <code>IO</code> boxes. </p> <p> Program <a href="https://en.wikipedia.org/wiki/Entry_point">entry points</a> are all impure. The return value of the entry point must be <code>IO&lt;Unit&gt;</code>. The hypothetical parallel-universe framework executes the <code>IO</code> value returned by <code>Main</code>. </p> <p> Haskell is a lazily evaluated language. That's an important piece of the puzzle, so I've modelled the <code>IO&lt;T&gt;</code> example so that it only wraps lazily evaluated <code>Func</code> values. That emulates Haskell's behaviour in C#. In the next article, you'll see how I wrote the C# code that supports these articles. </p> <p> <strong>Next:</strong> <a href="/2020/07/13/implementation-of-the-c-io-container">Implementation of the C# IO container</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="52e8a987dc234e839d259cc3296176d7"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#52e8a987dc234e839d259cc3296176d7">#</a></div> <div class="comment-content"> <p> In a <a href="https://blog.ploeh.dk/2020/02/24/discerning-and-maintaining-purity/">previous post</a>, you said </p> <blockquote> ...a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> has to obey two rules: <ul> <li>The same input always produces the same output.</li> <li>Calling it causes no side effects.</li> </ul> </blockquote> <p> In this post, you said </p> <blockquote>...the two rules of pure functions: <ul> <li>Pure functions must be deterministic</li> <li>Pure functions may have no side effects</li> </ul> </blockquote> <p> The first two items are not the same. Is this difference intentional? </p> </div> <div class="comment-date">2020-07-06 11:07 UTC</div> </div> <div class="comment" id="73bfc62f4f464ac2b5a6131e95884c11"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#73bfc62f4f464ac2b5a6131e95884c11">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Indeed, the words er verbatim not the same, but I do intend them to carry the same meaning. </p> <p> If one wants to define purity in a way that leaves little ambiguity, one has to use more words. Just look at the linked Wikipedia article. I link to Wikipedia for the benefit of those readers who'd like to get a more rigorous definition of the term, while at the same time I enumerate the two rules as a summary for the benefit of those readers who just need a reminder. </p> <p> Does that answer your question? </p> </div> <div class="comment-date">2020-07-06 11:26 UTC</div> </div> <div class="comment" id="1338dd35c4c6405f861ffa26034b13e6"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#1338dd35c4c6405f861ffa26034b13e6">#</a></div> <div class="comment-content"> <blockquote> ...I do intend them to carry the same meaning. </blockquote> <p> I don't think they don't mean the same thing. That is part of the discussion we are having on <a href="https://blog.ploeh.dk/2020/02/24/discerning-and-maintaining-purity/">this previous post</a>. I think the simplest example to see the difference is randomzied quicksort. For each input, the output is always the same. However, randomized quicksort is not deterministic because it uses randomness. </p> <p> Do you still think they mean the same thing? </p> </div> <div class="comment-date">2020-07-06 19:35 UTC</div> </div> <div class="comment" id="247a2c05d83a485a99225057829afa71"> <div class="comment-author"><a href="http://www.raboof.com/">Atif Aziz</a> <a href="#247a2c05d83a485a99225057829afa71">#</a></div> <div class="comment-content"> <blockquote> You can get around the issue using the language's wildcard pattern. This tells the compiler that you're actively ignoring the result of the action. </blockquote> <p> You made a standard variable declaration and I believe you meant to use a <a href="https://docs.microsoft.com/en-us/dotnet/csharp/discards#a-standalone-discard">stand-alone discard</a> rather than a wildcard pattern? Like below? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Greet(<span style="color:#2b91af;">DateTime</span>&nbsp;now,&nbsp;<span style="color:blue;">string</span>&nbsp;name) { &nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;=&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Side&nbsp;effect!&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Hello&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsMorning(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;morning&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsAfternoon(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;afternoon&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsEvening(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;evening&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">,&nbsp;</span>{name.Trim()}<span style="color:#a31515;">.&quot;</span>; }</pre> </p> </div> <div class="comment-date">2020-07-09 08:18 UTC</div> </div> <div class="comment" id="dcd18dda3cf742ffb0bba38ce0326b66"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dcd18dda3cf742ffb0bba38ce0326b66">#</a></div> <div class="comment-content"> <p> Tyson, I apologise in advance for my use of <a href="https://en.wikipedia.org/wiki/Weasel_word">weasel words</a>, but 'in the literature', a function that always returns the same output when given the same input is called 'deterministic'. I can't give you a comprehensive list of 'the literature' right now, but <a href="https://en.wikipedia.org/wiki/Deterministic_algorithm">here's at least one example</a>. </p> <p> I'm well aware that this might be confusing. One could argue that querying a database is deterministic, because the output is completely determined by the state of the database. The same goes for reading the contents of a file. Such operations, however, may return different outputs for the same inputs, as the state of the resource changes. There's no stochastic process involved in such state changes, but we still consider such actions non-deterministic. </p> <p> In the same vein, in this jargon, 'deterministic' doesn't imply the absence of internal randomness, as <a href="/2020/02/24/discerning-and-maintaining-purity#a06da27b57944c89aefc16914234939c">you have so convincingly argued</a>. In the present context, 'deterministic' is <em>defined</em> as the property that a given input value always produces the same output value. </p> <p> That's the reason I tend to use those phrases interchangeably. In this context, they literally mean the same. I can see how this might be confusing, though. </p> </div> <div class="comment-date">2020-07-09 10:57 UTC</div> </div> <div class="comment" id="d0dc32b4548a4d4ab5d55652a7b42502"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d0dc32b4548a4d4ab5d55652a7b42502">#</a></div> <div class="comment-content"> <p> Atif, thank you for writing. I didn't know about that language construct. Thank you for pointing it out to me! </p> </div> <div class="comment-date">2020-07-09 18:11 UTC</div> </div> <div class="comment" id="956af671404548038caa603fede12e88"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#956af671404548038caa603fede12e88">#</a></div> <div class="comment-content"> <blockquote> <p> [<a href="https://en.wikipedia.org/wiki/Deterministic_algorithm">Wikipedia</a> says that] a function that always returns the same output when given the same input is called 'deterministic'. </p> <p> In the same vein, in this jargon, 'deterministic' doesn't imply the absence of internal randomness, as <a href="/2020/02/24/discerning-and-maintaining-purity#a06da27b57944c89aefc16914234939c">you have so convincingly argued</a>. In the present context, 'deterministic' is <em>defined</em> as the property that a given input value always produces the same output value. </p> <p> That's the reason I tend to use those phrases interchangeably. In this context, they literally mean the same. I can see how this might be confusing, though. </p> </blockquote> <p> I am certainly still confused.  I can't tell if you are purposefully providing your own definition for determinism, if you are accidentally misunderstanding the actual definition of determinism, if you think the definition of determinism is equivalent to the definition you gave due to being in some context or operating with an additional assumption (of which I am unsure), or maybe something else. </p> <p> If I had to guess, then I think you do see the difference between the two definitions but are claiming that are equivalent due to being in some context or operating with an additional assumption.  If this guess is correct, then what is the additional assumption you are making? </p> <p> To ensure that you do understand determinism, I will interpret your statements as literally as possible and then respond to them.  I apologize in advance if this is not the correct interpretation and for any harshness in the tone of my response. </p> <p> You misquoted the Wikipedia article.  Here is the exact text (after replacing "algorithm" with "function" since the difference between these is not important to us now). </p> <blockquote> a deterministic function is a function which, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states. </blockquote> <p> Your quotation essentially excludes the phrase "with the underlying machine always passing through the same sequence of states".  Let's put these two definitions next to each other. </p> <p> Let <code>f</code> be a function.  Consider the following two statements about <code>f</code> that might or might not be true (depending on the exact value of <code>f</code>). <ol> <li>Given a particular input, <code>f</code> will always produce the same output.</li> <li>Given a particular input, <code>f</code> will always produce the same output, with the underlying machine always passing through the same sequence of states.</li> </ol> </p> <p> Suppose some <code>f</code> satisfies statement 2.  Then <code>f</code> clearly satisfies statement 1 as well.  These two sentences together are equivalent to saying that statement 2 implies statement 1.  The <a href="https://en.wikipedia.org/wiki/Contraposition">contrapositive</a> then says that not satisfying statement 1 implies not satisfying statement 2.   Also, Wikipedia says that such an <code>f</code> is said to be deterministic. </p> <p> Now suppose some <code>f</code> satisfies statement 1.  Then <code>f</code> need not also satisfy statement 2.  An example that proves this is randomized quicksort (as I pointed out in <a href="https://blog.ploeh.dk/2020/02/24/discerning-and-maintaining-purity/#a06da27b57944c89aefc16914234939c">this comment</a>).  This means that statement 1 does not imply statement 2. </p> <p> "Wikipedia gave" a name to functions that satisfy statement 2.  I do not recall ever seeing anyone give a name to functions that satisfy statement 1.  In <a href="https://blog.ploeh.dk/2020/02/24/discerning-and-maintaining-purity/#9f9664e29c3f4fafa786abae039eb20e">this comment</a>, you asked me about what I meant by "weak determinism".  I am trying to give a name to functions that satisfy statement 1.  I am suggesting that we call them weakly deterministic.  This name allows us to say that a deterministic function is also weakly deterministic, but a weakly deterministic function might not be deterministic.  Furthermore, not being a weakly deterministic implies not being deterministic. </p> <blockquote> One could argue that querying a database is deterministic, because the output is completely determined by the state of the database. The same goes for reading the contents of a file. Such operations, however, may return different outputs for the same inputs, as the state of the resource changes. There's no stochastic process involved in such state changes, but we still consider such actions non-deterministic. </blockquote> <p> Indeed.  I agree.  If we distinguish determinism from weak determinism, then we can say that such a function is not weakly deterministic, which implies that it is not deterministic. </p> </div> <div class="comment-date">2020-07-10 01:36 UTC</div> </div> <div class="comment" id="c53873f100b2480296bfc0ea0709e97d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c53873f100b2480296bfc0ea0709e97d">#</a></div> <div class="comment-content"> <p> Tyson, I'm sorry, I picked a bad example. It's possible that my brain is playing a trick with me. I'm not <em>purposefully</em> providing my own definition of determinism. </p> <p> I've learned all of this from diverse sources, and I don't recall all of them. Some of them are books, some were conference talks I attended, some may have been conversations, and some online resources. All of that becomes an amalgam of knowledge. <em>Somewhere</em> I've picked up the shorthand that 'deterministic' is the same as 'the same input produces the same output'; I can't tell you the exact source of that habit, and it may, indeed, be confusing. </p> <p> It seems that I'm not the only person with that habit, though: <blockquote> <p> "Deterministic: They return the same output for the same input." </p> <footer><cite><a href="https://twitter.com/jdegoes/status/936301872066977792">John A De Goes</a></cite></footer> </blockquote> I'm not saying that John A De Goes is an authority you should unquestionably accept; I'm only pointing to that tweet to illustrate that I'm not the only person who occasionally use 'deterministic' in that way. And I don't think that John A De Goes picked up the habit from me. </p> <p> The key to all of this is referential transparency. In an ideal world, I wouldn't need to use the terms 'pure function' or 'deterministic'. I can't, however, write these articles and only refer to referential transparency. The articles are my attempt to share what I have learned with readers who would also like to learn. The problem with referential transparency is that it's an abstract concept: <em>can I replace a function call with its output?</em> This may be a tractable notion to pick up, but how do you evaluate that in practice? </p> <p> I believe that it's easier for readers to learn that they have to look for two properties: <ul> <li>Does the same input always produce the same output?</li> <li>Are there no side effects?</li> </ul> As far as I can tell, these two properties are enough to guarantee referential transparency. Again: this isn't a claim that <em>I'm</em> making; it's just my understanding based on what I've picked up so far. </p> <p> As all programmers know: language is imprecise. Even the above two bullets are vague. What's a side effect? Does the rule about input and output apply to all values in the function's domain? </p> <p> I don't think that it's possible to write perfectly precise and unambiguous prose. Mathematical notation was developed as an attempt to be more precise and unambiguous. Source code has the same quality. </p> <p> I'm not writing mathematical treatises, but I use C#, F#, and Haskell source code to demonstrate concepts as precisely as I can. The surrounding prose is my attempt at explaining what the code does, and why it's written the way it is. The prose will be ambiguous; I can't help it. </p> <p> Sometimes, I need a shorthand to remind the reader about referential transparency in a way that (hopefully) assists him or her. Sometimes, this is best done by using a few adjectives, such as <em>"a deterministic and side-effect-free function"</em>. It's not a definition, it's a reading aid. </p> </div> <div class="comment-date">2020-07-25 8:38 UTC</div> </div> <div class="comment" id="fa3e2af2ea6b498cb8b94baf594d1e6f"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#fa3e2af2ea6b498cb8b94baf594d1e6f">#</a></div> <div class="comment-content"> <p> Mark, I am also sorry. As I reread my comment, I think I was too critical. I admire your great compassion and humility, especially in your writing. I have been trying to write more like you, but my previous comment shows that I still have room for improvement. Your blog is still my favorite place to read and learn about software development and functional programming. It is almost scary how much I agree with you. I hope posting questions about my confusion or comments about our differences don't overshadow all the ways in which we agree and sour our relationship. </p> </div> <div class="comment-date">2020-07-25 15:15 UTC</div> </div> <div class="comment" id="dcd28380339f4db8955efd235ef125b6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dcd28380339f4db8955efd235ef125b6">#</a></div> <div class="comment-content"> <p> Tyson, don't worry about it. Questions teach me where there's room for improvement. You also sometimes <a href="/2020/07/13/implementation-of-the-c-io-container#8fea4b2ac22d4211bd91d352c7b4e55e">point out genuine mistakes that I make</a>. If you didn't do that, I might never realise my mistakes, and then I wouldn't be able to correct them. </p> <p> I appreciate the feedback because it improves the content and teaches me things that I didn't know. On the other hand, as the cliché goes, <em>all errors are my own.</em> </p> </div> <div class="comment-date">2020-07-29 9:56 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Syntactic sugar for IO https://blog.ploeh.dk/2020/06/29/syntactic-sugar-for-io 2020-06-29T05:49:00+00:00 Mark Seemann <div id="post"> <p> <em>How to make use of the C# IO container less ugly.</em> </p> <p> This article is part of <a href="/2020/06/08/the-io-container">an article series about the IO container in C#</a>. In <a href="/2020/06/15/io-container-in-a-parallel-c-universe">the previous article</a> you saw a basic C# <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">hello world</a> program using <code>IO&lt;T&gt;</code> to explicitly distinguish between <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> and impure actions. The code wasn't as pretty as you could hope for. In this article, you'll see how to improve the aesthetics a bit. </p> <p> The code isn't going to be perfect, but I think it'll be better. </p> <h3 id="c919d892a9374cd4a816693c62840620"> Sugared version <a href="#c919d892a9374cd4a816693c62840620" title="permalink">#</a> </h3> <p> The <code>IO&lt;T&gt;</code> container is an imitation of the <a href="https://www.haskell.org">Haskell</a> <code>IO</code> type. In Haskell, <code>IO</code> is a monad. This isn't a monad tutorial, and I hope that you're able to read the article without a deep understanding of monads. I only mention this because when you compose monadic values with each other, you'll sometimes have to write some 'noisy' code - even in Haskell. To alleviate that pain, Haskell offers syntactic sugar in the form of so-called <code>do</code> notation. </p> <p> Likewise, <a href="https://fsharp.org">F#</a> comes with <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a>, which also gives you syntactic sugar over monads. </p> <p> C#, too, comes with syntactic sugar over <a href="/2022/03/28/monads">monads</a>. This is <em>query syntax</em>, but it's not as powerful as Haskell <code>do</code> notation or F# computation expressions. It's powerful enough, though, to enable you to improve the <code>Main</code> method from the previous article: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">from</span>&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;What&#39;s&nbsp;your&nbsp;name?&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;name&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Console</span>.ReadLine() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;&nbsp;now&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Clock</span>.GetLocalTime() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;greeting&nbsp;=&nbsp;<span style="color:#2b91af;">Greeter</span>.Greet(now,&nbsp;name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;&nbsp;res&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(greeting) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;res; }</pre> </p> <p> If you use C# query syntax at all, you may think of it as exclusively the realm of object-relational mapping, but in fact it works for any monad. There's no data access going on here - just the interleaving of pure and impure code (in an <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>, even). </p> <h3 id="326b00adfdd347e684f5adfc90b001e6"> Infrastructure <a href="#326b00adfdd347e684f5adfc90b001e6" title="permalink">#</a> </h3> <p> For the above code to compile, you must add a pair of methods to the <code>IO&lt;T&gt;</code> API. You can write them as extension methods if you like, but here I've written them as instance methods on <code>IO&lt;T&gt;</code>. </p> <p> When you have multiple <code>from</code> keywords in the same query expression, you must supply a particular overload of <code>SelectMany</code>. This is an oddity of the implementation of the query syntax language feature in C#. You don't have to do anything similar to that in F# or Haskell. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">U</span>&gt;&gt;&nbsp;k,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;s) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;SelectMany(x&nbsp;=&gt;&nbsp;k(x).SelectMany(y&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(()&nbsp;=&gt;&nbsp;s(x,&nbsp;y)))); }</pre> </p> <p> Once you've implemented such overloads a couple of times, they're more tedious than challenging to write. They always follow the same template. First use <code>SelectMany</code> with <code>k</code>, and then <code>SelectMany</code> again with <code>s</code>. The only marginally stimulating part of the implementation is figuring out how to wrap the return value from <code>s</code>. </p> <p> You're also going to need <code>Select</code> as shown in the article about <a href="/2020/06/22/the-io-functor">IO as a functor</a>. </p> <h3 id="589ab47b68244e21bafd2f16c4422800"> Conclusion <a href="#589ab47b68244e21bafd2f16c4422800" title="permalink">#</a> </h3> <p> C#'s query syntax offers limited syntactic sugar over <a href="/2018/03/22/functors">functors</a> and monads. Compared with F# and Haskell, the syntax is odd and its functionality limited. The most galling lacuna is that you can't branch (e.g. use <code>if</code> or <code>switch</code>) inside query expressions. </p> <p> The point of these articles is (still) not to endorse this style of programming. While the code I show in this article series is working C# code that runs and passes its tests, I'm pretending that all impure actions in C# return <code>IO</code> results. To be clear, the <code>Console</code> class this code interacts with isn't <a href="https://docs.microsoft.com/dotnet/api/system.console">the Console class from the base class library</a>. It's a class that pretends to be such a class from a parallel universe. </p> <p> So far in these articles, you've seen how to compose impure actions with pure functions. What I haven't covered yet is the motivation for it all. We want the compiler to enforce the <a href="/2018/11/19/functional-architecture-a-definition">functional interaction law</a>: a pure function shouldn't be able to invoke an impure action. That's the topic for the next article. </p> <p> <strong>Next:</strong> <a href="/2020/07/06/referential-transparency-of-io">Referential transparency of IO</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The IO functor https://blog.ploeh.dk/2020/06/22/the-io-functor 2020-06-22T06:23:00+00:00 Mark Seemann <div id="post"> <p> <em>The IO container forms a functor. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. Previous articles have covered <a href="/2018/03/26/the-maybe-functor">Maybe</a>, <a href="/2018/09/10/the-lazy-functor">Lazy</a>, and other functors. This article provides another example. </p> <h3 id="cde6cc0f422941c6a89cc7717f3b62b5"> Functor <a href="#cde6cc0f422941c6a89cc7717f3b62b5" title="permalink">#</a> </h3> <p> In a recent article, I gave an example of <a href="/2020/06/15/io-container-in-a-parallel-c-universe">what <em>IO</em> might look like in C#</a>. The <code>IO&lt;T&gt;</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> already has sufficient API to make it a functor. All it needs is a <code>Select</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;SelectMany(x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(()&nbsp;=&gt;&nbsp;selector(x))); }</pre> </p> <p> This is an instance method on <code>IO&lt;T&gt;</code>, but you can also write it as an extension method, if that's more to your liking. </p> <p> When you call <code>selector(x)</code>, the return value is an object of the type <code>TResult</code>. The <code>SelectMany</code> method, however, wants you to return an object of the type <code>IO&lt;TResult&gt;</code>, so you use the <code>IO</code> constructor to wrap that return value. </p> <h3 id="0f17690c09b34160a7cc843a461d836d"> Haskell <a href="#0f17690c09b34160a7cc843a461d836d" title="permalink">#</a> </h3> <p> The C# <code>IO&lt;T&gt;</code> container is an illustration of how <a href="https://www.haskell.org">Haskell</a>'s <code>IO</code> type works. It should come as no surprise to Haskellers that <code>IO</code> is a functor. In fact, it's a monad, and all monads are also functors. </p> <p> The C# <code>IO&lt;T&gt;</code> API is based around a constructor and the <code>SelectMany</code> method. The constructor wraps a plain <code>T</code> value in <code>IO&lt;T&gt;</code>, so that corresponds to Haskell's <code>return</code> method. The <code>SelectMany</code> method corresponds to Haskell's monadic <em>bind</em> operator <code>&gt;&gt;=</code>. When you have lawful <code>return</code> and <code>&gt;&gt;=</code> implementations, you can have a <code>Monad</code> instance. When you have a <code>Monad</code> instance, you not only <em>can</em> have <code>Functor</code> and <code>Applicative</code> instances, you <em>must</em> have them. </p> <h3 id="2c8de38e7bd04656855c405529c71908"> Conclusion <a href="#2c8de38e7bd04656855c405529c71908" title="permalink">#</a> </h3> <p> IO forms a functor, among other abstractions. In C#, this manifests as a proper implementation of a <code>Select</code> method. </p> <p> <strong>Next:</strong> <a href="/2020/10/19/monomorphic-functors">Monomorphic functors</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="a571ffe129e44ddbb2ecec0bf4d108ac"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#a571ffe129e44ddbb2ecec0bf4d108ac">#</a></div> <div class="comment-content"> <blockquote> The constructor wraps a plain <code>T</code> value in <code>IO&lt;T&gt;</code> </blockquote> <p> Did you mean to say that the constructor wraps a <code>Lazy&lt;T&gt;</code> value in <code>IO&lt;T&gt;</code>? </p> </div> <div class="comment-date">2020-06-22 14:05 UTC</div> </div> <div class="comment" id="6c7f026e825942429254efc19440b2e2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6c7f026e825942429254efc19440b2e2">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Well, yes, that's technically what happens... I'm deliberately being imprecise with the language because I'm trying to draw a parallel to Haskell. In Haskell, <code>return</code> takes a value and wraps it in <code>IO</code> (the type is effectively <code>a -&gt; IO a</code>). In Haskell, however, computation is lazy by default. This means that the value you wrap in <code>IO</code> is already lazy. This turns out to be important, as I'll explain in a future article, so in C# we have to first make sure that the value is lazy. </p> <p> The concept, however, involves taking a 'bare' value and wrapping it in a container, and that's the reason I chose my words as I did. </p> </div> <div class="comment-date">2020-06-22 14:45 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. IO container in a parallel C# universe https://blog.ploeh.dk/2020/06/15/io-container-in-a-parallel-c-universe 2020-06-15T05:55:00+00:00 Mark Seemann <div id="post"> <p> <em>A C# model of IO at the type level.</em> </p> <p> This article is part of <a href="/2020/06/08/the-io-container">an article series about the IO container in C#</a>. The previous article provided a conceptual overview. In this article you'll see C# code examples. </p> <h3 id="03b3ac7f1b2542b583e7f122f1c8da7d"> In a world... <a href="#03b3ac7f1b2542b583e7f122f1c8da7d" title="permalink">#</a> </h3> <p> Imagine a parallel universe where a C# <a href="https://en.wikipedia.org/wiki/Entry_point">entry point</a> is supposed to look like this: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args)</pre> </p> <p> Like another Neo, you notice that something in your reality is odd, so you decide to follow the white rabbit. <code>Unit</code>? What's that? Navigating to its definition, you see this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Unit</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Unit</span>&nbsp;Instance; }</pre> </p> <p> There's not much to see here. <code>Unit</code> is a type that serves the same role as the <code>void</code> keyword. <a href="/2018/01/15/unit-isomorphisms">They're isomorphic</a>, but <code>Unit</code> has the advantage that it's a proper type. This means that it can be a type parameter in a generically typed container like <code>IO</code>. </p> <p> So what's <code>IO</code>? When you view its definition, this is what you see: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector) }</pre> </p> <p> There's a constructor you can initialise with a lazily evaluated value, and a <code>SelectMany</code> method that looks strikingly like something out of LINQ. </p> <p> You'll probably notice right away that while you can put a value into the <code>IO</code> container, you can't get it back. As the introductory article explained, this is by design. Still, you may think: <em>What's the point?</em> Why would I ever want to use this class? </p> <p> You get part of the answer when you try to implement your program's entry point. In order for it to compile, the <code>Main</code> method must return an <code>IO&lt;Unit&gt;</code> object. Thus, the simplest <code>Main</code> method that compiles is this: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;(()&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Unit</span>.Instance); };</pre> </p> <p> That's only a roundabout <a href="https://en.wikipedia.org/wiki/NOP_(code)">no-op</a>. What if you want write <em>real</em> code? Like <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">hello world</a>? </p> <h3 id="01dbc48c2136426680432d9b290ed281"> Impure actions <a href="#01dbc48c2136426680432d9b290ed281" title="permalink">#</a> </h3> <p> You'd like to write a program that asks the user about his or her name. Based on the answer, and the time of day, it'll write <em>Hello, Nearth,</em> or <em>Good evening, Kate.</em> You'd like to know how to take user input and write to the standard output stream. In this parallel world, the <code>Console</code> API looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Console</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;ReadLine(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;WriteLine(<span style="color:blue;">string</span>&nbsp;value); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members&nbsp;here...</span> }</pre> </p> <p> You notice that both methods return <code>IO</code> values. This immediately tells you that they're impure. This is hardly surprising, since <code>ReadLine</code> is non-deterministic and <code>WriteLine</code> has a side effect. </p> <p> You'll also need the current time of day. How do you get that? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Clock</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;GetLocalTime(); }</pre> </p> <p> Again, <code>IO</code> signifies that the returned <code>DateTime</code> value is impure; it's non-deterministic. </p> <h3 id="7c7d0747805541fbb5c31f4257f5ab6e"> Pure logic <a href="#7c7d0747805541fbb5c31f4257f5ab6e" title="permalink">#</a> </h3> <p> A major benefit of functional programming is the natural <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">separation of concerns</a>; separation of business logic from implementation details (a.k.a. the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>). </p> <p> Write the logic of the program as a pure function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Greet(<span style="color:#2b91af;">DateTime</span>&nbsp;now,&nbsp;<span style="color:blue;">string</span>&nbsp;name) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Hello&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsMorning(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;morning&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsAfternoon(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;afternoon&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsEvening(now)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;greeting&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Good&nbsp;evening&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">$&quot;</span>{greeting}<span style="color:#a31515;">,&nbsp;</span>{name.Trim()}<span style="color:#a31515;">.&quot;</span>; }</pre> </p> <p> You can tell that this is a pure function from its return type. In this parallel universe, <em>all</em> impure library methods look like the above <code>Console</code> and <code>Clock</code> methods. Thus, by elimination, a method that <em>doesn't</em> return <code>IO</code> is pure. </p> <h3 id="a16d564dc28149eaa462813173276fbd"> Composition <a href="#a16d564dc28149eaa462813173276fbd" title="permalink">#</a> </h3> <p> You have impure actions you can invoke, and a pure piece of logic. You can use <code>ReadLine</code> to get the user's name, and <code>GetLocalTime</code> to get the local time. When you have those two pieces of data, you can call the <code>Greet</code> method. </p> <p> This is where most people run aground. <em>"Yes, but Greet needs a string, but I have an IO&lt;string&gt;. <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">How do I get the string out of the IO?</a></em> </p> <p> If you've been following the plot so far, you know the answer: <a href="https://en.wikipedia.org/wiki/Mu_(negative)">mu</a>. You don't. You compose all the things with <code>SelectMany</code>: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IO</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;What&#39;s&nbsp;your&nbsp;name?&quot;</span>).SelectMany(_&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.ReadLine().SelectMany(name&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Clock</span>.GetLocalTime().SelectMany(now&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;greeting&nbsp;=&nbsp;<span style="color:#2b91af;">Greeter</span>.Greet(now,&nbsp;name); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(greeting); &nbsp;&nbsp;&nbsp;&nbsp;}))); }</pre> </p> <p> I'm not going to walk through all the details of how this works. There's <a href="https://cleancoders.com/videos/humane-code-real">plenty of monad tutorials out there</a>, but take a moment to contemplate the <code>SelectMany</code> method's <code>selector</code> argument. It takes a <em>plain</em> <code>T</code> value as input, but must return an <code>IO&lt;TResult&gt;</code> object. That's what each of the above lambda expressions do, but that means that <code>name</code> and <code>now</code> are <em>unwrapped values;</em> i.e. <code>string</code> and <code>DateTime</code>. </p> <p> That means that you can call <code>Greet</code> with <code>name</code> and <code>now</code>, which is exactly what happens. </p> <p> Notice that the above lambda expressions are nested. With <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> formatting, they'd exhibit the dreaded <a href="http://wiki.c2.com/?ArrowAntiPattern">arrow shape</a>, but with this formatting, it looks more like the sequential composition that it actually is. </p> <h3 id="03f739d929874049be990b3c90c2d7eb"> Conclusion <a href="#03f739d929874049be990b3c90c2d7eb" title="permalink">#</a> </h3> <p> The code you've seen here all works. The only difference between this hypothetical C# and the real C# is that your <code>Main</code> method can't look like that, and impure library methods don't return <code>IO</code> values. </p> <p> The point of the article isn't to recommend this style of programming. You can't, really, since it relies on the counter-factual assumption that all impure library methods return <code>IO</code>. The point of the article is to explain how a language like <a href="https://www.haskell.org">Haskell</a> uses the type system to distinguish between pure functions and impure actions. </p> <p> Perhaps the code in that <code>Main</code> method isn't the prettiest code you've ever seen, but we can make it a little nicer. That's the topic of the next article in the series. </p> <p> <strong>Next:</strong> <a href="/2020/06/29/syntactic-sugar-for-io">Syntactic sugar for IO</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c0bb2d1ce63d41adba638e489f926ee1"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#c0bb2d1ce63d41adba638e489f926ee1">#</a></div> <div class="comment-content"> <p> Why is the input of the IO constructor of type <code>Lazy&lt;T&gt;</code> instead of just type <code>T</code>? </p> </div> <div class="comment-date">2020-06-22 12:16 UTC</div> </div> <div class="comment" id="8ff4e7987f3e46f4bffe328ec2706679"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8ff4e7987f3e46f4bffe328ec2706679">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. A future article in this article series will answer that question 😀 </p> </div> <div class="comment-date">2020-06-22 12:25 UTC</div> </div> <div class="comment" id="43ca1bf8d83a4124a4a9ccd33343e864"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#43ca1bf8d83a4124a4a9ccd33343e864">#</a></div> <div class="comment-content"> <p> FWIW, the promised article is <a href="/2020/07/06/referential-transparency-of-io">now available</a>. </p> </div> <div class="comment-date">2020-07-06 6:01 UTC</div> </div> <div class="comment" id="5870eefe47424f4f925660991e257fac"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#5870eefe47424f4f925660991e257fac">#</a></div> <div class="comment-content"> <p> Ah, sure. I was thinking that <code>IO&lt;T&gt;</code> is a monad (in <code>T</code>), so there should be a constructor with argument <code>T</code>. However, the function doens't need to be a constructor. The lambda expression <code>t =&gt; new IO&lt;T&gt;(new Lazy&lt;T&gt;(() =&gt; t))</code> satisifies the requirement. </p> </div> <div class="comment-date">2020-07-13 19:35 UTC</div> </div> <div class="comment" id="3178b75249e749a59fd6e08ee7738daa"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3178b75249e749a59fd6e08ee7738daa">#</a></div> <div class="comment-content"> <p> Yes 👍 </p> </div> <div class="comment-date">2020-07-14 6:19 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The IO Container https://blog.ploeh.dk/2020/06/08/the-io-container 2020-06-08T05:53:00+00:00 Mark Seemann <div id="post"> <p> <em>How a type system can distinguish between pure and impure code.</em> </p> <p> <a href="https://en.wikipedia.org/wiki/Referential_transparency">Referential transparency</a> is the foundation of <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a>. If you categorise all operations into <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> and impure actions, then most other traits associated with functional programming follow. </p> <p> Unfortunately, <a href="/2020/02/24/discerning-and-maintaining-purity">mainstream programming languages don't distinguish between pure functions and impure actions</a>. Identifying pure functions is tricky, and the knowledge is fleeting. What was a pure function today may become impure next time someone changes the code. </p> <p> Separating pure and impure code is important. It'd be nice if you could automate the process. Perhaps you could run some tests, or, <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">even better</a>, make the compiler do the work. That's what <a href="https://www.haskell.org">Haskell</a> and a few other languages do. </p> <p> In Haskell, the distinction is made with a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> called <code>IO</code>. This static type enforces the functional interaction law at compile time: pure functions can't invoke impure actions. </p> <h3 id="5bb03bce8e314e7696aefa1800f94637"> Opaque container <a href="#5bb03bce8e314e7696aefa1800f94637" title="permalink">#</a> </h3> <p> Regular readers of this blog know that I often use Haskell to demonstrate principles of functional programming. If you don't know Haskell, however, its ability to guarantee the functional interaction law at compile time may seem magical. It's not. </p> <p> Fortunately, the design is so simple that it's easy to explain the fundamental concept: Results of impure actions are always enclosed in an opaque container called <em>IO</em>. You can think of it as a box with a label. </p> <p> <img src="/content/binary/io-box-with-int-inside.png" alt="A cardboard box with the label 'int inside' on the side."> </p> <p> The label only tells you about the static type of the value inside the box. It could be an <code>int</code>, a <code>DateTime</code>, or your own custom type, say <code>Reservation</code>. While you know what type of value is inside the box, you can't see what's in it, and you can't open it. </p> <h3 id="803d5847d88b4b51bebcecddb2581f3a"> Name <a href="#803d5847d88b4b51bebcecddb2581f3a" title="permalink">#</a> </h3> <p> The container itself is called <code>IO</code>, but don't take the word too literally. While all <a href="https://en.wikipedia.org/wiki/Input/output">I/O (input/output)</a> is inherently impure, other operations that you don't typically think of as I/O is impure as well. Generation of random numbers (including GUIDs) is the most prominent example. Random number generators rely on the system clock, which you <em>can</em> think of as an input device, although I think many programmers don't. </p> <p> I could have called the container <em>Impure</em> instead, but I chose to go with <em>IO</em>, since this is the word used in Haskell. It also has the advantage of being short. </p> <h3 id="8964b332bd1a423ba2f9729d679b6225"> What's in the boooox? <a href="#8964b332bd1a423ba2f9729d679b6225" title="permalink">#</a> </h3> <p> A question frequently comes up: <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">How do I get the value out of my IO?</a> As always, the answer is <a href="https://en.wikipedia.org/wiki/Mu_(negative)">mu</a>. You don't. You inject the desired behaviour into the container. This goes for all <a href="/2022/03/28/monads">monads</a>, including IO. </p> <p> But naturally you wonder: If you can't see the value inside the IO box then <em>what's the point?</em> </p> <p> The point is to enforce the functional interaction law at the type level. A pure function that calls an impure action will receive a sealed, opaque IO box. There's no API that enables a pure function to extract the contents of the container, so this effectively enforces the rule that pure functions can't call impure actions. </p> <p> The other three types of interactions are still possible. <ul> <li>Pure functions should be able to call pure functions. Pure functions return 'normal' values (i.e. values not hidden in IO boxes), so they can call each other as usual.</li> <li>Impure actions should be able to call pure functions. This becomes possible because you can inject pure behaviour into any monad. You'll see example of that in later articles in this series.</li> <li>Impure actions should be able to call other impure actions. Likewise, you can compose many IO actions into one IO action via the IO API.</li> </ul> When you're outside of all IO boxes, there's no way to open the box, and you can't see its contents. </p> <p> On the other hand, <em>if you're already inside the box</em>, you can see the contents. And there's one additional rule: If you're already inside an IO box, you <em>can</em> open other IO boxes and see their contents! </p> <p> In subsequent articles in this article series, you'll see how all of this manifests as C# code. This article gives a high-level view of the concept. I suggest that you go back and re-read it once you've seen the code. </p> <h3 id="2f29062933db44278c87649c31704c9a"> The many-worlds interpretation <a href="#2f29062933db44278c87649c31704c9a" title="permalink">#</a> </h3> <p> If you're looking for metaphors or other ways to understand what's going on, there's two perspectives I find useful. None of them offer the full picture, but together, I find that they help. </p> <p> A common interpretation of IO is that it's like the box in which you put <a href="https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat">Schrödinger's cat</a>. <code>IO&lt;Cat&gt;</code> can be viewed as the superposition of the two states of cat (assuming that <code>Cat</code> is basically a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> with the cases <code>Alive</code> and <code>Dead</code>). Likewise, <code>IO&lt;int&gt;</code> represents the superposition of all 4,294,967,296 32-bit integers, <code>IO&lt;string&gt;</code> the superposition of infinitely many strings, etcetera. </p> <p> Only when you observe the contents of the box does the superposition collapse to a single value. </p> <p> But... you can't observe the contents of an IO box, can you? </p> <h3 id="0f85d7e2346d4805ae05045965f9231d"> The black hole interpretation <a href="#0f85d7e2346d4805ae05045965f9231d" title="permalink">#</a> </h3> <p> The IO container represents an impenetrable barrier between the outside and the inside. It's like a <a href="https://en.wikipedia.org/wiki/Black_hole">black hole</a>. Matter can fall into a black hole, but no information can escape its <a href="https://en.wikipedia.org/wiki/Event_horizon">event horizon</a>. </p> <p> In high school I took cosmology, among many other things. I don't know if the following is still current, but we learned a formula for calculating the density of a black hole, based on its mass. When you input the estimated mass of the universe, the formula suggests a density near vacuum. Wait, what?! Are we actually living inside a black hole? Perhaps. Could there be other universes 'inside' black holes? </p> <p> The analogy to the IO container seems apt. You can't see into a black hole from the outside, but once <a href="https://amzn.to/2BlYK6D">beyond the blue event horizon</a>, you can observe everything that goes on in that interior universe. You can't escape to the original universe, though. </p> <p> As with all metaphors, this one breaks down if you think too much about it. Code running in IO can unpack other IO boxes, even nested boxes. There's no reason to believe that if you're inside a black hole that you can then gaze beyond the event horizon of nested black holes. </p> <h3 id="524b6fbfa7c54eb2b26ec8212b7db64e"> Code examples <a href="#524b6fbfa7c54eb2b26ec8212b7db64e" title="permalink">#</a> </h3> <p> In the next articles in this series, you'll see C# code examples that illustrate how this concept might be implemented. The purpose of these code examples is to give you a sense for how <code>IO</code> works in Haskell, but with more familiar syntax. <ul> <li><a href="/2020/06/15/io-container-in-a-parallel-c-universe">IO container in a parallel C# universe</a></li> <li><a href="/2020/06/29/syntactic-sugar-for-io">Syntactic sugar for IO</a></li> <li><a href="/2020/07/06/referential-transparency-of-io">Referential transparency of IO</a></li> <li><a href="/2020/07/13/implementation-of-the-c-io-container">Implementation of the C# IO container</a></li> <li><a href="/2020/07/27/task-asynchronous-programming-as-an-io-surrogate">Task asynchronous programming as an IO surrogate</a></li> </ul> These code examples are <em>illustrations</em>, not recommendations. </p> <h3 id="729688b622f64684b3f5ad2816eadaca"> Conclusion <a href="#729688b622f64684b3f5ad2816eadaca" title="permalink">#</a> </h3> <p> When you saw the title, did you think that this would be an article about IoC Containers? It's not. The title isn't a typo, and I never use the term <em>IoC Container</em>. As <a href="https://blogs.cuttingedge.it/steven/">Steven</a> and I explain in <a href="/dippp">our book</a>, <em>Inversion of Control</em> (IoC) is a broader concept than Dependency Injection (DI). It's called a <em>DI Container</em>. </p> <p> IO, on the other hand, is a container of impure values. Its API enables you to 'build' bigger structures (programs) from smaller IO boxes. You can compose IO actions together and inject pure functions into them. The boxes, however, are opaque. Pure functions can't see their contents. This effectively enforces the functional interaction law at the type level. </p> <p> <strong>Next:</strong> <a href="/2020/06/15/io-container-in-a-parallel-c-universe">IO container in a parallel C# universe</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="33c7fbbfa5ba409a885296b880592e80"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#33c7fbbfa5ba409a885296b880592e80">#</a></div> <div class="comment-content"> <p> Come on, you know there is a perfect metaphor. <a href="https://blog.plover.com/prog/burritos.html">Monads are like burritos.</a> </p> </div> <div class="comment-date">2020-06-08 11:08 UTC</div> </div> <div class="comment" id="d5b99040cf664be5a7b3ae5bfc2b6394"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d5b99040cf664be5a7b3ae5bfc2b6394">#</a></div> <div class="comment-content"> <p> Christer, I appreciate that this is all in good fun 🤓 </p> <p> For the benefit of readers who may still be trying to learn these concepts, I'll point out that just as this isn't an article about <em>IoC containers</em>, it's not a monad tutorial. It's an explanation of a particular API called <em>IO</em>, which, among other traits, also forms a monad. I'm trying to downplay that aspect here, because I hope that you can understand most of this and the next articles without knowing what a monad is. </p> </div> <div class="comment-date">2020-06-08 11:27 UTC</div> </div> <div class="comment" id="6d8a99b9728b452193311fadec795cf4"> <div class="comment-author"><a href="https://github.com/DunetsNM/">Nick Dunets</a> <a href="#6d8a99b9728b452193311fadec795cf4">#</a></div> <div class="comment-content"> <p> "While you know what type of value is inside the box, you can't see what's in it, and you can't open it." </p> <p> Well you technically can, with unsafePerformIO ;) although it defeats the whole purpose. </p> </div> <div class="comment-date">2020-06-10 02:31 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Retiring old service versions https://blog.ploeh.dk/2020/06/01/retiring-old-service-versions 2020-06-01T09:36:00+00:00 Mark Seemann <div id="post"> <p> <em>A few ideas on how to retire old versions of a web service.</em> </p> <p> I was recently listening to <a href="https://dotnetrocks.com/?show=1688">a .NET Rocks! episode on web APIs</a>, and one of the topics that came up was how to retire old versions of a web service. It's not easy to do, but not necessarily impossible. </p> <p> The best approach to API versioning is to never break compatibility. As long as there's no breaking changes, you don't have to version your API. It's rarely possible to completely avoid breaking changes, but the fewer of those that you introduce, the fewer API version you have to maintain. I've <a href="/2015/06/22/rest-implies-content-negotiation">previously described how to version REST APIs</a>, but this article doesn't assume any particular versioning strategy. </p> <p> Sooner or later you'll have an old version that you'd like to retire. How do you do that? </p> <h3 id="66f738fcae73491988edafb0eb766001"> Incentives <a href="#66f738fcae73491988edafb0eb766001" title="permalink">#</a> </h3> <p> First, take a minute to understand why some clients keep using old versions of your API. It may be obvious, but I meet enough programmers who never give incentives a thought that I find it worthwhile to point out. </p> <p> When you introduce a breaking change, by definition this is a change that <em>breaks</em> clients. Thus, upgrading from an old version to a newer version of your API is likely to give client developers extra work. If what they have already works to their satisfaction, why should they upgrade their clients? </p> <p> You might argue that your new version is 'better' because it has more features, or is faster, or whatever it is that makes it better. Still, if the old version is good enough for a particular purpose, some clients are going to stay there. The client maintainers have no incentives to upgrade. There's only cost, and no benefit, to upgrading. </p> <p> Even if the developers who maintain those clients would like to upgrade, they may be prohibited from doing so by their managers. If there's no business reason to upgrade, efforts are better used somewhere else. </p> <h3 id="ccca66feb014455288e12ee519c56ada"> Advance warning <a href="#ccca66feb014455288e12ee519c56ada" title="permalink">#</a> </h3> <p> Web services exist in various contexts. Some are only available on an internal network, while others are publicly available on the internet. Some require an authentication token or API key, while others allow anonymous client access. </p> <p> With some services, you have a way to contact every single client developer. With other services, you don't know who uses your service. </p> <p> Regardless of the situation, when you wish to retire a version, you should first try to give clients advance warning. If you have an address list of all client developers, you can simply write them all, but you can't expect that everyone reads their mail. If you don't know the clients, you can publish the warning. If you have a blog or a marketing site, you can publish the warning there. If you run a mailing list, you can write about the upcoming change there. You can tweet it, post it on Facebook, or dance it on TikTok. </p> <p> Depending on SLAs and contracts, there may even be a legally valid communications channel that you can use. </p> <p> Give advance warning. That's the decent thing to do. </p> <h3 id="bfa13823398b42a8aa1f1ef3de2c6b29"> Slow it down <a href="#bfa13823398b42a8aa1f1ef3de2c6b29" title="permalink">#</a> </h3> <p> Even with advance warning, not everyone gets the message. Or, even if everyone got the message, some people deliberately decide to ignore it. Consider their incentives. They may gamble that as long as your logs show that they use the old version, you'll keep it online. What do you do then? </p> <p> You can, of course, just take the version off line. That's going to break clients that still depend on that version. That's rarely the best course of action. </p> <p> Another option is to degrade the performance of that version. Make it slower. You can simply add a constant delay when clients access that service, or you can gradually degrade performance. </p> <p> Many HTTP client libraries have long timeouts. For example, the <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpclient.timeout">default HttpClient timeout is 100 seconds</a>. Imagine that you want to introduce a gradual performance degradation that starts at no delay on June 1, 2020 and reaches 100 seconds after one year. You can use the formula <code>d = 100 s * (t - t0) / 1 year</code>, where <code>d</code> is the delay, <code>t</code> is the current time, and <code>t0</code> is the start time (e.g. June 1, 2020). This'll cause requests for resources to gradually slow down. After a year, clients still talking to the retiring version will begin to time out. </p> <p> You can think of this as another way to give advance warning. With the gradual deterioration of performance, users will likely notice the long wait times well before calls actually time out. </p> <p> When client developers contact you about the bad performance, you can tell them that the issue is fixed in more recent versions. You've just given the client organisation an incentive to upgrade. </p> <h3 id="741bec1a72bf46cc9e26f6e7600a6fab"> Failure injection <a href="#741bec1a72bf46cc9e26f6e7600a6fab" title="permalink">#</a> </h3> <p> Another option is to deliberately make the service err now and then. Randomly return a <code>500 Internal Server Error</code> from time to time, even if the service <em>can</em> handle the request. </p> <p> Like deliberate performance degradation, you can gradually make the deprecated version more and more unreliable. Again, end users are likely to start complaining about the unreliability of the system long before it becomes entirely useless. </p> <h3 id="bc73620aa71141b6a74f4a4aaf395d75"> Reverse proxy <a href="#bc73620aa71141b6a74f4a4aaf395d75" title="permalink">#</a> </h3> <p> One of the many benefits of HTTP-based services is that you can put a <a href="https://en.wikipedia.org/wiki/Reverse_proxy">reverse proxy</a> in front of your application servers. I've no idea how to configure or operate <a href="https://en.wikipedia.org/wiki/Nginx">NGINX</a> or <a href="https://en.wikipedia.org/wiki/Varnish_(software)">Varnish</a>, but from talking to people who do know, I get the impression that they're quite scriptable. </p> <p> Since the above ideas are independent of actual service implementation or behaviour, it's a generic problem that you should seek to address with general-purpose software. </p> <p> <img src="/content/binary/reverse-proxy-based-delay.png" alt="Sequence diagram of a client, reverse proxy, and application server."> </p> <p> Imagine having a general-purpose reverse proxy that detects whether incoming HTTP requests are for the version you'd like to retire (<em>version 1</em> in the diagram) or another version. If the proxy detects that the request is for a retiring version, it inserts a delay before it forward the request to the application server. For all requests for current versions, it just forwards the request. </p> <p> I could imagine doing something similar with failure injections. </p> <h3 id="a0426537b459414b831db955de4cca26"> Legal ramifications <a href="#a0426537b459414b831db955de4cca26" title="permalink">#</a> </h3> <p> All of the above are only ideas. If you can use them, great. Consider the implications, though. You may be legally obliged to keep an SLA. In that case, you can't degrade the performance or reliability below the SLA level. </p> <p> In any case, I don't think you should do any of these things lightly. Involve relevant stakeholders before you engage in something like the above. </p> <p> Legal specialists are as careful and conservative as traditional operations teams. Their default reaction to any change proposal is to say <em>no</em>. That's not a judgement on their character or morals, but simply another observation based on incentives. As long as everything works as it's supposed to work, any change represents a risk. Legal specialists, like operations teams, are typically embedded in incentive systems that punish risk-taking. </p> <p> To counter other stakeholders' reluctance to engage in unorthodox behaviour, you'll have to explain why retiring an old version of the service is important. It works best if you can quantify the reason. If you can, measure how much extra time you waste on maintaining the old version. If the old version runs on separate hardware, or a separate cloud service, quantify the cost overhead. </p> <p> If you can't produce a compelling argument to retire an old version, then perhaps it isn't that important after all. </p> <h3 id="725c16c05869474a8cf58da8f0705b7e"> Logs <a href="#725c16c05869474a8cf58da8f0705b7e" title="permalink">#</a> </h3> <p> Server logs can be useful. They can tell you how many requests the old version serves, which IP addresses they come from, at which times or dates you have most traffic, and whether the usage trend is increasing or decreasing. </p> <p> These measures can be used to support your argument that a particular version should be retired. </p> <h3 id="226e48b1d4ef4a14bbf778a9fd6983f7"> Conclusion <a href="#226e48b1d4ef4a14bbf778a9fd6983f7" title="permalink">#</a> </h3> <p> Versioning web services is already a complex subject, but once you've figured it out, you'll sooner or later want to retire an old version of your API. If some clients still make good use of that version, you'll have to give them incentive to upgrade to a newer version. </p> <p> It's best if you can proactively make clients migrate, so prioritise amiable solutions. Sometimes, however, you have no way to reach all client developers, or no obvious way to motivate them to upgrade. In those cases, gentle and gradual performance or reliability degradation of deprecated versions could be a way. </p> <p> I present these ideas as nothing more than that: ideas. Use them if they make sense in your context, but think things through. The responsibility is yours. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c7751c3a6d5b4facbf7a552eeaa62792"> <div class="comment-author"><a href="http://blog.strobaek.org">Karsten Strøbæk</a> <a href="#c7751c3a6d5b4facbf7a552eeaa62792">#</a></div> <div class="comment-content"> <p> Hi Mark. As always an excellent piece. A few comments if I may. </p> <p> An assumption seems to be, that the client is able to update to a new version of the API, but is not inclined to do so for various reasons. I work with organisations where updating a client if nearly impossible. Not because of lack of will, but due to other factors such as government regulations, physical location of client, hardware version or capabilities of said client to name just a few. </p> <p> We have a tendency in the software industry to see updates as a question of 'running Windows update' and then all is good. Most likely because that is the world we live in. If we wish to update a program or even an OS, it is fairly easy even your above considerations taken into account. </p> <p> In the 'physical' world of manufacturing (or pharmacy or mining or ...) the situation is different. The lifetime of the 'thing' running the client is regularly measured in years or even decades and not weeks or months as it is for a piece of software. </p> <p> Updates are often equal to bloating of resource requirements meaning you have to replace the hardware. This might not always be possible again for various reasons. Cost (company is pressed on margin or client is located in Outer Mongolia) or risk (client is located in Syria or some other hotspot) are some I've personally come across. </p> <p> REST/HTTP is not used. I acknowledge that the original podcast from .NET Rocks! was about updating a web service. This does not change the premises of your arguments, but it potentially adds a layer of complication. </p> </div> <div class="comment-date">2020-06-02 14:47 UTC</div> </div> <div class="comment" id="3e0454135e9b49f1b27bec577318484a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3e0454135e9b49f1b27bec577318484a">#</a></div> <div class="comment-content"> <p> Karsten, thank you for writing. You are correct that the underlying assumption is that you can retire old versions. </p> <p> I, too, have written REST APIs where retiring service versions weren't an option. These were APIs that consumer-grade hardware would communicate with. We had no way to assure that consumers would upgrade their hardware. Those boxes wouldn't have much of a user-interface. Owners might not realise that firmware updates were available, even if they were. </p> <p> This article does, indeed, assume that the reader has made an informed decision that it's fundamentally acceptable to retire a service version. I should have made that more explicit. </p> </div> <div class="comment-date">2020-06-04 5:32 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Where's the science? https://blog.ploeh.dk/2020/05/25/wheres-the-science 2020-05-25T05:50:00+00:00 Mark Seemann <div id="post"> <p> <em>Is a scientific discussion about software development possible?</em> </p> <p> Have you ever found yourself in a heated discussion about a software development topic? Which is best? Tabs or spaces? Where do you put the curly brackets? <a href="/2020/05/04/significant-whitespace-is-dry">Is significant whitespace a good idea?</a> Is Python better than Go? Does test-driven development yield an advantage? <a href="/2019/07/01/yes-silver-bullet">Is there a silver bullet?</a> Can you <a href="https://martinfowler.com/bliki/CannotMeasureProductivity.html">measure software development productivity?</a> </p> <p> I've had plenty of such discussions, and I'll have them again in the future. </p> <p> While some of these discussions may resemble debates on <a href="https://en.wikipedia.org/wiki/How_many_angels_can_dance_on_the_head_of_a_pin%3F">how many angels can dance on the head of a pin</a>, other discussions might be important. Ex ante, it's hard to tell which is which. </p> <p> Why don't we settle these discussions with science? </p> <h3 id="dc849de642834dd998e0fdce57b91550"> A notion of science <a href="#dc849de642834dd998e0fdce57b91550" title="permalink">#</a> </h3> <p> I love science. Why don't I apply scientific knowledge instead of arguments based on <a href="https://martinfowler.com/bliki/AnecdotalEvidence.html">anecdotal evidence</a>? </p> <p> To answer such questions, we must first agree on a definition of science. I favour <a href="https://en.wikipedia.org/wiki/Karl_Popper">Karl Popper</a>'s description of <a href="https://en.wikipedia.org/wiki/Falsifiability">empirical falsifiability</a>. A hypothesis that makes successful falsifiable predictions of the future is a good scientific theory. Such a a theory has <a href="https://en.wikipedia.org/wiki/Predictive_power">predictive power</a>. </p> <p> Newton's theory of gravity had ample predictive power, but Einstein's theory of general relativity supplanted it because its predictive power was even better. </p> <p> <a href="https://en.wikipedia.org/wiki/Gregor_Mendel">Mendel</a>'s theory of inheritance had predictive power, but was refined into what is modern-day genetics which yields much greater predictive power. </p> <p> Is predictive power the only distinguishing trait of good science? I'm already venturing into controversial territory by taking this position. I've met people in the programming community who consider my position naive or reductionist. </p> <p> What about <em>explanatory power?</em> If a theory satisfactorily explains observed phenomena, doesn't that count as a proper scientific theory? </p> <h3 id="080ec1ebb66c498f8d21bfce5939e480"> Controversy <a href="#080ec1ebb66c498f8d21bfce5939e480" title="permalink">#</a> </h3> <p> I don't believe in explanatory power as a sufficient criterion for science. Without predictive power, we have little evidence that an explanation is correct. An explanatory theory can even be internally consistent, and yet we may not know if it describes reality. </p> <p> Theories with explanatory power are the realm of politics or religion. Consider the observation that some people are rich and some are poor. You can believe in a theory that explains this by claiming structural societal oppression. You can believe in another theory that views poor people as fundamentally lazy. Both are (somewhat internally consistent) political theories, but they have yet to demonstrate much predictive power. </p> <p> Likewise, you may believe that some deity created the universe, but that belief produces no predictions. You can apply <a href="https://en.wikipedia.org/wiki/Occam%27s_razor">Occam's razor</a> and explain the same phenomena without a god. A belief in one or more gods is a religious theory, not a scientific theory. </p> <p> It seems to me that there's a correlation between explanatory power and controversy. Over time, theories with predictive power become uncontroversial. Even if they start out controversial (such as Einstein's theory of general relativity), the dust soon settles because it's hard to argue with results. </p> <p> Theories with mere explanatory power, on the other hand, can fuel controversy forever. Explanations can be compelling, and without evidence to refute them, the theories linger. </p> <p> Ironically, you might argue that Popper's theory of scientific discovery itself is controversial. It's a great explanation, but does it have predictive power? Not much, I admit, but I'm also not aware of competing views on science with better predictive power. Thus, you're free to disagree with everything in this article. I admit that it's a piece of philosophy, not of science. </p> <h3 id="1f26035f0b3c40d4b0681a6b2f45176c"> The practicality of experimental verification <a href="#1f26035f0b3c40d4b0681a6b2f45176c" title="permalink">#</a> </h3> <p> We typically see our field of software development as one of the pillars of <a href="https://en.wikipedia.org/wiki/Science,_technology,_engineering,_and_mathematics">STEM</a>. Many of us have STEM educations (I don't; I'm an economist). Yet, we're struggling to come to grips with the lack of scientific methodology in our field. It seems to me that we suffer from <a href="https://en.wikipedia.org/wiki/Physics_envy">physics envy</a>. </p> <p> It's really hard to compete with physics when it comes to predictive power, but even modern physics struggle with experimental verification. Consider an establishment like <a href="https://home.cern">CERN</a>. It takes billions of euros of investment to make today's physics experiments possible. The only reason we make such investments, I think, is that physics so far has had a good track record. </p> <p> What about another fairly 'hard' science like medicine? In order to produce proper falsifiable predictions, medical science have evolved the process of the <a href="https://en.wikipedia.org/wiki/Randomized_controlled_trial">randomised controlled trial</a>. It works well when you're studying short-term effects. Does this medicine cure this disease? Does this surgical procedure improve a patient's condition? How does lack of REM sleep for three days affect your ability to remember strings of numbers? </p> <p> When a doctor tells you that a particular medicine helps, or that surgery might be warranted, he or she is typically on solid scientific grounds. </p> <p> Here's where things start to become less clear, though, What if <a href="https://en.wikipedia.org/wiki/Atkins_diet">a doctor tells you that a particular diet</a> will improve your expected life span? Is he or she on scientific solid ground? </p> <p> That's rarely the case, because you can't make randomised controlled trials about life styles. Or, rather, a totalitarian society might be able to do that, but we'd consider it unethical. Consider what it would involve: You'd have to randomly select a significant number of <em>babies</em> and group them into those who must follow a particular life style, and those who must not. Then you'll somehow have to force those people to stick to their randomly assigned life style for the entirety of their lives. This is not only unethical, but the experiment also takes the most of a century to perform. </p> <p> What life-style scientists instead do is resort to demographic studies, with all the problems of statistics they involve. Again, the question is whether scientific theories in this field offer predictive power. Perhaps they do, but it takes decades to evaluate the results. </p> <p> My point is that medicine isn't exclusively a hard science. Some medicine is, and some is closer to social sciences. </p> <p> I'm an economist by education. Economics is generally not considered a hard science, although it's a field where it's trivial to make falsifiable predictions. Almost all of economics is about numbers, so making a falsifiable prediction is trivial: <em>The MSFT stock will be at 200 by January 1 2021. The unemployment rate in Denmark will be below 5% in third quarter of 2020.</em> The problem with economics is that most of such predictions turn out to be no better than the toss of a coin - even when made by economists. You can make falsifiable predictions in economics, but most of them do, in fact, get falsified. </p> <p> On the other hand, with the advances in such disparate fields as DNA forensics, satellite surveys, and computer-aided image processing, a venerable 'art' like archaeology is gaining predictive power. <em>We predict that if we dig here, we'll find artefacts from the iron age. We predict that if we make a DNA test of these skeletal remains, they'll show that the person buried was a middle-aged women.</em> And so on. </p> <p> One thing is the ability to produce falsifiable predictions. Another things is whether or not the associated experiment is practically possible. </p> <h3 id="1dda923d8e8f4eecbf6cdd79cdc0e27c"> The science of software development <a href="#1dda923d8e8f4eecbf6cdd79cdc0e27c" title="permalink">#</a> </h3> <p> Do we have a science of software development? I don't think that we have. </p> <p> There's <a href="https://en.wikipedia.org/wiki/Computer_science">computer science</a>, but that's not quite the same. That field of study has produced many predictions that hold. <em>In general, <a href="https://en.wikipedia.org/wiki/Quicksort">quicksort</a> will be faster than <a href="https://en.wikipedia.org/wiki/Bubble_sort">bubble sort</a>. There's an algorithm for finding the shortest way through a network.</em> That sort of thing. </p> <p> You will notice that these result are hardly controversial. It's not those topics that we endlessly debate. </p> <p> We debate whether certain ways to organise work is more 'productive'. The entire productivity debate revolves around an often implicit context: that what we discuss is long-term productivity. We don't much argue how to throw together something during a weekend hackaton. We argue whether we can complete a nine-month software project safer with test-driven development. We argue whether a code base can better sustain its organisation year after year if it's written in <a href="https://fsharp.org">F#</a> or JavaScript. </p> <p> There's <a href="https://www.goodreads.com/review/show/1956107880">little scientific evidence on those questions</a>. </p> <p> The main problem, as I see it, is that it's impractical to perform experiments. Coming up with falsifiable predictions is easy. </p> <p> Let's consider an example. Imagine that your hypothesis is that test-driven development makes you more productive in the middle and long run. You'll have to turn that into a falsifiable claim, so first, pick a software development project of sufficient complexity. Imagine that you can find a project that someone estimates will take around ten months to complete for a team of five people. This has to be a real project that someone needs done, complete with vague, contradictory, and changing requirements. Now you formulate your falsifiable prediction, for example: <em>"This project will be delivered one month earlier with test-driven development."</em> </p> <p> Next, you form teams to undertake the work. One team to perform the work <em>with</em> test-driven development, and one team to do the work without it. Then you measure when they're done. </p> <p> This is already impractical, because who's going to pay for two teams when one would suffice? </p> <p> Perhaps, if you're an exceptional proposal writer, you could get a research grant for that, but alas, that wouldn't be good enough. </p> <p> With two competing teams of five people each, it might happen that <a href="/2019/09/30/10x-developers">one team member exhibits productivity orders of magnitudes different from the others</a>. That could skew the experimental outcome, so you'd have to go for a proper randomised controlled trial. This would involve picking numerous teams and assigning a methodology at random: either they do test-driven development, or they don't. Nothing else should vary. They should all use the same programming language, the same libraries, the same development tools, and work the same hours. Furthermore, no-one outside the team should know which teams follow which method. </p> <p> Theoretically possible, but impractical. It would require finding and paying many software teams for most of a year. One such experiment would cost millions of euros. </p> <p> If you did such an experiment, it would tell you something, but it'd still be open to interpretation. You might argue that the programming language used caused the outcome, but that one can't extrapolate from that result to other languages. Or perhaps there was something special about the project that you think doesn't generalise. Or perhaps you take issue with the pool from which the team members were drawn. You'd have to repeat the experiment while varying one of the other dimensions. That'll cost millions more, and take another year. </p> <p> Considering the billions of euros/dollars/pounds the world's governments pour into research, you'd think that we could divert a few hundred millions to do proper research in software development, but it's unlikely to happen. That's the reason we have to contend ourselves with arguing from anecdotal evidence. </p> <h3 id="6af72b5a24aa4caba250da2373a975d9"> Conclusion <a href="#6af72b5a24aa4caba250da2373a975d9" title="permalink">#</a> </h3> <p> I can imagine how scientific inquiry into software engineering could work. It'd involve making a falsifiable prediction, and then set up an experiment to prove it wrong. Unfortunately, to be on a scientifically sound footing, experiments should be performed with randomised controlled trials, with a statistically significant number of participants. It's not too hard to conceive of such experiments, but they'd be prohibitively expensive. </p> <p> In the meantime, the software development industry moves forward. We share ideas and copy each other. Some of us are successful, and some of us fail. Slowly, this might lead to improvements. </p> <p> That process, however, looks more like evolution than scientific progress. The fittest software development organisations survive. They need not be the best, as they could be stuck in local maxima. </p> <p> When we argue, when we write blog posts, when we speak at conferences, when we appear on podcasts, we exchange ideas and experiences. Not genes, but <a href="https://en.wikipedia.org/wiki/Meme">memes</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5eec3680e62d40ca8d6e6ab7cc3cf34f"> <div class="comment-author">Sergey Petrov <a href="#5eec3680e62d40ca8d6e6ab7cc3cf34f">#</a></div> <div class="comment-content"> <p> That topic is something many software developers think about, at least I do from time to time. </p> <p> Your post reminded me of that conference talk <a href="https://www.youtube.com/watch?v=WELBnE33dpY">Intro to Empirical Software Engineering: What We Know We Don't Know</a> by Hillel Wayne. Just curious - have you seen the talk and if so - what do you think? Researches mentioned in the talk are not proper scientific expreiments as you describe, but anyway looked really interesting to me. </p> </div> <div class="comment-date">2020-05-28 12:40 UTC</div> </div> <div class="comment" id="c223fed5e0294187a5faaca6c885d945"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c223fed5e0294187a5faaca6c885d945">#</a></div> <div class="comment-content"> <p> Sergey, thank you for writing. I didn't know about that talk, but Hillel Wayne regularly makes an appearance in my Twitter feed. I've now seen the talk, and I think it offers a perspective close to mine. </p> <p> I've already read <em>The Leprechauns of Software Engineering</em> (above, I linked to my review), but while I was aware of <a href="https://amzn.to/2XQPHSV">Making Software</a>, I've yet to read it. Several people have reacted to my article by recommending that book, so it's now on it's way to me in the mail. </p> </div> <div class="comment-date">2020-06-02 10:26 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Modelling versus shaping reality https://blog.ploeh.dk/2020/05/18/modelling-versus-shaping-reality 2020-05-18T07:08:00+00:00 Mark Seemann <div id="post"> <p> <em>How does software development relate to reality?</em> </p> <p> I recently <a href="https://dotnetrocks.com/?show=1685">appeared as a guest on the <em>.NET Rocks!</em> podcast</a> where we discussed <a href="https://en.wikipedia.org/wiki/Fred_Brooks">Fred Brooks</a>' 1986 essay <a href="https://en.wikipedia.org/wiki/No_Silver_Bullet">No Silver Bullet</a>. As a reaction, <a href="https://jonsmusings.com">Jon Suda</a> wrote <a href="https://jonsmusings.com/Full-Silver-Jacket">a thoughtful piece of his own</a>. That made me think some more. </p> <h3 id="80fd2177df7e4569a292b795ec32a1a3"> Beware of Greeks... <a href="#80fd2177df7e4569a292b795ec32a1a3" title="permalink">#</a> </h3> <p> Brooks' central premise is <a href="https://en.wikipedia.org/wiki/Aristotle">Aristotle</a>'s distinction between <em>essential</em> and <em>accidental</em> complexity. I've already examined the argument in <a href="/2019/07/01/yes-silver-bullet">my previous article on the topic</a>, but in summary, Brooks posits that complexity in software development can be separated into those two categories: </p> <p> <pre>c = E + a</pre> </p> <p> Here, <code>c</code> is the total complexity, <code>E</code> is the essential complexity, and <code>a</code> the accidental complexity. I've deliberately represented <code>c</code> and <code>a</code> with lower-case letters to suggest that they represent variables, whereas I mean to suggest with the upper-case <code>E</code> that the essential complexity is constant. That's Brooks' argument: Every problem has an essential complexity that can't be changed. Thus, your only chance to reduce total complexity <code>c</code> is to reduce the accidental complexity <code>a</code>. </p> <p> Jon Suda writes that <blockquote> <p> "Mark doesn’t disagree with the classification of problems" </p> <footer><cite><a href="https://jonsmusings.com/Full-Silver-Jacket">Jon Suda</a></cite></footer> </blockquote> That got me thinking, because actually I do. When I wrote <a href="/2019/07/01/yes-silver-bullet">Yes silver bullet</a> I wanted to engage with Brooks' essay on its own terms. </p> <p> I do think, however, that one should be sceptical of any argument originating from the ancient Greeks. These people believed that all matter was composed of <a href="https://en.wikipedia.org/wiki/Classical_element">earth, water, air, and fire</a>. They believed in the <a href="https://en.wikipedia.org/wiki/Emission_theory_(vision)">extramission theory of vision</a>. They practised medicine by <a href="https://en.wikipedia.org/wiki/Humorism">trying to balance blood, phlegm, yellow, and black bile</a>. Aristotle believed in <a href="https://en.wikipedia.org/wiki/Spontaneous_generation">spontaneous generation</a>. He also <a href="https://en.wikipedia.org/wiki/History_of_neuroscience">believed that the brain was a radiator while the heart was the seat of intelligence</a>. </p> <p> I think that Aristotle's distinction between essential and accidental complexity is a <a href="https://en.wikipedia.org/wiki/False_dilemma">false dichotomy</a>, at least in the realm of software development. </p> <p> The problem is the assumption that there's a single, immutable underlying reality to be modelled. </p> <h3 id="006f597e772546c2a1083d98ec21f87c"> Modelling reality <a href="#006f597e772546c2a1083d98ec21f87c" title="permalink">#</a> </h3> <p> Jon Suda touches on this as well: <blockquote> <p> "Conceptual (or business) problems are rooted in the problem domain (i.e., the real world)[...]" </p> <p> "Dealing with the "messy real world" is what makes software development hard these days" </p> <footer><cite><a href="https://jonsmusings.com/Full-Silver-Jacket">Jon Suda</a></cite></footer> </blockquote> I agree that the 'real world' (whatever it is) is often messy, and our ability to deal with the mess is how we earn our keep. It seems to me, though, that there's an underlying assumption that there's a single real world to be modelled. </p> <p> I think that a more fruitful perspective is to question that assumption. <a href="http://udidahan.com/2012/03/05/dont-try-to-model-the-real-world-it-doesnt-exist">Don’t try to model the real world, it doesn’t exist.</a> </p> <p> I've mostly worked with business software. Web shops, <a href="https://twitter.com/ploeh/status/530320252790669313">BLOBAs</a>, and other software to support business endeavours. This is the realm implicitly addressed by <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> and a technique like <a href="https://en.wikipedia.org/wiki/Behavior-driven_development">behaviour-driven development</a>. The assumption is that there's one or more <em>domain experts</em> who know how the business operates, and the job of the software development team is to translate that understanding into working software. </p> <p> This is often the way it happens. Lord knows that I've been involved in enough fixed-requirements contracts to know that sometimes, all you can do is try to implement the requirements as best you can, regardless of how messy they are. In other words, I agree with Jon Suda that messy problems are a reality. </p> <p> Where I disagree with Brooks and Aristotle is that business processes contain essential complexity. In the old days, before computers, businesses ran according to a set of written and unwritten rules. Routine operations might be fairly consistent, but occasionally, something out of the ordinary would happen. Organisations don't have a script for every eventuality. When the unexpected happens, people wing it. </p> <p> A business may have a way it sells its goods. It may have a standard price list, and a set of discounts that salespeople are authorised to offer. It may have standard payment terms that customers are expected to comply with. It may even have standard operating procedures for dealing with missing payments. </p> <p> Then one day, say, the German government expresses interest in placing an order greater than the business has ever handled before. The Germans, however, want a special discount, and special terms of payment. What happens? Do the salespeople refuse because those requests don't comply with the way the organisation does business? Of course not. The case is escalated to people with the authority to change the rules, and a deal is made. </p> <p> Later, the French government comes by, and a similar situation unfolds, but with the difference that someone else handles the French, and the deal struck is different. </p> <p> The way these two cases are handled could be internally inconsistent. Decisions are made based on concrete contexts, but with incomplete information and based on gut feelings. </p> <p> While there may be a system to how an organisation does routine business, there's no uniform reality to be modelled. </p> <p> You can model standard operating procedures in software, but I think it's a mistake to think that it's a model of reality. It's just an interpretation on how routine business is conducted. </p> <p> There's no single, unyielding essential complexity, because the essence isn't there. </p> <h3 id="e9af89838d33486d8c983b50e154e389"> Shaping reality <a href="#e9af89838d33486d8c983b50e154e389" title="permalink">#</a> </h3> <p> <a href="https://www.infoq.com/presentations/Fowler-North-Crevasse-of-Doom">Dan North tells a story</a> of a project where a core business requirement was the ability to print out data. When investigating the issue, it turned out that users needed to have the printout next to their computers so that they could type the data into another system. When asked whether they wouldn't rather prefer to have the data just appear in the other system, they incredulously replied, <em>"You can do that?!</em> </p> <p> <a href="https://twitter.com/ploeh/status/1259832000095059968">This turns out to be a common experience</a>. Someone may tell you about an essential requirement, but when you investigate, it turns out to be not at all essential. There may not be any essential complexity. </p> <p> There's likely to be complexity, but the distinction between essential and accidental complexity seems artificial. While software can model business processes, it can also shape reality. Consider a company like Amazon. The software that supports Amazon wasn't developed <em>after</em> the company was already established. Amazon developed it concurrently with setting up business. </p> <p> Consider companies like Google, Facebook, Uber, or Airbnb. Such software doesn't model reality; it <em>shapes</em> reality. </p> <p> In the beginning of the IT revolution, the driving force behind business software development was to automate existing real-world processes, but this is becoming increasingly rare. New companies enter the markets digitally born. Older organisations may be on their second or third system since they went digital. Software not only models 'messy' reality - it shapes 'messy' reality. </p> <h3 id="8b73389dded94ac593f577969cbdfbea"> Conclusion <a href="#8b73389dded94ac593f577969cbdfbea" title="permalink">#</a> </h3> <p> It may look as though I fundamentally disagree with Jon Suda's blog post. I don't. I agree with almost all of it. It did, however, inspire me to put my thoughts into writing. </p> <p> My position is that I find the situation more nuanced than Fred Brooks suggests by setting off from Aristotle. I don't think that the distinction between essential and accidental complexity is the whole story. Granted, it provides a fruitful and inspiring perspective, but while we find it insightful, we shouldn't accept it as gospel. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="10c9a14dbcd64f28835e7c136d4f9a93"> <div class="comment-author"><a href="https://jonsmusings.com/">Jon Suda</a> <a href="#10c9a14dbcd64f28835e7c136d4f9a93">#</a></div> <div class="comment-content"> <p> I think I agree with virtually everything said here – if not <em>actually</em> everything 😊 </p> <p> As (virtually, if not <em>actually</em>) always, though, there are a few things I’d like to add, clarify, and elaborate on 😊 </p> <p> I fully share your reluctance to accept ancient Greek philosophy. I once discussed No Silver Bullet (and drank Scotch) with a (non-developer) “philosopher” friend of mine. (Understand: He has a BA in philosophy 😊) He said something to the effect of <em>do you understand this essence/accident theory is considered obsolete?</em> I answered that it was beside the point here – at least as far as I was concerned. For me, the dichotomy serves as an inspiration, a metaphor perhaps, not a rigorous theoretical framework – that’s one of the reasons why I adapted it into the (informal) conceptual/technical distinction. </p> <p> I also 100% agree with the notion that software shouldn’t merely “model” or “automate” reality. In fact, I now remember that back in university, this was one of the central themes of my “thesis” (or whatever it was). I pondered the difference/boundary between analysis and design activities and concluded that the idea of creating a model of the “real world” during analysis and then building a technical representation of this model as part of design/implementation couldn’t adequately describe many of the more “inventive” software projects and products. </p> <p> I don’t, however, believe this makes the conceptual/technical (essential/accidental) distinction go away. Even though the point of a project may not (and should not) be a mere automation of a preexisting reality, you still need to know what you want to achieve (i.e., “invent”), <em>conceptually</em>, to be able to fashion a <em>technical</em> implementation of it. And yes, your conceptual model should be based on what’s possible with all available technology – which is why you’ll hopefully end up with something <em>way</em> better than the old solution. (Note that in my post, I never talk about “modeling” the “messy real world” but rather “dealing with it”; even your revolutionary new solution will have to coexist with the messy outside world.) </p> <p> For me, one of the main lessons of No Silver Bullet, the moral of the story, is this: Developers tend to spend inordinate amounts of time discussing and brooding over geeky technical stuff; perhaps they should, instead, talk to their users a bit more and learn something about the problem domain; that’s where the biggest room for improvement is, in my opinion. </p> </div> <div class="comment-date">2020-05-20 16:40 UTC</div> </div> <div class="comment" id="15e5f7c1886343cdbde0bcd30f7cfbc4"> <div class="comment-author"><a href="https://jonsmusings.com/">Jon Suda</a> <a href="#15e5f7c1886343cdbde0bcd30f7cfbc4">#</a></div> <div class="comment-content"> <p> FWIW </p> <p> <a href="https://jonsmusings.com/Transmutation-of-Reality">https://jonsmusings.com/Transmutation-of-Reality</a> </p> <p> Let the inspiration feedback loop continue 😊 I agree with more-or-less everything you say; I just don’t necessarily think that your ideas are incompatible with those expressed in No Silver Bullet. That, to a significant extent (but not exclusively), is what my new post is about. As I said in my previous comment, your article reminded me of my university “thesis,” and I felt that the paper I used as its “conceptual framework” was worth presenting in a “non-academic” form. It can mostly be used to support your arguments – I certainly use it that way. </p> </div> <div class="comment-date">2020-05-29 17:28 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AFK https://blog.ploeh.dk/2020/05/11/afk 2020-05-11T07:04:00+00:00 Mark Seemann <div id="post"> <p> <em>Software development productivity tip: regularly step away from the computer.</em> </p> <p> In these days of covid-19, people are learning that productivity is imperfectly correlated to the amount of time one is physically present in an office. Indeed, the time you spend in front of you computer is hardly correlated to your productivity. After all, <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">programming productivity isn't measured by typing speed</a>. Additionally, you can waste much time at the computer, checking Twitter, watching cat videos on YouTube, etc. </p> <h3 id="63ec07d591da4ac0b8f47501ba65eb6a"> Pomodorobut <a href="#63ec07d591da4ac0b8f47501ba65eb6a" title="permalink">#</a> </h3> <p> I've worked from home for years, so I thought I'd share some productivity tips. I only report what works for me. If you can use some of the tips, then great. If they're not for you, that's okay too. I don't pretend that I've found any secret sauce or universal truth. </p> <p> A major problem with productivity is finding the discipline to work. I use <em>Pomodorobut</em>. It's like <a href="https://www.scrum.org/resources/what-scrumbut">Scrumbut</a>, but for the <a href="https://en.wikipedia.org/wiki/Pomodoro_Technique">Pomodoro technique</a>. For years I thought I was using the Pomodoro technique, but <a href="https://twitter.com/arialdomartini/status/1238426025429794818">Arialdo Martini makes a compelling case that I'm not</a>. He suggests just calling it <em>timeboxing</em>. </p> <p> I think it's a little more than that, though, because the way I use Pomodorobut has two equally important components: <ul> <li>The 25-minute work period</li> <li>The 5-minute break</li> </ul> The 25 minutes is a sufficiently short period that even if you loathe the task ahead of you, you can summon the energy to do it for 25 minutes. Once you get started, it's usually not that bad. </p> <p> When you program, you often run into problems: <ul> <li>There's a bug that you don't understand.</li> <li>You can't figure out the correct algorithm to implement a particular behaviour.</li> <li>Your code doesn't compile, and you don't understand why.</li> <li>A framework behaves inexplicably.</li> </ul> When you're in front of your computer, you can be stuck at such problems for hours on end. </p> <p> The solution: take a break. </p> <h3 id="402a30606d684e179e8a0379c967a31a"> Breaks give a fresh perspective <a href="#402a30606d684e179e8a0379c967a31a" title="permalink">#</a> </h3> <p> I take my Pomodorobut breaks seriously. My rule is that I must leave my office during the break. I usually go get a glass of water or the like. The point is to get out of the chair and <em>afk</em> (away from keyboard). </p> <p> While I'm out the room, it often happens that I get an insight. If I'm stuck on something, I may think of a potential solution, or I may realise that the problem is irrelevant, because of a wider context I forgot about when I sat in front of the computer. </p> <p> You may have heard about <a href="https://en.wikipedia.org/wiki/Rubber_duck_debugging">rubber ducking</a>. Ostensibly <blockquote> <p> "By having to verbalize [...], you may suddenly gain new insight into the problem." </p> <footer><cite><a href="http://bit.ly/the-pragmatic-programmer">Andy Hunt and Dave Thomas</a></cite></footer> </blockquote> I've tried it enough times: you ask a colleague if he or she has a minute, launch into an explanation of your problem, only to break halfway through and say: <em>"Never mind! I suddenly realised what the problem is. Thank you for your help."</em> </p> <p> Working from home, I haven't had a colleague I could disturb like that for years, and I don't actually use a rubber duck. In my experience, getting out of my chair works equally well. </p> <p> The Pomodorobut technique makes me productive because the breaks, and the insights they spark, reduce the time I waste on knotty problems. When I'm in danger of becoming stuck, I often find a way forward in less than 30 minutes: at most 25 minutes being stuck, and a 5-minute break to get unstuck. </p> <h3 id="814a2859670247dc8add4a6d1a2cd29c"> Hammock-driven development <a href="#814a2859670247dc8add4a6d1a2cd29c" title="permalink">#</a> </h3> <p> Working from home gives you extra flexibility. I have a regular routine where I go for a run around 10 in the morning. I also routinely go grocery shopping around 14 in the afternoon. Years ago, when I still worked in an office, I'd ride my bicycle to and from work every day. I've had my good ideas during those activities. </p> <p> In fact, I can't recall ever having had a profound insight in front of the computer. They always come to me when I'm away from it. For instance, I distinctly remember walking around in my apartment doing other things when I realised that <a href="/2018/06/25/visitor-as-a-sum-type">the Visitor design pattern is just another encoding of a sum type</a>. </p> <p> Insights don't come for free. As Rich Hickey points out in his talk about <a href="https://youtu.be/f84n5oFoZBc">hammock-driven development</a>, you must feed your 'background mind'. That involves deliberate focus on the problem. </p> <p> Good ideas don't come if you're permanently away from the computer, but neither do they arrive if all you do is stare at the screen. It's the variety that makes you productive. </p> <h3 id="3bcce686b9ed4deba31c7e9f7bb27893"> Conclusion <a href="#3bcce686b9ed4deba31c7e9f7bb27893" title="permalink">#</a> </h3> <p> Software development productivity is weakly correlated with the time you spend in front of the computer. I find that I'm most productive when I can vary my activities during the day. Do a few 25-minute sessions, rigidly interrupted by breaks. Go for a run. Work a bit more on the computer. Have lunch. Do one or two more time-boxed sessions. Go grocery shopping. Conclude with a final pair of work sessions. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Significant whitespace is DRY https://blog.ploeh.dk/2020/05/04/significant-whitespace-is-dry 2020-05-04T10:02:00+00:00 Mark Seemann <div id="post"> <p> <em>Languages with explicit scoping encourage you to repeat yourself.</em> </p> <p> When the talk falls on significant whitespace, the most common reaction I hear seems to be one of nervous <a href="https://en.wikipedia.org/wiki/Apotropaic_magic">apotropaic</a> deflection. <blockquote> <p> "Indentation matters? Oh, no! I'm horrified." </p> <footer><cite><a href="https://dotnetrocks.com/?show=1665">Carl Franklin</a></cite></footer> </blockquote> I've always wondered why people react like that. What's the problem with significant whitespace? </p> <p> If given a choice, I'd <em>prefer</em> indentation to matter. In that way, I don't have to declare scope more than once. </p> <h3 id="d4077d55ab644786b54672af077f4729"> Explicit scope <a href="#d4077d55ab644786b54672af077f4729" title="permalink">#</a> </h3> <p> If you had to choose between the following three C# implementations of the <a href="http://codingdojo.org/kata/FizzBuzz/">FizzBuzz kata</a>, which one would you choose? </p> <p> <em>a:</em> </p> <p> <pre><span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Program</span> { <span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Main</span>() { <span style="font-weight:bold;color:#8f08c4;">for</span>&nbsp;(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&nbsp;1;&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;&lt;&nbsp;100;&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>++) { <span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;15&nbsp;==&nbsp;0) { <span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;FizzBuzz&quot;</span>); } <span style="font-weight:bold;color:#8f08c4;">else</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;3&nbsp;==&nbsp;0) { <span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;Fizz&quot;</span>); } <span style="font-weight:bold;color:#8f08c4;">else</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;5&nbsp;==&nbsp;0) { <span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;Buzz&quot;</span>); } <span style="font-weight:bold;color:#8f08c4;">else</span> { <span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="font-weight:bold;color:#1f377f;">i</span>); } } } }</pre> </p> <p> <em>b:</em> </p> <p> <pre><span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Program</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Main</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">for</span>&nbsp;(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&nbsp;1;&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;&lt;&nbsp;100;&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;15&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;FizzBuzz&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;3&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;Fizz&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;5&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;Buzz&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="font-weight:bold;color:#1f377f;">i</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> <em>c:</em> </p> <p> <pre><span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Program</span>&nbsp;{&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">Main</span>()&nbsp;{&nbsp;<span style="font-weight:bold;color:#8f08c4;">for</span>&nbsp;(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;=&nbsp;1;&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;&lt;&nbsp;100;&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>++)&nbsp;{&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;15&nbsp;==&nbsp;0)&nbsp;{&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;FizzBuzz&quot;</span>);&nbsp;}&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;3&nbsp;==&nbsp;0)&nbsp;{&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;Fizz&quot;</span>);&nbsp;}&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;%&nbsp;5&nbsp;==&nbsp;0)&nbsp;{&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="color:#a31515;">&quot;Buzz&quot;</span>);&nbsp;}&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span>&nbsp;{&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="font-weight:bold;color:#1f377f;">i</span>);&nbsp;}&nbsp;}&nbsp;}&nbsp;}</pre> </p> <p> Which of these do you prefer? <em>a</em>, <em>b</em>, or <em>c?</em> </p> <p> You prefer <em>b.</em> Everyone does. </p> <p> Yet, those three examples are equivalent. Not only do they behave the same - except for whitespace, they're identical. They produce the same effective abstract syntax tree. </p> <p> Even though a language like C# has explicit scoping and statement termination with its curly brackets and semicolons, indentation still matters. It doesn't matter to the compiler, but it matters to humans. </p> <p> When you format code like option <em>b</em>, you express scope twice. Once for the compiler, and once for human readers. You're repeating yourself. </p> <h3 id="f0433a7047ca4809ae347082f9ca6726"> Significant whitespace <a href="#f0433a7047ca4809ae347082f9ca6726" title="permalink">#</a> </h3> <p> Some languages dispense with the <a href="/2019/12/16/zone-of-ceremony">ceremony</a> and let indentation indicate scope. The most prominent is <a href="https://www.python.org">Python</a>, but I've more experience with <a href="https://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>. In F#, you could write FizzBuzz like this: </p> <p> <pre>[&lt;EntryPoint&gt;] <span style="color:blue;">let</span>&nbsp;main&nbsp;argv&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;[1..100]&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;i&nbsp;%&nbsp;15&nbsp;=&nbsp;0&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printfn&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">if</span>&nbsp;i&nbsp;%&nbsp;3&nbsp;=&nbsp;0&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printfn&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">if</span>&nbsp;i&nbsp;%&nbsp;5&nbsp;=&nbsp;0&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printfn&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printfn&nbsp;<span style="color:#a31515;">&quot;%i&quot;</span>&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;0</pre> </p> <p> You don't have to explicitly scope variables or expressions. The scope is automatically indicated by the indentation. You <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">don't repeat yourself</a>. Scope is expressed once, and both compiler and human understand it. </p> <p> I've programmed in F# for a decade now, and I don't find its use of significant whitespace to be a problem. I'd recommend, however, to turn on the feature in your IDE of choice that shows whitespace characters. </p> <h3 id="31e1f00baf044bab8504cf4e2a492773"> Summary <a href="#31e1f00baf044bab8504cf4e2a492773" title="permalink">#</a> </h3> <p> Significant whitespace is a good language feature. You're going to indent your code anyway, so why not let the indentation carry meaning? In that way, you don't repeat yourself. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An F# implementation of the Maître d' kata https://blog.ploeh.dk/2020/04/27/an-f-implementation-of-the-maitre-d-kata 2020-04-27T14:41:00+00:00 Mark Seemann <div id="post"> <p> <em>This article walks you through the Ma&icirc;tre d' kata done in F#.</em> </p> <p> In a previous article, I presented <a href="/2020/01/27/the-maitre-d-kata">the Ma&icirc;tre d' kata</a> and promised to publish a walkthrough. Here it is. </p> <h3 id="f7bc269790014e9d9510c4c034e9a1bb"> Preparation <a href="#f7bc269790014e9d9510c4c034e9a1bb" title="permalink">#</a> </h3> <p> I used <a href="/2019/10/21/a-red-green-refactor-checklist">test-driven development</a> and <a href="https://fsharp.org">F#</a> for both unit tests and implementation. As usual, my test framework was <a href="https://xunit.net">xUnit.net</a> (2.4.0) with <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> (5.0.0) as the assertion library. </p> <p> I could have done the exercise with a <a href="/property-based-testing-intro">property-based testing</a> framework like <a href="https://fscheck.github.io/FsCheck">FsCheck</a> or <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a>, but I chose instead to take my own medicine. In the kata description, I suggested some test cases, so I wanted to try and see if they made sense. </p> <p> The entire code base is <a href="https://github.com/ploeh/maitred-kata-in-fsharp-1">available on GitHub</a>. </p> <h3 id="adc8a50bbe72453c87853ae566b9a185"> Boutique restaurant <a href="#adc8a50bbe72453c87853ae566b9a185" title="permalink">#</a> </h3> <p> I wrote the first suggested test case this way: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Boutique&nbsp;restaurant``&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;canAccept&nbsp;12&nbsp;[]&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;1&nbsp;}&nbsp;@&gt;</pre> </p> <p> This uses Unquote's <code>test</code> function to verify that a Boolean expression is true. The expression is a function call to <code>canAccept</code> with the capacity <code>12</code>, no existing reservations, and a reservation with <code>Quantity = 1</code>. </p> <p> The simplest thing that could possibly work was this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Reservation&nbsp;=&nbsp;{&nbsp;Quantity&nbsp;:&nbsp;int&nbsp;} <span style="color:blue;">let</span>&nbsp;canAccept&nbsp;_&nbsp;_&nbsp;_&nbsp;=&nbsp;<span style="color:blue;">true</span></pre> </p> <p> The <code>Reservation</code> type was required to make the test compile, but the <code>canAccept</code> function didn't have to consider its arguments. It could simply return <code>true</code>. </p> <h3 id="d169683993034e3c86db04a1dfaca279"> Parametrisation <a href="#d169683993034e3c86db04a1dfaca279" title="permalink">#</a> </h3> <p> The next test case made me turn the test function into a parametrised test: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData(&nbsp;1,&nbsp;&nbsp;<span style="color:blue;">true</span>)&gt;] [&lt;InlineData(13,&nbsp;<span style="color:blue;">false</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Boutique&nbsp;restaurant``&nbsp;quantity&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;canAccept&nbsp;12&nbsp;[]&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;quantity&nbsp;}</pre> </p> <p> So far, the only test parameters were <code>quantity</code> and the <code>expected</code> result. I could no longer use <code>test</code> to verify the result of calling <code>canAccept</code>, since I added variation to the <code>expected</code> result. I changed <code>test</code> into Unquote's <code>=!</code> (<em>must equal</em>) operator. </p> <p> The simplest passing implementation I could think of was: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;_&nbsp;_&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q&nbsp;}&nbsp;=&nbsp;q&nbsp;=&nbsp;1</pre> </p> <p> It ignored the capacity and instead checked whether <code>q</code> is <code>1</code>. That passed both tests. </p> <h3 id="43fddd9f6ad845aa85b4f04357815659"> Test data API <a href="#43fddd9f6ad845aa85b4f04357815659" title="permalink">#</a> </h3> <p> Before adding another test case, I decided to refactor my test code a bit. When working with a real domain model, you often have to furnish test data in order to make code compile - even if that data isn't relevant to the test. I wanted to demonstrate how to deal with this issue. My first step was to introduce an 'arbitrary' <code>Reservation</code> value in the spirit of <a href="/2017/09/11/test-data-without-builders">Test data without Builders</a>. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;aReservation&nbsp;=&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;1&nbsp;}</pre> </p> <p> This enabled me to rewrite the test: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData(&nbsp;1,&nbsp;&nbsp;<span style="color:blue;">true</span>)&gt;] [&lt;InlineData(13,&nbsp;<span style="color:blue;">false</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Boutique&nbsp;restaurant``&nbsp;quantity&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;canAccept&nbsp;12&nbsp;[]&nbsp;{&nbsp;aReservation&nbsp;<span style="color:blue;">with</span>&nbsp;Quantity&nbsp;=&nbsp;quantity&nbsp;}</pre> </p> <p> This doesn't look like an immediate improvement, but it made it possible to make the <code>Reservation</code> record type more realistic without damage to the test: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Reservation&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;:&nbsp;DateTime &nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;:&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;:&nbsp;string &nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;:&nbsp;int&nbsp;}</pre> </p> <p> I added some fields that a real-world reservation would have. The <code>Quantity</code> field will be useful later on, but the <code>Name</code> and <code>Email</code> fields are irrelevant in the context of the kata. </p> <p> This is the type of API change that often gives people grief. To create a <code>Reservation</code> value, you <em>must</em> supply all four fields. This often adds noise to tests. </p> <p> Not here, though, because the only concession I had to make was to change <code>aReservation</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;aReservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;DateTime&nbsp;(2019,&nbsp;11,&nbsp;29,&nbsp;12,&nbsp;0,&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> The test code remained unaltered. </p> <p> With that in place, I could add the third test case: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData(&nbsp;1,&nbsp;&nbsp;<span style="color:blue;">true</span>)&gt;] [&lt;InlineData(13,&nbsp;<span style="color:blue;">false</span>)&gt;] [&lt;InlineData(12,&nbsp;&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Boutique&nbsp;restaurant``&nbsp;quantity&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;canAccept&nbsp;12&nbsp;[]&nbsp;{&nbsp;aReservation&nbsp;<span style="color:blue;">with</span>&nbsp;Quantity&nbsp;=&nbsp;quantity&nbsp;}</pre> </p> <p> The simplest passing implementation I could think of was: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;_&nbsp;_&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q&nbsp;}&nbsp;=&nbsp;q&nbsp;&lt;&gt;&nbsp;13</pre> </p> <p> This implementation still ignored the restaurant's capacity and simply checked that <code>q</code> was different from <code>13</code>. That was enough to pass all three tests. </p> <h3 id="b25e3ea455db4fc6a0a18ed84d3be679"> Refactor test case code <a href="#b25e3ea455db4fc6a0a18ed84d3be679" title="permalink">#</a> </h3> <p> Adding the next suggested test case proved to be a problem. I wanted to write a single <code>[&lt;Theory&gt;]</code>-driven test function fed by all the <em>Boutique restaurant</em> test data. To do that, I'd have to supply arrays of test input, but unfortunately, <a href="https://github.com/dotnet/fsharp/issues/7916">that wasn't possible in F#</a>. </p> <p> Instead I decided to refactor the test case code to use <code>ClassData</code>-driven test cases. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;BoutiqueTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int,&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;(&nbsp;1,&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(13,&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;<span style="color:blue;">true</span>) [&lt;Theory;&nbsp;ClassData(typeof&lt;BoutiqueTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Boutique&nbsp;restaurant``&nbsp;quantity&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;canAccept&nbsp;12&nbsp;[]&nbsp;{&nbsp;aReservation&nbsp;<span style="color:blue;">with</span>&nbsp;Quantity&nbsp;=&nbsp;quantity&nbsp;}</pre> </p> <p> These are the same test cases as before, but now expressed by a class inheriting from <code>TheoryData&lt;int, bool&gt;</code>. The implementing code remains the same. </p> <h3 id="d1c356e19bd940cd8e37c24848b4f896"> Existing reservation <a href="#d1c356e19bd940cd8e37c24848b4f896" title="permalink">#</a> </h3> <p> The next suggested test case includes an existing reservation. To support that, I changed the test case base class to <code>TheoryData&lt;int, int list, int, bool&gt;</code>, and passed empty lists for the first three test cases. For the new, fourth test case, I supplied a number of seats. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;BoutiqueTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int,&nbsp;int&nbsp;list,&nbsp;int,&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;[],&nbsp;&nbsp;1,&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;[],&nbsp;13,&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;[],&nbsp;12,&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;4,&nbsp;[2],&nbsp;&nbsp;3,&nbsp;<span style="color:blue;">false</span>) [&lt;Theory;&nbsp;ClassData(typeof&lt;BoutiqueTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Boutique&nbsp;restaurant``&nbsp;capacity&nbsp;reservatedSeats&nbsp;quantity&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rs&nbsp;=&nbsp;List.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;aReservation&nbsp;<span style="color:blue;">with</span>&nbsp;Quantity&nbsp;=&nbsp;s&nbsp;})&nbsp;reservatedSeats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;canAccept&nbsp;capacity&nbsp;rs&nbsp;{&nbsp;aReservation&nbsp;<span style="color:blue;">with</span>&nbsp;Quantity&nbsp;=&nbsp;quantity&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> This also forced me to to change the body of the test function. At this stage, it could be prettier, but it got the job done. I soon after improved it. </p> <p> My implementation, as usual, was <em>the simplest thing that could possibly work</em>. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;_&nbsp;reservations&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;q&nbsp;&lt;&gt;&nbsp;13&nbsp;&amp;&amp;&nbsp;Seq.isEmpty&nbsp;reservations</pre> </p> <p> Notice that although the fourth test case varied the <code>capacity</code>, I still managed to pass all tests without looking at it. </p> <h3 id="b08aade19008419cab173c9ab720cfca"> Accept despite existing reservation <a href="#b08aade19008419cab173c9ab720cfca" title="permalink">#</a> </h3> <p> The next test case introduced another existing reservation, but this time with enough capacity to accept a new reservation. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;BoutiqueTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int,&nbsp;int&nbsp;list,&nbsp;int,&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;[],&nbsp;&nbsp;1,&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;[],&nbsp;13,&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;[],&nbsp;12,&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;4,&nbsp;[2],&nbsp;&nbsp;3,&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(10,&nbsp;[2],&nbsp;&nbsp;3,&nbsp;&nbsp;<span style="color:blue;">true</span>)</pre> </p> <p> The test function remained unchanged. </p> <p> In the spirit of the <a href="/2019/10/07/devils-advocate">Devil's advocate technique</a>, I actively sought to avoid a correct implementation. I came up with this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;capacity&nbsp;reservations&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Seq.tryHead&nbsp;reservations&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;q&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> Since all test cases supplied at most one existing reservation, it was enough to consider the first reservation, if present. </p> <p> To many people, it may seem strange to actively seek out incorrect implementations like this. An incorrect implementation that passes all tests does, however, demonstrate the need for more tests. </p> <h3 id="587afbd29a9d41879994d2663c26f8bb"> The sum of all reservations <a href="#587afbd29a9d41879994d2663c26f8bb" title="permalink">#</a> </h3> <p> I then added another test case, this time with three existing reservations: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;BoutiqueTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int,&nbsp;int&nbsp;list,&nbsp;int,&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;&nbsp;1,&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;13,&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;12,&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;4,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2],&nbsp;&nbsp;3,&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2],&nbsp;&nbsp;3,&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(10,&nbsp;[3;2;3],&nbsp;&nbsp;3,&nbsp;<span style="color:blue;">false</span>)</pre> </p> <p> Again, I left the test function untouched. </p> <p> On the side of the implementation, I couldn't think of more hoops to jump through, so I finally gave in and provided a 'proper' implementation: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;capacity&nbsp;reservations&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;q&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> Not only does it look simpler that before, but I also felt that the implementation was warranted. <blockquote> <p> “As the tests get more specific, the code gets more generic.” </p> <footer><cite><a href="https://blog.cleancoder.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html">Robert C. Martin</a></cite></footer> </blockquote> Although I'd only tested <code>canAccept</code> with lists, I decided to implement it with <code>Seq</code>. This was a decision I later regretted. </p> <h3 id="987a4fc4119a4ab29bc968712659bdc2"> Another date <a href="#987a4fc4119a4ab29bc968712659bdc2" title="permalink">#</a> </h3> <p> The last <em>Boutique restaurant</em> test case was to supply an existing reservation on another date. The <code>canAccept</code> function should only consider existing reservations on the date in question. </p> <p> First, I decided to model the two separate dates as two values: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;d1&nbsp;=&nbsp;DateTime&nbsp;(2023,&nbsp;9,&nbsp;14) <span style="color:blue;">let</span>&nbsp;d2&nbsp;=&nbsp;DateTime&nbsp;(2023,&nbsp;9,&nbsp;15)</pre> </p> <p> I hoped that it would make my test cases more readable, because the dates would have a denser representation. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;BoutiqueTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int,&nbsp;(int&nbsp;*&nbsp;DateTime)&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime),&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(&nbsp;1,&nbsp;d1),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(13,&nbsp;d1),&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(12,&nbsp;d1),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;4,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d1)],&nbsp;(&nbsp;3,&nbsp;d1),&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d1)],&nbsp;(&nbsp;3,&nbsp;d1),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(10,&nbsp;[(3,&nbsp;d1);(2,&nbsp;d1);(3,&nbsp;d1)],&nbsp;(&nbsp;3,&nbsp;d1),&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;4,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d2)],&nbsp;(&nbsp;3,&nbsp;d1),&nbsp;&nbsp;<span style="color:blue;">true</span>)</pre> </p> <p> I changed the representation of a reservation from just an <code>int</code> to a tuple of a number and a date. I also got tired of looking at that noisy unit test, so I introduced a test-specific helper function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;reserve&nbsp;(q,&nbsp;d)&nbsp;=&nbsp;{&nbsp;aReservation&nbsp;<span style="color:blue;">with</span>&nbsp;Quantity&nbsp;=&nbsp;q;&nbsp;Date&nbsp;=&nbsp;d&nbsp;}</pre> </p> <p> Since it takes a tuple of a number and a date, I could use it to simplify the test function: </p> <p> <pre>[&lt;Theory;&nbsp;ClassData(typeof&lt;BoutiqueTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Boutique&nbsp;restaurant``&nbsp;(capacity,&nbsp;rs,&nbsp;r,&nbsp;expected)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservations&nbsp;=&nbsp;List.map&nbsp;reserve&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;canAccept&nbsp;capacity&nbsp;reservations&nbsp;(reserve&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> The <code>canAccept</code> function now had to filter the <code>reservations</code> on <code>Date</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;capacity&nbsp;reservations&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q;&nbsp;Date&nbsp;=&nbsp;d&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;relevantReservations&nbsp;=&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Date&nbsp;=&nbsp;d)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;relevantReservations &nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;q&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> This implementation specifically compared dates, though, so while it passed all tests, it'd behave incorrectly if the dates were as much as nanosecond off. That implied that another test case was required. </p> <h3 id="535fc318a9b74ffda89e4e02992efe41"> Same date, different time <a href="#535fc318a9b74ffda89e4e02992efe41" title="permalink">#</a> </h3> <p> The final test case for the <em>Boutique restaurant</em>, then, was to use two <code>DateTime</code> values on the same date, but with different times. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;BoutiqueTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int,&nbsp;(int&nbsp;*&nbsp;DateTime)&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime),&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(&nbsp;1,&nbsp;d1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(13,&nbsp;d1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(12,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(12,&nbsp;d1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;4,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d1)],&nbsp;(&nbsp;3,&nbsp;d1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d1)],&nbsp;(&nbsp;3,&nbsp;d1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(10,&nbsp;[(3,&nbsp;d1);(2,&nbsp;d1);(3,&nbsp;d1)],&nbsp;(&nbsp;3,&nbsp;d1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;4,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d2)],&nbsp;(&nbsp;3,&nbsp;d1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;4,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d1)],&nbsp;(&nbsp;3,&nbsp;d1.AddHours&nbsp;1.),&nbsp;<span style="color:blue;">false</span>)</pre> </p> <p> I just added a new test case as a new line and lined up the data. The test function, again, didn't change. </p> <p> To address the new test case, I generalised the first filter. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;capacity&nbsp;reservations&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q;&nbsp;Date&nbsp;=&nbsp;d&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;relevantReservations&nbsp;=&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Date.Date&nbsp;=&nbsp;d.Date)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;relevantReservations &nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;q&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> An expression like <code>r.Date.Date</code> looks a little odd. <code>DateTime</code> values have a <code>Date</code> property that represents its date part. The first <code>Date</code> is the <code>Reservation</code> field, and the second is the date part. </p> <p> I was now content with the <em>Boutique restaurant</em> implementation. </p> <h3 id="ea07bcad3ac8429194c55edddb7187d7"> Haute cuisine <a href="#ea07bcad3ac8429194c55edddb7187d7" title="permalink">#</a> </h3> <p> In the next phase of the kata, I now had to deal with a configuration of more than one table, so I introduced a type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Table&nbsp;=&nbsp;{&nbsp;Seats&nbsp;:&nbsp;int&nbsp;}</pre> </p> <p> It's really only a glorified wrapper around an <code>int</code>, but with a real domain model in place, I could make its constructor private and instead afford a smart constructor that only accepts positive integers. </p> <p> I changed the <code>canAccept</code> function to take a list of tables, instead of <code>capacity</code>. This also required me to change the existing test function to take a singleton list of tables: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;canAccept&nbsp;[table&nbsp;capacity]&nbsp;reservations&nbsp;(reserve&nbsp;r)</pre> </p> <p> where <code>table</code> is a test-specific helper function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;table&nbsp;s&nbsp;=&nbsp;{&nbsp;Seats&nbsp;=&nbsp;s&nbsp;}</pre> </p> <p> I also added a new test function and a single test case: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;d3&nbsp;=&nbsp;DateTime&nbsp;(2024,&nbsp;6,&nbsp;&nbsp;7) <span style="color:blue;">type</span>&nbsp;HauteCuisineTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime)&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime),&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;([2;2;4;4],&nbsp;[],&nbsp;(4,&nbsp;d3),&nbsp;<span style="color:blue;">true</span>) [&lt;Theory;&nbsp;ClassData(typeof&lt;HauteCuisineTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Haute&nbsp;cuisine``&nbsp;(tableSeats,&nbsp;rs,&nbsp;r,&nbsp;expected)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;tables&nbsp;=&nbsp;List.map&nbsp;table&nbsp;tableSeats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservations&nbsp;=&nbsp;List.map&nbsp;reserve&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;canAccept&nbsp;tables&nbsp;reservations&nbsp;(reserve&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> The change to <code>canAccept</code> is modest: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;tables&nbsp;reservations&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q;&nbsp;Date&nbsp;=&nbsp;d&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capacity&nbsp;=&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;t.Seats)&nbsp;tables &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;relevantReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Date.Date&nbsp;=&nbsp;d.Date)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;relevantReservations &nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;q&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> It still works by looking at a total capacity as if there was just a single communal table. Now it just calculates <code>capacity</code> from the sequence of <code>tables</code>. </p> <h3 id="334eab19b5864f72a92290cfe5031b66"> Reject reservation that doesn't fit largest table <a href="#334eab19b5864f72a92290cfe5031b66" title="permalink">#</a> </h3> <p> Then I added the next test case to the new test function: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;HauteCuisineTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime)&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime),&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;([2;2;4;4],&nbsp;[],&nbsp;(4,&nbsp;d3),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;([2;2;4;4],&nbsp;[],&nbsp;(5,&nbsp;d3),&nbsp;<span style="color:blue;">false</span>)</pre> </p> <p> This one attempts to make a reservation for five people. The largest table only fits four people, so this reservation should be rejected. The current implementation just considered the total capacity of all tables, to it accepted the reservation, and thereby failed the test. </p> <p> This change to <code>canAccept</code> passes all tests: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;tables&nbsp;reservations&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q;&nbsp;Date&nbsp;=&nbsp;d&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capacity&nbsp;=&nbsp;tables&nbsp;|&gt;&nbsp;Seq.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;t.Seats)&nbsp;|&gt;&nbsp;Seq.max &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;relevantReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Date.Date&nbsp;=&nbsp;d.Date)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;relevantReservations &nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;q&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> The function now only considered the largest table in the restaurant. While it's incorrect to ignore all other tables, all tests passed. </p> <h3 id="54e03a3eeb744db3bc82b75aace9556f"> Accept when there's still a remaining table <a href="#54e03a3eeb744db3bc82b75aace9556f" title="permalink">#</a> </h3> <p> Only considering the largest table is obviously wrong, so I added another test case where there's an existing reservation. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;HauteCuisineTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime)&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime),&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;([2;2;4;4],&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(4,&nbsp;d3),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;([2;2;4;4],&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(5,&nbsp;d3),&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;&nbsp;[2;2;4],&nbsp;[(2,&nbsp;d3)],&nbsp;(4,&nbsp;d3),&nbsp;&nbsp;<span style="color:blue;">true</span>)</pre> </p> <p> While <code>canAccept</code> should accept the reservation, it didn't when I added the test case. In a variation of the <a href="/2019/10/07/devils-advocate">Devil's Advocate</a> technique, I came up with this implementation to pass all tests: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;tables&nbsp;reservations&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;q;&nbsp;Date&nbsp;=&nbsp;d&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;largestTable&nbsp;=&nbsp;tables&nbsp;|&gt;&nbsp;Seq.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;t.Seats)&nbsp;|&gt;&nbsp;Seq.max &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capacity&nbsp;=&nbsp;tables&nbsp;|&gt;&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;t.Seats) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;relevantReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Date.Date&nbsp;=&nbsp;d.Date)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;relevantReservations &nbsp;&nbsp;&nbsp;&nbsp;q&nbsp;&lt;=&nbsp;largestTable&nbsp;&amp;&amp;&nbsp;reservedSeats&nbsp;+&nbsp;q&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> This still wasn't the correct implementation. It represented a return to looking at the total capacity of all tables, with the extra rule that you couldn't make a reservation larger than the largest table. At least one more test case was needed. </p> <h3 id="d900c55b4ebc4bcbba572cd8cbca33ed"> Accept when remaining table is available <a href="#d900c55b4ebc4bcbba572cd8cbca33ed" title="permalink">#</a> </h3> <p> I added another test case to the <em>haute cuisine</em> test cases. This one came with one existing reservation for three people, effectively reserving the four-person table. While the remaining tables have an aggregate capacity of four, it's two separate tables. Therefore, a reservation for four people should be rejected. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;HauteCuisineTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;int&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime)&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime),&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;([2;2;4;4],&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(4,&nbsp;d3),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;([2;2;4;4],&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[],&nbsp;(5,&nbsp;d3),&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;&nbsp;[2;2;4],&nbsp;[(2,&nbsp;d3)],&nbsp;(4,&nbsp;d3),&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;(&nbsp;&nbsp;[2;2;4],&nbsp;[(3,&nbsp;d3)],&nbsp;(4,&nbsp;d3),&nbsp;<span style="color:blue;">false</span>)</pre> </p> <p> It then dawned on me that I had to explicitly distinguish between a communal table configuration, and individual tables that aren't communal, regardless of size. This triggered quite a refactoring. </p> <p> I defined a new type to distinguish between these two types of table layout: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;TableConfiguration&nbsp;=&nbsp;Communal&nbsp;<span style="color:blue;">of</span>&nbsp;int&nbsp;|&nbsp;Tables&nbsp;<span style="color:blue;">of</span>&nbsp;Table&nbsp;list</pre> </p> <p> I also had to change the existing test functions, including the <em>boutique restaurant</em> test </p> <p> <pre>[&lt;Theory;&nbsp;ClassData(typeof&lt;BoutiqueTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Boutique&nbsp;restaurant``&nbsp;(capacity,&nbsp;rs,&nbsp;r,&nbsp;expected)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservations&nbsp;=&nbsp;List.map&nbsp;reserve&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;canAccept&nbsp;(Communal&nbsp;capacity)&nbsp;reservations&nbsp;(reserve&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> and the <em>haute cuisine</em> test </p> <p> <pre>[&lt;Theory;&nbsp;ClassData(typeof&lt;HauteCuisineTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Haute&nbsp;cuisine``&nbsp;(tableSeats,&nbsp;rs,&nbsp;r,&nbsp;expected)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;tables&nbsp;=&nbsp;List.map&nbsp;table&nbsp;tableSeats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservations&nbsp;=&nbsp;List.map&nbsp;reserve&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;canAccept&nbsp;(Tables&nbsp;tables)&nbsp;reservations&nbsp;(reserve&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> In both cases I had to change the call to <code>canAccept</code> to pass either a <code>Communal</code> or a <code>Tables</code> value. </p> <h3 id="c3c6acc07a0f4e129fc7159247a7058a"> Delete first <a href="#c3c6acc07a0f4e129fc7159247a7058a" title="permalink">#</a> </h3> <p> I'd previously done the kata in <a href="https://www.haskell.org">Haskell</a> and was able to solve this phase of the kata using the built-in <code>deleteFirstsBy</code> function. This function doesn't exist in the F# core library, so I decided to add it. I created a new module named <code>Seq</code> and first defined a function that deletes the first element that satisfies a predicate: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;bool)&nbsp;-&gt;&nbsp;seq&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;seq&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;deleteFirstBy&nbsp;pred&nbsp;(xs&nbsp;:&nbsp;_&nbsp;seq)&nbsp;=&nbsp;seq&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;found&nbsp;=&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;e&nbsp;=&nbsp;xs.GetEnumerator&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;e.MoveNext&nbsp;()&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;found &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">yield</span>&nbsp;e.Current &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">if</span>&nbsp;pred&nbsp;e.Current &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;found&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">yield</span>&nbsp;e.Current &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> It moves over a sequence of elements and looks for an element that satisfies <code>pred</code>. If such an element is found, it's omitted from the output sequence. The function only deletes the first occurrence from the sequence, so any other elements that satisfy the predicate are still included. </p> <p> This function corresponds to Haskell's <code>deleteBy</code> function and can be used to implement <code>deleteFirstsBy</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;bool)&nbsp;-&gt;&nbsp;seq&lt;&#39;b&gt;&nbsp;-&gt;&nbsp;seq&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;seq&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;deleteFirstsBy&nbsp;pred&nbsp;=&nbsp;Seq.fold&nbsp;(<span style="color:blue;">fun</span>&nbsp;xs&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;deleteFirstBy&nbsp;(pred&nbsp;x)&nbsp;xs)</pre> </p> <p> As <a href="https://hackage.haskell.org/package/base/docs/Data-List.html#v:deleteFirstsBy">the Haskell documentation</a> explains, the "<code>deleteFirstsBy</code> function takes a predicate and two lists and returns the first list with the first occurrence of each element of the second list removed." My F# function does the same, but works on sequences instead of linked lists. </p> <p> I could use it to find and remove tables that were already reserved. </p> <h3 id="f2c9fa5bfc6642378cf0d8edb2a637d5"> Find remaining tables <a href="#f2c9fa5bfc6642378cf0d8edb2a637d5" title="permalink">#</a> </h3> <p> I first defined a little helper function to determine whether a table can accommodate a reservation: </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;Table&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;fits&nbsp;r&nbsp;t&nbsp;=&nbsp;r.Quantity&nbsp;&lt;=&nbsp;t.Seats</pre> </p> <p> The rule is simply that the table's number of <code>Seats</code> must be greater than or equal to the reservation's <code>Quantity</code>. I could use this function for the predicate for <code>Seq.deleteFirstsBy</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;config&nbsp;reservations&nbsp;({&nbsp;Quantity&nbsp;=&nbsp;q;&nbsp;Date&nbsp;=&nbsp;d&nbsp;}&nbsp;<span style="color:blue;">as</span>&nbsp;r)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contemporaneousReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Date.Date&nbsp;=&nbsp;d.Date)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;config&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Communal&nbsp;capacity&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;q&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Tables&nbsp;tables&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rs&nbsp;=&nbsp;Seq.sort&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;remainingTables&nbsp;=&nbsp;Seq.deleteFirstsBy&nbsp;fits&nbsp;(Seq.sort&nbsp;tables)&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.exists&nbsp;(fits&nbsp;r)&nbsp;remainingTables</pre> </p> <p> The <code>canAccept</code> function now branched on <code>Communal</code> versus <code>Tables</code> configurations. In the <code>Communal</code> configuration, it simply compared the <code>reservedSeats</code> and reservation quantity to the communal table's <code>capacity</code>. </p> <p> In the <code>Tables</code> case, the function used <code>Seq.deleteFirstsBy fits</code> to remove all the tables that are already reserved. The result is the <code>remainingTables</code>. If there exists a remaining table that <code>fits</code> the reservation, then the function accepts the reservation. </p> <p> This seemed to me an appropriate implementation of the <em>haute cuisine</em> phase of the kata. </p> <h3 id="5a4a8061a2324ceab96e22a10a5ff445"> Second seatings <a href="#5a4a8061a2324ceab96e22a10a5ff445" title="permalink">#</a> </h3> <p> Now it was time to take seating duration into account. While I could have written my test cases directly against the <a href="https://docs.microsoft.com/dotnet/api/system.timespan">TimeSpan API</a>, I didn't want to write <code>TimeSpan.FromHours 2.5</code>, <code>TimeSpan.FromDays 1.</code>, and so on. I found that it made my test cases harder to read, so I added some literal extensions: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Int32&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;x.hours&nbsp;=&nbsp;TimeSpan.FromHours&nbsp;(float&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;x.days&nbsp;=&nbsp;TimeSpan.FromDays&nbsp;(float&nbsp;x)</pre> </p> <p> This enabled me to write expressions like <code>1 .days</code> and <code>2 .hours</code>, as shown in the first test case: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;d4&nbsp;=&nbsp;DateTime&nbsp;(2023,&nbsp;10,&nbsp;22,&nbsp;18,&nbsp;0,&nbsp;0) <span style="color:blue;">type</span>&nbsp;SecondSeatingsTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;TimeSpan,&nbsp;int&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime)&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime),&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;(2&nbsp;.hours,&nbsp;[2;2;4],&nbsp;[(4,&nbsp;d4)],&nbsp;(3,&nbsp;d4.Add&nbsp;(2&nbsp;.hours)),&nbsp;<span style="color:blue;">true</span>)</pre> </p> <p> I used this initial parametrised test case for a new test function: </p> <p> <pre>[&lt;Theory;&nbsp;ClassData(typeof&lt;SecondSeatingsTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Second&nbsp;seatings``&nbsp;(dur,&nbsp;tableSeats,&nbsp;rs,&nbsp;r,&nbsp;expected)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;tables&nbsp;=&nbsp;List.map&nbsp;table&nbsp;tableSeats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservations&nbsp;=&nbsp;List.map&nbsp;reserve&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;canAccept&nbsp;dur&nbsp;(Tables&nbsp;tables)&nbsp;reservations&nbsp;(reserve&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> My motivation for this test case was mostly to introduce an API change to <code>canAccept</code>. I didn't want to rock the boat too much, so I picked a test case that wouldn't trigger a big change to the implementation. I prefer incremental changes. The only change is the introduction of the <code>seatingDur</code> argument: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;canAccept&nbsp;(seatingDur&nbsp;:&nbsp;TimeSpan)&nbsp;config&nbsp;reservations&nbsp;({&nbsp;Date&nbsp;=&nbsp;d&nbsp;}&nbsp;<span style="color:blue;">as</span>&nbsp;r)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contemporaneousReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Date.Subtract&nbsp;seatingDur&nbsp;&lt;&nbsp;d.Date)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;config&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Communal&nbsp;capacity&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;r.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Tables&nbsp;tables&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rs&nbsp;=&nbsp;Seq.sort&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;remainingTables&nbsp;=&nbsp;Seq.deleteFirstsBy&nbsp;fits&nbsp;(Seq.sort&nbsp;tables)&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.exists&nbsp;(fits&nbsp;r)&nbsp;remainingTables</pre> </p> <p> While the function already considered <code>seatingDur</code>, the way it filtered <code>reservation</code> wasn't entirely correct. It passed all tests, though. </p> <h3 id="2411805a41764ba3b166612ffa392246"> Filter reservations based on seating duration <a href="#2411805a41764ba3b166612ffa392246" title="permalink">#</a> </h3> <p> The next test case I added made me write what I consider the right implementation, but I subsequently decided to add two more test cases just for confidence. Here's all of them: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;SecondSeatingsTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;TimeSpan,&nbsp;int&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime)&nbsp;list,&nbsp;(int&nbsp;*&nbsp;DateTime),&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;.hours, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2;2;4], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(4,&nbsp;d4)], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(3,&nbsp;d4.Add&nbsp;(2&nbsp;.hours)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.5&nbsp;.hours, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2;4;4], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d4);(1,&nbsp;d4.AddMinutes&nbsp;15.);(2,&nbsp;d4.Subtract&nbsp;(15&nbsp;.minutes))], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(3,&nbsp;d4.AddHours&nbsp;2.), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.5&nbsp;.hours, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2;4;4], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d4);(2,&nbsp;d4.Subtract&nbsp;(15&nbsp;.minutes))], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(3,&nbsp;d4.AddHours&nbsp;2.), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.5&nbsp;.hours, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2;4;4], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(2,&nbsp;d4);(1,&nbsp;d4.AddMinutes&nbsp;15.);(2,&nbsp;d4.Subtract&nbsp;(15&nbsp;.minutes))], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(3,&nbsp;d4.AddHours&nbsp;2.25), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>)</pre> </p> <p> The new test cases use some more literal extensions: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Int32&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;x.minutes&nbsp;=&nbsp;TimeSpan.FromMinutes&nbsp;(float&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;x.hours&nbsp;=&nbsp;TimeSpan.FromHours&nbsp;(float&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;x.days&nbsp;=&nbsp;TimeSpan.FromDays&nbsp;(float&nbsp;x) <span style="color:blue;">type</span>&nbsp;Double&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;x.hours&nbsp;=&nbsp;TimeSpan.FromHours&nbsp;x</pre> </p> <p> I added a private <code>isContemporaneous</code> function to the code base and used it to filter the reservation to pass the tests: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;isContemporaneous &nbsp;&nbsp;&nbsp;&nbsp;(seatingDur&nbsp;:&nbsp;TimeSpan) &nbsp;&nbsp;&nbsp;&nbsp;(candidate&nbsp;:&nbsp;Reservation) &nbsp;&nbsp;&nbsp;&nbsp;(existing&nbsp;:&nbsp;Reservation)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;aSeatingBefore&nbsp;=&nbsp;candidate.Date.Subtract&nbsp;seatingDur &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;aSeatingAfter&nbsp;=&nbsp;candidate.Date.Add&nbsp;seatingDur &nbsp;&nbsp;&nbsp;&nbsp;aSeatingBefore&nbsp;&lt;&nbsp;existing.Date&nbsp;&amp;&amp;&nbsp;existing.Date&nbsp;&lt;&nbsp;aSeatingAfter <span style="color:blue;">let</span>&nbsp;canAccept&nbsp;(seatingDur&nbsp;:&nbsp;TimeSpan)&nbsp;config&nbsp;reservations&nbsp;r&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contemporaneousReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.filter&nbsp;(isContemporaneous&nbsp;seatingDur&nbsp;r)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;config&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Communal&nbsp;capacity&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;r.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Tables&nbsp;tables&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rs&nbsp;=&nbsp;Seq.sort&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;remainingTables&nbsp;=&nbsp;Seq.deleteFirstsBy&nbsp;fits&nbsp;(Seq.sort&nbsp;tables)&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.exists&nbsp;(fits&nbsp;r)&nbsp;remainingTables</pre> </p> <p> I could have left the functionality of <code>isContemporaneous</code> inside of <code>canAccept</code>, but I found it just hard enough to get my head around that I preferred to put it in a named helper function. Checking that a value is in a range is in itself trivial, but for some reason, figuring out the limits of the range didn't come naturally to me. </p> <p> This version of <code>canAccept</code> only considered existing reservations if they in any way overlapped with the reservation in question. It passed all tests. It also seemed to me to be a satisfactory implementation of the <em>second seatings</em> scenario. </p> <h3 id="7b7ede53fafc4a25b6bbb3fe9bbb4f09"> Alternative table configurations <a href="#7b7ede53fafc4a25b6bbb3fe9bbb4f09" title="permalink">#</a> </h3> <p> This state of the kata introduces groups of tables that can be reserved individually, or combined. To support that, I changed the definition of <code>Table</code>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Table&nbsp;=&nbsp;Discrete&nbsp;<span style="color:blue;">of</span>&nbsp;int&nbsp;|&nbsp;Group&nbsp;<span style="color:blue;">of</span>&nbsp;int&nbsp;list</pre> </p> <p> A <code>Table</code> is now either a <code>Discrete</code> table that can't be combined, or a <code>Group</code> of tables that can either be reserved individually, or combined. </p> <p> I had to change the test-specific <code>table</code> function to behave like before. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;table&nbsp;s&nbsp;=&nbsp;Discrete&nbsp;s</pre> </p> <p> Before this change to the <code>Table</code> type, all tables were implicitly <code>Discrete</code> tables. </p> <p> This enabled me to add the first test case: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;AlternativeTableConfigurationTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;Table&nbsp;list,&nbsp;int&nbsp;list,&nbsp;int,&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>) [&lt;Theory;&nbsp;ClassData(typeof&lt;AlternativeTableConfigurationTestCases&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Alternative&nbsp;table&nbsp;configurations``&nbsp;(tables,&nbsp;rs,&nbsp;r,&nbsp;expected)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;res&nbsp;i&nbsp;=&nbsp;reserve&nbsp;(i,&nbsp;d4) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservations&nbsp;=&nbsp;List.map&nbsp;res&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;canAccept&nbsp;(1&nbsp;.days)&nbsp;(Tables&nbsp;tables)&nbsp;reservations&nbsp;(res&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> Like I did when I introduced the <code>seatingDur</code> argument, I deliberately chose a test case that didn't otherwise rock the boat too much. The same was the case now, so the only other change I had to make to pass all tests was to adjust the <code>fits</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;Table&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;fits&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Discrete&nbsp;seats&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity&nbsp;&lt;=&nbsp;seats &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Group&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span></pre> </p> <p> It's clearly not correct to return <code>true</code> for any <code>Group</code>, but it passed all tests. </p> <h3 id="413588fbf7524177a24171c3a25f4fe7"> Accept based on sum of table group <a href="#413588fbf7524177a24171c3a25f4fe7" title="permalink">#</a> </h3> <p> I wanted to edge a little closer to correctly handling the <code>Group</code> case, so I added a test case: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;AlternativeTableConfigurationTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;Table&nbsp;list,&nbsp;int&nbsp;list,&nbsp;int,&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">false</span>)</pre> </p> <p> A restaurant with this table configuration can't accept a reservation for seven people, but because <code>fits</code> returned <code>true</code> for any <code>Group</code>, <code>canAccept</code> would return <code>true</code>. Since the test expected the result to be <code>false</code>, this caused the test to fail. </p> <p> Edging closer to correct behaviour, I adjusted <code>fits</code> again: </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;Table&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;fits&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Discrete&nbsp;seats&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity&nbsp;&lt;=&nbsp;seats &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Group&nbsp;tables&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity&nbsp;&lt;=&nbsp;List.sum&nbsp;tables</pre> </p> <p> This was still not correct, because it removed an entire group of tables when <code>fits</code> returned <code>true</code>, but it passed all tests so far. </p> <h3 id="86a27564f5b846ef8dc3afdeadd613a0"> Accept reservation by combining two tables <a href="#86a27564f5b846ef8dc3afdeadd613a0" title="permalink">#</a> </h3> <p> I added another failing test: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;AlternativeTableConfigurationTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;Table&nbsp;list,&nbsp;int&nbsp;list,&nbsp;int,&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2;1], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>)</pre> </p> <p> The last test case failed because the existing reservations should only have reserved one of the tables in the group, but because of the way <code>fits</code> worked, the entire group was deleted by <code>Seq.deleteFirstsBy fits</code>. This made <code>canAccept</code> reject the four-person reservation. </p> <p> To be honest, this step was difficult for me. I should probably have found out how to make a smaller step. </p> <p> I wanted a function that would compare a <code>Reservation</code> to a <code>Table</code>, but unlike <code>Fits</code> return <code>None</code> if it decided to 'use' the table, or a <code>Some</code> value if it decided that it didn't need to use the entire table. This would enable me to pick only some of the tables from a <code>Group</code>, but still return a <code>Some</code> value with the rest of tables. </p> <p> I couldn't figure out an elegant way to do this with the existing <code>Seq</code> functionality, so I started to play around with something more specific. The implementation came accidentally as I was struggling to come up with something more general. As I was experimenting, all of a sudden, all tests passed! </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;Table&nbsp;-&gt;&nbsp;Table&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;allot&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Discrete&nbsp;seats&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;r.Quantity&nbsp;&lt;=&nbsp;seats &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;Some&nbsp;(Discrete&nbsp;seats) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Group&nbsp;tables&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Some&nbsp;(Group&nbsp;tables) <span style="color:green;">//&nbsp;seq&lt;Table&gt;&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;seq&lt;Table&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;allocate&nbsp;(tables&nbsp;:&nbsp;Table&nbsp;seq)&nbsp;r&nbsp;=&nbsp;seq&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;found&nbsp;=&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;e&nbsp;=&nbsp;tables.GetEnumerator&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;e.MoveNext&nbsp;()&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;found &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">yield</span>&nbsp;e.Current &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;allot&nbsp;r&nbsp;e.Current&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;found&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">yield</span>&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;} <span style="color:blue;">let</span>&nbsp;canAccept&nbsp;(seatingDur&nbsp;:&nbsp;TimeSpan)&nbsp;config&nbsp;reservations&nbsp;r&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contemporaneousReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.filter&nbsp;(isContemporaneous&nbsp;seatingDur&nbsp;r)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;config&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Communal&nbsp;capacity&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;r.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Tables&nbsp;tables&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rs&nbsp;=&nbsp;Seq.sort&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;remainingTables&nbsp;=&nbsp;Seq.fold&nbsp;allocate&nbsp;(Seq.sort&nbsp;tables)&nbsp;rs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.exists&nbsp;(fits&nbsp;r)&nbsp;remainingTables</pre> </p> <p> I wasn't too happy with the implementation, which I found (and still find) too complicated. This was, however, the first time I've done this part of the kata (in any language), so I wasn't sure where this was going. </p> <p> The <code>allocate</code> function finds and allocates one of its input tables to a reservation. It does that by <em>not</em> yielding the first table it finds that can accommodate the reservation. Don't hurt your head too much with the code in this version, because there's plenty of cases that it incorrectly handles. It's full of bugs. Still, it passed all tests. </p> <h3 id="34ef030a71e24dfabc3bd2ff0d3bdf4c"> Reject when group has been reduced <a href="#34ef030a71e24dfabc3bd2ff0d3bdf4c" title="permalink">#</a> </h3> <p> The implementation was wrong because the <code>allot</code> function would just keep returning a <code>Group</code> without consuming it. This would imply that <code>canAccept</code> would use it more than once, which was wrong, so I added a test case: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;AlternativeTableConfigurationTestCases&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;Table&nbsp;list,&nbsp;int&nbsp;list,&nbsp;int,&nbsp;bool&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2;1], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Discrete&nbsp;4;&nbsp;Discrete&nbsp;1;&nbsp;Discrete&nbsp;2;&nbsp;Group&nbsp;[2;2;2]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[3;1;2;1;4], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">false</span>)</pre> </p> <p> Given the existing reservations, this restaurant is effectively sold out that day. All the <code>Discrete</code> tables are reserved, and the last two reservations for one and four effectively consumes the <code>Group</code>. The latest test case expected <code>canAccept</code> to return <code>false</code>, but it returned <code>true</code>. Since I was following test-driven development, I expected that. </p> <h3 id="af5554a2e5b9430cb6ec5d1aaa10ec44"> Consume <a href="#af5554a2e5b9430cb6ec5d1aaa10ec44" title="permalink">#</a> </h3> <p> I needed a function that would consume from a <code>Group</code> of tables and return the remaining tables from that group; that is, the tables not consumed. I've already <a href="/2019/12/16/zone-of-ceremony">discussed this function in a different context</a>. </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;seq&lt;int&gt;&nbsp;-&gt;&nbsp;seq&lt;int&gt;</span> <span style="color:blue;">let</span>&nbsp;consume&nbsp;quantity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;go&nbsp;(acc,&nbsp;xs)&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;quantity&nbsp;&lt;=&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;(acc,&nbsp;Seq.append&nbsp;xs&nbsp;(Seq.singleton&nbsp;x)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;(acc&nbsp;+&nbsp;x,&nbsp;xs) &nbsp;&nbsp;&nbsp;&nbsp;Seq.fold&nbsp;go&nbsp;(0,&nbsp;Seq.empty)&nbsp;&gt;&gt;&nbsp;snd</pre> </p> <p> I put this function in my own <code>Seq</code> module. It consumes values from the left until the sum is greater than or equal to the desired <code>quantity</code>. It then returns the rest of the sequence: </p> <p> <pre>&gt; consume 1 [1;2;3];; val it : seq&lt;int&gt; = seq [2; 3] &gt; consume 2 [1;2;3];; val it : seq&lt;int&gt; = seq [3] &gt; consume 3 [1;2;3];; val it : seq&lt;int&gt; = seq [3] &gt; consume 4 [1;2;3];; val it : seq&lt;int&gt; = seq []</pre> </p> <p> The first example consumes only the leading <code>1</code>, while both the second and the third example consumes both <code>1</code> and <code>2</code> because the sum of those values is <code>3</code>, and the requested quantity is <code>2</code> and <code>3</code>, respectively. The fourth example consumes all elements because the requested quantity is <code>4</code>, and you need both <code>1</code>, <code>2</code>, and <code>3</code> before the sum is large enough. You have to pick strictly from the left, so you can't decide to just take the elements <code>1</code> and <code>3</code>. </p> <h3 id="b9e29990b0fd4a5ab5e7c8812be6b07b"> Consuming tables from a group <a href="#b9e29990b0fd4a5ab5e7c8812be6b07b" title="permalink">#</a> </h3> <p> I could now use my <code>Seq.consume</code> function to improve <code>allot</code>: </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;Table&nbsp;-&gt;&nbsp;Table&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;allot&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Discrete&nbsp;seats&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;r.Quantity&nbsp;&lt;=&nbsp;seats &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;Some&nbsp;(Discrete&nbsp;seats) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Group&nbsp;tables&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tables&nbsp;|&gt;&nbsp;Seq.consume&nbsp;r.Quantity&nbsp;|&gt;&nbsp;Seq.toList&nbsp;|&gt;&nbsp;Group&nbsp;|&gt;&nbsp;Some</pre> </p> <p> It handles the <code>Group</code> case by consuming the reservation <code>Quantity</code> and then returning a <code>Some Group</code> with the remaining tables. </p> <p> It also turned out that sorting the reservations wasn't appropriate, mainly because it's not entirely clear how to sort a list with elements of a discriminated union. My final implementation of <code>canAccept</code> was this: </p> <p> <pre><span style="color:green;">//&nbsp;TimeSpan&nbsp;-&gt;&nbsp;TableConfiguration&nbsp;-&gt;&nbsp;seq&lt;Reservation&gt;&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;canAccept&nbsp;seatingDur&nbsp;config&nbsp;reservations&nbsp;r&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;contemporaneousReservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.filter&nbsp;(isContemporaneous&nbsp;seatingDur&nbsp;r)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;config&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Communal&nbsp;capacity&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;r.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Tables&nbsp;tables&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;remainingTables&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.fold&nbsp;allocate&nbsp;(Seq.ofList&nbsp;tables)&nbsp;contemporaneousReservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seq.exists&nbsp;(fits&nbsp;r)&nbsp;remainingTables</pre> </p> <p> Nothing much has changed - only that neither reservations nor tables are now sorted. It passes all tests. </p> <h3 id="fcba96687bac4cb3b35cbaef1b8232db"> Retrospective <a href="#fcba96687bac4cb3b35cbaef1b8232db" title="permalink">#</a> </h3> <p> I must admit that I ran out of steam towards the end. It's possible that there's some edge cases I didn't think of. I'd probably feel more confident of the final implementation if I'd used property-based testing instead of <a href="https://twitter.com/tastapod/status/1157996947581652993">Example-Guided Development</a>. </p> <p> I also took some unfortunate turns along the way. Early in the kata, I could easily implement <code>canAccept</code> with functionality from the <code>Seq</code> module. This meant that the function could take a <code>seq&lt;Reservation&gt;</code> as an input argument. I'm always inclined to follow <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> and be liberal with what I accept. I thought that being able to accept any <code>seq&lt;Reservation&gt;</code> was a good design decision. It might have been if I'd been publishing a reusable library, but it made things more awkward. </p> <p> I'm also not sure that I chose to model the table layouts in the best way. For example, I currently can't handle a scenario with both bar seating and individual tables. I think I should have made <code>Communal</code> a case of <code>Table</code>. This would have enabled me to model layouts with several communal tables combined with discrete tables, and even groups of tables. </p> <p> In general, my solution seems too complicated, but I don't see an easy fix. Often, if I work some more with the problem, insight arrives. It usually arrives when you least need it, so I thought it better to let the problem rest for a while. I can always return to it when I feel that I have a fresh approach. </p> <h3 id="d19eccbe58924d8a96a1acab89f9bbaa"> Summary <a href="#d19eccbe58924d8a96a1acab89f9bbaa" title="permalink">#</a> </h3> <p> This article walks you through my first F# attempt at the <em>Maître d' kata</em>. The repository is available on GitHub. </p> <p> I'm not entirely happy with how it turned out, but I don't consider it an utter failure either. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="61ab9d61eb6346d39b4fcf94d094a6ef"> <div class="comment-author">Ghillie Dhu <a href="#61ab9d61eb6346d39b4fcf94d094a6ef">#</a></div> <div class="comment-content"> <p> You could leverage library functions and avoid mutability like so: <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;bool)&nbsp;-&gt;&nbsp;seq&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;seq&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;deleteFirstBy&nbsp;pred&nbsp;(xs&nbsp;:&nbsp;_&nbsp;seq)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Seq.tryFindIndex pred xs&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-></span>&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;n&nbsp;<span style="color:blue;">-></span>&nbsp;Seq.concat&nbsp;(Seq.take&nbsp;(n-1)&nbsp;xs)&nbsp;(Seq.skip&nbsp;n&nbsp;xs)</pre> </p> </div> <div class="comment-date">2020-04-28 19:02 UTC</div> </div> <div class="comment" id="6fe45d2631bf410285b5403b1da737df"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6fe45d2631bf410285b5403b1da737df">#</a></div> <div class="comment-content"> <p> Ghillie, thank you for writing. It looks like you're on the right track, but I think you have to write the function like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;deleteFirstBy&nbsp;pred&nbsp;(xs&nbsp;:&nbsp;_&nbsp;seq)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Seq.tryFindIndex&nbsp;pred&nbsp;xs&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;n&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Seq.append&nbsp;(Seq.take&nbsp;(n-1)&nbsp;xs)&nbsp;(Seq.skip&nbsp;n&nbsp;xs)</pre> </p> </div> <div class="comment-date">2020-04-28 19:21 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Unit bias against collections https://blog.ploeh.dk/2020/04/20/unit-bias-against-collections 2020-04-20T06:27:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you get the value out of a collection? Mu. Which value?</em> </p> <p> The other day I was looking for documentation on how to write automated tests with a self-hosted ASP.NET Core 3 web application. I've <a href="/outside-in-tdd">done this numerous times with previous versions of the framework</a>, but ASP.NET Core 3 is new, so I wanted to learn how I'm supposed to do it this year. I found <a href="https://docs.microsoft.com/aspnet/core/test/integration-tests">official documentation</a> that helped me figure it out. </p> <p> One of the code examples in that article displays a motif that I keep encountering. It displays behaviour close enough to <em>unit bias</em> that I consider it reasonable to use that label. Unit bias is the cognitive tendency to consider a <em>unit</em> of something the preferred amount. Our brains don't like fractions, and they don't like multiples either. </p> <h3 id="c9468b526bcb444a99680d8a6a5deb29"> Unit bias in action <a href="#c9468b526bcb444a99680d8a6a5deb29" title="permalink">#</a> </h3> <p> The sample code in the ASP.NET Core documentation differs in the type of dependency it looks for, but is otherwise identical to this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">descriptor</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span>.<span style="font-weight:bold;color:#74531f;">SingleOrDefault</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>.ServiceType&nbsp;<span style="font-weight:bold;color:#74531f;">==</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IReservationsRepository</span>)); <span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">descriptor</span>&nbsp;!=&nbsp;<span style="color:blue;">null</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span>.<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="font-weight:bold;color:#1f377f;">descriptor</span>); }</pre> </p> <p> The goal is to enable an automated integration test to <a href="/2019/04/01/an-example-of-state-based-testing-in-c">run against a Fake database</a> instead of an actual relational database. My production <a href="/2011/07/28/CompositionRoot">Composition Root</a> registers an implementation of <code>IReservationsRepository</code> that communicates with an actual database. In an automated integration test, I'd like to unregister the existing dependency and replace it with <a href="http://xunitpatterns.com/Fake%20Object.html">a Fake</a>. Here's the code in context: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">RestaurantApiFactory</span>&nbsp;:&nbsp;<span style="color:#2b91af;">WebApplicationFactory</span>&lt;<span style="color:#2b91af;">Startup</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ConfigureWebHost</span>(<span style="color:#2b91af;">IWebHostBuilder</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">builder</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">builder</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>.<span style="font-weight:bold;color:#74531f;">ConfigureServices</span>(<span style="font-weight:bold;color:#1f377f;">services</span>&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">descriptor</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span>.<span style="font-weight:bold;color:#74531f;">SingleOrDefault</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>.ServiceType&nbsp;<span style="font-weight:bold;color:#74531f;">==</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IReservationsRepository</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">descriptor</span>&nbsp;!=&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span>.<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="font-weight:bold;color:#1f377f;">descriptor</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span>.<span style="font-weight:bold;color:#74531f;">AddSingleton</span>&lt;<span style="color:#2b91af;">IReservationsRepository</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeDatabase</span>()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> It works as intended, so what's the problem? </p> <h3 id="d84651bde63944e9bb04873f51a58085"> How do I get the value out of my collection? <a href="#d84651bde63944e9bb04873f51a58085" title="permalink">#</a> </h3> <p> The problem is that it's fragile. What happens if there's more than one registration of <code>IReservationsRepository</code>? </p> <p> This happens: </p> <p> <pre>System.InvalidOperationException : Sequence contains more than one matching element</pre> </p> <p> This is a completely avoidable error, stemming from unit bias. </p> <p> A large proportion of programmers I meet seem to be fundamentally uncomfortable with thinking in multitudes. They subconsciously prefer thinking in procedures and algorithms that work on a single object. The programmer who wrote the above call to <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.singleordefault">SingleOrDefault</a> exhibits behaviour putting him or her in that category. </p> <p> This is nothing but a specific instantiation of a more general programmer bias: <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">How do I get the value out of the monad?</a> </p> <p> As usual, the answer is <a href="https://en.wikipedia.org/wiki/Mu_(negative)">mu</a>. You don't. The question borders on the nonsensical. <em>How do I get the value out of my collection?</em> Which value? The first? The last? Some arbitrary value at an unknown position? Which value do you want if the collection is empty? Which value do you want if there's more than one that fits a predicate? </p> <p> If you can answer such questions, you <em>can</em> get 'the' value out of a collection, but often, you can't. In the current example, the code doesn't handle multiple <code>IReservationsRepository</code> registrations. </p> <p> It easily could, though. </p> <h3 id="568f351c446f4e33a158eccdfbb461e6"> Inject the behaviour into the collection <a href="#568f351c446f4e33a158eccdfbb461e6" title="permalink">#</a> </h3> <p> The best answer to the question of <em>how to get the value out of the monad</em> (in this case, <em>the collection</em>) is that you don't. Instead, you inject the desired behaviour into it. </p> <p> In this case, the desired behaviour is to remove a <code>descriptor</code>. The monad in question is the collection of <code>services</code>. What does that mean in practice? </p> <p> A first attempt might be something like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">descriptors</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span> &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Where</span>(<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>.ServiceType&nbsp;<span style="font-weight:bold;color:#74531f;">==</span>&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IReservationsRepository</span>)); <span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">descriptor</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">descriptors</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span>.<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="font-weight:bold;color:#1f377f;">descriptor</span>);</pre> </p> <p> Unfortunately, this doesn't quite work: </p> <p> <pre>System.InvalidOperationException : Collection was modified; enumeration operation may not execute.</pre> </p> <p> This happens because <code>descriptors</code> is a lazily evaluated <a href="https://en.wikipedia.org/wiki/Iterator_pattern">Iterator</a> over <code>services</code>, and you're not allowed to remove elements from a collection while you enumerate it. It could lead to bugs if you could. </p> <p> That problem is easily solved. Just copy the selected <code>descriptors</code> to an array or list: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">descriptors</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span> &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Where</span>(<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>.ServiceType&nbsp;<span style="font-weight:bold;color:#74531f;">==</span>&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IReservationsRepository</span>)) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">ToList</span>(); <span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">descriptor</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">descriptors</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span>.<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="font-weight:bold;color:#1f377f;">descriptor</span>);</pre> </p> <p> This achieves the desired outcome regardless of the number of matches to the predicate. This is a more robust solution, and it requires the same amount of code. </p> <p> You can stop there, since the code now works, but if you truly want to <em>inject the behaviour into the collection</em>, you're not quite done yet. </p> <p> But you're close. All you have to do is this: </p> <p> <pre><span style="font-weight:bold;color:#1f377f;">services</span> &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Where</span>(<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>.ServiceType&nbsp;<span style="font-weight:bold;color:#74531f;">==</span>&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IReservationsRepository</span>)) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">ToList</span>() &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">ForEach</span>(<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">services</span>.<span style="font-weight:bold;color:#74531f;">Remove</span>(<span style="font-weight:bold;color:#1f377f;">d</span>));</pre> </p> <p> Notice how this statement never produces an output. Instead, you 'inject' the call to <code>services.Remove</code> into the list, using the <a href="https://docs.microsoft.com/dotnet/api/system.collections.generic.list-1.foreach">ForEach</a> method, which then mutates the <code>services</code> collection. </p> <p> Whether you prefer the version that uses the <code>foreach</code> keyword or the version that uses <code>List&lt;T&gt;.ForEach</code> doesn't matter. What matters is that you don't use the <a href="https://en.wikipedia.org/wiki/Partial_function">partial</a> <code>SingleOrDefault</code> function. </p> <h3 id="cd80281860ed4b8299278cdaa2e65709"> Conclusion <a href="#cd80281860ed4b8299278cdaa2e65709" title="permalink">#</a> </h3> <p> It's a common code smell when programmers try to extract a single value from a collection. Sometimes it's appropriate, but there are several edge cases you should be prepared to address. What should happen if the collection is empty? What should happen if the collection contains many elements? What should happen if the collection is infinite? (I didn't address that in this article.) </p> <p> You can often both simplify your code and make it more robust by staying 'in' the collection, so to speak. Let the desired behaviour apply to all appropriate elements of the collection. </p> <p> Don't be biased against collections. </p> </div> <hr> <div id="comments"> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e6675033a3a9dc8a21c64650dff91b8432a9a151"> <div class="comment-author">Julius H <a href="#e6675033a3a9dc8a21c64650dff91b8432a9a151">#</a></div> <div class="comment-content"> <p>I concur that often the first element of a collection is picked without thinking. Anecdotally, I experienced a system that would not work if set up freshly because in some places there was no consideration for empty collections. (The testing environment always contained some data)</p> <p> Yet I would reverse the last change (towards <code>.ForEach</code>). For one, because (to my biased eye) it <i>looks</i> side effect free but isn't. And then it does add value compared to a forech loop, also both solutions are needlessy inefficient. If you want to avoid the foreach, go for the <code>RemoveAll()</code> method (also present on List&lt;T&gt;): </p> <pre> services.<span style="font-weight:bold;color:#74531f;">RemoveAll</span>&lt;<span style="color:#2b91af;">IReservationsRepository</span>&gt;(); </pre> <p> </div> <div class="comment-date">2020-04-29 8:52 UTC</div> </div> <div class="comment" id="99baee4e923b471891eef04b499b64a1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#99baee4e923b471891eef04b499b64a1">#</a></div> <div class="comment-content"> <p> Julius, thank you for writing. Yes, I agree that in C# it's more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> to use <code>foreach</code>. </p> <p> How would using <code>RemoveAll</code> work? Isn't that going to remove the entries from the <code>List</code> instead of from <code>services</code>? </p> </div> <div class="comment-date">2020-04-29 10:49 UTC</div> </div> <div class="comment" id="17b7d3a21cff5b6585d6d2fa0f63f1539f9bce3e"> <div class="comment-author">Julius H <a href="#17b7d3a21cff5b6585d6d2fa0f63f1539f9bce3e">#</a></div> <div class="comment-content"> <p> Hi Mark,<br>As you&quot;re calling <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.extensions.servicecollectiondescriptorextensions.removeall?view=dotnet-plat-ext-3.1"><code>IServiceCollection.RemoveAll()</code></a>, it will remove it from the collection. I tried it, and to me it seems to be working. (As long as you are not copying the services into a list beforehand) </p> <p> But to your main point, I remember when I wrote <code>.Single()</code> once, and years later I got a bug report because of it. I see two approaches there: Fail as fast and hard as possible or check just as much as needed for the moment. Considering TDD in the former approach, one would need to write a lot of test code for scenarios, that should never happen to verify the exceptions happen. Still, in the latter approach, a subtle bug could stay in the system for quite some time... What do you prefer? </p> </div> <div class="comment-date">2020-05-01 18:07 UTC</div> </div> <div class="comment" id="3eed1b53957e4abf8b92c83c15e63ec6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3eed1b53957e4abf8b92c83c15e63ec6">#</a></div> <div class="comment-content"> <p> Julius, thank you for writing. It turns out that <code>RemoveAll</code> are <a href="https://docs.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.extensions.servicecollectiondescriptorextensions.removeall">a couple of extension methods on <code>IServiceCollection</code></a>. One has to import <code>Microsoft.Extensions.DependencyInjection.Extensions</code> with a <code>using</code> directive before one can use them. I didn't know about these methods, but I agree that they seem to do their job. Thank you for the tip. </p> <p> As for your question, my preference is for robustness. In my experience, there's rarely such a thing as a scenario that never happens. If the code allows something to happen, it'll likely happen sooner or later. Code changes, so even if you've analysed that some combination of input isn't possible today, a colleague may change the situation tomorrow. It pays to write that extra unit test or two to make sure that encapsulation isn't broken. </p> <p> This is also one of the reasons I'm fond of <a href="/property-based-testing-intro">property-based testing</a>. You automatically get coverage of all sorts of boundary conditions you'd normally not think about testing. </p> </div> <div class="comment-date">2020-05-02 9:12 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Curb code rot with thresholds https://blog.ploeh.dk/2020/04/13/curb-code-rot-with-thresholds 2020-04-13T08:43:00+00:00 Mark Seemann <div id="post"> <p> <em>Code bases deteriorate unless you actively prevent it. Institute some limits that encourage developers to clean up.</em> </p> <p> From time to time I manage to draw the ire of people, with articles such as <a href="/2019/11/04/the-80-24-rule">The 80/24 rule</a> or <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">Put cyclomatic complexity to good use</a>. I can understand why. These articles suggest specific constraints to which people should consider consenting. <em>Don't write code wider than 80 characters. Don't write code with a <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> higher than 7</em>. </p> <p> It makes people uncomfortable. </p> <h3 id="6c5e4b0817f742bf9ecc0a9050ecb7b5"> Sophistication <a href="#6c5e4b0817f742bf9ecc0a9050ecb7b5" title="permalink">#</a> </h3> <p> I hope that regular readers understand that I'm a more sophisticated thinker than some of my texts may suggest. I deliberately simplify my points. </p> <p> I do this to make the text more readable. I also aspire to present sufficient arguments, and enough context, that a charitable reader will understand that everything I write should be taken as food for thought rather than gospel. </p> <p> Consider a sentence like the above: <em>I deliberately simplify my points</em>. That sentence, in itself, is an example of deliberate simplification. In reality, I don't <em>always</em> simplify my points. Perhaps sometimes I simplify, but it's not <em>deliberate</em>. I could have written: <em>I often deliberately simplify some of my points</em>. Notice the extra <a href="https://en.wikipedia.org/wiki/Hedge_(linguistics)">hedge words</a>. Imagine an entire text written like that. It would be less readable. </p> <p> I could hedge my words when I write articles, but I don't. I believe that a text that states its points as clearly as possible is easier to understand for any careful reader. I also believe that hedging my language will not prevent casual readers from misunderstanding what I had in mind. </p> <h3 id="a5da32d0580d4a0f8c19069c57c2a335"> Archetypes <a href="#a5da32d0580d4a0f8c19069c57c2a335" title="permalink">#</a> </h3> <p> Why do I suggest hard limits on line width, cyclomatic complexity, and so on? </p> <p> In light of the above, realise that the limits I offer are suggestions. A number like 80 characters isn't a hard limit. It's a representation of an idea; a token. The same is true for <a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two">the magic number seven, plus or minus two</a>. That too, represents an idea - the idea that human short-term memory is limited, and that this impacts our ability to read and understand code. </p> <p> The number seven serves as an archetype. It's a proxy for a more complex idea. It's a simplification that, hopefully, makes it easier to follow the plot. </p> <p> <em>Each method should have a maximum cyclomatic complexity of seven.</em> That's easier to understand than <em>each method should have a maximum cyclomatic complexity small enough that it fits within the cognitive limits of the human brain's short-term memory</em>. </p> <p> I've noticed that a subset of the developer population is quite literal-minded. If I declare: <em>don't write code wider than 80 characters</em> they're happy if they agree, and infuriated if they don't. </p> <p> If you've been paying attention, you now understand that this isn't about the number <em>80</em>, or <em>24</em>, or <em>7</em>. It's about instituting useful quantitative guidance. The actual number is less important. </p> <p> I have reasons to prefer those specific values. I've already motivated them in previous articles. I'm not, though, obdurately attached to those particular numbers. I'd rather work with a team that has agreed to a 120-character maximum width than with a team that follows no policy. </p> <h3 id="8b7d0a456f6f4bdca9ea2814a67144fc"> How code rots <a href="#8b7d0a456f6f4bdca9ea2814a67144fc" title="permalink">#</a> </h3> <p> No-one deliberately decides to write legacy code. Code bases gradually deteriorate. </p> <p> Here's another deliberate simplification: code gradually becomes more complicated because each change seems small, and no-one pays attention to the overall quality. It doesn't happen overnight, but one day you realise that you've developed a legacy code base. When that happens, it's too late to do anything about it. </p> <p> <img src="/content/binary/gradual-increase-of-complexity.png" alt="Line chart showing increasing complexity as time passes."> </p> <p> At the beginning, a method has low complexity, but as you fix defects and add features, the complexity increases. If you don't pay attention to cyclomatic complexity, you pass <em>7</em> without noticing it. You pass <em>10</em> without noticing it. You pass <em>15</em> and <em>20</em> without noticing it. </p> <p> One day you discover that you have a problem - not because you finally look at a metric, but because the code has now become so complicated that everyone notices. Alas, now it's too late to do anything about it. </p> <p> <a href="https://en.wikipedia.org/wiki/Software_rot">Code rot</a> sets in a little at a time; it works like <a href="https://en.wikipedia.org/wiki/Boiling_frog">boiling the proverbial frog</a>. </p> <h3 id="4fd818d9459548a7863f38ebdd6c40f8"> Thresholds <a href="#4fd818d9459548a7863f38ebdd6c40f8" title="permalink">#</a> </h3> <p> Agreeing on a threshold can help curb code rot. Institute a rule and monitor a metric. For example, you could agree to keep an eye on cyclomatic complexity. If it exceeds <em>7</em>, you reject the change. </p> <p> <img src="/content/binary/development-of-complexity-guarded-by-threshold.png" alt="Line chart showing how complexity is curbed by a threshold of 7."> </p> <p> Such rules work because they can be used to counteract gradual decay. It's not the specific value <em>7</em> that contributes to better <a href="/2019/03/04/code-quality-is-not-software-quality">code quality</a>; it's the automatic activation of a rule based on a threshold. If you decide that the threshold should be <em>10</em> instead, that'll also make a difference. </p> <p> Notice that the above diagram suggests that exceeding the threshold is still possible. Rules are in the way if you must rigidly obey them. Situations arise where breaking a rule is the best response. Once you've responded to the situation, however, find a way to bring the offending code back in line. Once a threshold is exceeded, you don't get any further warnings, and there's a risk that that particular code will gradually decay. </p> <h3 id="431b3a30bb6448b6b34c06a169b6fefb"> What you measure is what you get <a href="#431b3a30bb6448b6b34c06a169b6fefb" title="permalink">#</a> </h3> <p> You could automate the process. Imagine running cyclomatic complexity analysis as part of a Continuous Integration build and rejecting changes that exceed a threshold. This is, in a way, a deliberate attempt to hack the management effect where you get what you measure. With emphasis on a metric like cyclomatic complexity, developers will pay attention to it. </p> <p> Be aware, however, of <a href="https://en.wikipedia.org/wiki/Goodhart%27s_law">Goodhart's law</a> and <a href="https://en.wikipedia.org/wiki/Unintended_consequences">the law of unintended consequences</a>. Just as <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">code coverage is a useless target measure</a>, you have to be careful when you institute hard rules. </p> <p> I've had success with introducing threshold rules because they increase awareness. It can help a technical leader shift emphasis to the qualities that he or she wishes to improve. Once the team's mindset has changed, the rule itself becomes redundant. </p> <p> I'm reminded of <a href="https://en.wikipedia.org/wiki/Dreyfus_model_of_skill_acquisition">the Dreyfus model of skill acquisition</a>. Rules make great training wheels. Once you become proficient, the rules are no longer required. They may even be in your way. When that happens, get rid of them. </p> <h3 id="97ad7bcd44644a05b78c75e82a3c4ad7"> Conclusion <a href="#97ad7bcd44644a05b78c75e82a3c4ad7" title="permalink">#</a> </h3> <p> Code deteriorates gradually, when you aren't looking. Instituting rules that make you pay attention can combat code rot. Using thresholds to activate your attention can be an effective countermeasure. The specific value of the threshold is less important. </p> <p> In this article, I've mostly used cyclomatic complexity as an example of a metric where a threshold could be useful. Another example is line width; don't exceed 80 characters. Or line height: methods shouldn't exceed 24 lines of code. Those are examples. If you agree that keeping an eye on a metric would be useful, but you disagree with the threshold I suggest, pick a value that suits you better. </p> <p> It's not the specific threshold value that improves your code; paying attention does. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f50c5f69f3ed45d4a1fd1a0d4099f079"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#f50c5f69f3ed45d4a1fd1a0d4099f079">#</a></div> <div class="comment-content"> <p>In F#, it's not uncommon to have inner functions (local functions defined inside other functions). How would you calculate the cyclomatic complexity of a function that contains inner functions?</p> <p>To be specific, I'm actually wondering about how to count the number of activated objects in a function, which you talk about in your book, <em>Code That Fits in Your Head</em>. I have been wanting to ask you this for some time, but haven't been able to find a good article to comment on. I think this is the closest I can get.</p> <p>In terms of activated objects: Would you count all activated objects in all sub-functions as counting towards the top-level function? Or would you count the inner functions separately, and have calls to the inner function contribute only "one point" to the top-level functions? I think the latter makes most sense, but I'm not sure. I base my reasoning on the fact that an inner function, being a closure, is similar to a class with fields and a single method (<a href="http://wiki.c2.com/?ClosuresAndObjectsAreEquivalent">closures are a poor man's objects and vice versa</a>). Another way to view it is that you could refactor by extracting the function and adding any necessary parameters.</p> <p>PS, this is not just theoretical. I am toying with a linter for F# and want "number of activated objects" as one of the rules.</p> </div> <div class="comment-date">2023-04-20 14:00 UTC</div> </div> <div class="comment" id="bfdeb86dcefc457ab84cbd0b42bfcdd0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bfdeb86dcefc457ab84cbd0b42bfcdd0">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. For the purposes of calculating cyclomatic complexity of inner functions, aren't they equivalent to (private) helper methods? </p> <p> If so, they don't count towards the cyclomatic complexity of the containing function. </p> <p> As for the other question, I don't count functions as activated objects, but I do count the values they return. When the function is <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>, however, they're equal. I do talk more about this in my talk <em>Fractal Architecture</em>, of which you can find several recordings on the internet; <a href="https://youtu.be/mRbTMlE5ElA">here</a>'s one. </p> <p> The book also discusses this, and that part is also <a href="/2021/07/28/referential-transparency-fits-in-your-head">freely available here on the blog</a>. </p> <p> The short answer is that it's essential that you can prevent 'inner' objects from leaking out from method calls. Per definition, functions that are referentially transparent do have that property. For methods that aren't referentially transparent, encapsulation may still achieve the same effect, if done right. Usually, however, it isn't. </p> </div> <div class="comment-date">2023-04-21 16:26 UTC</div> </div> <div class="comment" id="ad61d353f8614a059c36d497de796b8d"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#ad61d353f8614a059c36d497de796b8d">#</a></div> <div class="comment-content"> <p>Mark, thank you for the response. What you say about cyclomatic complexity makes sense, especially given your previous writings on the subject. I am still a bit fuzzy on how to count activated objects, though.</p> <p>If a function returns a tuple of which one item is ignored, would that ignored object count as an activated object? (Does the fact that a tuple could technically be considered as a single object with <code>.Item1</code> and <code>.Item2</code> properties change the answer?) And what about piping? Eta reduction?</p> <p><a href="http://www.exampler.com/">An example would be handy right about now</a>, so what would you say are the activated object counts of the following functionally identical functions, and why?<p> <ol> <li><code>let f (a, b) = let c, _ = getCD (a, b) in c</code></li> <li><code>let f (a, b) = (a, b) |> getCD |> fst</code></li> <li><code>let f = getCD >> fst</code></li> <li><code>let f = getCDFst</code></li> </ol> <p>Note the "trick" of number 3: By removing the explicit parameter, it is now impossible to tell just from looking at the code how many tupled items <code>f</code> accepts, if any. And in number 4, even the knowledge that an intermediate function in the composition returns a tuple of 2 values is removed.</p> <p>Additionally: I assume that returning a value counts as "activating" an object? So <code>let f x = x</code> has 1 activated object? What about the functionally identical <code>let f = id</code>? Would that be 1 or 0?</p> <p>I guess what I'm after is a more fully developed concept of "the number of activated objects" of a function/method, to the point where a useful linter rule could be implemented based on it; something similar to your previous writings on how <a href="https://blog.ploeh.dk/2019/12/09/put-cyclomatic-complexity-to-good-use/#de927bfcc95d410bbfcd0adf7a63926b">method calls do not increase the cyclomatic complexity</a>, which was a very useful clarification that I have seen you repeat several times. I have given the matter some thought myself, but as you can see, I haven't been able to come up with good answer.</p> </div> <div class="comment-date">2023-04-22 06:03 UTC</div> </div> <div class="comment" id="313c192fff9d4748bfafb8d8997e47dc"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#313c192fff9d4748bfafb8d8997e47dc">#</a></div> <div class="comment-content"> <p> It seems that I should have been more explicit about the terminology related to the adjective <em>activated</em>. I was inspired by the notion of object activation described in <a href="/ref/thinking-fast-and-slow">Thinking Fast and Slow</a>. The idea is that certain pieces of information move to the forefront in the mind, and that these 'objects' impact decision-making. Kahneman labels such information as <em>activated</em> when it impacts the decision process. </p> <p> The heuristic I had in mind was to port that idea to an (informal) readability analysis. Given that the human short-time memory is quite limited, I find it useful to count the mental load of a given piece of code. </p> <p> The point, then, is not to count all objects or values in scope, but rather those that are required to understand what a piece of code does. For example, if you look at an instance method on a class, the class could have four class fields, but if only one of those fields are used in the method, only that one is activated - even though the other three are also in scope. </p> <p> With that in mind, let's try to look at your four examples. </p> <ol> <li>This example activates <code>a</code>, <code>b</code>, and <code>c</code>: <em>3</em> objects.</li> <li>This example activates <code>a</code> and <code>b</code>: <em>2</em> objects.</li> <li>No objects, unless you now want to count <code>getCD</code> as an object.</li> <li>Again, probably no objects.</li> </ol> <p> Note that I've employed qualifying words. The point of the analysis is to highlight objects that might stress our short-term memory. It's not an exact science, and I never intended it to be. Rather, I see it as a possible springboard for having a discussion about relative readability of code. A team can use the heuristic to compare alternatives. </p> <p> With your examples in mind, you'd be likely to run into programmers who find the first two examples more readable than the third. It's certainly more 'detailed', so, in a sense, it's easier to understand what's going on. That works as long as you only have a few values in play, but cognitively, it doesn't scale. </p> <p> I do tend to prefer eta reductions and point-free notation exactly because they tend to reduce the number of activated objects, but these techniques certainly also raise the abstraction level. On the other hand, once someone understands something like function composition (<code>&gt;&gt;</code>) or point-free notation, they can now leverage long-term memory for that, instead of having to rely on limited short-term memory in order to understand a piece of code. By moving more information to long-term memory, we can reduce the load on short-term memory, thereby freeing it up for other information. </p> <p> Perhaps that's a bit of a digression, but I always intended the notion of object activation to be a heuristic rather than an algorithm. </p> </div> <div class="comment-date">2023-04-23 20:01 UTC</div> </div> <div class="comment" id="66b5e9838a4a42eda1108eb2338a0fe5"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#66b5e9838a4a42eda1108eb2338a0fe5">#</a></div> <div class="comment-content"> <p>Mark, thank you for the excellent clarification. It gave me one of those "a-ha" moments that accompanies a sudden jump in understanding. In hindsight, of course this is about the cognitive load of a piece of code, and of course that will be different for different people, based for example on which abstractions they are used to.</p> <p>In terms of the code examples, I think we both agree that <code>let f = a >> b</code> requires less mental load than <code>let f = a >> b >> c >> d >> e</code>. In other words, I would argue that functions in a composition do contribute to cognitive load. This may however also depend on the actual functions that are composed.</p> <p>In any case, I am now less certain than before that a simple linter rule (i.e., an algorithm) can capture cognitive load in a way that is generally useful. I will have to think about this some more.</p> </div> <div class="comment-date">2023-04-24 13:47 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Repeatable execution in C# https://blog.ploeh.dk/2020/04/06/repeatable-execution-in-c 2020-04-06T07:46:00+00:00 Mark Seemann <div id="post"> <p> <em>A C# example of Goldilogs.</em> </p> <p> This article is part of <a href="/2020/03/23/repeatable-execution">a series of articles about repeatable execution</a>. The introductory article argued that if you've logged the impure actions that a system made, you have enough information to reproduce what happened. The <a href="/2020/03/30/repeatable-execution-in-haskell">previous article</a> verified that for the example scenario, the impure actions were limited to reading the current time and interacting with the application database. </p> <p> This article shows how to implement equivalent functionality in C#. You should be able to extrapolate from this to other object-oriented programming languages. </p> <p> The code is <a href="https://github.com/ploeh/reservation-api-slice-csharp">available on GitHub</a>. </p> <h3 id="338d728ddae0410dbb7fefccd6781f6f"> Impure actions <a href="#338d728ddae0410dbb7fefccd6781f6f" title="permalink">#</a> </h3> <p> In the previous article I <a href="/2017/07/10/pure-interactions">modelled impure actions as free monads</a>. In C#, it'd be more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> to <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">use Dependency Injection</a>. Model each impure interaction as an interface. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IClock</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetCurrentDateTime</span>(); }</pre> </p> <p> The demo code demonstrates a single feature of a REST API and it only requires a single method on this interface to work. Following the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> <blockquote> <p> "clients [...] own the abstract interfaces" </p> <footer><cite><a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices</a>, chapter 11</cite></footer> </blockquote> This interface only defines a single method, because that's all the client code requires. </p> <p> Likewise, the client code only needs two methods to interact with the database: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>); }</pre> </p> <p> In the Haskell example code base, I also implemented <code>GET</code> for <code>/reservations</code>, but I forgot to do that here. There's only two methods on the interface: one to query the database, and one to create a new row. </p> <h3 id="63f39b6964f74a9c847913f4e1b5359c"> Receive a reservation <a href="#63f39b6964f74a9c847913f4e1b5359c" title="permalink">#</a> </h3> <p> The central feature of the service is to receive and handle an HTTP POST request, as described in the introductory article. When a document arrives it triggers a series of non-trivial work: <ol> <li>The service validates the input data. Among other things, it checks that the reservation is in the future. It uses <code>GetCurrentDateTime</code> for this.</li> <li>It queries the database for existing reservations. It uses <code>ReadReservations</code> for this.</li> <li>It uses complex business logic to determine whether to accept the reservation. This essentially implements the <a href="/2020/01/27/the-maitre-d-kata">Ma&icirc;tre d' kata</a>.</li> <li>If it accepts the reservation, it stores it. It uses <code>Create</code> for this.</li> </ol> These steps manifest as this Controller method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Post</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:blue;">_</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BadRequest</span>(<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Mapper</span>.<span style="color:#74531f;">Map</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date&nbsp;<span style="font-weight:bold;color:#74531f;">&lt;</span>&nbsp;Clock.<span style="font-weight:bold;color:#74531f;">GetCurrentDateTime</span>()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BadRequest</span>(<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>&nbsp;=&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">accepted</span>&nbsp;=&nbsp;maîtreD.<span style="font-weight:bold;color:#74531f;">CanAccept</span>(<span style="font-weight:bold;color:#1f377f;">reservations</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="font-weight:bold;color:#1f377f;">accepted</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">StatusCode</span>(<span style="color:#2b91af;">StatusCodes</span>.Status500InternalServerError,&nbsp;<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Ok</span>(); }</pre> </p> <p> <code>Clock</code> and <code>Repository</code> are injected dependencies, and <code>maîtreD</code> is an object that implements the decision logic as the <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> <code>CanAccept</code> function. </p> <h3 id="c7042ff0e8c246cc8a9f12e656a4bf54"> Composition <a href="#c7042ff0e8c246cc8a9f12e656a4bf54" title="permalink">#</a> </h3> <p> The <code>Post</code> method is defined on a class called <code>ReservationsController</code> with these dependencies: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seatingDuration</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">tables</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">repository</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IClock</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">clock</span>)</pre> </p> <p> The <code>seatingDuration</code> and <code>tables</code> arguments are <a href="/2012/07/02/PrimitiveDependencies">primitive dependencies</a> used to configure the <code>maîtreD</code> object. I could also have injected <code>maîtreD</code> as a <a href="/2012/08/31/ConcreteDependencies">concrete dependency</a>, but I decided against that for no particular reason. </p> <p> There's no logging dependency, but the system still logs. Like in the previous example, logging is a cross-cutting concern and exclusively addressed through composition: </p> <p> <pre><span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">controllerType</span>&nbsp;<span style="font-weight:bold;color:#74531f;">==</span>&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ReservationsController</span>)) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ScopedLog</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FileLog</span>(LogFile)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">controller</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SeatingDuration, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tables, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">LogReservationsRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlReservationsRepository</span>(ConnectionString), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">LogClock</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SystemClock</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>)); &nbsp;&nbsp;&nbsp;&nbsp;Logs.<span style="font-weight:bold;color:#74531f;">AddOrUpdate</span>(<span style="font-weight:bold;color:#1f377f;">controller</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>,&nbsp;(<span style="font-weight:bold;color:#1f377f;">_</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>)&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">controller</span>; }</pre> </p> <p> Each dependency is wrapped by a logger. We'll return to that in a minute, but consider first the actual implementations. </p> <h3 id="40cf79001f6f4982ba694efe324cb2b1"> Using the system clock <a href="#40cf79001f6f4982ba694efe324cb2b1" title="permalink">#</a> </h3> <p> Using the system clock is easy: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SystemClock</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IClock</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetCurrentDateTime</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">DateTime</span>.Now; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation of <code>IClock</code> simply delegates to <code>DateTime.Now</code>. Again, no logging service is injected. </p> <h3 id="392b67a393cc4deaad9b19901ea4c4c4"> Using the database <a href="#392b67a393cc4deaad9b19901ea4c4c4" title="permalink">#</a> </h3> <p> Using the database isn't much harder. I don't find that <a href="https://en.wikipedia.org/wiki/Object-relational_mapping">ORMs</a> offer any benefits, so I prefer to implement database functionality using basic database APIs: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">conn</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(ConnectionString)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(createReservationSql,&nbsp;<span style="font-weight:bold;color:#1f377f;">conn</span>)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>.Parameters.<span style="font-weight:bold;color:#74531f;">Add</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Guid&quot;</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>.Id)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>.Parameters.<span style="font-weight:bold;color:#74531f;">Add</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>.Parameters.<span style="font-weight:bold;color:#74531f;">Add</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>.Name)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>.Parameters.<span style="font-weight:bold;color:#74531f;">Add</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Email&quot;</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>.Email)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>.Parameters.<span style="font-weight:bold;color:#74531f;">Add</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Quantity&quot;</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">conn</span>.<span style="font-weight:bold;color:#74531f;">Open</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">cmd</span>.<span style="font-weight:bold;color:#74531f;">ExecuteNonQuery</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;createReservationSql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;INSERT&nbsp;INTO&nbsp;[dbo].[Reservations]&nbsp;([Guid],&nbsp;[Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity]) &nbsp;&nbsp;&nbsp;&nbsp;OUTPUT&nbsp;INSERTED.Id &nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(@Guid,&nbsp;@Date,&nbsp;@Name,&nbsp;@Email,&nbsp;@Quantity)&quot;</span>;</pre> </p> <p> The above code snippet implements the <code>Create</code> method of the <code>IReservationsRepository</code> interface. Please refer to the Git repository for the full code if you need more details. </p> <p> If you prefer to implement your database functionality with an ORM, or in another way, you can do that. It doesn't change the architecture of the system. No logging service is required to interact with the database. </p> <h3 id="e70ddcfa03c04444931f0b3737e09101"> Compose with logging <a href="#e70ddcfa03c04444931f0b3737e09101" title="permalink">#</a> </h3> <p> As the above composition code snippet suggests, logging is implemented with <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a>. The ultimate implementation of <code>IClock</code> is <code>SystemClock</code>, but the <a href="/2011/07/28/CompositionRoot">Composition Root</a> decorates it with <code>LogClock</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">LogClock</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IClock</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">LogClock</span>(<span style="color:#2b91af;">IClock</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">inner</span>,&nbsp;<span style="color:#2b91af;">ScopedLog</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">log</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inner&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">inner</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">log</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IClock</span>&nbsp;Inner&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ScopedLog</span>&nbsp;Log&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetCurrentDateTime</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">currentDateTime</span>&nbsp;=&nbsp;Inner.<span style="font-weight:bold;color:#74531f;">GetCurrentDateTime</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Observe</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Interaction</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Operation&nbsp;=&nbsp;<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#74531f;">GetCurrentDateTime</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Output&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">currentDateTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">currentDateTime</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> <code>ScopedLog</code> is a Concrete Dependency that, among other members, affords the <code>Observe</code> method. Notice that <code>LogClock</code> implements <code>IClock</code> by decorating another polymorphic <code>IClock</code> instance. It delegates functionality to <code>inner</code>, logs the <code>currentDateTime</code> and returns it. </p> <p> The <code>LogReservationsRepository</code> class implements the same pattern: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">LogReservationsRepository</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">LogReservationsRepository</span>(<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">inner</span>,&nbsp;<span style="color:#2b91af;">ScopedLog</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">log</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inner&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">inner</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">log</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Inner&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ScopedLog</span>&nbsp;Log&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Observe</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Interaction</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Operation&nbsp;=&nbsp;<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#74531f;">Create</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Input&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inner.<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>&nbsp;=&nbsp;Inner.<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="font-weight:bold;color:#1f377f;">date</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Observe</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Interaction</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Operation&nbsp;=&nbsp;<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#74531f;">ReadReservations</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Input&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Output&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This architecture not only implements the desired functionality, but also <em>Goldilogs</em>: not too little, not too much, but just what you need. Notice that I didn't have to change any of my Domain Model or HTTP-specific code to enable logging. This cross-cutting concern is enabled entirely via composition. </p> <h3 id="59b912d918c9451e97e09b8274d7fb87"> Repeatability <a href="#59b912d918c9451e97e09b8274d7fb87" title="permalink">#</a> </h3> <p> An HTTP request like this: </p> <p> <pre>POST /reservations/ HTTP/1.1 Content-Type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;7bc3fc93-a777-4138-8630-a805e7246335&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-03-20&nbsp;18:45:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kozue Kaburagi&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;ninjette@example.net&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 }</pre> </p> <p> produces a log entry like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;entry&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-01-02T09:50:34.2678703+01:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;operation&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Post&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;input&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;dto&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;7bc3fc93-a777-4138-8630-a805e7246335&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-03-20&nbsp;18:45:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;ninjette@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kozue Kaburagi&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;output&quot;</span>:&nbsp;<span style="color:blue;">null</span> &nbsp;&nbsp;}, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;interactions&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-01-02T09:50:34.2726143+01:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;operation&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;GetCurrentDateTime&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;input&quot;</span>:&nbsp;<span style="color:blue;">null</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;output&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-01-02T09:50:34.2724012+01:00&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-01-02T09:50:34.3571224+01:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;operation&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;ReadReservations&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;input&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-03-20T18:45:00&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;output&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;c3cbfbc7-6d64-4ead-84ef-7f89de5b7e1c&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-03-20T19:00:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;emp@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Elissa&nbsp;Megan&nbsp;Powers&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-01-02T09:50:34.3587586+01:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;operation&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Create&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;input&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;reservation&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;7bc3fc93-a777-4138-8630-a805e7246335&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-03-20T18:45:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;ninjette@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Kozue Kaburagi&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;output&quot;</span>:&nbsp;<span style="color:blue;">null</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;], &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;exit&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;time&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-01-02T09:50:34.3645105+01:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;operation&quot;</span>:&nbsp;<span style="color:blue;">null</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;input&quot;</span>:&nbsp;<span style="color:blue;">null</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;output&quot;</span>:&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;statusCode&quot;</span>:&nbsp;200&nbsp;} &nbsp;&nbsp;} }</pre> </p> <p> I chose to gather all information regarding a single HTTP request into a single log entry and format it as JSON. I once worked with an organisation that used the ELK stack in that way, and it made it easy to identify and troubleshoot issues in production. </p> <p> You can use such a log file to reproduce the observed behaviour, for example in a unit test: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">NinjetteRepro</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">log</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Log</span>.<span style="color:#74531f;">LoadFile</span>(<span style="color:#a31515;">&quot;Ninjette.json&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#2b91af;">ReservationsController</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>,&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Log</span>.<span style="color:#74531f;">LoadReservationsControllerPostScenario</span>(<span style="font-weight:bold;color:#1f377f;">log</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.<span style="font-weight:bold;color:#74531f;">Post</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">IsAssignableFrom</span>&lt;<span style="color:#2b91af;">OkResult</span>&gt;(<span style="font-weight:bold;color:#1f377f;">actual</span>); }</pre> </p> <p> This test reproduces the behaviour that was recorded in the above JSON log. While there was already one existing reservation (returned from <code>ReadReservations</code>), the system had enough remaining capacity to accept the new reservation. Therefore, the expected result is an <code>OkResult</code>. </p> <h3 id="2e246dfafa7e49c58600a066adf3ed0d"> Replay <a href="#2e246dfafa7e49c58600a066adf3ed0d" title="permalink">#</a> </h3> <p> You probably noticed the helper methods <code>Log.LoadFile</code> and <code>Log.LoadReservationsControllerPostScenario</code>. This API is just a prototype to get the point across. There's little to say about <code>LoadFile</code>, since it just reads the file. The <code>LoadReservationsControllerPostScenario</code> method performs the bulk of the work. It parses the JSON string into a collection of observations. It then feeds these observations to test-specific implementations of the dependencies required by <code>ReservationsController</code>. </p> <p> For example, here's the test-specific implementation of <code>IClock</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReplayClock</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IClock</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Queue</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;times; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReplayClock</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">times</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.times&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Queue</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;(<span style="font-weight:bold;color:#1f377f;">times</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetCurrentDateTime</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;times.<span style="font-weight:bold;color:#74531f;">Dequeue</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The above JSON log example only contains a single observation of <code>GetCurrentDateTime</code>, but an arbitrary log may contain zero, one, or several observations. The idea is to replay them, starting with the earliest. <code>ReplayClock</code> just creates a <code>Queue</code> of them and <code>Dequeue</code> every time <code>GetCurrentDateTime</code> executes. </p> <p> The test-specific <code>ReplayReservationsRepository</code> class works the same way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReplayReservationsRepository</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IDictionary</span>&lt;<span style="color:#2b91af;">DateTime</span>,&nbsp;<span style="color:#2b91af;">Queue</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&gt;&nbsp;reads; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReplayReservationsRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDictionary</span>&lt;<span style="color:#2b91af;">DateTime</span>,&nbsp;<span style="color:#2b91af;">Queue</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reads</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.reads&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">reads</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;reads[<span style="font-weight:bold;color:#1f377f;">date</span>].<span style="font-weight:bold;color:#74531f;">Dequeue</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You'll notice that in order to implement <code>ReadReservations</code>, the <code>ReplayReservationsRepository</code> class needs a dictionary of queues. The <code>ReplayClock</code> class didn't need a dictionary, because <code>GetCurrentDateTime</code> takes no input. The <code>ReadReservations</code> method, on the other hand, takes a <code>date</code> as a method argument. You might have observations of <code>ReadReservations</code> for different dates, and multiple observations for each date. That's the reason that <code>ReplayReservationsRepository</code> needs a dictionary of queues. </p> <p> The <code>Create</code> method doesn't return anything, so I decided that this methods should do nothing. </p> <p> The <code>LoadReservationsControllerPostScenario</code> function parses the JSON log and creates instances of these <a href="https://en.wikipedia.org/wiki/Test_double">Test Doubles</a>. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">repository</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReplayReservationsRepository</span>(<span style="font-weight:bold;color:#1f377f;">reads</span>); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">clock</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReplayClock</span>(<span style="font-weight:bold;color:#1f377f;">times</span>); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">controller</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(<span style="font-weight:bold;color:#1f377f;">seatingDuration</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">tables</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">repository</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">clock</span>);</pre> </p> <p> And that, together with the parsed HTTP input, is what <code>LoadReservationsControllerPostScenario</code> returns: </p> <p> <pre><span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">controller</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>);</pre> </p> <p> This is only a prototype to illustrate the point that you can reproduce an interaction if you have all the impure inputs and outputs. The details are available in the source code repository. </p> <h3 id="cde93455d7354c3bb99b4e6fbb1b4792"> Summary <a href="#cde93455d7354c3bb99b4e6fbb1b4792" title="permalink">#</a> </h3> <p> This article demonstrated how making the distinction between pure and impure code is useful in many situations. For logging purposes, you only need to log the impure inputs and outputs. That's neither too little logging, nor too much, but just right: <em>Goldilogs</em>. </p> <p> Model any (potentially) impure interaction as a dependency and use Dependency Injection. This enables you to reproduce observed behaviour from logs. Don't inject logging services into your Controllers or Domain Models. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Repeatable execution in Haskell https://blog.ploeh.dk/2020/03/30/repeatable-execution-in-haskell 2020-03-30T08:02:00+00:00 Mark Seemann <div id="post"> <p> <em>A way to figure out what to log, and what not to log, using Haskell.</em> </p> <p> This article is part of <a href="/2020/03/23/repeatable-execution">a series of articles about repeatable execution</a>. The previous article argued that if you've logged the impure actions that a system made, you have enough information to reproduce what happened. </p> <p> In most languages, <a href="/2020/02/24/discerning-and-maintaining-purity">it's difficult to discriminate between pure functions and impure actions</a>, but <a href="https://www.haskell.org">Haskell</a> explicitly makes that distinction. I often use it for proof of concepts for that reason. I'll do that here as well. </p> <p> This proof of concept is mostly to verify what a decade of functional programming has already taught me. For the functionality that the previous article introduced, the impure actions involve a database and the system clock. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/reservation-api-slice-haskell">available on GitHub</a>. </p> <h3 id="1c5f6ac50111450c8cf9b6d064977bcc"> Pure interactions <a href="#1c5f6ac50111450c8cf9b6d064977bcc" title="permalink">#</a> </h3> <p> I'll use <a href="/2017/07/10/pure-interactions">free monads to model impure interactions as pure functions</a>. For this particular example code base, an <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a> would have been sufficient. I do, however, get the impression that many readers find it hard to extrapolate from impureim sandwiches to a general architecture. For the benefit of those readers, the example uses free monads. </p> <p> The system clock interaction is the simplest: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;ClockInstruction&nbsp;next&nbsp;=&nbsp;CurrentTime&nbsp;(LocalTime&nbsp;-&gt;&nbsp;next)&nbsp;<span style="color:blue;">deriving</span>&nbsp;Functor</pre> </p> <p> There's only one instruction. It takes no input, but returns the current time and date. </p> <p> For database interactions, I went through a few iterations and arrived at this set of instructions: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;ReservationsInstruction&nbsp;next&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;ReadReservation&nbsp;UUID&nbsp;(Maybe&nbsp;Reservation&nbsp;-&gt;&nbsp;next) &nbsp;&nbsp;|&nbsp;ReadReservations&nbsp;LocalTime&nbsp;([Reservation]&nbsp;-&gt;&nbsp;next) &nbsp;&nbsp;|&nbsp;CreateReservation&nbsp;Reservation&nbsp;next &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;Functor</pre> </p> <p> There's two queries and a command. The intent with the <code>CreateReservation</code> command is to create a new reservation row in the database. The two queries fetch a single reservation based on ID, or a set of reservations based on a date. A central type for this instruction set is <code>Reservation</code>: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Reservation&nbsp;=&nbsp;Reservation &nbsp;&nbsp;{&nbsp;reservationId&nbsp;::&nbsp;UUID &nbsp;&nbsp;,&nbsp;reservationDate&nbsp;::&nbsp;LocalTime &nbsp;&nbsp;,&nbsp;reservationName&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;reservationEmail&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;reservationQuantity&nbsp;::&nbsp;Int &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>,&nbsp;<span style="color:#2b91af;">Generic</span>)</pre> </p> <p> The program has to interact both with the system clock and the database, so ultimately it turned out to be useful to combine these two instruction sets into one: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ReservationsProgram&nbsp;=&nbsp;Free&nbsp;(Sum&nbsp;ReservationsInstruction&nbsp;ClockInstruction)</pre> </p> <p> I used the <code>Sum</code> functor to combine the two instruction sets, and then turned them into a <code>Free</code> monad. </p> <p> With free monads, I find that my code becomes more readable if I define helper functions for each instruction: </p> <p> <pre><span style="color:#2b91af;">readReservation</span>&nbsp;::&nbsp;<span style="color:blue;">UUID</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;(<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:blue;">Reservation</span>) readReservation&nbsp;rid&nbsp;=&nbsp;liftF&nbsp;$&nbsp;InL&nbsp;$&nbsp;ReadReservation&nbsp;rid&nbsp;<span style="color:blue;">id</span> <span style="color:#2b91af;">readReservations</span>&nbsp;::&nbsp;<span style="color:blue;">LocalTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;[<span style="color:blue;">Reservation</span>] readReservations&nbsp;t&nbsp;=&nbsp;liftF&nbsp;$&nbsp;InL&nbsp;$&nbsp;ReadReservations&nbsp;t&nbsp;<span style="color:blue;">id</span> <span style="color:#2b91af;">createReservation</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;() createReservation&nbsp;r&nbsp;=&nbsp;liftF&nbsp;$&nbsp;InL&nbsp;$&nbsp;CreateReservation&nbsp;r&nbsp;<span style="color:blue;">()</span> <span style="color:#2b91af;">currentTime</span>&nbsp;::&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;<span style="color:blue;">LocalTime</span> currentTime&nbsp;=&nbsp;liftF&nbsp;$&nbsp;InR&nbsp;$&nbsp;CurrentTime&nbsp;<span style="color:blue;">id</span></pre> </p> <p> There's much else going on in the code base, but that's how I model feature-specific impure actions. </p> <h3 id="46812b6dc4594937b0697ca09134af83"> Receive a reservation <a href="#46812b6dc4594937b0697ca09134af83" title="permalink">#</a> </h3> <p> The central feature of the service is to receive and handle an HTTP POST request, as described in the introductory article. When a document arrives it triggers a series of non-trivial work: <ol> <li>The service validates the input data. Among other things, it checks that the reservation is in the future. It uses <code>currentTime</code> for this.</li> <li>It queries the database for existing reservations. It uses <code>readReservations</code> for this.</li> <li>It uses complex business logic to determine whether to accept the reservation. This essentially implements the <a href="/2020/01/27/the-maitre-d-kata">Ma&icirc;tre d' kata</a>.</li> <li>If it accepts the reservation, it stores it. It uses <code>createReservation</code> for this.</li> </ol> These steps manifest as this function: </p> <p> <pre><span style="color:#2b91af;">tryAccept</span>&nbsp;::&nbsp;<span style="color:blue;">NominalDiffTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Table</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ExceptT</span>&nbsp;(<span style="color:blue;">APIError</span>&nbsp;<span style="color:blue;">ByteString</span>)&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;() tryAccept&nbsp;seatingDuration&nbsp;tables&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;now&nbsp;&lt;-&nbsp;lift&nbsp;currentTime &nbsp;&nbsp;_&nbsp;&lt;-&nbsp;liftEither&nbsp;$&nbsp;validateReservation&nbsp;now&nbsp;r &nbsp;&nbsp;reservations&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;(removeNonOverlappingReservations&nbsp;seatingDuration&nbsp;r)&nbsp;&lt;$&gt; &nbsp;&nbsp;&nbsp;&nbsp;lift&nbsp;$&nbsp;readReservations&nbsp;$&nbsp;reservationDate&nbsp;r &nbsp;&nbsp;_&nbsp;&lt;-&nbsp;liftEither&nbsp;$&nbsp;canAccommodateReservation&nbsp;tables&nbsp;reservations&nbsp;r &nbsp;&nbsp;lift&nbsp;$&nbsp;createReservation&nbsp;r</pre> </p> <p> If you're interested in details, the code is available on GitHub. I may later write other articles about interesting details. </p> <p> In the context of repeatable execution and logging, the key is that this is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>. It does, however, return a <code>ReservationsProgram</code> (free monad), so it's not going to <em>do</em> anything until interpreted. The interpreters are impure, so this is where logging has to take place. </p> <h3 id="3cc3141ffcc644c78618f117c53f66ba"> HTTP API <a href="#3cc3141ffcc644c78618f117c53f66ba" title="permalink">#</a> </h3> <p> The above <code>tryAccept</code> function is decoupled from boundary concerns. It has little HTTP-specific functionality. </p> <p> I've written the actual HTTP API using <a href="https://www.servant.dev">Servant</a>. The following function translates the above Domain Model to an HTTP API: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ReservationsProgramT&nbsp;=&nbsp;FreeT&nbsp;(Sum&nbsp;ReservationsInstruction&nbsp;ClockInstruction) <span style="color:#2b91af;">reservationServer</span>&nbsp;::&nbsp;<span style="color:blue;">NominalDiffTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Table</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ServerT</span>&nbsp;<span style="color:blue;">ReservationAPI</span>&nbsp;(<span style="color:blue;">ReservationsProgramT</span>&nbsp;<span style="color:blue;">Handler</span>) reservationServer&nbsp;seatingDuration&nbsp;tables&nbsp;=&nbsp;getReservation&nbsp;:&lt;|&gt;&nbsp;postReservation &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;getReservation&nbsp;rid&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mr&nbsp;&lt;-&nbsp;toFreeT&nbsp;$&nbsp;readReservation&nbsp;rid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;mr&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;r&nbsp;-&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;-&gt;&nbsp;throwError&nbsp;err404 &nbsp;&nbsp;&nbsp;&nbsp;postReservation&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;&lt;-&nbsp;toFreeT&nbsp;$&nbsp;runExceptT&nbsp;$&nbsp;tryAccept&nbsp;seatingDuration&nbsp;tables&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;e&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Right&nbsp;<span style="color:blue;">()</span>&nbsp;-&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">()</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Left&nbsp;(ValidationError&nbsp;err)&nbsp;-&gt;&nbsp;throwError&nbsp;$&nbsp;err400&nbsp;{&nbsp;errBody&nbsp;=&nbsp;err&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Left&nbsp;&nbsp;(ExecutionError&nbsp;err)&nbsp;-&gt;&nbsp;throwError&nbsp;$&nbsp;err500&nbsp;{&nbsp;errBody&nbsp;=&nbsp;err&nbsp;}</pre> </p> <p> This API also exposes a reservation as a resource you can query with a <code>GET</code> request, but I'm not going to comment much on that. It uses the above <code>readReservation</code> helper function, but there's little logic involved in the implementation. </p> <p> The above <code>reservationServer</code> function implements, by the way, only a <em>partial</em> API. It defines the <code>/reservations</code> resource, as explained in the overview article. Its type is defined as: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ReservationAPI&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capture&nbsp;<span style="color:#a31515;">&quot;reservationId&quot;</span>&nbsp;UUID&nbsp;:&gt;&nbsp;Get&nbsp;&#39;[JSON]&nbsp;Reservation &nbsp;&nbsp;:&lt;|&gt;&nbsp;ReqBody&nbsp;&#39;[JSON]&nbsp;Reservation&nbsp;:&gt;&nbsp;Post&nbsp;&#39;[JSON]&nbsp;<span style="color:blue;">()</span></pre> </p> <p> That's just one resource. <em>Servant</em> enables you define many resources and combine them into a larger API. For this example, the <code>/reservations</code> resource is all there is, so I define the entire API like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;API&nbsp;=&nbsp;<span style="color:#a31515;">&quot;reservations&quot;</span>&nbsp;:&gt;&nbsp;ReservationAPI</pre> </p> <p> You can also define your complete <code>server</code> from several partial services, but in this example, I only have one: </p> <p> <pre>server&nbsp;=&nbsp;reservationServer</pre> </p> <p> Had I had more resources, I could have combined several values with a combinator, but now that I have only <code>reservationServer</code> it seems redundant, I admit. </p> <h3 id="678638fe747f41c491994dc43be49d7b"> Hosting the API <a href="#678638fe747f41c491994dc43be49d7b" title="permalink">#</a> </h3> <p> The <code>reservationServer</code> function, and thereby also <code>server</code>, returns a <code>ServerT</code> value. <em>Servant</em> ultimately demands a <code>Server</code> value to <code>serve</code> it. We need to transform the <code>ServerT</code> value into a <code>Server</code> value, which we can do with <code>hoistServer</code>: </p> <p> <pre><span style="color:#2b91af;">runApp</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() runApp&nbsp;connStr&nbsp;port&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;$&nbsp;<span style="color:#a31515;">&quot;Starting&nbsp;server&nbsp;on&nbsp;port&nbsp;&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;port&nbsp;++&nbsp;<span style="color:#a31515;">&quot;.&quot;</span> &nbsp;&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;<span style="color:#a31515;">&quot;Press&nbsp;Ctrl&nbsp;+&nbsp;C&nbsp;to&nbsp;stop&nbsp;the&nbsp;server.&quot;</span> &nbsp;&nbsp;ls&nbsp;&lt;-&nbsp;loggerSet &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;logLn&nbsp;s&nbsp;=&nbsp;pushLogStrLn&nbsp;ls&nbsp;$&nbsp;toLogStr&nbsp;s &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hoistSQL&nbsp;=&nbsp;hoistServer&nbsp;api&nbsp;$&nbsp;runInSQLServerAndOnSystemClock&nbsp;logLn&nbsp;$&nbsp;pack&nbsp;connStr &nbsp;&nbsp;(seatingDuration,&nbsp;tables)&nbsp;&lt;-&nbsp;readConfig &nbsp;&nbsp;logHttp&nbsp;&lt;-&nbsp;logHttpMiddleware&nbsp;ls &nbsp;&nbsp;run&nbsp;port&nbsp;$&nbsp;logHttp&nbsp;$&nbsp;serve&nbsp;api&nbsp;$&nbsp;hoistSQL&nbsp;$&nbsp;server&nbsp;seatingDuration&nbsp;tables</pre> </p> <p> The <code>hoistServer</code> function enables you to translate a <code>ServerT api m</code> into a <code>ServerT api n</code> value. Since <code>Server</code> is a type alias for <code>ServerT api Handler</code>, we need to translate the complicated monad returned from <code>server</code> into a <code>Handler</code>. The <code>runInSQLServerAndOnSystemClock</code> function does most of the heavy lifting. </p> <p> You'll also notice that the <code>runApp</code> function configures some logging. Apart from some HTTP-level middleware, the <code>logLn</code> function logs a line to a text file. The <code>runApp</code> function passes it as an argument to the <code>runInSQLServerAndOnSystemClock</code> function. We'll return to logging later in this article, but first I find it instructive to outline what happens in <code>runInSQLServerAndOnSystemClock</code>. </p> <p> As the name implies, two major actions take place. The function interprets database interactions by executing impure actions against SQL Server. It also interprets clock interactions by querying the system clock. </p> <h3 id="05b701cca27f44479c4c3bc1d169d935"> Using the system clock <a href="#05b701cca27f44479c4c3bc1d169d935" title="permalink">#</a> </h3> <p> The system-clock-based interpreter is the simplest of the two interpreters. It interprets <code>ClockInstruction</code> values by querying the system clock for the current time: </p> <p> <pre><span style="color:#2b91af;">runOnSystemClock</span>&nbsp;::&nbsp;<span style="color:blue;">MonadIO</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">ClockInstruction</span>&nbsp;(m&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a runOnSystemClock&nbsp;(CurrentTime&nbsp;next)&nbsp;=&nbsp;liftIO&nbsp;(zonedTimeToLocalTime&nbsp;&lt;$&gt;&nbsp;getZonedTime)&nbsp;&gt;&gt;=&nbsp;next</pre> </p> <p> This function translates a <code>ClockInstruction (m a)</code> to an <code>m a</code> value by executing the impure <code>getZonedTime</code> function. From the returned <code>ZonedTime</code> value, it then extracts the local time, which it passes to <code>next</code>. </p> <p> You may have two questions: <ul> <li>Why map <code>ClockInstruction (m a)</code> instead of <code>ClockInstruction a</code>?</li> <li>Why <code>MonadIO</code>?</li> </ul> I'll address each in turn. </p> <p> My ultimate goal with each of these interpreters is to compose them into <code>runInSQLServerAndOnSystemClock</code>. As described above, this function transforms <code>ServerT API (ReservationsProgramT Handler)</code> into a <code>ServerT API Handler</code> (also known as <code>Server API</code>). Another way to put this is that we need to collapse <code>ReservationsProgramT Handler</code> to <code>Handler</code> by, so to speak, removing <code>ReservationsProgramT</code>. </p> <p> Recall that a type like <code>ReservationsProgramT Handler</code> is really in 'curried' form. This is actually the parametrically polymorphic type <code>ReservationsProgramT Handler a</code>. Likewise, <code>Handler</code> is also parametrically polymorphic: <code>Handler a</code>. What we need, then, is a function with the type <code>ReservationsProgramT Handler a -&gt; Handler a</code> or, more generally, <code>FreeT f m a -&gt; m a</code>. This follows because <code>ReservationsProgramT</code> is an alias for <code>FreeT ...</code>, and <code>Handler</code> is <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">a container</a> of <code>a</code> values. </p> <p> There's a function for that in <a href="http://hackage.haskell.org/package/free/docs/Control-Monad-Trans-Free.html">Control.Monad.Trans.Free</a> called <code>iterT</code>: </p> <p> <pre><span style="color:#2b91af;">iterT</span>&nbsp;::&nbsp;(<span style="color:blue;">Functor</span>&nbsp;f,&nbsp;<span style="color:blue;">Monad</span>&nbsp;m)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(f&nbsp;(m&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FreeT</span>&nbsp;f&nbsp;m&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a</pre> </p> <p> This fits our need. For each of the functors in <code>ReservationsProgramT</code>, then, we need a function <code>f (m a) -&gt; m a</code>. Specifically, for <code>ClockInstruction</code>, we need to define a function with the type <code>ClockInstruction (Handler a) -&gt; Handler a</code>. Consider, however, the definition of <code>Handler</code>. It's a <code>newtype</code> over a <code>newtype</code>, so much wrapping is required. If I specifically wanted to return that explicit type, I'd have to take the <code>IO</code> vale produced by <code>getZonedTime</code> and wrap it in <code>Handler</code>, which would require me to first wrap it in <code>ExceptT</code>, which again would require me to wrap it in <code>Either</code>. That's a lot of bother, but <code>Handler</code> is also a <code>MonadIO</code> instance, and that elegantly sidesteps the issue. By implementing <code>runOnSystemClock</code> with <code>liftIO</code>, it works for all <code>MonadIO</code> instances, including <code>Handler</code>. </p> <p> Hopefully, that explains why <code>runOnSystemClock</code> has the type that it has. </p> <h3 id="9f530f57c49549b6823c45859ee5890c"> Using the database <a href="#9f530f57c49549b6823c45859ee5890c" title="permalink">#</a> </h3> <p> The database interpreter is more complex than <code>runOnSystemClock</code>, but it follows the same principles. The reasoning outlined above also apply here. </p> <p> <pre><span style="color:#2b91af;">runInSQLServer</span>&nbsp;::&nbsp;<span style="color:blue;">MonadIO</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Text</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsInstruction</span>&nbsp;(m&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a runInSQLServer&nbsp;connStr&nbsp;(ReadReservation&nbsp;rid&nbsp;next)&nbsp;= &nbsp;&nbsp;liftIO&nbsp;(readReservation&nbsp;connStr&nbsp;rid)&nbsp;&gt;&gt;=&nbsp;next runInSQLServer&nbsp;connStr&nbsp;(ReadReservations&nbsp;t&nbsp;next)&nbsp;= &nbsp;&nbsp;liftIO&nbsp;(readReservations&nbsp;connStr&nbsp;t)&nbsp;&gt;&gt;=&nbsp;next runInSQLServer&nbsp;connStr&nbsp;(CreateReservation&nbsp;r&nbsp;next)&nbsp;= &nbsp;&nbsp;liftIO&nbsp;(insertReservation&nbsp;connStr&nbsp;r)&nbsp;&gt;&gt;&nbsp;next</pre> </p> <p> Since <code>ReservationsInstruction</code> is a sum type with three cases, the <code>runInSQLServer</code> action has to handle all three. Each case calls a dedicated helper function. I'll only show one of these to give you a sense for how they look. </p> <p> <pre><span style="color:#2b91af;">readReservations</span>&nbsp;::&nbsp;<span style="color:blue;">Text</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">LocalTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;[<span style="color:blue;">Reservation</span>] readReservations&nbsp;connStr&nbsp;(LocalTime&nbsp;d&nbsp;_)&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sql&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;SELECT&nbsp;[Guid],&nbsp;[Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity]\ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\FROM&nbsp;[dbo].[Reservations]\ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\WHERE&nbsp;CONVERT(DATE,&nbsp;[Date])&nbsp;=&nbsp;&quot;</span>&nbsp;&lt;&gt;&nbsp;toSql&nbsp;d &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;withConnection&nbsp;connStr&nbsp;$&nbsp;\conn&nbsp;-&gt;&nbsp;<span style="color:blue;">fmap</span>&nbsp;unDbReservation&nbsp;&lt;$&gt;&nbsp;query&nbsp;conn&nbsp;sql</pre> </p> <p> You can see all the details about <code>withConnection</code>, <code>unDbReservation</code>, etcetera in the Git repository. The principal point is that these are just normal <code>IO</code> actions. </p> <h3 id="7dd30abf99b24ec6b13a1c22d0f6f150"> Basic composition <a href="#7dd30abf99b24ec6b13a1c22d0f6f150" title="permalink">#</a> </h3> <p> The two interpreters are all we need to compose a working system: </p> <p> <pre><span style="color:#2b91af;">runInSQLServerAndOnSystemClock</span>&nbsp;::&nbsp;<span style="color:blue;">MonadIO</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Text</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsProgramT</span>&nbsp;m&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a runInSQLServerAndOnSystemClock&nbsp;connStr&nbsp;=&nbsp;iterT&nbsp;go &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;go&nbsp;(InL&nbsp;rins)&nbsp;=&nbsp;DB.runInSQLServer&nbsp;connStr&nbsp;rins &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;(InR&nbsp;cins)&nbsp;=&nbsp;runOnSystemClock&nbsp;cins</pre> </p> <p> The <code>iterT</code> function enables you to interpret a <code>FreeT</code> value, of which <code>ReservationsProgramT</code> is an alias. The <code>go</code> function just pattern-matches on the two cases of the <code>Sum</code> functor, and delegates to the corresponding interpreter. </p> <p> This composition enables the system to run and do the intended work. You can start the server and make <code>GET</code> and <code>POST</code> requests against the <code>/reservations</code> resource, as outlined in the first article in this small series. </p> <p> This verifies what I already hypothesized. This feature set requires two distinct sets of impure interactions: <ul> <li>Getting the current time</li> <li>Querying and writing to a database</li> </ul> Once you've worked with Haskell for some time, you'll get good at predicting which actions are impure, and which functionality can be kept pure. The current result isn't surprising. </p> <p> It does make it clear what ought to be logged. All the pure functionality can be reproduced if you have the inputs. You only need to log the impure interactions, and now you know what they are. </p> <h3 id="f29d957fcfe644e9a9efaf70bf58ad74"> Compose with logging <a href="#f29d957fcfe644e9a9efaf70bf58ad74" title="permalink">#</a> </h3> <p> You need to log the impure operations, and you know that they're interacting with the system clock and the database. As usual, starting with the system clock is most accessible. You can write what's essentially a <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> of any <code>ClockInstruction</code> interpreter: </p> <p> <pre><span style="color:#2b91af;">logClock</span>&nbsp;::&nbsp;<span style="color:blue;">MonadIO</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(forall&nbsp;x.&nbsp;<span style="color:blue;">ClockInstruction</span>&nbsp;(m&nbsp;x)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ClockInstruction</span>&nbsp;(m&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a logClock&nbsp;logLn&nbsp;inner&nbsp;(CurrentTime&nbsp;next)&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;output&nbsp;&lt;-&nbsp;inner&nbsp;$&nbsp;CurrentTime&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;liftIO&nbsp;$&nbsp;writeLogEntry&nbsp;logLn&nbsp;<span style="color:#a31515;">&quot;CurrentTime&quot;</span>&nbsp;<span style="color:blue;">()</span>&nbsp;output &nbsp;&nbsp;next&nbsp;output</pre> </p> <p> The <code>logClock</code> action decorates any <code>inner</code> interpreter with the logging action <code>logLn</code>. It returns an action of the same type as it decorates. </p> <p> It relies on a helper function called <code>writeLogEntry</code>, which handles some of the formalities of formatting and time-stamping each log entry. </p> <p> You can decorate any database interpreter in the same way: </p> <p> <pre><span style="color:#2b91af;">logReservations</span>&nbsp;::&nbsp;<span style="color:blue;">MonadIO</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(forall&nbsp;x.&nbsp;<span style="color:blue;">ReservationsInstruction</span>&nbsp;(m&nbsp;x)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsInstruction</span>&nbsp;(m&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a logReservations&nbsp;logLn&nbsp;inner&nbsp;(ReadReservation&nbsp;rid&nbsp;next)&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;output&nbsp;&lt;-&nbsp;inner&nbsp;$&nbsp;ReadReservation&nbsp;rid&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;liftIO&nbsp;$&nbsp;writeLogEntry&nbsp;logLn&nbsp;<span style="color:#a31515;">&quot;ReadReservation&quot;</span>&nbsp;rid&nbsp;output &nbsp;&nbsp;next&nbsp;output logReservations&nbsp;logLn&nbsp;inner&nbsp;(ReadReservations&nbsp;t&nbsp;next)&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;output&nbsp;&lt;-&nbsp;inner&nbsp;$&nbsp;ReadReservations&nbsp;t&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;liftIO&nbsp;$&nbsp;writeLogEntry&nbsp;logLn&nbsp;<span style="color:#a31515;">&quot;ReadReservations&quot;</span>&nbsp;t&nbsp;output &nbsp;&nbsp;next&nbsp;output logReservations&nbsp;logLn&nbsp;inner&nbsp;(CreateReservation&nbsp;r&nbsp;next)&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;output&nbsp;&lt;-&nbsp;inner&nbsp;$&nbsp;CreateReservation&nbsp;r&nbsp;(<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">()</span>) &nbsp;&nbsp;liftIO&nbsp;$&nbsp;writeLogEntry&nbsp;logLn&nbsp;<span style="color:#a31515;">&quot;CreateReservation&quot;</span>&nbsp;r&nbsp;output &nbsp;&nbsp;next</pre> </p> <p> The <code>logReservations</code> action follows the same template as <code>logClock</code>; only it has more lines of code because <code>ReservationsInstruction</code> is a discriminated union with three cases. </p> <p> With these Decorator actions you can change the application composition so that it logs all impure inputs and outputs: </p> <p> <pre><span style="color:#2b91af;">runInSQLServerAndOnSystemClock</span>&nbsp;::&nbsp;<span style="color:blue;">MonadIO</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Text</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsProgramT</span>&nbsp;m&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;a runInSQLServerAndOnSystemClock&nbsp;logLn&nbsp;connStr&nbsp;=&nbsp;iterT&nbsp;go &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;go&nbsp;(InL&nbsp;rins)&nbsp;=&nbsp;logReservations&nbsp;logLn&nbsp;(DB.runInSQLServer&nbsp;connStr)&nbsp;rins &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;(InR&nbsp;cins)&nbsp;=&nbsp;logClock&nbsp;logLn&nbsp;runOnSystemClock&nbsp;cins</pre> </p> <p> This not only implements the desired functionality, but also <em>Goldilogs:</em> not too little, not too much, but just what you need. Notice that I didn't have to change any of my Domain Model or HTTP-specific code to enable logging. This cross-cutting concern is enabled entirely via composition. </p> <h3 id="3d37879949774c10a002ad7ff93ad571"> Repeatability <a href="#3d37879949774c10a002ad7ff93ad571" title="permalink">#</a> </h3> <p> An HTTP request like this: </p> <p> <pre>POST /reservations/ HTTP/1.1 Content-Type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;c3cbfbc7-6d64-4ead-84ef-7f89de5b7e1c&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2020-03-20&nbsp;19:00:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Elissa&nbsp;Megan&nbsp;Powers&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;emp@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;3 }</pre> </p> <p> produces a series of log entries like these: </p> <p> <pre>LogEntry {logTime = 2019-12-29 20:21:53.0029235 UTC, logOperation = "CurrentTime", logInput = "()", logOutput = "2019-12-29 21:21:53.0029235"} LogEntry {logTime = 2019-12-29 20:21:54.0532677 UTC, logOperation = "ReadReservations", logInput = "2020-03-20 19:00:00", logOutput = "[]"} LogEntry {logTime = 2019-12-29 20:21:54.0809254 UTC, logOperation = "CreateReservation", logInput = "Reservation {reservationId = c3cbfbc7-6d64-4ead-84ef-7f89de5b7e1c, reservationDate = 2020-03-20 19:00:00, reservationName = \"Elissa Megan Powers\", reservationEmail = \"emp@example.com\", reservationQuantity = 3}", logOutput = "()"} LogEntry {logTime = 2019-12-29 20:21:54 UTC, logOperation = "PostReservation", logInput = "\"{ \\\"id\\\": \\\"c3cbfbc7-6d64-4ead-84ef-7f89de5b7e1c\\\", \\\"date\\\": \\\"2020-03-20 19:00:00\\\", \\\"name\\\": \\\"Elissa Megan Powers\\\", \\\"email\\\": \\\"emp@example.com\\\", \\\"quantity\\\": 3 }\"", logOutput = "()"}</pre> </p> <p> This is only a prototype to demonstrate what's possible. In an attempt to make things simple for myself, I decided to just log data by using the <code>Show</code> instance of each value being logged. In order to reproduce behaviour, I'll rely on the corresponding <code>Read</code> instance for the type. This was probably naive, and not a decision I would employ in a production system, but it's good enough for a prototype. </p> <p> For example, the above log entry states that the <code>CurrentTime</code> instruction was evaluated and that the output was <code>2019-12-29 21:21:53.0029235</code>. Second, the <code>ReadReservations</code> instruction was evaluated with the input <code>2020-03-20 19:00:00</code> and the output was the empty list (<code>[]</code>). The third line records that the <code>CreateReservation</code> instruction was evaluated with a particular input, and that the output was <code>()</code>. </p> <p> The fourth and final record is the the actual values observed at the HTTP boundary. </p> <p> You can load and parse the logged data into a unit test or an interactive session: </p> <p> <pre>λ&gt; l &lt;- lines &lt;$&gt; readFile "the/path/to/the/log.txt" λ&gt; replayData = readReplayData l λ&gt; replayData ReplayData { &nbsp;&nbsp;observationsOfPostReservation = &nbsp;&nbsp;&nbsp;&nbsp;[Reservation { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservationId = c3cbfbc7-6d64-4ead-84ef-7f89de5b7e1c, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservationDate = 2020-03-20 19:00:00, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservationName = "Elissa Megan Powers", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservationEmail = "emp@example.com", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservationQuantity = 3}], &nbsp;&nbsp;observationsOfRead = fromList [], &nbsp;&nbsp;observationsOfReads = fromList [(2020-03-20 19:00:00,[[]])], &nbsp;&nbsp;observationsOfCurrentTime = [2019-12-29 21:21:53.0029235]} λ&gt; r = head $ observationsOfPostReservation replayData λ&gt; r Reservation { &nbsp;&nbsp;reservationId = c3cbfbc7-6d64-4ead-84ef-7f89de5b7e1c, &nbsp;&nbsp;reservationDate = 2020-03-20 19:00:00, &nbsp;&nbsp;reservationName = "Elissa Megan Powers", &nbsp;&nbsp;reservationEmail = "emp@example.com", &nbsp;&nbsp;reservationQuantity = 3}</pre> </p> <p> (I've added line breaks and indentation to some of the output to make it more readable, compared to what GHCi produces.) </p> <p> The most important thing to notice is the <code>readReplayData</code> function that parses the log file into Haskell data. I've also written a prototype of a function that can <code>replay</code> the actions as they happened: </p> <p> <pre>λ&gt; (seatingDuration, tables) &lt;- readConfig λ&gt; replay replayData $ tryAccept seatingDuration tables r Right ()</pre> </p> <p> The original HTTP request returned <code>200 OK</code> and that's exactly how <code>reservationServer</code> translates a <code>Right ()</code> result. So the above interaction is a faithful reproduction of what actually happened. </p> <h3 id="5d94afef4145456abe86ae6f2dd9445a"> Replay <a href="#5d94afef4145456abe86ae6f2dd9445a" title="permalink">#</a> </h3> <p> You may have noticed that I used a <code>replay</code> function above. This is only a prototype to get the point across. It's just another interpreter of <code>ReservationsProgram</code> (or, rather an <code>ExceptT</code> wrapper of <code>ReservationsProgram</code>): </p> <p> <pre><span style="color:#2b91af;">replay</span>&nbsp;::&nbsp;<span style="color:blue;">ReplayData</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ExceptT</span>&nbsp;e&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;e&nbsp;a replay&nbsp;d&nbsp;=&nbsp;replayImp&nbsp;d&nbsp;.&nbsp;runExceptT &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">replayImp</span>&nbsp;::&nbsp;<span style="color:blue;">ReplayData</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a &nbsp;&nbsp;&nbsp;&nbsp;replayImp&nbsp;rd&nbsp;p&nbsp;=&nbsp;State.evalState&nbsp;(iterM&nbsp;go&nbsp;p)&nbsp;rd &nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;(InL&nbsp;(ReadReservation&nbsp;rid&nbsp;next))&nbsp;=&nbsp;replayReadReservation&nbsp;rid&nbsp;&gt;&gt;=&nbsp;next &nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;(InL&nbsp;(ReadReservations&nbsp;t&nbsp;next))&nbsp;=&nbsp;replayReadReservations&nbsp;t&nbsp;&gt;&gt;=&nbsp;next &nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;(InL&nbsp;(CreateReservation&nbsp;_&nbsp;next))&nbsp;=&nbsp;next &nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;(InR&nbsp;(CurrentTime&nbsp;next))&nbsp;=&nbsp;replayCurrentTime&nbsp;&gt;&gt;=&nbsp;next</pre> </p> <p> While this is compact Haskell code that <em>I</em> wrote, I still found it so abstruse that I decided to add a type annotation to a local function. It's not required, but I find that it helps me understand what <code>replayImp</code> does. It uses <code>iterM</code> (a cousin to <code>iterT</code>) to interpret the <code>ReservationsProgram</code>. The entire interpretation is stateful, so runs in the <code>State</code> monad. Here's an example: </p> <p> <pre><span style="color:#2b91af;">replayCurrentTime</span>&nbsp;::&nbsp;<span style="color:blue;">State</span>&nbsp;<span style="color:blue;">ReplayData</span>&nbsp;<span style="color:blue;">LocalTime</span> replayCurrentTime&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;xs&nbsp;&lt;-&nbsp;State.gets&nbsp;observationsOfCurrentTime &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(observation:rest)&nbsp;=&nbsp;xs &nbsp;&nbsp;State.modify&nbsp;(\s&nbsp;-&gt;&nbsp;s&nbsp;{&nbsp;observationsOfCurrentTime&nbsp;=&nbsp;rest&nbsp;}) &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;observation</pre> </p> <p> The <code>replayCurrentTime</code> function replays log observations of <code>CurrentTime</code> instructions. The <code>observationsOfCurrentTime</code> field is a list of observed values, parsed from a log. A <code>ReservationsProgram</code> might query the <code>CurrentTime</code> multiple times, so there could conceivably be several such observations. The idea is to replay them, starting with the earliest. </p> <p> Each time the function replays an observation, it should remove it from the log. It does that by first retrieving all observations from the state. It then pattern-matches the <code>observation</code> from the <code>rest</code> of the observations. I execute my code with the <code>-Wall</code> option, so I'm puzzled that I don't get a warning from the compiler about that line. After all, the <code>xs</code> list could be empty. This is, however, prototype code, so I decided to ignore that issue. </p> <p> Before the function returns the <code>observation</code> it updates the replay data by effectively removing the <code>observation</code>, but without touching anything else. </p> <p> The <code>replayReadReservation</code> and <code>replayReadReservations</code> functions follow the same template. You can consult the source code repository if you're curious about the details. You may also notice that the <code>go</code> function doesn't do anything when it encounters a <code>CreateReservation</code> instruction. This is because that instruction has no return value, so there's no reason to consult a log to figure out what to return. </p> <h3 id="fa5477f706a143d59c0006e52af2fe2d"> Summary <a href="#fa5477f706a143d59c0006e52af2fe2d" title="permalink">#</a> </h3> <p> The point of this article was to flesh out a fully functional feature (a vertical slice, if you're so inclined) in Haskell, in order to verify that the only impure actions involved are: <ul> <li>Getting the current time</li> <li>Interacting with the application database</li> </ul> This turns out to be the case. </p> <p> Furthermore, prototype code demonstrates that based on a log of impure interactions, you can repeat the logged execution. </p> <p> Now that we know what is impure and what can be pure, we can reproduce the same architecture in C# (or another mainstream programming language). </p> <p> <strong>Next:</strong> <a href="/2020/04/06/repeatable-execution-in-c">Repeatable execution in C#</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4d71311250a54b1ca558a8469acdf545"> <div class="comment-author"><a href="https://majiehong.com/">Jiehong</a> <a href="#4d71311250a54b1ca558a8469acdf545">#</a></div> <div class="comment-content"> <p>The Free Monad, as any monad, enforces sequential operations.</p> <p> How would you deal with having to sent multiple transactions (let's say to the db and via http), while also retrying <em>n</em> times if it fails? </p> </div> <div class="comment-date">2020-04-06 9:38 UTC</div> </div> <div class="comment" id="8f689c5cb98d42ad8e4d8eb02ef81cc3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8f689c5cb98d42ad8e4d8eb02ef81cc3">#</a></div> <div class="comment-content"> <p> Jiehong, thank you for writing. I'm not sure that I can give you a complete answer, as this is something that I haven't experimented with in Haskell. </p> <p> In C#, on the other hand, you can implement stability patterns like <a href="https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern">Circuit Breaker</a> and retries with <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a>. I don't see why you can't do that in Haskell as well. </p> </div> <div class="comment-date">2020-04-10 10:15 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Repeatable execution https://blog.ploeh.dk/2020/03/23/repeatable-execution 2020-03-23T08:17:00+00:00 Mark Seemann <div id="post"> <p> <em>What to log, and how to log it.</em> </p> <p> When I visit software organisations to help them make their code more maintainable, I often see code like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ILog</span>&nbsp;Log&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Post</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Debug</span>(<span style="color:#a31515;">$&quot;Entering&nbsp;</span>{<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#74531f;">Post</span>)}<span style="color:#a31515;">&nbsp;method...&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:blue;">_</span>)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Warning</span>(<span style="color:#a31515;">&quot;Invalid&nbsp;reservation&nbsp;date.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BadRequest</span>(<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Debug</span>(<span style="color:#a31515;">&quot;Mapping&nbsp;DTO&nbsp;to&nbsp;Domain&nbsp;Model.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Mapper</span>.<span style="color:#74531f;">Map</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date&nbsp;<span style="font-weight:bold;color:#74531f;">&lt;</span>&nbsp;<span style="color:#2b91af;">DateTime</span>.Now) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Warning</span>(<span style="color:#a31515;">&quot;Invalid&nbsp;reservation&nbsp;date.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BadRequest</span>(<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Debug</span>(<span style="color:#a31515;">&quot;Reading&nbsp;existing&nbsp;reservations&nbsp;from&nbsp;database.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>&nbsp;=&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">accepted</span>&nbsp;=&nbsp;maîtreD.<span style="font-weight:bold;color:#74531f;">CanAccept</span>(<span style="font-weight:bold;color:#1f377f;">reservations</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="font-weight:bold;color:#1f377f;">accepted</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Warning</span>(<span style="color:#a31515;">&quot;Not&nbsp;enough&nbsp;capacity&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">StatusCode</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">StatusCodes</span>.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Info</span>(<span style="color:#a31515;">&quot;Adding&nbsp;reservation&nbsp;to&nbsp;database.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;Log.<span style="font-weight:bold;color:#74531f;">Debug</span>(<span style="color:#a31515;">$&quot;Leaving&nbsp;</span>{<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#74531f;">Post</span>)}<span style="color:#a31515;">&nbsp;method...&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Ok</span>(); }</pre> </p> <p> Logging like this annoys me. It adds avoidable noise to the code, making it harder to read, and thus, more difficult to maintain. </p> <h3 id="75930691088c44568c80941c70bc147a"> Ideal <a href="#75930691088c44568c80941c70bc147a" title="permalink">#</a> </h3> <p> The above code ought to look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Post</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:blue;">_</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BadRequest</span>(<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Mapper</span>.<span style="color:#74531f;">Map</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date&nbsp;<span style="font-weight:bold;color:#74531f;">&lt;</span>&nbsp;Clock.<span style="font-weight:bold;color:#74531f;">GetCurrentDateTime</span>()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BadRequest</span>(<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date}<span style="color:#a31515;">.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>&nbsp;=&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">accepted</span>&nbsp;=&nbsp;maîtreD.<span style="font-weight:bold;color:#74531f;">CanAccept</span>(<span style="font-weight:bold;color:#1f377f;">reservations</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="font-weight:bold;color:#1f377f;">accepted</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">StatusCode</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">StatusCodes</span>.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Ok</span>(); }</pre> </p> <p> This is more readable. The logging statements are gone from the code, thereby amplifying the essential behaviour of the <code>Post</code> method. The noise is gone. </p> <p> <em>Wait a minute!</em> you might say, <em>You can't just remove logging! Logging is important.</em> </p> <p> Yes, I agree, and I didn't. This code still logs. It logs just what you need to log. No more, no less. </p> <h3 id="c176715622a74d69a312cfa8e47159fb"> Types of logging <a href="#c176715622a74d69a312cfa8e47159fb" title="permalink">#</a> </h3> <p> Before we talk about the technical details, I think it's important to establish some vocabulary and context. In this article, I use the term <em>logging</em> broadly to describe any sort of action of recording what happened while software executed. There's more than one reason an application might have to do that: <ul> <li> <strong>Instrumentation.</strong> You may log to support your own work. The first code listing in this article is a typical example of this style of logging. If you've ever had the responsibility of having to support an application that runs in production, you know that you need insight into what happens. When people report strange behaviours, you need those logs to support troubleshooting. </li> <li> <strong>Telemetry.</strong> You may log to support other people's work. You can write status updates, warnings, and errors to support operations. You can record Key Performance Indicators (KPIs) to support 'the business'. </li> <li> <strong>Auditing.</strong> You may log because you're legally obliged to do so. </li> <li> <strong>Metering.</strong> You may log who does what so that you can bill users based on consumption. </li> </ul> Regardless of motivation, I still consider these to be types of logging. All are <em>cross-cutting concerns</em> in that they're independent of application features. Logging records what happened, when it happened, who or what triggered the event, and so on. The difference between instrumentation, telemetry, auditing, and metering is only <em>what</em> you choose to persist. </p> <p> Particularly when it comes to instrumentation, I often see examples of 'overlogging'. When logging is done to support future troubleshooting, you can't predict what you're going to need, so it's better to log too much data than too little. </p> <p> It'd be even better to log <em>only</em> what you need. Not too little, not too much, but just the right amount of logging. Obviously, we should call this <em>Goldilogs</em>. </p> <h3 id="ea53735b49ef4ad7849ebceeabc616ac"> Repeatability <a href="#ea53735b49ef4ad7849ebceeabc616ac" title="permalink">#</a> </h3> <p> How do you know what to log? How do you know that you've logged everything that you'll need, when you don't know your future needs? </p> <p> The key is repeatability. Just like you should be able to <a href="https://en.wikipedia.org/wiki/Reproducible_builds">reproduce builds</a> and <a href="https://en.wikipedia.org/wiki/Continuous_delivery">repeat deployments</a>, you should also be able to reproduce <em>execution</em>. </p> <p> If you can replay what happened when a problem manifested itself, you can troubleshoot it. You need to log just enough data to enable you to repeat execution. How do you identify that data? </p> <p> Consider a line of code like this: </p> <p> <pre><span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">z</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;+&nbsp;<span style="font-weight:bold;color:#1f377f;">y</span>;</pre> </p> <p> Would you log that? </p> <p> It might make sense to log what <code>x</code> and <code>y</code> are, particularly if these values are run-time values (e.g. entered by a user, the result of a web service call, etc.): </p> <p> <pre>Log.<span style="font-weight:bold;color:#74531f;">Debug</span>(<span style="color:#a31515;">$&quot;Adding </span>{<span style="font-weight:bold;color:#1f377f;">x</span>}<span style="color:#a31515;"> and </span>{<span style="font-weight:bold;color:#1f377f;">y</span>}<span style="color:#a31515;">.&quot;</span>); <span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">z</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;+&nbsp;<span style="font-weight:bold;color:#1f377f;">y</span>;</pre> </p> <p> Would you ever log the result, though? </p> <p> <pre>Log.<span style="font-weight:bold;color:#74531f;">Debug</span>(<span style="color:#a31515;">$&quot;Adding </span>{<span style="font-weight:bold;color:#1f377f;">x</span>}<span style="color:#a31515;"> and </span>{<span style="font-weight:bold;color:#1f377f;">y</span>}<span style="color:#a31515;">.&quot;</span>); <span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">z</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;+&nbsp;<span style="font-weight:bold;color:#1f377f;">y</span>; Log.<span style="font-weight:bold;color:#74531f;">Debug</span>(<span style="color:#a31515;">$&quot;Result of addition: </span>{<span style="font-weight:bold;color:#1f377f;">z</span>}<span style="color:#a31515;">&quot;</span>);</pre> </p> <p> There's no reason to log the result of the calculation. Addition is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>; it's <em>deterministic</em>. If you know the inputs, you can always repeat the calculation to get the output. Two plus two is always four. </p> <p> The more your code is composed from pure functions, the less you need to log. </p> <h3 id="a59305ce898b48879d5ca90db24f243a"> Log only impure actions <a href="#a59305ce898b48879d5ca90db24f243a" title="permalink">#</a> </h3> <p> In principle, all code bases interleave pure functions with impure actions. In most procedural or object-oriented code, no attempt is made of separating the two: </p> <p> <img src="/content/binary/impure-with-stripes-of-purity.png" alt="A box of mostly impure (red) code with vertical stripes of green symbolising pure code."> </p> <p> I've here illustrated impure actions with red and pure functions with green. Imagine that this is a conceptual block of code, with execution flowing from top to bottom. When you write normal procedural or object-oriented code, most of the code will have some sort of local side effect in the form of a state change, a more system-wide side effect, or be non-deterministic. Occasionally, arithmetic calculation or similar will form small pure islands. </p> <p> While you don't need to log the output of those pure functions, it hardly makes a difference, since most of the code is impure. It would be a busy log, in any case. </p> <p> Once you shift towards functional-first programming, your code may begin to look like this instead: </p> <p> <img src="/content/binary/functional-first-impure-pure-box.png" alt="A box of mostly pure (green) code with a few vertical stripes of red symbolising impure code."> </p> <p> You may still have some code that occasionally executes impure actions, but largely, most of the code is pure. If you know the inputs to all the pure code, you can reproduce that part of the code. This means that you only need to log the non-deterministic parts: the impure actions. Particularly, you need to log the outputs from the impure actions, because these impure output values become the inputs to the next pure block of code. </p> <p> This style of architecture is what you'll often get with a well-designed <a href="https://fsharp.org">F#</a> code base, but you can also replicate it in C# or another object-oriented programming language. I'd also draw a diagram like this to illustrate how <a href="https://www.haskell.org">Haskell</a> code works if you <a href="/2017/07/10/pure-interactions">model interactions with free monads</a>. </p> <p> This is the most generally applicable example, so articles in this short series show a Haskell code base with free monads, as well as a C# code base. </p> <p> In reality, you can often get away with an <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>: </p> <p> <img src="/content/binary/impure-pure-impure-sandwich-box.png" alt="A box with a thin red slice on top, a thick green middle, and a thin red slice at the bottom."> </p> <p> This architecture makes things simpler, including logging. You only need to log the inital and the concluding impure actions. The rest, you can always recompute. </p> <p> I <em>could</em> have implemented the comprehensive example code shown in the next articles as impureim sandwiches, but I chose to use free monads in the Haskell example, and Dependency Injection in the C# example. I did this in order to offer examples from which you can extrapolate a more complex architecture for your production code. </p> <h3 id="28bbf72ea37d47229312d9cf7bf14b9d"> Examples <a href="#28bbf72ea37d47229312d9cf7bf14b9d" title="permalink">#</a> </h3> <p> I've produced two equivalent example code bases to show how to log just enough data. The first is in Haskell because <a href="/2020/02/24/discerning-and-maintaining-purity">it's the best way to be sure that pure and impure code is properly separated</a>. </p> <p> Both example applications have the same externally visible behaviour. They showcase a focused vertical slice of a restaurant reservation system. The only feature they support is the creation of a reservation. </p> <p> Clients make reservations by making an HTTP POST request to the reservation system: </p> <p> <pre>POST /reservations HTTP/1.1 Content-Type: application/json { &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;id&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;84cef648-1e5f-467a-9d13-1b81db7f6df3&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2021-12-21&nbsp;19:00:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 }</pre> </p> <p> This is an attempt to make a reservation for four people at December 21, 2021 at seven in the evening. Both code bases support this HTTP API. </p> <p> If the web service accepts the reservation, it'll write the reservation as a record in a SQL Server database. The table is defined as: </p> <p> <pre><span style="color:blue;">CREATE</span>&nbsp;<span style="color:blue;">TABLE</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Reservations]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span> &nbsp;&nbsp;&nbsp;&nbsp;[Id]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">IDENTITY</span><span style="color:gray;">,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Guid]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">UNIQUEIDENTIFIER</span>&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">UNIQUE</span><span style="color:gray;">,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Date]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">DATETIME2</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Name]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">NVARCHAR&nbsp;</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Email]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">NVARCHAR&nbsp;</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Quantity]&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">PRIMARY</span>&nbsp;<span style="color:blue;">KEY</span>&nbsp;<span style="color:blue;">CLUSTERED&nbsp;</span><span style="color:gray;">(</span>[Id]&nbsp;<span style="color:blue;">ASC</span><span style="color:gray;">)</span></pre> </p> <p> Both implementations of the service can run on the same database. </p> <p> The examples follow in separate articles: <ul> <li><a href="/2020/03/30/repeatable-execution-in-haskell">Repeatable execution in Haskell</a></li> <li><a href="/2020/04/06/repeatable-execution-in-c">Repeatable execution in C#</a></li> </ul> Readers not comfortable with Haskell are welcome to skip directly to the C# article. </p> <h3 id="3068df1aa90b48bab281478d4bbf659c"> Log metadata <a href="#3068df1aa90b48bab281478d4bbf659c" title="permalink">#</a> </h3> <p> In this article series, I focus on run-time data. The point is that there's a formal method to identify what to log: <em>Log the inputs to and outputs from impure actions.</em> </p> <p> I don't focus on metadata, but apart from run-time data, each log entry should be accompanied by metadata. As a minimum, each entry should come with information about the time it was observed, but here's a list of metadata to consider: <ul> <li>Date and time of the log entry. Make sure to include the time zone, or alternatively, log exclusively in UTC.</li> <li>The version of the software that produced the entry. This is particularly important if you deploy new versions of the software several times a day.</li> <li>The user account or security context in which the application runs.</li> <li>The machine ID, if you consolidate server farm logs in one place.</li> <li>Correlation IDs, if present.</li> </ul> I don't claim that this list is complete, but I hope it can inspire you to add the metadata that you need. </p> <h3 id="f8469ec9c7e54089b05c6a84562c404d"> Conclusion <a href="#f8469ec9c7e54089b05c6a84562c404d" title="permalink">#</a> </h3> <p> You only need to log what happens in impure actions. In a normal imperative or object-oriented code base, this is almost a useless selection criterion, because most of what happens is impure. Thus, you need to log almost everything. </p> <p> There's many benefits to be had from moving towards a <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a>. One of them is that it simplifies logging. Even a functional-first approach, as is often seen in <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> F# code bases, can simplify your logging efforts. The good news is that you can adopt a similar architecture in object-oriented code. You don't even have to compromise the design. </p> <p> I've worked on big C# code bases where we logged all the impure actions. It was typically less than a dozen impure actions per HTTP request. When there was a problem in production, I could usually reproduce what caused it based on the logs. </p> <p> You don't have to <em>overlog</em> to be able to troubleshoot your production code. Log the data that matters, and only that. Log the impure inputs and outputs. </p> <p> <strong>Next:</strong> <a href="/2020/03/30/repeatable-execution-in-haskell">Repeatable execution in Haskell</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="a7018cf7caa54fc8962e7dbdea833f94"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#a7018cf7caa54fc8962e7dbdea833f94">#</a></div> <div class="comment-content"> <p> I like the simplicity of "<em>log the impure inputs and outputs</em>", and logging to ensure repeatability. But consider a common workflow: Load a (DDD) aggregate from DB, call pure domain logic it, and store the result. </p> <p> The aggregate may be very complex, e.g. an order with not just many properties itself, but also many sub-entities (line items, etc.) and value objects. In order to get repeatability, you need to log the entire aggregate that was loaded. This is hard/verbose (don't get too tempted by F#'s nice stringification of records and unions – you'll want to avoid logging sensitive information), and you'll still end up with huge multi-line dumps in your log (say, 10 lines for the order and 10 lines per line item = 100 lines for an order with 9 line items, for a single operation / log statement). </p> <p> Intuitively to me, that seems like a silly amount of logging, but I can't see any alternative if you want to get full repeatability in order to debug problems. Thoughts? </p> </div> <div class="comment-date">2020-04-02 10:21 UTC</div> </div> <div class="comment" id="74a774a447f246d0a00d7d2640b9fa96"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#74a774a447f246d0a00d7d2640b9fa96">#</a></div> <div class="comment-content"> <blockquote> ...you'll want to avoid logging sensitive information... </blockquote> <p> If your application caches sensitive information, then it would be reasonble to only cache an encrypted version. That way, logging the information is not a security issue and neither is holding that infomration in memory (where malware could read it via <a href="https://en.wikipedia.org/wiki/Memory-scraping_malware">memory-scraping</a>). </p> <blockquote> ...you'll still end up with huge multi-line dumps in your log... </blockquote> <p> Not all logging is meant to be directly consumed by humans. <a href="https://github.com/serilog/serilog/wiki/Structured-Data">Structued logging</a> is makes it easy for computers to consume logging events. For an event sourcing architecture (such as the Elm architecture), one can record all events and then create tooling to allow playback. I hope Elmish.WPF gets something like this <a href="https://github.com/elmish/elmish/issues/194#issuecomment-549322498">if/when some of the Fable debugging tools are ported to F#</a>. </p> </div> <div class="comment-date">2020-04-04 19:49 UTC</div> </div> <div class="comment" id="2d78fcdcc4474b3fada0e0a089679c0c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2d78fcdcc4474b3fada0e0a089679c0c">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. There's two different concerns, as far as I can see. I'd like to start with the size issue. </p> <p> The size of software data is unintuitive to the human brain. A data structure that looks overwhelmingly vast to us is nothing but a few hundred kilobytes in raw data. I often have that discussion regarding API design, but the same arguments could apply to logging. How much would 100 lines of structured JSON entail? </p> <p> Let's assume some that JSON properties are numbers (prices, quantities, IDs, etcetera.) while others could be unbounded strings. Let's assume that the numbers all take up four bytes, while Unicode strings are each 100 bytes on average. The average byte size of a 'line' could be around 50 bytes, depending on the proportion of numbers to strings. 100 lines, then, would be 5,000 bytes, or around 5 kB. </p> <p> Even if data structures were a few orders of magnitude larger, that in itself wouldn't keep me up at night. </p> <p> Of course, that's not the whole story. The <em>volume</em> of data is also important. If you log hundred such entries every second, it obviously adds up. It could be prohibitive. </p> <p> That scenario doesn't fit my experience, but for the sake of argument, let's assume that that's the case. What's the alternative to logging the impure operations? </p> <p> You can decide to log less, but that has to be an explicit architectural decision, because if you do that, there's going to be behaviour that you can't reproduce. Logging all impure operations is the minimum amount of logging that'll guarantee that you can fully reproduce all behaviour. You may decide that there's behaviour that you don't need to be able to reconstruct, but I think that this ought to be an explicit decision. </p> <p> There may be a better alternative. It also addresses the issue regarding sensitive data. </p> <p> Write pure functions that take as little input as possible, and produce as little output as possible. In the realm of security design there's the concept of <a href="https://martinfowler.com/bliki/Datensparsamkeit.html">datensparsamkeit</a> (data frugality). Only deal with the data you really need. </p> <p> Does that pure function really need to take as input an entire aggregate, or would a smaller projection do? If the latter, implement the impure operation so that it returns the projection. That's a smaller data set, so less to log. </p> <p> The same goes for sensitive input. Perhaps instead of a <a href="/2018/12/10/danish-cpr-numbers-in-f">CPR number</a> the function only needs an age value. If so, then you only need to log the age. </p> <p> These are deliberate design decision you must take. You don't get such solutions for free, but you can if you will. </p> </div> <div class="comment-date">2020-04-09 15:06 UTC</div> </div> <div class="comment" id="77d77a57bef8407bb7dfd08f6305236b"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#77d77a57bef8407bb7dfd08f6305236b">#</a></div> <div class="comment-content"> <p> Thank you for the reply. It makes sense that this should be a deliberate design decision. </p> <p> I'm working full-time with F# myself, and would be very interested in seeing how you think this could be solved in F#. In this series, you are demonstrating solutions for Haskell (free monads) and C# (dependency injection), but as you have alluded to previously on your blog, neither of those are idiomatic solutions in F# (free monads are cumbersome without higher-kinded types, and DI primarily fits an object-oriented architecture). </p> <p> I realize you may not choose to write another blog post in this series tackling F# (though it would nicely "fill the gap" between Haskell and C#, and you're definitely the one to write it!), but even a few keywords/pointers would be helpful. </p> </div> <div class="comment-date">2020-04-10 20:54 UTC</div> </div> <div class="comment" id="ed430ce7df87483dbce0f91d498b561e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ed430ce7df87483dbce0f91d498b561e">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. I am, indeed, not planning to add another article to this small series. Not that writing the article itself would be too much trouble, but in order to stay on par with the two other articles, I'd have to develop the corresponding F# code base. That currently doesn't look like it'd be the best use of my time. </p> <p> In F# you can <a href="/2017/01/30/partial-application-is-dependency-injection">use partial application for dependency injection</a>. I hope that nothing I wrote gave the impression that this isn't <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> F#. What I've demonstrated is that it isn't <a href="/2018/11/19/functional-architecture-a-definition">functional</a>, but since F# is a multiparadigmatic language, that's still fine. </p> <p> The <a href="/2020/04/06/repeatable-execution-in-c">C# example in this article series</a> shows what's essentially an <a href="/2020/03/02/impureim-sandwich">impureim sandwich</a>, so it shouldn't be too hard to translate that architecture to F#. It's already quite functional. </p> </div> <div class="comment-date">2020-04-14 8:03 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Conway's Law: latency versus throughput https://blog.ploeh.dk/2020/03/16/conways-law-latency-versus-throughput 2020-03-16T06:46:00+00:00 Mark Seemann <div id="post"> <p> <em>Organising work in one way optimises for low latency; in another for throughput.</em> </p> <p> It's a cliché that the software industry is desperate for talent. I also believe that it's a myth. As I've <a href="/2015/12/04/the-rules-of-attraction-location">previously observed</a>, the industry seems desperate for talent <em>within commute range</em>. The implication is that although we perform some of the most intangible and digitised work imaginable, we're expected to be physically present in an office. </p> <p> Since 2014 I've increasingly been working from home, and I love it. I also believe that it's an efficient way to develop software, but not only for the reasons usually given. </p> <p> I believe that distributed, asynchronous software development optimises throughput, but may sacrifice reaction time (i.e. increase latency). </p> <h3 id="dda9b0cd03e941c79365f6ff81969107"> The advantages of working in an office <a href="#dda9b0cd03e941c79365f6ff81969107" title="permalink">#</a> </h3> <p> It's easy to criticise office work, but if it's so unpopular, why is it still the mainstream? </p> <p> I think that there's a multitude of answers to that question. One is that this may be the only way that management can imagine. Since programming is so intangible, it's impossible to measure productivity. What a manager <em>can</em> do, though, is to watch who arrives early, who's the last to leave, and who seems to be always in front of his or her computer, or in a meeting, and so on. </p> <p> Another answer to the question is that people actually <em>like</em> working together. I currently advice <a href="https://idq.dk">IDQ</a> on software development principles and architecture. They have a tight-knit development team. The first day I visited them, I could feel a warm and friendly vibe. I've been visiting them regularly for about a year, now, and the first impression has proven correct. As we Danes say, that work place is positively <a href="https://en.wikipedia.org/wiki/Hygge">hyggelig</a>. </p> <p> Some people also prefer to go to the office to have a formal boundary between their professional and private lives. </p> <p> Finally, if you're into agile software development, you've probably heard about the benefits of <em>team co-location</em>. </p> <p> When the team is located in the same room, working towards the same goals, communication is <em>efficient</em> - or is it? </p> <p> You can certainly get answers to your questions quickly. All you have to do is to <em>interrupt</em> the person who can answer. If you don't know who that is, you just interrupt everybody until you've figured it out. While offices are interruption factories (as <a href="https://dhh.dk">DHH</a> puts it), this style of work can reduce latency. </p> <p> If you explicitly follow e.g. <a href="http://amzn.to/108Pxzb">lean software development</a> and implement something like <em>one-piece flow</em>, you can reduce your cycle time. The less delay between activities, the faster you can deliver value. Once you've delivered one piece (e.g. a <em>feature</em>), you move on to the next. </p> <p> <img src="/content/binary/one-piece-flow.png" alt="Alternating blocks of activities and delays going from left to right."> </p> <p> If this is truly the goal, then putting all team members in the same office makes sense. You don't get higher communications bandwidth than when you're physically together. All the subtle intonations of the way your colleagues speak, the non-verbal cues, etcetera are there if you know how to interpret them. </p> <h3 id="da39de4510544526b6234fa7d75e50bc"> Consequences of team co-location <a href="#da39de4510544526b6234fa7d75e50bc" title="permalink">#</a> </h3> <p> I've seen team co-location work for small teams. People can <a href="https://en.wikipedia.org/wiki/Pair_programming">pair program</a> or even <a href="https://en.wikipedia.org/wiki/Mob_programming">mob program</a>. You can easily draw on the expertise of your co-workers. It does require, however, that everyone respects boundaries. </p> <p> It's a balancing act. You may get your answer sooner, but your interruption could break your colleague's concentration. The net result could be negative productivity. </p> <p> While I've seen team co-location work, I've seen it fail more frequently. There are many reasons for this. </p> <p> First, there's all the interruptions. Most programmers don't like being interrupted. </p> <p> Second, the opportunity for ad-hoc communication easily leads to poor software architecture. This follows from <a href="https://en.wikipedia.org/wiki/Conway%27s_law">Conway's law</a>, which argues that <blockquote> <p> "Any organization that designs a system [...] will inevitably produce a design whose structure is a copy of the organization's communication structure." </p> <footer><cite><a href="http://www.melconway.com/Home/Committees_Paper.html">Melvin Conway</a></cite></footer> </blockquote> </p> <p> I know that it's not a law in any rigid sense of the word, but it can often be fruitful to keep an eye out for this sort of interaction. Based on my experience, it seems to happen often. </p> <p> Ad-hoc office communication leads to ad-hoc communication structures in the code. There's typically little explicit architecture. Knowledge is in the heads of people. </p> <p> Such an organisation tends to have an oral culture. There's no permanence of knowledge, no emphasis on readability of code (because you can always ask someone if there's code you don't understand), and meetings all the time. </p> <p> I once worked as a consultant for a company where there was only one old-timer around. He spent most of his time in meetings, because he knew all the intricate details of how everything worked and talked together, and other people needed to know. </p> <p> After I'd been involved with that (otherwise wonderful) company on and off for a few years, I accumulated some knowledge as well, and people wanted to have meetings with me. </p> <p> In the beginning, I obliged. Then it turned out that a week after I'd had a meeting, I'd be called to what would essentially be <em>the same</em> meeting again. Why? Because some other stakeholder heard about the first meeting and decided that he or she also required that information. The solution? Call another meeting. </p> <p> My counter-move was to begin to write things down. When people would call a meeting, I'd ask for an agenda. That alone filtered away more than half of the meetings. When I did receive an agenda, I could often reply: </p> <p> "Based on the agenda, I believe you'll find everything you need to know <em>here</em>. If not, please let me know what's missing so that I can update the document" </p> <p> I'd attach said document. By doing that, I eliminated ninety percent of my meetings. </p> <p> Notice what I did. I changed the communication structure - at least locally around me. Soon after, I went remote with that client, and had a few successful years doing that. </p> <p> I hope that the previous section outlined that working in an office can be effective, but as I've now outlined, it can also be dysfunctional. </p> <p> If you truly must deliver as soon as possible, because if you don't, the organisation isn't going to be around in five years, office work, with its low communications latency may be the best option. </p> <h3 id="e9958e6f5b75431987f76b9d084b1e30"> Remote office work <a href="#e9958e6f5b75431987f76b9d084b1e30" title="permalink">#</a> </h3> <p> I often see companies advertise for programmers. When remote work is an option, it often comes with the qualification that it must be within a particular country, or a particular time zone. </p> <p> There can be legal or bureaucratic reasons why a company only wants to hire within a country. I get that, but I consider a time zone requirement a danger sign. The same goes for <em>"we use Slack"</em> or whatever other 'team room' instant messaging technology is cool these days. </p> <p> That tells me that while the company allows people to be physically not in the office, they must still obey office hours. This indicates to me that communication remains ad-hoc and transient. Again, <a href="/2019/03/04/code-quality-is-not-software-quality">code quality</a> suffers. </p> <p> These days, because of the Corona virus, many organisations deeply entrenched in the oral culture of co-location find that they must now work as a distributed team. They try to address the situation by setting up day-long video conference calls. </p> <p> It may work in an office, but it's not the best fit for a distributed team. </p> <h3 id="1dd79d5d27e44a4da134a08a0df29fc4"> Distributed asynchronous software development <a href="#1dd79d5d27e44a4da134a08a0df29fc4" title="permalink">#</a> </h3> <p> Decades of open-source development has shown another way. Successful open-source software (OSS) projects are distributed and use asynchronous communication channels (mailing lists, issue trackers). It's worth considering the causation. I don't think anyone sat down and decided to do it this way in order to be successful. I think that the OSS projects that became successful became successful exactly because they organised work that way. </p> <p> When contributions are voluntary, you have to cast a wide net. A successful OSS project should accept contributions from around the world. If an excellent contribution from Japan falters because the project team is based in the US, and immediate, real-time communication is required, then that project has odds against it. </p> <p> An OSS project that works asynchronously can receive contributions from any time zone. The disadvantage can be significant communication lag. </p> <p> If you get a contribution from Australia, but you're in Europe, you may send a reply asking for clarifications or improvements. At the time you do that, the contributor may have already gone to bed. He or she isn't going to reply later, at which time you've gone to bed. </p> <p> It can take days to get anything done. That doesn't sound efficient, and if you're in a <em>one-piece flow</em> mindset it isn't. You need to enable parallel development. If you do that, you can work on something else while you wait for your asynchronous collaborator to respond. </p> <p> <img src="/content/binary/one-piece-flow-vs-paralle-development.png" alt="A diagram juxtaposing two pieces finished one after the other, against two pieces finished in parallel."> </p> <p> In this diagram, the wait-times in the production of one piece (e.g. one feature) can be used to move forward with another feature. The result is that you may actually be able to finish both tasks sooner than if you stick strictly to one-piece flow. </p> <p> Before you protest: in reality, delay times are much longer than implied by the diagram. An activity could be something as brief as responding to a request for more information. You may be able to finish this activity in 30 minutes, whereafter the delay time is another twenty hours. Thus, in order to keep throughput comparable, you need to juggle not two, but dozens of parallel processes. </p> <p> You may also feel the urge to protest that the diagram postulates a false dichotomy. That's not my intention. Even with co-location, you could do parallel development. </p> <p> There's also the argument that parallel development requires context switching. That's true, and it comes with overhead. </p> <p> My argument is only this: if you decide to shift to an asynchronous process, then I consider parallel development essential. Even with parallel development, you can't get the same (low) latency as is possible in the office, but you may be able to get better throughput. </p> <p> This again has implications for software architecture. Parallel development works when features can be developed independently of each other - when there's only minimal dependencies between various areas of the code. </p> <p> Conway's law is relevant here as well. If you decouple the communication between various parts of the system, you can also decouple the development of said parts. Ultimately, the best fit for a distributed, asynchronous software development <em>process</em> may be a distributed, asynchronous system. </p> <h3 id="f02f525b49564f1fadfa27d471f41e06"> Quadrants <a href="#f02f525b49564f1fadfa27d471f41e06" title="permalink">#</a> </h3> <p> This is the point where, if this was a Gartner report, it'd include a 2x2 table with four quadrants. It's not, but I'll supply it anyway: <table> <thead> <tr> <td></td> <td>Synchronous</td> <td>Asynchronous</td> </tr> </thead> <tbody> <tr> <td>Distributed</td> <td>Virtual office</td> <td>OSS-like parallel development</td> </tr> <tr> <td>Co-located</td> <td>Scrum, XP, etc.</td> <td>Emailing the person next to you</td> </tr> </tbody> </table> The current situation where the oral, co-located teams find themselves forced to work remotely is what I call the <em>virtual office</em>. If the Corona outbreak is over in weeks, it's probably best to just carry on in that way. I don't, however, think it's a sustainable process model. There's still too much friction involved in having to be connected to a video conference call for 8 hours each day. Long-term, I think that a migration towards a distributed, asynchronous process would be beneficial. </p> <p> I've yet to discuss the fourth quadrant. This is where people sit next to each other, yet still email each other. That's just weird. Like the <em>virtual office</em>, I don't think it's a long-term sustainable process. The advantages of just talking to each other is just too great. If you're co-located, ad-hoc communication is possible, so that's where the software architecture will gravitate as well. Again, Conway's law applies. </p> <p> If you want to move towards a sustainable distributed process, you should consider adjusting everything accordingly. A major endeavour in that shift involves migrating from an oral to a written culture. <a href="https://basecamp.com/guides/how-we-communicate">Basecamp has a good guide to get you started.</a> </p> <h3 id="d0381a7649cb40deab87c4388001f339"> Your writer reveals himself <a href="#d0381a7649cb40deab87c4388001f339" title="permalink">#</a> </h3> <p> I intend this to be an opinion piece. It's based on a combination of observations made by others, mixed with my personal experiences, but I also admit that it's coloured by my personal preferences. I strongly prefer distributed, asynchronous processes with an emphasis on written communication. Since this blog contains more than 500 articles, it should hardly come as a surprise to anyone that I'm a prolific writer. </p> <p> I've had great experiences with distributed, asynchronous software development. One such experience was the decade I led the <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> open-source project. Other experiences include a handful of commercial, closed-source projects where I did the bulk of the work remotely. </p> <p> This style of work benefits my employer. By working asynchronously, I have to document what I do, and why I do it. I leave behind a trail of text artefacts other people can consult when I'm not available. </p> <p> I like asynchronous processes because they liberate me to work when I want to, where I want to. I take advantage of this to go for a run during daylight hours (otherwise an issue during Scandinavian winters), to go grocery shopping outside of rush hour, to be with my son when he comes home from school, etcetera. I compensate by working at other hours (evenings, weekends). This isn't a lifestyle that suits everyone, but it suits me. </p> <p> This preference produces a bias in the way that I see the world. I don't think I can avoid that. Like DHH I view offices as interruption factories. I self-identify as an introvert. I like being alone. </p> <p> Still, I've tried to describe some forces that affect how work is done. I've tried to be fair to co-location, even though I don't like it. </p> <h3 id="b8e64a09f56345c883b596ff612f942f"> Conclusion <a href="#b8e64a09f56345c883b596ff612f942f" title="permalink">#</a> </h3> <p> Software development with a co-located team can be efficient. It offers the benefits of high-bandwidth communication, pair programming, and low-latency decision making. It also implies an oral tradition. Knowledge has little permanence and the team is vulnerable to key team members going missing. </p> <p> While such a team organisation can work well when team members are physically close to each other, I believe that this model comes under pressure when team members work remotely. I haven't seen the oral, ad-hoc team process work well in a distributed setting. </p> <p> Successful distributed software development is asynchronous and based on a literate culture. It only works if the software architecture allows it. Code has to be decoupled and independently deployable. If it is, though, you can perform work in parallel. Conway's law still applies. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Polymorphic Builder https://blog.ploeh.dk/2020/03/09/polymorphic-builder 2020-03-09T06:47:00+00:00 Mark Seemann <div id="post"> <p> <em>Keeping illegal states unrepresentable with the Builder pattern.</em> </p> <p> As a reaction to <a href="/2020/02/10/builder-isomorphisms">my article on Builder isomorphisms</a> Tyson Williams asked: <blockquote> <p> "If a <code>GET</code> or <code>DELETE</code> request had a body or if a <code>POST</code> request did not have a body, then I would suspect that such behavior was a bug. </p> <p> "For the sake of a question that I would like to ask, let's suppose that a body must be added if and only if the method is <code>POST</code>. Under this assumption, <code>HttpRequestMessageBuilder</code> can create invalid messages. For example, it can create a <code>GET</code> request with a body, and it can create a <code>POST</code> request without a body. Under this assumption, how would you modify your design so that only valid messages can be created?" </p> </blockquote> I'm happy to receive that question, because I struggled to find a compelling example of a <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder</a> where polymorphism seems warranted. Here, it does. </p> <h3 id="49a6ffa95ffd44dfbf7c1160879af8e9"> Valid combinations <a href="#49a6ffa95ffd44dfbf7c1160879af8e9" title="permalink">#</a> </h3> <p> Before showing code, I think a few comments are in order. As far as I'm aware, the HTTP specification doesn't prohibit weird combinations like a <code>GET</code> request with a body. Still, such a combination is so odd that it seems fair to design an API to prevent this. </p> <p> On the other hand I think that a <code>POST</code> request without a body should still be allowed. It's not too common, but there are edge cases where this combination is valid. If you want to cause a side effect to happen, a <code>GET</code> is inappropriate, but sometimes all you want do to is to produce an effect. In the <a href="http://amzn.to/YFnkRg">Restful Web Services Cookbook</a> Subbu Allamaraju gives this example of a <em>fire-and-forget bulk task:</em> </p> <p> <pre>POST /address-correction?before=2010-01-01 HTTP/1.1</pre> </p> <p> As he puts it, <em>"in essence, the client is "flipping a switch" to start the work."</em> </p> <p> I'll design the following API to allow this combination, also because it showcases how that sort of flexibility can still be included. On the other hand, I'll prohibit the combination of a request body in a <code>GET</code> request, as Tyson Williams suggested. </p> <h3 id="5509ba650064423097e277f6f532d578"> Expanded API <a href="#5509ba650064423097e277f6f532d578" title="permalink">#</a> </h3> <p> I'll expand on the <code>HttpRequestMessageBuilder</code> example shown in the previous article. As <a href="/2020/02/17/builder-as-a-monoid#6d834828809b4ac2b36f59bb244e5952">outlined in another article</a>, apart from the <code>Build</code> method the Builder really only has two capabilities: <ul> <li>Change the HTTP method</li> <li>Add (or update) a JSON body</li> </ul> These are the two combinations we now need to model differently. If I do that now, there'd be no other affordances offered by the Builder API. In order to make the example more explicit, I'll first add a pair of new capabilities: <ul> <li>Add or change the <code>Accept</code> header</li> <li>Add or change a <code>Bearer</code> token</li> </ul> When I do that, the <code>HttpRequestMessageBuilder</code> class now looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;url; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">object</span>?&nbsp;jsonBody; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;acceptHeader; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;bearerToken; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="font-weight:bold;color:#1f377f;">url</span>))&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>(<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:#2b91af;">HttpMethod</span>.Get,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>)&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpMethod</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">method</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">acceptHeader</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">bearerToken</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.url&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">method</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.jsonBody&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.acceptHeader&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">acceptHeader</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.bearerToken&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">bearerToken</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpMethod</span>&nbsp;Method&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithMethod</span>(<span style="color:#2b91af;">HttpMethod</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jsonBody, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acceptHeader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bearerToken); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">object</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acceptHeader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bearerToken); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithAcceptHeader</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newAcceptHeader</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jsonBody, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">newAcceptHeader</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bearerToken); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithBearerToken</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newBearerToken</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jsonBody, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acceptHeader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">newBearerToken</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Build</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>(Method,&nbsp;url); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">BuildBody</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AddAcceptHeader</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AddBearerToken</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BuildBody</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(jsonBody&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#2b91af;">JsonConvert</span>.<span style="color:#74531f;">SerializeObject</span>(jsonBody); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Content&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StringContent</span>(<span style="font-weight:bold;color:#1f377f;">json</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Content.Headers.ContentType.MediaType&nbsp;=&nbsp;<span style="color:#a31515;">&quot;application/json&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddAcceptHeader</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(acceptHeader&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Headers.Accept.<span style="font-weight:bold;color:#74531f;">ParseAdd</span>(acceptHeader); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddBearerToken</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(bearerToken&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Headers.Authorization&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AuthenticationHeaderValue</span>(<span style="color:#a31515;">&quot;Bearer&quot;</span>,&nbsp;bearerToken); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that I've added the methods <code>WithAcceptHeader</code> and <code>WithBearerToken</code>, with supporting implementation. So far, those are the only changes. </p> <p> It enables you to build HTTP request messages like this: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">WithBearerToken</span>(<span style="color:#a31515;">&quot;cGxvZWg=&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> Or this: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">WithMethod</span>(<span style="color:#2b91af;">HttpMethod</span>.Post) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">new</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">NewGuid</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2021-02-09&nbsp;19:15:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Hervor&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;hervor@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">WithAcceptHeader</span>(<span style="color:#a31515;">&quot;application/vnd.foo.bar+json&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">WithBearerToken</span>(<span style="color:#a31515;">&quot;cGxvZWg=&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> It still doesn't address Tyson Williams' requirement, because you can build an HTTP request like this: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">new</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">NewGuid</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2020-03-22&nbsp;19:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Ælfgifu&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;ælfgifu@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity&nbsp;=&nbsp;1&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> Recall that the default HTTP method is <code>GET</code>. Since the above code doesn't specify a method, it creates a <code>GET</code> request with a message body. That's what shouldn't be possible. Let's <a href="https://blog.janestreet.com/effective-ml-video">make illegal states unrepresentable</a>. </p> <h3 id="95ac7d907e2b461ea56247bfe4eec6da"> Builder interface <a href="#95ac7d907e2b461ea56247bfe4eec6da" title="permalink">#</a> </h3> <p> Making illegal states unrepresentable is a catch phrase coined by <a href="https://twitter.com/yminsky">Yaron Minsky</a> to describe advantages of statically typed functional programming. Unintentionally, it also describes a fundamental tenet of object-oriented programming. In <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a> Bertrand Meyer describes object-oriented programming as the discipline of guaranteeing that an object can never be in an invalid state. </p> <p> In the present example, we can't allow an arbitrary HTTP Builder object to afford an operation to add a body, because that Builder object might produce a <code>GET</code> request. On the other hand, there are operations that are <em>always</em> legal: adding an <code>Accept</code> header or a <code>Bearer</code> token. Because these operations are always legal, they constitute a shared API. Extract those to an interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithAcceptHeader</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newAcceptHeader</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithBearerToken</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newBearerToken</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Build</span>(); }</pre> </p> <p> Notice that both the <code>With[...]</code> methods return the new interface. Any <code>IHttpRequestMessageBuilder</code> must implement the interface, but is free to support other operations not part of the interface. </p> <h3 id="23af7eedc51441118c8bd8819f9b3ce3"> HTTP GET Builder <a href="#23af7eedc51441118c8bd8819f9b3ce3" title="permalink">#</a> </h3> <p> You can now implement the interface to build HTTP <code>GET</code> requests: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">HttpGetMessageBuilder</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;url; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;acceptHeader; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;bearerToken; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpGetMessageBuilder</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="font-weight:bold;color:#1f377f;">url</span>))&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpGetMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>)&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">HttpGetMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">acceptHeader</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">bearerToken</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.url&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.acceptHeader&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">acceptHeader</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.bearerToken&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">bearerToken</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithAcceptHeader</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newAcceptHeader</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpGetMessageBuilder</span>(url,&nbsp;<span style="font-weight:bold;color:#1f377f;">newAcceptHeader</span>,&nbsp;bearerToken); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithBearerToken</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newBearerToken</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpGetMessageBuilder</span>(url,&nbsp;acceptHeader,&nbsp;<span style="font-weight:bold;color:#1f377f;">newBearerToken</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Build</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>(<span style="color:#2b91af;">HttpMethod</span>.Get,&nbsp;url); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AddAcceptHeader</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AddBearerToken</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddAcceptHeader</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(acceptHeader&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Headers.Accept.<span style="font-weight:bold;color:#74531f;">ParseAdd</span>(acceptHeader); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddBearerToken</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(bearerToken&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Headers.Authorization&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AuthenticationHeaderValue</span>(<span style="color:#a31515;">&quot;Bearer&quot;</span>,&nbsp;bearerToken); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that the <code>Build</code> method hard-codes <code>HttpMethod.Get</code>. When you're using an <code>HttpGetMessageBuilder</code> object, you can't modify the HTTP method. You also can't add a request body, because there's no API that affords that operation. </p> <p> What you <em>can</em> do, for example, is to create an HTTP request with an <code>Accept</code> header: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpGetMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">WithAcceptHeader</span>(<span style="color:#a31515;">&quot;application/vnd.foo.bar+json&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> This creates a request with an <code>Accept</code> header, but no <code>Bearer</code> token. </p> <h3 id="b96c590c45394c1d86ec0ff841b82a52"> HTTP POST Builder <a href="#b96c590c45394c1d86ec0ff841b82a52" title="permalink">#</a> </h3> <p> As a peer to <code>HttpGetMessageBuilder</code> you can implement the <code>IHttpRequestMessageBuilder</code> interface to support <code>POST</code> requests: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;url; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">object</span>?&nbsp;jsonBody; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;acceptHeader; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>?&nbsp;bearerToken; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="font-weight:bold;color:#1f377f;">url</span>))&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>)&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:blue;">object</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="font-weight:bold;color:#1f377f;">url</span>),&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:blue;">object</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>(<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>,&nbsp;<span style="color:blue;">null</span>,&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">acceptHeader</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">bearerToken</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.url&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.jsonBody&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.acceptHeader&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">acceptHeader</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.bearerToken&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">bearerToken</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithAcceptHeader</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newAcceptHeader</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jsonBody, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">newAcceptHeader</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bearerToken); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithBearerToken</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newBearerToken</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jsonBody, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acceptHeader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">newBearerToken</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Build</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>(<span style="color:#2b91af;">HttpMethod</span>.Post,&nbsp;url); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">BuildBody</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AddAcceptHeader</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">AddBearerToken</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BuildBody</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(jsonBody&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#2b91af;">JsonConvert</span>.<span style="color:#74531f;">SerializeObject</span>(jsonBody); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Content&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StringContent</span>(<span style="font-weight:bold;color:#1f377f;">json</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Content.Headers.ContentType.MediaType&nbsp;=&nbsp;<span style="color:#a31515;">&quot;application/json&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddAcceptHeader</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(acceptHeader&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Headers.Accept.<span style="font-weight:bold;color:#74531f;">ParseAdd</span>(acceptHeader); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddBearerToken</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(bearerToken&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Headers.Authorization&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AuthenticationHeaderValue</span>(<span style="color:#a31515;">&quot;Bearer&quot;</span>,&nbsp;bearerToken); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class affords various constructor overloads. Two of them don't take a JSON body, and two of them do. This supports both the case where you do want to supply a request body, and the edge case where you don't. </p> <p> I didn't add an explicit <code>WithJsonBody</code> method to the class, so you can't change your mind once you've created an instance of <code>HttpPostMessageBuilder</code>. The only reason I didn't, though, was to save some space. You can add such a method if you'd like to. As long as it's not part of the interface, but only part of the concrete <code>HttpPostMessageBuilder</code> class, illegal states are still unrepresentable. You can represent a <code>POST</code> request with or without a body, but you can't represent a <code>GET</code> request with a body. </p> <p> You can now build requests like this: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpPostMessageBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">NewGuid</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2021-02-09&nbsp;19:15:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Hervor&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;hervor@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">WithAcceptHeader</span>(<span style="color:#a31515;">&quot;application/vnd.foo.bar+json&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">WithBearerToken</span>(<span style="color:#a31515;">&quot;cGxvZWg=&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> This builds a <code>POST</code> request with both a JSON body, an <code>Accept</code> header, and a <code>Bearer</code> token. </p> <h3 id="4104677292a94451a896e6bee22df93a"> Is polymorphism required? <a href="#4104677292a94451a896e6bee22df93a" title="permalink">#</a> </h3> <p> In <a href="/2020/02/10/builder-isomorphisms">my previous Builder article</a>, I struggled to produce a compelling example of a polymorphic Builder. It seems that I've now mended the situation. Or have I? </p> <p> Is the <code>IHttpRequestMessageBuilder</code> interface really required? </p> <p> Perhaps. It depends on your usage scenarios. I can actually delete the interface, and none of the usage examples I've shown here need change. </p> <p> On the other hand, had I written helper methods against the interface, obviously I couldn't just delete it. </p> <p> The bottom line is that polymorphism can be helpful, but it still strikes me as being non-essential to the Builder pattern. </p> <h3 id="07ed5f99efb549509a3af0c7b510553a"> Conclusion <a href="#07ed5f99efb549509a3af0c7b510553a" title="permalink">#</a> </h3> <p> In this article, I've shown how to guarantee that Builders never get into invalid states (according to the rules we've arbitrarily established). I used the common trick of <a href="/2011/05/30/DesignSmellDefaultConstructor">using constructors for object initialisation</a>. If a constructor completes without throwing an exception, we should expect the object to be in a valid state. The price I've paid for this design is some code duplication. </p> <p> You may have noticed that there's duplicated code between <code>HttpGetMessageBuilder</code> and <code>HttpPostMessageBuilder</code>. There are ways to address that concern, but I'll leave that as an exercise. </p> <p> For the sake of brevity, I've only shown examples written as Immutable Fluent Builders. You can refactor all the examples to mutable Fluent Builders or to the original Gang-of-Four Builder pattern. This, too, will remain an exercise for the interested reader. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c1ea86ea39814f278e1a19f82b45622b"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#c1ea86ea39814f278e1a19f82b45622b">#</a></div> <div class="comment-content"> <blockquote> I'm happy to receive that question, because I struggled to find a compelling example of a Builder where polymorphism seems warranted. Here, it does. </blockquote> <p> I know of essentially one occurrence in .NET. Starting with <code>IEnumerable&lt;T&gt;</code>, calling either of the extension methods <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderby?view=netframework-4.8">OrderBy</a> or <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderbydescending?view=netframework-4.8">OrderByDescending</a> returns <code>IOrderedEnumerable&lt;T&gt;</code>, which has the additional extension methods <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.thenby?view=netframework-4.8">ThenBy</a> and <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.thenbydescending?view=netframework-4.8">ThenByDescending</a>. </p> <p> Quoting your recent <a href="https://blog.ploeh.dk/2020/02/10/builder-isomorphisms/">Builder isomorphisms</a> post. </p> <blockquote> The Builder pattern isn't useful only because it enables you to "separate the construction of a complex object from its representation." It's useful because it enables you to present an API that comes with good default behaviour, but which can be tweaked into multiple configurations. </blockquote> <p> I also find the builder pattern useful because its methods typically accept one argument one at a time. The builders in your recent posts are like this. The <code>OrderBy</code> and <code>ThenBy</code> methods and their <code>Descending</code> alternatives in .NET are also examples of this. </p> <p> However, some of the builders in your recent posts have some constructors that take multiple arguments. That is the situation that I was trying to address when <a href="https://blog.ploeh.dk/2017/09/11/test-data-without-builders/#b30ddcf557964bf2a7b63c0e247294e1">I asked</a> </p> <blockquote> Have you ever written a builder that accepted multiple arguments one at a time none of which have reasonable defaults? </blockquote> <p> This could be a <a href="https://blog.ploeh.dk/2020/01/13/on-doing-katas/">kata variation</a>: all public functions accept at most one argument. So <code>Foo(a, b)</code> would not be allowed but <code>Foo.WithA(a).WithB(b)</code> would. In an issue on this blog's GitHub, jaco0646 nicely summerized the reasoning that could lead to applying this design philosophy to production code by <a href="https://github.com/ploeh/ploeh.github.com/issues/539#issuecomment-585837934">saying</a> </p> <blockquote> Popular advice for a builder with required parameters is to put those in a constructor; but with more than a handful of required parameters, we return to the original problem: too much complexity in a constructor. </blockquote> <p> That comment by jaco0646 also supplied names by which this type of design is known. Those names (with the same links from the comment) are <a href="https://blog.jayway.com/2012/02/07/builder-pattern-with-a-twist/">Builder with a twist</a> or <a href="https://rdafbn.blogspot.com/2012/07/step-builder-pattern_28.html">Step Builder</a>. This is great, because I didn't have any good names. (I vaguely recall once thinking that another name was "progressive API" or "progressive fluent API", but now when I search for anything with "progressive", all I get are false positives for <a href="https://en.wikipedia.org/wiki/Progressive_web_application">progressive web app</a>. </p> <p> When replacing a multi-argument constructor with a sequence of function calls that each accept one argument, care must be taken to ensure that illegal state remains unrepresentable. My general impression is that many libraries have designed such APIs well. The two that I have enough experience with to recommend as good examples of this design are the <a href="https://www.learnentityframeworkcore.com/configuration/fluent-api">fluent configuration API in Entity Framework</a> and <a href="https://fluentassertions.com/introduction">Fluent Assertions</a>. As I <a href="https://blog.ploeh.dk/2017/09/11/test-data-without-builders/#be62acdf925841e09485f4253ab0c5fe">said before</a>, the most formal treatment I have seen about this type of API design was in <a href="https://blog.jooq.org/2012/01/05/the-java-fluent-api-designer-crash-course/">this blog post</a>. </p> </div> <div class="comment-date">2020-03-11 17:57 UTC</div> </div> <div class="comment" id="d31b6312b4204c7e8a1c047ed49cd4e6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d31b6312b4204c7e8a1c047ed49cd4e6">#</a></div> <div class="comment-content"> <p> Tyson, apart from as <a href="/2020/01/13/on-doing-katas">a kata constraint</a>, is there any reason to prefer such a design? </p> <p> I'll be happy to give it a more formal treatment if there's reasonable scenario. Can you think of one? </p> <p> I don't find the motivation given by jaco0646 convincing. If you have more than a handful of required parameters, I agree that that's an issue with complexity, but I don't agree that the solution is to add more complexity on top of it. Builders add complexity. </p> <p> At a glance, though, with something like <code>Foo.WithA(a).WithB(b)</code> it seems to me that you're essentially reinventing <a href="https://en.wikipedia.org/wiki/Currying">currying</a> the hard way around. </p> <p> Related to the overall Builder discussion (but not to currying) you may also find <a href="https://blog.frankel.ch/builder-pattern-finite-state-machine">this article</a> and <a href="https://stackoverflow.com/a/2267467/126014">this Stack Overflow answer</a> interesting. </p> </div> <div class="comment-date">2020-03-13 6:19 UTC</div> </div> <div class="comment" id="b30263d2a8e440e7b89dc23bd9d8d3c6"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#b30263d2a8e440e7b89dc23bd9d8d3c6">#</a></div> <div class="comment-content"> <blockquote> ...is there any reason to prefer such a design? </blockquote> <p> Yes. Just like you, I want to write <a href="https://blog.ploeh.dk/2019/11/04/the-80-24-rule/">small functions</a>. In that post, you suggest an arbitrary maximum of 24 lines. One thing I find fascinating about functional programming is how useful the common functions are (such as <code>map</code>) and how they are implemented in only a few lines (often just one line). There is a correlation between the number of function arguments and the length of the function. So to help control the length of a function, it helps to control the number of arguments to the functions. I think Robert Martin has a similar argument. When talking about functions in chapter 3 of <a href="">Clean Code</a>, his first section is about writing small functions and a later section about function arguments open by saying </p> <blockquote> The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification--and then shouldn't be used anyway. </blockquote> <p> In the C# code <code>a.Foo(b)</code>, <code>Foo</code> is an instance method that "feels" like it only has one argument. In reality, its two inputs are <code>a</code> and <code>b</code>, and that code uses infix notation. The situation is similar in the F# code <code>a |&gt; List.map f</code>. The function <code>List.map</code> (as well as the operator <code>|&gt;</code>) has two arguments and is applied using infix notation. I try to avoid creating functions that have more than two arguments. </p> <blockquote> I don't find the motivation given by jaco0646 convincing. If you have more than a handful of required parameters, I agree that that's an issue with complexity, but I don't agree that the solution is to add more complexity on top of it. Builders add complexity. </blockquote> <p> I am not sure how you are measuring complexity. I like to think that there are two types of complexity: local and global. For the sake of argument, let's suppose <ol> <li>that local complexity is only defined for a function and is the number of arguments of that function and</li> <li>that global complexity is only defined for a entire program and is the number of lines in the program.</li> </ol> I would argue that many required arguments is an issue of local complexity. We can decrease the local complexity at the expense of increasing the global complexity by replacing one function accepting many arguments with several functions accepting fewer arguments. I like to express this idea with the phrase "minimize the maximum (local) complexity". </p> <blockquote> ...you may also find <a href="https://blog.frankel.ch/builder-pattern-finite-state-machine">this article</a> [titled The Builder pattern is a finite state machine]...interesting. </blockquote> <p> Indeed, that is a nice article. Finite state machines/automata (both deterministic and nondeterministic) have the same expressiveness as regular expressions. </p> <blockquote> At a glance, though, with something like <code>Foo.WithA(a).WithB(b)</code> it seems to me that you're essentially reinventing <a href="https://en.wikipedia.org/wiki/Currying">currying</a> the hard way around. </blockquote> <p> It is. As a regular expression, it would be something like <code>AB</code>. I was just trying to give a simple example. The point of the article you shared is that the builder pattern is much more expressive. I have previously shared a <a href="https://blog.jooq.org/2012/01/05/the-java-fluent-api-designer-crash-course/">similar article</a>, but I like yours better. Thanks :) </p> <blockquote> ...you may also find...<a href="https://stackoverflow.com/a/2267467/126014">this Stack Overflow answer</a> interesting. </blockquote> <p> Wow. That is extremely cleaver! I would never thought of that. Thank you very much for sharing. </p> <blockquote> I'll be happy to give it a more formal treatment if there's reasonable scenario. Can you think of one? </blockquote> <p> As I said above, I often try to find ways to minimize the maximum complexity of the code that I write. In this case, the reason that I <a href="https://blog.ploeh.dk/2017/09/11/test-data-without-builders/#cb79773e74624514a1801af115f52f6b">originally asked you about the builder pattern</a> is that I was trying to improve the API for creating a binding in <a href="https://github.com/elmish/Elmish.WPF">Elmish.WPF</a>. The tutorial has a great <a href="https://github.com/elmish/Elmish.WPF/blob/master/TUTORIAL.md#the-elmishwpf-bindings">section about bindings</a>. There are many binding types, and each has multiple ways to create it. Most arguments are required and some are optional. </p> <p> <a href="https://github.com/elmish/Elmish.WPF/issues/87">Here</a> is a closed issue that was created during the transition to the current binding API, which uses method overloading. In an attempt to come up with a better API, <a href="https://github.com/elmish/Elmish.WPF/issues/87#issuecomment-530962855">I suggested</a> that we could use your suggestion to <a href="https://blog.ploeh.dk/2013/10/21/replace-overloading-with-discriminated-unions/">replace overloading with discriminated unions</a>, but my co-maintainer wasn't convinced that it would be better. </p> <p> Three days later, I increased the expressiveness of our bindings in <a href="https://github.com/elmish/Elmish.WPF/pull/118/files">this pull request</a>. Conceptually it was a small change; I added a single optional argument. For a regular expression, such a change is trivial. However, in my case it was a delta of +300 lines of mind-numbingly boring code. </p> <p> I agree with my co-maintainer that the current binding API is pretty good for the user. On the implementation side though, I am not satisfied. I want to find something better without sacrificing (and maybe even improving) the user's experience. </p> </div> <div class="comment-date">2020-03-16 04:03 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Impureim sandwich https://blog.ploeh.dk/2020/03/02/impureim-sandwich 2020-03-02T07:03:00+00:00 Mark Seemann <div id="post"> <p> <em>Pronounced 'impurium sandwich'.</em> </p> <p> <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">Since January 2017</a> I've been singing the praise of the <em>impure/pure/impure</em> sandwich, but I've never published an article that defines the term. I intend this article to remedy the situation. </p> <h3 id="b1755f32e36d4a07b336d1b8fdc1b227"> Functional architecture <a href="#b1755f32e36d4a07b336d1b8fdc1b227" title="permalink">#</a> </h3> <p> In <a href="/2018/11/19/functional-architecture-a-definition">a functional architecture</a> <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> can't call impure actions. On the other hand, as <a href="https://en.wikipedia.org/wiki/Simon_Peyton_Jones">Simon Peyton Jones</a> observed in a lecture, <em>observing the result of pure computation is a side-effect</em>. In practical terms, <em>executing</em> a pure function is also impure, because it happens non-deterministically. Thus, even for a piece of software written in a functional style, the entry point must be impure. </p> <p> While pure functions can't call impure actions, there's no rule to prevent the obverse. Impure actions <em>can</em> call pure functions. </p> <p> Therefore, the best we can ever hope to achieve is an impure entry point that calls pure code and impurely reports the result from the pure function. </p> <p> <img src="/content/binary/impure-pure-impure-sandwich-box.png" alt="A box with a thin red slice on top, a thick green middle, and a thin red slice at the bottom."> </p> <p> The flow of code here goes from top to bottom: <ol> <li>Gather data from impure sources.</li> <li>Call a pure function with that data.</li> <li>Change state (including user interface) based on return value from pure function.</li> </ol> This is the <em>impure/pure/impure</em> sandwich. </p> <h3 id="c568a515c5a04024832079e491432bc4"> Metaphor <a href="#c568a515c5a04024832079e491432bc4" title="permalink">#</a> </h3> <p> The reason I call this a <em>sandwich</em> is that I think that it <em>looks</em> like a sandwich, albeit, perhaps, a rather tall one. According to the <a href="https://en.wikipedia.org/wiki/Sandwich">myth of the sandwich</a>, the <a href="https://en.wikipedia.org/wiki/John_Montagu,_4th_Earl_of_Sandwich">4th Earl of Sandwich</a> was a notorious gambler. While playing cards, he'd order two slices of bread with meat in between. This enabled him to keep playing without greasing the cards. His compatriots would order <em>the same as Sandwich</em>, or simply <em>a Sandwich</em>, and the name stuck. </p> <p> I like the sandwich as a metaphor. The bread is an <em>affordance</em>, in <a href="http://amzn.to/1NVqXQH">the spirit of Donald A. Norman</a>. It enables you to handle the meat without getting your fingers greased. In the same way, I think, impure actions enable you to handle a pure function. They let you invoke and observe the result of it. </p> <h3 id="0aa40b6f0f7c4648ac9a83f8fe2ab449"> Examples <a href="#0aa40b6f0f7c4648ac9a83f8fe2ab449" title="permalink">#</a> </h3> <p> One of the cleanest examples of an <em>impureim sandwich</em> remains <a href="/2017/02/02/dependency-rejection">my original article</a>: </p> <p> <pre><span style="color:#600277;">tryAcceptComposition</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;(Maybe&nbsp;Int) tryAcceptComposition&nbsp;reservation&nbsp;<span style="color:#666666;">=</span>&nbsp;runMaybeT&nbsp;<span style="color:#666666;">$</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;liftIO&nbsp;(<span style="color:#dd0000;">DB</span><span style="color:#666666;">.</span>readReservations&nbsp;connectionString&nbsp;<span style="color:#666666;">$</span>&nbsp;date&nbsp;reservation)</span> <span style="background-color: palegreen;">&nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;<span style="color:#dd0000;">MaybeT</span>&nbsp;<span style="color:#666666;">.</span>&nbsp;return&nbsp;<span style="color:#666666;">.</span>&nbsp;flip&nbsp;(tryAccept&nbsp;<span style="color:#09885a;">10</span>)&nbsp;reservation</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;liftIO&nbsp;<span style="color:#666666;">.</span>&nbsp;<span style="color:#dd0000;">DB</span><span style="color:#666666;">.</span>createReservation&nbsp;connectionString</span></pre> </p> <p> I've here repeated the code, but coloured the background of the impure, pure, and impure parts of the sandwich. </p> <p> I've shown plenty of other examples of this sandwich architecture, recently, for example, while <a href="/2019/12/02/refactoring-registration-flow-to-functional-architecture">refactoring a registration flow</a> in <a href="https://fsharp.org">F#</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;validityOfProof&nbsp;=&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid</span> <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;decision&nbsp;=&nbsp;completeRegistrationWorkflow&nbsp;r&nbsp;validityOfProof</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decision &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.traverseBoth&nbsp;db.CompleteRegistration&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired</span> &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> This last example looks as though the bottom part of the sandwich is larger then the rest of the composition. This can sometimes happen (and, in fact, last line of code is also pure). On the other hand, the pure part in the middle will typically <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use">look like just a single line of code, even when the invoked function performs work of significant complexity</a>. </p> <p> The sandwich is a pattern independent of language. You can also <a href="/2019/02/11/asynchronous-injection">apply it in C#</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date)</span> <span style="background-color: palegreen;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(rs&nbsp;=&gt;&nbsp;maîtreD.TryAccept(rs,&nbsp;reservation))</span> <span style="background-color: lightsalmon;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(m&nbsp;=&gt;&nbsp;m.Traverse(Repository.Create)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>),&nbsp;Ok);</span> }</pre> </p> <p> Like in the previous F# example, the final <code>Match</code> is most likely pure. In practice, <a href="/2020/02/24/discerning-and-maintaining-purity">you may not know</a>, because a method like <code>InternalServerError</code> or <code>Ok</code> is an inherited base class method. Regardless, I don't think that it's architecturally important, because what's going on there is rather trivial. </p> <h3 id="de8d05083dcd4dedae2b8b714702cc44"> Naming <a href="#de8d05083dcd4dedae2b8b714702cc44" title="permalink">#</a> </h3> <p> Since the metaphor occurred to me, I've been looking for a better name. The term <em>impure/pure/impure sandwich</em> seems too inconvenient, but nevertheless, people seem to have picked it up. </p> <p> I want a more distinct name, but have had trouble coming up with one. I've been toying with various abbreviations of <em>impure</em> and <em>pure</em>, but have finally settled on <em>impureim sandwich</em>. It's a contraction of <strong>im</strong>pure/<strong>pure</strong>/<strong>im</strong>pure. </p> <p> Why this particular contraction? </p> <p> I've played with lots of alternatives: <ul> <li>impureim: <strong>im</strong>pure/<strong>pure</strong>/<strong>im</strong>pure</li> <li>ipi: <strong>i</strong>mpure/<strong>p</strong>ure/<strong>i</strong>mpure</li> <li>impi: <strong>im</strong>pure/<strong>p</strong>ure/<strong>i</strong>mpure</li> <li>impim: <strong>im</strong>pure/<strong>p</strong>ure/<strong>im</strong>pure</li> </ul> and so on... </p> <p> I like <em>impureim</em> because the only <a href="https://en.wikipedia.org/wiki/Anagram">anagram</a> that I'm aware of is <em>imperium</em>. I therefore suggest that you pronounce it <em>impurium sandwich</em>. That'll work as a <a href="https://martinfowler.com/bliki/Neologism.html">neologic</a> <a href="https://en.wikipedia.org/wiki/Shibboleth">shibboleth</a>. </p> <h3 id="df74d63c013646aa9355b95e74fc3edc"> Summary <a href="#df74d63c013646aa9355b95e74fc3edc" title="permalink">#</a> </h3> <p> Functional architecture prohibits pure functions from invoking impure actions. On the other hand, a pure function is useless if you can't observe its result. A functional architecture, thus, must have an impure entry point that invokes a pure function and uses another impure action to act on the result. </p> <p> I suggest that we call such an <em>impure/pure/impure</em> interaction an <em>impureim sandwich</em>, and that we pronounce it an <em>impurium sandwich</em>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4d71311250a54b1ca558a8469acdf445"> <div class="comment-author"><a href="https://massivepixel.co">Toni Petrina</a> <a href="#4d71311250a54b1ca558a8469acdf445">#</a></div> <div class="comment-content"> <p> I find this example slightly simplistic. What happens when the logic has to do cascade reads/validations as it is typically done? Then you get impureimpureim...? Or do you fetch all data upfront even though it might be...irrelevant? For example, you want to send a comment to a blog post, but that post has forbidden new comments? Wouldn't you want to validate first and then fetch blog post if necessary? </p> </div> <div class="comment-date">2020-03-02 07:45 UTC</div> </div> <div class="comment" id="3cfe29c36690499cab429040260d818e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3cfe29c36690499cab429040260d818e">#</a></div> <div class="comment-content"> <p> Toni, thank you for writing. As I write <a href="/2019/12/02/refactoring-registration-flow-to-functional-architecture">in another article</a>, <blockquote> <p> "It's my experience that it's conspicuously often possible to implement an impure/pure/impure sandwich." </p> </blockquote> On the other hand, I never claimed that you can <em>always</em> do this. The impureim sandwich is a design pattern. It gives a name to a <a href="https://en.wikipedia.org/wiki/Software_design_pattern">general, reusable solution to a commonly occurring problem within a given context</a>. </p> <p> In cases where you can't apply the impureim sandwich pattern, <a href="/2017/07/10/pure-interactions">other patterns are available</a>. </p> </div> <div class="comment-date">2020-03-02 8:54 UTC</div> </div> <div class="comment" id="6880b482be54493cb2ffb05b90d50d56"> <div class="comment-author">Flechto <a href="#6880b482be54493cb2ffb05b90d50d56">#</a></div> <div class="comment-content"> <p> I like this idea and it gives a word to they pattern I have been trying to use but I do have some questions. In the C# example you have a field `maîtreD`. I am assuming that the value comes from dependency injection. Is that the case? And if so can it really be called a pure function? Is that tested in isolation and the test for the function in the example you test that the results from ReadReservations are passed to `maîtreD.TryAccept`? Or is there something else I am missing? </p> </div> <div class="comment-date">2021-04-23 21:41 UTC</div> </div> <div class="comment" id="cce27701773f4a3a9a6ce5dd25dda95e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cce27701773f4a3a9a6ce5dd25dda95e">#</a></div> <div class="comment-content"> <p> Flechto, thank you for writing. You don't have to assume anything about the code. If you following links in the article, you should be able to find <a href="https://github.com/ploeh/asynchronous-injection">the source code</a>. </p> <p> Conceptually, yes, the <code>maîtreD</code> class field is initialised via Constructor Injection. What makes you think that that makes it impure? </p> </div> <div class="comment-date">2021-04-25 15:47 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Discerning and maintaining purity https://blog.ploeh.dk/2020/02/24/discerning-and-maintaining-purity 2020-02-24T07:31:00+00:00 Mark Seemann <div id="post"> <p> <em>Functional programming depends on referential transparency, but identifying and keeping functions pure requires deliberate attention.</em> </p> <p> <a href="https://en.wikipedia.org/wiki/Referential_transparency">Referential transparency</a> is the essence of functional programming. Most other traits that people associate with functional programming emerge from it: immutability, recursion, <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order functions</a>, <a href="/2018/03/22/functors">functors</a> and monads, etcetera. </p> <p> To summarise, a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> has to obey two rules: <ul> <li>The same input always produces the same output.</li> <li>Calling it causes no side effects.</li> </ul> While those rules are easy to understand and remember, in practice they're harder to follow than most people realise. </p> <h3 id="8435839fae0b484399d4ada9f06e695d"> Lack of abstraction <a href="#8435839fae0b484399d4ada9f06e695d" title="permalink">#</a> </h3> <p> Mainstream programming languages don't distinguish between pure functions and impure actions. I'll use C# for examples, but you can draw the same conclusions for Java, C, C++, Visual Basic .NET and so on - even for <a href="https://fsharp.org">F#</a> and <a href="https://clojure.org">Clojure</a>. </p> <p> Consider this line of code: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">validationMsg</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Validator</span>.<span style="color:#74531f;">Validate</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>);</pre> </p> <p> Is <code>Validate</code> a pure function? </p> <p> You might want to look at the method signature before you answer: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>)</pre> </p> <p> This is, unfortunately, not helpful. Will <code>Validate</code> always return the same <code>string</code> for the same <code>dto</code>? Can we guarantee that there's no side effects? </p> <p> You can't answer these questions only by examining the method signature. You'll have to go and <em>read</em> the code. </p> <p> This breaks <a href="/encapsulation-and-solid">encapsulation</a>. It ruins abstraction. It makes code harder to maintain. </p> <p> I can't stress this enough. This is what I've attempted to describe in my <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">Humane Code</a> video. We waste significant time <em>reading</em> existing code. Mostly because it's difficult to understand. It doesn't fit in our brains. </p> <p> <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices</a> defines an <em>abstraction</em> as <blockquote> <p> "the amplification of the essential and the elimination of the irrelevant" </p> <footer><cite>Robert C. Martin</cite></footer> </blockquote> This fits with the definition of encapsulation from <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a>. You should be able to interact with an object without knowledge of its implementation details. </p> <p> When you have to read the code of a method, it indicates a lack of abstraction and encapsulation. Unfortunately, that's the state of affairs when it comes to referential transparency in mainstream programming languages. </p> <h3 id="eabec7ed53c3482d86d1e4968101741f"> Manual analysis <a href="#eabec7ed53c3482d86d1e4968101741f" title="permalink">#</a> </h3> <p> If you read the source code of the <code>Validate</code> method, however, it's easy to figure out whether it's pure: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:blue;">_</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; }</pre> </p> <p> Is the method deterministic? It seems like it. In fact, in order to answer that question, you need to know if <code>DateTime.TryParse</code> is deterministic. Assume that it is. Apart from the <code>TryParse</code> call, you can easily reason about the rest of this method. There's no randomness or other sources of non-deterministic behaviour in the method, so it seems reasonable to conclude that it's deterministic. </p> <p> Does the method produce side effects? Again, you have to know about the behaviour of <code>DateTime.TryParse</code>, but I think it's safe to conclude that there's no side effects. </p> <p> In other words, <code>Validate</code> is a pure function. </p> <h3 id="58667892c58f45ebac8e50946a9f1f2a"> Testability <a href="#58667892c58f45ebac8e50946a9f1f2a" title="permalink">#</a> </h3> <p> Pure functions are <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a> because they depend exclusively on their input. </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ValidDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;{&nbsp;Date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2021-12-21&nbsp;19:00&quot;</span>,&nbsp;Quantity&nbsp;=&nbsp;2&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Validator</span>.<span style="color:#74531f;">Validate</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Empty</span>(<span style="font-weight:bold;color:#1f377f;">actual</span>); }</pre> </p> <p> This unit test creates a reservation <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a> (DTO) with a valid date string and a positive quantity. There's no error message to produce for a valid DTO. The test asserts that the error message is empty. It passes. </p> <p> You can with similar ease write a test that verifies what happens if you supply an invalid <code>Date</code> string. </p> <h3 id="de8e9b17b8c14448a76165337ebdc410"> Maintaining purity <a href="#de8e9b17b8c14448a76165337ebdc410" title="permalink">#</a> </h3> <p> The problem with manual analysis of purity is that any conclusion you reach only lasts until someone edits the code. Every time the code changes, you must re-evaluate. </p> <p> Imagine that you need to add a new validation rule. The system shouldn't accept reservations in the past, so you edit the <code>Validate</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid date: </span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">date</span>&nbsp;<span style="font-weight:bold;color:#74531f;">&lt;</span>&nbsp;<span style="color:#2b91af;">DateTime</span>.Now) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid date: </span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; }</pre> </p> <p> Is the method still pure? No, it's not. It's now non-deterministic. One way to observe this is to let time pass. Assume that you wrote the above unit test well before December 21, 2021. That test still passes when you make the change, but months go by. One day (on December 21, 2021 at 19:00) the test starts failing. No code changed, but now you have a failing test. </p> <p> I've made sure that the examples in this article are simple, so that they're easy to follow. This could mislead you to think that the shift from referential transparency to impurity isn't such a big deal. After all, the test is easy to read, and it's clear why it starts failing. </p> <p> Imagine, however, that the code is as complex as the code base you work with professionally. A subtle change to a method deep in the bowels of a system can have profound impact on the entire architecture. You thought that you had a <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a>, but you probably don't. </p> <p> Notice that no types changed. The method signature remains the same. It's surprisingly difficult to maintain purity in a code base, even if you explicitly set out to do so. There's no <a href="https://en.wikipedia.org/wiki/Poka-yoke">poka-yoke</a> here; constant vigilance is required. </p> <h3 id="dcf2b00c3b4f49d192a10f0cb269d427"> Automation attempts <a href="#dcf2b00c3b4f49d192a10f0cb269d427" title="permalink">#</a> </h3> <p> When I explain these issues, people typically suggest some sort of annotation mechanism. Couldn't we use attributes to identify pure functions? Perhaps like this: </p> <p> <pre>[<span style="color:#2b91af;">Pure</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>)</pre> </p> <p> This doesn't solve the problem, though, because this still still compiles: </p> <p> <pre>[<span style="color:#2b91af;">Pure</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid date: </span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">date</span>&nbsp;<span style="font-weight:bold;color:#74531f;">&lt;</span>&nbsp;<span style="color:#2b91af;">DateTime</span>.Now) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid date: </span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; }</pre> </p> <p> That's an impure action annotated with the <code>[Pure]</code> attribute. It still compiles and passes all tests (if you run them before December 21, 2021). The annotation is a lie. </p> <p> As I've already implied, you also have the compound problem that you need to know the purity (or lack thereof) of all APIs from the base library or third-party libraries. Can you be sure that no pure function becomes impure when you update a library from version 2.3.1 to 2.3.2? </p> <p> I'm not aware of any robust automated way to verify referential transparency in mainstream programming languages. </p> <h3 id="5413987a6af14316aee1b1de82aee73d"> Language support <a href="#5413987a6af14316aee1b1de82aee73d" title="permalink">#</a> </h3> <p> While no mainstream languages distinguish between pure functions and impure actions, there are languages that do. The most famous of these is <a href="https://www.haskell.org">Haskell</a>, but other examples include <a href="http://www.purescript.org">PureScript</a> and <a href="https://www.idris-lang.org">Idris</a>. </p> <p> I find Haskell useful for exactly that reason. The compiler enforces the functional interaction law. You can't call impure actions from pure functions. Thus, you wouldn't be able to make a change to a function like <code>Validate</code> without changing its type. That would break most consuming code, which is a good thing. </p> <p> You could write an equivalent to the original, pure version of <code>Validate</code> in Haskell like this: </p> <p> <pre><span style="color:#2b91af;">validateReservation</span>&nbsp;::&nbsp;<span style="color:blue;">ReservationDTO</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">ReservationDTO</span> validateReservation&nbsp;r@(ReservationDTO&nbsp;_&nbsp;d&nbsp;_&nbsp;_&nbsp;_)&nbsp;= &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;readMaybe&nbsp;d&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;-&gt;&nbsp;Left&nbsp;$&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;date:&nbsp;&quot;</span>&nbsp;++&nbsp;d&nbsp;++&nbsp;<span style="color:#a31515;">&quot;.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;(_&nbsp;::&nbsp;LocalTime)&nbsp;-&gt;&nbsp;Right&nbsp;r</pre> </p> <p> This is a pure function, because all Haskell functions are pure by default. </p> <p> You can change it to also check for reservations in the past, but only if you also change the type: </p> <p> <pre><span style="color:#2b91af;">validateReservation</span>&nbsp;::&nbsp;<span style="color:blue;">ReservationDTO</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;(<span style="color:#2b91af;">Either</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">ReservationDTO</span>) validateReservation&nbsp;r@(ReservationDTO&nbsp;_&nbsp;d&nbsp;_&nbsp;_&nbsp;_)&nbsp;= &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;readMaybe&nbsp;d&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;-&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;Left&nbsp;$&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;date:&nbsp;&quot;</span>&nbsp;++&nbsp;d&nbsp;++&nbsp;<span style="color:#a31515;">&quot;.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;date&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;utcNow&nbsp;&lt;-&nbsp;getCurrentTime &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tz&nbsp;&lt;-&nbsp;getCurrentTimeZone &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;now&nbsp;=&nbsp;utcToLocalTime&nbsp;tz&nbsp;utcNow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;date&nbsp;&lt;&nbsp;now &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;Left&nbsp;$&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;date:&nbsp;&quot;</span>&nbsp;++&nbsp;d&nbsp;++&nbsp;<span style="color:#a31515;">&quot;.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;Right&nbsp;r</pre> </p> <p> Notice that I had to change the return type from <code>Either String ReservationDTO</code> to <code>IO (Either String ReservationDTO)</code>. The presence of <code>IO</code> marks the 'function' as impure. If I hadn't changed the type, the code simply wouldn't have compiled, because <code>getCurrentTime</code> and <code>getCurrentTimeZone</code> are impure actions. These types ripple through entire code bases, enforcing the functional interaction law at every level of the code base. </p> <h3 id="938b7dc2db644b6b94f6a484d65bb320"> Pure date validation <a href="#938b7dc2db644b6b94f6a484d65bb320" title="permalink">#</a> </h3> <p> How would you validate, then, that a reservation is in the future? In Haskell, like this: </p> <p> <pre><span style="color:#2b91af;">validateReservation</span>&nbsp;::&nbsp;<span style="color:blue;">LocalTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationDTO</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">ReservationDTO</span> validateReservation&nbsp;now&nbsp;r@(ReservationDTO&nbsp;_&nbsp;d&nbsp;_&nbsp;_&nbsp;_)&nbsp;= &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;readMaybe&nbsp;d&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;-&gt;&nbsp;Left&nbsp;$&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;date:&nbsp;&quot;</span>&nbsp;++&nbsp;d&nbsp;++&nbsp;<span style="color:#a31515;">&quot;.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;date&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;date&nbsp;&lt;&nbsp;now &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;Left&nbsp;$&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;date:&nbsp;&quot;</span>&nbsp;++&nbsp;d&nbsp;++&nbsp;<span style="color:#a31515;">&quot;.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;Right&nbsp;r</pre> </p> <p> This function remains pure, although it still changes type. It now takes an additional <code>now</code> argument that represents the current time. You can retrieve the current time as an impure action before you call <code>validateReservation</code>. Impure actions can always call pure functions. This enables you to keep your complex domain model pure, which makes it simpler, and easier to test. </p> <p> Translated to C#, that corresponds to this version of <code>Validate</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">now</span>,&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">date</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">date</span>&nbsp;<span style="font-weight:bold;color:#74531f;">&lt;</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">now</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; }</pre> </p> <p> This version takes an additional <code>now</code> input parameter, but remains deterministic and free of side effects. Since it's pure, it's trivial to unit test. </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2010-01-01&nbsp;00:01&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2011-09-11&nbsp;18:30&quot;</span>,&nbsp;3)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2019-11-26&nbsp;13:59&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2019-11-26&nbsp;19:00&quot;</span>,&nbsp;2)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2030-10-02&nbsp;23:33&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2030-10-03&nbsp;00:00&quot;</span>,&nbsp;2)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ValidDate</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">now</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservationDate</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;{&nbsp;Date&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">reservationDate</span>,&nbsp;Quantity&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Validator</span>.<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">Parse</span>(<span style="font-weight:bold;color:#1f377f;">now</span>),&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Empty</span>(<span style="font-weight:bold;color:#1f377f;">actual</span>); }</pre> </p> <p> Notice that while the <code>now</code> parameter plays the <em>role</em> of the current time, the fact that it's just a value makes it trivial to run <em>simulations</em> of what would have happened if you ran this function in 2010, or what will happen when you run it in 2030. A test is really just a simulation by another name. </p> <h3 id="67101ecee7b94a849b24126ac01851fe"> Summary <a href="#67101ecee7b94a849b24126ac01851fe" title="permalink">#</a> </h3> <p> Most programming languages don't explicitly distinguish between pure and impure code. This doesn't make it impossible to do functional programming, but it makes it arduous. Since the language doesn't help you, you must constantly review changes to the code and its dependencies to evaluate whether code that's supposed to be pure remains pure. </p> <p> Tests can help, particularly if you employ <a href="/property-based-testing-intro">property-based testing</a>, but vigilance is still required. </p> <p> While Haskell isn't a mainstream programming language, I find that it helps me flush out my wrong assumptions about functional programming. I write many prototypes and proofs of concept in Haskell for that reason. </p> <p> Once you get the hang of it, it becomes easier to spot sources of impurity in other languages as well. <ul> <li>Anything with the <code>void</code> return type must be assumed to induce side effects.</li> <li>Everything that involves random numbers is non-deterministic.</li> <li>Everything that relies on the system clock is non-deterministic.</li> <li>Generating a GUID is non-deterministic.</li> <li>Everything that involves input/output is non-deterministic. That includes the file system and everything that involves network communication. In C# this implies that all <a href="/2016/04/11/async-as-surrogate-io">asynchronous APIs should be considered highly suspect</a>.</li> </ul> If you want to harvest the benefits of functional programming in a mainstream language, you must look out for such pitfalls. There's no tooling to assist you. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1925963B8A194CF9BC44E0EB59E92C9C"> <div class="comment-author"><a href="http://criticalsoftwareblog.com">Yacoub Massad</a> <a href="#1925963B8A194CF9BC44E0EB59E92C9C">#</a></div> <div class="comment-content"> <p> You might be interested in taking a look at <a href="https://github.com/ymassad/PurityAnalyzer">PurityAnalyzer</a>; An open source roslyn-based analyzer for C# that I started developing to help maintain pure C# code. </p> <p> Unfortunately, it is still not production-ready yet and I didn't have time to work on it in the last year. I was hoping contributors would help. </p> </div> <div class="comment-date">2020-02-24 08:16 UTC</div> </div> <div class="comment" id="50049d1906e04e1ab3811765ca5c3156"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#50049d1906e04e1ab3811765ca5c3156">#</a></div> <div class="comment-content"> <p> Yacoub, thank you for writing. I wasn't aware of PurityAnalyzer. Do I understand it correctly that it's based mostly on a table of methods known (or assumed) to be pure? It also seems to look for certain attributes, under the assumption that if a <code>[Pure]</code> attribute is present, then one can trust it. Did I understand it correctly? </p> <p> The fundamental problems with such an approach aside, I can't think of a better solution for the current .NET platform. If you want contributors, though, you should edit the repository's readme-file so that it explains how the tool works, and how contributors could get involved. </p> </div> <div class="comment-date">2020-02-26 7:12 UTC</div> </div> <div class="comment" id="D45AAB9A9C2C4625BD949B2130416D79"> <div class="comment-author"><a href="http://criticalsoftwareblog.com">Yacoub Massad</a> <a href="#D45AAB9A9C2C4625BD949B2130416D79">#</a></div> <div class="comment-content"> <p>Here are the answers to your questions:</p> <p>1.it's based mostly on a table of methods known (or assumed) to be pure?</p> <p>This is true for compiled methods, e.g., methods in the .NET frameworks. There are lists maintained for .NET methods that are pure. The lists of course are still incomplete.</p> <p>For methods in the source code, the analyzer checks if they call impure methods, but it also checks other things like whether they access mutable state. The list of other things is not trivial. If you are interested in the details, see <a href="https://www.dotnetcurry.com/csharp/1464/pure-code-csharp"> this article</a>. It shows some of the details.</p> <p>2. It also seems to look for certain attributes, under the assumption that if a [Pure] attribute is present, then one can trust it. Did I understand it correctly?</p> <p>I don't use the [Pure] attribute because I think that the definition of pure used by Microsoft with this attribute is different than what I consider to be pure. I used a special [IsPure] attribute. There are also other attributes like [IsPureExceptLocally], [IsPureExceptReadLocally], [ReturnsNewObject], etc. The article I mentioned above explains some differences between these.</p> <p>I agree with you that I should work on readme file to explain details and ask for contributors.</p> </div> <div class="comment-date">2020-02-26 09:51 UTC</div> </div> <div class="comment" id="d28c24c07228400f9ed141f97b5c72b5"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#d28c24c07228400f9ed141f97b5c72b5">#</a></div> <div class="comment-content"> <p> I love this post and enthusiastically agree with all the points you made. </p> <blockquote> Is the method deterministic? It seems like it. In fact, in order to answer that question, you need to know if <code>DateTime.TryParse</code> is deterministic. Assume that it is. </blockquote> <p> For what its worth, that <a href="https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,1466">overload of <code>DateTime.TryParse</code></a> is impure because it depends on <a href="https://referencesource.microsoft.com/#mscorlib/system/globalization/datetimeformatinfo.cs,d8a79667802e6102"><code>DateTimeFormatInfo.CurrentInfo</code></a>, which depends on <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread.currentculture?view=netframework-4.8#System_Threading_Thread_CurrentCulture"><code>System.Threading.Thread.CurrentThread.CurrentCulture</code></a>, which is mutable. </p> <blockquote> There are lists maintained for .NET methods that are pure. </blockquote> <p> Yacoub, could you share some links to such lists? </p> </div> <div class="comment-date">2020-02-26 20:14 UTC</div> </div> <div class="comment" id="d2cd79f75b0c4904bd55f34887513b61"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d2cd79f75b0c4904bd55f34887513b61">#</a></div> <div class="comment-content"> <p> Tyson, I actually knew that, but in order to keep the example simple and compelling, I chose to omit that fact. That's why I phrased the sentence "<em>Assume</em> that it is" (my emphasis) 😉 </p> </div> <div class="comment-date">2020-02-26 21:56 UTC</div> </div> <div class="comment" id="CED4372686884B47B7FCC4CC03A50C47"> <div class="comment-author"><a href="http://criticalsoftwareblog.com">Yacoub Massad</a> <a href="#CED4372686884B47B7FCC4CC03A50C47">#</a></div> <div class="comment-content"> <p> Tyson, I meant lists maintained as part of the PurityAnalyzer project. You can find them <a href="https://github.com/ymassad/PurityAnalyzer/tree/master/PurityAnalyzer/Resources">here</a>. </p> </div> <div class="comment-date">2020-02-27 07:48 UTC</div> </div> <div class="comment" id="0d0c85170c1c4062addf406f189c0984"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#0d0c85170c1c4062addf406f189c0984">#</a></div> <div class="comment-content"> <blockquote> The [Haskell] compiler enforces the functional interaction law. You can't call impure actions from pure functions. </blockquote> <p> And in contrast, the C# compiler does not enfore the functional interaction law, right? </p> <p> For exampe, suppose <code>Foo</code> and <code>Bar</code> are pure functions such that <code>Foo</code> calls <code>Bar</code> and the code compiles. Then only change the implementation of <code>Bar</code> in such a way that it is now impure and the code still compiles, which is possible. So <code>Foo</code> is now also impure as well, but its implementation didn't change. Therefore, the C# compiler does not enfore the functional interaction law. </p> <p> Is this consistent with what you mean by the functional interaction law? </p> </div> <div class="comment-date">2020-03-07 12:59 UTC</div> </div> <div class="comment" id="2d47bc8c9c07456091f923ea5298cd7e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2d47bc8c9c07456091f923ea5298cd7e">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. The C# compiler doesn't help protect your <em>intent</em>, if your intent is to apply a functional architecture. </p> <p> In your example, <code>Foo</code> starts out pure, but becomes impure. That's a <em>result</em> of the law. The law itself isn't broken, but the relationships change. That's often not what you want, so you can say that the compiler doesn't help you maintain a functional architecture. </p> <p> A compiler like Haskell protects the intent of the law. If <code>foo</code> (Haskell functions <em>must</em> start with a lower-case letter) and <code>bar</code> both start out pure, <code>foo</code> can call <code>bar</code>. When <code>bar</code> later becomes impure, its type changes and <code>foo</code> can no longer invoke it. </p> <p> I can try to express the main assertion of the functional interaction law like this: <em>a pure function can't call an impure action.</em> This has different implications in different compiler contexts. In Haskell, functions can be statically declared to be either pure or impure. This means that the Haskell compiler can prevent pure functions from calling impure actions. In C#, there's no such distinction at the type level. The implication is therefore different: that if <code>Foo</code> calls <code>Bar</code> and <code>Bar</code> is impure, then <code>Foo</code> must also be impure. This follows by elimination, because a pure function can't call an impure action. Therefore, since <code>Foo</code> <em>can</em> call <code>Bar</code>, and <code>Bar</code> is impure, then <code>Foo</code> must also be impure. </p> <p> The causation is reversed, so to speak. </p> <p> Does that answer your question? </p> </div> <div class="comment-date">2020-03-08 11:32 UTC</div> </div> <div class="comment" id="32759f1428c34fd0860e57c077df2972"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#32759f1428c34fd0860e57c077df2972">#</a></div> <div class="comment-content"> <p> Yes, that was a good answer. Thank you. </p> <blockquote> ...a pure function can't call an impure action. </blockquote> <p> We definitely want this to be true, but let's try to make sure it is. What do you think about the C# function <code>void Foo() =&gt; DateTime.Now;</code>? It has lots of good propertie: it alreays returns the same value (something isomorphic to <code>Unit</code>), and it does not mutate anything. However, it calls the impure property <code>DateTime.Now</code>. I think a reasonable person could argue that this function is pure. My guess is that you would say that it is impure. Am I right? I am willing to accept that. </p> <blockquote> ...a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> has to obey two rules: <ul> <li>The same input always produces the same output.</li> <li>Calling it causes no side effects.</li> </ul> </blockquote> <p> Is it possible for a function to violate the first rule but not violate the second rule? </p> </div> <div class="comment-date">2020-03-09 04:12 UTC</div> </div> <div class="comment" id="a7375c806c1147c1b37187834fb04153"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a7375c806c1147c1b37187834fb04153">#</a></div> <div class="comment-content"> <p> Tyson, I'm going to assume that you mean something like <code>void Foo() { var _ = DateTime.Now; }</code>, since the code you ask about doesn't compile 😉 </p> <p> That function is, indeed pure, because it has no observable side effects, and it always <a href="/2018/01/15/unit-isomorphisms">returns unit</a>. Purity is mostly a question of what we can observe if we consider the function a black box. </p> <p> Obviously, based on that criterion, we can refactor the function to <code>void Foo() { }</code> and we wouldn't be able to tell the difference. This version of <code>Foo</code> is clearly pure, although degenerate. <blockquote> Is it possible for a function to violate the first rule but not violate the second rule? </blockquote> Yes, the following method is non-deterministic, but has no side effects: <code>DateTime Foo() =&gt; DateTime.Now;</code> The input is always <em>unit</em>, but the return value can change. </p> </div> <div class="comment-date">2020-03-10 9:03 UTC</div> </div> <div class="comment" id="60eb2b1f83dd4b2fbc0be684d729af96"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#60eb2b1f83dd4b2fbc0be684d729af96">#</a></div> <div class="comment-content"> <p> I think I need to practice test driven comment writing ;) Thanks for seeing through my syntax errors again. </p> <p> Oh, you think that that function is pure. Interesting. It follows then that the functional interaction law (pure functions cannot call impure actions) does <i>not</i> follow from the definition of a pure function. It is possible, in theory and in practice, for a pure function to call an impure action. Instead, the functional interaction law is "just" a goal to aspire to when designing a programming language. Haskell achieved that goal while C# and F# did not. Do you agree with this? (This is really what I was driving towards in <a href="https://blog.ploeh.dk/2020/02/24/discerning-and-maintaining-purity/#0d0c85170c1c4062addf406f189c0984">this comment above</a>, but I was trying to approach this "blasphemous" claim slowly.) </p> <p> Just as you helped me distinguish between function purity and totality in <a href="https://blog.ploeh.dk/2015/05/07/functional-design-is-intrinsically-testable/#97e4ae29436e4828813bf445ee1c37dc">this comment</a>, I think it would be helpful for us to consider separately the two defining properties of a pure function. The first property is "the same input always produces the same output". Let's call this weak determinism. <a href="https://en.wikipedia.org/wiki/Deterministic_algorithm">Determinism</a> is could be defined as "the same input always produces the same sequence of states", which includes the state of the output, so determinism is indeed stronger than weak determinism. The second property is "causes no side effect". It seems to me that there is either a lack of consensus or a lack of clarity about what constitutes a side effect. One definition I like is mutation of state outside of the current stack frame. </p> <p> One reason the functional interaction law is false in general is because the corresponding interaction law for weak determinism also false in general. The function I gave above (that called <code>DateTime.Now</code> and then returned unit) is a trivial example of that. A nontrivial example is <a href="https://en.wikipedia.org/wiki/Quicksort">quicksort</a>. </p> <p> At this point, I wanted to claim that the side effect interaction law is true in general, but it is not. This law says that a function that is side-effect free cannot call a function that causes a side effect. A counterexample is <code>void Foo() { int i = 0; Bar(ref i); }</code> with <code>void Bar(ref int i) => i++;</code>. That is, <code>Bar</code> mutates state outside of its stack frame, namely in the stack frame of <code>Foo</code>, so it is not side-effect free, but <code>Foo</code> is. (And I promise that I tested that code for compiler errors.) </p> <p> I need to think more about that. Is there a better definition of side effect, one for which the side effect interaction law is true? </p> <p> I just realized something that I think is interesting. Purely functional programming languages enforce a property of functions stronger than purity. With respect to the first defining property of a pure function (aka weak determinism), purely functional programming languages enforce the stronger notion of determinism. Otherwise, the compiler would need to realize that functions like quicksort should be allowed (because it is weakly deterministic). This reminds me of the debate between static and dynamic programming languages. In the process of forbidding certain unsafe code, static languages end up forbidding some safe code as well. </p> </div> <div class="comment-date">2020-03-10 14:05 UTC</div> </div> <div class="comment" id="dd776b1b8b0d43c58171ff7fec311c2c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dd776b1b8b0d43c58171ff7fec311c2c">#</a></div> <div class="comment-content"> <p> Tyson, I disagree with your basic premise: <blockquote> "It follows then that the functional interaction law (pure functions cannot call impure actions) does not follow from the definition of a pure function." </blockquote> I don't think that this follows. </p> <p> The key is that your example is <em>degenerate</em>. The <code>Foo</code> function is only pure because <code>DateTime.Now</code> isn't used. The actual, underlying property that we're aiming for is <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>. Can you replace <code>Foo</code> with its value? Yes, you can. </p> <p> Perhaps you think this is a hand-wavy attempt to dodge a bullet, but I don't think that it is. You can write the equivalent function in Haskell like this: </p> <p> <pre><span style="color:#2b91af;">foo</span>&nbsp;::&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;() foo&nbsp;<span style="color:blue;">()</span>&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;_&nbsp;=&nbsp;getCurrentTime &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">()</span></pre> </p> <p> I don't recall if you're familiar with Haskell, but for the benefit of any reader who comes by and wishes to follow this discussion, here are the important points: <ul> <li>The function calls <code>getCurrentTime</code>, which is an impure action. Its type is <code>IO UTCTime</code>. The <code>IO</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> marks the action as impure.</li> <li>The underscore is a <em>wildcard</em> that tells Haskell to discard the value.</li> <li>The type of <code>foo</code> is <code>() -&gt; ()</code>. It takes <a href="/2018/01/15/unit-isomorphisms">unit</a> as input and returns <em>unit</em>. There's no <code>IO</code> container involved, so the function is pure.</li> </ul> This works because Haskell is a strictly functional language. Every expression is referentially transparent. The implication is that something like <code>IO UTCTime</code> is an <em>opaque</em> container of <code>UTCTime</code> values. A pure caller can see the container, but not its contents. A common interpretation of this is that <code>IO</code> represents the superposition of all possible values, just like <a href="https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat">Schrödinger's box</a>. Also, since Haskell is a lazily evaluated language, actions are only evaluated when their values are needed for something. Since the value of <code>getCurrentTime</code> is discarded, the impure action never runs (the box is never opened). This may be clearer with this example: </p> <p> <pre><span style="color:#2b91af;">bar</span>&nbsp;::&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;() bar&nbsp;<span style="color:blue;">()</span>&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;_&nbsp;=&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;<span style="color:#a31515;">&quot;Bar!&quot;</span> &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">()</span></pre> </p> <p> Like <code>foo</code>, <code>bar</code> calls an impure action: <code>putStrLn</code>, which corresponds to <code>Console.WriteLine</code>. Having the type <code>String -&gt; IO ()</code> it's impure. It works like this: </p> <p> <pre>&gt; putStrLn "Example" Example</pre> </p> <p> None the less, because <code>bar</code> discards the <code>IO ()</code> return value after it calls <code>putStrLn</code>, it never evaluates: </p> <p> <pre>&gt; bar () ()</pre> </p> <p> Perhaps a subtle rephrasing of the functional interaction law would be more precise. Perhaps it should say that a pure function can't <em>evaluate</em> an impure action. </p> <p> Bringing this back to C#, we have to keep in mind that C# doesn't enforce the functional interaction law in any way. Thus, the law works ex-post, instead of in Haskell, where it works ex-ante. Is the <code>Foo</code> C# code pure? Yes, it is, because it's referentially transparent. </p> <p> Regarding the purity of QuickSort, you may find <a href="https://stackoverflow.com/q/7717691/126014">this discussion</a> interesting. </p> </div> <div class="comment-date">2020-03-12 7:40 UTC</div> </div> <div class="comment" id="a06da27b57944c89aefc16914234939c"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#a06da27b57944c89aefc16914234939c">#</a></div> <div class="comment-content"> <blockquote> ...Haskell is a strictly functional language. Every expression is referentially transparent. ... Is the <code>Foo</code> C# code pure? Yes, it is, because it's referentially transparent. </blockquote> <p> So every function in Haskell is referentially transparent, and if a funciton in C# is referentially transparent, then it is pure. Is C# necessary there? Does referential transparency impliy purity regardless of langauge? Do you consider purity and referential transparency to be concepts that imply each other regulardless of language? I think a function is referential transparency if and only if it is pure, and I think this is independent of the langauge. </p> <p> If C# is not necessary, then it follows that every function in Haskell is pure. This seems like a contradiction with this statement. </p> <blockquote> The function calls <code>getCurrentTime</code>, which is an impure action. Its [return] type is <code>IO UTCTime</code>. The <code>IO</code> <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> marks the action as impure. </blockquote> <p> You cited Bartosz Milewski there. <a href="https://www.schoolofhaskell.com/school/starting-with-haskell/basics-of-haskell/3-pure-functions-laziness-io#pure-functions">He also says</a> that every function in Haskell is pure. He calls Haskell functions returning IO a pure action. I agree with Milewski; I think every function in Haskell is pure. </p> <blockquote> Perhaps a subtle rephrasing of the functional interaction law would be more precise. Perhaps it should say that a pure function can't <em>evaluate</em> an impure action. </blockquote> <p> How does this rephrasing help? In the exmaple from my previous comment, <code>bar</code> is impure while <code>foo</code> is pure even though <code>foo</code> evaluates <code>bar</code>, which can be verified by putting a breakpoint in <code>bar</code> when evaluating <code>foo</code> or by observing that <code>i</code> has value <code>1</code> when <code>foo</code> returns. If Haskell contained impure functions, then replacing "calls" with "evalutes" helps because everything is lazy in Haskell, but I don't see how it helps in an eager langauge like C#. </p> <blockquote> Regarding the purity of QuickSort, you may find <a href="https://stackoverflow.com/q/7717691/126014">this discussion</a> interesting. </blockquote> <p> Oh, sorry. I now see that my reference to quicksort was unclear. I meant the randomized version of quicksort for the pivot is selected uniformily at random from all elements being sorted. That refrasing of the functional interaction law doesn't address the issue I am trying to point out with quicksort. To elborate, consider this randomized version of quicksort that has no side effects. I think this function is pure even though it uses randomness, which is necessarily obtained from an impure function. </p> </div> <div class="comment-date">2020-07-06 13:57 UTC</div> </div> <div class="comment" id="9f9664e29c3f4fafa786abae039eb20e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9f9664e29c3f4fafa786abae039eb20e">#</a></div> <div class="comment-content"> <p> Tyson, my apologies that I've been so dense. I think that I'm beginning to understand where you're going with this. Calling out randomised pivot selection in quicksort helped, I think. </p> <p> I would consider a quicksort function referentially transparent, even if it were to choose the pivot at random. Even if it does that, you can replace a given function call with its output. The only difference you might observe across multiple function calls would be varying execution time, due to lucky versus unlucky random pivot selection. Execution time is, however, not a property that impacts whether or not we consider a function pure. </p> <p> <em>Safe Haskell</em> can't do that, though, so you're correct when you say: <blockquote> "In the process of forbidding certain unsafe code, static languages end up forbidding some safe code as well." </blockquote> (Actually, you <em>can</em> implement quicksort like that in Haskell as well. In order to not muddy the waters, I've so far ignored that the language has an escape hatch for (among other purposes) this sort of scenario: <code>unsafePerformIO</code>. In <em>Safe Haskell</em>, however, you can't use it, and I've never myself had to use it.) </p> <p> I'm going to skip the discussion about whether or not all of Haskell is pure, because I think it's a red herring. We can discuss it later, if you're interested. </p> <p> I think that you're right, though, that the functional interaction law has to come with a disclaimer. I'm not sure exactly how to formulate it, but I need to take a detour around side effects, and then perhaps you can help me with that. </p> <p> Functional programmers know that every execution has side effects. In the extreme, running any calculation on a computer produces heat. There could be other side effects as well, such as CPU registers changing values, data moving in and out of processor caches, and so on. The question is: when do side effects become significant? </p> <p> We don't consider the generation of heat a significant side effect. What about a debug trace? If it doesn't affect the state of the system, does it count? If not, then how about logging or auditing? </p> <p> We usually draw the line somewhere and say that anything on one side counts, and things on the other side don't. The bottom line is, though, that we consider some side effects insignificant. </p> <p> I think that you have now demonstrated that there's symmetry. Not only are there insignificant side effects, but insignificant randomness also exists. The randomness involved in choosing a pivot in quicksort has no significant impact on the output. </p> <p> Was that what you meant by <em>weak determinism?</em> </p> </div> <div class="comment-date">2020-07-07 19:53 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Builder as a monoid https://blog.ploeh.dk/2020/02/17/builder-as-a-monoid 2020-02-17T07:18:00+00:00 Mark Seemann <div id="post"> <p> <em>Builder, particularly Fluent Builder, is one of the more useful design patterns. Here's why.</em> </p> <p> This article is part of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions/">a series of articles about design patterns and their universal abstraction counterparts</a>. </p> <p> The <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder design pattern</a> is an occasionally useful pattern, but mostly in its Fluent Builder variation. I've already described that <a href="/2020/02/10/builder-isomorphisms">Builder, Fluent Builder, and Immutable Fluent Builder are isomorphic</a>. The Immutable Fluent Builder variation is a set of <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>, so among the three variations, it best fits the set of universal abstractions that I've so far discussed in <a href="/2017/10/04/from-design-patterns-to-category-theory">this article series</a>. </p> <p> <a href="http://amzn.to/XBYukB">Design Patterns</a> describes 23 patterns. Some of these are more useful than others. I first read the book in 2003, and while I initially used many of the patterns, after some years I settled into a routine where I'd reach for the same handful of patterns and ignore the rest. </p> <p> What makes some design patterns more universally useful than others? There's probably components of both subjectivity and chance, but I also believe that there's some correlation to universal abstractions. I consider abstractions universal when they are derived from universal truths (i.e. mathematics) instead of language features or 'just' experience. That's what the overall article series is about. In this article, you'll learn how the Builder pattern is an instance of a universal abstraction. Hopefully, this goes a long way towards explaining why it seems to be so universally useful. </p> <h3 id="6d834828809b4ac2b36f59bb244e5952"> Builder API, isolated <a href="#6d834828809b4ac2b36f59bb244e5952" title="permalink">#</a> </h3> <p> I'll start with the <code>HttpRequestMessageBuilder</code> from the article about <a href="/2020/02/10/builder-isomorphisms">Builder isomorphisms</a>, particularly its Immutable Fluent Builder incarnation. Start by isolating those methods that manipulate the Builder. These are the functions that had <code>void</code> return types in the original Builder incarnation. Imagine, for example, that you extract an interface of only those methods. What would such an interface look like? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IHttpRequestMessageBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">object</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithMethod</span>(<span style="color:#2b91af;">HttpMethod</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>); }</pre> </p> <p> Keep in mind that on all instance methods, <a href="/2018/02/12/object-isomorphisms">the instance itself can be viewed as 'argument 0'</a>. In that light, each of these methods take two arguments: a Builder and the formal argument (<code>jsonBody</code> and <code>newMethod</code>, respectively). Each method returns a Builder. I've <a href="/2019/01/28/better-abstractions-revisited#047886dcfa5a4a1398965138669e0ddc">already described how this is equivalent to an endomorphism</a>. An <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a> is a function that returns the same type of output as its input, and <a href="/2017/11/13/endomorphism-monoid">it forms a monoid</a>. </p> <p> This can be difficult to see, so I'll make it explicit. The code that follows only exists to illustrate the point. In no way do I endorse that you write code in this way. </p> <h3 id="e022825850dc4fbf8d78b550bebb5d7c"> Explicit endomorphism <a href="#e022825850dc4fbf8d78b550bebb5d7c" title="permalink">#</a> </h3> <p> You can define a formal interface for an endomorphism: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>); }</pre> </p> <p> Notice that it's completely generic. The <code>Run</code> method takes a value of the generic type <code>T</code> and returns a value of the type <code>T</code>. The identity of the <a href="/2017/10/06/monoids">monoid</a>, you may recall, is the eponymously named <em>identity</em> function which returns its input without modification. You can also define the monoidal combination of two endomorphisms: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AppendEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;morphism1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;morphism2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AppendEndomorphism</span>(<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">morphism1</span>,&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">morphism2</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.morphism1&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">morphism1</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.morphism2&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">morphism2</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;morphism2.<span style="font-weight:bold;color:#74531f;">Run</span>(morphism1.<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="font-weight:bold;color:#1f377f;">x</span>)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation of <code>IEndomorphism&lt;T&gt;</code> composes two other <code>IEndomorphism&lt;T&gt;</code> objects. When its <code>Run</code> method is called, it first calls <code>Run</code> on <code>morphism1</code> and then uses the return value of that method call (still a <code>T</code> object) as input for <code>Run</code> on <code>morphism2</code>. </p> <p> If you need to combine more than two endomorphisms then that's also possible, because <a href="/2017/11/20/monoids-accumulate">monoids accumulate</a>. </p> <h3 id="5f10f8e180b349a0b0584812a0c62431"> Explicit endomorphism to change HTTP method <a href="#5f10f8e180b349a0b0584812a0c62431" title="permalink">#</a> </h3> <p> You can adapt the <code>WithMethod</code> method to the <code>IEndomorphism&lt;HttpRequestMessageBuilder&gt;</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ChangeMethodEndomorphism</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">HttpMethod</span>&nbsp;newMethod; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ChangeMethodEndomorphism</span>(<span style="color:#2b91af;">HttpMethod</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.newMethod&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">x</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>.<span style="font-weight:bold;color:#74531f;">WithMethod</span>(newMethod); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In itself, this is simple code, but it does turn things on their head. The <code>newMethod</code> argument is now a class field (and constructor argument), while the <code>HttpRequestMessageBuilder</code> has been turned into a method argument. Keep in mind that I'm not doing this because I endorse this style of API design; I do it to demonstrate how the Immutable Fluent Builder pattern is an endomorphism. </p> <p> Since <code>ChangeMethodEndomorphism</code> is an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> between <code>IEndomorphism&lt;HttpRequestMessageBuilder&gt;</code> and the <code>WithMethod</code> method, I hope that this is becoming apparent. I'll show one more Adapter. </p> <h3 id="73a3f6e43a19457a937b36ed8682defa"> Explicit endomorphism to add a JSON body <a href="#73a3f6e43a19457a937b36ed8682defa" title="permalink">#</a> </h3> <p> In the example code, there's one more method that modifies an <code>HttpRequestMessageBuilder</code> object, and that's the <code>AddJsonBody</code> method. You can also create an Adapter over that method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AddJsonBodyEndomorphism</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">object</span>&nbsp;jsonBody; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AddJsonBodyEndomorphism</span>(<span style="color:blue;">object</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.jsonBody&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">x</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>.<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(jsonBody); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While the <code>AddJsonBody</code> method itself is more complicated than <code>WithMethod</code>, the Adapter is strikingly similar. </p> <h3 id="680c696032f542ae8cddcf7a520e84eb"> Running an explicit endomorphism <a href="#680c696032f542ae8cddcf7a520e84eb" title="permalink">#</a> </h3> <p> You can use the <code>IEndomorphism&lt;T&gt;</code> API to compose a pipeline of operations that will, for example, make an <code>HttpRequestMessageBuilder</code> build an HTTP <code>POST</code> request with a JSON body: </p> <p> <pre><span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">morphism</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AppendEndomorphism</span>&lt;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChangeMethodEndomorphism</span>(<span style="color:#2b91af;">HttpMethod</span>.Post), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AddJsonBodyEndomorphism</span>(<span style="color:blue;">new</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">NewGuid</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2020-03-22&nbsp;19:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Ælfgifu&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;ælfgifu@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;}));</pre> </p> <p> You can then <code>Run</code> the endomorphism over a new <code>HttpRequestMessageBuilder</code> object to produce an HTTP request: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">morphism</span>.<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>)).<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> The <code>msg</code> object represents an HTTP <code>POST</code> request with the supplied JSON body. </p> <p> Once again, I stress that the purpose of this little exercise is only to demonstrate how an Immutable Fluent Builder is an endomorphism, which is a monoid. </p> <h3 id="cfbf771a96144050ac21bf7c58333595"> Test Data Builder endomorphism <a href="#cfbf771a96144050ac21bf7c58333595" title="permalink">#</a> </h3> <p> You can give <a href="http://www.natpryce.com/articles/000714.html">Test Data Builders</a> the same treatment, again only to demonstrate that the reason they compose so well is because they're monoids. I'll use an immutable variation of the <code>AddressBuilder</code> from <a href="/2017/08/15/test-data-builders-in-c">this article</a>. </p> <p> For example, to modify a city, you can introduce an endomorphism like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CityEndomorphism</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">AddressBuilder</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;city; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CityEndomorphism</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">city</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.city&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">city</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:#2b91af;">AddressBuilder</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>.<span style="font-weight:bold;color:#74531f;">WithCity</span>(city); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can use it to create an address in Paris like this: </p> <p> <pre><span style="color:#2b91af;">IEndomorphism</span>&lt;<span style="color:#2b91af;">AddressBuilder</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">morphism</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CityEndomorphism</span>(<span style="color:#a31515;">&quot;Paris&quot;</span>); <span style="color:#2b91af;">Address</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">address</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">morphism</span>.<span style="font-weight:bold;color:#74531f;">Run</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>()).<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> The <code>address</code> is fully populated with <code>Street</code>, <code>PostCode</code>, and so on, but apart from <code>City</code>, you know none of the values. </p> <h3 id="c5d669df78134e22a5910233da2cc813"> Sweet spot <a href="#c5d669df78134e22a5910233da2cc813" title="permalink">#</a> </h3> <p> Let's return to the question from the introduction to the article. What makes some design patterns useful? I don't think that there's a single answer to that question, but I find it intriguing that so many of the useful patterns turn out to be equivalent to universal abstractions. The Builder pattern is a monoid. From a programming perspective, the most useful characteristic of <a href="/2017/11/27/semigroups">semigroups</a> and monoids is that they enable you to treat many objects as one object. Monoids compose. </p> <p> Of the three Builder variations, the Immutable Fluent Builder is the most useful. It's also the variation that most clearly corresponds to the endomorphism monoid. Viewing it as an endomorphism reveals its strengths. When or where is a Builder most useful? </p> <p> Don't be mislead by <em>Design Patterns</em>, which states the intent of the Builder pattern like this: <blockquote> <p> "Separate the construction of a complex object from its representation so that the same construction process can create different representations." </p> <footer><cite>Gamma et al, Design Patterns, 1994</cite></footer> </blockquote> This may still be the case, but I don't find that this is the primary advantage offered by the pattern. We've learned much about the utility of each design pattern since 1994, so I don't blame the Gang of Four for not seeing this. I do think, however, that it's important to emphasise that the benefit you can derive from a pattern may differ from the original motivation. </p> <p> An endomorphism represents a modification of a value. You need a value to get started, and you get a modified value (of the same type) as output. </p> <p> <img src="/content/binary/single-step-endomorphism.png" alt="An object (a little circle) to the left, going into a horizontally oriented pipe, and coming out to the right in a different colour."> </p> <p> Sometimes, all you need is the initial object. </p> <p> <img src="/content/binary/single-object-depicted-as-a-circle.png" alt="An object represented as a little circle."> </p> <p> And sometimes, you need to compose several changes. </p> <p> <img src="/content/binary/double-step-endomorphism.png" alt="An object (a little circle) to the left, going into two horizontally oriented pipe, on after the other, and coming out to the right in a different colour."> </p> <p> To me, this makes the sweet spot for the pattern clear. Use an (Immutable) Fluent Builder when you have a basic object that's useful in itself, but where you want to give client code the option to make changes to the defaults. </p> <p> Sometimes, the initial object has self-contained default values. Test Data Builders are good examples of that: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.street&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.city&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.postCode&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCodeBuilder</span>().<span style="font-weight:bold;color:#74531f;">Build</span>(); }</pre> </p> <p> The <code>AddressBuilder</code> constructor fully initialises the object. You can use its <code>WithNoPostcode</code>, <code>WithStreet</code>, etcetera methods to make changes to it, but you can also use it as is. </p> <p> In other cases, client code must initialise the object to be built. The <code>HttpRequestMessageBuilder</code> is an example of that: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="font-weight:bold;color:#1f377f;">url</span>))&nbsp;{&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:#2b91af;">HttpMethod</span>.Get,&nbsp;<span style="color:blue;">null</span>)&nbsp;{&nbsp;} <span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:#2b91af;">HttpMethod</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">method</span>,&nbsp;<span style="color:blue;">object</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.url&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>; &nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">method</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.jsonBody&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>; }</pre> </p> <p> While there's more than one constructor overload, client code must supply a <code>url</code> in one form or other. That's the precondition of this class. Given a valid <code>url</code>, though, an <code>HttpRequestMessageBuilder</code> object can be useful without further modification, but you can also modify it by calling its methods. </p> <p> You often see the Builder pattern used for configuration APIs. The ASP.NET Core <a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.builder.iapplicationbuilder">IApplicationBuilder</a> is a prominent example of the Fluent Builder pattern. The <a href="https://docs.particular.net/samples/endpoint-configuration">NServiceBus endpoint configuration API</a>, on the other hand, is based on the classic Builder pattern. It makes sense to use an endomorphic design for framework configuration. Framework designers want to make it as easy to get started with their framework as possible. For this reason, it's important to provide a useful default configuration, so that you can get started with as little <a href="/2019/12/16/zone-of-ceremony">ceremony</a> as possible. On the other hand, a framework must be flexible. You need a way to tweak the configuration to support your particular needs. The Builder pattern supports both scenarios. </p> <p> Other examples include Test Data Builders, as well as specialised Builders such as <a href="https://docs.microsoft.com/dotnet/api/system.uribuilder">UriBuilder</a> and <a href="https://docs.microsoft.com/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder">SqlConnectionStringBuilder</a>. </p> <p> It's also worth noting that <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/copy-and-update-record-expressions">F# copy-and-update expressions</a> are endomorphisms. That's the reason that when you have immutable records, <a href="/2017/09/11/test-data-without-builders">you need no Test Data Builders</a>. </p> <h3 id="64b49f4143eb49fc80ab6344e247a0bd"> Summary <a href="#64b49f4143eb49fc80ab6344e247a0bd" title="permalink">#</a> </h3> <p> The Builder pattern comes in (at least) three variations: the Gang-of-Four Builder pattern, Fluent Builder, and Immutable Fluent Builder. All are isomorphic to each other, and are equivalent to the endomorphism monoid. </p> <p> Viewing Builders as endomorphisms may mostly be an academic exercise, but I think it highlights the sweet spot for the pattern. It's particularly useful when you wish to expose an API that offers simple defaults, while at the same time enabling client code to make changes to those defaults. When those changes involve several steps (as e.g. <code>AddJsonBody</code>) you can view each modifier method as a <a href="https://en.wikipedia.org/wiki/Facade_pattern">Facade</a>. </p> <p> <strong>Next:</strong> <a href="/2018/06/25/visitor-as-a-sum-type">Visitor as a sum type</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Builder isomorphisms https://blog.ploeh.dk/2020/02/10/builder-isomorphisms 2020-02-10T07:06:00+00:00 Mark Seemann <div id="post"> <p> <em>The Builder pattern is equivalent to the Fluent Builder pattern.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. An isomorphism is when a bi-directional lossless translation exists between two representations. Such translations exist between the <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder</a> pattern and two variations of the <em>Fluent Builder</em> pattern. Since the names sound similar, this is hardly surprising. </p> <p> <img src="/content/binary/builder-fluent-builder-isomorphism.png" alt="isomorphism between Builder, Fluent Builder, and Immutable Fluent Builder."> </p> <p> Given an implementation that uses one of those three patterns, you can translate your design into one of the other options. This doesn't imply that each is of equal value. When it comes to composability, both versions of Fluent Builder are superior to the classic Builder pattern. </p> <h3 id="553fdf908eb84ccb86a7c7972d45bc77"> A critique of the Maze Builder example <a href="#553fdf908eb84ccb86a7c7972d45bc77" title="permalink">#</a> </h3> <p> In these articles, I usually first introduce the form presented in <a href="http://amzn.to/XBYukB">Design Patterns</a>. The code example given by the Gang of Four is, however, problematic. I'll start by pointing out the problems and then proceed to present a simpler, more useful example. </p> <p> The book presents an example centred on a <code>MazeBuilder</code> abstract class. The original example is in C++, but I here present my C# interpretation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MazeBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BuildMaze</span>()&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BuildRoom</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">room</span>)&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BuildDoor</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">roomFrom</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">roomTo</span>)&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:#2b91af;">Maze</span>&nbsp;<span style="font-weight:bold;color:#74531f;">GetMaze</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As the book states, "the maze-building operations of MazeBuilder do nothing by default. They're not declared pure virtual to let derived classes override only those methods in which they're interested." This means that you could technically write a derived class that overrides only <code>BuildRoom</code>. That's unlikely to be useful, since <code>GetMaze</code> still returns <code>null</code>. </p> <p> Moreover, the presence of the <code>BuildMaze</code> method indicates <a href="https://en.wikipedia.org/wiki/Sequential_coupling">sequential coupling</a>. A client (a <em>Director</em>, in the pattern language of <em>Design Patterns</em>) is supposed to first call <code>BuildMaze</code> before calling any of the other methods. What happens if a client forgets to call <code>BuildMaze</code>? What happens if client code calls the method <em>after</em> some of the other methods. What happens if it calls it multiple times? </p> <p> Another issue with the sample code is that it's unclear how it accomplishes its stated goal of separating "the construction of a complex object from its representation." The <code>StandardMazeBuilder</code> presented seems tightly coupled to the <code>Maze</code> class to a degree where it's hard to see how to untangle the two. The book fails to make a compelling example by instead presenting a <code>CountingMazeBuilder</code> that never implements <code>GetMaze</code>. It never constructs the desired complex object. </p> <p> Don't interpret this critique as a sweeping dismissal of the pattern, or the book in general. As this article series implies, I've invested significant energy in it. I consider the book seminal, but we've learned much since its publication in 1994. A common experience is that not all of the patterns in the book are equally useful, and of those that are, some are useful for different reasons than the book gives. The Builder pattern is an example of that. </p> <p> The Builder pattern isn't useful only because it enables you to "separate the construction of a complex object from its representation." It's useful because it enables you to present an API that comes with good default behaviour, but which can be tweaked into multiple configurations. The pattern is useful even without polymorphism. </p> <h3 id="ed99bc3715f541be8b6b9239848395e3"> HTTP request Builder <a href="#ed99bc3715f541be8b6b9239848395e3" title="permalink">#</a> </h3> <p> The <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httprequestmessage">HttpRequestMessage</a> class is a versatile API with good default behaviour, but it can be a bit awkward if you want to make an HTTP request with a body and particular headers. You can often get around the problem by using methods like <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpclient.postasync">PostAsync</a> on <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpclient">HttpClient</a>, but sometimes you need to drop down to <a href="https://docs.microsoft.com/dotnet/api/system.net.http.httpclient.sendasync">SendAsync</a>. When that happens, you need to build your own <code>HttpRequestMessage</code> objects. A Builder can encapsulate some of that work. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;url; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">object</span>?&nbsp;jsonBody; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="font-weight:bold;color:#1f377f;">url</span>))&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.url&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;=&nbsp;<span style="color:#2b91af;">HttpMethod</span>.Get; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpMethod</span>&nbsp;Method&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">object</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.jsonBody&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Build</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>(Method,&nbsp;url); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#74531f;">BuildBody</span>(<span style="font-weight:bold;color:#1f377f;">message</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BuildBody</span>(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(jsonBody&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">json</span>&nbsp;=&nbsp;<span style="color:#2b91af;">JsonConvert</span>.<span style="color:#74531f;">SerializeObject</span>(jsonBody); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Content&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StringContent</span>(<span style="font-weight:bold;color:#1f377f;">json</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>.Content.Headers.ContentType.MediaType&nbsp;=&nbsp;<span style="color:#a31515;">&quot;application/json&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Compared to <em>Design Patterns'</em> example, <code>HttpRequestMessageBuilder</code> isn't polymorphic. It doesn't inherit from a base class or implement an interface. As I pointed out in my critique of the <code>MazeBuilder</code> example, polymorphism doesn't seem to be the crux of the matter. You could easily introduce a base class or interface that defines the <code>Method</code>, <code>AddJsonBody</code>, and <code>Build</code> members, but what would be the point? Just like the <code>MazeBuilder</code> example fails to present a compelling <em>second</em> implementation, I can't think of another useful implementation of a hypothetical <code>IHttpRequestMessageBuilder</code> interface. </p> <p> Notice that I dropped the <em>Build</em> prefix from most of the Builder's members. Instead, I reserved the word <code>Build</code> for the method that actually creates the desired object. This is consistent with most modern Builder examples I've encountered. </p> <p> The <code>HttpRequestMessageBuilder</code> comes with a reasonable set of default behaviours. If you just want to make a <code>GET</code> request, you can easily do that: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>); <span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>.<span style="font-weight:bold;color:#74531f;">Build</span>(); <span style="color:#2b91af;">HttpClient</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">client</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#74531f;">GetClient</span>(); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">client</span>.<span style="font-weight:bold;color:#74531f;">SendAsync</span>(<span style="font-weight:bold;color:#1f377f;">msg</span>);</pre> </p> <p> Since you only call the <code>builder</code>'s <code>Build</code> method, but never any of the other members, you get the default behaviour. A <code>GET</code> request with no body. </p> <p> Notice that the <code>HttpRequestMessageBuilder</code> protects its invariants. It follows the maxim that you should never be able to put an object into an invalid state. Contrary to <em>Design Patterns'</em> <code>StandardMazeBuilder</code>, it uses its constructors to enforce an invariant. Regardless of what sort of <code>HttpRequestMessage</code> you want to build, it must have a URL. Both constructor overloads require all clients to supply one. (In order to keep the code example as simple as possible, I've omitted all sorts of precondition checks, like checking that <code>url</code> isn't null, that it's a valid URL, and so on.) </p> <p> If you need to make a <code>POST</code> request with a JSON body, you can change the defaults: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>); <span style="font-weight:bold;color:#1f377f;">builder</span>.Method&nbsp;=&nbsp;<span style="color:#2b91af;">HttpMethod</span>.Post; <span style="font-weight:bold;color:#1f377f;">builder</span>.<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">new</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">NewGuid</span>(), &nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2020-03-22&nbsp;19:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Ælfgifu&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;ælfgifu@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;quantity&nbsp;=&nbsp;1&nbsp;}); <span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>.<span style="font-weight:bold;color:#74531f;">Build</span>(); <span style="color:#2b91af;">HttpClient</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">client</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#74531f;">GetClient</span>(); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">response</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">client</span>.<span style="font-weight:bold;color:#74531f;">SendAsync</span>(<span style="font-weight:bold;color:#1f377f;">msg</span>);</pre> </p> <p> Other combinations of <code>Method</code> and <code>AddJsonBody</code> are also possible. You could, for example, make a <code>DELETE</code> request without a body by only changing the <code>Method</code>. </p> <p> This incarnation of <code>HttpRequestMessageBuilder</code> is cumbersome to use. You must first create a <code>builder</code> object and then mutate it. Once you've invoked its <code>Build</code> method, you rarely need the object any longer, but the <code>builder</code> variable is still in scope. You can address those usage issues by refactoring a Builder to a Fluent Builder. </p> <h3 id="38b145cd45674bd989ebd37ca92b40f6"> HTTP request Fluent Builder <a href="#38b145cd45674bd989ebd37ca92b40f6" title="permalink">#</a> </h3> <p> In the Gang of Four Builder pattern, no methods return anything, except the method that creates the object you're building (<code>GetMaze</code> in the <code>MazeBuilder</code> example, <code>Build</code> in the <code>HttpRequestMessageBuilder</code> example). It's always possible to refactor such a Builder so that the <code>void</code> methods return something. They can always return the object itself: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpMethod</span>&nbsp;Method&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithMethod</span>(<span style="color:#2b91af;">HttpMethod</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">this</span>; } <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">object</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.jsonBody&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">this</span>; }</pre> </p> <p> Changing <code>AddJsonBody</code> is as easy as changing its return type and returning <code>this</code>. Refactoring the <code>Method</code> property is a bit more involved. It's a language feature of C# (and a few other languages) that classes can have properties, so this concern isn't general. In languages without properties, things are simpler. In C#, however, I chose to make the property setter private and instead add a method that returns <code>HttpRequestMessageBuilder</code>. Perhaps it's a little confusing that the name of the method includes the word <em>method</em>, but keep in mind that the method in question is an HTTP method. </p> <p> You can now create a <code>GET</code> request with a one-liner: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>).<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> You don't have to declare any <code>builder</code> variable to mutate. Even when you need to change the defaults, you can just start with a builder and keep on chaining method calls: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">WithMethod</span>(<span style="color:#2b91af;">HttpMethod</span>.Post) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">new</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">NewGuid</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2020-03-22&nbsp;19:30:00&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Ælfgifu&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;ælfgifu@example.net&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity&nbsp;=&nbsp;1&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> This creates a <code>POST</code> request with a JSON message body. </p> <p> We can call this pattern <em>Fluent Builder</em> because this version of the Builder pattern has a <a href="https://www.martinfowler.com/bliki/FluentInterface.html">Fluent Interface</a>. </p> <p> This usually works well enough in practice, but is vulnerable to <a href="https://en.wikipedia.org/wiki/Aliasing_(computing)">aliasing</a>. What happens if you reuse an <code>HttpRequestMessageBuilder</code> object? </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">deleteMsg</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>.<span style="font-weight:bold;color:#74531f;">WithMethod</span>(<span style="color:#2b91af;">HttpMethod</span>.Delete).<span style="font-weight:bold;color:#74531f;">Build</span>(); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">getMsg</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> As the variable names imply, the programmer responsible for these three lines of code incorrectly believed that without the call to <code>WithMethod</code>, the <code>builder</code> will use its default behaviour when <code>Build</code> is called. The previous line of code, however, mutated the <code>builder</code> object. Its <code>Method</code> property remains <code>HttpMethod.Delete</code> until another line of code changes it! </p> <h3 id="a75c7962ece449f6b6161fe6ae7beb1e"> HTTP request Immutable Fluent Builder <a href="#a75c7962ece449f6b6161fe6ae7beb1e" title="permalink">#</a> </h3> <p> You can disarm the aliasing booby trap by making the Fluent Builder immutable. A good first step in that refactoring is making sure that all class fields are <code>readonly</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;url; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">object</span>?&nbsp;jsonBody;</pre> </p> <p> The <code>url</code> field was already marked <code>readonly</code>, so the change only applies to the <code>jsonBody</code> field. In addition to the class fields, don't forget any automatic properties: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpMethod</span>&nbsp;Method&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}</pre> </p> <p> The <code>HttpMethod</code> property previously had a <code>private</code> setter, but this is now gone. It's also strictly read only. </p> <p> Now that all data is read only, the only way you can 'change' values is via a constructor. Add a constructor overload that receives all data and chain the other constructors into it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="font-weight:bold;color:#1f377f;">url</span>))&nbsp;{&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>)&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:#2b91af;">HttpMethod</span>.Get,&nbsp;<span style="color:blue;">null</span>)&nbsp;{&nbsp;} <span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="color:#2b91af;">Uri</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>,&nbsp;<span style="color:#2b91af;">HttpMethod</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">method</span>,&nbsp;<span style="color:blue;">object</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.url&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">url</span>; &nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">method</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.jsonBody&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>; }</pre> </p> <p> I'm usually not keen on allowing <code>null</code> arguments, but I made the all-encompassing constructor <code>private</code>. In that way, at least no client code gets the wrong idea. </p> <p> The optional modification methods can now only do one thing: return a new object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithMethod</span>(<span style="color:#2b91af;">HttpMethod</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(url,&nbsp;<span style="font-weight:bold;color:#1f377f;">newMethod</span>,&nbsp;jsonBody); } <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>&nbsp;<span style="font-weight:bold;color:#74531f;">AddJsonBody</span>(<span style="color:blue;">object</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(url,&nbsp;Method,&nbsp;<span style="font-weight:bold;color:#1f377f;">jsonBody</span>); }</pre> </p> <p> The client code looks the same as before, but now you no longer have an aliasing problem: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">deleteMsg</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>.<span style="font-weight:bold;color:#74531f;">WithMethod</span>(<span style="color:#2b91af;">HttpMethod</span>.Delete).<span style="font-weight:bold;color:#74531f;">Build</span>(); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">getMsg</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">builder</span>.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> Now <code>deleteMsg</code> represents a <code>Delete</code> request, and <code>getMsg</code> truly represents a <code>GET</code> request. </p> <p> Since this variation of the Fluent Builder pattern is immutable, it's natural to call it an <em>Immutable Fluent Builder</em>. </p> <p> You've now seen how to refactor from Builder via Fluent Builder to Immutable Fluent Builder. If these three pattern variations are truly isomorphic, it should also be possible to move in the other direction. I'll leave it as an exercise for the reader to do this with the HTTP request Builder example. Instead, I will briefly discuss another example that starts at the Fluent Builder pattern. </p> <h3 id="87464a8b3f4d4922b3929c50e098c344"> Test Data Fluent Builder <a href="#87464a8b3f4d4922b3929c50e098c344" title="permalink">#</a> </h3> <p> A prominent example of the Fluent Builder pattern would be the set of all <a href="http://www.natpryce.com/articles/000714.html">Test Data Builders</a>. I'm going to use the example I've <a href="/2017/08/15/test-data-builders-in-c">already covered</a>. You can visit the previous article for all details, but in summary, you can, for example, write code like this: </p> <p> <pre><span style="color:#2b91af;">Address</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">address</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>().<span style="font-weight:bold;color:#74531f;">WithCity</span>(<span style="color:#a31515;">&quot;Paris&quot;</span>).<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> This creates an <code>Address</code> object with the <code>City</code> property set to <code>"Paris"</code>. The <code>Address</code> class comes with other properties. You can trust that the <code>AddressBuilder</code> gave them values, but you don't know what they are. You can use this pattern in unit tests when you need an <code>Address</code> in <a href="https://en.wikipedia.org/wiki/Paris">Paris</a>, but you don't care about any of the other data. </p> <p> In my previous article, I implemented <code>AddressBuilder</code> as a Fluent Builder. I did that in order to stay as true to <a href="http://www.natpryce.com">Nat Pryce</a>'s original example as possible. Whenever I use the Test Data Builder pattern in earnest, however, I use the immutable variation so that I avoid the aliasing issue. </p> <h3 id="4274fe6c008e41bd8d4e596b9cc11762"> Test Data Builder as a Gang-of-Four Builder <a href="#4274fe6c008e41bd8d4e596b9cc11762" title="permalink">#</a> </h3> <p> You can easily refactor a typical Test Data Builder like <code>AddressBuilder</code> to a shape more reminiscent of the Builder pattern presented in <em>Design Patterns</em>. Apart from the <code>Build</code> method that produces the object being built, change all other methods to <code>void</code> methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">string</span>&nbsp;street; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">string</span>&nbsp;city; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">PostCode</span>&nbsp;postCode; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.street&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.city&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.postCode&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCodeBuilder</span>().<span style="font-weight:bold;color:#74531f;">Build</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithStreet</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newStreet</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.street&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">newStreet</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithCity</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newCity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.city&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">newCity</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithPostCode</span>(<span style="color:#2b91af;">PostCode</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newPostCode</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.postCode&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">newPostCode</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithNoPostcode</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.postCode&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCode</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Build</span>() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:blue;">this</span>.street,&nbsp;<span style="color:blue;">this</span>.city,&nbsp;<span style="color:blue;">this</span>.postCode); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can still build a test address in Paris, but it's now more inconvenient. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">addressBuilder</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>(); <span style="font-weight:bold;color:#1f377f;">addressBuilder</span>.<span style="font-weight:bold;color:#74531f;">WithCity</span>(<span style="color:#a31515;">&quot;Paris&quot;</span>); <span style="color:#2b91af;">Address</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">address</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">addressBuilder</span>.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> You can still use multiple Test Data Builders to build more complex test data, but the classic Builder pattern doesn't compose well. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">invoiceBuilder</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvoiceBuilder</span>(); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">recipientBuilder</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RecipientBuilder</span>(); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">addressBuilder</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>(); <span style="font-weight:bold;color:#1f377f;">addressBuilder</span>.<span style="font-weight:bold;color:#74531f;">WithNoPostcode</span>(); <span style="font-weight:bold;color:#1f377f;">recipientBuilder</span>.<span style="font-weight:bold;color:#74531f;">WithAddress</span>(<span style="font-weight:bold;color:#1f377f;">addressBuilder</span>.<span style="font-weight:bold;color:#74531f;">Build</span>()); <span style="font-weight:bold;color:#1f377f;">invoiceBuilder</span>.<span style="font-weight:bold;color:#74531f;">WithRecipient</span>(<span style="font-weight:bold;color:#1f377f;">recipientBuilder</span>.<span style="font-weight:bold;color:#74531f;">Build</span>()); <span style="color:#2b91af;">Invoice</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">invoice</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">invoiceBuilder</span>.<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> <p> These seven lines of code creates an <code>Invoice</code> object with a address without a post code. Compare that with the Fluent Builder <a href="/2017/08/15/test-data-builders-in-c#de2e6fb74f6f4319a0fef86dcd9b839e">example in the previous article</a>. This is a clear example that while the variations are isomorphic, they aren't equally useful. The classic Builder pattern isn't as practical as one of the Fluent variations. </p> <p> You might protest that this variation of <code>AddressBuilder</code>, <code>InvoiceBuilder</code>, etcetera isn't equivalent to the Builder pattern. After all, the Builder shown in <em>Design Patterns</em> is polymorphic. That's really not an issue, though. Just extract an interface from the concrete builder: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IAddressBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Build</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithCity</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newCity</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithNoPostcode</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithPostCode</span>(<span style="color:#2b91af;">PostCode</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newPostCode</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">WithStreet</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">newStreet</span>); }</pre> </p> <p> Make the concrete class implement the interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IAddressBuilder</span></pre> </p> <p> You could argue that this adds no value. You'd be right. This goes contrary to the <a href="http://www.codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle</a>. I think that the same criticism applies to <em>Design Patterns</em>' original description of the pattern, as I've already pointed out. The utility in the pattern comes from how it gives client code good defaults that it can then tweak as necessary. </p> <h3 id="a1e866bafec74def9a66f5ba96a0cca4"> Summary <a href="#a1e866bafec74def9a66f5ba96a0cca4" title="permalink">#</a> </h3> <p> The Builder pattern was originally described in <em>Design Patterns</em>. Later, smart people like Nat Pryce figured out that by letting each mutating operation return the (mutated) Builder, such a Fluent API offered superior composability. A further improvement to the Fluent Builder pattern makes the Builder immutable in order to avoid aliasing issues. </p> <p> All three variations are isomorphic. Work that one of these variations afford is also afforded by the other variations. </p> <p> On the other hand, the variations aren't equally useful. Fluent APIs offer superior composability. </p> <p> <strong>Next:</strong> <a href="/2018/05/22/church-encoding">Church encoding</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f3f59d590234452aa6a9cd7ba56779f0"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#f3f59d590234452aa6a9cd7ba56779f0">#</a></div> <div class="comment-content"> <blockquote> <p> You can now[, with the fluent builder implementation,] create a <code>GET</code> request with a one-liner: </p> <p> <pre><span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">msg</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessageBuilder</span>(<span style="font-weight:bold;color:#1f377f;">url</span>).<span style="font-weight:bold;color:#74531f;">Build</span>();</pre> </p> </blockquote> <p> It is also possible to write that one-liner with the original (non-fluent) builder implementation. Did you mean to show how it is possible with the fluent builder implementation to create a <code>DELETE</code> request with a one-liner? You have such an example two code blocks later. </p> </div> <div class="comment-date">2020-02-25 02:18 UTC</div> </div> <div class="comment" id="c12cb112352645a293bc5ecaf7d9c9aa"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c12cb112352645a293bc5ecaf7d9c9aa">#</a></div> <div class="comment-content"> <p> Tyson, you are, of course, right. The default behaviour could also have been a one-liner with the non-fluent design. Every other configuration, however, can't be a one-liner with the Gang-of-Four pattern, while it can in the Fluent guise. </p> </div> <div class="comment-date">2020-02-25 6:44 UTC</div> </div> <div class="comment" id="ecec868120f644c28381b188ee2e75e4"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#ecec868120f644c28381b188ee2e75e4">#</a></div> <div class="comment-content"> <p> Among the example uses of your <code>HttpRequestMessageBuilder</code>, I see three HTTP verbs used: <code>GET</code>, <code>DELETE</code>, and <code>POST</code>. Furthermore, a body is added if and only if the method is <code>POST</code>. This matches my expectations gained from my limited experience doing web programming. If a <code>GET</code> or <code>DELETE</code> request had a body or if a <code>POST</code> request did not have a body, then I would suspect that such behavior was a bug. </p> <p> For the sake of a question that I would like to ask, let's suppose that a body must be added if and only if the method is <code>POST</code>. Under this assumption, <code>HttpRequestMessageBuilder</code> can create invalid messages. For example, it can create a <code>GET</code> request with a body, and it can create a <code>POST</code> request without a body. Under this assumption, how would you modify your design so that only valid messages can be created? </p> </div> <div class="comment-date">2020-02-25 14:34 UTC</div> </div> <div class="comment" id="1a3c2d6c7d484c5da92e49ce81c8755d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1a3c2d6c7d484c5da92e49ce81c8755d">#</a></div> <div class="comment-content"> <p> Tyson, thank you for another inspiring question! It gives me a good motivation to write about polymorphic Builders. I'll try to address this question in a future article. </p> </div> <div class="comment-date">2020-03-02 8:40 UTC</div> </div> <div class="comment" id="484b7f2bd2714a2d8c08adece0955668"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#484b7f2bd2714a2d8c08adece0955668">#</a></div> <div class="comment-content"> <p> Tyson, I've now attempted to answer your question in <a href="/2020/03/09/polymorphic-builder">a new article</a>. </p> </div> <div class="comment-date">2020-03-09 6:53 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Non-exceptional averages https://blog.ploeh.dk/2020/02/03/non-exceptional-averages 2020-02-03T06:38:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you code without exceptions? Here's one example.</em> </p> <p> Encouraging object-oriented programmers to <a href="https://hackernoon.com/the-throw-keyword-was-a-mistake-l9e532di">avoid throwing exceptions</a> is as fun as telling them to renounce null references. To be fair, exception-throwing is such an ingrained feature of C#, Java, C++, etcetera that it can be hard to see how to do without it. </p> <p> To be clear, I don't insist that you pretend that exceptions don't exist in languages that have them. I'm also <a href="https://eiriktsarpalis.wordpress.com/2017/02/19/youre-better-off-using-exceptions">not advocating that you catch all exceptions in order to resurface them as railway-oriented programming</a>. On the other hand, I do endorse the generally good advice that you shouldn't use exceptions for <a href="https://en.wikipedia.org/wiki/Control_flow">control flow</a>. </p> <p> What can you do instead? Despite <a href="https://fsharpforfunandprofit.com/posts/against-railway-oriented-programming">all the warnings against railway-oriented programming</a>, <a href="/2018/06/11/church-encoded-either">Either</a> is still a good choice for a certain kind of control flow. Exceptions are for <em>exceptional</em> situations, such as network partitions, running out of memory, disk failures, and so on. Many run-time errors are both foreseeable and preventable. Prefer code that prevents errors. </p> <p> There's a few ways you can do that. One of them is to protect invariants by enforcing pre-conditions. If you have a static type system, you can use the type system to prevent errors. </p> <h3 id="ccb791521ac0417f870a2fe4dac946c7"> Average duration <a href="#ccb791521ac0417f870a2fe4dac946c7" title="permalink">#</a> </h3> <p> How would you calculate the average of a set of durations? You might, for example, <a href="/2017/06/27/pure-times">need to calculate average duration of message handling for a polling consumer</a>. C# offers many built-in overloads of the <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.average">Average</a> extension method, but none that calculates the average of <a href="https://docs.microsoft.com/dotnet/api/system.timespan">TimeSpan</a> values. </p> <p> How would you write that method yourself? </p> <p> It's not a trick question. </p> <p> Based on my experience coaching development teams, this is a representative example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.Zero; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+=</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>++; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>; }</pre> </p> <p> This gets the job done in most situations, but it has two error modes. It doesn't work if <code>timeSpans</code> is empty, and it doesn't work if it's infinite. </p> <p> When the input collection is empty, you'll be trying to divide by zero, which isn't allowed. How do you deal with that? Most programmers I've met just shrug and say: <em>don't call the method with an empty collection.</em> Apparently, it's your responsibility as the caller. You have to memorise that this particular <code>Average</code> method has that particular precondition. </p> <p> I don't think that's a professional position. This puts the burden on client developers. In a world like that, you have to learn by rote the preconditions of thousands of APIs. </p> <p> What can you do? You could add a <a href="https://en.wikipedia.org/wiki/Guard_(computer_science)">Guard Clause</a> to the method. </p> <h3 id="38f629930c7845c0af1c50c586dfcb82"> Guard Clause <a href="#38f629930c7845c0af1c50c586dfcb82" title="permalink">#</a> </h3> <p> Adding a Guard Clause doesn't really make the method much easier to reason about for client developers, but at least it protects an invariant. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.<span style="font-weight:bold;color:#74531f;">Any</span>()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentOutOfRangeException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">timeSpans</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Can&#39;t&nbsp;calculate&nbsp;the&nbsp;average&nbsp;of&nbsp;an&nbsp;empty&nbsp;collection.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.Zero; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+=</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>++; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">count</span>; }</pre> </p> <p> Don't get me wrong. I often write code like this because it makes it easier for me as a library developer to reason about the rest of the method body. On the other hand, it basically just replaces one run-time exception with another. Before I added the Guard Clause, calling <code>Average</code> with an empty collection would cause it to throw an <code>OverflowException</code>; now it throws an <code>ArgumentOutOfRangeException</code>. </p> <p> From client developers' perspective, this is only a marginal improvement. You're still getting no help from the type system, but at least the run-time error is a bit more informative. Sometimes, that's the best you can do. </p> <h3 id="3825bb6fb4db4cd28b9134763d202b3a"> Finite collections <a href="#3825bb6fb4db4cd28b9134763d202b3a" title="permalink">#</a> </h3> <p> The <code>Average</code> method has two preconditions, but we've only addressed one. The other precondition is that the input <code>timeSpans</code> must be finite. Unfortunately, this compiles: </p> <p> <pre><span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;<span style="font-weight:bold;color:#74531f;">InfinitelyRepeat</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">while</span>&nbsp;(<span style="color:blue;">true</span>)&nbsp;<span style="font-weight:bold;color:#8f08c4;">yield</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>; } <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>(1,&nbsp;2,&nbsp;3,&nbsp;4); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">tss</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#74531f;">InfinitelyRepeat</span>(<span style="font-weight:bold;color:#1f377f;">ts</span>); <span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">avg</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">tss</span>.<span style="font-weight:bold;color:#74531f;">Average</span>();</pre> </p> <p> Since <code>tss</code> infinitely repeats <code>ts</code>, the <code>Average</code> method call (theoretically) loops forever; in fact it quickly overflows because it keeps adding <code>TimeSpan</code> values together. </p> <p> Infinite collections aren't allowed. Can you make that precondition explicit? </p> <p> I don't know of a way to test that <code>timeSpans</code> is finite at run time, but I can change the input type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.<span style="font-weight:bold;color:#74531f;">Any</span>()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentOutOfRangeException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">timeSpans</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Can&#39;t&nbsp;calculate&nbsp;the&nbsp;average&nbsp;of&nbsp;an&nbsp;empty&nbsp;collection.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.Zero; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+=</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Count; }</pre> </p> <p> Instead of accepting any <code>IEnumerable&lt;TimeSpan&gt;</code> as an input argument, I've now constrained <code>timeSpans</code> to an <code>IReadOnlyCollection&lt;TimeSpan&gt;</code>. <a href="https://docs.microsoft.com/dotnet/api/system.collections.generic.ireadonlycollection-1">This interface</a> has been in .NET since .NET 4.5 (<a href="/2013/07/20/linq-versus-the-lsp">I think</a>), but it lives a quiet existence. Few people know of it. </p> <p> It's just <code>IEnumerable&lt;T&gt;</code> with an extra constraint: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;Count&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> The <code>Count</code> property strongly implies that the <code>IEnumerable&lt;T&gt;</code> is finite. Also, that the value is an <code>int</code> implies that the maximum size of the collection is 2,147,483,647. That's probably going to be enough for most day-to-day use. </p> <p> You can no longer pass an infinite stream of values to the <code>Average</code> method. It's simply not going to compile. That both communicates and protects the invariant that infinite collections aren't allowed. It also makes the implementation code simpler, since the method doesn't have to count the elements. That information is already available from <code>timeSpans.Count</code>. </p> <p> If a type can address one invariant, can it also protect the other? </p> <h3 id="b1464fa0dafc42838b6f7db24d8356c8"> Non-empty collection <a href="#b1464fa0dafc42838b6f7db24d8356c8" title="permalink">#</a> </h3> <p> You can change the input type again. Here I've used <a href="/2017/12/11/semigroups-accumulate">this NotEmptyCollection&lt;T&gt; implementation</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Head; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Tail) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+=</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">ts</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Count; }</pre> </p> <p> Now client code can no longer call the <code>Average</code> method with an empty collection. That's also not going to compile. </p> <p> You've replaced a run-time check with a compile-time check. It's now clear to client developers who want to call the method that they must supply a <code>NotEmptyCollection&lt;TimeSpan&gt;</code>, instead of just any <code>IReadOnlyCollection&lt;TimeSpan&gt;</code>. </p> <p> You can also simplify the implementation code: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#74531f;">Average</span>(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.<span style="font-weight:bold;color:#74531f;">Aggregate</span>((<span style="font-weight:bold;color:#1f377f;">x</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">y</span>)&nbsp;=&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">x</span>&nbsp;<span style="font-weight:bold;color:#74531f;">+</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">y</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sum</span>&nbsp;<span style="font-weight:bold;color:#74531f;">/</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">timeSpans</span>.Count; }</pre> </p> <p> How do we know that <code>NotEmptyCollection&lt;T&gt;</code> contains at least one element? The constructor enforces that constraint: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">NotEmptyCollection</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">head</span>,&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">T</span>[]&nbsp;<span style="font-weight:bold;color:#1f377f;">tail</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">head</span>&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">head</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Head&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">head</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Tail&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">tail</span>; }</pre> </p> <p> But wait, there's a Guard Clause and a <code>throw</code> there! Have we even accomplished anything, or did we just move the <code>throw</code> around? </p> <h3 id="ef73390937dc4f88997c7d42496b591e"> Parse, don't validate <a href="#ef73390937dc4f88997c7d42496b591e" title="permalink">#</a> </h3> <p> A Guard Clause is a kind of validation. It validates that input fulfils preconditions. The problem with validation is that you have to repeat it in various different places. Every time you receive some data as an input argument, it may or may not have been validated. A receiving method can't tell. There's no flag on a string, or a number, or a collection, which is set when data has been validated. </p> <p> Every method that receives such an input will have to perform validation, just to be sure that the preconditions hold. This leads to validation code being duplicated over a code base. When you duplicate code, you later update it in most of the places it appears, but forget to update it in a few places. Even if you're meticulous, a colleague may not know about the proper way of validating a piece of data. This leads to bugs. </p> <p> As <a href="https://lexi-lambda.github.io">Alexis King</a> explains in her <a href="https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate">Parse, don’t validate</a> article, 'parsing' is the process of validating input of weaker type into a value of a stronger type. The stronger type indicates that validation has happened. It's like a Boolean flag that indicates that, yes, the data contained in the type has been through validation, and found to hold. </p> <p> This is also the case of <code>NotEmptyCollection&lt;T&gt;</code>. If you have an object of that type, you know that it has already been validated. You know that the collection isn't empty. Even if you think that it looks like we've just replaced one exception with another, that's not the point. The point is that we've replaced scattered and unsystematic validation code with a single verification step. </p> <p> You may still be left with the nagging doubt that I didn't really avoid throwing an exception. I think that the <code>NotEmptyCollection&lt;T&gt;</code> constructor strikes a pragmatic balance. If you look only at the information revealed by the type (i.e. what an <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> would display), you'll see this when you program against the class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">NotEmptyCollection</span>(<span style="color:#2b91af;">T</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">head</span>,&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">T</span>[]&nbsp;<span style="font-weight:bold;color:#1f377f;">tail</span>)</pre> </p> <p> While you could, technically, pass <code>null</code> as the <code>head</code> parameter, it should be clear to you that you're trying to do something you're not supposed to do: <code>head</code> is <em>not</em> an optional argument. Had it been optional, the API designer should have provided an overload that you could call without any value. Such a constructor overload isn't available here, so if you try to cheat the compiler by passing <code>null</code>, don't be surprised to get a run-time exception. </p> <p> For what it's worth, I believe that you can only be pragmatic if you know how to be dogmatic. Is it possible to protect <code>NotEmptyCollection&lt;T&gt;</code>'s invariants without throwing exceptions? </p> <p> Yes, you could do that by making the constructor <code>private</code> and instead afford a static factory method that returns a <a href="/2018/03/26/the-maybe-functor">Maybe</a> or <a href="/2018/06/11/church-encoded-either">Either</a> value. In <a href="https://www.haskell.org">Haskell</a>, this is typically called a <em>smart constructor</em>. It's only a few lines of code, so I could easily show it here. I chose not to, though, because I'm concerned that readers will interpret this article the wrong way. I like Maybe and Either a lot, but I agree with the above critics that it may not be <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> in object-oriented languages. </p> <h3 id="737101b053e847f08124d49659804bf6"> Summary <a href="#737101b053e847f08124d49659804bf6" title="permalink">#</a> </h3> <p> <em>Encapsulation</em> is central to object-oriented design. It's the notion that it's an object's own responsibility to protect its invariants. In statically typed object-oriented programming languages, objects are instances of classes. Classes are types. Types encapsulate invariants; they carry with them guarantees. </p> <p> You can sometimes model invariants by using types. Instead of performing a run-time check on input arguments, you can declare constructors and methods in such a way that they only take arguments that are already guaranteed to be valid. </p> <p> That's one way to reduce the amount of exceptions that your code throws. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="127722bf00aa49c8aa467df2200028f6"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#127722bf00aa49c8aa467df2200028f6">#</a></div> <div class="comment-content"> <p> Great post. I too prefer to avoid exceptions by strengthening preconditions using types. </p> <blockquote> Since <code>tss</code> infinitely repeats <code>ts</code>, the <code>Average</code> method call (theoretically) loops forever; in fact it quickly overflows because it keeps adding <code>TimeSpan</code> values together. </blockquote> <p> I am not sure what you mean here.  My best guess is that you are saying that this code would execute forever except that it will overflow, which will halt the execution.  However, I think the situation is ambiguous.  This code is impure because, as the <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked-and-unchecked">Checked and Unchecked documentation</a> says, its behavior depends on whether or not the <code>-checked</code> compiler option is given.  This dependency on the compiler option can be removed by wrapping this code in a <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked">checked</a> or <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unchecked">unchecked</a> block, which would either result in a thrown exception or an infinite loop respectively. </p> <blockquote> This gets the job done in most situations, but it has two error modes. It doesn't work if <code>timeSpans</code> is empty, and it doesn't work if it's infinite. </blockquote> <p> There is a third error mode, and it exists in every implementation you gave.  The issue of overflow is not restricted to the case of infinitely many <code>TimeSpan</code>s.  It only takes two.  I know of or remember this bug as <a href="https://thebittheories.com/the-curious-case-of-binary-search-the-famous-bug-that-remained-undetected-for-20-years-973e89fc212">"the last binary search bug"</a>.  That article shows how to correctly compute the average of two integers without overflowing.  A correct implementation for computing the average of more than two integers is to map each element to a mixed fraction with the count as the divisor and then appropriately aggregate those values.  The implementation given in <a href="https://www.quora.com/How-can-I-compute-the-average-of-a-large-array-of-integers-without-running-into-overflow/answer/Mark-Gordon-6">this Quora answer</a> seems correct to me. </p> <p> I know all this is unrelated to the topic of your post, but I also know how much you prefer to use examples that avoid this kind of accidental complexity.  Me too!  However, I still like your example and can't think of a better one at the moment. </p> </div> <div class="comment-date">2020-02-05 14:13 UTC</div> </div> <div class="comment" id="0bf86e75e130497dbc8384a235f100f5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0bf86e75e130497dbc8384a235f100f5">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Given an infinite stream of values, the method throws an <code>OverflowException</code>. This is because <code>TimeSpan</code> addition explicitly does that: </p> <p> <pre>&gt; <span style="color:#2b91af;">TimeSpan</span>.MaxValue&nbsp;<span style="font-weight:bold;color:#74531f;">+</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>(1) <span style="color:red">System.OverflowException: TimeSpan overflowed because the duration is too long. + System.TimeSpan.Add(System.TimeSpan) + System.TimeSpan.op_Addition(System.TimeSpan, System.TimeSpan)</span></pre> </p> <p> This little snippet from <em>C# Interactive</em> also illustrates the third error mode that I hadn't considered. Good point, that. </p> </div> <div class="comment-date">2020-02-06 6:47 UTC</div> </div> <div class="comment" id="d3727f9523f24793adb6c632d1a93a67"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#d3727f9523f24793adb6c632d1a93a67">#</a></div> <div class="comment-content"> <p> Ah, yes. You are correct. Thanks for pointing out my mistake. Another way to verify this is inspecting <a href="https://referencesource.microsoft.com/#mscorlib/system/timespan.cs,153"><code>TimeSpan.Add</code> in Mircosoft's reference source</a>. I should have done those checks before posting. Thanks again! </p> </div> <div class="comment-date">2020-02-06 13:33 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Maître d' kata https://blog.ploeh.dk/2020/01/27/the-maitre-d-kata 2020-01-27T06:45:00+00:00 Mark Seemann <div id="post"> <p> <em>A programming kata.</em> </p> <p> I <a href="/2020/01/13/on-doing-katas">recently wrote about doing programming katas</a>. You can find katas in many different places. Some sites exist exclusively for that purpose, such as the <a href="http://bit.ly/codekatas">Coding Dojo</a> or <a href="http://codekata.com">CodeKata</a>. In other cases, you can find individual katas on blogs; one of my favourites is the <a href="http://claysnow.co.uk/recycling-tests-in-tdd">Diamond kata</a>. You can also lift exercises from other sources and treat them as katas. For example, I recently followed <a href="https://github.com/mikehadlow/Journeys">Mike Hadlow's lead</a> and <a href="/2019/10/28/a-basic-haskell-solution-to-the-robot-journeys-coding-exercise">turned a job applicant test into a programming exercise</a>. I've also taken exercises from books and repurposed them. For example, <a href="/2017/10/23/convex-hull-monoid">I've implemented the Graham Scan algorithm for finding convex hulls</a> a couple of times. </p> <p> In this article, I'll share an exercise that I've found inspiring myself. I'll call it <em>the Ma&icirc;tre d' kata</em>. </p> <p> I present no code in this article. Part of what makes the exercise interesting, I think, is to figure out how to model the problem domain. I will, however, later publish one of my attempts at the kata. </p> <h3 id="73c0d7c81c1047d598cb6dbc8a49c99b"> Problem statement <a href="#73c0d7c81c1047d598cb6dbc8a49c99b" title="permalink">#</a> </h3> <p> Imagine that you're developing an online restaurant reservation system. Part of the behaviour of such a system is to decide whether or not to accept a reservation. At a real restaurant, employees fill various roles required to make it work. In a high-end restaurant, the <a href="https://en.wikipedia.org/wiki/Ma%C3%AEtre_d%27h%C3%B4tel">ma&icirc;tre d'</a> is responsible for taking reservations. I've named the kata after this role. If you're practising <a href="https://en.wikipedia.org/wiki/Domain-driven_design">domain-driven design</a>, you might want to name your object, class, or module <code>Ma&icirc;treD</code> or some such. </p> <p> The objective of the exercise is to implement the <code>Ma&icirc;treD</code> decision logic. </p> <p> Reservations are accepted on a first-come, first-served basis. As long as the restaurant has available seats for the desired reservation, it'll accept it. </p> <p> A reservation contains, at a minimum, a date and time as well as a positive quantity. Here's some examples: <table> <thead> <tr> <td>Date</td> <td style="text-align:right">Quantity</td> </tr> </thead> <tbody> <tr> <td>August 8, 2050 at 19:30</td> <td style="text-align:right">3</td> </tr> <tr> <td>November 27, 2022 at 18:45</td> <td style="text-align:right">4</td> </tr> <tr> <td>February 27, 2014 at 13:22</td> <td style="text-align:right">12</td> </tr> </tbody> </table> </p> <p> Notice that dates can be in your future or past. You might want to assume that the ma&icirc;tre d' would reject reservations in the past, but you can't assume <em>when</em> the code runs (or ran), so don't worry about that. Notice also that quantities are positive integers. While a quantity shouldn't be negative or zero, it could conceivably be large. I find it realistic, however, to keep quantities at low two-digit numbers or less. </p> <p> A reservation will likely contain other data, such as the name of the person making the reservation, contact information such as email or phone number, possibly also an ID, and so on. You may add these details if you want to make the exercise more realistic, but they're not required. </p> <p> I'm going to present one feature requirement at a time. If you read the entire article before you do the exercise, it'd correspond to gathering detailed requirements before starting to code. Alternatively, you could read the first requirement, do the exercise, read the next requirement, refactor your code, and so on. This would simulate a situation where your organisation gradually uncovers how the system ought to work. </p> <h3 id="f538ee06b5c54215bae10f661385893c"> Boutique restaurant <a href="#f538ee06b5c54215bae10f661385893c" title="permalink">#</a> </h3> <p> As readers of <a href="/dippp">my book</a> may have detected, I'm a foodie. Some years ago I ate at <a href="https://www.blancanyc.com">Blanca</a> in Brooklyn. That restaurant has one communal bar where everyone sits. There was room for twelve people, and dinner started at 19:00 whether you arrived on time or not. Such restaurants actually exist. It's an easy first step for the kata. Assume that the restaurant is only open for dinner, has no second seating, and a single shared table. This implies that the time of day of reservations doesn't matter, while the date still matters. Some possible test cases could be: <table> <thead> <tr> <td style="text-align:right">Table size</td> <td>Existing reservations</td> <td>Candidate reservation</td> <td>Expected outcome</td> </tr> </thead> <tbody> <tr> <td style="text-align:right">12</td> <td><em>none</em></td> <td>Quantity: 1</td> <td>Accepted</td> </tr> <tr> <td style="text-align:right">12</td> <td><em>none</em></td> <td>Quantity: 13</td> <td>Rejected</td> </tr> <tr> <td style="text-align:right">12</td> <td><em>none</em></td> <td>Quantity: 12</td> <td>Accepted</td> </tr> <tr> <td style="text-align:right">4</td> <td>Quantity: 2, Date: 2023-09-14</td> <td>Quantity: 3, Date: 2023-09-14</td> <td>Rejected</td> </tr> <tr> <td style="text-align:right">10</td> <td>Quantity: 2, Date: 2023-09-14</td> <td>Quantity: 3, Date: 2023-09-14</td> <td>Accepted</td> </tr> <tr> <td style="text-align:right">10</td> <td> Quantity: 3, Date: 2023-09-14<br> Quantity: 2, Date: 2023-09-14<br> Quantity: 3, Date: 2023-09-14 </td> <td>Quantity: 3, Date: 2023-09-14</td> <td>Rejected</td> </tr> <tr> <td style="text-align:right">4</td> <td>Quantity: 2, Date: 2023-09-15</td> <td>Quantity: 3, Date: 2023-09-14</td> <td>Accepted</td> </tr> </tbody> </table> </p> <p> This may not be an exhaustive set of test cases, but hopefully illustrates the desired behaviour. Try using the <a href="/2019/10/07/devils-advocate">Devil's Advocate technique</a> or <a href="/property-based-testing-intro">property-based testing</a> to identify more test cases. </p> <h3 id="05f0ec26d90044288845f5ad308742c2"> Haute cuisine <a href="#05f0ec26d90044288845f5ad308742c2" title="permalink">#</a> </h3> <p> The single-shared-table configuration is unusual. Most restaurants have separate tables. High-end restaurants like those on the <a href="https://www.theworlds50best.com">World's 50 best</a> list, or those with <a href="https://en.wikipedia.org/wiki/Michelin_Guide">Michelin stars</a> often have only a single seating. This is a good expansion of the domain logic. </p> <p> Assume that a restaurant has several tables, perhaps of different sizes. A table for four will seat one, two, three, or four people. Once a table is reserved, however, all the seats at that table are reserved. A reservation for three people will occupy a table for four, and the redundant seat is wasted. Obviously, the restaurant wants to maximise the number of guests, so it'll favour reserving two-person tables for one and two people, four-person tables for three and four people, and so on. </p> <p> In order to illustrate the desired behaviour, here's some extra test cases to add to the ones already in place: <table> <thead> <tr> <td>Tables</td> <td>Existing reservations</td> <td>Candidate reservation</td> <td>Expected outcome</td> </tr> </thead> <tbody> <tr> <td> Two tables for two<br> Two tables for four </td> <td><em>none</em></td> <td>Quantity: 4, Date: 2024-06-07</td> <td>Accepted</td> </tr> <tr> <td> Two tables for two<br> Two tables for four </td> <td><em>none</em></td> <td>Quantity: 5, Date: 2024-06-07</td> <td>Rejected</td> </tr> <tr> <td> Two tables for two<br> One table for four </td> <td>Quantity: 2, Date: 2024-06-07</td> <td>Quantity: 4, Date: 2024-06-07</td> <td>Accepted</td> </tr> <tr> <td> Two tables for two<br> One table for four </td> <td>Quantity: 3, Date: 2024-06-07</td> <td>Quantity: 4, Date: 2024-06-07</td> <td>Rejected</td> </tr> </tbody> </table> </p> <p> Again, you should consider adding more test cases if you're unit-testing the kata. </p> <h3 id="e008d27660b54882816e2e496ee89709"> Second seatings <a href="#e008d27660b54882816e2e496ee89709" title="permalink">#</a> </h3> <p> Some restaurants (even some of those on the <em>World's 50 best</em> list) have a second seating. As a diner, you have a limited time (e.g. 2&frac12; hours) to complete your meal. After that, other guests get your table. </p> <p> This implies that you must now consider the time of day of reservations. You should also be able to use an arbitrary (positive) seating duration. All previous rules should still apply. New test cases include: <table> <thead> <tr> <td>Seating duration</td> <td>Tables</td> <td>Existing reservations</td> <td>Candidate reservation</td> <td>Expected outcome</td> </tr> </thead> <tbody> <tr> <td>2 hours</td> <td> Two tables for two<br> One table for four </td> <td>Quantity: 4, Date: 2023-10-22, Time: 18:00</td> <td>Quantity: 3, Date: 2023-10-22, Time: 20:00</td> <td>Accepted</td> </tr> <tr> <td>2&frac12; hours</td> <td> One table for two<br> Two tables for four </td> <td> Quantity: 2, Date: 2023-10-22, Time: 18:00<br> Quantity: 1, Date: 2023-10-22, Time: 18:15<br> Quantity: 2, Date: 2023-10-22, Time: 17:45 </td> <td>Quantity: 3, Date: 2023-10-22, Time: 20:00</td> <td>Rejected</td> </tr> <tr> <td>2&frac12; hours</td> <td> One table for two<br> Two tables for four </td> <td> Quantity: 2, Date: 2023-10-22, Time: 18:00<br> Quantity: 2, Date: 2023-10-22, Time: 17:45 </td> <td>Quantity: 3, Date: 2023-10-22, Time: 20:00</td> <td>Accepted</td> </tr> <tr> <td>2&frac12; hours</td> <td> One table for two<br> Two tables for four </td> <td> Quantity: 2, Date: 2023-10-22, Time: 18:00<br> Quantity: 1, Date: 2023-10-22, Time: 18:15<br> Quantity: 2, Date: 2023-10-22, Time: 17:45 </td> <td>Quantity: 3, Date: 2023-10-22, Time: 20:15</td> <td>Accepted</td> </tr> </tbody> </table> </p> <p> If you make the seating duration short enough, you may even make room for a third seating, and so on. </p> <h3 id="cec332ae8ea444de929c2f5ca5603e90"> Alternative table configurations <a href="#cec332ae8ea444de929c2f5ca5603e90" title="permalink">#</a> </h3> <p> If tables are rectangular, the restaurant has the option to combine several smaller tables into one larger. Consider a typical restaurant layout like this: </p> <p> <img src="/content/binary/restaurant-configuration-with-three-individual-two-person-tables.png" alt="A map of a restaurant including three adjacent two-person tables."> </p> <p> There's a round four-person table, as well as a few small tables that can't easily be pushed together. There's also three (orange) two-person tables where one guest sits against the wall, and the other diner faces him or her. These can be used as shown above, but the restaurant can also push two of these tables together to accommodate four people: </p> <p> <img src="/content/binary/restaurant-configuration-with-two-two-person-tables-combined.png" alt="A map of a restaurant where two of the three adjacent two-person tables have been pushed together."> </p> <p> This still leaves one of the adjacent two-person tables as an individual table, but the restaurant can also push all three tables together to accommodate six people: </p> <p> <img src="/content/binary/restaurant-configuration-with-all-two-person-tables-combined.png" alt="A map of a restaurant where all three adjacent two-person tables have been pushed together."> </p> <p> Implement decision logic that allows for alternative table configurations. Remember to take seating durations into account. Consider both the configuration illustrated, as well as other configurations. Note that in the above configuration, not all two-person tables can be combined. </p> <h3 id="fa8d74d3a39f462ca23818f79eefb7a8"> More domain logic <a href="#fa8d74d3a39f462ca23818f79eefb7a8" title="permalink">#</a> </h3> <p> You can, if you will, invent extra rules. For example, restaurants have opening hours. A restaurant that opens at 18:00 and closes at 0:00 will not accept reservations for 13:30, regardless of table configuration, existing reservations, seating duration, and so on. </p> <p> Building on that idea, some restaurants have different opening hours on various weekdays. Some are closed Mondays, serve dinner only Tuesday to Friday, but are then open for both lunch and dinner in the weekend. </p> <p> Going in that direction, however, opens a can of worms. Perhaps the restaurant is closed on public holidays. Or perhaps it's explicitly open on public holidays, to cater for an audience that may not otherwise dine out. <a href="/2017/04/24/simple-holidays">But implementing a holiday calender is far from as simple as it sounds</a>. That's the reason I left such rules out of the above specifications of the kata. </p> <p> Another idea that you may consider is to combine communal bar seating with more traditional tables. <a href="https://thecloveclub.com">The Clove Club</a> is an example of restaurant that does it that way. </p> <h3 id="66b7a3d1b3e5453ca343494ccd0bc51a"> Summary <a href="#66b7a3d1b3e5453ca343494ccd0bc51a" title="permalink">#</a> </h3> <p> This is a programming kata description. Implement the decision logic of a ma&icirc;tre d': <em>Can the restaurant accept a given reservation?</em> </p> <p> After some time has gone by, I'll post at least one of my own attempts. You're welcome to <a href="https://github.com/ploeh/ploeh.github.com#comments">leave a comment</a> if you do the kata and wish to share your results. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Algebraic data types aren't numbers on steroids https://blog.ploeh.dk/2020/01/20/algebraic-data-types-arent-numbers-on-steroids 2020-01-20T07:39:00+00:00 Mark Seemann <div id="post"> <p> <em>A common red herring in the type debate.</em> </p> <p> I regularly get involved in debates about static versus dynamic typing. This post isn't an attempt to persuade anyone that static types are better. One of the reasons that I so often find myself debating this topic is that it intrigues me. I get the impression that most of the software luminaries that I admire (e.g. <a href="https://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a>, <a href="https://en.wikipedia.org/wiki/Robert_C._Martin">Robert C. Martin</a>, <a href="https://www.r7krecon.com/michael-feathers-bio">Michael Feathers</a>) seem to favour dynamically typed languages. What is it that smart people have figured out that I haven't? </p> <p> The debate continues, and this article isn't going to stop it. It may, perhaps, put one misconception to rest. There are still good arguments on either side. It's not my goal to dispute any of the good arguments. It's my goal to counter a common bad argument. </p> <h3 id="41cff26ef5fe4a56943d34da2e7ad657"> Misconception: static typing as numbers on steroids <a href="#41cff26ef5fe4a56943d34da2e7ad657" title="permalink">#</a> </h3> <p> I get the impression that many people think about static types as something that has to do with strings and numbers - particularly numbers. Introductions to programming languages often introduce strings first. That's natural, since the most common first example is <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">Hello, world!</a>. After that usually follows an introduction to basic arithmetic, and that often includes an explanation about types of numbers - at least the distinction between integers and floating-point numbers. At the time I'm writing this, <a href="https://docs.microsoft.com/dotnet/csharp/tutorials/">the online C# tutorial</a> is a typical example of this. <a href="http://bit.ly/real-world-haskell">Real World Haskell</a> takes the same approach to introducing types. </p> <p> It's a natural enough way to introduce static types, but it seems to leave some learners with the impression that static types are mostly useful to prevent them from calling a method with a floating-point number when an integer was expected. That's the vibe I'm getting from <a href="https://blog.cleancoder.com/uncle-bob/2017/01/13/TypesAndTests.html">this article by Robert C. Martin</a>. </p> <p> When presented with the notion of a 'stronger' type system, people with that mindset seem to extrapolate what they already know about static types. </p> <p> <img src="/content/binary/extrapolation-of-static-primitive-types.png" alt="Three boxes, from left to right: no types, static primitive types, and static primitive types on steroids."> </p> <p> If you mostly think of static types as a way to distinguish between various primitive types (such as strings and a zoo of number types), I can't blame you for extrapolating that notion. This seems to happen often, and it leads to a lot of frustration. </p> <p> People who want 'stronger numbers' try to: <ul> <li>Model natural numbers; i.e. to define a type that represents only positive integers</li> <li>Model positive numbers; i.e. rational or real numbers greater than zero</li> <li>Model non-negative numbers</li> <li>Model numbers in a particular range; e.g. between 0 and 100</li> <li><a href="https://ren.zone/articles/safe-money">Model money in different currencies</a></li> </ul> Particularly, people run into all sorts of trouble when they try to accomplish such goals with <a href="https://www.haskell.org">Haskell</a>. They've heard that Haskell has a powerful type system, and now they want to do those things. </p> <p> Haskell does have a powerful type system, but it's a type system that builds on the concept of <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>. (If you want to escape the jargon of that Wikipedia article, I recommend <a href="http://tomasp.net">Tomas Petricek</a>'s lucid and straightforward explanation <a href="http://tomasp.net/blog/types-and-math.aspx">Power of mathematics: Reasoning about functional types</a>.) </p> <p> There are type systems that enable you to take the notion of numbers to the next level. This is called either <a href="https://en.wikipedia.org/wiki/Refinement_type">refinement types</a> or <a href="https://en.wikipedia.org/wiki/Dependent_type">dependent types</a>, contingent on what exactly it is that you want to do. Haskell doesn't support that out of the box. The most prominent dependently-typed programming language is probably <a href="https://www.idris-lang.org">Idris</a>, which is still a research language. As far as I know, there's no 'production strength' languages that support refinement or dependent types, unless you consider <a href="https://en.wikipedia.org/wiki/Liquid_Haskell">Liquid Haskell</a> to fit that description. Honestly, all this is at the fringe of my expertise. </p> <p> I'll return to an example of this kind of frustration later, and also suggest a simple alternative. Before I do that, though, I'd like to outline what it is proponents of 'strong' type systems mean. </p> <h3 id="76b0c9b8e461448796d44443351619f1"> Make illegal states unrepresentable <a href="#76b0c9b8e461448796d44443351619f1" title="permalink">#</a> </h3> <p> Languages like Haskell, <a href="https://ocaml.org">OCaml</a>, and <a href="https://fsharp.org">F#</a> have algebraic type systems. They still distinguish between various primitive types, but they take the notion of static types in a completely different direction. They introduce a new dimension of static type safety, so to speak. </p> <p> <img src="/content/binary/algebraic-data-types-as-another-dimension.png" alt="Three boxes. At the bottom left: no types. To the right of that: static primitive types. To the top of the no-types box: algebraic data types"> </p> <p> It's a completely different way to think about static types. The advantage isn't that it prevents you from using a floating point where an integer was required. The advantage is that it enables you to model domain logic in a way that flushes out all sorts of edge cases at compile time. </p> <p> I've previously <a href="/2016/11/28/easy-domain-modelling-with-types">described a real-world example of domain modelling with types</a>, so I'm not going to repeat that effort here. Most business processes can be described as a progression of states. With algebraic data types, not only can you model what a valid state looks like - you can also model the state machine in such a way that you can't represent illegal states. </p> <p> This notion is eloquently captured by the aphorism: <blockquote> <p> Make illegal states unrepresentable. </p> <footer><cite><a href="https://youtu.be/-J8YyfrSwTk">Yaron Minsky</a></cite></footer> </blockquote> This is solving an entirely different type of problem than distinguishing between 32-bit and 64-bit integers. Writing even moderately complex code involves dealing with many edge cases. In most mainstream languages (including C# and Java), it's your responsibility to ensure that you've handled all edge cases. It's easy to overlook or forget a few of those. With algebraic data types, the compiler keeps track of that for you. That's a tremendous boon because it enables you to forget about those technical details and instead focus on adding value. </p> <p> Scott Wlaschin wrote <a href="https://amzn.to/2OyI51M">an entire book about domain modelling with algebraic data types</a>. That's what we talk about when we talk about stronger type systems. Not 'numbers on steroids'. </p> <h3 id="4c5ac4aa723f4f078d5cfbb13164194c"> Exhibit: summing notionals <a href="#4c5ac4aa723f4f078d5cfbb13164194c" title="permalink">#</a> </h3> <p> I consider this notion of <em>strong type systems viewed as numbers on steroids</em> a red herring. I don't blame anyone from extrapolating from what they already know. That's a natural way to try to make sense of the world. We all do it. </p> <p> I came across a recent example of this way of thinking in a great article by <a href="https://alexnixon.github.io">Alex Nixon</a> titled <a href="https://alexnixon.github.io/2020/01/14/static-types-are-dangerous.html">Static types are dangerously interesting</a>. The following is in no way meant to excoriate Alex or his article, but I think it's a great example of how easily one can be lead astray by thinking that strong type systems imply numbers on steroids. </p> <p> You should read the article. It's well-written and uses more sophisticated features of Haskell than I'm comfortable with. The example problem it tries to solve is basically this: Given a set of trades, calculate the <em>total notional in each currency</em>. Consider a collection of trades: </p> <p> <pre>Quantity, Ticker, Price, Currency 100, VOD.L, 1, GBP 200, VOD.L, 2, GBP 300, AAPL.O, 3, USD 50, 4151.T, 5, JPY</pre> </p> <p> I'll let Alex explain what it is that he wants to do: <blockquote> <p> "I want to write a function which calculates the <em>total notional in each currency</em>. The word <em>notional</em> is a fancy way of saying <code>price * quantity</code>. Think of it as "value of the thing that changed hands". </p> <p> "For illustration, the function signature might look something like this: </p> <p> "<code>sumNotionals :: [Trade] -> Map Currency Rational</code> </p> <p> "In English, it’s a function that takes a list of trades and returns a map from currency to quantity." </p> </blockquote> If given the above trades, the output would be: </p> <p> <pre>Currency, Notional GBP, 500 USD, 900 JPY, 250</pre> </p> <p> The article proceeds to explore how to model this problem with Haskell's strong type system. Alex wants to be able to calculate with money, but on the other hand, he wants the type system to prevent accidents. You can't add <em>100 GBP</em> to <em>300 USD</em>. The type system should prevent that. </p> <p> Early on, he defines a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> to model currencies: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Currency &nbsp;&nbsp;=&nbsp;USD &nbsp;&nbsp;|&nbsp;GBP &nbsp;&nbsp;|&nbsp;JPY &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Ord</span>,&nbsp;<span style="color:#2b91af;">Show</span>)</pre> </p> <p> Things basically go downhill from there. Read the article; it's good. </p> <h3 id="f04845b0c38f4924a5d34d1d5802912e"> Sum types should distinguish behaviour, not values <a href="#f04845b0c38f4924a5d34d1d5802912e" title="permalink">#</a> </h3> <p> I doubt that Alex Nixon views his proposed <code>Currency</code> type as anything but a proof of concept. In a 'real' code base, you'd enumerate all the currencies you'd trade, right? </p> <p> I wouldn't. This is the red herring in action. Algebraic data types are useful because they enable us to distinguish between cases that we should treat differently, by writing specific code that deals with each case. That's not the case with a currency. You add US dollars together in exactly the same way that you add euros together. The currency doesn't change the behaviour of that operation. </p> <p> But we can't just enable addition of arbitrary monetary values, right? After all, we shouldn't be able to add <em>20 USD</em> and <em>300 DKK</em>. At least, without an exchange rate, that shouldn't compile. </p> <p> Let's imagine, for the sake of argument, that we encode all the currencies we trade into a type. What happens if our traders decide to trade a currency that they haven't previously traded? What if a country decides to <a href="https://en.wikipedia.org/wiki/Redenomination">reset their currency</a>? What if a country splits into two countries, each with <a href="https://en.wikipedia.org/wiki/South_Sudanese_pound">their own currency</a>? </p> <p> If you model currency as a type, you'd have to edit and recompile your code every time such an external event occurs. I don't think this is a good use of a type system. </p> <p> Types should, I think, help us programmers identify the parts of our code bases where we need to treat various cases differently. They shouldn't be used to distinguish run-time values. Types provide value at compile time; run-time values only exist at run time. To paraphrase Kent Beck, <em>keep things together that change together; keep things apart that don't</em>. </p> <p> I'd model currency as a run-time value, because the behaviour of money doesn't vary with the currency. </p> <h3 id="cfa838f3fad84259b9629f24f09f37eb"> Boring Haskell <a href="#cfa838f3fad84259b9629f24f09f37eb" title="permalink">#</a> </h3> <p> How would I calculate the notionals, then? With <a href="https://www.snoyman.com/blog/2019/11/boring-haskell-manifesto">boring Haskell</a>. Really boring Haskell, in fact. I'm only going to need two imports and no language pragmas: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Trades&nbsp;<span style="color:blue;">where</span> <span style="color:blue;">import</span>&nbsp;Data.List <span style="color:blue;">import</span>&nbsp;Data.Map.Strict&nbsp;(<span style="color:blue;">Map</span>) <span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.Map.Strict&nbsp;<span style="color:blue;">as</span>&nbsp;Map</pre> </p> <p> Which types do I need? For this particular purpose, I think I'll just stick with a single <code>Trade</code> type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Trade&nbsp;=&nbsp;Trade&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;tradeQuantity&nbsp;::&nbsp;Int &nbsp;&nbsp;,&nbsp;tradeTicker&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;tradePrice&nbsp;::&nbsp;Rational &nbsp;&nbsp;,&nbsp;tradeCurrency&nbsp;::&nbsp;String&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>)</pre> </p> <p> Shouldn't I introduce a <code>Money</code> type? I could, but I don't have to. As <a href="https://lexi-lambda.github.io">Alexis King</a> so clearly explains, <a href="https://lexi-lambda.github.io/blog/2020/01/19/no-dynamic-type-systems-are-not-inherently-more-open">you don't have to model more than you need to do the job</a>. </p> <p> By not introducing a <code>Money</code> type and making it an instance of various type classes, I still prevent client code from adding things together that shouldn't be added together. You can't add <code>Trade</code> values together because <code>Trade</code> isn't a <code>Num</code> instance. </p> <p> How do we calculate the notionals, then? It's easy; it's a one-liner: </p> <p> <pre><span style="color:#2b91af;">sumNotionals</span>&nbsp;::&nbsp;<span style="color:blue;">Foldable</span>&nbsp;t&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;t&nbsp;<span style="color:blue;">Trade</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Map</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">Rational</span> sumNotionals&nbsp;=&nbsp;foldl&#39;&nbsp;(\m&nbsp;t&nbsp;-&gt;&nbsp;Map.insertWith&nbsp;<span style="color:#2b91af;">(+)</span>&nbsp;(key&nbsp;t)&nbsp;(value&nbsp;t)&nbsp;m)&nbsp;Map.empty &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;key&nbsp;&nbsp;&nbsp;(Trade&nbsp;_&nbsp;_&nbsp;_&nbsp;currency)&nbsp;=&nbsp;currency &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;(Trade&nbsp;quantity&nbsp;_&nbsp;price&nbsp;_)&nbsp;=&nbsp;<span style="color:blue;">toRational</span>&nbsp;quantity&nbsp;*&nbsp;price</pre> </p> <p> Okay, that looks more like four lines of code, but the first is an optional type declaration, so it doesn't count. The <code>key</code> and <code>value</code> functions could be inlined to make the function a single (wide) line of code, but I made them two named functions in order to make the code more readable. </p> <p> It gets the job done: </p> <p> <pre>*Trades&gt; sumNotionals trades fromList [("GBP",500 % 1),("JPY",250 % 1),("USD",900 % 1)]</pre> </p> <p> While this code addresses this particular problem, you probably consider it cheating because I've failed to address a wider concern. How does one model money in several currencies? I've <a href="/2017/10/16/money-monoid">previously covered that, including a simple Haskell example</a>, but in general, I consider it more productive to have a problem and <em>then</em> go looking for a solution, rather than inventing a solution and go looking for a problem. </p> <h3 id="8d472a61051e4ddfaf2698c8d215d658"> Summary <a href="#8d472a61051e4ddfaf2698c8d215d658" title="permalink">#</a> </h3> <p> When people enter into a debate, they use the knowledge they have. This is also the case in the debate about static versus dynamic types. Most programmers have experience with statically typed languages like C# or Java. It's natural to argue from what you know, and extrapolate from that. </p> <p> I think that when confronted with a phrase like <em>a more powerful type system</em>, many people extrapolate and think that they know what that means. They think that it means statically typed numbers on steroids. That's a red herring. </p> <p> That's usually not what we mean when we talk about <em>more powerful type systems</em>. We talk about algebraic data types, which make illegal states unrepresentable. Judged by the debates I've participated in, you can't <em>extrapolate</em> from mainstream type systems to algebraic data types. If you haven't tried programming with both sum and <a href="https://en.wikipedia.org/wiki/Product_type">product types</a>, you aren't going to <a href="/ref/stranger-in-a-strange-land">grok</a> what we mean when we talk about <em>strong type systems</em>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="61115b6622294923b7440cb1c4e32f84"> <div class="comment-author"><a href="https://github.com/Jankowski-J">Jakub Jankowski</a> <a href="#61115b6622294923b7440cb1c4e32f84">#</a></div> <div class="comment-content"> <p> "but in general, I consider it more productive to have a problem and <em>then</em> go looking for a solution, rather than inventing a solution and go looking for a problem." </p> <p> This really resonates with me. I've been observing this in my current team and the tendency to "lookout" for the solutions to problems not yet present, just for the sake of "making it a robust solution" so to say. </p> <p> I really like the properties of the Haskell solution. It handles all the currencies (no matter how many of them come in the dataset) without explicitly specifying them. And you can't accidentally add two different currencies together. The last part would be pretty verbose to implement in C#. </p> </div> <div class="comment-date">2020-01-20 20:54 UTC</div> </div> <div class="comment" id="a496e710c3ea4391a9de433da3e6d54d"> <div class="comment-author"><a href="https://github.com/drewjcooper">Andrew Cooper</a> <a href="#a496e710c3ea4391a9de433da3e6d54d">#</a></div> <div class="comment-content"> <p> I'm not sure the above is a good example of what you're trying to say about algebraic data types. The problem can be solve identically (at least semantically) in C#. Granted, the definition of the <code>Trade</code> type would be way more verbose, but once you have that, the <code>SumNotionals</code> method is basically the same as you code, albeit with different syntax: </p> <p> <pre>Dictionary&lt;string,&nbsp;int&gt;&nbsp;SumNotionals(IEnumerable&lt;Trade&gt;&nbsp;trades) { &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;trades &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GroupBy(t&nbsp;=&gt;&nbsp;t.Currency,&nbsp;t&nbsp;=&gt&nbsp;t.Price&nbsp;*&nbsp;t.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToDictionary(g&nbsp;=&gt;&nbsp;g.Key,&nbsp;g&nbsp;=&gt&nbsp;g.Sum()); }</pre> </p> <p> Am I missing something? </p> </div> <div class="comment-date">2020-01-20 22:30 UTC</div> </div> <div class="comment" id="bd48b3267e014ccc9b377e983369ddca"> <div class="comment-author"><a href="https://github.com/Jankowski-J">Jakub Jankowski</a> <a href="#bd48b3267e014ccc9b377e983369ddca">#</a></div> <div class="comment-content"> <p> You are right Andrew. The LINQ query indeed has the same properites as the Haskell function. </p> <p> I'm not sure what I was thinking yesterday, but I think I subconsciously "wanted" C# to be less robust. </p> </div> <div class="comment-date">2020-01-21 18:04 UTC</div> </div> <div class="comment" id="c9069e6554934325a86a27921cff7ac3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c9069e6554934325a86a27921cff7ac3">#</a></div> <div class="comment-content"> <p> Andrew, thank you for writing. I didn't intend to say much about algebraic data types in this article. It wasn't the topic I had in mind. It can be difficult to communicate any but the simplest ideas, so it's possible that I didn't state my intention well enough. If so, the fault is mine. I've <a href="/2016/11/28/easy-domain-modelling-with-types">tried to demonstrate the power of algebraic data types before</a>, so I didn't want to repeat the effort, since my agenda was another. That's why I linked to that other article. </p> <p> The reason I discussed Alex Nixon's blog post was that it was the article that originally inspired me to write this article. I always try to include an example so that the reader gets to see the connection between the general concept and specifics. </p> <p> I could have discussed Alex' article solely on its merits of showcasing failed attempts to model a 'stronger number'. That would, however, have left the reader without a resolution. I found that a bad way to structure my text. Readers would be left with questions. <em>Okay Mark, that's all fine, but then how would you solve the problem?</em> </p> <p> So I decided to include a simple solution in an attempt to <a href="https://en.wikipedia.org/wiki/Gordian_Knot">cut the Gordian know</a>, so to speak. </p> </div> <div class="comment-date">2020-01-22 14:11 UTC</div> </div> <div class="comment" id="d20281f1114f44738c29f4298c52a728"> <div class="comment-author"><a href="https://github.com/drewjcooper">Andrew Cooper</a> <a href="#d20281f1114f44738c29f4298c52a728">#</a></div> <div class="comment-content"> <p> Mark, thanks for your response. It does indeed clear up my confusion. In my eagerness to learn more about algrebraic data types I read the second half of your post the wrong way. Thanks for clearing it up. </p> </div> <div class="comment-date">2020-01-22 21:30 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. On doing katas https://blog.ploeh.dk/2020/01/13/on-doing-katas 2020-01-13T06:23:00+00:00 Mark Seemann <div id="post"> <p> <em>Approach programming katas differently than martial arts katas.</em> </p> <p> Would you like to become a better programmer? Then practice. It's no different from becoming a better musician, a better sports(wo)man, a better cook, a better artist, etcetera. </p> <p> How do you practice programming? </p> <p> There's many ways. Doing <a href="https://en.wikipedia.org/wiki/Kata_(programming)">programming katas</a> is one way. </p> <h3 id="16e8612d24b14930a4d7cc02ebad6fd6"> Variation, not repetition <a href="#16e8612d24b14930a4d7cc02ebad6fd6" title="permalink">#</a> </h3> <p> When I talk to other programmers about katas, I often get the impression that people fail to extract value from the exercises. You can find catalogues of exercises on the internet, but there's a dearth of articles that discuss <em>how</em> to do katas. </p> <p> Part of the problem is, I think, that <a href="https://en.wikipedia.org/wiki/Kata">the term comes from martial arts practice</a>. In martial arts, one repeats the same movements over and over again in order to build up <a href="https://en.wikipedia.org/wiki/Muscle_memory">muscle memory</a>. Repetition produces improvements. </p> <p> Some people translate that concept literally. They try to do programming katas by doing the <em>same exercise</em> again and again, with no variation. After a few days or weeks, they stop because they can't see the point. </p> <p> That's no wonder. Neither can I. </p> <p> Programming and software design is mostly an intellectual (and perhaps artistic) endeavour. Unless you can't <a href="https://en.wikipedia.org/wiki/Touch_typing">touch type</a>, there's little need to build up muscle memory. You train your brain unlike you train your muscles. Repetition numbs the brain. Variation stimulates it. </p> <h3 id="5e0804109e5c40cb80f0c3c9274afb30"> Suggested variations <a href="#5e0804109e5c40cb80f0c3c9274afb30" title="permalink">#</a> </h3> <p> I find that doing a kata is a great opportunity to explore alternatives. A kata is usually a limited exercise, which means that you can do it multiple times and compare outcomes. </p> <p> You can find various kata catalogues on the internet. One of my favourites is the <a href="http://bit.ly/codekatas">Coding Dojo</a>. Among the katas there, I particularly like the <a href="http://codingdojo.org/kata/Tennis">Tennis kata</a>. I'll use that as an example to describe how I often approach a kata. </p> <p> The first time I encounter a kata I've never done before, I do it with as little fuss as possible. I use the programming language I'm most comfortable with, and don't attempt any stunts. I no longer remember when I first encountered the Tennis kata, but it's many years ago, and C# was my preferred language. I'd do the Tennis kata in C#, then, just to get acquainted with the problem. </p> <p> Most good katas contain small surprises. They may sound simpler than they actually turn out to be. On the other hand, they're typically not overwhelmingly difficult. It pays to overcome the surprise the kata may hold without getting bogged down by trying some feat. The Tennis kata, for example, sounds easy, but most people stumble on the rules associated with <em>deuce</em> and <em>advantage</em>. How to model the API? How do you implement the algorithm? </p> <p> Once you're comfortable with the essence of the exercise, introduce variations. Most of the variations I use take the form of some sort of constraint. <a href="https://www.dotnetrocks.com/?show=1542">Constraints liberate</a>. <a href="/2015/04/13/less-is-more-language-features">Less is more</a>. </p> <p> Here's a list of suggestions: <ul> <li><a href="/2019/10/21/a-red-green-refactor-checklist">Follow test-driven development</a> (TDD). That's my usual modus operandi, but if you don't normally practice TDD, a kata is a great opportunity.</li> <li>Use the (<em>Gollum style</em>) <a href="/2019/10/07/devils-advocate">Devil's Advocate</a> technique with TDD.</li> <li>Follow the <a href="https://blog.cleancoder.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html">Transformation Priority Premise</a>.</li> <li>Do TDD without mocks.</li> <li>Do TDD with mocks.</li> <li>Use the <a href="http://www.natpryce.com/articles/000714.html">Test Data Builder design pattern</a>.</li> <li>Try <a href="/property-based-testing-intro">property-based testing</a>. I've <a href="/2016/02/10/types-properties-software">done that with the Tennis</a> kata multiple times.</li> <li>Put your mouse away.</li> <ins datetime="2020-02-20T08:38Z"><li>Hide the file tree in your editor or IDE. In Visual Studio, this is called the <em>Solution Explorer</em>, in Visual Studio Code it's just <em>Explorer</em>. Navigate the code by other means.</li></ins> <li>Use another editor or IDE.</li> <li>Use another programming language. A kata is a great way to practice a new language. When you're learning a new language, you're often fighting with unfamiliar syntax, which is the reason I recommend that you <em>first</em> do the kata in a language with which you're familiar.</li> <li>Use only immutable data structures. This is a good first step towards learning functional programming.</li> <li>Keep the <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of all methods at <em>1</em>. I <a href="/2011/05/16/TennisKatawithimmutabletypesandacyclomaticcomplexityof1">once did that with the Tennis kata</a>.</li> <li>Use an unfamiliar API. If you normally use <a href="https://nunit.org">NUnit</a> then try <a href="https://xunit.net">xUnit.net</a> instead. Use a new Test Double library. Use a different assertion library. I once did the Tennis kata in <a href="https://www.haskell.org">Haskell</a> using the <a href="http://hackage.haskell.org/package/lens">lens</a> library because I wanted to hone those skills. I've also done the <em>Mark IV coffee maker</em> exercise from <a href="http://amzn.to/19W4JHk">APPP</a> with <a href="http://reactivex.io">Reactive Extensions</a>.</li> <li>Employ a design pattern you'd like to understand better. I've had particular success with the <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor design pattern</a>.</li> <li>Refactor an existing kata solution to another design.</li> <li>Refactor another programmer's kata solution.</li> <li><a href="https://en.wikipedia.org/wiki/Pair_programming">Pair-program</a> the kata.</li> <li>Use the <a href="http://wiki.c2.com/?PairProgrammingPingPongPattern">Ping Pong pattern</a> when pair programming.</li> <li><a href="https://en.wikipedia.org/wiki/Mob_programming">Mob-program</a> it.</li> </ul> You'll probably come up with your own list of variations. </p> <p> What I like about katas is that they're small enough that you can do the same exercise multiple times, but with different designs. This makes it easy to learn new ways of doing things, because you can compare different approaches to the same problem. </p> <h3 id="1faffc34ac644021b87c7f0b0691eeef"> Conclusion <a href="#1faffc34ac644021b87c7f0b0691eeef" title="permalink">#</a> </h3> <p> The way that the idea of a programming kata <a href="http://www.butunclebob.com/ArticleS.UncleBob.TheProgrammingDojo">was originally introduced</a> is a bit unfortunate. On one hand, the metaphor may have helped adoption because martial arts are cool, and Japanese is a beautiful language. On the other hand, the underlying message is one of repetition, which is hardly helpful when it comes to exercising the brain. </p> <p> Repetition dulls the brain, while variation stimulates it. Katas are great because they're short exercises, but you have to deliberately introduce diversity to make them work for you. You're not building muscle memory, you're forming new neural pathways. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="?"> <div class="comment-author">Johannes Schmitt <a href="#?">#</a></div> <div class="comment-content"> <p> Regarding kata variations, I'd like mention Jeff Bay's <i>Object Calisthenics</i> (by Jeff Bay). One could use all rules at once or just a subset of them. </p> <p> Just briefly, this are the rules (details can be found on the web): <ul> <li>One level of indentation per method</li> <li>Don’t use the ELSE keyword</li> <li>Wrap all primitives and strings</li> <li>First class collections</li> <li>One dot per line</li> <li>Don't abbreviate</li> <li>Keep all entities small</li> <li>No classes with more than two instance variables</li> <li>No getters/setters/properties</li> </ul> </p> </div> <div class="comment-date">2020-01-14 12:42 UTC</div> </div> <div class="comment" id="cd5aa5d8944e48c59937986d9fbe464e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cd5aa5d8944e48c59937986d9fbe464e">#</a></div> <div class="comment-content"> <p> Johannes, that list is a great addition to my suggestions. Thank you. </p> </div> <div class="comment-date">2020-01-14 13:58 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The case of the unbalanced brackets https://blog.ploeh.dk/2020/01/06/the-case-of-the-unbalanced-brackets 2020-01-06T06:37:00+00:00 Mark Seemann <div id="post"> <p> <em>A code mystery.</em> </p> <p> One of my clients was kind enough to let me look at some of their legacy code. As I was struggling to understand how it worked, I encountered something that <em>looked</em> like this: </p> <p> <pre>ApplyDueAmountG89.<span style="font-weight:bold;color:#74531f;">Calculate</span>(<span style="font-weight:bold;color:#1f377f;">postState</span>.PartialMebershipsBAT.<span style="font-weight:bold;color:#74531f;">Where</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;(<span style="font-weight:bold;color:#1f377f;">d</span>.Data.Choicetype&nbsp;==&nbsp;<span style="color:#2b91af;">GarplyChoicetype</span>.AtoC&nbsp;|| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">retirablePartialMembershipNr</span>.<span style="font-weight:bold;color:#74531f;">Contains</span>(<span style="font-weight:bold;color:#1f377f;">d</span>.Data.PartialMembershipNr)).<span style="font-weight:bold;color:#74531f;">ToList</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ApplyDueAmountG89.Situation.Depreciation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ApplyDueAmountG89.RecordType.Primo);</pre> </p> <p> For the record, this isn't the actual code that my client gave me. I wouldn't post someone else's code without their permission. It is, however, a faithful imitation of the original code. What's wrong with it? </p> <p> I'll wait. </p> <h3 id="4eb9d6fa6cf04f45a4afdeb3638a9c8a"> Brackets <a href="#4eb9d6fa6cf04f45a4afdeb3638a9c8a" title="permalink">#</a> </h3> <p> Count the brackets. There's a missing closing bracket. </p> <p> Yet, the code compiles. How? </p> <p> Legacy code isn't <a href="https://cleancoders.com/video-details/humane-code-real-episode-1">humane code</a>. There's a multitude of ways in which code can be obscure. This article describes one of them. </p> <p> When brackets are nested and far apart, it's hard for the brain to parse and balance them. Yet, on closer inspection the brackets seem unbalanced. </p> <h3 id="0595d741e4ff4a139d3b26ab8fb3e75d"> Show whitespace <a href="#0595d741e4ff4a139d3b26ab8fb3e75d" title="permalink">#</a> </h3> <p> Ever since I started programming in <a href="https://fsharp.org">F#</a>, I've turned on the Visual Studio feature that shows whitespace. F# does, after all, use <a href="/2020/05/04/significant-whitespace-is-dry">significant whitespace</a> (AKA the <a href="https://en.wikipedia.org/wiki/Off-side_rule">Off-side rule</a>), and it helps to be able to detect if a tab character has slipped in among the spaces. </p> <p> Visual Studio shows whitespace with pale blue dots and arrows. When that feature is turned on (<kbd>Ctrl</kbd> + <kbd>e</kbd>, <kbd>s</kbd>), the above code example looks different: </p> <p> <pre>ApplyDueAmountG89.<span style="font-weight:bold;color:#74531f;">Calculate</span>(<span style="font-weight:bold;color:#1f377f;">postState</span>.PartialMebershipsBAT.<span style="font-weight:bold;color:#74531f;">Where</span>( <span style="color:#2b91af;">&middot;&middot;&middot;&middot;</span><span style="font-weight:bold;color:#1f377f;">d</span><span style="color:#2b91af;">&middot;</span>=&gt;<span style="color:#2b91af;">&middot;</span>(<span style="font-weight:bold;color:#1f377f;">d</span>.Data.Choicetype<span style="color:#2b91af;">&middot;</span>==<span style="color:#2b91af;">&middot;</span><span style="color:#2b91af;">GarplyChoicetype</span>.AtoC<span style="color:#2b91af;">&middot;</span>||<span style="color:#2b91af;">&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;</span> <span style="color:#2b91af;">&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;</span><span style="font-weight:bold;color:#1f377f;">retirablePartialMembershipNr</span>.<span style="font-weight:bold;color:#74531f;">Contains</span>(<span style="font-weight:bold;color:#1f377f;">d</span>.Data.PartialMembershipNr)).<span style="font-weight:bold;color:#74531f;">ToList</span>(), <span style="color:#2b91af;">&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;</span>ApplyDueAmountG89.Situation.Depreciation, <span style="color:#2b91af;">&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;&middot;</span>ApplyDueAmountG89.RecordType.Primo);</pre> </p> <p> Notice the space characters that seem to run off to the right of the <code>||</code> operator. What's at the end of those spaces? </p> <p> Yes, you guessed it: another Boolean expression, including the missing closing bracket: </p> <p> <pre><span style="font-weight:bold;color:#1f377f;">d</span>.Data.Choicetype&nbsp;==&nbsp;<span style="color:#2b91af;">GarplyChoicetype</span>.BtoC)&nbsp;&amp;&amp;</pre> </p> <p> If you delete all those redundant spaces, this is the actual code: </p> <p> <pre>ApplyDueAmountG89.<span style="font-weight:bold;color:#74531f;">Calculate</span>(<span style="font-weight:bold;color:#1f377f;">postState</span>.PartialMebershipsBAT.<span style="font-weight:bold;color:#74531f;">Where</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>&nbsp;=&gt;&nbsp;(<span style="font-weight:bold;color:#1f377f;">d</span>.Data.Choicetype&nbsp;==&nbsp;<span style="color:#2b91af;">GarplyChoicetype</span>.AtoC&nbsp;||&nbsp;<span style="font-weight:bold;color:#1f377f;">d</span>.Data.Choicetype&nbsp;==&nbsp;<span style="color:#2b91af;">GarplyChoicetype</span>.BtoC)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">retirablePartialMembershipNr</span>.<span style="font-weight:bold;color:#74531f;">Contains</span>(<span style="font-weight:bold;color:#1f377f;">d</span>.Data.PartialMembershipNr)).<span style="font-weight:bold;color:#74531f;">ToList</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ApplyDueAmountG89.Situation.Depreciation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ApplyDueAmountG89.RecordType.Primo);</pre> </p> <p> Imagine troubleshooting code like that, and not realising that there's another Boolean expression so far right that even a large screen doesn't show it. In the actual legacy code where I found this example, the extra Boolean expression started at column 209. </p> <h3 id="8a3d025a78304704864a782aa162e6e3"> Conclusion <a href="#8a3d025a78304704864a782aa162e6e3" title="permalink">#</a> </h3> <p> Hiding significant code so far right that it's effectively invisible seems positively <em>evil</em>, but I don't think anyone did it deliberately. Rather, my guess is that someone performed a search-and-replace through the code base, and that this automatic change somehow removed a newline character. </p> <p> In any case, keeping an eye on the line width of code could prevent something like this from happening. <a href="/2019/11/04/the-80-24-rule">Stay within 80 characters</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Semigroup resonance FizzBuzz https://blog.ploeh.dk/2019/12/30/semigroup-resonance-fizzbuzz 2019-12-30T10:44:00+00:00 Mark Seemann <div id="post"> <p> <em>An alternative solution to the FizzBuzz kata.</em> </p> <p> A common solution to the <a href="http://codingdojo.org/kata/FizzBuzz/">FizzBuzz kata</a> is to write a loop from 1 to 100 and perform a modulo check for each number. Functional programming languages like <a href="https://www.haskell.org">Haskell</a> don't have loops, so instead you'd typically solve the kata like this: </p> <p> <pre><span style="color:#2b91af;">isAMultipleOf</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> isAMultipleOf&nbsp;i&nbsp;multiplier&nbsp;=&nbsp;i&nbsp;`mod`&nbsp;multiplier&nbsp;==&nbsp;0 <span style="color:#2b91af;">convert</span>&nbsp;::&nbsp;(<span style="color:blue;">Integral</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> convert&nbsp;i&nbsp;|&nbsp;i&nbsp;`isAMultipleOf`&nbsp;3&nbsp;&amp;&amp;&nbsp;i&nbsp;`isAMultipleOf`&nbsp;5&nbsp;=&nbsp;<span style="color:#a31515;">&quot;FizzBuzz&quot;</span> convert&nbsp;i&nbsp;|&nbsp;i&nbsp;`isAMultipleOf`&nbsp;3&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span> convert&nbsp;i&nbsp;|&nbsp;i&nbsp;`isAMultipleOf`&nbsp;5&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span> convert&nbsp;i&nbsp;=&nbsp;<span style="color:blue;">show</span>&nbsp;i <span style="color:#2b91af;">main</span>&nbsp;::&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;=&nbsp;<span style="color:blue;">mapM_</span>&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;$&nbsp;convert&nbsp;&lt;$&gt;&nbsp;[1..100]</pre> </p> <p> There's more than one way to skin this cat. In this article, I'll demonstrate one based on <code>Semigroup</code> resonance. </p> <h3 id="66370c86e5c74b44a18bb9827c7dc34e"> Fizz stream <a href="#66370c86e5c74b44a18bb9827c7dc34e" title="permalink">#</a> </h3> <p> The fundamental idea is to use infinite streams that repeat at different intervals. That idea isn't mine, but I've never seen it done without resorting to some sort of Boolean conditional or pattern matching. </p> <p> You start with a finite sequence of values that represent the pulse of <em>Fizz</em> values: </p> <p> <pre>[Nothing,&nbsp;Nothing,&nbsp;Just&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span>]</pre> </p> <p> If you repeat that sequence indefinitely, you now have a pulse of <em>Fizz</em> values: </p> <p> <pre><span style="color:#2b91af;">fizzes</span>&nbsp;::&nbsp;[<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:#2b91af;">String</span>] fizzes&nbsp;=&nbsp;<span style="color:blue;">cycle</span>&nbsp;[Nothing,&nbsp;Nothing,&nbsp;Just&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span>]</pre> </p> <p> This stream of values is one-based, since the first two entries are <code>Nothing</code>, and only every third is <code>Just "Fizz"</code>: </p> <p> <pre>*FizzBuzz&gt; take 9 fizzes [Nothing, Nothing, Just "Fizz", Nothing, Nothing, Just "Fizz", Nothing, Nothing, Just "Fizz"]</pre> </p> <p> If you're wondering why I chose a stream of <code>Maybe String</code> instead of just a stream of <code>String</code> values, I'll explain that now. </p> <h3 id="000531d5739e4d3bbaeff702e052ca6c"> Buzz stream <a href="#000531d5739e4d3bbaeff702e052ca6c" title="permalink">#</a> </h3> <p> You can define an equivalent infinite stream of <em>Buzz</em> values: </p> <p> <pre><span style="color:#2b91af;">buzzes</span>&nbsp;::&nbsp;[<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:#2b91af;">String</span>] buzzes&nbsp;=&nbsp;<span style="color:blue;">cycle</span>&nbsp;[Nothing,&nbsp;Nothing,&nbsp;Nothing,&nbsp;Nothing,&nbsp;Just&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span>]</pre> </p> <p> The idea is the same, but the rhythm is different: </p> <p> <pre>*FizzBuzz&gt; take 10 buzzes [Nothing, Nothing, Nothing, Nothing, Just "Buzz", Nothing, Nothing, Nothing, Nothing, Just "Buzz"]</pre> </p> <p> Why not simply generate a stream of <code>String</code> values, like the following? </p> <p> <pre>*FizzBuzz&gt; take 10 $ cycle ["", "", "", "", "Buzz"] ["", "", "", "", "Buzz", "", "", "", "", "Buzz"]</pre> </p> <p> At first glance this looks simpler, but it makes it harder to merge the stream of <em>Fizz</em> and <em>Buzz</em> values with actual numbers. Distinguishing between <code>Just</code> and <code>Nothing</code> values enables you to use the <em>Maybe catamorphism</em>. </p> <h3 id="f95fb4415768473ca8fdc576e227882c"> Resonance <a href="#f95fb4415768473ca8fdc576e227882c" title="permalink">#</a> </h3> <p> You can now <em>zip</em> the <code>fizzes</code> with the <code>buzzes</code>: </p> <p> <pre><span style="color:#2b91af;">fizzBuzzes</span>&nbsp;::&nbsp;[<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:#2b91af;">String</span>] fizzBuzzes&nbsp;=&nbsp;<span style="color:blue;">zipWith</span>&nbsp;<span style="color:#2b91af;">(&lt;&gt;)</span>&nbsp;fizzes&nbsp;buzzes</pre> </p> <p> You combine the values by monoidal composition. Any <code>Maybe</code> over a <code>Semigroup</code> itself gives rise to a <code>Monoid</code>, and since <code>String</code> forms a <code>Monoid</code> (and therefore also a <code>Semigroup</code>) over concatenation, you can <em>zip</em> the two streams using the <code>&lt;&gt;</code> operator. </p> <p> <pre>*FizzBuzz&gt; take 20 fizzBuzzes [Nothing, Nothing, Just "Fizz", Nothing, Just "Buzz", Just "Fizz", Nothing, Nothing, Just "Fizz", Just "Buzz", Nothing, Just "Fizz", Nothing, Nothing, Just "FizzBuzz", Nothing, Nothing, Just "Fizz", Nothing, Just "Buzz"]</pre> </p> <p> Notice how the stream of <code>fizzes</code> enters into a resonance pattern with the stream of <code>buzzes</code>. Every fifteenth element the values <em>Fizz</em> and <em>Buzz</em> amplify each other and become <em>FizzBuzz</em>. </p> <h3 id="ef186fa307bd47b8a1651761cc1ae7af"> Numbers <a href="#ef186fa307bd47b8a1651761cc1ae7af" title="permalink">#</a> </h3> <p> While you have an infinite stream of <code>fizzBuzzes</code>, you also need a list of numbers. That's easy: </p> <p> <pre><span style="color:#2b91af;">numbers</span>&nbsp;::&nbsp;[<span style="color:#2b91af;">String</span>] numbers&nbsp;=&nbsp;<span style="color:blue;">show</span>&nbsp;&lt;$&gt;&nbsp;[1..100]</pre> </p> <p> You just use a list comprehension and map each number to its <code>String</code> representation using <code>show</code>: </p> <p> <pre>*FizzBuzz&gt; take 18 numbers ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"]</pre> </p> <p> Now you just need to figure out how to merge the <code>fizzBuzzes</code> with the <code>numbers</code>. </p> <h3 id="a84444b7decc42f1b2fb22b7a02718aa"> Zip with catamorphism <a href="#a84444b7decc42f1b2fb22b7a02718aa" title="permalink">#</a> </h3> <p> While you can trivially <code>zip</code> <code>fizzBuzzes</code> with <code>numbers</code>, it doesn't solve the problem of which value to pick: </p> <p> <pre>*FizzBuzz&gt; take 5 $ zip numbers fizzBuzzes [("1", Nothing), ("2", Nothing), ("3", Just "Fizz"), ("4", Nothing), ("5", Just "Buzz")]</pre> </p> <p> You want to use the second element of each tuple when there's a value, and only use the first element (the number) when the second element is <code>Nothing</code>. </p> <p> That's easily done with <code>fromMaybe</code> (you'll need to <code>import Data.Maybe</code> for that): </p> <p> <pre>*FizzBuzz&gt; fromMaybe "2" Nothing "2" *FizzBuzz&gt; fromMaybe "3" $ Just "Fizz" "Fizz"</pre> </p> <p> That's just what you need, so <em>zip</em> <code>numbers</code> with <code>fizzBuzzes</code> using <code>fromMaybe</code>: </p> <p> <pre><span style="color:#2b91af;">elements</span>&nbsp;::&nbsp;[<span style="color:#2b91af;">String</span>] elements&nbsp;=&nbsp;<span style="color:blue;">zipWith</span>&nbsp;fromMaybe&nbsp;numbers&nbsp;fizzBuzzes</pre> </p> <p> These <code>elements</code> is a list of the values the kata instructs you to produce: </p> <p> <pre>*FizzBuzz&gt; take 14 elements ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14"]</pre> </p> <p> <code>fromMaybe</code> is a specialisation of the <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>. I always find it interesting when I can solve a problem with <a href="/2019/04/29/catamorphisms">catamorphisms</a> and <a href="/2017/10/06/monoids">monoids</a>, because it shows that perhaps, there's some value in knowing <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstractions</a>. </p> <h3 id="02f08a325df6462994e6081928bf67bc"> From 1 to 100 <a href="#02f08a325df6462994e6081928bf67bc" title="permalink">#</a> </h3> <p> The kata instructions are to write a program that prints the numbers from 1 to 100, according to the special rules. You can use <code>mapM_ putStrLn</code> for that: </p> <p> <pre><span style="color:#2b91af;">main</span>&nbsp;::&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;=&nbsp;<span style="color:blue;">mapM_</span>&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;elements</pre> </p> <p> When you execute the <code>main</code> function, you get the desired output: </p> <p> <pre>1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16</pre> </p> <p> ... and so on. </p> <h3 id="399d6c6550344aa29b5bb2a88ff8c216"> Golf <a href="#399d6c6550344aa29b5bb2a88ff8c216" title="permalink">#</a> </h3> <p> Haskell <a href="https://en.wikipedia.org/wiki/Code_golf">golfers</a> may complain that the above code is unnecessarily verbose. I disagree, but you can definitely write the entire kata as a 'one-liner' if you want to: </p> <p> <pre><span style="color:#2b91af;">main</span>&nbsp;::&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;= &nbsp;&nbsp;<span style="color:blue;">mapM_</span>&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;$ &nbsp;&nbsp;<span style="color:blue;">zipWith</span>&nbsp;fromMaybe&nbsp;(<span style="color:blue;">show</span>&nbsp;&lt;$&gt;&nbsp;[1..100])&nbsp;$ &nbsp;&nbsp;<span style="color:blue;">zipWith</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">(&lt;&gt;)</span> &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">cycle</span>&nbsp;[Nothing,&nbsp;Nothing,&nbsp;Just&nbsp;<span style="color:#a31515;">&quot;Fizz&quot;</span>]) &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">cycle</span>&nbsp;[Nothing,&nbsp;Nothing,&nbsp;Nothing,&nbsp;Nothing,&nbsp;Just&nbsp;<span style="color:#a31515;">&quot;Buzz&quot;</span>])</pre> </p> <p> I've just mechanically in-lined all the values like <code>fizzes</code>, <code>buzzes</code>, etc. and formatted the code so that it fits comfortable in a <a href="/2019/11/04/the-80-24-rule">80x24 box</a>. Apart from that, I don't think that this is an improvement, but it has the same behaviour as the above, more verbose alternative. </p> <h3 id="1325c76bbf924cb58db4e7782bb95cfd"> Conclusion <a href="#1325c76bbf924cb58db4e7782bb95cfd" title="permalink">#</a> </h3> <p> You can implement the FizzBuzz kata using the fundamental concepts <em>catamorphism</em>, <em>semigroup</em> and <em>monoid</em>. No <code>if-then-else</code> instructions or pattern matching is required. Instead, you use the string concatenation semigroup to enable resonance between two pulses, and the <em>maybe catamorphism</em> to combine with the list of numbers. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The case of the mysterious curly bracket https://blog.ploeh.dk/2019/12/23/the-case-of-the-mysterious-curly-bracket 2019-12-23T06:46:00+00:00 Mark Seemann <div id="post"> <p> <em>The story of a curly bracket that I thought was redundant. Not so.</em> </p> <p> One of my clients was kind enough to show me some of their legacy code. As I was struggling to understand how it worked, I encountered something like this: </p> <p> <pre><span style="color:green;">//&nbsp;A&nbsp;lot&nbsp;of&nbsp;code&nbsp;has&nbsp;come&nbsp;before&nbsp;this.&nbsp;This&nbsp;is&nbsp;really&nbsp;on&nbsp;line&nbsp;665,&nbsp;column&nbsp;29.</span> <span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:#2b91af;">BARLifeScheme_BAR</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">scheme</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">postPartialMembership</span>.<span style="font-weight:bold;color:#74531f;">SchemesFilterObsolete</span>(<span style="color:#2b91af;">BARCompany</span>.ONETIMESUM,&nbsp;<span style="color:blue;">false</span>)) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">schemeCalculated</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#2b91af;">BARLifeSchemeCalculated_BAR</span>)<span style="font-weight:bold;color:#1f377f;">scheme</span>.SchemeCalculatedObsolete[<span style="font-weight:bold;color:#1f377f;">basis</span>.Data.Basis1order]; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">decimal</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">hfcFactor</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">postState</span>.OverallFuturerule&nbsp;==&nbsp;<span style="color:#2b91af;">BAROverallFuturerule</span>.Standard) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">bonusKey</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BonusKey</span>(<span style="font-weight:bold;color:#1f377f;">postState</span>.<span style="font-weight:bold;color:#74531f;">PNr</span>()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">hfcFactor</span>&nbsp;=&nbsp;1M&nbsp;-&nbsp;<span style="color:#2b91af;">CostFactory</span>.<span style="color:#74531f;">Instance</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">CostProvider</span>(<span style="font-weight:bold;color:#1f377f;">postState</span>.Data.FrameContractNr,&nbsp;<span style="font-weight:bold;color:#1f377f;">postState</span>.StateDate) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="font-weight:bold;color:#74531f;">GetAdministrationpercentageContribution</span>(<span style="font-weight:bold;color:#1f377f;">bonusKey</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">basis</span>.Data.Basis1order) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;100M; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Much&nbsp;more&nbsp;code&nbsp;comes&nbsp;after&nbsp;this...</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...and&nbsp;after&nbsp;this...</span> }</pre> </p> <p> For the record, this isn't the actual code that my client gave me. I wouldn't post someone else's code without their permission. It is, however, a faithful imitation of the original code. </p> <p> The actual code started at line 665 and further to the right. It was part of a larger block of code with <code>if</code> statements within <code>foreach</code> loops within <code>if</code> statements within <code>foreach</code> loops, and so on. The <code>foreach</code> keyword actually appeared at column 29. The entire file was 1708 lines long. </p> <p> The code has numerous smells, but here I'll focus on a single oddity. </p> <h3 id="62fa371342be48698054d125b8b326e0"> Inexplicable bracket <a href="#62fa371342be48698054d125b8b326e0" title="permalink">#</a> </h3> <p> Notice the curly bracket on the line before <code>hfcFactor</code>. Why is it there? </p> <p> Take a moment and see if you can guess. </p> <p> It doesn't seem to be required. It just surrounds a block of code, but it belongs to none of the usual language constructs that would normally call for the use of curly brackets. There's no <code>if</code>, <code>foreach</code>, <code>using</code>, or <code>try</code> before it. </p> <h3 id="f56822a0b88d4d288843723fece26f76"> Residue <a href="#f56822a0b88d4d288843723fece26f76" title="permalink">#</a> </h3> <p> I formed a theory as to why those brackets where in place. I thought that it might be the residue of an <code>if</code> statement that was no longer there; that, perhaps, once the code had looked like this: </p> <p> <pre><span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">something</span>&nbsp;&lt;&nbsp;<span style="font-weight:bold;color:#1f377f;">somethingElse</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">decimal</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">hfcFactor</span>;</pre> </p> <p> Later, a developer had discovered that the code in that block should <em>always</em> be executed, and had removed the <code>if</code> statement without removing the curly brackets. </p> <p> That was my theory, but then I noticed that this structure appeared frequently throughout the code. Mysterious curly brackets were common, sometimes even nesting each other. </p> <p> This idiom appeared too often that it looked like the legacy from earlier epochs. It looked deliberate. </p> <h3 id="1b30ee97ed92442caf8d054b4239e334"> The real reason <a href="#1b30ee97ed92442caf8d054b4239e334" title="permalink">#</a> </h3> <p> When I had the opportunity, I asked one of the developers. </p> <p> He smiled sheepishly when he told me that those curly brackets were there to introduce a <em>variable scope</em>. The curly brackets protected variables within them from colliding with other variables elsewhere in the 744-line method. </p> <p> Those scopes enabled programmers to declare variables with names that would otherwise collide with other variables. They even enabled developers to declare a variable with the same name, but a different type. </p> <p> I was appalled. </p> <h3 id="c0fc550039c341ac81340d20d4011e29"> Legacy <a href="#c0fc550039c341ac81340d20d4011e29" title="permalink">#</a> </h3> <p> I didn't write this article to point fingers. I don't think that professional software developers deliberately decide to write obscure code. </p> <p> Code becomes obscure over time. It's a slow, unyielding process. As Brian Foote and Joseph Yoder wrote in <em>The Selfish Class</em> (here quoted from <a href="http://amzn.to/1dEKjcj">Pattern Languages of Program Design 3</a>, p. 461): <blockquote> <p>"Will highly comprehensible code, by virtue of being easy to modify, inevitably be supplanted by increasingly less elegant code until some equilibrium is achieved between comprehensibility and fragility?"</p> <footer><cite>Brian Foote and Joseph Yoder</cite></footer> </blockquote> That's a disturbing thought. It suggests that 'good' code is unstable. I suspect that code tends to rot beyond comprehension. It's death by a thousand cuts. It's not any single edit that produces legacy code. It's the glacial, inexorable slide towards increasingly complicated code. </p> <h3 id="76d3bcfd084247ed8eb9c11239c7523c"> Conclusion <a href="#76d3bcfd084247ed8eb9c11239c7523c" title="permalink">#</a> </h3> <p> Methods may grow to such sizes that variables collide. The solution isn't to add artificial variable scopes. The solution is to extract helper methods. <a href="/2019/11/04/the-80-24-rule">Keep methods small</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Zone of Ceremony https://blog.ploeh.dk/2019/12/16/zone-of-ceremony 2019-12-16T09:51:00+00:00 Mark Seemann <div id="post"> <p> <em>Static typing doesn't have to involve much ceremony.</em> </p> <p> I seem to get involved in long and passionate debates about static versus dynamic typing on a regular basis. I find myself clearly on the side of static typing, but this article isn't about the virtues of static versus dynamic typing. The purpose is to correct a common misconception about statically typed languages. </p> <h3 id="07df6b539dd24732bd2c1f0038a27a71"> Ceremony <a href="#07df6b539dd24732bd2c1f0038a27a71" title="permalink">#</a> </h3> <p> People who favour dynamically typed languages over statically typed languages often emphasise that they find the lack of ceremony productive. That seems reasonable; only, it's a false dichotomy. </p> <ins datetime="2019-12-18T11:27Z"> <blockquote> <p> "Ceremony is what you have to do before you get to do what you really want to do." </p> <footer><cite><a href="https://youtu.be/4jCjDEb9KZI">Venkat Subramaniam</a></cite></footer> </blockquote> </ins> <p> Dynamically typed languages do seem to be light on ceremony, but you can't infer from that that statically typed languages have to require lots of ceremony. Unfortunately, all mainstream statically typed languages belong to the same family, and they <em>do</em> involve ceremony. I think that people extrapolate from what they know; they falsely conclude that all statically typed languages must come with the overhead of ceremony. </p> <p> It looks to me more as though there's an unfortunate <em>Zone of Ceremony</em>: </p> <p> <img src="/content/binary/zone-of-ceremony.png" alt="A conceptual spectrum of typing, from dynamic on the left, to static on the right. There's a zone of ceremony slightly to the right of the middle with the languages C++, C#, and Java."> </p> <p> Such a diagram can never be anything but a simplification, but I hope that it's illuminating. C++, Java, and C# are all languages that involve ceremony. To the right of them are what we could term the <em>trans-ceremonial languages</em>. These include <a href="https://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>. </p> <ins datetime="2019-12-18T19:30Z"> <p> In the following, I'll show some code examples in various languages. I'll discuss ceremony according to the above definition. The discussion focuses on the amount of preparatory work one has to do, such as creating a new file, declaring a new class, and declaring types. The discussion is <em>not</em> about the implementation code. For that reason, I've removed colouring from the implementation code, and emphasised the code that I consider ceremonial. </p> </ins> <h3 id="32ff9013011b4c04a24e32d635b55ab2"> Low ceremony of JavaScript <a href="#32ff9013011b4c04a24e32d635b55ab2" title="permalink">#</a> </h3> <p> Imagine that you're given a list of numbers, as well as a quantity. The quantity is a number to be consumed. You must remove elements from the left until you've consumed at least that quantity. Then return the rest of the list. </p> <p> <pre>&gt; consume ([1,2,3], 1); [ 2, 3 ] &gt; consume ([1,2,3], 2); [ 3 ] &gt; consume ([1,2,3], 3); [ 3 ] &gt; consume ([1,2,3], 4); []</pre> </p> <p> The first example consumes only the leading <code>1</code>, while both the second and the third example consumes both <code>1</code> and <code>2</code> because the sum of those values is <code>3</code>, and the requested quantity is <code>2</code> and <code>3</code>, respectively. The fourth example consumes all elements because the requested quantity is <code>4</code>, and you need both <code>1</code>, <code>2</code>, and <code>3</code> before the sum is large enough. You have to pick strictly from the left, so you can't decide to just take the elements <code>1</code> and <code>3</code>. </p> <ins datetime="2020-04-27T14:40Z"> <p> If you're wondering why such a function would be useful, <a href="/2020/04/27/an-f-implementation-of-the-maitre-d-kata">here's my motivating example</a>. </p> </ins> <p> In JavaScript, you could implement the <code>consume</code> function like this: </p> <p> <pre><strong><span style="color:blue;">var</span>&nbsp;consume&nbsp;=&nbsp;<span style="color:blue;">function</span>&nbsp;(source,&nbsp;quantity)&nbsp;{</strong> <span style="color: silver;">&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!source)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;accumulator&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;result&nbsp;=&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;source.length;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;x&nbsp;=&nbsp;source[i]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(quantity&nbsp;&lt;=&nbsp;accumulator) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.push(x); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accumulator&nbsp;+=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;result;</span> <strong>}</strong></pre> </p> <p> I'm a terrible JavaScript programmer, so I'm sure that it could have been done more elegantly, but as far as I can tell, it gets the job done. I wrote some tests, and I have 17 passing test cases. The point isn't about how you write the function, but how much ceremony is required. In JavaScript you don't need to declare any types. Just name the function and its arguments, and you're ready to write code. </p> <h3 id="2b1bcebf36084abcae5bb231ca0ebe15"> High ceremony of C# <a href="#2b1bcebf36084abcae5bb231ca0ebe15" title="permalink">#</a> </h3> <p> Contrast the JavaScript example with C#. The same function in C# would look like this: </p> <p> <pre><strong><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Enumerable</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="color:#74531f;">Consume</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{</strong> <span style="color: silver;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(source&nbsp;is&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;accumulator&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;i&nbsp;in&nbsp;source) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(quantity&nbsp;&lt;=&nbsp;accumulator) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;return&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accumulator&nbsp;+=&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span> <strong>&nbsp;&nbsp;&nbsp;&nbsp;} }</strong></pre> </p> <p> Here you have to declare the type of each method argument, as well as the return type of the method. You also have to put the method in a class. This may not seem like much overhead, but if you later need to change the types, editing is required. This can affect downstream callers, so simple type changes ripple through code bases. </p> <p> It gets worse, though. The above <code>Consume</code> method only handles <code>int</code> values. What if you need to call the method with <code>long</code> arrays? </p> <p> You'd have to add an overload: </p> <p> <pre><strong><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">long</span>&gt;&nbsp;<span style="color:#74531f;">Consume</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">long</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">source</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">long</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">quantity</span>) {</strong> <span style="color: silver;">&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(source&nbsp;is&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;accumulator&nbsp;=&nbsp;0L; &nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;i&nbsp;in&nbsp;source) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(quantity&nbsp;&lt;=&nbsp;accumulator) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;return&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accumulator&nbsp;+=&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;}</span> <strong>}</strong></pre> </p> <p> Do you need support for <code>short</code>? Add an overload. <code>decimal</code>? Add an overload. <code>byte</code>? Add an overload. </p> <p> No wonder people used to dynamic languages find this awkward. </p> <h3 id="bc9ab1e2693d41678a09cf843436c736"> Low ceremony of F# <a href="#bc9ab1e2693d41678a09cf843436c736" title="permalink">#</a> </h3> <p> You can write the same functionality in F#: </p> <p> <pre><strong><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">inline</span></strong><span style="color: silver;">&nbsp;consume&nbsp;quantity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;go&nbsp;(acc,&nbsp;xs)&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;quantity&nbsp;&lt;=&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;(acc,&nbsp;Seq.append&nbsp;xs&nbsp;(Seq.singleton&nbsp;x)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;(acc&nbsp;+&nbsp;x,&nbsp;xs) &nbsp;&nbsp;&nbsp;&nbsp;Seq.fold&nbsp;go&nbsp;(LanguagePrimitives.GenericZero,&nbsp;Seq.empty)&nbsp;&gt;&gt;&nbsp;snd</span></pre> </p> <p> There's no type declaration in sight, but nonetheless the function is statically typed. It has this somewhat complicated type: </p> <p> <pre>quantity: ^a -&gt; (seq&lt; ^b&gt; -&gt; seq&lt; ^b&gt;) when ( ^a or ^b) : (static member ( + ) : ^a * ^b -&gt; ^a) and ^a : (static member get_Zero : -&gt; ^a) and ^a : comparison</pre> </p> <p> While this looks arcane, it means that it support sequences of any type that comes with a zero value and supports addition and comparison. You can call it with both 32-bit integers, decimals, and so on: </p> <p> <pre>&gt; consume 2 [1;2;3];; val it : seq&lt;int&gt; = seq [3] &gt; consume 2m [1m;2m;3m];; val it : seq&lt;decimal&gt; = seq [3M]</pre> </p> <p> Static typing still means that you can't just call it with any type of value. An expression like <code>consume "foo" [true;false;true]</code> will not compile. </p> <p> You can explicitly declare types in F# (like you can in C#), but my experience is that if you don't, type changes tend to just propagate throughout your code base. Change a type of a function, and upstream callers generally just 'figure it out'. If you think of functions calling other functions as a graph, you often only have to adjust leaf nodes even when you change the type of something deep in your code base. </p> <h3 id="e360bba3f4954f6988fd1267c70b78e1"> Low ceremony of Haskell <a href="#e360bba3f4954f6988fd1267c70b78e1" title="permalink">#</a> </h3> <p> Likewise, you can write the function in Haskell: </p> <p> <pre><span style="color: silver;">consume&nbsp;quantity&nbsp;=&nbsp;reverse&nbsp;.&nbsp;snd&nbsp;.&nbsp;foldl&nbsp;go&nbsp;(0,&nbsp;[]) &nbsp;&nbsp;</span><strong>where</strong><span style="color: silver;"> &nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;(acc,&nbsp;ys)&nbsp;x&nbsp;=&nbsp;if&nbsp;quantity&nbsp;&lt;=&nbsp;acc&nbsp;then&nbsp;(acc,&nbsp;x:ys)&nbsp;else&nbsp;(acc&nbsp;+&nbsp;x,&nbsp;ys)</span></pre> </p> <p> Again, you don't have to explicitly declare any types. The compiler figures them out. You can ask GHCi about the function's type, and it'll tell you: </p> <p> <pre>&gt; :t consume consume :: (Foldable t, Ord a, Num a) =&gt; a -&gt; t a -&gt; [a]</pre> </p> <p> It's more compact than the inferred F# type, but the idea is the same. It'll compile for any <code>Foldable</code> container <code>t</code> and any type <code>a</code> that belongs to the classes of types called <code>Ord</code> and <code>Num</code>. <code>Num</code> supports addition and <code>Ord</code> supports comparison. </p> <p> There's little ceremony involved with the types in Haskell or F#, yet both languages are statically typed. In fact, their type systems are more powerful than C#'s or Java's. They can express relationships between types that those languages can't. </p> <h3 id="d215cd3b1cd84ce3877dc88e8ce944be"> Summary <a href="#d215cd3b1cd84ce3877dc88e8ce944be" title="permalink">#</a> </h3> <p> In debates about static versus dynamic typing, contributors often generalise from their experience with C++, Java, or C#. They dislike the amount of ceremony required in these languages, but falsely believe that it means that you can't have static types without ceremony. </p> <p> The statically typed mainstream languages seem to occupy a <em>Zone of Ceremony</em>. </p> <p> Static typing without ceremony is possible, as evidenced by languages like F# and Haskell. You could call such languages <em>trans-ceremonial languages</em>. They offer the best of both worlds: compile-time checking and little ceremony. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="7ed32b43867d490cad18388ec86baab4"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#7ed32b43867d490cad18388ec86baab4">#</a></div> <div class="comment-content"> <p> In your initial <code>int</code> C# example, I think your point is that method arguments and the return type require <a href="https://en.wikipedia.org/wiki/Manifest_typing">manifest</a> typing. Then for your example about <code>long</code> (and comments about <code>short</code>, <code>decimal</code>, and <code>byte</code>), I think your point is that C#'s type system is primarily <a href="https://en.wikipedia.org/wiki/Nominal_type_system">nominal</a>. You then contrast those C# examples with F# and Haskell examples that utilize <a href="https://en.wikipedia.org/wiki/Type_inference">inferred</a> and <a href="https://en.wikipedia.org/wiki/Structural_type_system">structural</a> aspects of their type systems. </p> <p> I also sometimes get involved in debates about static versus dynamic typing and find myself on the side of static typing. Furthermore, I also typically hear arguments against manifest and nominal typing instead of against static typing. In theory, I agree with those arguments; I also prefer type systems that are inferred and structural instead of those that are manifest and nominal. </p> <p> I see the tradeoff as being among the users of the programming language, those responsible for writing and maintaining the compiler/interpreter, and what can be said about the correctness of the code. (In the rest of this paragraph, all statements about things being simple or complex are meant to be relative. I will also exaggerate for the sake of simplifying my statements.) For a dynamic language, the interpreter and coding are simple but there are no guarantees about correctness. For a static, manifest, and nominal language, the compiler is somewhere between simple and complex, the coding is complex, but at least there are some guarantees about correctness. For a static, inferred, structural language, the compiler is complex, coding is simple, and there are some guarantees about correctness. </p> <p> Contrasting a dynamic language with one that is static, inferred, and structural, I see the tradeoff as being directly between the the compiler/interpreter writers and what can be said about the correctness of the code while the experience of those writing code in the language is mostly unchanged. I think that is your point being made by contrasting the JavaScript example (a dynamic language) with the F# and Haskell examples (that demonstrate the static, inferred, and structural behavior of their type systems). </p> <p> While we are on the topic, I would like to say something that I think is controversial about <a href="https://en.wikipedia.org/wiki/Duck_typing">duck typing</a>. I think duck typing is "just" a dynamic type system that is also structural. This contradicts the lead of its Wikipedia article (linked above) as well as the <a href="https://en.wikipedia.org/wiki/Duck_typing#Structural_type_systems">subsection about structural type systems</a>. They both imply that nominal vs structural typing is a spectrum that only exists for static languages. I disagree; I think dynamic languages can also exist on that spectrum. It is just that most dynamic languages are also structural. In contrast, I think that the manifest vs inferred spectrum exists for static languages but not for dynamic languages. </p> <p> Nonetheless, that subsection makes a great observation. For structural languages, the difference between static and dynamic languages is not just some guarantees about correctness. Dynamic languages check for type correctness at the last possible moment. (That is saying more than saying that the type check happens at runtime.) For example, consider a function with dead code that "doesn't type". If the type system were static, then this function cannot be executed, but if the type system were dynamic, then it could be executed. More practically, suppose the function is a simple <code>if-else</code> statement with code in the <code>else</code> branch that "doesn't type" and that the corresponding Boolean expression always evaluates to <code>true</code>. If the type system were static, then this function cannot be executed, but if the type system were dynamic, then it could be executed. </p> <p> In my experience, the typical solution of a functional programmer would be to strengthen the input types so that the <code>else</code> branch can be proved by the compiler to be dead code and then delete the dead code. This approach makes this one function simpler, and I generally am in favor of this. However, there is a sense in which we can't always repeat this for the calling function. Otherwise, we would end up with a program that is provably correct, which is impossible for a Turning-complete language. Instead, I think the practical solution is to (at some appropriate level) short-circuit the computation when given input that is not known to be good and either do nothing or report back to the user that the input wasn't accepted. </p> </div> <div class="comment-date">2019-12-16 17:12 UTC</div> </div> <div class="comment" id="048a007a6d7f4f67b4ed92b748c78c13"> <div class="comment-author">Romain Deneau <a href="https://twitter.com/DeneauRomain">@DeneauRomain</a> <a href="#048a007a6d7f4f67b4ed92b748c78c13">#</a></div> <div class="comment-content"> <p> Using mostly both C# and TypeScript, two statically typed languages, I’ve experienced how it’s terser in TypeScript, essentially thanks to its type inference and its structural typing. I like the notion of <em>“Ceremony”</em> you gave to describe this and the fact that it’s not correlated to the kind of typing, dynamic or static 👍 </p> <p> Still, TypeScript is more verbose than F#, as we can see with the following code translation from F# to TypeScript using object literal instead of tuple for the better support of the former: </p> <pre> <span class="hljs-comment" style="color:green;">// const consume = (source: number[], quantity: number): number[]</span> <span class="hljs-keyword" style="color:blue;">const</span> consume = (source: <span class="hljs-built_in" style="color:blue;">number</span>[], quantity: <span class="hljs-built_in" style="color:blue;">number</span>) =&gt;   source.reduce(({ acc, xs }, x) =&gt;     quantity &lt;= acc       ? { acc, xs: xs.concat(x) }       : { acc: acc + x, xs },     { acc: <span class="hljs-number" style="color:purple">0</span>, xs: [] <span class="hljs-keyword" style="color:blue;">as</span> <span class="hljs-built_in" style="color:blue;">number</span>[] }   ).xs;</pre> <p> Checks: </p> <pre> &gt; consume(1, [1,2,3]) [2,3] &gt; consume(2, [1,2,3]) [3] &gt; consume(3, [1,2,3]) [3] &gt; consume(4, [1,2,3]) []</pre> <p> As we can see, the code is a little more verbose than in JavaScript but still terser than in C#. The returned type is inferred as <code>number[]</code> but the <code>as number[]</code> is a pity, necessary because the inferred type of the empty array <code>[]</code> is <code>any[]</code>. </p> <p> <code>consume</code> is not generic: TypeScript/JavaScript as only one primitive for numbers: <code>number</code>. It works for common scenarios but their no simple way to make it work with <code>BigInt</code>, for instance using the union type <code>number | bigint</code>. The more pragmatic option would be to copy-paste, replacing <code>number</code> with <code>bigint</code> and <code>0</code> with <code>0n</code>. </p> </div> <div class="comment-date">2019-12-20 10:10 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Put cyclomatic complexity to good use https://blog.ploeh.dk/2019/12/09/put-cyclomatic-complexity-to-good-use 2019-12-09T14:37:00+00:00 Mark Seemann <div id="post"> <p> <em>An actually useful software metric.</em> </p> <p> In <a href="https://cleancoders.com/video-details/humane-code-real-episode-1">Humane Code</a> I argue that software development suffers from a lack of useful measurements. While I stand by that general assertion, a few code metrics can be useful. <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic complexity</a>, while no <a href="/2019/07/01/yes-silver-bullet">silver bullet</a>, can be put to good use. </p> <h3 id="1462c0daaa1d42eba09691214f9ac8da"> Recap <a href="#1462c0daaa1d42eba09691214f9ac8da" title="permalink">#</a> </h3> <p> I think of cyclomatic complexity as a measure of the number of pathways through a piece of code. Even the simplest body of code affords a single pathway, so the minimum cyclomatic complexity is <em>1</em>. You can easily 'calculate' the cyclomatic complexity of a method for function. You start at one, and then you count how many times <code>if</code> and <code>for</code> occurs. For each of these keywords you find, you increment the number (which started at <em>1</em>). </p> <p> The specifics are language-dependent. The idea is to count branching and looping instructions. In C#, for example, you'd also have to include <code>foreach</code>, <code>while</code>, <code>do</code>, and each <code>case</code> in a <code>switch</code> block. In other languages, the keywords to count will differ. </p> <p> What's the cyclomatic complexity of this <code>TryParse</code> method? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">TryParse</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>&nbsp;=&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">arr</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>.<span style="font-weight:bold;color:#74531f;">Split</span>(<span style="color:#a31515;">&#39;,&#39;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">arr</span>.Length&nbsp;&lt;&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>(<span style="font-weight:bold;color:#1f377f;">arr</span>[0],&nbsp;<span style="font-weight:bold;color:#1f377f;">arr</span>[1]); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> The cyclomatic complexity of this method is <em>2</em>. You start with the number <em>1</em> and then increment it every time you find one of the branching keywords. In this case, there's only a single <code>if</code>, so increment <em>1</em> to <em>2</em>. That's it. If you're in doubt, <a href="https://docs.microsoft.com/en-us/visualstudio/code-quality/code-metrics-values">Visual Studio can calculate metrics for you</a>. (It calculates other metrics as well, but I don't find those useful.) </p> <h3 id="ca340e4d0dbd4391a1e15bd56ab43076"> Guide for unit testing <a href="#ca340e4d0dbd4391a1e15bd56ab43076" title="permalink">#</a> </h3> <p> I find cyclomatic complexity useful because it measures the number of pathways through a method. As such, it indicates the <em>minimum</em> number of test cases you ought to furnish. <ins datetime="2023-07-14T06:48Z">(<em>Edit July 14, 2023:</em> While this tends to work well in practice, it turns out that it's not strictly true in general. See the article <a href="/2023/05/08/is-cyclomatic-complexity-really-related-to-branch-coverage">Is cyclomatic complexity really related to branch coverage?</a> and the comments for more details.)</ins> This is useful when reviewing code and tests. </p> <p> Sometimes I'm presented with code that other people wrote. When I look through the production code, I consider its cyclomatic complexity. If, for example, a method has a cyclomatic complexity of <em>5</em>, I'd expect to find at least five test cases to cover that method. </p> <p> At other times, I start by reading the tests. The number of test cases gives me a rough indication of what degree of complexity to expect. If I see four distinct tests for the same method, I expect it to have a cyclomatic complexity about <em>4</em>. </p> <p> I don't demand 100% coverage. Sometimes, people don't write tests for <a href="https://en.wikipedia.org/wiki/Guard_(computer_science)">guard clauses</a>, and I usually accept such omissions. On the other hand, I think that proper decision logic should be covered by tests. If I were to stick unwaveringly to cyclomatic complexity, that would make my reviews more objective, but not necessarily better. I could insist on 100% code coverage, but <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">I don't consider that a good idea</a>. </p> <p> Presented with the above <code>TryParse</code> method, I'd expect to see at least two unit tests, since the cyclomatic complexity is <em>2</em>. </p> <h3 id="bd88752e8821476984182c968e7a00bb"> The need for more test cases <a href="#bd88752e8821476984182c968e7a00bb" title="permalink">#</a> </h3> <p> Two unit tests aren't enough, though. You could write these two tests: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryParseSucceeds</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">couldParse</span>&nbsp;=&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>.<span style="color:#74531f;">TryParse</span>(<span style="color:#a31515;">&quot;foo,bar&quot;</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="font-weight:bold;color:#1f377f;">couldParse</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>(<span style="font-weight:bold;color:#1f377f;">expected</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>); } [<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryParseFails</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">couldParse</span>&nbsp;=&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>.<span style="color:#74531f;">TryParse</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">False</span>(<span style="font-weight:bold;color:#1f377f;">couldParse</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Null</span>(<span style="font-weight:bold;color:#1f377f;">actual</span>); }</pre> </p> <p> Using the <a href="/2019/10/07/devils-advocate">Devil's advocate</a> technique, however, this implementation of <code>TryParse</code> passes both tests: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">TryParse</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>&nbsp;=&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">candidate</span>&nbsp;!=&nbsp;<span style="color:#a31515;">&quot;foo,bar&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> This is clearly not the correct implementation, but it has 100% code coverage. It also still has cyclomatic complexity of <em>2</em>. The metric suggests a <em>minimum</em> number of tests - not a sufficient number. </p> <h3 id="15d3b6efa5f74ab4ba5428d81a9e2443"> More test cases <a href="#15d3b6efa5f74ab4ba5428d81a9e2443" title="permalink">#</a> </h3> <p> It often makes sense to cover each branch with a single parametrised test: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo,bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;baz,qux&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh,fnaah&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;fnaah&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo,bar,baz&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryParseSucceeds</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">userName</span>,&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">password</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">couldParse</span>&nbsp;=&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">candidate</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="font-weight:bold;color:#1f377f;">couldParse</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">expected</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>(<span style="font-weight:bold;color:#1f377f;">userName</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">password</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>(<span style="font-weight:bold;color:#1f377f;">expected</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>); } [<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo;bar&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">TryParseFails</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">couldParse</span>&nbsp;=&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">candidate</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">False</span>(<span style="font-weight:bold;color:#1f377f;">couldParse</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Null</span>(<span style="font-weight:bold;color:#1f377f;">actual</span>); }</pre> </p> <p> Is a total of eight test cases the correct number? Cyclomatic complexity can't help you here. You'll have to rely on other heuristics, such as test-driven development, the <a href="https://blog.cleancoder.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html">transformation priority premise</a>, and the Devil's Advocate. </p> <h3 id="a8b2fa3d0101430d871b99a4bab136b7"> Humane Code <a href="#a8b2fa3d0101430d871b99a4bab136b7" title="permalink">#</a> </h3> <p> I also find cyclomatic complexity useful for another reason. I keep an eye on complexity because I care about code maintainability. In my <a href="https://cleancoders.com/video-details/humane-code-real-episode-1">Humane Code</a> video, I discuss <a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two">the magic number seven, plus or minus two</a>. </p> <p> When you read code, you essentially run a little emulator in your brain. You have to maintain state in order to interpret the code you look at. <em>Will this conditional evaluate to true or false? Is the code going to exit that loop now? Is that array index out of bounds?</em> You can only follow the code by keeping track of variables' contents, and your brain can keep track of approximately seven things. </p> <p> Cyclomatic complexity is a measure of pathways - not how many things you need to keep track of. Still, in my experience, there seems to be a useful correlation. Code with high cyclomatic complexity tends to have many moving parts. There's too much to keep track of. With low cyclomatic complexity, on the other hand, the code involves few moving parts. </p> <p> I use cyclomatic complexity <em>7</em> as an approximate maximum for that reason. It's only a rule of thumb, since I'm painfully aware that I'm transplanting experimental psychology to a context where no conclusions can be scientifically drawn. But like <a href="/2019/11/04/the-80-24-rule">the 80/24 rule</a> I find that it works well in practice. </p> <h3 id="de927bfcc95d410bbfcd0adf7a63926b"> Complexity of a method call <a href="#de927bfcc95d410bbfcd0adf7a63926b" title="permalink">#</a> </h3> <p> Consider the above parametrised tests. Some of the test cases provide enough triangulation to defeat the Devil's attempt at hard-coding return values. This explains test values like <code>"foo,bar"</code>, <code>"baz,qux"</code>, and <code>"ploeh,fnaah"</code>, but why did I include the <code>"foo,bar,baz"</code> test case? And why did I include the empty string as one of the test cases for <code>TryParseFails</code>? </p> <p> When I write tests, I aspire to compose tests that verify the behaviour rather than the implementation of the System Under Test. The desired behaviour, I decided, is that any extra entries in the comma-separated input should be ignored. Likewise, if there's fewer than two entries, parsing should fail. There must be both a user name and a password. </p> <p> Fortunately, this happens to be how <a href="https://docs.microsoft.com/dotnet/api/system.string.split">Split</a> already works. If you consider all the behaviour that <code>Split</code> exhibits, it encapsulates moderate complexity. It can split on multiple alternative delimiters, it can throw away empty entries, and so on. What would happen if you inline some of that functionality? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">TryParse</span>(<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>?&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>&nbsp;=&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">element</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">c</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">c</span>&nbsp;==&nbsp;<span style="color:#a31515;">&#39;,&#39;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">element</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">element</span>&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">element</span>&nbsp;+=&nbsp;<span style="font-weight:bold;color:#1f377f;">c</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">element</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">l</span>.Count&nbsp;&lt;&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">credentials</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UserNamePassworCredentials</span>(<span style="font-weight:bold;color:#1f377f;">l</span>[0],&nbsp;<span style="font-weight:bold;color:#1f377f;">l</span>[1]); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> This isn't as sophisticated as the <code>Split</code> method it replaces, but it passes all eight test cases. Why did I do this? To illustrate the following point. </p> <p> What's the cyclomatic complexity now? </p> <p> Keep in mind that the externally observable behaviour (as defined by eight test cases) hasn't changed. The cyclomatic complexity, however, has. It's now <em>4</em> - double the previous metric. </p> <p> A method call (like a call to <code>Split</code>) can hide significant cyclomatic complexity. That's a desirable situation. This is the benefit that <a href="/encapsulation-and-solid">encapsulation</a> offers: that you don't have to worry about implementation details as long as both caller and callee fulfils the contract. </p> <p> When you calculate cyclomatic complexity, a method call doesn't increment the complexity, regardless of the degree of complexity that it encapsulates. </p> <h3 id="10a3f65b7bf54b04b35b8e69552803b9"> Summary <a href="#10a3f65b7bf54b04b35b8e69552803b9" title="permalink">#</a> </h3> <p> Cyclomatic complexity is one of the rare programming metrics that I find useful. It measures the number of pathways through a body of code. </p> <p> You can use it to guide your testing efforts. The number is the minimum number of tests you must write in order to cover all branches. You'll likely need more test cases than that. </p> <p> You can also <a href="/2020/04/13/curb-code-rot-with-thresholds">use the number as a threshold</a>. I suggest that <em>7</em> ought to be the maximum cyclomatic complexity of a method or function. You're welcome to pick another number, but keeping an eye on cyclomatic complexity is useful. It tells you when it's time to refactor a complex method. </p> <p> Cyclomatic complexity considers only the code that directly implements a method or function. That code can call other code, but what happens behind a method call doesn't impact the metric. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="6fa1adc55d8841918b27599749283abd"> <div class="comment-author">Ghillie Dhu <a href="#6fa1adc55d8841918b27599749283abd">#</a></div> <div class="comment-content"> <p> Do you know of a tool to calculate cyclomatic complexity for F#? It appears that the Visual Studio feature doesn't support it. </p> </div> <div class="comment-date">2019-12-09 19:20 UTC</div> </div> <div class="comment" id="1caf28073f484530ad8389f44ad4a531"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1caf28073f484530ad8389f44ad4a531">#</a></div> <div class="comment-content"> <p> Ghillie, thank you for writing. I'm not aware of any such tool. </p> <p> FWIW, it's not difficult to manually calculate cyclometric complexity for F#, but clearly that doesn't help if you'd like to automate the process. </p> <p> It might be a fine project for anyone looking for a way to contribute to the F# ecosystem. </p> </div> <div class="comment-date">2019-12-09 20:06 UTC</div> </div> <div class="comment" id="ab53d55f92eb4acea0a896436294f3af"> <div class="comment-author">Carlos Schults <a href="#ab53d55f92eb4acea0a896436294f3af">#</a></div> <div class="comment-content"> <p> Hi, Mark. Thanks for your article. I'd commenting because I'd like to learn more about your thoughts on mutation testing. I ask this because I know you're not the biggest fan of code coverage as a useful metric. I'm not either, or at least I wasnt, until I learned about mutation testing. </p> <p>My current view is that code coverage is only (mostly) meaningless if you don't have a way of measuring the quality of the tests. Since mutation testing's goal is exactly that (to test the tests, if you will) my opinion is that, if you use a mutation testing tool, then code coverage become really useful and you should try to get to 100%. I've even written a <a href="https://blog.ncrunch.net/post/mutation-testing-code-coverage.aspx">post about this subject.</a></p> <p>So, in short: what are your thoughts on mutation testing and how it affects the meaning of code coverage, if at all? Looking forward to read your answer. A whole post on this would be even better! </p> <p>Thanks!</p> </div> <div class="comment-date">2019-12-14 12:32 UTC</div> </div> <div class="comment" id="4b240074da164f87bfabcc484a2b8c7b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4b240074da164f87bfabcc484a2b8c7b">#</a></div> <div class="comment-content"> <p> Carlos, thank you for writing. I'm sympathetic to the idea of mutation testing, but apart from that, I have no opinion of it. I don't think that I ought to have an opinion about something with which I've no experience. </p> <p> I first heard about mutation testing decades ago, but I've never come across a mutation testing tool for C# (or F#, for that matter). Can you recommend one? </p> </div> <div class="comment-date">2019-12-14 13:51 UTC</div> </div> <div class="comment" id="46eafa3c33ba47b1bacd997f7e217c4f"> <div class="comment-author">Carlos Schults <a href="#46eafa3c33ba47b1bacd997f7e217c4f">#</a></div> <div class="comment-content"> <p> Unfortunately, tooling is indeed one of the main Achilles heels of mutation testing, at least when it comes to .NET. </p> <p> In the Java world, they have <a href="https://pitest.org/">PIT</a>, which is considered state of the art. For C#, I have tried a few tools, with no success. The most promising solution I've found so far, for C#, is <a href="https://stryker-mutator.io/stryker-net/">Stryker.net</a>, which is a port of the Stryker mutation, designed originally for JavaScript. The C# version is still in its early phases but it's already usable and it looks very promising. </p> </div> <div class="comment-date">2019-12-14 16:16 UTC</div> </div> <div class="comment" id="32db969bac2d466a9be05111cc505f9d"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#32db969bac2d466a9be05111cc505f9d">#</a></div> <div class="comment-content"> <p> Is mutation testing the automated version of what Mark has called the <a href="/2019/10/07/devils-advocate">Devil's Advocate technique</a>? </p> </div> <div class="comment-date">2019-12-15 02:26 UTC</div> </div> <div class="comment" id="c889aa4c4ee04df38e8954afc30e6a6f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c889aa4c4ee04df38e8954afc30e6a6f">#</a></div> <div class="comment-content"> <p> Tyson, I actually <a href="/2019/10/07/devils-advocate#26be7b38248c4dcba5134eb4529d8214">discuss the relationship with mutation testing</a> in that article. </p> </div> <div class="comment-date">2019-12-15 9:28 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Refactoring registration flow to functional architecture https://blog.ploeh.dk/2019/12/02/refactoring-registration-flow-to-functional-architecture 2019-12-02T08:19:00+00:00 Mark Seemann <div id="post"> <p> <em>An example showing a refactoring from F# partial application 'dependency injection' to an impure/pure/impure sandwich.</em> </p> <p> In <a href="/2017/02/02/dependency-rejection#36c724b49f614104842c47909cd9c916">a comment</a> to <a href="/2017/02/02/dependency-rejection">Dependency rejection</a>, I wrote: <blockquote> "I'd welcome a simplified, but still concrete example where the impure/pure/impure sandwich described here isn't going to be possible." </blockquote> <a href="https://www.relativisticramblings.com">Christer van der Meeren</a> kindly <a href="/2017/02/02/dependency-rejection#ade3787e6e3c4e569854e2c2bd038e29">replied with a suggestion.</a> </p> <p> The code in question relates to <del datetime="2023-06-28T17:47Z">validation</del><ins datetime="2023-06-28T17:47Z">verification</ins> of user accounts. You can read the complete description in the linked comment, but I'll try to summarise it here. I'll then show a refactoring to a <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a> - specifically, to an <a href="/2020/03/02/impureim-sandwich">impure/pure/impure sandwich</a>. </p> <p> The code is <a href="https://github.com/ploeh/RegistrationFlow">available on GitHub</a>. </p> <h3 id="53c0b4111cf640e0b6fd13066e24f3bd"> Registration flow <a href="#53c0b4111cf640e0b6fd13066e24f3bd" title="permalink">#</a> </h3> <p> The system in question uses two-factor authentication with mobile phones. When you sign up for the service, you give your phone number. You then receive an SMS, and must use whatever is in that SMS to prove ownership of the phone number. Christer van der Meeren illustrates the flow like this: </p> <p> <img src="/content/binary/complete-registration-workflow-with-2fa-difficult-to-sandwich.png" alt="A flowchart describing the workflow for completing a registration."> </p> <p> He also supplies sample code: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;(createProof:&nbsp;Mobile&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;ProofId&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(verifyProof:&nbsp;Mobile&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ProofId&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;bool&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(completeRegistration:&nbsp;Registration&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;unit&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(proofId:&nbsp;ProofId&nbsp;option) &nbsp;&nbsp;&nbsp;&nbsp;(registration:&nbsp;Registration) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Async&lt;CompleteRegistrationResult&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proofId&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;proofId&nbsp;=&nbsp;createProof&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;proofId&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;isValid&nbsp;=&nbsp;verifyProof&nbsp;registration.Mobile&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;completeRegistration&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;RegistrationCompleted &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;proofId&nbsp;=&nbsp;createProof&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> While this is <a href="https://fsharp.org">F#</a>, it's not functional, since it uses <a href="/2017/01/30/partial-application-is-dependency-injection">partial application for dependency injection</a>. From the description, I find it safe to assume that we can consider <a href="/2016/04/11/async-as-surrogate-io">Async as a surrogate for IO</a>. </p> <p> The code implies the existence of other types. I decided to define them like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Mobile&nbsp;=&nbsp;Mobile&nbsp;<span style="color:blue;">of</span>&nbsp;int <span style="color:blue;">type</span>&nbsp;ProofId&nbsp;=&nbsp;ProofId&nbsp;<span style="color:blue;">of</span>&nbsp;Guid <span style="color:blue;">type</span>&nbsp;Registration&nbsp;=&nbsp;{&nbsp;Mobile&nbsp;:&nbsp;Mobile&nbsp;} <span style="color:blue;">type</span>&nbsp;CompleteRegistrationResult&nbsp;=&nbsp;ProofRequired&nbsp;<span style="color:blue;">of</span>&nbsp;ProofId&nbsp;|&nbsp;RegistrationCompleted</pre> </p> <p> In reality, they're probably more complicated, but this is enough to make the code compile. </p> <p> Is it possible to refactor <code>completeRegistrationWorkflow</code> to an impure/pure/impure sandwich? </p> <h3 id="3266f23516dc4d9e98f3a8c87d072f89"> Applicability <a href="#3266f23516dc4d9e98f3a8c87d072f89" title="permalink">#</a> </h3> <p> It <em>is</em> possible to refactor <code>completeRegistrationWorkflow</code> to an impure/pure/impure sandwich. You'll see how to do that soon. Before we start that work, however, I'd like to warn against jumping to conclusions. It's possible that the problem statement doesn't capture some subtleties that one would have to deal with in the real world. It's also possible that I've misunderstood the essence of Christer van der Meeren's problem description. </p> <p> It's (relatively) easy to teach the basics of programming. You teach a beginner about keywords, programming constructs, how to compile or interpret a program, and so on. </p> <p> On the other hand, it's hard to write about dealing with complicated code. There are ways to make legacy code better, but the moves you have to make depend on myriad details. Complicated code is, by definition, something that's hard to learn. This means that truly complicated legacy code is rarely suitable for instructive examples. One has to strike a delicate balance and produce an example that looks complicated enough to warrant improvement, but on the other hand still be simple enough to be understood. </p> <p> I think that Christer van der Meeren has struck that balance. With three dependencies, the sample code looks just complicated enough to warrant refactoring. On the other hand, you can understand what it's supposed to do in a few minutes. There's a risk, however, that the example is <em>too</em> simplified. That could weaken the result of the refactoring that follows. Could you still apply that refactoring if the problem was more complicated? </p> <p> It's my experience that it's conspicuously often possible to implement an impure/pure/impure sandwich. </p> <h3 id="fedd0146b0a84ab3b768f3adcf4f684f"> Fakes <a href="#fedd0146b0a84ab3b768f3adcf4f684f" title="permalink">#</a> </h3> <p> In the rest of this article, I want to show how to refactor <code>completeRegistrationWorkflow</code> to an impure/pure/impure sandwich. As <a href="http://amzn.to/YPdQDf">Refactoring</a> admonishes: <blockquote> <p> "to refactor, the essential precondition is [...] solid tests" </p> <footer><cite>Martin Fowler</cite></footer> </blockquote> Right now, however, there's no tests, so I'm going to add some. </p> <p> The tests will need some <a href="https://en.wikipedia.org/wiki/Test_double">Test Doubles</a> to stand in for the three dependency functions. If possible, <a href="/2019/03/25/an-example-of-state-based-testing-in-f">I prefer state-based testing</a> over <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">interaction-based testing</a>. First, then, we need some Fakes. </p> <p> While <code>completeRegistrationWorkflow</code> takes three dependency functions, it looks as though there's only two architectural dependencies: <ul> <li>A two-factor authentication service</li> <li>A registration database (or service)</li> </ul> Defining a Fake two-factor authentication object is the most complicated of the two, but still manageable: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Fake2FA&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;proofs&nbsp;=&nbsp;Map.empty &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;_.CreateProof&nbsp;mobile&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Map.tryFind&nbsp;mobile&nbsp;proofs&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;(proofId,&nbsp;_)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;proofId&nbsp;=&nbsp;ProofId&nbsp;(Guid.NewGuid&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proofs&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;Map.add&nbsp;mobile&nbsp;(proofId,&nbsp;<span style="color:blue;">false</span>)&nbsp;proofs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:blue;">fun</span>&nbsp;proofId&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;proofId&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;_.VerifyProof&nbsp;mobile&nbsp;proofId&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Map.tryFind&nbsp;mobile&nbsp;proofs&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;(_,&nbsp;<span style="color:blue;">true</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:blue;">fun</span>&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;b&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;_.VerifyMobile&nbsp;mobile&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Map.tryFind&nbsp;mobile&nbsp;proofs&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;(proofId,&nbsp;_)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proofs&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;Map.add&nbsp;mobile&nbsp;(proofId,&nbsp;<span style="color:blue;">true</span>)&nbsp;proofs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;()</pre> </p> <p> In F#, I find that the easiest way to model a mutable resource is to use an object. This one just keeps track of a collection of proofs. The <code>CreateProof</code> method fits the function signature of <code>completeRegistrationWorkflow</code>'s <code>createProof</code> function argument. It looks for an existing proof for the mobile number so that it can reuse the same proof multiple times. If there's no proof for <code>mobile</code>, it creates a new <code>Guid</code> and returns it after having first added it to the collection. </p> <p> Likewise, the <code>VerifyProof</code> method fits the type of the <code>verifyProof</code> function argument. Proofs are actually tuples of IDs and a flag that keeps track of whether or not they've been verified. The method returns the flag if it's there, and <code>false</code> otherwise. </p> <p> The third <code>VerifyMobile</code> method is a test-specific functionality that enables a test to mark a proof as having been verified via two-factor authentication. </p> <p> Compared to <code>Fake2FA</code>, the Fake registration database is simple: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;FakeRegistrationDB&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;Collection&lt;Registration&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.CompleteRegistration&nbsp;r&nbsp;=&nbsp;async&nbsp;{&nbsp;this.Add&nbsp;r&nbsp;}</pre> </p> <p> Again, the <code>CompleteRegistration</code> method fits the <code>completeRegistration</code> function argument to <code>completeRegistrationWorkflow</code>. It just makes the inherited <code>Add</code> method <code>Async</code>. </p> <h3 id="8dbbe25d331f4517b3fe8ace6e95ffa9"> Fixture creation <a href="#8dbbe25d331f4517b3fe8ace6e95ffa9" title="permalink">#</a> </h3> <p> My plan is to add <a href="https://en.wikipedia.org/wiki/Characterization_test">Characterisation Tests</a> so that I can refactor. I do, however, plan to change the API of the System Under Test (SUT). This could break the tests, which would defy their purpose. To protect against this, I'll test against a <a href="https://en.wikipedia.org/wiki/Facade_pattern">Facade</a>. Initially, this Facade will be equivalent to the <code>completeRegistrationWorkflow</code> function, but this will change as I refactor. </p> <p> In addition to the SUT Facade, the tests will also need access to the 'injected' dependencies. You can address this by creating a <a href="/2009/03/16/FixtureObject">Fixture Object</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;createFixture&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;twoFA&nbsp;=&nbsp;Fake2FA&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeRegistrationDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.VerifyProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.CompleteRegistration &nbsp;&nbsp;&nbsp;&nbsp;sut,&nbsp;twoFA,&nbsp;db</pre> </p> <p> This function return a triple of values: the SUT Facade and the two Fakes. </p> <p> The SUT Facade is a partially applied function of the type <code>ProofId option -&gt; Registration -&gt; Async&lt;CompleteRegistrationResult&gt;</code>. In other words, it abstracts away the specifics about how impure actions are executed. It seems reasonable to imagine that the two remaining input arguments, <code>ProofId option</code> and <code> Registration</code>, are run-time values. Regardless of refactoring, the resulting function should be able to receive those arguments and produce the desired outcome. </p> <h3 id="a6dbde952b53422992ae006bdc305053"> Characterising the missing proof ID case <a href="#a6dbde952b53422992ae006bdc305053" title="permalink">#</a> </h3> <p> It looks like the <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of <code>completeRegistrationWorkflow</code> is <em>3</em>, so you're going to need three Characterisation Tests. You can add them in any order you like, but in this case I found it natural to follow the order in which the branches are laid out in the SUT. </p> <p> This test case verifies what happens if the proof ID is missing: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData&nbsp;123&gt;] [&lt;InlineData&nbsp;432&gt;] <span style="color:blue;">let</span>&nbsp;``Missing&nbsp;proof&nbsp;ID``&nbsp;mobile&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut,&nbsp;twoFA,&nbsp;db&nbsp;=&nbsp;createFixture&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;r&nbsp;=&nbsp;{&nbsp;Mobile&nbsp;=&nbsp;Mobile&nbsp;mobile&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;actual&nbsp;=&nbsp;sut&nbsp;None&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;expectedProofId&nbsp;=&nbsp;twoFA.CreateProof&nbsp;r.Mobile &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;ProofRequired&nbsp;expectedProofId &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;Seq.isEmpty&nbsp;db&nbsp;@&gt;&nbsp;}</pre> </p> <p> All the tests in this article use <a href="https://xunit.net">xUnit.net</a> 2.4.0 with <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> 5.0.0. </p> <p> This test calls the <code>sut</code> Facade with a <code>None</code> proof ID and an arbitrary <code>Registration</code> <code>r</code>. Had I used a <a href="/property-based-testing-intro">property-based testing</a> framework such as <a href="https://fscheck.github.io/FsCheck">FsCheck</a> or <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a>, I could have made the <code>Registration</code> value itself an arbitrary test argument, but I thought that this was overkill for this situation. </p> <p> In order to figure out the <code>expectedProofId</code>, the test relies on the behaviour of the <code>Fake2FA</code> class. The <code>CreateProof</code> method is <a href="https://en.wikipedia.org/wiki/Idempotence">idempotent</a>, so calling it several times with the same number should return the same proof. In this test case, we expect the <code>sut</code> to have done so already, so calling the method once more from the test should return the same value that the SUT received. The test then wraps the proof ID in the <code>ProofRequired</code> case and uses Unquote's <code>=!</code> (<em>must equal</em>) operator to verify that <code>expected</code> is equal to <code>actual</code>. </p> <p> Finally, the test also verifies that the reservations database remains empty. </p> <p> Since this is a Characterisation Test it already passes, <a href="/2013/04/02/why-trust-tests">which makes it untrustworthy</a>. How do I know that I didn't write a <a href="/2019/10/14/tautological-assertion">Tautological Assertion</a>? </p> <p> When I write Characterisation Tests, I always try to change the SUT to verify that the test fails for the appropriate reason. In order to fail the first assertion, I can make this change to the <code>None</code> branch of the SUT: </p> <p> <pre><span style="color:blue;">match</span>&nbsp;proofId&nbsp;<span style="color:blue;">with</span> |&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//let!&nbsp;proofId&nbsp;=&nbsp;createProof&nbsp;registration.Mobile</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;proofId&nbsp;=&nbsp;ProofId&nbsp;(Guid.NewGuid&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId</pre> </p> <p> This fails the <code>expected =! actual</code> assertion, as expected. </p> <p> Likewise, you can fail the second assertion with this change: </p> <p> <pre><span style="color:blue;">match</span>&nbsp;proofId&nbsp;<span style="color:blue;">with</span> |&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;completeRegistration&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;proofId&nbsp;=&nbsp;createProof&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId</pre> </p> <p> The addition of the <code>completeRegistration</code> statement causes the <code>test &lt;@ Seq.isEmpty db @&gt;</code> assertion to fail, again as expected. </p> <p> Now I trust that test. </p> <h3 id="a9dceb6d72af4d06bc46bae83464b201"> Characterising the valid proof ID case <a href="#a9dceb6d72af4d06bc46bae83464b201" title="permalink">#</a> </h3> <p> Next, you have the case where all is good. The proof ID is present and valid. You can characterise the behaviour with this test: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData&nbsp;987&gt;] [&lt;InlineData&nbsp;247&gt;] <span style="color:blue;">let</span>&nbsp;``Valid&nbsp;proof&nbsp;ID``&nbsp;mobile&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut,&nbsp;twoFA,&nbsp;db&nbsp;=&nbsp;createFixture&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;r&nbsp;=&nbsp;{&nbsp;Mobile&nbsp;=&nbsp;Mobile&nbsp;mobile&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;p&nbsp;=&nbsp;twoFA.CreateProof&nbsp;r.Mobile &nbsp;&nbsp;&nbsp;&nbsp;twoFA.VerifyMobile&nbsp;r.Mobile &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;actual&nbsp;=&nbsp;sut&nbsp;(Some&nbsp;p)&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;RegistrationCompleted&nbsp;=!&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;Seq.contains&nbsp;r&nbsp;db&nbsp;@&gt;&nbsp;}</pre> </p> <p> This test uses <code>CreateProof</code> to create a proof before the <code>sut</code> is exercised. It also uses the test-specific <code>VerifyMobile</code> method to mark the mobile number (and thereby the proof) as valid. </p> <p> Again, there's two assertions: one against the return value <code>actual</code>, and one that verifies that the registration database <code>db</code> now contains the registration <code>r</code>. </p> <p> As before, you can't trust a Characterisation Test before you've seen it fail, so first edit the <code>isValid</code> branch of the SUT like this: </p> <p> <pre><span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;completeRegistration&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//return&nbsp;RegistrationCompleted</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId</pre> </p> <p> This fails the <code>RegistrationCompleted =! actual</code> assertion, as expected. </p> <p> Now make this change: </p> <p> <pre><span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//do!&nbsp;completeRegistration&nbsp;registration</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;RegistrationCompleted</pre> </p> <p> Now the <code>test &lt;@ Seq.contains r db @&gt;</code> assertion fails, as expected. </p> <p> This test also seems trustworthy. </p> <h3 id="a4f44c3575914d628931c88095df477e"> Characterising the invalid proof ID case <a href="#a4f44c3575914d628931c88095df477e" title="permalink">#</a> </h3> <p> The final test case is when a proof ID exists, but it's invalid: </p> <p> <pre>[&lt;Theory&gt;] [&lt;InlineData&nbsp;327&gt;] [&lt;InlineData&nbsp;666&gt;] <span style="color:blue;">let</span>&nbsp;``Invalid&nbsp;proof&nbsp;ID``&nbsp;mobile&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut,&nbsp;twoFA,&nbsp;db&nbsp;=&nbsp;createFixture&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;r&nbsp;=&nbsp;{&nbsp;Mobile&nbsp;=&nbsp;Mobile&nbsp;mobile&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;p&nbsp;=&nbsp;twoFA.CreateProof&nbsp;r.Mobile &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;actual&nbsp;=&nbsp;sut&nbsp;(Some&nbsp;p)&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;expectedProofId&nbsp;=&nbsp;twoFA.CreateProof&nbsp;r.Mobile &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;ProofRequired&nbsp;expectedProofId &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;Seq.isEmpty&nbsp;db&nbsp;@&gt;&nbsp;}</pre> </p> <p> The <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">arrange phase</a> of the test is comparable to the previous test case. The only difference is that the new test <em>doesn't</em> invoke <code>twoFA.VerifyMobile r.Mobile</code>. This leaves the generated proof ID <code>p</code> invalid. </p> <p> The assertions, on the other hand, are identical to those of the <code>Missing proof ID</code> test case, which means that you can make the same edits to the <code>else</code> branch as you can to the <code>None</code> branch, as described above. If you do that, the assertions fail as they're supposed to. You can also trust this Characterisation Test. </p> <h3 id="3f733ce502814d458395b3561c63b897"> Eta expansion <a href="#3f733ce502814d458395b3561c63b897" title="permalink">#</a> </h3> <p> While I want to keep the SUT Facade's type unchanged, I do want change the way I compose it. The goal is an impure/pure/impure sandwich: Do something impure first, then call a pure function with the data obtained, and finally do something impure with the output of the pure function. </p> <p> This means that the composition is going to manipulate the input values to the SUT Facade. To make that easier, I perform an <em>eta conversion</em> on the <code>sut</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;createFixture&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;twoFA&nbsp;=&nbsp;Fake2FA&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeRegistrationDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.VerifyProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.CompleteRegistration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;sut,&nbsp;twoFA,&nbsp;db</pre> </p> <p> This doesn't change the behaviour or how the SUT is composed. It only makes the <code>pid</code> and <code>r</code> arguments explicitly visible. </p> <h3 id="9afbddfce5e14b3c98435a9d2e3f6848"> Move proof verification <a href="#9afbddfce5e14b3c98435a9d2e3f6848" title="permalink">#</a> </h3> <p> When you consider the current implementation of <code>completeRegistrationWorkflow</code>, it seems that the impure actions are interleaved with the decision-making code. How to separate them? </p> <p> The first opportunity that I identified was that it always calls <code>verifyProof</code> in the <code>Some</code> case. Whenever you want to call a method only in the <code>Some</code> case, but not in the <code>None</code> case, it suggest <code>Option.map</code>. </p> <p> It should be possible to run <code>Option.map (twoFA.VerifyProof r.Mobile) pid</code> as the initial impure action of the impure/pure/impure sandwich. If that's possible, we could pass the output of that pure function as an argument to <code>completeRegistrationWorkflow</code>. That would already make it simpler: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;(createProof:&nbsp;Mobile&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;ProofId&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(completeRegistration:&nbsp;Registration&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;unit&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(proof:&nbsp;bool&nbsp;option) &nbsp;&nbsp;&nbsp;&nbsp;(registration:&nbsp;Registration) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Async&lt;CompleteRegistrationResult&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proof&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;proofId&nbsp;=&nbsp;createProof&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;isValid&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;completeRegistration&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;RegistrationCompleted &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;proofId&nbsp;=&nbsp;createProof&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Notice that by changing the <code>proof</code> argument to a <code>bool option</code>, you no longer need to call <code>verifyProof</code>, so you can remove it. </p> <p> There's just one problem. The result of <code>Option.map (twoFA.VerifyProof r.Mobile) pid</code> is an <code>Option&lt;Async&lt;bool&gt;&gt;</code>, but you need an <code>Option&lt;bool&gt;</code>. </p> <p> You can compose the SUT Facade in an asynchronous workflow, and use a <code>let!</code> binding, but that's not going to solve the problem. A <code>let!</code> binding only works when the outer container is <code>Async</code>. Here, the outermost container is <code>Option</code>. You're going to need to flip the containers around so that you get an <code>Async&lt;Option&lt;bool&gt;&gt;</code> that you can <code>let!</code>-bind: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;p&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Option.map&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;b&#39;&nbsp;=&nbsp;b &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Some&nbsp;b&#39;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;None&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.CompleteRegistration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> By pattern-matching on <code>Option.map (twoFA.VerifyProof r.Mobile) pid</code>, you can return one of two alternative asynchronous workflows. </p> <p> Due to the <code>let!</code> binding, <code>p</code> is a <code>bool option</code> that you can pass to <code>completeRegistrationWorkflow</code>. </p> <h3 id="a271fa42747a4271a5420951763d3559"> Traversal <a href="#a271fa42747a4271a5420951763d3559" title="permalink">#</a> </h3> <p> I know what you're going to say. You'll protest that I just moved complex behaviour out of <code>completeRegistrationWorkflow</code>. The implied assumption here is that <code>completeRegistrationWorkflow</code> is the top-level behaviour that you'd compose in a <a href="/2011/07/28/CompositionRoot">Composition Root</a>. The <code>createFixture</code> function plays that role in this refactoring exercise. </p> <p> You'd normally view the Composition Root as a <a href="http://xunitpatterns.com/Humble%20Object.html">Humble Object</a> - an object that we accept isn't covered by tests because it has a cyclomatic complexity of one. This is no longer the case. </p> <p> The conversion of <code>Option&lt;Async&lt;bool&gt;&gt;</code> to <code>Async&lt;Option&lt;bool&gt;&gt;</code> is, however, a well-known operation. In <a href="https://www.haskell.org">Haskell</a> this is known as a <em>traversal</em>, and it's a completely generic operation: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;&#39;a&nbsp;option&nbsp;-&gt;&nbsp;Async&lt;&#39;b&nbsp;option&gt;</span> <span style="color:blue;">let</span>&nbsp;traverse&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;f&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Some&nbsp;x&#39;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;None&nbsp;}</pre> </p> <p> You can put this function in a general-purpose module called <code>AsyncOption</code> and cover it by unit tests if you will. You can even put this module in a separate library; it's perfectly decoupled from the the specifics of the registration flow domain. </p> <p> If you do that, <code>completeRegistrationWorkflow</code> doesn't change, but the composition does: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;p&nbsp;=&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.CompleteRegistration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> You're now back where you'd like to be: One impure action produces a value that you can pass to another function. There's no explicit branching in the code. The cyclomatic complexity remains one. </p> <h3 id="58e1461e9e304afd8c491f94150ebd35"> Change return type <a href="#58e1461e9e304afd8c491f94150ebd35" title="permalink">#</a> </h3> <p> That first refactoring takes care of one out of three impure dependencies. Next, you can get rid of <code>createProof</code>. This one seems to be more difficult to get rid of. It doesn't seem to be required only in the <code>Some</code> case, so a <code>map</code> or <code>traverse</code> can't work. In both cases, however, the result of calling <code>createProof</code> is handled in exactly the same way. </p> <p> Here's another common trick in functional programming: <a href="/2016/09/26/decoupling-decisions-from-effects">Decouple decisions from effects</a>. Return a value that indicates the decision that the function reaches, and then let the second impure action of the impure/pure/impure sandwich act on the decision. </p> <p> In this case, you can model your decision as a <code>Mobile option</code>. You might want to consider a more explicit type, in order to better communicate intent, but it's best to keep each refactoring step small: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;(completeRegistration:&nbsp;Registration&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;unit&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(proof:&nbsp;bool&nbsp;option) &nbsp;&nbsp;&nbsp;&nbsp;(registration:&nbsp;Registration) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Async&lt;Mobile&nbsp;option&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proof&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;Some&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;isValid&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;completeRegistration&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Some&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Notice that the <code>createProof</code> dependency is no longer required. I've removed it from the argument list of <code>completeRegistrationWorkflow</code>. </p> <p> The composition now looks like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;createFixture&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;twoFA&nbsp;=&nbsp;Fake2FA&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeRegistrationDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;p&nbsp;=&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;res&nbsp;=&nbsp;completeRegistrationWorkflow&nbsp;db.CompleteRegistration&nbsp;p&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;pidr&nbsp;=&nbsp;AsyncOption.traverse&nbsp;twoFA.CreateProof&nbsp;res &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;pidr &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.map&nbsp;ProofRequired &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.defaultValue&nbsp;RegistrationCompleted&nbsp;}</pre> </p> <p> Thanks to the <code>let!</code> binding, the result <code>res</code> is a <code>Mobile option</code>. You can now let the <code>twoFA.CreateProof</code> method <code>traverse</code> over <code>res</code>. This produces an <code>Async&lt;Option&lt;ProofId&gt;&gt;</code> that you can <code>let!</code>-bind to <code>pidr</code> - a <code>ProofId option</code>. </p> <p> You can use <code>Option.map</code> to wrap the <code>ProofId</code> value in a <code>ProofRequired</code> case, if it's there. This step of the final pipeline produces a <code>CompleteRegistrationResult option</code>. </p> <p> Finally, you can use <code>Option.defaultValue</code> to fold the <code>option</code> into a <code>CompleteRegistrationResult</code>. The default value is <code>RegistrationCompleted</code>. This is the case value that'll be used if the <code>option</code> is <code>None</code>. </p> <p> Again, the composition has a cyclomatic complexity of one, and the type of the <code>sut</code> remains <code>ProofId option -&gt; Registration -&gt; Async&lt;CompleteRegistrationResult&gt;</code>. This is a true refactoring. The type of the SUT remains the same, and no behaviour changes. The tests still pass, even though I haven't had to edit them. </p> <h3 id="6550df202542434e85937da702901cd1"> Change return type to Result <a href="#6550df202542434e85937da702901cd1" title="permalink">#</a> </h3> <p> Consider the intent of <code>completeRegistrationWorkflow</code>. The purpose of the operation is to <em>complete</em> a registration workflow. The name is quite explicit. Thus, the <em>happy path</em> is when the proof ID is valid and the function can call <code>completeRegistration</code>. </p> <p> Usually, when you call a function that returns an <code>option</code>, the implied contract is that the <code>Some</code> case represents the happy path. That's not the case here. The <code>Some</code> case carries information about the error paths. This isn't <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a>. </p> <p> It'd be more appropriate to use a <code>Result</code> return value: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;(completeRegistration:&nbsp;Registration&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;unit&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(proof:&nbsp;bool&nbsp;option) &nbsp;&nbsp;&nbsp;&nbsp;(registration:&nbsp;Registration) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Async&lt;Result&lt;unit,&nbsp;Mobile&gt;&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proof&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;Error&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;isValid&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;completeRegistration&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Error&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> This change is in itself small, but it does require some changes to the composition. Just as you had to add an <code>Option.traverse</code> function when the return type was an <code>option</code>, you'll now have to add similar functionality to <code>Result</code>. <em>Result</em> is also known as <a href="/2018/06/11/church-encoded-either">Either</a>. Not only <a href="/2019/01/07/either-bifunctor">is it a bifunctor</a>, you can also traverse both axes. Haskell calls this a <code>bitraversable</code> functor. </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;(&#39;c&nbsp;-&gt;&nbsp;Async&lt;&#39;d&gt;)&nbsp;-&gt;&nbsp;Result&lt;&#39;a,&#39;c&gt;&nbsp;-&gt;&nbsp;Async&lt;Result&lt;&#39;b,&#39;d&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;traverseBoth&nbsp;f&nbsp;g&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Ok&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;f&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok&nbsp;x&#39;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Error&nbsp;e&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;e&#39;&nbsp;=&nbsp;g&nbsp;e &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Error&nbsp;e&#39;&nbsp;}</pre> </p> <p> Here I just decided to call the function <code>traverseBoth</code> and the module <code>AsyncResult</code>. </p> <p> You're also going to need the equivalent of <code>Option.defaultValue</code> for <code>Result</code>. Something that translates both dimensions of <code>Result</code> into the same type. That's the <a href="/2019/06/03/either-catamorphism">Either catamorphism</a>, so you could, for example, introduce another general-purpose function called <code>cata</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Result&lt;&#39;a,&#39;c&gt;&nbsp;-&gt;&nbsp;&#39;b</span> <span style="color:blue;">let</span>&nbsp;cata&nbsp;f&nbsp;g&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Ok&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Error&nbsp;e&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;g&nbsp;e</pre> </p> <p> This is another entirely general-purpose function that you can put in a general-purpose module called <code>Result</code>, in a general-purpose library. You can also cover it by unit tests, if you like. </p> <p> These two general-purpose functions enable you to compose the workflow: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;createFixture&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;twoFA&nbsp;=&nbsp;Fake2FA&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeRegistrationDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;p&nbsp;=&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;res&nbsp;=&nbsp;completeRegistrationWorkflow&nbsp;db.CompleteRegistration&nbsp;p&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;pidr&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AsyncResult.traverseBoth &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;()&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;pidr &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Result.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;sut,&nbsp;twoFA,&nbsp;db</pre> </p> <p> This looks more confused than previous iterations. From here, though, it'll get better again. The first two lines of code are the same as before, but now <code>res</code> is a <code>Result&lt;unit, Mobile&gt;</code>. You still need to let <code>twoFA.CreateProof</code> traverse the 'error path', but now you also need to take care of the happy path. </p> <p> In the <code>Ok</code> case you have a <code>unit</code> value (<code>()</code>), but <code>traverseBoth</code> expects its <code>f</code> and <code>g</code> functions to return <code>Async</code> values. I could have fixed that with a more specialised <code>traverseError</code> function, but we'll soon move on from here, so it's hardly worthwhile. </p> <p> In Haskell, you can 'elevate' a value simply with the <code>pure</code> function, but in F#, you need the more cumbersome <code>(fun () -&gt; async { return () })</code> to achieve the same effect. </p> <p> The traversal produces <code>pidr</code> (for <em>Proof ID Result</em>) - a <code>Result&lt;unit, ProofId&gt;</code> value. </p> <p> Finally, it uses <code>Result.cata</code> to turn both the <code>Ok</code> and <code>Error</code> dimensions into a single <code>CompleteRegistrationResult</code> that can be returned. </p> <h3 id="d1c0adc81bc241c7a7a2ea9042356f24"> Removing the last dependency <a href="#d1c0adc81bc241c7a7a2ea9042356f24" title="permalink">#</a> </h3> <p> There's still one dependency left: the <code>completeRegistration</code> function, but it's now trivial to remove. Instead of calling the dependency function from within <code>completeRegistrationWorkflow</code> you can use the same trick as before. Decouple the decision from the effect. </p> <p> Return information about the decision the function made. In the above incarnation of the code, the <code>Ok</code> dimension is currently empty, since it only returns <code>unit</code>. You can use that 'channel' to communicate that you decided to complete a registration: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;(proof:&nbsp;bool&nbsp;option) &nbsp;&nbsp;&nbsp;&nbsp;(registration:&nbsp;Registration) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Async&lt;Result&lt;Registration,&nbsp;Mobile&gt;&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proof&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;Error&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;isValid&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Error&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> This is another small change. When <code>isValid</code> is <code>true</code>, the function no longer calls <code>completeRegistration</code>. Instead, it returns <code>Ok registration</code>. This means that the return type is now <code>Async&lt;Result&lt;Registration, Mobile&gt;&gt;</code>. It also means that you can remove the <code>completeRegistration</code> function argument. </p> <p> In order to compose this variation, you need one new general-purpose function. Perhaps you find this barrage of general-purpose functions exhausting, but it's an artefact of a design philosophy of the F# language. The F# base library contains only few general-purpose functions. Contrast this with <a href="https://en.wikipedia.org/wiki/Glasgow_Haskell_Compiler">GHC</a>'s <a href="http://hackage.haskell.org/package/base">base</a> library, which comes with all of these functions built in. </p> <p> The new function is like <code>Result.cata</code>, but over <code>Async&lt;Result&lt;_&gt;&gt;</code>. </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Async&lt;Result&lt;&#39;a,&#39;c&gt;&gt;&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;cata&nbsp;f&nbsp;g&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;r&#39;&nbsp;=&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Result.cata&nbsp;f&nbsp;g&nbsp;r&#39;&nbsp;}</pre> </p> <p> Since this function does conceptually the same as <code>Result.cata</code> I decided to retain the name <code>cata</code> and just put it in the <code>AsyncResult</code> module. (This may not be strictly correct, as I haven't really given a lot of thought to what a catamorphism for <code>Async</code> would look like, if one exists. I'm open to suggestions about better naming. After all, <code>cata</code> is hardly an idiomatic F# name.) </p> <p> With <code>AsyncResult.cata</code> you can now compose the system: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;p&nbsp;=&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;res&nbsp;=&nbsp;completeRegistrationWorkflow&nbsp;p&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.traverseBoth&nbsp;db.CompleteRegistration&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Not only did the call to <code>completeRegistrationWorkflow</code> get even simpler, but you also now avoid the awkwardly named <code>pidr</code> value. Thanks to the <code>let!</code> binding, <code>res</code> has the type <code>Result&lt;Registration, Mobile&gt;</code>. </p> <p> Note that you can now let both impure actions (<code>db.CompleteRegistration</code> and <code>twoFA.CreateProof</code>) traverse the result. This step produces an <code>Async&lt;Result&lt;unit, ProofId&gt;&gt;</code> that's immediately piped to <code>AsyncResult.cata</code>. This reduces the two alternative dimensions of the <code>Result</code> to a single <code>Async&lt;CompleteRegistrationResult&gt;</code> value. </p> <p> The <code>completeRegistrationWorkflow</code> function now begs to be further simplified. </p> <h3 id="db6e044e55f749ba8794d7a8f74e02f4"> Pure registration workflow <a href="#db6e044e55f749ba8794d7a8f74e02f4" title="permalink">#</a> </h3> <p> <a href="/2019/02/11/asynchronous-injection">Once you remove all dependencies, your domain logic doesn't have to be asynchronous</a>. Nothing asynchronous happens in <code>completeRegistrationWorkflow</code>, so simplify it: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;(proof:&nbsp;bool&nbsp;option) &nbsp;&nbsp;&nbsp;&nbsp;(registration:&nbsp;Registration) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Result&lt;Registration,&nbsp;Mobile&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proof&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;isValid&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span>&nbsp;Ok&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;Error&nbsp;registration.Mobile</pre> </p> <p> Gone is the <code>async</code> computation expression, including the <code>return</code> keyword. This is now a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>. </p> <p> You'll have to adjust the composition once more, but it's only a minor change: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;p&nbsp;=&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;completeRegistrationWorkflow&nbsp;p&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.traverseBoth&nbsp;db.CompleteRegistration&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> The result of invoking <code>completeRegistrationWorkflow</code> is no longer an <code>Async</code> value, so there's no reason to <code>let!</code>-bind it. Instead, you can call it and immediately pipe its output to <code>AsyncResult.traverseBoth</code>. </p> <h3 id="9a4c3af0f30843c2816af97a08b2f99b"> DRY <a href="#9a4c3af0f30843c2816af97a08b2f99b" title="permalink">#</a> </h3> <p> Consider <code>completeRegistrationWorkflow</code>. Can you make it simpler? </p> <p> At this point it should be evident that two of the branches contain duplicate code. Applying the <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY principle</a> you can simplify it: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;(proof:&nbsp;bool&nbsp;option) &nbsp;&nbsp;&nbsp;&nbsp;(registration:&nbsp;Registration) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Result&lt;Registration,&nbsp;Mobile&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proof&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;<span style="color:blue;">true</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;registration.Mobile</pre> </p> <p> I'm not too fond of this style of type annotation for simple functions like this, so I'd like to remove it: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow&nbsp;proof&nbsp;registration&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proof&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;<span style="color:blue;">true</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;registration.Mobile</pre> </p> <p> These two steps are pure refactorings: they only reorganise the code that implements <code>completeRegistrationWorkflow</code>, so the composition doesn't change. </p> <h3 id="705c34900d6342f3a79356734a67b355"> Essential complexity <a href="#705c34900d6342f3a79356734a67b355" title="permalink">#</a> </h3> <p> While reading this article, you may have felt frustration gather. <em>This is cheating! You took out all of the complexity. Now there's nothing left!</em> You're likely to feel that I've moved a lot of behaviour into untestable code. I've done nothing of the sort. </p> <p> I'll remind you that while functions like <code>AsyncOption.traverse</code> and <code>AsyncResult.cata</code> do contain branching behaviour, they <em>can</em> be tested. In fact, <a href="/2015/05/07/functional-design-is-intrinsically-testable">since they're pure functions, they're intrinsically testable</a>. </p> <p> It's true that a <em>composition</em> of a pure function with its impure dependencies may not be (unit) testable, but that's also true for a Dependency Injection-based object graph composed in a Composition Root. </p> <p> Compositions of functions may look non-trivial, but to a degree, the type system will assist you. If your composition compiles, it's likely that you've composed the impure/pure/impure sandwich correctly. </p> <p> Did I take out all the complexity? I didn't. There's a bit left; the function now has a cyclomatic complexity of <em>two</em>. If you look at the original function, you'll see that <em>the duplication was there all along</em>. Once you remove all the accidental complexity, you uncover the essential complexity. This happens to me so often when I apply functional programming principles that <a href="/2019/07/01/yes-silver-bullet">I fancy that functional programming is a silver bullet</a>. </p> <h3 id="0086592a037947e397169271eeaad627"> Pipeline composition <a href="#0086592a037947e397169271eeaad627" title="permalink">#</a> </h3> <p> We're mostly done now. The problem now appears in all its simplicity, and you have an impure/pure/impure sandwich. </p> <p> You can still improve the code, though. </p> <p> If you consider the current composition, you may find that <code>p</code> isn't the best variable name. I admit that I struggled with naming that variable. <a href="/2016/10/25/when-variable-names-are-in-the-way">Sometimes, variable names are in the way</a> and the code might be clearer if you could elide them by composing a pipeline of functions. </p> <p> That's always worth an attempt. This time, ultimately I find that it doesn't improve things, but even an attempt can be illustrative. </p> <p> If you want to eliminate a named value, you can often do so by piping the output of the function that produced the variable directly to the next function. This does, however, require that the function argument is the right-most. Currently, that's not the case. <code>registration</code> is right-most, and <code>proof</code> is to the left. </p> <p> There's no compelling reason that the arguments should come in that order, so flip them: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow&nbsp;registration&nbsp;proof&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proof&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;<span style="color:blue;">true</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;registration.Mobile</pre> </p> <p> This enables you to write the entire composition as a single pipeline: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Async.map&nbsp;(completeRegistrationWorkflow&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Async.bind&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AsyncResult.traverseBoth&nbsp;db.CompleteRegistration&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;AsyncResult.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired) &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> This does, however, call for two new general-purpose functions: <code>Async.map</code> and <code>Async.bind</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;x&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f&nbsp;x&#39;&nbsp;} <span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;bind&nbsp;f&nbsp;x&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;f&nbsp;x&#39;&nbsp;}</pre> </p> <p> In my opinion, these functions ought to belong to F#'s <code>Async</code> module, but for <a href="https://github.com/fsharp/fslang-suggestions/issues/318">for reasons that aren't clear to me, they don't</a>. As you can see, though, they're easy to add. </p> <p> While the this change gets rid of the <code>p</code> variable, I don't think it makes the overall composition easier to understand. The action of swapping the function arguments does, however, enable another simplification. </p> <h3 id="db99963569414669a865d4d10ad95b6e"> Eta reduction <a href="#db99963569414669a865d4d10ad95b6e" title="permalink">#</a> </h3> <p> Now that <code>proof</code> is <code>completeRegistrationWorkflow</code>'s last function argument, you can perform an <em>eta reduction:</em> </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow&nbsp;registration&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;<span style="color:blue;">true</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;registration.Mobile</pre> </p> <p> Not everyone is a fan of the <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free style</a>, but I like it. YMMV. </p> <h3 id="798d1bb566224090a676d386afc54ea4"> Sandwich <a href="#798d1bb566224090a676d386afc54ea4" title="permalink">#</a> </h3> <p> Regardless of whether you prefer <code>completeRegistrationWorkflow</code> in point-free or pointed style, I think that the composition needs improvement. It should explicitly communicate that it's an impure/pure/impure sandwich. This makes it necessary to reintroduce some variables, so I'm also going to bite the bullet and devise some better names. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;validityOfProof&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;decision&nbsp;=&nbsp;completeRegistrationWorkflow&nbsp;r&nbsp;validityOfProof &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decision &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.traverseBoth&nbsp;db.CompleteRegistration&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Instead of <code>p</code>, I decided to call the first value <code>validityOfProof</code>. This is the result of the first impure action in the sandwich (the upper slice of bread). </p> <p> While <code>validityOfProof</code> is the result of an impure action, the value itself is pure and can be used as input to <code>completeRegistrationWorkflow</code>. This is the pure part of the sandwich. I called the output <code>decision</code> because the workflow makes a decision based on its input, and it's up to the caller to act on that decision. </p> <p> Notice that <code>decision</code> is bound with a <code>let</code> binding (instead of a <code>let!</code> binding), despite taking place inside an <code>async</code> workflow. This is because <code>completeRegistrationWorkflow</code> is pure. It doesn't return an <code>Async</code> value. </p> <p> The second impure action acts on <code>decision</code> through a pipeline of <code>AsyncResult.traverseBoth</code> and <code>AsyncResult.cata</code>, as previously explained. </p> <p> I think that the impure/pure/impure sandwich is more visible like this, so that was my final edit. I'm happy with how it looks now. </p> <h3 id="a4c98d81322e4010a0dfcb1c59955812"> Conclusion <a href="#a4c98d81322e4010a0dfcb1c59955812" title="permalink">#</a> </h3> <p> I don't claim that you can always refactor code to an impure/pure/impure sandwich. In fact, <a href="/2017/07/10/pure-interactions">I can easily envision categories of software where such an architecture seems impossible</a>. </p> <p> Still, I find it intriguing that when I find myself in the realm of web services or message-based applications, I can't recall a case where a sandwich has been impossible. Surely, there must cases where it is so. That's the reason that I solicit examples. This article was a response to such an example. I found it fruitful, because it enabled me to discuss several useful techniques for composing behaviour in a functional architecture. On the other hand, it failed to be a counter-example. </p> <p> I'm sure that some readers are left with a nagging doubt. <em>That's all very impressive, but would you actually write code like that in a piece of production software?</em> </p> <p> If it was up to me, then: <em>yes.</em> I find that when I can keep code pure, it's trivial to unit test and there's no test-induced damage. Functions also compose in a way objects don't easily do, so there's many advantages to functional programming. I'll take them when they're available. </p> <p> As always, context matters. I've been in team settings where other team members would embrace this style of programming, and in other environments where team members wouldn't understand what was going on. In the latter case, I'd adjust my approach to challenge, not alienate, other team members. </p> <p> My intention with this article was to show what's <em>possible</em>, not to dictate what you should do. That's up to you. </p> <p> This article is the December 2 entry in the <a href="https://sergeytihon.com/2019/11/05/f-advent-calendar-in-english-2019">F# Advent Calendar in English 2019</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="7c05edb624b54cafacc204e60b42bbf3"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#7c05edb624b54cafacc204e60b42bbf3">#</a></div> <div class="comment-content"> <p>Thank you so much for the comprehensive reply to my comment. It was very instructive to see refactoring process, from thought to code. The post is an excellent reply to the question I asked.</p> <h3>A slight modification</h3> <p>In my original comment, I made one simplification that, in hindsight, I perhaps should not have made. It is not critical, but it complicates things slightly. In reality, the <code>completeRegistration</code> function does not return <code>Async&lt;unit&gt;</code>, but <code>Async&lt;Result&lt;unit, CompleteRegistrationError&gt;&gt;</code> (where, currently, <code>CompleteRegistrationError</code> has the single case <code>UserExists</code>, returned if the DB throws a unique constraint error).</p> <p>As I see it, the impact of this to your refactoring is two-fold:</p> <ul> <li>You can&#39;t easily use <code>AsyncResult.traverseBoth</code>, since the signatures between the two cases aren&#39;t compatible (unless you want to mess around with nested <code>Result</code> values). You could write a custom <code>traverse</code> function just for the needed signature, but then we’ve traveled well into the lands of “generic does not imply general”.</li> <li>It might be better to model the registration result (completed vs. proof required) as its own DU, with <code>Result</code> being reserved for actual errors.</li> </ul> <h3>Evaluating the refactoring</h3> <p>My original comment ended in the following question (emphasis added):</p> <blockquote><p>Is it possible to refactor this to direct input/output, <strong>in a way that actually reduces complexity where it matters?</strong></p> </blockquote> <p>With this (vague) question and the above modifications in mind, let&#39;s look at the relevant code before/after. In both cases, there are two functions: The workflow/logic, and the composition.</p> <h4>Before</h4> <p>Before refactoring, we have a slightly complex impure workflow (which still is fairly easily testable using state-based testing, as you so aptly demonstrated) – note the <code>asyncResult</code> CE (I’m using the excellent FsToolkit.ErrorHandling, if anyone wonders) and the updated signatures; otherwise it’s the same:</p> <pre><code class='language-f#' lang='f#'>let completeRegistrationWorkflow (createProof: Mobile -&gt; Async&lt;ProofId&gt;) (verifyProof: Mobile -&gt; ProofId -&gt; Async&lt;bool&gt;) (completeRegistration: Registration -&gt; Async&lt;Result&lt;unit, CompleteRegistrationError&gt;&gt;) (proofId: ProofId option) (registration: Registration) : Async&lt;Result&lt;CompleteRegistrationResult, CompleteRegistrationError&gt;&gt; = asyncResult { match proofId with | None -&gt; let! proofId = createProof registration.Mobile return ProofRequired proofId | Some proofId -&gt; let! isValid = verifyProof registration.Mobile proofId if isValid then do! completeRegistration registration return RegistrationCompleted else let! proofId = createProof registration.Mobile return ProofRequired proofId } </code></pre> <p>Secondly, we have the trivial &quot;humble object&quot; composition, which looks like this:</p> <pre><code class='language-f#' lang='f#'>let complete proofId validReg = Workflows.Registration.complete Http.createMobileClaimProof Http.verifyMobileClaimProof Db.completeRegistration proofId validReg </code></pre> <p>The composition is, indeed, humble – the only thing it does is call the higher-order workflow function with the correct parameters. It has no cyclomatic complexity and is trivial to read, and I don&#39;t think anyone would consider it necessary to test.</p> <h4>After</h4> <p>After refactoring, we have the almost trivial pure function we extracted (for simplicity I let it return <code>Result</code> here, as you proposed):</p> <pre><code class='language-f#' lang='f#'>let completePure reg proofValidity = match proofValidity with | Some true -&gt; Ok reg | Some false | None -&gt; Error reg.Mobile </code></pre> <p>Secondly, we have the composition function. Now, with the modification to <code>completeRegistration</code> (returning <code>Async&lt;Result&lt;_,_&gt;&gt;</code>), it can&#39;t as easily be written in point-free style. You might certainly be able to improve it, but here is my quick initial take.</p> <pre><code class='language-f#' lang='f#'>let complete proofId reg : Async&lt;Result&lt;CompleteRegistrationResult, CompleteRegistrationError&gt;&gt; = asyncResult { let! proofValidity = proofId |&gt; Option.traverseAsync (Http.verifyMobileClaimProof reg.Mobile) match completePure reg proofValidity with | Ok reg -&gt; do! Db.completeRegistration reg return RegistrationCompleted | Error mobile -&gt; let! proofId = Http.createMobileClaimProof mobile return ProofRequired proofId } </code></pre> <h4>Evaluation</h4> <p>Now that we have presented the code before/after, let us take stock of what we have gained and lost by the refactoring.</p> <p>Pros:</p> <ul> <li>We have gotten rid of the &quot;DI workflow&quot; entirely</li> <li>More of the logic is pure</li> </ul> <p>Cons:</p> <ul> <li>The logic we extracted to a pure function is almost trivial. This is not in itself bad, but one can wonder whether it was worth it (apart from the purely instructive aspects).</li> <li>If the extracted logic is pure, where then did the rest of the complexity go? The only place it could – it ended up in the &quot;composition&quot;, i.e. the &quot;humble object&quot;. The composition function isn&#39;t just calling a higher-order function with the correct function arguments any more; it has higher cyclomatic complexity and is much harder to read, and can&#39;t be easily tested (since it&#39;s a composition function). The new composition is, so to say, quite a bit less humble than the original composition. This is particularly evident in my updated version, but personally I also have to look at your simpler(?), point-free version a couple of times to convince myself that it is, really, not doing anything wrong. (Though regardless of whether a function is written point-free or not, it does the exact same thing and has the same complexity.)</li> <li>To the point above: The composition function needs many &quot;complex&quot; helper functions that would likely confuse, if not outright alienate beginner F# devs (which could, for example, lead to worse onboarding). This is particularly relevant for non-standard functions like <code>AsyncOption.traverse</code>, <code>AsyncResult.traverseBoth</code>, <code>AsyncResult.cata</code>, etc.</li> </ul> <p>Returning to my initial question: Does the refactoring “reduce complexity where it matters?“ I’m not sure. This is (at least partly) “personal opinions” territory, of course, and my vague question doesn’t help. But personally I find the result of the refactoring more complex to understand than the original, DI workflow-based version.</p> <p>Based on Scott Wlaschin’s book Domain Modelling Made Functional, it’s possible he might agree. He seems very fond of the “DI workflow” approach there. I personally prefer a bit more dependency rejection than that, because I find “DR”/sandwiches often leads to simpler code, but in this particular case, I may prefer the impure DI workflow, tested using state-based testing. At least for the more complex code I described, but perhaps also for your original example.</p> <p>Still, I truly appreciate your taking the time to respond in this manner. It was very instructive, as always, which was after all the point. And you’re welcome to share any insights regarding this comment, too.</p> </div> <div class="comment-date">2019-12-03 13:46 UTC</div> </div> <div class="comment" id="e90332adb7d24e2b8aa1484c302b6f8c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e90332adb7d24e2b8aa1484c302b6f8c">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. This is great! One of your comments inspires me to compose another article that I've long wanted to write. If I manage to produce it in time, I'll publish it Monday. Once that's done, I'll respond here in a more thorough manner. </p> <p> When I do that, however, I don't plan to reproduce your updated example, or address it in detail. I see nothing in it that invalidates what I've already written. As far as I can tell, you don't need to explicitly pattern-match on <code>completePure reg proofValidity</code>. You should be able to map or traverse over it like already shown. If you want my help with the details, I'll be happy to do so, but then please prepare a <a href="https://en.wikipedia.org/wiki/Minimal_working_example">minimal working example</a> like I did for this article. You can either fork <a href="https://github.com/ploeh/RegistrationFlow">my example</a> or make a new repository. </p> </div> <div class="comment-date">2019-12-04 8:35 UTC</div> </div> <div class="comment" id="785e708f61b14ad0825d1359cbebd8a2"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#785e708f61b14ad0825d1359cbebd8a2">#</a></div> <div class="comment-content"> <p> This is a fantastic post Mark! Thank you very much for going step-by-step while explaining how you refactored this code. </p> <blockquote> I find it intriguing that when I find myself in the realm of web services or message-based applications, I can't recall a case where a [impure/pure/impure] sandwich has been impossible. Surely, there must cases where it is so. That's the reason that I solicit examples. </blockquote> <p> I would like to suggest a example in the realm of web services or message-based applications that cannot be expressed as a impure/pure/impure sandwich. </p> <p> Let's call an "impure/pure/impure sandwich" an impure-pure-impure composition. More generally, any impure funciton can be expressed as a composition of the form <code>[pure-]impure(-pure-impure)*[-pure]</code>. That is, (1) it might begin with a pure step, then (2) there is an impure step, then (3) there is a sequence of length zero or more containing a pure step followed by an impure step, and lastly (4) it might end with another pure step. One reason an impure fucntion might intentially be expressed by a composition that ends with a pure step is to erase senitive informaiton form the memory hierarchy. For simplicity though, let's assume that any impure function can be refactored so that the corresponding composition ends with an impure step. Let the length of a composition be one plus the number of dashes (<code>-</code>) that it contains. </p> <p> Suppose <code>f</code> is a function with an impure-pure-impure composition such that <code>f</code> cannot be refactored to a fucntion with a composition of a smaller length. Then there exists fucntion <code>f'</code> with a pure-impure-pure-impure composition. The construction uses public-key cryptography. I think this is a natural and practical example. </p> <p> Here is the definition of <code>f'</code> in words. The user sends to the server ciphertext encryped using the server's public key. The user's request is received by a process that already has the server's private key loaded into memory. This process decrypts the user's ciphertext using its private key to obtain some plantext <code>p</code>. This step is pure. Then the process passes <code>p</code> into <code>f</code>. </p> <p> Using symmetric-key cryptography, it is possible to construct a function with a composition of an arbitrarily high length. The following construction reminds me of how <a href="https://en.wikipedia.org/wiki/Onion_routing">union routing</a> works (though each decryption in that case is intended to happen in a different process on a different server). I admit that this example is not very natural or practical. </p> <p> Suppose <code>f</code> is a function with a composition of length <code>n</code>. Then there exists fucntion <code>f'</code> with a composition of length greater than <code>n</code>. Specifically, if the original composition starts with a pure step, then the length is larger by one; if the original composition starts with an impure step, then the length is larger by two. </p> <p> Here is the definition of <code>f'</code> in words. The user sends to the server an ID and ciphertext encryped using a symmetric key that corresponds to the ID. The user's request is received by a process that does not have any keys loaded into memory. First, this process obtains from disk the appropriate symmetric key using the ID. This step is impure. Then this process decrypts the user's ciphertext using this key to obtain some plantext <code>p</code>. This step is pure. Then the process passes <code>p</code> into <code>f</code>. </p> </div> <div class="comment-date">2019-12-06 17:21 UTC</div> </div> <div class="comment" id="0ae83af0cc824c848e5988eb2fb35356"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0ae83af0cc824c848e5988eb2fb35356">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Unfortunately, I don't follow your chain of reasoning. Cryptography strikes me as fitting the impure/pure/impure sandwich architecture quite well. There's definitely an initial impure step because you have to initialise a random number generator, as well as load keys, salts, and whatnot from storage. From there, though, the cryptographic algorithms are, as far as I'm aware, pure calculation. I don't see how asymmetric cryptography changes that. </p> <p> The reason that I'm soliciting examples that defy the impure/pure/impure sandwich architecture, however, is that I'm looking for a compelling example. What to do when the sandwich architecture is impossible is a frequently asked question. To be clear, I know what to do in that situation, but I'd like to write an article that answers the question in a compelling way. For that, I need an example that an uninitiated reader can follow. </p> </div> <div class="comment-date">2019-12-07 10:24 UTC</div> </div> <div class="comment" id="e53d31d11b814bccac392dfc0bc03230"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#e53d31d11b814bccac392dfc0bc03230">#</a></div> <div class="comment-content"> <p> Sorry that my explanation was unclear. I should have included an example. </p> <blockquote> Cryptography strikes me as fitting the impure/pure/impure sandwich architecture quite well. There's definitely an initial impure step because you have to initialise a random number generator, as well as load keys, salts, and whatnot from storage. From there, though, the cryptographic algorithms are, as far as I'm aware, pure calculation. I don't see how asymmetric cryptography changes that. </blockquote> <p> I agree that cyptographic algorithms are pure. From your qoute that I included above, I get the impression that you have neglected to consider what computation is to be done with the output of the cyptogrpahic algorithm. </p> <p> Here is a specific example of my first construction, which uses public-key cyptography. Consider the function <code>sut</code> that concluded your post. I repeat it for clarity. </p> <p><pre><span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;validityOfProof&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;decision&nbsp;=&nbsp;completeRegistrationWorkflow&nbsp;r&nbsp;validityOfProof &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decision &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.traverseBoth&nbsp;db.CompleteRegistration&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Let <code>sut</code> be the function <code>f</code> in that construction. In particular, <code>sut</code> is an impure/pure/impure sandwich, or equivalently an impure-pure-impure composition that of course has length 3. Furthermore, I think it is clear that this behavior cannot be expressed as a pure-impure-pure composition, a pure-impure composition, an impure-pure composition, or an impure composition. You worked very hard to simplfy that code, and I believe an implicit claim of yours is that it cannot be simplified any further. </p> <p> In this case, <code>f'</code> would be the following function. </p> <p><pre><span style="color:blue;">let</span>&nbsp;privateKey&nbsp;=&nbsp;... <span style="color:blue;">let</span>&nbsp;sut'&nbsp;ciphertext&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(pid,&nbsp;r)&nbsp;=&nbsp;decrypt&nbsp;privateKey&nbsp;ciphertext &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;validityOfProof&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;decision&nbsp;=&nbsp;completeRegistrationWorkflow&nbsp;r&nbsp;validityOfProof &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decision &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.traverseBoth&nbsp;db.CompleteRegistration&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> As I defined it, this function is a pure-impure-pure-impure composition, which has length 4. Maybe in your jargon you would call this a pure/impure/pure/impure sandwich. My claim is that this function cannot be refactored into an impure/pure/impure sandwich. </p> <p> Do you think that my claim is correct? </p> </div> <div class="comment-date">2019-12-07 14:48 UTC</div> </div> <div class="comment" id="189069ae41704900b56403288656a8fe"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#189069ae41704900b56403288656a8fe">#</a></div> <div class="comment-content"> <p> Tyson, thank you for your patience with me. Now I get it. As stated, your composition looks like a pure-impure-pure-impure composition, but unless you hard-code <code>privateKey</code>, you'll have to load that value, which is an impure operation. That would make it an impure-pure-impure-pure-impure composition. </p> <p> The decryption step itself is an impure-pure composition, assuming that we need to load keys, salts, etc. from persistent storage. You might also want to think of it as a 'mostly' pure function, since you could probably load decryption keys once when the application process starts, and keep them around for its entire lifetime. </p> <p> It's a correct example of a more involved interaction model. Thank you for supplying it. Unfortunately, it's not one I can use for an article. Like other cross-cutting concerns like caching, logging, retry mechanisms, etcetera, security can be abstracted away as middleware. This implies that you'd have a middleware action that's implemented as an impure-pure-impure sandwich, and an application feature that's implemented as another impure-pure-impure sandwich. These two sandwiches are unrelated. A change to one of them is unlikely to trigger a change in the other. Thus, we can still base our application architecture on the notion of the impure-pure-impure sandwich. </p> <p> I hope I've explained my demurral in a sensible way. </p> </div> <div class="comment-date">2019-12-08 13:24 UTC</div> </div> <div class="comment" id="c7b6630ebec24c0fad9b5821a0802878"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#c7b6630ebec24c0fad9b5821a0802878">#</a></div> <div class="comment-content"> <blockquote> This implies that you'd have a middleware action that's implemented as an impure-pure-impure sandwich, and an application feature that's implemented as another impure-pure-impure sandwich. These two sandwiches are unrelated. A change to one of them is unlikely to trigger a change in the other. </blockquote> <p> The are unrelated semantically. Syntatically, the whole application sandwich is the last piece of impure bread on the middleware sandwich. This reminds me of a thought I have had and also heard recently, which is that the structure of code is like a fractal. </p> <p> Anyway, I am hearing you say that you want functions to have "one responsibility", to do "one thing", to change for "one reason". With that constraint satisfied, you are requesting an example of a funciton that is not an impure/pure/impure sandwich. I am up to that challenge. Here is another attempt. </p> <p> Suppose our job is to implement a <a href="https://en.wikipedia.org/wiki/Man-in-the-middle_attack">man-in-the-middle attack</a> in the style of <a href="https://www.schneier.com/blog/archives/2011/06/man-in-the-midd_3.html">Schneier's Chess Grandmaster Problem</a> in which Alice and Bob know that they are communicating with Malory while Malory simply repeats what she hears to the other person. Specifically, Alice is a client and Bob is a server. Mailory acts like a server to Alice and like a client to Bob. The funciton would look something like this. </p> <p><pre><span style="color:blue;">let</span>&nbsp;malroyInTheMiddle&nbsp;aliceToMalory&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;maloryToBob&nbsp;=&nbsp;convertIncoming&nbsp;aliceToMalory &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;bobToMalory&nbsp;=&nbsp;service&nbsp;maloryToBob &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;maloryToAlice&nbsp;=&nbsp;convertOutgoing&nbsp;bobToMalory &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;maloryToAlice }</pre></p> <p> This is a pure-impure-pure composition, which is different from an impure-pure-impure composition. </p> </div> <div class="comment-date">2019-12-09 14:28 UTC</div> </div> <div class="comment" id="f2db5259c16e4241b53934ae4dcb17a0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f2db5259c16e4241b53934ae4dcb17a0">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. The way I understand it, we are to assume that both <code>convertIncoming</code> and <code>convertOutgoing</code> are complicated functions that require substantial testing to get right. Under that assumption, I think that you're right. This doesn't directly fit the impure-pure-impure sandwich architecture. </p> <p> It does, however, fit a simple function composition. As far as I can see, it's equivalent to something like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;malroyInTheMiddle&nbsp;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;Async.fromResult &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;Async.map&nbsp;convertIncoming &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;Async.bind&nbsp;service &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;Async.map&nbsp;convertOutgoing</pre> </p> <p> I haven't tested it, but I'd imagine it to be something like that. </p> <p> To nitpick, this isn't a pure-impure-pure composition, but rather an impure-pure-impure-pure-impure composition. The entry point of a system is always impure, as is the output. </p> </div> <div class="comment-date">2019-12-10 11:29 UTC</div> </div> <div class="comment" id="fea1498fe43543f29a01eb6101dbdb9f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fea1498fe43543f29a01eb6101dbdb9f">#</a></div> <div class="comment-content"> <p> Christer, I'd hoped that I'd already addressed some of your concerns in the article itself, but I may not have done a good enough job of it. Overall, given a question like <blockquote> "Is it possible to refactor this to direct input/output, in a way that actually reduces complexity where it matters?" </blockquote> I tend to put emphasis on <em>is it possible</em>. Not that the rest of the question is unimportant, but it's more subjective. Perhaps you find that my article didn't answer your question, but I hope at least that I managed to establish that, yes, it's possible to refactor to an impure-pure-impure sandwich. </p> <p> Does it matter? I think it does, but that's subjective. I do think, though, that I can objectively say that <a href="/2018/11/19/functional-architecture-a-definition">my refactoring is functional</a>, whereas <a href="/2017/01/30/partial-application-is-dependency-injection">passing impure functions as arguments isn't</a>. Whether or not an architecture ought to be functional is, again, subjective. No-one says that it has to be. That's up to you. </p> <p> As I wrote in <a href="#e90332adb7d24e2b8aa1484c302b6f8c">my preliminary response</a>, I'm not going to address your modification. I don't see that it matters. Even when you return an <code>Async&lt;Result&lt;_,_&gt;&gt;</code> you can <code>map</code>, <code>bind</code>, or <code>traverse</code> over it. You may not be able to use <code>AsyncResult.traverseBoth</code>, but you can derive specialisations like <code>AsyncResult.traverseOk</code> and <code>AsyncResult.traverseError</code>. </p> <p> First, like you did, I find it illustrative to juxtapose the alternatives. I'm going to use the original example first: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;(createProof:&nbsp;Mobile&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;ProofId&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(verifyProof:&nbsp;Mobile&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ProofId&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;bool&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(completeRegistration:&nbsp;Registration&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Async&lt;unit&gt;) &nbsp;&nbsp;&nbsp;&nbsp;(proofId:&nbsp;ProofId&nbsp;option) &nbsp;&nbsp;&nbsp;&nbsp;(registration:&nbsp;Registration) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Async&lt;CompleteRegistrationResult&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;proofId&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;proofId&nbsp;=&nbsp;createProof&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;proofId&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;isValid&nbsp;=&nbsp;verifyProof&nbsp;registration.Mobile&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;isValid&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;completeRegistration&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;RegistrationCompleted &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;proofId&nbsp;=&nbsp;createProof&nbsp;registration.Mobile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ProofRequired&nbsp;proofId &nbsp;&nbsp;&nbsp;&nbsp;} <span style="color:blue;">let</span>&nbsp;sut&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;completeRegistrationWorkflow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twoFA.VerifyProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.CompleteRegistration</pre> </p> <p> In contrast, here's my refactoring: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;completeRegistrationWorkflow&nbsp;registration&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;<span style="color:blue;">true</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;registration &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;registration.Mobile <span style="color:blue;">let</span>&nbsp;sut&nbsp;pid&nbsp;r&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;validityOfProof&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AsyncOption.traverse&nbsp;(twoFA.VerifyProof&nbsp;r.Mobile)&nbsp;pid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;decision&nbsp;=&nbsp;completeRegistrationWorkflow&nbsp;r&nbsp;validityOfProof &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decision &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.traverseBoth&nbsp;db.CompleteRegistration&nbsp;twoFA.CreateProof &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;AsyncResult.cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;RegistrationCompleted)&nbsp;ProofRequired &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> It's true that my composition (<code>sut</code>) seems more involved than yours, but the overall trade-off looks good to me. In total, the code is simpler. </p> <p> In your <em>evaluation</em>, you make some claims that I'd like to specifically address. Most are reasonable, but I think a few require special attention: <blockquote> "The logic we extracted to a pure function is almost trivial." </blockquote> Indeed. This should be listed as a <em>pro</em>, not a <em>con</em>. Whether or not it's worth it is a different discussion. </p> <p> As I wrote in the article: <blockquote> "If you look at the original function, you'll see that <em>the duplication was there all along</em>. Once you remove all the accidental complexity, you uncover the essential complexity." </blockquote> (Emphasis from the original.) Isn't it always worth it to take away accidental complexity? <blockquote> "The composition function isn't just calling a higher-order function with the correct function arguments any more; it has higher cyclomatic complexity" </blockquote> <a href="/2019/12/09/put-cyclomatic-complexity-to-good-use/#de927bfcc95d410bbfcd0adf7a63926b">No, it doesn't</a>. It has a cyclomatic complexity of <em>1</em>, exactly like the original humble object. <blockquote> "can't be easily tested" </blockquote> True, but neither can the original humble object. <blockquote> "The new composition is, so to say, quite a bit less humble than the original composition." </blockquote> According to which criterion? It has the same cyclomatic complexity, but I admit that more characters went into typing it. On the other hand, the composition juxtaposed with the actual function has far fewer characters than the original example. </p> <p> You also write that <blockquote> "The composition function needs many "complex" helper functions [...]. This is particularly relevant for non-standard functions like <code>AsyncOption.traverse</code>, <code>AsyncResult.traverseBoth</code>, <code>AsyncResult.cata</code>, etc." </blockquote> I don't like the epithet <em>non-standard</em>. It's true that these functions aren't in <code>FSharp.Core</code>, for reasons that aren't clear to me. In comparison, they're part of the standard <code>base</code> library in Haskell. </p> <p> There's nothing non-standard about functions like these. Like <code>map</code> and <code>bind</code>, traversals and catamorphisms are <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstractions</a>. They exist independently of particular programming languages or library packages. </p> <p> I think that it's fair criticism that they may not be friendly to absolute beginners, but they're still fairly basic ideas that address real needs. The same can be said for the <code>asyncResult</code> computation expression that you've decided to use. It's also 'non-standard' only in the sense that it's not part of <code>FSharp.Core</code>, but otherwise standard in that it's just a stack of monads, and plenty of libraries supply that functionality. You can also write that computation expression yourself in a dozen lines of code. </p> <p> In the end, all of this is subjective. As I also wrote in my conclusion: <blockquote> <p> "I've been in team settings where [...] team members wouldn't understand what was going on. In the latter case, I'd adjust my approach to challenge, not alienate, other team members. </p> <p> "My intention with this article was to show what's possible, not to dictate what you should do." </p> </blockquote> What I do, however, think is important to realise is that what I suggest is to learn a set of concepts <em>once</em>. Once you understand <a href="/2018/03/22/functors">functors</a>, monads, traversals etcetera, that's knowledge that applies to F#, Haskell, C#, JavaScript (I suppose) and so on. </p> <p> Personally, I find it a better investment of my time to learn a general concept once, and then work with trivial code, rather than having to learn, over and over again, how to deal with each new code base's accidental complexity. </p> </div> <div class="comment-date">2019-12-12 2:56 UTC</div> </div> <div class="comment" id="e0c12e5a6148400aac256cf3b800ff4f"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#e0c12e5a6148400aac256cf3b800ff4f">#</a></div> <div class="comment-content"> <p>Mark, thank you for getting back to me with a detailed response.</p> <p>First, a general remark. I see that my comment might have been a bit &quot;sharp around the edges” and phrased somewhat carelessly, giving the impression that I was not happy with your treatment of my example. I’d just like to clearly state that I am. You replied in your usual clear manner to exactly the question I posed, and seeing your process and solution was instructive for me.</p> <p>We are all learning, all the time, and if I use a strong voice, that is primarily because <a href='https://blog.codinghorror.com/strong-opinions-weakly-held/'>strong opinions, weakly held</a> often seems to be a fruitful way to drive discussion and learning.</p> <p>With that in mind, allow me to address some of your remarks and possibly soften my previous comment.</p> <blockquote><p>Perhaps you find that my article didn&#39;t answer your question, but I hope at least that I managed to establish that, yes, it&#39;s possible to refactor to an impure-pure-impure sandwich.</p> </blockquote> <p>Your article did indeed answer my question. My takeaway (then, not necessarily now after reading the rest of your comment) was that you managed to refactor to impure-pure-impure at the &quot;expense” of making the non-pure part harder to understand. But as you say, that’s subjective, and your remarks on that later in your comment was a good point of reflection for me. I’ll get to that later.</p> <blockquote><p>Does it matter? I think it does, but that&#39;s subjective. I do think, though, that I can objectively say that <a href='https://blog.ploeh.dk/2018/11/19/functional-architecture-a-definition'>my refactoring is functional</a>, whereas <a href='https://blog.ploeh.dk/2017/01/30/partial-application-is-dependency-injection'>passing impure functions as arguments isn&#39;t</a>. Whether or not an architecture ought to be functional is, again, subjective. No-one says that it has to be. That&#39;s up to you.</p> </blockquote> <p>I agree on all points.</p> <blockquote><p>First, like you did, I find it illustrative to juxtapose the alternatives.</p> </blockquote> <p>I don’t agree 100% that it’s a completely fair comparison, since you’re leaving out the implementations of <code>AsyncOption.traverse</code>, <code>AsyncResult.traverseBoth</code>, and <code>AsyncResult.cata</code>. However, I get why you are doing it. These are generic utility functions for universal concepts that, as you say later in your comment, you “learn once”. In that respect, it’s fair to leave them out. My only issue with it is that since F# doesn’t have higher-kinded types, these utility functions have to be specific to the monads and monad stacks in use. I originally thought this made such functions less understandable and less useful, but after reading the rest of your comment, I’m not sure they are. More on that below.</p> <blockquote><p>In your <em>evaluation</em></p> </blockquote> <p>(Emphasis yours.) Just in case: “Evaluation” might have been a poor choice of words. I hope you did not take it to mean that I was a teacher grading a student’s test. This was not in any way intended personally (e.g. evaluating &quot;<em>your</em> solution”). I was merely looking to sum up my subjective opinions about the refactoring.</p> <blockquote><p>Isn&#39;t it always worth it to take away accidental complexity?</p> </blockquote> <p>I find it hard to say an unequivocal &quot;yes” to such general statements. Ultimately it depends on the specific context and tradeoffs involved. If the context is “a codebase to be used for onboarding new F# devs” and the tradeoffs are “use generic helper functions to traverse bifunctors in a stack of monads”, then I’m not sure. (It <em>may</em> still be, but it’s certainly not a given.)</p> <p>But generally, though I haven’t reflected deeply on this, I’m sure you’re right that it’s worthwhile to always take away accidental complexity.</p> <blockquote><p><a href='https://blog.ploeh.dk/2019/12/09/put-cyclomatic-complexity-to-good-use/#de927bfcc95d410bbfcd0adf7a63926b'>No, it doesn&#39;t</a>. It has a cyclomatic complexity of <em>1</em>, exactly like the original humble object.</p> </blockquote> <p>You’re right. Thank you for the clarifying article on cyclomatic complexity.</p> <blockquote><blockquote><p>can&#39;t be easily tested</p> </blockquote> <p>True, but neither can the original humble object.</p> </blockquote> <p>That’s correct, but my point was that the original composition was trivial (just calling a “DI function” with the correct arguments/dependencies) and didn’t need to be tested, whereas the refactored composition does more and might warrant testing (at least to a larger degree than the original).</p> <p>This raises an interesting point. It seems (subjectively to me based on what I’ve read) to be a general consensus that a function can be left untested (is “humble”, so to speak) as long as it consists of just generic helpers, like the refactored composition. That &quot;if it compiles, it works”. This is not a general truth, since for some signatures there may exist several transformations from the input type to the output type, where the output value is different for the different transformations. I have come across such cases, and even had bugs because I used the wrong transformation. Which is why I said:</p> <blockquote><blockquote><p>The new composition is, so to say, quite a bit less humble than the original composition.</p> </blockquote> <p>According to which criterion?</p> </blockquote> <p>It is more complex in the sense that it doesn’t just call a function with the correct dependencies. The original composition is more or less immediately recognizable as correct. The refactored composition, as I said, required me to look at it more carefully to convince myself that it was correct. (I will grant that this is to some extent subjective, though.)</p> <blockquote><p>I don&#39;t like the epithet <em>non-standard</em>. It&#39;s true that these functions aren&#39;t in <code>FSharp.Core</code>, for reasons that aren&#39;t clear to me. In comparison, they&#39;re part of the standard <code>base</code> library in Haskell.</p> <p>There&#39;s nothing non-standard about functions like these. Like <code>map</code> and <code>bind</code>, traversals and catamorphisms are <a href='https://blog.ploeh.dk/2017/10/04/from-design-patterns-to-category-theory'>universal abstractions</a>. They exist independently of particular programming languages or library packages. </p> <p>I think that it&#39;s fair criticism that they may not be friendly to absolute beginners, but they&#39;re still fairly basic ideas that address real needs.</p> <p>…</p> <p>What I do, however, think is important to realise is that what I suggest is to learn a set of concepts <em>once</em>. Once you understand <a href='https://blog.ploeh.dk/2018/03/22/functors'>functors</a>, monads, traversals etcetera, that&#39;s knowledge that applies to F#, Haskell, C#, JavaScript (I suppose) and so on.</p> <p>Personally, I find it a better investment of my time to learn a general concept once, and then work with trivial code, rather than having to learn, over and over again, how to deal with each new code base&#39;s accidental complexity.</p> </blockquote> <p>This is the primary point of reflection for me in your comment. While I frequently use monads and monad stacks (particularly <code>Async&lt;Result&lt;_,_&gt;&gt;</code>) and often write utility code to transform when needed (e.g. <code>List.traverseResult</code>), I try to limit the number of such custom utility functions. Why? I’m not sure, actually. It may very well have to do with my work environment, where for a long time I have been the only F# dev and I don’t want to alienate the other .NET devs before they even get started with F#.</p> <p>In light of your comment, perhaps F# devs are doing others a disservice if we limit our use of important, general concepts like functors, monads, traversals etc.? Then again, there’s certainly a balance to be struck. I got started (and thrilled) with F# by reading <a href='https://fsharpforfunandprofit.com/'>F# for fun and profit</a> and learning about algebraic types, the concise syntax, &quot;railway-oriented programming” etc. If my first glimpse of F# had instead been <code>AsyncSeq.traverseAsyncResultOption</code>, then I might never have left the warm embrace of C#.</p> <p>I might check out <a href='http://fsprojects.github.io/FSharpPlus/'>FSharpPlus</a>, which seems to make this kind of programming easier. I have previously steered away from that library because I deemed it “too complex” (c.f. my remarks about alienating coworkers), but it might be time to reconsider. If you have tried it, I would love to hear your thoughts on it in some form or another, though admittedly that isn’t directly related to the topic at hand.</p> </div> <div class="comment-date">2019-12-12 9:04 UTC</div> </div> <div class="comment" id="477da2bf6ca04dc2ac478811cd77435e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#477da2bf6ca04dc2ac478811cd77435e">#</a></div> <div class="comment-content"> <p> Christer, don't worry about the tone of the debate. I'm not in the least offended or vexed. On the contrary, I find this a valuable discussion, and I'm glad that we're having it in a medium where it's also visible to other people. </p> <p> I think that we're gravitating towards consensus. I definitely agree that the changes I suggest aren't beginner-friendly. </p> <p> People sometimes ask me for advice on how to get started with functional programming, and I always tell .NET developers to start with F#. It's a friendly language that enables everyone to learn gradually. If you already know C# (or Visual Basic .NET) the only thing you need to learn about F# is some syntax. Then you can write object-oriented F#. As you learn new functional concepts, you can gradually change the way you write F# code. That's what I did. </p> <p> I agree with your reservations about onboarding and beginner-friendliness. When that's a concern, I wouldn't write the F# code like I suggested either. </p> <p> For a more sophisticated team, however, I feel that my suggestions are improvements that matter. I grant you that the composition seems more convoluted, but I consider the overall trade-off beneficial. In the <a href="https://www.infoq.com/presentations/Simple-Made-Easy">terminology suggested by Rich Hickey</a>, it may not be easier, bit it's simpler. </p> <p> I have no experience with FSharpPlus or any similar libraries. I usually just add the monad stacks and functions to my code base on an as-needed basis. As we've seen here, such functions are mostly useful to compose other functions, so they rarely need to be exported as part of a code base's surface area. </p> </div> <div class="comment-date">2019-12-12 15:02 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. TimeSpan configuration values in .NET Core https://blog.ploeh.dk/2019/11/25/timespan-configuration-values-in-net-core 2019-11-25T07:04:00+00:00 Mark Seemann <div id="post"> <p> <em>You can use a standard string format for TimeSpan values in configuration files.</em> </p> <p> Sometimes you need to make <code>TimeSpan</code> values configurable. I often see configuration files that look like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;SeatingDurationInSeconds&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;9000&quot;</span> }</pre> </p> <p> Code can read such values from configuration files like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seatingDuration</span>&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.<span style="color:#74531f;">FromSeconds</span>(Configuration.<span style="font-weight:bold;color:#74531f;">GetValue</span>&lt;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;SeatingDurationInSeconds&quot;</span>));</pre> </p> <p> This works, but is abstruse. How long is 9000 seconds? </p> <p> The <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> configuration file format for .NET Core is JSON, which even prevents you from adding comments. Had the configuration been in XML, at least you could have added a comment: </p> <p> <pre><span style="color:blue;">&lt;!--</span><span style="color:green;">9000&nbsp;seconds&nbsp;=&nbsp;2½&nbsp;hours</span><span style="color:blue;">--&gt;</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">SeatingDurationInSeconds</span><span style="color:blue;">&gt;</span>9000<span style="color:blue;">&lt;/</span><span style="color:#a31515;">SeatingDurationInSeconds</span><span style="color:blue;">&gt;</span></pre> </p> <p> In this case, however, it doesn't matter. Use the <a href="https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings">standard TimeSpan string representation</a> instead: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;SeatingDuration&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2:30:00&quot;</span> }</pre> </p> <p> Code can read the value like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">seatingDuration</span>&nbsp;=&nbsp;Configuration.<span style="font-weight:bold;color:#74531f;">GetValue</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;(<span style="color:#a31515;">&quot;SeatingDuration&quot;</span>);</pre> </p> <p> I find a configuration value like <code>"2:30:00"</code> much easier to understand than <code>9000</code>, and the end result is the same. </p> <p> I haven't found this documented anywhere, but from experience I know that this capability is present in the .NET Framework, so I wondered if it was also available in .NET Core. It is. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="95e18fd2054447ab901aecc4d74c1547"> <div class="comment-author">Rex Ng <a href="#95e18fd2054447ab901aecc4d74c1547">#</a></div> <div class="comment-content"> <p>There is actually an <a href="https://en.wikipedia.org/wiki/ISO_8601#Durations">ISO 8601 standard for durations.</a></p> <p> In your example you can use <pre>{ "SeatingDuration": "P9000S" }</pre> or <pre>{ "SeatingDuration": "P2H30M" }</pre> which is also quite readable in my opinion.</p> <p>Not every JSON serializer supports it though.</p> </div> <div class="comment-date">2019-11-26 01:02 UTC</div> </div> <div class="comment" id="edb7484150934d94848fe923f0ee4a39"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#edb7484150934d94848fe923f0ee4a39">#</a></div> <div class="comment-content"> <p> Rex, thank you for writing. I was aware of the ISO standard, although I didn't know that you can use it in a .NET Core configuration file. This is clearly subjective, but I don't find that format as readable as <code>2:30:00</code>. </p> </div> <div class="comment-date">2019-11-26 6:59 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Small methods are easy to troubleshoot https://blog.ploeh.dk/2019/11/18/small-methods-are-easy-to-troubleshoot 2019-11-18T06:48:00+00:00 Mark Seemann <div id="post"> <p> <em>Write small methods. How small? Small enough that any unhandled exception is easy to troubleshoot.</em> </p> <p> Imagine that you receive a bug report. This one include a logged exception: </p> <p> <pre>System.NullReferenceException: Object reference not set to an instance of an object. at Ploeh.Samples.BookingApi.Validator.Validate(ReservationDto dto) at Ploeh.Samples.BookingApi.ReservationsController.Post(ReservationDto dto) at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) </pre> </p> <p> <em>Oh, no,</em> you think, <em>not a NullReferenceException.</em> </p> <p> If you find it hard to troubleshoot NullReferenceExceptions, you're not alone. It doesn't have to be difficult, though. Open the method at the top of the stack trace, <code>Validate</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Validator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:blue;">_</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Take a moment to consider this method in the light of the logged exception. What do you think went wrong? Which object was null? </p> <h3 id="efd0166dab0c4a85b23c624162f8da67"> Failed hypothesis <a href="#efd0166dab0c4a85b23c624162f8da67" title="permalink">#</a> </h3> <p> You may form one of a few hypotheses about which object was null. Could it be <code>dto</code>? <code>dto.Date</code>? Those are the only options I can see. </p> <p> When you encounter a bug in a production system, if at all possible, reproduce it as a unit test. </p> <p> If you think that the problem is that <code>dto.Date</code> is null, test your hypothesis in a unit test: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ValidateNullDate</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;{&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">null</span>&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Validator</span>.<span style="color:#74531f;">Validate</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">NotEmpty</span>(<span style="font-weight:bold;color:#1f377f;">actual</span>); }</pre> </p> <p> If you think that a null <code>dto.Date</code> should reproduce the above exception, you'd expect the test to fail. When you run it, however, it passes. It passes because <code>DateTime.TryParse(null, out var _)</code> returns <code>false</code>. It doesn't throw an exception. </p> <p> That's not the problem, then. </p> <p> That's okay, this sometimes happens. You form a hypothesis and fail to validate it. Reject it and move on. </p> <h3 id="e35877f1cc064a259b27046f05777807"> Validated hypothesis <a href="#e35877f1cc064a259b27046f05777807" title="permalink">#</a> </h3> <p> If the problem isn't with <code>dto.Date</code>, it must be with <code>dto</code> itself. Write a unit test to test that hypothesis: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="font-weight:bold;color:#74531f;">ValidateNullDto</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Validator</span>.<span style="color:#74531f;">Validate</span>(<span style="color:blue;">null</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">NotEmpty</span>(<span style="font-weight:bold;color:#1f377f;">actual</span>); }</pre> </p> <p> When you run this test, it does indeed fail: </p> <p> <pre>Ploeh.Samples.BookingApi.UnitTests.ValidatorTests.ValidateNullDto Duration: 6 ms Message: System.NullReferenceException : Object reference not set to an instance of an object. Stack Trace: Validator.Validate(ReservationDto dto) line 12 ValidatorTests.ValidateNullDto() line 36</pre> </p> <p> This looks like the exception included in the bug report. You can consider this as validation of your hypothesis. This test reproduces the defect. </p> <p> It's easy to address the issue: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Validate</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">dto</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;No&nbsp;reservation&nbsp;data&nbsp;supplied.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.<span style="color:#74531f;">TryParse</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:blue;">_</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">$&quot;Invalid&nbsp;date:&nbsp;</span>{<span style="font-weight:bold;color:#1f377f;">dto</span>.Date}<span style="color:#a31515;">.&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; }</pre> </p> <p> The point of this article, however, is to show that small methods are easy to troubleshoot. How you resolve the problem, once you've identified it, is up to you. </p> <h3 id="7b8e2476b4fb45a0b4f32b6b2d108724"> Ambiguity <a href="#7b8e2476b4fb45a0b4f32b6b2d108724" title="permalink">#</a> </h3> <p> Methods are usually more complex than the above example. Imagine, then, that you receive another bug report with this logged exception: </p> <p> <pre>System.NullReferenceException: Object reference not set to an instance of an object. at Ploeh.Samples.BookingApi.MaîtreD.CanAccept(IEnumerable`1 reservations, Reservation reservation) at Ploeh.Samples.BookingApi.ReservationsController.Post(ReservationDto dto) at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)</pre> </p> <p> When you open the code file for the <code>MaîtreD</code> class, this is what you see: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">capacity</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservedSeats</span>&nbsp;+=&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>.Quantity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This code throws a <code>NullReferenceException</code>, but which object is <code>null</code>? Can you identify it from the code? </p> <p> I don't think that you can. It could be <code>reservations</code> or <code>reservation</code> (or both). Without more details, you can't tell. The exception is ambiguous. </p> <p> The key to making troubleshooting easy is to increase your chances that, when exceptions are thrown, they're unambiguous. The problem with a <code>NullReferenceException</code> is that you can't tell which object was <code>null</code>. </p> <h3 id="6a46508d10734d5aaeaad9252e1f70ac"> Remove ambiguity by protecting invariants <a href="#6a46508d10734d5aaeaad9252e1f70ac" title="permalink">#</a> </h3> <p> Consider the <code>CanAccept</code> method. Clearly, it requires both <code>reservations</code> and <code>reservation</code> in order to work. This requirement is, however, currently implicit. You can make it more explicit by letting the method protect its invariants. (This is also known as <em>encapsulation</em>. For more details, watch my Pluralsight course <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID</a>.) </p> <p> A simple improvement is to add a <a href="https://en.wikipedia.org/wiki/Guard_(computer_science)">Guard Clause</a> for each argument: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="font-weight:bold;color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">reservations</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">reservations</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">reservation</span>&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>&nbsp;<span style="font-weight:bold;color:#8f08c4;">in</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservedSeats</span>&nbsp;+=&nbsp;<span style="font-weight:bold;color:#1f377f;">r</span>.Quantity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;<span style="font-weight:bold;color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> If you'd done that from the start, the logged exception would instead have been this: </p> <p> <pre>System.ArgumentNullException: Value cannot be null. Parameter name: reservations at Ploeh.Samples.BookingApi.Ma&#xEE;treD.CanAccept(IEnumerable`1 reservations, Reservation reservation) at Ploeh.Samples.BookingApi.ReservationsController.Post(ReservationDto dto) at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)</pre> </p> <p> It's now clear that it was the <code>reservations</code> argument that was <code>null</code>. Now fix that issue. Why does the <code>CanAccept</code> receive a <code>null</code> argument? </p> <p> You may now consider examining the next frame in the stack trace: <code>ReservationsController.Post</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Post</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">validationMsg</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Validator</span>.<span style="color:#74531f;">Validate</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">validationMsg</span>&nbsp;!=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BadRequest</span>(<span style="font-weight:bold;color:#1f377f;">validationMsg</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Mapper</span>.<span style="color:#74531f;">Map</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>&nbsp;=&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">accepted</span>&nbsp;=&nbsp;maîtreD.<span style="font-weight:bold;color:#74531f;">CanAccept</span>(<span style="font-weight:bold;color:#1f377f;">reservations</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="font-weight:bold;color:#1f377f;">accepted</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">StatusCode</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">StatusCodes</span>.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">id</span>&nbsp;=&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Ok</span>(<span style="font-weight:bold;color:#1f377f;">id</span>); }</pre> </p> <p> The <code>Post</code> code only calls <code>CanAccept</code> once, so again, you can unambiguously deduce where the <code>null</code> reference comes from. It comes from the <code>Repository.ReadReservations</code> method call. Now go there and figure out why it returns <code>null</code>. </p> <p> Notice how troubleshooting is trivial when the methods are small. </p> <h3 id="952d4ebb24e94cb990ab1b047c4a7140"> Size matters <a href="#952d4ebb24e94cb990ab1b047c4a7140" title="permalink">#</a> </h3> <p> How small is a small method? How large does a method have to be before it's no longer small? </p> <p> <em>A method is small when you can easily troubleshoot it based exclusively on a logged exception.</em> </p> <p> Until you get the hang of it, it can be hard to predict if a method is going to be easy to troubleshoot. I therefore also follow another rule of thumb: </p> <p> For languages like C#, <a href="/2019/11/04/the-80-24-rule">methods should have a maximum size of 80x24 characters</a>. </p> <p> What if you already have bigger methods? Those are much harder to troubleshoot. If you have a method that spans hundreds of lines of code, a logged exception is rarely sufficient for troubleshooting. In order to save space, I'm not going to show en example of such a method, but I'm sure that you can easily find one in the code base that you professionally work with. Such a method is likely to have dozens of objects that could be <code>null</code>, so a <code>NullReferenceException</code> and a stack trace will contain too little information to assist troubleshooting. </p> <p> I often see developers add tracing or logging to such code in order to facilitate troubleshooting. This makes the code even more bloated, and harder to troubleshoot. </p> <p> Instead, cut the big method into smaller helper methods. You can keep the helper methods <code>private</code> so that you don't change the API. Even so, a logged exception will now report the helper method in its stack trace. Keep those helper methods small, and troubleshooting becomes trivial. </p> <h3 id="2a34a1a9c82443d8975a22fb9da9845b"> Conclusion <a href="#2a34a1a9c82443d8975a22fb9da9845b" title="permalink">#</a> </h3> <p> Small methods come with many benefits. One of these is that it's easy to troubleshoot a small method from a logged exception. Small methods typically take few arguments, and use few variables. A logged exception will often be all that you need to pinpoint where the problem occurred. </p> <p> A large method body, on the other hand, is difficult to troubleshoot. If all you have is a logged exception, you'll be able to find multiple places in the code where that exception could have been thrown. </p> <p> Refactor big methods into smaller helper methods. If one of these helper methods throws an exception, the method name will be included in the stack trace of a logged exception. When the helper method is small, you can easily troubleshoot it. </p> <p> Keep methods small. Size does matter. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1734b6e6cd1b4898bf28c2412286f381"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#1734b6e6cd1b4898bf28c2412286f381">#</a></div> <div class="comment-content"> <p> How did you create those exception stack traces? </p> <p> Compared to your examples, I am used to seeing each stack frame line end with the source code line number. (Although, sometimes I have seen all line numbers being 0, which is equivalent to having no line numbers at all.) The exception stack trace in your unit test includes line numbers but your other exception stack traces do not. This <a href="https://stackoverflow.com/questions/4272579/how-to-print-full-stack-trace-in-exception/4272629#4272629">Stack Overflow answer</a> contains an exception stack trace like with line numbers like I am used to seeing. </p> <p> The line containing "lambda_method" also seem odd to me. As I recall, invoking a lambda expression also contributes more information to the stack trace than your examples include. Although, that information is cryptic enough that I don't remember off the top of my head what it means. (After a quick search, I didn't find a good example of how I am used to seeing lambda expressions displayed in a stack trace.) </p> <p> With this additional informaiton, the methods can be longer while reamining unambiguous (in the sense you described). </p> </div> <div class="comment-date">2019-11-18 14:57 UTC</div> </div> <div class="comment" id="8a66b203db9245d8b3db5e40399f039e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8a66b203db9245d8b3db5e40399f039e">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. IIRC, you only see line numbers for code compiled with debug information. That's why you normally don't see line numbers for .NET base class library methods, ASP.NET Core, etc. </p> <p> While you can deploy and run an application in Debug mode, I wouldn't. When you compile in Release mode, the compiler makes optimisations (such as in-lining) that it can't do when it needs to retain a map to a specific source. In Debug mode, you squander those optimisations. </p> <p> I've always deployed systems in Release mode. To make the examples realistic, I didn't include the line numbers in the stack trace, because in Release mode, they aren't going to be there. </p> <p> When you're troubleshooting by reproducing a logged exception as a unit test, on the other hand, it's natural to do so in Debug mode. </p> </div> <div class="comment-date">2019-11-18 16:39 UTC</div> </div> <div class="comment" id="e2c03feb26854496b7d0ec528ea3ae8d"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#e2c03feb26854496b7d0ec528ea3ae8d">#</a></div> <div class="comment-content"> <p> I just tried to verify this. I didn't find any difference between compiling in Debug or Release mode as well as no fundemental issue with in-lining. In all four cases (debug vs release and in-lining vs no inlining), exception stack traces still included line numbers. When in-lining, the only different was the absense of the in-lined method from the stack trace. </p> <p> Instead, I found that line numbers are included in exception stack traces if and only if the corresponding <a href="https://en.wikipedia.org/wiki/Program_database">.pdb</a> file is next to the .exe file when executed. </p> </div> <div class="comment-date">2019-11-18 19:07 UTC</div> </div> <div class="comment" id="2818246690a94b7b9c81ddfb47fc6d89"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2818246690a94b7b9c81ddfb47fc6d89">#</a></div> <div class="comment-content"> <p> Tyson, that sounds correct. To be honest, I stopped putting effort into learning advanced debugging skills a long time ago. Early in my career we debugged by writing debug statements directly to output! Then I learned test-driven development, and with that disappeared most of the need to debug full systems. At the time I began working with systems that logged unhandled exceptions, the habit of writing small methods was already so entrenched that I never felt that I needed the line numbers. </p> <p> FWIW, more than five years ago, I decided to <a href="https://github.com/AutoFixture/AutoFixture/issues/65">publish symbols with AutoFixture</a>, but I never found it useful. </p> </div> <div class="comment-date">2019-11-19 7:18 UTC</div> </div> <div class="comment" id="84d25a5ae21e4541bfcbb12009c4dfea"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#84d25a5ae21e4541bfcbb12009c4dfea">#</a></div> <div class="comment-content"> <p> <a href="https://github.com/dotnet/sdk/issues/1458#issuecomment-577283992">I recommend</a> <a href="https://github.com/elmish/Elmish.WPF/blob/a63df682f1984a5d726002f4e92437ed1f2b2acc/src/Elmish.WPF/Elmish.WPF.fsproj#L13">setting DebugType to Embedded</a> in every project file. This puts the PDB file inside the DLL. Having separate files is harmful in the same way that <a href="https://blog.ploeh.dk/2014/01/29/nuget-package-restore-considered-harmful/">NuGet package restore is harmful</a>. I don't think of this as an advanced debugging skill. It is trivial for one person to add the required line to each project. Then every future stack trace seen by any developer will include line numbers. Seems like an easy win to me. </p> </div> <div class="comment-date">2022-09-17 18:07 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Diamond rock https://blog.ploeh.dk/2019/11/11/diamond-rock 2019-11-11T09:15:00+00:00 Mark Seemann <div id="post"> <p> <em>A diamond kata implementation written in Rockstar</em> </p> <p> I've <a href="/2015/01/10/diamond-kata-with-fscheck">previously written about the diamond kata</a>, which has become one of my favourite programming exercises. I wanted to take the <a href="https://codewithrockstar.com">Rockstar</a> language out for a spin, and it seemed a good candidate problem. </p> <h3 id="ab491bf89ae945f48821fe7b324febeb"> Rockstar <a href="#ab491bf89ae945f48821fe7b324febeb" title="permalink">#</a> </h3> <p> If you're not aware of the Rockstar programming language, it started with a tweet from <a href="http://paulstovell.com">Paul Stovell</a>: <blockquote> <p> "To really confuse recruiters, someone should make a programming language called Rockstar." </p> <footer><cite><a href="https://twitter.com/paulstovell/status/1013960369465782273">Paul Stovell</a></cite></footer> </blockquote> This inspired <a href="http://www.dylanbeattie.net">Dylan Beattie</a> to create the Rockstar programming language. The language's <a href="https://codewithrockstar.com">landing page</a> already sports an <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> implementation of the <a href="http://codingdojo.org/kata/FizzBuzz">FizzBuzz kata</a>, so I had to pick another exercise. The <a href="http://claysnow.co.uk/recycling-tests-in-tdd">diamond kata</a> was a natural choice for me. </p> <h3 id="c17e0b1d17f04d19b9e965ad531f10d9"> Lyrics <a href="#c17e0b1d17f04d19b9e965ad531f10d9" title="permalink">#</a> </h3> <p> I'll start with the final result. What follows here are the final lyrics to <em>Diamond rock</em>. As you'll see, it starts with a short prologue after which it settles into a repetitive pattern. I imagine that this might make it useful for audience participation, in the anthem rock style of e.g. <a href="https://en.wikipedia.org/wiki/We_Will_Rock_You">We Will Rock You</a>. </p> <p> After 25 repetitions the lyrics change. I haven't written music to any of them, but I imagine that this essentially transitions into another song, just like <em>We Will Rock You</em> traditionally moves into <a href="https://en.wikipedia.org/wiki/We_Are_the_Champions">We Are the Champions</a>. The style of the remaining lyrics does, however, suggest something musically more complex than a rock anthem. </p> <p> <pre>Your memory says&nbsp;&nbsp; The way says A Despair takes your hope The key is nothing Your courage is a tightrope Let it be without it If your hope is your courage Let the key be the way Build your courage up If your hope is your courage The key says B Build your courage up If your hope is your courage The key says C Build your courage up If your hope is your courage The key says D Build your courage up If your hope is your courage The key says E Build your courage up If your hope is your courage The key says F Build your courage up If your hope is your courage The key says G Build your courage up If your hope is your courage The key says H Build your courage up If your hope is your courage The key says I Build your courage up If your hope is your courage The key says J Build your courage up If your hope is your courage The key says K Build your courage up If your hope is your courage The key says L Build your courage up If your hope is your courage The key says M Build your courage up If your hope is your courage The key says N Build your courage up If your hope is your courage The key says O Build your courage up If your hope is your courage The key says P Build your courage up If your hope is your courage The key says Q Build your courage up If your hope is your courage The key says R Build your courage up If your hope is your courage The key says S Build your courage up If your hope is your courage The key says T Build your courage up If your hope is your courage The key says U Build your courage up If your hope is your courage The key says V Build your courage up If your hope is your courage The key says W Build your courage up If your hope is your courage The key says X Build your courage up If your hope is your courage The key says Y Build your courage up If your hope is your courage The key says Z Give back the key Dream takes a tightrope Your hope's start'n'stop While Despair taking your hope ain't a tightrope Build your hope up Give back your hope Raindrop is a wintercrop Light is a misanthrope Let it be without raindrop Darkness is a kaleidoscope Let it be without raindrop Diamond takes your hour, love, and your day If love is the way Let your sorrow be your hour without light Put your sorrow over darkness into flight Put your memory of flight into motion Put it with love, and motion into the ocean Whisper the ocean If love ain't the way Let ray be your day of darkness without light Let your courage be your hour without darkness, and ray Put your courage over darkness into the night Put your memory of ray into action Let satisfaction be your memory of the night Let alright be satisfaction with love, action, love, and satisfaction Shout alright Listen to the wind Let flight be the wind Let your heart be Dream taking flight Let your breath be your heart of darkness with light Your hope's stop'n'start While your hope is lower than your heart Let away be Despair taking your hope Diamond taking your breath, away, and your hope Build your hope up Renown is southbound While your hope is as high as renown Let away be Despair taking your hope Diamond taking your breath, away, and your hope Knock your hope down</pre> </p> <p> Not only do these look like lyrics, but it's also an executable program! </p> <h3 id="994b9a9d46dc467383c528a17dc99f65"> Execution <a href="#994b9a9d46dc467383c528a17dc99f65" title="permalink">#</a> </h3> <p> If you want to run the program, you can copy the text and paste it into <a href="https://codewithrockstar.com/online">the web-based Rockstar interpreter</a>; that's what I did. </p> <p> When you <em>Rock!</em> the lyrics, the interpreter will prompt you for an input. There's no instructions or input validation, but <em>the only valid input is the letters</em> <code>A</code> to <code>Z</code>, and <em>only in upper case</em>. If you type anything else, I don't know what'll happen, but most likely it'll just enter an infinite loop, and you'll have to reboot your computer. </p> <p> If you input, say, <code>E</code>, the output will be the expected diamond figure: </p> <p> <pre> A B B C C D D E E D D C C B B A </pre> </p> <p> When you paste the code, be sure to include everything. There's significant whitespace in those lyrics; I'll explain later. </p> <h3 id="12f2c388f5c8499594a3bf90f37e07f4"> Readable code <a href="#12f2c388f5c8499594a3bf90f37e07f4" title="permalink">#</a> </h3> <p> As the Rockstar documentation strongly implies, <em>singability</em> is more important than readability. You can, however, write more readable Rockstar code, and that's what I started with: </p> <p> <pre>Space says&nbsp;&nbsp; LetterA says A GetLetter takes index If index is 0 Give back LetterA If index is 1 retVal says B Give back retVal If index is 2 retVal says C Give back retVal GetIndex takes letter Index is 0 While GetLetter taking Index ain't letter Build Index up Give back Index PrintLine takes width, l, lidx If l is LetterA Let completeSpaceCount be width minus 1 Let paddingCount be completeSpaceCount over 2 Let padding be Space times paddingCount Let line be padding plus l plus padding Say line Else Let internalSpaceSize be lidx times 2 minus 1 Let filler be Space times internalSpaceSize Let totalOuterPaddingSize be width minus 2, internalSpaceSize Let paddingSize be totalOuterPaddingSize over 2 Let padding be Space times paddingSize Let line be padding plus l, filler, l, padding Say line Listen to input Let idx be GetIndex taking input Let width be idx times 2 plus 1 Let counter be 0 While counter is lower than idx Let l be GetLetter taking counter PrintLine taking width, l, counter Build counter up While counter is as high as 0 Let l be GetLetter taking counter PrintLine taking width, l, counter Knock counter down</pre> </p> <p> This prototype only handled the input letters <code>A</code>, <code>B</code>, and <code>C</code>, but it was enough to verify that the algorithm worked. I've done the diamond kata several times before, so I only had to find the most imperative implementation on my hard drive. It wasn't too hard to translate to Rockstar. </p> <p> Although Rockstar supports mainstream quoted strings like <code>"A"</code>, <code>"B"</code>, and so on, you can see that I went straight for <em>poetic string literals</em>. Before I started persisting Rockstar code to a file, I experimented with the language using the online interpreter. I wanted the program to look as much like rock lyrics as I could, so I didn't want to have too many statements like <code>Shout "FizzBuzz!"</code> in my code. </p> <h3 id="1ae1c1c329894b438bc934097090901b"> Obscuring space <a href="#1ae1c1c329894b438bc934097090901b" title="permalink">#</a> </h3> <p> My first concern was whether I could obscure the space character. Using a poetic string literal, I could: </p> <p> <pre>Space says&nbsp;&nbsp;</pre> </p> <p> The rules of poetic string literals is that everything between <code>says&nbsp;</code> and the newline character becomes the value of the string variable. So there's an extra space after <code>says&nbsp;</code>! </p> <p> After I renamed all the variables and functions, that line became: </p> <p> <pre>Your memory says&nbsp;&nbsp;</pre> </p> <p> Perhaps it isn't an unprintable character, but it <em>is</em> unsingable. </p> <h3 id="d1ae4568cc104312a3c20fe8019ccb18"> No else <a href="#d1ae4568cc104312a3c20fe8019ccb18" title="permalink">#</a> </h3> <p> The keyword <code>Else</code> looks conspicuously like a programming construct, so I wanted to get rid of that as well. That was easy, because I could just invert the initial <code>if</code> condition: </p> <p> <pre>If l ain't LetterA</pre> </p> <p> This effectively switches between the two alternative code blocks. </p> <h3 id="cbaa9c218905453bb3df2155b9e9b822"> Obscuring letter indices <a href="#cbaa9c218905453bb3df2155b9e9b822" title="permalink">#</a> </h3> <p> I also wanted to obscure the incrementing index values <code>1</code>, <code>2</code>, <code>3</code>, etcetera. Since the indices are monotonically increasing, I realised that I could use a counter and increment it: </p> <p> <pre>number is 0 If index is number Let retVal be LetterA Build number up If index is number retVal says B Build number up If index is number retVal says C</pre> </p> <p> The function initialises <code>number</code> to <code>0</code> and assigns a value to <code>retVal</code> if the input <code>index</code> is also <code>0</code>. </p> <p> If not, it increments the <code>number</code> (so that it's now <code>1</code>) and again compares it to <code>index</code>. This sufficiently obscures the indices, but if there's a way to hide the letters of the alphabet, I'm not aware of it. </p> <p> After I renamed the variables, the code became: </p> <p> <pre>Your courage is a tightrope Let it be without it If your hope is your courage Let the key be the way Build your courage up If your hope is your courage The key says B Build your courage up If your hope is your courage The key says C</pre> </p> <p> There's one more line of code in the final lyrics, compared to the above snippet. The line <code>Let it be without it</code> has no corresponding line of code in the readable version. What's going on? </p> <h3 id="aef4cf50f4484327b37cd61995c74d99"> Obscuring numbers <a href="#aef4cf50f4484327b37cd61995c74d99" title="permalink">#</a> </h3> <p> Like poetic string literals, Rockstar also supports <em>poetic number literals</em>. Due to its modulo-ten-based system, however, I found it difficult to come up with a good ten-letter word that fit the song's lyrical theme. I <em>could</em> have done something like this to produce the number <code>0</code>: </p> <p> <pre>Your courage is barbershop</pre> </p> <p> or some other ten-letter word. My problem was that regardless of what I chose, it didn't sound good. Some article like <code>a</code> or <code>the</code> would sound better, but that would change the value of the poetic number literal. <code>a tightrope</code> is the number <em>19</em>, because <code>a</code> has one letter, and <code>tightrope</code> has nine. </p> <p> There's a simple way to produce <em>0</em> from any number: just subtract the number from itself. That's what <code>Let it be without it</code> does. I could also have written it as <code>Let your courage be without your courage</code>, but I chose to take advantage of Rockstar's <em>pronoun</em> feature instead. I'd been looking for an opportunity to include the phrase <a href="https://en.wikipedia.org/wiki/Let_It_Be_(Beatles_song)">Let It Be</a> ever since I learned about the <code>Let x be y</code> syntax. </p> <p> The following code snippet initialises the variable <code>Your courage</code> to <code>19</code>, but on the next line subtracts 19 from 19 and updates the variable so that its value is now <code>0</code>. </p> <p> <pre>Your courage is a tightrope Let it be without it</pre> </p> <p> I had the same problem with initialising the numbers <em>1</em> and <em>2</em>, so further down I resorted to similar tricks: </p> <p> <pre>Raindrop is a wintercrop Light is a misanthrope Let it be without raindrop Darkness is a kaleidoscope Let it be without raindrop </pre> </p> <p> Here I had the additional constraint that I wanted the words to rhyme. The rhymes are a continuation of the previous lines' <code>up</code> and <code>hope</code>, so I struggled to come up with a ten-letter word that rhymes with <code>up</code>; <code>wintercrop</code> was the best I could do. <code>a wintercrop</code> is <em>10</em>, and the strategy is to define <code>Light</code> and <code>Darkness</code> as <em>11</em> and <em>12</em>, and then subtract <em>10</em> from both. At the first occurrence of <code>Let it be without raindrop</code>, <code>it</code> refers to <code>Light</code>, whereas the second time <code>it</code> refers to <code>Darkness</code>. </p> <h3 id="46b60c0d25e54f9599e5bd40b7e88d80"> Lyrical theme <a href="#46b60c0d25e54f9599e5bd40b7e88d80" title="permalink">#</a> </h3> <p> Once I had figured out how to obscure strings and numbers, it was time to rename all the readable variables and function names into idiomatic Rockstar. </p> <p> At first, I thought that I'd pattern my lyrics after <a href="https://en.wikipedia.org/wiki/Shine_On_You_Crazy_Diamond">Shine On You Crazy Diamond</a>, but I soon ran into problems with the keyword <code>taking</code>. I found it difficult to find words that would naturally succeed <code>taking</code>. Some options I thought of were: <ul> <li>taking the bus</li> <li>taking a chance</li> <li>taking hold</li> <li>taking flight</li> <li>taking time</li> </ul> Some of these didn't work for various reasons. In Rockstar <code>times</code> is a keyword, and apparently <code>time</code> is reserved as well. At least, the online interpreter choked on it. </p> <p> <code>Taking a chance</code> sounded <a href="https://en.wikipedia.org/wiki/Take_a_Chance_on_Me">too much like ABBA</a>. <code>Taking hold</code> created the derived problem that I had to initialise and use a variable called <code>hold</code>, and I couldn't make that work. </p> <p> <code>Taking flight</code>, on the other hand, turned out to provide a fertile opening. </p> <p> I soon realised, though, that my choice of words pulled the lyrical theme away from idiomatic Rockstar vocabulary. While I do get the <a href="https://en.wikipedia.org/wiki/Livin%27_on_a_Prayer">Tommy and Gina</a> references, I didn't feel at home in that poetic universe. </p> <p> On the other hand, I thought that the words started to sound like <a href="https://en.wikipedia.org/wiki/Yes_(band)">Yes</a>. I've listened to a lot of Yes. The lyrics are the same kind of lame and vapid as what was taking form in my editor. I decided to go in that direction. </p> <p> Granted, this is no longer idiomatic Rockstar, since it's more <a href="https://en.wikipedia.org/wiki/Progressive_rock">prog rock</a> than <a href="https://en.wikipedia.org/wiki/Glam_metal">hair metal</a>. I invoke creative license. </p> <p> Soon I also conceived of the extra ambition that I wanted the verses to rhyme. Here, it proved fortunate that the form <code>let x be y</code> is interchangeable with the form <code>put y into x</code>. Some words, like <em>darkness</em>, are difficult to rhyme with, so it helps that you can hide them within a <code>put y into x</code> form. </p> <p> Over the hours(!) I worked on this, a theme started to emerge. I'm particularly fond of the repeated motifs like: </p> <p> <pre>Your hope's start'n'stop</pre> </p> <p> which rhymes with <code>up</code>, but then later it appears again as </p> <p> <pre>Your hope's stop'n'start</pre> </p> <p> which rhymes with <code>heart</code>. Both words, by the way, represent the number <em>0</em>, since there's ten letters when you ignore the single quotes. </p> <h3 id="234366a7cb1a4998813719845f3c08d7"> Conclusion <a href="#234366a7cb1a4998813719845f3c08d7" title="permalink">#</a> </h3> <p> I spent more time on this that I suppose I ought to, but once I got started, it was hard to stop. I found the translation from readable code into 'idiomatic' Rockstar at least as difficult as writing working software. There's a lesson there, I believe. </p> <p> Rockstar is still a budding language, so I did miss a few features, chief among which would be <em>arrays</em>, but I'm not sure how one would make arrays sufficiently rock'n'roll. </p> <p> A unit testing framework would also be nice. </p> <p> If you liked this article, please <a href="https://www.linkedin.com/in/ploeh/">endorse my <em>Rockstar</em> skills on LinkedIn</a> so that we can <em>"confuse recruiters."</em> </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The 80/24 rule https://blog.ploeh.dk/2019/11/04/the-80-24-rule 2019-11-04T06:51:00+00:00 Mark Seemann <div id="post"> <p> <em>Write small blocks of code. How small? Here's how small.</em> </p> <p> One of the most common questions I get is this: </p> <p> <em>If you could give just one advice to programmers, what would it be?</em> </p> <p> That's easy: </p> <p> <em>Write small blocks of code.</em> </p> <p> Small methods. Small functions. Small procedures. </p> <p> How small? </p> <h3 id="849d07675e3a4c5681641eb0c67dfafb"> Few lines of code <a href="#849d07675e3a4c5681641eb0c67dfafb" title="permalink">#</a> </h3> <p> You can't give a universally good answer to that question. Among other things, it depends on the programming language in question. Some languages are much denser than others. The densest language I've ever encountered is <a href="https://en.wikipedia.org/wiki/APL_(programming_language)">APL</a>. </p> <p> Most mainstream languages, however, seem to be verbose to approximately the same order of magnitude. My experience is mostly with C#, so I'll use that (and similar languages like Java) as a starting point. </p> <p> When I write C# code, I become uncomfortable when my method size approaches fifteen or twenty lines of code. C# is, however, a fairly wordy language, so it sometimes happens that I have to allow a method to grow larger. My limit is probably somewhere around 25 lines of code. </p> <p> That's an arbitrary number, but if I have to quote a number, it would be around that size. Since it's arbitrary anyway, let's make it <em>24</em>, for reasons that I'll explain later. </p> <p> The maximum line count of a C# (or Java, or JavaScript, etc.) method, then, should be 24. </p> <p> To repeat the point from before, this depends on the language. I'd consider a 24-line <a href="https://www.haskell.org">Haskell</a> or <a href="https://fsharp.org">F#</a> function to be so huge that if I received it as a pull request, I'd reject it <a href="/2015/01/15/10-tips-for-better-pull-requests">on the grounds of size</a> alone. </p> <h3 id="8506ebcba585459b9739d84a7bcad758"> Narrow line width <a href="#8506ebcba585459b9739d84a7bcad758" title="permalink">#</a> </h3> <p> Most languages allow for flexibility in layout. For example, C-based languages use the <code>;</code> character as a delimiter. This enables you to write more than one statement per line: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">foo</span>&nbsp;=&nbsp;32;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">bar</span>&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">foo</span>&nbsp;+&nbsp;10;&nbsp;<span style="color:#2b91af;">Console</span>.<span style="color:#74531f;">WriteLine</span>(<span style="font-weight:bold;color:#1f377f;">bar</span>);</pre> </p> <p> You could attempt to avoid the 24-line-height rule by writing wide lines. That would, however, be to defeat the purpose. </p> <p> The purpose of writing small methods is to nudge yourself towards writing readable code; code that fits in your brain. The smaller, the better. </p> <p> For completeness sake, let's institute a maximum line width as well. If there's any accepted industry standard for maximum line width, it's 80 characters. I've used that maximum for years, and it's a good maximum. </p> <p> Like all other programmers, other people's code annoys me. The most common annoyance is that people write too wide code. </p> <p> This is probably because most programmers have drunk the Cool Aid that bigger screens make you more productive. When you code on a big screen, you don't notice how wide your lines become. </p> <p> There's many scenarios where wide code is problematic: <ul> <li>When you're comparing changes to a file side-by-side. This often happens when you review pull requests. Now you have only half of your normal screen width.</li> <li>When you're looking at code on a smaller device.</li> <li>When you're getting old, or are otherwise visually impaired. After I turned 40, I discovered that I found it increasingly difficult to see small things. I still use a 10-point font for programming, but I foresee that this will not last much longer.</li> <li>When you're <a href="https://en.wikipedia.org/wiki/Mob_programming">mob programming</a> you're limited to the size of the shared screen.</li> <li>When you're sharing your screen via the web, for remote pair programming or similar.</li> <li>When you're presenting code at meetups, user groups, conferences, etc.</li> </ul> What most programmers need, I think, is just a <a href="https://en.wikipedia.org/wiki/Nudge_theory">nudge</a>. In Visual Studio, for example, you can install the <a href="https://marketplace.visualstudio.com/items?itemName=PaulHarrington.EditorGuidelines">Editor Guidelines</a> extension, which will display one or more vertical guidelines. You can configure it as you'd like, but I've mine set to 80 characters, and bright red: </p> <p> <img src="/content/binary/vertical-guideline-at-80-characters.png" alt="Screen shot of editor with code, showing red vertical line at 80 characters."> </p> <p> Notice the red dotted vertical line that cuts through <code>universe</code>. It tells me where the 80 character limit is. </p> <h3 id="d94a52940215425e9cb19492d9f51a41"> Terminal box <a href="#d94a52940215425e9cb19492d9f51a41" title="permalink">#</a> </h3> <p> The 80-character limit has a long and venerable history, but what about the 24-line limit? While both are, ultimately, arbitrary, both fit the size of the popular <a href="https://en.wikipedia.org/wiki/VT100">VT100</a> terminal, which had a display resolution of 80x24 characters. </p> <p> A box of 80x24 characters thus reproduces the size of an old terminal. Does this mean that I suggest that you should write programs on terminals? No, people always misunderstand this. That should be the maximum size of a method. On larger screens, you'd be able to see multiple small methods at once. For example, you could view a unit test and its target in a split screen configuration. </p> <p> The exact sizes are arbitrary, but I think that there's something fundamentally right about such continuity with the past. </p> <p> I've been using the 80-character mark as a soft limit for years. I tend to stay within it, but I occasionally decide to make my code a little wider. I haven't paid quite as much attention to the number of lines of my methods, but only for the reason that I know that I tend to write methods shorter than that. Both limits have served me well for years. </p> <h3 id="e5301bcefd8f444487906af03df293b0"> Example <a href="#e5301bcefd8f444487906af03df293b0" title="permalink">#</a> </h3> <p> Consider this example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ActionResult</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Post</span>(<span style="color:#2b91af;">ReservationDto</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">dto</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">validationMsg</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Validator</span>.<span style="color:#74531f;">Validate</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">validationMsg</span>&nbsp;!=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">BadRequest</span>(<span style="font-weight:bold;color:#1f377f;">validationMsg</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Mapper</span>.<span style="color:#74531f;">Map</span>(<span style="font-weight:bold;color:#1f377f;">dto</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">reservations</span>&nbsp;=&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">ReadReservations</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">accepted</span>&nbsp;=&nbsp;maîtreD.<span style="font-weight:bold;color:#74531f;">CanAccept</span>(<span style="font-weight:bold;color:#1f377f;">reservations</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(!<span style="font-weight:bold;color:#1f377f;">accepted</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">StatusCode</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">StatusCodes</span>.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">id</span>&nbsp;=&nbsp;Repository.<span style="font-weight:bold;color:#74531f;">Create</span>(<span style="font-weight:bold;color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Ok</span>(<span style="font-weight:bold;color:#1f377f;">id</span>); }</pre> </p> <p> This method is 18 lines long, which includes the method declaration, curly brackets and blank lines. It easily stays within the 80-character limit. Note that I've deliberately formatted the code so that it behaves. You can see it in this fragment: </p> <p> <pre><span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">StatusCode</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">StatusCodes</span>.Status500InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>);</pre> </p> <p> Most people write it like this: </p> <p> <pre><span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="font-weight:bold;color:#74531f;">StatusCode</span>(<span style="color:#2b91af;">StatusCodes</span>.Status500InternalServerError,&nbsp;<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>);</pre> </p> <p> That doesn't look bad, but I've seen much worse examples. </p> <p> Another key to writing small methods is to call other methods. The above <code>Post</code> method doesn't look like much, but significant functionality could be hiding behind <code>Validator.Validate</code>, <code>Repository.ReadReservations</code>, or <code>maîtreD.CanAccept</code>. I hope that you agree that each of these objects and methods are named well enough to give you an idea about their purpose. </p> <h3 id="ca4e5a8403244f93a95c2736cdaf7eee"> Code that fits in your brain <a href="#ca4e5a8403244f93a95c2736cdaf7eee" title="permalink">#</a> </h3> <p> As I describe in my <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">Humane Code</a> video, the human brain can only keep track of <a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two">about seven things</a>. I think that this rule of thumb applies to the way we read and interpret code. If you need to understand and keep track of more than seven separate things at the same time, the code becomes harder to understand. </p> <p> This could explain why small methods are good. They're only good, however, if they're self-contained. When you look at a method like the above <code>Post</code> method, you'll be most effective if you don't need to have a deep understanding of how each of the dependencies work. If this is true, the method only juggles about five dependencies: <code>Validator</code>, <code>Mapper</code>, <code>Repository</code>, <code>maîtreD</code>, and its own base class (which provides the methods <code>BadRequest</code>, <code>StatusCode</code>, and <code>Ok</code>). Five dependencies is fewer than seven. </p> <p> Another way to evaluate the cognitive load of a method is to measure its <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a>. The <code>Post</code> method's cyclomatic complexity is <em>3</em>, so that should be easily within the brain's capacity. </p> <p> These are all heuristics, so read this for inspiration, not as law. They've served me well for years, though. </p> <h3 id="1c2120e143784dde8e520026b67651d4"> Conclusion <a href="#1c2120e143784dde8e520026b67651d4" title="permalink">#</a> </h3> <p> You've probably heard about the <em>80/20 rule</em>, also known as the <a href="https://en.wikipedia.org/wiki/Pareto_principle">Pareto principle</a>. Perhaps the title lead you to believe that this article was a misunderstanding. I admit that I went for an arresting title; perhaps a more proper name is the <em>80x24 rule</em>. </p> <p> The exact numbers can vary, but I've found a maximum method size of 80x24 characters to work well for C#. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="cbdf43b5551242efa330a163f01119ca"> <div class="comment-author">Jiehong <a href="#cbdf43b5551242efa330a163f01119ca">#</a></div> <div class="comment-content"> <p> As a matter of fact, <em>terminals</em> had 80 characters lines, because <a href="https://en.wikipedia.org/wiki/Punched_card#/media/File:FortranCardPROJ039.agr.jpg">IBM punch cards</a>, representing only 1 line, had 80 symbols (even though only the first 72 were used at first). However, I don't know why terminals settled for 24 lines! In Java, which is similar to C# in term of verbosity, Clean Code tend to push towards 20-lines long functions or less. One of the danger to make functions even smaller is that many more functions can create many indirections, and that becomes harder to keep track within our brains. </p> </div> <div class="comment-date">2019-11-04 11:13 UTC</div> </div> <div class="comment" id="7fc45f8fd537ba9907ad73daa2c85b52"> <div class="comment-author">Terrell <a href="#7fc45f8fd537ba9907ad73daa2c85b52">#</a></div> <div class="comment-content"> <p> Some additional terminal sizing history in Mike Hoye's recent similarly-named post: <a href="http://exple.tive.org/blarg/2019/10/23/80x25/">http://exple.tive.org/blarg/2019/10/23/80x25/</a> Spoiler - Banknotes! </p> </div> <div class="comment-date">2019-11-04 13:25 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A basic Haskell solution to the robot journeys coding exercise https://blog.ploeh.dk/2019/10/28/a-basic-haskell-solution-to-the-robot-journeys-coding-exercise 2019-10-28T04:34:00+00:00 Mark Seemann <div id="post"> <p> <em>This article shows an idiomatic, yet beginner-friendly Haskell solution to a coding exercise.</em> </p> <p> <a href="https://twitter.com/mikehadlow/status/1186332184086495233">Mike Hadlow tweeted</a> a coding exercise that involves parsing and evaluating instruction sets. <a href="https://www.haskell.org">Haskell</a> excels at such problems, so I decided to give it a go. Since this was only an exercise for the fun of it, I didn't want to set up a complete Haskell project. Rather, I wanted to write one or two <code>.hs</code> files that I could interact with via <em>GHCi</em>. This means no lenses, monad transformers, or other fancy libraries. </p> <p> Hopefully, this makes the code friendly to Haskell beginners. It shows what I consider <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a>, but basic Haskell, solving a problem of moderate difficulty. </p> <h3 id="79b308125b2d4bcc9df79f7c6569a6e9"> The problem <a href="#79b308125b2d4bcc9df79f7c6569a6e9" title="permalink">#</a> </h3> <p> <a href="https://github.com/mikehadlow/Journeys">Mike Hadlow has a detailed description of the exercise</a>, but in short, you're given a file with a set of instructions that look like this: </p> <p> <pre>1 1 E RFRFRFRF 1 1 E</pre> </p> <p> The first and last lines describe the position and orientation of a robot. The first line, for example, describes a robot at position (1, 1) facing east. A robot can face in one of the four normal directions of the map: north, east, south, and west. </p> <p> The first line gives the robot's start position, and the last line the <em>expected</em> end position. </p> <p> The middle line is a set of instructions to the robot. It can turn left or right, or move forward. </p> <p> The exercise is to evaluate whether journeys are valid; that is, whether the robot's end position matches the expected end position if it follows the commands. </p> <h3 id="cf43d781d49545c38e30f487867e904f"> Imports <a href="#cf43d781d49545c38e30f487867e904f" title="permalink">#</a> </h3> <p> I managed to solve the exercise with a single <code>Main.hs</code> file. Here's the module declaration and the required imports: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Main&nbsp;<span style="color:blue;">where</span> <span style="color:blue;">import</span>&nbsp;Data.Foldable <span style="color:blue;">import</span>&nbsp;Data.Ord <span style="color:blue;">import</span>&nbsp;Text.Read&nbsp;(<span style="color:#2b91af;">readPrec</span>) <span style="color:blue;">import</span>&nbsp;Text.ParserCombinators.ReadP <span style="color:blue;">import</span>&nbsp;Text.ParserCombinators.ReadPrec&nbsp;(<span style="color:#2b91af;">readPrec_to_P</span>,&nbsp;<span style="color:#2b91af;">minPrec</span>)</pre> </p> <p> These imports are only required to support parsing of input. Once parsed, you can evaluate each journey using nothing but the functions available in the standard <code>Prelude</code>. </p> <h3 id="8329bba327f54dbfb8474f9102ba8662"> Types <a href="#8329bba327f54dbfb8474f9102ba8662" title="permalink">#</a> </h3> <p> Haskell is a statically typed language, so it often pays to define some types. Granted, the exercise hardly warrants all of these types, but as an example of idiomatic Haskell, I think that this is still good practice. After all, Haskell types are easy to declare. Often, they are one-liners: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Direction&nbsp;=&nbsp;North&nbsp;|&nbsp;East&nbsp;|&nbsp;South&nbsp;|&nbsp;West&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> The <code>Direction</code> type enumerates the four corners of the world. </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Robot&nbsp;=&nbsp;Robot&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;robotPosition&nbsp;::&nbsp;(Integer,&nbsp;Integer) &nbsp;&nbsp;,&nbsp;robotDirection&nbsp;::&nbsp;Direction&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> The <code>Robot</code> record type represents the state of a robot: its position and the direction it faces. </p> <p> You'll also need to enumerate the commands that you can give a robot: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Command&nbsp;=&nbsp;TurnLeft&nbsp;|&nbsp;TurnRight&nbsp;|&nbsp;MoveForward&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> Finally, you can also define a type for a <code>Journey</code>: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Journey&nbsp;=&nbsp;Journey&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;journeyStart&nbsp;::&nbsp;Robot &nbsp;&nbsp;,&nbsp;journeyCommands&nbsp;::&nbsp;[Command] &nbsp;&nbsp;,&nbsp;journeyEnd&nbsp;::&nbsp;Robot&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> These are all the types required for solving the exercise. </p> <h3 id="5ada3f24aa4e4858892855ae076f457b"> Parsing <a href="#5ada3f24aa4e4858892855ae076f457b" title="permalink">#</a> </h3> <p> The format of the input file is simple enough that it could be done in an ad-hoc fashion using <code>lines</code>, <code>word</code>, <code>read</code>, and a few other low-level functions. While the format barely warrants the use of parser combinators, I'll still use some to showcase the power of that approach. </p> <p> Since one of my goals is to implement the functionality using a single <code>.hs</code> file, I can't pull in external parser combinator libraries. Instead, I'll use the built-in <code>ReadP</code> module, which I've often found sufficient to parse files like the present exercise input file. </p> <p> First, you're going to have to be able to parse numbers, which can be done using the <code>Read</code> type class. You'll need, however, to be able to compose <code>Integer</code> parsers with other <code>ReadP</code> parsers. </p> <p> <pre><span style="color:#2b91af;">parseRead</span>&nbsp;::&nbsp;<span style="color:blue;">Read</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">ReadP</span>&nbsp;a parseRead&nbsp;=&nbsp;readPrec_to_P&nbsp;readPrec&nbsp;minPrec</pre> </p> <p> This turns every <code>Read</code> instance value into a <code>ReadP</code> value. (I admit that I wasn't sure which precedence number to use, but <code>minPrec</code> seems to work.) </p> <p> Next, you need a parser for <code>Direction</code> values: </p> <p> <pre><span style="color:#2b91af;">parseDirection</span>&nbsp;::&nbsp;<span style="color:blue;">ReadP</span>&nbsp;<span style="color:blue;">Direction</span> parseDirection&nbsp;= &nbsp;&nbsp;choice&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;<span style="color:#a31515;">&#39;N&#39;</span>&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;North, &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;<span style="color:#a31515;">&#39;E&#39;</span>&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;East, &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;<span style="color:#a31515;">&#39;S&#39;</span>&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;South, &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;<span style="color:#a31515;">&#39;W&#39;</span>&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;West&nbsp;]</pre> </p> <p> Notice how declarative this looks. The <code>choice</code> function combines a list of other parsers. When an individual parser in that list encounters the <code>'N'</code> character, it'll parse it as <code>North</code>, <code>'E'</code> as <code>East</code>, and so on. </p> <p> You can now parse an entire <code>Robot</code> using the <code>Applicative</code> <code>&lt;*&gt;</code> and <code>&lt;*</code> operators. </p> <p> <pre><span style="color:#2b91af;">parseRobot</span>&nbsp;::&nbsp;<span style="color:blue;">ReadP</span>&nbsp;<span style="color:blue;">Robot</span> parseRobot&nbsp;= &nbsp;&nbsp;(\x&nbsp;y&nbsp;d&nbsp;-&gt;&nbsp;Robot&nbsp;(x,&nbsp;y)&nbsp;d)&nbsp;&lt;$&gt; &nbsp;&nbsp;(parseRead&nbsp;&lt;*&nbsp;char&nbsp;<span style="color:#a31515;">&#39;&nbsp;&#39;</span>)&nbsp;&lt;*&gt; &nbsp;&nbsp;(parseRead&nbsp;&lt;*&nbsp;char&nbsp;<span style="color:#a31515;">&#39;&nbsp;&#39;</span>)&nbsp;&lt;*&gt; &nbsp;&nbsp;&nbsp;parseDirection</pre> </p> <p> The <code>&lt;*&gt;</code> operator combines two parsers by using the output of both of them, whereas the <code>&lt;*</code> combines two parsers by running both of them, but discarding the output of the right-hand parser. A good mnemonic is that the operator points to the parser that produces an output. Here', the <code>parseRobot</code> function uses the <code>&lt;*</code> operator to require that each number is followed by a space. The space, however, is just a delimiter, so you throw it away. </p> <p> <code>parseRead</code> parses any <code>Read</code> instance. Here, the <code>parseRobot</code> function uses it to parse each <code>Integer</code> in a robot's position. It also uses <code>parseDirection</code> to parse the robot's direction. </p> <p> Similar to how you can parse directions, you can also parse the commands: </p> <p> <pre><span style="color:#2b91af;">parseCommand</span>&nbsp;::&nbsp;<span style="color:blue;">ReadP</span>&nbsp;<span style="color:blue;">Command</span> parseCommand&nbsp;= &nbsp;&nbsp;choice&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;<span style="color:#a31515;">&#39;L&#39;</span>&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;TurnLeft, &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;<span style="color:#a31515;">&#39;R&#39;</span>&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;TurnRight, &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;MoveForward]</pre> </p> <p> Likewise, similar to how you parse a single robot, you can now parse a journey: </p> <p> <pre><span style="color:#2b91af;">parseJourney</span>&nbsp;::&nbsp;<span style="color:blue;">ReadP</span>&nbsp;<span style="color:blue;">Journey</span> parseJourney&nbsp;= &nbsp;&nbsp;Journey&nbsp;&lt;$&gt; &nbsp;&nbsp;(parseRobot&nbsp;&lt;*&nbsp;string&nbsp;<span style="color:#a31515;">&quot;\n&quot;</span>)&nbsp;&lt;*&gt; &nbsp;&nbsp;(many&nbsp;parseCommand&nbsp;&lt;*&nbsp;string&nbsp;<span style="color:#a31515;">&quot;\n&quot;</span>)&nbsp;&lt;*&gt; &nbsp;&nbsp;&nbsp;parseRobot</pre> </p> <p> The only new element compared to <code>parseRobot</code> is the use of the <code>many</code> parser combinator, which looks for zero, one, or many <code>Command</code> values. </p> <p> This gives you a way to parse a complete journey, but the input file contains many of those, separated by newlines and other whitespace: </p> <p> <pre><span style="color:#2b91af;">parseJourneys</span>&nbsp;::&nbsp;<span style="color:blue;">ReadP</span>&nbsp;[<span style="color:blue;">Journey</span>] parseJourneys&nbsp;=&nbsp;parseJourney&nbsp;`sepBy`&nbsp;skipSpaces</pre> </p> <p> Finally, you can parse a multi-line string into a list of journeys: </p> <p> <pre><span style="color:#2b91af;">parseInput</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Journey</span>] parseInput&nbsp;=&nbsp;<span style="color:blue;">fst</span>&nbsp;.&nbsp;minimumBy&nbsp;(comparing&nbsp;<span style="color:blue;">snd</span>)&nbsp;.&nbsp;readP_to_S&nbsp;parseJourneys</pre> </p> <p> When you run <code>readP_to_S</code>, it'll produce a list of alternatives, as there's more than one way to interpret the file according to <code>parseJourneys</code>. Each alternative is presented as a tuple of the parse result and the remaining (or unconsumed) string. I'm after the alternative that consumes as much of the input file as possible (which turns out to be all of it), so I use <code>minimumBy</code> to find the tuple that has the smallest second element. Then I return the first element of that tuple. </p> <p> Play around with <code>readP_to_S parseJourneys</code> in GHCi if you want all the details. </p> <h3 id="34135daae9634db2885e28382760d1fd"> Evaluation <a href="#34135daae9634db2885e28382760d1fd" title="permalink">#</a> </h3> <p> Haskell beginners may still find operators like <code>&lt;*&gt;</code> cryptic, but they're essential to parser combinators. Evaluation of the journeys is, in comparison, simple. </p> <p> You can start by defining a function to turn right: </p> <p> <pre><span style="color:#2b91af;">turnRight</span>&nbsp;::&nbsp;<span style="color:blue;">Robot</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Robot</span> turnRight&nbsp;r@(Robot&nbsp;_&nbsp;North)&nbsp;=&nbsp;r&nbsp;{&nbsp;robotDirection&nbsp;=&nbsp;East&nbsp;} turnRight&nbsp;r@(Robot&nbsp;_&nbsp;&nbsp;East)&nbsp;=&nbsp;r&nbsp;{&nbsp;robotDirection&nbsp;=&nbsp;South&nbsp;} turnRight&nbsp;r@(Robot&nbsp;_&nbsp;South)&nbsp;=&nbsp;r&nbsp;{&nbsp;robotDirection&nbsp;=&nbsp;West&nbsp;} turnRight&nbsp;r@(Robot&nbsp;_&nbsp;&nbsp;West)&nbsp;=&nbsp;r&nbsp;{&nbsp;robotDirection&nbsp;=&nbsp;North&nbsp;}</pre> </p> <p> There's more than one way to write a function that rotates one direction to the right, but I chose one that I found most readable. It trades clarity for verbosity by relying on simple pattern matching. I hope that it's easy to understand for Haskell beginners, and perhaps even for people who haven't seen Haskell code before. </p> <p> The function to turn left uses the same structure: </p> <p> <pre><span style="color:#2b91af;">turnLeft</span>&nbsp;::&nbsp;<span style="color:blue;">Robot</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Robot</span> turnLeft&nbsp;r@(Robot&nbsp;_&nbsp;North)&nbsp;=&nbsp;r&nbsp;{&nbsp;robotDirection&nbsp;=&nbsp;West&nbsp;} turnLeft&nbsp;r@(Robot&nbsp;_&nbsp;&nbsp;West)&nbsp;=&nbsp;r&nbsp;{&nbsp;robotDirection&nbsp;=&nbsp;South&nbsp;} turnLeft&nbsp;r@(Robot&nbsp;_&nbsp;South)&nbsp;=&nbsp;r&nbsp;{&nbsp;robotDirection&nbsp;=&nbsp;East&nbsp;} turnLeft&nbsp;r@(Robot&nbsp;_&nbsp;&nbsp;East)&nbsp;=&nbsp;r&nbsp;{&nbsp;robotDirection&nbsp;=&nbsp;North&nbsp;}</pre> </p> <p> The last command you need to implement is moving forward: </p> <p> <pre><span style="color:#2b91af;">moveForward</span>&nbsp;::&nbsp;<span style="color:blue;">Robot</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Robot</span> moveForward&nbsp;(Robot&nbsp;(x,&nbsp;y)&nbsp;North)&nbsp;=&nbsp;Robot&nbsp;(x,&nbsp;y&nbsp;+&nbsp;1)&nbsp;North moveForward&nbsp;(Robot&nbsp;(x,&nbsp;y)&nbsp;&nbsp;East)&nbsp;=&nbsp;Robot&nbsp;(x&nbsp;+&nbsp;1,&nbsp;y)&nbsp;East moveForward&nbsp;(Robot&nbsp;(x,&nbsp;y)&nbsp;South)&nbsp;=&nbsp;Robot&nbsp;(x,&nbsp;y&nbsp;-&nbsp;1)&nbsp;South moveForward&nbsp;(Robot&nbsp;(x,&nbsp;y)&nbsp;&nbsp;West)&nbsp;=&nbsp;Robot&nbsp;(x&nbsp;-&nbsp;1,&nbsp;y)&nbsp;West</pre> </p> <p> The <code>moveForward</code> function also pattern-matches on the direction the robot is facing, this time to increment or decrement the <code>x</code> or <code>y</code> coordinate as appropriate. </p> <p> You can now evaluate all three commands: </p> <p> <pre><span style="color:#2b91af;">evalCommand</span>&nbsp;::&nbsp;<span style="color:blue;">Command</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Robot</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Robot</span> evalCommand&nbsp;&nbsp;&nbsp;TurnRight&nbsp;=&nbsp;turnRight evalCommand&nbsp;&nbsp;&nbsp;&nbsp;TurnLeft&nbsp;=&nbsp;turnLeft evalCommand&nbsp;MoveForward&nbsp;=&nbsp;moveForward</pre> </p> <p> The <code>evalCommand</code> pattern-matches on all three <code>Command</code> cases and returns the appropriate function for each. </p> <p> You can now evaluate whether a <code>Journey</code> is valid: </p> <p> <pre><span style="color:#2b91af;">isJourneyValid</span>&nbsp;::&nbsp;<span style="color:blue;">Journey</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> isJourneyValid&nbsp;(Journey&nbsp;s&nbsp;cs&nbsp;e)&nbsp;=&nbsp;<span style="color:blue;">foldl</span>&nbsp;(<span style="color:blue;">flip</span>&nbsp;evalCommand)&nbsp;s&nbsp;cs&nbsp;==&nbsp;e</pre> </p> <p> The <code>isJourneyValid</code> function pattern-matches the constituent values out of <code>Journey</code>. I named the <code>journeyStart</code> value <code>s</code> (for <em>start</em>), the <code>journeyCommands</code> value <code>cs</code> (for <em>commands</em>), and the <code>journeyEnd</code> value <code>e</code> (for <em>end</em>). </p> <p> The <code>evalCommand</code> function evaluates a single <code>Command</code>, but a <code>Journey</code> contains many commands. You'll need to evaluate the first command to find the position from which you evaluate the second command, and so on. Imperative programmers would use a <em>for loop</em> for something like that, but in functional programming, a <em>fold</em>, in this case from the left, is how it's done. </p> <p> <code>foldl</code> requires you to supply an initial state <code>s</code> as well as the list of commands <code>cs</code>. The entire <code>foldl</code> expression produces a final <code>Robot</code> state that you can compare against the expected end state <code>e</code>. </p> <h3 id="6267cc3a03594d0fb21cd7cc61430eb0"> Execution <a href="#6267cc3a03594d0fb21cd7cc61430eb0" title="permalink">#</a> </h3> <p> Load the input file, parse it, and evaluate each journey in the <code>main</code> function: </p> <p> <pre><span style="color:#2b91af;">main</span>&nbsp;::&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;input&nbsp;&lt;-&nbsp;parseInput&nbsp;&lt;$&gt;&nbsp;<span style="color:blue;">readFile</span>&nbsp;<span style="color:#a31515;">&quot;input.txt&quot;</span> &nbsp;&nbsp;<span style="color:blue;">mapM_</span>&nbsp;<span style="color:blue;">print</span>&nbsp;$&nbsp;isJourneyValid&nbsp;&lt;$&gt;&nbsp;input</pre> </p> <p> I just load the <code>Main.hs</code> file in GHCi and run the <code>main</code> function: </p> <p> <pre>Prelude&gt; :load Main.hs [1 of 1] Compiling Main ( Main.hs, interpreted ) Ok, one module loaded. *Main&gt; main True True True</pre> </p> <p> I used the same input file as Mike Hadlow, and it turns out that all journeys are valid. That's not what I'd expected from an exercise like this, so I cloned and ran Mike's solution as well, and it seems that it arrives at the same result. </p> <h3 id="683ea9809a774338b16aed1ad41e1984"> Conclusion <a href="#683ea9809a774338b16aed1ad41e1984" title="permalink">#</a> </h3> <p> Haskell is a great language for small coding exercises that require parsing and interpretation. In this article, I demonstrated one solution to the <em>robot journeys</em> coding exercise. My goal was to show some beginner-friendly, but still idiomatic Haskell code. </p> <p> Granted, the use of parser combinators is on the verge of being overkill, but I wanted to show an example; Haskell examples are scarce, so I hope it's helpful. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A red-green-refactor checklist https://blog.ploeh.dk/2019/10/21/a-red-green-refactor-checklist 2019-10-21T06:49:00+00:00 Mark Seemann <div id="post"> <p> <em>A simple read-do checklist for test-driven development.</em> </p> <p> I recently read <a href="https://amzn.to/35Wk5yD">The Checklist Manifesto</a>, a book about the power of checklists. That may sound off-putting and tedious, but I actually <a href="https://www.goodreads.com/review/show/2949987528">found it inspiring</a>. It explains how checklists empower skilled professionals to focus on difficult problems, while preventing avoidable mistakes. </p> <p> Since I read the book with the intent to see if there were ideas that we could apply in software development, I thought about checklists one might create for software development. Possibly the simplest checklist is one that describes the <em>red-green-refactor</em> cycle of test-driven development. </p> <h3 id="530e04dfea574602952023fa218d8a7c"> Types of checklists <a href="#530e04dfea574602952023fa218d8a7c" title="permalink">#</a> </h3> <p> As the book describes, there's basically two types of checklists: <ul> <li><strong>Do-confirm.</strong> With such a checklist, you perform a set of tasks, and then subsequently, at a sufficient <em>pause point</em> go through the checklist to verify that you remembered to perform all the tasks on the list.</li> <li><strong>Read-do.</strong> With this type of checklist, you read each item for instructions and then perform the task. Only when you've performed the task do you move on to the next item on the list.</li> </ul> I find it most intuitive to describe the red-green-refactor cycle as a <em>read-do</em> list. I did, however, find it expedient to include a <em>do-confirm</em> sub-list for one of the overall steps. </p> <p> This list is, I think, mostly useful if you're still learning test-driven development. It can be easily internalised. As such, I offer this for inspiration, and as a learning aid. </p> <h3 id="05b38ebc9c0c419b9a146be976578bd2"> Red-green-refactor checklist <a href="#05b38ebc9c0c419b9a146be976578bd2" title="permalink">#</a> </h3> <p> Read each of the steps in the list and perform the task. <ol> <li>Write a failing test. <ul> <li>Did you run the test?</li> <li>Did it fail?</li> <li>Did it fail because of an assertion?</li> <li>Did it fail because of the <em>last</em> assertion?</li> </ul> </li> <li>Make all tests pass by doing the simplest thing that could possibly work.</li> <li>Consider the resulting code. Can it be improved? If so, do it, but make sure that all tests still pass.</li> <li>Repeat</li> </ol> Perhaps the most value this checklist provides isn't so much the overall <em>read-do</em> list, but rather the subordinate <em>do-confirm</em> list associated with the first step. </p> <p> I regularly see people write failing tests as an initial step. The reason the test fails, however, is because the implementation throws an exception. </p> <h3 id="24a066fa0b9b401687d47b92473d63d0"> Improperly failing tests <a href="#24a066fa0b9b401687d47b92473d63d0" title="permalink">#</a> </h3> <p> Consider, as an example, the first test you might write when doing the <a href="https://en.wikipedia.org/wiki/Fizz_buzz">FizzBuzz</a> kata. </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">One</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#2b91af;">FizzBuzz</span>.<span style="color:#74531f;">Convert</span>(1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Equal</span>(<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> I wrote this test first (i.e. before the 'production' code) and used Visual Studio's refactoring tools to generate the implied type and method. </p> <p> When I run the test, it fails. </p> <p> Further investigation, however, reveals that the test fails when <code>Convert</code> is called: </p> <p> <pre>Ploeh.Katas.FizzBuzzKata.FizzBuzzTests.One Source: FizzBuzzTests.cs line: 11 Duration: 8 ms Message: System.NotImplementedException : The method or operation is not implemented. Stack Trace: at FizzBuzz.Convert(Int32 i) in FizzBuzz.cs line: 9 at FizzBuzzTests.One() in FizzBuzzTests.cs line: 13 </pre> </p> <p> This is hardly surprising, since this is the current 'implementation': </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Convert</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotImplementedException</span>(); }</pre> </p> <p> This is what the subordinate <em>do-confirm</em> checklist is for. Did the test fail because of an assertion? In this case, the answer is no. </p> <p> This means that you're not yet done with the <em>read</em> phase. </p> <h3 id="97295029364d4cd7ace15be9c9f8dc64"> Properly failing tests <a href="#97295029364d4cd7ace15be9c9f8dc64" title="permalink">#</a> </h3> <p> You can address the issue by changing the <code>Convert</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Convert</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; }</pre> </p> <p> This causes the test to fail because of an assertion: </p> <p> <pre> Ploeh.Katas.FizzBuzzKata.FizzBuzzTests.One Source: FizzBuzzTests.cs line: 11 Duration: 13 ms Message: Assert.Equal() Failure ↓ (pos 0) Expected: 1 Actual: ↑ (pos 0) Stack Trace: at FizzBuzzTests.One() in FizzBuzzTests.cs line: 14 </pre> </p> <p> Not only does the test fail because of an assertion - it fails because of the last assertion (since there's only one assertion). This completes the <em>do-confirm</em> checklist, and you're now ready to make the simplest change that could possibly work: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Convert</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>; }</pre> </p> <p> This passes the test suite. </p> <h3 id="bdb785a78ebf475c95c64197274268c7"> Conclusion <a href="#bdb785a78ebf475c95c64197274268c7" title="permalink">#</a> </h3> <p> It's important to see tests fail. Particularly, it's important to see tests fail for the reason you expect them to fail. You'd be surprised how often you inadvertently write an <a href="/2019/10/14/tautological-assertion">assertion that can never fail</a>. </p> <p> Once you've seen the test fail for the proper reason, make it pass. </p> <p> Finally, refactor the code if necessary. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="99be0da15a164d5782afdef808300828"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#99be0da15a164d5782afdef808300828">#</a></div> <div class="comment-content"> <p> I remember the first time that I realized that I did the red step wrong because my test didn't fail for the intended reason (i.e. it didn't fail because of an assertion). Before that, I didn't realize that I needed to This is a nice programming checklist. Thanks for sharing it :) </p> <blockquote> 3. Consider the resulting code. Can it be improved? If so, do it, but make sure that all tests still pass. </blockquote> <blockquote> Finally, refactor the code if necessary. </blockquote> <p> If I can be a <a href="https://blog.ploeh.dk/2019/10/07/devils-advocate/">Devil's advocate</a> for a moment, then I would say that code can always be improved and few things are necessary. In all honesty though, I think the refactoring step is the most interesting. All three steps include aspects of science and art, but I think the refactor step includes the most of both. On the one hand, it is extremely creative and full of judgement calls about what code should be refactored and what properties the resulting code should have. On the other hand, much of the work of how to (properly) refactor is laid out in books like <a href="https://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Signature/dp/0134757599">Martin Fowler's Refacoring</a> and is akin to algebraic manipulations of an algebraic formula. </p> <p> In other words, I feel like there is room to expand on this checklist in the refactor step. Do you have any thoughts about you might expand it? </p> </div> <div class="comment-date">2019-10-25 00:33 UTC</div> </div> <div class="comment" id="28976782c7984115a65d539eff3d0414"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#28976782c7984115a65d539eff3d0414">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I agree that the <em>refactoring</em> step is both important and compelling. I can't, however, imagine how a checklist would be useful. </p> <p> The point of <em>The Checklist Manifesto</em> is that checklists help identify avoidable mistakes. A checklist isn't intended to describe an algorithm, but rather to make sure that crucial steps aren't forgotten. </p> <p> Another important point from <em>The Checklist Manifesto</em> is that a checklist is only effective if it's not too big. A checklist that tries to cover every eventuality isn't useful, because then people don't follow it. </p> <p> As you write, refactoring is a big topic, covered by several books. All the creativity and experience that goes into refactoring doesn't seem like something that can easily be expressed as an effective checklist. </p> <p> I don't mind being proven wrong, though, so by all means give it a go. </p> </div> <div class="comment-date">2019-10-25 21:51 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tautological assertion https://blog.ploeh.dk/2019/10/14/tautological-assertion 2019-10-14T18:39:00+00:00 Mark Seemann <div id="post"> <p> <em>It's surprisingly easy to write a unit test assertion that never fails.</em> </p> <p> Recently I was mob programming with a pair of <a href="https://idq.dk">IDQ</a>'s programmers. We were starting a new code base, using test-driven development (TDD). This was the first test we wrote: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HandleObserveUnitStatusStartsSaga</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">subscribers</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;&nbsp;{&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">Parse</span>(<span style="color:#a31515;">&quot;{4D093799-9CCC-4135-8CB3-8661985A5853}&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StatusPolicy</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StatusPolicyData</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UnitId&nbsp;=&nbsp;123, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subscribers&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">subscribers</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">subscriber</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">Parse</span>(<span style="color:#a31515;">&quot;{003C5527-7747-4C7A-980E-67040DB738C3}&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ObserveUnitStatus</span>(123,&nbsp;<span style="font-weight:bold;color:#1f377f;">subscriber</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TestableMessageHandlerContext</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.<span style="font-weight:bold;color:#74531f;">Handle</span>(<span style="font-weight:bold;color:#1f377f;">message</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Contains</span>(<span style="font-weight:bold;color:#1f377f;">subscriber</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.Data.Subscribers); }</pre> </p> <p> This unit test uses <a href="https://xunit.net">xUnit.net</a> 2.4.0 and <a href="https://particular.net/nservicebus">NServiceBus</a> 7.1.10 on .NET Core 2.2. The System Under Test (SUT) is intended to be an NServiceBus Saga that monitors a resource for status changes. If a <em>unit</em> changes status, the Saga will alert its subscribers. </p> <p> The test verifies that when a new subscriber wishes to observe a unit, then its ID is added to the policy's list of subscribers. </p> <p> The test induced us to implement <code>Handle</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Handle</span>(<span style="color:#2b91af;">ObserveUnitStatus</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>,&nbsp;<span style="color:#2b91af;">IMessageHandlerContext</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Data.Subscribers.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">message</span>.SubscriberId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Task</span>.CompletedTask; }</pre> </p> <p> Following the <em>red-green-refactor</em> cycle of TDD, this seemed an appropriate implementation. </p> <h3 id="8182b3c72b2940b98195f41b7a1193e8"> Enter the Devil <a href="#8182b3c72b2940b98195f41b7a1193e8" title="permalink">#</a> </h3> <p> I often use the <a href="/2019/10/07/devils-advocate">Devil's advocate</a> technique to figure out what to do next, so I made this change to the <code>Handle</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Handle</span>(<span style="color:#2b91af;">ObserveUnitStatus</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>,&nbsp;<span style="color:#2b91af;">IMessageHandlerContext</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Data.Subscribers.<span style="font-weight:bold;color:#74531f;">Clear</span>(); &nbsp;&nbsp;&nbsp;&nbsp;Data.Subscribers.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">message</span>.SubscriberId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Task</span>.CompletedTask; }</pre> </p> <p> The change is that the method first deletes all existing subscribers. This is obviously wrong, but it passes all tests. That's no surprise, since I intentionally introduced the change to make us improve the test. </p> <h3 id="55331957e1de4691bb182c2aae614f4e"> False negative <a href="#55331957e1de4691bb182c2aae614f4e" title="permalink">#</a> </h3> <p> We had to write a new test, or improve the existing test, so that the defect I just introduced would be caught. I suggested an improvement to the existing test: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HandleObserveUnitStatusStartsSaga</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">subscribers</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;&nbsp;{&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">Parse</span>(<span style="color:#a31515;">&quot;{4D093799-9CCC-4135-8CB3-8661985A5853}&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StatusPolicy</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StatusPolicyData</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UnitId&nbsp;=&nbsp;123, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subscribers&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">subscribers</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">subscriber</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">Parse</span>(<span style="color:#a31515;">&quot;{003C5527-7747-4C7A-980E-67040DB738C3}&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ObserveUnitStatus</span>(123,&nbsp;<span style="font-weight:bold;color:#1f377f;">subscriber</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TestableMessageHandlerContext</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.<span style="font-weight:bold;color:#74531f;">Handle</span>(<span style="font-weight:bold;color:#1f377f;">message</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Contains</span>(<span style="font-weight:bold;color:#1f377f;">subscriber</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.Data.Subscribers); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Superset</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">expectedSubset</span>:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="font-weight:bold;color:#1f377f;">subscribers</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="font-weight:bold;color:#1f377f;">sut</span>.Data.Subscribers)); }</pre> </p> <p> The only change is the addition of the last assertion. </p> <p> Smugly I asked the keyboard driver to run the tests, anticipating that it would now fail. </p> <p> It passed. </p> <p> We'd just managed to write a <a href="http://xunitpatterns.com/false%20negative.html">false negative</a>. Even though there's a defect in the code, the test still passes. I was nonplussed. None of us expected the test to pass, yet it does. </p> <p> It took us a minute to figure out what was wrong. Before you read on, try to figure it out for yourself. Perhaps it's immediately clear to you, but it took three people with decades of programming experience a few minutes to spot the problem. </p> <h3 id="6f985240963a40d8af913e251b3b86bf"> Aliasing <a href="#6f985240963a40d8af913e251b3b86bf" title="permalink">#</a> </h3> <p> The problem is <a href="https://en.wikipedia.org/wiki/Aliasing_(computing)">aliasing</a>. While named differently, <code>subscribers</code> and <code>sut.Data.Subscribers</code> is the same object. Of course one is a subset of the other, since a set is considered to be a subset of itself. </p> <p> The assertion is tautological. It can never fail. </p> <h3 id="9e8ec91f731e4f67aceefbebb76799d4"> Fixing the problem <a href="#9e8ec91f731e4f67aceefbebb76799d4" title="permalink">#</a> </h3> <p> It's surprisingly easy to write tautological assertions when working with mutable state. This regularly happens to me, perhaps a few times a month. Once you've realised that this has happened, however, it's easy to address. </p> <p> <code>subscribers</code> shouldn't change during the test, so make it immutable. </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;<span style="font-weight:bold;color:#74531f;">HandleObserveUnitStatusStartsSaga</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;&nbsp;<span style="font-weight:bold;color:#1f377f;">subscribers</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">Parse</span>(<span style="color:#a31515;">&quot;{4D093799-9CCC-4135-8CB3-8661985A5853}&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StatusPolicy</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StatusPolicyData</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UnitId&nbsp;=&nbsp;123, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subscribers&nbsp;=&nbsp;<span style="font-weight:bold;color:#1f377f;">subscribers</span>.<span style="font-weight:bold;color:#74531f;">ToList</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">subscriber</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.<span style="color:#74531f;">Parse</span>(<span style="color:#a31515;">&quot;{003C5527-7747-4C7A-980E-67040DB738C3}&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ObserveUnitStatus</span>(123,&nbsp;<span style="font-weight:bold;color:#1f377f;">subscriber</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TestableMessageHandlerContext</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.<span style="font-weight:bold;color:#74531f;">Handle</span>(<span style="font-weight:bold;color:#1f377f;">message</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Contains</span>(<span style="font-weight:bold;color:#1f377f;">subscriber</span>,&nbsp;<span style="font-weight:bold;color:#1f377f;">sut</span>.Data.Subscribers); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">Superset</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">expectedSubset</span>:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="font-weight:bold;color:#1f377f;">subscribers</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#1f377f;">actual</span>:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="font-weight:bold;color:#1f377f;">sut</span>.Data.Subscribers)); }</pre> </p> <p> An array strictly isn't immutable, but declaring it as <code>IEnumerable&lt;Guid&gt;</code> hides the mutation capabilities. The test now has to copy <code>subscribers</code> to a list before assigning it to the policy's data. This anti-aliases <code>subscribers</code> from <code>sut.Data.Subscribers</code>, and causes the test to fail. After all, there's a defect in the <code>Handle</code> method. </p> <p> You now have to remove the offending line: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;<span style="font-weight:bold;color:#74531f;">Handle</span>(<span style="color:#2b91af;">ObserveUnitStatus</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">message</span>,&nbsp;<span style="color:#2b91af;">IMessageHandlerContext</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">context</span>) { &nbsp;&nbsp;&nbsp;&nbsp;Data.Subscribers.<span style="font-weight:bold;color:#74531f;">Add</span>(<span style="font-weight:bold;color:#1f377f;">message</span>.SubscriberId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#2b91af;">Task</span>.CompletedTask; }</pre> </p> <p> This makes the test pass. </p> <h3 id="fd8e8ab5e7314ebbadc4775741af11fa"> Summary <a href="#fd8e8ab5e7314ebbadc4775741af11fa" title="permalink">#</a> </h3> <p> This article shows an example where I was surprised by aliasing. An assertion that I thought would fail turned out to be a false negative. </p> <p> You can easily make the mistake of writing a test that always passes. If you haven't tried it, you may think that you're too smart to do that, but it regularly happens to me. Other TDD practitioners have told me that it also happens to them. </p> <p> This is the reason that the <em>red-green-refactor</em> process encourages you to run each new test <em>and see it fail</em>. If you haven't seen it fail, you don't know if you've avoided a tautology. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Devil's advocate https://blog.ploeh.dk/2019/10/07/devils-advocate 2019-10-07T15:00:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you know when you have enough test cases. The Devil's Advocate technique can help you decide.</em> </p> <p> When I review unit tests, I often utilise a technique I call <em>Devil's Advocate</em>. I do the same whenever I consider if I have a sufficient number of test cases. The first time I explicitly named the technique was, I think, in my <a href="/outside-in-tdd">Outside-in TDD Pluralsight course</a>, in which I also discuss the so-called <em>Gollum style</em> variation. I don't think, however, that I've ever written an article explicitly about this topic. The current text attempts to rectify that omission. </p> <h3 id="a66b04a5812b4a84ba3a60a8609e58be"> Coverage <a href="#a66b04a5812b4a84ba3a60a8609e58be" title="permalink">#</a> </h3> <p> Programmers new to unit testing often struggle with identifying useful test cases. I sometimes see people writing redundant unit tests, while, on the other hand, forgetting to add important test cases. How do you know which test cases to add, and how do you know when you've added enough? </p> <p> I may return to the first question in another article, but in this, I wish to address the second question. How do you know that you have a sufficient set of test cases? </p> <p> You may think that this is a question of turning on code coverage. Surely, if you have <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">100% code coverage</a>, that's sufficient? </p> <p> It's not. Consider this simple class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">capacity</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;<span style="color:#1f377f;">capacity</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="color:#1f377f;">reservation</span>.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class implements the (simplified) decision logic for an online restaurant reservation system. The <code>CanAccept</code> method has a cyclomatic complexity of 2, so it should be easy to cover with a pair of unit tests: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWithNoPriorReservations</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>[0],&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); } [<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptOnInsufficientCapacity</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;7&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">False</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> These two tests together completely cover the <code>CanAccept</code> method: </p> <p> <img src="/content/binary/coverage-of-can-accept-method.png" alt="Screen shot showing that the CanAccept method is 100% covered."> </p> <p> You'd think that this is a sufficient number of test cases of the method, then. </p> <h3 id="0ac61ef87e3f4e738475e97179242db5"> As the Devil reads the Bible <a href="#0ac61ef87e3f4e738475e97179242db5" title="permalink">#</a> </h3> <p> In Scandinavia we have an idiom that <a href="https://www.kentbeck.com">Kent Beck</a> (who's worked with Norwegian companies) has also encountered: <blockquote> <p> "TIL: "like the devil reads the Bible"--meaning someone who carefully reads a book to subvert its intent" </p> <footer><cite><a href="https://twitter.com/kentbeck/status/651817458857320449">Kent Beck</a></cite></footer> </blockquote> We have the same saying in Danish, and the Swedes also use it. </p> <p> If you think of a unit test suite as an executable specification, you may consider if you can follow the specification to the letter while intentionally introduce a defect. You can easily do that with the above <code>CanAccept</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(Capacity&nbsp;&lt;=&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="color:#1f377f;">reservation</span>.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> This still passes both tests, and still has a code coverage of 100%, yet it's 'obviously' wrong. </p> <p> Can you spot the difference? </p> <p> Instead of a <em>less-than</em> comparison, it now uses a <em>less-than-or-equal</em> comparison. You could easily, inadvertently, make such a mistake while programming. It belongs in the category of <em>off-by-one errors</em>, which is one of the most common type of bugs. </p> <p> This is, in a nutshell, the Devil's Advocate technique. The intent isn't to break the software by sneaking in defects, but to explore how effectively the test suite detects bugs. In the current (simplified) example, the effectiveness of the test suite isn't impressive. </p> <h3 id="d6e4c657adec4cc1bb6d10af351a415f"> Add test cases <a href="#d6e4c657adec4cc1bb6d10af351a415f" title="permalink">#</a> </h3> <p> The problem introduced by the Devil's Advocate is an edge case. If the reservation under consideration fits the restaurant's remaining capacity, but entirely consumes it, the <code>MaîtreD</code> class should still accept it. Currently, however, it doesn't. </p> <p> It'd seem that the obvious solution is to 'fix' the unit test: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWithNoPriorReservations</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;10 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>[0],&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> Changing the requested <code>Quantity</code> to <code>10</code> does, indeed, cause the test to fail. </p> <h3 id="26be7b38248c4dcba5134eb4529d8214"> Beyond mutation testing <a href="#26be7b38248c4dcba5134eb4529d8214" title="permalink">#</a> </h3> <p> Until this point, you may think that the Devil's Advocate just looks like <em>an ad-hoc, informally-specified, error-prone, manual version of half of <a href="https://en.wikipedia.org/wiki/Mutation_testing">mutation testing</a></em>. So far, the change I made above could also have been made during mutation testing. </p> <p> What I sometimes do with the Devil's Advocate technique is to experiment with other, less heuristically driven changes. For instance, based on my knowledge of the existing test cases, it's not too difficult to come up with this change: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(<span style="color:#1f377f;">reservation</span>.Quantity&nbsp;!=&nbsp;10) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> That's an even simpler implementation than the original, but obviously wrong. </p> <p> This should prompt you to add at least one other test case: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4)] [<span style="color:#2b91af;">InlineData</span>(10)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWithNoPriorReservations</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">quantity</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>[0],&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> Notice that I converted the test to a parametrised test. This breaks the Devil's latest attempt, while the original implementation passes all tests. </p> <p> The Devil, not to be outdone, now switches tactics and goes after the <code>reservations</code> instead: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;!<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Any</span>(); }</pre> </p> <p> This still passes all tests, including the new test case. This indicates that you'll need to add at least one test case with existing reservations, but where there's still enough capacity to accept another reservation: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWithOnePriorReservation</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;4&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> This new test fails, prompting you to correct the implementation of <code>CanAccept</code>. The Devil, however, can do this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;!=&nbsp;7; }</pre> </p> <p> This is still not correct, but passes all tests. It does, however, look like you're getting closer to a proper implementation. </p> <h3 id="9b955ad4a2084823a9eeb668415eb696"> Reverse Transformation Priority Premise <a href="#9b955ad4a2084823a9eeb668415eb696" title="permalink">#</a> </h3> <p> If you find this process oddly familiar, it's because it resembles the <a href="https://blog.cleancoder.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html">Transformation Priority Premise</a> (TPP), just reversed. <blockquote> <p> “As the tests get more specific, the code gets more generic.” </p> <footer><cite><a href="https://blog.cleancoder.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html">Robert C. Martin</a></cite></footer> </blockquote> </p> <p> When I test-drive code, I often try to follow the TPP, but when I review code with tests, the code and the tests are already in place, and it's my task to assess both. </p> <p> Applying the Devil's Advocate review technique to <code>CanAccept</code>, it seems as though I'm getting closer to a proper implementation. It does, however, require more tests. As your next move you may, for instance, consider parametrising the test case that verifies what happens when capacity is insufficient: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(7)] [<span style="color:#2b91af;">InlineData</span>(8)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptOnInsufficientCapacity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">False</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> That doesn't help much, though, because this passes all tests: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;&lt;&nbsp;7; }</pre> </p> <p> Compared to the initial, 'desired' implementation, there's at least two issues with this code: <ul> <li>It doesn't consider <code>reservation.Quantity</code></li> <li>It doesn't take into account the <code>Capacity</code> of the restaurant</li> </ul> This indicates that you're going to have to add more test cases, varying both <code>reservation.Quantity</code> and <code>Capacity</code>. The happy-path test cases already varies <code>reservation.Quantity</code> a bit, but <code>CanAcceptOnInsufficientCapacity</code> does not, so perhaps you can follow the TPP by varying <code>reservation.Quantity</code> in that method as well: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(&nbsp;1,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;2,&nbsp;&nbsp;9)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;3,&nbsp;&nbsp;8)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;&nbsp;8)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;5,&nbsp;&nbsp;6)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;6,&nbsp;&nbsp;5)] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;&nbsp;1)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptOnInsufficientCapacity</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>,&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">quantity</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">False</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> This makes it harder for the Devil to come up with a malevolent implementation. Harder, but not impossible. </p> <p> It seems clear that since all test cases still use a hard-coded capacity, it ought to be possible to write an implementation that ignores the <code>Capacity</code>, but at this point I don't see a simple way to avoid looking at <code>reservation.Quantity</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="color:#1f377f;">reservation</span>.Quantity&nbsp;&lt;&nbsp;11; }</pre> </p> <p> This implementation passes all the tests. The last batch of test cases forced the Devil to consider <code>reservation.Quantity</code>. This strongly implies that if you vary <code>Capacity</code> as well, the proper implementation out to emerge. </p> <h3 id="e3ed81c93d5847039ea53e7acd978d99"> Diminishing returns <a href="#e3ed81c93d5847039ea53e7acd978d99" title="permalink">#</a> </h3> <p> What happens, then, if you add just one test case with a different <code>Capacity</code>? </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(&nbsp;1,&nbsp;10,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;2,&nbsp;&nbsp;9,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;3,&nbsp;&nbsp;8,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;&nbsp;7,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;&nbsp;8,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;5,&nbsp;&nbsp;6,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;6,&nbsp;&nbsp;5,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;&nbsp;1,&nbsp;10)] [<span style="color:#2b91af;">InlineData</span>(&nbsp;1,&nbsp;&nbsp;1,&nbsp;&nbsp;1)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptOnInsufficientCapacity</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">capacity</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">quantity</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">False</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> Notice that I just added one test case with a <code>Capacity</code> of <code>1</code>. </p> <p> You may think that this is about where the Devil ought to capitulate, but not so. This passes all tests: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">r</span>&nbsp;<span style="color:#8f08c4;">in</span>&nbsp;<span style="color:#1f377f;">reservations</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">r</span>.Quantity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">break</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="color:#1f377f;">reservation</span>.Quantity&nbsp;&lt;=&nbsp;Capacity; }</pre> </p> <p> Here you may feel the urge to protest. So far, all the Devil's Advocate implementations have been objectively <em>simpler</em> than the 'desired' implementation because it has involved fewer elements and has had a lower or equivalent <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a>. This new attempt to circumvent the specification seems more complex. </p> <p> It's also seems clearly ill-intentioned. Recall that the intent of the Devil's Advocate technique isn't to 'cheat' the unit tests, but rather to explore how well the test describe the desired behaviour of the system. The motivation is that it's easy to make off-by-one errors like inadvertently use <code>&lt;=</code> instead of <code>&lt;</code>. It doesn't seem quite as reasonable that a well-intentioned programmer accidentally would leave behind an implementation like the above. </p> <p> You can, however, make it <em>look</em> less complicated: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Quantity).<span style="color:#74531f;">FirstOrDefault</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="color:#1f377f;">reservation</span>.Quantity&nbsp;&lt;=&nbsp;Capacity; }</pre> </p> <p> You could argue that this still looks intentionally wrong, but I've seen much code that looks like this. It seems to me that there's a kind of programmer who seems generally uncomfortable thinking in collections; they seem to subconsciously gravitate towards code that deals with singular objects. Code that attempts to get 'the' value out of a collection is, unfortunately, not that uncommon. </p> <p> Still, you might think that at this point, you've added enough test cases. That's reasonable. </p> <p> The Devil's Advocate technique isn't an <em>algorithm</em>; it has no deterministic exit criterion. It's just a heuristic that I use to explore the quality of tests. There comes a point where subjectively, I judge that the test cases <em>sufficiently</em> describe the desired behaviour. </p> <p> You may find that we've reached that point now. You could, for example, argue that in order to calculate <code>reservedSeats</code>, <code>reservations.Sum(r =&gt; r.Quantity)</code> is simpler than <code>reservations.Select(r =&gt; r.Quantity).FirstOrDefault()</code>. I'd be inclined to agree. </p> <p> There's diminishing returns to the Devil's Advocate technique. Once you find that the gains from insisting on intentionally pernicious implementations are smaller than the effort required to add more test cases, it's time to stop and commit to the test cases now in place. </p> <h3 id="609ddb35ae364efbbfb7965a646d857e"> Test case variability <a href="#609ddb35ae364efbbfb7965a646d857e" title="permalink">#</a> </h3> <p> Tests specify desired behaviour. If the tests contain less variability than the code they cover, then how can you be certain that the implementation code is correct? </p> <p> The discussion now moves into territory where I usually exercise a great deal of judgement. Read the following for inspiration, not as rigid instructions. My intent with the following is not to imply that you must always go to like extremes, but simply to demonstrate what you <em>can</em> do. Depending on circumstances (such as the cost of a defect in production), I may choose to do the following, and sometimes I may choose to skip it. </p> <p> If you consider the original implementation of <code>CanAccept</code> at the top of the article, notice that it works with <code>reservations</code> of indefinite size. If you think of <code>reservations</code> as a finite collection, it can contain zero, one, two, ten, or hundreds of elements. Yet, no test case goes beyond a single existing reservation. This is, I think, a disconnect. The tests come not even close to the degree of variability that the method can handle. If this is a piece of mission-critical software, that could be a cause for concern. </p> <p> You should add some test cases where there's two, three, or more existing reservations. People often don't do that because it seems that you'd now have to write a test method that exercises one or more test cases with two existing reservations: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWithTwoPriorReservations</span>() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;4&nbsp;},&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;1&nbsp;}&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> While this method now covers the two-existing-reservations test case, you need one to cover the three-existing-reservations test case, and so on. This seems repetitive, and probably bothers you at more than one level: <ul> <li>It's just plain tedious to have to add that kind of variability</li> <li>It seems to violate the <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY principle</a></li> </ul> I don't hold the DRY principle as an absolute that must always be followed, but it often indicates a maintainability problem. I think this is the case here, because the new <code>CanAcceptWithTwoPriorReservations</code> test method looks a lot like the previous <code>CanAcceptWithOnePriorReservation</code> method. If someone makes changes to the <code>MaîtreD</code> class, they would have to go and revisit all those test methods. </p> <p> What you can do instead is to parametrise the key values of the collection(s) in question. While you can't put collections of objects in <code>[InlineData]</code> attributes, you <em>can</em> put arrays of constants. For existing reservations, the key values are the quantities, so supply an array of integers as a test argument: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0])] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0])] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4,&nbsp;1&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(&nbsp;2,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;1,&nbsp;3,&nbsp;2&nbsp;})] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWhenCapacityIsSufficient</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>,&nbsp;<span style="color:blue;">int</span>[]&nbsp;<span style="color:#1f377f;">reservationQantities</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">quantity</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservationQantities</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">q</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">q</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>(<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> This single test method replaces the previous three 'happy path' test methods. The first four <code>[InlineData]</code> annotations reproduce the previous test cases, whereas the fifth <code>[InlineData]</code> annotation adds a new test case with four existing reservations. </p> <p> I gave the method a new name to better reflect the more general nature of it. </p> <p> Notice that the <code>CanAcceptWhenCapacityIsSufficient</code> method uses <code>Select</code> to turn the array of integers into a collection of <code>Reservation</code> objects. </p> <p> You may think that I cheated, since I didn't supply any other values, such as the <code>Date</code> property, to the existing reservations. This is easily addressed: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0])] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0])] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4,&nbsp;1&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(&nbsp;2,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;1,&nbsp;3,&nbsp;2&nbsp;})] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWhenCapacityIsSufficient</span>(<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>,&nbsp;<span style="color:blue;">int</span>[]&nbsp;<span style="color:#1f377f;">reservationQantities</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">date</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:#1f377f;">date</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">quantity</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>:&nbsp;10); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservationQantities</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">q</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">q</span>,&nbsp;Date&nbsp;=&nbsp;<span style="color:#1f377f;">date</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>(<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> The only change compared to before is that <code>date</code> is now a variable assigned not only to <code>reservation</code>, but also to all the <code>Reservation</code> objects in <code>reservations</code>. </p> <h3 id="0564ebd7cafc44f4ba6ad017e3f0d0ce"> Towards property-based testing <a href="#0564ebd7cafc44f4ba6ad017e3f0d0ce" title="permalink">#</a> </h3> <p> Looking at a test method like <code>CanAcceptWhenCapacityIsSufficient</code> it should bother you that the <code>capacity</code> is still hard-coded. Why don't you make that a test argument as well? </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;&nbsp;4,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0])] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;10,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0])] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4,&nbsp;1&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(10,&nbsp;&nbsp;2,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;1,&nbsp;3,&nbsp;2&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(20,&nbsp;10,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;2,&nbsp;2,&nbsp;2&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(20,&nbsp;&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;2,&nbsp;4,&nbsp;1,&nbsp;3,&nbsp;3&nbsp;})] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWhenCapacityIsSufficient</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">capacity</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>[]&nbsp;<span style="color:#1f377f;">reservationQantities</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">date</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:#1f377f;">date</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">quantity</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservationQantities</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">q</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">q</span>,&nbsp;Date&nbsp;=&nbsp;<span style="color:#1f377f;">date</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>(<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> The first five <code>[InlineData]</code> annotations just reproduce the test cases that were already present, whereas the bottom two annotations are new test cases with another <code>capacity</code>. </p> <p> How do I come up with new test cases? It's easy: In the happy-path case, the sum of existing reservation quantities, plus the requested quantity, must be less than or equal to the <code>capacity</code>. </p> <p> It sometimes helps to slightly reframe the test method. If you allow the collection of existing reservations to be the most variable element in the test method, you can express the other values relative to that input. For example, instead of supplying the <code>capacity</code> as an absolute number, you can express a test case's capacity in relation to the existing reservations: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(6,&nbsp;&nbsp;4,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0])] [<span style="color:#2b91af;">InlineData</span>(0,&nbsp;10,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0])] [<span style="color:#2b91af;">InlineData</span>(2,&nbsp;&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(1,&nbsp;&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4,&nbsp;1&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(0,&nbsp;&nbsp;2,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;1,&nbsp;3,&nbsp;2&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(2,&nbsp;10,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;2,&nbsp;2,&nbsp;2&nbsp;})] [<span style="color:#2b91af;">InlineData</span>(1,&nbsp;&nbsp;4,&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;2,&nbsp;4,&nbsp;1,&nbsp;3,&nbsp;3&nbsp;})] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWhenCapacityIsSufficient</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">capacitySurplus</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;<span style="color:#1f377f;">quantity</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>[]&nbsp;<span style="color:#1f377f;">reservationQantities</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">date</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:#1f377f;">date</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">quantity</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservationQantities</span>.<span style="color:#74531f;">Sum</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">capacity</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="color:#1f377f;">quantity</span>&nbsp;+&nbsp;<span style="color:#1f377f;">capacitySurplus</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservationQantities</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">q</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">q</span>,&nbsp;Date&nbsp;=&nbsp;<span style="color:#1f377f;">date</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>(<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> Notice that the value supplied as a test argument is now named <code>capacitySurplus</code>. This represents the surplus capacity for each test case. For example, in the first test case, the <code>capacity</code> was previously supplied as the absolute number <code>10</code>. The requested quantity is <code>4</code>, and since there's no prior reservations in that test case, the capacity surplus, after accepting the reservation, is <code>6</code>. </p> <p> Likewise, in the second test case, the requested quantity is <code>10</code>, and since the absolute capacity is also <code>10</code>, when you reframe the test case, the surplus capacity, after accepting the reservation, is <code>0</code>. </p> <p> This seems odd if you aren't used to it. You'd probably intuitively think of a restaurant's <code>Capacity</code> as 'the most absolute' number, in that it's often a number that originates from physical constraints. </p> <p> When you're looking for test cases, however, you aren't looking for test cases for a particular restaurant. You're looking for test cases for an arbitrary restaurant. In other words, you're looking for test inputs that belong to the same <em>equivalence class</em>. </p> <h3 id="174e2338027e4f3ca0b84dd0fb6adc5f"> Property-based testing <a href="#174e2338027e4f3ca0b84dd0fb6adc5f" title="permalink">#</a> </h3> <p> I haven't explicitly stated this yet, but both the <code>capacity</code> and each reservation <code>Quantity</code> should be a positive number. This should really have been <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">captured as a proper domain object</a>, but I chose to keep these values as primitive integers in order to not complicate the example too much. </p> <p> If you look at the test parameters for the latest incarnation of <code>CanAcceptWhenCapacityIsSufficient</code>, you may now observe the following: <ul> <li><code>capacitySurplus</code> can be an arbitrary non-negative number</li> <li><code>quantity</code> can be an arbitrary positive number</li> <li><code>reservationQantities</code> can be an arbitrary array of positive numbers, including the empty array</li> </ul> This isn't too hard to express with, say, <a href="https://fscheck.github.io/FsCheck">FsCheck</a> (2.14.0): </p> <p> <pre>[<span style="color:#2b91af;">Property</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;<span style="color:#74531f;">CanAcceptWhenCapacityIsSufficient</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">NonNegativeInt</span>&nbsp;<span style="color:#1f377f;">capacitySurplus</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">PositiveInt</span>&nbsp;<span style="color:#1f377f;">quantity</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">PositiveInt</span>[]&nbsp;<span style="color:#1f377f;">reservationQantities</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">date</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;8,&nbsp;30); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservation</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:#1f377f;">date</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">quantity</span>.Item &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservationQantities</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">x</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">x</span>.Item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">capacity</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="color:#1f377f;">quantity</span>.Item&nbsp;+&nbsp;<span style="color:#1f377f;">capacitySurplus</span>.Item; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">sut</span>&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(<span style="color:#1f377f;">capacity</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#1f377f;">reservationQantities</span>.<span style="color:#74531f;">Select</span>(<span style="color:#1f377f;">q</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;{&nbsp;Quantity&nbsp;=&nbsp;<span style="color:#1f377f;">q</span>.Item,&nbsp;Date&nbsp;=&nbsp;<span style="color:#1f377f;">date</span>&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">actual</span>&nbsp;=&nbsp;<span style="color:#1f377f;">sut</span>.<span style="color:#74531f;">CanAccept</span>(<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#1f377f;">reservation</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.<span style="color:#74531f;">True</span>(<span style="color:#1f377f;">actual</span>); }</pre> </p> <p> This refactoring takes advantage of FsCheck's built-in wrapper types <code>NonNegativeInt</code> and <code>PositiveInt</code>. If you'd like an introduction to FsCheck, you could watch my <a href="/property-based-testing-intro">Introduction to Property-based Testing with F#</a> Pluralsight course. </p> <p> By default, FsCheck runs each property 100 times, so now, instead of seven test cases, you now have 100. </p> <h3 id="1eddf5bb91324b18880c78d1f1825ea6"> Limits to the Devil's Advocate technique <a href="#1eddf5bb91324b18880c78d1f1825ea6" title="permalink">#</a> </h3> <p> There's a limit to the Devil's Advocate technique. Unless you're working with <a href="/2015/02/23/property-based-testing-without-a-property-based-testing-framework">a problem where you can exhaust the entire domain of possible test cases</a>, your testing strategy is always going to be a sampling strategy. You run your automated tests with either hard-coded values or randomly generated values, but regardless, a test run isn't going to cover all possible input combinations. </p> <p> For example, a truly hostile Devil could make this change to the <code>CanAccept</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:#74531f;">CanAccept</span>(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;<span style="color:#1f377f;">reservations</span>,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;<span style="color:#1f377f;">reservation</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(<span style="color:#1f377f;">reservation</span>.Quantity&nbsp;==&nbsp;3953911) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;=&nbsp;<span style="color:#1f377f;">reservations</span>.<span style="color:#74531f;">Sum</span>(<span style="color:#1f377f;">r</span>&nbsp;=&gt;&nbsp;<span style="color:#1f377f;">r</span>.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:#1f377f;">reservedSeats</span>&nbsp;+&nbsp;<span style="color:#1f377f;">reservation</span>.Quantity&nbsp;&lt;=&nbsp;Capacity; }</pre> </p> <p> Even if you increase the number of test cases that FsCheck generates to, say, 100,000, it's unlikely to find the poisonous branch. The chance of randomly generating a <code>quantity</code> of <em>exactly</em> <code>3953911</code> isn't that great. </p> <p> The Devil's Advocate technique doesn't guarantee that you'll have enough test cases to protect yourself against all sorts of odd defects. It does, however, still work well as an analysis tool to figure out if there's 'enough' test cases. </p> <h3 id="6ad7a48fd0c04f91af236d99f0722617"> Conclusion <a href="#6ad7a48fd0c04f91af236d99f0722617" title="permalink">#</a> </h3> <p> The Devil's Advocate technique is a heuristic you can use to evaluate whether more test cases would improve confidence in the test suite. You can use it to review existing (test) code, but you can also use it as inspiration for new test cases that you should consider adding. </p> <p> The technique is to deliberately implement the system under test incorrectly. The more incorrect you can make it, the more test cases you'll be likely to have to add. </p> <p> When there's only a few test cases, you can probably get away with a decidedly unsound implementation that still passes all tests. These are often simpler than the 'intended' implementation. In this phase of applying the heuristic, this clearly demonstrates the need for more test cases. </p> <p> At a later stage, you'll have to go deliberately out of your way to produce a wrong implementation that still passes all tests. When that happens, it may be time to stop. </p> <p> The intent of the technique is to uncover how many test cases you need to protect against common defects in the future. Thus, it's not a measure of <em>current</em> code coverage. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="fd53c72c360b42999b87c87649460e78"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#fd53c72c360b42999b87c87649460e78">#</a></div> <div class="comment-content"> <blockquote> <p> When there's only a few test cases, you can probably get away with a decidedly unsound implementation that still passes all tests. These are often simpler than the 'intended' implementation. In this phase of applying the heuristic, this clearly demonstrates the need for more test cases. </p> <p> At a later stage, you'll have to go deliberately out of your way to produce a wrong implementation that still passes all tests. When that happens, it may be time to stop. </p> </blockquote> <p> I like to think of this behavior as a phrase transition. </p> <blockquote> Unless you're working with a problem where you can exhaust the entire domain of possible test cases, your testing strategy is always going to be a sampling strategy. </blockquote> <p> I agree with this in practice, but it is not always true in theory. A counter eaxample is <a href="https://en.wikipedia.org/wiki/Polynomial_interpolation">polynomial interpolation</a>. </p> <p> Normally we think of a polynomial in an indeterminate <code>x</code> of degree <code>n</code> as being specified by a list of <code>n + 1</code> coefficients, where the <code>i</code>th coefficient is the coefficient of <code>x<sup>i</sup></code>. Evaluating this polynomial given a value for <code>x</code> is easy; it just involves exponentiation, multiplication, and addition. Polynomial evaluation has a conceptual inverse called polynomial interpolation. In this direction, the input is evaluations at <code>n + 1</code> points in "general position" and the output is the <code>n + 1</code> coefficients. For example, a line is a polynomial of degree <code>1</code> and two points are in general position if they are not the same point. This is commonly expressed the phrase "Any two (distinct) points defines a line." Three points are in general position if they are not co-linear, where co-linear means that all three points are on the same line. In general, <code>n + 1</code> points are in general position if they are not all on the same polynomial of degree <code>n</code>. <p> <p> Anyway, here is the point. If a pure function is known to implement some polynomial of degree (at most) <code>n</code>, then even if the domain is infinite, there exists <code>n + 1</code> inputs such that it is sufficient to test this function for correctness on those inputs. </p> <p> This is why I think the phrase transition in the Devil's advocate testing is critical. There is some objective measure of complexity of the function under test (such as cyclomatic complexity), and we have an intuitive sense that a certain number of tests is sufficient for testing functions with that complexity. If the Devil is allowed to add monomials to the polynomial (or, heaven forbid, modify the implementation so that it is not a polynomial), then any finite number of tests can be circumvented. If instead the Devil is only allowed to modify the coefficients of the polynomial, then we have a winning strategy. </p> <blockquote> Here you may feel the urge to protest. So far, all the Devil's Advocate implementations have been objectively simpler than the 'desired' implementation because it has involved fewer elements and has had a lower or equivalent cyclomatic complexity. This new attempt to circumvent the specification seems more complex. </blockquote> <p> I think it would be exceedingly intersting if you can formally define what you mean here by "objectively". In the case of a polynomial (and speaking slightly roughly), changing the "first" nonzero coefficient to <code>0</code> decreases the complexity (i.e. the degree of the polynomial) while any other change to that coefficient or any change to any other coefficient maintains the complexity. </p> </div> <div class="comment-date">2019-10-25 01:32 UTC</div> </div> <div class="comment" id="cb15452b8c96429998efd50b67373da3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cb15452b8c96429998efd50b67373da3">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. What I meant by <em>objectively simpler</em> I partially explain in the same paragraph. I consider cyclomatic complexity one of hardly any useful measurements in software development. As I also imply in the article, I consider Robert C. Martin's <em>Transformation Priority Premise</em> to include a good ranking of code constructs, e.g. that using a constant is simpler than using a variable, and so on. </p> <p> I don't think you need to reach for polynomial interpolation in order to make your point. Just consider a function that returns a constant value, like this one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Foo</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>; }</pre> </p> <p> You can make a similar argument about this function: You only need a single test value in order to demonstrate that it works as intended. I suppose you could view that as a zero-degree polynomial. </p> <p> Beyond what you think of as the <em>phase transition</em> I sometimes try to see what happens if I slightly <em>increase</em> the complexity of a function. For the <code>Foo</code> function, it could be a change like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;<span style="color:#74531f;">Foo</span>(<span style="color:blue;">int</span>&nbsp;<span style="font-weight:bold;color:#1f377f;">i</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">if</span>&nbsp;(<span style="font-weight:bold;color:#1f377f;">i</span>&nbsp;&lt;&nbsp;-1000) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;color:#8f08c4;">return</span>&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>; }</pre> </p> <p> Unless you just happened to pick a number less than <code>-1000</code> for your test value, your test will not discover such a change. </p> <p> Your argument attempts to guard against that sort of change by assuming that we can somehow 'forbid' a change from a polynomial to something irregular. Real code doesn't work that way. Real code is rarely a continuous function, but rather discrete. That's the reason we have a concept such as <em>edge case</em>, because code branches at discrete values. </p> <p> A polynomial is a single function, regardless of degree. Implemented in code, it'll have a cyclomatic complexity of 1. That may not even be worth testing, because you'd essentially only be reproducing the implementation code in your test. </p> <p> The purpose of the Devil's Advocate technique isn't to demonstrate correctness; that's what unit tests are for. The purpose of the Devil's Advocate technique is to critique the tests. </p> <p> In reality, I never imagine that some malicious developer gains access to the source code. On the other hand, we all make mistakes, and I try to imagine what a likely mistake might look like. </p> </div> <div class="comment-date">2019-10-26 3:57 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. 10x developers https://blog.ploeh.dk/2019/09/30/10x-developers 2019-09-30T06:56:00+00:00 Mark Seemann <div id="post"> <p> <em>Do 10x developers exist? I believe that they do, but not like you may think.</em> </p> <p> The notion that some software developers are ten times (10x) as productive as 'normal' developers is decades old. Once in a while, the discussion resurfaces. It's a controversial subject, but something I've been thinking about for years, so I thought that I'd share my perspective because I don't see anyone else arguing from this position. </p> <p> While I'll try to explain my reasoning, I'll make no attempt at passing this off as anything but my current, subjective viewpoint. Please leave a comment if you have something to add. </p> <h3 id="11f6ec960f694758892bff549eb79d59"> Perspective <a href="#11f6ec960f694758892bff549eb79d59" title="permalink">#</a> </h3> <p> Meet Yohan. You've probably had a colleague like him. He's one of those software developers who gets things done, who never says no when the business asks him to help them out, who always respond with a smile to any request. </p> <p> I've had a few colleagues like Yohan in my career. It can be enlightening overhearing non-technical stakeholders discuss software developers: </p> <p> <strong>Alice:</strong> Yohan is such a dear; he helped me out with that feature on the web site, you know... </p> <p> <strong>Bob:</strong> Yes, he's a real go-getter. All the other programmers just say no and look pissed when I approach them about anything. </p> <p> <strong>Alice:</strong> Yohan always says yes, and he gets things done. He's a real 10x developer. </p> <p> <strong>Bob:</strong> We're so lucky we have him... </p> <p> Overhearing such a conversation can be frustrating. Yohan is your colleague, and you've just about had enough of him. Yohan is one of those developers who'll surround all code with a <code>try-catch</code> block, because then there'll be no exceptions in production. Yohan will make changes directly to the production system and tell no-one. Yohan will copy and paste code. Yohan will put business logic in database triggers, or rewrite logs, or use email as a messaging system, or call, parse, and run HTML-embedded JavaScript code on back-end servers. All 'because it's faster and provides more business value.' </p> <p> Yohan is a 10x developer. </p> <p> You, and the rest of your team, get nothing done. </p> <p> You get nothing done because you waste all your time cleaning up the trail of garbage and technical debt Yohan leaves in his wake. </p> <p> Business stakeholders may view Yohan as being orders of magnitude more productive than other developers, because most programming work is invisible and intangible. Whether or not someone is a 10x developer is highly subjective, and depends on perspective. </p> <h3 id="57c36b775bca4e848cadf50182028191"> Context <a href="#57c36b775bca4e848cadf50182028191" title="permalink">#</a> </h3> <p> The notion that some people are orders of magnitude more productive than the 'baseline' programmer has other problems. It implicitly assumes that a 'baseline' programmer exists in the first place. Modern software development, however, is specialised. </p> <p> As an example, I've been doing test-driven, ASP.NET-based C# server-side enterprise development for decades. Drop me into a project with my favourite stack and watch me go. On the other hand, try asking me to develop a game for the Sony PlayStation, and watch me stall. </p> <p> Clearly, then, I'm a 10x developer, for the tautological reason that I'm much better at the things that I'm good at than the things I'm not good at. </p> <p> Even the greatest <a href="https://en.wikipedia.org/wiki/R_(programming_language)">R</a> developer is unlikely to be of much help on your next <a href="https://en.wikipedia.org/wiki/COBOL">COBOL</a> project. </p> <p> As always, context matters. You can be a great programmer in a particular context, and suck in another. </p> <p> This isn't limited to technology stacks. Some people prefer co-location, while others work best by themselves. Some people are detail-oriented, while others like to look at the big picture. Some people do their best work early in the morning, and others late at night. </p> <p> And some teams of 'mediocre' programmers outperform all-star teams. (This, incidentally, is a phenomenon also <a href="https://en.wikipedia.org/wiki/UEFA_Euro_1992">sometimes seen</a> in professional <a href="https://en.wikipedia.org/wiki/Association_football">Soccer</a>.) </p> <h3 id="34e113a19c8040b0bcb38f7abf1b532d"> Evidence <a href="#34e113a19c8040b0bcb38f7abf1b532d" title="permalink">#</a> </h3> <p> Unfortunately, as I explain in my <a href="https://cleancoders.com/video-details/humane-code-real-episode-1">Humane Code</a> video, I believe that you can't measure software development productivity. Thus, the notion of a 10x developer is subjective. </p> <p> The original idea, however, is decades old, and seems, at first glance, to originate in a 'study'. If you're curious about its origins, I can't recommend <a href="http://bit.ly/leprechauns-of-software-engineering">The Leprechauns of Software Engineering</a> enough. In that book, Laurent Bossavit explains just how insubstantial the evidence is. </p> <p> If the evidence is so weak, then why does the idea that 10x developers exist keep coming back? </p> <h3 id="6988798f60bf4622808b58a3e7f55bce"> 0x developers <a href="#6988798f60bf4622808b58a3e7f55bce" title="permalink">#</a> </h3> <p> I think that the reason that the belief is recurring is that (subjectively) <em>it seems so evident</em>. Barring confirmation bias, I'm sure everyone has encountered a team member that never seemed to get anything done. </p> <p> I know that I've certainly had that experience from time to time. </p> <p> The first job I had, I hated. I just couldn't muster any enthusiasm for the work, and I'd postpone and drag out as long as possible even the simplest task. That wasn't very mature, but I was 25 and it was my first job, and I didn't know how to handle the situation I found myself in. I'm sure that my colleagues back then found that I didn't pull my part. I didn't, and I'm not proud of it, but it's true. </p> <p> I believe now that I was in the wrong context. It wasn't that I was incapable of doing the job, but at that time in my career, I absolutely loathed it, and for that reason, I wasn't productive. </p> <p> Another time, I had a colleague who seemed incapable of producing anything that helped us achieve our goals. I was concerned that I'd <a href="https://en.wikipedia.org/wiki/Bozo_bit">flipped the bozo bit</a> on that colleague, so I started to collect evidence. Our Git repository had few commits from that colleague, and the few that I could find I knew had been made in collaboration with another team member. We shared an office, and I had a pretty good idea about who worked together with whom when. </p> <p> This colleague spent a lot of time talking to other people. Us, other stakeholders, or any hapless victim who didn't escape in time. Based on these meetings and discussions, we'd hear about all sorts of ideas for improvements for our code or development process, but nothing would be implemented, and rarely did it have any relevance to what we were trying to accomplish. </p> <p> I've met programmers who get nothing done more than once. Sometimes, like the above story, they're boisterous bluffs, but most often, they just sit quietly in their corner and fidget with who knows what. </p> <p> Based on the above, mind you, I'm not saying that these people are necessarily incompetent (although I suspect that some are). They might also just find themselves in a wrong context, like I did in my first job. </p> <p> It seems clear to me, then, that there's such a thing as a <em>0x developer</em>. This is a developer who gets zero times (0x) as much done as the 'average' developer. </p> <p> For that reason it seems evident to me that 10x developers exist. Any developer who regularly manages to get code deployed to production is not only ten times, but infinitely more productive than 0x developers. </p> <p> It gets worse, though. </p> <h3 id="fe07476743704027acab9c7b949bc3a4"> −nx developers <a href="#fe07476743704027acab9c7b949bc3a4" title="permalink">#</a> </h3> <p> Not only is it my experience that 0x developers exist, I also believe that I've met more than one <em>−nx developer</em>. These are developers who are <em>minus n times</em> 'more' productive than the 'baseline' developer. In other words, they are software developers who have negative productivity. </p> <p> I've never met anyone who I suspected of deliberately sabotaging our efforts; they always seem well-meaning, but some people can produce more mess than three colleagues can clean up. Yohan, above, is such an archetype. </p> <p> One colleague I had, long ago, was so bad that the rest of the team deliberately compartmentalised him/her. We'd ask him/her to work on an isolated piece of the system, knowing that (s)he would be assigned to another project after four months. We then secretly planned to throw away the code once (s)he was gone, and rewrite it. I don't know if that was the right decision, but since we had padded all other estimates accordingly, we made our deadlines without more than the usual overruns. </p> <p> If you accept the assertion that −nx developers exist, then clearly, anyone who gets anything done at all is an <em>∞x developer</em>. </p> <h3 id="8257729b743641d0afa1c2ddbfe1b0df"> Summary <a href="#8257729b743641d0afa1c2ddbfe1b0df" title="permalink">#</a> </h3> <p> 10x developers exist, but not in the way that people normally interpret the term. </p> <p> 10x developers exist because there's great variability in (perceived) productivity. Much of the variability is context-dependent, so it's less clear if some people are just 'better at programming' than others. Still, when we consider that people like <a href="https://en.wikipedia.org/wiki/Linus_Torvalds">Linus Torvalds</a> exist, it seems compelling that this might be the case. </p> <p> Most of the variability, however, I think correlates with environment. Are you working in a technology stack with which you're comfortable? Do you like what you're doing? Do you like your colleagues? Do you like your hours? Do you like your working environment? </p> <p> Still, even if we could control for all of those variables, we might still find that some people get stuff done, and some people don't. The people who get anything done are ∞x developers. </p> <p> Employers and non-technical start-up founders sometimes look for the 10x unicorns, just like they look for <em>rock star developers</em>. <blockquote> <p> "To really confuse recruiters, someone should make a programming language called Rockstar." </p> <footer><cite><a href="https://twitter.com/paulstovell/status/1013960369465782273">Paul Stovell</a></cite></footer> </blockquote> The above tweet inspired <a href="http://www.dylanbeattie.net">Dylan Beattie</a> to create <a href="https://codewithrockstar.com">the Rockstar programming language</a>. </p> <p> Perhaps we should also create a <em>10x</em> programming language, so that we could put <em>certified Rockstar programmer, 10x developer</em> on our resumes. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="cf678a24cefb43b0a4b68bd6f50e10d8"> <div class="comment-author">Markus <a href="#cf678a24cefb43b0a4b68bd6f50e10d8">#</a></div> <div class="comment-content"> <p> Become a 10x developer today! </p> <p> Just a few days ago I heared first about the Rockstar language. Now I stubled over your post just to learn it's really important. So looked around for the show stopper: the <em>10x</em> programming language. And I found <a href="http://x10-lang.org/">X10</a>. It's there! So you can become at least a X10 developer. Perhaps that's enough for the next resume? </p> </div> <div class="comment-date">2020-05-30 18:18 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Unit testing wai applications https://blog.ploeh.dk/2019/09/23/unit-testing-wai-applications 2019-09-23T06:35:00+00:00 Mark Seemann <div id="post"> <p> <em>One way to unit test a wai application with the API provided by Network.Wai.Test.</em> </p> <p> I'm currently developing a REST API in <a href="https://www.haskell.org">Haskell</a> using <a href="https://haskell-servant.readthedocs.io">Servant</a>, and I'd like to test the HTTP API as well as the functions that I use to compose it. The Servant documentation, as well as the <em>servant</em> <a href="https://haskellstack.org">Stack</a> template, uses <a href="http://hackage.haskell.org/package/hspec">hspec</a> to drive the tests. </p> <p> I tried to develop my code with hspec, but I found it confusing and inflexible. It's possible that I only found it inflexible because I didn't understand it well enough, but I don't think you can argue with my experience of finding it confusing. </p> <p> I prefer a combination of <a href="https://hackage.haskell.org/package/HUnit">HUnit</a> and <a href="http://hackage.haskell.org/package/QuickCheck">QuickCheck</a>. It turns out that it's possible to test a <a href="http://hackage.haskell.org/package/wai">wai</a> application (including Servant) using only those test libraries. </p> <h3 id="1c7a7365bd0c425e85691625d00adcd0"> Testable HTTP requests <a href="#1c7a7365bd0c425e85691625d00adcd0" title="permalink">#</a> </h3> <p> When testing against the HTTP API itself, you want something that can simulate the HTTP traffic. That capability is provided by <a href="http://hackage.haskell.org/package/wai-extra/docs/Network-Wai-Test.html">Network.Wai.Test</a>. At first, however, it wasn't entirely clear to me how that library works, but I could see that the Servant-recommended <a href="http://hackage.haskell.org/package/hspec-wai/docs/Test-Hspec-Wai.html">Test.Hspec.Wai</a> is just a thin wrapper over <em>Network.Wai.Test</em> (notice how <a href="/2019/07/01/yes-silver-bullet">open source makes such research much easier</a>). </p> <p> It turns out that <em>Network.Wai.Test</em> enables you to run your tests in a <code>Session</code> monad. You can, for example, define a simple HTTP GET request like this: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.ByteString&nbsp;<span style="color:blue;">as</span>&nbsp;BS <span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.ByteString.Lazy&nbsp;<span style="color:blue;">as</span>&nbsp;LBS <span style="color:blue;">import</span>&nbsp;Network.HTTP.Types <span style="color:blue;">import</span>&nbsp;Network.Wai <span style="color:blue;">import</span>&nbsp;Network.Wai.Test <span style="color:#2b91af;">get</span>&nbsp;::&nbsp;<span style="color:blue;">BS</span>.<span style="color:blue;">ByteString</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Session</span>&nbsp;<span style="color:blue;">SResponse</span> get&nbsp;url&nbsp;=&nbsp;request&nbsp;$&nbsp;setPath&nbsp;defaultRequest&nbsp;{&nbsp;requestMethod&nbsp;=&nbsp;methodGet&nbsp;}&nbsp;url </pre> </p> <p> This <code>get</code> function takes a <code>url</code> and returns a <code>Session SResponse</code>. It uses the <code>defaultRequest</code>, so it doesn't set any specific HTTP headers. </p> <p> For HTTP POST requests, I needed a function that'd POST a JSON document to a particular URL. For that purpose, I had to do a little more work: </p> <p> <pre><span style="color:#2b91af;">postJSON</span>&nbsp;::&nbsp;<span style="color:blue;">BS</span>.<span style="color:blue;">ByteString</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">LBS</span>.<span style="color:blue;">ByteString</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Session</span>&nbsp;<span style="color:blue;">SResponse</span> postJSON&nbsp;url&nbsp;json&nbsp;=&nbsp;srequest&nbsp;$&nbsp;SRequest&nbsp;req&nbsp;json &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;req&nbsp;=&nbsp;setPath&nbsp;defaultRequest &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;requestMethod&nbsp;=&nbsp;methodPost &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;requestHeaders&nbsp;=&nbsp;[(hContentType,&nbsp;<span style="color:#a31515;">&quot;application/json&quot;</span>)]}&nbsp;url</pre> </p> <p> This is a little more involved than the <code>get</code> function, because it also has to supply the <code>Content-Type</code> HTTP header. If you don't supply that header with the <code>application/json</code> value, your API is going to reject the request when you attempt to post a string with a JSON object. </p> <p> Apart from that, it works the same way as the <code>get</code> function. </p> <h3 id="d726d432f53b4817b5dc9716a2fabc36"> Running a test session <a href="#d726d432f53b4817b5dc9716a2fabc36" title="permalink">#</a> </h3> <p> The <code>get</code> and <code>postJSON</code> functions both return <code>Session</code> values, so a test must run in the <code>Session</code> monad. This is easily done with Haskell's <code>do</code> notation; you'll see an example of that later in the article. </p> <p> First, however, you'll need a way to run a <code>Session</code>. <em>Network.Wai.Test</em> provides a function for that, called <code>runSession</code>. Besides a <code>Session a</code> value, though, it also requires an <code>Application</code> value. </p> <p> In my test library, I already have an <code>Application</code>, although it's running in <code>IO</code> (for reasons that'll take another article to explain): </p> <p> <pre><span style="color:#2b91af;">app</span>&nbsp;::&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;<span style="color:blue;">Application</span></pre> </p> <p> With this value, you can easily convert any <code>Session a</code> to <code>IO a</code>: </p> <p> <pre><span style="color:#2b91af;">runSessionWithApp</span>&nbsp;::&nbsp;<span style="color:blue;">Session</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;a runSessionWithApp&nbsp;s&nbsp;=&nbsp;app&nbsp;&gt;&gt;=&nbsp;runSession&nbsp;s</pre> </p> <p> The next step is to figure out how to turn an <code>IO a</code> into a test. </p> <h3 id="febab39aa10d4d78bfd0bc4d3d45ca8a"> Running a property <a href="#febab39aa10d4d78bfd0bc4d3d45ca8a" title="permalink">#</a> </h3> <p> You can turn an <code>IO a</code> into a <code>Property</code> with either <code>ioProperty</code> or <code>idempotentIOProperty</code>. I admit that the documentation doesn't make the distinction between the two entirely clear, but <code>ioProperty</code> sounds like the safer choice, so that's what I went for here. </p> <p> With <code>ioProperty</code> you now have a <code>Property</code> that you can turn into a <code>Test</code> using <code>testProperty</code> from <a href="http://hackage.haskell.org/package/test-framework-quickcheck2/docs/Test-Framework-Providers-QuickCheck2.html">Test.Framework.Providers.QuickCheck2</a>: </p> <p> <pre><span style="color:#2b91af;">appProperty</span>&nbsp;::&nbsp;(<span style="color:blue;">Functor</span>&nbsp;f,&nbsp;<span style="color:blue;">Testable</span>&nbsp;prop,&nbsp;<span style="color:blue;">Testable</span>&nbsp;(f&nbsp;<span style="color:blue;">Property</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;TestName&nbsp;-&gt;&nbsp;f&nbsp;(Session&nbsp;prop)&nbsp;-&gt;&nbsp;Test appProperty&nbsp;name&nbsp;= &nbsp;&nbsp;testProperty&nbsp;name&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(ioProperty&nbsp;.&nbsp;runSessionWithApp)</pre> </p> <p> The type of this function seems more cryptic than strictly necessary. What's that <code>Functor f</code> doing there? </p> <p> The way I've written the tests, each property receives input from QuickCheck in the form of function arguments. I could have given the <code>appProperty</code> function a more restricted type, to make it clearer what's going on: </p> <p> <pre><span style="color:#2b91af;">appProperty</span>&nbsp;::&nbsp;(<span style="color:blue;">Arbitrary</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a,&nbsp;<span style="color:blue;">Testable</span>&nbsp;prop) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;TestName&nbsp;-&gt;&nbsp;(a&nbsp;-&gt;&nbsp;Session&nbsp;prop)&nbsp;-&gt;&nbsp;Test appProperty&nbsp;name&nbsp;= &nbsp;&nbsp;testProperty&nbsp;name&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(ioProperty&nbsp;.&nbsp;runSessionWithApp)</pre> </p> <p> This is the same function, just with a more restricted type. It states that for any <code>Arbitrary a, Show a</code>, a test is a function that takes <code>a</code> as input and returns a <code>Session prop</code>. This restricts tests to take a single input value, which means that you'll have to write all those properties in tupled, uncurried form. You could relax that requirement by introducing a <code>newtype</code> and a type class with an instance that recursively enables curried functions. That's what <a href="http://hackage.haskell.org/package/hspec-wai/docs/Test-Hspec-Wai-QuickCheck.html">Test.Hspec.Wai.QuickCheck</a> does. I decided not to add that extra level of indirection, and instead living with having to write all my properties in tupled form. </p> <p> The <code>Functor f</code> in the above, relaxed type, then, is in actual use the Reader functor. You'll see some examples next. </p> <h3 id="0ebc8724e39149e88e5e71763b03d499"> Properties <a href="#0ebc8724e39149e88e5e71763b03d499" title="permalink">#</a> </h3> <p> You can now define some properties. Here's a simple example: </p> <p> <pre>appProperty&nbsp;<span style="color:#a31515;">&quot;responds&nbsp;with&nbsp;404&nbsp;when&nbsp;no&nbsp;reservation&nbsp;exists&quot;</span>&nbsp;$&nbsp;\rid&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;get&nbsp;$&nbsp;<span style="color:#a31515;">&quot;/reservations/&quot;</span>&nbsp;&lt;&gt;&nbsp;toASCIIBytes&nbsp;rid &nbsp;&nbsp;assertStatus&nbsp;404&nbsp;actual</pre> </p> <p> This is an inlined property, similar to how <a href="/2018/05/07/inlined-hunit-test-lists">I inline HUnit tests in test lists</a>. </p> <p> First, notice that the property is written as a lambda expression, which means that it fits the mould of <code>a -&gt; Session prop</code>. The input value <code>rid</code> (<em>reservationID</em>) is a <a href="http://hackage.haskell.org/package/uuid/docs/Data-UUID.html">UUID</a> value (for which an <code>Arbitrary</code> instance exists via <a href="http://hackage.haskell.org/package/quickcheck-instances">quickcheck-instances</a>). </p> <p> While the test runs in the <code>Session</code> monad, the <code>do</code> notation makes <code>actual</code> an <code>SResponse</code> value that you can then assert with <code>assertStatus</code> (from <em>Network.Wai.Test</em>). </p> <p> This property reproduces an interaction like this: </p> <p> <pre>&amp; curl -v http://localhost:8080/reservations/db38ac75-9ccd-43cc-864a-ce13e90a71d8 * Trying ::1:8080... * TCP_NODELAY set * Trying 127.0.0.1:8080... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 8080 (#0) &gt; GET /reservations/db38ac75-9ccd-43cc-864a-ce13e90a71d8 HTTP/1.1 &gt; Host: localhost:8080 &gt; User-Agent: curl/7.65.1 &gt; Accept: */* &gt; * Mark bundle as not supporting multiuse &lt; HTTP/1.1 404 Not Found &lt; Transfer-Encoding: chunked &lt; Date: Tue, 02 Jul 2019 18:09:51 GMT &lt; Server: Warp/3.2.27 &lt; * Connection #0 to host localhost left intact</pre> </p> <p> The important result is that the status code is <code>404 Not Found</code>, which is also what the property asserts. </p> <p> If you need more than one input value to your property, you have to write the property in tupled form: </p> <p> <pre>appProperty&nbsp;<span style="color:#a31515;">&quot;fails&nbsp;when&nbsp;reservation&nbsp;is&nbsp;POSTed&nbsp;with&nbsp;invalid&nbsp;quantity&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(ValidReservation&nbsp;r,&nbsp;NonNegative&nbsp;q)&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;invalid&nbsp;=&nbsp;r&nbsp;{&nbsp;reservationQuantity&nbsp;=&nbsp;<span style="color:blue;">negate</span>&nbsp;q&nbsp;} &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;postJSON&nbsp;<span style="color:#a31515;">&quot;/reservations&quot;</span>&nbsp;$&nbsp;encode&nbsp;invalid &nbsp;&nbsp;assertStatus&nbsp;400&nbsp;actual</pre> </p> <p> This property still takes a single input, but that input is a tuple where the first element is a <code>ValidReservation</code> and the second element a <code>NonNegative Int</code>. The <a href="/2019/09/02/naming-newtypes-for-quickcheck-arbitraries">ValidReservation newtype wrapper</a> ensures that <code>r</code> is a valid reservation record. This ensures that the property only exercises the path where the reservation quantity is zero or negative. It accomplishes this by negating <code>q</code> and replacing the <code>reservationQuantity</code> with that negative (or zero) number. </p> <p> It then encodes (with <a href="http://hackage.haskell.org/package/aeson">aeson</a>) the <code>invalid</code> reservation and posts it using the <code>postJSON</code> function. </p> <p> Finally it asserts that the HTTP status code is <code>400 Bad Request</code>. </p> <h3 id="ae5b3f7b07634799ad2af0d9a2ac668c"> Summary <a href="#ae5b3f7b07634799ad2af0d9a2ac668c" title="permalink">#</a> </h3> <p> After having tried using <em>Test.Hspec.Wai</em> for some time, I decided to refactor my tests to QuickCheck and HUnit. Once I figured out how <em>Network.Wai.Test</em> works, the remaining work wasn't too difficult. While there's little written documentation for the modules, the types (as usual) act as documentation. Using the types, and looking a little at the underlying code, I was able to figure out how to use the test API. </p> <p> You write tests against <em>wai</em> applications in the <code>Session</code> monad. You can then use <code>runSession</code> to turn the <code>Session</code> into an <code>IO</code> value. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Picture archivist in F# https://blog.ploeh.dk/2019/09/16/picture-archivist-in-f 2019-09-16T05:59:00+00:00 Mark Seemann <div id="post"> <p> <em>A comprehensive code example showing how to implement a functional architecture in F#.</em> </p> <p> This article shows how to implement the <a href="/2019/08/26/functional-file-system">picture archivist architecture described in a previous article</a>. In short, the task is to move some image files to directories based on their date-taken metadata. The architectural idea is to load a directory structure from disk into an in-memory tree, manipulate that tree, and use the resulting tree to perform the desired actions: </p> <p> <img src="/content/binary/functional-file-system-interaction.png" alt="A functional program typically loads data, transforms it, and stores it again."> </p> <p> Much of the program will manipulate the tree data, which is immutable. </p> <p> The previous article showed how to implement the <a href="/2019/09/09/picture-archivist-in-haskell">picture archivist architecture in Haskell</a>. In this article, you'll see how to do it in <a href="https://fsharp.org">F#</a>. This is essentially a port of the <a href="https://www.haskell.org">Haskell</a> code. </p> <h3 id="949a876ffec843e09d4faa5ae1c1b4c5"> Tree <a href="#949a876ffec843e09d4faa5ae1c1b4c5" title="permalink">#</a> </h3> <p> You can start by defining a <a href="https://en.wikipedia.org/wiki/Rose_tree">rose tree</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Tree&lt;&#39;a,&nbsp;&#39;b&gt;&nbsp;=&nbsp;Node&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a&nbsp;*&nbsp;Tree&lt;&#39;a,&nbsp;&#39;b&gt;&nbsp;list&nbsp;|&nbsp;Leaf&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;b</pre> </p> <p> If you wanted to, you could put all the <code>Tree</code> code in a reusable library, because none of it is coupled to a particular application, such as <a href="https://amzn.to/2V06Kji">moving pictures</a>. You could also write a comprehensive test suite for the following functions, but in this article, I'll skip that. </p> <p> Notice that this sort of tree explicitly distinguishes between internal and leaf nodes. This is necessary because you'll need to keep track of the directory names (the internal nodes), while at the same time you'll want to enrich the leaves with additional data - data that you can't meaningfully add to the internal nodes. You'll see this later in the article. </p> <p> While I typically tend to define F# types outside of modules (so that you don't have to, say, prefix the type name with the module name - <code>Tree.Tree</code> is so awkward), the rest of the tree code goes into a module, including two helper functions: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Tree&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;&#39;b&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;leaf&nbsp;=&nbsp;Leaf &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;&nbsp;list&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;node&nbsp;x&nbsp;xs&nbsp;=&nbsp;Node&nbsp;(x,&nbsp;xs)</pre> </p> <p> The <code>leaf</code> function doesn't add much value, but the <code>node</code> function offers a curried alternative to the <code>Node</code> case constructor. That's occasionally useful. </p> <p> The rest of the code related to trees is also defined in the <code>Tree</code> module, but I'm going to present it formatted as free-standing functions. If you're confused about the layout of the code, the entire code base is <a href="https://github.com/ploeh/picture-archivist">available on GitHub</a>. </p> <p> The <a href="/2019/08/05/rose-tree-catamorphism">rose tree catamorphism</a> is this <code>cata</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;c&nbsp;list&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;(&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;cata&nbsp;fd&nbsp;ff&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Leaf&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ff&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Node&nbsp;(x,&nbsp;xs)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;xs&nbsp;|&gt;&nbsp;List.map&nbsp;(cata&nbsp;fd&nbsp;ff)&nbsp;|&gt;&nbsp;fd&nbsp;x</pre> </p> <p> In the corresponding Haskell implementation of this architecture, I called this function <code>foldTree</code>, so why not retain that name? The short answer is that the naming conventions differ between Haskell and F#, and while I favour learning from Haskell, I still want my F# code to be as <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> as possible. </p> <p> While I don't enforce that client code <em>must</em> use the <code>Tree</code> module name to access the functions within, I prefer to name the functions so that they make sense when used with qualified access. Having to write <code>Tree.foldTree</code> seems redundant. A more idiomatic name would be <code>fold</code>, so that you could write <code>Tree.fold</code>. The problem with that name, though, is that <code>fold</code> usually implies a list-biased <em>fold</em> (corresponding to <code>foldl</code> in Haskell), and I'll actually need that name for that particular purpose later. </p> <p> So, <code>cata</code> it is. </p> <p> In this article, tree functionality is (with one exception) directly or transitively implemented with <code>cata</code>. </p> <h3 id="3f30722983ad47bd83c88cec4ba80983"> Filtering trees <a href="#3f30722983ad47bd83c88cec4ba80983" title="permalink">#</a> </h3> <p> It'll be useful to be able to filter the contents of a tree. For example, the picture archivist program will only move image files with valid metadata. This means that it'll need to filter out all files that aren't image files, as well as image files without valid metadata. </p> <p> It turns out that it'll be useful to supply a function that throws away <code>None</code> values from a tree of <code>option</code> leaves. This is similar to <a href="https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/list.choose%5B't%2C'u%5D-function-%5Bfsharp%5D">List.choose</a>, so I call it <code>Tree.choose</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;option)&nbsp;-&gt;&nbsp;Tree&lt;&#39;c,&#39;a&gt;&nbsp;-&gt;&nbsp;Tree&lt;&#39;c,&#39;b&gt;&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;choose&nbsp;f&nbsp;=&nbsp;cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;List.choose&nbsp;id&nbsp;&gt;&gt;&nbsp;node&nbsp;x&nbsp;&gt;&gt;&nbsp;Some)&nbsp;(f&nbsp;&gt;&gt;&nbsp;Option.map&nbsp;Leaf)</pre> </p> <p> You may find the type of the function surprising. Why does it return a <code>Tree option</code>, instead of simply a <code>Tree</code>? </p> <p> While <code>List.choose</code> simply returns a list, it can do this because lists can be empty. This <code>Tree</code> type, on the other hand, can't be empty. If the purpose of <code>Tree.choose</code> is to throw away all <code>None</code> values, then how do you return a tree from <code>Leaf None</code>? </p> <p> You can't return a <code>Leaf</code> because you have no value to put in the leaf. Similarly, you can't return a <code>Node</code> because, again, you have no value to put in the node. </p> <p> In order to handle this edge case, then, you'll have to return <code>None</code>: </p> <p> <pre>&gt; let l : Tree&lt;string, int option&gt; = Leaf None;; val l : Tree&lt;string,int option&gt; = Leaf None &gt; Tree.choose id l;; val it : Tree&lt;string,int&gt; option = None</pre> </p> <p> If you have anything other than a <code>None</code> leaf, though, you'll get a proper tree, but wrapped in an <code>option</code>: </p> <p> <pre>&gt; Tree.node "Foo" [Leaf (Some 42); Leaf None; Leaf (Some 2112)] |&gt; Tree.choose id;; val it : Tree&lt;string,int&gt; option = Some (Node ("Foo",[Leaf 42; Leaf 2112]))</pre> </p> <p> While the resulting tree is wrapped in a <code>Some</code> case, the leaves contain unwrapped values. </p> <h3 id="32f46f2c16cf428abc39c3d79433caa6"> Bifunctor, functor, and folds <a href="#32f46f2c16cf428abc39c3d79433caa6" title="permalink">#</a> </h3> <p> Through its type class language feature, Haskell has formal definitions of <a href="/2018/03/22/functors">functors</a>, <a href="/2018/12/24/bifunctors">bifunctors</a>, and other types of <em>folds</em> (list-biased <a href="/2019/04/29/catamorphisms">catamorphisms</a>). F# doesn't have a similar degree of formalism, which means that while you can still implement the corresponding functionality, you'll have to rely on conventions to make the functions recognisable. </p> <p> It's straighforward to start with the bifunctor functionality: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;d)&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;c&gt;&nbsp;-&gt;&nbsp;Tree&lt;&#39;b,&#39;d&gt;</span> <span style="color:blue;">let</span>&nbsp;bimap&nbsp;f&nbsp;g&nbsp;=&nbsp;cata&nbsp;(f&nbsp;&gt;&gt;&nbsp;node)&nbsp;(g&nbsp;&gt;&gt;&nbsp;leaf)</pre> </p> <p> This is, apart from the syntax differences, the same implementation as in Haskell. Based on <code>bimap</code>, you can also trivially implement <code>mapNode</code> and <code>mapLeaf</code> functions if you'd like, but you're not going to need those for the code in this article. You do need, however, a function that we could consider an alias of a hypothetical <code>mapLeaf</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;c&gt;</span> <span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;=&nbsp;bimap&nbsp;id&nbsp;f</pre> </p> <p> This makes <code>Tree</code> a functor. </p> <p> It'll also be useful to reduce a tree to a potentially more compact value, so you can add some specialised folds: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;c&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;bifold&nbsp;f&nbsp;g&nbsp;z&nbsp;t&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;flip&nbsp;f&nbsp;x&nbsp;y&nbsp;=&nbsp;f&nbsp;y&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;xs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;flip&nbsp;f&nbsp;x&nbsp;&gt;&gt;&nbsp;List.fold&nbsp;(&gt;&gt;)&nbsp;id&nbsp;xs)&nbsp;(flip&nbsp;g)&nbsp;t&nbsp;z <span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;c&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;(&#39;b&nbsp;-&gt;&nbsp;&#39;c&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;&#39;c&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;bifoldBack&nbsp;f&nbsp;g&nbsp;t&nbsp;z&nbsp;=&nbsp;cata&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;xs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;List.foldBack&nbsp;(&lt;&lt;)&nbsp;xs&nbsp;id&nbsp;&gt;&gt;&nbsp;f&nbsp;x)&nbsp;g&nbsp;t&nbsp;z</pre> </p> <p> In an attempt to emulate the F# naming conventions, I named the functions as I did. There are similar functions in the <code>List</code> and <code>Option</code> modules, for instance. If you're comparing the F# code with the Haskell code in the previous article, <code>Tree.bifold</code> corresponds to <code>bifoldl</code>, and <code>Tree.bifoldBack</code> corresponds to <code>bifoldr</code>. </p> <p> These enable you to implement folds over leaves only: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;c&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;fold&nbsp;f&nbsp;=&nbsp;bifold&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x)&nbsp;f <span style="color:green;">//&nbsp;(&#39;b&nbsp;-&gt;&nbsp;&#39;c&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;&#39;c&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;foldBack&nbsp;f&nbsp;=&nbsp;bifoldBack&nbsp;(<span style="color:blue;">fun</span>&nbsp;_&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x)&nbsp;f</pre> </p> <p> These, again, enable you to implement another function that'll turn out to be useful in this article: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;b&nbsp;-&gt;&nbsp;unit)&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;unit</span> <span style="color:blue;">let</span>&nbsp;iter&nbsp;f&nbsp;=&nbsp;fold&nbsp;(<span style="color:blue;">fun</span>&nbsp;()&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;x)&nbsp;()</pre> </p> <p> The picture archivist program isn't going to explicitly need all of these, but transitively, it will. </p> <h3 id="8a9a50c69a2d461cac5bb87fa4cf3cd9"> Moving pictures <a href="#8a9a50c69a2d461cac5bb87fa4cf3cd9" title="permalink">#</a> </h3> <p> So far, all the code shown here could be in a general-purpose reusable library, since it contains no functionality specifically related to image files. The rest of the code in this article, however, will be specific to the program. I'll put the domain model code in another module that I call <code>Archive</code>. Later in the article, we'll look at how to load a tree from the file system, but for now, we'll just pretend that we have such a tree. </p> <p> The major logic of the program is to create a destination tree based on a source tree. The leaves of the tree will have to carry some extra information apart from a file path, so you can introduce a specific type to capture that information: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;PhotoFile&nbsp;=&nbsp;{&nbsp;File&nbsp;:&nbsp;FileInfo;&nbsp;TakenOn&nbsp;:&nbsp;DateTime&nbsp;}</pre> </p> <p> A <code>PhotoFile</code> not only contains the file path for an image file, but also the date the photo was taken. This date can be extracted from the file's metadata, but that's an impure operation, so we'll delegate that work to the start of the program. We'll return to that later. </p> <p> Given a source tree of <code>PhotoFile</code> leaves, though, the program must produce a destination tree of files: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;Tree&lt;&#39;a,PhotoFile&gt;&nbsp;-&gt;&nbsp;Tree&lt;string,FileInfo&gt;</span> <span style="color:blue;">let</span>&nbsp;moveTo&nbsp;destination&nbsp;t&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;dirNameOf&nbsp;(dt&nbsp;:&nbsp;DateTime)&nbsp;=&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;%d-%02d&quot;</span>&nbsp;dt.Year&nbsp;dt.Month &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;groupByDir&nbsp;pf&nbsp;m&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;key&nbsp;=&nbsp;dirNameOf&nbsp;pf.TakenOn &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;dir&nbsp;=&nbsp;Map.tryFind&nbsp;key&nbsp;m&nbsp;|&gt;&nbsp;Option.defaultValue&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Map.add&nbsp;key&nbsp;(pf.File&nbsp;::&nbsp;dir)&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;addDir&nbsp;name&nbsp;files&nbsp;dirs&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tree.node&nbsp;name&nbsp;(List.map&nbsp;Leaf&nbsp;files)&nbsp;::&nbsp;dirs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;m&nbsp;=&nbsp;Tree.foldBack&nbsp;groupByDir&nbsp;t&nbsp;Map.empty &nbsp;&nbsp;&nbsp;&nbsp;Map.foldBack&nbsp;addDir&nbsp;m&nbsp;[]&nbsp;|&gt;&nbsp;Tree.node&nbsp;destination</pre> </p> <p> This <code>moveTo</code> function looks, perhaps, overwhelming, but it's composed of three conceptual steps: <ol> <li>Create a map of destination folders (<code>m</code>).</li> <li>Create a list of branches from the map (<code>Map.foldBack addDir m []</code>).</li> <li>Create a tree from the list (<code>Tree.node destination</code>).</li> </ol> The <code>moveTo</code> function starts by folding the input data into a map <code>m</code>. The map is keyed by the directory name, which is formatted by the <code>dirNameOf</code> function. This function takes a <code>DateTime</code> as input and formats it to a <code>YYYY-MM</code> format. For example, December 20, 2018 becomes <code>"2018-12"</code>. </p> <p> The entire mapping step groups the <code>PhotoFile</code> values into a map of the type <code>Map&lt;string,FileInfo list&gt;</code>. All the image files taken in April 2014 are added to the list with the <code>"2014-04"</code> key, all the image files taken in July 2011 are added to the list with the <code>"2011-07"</code> key, and so on. </p> <p> In the next step, the <code>moveTo</code> function converts the map to a list of trees. This will be the branches (or sub-directories) of the <code>destination</code> directory. Because of the desired structure of the destination tree, this is a list of shallow branches. Each node contains only leaves. </p> <p> <img src="/content/binary/shallow-photo-destination-directories.png" alt="Shallow photo destination directories."> </p> <p> The only remaining step is to add that list of branches to a <code>destination</code> node. This is done by piping (<code>|&gt;</code>) the list of sub-directories into <code>Tree.node destination</code>. </p> <p> Since this is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, it's <a href="/2015/05/07/functional-design-is-intrinsically-testable">easy to unit test</a>. Just create some test cases and call the function. First, the test cases. </p> <p> In this code base, I'm using <a href="https://xunit.net">xUnit.net</a> 2.4.1, so I'll first create a set of test cases as a test-specific class: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;MoveToDestinationTestData&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;Tree&lt;string,&nbsp;PhotoFile&gt;,&nbsp;string,&nbsp;Tree&lt;string,&nbsp;string&gt;&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;photoLeaf&nbsp;name&nbsp;(y,&nbsp;mth,&nbsp;d,&nbsp;h,&nbsp;m,&nbsp;s)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;{&nbsp;File&nbsp;=&nbsp;FileInfo&nbsp;name;&nbsp;TakenOn&nbsp;=&nbsp;DateTime&nbsp;(y,&nbsp;mth,&nbsp;d,&nbsp;h,&nbsp;m,&nbsp;s)&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>&nbsp;(2018,&nbsp;11,&nbsp;9,&nbsp;11,&nbsp;47,&nbsp;17), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;D&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(&nbsp;<span style="color:#a31515;">&quot;D&quot;</span>,&nbsp;[Node&nbsp;(<span style="color:#a31515;">&quot;2018-11&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>])])) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;S&quot;</span>,&nbsp;[photoLeaf&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>&nbsp;(1972,&nbsp;6,&nbsp;6,&nbsp;16,&nbsp;15,&nbsp;0)]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;D&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;D&quot;</span>,&nbsp;[Node&nbsp;(<span style="color:#a31515;">&quot;1972-06&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>])])) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;S&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;L&quot;</span>&nbsp;(2002,&nbsp;10,&nbsp;12,&nbsp;17,&nbsp;16,&nbsp;15); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;J&quot;</span>&nbsp;(2007,&nbsp;4,&nbsp;21,&nbsp;17,&nbsp;18,&nbsp;19)]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;D&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;D&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;2002-10&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;L&quot;</span>]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;2007-04&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;J&quot;</span>])])) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;(2010,&nbsp;1,&nbsp;12,&nbsp;17,&nbsp;16,&nbsp;15); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>&nbsp;(2010,&nbsp;3,&nbsp;12,&nbsp;17,&nbsp;16,&nbsp;15); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>&nbsp;(2010,&nbsp;1,&nbsp;21,&nbsp;17,&nbsp;18,&nbsp;19)]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;2&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;2010-01&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>;&nbsp;Leaf&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;2010-03&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>])])) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;(2010,&nbsp;1,&nbsp;12,&nbsp;17,&nbsp;16,&nbsp;15); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>&nbsp;(2010,&nbsp;3,&nbsp;12,&nbsp;17,&nbsp;16,&nbsp;15); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>&nbsp;(2010,&nbsp;1,&nbsp;21,&nbsp;17,&nbsp;18,&nbsp;19)]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;baz&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;d&quot;</span>&nbsp;(2010,&nbsp;3,&nbsp;1,&nbsp;2,&nbsp;3,&nbsp;4); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;photoLeaf&nbsp;<span style="color:#a31515;">&quot;e&quot;</span>&nbsp;(2011,&nbsp;3,&nbsp;4,&nbsp;3,&nbsp;2,&nbsp;1)])]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;qux&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;2010-01&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>;&nbsp;Leaf&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;2010-03&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>;&nbsp;Leaf&nbsp;<span style="color:#a31515;">&quot;d&quot;</span>]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;2011-03&quot;</span>,&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;e&quot;</span>])]))</pre> </p> <p> That looks like a lot of code, but is really just a list of test cases. Each test case is a triple of a source tree, a destination directory name, and an expected result (another tree). </p> <p> The test itself, on the other hand, is compact: </p> <p> <pre>[&lt;Theory;&nbsp;ClassData(typeof&lt;MoveToDestinationTestData&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Move&nbsp;to&nbsp;destination``&nbsp;source&nbsp;destination&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Archive.moveTo&nbsp;destination&nbsp;source &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;Tree.map&nbsp;string&nbsp;actual</pre> </p> <p> The <code>=!</code> operator comes from <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> and means something like <em>must equal</em>. It's an assertion that will throw an exception if <code>expected</code> isn't equal to <code>Tree.map string actual</code>. </p> <p> The reason that the assertion maps <code>actual</code> to a tree of strings is that <code>actual</code> is a <code>Tree&lt;string,FileInfo&gt;</code>, but <code>FileInfo</code> doesn't have structural equality. So either I had to implement a test-specific equality comparer for <code>FileInfo</code> (and for <code>Tree&lt;string,FileInfo&gt;</code>), or map the tree to something with proper equality, such as a <code>string</code>. I chose the latter. </p> <h3 id="abe95ba6865745bc9df8004079d8a250"> Calculating moves <a href="#abe95ba6865745bc9df8004079d8a250" title="permalink">#</a> </h3> <p> One pure step remains. The result of calling the <code>moveTo</code> function is a tree with the desired structure. In order to actually move the files, though, for each file you'll need to keep track of both the source path and the destination path. To make that explicit, you can define a type for that purpose: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Move&nbsp;=&nbsp;{&nbsp;Source&nbsp;:&nbsp;FileInfo;&nbsp;Destination&nbsp;:&nbsp;FileInfo&nbsp;}</pre> </p> <p> A <code>Move</code> is simply a data structure. Contrast this with typical object-oriented design, where it would be a (possibly polymorphic) method on an object. In functional programming, you'll regularly model <em>intent</em> with a data structure. As long as intents remain data, you can easily manipulate them, and once you're done with that, you can run an interpreter over your data structure to perform the work you want accomplished. </p> <p> The unit test cases for the <code>moveTo</code> function suggest that file names are local file names like <code>"L"</code>, <code>"J"</code>, <code>"a"</code>, and so on. That was only to make the tests as compact as possible, since the function actually doesn't manipulate the specific <code>FileInfo</code> objects. </p> <p> In reality, the file names will most likely be longer, and they could also contain the full path, instead of the local path: <code>"C:\foo\bar\a.jpg"</code>. </p> <p> If you call <code>moveTo</code> with a tree where each leaf has a fully qualified path, the output tree will have the desired structure of the destination tree, but the leaves will still contain the full path to each source file. That means that you can calculate a <code>Move</code> for each file: </p> <p> <pre><span style="color:green;">//&nbsp;Tree&lt;string,FileInfo&gt;&nbsp;-&gt;&nbsp;Tree&lt;string,Move&gt;</span> <span style="color:blue;">let</span>&nbsp;calculateMoves&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;replaceDirectory&nbsp;(f&nbsp;:&nbsp;FileInfo)&nbsp;d&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInfo&nbsp;(Path.Combine&nbsp;(d,&nbsp;f.Name)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;imp&nbsp;path&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Leaf&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;{&nbsp;Source&nbsp;=&nbsp;x;&nbsp;Destination&nbsp;=&nbsp;replaceDirectory&nbsp;x&nbsp;path&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Node&nbsp;(x,&nbsp;xs)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newNPath&nbsp;=&nbsp;Path.Combine&nbsp;(path,&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tree.node&nbsp;newNPath&nbsp;(List.map&nbsp;(imp&nbsp;newNPath)&nbsp;xs) &nbsp;&nbsp;&nbsp;&nbsp;imp&nbsp;<span style="color:#a31515;">&quot;&quot;</span></pre> </p> <p> This function takes as input a <code>Tree&lt;string,FileInfo&gt;</code>, which is compatible with the output of <code>moveTo</code>. It returns a <code>Tree&lt;string,Move&gt;</code>, i.e. a tree where the leaves are <code>Move</code> values. </p> <p> Earlier, I wrote that you can implement desired <code>Tree</code> functionality with the <code>cata</code> function, but that was a simplification. If you can implement the functionality of <code>calculateMoves</code> with <code>cata</code>, I don't know how. You can, however, implement it using explicit pattern matching and simple recursion. </p> <p> The <code>imp</code> function builds up a file path as it recursively negotiates the tree. All <code>Leaf</code> nodes are converted to a <code>Move</code> value using the leaf node's current <code>FileInfo</code> value as the <code>Source</code>, and the <code>path</code> to figure out the desired <code>Destination</code>. </p> <p> This code is still easy to unit test. First, test cases: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;CalculateMovesTestData&nbsp;()&nbsp;<span style="color:blue;">as</span>&nbsp;this&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;TheoryData&lt;Tree&lt;string,&nbsp;FileInfo&gt;,&nbsp;Tree&lt;string,&nbsp;(string&nbsp;*&nbsp;string)&gt;&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;(Leaf&nbsp;(FileInfo&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>),&nbsp;Leaf&nbsp;(<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;[Leaf&nbsp;(FileInfo&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>)]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;[Leaf&nbsp;(<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;Path.Combine&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>))])) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;[Leaf&nbsp;(FileInfo&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>);&nbsp;Leaf&nbsp;(FileInfo&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>)]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;(<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;Path.Combine&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;(<span style="color:#a31515;">&quot;2&quot;</span>,&nbsp;Path.Combine&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>))])) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do</span>&nbsp;this.Add&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;b&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;(FileInfo&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;(FileInfo&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>)]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;c&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;(FileInfo&nbsp;<span style="color:#a31515;">&quot;3&quot;</span>)])]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(Path.Combine&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>),&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;(<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;Path.Combine&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;(<span style="color:#a31515;">&quot;2&quot;</span>,&nbsp;Path.Combine&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>))]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(Path.Combine&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>),&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;(<span style="color:#a31515;">&quot;3&quot;</span>,&nbsp;Path.Combine&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;3&quot;</span>))])]))</pre> </p> <p> The test cases in this parametrised test are tuples of an input tree and the expected tree. For each test case, the test calls the <code>Archive.calculateMoves</code> function with <code>tree</code> and asserts that the <code>actual</code> tree is equal to the <code>expected</code> tree: </p> <p> <pre>[&lt;Theory;&nbsp;ClassData(typeof&lt;CalculateMovesTestData&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Calculate&nbsp;moves``&nbsp;tree&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Archive.calculateMoves&nbsp;tree &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;Tree.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;m&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(m.Source.ToString&nbsp;(),&nbsp;m.Destination.ToString&nbsp;()))&nbsp;actual</pre> </p> <p> Again, the test maps <code>FileInfo</code> objects to <code>strings</code> to support easy comparison. </p> <p> That's all the pure code you need in order to implement the desired functionality. Now you only need to write some code that loads a tree from disk, and imprints a destination tree to disk, as well as the code that composes it all. </p> <h3 id="bac6be79cf8c44a7b47923e2ec90d99f"> Loading a tree from disk <a href="#bac6be79cf8c44a7b47923e2ec90d99f" title="permalink">#</a> </h3> <p> The remaining code in this article is impure. You could put it in dedicated modules, but for this program, you're only going to need three functions and a bit of composition code, so you could also just put it all in the <code>Program</code> module. That's what I did. </p> <p> To load a tree from disk, you'll need a root directory, under which you load the entire tree. Given a directory path, you read a tree using a recursive function like this: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;Tree&lt;string,string&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;readTree&nbsp;path&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;File.Exists&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;Leaf&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;dirsAndFiles&nbsp;=&nbsp;Directory.EnumerateFileSystemEntries&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;branches&nbsp;=&nbsp;Seq.map&nbsp;readTree&nbsp;dirsAndFiles&nbsp;|&gt;&nbsp;Seq.toList &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(path,&nbsp;branches)</pre> </p> <p> This recursive function starts by checking whether the <code>path</code> is a file that exists. If it does, the path is a file, so it creates a new <code>Leaf</code> with that path. </p> <p> If <code>path</code> isn't a file, it's a directory. In that case, use <code>Directory.EnumerateFileSystemEntries</code> to enumerate all the directories and files in that directory, and map all those directory entries recursively. That produces all the <code>branches</code> for the current node. Finally, return a new <code>Node</code> with the <code>path</code> and the <code>branches</code>. </p> <h3 id="7f5e06eb61024264ad214d41b63a8a74"> Loading metadata <a href="#7f5e06eb61024264ad214d41b63a8a74" title="permalink">#</a> </h3> <p> The <code>readTree</code> function only produces a tree with <code>string</code> leaves, while the program requires a tree with <code>PhotoFile</code> leaves. You'll need to read the <a href="https://en.wikipedia.org/wiki/Exif">Exif</a> metadata from each file and enrich the tree with the <em>date-taken</em> data. </p> <p> In this code base, I've written a little <code>Photo</code> module to extract the desired metadata from an image file. I'm not going to list all the code here; if you're interested, the code is <a href="https://github.com/ploeh/picture-archivist">available on GitHub</a>. The <code>Photo</code> module enables you to write an impure operation like this: </p> <p> <pre><span style="color:green;">//&nbsp;FileInfo&nbsp;-&gt;&nbsp;PhotoFile&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;readPhoto&nbsp;file&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;Photo.extractDateTaken&nbsp;file &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;dateTaken&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;File&nbsp;=&nbsp;file;&nbsp;TakenOn&nbsp;=&nbsp;dateTaken&nbsp;})</pre> </p> <p> This operation can fail for various reasons: <ul> <li>The file may not exist.</li> <li>The file exists, but has no metadata.</li> <li>The file has metadata, but no <em>date-taken</em> metadata.</li> <li>The <em>date-taken</em> metadata string is malformed.</li> </ul> When you traverse a <code>Tree&lt;string,string&gt;</code> with <code>readPhoto</code>, you'll get a <code>Tree&lt;string,PhotoFile option&gt;</code>. That's when you'll need <code>Tree.choose</code>. You'll see this soon. </p> <h3 id="59159ef499884e10ae92e5ef6e666c36"> Writing a tree to disk <a href="#59159ef499884e10ae92e5ef6e666c36" title="permalink">#</a> </h3> <p> The above <code>calculateMoves</code> function creates a <code>Tree&lt;string,Move&gt;</code>. The final piece of impure code you'll need to write is an operation that traverses such a tree and executes each <code>Move</code>. </p> <p> <pre><span style="color:green;">//&nbsp;Tree&lt;&#39;a,Move&gt;&nbsp;-&gt;&nbsp;unit</span> <span style="color:blue;">let</span>&nbsp;writeTree&nbsp;t&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;copy&nbsp;m&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Directory.CreateDirectory&nbsp;m.Destination.DirectoryName&nbsp;|&gt;&nbsp;ignore &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.Source.CopyTo&nbsp;m.Destination.FullName&nbsp;|&gt;&nbsp;ignore &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printfn&nbsp;<span style="color:#a31515;">&quot;Copied&nbsp;to&nbsp;%s&quot;</span>&nbsp;m.Destination.FullName &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;compareFiles&nbsp;m&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sourceStream&nbsp;=&nbsp;File.ReadAllBytes&nbsp;m.Source.FullName &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;destinationStream&nbsp;=&nbsp;File.ReadAllBytes&nbsp;m.Destination.FullName &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceStream&nbsp;=&nbsp;destinationStream &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;move&nbsp;m&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;compareFiles&nbsp;m&nbsp;<span style="color:blue;">then</span>&nbsp;m.Source.Delete&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;Tree.iter&nbsp;move&nbsp;t</pre> </p> <p> The <code>writeTree</code> function traverses the input tree, and for each <code>Move</code>, it first copies the file, then it verifies that the copy was successful, and finally, if that's the case, it deletes the source file. </p> <h3 id="f30093164b184bbf877f307fa4cf4c63"> Composition <a href="#f30093164b184bbf877f307fa4cf4c63" title="permalink">#</a> </h3> <p> You can now compose an <a href="/2020/03/02/impureim-sandwich">impure-pure-impure sandwich</a> from all the Lego pieces: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;string&nbsp;-&gt;&nbsp;unit</span> <span style="color:blue;">let</span>&nbsp;movePhotos&nbsp;source&nbsp;destination&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sourceTree&nbsp;=&nbsp;readTree&nbsp;source&nbsp;|&gt;&nbsp;Tree.map&nbsp;FileInfo &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;photoTree&nbsp;=&nbsp;Tree.choose&nbsp;readPhoto&nbsp;sourceTree &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;destinationTree&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Option.map&nbsp;(Archive.moveTo&nbsp;destination&nbsp;&gt;&gt;&nbsp;Archive.calculateMoves)&nbsp;photoTree &nbsp;&nbsp;&nbsp;&nbsp;Option.iter&nbsp;writeTree&nbsp;destinationTree</pre> </p> <p> First, you load the <code>sourceTree</code> using the <code>readTree</code> operation. This returns a <code>Tree&lt;string,string&gt;</code>, so map the leaves to <code>FileInfo</code> objects. You then load the image metatadata by traversing <code>sourceTree</code> with <code>Tree.choose readPhoto</code>. Each call to <code>readPhoto</code> produces a <code>PhotoFile option</code>, so this is where you want to use <code>Tree.choose</code> to throw all the <code>None</code> values away. </p> <p> Those two lines of code constitute the initial impure step of the sandwich (yes: mixed metaphors, I know). </p> <p> The pure part of the sandwich is the composition of the pure functions <code>moveTo</code> and <code>calculateMoves</code>. Since <code>photoTree</code> is a <code>Tree&lt;string,PhotoFile&gt; option</code>, you'll need to perform that transformation inside of <code>Option.map</code>. The resulting <code>destinationTree</code> is a <code>Tree&lt;string,Move&gt; option</code>. </p> <p> The final, impure step of the sandwich, then, is to apply all the moves with <code>writeTree</code>. </p> <h3 id="ab0013f79c184586a10aa014db496bef"> Execution <a href="#ab0013f79c184586a10aa014db496bef" title="permalink">#</a> </h3> <p> The <code>movePhotos</code> operation takes <code>source</code> and <code>destination</code> arguments. You could hypothetically call it from a rich client or a background process, but here I'll just call if from a command-line program. The <code>main</code> operation will have to parse the input arguments and call <code>movePhotos</code>: </p> <p> <pre>[&lt;EntryPoint&gt;] <span style="color:blue;">let</span>&nbsp;main&nbsp;argv&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;argv&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[|source;&nbsp;destination|]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;movePhotos&nbsp;source&nbsp;destination &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;printfn&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;provide&nbsp;source&nbsp;and&nbsp;destination&nbsp;directories&nbsp;as&nbsp;arguments.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;<span style="color:green;">//&nbsp;return&nbsp;an&nbsp;integer&nbsp;exit&nbsp;code</span></pre> </p> <p> You could write more sophisticated parsing of the program arguments, but that's not the topic of this article, so I only wrote the bare minimum required to get the program working. </p> <p> You can now compile and run the program: </p> <p> <pre>$ ./ArchivePictures "C:\Users\mark\Desktop\Test" "C:\Users\mark\Desktop\Test-Out" Copied to C:\Users\mark\Desktop\Test-Out\2003-04\2003-04-29 15.11.50.jpg Copied to C:\Users\mark\Desktop\Test-Out\2011-07\2011-07-10 13.09.36.jpg Copied to C:\Users\mark\Desktop\Test-Out\2014-04\2014-04-18 14.05.02.jpg Copied to C:\Users\mark\Desktop\Test-Out\2014-04\2014-04-17 17.11.40.jpg Copied to C:\Users\mark\Desktop\Test-Out\2014-05\2014-05-23 16.07.20.jpg Copied to C:\Users\mark\Desktop\Test-Out\2014-06\2014-06-21 16.48.40.jpg Copied to C:\Users\mark\Desktop\Test-Out\2014-06\2014-06-30 15.44.52.jpg Copied to C:\Users\mark\Desktop\Test-Out\2016-05\2016-05-01 09.25.23.jpg Copied to C:\Users\mark\Desktop\Test-Out\2017-08\2017-08-22 19.53.28.jpg</pre> </p> <p> This does indeed produce the expected destination directory structure. </p> <p> <img src="/content/binary/picture-archivist-destination-directory.png" alt="Seven example directories with pictures."> </p> <p> It's always nice when something turns out to work in practice, as well as in theory. </p> <h3 id="3e4503b89d8f4b81b8b9cac9d1f39021"> Summary <a href="#3e4503b89d8f4b81b8b9cac9d1f39021" title="permalink">#</a> </h3> <p> <a href="/2018/11/19/functional-architecture-a-definition">Functional software architecture</a> involves separating pure from impure code so that no pure functions invoke impure operations. Often, you can achieve that with what I call the <em>impure-pure-impure sandwich</em> architecture. In this example, you saw how to model the file system as a tree. This enables you to separate the impure file interactions from the pure program logic. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="68b26807cc424856b8f762f214389826"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#68b26807cc424856b8f762f214389826">#</a></div> <div class="comment-content"> <blockquote> <p> You do need, however, a function that we could consider an alias of a hypothetical <code>mapLeaf</code> function... </p> <p> ... </p> <p> This makes <code>Tree</code> a functor. </p> </blockquote> <p> I find that last statement slightly ambiguous. I prefer to say... </p> <blockquote> This makes <code>Tree&lt;'a, 'b&gt;</code> a functor in <code>'b</code>. </blockquote> <p> ...which is more precise. </p> <blockquote> In an attempt to emulate the F# naming conventions, I named the functions [<code>bifold</code> and <code>bifoldBack</code>]. There are similar functions in the <code>List</code> and <code>Option</code> modules, for instance. If you're comparing the F# code with the Haskell code in the previous article, <code>Tree.bifold</code> corresponds to <code>bifoldl</code>, and <code>Tree.bifoldBack</code> corresponds to <code>bifoldr</code>. </blockquote> <p> I was very confused by these names at first. They suggest that the most important difference between them is the use of <code>List.fold</code> and <code>List.foldBack</code> in their respective implementations. However, for both <code>bifold</code> and <code>bifoldBack</code>, the behavior does not depend at all on the choice between <code>List.fold</code> and <code>List.foldBack</code> (as long as <code>id</code> and <code>xs</code> are given in the correct order). Instead, the difference between <code>bifold</code> and <code>bifoldBack</code> is completely determined by (the minor choice to use <code>flip</code> in <code>bifold</code> and) whether the function composition operator is to the right (as in <code>bifold</code>) or to the left (as in <code>bifoldBack</code>). This is slightly easier to see when <code>bifoldBack</code> is implemented as <code>cata (fun x xs -&gt; f x &lt;&lt; List.foldBack (&lt;&lt;) xs id) g t z</code>. The reason that the choice between <code>List.fold</code> and <code>List.foldBack</code> doesn't matter is because both function composition operators are associative (and because the seed value is the identity element for both functions). </p> <p> The idea of a catamorphism is still very new to me. Instead of directly aggregating the parts of a tree into a single value like <code>bifold</code> and <code>bifoldBack</code> (via <code>cata</code>), I have historically exposed a minimal set of needed <a href="https://en.wikipedia.org/wiki/Tree_traversal">tree traversal orderings</a> and then follow such a call with <code>Seq.fold</code> or <code>Seq.foldBack</code>. I think <code>bifold</code> does a <a href="https://en.wikipedia.org/wiki/Depth-first_search#Vertex_orderings">preorder traversal</a> and <code>bifoldBack</code> does a reverse preorder traversal. So, after all that, I now understand the names. </p> <blockquote> <p> [The function <code>calculateMoves </code>] takes as input a <code>Tree&lt;string,FileInfo&gt;</code>, which is compatible with the output of <code>moveTo</code>. It returns a <code>Tree&lt;string,Move&gt;</code>, i.e. a tree where the leaves are <code>Move</code> values. </p> <p> Earlier, I wrote that you can implement desired <code>Tree</code> functionality with the <code>cata</code> function, but that was a simplification. If you can implement the functionality of <code>calculateMoves</code> with <code>cata</code>, I don't know how. You can, however, implement it using explicit pattern matching and simple recursion. </p> <p> The <code>imp</code> function builds up a file path as it recursively negotiates the tree. All <code>Leaf</code> nodes are converted to a <code>Move</code> value using the leaf node's current <code>FileInfo</code> value as the <code>Source</code>, and the <code>path</code> to figure out the desired <code>Destination</code>. </p> </blockquote> <p> I don't know how to implement <code>calculateMoves</code> via <code>cata</code> either. Nonetheless, there is still a domain-independent abstraction waiting to be extracted. </p> <p> Think of the <code>scan</code> function that exists in F# in the <a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-seqmodule.html#scan"><code>Seq</code></a> and <a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-listmodule.html#scan"><code>List</code></a> modules. We can implement a similar function for your rose tree. I did so in <a href="https://github.com/bender2k14/picture-archivist/commit/76d2aa83c5d0f271ae7b66ca9540a9b53de7d56a">this commit</a>. Now <code>calculateMoves</code> is trivial, it still passes your domain-specific tests, and <code>scan</code> can be subjected to domain-independent unit tests. </p> <p> Now the question is...can <code>scan</code> be implemented by <code>cata</code>? Or maybe...can <code>cata</code> be implemented by <code>scan</code>? I don't know the answer to either of these questions. Alternatively, we can ask...does <code>scan</code> correspond to some concept in category theory? I don't know that either. You are way ahead of me in your understanding of category theory, but I am doing my best to catch up. </p> </div> <div class="comment-date">2020-08-04 05:35 UTC</div> </div> <div class="comment" id="aabd1eaca733405ebdb0f6c781cfe719"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#aabd1eaca733405ebdb0f6c781cfe719">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. That's a neat refactoring. I spent a couple of hours with it yesterday to see if I could implement your <code>scan</code> function with <code>cata</code>, but like you, it eludes me. It doesn't look like it's possible, although I'd love to be proven wrong. </p> <p> I'm not aware of any theoretical foundations for <code>scan</code>, but there's so many things I don't know... </p> <p> I originally came across the concept of F-Algebras and catamorphisms when I read <a href="https://bartoszmilewski.com/2017/02/28/f-algebras">Bartosz Milewski's article</a>. I've later discovered that the <a href="https://hackage.haskell.org/package/recursion-schemes">recursion-schemes</a> package was there all along. Not only does it define <code>cata</code>, but it also includes much other functionality that I still haven't absorbed. Perhaps there might be a clue there... </p> </div> <div class="comment-date">2020-08-12 5:36 UTC</div> </div> <div class="comment" id="a35d494176b5475cada4b51f5706c347"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#a35d494176b5475cada4b51f5706c347">#</a></div> <div class="comment-content"> <blockquote> In this article, tree functionality is (with one exception) directly or transitively implemented with <code>cata</code>. </blockquote> <p> One tree functionality that this article didn't use is the <code>apply</code> function of an applicative functor. Of course <code>apply</code> can be implemented in terms of <code>bind</code>. Doing so here would yield an implementation of <code>apply</code> that transitively depends on <code>cata</code>. </p> <p> Is there a way (perhaps an ellegant way) to directly implement <code>apply</code> via <code>cata</code>? I am asking because I have a monad with <code>apply</code> implemented in terms of <code>bind</code>, but I would like an <a href="https://github.com/hedgehogqa/fsharp-hedgehog/issues/272">implementation with better behavior</a>. </p> </div> <div class="comment-date">2021-01-21 21:03 UTC</div> </div> <div class="comment" id="654a6b5cf56a4fe590091c33fd1b0a13"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#654a6b5cf56a4fe590091c33fd1b0a13">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Yes, <a href="/2019/06/10/tree-catamorphism#8647c7bd03aa4d4b8a01a8252058830f">you can implement the Applicative instance directly from the catamorphism</a>. </p> </div> <div class="comment-date">2021-01-22 12:49 UTC</div> </div> <div class="comment" id="05835f4baece4954a5186fac6531be3e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#05835f4baece4954a5186fac6531be3e">#</a></div> <div class="comment-content"> <p> Tyson, FWIW I figured out <a href="/2021/04/12/threading-context-through-a-catamorphism">how to implement calculateMoves directly with the catamorphism</a>. </p> </div> <div class="comment-date">2021-04-12 11:17 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Picture archivist in Haskell https://blog.ploeh.dk/2019/09/09/picture-archivist-in-haskell 2019-09-09T08:19:00+00:00 Mark Seemann <div id="post"> <p> <em>A comprehensive code example showing how to implement a functional architecture in Haskell.</em> </p> <p> This article shows how to implement the <a href="/2019/08/26/functional-file-system">picture archivist architecture described in the previous article</a>. In short, the task is to move some image files to directories based on their date-taken metadata. The architectural idea is to load a directory structure from disk into an in-memory tree, manipulate that tree, and use the resulting tree to perform the desired actions: </p> <p> <img src="/content/binary/functional-file-system-interaction.png" alt="A functional program typically loads data, transforms it, and stores it again."> </p> <p> Much of the program will manipulate the tree data, which is immutable. </p> <h3 id="770cf37f0e3c457782ea20b53257f2d1"> Tree <a href="#770cf37f0e3c457782ea20b53257f2d1" title="permalink">#</a> </h3> <p> You can start by defining a <a href="https://en.wikipedia.org/wiki/Rose_tree">rose tree</a>: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Tree&nbsp;a&nbsp;b&nbsp;=&nbsp;Node&nbsp;a&nbsp;[Tree&nbsp;a&nbsp;b]&nbsp;|&nbsp;Leaf&nbsp;b&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> If you wanted to, you could put all the <code>Tree</code> code in a reusable library, because none of it is coupled to a particular application, such as <a href="https://amzn.to/2V06Kji">moving pictures</a>. You could also write a comprehensive test suite for the following functions, but in this article, I'll skip that. </p> <p> Notice that this sort of tree explicitly distinguishes between internal and leaf nodes. This is necessary because you'll need to keep track of the directory names (the internal nodes), while at the same time you'll want to enrich the leaves with additional data - data that you can't meaningfully add to the internal nodes. You'll see this later in the article. </p> <p> The <a href="/2019/08/05/rose-tree-catamorphism">rose tree catamorphism</a> is this <code>foldTree</code> function: </p> <p> <pre><span style="color:#2b91af;">foldTree</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[c]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;a&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c foldTree&nbsp;&nbsp;_&nbsp;fl&nbsp;(Leaf&nbsp;x)&nbsp;=&nbsp;fl&nbsp;x foldTree&nbsp;fn&nbsp;fl&nbsp;(Node&nbsp;x&nbsp;xs)&nbsp;=&nbsp;fn&nbsp;x&nbsp;$&nbsp;foldTree&nbsp;fn&nbsp;fl&nbsp;&lt;$&gt;&nbsp;xs</pre> </p> <p> Sometimes I name the catamorphism <code>cata</code>, sometimes something like <code>tree</code>, but using a library like <code>Data.Tree</code> as another source of inspiration, in this article I chose to name it <code>foldTree</code>. </p> <p> In this article, tree functionality is (with one exception) directly or transitively implemented with <code>foldTree</code>. </p> <h3 id="f5541d8a36b04cf9a455824c5f3a21c7"> Filtering trees <a href="#f5541d8a36b04cf9a455824c5f3a21c7" title="permalink">#</a> </h3> <p> It'll be useful to be able to filter the contents of a tree. For example, the picture archivist program will only move image files with valid metadata. This means that it'll need to filter out all files that aren't image files, as well as image files without valid metadata. </p> <p> It turns out that it'll be useful to supply a function that throws away <code>Nothing</code> values from a tree of <code>Maybe</code> leaves. This is similar to the <code>catMaybes</code> function from <code>Data.Maybe</code>, so I call it <code>catMaybeTree</code>: </p> <p> <pre><span style="color:#2b91af;">catMaybeTree</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;a&nbsp;(<span style="color:#2b91af;">Maybe</span>&nbsp;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;(<span style="color:blue;">Tree</span>&nbsp;a&nbsp;b) catMaybeTree&nbsp;=&nbsp;foldTree&nbsp;(\x&nbsp;-&gt;&nbsp;Just&nbsp;.&nbsp;Node&nbsp;x&nbsp;.&nbsp;catMaybes)&nbsp;(<span style="color:blue;">fmap</span>&nbsp;Leaf)</pre> </p> <p> You may find the type of the function surprising. Why does it return a <code>Maybe Tree</code>, instead of simply a <code>Tree</code>? And if you accept the type as given, isn't this simply the <code>sequence</code> function? </p> <p> While <code>catMaybes</code> simply returns a list, it can do this because lists can be empty. This <code>Tree</code> type, on the other hand, can't be empty. If the purpose of <code>catMaybeTree</code> is to throw away all <code>Nothing</code> values, then how do you return a tree from <code>Leaf Nothing</code>? </p> <p> You can't return a <code>Leaf</code> because you have no value to put in the leaf. Similarly, you can't return a <code>Node</code> because, again, you have no value to put in the node. </p> <p> In order to handle this edge case, then, you'll have to return <code>Nothing</code>: </p> <p> <pre>Prelude Tree&gt; catMaybeTree $ Leaf Nothing Nothing</pre> </p> <p> Isn't this the same as <code>sequence</code>, then? It's not, because <code>sequence</code> short-circuits all data, as this list example shows: </p> <p> <pre>Prelude&gt; sequence [Just 42, Nothing, Just 2112] Nothing</pre> </p> <p> Contrast this with the behaviour of <code>catMaybes</code>: </p> <p> <pre>Prelude Data.Maybe&gt; catMaybes [Just 42, Nothing, Just 2112] [42,2112]</pre> </p> <p> You've yet to see the <code>Traversable</code> instance for <code>Tree</code>, but it behaves in the same way: </p> <p> <pre>Prelude Tree&gt; sequence $ Node "Foo" [Leaf (Just 42), Leaf Nothing, Leaf (Just 2112)] Nothing</pre> </p> <p> The <code>catMaybeTree</code> function, on the other hand, returns a filtered tree: </p> <p> <pre>Prelude Tree&gt; catMaybeTree $ Node "Foo" [Leaf (Just 42), Leaf Nothing, Leaf (Just 2112)] Just (Node "Foo" [Leaf 42,Leaf 2112])</pre> </p> <p> While the resulting tree is wrapped in a <code>Just</code> case, the leaves contain unwrapped values. </p> <h3 id="5f0287c6d6fe42f3ad73a8e31ba9b3c4"> Instances <a href="#5f0287c6d6fe42f3ad73a8e31ba9b3c4" title="permalink">#</a> </h3> <p> The <a href="/2019/08/05/rose-tree-catamorphism">article about the rose tree catamorphism</a> already covered how to add instances of <code>Bifunctor</code>, <code>Bifoldable</code>, and <code>Bitraversable</code>, so I'll give this only cursory treatment. Refer to that article for a more detailed treatment. The code that accompanies that article also has <a href="http://hackage.haskell.org/package/QuickCheck">QuickCheck</a> properties that verify the various laws associated with those instances. Here, I'll just list the instances without further comment: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bifunctor</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bimap&nbsp;f&nbsp;s&nbsp;=&nbsp;foldTree&nbsp;(Node&nbsp;.&nbsp;f)&nbsp;(Leaf&nbsp;.&nbsp;s) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bifoldable</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bifoldMap&nbsp;f&nbsp;=&nbsp;foldTree&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;f&nbsp;x&nbsp;&lt;&gt;&nbsp;mconcat&nbsp;xs) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bitraversable</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bitraverse&nbsp;f&nbsp;s&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;foldTree&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;Node&nbsp;&lt;$&gt;&nbsp;f&nbsp;x&nbsp;&lt;*&gt;&nbsp;sequenceA&nbsp;xs)&nbsp;(<span style="color:blue;">fmap</span>&nbsp;Leaf&nbsp;.&nbsp;s) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">Tree</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;=&nbsp;second <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;(<span style="color:blue;">Tree</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;foldMap&nbsp;=&nbsp;bifoldMap&nbsp;mempty <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;(<span style="color:blue;">Tree</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;bisequenceA&nbsp;.&nbsp;first&nbsp;pure</pre> </p> <p> The picture archivist program isn't going to explicitly need all of these, but transitively, it will. </p> <h3 id="d1bbd6ef895f45619822126f44bf6bfb"> Moving pictures <a href="#d1bbd6ef895f45619822126f44bf6bfb" title="permalink">#</a> </h3> <p> So far, all the code shown here could be in a general-purpose reusable library, since it contains no functionality specifically related to image files. The rest of the code in this article, however, will be specific to the program. I'll put the domain model code in another module and import some functionality: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Archive&nbsp;<span style="color:blue;">where</span> <span style="color:blue;">import</span>&nbsp;Data.Time <span style="color:blue;">import</span>&nbsp;Text.Printf <span style="color:blue;">import</span>&nbsp;System.FilePath <span style="color:blue;">import</span>&nbsp;<span style="color:blue;">qualified</span>&nbsp;Data.Map.Strict&nbsp;<span style="color:blue;">as</span>&nbsp;Map <span style="color:blue;">import</span>&nbsp;Tree</pre> </p> <p> Notice that <code>Tree</code> is one of the imported modules. </p> <p> Later, we'll look at how to load a tree from the file system, but for now, we'll just pretend that we have such a tree. </p> <p> The major logic of the program is to create a destination tree based on a source tree. The leaves of the tree will have to carry some extra information apart from a file path, so you can introduce a specific type to capture that information: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;PhotoFile&nbsp;= &nbsp;&nbsp;PhotoFile&nbsp;{&nbsp;photoFileName&nbsp;::&nbsp;FilePath,&nbsp;takenOn&nbsp;::&nbsp;LocalTime&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> A <code>PhotoFile</code> not only contains the file path for an image file, but also the date the photo was taken. This date can be extracted from the file's metadata, but that's an impure operation, so we'll delegate that work to the start of the program. We'll return to that later. </p> <p> Given a source tree of <code>PhotoFile</code> leaves, though, the program must produce a destination tree of files: </p> <p> <pre><span style="color:#2b91af;">moveTo</span>&nbsp;::&nbsp;(<span style="color:blue;">Foldable</span>&nbsp;t,&nbsp;<span style="color:blue;">Ord</span>&nbsp;a,&nbsp;<span style="color:blue;">PrintfType</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;t&nbsp;<span style="color:blue;">PhotoFile</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;a&nbsp;<span style="color:#2b91af;">FilePath</span> moveTo&nbsp;destination&nbsp;= &nbsp;&nbsp;Node&nbsp;destination&nbsp;.&nbsp;Map.foldrWithKey&nbsp;addDir&nbsp;<span style="color:blue;">[]</span>&nbsp;.&nbsp;<span style="color:blue;">foldr</span>&nbsp;groupByDir&nbsp;Map.empty &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;dirNameOf&nbsp;(LocalTime&nbsp;d&nbsp;_)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(y,&nbsp;m,&nbsp;_)&nbsp;=&nbsp;toGregorian&nbsp;d&nbsp;<span style="color:blue;">in</span>&nbsp;printf&nbsp;<span style="color:#a31515;">&quot;%d-%02d&quot;</span>&nbsp;y&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;groupByDir&nbsp;(PhotoFile&nbsp;fileName&nbsp;t)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Map.insertWith&nbsp;<span style="color:#2b91af;">(++)</span>&nbsp;(dirNameOf&nbsp;t)&nbsp;[fileName] &nbsp;&nbsp;&nbsp;&nbsp;addDir&nbsp;name&nbsp;files&nbsp;dirs&nbsp;=&nbsp;Node&nbsp;name&nbsp;(Leaf&nbsp;&lt;$&gt;&nbsp;files)&nbsp;:&nbsp;dirs</pre> </p> <p> This <code>moveTo</code> function looks, perhaps, overwhelming, but it's composed of only three steps: <ol> <li>Create a map of destination folders (<code>foldr groupByDir Map.empty</code>).</li> <li>Create a list of branches from the map (<code>Map.foldrWithKey addDir []</code>).</li> <li>Create a tree from the list (<code>Node destination</code>).</li> </ol> Recall that when Haskell functions are composed with the <code>.</code> operator, you'll have to read the composition from right to left. </p> <p> Notice that this function works with any <code>Foldable</code> data container, so it'd work with lists and other data structures besides trees. </p> <p> The <code>moveTo</code> function starts by folding the input data into a map. The map is keyed by the directory name, which is formatted by the <code>dirNameOf</code> function. This function takes a <code>LocalTime</code> as input and formats it to a <code>YYYY-MM</code> format. For example, December 20, 2018 becomes <code>"2018-12"</code>. </p> <p> The entire mapping step groups the <code>PhotoFile</code> values into a map of the type <code>Map a [FilePath]</code>. All the image files taken in April 2014 are added to the list with the <code>"2014-04"</code> key, all the image files taken in July 2011 are added to the list with the <code>"2011-07"</code> key, and so on. </p> <p> In the next step, the <code>moveTo</code> function converts the map to a list of trees. This will be the branches (or sub-directories) of the <code>destination</code> directory. Because of the desired structure of the destination tree, this is a list of shallow branches. Each node contains only leaves. </p> <p> <img src="/content/binary/shallow-photo-destination-directories.png" alt="Shallow photo destination directories."> </p> <p> The only remaining step is to add that list of branches to a <code>destination</code> node. </p> <p> Since this is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, it's <a href="/2015/05/07/functional-design-is-intrinsically-testable">easy to unit test</a>. Just create some input values and call the function: </p> <p> <pre><span style="color:#a31515;">&quot;Move&nbsp;to&nbsp;destination&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(source,&nbsp;destination,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>&nbsp;$&nbsp;lt&nbsp;2018&nbsp;11&nbsp;9&nbsp;11&nbsp;47&nbsp;17 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;D&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;D&quot;</span>&nbsp;[Node&nbsp;<span style="color:#a31515;">&quot;2018-11&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>]]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;S&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>&nbsp;$&nbsp;lt&nbsp;1972&nbsp;6&nbsp;6&nbsp;16&nbsp;15&nbsp;00] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;D&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;D&quot;</span>&nbsp;[Node&nbsp;<span style="color:#a31515;">&quot;1972-06&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>]]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;S&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;L&quot;</span>&nbsp;$&nbsp;lt&nbsp;2002&nbsp;10&nbsp;12&nbsp;17&nbsp;16&nbsp;15, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;J&quot;</span>&nbsp;$&nbsp;lt&nbsp;2007&nbsp;4&nbsp;21&nbsp;17&nbsp;18&nbsp;19] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;D&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;D&quot;</span>&nbsp;[Node&nbsp;<span style="color:#a31515;">&quot;2002-10&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;L&quot;</span>],&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;2007-04&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;J&quot;</span>]]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;$&nbsp;lt&nbsp;2010&nbsp;1&nbsp;12&nbsp;17&nbsp;16&nbsp;15, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>&nbsp;$&nbsp;lt&nbsp;2010&nbsp;3&nbsp;12&nbsp;17&nbsp;16&nbsp;15, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>&nbsp;$&nbsp;lt&nbsp;2010&nbsp;1&nbsp;21&nbsp;17&nbsp;18&nbsp;19] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;2&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;2010-01&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;Leaf&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;2010-03&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>]]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;$&nbsp;lt&nbsp;2010&nbsp;1&nbsp;12&nbsp;17&nbsp;16&nbsp;15, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>&nbsp;$&nbsp;lt&nbsp;2010&nbsp;3&nbsp;12&nbsp;17&nbsp;16&nbsp;15, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>&nbsp;$&nbsp;lt&nbsp;2010&nbsp;1&nbsp;21&nbsp;17&nbsp;18&nbsp;19], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;d&quot;</span>&nbsp;$&nbsp;lt&nbsp;2010&nbsp;3&nbsp;1&nbsp;2&nbsp;3&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;PhotoFile&nbsp;<span style="color:#a31515;">&quot;e&quot;</span>&nbsp;$&nbsp;lt&nbsp;2011&nbsp;3&nbsp;4&nbsp;3&nbsp;2&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;2010-01&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;Leaf&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;2010-03&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>,&nbsp;Leaf&nbsp;<span style="color:#a31515;">&quot;d&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;2011-03&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;e&quot;</span>]]) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;moveTo&nbsp;destination&nbsp;source &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> This is an <a href="/2018/05/07/inlined-hunit-test-lists">inlined</a> <a href="/2018/04/30/parametrised-unit-tests-in-haskell">parametrised HUnit test</a>. While it looks like a big unit test, it still follows my <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">test formatting heuristic</a>. There's only three expressions, but the <em>arrange</em> expression is big because it creates a list of test cases. </p> <p> Each test case is a triple of a <code>source</code> tree, a <code>destination</code> directory name, and an <code>expected</code> result. In order to make the test data code more compact, it utilises this test-specific helper function: </p> <p> <pre>lt&nbsp;y&nbsp;mth&nbsp;d&nbsp;h&nbsp;m&nbsp;s&nbsp;=&nbsp;LocalTime&nbsp;(fromGregorian&nbsp;y&nbsp;mth&nbsp;d)&nbsp;(TimeOfDay&nbsp;h&nbsp;m&nbsp;s)</pre> </p> <p> For each test case, the test calls the <code>moveTo</code> function with the <code>destination</code> directory name and the <code>source</code> tree. It then asserts that the <code>expected</code> value is equal to the <code>actual</code> value. </p> <h3 id="bcf9e8fd9d1b42bbb47b811be75385d0"> Calculating moves <a href="#bcf9e8fd9d1b42bbb47b811be75385d0" title="permalink">#</a> </h3> <p> One pure step remains. The result of calling the <code>moveTo</code> function is a tree with the desired structure. In order to actually move the files, though, for each file you'll need to keep track of both the source path and the destination path. To make that explicit, you can define a type for that purpose: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Move&nbsp;= &nbsp;&nbsp;Move&nbsp;{&nbsp;sourcePath&nbsp;::&nbsp;FilePath,&nbsp;destinationPath&nbsp;::&nbsp;FilePath&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> A <code>Move</code> is simply a data structure. Contrast this with typical object-oriented design, where it would be a (possibly polymorphic) method on an object. In functional programming, you'll regularly model <em>intent</em> with a data structure. As long as intents remain data, you can easily manipulate them, and once you're done with that, you can run an interpreter over your data structure to perform the work you want accomplished. </p> <p> The unit test cases for the <code>moveTo</code> function suggest that file names are local file names like <code>"L"</code>, <code>"J"</code>, <code>"a"</code>, and so on. That was only to make the tests as compact as possible, since the function actually doesn't manipulate the specific <code>FilePath</code> values. </p> <p> In reality, the file names will most likely be longer, and they could also contain the full path, instead of the local path: <code>"C:\foo\bar\a.jpg"</code>. </p> <p> If you call <code>moveTo</code> with a tree where each leaf has a fully qualified path, the output tree will have the desired structure of the destination tree, but the leaves will still contain the full path to each source file. That means that you can calculate a <code>Move</code> for each file: </p> <p> <pre><span style="color:#2b91af;">calculateMoves</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">Move</span> calculateMoves&nbsp;=&nbsp;imp&nbsp;<span style="color:#a31515;">&quot;&quot;</span> &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;imp&nbsp;path&nbsp;&nbsp;&nbsp;&nbsp;(Leaf&nbsp;x)&nbsp;=&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;x&nbsp;$&nbsp;replaceDirectory&nbsp;x&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imp&nbsp;path&nbsp;(Node&nbsp;x&nbsp;xs)&nbsp;=&nbsp;Node&nbsp;(path&nbsp;&lt;/&gt;&nbsp;x)&nbsp;$&nbsp;imp&nbsp;(path&nbsp;&lt;/&gt;&nbsp;x)&nbsp;&lt;$&gt;&nbsp;xs</pre> </p> <p> This function takes as input a <code>Tree FilePath FilePath</code>, which is compatible with the output of <code>moveTo</code>. It returns a <code>Tree FilePath Move</code>, i.e. a tree where the leaves are <code>Move</code> values. </p> <p> To be fair, returning a tree is overkill. A <code>[Move]</code> (list of moves) would have been just as useful, but in this article, I'm trying to describe how to write code with a <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a>. In the overview article, I explained how you can model a file system using a rose tree, and in order to emphasise that point, I'll stick with that model a little while longer. </p> <p> Earlier, I wrote that you can implement desired <code>Tree</code> functionality with the <code>foldTree</code> function, but that was a simplification. If you can implement the functionality of <code>calculateMoves</code> with <code>foldTree</code>, I don't know how. You can, however, implement it using explicit pattern matching and simple recursion. </p> <p> The <code>imp</code> function builds up a file path (using the <code>&lt;/&gt;</code> path combinator) as it recursively negotiates the tree. All <code>Leaf</code> nodes are converted to a <code>Move</code> value using the leaf node's current <code>FilePath</code> value as the <code>sourcePath</code>, and the <code>path</code> to figure out the desired <code>destinationPath</code>. </p> <p> This code is still easy to unit test: </p> <p> <pre><span style="color:#a31515;">&quot;Calculate&nbsp;moves&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(tree,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Leaf&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Node&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>],&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;[Leaf&nbsp;$&nbsp;Move&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>&nbsp;$&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Node&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;Leaf&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>],&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>&nbsp;$&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>&nbsp;$&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Node&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;[Node&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>,&nbsp;Leaf&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>],&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>&nbsp;[Leaf&nbsp;<span style="color:#a31515;">&quot;3&quot;</span>]], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>)&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>&nbsp;$&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;1&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>&nbsp;$&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;b&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;2&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;(<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>)&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leaf&nbsp;$&nbsp;Move&nbsp;<span style="color:#a31515;">&quot;3&quot;</span>&nbsp;$&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>&nbsp;&lt;/&gt;&nbsp;<span style="color:#a31515;">&quot;3&quot;</span>]]) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;calculateMoves&nbsp;tree &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual</pre> </p> <p> The test cases in this parametrised test are tuples of an input <code>tree</code> and the <code>expected</code> tree. For each test case, the test calls the <code>calculateMoves</code> function with <code>tree</code> and asserts that the <code>actual</code> tree is equal to the <code>expected</code> tree. </p> <p> That's all the pure code you need in order to implement the desired functionality. Now you only need to write some code that loads a tree from disk, and imprints a destination tree to disk, as well as the code that composes it all. </p> <h3 id="062fff475b2b47e188dbd2bc930aa882"> Loading a tree from disk <a href="#062fff475b2b47e188dbd2bc930aa882" title="permalink">#</a> </h3> <p> The remaining code in this article is impure. You could put it in dedicated modules, but for this program, you're only going to need three functions and a bit of composition code, so you could also just put it all in the <code>Main</code> module. That's what I did. </p> <p> To load a tree from disk, you'll need a root directory, under which you load the entire tree. Given a directory path, you read a tree using a recursive function like this: </p> <p> <pre><span style="color:#2b91af;">readTree</span>&nbsp;::&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;(<span style="color:blue;">Tree</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:#2b91af;">FilePath</span>) readTree&nbsp;path&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;isFile&nbsp;&lt;-&nbsp;doesFileExist&nbsp;path &nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;isFile &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;Leaf&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dirsAndfiles&nbsp;&lt;-&nbsp;listDirectory&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;paths&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;(path&nbsp;&lt;/&gt;)&nbsp;dirsAndfiles &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;branches&nbsp;&lt;-&nbsp;traverse&nbsp;readTree&nbsp;paths &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;Node&nbsp;path&nbsp;branches</pre> </p> <p> This recursive function starts by checking whether the <code>path</code> is a file or a directory. If it's a file, it creates a new <code>Leaf</code> with that <code>FilePath</code>. </p> <p> If <code>path</code> isn't a file, it's a directory. In that case, use <code>listDirectory</code> to enumerate all the directories and files in that directory. These are only local names, so prefix them with <code>path</code> to create full paths, then <code>traverse</code> all those directory entries recursively. That produces all the <code>branches</code> for the current node. Finally, return a new <code>Node</code> with the <code>path</code> and the <code>branches</code>. </p> <h3 id="5ba31d6e6e7f4eee942e39349a45e1ed"> Loading metadata <a href="#5ba31d6e6e7f4eee942e39349a45e1ed" title="permalink">#</a> </h3> <p> The <code>readTree</code> function only produces a tree with <code>FilePath</code> leaves, while the program requires a tree with <code>PhotoFile</code> leaves. You'll need to read the <a href="https://en.wikipedia.org/wiki/Exif">Exif</a> metadata from each file and enrich the tree with the <em>date-taken</em> data. </p> <p> In this code base, I've used the <a href="http://hackage.haskell.org/package/hsexif">hsexif</a> library for this. That enables you to write an impure operation like this: </p> <p> <pre><span style="color:#2b91af;">readPhoto</span>&nbsp;::&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;(<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:blue;">PhotoFile</span>) readPhoto&nbsp;path&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;exifData&nbsp;&lt;-&nbsp;parseFileExif&nbsp;path &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;dateTaken&nbsp;=&nbsp;either&nbsp;(<span style="color:blue;">const</span>&nbsp;Nothing)&nbsp;Just&nbsp;exifData&nbsp;&gt;&gt;=&nbsp;getDateTimeOriginal &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;PhotoFile&nbsp;path&nbsp;&lt;$&gt;&nbsp;dateTaken</pre> </p> <p> This operation can fail for various reasons: <ul> <li>The file may not exist.</li> <li>The file exists, but has no metadata.</li> <li>The file has metadata, but no <em>date-taken</em> metadata.</li> <li>The <em>date-taken</em> metadata string is malformed.</li> </ul> The program is just going to skip all files from which it can't extract <em>date-taken</em> metadata, so <code>readPhoto</code> converts the <code>Either</code> value returned by <code>parseFileExif</code> to <code>Maybe</code> and binds the result with <code>getDateTimeOriginal</code>. </p> <p> When you <code>traverse</code> a <code>Tree FilePath FilePath</code> with <code>readPhoto</code>, you'll get a <code>Tree FilePath (Maybe PhotoFile)</code>. That's when you'll need <code>catMaybeTree</code>. You'll see this soon. </p> <h3 id="8b8d1709f9ed4fe2bc78e4ea9b2a2508"> Writing a tree to disk <a href="#8b8d1709f9ed4fe2bc78e4ea9b2a2508" title="permalink">#</a> </h3> <p> The above <code>calculateMoves</code> function creates a <code>Tree FilePath Move</code>. The final piece of impure code you'll need to write is an operation that traverses such a tree and executes each <code>Move</code>. </p> <p> <pre><span style="color:#2b91af;">applyMoves</span>&nbsp;::&nbsp;<span style="color:blue;">Foldable</span>&nbsp;t&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;t&nbsp;<span style="color:blue;">Move</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() applyMoves&nbsp;=&nbsp;traverse_&nbsp;move &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;move&nbsp;m&nbsp;=&nbsp;copy&nbsp;m&nbsp;&gt;&gt;&nbsp;compareFiles&nbsp;m&nbsp;&gt;&gt;=&nbsp;deleteSource &nbsp;&nbsp;&nbsp;&nbsp;copy&nbsp;(Move&nbsp;s&nbsp;d)&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createDirectoryIfMissing&nbsp;True&nbsp;$&nbsp;takeDirectory&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copyFileWithMetadata&nbsp;s&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;$&nbsp;<span style="color:#a31515;">&quot;Copied&nbsp;to&nbsp;&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;compareFiles&nbsp;m@(Move&nbsp;s&nbsp;d)&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceBytes&nbsp;&lt;-&nbsp;B.<span style="color:blue;">readFile</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destinationBytes&nbsp;&lt;-&nbsp;B.<span style="color:blue;">readFile</span>&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;<span style="color:blue;">if</span>&nbsp;sourceBytes&nbsp;==&nbsp;destinationBytes&nbsp;<span style="color:blue;">then</span>&nbsp;Just&nbsp;m&nbsp;<span style="color:blue;">else</span>&nbsp;Nothing &nbsp;&nbsp;&nbsp;&nbsp;deleteSource&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;=&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">()</span> &nbsp;&nbsp;&nbsp;&nbsp;deleteSource&nbsp;(Just&nbsp;(Move&nbsp;s&nbsp;_))&nbsp;=&nbsp;removeFile&nbsp;s</pre> </p> <p> As I wrote above, a tree of <code>Move</code> values is, to be honest, overkill. Any <code>Foldable</code> container will do, as the <code>applyMoves</code> operation demonstrates. It traverses the data structure, and for each <code>Move</code>, it first copies the file, then it verifies that the copy was successful, and finally, if that's the case, it deletes the source file. </p> <p> All of the operations invoked by these three steps are defined in various libraries part of the base GHC installation. You're welcome to peruse <a href="https://github.com/ploeh/picture-archivist">the source code repository</a> if you're interested in the details. </p> <h3 id="d336cf55dc9746c08cbed32041803173"> Composition <a href="#d336cf55dc9746c08cbed32041803173" title="permalink">#</a> </h3> <p> You can now compose an <a href="/2020/03/02/impureim-sandwich">impure-pure-impure sandwich</a> from all the Lego pieces: </p> <p> <pre><span style="color:#2b91af;">movePhotos</span>&nbsp;::&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">FilePath</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() movePhotos&nbsp;source&nbsp;destination&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;fold&nbsp;$&nbsp;runMaybeT&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;sourceTree&nbsp;&lt;-&nbsp;lift&nbsp;$&nbsp;readTree&nbsp;source &nbsp;&nbsp;photoTree&nbsp;&lt;-&nbsp;MaybeT&nbsp;$&nbsp;catMaybeTree&nbsp;&lt;$&gt;&nbsp;traverse&nbsp;readPhoto&nbsp;sourceTree &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;destinationTree&nbsp;=&nbsp;calculateMoves&nbsp;$&nbsp;moveTo&nbsp;destination&nbsp;photoTree &nbsp;&nbsp;lift&nbsp;$&nbsp;applyMoves&nbsp;destinationTree</pre> </p> <p> First, you load the <code>sourceTree</code> using the <code>readTree</code> operation. This is a <code>Tree FilePath FilePath</code> value, because the code is written in <code>do</code> notation, and the context is <code>MaybeT IO ()</code>. You then load the image metatadata by traversing <code>sourceTree</code> with <code>readPhoto</code>. This produces a <code>Tree FilePath (Maybe PhotoFile)</code> that you then filter with <code>catMaybeTree</code>. Again, because of <code>do</code> notation and monad transformer shenanigans, <code>photoTree</code> is a <code>Tree FilePath PhotoFile</code> value. </p> <p> Those two lines of code is the initial impure step of the sandwich (yes: mixed metaphors, I know). </p> <p> The pure part of the sandwich is the composition of the pure functions <code>moveTo</code> and <code>calculateMoves</code>. The result is a <code>Tree FilePath Move</code> value. </p> <p> The final, impure step of the sandwich, then, is to <code>applyMoves</code>. </p> <h3 id="8b44f4d2cd2241e18bff6d40c1ad9ee9"> Execution <a href="#8b44f4d2cd2241e18bff6d40c1ad9ee9" title="permalink">#</a> </h3> <p> The <code>movePhotos</code> operation takes <code>source</code> and <code>destination</code> arguments. You could hypothetically call it from a rich client or a background process, but here I'll just call if from a command-line program. The <code>main</code> operation will have to parse the input arguments and call <code>movePhotos</code>: </p> <p> <pre><span style="color:#2b91af;">main</span>&nbsp;::&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;() main&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;args&nbsp;&lt;-&nbsp;getArgs &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;args&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;[source,&nbsp;destination]&nbsp;-&gt;&nbsp;movePhotos&nbsp;source&nbsp;destination &nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;-&gt;&nbsp;<span style="color:blue;">putStrLn</span>&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;provide&nbsp;source&nbsp;and&nbsp;destination&nbsp;directories&nbsp;as&nbsp;arguments.&quot;</span></pre> </p> <p> You could write more sophisticated parsing of the program arguments, but that's not the topic of this article, so I only wrote the bare minimum required to get the program working. </p> <p> You can now compile and run the program: </p> <p> <pre>$ ./archpics "C:\Users\mark\Desktop\Test" "C:\Users\mark\Desktop\Test-Out" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2003-04\\2003-04-29 15.11.50.jpg" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2011-07\\2011-07-10 13.09.36.jpg" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2014-04\\2014-04-17 17.11.40.jpg" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2014-04\\2014-04-18 14.05.02.jpg" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2014-05\\2014-05-23 16.07.20.jpg" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2014-06\\2014-06-30 15.44.52.jpg" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2014-06\\2014-06-21 16.48.40.jpg" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2016-05\\2016-05-01 09.25.23.jpg" Copied to "C:\\Users\\mark\\Desktop\\Test-Out\\2017-08\\2017-08-22 19.53.28.jpg"</pre> </p> <p> This does indeed produce the expected destination directory structure. </p> <p> <img src="/content/binary/picture-archivist-destination-directory.png" alt="Seven example directories with pictures."> </p> <p> It's always nice when something turns out to work in practice, as well as in theory. </p> <h3 id="c50c7ac1276146d79715a5e7ddadfe6d"> Summary <a href="#c50c7ac1276146d79715a5e7ddadfe6d" title="permalink">#</a> </h3> <p> Functional software architecture involves separating pure from impure code so that no pure functions invoke impure operations. Often, you can achieve that with what I call the <em>impure-pure-impure sandwich</em> architecture. In this example, you saw how to model the file system as a tree. This enables you to separate the impure file interactions from the pure program logic. </p> <p> The Haskell type system enforces the <em>functional interaction law</em>, which implies that the architecture is, indeed, properly functional. Other languages, like <a href="https://fsharp.org">F#</a>, don't enforce the law via the compiler, but that doesn't prevent you doing functional programming. Now that we've verified that the architecture is, indeed, functional, we can port it to F#. </p> <p> <strong>Next:</strong> <a href="/2019/09/16/picture-archivist-in-f">Picture archivist in F#</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f237d98d453a4bcb9a3d58a05bf21d34"> <div class="comment-author"><a href="https://majiehong.com">Jiehong</a> <a href="#f237d98d453a4bcb9a3d58a05bf21d34">#</a></div> <div class="comment-content"> <p> This seems a fair architecture. </p> <p> However, at first glance it does not seem very memory efficient, because everything might be loaded in RAM, and that poses a strict limit. </p> <p> But then, I remember that Haskell does lazy evaluation, so is it the case here? Are path and the tree lazily loaded and processed? </p> <p> In "traditional" architectures, IO would be scattered inside the program, and as each file might be read one at a time, and handled. This sandwich of purity with impure buns forces not to do that. </p> </div> <div class="comment-date">2019-09-09 11:47 UTC</div> </div> <div class="comment" id="ca660cdc1f094bfb8cc9896bb1084460"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ca660cdc1f094bfb8cc9896bb1084460">#</a></div> <div class="comment-content"> <p> Jiehong, thank you for writing. It's true that Haskell is lazily evaluated, but some strictness rules apply to <code>IO</code>, so it's not so simple. </p> <p> Just running a quick experiment with the code base shown here, when I try to move thousands of files, the program sits and thinks for quite some time before it starts to output progress. This indicates to me that it does, indeed, load at least the <em>structure</em> of the tree into memory before it starts moving the files. Once it does that, though, it looks like it runs at constant memory. </p> <p> There's an interplay of laziness and <code>IO</code> in Haskell that I still don't sufficiently master. When I publish the port to F#, however, it should be clear that you could replace all the nodes of the tree with explicitly lazy values. I'd be surprised if something like that isn't possible in Haskell as well, but here I'll solicit help from readers more well-versed in these matters than I am. </p> </div> <div class="comment-date">2019-09-09 19:16 UTC</div> </div> <div class="comment" id="dd26f6d047b5492b8a012b30d96ad18b"> <div class="comment-author">André Cardoso <a href="#dd26f6d047b5492b8a012b30d96ad18b">#</a></div> <div class="comment-content"> <p> I really like your posts and I'm really liking this series. But I struggle with Haskell syntax, specially the difference between the operators $, &lt;$&gt;, &lt;&gt;, &lt;*&gt;. Is there a cheat sheet explaining these operators? </p> </div> <div class="comment-date">2019-09-12 13:51 UTC</div> </div> <div class="comment" id="2e71f695ed9f4cfa8467df818f072da8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2e71f695ed9f4cfa8467df818f072da8">#</a></div> <div class="comment-content"> <p> André, thank you for writing. I've written about why <a href="/2018/07/02/terse-operators-make-business-code-more-readable">I think that terse operators make the code overall more readable</a>, but that's obviously not an explanation of any of those operators. </p> <p> I'm not aware of any cheat sheets for Haskell, although a Google search seems to indicate that many exist. I'm not sure that a cheat sheet will help much if one doesn't know Haskell, and if one does know Haskell, one is likely to also know those operators. </p> <p> <a href="https://hackage.haskell.org/package/base/docs/Prelude.html#v:-36-">$</a> is a sort of delimiter that often saves you from having to nest other function calls in brackets. </p> <p> <a href="https://hackage.haskell.org/package/base/docs/Prelude.html#v:-60--36--62-">&lt;$&gt;</a> is just an infix alias for <code>fmap</code>. In C#, that <a href="/2018/03/22/functors">corresponds to the <code>Select</code> method</a>. </p> <p> <code>&lt;&gt;</code> is a generalised associative binary operation as defined by <a href="http://hackage.haskell.org/package/base/docs/Data-Semigroup.html">Data.Semigroup</a> or <a href="http://hackage.haskell.org/package/base/docs/Data-Monoid.html">Data.Monoid</a>. You can <a href="/2017/10/05/monoids-semigroups-and-friends">read more about monoids and semigroups here on the blog</a>. </p> <p> <a href="http://hackage.haskell.org/package/base/docs/Control-Applicative.html">&lt;*&gt;</a> is part of the <code>Applicative</code> type class. It's hard to translate to other languages, but <a href="/2018/10/01/applicative-functors">when I make the attempt</a>, I usually call it <code>Apply</code>. </p> </div> <div class="comment-date">2019-09-12 15:45 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Naming newtypes for QuickCheck Arbitraries https://blog.ploeh.dk/2019/09/02/naming-newtypes-for-quickcheck-arbitraries 2019-09-02T13:07:00+00:00 Mark Seemann <div id="post"> <p> <em>A simple naming scheme for newtypes to add Arbitrary instances.</em> </p> <p> Naming is one of those recurring difficult problems in software development. How do you come up with good names? </p> <p> I'm not aware of any <em>general</em> heuristic for that, but sometimes, in specific contexts, a naming scheme presents itself. Here's one. </p> <h3 id="c7391ad662e943f1bbe2b52d6b8bde59"> Orphan instances <a href="#c7391ad662e943f1bbe2b52d6b8bde59" title="permalink">#</a> </h3> <p> When you write <a href="http://hackage.haskell.org/package/QuickCheck">QuickCheck</a> properties that involve your own custom types, you'll have to add <code>Arbitrary</code> instances for those types. As an example, here's a restaurant reservation record type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Reservation&nbsp;=&nbsp;Reservation &nbsp;&nbsp;{&nbsp;reservationId&nbsp;::&nbsp;UUID &nbsp;&nbsp;,&nbsp;reservationDate&nbsp;::&nbsp;LocalTime &nbsp;&nbsp;,&nbsp;reservationName&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;reservationEmail&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;reservationQuantity&nbsp;::&nbsp;Int &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Read</span>,&nbsp;<span style="color:#2b91af;">Generic</span>)</pre> </p> <p> You can easily add an Arbitrary instance to such a type: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Arbitrary</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;arbitrary&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;liftM5&nbsp;Reservation&nbsp;arbitrary&nbsp;arbitrary&nbsp;arbitrary&nbsp;arbitrary&nbsp;arbitrary</pre> </p> <p> The type itself is part of your domain model, while the <code>Arbitrary</code> instance only belongs to your test code. You shouldn't add the <code>Arbitrary</code> instance to the domain model, but that means that you'll have to define the instance apart from the type definition. That, however, is an orphan instance, and the compiler will complain: </p> <p> <pre>test\ReservationAPISpec.hs:31:1: <span style="color:red;">warning:</span> [<span style="color:red;">-Worphans</span>] Orphan instance: instance Arbitrary Reservation To avoid this move the instance declaration to the module of the class or of the type, or wrap the type with a newtype and declare the instance on the new type. <span style="color:blue;">|</span> <span style="color:blue;">31 |</span> <span style="color:red;">instance Arbitrary Reservation where</span> <span style="color:blue;">|</span> <span style="color:red;">^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...</span></pre> </p> <p> Technically, this isn't a difficult problem to solve. The warning even suggests remedies. Moving the instance to the module that declares the type is, however, inappropriate, since test-specific instances don't belong in the domain model. Wrapping the type in a <code>newtype</code> is more appropriate, but what should you call the type? </p> <h3 id="c192d6524b4b4444a35121443f9a61a8"> Suppress the warning <a href="#c192d6524b4b4444a35121443f9a61a8" title="permalink">#</a> </h3> <p> I had trouble coming up with good names for such <code>newtype</code> wrappers, so at first I decided to just suppress that particular compiler warning. I simply added the <code>-fno-warn-orphans</code> flag <em>exclusively to my test code</em>. </p> <p> That solved the immediate problem, but I felt a little dirty. It's okay, though, because you're not supposed to reuse test libraries anyway, so the usual problems with orphan instances don't apply. </p> <p> After having worked a little like this, however, it dawned on me that I needed more than one <code>Arbitrary</code> instance, and a naming scheme presented itself. </p> <h3 id="a946b2c622c6403cb69a3f224551514c"> Naming scheme <a href="#a946b2c622c6403cb69a3f224551514c" title="permalink">#</a> </h3> <p> For some of the properties I wrote, I needed a <em>valid</em> <code>Reservation</code> value. In this case, <em>valid</em> means that the <code>reservationQuantity</code> is a positive number, and that the <code>reservationDate</code> is in the future. It seemed natural to signify these constraints with a <code>newtype</code>: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;ValidReservation&nbsp;=&nbsp;ValidReservation&nbsp;Reservation&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Arbitrary</span>&nbsp;<span style="color:blue;">ValidReservation</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;arbitrary&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;rid&nbsp;&lt;-&nbsp;arbitrary &nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;&lt;-&nbsp;(\dt&nbsp;-&gt;&nbsp;addLocalTime&nbsp;(getPositive&nbsp;dt)&nbsp;now2019)&nbsp;&lt;$&gt;&nbsp;arbitrary &nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;&lt;-&nbsp;arbitrary &nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;&lt;-&nbsp;arbitrary &nbsp;&nbsp;&nbsp;&nbsp;(Positive&nbsp;q)&nbsp;&lt;-&nbsp;arbitrary &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;ValidReservation&nbsp;$&nbsp;Reservation&nbsp;rid&nbsp;d&nbsp;n&nbsp;e&nbsp;q</pre> </p> <p> The <code>newtype</code> is, naturally, called <code>ValidReservation</code> and can, for example, be used like this: </p> <p> <pre>it&nbsp;<span style="color:#a31515;">&quot;responds&nbsp;with&nbsp;200&nbsp;after&nbsp;reservation&nbsp;is&nbsp;added&quot;</span>&nbsp;$&nbsp;WQC.property&nbsp;$&nbsp;\ &nbsp;&nbsp;(ValidReservation&nbsp;r)&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;_&nbsp;&lt;-&nbsp;postJSON&nbsp;<span style="color:#a31515;">&quot;/reservations&quot;</span>&nbsp;$&nbsp;encode&nbsp;r &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;get&nbsp;$&nbsp;<span style="color:#a31515;">&quot;/reservations/&quot;</span>&nbsp;&lt;&gt;&nbsp;toASCIIBytes&nbsp;(reservationId&nbsp;r) &nbsp;&nbsp;actual&nbsp;`shouldRespondWith`&nbsp;200</pre> </p> <p> For the few properties where <em>any</em> <code>Reservation</code> goes, a name for a <code>newtype</code> now also suggests itself: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;AnyReservation&nbsp;=&nbsp;AnyReservation&nbsp;Reservation&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Arbitrary</span>&nbsp;<span style="color:blue;">AnyReservation</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;arbitrary&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;AnyReservation&nbsp;&lt;$&gt; &nbsp;&nbsp;&nbsp;&nbsp;liftM5&nbsp;Reservation&nbsp;arbitrary&nbsp;arbitrary&nbsp;arbitrary&nbsp;arbitrary&nbsp;arbitrary</pre> </p> <p> The only use I've had for that particular instance so far, though, is to ensure that any <code>Reservation</code> correctly serialises to, and deserialises from, JSON: </p> <p> <pre>it&nbsp;<span style="color:#a31515;">&quot;round-trips&quot;</span>&nbsp;$&nbsp;property&nbsp;$&nbsp;\(AnyReservation&nbsp;r)&nbsp;-&gt;&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;json&nbsp;=&nbsp;encode&nbsp;r &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;decode&nbsp;json &nbsp;&nbsp;actual&nbsp;`shouldBe`&nbsp;Just&nbsp;r</pre> </p> <p> With those two <code>newtype</code> wrappers, I no longer have any orphan instances. </p> <h3 id="758fef8609784b998c3fad65b2fe6e2f"> Summary <a href="#758fef8609784b998c3fad65b2fe6e2f" title="permalink">#</a> </h3> <p> A simple naming scheme for <code>newtype</code> wrappers for QuickCheck <code>Arbitrary</code> instances, then, is: <ul> <li>If the instance is truly unbounded, prefix the wrapper name with <em>Any</em></li> <li>If the instance only produces valid values, prefix the wrapper name with <em>Valid</em></li> </ul> This strikes me as a practical naming scheme. Other variations seem natural. If, for example, you need an <em>invalid</em> value, you can prefix the wrapper name with <em>Invalid</em>. Why you'd need that, though, I'm not sure. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functional file system https://blog.ploeh.dk/2019/08/26/functional-file-system 2019-08-26T06:00:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you model file systems in a functional manner, so that unit testing is enabled? An overview.</em> </p> <p> One of the many reasons that I like functional programming is that it's <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. In object-oriented programming, you often have to jump through hoops to enable testing. This is also the case whenever you need to interact with the computer's file system. Just try to search the web for <em>file system interface</em>, or <em>mock file system</em>. I'm not going to give you any links, because I think such questions are <a href="https://en.wikipedia.org/wiki/XY_problem">XY problems</a>. I don't think that the most common suggestions are proper solutions. </p> <p> In functional programming, anyway, <a href="/2017/01/30/partial-application-is-dependency-injection">Dependency Injection isn't functional, because it makes everything impure</a>. How, then, do you model the file system in such a way that it's pure, decoupled from the logic you'd like to add on top of it, and still has enough fidelity that you can perform most tasks? </p> <p> You model the file system as a tree, or a forest. </p> <h3 id="4920bedd948d4f7487a13fa96f836371"> File systems are hierarchies <a href="#4920bedd948d4f7487a13fa96f836371" title="permalink">#</a> </h3> <p> It should come as no surprise that file systems are hierarchies, or trees. Each logical drive is the root of a tree. Files are leaves, and directories are internal nodes. Does that sound familiar? That sounds like a <a href="/2019/07/29/church-encoded-rose-tree">rose tree</a>. </p> <p> Rose trees are immutable data structures. It doesn't get much more functional than that. Why not use a rose tree (or a forest) to model the file system? </p> <p> What about interaction with the actual file system? Usually, when you encounter object-oriented attempts at decoupling an abstraction from the actual file system, you'll find polymorphic operations such as <code>WriteAllText</code>, <code>GetFileSystemEntries</code>, <code>CreateDirectory</code>, and so on. These would be the (mockable) methods that you have to implement, usually as <a href="http://xunitpatterns.com/Humble%20Object.html">Humble Objects</a>. </p> <p> If you, instead of a set of interfaces, model the file system as a forest, interacting with the actual file system is not even part of the abstraction. That's a typical shift of perspective from object-oriented design to functional programming. </p> <p> <img src="/content/binary/ood-and-fp-views-on-fily-system-abstraction.png" alt="Object-oriented and functional ways to abstractly model file systems."> </p> <p> In object-oriented design, you typically attempt to model <em>data with behaviour</em>. Sometimes that fits the underlying reality well, but in this case it doesn't. While you have file and directory objects with behaviour, the actual structure of a file system is implicit. It's hidden in the interactions between the objects. </p> <p> By modelling the file system as a tree, you explicitly use the structure of the data. How you load a tree into program memory, or how you imprint a tree unto the file system isn't part of the abstraction. When it comes to input and output, you're free to do what you want. </p> <p> Once you have a model of a directory structure in memory, you can manipulate it to your heart's content. Since <a href="/2019/08/19/a-rose-tree-functor">rose trees are functors</a>, you know that all transformations are structure-preserving. That means that you don't even need to write tests for those parts of your application. </p> <p> You'll appreciate an example, I'm sure. </p> <h3 id="5e19438122b94e059c155509e96c964f"> Picture archivist example <a href="#5e19438122b94e059c155509e96c964f" title="permalink">#</a> </h3> <p> As an example, I'll attempt to answer <a href="https://codereview.stackexchange.com/q/99271/3878">an old Code Review question</a>. I already gave <a href="https://codereview.stackexchange.com/a/99290/3878">an answer</a> in 2015, but I'm not so happy with it today as I was back then. The question is great, though, because it explicitly demonstrates how people have a hard time escaping the notion that abstraction is only available via interfaces or abstract base classes. In 2015, I had long since figured out that <a href="/2009/05/28/DelegatesAreAnonymousInterfaces">delegates (and thus functions) are anonymous interfaces</a>, but I still hadn't figured out how to separate pure from impure behaviour. </p> <p> The question's scenario is how to implement a small program that can inspect a collection of image files, extract the date-taken metadata from each file, and move the files to a new directory structure based on that information. </p> <p> For example, you could have files organised in various directories according to motive. </p> <p> <img src="/content/binary/picture-archivist-source-directory.png" alt="Three example directories with pictures."> </p> <p> You soon realise, however, that that archiving strategy is untenable, because what do you do if there's more than one type of motive in a picture? Instead, you decide to organise the files according to month and year. </p> <p> <img src="/content/binary/picture-archivist-destination-directory.png" alt="Seven example directories with pictures."> </p> <p> Clearly, there's some input and output involved in this application, but there's also some logic that you'd like to unit test. You need to parse the metadata, figure out where to move each image file, filter out files that are not images, and so on. </p> <h3 id="e3bc8b23a3494628a44348749a0369ca"> Object-oriented picture archivist <a href="#e3bc8b23a3494628a44348749a0369ca" title="permalink">#</a> </h3> <p> If you were to implement such a picture archivist program with an object-oriented design, you may use Dependency Injection so that you can 'mock' the file system during unit testing. A typical program might then work like this at run time: </p> <p> <img src="/content/binary/object-oriented-file-system-interaction.png" alt="An object-oriented program typically has busy interaction with the file system."> </p> <p> The program has fine-grained, busy interaction with the file system (through a polymorphic interface). It'll typically read one file, load its metadata, decide where to put the file, and copy it there. Then it'll move on to the next file, although it might also do this in parallel. Throughout the program execution, there's input and output going on, which makes it difficult to isolate the pure from the impure code. </p> <p> Even if you write a program like that in <a href="https://fsharp.org">F#</a>, it's hardly a <a href="/2018/11/19/functional-architecture-a-definition">functional architecture</a>. </p> <p> Such an architecture is, in theory, testable, but my experience is that if you attempt to reproduce such busy, fine-grained interaction with mocks and stubs, you're likely to end up with brittle tests. </p> <h3 id="6cddf0e7ca3549c49a87006bfba5d349"> Functional picture archivist <a href="#6cddf0e7ca3549c49a87006bfba5d349" title="permalink">#</a> </h3> <p> In functional programming, you'll have to <a href="/2017/02/02/dependency-rejection">reject the notion of dependencies</a>. Instead, you can often resort to the simple architecture I call an <a href="/2020/03/02/impureim-sandwich">impure-pure-impure sandwich</a>; here, specifically: <ol> <li>Load data from disk (impure)</li> <li>Transform the data (pure)</li> <li>Write data to disk (impure)</li> </ol> A typical program might then work like this at run time: </p> <p> <img src="/content/binary/functional-file-system-interaction.png" alt="A functional program typically loads data, transforms it, and stores it again."> </p> <p> When the program starts, it loads data from disk into a tree. It then manipulates the in-memory model of the files in question, and once it's done, it traverses the entire tree and applies the changes. </p> <p> This gives you a much clearer separation between the pure and impure parts of the code base. The pure part is bigger, and easier to unit test. </p> <h3 id="09d2184be64a428d85b4f01f1149ea7a"> Example code <a href="#09d2184be64a428d85b4f01f1149ea7a" title="permalink">#</a> </h3> <p> This article gave you an overview of the functional architecture. In the next two articles, you'll see how to do this in practice. First, I'll implement the above architecture in <a href="https://www.haskell.org">Haskell</a>, so that we know that if it works there, the architecture does, indeed, respect <a href="/2018/11/19/functional-architecture-a-definition">the functional interaction law</a>. </p> <p> Based on the Haskell implementation, you'll then see a port to F#. <ul> <li><a href="/2019/09/09/picture-archivist-in-haskell">Picture archivist in Haskell</a></li> <li><a href="/2019/09/16/picture-archivist-in-f">Picture archivist in F#</a></li> </ul> These two articles share the same architecture. You can read both, or one of them, as you like. The source code is available on GitHub. </p> <h3 id="09e32b681b7a48aa808965bd66c4794b"> Summary <a href="#09e32b681b7a48aa808965bd66c4794b" title="permalink">#</a> </h3> <p> One of the hardest problems in transitioning from object-oriented programming to functional programming is that the design approach is so different. Many well-understood design patterns and principles don't translate easily. Dependency Injection is one of those. Often, you'll have to flip the model on its head, so to speak, before you can take it on in a functional manner. </p> <p> While most object-oriented programmers would say that object-oriented design involves focusing on 'the nouns', in practice, it often revolves around interactions and behaviour. Sometimes, that's appropriate, but often, it's not. </p> <p> Functional programming, in contrast, tends to take a more data-oriented perspective. Load some data, manipulate it, and publish it. If you can come up with an appropriate data structure for the data, you're probably on your way to implementing a functional architecture. </p> <p> <strong>Next:</strong> <a href="/2019/09/09/picture-archivist-in-haskell">Picture archivist in Haskell</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A rose tree functor https://blog.ploeh.dk/2019/08/19/a-rose-tree-functor 2019-08-19T08:08:00+00:00 Mark Seemann <div id="post"> <p> <em>Rose trees form normal functors. A place-holder article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. As another article explains, <a href="/2019/08/12/rose-tree-bifunctor">a rose tree is a bifunctor</a>. This makes it trivially a functor. As such, this article is mostly a place-holder to fit the spot in the <em>functor table of contents</em>, thereby indicating that rose trees are functors. </p> <p> Since a rose tree is a bifunctor, it's actually not one, but two, functors. Many languages, C# included, are best equipped to deal with unambiguous functors. This is also true in <a href="https://haskell.org">Haskell</a>, where you'd usally define the <code>Functor</code> instance over a bifunctor's right, or second, side. Likewise, in C#, you can make <code>IRoseTree&lt;N, L&gt;</code> a functor by implementing <code>Select</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectLeaf(selector); }</pre> </p> <p> This method simply delegates all implementation to the <code>SelectLeaf</code> method; it's just <code>SelectLeaf</code> by another name. It obeys the functor laws, since these are just specializations of the bifunctor laws, and we know that a rose tree is a proper bifunctor. </p> <p> It would have been technically possible to instead implement a <code>Select</code> method by calling <code>SelectNode</code>, but it seems marginally more useful to enable syntactic sugar for mapping over the leaves. </p> <h3 id="134b75d98069421e9fe70a8630ac140f"> Menu example <a href="#134b75d98069421e9fe70a8630ac140f" title="permalink">#</a> </h3> <p> As an example, imagine that you're defining part of a menu bar for an old-fashioned desktop application. Perhaps you're even loading the structure of the menu from a text file. Doing so, you could create a simple tree that represents the <em>edit</em> menu: </p> <p> <pre><span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;editMenuTemplate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#a31515;">&quot;Edit&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#a31515;">&quot;Find&nbsp;and&nbsp;Replace&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;Find&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;Replace&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#a31515;">&quot;Case&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;Upper&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;Lower&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;Cut&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;Copy&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;Paste&quot;</span>));</pre> </p> <p> At this point, you have an <code>IRoseTree&lt;string, string&gt;</code>, so you might as well have used a <a href="/2018/08/06/a-tree-functor">'normal' tree</a> instead of a rose tree. The above template, however, is only a first step, because you have this <a href="https://en.wikipedia.org/wiki/Command_pattern">Command</a> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Command</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Command(<span style="color:blue;">string</span>&nbsp;name) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Execute() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Apart from this base class, you also have classes that derive from it: <code>FindCommand</code>, <code>ReplaceCommand</code>, and so on. These classes override the <code>Execute</code> method by implenting <em>find</em>, <em>replace</em>, etc. functionality. Imagine that you also have a store or dictionary of these derived objects. This enables you to transform the template tree into a useful user menu: </p> <p> <pre><span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:#2b91af;">Command</span>&gt;&nbsp;editMenu&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;name&nbsp;<span style="color:blue;">in</span>&nbsp;editMenuTemplate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;commandStore.Lookup(name);</pre> </p> <p> Notice how this transforms only the leaves, using the command store's <code>Lookup</code> method. This example uses C# query syntax, because this is what the <code>Select</code> method enables, but you could also have written the translation by just calling the <code>Select</code> method. </p> <p> The internal nodes in a menu have no behavious, so it makes little sense to attempt to turn them into <code>Command</code> objects as well. They're only there to provide structure to the menu. With a 'normal' tree, you wouldn't have been able to enrich only the leaves, while leaving the internal nodes untouched, but with a rose tree you can. </p> <p> The above example uses the <code>Select</code> method (via query syntax) to translate the nodes, thereby providing a demonstration of how to use the rose tree as the functor it is. </p> <h3 id="c77f1f9491b246f1bdb7c75d93eaa4ff"> Summary <a href="#c77f1f9491b246f1bdb7c75d93eaa4ff" title="permalink">#</a> </h3> <p> The <code>Select</code> doesn't implement any behaviour not already provided by <code>SelectLeaf</code>, but it enables C# query syntax. The C# compiler understands functors, but not bifunctors, so when you have a bifunctor, you might as well light up that language feature as well by adding a <code>Select</code> method. </p> <p> <strong>Next:</strong> <a href="/2018/08/13/a-visitor-functor">A Visitor functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Rose tree bifunctor https://blog.ploeh.dk/2019/08/12/rose-tree-bifunctor 2019-08-12T10:33:00+00:00 Mark Seemann <div id="post"> <p> <em>A rose tree forms a bifunctor. An article for object-oriented developers.</em> </p> <p> This article is an instalment in <a href="/2018/12/24/bifunctors">an article series about bifunctors</a>. While the overview article explains that there's essentially two practically useful bifunctors, here's a third one. <a href="https://en.wikipedia.org/wiki/Rose_tree">rose trees</a>. </p> <h3 id="985e3bc5291c4f8ba98ce258e78f4ec8"> Mapping both dimensions <a href="#985e3bc5291c4f8ba98ce258e78f4ec8" title="permalink">#</a> </h3> <p> Like in the <a href="/2019/01/07/either-bifunctor">previous article on the Either bifunctor</a>, I'll start by implementing the simultaneous two-dimensional translation <code>SelectBoth</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N1</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;SelectBoth&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">N1</span>,&nbsp;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">N1</span>&gt;&nbsp;selectNode, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;selectLeaf) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Cata( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node:&nbsp;(n,&nbsp;branches)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseNode</span>&lt;<span style="color:#2b91af;">N1</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;(selectNode(n),&nbsp;branches), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf:&nbsp;l&nbsp;=&gt;&nbsp;(<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N1</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;)<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">N1</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;(selectLeaf(l))); }</pre> </p> <p> This article uses the previously shown <a href="/2019/07/29/church-encoded-rose-tree">Church-encoded rose tree</a> and <a href="/2019/08/05/rose-tree-catamorphism">its catamorphism</a> <code>Cata</code>. </p> <p> In the <code>leaf</code> case, the <code>l</code> argument received by the lambda expression is an object of the type <code>L</code>, since the <code>source</code> tree is an <code>IRoseTree&lt;N, L&gt;</code> object; i.e. a tree with leaves of the type <code>L</code> and nodes of the type <code>N</code>. The <code>selectLeaf</code> argument is a function that converts an <code>L</code> object to an <code>L1</code> object. Since <code>l</code> is an <code>L</code> object, you can call <code>selectLeaf</code> with it to produce an <code>L1</code> object. You can use this resulting object to create a new <code>RoseLeaf&lt;N1, L1&gt;</code>. Keep in mind that while the <code>RoseLeaf</code> class requires two type arguments, it never requires an object of its <code>N</code> type argument, which means that you can create an object with any <em>node</em> type argument, including <code>N1</code>, even if you don't have an object of that type. </p> <p> In the <code>node</code> case, the lambda expression receives two objects: <code>n</code> and <code>branches</code>. The <code>n</code> object has the type <code>N</code>, while the <code>branches</code> object has the type <code>IEnumerable&lt;IRoseTree&lt;N1, L1&gt;&gt;</code>. In other words, the <code>branches</code> have already been translated to the desired result type. That's how the catamorphism works. This means that you only have to figure out how to translate the <code>N</code> object <code>n</code> to an <code>N1</code> object. The <code>selectNode</code> function argument can do that, so you can then create a new <code>RoseNode&lt;N1, L1&gt;</code> and return it. </p> <p> This works as expected: </p> <p> <pre>&gt; <span style="color:blue;">var</span>&nbsp;tree&nbsp;=&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1337)); &gt; tree RoseNode&lt;string, int&gt;("foo", IRoseTree&lt;string, int&gt;[2] { 42, 1337 }) &gt; tree.SelectBoth(s&nbsp;=&gt;&nbsp;s.Length,&nbsp;i&nbsp;=&gt;&nbsp;i.ToString()) RoseNode&lt;int, string&gt;(3, IRoseTree&lt;int, string&gt;[2] { "42", "1337" })</pre> </p> <p> This <em>C# Interactive</em> example shows how to convert a tree with internal string nodes and integer leaves to a tree of internal integer nodes and string leaves. The strings are converted to strings by counting their <code>Length</code>, while the integers are turned into strings using the standard <code>ToString</code> method available on all objects. </p> <h3 id="c0ea04cfe7d3412c86b9ba3953812025"> Mapping nodes <a href="#c0ea04cfe7d3412c86b9ba3953812025" title="permalink">#</a> </h3> <p> When you have <code>SelectBoth</code>, you can trivially implement the translations for each dimension in isolation. For <a href="/2018/12/31/tuple-bifunctor">tuple bifunctors</a>, I called these methods <code>SelectFirst</code> and <code>SelectSecond</code>, while for <a href="/2019/01/07/either-bifunctor">Either bifunctors</a>, I chose to name them <code>SelectLeft</code> and <code>SelectRight</code>. Continuing the trend of naming the translations after what they translate, instead of their positions, I'll name the corresponding methods here <code>SelectNode</code> and <code>SelectLeaf</code>. In <a href="https://www.haskell.org">Haskell</a>, the functions associated with <code>Data.Bifunctor</code> are always called <code>first</code> and <code>second</code>, but I see no reason to preserve such abstract naming in C#. In Haskell, these functions are part of the <code>Bifunctor</code> type class; the abstract names serve an actual purpose. This isn't the case in C#, so there's no reason to retain the abstract names. You might as well use names that communicate intent, which is what I've tried to do here. </p> <p> If you want to map only the internal nodes, you can implement a <code>SelectNode</code> method based on <code>SelectBoth</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N1</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;SelectNode&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">N1</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">N1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectBoth(selector,&nbsp;l&nbsp;=&gt;&nbsp;l); }</pre> </p> <p> This simply uses the <code>l =&gt; l</code> lambda expression as an ad-hoc <em>identity</em> function, while passing <code>selector</code> as the <code>selectNode</code> argument to the <code>SelectBoth</code> method. </p> <p> You can use this to map the above <code>tree</code> to a tree made entirely of numbers: </p> <p> <pre>&gt; <span style="color:blue;">var</span>&nbsp;tree&nbsp;=&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1337)); &gt; tree.SelectNode(s =&gt; s.Length) RoseNode&lt;int, int&gt;(3, IRoseTree&lt;int, int&gt;[2] { 42, 1337 })</pre> </p> <p> Such a tree is, incidentally, isomorphic to a <a href="/2018/08/06/a-tree-functor">'normal' tree</a>. It might be a good exercise, if you need one, to demonstrate the isormorphism by writing functions that convert a <code>Tree&lt;T&gt;</code> into an <code>IRoseTree&lt;T, T&gt;</code>, and vice versa. </p> <h3 id="baa9136b506241e39e13639e43679b31"> Mapping leaves <a href="#baa9136b506241e39e13639e43679b31" title="permalink">#</a> </h3> <p> Similar to <code>SelectNode</code>, you can also trivially implement <code>SelectLeaf</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;SelectLeaf&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectBoth(n&nbsp;=&gt;&nbsp;n,&nbsp;selector); }</pre> </p> <p> This is another one-liner calling <code>SelectBoth</code>, with the difference that the identity function <code>n =&gt; n</code> is passed as the first argument, instead of as the last. This ensures that only <code>RoseLeaf</code> values are mapped: </p> <p> <pre>&gt; <span style="color:blue;">var</span>&nbsp;tree&nbsp;=&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1337)); &gt; tree.SelectLeaf(i =&gt; i % 2 == 0) RoseNode&lt;string, bool&gt;("foo", IRoseTree&lt;string, bool&gt;[2] { true, false })</pre> </p> <p> In the above <em>C# Interactive</em> session, the leaves are mapped to Boolean values, indicating whether they're even or odd. </p> <h3 id="afddb846bd244f4aa8f658fb5716b392"> Identity laws <a href="#afddb846bd244f4aa8f658fb5716b392" title="permalink">#</a> </h3> <p> Rose trees obey all the bifunctor laws. While it's formal work to prove that this is the case, you can get an intuition for it via examples. Often, I use a property-based testing library like <a href="https://fscheck.github.io/FsCheck">FsCheck</a> or <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> to demonstrate (not prove) that laws hold, but in this article, I'll keep it simple and only cover each law with a parametrised test. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Id&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;x)&nbsp;=&gt;&nbsp;x; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">object</span>[]&gt;&nbsp;BifunctorLawsData { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(42)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(42,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;exampleTree&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectNodeObeysFirstFunctorLaw(<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;t) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectNode(Id)); }</pre> </p> <p> This test uses <a href="https://xunit.net">xUnit.net</a>'s <code>[Theory]</code> feature to supply a small set of example input values. The input values are defined by the <code>BifunctorLawsData</code> property, since I'll reuse the same values for all the bifunctor law demonstration tests. The <code>exampleTree</code> object is the tree shown in <a href="/2019/07/29/church-encoded-rose-tree">Church-encoded rose tree</a>. </p> <p> The tests also use the identity function implemented as a <code>private</code> function called <code>Id</code>, since C# doesn't come equipped with such a function in the Base Class Library. </p> <p> For all the <code>IRoseTree&lt;int, string&gt;</code> objects <code>t</code>, the test simply verifies that the original tree <code>t</code> is equal to the tree projected over the first axis with the <code>Id</code> function. </p> <p> Likewise, the first functor law applies when translating over the second dimension: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectLeafObeysFirstFunctorLaw(<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;t) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectLeaf(Id)); }</pre> </p> <p> This is the same test as the previous test, with the only exception that it calls <code>SelectLeaf</code> instead of <code>SelectNode</code>. </p> <p> Both <code>SelectNode</code> and <code>SelectLeaf</code> are implemented by <code>SelectBoth</code>, so the real test is whether this method obeys the identity law: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothObeysIdentityLaw(<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;t) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectBoth(Id,&nbsp;Id)); }</pre> </p> <p> Projecting over both dimensions with the identity function does, indeed, return an object equal to the input object. </p> <h3 id="bfaa0b763e5346c488f4bd9576ab894c"> Consistency law <a href="#bfaa0b763e5346c488f4bd9576ab894c" title="permalink">#</a> </h3> <p> In general, it shouldn't matter whether you map with <code>SelectBoth</code> or a combination of <code>SelectNode</code> and <code>SelectLeaf</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConsistencyLawHolds(<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;t) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;f(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;g(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.IsNullOrWhiteSpace(s); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t.SelectBoth(f,&nbsp;g),&nbsp;t.SelectLeaf(g).SelectNode(f)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectNode(f).SelectLeaf(g), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectLeaf(g).SelectNode(f)); }</pre> </p> <p> This example creates two local functions <code>f</code> and <code>g</code>. The first function, <code>f</code>, creates a new <code>DateTime</code> object from an integer, using one of the <code>DateTime</code> constructor overloads. The second function, <code>g</code>, just delegates to <code>string.IsNullOrWhiteSpace</code>, although I want to stress that this is just an example. The law should hold for any two (<a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>) functions. </p> <p> The test then verifies that you get the same result from calling <code>SelectBoth</code> as when you call <code>SelectNode</code> followed by <code>SelectLeaf</code>, or the other way around. </p> <h3 id="dd3046c49d564991bb47924b6e8e65fb"> Composition laws <a href="#dd3046c49d564991bb47924b6e8e65fb" title="permalink">#</a> </h3> <p> The composition laws insist that you can compose functions, or translations, and that again, the choice to do one or the other doesn't matter. Along each of the axes, it's just the second functor law applied. This parametrised test demonstrates that the law holds for <code>SelectNode</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectNode(<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;t) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">char</span>&nbsp;f(<span style="color:blue;">bool</span>&nbsp;b)&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectNode(x&nbsp;=&gt;&nbsp;f(g(x))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectNode(g).SelectNode(f)); }</pre> </p> <p> Here, <code>f</code> is a local function that returns the the character <code>'T'</code> for <code>true</code>, and <code>'F'</code> for <code>false</code>; <code>g</code> is the <em>even</em> function. The second functor law states that mapping <code>f(g(x))</code> in a single step is equivalent to first mapping over <code>g</code> and then map the result of that using <code>f</code>. </p> <p> The same law applies if you fix the first dimension and translate over the second: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectLeaf(<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;t) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;g(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectLeaf(x&nbsp;=&gt;&nbsp;f(g(x))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectLeaf(g).SelectLeaf(f)); }</pre> </p> <p> Here, <code>f</code> is the <em>even</em> function, whereas <code>g</code> is a local function that returns the length of a string. Again, the test demonstrates that the output is the same whether you map over an intermediary step, or whether you map using only a single step. </p> <p> This generalises to the composition law for <code>SelectBoth</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothCompositionLawHolds(<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;t) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">char</span>&nbsp;f(<span style="color:blue;">bool</span>&nbsp;b)&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;h(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;i(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectBoth(x&nbsp;=&gt;&nbsp;f(g(x)),&nbsp;y&nbsp;=&gt;&nbsp;h(i(y))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectBoth(g,&nbsp;i).SelectBoth(f,&nbsp;h)); }</pre> </p> <p> Again, whether you translate in one or two steps shouldn't affect the outcome. </p> <p> As all of these tests demonstrate, the bifunctor laws hold for rose trees. The tests only showcase five examples, but I hope it gives you an intuition how any rose tree is a bifunctor. After all, the <code>SelectNode</code>, <code>SelectLeaf</code>, and <code>SelectBoth</code> methods are all generic, and they behave the same for all generic type arguments. </p> <h3 id="a1a5dea3d85d4ed1a3ee3fb0a4dca820"> Summary <a href="#a1a5dea3d85d4ed1a3ee3fb0a4dca820" title="permalink">#</a> </h3> <p> Rose trees are bifunctors. You can translate the node and leaf dimension of a rose tree independently of each other, and the bifunctor laws hold for any pure translation, no matter how you compose the projections. </p> <p> As always, there can be performance differences between the various compositions, but the outputs will be the same regardless of composition. </p> <p> A functor, and by extension, a bifunctor, is a structure-preserving map. This means that any projection preserves the structure of the underlying container. For rose trees this means that the shape of the tree remains the same. The number of leaves remain the same, as does the number of internal nodes. </p> <p> <strong>Next:</strong> <a href="/2021/09/02/contravariant-functors">Contravariant functors</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Rose tree catamorphism https://blog.ploeh.dk/2019/08/05/rose-tree-catamorphism 2019-08-05T08:30:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for a tree with different types of nodes and leaves is made up from two functions.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for a <a href="https://en.wikipedia.org/wiki/Rose_tree">rose tree</a>, as well as how to identify it. The beginning of this article presents the catamorphism in C#, with examples. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <p> A rose tree is a general-purpose data structure where each node in a tree has an associated value. Each node can have an arbitrary number of branches, including none. The distinguishing feature from a rose tree and just any <a href="https://en.wikipedia.org/wiki/Tree_(data_structure)">tree</a> is that internal nodes can hold values of a different type than leaf values. </p> <p> <img src="/content/binary/rose-tree-example.png" alt="A rose tree example diagram, with internal nodes containing integers, and leafs containing strings."> </p> <p> The diagram shows an example of a tree of internal integers and leaf strings. All internal nodes contain integer values, and all leaves contain strings. Each node can have an arbitrary number of branches. </p> <h3 id="078386d5f3924a63add86ff199fd88d0"> C# catamorphism <a href="#078386d5f3924a63add86ff199fd88d0" title="permalink">#</a> </h3> <p> As a C# representation of a rose tree, I'll use the <a href="/2019/07/29/church-encoded-rose-tree">Church-encoded rose tree I've previously described</a>. The catamorphism is this extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Cata&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;tree, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;node, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;leaf) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node:&nbsp;(n,&nbsp;branches)&nbsp;=&gt;&nbsp;node(n,&nbsp;branches.Select(t&nbsp;=&gt;&nbsp;t.Cata(node,&nbsp;leaf))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf:&nbsp;leaf); }</pre> </p> <p> Like most of the other catamorphisms shown in this article series, this one consists of two functions. One that handles the <em>leaf</em> case, and one that handles the partially reduced <em>node</em> case. Compare it with the <a href="/2019/06/10/tree-catamorphism">tree catamorphism</a>: notice that the rose tree catamorphism's <code>node</code> function is identical to the the tree catamorphism. The <code>leaf</code> function, however, is new. </p> <p> In previous articles, you've seen other examples of catamorphisms for <a href="/2018/05/22/church-encoding">Church-encoded</a> types. The most common pattern has been that the Church encoding (the <code>Match</code> method) was also the catamorphism, with the <a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a> being the only exception so far. When it comes to the Peano catamorphism, however, I'm not entirely confident that the difference between Church encoding and catamorphism is real, or whether it's just an artefact of the way I originally designed the Church encoding. </p> <p> When it comes to the present rose tree, however, notice that the catamorphisms is distinctly different from the Church encoding. That's the reason I called the method <code>Cata</code> instead of <code>Match</code>. </p> <p> The method simply delegates the <code>leaf</code> handler to <code>Match</code>, while it adds behaviour to the <code>node</code> case. It works the same way as for the 'normal' tree catamorphism. </p> <h3 id="87e2c79711c24c63a5ed82fbe4f7b581"> Examples <a href="#87e2c79711c24c63a5ed82fbe4f7b581" title="permalink">#</a> </h3> <p> You can use <code>Cata</code> to implement most other behaviour you'd like <code>IRoseTree&lt;N, L&gt;</code> to have. In a future article, you'll see how to <a href="/2019/08/12/rose-tree-bifunctor">turn the rose tree into a bifunctor</a> and <a href="/2019/08/19/a-rose-tree-functor">functor</a>, so here, we'll look at some other, more ad hoc, examples. As is also the case for the 'normal' tree, you can calculate the sum of all nodes, if you can associate a number with each node. </p> <p> Consider the example tree in the above diagram. You can create it as an <code>IRoseTree&lt;int, string&gt;</code> object like this: </p> <p> <pre><span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;exampleTree&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(42, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(1337, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(2112, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(90125, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;baz&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;qux&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;quux&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;quuz&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;corge&quot;</span>));</pre> </p> <p> If you want to calculate a sum for a tree like that, you can use the integers for the internal nodes, and perhaps the length of the strings of the leaves. That hardly makes much sense, but is technically possible: </p> <p> <pre>&gt; exampleTree.Cata((x,&nbsp;xs)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;xs.Sum(),&nbsp;x&nbsp;=&gt;&nbsp;x.Length) 93641</pre> </p> <p> Perhaps slightly more useful is to count the number of leaves: </p> <p> <pre>&gt; exampleTree.Cata((_,&nbsp;xs)&nbsp;=&gt;&nbsp;xs.Sum(),&nbsp;_&nbsp;=&gt;&nbsp;1) 7</pre> </p> <p> A leaf node has, by definition, exactly one leaf node, so the <code>leaf</code> lambda expression always returns <code>1</code>. In the <code>node</code> case, <code>xs</code> contains the partially summed leaf node count, so just <code>Sum</code> those together while ignoring the value of the internal node. </p> <p> You can also measure the maximum depth of the tree: </p> <p> <pre>&gt; exampleTree.Cata((_,&nbsp;xs)&nbsp;=&gt;&nbsp;1&nbsp;+&nbsp;xs.Max(),&nbsp;_&nbsp;=&gt;&nbsp;0) 3</pre> </p> <p> Consistent with the example for 'normal' trees, you can arbitrarily decide that the depth of a leaf node is <code>0</code>, so again, the <code>leaf</code> lambda expression just returns a constant value. The <code>node</code> lambda expression takes the <code>Max</code> of the partially reduced <code>xs</code> and adds <code>1</code>, since an internal node represents another level of depth in a tree. </p> <h3 id="9e673c50edc14c1790a9e89a67d069d1"> Rose tree F-Algebra <a href="#9e673c50edc14c1790a9e89a67d069d1" title="permalink">#</a> </h3> <p> As in the <a href="/2019/06/10/tree-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. </p> <p> As always, start with the underlying endofunctor: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;RoseTreeF&nbsp;a&nbsp;b&nbsp;c&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;NodeF&nbsp;{&nbsp;nodeValue&nbsp;::&nbsp;a,&nbsp;nodes&nbsp;::&nbsp;ListFix&nbsp;c&nbsp;} &nbsp;&nbsp;|&nbsp;LeafF&nbsp;{&nbsp;leafValue&nbsp;::&nbsp;b&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">RoseTreeF</span>&nbsp;a&nbsp;b)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;(NodeF&nbsp;x&nbsp;ns)&nbsp;=&nbsp;NodeF&nbsp;x&nbsp;$&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;ns &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;(LeafF&nbsp;x)&nbsp;=&nbsp;LeafF&nbsp;x</pre> </p> <p> Instead of using Haskell's standard list (<code>[]</code>) for the nodes, I've used <code>ListFix</code> from <a href="/2019/05/27/list-catamorphism">the article on list catamorphism</a>. This should, hopefully, demonstrate how you can build on already established definitions derived from first principles. </p> <p> As usual, I've called the 'data' types <code>a</code> and <code>b</code>, and the carrier type <code>c</code> (for <em>carrier</em>). The <code>Functor</code> instance as usual translates the carrier type; the <code>fmap</code> function has the type <code>(c -&gt; c1) -&gt; RoseTreeF a b c -&gt; RoseTreeF a b c1</code>. </p> <p> As was the case when deducing the recent catamorphisms, Haskell isn't too happy about defining instances for a type like <code>Fix (RoseTreeF a b)</code>. To address that problem, you can introduce a <code>newtype</code> wrapper: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;RoseTreeFix&nbsp;a&nbsp;b&nbsp;= &nbsp;&nbsp;RoseTreeFix&nbsp;{&nbsp;unRoseTreeFix&nbsp;::&nbsp;Fix&nbsp;(RoseTreeF&nbsp;a&nbsp;b)&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> You can define <code>Bifunctor</code>, <code>Bifoldable</code>, <code>Bitraversable</code>, etc. instances for this type without resorting to any funky GHC extensions. Keep in mind that ultimately, the purpose of all this code is just to figure out what the catamorphism looks like. This code isn't intended for actual use. </p> <p> A pair of helper functions make it easier to define <code>RoseTreeFix</code> values: </p> <p> <pre><span style="color:#2b91af;">roseLeafF</span>&nbsp;::&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;a&nbsp;b roseLeafF&nbsp;=&nbsp;RoseTreeFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;LeafF <span style="color:#2b91af;">roseNodeF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;(<span style="color:blue;">RoseTreeFix</span>&nbsp;a&nbsp;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;a&nbsp;b roseNodeF&nbsp;x&nbsp;=&nbsp;RoseTreeFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;NodeF&nbsp;x&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;unRoseTreeFix</pre> </p> <p> <code>roseLeafF</code> creates a leaf node: </p> <p> <pre>Prelude Fix List RoseTree&gt; roseLeafF "ploeh" RoseTreeFix {unRoseTreeFix = Fix (LeafF "ploeh")}</pre> </p> <p> <code>roseNodeF</code> is a helper function to create internal nodes: </p> <p> <pre>Prelude Fix List RoseTree&gt; roseNodeF 6 (consF (roseLeafF 0) nilF) RoseTreeFix {unRoseTreeFix = Fix (NodeF 6 (ListFix (Fix (ConsF (Fix (LeafF 0)) (Fix NilF)))))}</pre> </p> <p> Even with helper functions, construction of <code>RoseTreeFix</code> values is cumbersome, but keep in mind that the code shown here isn't meant to be used in practice. The goal is only to deduce catamorphisms from more basic universal abstractions, and you now have all you need to do that. </p> <h3 id="0bfc3f600a9e43eea1026f1a4a3b7604"> Haskell catamorphism <a href="#0bfc3f600a9e43eea1026f1a4a3b7604" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>RoseTreeF a b</code>), and an object <code>c</code>, but you still need to find a morphism <code>RoseTreeF a b c -&gt; c</code>. Notice that the algebra you have to find is the function that reduces the functor to its <em>carrier type</em> <code>c</code>, not any of the 'data types' <code>a</code> or <code>b</code>. This takes some time to get used to, but that's how catamorphisms work. This doesn't mean, however, that you get to ignore <code>a</code> or <code>b</code>, as you'll see. </p> <p> As in the previous articles, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>roseTreeF&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unRoseTreeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(NodeF&nbsp;x&nbsp;ns)&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;(LeafF&nbsp;x)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from the <code>LeafF</code> case? You could pass a function argument to the <code>roseTreeF</code> function and use it with <code>x</code>: </p> <p> <pre>roseTreeF&nbsp;fl&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unRoseTreeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(NodeF&nbsp;x&nbsp;ns)&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;(LeafF&nbsp;x)&nbsp;=&nbsp;fl&nbsp;x</pre> </p> <p> While you could, technically, pass an argument of the type <code>c</code> to <code>roseTreeF</code> and then return that value from the <code>LeafF</code> case, that would mean that you would ignore the <code>x</code> value. This would be incorrect, so instead, make the argument a function and call it with <code>x</code>. Likewise, you can deal with the <code>NodeF</code> case in the same way: </p> <p> <pre><span style="color:#2b91af;">roseTreeF</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;a&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c roseTreeF&nbsp;fn&nbsp;fl&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unRoseTreeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(NodeF&nbsp;x&nbsp;ns)&nbsp;=&nbsp;fn&nbsp;x&nbsp;ns &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;(LeafF&nbsp;x)&nbsp;=&nbsp;fl&nbsp;x</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>RoseTreeF</code>, the compiler infers that the <code>alg</code> function has the type <code>RoseTreeF a b c -&gt; c</code>, which is just what you need! </p> <p> You can now see what the carrier type <code>c</code> is for. It's the type that the algebra extracts, and thus the type that the catamorphism returns. </p> <p> This, then, is the catamorphism for a rose tree. As has been the most common pattern so far, it's a pair, made from two functions. It's still not the only possible catamorphism, since you could trivially flip the arguments to <code>roseTreeF</code>, or the arguments to <code>fn</code>. </p> <p> I've chosen the representation shown here because it's similar to the catamorphism I've shown for a 'normal' tree, just with the added function for leaves. </p> <h3 id="256fd0a09c4a4651b6c27b5626b0fb33"> Basis <a href="#256fd0a09c4a4651b6c27b5626b0fb33" title="permalink">#</a> </h3> <p> You can implement most other useful functionality with <code>roseTreeF</code>. Here's the <code>Bifunctor</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bifunctor</span>&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bimap&nbsp;f&nbsp;s&nbsp;=&nbsp;roseTreeF&nbsp;(roseNodeF&nbsp;.&nbsp;f)&nbsp;(roseLeafF&nbsp;.&nbsp;s)</pre> </p> <p> Notice how naturally the catamorphism implements <code>bimap</code>. </p> <p> From that instance, the <code>Functor</code> instance trivially follows: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">RoseTreeFix</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;=&nbsp;second</pre> </p> <p> You could probably also add <code>Applicative</code> and <code>Monad</code> instances, but I find those hard to grasp, so I'm going to skip them in favour of <code>Bifoldable</code>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bifoldable</span>&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bifoldMap&nbsp;f&nbsp;=&nbsp;roseTreeF&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;f&nbsp;x&nbsp;&lt;&gt;&nbsp;fold&nbsp;xs)</pre> </p> <p> The <code>Bifoldable</code> instance enables you to trivially implement the <code>Foldable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;(<span style="color:blue;">RoseTreeFix</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;foldMap&nbsp;=&nbsp;bifoldMap&nbsp;mempty</pre> </p> <p> You may find the presence of <code>mempty</code> puzzling, since <code>bifoldMap</code> takes two functions as arguments. Is <code>mempty</code> a function? </p> <p> Yes, <code>mempty</code> can be a function. Here, it is. There's a <code>Monoid</code> instance for any function <code>a -&gt; m</code>, where <code>m</code> is a <code>Monoid</code> instance, and <code>mempty</code> is the identity for that monoid. That's the instance in use here. </p> <p> Just as <code>RoseTreeFix</code> is <code>Bifoldable</code>, it's also <code>Bitraversable</code>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bitraversable</span>&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bitraverse&nbsp;f&nbsp;s&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;roseTreeF&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;roseNodeF&nbsp;&lt;$&gt;&nbsp;f&nbsp;x&nbsp;&lt;*&gt;&nbsp;sequenceA&nbsp;xs)&nbsp;(<span style="color:blue;">fmap</span>&nbsp;roseLeafF&nbsp;.&nbsp;s)</pre> </p> <p> You can comfortably implement the <code>Traversable</code> instance based on the <code>Bitraversable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;(<span style="color:blue;">RoseTreeFix</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;bisequenceA&nbsp;.&nbsp;first&nbsp;pure</pre> </p> <p> That rose trees are <code>Traversable</code> turns out to be useful, as a future article will show. </p> <h3 id="c02950d3b4954435b384b1f7520d24d4"> Relationships <a href="#c02950d3b4954435b384b1f7520d24d4" title="permalink">#</a> </h3> <p> As was the case for 'normal' trees, the catamorphism for rose trees is more powerful than the <em>fold</em>. There are operations that you can express with the <code>Foldable</code> instance, but other operations that you can't. Consider the tree shown in the diagram at the beginning of the article. This is also the tree that the above C# examples use. In Haskell, using <code>RoseTreeFix</code>, you can define that tree like this: </p> <p> <pre>exampleTree&nbsp;= &nbsp;&nbsp;roseNodeF&nbsp;42&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roseNodeF&nbsp;1337&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(roseLeafF&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(roseLeafF&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>)&nbsp;nilF))&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roseNodeF&nbsp;2112&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roseNodeF&nbsp;90125&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(roseLeafF&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(roseLeafF&nbsp;<span style="color:#a31515;">&quot;qux&quot;</span>)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(roseLeafF&nbsp;<span style="color:#a31515;">&quot;quux&quot;</span>)&nbsp;nilF))&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(roseLeafF&nbsp;<span style="color:#a31515;">&quot;quuz&quot;</span>)&nbsp;nilF))&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roseLeafF&nbsp;<span style="color:#a31515;">&quot;corge&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;nilF)</pre> </p> <p> You can trivially calculate the sum of string lengths of all leaves, using only the <code>Foldable</code> instance: </p> <p> <pre>Prelude RoseTree&gt; sum $ length &lt;$&gt; exampleTree 25</pre> </p> <p> You can also fairly easily calculate a sum of all nodes, using the length of the strings as in the above C# example, but that requires the <code>Bifoldable</code> instance: </p> <p> <pre>Prelude Data.Bifoldable Data.Semigroup RoseTree&gt; bifoldMap Sum (Sum . length) exampleTree Sum {getSum = 93641}</pre> </p> <p> Fortunately, we get the same result as above. </p> <p> Counting leaves, or measuring the depth of a tree, on the other hand, is impossible with the <code>Foldable</code> instance, but interestingly, it turns out that counting leaves is possible with the <code>Bifoldable</code> instance: </p> <p> <pre><span style="color:#2b91af;">countLeaves</span>&nbsp;::&nbsp;(<span style="color:blue;">Bifoldable</span>&nbsp;p,&nbsp;<span style="color:blue;">Num</span>&nbsp;n)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;p&nbsp;a&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;n countLeaves&nbsp;=&nbsp;getSum&nbsp;.&nbsp;bifoldMap&nbsp;(<span style="color:blue;">const</span>&nbsp;$&nbsp;Sum&nbsp;0)&nbsp;(<span style="color:blue;">const</span>&nbsp;$&nbsp;Sum&nbsp;1)</pre> </p> <p> This works well with the example tree: </p> <p> <pre>Prelude RoseTree&gt; countLeaves exampleTree 7</pre> </p> <p> Notice, however, that <code>countLeaves</code> works for any <code>Bifoldable</code> instance. Does that mean that you can 'count the leaves' of a tuple? Yes, it does: </p> <p> <pre>Prelude RoseTree&gt; countLeaves ("foo", "bar") 1 Prelude RoseTree&gt; countLeaves (1, 42) 1</pre> </p> <p> Or what about <code>EitherFix</code>: </p> <p> <pre>Prelude RoseTree Either&gt; countLeaves $ leftF "foo" 0 Prelude RoseTree Either&gt; countLeaves $ rightF "bar" 1</pre> </p> <p> Notice that 'counting the leaves' of tuples always returns <code>1</code>, while 'counting the leaves' of <code>Either</code> always returns <code>0</code> for <code>Left</code> values, and <code>1</code> for <code>Right</code> values. This is because <code>countLeaves</code> considers the left, or <em>first</em>, data type to represent internal nodes, and the right, or <em>second</em>, data type to indicate leaves. </p> <p> You can further follow that train of thought to realise that you can convert both tuples and <code>EitherFix</code> values to small rose trees: </p> <p> <pre><span style="color:#2b91af;">fromTuple</span>&nbsp;::&nbsp;(a,&nbsp;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;a&nbsp;b fromTuple&nbsp;(x,&nbsp;y)&nbsp;=&nbsp;roseNodeF&nbsp;x&nbsp;(consF&nbsp;(roseLeafF&nbsp;y)&nbsp;nilF) <span style="color:#2b91af;">fromEitherFix</span>&nbsp;::&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;a&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;a&nbsp;b fromEitherFix&nbsp;=&nbsp;eitherF&nbsp;(`roseNodeF`&nbsp;nilF)&nbsp;roseLeafF</pre> </p> <p> The <code>fromTuple</code> function creates a small rose tree with one internal node and one leaf. The label of the internal node is the first value of the tuple, and the label of the leaf is the second value. Here's an example: </p> <p> <pre>Prelude RoseTree&gt; fromTuple ("foo", 42) RoseTreeFix {unRoseTreeFix = Fix (NodeF "foo" (ListFix (Fix (ConsF (Fix (LeafF 42)) (Fix NilF)))))}</pre> </p> <p> The <code>fromEitherFix</code> function turns a <em>left</em> value into an internal node with no leaves, and a <em>right</em> value into a leaf. Here are some examples: </p> <p> <pre>Prelude RoseTree Either&gt; fromEitherFix $ leftF "foo" RoseTreeFix {unRoseTreeFix = Fix (NodeF "foo" (ListFix (Fix NilF)))} Prelude RoseTree Either&gt; fromEitherFix $ rightF 42 RoseTreeFix {unRoseTreeFix = Fix (LeafF 42)}</pre> </p> <p> While counting leaves can be implemented using <code>Bifoldable</code>, that's not the case for measuring the depths of trees (I think; leave a comment if you know of a way to do this with one of the instances shown here). You can, however, measure tree depth with the catamorphism: </p> <p> <pre><span style="color:#2b91af;">treeDepth</span>&nbsp;::&nbsp;<span style="color:blue;">RoseTreeFix</span>&nbsp;a&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Integer</span> treeDepth&nbsp;=&nbsp;roseTreeF&nbsp;(\_&nbsp;xs&nbsp;-&gt;&nbsp;1&nbsp;+&nbsp;<span style="color:blue;">maximum</span>&nbsp;xs)&nbsp;(<span style="color:blue;">const</span>&nbsp;0)</pre> </p> <p> The implementation is similar to the implementation for 'normal' trees. I've arbitrarily decided that leaves have a depth of zero, so the function that handles leaves always returns <code>0</code>. The function that handles internal nodes receives <code>xs</code> as a partially reduced list of depths below the node in question. Take the maximum of those and add <code>1</code>, since each internal node has a depth of one. </p> <p> <pre>Prelude RoseTree&gt; treeDepth exampleTree 3</pre> </p> <p> This, hopefully, illustrates that the catamorphism is more capable, and that the fold is just a (list-biased) specialisation. </p> <h3 id="4276c6f8fab248c0acc52a7f14462e41"> Summary <a href="#4276c6f8fab248c0acc52a7f14462e41" title="permalink">#</a> </h3> <p> The catamorphism for rose trees is a pair of functions. One function transforms internal nodes with their partially reduced branches, while the other function transforms leaves. </p> <p> For a realistic example of using a rose tree in a real program, see <a href="/2019/09/09/picture-archivist-in-haskell">Picture archivist in Haskell</a>. </p> <p> This article series has so far covered progressively more complex data structures. The first examples (<a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a> and <a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a>) were neither <a href="/2018/03/22/functors">functors</a>, <a href="/2018/10/01/applicative-functors">applicatives</a>, nor monads. All subsequent examples, on the other hand, are all of these, and more. The next example presents a functor that's neither applicative nor monad, yet still foldable. Obviously, what functionality it offers is still based on a catamorphism. </p> <p> <strong>Next:</strong> <a href="/2019/06/24/full-binary-tree-catamorphism">Full binary tree catamorphism</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f501a0d8e3ab4b27b7bb8d56e84fe05c"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#f501a0d8e3ab4b27b7bb8d56e84fe05c">#</a></div> <div class="comment-content"> <blockquote> Each node can have an arbitrary number of branches, including none. </blockquote> <p> Because of this sentence, in the picture of an example after the containing paragraph, I expected to see a(n) (internal) node with no branches. </p> <blockquote> <p> You can also measure the maximum depth of the tree: </p> <p> <pre>&gt; exampleTree.Cata((_,&nbsp;xs)&nbsp;=&gt;&nbsp;1&nbsp;+&nbsp;xs.Max(),&nbsp;_&nbsp;=&gt;&nbsp;0) 3</pre> </p> </blockquote> <p> <code>Max</code> will throw an exception when given an internal node with no children. What value do you want to return in that case? </p> </div> <div class="comment-date">2020-08-03 16:49 UTC</div> </div> <div class="comment" id="bddc949f2130444e89e3d8a73e57935d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bddc949f2130444e89e3d8a73e57935d">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. You're right that my implementation doesn't properly handle the empty edge case. That's also the case for Haskell's <code>maximum</code> function. I find it annoying that it's a partial function. </p> <p> One can handle that edge case by assigning an arbitrary depth to an empty node, just as is the case for leaf nodes. If leaf nodes get assigned a depth of <em>0</em>, wouldn't it be natural to also give empty internal nodes a depth of <em>0?</em> </p> </div> <div class="comment-date">2020-08-03 17:29 UTC</div> </div> <div class="comment" id="7e63b7c5baab45279298e811148a019a"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#7e63b7c5baab45279298e811148a019a">#</a></div> <div class="comment-content"> <p> Yes, very natural. In particular, such a definition would be consistent with the corresponding definition for <code>Tree&lt;&gt;</code>. More specifically, we want the behaviors to be the same when the two type parameters in <code>IRoseTree&lt;,&gt;</code> are the same (and the function passed in for <code>leaf</code> is the same as the one passed in for <code>node</code> after fixing the second argument to <code>Enumberable.Empty&lt;TResult>()&gt;</code>). </p> <p> I think the smallest change to get the depth to be <code>0</code> for an internal node with no children is to replace <code>Max</code> with a slight variant that returns <code>-1</code> when there are no children. I don't think this is readable though. It is quite the magic number. But it is just the codification of the thought process that lead to it. </p> <blockquote> <p> Each (internal) node can have an arbitrary number of branches, including none. </p> <p> ... </p> <p> ...an internal node represents another level of depth in a tree. </p> </blockquote> <p> It is because of such edge cases that Jeremy Gibbons in his PhD thesis <a href="https://www.cs.ox.ac.uk/files/3422/PRG94.pdf">Algebras for Tree Algorithms</a> says (on page 44) that the internal nodes of his rose tree must include at least one child. </p> <blockquote> Meertens allows his lists of children to be empty, so permitting parents with no children; to avoid this rather strange concept we use non-empty lists. </blockquote> <p> I think Jeremy has me convinced. One could say that this heterogenous rose tree was obtained from the homogeneous variant by adding a type for the leaves. The homogeneous variant denoted leaves via an empty list of children. It makes sense then to remove the empty list approach for making a leaf when adding the typed approach. So, I think the best fix then would be to modify your definition of <code>RoseNode&lt;,&gt;</code> in <a href="https://blog.ploeh.dk/2019/07/29/church-encoded-rose-tree/">your first rose tree article</a> to be the same as Jeremy's (by requiring that <code>IEnumerable&lt;&gt;</code> of children is non-empty). This change would also better match your example pictures of a rose tree, which do not include an internal node without children. </p> </div> <div class="comment-date">2020-08-03 18:37 UTC</div> </div> <div class="comment" id="1347af54693841828f6ae1e7742311dd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1347af54693841828f6ae1e7742311dd">#</a></div> <div class="comment-content"> <p> Yes, it'd definitely be an option to change the definition of an internal node to a <a href="/2017/12/11/semigroups-accumulate#68b942712506472ebe4c6933ca7dbd56">NonEmptyCollection</a>. </p> <p> My underlying motivation for defining the type like I've done in these articles, however, was to provide the underlying abstraction for a <a href="/2019/08/26/functional-file-system">functional file system</a>. In order to model a file system, empty nodes should be possible, because they correspond to empty directories. </p> </div> <div class="comment-date">2020-08-03 19:41 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Church-encoded rose tree https://blog.ploeh.dk/2019/07/29/church-encoded-rose-tree 2019-07-29T13:14:00+00:00 Mark Seemann <div id="post"> <p> <em>A rose tree is a tree with leaf nodes of one type, and internal nodes of another.</em> </p> <p> This article is part of <a href="/2018/05/22/church-encoding">a series of articles about Church encoding</a>. In the previous articles, you've seen <a href="/2018/06/04/church-encoded-maybe">how to implement a Maybe container</a>, and <a href="/2018/06/11/church-encoded-either">how to implement an Either container</a>. Through these examples, you've learned how to model <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a> without explicit language support. In this article, you'll see how to model a <a href="https://en.wikipedia.org/wiki/Rose_tree">rose tree</a>. </p> <p> A rose tree is a general-purpose data structure where each node in a tree has an associated value. Each node can have an arbitrary number of branches, including none. The distinguishing feature from a rose tree and just any <a href="https://en.wikipedia.org/wiki/Tree_(data_structure)">tree</a> is that internal nodes can hold values of a different type than leaf values. </p> <p> <img src="/content/binary/rose-tree-example.png" alt="A rose tree example diagram, with internal nodes containing integers, and leaves containing strings."> </p> <p> The diagram shows an example of a tree of internal integers and leaf strings. All internal nodes contain integer values, and all leaves contain strings. Each node can have an arbitrary number of branches. </p> <h3 id="5255946728c14810a5aaef3c1022d126"> Contract <a href="#5255946728c14810a5aaef3c1022d126" title="permalink">#</a> </h3> <p> In C#, you can represent the fundamental structure of a rose tree with a Church encoding, starting with an interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;node, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;leaf); }</pre> </p> <p> The structure of a rose tree includes two mutually exclusive cases: internal nodes and leaf nodes. Since there's two cases, the <code>Match</code> method takes two arguments, one for each case. </p> <p> The interface is generic, with two type arguments: <code>N</code> (for <em>Node</em>) and <code>L</code> (for <em>leaf</em>). Any consumer of an <code>IRoseTree&lt;N, L&gt;</code> object must supply two functions when calling the <code>Match</code> method: a function that turns a node into a <code>TResult</code> value, and a function that turns a leaf into a <code>TResult</code> value. </p> <p> Both cases must have a corresponding implementation. </p> <h3 id="89c4833c4e4d46cc8eef2d5eb546f61d"> Leaves <a href="#89c4833c4e4d46cc8eef2d5eb546f61d" title="permalink">#</a> </h3> <p> The <em>leaf</em> implementation is the simplest: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">L</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;RoseLeaf(<span style="color:#2b91af;">L</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;node, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;leaf) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;leaf(value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(value,&nbsp;other.value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;value.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>RoseLeaf</code> class is an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> over a value of the generic type <code>L</code>. As is always the case with Church encoding, it implements the <code>Match</code> method by unconditionally calling one of the arguments, in this case the <code>leaf</code> function, with its adapted <code>value</code>. </p> <p> While it doesn't have to do this, it also overrides <code>Equals</code> and <code>GetHashCode</code>. This is an immutable class, so it's a great candidate to be a <a href="https://martinfowler.com/bliki/ValueObject.html">Value Object</a>. Making it a Value Object makes it easier to compare expected and actual values in unit tests, among other benefits. </p> <h3 id="f211476563fe40379eac66ee887ed75b"> Nodes <a href="#f211476563fe40379eac66ee887ed75b" title="permalink">#</a> </h3> <p> The <em>node</em> implementation is slightly more complex: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">RoseNode</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">N</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&gt;&nbsp;branches; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;RoseNode(<span style="color:#2b91af;">N</span>&nbsp;value,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&gt;&nbsp;branches) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.branches&nbsp;=&nbsp;branches; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;node, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;leaf) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;node(value,&nbsp;branches); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">RoseNode</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(value,&nbsp;other.value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:#2b91af;">Enumerable</span>.SequenceEqual(branches,&nbsp;other.branches); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;value.GetHashCode()&nbsp;^&nbsp;branches.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> A node contains both a value (of the type <code>N</code>) and a collection of sub-trees, or <code>branches</code>. The class implements the <code>Match</code> method by unconditionally calling the <code>node</code> function argument with its constituent values. </p> <p> Again, it overrides <code>Equals</code> and <code>GetHashCode</code> for the same reasons as <code>RoseLeaf</code>. This isn't required to implement Church encoding, but makes comparison and unit testing easier. </p> <h3 id="a5c04c7e127349ed9b759e6361af5ab3"> Usage <a href="#a5c04c7e127349ed9b759e6361af5ab3" title="permalink">#</a> </h3> <p> You can use the <code>RoseLeaf</code> and <code>RoseNode</code> constructors to create new trees, but it sometimes helps to have a static helper method to create values. It turns out that there's little value in a helper method for leaves, but for nodes, it's marginally useful: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;Node&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;(<span style="color:#2b91af;">N</span>&nbsp;value,&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;[]&nbsp;branches) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseNode</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;(value,&nbsp;branches); }</pre> </p> <p> This enables you to create tree objects, like this: </p> <p> <pre><span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;tree&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1337));</pre> </p> <p> That's a single node with the label <code>"foo"</code> and two leaves with the values <code>42</code> and <code>1337</code>, respectively. You can create the tree shown in the above diagram like this: </p> <p> <pre><span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;exampleTree&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(42, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(1337, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(2112, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(90125, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;baz&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;qux&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;quux&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;quuz&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;corge&quot;</span>));</pre> </p> <p> You can add various extension methods to implement useful functionality. In later articles, you'll see some more compelling examples, so here, I'm only going to show a few basic examples. One of the simplest features you can add is a method that will tell you if an <code>IRoseTree&lt;N, L&gt;</code> object is a node or a leaf: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsLeaf&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IChurchBoolean</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node:&nbsp;(_,&nbsp;__)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf:&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>()); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsNode&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">N</span>,&nbsp;<span style="color:#2b91af;">L</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(source.IsLeaf()); }</pre> </p> <p> Since this article is part of the overall article series on Church encoding, and the purpose of that article series is also to show how basic language features can be created from Church encodings, these two methods return <a href="/2018/05/24/church-encoded-boolean-values">Church-encoded Boolean values</a> instead of the built-in <code>bool</code> type. I'm sure you can imagine how you could change the type to <code>bool</code> if you'd like. </p> <p> You can use these methods like this: </p> <p> <pre>&gt; <span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">double</span>&gt;&nbsp;tree&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">double</span>&gt;(-3.2); &gt; tree.IsLeaf() ChurchTrue { } &gt; tree.IsNode() ChurchNot(ChurchTrue) &gt; <span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:blue;">long</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;tree&nbsp;=&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node&lt;<span style="color:blue;">long</span>,&nbsp;<span style="color:blue;">string</span>&gt;(42); &gt; tree.IsLeaf() ChurchFalse { } &gt; tree.IsNode() ChurchNot(ChurchFalse)</pre> </p> <p> In a <a href="/2019/09/16/picture-archivist-in-f">future article, you'll see some more compelling examples</a>. </p> <h3 id="3be01779f059443799df57342e2510cb"> Terminology <a href="#3be01779f059443799df57342e2510cb" title="permalink">#</a> </h3> <p> It's not entirely clear what to call a tree like the one shown here. <a href="https://en.wikipedia.org/wiki/Rose_tree">The Wikipedia entry</a> doesn't state one way or the other whether internal node types ought to be distinguishable from leaf node types, but there are <a href="https://twitter.com/kbattocchi/status/1072538730911752192">indications that this could be the case</a>. At least, it seems that the <a href="https://mail.haskell.org/pipermail/haskell-cafe/2015-May/119633.html">term isn't well-defined</a>, so I took the liberty to retcon the name <em>rose tree</em> to the data structure shown here. </p> <p> In the paper that introduces the <em>rose tree</em> term, Meertens writes: <blockquote> <p> "We consider trees whose internal nodes may fork into an arbitrary (natural) number of sub-trees. (If such a node has zero descendants, we still consider it internal.) Each external node carries a data item. No further information is stored in the tree; in particular, internal nodes are unlabelled." </p> <footer><cite><em>First Steps towards the Theory of Rose Trees</em>, Lambert Meertens, 1988</cite></footer> </blockquote> While the concept is foreign in C#, you can trivially introduce a <a href="/2018/01/15/unit-isomorphisms">unit</a> data type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Unit</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Unit</span>&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Unit</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Unit()&nbsp;{&nbsp;} }</pre> </p> <p> This enables you to create a rose tree according to Meertens' definition: </p> <p> <pre><span style="color:#2b91af;">IRoseTree</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;meertensTree&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#2b91af;">Unit</span>.Instance, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#2b91af;">Unit</span>.Instance, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#2b91af;">Unit</span>.Instance, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:blue;">int</span>&gt;(2112)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1337), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:blue;">int</span>&gt;(90125)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RoseTree</span>.Node(<span style="color:#2b91af;">Unit</span>.Instance, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1984)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RoseLeaf</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:blue;">int</span>&gt;(666));</pre> </p> <p> Visually, you could draw it like this: </p> <p> <img src="/content/binary/meertens-tree-example.png" alt="A Meertens rose tree example diagram, with leaves containing integers."> </p> <p> Thus, the tree structure shown here seems to be a generalisation of Meertens' original definition. </p> <p> I'm not a mathematician, so I may have misunderstood some things. If you have a better name than <em>rose tree</em> for the data structure shown here, please leave a comment. </p> <h3 id="331fa8452cdd435c86ce87b5d39d51c5"> Yeats <a href="#331fa8452cdd435c86ce87b5d39d51c5" title="permalink">#</a> </h3> <p> Now that we're on the topic of <em>rose tree</em> as a term, you may, as a bonus, enjoy a similarly-titled poem: <blockquote> <h4>THE ROSE TREE</h4> <p> "O words are lightly spoken"<br> Said Pearse to Connolly,<br> "Maybe a breath of politic words<br> Has withered our Rose Tree;<br> Or maybe but a wind that blows<br> Across the bitter sea." </p> <p> "It needs to be but watered,"<br> James Connolly replied,<br> "To make the green come out again<br> And spread on every side,<br> And shake the blossom from the bud<br> To be the garden's pride."<br> </p> <p> "But where can we draw water"<br> Said Pearse to Connolly,<br> "When all the wells are parched away?<br> O plain as plain can be<br> There's nothing but our own red blood<br> Can make a right Rose Tree." </p> <footer><cite><a href="https://en.wikipedia.org/wiki/W._B._Yeats">W. B. Yeats</a></cite></footer> </blockquote> As far as I can tell, though, Yeats' metaphor is dissimilar to Meertens'. </p> <h3 id="9906b9a8856248f38b4f03e40252b761"> Summary <a href="#9906b9a8856248f38b4f03e40252b761" title="permalink">#</a> </h3> <p> You may occasionally find use for a tree that distinguishes between internal and leaf nodes. You can model such a tree with a Church encoding, as shown in this article. </p> <p> <strong>Next: </strong> <a href="/2019/04/29/catamorphisms">Catamorphisms</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="101cacfe58474a0cacbb00fa07df1461"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#101cacfe58474a0cacbb00fa07df1461">#</a></div> <div class="comment-content"> <blockquote> If you have a better name than <em>rose tree</em> for the data structure shown here, please leave a comment. </blockquote> <p> I would consider using <em>heterogeneous rose tree</em>. </p> <p> In your linked Twitter thread, <a href="https://twitter.com/kbattocchi/status/1072538730911752192">Keith Battocchi shared a link</a> to the <a href="https://www.cs.ox.ac.uk/files/3422/PRG94.pdf">thesis of Jeremy Gibbons</a> (which is titled <em>Algebras for Tree Algorithms</em>). In his thesis, he defines rose tree as you have and then derives from that (on page 45) the special cases that he calls unlabelled rose tree, leaf-labeled rose tree, branch-labeled rose tree, and homogeneous rose tree. </p> <p> The advantage of Jeremy's approach is that the name of each special case is formed from the named of the general case by adding an adjective. The disadvantage is the ambiguity that comes from being inconsistent with the definition of <em>rose tree</em> compared to both previous works and current usage (as shown by your numerous links). </p> <p> The <a href="https://tysonwilliams.coding.blog/2020-07-17_naming_optimization_problem">goal of naming is communication</a>, and the name <em>rose tree</em> risks miscommunication, which is worse than no communication at all. Miscommunication would result by (for example) calling this heterogeneous rose tree a <em>rose tree</em> and someone that knows the rose tree definition in Haskell skims over your definition thinking that they already know it. However, I think you did a good job with this article and made that unlikely. </p> <p> The advantage of <em>heterogeneous rose tree</em> is that the name is not overloaded and <em>heterogeneous</em> clearly indicates the intended variant. If a reader has heard of a <em>rose tree</em>, then they probably know there are several variants and can infer the correct one from this additional adjective. </p> <p> In the end though, I think using the name <em>rose tree</em> as you did was a good choice. Your have now written several articles involving rose trees and they all use the same variant. Since you always use the same variant, it would be a bit verbose to always include an additional adjective to specify the variant. </p> <p> The only thing I would have considered changing is the first mention of rose tree in this article. It is common in academic writing to start with the general definition and then give shorter alternatives for brevity. This is one way it could have been written in this article. </p> <blockquote> <p> <em>A heterogeneous rose tree is a tree with leaf nodes of one type, and internal nodes of another.</em> </p> <p> This article is part of <a href="/2018/05/22/church-encoding">a series of articles about Church encoding</a>. In the previous articles, you've seen <a href="/2018/06/04/church-encoded-maybe">how to implement a Maybe container</a>, and <a href="/2018/06/11/church-encoded-either">how to implement an Either container</a>. Through these examples, you've learned how to model <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a> without explicit language support. In this article, you'll see how to model a heterogeneous <a href="https://en.wikipedia.org/wiki/Rose_tree">rose tree</a>. </p> <p> A heterogeneous rose tree is a general-purpose data structure where each node in a tree has an associated value. Each node can have an arbitrary number of branches, including none. The distinguishing feature from a heterogeneous rose tree and just any <a href="https://en.wikipedia.org/wiki/Tree_(data_structure)">tree</a> is that internal nodes can hold values of a different type than leaf values. For brevity, we will omit <em>heterogeneous</em> and simply call this data structure a <em>rose tree</em>. </p> </blockquote> </div> <div class="comment-date">2020-08-03 02:02 UTC</div> </div> <div class="comment" id="2006ba182f934baf8fad56d9654c0d75"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2006ba182f934baf8fad56d9654c0d75">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I agree that specificity is desirable. I haven't read the Gibbons paper, so I'll have to reflect on your summary. If I understand you correctly, a type like <code>IRoseTree</code> shown in this article constitutes the general case. In Haskell, <a href="/2019/09/09/picture-archivist-in-haskell#770cf37f0e3c457782ea20b53257f2d1">I simply named it <code>Tree a b</code></a>, which is probably too general, but may help to illustrate the following point. </p> <p> As far as I remember, C# doesn't have type aliases, so Haskell makes the point more succinct. If I understand you correctly, then, you could define a <em>heterogeneous rose tree</em> as: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;HeterogeneousRoseTree&nbsp;=&nbsp;Tree</pre> </p> <p> Furthermore, I suppose that a <em>leaf-labeled rose tree</em> is this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;LeafLabeledRoseTree&nbsp;b&nbsp;=&nbsp;Tree&nbsp;<span style="color:blue;">()</span>&nbsp;b</pre> </p> <p> Would the following be a <em>branch-labeled rose tree?</em> </p> <p> <pre><span style="color:blue;">type</span>&nbsp;BranchLabeledRoseTree&nbsp;a&nbsp;=&nbsp;Tree&nbsp;a&nbsp;<span style="color:blue;">()</span></pre> </p> <p> And this is, I suppose, a <em>homogeneous rose tree:</em> </p> <p> <pre><span style="color:blue;">type</span>&nbsp;HomogeneousRoseTree&nbsp;a&nbsp;=&nbsp;Tree&nbsp;a&nbsp;a</pre> </p> <p> I can't imagine what an <em>unlabelled rose tree</em> is, unless it's this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;UnlabelledRoseTree&nbsp;=&nbsp;Tree&nbsp;<span style="color:blue;">()</span>&nbsp;<span style="color:blue;">()</span></pre> </p> <p> I don't see how that'd be of much use, but I suppose that's just my lack of imagination. </p> </div> <div class="comment-date">2020-08-09 15:23 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Chain of Responsibility as catamorphisms https://blog.ploeh.dk/2019/07/22/chain-of-responsibility-as-catamorphisms 2019-07-22T14:11:00+00:00 Mark Seemann <div id="post"> <p> <em>The Chain of Responsibility design pattern can be viewed as a list fold over the First monoid, followed by a Maybe fold.</em> </p> <p> This article is part of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">a series of articles about specific design patterns and their category theory counterparts</a>. In it, you'll see how the <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">Chain of Responsibility design pattern</a> is equivalent to a succession of <a href="/2019/04/29/catamorphisms">catamorphisms</a>. First, you apply the <a href="/2018/04/03/maybe-monoids">First Maybe monoid</a> over the <a href="/2019/05/27/list-catamorphism">list catamorphism</a>, and then you conclude the reduction with the <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>. </p> <h3 id="46a6c41949db446d9387c8befbf3fdb1"> Pattern <a href="#46a6c41949db446d9387c8befbf3fdb1" title="permalink">#</a> </h3> <p> The Chain of Responsibility design pattern gives you a way to model cascading conditionals with an object structure. It's a chain (or linked list) of objects that all implement the same interface (or base class). Each object (apart from the the last) has a reference to the next object in the list. </p> <p> <img src="/content/binary/chain-of-responsibility-diagram.png" alt="General diagram of the Chain of Responsibility design pattern."> </p> <p> A client (some other code) calls a method on the first object in the list. If that object can handle the request, it does so, and the interaction ends there. If the method returns a value, the object returns the value. </p> <p> If the first object determines that it can't handle the method call, it calls the next object in the chain. It only knows the next object as the interface, so the only way it can delegate the call is by calling the same method as the first one. In the above diagram, <em>Imp1</em> can't handle the method call, so it calls the same method on <em>Imp2</em>, which also can't handle the request and delegates responsibility to <em>Imp3</em>. In the diagram, <em>Imp3</em> can handle the method call, so it does so and returns a result that propagates back up the chain. In that particular example, <em>Imp4</em> never gets involved. </p> <p> You'll see an example below. </p> <p> One of the advantages of the pattern is that you can rearrange the chain to change its behaviour. You can even do this at run time, if you'd like, since all objects implement the same interface. </p> <h3 id="08a67dafd71f4bdd9a2e2577b0e43f9a"> User icon example <a href="#08a67dafd71f4bdd9a2e2577b0e43f9a" title="permalink">#</a> </h3> <p> Consider an online system that maintains user profiles for users. A user is modelled with the <code>User</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;User(<span style="color:blue;">int</span>&nbsp;id,&nbsp;<span style="color:blue;">string</span>&nbsp;name,&nbsp;<span style="color:blue;">string</span>&nbsp;email,&nbsp;<span style="color:blue;">bool</span>&nbsp;useGravatar,&nbsp;<span style="color:blue;">bool</span>&nbsp;useIdenticon)</pre> </p> <p> While I only show the signature of the class' constructor, it should be enough to give you an idea. If you need more details, the entire example code base is <a href="https://github.com/ploeh/UserProfile">available on GitHub</a>. </p> <p> Apart from an <code>id</code>, a <code>name</code> and <code>email</code> address, a user also has two flags. One flag tracks whether the user wishes to use his or her <a href="http://www.gravatar.com">Gravatar</a>, while another flag tracks if the user would like to use an <a href="https://en.wikipedia.org/wiki/Identicon">Identicon</a>. Obviously, both flags could be <code>true</code>, in which case the current business rule states that the Gravatar should take precedence. </p> <p> If none of the flags are set, users might still have a picture associated with their profile. This could be a picture that they've uploaded to the system, and is being tracked by a database. </p> <p> If no user icon can be found or generated, ultimately the system should use a fallback, default icon: </p> <p> <img src="/content/binary/default-user-icon.png" alt="Default user icon."> </p> <p> To summarise, the current rules are: <ol> <li>Use Gravatar if flag is set.</li> <li>Use Identicon if flag is set.</li> <li>Use uploaded picture if available.</li> <li>Use default icon.</li> </ol> The order of precedence could change in the future, new images sources could be added, or some of the present sources could be removed. Modelling this set of rules as a Chain of Responsibility makes it easy for you to reorder the rules, should you need to. </p> <p> To request an icon, a client can use the <code>IIconReader</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Icon</span>&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user); }</pre> </p> <p> The <code>Icon</code> class is just a <a href="https://martinfowler.com/bliki/ValueObject.html">Value Object</a> wrapper around a URL. The idea is that such a URL can be used in an <code>img</code> tag to show the icon. Again, the full source code is available on GitHub if you'd like to investigate the details. </p> <p> The various rules for icon retrieval can be implemented using this interface. </p> <h3 id="b2a4cbfb576949c392ea0e0b3d440175"> Gravatar reader <a href="#b2a4cbfb576949c392ea0e0b3d440175" title="permalink">#</a> </h3> <p> Although you don't have to implement the classes in the order in which you are going to compose them, it seems natural to do so, starting with the Gravatar implementation. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">GravatarReader</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IIconReader</span>&nbsp;next; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;GravatarReader(<span style="color:#2b91af;">IIconReader</span>&nbsp;next) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.next&nbsp;=&nbsp;next; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Icon</span>&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(user.UseGravatar) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Icon</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Gravatar</span>(user.Email).Url); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;next.ReadIcon(user); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>GravatarReader</code> class both implements the <code>IIconReader</code> interface, but also decorates another object of the same polymorphic type. If <code>user.UseGravatar</code> is <code>true</code>, it generates the appropriate Gravatar URL based on the user's <code>Email</code> address; otherwise, it delegates the work to the <code>next</code> object in the Chain of Responsibility. </p> <p> The <code>Gravatar</code> class contains the implementation details to generate the Gravatar <code>Url</code>. Again, please refer to the GitHub repository if you're interested in the details. </p> <h3 id="222ae025b264455695f1dbbd74cad17b"> Identicon reader <a href="#222ae025b264455695f1dbbd74cad17b" title="permalink">#</a> </h3> <p> When you compose the chain, according to the above business logic, the next type of icon you should attempt to generate is an Identicon. It's natural to implement the Identicon reader next, then: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IdenticonReader</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IIconReader</span>&nbsp;next; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IdenticonReader(<span style="color:#2b91af;">IIconReader</span>&nbsp;next) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.next&nbsp;=&nbsp;next; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Icon</span>&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(user.UseIdenticon) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Icon</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(baseUrl,&nbsp;HashUser(user))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;next.ReadIcon(user); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Implementation&nbsp;details&nbsp;go&nbsp;here...</span> }</pre> </p> <p> Again, I'm omitting implementation details in order to focus on the Chain of Responsibility design pattern. If <code>user.UseIdenticon</code> is <code>true</code>, the <code>IdenticonReader</code> generates the appropriate Identicon and returns the URL for it; otherwise, it delegates the work to the <code>next</code> object in the chain. </p> <h3 id="e9f2904333b940c1a9a90522d19a41f3"> Database icon reader <a href="#e9f2904333b940c1a9a90522d19a41f3" title="permalink">#</a> </h3> <p> The <code>DBIconReader</code> class attempts to find an icon ID in a database. If it succeeds, it creates a URL corresponding to that ID. The assumption is that that resource exists; either it's a file on disk, or it's an image resource generated on the spot based on binary data stored in the database. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DBIconReader</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IIconReader</span>&nbsp;next; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;DBIconReader(<span style="color:#2b91af;">IUserRepository</span>&nbsp;repository,&nbsp;<span style="color:#2b91af;">IIconReader</span>&nbsp;next) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.next&nbsp;=&nbsp;next; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Icon</span>&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!repository.TryReadIconId(user.Id,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">string</span>&nbsp;iconId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;next.ReadIcon(user); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;parameters&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#a31515;">&quot;iconId&quot;</span>,&nbsp;iconId&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Icon</span>(urlTemplate.BindByName(baseUrl,&nbsp;parameters)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Uri</span>&nbsp;baseUrl&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Uri</span>(<span style="color:#a31515;">&quot;https://example.com&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">UriTemplate</span>&nbsp;urlTemplate&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UriTemplate</span>(<span style="color:#a31515;">&quot;users/{iconId}/icon&quot;</span>); }</pre> </p> <p> This class demonstrates some variations in the way you can implement the Chain of Responsibility design pattern. The above <code>GravatarReader</code> and <code>IdenticonReader</code> classes both follow the same implementation pattern of checking a condition, and then performing work if the condition is <code>true</code>. The delegation to the next object in the chain happens, in those two classes, outside of the <code>if</code> statement. </p> <p> The <code>DBIconReader</code> class, on the other hand, reverses the structure of the code. It uses a <a href="https://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html">Guard Clause</a> to detect whether to exit early, which is done by delegating work to the <code>next</code> object in the chain. </p> <p> If <code>TryReadIconId</code> returns <code>true</code>, however, the <code>ReadIcon</code> method proceeds to create the appropriate icon URL. </p> <p> Another variation on the Chain of Responsibility design pattern demonstrated by the <code>DBIconReader</code> class is that it takes a second dependency, apart from <code>next</code>. The <code>repository</code> is the usual misapplication of the Repository design pattern that everyone think they use correctly. Here, it's used in the common sense to provide access to a database. The main point, though, is that you can add as many other dependencies to a link in the chain as you'd like. All links, apart from the last, however, must have a reference to the <code>next</code> link in the chain. </p> <h3 id="cee40120578b4732892e6fd72329d5de"> Default icon reader <a href="#cee40120578b4732892e6fd72329d5de" title="permalink">#</a> </h3> <p> Like linked lists, a Chain of Responsibility has to ultimately terminate. You can use the following <code>DefaultIconReader</code> for that. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DefaultIconReader</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Icon</span>&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Icon</span>.Default; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class unconditionally returns the <code>Default</code> icon. Notice that it doesn't have any <code>next</code> object it delegates to. This terminates the chain. If no previous implementation of the <code>IIconReader</code> has returned an <code>Icon</code> for the <code>user</code>, this one does. </p> <h3 id="8eb05bed2d98488a91c09bab52b00a53"> Chain composition <a href="#8eb05bed2d98488a91c09bab52b00a53" title="permalink">#</a> </h3> <p> With four implementations of <code>IIconReader</code>, you can now compose the Chain of Responsibility: </p> <p> <pre><span style="color:#2b91af;">IIconReader</span>&nbsp;reader&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GravatarReader</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdenticonReader</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DBIconReader</span>(repo, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultIconReader</span>())));</pre> </p> <p> The first link in the chain is a <code>GravatarReader</code> object that contains an <code>IdenticonReader</code> object as its <code>next</code> link, and so on. Referring back to the source code of <code>GravatarReader</code>, notice that its <code>next</code> dependency is declared as an <code>IIconReader</code>. Since the <code>IdenticonReader</code> class implements that interface, you can compose the chain like this, but if you later decide to change the order of the objects, you can do so simply by changing the composition. You could remove objects altogether, or add new classes, and you could even do this at run time, if required. </p> <p> The <code>DBIconReader</code> class requires an extra <code>IUserRepository</code> dependency, here simply an existing object called <code>repo</code>. </p> <p> The <code>DefaultIconReader</code> takes no other dependencies, so this effectively terminates the chain. If you try to pass another <code>IIconReader</code> to its constructor, the code doesn't compile. </p> <h3 id="fc1551665bb940b8ba5e75be81c0629a"> Haskell proof of concept <a href="#fc1551665bb940b8ba5e75be81c0629a" title="permalink">#</a> </h3> <p> When evaluating whether a design is <a href="/2018/11/19/functional-architecture-a-definition">a functional architecture</a>, I often port the relevant parts to <a href="https://www.haskell.org">Haskell</a>. You can do the same with the above example, and put it in a form where it's clearer that the Chain of Responsibility pattern is equivalent to two well-known catamorphisms. </p> <p> Readers not comfortable with Haskell can skip the next few sections. The object-oriented example continues below. </p> <p> <code>User</code> and <code>Icon</code> types are defined by types equivalent to above. There's no explicit interface, however. Creation of Gravatars and Identicons are both pure functions with the type <code>User -&gt; Maybe Icon</code>. Here's the Gravatar function, but the Identicon function looks similar: </p> <p> <pre><span style="color:#2b91af;">gravatarUrl</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> gravatarUrl&nbsp;email&nbsp;= &nbsp;&nbsp;<span style="color:#a31515;">&quot;https://www.gravatar.com/avatar/&quot;</span>&nbsp;++&nbsp;<span style="color:blue;">show</span>&nbsp;(hashString&nbsp;email&nbsp;::&nbsp;MD5Digest) <span style="color:#2b91af;">getGravatar</span>&nbsp;::&nbsp;<span style="color:blue;">User</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;<span style="color:blue;">Icon</span> getGravatar&nbsp;u&nbsp;= &nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;useGravatar&nbsp;u &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;Just&nbsp;$&nbsp;Icon&nbsp;$&nbsp;gravatarUrl&nbsp;$&nbsp;userEmail&nbsp;u &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;Nothing</pre> </p> <p> Reading an icon ID from a database, however, is an impure operation, so the function to do this has the type <code>User -&gt; IO (Maybe Icon)</code>. </p> <h3 id="11adf8bd104d41fab9e6bcaef249210c"> Lazy I/O in Haskell <a href="#11adf8bd104d41fab9e6bcaef249210c" title="permalink">#</a> </h3> <p> Notice that the database icon-querying function has the return type <code>IO (Maybe Icon)</code>. In the introduction you read that the Chain of Responsibility design pattern is a sequence of catamorphisms - the first one over a list of <code>First</code> values. While <code>First</code> is, in itself, a <code>Semigroup</code> instance, it gives rise to a <code>Monoid</code> instance when combined with <code>Maybe</code>. Thus, to showcase the abstractions being used, you could create a list of <code>Maybe (First Icon)</code> values. This forms a <code>Monoid</code>, so is easy to fold. </p> <p> The problem with that, however, is that <code>IO</code> is strict under evaluation, so while it works, <a href="https://stackoverflow.com/q/47120384/126014">it's no longer lazy</a>. You can combine <code>IO (Maybe (First Icon))</code> values, but it leads to too much I/O activity. </p> <p> You can <a href="https://stackoverflow.com/q/47120384/126014">solve this problem with a newtype wrapper</a>: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;FirstIO&nbsp;a&nbsp;=&nbsp;FirstIO&nbsp;(MaybeT&nbsp;IO&nbsp;a)&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Functor</span>,&nbsp;<span style="color:#2b91af;">Applicative</span>,&nbsp;<span style="color:#2b91af;">Monad</span>,&nbsp;<span style="color:#2b91af;">Alternative</span>) <span style="color:#2b91af;">firstIO</span>&nbsp;::&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;(<span style="color:#2b91af;">Maybe</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FirstIO</span>&nbsp;a firstIO&nbsp;=&nbsp;FirstIO&nbsp;.&nbsp;MaybeT <span style="color:#2b91af;">getFirstIO</span>&nbsp;::&nbsp;<span style="color:blue;">FirstIO</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;(<span style="color:#2b91af;">Maybe</span>&nbsp;a) getFirstIO&nbsp;(FirstIO&nbsp;(MaybeT&nbsp;x))&nbsp;=&nbsp;x <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;(<span style="color:blue;">FirstIO</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:#2b91af;">(&lt;&gt;)</span>&nbsp;=&nbsp;<span style="color:#2b91af;">(&lt;|&gt;)</span> <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;(<span style="color:blue;">FirstIO</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;mempty&nbsp;=&nbsp;empty</pre> </p> <p> This uses the <code>GeneralizedNewtypeDeriving</code> GHC extension to automatically make <code>FirstIO</code> <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, and <code>Alternative</code>. It also uses the <code>Alternative</code> instance to implement <code>Semigroup</code> and <code>Monoid</code>. You may recall from <a href="http://hackage.haskell.org/package/base/docs/Control-Applicative.html">the documentation</a> that <code>Alternative</code> is already a "monoid on applicative functors." </p> <h3 id="995f9ea8f8344aea93b2ffd0b3aad71f"> Alignment <a href="#995f9ea8f8344aea93b2ffd0b3aad71f" title="permalink">#</a> </h3> <p> You now have three functions with different types: two pure functions with the type <code>User -&gt; Maybe Icon</code> and one impure database-bound function with the type <code>User -&gt; IO (Maybe Icon)</code>. In order to have a common abstraction, you should align them so that all types match. At first glance, <code>User -&gt; IO (Maybe (First Icon))</code> seems like a type that fits all implementations, but that causes too much I/O to take place, so instead, use <code>User -&gt; FirstIO Icon</code>. Here's how to lift the pure <code>getGravatar</code> function: </p> <p> <pre><span style="color:#2b91af;">getGravatarIO</span>&nbsp;::&nbsp;<span style="color:blue;">User</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FirstIO</span>&nbsp;<span style="color:blue;">Icon</span> getGravatarIO&nbsp;=&nbsp;firstIO&nbsp;.&nbsp;<span style="color:blue;">return</span>&nbsp;.&nbsp;getGravatar</pre> </p> <p> You can lift the other functions in similar fashion, to produce <code>getGravatarIO</code>, <code>getIdenticonIO</code>, and <code>getDBIconIO</code>, all with the mutual type <code>User -&gt; FirstIO Icon</code>. </p> <h3 id="f601a51f3006430398232e05b6595da0"> Haskell composition <a href="#f601a51f3006430398232e05b6595da0" title="permalink">#</a> </h3> <p> The goal of the Haskell proof of concept is to compose a function that can provide an <code>Icon</code> for any <code>User</code> - just like the above C# composition that uses Chain of Responsibility. There's, however, no way around impurity, because one of the steps involve a database, so the aim is a composition with the type <code>User -&gt; IO Icon</code>. </p> <p> While a more compact composition is possible, I'll show it in a way that makes the catamorphisms explicit: </p> <p> <pre><span style="color:#2b91af;">getIcon</span>&nbsp;::&nbsp;<span style="color:blue;">User</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;<span style="color:blue;">Icon</span> getIcon&nbsp;u&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;lazyIcons&nbsp;=&nbsp;<span style="color:blue;">fmap</span>&nbsp;(\f&nbsp;-&gt;&nbsp;f&nbsp;u)&nbsp;[getGravatarIO,&nbsp;getIdenticonIO,&nbsp;getDBIconIO] &nbsp;&nbsp;m&nbsp;&lt;-&nbsp;getFirstIO&nbsp;$&nbsp;fold&nbsp;lazyIcons &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;fromMaybe&nbsp;defaultIcon&nbsp;m</pre> </p> <p> The <code>getIcon</code> function starts with a list of all three functions. For each of them, it calls the function with the <code>User</code> value <code>u</code>. This may seem inefficient and redundant, because all three function calls may not be required, but since the return values are <code>FirstIO</code> values, all three function calls are lazily evaluated - even under <code>IO</code>. The result, <code>lazyIcons</code>, is a <code>[FirstIO Icon]</code> value; i.e. a lazily evaluated list of lazily evaluated values. </p> <p> This first step is just to put the potential values in a form that's recognisable. You can now <code>fold</code> the <code>lazyIcons</code> to a single <code>FirstIO Icon</code> value, and then use <code>getFirstIO</code> to unwrap it. Due to <code>do</code> notation, <code>m</code> is a <code>Maybe Icon</code> value. </p> <p> This is the first catamorphism. Granted, the generalisation that <code>fold</code> offers is not really required, since <code>lazyIcons</code> is a list; <code>mconcat</code> would have worked just as well. I did, however, choose to use <code>fold</code> (from <code>Data.Foldable</code>) to emphasise the point. While the <code>fold</code> function itself isn't the catamorphism for lists, we know that <a href="/2019/05/27/list-catamorphism">it's derived from the list catamorphism</a>. </p> <p> The final step is to utilise the Maybe catamorphism to reduce the <code>Maybe Icon</code> value to an <code>Icon</code> value. Again, the <code>getIcon</code> function doesn't use the Maybe catamorphism directly, but rather the derived <code>fromMaybe</code> function. The <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a> is the <code>maybe</code> function, but you can trivially implement <code>fromMaybe</code> with <code>maybe</code>. </p> <p> For <a href="https://en.wikipedia.org/wiki/Code_golf">golfers</a>, it's certainly possible to write this function in a more compact manner. Here's a <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free</a> version: </p> <p> <pre><span style="color:#2b91af;">getIcon</span>&nbsp;::&nbsp;<span style="color:blue;">User</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;<span style="color:blue;">Icon</span> getIcon&nbsp;= &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;(fromMaybe&nbsp;defaultIcon)&nbsp;.&nbsp;getFirstIO&nbsp;.&nbsp;fold&nbsp;[getGravatarIO,&nbsp;getIdenticonIO,&nbsp;getDBIconIO]</pre> </p> <p> This alternative version utilises that <code>a -&gt; m</code> is a <code>Monoid</code> instance when <code>m</code> is a <code>Monoid</code> instance. That's the reason that you can <code>fold</code> a list of functions. The more explicit version above doesn't do that, but the behaviour is the same in both cases. </p> <p> That's all the Haskell code we need to discern the universal abstractions involved in the Chain of Responsibility design pattern. We can now return to the C# code example. </p> <h3 id="492ff50788784d7dbf6560ed08ed6bf7"> Chains as lists <a href="#492ff50788784d7dbf6560ed08ed6bf7" title="permalink">#</a> </h3> <p> The Chain of Responsibility design pattern is often illustrated like above, in a staircase-like diagram. There's, however, no inherent requirement to do so. You could also flatten the diagram: </p> <p> <img src="/content/binary/chain-of-responsibility-as-a-linked-list.png" alt="Chain of Responsibility illustrated as a linked list."> </p> <p> This looks a lot like a linked list. </p> <p> The difference is, however, that the terminator of a linked list is usually empty. Here, however, you have two types of objects. All objects apart from the rightmost object represent a <em>potential</em>. Each object may, or may not, handle the method call and produce an outcome; if an object can't handle the method call, it'll delegate to the next object in the chain. </p> <p> The rightmost object, however, is different. This object can't delegate any further, but <em>must</em> handle the method call. In the icon reader example, this is the <code>DefaultIconReader</code> class. </p> <p> Once you start to see most of the list as a list of potential values, you may realise that you'll be able to collapse into it a single potential value. This is possible because <a href="/2018/04/03/maybe-monoids">a list of values where you pick the first non-empty value forms a monoid</a>. This is sometimes called the <em>First</em> <a href="/2017/10/06/monoids">monoid</a>. </p> <p> In other words, you can reduce, or fold, all of the list, except the rightmost value, to a single potential value: </p> <p> <img src="/content/binary/chain-of-responsibility-as-a-linked-list-single-fold.png" alt="Chain of Responsibility illustrated as a linked list, with all but the rightmost objects folded to one."> </p> <p> When you do that, however, you're left with a single potential value. The result of folding most of the list is that you get the leftmost non-empty value in the list. There's no guarantee, however, that that value is non-empty. If all the values in the list are empty, the result is also empty. This means that you somehow need to combine a potential value with a value that's guaranteed to be present: the terminator. </p> <p> You can do that wither another fold: </p> <p> <img src="/content/binary/chain-of-responsibility-as-a-linked-list-double-fold.png" alt="Chain of Responsibility illustrated as a linked list, with two consecutive folds."> </p> <p> This second fold isn't a list fold, but rather a Maybe fold. </p> <h3 id="7632b9ff458d417fa49b1c65f7b198ed"> Maybe <a href="#7632b9ff458d417fa49b1c65f7b198ed" title="permalink">#</a> </h3> <p> The <em>First</em> monoid is a monoid over <a href="/2018/03/26/the-maybe-functor">Maybe</a>, so add a <code>Maybe</code> class to the code base. In Haskell, the catamorphism for Maybe is called <code>maybe</code>, but that's not a good method name in object-oriented design. Another option is some variation of <em>fold</em>, but in C#, this functionality tends to be called <code>Aggregate</code>, at least for <code>IEnumerable&lt;T&gt;</code>, so I'll reuse that terminology: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Aggregate&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">TResult</span>&nbsp;@default,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;func) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(func&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(func)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;hasItem&nbsp;?&nbsp;func(item)&nbsp;:&nbsp;@default; }</pre> </p> <p> You can implement another, more list-like <code>Aggregate</code> overload from this one, but for this article, you don't need it. </p> <h3 id="8b60d0c605d14cffbfa5e237cf26b7b2"> From TryReadIconId to Maybe <a href="#8b60d0c605d14cffbfa5e237cf26b7b2" title="permalink">#</a> </h3> <p> In the above code examples, <code>DBIconReader</code> depends on <code>IUserRepository</code>, which defined this method: </p> <p> <pre><span style="color:blue;">bool</span>&nbsp;TryReadIconId(<span style="color:blue;">int</span>&nbsp;userId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">string</span>&nbsp;iconId);</pre> </p> <p> From <a href="/2019/07/15/tester-doer-isomorphisms">Tester-Doer isomorphisms</a> we know, however, that such a design is isomorphic to returning a Maybe value, and since that's more composable, do that: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;ReadIconId(<span style="color:blue;">int</span>&nbsp;userId);</pre> </p> <p> This requires you to refactor the <code>DBIconReader</code> implementation of the <code>ReadIcon</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Icon</span>&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;mid&nbsp;=&nbsp;repository.ReadIconId(user.Id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;&nbsp;lazyResult&nbsp;=&nbsp;mid.Aggregate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@default:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;(()&nbsp;=&gt;&nbsp;next.ReadIcon(user)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func:&nbsp;id&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;(()&nbsp;=&gt;&nbsp;CreateIcon(id))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;lazyResult.Value; }</pre> </p> <p> A few things are worth a mention. Notice that the above <code>Aggregate</code> method (the Maybe catamorphism) requires you to supply a <code>@default</code> value (to be used if the Maybe object is empty). In the Chain of Responsibility design pattern, however, the fallback value is produced by calling the <code>next</code> object in the chain. If you do this unconditionally, however, you perform too much work. You're only supposed to call <code>next</code> if the current object can't handle the method call. </p> <p> The solution is to aggregate the <code>mid</code> object to a <code>Lazy&lt;Icon&gt;</code> and then return its <code>Value</code>. The <code>@default</code> value is now a lazy computation that calls <code>next</code> only if its <code>Value</code> is read. When <code>mid</code> is populated, on the other hand, the lazy computation calls the private <code>CreateIcon</code> method when <code>Value</code> is accessed. The private <code>CreateIcon</code> method contains the same logic as before the refactoring. </p> <p> This change of <code>DBIconReader</code> isn't strictly necessary in order to change the overall Chain of Responsibility to a pair of catamorphisms, but serves, I think, as a nice introduction to the use of the Maybe catamorphism. </p> <h3 id="ec329c8a0b70432d81d6f69e7084c13f"> Optional icon readers <a href="#ec329c8a0b70432d81d6f69e7084c13f" title="permalink">#</a> </h3> <p> Previously, the <code>IIconReader</code> interface <em>required</em> each implementation to return an <code>Icon</code> object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Icon</span>&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user); }</pre> </p> <p> When you have an object like <code>GravatarReader</code> that may or may not return an <code>Icon</code>, this requirement leads toward the Chain of Responsibility design pattern. You can, however, shift the responsibility of what to do next by changing the interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user); }</pre> </p> <p> An implementation like <code>GravatarReader</code> becomes simpler: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">GravatarReader</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(user.UseGravatar) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Icon</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Gravatar</span>(user.Email).Url)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> No longer do you have to pass in a <code>next</code> dependency. Instead, you just return an empty <code>Maybe&lt;Icon&gt;</code> if you can't handle the method call. The same change applies to the <code>IdenticonReader</code> class. </p> <p> Since <a href="/2018/03/26/the-maybe-functor">Maybe is a functor</a>, and the <code>DBIconReader</code> already works on a <code>Maybe&lt;string&gt;</code> value, its implementation is greatly simplified: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;repository.ReadIconId(user.Id).Select(CreateIcon); }</pre> </p> <p> Since <code>ReadIconId</code> returns a <code>Maybe&lt;string&gt;</code>, you can simply use <code>Select</code> to transform the icon ID to an <code>Icon</code> object if the Maybe is populated. </p> <h3 id="94cac3b9e52e48c2a1768fd24c72e4bd"> Coalescing Composite <a href="#94cac3b9e52e48c2a1768fd24c72e4bd" title="permalink">#</a> </h3> <p> As an intermediate step, you can compose the various readers using a <a href="/2018/04/09/coalescing-composite-as-a-monoid">Coalescing Composite</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CompositeIconReader</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IIconReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IIconReader</span>[]&nbsp;iconReaders; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;CompositeIconReader(<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">IIconReader</span>[]&nbsp;iconReaders) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.iconReaders&nbsp;=&nbsp;iconReaders; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;iconReader&nbsp;<span style="color:blue;">in</span>&nbsp;iconReaders) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;mIcon&nbsp;=&nbsp;iconReader.ReadIcon(user); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(IsPopulated(mIcon)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;mIcon; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsPopulated&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;m) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;m.Aggregate(<span style="color:blue;">false</span>,&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I prefer a more explicit design over this one, so this is just an intermediate step. This <code>IIconReader</code> implementation composes an array of other <code>IIconReader</code> objects and queries each in order to return the first populated Maybe value it finds. If it doesn't find any populated value, it returns an empty Maybe object. </p> <p> You can now compose your <code>IIconReader</code> objects into a <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a>: </p> <p> <pre><span style="color:#2b91af;">IIconReader</span>&nbsp;reader&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CompositeIconReader</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GravatarReader</span>(), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdenticonReader</span>(), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DBIconReader</span>(repo));</pre> </p> <p> While this gives you a single object on which you can call <code>ReadIcon</code>, the return value of that method is still a <code>Maybe&lt;Icon&gt;</code> object. You still need to reduce the <code>Maybe&lt;Icon&gt;</code> object to an <code>Icon</code> object. You can do this with a Maybe helper method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;GetValueOrDefault(<span style="color:#2b91af;">T</span>&nbsp;@default) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Aggregate(@default,&nbsp;x&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> Given a <code>User</code> object named <code>user</code>, you can now use the composition and the <code>GetValueOrDefault</code> method to get an <code>Icon</code> object: </p> <p> <pre><span style="color:#2b91af;">Icon</span>&nbsp;icon&nbsp;=&nbsp;reader.ReadIcon(user).GetValueOrDefault(<span style="color:#2b91af;">Icon</span>.Default);</pre> </p> <p> First you use the composed <code>reader</code> to produce a <code>Maybe&lt;Icon&gt;</code> object, and then you use the <code>GetValueOrDefault</code> method to reduce the <code>Maybe&lt;Icon&gt;</code> object to an <code>Icon</code> object. </p> <p> The latter of these two steps, <code>GetValueOrDefault</code>, is already based on the Maybe catamorphism, but the first step is still too implicit to clearly show the nature of what's actually going on. The next step is to refactor the Coalescing Composite to a list of monoidal values. </p> <h3 id="c75ce57c2b4f4315a93eaa91b653a370"> First <a href="#c75ce57c2b4f4315a93eaa91b653a370" title="permalink">#</a> </h3> <p> While not strictly necessary, you can introduce a <code>First&lt;T&gt;</code> wrapper: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;First(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(Item,&nbsp;other.Item); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Item.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In this particular example, the <code>First&lt;T&gt;</code> class adds no new capabilities, so it's technically redundant. You could add to it methods to combine two <code>First&lt;T&gt;</code> objects into one (since <em>First</em> forms a <a href="/2017/11/27/semigroups">semigroup</a>), and perhaps a method or two to <a href="/2017/12/11/semigroups-accumulate">accumulate multiple values</a>, but in this article, none of those are required. </p> <p> While the class as shown above doesn't add any behaviour, I like that it signals intent, so I'll use it in that role. </p> <h3 id="c3feb40d90fc4d389fa0b3812abaa62c"> Lazy I/O in C# <a href="#c3feb40d90fc4d389fa0b3812abaa62c" title="permalink">#</a> </h3> <p> Like in the above Haskell code, you'll need to be able to combine two <code>First&lt;T&gt;</code> objects in a lazy fashion, in such a way that if the first object is populated, the I/O associated with producing the second value never happens. In Haskell I addressed that concern with a <code>newtype</code> that, among other abstractions, is a monoid. You can do the same in C# with an extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&gt;&nbsp;FindFirst&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&gt;&nbsp;m, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&gt;&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(m.Value.IsPopulated()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;m; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;other; } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsPopulated&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;m) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;m.Aggregate(<span style="color:blue;">false</span>,&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">true</span>); }</pre> </p> <p> The <code>FindFirst</code> method returns the first (leftmost) non-empty object of two options. It's a lazy version of the <em>First</em> monoid, and <a href="/2019/04/15/lazy-monoids">that's still a monoid</a>. It's truly lazy because it never accesses the <code>Value</code> property on <code>other</code>. While it has to force evaluation of the first lazy computation, <code>m</code>, it doesn't have to evaluate <code>other</code>. Thus, whenever <code>m</code> is populated, <code>other</code> can remain non-evaluated. </p> <p> Since <a href="/2017/11/20/monoids-accumulate">monoids accumulate</a>, you can also write an extension method to implement that functionality: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&gt;&nbsp;FindFirst&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&gt;&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;identity&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&gt;(()&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Aggregate(identity,&nbsp;(acc,&nbsp;x)&nbsp;=&gt;&nbsp;acc.FindFirst(x)); }</pre> </p> <p> This overload just uses the earlier <code>FindFirst</code> extension method to fold an arbitrary number of lazy <code>First&lt;T&gt;</code> objects into one. Notice that <code>Aggregate</code> is the C# name for the list catamorphisms. </p> <p> You can now compose the desired functionality using the basic building blocks of monoids, <a href="/2018/03/22/functors">functors</a>, and catamorphisms. </p> <h3 id="0fe80a69c74c463dacb8af0f86898518"> Composition from universal abstractions <a href="#0fe80a69c74c463dacb8af0f86898518" title="permalink">#</a> </h3> <p> The goal is still a function that takes a <code>User</code> object as input and produces an <code>Icon</code> object as output. While you could compose that functionality directly in-line where you need it, I think it may be helpful to package the composition in a <a href="https://en.wikipedia.org/wiki/Facade_pattern">Facade</a> object. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IconReaderFacade</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">IIconReader</span>&gt;&nbsp;readers; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IconReaderFacade(<span style="color:#2b91af;">IUserRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readers&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IIconReader</span>[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GravatarReader</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdenticonReader</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DBIconReader</span>(repository) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Icon</span>&nbsp;ReadIcon(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;&gt;&gt;&gt;&nbsp;lazyIcons&nbsp;=&nbsp;readers &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;&gt;&gt;(()&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.ReadIcon(user).Select(i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;(i)))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">Icon</span>&gt;&gt;&gt;&nbsp;m&nbsp;=&nbsp;lazyIcons.FindFirst(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;m.Value.Aggregate(<span style="color:#2b91af;">Icon</span>.Default,&nbsp;fi&nbsp;=&gt;&nbsp;fi.Item); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> When you initialise an <code>IconReaderFacade</code> object, it creates an array of the desired <code>readers</code>. Whenever <code>ReadIcon</code> is invoked, it first transforms all those readers to a sequence of potential icons. All the values in the sequence are lazily evaluated, so in this step, nothing actually happens, even though it looks as though all readers' <code>ReadIcon</code> method gets called. The <code>Select</code> method is a structure-preserving map, so all readers are still potential producers of <code>Icon</code> objects. </p> <p> You now have an <code>IEnumerable&lt;Lazy&lt;Maybe&lt;First&lt;Icon&gt;&gt;&gt;&gt;</code>, which must be a good candidate for the prize for the <em>most nested generic .NET type of 2019</em>. It fits, though, the input type for the above <code>FindFirst</code> overload, so you can call that. The result is a single potential value <code>m</code>. That's the list catamorphism applied. </p> <p> Finally, you force evaluation of the lazy computation and apply the Maybe catamorphism (<code>Aggregate</code>). The <code>@default</code> value is <code>Icon.Default</code>, which gets returned if <code>m</code> turns out to be empty. When <code>m</code> is populated, you pull the <code>Item</code> out of the <code>First</code> object. In either case, you now have an <code>Icon</code> object to return. </p> <p> This composition has exactly the same behaviour as the initial Chain of Responsibility implementation, but is now composed from universal abstractions. </p> <h3 id="23819ca370344b94875ddbf5bde5aef3"> Summary <a href="#23819ca370344b94875ddbf5bde5aef3" title="permalink">#</a> </h3> <p> The Chain of Responsibility design pattern describes a flexible way to implement conditional logic. Instead of relying on keywords like <code>if</code> or <code>switch</code>, you can compose the conditional logic from polymorphic objects. This gives you several advantages. One is that you get better separations of concerns, which will tend to make it easier to refactor the code. Another is that it's possible to change the behaviour at run time, by moving the objects around. </p> <p> You can achieve a similar design, with equivalent advantages, by composing polymorphically similar functions in a list, map the functions to a list of potential values, and then use the list catamorphism to reduce many potential values to one. Finally, you apply the Maybe catamorphism to produce a value, even if the potential value is empty. </p> <p> <strong>Next:</strong> <a href="/2022/09/05/the-state-pattern-and-the-state-monad">The State pattern and the State monad</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tester-Doer isomorphisms https://blog.ploeh.dk/2019/07/15/tester-doer-isomorphisms 2019-07-15T07:35:00+00:00 Mark Seemann <div id="post"> <p> <em>The Tester-Doer pattern is equivalent to the Try-Parse idiom; both are equivalent to Maybe.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. An isomorphism is when a bi-directional lossless translation exists between two representations. Such translations exist between the <em>Tester-Doer</em> pattern and the <em>Try-Parse</em> idiom. Both can also be translated into operations that return <a href="/2018/03/26/the-maybe-functor">Maybe</a>. </p> <p> <img src="/content/binary/tester-doer-try-parse-maybe-isomorphism.png" alt="Isomorphisms between Tester-Doer, Try-Parse, and Maybe."> </p> <p> Given an implementation that uses one of those three idioms or abstractions, you can translate your design into one of the other options. This doesn't imply that each is of equal value. When it comes to composability, Maybe is superior to the two other alternatives, and Tester-Doer isn't thread-safe. </p> <h3 id="e95c8f5d7a6445139b58445d30498493"> Tester-Doer <a href="#e95c8f5d7a6445139b58445d30498493" title="permalink">#</a> </h3> <p> The first time I explicitly encountered the Tester-Doer pattern was in the <a href="https://amzn.to/2zXCCfH">Framework Design Guidelines</a>, which is from where I've taken the name. The pattern is, however, older. The idea that you can query an object about whether a given operation would be possible, and then you only perform it if the answer is affirmative, is almost a leitmotif in <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a>. Bertrand Meyer often uses linked lists and stacks as examples, but I'll instead use the example that Krzysztof Cwalina and Brad Abrams use: </p> <p> <pre><span style="color:#2b91af;">ICollection</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;numbers&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">if</span>&nbsp;(!numbers.IsReadOnly) &nbsp;&nbsp;&nbsp;&nbsp;numbers.Add(1);</pre> </p> <p> The idea with the Tester-Doer pattern is that you test whether an intended operation is legal, and only perform it if the answer is affirmative. In the example, you only add to the <code>numbers</code> collection if <code>IsReadOnly</code> is <code>false</code>. Here, <code>IsReadOnly</code> is the <em>Tester</em>, and <code>Add</code> is the <em>Doer</em>. </p> <p> As Jeffrey Richter points out in the book, this is a dangerous pattern: <blockquote> "The potential problem occurs when you have multiple threads accessing the object at the same time. For example, one thread could execute the test method, which reports that all is OK, and before the doer method executes, another thread could change the object, causing the doer to fail." </blockquote> In other words, the pattern isn't thread-safe. While multi-threaded programming was always supported in .NET, this was less of a concern when the guidelines were first published (2006) than it is today. The guidelines were in internal use in Microsoft years before they were published, and there wasn't many multi-core processors in use back then. </p> <p> Another problem with the Tester-Doer pattern is with discoverability. If you're looking for a way to add an element to a collection, you'd usually consider your search over once you find the <code>Add</code> method. Even if you wonder <em>Is this operation safe? Can I always add an element to a collection?</em> you <em>might</em> consider looking for a <code>CanAdd</code> method, but not an <code>IsReadOnly</code> property. Most people don't even ask the question in the first place, though. </p> <h3 id="08bc9f42d8f048119f952aa9c2d94b34"> From Tester-Doer to Try-Parse <a href="#08bc9f42d8f048119f952aa9c2d94b34" title="permalink">#</a> </h3> <p> You could refactor such a Tester-Doer API to a single method, which is both thread-safe and discoverable. One option is a variation of the Try-Parse idiom (discussed in detail below). Using it could look like this: </p> <p> <pre><span style="color:#2b91af;">ICollection</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;numbers&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">bool</span>&nbsp;wasAdded&nbsp;=&nbsp;numbers.TryAdd(1);</pre> </p> <p> In this special case, you may not need the <code>wasAdded</code> variable, because the original <code>Add</code> operation never returned a value. If, on the other hand, you do care whether or not the element was added to the collection, you'd have to figure out what to do in the case where the return value is <code>true</code> and <code>false</code>, respectively. </p> <p> Compared to the more idiomatic example of the Try-Parse idiom below, you may have noticed that the <code>TryAdd</code> method shown here takes no <code>out</code> parameter. This is because the original <code>Add</code> method returns <code>void</code>; there's nothing to return. From <a href="/2018/01/15/unit-isomorphisms">unit isomorphisms</a>, however, we know that <em>unit</em> is isomorphic to <code>void</code>, so we could, more explicitly, have defined a <code>TryAdd</code> method with this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;TryAdd(<span style="color:#2b91af;">T</span>&nbsp;item,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">Unit</span>&nbsp;unit)</pre> </p> <p> There's no point in doing this, however, apart from demonstrating that the isomorphism holds. </p> <h3 id="e246bcfabcab42e8b76e2b3e314174c4"> From Tester-Doer to Maybe <a href="#e246bcfabcab42e8b76e2b3e314174c4" title="permalink">#</a> </h3> <p> You can also refactor the add-to-collection example to return a Maybe value, although in this degenerate case, it makes little sense. If you automate the refactoring process, you'd arrive at an API like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;TryAdd(<span style="color:#2b91af;">T</span>&nbsp;item)</pre> </p> <p> Using it would look like this: </p> <p> <pre><span style="color:#2b91af;">ICollection</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;numbers&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&nbsp;m&nbsp;=&nbsp;numbers.TryAdd(1);</pre> </p> <p> The contract is consistent with what Maybe implies: You'd get an empty <code>Maybe&lt;Unit&gt;</code> object if the <em>add</em> operation 'failed', and a populated <code>Maybe&lt;Unit&gt;</code> object if the <em>add</em> operation succeeded. Even in the populated case, though, the value contained in the Maybe object would be <em>unit</em>, which carries no further information than its existence. </p> <p> To be clear, this isn't close to a proper functional design because all the interesting action happens as a side effect. Does the design have to be functional? No, it clearly isn't in this case, but Maybe is a concept that originated in functional programming, so you could be misled to believe that I'm trying to pass this particular design off as functional. It's not. </p> <p> A functional version of this API could look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">ICollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;TryAdd(<span style="color:#2b91af;">T</span>&nbsp;item)</pre> </p> <p> An implementation wouldn't mutate the object itself, but rather return a new collection with the added item, in case that was possible. This is, however, always possible, because you can always concatenate <code>item</code> to the front of the collection. In other words, this particular line of inquiry is increasingly veering into the territory of the absurd. This isn't, however, a counter-example of my proposition that the isomorphism exists; it's just a result of the initial example being degenerate. </p> <h3 id="9817f0d35d99428f93c38cab9fabc9ad"> Try-Parse <a href="#9817f0d35d99428f93c38cab9fabc9ad" title="permalink">#</a> </h3> <p> Another idiom described in the Framework Design Guidelines is the Try-Parse idiom. This seems to be a coding idiom more specific to the .NET framework, which is the reason I call it an <em>idiom</em> instead of a <em>pattern</em>. (Perhaps it is, after all, a pattern... I'm sure many of my readers are better informed about how problems like these are solved in other languages, and can enlighten me.) </p> <p> A better name might be <em>Try-Do</em>, since the idiom doesn't have to be constrained to parsing. The example that Cwalina and Abrams supply, however, relates to parsing a <code>string</code> into a <code>DateTime</code> value. Such an API is <a href="https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparse">already available in the base class library</a>. Using it looks like this: </p> <p> <pre><span style="color:blue;">bool</span>&nbsp;couldParse&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.TryParse(candidate,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;dateTime);</pre> </p> <p> Since <code>DateTime</code> is a <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types">value type</a>, the <code>out</code> parameter will never be <code>null</code>, even if parsing fails. You can, however, examine the return value <code>couldParse</code> to determine whether the <code>candidate</code> could be parsed. </p> <p> In the running commentary in the book, Jeffrey Richter likes this much better: <blockquote> "I like this guideline a lot. It solves the race-condition problem and the performance problem." </blockquote> I agree that it's better than Tester-Doer, but that doesn't mean that you can't refactor such a design to that pattern. </p> <h3 id="166ef01b6b64481a85fe64a6e9e07dc6"> From Try-Parse to Tester-Doer <a href="#166ef01b6b64481a85fe64a6e9e07dc6" title="permalink">#</a> </h3> <p> While I see no compelling reason to design parsing attempts with the Tester-Doer pattern, it's possible. You could create an API that enables interaction like this: </p> <p> <pre><span style="color:#2b91af;">DateTime</span>&nbsp;dateTime&nbsp;=&nbsp;<span style="color:blue;">default</span>(<span style="color:#2b91af;">DateTime</span>); <span style="color:blue;">bool</span>&nbsp;canParse&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeEnvy</span>.CanParse(candidate); <span style="color:blue;">if</span>&nbsp;(canParse) &nbsp;&nbsp;&nbsp;&nbsp;dateTime&nbsp;=&nbsp;<span style="color:#2b91af;">DateTime</span>.Parse(candidate);</pre> </p> <p> You'd need to add a new <code>CanParse</code> method with this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;CanParse(<span style="color:blue;">string</span>&nbsp;candidate)</pre> </p> <p> In this particular example, you don't have to add a <code>Parse</code> method, because it already exists in the base class library, but in other examples, you'd have to add such a method as well. </p> <p> This example doesn't suffer from issues with thread safety, since strings are immutable, but in general, that problem is always a concern with the Tester-Doer <a href="/2019/01/21/some-thoughts-on-anti-patterns">anti-pattern</a>. Discoverability still suffers in this example. </p> <h3 id="ffd6284cfc8f4f528d1a3b80849fbf8c"> From Try-Parse to Maybe <a href="#ffd6284cfc8f4f528d1a3b80849fbf8c" title="permalink">#</a> </h3> <p> While the Try-Parse idiom is thread-safe, it isn't composable. Every time you run into an API modelled over this template, you have to stop what you're doing and check the return value. Did the operation succeed? Was should the code do if it didn't? </p> <p> <em>Maybe</em>, on the other hand, is composable, so is a much better way to model problems such as parsing. Typically, methods or functions that return Maybe values are still prefixed with <em>Try</em>, but there's no longer any <code>out</code> parameter. A Maybe-based <code>TryParse</code> function could look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;TryParse(<span style="color:blue;">string</span>&nbsp;candidate)</pre> </p> <p> You could use it like this: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;m&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeEnvy</span>.TryParse(candidate);</pre> </p> <p> If the <code>candidate</code> was successfully parsed, you get a populated <code>Maybe&lt;DateTime&gt;</code>; if the string was invalid, you get an empty <code>Maybe&lt;DateTime&gt;</code>. </p> <p> A Maybe object composes much better with other computations. Contrary to the Try-Parse idiom, you don't have to stop and examine a Boolean return value. You don't even have to deal with empty cases at the point where you parse. Instead, you can defer the decision about what to do in case of failure until a later time, where it may be more obvious what to do in that case. </p> <h3 id="4f27ce3476114a5f9b0f80fd415e5370"> Maybe <a href="#4f27ce3476114a5f9b0f80fd415e5370" title="permalink">#</a> </h3> <p> In my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID</a> Pluralsight course, you get a walk-through of all three options for dealing with an operation that could potentially fail. Like in this article, the course starts with Tester-Doer, progresses over Try-Parse, and arrives at a Maybe-based implementation. In that course, the example involves reading a (previously stored) message from a text file. The final API looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;Read(<span style="color:blue;">int</span>&nbsp;id)</pre> </p> <p> The protocol implied by such a signature is that you supply an ID, and if a message with that ID exists on disc, you receive a populated <code>Maybe&lt;string&gt;</code>; otherwise, an empty object. This is not only composable, but also thread-safe. For anyone who understands the <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> of Maybe, it's clear that this is an operation that could fail. Ultimately, client code will have to deal with empty Maybe values, but this doesn't have to happen immediately. Such a decision can be deferred until a proper context exists for that purpose. </p> <h3 id="d35fbacb32bb4ef6afc843813ba901f1"> From Maybe to Tester-Doer <a href="#d35fbacb32bb4ef6afc843813ba901f1" title="permalink">#</a> </h3> <p> Since Tester-Doer is the least useful of the patterns discussed in this article, it makes little sense to refactor a Maybe-based API to a Tester-Doer implementation. Nonetheless, it's still possible. The API could look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Exists(<span style="color:blue;">int</span>&nbsp;id) <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Read(<span style="color:blue;">int</span>&nbsp;id)</pre> </p> <p> Not only is this design not thread-safe, but it's another example of poor discoverability. While the doer is called <code>Read</code>, the tester isn't called <code>CanRead</code>, but rather <code>Exists</code>. If the class has other members, these could be listed interleaved between <code>Exists</code> and <code>Read</code>. It wouldn't be obvious that these two members were designed to be used together. </p> <p> Again, the intended usage is code like this: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;message; <span style="color:blue;">if</span>&nbsp;(fileStore.Exists(49)) &nbsp;&nbsp;&nbsp;&nbsp;message&nbsp;=&nbsp;fileStore.Read(49);</pre> </p> <p> This is still problematic, because you need to decide what to do in the <code>else</code> case as well, although you don't see that case here. </p> <p> The point is, still, that you <em>can</em> translate from one representation to another without loss of information; not that you should. </p> <h3 id="3bbc92082af143d29681b2ce0bb11ccb"> From Maybe to Try-Parse <a href="#3bbc92082af143d29681b2ce0bb11ccb" title="permalink">#</a> </h3> <p> Of the three representations discussed in this article, I firmly believe that a Maybe-based API is superior. Unfortunately, the .NET base class library doesn't (yet) come with a built-in Maybe object, so if you're developing an API as part of a reusable library, you have two options: <ul> <li>Export the library's <code>Maybe&lt;T&gt;</code> type together with the methods that return it.</li> <li>Use Try-Parse for interoperability reasons.</li> </ul> This is the only reason I can think of to use the Try-Parse idiom. For the <code>FileStore</code> example from my Pluralsight course, this would imply not a <code>TryParse</code> method, but a <code>TryRead</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;TryRead(<span style="color:blue;">int</span>&nbsp;id,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">string</span>&nbsp;message)</pre> </p> <p> This would enable you to expose the method in a reusable library. Client code could interact with it like this: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;message; <span style="color:blue;">if</span>&nbsp;(!fileStore.TryRead(50,&nbsp;<span style="color:blue;">out</span>&nbsp;message)) &nbsp;&nbsp;&nbsp;&nbsp;message&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>;</pre> </p> <p> This has all the problems associated with the Try-Parse idiom already discussed in this article, but it does, at least, have a basic use case. </p> <h3 id="c04073bcc534481eaaf1ba43dd2a22a4"> Isomorphism with Either <a href="#c04073bcc534481eaaf1ba43dd2a22a4" title="permalink">#</a> </h3> <p> At this point, I hope that you find it reasonable to believe that the three representations, Tester-Doer, Try-Parse, and Maybe, are isomorphic. You can translate between any of these representations to any other of these without loss of information. This also means that you can translate back again. </p> <p> While I've only argued with a series of examples, it's my experience that these three representations are truly isomorphic. You can always translate any of these representations into another. Mostly, though, I translate into Maybe. If you disagree with my proposition, all you have to do is to provide a counter-example. </p> <p> There's a fourth isomorphism that's already well-known, and that's between Maybe and <a href="/2018/06/11/church-encoded-either">Either</a>. Specifically, <code>Maybe&lt;T&gt;</code> is isomorphic to <code>Either&lt;Unit, T&gt;</code>. In <a href="https://www.haskell.org">Haskell</a>, this is easily demonstrated with this set of functions: </p> <p> <pre><span style="color:#2b91af;">toMaybe</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;()&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a toMaybe&nbsp;(Left&nbsp;<span style="color:blue;">()</span>)&nbsp;=&nbsp;Nothing toMaybe&nbsp;(Right&nbsp;x)&nbsp;=&nbsp;Just&nbsp;x <span style="color:#2b91af;">fromMaybe</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;()&nbsp;a fromMaybe&nbsp;Nothing&nbsp;=&nbsp;Left&nbsp;<span style="color:blue;">()</span> fromMaybe&nbsp;(Just&nbsp;x)&nbsp;=&nbsp;Right&nbsp;x</pre> </p> <p> Translated to C#, using the <a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a> together with the Church-encoded Either, these two functions could look like the following, starting with the conversion from Maybe to Either: </p> <p> <pre><span style="color:green;">//&nbsp;On&nbsp;Maybe:</span> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;ToEither&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Unit</span>.Value), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(x)); }</pre> </p> <p> Likewise, the conversion from Either to Maybe: </p> <p> <pre><span style="color:green;">//&nbsp;On&nbsp;Either:</span> <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;ToMaybe&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onLeft:&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:#2b91af;">T</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onRight:&nbsp;x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:#2b91af;">T</span>&gt;(x)); }</pre> </p> <p> You can convert back and forth to your heart's content, as this parametrised <a href="https://xunit.net">xUnit.net</a> 2.3.1 test shows: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(42)] [<span style="color:#2b91af;">InlineData</span>(1337)] [<span style="color:#2b91af;">InlineData</span>(2112)] [<span style="color:#2b91af;">InlineData</span>(90125)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;IsomorphicWithPopulatedMaybe(<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">Unit</span>,&nbsp;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;expected.ToMaybe().ToEither(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); }</pre> </p> <p> I decided to exclude <code>IEither&lt;Unit, T&gt;</code> from the overall theme of this article in order to better contrast three alternatives that may not otherwise look equivalent. That <code>IEither&lt;Unit, T&gt;</code> is isomorphic to <code>IMaybe&lt;T&gt;</code> is a well-known result. Besides, I think that both of these two representations already inhabit the same conceptual space. Either and Maybe are both well-known in statically typed functional programming. </p> <h3 id="8e3e7b55ac1e49568712675713426e59"> Summary <a href="#8e3e7b55ac1e49568712675713426e59" title="permalink">#</a> </h3> <p> The Tester-Doer pattern is a decades-old design pattern that attempts to model how to perform operations that can potentially fail, without relying on exceptions for flow control. It predates mainstream multi-core processors by decades, which can explain why it even exists as a pattern in the first place. At the time people arrived at the pattern, thread-safety wasn't a big concern. </p> <p> The Try-Parse idiom is a thread-safe alternative to the Tester-Doer pattern. It combines the two <em>tester</em> and <em>doer</em> methods into a single method with an <code>out</code> parameter. While thread-safe, it's not composable. </p> <p> <em>Maybe</em> offers the best of both worlds. It's both thread-safe and composable. It's also as discoverable as any Try-Parse method. </p> <p> These three alternatives are all, however, isomorphic. This means that you can refactor any of the three designs into one of the other designs, without loss of information. It also means that you can implement <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapters</a> between particular implementations, should you so desire. You see this frequently in <a href="https://fsharp.org">F#</a> code, where functions that return <code>'a option</code> adapt Try-Parse methods from the .NET base class library. </p> <p> While all three designs are equivalent in the sense that you can translate one into another, it doesn't imply that they're equally useful. <em>Maybe</em> is the superior design, and Tester-Doer clearly inferior. </p> <p> <strong>Next:</strong> <a href="/2020/02/10/builder-isomorphisms">Builder isomorphisms</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Payment types catamorphism https://blog.ploeh.dk/2019/07/08/payment-types-catamorphism 2019-07-08T06:08:00+00:00 Mark Seemann <div id="post"> <p> <em>You can find the catamorphism for a custom sum type. Here's an example.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for a domain-specific <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>, as well as how to identify it. The beginning of this article presents the catamorphism in C#, with a few examples. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <p> In all previous articles in the series, you've seen catamorphisms for well-known data structures: <a href="/2019/05/06/boolean-catamorphism">Boolean values</a>, <a href="/2019/05/13/peano-catamorphism">Peano numbers</a>, <a href="/2019/05/20/maybe-catamorphism">Maybe</a>, <a href="/2019/06/10/tree-catamorphism">trees</a>, and so on. These are all general-purpose data structures, so you might be left with the impression that catamorphisms are only related to such general types. That's not the case. The point of this article is to demonstrate that you can find the catamorphism for your own custom, domain-specific sum type as well. </p> <h3 id="2b6f7df594c0474589ae9805f1e1a1d0"> C# catamorphism <a href="#2b6f7df594c0474589ae9805f1e1a1d0" title="permalink">#</a> </h3> <p> The custom type we'll examine in this article is the <a href="/2018/06/18/church-encoded-payment-types">Church-encoded payment types</a> I've previously written about. It's just an example of a custom data type, but it serves the purpose of illustration because I've already shown it as a Church encoding in C#, <a href="/2018/06/25/visitor-as-a-sum-type">as a Visitor in C#</a>, and <a href="/2016/11/28/easy-domain-modelling-with-types">as a discriminated union in F#</a>. </p> <p> The catamorphism for the <code>IPaymentType</code> interface is the <code>Match</code> method: </p> <p> <pre><span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;individual, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;parent, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;child);</pre> </p> <p> As has turned out to be a common trait, the catamorphism is identical to the Church encoding. </p> <p> I'm not going to show more than a few examples of using the <code>Match</code> method, because you can find other examples in the previous articles, </p> <p> <pre>&gt; <span style="color:#2b91af;">IPaymentType</span>&nbsp;p&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Individual</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>(<span style="color:#a31515;">&quot;Visa&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Pay&quot;</span>)); &gt; p.Match(ps&nbsp;=&gt;&nbsp;ps.Name,&nbsp;ps&nbsp;=&gt;&nbsp;ps.Name,&nbsp;cps&nbsp;=&gt;&nbsp;cps.PaymentService.Name) "Visa" &gt; <span style="color:#2b91af;">IPaymentType</span>&nbsp;p&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Parent</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>(<span style="color:#a31515;">&quot;Visa&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Pay&quot;</span>)); &gt; p.Match(ps&nbsp;=&gt;&nbsp;ps.Name,&nbsp;ps&nbsp;=&gt;&nbsp;ps.Name,&nbsp;cps&nbsp;=&gt;&nbsp;cps.PaymentService.Name) "Visa" &gt; <span style="color:#2b91af;">IPaymentType</span>&nbsp;p&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Child</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChildPaymentService</span>(<span style="color:#a31515;">&quot;1234&quot;</span>,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>(<span style="color:#a31515;">&quot;Visa&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Pay&quot;</span>))); &gt; p.Match(ps&nbsp;=&gt;&nbsp;ps.Name,&nbsp;ps&nbsp;=&gt;&nbsp;ps.Name,&nbsp;cps&nbsp;=&gt;&nbsp;cps.PaymentService.Name) "Visa"</pre> </p> <p> These three examples from a <em>C# Interactive</em> session demonstrate that no matter which payment method you use, you can use the same <code>Match</code> method call to extract the payment name from the <code>p</code> object. </p> <h3 id="f2334a900eef421cb24c6e48a96e411b"> Payment types F-Algebra <a href="#f2334a900eef421cb24c6e48a96e411b" title="permalink">#</a> </h3> <p> As in the <a href="/2019/06/24/full-binary-tree-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. </p> <p> First, you'll have to define the auxiliary types involved in this API: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;PaymentService&nbsp;=&nbsp;PaymentService&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;paymentServiceName&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;paymentServiceAction&nbsp;::&nbsp;String &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">data</span>&nbsp;ChildPaymentService&nbsp;=&nbsp;ChildPaymentService&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;originalTransactionKey&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;parentPaymentService&nbsp;::&nbsp;PaymentService &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> While F-Algebras and fixed points are mostly used for recursive data structures, you can also define an F-Algebra for a non-recursive data structure. You already saw examples of that in the articles about <a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a>, <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>, and <a href="/2019/06/03/either-catamorphism">Either catamorphism</a>. While each of the three payment types have associated data, none of it is parametrically polymorphic, so a single type argument for the carrier type suffices: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;PaymentTypeF&nbsp;c&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;IndividualF&nbsp;PaymentService &nbsp;&nbsp;|&nbsp;ParentF&nbsp;PaymentService &nbsp;&nbsp;|&nbsp;ChildF&nbsp;ChildPaymentService &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">PaymentTypeF</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;(IndividualF&nbsp;ps)&nbsp;=&nbsp;IndividualF&nbsp;ps &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ParentF&nbsp;ps)&nbsp;=&nbsp;ParentF&nbsp;ps &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ChildF&nbsp;cps)&nbsp;=&nbsp;ChildF&nbsp;cps</pre> </p> <p> I chose to call the carrier type <code>c</code> (for <em>carrier</em>). As was also the case with <code>BoolF</code>, <code>MaybeF</code>, and <code>EitherF</code>, the <code>Functor</code> instance ignores the map function because the carrier type is missing from all three cases. Like the <code>Functor</code> instances for <code>BoolF</code>, <code>MaybeF</code>, and <code>EitherF</code>, it'd seem that nothing happens, but at the type level, this is still a translation from <code>PaymentTypeF c</code> to <code>PaymentTypeF c1</code>. Not much of a function, perhaps, but definitely an <em>endofunctor</em>. </p> <p> Some helper functions make it a little easier to create <code>Fix PaymentTypeF</code> values, but there's really not much to them: </p> <p> <pre><span style="color:#2b91af;">individualF</span>&nbsp;::&nbsp;<span style="color:blue;">PaymentService</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">PaymentTypeF</span> individualF&nbsp;=&nbsp;Fix&nbsp;.&nbsp;IndividualF <span style="color:#2b91af;">parentF</span>&nbsp;::&nbsp;<span style="color:blue;">PaymentService</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">PaymentTypeF</span> parentF&nbsp;=&nbsp;Fix&nbsp;.&nbsp;ParentF <span style="color:#2b91af;">childF</span>&nbsp;::&nbsp;<span style="color:blue;">ChildPaymentService</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">PaymentTypeF</span> childF&nbsp;=&nbsp;Fix&nbsp;.&nbsp;ChildF</pre> </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="da3c2c0fee2747bebb1db38c15110bcb"> Haskell catamorphism <a href="#da3c2c0fee2747bebb1db38c15110bcb" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>PaymentTypeF</code>), and an object <code>c</code>, but you still need to find a morphism <code>PaymentTypeF c -&gt; c</code>. </p> <p> As in the previous articles, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>paymentF&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(IndividualF&nbsp;ps)&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ParentF&nbsp;ps)&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ChildF&nbsp;cps)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from the <code>IndividualF</code> case? You could pass an argument to the <code>paymentF</code> function, but you shouldn't ignore the data <code>ps</code> contained in the case, so it has to be a function: </p> <p> <pre>paymentF&nbsp;fi&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(IndividualF&nbsp;ps)&nbsp;=&nbsp;fi&nbsp;ps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ParentF&nbsp;ps)&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ChildF&nbsp;cps)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> I chose to call the argument <code>fi</code>, for <em>function, individual</em>. You can pass a similar argument to deal with the <code>ParentF</code> case: </p> <p> <pre>paymentF&nbsp;fi&nbsp;fp&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(IndividualF&nbsp;ps)&nbsp;=&nbsp;fi&nbsp;ps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ParentF&nbsp;ps)&nbsp;=&nbsp;fp&nbsp;ps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ChildF&nbsp;cps)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> And of course with the remaining <code>ChildF</code> case as well: </p> <p> <pre><span style="color:#2b91af;">paymentF</span>&nbsp;::&nbsp;(<span style="color:blue;">PaymentService</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">PaymentService</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">ChildPaymentService</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Fix&nbsp;PaymentTypeF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c paymentF&nbsp;fi&nbsp;fp&nbsp;fc&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(IndividualF&nbsp;ps)&nbsp;=&nbsp;fi&nbsp;ps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ParentF&nbsp;ps)&nbsp;=&nbsp;fp&nbsp;ps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ChildF&nbsp;cps)&nbsp;=&nbsp;fc&nbsp;cps</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>PaymentTypeF</code>, the compiler infers that the <code>alg</code> function has the type <code>PaymentTypeF c -&gt; c</code>, which is just what you need! </p> <p> You can now see what the carrier type <code>c</code> is for. It's the type that the algebra extracts, and thus the type that the catamorphism returns. </p> <p> This, then, is the catamorphism for the payment types. Except for the <a href="/2019/06/10/tree-catamorphism">tree catamorphism</a>, all catamorphisms so far have been pairs, but this one is a triplet of functions. This is because the sum type has three cases instead of two. </p> <p> As you've seen repeatedly, this isn't the only possible catamorphism, since you can, for example, trivially reorder the arguments to <code>paymentF</code>. The version shown here is, however, equivalent to the above C# <code>Match</code> method. </p> <h3 id="e6248a9ea34148c79c2b03acc92de5f7"> Usage <a href="#e6248a9ea34148c79c2b03acc92de5f7" title="permalink">#</a> </h3> <p> You can use the catamorphism as a basis for other functionality. If, for example, you want to convert a <code>Fix PaymentTypeF</code> value to JSON, you can first define an <a href="http://hackage.haskell.org/package/aeson/docs/Data-Aeson.html">Aeson</a> record type for that purpose: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;PaymentJson&nbsp;=&nbsp;PaymentJson&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;action&nbsp;::&nbsp;String &nbsp;&nbsp;,&nbsp;startRecurrent&nbsp;::&nbsp;Bool &nbsp;&nbsp;,&nbsp;transactionKey&nbsp;::&nbsp;Maybe&nbsp;String &nbsp;&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Generic</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">ToJSON</span>&nbsp;<span style="color:blue;">PaymentJson</span></pre> </p> <p> Subsequently, you can use <code>paymentF</code> to implement a conversion from <code>Fix PaymentTypeF</code> to <code>PaymentJson</code>, as in the previous articles: </p> <p> <pre><span style="color:#2b91af;">toJson</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">PaymentTypeF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">PaymentJson</span> toJson&nbsp;= &nbsp;&nbsp;paymentF &nbsp;&nbsp;&nbsp;&nbsp;(\(PaymentService&nbsp;n&nbsp;a)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;&nbsp;PaymentJson&nbsp;n&nbsp;a&nbsp;False&nbsp;Nothing) &nbsp;&nbsp;&nbsp;&nbsp;(\(PaymentService&nbsp;n&nbsp;a)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;&nbsp;PaymentJson&nbsp;n&nbsp;a&nbsp;True&nbsp;Nothing) &nbsp;&nbsp;&nbsp;&nbsp;(\(ChildPaymentService&nbsp;k&nbsp;(PaymentService&nbsp;n&nbsp;a))&nbsp;-&gt;&nbsp;PaymentJson&nbsp;n&nbsp;a&nbsp;False&nbsp;$&nbsp;Just&nbsp;k)</pre> </p> <p> Testing it in GHCi, it works as it's supposed to: </p> <p> <pre>Prelude Data.Aeson B Payment&gt; B.putStrLn $ encode $ toJson $ parentF $ PaymentService "Visa" "Pay" {"transactionKey":null,"startRecurrent":true,"action":"Pay","name":"Visa"}</pre> </p> <p> Clearly, it would have been easier to define the payment types shown here as a regular Haskell sum type and just use standard pattern matching, but the purpose of this article isn't to present useful code; the only purpose of the code here is to demonstrate how to identify the catamorphism for a custom domain-specific sum type. </p> <h3 id="153479fffaf647f6ad6f5fc6a63fe025"> Summary <a href="#153479fffaf647f6ad6f5fc6a63fe025" title="permalink">#</a> </h3> <p> Even custom, domain-specific sum types have catamorphisms. This article presented the catamorphism for a custom payment sum type. Because this particular sum type has three cases, the catamorphism is a triplet, instead of a pair, which has otherwise been the most common shape of catamorphisms in previous articles. </p> <p> <strong>Next:</strong> <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">Some design patterns as universal abstractions</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Yes silver bullet https://blog.ploeh.dk/2019/07/01/yes-silver-bullet 2019-07-01T07:38:00+00:00 Mark Seemann <div id="post"> <p> <em>Since Fred Brooks published his essay, I believe that we, contrary to his prediction, have witnessed several silver bullets.</em> </p> <p> I've been rereading <a href="https://en.wikipedia.org/wiki/Fred_Brooks">Fred Brooks</a>'s 1986 essay <a href="https://en.wikipedia.org/wiki/No_Silver_Bullet">No Silver Bullet</a> because I've become increasingly concerned that people seem to draw the wrong conclusions from it. <a href="https://martinfowler.com/bliki/SemanticDiffusion.html">Semantic diffusion</a> seems to have set in. These days, when people state something along the lines that there's <em>no silver bullet in software development</em>, I often get the impression that they mean that there's no panacea. </p> <p> Indeed; I agree. There's no miracle cure that will magically make all problems in software development go away. That's not what the essay states, however. It is, fortunately, more subtle than that. </p> <h3 id="712292e6c9c34663801dd40b4f278d3d"> No silver bullet reread <a href="#712292e6c9c34663801dd40b4f278d3d" title="permalink">#</a> </h3> <p> It's a great essay. It's not my intent to dispute the central argument of the essay, but I think that Brooks made one particular assumption that I disagree with. That doesn't make me smarter in any way. He wrote the essay in 1986. I'm writing this in 2019, with the benefit of the experience of all the years in-between. Hindsight is 20-20, so anyone could make the observations that I do here. </p> <p> Before we get to that, though, a brief summary of the essence of the essay is in order. In short, the conclusion is this: <blockquote> <p> "There is no single development, in either technology or management technique, which by itself promises even one order-of-magnitude improvement within a decade in productivity, in reliability, in simplicity." </p> <footer><cite>Fred Brooks, <em>No Silver Bullet</em>, 1986</cite></footer> </blockquote> The beginning of the essay is a brilliant analysis of the reasons why software development is inherently difficult. If you read this together with Jack Reeves <em>What Is Software Design?</em> (available various places on the internet, or as an appendix in <a href="http://amzn.to/19W4JHk">APPP</a>), you'll probably agree that there's an inherent complexity to software development that no invention is likely to dispel. </p> <p> Ostensibly in the tradition of <a href="https://en.wikipedia.org/wiki/Aristotle">Aristotle</a>, Brooks distinguishes between <em>essential</em> and <em>accidental</em> complexity. This distinction is central to his argument, so it's worth discussing for a minute. </p> <p> Software development problems are complex, i.e. made up of many interacting sub-problems. Some of that complexity is <em>accidental</em>. This doesn't imply randomness or sloppiness, but only that the complexity isn't inherent to the problem; that it's only the result of our (human) failure to achieve perfection. </p> <p> If you imagine that you could whittle away all the accidental complexity, you'd ultimately reach a point where, in the words of Saint Exupéry, <em>there is nothing more to remove</em>. What's left is the <em>essential</em> complexity. </p> <p> Brooks' conjecture is that a typical software development project comes with both essential and accidental complexity. In his 1995 reflections <em>"No Silver Bullet" Refired</em> (available in <a href="http://bit.ly/mythical-man-month">The Mythical Man-Month</a>), he clarifies what he already implied in 1986: <blockquote> <p> "It is my opinion, and that is all, that the accidental or representational part of the work is now down to about half or less of the total." </p> <footer><cite>Fred Brooks, <em>"No Silver Bullet" Refired</em>, 1995</cite></footer> </blockquote> This I fundamentally disagree with, but more on that later. It makes sense to me to graphically represent the argument like this: </p> <p> <img src="/content/binary/essential-accidental-complexity-shells-brooks-scenario.png" alt="Some, but not much, accidental complexity as a shell around essential complexity."> </p> <p> The way that I think of Brooks' argument is that any software project contains some essential and some accidental complexity. For a given project, the size of the essential complexity is fixed. </p> <p> Brooks believes that less than half of the overall complexity is accidental: </p> <p> <img src="/content/binary/essential-accidental-complexity-pie-chart-brooks-scenario.png" alt="Essential and accidental complexity pie chart."> </p> <p> While a pie chart better illustrates the supposed ratio between the two types of complexity, I prefer to view Brooks' arguments as the first diagram, above. In that visualisation, the essential complexity is a core of fixed size, while accidental complexity is something you can work at removing. If you keep improving your process and technology, you may, conceptually, be able to remove (almost) all of it. </p> <p> <img src="/content/binary/essential-almost-no-accidental-complexity-shells.png" alt="Essential complexity with a very thin shell of accidental complexity."> </p> <p> Brooks' point, with which I agree, is that if the essential complexity is inherent, then you can't reduce the size of it. The only way to decrease the overall complexity is to reduce the accidental complexity. </p> <p> If you agree with the assessment that less than half of the overall complexity in modern software development is accidental, then it follows that no dramatic improvements are available. Even if you remove all accidental complexity, you've only reduced overall complexity by, say, forty percent. </p> <h3 id="d8e6f84d104b4ff6ad6b5473e46a4e30"> Accidental complexity abounds <a href="#d8e6f84d104b4ff6ad6b5473e46a4e30" title="permalink">#</a> </h3> <p> I find Brooks' arguments compelling. I do not, however, accept the premise that there's only little accidental complexity left. Instead of the above diagrams, I believe that the situation looks more like this (not to scale): </p> <p> <img src="/content/binary/accidental-complexity-with-tiny-core-of-essential-complexity.png" alt="Accidental complexity with a tiny core of essential complexity."> </p> <p> I think that most of the complexity in software development is accidental. I'm not sure about today, but I believe that I have compelling evidence that this was the case in 1986, so I don't see why it shouldn't still be the case. </p> <p> To be clear, this is all anecdotal, since I don't believe that software development is quantifiable. In the essay, Brooks explicitly talks about the <em>invisibility</em> of software. Software is pure <em>thought stuff;</em> you can't measure it. I discuss this in my <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">Humane Code video</a>, but I also recommend that you read <a href="http://bit.ly/leprechauns-of-software-engineering">The Leprechauns of Software Engineering</a> if you have any illusions that we, as an industry, have any reliable measurements of productivity. </p> <p> Brooks predicts that, within the decade (from 1986 to 1996), there would be no single development that would increase productivity with an order of magnitude, i.e. by a factor of at least ten. Ironically, when he wrote <em>"No Silver Bullet" Refired</em> in 1995, at least two such developments were already in motion. </p> <p> We can't blame Brooks for not identifying those developments, because in 1995, their impact was not yet apparent. Again, hindsight is 20-20. </p> <p> Neither of these two developments are purely technological, although technology plays a role. Notice, though, that Brooks' prediction included <em>technology or management technique</em>. It's in the interaction between technology and the humane that the orders-of-magnitude developments emerged. </p> <h3 id="1d23f6fb89884b6d9833ce09d68a3b0f"> World Wide Web <a href="#1d23f6fb89884b6d9833ce09d68a3b0f" title="permalink">#</a> </h3> <p> I have a dirty little secret. In the beginning of my programming career, I became quite the expert on a programming framework called <a href="https://en.wikipedia.org/wiki/Microsoft_Commerce_Server">Microsoft Commerce Server</a>. In fact, I co-authored a chapter of <a href="https://amzn.to/2CpE4rr">Professional Commerce Server 2000 Programming</a>, and in 2003 I received an <a href="https://mvp.microsoft.com">MVP</a> award as an acknowledgement of my work in the Commerce Server community (such as it were; it was mostly on <a href="https://en.wikipedia.org/wiki/Usenet">Usenet</a>). </p> <p> The Commerce Server framework was a black box. This was long before Microsoft embraced open source, and while there was a bit of official documentation, it was superficial; it was mostly of the <em>getting-started</em> kind. </p> <p> Over several years, I managed to figure out how the framework really worked, and thus, how one could extend it. This was a painstaking process. Since it was a black box, I couldn't just go and read the code to figure out how it worked. The framework was written in C++ and Visual Basic, so there wasn't even IL code to decompile. </p> <p> I had one window into the framework. It relied on SQL Server, and I could attach the profiler tool to spy on its interaction with the database. Painstakingly, over several years, I managed to wrest the framework's secrets from it. </p> <p> I wasted much time doing detective work like that. </p> <p> In general, programming in the late nineties and early two-thousands was less productive, not because the languages or tools were orders-of-magnitude worse than today, but because when you hit a snag, you were in trouble. </p> <p> These days, if you run into a problem beyond your abilities, you can ask for help on the World Wide Web. Usually, you'll find an existing answer on <a href="https://stackoverflow.com">Stack Overflow</a>, and you'll be able to proceed without too much delay. </p> <p> Compared to twenty years ago, I believe that the World Wide Web has increased my productivity more than ten-fold. While it also existed in 1995, there wasn't much content. It's not the technology itself that provides the productivity increase, but rather the synergy of technology and human knowledge. </p> <p> I think that Brooks vastly underestimated how much time one can waste when one is stuck. That's a sort of accidental complexity, although in the development process rather than in the technology itself. </p> <h3 id="a3b19483cd6a4c509d8c3a77fe324872"> Automated testing <a href="#a3b19483cd6a4c509d8c3a77fe324872" title="permalink">#</a> </h3> <p> In the late nineties, I was developing web sites (with Commerce Server). When I wanted to run my code to see if it worked, I'd launch the web site on my laptop, log in, click around and enter data until I was convinced that the functionality was working as it should. Most of the time, however, it wasn't, so I'd change a bit of the code, and go through the same process again. </p> <p> I think that's a common way to 'test' software; at least, it was back then. </p> <p> While you could get good at going through these motions quickly, verifying a single, or a handful of related functionalities, could easily take at least a couple of seconds, and usually more like half a minute. </p> <p> If you had dozens, or even hundreds, of different scenarios to address, you obviously wouldn't run through them all every time you changed the code. At the very best, you'd click your way through three of four usage scenarios that you thought were relevant to the change you'd made. Other functionality, earlier declared <em>done</em>, you just considered to be unaffected. </p> <p> Needless to say, regressions were regular occurrences. </p> <p> In 2003 I discovered test-driven development, and through that, automated testing. While you can't directly compare unit tests with whole usage scenarios, I think it's fair to compare something like automated integration tests or user-scenario tests (whatever you want to call them) with manually clicking through an application. </p> <p> Even an integration test, if written properly, can verify a scenario <em>at least</em> ten times faster than you can do it by hand. A more realistic estimate is probably hundred times faster, or more. </p> <p> Granted, you have to write the automated test as well, and I know that it's not always trivial. Still, once you have an automated test suite in place, you can run it all the time. </p> <p> I never ran through <em>all</em> usage scenarios when I manually 'tested' my software. With automated tests, I do. This saves me from most regressions. </p> <p> This improvement is, in my opinion, a no-brainer. It's easily a factor ten improvement. All the time wasted manually 'testing' the software, plus the time wasted fixing regressions, can be put to better use. </p> <p> At the time Brooks was writing his own retrospective (in 1995), Kent Beck was beginning to talk to other people about test-driven development. As is a common theme in this article, hindsight is 20-20. </p> <h3 id="c7ca9269cce04b3ab934c97bc8cf0328"> Honourable mentions <a href="#c7ca9269cce04b3ab934c97bc8cf0328" title="permalink">#</a> </h3> <p> There's been other improvements in software development since 1986. I considered including several other improvements as bona fide orders-of-magnitude improvements, but I think that's probably going too far. Each of the following developments have, however, offered significant improvements: <ul> <li> <strong>Git.</strong> It's surprising how much more productive Git can make you. While it's somewhat better than centralised source control systems at the functionality also available with those other systems, the productivity increase comes from all the new, unanticipated workflows it enables. Before I started using DVCS, I'd have lots of code that was commented out, so that I could experiment with various alternatives. With Git, I just create a new branch, or stash my changes, and experiment with abandon. While it's probably not a ten-fold increase in productivity, I believe it's the simplest technology change you can make to dramatically increase your productivity. </li> <li> <strong>Garbage collection.</strong> Since I've admitted that I worked with Microsoft Commerce Server, I've probably lost all credibility with my reader already, but let's see if I can win back a little. While Commerce Server programming involved <a href="https://en.wikipedia.org/wiki/VBScript">VBScript</a> programming, it also often involved <a href="https://en.wikipedia.org/wiki/Component_Object_Model">COM</a> programming, and I did quite a bit of that in C++. Having to make sure that you've cleaned up all memory after use is a bother. Garbage collection just makes this work go away. It's hardly a ten-fold improvement in productivity, but I do find it significant. </li> <li> <strong>Agile software development.</strong> The methodology of decreasing the feedback time between implementation and deployment has made me much more productive. I'm not interested in peddling any particular methodology like Scrum as much as just the general concept of getting rapid feedback. Particularly if you combine continuous delivery with Git, you have a powerful combination. Brooks already talked about incremental software development, and had some hopes attached to this as well. My personal experience can only agree with his sentiment. Again, probably not in itself a ten-fold increase in productivity, but enough that I wouldn't want to work on a project where rapid feedback and incremental development wasn't valued. </li> </ul> I'm probably forgetting lots of other improvements that have happened in the last decades. That's fine. The purpose of this article isn't to produce an exhaustive list, but rather to make the argument that significant improvements have been made since Brooks wrote his essay. I think it'd be folly, then, to believe that we've seen the last of such improvements. </p> <p> Personally, I'm inclined to believe another order-of-magnitude improvement is right at our feet. </p> <h3 id="bd2d47d8dac2401e936ca7902bc9109d"> Statically typed functional programming <a href="#bd2d47d8dac2401e936ca7902bc9109d" title="permalink">#</a> </h3> <p> This section is conjecture on my part. The improvements I've so far covered are already realised (at least for those who choose to take advantage of them). The improvement I'll cover here is more speculative. </p> <p> I believe that statically typed functional programming offers another order-of-magnitude improvement over existing software development. Twenty years ago, I believed that object-oriented programming was a good idea. I now believe that I was wrong about that, so it's possible that in another twenty years, I'll also believe that I was wrong about functional programming. Take the following for what it is. </p> <p> When I carefully reread <em>No Silver Bullet</em>, I got the distinct impression that Brooks considered low-level details of programming part of its essential complexity: <blockquote> <p> "Much of the complexity in a software construct is, however, not due to conformity to the external world but rather to the implementation itself - its data structures, its algorithms, its connectivity." </p> <footer><cite>Fred Brooks, <em>"No Silver Bullet" Refired</em>, 1995</cite></footer> </blockquote> It's unreasonable to blame anyone writing in 1986, or 1995 for that matter, to think that <code>for</code> loops, variables, program state, and such other programming stables were anything but essential parts of the complexity of developing software. </p> <p> Someone, unfortunately I forget who, once made the point that all mainstream programming languages are layers of abstractions of how a CPU works. Assembly language is basically just mnemonics on top of a CPU instruction set, then C can be thought of as an abstraction over assembly language, C++ as the next step in abstraction, Java and C# as sort of abstractions of C++, and so on. The origin of the design is the physical CPU. You could say that these languages are designed in a bottom-up fashion. </p> <p> <img src="/content/binary/imperative-bottom-up-functional-top-down.png" alt="Imperative languages depicted as designed bottom-up, and functional languages as designed top-down."> </p> <p> Some functional languages (perhaps most famously <a href="https://www.haskell.org">Haskell</a>, but also <a href="https://en.wikipedia.org/wiki/APL_(programming_language)">APL</a>, and, possibly, <a href="https://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a>) are designed in a much more top-down fashion. You start with mathematical abstractions like <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a> and then figure out how to crystallise the theory into a programming language, and then again, via more layers of abstractions, how to turn the abstract language into machine code. </p> <p> The more you learn about the <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> functional alternative to programming, the more you begin to see mutable program state, variables, <code>for</code> loops, and similar language constructs merely as artefacts of the underlying model. Brooks, I think, thought of these as part of the essential complexity of programming. I don't think that that's the case. You can get by just fine with other abstractions instead. </p> <p> Besides, Brooks writes, under the heading of <em>Complexity:</em> <blockquote> <p> "From the complexity comes the difficulty of enumerating, much less understanding, all the possible states of the program, and from that comes the unreliability. From the complexity of the functions comes the difficulty of invoking those functions, which makes programs hard to use." </p> <footer><cite>Fred Brooks, <em>No Silver Bullet</em>, 1986</cite></footer> </blockquote> When he writes <em>functions</em>, I don't think that he means functions in the Haskell sense. I think that he means <em>operations</em>, <em>procedures</em>, or <em>methods</em>. </p> <p> Indeed, when you look at a C# method signature like the following, it's hard to enumerate, understand, or remember, all that it does: </p> <p> <pre><span style="color:blue;">int</span>?&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation);</pre> </p> <p> If this is a high-level function, many things could happen when you call that method. It could change the state of a database. It could send an email. It could mutate a variable. Not only that, but the behaviour could depend on non-deterministic factors, such as the date, time of day, or just raw randomness. Finally, how should you handle the return value? What does it mean if the return value is <em>null</em>? What if it's not? Is <code>0</code> a valid value? Are negative numbers valid? Are they different from positive values? </p> <p> It is, indeed, difficult to enumerate all the possible states of such a function. </p> <p> Consider, instead, a Haskell function with a type like this: </p> <p> <pre><span style="color:#2b91af;">tryAccept</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeT</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;<span style="color:#2b91af;">Int</span></pre> </p> <p> What happens if you invoke this function? It returns a value. Does it send any emails? Does it mutate any state? No, it can't, because the static type informs us that this is a pure function. If any programmer, anywhere inside of the function, or the functions it calls, or functions they call, etc. tried to do something impure, it wouldn't have compiled. </p> <p> Can we enumerate the states of the program? Certainly. We just have to figure out what <code>ReservationsProgram</code> is. After following a few types, we find this statically typed enumeration: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;ReservationsInstruction&nbsp;next&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;IsReservationInFuture&nbsp;Reservation&nbsp;(Bool&nbsp;-&gt;&nbsp;next) &nbsp;&nbsp;|&nbsp;ReadReservations&nbsp;UTCTime&nbsp;([Reservation]&nbsp;-&gt;&nbsp;next) &nbsp;&nbsp;|&nbsp;Create&nbsp;Reservation&nbsp;(Int&nbsp;-&gt;&nbsp;next) &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;Functor</pre> </p> <p> Essentially, there's three 'actions' that this type enables. The <code>tryAccept</code> function returns the <code>ReservationsProgram</code> inside of a <code>MaybeT</code> container, so there's a fourth option that something short-circuits along the way. </p> <p> You don't even have to keep track of this yourself. The compiler keeps you honest. Whenever you invoke the <code>tryAccept</code> function, the compiler will insist that you write code that can handle all possible outcomes. If you turn on the right compiler flags, the code is not going to compile if you don't. </p> <p> (Both code examples are taken from <a href="https://github.com/ploeh/dependency-injection-revisited">the same repository</a>.) </p> <p> Haskellers jokingly declare that <em>if Haskell code compiles, it works</em>. While humorous, there's a kernel of truth in that. An advanced type system can carry much information about the behaviour of a program. Some people, particularly programmers who come from a dynamically typed background, find Haskell's type system rigid. That's not an unreasonable criticism, but often, in dynamically typed languages, you have to write many automated tests to ensure that your program behaves as desired, and that it correctly handles various edge cases. A type system like Haskell's, on the other hand, embeds those rules in types instead of in tests. </p> <p> While you should still write automated tests for Haskell programs, fewer are needed. How many fewer? Compared to C-based languages, a factor ten isn't an unreasonable guess. </p> <p> After a few false starts, in 2014 I finally decided that <a href="https://fsharp.org">F#</a> would be my default choice of language on .NET. The reason for that decision was that I felt so much more productive in F# compared to C#. While F#'s type system doesn't embed information about pure versus impure functions, it does support <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a>, which is what enables the sort of compile-time <em>enumeration</em> that Brooks discusses. </p> <p> F# is still my .NET language of choice, but I find that I mostly 'think in' Haskell these days. My conjecture is that a sufficiently advanced type system (like Haskell's) could easily represent another order-of-magnitude improvement over mainstream imperative languages. </p> <h3 id="a75ae35933314755b1a0cdb665262bc5"> Improvements for those who want them <a href="#a75ae35933314755b1a0cdb665262bc5" title="permalink">#</a> </h3> <p> The essay <em>No Silver Bullet</em> is a perspicacious work. I think more people should read at least the first part, where Brooks explains why software development is hard. I find that analysis brilliant, and I agree: software development presupposes essential complexity. It's inherently hard. </p> <p> There's no reason to make it harder than it has to be, though. </p> <p> More than once, I've discussed productivity improvements with people, only to be met with the dismissal that 'there's no silver bullet'. </p> <p> Granted, there's no magical solution that will solve all problems with software development, but that doesn't mean that improvements can't be had. </p> <p> Consider the improvements I've argued for here. Everyone now uses the World Wide Web and sites like Stack Overflow for research; that particular improvement is firmly embedded in all organisations. On the other hand, I still regularly talk to organisations that don't routinely use automated testing. </p> <p> People still use centralised version control (like TFS or SVN). If there was ever a low-hanging fruit, changing to Git is one. Git is <em>free</em>, and there's plenty of tools you can use to migrate your version history to it. There's also plenty of training and help to be had. Yes, it'll require a small investment to make the change, but the productivity increase is significant. <blockquote> <p> "The future is already here — it's just not very evenly distributed." </p> <footer><cite>William Gibson</cite></footer> </blockquote> So it is with technology improvements. Automated testing is available, but not ubiquitous. Git is free, but still organisations stick to suboptimal version control. Haskell and F# are mature languages, yet programmers still program in C# or Java. </p> <h3 id="864e39a22bc84129bfecaafe33dd1757"> Summary <a href="#864e39a22bc84129bfecaafe33dd1757" title="permalink">#</a> </h3> <p> The essay <em>No Silver Bullet</em> was written in 1986, but seems to me to be increasingly misunderstood. When people today talk about it at all, it's mostly as an excuse to stay where they are. "There's no silver bullets," they'll say. </p> <p> The essay, however, doesn't argue that no improvements can be had. It only argues that no more order-of-magnitude improvements can be had. </p> <p> In the present essay I argue that, since Brooks wrote <em>No Silver Bullet</em>, more than one such improvement happened. Once the World Wide Web truly began furnishing <em>information at your fingertips</em>, you could be more productive because you wouldn't be <em>stuck</em> for days or weeks. Automated testing reduces the work that manual testers used to perform, as well as limiting regressions. </p> <p> If you accept my argument, that order-of-magnitude improvements appeared after 1986, this implies that Brooks' premise was wrong. In that case, there's no reason to believe that we've seen the last significant improvement to software development. </p> <p> I think that more such improvements await us. I suggest that statically typed functional programming offers such an advance, but if history teaches us anything, it seems that breakthroughs tend to be unpredictable. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="7e7e932f5eea47f3bab328c58e9d164a"> <div class="comment-author"><a href="http://blog.strobaek.org">Karsten Strøbæk</a> <a href="#7e7e932f5eea47f3bab328c58e9d164a">#</a></div> <div class="comment-content"> <p> As always I enjoy reading your blog, even though I don't understand half of it most of the time. Or is that most of it half of the time? Allow me to put a few observations forward. </p> <p> First I should confess, that I have actually not read the whole of Brook's essay. When I initially tried I got about half way through; it sounds like I should make another go at it. That of course will not stop me from commenting on the above. </p> <p> Brook talks about complexity. To me designing and implementing a software system is not complex. Quantum physics is complex. Flying an airplane is difficult. Software development may be difficult depending on the task at hand (and unfortunately the qualifications of the team), but I would argue that it at most falls into the same category as flying an airplane. </p> <p> I would properly also state, that there are no silver bullets. But like you I feel that people understand it incorrectly and there is definetely no reason for making things harder than they are. I think the examples of technology that helps are excellent and exactly describe that things do move forward. </p> <p> That being said, it does not take away the creativity of the right decomposition, the responsibility for getting the use cases right, and especially the liability for getting it wrong. Sadly especially the last of overlooked. People should be reminded of where the phrase 'live under the bridge' comes from. </p> <p> To end my ramblins, I would also look a little into the future. As you know I am somewhat sceptial about machine learning and AI. However, looking at the recent break throughs and use cases in these areas, I would not be surprised of a future where software development is done by 'an AI' assemblying pre-defined 'entities' to create the software we need. Like an F16 cannot be flown without a computer, future software cannot be created by a human. </p> </div> <div class="comment-date">2019-07-04 18:29:00 UTC</div> </div> <div class="comment" id="756066e5cb0e42368ff9eeb9569fa47f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#756066e5cb0e42368ff9eeb9569fa47f">#</a></div> <div class="comment-content"> <p> Karsten, thank you for writing. I'm not inclined to agree that software development falls into the same category of complexity as flying a plane. It seems to me to be orders of magnitudes more complex. </p> <p> Just look at error rates. </p> <p> Would you ever board an air plane if flying had error rates similar to those observed in software development? Would you fly even if only one percent of all flights ended with plane crash? </p> <p> In reality, flying is extremely safe. Would you claim that software development is as safe, predictable, and manageable as flying? </p> <p> I see no evidence of that. </p> <p> Are pilots significantly more capable human beings than software developers, or does something else explain the discrepancy in failure rates? </p> </div> <div class="comment-date">2019-07-05 15:47 UTC</div> </div> <div class="comment" id="7e7e932f5eea47f3bab328c58e9d164b"> <div class="comment-author"><a href="http://blog.strobaek.org">Karsten Strøbæk</a> <a href="#7e7e932f5eea47f3bab328c58e9d164b">#</a></div> <div class="comment-content"> <p> Hi Mark. The fact that error rates are higher in software development is more a statement to the bad state our industry is in and has been for a milinium or more. </p> <p> Why do we except that we produce crappy systems or in your words software that is not safe, predictable, and manageble? The list of excuses is very long and the list of results is very short. We as an industry are simply doing it wrong, but most people prefers hand waving and marketing than simple and plausible heuristic. </p> <p> To use your analogy about planes I could ask if you would fly with a place that had (only) been unit tested? Properly not as it is never the unit that fails, but always the integration. Should be test all integrations then? Yes, why not? </p> <p> The used of planes or pilots (or whatever) may have been bad. My point was, that I do not see software development as complex. </p> </div> <div class="comment-date">2019-07-05 20:12 UTC</div> </div> <div class="comment" id="0df7412992fb499d915e6f4cdbb644a0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0df7412992fb499d915e6f4cdbb644a0">#</a></div> <div class="comment-content"> <p> Karsten, if we, as an industry, are doing it wrong, then why are we doing that? </p> <p> And what should we be doing instead? </p> </div> <div class="comment-date">2019-07-06 16:00 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Full binary tree catamorphism https://blog.ploeh.dk/2019/06/24/full-binary-tree-catamorphism 2019-06-24T06:00:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for a full binary tree is a pair of functions.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for a full <a href="https://en.wikipedia.org/wiki/Binary_tree">binary tree</a>, as well as how to identify it. The beginning of this article presents the catamorphism in C#, with examples. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <p> A <em>full binary tree</em> (also known as a <em>proper</em> or <em>plane</em> binary tree) is a tree in which each node has either two or no branches. </p> <p> <img src="/content/binary/full-binary-tree-example.png" alt="A full binary tree example diagram, with each node containing integers."> </p> <p> The diagram shows an example of a tree of integers. The left branch contains two children, of which the right branch again contains two sub-branches. The rest of the nodes are leaf-nodes with no sub-branches. </p> <h3 id="d6b9699fa3894a4383f9b2b2992a9e8f"> C# catamorphism <a href="#d6b9699fa3894a4383f9b2b2992a9e8f" title="permalink">#</a> </h3> <p> As a C# representation of a full binary tree, I'll start with the <code>IBinaryTree&lt;T&gt;</code> API from <a href="/2018/08/13/a-visitor-functor">A Visitor functor</a>. The catamorphism is the <code>Accept</code> method: </p> <p> <pre><span style="color:#2b91af;">TResult</span>&nbsp;Accept&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">IBinaryTreeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;visitor);</pre> </p> <p> So far in this article series, you've mostly seen <a href="/2018/05/22/church-encoding">Church-encoded</a> catamorphisms, so a catamorphism represented as a <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a> may be too big of a cognitive leap. We know, however, from <a href="/2018/06/25/visitor-as-a-sum-type">Visitor as a sum type</a> that a Visitor representation is isomorphic to a Church encoding. Since these are isomorphic, it's possible to refactor <code>IBinaryTree&lt;T&gt;</code> to a Church encoding. The <a href="https://github.com/ploeh/ChurchEncoding">GitHub repository</a> contains a series of commits that demonstrates how that refactoring works. Once you're done, you arrive at this <code>Match</code> method, which is the refactored <code>Accept</code> method: </p> <p> <pre><span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;node,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;leaf);</pre> </p> <p> This method takes a pair of functions as arguments. The <code>node</code> function deals with an internal node in the tree (the blue nodes in the above diagram), whereas the <code>leaf</code> function deals with the leaf nodes (the green nodes in the diagram). </p> <p> The <code>leaf</code> function may be the easiest one to understand. A leaf node only contains a value of the type <code>T</code>, so the only operation the function has to support is translating the <code>T</code> value to a <code>TResult</code> value. This is also the premise of the <code>Leaf</code> class' implementation of the method: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;item; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;node,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;leaf) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;leaf(item); }</pre> </p> <p> The <code>node</code> function is more tricky. It takes three input arguments, of the types <code>TResult</code>, <code>T</code>, and <code>TResult</code>. The roles of these are respectively <em>left</em>, <em>item</em>, and <em>right</em>. This is a typical representation of a binary node. Since there's always a left and a right branch, you put the node's value in the middle. As was the case with the <a href="/2019/06/10/tree-catamorphism">tree catamorphism</a>, the catamorphism function receives the branches as already-translated values; that is, both the left and right branch have already been translated to <code>TResult</code> when <code>node</code> is called. While it looks like magic, as always it's just the result of recursion: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;left; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;item; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;right; <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;node,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;leaf) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;node(left.Match(node,&nbsp;leaf),&nbsp;item,&nbsp;right.Match(node,&nbsp;leaf)); }</pre> </p> <p> This is the <code>Node&lt;T&gt;</code> class implementation of the <code>Match</code> method. It calls <code>node</code> and returns whatever it returns, but notice that as the <code>left</code> and <code>right</code> arguments, if first, recursively, calls <code>left.Match</code> and <code>right.Match</code>. This is how it can call <code>node</code> with the translated branches, as well as with the basic <code>item</code>. </p> <p> The recursion stops and unwinds on <code>left</code> and <code>right</code> whenever one of those are <code>Leaf</code> instances. </p> <h3 id="c64210d585c94cb78653b96380cbf0e6"> Examples <a href="#c64210d585c94cb78653b96380cbf0e6" title="permalink">#</a> </h3> <p> You can use <code>Match</code> to implement most other behaviour you'd like <code>IBinaryTree&lt;T&gt;</code> to have. In <a href="/2018/08/13/a-visitor-functor">the original article on the full binary tree functor</a> you saw how to implement <code>Select</code> with a Visitor, but now that the API is Church-encoded, you can derive <code>Select</code> from <code>Match</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;tree, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(tree&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(tree)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(selector)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node:&nbsp;(l,&nbsp;x,&nbsp;r)&nbsp;=&gt;&nbsp;Create(l,&nbsp;selector(x),&nbsp;r), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf:&nbsp;x&nbsp;=&gt;&nbsp;Leaf(selector(x))); }</pre> </p> <p> In the <code>leaf</code> case, the <code>Select</code> method simply calls <code>selector</code> with the <code>x</code> value it receives, and puts the resulting <code>TResult</code> object into a new <code>Leaf</code> object. </p> <p> In the <code>node</code> case, the lambda expression receives three arguments: <code>l</code> and <code>r</code> are the <em>already-translated</em> left and right branches, so you only need to call <code>selector</code> on <code>x</code> and call the <code>Create</code> helper method to produce a new <code>Node</code> object. </p> <p> You can also implement more specialised functionality, like calculating the sum of nodes, measuring the depth of the tree, and similar functions. You saw equivalent examples in the <a href="/2019/06/10/tree-catamorphism">previous article</a>. </p> <p> For the examples in this article, I'll use the tree shown in the above diagram. Using static helper methods, you can write it like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;tree&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(42), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1337, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(2112), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5040, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(1984))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(90125));</pre> </p> <p> To calculate the sum of all nodes, you can write a function like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Sum(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Match((l,&nbsp;x,&nbsp;r)&nbsp;=&gt;&nbsp;l&nbsp;+&nbsp;x&nbsp;+&nbsp;r,&nbsp;x&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> The <code>leaf</code> function just returns the value of the node, while the <code>node</code> function adds the numbers together. It works for the above <code>tree</code>: </p> <p> <pre>&gt; tree.Sum() 100642</pre> </p> <p> To find the maximum value, you can write another extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Max(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Match((l,&nbsp;x,&nbsp;r)&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Math</span>.Max(<span style="color:#2b91af;">Math</span>.Max(l,&nbsp;r),&nbsp;x),&nbsp;x&nbsp;=&gt;&nbsp;x); }</pre> </p> <p> Again, the <code>leaf</code> function just returns the value of the node. The <code>node</code> function receives the value of the current node <code>x</code>, as well as the already-found maximum value of the left branch and the right branch; it then returns the maximum of these three values: </p> <p> <pre>&gt; tree.Max() 90125</pre> </p> <p> As was also the case for trees, both of these operations are part of the standard repertoire available via a data structure's <em>fold</em>. That's not the case for the next two functions, which can't be implemented using a fold, but which can be defined with the catamorphism. The first is a function to count the leaves of a tree: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;CountLeaves&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Match((l,&nbsp;_,&nbsp;r)&nbsp;=&gt;&nbsp;l&nbsp;+&nbsp;r,&nbsp;_&nbsp;=&gt;&nbsp;1); }</pre> </p> <p> Since the <code>leaf</code> function handles a leaf node, the number of leaf nodes in a leaf node is, by definition, <em>one</em>. Thus, that function can ignore the value of the node and always return <code>1</code>. The <code>node</code> function, on the other hand, receives the number of leaf nodes on the left-hand side (<code>l</code>), the value of the current node, and the number of leaf nodes on the right-hand side (<code>r</code>). Notice that since an internal node is never a leaf node, it doesn't count; instead, just add <code>l</code> and <code>r</code> together. Notice that, again, the value of the node itself is irrelevant. </p> <p> How many leaf nodes does the above tree have? </p> <p> <pre>&gt; tree.CountLeaves() 4</pre> </p> <p> You can also measure the maximum depth of a tree: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;MeasureDepth&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Match((l,&nbsp;_,&nbsp;r)&nbsp;=&gt;&nbsp;1&nbsp;+&nbsp;<span style="color:#2b91af;">Math</span>.Max(l,&nbsp;r),&nbsp;_&nbsp;=&gt;&nbsp;0); }</pre> </p> <p> Like in the previous article, I've arbitrarily decided that the depth of a leaf node is <em>zero</em>; therefore, the <code>leaf</code> function always returns <code>0</code>. The <code>node</code> function receives the depth of the left and right branches, and returns the maximum of those two values, plus one, since the current node adds one level of depth. </p> <p> <pre>&gt; tree.MeasureDepth() 3</pre> </p> <p> You may not have much need for working with full binary trees in your normal, day-to-day C# work, but I found it worthwhile to include this example for a couple of reasons. First, because the original of the API shows that a catamorphism may be hiding in a Visitor. Second, because binary trees are interesting, in that they're foldable <a href="/2018/03/22/functors">functors</a>, but not monads. </p> <p> Where does the catamorphism come from, though? How can you trust that the <code>Match</code> method is the catamorphism? </p> <h3 id="d015bcc9afe742408d7c8ba6c6edce2a"> Binary tree F-Algebra <a href="#d015bcc9afe742408d7c8ba6c6edce2a" title="permalink">#</a> </h3> <p> As in the <a href="/2019/06/10/tree-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. </p> <p> As always, start with the underlying endofunctor. You can think of this one as a specialisation of the rose tree from the previous article: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;FullBinaryTreeF&nbsp;a&nbsp;c&nbsp;=&nbsp;LeafF&nbsp;a&nbsp;|&nbsp;NodeF&nbsp;c&nbsp;a&nbsp;c&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">FullBinaryTreeF</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(LeafF&nbsp;x)&nbsp;=&nbsp;LeafF&nbsp;x &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;(NodeF&nbsp;l&nbsp;x&nbsp;r)&nbsp;=&nbsp;NodeF&nbsp;(f&nbsp;l)&nbsp;x&nbsp;(f&nbsp;r)</pre> </p> <p> As usual, I've called the 'data' type <code>a</code> and the carrier type <code>c</code> (for <em>carrier</em>). The <code>Functor</code> instance as usual translates the carrier type; the <code>fmap</code> function has the type <code>(c -&gt; c1) -&gt; FullBinaryTreeF a c -&gt; FullBinaryTreeF a c1</code>. </p> <p> As was the case when deducing the recent catamorphisms, Haskell isn't too happy about defining instances for a type like <code>Fix (FullBinaryTreeF a)</code>. To address that problem, you can introduce a <code>newtype</code> wrapper: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;FullBinaryTreeFix&nbsp;a&nbsp;= &nbsp;&nbsp;FullBinaryTreeFix&nbsp;{&nbsp;unFullBinaryTreeFix&nbsp;::&nbsp;Fix&nbsp;(FullBinaryTreeF&nbsp;a)&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> You can define <code>Functor</code>, <code>Foldable</code>, and <code>Traversable</code> instances (but not <code>Monad</code>) for this type without resorting to any funky GHC extensions. Keep in mind that ultimately, the purpose of all this code is just to figure out what the catamorphism looks like. This code isn't intended for actual use. </p> <p> A pair of helper functions make it easier to define <code>FullBinaryTreeFix</code> values: </p> <p> <pre><span style="color:#2b91af;">fbtLeafF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;a fbtLeafF&nbsp;=&nbsp;FullBinaryTreeFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;LeafF <span style="color:#2b91af;">fbtNodeF</span>&nbsp;::&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;a fbtNodeF&nbsp;(FullBinaryTreeFix&nbsp;l)&nbsp;x&nbsp;(FullBinaryTreeFix&nbsp;r)&nbsp;=&nbsp;FullBinaryTreeFix&nbsp;$&nbsp;Fix&nbsp;$&nbsp;NodeF&nbsp;l&nbsp;x&nbsp;r</pre> </p> <p> In order to distinguish these helper functions from the ones that create <code>TreeFix a</code> values, I prefixed them with <code>fbt</code> (for <em>Full Binary Tree</em>). <code>fbtLeafF</code> creates a leaf node: </p> <p> <pre>Prelude Fix FullBinaryTree&gt; fbtLeafF "fnaah" FullBinaryTreeFix {unFullBinaryTreeFix = Fix (LeafF "fnaah")}</pre> </p> <p> <code>fbtNodeF</code> is a helper function to create an internal node: </p> <p> <pre>Prelude Fix FullBinaryTree&gt; fbtNodeF (fbtLeafF 1337) 42 (fbtLeafF 2112) FullBinaryTreeFix {unFullBinaryTreeFix = Fix (NodeF (Fix (LeafF 1337)) 42 (Fix (LeafF 2112)))}</pre> </p> <p> The <code>FullBinaryTreeFix</code> type, or rather the underlying <code>FullBinaryTreeF a</code> functor, is all you need to identify the catamorphism. </p> <h3 id="ced0da7dc61943b0be872ec79b4e3651"> Haskell catamorphism <a href="#ced0da7dc61943b0be872ec79b4e3651" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>FullBinaryTreeF a</code>), and an object <code>c</code>, but you still need to find a morphism <code>FullBinaryTreeF a c -&gt; c</code>. Notice that the algebra you have to find is the function that reduces the functor to its <em>carrier type</em> <code>c</code>, not the 'data type' <code>a</code>. This takes some time to get used to, but that's how catamorphisms work. This doesn't mean, however, that you get to ignore <code>a</code>, as you'll see. </p> <p> As in the previous articles, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>fullBinaryTreeF&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unFullBinaryTreeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(LeafF&nbsp;x)&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(NodeF&nbsp;l&nbsp;x&nbsp;r)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementation of <code>alg</code>, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from <code>alg</code>? You could pass a function argument to the <code>fullBinaryTreeF</code> function and use it with <code>x</code>: </p> <p> <pre>fullBinaryTreeF&nbsp;fl&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unFullBinaryTreeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(LeafF&nbsp;x)&nbsp;=&nbsp;fl&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(NodeF&nbsp;l&nbsp;x&nbsp;r)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> I called the function <code>fl</code> for <em>function, leaf</em>, because we're also going to need a function for the <code>NodeF</code> case: </p> <p> <pre><span style="color:#2b91af;">fullBinaryTreeF</span>&nbsp;::&nbsp;(c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c fullBinaryTreeF&nbsp;fn&nbsp;fl&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unFullBinaryTreeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(LeafF&nbsp;x)&nbsp;=&nbsp;fl&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(NodeF&nbsp;l&nbsp;x&nbsp;r)&nbsp;=&nbsp;fn&nbsp;l&nbsp;x&nbsp;r</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>FullBinaryTreeF</code>, the compiler infers that the <code>alg</code> function has the type <code>FullBinaryTreeF a c -&gt; c</code>, which is just what you need! </p> <p> You can now see what the carrier type <code>c</code> is for. It's the type that the algebra extracts, and thus the type that the catamorphism returns. </p> <p> This, then, is the catamorphism for a full binary tree. As always, it's not the only possible catamorphism, since you can easily reorder the arguments to both <code>fullBinaryTreeF</code>, <code>fn</code>, and <code>fl</code>. These would all be isomorphic, though. </p> <h3 id="3f87d49db58f4cd59dec76a97d31c0d2"> Basis <a href="#3f87d49db58f4cd59dec76a97d31c0d2" title="permalink">#</a> </h3> <p> You can implement most other useful functionality with <code>treeF</code>. Here's the <code>Functor</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;=&nbsp;fullBinaryTreeF&nbsp;(\l&nbsp;x&nbsp;r&nbsp;-&gt;&nbsp;fbtNodeF&nbsp;l&nbsp;(f&nbsp;x)&nbsp;r)&nbsp;(fbtLeafF&nbsp;.&nbsp;f)</pre> </p> <p> The <code>fl</code> function first invokes <code>f</code>, followed by <code>fbtLeafF</code>. The <code>fn</code> function uses the <code>fbtNodeF</code> helper function to create a new internal node. <code>l</code> and <code>r</code> are already-translated branches, so you just need to call <code>f</code> with the node value <code>x</code>. </p> <p> There's no <code>Monad</code> instance for binary trees, because you can't flatten a binary tree of binary trees. You can, on the other hand, define a <code>Foldable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;foldMap&nbsp;f&nbsp;=&nbsp;fullBinaryTreeF&nbsp;(\l&nbsp;x&nbsp;r&nbsp;-&gt;&nbsp;l&nbsp;&lt;&gt;&nbsp;f&nbsp;x&nbsp;&lt;&gt;&nbsp;r)&nbsp;f</pre> </p> <p> The <code>f</code> function passed to <code>foldMap</code> has the type <code>Monoid m =&gt; (a -&gt; m)</code>, so the <code>fl</code> function that handles leaf nodes simply calls <code>f</code> with the contents of the node. The <code>fn</code> function receives two branches already translated to <code>m</code>, so it just has to call <code>f</code> with <code>x</code> and combine all the <code>m</code> values using the <code>&lt;&gt;</code> operator. </p> <p> The <code>Traversable</code> instance follows right on the heels of <code>Foldable</code>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;fullBinaryTreeF&nbsp;(liftA3&nbsp;fbtNodeF)&nbsp;(<span style="color:blue;">fmap</span>&nbsp;fbtLeafF)</pre> </p> <p> There are operations on binary trees that you can implement with a fold, but some that you can't. Consider the tree shown in the diagram at the beginning of the article. This is also the tree that the above C# examples use. In Haskell, using <code>FullBinaryTreeFix</code>, you can define that tree like this: </p> <p> <pre>tree&nbsp;=&nbsp; &nbsp;&nbsp;fbtNodeF &nbsp;&nbsp;&nbsp;&nbsp;(fbtNodeF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(fbtLeafF&nbsp;42) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1337 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(fbtNodeF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(fbtLeafF&nbsp;2112) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5040 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(fbtLeafF&nbsp;1984))) &nbsp;&nbsp;&nbsp;&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;(fbtLeafF&nbsp;90125)</pre> </p> <p> Since <code>FullBinaryTreeFix</code> is <code>Foldable</code>, and that type class already comes with <code>sum</code> and <code>maximum</code> functions, no further work is required to repeat the first two of the above C# examples: </p> <p> <pre>Prelude Fix FullBinaryTree&gt; sum tree 100642 Prelude Fix FullBinaryTree&gt; maximum tree 90125</pre> </p> <p> Counting leaves, or measuring the depth of a tree, on the other hand, is impossible with the <code>Foldable</code> instance, but can be implemented using the catamorphism: </p> <p> <pre><span style="color:#2b91af;">countLeaves</span>&nbsp;::&nbsp;<span style="color:blue;">Num</span>&nbsp;n&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;n countLeaves&nbsp;=&nbsp;fullBinaryTreeF&nbsp;(\l&nbsp;_&nbsp;r&nbsp;-&gt;&nbsp;l&nbsp;+&nbsp;r)&nbsp;(<span style="color:blue;">const</span>&nbsp;1) <span style="color:#2b91af;">treeDepth</span>&nbsp;::&nbsp;(<span style="color:blue;">Ord</span>&nbsp;n,&nbsp;<span style="color:blue;">Num</span>&nbsp;n)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">FullBinaryTreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;n treeDepth&nbsp;=&nbsp;fullBinaryTreeF&nbsp;(\l&nbsp;_&nbsp;r&nbsp;-&gt;&nbsp;1&nbsp;+&nbsp;<span style="color:blue;">max</span>&nbsp;l&nbsp;r)&nbsp;(<span style="color:blue;">const</span>&nbsp;0)</pre> </p> <p> The reasoning is the same as already explained in the above C# examples. The functions also produce the same results: </p> <p> <pre>Prelude Fix FullBinaryTree&gt; countLeaves tree 4 Prelude Fix FullBinaryTree&gt; treeDepth tree 3</pre> </p> <p> This, hopefully, illustrates that the catamorphism is more capable, and that the fold is just a (list-biased) specialisation. </p> <h3 id="81b3e77b6fbe4760bc8c74805e4edba8"> Summary <a href="#81b3e77b6fbe4760bc8c74805e4edba8" title="permalink">#</a> </h3> <p> The catamorphism for a full binary tree is a pair of functions. One function handles internal nodes, while the other function handles leaf nodes. </p> <p> I thought it was interesting to show this example for two reasons: First, the original example was a Visitor implementation, and I think it's worth realising that a Visitor's <code>Accept</code> method can also be viewed as a catamorphism. Second, a binary tree is an example of a data structure that has a fold, but isn't a monad. </p> <p> All articles in the article series have, so far, covered data structures well-known from computer science. The next example will, on the other hand, demonstrate that even completely ad-hoc domain-specific data structures have catamorphisms. </p> <p> <strong>Next:</strong> <a href="/2019/07/08/payment-types-catamorphism">Payment types catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Composition Root location https://blog.ploeh.dk/2019/06/17/composition-root-location 2019-06-17T05:55:00+00:00 Mark Seemann <div id="post"> <p> <em>A Composition Root should be located near the point where user code first executes.</em> </p> <p> Prompted by a recent Internet discussion, my <a href="/dippp">DIPPP</a> co-author <a href="https://blogs.cuttingedge.it/steven/">Steven van Deursen</a> wrote to me in order to help clarify the <a href="/2011/07/28/CompositionRoot">Composition Root</a> pattern. </p> <p> In the email, Steven ponders whether it's defensible to use an API that <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">looks like a Service Locator</a> from within a unit test. He specifically calls out my article that describes the <a href="/2013/03/11/auto-mocking-container">Auto-mocking Container design pattern</a>. </p> <p> In that article, I show how to use Castle Windsor's <code>Resolve</code> method from within a unit test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsController() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> container = <span style="color: blue;">new</span> <span style="color: #2b91af;">WindsorContainer</span>().Install(<span style="color: blue;">new</span> <span style="color: #2b91af;">ShopFixture</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = container.Resolve&lt;<span style="color: #2b91af;">BasketController</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IHttpController</span>&gt;(sut); }</pre> </p> <p> Is the test using a <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator</a>? If so, why is that okay? If not, why isn't it a Service Locator? </p> <p> This article argues that that this use of <code>Resolve</code> isn't a Service Locator. </p> <h3 id="e9a6c124fa1d4610ae57b3cba83254b0"> Entry points defined <a href="#e9a6c124fa1d4610ae57b3cba83254b0" title="permalink">#</a> </h3> <p> The <a href="/2011/07/28/CompositionRoot">original article about the Composition Root pattern</a> defines a Composition Root as the place where you compose your object graph(s). It repeatedly describes how this ought to happen in, or as close as possible to, the application's entry point. I believe that this definition is compatible with the pattern description given in <a href="/dippp">our book</a>. </p> <p> I do realise, however, that we may never have explicitly defined what an <em>entry point</em> is. </p> <p> In order to do so, it may be helpful to establish a bit of terminology. In the following, I'll use the terms <em>user code</em> as opposed to <em>framework code</em>. </p> <p> Much of the code you write probably runs within some sort of framework. If you're writing a web application, you're probably using a web framework. If you're writing a message-based application, you might be using some message bus, or actor, framework. If you're writing an app for a mobile device, you're probably using some sort of framework for that, too. </p> <p> Even as a programmer, you're a <em>user</em> of frameworks. </p> <p> As I usually do, I'll use <a href="http://tomasp.net">Tomas Petricek</a>'s distinction between <a href="http://tomasp.net/blog/2015/library-frameworks">libraries and frameworks</a>. A library is a collection of APIs that you can call. A framework is a software system that calls your code. </p> <p> <img src="/content/binary/user-code-in-framework.png" alt="User code running in a framework."> </p> <p> The reality is often more complex, as illustrated by the figure. While a framework will call your code, you can also invoke APIs afforded by the framework. </p> <p> The point, however, is that <em>user code</em> is code that you write, while <em>framework code</em> is code that someone else wrote to develop the framework. The framework starts up first, and at some point in its lifetime, it calls your code. </p> <p class="text-center"> <strong>Definition:</strong> The <em>entry point</em> is the user code that the framework calls first. </p> <p> As an example, in ASP.NET Core, the (conventional) <code>Startup</code> class is the first user code that the framework calls. (If you follow Tomas Petricek's definition to the letter, ASP.NET Core isn't a framework, but a library, because you have to write a <code>Main</code> method and call <code>WebHost.CreateDefaultBuilder(args).UseStartup&lt;Startup&gt;().Build().Run()</code>. In reality, though, you're supposed to configure the application from your <code>Startup</code> class, making it the <em>de facto</em> entry point.) </p> <h3 id="61e3f212e0e244f18ac998f4b9fbb635"> Unit testing endpoints <a href="#61e3f212e0e244f18ac998f4b9fbb635" title="permalink">#</a> </h3> <p> Most .NET-based unit testing packages are frameworks. There's typically little explicit configuration. Instead, you just write a method and adorn it with an attribute: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;ReservationSucceeds() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;repo&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeReservationsRepository</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(10,&nbsp;repo); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservation&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(2018,&nbsp;8,&nbsp;13,&nbsp;16,&nbsp;53,&nbsp;0,&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(2)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email:&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity:&nbsp;4); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(repo.Contains(reservation.Accept())); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expectedId&nbsp;=&nbsp;repo.GetId(reservation.Accept()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ok&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkActionResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expectedId,&nbsp;ok.Value); } [<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;ReservationFails() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;repo&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FakeReservationsRepository</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(10,&nbsp;repo); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservation&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(2018,&nbsp;8,&nbsp;13,&nbsp;16,&nbsp;53,&nbsp;0,&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(2)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email:&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quantity:&nbsp;11); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Post(reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(reservation.IsAccepted); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(repo.Contains(reservation)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">InternalServerErrorActionResult</span>&gt;(actual); }</pre> </p> <p> With <a href="https://xunit.net">xUnit.net</a>, the attribute is called <code>[Fact]</code>, but the principle is the same in <a href="https://nunit.org">NUnit</a> and MSTest, only that names are different. </p> <p> Where's the entry point? </p> <p> Each test is it's own entry point. The test is (typically) the first user code that the test runner executes. Furthermore, each test runs independently of any other. </p> <p> For the sake of argument, you could write each test case in a new application, and run all your test applications in parallel. It would be impractical, but it oughtn't change the way you organise the tests. Each test method is, conceptually, a mini-application. </p> <p> A test method is its own Composition Root; or, more generally, each test has its own Composition Root. In fact, xUnit.net has various extensibility points that enable you to hook into the framework before each test method executes. You can, for example, <a href="/2010/10/08/AutoDataTheorieswithAutoFixture">combine a <code>[Theory]</code> attribute with a custom <code>AutoDataAttribute</code></a>, or you can adorn your tests with a <code>BeforeAfterTestAttribute</code>. This doesn't change that the test runner will run each test case independently of all the other tests. Those pre-execution hooks play the same role as middleware in real applications. </p> <p> You can, therefore, consider the <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">Arrange phase</a> the Composition Root for each test. </p> <p> Thus, I don't consider the use of an Auto-mocking Container to be a Service Locator, since <a href="/2011/08/25/ServiceLocatorrolesvs.mechanics">its role is to resolve object graphs at the entry point instead of locating services from arbitrary locations in the code base</a>. </p> <h3 id="200be4483e4b4369abe5912b2a8213c3"> Summary <a href="#200be4483e4b4369abe5912b2a8213c3" title="permalink">#</a> </h3> <p> A Composition Root is located at, or near, the <em>entry point</em>. An entry point is where <em>user code</em> is first executed by a framework. Each unit test method constitutes a separate, independent entry point. Therefore, it's consistent with these definitions to use an Auto-mocking Container in a unit test. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tree catamorphism https://blog.ploeh.dk/2019/06/10/tree-catamorphism 2019-06-10T09:10:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for a tree is just a single function with a particular type.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for a <a href="https://en.wikipedia.org/wiki/Tree_(data_structure)">tree</a>, as well as how to identify it. The beginning of this article presents the catamorphism in C#, with examples. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <p> A tree is a general-purpose data structure where each node in a tree has an associated value. Each node can have an arbitrary number of branches, including none. </p> <p> <img src="/content/binary/tree-example.png" alt="A tree example diagram, with each node containing integers."> </p> <p> The diagram shows an example of a tree of integers. The left branch contains a sub-tree with only a single branch, whereas the right branch contains a sub-tree with three branches. Each of the leaf nodes are trees in their own right, but they all have zero branches. </p> <p> In this example, each branch at the 'same level' has the same depth, but this isn't required. </p> <h3 id="7d3f657d0c6b443f83eac89370e0c660"> C# catamorphism <a href="#7d3f657d0c6b443f83eac89370e0c660" title="permalink">#</a> </h3> <p> As a C# representation of a tree, I'll use the <code>Tree&lt;T&gt;</code> class from <a href="/2018/08/06/a-tree-functor">A Tree functor</a>. The catamorphism is this instance method on <code>Tree&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Cata&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;func) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;func(Item,&nbsp;children.Select(c&nbsp;=&gt;&nbsp;c.Cata(func)).ToArray()); }</pre> </p> <p> Contrary to previous articles, I didn't call this method <code>Match</code>, but simply <code>Cata</code> (for <em>catamorphism</em>). The reason is that those other methods are called <code>Match</code> for a particular reason. The data structures for which they are catamorphisms are all <a href="/2018/05/22/church-encoding">Church-encoded</a> <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a>. For those types, the <code>Match</code> methods enable a syntax similar to pattern matching in <a href="https://fsharp.org">F#</a>. That's not the case for <code>Tree&lt;T&gt;</code>. It's not a sum type, and it isn't Church-encoded. </p> <p> The method takes a single function as an input argument. This is the first catamorphism in this article series that isn't made up of a pair of some sort. The <a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a> is a pair of values, the <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a> is a pair made up of a value and a function, and the <a href="/2019/06/03/either-catamorphism">Either catamorphism</a> is a pair of functions. The tree catamorphism, in contrast, is just a single function. </p> <p> The first argument to the function is a value of the type <code>T</code>. This will be an <code>Item</code> value. The second argument to the function is a finite collection of <code>TResult</code> values. This may take a little time getting used to, but it's a collection of already reduced sub-trees. When you supply such a function to <code>Cata</code>, that function must return a single value of the type <code>TResult</code>. Thus, the function must be able to digest a finite collection of <code>TResult</code> values, as well as a <code>T</code> value, to a single <code>TResult</code> value. </p> <p> The <code>Cata</code> method accomplishes this by calling <code>func</code> with the current <code>Item</code>, as well as by recursively applying itself to each of the sub-trees. Eventually, <code>Cata</code> will recurse into leaf nodes, which means that <code>children</code> will be empty. When that happens, the lambda expression inside <code>children.Select</code> never runs, and recursion stops and unwinds. </p> <h3 id="167ba023ee654db39fb5eb448d35a8df"> Examples <a href="#167ba023ee654db39fb5eb448d35a8df" title="permalink">#</a> </h3> <p> You can use <code>Cata</code> to implement most other behaviour you'd like <code>Tree&lt;T&gt;</code> to have. In <a href="/2018/08/06/a-tree-functor">the original article on the Tree functor</a> you saw an ad-hoc implementation of <code>Select</code>, but instead, you can derive <code>Select</code> from <code>Cata</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Cata&lt;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;((x,&nbsp;nodes)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(selector(x),&nbsp;nodes)); }</pre> </p> <p> The lambda expression receives <code>x</code>, an object of the type <code>T</code>, as well as <code>nodes</code>, which is a finite collection of already translated sub-trees. It simply translates <code>x</code> with <code>selector</code> and returns a <code>new Tree&lt;TResult&gt;</code> with the translated value and the already translated <code>nodes</code>. </p> <p> This works just as well as the ad-hoc implementation; it passes all the same tests as shown in the previous article. </p> <p> If you have a tree of numbers, you can add them all together: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Sum(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Cata&lt;<span style="color:blue;">int</span>&gt;((x,&nbsp;xs)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;xs.Sum()); }</pre> </p> <p> This uses the built-in <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.sum">Sum method</a> for <code>IEnumerable&lt;T&gt;</code> to add all the partly calculated sub-trees together, and then adds the value of the current node. In this and remaining examples, I'll use the tree shown in the above diagram: </p> <p> <pre><span style="color:#2b91af;">Tree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(42, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(1337, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(-3)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(7, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(-99), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(100), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(0)));</pre> </p> <p> You can now calculate the sum of all these nodes: </p> <p> <pre>&gt; tree.Sum() 1384</pre> </p> <p> Another option is to find the maximum value anywhere in a tree: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Max(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Cata&lt;<span style="color:blue;">int</span>&gt;((x,&nbsp;xs)&nbsp;=&gt;&nbsp;xs.Any()&nbsp;?&nbsp;<span style="color:#2b91af;">Math</span>.Max(x,&nbsp;xs.Max())&nbsp;:&nbsp;x); }</pre> </p> <p> This method again utilises one of the LINQ methods available via the .NET base class library: <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.max">Max</a>. It is, however, necessary to first check whether the partially reduced <code>xs</code> is empty or not, because the <code>Max</code> extension method on <code>IEnumerable&lt;int&gt;</code> doesn't know how to deal with an empty collection (it throws an exception). When <code>xs</code> is empty that implies a leaf node, in which case you can simply return <code>x</code>; otherwise, you'll first have to use the <code>Max</code> method on <code>xs</code> to find the maximum value there, and then use <code>Math.Max</code> to find the maximum of those two. (I'll here remind the attentive reader that finding the maximum number forms a <a href="/2017/11/27/semigroups">semigroup</a> and that <a href="/2017/12/11/semigroups-accumulate">semigroups accumulate</a> when collections are non-empty. It all fits together. Isn't maths lovely?) </p> <p> Using the same <code>tree</code> as before, you can see that this method, too, works as expected: </p> <p> <pre>&gt; tree.Max() 1337</pre> </p> <p> So far, these two extension methods are just specialised <em>folds</em>. In Haskell, <code>Foldable</code> is a specific type class, and <code>sum</code> and <code>max</code> are available for all instances. As promised in <a href="/2019/04/29/catamorphisms">the introduction to the series</a>, though, there are some functions on trees that you can't implement using a fold. One of these is to count all the leaf nodes. You can still derive that functionality from the catamorphism, though: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;CountLeaves() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Cata&lt;<span style="color:blue;">int</span>&gt;((x,&nbsp;xs)&nbsp;=&gt;&nbsp;xs.Any()&nbsp;?&nbsp;xs.Sum()&nbsp;:&nbsp;1); }</pre> </p> <p> Like <code>Max</code>, the lambda expression used to implement <code>CountLeaves</code> uses <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.any">Any</a> to detect whether or not <code>xs</code> is empty, which is when <code>Any</code> is <code>false</code>. Empty <code>xs</code> indicates that you've found a leaf node, so return <code>1</code>. When <code>xs</code> isn't empty, it contains a collection of <code>1</code> values - one for each leaf node recursively found; add them together with <code>Sum</code>. </p> <p> This also works for the same <code>tree</code> as before: </p> <p> <pre>&gt; tree.CountLeaves() 4</pre> </p> <p> You can also measure the maximum depth of a tree: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;MeasureDepth() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Cata&lt;<span style="color:blue;">int</span>&gt;((x,&nbsp;xs)&nbsp;=&gt;&nbsp;xs.Any()&nbsp;?&nbsp;1&nbsp;+&nbsp;xs.Max()&nbsp;:&nbsp;0); }</pre> </p> <p> This implementation considers a leaf node to have no depth: </p> <p> <pre>&gt; <span style="color:#2b91af;">Tree</span>.Leaf(<span style="color:#a31515;">&quot;foo&quot;</span>).MeasureDepth() 0</pre> </p> <p> This is a discretionary definition; you could also argue that, by definition, a leaf node ought to have a depth of one. If you think so, you'll need to change the <code>0</code> to <code>1</code> in the above <code>MeasureDepth</code> implementation. </p> <p> Once more, you can use <code>Any</code> to detect leaf nodes. Whenever you find a leaf node, you return its depth, which, by definition, is <code>0</code>. Otherwise, you find the maximum depth already found among <code>xs</code>, and add <code>1</code>, because <code>xs</code> contains the maximum depths of all immediate sub-trees. </p> <p> Using the same <code>tree</code> again: </p> <p> <pre>&gt; tree.MeasureDepth() 2</pre> </p> <p> The above <code>tree</code> has the same depth for all sub-trees, so here's an example of a tilted tree: </p> <p> <pre>&gt; <span style="color:#2b91af;">Tree</span>.Create(3, . <span style="color:#2b91af;">Tree</span>.Create(1, . <span style="color:#2b91af;">Tree</span>.Leaf(0), . <span style="color:#2b91af;">Tree</span>.Leaf(0)), . <span style="color:#2b91af;">Tree</span>.Leaf(0), . <span style="color:#2b91af;">Tree</span>.Leaf(0), . <span style="color:#2b91af;">Tree</span>.Create(2, . <span style="color:#2b91af;">Tree</span>.Create(1, . <span style="color:#2b91af;">Tree</span>.Leaf(0)))) . .MeasureDepth() 3</pre> </p> <p> To make it easier to understand, I've labelled all the leaf nodes with <code>0</code>, because that's their depth. I've then labelled the other nodes with the maximum number 'under' them, plus one. That's the algorithm used. </p> <h3 id="82e5042d05534db2a67c8f7c37f78419"> Tree F-Algebra <a href="#82e5042d05534db2a67c8f7c37f78419" title="permalink">#</a> </h3> <p> As in the <a href="/2019/06/03/either-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. </p> <p> As always, start with the underlying endofunctor. I've taken some inspiration from <code>Tree a</code> from <code>Data.Tree</code>, but changed some names: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;TreeF&nbsp;a&nbsp;c&nbsp;=&nbsp;NodeF&nbsp;{&nbsp;nodeValue&nbsp;::&nbsp;a,&nbsp;nodes&nbsp;::&nbsp;ListFix&nbsp;c&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">TreeF</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;(NodeF&nbsp;x&nbsp;ns)&nbsp;=&nbsp;NodeF&nbsp;x&nbsp;$&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;ns</pre> </p> <p> Instead of using Haskell's standard list (<code>[]</code>) for the sub-forest, I've used <code>ListFix</code> from <a href="/2019/05/27/list-catamorphism">the article on list catamorphism</a>. This should, hopefully, demonstrate how you can build on already established definitions derived from first principles. </p> <p> As usual, I've called the 'data' type <code>a</code> and the carrier type <code>c</code> (for <em>carrier</em>). The <code>Functor</code> instance as usual translates the carrier type; the <code>fmap</code> function has the type <code>(c -&gt; c1) -&gt; TreeF a c -&gt; TreeF a c1</code>. </p> <p> As was the case when deducing the recent catamorphisms, Haskell isn't too happy about defining instances for a type like <code>Fix (TreeF a)</code>. To address that problem, you can introduce a <code>newtype</code> wrapper: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;TreeFix&nbsp;a&nbsp;=&nbsp;TreeFix&nbsp;{&nbsp;unTreeFix&nbsp;::&nbsp;Fix&nbsp;(TreeF&nbsp;a)&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> You can define <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, etc. instances for this type without resorting to any funky GHC extensions. Keep in mind that ultimately, the purpose of all this code is just to figure out what the catamorphism looks like. This code isn't intended for actual use. </p> <p> A pair of helper functions make it easier to define <code>TreeFix</code> values: </p> <p> <pre><span style="color:#2b91af;">leafF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;a leafF&nbsp;x&nbsp;=&nbsp;TreeFix&nbsp;$&nbsp;Fix&nbsp;$&nbsp;NodeF&nbsp;x&nbsp;nilF <span style="color:#2b91af;">nodeF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;(<span style="color:blue;">TreeFix</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;a nodeF&nbsp;x&nbsp;=&nbsp;TreeFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;NodeF&nbsp;x&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;unTreeFix</pre> </p> <p> <code>leafF</code> creates a leaf node: </p> <p> <pre>Prelude Fix List Tree&gt; leafF "ploeh" TreeFix {unTreeFix = Fix (NodeF "ploeh" (ListFix (Fix NilF)))}</pre> </p> <p> <code>nodeF</code> is a helper function to create a non-leaf node: </p> <p> <pre>Prelude Fix List Tree&gt; nodeF 4 (consF (leafF 9) nilF) TreeFix {unTreeFix = Fix (NodeF 4 (ListFix (Fix (ConsF (Fix (NodeF 9 (ListFix (Fix NilF)))) (Fix NilF)))))}</pre> </p> <p> Even with helper functions, construction of <code>TreeFix</code> values is cumbersome, but keep in mind that the code shown here isn't meant to be used in practice. The goal is only to deduce catamorphisms from more basic universal abstractions, and you now have all you need to do that. </p> <h3 id="ca5669298d814809a3f0d4b0422b860f"> Haskell catamorphism <a href="#ca5669298d814809a3f0d4b0422b860f" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>TreeF a</code>), and an object <code>c</code>, but you still need to find a morphism <code>TreeF a c -&gt; c</code>. Notice that the algebra you have to find is the function that reduces the functor to its <em>carrier type</em> <code>c</code>, not the 'data type' <code>a</code>. This takes some time to get used to, but that's how catamorphisms work. This doesn't mean, however, that you get to ignore <code>a</code>, as you'll see. </p> <p> As in the previous articles, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>treeF&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unTreeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(NodeF&nbsp;x&nbsp;ns)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementation of <code>alg</code>, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from <code>alg</code>? You could pass a function argument to the <code>treeF</code> function and use it with <code>x</code> and <code>ns</code>: </p> <p> <pre><span style="color:#2b91af;">treeF</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c treeF&nbsp;f&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unTreeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;(NodeF&nbsp;x&nbsp;ns)&nbsp;=&nbsp;f&nbsp;x&nbsp;ns</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>TreeF</code>, the compiler infers that the <code>alg</code> function has the type <code>TreeF a c -&gt; c</code>, which is just what you need! </p> <p> You can now see what the carrier type <code>c</code> is for. It's the type that the algebra extracts, and thus the type that the catamorphism returns. </p> <p> This, then, is the catamorphism for a tree. So far in this article series, all previous catamorphisms have been pairs, but this one is just a single function. It's still not the only possible catamorphism, since you could trivially flip the arguments to <code>f</code>. </p> <p> I've chosen the representation shown here because it's isomorphic to the <code>foldTree</code> function from Haskell's built-in <code>Data.Tree</code> module, which explicitly documents that the function "is also known as the catamorphism on trees." <code>foldTree</code> is defined using Haskell's standard list type (<code>[]</code>), so the type is simpler: <code>(a -&gt; [b] -&gt; b) -&gt; Tree a -&gt; b</code>. The two representations of trees, <code>TreeFix</code> and <code>Tree</code> are, however, isomorphic, so <code>foldTree</code> is equivalent to <code>treeF</code>. Notice how both of these functions are also equivalent to the above C# <code>Cata</code> method. </p> <h3 id="8647c7bd03aa4d4b8a01a8252058830f"> Basis <a href="#8647c7bd03aa4d4b8a01a8252058830f" title="permalink">#</a> </h3> <p> You can implement most other useful functionality with <code>treeF</code>. Here's the <code>Functor</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;=&nbsp;treeF&nbsp;(nodeF&nbsp;.&nbsp;f)</pre> </p> <p> <code>nodeF . f</code> is just the <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free</a> version of <code>\x ns -&gt; nodeF (f x) ns</code>, which follows the exact same implementation logic as the above C# <code>Select</code> implementation. </p> <p> The <code>Applicative</code> instance is, I'm afraid, the most complex code you've seen so far in this article series: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;=&nbsp;leafF &nbsp;&nbsp;ft&nbsp;&lt;*&gt;&nbsp;xt&nbsp;=&nbsp;treeF&nbsp;(\f&nbsp;ts&nbsp;-&gt;&nbsp;addNodes&nbsp;ts&nbsp;$&nbsp;f&nbsp;&lt;$&gt;&nbsp;xt)&nbsp;ft &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;addNodes&nbsp;ns&nbsp;(TreeFix&nbsp;(Fix&nbsp;(NodeF&nbsp;x&nbsp;xs)))&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeFix&nbsp;(Fix&nbsp;(NodeF&nbsp;x&nbsp;(xs&nbsp;&lt;&gt;&nbsp;(unTreeFix&nbsp;&lt;$&gt;&nbsp;ns))))</pre> </p> <p> I'd be surprised if it's impossible to make this terser, but I thought that it was just complicated enough that I needed to make one of the steps explicit. The <code>addNodes</code> helper function has the type <code>ListFix (TreeFix a) -&gt; TreeFix a -&gt; TreeFix a</code>, and it adds a list of sub-trees to the top node of a tree. It looks worse than it is, but it really just peels off the wrappers (<code>TreeFix</code>, <code>Fix</code>, and <code>NodeF</code>) to access the data (<code>x</code> and <code>xs</code>) of the top node. It then concatenates <code>xs</code> with <code>ns</code>, and puts all the wrappers back on. </p> <p> I have to admit, though, that the <code>Applicative</code> and <code>Monad</code> instance in general are mind-binding to me. The <code>&lt;*&gt;</code> operation, particularly, takes a <em>tree of functions</em> and has to combine it with a <em>tree of values</em>. What does that even mean? How does one do that? </p> <p> Like the above, apparently. I took the <code>Applicative</code> behaviour from <code>Data.Tree</code> and made sure that my implementation is isomorphic. I even have a property to make 'sure' that's the case: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Applicative&nbsp;behaves&nbsp;like&nbsp;Data.Tree&quot;</span>&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:#2b91af;">xt</span>&nbsp;::&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;<span style="color:#2b91af;">Integer</span>&nbsp;&lt;-&nbsp;fromTree&nbsp;&lt;$&gt;&nbsp;resize&nbsp;10&nbsp;arbitrary &nbsp;&nbsp;<span style="color:#2b91af;">ft</span>&nbsp;::&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;(<span style="color:#2b91af;">Integer</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span>)&nbsp;&lt;-&nbsp;fromTree&nbsp;&lt;$&gt;&nbsp;resize&nbsp;5&nbsp;arbitrary &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;ft&nbsp;&lt;*&gt;&nbsp;xt &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;toTree&nbsp;ft&nbsp;&lt;*&gt;&nbsp;toTree&nbsp;xt &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;===&nbsp;toTree&nbsp;actual</pre> </p> <p> The <code>Monad</code> instance looks similar to the <code>Applicative</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;t&nbsp;&gt;&gt;=&nbsp;f&nbsp;=&nbsp;treeF&nbsp;(\x&nbsp;ns&nbsp;-&gt;&nbsp;addNodes&nbsp;ns&nbsp;$&nbsp;f&nbsp;x)&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;addNodes&nbsp;ns&nbsp;(TreeFix&nbsp;(Fix&nbsp;(NodeF&nbsp;x&nbsp;xs)))&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeFix&nbsp;(Fix&nbsp;(NodeF&nbsp;x&nbsp;(xs&nbsp;&lt;&gt;&nbsp;(unTreeFix&nbsp;&lt;$&gt;&nbsp;ns))))</pre> </p> <p> The <code>addNodes</code> helper function is the same as for <code>&lt;*&gt;</code>, so you may wonder why I didn't extract that as a separate, reusable function. I decided, however, to apply the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">rule of three</a>, and since, ultimately, <code>addNodes</code> appear only twice, I left them as the implementation details they are. </p> <p> Fortunately, the <code>Foldable</code> instance is easier on the eyes: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;foldMap&nbsp;f&nbsp;=&nbsp;treeF&nbsp;(\x&nbsp;xs&nbsp;-&gt;&nbsp;f&nbsp;x&nbsp;&lt;&gt;&nbsp;fold&nbsp;xs)</pre> </p> <p> Since <code>f</code> is a function of the type <code>a -&gt; m</code>, where <code>m</code> is a <code>Monoid</code> instance, you can use <code>fold</code> and <code>&lt;&gt;</code> to accumulate everything to a single <code>m</code> value. </p> <p> The <code>Traversable</code> instance is similarly terse: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;treeF&nbsp;(\x&nbsp;ns&nbsp;-&gt;&nbsp;nodeF&nbsp;&lt;$&gt;&nbsp;x&nbsp;&lt;*&gt;&nbsp;sequenceA&nbsp;ns)</pre> </p> <p> Finally, you can implement conversions to and from the <code>Tree</code> type from <code>Data.Tree</code>, using <code>ana</code> as the dual of <code>cata</code>: </p> <p> <pre><span style="color:#2b91af;">toTree</span>&nbsp;::&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;a toTree&nbsp;=&nbsp;treeF&nbsp;(\x&nbsp;ns&nbsp;-&gt;&nbsp;Node&nbsp;x&nbsp;$&nbsp;toList&nbsp;ns) <span style="color:#2b91af;">fromTree</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;a fromTree&nbsp;=&nbsp;TreeFix&nbsp;.&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;(Node&nbsp;x&nbsp;ns)&nbsp;=&nbsp;NodeF&nbsp;x&nbsp;(fromList&nbsp;ns)</pre> </p> <p> This demonstrates that <code>TreeFix</code> is isomorphic to <code>Tree</code>, which again establishes that <code>treeF</code> and <code>foldTree</code> are equivalent. </p> <h3 id="7180d37efb404a70b707f8c9b8639a35"> Relationships <a href="#7180d37efb404a70b707f8c9b8639a35" title="permalink">#</a> </h3> <p> In this series, you've seen various examples of catamorphisms of structures that have no folds, catamorphisms that coincide with folds, and catamorphisms that are more general than the fold. The introduction to the series included this diagram: </p> <p> <img src="/content/binary/catamorphism-and-fold-relations.png" alt="Catamorphisms and folds as sets, for various sum types."> </p> <p> The <a href="/2019/06/03/either-catamorphism">Either catamorphism</a> is another example of a catamorphism that is more general than the fold, but that one turned out to be identical to the <em>bifold</em>. That's not the case here, because <code>TreeFix</code> isn't a <code>Bifoldable</code> instance at all. </p> <p> There are operations on trees that you can implement with a fold, but some that you can't. Consider the tree in shown in the diagram at the beginning of the article. This is also the tree that the above C# examples use. In Haskell, using <code>TreeFix</code>, you can define that tree like this: </p> <p> <pre>tree&nbsp;= &nbsp;&nbsp;nodeF&nbsp;42 &nbsp;&nbsp;&nbsp;&nbsp;(consF&nbsp;(nodeF&nbsp;1337 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(consF&nbsp;(leafF&nbsp;(-3))&nbsp;nilF))&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(nodeF&nbsp;7 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(consF&nbsp;(leafF&nbsp;(-99))&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(leafF&nbsp;100)&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consF&nbsp;(leafF&nbsp;0)&nbsp;nilF)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nilF)</pre> </p> <p> Yes, that almost looks like some Lisp dialect... </p> <p> Since <code>TreeFix</code> is <code>Foldable</code>, and that type class already comes with <code>sum</code> and <code>maximum</code> functions, no further work is required to repeat the first two of the above C# examples: </p> <p> <pre>*Tree Fix List Tree&gt; sum tree 1384 *Tree Fix List Tree&gt; maximum tree 1337</pre> </p> <p> Counting leaves, or measuring the depth of a tree, on the other hand, is impossible with the <code>Foldable</code> instance, but can be implemented using the catamorphism: </p> <p> <pre><span style="color:#2b91af;">countLeaves</span>&nbsp;::&nbsp;<span style="color:blue;">Num</span>&nbsp;n&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;n countLeaves&nbsp;=&nbsp;treeF&nbsp;(\_&nbsp;xs&nbsp;-&gt;&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:blue;">null</span>&nbsp;xs&nbsp;<span style="color:blue;">then</span>&nbsp;1&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">sum</span>&nbsp;xs) <span style="color:#2b91af;">treeDepth</span>&nbsp;::&nbsp;(<span style="color:blue;">Ord</span>&nbsp;n,&nbsp;<span style="color:blue;">Num</span>&nbsp;n)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">TreeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;n treeDepth&nbsp;=&nbsp;treeF&nbsp;(\_&nbsp;xs&nbsp;-&gt;&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:blue;">null</span>&nbsp;xs&nbsp;<span style="color:blue;">then</span>&nbsp;0&nbsp;<span style="color:blue;">else</span>&nbsp;1&nbsp;+&nbsp;<span style="color:blue;">maximum</span>&nbsp;xs)</pre> </p> <p> The reasoning is the same as already explained in the above C# examples. The functions also produce the same results: </p> <p> <pre>*Tree Fix List Tree&gt; countLeaves tree 4 *Tree Fix List Tree&gt; treeDepth tree 2</pre> </p> <p> This, hopefully, illustrates that the catamorphism is more capable, and that the fold is just a (list-biased) specialisation. </p> <h3 id="66f69ba33cef4ed58d01f1f0bafef14a"> Summary <a href="#66f69ba33cef4ed58d01f1f0bafef14a" title="permalink">#</a> </h3> <p> The catamorphism for a tree is just a single function, which is recursively evaluated. It enables you to translate, traverse, and reduce trees in many interesting ways. </p> <p> You can use the catamorphism to implement a (list-biased) fold, including enumerating all nodes as a flat list, but there are operations (such as counting leaves) that you can implement with the catamorphism, but not with the fold. </p> <p> <strong>Next:</strong> <a href="/2019/08/05/rose-tree-catamorphism">Rose tree catamorphism</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f0f4f607f85846f3bf7b94a36e5c856e"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#f0f4f607f85846f3bf7b94a36e5c856e">#</a></div> <div class="comment-content"> <blockquote> <p> This [<code>Max</code>] method again utilises one of the LINQ methods available via the .NET base class library: <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.max">Max</a>. It is, however, necessary to first check whether the partially reduced <code>xs</code> is empty or not, because the <code>Max</code> extension method on <code>IEnumerable&lt;int&gt;</code> doesn't know how to deal with an empty collection (it throws an exception). When <code>xs</code> is empty that implies a leaf node, in which case you can simply return <code>x</code>; otherwise, you'll first have to use the <code>Max</code> method on <code>xs</code> to find the maximum value there, and then use <code>Math.Max</code> to find the maximum of those two. (I'll here remind the attentive reader that finding the maximum number forms a <a href="/2017/11/27/semigroups">semigroup</a> and that <a href="/2017/12/11/semigroups-accumulate">semigroups accumulate</a> when collections are non-empty. It all fits together. Isn't maths lovely?) </p> <p> ... </p> <p> So far, these two [<code>Sum</code> and <code>Max</code>] extension methods are just specialised <em>folds</em>. </blockquote> <p> Indeed, and <code>fold</code> is implemented for <code>IEnumerable&lt;&gt;</code> via <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate">Aggregate</a>. As such, you can replace the case analysis in... </p> <blockquote> <code>(x, xs) =&gt; xs.Any() ? Math.Max(x, xs.Max()) : x</code> </blockquote> <p> ...with a simple call to <code>Aggregate</code>: </p> <blockquote> <code>(x, xs) =&gt; xs.Aggregate(x, Math.Max)</code> </blockquote> </div> <div class="comment-date">2020-08-03 16:13 UTC</div> </div> <div class="comment" id="d832cf82a2984719a0131fa6d802f584"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d832cf82a2984719a0131fa6d802f584">#</a></div> <div class="comment-content"> <p> <a href="/2017/12/11/semigroups-accumulate#f80749af4347478e905f55c0169687b4">Yes</a>. </p> </div> <div class="comment-date">2020-08-03 17:12 UTC</div> </div> <div class="comment" id="2ee32b71b5e948a9b39ef738ce443719"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#2ee32b71b5e948a9b39ef738ce443719">#</a></div> <div class="comment-content"> <blockquote> I have to admit, though, that the <code>Applicative</code> and <code>Monad</code> instance in general are mind-binding to me. The <code>&lt;*&gt;</code> operation, particularly, takes a <em>tree of functions</em> and has to combine it with a <em>tree of values</em>. What does that even mean? How does one do that? </blockquote> <p> It is also unclear to me. Thankfully <a href="https://nikosbaxevanis.com/">Nikos Baxevanis</a> <a href="https://github.com/hedgehogqa/fsharp-hedgehog/issues/268#issuecomment-914318463">shared</a> <a href="https://well-typed.com/blog/2019/05/integrated-shrinking/">this excellent post by Edsko de Vries</a>. That post helped me <a href="https://github.com/hedgehogqa/fsharp-hedgehog/blob/b48f662ba42541dd51b0a116cca2489339d14d6a/src/Hedgehog/Tree.fs#L56-L64">implement this in F#</a> and gain some understanding of what is happening by considering the many examples. </p> </div> <div class="comment-date">2022-09-17 16:59 UTC</div> </div> <div class="comment" id="0b6120ae739747c3b040bb5734102ef8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0b6120ae739747c3b040bb5734102ef8">#</a></div> <div class="comment-content"> <p> Tyson, thank you for the link. It's a great article, and it does help with establishing an intuition for applicative trees. </p> </div> <div class="comment-date">2022-09-18 19:12 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Either catamorphism https://blog.ploeh.dk/2019/06/03/either-catamorphism 2019-06-03T06:05:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for Either is a generalisation of its fold. The catamorphism enables operations not available via fold.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for <a href="/2018/06/11/church-encoded-either">Either</a> (also known as <em>Result</em>), as well as how to identify it. The beginning of this article presents the catamorphism in C#, with examples. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <p> <em>Either</em> is a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">data container</a> that models two mutually exclusive results. It's often used to return values that may be either correct (<em>right</em>), or incorrect (<em>left</em>). In statically typed functional programming with a preference for total functions, Either offers a saner, more reasonable way to model success and error results than throwing exceptions. </p> <h3 id="d8214c38aac04ee7b80b9352d57d3bd1"> C# catamorphism <a href="#d8214c38aac04ee7b80b9352d57d3bd1" title="permalink">#</a> </h3> <p> This article uses <a href="/2018/06/11/church-encoded-either">the Church encoding of Either</a>. The catamorphism for Either is the <code>Match</code> method: </p> <p> <pre><span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight);</pre> </p> <p> Until this article, all previous catamorphisms have been a pair made from an initial value and a function. The Either catamorphism is a generalisation, since it's a pair of functions. One function handles the case where there's a <em>left</em> value, and the other function handles the case where there's a <em>right</em> value. Both functions must return the same, unifying type, which is often a string or something similar that can be shown in a user interface: </p> <p> <pre>&gt; <span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">TimeSpan</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">TimeSpan</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#2b91af;">TimeSpan</span>.FromMinutes(3)); &gt; e.Match(ts&nbsp;=&gt;&nbsp;ts.ToString(),&nbsp;i&nbsp;=&gt;&nbsp;i.ToString()) "00:03:00" &gt; <span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">TimeSpan</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">TimeSpan</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42); &gt; e.Match(ts&nbsp;=&gt;&nbsp;ts.ToString(),&nbsp;i&nbsp;=&gt;&nbsp;i.ToString()) "42"</pre> </p> <p> You'll often see examples like the above that turns both left and right cases into strings or something that can be represented as a unifying response type, but this is in no way required. If you can come up with a unifying type, you can convert both cases to that type: </p> <p> <pre>&gt; <span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;e&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#2b91af;">Guid</span>.NewGuid()); &gt; e.Match(g&nbsp;=&gt;&nbsp;g.ToString().Count(c&nbsp;=&gt;&nbsp;<span style="color:#a31515;">&#39;a&#39;</span>&nbsp;&lt;=&nbsp;c),&nbsp;s&nbsp;=&gt;&nbsp;s.Length) 12 &gt; <span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;e&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>); &gt; e.Match(g&nbsp;=&gt;&nbsp;g.ToString().Count(c&nbsp;=&gt;&nbsp;<span style="color:#a31515;">&#39;a&#39;</span>&nbsp;&lt;=&nbsp;c),&nbsp;s&nbsp;=&gt;&nbsp;s.Length) 3</pre> </p> <p> In the two above examples, you use two different functions that both reduce respectively <code>Guid</code> and <code>string</code> values to a number. The function that turns <code>Guid</code> values into a number counts how many of the hexadecimal digits that are greater than or equal to A (10). The other function simply returns the length of the <code>string</code>, if it's there. That example makes little sense, but the <code>Match</code> method doesn't care about that. </p> <p> In practical use, Either is often used for error handling. The <a href="/2018/06/11/church-encoded-either">article on the Church encoding of Either</a> contains an example. </p> <h3 id="99e1027823114e95bebf81c08d35779f"> Either F-Algebra <a href="#99e1027823114e95bebf81c08d35779f" title="permalink">#</a> </h3> <p> As in the <a href="/2019/05/27/list-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. </p> <p> While F-Algebras and fixed points are mostly used for recursive data structures, you can also define an F-Algebra for a non-recursive data structure. You already saw an example of that in the articles about <a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a> and <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>. The difference between e.g. Maybe values and Either is that both cases of Either carry a value. You can model this as a <code>Functor</code> with both a carrier type and two type arguments for the data that Either may contain: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;EitherF&nbsp;l&nbsp;r&nbsp;c&nbsp;=&nbsp;LeftF&nbsp;l&nbsp;|&nbsp;RightF&nbsp;r&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">EitherF</span>&nbsp;l&nbsp;r)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;(LeftF&nbsp;l)&nbsp;=&nbsp;LeftF&nbsp;l &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;(RightF&nbsp;r)&nbsp;=&nbsp;RightF&nbsp;r</pre> </p> <p> I chose to call the 'data types' <code>l</code> (for <em>left</em>) and <code>r</code> (for <em>right</em>), and the carrier type <code>c</code> (for <em>carrier</em>). As was also the case with <code>BoolF</code> and <code>MaybeF</code>, the <code>Functor</code> instance ignores the map function because the carrier type is missing from both the <code>LeftF</code> case and the <code>RightF</code> case. Like the <code>Functor</code> instances for <code>BoolF</code> and <code>MaybeF</code>, it'd seem that nothing happens, but at the type level, this is still a translation from <code>EitherF l r c</code> to <code>EitherF l r c1</code>. Not much of a function, perhaps, but definitely an <em>endofunctor</em>. </p> <p> As was also the case when deducing the Maybe and List catamorphisms, Haskell isn't too happy about defining instances for a type like <code>Fix (EitherF l r)</code>. To address that problem, you can introduce a <code>newtype</code> wrapper: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;EitherFix&nbsp;l&nbsp;r&nbsp;=&nbsp;EitherFix&nbsp;{&nbsp;unEitherFix&nbsp;::&nbsp;Fix&nbsp;(EitherF&nbsp;l&nbsp;r)&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> You can define <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, etc. instances for this type without resorting to any funky GHC extensions. Keep in mind that ultimately, the purpose of all this code is just to figure out what the catamorphism looks like. This code isn't intended for actual use. </p> <p> A pair of helper functions make it easier to define <code>EitherFix</code> values: </p> <p> <pre><span style="color:#2b91af;">leftF</span>&nbsp;::&nbsp;l&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;l&nbsp;r leftF&nbsp;=&nbsp;EitherFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;LeftF <span style="color:#2b91af;">rightF</span>&nbsp;::&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;l&nbsp;r rightF&nbsp;=&nbsp;EitherFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;RightF</pre> </p> <p> With those functions, you can create <code>EitherFix</code> values: </p> <p> <pre>Prelude Data.UUID Data.UUID.V4 Fix Either&gt; leftF &lt;$&gt; nextRandom EitherFix {unEitherFix = Fix (LeftF e65378c2-0d6e-47e0-8bcb-7cc29d185fad)} Prelude Data.UUID Data.UUID.V4 Fix Either&gt; rightF "foo" EitherFix {unEitherFix = Fix (RightF "foo")}</pre> </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="67d13d05a8564f3481e181d0c32b3165"> Haskell catamorphism <a href="#67d13d05a8564f3481e181d0c32b3165" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>EitherF l r</code>), and an object <code>c</code>, but you still need to find a morphism <code>EitherF l r c -&gt; c</code>. Notice that the algebra you have to find is the function that reduces the functor to its <em>carrier type</em> <code>c</code>, not the 'data types' <code>l</code> and <code>r</code>. This takes some time to get used to, but that's how catamorphisms work. This doesn't mean, however, that you get to ignore <code>l</code> and <code>r</code>, as you'll see. </p> <p> As in the previous articles, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>eitherF&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unEitherFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;(LeftF&nbsp;l)&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(RightF&nbsp;r)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from the <code>LeftF</code> case? You could pass an argument to the <code>eitherF</code> function: </p> <p> <pre>eitherF&nbsp;fl&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unEitherFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;(LeftF&nbsp;l)&nbsp;=&nbsp;fl&nbsp;l &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(RightF&nbsp;r)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While you could, technically, pass an argument of the type <code>c</code> to <code>eitherF</code> and then return that value from the <code>LeftF</code> case, that would mean that you would ignore the <code>l</code> value. This would be incorrect, so instead, make the argument a function and call it with <code>l</code>. Likewise, you can deal with the <code>RightF</code> case in the same way: </p> <p> <pre><span style="color:#2b91af;">eitherF</span>&nbsp;::&nbsp;(l&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;l&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c eitherF&nbsp;fl&nbsp;fr&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unEitherFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;(LeftF&nbsp;l)&nbsp;=&nbsp;fl&nbsp;l &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(RightF&nbsp;r)&nbsp;=&nbsp;fr&nbsp;r</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>EitherF</code>, the compiler infers that the <code>alg</code> function has the type <code>EitherF l r c -&gt; c</code>, which is just what you need! </p> <p> You can now see what the carrier type <code>c</code> is for. It's the type that the algebra extracts, and thus the type that the catamorphism returns. </p> <p> This, then, is the catamorphism for Either. As has been consistent so far, it's a pair, but now made from two functions. As you've seen repeatedly, this isn't the only possible catamorphism, since you can, for example, trivially flip the arguments to <code>eitherF</code>. </p> <p> I've chosen the representation shown here because it's isomorphic to the <code>either</code> function from Haskell's built-in <code>Data.Either</code> module, which calls the function the "Case analysis for the <code>Either</code> type". Both of these functions (<code>eitherF</code> and <code>either</code>) are equivalent to the above C# <code>Match</code> method. </p> <h3 id="77731d63c79543b0994c06721521b6f3"> Basis <a href="#77731d63c79543b0994c06721521b6f3" title="permalink">#</a> </h3> <p> You can implement most other useful functionality with <code>eitherF</code>. Here's the <code>Bifunctor</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bifunctor</span>&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bimap&nbsp;f&nbsp;s&nbsp;=&nbsp;eitherF&nbsp;(leftF&nbsp;.&nbsp;f)&nbsp;(rightF&nbsp;.&nbsp;s)</pre> </p> <p> From that instance, the <code>Functor</code> instance trivially follows: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">EitherFix</span>&nbsp;l)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;=&nbsp;second</pre> </p> <p> On top of <code>Functor</code> you can add <code>Applicative</code>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;(<span style="color:blue;">EitherFix</span>&nbsp;l)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;=&nbsp;rightF &nbsp;&nbsp;f&nbsp;&lt;*&gt;&nbsp;x&nbsp;=&nbsp;eitherF&nbsp;leftF&nbsp;(&lt;$&gt;&nbsp;x)&nbsp;f</pre> </p> <p> Notice that the <code>&lt;*&gt;</code> implementation is similar to to the <code>&lt;*&gt;</code> implementation for <code>MaybeFix</code>. The same is the case for the <code>Monad</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;(<span style="color:blue;">EitherFix</span>&nbsp;l)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;x&nbsp;&gt;&gt;=&nbsp;f&nbsp;=&nbsp;eitherF&nbsp;leftF&nbsp;f&nbsp;x</pre> </p> <p> Not only is <code>EitherFix</code> <code>Foldable</code>, it's <code>Bifoldable</code>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bifoldable</span>&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bifoldMap&nbsp;=&nbsp;eitherF</pre> </p> <p> Notice, interestingly, that <code>bifoldMap</code> is identical to <code>eitherF</code>. </p> <p> The <code>Bifoldable</code> instance enables you to trivially implement the <code>Foldable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;(<span style="color:blue;">EitherFix</span>&nbsp;l)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;foldMap&nbsp;=&nbsp;bifoldMap&nbsp;mempty</pre> </p> <p> You may find the presence of <code>mempty</code> puzzling, since <code>bifoldMap</code> (or <code>eitherF</code>; they're identical) takes as arguments two functions. Is <code>mempty</code> a function? </p> <p> Yes, <code>mempty</code> can be a function. Here, it is. There's a <code>Monoid</code> instance for any function <code>a -&gt; m</code>, where <code>m</code> is a <code>Monoid</code> instance, and <code>mempty</code> is the identity for that monoid. That's the instance in use here. </p> <p> Just as <code>EitherFix</code> is <code>Bifoldable</code>, it's also <code>Bitraversable</code>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Bitraversable</span>&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;bitraverse&nbsp;fl&nbsp;fr&nbsp;=&nbsp;eitherF&nbsp;(<span style="color:blue;">fmap</span>&nbsp;leftF&nbsp;.&nbsp;fl)&nbsp;(<span style="color:blue;">fmap</span>&nbsp;rightF&nbsp;.&nbsp;fr)</pre> </p> <p> You can comfortably implement the <code>Traversable</code> instance based on the <code>Bitraversable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;(<span style="color:blue;">EitherFix</span>&nbsp;l)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;bisequenceA&nbsp;.&nbsp;first&nbsp;pure</pre> </p> <p> Finally, you can implement conversions to and from the standard <code>Either</code> type, using <code>ana</code> as the dual of <code>cata</code>: </p> <p> <pre><span style="color:#2b91af;">toEither</span>&nbsp;::&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;l&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;l&nbsp;r toEither&nbsp;=&nbsp;eitherF&nbsp;Left&nbsp;Right <span style="color:#2b91af;">fromEither</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Either</span>&nbsp;a&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">EitherFix</span>&nbsp;a&nbsp;b fromEither&nbsp;=&nbsp;EitherFix&nbsp;.&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;&nbsp;(Left&nbsp;l)&nbsp;=&nbsp;&nbsp;LeftF&nbsp;l &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;(Right&nbsp;r)&nbsp;=&nbsp;RightF&nbsp;r</pre> </p> <p> This demonstrates that <code>EitherFix</code> is isomorphic to <code>Either</code>, which again establishes that <code>eitherF</code> and <code>either</code> are equivalent. </p> <h3 id="00a274a2317548538521d4a171191230"> Relationships <a href="#00a274a2317548538521d4a171191230" title="permalink">#</a> </h3> <p> In this series, you've seen various examples of catamorphisms of structures that have no folds, catamorphisms that coincide with folds, and now a catamorphism that is more general than the fold. The introduction to the series included this diagram: </p> <p> <img src="/content/binary/catamorphism-and-fold-relations.png" alt="Catamorphisms and folds as sets, for various sum types."> </p> <p> This shows that Boolean values and Peano numbers have catamorphisms, but no folds, whereas for Maybe and List, the fold and the catamorphism is the same. For Either, however, the fold is a special case of the catamorphism. The fold for Either 'pretends' that the left side doesn't exist. Instead, the left value is interpreted as a missing right value. Thus, in order to fold Either values, you must supply a 'fallback' value that will be used in case an Either value isn't a <em>right</em> value: </p> <p> <pre>Prelude Fix Either&gt; e = rightF LT :: EitherFix Integer Ordering Prelude Fix Either&gt; foldr (const . show) "" e "LT" Prelude Fix Either&gt; e = leftF 42 :: EitherFix Integer Ordering Prelude Fix Either&gt; foldr (const . show) "" e ""</pre> </p> <p> In a GHCi session like the above, you can create two Either values of the same type. The <em>right</em> case is an <code>Ordering</code> value, while the <em>left</em> case is an <code>Integer</code> value. </p> <p> With <code>foldr</code>, there's no way to access the <em>left</em> case. While you can access and transform the right <code>Ordering</code> value, the number <code>42</code> is simply ignored during the fold. Instead, the default value <code>""</code> is returned. </p> <p> Contrast this with the catamorphism, which can access both cases: </p> <p> <pre>Prelude Fix Either&gt; e = rightF LT :: EitherFix Integer Ordering Prelude Fix Either&gt; eitherF show show e "LT" Prelude Fix Either&gt; e = leftF 42 :: EitherFix Integer Ordering Prelude Fix Either&gt; eitherF show show e "42"</pre> </p> <p> In a session like this, you recreate the same values, but using the catamorphism <code>eitherF</code>, you can now access and transform both the <em>left</em> and the <em>right</em> cases. In other words, the catamorphism enables you to perform operations not possible with the fold. </p> <p> It's interesting, however, to note that while the fold is a specialisation of the catamorphism, the <em>bifold</em> is identical to the catamorphism. </p> <h3 id="7512bca753b747ad9accde04bf13b6ca"> Summary <a href="#7512bca753b747ad9accde04bf13b6ca" title="permalink">#</a> </h3> <p> The catamorphism for Either is a pair of functions. One function transforms the <em>left</em> case, while the other function transforms the <em>right</em> case. For any Either value, only one of those functions will be used. </p> <p> When I originally encountered the concept of a <em>catamorphism</em>, I found it difficult to distinguish between catamorphism and fold. My problem was, I think, that the tutorials I ran into mostly used linked lists to demonstrate how, <a href="/2019/05/27/list-catamorphism">in that case</a>, the fold <em>is</em> the catamorphism. It turns out, however, that this isn't always the case. A catamorphism is a general abstraction. A fold, on the other hand, seems to me to be mostly related to collections. </p> <p> In this article you saw the first example of a catamorphism that can do more than the fold. For Either, the fold is just a special case of the catamorphism. You also saw, however, how the catamorphism was identical to the <em>bifold</em>. Thus, it's still not entirely clear how these concepts relate. Therefore, in the next article, you'll get an example of a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> where there's no bifold, and where the catamorphism is, indeed, a generalisation of the fold. </p> <p> <strong>Next:</strong> <a href="/2019/06/10/tree-catamorphism">Tree catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. List catamorphism https://blog.ploeh.dk/2019/05/27/list-catamorphism 2019-05-27T06:10:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for a list is the same as its fold.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for (linked) lists, and other collections in general. It also shows how to identify it. The beginning of this article presents the catamorphism in C#, with an example. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <p> The C# part of the article will discuss <code>IEnumerable&lt;T&gt;</code>, while the Haskell part will deal specifically with linked lists. Since C# is a less strict language anyway, we have to make some concessions when we consider how concepts translate. In my experience, the functionality of <code>IEnumerable&lt;T&gt;</code> closely mirrors that of Haskell lists. </p> <h3 id="3190f7181a954b6388d77f61a1dbb928"> C# catamorphism <a href="#3190f7181a954b6388d77f61a1dbb928" title="permalink">#</a> </h3> <p> The .NET base class library defines this <a href="https://docs.microsoft.com/dotnet/api/system.linq.enumerable.aggregate">Aggregate</a> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TAccumulate</span>&nbsp;Aggregate&lt;<span style="color:#2b91af;">TSource</span>,&nbsp;<span style="color:#2b91af;">TAccumulate</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TSource</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TAccumulate</span>&nbsp;seed, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">TAccumulate</span>,&nbsp;<span style="color:#2b91af;">TSource</span>,&nbsp;<span style="color:#2b91af;">TAccumulate</span>&gt;&nbsp;func);</pre> </p> <p> This is the catamorphism for linked lists (and, I'll conjecture, for <code>IEnumerable&lt;T&gt;</code> in general). The <a href="/2019/04/29/catamorphisms">introductory article</a> already used it to show several motivating examples, of which I'll only repeat the last: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;} . .Aggregate(<span style="color:#2b91af;">Angle</span>.Identity,&nbsp;(a,&nbsp;i)&nbsp;=&gt;&nbsp;a.Add(<span style="color:#2b91af;">Angle</span>.FromDegrees(i))) [{ Angle = 207° }]</pre> </p> <p> In short, the catamorphism is, similar to the previous catamorphisms covered in this article series, a pair made from an initial value and a function. This has been true for both the <a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a> and the <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>. An initial value is just a value in all three cases, but you may notice that the function in question becomes increasingly elaborate. For <code>IEnumerable&lt;T&gt;</code>, it's a function that takes two values. One of the values are of the type of the input list, i.e. for <code>IEnumerable&lt;TSource&gt;</code> it would be <code>TSource</code>. By elimination you can deduce that this value must come from the input list. The other value is of the type <code>TAccumulate</code>, which implies that it could be the <code>seed</code>, or the result from a previous call to <code>func</code>. </p> <h3 id="46afb325e03743d9ac2c2cf391607f82"> List F-Algebra <a href="#46afb325e03743d9ac2c2cf391607f82" title="permalink">#</a> </h3> <p> As in the <a href="/2019/05/20/maybe-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. The <code>ListF</code> type comes from his article as well, although I've renamed the type arguments: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;ListF&nbsp;a&nbsp;c&nbsp;=&nbsp;NilF&nbsp;|&nbsp;ConsF&nbsp;a&nbsp;c&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">ListF</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NilF&nbsp;&nbsp;=&nbsp;NilF &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;(ConsF&nbsp;a&nbsp;c)&nbsp;=&nbsp;ConsF&nbsp;a&nbsp;$&nbsp;f&nbsp;c</pre> </p> <p> Like I did with <code>MaybeF</code>, I've named the 'data' type argument <code>a</code>, and the carrier type <code>c</code> (for <em>carrier</em>). Once again, notice that the <code>Functor</code> instance maps over the carrier type <code>c</code>; not over the 'data' type <code>a</code>. </p> <p> As was also the case when deducing the Maybe catamorphism, Haskell isn't too happy about defining instances for a type like <code>Fix (ListF a)</code>. To address that problem, you can introduce a <code>newtype</code> wrapper: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;ListFix&nbsp;a&nbsp;=&nbsp;ListFix&nbsp;{&nbsp;unListFix&nbsp;::&nbsp;Fix&nbsp;(ListF&nbsp;a)&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> You can define <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, etc. instances for this type without resorting to any funky GHC extensions. Keep in mind that ultimately, the purpose of all this code is just to figure out what the catamorphism looks like. This code isn't intended for actual use. </p> <p> A few helper functions make it easier to define <code>ListFix</code> values: </p> <p> <pre><span style="color:#2b91af;">nilF</span>&nbsp;::&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a nilF&nbsp;=&nbsp;ListFix&nbsp;$&nbsp;Fix&nbsp;NilF <span style="color:#2b91af;">consF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a consF&nbsp;x&nbsp;=&nbsp;ListFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;ConsF&nbsp;x&nbsp;.&nbsp;unListFix</pre> </p> <p> With those functions, you can create <code>ListFix</code> linked lists: </p> <p> <pre>Prelude Fix List&gt; nilF ListFix {unListFix = Fix NilF} Prelude Fix List&gt; consF 42 $ consF 1337 $ consF 2112 nilF ListFix {unListFix = Fix (ConsF 42 (Fix (ConsF 1337 (Fix (ConsF 2112 (Fix NilF))))))}</pre> </p> <p> The first example creates an empty list, whereas the second creates a list of three integers, corresponding to <code>[42,1337,2112]</code>. </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="db44e85b27fb40d0b570c53cf4ce1843"> Haskell catamorphism <a href="#db44e85b27fb40d0b570c53cf4ce1843" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>ListF</code>), and an object <code>a</code>, but you still need to find a morphism <code>ListF a c -&gt; c</code>. Notice that the algebra you have to find is the function that reduces the functor to its <em>carrier type</em> <code>c</code>, not the 'data type' <code>a</code>. This takes some time to get used to, but that's how catamorphisms work. This doesn't mean, however, that you get to ignore <code>a</code>, as you'll see. </p> <p> As in the previous article, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>listF&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unListFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NilF&nbsp;&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(ConsF&nbsp;h&nbsp;t)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from the <code>NilF</code> case? You could pass an argument to the <code>listF</code> function: </p> <p> <pre>listF&nbsp;n&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unListFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NilF&nbsp;&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(ConsF&nbsp;h&nbsp;t)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> The <code>ConsF</code> case, contrary to <code>NilF</code>, contains both a head <code>h</code> (of type <code>a</code>) and a tail <code>t</code> (of type <code>c</code>). While you could make the code compile by simply returning <code>t</code>, it'd be incorrect to ignore <code>h</code>. In order to deal with both, you'll need a function that turns both <code>h</code> and <code>t</code> into a value of the type <code>c</code>. You can do this by passing a function to <code>listF</code> and using it: </p> <p> <pre><span style="color:#2b91af;">listF</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c listF&nbsp;f&nbsp;n&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unListFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NilF&nbsp;&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(ConsF&nbsp;h&nbsp;t)&nbsp;=&nbsp;f&nbsp;h&nbsp;t</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>ListF</code>, the compiler infers that the <code>alg</code> function has the type <code>ListF a c -&gt; c</code>, which is just what you need! </p> <p> You can now see what the carrier type <code>c</code> is for. It's the type that the algebra extracts, and thus the type that the catamorphism returns. </p> <p> This, then, is the catamorphism for lists. As has been consistent so far, it's a pair made from an initial value and a function. Once more, this isn't the only possible catamorphism, since you can, for example, trivially flip the arguments to <code>listF</code>: </p> <p> <pre><span style="color:#2b91af;">listF</span>&nbsp;::&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c listF&nbsp;n&nbsp;f&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unListFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NilF&nbsp;&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(ConsF&nbsp;h&nbsp;t)&nbsp;=&nbsp;f&nbsp;h&nbsp;t</pre> </p> <p> You can also flip the arguments of <code>f</code>: </p> <p> <pre><span style="color:#2b91af;">listF</span>&nbsp;::&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c listF&nbsp;n&nbsp;f&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unListFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NilF&nbsp;&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(ConsF&nbsp;h&nbsp;t)&nbsp;=&nbsp;f&nbsp;t&nbsp;h</pre> </p> <p> These representations are all isomorphic to each other, but notice that the last variation is similar to the above C# <code>Aggregate</code> overload. The initial <code>n</code> value is the <code>seed</code>, and the function <code>f</code> has the same shape as <code>func</code>. Thus, I consider it reasonable to conjecture that that <code>Aggregate</code> overload is the catamorphism for <code>IEnumerable&lt;T&gt;</code>. </p> <h3 id="3ba9143e87544443b713727eb4ea60ba"> Basis <a href="#3ba9143e87544443b713727eb4ea60ba" title="permalink">#</a> </h3> <p> You can implement most other useful functionality with <code>listF</code>. The rest of this article uses the first of the variations shown above, with the type <code>(a -&gt; c -&gt; c) -&gt; c -&gt; ListFix a -&gt; c</code>. Here's the <code>Semigroup</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;(<span style="color:blue;">ListFix</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;xs&nbsp;&lt;&gt;&nbsp;ys&nbsp;=&nbsp;listF&nbsp;consF&nbsp;ys&nbsp;xs</pre> </p> <p> The initial value passed to <code>listF</code> is <code>ys</code>, and the function to apply is simply the <code>consF</code> function, thus 'consing' the two lists together. Here's an example of the operation in action: </p> <p> <pre>Prelude Fix List&gt; consF 42 $ consF 1337 nilF &lt;&gt; (consF 2112 $ consF 1 nilF) ListFix {unListFix = Fix (ConsF 42 (Fix (ConsF 1337 (Fix (ConsF 2112 (Fix (ConsF 1 (Fix NilF))))))))}</pre> </p> <p> With a <code>Semigroup</code> instance, it's trivial to also implement the <code>Monoid</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;(<span style="color:blue;">ListFix</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;mempty&nbsp;=&nbsp;nilF</pre> </p> <p> While you <em>could</em> implement <code>mempty</code> with <code>listF</code> (<code>mempty = listF (const id) nilF nilF</code>), that'd be overcomplicated. Just because you can implement all functionality using <code>listF</code>, it doesn't mean that you should, if a simpler alternative exists. </p> <p> You can, on the other hand, use <code>listF</code> for the <code>Functor</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;=&nbsp;listF&nbsp;(\h&nbsp;l&nbsp;-&gt;&nbsp;consF&nbsp;(f&nbsp;h)&nbsp;l)&nbsp;nilF</pre> </p> <p> You could write the function you pass to <code>listF</code> in a point-free fashion as <code>consF . f</code>, but I thought it'd be easier to follow what happens when written as an explicit lambda expression. The function receives a 'current value' <code>h</code>, as well as the part of the list which has already been translated <code>l</code>. Use <code>f</code> to translate <code>h</code>, and <code>consF</code> the result unto <code>l</code>. </p> <p> You can add <code>Applicative</code> and <code>Monad</code> instances in a similar fashion: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;x&nbsp;=&nbsp;consF&nbsp;x&nbsp;nilF &nbsp;&nbsp;fs&nbsp;&lt;*&gt;&nbsp;xs&nbsp;=&nbsp;listF&nbsp;(\f&nbsp;acc&nbsp;-&gt;&nbsp;(f&nbsp;&lt;$&gt;&nbsp;xs)&nbsp;&lt;&gt;&nbsp;acc)&nbsp;nilF&nbsp;fs <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;xs&nbsp;&gt;&gt;=&nbsp;f&nbsp;=&nbsp;listF&nbsp;(\x&nbsp;acc&nbsp;-&gt;&nbsp;f&nbsp;x&nbsp;&lt;&gt;&nbsp;acc)&nbsp;nilF&nbsp;xs</pre> </p> <p> What may be more interesting, however, is the <code>Foldable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">foldr</span>&nbsp;=&nbsp;listF</pre> </p> <p> The demonstrates that <code>listF</code> and <code>foldr</code> is the same. </p> <p> Next, you can also add a <code>Traversable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;listF&nbsp;(\x&nbsp;acc&nbsp;-&gt;&nbsp;consF&nbsp;&lt;$&gt;&nbsp;x&nbsp;&lt;*&gt;&nbsp;acc)&nbsp;(pure&nbsp;nilF)</pre> </p> <p> Finally, you can implement conversions to and from the standard list <code>[]</code> type, using <code>ana</code> as the dual of <code>cata</code>: </p> <p> <pre><span style="color:#2b91af;">toList</span>&nbsp;::&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] toList&nbsp;=&nbsp;listF&nbsp;<span style="color:#2b91af;">(:)</span>&nbsp;<span style="color:blue;">[]</span> <span style="color:#2b91af;">fromList</span>&nbsp;::&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ListFix</span>&nbsp;a fromList&nbsp;=&nbsp;ListFix&nbsp;.&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;&nbsp;&nbsp;<span style="color:blue;">[]</span>&nbsp;&nbsp;=&nbsp;NilF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;(h:t)&nbsp;=&nbsp;ConsF&nbsp;h&nbsp;t</pre> </p> <p> This demonstrates that <code>ListFix</code> is isomorphic to <code>[]</code>, which again establishes that <code>listF</code> and <code>foldr</code> are equivalent. </p> <h3 id="616c31c72b1f43cca647760d9fa8b226"> Summary <a href="#616c31c72b1f43cca647760d9fa8b226" title="permalink">#</a> </h3> <p> The catamorphism for lists is a pair made from an initial value and a function. One variation is equal to <code>foldr</code>. Like Maybe, the catamorphism is the same as the fold. </p> <p> In C#, this function corresponds to the <code>Aggregate</code> extension method identified above. </p> <p> You've now seen two examples where the catamorphism coincides with the fold. You've also seen examples (<a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a> and <a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a>) where there's a catamorphism, but no fold at all. In <a href="/2019/06/03/either-catamorphism">a subsequent article</a>, you'll see an example of a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> that has both catamorphism and fold, but where the catamorphism is more general than the fold. </p> <p> <strong>Next:</strong> <a href="/2023/08/07/nonempty-catamorphism">NonEmpty catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Maybe catamorphism https://blog.ploeh.dk/2019/05/20/maybe-catamorphism 2019-05-20T06:04:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for Maybe is just a simplification of its fold.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for <a href="/2018/03/26/the-maybe-functor">Maybe</a>, as well as how to identify it. The beginning of this article presents the catamorphism in C#, with examples. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <p> <em>Maybe</em> is a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">data container</a> that models the absence or presence of a value. <a href="/2015/11/13/null-has-no-type-but-maybe-has">Contrary to null, Maybe has a type</a>, so offers a sane and reasonable way to model that situation. </p> <h3 id="1feeee3382ff44d182e0a28a33f8f80a"> C# catamorphism <a href="#1feeee3382ff44d182e0a28a33f8f80a" title="permalink">#</a> </h3> <p> This article uses <a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a>. Other, <a href="/2018/03/26/the-maybe-functor">alternative implementations of Maybe are possible</a>. The catamorphism for Maybe is the <code>Match</code> method: </p> <p> <pre><span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">TResult</span>&nbsp;nothing,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;just);</pre> </p> <p> Like the <a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a>, the Maybe catamorphism is a pair of a value and a function. The <code>nothing</code> value corresponds to the absence of data, whereas the <code>just</code> function handles the presence of data. </p> <p> Given, for example, a Maybe containing a number, you can use <code>Match</code> to <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">get the value out of the Maybe</a>: </p> <p> <pre>&gt; <span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;maybe&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">int</span>&gt;(42); &gt; maybe.Match(0,&nbsp;x&nbsp;=&gt;&nbsp;x) 42 &gt; <span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;maybe&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">int</span>&gt;(); &gt; maybe.Match(0,&nbsp;x&nbsp;=&gt;&nbsp;x) 0</pre> </p> <p> The functionality is, however, more useful than a simple <em>get-value-or-default</em> operation. Often, you don't have a good default value for the type potentially wrapped in a Maybe object. In the core of your application architecture, it may not be clear how to deal with, say, the absence of a <code>Reservation</code> object, whereas at the boundary of your system, it's evident how to convert both absence and presence of data into a unifying type, such as an HTTP response: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;maybe&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">return</span>&nbsp;maybe &nbsp;&nbsp;&nbsp;&nbsp;.Select(r&nbsp;=&gt;&nbsp;Repository.Create(r)) &nbsp;&nbsp;&nbsp;&nbsp;.Match&lt;<span style="color:#2b91af;">IHttpActionResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;Content( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpStatusCode</span>.InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpError</span>(<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;id&nbsp;=&gt;&nbsp;Ok(id));</pre> </p> <p> This enables you to avoid special cases, such as null <code>Reservation</code> objects, or magic numbers like <code>-1</code> to indicate the absence of <code>id</code> values. At the boundary of an HTTP-based application, you know that you must return an HTTP response. That's the unifying type, so you can return <code>200 OK</code> with a reservation ID in the response body when data is present, and <code>500 Internal Server Error</code> when data is absent. </p> <h3 id="87d91da8944f4eb5b8b24e9ea20d3e1b"> Maybe F-Algebra <a href="#87d91da8944f4eb5b8b24e9ea20d3e1b" title="permalink">#</a> </h3> <p> As in the <a href="/2019/05/13/peano-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. </p> <p> While F-Algebras and fixed points are mostly used for recursive data structures, you can also define an F-Algebra for a non-recursive data structure. You already saw an example of that in the article about <a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a>. The difference between Boolean values and Maybe is that the <em>just</em> case of Maybe carries a value. You can model this as a <code>Functor</code> with both a carrier type and a type argument for the data that Maybe may contain: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;MaybeF&nbsp;a&nbsp;c&nbsp;=&nbsp;NothingF&nbsp;|&nbsp;JustF&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">MaybeF</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;NothingF&nbsp;=&nbsp;NothingF &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;JustF&nbsp;x</pre> </p> <p> I chose to call the 'data type' <code>a</code> and the carrier type <code>c</code> (for <em>carrier</em>). As was also the case with <code>BoolF</code>, the <code>Functor</code> instance ignores the map function because the carrier type is missing from both the <code>NothingF</code> case and the <code>JustF</code> case. Like the <code>Functor</code> instance for <code>BoolF</code>, it'd seem that nothing happens, but at the type level, this is still a translation from <code>MaybeF a c</code> to <code>MaybeF a c1</code>. Not much of a function, perhaps, but definitely an <em>endofunctor</em>. </p> <p> In the previous articles, it was possible to work directly with the fixed points of both functors; i.e. <code>Fix BoolF</code> and <code>Fix NatF</code>. Haskell isn't happy about attempts to define various instances for <code>Fix (MaybeF a)</code>, so in order to make this easier, you can define a <code>newtype</code> wrapper: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;MaybeFix&nbsp;a&nbsp;= &nbsp;&nbsp;MaybeFix&nbsp;{&nbsp;unMaybeFix&nbsp;::&nbsp;Fix&nbsp;(MaybeF&nbsp;a)&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> In order to make it easier to work with <code>MaybeFix</code> you can add helper functions to create values: </p> <p> <pre><span style="color:#2b91af;">nothingF</span>&nbsp;::&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a nothingF&nbsp;=&nbsp;MaybeFix&nbsp;$&nbsp;Fix&nbsp;NothingF <span style="color:#2b91af;">justF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a justF&nbsp;=&nbsp;MaybeFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;JustF</pre> </p> <p> You can now create <code>MaybeFix</code> values to your heart's content: </p> <p> <pre>Prelude Fix Maybe&gt; justF 42 MaybeFix {unMaybeFix = Fix (JustF 42)} Prelude Fix Maybe&gt; nothingF MaybeFix {unMaybeFix = Fix NothingF}</pre> </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="24db4c715d1f4540bd8f87604819952f"> Haskell catamorphism <a href="#24db4c715d1f4540bd8f87604819952f" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>MaybeF</code>), and an object <code>a</code>, but you still need to find a morphism <code>MaybeF a c -&gt; c</code>. Notice that the algebra you have to find is the function that reduces the functor to its <em>carrier type</em> <code>c</code>, not the 'data type' <code>a</code>. This takes some time to get used to, but that's how catamorphisms work. This doesn't mean, however, that you get to ignore <code>a</code>, as you'll see. </p> <p> As in the previous article, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>maybeF&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unMaybeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;NothingF&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from the <code>NothingF</code> case? You could pass an argument to the <code>maybeF</code> function: </p> <p> <pre>maybeF&nbsp;n&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unMaybeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;NothingF&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> The <code>JustF</code> case, contrary to <code>NothingF</code>, already contains a value, and it'd be incorrect to ignore it. On the other hand, <code>x</code> is a value of type <code>a</code>, and you need to return a value of type <code>c</code>. You'll need a function to perform the conversion, so pass such a function as an argument to <code>maybeF</code> as well: </p> <p> <pre><span style="color:#2b91af;">maybeF</span>&nbsp;::&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c maybeF&nbsp;n&nbsp;f&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unMaybeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;NothingF&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;f&nbsp;x</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>MaybeF</code>, the compiler infers that the <code>alg</code> function has the type <code>MaybeF a c -&gt; c</code>, which is just what you need! </p> <p> You can now see what the carrier type <code>c</code> is for. It's the type that the algebra extracts, and thus the type that the catamorphism returns. </p> <p> Notice that <code>maybeF</code>, like the above C# <code>Match</code> method, takes as arguments a pair of a value and a function (together with the Maybe value itself). Those are two representations of the same idea. Furthermore, this is nearly identical to the <code>maybe</code> function in Haskell's <code>Data.Maybe</code> module. I found if fitting, therefore, to name the function <code>maybeF</code>. </p> <h3 id="d8a0eed800de48a994085c419b7b5379"> Basis <a href="#d8a0eed800de48a994085c419b7b5379" title="permalink">#</a> </h3> <p> You can implement most other useful functionality with <code>maybeF</code>. Here's the <code>Functor</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;=&nbsp;maybeF&nbsp;nothingF&nbsp;(justF&nbsp;.&nbsp;f)</pre> </p> <p> Since <code>fmap</code> should be a structure-preserving map, you'll have to map the <em>nothing</em> case to the <em>nothing</em> case, and <em>just</em> to <em>just</em>. When calling <code>maybeF</code>, you must supply a value for the <em>nothing</em> case and a function to deal with the <em>just</em> case. The <em>nothing</em> case is easy to handle: just use <code>nothingF</code>. </p> <p> In the <em>just</em> case, first apply the function <code>f</code> to map from <code>a</code> to <code>b</code>, and then use <code>justF</code> to wrap the <code>b</code> value in a new <code>MaybeFix</code> container to get <code>MaybeFix b</code>. </p> <p> <code>Applicative</code> is a little harder, but not much: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;=&nbsp;justF &nbsp;&nbsp;f&nbsp;&lt;*&gt;&nbsp;x&nbsp;=&nbsp;maybeF&nbsp;nothingF&nbsp;(&lt;$&gt;&nbsp;x)&nbsp;f</pre> </p> <p> The <code>pure</code> function is just <em>justF</em> (pun intended). The <em>apply</em> operator <code>&lt;*&gt;</code> is more complex. </p> <p> Both <code>f</code> and <code>x</code> surrounding <code>&lt;*&gt;</code> are <code>MaybeFix</code> values: <code>f</code> is <code>MaybeFix (a -&gt; b)</code>, and <code>x</code> is <code>MaybeFix a</code>. While it's becoming increasingly clear that you can use a catamorphism like <code>maybeF</code> to implement most other functionality, to which <code>MaybeFix</code> value should you apply it? To <code>f</code> or <code>x</code>? </p> <p> Both are possible, but the code looks (in my opinion) more readable if you apply it to <code>f</code>. Again, when <code>f</code> is <em>nothing</em>, return <code>nothingF</code>. When <code>f</code> is <em>just</em>, use the functor instance to map <code>x</code> (using the infix <code>fmap</code> alias <code>&lt;$&gt;</code>). </p> <p> The <code>Monad</code> instance, on the other hand, is almost trivial: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;x&nbsp;&gt;&gt;=&nbsp;f&nbsp;=&nbsp;maybeF&nbsp;nothingF&nbsp;f&nbsp;x</pre> </p> <p> As usual, map <em>nothing</em> to <em>nothing</em> by supplying <code>nothingF</code>. <code>f</code> is already a function that returns a <code>MaybeFix b</code> value, so just use that. </p> <p> The <code>Foldable</code> instance is likewise trivial (although, as you'll see below, you can make it even more trivial): </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;foldMap&nbsp;=&nbsp;maybeF&nbsp;mempty</pre> </p> <p> The <code>foldMap</code> function must return a <code>Monoid</code> instance, so for the <em>nothing</em> case, simply return the identity, <em>mempty</em>. Furthermore, <code>foldMap</code> takes a function <code>a -&gt; m</code>, but since the <code>foldMap</code> implementation is <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free</a>, you can't 'see' that function as an argument. </p> <p> Finally, for the sake of completeness, here's the <code>Traversable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;maybeF&nbsp;(pure&nbsp;nothingF)&nbsp;(justF&nbsp;&lt;$&gt;)</pre> </p> <p> In the <em>nothing</em> case, you can put <code>nothingF</code> into the desired <code>Applicative</code> with <code>pure</code>. In the <em>just</em> case you can take advantage of the desired <code>Applicative</code> being also a <code>Functor</code> by simply mapping the inner value(s) with <code>justF</code>. </p> <p> Since the <code>Applicative</code> instance for <code>MaybeFix</code> equals <code>pure</code> to <code>justF</code>, you could alternatively write the <code>Traversable</code> instance like this: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;maybeF&nbsp;(pure&nbsp;nothingF)&nbsp;(pure&nbsp;&lt;$&gt;)</pre> </p> <p> I like this alternative less, since I find it confusing. The two appearances of <code>pure</code> relate to two different types. The <code>pure</code> in <code>pure nothingF</code> has the type <code>MaybeFix a -&gt; f (MaybeFix a)</code>, while the <code>pure</code> in <code>pure&nbsp;&lt;$&gt;</code> has the type <code>a -&gt; MaybeFix a</code>! </p> <p> Both implementations work the same, though: </p> <p> <pre>Prelude Fix Maybe&gt; sequenceA (justF ("foo", 42)) ("foo",MaybeFix {unMaybeFix = Fix (JustF 42)})</pre> </p> <p> Here, I'm using the <code>Applicative</code> instance of <code>(,) String</code>. </p> <p> Finally, you can implement conversions to and from the standard <code>Maybe</code> type, using <code>ana</code> as the dual of <code>cata</code>: </p> <p> <pre><span style="color:#2b91af;">toMaybe</span>&nbsp;::&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a toMaybe&nbsp;=&nbsp;maybeF&nbsp;Nothing&nbsp;<span style="color:blue;">return</span> <span style="color:#2b91af;">fromMaybe</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a fromMaybe&nbsp;=&nbsp;MaybeFix&nbsp;.&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;&nbsp;Nothing&nbsp;=&nbsp;NothingF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;(Just&nbsp;x)&nbsp;=&nbsp;JustF&nbsp;x</pre> </p> <p> This demonstrates that <code>MaybeFix</code> is isomorphic to <code>Maybe</code>, which again establishes that <code>maybeF</code> and <code>maybe</code> are equivalent. </p> <h3 id="2ec047e5122b4750a10cbe2012285524"> Alternatives <a href="#2ec047e5122b4750a10cbe2012285524" title="permalink">#</a> </h3> <p> As usual, the above <code>maybeF</code> isn't the only possible catamorphism. A trivial variation is to flip its arguments, but other variations exist. </p> <p> It's a recurring observation that a catamorphism is just a generalisation of a <em>fold</em>. In the above code, the <code>Foldable</code> instance already looked as simple as anyone could desire, but another variation of a catamorphism for Maybe is this gratuitously embellished definition: </p> <p> <pre><span style="color:#2b91af;">maybeF</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c maybeF&nbsp;f&nbsp;n&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unMaybeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;NothingF&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;f&nbsp;x&nbsp;n</pre> </p> <p> This variation redundantly passes <code>n</code> as an argument to <code>f</code>, thereby changing the type of <code>f</code> to <code>a -&gt; c -&gt; c</code>. There's no particular motivation for doing this, apart from establishing that this catamorphism is exactly the same as the fold: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">foldr</span>&nbsp;=&nbsp;maybeF</pre> </p> <p> You can still implement the other instances as well, but the rest of the code suffers in clarity. Here's a few examples: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;=&nbsp;maybeF&nbsp;(<span style="color:blue;">const</span>&nbsp;.&nbsp;justF&nbsp;.&nbsp;f)&nbsp;nothingF <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;=&nbsp;justF &nbsp;&nbsp;f&nbsp;&lt;*&gt;&nbsp;x&nbsp;=&nbsp;maybeF&nbsp;(<span style="color:blue;">const</span>&nbsp;.&nbsp;(&lt;$&gt;&nbsp;x))&nbsp;nothingF&nbsp;f <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;x&nbsp;&gt;&gt;=&nbsp;f&nbsp;=&nbsp;maybeF&nbsp;(<span style="color:blue;">const</span>&nbsp;.&nbsp;f)&nbsp;nothingF&nbsp;x</pre> </p> <p> I find that the need to compose with <code>const</code> does nothing to improve the readability of the code, so this variation is mostly, I think, of academic interest. It does show, though, that the catamorphism of Maybe is isomorphic to its fold, as the diagram in the overview article indicated: </p> <p> <img src="/content/binary/catamorphism-and-fold-relations.png" alt="Catamorphisms and folds as sets, for various sum types."> </p> <p> You can demonstrate that this variation, too, is isomorphic to <code>Maybe</code> with a set of conversion: </p> <p> <pre><span style="color:#2b91af;">toMaybe</span>&nbsp;::&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a toMaybe&nbsp;=&nbsp;maybeF&nbsp;(<span style="color:blue;">const</span>&nbsp;.&nbsp;<span style="color:blue;">return</span>)&nbsp;Nothing <span style="color:#2b91af;">fromMaybe</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a fromMaybe&nbsp;=&nbsp;MaybeFix&nbsp;.&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;&nbsp;Nothing&nbsp;=&nbsp;NothingF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;(Just&nbsp;x)&nbsp;=&nbsp;JustF&nbsp;x</pre> </p> <p> Only <code>toMaybe</code> has changed, compared to above; the <code>fromMaybe</code> function remains the same. The only change to <code>toMaybe</code> is that the arguments have been flipped, and <code>return</code> is now composed with <code>const</code>. </p> <p> Since (according to <a href="http://amzn.to/13tGJ0f">Conceptual Mathematics</a>) isomorphisms are transitive this means that the two variations of <code>maybeF</code> are isomorphic. The latter, more complex, variation of <code>maybeF</code> is identical <code>foldr</code>, so we can consider the simpler, more frequently encountered variation a simplification of <em>fold</em>. </p> <h3 id="f88757d425a04e97956d89270b32c0c0"> Summary <a href="#f88757d425a04e97956d89270b32c0c0" title="permalink">#</a> </h3> <p> The catamorphism for Maybe is the same as its Church encoding: a pair made from a default value and a function. In Haskell's base library, this is simply called <code>maybe</code>. In the above C# code, it's called <code>Match</code>. </p> <p> This function is total, and you can implement any other functionality you need with it. I therefore consider it the canonical representation of Maybe, which is also why it annoys me that most Maybe implementations come equipped with partial functions like <code>fromJust</code>, or F#'s <code>Option.get</code>. Those functions shouldn't be part of a sane and reasonable Maybe API. You shouldn't need them. </p> <p> In this series of articles about catamorphisms, you've now seen the first example of catamorphism and fold coinciding. In the next article, you'll see another such example - probably the most well-known catamorphism example of them all. </p> <p> <strong>Next:</strong> <a href="/2019/05/27/list-catamorphism">List catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Peano catamorphism https://blog.ploeh.dk/2019/05/13/peano-catamorphism 2019-05-13T05:10:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for Peano numbers involves a base value and a successor function.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for <a href="https://en.wikipedia.org/wiki/Natural_number">natural numbers</a>, as well as how to identify it. The beginning of the article presents the catamorphism in C#, with examples. The rest of the article describes how I deduced the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <h3 id="742f4b9c0c014152a933961c10f98b66"> C# catamorphism <a href="#742f4b9c0c014152a933961c10f98b66" title="permalink">#</a> </h3> <p> In this article, I model natural numbers using <a href="https://en.wikipedia.org/wiki/Peano_axioms">Peano's model</a>, and I'll reuse the <a href="/2018/05/28/church-encoded-natural-numbers">Church-encoded implementation you've seen before</a>. The catamorphism for <code>INaturalNumber</code> is: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Cata&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;zero,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;succ) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Match(zero,&nbsp;p&nbsp;=&gt;&nbsp;p.Cata(succ(zero),&nbsp;succ)); }</pre> </p> <p> Notice that this is an extension method on <code>INaturalNumber</code>, taking two other arguments: a <code>zero</code> argument which will be returned when the number is <em>zero</em>, and a successor function to return the 'next' value based on a previous value. </p> <p> The <code>zero</code> argument is the easiest to understand. It's simply passed to <code>Match</code> so that this is the value that <code>Cata</code> returns when <code>n</code> is <em>zero</em>. </p> <p> The other argument to <code>Match</code> must be a <code>Func&lt;INaturalNumber, T&gt;</code>; that is, a function that takes an <code>INaturalNumber</code> as input and returns a value of the type <code>T</code>. You can supply such a function by using a lambda expression. This expression receives a predecessor <code>p</code> as input, and has to return a value of the type <code>T</code>. The only function available in this context, however, is <code>succ</code>, which has the type <code>Func&lt;T, T&gt;</code>. How can you make that work? </p> <p> As is often the case when programming with generics, it pays to <em>follow the types</em>. A <code>Func&lt;T, T&gt;</code> requires an input of the type <code>T</code>. Do you have any <code>T</code> objects around? </p> <p> The only available <code>T</code> object is <code>zero</code>, so you could call <code>succ(zero)</code> to produce another <code>T</code> value. While you could return that immediately, that'd ignore the predecessor <code>p</code>, so that's not going to work. Another option, which is the one that works, is to recursively call <code>Cata</code> with <code>succ(zero)</code> as the <code>zero</code> value, and <code>succ</code> as the second argument. </p> <p> What this accomplishes is that <code>Cata</code> keeps recursively calling itself until <code>n</code> is <em>zero</em>. The <code>zero</code> object, however, will be the result of repeated applications of <code>succ(zero)</code>. In other words, <code>succ</code> will be called as many times as the natural number. If <code>n</code> is 7, then <code>succ</code> will be called seven times, the first time with the original <code>zero</code> value, the next time with the result of <code>succ(zero)</code>, the third time with the result of <code>succ(succ(zero))</code>, and so on. If the number is 42, <code>succ</code> will be called 42 times. </p> <h3 id="633dae2048cd45ebaa17962710048c67"> Arithmetic <a href="#633dae2048cd45ebaa17962710048c67" title="permalink">#</a> </h3> <p> You can implement all the functionality you saw in the article on Church-encoded natural numbers. You can start gently by converting a Peano number into a normal C# <code>int</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Count(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Cata(0,&nbsp;x&nbsp;=&gt;&nbsp;1&nbsp;+&nbsp;x); }</pre> </p> <p> You can play with the functionality in <em>C# Interactive</em> to get a feel for how it works: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Eight.Count() 8 &gt; <span style="color:#2b91af;">NaturalNumber</span>.Five.Count() 5</pre> </p> <p> The <code>Count</code> extension method uses <code>Cata</code> to count the level of recursion. The <code>zero</code> value is, not surprisingly, <code>0</code>, and the successor function simply adds one to the previous number. Since the successor function runs as many times as encoded by the Peano number, and since the initial value is <code>0</code>, you get the integer value of the number when <code>Cata</code> exits. </p> <p> A useful building block you can write using <code>Cata</code> is a function to increment a natural number by one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Increment(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Cata(One,&nbsp;p&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(p)); }</pre> </p> <p> This, again, works as you'd expect: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Zero.Increment().Count() 1 &gt; <span style="color:#2b91af;">NaturalNumber</span>.Eight.Increment().Count() 9</pre> </p> <p> With the <code>Increment</code> method and <code>Cata</code>, you can easily implement addition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Cata(y,&nbsp;p&nbsp;=&gt;&nbsp;p.Increment()); }</pre> </p> <p> The trick here is to use <code>y</code> as the <code>zero</code> case for <code>x</code>. In other words, if <code>x</code> is <em>zero</em>, then <code>Add</code> should return <code>y</code>. If <code>x</code> isn't <em>zero</em>, then <code>Increment</code> it as many times as the number encodes, but starting at <code>y</code>. In other words, start with <code>y</code> and <code>Increment</code> <code>x</code> times. </p> <p> The catamorphism makes it much easier to implement arithmetic operation. Just consider multiplication, which wasn't the simplest implementation in the previous article. Now, it's as simple as this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Multiply(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Cata(Zero,&nbsp;p&nbsp;=&gt;&nbsp;p.Add(y)); }</pre> </p> <p> Start at <code>0</code> and simply <code>Add(y)</code> <code>x</code> times. </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Nine.Multiply(<span style="color:#2b91af;">NaturalNumber</span>.Four).Count() 36</pre> </p> <p> Finally, you can also implement some common predicates: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsZero(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Cata&lt;<span style="color:#2b91af;">IChurchBoolean</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(),&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>()); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsEven(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Cata&lt;<span style="color:#2b91af;">IChurchBoolean</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(),&nbsp;b&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(b)); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsOdd(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(n.IsEven()); }</pre> </p> <p> Particularly <code>IsEven</code> is elegant: It considers <code>zero</code> even, so simply uses a <code>new ChurchTrue()</code> object for that case. In all other cases, it alternates between <em>false</em> and <em>true</em> by negating the predecessor. </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Three.IsEven().ToBool() false</pre> </p> <p> It seems convincing that we can use <code>Cata</code> to implement all the other functionality we need. That seems to be a characteristic of a catamorphism. Still, how do we know that <code>Cata</code> is, in fact, the catamorphism for natural numbers? </p> <h3 id="05dbca489b8a49be830df87e13bfcae3"> Peano F-Algebra <a href="#05dbca489b8a49be830df87e13bfcae3" title="permalink">#</a> </h3> <p> As in the <a href="/2019/05/06/boolean-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. The <code>NatF</code> type comes from his article as well: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;NatF&nbsp;a&nbsp;=&nbsp;ZeroF&nbsp;|&nbsp;SuccF&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;ZeroF&nbsp;=&nbsp;ZeroF &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;(SuccF&nbsp;x)&nbsp;=&nbsp;SuccF&nbsp;$&nbsp;f&nbsp;x</pre> </p> <p> You can use the fixed point of this functor to define numbers with a shared type. Here's just the first ten: </p> <p> <pre><span style="color:#2b91af;">zeroF</span>,&nbsp;<span style="color:#2b91af;">oneF</span>,&nbsp;<span style="color:#2b91af;">twoF</span>,&nbsp;<span style="color:#2b91af;">threeF</span>,&nbsp;<span style="color:#2b91af;">fourF</span>,&nbsp;<span style="color:#2b91af;">fiveF</span>,&nbsp;<span style="color:#2b91af;">sixF</span>,&nbsp;<span style="color:#2b91af;">sevenF</span>,&nbsp;<span style="color:#2b91af;">eightF</span>,&nbsp;<span style="color:#2b91af;">nineF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> zeroF&nbsp;&nbsp;=&nbsp;Fix&nbsp;ZeroF oneF&nbsp;&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;zeroF twoF&nbsp;&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;oneF threeF&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;twoF fourF&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;threeF fiveF&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;fourF sixF&nbsp;&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;fiveF sevenF&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;sixF eightF&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;sevenF nineF&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;eightF</pre> </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="f0e66c873a034830a4b069229971299a"> Haskell catamorphism <a href="#f0e66c873a034830a4b069229971299a" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>NatF</code>), and an object <code>a</code>, but you still need to find a morphism <code>NatF a -&gt; a</code>. </p> <p> As in the previous article, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>natF&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;ZeroF&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(SuccF&nbsp;predecessor)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>a</code> from the <code>ZeroF</code> case? You could pass an argument to the <code>natF</code> function: </p> <p> <pre>natF&nbsp;z&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;ZeroF&nbsp;=&nbsp;z &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(SuccF&nbsp;predecessor)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> In the <code>SuccF</code> case, <code>predecessor</code> is already of the polymorphic type <code>a</code>, so instead of returning a constant value, you can supply a function as an argument to <code>natF</code> and use it in that case: </p> <p> <pre><span style="color:#2b91af;">natF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a natF&nbsp;z&nbsp;next&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;ZeroF&nbsp;=&nbsp;z &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(SuccF&nbsp;predecessor)&nbsp;=&nbsp;next&nbsp;predecessor</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>NatF</code>, the compiler infers that the <code>alg</code> function has the type <code>NatF a -&gt; a</code>, which is just what you need! </p> <p> For good measure, I should point out that, as usual, the above <code>natF</code> function isn't the only possible catamorphism. Trivially, you can flip the order of the arguments, and this would also be a catamorphism. These two alternatives are isomorphic. </p> <p> The <code>natF</code> function identifies the Peano number catamorphism, which is equivalent to the C# representation in the beginning of the article. I called the function <code>natF</code>, because there's a tendency in Haskell to name the 'case analysis' or catamorphism after the type, just with a lower-case initial letter. </p> <h3 id="e78ae79059e14f4b92f525393cc74861"> Basis <a href="#e78ae79059e14f4b92f525393cc74861" title="permalink">#</a> </h3> <p> A catamorphism can be used to implement most (if not all) other useful functionality, like all of the above C# functionality. In fact, I wrote the Haskell code first, and then translated my implementations into the above C# extension methods. This means that the following functions apply the same reasoning: </p> <p> <pre><span style="color:#2b91af;">evenF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> evenF&nbsp;=&nbsp;natF&nbsp;trueF&nbsp;notF <span style="color:#2b91af;">oddF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> oddF&nbsp;=&nbsp;notF&nbsp;.&nbsp;evenF <span style="color:#2b91af;">incF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> incF&nbsp;=&nbsp;natF&nbsp;oneF&nbsp;$&nbsp;Fix&nbsp;.&nbsp;SuccF <span style="color:#2b91af;">addF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> addF&nbsp;x&nbsp;y&nbsp;=&nbsp;natF&nbsp;y&nbsp;incF&nbsp;x <span style="color:#2b91af;">multiplyF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> multiplyF&nbsp;x&nbsp;y&nbsp;=&nbsp;natF&nbsp;zeroF&nbsp;(addF&nbsp;y)&nbsp;x</pre> </p> <p> Here are some GHCi usage examples: </p> <p> <pre>Prelude Boolean Nat&gt; evenF eightF Fix TrueF Prelude Boolean Nat&gt; toNum $ multiplyF sevenF sixF 42</pre> </p> <p> The <code>toNum</code> function corresponds to the above <code>Count</code> C# method. It is, again, based on <code>cata</code>. You can use <code>ana</code> to convert the other way: </p> <p> <pre><span style="color:#2b91af;">toNum</span>&nbsp;::&nbsp;<span style="color:blue;">Num</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a toNum&nbsp;=&nbsp;natF&nbsp;0&nbsp;(+&nbsp;1) <span style="color:#2b91af;">fromNum</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Num</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> fromNum&nbsp;=&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;0&nbsp;=&nbsp;ZeroF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;x&nbsp;=&nbsp;SuccF&nbsp;$&nbsp;x&nbsp;-&nbsp;1</pre> </p> <p> This demonstrates that <code>Fix NatF</code> is isomorphic to <code>Num</code> instances, such as <code>Integer</code>. </p> <h3 id="d09e79446af14875a42f66869e10f33a"> Summary <a href="#d09e79446af14875a42f66869e10f33a" title="permalink">#</a> </h3> <p> The catamorphism for Peano numbers is a pair consisting of a zero value and a successor function. The most common description of catamorphisms that I've found emphasise how a catamorphism is like a <em>fold;</em> an operation you can use to reduce a data structure like a list or a tree to a single value. This is what happens here, but even so, the <code>Fix NatF</code> type isn't a <code>Foldable</code> instance. The reason is that while <code>NatF</code> is a polymorphic type, its fixed point <code>Fix NatF</code> isn't. Haskell's <code>Foldable</code> type class requires foldable containers to be polymorphic (what C# programmers would call 'generic'). </p> <p> When I first ran into the concept of a <em>catamorphism</em>, it was invariably described as a 'generalisation of fold'. The examples shown were always how the catamorphism for linked list is the same as its <em>fold</em>. I found such explanations unhelpful, because I couldn't understand how those two concepts differ. </p> <p> The purpose with this article series is to show just how much more general the abstraction of a catamorphism is. In this article you saw how an infinitely recursive data structure like Peano numbers have a catamorphism, even though it isn't a parametrically polymorphic type. In the next article, though, you'll see the first example of a polymorphic type where the catamorphism coincides with the fold. </p> <p> <strong>Next:</strong> <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Boolean catamorphism https://blog.ploeh.dk/2019/05/06/boolean-catamorphism 2019-05-06T12:30:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for Boolean values is just the common ternary operator.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for Boolean values, as well as how you identify it. The beginning of this article presents the catamorphism in C#, with a simple example. The rest of the article describes how I deduced the catamorphism. That part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <h3 id="35155b758274445cbe57f75d730a4eb6"> C# catamorphism <a href="#35155b758274445cbe57f75d730a4eb6" title="permalink">#</a> </h3> <p> The catamorphism for Boolean values is the familiar <a href="https://en.wikipedia.org/wiki/%3F:">ternary conditional operator</a>: </p> <p> <pre>&gt; <span style="color:#2b91af;">DateTime</span>.Now.Day&nbsp;%&nbsp;2&nbsp;==&nbsp;0&nbsp;?&nbsp;<span style="color:#a31515;">&quot;Even&nbsp;date&quot;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&quot;Odd&nbsp;date&quot;</span> "Odd date"</pre> </p> <p> Given a Boolean expression, you basically provide two values: one to use in case the Boolean expression is <em>true</em>, and one to use in case it's <em>false</em>. </p> <p> For <a href="/2018/05/24/church-encoded-boolean-values">Church-encoded Boolean values</a>, the catamorphism looks like this: </p> <p> <pre><span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;trueCase,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;falseCase);</pre> </p> <p> This is an instance method where you must, again, supply two alternatives. When the instance represents <em>true</em>, you'll get the left-most value <code>trueCase</code>; otherwise, when the instance represents <em>false</em>, you'll get the right-most value <code>falseCase</code>. </p> <p> The catamorphism turns out to be the same as the <a href="/2018/05/22/church-encoding">Church encoding</a>. This seems to be a recurring pattern. </p> <h3 id="cd81cb92ed2d42f8bc0ad5adbde4b014"> Alternatives <a href="#cd81cb92ed2d42f8bc0ad5adbde4b014" title="permalink">#</a> </h3> <p> To be accurate, there's more than one catamorphism for Boolean values. It's only by convention that the value corresponding to <em>true</em> goes on the left, and the <em>false</em> value goes to the right. You could flip the arguments, and it would still be a catamorphism. This is, in fact, what Haskell's <code>Data.Bool</code> module does: </p> <p> <pre>Prelude Data.Bool&gt; bool "Odd date" "Even date" $ even date "Odd date"</pre> </p> <p> The <a href="http://hackage.haskell.org/package/base/docs/Data-Bool.html">module documentation</a> calls this the <em>"Case analysis for the <code>Bool</code> type"</em>, instead of a catamorphism, but the two representations are isomorphic: <blockquote> "This is equivalent to <code>if p then y else x</code>; that is, one can think of it as an if-then-else construct with its arguments reordered." </blockquote> This is another recurring result. There's typically more than one catamorphism, but the alternatives are isomorphic. In this article series, I'll mostly present the alternative that strikes me as the one you'll encounter most frequently. </p> <h3 id="60235fb428d14785a5aeea440c05cce5"> Fix <a href="#60235fb428d14785a5aeea440c05cce5" title="permalink">#</a> </h3> <p> In this and future articles, I'll derive the catamorphism from an F-Algebra. For an introduction to F-Algebras and fixed points, I'll refer you to <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on the topic</a>. In it, he presents a generic data type for a fixed point, as well as polymorphic functions for catamorphisms and anamorphisms. While they're available in his article, I'll repeat them here for good measure: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;Fix&nbsp;f&nbsp;=&nbsp;Fix&nbsp;{&nbsp;unFix&nbsp;::&nbsp;f&nbsp;(Fix&nbsp;f)&nbsp;} <span style="color:#2b91af;">cata</span>&nbsp;::&nbsp;<span style="color:blue;">Functor</span>&nbsp;f&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(f&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;f&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a cata&nbsp;alg&nbsp;=&nbsp;alg&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(cata&nbsp;alg)&nbsp;.&nbsp;unFix <span style="color:#2b91af;">ana</span>&nbsp;::&nbsp;<span style="color:blue;">Functor</span>&nbsp;f&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;f ana&nbsp;coalg&nbsp;=&nbsp;Fix&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(ana&nbsp;coalg)&nbsp;.&nbsp;coalg</pre> </p> <p> This should be recognisable from Bartosz Milewski's article. With one small exception, this is just a copy of the code shown there. </p> <h3 id="6b0fdc2b04e540d19322f0e30c00e86c"> Boolean F-Algebra <a href="#6b0fdc2b04e540d19322f0e30c00e86c" title="permalink">#</a> </h3> <p> While F-Algebras and fixed points are mostly used for recursive data structures, you can also define an F-Algebra for a non-recursive data structure. As data types go, they don't get much simpler than Boolean values, which are just two mutually exclusive cases. In order to make a <code>Functor</code> out of the definition, though, you can equip it with a <em>carrier type:</em> </p> <p> <pre><span style="color:blue;">data</span>&nbsp;BoolF&nbsp;a&nbsp;=&nbsp;TrueF&nbsp;|&nbsp;FalseF&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;TrueF&nbsp;=&nbsp;&nbsp;TrueF &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;FalseF&nbsp;=&nbsp;FalseF</pre> </p> <p> The <code>Functor</code> instance simply ignores the carrier type and just returns <code>TrueF</code> and <code>FalseF</code>, respectively. It'd seem that nothing happens, but at the type level, this is still a translation from <code>BoolF a</code> to <code>BoolF b</code>. Not much of a function, perhaps, but definitely an <em>endofunctor</em>. </p> <p> Another note that may be in order here, as well as for all future articles in this series, is that you'll notice that most types and custom functions come with the <code>F</code> suffix. This is simply a suffix I've added to avoid conflicts with built-in types, values, and functions, such as <code>Bool</code>, <code>True</code>, <code>and</code>, and so on. The <code>F</code> is for <em>F-Algebra</em>. </p> <p> You can lift these values into <code>Fix</code> in order to make it fit with the <code>cata</code> function: </p> <p> <pre><span style="color:#2b91af;">trueF</span>,&nbsp;<span style="color:#2b91af;">falseF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> trueF&nbsp;&nbsp;=&nbsp;Fix&nbsp;&nbsp;TrueF falseF&nbsp;=&nbsp;Fix&nbsp;FalseF</pre> </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="a28e972fc7eb45038427cff258c0c8f2"> Haskell catamorphism <a href="#a28e972fc7eb45038427cff258c0c8f2" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>BoolF</code>), and an object <code>a</code>, but you still need to find a morphism <code>BoolF a -&gt; a</code>. At first glance, this seems impossible, because neither <code>TrueF</code> nor <code>FalseF</code> actually contain a value of the type <code>a</code>. How, then, can you conjure an <code>a</code> value out of thin air? </p> <p> The <code>cata</code> function has the answer. </p> <p> What you can do is to start writing the function that will become the catamorphism, basing it on <code>cata</code>: </p> <p> <pre>boolF&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;TrueF&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;FalseF&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>a</code> from the <code>TrueF</code> case? You could pass an argument to the <code>boolF</code> function: </p> <p> <pre>boolF&nbsp;x&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;TrueF&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;FalseF&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> That seems promising, so do that for the <code>FalseF</code> case as well: </p> <p> <pre><span style="color:#2b91af;">boolF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a boolF&nbsp;x&nbsp;y&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;TrueF&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;FalseF&nbsp;=&nbsp;y</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>BoolF</code>, the compiler infers that the <code>alg</code> function has the type <code>BoolF a -&gt; a</code>, which is just what you need! </p> <p> The <code>boolF</code> function identifies the Boolean catamorphism, which is equivalent to representations in the beginning of the article. I called the function <code>boolF</code>, because there's a tendency in Haskell to name the 'case analysis' or catamorphism after the type, just with a lower-case initial letter. </p> <p> You can use the <code>boolF</code> function just like the above ternary operator: </p> <p> <pre>Prelude Boolean Nat&gt; boolF "Even date" "Odd date" $ evenF dateF "Odd date"</pre> </p> <p> Here, I've also used <code>evenF</code> from the <code>Nat</code> module shown in the next article in the series. </p> <p> From the above definition of <code>boolF</code>, it should also be clear that you can arrive at the alternative catamorphism defined by <code>Data.Bool.bool</code> by simply flipping <code>x</code> and <code>y</code>. </p> <h3 id="54886f1be8684dd4a5909e4d50b7d5dc"> Basis <a href="#54886f1be8684dd4a5909e4d50b7d5dc" title="permalink">#</a> </h3> <p> A catamorphism can be used to implement most (if not all) other useful functionality. For Boolean values, that would be the standard Boolean operations <em>and</em>, <em>or</em>, and <em>not:</em> </p> <p> <pre><span style="color:#2b91af;">andF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> andF&nbsp;x&nbsp;y&nbsp;=&nbsp;boolF&nbsp;y&nbsp;falseF&nbsp;x <span style="color:#2b91af;">orF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> orF&nbsp;x&nbsp;y&nbsp;=&nbsp;boolF&nbsp;trueF&nbsp;y&nbsp;x <span style="color:#2b91af;">notF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> notF&nbsp;=&nbsp;boolF&nbsp;falseF&nbsp;trueF</pre> </p> <p> They work as you'd expect them to work: </p> <p> <pre>Prelude Boolean&gt; andF trueF falseF Fix FalseF Prelude Boolean&gt; orF trueF falseF Fix TrueF Prelude Boolean&gt; orF (notF trueF) falseF Fix FalseF</pre> </p> <p> You can also implement conversion to and from the built-in <code>Bool</code> type: </p> <p> <pre><span style="color:#2b91af;">toBool</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> toBool&nbsp;=&nbsp;boolF&nbsp;True&nbsp;False <span style="color:#2b91af;">fromBool</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Bool</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> fromBool&nbsp;=&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;&nbsp;True&nbsp;=&nbsp;TrueF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;False&nbsp;=&nbsp;FalseF</pre> </p> <p> This demonstrates that <code>Fix BoolF</code> is isomorphic to <code>Bool</code>. </p> <h3 id="327411e72cea46bbb1f6fe167738c7b2"> Summary <a href="#327411e72cea46bbb1f6fe167738c7b2" title="permalink">#</a> </h3> <p> The catamorphism for Boolean values is a function, method, or operator akin to the familiar ternary conditional operator. The most common descriptions of catamorphisms that I've found emphasise how a catamorphism is like a <em>fold;</em> an operation you can use to reduce a data structure like a list or a tree to a single value. In that light, it may be surprising that something as simple as Boolean values have an associated catamorphism. </p> <p> Since <code>Fix BoolF</code> is isomorphic to <code>Bool</code>, you may wonder what the point is. Why define this data type, and implement functions like <code>andF</code>, <code>orF</code>, and <code>notF</code>? </p> <p> The code presented here is nothing but an analysis tool. It's a way to identify the catamorphism for Boolean values. </p> <p> <strong>Next:</strong> <a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Catamorphisms https://blog.ploeh.dk/2019/04/29/catamorphisms 2019-04-29T18:31:00+00:00 Mark Seemann <div id="post"> <p> <em>A catamorphism is a general abstraction that enables you to handle multiple values, for example in order to reduce them to a single value.</em> </p> <p> This article series is part of <a href="/2017/10/04/from-design-patterns-to-category-theory">an even larger series of articles about the relationship between design patterns and category theory</a>. In another article series in this big series of articles, you learned about <a href="/2018/03/19/functors-applicatives-and-friends">functors, applicatives, and other types of data containers</a>. </p> <p> You may have heard about <em>map-reduce</em> architectures. Much software can be designed around two general types of operations: those that <em>map</em> data, and those that <em>reduce</em> data. A <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">functor is a container of data</a> that supports structure-preserving maps. Thus, you can think of <a href="/2018/03/22/functors">functors</a> as the general abstraction for map operations (also sometimes called <em>projections</em>). Does a similar universal abstraction exist for operations that reduce data? </p> <p> Yes, that abstraction is called a <em>catamorphism</em>. </p> <h3 id="bb64d005b16b49f892c00824ef803997"> Aggregation <a href="#bb64d005b16b49f892c00824ef803997" title="permalink">#</a> </h3> <p> <em>Catamorphism</em> is an intimidating word, so let's start with an example. You often have a collection of values that you'd like to reduce to a single value. Such a collection can contain arbitrarily complex objects, but I'll keep it simple and start with a collection of numbers: </p> <p> <pre><span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;};</pre> </p> <p> This particular list of numbers is an array, but that's not important. What comes next works for any <code>IEnumerable&lt;T&gt;</code>, including arrays. I only chose an array because the C# syntax for array creation is more compact than for other collection types. </p> <p> How do you reduce those seven numbers to a single number? That depends on what you want that number to tell you. One option is to add the numbers together. There's a specific, built-in function for that: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;}.Sum(); 100647</pre> </p> <p> The <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sum">Sum</a> extension method is a one of many built-in functions that enable you to reduce a list of numbers to a single number: <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.average">Average</a>, <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.max">Max</a>, <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.count">Count</a>, and so on. </p> <p> What do you do, though, if you need to reduce many values to one, and there's no existing function for that? What if, for example, you need to add all the numbers using <a href="/2018/07/16/angular-addition-monoid">modulo 360 addition</a>? </p> <p> In that case, you use <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate">Aggregate</a>: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;}.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) 207</pre> </p> <p> The way to interpret this result is that the initial array represents a sequence of rotations (measured in degrees), and the result is the final angle after all the rotations have completed. </p> <p> In other (functional) languages, such a 'reduce' operation is called a <em>fold</em>. The metaphor, I suppose, is that you fold multiple values together, two by two. </p> <p> A <em>fold</em> is a catamorphism, but a catamorphism is a more general abstraction. For some data structures, the catamorphism is more powerful than the fold, but for collections, there's no difference. </p> <p> There's one edge case we need to be aware of, though. What if the collection is empty? </p> <h3 id="65483950f21d453ebe4e8949eac5751f"> Aggregation of empty containers <a href="#65483950f21d453ebe4e8949eac5751f" title="permalink">#</a> </h3> <p> What happens if you attempt to aggregate an empty collection? </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0].Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) <span style="color:red;">Sequence contains no elements + System.Linq.Enumerable.Aggregate&lt;TSource&gt;(IEnumerable&lt;TSource&gt;, Func&lt;TSource, TSource, TSource&gt;)</span></pre> </p> <p> The <code>Aggregate</code> method throws an exception because it doesn't know how to deal with empty collections. The lambda expression you supply tells the <code>Aggregate</code> method how to combine two values into one. This is, for instance, how <a href="/2017/12/11/semigroups-accumulate">semigroups accumulate</a>. </p> <p> The lambda expression handles all cases where you have two or more values. If you have only a single value, then that's no problem either: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;1337&nbsp;}.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) 1337</pre> </p> <p> In that case, the lambda expression isn't involved at all, because the single value is simply returned without modification. In this example, this could even be interpreted as being incorrect, since you'd expect the result to be 257 (<code>1337 % 360</code>). </p> <p> It's safer to use the <code>Aggregate</code> overload that takes a <em>seed</em> value: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0].Aggregate(0,&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) 0</pre> </p> <p> Not only does that gracefully handle empty collections, it also gives you a 'better' result for a single value: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;1337&nbsp;}.Aggregate(0,&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) 257</pre> </p> <p> This works better because the method always starts with the <em>seed</em> value, which means that even if there's only a single value (<code>1337</code>), the lambda expression still runs (<code>(0 + 1337) % 360</code>). </p> <p> This overload of <code>Aggregate</code> has a different type, though: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TAccumulate</span>&nbsp;Aggregate&lt;<span style="color:#2b91af;">TSource</span>,&nbsp;<span style="color:#2b91af;">TAccumulate</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TSource</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TAccumulate</span>&nbsp;seed, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">TAccumulate</span>,&nbsp;<span style="color:#2b91af;">TSource</span>,&nbsp;<span style="color:#2b91af;">TAccumulate</span>&gt;&nbsp;func);</pre> </p> <p> Notice that the <code>func</code> doesn't require the accumulator to have the same type as elements from the <code>source</code> collection. This enables you to translate on the fly, so to speak. You can still use binary operations like the above modulo 360 addition, because that just implies that both <code>TSource</code> and <code>TAccumulate</code> are <code>int</code>. </p> <p> With this overload, you could, for example, use <a href="/2018/07/16/angular-addition-monoid">the Angle class</a> to perform the work: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;} . .Aggregate(<span style="color:#2b91af;">Angle</span>.Identity,&nbsp;(a,&nbsp;i)&nbsp;=&gt;&nbsp;a.Add(<span style="color:#2b91af;">Angle</span>.FromDegrees(i))) [{ Angle = 207° }]</pre> </p> <p> Now the <code>seed</code> argument is <code>Angle.Identity</code>, which implies that <code>TAccumulate</code> is <code>Angle</code>. The <code>source</code> is still a collection of numbers, so <code>TSource</code> is <code>int</code>. Hence, I called the angle <code>a</code> and the integer <code>i</code> in the lambda expression. The output is an <code>Angle</code> object that represents 207°. </p> <p> That <code>Aggregate</code> overload is the catamorphism for collections. It reduces a collection to an object. </p> <h3 id="5a964dee9b1f4cdd8427ce0a0806d65d"> Catamorphisms and folds <a href="#5a964dee9b1f4cdd8427ce0a0806d65d" title="permalink">#</a> </h3> <p> Is <em>catamorphism</em> just an intimidating word for <em>aggregate</em>, <em>accumulate</em>, <em>fold</em>, or <em>reduce?</em> </p> <p> It took me a long time to be able to tell the difference, because in many cases, it seems that there's no difference. The purpose of this article series is to make the distinction clearer. In short, a catamorphism is a more general concept. </p> <p> <img src="/content/binary/catamorphism-and-fold-relations.png" alt="Catamorphisms and folds as sets, for various sum types."> </p> <p> For some data structures, such as <a href="/2018/05/24/church-encoded-boolean-values">Boolean values</a>, or <a href="/2018/05/28/church-encoded-natural-numbers">Peano numbers</a>, the catamorphism is all there is; no fold exists. For other data structures, such as <a href="/2018/06/04/church-encoded-maybe">Maybe</a> or collections, the catamorphism and the fold coincide. Still other data structures, such as <a href="/2018/06/11/church-encoded-either">Either</a> and <a href="/2018/08/06/a-tree-functor">trees</a>, support folding, but the fold is based on the catamorphism. For those types, there are operations you can do with the catamorphism that are impossible to implement with the <em>fold</em> function. One example is that a tree's catamorphism enables you to count its leaves; you can't do that with its <em>fold</em> function. </p> <p> You'll see plenty of examples in this article series: </p> <p> <ul> <li><a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a></li> <li><a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a></li> <li><a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a></li> <li><a href="/2019/05/27/list-catamorphism">List catamorphism</a></li> <li><a href="/2023/08/07/nonempty-catamorphism">NonEmpty catamorphism</a></li> <li><a href="/2019/06/03/either-catamorphism">Either catamorphism</a></li> <li><a href="/2019/06/10/tree-catamorphism">Tree catamorphism</a></li> <li><a href="/2019/08/05/rose-tree-catamorphism">Rose tree catamorphism</a></li> <li><a href="/2019/06/24/full-binary-tree-catamorphism">Full binary tree catamorphism</a></li> <li><a href="/2019/07/08/payment-types-catamorphism">Payment types catamorphism</a></li> </ul> </p> <p> Each of these articles will contain a fair amount of <a href="https://www.haskell.org">Haskell</a> code, but even if you're an object-oriented programmer who doesn't read Haskell, you should still scan them, as I'll start each with some C# examples. The Haskell code, by the way, is <a href="https://github.com/ploeh/FAlgebras">available on GitHub</a>. </p> <h3 id="cc687d1bebed47229cbdeffdf98fd666"> Greek <a href="#cc687d1bebed47229cbdeffdf98fd666" title="permalink">#</a> </h3> <p> When encountering a word like <em>catamorphism</em>, your reaction might be: <blockquote> "Catamorphism?! What does that even mean? It's all Greek to me." </blockquote> Indeed, it's Greek, as is so much of mathematical terminology. The <em>cata</em> prefix means 'down'; lots of words start with <em>cata</em>, like <em>catastrophe</em>, <em>catalogue</em>, <em>catatonia</em>, <em>catacomb</em>, etc. </p> <p> The <em>morph</em> suffix generally means 'shape'. While the <em>cata</em> prefix appears in common words like <em>catastrophe</em>, the <em>morph</em> suffix mostly appears in more academic contexts. Programmers will probably have encountered <em>polymorphism</em> and <em>skeuomorphism</em>, not to mention <a href="/2018/01/08/software-design-isomorphisms">isomorphism</a>. While <em>morphism</em> is heavily used in mathematics, other sciences use the suffix too, like <em>dimorphism</em> in biology. </p> <p> In category theory, a <em>morphism</em> is basically just an arrow that points from one object to another. Think of it as a function. </p> <p> If a morphism is just a function, why don't we just call it that, then? Is it really necessary with this intimidating terminology? Yes and no. </p> <p> If someone had originally figured all of this out in the context of mainstream programming, he or she would probably have used friendlier names, like <em>condense</em>, <em>reduce</em>, <em>fold</em>, and so on. This would have been more encouraging, although <a href="/2017/10/05/monoids-semigroups-and-friends">I'm not sure it would have been better</a>. </p> <p> In software architecture we use many overloaded terms. For example, what's a <em>service</em>, or a <em>client?</em> What does <em>tier</em> mean? Is it the same as a <em>layer</em>, or is it something different? What's the <a href="http://tomasp.net/blog/2015/library-frameworks">difference between a library and a framework</a>? </p> <p> At least a word like <em>catamorphism</em> is concise. It's not in common use, so isn't overloaded and vague. </p> <p> Another, more pragmatic, concern is that whether you like it or not, the terminology is already established. Mathematicians decided to name the concept <em>catamorphism</em>. While the name may seem intimidating, I prefer to teach concepts like these using established terminology. This means that if my articles are unclear, you can do further research with other resources. That's the benefit of established terminology, whether you like the specific words or not. </p> <h3 id="c179dad7693c48c2ae592d33cb2be792"> Summary <a href="#c179dad7693c48c2ae592d33cb2be792" title="permalink">#</a> </h3> <p> You can compose entire applications based on the abstractions of <em>map</em> and <em>reduce</em>. You can see one example of such a system in my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">A Functional Architecture with F#</a> Pluralsight course. </p> <p> The terms <em>map</em> and <em>reduce</em> may, however, not be helpful, because it may not be clear exactly what types of data you can map, and what types you can reduce. One of the most important goals of this overall article series about universal abstractions is to help you identify when such software architectures apply. This is more often that you think. </p> <p> What sort of data can you map? You can map <em>functors</em>. While hardly finite, there's a catalogue of well-known functors, of which I've covered some, but not all. That catalogue contains data containers like <a href="/2018/03/26/the-maybe-functor">Maybe</a>, <a href="/2018/08/06/a-tree-functor">Tree</a>, <a href="/2018/09/10/the-lazy-functor">lazy computations</a>, <a href="/2018/09/24/asynchronous-functors">tasks</a>, and perhaps a score more. The catalogue of (actually useful) functors has, in my experience, a manageable size. </p> <p> Likewise you could ask: What sort of data can you reduce? How do you implement that reduction? Again, there's a compact set of well-known catamorphisms. How do you reduce a collection? You use its catamorphism (which is equal to a fold). How do you reduce a tree? You use its catamorphism. How do you reduce an Either object? You use its catamorphism. </p> <p> When we learn new programming languages, new libraries, new frameworks, we gladly invest time in learning hundreds, if not thousands, of keywords, APIs, extensibility points, and so on. May I offer, for your consideration, that your mental resources are better spent learning only a handful of universal abstractions? </p> <p> <strong>Next:</strong> <a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="673934773ecd4263a557599471eaf57c"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#673934773ecd4263a557599471eaf57c">#</a></div> <div class="comment-content"> <blockquote> In other (functional) languages, such a 'reduce' operation is called a <em>fold</em>. The metaphor, I suppose, is that you fold multiple values together, two by two. <br> ...the <code>Aggregate</code> overload that takes a <em>seed</em> value... </blockquote> <p> My impression is that part of the functional programming style is to avoid function overloading. Consistent with that is the naming used by Language Ext for these concepts. In Language Ext, the function with type (in F# notation) <code>seq&lt;'a&gt; -&gt; ('a -&gt; 'a -&gt; 'a) -&gt; 'a</code> is called <a href="https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/DataTypes/List/Lst.Extensions.cs#L599">Reduce</a> and the function with type (in F# notation) <code>seq&lt;'a&gt; -&gt; 'b -&gt; ('b -&gt; 'a -&gt; 'b) -&gt; 'b</code> is called <a href="https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/DataTypes/List/Lst.Extensions.cs#L420">Fold</a>. </p> <p> I don't know the origin of these two names, but I remember the difference by thinking about preparing food. In cooking, <a href="https://en.wikipedia.org/wiki/Reduction_(cooking)">reduction</a> increases the concentration of a liquid by boiling away some of its water. I think of the returned <code>'a</code> as being a highly concentrated form of the input sequence since every sequence element (and only those elements) was used to create that return value. In baking, <a href="https://www.wikihow.com/Fold-(Baking)">folding</a> is a technique that carefully combines two mixtures into one. This reminds me of how the seed value <code>'b</code> and the sequence of <code>'a</code> are (typically) two different types and are combined by the given function. They are not perfect analogies, but they work for me. </p> <p> On a related note, <a href="https://github.com/louthy/language-ext/issues/583">I dislike</a> that Reduce returns <code>'a</code> instead of <code>Option<'a></code>. </p> </div> <div class="comment-date">2019-07-12 12:20 UTC</div> </div> <div class="comment" id="43e48df0645b4c05992b0f599cc71d10"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#43e48df0645b4c05992b0f599cc71d10">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. As you may know, <a href="/dippp">my book</a> liberally employs cooking analogies, but I admit that I've never thought about <em>reduction</em> and <em>fold</em> in that light before. Good analogies, although perhaps a bit <em>strained</em> (pun intended). </p> <p> They do work well, though, for the reasons you give. <blockquote> "the functional programming style is to avoid function overloading" </blockquote> As far as I can tell, this has more to do with the combination of <a href="https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system">Hindley–Milner type inference</a> and currying you encounter in Haskell and ML-derived languages than it has to do with functional programming in itself. If I recall correctly, <a href="https://clojure.org">Clojure</a> makes copious use of overloading. </p> <p> The problem with overloading in a language like <a href="https://fsharp.org">F#</a> is that if you imagine that the function you refer to as <code>fold</code> was also called <code>reduce</code>, a partially applied expression like this would be ambiguous: </p> <p> <pre>let foo = reduce xs bar</pre> </p> <p> What is <code>bar</code>, here? If <code>reduce</code> is overloaded, is it a function, or is it a 'seed value'? </p> <p> As far as I can tell, the compiler can't infer that, so instead of compromising on type inference, the languages in question disallow function overloading. </p> <p> Notice, additionally, that F# does allow <em>method</em> overloading, for the part of the language that enables interoperability with the rest of .NET. In that part of the language, type inference rarely works anyway. I'm not an expert in how the F# compiler works, but I've always understood this to indicate that the interop part of F# isn't based on Hindley-Milner. I don't see how it could be, since the .NET/IL type system isn't a Hindley-Milner type system. </p> <p> The <code>reduce</code> function you refer to is, by the way, based on a <a href="/2017/11/27/semigroups">semigroup</a> instance. More specifically, it's simply how <a href="/2017/12/11/semigroups-accumulate">semigroups accumulate</a>. I agree that <code>reduce</code> is partial, and therefore not as pretty as one could wish, but I think a more appropriate solution is to define it on <code>NotEmptyCollection&lt;T&gt;</code>, instead of on <code>IEnumerable&lt;T&gt;</code>, as shown in <a href="/2017/12/11/semigroups-accumulate">that article</a>. </p> <p> In other words, I don't think <code>reduce</code> belongs on <code>IEnumerable&lt;T&gt;</code> at all. I know it's in both F# and Haskell, but my personal opinion is that it shouldn't be, just like Haskell's <code>head</code> function ought not to exist either... </p> </div> <div class="comment-date">2019-07-14 16:29 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative monoids https://blog.ploeh.dk/2019/04/22/applicative-monoids 2019-04-22T05:36:00+00:00 Mark Seemann <div id="post"> <p> <em>An applicative functor containing monoidal values itself forms a monoid.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. An applicative functor is a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">data container</a> that supports combinations. If an applicative functor contains values of a type that gives rise to a <a href="/2017/10/06/monoids">monoid</a>, then the <a href="/2018/03/22/functors">functor</a> itself forms a monoid. </p> <p> In a previous article you learned that <a href="/2019/04/15/lazy-monoids">lazy computations of monoids remain monoids</a>. Furthermore, <a href="/2018/12/17/the-lazy-applicative-functor">a lazy computation is an applicative functor</a>, and it turns out that the result generalises. The result regarding lazy computation is just a special case. </p> <h3 id="3c6acb0da15b4ae8b78f5d8879b7efe3"> Monap <a href="#3c6acb0da15b4ae8b78f5d8879b7efe3" title="permalink">#</a> </h3> <p> Since version 4.11 of <a href="https://www.haskell.org">Haskell</a>'s <em>base</em> library, <code>Monoid</code> is a subset of <code>Semigroup</code>, so in order to create a <code>Monoid</code> instance, you must first define a <code>Semigroup</code> instance. </p> <p> In order to escape the need for flexible contexts, you'll have to define a wrapper <code>newtype</code> that'll be the instance. What should you call it? It's going to be an applicative functor of monoids, so perhaps something like <em>ApplicativeMonoid?</em> Nah, that's too long. <em>AppMon</em>, then? Sure, but how about flipping the terms: <em>MonApp?</em> That's better. Let's drop the last <em>p</em> and dispense with the <a href="https://en.wikipedia.org/wiki/Camel_case">Pascal case</a>: <em>Monap</em>. </p> <p> <em>Monap</em> almost looks like <em>Monad</em>, only with the last letter rotated half a revolution. This should allow for maximum confusion. </p> <p> To be clear, I normally don't advocate for droll word play when writing production code, but I occasionally do it in articles and presentations. The <em>Monap</em> in this article exists only to illustrate a point. It's not intended to be used. Furthermore, this article doesn't discuss monads at all, so the risk of confusion should, hopefully, be minimised. I may, however, regret this decision... </p> <h3 id="fb2568655a314bba8e417d06317b2690"> Applicative semigroup <a href="#fb2568655a314bba8e417d06317b2690" title="permalink">#</a> </h3> <p> First, introduce the wrapper <code>newtype</code>: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;Monap&nbsp;f&nbsp;a&nbsp;=&nbsp;Monap&nbsp;{&nbsp;runMonap&nbsp;::&nbsp;f&nbsp;a&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>)</pre> </p> <p> This only states that there's a type called <code>Monap</code> that wraps some higher-kinded type <code>f a</code>; that is, a container <code>f</code> of values of the type <code>a</code>. The intent is that <code>f</code> is an applicative functor, hence the use of the letter <em>f</em>, but the type itself doesn't constrain <code>f</code> to any type class. </p> <p> The <code>Semigroup</code> instance does, though: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;(<span style="color:blue;">Applicative</span>&nbsp;f,&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;(<span style="color:blue;">Monap</span>&nbsp;f&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;(Monap&nbsp;x)&nbsp;&lt;&gt;&nbsp;(Monap&nbsp;y)&nbsp;=&nbsp;Monap&nbsp;$&nbsp;liftA2&nbsp;<span style="color:#2b91af;">(&lt;&gt;)</span>&nbsp;x&nbsp;y </pre> </p> <p> This states that when <code>f</code> is a <code>Applicative</code> instance, and <code>a</code> is a <code>Semigroup</code> instance, then <code>Monap f a</code> is also a <code>Semigroup</code> instance. </p> <p> Here's an example of combining two applicative <a href="/2017/11/27/semigroups">semigroups</a>: </p> <p> <pre>λ&gt; Monap (Just (Max 42)) &lt;&gt; Monap (Just (Max 1337)) Monap {runMonap = Just (Max {getMax = 1337})}</pre> </p> <p> This example uses the <code>Max</code> semigroup container, and <code>Maybe</code> as the applicative functor. For <code>Max</code>, the <code>&lt;&gt;</code> operator returns the value that contains the highest value, which in this case is 1337. </p> <p> It even works when the applicative functor in question is <code>IO</code>: </p> <p> <pre>λ&gt; runMonap $ Monap (Sum &lt;$&gt; randomIO @Word8) &lt;&gt; Monap (Sum &lt;$&gt; randomIO @Word8) Sum {getSum = 165}</pre> </p> <p> This example uses <code>randomIO</code> to generate two random values. It uses the <code>TypeApplications</code> GHC extension to make <code>randomIO</code> generate <code>Word8</code> values. Each random number is projected into the <code>Sum</code> container, which means that <code>&lt;&gt;</code> will add the numbers together. In the above example, the result is 165, but if you evaluate the expression a second time, you'll most likely get another result: </p> <p> <pre>λ&gt; runMonap $ Monap (Sum &lt;$&gt; randomIO @Word8) &lt;&gt; Monap (Sum &lt;$&gt; randomIO @Word8) Sum {getSum = 246}</pre> </p> <p> You can also use linked list (<code>[]</code>) as the applicative functor. In this case, the result may be surprising (depending on what you expect): </p> <p> <pre>λ&gt; Monap [Product 2, Product 3] &lt;&gt; Monap [Product 4, Product 5, Product 6] Monap {runMonap = [Product {getProduct = 8},Product {getProduct = 10},Product {getProduct = 12}, Product {getProduct = 12},Product {getProduct = 15},Product {getProduct = 18}]}</pre> </p> <p> Notice that we get all the combinations of products: <em>2</em> multiplied with each element in the second list, followed by <em>3</em> multiplied by each of the elements in the second list. This shouldn't be that startling, though, since you've already, previously in this article series, seen several examples of how an applicative functor implies combinations. </p> <h3 id="33d4ff4f0fbf4e3cbf4d802b0f287f63"> Applicative monoid <a href="#33d4ff4f0fbf4e3cbf4d802b0f287f63" title="permalink">#</a> </h3> <p> With the <code>Semigroup</code> instance in place, you can now add the <code>Monoid</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;(<span style="color:blue;">Applicative</span>&nbsp;f,&nbsp;<span style="color:blue;">Monoid</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;(<span style="color:blue;">Monap</span>&nbsp;f&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;mempty&nbsp;=&nbsp;Monap&nbsp;$&nbsp;pure&nbsp;$&nbsp;mempty </pre> </p> <p> This is straightforward: you take the identity (<code>mempty</code>) of the monoid <code>a</code>, promote it to the applicative functor <code>f</code> with <code>pure</code>, and finally put that value into the <code>Monap</code> wrapper. </p> <p> This works fine as well: </p> <p> <pre>λ&gt; mempty :: Monap Maybe (Sum Integer) Monap {runMonap = Just (Sum {getSum = 0})} λ&gt; mempty :: Monap [] (Product Word8) Monap {runMonap = [Product {getProduct = 1}]}</pre> </p> <p> The identity laws also seem to hold: </p> <p> <pre>λ&gt; Monap (Right mempty) &lt;&gt; Monap (Right (Sum 2112)) Monap {runMonap = Right (Sum {getSum = 2112})} λ&gt; Monap ("foo", All False) &lt;&gt; Monap mempty Monap {runMonap = ("foo",All {getAll = False})}</pre> </p> <p> The last, right-identity example is interesting, because the applicative functor in question is a tuple. Tuples are <code>Applicative</code> instances when the first, or left, element is a <code>Monoid</code> instance. In other words, <code>f</code> is, in this case, <code>(,) String</code>. The <code>Monoid</code> instance that <code>Monap</code> sees as <code>a</code>, on the other hand, is <code>All</code>. </p> <p> Since <a href="/2017/10/30/tuple-monoids">tuples of monoids are themselves monoids</a>, however, I can get away with writing <code>Monap mempty</code> on the right-hand side, instead of the more elaborate template the other examples use: </p> <p> <pre>λ&gt; Monap ("foo", All False) &lt;&gt; Monap ("", mempty) Monap {runMonap = ("foo",All {getAll = False})}</pre> </p> <p> or perhaps even: </p> <p> <pre>λ&gt; Monap ("foo", All False) &lt;&gt; Monap (mempty, mempty) Monap {runMonap = ("foo",All {getAll = False})}</pre> </p> <p> Ultimately, all three alternatives mean the same. </p> <h3 id="6b484b60d19b4ef4a3716ceaa693cb8b"> Associativity <a href="#6b484b60d19b4ef4a3716ceaa693cb8b" title="permalink">#</a> </h3> <p> As usual, I'm not going to do the work of formally proving that the monoid laws hold for the <code>Monap</code> instances, but I'd like to share some QuickCheck properties that indicate that they do, starting with a property that verifies associativity: </p> <p> <pre><span style="color:#2b91af;">assocLaw</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a,&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Property</span> assocLaw&nbsp;x&nbsp;y&nbsp;z&nbsp;=&nbsp;(x&nbsp;&lt;&gt;&nbsp;y)&nbsp;&lt;&gt;&nbsp;z&nbsp;===&nbsp;x&nbsp;&lt;&gt;&nbsp;(y&nbsp;&lt;&gt;&nbsp;z)</pre> </p> <p> This property is entirely generic. It'll verify associativity for any <code>Semigroup a</code>, not only for <code>Monap</code>. You can, however, run it for various <code>Monap</code> types, as well. You'll see how this is done a little later. </p> <h3 id="51973eba155f418e8903d37f6d3938d2"> Identity <a href="#51973eba155f418e8903d37f6d3938d2" title="permalink">#</a> </h3> <p> Likewise, you can write two properties that check left and right identity, respectively. </p> <p> <pre><span style="color:#2b91af;">leftIdLaw</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a,&nbsp;<span style="color:blue;">Monoid</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Property</span> leftIdLaw&nbsp;x&nbsp;=&nbsp;x&nbsp;===&nbsp;mempty&nbsp;&lt;&gt;&nbsp;x <span style="color:#2b91af;">rightIdLaw</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a,&nbsp;<span style="color:blue;">Monoid</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Property</span> rightIdLaw&nbsp;x&nbsp;=&nbsp;x&nbsp;===&nbsp;x&nbsp;&lt;&gt;&nbsp;mempty </pre> </p> <p> Again, this is entirely generic. These properties can be used to test the identity laws for any monoid, including <code>Monap</code>. </p> <h3 id="0cc5968e519c48a0816574e1dc1667fc"> Properties <a href="#0cc5968e519c48a0816574e1dc1667fc" title="permalink">#</a> </h3> <p> You can run each of these properties multiple time, for various different functors and monoids. As <code>Applicative</code> instances, I've used <code>Maybe</code>, <code>[]</code>, <code>(,) Any</code>, and <code>Identity</code>. As <code>Monoid</code> instances, I've used <code>String</code>, <code>Sum Integer</code>, <code>Max Int16</code>, and <code>[Float]</code>. Notice that a list (<code>[]</code>) is both an applicative functor as well as a monoid. In this test set, I've used it in both roles. </p> <p> <pre>tests&nbsp;= &nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;testGroup&nbsp;<span style="color:#a31515;">&quot;Properties&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Associativity&nbsp;law,&nbsp;Maybe&nbsp;String&quot;</span>&nbsp;(assocLaw&nbsp;@(Monap&nbsp;Maybe&nbsp;String)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Left&nbsp;identity&nbsp;law,&nbsp;Maybe&nbsp;String&quot;</span>&nbsp;(leftIdLaw&nbsp;@(Monap&nbsp;Maybe&nbsp;String)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Right&nbsp;identity&nbsp;law,&nbsp;Maybe&nbsp;String&quot;</span>&nbsp;(rightIdLaw&nbsp;@(Monap&nbsp;Maybe&nbsp;String)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Associativity&nbsp;law,&nbsp;[Sum&nbsp;Integer]&quot;</span>&nbsp;(assocLaw&nbsp;@(Monap&nbsp;<span style="color:blue;">[]</span>&nbsp;(Sum&nbsp;Integer))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Left&nbsp;identity&nbsp;law,&nbsp;[Sum&nbsp;Integer]&quot;</span>&nbsp;(leftIdLaw&nbsp;@(Monap&nbsp;<span style="color:blue;">[]</span>&nbsp;(Sum&nbsp;Integer))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Right&nbsp;identity&nbsp;law,&nbsp;[Sum&nbsp;Integer]&quot;</span>&nbsp;(rightIdLaw&nbsp;@(Monap&nbsp;<span style="color:blue;">[]</span>&nbsp;(Sum&nbsp;Integer))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Associativity&nbsp;law,&nbsp;(Any,&nbsp;Max&nbsp;Int8)&quot;</span>&nbsp;(assocLaw&nbsp;@(Monap&nbsp;(<span style="color:#2b91af;">(,)</span>&nbsp;Any)&nbsp;(Max&nbsp;Int8))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Left&nbsp;identity&nbsp;law,&nbsp;(Any,&nbsp;Max&nbsp;Int8)&quot;</span>&nbsp;(leftIdLaw&nbsp;@(Monap&nbsp;(<span style="color:#2b91af;">(,)</span>&nbsp;Any)&nbsp;(Max&nbsp;Int8))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Right&nbsp;identity&nbsp;law,&nbsp;(Any,&nbsp;Max&nbsp;Int8)&quot;</span>&nbsp;(rightIdLaw&nbsp;@(Monap&nbsp;(<span style="color:#2b91af;">(,)</span>&nbsp;Any)&nbsp;(Max&nbsp;Int8))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Associativity&nbsp;law,&nbsp;Identity&nbsp;[Float]&quot;</span>&nbsp;(assocLaw&nbsp;@(Monap&nbsp;Identity&nbsp;[Float])), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Left&nbsp;identity&nbsp;law,&nbsp;Identity&nbsp;[Float]&quot;</span>&nbsp;(leftIdLaw&nbsp;@(Monap&nbsp;Identity&nbsp;[Float])), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Right&nbsp;identity&nbsp;law,&nbsp;Identity&nbsp;[Float]&quot;</span>&nbsp;(rightIdLaw&nbsp;@(Monap&nbsp;Identity&nbsp;[Float])) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;] </pre> </p> <p> All of these properties pass. </p> <h3 id="af35f2986a734a16be80590c86d0432d"> Summary <a href="#af35f2986a734a16be80590c86d0432d" title="permalink">#</a> </h3> <p> It seems that any applicative functor that contains monoidal values itself forms a monoid. The <code>Monap</code> type presented in this article only exists to demonstrate this conjecture; it's not intended to be <em>used</em>. </p> <p> If it holds, I think it's an interesting result, because it further enables you to reason about the properties of complex systems, based on the properties of simpler systems. </p> <p> <strong>Next: </strong> <a href="/2018/12/24/bifunctors">Bifunctors</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9a3acf3cf4174e178dc9349e11fee488"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#9a3acf3cf4174e178dc9349e11fee488">#</a></div> <div class="comment-content"> <blockquote> It seems that any applicative functor that contains monoidal values itself forms a monoid. </blockquote> <p> Is it necessary for the functor to be applicative? Do you know of a functor that contains monoidal values for which itself does <em>not</em> form a monoid? </p> </div> <div class="comment-date">2019-05-13 11:28 UTC</div> </div> <div class="comment" id="a164909adb884cd78b309a81029a2dd8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a164909adb884cd78b309a81029a2dd8">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Yes, it's necessary for the functor to be applicative, because you need the applicative combination operator <code>&lt;*&gt;</code> in order to implement the combination. In C#, you'd need an <code>Apply</code> method <a href="/2018/10/01/applicative-functors/#cef395ee19644f30bfd1ad7a84b6f912">as shown here</a>. </p> <p> Technically, the monoidal <code>&lt;&gt;</code> operator for <code>Monap</code> is, as you can see, implemented with a call to <code>liftA2</code>. In Haskell, you can implement an instance of <code>Applicative</code> by implementing either <code>liftA2</code> or <code>&lt;*&gt;</code>, as well as <code>pure</code>. You usually see <code>Applicative</code> described by <code>&lt;*&gt;</code>, which is what I've done in <a href="/2018/10/01/applicative-functors">my article series on applicative functors</a>. If you do that, you can define <code>liftA2</code> by a combination of <code>&lt;*&gt;</code> and <code>fmap</code> (the <code>Select</code> method that defines functors). </p> <p> If you want to put this in C# terms, you need both <code>Select</code> and <code>Apply</code> in order to be able to lift a monoid into a functor. </p> <p> Is there a functor that contains monoidal values that itself doesn't form a monoid? </p> <p> Yes, indeed. In order to answer that question, we 'just' need to identify a functor that's <em>not</em> an applicative functor. Tuples are good examples. </p> <p> A <a href="https://blog.ploeh.dk/2018/12/31/tuple-bifunctor/#d918d0271c33406ba3047ef162212100">tuple forms a functor</a>, but in general nothing more than that. Consider a tuple where the first element is a <code>Guid</code>. It's a functor, but can you implement the following function? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotImplementedException</span>(<span style="color:#a31515;">&quot;What&nbsp;would&nbsp;you&nbsp;write&nbsp;here?&quot;</span>); }</pre> </p> <p> You can pull the <code>T</code> value out of <code>source</code> and project it to a <code>TResult</code> value with <code>selector</code>, but you'll need to put it back in a <code>Tuple&lt;Guid, TResult&gt;</code>. Which <code>Guid</code> value are you going to use for that tuple? </p> <p> There's no clear answer to that question. </p> <p> More specifically, consider <code>Tuple&lt;Guid, int&gt;</code>. This is a functor that contains monoidal values. Let's say that we want to use the addition monoid over integers. How would you implement the following method? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotImplementedException</span>(<span style="color:#a31515;">&quot;What&nbsp;would&nbsp;you&nbsp;write&nbsp;here?&quot;</span>); }</pre> </p> <p> Again, you run into the issue that while you can pull the integers out of the tuples and add them together, there's no clear way to figure out which <code>Guid</code> value to put into the tuple that contains the sum. </p> <p> The issue particularly with tuples is that there's no general way to combine the leftmost values of the tuples. If there is - that is, if leftmost values form a monoid - then the tuple is also an applicative functor. For example, <code>Tuple&lt;string, int&gt;</code> is applicative and forms a monoid over addition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selector.Item1&nbsp;+&nbsp;source.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selector.Item2(source.Item2)); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(x.Item1&nbsp;+&nbsp;y.Item1,&nbsp;x.Item2&nbsp;+&nbsp;y.Item2); }</pre> </p> <p> You can also implement <code>Add</code> with <code>Apply</code>, but you're going to need two <code>Apply</code> overloads to make it work. </p> <p> Incidentally, unbeknownst to me, the <code>Ap</code> wrapper was added to Haskell's <code>Data.Monoid</code> module 12 days before I wrote this article. In all but name, it's equivalent to the <code>Monap</code> wrapper presented here. </p> </div> <div class="comment-date">2019-05-14 20:44 UTC</div> </div> <div class="comment" id="8900297a3b484ac0adfb8574a66cff87"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#8900297a3b484ac0adfb8574a66cff87">#</a></div> <div class="comment-content"> <blockquote> <p> ...if leftmost values form a monoid - then the tuple is also an applicative functor. For example, <code>Tuple&lt;string, int&gt;</code> is applicative... </p> </blockquote> <p> I want to add some prepositional phrases to our statements like I <a href="https://blog.ploeh.dk/2019/01/07/either-bifunctor/#79f5d74763e34cb0997a7a79df1e05f0">commented here</a> to help claify things. I don't think that <code>Tuple&lt;string, int&gt;</code> can be applicative because there are no type parameters in which it could be applicative. Were you trying to say that <code>Tuple&lt;string, B&gt;</code> is applicative in <code>B</code>? This seems to match your <code>Apply</code> function, which doesn't depend on <code>int</code>. </p> </div> <div class="comment-date">2019-05-30 05:02 UTC</div> </div> <div class="comment" id="daa6444c8fb0484a8bf1accdb3cc544b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#daa6444c8fb0484a8bf1accdb3cc544b">#</a></div> <div class="comment-content"> <p> Tyson, you're quite right; good catch. My wording was incorrect (I was probably either tired or in a hurry when I wrote that), but fortunately, the code looks like it's correct. </p> <p> That you for pointing out my error. </p> </div> <div class="comment-date">2019-05-30 13:00 UTC</div> </div> <div class="comment" id="075df592e63948e695851d7ae20842ea"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#075df592e63948e695851d7ae20842ea">#</a></div> <div class="comment-content"> <blockquote> <p> ...<code>Tuple&lt;string, int&gt;</code> is applicative and forms a monoid over addition... </p> </blockquote> <p> I do agree with the monoid part, where "addition" means string concatenation for the first item and integer addition for the second item. </p> <blockquote> <p> <code>Tuple&lt;string, B&gt;</code> is applicative in <code>B</code> </p> </blockquote> <p> Now I am trying to improve my understanding of this statement. In Haskell, my understanding the definition of the <a href="https://en.wikibooks.org/wiki/Haskell/Applicative_functors#The_Applicative_class">Applicative type class</a> applied to <code>Tuple&lt;string, B&gt;</code> requires a function <code>pure</code> from <code>B</code> to <code>Tuple&lt;string, B&gt;</code>. What it the definition of this funciton? Does it use the empty string in order to make an instance of <code>Tuple&lt;string, B&gt;</code>? If so, what is the justification for this? Or maybe my reasoning here is mistaken. </p> </div> <div class="comment-date">2019-05-31 12:52 UTC</div> </div> <div class="comment" id="ce69d4440f314827a8ec441511d0ce71"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ce69d4440f314827a8ec441511d0ce71">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. In Haskell, it's true that applicative functors must also define <code>pure</code>. In this article series, I've glosssed over that constraint, since I'm not aware of any data containers that can lawfully implement <code>&lt;*&gt;</code> or <code>liftA2</code>, but <em>can't</em> define <code>pure</code>. </p> <p> The applicative instance for tuples is, however, constrained: </p> <p> <pre>Monoid a =&gt; Applicative ((,) a)</pre> </p> <p> The construct <code>((,) a)</code> means any tuple where the first element has the generic type <code>a</code>. The entire expression means that tuples are applicative functors when the first element forms a monoid; that's the restriction on <code>a</code>. The definition of <code>pure</code>, then, is: </p> <p> <pre>pure x = (mempty, x)</pre> </p> <p> That is, use the monoidal identity (<code>mempty</code>) for the first element, and use <code>x</code> as the second element. For strings, since the identity for string concatenation is the empty string, yes, it does exactly mean that <code>pure</code> for <code>Tuple&lt;string, B&gt;</code> would return a tuple with the empty string, and the input argument as the second element: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;Pure&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;x); }</pre> </p> <p> That's the behaviour you get from Haskell as well: </p> <p> <pre>Prelude Data.Monoid&gt; pure 42 :: (String, Int) ("",42)</pre> </p> </div> <div class="comment-date">2019-05-31 14:59 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Lazy monoids https://blog.ploeh.dk/2019/04/15/lazy-monoids 2019-04-15T13:54:00+00:00 Mark Seemann <div id="post"> <p> <em>Lazy monoids are monoids. An article for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). Previous articles have shown how more complex monoids arise from simpler monoids, such as <a href="/2017/10/30/tuple-monoids">tuple monoids</a>, <a href="/2017/11/06/function-monoids">function monoids</a>, and <a href="/2018/04/03/maybe-monoids">Maybe monoids</a>. This article shows another such result: how lazy computations of monoids itself form monoids. </p> <p> You'll see how simple this is through a series of examples. Specifically, you'll revisit several of the examples you've already seen in this article series. </p> <h3 id="a715cff45376401db9863a095a5e156d"> Lazy addition <a href="#a715cff45376401db9863a095a5e156d" title="permalink">#</a> </h3> <p> Perhaps the most intuitive monoid is <em>addition</em>. Lazy addition forms a monoid as well. In C#, you can implement this with a simple extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;+&nbsp;y.Value); }</pre> </p> <p> This <code>Add</code> method simply adds two lazy integers together in a lazy computation. You use it like any other extension method: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;sum&nbsp;=&nbsp;x.Add(y);</pre> </p> <p> I'll spare you the tedious listing of <a href="https://fscheck.github.io/FsCheck">FsCheck</a>-based properties that demonstrate that the monoid laws hold. We'll look at an example of such a set of properties later in this article, for one of the other monoids. </p> <h3 id="eda5d39029904d70995ebee84570cf60"> Lazy multiplication <a href="#eda5d39029904d70995ebee84570cf60" title="permalink">#</a> </h3> <p> Not surprisingly, I hope, you can implement multiplication over lazy numbers in the same way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Multiply(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;*&nbsp;y.Value); }</pre> </p> <p> Usage is similar to lazy addition: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;product&nbsp;=&nbsp;x.Multiply(y);</pre> </p> <p> As is the case with lazy addition, this <code>Multiply</code> method currently only works with lazy <code>int</code> values. If you also want it to work with <code>long</code>, <code>short</code>, or other types of numbers, you'll have to add method overloads. </p> <h3 id="93b85bb3c55045dabdb16fd11167dad7"> Lazy Boolean monoids <a href="#93b85bb3c55045dabdb16fd11167dad7" title="permalink">#</a> </h3> <p> There are four monoids over Boolean values, although I've customarily only shown two of them: <em>and</em> and <em>or</em>. These also, trivially, work with lazy Boolean values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;And(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;&amp;&amp;&nbsp;y.Value); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;Or(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;||&nbsp;y.Value); }</pre> </p> <p> Given the previous examples, you'll hardly be surprised to see how you can use one of these extension methods: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;b&nbsp;=&nbsp;x.And(y);</pre> </p> <p> Have you noticed a pattern in how the lazy binary operations <code>Add</code>, <code>Multiply</code>, <code>And</code>, and <code>Or</code> are implemented? Could this be generalised? </p> <h3 id="12b7d0077481413cab6acf8f6bf696cf"> Lazy angular addition <a href="#12b7d0077481413cab6acf8f6bf696cf" title="permalink">#</a> </h3> <p> In a previous article you saw how <a href="/2018/07/16/angular-addition-monoid">angular addition forms a monoid</a>. Lazy angular addition forms a monoid as well, which you can implement with another extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value.Add(y.Value)); }</pre> </p> <p> Until now, you may have noticed that all the extension methods seemed to follow a common pattern that looks like this: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Foo</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;&diamond;&nbsp;y.Value);</pre> </p> <p> I've here used the diamond operator <code>&diamond;</code> as a place-holder for any sort of binary operation. My choice of that particular character is strongly influenced by <a href="https://www.haskell.org">Haskell</a>, where <a href="/2017/11/27/semigroups">semigroups</a> and monoids polymorphically are modelled with (among other options) the <code>&lt;&gt;</code> operator. </p> <p> The lazy angular addition implementation looks a bit different, though. This is because the original example uses an instance method to model the binary operation, instead of an infix operator such as <code>+</code>, <code>&&</code>, and so on. Given that the implementation of a lazy binary operation can also look like this, can you still imagine a generalisation? </p> <h3 id="669710b096144c6d8aaaca4426f8f795"> Lazy string concatenation <a href="#669710b096144c6d8aaaca4426f8f795" title="permalink">#</a> </h3> <p> If we follow the rough ordering of examples introduced in this article series about monoids, we've now reached <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">concatenation as a monoid</a>. While various lists, arrays, and other sorts of collections also form a monoid over concatenation, in .NET, <code>IEnumerable&lt;T&gt;</code> already enables lazy evaluation, so I think it's more interesting to consider lazy string concatenation. </p> <p> The implementation, however, holds few surprises: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;Concat(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;+&nbsp;y.Value); }</pre> </p> <p> The overall result, so far, seems encouraging. All the basic monoids we've covered are also monoids when lazily computed. </p> <h3 id="29df873ebae145e2b903f6562825b69e"> Lazy money addition <a href="#29df873ebae145e2b903f6562825b69e" title="permalink">#</a> </h3> <p> The portfolio example from Kent Beck's book <a href="http://bit.ly/tddbe">Test-Driven Development By Example</a> also <a href="/2017/10/16/money-monoid">forms a monoid</a>. You can, again, implement an extension method that enables you to add lazy expressions together: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;Plus( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;addend) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;(()&nbsp;=&gt;&nbsp;source.Value.Plus(addend.Value)); }</pre> </p> <p> So far, you've seen several examples of implementations, but are they really monoids? All are clearly binary operations, but are they associative? Do identities exist? In other words, do the lazy binary operations obey the monoid laws? </p> <p> As usual, I'm not going to prove that they do, but I do want to share a set of FsCheck properties that demonstrate that the monoid laws hold. As an example, I'll share the properties for this lazy <code>Plus</code> method, but you can write similar properties for all of the above methods as well. </p> <p> You can verify the associativity law like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;LazyPlusIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Plus(y).Plus(z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Plus(y.Plus(z)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank); }</pre> </p> <p> Here, <code>Compare.UsingBank</code> is just a <a href="http://xunitpatterns.com/Test%20Utility%20Method.html">test utility API</a> to make the code more readable: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Compare</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>&nbsp;UsingBank&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>(); }</pre> </p> <p> This takes advantage of the overloads for <a href="https://xunit.net">xUnit.net</a>'s <code>Assert</code> methods that take custom equality comparers as an extra, optional argument. <code>ExpressionEqualityComparer</code> is implemented in the test code base: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEqualityComparer</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;,&nbsp;<span style="color:#2b91af;">IEqualityComparer</span>&lt;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Bank</span>&nbsp;bank; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ExpressionEqualityComparer() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bank&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Bank</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bank.AddRate(<span style="color:#a31515;">&quot;CHF&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>,&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:#2b91af;">IExpression</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;xm&nbsp;=&nbsp;bank.Reduce(x,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ym&nbsp;=&nbsp;bank.Reduce(y,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(xm,&nbsp;ym); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode(<span style="color:#2b91af;">IExpression</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;bank.Reduce(obj,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>).GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(x.Value,&nbsp;y.Value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode(<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;GetHashCode(obj.Value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If you think that the exchange rate between Dollars and Swiss Francs looks ridiculous, it's because I'm using the rate that Kent Beck used in his book, which is from 2002 (but otherwise timeless). </p> <p> The above property passes for hundreds of randomly generated input values, as is the case for this property, which verifies the left and right identity: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;LazyPlusHasIdentity(<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;x.Plus(<span style="color:#2b91af;">Plus</span>.Identity.ToLazy()),&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;<span style="color:#2b91af;">Plus</span>.Identity.ToLazy().Plus(x),&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank); }</pre> </p> <p> These properties are just examples, not proofs. Still, they give confidence that lazy computations of monoids are themselves monoids. </p> <h3 id="6b5aa8f3a0b34d4c9d145eac3e59fe9b"> Lazy Roster combinations <a href="#6b5aa8f3a0b34d4c9d145eac3e59fe9b" title="permalink">#</a> </h3> <p> The last example you'll get in this article is the <code>Roster</code> example from the article on <a href="/2017/10/30/tuple-monoids">tuple monoids</a>. Here's yet another extension method that enables you to combine two lazy rosters: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Roster</span>&gt;&nbsp;Combine(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Roster</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Roster</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Roster</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value.Combine(y.Value)); }</pre> </p> <p> At this point it should be clear that there's essentially two variations in how the above extension methods are implemented. One variation is when the binary operation is implemented with an infix operator (like <code>+</code>, <code>||</code>, and so on), and another variation is when it's modelled as an instance method. How do these implementations generalise? </p> <p> I'm sure you could come up with an ad-hoc higher-order function, abstract base class, or interface to model such a generalisation, but I'm not motivated by <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">saving keystrokes</a>. What I'm trying to uncover with this <a href="/2017/10/04/from-design-patterns-to-category-theory">overall article series</a> is how universal abstractions apply to programming. </p> <p> Which universal abstraction is in play here? </p> <h3 id="6d247327cf214fa698dc9ec29693bd6f"> Lazy monoids as applicative operations <a href="#6d247327cf214fa698dc9ec29693bd6f" title="permalink">#</a> </h3> <p> <a href="/2018/12/17/the-lazy-applicative-functor">Lazy&lt;T&gt; forms an applicative functor</a>. Using appropriate <code>Apply</code> overloads, you can rewrite all the above implementations in applicative style. Here's lazy addition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f&nbsp;=&nbsp;(i,&nbsp;j)&nbsp;=&gt;&nbsp;i&nbsp;+&nbsp;j; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); }</pre> </p> <p> This first declares a <code>Func</code> value <code>f</code> that invokes the non-lazy binary operation, in this case <code>+</code>. Next, you can leverage the applicative nature of <code>Lazy&lt;T&gt;</code> to apply <code>f</code> to the two lazy values <code>x</code> and <code>y</code>. </p> <p> Multiplication, as well as the Boolean operations, follow the exact same template: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Multiply(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f&nbsp;=&nbsp;(i,&nbsp;j)&nbsp;=&gt;&nbsp;i&nbsp;*&nbsp;j; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;And(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;(b1,&nbsp;b2)&nbsp;=&gt;&nbsp;b1&nbsp;&amp;&amp;&nbsp;b2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;Or(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;(b1,&nbsp;b2)&nbsp;=&gt;&nbsp;b1&nbsp;||&nbsp;b2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); }</pre> </p> <p> Notice that in all four implementations, the second line of code is verbatim the same: <code>return f.Apply(x).Apply(y);</code> </p> <p> Does this generalisation also hold when the underlying, non-lazy binary operation is modelled as an instance method, as is the case of e.g. angular addition? Yes, indeed: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Angle</span>,&nbsp;<span style="color:#2b91af;">Angle</span>,&nbsp;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;f&nbsp;=&nbsp;(i,&nbsp;j)&nbsp;=&gt;&nbsp;i.Add(j); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); }</pre> </p> <p> You can implement the <code>Combine</code> method for lazy <code>Roster</code> objects in the same way, as well as <code>Plus</code> for lazy monetary expressions. The latter is worth revisiting. </p> <h3 id="445af7622459483ab64f4ff31ceb1c2e"> Using the Lazy functor over portfolio expressions <a href="#445af7622459483ab64f4ff31ceb1c2e" title="permalink">#</a> </h3> <p> The lazy <code>Plus</code> implementation looks like all of the above <code>Apply</code>-based implementations: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;Plus( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;source,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;addend) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IExpression</span>,&nbsp;<span style="color:#2b91af;">IExpression</span>,&nbsp;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;f&nbsp;=&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;x.Plus(y); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(source).Apply(addend); }</pre> </p> <p> In my article, however, you saw how, when <code>Plus</code> is a monoid, you can implement <code>Times</code> as an extension method. Can you implement a lazy version of <code>Times</code> as well? Must it be another extension method? </p> <p> Yes, but instead of an ad-hoc implementation, you can take advantage of the functor nature of Lazy: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;Times(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;exp,&nbsp;<span style="color:blue;">int</span>&nbsp;multiplier) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;exp.Select(x&nbsp;=&gt;&nbsp;x.Times(multiplier)); }</pre> </p> <p> Notice that instead of explicitly reaching into the lazy <code>Value</code>, you can simply call <code>Select</code> on <code>exp</code>. This lazily projects the <code>Times</code> operation, while preserving the invariants of <code>Lazy&lt;T&gt;</code> (i.e. that the computation is deferred until you ultimately access the <code>Value</code> property). </p> <p> You can implement a lazy version of <code>Reduce</code> in the same way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Money</span>&gt;&nbsp;Reduce(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;exp,&nbsp;<span style="color:#2b91af;">Bank</span>&nbsp;bank,&nbsp;<span style="color:blue;">string</span>&nbsp;to) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;exp.Select(x&nbsp;=&gt;&nbsp;x.Reduce(bank,&nbsp;to)); }</pre> </p> <p> The question is, however, is it even worthwhile? Do you need to create all these overloads, or could you just leverage <code>Select</code> when you have a lazy value? </p> <p> For example, if the above <code>Reduce</code> overload didn't exist, you'd still be able to work with the portfolio API like this: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;portfolio&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Money</span>&gt;&nbsp;result&nbsp;=&nbsp;portfolio.Select(x&nbsp;=&gt;&nbsp;x.Reduce(bank,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>));</pre> </p> <p> If you only occasionally use <code>Reduce</code>, then perhaps this is good enough. If you frequently call <code>Reduce</code>, however, it might be worth to add the above overload, in which case you could then instead write: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;portfolio&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Money</span>&gt;&nbsp;result&nbsp;=&nbsp;portfolio.Reduce(bank,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>);</pre> </p> <p> In both cases, however, I think that you'd be putting the concept of an applicative functor to good use. </p> <h3 id="a5942465683c459a92457ea13e3bd21d"> Towards generalisation <a href="#a5942465683c459a92457ea13e3bd21d" title="permalink">#</a> </h3> <p> Is the applicative style better than the initial ad-hoc implementations? That depends on how you evaluate 'better'. If you count lines of code, then the applicative style is twice as verbose as the ad-hoc implementations. In other words, this: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;+&nbsp;y.Value);</pre> </p> <p> seems simpler than this: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f&nbsp;=&nbsp;(i,&nbsp;j)&nbsp;=&gt;&nbsp;i&nbsp;+&nbsp;j; <span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y);</pre> </p> <p> This is, however, mostly because C# is too weak to express such abstractions in an elegant way. In <a href="https://fsharp.org">F#</a>, using the custom <code>&lt;*&gt;</code> operator from <a href="/2018/12/17/the-lazy-applicative-functor">the article on the Lazy applicative functor</a>, you could express the lazy addition as simply as: </p> <p> <pre><span style="color:blue;">lazy</span>&nbsp;(+)&nbsp;&lt;*&gt;&nbsp;x&nbsp;&lt;*&gt;&nbsp;y</pre> </p> <p> In <a href="https://www.haskell.org">Haskell</a> (if we, once more, pretend that <code>Identity</code> is equivalent to <code>Lazy</code>), you can simplify even further to: </p> <p> <pre>(+) &lt;$&gt; x &lt;*&gt; y</pre> </p> <p> Or rather, if you want it in <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free style</a>, <code>liftA2 (+)</code>. </p> <h3 id="f418f44f772b44c287365c7054d51aa1"> Summary <a href="#f418f44f772b44c287365c7054d51aa1" title="permalink">#</a> </h3> <p> The point of this article series isn't to save keystrokes, but to identify universal laws of computing, even as they relate to object-oriented programming. The pattern seems clear enough that I dare propose the following: </p> <p> <em>All monoids remain monoids under lazy computation.</em> </p> <p> In a future article I'll offer further support for that proposition. </p> <p> <strong>Next:</strong> <a href="/2017/11/20/monoids-accumulate">Monoids accumulate</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A pure Test Spy https://blog.ploeh.dk/2019/04/08/a-pure-test-spy 2019-04-08T06:02:00+00:00 Mark Seemann <div id="post"> <p> <em>Ad-hoc Test Spies can be implemented in Haskell using the Writer monad.</em> </p> <p> In a previous article on <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">state-based testing in Haskell</a>, I made the following throw-away statement: <blockquote> "you <em>could</em> write an ad-hoc Mock using, for example, the Writer monad" </blockquote> In that article, I didn't pursue that thought, since the theme was another. Instead, I'll expand on it here. </p> <h3 id="552a47e82a99466ebb8fe9d840b80a2f"> Test Double continuum <a href="#552a47e82a99466ebb8fe9d840b80a2f" title="permalink">#</a> </h3> <p> More than a decade ago, I wrote an MSDN Magazine article called <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2007/september/unit-testing-exploring-the-continuum-of-test-doubles"><em>Exploring The Continuum Of Test Doubles</em></a>. It was in the September 2007 issue, and you can also <a href="https://msdn.microsoft.com/en-us/magazine/msdn-magazine-issues.aspx">download the entire issue as a single file</a> and read the article offline, should you want to. </p> <p> In the article, I made the argument that the classification of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a> presented in the excellent <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> should be thought of more as a continuum with vague and fuzzy transitions, rather than discrete categories. </p> <p> <img src="/content/binary/msdn-test-double-continuum.png" alt="Spectrum of Test Doubles."> </p> <p> This figure appeared in the original article. Given that the entire MSDN Magazine issue is available for free, and that I'm the original author of the article, I consider it fair use to repeat it here. </p> <p> The point is that it's not always clear whether a Test Double is, say, a Mock, or a Spy. What I'll show you in this article is closer to a Test Spy than to a Mock, but since the distinction is blurred anyway, I think that I can get away with it. </p> <h3 id="200ba4d9157442fe9caef2429371f7c6"> Test Spy <a href="#200ba4d9157442fe9caef2429371f7c6" title="permalink">#</a> </h3> <p> <em>xUnit Test Patterns</em> defines a Test Spy as a Test Double that captures "the indirect output calls made to another component by the SUT [System Under Test] for later verification by the test." When, as shown in <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">a previous article</a>, you use <code>Mock&lt;T&gt;.Verify</code> to assert than an interaction took place, you're using the Test Double more as a Spy than a Mock: </p> <p> <pre>repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(user));</pre> </p> <p> Strictly speaking, a Mock is a Test Double that <em>immediately</em> fails the test if any unexpected interaction takes place. People often call those <em>Strict Mocks</em>, but according to the book, that's a Mock. If the Test Double only records what happens, so that you can later query it to verify whether some interaction took place, it's closer to being a Test Spy. </p> <p> Whether you call it a Mock or a Spy, you can implement verification similar to the above <code>Verify</code> method in functional programming using the Writer monad. </p> <h3 id="cf14cbf8e65a4dd6a47f879c1b850de5"> Writer-based Spy <a href="#cf14cbf8e65a4dd6a47f879c1b850de5" title="permalink">#</a> </h3> <p> I'll show you a single example in <a href="https://www.haskell.org">Haskell</a>. In <a href="http://blog.ploeh.dk/2018/07/30/flattening-arrow-code-using-a-stack-of-monads">a previous article</a>, you saw a simplified function to implement a restaurant reservation feature, repeated here for your convenience: </p> <p> <pre><span style="color:#2b91af;">tryAccept</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeT</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;<span style="color:#2b91af;">Int</span> tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;guard&nbsp;=&lt;&lt;&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;reservations&nbsp;&lt;-&nbsp;readReservations&nbsp;$&nbsp;reservationDate&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;$&nbsp;reservationQuantity&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;guard&nbsp;$&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;create&nbsp;$&nbsp;reservation&nbsp;{&nbsp;reservationIsAccepted&nbsp;=&nbsp;True&nbsp;} </pre> </p> <p> This function runs in the <code>MaybeT</code> monad, so the two <code>guard</code> functions could easily prevent if from running 'to completion'. In the happy path, though, execution should reach 'the end' of the function and call the <code>create</code> function. </p> <p> In order to test this happy path, you'll need to not only run a test-specific interpreter over the <code>ReservationsProgram</code> free monad, you should also verify that <code>reservationIsAccepted</code> is <code>True</code>. </p> <p> You can do this using the <code>Writer</code> monad to implement a Test Spy: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;tryAccept,&nbsp;happy&nbsp;path&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(NonNegative&nbsp;i) &nbsp;&nbsp;(<span style="color:blue;">fmap</span>&nbsp;getReservation&nbsp;-&gt;&nbsp;reservations) &nbsp;&nbsp;(ArbReservation&nbsp;reservation) &nbsp;&nbsp;expected &nbsp;&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;spy&nbsp;(IsReservationInFuture&nbsp;_&nbsp;next)&nbsp;=&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;next&nbsp;True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spy&nbsp;(ReadReservations&nbsp;_&nbsp;next)&nbsp;=&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;next&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spy&nbsp;(Create&nbsp;r&nbsp;next)&nbsp;=&nbsp;tell&nbsp;[r]&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;(next&nbsp;expected) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;$&nbsp;reservationQuantity&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;capacity&nbsp;=&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;+&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(actual,&nbsp;observedReservations)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runWriter&nbsp;$&nbsp;foldFreeT&nbsp;spy&nbsp;$&nbsp;runMaybeT&nbsp;$&nbsp;tryAccept&nbsp;capacity&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;&nbsp;Just&nbsp;expected&nbsp;==&nbsp;actual&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[True]&nbsp;==&nbsp;(reservationIsAccepted&nbsp;&lt;$&gt;&nbsp;observedReservations)</pre> </p> <p> This test is an <a href="http://blog.ploeh.dk/2018/05/07/inlined-hunit-test-lists">inlined</a> QuickCheck-based property. The entire <a href="https://github.com/ploeh/dependency-injection-revisited">source code is available on GitHub</a>. </p> <p> Notice the <code>spy</code> function. As the name implies, it's the Test Spy for the test. Its full type is: </p> <p> <pre>spy&nbsp;::&nbsp;Monad&nbsp;m&nbsp;=&gt;&nbsp;ReservationsInstruction&nbsp;a&nbsp;-&gt;&nbsp;WriterT&nbsp;[Reservation]&nbsp;m&nbsp;a</pre> </p> <p> This is a function that, for a given <code>ReservationsInstruction</code> value returns a <code>WriterT</code> value where the type of data being written is <code>[Reservation]</code>. The function only writes to the writer context in one of the three cases: the <code>Create</code> case. The <code>Create</code> case carries with it a <code>Reservation</code> value here named <code>r</code>. Before returning the <code>next</code> step in interpreting the free monad, the <code>spy</code> function calls <code>tell</code>, thereby writing a singleton list of <code>[r]</code> to the writer context. </p> <p> In the Act phase of the test, it calls the <code>tryAccept</code> function and proceeds to interpret the result, which is a <code>MaybeT ReservationsProgram Int</code> value. Calling <code>runMaybeT</code> produces a <code>ReservationsProgram (Maybe Int)</code>, which you can then interpret with <code>foldFreeT spy</code>. This returns a <code>Writer [Reservation] (Maybe Int)</code>, which you can finally run with <code>runWriter</code> to get a <code>(Maybe Int, [Reservation])</code> tuple. Thus, <code>actual</code> is a <code>Maybe Int</code> value, and <code>observedReservations</code> is a <code>[Reservation]</code> value - the reservation that was written by <code>spy</code> using <code>tell</code>. </p> <p> The Assert phase of the test is a Boolean expression that checks that <code>actual</code> is as expected, and that <code>reservationIsAccepted</code> of the observed reservation is <code>True</code>. </p> <p> It takes a little while to make the pieces of the puzzle fit, but it's basically just standard Haskell library functions clicked together. </p> <h3 id="fd3478dba1a241a7bb95d9a97140ebac"> Summary <a href="#fd3478dba1a241a7bb95d9a97140ebac" title="permalink">#</a> </h3> <p> People sometimes ask me: <em>How do Mocks and Stubs work in functional programming?</em> </p> <p> In general, my answer is that you don't need Mocks and Stubs because when functions are <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, you don't need to to test interactions. Sooner or later, though, you may run into higher-level interactions, even if they're <a href="http://blog.ploeh.dk/2017/07/10/pure-interactions">pure interactions</a>, and you'll likely want to unit test those. </p> <p> In a previous article you saw how to apply <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">state-based testing in Haskell, using the State monad</a>. In this article you've seen how you can create ad-hoc Mocks or Spies with the Writer monad. No auto-magical test-specific 'isolation frameworks' are required. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An example of state-based testing in C# https://blog.ploeh.dk/2019/04/01/an-example-of-state-based-testing-in-c 2019-04-01T05:50:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of avoiding Mocks and Stubs in C# unit testing.</em> </p> <p> This article is an instalment in an article series about how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based testing to state-based testing</a>. In the previous article, you saw <a href="/2019/03/25/an-example-of-state-based-testing-in-f">an example of a pragmatic state-based test in F#</a>. You can now take your new-found knowledge and apply it to the <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">original C# example</a>. </p> <p> In the spirit of <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a>, in this article you'll see how to refactor the tests while keeping the implementation code constant. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="da7f393fdef24bae9e72c4bcad7e8373"> Connect two users <a href="#da7f393fdef24bae9e72c4bcad7e8373" title="permalink">#</a> </h3> <p> The <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">previous article</a> provides more details on the System Under Test (SUT), but here it is, repeated, for your convenience: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ApiController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ConnectionsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserReader</span>&nbsp;userReader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;userRepository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserReader&nbsp;=&nbsp;userReader; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserRepository&nbsp;=&nbsp;userRepository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUserReader</span>&nbsp;UserReader&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;UserRepository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:blue;">string</span>&nbsp;userId,&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;userRes&nbsp;=&nbsp;UserReader.Lookup(userId).SelectError( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&gt;&nbsp;error.Accept(<span style="color:#2b91af;">UserLookupError</span>.Switch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onInvalidId:&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onNotFound:&nbsp;&nbsp;<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;otherUserRes&nbsp;=&nbsp;UserReader.Lookup(otherUserId).SelectError( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&gt;&nbsp;error.Accept(<span style="color:#2b91af;">UserLookupError</span>.Switch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onInvalidId:&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onNotFound:&nbsp;&nbsp;<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connect&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;user&nbsp;<span style="color:blue;">in</span>&nbsp;userRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;otherUser&nbsp;<span style="color:blue;">in</span>&nbsp;otherUserRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;Connect(user,&nbsp;otherUser); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;connect.SelectBoth(Ok,&nbsp;BadRequest).Bifold(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">User</span>&nbsp;Connect(<span style="color:#2b91af;">User</span>&nbsp;user,&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.Connect(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserRepository.Update(user); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;otherUser; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation code is a simplification of the code example that serves as an example running through my two <a href="https://cleancoders.com">Clean Coders</a> videos, <a href="https://cleancoders.com/episode/humane-code-real-episode-4/show">Church Visitor</a> and <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a>. </p> <h3 id="4e6812ee83ad4cda9b8deedef99ab4e1"> A Fake database <a href="#4e6812ee83ad4cda9b8deedef99ab4e1" title="permalink">#</a> </h3> <p> As in the previous article, you can define a test-specific <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> database: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeDB</span>&nbsp;:&nbsp;<span style="color:#2b91af;">Collection</span>&lt;<span style="color:#2b91af;">User</span>&gt;,&nbsp;<span style="color:#2b91af;">IUserReader</span>,&nbsp;<span style="color:#2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IResult</span>&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;&nbsp;Lookup(<span style="color:blue;">string</span>&nbsp;id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(<span style="color:blue;">int</span>.TryParse(id,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">int</span>&nbsp;i))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(<span style="color:#2b91af;">UserLookupError</span>.InvalidId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;user&nbsp;=&nbsp;<span style="color:blue;">this</span>.FirstOrDefault(u&nbsp;=&gt;&nbsp;u.Id&nbsp;==&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(user&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(<span style="color:#2b91af;">UserLookupError</span>.NotFound); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(user); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsDirty&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Update(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsDirty&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!Contains(user)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(user); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is one of the few cases where I find inheritance more convenient than composition. By deriving from <code>Collection&lt;User&gt;</code>, you don't have to explicitly write code to expose a <em>Retrieval Interface</em>. The entirety of a standard collection API is already available via the base class. Had this class been part of a public API, I'd be concerned that inheritance could introduce future breaking changes, but as part of a suite of unit tests, I hope that I've made the right decision. </p> <p> Although you can derive a Fake database from a base class, you can still implement required interfaces - in this case <code>IUserReader</code> and <code>IUserRepository</code>. The <code>Update</code> method is the easiest one to implement, since it simply sets the <code>IsDirty</code> flag to <code>true</code> and adds the <code>user</code> if it's not already part of the collection. </p> <p> The <code>IsDirty</code> flag is the only custom Retrieval Interface added to the <code>FakeDB</code> class. As the previous article explains, this flag provides a convenient was to verify whether or not the database has changed. </p> <p> The <code>Lookup</code> method is a bit more involved, since it has to support all three outcomes implied by the protocol: <ul> <li>If the <code>id</code> is invalid, a result to that effect is returned.</li> <li>If the user isn't found, a result to that effect is returned.</li> <li>If the user with the requested <code>id</code> is found, then that user is returned.</li> </ul> This is a typical quality of a Fake: it contains <em>some</em> production-like behaviour, while still taking shortcuts compared to a full production implementation. In this case, it properly adheres to the protocol implied by the interface and protects its invariants. It still doesn't implement persistent storage, though. </p> <h3 id="92480e7451ad4c02a524f9fcd1ef1c8d"> Happy path test case <a href="#92480e7451ad4c02a524f9fcd1ef1c8d" title="permalink">#</a> </h3> <p> This is all you need in terms of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>. You now have a test-specific <code>IUserReader</code> and <code>IUserRepository</code> implementation that you can pass to the <code>Post</code> method. Notice that a single class implements multiple interfaces. This is often key to be able to implement a Fake object in the first place. </p> <p> Like in the previous article, you can start by exercising the happy path where a user successfully connects with another user: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersSuccessfullyConnect( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;db.Add(user); &nbsp;&nbsp;&nbsp;&nbsp;db.Add(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ok&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkNegotiatedContentResult</span>&lt;<span style="color:#2b91af;">User</span>&gt;&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(otherUser,&nbsp;ok.Content); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(db.IsDirty); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(otherUser.Id,&nbsp;user.Connections); }</pre> </p> <p> This, and all other tests in this article use <a href="https://xunit.net">xUnit.net</a> 2.3.1 and <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> 4.1.0. </p> <p> The test is organised according to my standard <a href="http://blog.ploeh.dk/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">heuristic for formatting tests according to the Arrange Act Assert pattern</a>. In the Arrange phase, it adds the two valid <code>User</code> objects to the Fake <code>db</code> and sets the <code>IsDirty</code> flag to false. </p> <p> Setting the flag is necessary because this is object-oriented code, where objects have mutable state. In the previous articles with examples in <a href="https://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>, the <code>User</code> types were immutable. Connecting two users didn't mutate one of the users, but rather returned a new <code>User</code> value, as this F# example demonstrates: </p> <p> <pre><span style="color:green;">//&nbsp;User&nbsp;-&gt;&nbsp;User&nbsp;-&gt;&nbsp;User</span> <span style="color:blue;">let</span>&nbsp;addConnection&nbsp;user&nbsp;otherUser&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;user&nbsp;<span style="color:blue;">with</span>&nbsp;ConnectedUsers&nbsp;=&nbsp;otherUser&nbsp;::&nbsp;user.ConnectedUsers&nbsp;}</pre> </p> <p> In the current object-oriented code base, however, connecting one user to another is an instance method on the <code>User</code> class that mutates its state: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Connect(<span style="color:#2b91af;">User</span>&nbsp;otherUser) { &nbsp;&nbsp;&nbsp;&nbsp;connections.Add(otherUser.Id); }</pre> </p> <p> As a consequence, the <code>Post</code> method could, if someone made a mistake in its implementation, call <code>user.Connect</code>, but forget to invoke <code>UserRepository.Update</code>. Even if that happened, then all the other assertions would pass. This is the reason that you need the <code>Assert.True(db.IsDirty)</code> assertion in the Assert phase of the test. </p> <p> While we can apply to object-oriented code what we've learned from functional programming, the latter remains simpler. </p> <h3 id="011dc9e733ad44c9a656c07b2d420e1f"> Error test cases <a href="#011dc9e733ad44c9a656c07b2d420e1f" title="permalink">#</a> </h3> <p> While there's one happy path, there's four distinct error paths that you ought to cover. You can use the Fake database for that as well: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersFailToConnectWhenUserIdIsInvalid( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;userId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(userId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); &nbsp;&nbsp;&nbsp;&nbsp;db.Add(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(userId,&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(db.IsDirty); } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersFailToConnectWhenOtherUserIdIsInvalid( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(otherUserId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); &nbsp;&nbsp;&nbsp;&nbsp;db.Add(user); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUserId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(db.IsDirty); } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersDoNotConnectWhenUserDoesNotExist( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;userId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;db.Add(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(userId.ToString(),&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(db.IsDirty); } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersDoNotConnectWhenOtherUserDoesNotExist( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;otherUserId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;db.Add(user); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUserId.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(db.IsDirty); }</pre> </p> <p> There's little to say about these tests that hasn't already been said in at least one of the previous articles. All tests inspect the state of the Fake database after calling the <code>Post</code> method. The exact interactions between <code>Post</code> and <code>db</code> aren't specified. Instead, these tests rely on setting up the initial state, exercising the SUT, and verifying the final state. These are all state-based tests that avoid over-specifying the interactions. </p> <p> Specifically, none of these tests use Mocks and Stubs. In fact, at this incarnation of the test code, I was able to entirely remove the reference to <a href="https://github.com/moq/moq4">Moq</a>. </p> <h3 id="2e73c70d74cf40dfbb3e258e4293a5cf"> Summary <a href="#2e73c70d74cf40dfbb3e258e4293a5cf" title="permalink">#</a> </h3> <p> The premise of <a href="http://amzn.to/YPdQDf">Refactoring</a> is that in order to be able to refactor, the "precondition is [...] solid tests". In reality, many development organisations have the opposite experience. When programmers attempt to make changes to how their code is organised, tests break. In <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> this problem is called <em>Fragile Tests</em>, and the cause is often <em>Overspecified Software</em>. This means that tests are tightly coupled to implementation details of the SUT. </p> <p> It's easy to inadvertently fall into this trap when you use Mocks and Stubs, even when you follow the rule of using <a href="http://blog.ploeh.dk/2013/10/23/mocks-for-commands-stubs-for-queries">Mocks for Commands and Stubs for Queries</a>. Refactoring tests towards state-based testing with Fake objects, instead of interaction-based testing, could make test suites more robust to changes. </p> <p> It's intriguing, though, that state-based testing is simpler in functional programming. In Haskell, you can simply write your tests in the State monad and compare the expected outcome to the actual outcome. Since state in Haskell is immutable, it's trivial to compare the expected with the actual state. </p> <p> As soon as you introduce mutable state, structural equality is no longer safe, and instead you have to rely on other inspection mechanisms, such as the <code>IsDirty</code> flag seen in this, and the previous, article. This makes the tests slightly more brittle, because it tends to pull towards interaction-based testing. </p> <p> While you can implement the State monad in both F# and C#, it's probably more pragmatic to express state-based tests using mutable state and the occasional <code>IsDirty</code> flag. As always, there's no panacea. </p> <p> While this article concludes the series on moving towards state-based testing, I think that an appendix on Test Spies is in order. </p> <p> <strong>Next:</strong> <a href="/2019/04/08/a-pure-test-spy">A pure Test Spy</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b66a6b327b8949b69db335722c9501e3"> <div class="comment-author">ladeak <a href="#b66a6b327b8949b69db335722c9501e3">#</a></div> <div class="comment-content"> <p> If we had checked the FakeDB contains to user (by retrieving, similar as in the F# case), and assert Connections property on the retrieved objects, would we still need the IsDirty flag? I think it would be good to create a couple of cases which demonstrates refactoring, and how overspecified tests break with the interaction based tests, while works nicely here. </p> </div> <div class="comment-date">2019-04-05 17:20 UTC</div> </div> <div class="comment" id="39fa5e0bfdf24153854a260addf32d0e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#39fa5e0bfdf24153854a260addf32d0e">#</a></div> <div class="comment-content"> <p> ladeak, thank you for writing. The <code>IsDirty</code> flag is essentially a hack to work around the mutable nature of the <code>FakeDB</code>. As the <a href="/2019/03/25/an-example-of-state-based-testing-in-f#dd0384ed4c2d478f8374dbd55c86b197">previous article describes</a>: <blockquote> "In the previous article, the Fake database was simply an immutable dictionary. This meant that tests could easily compare expected and actual values, since they were immutable. When you use a mutable object, like the above dictionary, this is harder. Instead, what I chose to do here was to introduce an <code>IsDirty</code> flag. This enables easy verification of whether or not the database changed." </blockquote> The <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">Haskell example</a> demonstrates how no <code>IsDirty</code> flag is required, because you can simply compare the state before and after the SUT was exercised. </p> <p> You could do something similar in C# or F#, but that would require you to take an immutable snapshot of the Fake database before exercising the SUT, and then compare that snapshot with the state of the Fake database after the SUT was exercised. This is definitely also doable (as the Haskell example demonstrates), but a bit more work, which is the (unprincipled, pragmatic) reason I instead chose to use an <code>IsDirty</code> flag. </p> <p> Regarding more examples, I originally wrote another sample code base to support <a href="https://vimeo.com/296652563">this talk</a>. That sample code base contains examples that demonstrate how overspecified tests break even when you make small internal changes. I haven't yet, however, found a good home for that code base. </p> </div> <div class="comment-date">2019-04-06 10:25 UTC</div> </div> <div class="comment" id="ec4abdfcbf92495dabfb20b9b7458715"> <div class="comment-author">Sven Grosen <a href="#ec4abdfcbf92495dabfb20b9b7458715">#</a></div> <div class="comment-content"> <p> First of all, thank you for yet another awesome series. </p> <p> The fragility of my various employers' unit tests has always bothered me, but I couldn't necessarily offer an alternative that reduced/removed that. After further thought, I initially came up with two objections to this approach (based on actual enterprise experience), but was easily able to dismiss them: <ol> <li>What if the SUT has a lot of dependencies? <ul> <li>Then--following best practices--the SUT is doing too much</li> </ul> </li> <li>What if the dependency has a lot of methods <ul> <li>Then--following best practices--the dependency is doing too much</li> </ul> </li> </ol> The one point I wanted to seek clarification on though was that--as you spell out throughout the series--this "state-based" approach is no panacea and you may still need to do some test refactoring if you change the implementation of the SUT (e.g. if, referencing point #2 above, we break up a large dependency into a more targeted one). You may need to update your "fake" to account for that, but that that effort is much smaller than updating innumerable mock setup/verify calls, is that a correct summation? So I would have a "fake" per unit test fixture (to use nunit terminology) and would only need to update that one fake if/when the SUT for that fixture is refactored in such a way that impacts the fake. </p> <p> After reading this series I was trying to imagine how I could introduce this into my team's codebase where both of the objections I listed above are very real problems (I am new to the team and trying to wrangle with these issues). I imagine a pragmatic first step would be to define multiple fakes for a given large SUT that attempt to group dependency behavior in some logical fashion. Per usual, you've given me a lot to think about and some motivation to clean up some code! </p> </div> <div class="comment-date">2019-12-12 15:24 UTC</div> </div> <div class="comment" id="0c024d78ebeb418d9cc70c7a267daeed"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0c024d78ebeb418d9cc70c7a267daeed">#</a></div> <div class="comment-content"> <p> Sven, thank you for writing. I think that your summary of my position is accurate. A Fake affords a 'one-stop' place where you can go and address changes in you SUT APIs. You'll still need to edit test code (your Fake implementation), but in single place. </p> <p> We can, on the other hand, view multiple <code>Setup</code>/<code>Verify</code> changes as a violation of the DRY principle. </p> <p> I don't understand, however, why you want to involve the concept of a <a href="http://xunitpatterns.com/test%20fixture%20-%20ambiguous.html">Fixture</a>, one way or another. A Fake is Fake, regardless of the Fixture in which it appears. </p> </div> <div class="comment-date">2019-12-12 19:47 UTC</div> </div> <div class="comment" id="dd26a0b6dc964b96a003ff834caa0b89"> <div class="comment-author">Sven Grosen <a href="#dd26a0b6dc964b96a003ff834caa0b89">#</a></div> <div class="comment-content"> <p> <blockquote> I don't understand, however, why you want to involve the concept of a <a href="http://xunitpatterns.com/test%20fixture%20-%20ambiguous.html">Fixture</a>, one way or another. A Fake is Fake, regardless of the Fixture in which it appears. </blockquote> Mark, you are right and I had intended to remove that reference to fixtures but forgot to. I could easily see fakes living completely outside of any specific fixture. </p> </div> <div class="comment-date">2019-12-13 02:30 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An example of state based-testing in F# https://blog.ploeh.dk/2019/03/25/an-example-of-state-based-testing-in-f 2019-03-25T06:34:00+00:00 Mark Seemann <div id="post"> <p> <em>While F# is a functional-first language, it's okay to occasionally be pragmatic and use mutable state, for example to easily write some sustainable state-based tests.</em> </p> <p> This article is an instalment in an article series about how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based testing to state-based testing</a>. In the previous article, you saw how to write <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">state-based tests in Haskell</a>. In this article, you'll see how to apply what you've learned in <a href="https://fsharp.org">F#</a>. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="57929a79c0a740c5986df12fc6e4b1c1"> A function to connect two users <a href="#57929a79c0a740c5986df12fc6e4b1c1" title="permalink">#</a> </h3> <p> This article, like the others in this series, implements an operation to connect two users. I explain the example in details in my two <a href="https://cleancoders.com">Clean Coders</a> videos, <a href="https://cleancoders.com/episode/humane-code-real-episode-4/show">Church Visitor</a> and <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a>. </p> <p> Like in the previous <a href="https://www.haskell.org">Haskell</a> example, in this article we'll start with the implementation, and then see how to unit test it. </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Result&lt;User,UserLookupError&gt;)&nbsp;-&gt;&nbsp;(User&nbsp;-&gt;&nbsp;unit)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;HttpResponse&lt;User&gt;</span> <span style="color:blue;">let</span>&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;userId&nbsp;otherUserId&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;userRes&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lookupUser&nbsp;userId&nbsp;|&gt;&nbsp;Result.mapError&nbsp;(<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;InvalidId&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;NotFound&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;otherUserRes&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lookupUser&nbsp;otherUserId&nbsp;|&gt;&nbsp;Result.mapError&nbsp;(<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;InvalidId&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;NotFound&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;connect&nbsp;=&nbsp;result&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;user&nbsp;=&nbsp;userRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;otherUser&nbsp;=&nbsp;otherUserRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addConnection&nbsp;user&nbsp;otherUser&nbsp;|&gt;&nbsp;updateUser &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;otherUser&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;connect&nbsp;<span style="color:blue;">with</span>&nbsp;Ok&nbsp;u&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;OK&nbsp;u&nbsp;|&nbsp;Error&nbsp;msg&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;BadRequest&nbsp;msg</pre> </p> <p> While <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">the original C# example</a> used Constructor Injection, the above <code>post</code> function uses <a href="/2017/01/30/partial-application-is-dependency-injection">partial application for Dependency Injection</a>. The two function arguments <code>lookupUser</code> and <code>updateUser</code> represent interactions with a database. Since functions are polymorphic, however, it's possible to replace them with <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>. </p> <h3 id="dd0384ed4c2d478f8374dbd55c86b197"> A Fake database <a href="#dd0384ed4c2d478f8374dbd55c86b197" title="permalink">#</a> </h3> <p> Like in the Haskell example, you can implement a <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> database in F#. It's also possible to implement the State monad in F#, but there's less need for it. F# is a functional-first language, but you can also write mutable code if need be. You could, then, choose to be pragmatic and base your Fake database on mutable state. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;FakeDB&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;users&nbsp;=&nbsp;Dictionary&lt;int,&nbsp;User&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:blue;">val</span>&nbsp;IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>&nbsp;<span style="color:blue;">with</span>&nbsp;get,&nbsp;set &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.AddUser&nbsp;user&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;users.Add&nbsp;(user.UserId,&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.TryFind&nbsp;i&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;users.TryGetValue&nbsp;i&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;u&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Some&nbsp;u &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.LookupUser&nbsp;s&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Int32.TryParse&nbsp;s&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;InvalidId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;users.TryGetValue&nbsp;i&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;NotFound &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;u&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;u &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.UpdateUser&nbsp;u&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;users.[u.UserId]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;u</pre> </p> <p> This <code>FakeDB</code> type is <em>a class</em> that wraps a mutable dictionary. While it 'implements' <code>LookupUser</code> and <code>UpdateUser</code>, it also exposes what <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> calls a <em>Retrieval Interface:</em> an API that tests can use to examine the state of the object. </p> <p> Immutable values normally have structural equality. This means that two values are considered equal if they contain the same constituent values, and have the same structure. Mutable objects, on the other hand, typically have reference equality. This makes it harder to compare two objects, which is, however, what almost all unit testing is about. You compare expected state with actual state. </p> <p> In the previous article, the Fake database was simply an immutable dictionary. This meant that tests could easily compare expected and actual values, since they were immutable. When you use a mutable object, like the above dictionary, this is harder. Instead, what I chose to do here was to introduce an <code>IsDirty</code> flag. This enables easy verification of whether or not the database changed. </p> <h3 id="88dbfa0dd8f34f4ba91f2b7acc6173b1"> Happy path test case <a href="#88dbfa0dd8f34f4ba91f2b7acc6173b1" title="permalink">#</a> </h3> <p> This is all you need in terms of Test Doubles. You now have test-specific <code>LookupUser</code> and <code>UpdateUser</code> methods that you can pass to the <code>post</code> function. </p> <p> Like in the previous article, you can start by exercising the happy path where a user successfully connects with another user: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;successfully&nbsp;connect``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;user&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;otherUser&nbsp;=&nbsp;Gen.withOtherId&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;otherUser &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;(string&nbsp;user.UserId)&nbsp;(string&nbsp;otherUser.UserId) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;db.TryFind&nbsp;user.UserId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">fun</span>&nbsp;u&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;u.ConnectedUsers&nbsp;|&gt;&nbsp;List.contains&nbsp;otherUser)&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isOK&nbsp;actual&nbsp;@&gt;&nbsp;}</pre> </p> <p> All tests in this article use <a href="https://xunit.net">xUnit.net</a> 2.3.1, <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> 4.0.0, and <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> 0.7.0.0. </p> <p> This test first adds two valid users to the Fake database <code>db</code>. It then calls the <code>post</code> function, passing the <code>db.LookupUser</code> and <code>db.UpdateUser</code> methods as arguments. Finally, it verifies that the 'first' user's <code>ConnectedUsers</code> now contains the <code>otherUser</code>. It also verifies that <code>actual</code> represents a <code>200 OK</code> HTTP response. </p> <h3 id="750f435b6e854db898e0da44119ee4f6"> Missing user test case <a href="#750f435b6e854db898e0da44119ee4f6" title="permalink">#</a> </h3> <p> While there's one happy-path test case, there's four other test cases left. One of these is when the first user doesn't exist: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;user&nbsp;doesn&#39;t&nbsp;exist``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;i&nbsp;=&nbsp;Range.linear&nbsp;1&nbsp;1_000_000&nbsp;|&gt;&nbsp;Gen.int &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;otherUser&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;otherUser &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uniqueUserId&nbsp;=&nbsp;string&nbsp;(otherUser.UserId&nbsp;+&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;uniqueUserId&nbsp;(string&nbsp;otherUser.UserId) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;db.IsDirty&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isBadRequest&nbsp;actual&nbsp;@&gt;&nbsp;}</pre> </p> <p> This test adds one valid user to the Fake database. Once it's done with configuring the database, it sets <code>IsDirty</code> to <code>false</code>. The <code>AddUser</code> method sets <code>IsDirty</code> to <code>true</code>, so it's important to reset the flag before the <em>act</em> phase of the test. You could consider this a bit of a hack, but I think it makes the intent of the test clear. This is, however, a position I'm ready to reassess should the tests evolve to make this design awkward. </p> <p> As explained in the previous article, this test case requires an ID of a user that doesn't exist. Since this is a property-based test, there's a risk that Hedgehog might generate a number <code>i</code> equal to <code>otherUser.UserId</code>. One way to get around that problem is to add the two numbers together. Since <code>i</code> is generated from the range <em>1 - 1,000,000</em>, <code>uniqueUserId</code> is guaranteed to be different from <code>otherUser.UserId</code>. </p> <p> The test verifies that the state of the database didn't change (that <code>IsDirty</code> is still <code>false</code>), and that <code>actual</code> represents a <code>400 Bad Request</code> HTTP response. </p> <h3 id="4dbe21251f8440b1a594192d07b53c9f"> Remaining test cases <a href="#4dbe21251f8440b1a594192d07b53c9f" title="permalink">#</a> </h3> <p> You can write the remaining three test cases in the same vein: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;other&nbsp;user&nbsp;doesn&#39;t&nbsp;exist``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;i&nbsp;=&nbsp;Range.linear&nbsp;1&nbsp;1_000_000&nbsp;|&gt;&nbsp;Gen.int &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;user&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uniqueOtherUserId&nbsp;=&nbsp;string&nbsp;(user.UserId&nbsp;+&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;(string&nbsp;user.UserId)&nbsp;uniqueOtherUserId&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;db.IsDirty&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isBadRequest&nbsp;actual&nbsp;@&gt;&nbsp;} [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;user&nbsp;Id&nbsp;is&nbsp;invalid``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;s&nbsp;=&nbsp;Gen.alphaNum&nbsp;|&gt;&nbsp;Gen.string&nbsp;(Range.linear&nbsp;0&nbsp;100)&nbsp;|&gt;&nbsp;Gen.filter&nbsp;isIdInvalid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;otherUser&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;otherUser &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;s&nbsp;(string&nbsp;otherUser.UserId) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;db.IsDirty&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isBadRequest&nbsp;actual&nbsp;@&gt;&nbsp;} [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;other&nbsp;user&nbsp;Id&nbsp;is&nbsp;invalid``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;s&nbsp;=&nbsp;Gen.alphaNum&nbsp;|&gt;&nbsp;Gen.string&nbsp;(Range.linear&nbsp;0&nbsp;100)&nbsp;|&gt;&nbsp;Gen.filter&nbsp;isIdInvalid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;user&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;(string&nbsp;user.UserId)&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;db.IsDirty&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isBadRequest&nbsp;actual&nbsp;@&gt;&nbsp;}</pre> </p> <p> All tests inspect the state of the Fake database after the calling the <code>post</code> function. The exact interactions between <code>post</code> and <code>db</code> aren't specified. Instead, these tests rely on setting up the initial state, exercising the System Under Test, and verifying the final state. These are all state-based tests that avoid over-specifying the interactions. </p> <h3 id="102ff7e3a9b247b4914f113b06afd6df"> Summary <a href="#102ff7e3a9b247b4914f113b06afd6df" title="permalink">#</a> </h3> <p> While the previous Haskell example demonstrated that it's possible to write state-based unit tests in a functional style, when using F#, it sometimes make sense to leverage the object-oriented features already available in the .NET framework, such as mutable dictionaries. It would have been possible to write purely functional state-based tests in F# as well, by porting the Haskell examples, but here, I wanted to demonstrate that this isn't required. </p> <p> I tend to be of the opinion that it's only possible to be pragmatic if you know how to be dogmatic, but now that we know how to write state-based tests in a strictly functional style, I think it's fine to be pragmatic and use a bit of mutable state in F#. The benefit of this is that it now seems clear how to apply what we've learned to the original C# example. </p> <p> <strong>Next: </strong> <a href="/2019/04/01/an-example-of-state-based-testing-in-c">An example of state-based testing in C#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The programmer as decision maker https://blog.ploeh.dk/2019/03/18/the-programmer-as-decision-maker 2019-03-18T07:44:00+00:00 Mark Seemann <div id="post"> <p> <em>As a programmer, your job is to make technical decisions. Make some more.</em> </p> <p> When <a href="/schedule">I speak at conferences</a>, people often come and talk to me. (I welcome that, BTW.) Among all the conversations I've had over the years, there's a pattern to some of them. The attendee will start by telling me how inspired (s)he is by the talk I just gave, or something I've written. That's gratifying, and a good way to start a conversation, but is often followed up like this: </p> <p> <strong>Attendee:</strong> "I just wish that we could do something like that in our organisation..." </p> <p> Let's just say that here we're talking about test-driven development, or perhaps just unit testing. Nothing too controversial. I'd typically respond, </p> <p> <strong>Me:</strong> "Why can't you?" </p> <p> <strong>Attendee:</strong> "Our boss won't let us..." </p> <p> That's unfortunate. If your boss has explicitly forbidden you to write and run unit tests, then there's not much you can do. Let me make this absolutely clear: I'm not going on record saying that you should actively disobey a direct order (unless it's unethical, that is). I do wonder, however: </p> <p> <em>Why is the boss even involved in that decision?</em> </p> <p> It seems to me that programmers often defer too much authority to their managers. </p> <h3 id="510367ded1ca47e7922b540d2fff50b0"> A note on culture <a href="#510367ded1ca47e7922b540d2fff50b0" title="permalink">#</a> </h3> <p> I'd like to preface the rest of this article with my own context. I've spent most of my programming career in Danish organisations. Even when I worked for Microsoft, I worked for Danish subsidiaries, with Danish managers. </p> <p> The power distance in Denmark is (in)famously short. It's not unheard of for individual contributors to question their superiors' decisions; sometimes to their face, and sometimes even when other people witness this. When done respectfully (which it often is), this can be extremely efficient. Managers are as fallible as the rest of us, and often their subordinates know of details that could impact a decision that a manager is about to make. Immediately discussing such details can help ensure that good decisions are made, and bad decisions are cancelled. </p> <p> This helps managers make better decisions, so enlightened managers welcome feedback. </p> <p> In general, Danish employees also tend to have a fair degree of autonomy. What I'll suggest in this article is unlikely to get you fired in Denmark. Please use your own judgement if you consider transplanting the following to your own culture. </p> <h3 id="eaa896ed8a6240729301c54fed859ab7"> Technical decisions <a href="#eaa896ed8a6240729301c54fed859ab7" title="permalink">#</a> </h3> <p> If your job is <em>programmer</em>, <em>software developer</em>, or similar, the value you add to the team is that you bring <em>technical expertise</em>. Maybe some of your colleagues are programmers as well, but together, you are the people with the technical expertise. </p> <p> Even if the project manager or other superiors used to program, unless they're also writing code for the current code base, they only have general technical expertise, but not specific expertise related to the code base you're working with. The people with most technical expertise are you and your colleagues. </p> <p> You are decision makers. </p> <p> Whenever you interact with your code base, you make technical decisions. </p> <p> In order to handle incoming HTTP requests to a <code>/reservations</code> resource, you may first decide to create a <a href="/2019/02/11/asynchronous-injection">new file called <code>ReservationsController.cs</code></a>. You'd most likely also decide to open that file and start adding code to it. </p> <p> Perhaps you add a method called <code>Post</code> that takes a <code>Reservation</code> argument. Perhaps you decide to inject an <code>IMaîtreD</code> dependency. </p> <p> At various steps along the way, you may decide to compile the code. </p> <p> Once you think that you've made enough changes to address your current work item, you may decide to run the program to see if it works. For a web-based piece of software, that typically involves starting up a browser and somehow interacting with the service. If your program is a web site, you may start at the front page, log in, click around, and fill in some forms. If your program is a REST API, you may interact with it via Fiddler or Postman (I prefer curl or <a href="https://github.com/ploeh/Furl">Furl</a>, but most people I've met still prefer something they can click on, it seems). </p> <p> What often happens is that your changes don't work the first time around, so you'll have to troubleshoot. Perhaps you decide to use a debugger. </p> <p> How many decisions are that? </p> <p> I just described seven or eight types of the sort of decisions you make as a programmer. You make such decisions all the time. Do you ask your managers permission before you start a debugging session? Before you create a new file? Before you name a variable? </p> <p> Of course you don't. You're the technical expert. There's no-one better equipped than you or your team members to make those decisions. </p> <h3 id="bed169525ccf4a90aaa9c0e33fcbf8d0"> Decide to add unit tests <a href="#bed169525ccf4a90aaa9c0e33fcbf8d0" title="permalink">#</a> </h3> <p> If you want to add unit tests, why don't you just decide to add them? If you want to apply test-driven development, why don't you just do so? </p> <p> A unit test is one or more code files. You're already authorised to make decisions about adding files. </p> <p> You can run a test suite instead of launching the software every time you want to interact with it. It's likely to be faster, even. </p> <p> Why should you ask permission to do that? </p> <h3 id="e42aac76e52141e68a0bc08fefa56a9b"> Decide to refactor <a href="#e42aac76e52141e68a0bc08fefa56a9b" title="permalink">#</a> </h3> <p> Another complaint I hear is that people aren't allowed to refactor. </p> <p> Why are you even asking permission to refactor? </p> <p> <a href="http://amzn.to/YPdQDf">Refactoring</a> means reorganising the code without changing the behaviour of the system. Another word for that is <em>editing</em> the code. It's okay. You're already permitted to edit code. It's part of your job description. </p> <p> I think I know what the underlying problem is, though... </p> <h3 id="f8c290ef29bd437daf2a81b3e06e131c"> Make technical decisions in the small <a href="#f8c290ef29bd437daf2a81b3e06e131c" title="permalink">#</a> </h3> <p> As an individual contributor, you're empowered to make small-scale technical decisions. These are decisions that are unlikely to impact schedules or allocation of programmers, including new hires. Big decisions probably should involve your manager. </p> <p> I have an inkling of why people feel that they need permission to refactor. It's because the refactoring they have in mind is going to take weeks. Weeks in which nothing else can be done. Weeks where perhaps the code doesn't even compile. </p> <p> Many years ago (but not as many as I'd like it to be), my colleague and I had what Eric Evans in <a href="http://amzn.to/WBCwx7">DDD</a> calls a <em>breakthrough</em>. We wanted to refactor towards deeper insight. What prompted the insight was a new feature that we had to add, and we'd been throwing design ideas back and forth for some time before the new insight arrived. </p> <p> We could implement the new feature if we changed one of the core abstractions in our domain model, but it required substantial changes to the existing code base. We informed our manager of our new insight and our plan, estimating that it would take less than a week to make the changes and implement the new feature. Our manager agreed with the plan. </p> <p> Two weeks later our code hadn't been in a compilable state for a week. Our manager pulled me away to tell me, quietly and equitably, that he was not happy with our lack of progress. I could only concur. </p> <p> After more heroic work, we finally managed to complete the changes and implement the new feature. Nonetheless, blocking all other development for two-three weeks in order to make a change isn't acceptable. </p> <p> That sort of change is a big decision because it impacts other team members, schedules, and perhaps overall business plans. Don't make those kinds of decisions without consulting with stakeholders. </p> <p> This still leaves, I believe, lots of room for individual decision-making in the small. What I learned from the experience I just recounted was not to engage in big changes to a code base. Learn how to make multiple incremental changes instead. In case that's completely impossible, add the new model side-by-side with the old model, and incrementally change over. That's what I should have done those many years ago. </p> <h3 id="298ca23885544e5f906379c1a6d15586"> Don't be sneaky <a href="#298ca23885544e5f906379c1a6d15586" title="permalink">#</a> </h3> <p> When I give talks about the blessings of functional programming, I sometimes get into another type of discussion. </p> <p> <strong>Attendee:</strong> It's so inspiring how beautiful and simple complex domain models become in <a href="https://fsharp.org">F#</a>. How can we do the same in C#? </p> <p> <strong>Me:</strong> You can't. If you're already using C#, you should strongly consider F# if you wish to do functional programming. Since it's also a .NET language, you can gradually introduce F# code and mix the compiled code with your existing C# code. </p> <p> <strong>Attendee:</strong> Yes... [already getting impatient with me] But we can't do that... </p> <p> <strong>Me:</strong> Why not? </p> <p> <strong>Attendee:</strong> Because our manager will not allow it. </p> <p> Based on the suggestions I've already made here, you may expect me to say that that's another technical decision that you should make without asking permission. Like the previous example about blocking refactorings, however, this is another large-scale decision. </p> <p> Your manager may be concerned that it'd be hard to find new employees if the code base is written in some niche language. <a href="/2015/12/03/the-rules-of-attraction-language">I tend to disagree with that position</a>, but I do understand why a manager would take that position. While I think it suboptimal to restrict an entire development organisation to a single language (whether it's C#, Java, C++, Ruby, etc.), I'll readily accept that language choice is a strategic decision. </p> <p> If every programmer got to choose the programming language they prefer the most that day, you'd have code bases written in dozens of different languages. While you can train bright new hires to learn a new language or two, it's unrealistic that a new employee will be able to learn thirty different languages in a short while. </p> <p> I find it reasonable that a manager has the final word on the choice of language, even when I often disagree with the decisions. </p> <p> The outcome usually is that people are stuck with C# (or Java, or...). Hence the question: <em>How can we do functional programming in C#?</em> </p> <p> I'll give the answer that I often give here on the blog: <a href="https://en.wikipedia.org/wiki/Mu_(negative)">mu</a> (<em>unask the question</em>). You can, in fact, translate functional concepts to C#, but <a href="/2018/07/24/dependency-injection-revisited">the result is so non-idiomatic</a> that only the syntax remains of C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isReservationInFuture:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IsReservationInFuture</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;=&gt;&nbsp;selector(t.Item2(b)))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReadReservations</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&gt;&nbsp;selector(t.Item2(d)))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Create</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&gt;&nbsp;selector(t.Item2(r))))); }</pre> </p> <p> Keep in mind the manager's motivation for standardising on C#. It's often related to concerns about being able to hire new employees, or move employees from project to project. </p> <p> If you write 'functional' C#, you'll end up with code like the above, or the following real-life example: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;sendRequest( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ApiMethodNames</span>.InitRegistration, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GSObject</span>()) &nbsp;&nbsp;&nbsp;&nbsp;.Map(r&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">ValidateResponse</span>.Validate(r) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.MapFailure(_&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">ErrorResponse</span>.RegisterErrorResponse())) &nbsp;&nbsp;&nbsp;&nbsp;.Bind(r&nbsp;=&gt;&nbsp;r.RetrieveField(<span style="color:#a31515;">&quot;regToken&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;.BindAsync(token&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendRequest( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ApiMethodNames</span>.RegisterAccount, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CreateRegisterRequest( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mailAddress, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;token)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Map(<span style="color:#2b91af;">ValidateResponse</span>.Validate) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Bind(response&nbsp;=&gt;&nbsp;getIdentity(response) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToResult(<span style="color:#2b91af;">ErrorResponse</span>.ExternalServiceResponseInvalid))) &nbsp;&nbsp;&nbsp;&nbsp;.Map(id&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">GigyaIdentity</span>.CreateNewSiteUser(id.UserId,&nbsp;mailAddress));</pre> </p> <p> (I'm indebted to <a href="https://twitter.com/runeibsen">Rune Ibsen</a> for this example.) </p> <p> A new hire can have ten years of C# experience and still have no chance in a code base like that. You'll first have to teach him or her functional programming. If you can do that, you might as well also teach a new language, like F#. </p> <p> It's my experience that learning the syntax of a new language is easy, and usually doesn't take much time. The hard part is learning a new way to think. </p> <p> Writing 'functional' C# makes it doubly hard on new team members. Not only do they have to learn a new paradigm (functional programming), but they have to learn it in a language unsuited for that paradigm. </p> <p> That's why I think you should unask the question. If your manager doesn't want to allow F#, then writing 'functional' C# is just being sneaky. That'd be obeying the letter of the law while breaking the spirit of it. That is, in my opinion, immoral. Don't be sneaky. </p> <h3 id="2f1b38953bbc4814994ee471caf4c5ac"> Summary <a href="#2f1b38953bbc4814994ee471caf4c5ac" title="permalink">#</a> </h3> <p> As a professional programmer, your job is to be a technical expert. In normal circumstances (at least the ones I know from my own career), you have agency. In order to get anything done, you make small decisions all the time, such as editing code. That's not only okay, but expected of you. </p> <p> Some decision, on the other hand, can have substantial ramifications. Choosing to write code in an unsanctioned language tends to fall on the side where a manager should be involved in the decision. </p> <p> In between is a grey area. </p> <p> <img src="/content/binary/small-vs-big-decisions-gradient.png" alt="A spectrum of decisions from small to the left to big to the right."> </p> <p> I don't even consider adding unit tests to be in the grey area, but some refactorings may be. <blockquote> <p>"It's easier to ask forgiveness than it is to get permission."</p> <footer><cite>Grace Hopper</cite></footer> </blockquote> </p> <p> To navigate grey areas you need a moral compass. </p> <p> I'll let you be the final judge of what you can get away with, but I consider it both appropriate and ethical to make the decision to add unit tests, and to continually improve code bases. You shouldn't have to ask permission to do that. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="dd2ab8d5dc6e4c5f9e49d7d7f35a8759"> <div class="comment-author"><a href="https://github.com/chicocode">Francisco Berrocal</a> <a href="#dd2ab8d5dc6e4c5f9e49d7d7f35a8759">#</a></div> <div class="comment-content"> <p>Before all, I'd just like to thank all the content you share, they all make me think in a good way!</p> <p> Now regarding to this post, while I tend to agree that a developer can take the decision to add (or not) unit tests by himself, there is no great value comming out of it, if that's not an approach of the whole development team, right? I believe we need the entire team on board to maximize the values of unit tests. There are changes we need to consider, from changes in the mindset of how you develop to actually running them on continuour integration pipelines. Doesn't all of that push simple decisions like "add unit test" from green area towards orange area? </p> </div> <div class="comment-date">2019-03-18 13:14 UTC</div> </div> <div class="comment" id="2bc69a5123d8499ca40631b9ce946919"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2bc69a5123d8499ca40631b9ce946919">#</a></div> <div class="comment-content"> <p> Francisco, thank you for writing. If you have a team of developers, then I agree that unit tests are going to be most valuable if the team decides to use them. </p> <p> This is still something that you ought to be competent to decide as a self-organising team of developers. Do you need to ask a manager's permission? </p> <p> I'm not trying to pretend that this is easy. I realise that it can be difficult. </p> <p> I've heard about teams where other developers are hostile to the idea of unit testing. In that situation, I can offer no easy fixes. What a lone developer can try to do in that situation is to add and run unit tests locally, on his or her own machine. This will incur some friction, because other team members will be oblivious to the tests, so they'll change code that will cause those unit tests to break. </p> <p> This might teach the lone developer to write tests so that they're as robust to trivial changes as possible. That's a valuable skill in any case. There's still going to be some overhead of maintaining the unit tests in a scenario like that, but if that overhead is smaller than the productivity gained, then in might still be worthwhile. </p> <p> What might then happen could be that other developers who are on the fence see that the lone unit tester is more effective than they are. Perhaps they'll get curious about unit tests after all, once they can see the contours of advantages. </p> <p> The next scenario, then, is a team with a few developers writing unit tests, and other who don't. At some number, you'll have achieved enough critical mass that, at least, you get to check in the unit tests together with the source code. Soon after, you may be able to institute a policy that while not everyone writes unit tests, it's not okay to break existing tests. </p> <p> The next thing you can do, then, is to set up a test run as part of continuous integration and declare that a failing test run means that the build broke. You still have team members who don't write tests, but at least you get to do it, and the tests add value to the whole team. </p> <p> Perhaps the sceptics will slowly start to write unit tests over time. Some die-hards probably never will. </p> <p> You may be able to progress through such stages without asking a manager, but I do understand that there's much variation in organisation and team dynamics. If you can use any of the above sketches as inspiration, then that's great. If you (or other readers) have other success stories to tell, then please share them. </p> <p> The point I was trying to make with this article is that programmers have agency. This isn't a licence to do whatever you please. You still have to navigate the dynamics of whatever organisation you're in. You may not, however, need to ask your manager about every little thing that you're competent to decide yourselves. </p> </div> <div class="comment-date">2019-03-19 7:57 UTC</div> </div> <div class="comment"> <div class="comment-author"><a href="https://hettomei.github.io/">Timothée GAUTHIER</a> <a href="#">#</a></div> <div class="comment-content"> <p> Thank you A LOT for putting words on all these thought. You'll be my reference whenever I want to introduce unit test. </p> <p> My usual example is "a surgeon doesn't need to ask to the manager if he can wash his hand. Whashing his hand is part of his job". (Not mine, but I can't remember where it comes from) </p> </div> <div class="comment-date">2019-03-19 20:15 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An example of state-based testing in Haskell https://blog.ploeh.dk/2019/03/11/an-example-of-state-based-testing-in-haskell 2019-03-11T07:55:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you do state-based testing when state is immutable? You use the State monad.</em> </p> <p> This article is an instalment in an article series about how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based testing to state-based testing</a>. In the previous article, you saw <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">an example of an interaction-based unit test</a> written in C#. The problem that this article series attempts to address is that interaction-based testing can lead to what <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> calls <em>Fragile Tests</em>, because the tests get coupled to implementation details, instead of overall behaviour. </p> <p> My experience is that functional programming is better aligned with unit testing because <a href="/2015/05/07/functional-design-is-intrinsically-testable">functional design is intrinsically testable</a>. While I believe that functional programming is no panacea, it still seems to me that we can learn many valuable lessons about programming from it. </p> <p> People often ask me about <a href="https://fsharp.org">F#</a> programming: <em>How do I know that my F# code is functional?</em> </p> <p> I sometimes wonder that myself, about my own F# code. One can certainly choose to ignore such a question as irrelevant, and I sometimes do, as well. Still, in my experience, asking such questions can create learning opportunities. </p> <p> The best answer that I've found is: <em>Port the F# code to <a href="https://www.haskell.org">Haskell</a>.</em> </p> <p> Haskell enforces <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a> via its compiler. If Haskell code compiles, it's functional. In this article, then, I take the problem from the previous article and port it to Haskell. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="ae68546c3d9f4810a18ea99c2bcc873c"> A function to connect two users <a href="#ae68546c3d9f4810a18ea99c2bcc873c" title="permalink">#</a> </h3> <p> In the previous article, you saw implementation and test coverage of a piece of software functionality to connect two users with each other. This was a simplification of the example running through my two <a href="https://cleancoders.com">Clean Coders</a> videos, <a href="https://cleancoders.com/episode/humane-code-real-episode-4/show">Church Visitor</a> and <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a>. </p> <p> In contrast to the previous article, we'll start with the implementation of the System Under Test (SUT). </p> <p> <pre><span style="color:#2b91af;">post</span>&nbsp;::&nbsp;<span style="color:blue;">Monad</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a&nbsp;-&gt;&nbsp;m&nbsp;(Either&nbsp;UserLookupError&nbsp;User))&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(User&nbsp;-&gt;&nbsp;m&nbsp;<span style="color:blue;">()</span>)&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;(HttpResponse&nbsp;User) post&nbsp;lookupUser&nbsp;updateUser&nbsp;userId&nbsp;otherUserId&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;userRes&nbsp;&lt;-&nbsp;first&nbsp;(\<span style="color:blue;">case</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InvalidId&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotFound&nbsp;&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&lt;$&gt;&nbsp;lookupUser&nbsp;userId &nbsp;&nbsp;otherUserRes&nbsp;&lt;-&nbsp;first&nbsp;(\<span style="color:blue;">case</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InvalidId&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotFound&nbsp;&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&lt;$&gt;&nbsp;lookupUser&nbsp;otherUserId &nbsp;&nbsp;connect&nbsp;&lt;-&nbsp;runExceptT&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user&nbsp;&lt;-&nbsp;ExceptT&nbsp;$&nbsp;<span style="color:blue;">return</span>&nbsp;userRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;otherUser&nbsp;&lt;-&nbsp;ExceptT&nbsp;$&nbsp;<span style="color:blue;">return</span>&nbsp;otherUserRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lift&nbsp;$&nbsp;updateUser&nbsp;$&nbsp;addConnection&nbsp;user&nbsp;otherUser &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;otherUser &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;either&nbsp;BadRequest&nbsp;OK&nbsp;connect </pre> </p> <p> This is as direct a translation of the C# code as makes sense. If I'd only been implementing the desired functionality in Haskell, without having to port existing code, I'd designed the code differently. </p> <p> This <code>post</code> function uses <a href="/2017/01/30/partial-application-is-dependency-injection">partial application as an analogy to dependency injection</a>, but in order to enable potentially impure operations to take place, everything must happen inside of some monad. While the production code must ultimately run in the <code>IO</code> monad in order to interact with a database, tests can choose to run in another monad. </p> <p> In the C# example, two dependencies are injected into the class that defines the <code>Post</code> method. In the above Haskell function, these two dependencies are instead passed as function arguments. Notice that both functions return values in the monad <code>m</code>. </p> <p> The intent of the <code>lookupUser</code> argument is that it'll query a database with a user ID. It'll return the user if present, but it could also return a <code>UserLookupError</code>, which is a simple <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type:</a> </p> <p> <pre><span style="color:blue;">data</span>&nbsp;UserLookupError&nbsp;=&nbsp;InvalidId&nbsp;|&nbsp;NotFound&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>)</pre> </p> <p> If both users are found, the function connects the users and calls the <code>updateUser</code> function argument. The intent of this 'dependency' is that it updates the database. This is recognisably a <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command</a>, since its return type is <code>m ()</code> - <a href="/2018/01/15/unit-isomorphisms"><em>unit</em> (<code>()</code>) is equivalent to <code>void</code></a>. </p> <h3 id="6855a96abefe47c3a924b6d7d94e7bc8"> State-based testing <a href="#6855a96abefe47c3a924b6d7d94e7bc8" title="permalink">#</a> </h3> <p> How do you unit test such a function? How do you use Mocks and Stubs in Haskell? You don't; you don't have to. While the <code>post</code> method <em>can</em> be impure (when <code>m</code> is <code>IO</code>), it doesn't have to be. Functional design is intrinsically testable, but that proposition depends on purity. Thus, it's worth figuring out how to keep the <code>post</code> function <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> in the context of unit testing. </p> <p> While <code>IO</code> implies impurity, most common monads are pure. Which one should you choose? You could attempt to entirely 'erase' the monadic quality of the <code>post</code> function with the <code>Identity</code> monad, but if you do that, you can't verify whether or not <code>updateUser</code> was invoked. </p> <p> While you <em>could</em> write an ad-hoc Mock using, for example, the <code>Writer</code> monad, it might be a better choice to investigate if something closer to state-based testing would be possible. </p> <p> In an object-oriented context, state-based testing implies that you exercise the SUT, which mutates some state, and then you verify that the (mutated) state matches your expectations. You can't do that when you test a pure function, but you can examine the state of the function's return value. The <code>State</code> monad is an obvious choice, then. </p> <h3 id="6d9ce24e5e374c5d9987ac481b1fbd37"> A Fake database <a href="#6d9ce24e5e374c5d9987ac481b1fbd37" title="permalink">#</a> </h3> <p> Haskell's <code>State</code> monad is parametrised on the state type as well as the normal 'value type', so in order to be able to test the <code>post</code> function, you'll have to figure out what type of state to use. The interactions implied by the <code>post</code> function's <code>lookupUser</code> and <code>updateUser</code> arguments are those of database interactions. A <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> database seems an obvious choice. </p> <p> For the purposes of testing the <code>post</code> function, an in-memory database implemented using a <code>Map</code> is appropriate: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;DB&nbsp;=&nbsp;Map&nbsp;Integer&nbsp;User</pre> </p> <p> This is simply a dictionary keyed by <code>Integer</code> values and containing <code>User</code> values. You can implement compatible <code>lookupUser</code> and <code>updateUser</code> functions with <code>State DB</code> as the <code>Monad</code>. The <code>updateUser</code> function is the easiest one to implement: </p> <p> <pre><span style="color:#2b91af;">updateUser</span>&nbsp;::&nbsp;<span style="color:blue;">User</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">State</span>&nbsp;<span style="color:blue;">DB</span>&nbsp;() updateUser&nbsp;user&nbsp;=&nbsp;modify&nbsp;$&nbsp;Map.insert&nbsp;(userId&nbsp;user)&nbsp;user</pre> </p> <p> This simply inserts the <code>user</code> into the database, using the <code>userId</code> as the key. The type of the function is compatible with the general requirement of <code>User -&gt; m ()</code>, since here, <code>m</code> is <code>State DB</code>. </p> <p> The <code>lookupUser</code> Fake implementation is a bit more involved: </p> <p> <pre><span style="color:#2b91af;">lookupUser</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">State</span>&nbsp;<span style="color:blue;">DB</span>&nbsp;(<span style="color:#2b91af;">Either</span>&nbsp;<span style="color:blue;">UserLookupError</span>&nbsp;<span style="color:blue;">User</span>) lookupUser&nbsp;s&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;maybeInt&nbsp;=&nbsp;readMaybe&nbsp;s&nbsp;::&nbsp;Maybe&nbsp;Integer &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;eitherInt&nbsp;=&nbsp;<span style="color:blue;">maybe</span>&nbsp;(Left&nbsp;InvalidId)&nbsp;Right&nbsp;maybeInt &nbsp;&nbsp;db&nbsp;&lt;-&nbsp;get &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;eitherInt&nbsp;&gt;&gt;=&nbsp;<span style="color:blue;">maybe</span>&nbsp;(Left&nbsp;NotFound)&nbsp;Right&nbsp;.&nbsp;<span style="color:blue;">flip</span>&nbsp;Map.<span style="color:blue;">lookup</span>&nbsp;db</pre> </p> <p> First, consider the type. The function takes a <code>String</code> value as an argument and returns a <code>State DB (Either UserLookupError User)</code>. The requirement is a function compatible with the type <code>a -&gt; m (Either UserLookupError User)</code>. This works when <code>a</code> is <code>String</code> and <code>m</code> is, again, <code>State DB</code>. </p> <p> The entire function is written in <code>do</code> notation, where the inferred <code>Monad</code> is, indeed, <code>State DB</code>. The first line attempts to parse the <code>String</code> into an <code>Integer</code>. Since the built-in <code>readMaybe</code> function returns a <code>Maybe Integer</code>, the next line uses the <code>maybe</code> function to handle the two possible cases, converting the <code>Nothing</code> case into the <code>Left InvalidId</code> value, and the <code>Just</code> case into a <code>Right</code> value. </p> <p> It then uses the <code>State</code> module's <code>get</code> function to access the database <code>db</code>, and finally attempt a <code>lookup</code> against that <code>Map</code>. Again, <code>maybe</code> is used to convert the <code>Maybe</code> value returned by <code>Map.lookup</code> into an <code>Either</code> value. </p> <h3 id="280bfa2e191e42d095f6f762e0a94a55"> Happy path test case <a href="#280bfa2e191e42d095f6f762e0a94a55" title="permalink">#</a> </h3> <p> This is all you need in terms of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>. You now have test-specific <code>lookupUser</code> and <code>updateUser</code> functions that you can pass to the <code>post</code> function. </p> <p> Like in the previous article, you can start by exercising the happy path where a user successfully connects with another user: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;successfully&nbsp;connect&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;user&nbsp;otherUser&nbsp;-&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;put&nbsp;$&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;user,&nbsp;toDBEntry&nbsp;otherUser] &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;user)&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;otherUser) &nbsp;&nbsp;db&nbsp;&lt;-&nbsp;get &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;isOK&nbsp;actual&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">any</span>&nbsp;(<span style="color:blue;">elem</span>&nbsp;otherUser&nbsp;.&nbsp;connectedUsers)&nbsp;(Map.<span style="color:blue;">lookup</span>&nbsp;(userId&nbsp;user)&nbsp;db)</pre> </p> <p> Here I'm <a href="/2018/05/07/inlined-hunit-test-lists">inlining test cases as anonymous functions</a> - this time expressing the tests as QuickCheck properties. I'll later return to the <code>runStateTest</code> helper function, but first I want to focus on the test body itself. It's written in <code>do</code> notation, and specifically, it runs in the <code>State DB</code> monad. </p> <p> <code>user</code> and <code>otherUser</code> are input arguments to the property. These are both <code>User</code> values, since the test also defines <code>Arbitrary</code> instances for that type (not shown in this article; see the source code repository for details). </p> <p> The first step in the test is to 'save' both users in the Fake database. This is easily done by converting each <code>User</code> value to a database entry: </p> <p> <pre><span style="color:#2b91af;">toDBEntry</span>&nbsp;::&nbsp;<span style="color:blue;">User</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(<span style="color:#2b91af;">Integer</span>,&nbsp;<span style="color:blue;">User</span>) toDBEntry&nbsp;=&nbsp;userId&nbsp;&amp;&amp;&amp;&nbsp;<span style="color:blue;">id</span></pre> </p> <p> Recall that the Fake database is nothing but an alias over <code>Map Integer User</code>, so the only operation required to turn a <code>User</code> into a database entry is to extract the key. </p> <p> The next step in the test is to exercise the SUT, passing the test-specific <code>lookupUser</code> and <code>updateUser</code> Test Doubles to the <code>post</code> function, together with the user IDs converted to <code>String</code> values. </p> <p> In the <em>assert</em> phase of the test, it first extracts the current state of the database, using the <code>State</code> library's built-in <code>get</code> function. It then verifies that <code>actual</code> represents a <code>200 OK</code> value, and that the <code>user</code> entry in the database now contains <code>otherUser</code> as a connected user. </p> <h3 id="050ecaa9962a487c9381da982ab264e7"> Missing user test case <a href="#050ecaa9962a487c9381da982ab264e7" title="permalink">#</a> </h3> <p> While there's one happy-path test case, there's four other test cases left. One of these is when the first user doesn't exist: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;user&nbsp;doesn&#39;t&nbsp;exist&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(Positive&nbsp;i)&nbsp;otherUser&nbsp;-&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;otherUser] &nbsp;&nbsp;put&nbsp;db &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uniqueUserId&nbsp;=&nbsp;<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;otherUser&nbsp;+&nbsp;i &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;uniqueUserId&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;otherUser) &nbsp;&nbsp;assertPostFailure&nbsp;db&nbsp;actual</pre> </p> <p> What ought to trigger this test case is that the 'first' user doesn't exist, even if the <code>otherUser</code> does exist. For this reason, the test inserts the <code>otherUser</code> into the Fake database. </p> <p> Since the test is a QuickCheck property, <code>i</code> could be <em>any</em> positive <code>Integer</code> value - <em>including</em> the <code>userId</code> of <code>otherUser</code>. In order to properly exercise the test case, however, you'll need to call the <code>post</code> function with a <code>uniqueUserId</code> - thas it: an ID which is guaranteed to not be equal to the <code>userId</code> of <code>otherUser</code>. There's several options for achieving this guarantee (including, as you'll see soon, the <code>==&gt;</code> operator), but a simple way is to add a non-zero number to the number you need to avoid. </p> <p> You then exercise the <code>post</code> function and, as a verification, call a reusable <code>assertPostFailure</code> function: </p> <p> <pre><span style="color:#2b91af;">assertPostFailure</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;s,&nbsp;<span style="color:blue;">Monad</span>&nbsp;m)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">HttpResponse</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">StateT</span>&nbsp;s&nbsp;m&nbsp;<span style="color:#2b91af;">Bool</span> assertPostFailure&nbsp;stateBefore&nbsp;resp&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;stateAfter&nbsp;&lt;-&nbsp;get &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stateDidNotChange&nbsp;=&nbsp;stateBefore&nbsp;==&nbsp;stateAfter &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;stateDidNotChange&nbsp;&amp;&amp;&nbsp;isBadRequest&nbsp;resp</pre> </p> <p> This function verifies that the state of the database didn't change, and that the response value represents a <code>400 Bad Request</code> HTTP response. This verification doesn't actually verify that the error message associated with the <code>BadRequest</code> case is the expected message, like in the previous article. This would, however, involve a fairly trivial change to the code. </p> <h3 id="dbd934d3e4f7431d95d78dfb0c1d7f4e"> Missing other user test case <a href="#dbd934d3e4f7431d95d78dfb0c1d7f4e" title="permalink">#</a> </h3> <p> Similar to the above test case, users will also fail to connect if the 'other user' doesn't exist. The property is almost identical: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;other&nbsp;user&nbsp;doesn&#39;t&nbsp;exist&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(Positive&nbsp;i)&nbsp;user&nbsp;-&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;user] &nbsp;&nbsp;put&nbsp;db &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uniqueOtherUserId&nbsp;=&nbsp;<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;user&nbsp;+&nbsp;i &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;user)&nbsp;uniqueOtherUserId &nbsp;&nbsp;assertPostFailure&nbsp;db&nbsp;actual</pre> </p> <p> Since this test body is so similar to the previous test, I'm not going to give you a detailed walkthrough. I did, however, promise to describe the <code>runStateTest</code> helper function: </p> <p> <pre><span style="color:#2b91af;">runStateTest</span>&nbsp;::&nbsp;<span style="color:blue;">State</span>&nbsp;(<span style="color:blue;">Map</span>&nbsp;k&nbsp;a)&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;b runStateTest&nbsp;=&nbsp;<span style="color:blue;">flip</span>&nbsp;evalState&nbsp;Map.empty</pre> </p> <p> Since this is a one-liner, you could also write all the tests by simply in-lining that little expression, but I thought that it made the tests more readable to give this function an explicit name. </p> <p> It takes any <code>State (Map k a) b</code> and runs it with an empty map. Thus, all <code>State</code>-valued functions, like the tests, must explicitly put data into the state. This is also what the tests do. </p> <p> Notice that all the tests return <code>State</code> values. For example, the <code>assertPostFailure</code> function returns <code>StateT s m Bool</code>, of which <code>State s Bool</code> is an alias. This fits <code>State (Map k a) b</code> when <code>s</code> is <code>Map k a</code>, which again is aliased to <code>DB</code>. Reducing all of this, the tests are simply functions that return <code>Bool</code>. </p> <h3 id="bc67a5ebaf7347ffa4906b209c31d156"> Invalid user ID test cases <a href="#bc67a5ebaf7347ffa4906b209c31d156" title="permalink">#</a> </h3> <p> Finally, you can also cover the two test cases where one of the user IDs is invalid: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;user&nbsp;Id&nbsp;is&nbsp;invalid&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;s&nbsp;otherUser&nbsp;-&gt;&nbsp;isIdInvalid&nbsp;s&nbsp;==&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;otherUser] &nbsp;&nbsp;put&nbsp;db &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;s&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;otherUser) &nbsp;&nbsp;assertPostFailure&nbsp;db&nbsp;actual , testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;other&nbsp;user&nbsp;Id&nbsp;is&nbsp;invalid&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;s&nbsp;user&nbsp;-&gt;&nbsp;isIdInvalid&nbsp;s&nbsp;==&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;user] &nbsp;&nbsp;put&nbsp;db &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;user)&nbsp;s &nbsp;&nbsp;assertPostFailure&nbsp;db&nbsp;actual</pre> </p> <p> Both of these properties take a <code>String</code> value <code>s</code> as input. When QuickCheck generates a <code>String</code>, that could be any <code>String</code> value. Both tests require that the value is an invalid user ID. Specifically, it mustn't be possible to parse the string into an <code>Integer</code>. If you don't constrain QuickCheck, it'll generate various strings, including e.g. <code>"8"</code> and other strings that can be parsed as numbers. </p> <p> In the above <code>"Users don't connect when user doesn't exist"</code> test, you saw how one way to explicitly model constraints on data is to project a seed value in such a way that the constraint always holds. Another way is to use QuickCheck's built-in <code>==&gt;</code> operator to filter out undesired values. In this example, both tests employ the <code>isIdInvalid</code> function: </p> <p> <pre><span style="color:#2b91af;">isIdInvalid</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> isIdInvalid&nbsp;s&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;userInt&nbsp;=&nbsp;readMaybe&nbsp;s&nbsp;::&nbsp;Maybe&nbsp;Integer &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;isNothing&nbsp;userInt</pre> </p> <p> Using <code>isIdInvalid</code> with the <code>==&gt;</code> operator guarantees that <code>s</code> is an invalid ID. </p> <h3 id="5d89ffd490454c0890dedff39c2f0852"> Summary <a href="#5d89ffd490454c0890dedff39c2f0852" title="permalink">#</a> </h3> <p> While state-based testing may, at first, sound incompatible with strictly functional programming, it's not only possible with the State monad, but even, with good language support, easily done. </p> <p> The tests shown in this article aren't concerned with the interactions between the SUT and its dependencies. Instead, they compare the initial state with the state after exercising the SUT. Comparing values, even complex data structures such as maps, tends to be trivial in functional programming. Immutable values typically have built-in structural equality (in Haskell signified by the automatic <code>Eq</code> type class), which makes comparing them trivial. </p> <p> Now that we know that state-based testing is possible even with Haskell's enforced purity, it should be clear that we can repeat the feat in F#. </p> <p> <strong>Next:</strong> <a href="/2019/03/25/an-example-of-state-based-testing-in-f">An example of state based-testing in F#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Code quality isn't software quality https://blog.ploeh.dk/2019/03/04/code-quality-is-not-software-quality 2019-03-04T07:38:00+00:00 Mark Seemann <div id="post"> <p> <em>A trivial observation made explicit.</em> </p> <p> You'd think that it's evident that code quality and software quality are two different things. Yet, I often see or hear arguments about one or the other that indicates to me that some people don't make that distinction. I wonder why; I do. </p> <h3 id="56d219ddf746496c94b28d22def9a182"> Software quality <a href="#56d219ddf746496c94b28d22def9a182" title="permalink">#</a> </h3> <p> There's a school of thought leaders who advocate that, ultimately, we write code to solve problems, or to improve life, for people. I have nothing against that line of reasoning; it's just not one that I pursue much. Why should I use my energy on this message when someone like <a href="https://dannorth.net">Dan North</a> does it so much better than I could? </p> <p> Dan North is far from the only person making the point that our employers, or clients, or end-users don't care about the code; he is, in my opinion, one of the best communicators in that field. It makes sense that, with that perspective on software development, you'd invent something like <a href="https://en.wikipedia.org/wiki/Behavior-driven_development">behaviour-driven development</a>. </p> <p> The evaluation criterion used in this discourse is one of utility. Does the software serve a purpose? Does it do it well? </p> <p> In that light, <em>quality software</em> is software that serves its purpose beyond expectation. It rarely, if ever, crashes. It's easy to use. It's sufficiently responsive. It's pretty. It works both on-line and off-line. Attributes like that are externally observable qualities. </p> <p> You can write quality software in many different languages, using various styles. When you evaluate the externally observable qualities of software, the code is invisible. It's not part of the evaluation. </p> <p> It seems to me that some people try to make an erroneous conclusion from this premise. They'd say that since no employer, client, or end user evaluates the software based on the code that produced it, then no one cares about the code. </p> <h3 id="6bfca38c59a543b485fb2658ec86a615"> Code quality <a href="#6bfca38c59a543b485fb2658ec86a615" title="permalink">#</a> </h3> <p> It's easy to refute that argument. All you have to do is to come up with a counter-example. You just have to find <em>one</em> person who cares about the code. That's easy. </p> <p> <em>You</em> care about the code. </p> <p> Perhaps you react negatively to that assertion. Perhaps you say: <em>"No! I'm not one of those effete aesthetes who <a href="http://www.sandraandwoo.com/2015/12/24/0747-melodys-guide-to-programming-languages">only program in Plankalkül</a>."</em> Fine. Maybe you're not the type who likes to polish the code; maybe you're the practical, down-to-earth type who just likes to get stuff done, so that your employer/client/end-user is happy. </p> <p> Even so, I think that you still care about the code. Have you ever looked with bewilderment at a piece of code and thought: <em>"Who the hell wrote this piece of shit!?"</em> How many <a href="https://www.osnews.com/story/19266/wtfsm/">WTFs/m</a> is your code? </p> <p> I think every programmer cares about their code bases; if not in an active manner, then at least in a passive way. Bad code can seriously impede progress. I've seen more than one organisation effectively go out of business because of bad legacy code. </p> <p> Code quality is when you care about the readability and malleability of the code. It's when you care about the code's ability to <em>sustain</em> the business, not only today, but also in the future. </p> <h3 id="19f49a7c758947d9b9a8b4c52e2f8e8d"> Sustainable code <a href="#19f49a7c758947d9b9a8b4c52e2f8e8d" title="permalink">#</a> </h3> <p> I often get the impression that some people look at code quality and software quality as a (false) dichotomy. </p> <p> <img src="/content/binary/software-vs-code-quality-false-dichotomy.png" alt="Software quality versus code quality as a false dichotomy."> </p> <p> Such arguments often seem to imply that you can't have one without sacrificing the other. You must choose. </p> <p> The reality is, of course, that you can do both. </p> <p> <img src="/content/binary/software-code-quality-venn.png" alt="Software and code quality Venn diagram."> </p> <p> At the intersection between software and code quality the code sustains the business both now, and in the future. </p> <p> Yes, you should write code such that it produces software that provides value here and now, but you should also do your best to enable it to provide value in the future. This is <em>sustainable code</em>. It's code that can sustain the organisation during its lifetime. </p> <h3 id="1b88784a7a8a4ce5b44334a5ef474a85"> No gold-plating <a href="#1b88784a7a8a4ce5b44334a5ef474a85" title="permalink">#</a> </h3> <p> To be clear: this is not a call for <a href="https://en.wikipedia.org/wiki/Gold_plating_(project_management)">gold plating</a> or <a href="http://wiki.c2.com/?SpeculativeGenerality">speculative generality</a>. You probably can't predict the future needs of the stake-holders. </p> <p> Quality code doesn't have to be able to perfectly address all future requirements. In order to be sustainable, though, it should be easy to modify in the future, or perhaps just easy to throw away and rewrite. I think a good start is to write <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">humane code</a>; code that fits in your brain. </p> <p> At least, do your best to avoid writing legacy code. </p> <h3 id="74a27b6da02840919eed541b1c93f92f"> Summary <a href="#74a27b6da02840919eed541b1c93f92f" title="permalink">#</a> </h3> <p> Software quality and code quality can co-exist. You can write quality code that compiles to quality software, but one doesn't imply the other. These are two independent quality dimensions. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An example of interaction-based testing in C# https://blog.ploeh.dk/2019/02/25/an-example-of-interaction-based-testing-in-c 2019-02-25T05:42:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of using Mocks and Stubs for unit testing in C#.</em> </p> <p> This article is an instalment in an article series about how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based testing to state-based testing</a>. In this series, you'll be presented with some alternatives to interaction-based testing with Mocks and Stubs. Before we reach the alternatives, however, we need to establish an example of interaction-based testing, so that you have something against which you can compare those alternatives. In this article, I'll present a simple example, in the form of C# code. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="7cc38b5dd1bc44c6aacb5077ae65c288"> Connect two users <a href="#7cc38b5dd1bc44c6aacb5077ae65c288" title="permalink">#</a> </h3> <p> For the example, I'll use a simplified version of the example that runs through my two <a href="https://cleancoders.com">Clean Coders</a> videos, <a href="https://cleancoders.com/episode/humane-code-real-episode-4/show">Church Visitor</a> and <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a>. </p> <p> The desired functionality is simple: implement a REST API that enables one user to connect to another user. You could imagine some sort of social media platform, or essentially any sort of online service where users might be interested in connecting with, or following, other users. </p> <p> In essence, you could imagine that a user interface makes an HTTP POST request against our REST API: </p> <p> <pre>POST /connections/42 HTTP/1.1 Content-Type: application/json { "otherUserId": 1337 }</pre> </p> <p> Let's further imagine that we implement the desired functionality with a C# method with this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:blue;">string</span>&nbsp;userId,&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId)</pre> </p> <p> We'll return to the implementation later, but I want to point out a few things. </p> <p> First, notice that both <code>userId</code> and <code>otherUserId</code> are <code>string</code> arguments. While the above example encodes both IDs as numbers, essentially, both URLs and JSON are text-based. Following <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>, the method should also accept JSON like <code>{ "otherUserId": "1337" }</code>. That's the reason the <code>Post</code> method takes <code>string</code> arguments instead of <code>int</code> arguments. </p> <p> Second, the return type is <code>IHttpActionResult</code>. Don't worry if you don't know that interface. It's just a way to model HTTP responses, such as <code>200 OK</code> or <code>400 Bad Request</code>. </p> <p> Depending on the input values, and the state of the application, several outcomes are possible: <table> <col> <col> <colgroup span="3"></colgroup> <thead> <tr> <td colspan="2" rowspan="2"></td> <th colspan="3" scope="colgroup">Other user</th> </tr> <tr> <th scope="col">Found</th> <th scope="col">Not found</th> <th scope="col">Invalid</th> </tr> </thead> <tbody> <tr> <th rowspan="3" scope="rowgroup">User</th> <th scope="row">Found</th> <td><em>Other user</em></td> <td><code>"Other user not found."</code></td> <td><code>"Invalid ID for other user."</code></td> </tr> <tr> <th scope="row">Not found</th> <td><code>"User not found."</code></td> <td><code>"User not found."</code></td> <td><code>"User not found."</code></td> </tr> <tr> <th scope="row">Invalid</th> <td><code>"Invalid user ID."</code></td> <td><code>"Invalid user ID."</code></td> <td><code>"Invalid user ID."</code></td> </tr> </tbody> </table> You'll notice that although this is a 3x3 matrix, there's only five distinct outcomes. This is just an implementation decision. If the first user ID is invalid (e.g. if it's a string like <code>"foo"</code> that doesn't represent a number), then it doesn't matter if the other user exists. Likewise, even if the first user ID is well-formed, it might still be the case that no user with that ID exists in the database. </p> <p> The assumption here is that the underlying user database uses integers as row IDs. </p> <p> When both users are found, the other user should be returned in the HTTP response, like this: </p> <p> <pre>HTTP/1.1 200 OK Content-Type: application/json { "id": 1337, "name": "ploeh", "connections": [{ "id": 42, "name": "fnaah" }, { "id": 2112, "name": "ndøh" }] }</pre> </p> <p> The intent is that when the first user (e.g. the one with the <code>42</code> ID) successfully connects to user <em>1337</em>, a user interface can show the full details of the other user, including the other user's connections. </p> <h3 id="2f013c5e86ef4abda25de01c06a90f25"> Happy path test case <a href="#2f013c5e86ef4abda25de01c06a90f25" title="permalink">#</a> </h3> <p> Since there's five distinct outcomes, you ought to write at least five test cases. You could start with the happy-path case, where both user IDs are well-formed and the users exist. </p> <p> All tests in this article use <a href="https://xunit.net">xUnit.net</a> 2.3.1, <a href="https://github.com/moq/moq4">Moq</a> 4.8.1, and <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> 4.1.0. </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersSuccessfullyConnect( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(user.Id.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(user)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(otherUser.Id.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(otherUser)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ok&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkNegotiatedContentResult</span>&lt;<span style="color:#2b91af;">User</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(otherUser,&nbsp;ok.Content); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(user)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(otherUser.Id,&nbsp;user.Connections); }</pre> </p> <p> To be clear, as far as Overspecified Software goes, this isn't a bad test. It only has two Test Doubles, <code>readerTD</code> and <code>repoTD</code>. My current habit is to name any Test Double with the <em>TD</em> suffix (for <em>Test Double</em>), instead of explicitly naming them <code>readerStub</code> and <code>repoMock</code>. The latter would have been more correct, though, since the <code>Mock&lt;IUserReader&gt;</code> object is consistently used as a Stub, whereas the <code>Mock&lt;IUserRepository&gt;</code> object is used only as a Mock. This is as it should be, because it follows the rule that you should use <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Mocks for Commands, Stubs for Queries</a>. </p> <p> <code>IUserRepository.Update</code> is, indeed a Command: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Update(<span style="color:#2b91af;">User</span>&nbsp;user); }</pre> </p> <p> Since the method returns <code>void</code>, unless it doesn't do anything at all, the only thing it can do is to somehow change the state of the system. The test verifies that <code>IUserRepository.Update</code> was invoked with the appropriate input argument. </p> <p> This is fine. </p> <p> I'd like to emphasise that this isn't the biggest problem with this test. A Mock like this verifies that a desired interaction took place. If <code>IUserRepository.Update</code> isn't called in this test case, it would constitute a defect. The software wouldn't have the desired behaviour, so the test ought to fail. </p> <p> The signature of <code>IUserReader.Lookup</code>, on the other hand, implies that it's a Query: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IUserReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IResult</span>&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;&nbsp;Lookup(<span style="color:blue;">string</span>&nbsp;id); }</pre> </p> <p> In C# and most other languages, you can't be sure that implementations of the <code>Lookup</code> method have no side effects. If, however, we assume that the code base in question obeys the <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> principle, then, by elimination, this must be a Query (since it's not a Command, because the return type isn't <code>void</code>). </p> <p> For a detailed walkthrough of the <code>IResult&lt;S, E&gt;</code> interface, see my <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a> video. It's just an <a href="/2018/06/11/church-encoded-either">Either</a> with different terminology, though. <code>Right</code> is equivalent to <code>SuccessResult</code>, and <code>Left</code> corresponds to <code>ErrorResult</code>. </p> <p> The test configures the <code>IUserReader</code> Stub twice. It's necessary to give the Stub some behaviour, but unfortunately you can't just use Moq's <code>It.IsAny&lt;string&gt;()</code> for configuration, because in order to model the test case, the reader should return two different objects for two different inputs. </p> <p> This starts to look like Overspecified Software. </p> <p> Ideally, a Stub should just be present to 'make happy noises' in case the SUT decides to interact with the dependency, but with these two <code>Setup</code> calls, the interaction is overspecified. The test is tightly coupled to how the SUT is implemented. If you change the interaction implemented in the <code>Post</code> method, you could break the test. </p> <p> In any case, what the test does specify is that when you query the <code>UserReader</code>, it returns a <code>Success</code> object for both user lookups, a <code>200 OK</code> result is returned, and the <code>Update</code> method was called with <code>user</code>. </p> <h3 id="e60081720b2e4468883e3b35b9555c47"> Invalid user ID test case <a href="#e60081720b2e4468883e3b35b9555c47" title="permalink">#</a> </h3> <p> If the first user ID is invalid (i.e. not an integer) then the return value should represent <code>400 Bad Request</code> and the message body should indicate as much. This test verifies that this is the case: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersFailToConnectWhenUserIdIsInvalid( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;userId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(userId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(userId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.InvalidId)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(userId,&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">User</span>&gt;()),&nbsp;<span style="color:#2b91af;">Times</span>.Never()); }</pre> </p> <p> This test starts with a Guard Assertion that <code>userId</code> isn't an integer. This is mostly an artefact of using AutoFixture. Had you used specific example values, then this wouldn't have been necessary. On the other hand, had you written the test case as a property-based test, it would have been even more important to <a href="/2016/01/18/make-pre-conditions-explicit-in-property-based-tests">explicitly encode such a constraint</a>. </p> <p> Perhaps a better design would have been to use a domain-specific method to check for the validity of the ID, but there's always room for improvement. </p> <p> This test is more brittle than it looks. It only defines what should happen when <code>IUserReader.Lookup</code> is called with the invalid <code>userId</code>. What happens if <code>IUserReader.Lookup</code> is called with the <code>Id</code> associated with <code>otherUser</code>? </p> <p> This currently doesn't matter, so the test passes. </p> <p> The test relies, however, on an implementation detail. This test implicitly assumes that the implementation short-circuits as soon as it discovers that <code>userId</code> is invalid. What if, however, you'd made some performance measurements, and you'd discovered that in most cases, the software would run faster if you <code>Lookup</code> both users in parallel? </p> <p> Such an innocuous performance optimisation could break the test, because the behaviour of <code>readerTD</code> is unspecified for all other cases than for <code>userId</code>. </p> <h3 id="d691fe8c85fa42b7baa3ad565c3e6f10"> Invalid ID for other user test case <a href="#d691fe8c85fa42b7baa3ad565c3e6f10" title="permalink">#</a> </h3> <p> What happens if the other user ID is invalid? This unit test exercises that test case: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersFailToConnectWhenOtherUserIdIsInvalid( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(otherUserId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(user.Id.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(user)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(otherUserId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.InvalidId)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUserId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">User</span>&gt;()),&nbsp;<span style="color:#2b91af;">Times</span>.Never()); }</pre> </p> <p> Notice how the test configures <code>readerTD</code> twice: once for the <code>Id</code> associated with <code>user</code>, and once for <code>otherUserId</code>. Why does this test look different from the previous test? </p> <p> Why is the first <code>Setup</code> required? Couldn't the <em>arrange</em> phase of the test just look like the following? </p> <p> <pre><span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(otherUserId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); readerTD &nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(otherUserId)) &nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.InvalidId));</pre> </p> <p> If you wrote the test like that, it would resemble the previous test (<code>UsersFailToConnectWhenUserIdIsInvalid</code>). The problem, though, is that if you remove the <code>Setup</code> for the valid user, the test fails. </p> <p> This is another example of how the use of interaction-based testing makes the tests brittle. The tests are tightly coupled to the implementation. </p> <h3 id="1d8f30969e8345f394af8f3e1bda37ae"> Missing users test cases <a href="#1d8f30969e8345f394af8f3e1bda37ae" title="permalink">#</a> </h3> <p> I don't want to belabour the point, so here's the two remaining tests: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersDoNotConnectWhenUserDoesNotExist( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;userId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(userId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.NotFound)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(userId,&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">User</span>&gt;()),&nbsp;<span style="color:#2b91af;">Times</span>.Never()); } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersDoNotConnectWhenOtherUserDoesNotExist( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;otherUserId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(user.Id.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(user)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(otherUserId.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.NotFound)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUserId.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">User</span>&gt;()),&nbsp;<span style="color:#2b91af;">Times</span>.Never()); }</pre> </p> <p> Again, notice the asymmetry of these two tests. The top one passes with only one <code>Setup</code> of <code>readerTD</code>, whereas the bottom test requires two in order to pass. </p> <p> You can add a second <code>Setup</code> to the top test to make the two tests equivalent, but people often forget to take such precautions. The result is Fragile Tests. </p> <h3 id="ece9e2b8532f4e7c9e445e9840789121"> Post implementation <a href="#ece9e2b8532f4e7c9e445e9840789121" title="permalink">#</a> </h3> <p> In the spirit of test-driven development, I've shown you the tests before the implementation. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ApiController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ConnectionsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserReader</span>&nbsp;userReader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;userRepository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserReader&nbsp;=&nbsp;userReader; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserRepository&nbsp;=&nbsp;userRepository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUserReader</span>&nbsp;UserReader&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;UserRepository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:blue;">string</span>&nbsp;userId,&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;userRes&nbsp;=&nbsp;UserReader.Lookup(userId).SelectError( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&gt;&nbsp;error.Accept(<span style="color:#2b91af;">UserLookupError</span>.Switch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onInvalidId:&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onNotFound:&nbsp;&nbsp;<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;otherUserRes&nbsp;=&nbsp;UserReader.Lookup(otherUserId).SelectError( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&gt;&nbsp;error.Accept(<span style="color:#2b91af;">UserLookupError</span>.Switch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onInvalidId:&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onNotFound:&nbsp;&nbsp;<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connect&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;user&nbsp;<span style="color:blue;">in</span>&nbsp;userRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;otherUser&nbsp;<span style="color:blue;">in</span>&nbsp;otherUserRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;Connect(user,&nbsp;otherUser); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;connect.SelectBoth(Ok,&nbsp;BadRequest).Bifold(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">User</span>&nbsp;Connect(<span style="color:#2b91af;">User</span>&nbsp;user,&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.Connect(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserRepository.Update(user); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;otherUser; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is a simplified version of the code shown towards the end of my <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a> video, so I'll refer you there for a detailed explanation. </p> <h3 id="ed29f1f1efa14245a29e33010c4dd4d1"> Summary <a href="#ed29f1f1efa14245a29e33010c4dd4d1" title="permalink">#</a> </h3> <p> The premise of <a href="http://amzn.to/YPdQDf">Refactoring</a> is that in order to be able to refactor, the "precondition is [...] solid tests". In reality, many development organisations have the opposite experience. When programmers attempt to make changes to how their code is organised, tests break. In <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> this problem is called <em>Fragile Tests</em>, and the cause is often <em>Overspecified Software</em>. This means that tests are tightly coupled to implementation details of the System Under Test (SUT). </p> <p> It's easy to inadvertently fall into this trap when you use Mocks and Stubs, even when you follow the rule of using Mocks for Commands and Stubs for Queries. In my experience, it's often the explicit configuration of Stubs that tend to make tests brittle. A Command represents an intentional side effect, and you want to verify that such a side effect takes place. A Query, on the other hand, has no side effect, so a black-box test shouldn't be concerned with any interactions involving Queries. </p> <p> Yet, using an 'isolation framework' such as Moq, <a href="https://fakeiteasy.github.io/">FakeItEasy</a>, <a href="http://nsubstitute.github.io/">NSubstitute</a>, and so on, will pull you towards overspecifying the interactions the SUT has with its Query dependencies. </p> <p> How can we improve? One strategy is to move towards a more functional design, which is <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. In the next article, you'll see how to rewrite both tests and implementation in <a href="https://www.haskell.org">Haskell</a>. </p> <p> <strong>Next:</strong> <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">An example of state-based testing in Haskell</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment"> <div class="comment-author"><a href="https://remibou.github.io/">Rémi Bourgarel</a> <a href="#">#</a></div> <div class="comment-content"> <p> Hi Mark, </p> <p> I think I came to the same conclusion (maybe not the same solution), meaning you can't write solid tests when mocking all the dependencies interaction : all these dependencies interaction are implementation details (even the database system you chose). For writing solid tests I chose to write my tests like this : start all the services I can in test environment (database, queue ...), mock only things I have no choice (external PSP or Google Captcha), issue command (using MediatR) and check the result with a query. You can find some of my work <a href="https://github.com/RemiBou/Toss.Blazor/blob/master/Toss.Tests/Server/Models/Tosses/LastTossQueryHandlerTest.cs"> here </a>. The work is not done on all the tests but this is the way I want to go. Let me know what you think about it. </p> <p> I could have launched the tests at the Controller level but I chose Command and Query handler.</p> <p> Can't wait to see your solution </p> </div> <div class="comment-date">2019-02-25 07:53 UTC</div> </div> <div class="comment" id="7b60769bf7eb4be3969623d4819d5c0e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7b60769bf7eb4be3969623d4819d5c0e">#</a></div> <div class="comment-content"> <p> Rémi, thank you for writing. Hosting services as part of a test run can be a valuable addition to an overall testing or release pipeline. It's reminiscent of the approach taken in <a href="http://bit.ly/growingoos">GOOS</a>. I've also touched on this option in my Pluralsight course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. This is, however, a set of tests I would identify as belonging towards the top of a <a href="https://martinfowler.com/bliki/TestPyramid.html">Test Pyramid</a>. In my experience, such tests tend to run (an order of magnitude) slower than unit tests. </p> <p> That doesn't preclude their use. Depending on circumstances, I still prefer having tests like that. I think that I've written a few applications where tests like that constituted the main body of unit tests. </p> <p> I do, however, also find this style of testing too limiting in many situation. I tend to prefer 'real' unit tests, since they tend to be easier to write, and they execute faster. </p> <p> Apart from performance and maintainability concerns, one problem that I often see with integration tests is that <a href="https://www.infoq.com/presentations/integration-tests-scam">it's practically impossible to cover all edge cases</a>. This tends to lead to either bug-ridden software, or unmaintainable test suites. </p> <p> Still, I think that, ultimately, having enough experience with different styles of testing enables one to make an informed choice. That's my purpose with these articles: to point out that alternatives exist. </p> </div> <div class="comment-date">2019-03-01 9:31 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. From interaction-based to state-based testing https://blog.ploeh.dk/2019/02/18/from-interaction-based-to-state-based-testing 2019-02-18T08:19:00+00:00 Mark Seemann <div id="post"> <p> <em>Indiscriminate use of Mocks and Stubs can lead to brittle test suites. A more functional design can make state-based testing easier, leading to more robust test suites.</em> </p> <p> The original premise of <a href="http://amzn.to/YPdQDf">Refactoring</a> was that in order to refactor, you must have a trustworthy suite of unit tests, so that you can be confident that you didn't break any functionality. <blockquote> <p>"to refactor, the essential precondition is [...] solid tests"</p> <footer><cite>Martin Fowler, <a href="http://amzn.to/YPdQDf">Refactoring</a></cite></footer> </blockquote> The idea is that you can change how the code is organised, and as long as you don't break any tests, all is good. The experience that most people seem to have, though, is that when they change something in the code, tests break. </p> <p> This is a well-known test smell. In <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> this is called <em>Fragile Test</em>, and it's often caused by <em>Overspecified Software</em>. Even if you follow the proper practice of using <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Mocks for Commands, Stubs for Queries</a>, you can still end up with a code base where the tests are highly coupled to implementation details of the software. </p> <p> The cause is often that when relying on Mocks and Stubs, test verification hinges on how the System Under Test (SUT) interacts with its dependencies. For that reason, we can call such tests <em>interaction-based tests</em>. For more information, watch my Pluralsight course <a href="https://blog.ploeh.dk/advanced-unit-testing">Advanced Unit Testing</a>. </p> <h3 id="fb4f2eb1191943c09450c7281a6c8cb0"> Lessons from functional programming <a href="#fb4f2eb1191943c09450c7281a6c8cb0" title="permalink">#</a> </h3> <p> Another way to verify the outcome of a test is to inspect the state of the system after exercising the SUT. We can, quite naturally, call this <em>state-based testing</em>. In object-oriented design, this can lead to other problems. <a href="http://natpryce.com">Nat Pryce</a> has pointed out that <a href="http://natpryce.com/articles/000342.html">state-based testing breaks encapsulation</a>. </p> <p> Interestingly, in his article, Nat Pryce concludes: <blockquote> "I have come to think of object oriented programming as an inversion of functional programming. In a lazy functional language data is pulled through functions that transform the data and combine it into a single result. In an object oriented program, data is pushed out in messages to objects that transform the data and push it out to other objects for further processing." </blockquote> That's an impressively perceptive observation to make in 2004. I wish I was that perspicacious, but I only <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">reached a similar conclusion ten years later</a>. </p> <p> Functional programming is based on the fundamental principle of <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>, which, among other things, means that data must be immutable. Thus, no objects change state. Instead, functions can return data that contains immutable state. In unit tests, you can verify that return values are as expected. <a href="/2015/05/07/functional-design-is-intrinsically-testable">Functional design is intrinsically testable</a>; we can consider it a kind of state-based testing, although the states you'd be verifying are immutable return values. </p> <p> In this article series, you'll see three different styles of testing, from interaction-based testing with Mocks and Stubs in C#, over strictly functional state-based testing in <a href="https://www.haskell.org">Haskell</a>, to pragmatic state-based testing in <a href="https://fsharp.org">F#</a>, finally looping back to C# to apply the lessons from functional programming. <ul> <li><a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">An example of interaction-based testing in C#</a></li> <li><a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">An example of state-based testing in Haskell</a></li> <li><a href="/2019/03/25/an-example-of-state-based-testing-in-f">An example of state based-testing in F#</a></li> <li><a href="/2019/04/01/an-example-of-state-based-testing-in-c">An example of state-based testing in C#</a></li> <li><a href="/2019/04/08/a-pure-test-spy">A pure Test Spy</a></li> </ul> The code for all of these articles is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="d370a0ae3bc34440b68f8fddab6c1b25"> Summary <a href="#d370a0ae3bc34440b68f8fddab6c1b25" title="permalink">#</a> </h3> <p> Adopting a more functional design, even in a fundamentally object-oriented language like C# can, in my experience, lead to a more sustainable code base. Various maintenance tasks become easier, including unit tests. Functional programming, however, is no panacea. My intent with this article series is only to inspire; to show alternatives to the ways things are normally done. Adopting one of those alternatives could lead to better code, but you must still exercise context-specific judgement. </p> <p> <strong>Next:</strong> <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">An example of interaction-based testing in C#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Asynchronous Injection https://blog.ploeh.dk/2019/02/11/asynchronous-injection 2019-02-11T07:43:00+00:00 Mark Seemann <div id="post"> <p> <em>How to combine asynchronous programming with Dependency Injection without leaky abstractions.</em> </p> <p> C# has decent support for asynchronous programming, but it ultimately leads to leaky abstractions. This is often conspicuous when combined with Dependency Injection (DI). This leads to frequently asked questions around the combination of DI and asynchronous programming. This article outlines the problem and suggests an alternative. </p> <p> The code base supporting this article is <a href="https://github.com/ploeh/asynchronous-injection">available on GitHub</a>. </p> <h3 id="0463aa2fd41b46bbbb837709ed9bc58b"> A synchronous example <a href="#0463aa2fd41b46bbbb837709ed9bc58b" title="permalink">#</a> </h3> <p> In this article, you'll see various stages of a small sample code base that pretends to implement the server-side behaviour of an on-line restaurant reservation system (my favourite example scenario). In the first stage, the code uses DI, but no asynchronous I/O. </p> <p> At the boundary of the application, a <code>Post</code> method receives a <code>Reservation</code> object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ControllerBase</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController(<span style="color:#2b91af;">IMaîtreD</span>&nbsp;maîtreD) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MaîtreD&nbsp;=&nbsp;maîtreD; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span>&nbsp;MaîtreD&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IActionResult</span>&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>?&nbsp;id&nbsp;=&nbsp;MaîtreD.TryAccept(reservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok(id.Value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Reservation</code> object is just a simple bundle of properties: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Reservation</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Date&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsAccepted&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> In a production code base, I'd favour a separation of <a href="https://en.wikipedia.org/wiki/Data_transfer_object">DTOs</a> and domain objects with proper encapsulation, but in order to keep the code example simple, here the two roles are combined. </p> <p> The <code>Post</code> method simply delegates most work to an injected <code>IMaîtreD</code> object, and translates the return value to an HTTP response. </p> <p> The code example is overly simplistic, to the point where you may wonder what is the point of DI, since it seems that the <code>Post</code> method doesn't perform any work itself. A slightly <a href="/2017/01/27/dependency-injection-is-passing-an-argument">more realistic example includes some input validation and mapping between layers</a>. </p> <p> The <code>IMaîtreD</code> implementation is this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaîtreD(<span style="color:blue;">int</span>&nbsp;capacity,&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>?&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Repository.Create(reservation); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The protocol for the <code>TryAccept</code> method is that it returns the reservation ID if it accepts the reservation. If the restaurant has too little remaining <code>Capacity</code> for the requested date, it instead returns <code>null</code>. Regular readers of this blog will know that I'm <a href="/2015/11/13/null-has-no-type-but-maybe-has">no fan of null</a>, but this keeps the example realistic. I'm also no fan of state mutation, but the example does that as well, by setting <code>IsAccepted</code> to <code>true</code>. </p> <h3 id="fa919e35f82a4387822c9888dd5d7537"> Introducing asynchrony <a href="#fa919e35f82a4387822c9888dd5d7537" title="permalink">#</a> </h3> <p> The above example is entirely synchronous, but perhaps you wish to introduce some asynchrony. For example, the <code>IReservationsRepository</code> implies synchrony: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;ReadReservations(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;Create(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> In reality, though, you know that the implementation of this interface queries and writes to a relational database. Perhaps making this communication asynchronous could improve application performance. It's worth a try, at least. </p> <p> How do you make something asynchronous in C#? You change the return type of the methods in question. Therefore, you have to change the <code>IReservationsRepository</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">Reservation</span>[]&gt;&nbsp;ReadReservations(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Create(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> The Repository methods now return Tasks. This is the first leaky abstraction. From the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> it follows that <blockquote> <p>"clients [...] own the abstract interfaces"</p> <footer><cite>Robert C. Martin, <a href="http://amzn.to/19W4JHk">APPP</a>, chapter 11</cite></footer> </blockquote> The <code>MaîtreD</code> class is the client of the <code>IReservationsRepository</code> interface, which should be designed to support the needs of that class. <code>MaîtreD</code> doesn't need <code>IReservationsRepository</code> to be asynchronous. </p> <p> The change of the interface has nothing to with what <code>MaîtreD</code> needs, but rather with a particular implementation of the <code>IReservationsRepository</code> interface. Because this implementation queries and writes to a relational database, this implementation detail leaks into the interface definition. It is, therefore, a leaky abstraction. </p> <p> On a more practical level, accommodating the change is easily done. Just add <code>async</code> and <code>await</code> keywords in appropriate places: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(reservation); }</pre> </p> <p> In order to compile, however, you also have to fix the <code>IMaîtreD</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> This is the second leaky abstraction, and it's worse than the first. Perhaps you could successfully argue that it was conceptually acceptable to model <code>IReservationsRepository</code> as asynchronous. After all, a Repository conceptually represents a data store, and these are generally out-of-process resources that require I/O. </p> <p> The <code>IMaîtreD</code> interface, on the other hand, is a domain object. It models how business is done, not how data should be accessed. Why should business logic be asynchronous? </p> <p> It's hardly news that <a href="http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/">async and await is infectious</a>. Once you introduce Tasks, it's <em>async all the way!</em> </p> <p> That doesn't mean that asynchrony isn't one big leaky abstraction. It is. </p> <p> You've probably already realised what this means in the context of the little example. You must also patch the <code>Post</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>?&nbsp;id&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;MaîtreD.TryAccept(reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok(id.Value); }</pre> </p> <p> Pragmatically, I'd be ready to accept the argument that this isn't a big deal. After all, you just replace all return values with Tasks, and add <code>async</code> and <code>await</code> keywords where they need to go. This hardly impacts the maintainability of a code base. </p> <p> In C#, I'd be inclined to just acknowledge that, <em>hey, there's a leaky abstraction. Moving on...</em> </p> <p> On the other hand, sometimes people imply that it has to be like this. That there is no other way. </p> <p> <a href="https://en.wikipedia.org/wiki/Falsifiability">Falsifiable claims</a> like that often get my attention. <em>Oh, really?!</em> </p> <h3 id="46df3267f63a4c908ed710c2b4f9d3f9"> Move impure interactions to the boundary of the system <a href="#46df3267f63a4c908ed710c2b4f9d3f9" title="permalink">#</a> </h3> <p> We can <a href="/2018/09/24/asynchronous-functors">pretend that <code>Task&lt;T&gt;</code> forms a functor</a>. It's <a href="/2022/06/06/asynchronous-monads">also a monad</a>. <a href="/2022/03/28/monads">Monads</a> are those incredibly useful programming abstractions that have been propagating from their origin in statically typed functional programming languages to more mainstream languages like C#. </p> <p> In functional programming, <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">impure interactions happen at the boundary of the system</a>. Taking inspiration from functional programming, you can <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">move the impure interactions to the boundary of the system</a>. </p> <p> In the interest of keeping the example simple, I'll only move the impure operations one level out: from <code>MaîtreD</code> to <code>ReservationsController</code>. The approach can be generalised, although you may have to look into how to handle <a href="/2017/07/10/pure-interactions">pure interactions</a>. </p> <p> Where are the impure interactions in <code>MaîtreD</code>? They are in the two interactions with <code>IReservationsRepository</code>. The <code>ReadReservations</code> method is non-deterministic, because the same input value can return different results, depending on the state of the database when you call it. The <code>Create</code> method causes a side effect to happen, because it creates a row in the database. This is one way in which the state of the database could change, which makes <code>ReadReservations</code> non-deterministic. Additionally, <code>Create</code> also violates <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> (CQS) by returning the ID of the row it creates. This, again, is non-deterministic, because the same input value will produce a new return value every time the method is called. (Incidentally, you should <a href="/2014/08/11/cqs-versus-server-generated-ids">design <code>Create</code> methods so that they don't violate CQS</a>.) </p> <h3 id="dea3b9778dd54d6c9b18e9b3e9c95153"> Move reservations to a method argument <a href="#dea3b9778dd54d6c9b18e9b3e9c95153" title="permalink">#</a> </h3> <p> The first refactoring is the easiest. Move the <code>ReadReservations</code> method call to the application boundary. In the above state of the code, the <code>TryAccept</code> method unconditionally calls <code>Repository.ReadReservations</code> to populate the <code>reservations</code> variable. Instead of doing this from within <code>TryAccept</code>, just pass <code>reservations</code> as a method argument: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(reservation); }</pre> </p> <p> This no longer compiles until you also change the <code>IMaîtreD</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> You probably think that this is a much worse leaky abstraction than returning a Task. I'd be inclined to agree, but trust me: ultimately, this will matter not at all. </p> <p> When you move an impure operation outwards, it means that when you remove it from one place, you must add it to another. In this case, you'll have to query the Repository from the <code>ReservationsController</code>, which also means that you need to add the Repository as a dependency there: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ControllerBase</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMaîtreD</span>&nbsp;maîtreD, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MaîtreD&nbsp;=&nbsp;maîtreD; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span>&nbsp;MaîtreD&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>?&nbsp;id&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;MaîtreD.TryAccept(reservations,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok(id.Value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is a refactoring in the true sense of the word. It just reorganises the code without changing the overall behaviour of the system. Now the <code>Post</code> method has to query the Repository before it can delegate the business decision to <code>MaîtreD</code>. </p> <h3 id="6a492b66a0d14edda105eb3fb3b4ebb4"> Separate decision from effect <a href="#6a492b66a0d14edda105eb3fb3b4ebb4" title="permalink">#</a> </h3> <p> As far as I can tell, the main reason to use DI is because some impure interactions are conditional. This is also the case for the <code>TryAccept</code> method. Only if there's sufficient remaining capacity does it call <code>Repository.Create</code>. If it detects that there's too little remaining capacity, it immediately returns <code>null</code> and doesn't call <code>Repository.Create</code>. </p> <p> In object-oriented code, DI is the most common way to decouple decisions from effects. Imperative code reaches a decision and calls a method on an object based on that decision. The effect of calling the method can vary because of polymorphism. </p> <p> In functional programming, you typically use a <a href="/2018/03/22/functors">functor</a> like <a href="/2018/03/26/the-maybe-functor">Maybe</a> or <a href="/2018/06/11/church-encoded-either">Either</a> to <a href="/2016/09/26/decoupling-decisions-from-effects">separate decisions from effects</a>. You can do the same here. </p> <p> The protocol of the <code>TryAccept</code> method already communicates the decision reached by the method. An <code>int</code> value is the reservation ID; this implies that the reservation was accepted. On the other hand, <code>null</code> indicates that the reservation was declined. </p> <p> You can use the same sort of protocol, but instead of returning a <code>Nullable&lt;int&gt;</code>, you can return a <code>Maybe&lt;Reservation&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&nbsp;TryAccept( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Maybe</span>.Empty&lt;<span style="color:#2b91af;">Reservation</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reservation.ToMaybe(); }</pre> </p> <p> This completely decouples the decision from the effect. By returning <code>Maybe&lt;Reservation&gt;</code>, the <code>TryAccept</code> method communicates the decision it made, while leaving further processing entirely up to the caller. </p> <p> In this case, the caller is the <code>Post</code> method, which can now compose the result of invoking <code>TryAccept</code> with <code>Repository.Create</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;m&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;MaîtreD.TryAccept(reservations,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:blue;">async</span>&nbsp;r&nbsp;=&gt;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(r)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;<span style="color:blue;">async</span>&nbsp;id&nbsp;=&gt;&nbsp;Ok(<span style="color:blue;">await</span>&nbsp;id)); }</pre> </p> <p> Notice that the <code>Post</code> method never attempts to <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">extract 'the value' from <code>m</code></a>. Instead, it injects the desired behaviour (<code>Repository.Create</code>) into the monad. The result of calling <code>Select</code> with an asynchronous lambda expression like that is a <code>Maybe&lt;Task&lt;int&gt;&gt;</code>, which is a awkward combination. You can fix that later. </p> <p> The <code>Match</code> method is the <a href="/2019/05/20/maybe-catamorphism">catamorphism for Maybe</a>. It looks exactly like the <code>Match</code> method on the <a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a>. It handles both the case when <code>m</code> is empty, and the case when <code>m</code> is populated. In both cases, it returns a <code>Task&lt;IActionResult&gt;</code>. </p> <h3 id="cfd87db1b9304255bda17d1da93c32ee"> Synchronous domain logic <a href="#cfd87db1b9304255bda17d1da93c32ee" title="permalink">#</a> </h3> <p> At this point, you have a compiler warning in your code: <blockquote> Warning CS1998 This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. </blockquote> Indeed, the current incarnation of <code>TryAccept</code> is synchronous, so remove the <code>async</code> keyword and change the return type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;TryAccept( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Maybe</span>.Empty&lt;<span style="color:#2b91af;">Reservation</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reservation.ToMaybe(); }</pre> </p> <p> This requires a minimal change to the <code>Post</code> method: it no longer has to <code>await</code> <code>TryAccept</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;m&nbsp;=&nbsp;MaîtreD.TryAccept(reservations,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:blue;">async</span>&nbsp;r&nbsp;=&gt;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(r)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;<span style="color:blue;">async</span>&nbsp;id&nbsp;=&gt;&nbsp;Ok(<span style="color:blue;">await</span>&nbsp;id)); }</pre> </p> <p> Apart from that, this version of <code>Post</code> is the same as the one above. </p> <p> Notice that at this point, the domain logic (<code>TryAccept</code>) is no longer asynchronous. The leaky abstraction is gone. </p> <h3 id="da9febf381b1483883423b96c5ceb9a3"> Redundant abstraction <a href="#da9febf381b1483883423b96c5ceb9a3" title="permalink">#</a> </h3> <p> The overall work is done, but there's some tidying up remaining. If you review the <code>TryAccept</code> method, you'll notice that it no longer uses the injected <code>Repository</code>. You might as well simplify the class by removing the dependency: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaîtreD(<span style="color:blue;">int</span>&nbsp;capacity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;TryAccept( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Maybe</span>.Empty&lt;<span style="color:#2b91af;">Reservation</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reservation.ToMaybe(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>TryAccept</code> method is now deterministic. The same input will always return the same input. This is not yet a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, because it still has a single side effect: it mutates the state of <code>reservation</code> by setting <code>IsAccepted</code> to <code>true</code>. You could, however, without too much trouble refactor <code>Reservation</code> to an immutable <a href="https://en.wikipedia.org/wiki/Value_object">Value Object</a>. </p> <p> This would enable you to write the last part of the <code>TryAccept</code> method like this: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;reservation.Accept().ToMaybe();</pre> </p> <p> In any case, the method is close enough to be <a href="/2015/05/07/functional-design-is-intrinsically-testable">pure that it's testable</a>. The interactions of <code>TryAccept</code> and any client code (including unit tests) is completely controllable and observable by the client. </p> <p> This means that there's no reason to <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Stub it out</a>. You might as well just use the function directly in the <code>Post</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ControllerBase</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;capacity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;m&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(Capacity).TryAccept(reservations,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:blue;">async</span>&nbsp;r&nbsp;=&gt;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(r)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;<span style="color:blue;">async</span>&nbsp;id&nbsp;=&gt;&nbsp;Ok(<span style="color:blue;">await</span>&nbsp;id)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that <code>ReservationsController</code> no longer has an <code>IMaîtreD</code> dependency. </p> <p> All this time, whenever you make a change to the <code>TryAccept</code> method signature, you'd also have to fix the <code>IMaîtreD</code> interface to make the code compile. If you worried that all of these changes were leaky abstractions, you'll be happy to learn that <a href="https://amzn.to/2PzDpJu">in the end, it doesn't even matter</a>. No code uses that interface, so you can delete it. </p> <h3 id="843e56ff0b74406eb236bebd2fad4828"> Grooming <a href="#843e56ff0b74406eb236bebd2fad4828" title="permalink">#</a> </h3> <p> The <code>MaîtreD</code> class looks fine, but the <code>Post</code> method could use some grooming. I'm not going to tire you with all the small refactoring steps. You can follow them in the GitHub repository if you're interested. Eventually, you could arrive at an implementation like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ControllerBase</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;capacity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maîtreD&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(capacity); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;maîtreD; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(rs&nbsp;=&gt;&nbsp;maîtreD.TryAccept(rs,&nbsp;reservation)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(m&nbsp;=&gt;&nbsp;m.Traverse(Repository.Create)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>),&nbsp;Ok); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Now the <code>Post</code> method is just a single, composed asynchronous pipeline. Is it a coincidence that this is possible? </p> <p> This is no coincidence. This top-level method executes in the 'Task monad', and a monad is, by definition, composable. You can chain operations together, and they don't all have to be asynchronous. Specifically, <code>maîtreD.TryAccept</code> is a synchronous piece of business logic. It's unaware that it's being injected into an asynchronous context. This type of design would be completely run of the mill in <a href="https://fsharp.org">F#</a> with its <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/async-expressions">asynchronous workflows</a>. </p> <h3 id="ff3eb0a846d24e1b82291004b5acf126"> Summary <a href="#ff3eb0a846d24e1b82291004b5acf126" title="permalink">#</a> </h3> <p> Dependency Injection frequently involves I/O-bound operations. Those typically get hidden behind interfaces so that they can be mocked or stubbed. You may want to access those I/O-bound resources asynchronously, but with C#'s support for asynchronous programming, you'll have to make your abstractions asynchronous. </p> <p> When you make the leaf nodes in your call graph asynchronous, that design change ripples through the entire code base, forcing you to be <em>async all the way</em>. One result of this is that the domain model must also accommodate asynchrony, although this is rarely required by the logic it implements. These concessions to asynchrony are leaky abstractions. </p> <p> Pragmatically, it's hardly a big problem. You can use the <code>async</code> and <code>await</code> keywords to deal with the asynchrony, and it's unlikely to, in itself, cause a problem with maintenance. </p> <p> In functional programming, monads can address asynchrony without introducing sweeping leaky abstractions. Instead of making DI asynchronous, you can inject desired behaviour into an asynchronous context. </p> <p> Behaviour Injection, not Dependency Injection. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="6d45d75e1bfd495a8cd688768f960c06"> <div class="comment-author">Ramon Pfeiffer <a href="#6d45d75e1bfd495a8cd688768f960c06">#</a></div> <div class="comment-content"> <p> Hi Mark, </p> <p> aren't you loading more responsibilities on the <code>ReservationsController</code>? Previously, it only had to delegate all the work to <code>MaîtreD</code> and return an appropriate result, now it additionally fetches reservations from the repository. You are also loading the handling of any errors the reservations repository might throw onto the controller, instead of handling them in the <code>MaîtreD</code> class. </p> <p> You are also hard wiring a dependency on <code>MaîtreD</code> into the <code>ReservationsController</code>; I thought one of the advantages of DI were to avoid newing up dependencies to concrete implementations outside of a centralized "builder class". </p> <p> Could you elaborate on these points? Thanks! </p> </div> <div class="comment-date">2019-02-11 10:39 UTC</div> </div> <div class="comment" id="0961c2d19e0f4990941ba597ffc0514f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0961c2d19e0f4990941ba597ffc0514f">#</a></div> <div class="comment-content"> <p> Ramon, thank you for writing. Am I loading more responsibilities on the Controller? Yes, I am. Too many? I don't think so. </p> <p> To be fair, however, this example is unrealistically simplified (in order to make it easily understandable). There isn't much going on, overall, so one has to imagine that more things are happening than is actually the case. For instance, at the beginning of the example, so little is going on in the Controller that I think it'd be fair to ask why it's even necessary to distinguish between a Controller and a <code>MaîtreD</code> class. </p> <p> Usually, I'd say that the responsibility of a Controller object is to facilitate the translation of what goes on at the boundary of the application and what happens in the domain model. Using the terminology of the <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">ports and adapters</a> architecture, you could say that a Controller's responsibility is to serve as an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> between the technology-agnostic domain model and the technology-specific SDKs you'll need to bring into play to communicate with the 'real world'. Talking to databases fits that responsibility, I think. </p> <p> The <code>MaîtreD</code> class didn't handle any database errors before, so I don't agree that I've moved that responsibility. </p> <p> When it comes to using a <code>MaîtreD</code> object from inside the Controller, I don't agree that I've 'hard-wired' it. It's not a dependency in the Dependency Injection sense; it's an implementation detail. Notice that it's a <code>private</code> class field. </p> <p> Is it an 'advantage of DI' that you can <em>"avoid newing up dependencies to concrete implementations outside of a centralized "builder class"?"</em> How is that an advantage? Is that a goal? </p> <p> In future articles, I'll discuss this sort of 'dependency elimination' in more details. </p> </div> <div class="comment-date">2019-02-11 15:29 UTC</div> </div> <div class="comment" id="b1876a214ea54221935748c21e148d2a"> <div class="comment-author">Ramon Pfeiffer <a href="#b1876a214ea54221935748c21e148d2a">#</a></div> <div class="comment-content"> <p> Mark, thanks for replying. </p> <p> I assumed that some exception handling would be happening in the <code>MaitreD</code> class that would then migrate to the <code>ReservationsController</code> and you left it out for the sake of simplicity. But granted, that can still happen inside the respository class. </p> <p> Let's imagine that for some reason, you want to write to the filesystem in addition to the database (eg. writing some reservation data like table number that can be printed and given to the customer). Following your reasoning, there would now be a reference to some <code>IReservationPrinter</code> in the Controller. It suddenly has to hold references to all data exchange classes that it was previously unaware of, only caring about the result <code>MaîtreD</code> was returning. </p> <p> Maybe I didn't express myself properly: I thought Dependency Injection is a technique to resolve all implementation types at a single composition root. Of course this only applies to dependencies in the sense of DI, so where do you draw the line between implementation detail and dependency? </p> <p> In any case I'm looking forward to reading more articles on this topic! </p> </div> <div class="comment-date">2019-02-11 18:55 UTC</div> </div> <div class="comment" id="0ca54917c833417c816bf086d6324af5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0ca54917c833417c816bf086d6324af5">#</a></div> <div class="comment-content"> <p> Ramon, in general when it comes to exception handling, you either handle exceptions at the source (i.e. in the Repository) or at the boundary of the application (which is typically done by frameworks already). <a href="/2013/07/08/defensive-coding">I'm no fan of defensive coding</a>. <blockquote> "It suddenly has to hold references to all data exchange classes that it was previously unaware of" </blockquote> Yes, but now <code>MaîtreD</code> doesn't have to do that. Is there anything inherently associated with business logic that stipulates that it handles data access? </p> <p> The following line of argument may be increasingly difficult to relate to as time moves forward, and business becomes increasingly digital, but there once was a time when business logic was paper-based. In paper-based organisations, data would flow through a business in the shape of paper; typically as forms. Data would arrive at the desk of a clerk or domain expert who would add more data or annotations to a form, and put it in his or her out-box for later collection. </p> <p> My point is that I see nothing inherent in business logic to stipulate that business objects should be responsible for data retrieval or persistence. I recommend <a href="https://amzn.to/2OyI51M">Domain Modeling Made Functional</a> if you're interested in a comprehensive treatment of this way of looking at modelling business logic. <blockquote> "I thought Dependency Injection is a technique to resolve all implementation types at a single composition root." </blockquote> It is, and that still happens here. There are, however, fewer dependencies overall. I would argue that with the final design outlined here, the remaining dependency (<code>IReservationsRepository</code>) is also, architecturally, the only real dependency of the application. The initial <code>IMaîtreD</code> dependency is, in my opinion, an implementation detail. Exposing it as a dependency makes the code more brittle, and harder to refactor, but that's what I'm going to cover in future articles. </p> </div> <div class="comment-date">2019-02-12 9:24 UTC</div> </div> <div class="comment" id="2f8b9958df594883994859909d1bd5d3"> <div class="comment-author">Ramon Pfeiffer <a href="#2f8b9958df594883994859909d1bd5d3">#</a></div> <div class="comment-content"> <p> Mark, I have to admit that I'm still not convinced (without having read the book you mentioned): </p> <p> Expanding on your analogy, a clerk would maybe make a phone call or walk over to another desk if he needs more information regarding his current form (I know I do at my office). A maître d'hôtel would presumably open his book of reservations to check if he still has a table available and would write a new reservation in his book. </p> <p> The <code>MaîtreD</code> doesn't need to know if the data it needs comes from the file system or a database or a web service (that's the responsibility of the repository class), all it cares about is that it needs some data. Currently, some other part of the system decides what data <code>MaîtreD</code> has to work with. </p> <p> Again, I didn't have a look at the reading recommendation yet. Maybe I should. ;) </p> </div> <div class="comment-date">2019-02-12 10:50 UTC</div> </div> <div class="comment" id="82fd379429464775b97f52b48c0a88c1"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#82fd379429464775b97f52b48c0a88c1">#</a></div> <div class="comment-content"> <p> I definitely agree with Mark that the business logic (in the final version of <code>MaîtreD.TryAccept</code>) should be in a function that is pure and synchronous. However, I am also sympathetic to Ramon's argument. </p> <p> There are two UIs for the application that I am currently building at work. The primary interface is over HTTP and uses web controllers just like in Mark's example. The second interface is a CLI (that is only accessable to administrators with phsyical access to the server). Suppose my application was also an on-line restaurant reservation system and that a reservation could be made with both UIs. </p> <p> Looking back at the final implementation of <code>ReservationsController.Post</code>, the first three lines are independent of <code>ControllerBase</code> and would also need to be executed when accessing the system though the CLI. My understanding is that Ramon's primary suggestion is to move these three lines into <code>MaîtreD.TryAccept</code>. I am sympathetic to Ramon's argument in that I am in favor of extracting those three lines. However, I don't want them to be colocated with the final implimentatiion of <code>MaîtreD.TryAccept</code>. </p> <p> In my mind, the single responsibility of <code>ReservationsController.Post</code> is to translate the result of the reseravation request into the expected type of response. That would be just the fourth line in the final implementation of this method. In terms of naming, I like Ramon's suggestion that the first three lines of <code>ReservationsController.Post</code> be moved to <code>MaîtreD.TryAccept</code>. But then I also want to move the final implementation of <code>MaîtreD.TryAccept</code> to a method on a different type. As we all know, naming is an impossible problem, so I don't have a good name for this new third type. </p> <p> What do you think Ramon? Have I understood your concerns and suggested something that you could get behind? </p> <p> What about you Mark? You said that there was <blockquote> so little...going on in the Controller that I think it'd be fair to ask why it's even necessary to distinguish between a Controller and a <code>MaîtreD</code> class. </blockquote> Would two UIs be sufficient motivation in your eyes to justify distinguishing between a Controller and a <code>MaîtreD</code> class? </p> </div> <div class="comment-date">2019-02-12 17:00 UTC</div> </div> <div class="comment" id="0ecaa41899db43c6ae6eced05d7cf6e6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0ecaa41899db43c6ae6eced05d7cf6e6">#</a></div> <div class="comment-content"> <p> Tyson, thank you for joining the discussion. By adding a particular problem (more than one user interface) to be addressed, you make the discussion more specific. I think this helps to clarify some issues. </p> <p> Ramon wrote: <blockquote> "I have to admit that I'm still not convinced" </blockquote> That's okay; you don't have to be. I rarely write articles with the explicit intent of telling people that they <em>must</em> do something, or that they should <em>never</em> do something else. While it does happen, this article isn't such an article. If it helps you address a problem, then take what you find useful. If it doesn't, then ignore it. </p> <p> With Tyson's help, though, we can now discuss something more concrete. I think some of those observations identify a tender spot in my line of argument. In the initial version of <code>ReservationsController</code>, the only responsibility of the <code>Post</code> method was to translate from and to HTTP. That's a distinct separation of responsibility, so clearly preferable. </p> <p> When I add the <code>Repository</code> dependency, I widen the scope of the <code>ReservationsController</code>'s responsibility, which now includes 'all IO'. This does blur the demarcation of responsibility, but often still works out well in practice, I find. Still, it depends on how much other stuff is going on related to IO. If you have too much IO going on, another separation of responsibilities is in order. </p> <p> I do find, however, that when implementing the same sort of software capability in different user interfaces, I need to specifically design for each user interface paradigm. A web-based user interface is quite different from a command-line interface, which is again different from a native application, or a voice-based interface, and so on. A web-based interface is, for example, stateless, whereas a native smart phone application would often be stateful. You can rarely reuse the 'user interface controller layer' for one type of application in a different type of application. </p> <p> Even a command-line interface could be stateful by <a href="/2017/07/17/a-pure-command-line-wizard">interactively asking a series of questions</a>. That's such a different user interface paradigm that an object designed for one type of interaction is rarely reusable in another context. </p> <p> What I do find is that fine-grained building blocks still compose. When <code>TryAccept</code> is a pure function, it's <em>always</em> composable. This means that my chance of being able to reuse it becomes much higher than if it's an object injected with various dependencies. <blockquote> "a clerk would maybe make a phone call or walk over to another desk if he needs more information regarding his current form" </blockquote> Indeed, but how do you model this in software? A program doesn't have the degree of ad-hoc flexibility that people have. It can't just arbitrarily decide to make a phone call if it doesn't have a 'phone' dependency. Even when using Dependency Injection, you'll have to add that dependency to a business object. You'll have to explicitly write code to give it that capability, and even so, an injected dependency doesn't magically imbue a business object with the capability to make 'ad-hoc phone calls'. A dependency comes with specific methods you can call in order to answer specific questions. </p> <p> Once you're adding code that enables an object to ask specific questions, you might as well just answer those questions up-front and pass the answer as method arguments. That's what this article's refactoring does. It knows that the <code>MaîtreD</code> object is going to ask about the existing reservations for the requested date, so it just passes that information as part of an 'execution context'. <blockquote> "A maître d'hôtel would presumably open his book of reservations to check if he still has a table available and would write a new reservation in his book" </blockquote> That's a brilliant observation! This just once again demonstrates what Evans wrote in <a href="http://amzn.to/WBCwx7">DDD</a>, that insight about the domain arrive piecemeal. A maître d'hôtel clearly doesn't depend on any <em>repository</em>, but rather on the book of reservations. You can add that as a dependency, or pass it as a method argument. I'd lean toward doing the latter, because I'd tend to view a book as a piece of data. </p> <p> Ultimately, if we are to take the idea of <em>inversion of control</em> seriously, we should, well, invert control. When we inject dependencies, we let the object with those dependencies control its interactions with them. Granted, those interactions are now polymorphic, but control isn't inverted. </p> <p> If you truly want to invert control, then load data, pass it to functions, and persist the return values. In that way, functions have no control of where data comes from, or what happens to it afterwards. This keeps a software design supple. </p> </div> <div class="comment-date">2019-02-13 7:26 UTC</div> </div> <div class="comment" id="c5208d5f4c534984891cdc6d5f5a9ca6"> <div class="comment-author">Marek Calus <a href="#c5208d5f4c534984891cdc6d5f5a9ca6">#</a></div> <div class="comment-content"> <p> Hi Mark, Thanks for your post, I think it's very valuable. </p> <p> In the past, I had a situation when I was a junior software developer and just started working on a small, internal web application (ASP.NET MVC) to support HR processes in our company. At the time, I was discovering blogs like yours, or fsharpforfunandprofit.com and was especially fond of the sandwich architecture. I was preparing to refactor one of the controllers just like your example in this post (Controller retrieving necessary data from the repository, passing it to the pure business logic, then wrapping the results in a request). Unfortunately, My more experienced colleague said that it's a "fat controller antipattern" and that the controller can have only one line of code - redirecting the request to the proper business logic method. I wanted to explain to him that he is wrong, but couldn't find proper arguments, or examples. </p> <p> Now I have them. This post is great for this particular purpose. </p> </div> <div class="comment-date">2019-02-13 11:54 UTC</div> </div> <div class="comment" id="38791df654f84d438db15b3e79112e6d"> <div class="comment-author">Ramon Pfeiffer <a href="#38791df654f84d438db15b3e79112e6d">#</a></div> <div class="comment-content"> <p> I guess it comes down to the amount of responsibilities the controller should have. </p> <p> Marek named the fat controller antipattern. I remember reading about some years ago and it stuck, that's why I usually model my controllers to delegate the request to a worker class, maybe map a return value to a transfer object and wrap it all in some <code>ActionResult</code>. I can relate to the argument that all I/O should happen at the boundaries of the system, though I'm not seeing it on the controller's responsibility list, all the more so when I/O exceeds a simple database call. </p> <blockquote> If you have too much IO going on, another separation of responsibilities is in order. </blockquote> <p> I think that is what I was aiming for. The third type that Tyson is looking a name for could then be some kind of thin Data Access Layer, serving as a façade to encapsulate all calls to I/O, that can be injected into the <code>MaîtreD</code> class. </p> <p> Isn't code flexibility usually modeled using conditionals? Assume we are a very important guest and our maître d'hôtel really wishes to make a reservation for us, but all tables are taken. He could decide to phone all currently known guests to ask for a confirmation, if some guest cannot make it, he could give the table to us. </p> <p> Using the initial version of <code>TryAccept</code>, it would lead to something like this: </p> <pre> public async Task&lt;int?&gt; TryAccept(Reservation reservation) { if(await CheckTableAvailability(reservation)) { reservation.IsAccepted = true; return await Repository.Create(reservation); } else { return null; } } private async Task&lt;bool&gt; CheckTableAvailability(Reservation reservation) { var reservations = await Repository.ReadReservations(reservation.Date); int reservedSeats = reservations.Sum(r => r.Quantity); if(Capacity < reservedSeats + reservation.Quantity) { foreach(var r in reservations) { if(!(await Telephone.AskConfirmation(r.Guest.PhoneNumber))) { //some guest cannot make it for his reservation return true; } } //all guests have confirmed their reservation - no table for us return false; } return true; } </pre> <p> That is assuming that <code>MaîtreD</code> has a dependency on both the Repository and a Telephone. Not the best code I've ever written, but it serves its purpose. If the dependency on <code>Reservation</code> is taken out of the <code>MaîtreD</code>, so could the dependency on <code>Telephone</code>. But then, you are deciding beforehand in the controller that <code>MaîtreD</code> <i>might</i> need to make a telephone call - that's business logic in the controller class and a weaker separation of concerns. </p> <blockquote> A maître d'hôtel clearly doesn't depend on any repository, but rather on the book of reservations. You can add that as a dependency, or pass it as a method argument. I'd lean toward doing the latter, because I'd tend to view a book as a piece of data. </blockquote> <p> And this is where I tend to disagree. The book of reservations in my eyes is owned and preciously guarded by the maître d'hôtel. Imagine some lowly garçon scribbling reservations in it. Unbelievable! Joking aside, the reservations in the book are pieces of data, no doubt about that - but I'd see the whole book as a resource owned by le maître and only him being able to request data from it. Of course, this depends on the model of the restaurant that I have in my mind, it might very well be different from yours - we didn't talk about a common model beforehand. </p> </div> <div class="comment-date">2019-02-13 19:54 UTC</div> </div> <div class="comment" id="451723a601f9487397c9aa7fa09381dc"> <div class="comment-author">Ramon Pfeiffer <a href="#451723a601f9487397c9aa7fa09381dc">#</a></div> <div class="comment-content"> <p> Apparently, I answered my own question when I moved the table availability check into its own private method. This way, a new dependency <code>TableAvailabilityChecker</code> can handle the availability check (complete with reservations book and phone calls), acting as a common data access layer. </p> <p> I have created a <a href="https://github.com/Thaoden/RestaurantReservation" target="_blank">repository</a>, where I tried to follow the steps outlined in this blog post with the new dependency. After all refactorings the controller looks like this: </p> <pre> public class ReservationsController : ControllerBase { private readonly MaitreD _maitreD; public ReservationsController(int capacity, IReservationsRepository repository, ITelephone telephone) { _maitreD = new MaitreD(capacity); Repository = repository; Telephone = telephone; } public IReservationsRepository Repository { get; } public ITelephone Telephone { get; } public async Task<iactionresult> Post(Reservation reservation) { Reservation[] currentReservations = await Repository.ReadReservations(reservation.Date); var confirmationCalls = currentReservations.Select(cr => Telephone.AskConfirmation(cr.Guest.PhoneNumber)); return _maitreD.CheckTableAvailability(currentReservations, reservation) .Match( some: r => new Maybe<reservation>(r), none: _maitreD.AskConfirmation(await Task.WhenAll(confirmationCalls), reservation) ) .Match( some: r => Ok(Repository.Create(_maitreD.Accept(r))), none: new ContentResult { Content = "Table unavailable", StatusCode = StatusCodes.Status500InternalServerError } as ActionResult ); } } </reservation></iactionresult></pre> <p> During the refactorings, I was able to remove the <code>TableAvailabilityChecker</code> again; I'm quite happy that the maître d'hôtel is checking the table availability and asking for the confirmations with the resources that are given to him. I'm not so happy with the <code>Task.WhenAll()</code> part, but I don't know how to make this more readable and at the same time make the calls only if we need them. </p> <p> All in all, I now think a bit differently about the controller responsibilities: Being at the boundary of the system, it is arguably the best place to make calls to external systems. If and how the information gathered from the outside <i>is used</i> however is still up to the business objects. Thanks, Mark, for the insight! </p> </div> <div class="comment-date">2019-02-15 11:40 UTC</div> </div> <div class="comment" id="b66a6b327b8949b69db335722c9501e1"> <div class="comment-author">Max <a href="#b66a6b327b8949b69db335722c9501e1">#</a></div> <div class="comment-content"> <p> Thanks for writing this article. Doesn't testability suffer from turning the Maître d into an implementation detail of the ReservationsController? Now, we not only have to test for the controller's specific responsibilities but also for the behaviour that is implemented by the Maître d. Previously we could have provided an appropriate test double when instantiating the controller, knowing that the Maître d is tested and working. The resulting test classes would be more specific and focused. Is this a trade-off you made in favour of bringing the article's point across? </p> </div> <div class="comment-date">2019-02-17 14:00 UTC</div> </div> <div class="comment" id="28415cd446b34649a210bc03c4e084e9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#28415cd446b34649a210bc03c4e084e9">#</a></div> <div class="comment-content"> <p> Max, thank you for writing. I don't think that testability suffers; on the contrary, I think that it improves. Once the <code>MaîtreD</code> class becomes deterministic, you no longer have to hide it behind a Test Double in order to be able to control its behaviour. You can control its behaviour simply by making sure that it receives the appropriate input arguments. </p> <p> The <a href="/2012/06/27/FacadeTest">Facade Tests</a> that cover <code>ReservationsController</code> in <a href="https://github.com/ploeh/asynchronous-injection">the repository</a> are, in my opinion, readable and maintainable. </p> <p> I've started <a href="/2019/02/18/from-interaction-based-to-state-based-testing">a new article series about this topic</a>, since I knew it'd come up. I hope that these articles will help illustrate my position. </p> </div> <div class="comment-date">2019-02-18 8:33 UTC</div> </div> <div class="comment" id="014bac380a0e44fb9e51d6846d643b97"> <div class="comment-author">Mykola Musiienko <a href="#014bac380a0e44fb9e51d6846d643b97">#</a></div> <div class="comment-content"> <p> Hi, Mark! Thank you for this blog post. </p> <p> I really like the way of composing effectful and pure code the post explains. But here are some things I keep wondering about. </p> <p> 1) Is it correct that &mdash; given this approach &mdash; pure code cannot call back into impure code. When I say call back I mean it in a general way: maybe invoking a lambda passed as an argument to a function, maybe invoking a method on an injected dependency &mdash; basically the specific mechanics of calling back are irrelevant in this case. </p> <p> 2) In case point 1) is actually correct, have you ever had in your practice a task where the "ban" on callbacks was too limiting/impractical? </p> <p> To give a more specific example of scenarios I have in mind, let's get back to <code>MaitreD</code> for a second. Let's imagine the amount of reservations data grew too big to load all at once. As a result <code>MaitreD</code> needs an instance of <code>ReservationRepository</code> so it can first run some business logic and based on the outcome read only a small specific subset of reservations from the repository. </p> <p> Or let's take a look at another imaginary scenario. Before confirming a reservation <code>MaitreD</code> must make a call to an external payment service to block a certain sum of money on the customer's card. </p> <p> These are only quick examples off the top of my head. Maybe dealing with them is easy, I would still be really grateful if you could give a couple of scenarios where you found it hard or impractical to do without "callbacks" and if and how you eventually manage to overcome the complications. </p> </div> <div class="comment-date">2019-06-14 17:56 UTC</div> </div> <div class="comment" id="a3393816ae90405299f7f030439f5b8f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a3393816ae90405299f7f030439f5b8f">#</a></div> <div class="comment-content"> <p> Mykola, thank you for writing. Yes, it's correct that <a href="/2018/11/19/functional-architecture-a-definition">a pure function can't call an impure function</a>. This means, among other things, that <a href="/2017/01/30/partial-application-is-dependency-injection">you can't use Dependency Injection in functional programming</a>. </p> <p> Is that rule impractical? It depends on the programming language. In Haskell, that rule is <em>enforced</em> by the compiler. You can't break that rule, but the language is also designed in such a way that there's plenty of better ways to do things. In Haskell, that rule isn't impractical. </p> <p> In a language like F#, that rule is no longer enforced, and there's also fewer built-in alternatives. The general solution to the problem is to use a free monad, but while it's possible to use <a href="/2017/08/07/f-free-monad-recipe">free monads in F#</a>, it's not as <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a>, and there's a compelling argument to be made that going with <a href="/2017/01/30/partial-application-is-dependency-injection">partial application as Dependency Injection</a> is more practical. </p> <p> It's also <a href="/2018/07/24/dependency-injection-revisited">possible to employ free monads in C#</a>, but it's really non-idiomatic and hard to understand. </p> <p> Haskell has some other general-purpose solutions (e.g. the so-called <em>mtl style</em>), but the only type of architecture I'm aware of that translates to F# or C# is free monads. </p> <p> Does this mean that the ideas of this article is impractical for real software? </p> <p> I don't think so. Let's consider your examples: <blockquote> <p> "Let's imagine the amount of reservations data grew too big to load all at once." </p> </blockquote> That's a frequently asked question, but in reality, I have a hard time imagining that. How much data is a reservation? It's a date (8 bytes), a quantity (in reality, a single byte is enough to keep track of that), as well as a name and an email address. Let's assume that an email address is, on average, shorter than 30 characters, and a name is shorter than 50 characters. We'll assume that we save both strings in UTF-8. Most characters are probably still just going to be 1 byte, but let's be generous and assume 2 bytes per character. That's 169 bytes per reservation, but let's be even more generous and say 200 bytes per reservation. </p> <p> What if we load 1,000 reservations? That's 200 kilobytes of memory. 10,000 reservations is 2 megabytes. That's about the size of an average web page. Is that too much data? </p> <p> We routinely load web pages over the internet, and none but the Australians complain about the size. </p> <p> My point is that I find it incredulous to claim that it'd be too much data if you need to load a couple of hundred of reservations in one go. </p> <p> I can definitely imagine scenarios where you'd like to load reservations not only for the date in question, but also for surrounding dates. Even for a medium-sized restaurant, that's unlikely to be more than a few hundred, or perhaps a few thousand of reservations. That's not a lot of data. Most pictures on the WWW are bigger. </p> <p> Just <a href="https://en.wikipedia.org/wiki/Speculative_execution">speculatively load extra data</a> in one go. It's going to make your code much simpler, and is unlikely to affect performance if you're being smart about it. You can even consider to cache that data... </p> <p> When it comes to your other question, I'll refer to a catch-phrase from the early days of large-scale web commerce (Pat Helland): <em>take the money</em>. </p> <p> Don't make payment a blocking call. As soon as you have enough information to execute a purchase, kick it off as an asynchronous background job. </p> <p> If a restaurant has that type of workflow that requires reservation of an amount on a credit card, you turn the business process into an asynchronous workflow. On the UI side, you make sure to reflect the current state of the system so that users don't try to reserve on dates that you already know are sold out. I show such a workflow in my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">functional architecture with F# Pluralsight course</a>. </p> <p> When a user makes a reservation, you take the reservation data and put it on a queue, and tell the user that you're working on it. </p> <p> A background job receives the queued message and decides whether or not to accept the reservation. If it decides to accept it, it creates two other messages: one to reserve the money, and another a timeout. </p> <p> Another background job receives the message to reserve the money on the credit card and attempts to do that. Once that's over, it reports success or failure by putting another message on a queue. </p> <p> The reservation system receives the asynchronous message about the credit card and either commits or cancels the reservation. If it never gets such a message, the timeout message will eventually trigger a cancellation. </p> <p> Each message handler can use the <a href="/2017/02/02/dependency-rejection">impure-pure-impure sandwich pattern</a>, and in that way keep the business logic pure. </p> <p> In my experience, you can often address issues like the ones you bring up by selecting an application architecture that best addresses those particular concerns. That'll make the implementation code simpler. </p> <p> In fact, I'm often struggling to come up with an example scenario where something like a free monad would be necessary, because I always think to myself: <em>Why would I do it that way? I'd just architect my application in this other way, and then the problem will go away by itself.</em> </p> </div> <div class="comment-date">2019-06-15 19:25 UTC</div> </div> <div class="comment" id="9a8a1152bf484fd98804339a4f8239f5"> <div class="comment-author">votroto <a href="#9a8a1152bf484fd98804339a4f8239f5">#</a></div> <div class="comment-content"> <p> Hi Mark, I was just wondering - why is the controller asking its clients for a capacity, when it only uses it to imediately create a MaitreD? Should it not ask for the MaitreD in the first place, instead of sneakily conjuring it up in the constructor? </p> <p> It feels like the controller owns the MaitreD, but the MaitreD represents pure and deterministic behaviour set up by its "capacity" dependency - and the capacity is only injected into the controller. So should it really be private? </p> <p> Can't we just ask for the MaitreD, leave it public, forget about the "implementation detail" of a capacity, and be done with it? </p> <p> Thanks! </p> </div> <div class="comment-date">2020-09-19 15:16 UTC</div> </div> <div class="comment" id="cb014e7f4c6d420faab1f3328331e7ba"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cb014e7f4c6d420faab1f3328331e7ba">#</a></div> <div class="comment-content"> <p> votroto, thank you for writing. Injecting a <a href="/2012/08/31/ConcreteDependencies">concrete dependency</a> is definitely an option. I'm currently working on a larger example code base that also has a <code>MaitreD</code> class, and in that code base I decided to inject that into the Controller instead of its constituent elements. </p> <p> It's a trade-off; I don't see one option as more correct than the other. In the present article, the <code>MaîtreD</code> class is so simple that it only has a single dependency: <code>capacity</code>. In this case, it's a toss-up. Either you inject <code>capacity</code>, or you inject the entire <code>MaîtreD</code> class. In both cases, you have a single dependency in addition to that <code>IReservationsRepository</code> dependency. In this situation, I chose to follow the principle of least knowledge. By injecting only the <code>capacity</code>, the <code>MaîtreD</code> class is an implementation detail not exposed to the rest of the world. </p> <p> In the larger example code base that I'm currently working on, the <code>MaitreD</code> class is more complex: it has several configuration values that determine its behaviour. If I wanted to keep the <code>MaitreD</code> class an implementation detail, the Controller constructor would look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeOfDay</span>&nbsp;opensAt, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeOfDay</span>&nbsp;lastSeating, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;seatingDuration, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Table</span>&gt;&nbsp;tables) { &nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;MaitreD&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaitreD</span>(opensAt,&nbsp;lastSeating,&nbsp;seatingDuration,&nbsp;tables); }</pre> </p> <p> I don't think that this addresses any real concerns. If I ever decide to change the <code>MaitreD</code> constructor, I'd have to also change the <code>ReservationsController</code> constructor. Thus, while <code>ReservationsController</code> might not 'formally' depend on <code>MaitreD</code>, it still does so in practice. In that case I chose to inject <code>MaitreD</code> instead: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">MaitreD</span>&nbsp;maitreD) { &nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;MaitreD&nbsp;=&nbsp;maitreD; }</pre> </p> <p> Don't read the above as an argument for one option over the other. I'm only trying to explain the deliberations I go through to arrive at a decision, one way or the other. </p> </div> <div class="comment-date">2019-09-20 10:18 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. How to get the value out of the monad https://blog.ploeh.dk/2019/02/04/how-to-get-the-value-out-of-the-monad 2019-02-04T07:45:00+00:00 Mark Seemann <div id="post"> <p> <em>How do I get the value out of my monad? You don't. You inject the desired behaviour into the monad.</em> </p> <p> A frequently asked question about monads can be paraphrased as: <em>How do I get the value out of my monad?</em> This seems to particularly come up when the monad in question is <a href="https://www.haskell.org">Haskell</a>'s <code>IO</code> monad, from which you <em>can't</em> extract the value. This is by design, but then beginners are often stumped on how to write the code they have in mind. </p> <p> You can encounter variations of the question, or at least the underlying conceptual misunderstanding, with other monads. This seems to be particularly prevalent when object-oriented or procedural programmers start working with <a href="/2018/03/26/the-maybe-functor">Maybe</a> or <a href="/2019/01/14/an-either-functor">Either</a>. People really want to extract 'the value' from those monads as well, despite the lack of guarantee that there will be a value. </p> <p> So how do you extract the value from a monad? </p> <p> The answer isn't <em>use a comonad</em>, although it could be, for a limited set of monads. Rather, the answer is <a href="https://en.wikipedia.org/wiki/Mu_(negative)">mu</a>. </p> <h3 id="8edf2d16396b46ad9b5a7d595c36b4a5"> Unit containers <a href="#8edf2d16396b46ad9b5a7d595c36b4a5" title="permalink">#</a> </h3> <p> Before I attempt to address how to work with monads, I think it's worthwhile to speculate on what misleads people into thinking that it makes sense to even contemplate extracting 'the value' from a monad. After all, you rarely encounter the question: <em>How do I get the value out of my collection?</em> </p> <p> Various collections form monads, but everyone intuitively understand that there isn't a single value in a collection. Collections could be empty, or contain many elements. Collections could easily be the most ordinary monad. Programmers deal with collections all the time. </p> <p> Yet, I think that most programmers don't realise that collections form monads. The reason for this could be that mainstream languages rarely makes this relationship explicit. Even <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq">C# query syntax</a>, which is nothing but monads in disguise, hides this fact. </p> <p> What happens, I think, is that when programmers first come across monads, they often encounter one of a few <em>unit containers</em>. </p> <p> What's a <em>unit container?</em> I admit that the word is one I made up, because I couldn't detect existing terminology on this topic. The idea, though, is that it's a <a href="/2018/03/22/functors">functor</a> guaranteed to contain exactly one value. Since <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">functors are containers</a>, I call such types <em>unit containers</em>. Examples include <a href="/2018/09/03/the-identity-functor">Identity</a>, <a href="/2018/09/10/the-lazy-functor">Lazy</a>, and <a href="/2018/09/24/asynchronous-functors">asynchronous functors</a>. </p> <p> You can extract 'the value' from most unit containers (with <code>IO</code> being the notable exception from the rule). Trivially, you can get the item contained in an Identity container: </p> <p> <pre>&gt; <span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">"bar"</span>); &gt; x.Item "bar"</pre> </p> <p> Likewise, you can extract the value from lazy and asynchronous values: </p> <p> <pre>&gt; <span style="color:#4ec9b0;">Lazy</span>&lt;<span style="color:#569cd6;">int</span>&gt;&nbsp;x&nbsp;<span style="color:#b4b4b4;">=</span>&nbsp;<span style="color:#569cd6;">new</span>&nbsp;<span style="color:#4ec9b0;">Lazy</span>&lt;<span style="color:#569cd6;">int</span>&gt;(()&nbsp;<span style="color:#b4b4b4;">=&gt;</span>&nbsp;<span style="color:#b5cea8;">42</span>); &gt; x.Value 42 &gt; <span style="color:#4ec9b0;">Task</span>&lt;<span style="color:#569cd6;">int</span>&gt;&nbsp;y&nbsp;<span style="color:#b4b4b4;">=</span>&nbsp;<span style="color:#4ec9b0;">Task</span><span style="color:#b4b4b4;">.</span>Run(()&nbsp;<span style="color:#b4b4b4;">=&gt;</span>&nbsp;<span style="color:#b5cea8;">1337</span>); &gt; <span style="color:#569cd6;">await</span>&nbsp;y 1337</pre> </p> <p> My theory, then, is that some programmers are introduced to the concept of monads via lazy or asynchronous computations, and that this could establish incorrect mental models. </p> <h3 id="393e8bcfa18244a988ff8ea56a1209b3"> Semi-containers <a href="#393e8bcfa18244a988ff8ea56a1209b3" title="permalink">#</a> </h3> <p> There's another category of monad that we could call <em>semi-containers</em> (again, I'm open to suggestions for a better name). These are data containers that contain either a single value, or no value. In this set of monads, we find <a href="https://stackoverflow.com/a/48490711/126014">Nullable&lt;T&gt;</a>, Maybe, and Either. </p> <p> Unfortunately, Maybe implementations often come with an API that enables you to ask a Maybe object if it's populated or empty, and a way to extract the value from the Maybe container. This misleads many programmers to write code like this: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;id&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">if</span>&nbsp;(id.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Customer</span>(id.Item); <span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DontKnowWhatToDoException</span>();</pre> </p> <p> Granted, in many cases, people do something more reasonable than throwing a useless exception. In a specific context, it may be clear what to do with an empty Maybe object, but there are problems with this Tester-Doer approach: <ul> <li>It doesn't compose.</li> <li>There's no systematic technique to apply. You always need to handle empty objects in a context-specific way.</li> </ul> These issues interact in unpleasant ways. </p> <p> If you throw an exception when the object is empty, you'll likely have to deal with that exception further up in the call stack. </p> <p> If you return a magic value (like returning <code>-1</code> when a natural number is expected), you again force all callers to check for that magic number. </p> <p> If you set a flag that indicates that an object was empty, again, you put the burden on callers to check for the flag. </p> <p> This leads to <a href="/2013/07/08/defensive-coding">defensive coding</a>, which, at best, makes the code unreadable. </p> <h3 id="f2fd9c0c72fd45ea86908edb606740b6"> Behaviour Injection <a href="#f2fd9c0c72fd45ea86908edb606740b6" title="permalink">#</a> </h3> <p> Interestingly, programmers rarely take a Tester-Doer approach to working with collections. Instead, they rely on APIs for collections and arrays. </p> <p> In C#, <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq">LINQ</a> has been around since 2007, and most programmers love it. It's common knowledge that you can use the <code>Select</code> method to, for example, convert an array of numbers to an array of strings: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125&nbsp;}.Select(i&nbsp;=&gt;&nbsp;i.ToString()) string[4] { "42", "1337", "2112", "90125" }</pre> </p> <p> You can do that with all functors, including Maybe: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;id&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Customer</span>&gt;&nbsp;c&nbsp;=&nbsp;id.Select(x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Customer</span>(x));</pre> </p> <p> A <a href="/2018/03/26/the-maybe-functor">previous article</a> offers a slightly more compelling example: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;viewModel&nbsp;=&nbsp;repository.Read(id).Select(r&nbsp;=&gt;&nbsp;r.ToViewModel());</pre> </p> <p> Common to all the three above examples is that instead of trying to extract a value from the monad (which makes no sense in the array example), you inject the desired behaviour into the context of the data container. What that eventually brings about depends on the monad in question. </p> <p> In the array example, the behaviour being injected is that of turning a number into a string. Since this behaviour is injected into a collection, it's applied to every element in the source array. </p> <p> In the second example, the behaviour being injected is that of turning an integer into a <code>Customer</code> object. Since this behaviour is injected into a Maybe, it's only applied if the source object is populated. </p> <p> In the third example, the behaviour being injected is that of turning a <code>Reservation</code> domain object into a View Model. Again, this only happens if the original Maybe object is populated. </p> <h3 id="9bfd775fc56740198bec05809c6c1b06"> Composability <a href="#9bfd775fc56740198bec05809c6c1b06" title="permalink">#</a> </h3> <p> The marvellous quality of a monad is that it's composable. You could, for example, start by attempting to parse a string into a number: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;candidate&nbsp;=&nbsp;<span style="color:green;">//&nbsp;Some&nbsp;string&nbsp;from&nbsp;application&nbsp;boundary</span> <span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;idm&nbsp;=&nbsp;TryParseInt(candidate);</pre> </p> <p> This code could be defined in a part of your code base that deals with user input. Instead of trying to get 'the value' out of <code>idm</code>, you can pass the entire object to other parts of the code. The next step, defined in a different method, in a different class, perhaps even in a different library, then queries a database to read a <code>Reservation</code> object corresponding to that ID - if the ID is there, that is: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;rm&nbsp;=&nbsp;idm.SelectMany(repository.Read);</pre> </p> <p> The <code>Read</code> method on the repository has this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;Read(<span style="color:blue;">int</span>&nbsp;id)</pre> </p> <p> The <code>Read</code> method returns a <code>Maybe&lt;Reservation&gt;</code> object because you could pass any <code>int</code> to the method, but there may not be a row in the database that corresponds to that number. Had you used <code>Select</code> on <code>idm</code>, the return type would have been <code>Maybe&lt;Maybe&lt;Reservation&gt;&gt;</code>. This is a typical example of a nested functor, so instead, you use <code>SelectMany</code>, which flattens the functor. You can do this because Maybe is a monad. </p> <p> The result at this stage is a <code>Maybe&lt;Reservation&gt;</code> object. If all goes according to plan, it's populated with a <code>Reservation</code> object from the database. Two things could go wrong at this stage, though: <ol> <li>The <code>candidate</code> string didn't represent a number.</li> <li>The database didn't contain a row for the parsed ID.</li> </ol> If any of these errors occur, <code>idm</code> is empty. </p> <p> You can now pass <code>rm</code> to another part of the code base, which then performs this step: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">ReservationViewModel</span>&gt;&nbsp;vm&nbsp;=&nbsp;rm.Select(r&nbsp;=&gt;&nbsp;r.ToViewModel());</pre> </p> <p> Functors and monads are composable (i.e. 'chainable'). This is a fundamental trait of functors; they're (endo)morphisms, which, by definition, are composable. In order to leverage that composability, though, you must retain the monad. If you extract 'the value' from the monad, composability is lost. </p> <p> For that reason, you're not supposed to 'get the value out of the monad'. Instead, you inject the desired behaviour into the monad in question, so that it stays composable. In the above example, <code>repository.Read</code> and <code>r.ToViewModel()</code> are behaviors injected into the Maybe monad. </p> <h3 id="e3be91ccb5a04a99a67d57c39e49b446"> Summary <a href="#e3be91ccb5a04a99a67d57c39e49b446" title="permalink">#</a> </h3> <p> When we learn something new, there's always a phase where we struggle to understand a new concept. Sometimes, we may, inadvertently, erect a tentative, but misleading mental model of a concept. It seems to me that this happens to many people while they're grappling with the concept of functors and monads. </p> <p> One common mental wrong turn that many people seem to take is to try to 'get the value out of the monad'. This seems to be particularly common with <code>IO</code> in Haskell, where the issue is a <a href="https://stackoverflow.com/q/51614573/126014">frequently</a> <a href="https://stackoverflow.com/q/8567743/126014">asked</a> <a href="https://stackoverflow.com/q/7314789/126014">question</a>. </p> <p> I've also reviewed enough <a href="https://fsharp.org">F#</a> code to have noticed that people often take the imperative, Tester-Doer road to <code>'a option</code>. That's the reason this article uses a majority of its space on various Maybe examples. </p> <p> In a future article, I'll show a more complete and compelling example of behaviour injection. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="3375d8afbf574cefa3bd95072d2a936c"> <div class="comment-author">Sean Donohue <a href="#3375d8afbf574cefa3bd95072d2a936c">#</a></div> <div class="comment-content"> <p> Hi Mark, was very interested in your post as I do try and use Option Monads in my code, and I think I understand the point you are making about not thinking of an optional value as something that is composable. However, I recently had a couple of situations where I reluctantly had to check the value, would really appreciate any thoughts you may have? </p> <p> The first example was where I have a UI and the user may specify a Latitude and a Longitude. The user may not yet have specified both values, so each is held as an Option<double>. We then need to calculate the rhumb bearing to a fixed location, so I wrote: <pre> if(latitude.HasValue && longitude.HasValue) Bearing = CalculateRhumbBearing(latitude.Value, longitude.Value, fixedLatitude, fixedLongitude).ToOptionMonad(); else Bearing = OptionMonad&lt;double&gt;.None;</pre> <p> <p> Having read your article, I realise I could change this to a Select statement on latitude, but that lambda would still need to check longitude.HasValue. Should I combine the two options somehow before doing a single Select? </p> <p> </p> <p> The second example again relates to a UI where the user can enter values in a grid, or leave a row blank. I would like to calculate the mean, standard deviation and root mean square of the values, and normally all these functions would have the signature: double Mean(ICollection&lt;double&gt; values) </p> <p> If I keep this then I need a function like <pre> foreach(var item in values) { if(item.HasValue) { yield return item.Value; } } </pre> </p> <p> Or some equivalent Where/Select combination. Can you advise me please, how you recommend transforming an IEnumerable&lt;OptionMonad&lt;X&gt;&gt; to an enumerable&lt;X&gt;? Or should I write a signature overload double Mean(ICollection&lt;OptionMonad&lt;double&gt;&gt; possibleValues) and ditto for SD and RMS? <p> <p> Thanks, Sean </p> </div> <div class="comment-date">2018-02-05 11:30 UTC</div> </div> <div class="comment" id="f8d3e120bf9a46d09b479106812e7020"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f8d3e120bf9a46d09b479106812e7020">#</a></div> <div class="comment-content"> <p> Sean, thank you for writing. The first example you give is quite common, and is easily addressed with using the <a href="/2018/10/29/the-maybe-applicative-functor">applicative</a> or monadic capabilities of <em>Maybe</em>. Often, in a language like C#, it's easiest to use monadic <em>bind</em> (in C# called <code>SelectMany</code>): </p> <p> <pre>Bearing&nbsp;=&nbsp;latitude &nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(lat&nbsp;=&gt;&nbsp;longitude &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(lon&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CalculateRhumbBearing(lat,&nbsp;lon,&nbsp;fixedLatitude,&nbsp;fixedLongitude)));</pre> </p> <p> If you find code like that disagreeable, you can also write it with query syntax: </p> <p> <pre>Bearing&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;lat&nbsp;<span style="color:blue;">in</span>&nbsp;latitude &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;lon&nbsp;<span style="color:blue;">in</span>&nbsp;longitude &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;CalculateRhumbBearing(lat,&nbsp;lon,&nbsp;fixedLatitude,&nbsp;fixedLongitude);</pre> </p> <p> Here, <code>Bearing</code> is a <em>Maybe</em> value. As you can see, in neither of the above alternatives is it necessary to check and extract the values. <code>Bearing</code> will be populated when both <code>latitude</code> and <code>longitude</code> are populated, and empty otherwise. </p> <p> Regarding the other question, being able to filter out empty values from a collection is a standard operation in both F# and Haskell. In C#, you can write it like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Choose&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectMany(m&nbsp;=&gt;&nbsp;m.Match(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">T</span>[0],&nbsp;x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;x&nbsp;})); }</pre> </p> <p> This example is based on the <a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a>, which is currently my favourite implementation. I decided to call the method <code>Choose</code>, as this is also the name it has in F#. In Haskell, this function is called <code>catMaybes</code>. </p> </div> <div class="comment-date">2019-02-05 16:25 UTC</div> </div> <div class="comment" id="cd846c7abe1040edaeaa8ab97f7190ff"> <div class="comment-author">Achim Stuy <a href="#cd846c7abe1040edaeaa8ab97f7190ff">#</a></div> <div class="comment-content"> <p> Hi Mark, did you ever think about publishing a Library containing all these types missing in .net Framework like <code>Either</code>? Or can you recommend an existing library? </p> </div> <div class="comment-date">2019-02-07 07:59 UTC</div> </div> <div class="comment" id="885dd7685e8142d7b5af72796ea8c770"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#885dd7685e8142d7b5af72796ea8c770">#</a></div> <div class="comment-content"> <p> Achim, thank you for writing. The thought has crossed my mind, but my position on this question seems to be changing. </p> <p> Had you asked me one or two years ago, I'd have answered that I hadn't seriously considered doing that, and that I saw little reason to do so. There is, as far as I can tell, plenty of such libraries out there, although I can't recommend any in particular. This seems to be something that many people create as part of a learning exercise. It seems to be a rite of passage for many people, similarly to developing a Dependency Injection container, or an ORM. </p> <p> Besides, a reusable library would mean another dependency that your code would have to take on. </p> <p> These days, however, I'm beginning to reconsider my position. It seems that no such library is emerging as dominant, and some of the types involved (particularly Maybe) would really be useful. </p> <p> Ideally, these types ought be in the .NET Base Class Library, but perhaps a second-best alternative would be to put them in a commonly-used shared library. </p> </div> <div class="comment-date">2019-02-07 11:15 UTC</div> </div> <div class="comment" id="88e67e633795438f9b1e927a6ca1410c"> <div class="comment-author">Ralph Hendriks <a href="#88e67e633795438f9b1e927a6ca1410c">#</a></div> <div class="comment-content"> <p> Hi Mark, thank you for the interesting article series. </p> <p> Can you maybe provide guidance of how asynchronous operations can become part of a chain of operations? How would the 'functor flattening' be combined with the built Task/Task&lt;T&gt; types? Extending your example, how would you go about if we would like to enrich the reservation retrieved from repository with that day's special, which happens to be async: <pre>Task&lt;ReservationWithMenuSuggestion&gt; EnrichWithSpecialOfTheDayAsync(Reservation reservation)</pre> <p> <p> I tried with your Church encoded Maybe implementation, but I got stuck with the Task&lt;T&gt; wrapping/unwrapping/awaiting. </div> <div class="comment-date">2019-02-07 15:06 UTC</div> </div> <div class="comment" id="3588d6472d4f4e76832925221aae374a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3588d6472d4f4e76832925221aae374a">#</a></div> <div class="comment-content"> <p> Ralph, thank you for writing. Please see if my new article <a href="/2019/02/11/asynchronous-injection">Asynchronous Injection</a> answers your question. </p> </div> <div class="comment-date">2019-02-11 7:56 UTC</div> </div> <div class="comment" id="515ed16bdbab4b9aa12a20b20e2210e1"> <div class="comment-author">Dominik Jeske <a href="#515ed16bdbab4b9aa12a20b20e2210e1">#</a></div> <div class="comment-content"> <p> Hi Mark, I'm curious what do you think about this approach to monads - <a href="https://habr.com/en/post/458692/">Maybe monad through async/await in C#</a> </p> </div> <div class="comment-date">2019-07-31 21:15 UTC</div> </div> <div class="comment" id="db0c16dd403d43f0947b536dc91ec1b1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#db0c16dd403d43f0947b536dc91ec1b1">#</a></div> <div class="comment-content"> <p> Dominik, thank you for writing. That's a clever article. As far as I can tell, the approach is similar to Nick Palladinos' <a href="https://github.com/nessos/Eff">Eff</a> library. You can see how he <a href="https://github.com/palladin/dependency-injection-revisited">rewrote one of my sample applications using it</a>. </p> <p> I've no personal experience with this approach, so I could easily be wrong in my assessment. Nick reports that he's had some success getting other people on board with such an approach, because <a href="https://github.com/palladin/dependency-injection-revisited/blob/5b45be31ef14144895006cb4ca49c8365ddeae3e/CSharp/BookingApi/Ma%C3%AEtreDEffects.cs">the resulting user code looks like idiomatic C#</a>. That is, I think, one compelling argument. </p> <p> What I do find less appealing, however, is that, if I understand this correctly, the C# compiler enables you to mix, or interleave, disparate effects. As long as your method returns an awaitable object, you can await true asynchronous tasks, bind Maybe values, and conceivably invoke other effectful operations all in the same method - and you wouldn't be able to tell from the return type what to expect. </p> <p> In my opinion, one of the most compelling benefits of modelling with universal abstractions is that they provide excellent encapsulation. You can use the type system to communicate the pre- and post-conditions of an operation. </p> <p> If I see an operation that returns <code>Maybe&lt;User&gt;</code>, I expect that it may or may not return a <code>User</code> object. If I see a return type of <code>Task&lt;User&gt;</code>, I expect that I'm guaranteed to receive a <code>User</code> object, but that this'll happen asynchronously. Only if I see something like <code>Task&lt;Maybe&lt;User&gt;&gt;</code> do I expect the combination of those two effects. </p> <p> My concern with making Maybe awaitable is that this enables one to return <code>Maybe&lt;User&gt;</code> from a method, but still make the implementation asynchronous (e.g. by querying a database for the user). That effect is now hidden, which in my view break encapsulation because you now have to go and read the implementation code in order to discover that this is taking place. </p> </div> <div class="comment-date">2019-08-02 10:54 UTC</div> </div> <div class="comment" id="caf235dc36ae4f16a2441ada829caeeb"> <div class="comment-author">Dominik Jeske <a href="#caf235dc36ae4f16a2441ada829caeeb">#</a></div> <div class="comment-content"> <p> Mark, thanks for useful respond! </p> <p> You are absolutely true that awaiting Maybe that have Task have side effects not visible to invoker. I agree that this is the smell I felt but you gave me the source of it. </p> </div> <div class="comment-date">2019-08-03 20:43 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Better abstractions revisited https://blog.ploeh.dk/2019/01/28/better-abstractions-revisited 2019-01-28T07:45:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you design better abstractions? A retrospective look on an old article for object-oriented programmers.</em> </p> <p> About a decade ago, I had already been doing test-driven development (TDD) and used Dependency Injection for many years, but I'd started to notice some patterns about software design. I'd noticed that <a href="/2010/12/02/Interfacesarenotabstractions">interfaces aren't abstractions</a> and that <a href="/2010/12/22/TheTDDApostate">TDD isn't a design methodology</a>. Sometimes, I'd arrive at interfaces that turned out to be good abstractions, but at other times, the interfaces I created seemed to serve no other purpose than enabling unit testing. </p> <p> In 2010 I thought that I'd noticed some patterns for good abstractions, so I wrote an article called <a href="/2010/12/03/Towardsbetterabstractions">Towards better abstractions</a>. I still consider it a decent attempt at communicating my findings, but I don't think that I succeeded. My thinking on the subject was still too immature, and I lacked a proper vocabulary. </p> <p> While I had hoped that I would be able to elaborate on such observations, and perhaps turn them into heuristics, my efforts soon after petered out. I moved on to other things, and essentially gave up on this particular research programme. Years later, while trying to learn <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, I suddenly realised that mathematical disciplines like category theory and abstract algebra could supply the vocabulary. After some further work, I started publishing a substantial and long-running article series called <a href="/2017/10/04/from-design-patterns-to-category-theory">From design patterns to category theory</a>. It goes beyond my initial attempt, but it finally enabled me to crystallise those older observations. </p> <p> In this article, I'll revisit that old article, <em>Towards better abstractions</em>, and translate the vague terminology I used then, to the terminology presented in <em>From design patterns to category theory</em>. </p> <p> The thrust of the old article is that if you can create a <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composite</a> or a <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a> from an interface, then it's likely to be a good abstraction. I still consider that a useful rule of thumb. </p> <p> When can you create a Composite? <a href="/2018/03/12/composite-as-a-monoid">When the abstraction gives rise to a monoid</a>. When can you create a Null Object? <a href="/2018/04/23/null-object-as-identity">When the abstraction gives rise to a monoid</a>. </p> <p> <img src="/content/binary/better-abstractions-as-monoids.png" alt="The terms from the better abstractions article embedded in the set of monoids."> </p> <p> All the 'API shapes' I'd identified in <em>Towards better abstractions</em> form <a href="/2017/10/06/monoids">monoids</a>. </p> <h3 id="8ef17cb1e20547af9515c874d23a574d"> Commands <a href="#8ef17cb1e20547af9515c874d23a574d" title="permalink">#</a> </h3> <p> A <a href="https://en.wikipedia.org/wiki/Command_pattern">Command</a> seems to be universally identified by a method typically called <code>Execute</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Execute()</pre> </p> <p> From <a href="/2018/01/15/unit-isomorphisms">unit isomorphisms</a> we know that methods with the <code>void</code> return type are <a href="/2018/01/08/software-design-isomorphisms">isomorphic</a> to (impure) functions that return <em>unit</em>, and that <em>unit</em> forms a monoid. </p> <p> Furthermore, we know from <a href="/2017/11/06/function-monoids">function monoids</a> that methods that return a monoid themselves form monoids. Therefore, Commands form monoids. </p> <p> In early 2011 I'd already explicitly noticed that <a href="/2011/03/22/CommandsareComposable">Commands are composable</a>. Now I know the deeper reason for this: they're monoids. </p> <h3 id="42ac895ddb2a4fa8be678f4c74493cfc"> Closure of operations <a href="#42ac895ddb2a4fa8be678f4c74493cfc" title="permalink">#</a> </h3> <p> In <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>, Eric Evans discusses the benefits of designing APIs that exhibit <em>closure of operations</em>. This means that a method returns the same type as all its input arguments. The simplest example is the one that I show in the old article: </p> <p> <pre><span style="color:blue;">public&nbsp;static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;DoIt(<span style="color:#2b91af;">T</span>&nbsp;x)</pre> </p> <p> That's just an <a href="/2017/11/13/endomorphism-monoid">endomorphism, which forms a monoid</a>. </p> <p> Another variation is a method that takes two arguments: </p> <p> <pre><span style="color:blue;">public&nbsp;static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;DoIt(<span style="color:#2b91af;">T</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;y)</pre> </p> <p> This is a binary operation. While it's certainly a <a href="/2017/12/27/magmas">magma</a>, in itself it's not guaranteed to be a monoid. In fact, Evans' <a href="/2018/01/02/colour-mixing-magma">colour-mixing example is only a magma</a>, but not a monoid. You can, however, also view this as a special case of the <em>reduction of input</em> shape, below, where the 'extra' arguments just happen to have the same type as the return type. In that interpretation, such a method still forms a monoid, but it's not guaranteed to be meaningful. (Just like modulo 31 addition forms a monoid; it's hardly useful.) </p> <p> The same sort of argument goes for methods with closure of operations, but more input arguments, like: </p> <p> <pre><span style="color:blue;">public&nbsp;static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;DoIt(<span style="color:#2b91af;">T</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;z)</pre> </p> <p> This sort of method is, however, rare, unless you're working in a <a href="http://wiki.c2.com/?StringlyTyped">stringly typed</a> code base where methods look like this: </p> <p> <pre><span style="color:blue;">public&nbsp;static</span>&nbsp;<span style="color:blue">string</span>&nbsp;DoIt(<span style="color:blue">string</span>&nbsp;x,&nbsp;<span style="color:blue">string</span>&nbsp;y,&nbsp;<span style="color:blue">string</span>&nbsp;z)</pre> </p> <p> That's a different situation, though, because those strings should probably be <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">turned into domain types</a> that properly communicate their roles. Once you do that, you'll probably find that the method arguments have different types. </p> <p> In any case, regardless of cardinality, you can view all methods with closure of operations as special cases of the <em>reduction of input</em> shape below. </p> <h3 id="047886dcfa5a4a1398965138669e0ddc"> Reduction of input <a href="#047886dcfa5a4a1398965138669e0ddc" title="permalink">#</a> </h3> <p> This is the part of the original article where my struggles with vocabulary began in earnest. The situation is when you have a method that looks like this, perhaps as an interface method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T3</span>&nbsp;z); }</pre> </p> <p> In order to stay true to the terminology of my original article, I've named this <em>reduction of input</em> generic example <code>IInputReducer</code>. The reason I originally called it <em>reduction of input</em> is that such a method takes a set of input types as arguments, but only returns a value of a type that's a subset of the set of input types. Thus, the method looks like it's reducing the range of input types to a single one of those types. </p> <p> <img src="/content/binary/reduction-of-input.png" alt="Diagram showing three generic types T1, T2, and T3 entering a funnel that only lets T1 pass through."> </p> <p> A realistic example could be a piece of HTTP middleware that defines an <em>action filter</em> as an interface that you can implement to intercept each HTTP request: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IActionFilter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;&nbsp;ExecuteActionFilterAsync( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpActionContext</span>&nbsp;actionContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">CancellationToken</span>&nbsp;cancellationToken, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;&nbsp;continuation); }</pre> </p> <p> This is a slightly modified version of <a href="https://docs.microsoft.com/en-us/previous-versions/aspnet/mt175073(v%3dvs.118)">an earlier version of the ASP.NET Web API</a>. Notice that in this example, it's not the first argument's type that doubles as the return type, but rather the third and last argument. The <em>reduction of input</em> 'shape' can take an arbitrary number of arguments, and any of the argument types can double as a return type, regardless of position. </p> <p> Returning to the generic <code>IInputReducer</code> example, you can easily make a Composite of it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CompositeInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;[]&nbsp;reducers; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;CompositeInputReducer(<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;[]&nbsp;reducers) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.reducers&nbsp;=&nbsp;reducers; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T3</span>&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;acc&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;reducer&nbsp;<span style="color:blue;">in</span>&nbsp;reducers) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;reducer.DoIt(acc,&nbsp;y,&nbsp;z); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that you call <code>DoIt</code> on all the composed <code>reducers</code>. The arguments that aren't part of the return type, <code>y</code> and <code>z</code>, are passed to each call to <code>DoIt</code> unmodified, whereas the <code>T1</code> value <code>x</code> is only used to initialise the accumulator <code>acc</code>. Each call to <code>DoIt</code> also returns a <code>T1</code> object, so the <code>acc</code> value is updated to that object, so that you can use it as an input for the next iteration. </p> <p> This is an imperative implementation, but as you'll see below, you can also implement the same behaviour in a functional manner. </p> <p> For the sake of argument, pretend that you reorder the method arguments so that the method looks like this: </p> <p> <pre><span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T3</span>&nbsp;z,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;x);</pre> </p> <p> From <a href="/2018/02/05/uncurry-isomorphisms">Uncurry isomorphisms</a> you know that a method like that is isomorphic to a function with the type <code>'T3 -&gt; 'T2 -&gt; 'T1 -&gt; 'T1</code> (<a href="https://fsharp.org">F#</a> syntax). You can think of such a curried function as a function that returns a function that returns a function: <code>'T3 -&gt; ('T2 -&gt; ('T1 -&gt; 'T1))</code>. The rightmost function <code>'T1 -&gt; 'T1</code> is clearly an endomorphism, and you already know that an endomorphism gives rise to a monoid. Finally, <a href="/2017/11/06/function-monoids">Function monoids</a> informs us that a function that returns a monoid itself forms a monoid, so <code>'T2 -&gt; ('T1 -&gt; 'T1)</code> forms a monoid. This argument applies recursively, because if that's a monoid, then <code>'T3 -&gt; ('T2 -&gt; ('T1 -&gt; 'T1))</code> is also a monoid. </p> <p> What does that look like in C#? </p> <p> In the rest of this article, I'll revert the <code>DoIt</code> method signature to <code>T1 DoIt(T1 x, T2 y, T3 z);</code>. The monoid implementation looks much like the endomorphism code. Start with a binary operation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;Append&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r1, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r2) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AppendedReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;(r1,&nbsp;r2); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AppendedReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;AppendedReducer( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r2) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.r1&nbsp;=&nbsp;r1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.r2&nbsp;=&nbsp;r2; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T3</span>&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;r2.DoIt(r1.DoIt(x,&nbsp;y,&nbsp;z),&nbsp;y,&nbsp;z); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is similar to the endomorphism <code>Append</code> implementation. When you combine two <code>IInputReducer</code> objects, you receive an <code>AppendedReducer</code> that implements <code>DoIt</code> by first calling <code>DoIt</code> on the first object, and then using the return value from that method call as the input for the second <code>DoIt</code> method call. Notice that <code>y</code> and <code>z</code> are just 'context' variables used for both reducers. </p> <p> Just like the endomorphism, you can also implement the <em>identity</em> input reducer: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IdentityInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T3</span>&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This simply returns <code>x</code> while ignoring <code>y</code> and <code>z</code>. The <code>Append</code> method is associative, and the <code>IdentityInputReducer</code> is both <em>left</em> and <em>right identity</em> for the operation, so this is a monoid. Since <a href="/2017/11/20/monoids-accumulate">monoids accumulate</a>, you can also implement an <code>Accumulate</code> extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;Accumulate&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&gt;&nbsp;reducers) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;identity&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdentityInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reducers.Aggregate(identity,&nbsp;(acc,&nbsp;reducer)&nbsp;=&gt;&nbsp;acc.Append(reducer)); }</pre> </p> <p> This implementation follows the overall implementation pattern for accumulating monoidal values: start with the identity and combine pairwise. While I usually show this in a more imperative form, I've here used a proper functional implementation for the method. </p> <p> The <code>IInputReducer</code> object returned from that <code>Accumulate</code> function has exactly the same behaviour as the <code>CompositeInputReducer</code>. </p> <p> The <em>reduction of input</em> shape forms another monoid, and is therefore composable. The Null Object is the <code>IdentityInputReducer&lt;T1, T2, T3&gt;</code> class. If you set <code>T1 = T2 = T3</code>, you have the <em>closure of operations</em> 'shapes' discussed above; they're just special cases, so form at least this type of monoid. </p> <h3 id="53d27a7c707943b5b2d502e6f7977526"> Composable return types <a href="#53d27a7c707943b5b2d502e6f7977526" title="permalink">#</a> </h3> <p> The original article finally discusses methods that in themselves don't look composable, but turn out to be so anyway, because their return types are composable. Without knowing it, I'd figured out that <a href="/2017/11/06/function-monoids">methods that return monoids are themselves monoids</a>. </p> <p> In 2010 I didn't have the vocabulary to put this into specific language, but that's all it says. </p> <h3 id="8b801b9446f34a2587d844b9eb67a6bd"> Summary <a href="#8b801b9446f34a2587d844b9eb67a6bd" title="permalink">#</a> </h3> <p> In 2010 I apparently discovered an ad-hoc, informally specified, vaguely glimpsed, half-understood description of half of abstract algebra. </p> <p> Riffs on <a href="https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule">Greenspun's tenth rule</a> aside, things clicked for me once I started to investigate what category theory was about, and why it seemed so closely linked to <a href="https://www.haskell.org">Haskell</a>. That's one of the reasons I started writing the <a href="/2017/10/04/from-design-patterns-to-category-theory">From design patterns to category theory</a> article series. </p> <p> The patterns I thought that I could see in 2010 all form monoids, but there are many other universal abstractions from mathematics that apply to programming as well. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Some thoughts on anti-patterns https://blog.ploeh.dk/2019/01/21/some-thoughts-on-anti-patterns 2019-01-21T07:30:00+00:00 Mark Seemann <div id="post"> <p> <em>What's an anti-pattern? Are there rules to identify them, or is it just name-calling? Before I use the term, I try to apply some rules of thumb.</em> </p> <p> It takes time to write a book. Months, even years. It took me two years to write the first edition of <a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a>. The <a href="/dippp">second edition of Dependency Injection in .NET</a> is also the result of much work; not so much by me, but by my co-author <a href="http://www.cuttingedge.it/blogs/steven">Steven van Deursen</a>. </p> <p> When you write a book single-handedly, you can be as opinionated as you'd like. When you have a co-author, regardless of how much you think alike, there's bound to be some disagreements. Steven and I agreed about most of the changes we'd like to make to the second edition, but each of us had to yield or compromise a few times. </p> <p> An interesting experience has been that on more than one occasion where I've reluctantly had to yield to Steven, over the time, I've come to appreciate his position. Two minds think better than one. </p> <h3 id="6497bdfd47db433c92a6ec4fabfd4e92"> Ambient Context <a href="#6497bdfd47db433c92a6ec4fabfd4e92" title="permalink">#</a> </h3> <p> One of the changes that Steven wanted to make was that he wanted to change the status of the <em>Ambient Context</em> pattern to an anti-pattern. While I never use that pattern myself, I included it in the first edition in the spirit of the original <a href="http://amzn.to/XBYukB">Design Patterns</a> book. The <em>Gang of Four</em> made it clear that the patterns they'd described weren't invented, but rather discovered: <blockquote> <p> "We have included only designs that have been applied more than once in different systems." </p> <footer><cite>Gamma et al, <em>Design Patterns</em>, 1994, p. 2</cite></footer> </blockquote> The spirit, as I understand it, is to identify solutions that already exist, and catalogue them. When I wrote the first edition of my book, I tried to do that as well. </p> <p> I'd noticed what I eventually named the <em>Ambient Context</em> pattern several places in the .NET Base Class Library. Some of those APIs are still around today. <a href="https://docs.microsoft.com/dotnet/api/system.threading.thread.currentprincipal">Thread.CurrentPrincipal</a>, <a href="https://docs.microsoft.com/dotnet/api/system.globalization.cultureinfo.currentculture">CultureInfo.CurrentCulture</a>, <a href="https://en.wikipedia.org/wiki/Thread-local_storage">thread-local storage</a>, <a href="https://docs.microsoft.com/dotnet/api/system.web.httpcontext.current">HttpContext.Current</a>, and so on. </p> <p> None of these really have anything to do with Dependency Injection (DI), but people sometimes attempt to use them to solve problems similar to the problems that DI addresses. For that reason, and because the pattern was so prevalent, I included it in the book - as a pattern, not an anti-pattern. </p> <p> Steven wanted to make it an anti-pattern, and I conceded. I wasn't sure I was ready to explicitly call it out as an anti-pattern, but I agreed to the change. I'm becoming increasingly happy that Steven talked me into it. </p> <h3 id="08fbf0fd59154a57bbba936128f6c60e"> Pareto efficiency <a href="#08fbf0fd59154a57bbba936128f6c60e" title="permalink">#</a> </h3> <p> I've heard said of me that I'm one of those people who call everything I don't like an anti-pattern. I don't think that's true. </p> <p> I think people's perception of me is skewed because even today, the most visited page (my greatest hit, if you will) is an article called <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator is an Anti-Pattern</a>. (It concerns me a bit that an article from 2010 seems to be my crowning achievement. I hope I haven't peaked yet, but the numbers tell a different tale.) </p> <p> While I've used the term <em>anti-pattern</em> in other connections, I prefer to be conservative with my use of the word. I tend to use it only when I feel confident that something is, indeed, an anti-pattern. </p> <p> What's an anti-pattern? <a href="https://amzn.to/2VHDX3m">AntiPatterns</a> defines it like this: <blockquote> <p> "An AntiPattern is a literary form that describes a commonly occurring solution to a problem that generates decidedly negative consequences." </p> <footer><cite>Brown et al, <em>AntiPatterns</em>, 1998, p. 7</cite></footer> </blockquote> As definitions go, it's quite amphibolous. Is it the problem that generates negative consequences? Hardly. In the context, it's clear that it's the solution that causes problems. In any case, just because it's in a book doesn't necessarily make it right, but I find it a good start. </p> <p> I think that the phrase <em>decidedly negative consequences</em> is key. Most solutions come with <em>some</em> disadvantages, but in order for a 'solution' to be an anti-pattern, the disadvantages must clearly outweigh any advantages produced. </p> <p> I usually look at it another way. If I can solve the problem in a different way that generates at least as many advantages, but fewer disadvantages, then the first 'solution' might be an anti-pattern. This way of viewing the problem may stem from my background in economics. In that perspective, an anti-pattern simply isn't <a href="https://en.wikipedia.org/wiki/Pareto_efficiency">Pareto optimal</a>. </p> <h3 id="eb04af30ae5b40529fda1588525aee6d"> Falsifiability <a href="#eb04af30ae5b40529fda1588525aee6d" title="permalink">#</a> </h3> <p> Another rule of thumb I employ to determine whether a solution could be an anti-pattern is <a href="https://en.wikipedia.org/wiki/Karl_Popper">Popper</a>'s concept of <a href="https://en.wikipedia.org/wiki/Falsifiability">falsifiability</a>. As a continuation of the Pareto efficiency perspective, an anti-pattern is a 'solution' that you can improve without any (significant) trade-offs. </p> <p> That turns claims about anti-patterns into falsifiable statements, which I consider is the most intellectually honest way to go about claiming that things are bad. </p> <p> Take, for example, the claim that <em>Service Locator is an anti-pattern</em>. In light of Pareto efficiency, that's a falsifiable claim. All you have to do to prove me wrong is to present a situation where Service Locator solves a problem, and I can't come up with a better solution. </p> <p> I made the claim about Service Locator in 2010, and so far, no one has been able to present such a situation, even though several have tried. I'm fairly confident making that claim. </p> <p> This way of looking at the term anti-pattern, however, makes me wary of declaiming solutions anti-patterns just because I don't like them. Could there be a counter-argument, some niche scenario, where the pattern actually couldn't be improved without trade-offs? </p> <p> I didn't take it lightly when Steven suggested making Ambient Context an anti-pattern. </p> <h3 id="e027fe12038c4160a3be19bce065cd74"> Preliminary status <a href="#e027fe12038c4160a3be19bce065cd74" title="permalink">#</a> </h3> <p> I've had some time to think about Ambient Context since I had the (civil) discussion with Steven. The more I think about it, the more I think that he's right; that Ambient Context really <em>is</em> an anti-pattern. </p> <p> I never use that pattern myself, so it's clear to me that for all the situations that I typically encounter, there's always better solutions, with no significant trade-offs. </p> <p> The question is: could there be some niche scenario that I'm not aware of, where Ambient Context is a bona fide good solution? </p> <p> The more I think about this, the more I'm beginning to believe that there isn't. It remains to be seen, though. It remains to be falsified. </p> <h3 id="ed7478fa76324922a5543964e9c8c48e"> Summary <a href="#ed7478fa76324922a5543964e9c8c48e" title="permalink">#</a> </h3> <p> I'm so happy that Steven van Deursen agreed to co-author the second edition of <em>Dependency Injection in .NET</em> with me. The few areas where we've disagreed, I've ultimately come around to agree with him. He's truly taken a good book and made it better. </p> <p> One of the changes is that Ambient Context is now classified as an anti-pattern. Originally, I wasn't sure that this was the correct thing to do, but I've since changed my mind. I do think that Ambient Context belongs in the anti-patterns chapter. </p> <p> I could be wrong, though. I was before. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4493b077ecd84d6d8217d4944e11471a"> <div class="comment-author"><a href="https://rebus.fm">Mogens Heller Grabe</a> <a href="#4493b077ecd84d6d8217d4944e11471a">#</a></div> <div class="comment-content"> <p> Thanks for great input for discussion :P </p> <p> Like with all other patterns and anti-patterns, I think there's a time and a place. </p> <p> Simply looking at it in a one-dimensional manner, i.e. asking "does there exist a solution to this problem with the same advantages but less downsides?" must be qualified with "IN THIS TIME AND PLACE", in my opinion. </p> <p> This way, the patterns/anti-patterns distinction does not make that much sense in a global perspective, because all patterns can be an anti-patterns in some situations, and vice versa. </p> <p> For example, I like what <em>Ambient Context</em> does in <a href="https://github.com/rebus-org/Rebus">Rebus</a>: It provides a mechanism that enables user code to transparently enlist its bus operations in a unit of work, without requiring user code to pass that unit of work to each operation. </p> <p> This is very handy, e.g. in OWIN-based applications, where the unit of work can be managed by an OWIN middleware that uses a <a href="https://github.com/rebus-org/Rebus/wiki/Transactions#i-am-not-in-a-transaction---i-want-to-either-publish-a-message-or-send-several-messages"><code>RebusTransactionScope</code></a>, this way enlisting all send/publish operations on the bus in that unit of work. </p> <p> Had it not been possible to automatically pick up an ongoing ambient Rebus transaction context, one would probably need to pollute the interfaces of one's application with an <code>ITransactionContext</code> argument, thus not handling the cross-cutting concern of managing the unit of work in a cross-cutting manner. </p> </div> <div class="comment-date">2019-01-21 12:37 UTC</div> </div> <div class="comment" id="6d2c0b22097341acadc41e37ce4f1dc8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6d2c0b22097341acadc41e37ce4f1dc8">#</a></div> <div class="comment-content"> <p> Mogens, thank you for writing. The reason I explicitly framed my treatment in a discourse related to <a href="https://en.wikipedia.org/wiki/Pareto_efficiency">Pareto efficiency</a> is exactly because this view on optima is multi-dimensional. When considering whether a 'solution coordinate' is Pareto-optimal or not, the question is exactly whether or not it's possible to improve at least one dimension without exacerbating any other dimension. If you can make one dimension better <em>without trade-offs</em>, then you can make a Pareto improvement. If you can only make one dimension better at the cost of one or more other dimensions, then you already have a Pareto-optimal solution. </p> <p> The theory of Pareto efficiency doesn't say anything about the number of dimensions. Usually, as in the linked Wikipedia article, the concept is illustrated in the plane, but conceptually, it applies to an arbitrary number of dimensions. </p> <p> In the context of anti-patterns, those dimensions include time and place, as you say. </p> <p> I consider something to be an anti-pattern if I can make a change that constitutes an improvement in at least one dimension, without trading off of any other dimensions. In other words, in this article, I'm very deliberately not looking at it in a one-dimensional manner. </p> <p> As I wrote, I'm still not sure that Ambient Context is an anti-pattern (although I increasingly believe it to be). How can we even test that hypothesis when we can't really quantify software design? </p> <p> On the other hand, if we leave the question about Ambient Context for a moment, I feel confident that Service Locator is an anti-pattern, even in what you call a global perspective. The reason I believe that is that I made that falsifiable claim in 2010, and here, almost nine years later, no-one has successfully produced a valid counter-example. </p> <p> I don't have the same long history with the claim about Ambient Context, so I could be wrong. Perhaps you are, right now, proving me wrong. I can't tell, though, because I don't (yet) know enough about Rebus to be able to tell whether what you describe is Pareto-optimal. </p> <p> The question isn't whether the current design is 'handy'. The question is whether it's possible to come up with a design that's 'globally' better; i.e. either has all the advantages of the current design, but fewer disadvantages; or has more advantages, and only the same disadvantages. </p> <p> I may be able to suggest such an improvement if provided with some code examples, but in the end we may never agree whether one design is better than another. After all, since we can't quantify software design, a subjective judgement will always remain. </p> </div> <div class="comment-date">2019-01-24 8:00 UTC</div> </div> <div class="comment" id="2f2b71feaf034750bdddffdd66af1ab9"> <div class="comment-author"><a href="https://drew.from-wi.com/">Drew Douglas</a> <a href="#2f2b71feaf034750bdddffdd66af1ab9">#</a></div> <div class="comment-content"> <p> Mark, <br>Thanks for this thoughtful meta-analysis of what it means to be an anti-pattern and how we think about them. What are some considerations when designing a replacement for ambient context objects? What patterns have you found succesful in object-oriented code? I'm sure you'd mention <a href="https://blog.ploeh.dk/2011/03/03/InjectionConstructorsshouldbesimple/">Constructor Injection</a> and <a href="https://msdn.microsoft.com/en-us/magazine/gg983487.aspx">CQRS</a> with <a href="https://blog.ploeh.dk/2011/03/22/CommandsareComposable/">decorators</a>. </p> <p>How might one refactor ambient context out of a solution? Some say <a href="https://stackoverflow.com/questions/21050247/refactoring-out-singletons-globals-to-use-dependency-injection-for-unit-testing">passing around an object</a>, maybe some sort of <a href="https://en.wikipedia.org/wiki/Continuation-passing_style">continuation</a>?</p> </div> <div class="comment-date">2019-04-12 6:09 UTC</div> </div> <div class="comment" id="86b35a424def4985a24f4ff2c5e06ef1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#86b35a424def4985a24f4ff2c5e06ef1">#</a></div> <div class="comment-content"> <p> Drew, thank you for writing. Indeed, in object-oriented code, I'd typically replace Ambient Contexts with either injected dependencies or Decorators. I'm not sure I see how CQRS fits into this picture. </p> <p> When refactoring away an Ambient Context, it's typically, before the refactoring, used like <code>Context.DoSomething()</code>. The first step is often to inject a dependency and make it available to the class as a property called <code>Context</code>. IIRC, the C# overload resolution system should then pick the instance property over the static class, so that <code>Context.DoSomething()</code> calls <code>DoSomething</code> on the injected <code>Context</code> property. </p> <p> There may be some static member that also use the Ambient Context. If that's the case, you'll first have to make those static members instance members. </p> <p> Once you've made those replacements everywhere, you should be able to delete the Ambient Context. </p> </div> <div class="comment-date">2019-04-13 16:43 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An Either functor https://blog.ploeh.dk/2019/01/14/an-either-functor 2019-01-14T07:27:00+00:00 Mark Seemann <div id="post"> <p> <em>Either forms a normal functor. A placeholder article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. As another article explains, <a href="/2019/01/07/either-bifunctor">Either is a bifunctor</a>. This makes it trivially a functor. As such, this article is mostly a place-holder to fit the spot in the <em>functor table of contents</em>, thereby indicating that Either is a functor. </p> <p> Since Either is a bifunctor, it's actually not one, but two, functors. Many languages, C# included, are best equipped to deal with unambiguous functors. This is also true in <a href="https://haskell.org">Haskell</a>, where <code>Either l r</code> is only a <code>Functor</code> over the right side. Likewise, in C#, you can make <code>IEither&lt;L, R&gt;</code> a functor by implementing <code>Select</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectRight(selector); }</pre> </p> <p> This method simply delegates all implementation to the <code>SelectRight</code> method; it's just <code>SelectRight</code> by another name. It obeys the functor laws, since these are just specializations of the bifunctor laws, and we know that Either is a proper bifunctor. </p> <p> It would have been technically possible to instead implement a <code>Select</code> method by calling <code>SelectLeft</code>, but it seems generally more useful to enable syntactic sugar for mapping over 'happy path' scenarios. This enables you to write projections over operations that can fail. </p> <p> Here's some <em>C# Interactive</em> examples that use the <code>FindWinner</code> helper method from <a href="/2018/06/11/church-encoded-either">the Church-encoded Either article</a>. Imagine that you're collecting votes; you're trying to pick the highest-voted integer, but in reality, you're only interested in seeing if the number is positive or not. Since <code>FindWinner</code> returns <code>IEither&lt;VoteError, T&gt;</code>, and this type is a functor, you can project the right result, while any left result short-circuits the query. First, here's a successful query: </p> <p> <pre>&gt; <span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;FindWinner(1,&nbsp;2,&nbsp;-3,&nbsp;-1,&nbsp;2,&nbsp;-1,&nbsp;-1)&nbsp;<span style="color:blue;">select</span>&nbsp;i&nbsp;&gt;&nbsp;0 Right&lt;VoteError, bool&gt;(false)</pre> </p> <p> This query succeeds, resulting in a <code>Right</code> object. The contained value is <code>false</code> because the winner of the vote is <code>-1</code>, which isn't a positive number. </p> <p> On the other hand, the following query fails because of a tie. </p> <p> <pre>&gt; <span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;FindWinner(1,&nbsp;2,&nbsp;-3,&nbsp;-1,&nbsp;2,&nbsp;-1)&nbsp;<span style="color:blue;">select</span>&nbsp;i&nbsp;&gt;&nbsp;0 Left&lt;VoteError, bool&gt;(Tie)</pre> </p> <p> Because the result is tied on <code>-1</code>, the return value is a <code>Left</code> object containing the <code>VoteError</code> value <code>Tie</code>. </p> <p> Another source of error is an empty input collection: </p> <p> <pre>&gt; <span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;FindWinner&lt;<span style="color:blue;">int</span>&gt;()&nbsp;<span style="color:blue;">select</span>&nbsp;i&nbsp;&gt;&nbsp;0 Left&lt;VoteError, bool&gt;(Empty)</pre> </p> <p> This time, the <code>Left</code> object contains the <code>Empty</code> error value, since no winner can be found from an empty collection. </p> <p> While the <code>Select</code> method doesn't implement any behaviour that <code>SelectRight</code> doesn't already afford, it enables you to use C# query syntax, as demonstrated by the above examples. </p> <p> <strong>Next:</strong> <a href="/2018/08/06/a-tree-functor">A Tree functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Either bifunctor https://blog.ploeh.dk/2019/01/07/either-bifunctor 2019-01-07T09:13:00+00:00 Mark Seemann <div id="post"> <p> <em>Either forms a bifunctor. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/12/24/bifunctors">an article series about bifunctors</a>. As the overview article explains, essentially there's two practically useful bifunctors: pairs and <a href="/2018/06/11/church-encoded-either">Either</a>. In <a href="/2018/12/31/tuple-bifunctor">the previous article</a>, you saw how a pair (a two-tuple) forms a bifunctor. In this article, you'll see how Either also forms a bifunctor. </p> <h3 id="b30721a6a66a427eb1a7159d66290337"> Mapping both dimensions <a href="#b30721a6a66a427eb1a7159d66290337" title="permalink">#</a> </h3> <p> In the previous article, you saw how, if you have maps over both dimensions, you can trivially implement <code>SelectBoth</code> (what <a href="https://www.haskell.org">Haskell</a> calls <code>bimap</code>): </p> <p> <pre><span style="color:blue;">return</span>&nbsp;source.SelectFirst(selector1).SelectSecond(selector2);</pre> </p> <p> The relationship can, however, go both ways. If you implement <code>SelectBoth</code>, you can derive <code>SelectFirst</code> and <code>SelectSecond</code> from it. In this article, you'll see how to do that for Either. </p> <p> Given the <a href="/2018/06/11/church-encoded-either">Church-encoded Either</a>, the implementation of <code>SelectBoth</code> can be achieved in a single expression: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;SelectBoth&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;selectLeft, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;selectRight) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onLeft:&nbsp;&nbsp;l&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;(&nbsp;selectLeft(l)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onRight:&nbsp;r&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;(selectRight(r))); }</pre> </p> <p> Given that the input <code>source</code> is an <code>IEither&lt;L, R&gt;</code> object, there's isn't much you can do. That interface only defines a single member, <code>Match</code>, so that's the only method you can call. When you do that, you have to supply the two arguments <code>onLeft</code> and <code>onRight</code>. </p> <p> The <code>Match</code> method is defined like this: </p> <p> <pre><span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight)</pre> </p> <p> Given the desired return type of <code>SelectBoth</code>, you know that <code>T</code> should be <code>IEither&lt;L1, R1&gt;</code>. This means, then, that for <code>onLeft</code>, you must supply a function of the type <code>Func&lt;L, IEither&lt;L1, R1&gt;&gt;</code>. Since a functor is a structure-preserving map, you should translate a <em>left</em> case to a <em>left</em> case, and a <em>right</em> case to a <em>right</em> case. This implies that the concrete return type that matches <code>IEither&lt;L1, R1&gt;</code> for the <code>onLeft</code> argument is <code>Left&lt;L1, R1&gt;</code>. </p> <p> When you write the function with the type <code>Func&lt;L, IEither&lt;L1, R1&gt;&gt;</code> as a lambda expression, the input argument <code>l</code> has the type <code>L</code>. In order to create a <code>new Left&lt;L1, R1&gt;</code>, however, you need an <code>L1</code> object. How do you produce an <code>L1</code> object from an <code>L</code> object? You call <code>selectLeft</code> with <code>l</code>, because <code>selectLeft</code> is a function of the type <code>Func&lt;L, L1&gt;</code>. </p> <p> You can apply the same line of reasoning to the <code>onRight</code> argument. Write a lambda expression that takes an <code>R</code> object <code>r</code> as input, call <code>selectRight</code> to turn that into an <code>R1</code> object, and return it wrapped in a <code>new Right&lt;L1, R1&gt;</code> object. </p> <p> This works as expected: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>).SelectBoth(<span style="color:blue;">string</span>.IsNullOrWhiteSpace,&nbsp;i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i)) Left&lt;bool, DateTime&gt;(false) &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1337).SelectBoth(<span style="color:blue;">string</span>.IsNullOrWhiteSpace,&nbsp;i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i)) Right&lt;bool, DateTime&gt;([01.01.0001 00:00:00])</pre> </p> <p> Notice that both of the above statements evaluated in <em>C# Interactive</em> use the same projections as input to <code>SelectBoth</code>. Clearly, though, because the inputs are first a <code>Left</code> value, and secondly a <code>Right</code> value, the outputs differ. </p> <h3 id="10a641d7ad0b44158f74665fd87feb5e"> Mapping the left side <a href="#10a641d7ad0b44158f74665fd87feb5e" title="permalink">#</a> </h3> <p> When you have <code>SelectBoth</code>, you can trivially implement the translations for each dimension in isolation. In the previous article, I called these methods <code>SelectFirst</code> and <code>SelectSecond</code>. In this article, I've chosen to instead name them <code>SelectLeft</code> and <code>SelectRight</code>, but they still corresponds to Haskell's <code>first</code> and <code>second</code> <code>Bifunctor</code> functions. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;SelectLeft&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;source,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectBoth(selector,&nbsp;r&nbsp;=&gt;&nbsp;r); }</pre> </p> <p> The method body is literally a one-liner. Just call <code>SelectBoth</code> with <code>selector</code> as the projection for the left side, and the identity function as the projection for the right side. This ensures that if the actual value is a <code>Right&lt;L, R&gt;</code> object, nothing's going to happen. Only if the input is a <code>Left&lt;L, R&gt;</code> object will the projection run: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;&quot;</span>).SelectLeft(<span style="color:blue;">string</span>.IsNullOrWhiteSpace) Left&lt;bool, int&gt;(true) &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>).SelectLeft(<span style="color:blue;">string</span>.IsNullOrWhiteSpace) Left&lt;bool, int&gt;(false) &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42).SelectLeft(<span style="color:blue;">string</span>.IsNullOrWhiteSpace) Right&lt;bool, int&gt;(42)</pre> </p> <p> In the above <em>C# Interactive</em> session, you can see how projecting three different objects using <code>string.IsNullOrWhiteSpace</code> works. When the <code>Left</code> object indeed does contain an empty string, the result is a <code>Left</code> value containing <code>true</code>. When the object contains <code>"bar"</code>, however, it contains <code>false</code>. Furthermore, when the object is a <code>Right</code> value, the mapping has no effect. </p> <h3 id="b9027a241d6444d5b869a8a1b92659c5"> Mapping the right side <a href="#b9027a241d6444d5b869a8a1b92659c5" title="permalink">#</a> </h3> <p> Similar to <code>SelectLeft</code>, you can also trivially implement <code>SelectRight</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;SelectRight&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;source,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectBoth(l&nbsp;=&gt;&nbsp;l,&nbsp;selector); }</pre> </p> <p> This is another one-liner calling <code>SelectBoth</code>, with the difference that the identity function <code>l =&gt; l</code> is passed as the first argument, instead of as the last. This ensures that only <code>Right</code> values are mapped: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;baz&quot;</span>).SelectRight(i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i)) Left&lt;string, DateTime&gt;("baz") &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1_234_567_890).SelectRight(i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i)) Right&lt;string, DateTime&gt;([01.01.0001 00:02:03])</pre> </p> <p> In the above examples, <code>Right</code> integers are projected into <code>DateTime</code> values, whereas <code>Left</code> strings stay strings. </p> <h3 id="a80a8d7ebbce46b4914749f46cd99e89"> Identity laws <a href="#a80a8d7ebbce46b4914749f46cd99e89" title="permalink">#</a> </h3> <p> Either obeys all the bifunctor laws. While it's formal work to prove that this is the case, you can get an intuition for it via examples. Often, I use a property-based testing library like <a href="https://fscheck.github.io/FsCheck">FsCheck</a> or <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> to demonstrate (not prove) that laws hold, but in this article, I'll keep it simple and only cover each law with a parametrised test. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Id&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;x)&nbsp;=&gt;&nbsp;x; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">object</span>[]&gt;&nbsp;BifunctorLawsData { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;baz&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(&nbsp;&nbsp;&nbsp;42)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(&nbsp;1337)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(&nbsp;&nbsp;&nbsp;&nbsp;0)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectLeftObeysFirstFunctorLaw(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e,&nbsp;e.SelectLeft(Id)); }</pre> </p> <p> This test uses <a href="https://xunit.net">xUnit.net</a>'s <code>[Theory]</code> feature to supply a small set of example input values. The input values are defined by the <code>BifunctorLawsData</code> property, since I'll reuse the same values for all the bifunctor law demonstration tests. </p> <p> The tests also use the identity function implemented as a <code>private</code> function called <code>Id</code>, since C# doesn't come equipped with such a function in the Base Class Library. </p> <p> For all the <code>IEither&lt;string, int&gt;</code> objects <code>e</code>, the test simply verifies that the original Either <code>e</code> is equal to the Either projected over the first axis with the <code>Id</code> function. </p> <p> Likewise, the first functor law applies when translating over the second dimension: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectRightObeysFirstFunctorLaw(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e,&nbsp;e.SelectRight(Id)); }</pre> </p> <p> This is the same test as the previous test, with the only exception that it calls <code>SelectRight</code> instead of <code>SelectLeft</code>. </p> <p> Both <code>SelectLeft</code> and <code>SelectRight</code> are implemented by <code>SelectBoth</code>, so the real test is whether this method obeys the identity law: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothObeysIdentityLaw(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e,&nbsp;e.SelectBoth(Id,&nbsp;Id)); }</pre> </p> <p> Projecting over both dimensions with the identity function does, indeed, return an object equal to the input object. </p> <h3 id="5ee147f408d64bc78171ade1f968b9aa"> Consistency law <a href="#5ee147f408d64bc78171ade1f968b9aa" title="permalink">#</a> </h3> <p> In general, it shouldn't matter whether you map with <code>SelectBoth</code> or a combination of <code>SelectLeft</code> and <code>SelectRight</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConsistencyLawHolds(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.IsNullOrWhiteSpace(s); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e.SelectBoth(f,&nbsp;g),&nbsp;e.SelectRight(g).SelectLeft(f)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.SelectLeft(f).SelectRight(g), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.SelectRight(g).SelectLeft(f)); }</pre> </p> <p> This example creates two local functions <code>f</code> and <code>g</code>. The first function, <code>f</code>, just delegates to <code>string.IsNullOrWhiteSpace</code>, although I want to stress that this is just an example. The law should hold for any two (<a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>) functions. The second function, <code>g</code>, creates a new <code>DateTime</code> object from an integer, using one of the <code>DateTime</code> constructor overloads. </p> <p> The test then verifies that you get the same result from calling <code>SelectBoth</code> as when you call <code>SelectLeft</code> followed by <code>SelectRight</code>, or the other way around. </p> <h3 id="509bf515dac14e0e95346ee604ba26af"> Composition laws <a href="#509bf515dac14e0e95346ee604ba26af" title="permalink">#</a> </h3> <p> The composition laws insist that you can compose functions, or translations, and that again, the choice to do one or the other doesn't matter. Along each of the axes, it's just the second functor law applied. This parametrised test demonstrates that the law holds for <code>SelectLeft</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectLeft(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;g(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e.SelectLeft(x&nbsp;=&gt;&nbsp;f(g(x))),&nbsp;e.SelectLeft(g).SelectLeft(f)); }</pre> </p> <p> Here, <code>f</code> is the <em>even</em> function, whereas <code>g</code> is a local function that returns the length of a string. The second functor law states that mapping <code>f(g(x))</code> in a single step is equivalent to first mapping over <code>g</code> and then map the result of that using <code>f</code>. </p> <p> The same law applies if you fix the first dimension and translate over the second: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectRight(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">char</span>&nbsp;f(<span style="color:blue;">bool</span>&nbsp;b)&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e.SelectRight(x&nbsp;=&gt;&nbsp;f(g(x))),&nbsp;e.SelectRight(g).SelectRight(f)); }</pre> </p> <p> Here, <code>f</code> is a local function that returns <code>'T'</code> for <code>true</code> and <code>'F'</code> for <code>false</code>, and <code>g</code> is a local function that, as you've seen before, determines whether a number is even. Again, the test demonstrates that the output is the same whether you map over an intermediary step, or whether you map using only a single step. </p> <p> This generalises to the composition law for <code>SelectBoth</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothCompositionLawHolds(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;g(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">char</span>&nbsp;h(<span style="color:blue;">bool</span>&nbsp;b)&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;i(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.SelectBoth(x&nbsp;=&gt;&nbsp;f(g(x)),&nbsp;y&nbsp;=&gt;&nbsp;h(i(y))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.SelectBoth(g,&nbsp;i).SelectBoth(f,&nbsp;h)); }</pre> </p> <p> Again, whether you translate in one or two steps shouldn't affect the outcome. </p> <p> As all of these tests demonstrate, the bifunctor laws hold for Either. The tests only showcase six examples for either a string or an integer, but I hope it gives you an intuition how any Either object is a bifunctor. After all, the <code>SelectLeft</code>, <code>SelectRight</code>, and <code>SelectBoth</code> methods are all generic, and they behave the same for all generic type arguments. </p> <h3 id="7b8babee4af64eeaa93fe4562ed721bd"> Summary <a href="#7b8babee4af64eeaa93fe4562ed721bd" title="permalink">#</a> </h3> <p> Either objects are bifunctors. You can translate the first and second dimension of an Either object independently of each other, and the bifunctor laws hold for any pure translation, no matter how you compose the projections. </p> <p> As always, there can be performance differences between the various compositions, but the outputs will be the same regardless of composition. </p> <p> A functor, and by extension, a bifunctor, is a structure-preserving map. This means that any projection preserves the structure of the underlying container. For Either objects, it means that <em>left</em> objects remain <em>left</em> objects, and <em>right</em> objects remain <em>right</em> objects, even if the contained values change. Either is characterised by containing exactly one value, but it can be either a <em>left</em> value or a <em>right</em> value. No matter how you translate it, it still contains only a single value - <em>left</em> or <em>right</em>. </p> <p> The other common bifunctor, <em>pair</em>, is complementary. Not only does it also have two dimensions, but a pair always contains both values at once. </p> <p> <strong>Next:</strong> <a href="/2019/08/12/rose-tree-bifunctor">Rose tree bifunctor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="79f5d74763e34cb0997a7a79df1e05f0"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#79f5d74763e34cb0997a7a79df1e05f0">#</a></div> <div class="comment-content"> <p> I feel like the concepts of functor and bifunctor were used somewhat interchangeably in this post. Can we clarify this relationship? </p> <p> To help us with this, consider <a href="https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)">type variance</a>. The generic type <span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">A</span>&gt; is covariant, but more specifically, it is covariant on <span style="color:#2b91af;">A</span>. That additional prepositional phrase is often omitted because it can be inferred. In contrast, the generic type <span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">A</span>, <span style="color:#2b91af;">B</span>&gt; is both covariant and contravariant but (of course) not on the same type parameter. It is covariant in <span style="color:#2b91af;">B</span> and contravariant in <span style="color:#2b91af;">A</span>. </p> <p> I feel like saying that a generic type with two type parameters is a (bi)functor also needs an additional prepositional phrase. Like, <span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>, <span style="color:#2b91af;">R</span>&gt; is a bifunctor in <span style="color:#2b91af;">L</span> and <span style="color:#2b91af;">R</span>, so it is also a functor in <span style="color:#2b91af;">L</span> and a functor in <span style="color:#2b91af;">R</span>. </p> <p> Does this seem like a clearer way to talk about a specific type being both a bifunctor and a fuctor? </p> </div> <div class="comment-date">2019-05-15 11:51 UTC</div> </div> <div class="comment" id="19253916a1bd49a08b19b15898816657"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#19253916a1bd49a08b19b15898816657">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I find no fault with what you wrote. Is it clearer? I don't know. </p> <p> One thing that's surprised me throughout <a href="/2017/10/04/from-design-patterns-to-category-theory">this endeavour</a> is exactly what does or doesn't confuse readers. This, I can't predict. </p> <p> A functor is, by its definition, assumed to be covariant. Contravariant functors also exist, but they're explicitly named <em>contravariant functors</em> to distinguish them from standard functors. </p> <p> Ultimately, co- or contravariance of generic type arguments is (I think) insufficient to identify a type as a functor. Whether or not something is a functor is determined by whether or not it obeys the functor laws. Can we guarantee that all types with a covariant type argument will obey the functor laws? </p> </div> <div class="comment-date">2019-05-15 12:31 UTC</div> </div> <div class="comment" id="a7a894fc72824c639bf571ddffc6551d"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#a7a894fc72824c639bf571ddffc6551d">#</a></div> <div class="comment-content"> <p> I wasn't trying to discuss the relationship between functors and type variance. I just brought up type variance as an example in programming where I think adding additional prepositional phrases to statements can clarify things. </p> </div> <div class="comment-date">2019-05-30 05:12 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tuple bifunctor https://blog.ploeh.dk/2018/12/31/tuple-bifunctor 2018-12-31T12:13:00+00:00 Mark Seemann <div id="post"> <p> <em>A Pair (a two-tuple) forms a bifunctor. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/12/24/bifunctors">an article series about bifunctors</a>. In the previous overview, you learned about the general concept of a bifunctor. In practice, there's two useful bifunctor instances: pairs (two-tuples) and <a href="/2018/06/11/church-encoded-either">Either</a>. In this article, you'll see how a pair is a bifunctor, and in the next article, you'll see how Either fits the same abstraction. </p> <h3 id="d918d0271c33406ba3047ef162212100"> Tuple as a functor <a href="#d918d0271c33406ba3047ef162212100" title="permalink">#</a> </h3> <p> You can treat a normal pair (two-tuple) as a <a href="/2018/03/22/functors">functor</a> by mapping one of the elements, while keeping the other generic type fixed. In <a href="https://www.haskell.org">Haskell</a>, when you have types with multiple type arguments, you often 'fix' the types from the left, leaving the right-most type free to vary. Doing this for a pair, which in C# has the type <code>Tuple&lt;T, U&gt;</code>, this means that tuples are functors if we keep <code>T</code> fixed and enable translation of the second element from <code>U1</code> to <code>U2</code>. </p> <p> This is easy to implement with a standard <code>Select</code> extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U1</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(source.Item1,&nbsp;selector(source.Item2)); }</pre> </p> <p> You simply return a new tuple by carrying <code>source.Item1</code> over without modification, while on the other hand calling <code>selector</code> with <code>source.Item2</code>. Here's a simple example, which also highlights that C# understands functors: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0;</pre> </p> <p> Here, <code>actual</code> is a <code>Tuple&lt;string, bool&gt;</code> with the values <code>"foo"</code> and <code>true</code>. Inside the query expression, <code>i</code> is an <code>int</code>, and the <code>select</code> expression returns a <code>bool</code> value indicating whether the number is even or odd. Notice that the <code>string</code> in the first element disappears inside the query expression. It's still there, but the code inside the query expression can't see <code>"foo"</code>. </p> <h3 id="589f054d8dab47a4bb308951ee76f96b"> Mapping the first element <a href="#589f054d8dab47a4bb308951ee76f96b" title="permalink">#</a> </h3> <p> There's no technical reason why the mapping has to be over the second element; it's just how Haskell does it by convention. There are other, more philosophical reasons for that convention, but in the end, they boil down to the ultimately arbitrary cultural choice of reading from left to right (in Western scripts). </p> <p> You can translate the first element of a tuple as easily: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">U</span>&gt;&nbsp;SelectFirst&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">U</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">U</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(selector(source.Item1),&nbsp;source.Item2); }</pre> </p> <p> While, technically, you <em>can</em> call this method <code>Select</code>, this can confuse the C# compiler's overload resolution system - at least if you have a tuple of two identical types (e.g. <code>Tuple&lt;int, int&gt;</code> or <code>Tuple&lt;string, string&gt;</code>). In order to avoid that sort of confusion, I decided to give the method another name, and in keeping with how C# LINQ methods tend to get names, I thought <code>SelectFirst</code> sounded reasonable. </p> <p> In Haskell, this function is called <code>first</code>, and is part of the <code>Bifunctor</code> type class: </p> <p> <pre>Prelude Data.Bifunctor&gt; first (even . length) ("foo", 42) (False,42)</pre> </p> <p> In C#, you can perform the same translation using the above <code>SelectFirst</code> extension method: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;t.SelectFirst(s&nbsp;=&gt;&nbsp;s.Length&nbsp;%&nbsp;2&nbsp;==&nbsp;0);</pre> </p> <p> This also returns a <code>Tuple&lt;bool, int&gt;</code> containing the values <code>false</code> and <code>42</code>. Notice that in this case, the first element <code>"foo"</code> is translated into <code>false</code> (because its length is odd), while the second element <code>42</code> carries over unchanged. </p> <h3 id="7da2d817bebb40cbb2221212158324f8"> Mapping the second element <a href="#7da2d817bebb40cbb2221212158324f8" title="permalink">#</a> </h3> <p> You've already seen how the above <code>Select</code> method maps over the second element of a pair. This means that you can already map over both dimensions of the bifunctor, but perhaps, for consistency's sake, you'd also like to add an explicit <code>SelectSecond</code> method. This is now trivial to implement, since it can delegate its work to <code>Select</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;SelectSecond&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U1</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Select(selector); }</pre> </p> <p> There's no separate implementation; the only thing this method does is to delegate work to the <code>Select</code> method. It's literally the <code>Select</code> method, just with another name. </p> <p> Clearly, you could also have done it the other way around: implement <code>SelectSecond</code> and then call it from <code>Select</code>. </p> <p> The <code>SelectSecond</code> method works as you'd expect: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;1337); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;t.SelectSecond(i&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0);</pre> </p> <p> Again, <code>actual</code> is a tuple containing the values <code>"foo"</code> and <code>false</code>, because <code>1337</code> isn't even. </p> <p> This fits with the Haskell implementation, where <code>SelectSecond</code> is called <code>second</code>: </p> <p> <pre>Prelude Data.Bifunctor&gt; second even ("foo", 1337) ("foo",False)</pre> </p> <p> The result is still a pair where the first element is <code>"foo"</code> and the second element <code>False</code>, exactly like in the C# example. </p> <h3 id="17ce6b48946b4877924ddc5e3f841283"> Mapping both elements <a href="#17ce6b48946b4877924ddc5e3f841283" title="permalink">#</a> </h3> <p> With <code>SelectFirst</code> and <code>SelectSecond</code>, you can trivially implement <code>SelectBoth</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;SelectBoth&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">U1</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;&nbsp;selector1, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;selector2) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectFirst(selector1).SelectSecond(selector2); }</pre> </p> <p> This method takes two translations, <code>selector1</code> and <code>selector2</code>, and first uses <code>SelectFirst</code> to project along the first axis, and then <code>SelectSecond</code> to map the second dimension. </p> <p> This implementation creates an intermediary pair that callers never see, so this could theoretically be inefficient. In this article, however, I want to show you that it's possible to implement <code>SelectBoth</code> based on <code>SelectFirst</code> and <code>SelectSecond</code>. In the next article, you'll see how to do it the other way around. </p> <p> Using <code>SelectBoth</code> is easy: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;t.SelectBoth(s&nbsp;=&gt;&nbsp;s.First(),&nbsp;i&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0);</pre> </p> <p> This translation returns a pair where the first element is <code>'f'</code> and the second element is <code>true</code>. This is because the first lambda expression <code>s =&gt; s.First()</code> returns the first element of the input string <code>"foo"</code>, whereas the second lambda expression <code>i =&gt; i % 2 == 0</code> determines that <code>42</code> is even. </p> <p> In Haskell, <code>SelectBoth</code> is called <code>bimap</code>: </p> <p> <pre>Prelude Data.Bifunctor&gt; bimap head even ("foo", 42) ('f',True)</pre> </p> <p> The return value is consistent with the C# example, since the input is also equivalent. </p> <h3 id="93cde34382844098885a14a06aa03948"> Identity laws <a href="#93cde34382844098885a14a06aa03948" title="permalink">#</a> </h3> <p> Pairs obey all the bifunctor laws. While it's formal work to prove that this is the case, you can get an intuition for it via examples. Often, I use a property-based testing library like <a href="https://fscheck.github.io/FsCheck">FsCheck</a> or <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> to demonstrate (not prove) that laws hold, but in this article, I'll keep it simple and only cover each law with a parametrised test. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Id&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;x)&nbsp;=&gt;&nbsp;x; [<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectFirstObeysFirstFunctorLaw(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectFirst(Id)); }</pre> </p> <p> This test uses <a href="https://xunit.net">xUnit.net</a>'s <code>[Theory]</code> feature to supply a small set of example input values. It defines the identity function as a <code>private</code> function called <code>Id</code>, since C# doesn't come equipped with such a function in the Base Class Library. </p> <p> The test simply creates a tuple with the input values and verifies that the original tuple <code>t</code> is equal to the tuple projected over the first axis with the <code>Id</code> function. </p> <p> Likewise, the first functor law applies when translating over the second dimension: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectSecondObeysFirstFunctorLaw(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectSecond(Id)); }</pre> </p> <p> This is the same test as the previous test, with the only exception that it calls <code>SelectSecond</code> instead of <code>SelectFirst</code>. </p> <p> Since <code>SelectBoth</code> in this example is implemented by composing <code>SelectFirst</code> and <code>SelectSecond</code>, you should expect it to obey the general identity law for bifunctors. It does, but it's always nice to see it with your own eyes: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothObeysIdentityLaw(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectBoth(Id,&nbsp;Id)); }</pre> </p> <p> Here you can see that projecting over both dimensions with the identity function returns the original tuple. </p> <h3 id="6bd839540b9646db9acfe25b0227b241"> Consistency law <a href="#6bd839540b9646db9acfe25b0227b241" title="permalink">#</a> </h3> <p> In general, it shouldn't matter whether you map with <code>SelectBoth</code> or a combination of <code>SelectFirst</code> and <code>SelectSecond</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConsistencyLawHolds(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">string</span>.IsNullOrWhiteSpace; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;g&nbsp;=&nbsp;i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectBoth(f,&nbsp;g),&nbsp;t.SelectSecond(g).SelectFirst(f)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectFirst(f).SelectSecond(g), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectSecond(g).SelectFirst(f)); }</pre> </p> <p> This example creates two functions <code>f</code> and <code>g</code>. The first function, <code>f</code>, is just an alias for <code>string.IsNullOrWhiteSpace</code>, although I want to stress that it's just an example. The law should hold for any two (<a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>) functions. The second function, <code>g</code>, creates a new <code>DateTime</code> object from an integer, using one of the <code>DateTime</code> constructor overloads. </p> <p> The test then verifies that you get the same result from calling <code>SelectBoth</code> as when you call <code>SelectFirst</code> followed by <code>SelectSecond</code>, or the other way around. </p> <h3 id="866a68ce5f3e4513882409498e743bb3"> Composition laws <a href="#866a68ce5f3e4513882409498e743bb3" title="permalink">#</a> </h3> <p> The composition laws insist that you can compose functions, or translations, and that again, the choice to do one or the other doesn't matter. Along each of the axes, it's just the second functor law applied. You've already seen that <code>SelectSecond</code> is nothing but an alias for <code>Select</code>, so surely, the second functor law must hold for <code>SelectSecond</code> as well. This parametrised test demonstrates that it does: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectSecond(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;f&nbsp;=&nbsp;b&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;g&nbsp;=&nbsp;i&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectSecond(x&nbsp;=&gt;&nbsp;f(g(x))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectSecond(g).SelectSecond(f)); }</pre> </p> <p> Here, <code>f</code> is a function that returns <code>'T'</code> for <code>true</code> and <code>'F'</code> for <code>false</code>, and <code>g</code> is a function that, as you've seen before, determines whether a number is even. The second functor law states that mapping <code>f(g(x))</code> in a single step is equivalent to first mapping over <code>g</code> and then map the result of that using <code>f</code>. </p> <p> The same law applies if you fix the second dimension and translate over the first: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectFirst(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;g&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectFirst(x&nbsp;=&gt;&nbsp;f(g(x))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectFirst(g).SelectFirst(f)); }</pre> </p> <p> Here, <code>f</code> is the <em>even</em> function, whereas <code>g</code> is a function that returns the length of a string. Again, the test demonstrates that the output is the same whether you map over an intermediary step, or whether you map using only a single step. </p> <p> This generalises to the composition law for <code>SelectBoth</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothCompositionLawHolds(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;g&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;h&nbsp;=&nbsp;b&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;i&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectBoth(x&nbsp;=&gt;&nbsp;f(g(x)),&nbsp;y&nbsp;=&gt;&nbsp;h(i(y))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectBoth(g,&nbsp;i).SelectBoth(f,&nbsp;h)); }</pre> </p> <p> Again, whether you translate in one or two steps shouldn't affect the outcome. </p> <p> As all of these tests demonstrate, the bifunctor laws hold for pairs. The tests only showcase 4-5 examples for a pair of string and integer, but I hope it gives you an intuition how any pair is a bifunctor. After all, the <code>SelectFirst</code>, <code>SelectSecond</code>, and <code>SelectBoth</code> methods are all generic, and they behave the same for all generic type arguments. </p> <h3 id="dfd4aa6edc66462c9d02d46ce44b1bda"> Summary <a href="#dfd4aa6edc66462c9d02d46ce44b1bda" title="permalink">#</a> </h3> <p> Pairs (two-tuples) are bifunctors. You can translate the first and second element of a pair independently of each other, and the bifunctor laws hold for any pure translation, no matter how you compose the projections. </p> <p> As always, there can be performance differences between the various compositions, but the outputs will be the same regardless of composition. </p> <p> A functor, and by extension, a bifunctor, is a structure-preserving map. This means that any projection preserves the structure of the underlying container. In practice that means that for pairs, no matter how you translate a pair, it remains a pair. A pair is characterised by containing two values at once, and no matter how you translate it, it'll still contain two values. </p> <p> The other common bifunctor, Either, is complementary. While it has two dimensions, it only contains one value, which is of either the one type or the other. It's still a bifunctor, though, because mappings preserve the structure of Either, too. </p> <p> <strong>Next:</strong> <a href="/2019/01/07/either-bifunctor">Either bifunctor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Bifunctors https://blog.ploeh.dk/2018/12/24/bifunctors 2018-12-24T14:40:00+00:00 Mark Seemann <div id="post"> <p> <em>Bifunctors are like functors, only they vary in two dimensions instead of one. An article for object-oriented programmers.</em> </p> <p> This article is a continuation of <a href="/2018/03/22/functors">the article series about functors</a> and about <a href="/2018/10/01/applicative-functors">applicative functors</a>. In this article, you'll learn about a generalisation called a <em>bifunctor</em>. The prefix <em>bi</em> typically indicates that there's <em>two</em> of something, and that's also the case here. </p> <p> As you've already seen in the functor articles, a functor is a mappable <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> of generic values, like <code>Foo&lt;T&gt;</code>, where the type of the contained value(s) can be any generic type <code>T</code>. A bifunctor is just a container with two independent generic types, like <code>Bar&lt;T, U&gt;</code>. If you can map each of the types independently of the other, you may have a bifunctor. </p> <p> The two most common bifunctors are tuples and Either. </p> <h3 id="c8062c28a0ed471a8995bee180d14aa6"> Maps <a href="#c8062c28a0ed471a8995bee180d14aa6" title="permalink">#</a> </h3> <p> A normal functor is based on a structure-preserving map of the contents within a container. You can, for example, translate an <code>IEnumerable&lt;int&gt;</code> to an <code>IEnumerable&lt;string&gt;</code>, or a <code>Maybe&lt;DateTime&gt;</code> to a <code>Maybe&lt;bool&gt;</code>. The axis of variability is the generic type argument <code>T</code>. You can translate <code>T1</code> to <code>T2</code> inside a container, but the type of the container remains the same: you can translate <code>Tree&lt;T1&gt;</code> to <code>Tree&lt;T2&gt;</code>, but it remains a <code>Tree&lt;&gt;</code>. </p> <p> <img src="/content/binary/monofunctors.png" alt="Three translations: IEnumerable, Maybe, and Tree."> </p> <p> A bifunctor involves a pair of maps, one for each generic type. You can map a <code>Bar&lt;string, int&gt;</code> to a <code>Bar&lt;bool, int&gt;</code>, or to a <code>Bar&lt;string, DateTime&gt;</code>, or even to a <code>Bar&lt;bool, DateTime&gt;</code>. Notice that the last example, mapping from <code>Bar&lt;string, int&gt;</code> to <code>Bar&lt;bool, DateTime&gt;</code> could be viewed as translating both axes simultaneously. </p> <p> <img src="/content/binary/bifunctor.png" alt="Bifunctor diagram."> </p> <p> In <a href="https://www.haskell.org">Haskell</a>, the two maps are called <code>first</code> and <code>second</code>, while the 'simultaneous' map is called <code>bimap</code>. </p> <p> The <code>first</code> translation translates the first, or left-most, value in the container. You can use it to map <code>Bar&lt;string, int&gt;</code> to a <code>Bar&lt;bool, int&gt;</code>. In C#, we could decide to call the method <code>SelectFirst</code>, or <code>SelectLeft</code>, in order to align with the C# naming convention of calling the functor morphism <code>Select</code>. </p> <p> Likewise, the <code>second</code> map translates the second, or right-most, value in the container. This is where you map <code>Bar&lt;string, int&gt;</code> to <code>Bar&lt;string, DateTime&gt;</code>. In C#, we could call the method <code>SelectSecond</code>, or <code>SelectRight</code>. </p> <p> The <code>bimap</code> function maps both values in the container in one go. This corresponds to a translation from <code>Bar&lt;string, int&gt;</code> to <code>Bar&lt;bool, DateTime&gt;</code>. In C#, we could call the method <code>SelectBoth</code>. There's no established naming conventions for bifunctors in C# that I know of, so these names are just some that I made up. </p> <p> You'll see examples of how to implement and use such functions in the next articles: <ul> <li><a href="/2018/12/31/tuple-bifunctor">Tuple bifunctor</a></li> <li><a href="/2019/01/07/either-bifunctor">Either bifunctor</a></li> <li><a href="/2019/08/12/rose-tree-bifunctor">Rose tree bifunctor</a></li> </ul> Other bifunctors exist, but the first two are the most common. </p> <h3 id="838097e59afd4555b989594b62082f39"> Identity laws <a href="#838097e59afd4555b989594b62082f39" title="permalink">#</a> </h3> <p> As is the case with functors, laws govern bifunctors. Some of the functor laws carry over, but are simply repeated over both axes, while other laws are generalisations of the functor laws. For example, the first functor law states that if you translate a container with the identity function, the result is the original input. This generalises to bifunctors as well: </p> <p> <pre>bimap id id ≡ id</pre> </p> <p> This just states that if you translate both axes using the <a href="/2017/11/13/endomorphism-monoid">endomorphic</a> Identity, it's equivalent to applying the Identity. </p> <p> <img src="/content/binary/bimap-id-id-bifunctor-law.png" alt="The bifunctor law for applying id to both axes simultaneously."> </p> <p> Using C# syntax, you could express the law like this: </p> <p> <pre>bf.SelectBoth(id,&nbsp;id)&nbsp;==&nbsp;bf;</pre> </p> <p> Here, <code>bf</code> is some bifunctor, and <code>id</code> is the identity function. The point is that if you translate over both axes, but actually don't perform a real translation, nothing happens. </p> <p> Likewise, if you consider a bifunctor a functor over two dimensions, the first functor law should hold for both: </p> <p> <pre>first id ≡ id second id ≡ id</pre> </p> <p> Both of those equalities only restate the first functor law for each dimension. If you map an axis with the identity function, nothing happens: </p> <p> <img src="/content/binary/first-id-second-id-bifunctor-laws.png" alt="The first functor law applied to both dimensions of a bifunctor."> </p> <p> In C#, you can express both laws like this: </p> <p> <pre>bf.SelectFirst(id)&nbsp;==&nbsp;bf; bf.SelectSecond(id)&nbsp;==&nbsp;bf;</pre> </p> <p> When calling <code>SelectFirst</code>, you translate only the first axis while you keep the second axis constant. When calling <code>SelectSecond</code> it's the other way around: you translate only the second axis while keeping the first axis constant. In both cases, though, if you use the identity function for the translation, you effectively keep the mapped dimension constant as well. Therefore, one would expect the result to be the same as the input. </p> <h3 id="57cfa8fd78084e8ca19d073d856a8c8a"> Consistency law <a href="#57cfa8fd78084e8ca19d073d856a8c8a" title="permalink">#</a> </h3> <p> As you'll see in the articles on tuple and Either bifunctors, you can derive <code>bimap</code> or <code>SelectBoth</code> from <code>first</code>/<code>SelectFirst</code> and <code>second</code>/<code>SelectSecond</code>, or the other way around. If, however, you decide to implement all three functions, they must act in a consistent manner. The name <em>Consistency law</em>, however, is entirely my own invention. If it has a more well-known name, I'm not aware of it. </p> <p> In pseudo-Haskell syntax, you can express the law like this: </p> <p> <pre>bimap f g ≡ first f . second g</pre> </p> <p> This states that mapping (using the functions <code>f</code> and <code>g</code>) simultaneously should produce the same result as mapping using an intermediary step: </p> <p> <img src="/content/binary/bifunctor-consistency-law.png" alt="The bifunctor Consistency law."> </p> <p> In C#, you could express it like this: </p> <p> <pre>bf.SelectBoth(f,&nbsp;g)&nbsp;==&nbsp;bf.SelectSecond(g).SelectFirst(f);</pre> </p> <p> You can project the input bifunctor <code>bf</code> using both <code>f</code> and <code>g</code> in a single step, or you can first translate the second dimension with <code>g</code> and then subsequently map that intermediary result along the first axis with <code>f</code>. </p> <p> The above diagram ought to commute: </p> <p> <img src="/content/binary/bifunctor-consistency-commutativity-law.png" alt="The bifunctor Consistency law."> </p> <p> It shouldn't matter whether the intermediary step is applying <code>f</code> along the first axis or <code>g</code> along the second axis. In C#, we can write it like this: </p> <p> <pre>bf.SelectFirst(f).SelectSecond(g)&nbsp;==&nbsp;bf.SelectSecond(g).SelectFirst(f);</pre> </p> <p> On the left-hand side, you first translate the bifunctor <code>bf</code> along the first axis, using <code>f</code>, and then translate that intermediary result along the second axis, using <code>g</code>. On the right-hand side, you first project <code>bf</code> along the second axis, using <code>g</code>, and then map that intermediary result over the first dimension, using <code>f</code>. </p> <p> Regardless of order of translation, the result should be the same. </p> <h3 id="a1d12a9d1afe428684b3d693a39cb5d1"> Composition laws <a href="#a1d12a9d1afe428684b3d693a39cb5d1" title="permalink">#</a> </h3> <p> Similar to how the first functor law generalises to bifunctors, the second functor law generalises as well. For (mono)functors, the second functor law states that if you have two functions over the same dimension, it shouldn't matter whether you perform a projection in one, composed step, or in two steps with an intermediary result. </p> <p> For bifunctors, you can generalise that law and state that you can project over both dimensions in one or two steps: </p> <p> <pre>bimap (f . g) (h . i) ≡ bimap f h . bimap g i</pre> </p> <p> If you have two functions, <code>f</code> and <code>g</code>, that compose, and two other functions, <code>h</code> and <code>i</code>, that also compose, you can translate in either one or two steps; the result should be the same. </p> <p> <img src="/content/binary/bimap-composition-law.png" alt="The bifunctor composition law."> </p> <p> In C#, you can express the law like this: </p> <p> <pre>bf.SelectBoth(x&nbsp;=&gt;&nbsp;f(g(x)),&nbsp;y&nbsp;=&gt;&nbsp;h(i(y)))&nbsp;==&nbsp;bf.SelectBoth(g,&nbsp;i).SelectBoth(f,&nbsp;h);</pre> </p> <p> On the left-hand side, the first dimension is translated in one step. For each <code>x</code> contained in <code>bf</code>, the translation first invokes <code>g(x)</code>, and then immediately calls <code>f</code> with the output of <code>g(x)</code>. The second dimension also gets translated in one step. For each <code>y</code> contained in <code>bf</code>, the translation first invokes <code>i(y)</code>, and then immediately calls <code>h</code> with the output of <code>i(y)</code>. </p> <p> On the right-hand side, you first translate <code>bf</code> along both axes using <code>g</code> and <code>i</code>. This produces an intermediary result that you can use as input for a second translation with <code>f</code> and <code>h</code>. </p> <p> The translation on the left-hand side should produce the same output as the right-hand side. </p> <p> Finally, if you keep one of the dimensions fixed, you essentially have a normal functor, and the second functor law should still hold. For example, if you hold the second dimension fixed, translating over the first dimension is equivalent to a normal functor projection, so the second functor law should hold: </p> <p> <pre>first (f . g) ≡ first f . first g</pre> </p> <p> If you replace <code>first</code> with <code>fmap</code>, you have the second functor law. </p> <p> <img src="/content/binary/second-functor-law-over-first-bifunctor-dimension.png" alt="The second functor law applied to the first dimension of a bifunctor."> </p> <p> In C#, you can write it like this: </p> <p> <pre>bf.SelectFirst(x&nbsp;=&gt;&nbsp;f(g(x)))&nbsp;==&nbsp;bf.SelectFirst(g).SelectFirst(f);</pre> </p> <p> Likewise, you can keep the first dimension constant and apply the second functor law to projections along the second axis: </p> <p> <pre>second (f . g) ≡ second f . second g</pre> </p> <p> Again, if you replace <code>second</code> with <code>fmap</code>, you have the second functor law. </p> <p> <img src="/content/binary/second-functor-law-over-second-bifunctor-dimension.png" alt="The second functor law applied to the second dimension of a bifunctor."> </p> <p> In C#, you express it like this: </p> <p> <pre>bf.SelectSecond(x&nbsp;=&gt;&nbsp;f(g(x)))&nbsp;==&nbsp;bf.SelectSecond(g).SelectSecond(f);</pre> </p> <p> The last two of these composition laws are specialisations of the general composition law, but where you fix either one or the other dimension. </p> <h3 id="41120f4d6ead482f9254a62853d9e1c4"> Summary <a href="#41120f4d6ead482f9254a62853d9e1c4" title="permalink">#</a> </h3> <p> A bifunctor is a container that can be translated over two dimensions, instead of a (mono)functor, which is a container that can be translated over a single dimension. In reality, there isn't a multitude of different bifunctors. While others exist, tuples and Either are the two most common bifunctors. They share an abstraction, but are still fundamentally different. A tuple always contains values of both dimensions at the same time, whereas Either only contains one of the values. </p> <p> Do trifunctors, quadfunctors, and so on, exist? Nothing prevents that, but they aren't particularly useful; in practice, you never run into them. </p> <p> <strong>Next:</strong> <a href="/2018/12/31/tuple-bifunctor">Tuple bifunctor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Lazy applicative functor https://blog.ploeh.dk/2018/12/17/the-lazy-applicative-functor 2018-12-17T07:52:00+00:00 Mark Seemann <div id="post"> <p> <em>Lazy computations form an applicative functor.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. A previous article has described how <a href="/2018/09/10/the-lazy-functor">lazy computations form a functor</a>. In this article, you'll see that lazy computations also form an applicative functor. </p> <h3 id="c04ca371d39847cc86a90240a7f53745"> Apply <a href="#c04ca371d39847cc86a90240a7f53745" title="permalink">#</a> </h3> <p> As you have <a href="/2018/10/15/an-applicative-password-list">previously seen</a>, C# isn't the best fit for the concept of applicative functors. Nevertheless, you can write an <code>Apply</code> extension method following the applicative 'code template': </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(()&nbsp;=&gt;&nbsp;selector.Value(source.Value)); }</pre> </p> <p> The <code>Apply</code> method takes both a lazy <code>selector</code> and a lazy value called <code>source</code>. It applies the function to the value and returns the result, still as a lazy value. If you have a lazy function <code>f</code> and a lazy value <code>x</code>, you can use the method like this: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;f&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y&nbsp;=&nbsp;f.Apply(x);</pre> </p> <p> The utility of <code>Apply</code>, however, mostly tends to emerge when you need to chain multiple <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">containers</a> together; in this case, multiple lazy values. You can do that by adding as many overloads to <code>Apply</code> as you need: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;(()&nbsp;=&gt;&nbsp;y&nbsp;=&gt;&nbsp;selector.Value(source.Value,&nbsp;y)); }</pre> </p> <p> This overload partially applies the input function. When <code>selector</code> is a function that takes two arguments, you can apply a single of those two arguments, and the result is a new function that closes over the value, but still waits for its second input argument. You can use it like this: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;f&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">char</span>&gt;&nbsp;c&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;i&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;s&nbsp;=&nbsp;f.Apply(c).Apply(i);</pre> </p> <p> Notice that you can chain the various overloads of <code>Apply</code>. In the above example, you have a lazy function that takes a <code>char</code> and an <code>int</code> as input, and returns a <code>string</code>. It could, for instance, be a function that invokes <a href="https://docs.microsoft.com/en-us/dotnet/api/system.string.-ctor?view=netframework-4.7.2#system-string-ctor(system-char-system-int32)">the equivalent <code>string</code> constructor overload</a>. </p> <p> Calling <code>f.Apply(c)</code> uses the overload that takes a <code>Lazy&lt;Func&lt;T1, T2, TResult&gt;&gt;</code> as input. The return value is a <code>Lazy&lt;Func&lt;int, string&gt;&gt;</code>, which the first <code>Apply</code> overload then picks up, to return a <code>Lazy&lt;string&gt;</code>. </p> <p> Usually, you may have one, two, or several lazy values, whereas your function itself isn't contained in a <code>Lazy</code> container. While you can use a helper method such as <code>Lazy.FromValue</code> to 'elevate' a 'normal' function to a lazy function value, it's often more convenient if you have another <code>Apply</code> overload like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;(()&nbsp;=&gt;&nbsp;y&nbsp;=&gt;&nbsp;selector(source.Value,&nbsp;y)); }</pre> </p> <p> The only difference to the equivalent overload is that in this overload, <code>selector</code> isn't a <code>Lazy</code> value, while <code>source</code> still is. This simplifies usage: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;f&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">char</span>&gt;&nbsp;c&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;i&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;s&nbsp;=&nbsp;f.Apply(c).Apply(i);</pre> </p> <p> Notice that in this variation of the example, <code>f</code> is no longer a <code>Lazy&lt;Func&lt;...&gt;&gt;</code>, but just a normal <code>Func</code>. </p> <h3 id="966d763816c84c839b7704cdebfd7bac"> F# <a href="#966d763816c84c839b7704cdebfd7bac" title="permalink">#</a> </h3> <p> <a href="https://fsharp.org">F#</a>'s type inference is more powerful than C#'s, so you don't have to resort to various overloads to make things work. You could, for example, create a minimal <code>Lazy</code> module: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Lazy&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Lazy&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Lazy&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;(x&nbsp;:&nbsp;Lazy&lt;&#39;a&gt;)&nbsp;=&nbsp;<span style="color:blue;">lazy</span>&nbsp;f&nbsp;x.Value &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Lazy&lt;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&gt;&nbsp;-&gt;&nbsp;Lazy&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Lazy&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;apply&nbsp;(x&nbsp;:&nbsp;Lazy&lt;_&gt;)&nbsp;(f&nbsp;:&nbsp;Lazy&lt;_&gt;)&nbsp;=&nbsp;<span style="color:blue;">lazy</span>&nbsp;f.Value&nbsp;x.Value</pre> </p> <p> In this code listing, I've repeated the <code>map</code> function shown in a <a href="/2018/09/10/the-lazy-functor">previous article</a>. It's not required for the implementation of <code>apply</code>, but you'll see it in use shortly, so I thought it was convenient to include it in the listing. </p> <p> If you belong to the camp of F# programmers who think that F# should emulate <a href="https://www.haskell.org">Haskell</a>, you can also introduce an operator: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;f&nbsp;x&nbsp;=&nbsp;Lazy.apply&nbsp;x&nbsp;f</pre> </p> <p> Notice that this <code>&lt;*&gt;</code> operator simply flips the arguments of <code>Lazy.apply</code>. If you introduce such an operator, be aware that the admonition from <a href="/2018/10/01/applicative-functors">the overview article</a> still applies. In Haskell, the <code>&lt;*&gt;</code> operator applies to any <code>Applicative</code>, which makes it truly general. In F#, once you define an operator like this, it applies specifically to a particular container type, which, in this case, is <code>Lazy&lt;'a&gt;</code>. </p> <p> You can replicate the first of the above C# examples like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;f&nbsp;:&nbsp;Lazy&lt;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;x&nbsp;:&nbsp;Lazy&lt;int&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;y&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;Lazy.apply&nbsp;x&nbsp;f</pre> </p> <p> Alternatively, if you want to use the <code>&lt;*&gt;</code> operator, you can compute <code>y</code> like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;y&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;f&nbsp;&lt;*&gt;&nbsp;x</pre> </p> <p> Chaining multiple lazy computations together also works: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;f&nbsp;:&nbsp;Lazy&lt;char&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;c&nbsp;:&nbsp;Lazy&lt;char&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;i&nbsp;:&nbsp;Lazy&lt;int&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;s&nbsp;=&nbsp;Lazy.apply&nbsp;c&nbsp;f&nbsp;|&gt;&nbsp;Lazy.apply&nbsp;i</pre> </p> <p> Again, you can compute <code>s</code> with the operator, if that's more to your liking: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;s&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;f&nbsp;&lt;*&gt;&nbsp;c&nbsp;&lt;*&gt;&nbsp;i</pre> </p> <p> Finally, if your function isn't contained in a <code>Lazy</code> value, you can start out with <code>Lazy.map</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;f&nbsp;:&nbsp;char&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;c&nbsp;:&nbsp;Lazy&lt;char&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;i&nbsp;:&nbsp;Lazy&lt;int&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;s&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;Lazy.map&nbsp;f&nbsp;c&nbsp;|&gt;&nbsp;Lazy.apply&nbsp;i</pre> </p> <p> This works without requiring additional overloads. Since F# natively supports partial function application, the first step in the pipeline, <code>Lazy.map f c</code> has the type <code>Lazy&lt;int -&gt; string&gt;</code> because <code>f</code> is a function of the type <code>char -&gt; int -&gt; string</code>, but in the first step, <code>Lazy.map f c</code> only supplies <code>c</code>, which contains a <code>char</code> value. </p> <p> Once more, if you prefer the infix operator, you can also compute <code>s</code> as: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;s&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;<span style="color:blue;">lazy</span>&nbsp;f&nbsp;&lt;*&gt;&nbsp;c&nbsp;&lt;*&gt;&nbsp;i</pre> </p> <p> While I find <a href="/2018/07/02/terse-operators-make-business-code-more-readable">operator-based syntax attractive in Haskell code</a>, I'm more hesitant about such syntax in F#. </p> <h3 id="63a6579eeb644627b02efc3f9aabd202"> Haskell <a href="#63a6579eeb644627b02efc3f9aabd202" title="permalink">#</a> </h3> <p> As outlined in the <a href="/2018/09/10/the-lazy-functor">previous article</a>, Haskell is already lazily evaluated, so it makes little sense to introduce an explicit <code>Lazy</code> data container. While Haskell's built-in <code>Identity</code> isn't quite equivalent to .NET's <code>Lazy&lt;T&gt;</code> object, some similarities remain; most notably, the <a href="/2018/09/03/the-identity-functor">Identity functor</a> is also applicative: </p> <p> <pre>Prelude Data.Functor.Identity&gt; :t f f :: a -&gt; Int -&gt; [a] Prelude Data.Functor.Identity&gt; :t c c :: Identity Char Prelude Data.Functor.Identity&gt; :t i i :: Num a =&gt; Identity a Prelude Data.Functor.Identity&gt; :t f &lt;$&gt; c &lt;*&gt; i f &lt;$&gt; c &lt;*&gt; i :: Identity String</pre> </p> <p> This little GHCi session simply illustrates that if you have a 'normal' function <code>f</code> and two <code>Identity</code> values <code>c</code> and <code>i</code>, you can compose them using the infix <em>map</em> operator <code>&lt;$&gt;</code>, followed by the infix <em>apply</em> operator <code>&lt;*&gt;</code>. This is equivalent to the F# expression <code>Lazy.map f c |&gt; Lazy.apply i</code>. </p> <p> Still, this makes little sense, since all Haskell expressions are already lazily evaluated. </p> <h3 id="d3ddcafbb8e14bfea2c18828cf44f244"> Summary <a href="#d3ddcafbb8e14bfea2c18828cf44f244" title="permalink">#</a> </h3> <p> The Lazy functor is also an <em>applicative functor</em>. This can be used to combine multiple lazily computed values into a single lazily computed value. </p> <p> <strong>Next:</strong> <a href="/2019/04/22/applicative-monoids">Applicative monoids</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Danish CPR numbers in F# https://blog.ploeh.dk/2018/12/10/danish-cpr-numbers-in-f 2018-12-10T08:14:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of domain-modelling in F#, including a fine example of using the option type as an applicative functor.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>, although the applicative functor example doesn't appear until towards the end. This article also serves the purpose of showing an example of <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> in F#. </p> <h3 id="163a2e2ecb144172a974d1eb23ff6703"> Danish personal identification numbers <a href="#163a2e2ecb144172a974d1eb23ff6703" title="permalink">#</a> </h3> <p> As outlined in the <a href="/2018/11/26/the-test-data-generator-applicative-functor">previous article</a>, in Denmark, everyone has a <a href="https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)">personal identification number</a>, in Danish called <em>CPR-nummer</em> (<em>CPR number</em>). </p> <p> CPR numbers have a simple format: <code>DDMMYY-SSSS</code>, where the first six digits indicate a person's birth date, and the last four digits are a sequence number. Some information, however, is also embedded in the sequence number. An example could be <code>010203-1234</code>, which indicates a woman born February 1, 1903. </p> <p> One way to model this in <a href="https://fsharp.org">F#</a> is with a single-case discriminated union: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CprNumber</span>&nbsp;=&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">CprNumber</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">int</span>&nbsp;*&nbsp;<span style="color:teal;">int</span>&nbsp;*&nbsp;<span style="color:teal;">int</span>&nbsp;*&nbsp;<span style="color:teal;">int</span>)&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">override</span>&nbsp;x.<span style="color:navy;">ToString</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(day,&nbsp;month,&nbsp;year,&nbsp;sequenceNumber))&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%02d%02d%02d</span><span style="color:#a31515;">-</span><span style="color:teal;">%04d</span><span style="color:#a31515;">&quot;</span>&nbsp;day&nbsp;month&nbsp;year&nbsp;sequenceNumber</pre> </p> <p> This is a common idiom in F#. In object-oriented design with e.g. C# or Java, you'd typically create a class and put guard clauses in its constructor. This would prevent a user from initialising an object with invalid data (such as <code>401500-1234</code>). While you <em>can</em> create classes in F# as well, a single-case union with a private case constructor can achieve the same degree of encapsulation. </p> <p> In this case, I decided to use a quadruple (4-tuple) as the internal representation, but this isn't visible to users. This gives me the option to refactor the internal representation, if I need to, without breaking existing clients. </p> <h3 id="83eaa41329eb4d8b894e0640f600f110"> Creating CPR number values <a href="#83eaa41329eb4d8b894e0640f600f110" title="permalink">#</a> </h3> <p> Since the <code>CprNumber</code> case constructor is private, you can't just create new values like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:#9b9b9b;">cpr</span>&nbsp;=&nbsp;CprNumber&nbsp;(1,&nbsp;1,&nbsp;1,&nbsp;1118) </pre> </p> <p> If you're outside the <code>Cpr</code> module that defines the type, this <strong>doesn't compile</strong>. This is by design, but obviously you need a way to create values. For convenience, input values for day, month, and so on, are represented as <code>int</code>s, which can be zero, negative, or way too large for CPR numbers. There's no way to statically guarantee that you can create a value, so you'll have to settle for a <code>tryCreate</code> function; i.e. a function that returns <code>Some CprNumber</code> if the input is valid, or <code>None</code> if it isn't. In <a href="https://www.haskell.org">Haskell</a>, this pattern is called a <em>smart constructor</em>. </p> <p> There's a couple of rules to check. All integer values must fall in certain ranges. Days must be between 1 and 31, months must be between 1 and 12, and so on. One way to enable succinct checks like that is with an <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/active-patterns">active pattern</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;(|<span style="color:navy;">Between</span>|_|)&nbsp;min&nbsp;max&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;min&nbsp;&lt;=&nbsp;candidate&nbsp;&amp;&amp;&nbsp;candidate&nbsp;&lt;=&nbsp;max &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> Straightforward: return <code>Some candidate</code> if <code>candidate</code> is between <code>min</code> and <code>max</code>; otherwise, <code>None</code>. This enables you to pattern-match input integer values to particular ranges. </p> <p> Perhaps you've already noticed that years are represented with only two digits. CPR is an old system (from 1968), and back then, bits were expensive. No reason to waste bits on recording the millennium or century in which people were born. It turns out, after all, that there's a way to at least partially figure out the century in which people were born. The first digit of the sequence number contains that information: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;int</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">calculateFourDigitYear</span>&nbsp;year&nbsp;sequenceNumber&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;centuryDigit&nbsp;=&nbsp;sequenceNumber&nbsp;/&nbsp;1000&nbsp;<span style="color:green;">//&nbsp;Integer&nbsp;division</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Table&nbsp;from&nbsp;https://da.wikipedia.org/wiki/CPR-nummer</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;centuryDigit,&nbsp;year&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;3&nbsp;_,&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1900 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;36&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;2000 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1900 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Between</span>&nbsp;5&nbsp;8&nbsp;_,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;57&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;2000 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Between</span>&nbsp;5&nbsp;8&nbsp;_,&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1800 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;36&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;2000 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1900 &nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;year</pre> </p> <p> As the code comment informs the reader, there's a table that defines the century, based on the two-digit year and the first digit of the sequence number. Note that birth dates in the nineteenth century are possible. No Danes born before 1900 are alive any longer, but at the time the CPR system was introduced, one person in the system was born in 1863! </p> <p> The <code>calculateFourDigitYear</code> function starts by pulling the first digit out of the sequence number. This is a four-digit number, so dividing by 1,000 produces the digit. I left a comment about integer division, because I often miss that detail when I read code. </p> <p> The big pattern-match expression uses the <code>Between</code> active pattern, but it ignores the return value from the pattern. This explains the wild cards (<code>_</code>), I hope. </p> <p> Although a pattern-match expression is often formatted over several lines of code, it's a single expression that produces a single value. Often, you see code where a <code>let</code>-binding binds a named value to a pattern-match expression. Another occasional idiom is to pipe a pattern-match expression to a function. In the <code>calculateFourDigitYear</code> function I use a language construct I've never seen anywhere else: the eight-lines pattern-match expression returns an <code>int</code>, which I simply add to <code>year</code> using the <code>+</code> operator. </p> <p> Both <code>calculateFourDigitYear</code> and the <code>Between</code> active pattern are private functions. They're only there as support functions for the public API. You can now implement a <code>tryCreate</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;CprNumber&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryCreate</span>&nbsp;day&nbsp;month&nbsp;year&nbsp;sequenceNumber&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;month,&nbsp;year,&nbsp;sequenceNumber&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Between</span>&nbsp;1&nbsp;12&nbsp;m,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;99&nbsp;y,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;9999&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fourDigitYear&nbsp;=&nbsp;<span style="color:navy;">calculateFourDigitYear</span>&nbsp;y&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;1&nbsp;&lt;=&nbsp;day&nbsp;&amp;&amp;&nbsp;day&nbsp;&lt;=&nbsp;<span style="color:teal;">DateTime</span>.<span style="color:navy;">DaysInMonth</span>&nbsp;(fourDigitYear,&nbsp;m) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(day,&nbsp;m,&nbsp;y,&nbsp;s)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> The <code>tryCreate</code> function begins by pattern-matching a triple (3-tuple) using the <code>Between</code> active pattern. The <code>month</code> must always be between <code>1</code> and <code>12</code> (both included), the <code>year</code> must be between <code>0</code> and <code>99</code>, and the <code>sequenceNumber</code> must always be between <code>0</code> and <code>9999</code> (in fact, I'm not completely sure if <code>0000</code> is valid). </p> <p> Finding the appropriate range for the <code>day</code> is more intricate. Is <code>31</code> always valid? Clearly not, because there's no November 31, for example. Is <code>30</code> always valid? No, because there's never a February 30. Is <code>29</code> valid? This depends on whether or not the year is a leap year. </p> <p> This reveals why you need <code>calculateFourDigitYear</code>. While you can use <code>DateTime.DaysInMonth</code> to figure out how many days a given month has, you need the year. Specifically, February 19<strong>00</strong> had 28 days, while February 20<strong>00</strong> had 29. </p> <p> Ergo, if <code>day</code>, <code>month</code>, <code>year</code>, and <code>sequenceNumber</code> all fall within their appropriate ranges, <code>tryCreate</code> returns a <code>Some CprNumber</code> value; otherwise, it returns <code>None</code>. </p> <p> Notice how this is different from an object-oriented constructor with guard clauses. If you try to create an object with invalid input, it'll throw an exception. If you try to create a <code>CprNumber</code> value, you'll receive a <code>CprNumber option</code>, and you, as the client developer, <em>must</em> handle both the <code>Some</code> and the <code>None</code> case. The compiler will enforce this. </p> <p> <pre>&gt; let gjern = Cpr.tryCreate 11 11 11 1118;; val gjern : Cpr.CprNumber option = Some 111111-1118 &gt; gjern |&gt; Option.map Cpr.born;; val it : DateTime option = Some 11.11.1911 00:00:00</pre> </p> <p> As most F# developers know, F# gives you enough syntactic sugar to make this a joy rather than a chore... and the warm and fuzzy feeling of safety is priceless. </p> <h3 id="78793bd7fceb4dc99bab81ccb7abb305"> CPR data <a href="#78793bd7fceb4dc99bab81ccb7abb305" title="permalink">#</a> </h3> <p> The above FSI session uses <code>Cpr.born</code>, which you haven't seen yet. With the tools available so far, it's trivial to implement; all the work is already done: </p> <p> <pre><span style="color:green;">//&nbsp;CprNumber&nbsp;-&gt;&nbsp;DateTime</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">born</span>&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(day,&nbsp;month,&nbsp;year,&nbsp;sequenceNumber))&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">DateTime</span>&nbsp;(<span style="color:navy;">calculateFourDigitYear</span>&nbsp;year&nbsp;sequenceNumber,&nbsp;month,&nbsp;day)</pre> </p> <p> While the <code>CprNumber</code> case constructor is <code>private</code>, it's still available from inside of the module. The <code>born</code> function pattern-matches <code>day</code>, <code>month</code>, <code>year</code>, and <code>sequenceNumber</code> out of its input argument, and trivially delegates the hard work to <code>calculateFourDigitYear</code>. </p> <p> Another piece of data you can deduce from a CPR number is the gender of the person: </p> <p> <pre><span style="color:green;">//&nbsp;CprNumber&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isFemale</span>&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(_,&nbsp;_,&nbsp;_,&nbsp;sequenceNumber))&nbsp;=&nbsp;sequenceNumber&nbsp;%&nbsp;2&nbsp;&nbsp;=&nbsp;0 <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isMale</span>&nbsp;&nbsp;&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(_,&nbsp;_,&nbsp;_,&nbsp;sequenceNumber))&nbsp;=&nbsp;sequenceNumber&nbsp;%&nbsp;2&nbsp;&lt;&gt;&nbsp;0</pre> </p> <p> The rule is that if the sequence number is even, then the person is female; otherwise, the person is male (and if you change sex, you get a new CPR number). </p> <p> <pre>&gt; gjern |&gt; Option.map Cpr.isFemale;; val it : bool option = Some true</pre> </p> <p> Since <code>1118</code> is even, this is a woman. </p> <h3 id="235df75fd03a41e49b5a1fc3767e6ce8"> Parsing CPR strings <a href="#235df75fd03a41e49b5a1fc3767e6ce8" title="permalink">#</a> </h3> <p> CPR numbers are often passed around as text, so you'll need to be able to parse a <code>string</code> representation. As described in the <a href="/2018/11/26/the-test-data-generator-applicative-functor">previous article</a>, you should follow <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. Input could include extra white space, and the middle dash could be missing. </p> <p> The .NET Base Class Library contains enough utility methods working on <code>string</code> values that this isn't going to be particularly difficult. It can, however, be awkward to interoperate with object-oriented APIs, so you'll often benefit from adding a few utility functions that give you curried functions instead of objects with methods. Here's one that adapts <code>Int32.TryParse</code>: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:teal;">Int</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;int&nbsp;option</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryParse</span>&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;candidate&nbsp;|&gt;&nbsp;<span style="color:teal;">Int32</span>.<span style="color:navy;">TryParse</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> Nothing much goes on here. While F# has pleasant syntax for handling <code>out</code> parameters, it can be inconvenient to have to pattern-match every time you'd like to try to parse an integer. </p> <p> Here's another helper function: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:teal;">String</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;string&nbsp;-&gt;&nbsp;string&nbsp;option</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">trySubstring</span>&nbsp;startIndex&nbsp;length&nbsp;(s&nbsp;:&nbsp;<span style="color:teal;">string</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;s.Length&nbsp;&lt;&nbsp;startIndex&nbsp;+&nbsp;length &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(s.<span style="color:navy;">Substring</span>&nbsp;(startIndex,&nbsp;length))</pre> </p> <p> This one comes with two benefits: The first benefit is that it's curried, which enables partial application and piping. You'll see an example of this further down. The second benefit is that it handles at least one error condition in a type-safe manner. When trying to extract a sub-string from a string, the <code>Substring</code> <em>method</em> can throw an exception if the index or length arguments are out of range. This function checks whether it can extract the requested sub-string, and returns <code>None</code> if it can't. </p> <p> I wouldn't be surprised if there are edge cases (for example involving negative integers) that <code>trySubstring</code> doesn't handle gracefully, but as you may have noticed, this is a function in a <code>private</code> module. I only need it to handle a particular use case, and it does that. </p> <p> You can now add the <code>tryParse</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;CprNumber&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryParse</span>&nbsp;(candidate&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;fo&nbsp;xo&nbsp;=&nbsp;fo&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;xo&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;canonicalized&nbsp;=&nbsp;candidate.<span style="color:navy;">Trim</span>().<span style="color:navy;">Replace</span>(<span style="color:#a31515;">&quot;-&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;dayCandidate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;canonicalized&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">trySubstring</span>&nbsp;0&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;monthCandidate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;canonicalized&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">trySubstring</span>&nbsp;2&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;yearCandidate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;canonicalized&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">trySubstring</span>&nbsp;4&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sequenceNumberCandidate&nbsp;=&nbsp;canonicalized&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">trySubstring</span>&nbsp;6&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;<span style="color:navy;">tryCreate</span> &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:teal;">Int</span>.<span style="color:navy;">tryParse</span>&nbsp;dayCandidate &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:teal;">Int</span>.<span style="color:navy;">tryParse</span>&nbsp;monthCandidate &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:teal;">Int</span>.<span style="color:navy;">tryParse</span>&nbsp;yearCandidate &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:teal;">Int</span>.<span style="color:navy;">tryParse</span>&nbsp;sequenceNumberCandidate &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">id</span></pre> </p> <p> The function starts by defining a private <code>&lt;*&gt;</code> operator. Readers of the <a href="/2018/10/01/applicative-functors">applicative functor article series</a> will recognise this as the 'apply' operator. The reason I added it as a private operator is that I don't need it anywhere else in the code base, and in F#, I'm always worried that if I add <code>&lt;*&gt;</code> at a more visible level, it could collide with other definitions of <code>&lt;*&gt;</code> - for example <a href="/2018/10/08/full-deck">one for lists</a>. This one particularly makes <code>option</code> an applicative functor. </p> <p> The first step in parsing <code>candidate</code> is to remove surrounding white space and the interior dash. </p> <p> The next step is to use <code>String.trySubstring</code> to pull out candidates for <em>day</em>, <em>month</em>, and so on. Each of these four are <code>string option</code> values. </p> <p> All four of these must be <code>Some</code> values before we can even start to attempt to turn them into a <code>CprNumber</code> value. If only a single value is <code>None</code>, <code>tryParse</code> should return <code>None</code> as well. </p> <p> You may want to re-read the <a href="/2018/10/15/an-applicative-password-list">article on the List applicative functor</a> for a detailed explanation of how the <code>&lt;*&gt;</code> operator works. In <code>tryParse</code>, you have four <code>option</code> values, so you apply them all using four <code>&lt;*&gt;</code> operators. Since four values are being applied, you'll need a function that takes four curried input arguments of the appropriate types. In this case, all four are <code>int option</code> values, so for the first expression in the <code>&lt;*&gt;</code> chain, you'll need an option of a function that takes four <code>int</code> arguments. </p> <p> Lo and behold! <code>tryCreate</code> takes four <code>int</code> arguments, so the only action you need to take is to make it an <code>option</code> by putting it in a <code>Some</code> case. </p> <p> The only remaining hurdle is that <code>tryCreate</code> returns <code>CprNumber option</code>, and since you're already 'in' the <code>option</code> applicative functor, you now have a <code>CprNumber option option</code>. Fortunately, <code>bind id</code> is always the 'flattening' combo, so that's easily dealt with. </p> <p> <pre>&gt; let andreas = Cpr.tryParse " 0109636221";; val andreas : Cpr.CprNumber option = Some 010963-6221</pre> </p> <p> Since you now have both a way to parse a string, and turn a <code>CprNumber</code> into a string, you can write the usual round-trip property: </p> <p> <pre>[&lt;<span style="color:teal;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``CprNumber&nbsp;correctly&nbsp;round-trips``</span>&nbsp;()&nbsp;=&nbsp;<span style="color:teal;">Property</span>.<span style="color:navy;">check</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">property</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:teal;">Gen</span>.cprNumber &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;actual&nbsp;&nbsp;&nbsp;=&nbsp;expected&nbsp;|&gt;&nbsp;<span style="color:navy;">string</span>&nbsp;|&gt;&nbsp;<span style="color:teal;">Cpr</span>.<span style="color:navy;">tryParse</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;expected&nbsp;=!&nbsp;actual&nbsp;}</pre> </p> <p> This test uses <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a>, <a href="https://github.com/SwensenSoftware/unquote">Unquote</a>, and <a href="https://xunit.net">xUnit.net</a>. The previous article demonstrates a way to test that <code>Cpr.tryParse</code> can handle mangled input. </p> <h3 id="503bb1791d344b1f917734cffe2320f6"> Summary <a href="#503bb1791d344b1f917734cffe2320f6" title="permalink">#</a> </h3> <p> This article mostly exhibited various F# design techniques you can use to achieve an even better degree of encapsulation than you can easily get with object-oriented languages. Towards the end, you saw how using <code>option</code> as an applicative functor enables you to compose more complex optional values from smaller values. If just a single value is <code>None</code>, the entire expression becomes <code>None</code>, but if all values are <code>Some</code> values, the computation succeeds. </p> <p> This article is an entry in the <a href="https://sergeytihon.com/2018/10/22/f-advent-calendar-in-english-2018">F# Advent Calendar in English 2018</a>. </p> <p> <strong>Next: </strong> <a href="/2018/12/17/the-lazy-applicative-functor">The Lazy applicative functor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="559c8543d61042a0bdcbe938ac97de12"> <div class="comment-author"><a href="https://activesolution.se">Viktor Andersson</a> <a href="#559c8543d61042a0bdcbe938ac97de12">#</a></div> <div class="comment-content"> <p> Great post, a very good read! Interestingly enough we recently made an F# implementation for the swedish personal identification number. In fact <a href="https://github.com/ActiveLogin/ActiveLogin.Identity">v1.0.0</a> will be published any day now. Interesting to see how the problem with four-digit years are handled differently in Denmark and Sweden. </p> <p> I really like the Between Active pattern of your solution, we did not really take as a generic approach, instead we modeled with types for Year, Month, Day, etc. But I find your solution to be very concise and clear. Also we worked with the Result type instead of Option to be able to provide the client with helpful error messages. For our Object Oriented friends we are also exposing a C#-friendly facade which adds a bit of boiler plate code. </p> </div> <div class="comment-date">2018-12-18 06:26 UTC</div> </div> <div class="comment" id="26683afcb79c444f9887f112323b935a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#26683afcb79c444f9887f112323b935a">#</a></div> <div class="comment-content"> <p> Viktor, thank you for your kind words. The <code>Result</code> (or <a href="/2018/06/11/church-encoded-either">Either</a>) type does, indeed, provide more information when things go wrong. This can be useful when client code needs to handle different error cases in different ways. Sometimes, it may also be useful, as you write, when you want to provide more <a href="/2014/12/23/exception-messages-are-for-programmers">helpful error messages</a>. </p> <p> Particularly when it comes to parsing or input validation, <a href="/2018/11/05/applicative-validation">Either can be useful</a>. </p> <p> The main reason I chose to model with <code>option</code> in this article was that I wanted to demonstrate how to use the <a href="/2018/10/01/applicative-functors">applicative</a> nature of <code>option</code>, but I suppose I could have equally done so with <code>Result</code>. </p> </div> <div class="comment-date">2018-12-18 9:09 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Set is not a functor https://blog.ploeh.dk/2018/12/03/set-is-not-a-functor 2018-12-03T07:58:00+00:00 Mark Seemann <div id="post"> <p> <em>Sets aren't functors. An example for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. The way functors are frequently presented to programmers is as a generic <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> (<code>Foo&lt;T&gt;</code>) equipped with a translation method, normally called <em>map</em>, but in C# <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatically</a> called <code>Select</code>. </p> <p> It'd be tempting to believe that any generic type with a <code>Select</code> method is a functor, but it takes more than that. The <code>Select</code> method must also obey the <a href="/2018/03/22/functors">functor laws</a>. This article shows an example of a translation that violates the second functor law. </p> <h3 id="5a13402d14f54ce689a7ae4e62f54233"> Mapping sets <a href="#5a13402d14f54ce689a7ae4e62f54233" title="permalink">#</a> </h3> <p> The .NET Base Class Library comes with a class called <a href="https://docs.microsoft.com/en-gb/dotnet/api/system.collections.generic.hashset-1">HashSet&lt;T&gt;</a>. This generic class implements <code>IEnumerable&lt;T&gt;</code>, so, via extension methods, it has a <code>Select</code> method. </p> <p> Unfortunately, that <code>Select</code> method isn't a structure-preserving translation of sets. The problem is that it treats sets as enumerable sequences, which implies an order to elements that isn't part of a set's structure. </p> <p> In order to understand what the problem is, consider this <a href="https://xunit.net">xUnit.net</a> test: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SetAsEnumerableSelectLeadsToWrongConclusions() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;set1&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;{&nbsp;1,&nbsp;2,&nbsp;3&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;set2&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;{&nbsp;3,&nbsp;2,&nbsp;1&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(set1.SetEquals(set2)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;id&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;seq1&nbsp;=&nbsp;set1.AsEnumerable().Select(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;seq2&nbsp;=&nbsp;set2.AsEnumerable().Select(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(seq1.SequenceEqual(seq2)); }</pre> </p> <p> This test creates two sets, and by using a <a href="http://xunitpatterns.com/Guard%20Assertion.html">Guard Assertion</a> demonstrates that they're equal to each other. It then proceeds to <code>Select</code> over both sets, using the identity function <code>id</code>. The return values aren't <code>HashSet&lt;T&gt;</code> objects, but rather <code>IEnumerable&lt;T&gt;</code> sequences. Due to an implementation detail in <code>HashSet&lt;T&gt;</code>, these two sequences are different, because they were populated in reverse order of each other. </p> <p> The problem is that the <code>Select</code> extension method has the signature <code>IEnumerable&lt;TResult&gt; Select&lt;T, TResult&gt;(IEnumerable&lt;T&gt;, Func&lt;T, TResult&gt;)</code>. It doesn't operate on <code>HashSet&lt;T&gt;</code>, but instead treats it as an ordered sequence of elements. A set isn't intrinsically ordered, so that's not the translation you need. </p> <p> To be clear, <code>IEnumerable&lt;T&gt;</code> <em>is</em> a functor (as long as the sequence is <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>). It's just not the functor you're looking for. What you need is a method with the signature <code>HashSet&lt;TResult&gt; Select&lt;T, TResult&gt;(HashSet&lt;T&gt;, Func&lt;T, TResult&gt;)</code>. Fortunately, such a method is trivial to implement: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(source.AsEnumerable().Select(selector)); }</pre> </p> <p> This extension method offers a proper <code>Select</code> method that translates one <code>HashSet&lt;T&gt;</code> into another. It does so by enumerating the elements of the set, then using the <code>Select</code> method for <code>IEnumerable&lt;T&gt;</code>, and finally wrapping the resulting sequence in a new <code>HashSet&lt;TResult&gt;</code> object. </p> <p> This is a better candidate for a translation of sets. Is it a functor, then? </p> <h3 id="44c78cf51897490a8d844a89eaa8ef73"> Second functor law <a href="#44c78cf51897490a8d844a89eaa8ef73" title="permalink">#</a> </h3> <p> Even though <code>HashSet&lt;T&gt;</code> and the new <code>Select</code> method have the correct types, it's still only a functor if it obeys the functor laws. It's easy, however, to come up with a counter-example that demonstrates that <code>Select</code> violates the second functor law. </p> <p> Consider a pair of conversion methods between <code>string</code> and <code>DateTimeOffset</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;ParseDateTime(<span style="color:blue;">string</span>&nbsp;s) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;dt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:#2b91af;">DateTimeOffset</span>.TryParse(s,&nbsp;<span style="color:blue;">out</span>&nbsp;dt)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;dt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.MinValue; } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;FormatDateTime(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;dt) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;dt.ToString(<span style="color:#a31515;">&quot;yyyy&#39;-&#39;MM&#39;-&#39;dd&#39;T&#39;HH&#39;:&#39;mm&#39;:&#39;sszzz&quot;</span>); }</pre> </p> <p> The first method, <code>ParseDateTime</code>, converts a <code>string</code> into a <code>DateTimeOffset</code> value. It tries to parse the input, and returns the corresponding <code>DateTimeOffset</code> value if this is possible; for all other input values, it simply returns <code>DateTimeOffset.MinValue</code>. You may dislike how the method deals with input values it can't parse, but that's not important. In order to prove that sets aren't functors, I just need <em>one</em> counter-example, and the one I'll pick will not involve unparseable strings. </p> <p> The <code>FormatDateTime</code> method converts any <code>DateTimeOffset</code> value to an <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601 string</a>. </p> <p> The <code>DateTimeOffset</code> type is the interesting piece of the puzzle. In case you're not intimately familiar with it, a <code>DateTimeOffset</code> value contains a date, a time, and a time-zone offset. You can, for example create one like this: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(2018,&nbsp;4,&nbsp;17,&nbsp;15,&nbsp;9,&nbsp;55,&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(2)) [17.04.2018 15:09:55 +02:00]</pre> </p> <p> This represents April 17, 2018, at 15:09:55 at UTC+2. You can convert that value to UTC: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(2018,&nbsp;4,&nbsp;17,&nbsp;15,&nbsp;9,&nbsp;55,&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(2)).ToUniversalTime() [17.04.2018 13:09:55 +00:00]</pre> </p> <p> This value clearly contains different constituent elements, but it does represent the same instant in time. This is how <code>DateTimeOffset</code> implements <code>Equals</code>. These two object are considered equal, as the following test will demonstrate. </p> <p> In a sense, you could argue that there's some sense in implementing equality for <code>DateTimeOffset</code> in that way, but this unusual behaviour provides just the counter-example that demonstrates that sets aren't functors: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SetViolatesSecondFunctorLaw() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2018-04-17T13:05:28+02:00&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2018-04-17T11:05:28+00:00&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(ParseDateTime(x),&nbsp;ParseDateTime(y)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;set&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;{&nbsp;x,&nbsp;y&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;l&nbsp;=&nbsp;set.Select(ParseDateTime).Select(FormatDateTime); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;r&nbsp;=&nbsp;set.Select(s&nbsp;=&gt;&nbsp;FormatDateTime(ParseDateTime(s))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(l.SetEquals(r)); }</pre> </p> <p> This passing test provides the required counter-example. It first creates two ISO 8601 representations of the same instant. As the Guard Assertion demonstrates, the corresponding <code>DateTimeOffset</code> values are considered equal. </p> <p> In <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">the <em>act</em> phase</a>, the test creates a <code>set</code> of these two strings. It then performs two round-trips over <code>DateTimeOffset</code> and back to <code>string</code>. The second functor law states that it shouldn't matter whether you do it in one or two steps, but it does. <code>Assert.False</code> passes because <code>l</code> is <em>not</em> equal to <code>r</code>. Q.E.D. </p> <h3 id="f392310c11984d08a31bf6456849dd01"> An illustration <a href="#f392310c11984d08a31bf6456849dd01" title="permalink">#</a> </h3> <p> The problem is that sets only contain one element of each value, and due to the way that <code>DateTimeOffset</code> interprets equality, two values that both represent the same instant are collapsed into a single element when taking an intermediary step. You can illustrate it like this: </p> <p> <img src="/content/binary/set-violating-second-functor-law.png" alt="Diagram illustrating the above counter-example."> </p> <p> In this illustration, I've hidden the date behind an ellipsis in order to improve clarity. The date segments are irrelevant in this example, since they're all identical. </p> <p> You start with a set of two <code>string</code> values. These are obviously different, so the set contains two elements. When you map the set using the <code>ParseDateTime</code> function, you get two <code>DateTimeOffset</code> values that .NET considers equal. For that reason, <code>HashSet&lt;DateTimeOffset&gt;</code> considers the second value redundant, and ignores it. Therefore, the intermediary set contains only a single element. When you map that set with <code>FormatDateTime</code>, there's only a single element to translate, so the final set contains only a single <code>string</code> value. </p> <p> On the other hand, when you map the input set without an intermediary set, each <code>string</code> value is first converted into a <code>DateTimeOffset</code> value, and then immediately thereafter converted back to a <code>string</code> value. Since the two resulting strings are different, the resulting set contains two values. </p> <p> Which of these paths is correct, then? Both of them. That's the problem. When considering the semantics of sets, both translations produce correct results. Since the results diverge, however, the translation isn't a functor. </p> <h3 id="ee52a36b611b40c5a7d015bea3cbf1dc"> Summary <a href="#ee52a36b611b40c5a7d015bea3cbf1dc" title="permalink">#</a> </h3> <p> A functor must not only preserve the structure of the data container, but also of functions. The functor laws express that a translation of functions preserves the structure of how the functions compose, in addition to preserving the structure of the containers. Mapping a set doesn't preserve those structures, and that's the reason that sets aren't functors. </p> <h3 id="649aceb6a241450abc861ec63f34ff0a"> P.S. <a href="#649aceb6a241450abc861ec63f34ff0a" title="permalink">#</a> </h3> <p> <strong>2019-01-09.</strong> There's been some controversy around this post, and more than one person has pointed out to me that the underlying reason that the functor laws are violated in the above example is because <code>DateTimeOffset</code> behaves oddly. Specifically, its <code>Equals</code> implementation breaks the <em>substitution property</em> of equality. See e.g. <a href="https://giacomociti.github.io">Giacomo Citi</a>'s <a href="https://giacomociti.github.io/2019/01/02/Abstract-types-are-more-equal.html">response</a> for details. </p> <p> The term <em>functor</em> originates from <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, and I don't claim to be an expert on that topic. It's unclear to me if the underlying concept of <em>equality</em> is explicitly defined in category theory, but it wouldn't surprise me if it is. If that definition involves the substitution property, then the counter-example presented here says nothing about Set as a functor, but rather that <code>DateTimeOffset</code> has unlawful equality behaviour. </p> <p> <strong>Next:</strong> <a href="/2018/10/01/applicative-functors">Applicative functors</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b58fe3a0556347a9bcfb2f39b0b5250c"> <div class="comment-author">Matt Heuer <a href="#b58fe3a0556347a9bcfb2f39b0b5250c">#</a></div> <div class="comment-content"> <p> The Problem here is not the HashSet, but the DateTimeOffset Class. It defines Equality loosely, so Time-Comparison is possible between different Time-Zones. The Functor Property can be restored by applying a stricter Equality Comparer like this one: <pre> var l = set.Select(ParseDateTime, StrictDateTimeOffsetComparer._).Select(FormatDateTime); /// &lt;summary&gt; Compares both &lt;see cref="DateTimeOffset.Offset"/&gt; and &lt;see cref="DateTimeOffset.UtcTicks"/&gt; &lt;/summary&gt; public class StrictDateTimeOffsetComparer : IEqualityComparer&lt;DateTimeOffset&gt; { public bool Equals(DateTimeOffset x, DateTimeOffset y) => x.Offset.Equals(y.Offset) && x.UtcTicks == y.UtcTicks; public int GetHashCode(DateTimeOffset obj) => HashCode.Combine(obj.Offset, obj.UtcTicks); /// &lt;summary&gt; Singleton Instance &lt;/summary&gt; public static readonly StrictDateTimeOffsetComparer _ = new(); StrictDateTimeOffsetComparer(){} }</pre> As for preserving Referential Transparency, the Distinction between Equality and Identity is important. By Default, Classes implement 'Equals' and '==' using ReferenceEquals which guarantees Substitution. Structs don't have a Reference unless they are boxed, instead they are copied and usually have Value Semantics. To be truly 'referentially transparent' though, Equality must be based on ALL Fields. When someone overrides Equals with a weaker Equivalence Relation than ReferenceEquals, it is their Responsibility to preserve Substitution. No Library Author can cover all possible application cases for HashSet. </p> </div> <div class="comment-date">2021-03-13 13:16 UTC</div> </div> <div class="comment" id="2e5d7f35754e4b1b9ae2ffc2ab353be1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2e5d7f35754e4b1b9ae2ffc2ab353be1">#</a></div> <div class="comment-content"> <p> Matt, thank you for writing. <code>DateTimeOffset</code> already defines the <a href="https://docs.microsoft.com/dotnet/api/system.datetimeoffset.equalsexact">EqualsExact</a> method. Wouldn't it be easier to use that? </p> </div> <div class="comment-date">2021-03-14 8:40 UTC</div> </div> <div class="comment" id="fa6e9103c5fb4fc4807828e4e53b3929"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#fa6e9103c5fb4fc4807828e4e53b3929">#</a></div> <div class="comment-content"> <blockquote> In order to prove that sets aren't functors, I just need <em>one</em> counter-example, and the one I'll pick will not involve unparseable strings. </blockquote> <p> In context, you are correct; you can pick whatever example you want to create a counter-example. However, your counter-example does not prove that <code>HashSet&lt;&gt;</code> is not a functor. </p> <p> We often say that a type is or isn't a functor. That is not quite right. It is more correct to say that a type <code>T&lt;&gt;</code> <em>and</em> a function with inputs <code>T&lt;A&gt;</code> and <code>A</code> and output <code>T&lt;B&gt;</code> are or are not a functor. Then I think it is reasonable to say some type is a functor if (and only if) there <em>exists</em> a function such that the type and that functor are a functor. In that case, to conclude that <code>HashSet&lt;&gt;</code> is not a functor requires one to prove that, <em>for all </em> functions <code>f</code>, the pair <code>HashSet&lt;&gt;</code> and <code>f</code> are not a functor. Because of the universal quantification, this cannot be achieved via a single counter-example. </p> <p> Your counter-example shows that the type <code>HashSet&lt;&gt;</code> and <em>your</em> function that you called <code>Select</code> is not a functor. Your <code>Select</code> function uses <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.-ctor#System_Collections_Generic_HashSet_1__ctor_System_Collections_Generic_IEnumerable__0__">this constructor overload of <code>HashSet&lt;&gt;</code></a>, which eventually uses <code>EqualityComparer&lt;T&gt;.Default</code> in places like <a href="https://github.com/dotnet/runtime/blob/5ef6a0687ad58fdb726387897cde0d2584264fb6/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/HashSet.cs#L931">here</a> and <a href="https://github.com/dotnet/runtime/blob/5ef6a0687ad58fdb726387897cde0d2584264fb6/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/HashSet.cs#L321">here</a>. The instance of <code>EqualityComparer&lt;DateTimeOffset&gt;.Default</code> implements its <code>Equals</code> method via <code>DateTimeOffset.Equals</code>. The comments by Giacomo Citi and others explained "why" your <code>Select</code> function (and the type <code>HashSet&lt;&gt;</code>) doesn't satisfy the second functor law: you picked a constructor overload of <code>HashSet&lt;&gt;</code> that uses an implementation of <code>IEqualityComparer&lt;DateTimeOffset&gt;</code> that depends on <code>DateTimeOffset.Equals</code>, which doesn't satisfy the substitution property. </p> <blockquote> The term <em>functor</em> originates from <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, and I don't claim to be an expert on that topic. It's unclear to me if the underlying concept of <em>equality</em> is explicitly defined in category theory, but it wouldn't surprise me if it is. If that definition involves the substitution property, then the counter-example presented here says nothing about Set as a functor, but rather that <code>DateTimeOffset</code> has unlawful equality behaviour. </blockquote> <p> I think this is a red herring. Microsoft can implement <code>HashSet&lt;&gt;</code> however it wants and you can implement your <code>Select</code> function however you want. Then the pair are either a functor or not. Independent of this is that Microsoft gets to define the contract for <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1.equals#System_Collections_Generic_IEqualityComparer_1_Equals__0__0_"><code>IEqualityComparer&lt;&gt;.Equals</code></a>. The documentation states that a valid implementation must be reflexive, symmetric, and transitive but does not require that the substitution property is satisifed. </p> <p> I think the correct question is this: Is <code>HashSet&lt;&gt;</code> a functor?...which is equivalent to: Does there exist a function such that <code>HashSet&lt;&gt;</code> and that function are a functor? I think the answer is "yes". </p> <p> Implement <code>Select</code> by calling <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.-ctor#System_Collections_Generic_HashSet_1__ctor_System_Collections_Generic_IEnumerable__0__System_Collections_Generic_IEqualityComparer__0__">the constructor overload of <code>HashSet&lt;&gt;</code> that takes an instance of <code>IEnumerable&lt;&gt;</code> as well as an instance of <code>IEqualityComparer&lt;&gt;</code></a>. For the second argument, provide an instance that implements <code>Equals</code> by always returning <code>false</code> (and it suffices to implement <code>GetHashCode</code> by returning a constant). I claim that <code>HashSet&lt;&gt;</code> and that function are a functor. In fact, I further claim that this functor is isomorphic to the functor <code>IEnumerable&lt;&gt;</code> (with the function <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select#System_Linq_Enumerable_Select__2_System_Collections_Generic_IEnumerable___0__System_Func___0___1__"><code>Enumerable.Select</code></a>). That doesn't make it a very interesting functor, but it is a functor nonetheless. (Also, notice that this implementation of <code>Equals</code> is not reflexive, but I still used it to create a functor.) </p> </div> <div class="comment-date">2021-03-16 18:32 UTC</div> </div> <div class="comment" id="11b5047c500241058aa9a58320157824"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#11b5047c500241058aa9a58320157824">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Yes, I agree with most of what you write. As I added in <a href="#649aceb6a241450abc861ec63f34ff0a">the P.S.</a>, I'm aware that this post draws the wrong conclusions. I also understand why. </p> <p> It's true that one way to think of a functor is a type and an associated function. When I (and others) sometimes fall into the trap of directly equating types with functors, it's because this is the most common implementation of functors (and monads) in statically typed programming. This is probably most explicit in <a href="https://www.haskell.org">Haskell</a>, where a particular functor is explicitly associated with a type. This has the implication that in order to distinguish between multiple functors, Haskell comes with all sort of wrapper types. You mostly see this in the context of semigroups and monoids, where the <a href="https://hackage.haskell.org/package/base/docs/Data-Semigroup.html#t:Sum">Sum</a> and <a href="https://hackage.haskell.org/package/base/docs/Data-Semigroup.html#t:Product">Product</a> wrappers exist only to distinguish between two monoids over the same type (numbers). </p> <p> You could imagine something similar happening with <a href="/2019/01/07/either-bifunctor">Either</a> and <a href="/2018/12/31/tuple-bifunctor">tuple bifunctors</a>. By conventions (and because of a bit pseudo-principled hand-waving), we conventionally pick one of the their dimensions for the 'functor instance'. For example, we pick the <em>right</em> dimension of Either for the functor instance. In both Haskell, <a href="https://fsharp.org">F#</a>, and C#, this enables certain syntax, such as Haskell <code>do</code> notation, F# computation expressions, or C# query syntax. </p> <p> If we wanted to instead make mapping over, say, <em>left</em>, the 'functor instance', we can't do that without throwing away the language support for the <em>right</em> side. The solution is to introduce a new type that comes with the desired function associated. This enables the compiler to distinguish between two (or more) functors over what would otherwise be the same type. </p> <p> That aside, can we resolve the issue in the way that you suggest? Possibly. I haven't thought deep and long about it, but what you write sounds plausible. </p> <p> For anyone who wants to use such a <em>set functor</em> for real code, I'd suggest wrapping it in another type, since relying on client developers to use the 'correct' constructor overload seems too brittle. </p> </div> <div class="comment-date">2021-03-17 18:57 UTC</div> </div> <div class="comment" id="69992ac5293246b894cbef27f8fe7585"> <div class="comment-author">Matt Heuer <a href="#69992ac5293246b894cbef27f8fe7585">#</a></div> <div class="comment-content"> <p> Well, yes, but since you have to implement a proper matching HashCode Function anyway, I thought it would be more obvious to write a matching Equals Function too. </p> </div> <div class="comment-date">2021-03-19 8:43 UTC</div> </div> <div class="comment" id="7751aa9592eb4a149945a079a2d03968"> <div class="comment-author">Matt Heuer <a href="#7751aa9592eb4a149945a079a2d03968">#</a></div> <div class="comment-content"> <p> Ah, so you're proposing to subclass HashSet&lt;DateTimeOffset> and pass the proper EqualityComparer to the base class. Alright, that would be a proper Functor then. </p> </div> <div class="comment-date">2021-03-19 8:43 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Test Data Generator applicative functor https://blog.ploeh.dk/2018/11/26/the-test-data-generator-applicative-functor 2018-11-26T07:24:00+00:00 Mark Seemann <div id="post"> <p> <em>Test Data Generator modelled as an applicative functor, with examples in C# and F#.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. It shows yet another example of an applicative functor. I tend to think of applicative functors as an abstraction of 'combinations' of values and functions, but this is most evident for lists and other collections. Even so, I think that the intuition also holds for <a href="/2018/10/29/the-maybe-applicative-functor">Maybe as an applicative functor</a> as well as <a href="/2018/11/05/applicative-validation">validation as an applicative functor</a>. This is also the case for the <a href="/2017/09/18/the-test-data-generator-functor">Test Data Generator functor</a>. </p> <h3 id="bc5212f7353f44f793ed20399d359bfe"> Applicative generator in C# <a href="#bc5212f7353f44f793ed20399d359bfe" title="permalink">#</a> </h3> <p> In a <a href="/2017/09/18/the-test-data-generator-functor">previous article</a>, you saw how to implement a Test Data Generator as a functor in C#. The core class is this <code>Generator&lt;T&gt;</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;generate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Generator(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;generate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(generate&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(generate)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.generate&nbsp;=&nbsp;generate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T1</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(f&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(f)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;newGenerator&nbsp;=&nbsp;r&nbsp;=&gt;&nbsp;f(<span style="color:blue;">this</span>.generate(r)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T1</span>&gt;(newGenerator); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Generate(<span style="color:#2b91af;">Random</span>&nbsp;random) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(random&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(random)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.generate(random); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is merely repetition from the earlier article. </p> <p> <code>Generator&lt;T&gt;</code> is already a functor, but you can make it an <em>applicative</em> functor by adding an extension method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;generator) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selectors&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(selectors)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(generator&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(generator)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;newGenerator&nbsp;=&nbsp;r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;f&nbsp;=&nbsp;selectors.Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;generator.Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f(x); &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(newGenerator); }</pre> </p> <p> The <code>Apply</code> function combines a generator of functions with a generator of values. Given these two generators, it defines a closure over both, and packages that function in a new generator object. </p> <h3 id="58282543d8c4409cb596547e131bf249"> CPR example <a href="#58282543d8c4409cb596547e131bf249" title="permalink">#</a> </h3> <p> When is it interesting to combine a randomly selected function with a randomly generated value? Here's an example. </p> <p> In Denmark, everyone has a <a href="https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)">personal identification number</a>, in Danish called <em>CPR-nummer</em> (<em>CPR number</em>). It's somewhat comparable to the <a href="https://en.wikipedia.org/wiki/Social_Security_number">U.S. Social Security number</a>, but surely more Orwellian. </p> <p> CPR numbers have a simple format: <code>DDMMYY-SSSS</code>, where the first six digits indicate a person's birth date, and the last four digits are a sequence number. An example could be <code>010203-1234</code>, which indicates a woman born February 1, 1903. Assume that you're writing a system that has to accept CPR numbers as input. You've represented CPR number as a class called <code>CprNumber</code>, and you've already written a parser, but now it turns out that sometimes users enter slightly malformed data. </p> <p> Sometimes users copy and paste CPR numbers from other sources, and occasionally, they inadvertently include a leading or trailing space. Other users forget the dash between the birth date and the sequence number. In other words, sometimes you receive input like <code>"050301-4231 "</code> or <code>"1211993321"</code>. </p> <p> Using your fancy Test Data Generator, you'd like to write a test that verifies that your parser follows <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>: <em>Be conservative in what you send, be liberal in what you accept</em>. </p> <p> Assume that you already have a generator that can produce valid <code>CprNumber</code> objects. One test strategy, then, could be to generate a valid <code>CprNumber</code> object, convert it to a string, slightly taint or mangle that string, and then attempt to parse the tainted string. </p> <p> There's more than one way to taint a valid CPR number string. You could, for example, define three functions like these: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;addLeadingSpace&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>&nbsp;+&nbsp;s; <span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;addTrailingSpace&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>; <span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;removeDash&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Replace(<span style="color:#a31515;">&quot;-&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>);</pre> </p> <p> The first function adds a leading space to a string, the second adds a trailing space, and the third removes all dashes it finds. Can you make a generator out of those functions? </p> <p> Yes, you can, with this common general-purpose method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Elements&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">T</span>[]&nbsp;alternatives) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(alternatives&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(alternatives)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;(r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;index&nbsp;=&nbsp;r.Next(alternatives.Length); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;alternatives[index]; &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> This generator randomly selects a value from an array of values, with equal probability. It's a common method, because you'll find equivalent functions in <a href="https://en.wikipedia.org/wiki/QuickCheck">QuickCheck</a>, <a href="https://fscheck.github.io/FsCheck">FsCheck</a>, and <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> (where it's called <code>item</code>). </p> <p> You can write the entire test like this: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ParseCorrectlyHandlesTaintedInput() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;addLeadingSpace&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>&nbsp;+&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;addTrailingSpace&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;removeDash&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Replace(<span style="color:#a31515;">&quot;-&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;tainters&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Gen</span>.Elements( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addLeadingSpace, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addTrailingSpace, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;removeDash, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&gt;&nbsp;addLeadingSpace(removeDash(s)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&gt;&nbsp;addTrailingSpace(addLeadingSpace(s))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;g&nbsp;=&nbsp;tainters.Apply(<span style="color:#2b91af;">Gen</span>.CprNumber.Select(n&nbsp;=&gt;&nbsp;n.ToString())); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;rnd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Random</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;candidate&nbsp;=&nbsp;g.Generate(rnd); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">CprNumber</span>&nbsp;dummy; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">CprNumber</span>.TryParse(candidate,&nbsp;<span style="color:blue;">out</span>&nbsp;dummy); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(actual); }</pre> </p> <p> You first define the three 'tainting' functions, and then create the <code>tainters</code> generator. Notice that this generator not only contains the three functions, but also combinations of these functions. I didn't include all possible combinations of functions, but in <a href="/2018/10/22/applicative-combinations-of-functions">an earlier article</a>, you saw how to do just that. </p> <p> <code>Gen.CprNumber</code> is a <code>Generator&lt;CprNumber&gt;</code>, while you actually need a <code>Generator&lt;string&gt;</code>. Since <code>Generator&lt;T&gt;</code> is a functor, you can easily map <code>Gen.CprNumber</code> with its <code>Select</code> method. </p> <p> You now have a <code>Generator&lt;Func&lt;string, string&gt;&gt;</code> (<code>tainters</code>) and a <code>Generator&lt;string&gt;</code>, so you can combine them using <code>Apply</code>. The result <code>g</code> is another <code>Generator&lt;string&gt;</code> that generates 'tainted', but still valid, CPR string representations. </p> <p> In order to keep the example as simple as possible, the <em>Arrange</em> and <em>Assert</em> phases of the test only checks if <code>CprNumber.TryParse</code> returns <code>true</code>. A better test would also verify that the resulting <code>CprNumber</code> value was correct, but in order to do that, you need to keep the originally generated <code>CprNumber</code> object around so that you can compare this expected value to the actual value. This is possible, but complicates the code, so I left it out of the example. </p> <h3 id="f3d0db787eaf4a018a593daaa78f9e17"> F# Hedgehog <a href="#f3d0db787eaf4a018a593daaa78f9e17" title="permalink">#</a> </h3> <p> You can translate the above unit test to F#, using <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> as a property-based testing library. Another option would be <a href="https://fscheck.github.io/FsCheck">FsCheck</a>. Without further ado, I present to you the test: </p> <p> <pre>[&lt;<span style="color:teal;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Correctly&nbsp;parses&nbsp;tainted&nbsp;text``</span>&nbsp;()&nbsp;=&nbsp;<span style="color:teal;">Property</span>.<span style="color:navy;">check</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">property</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">removeDash</span>&nbsp;(s&nbsp;:&nbsp;<span style="color:teal;">string</span>)&nbsp;=&nbsp;s.<span style="color:navy;">Replace</span>&nbsp;(<span style="color:#a31515;">&quot;-&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">item</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%s</span><span style="color:#a31515;">&nbsp;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">removeDash</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">removeDash</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">&nbsp;&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">string</span>&nbsp;<span style="color:teal;">Gen</span>.cprNumber &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:teal;">Cpr</span>.<span style="color:navy;">tryParse</span>&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">isSome</span><span style="background:yellow;">&nbsp;@&gt;</span>&nbsp;}</pre> </p> <p> As I already mentioned in passing, Hedgehog's equivalent to the above <code>Elements</code> method is called <code>Gen.item</code>. Here, you see the same five functions as above passed in a list. Thanks to partial application and type inference, an expression like <code>sprintf " %s"</code> is already a function of the type <code>string -&gt; string</code>, as is <code>removeDash</code>. For the last of the five functions, I found it easier to write (and read) <code>sprintf " %s "</code> instead of <code>sprintf " %s" &gt;&gt; sprintf "%s "</code>. </p> <p> Equivalent to the C# example, <code>Gen.cprNumber</code> is a <code>Gen&lt;CprNumber&gt;</code>, so mapping it with the built-in <code>string</code> function translates it to a <code>Gen&lt;string&gt;</code>. </p> <p> Hedgehog already includes an <code>&lt;*&gt;</code> operator; <code>Gen</code> is an applicative functor. </p> <h3 id="f7c420f0d4e641a384749187d7bbf217"> Summary <a href="#f7c420f0d4e641a384749187d7bbf217" title="permalink">#</a> </h3> <p> Applicative functors are fairly common. I find it intuitive to think of them as an abstraction of how to make combinations of things. Modelling a Test Data Generator as an applicative functor enables you to create random combinations of functions and values. </p> <p> While working on the Hedgehog example, I discovered another great use of <code>option</code> as an applicative functor. You can see this in the next article. </p> <p> <strong>Next: </strong> <a href="/2018/12/10/danish-cpr-numbers-in-f">Danish CPR numbers in F#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functional architecture: a definition https://blog.ploeh.dk/2018/11/19/functional-architecture-a-definition 2018-11-19T09:44:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you know whether your software architecture follows good functional programming practices? Here's a way to tell.</em> </p> <p> Over the years, I've written articles on functional architecture, including <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">Functional architecture is Ports and Adapters</a>, given <a href="https://vimeo.com/180287057">conference talks</a>, and even produced <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">a Pluralsight course</a> on the topic. How should we define <em>functional architecture</em>, though? </p> <p> People sometimes ask me about their <a href="https://fsharp.org">F#</a> code: <em>How do I know that my F# code is functional?</em> </p> <p> Please permit me a little detour before I answer that question. </p> <h3 id="f752f048f6ef445d8625f1710317620b"> What's the definition of object-oriented design? <a href="#f752f048f6ef445d8625f1710317620b" title="permalink">#</a> </h3> <p> Object-oriented design (OOD) has been around for decades; at least since the nineteen-sixties. Sometimes people get into discussions about whether or not a particular design is good object-oriented design. I know, since I've found myself in such discussions more than once. </p> <p> These discussions usually die out without resolution, because it seems that no-one can provide a sufficiently rigorous definition of OOD that enables people to determine an outcome. One thing's certain, though, so I'd like to posit this corollary to <a href="https://en.wikipedia.org/wiki/Godwin%27s_law">Godwin's law</a>: <blockquote> As a discussion about OOD grows longer, the probability of a comparison involving Alan Kay approaches 1. </blockquote> Not that I, in any way, wish to suggest any logical relationship between <a href="https://en.wikipedia.org/wiki/Alan_Kay">Alan Kay</a> and Hitler, but in a discussion about OOD, sooner or later someone states: <blockquote> "That's not what Alan Kay had in mind!" </blockquote> That may be true, even. </p> <p> My problem with that assertion is that I've never been able to figure out exactly what Alan Kay had in mind. It's something that involves message-passing and <a href="https://en.wikipedia.org/wiki/Smalltalk">Smalltalk</a>, and conceivably, the best modern example of this style of programming might be <a href="https://www.erlang.org">Erlang</a> (often, ironically, touted as a functional programming language). </p> <p> This doesn't seem to be a good basis for determining whether or not something is object-oriented. </p> <p> In any case, despite what Alan Kay had in mind, that wasn't the object-oriented programming we got. While <a href="https://en.wikipedia.org/wiki/Eiffel_(programming_language)">Eiffel</a> is in many ways a strange programming language, the philosophy of OOD presented in <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a> feels, to me, like something from which <a href="https://www.java.com">Java</a> could develop. </p> <p> I'm not aware of the detailed history of Java, but the spirit of the language seems more compatible with Bertrand Meyer's vision than with Alan Kay's. </p> <p> Subsequently, C# would hardly look the way it does had it not been for Java. </p> <p> The OOD we got wasn't the OOD originally envisioned. To make matters worse, the OOD we did get seems to be driven by unclear principles. Yes, there's the idea about encapsulation, but while Meyer had some very specific ideas about design-by-contract, that was the distinguishing trait of his vision that <em>didn't</em> make the transition to Java or C#. </p> <p> It's not clear what OOD is, but I think we can do better when it comes to functional programming (FP). </p> <h3 id="305ccccf2de84354bdb68b5b80d34fc9"> Referential transparency <a href="#305ccccf2de84354bdb68b5b80d34fc9" title="permalink">#</a> </h3> <p> It's possible to pinpoint what FP is to a degree not possible with OOD. Some people may be uncomfortable with the following definition; I don't claim that this is a generally accepted definition. It does have, however, the advantage that it's precise and supports <a href="https://en.wikipedia.org/wiki/Falsifiability">falsification</a>. </p> <p> The foundation of FP is <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>. It's the idea that, for an expression, the left- and right-hand sides of the equal sign are truly equal: </p> <p> <pre>two = 1 + 1</pre> </p> <p> In <a href="https://www.haskell.org">Haskell</a>, this is enforced by the compiler. The <code>=</code> operator truly implies equality. To be clear, this isn't the case in C#: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;two&nbsp;=&nbsp;1&nbsp;+&nbsp;1;</pre> </p> <p> In C#, Java, and other imperative languages, the <code>=</code> implies <em>assignment</em>, not equality. Here, <code>two</code> can change, despite the absurdity of the claim. </p> <p> When code is referentially transparent, then you can substitute the expression on the right-hand side with the symbol on the left-hand side. This seems obvious when we consider addition of two numbers, but becomes less clear when we consider function invocation: </p> <p> <pre>i = findBestNumber [42, 1337, 2112, 90125]</pre> </p> <p> In Haskell, functions are referentially transparent. You don't know exactly what <code>findBestNumber</code> does, but you do know that you can substitute <code>i</code> with <code>findBestNumber [42, 1337, 2112, 90125]</code>, or vice versa. </p> <p> In order for a function to be referentially transparent (also known as a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>), it must have two properties: <ul> <li>It must always return the same output for the same input. We call this quality <em>determinism</em>.</li> <li>It must have no side effects.</li> </ul> As far as I can tell, all else in FP follows from this definition. For example, values must be immutable, because if they aren't, you could mutate them, and that would count as a side effect. </p> <p> The reason I prefer this definition is that it supports falsification. You can assert that a function or value is pure; all it takes is a single counter-example to prove that it's not. A counter-example can be either an input value that doesn't always produce the same return value, or a function call that produces a side effect. </p> <p> I'm not aware of any other definition that offers similar decision power. </p> <h3 id="43505c00cc25416db2b47a89912fd731"> IO <a href="#43505c00cc25416db2b47a89912fd731" title="permalink">#</a> </h3> <p> All software produces side effects: Changing a pixel on a monitor is a side effect. Writing a byte to disk is a side effect. Transmitting a bit over a network is a side effect. It seems that it'd be impossible to interact with pure functions, and indeed, it is, without some sort of affordance for impurity. </p> <p> Haskell resolves this problem with the <code>IO</code> monad, but the purpose of this article isn't to serve as an introduction to Haskell, monads, or <code>IO</code>. The point is only that in FP, you need some sort of 'wormhole' that will enable you to interact with the real world. There's no way around that, but logically, the rules still apply. Pure functions must stay deterministic and free of side effects. </p> <p> It follows that you have two groups of operations: impure activities and pure functions. </p> <p> <img src="/content/binary/impure-actions-pure-functions-no-arrows.png" alt="Two sets: the set of impure activities and the set of pure functions."> </p> <p> While there are rules for pure functions, those rules still allow for interaction. One pure function can call another pure function. Such an interaction doesn't change the properties of any of those functions. Both caller and callee remain side-effect-free and deterministic. </p> <p> <img src="/content/binary/impure-actions-pure-functions-pure-arrows.png" alt="Set of impure activities and set of pure functions. The pure functions now have arrows between them."> </p> <p> The impure activities can also interact. No rules apply to them: </p> <p> <img src="/content/binary/impure-actions-pure-functions-impure-and-pure-arrows.png" alt="Sets of impure activities and pure functions. Both sets now have internal arrows."> </p> <p> Finally, since no rules apply to impure activities, they can invoke pure functions: </p> <p> <img src="/content/binary/impure-actions-pure-functions-all-valid-arrows.png" alt="Sets of impure activities and pure functions, now also with arrows going from impure to pure."> </p> <p> Impure activities are unbound by rules, so they can do anything they need to do, including painting pixels, writing to files, or calling pure functions. A pure function is deterministic and has no side effects. Those properties don't change just because the result is subsequently displayed on a screen. </p> <p> The fourth combination of arrows is, however, illegal. <blockquote> A pure function can't invoke an impure activity. </blockquote> If it did, it would either transitively produce a side effect or non-deterministic behaviour. </p> <p> This is the rule of functional architecture. You can also explain it with a table: <table> <col> <col> <colgroup span="2"></colgroup> <thead> <tr> <td colspan="2" rowspan="2"></td> <th colspan="2" scope="colgroup">Callee</th> </tr> <tr> <th scope="col">Impure</th> <th scope="col">Pure</th> </tr> </thead> <tbody> <tr> <th rowspan="2" scope="rowgroup">Caller</th> <th scope="row">Impure</th> <td><span style="color:green">Valid</span></td> <td><span style="color:green">Valid</span></td> </tr> <tr> <th scope="row">Pure</th> <td><span style="color:red">Invalid</span></td> <td><span style="color:green">Valid</span></td> </tr> </tbody> </table> Let's call the above rule the <em>functional interaction law</em>: a pure function can't invoke an impure activity. A functional architecture, then, is a code base that obeys that law, and has a significant portion of pure code. </p> <p> Clearly, you can trivially obey the functional interaction law by writing exclusively impure code. In a sense, this is what you do by default in imperative programming languages. If you're familiar with Haskell, imagine writing an entire program in <code>IO</code>. That would be possible, but pointless. </p> <p> Thus, we need to add the qualifier that a significant part of the code base should consist of pure code. How much? The more, the better. Subjectively, I'd say significantly more than half the code base should be pure. I'm concerned, though, that stating a hard limit is as useful here <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">as it is for code coverage</a>. </p> <h3 id="1009a90ea922424285e5f9a4bb30524e"> Tooling <a href="#1009a90ea922424285e5f9a4bb30524e" title="permalink">#</a> </h3> <p> How do you verify that you obey the functional interaction law? Unfortunately, in most languages the answer is that this requires painstaking analysis. This can be surprisingly tricky to get right. Consider this realistic F# example: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;createEmailNotification&nbsp;templates&nbsp;msg&nbsp;(user&nbsp;:&nbsp;UserEmailData)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;{&nbsp;SubjectLine&nbsp;=&nbsp;subjectTemplate;&nbsp;Content&nbsp;=&nbsp;contentTemplate&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;templates &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Map.tryFind&nbsp;user.Localization &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.defaultValue&nbsp;(Map.find&nbsp;Localizations.english&nbsp;templates) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;r&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Templating.append &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Templating.replacementOfEnvelope&nbsp;msg) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Templating.replacementOfFlatRecord&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;subject&nbsp;=&nbsp;Templating.run&nbsp;subjectTemplate&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;content&nbsp;=&nbsp;Templating.run&nbsp;contentTemplate&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RecipientUserId&nbsp;=&nbsp;user.UserId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EmailAddress&nbsp;=&nbsp;user.EmailAddress &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotificationSubjectLine&nbsp;=&nbsp;subject &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotificationText&nbsp;=&nbsp;content &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CreatedDate&nbsp;=&nbsp;DateTime.UtcNow &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Is this a pure function? </p> <p> You may protest that this isn't a fair question, because you don't know what, say, <code>Templating.replacementOfFlatRecord</code> does, but that turns out to be irrelevant. The presence of <code>DateTime.UtcNow</code> makes the entire function impure, because getting the current date and time is non-deterministic. This trait is transitive, which means that any code that calls <code>createEmailNotification</code> is also going to be impure. </p> <p> That means that the purity of an expression like the following easily becomes obscure. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;emailMessages&nbsp;=&nbsp;specificUsers&nbsp;|&gt;&nbsp;Seq.map&nbsp;(createEmailNotification&nbsp;templates&nbsp;msg)</pre> </p> <p> Is this a pure expression? In this case, we've just established that <code>createEmailNotification</code> is impure, so that wasn't hard to answer. The problem, however, is that the burden is on you, the code reader, to remember which functions are pure, and which ones aren't. In a large code base, this soon becomes a formidable endeavour. </p> <p> It'd be nice if there was a tool that could automatically check the functional interaction law. </p> <p> This is where many people in the functional programming community become uncomfortable about this definition of functional architecture. The only tools that I'm aware of that enforce the functional interaction law are a few programming languages, most notably Haskell (others exist, too). </p> <p> Haskell enforces the functional interaction law via its <code>IO</code> type. You can't use an <code>IO</code> value from within a pure function (a function that doesn't return <code>IO</code>). If you try, your code doesn't compile. </p> <p> I've personally used Haskell repeatedly to understand the limits of functional architecture, for example to establish that <a href="/2017/01/30/partial-application-is-dependency-injection">Dependency Injection isn't functional</a> because it makes everything impure. </p> <p> The overall lack of tooling, however, may make people uncomfortable, because it means that most so-called functional languages (e.g. F#, Erlang, <a href="https://elixir-lang.org/">Elixir</a>, and <a href="https://clojure.org">Clojure</a>) offer no support for validating or enforcing functional architecture. </p> <p> My own experience with writing entire applications in F# is that I frequently, inadvertently violate the functional interaction law somewhere deep in the bowels of my code. </p> <h3 id="8e33b2ffd51f46f18e38c5add37f1728"> Conclusion <a href="#8e33b2ffd51f46f18e38c5add37f1728" title="permalink">#</a> </h3> <p> What's functional architecture? I propose that it's code that obeys the functional architecture law, and that is made up of a significant portion of pure functions. </p> <p> This is a narrow definition. It excludes a lot of code bases that could easily be considered 'functional enough'. By the definition, I don't intend to denigrate fine programming languages like F#, Clojure, Erlang, etcetera. I personally find it a joy to write in F#, which is my default language choice for .NET programming. </p> <p> My motivation for offering this definition, albeit restrictive, is to avoid the OOD situation where it seems entirely subjective whether or not something is object-oriented. With the functional interaction law, we may conclude that most (non-Haskell) programs are probably not 'really' functional, but at least we establish a falsifiable ideal to strive for. </p> <p> This would enable us to look at, say, an F# code base and start discussing <em>how close to the ideal is it?</em> </p> <p> Ultimately, functional architecture isn't a goal in itself. It's a means to achieve an objective, such as a sustainable code base. I find that FP helps me keep a code base sustainable, but often, 'functional enough' is sufficient to accomplish that. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="ccf908a42b644b8b9bb2bf6bcbc25052"> <div class="comment-author"> <a href="https://github.com/MaxKot">Max Kiselev</a> <a href="#ccf908a42b644b8b9bb2bf6bcbc25052">#</a></div> <div class="comment-content"> <p> Good idea of basing the definition on falsifiability! </p> <p> The createEmailNotification example makes me wonder though. If it is not a functional design, then what design it actually is? I mean it has to be some design and it does not looks like object-oriented or procedural one. </p> </div> <div class="comment-date">2018-11-20 21:36 UTC</div> </div> <div class="comment" id="6a4ca5d3da3e4a63a413982f589c384c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6a4ca5d3da3e4a63a413982f589c384c">#</a></div> <div class="comment-content"> <p> Max, thank you for writing. I'm not sure whether the assertion that it <em>has</em> to be some design or other is axiomatically obvious, but I suppose that we humans have an innate need to categorise things. </p> <p> Don Syme calls F# a <em>functional-first</em> language, and that epithet could easily apply to that style of programming as well. Other options could be <em>near-functional</em>, or perhaps, if we're in a slightly more academic mood, <em>quasi-functional</em>. </p> <p> In daily use, we'd probably still call code like that <em>functional</em>, and I don't think it'll cause much confusion. </p> <p> If I remember the history of programming correctly, the first attempts at functional programming didn't focus on referential transparency, but simply on the concept of functions as first-class language features, including lambda expressions and higher-order functions. The little I know of <a href="https://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a> corroborates that view on the history of functional programming. </p> <p> Only later (I think) did languages appear that make compile-time distinction between pure and impure code. </p> <p> My purpose with this article wasn't to exclude a large number of languages or code bases from being functional, but just to offer a falsifiable test that we can use for evaluation purposes, if we're ever in doubt. This is something that I feel is sorely missing from the object-oriented debate, and while I can't think of a way to remedy the OOD situation, I hope that the present definition of functional architecture presents firmer ground upon which to have a debate. </p> </div> <div class="comment-date">2018-11-21 6:42 UTC</div> </div> <div class="comment" id="902887b4a1954daf8be09e35ce685fbb"> <div class="comment-author"><a href="https://github.com/wachulski">Marcin Wachulski</a> <a href="#902887b4a1954daf8be09e35ce685fbb">#</a></div> <div class="comment-content"> <p> Thank you for your definition that forms good grounds for reasoning about functional property of code. I remember some people say that even C# is functional as it allows for <em>delegates</em>. Several thoughts: </p> <p> 1. Remember <a href="https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts.pureattribute?view=netcore-2.1">[Pure]</a> attribute and CodeContracts? It aided the purpose of defensive programming to hold pre/postconditions and invariants. I have this impression that both functional purity and defensive programming help us find ad-hoc quasi-mathematical proofs of code correctness after we load a codebase to our heads. It's way easier then to presume how it works and make changes. Of course it's not mathematical in the strict sense, but still - we tend to trust the code more (e.g. threading). I'm pretty sure unit tests belong to this family too. </p> <p> 2. Isn't the purity concept anywhere close to gateways (code that crosses the boundaries of program determinism control zone, e.g. IO, time, volatile memory)? It's especially evident while refactoring legacy code to make it unit testable. We often extract out infrastructure dependent parts (impure activities, e.g. DateTime.Now) for the other be deterministic (pure) and hence - testable. </p> <p> 3. Can the whole program/system be as pure as this: <pre>INPUT -> PURE_CODE -> OUTPUT</pre> I'm afraid not as it'd mean the pure code needed to know all the required input data upfront. That's impossible most of times. So, when it comes to measures, I'd argue that the number of pure code lines is enough to tell how pure the codebase is. I'd accompany this with e.g. percentile distribution of pure chunk length (functions/blocks of contingent im/purity etc.). E.g. I'd personally favour 1) over 2) in the following: <pre> 1. INPUT -> 3 x LONG_PURE_CHUNK + 2 x LONG_IMPURE_CHUNK -> OUTPUT 2. INPUT -> 10 x SHORT_PURE_CHUNK + 7 x SHORT_IMPURE_CHUNK -> OUTPUT </pre> </p> <p> 4. I'd love to see such tools too :) I believe the purity concept does not pertain only to FP nor OO and is related to the early foundational days of computer programming with mathematical proofs they had. </p> </div> <div class="comment-date">2018-11-23 10:51 UTC</div> </div> <div class="comment" id="82b14ead068b42698186710ba7bcf145"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#82b14ead068b42698186710ba7bcf145">#</a></div> <div class="comment-content"> <p> Marcin, thank you for writing. To be clear, you can perform side effects or non-deterministic behaviour in C# delegates, so delegates aren't guaranteed to be referentially transparent. In fact, <a href="/2018/01/22/function-isomorphisms">delegates, and particularly closures, are isomorphic to objects</a>. In C#, <a href="/2014/03/10/solid-the-next-step-is-functional">they even compile to classes</a>. </p> <p> I'm aware that some people think that they're doing functional programming when they use lambda expressions in C#, but I disagree. </p> <p> I'll see if I can address your other comments below. </p> <h3 id="c085eef3102d437ba9c6cbe3363b8976"> Re: 1. <a href="#c085eef3102d437ba9c6cbe3363b8976" title="permalink">#</a> </h3> <p> Some functions are certainly ad-hoc, while <a href="/2017/10/04/from-design-patterns-to-category-theory">others are grounded in mathematics</a>. I agree that pure functions 'fit better in your brain', mostly because it's clear that the only stimuli that can affect the outcome of the function is the input. Granted, if we imagine a concrete, ad-hoc function that takes 23 function arguments, we might still consider it bad code. Small functions, though, tend to be easy to reason about. </p> <p> I've previously written about the <a href="/2016/02/10/types-properties-software">relationship between types and tests</a>, and I believe that there's some sort of linearity in that relationship. The better the type system, the fewer tests you need. The weaker the type system, the more tests you need. It's no wonder that the unit testing culture related to languages like Ruby seems stronger than in the Haskell community. </p> <h3 id="405184fce774458ba8d3484c2580f322"> Re: 2. <a href="#405184fce774458ba8d3484c2580f322" title="permalink">#</a> </h3> <p> In my experience, most people make legacy code more testable by introducing some variation of Dependency Injection. This may enable you to control some otherwise non-deterministic behaviour from tests, but <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">it doesn't make the design more functional</a>. Those types of dependencies (e.g. on <code>DateTime.Now</code>) are inherently impure, and therefore they make everything that invokes them impure as well. The above <em>functional interaction law</em> excludes such a design from being considered functional. </p> <p> <a href="/2015/05/07/functional-design-is-intrinsically-testable">Functional code, on the other hand, is intrinsically testable</a>. Watch out for a future series of articles that show how to move an object-oriented architecture towards something both more functional, more sustainable, and more testable. </p> <h3 id="5201a19bec7b413ebb6e7ca3e2dfbc81"> Re: 3. <a href="#5201a19bec7b413ebb6e7ca3e2dfbc81" title="permalink">#</a> </h3> <p> A command-line utility <em>could</em> be as pure as you suggest, but most other programs will need to at least <ol> <li>load some more data from impure sources, such as files, databases, or the current time,</li> <li>run some pure functions,</li> <li>output the results to some impure destinations, such as files, databases, UI, email, and so on.</li> </ol> I call this type of architecture a <a href="/2017/02/02/dependency-rejection">impure-pure-impure sandwich</a>, and you can <a href="/2017/07/10/pure-interactions">often, but not always, structure your application code in that way</a>. </p> <h3 id="e8befe231edf474899477eb39074d8d0"> Re: 4. <a href="#e8befe231edf474899477eb39074d8d0" title="permalink">#</a> </h3> <p> I'm not sure I understand what you mean by that comment, but the mathematical proofs about computability pre-date computer programming. Gödel's work on recursive functions is from 1933, Church's work on lambda calculus is from 1936, and Turing's paper <em>On Computable Numbers, with an Application to the Entscheidungsproblem</em> is from later in 1936. Turing and Church later showed that <a href="https://en.wikipedia.org/wiki/Church%E2%80%93Turing_thesis">all three definitions of computability are equivalent</a>. </p> <p> I don't know much about Gödel's work, but lambda calculus is defined entirely on the foundation of <em>functions</em>, while the Turing machines described in Turing's paper have nothing to do with functions in the mathematical sense. </p> <p> Mathematical functions are, however, referentially transparent. They're also <em>total</em>, meaning that they always return a result for any input in the function's domain. Due to the halting problem, a Turing-complete language can't guarantee that all functions are total. </p> </div> <div class="comment-date">2018-11-24 9:04 UTC</div> </div> <div class="comment" id="dd673d8d41de4ab6a4a7abce333848af"> <div class="comment-author"><a href="https://github.com/wallymathieu">Oskar Gewalli</a> <a href="#dd673d8d41de4ab6a4a7abce333848af">#</a></div> <div class="comment-content"> <p> Mathematical functions are not always defined for all input, as seen with division (since it's only defined in &#8477; &#8726; {0}, real numbers without 0). We see for instance the term <em>partial</em> applied instead of total. You could say that the domain of division in &#8477; is &#8477; &#8726; {0}. </p> <p> Having total functions for some space S where the domain of the function is the same as S are nice to have, but not we are not always that fortunate. </p> </div> <div class="comment-date">2019-08-28 20:41 UTC</div> </div> <div class="comment" id="df1d737c7fa947ccad5eb7df5a368288"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#df1d737c7fa947ccad5eb7df5a368288">#</a></div> <div class="comment-content"> <p> Oskar, yes, you're right. I admit that what I wrote about totality above was a bit too sweeping. If we really start to dissect what I wrote, it's wrong on more levels. Clearly, mathematical functions (or, at least, algorithms) that never return exist; an algorithm to calculate all the decimals of &#960; would be an example. </p> <p> If I understand the results that both Church and Gödel arrived at, such algorithms can be encoded in such a way that we can also consider them mathematical functions. If so, then not all mathematical functions are total. </p> <p> As you may be able to tell, it wasn't entirely clear to me what I was supposed to respond to, and it seems (now that I reread what I wrote) that I managed to write a few sentences that don't make clear sense. My main point, however, seems to have been that we can't write a general-purpose tool that'll be able to determine whether or not any arbitrary function is total or not. Does that part seem reasonable? </p> </div> <div class="comment-date">2019-08-29 6:14 UTC</div> </div> <div class="comment" id="3521b157f09647dfb93ef63d28922bf6"> <div class="comment-author"><a href="https://robinpokorny.com">Robin Pokorny</a> <a href="#3521b157f09647dfb93ef63d28922bf6">#</a></div> <div class="comment-content"> <p> Thanks for this article. I feel there is a huge benefit of having a precise definition like that. </p> <p> I want to point out that there is one more dimension in deciding how far a piece of code is from ‘the ideal functional architecture’. And that is developer's intention. I know, that sound stupid. And technically that code is not pure. But we are talking about how close to the ideal it is. </p> <p> This is a problem I discovered some time ago. Especially in dynamic languages even the code that looks pure might not be strictly speaking pure because of some dynamic behaviour of the language and other quirks. There are some examples in JavaScript in this article: <a href="https://medium.com/hackernoon/do-pure-functions-exist-in-javascript-b128ed5f0ed2">Do pure functions exist in JavaScript?</a>. </p> <p> Why is this a different dimention? For F# code there could exist a static analysis tool that would discover usage of impure function down in the code base. That is simply impossible in e.g. JavaScript. So I suggest that the intention the developer had and which is manifested in the code (assuming no harmful behaviour) should also play its role. </p> <p> What do you and others think? Should such a soft view be considered? </p> </div> <div class="comment-date">2019-10-16 21:03 UTC</div> </div> <div class="comment" id="02434876208f4a658ef71c8401d658ad"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#02434876208f4a658ef71c8401d658ad">#</a></div> <div class="comment-content"> <p> Robin, thank you for writing. If I understand you correctly, you're asking whether we should consider code functional if the programmer intended it to be functional? </p> <p> To be clear, I don't think that a static analysis tool like the one you suggest could be made for a language like F#. Not only do you need to analyse all of your own code, but if your code calls other libraries, you need to know whether or not these other libraries are pure as well. That's not a trivial undertaking. </p> <p> My point is that even in F#, a programmer could intend to write pure code, yet still inadvertently fail to do so. Should we still consider it functional? </p> <p> I admit that I haven't thought deeply about this, but I'd be inclined to say no. I'm sure that you can think of many examples, from other walks of life, where good intentions led to bad outcomes. </p> <p> The goal of functional architecture is ultimately larger than just purity for the sake of purity. Why is functional programming valuable? I believe that it's valuable because a pure function holds few surprises. When you call a pure function, it doesn't have unintended side-effects. The lack of state mutation eradicates <a href="https://en.wikipedia.org/wiki/Aliasing_(computing)">aliasing</a> bugs. </p> <p> If a programmer <em>intends</em> to write functional code, but nevertheless leaves behind many surprises in the code, the potential of functional programming remains unfulfilled. </p> <p> Perhaps I misunderstood what you meant, so please elaborate if I missed your point. </p> </div> <div class="comment-date">2019-10-17 20:56 UTC</div> </div> <div class="comment" id="31561e12ab854257b0d25d46a8e2f690"> <div class="comment-author"><a href="https://github.com/ShalokShalom">Matthias Schuster</a> <a href="#31561e12ab854257b0d25d46a8e2f690">#</a></div> <div class="comment-content"> <p> You mention, that most languages provide no way to determine whatever or not, a function is pure. I agree, that this is an oversight in functional programming and while FSharp is my most favorite language, is there one that is, you could say, quite surprisingly well defined in this regard. Surprisingly because it is not a functional language per se. It supports nearly all the important concepts, while not something like an HMD type system and immutable data structures. </p> <p> I am speaking about <a href="https://nim-lang.org/">Nim</a>. It has the keyword <em>func</em> for pure functions and opposite to this the keyword <em>proc</em> in order to perform procedures. In that context is it already obvious, what is the cause for all this confusion: </p> <p> Calling one thing function, while it is actually something impure, has let to the acceptance that functions <em>can</em> be impure. </p> And this is the cause of all this trouble: Both in programming, and as well about this specific topic. Talking about implicit versus explicit as well. </div> <div class="comment-date">2020-10-20 11:40 UTC</div> </div> <div class="comment" id="5a9d194c05594eadb04c010e90c1d4a4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5a9d194c05594eadb04c010e90c1d4a4">#</a></div> <div class="comment-content"> <p> Matthias, thank you for writing. I didn't know about Nim. </p> <p> I agree that it's confusing to call an impure procedure a <em>function</em>. These days, I try to stay away form that word in that context, and instead talk about <em>impure actions</em> or, as I did in this article, <em>impure activities</em>. </p> </div> <div class="comment-date">2020-10-23 7:50 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. What to test and not to test https://blog.ploeh.dk/2018/11/12/what-to-test-and-not-to-test 2018-11-12T07:45:00+00:00 Mark Seemann <div id="post"> <p> <em>Should you unit test everything? Hardly surprising, the answer is that It Depends™. This article outlines some of the circumstances you might consider.</em> </p> <p> Some years ago, I, somewhat to my own surprise, found myself on the wrong side of a controversy about whether one should <a href="/2013/03/08/test-trivial-code">test trivial code</a>. The context was a discussion about Test-Driven Development (TDD), and for reasons that I still stand behind today, I argued that you should test all code, including trivial code, such as property getters. </p> <p> Most of the 'TDD community' reacted quite negatively to that article, some in not-so-nice ways. Some people reacted, I believe, based on their dislike of the conclusion, without responding to my arguments. Others, however, gave reasoned rebuttals. When people like <a href="https://lostechies.com/derickbailey/2013/03/11/on-testing-trivia-code/">Derick Bailey</a> and <a href="https://rendlelabs.com/blog/dont-unit-test-trivial-code">Mark Rendle disagree with me</a>, in a reasoned matter, even, I consider that a good reason to revisit my thinking. </p> <p> Could I have been wrong? That certainly wouldn't be the first time, but even re-reading the article today, I find my logic sound. Yet, I've substantially nuanced my position since then. </p> <p> It took me some time to understand how I could disagree so radically with people I respect. It didn't take me five years, though, but while I've been at peace with the apparent conflict for years, I've never written a coherent description of my current understanding of this problem space. This article is my attempt to remedy that omission. </p> <h3 id="eb43b60a35394df6b812514ac794cefe"> Context matters <a href="#eb43b60a35394df6b812514ac794cefe" title="permalink">#</a> </h3> <p> Whenever you consult an expert about how to address a problem, you'll invariably get the answer that <em>it depends</em>. I'd suggest that if you don't get that answer, the person is probably not an expert, after all. A useful expert will also be able to tell you on <em>what</em> 'it' depends. </p> <p> In an abstract sense, what 'it' depends on is <em>the context</em>. </p> <p> I wrote my original piece from a particular context. Part of that context is explicitly present in the article, but another part is entirely implicit. People read the article from within their own contexts, which in many cases turned out to be incongruent with mine. No wonder people disagreed. </p> <h3 id="9181a2688ce042d882cfe73a3bfed57e"> Watching the wildlife <a href="#9181a2688ce042d882cfe73a3bfed57e" title="permalink">#</a> </h3> <p> My context at that time was that I had some success with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>, an open source library, which is what I consider <a href="/2012/12/18/RangersandZookeepers">wildlife</a> software. Once you've released a version of the software, you have no control of where it's installed, or how it's used. </p> <p> This means that backwards compatibility becomes important. If I broke something, I would inconvenience the users of my software. Making sure that compatibility didn't break became one of my highest priorities. I used unit tests for regression tests, and I did, indeed, test the entire public API of AutoFixture, to make sure that no breaking changes were introduced. </p> <p> That was my implicit context. Read in that light, my dogmatic insistence on testing everything hopefully makes a little more sense. </p> <p> Does that mean that my conclusion transfers to other circumstances? No, of course it doesn't. If you're developing and maintaining <a href="/2012/12/18/RangersandZookeepers">zoo software</a>, breaking changes are of less concern. From that perspective, my article could easily look like the creation of an unhinged mind. </p> <h3 id="a15643275c1a4d7ab484cacafa53b7da"> The purpose of tests <a href="#a15643275c1a4d7ab484cacafa53b7da" title="permalink">#</a> </h3> <p> In order to figure out what to test, and what not to test, you should ask yourself the question: <em>what's the purpose of testing?</em> </p> <p> At first glance, that may seem like an inane question, but there's actually more than one purpose of a unit test. When doing TDD, the purpose of a test is to provide feedback about the API you're developing. <a href="/2011/11/10/TDDimprovesreusability">A unit test is the first client of the production API</a>. If a test is difficult to write, the production API is difficult to use. More on TDD later, though. </p> <p> You may say that another purpose of automated tests is that they prevent errors. That's not the case, though. Automated tests prevent <em>regressions</em>. </p> <p> If you wrote the correct test, your test suite may also help to prevent errors, but a unit test is only as good as the programmer who wrote it. You could have made a mistake when you wrote the test. Or perhaps you misunderstood the specification of what you were trying to implement. <a href="/2013/04/02/why-trust-tests">Why do you even trust tests?</a> </p> <h3 id="d5b17ea45ed741899002d50fbb3e98c9"> The cost of regressions <a href="#d5b17ea45ed741899002d50fbb3e98c9" title="permalink">#</a> </h3> <p> Why do you want to prevent regressions? Because they're costly? </p> <p> Based on the little risk management I know about, you operate with two dimensions of risk: the impact of an event, should it occur, and the probability that the event occurs. </p> <p> Should we all be worried that an asteroid will hit the Earth and wipe out most life? The impact of such an event is truly catastrophic, yet the probability is diminishingly small, so the <em>risk</em> is insignificant. The risk of going for a drive in a car is much higher. </p> <p> How do you reduce risk? You either decrease the probability that the adverse event will happen, or you reduce the impact of it, should it happen. </p> <p> <a href="http://www.higherorderlogic.com/">Steve Freeman</a> once wrote a nice article about the distinction between fail-safe software, and software that could safely fail. Unfortunately, that article seems to have disappeared from the internet. The point, however, was that with unit tests, we attempt to make our software fail-safe. The unit tests act as a gate that prevents bad versions of the software from being released. That's not a bad strategy for managing risk, but only half of the strategies available. </p> <p> For example, <a href="http://amzn.to/1axt5YA">Continuous Delivery</a> describes how you can use <a href="https://martinfowler.com/bliki/CanaryRelease.html">Canary Releases</a> and automated rollbacks to reduce the impact of errors. That's what Steve Freeman called <em>safe fail</em>. </p> <p> I apologise for this detour around risk management, but I think that it's important that you make an explicit decision about automated testing. You can use unit tests to prevent regressions. What's the impact of an error in production, though? </p> <p> This depends on the type of software you're developing. When considering alternatives, I often envision the various options as inhabiting a continuum: </p> <p> <img src="/content/binary/test-coverage-continuum.png" alt="Test coverage continuum; no coverage to the left, maximum coverage to the right."> </p> <p> For some types of software, an error 'in production' could be fatal. This would be the case for guidance software for <a href="https://en.wikipedia.org/wiki/Voyager_1">Voyager 1</a>, <a href="https://en.wikipedia.org/wiki/Voyager_2">2</a>, other guidance software, software for medical devices, and so on. If you deploy a defect to Voyager 2, you've probably lost the craft for ever. </p> <p> (I'd be surprised if the Voyager guidance software is actually covered by unit tests, but I'd expect that other quality assurance checks are in place. For comparison, the space shuttle software development process has been appraised at CMMI level 5.) </p> <p> On the other side of the continuum, as a software developer, you probably write small ad-hoc development tools for your own use. For example, a few years ago I did a lot of <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> API development, and many of the APIs I worked with required <a href="https://en.wikipedia.org/wiki/OAuth">OAuth</a> authentication. I wrote a little command-line program that I could use to log on to an internal system and exchange that to a token. I don't think that I wrote any tests for that program. If there were problems with it, I'd just open the source code and fix the problem. Errors were cheap in that situation. </p> <p> Most software probably falls somewhere in the middle of those extremes. The cost of errors in wildlife software is probably higher than it is for zoo software, but most software can get by with less coverage than <em>everything</em>. </p> <h3 id="7db262526ca2459b813c0fda4f1e6999"> Cyclomatic complexity <a href="#7db262526ca2459b813c0fda4f1e6999" title="permalink">#</a> </h3> <p> How do you know that your software works? You test it. If you want to automate your testing efforts, you write unit tests... but a unit test suite is software. How do you know that your tests work? Is it going to be <a href="https://en.wikipedia.org/wiki/Turtles_all_the_way_down">turtles all the way down</a>? </p> <p> I think that we can <a href="/2013/04/02/why-trust-tests">trust tests for other reasons</a>, but one of them is that each test case exercises a deterministic path through a unit that supports many paths of execution. </p> <p> <img src="/content/binary/one-test-path-through-complex-unit.png" alt="Diagram that shows a unit test exercising one path through a unit."> </p> <p> In other words, each unit test is an example of a singular execution path. Tests, then, should have a <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of 1. In other words, you write (test) code with a cyclomatic complexity of 1 in order to test code with a higher cyclomatic complexity. </p> <p> Should you test code that has a cyclomatic complexity of 1? </p> <p> What would be the point of that? Why would you write a unit test with a cyclomatic complexity of 1 to test a piece of code with a cyclomatic complexity of 1? Wouldn't you just be adding more code? </p> <p> From the perspective of <em>trusting</em> the code, there's no reason to trust such a test more than the code that it exercises. In that light, I think it makes sense to <em>not</em> write that test. </p> <p> To be clear, there could be other reasons to test code with a cyclomatic complexity of 1. One reason, that I pointed out in my original article, is that you don't know if the simple piece of code will <em>stay</em> simple. Another reason is to prevent regressions. A common metaphor for unit testing is <a href="http://en.wikipedia.org/wiki/Double-entry_bookkeeping_system">double-entry bookkeeping</a>. If you write the unit test in a different way than the implementation, the two views on that behaviour may keep each other in check. You could do that with triangulation using <a href="http://xunitpatterns.com/Parameterized%20Test.html">parametrised tests</a>, or perhaps with property-based testing. </p> <p> I tend to use a heuristic where the farther to the left I am on the above continuum, the more I'm inclined to skip testing of simple functionality. Code with a cyclomatic complexity of 1 falls into that bucket. </p> <h3 id="5899a274dd0f4697b86772d3d0a5d6ce"> TDD <a href="#5899a274dd0f4697b86772d3d0a5d6ce" title="permalink">#</a> </h3> <p> Let's return our attention to TDD. The previous paragraphs have mostly discussed automated tests as a way to prevent regressions. TDD gives us an entirely different motivation for writing tests: the tests provide feedback on the design of our production code. </p> <p> Viewed like this, the tests themselves are only artefacts of the TDD process. It's usually a good idea to keep them around after the standard red-green-refactor cycle, because they serve double-duty as regression tests. </p> <p> Should you test-drive everything? If you're inexperienced with TDD, you get the best exercise by test-driving as much as possible. This still doesn't have to mean that you must write a an explicit test case for each class member. That's what both Mark Rendle and Derick Bailey pointed out. It's often enough if the tests somehow exercise those members. </p> <p> Revisiting my old article, my mistake was that I conflated TDD with regression testing. My motivation for writing an explicit test case for each member, no matter how trivial, was to preserve backwards compatibility. It really had nothing to do with TDD. </p> <h3 id="f0834719ee414d7e81a32c7cb32e8256"> When in doubt <a href="#f0834719ee414d7e81a32c7cb32e8256" title="permalink">#</a> </h3> <p> Despite all other rules of thumb I've so far listed, I'll suggest a few exceptions. </p> <p> Even if a piece of code theoretically has a cyclomatic complexity of 1, if you're in doubt of how it works, then write a test. </p> <p> If you have a defect in production, then reproduce that defect with one or more tests, even if the code in question is 'trivial'. Obviously, it wasn't trivial after all, if it caused a bug in production. </p> <h3 id="51cfec77312e45f7a2dd60b55096ba17"> Pragmatism <a href="#51cfec77312e45f7a2dd60b55096ba17" title="permalink">#</a> </h3> <p> When you're learning something new, you're typically struggling with even the most basic concepts. That's just how learning works. In that phase of learning, it often pays to follow explicit rules. A way to think about this is the <a href="http://en.wikipedia.org/wiki/Dreyfus_model_of_skill_acquisition">Dreyfus model of skill acquisition</a>. Once you gain some experience, you can start deviating from the rules. We could call this <em>pragmatism</em>. </p> <p> I often discuss TDD with people who plead for pragmatism. Those people have typically practised TDD for years, if not decades. So have I, and, believe it or not, I'm often quite pragmatic when I practice TDD 'for real'. This is, however, a prerogative of experience. <blockquote> You can only be pragmatic if you know how to be dogmatic. </blockquote> I use the concept of <em>dogmatism</em> as an antonym to <em>pragmatism</em>. I view pragmatism in programming as the choice of practical solutions over theoretical principles. It's a <em>choice</em>, which means that you must be aware of alternatives. </p> <p> If you don't know the (principled) alternative, there's no choice. </p> <p> When you're learning something new, you're still not aware of how to do things according to principle. That's natural. I find myself in that situation all the time. If you keep at it, though, eventually you'll have gained enough experience that you can make actual choices. </p> <p> This applies to TDD as well. When you're still learning TDD, stick to the principles, particularly when it's inconvenient. Once you've done TDD for a few years, you've earned the right to be pragmatic. </p> <h3 id="11ce36adfdeb4b8e8c6b640e28691aa0"> Conclusion <a href="#11ce36adfdeb4b8e8c6b640e28691aa0" title="permalink">#</a> </h3> <p> Which parts of your code base should you (unit) test? It Depends™. </p> <p> It depends on why you are unit testing, and on the cost of defects in production, and probably many other things I didn't think of. </p> <p> What's the purpose of tests? Are you using TDD to get feedback on your API design ideas? Or is the main purpose of tests to prevent regressions? Your answers to such questions should guide your decisions on how much to test. </p> <p> Recently, I've mostly been writing about topics related to computer science, such as the <a href="/2017/10/04/from-design-patterns-to-category-theory">relationships between various branches of mathematics to computation</a>. In such realms, laws apply, and answers tend to be either right or wrong. A piece like this article is different. </p> <p> This is fundamentally a deeply subjective essay. It's based on my experience with writing automated tests in various circumstances since 2003. I've tried to be as explicit about my context as possible, but I've most likely failed to identify one or more implicit assumptions or biases. I do, therefore, encourage comments. </p> <p> I wrote this commentary because people keep asking me about how much to test, and when. I suppose it's because they wish to learn from me, and I'm happy to share what I know, to the best of my ability. I have much to learn myself, though, so read this only as the partial, flawed, personal answer that it is. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative validation https://blog.ploeh.dk/2018/11/05/applicative-validation 2018-11-05T07:05:00+00:00 Mark Seemann <div id="post"> <p> <em>Validate input in applicative style for superior readability and composability.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. It demonstrates how applicative style can be used to compose small validation functions to a larger validation function in such a way that no validation messages are lost, and the composition remains readable. </p> <p> All example code in this article is given in <a href="https://www.haskell.org">Haskell</a>. No <a href="https://fsharp.org">F#</a> translation is offered, because <a href="http://fsharpforfunandprofit.com">Scott Wlaschin</a> has an <a href="https://fsharpforfunandprofit.com/posts/elevated-world-3/#validation">equivalent example covering input validation in F#</a>. </p> <h3 id="789d9704d9a2434f8631d718e8be3f17"> JSON validation <a href="#789d9704d9a2434f8631d718e8be3f17" title="permalink">#</a> </h3> <p> In my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">Pluralsight course about a functional architecture in F#</a>, you can see an example of an on-line restaurant reservation system. I often return to that example scenario, so for regular readers of this blog, it should be known territory. For newcomers, imagine that you've been asked to develop an HTTP-based API that accepts JSON documents containing restaurant reservations. Such a JSON document could look like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2017-06-27&nbsp;18:30:00+02:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 }</pre> </p> <p> It contains the date and time of the (requested) reservation, the email address and name of the person making the reservation, as well as the number of people who will be dining. Particularly, notice that the date and time is represented as a string value (specifically, in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format), since JSON has no built-in date and time data type. </p> <p> In Haskell, you can represent such a JSON document using a type like this: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">ReservationJson</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">ReservationJson</span>&nbsp;{ &nbsp;&nbsp;<span style="color:#600277;">jsonDate</span>&nbsp;::&nbsp;String, &nbsp;&nbsp;<span style="color:#600277;">jsonQuantity</span>&nbsp;::&nbsp;Double, &nbsp;&nbsp;<span style="color:#600277;">jsonName</span>&nbsp;::&nbsp;String, &nbsp;&nbsp;<span style="color:#600277;">jsonEmail</span>&nbsp;::&nbsp;String&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Read</span>,&nbsp;<span style="color:#a31515;">Generic</span>)</pre> </p> <p> Haskell's strength is in its type system, so you should prefer to model a reservation using a strong type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;{ &nbsp;&nbsp;<span style="color:#600277;">reservationDate</span>&nbsp;::&nbsp;<span style="color:blue;">ZonedTime</span>, &nbsp;&nbsp;<span style="color:#600277;">reservationQuantity</span>&nbsp;::&nbsp;Int, &nbsp;&nbsp;<span style="color:#600277;">reservationName</span>&nbsp;::&nbsp;String, &nbsp;&nbsp;<span style="color:#600277;">reservationEmail</span>&nbsp;::&nbsp;String&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Read</span>)</pre> </p> <p> Instead of modelling the date and time as a string, you model it as a <code>ZonedTime</code> value. Additionally, you should model quantity as an integer, since a floating point value doesn't make much sense. </p> <p> While you can always translate a <code>Reservation</code> value to a <code>ReservationJson</code> value, the converse doesn't hold. There are <code>ReservationJson</code> values that you can't translate to <code>Reservation</code>. Such <code>ReservationJson</code> values are invalid. </p> <p> You should write code to validate and translate <code>ReservationJson</code> values to <code>Reservation</code> values, if possible. </p> <h3 id="e71bff7b635a449eb05a503c75c4f887"> Specialised validations <a href="#e71bff7b635a449eb05a503c75c4f887" title="permalink">#</a> </h3> <p> The <code>ReservationJson</code> type is a complex type, because it's composed of multiple (four) elements of different types. You can easily define at least three validation rules that ought to hold: <ol> <li>You should be able to convert the <code>jsonDate</code> value to a <code>ZonedTime</code> value.</li> <li><code>jsonQuantity</code> must be a positive integer.</li> <li><code>jsonEmail</code> should look believably like an email address.</li> </ol> When you have a complex type where more than one validation rule applies, your code will be most readable and maintainable if you can write each rule as an independent function. </p> <p> In Haskell, people often use <code>Either</code> for validation, but instead of using <code>Either</code> directly, I'll introduce a specialised <code>Validation</code> type: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;e&nbsp;r&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(<span style="color:#dd0000;">Either</span>&nbsp;e&nbsp;r)&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Functor</span>) </pre> </p> <p> You'll notice that this is simply a redefinition of <code>Either</code>. Haskell can automatically derive its <code>Functor</code> instance with the <code>DeriveFunctor</code> language extension. </p> <p> My motivation for introducing a new type is that the way that <code>Either</code> is <code>Applicative</code> is not quite how I'd like it to be. Introducing a <code>newtype</code> enables you to change how a type behaves. More on that later. First, you can implement the three individual validation functions. </p> <h3 id="89e88d794a0449189eff92795a2bca04"> Date validation <a href="#89e88d794a0449189eff92795a2bca04" title="permalink">#</a> </h3> <p> If the JSON date value is an ISO 8601-formatted string, then you can parse it as a <code>ZonedTime</code>. In that case, you should return the <code>Right</code> case of <code>Validation</code>. If you can't parse the string into a <code>ZonedTime</code> value, you should return a <code>Left</code> value containing a helpful error message. </p> <p> <pre><span style="color:#600277;">validateDate</span>&nbsp;::&nbsp;String&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;[String]&nbsp;<span style="color:blue;">ZonedTime</span> validateDate&nbsp;candidate&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;readMaybe&nbsp;candidate&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;d&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Right&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Left&nbsp;[<span style="color:#a31515;">&quot;Not&nbsp;a&nbsp;date.&quot;</span>]</pre> </p> <p> This function uses <code>readMaybe</code> from <code>Text.Read</code> to attempt to parse the <code>candidate</code> <code>String</code>. When <code>readMaybe</code> can read the <code>String</code> value, it returns a <code>Just</code> value with the parsed value inside; otherwise, it returns <code>Nothing</code>. The function pattern-matches on those two cases and returns the appropriate value in each case. </p> <p> Notice that errors are represented as a list of <code>String</code> values, although this particular function only returns a single message in its list of error messages. The reason for that is that you should be able to collect multiple validation issues for a complex value such as <code>ReservationJson</code>, and keeping track of errors in a list makes that possible. </p> <p> Haskell <a href="https://en.wikipedia.org/wiki/Code_golf">golfers</a> may argue that this implementation is overly verbose, and it could, for instance, instead be written as: </p> <p> <pre>validateDate&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">.</span>&nbsp;maybe&nbsp;(Left&nbsp;[<span style="color:#a31515;">&quot;Not&nbsp;a&nbsp;date.&quot;</span>])&nbsp;Right&nbsp;<span style="color:#666666;">.</span>&nbsp;readMaybe </pre> </p> <p> which is true, but not as readable. Both versions get the job done, though, as these GCHi-based ad-hoc tests demonstrate: </p> <p> <pre>λ&gt; validateDate "2017/27/06 18:30:00 UTC+2" Validation (Left ["Not a date."]) λ&gt; validateDate "2017-06-27 18:30:00+02:00" Validation (Right 2017-06-27 18:30:00 +0200)</pre> </p> <p> That takes care of parsing dates. On to the next validation function. </p> <h3 id="df33c4c13805494b844793bac0577e5b"> Quantity validation <a href="#df33c4c13805494b844793bac0577e5b" title="permalink">#</a> </h3> <p> JSON numbers aren't guaranteed to be integers, so it's possible that even a well-formed Reservation JSON document could contain a <code>quantity</code> property of <code>9.7</code>, <code>-11.9463</code>, or similar. When handling restaurant reservations, however, it only makes sense to handle positive integers. Even <code>0</code> is useless in this context. Thus, validation must check for two conditions, so in principle, you could write two separate functions for that. In order to keep the example simple, though, I've included both tests in the same function: </p> <p> <pre><span style="color:#600277;">validateQuantity</span>&nbsp;::&nbsp;Double&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;[String]&nbsp;Int validateQuantity&nbsp;candidate&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;isInt&nbsp;candidate&nbsp;<span style="color:#666666;">&amp;&amp;</span>&nbsp;candidate&nbsp;<span style="color:#666666;">&gt;</span>&nbsp;<span style="color:#09885a;">0</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Right&nbsp;<span style="color:#666666;">$</span>&nbsp;round&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Left&nbsp;[<span style="color:#a31515;">&quot;Not&nbsp;a&nbsp;positive&nbsp;integer.&quot;</span>] &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;isInt&nbsp;x&nbsp;<span style="color:#666666;">=</span>&nbsp;x&nbsp;<span style="color:#666666;">==</span>&nbsp;fromInteger&nbsp;(round&nbsp;x)</pre> </p> <p> If <code>candidate</code> is both an integer, and greater than zero, then <code>validateQuantity</code> returns <code>Right</code>; otherwise, it returns a <code>Left</code> value containing an error message. Like <code>validateDate</code>, you can easily test <code>validateQuantity</code> in GHCi: </p> <p> <pre>λ&gt; validateQuantity 4 Validation (Right 4) λ&gt; validateQuantity (-1) Validation (Left ["Not a positive integer."]) λ&gt; validateQuantity 2.32 Validation (Left ["Not a positive integer."])</pre> </p> <p> Perhaps you can think of rules for names, but I can't, so we'll leave the name be and move on to validating email addresses. </p> <h3 id="7eb2c9f79bfa4f7fa866094d094c5e2c"> Email validation <a href="#7eb2c9f79bfa4f7fa866094d094c5e2c" title="permalink">#</a> </h3> <p> It's <a href="http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx">notoriously difficult to validate SMTP addresses</a>, so you shouldn't even try. It seems fairly safe to assume, however, that an email address must contain at least one <code>@</code> character, so that's going to be all the validation you have to implement: </p> <p> <pre><span style="color:#600277;">validateEmail</span>&nbsp;::&nbsp;String&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;[String]&nbsp;String validateEmail&nbsp;candidate&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;<span style="color:#a31515;">&#39;@&#39;</span>&nbsp;<span style="color:#666666;">`elem`</span>&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Right&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Left&nbsp;[<span style="color:#a31515;">&quot;Not&nbsp;an&nbsp;email&nbsp;address.&quot;</span>]</pre> </p> <p> Straightforward. Try it out in GHCI: </p> <p> <pre>λ&gt; validateEmail "foo" Validation (Left ["Not an email address."]) λ&gt; validateEmail "foo@example.org" Validation (Right "foo@example.org")</pre> </p> <p> Indeed, that works. </p> <h3 id="bcfce51ff4e94f79a6648a7e373ebc82"> Applicative composition <a href="#bcfce51ff4e94f79a6648a7e373ebc82" title="permalink">#</a> </h3> <p> What you really should be doing is to validate a <code>ReservationJson</code> value. You have the three validation rules implemented, so now you have to compose them. There is, however, a catch: you must evaluate all rules, and return a list of <em>all</em> the errors you encountered. That's probably going to be a better user experience for a user. </p> <p> That's the reason you can't use <code>Either</code>. While it's <code>Applicative</code>, it doesn't behave like you'd like it to behave in this scenario. Particularly, the problem is that it throws away all but the first <code>Left</code> value it finds: </p> <p> <pre>λ&gt; Right (,,) &lt;*&gt; Right 42 &lt;*&gt; Left "foo" &lt;*&gt; Left "bar" Left "foo"</pre> </p> <p> Notice how <code>Left "bar"</code> is ignored. </p> <p> With the new type <code>Validation</code> based on <code>Either</code>, you can now define how it behaves as an applicative functor: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;(<span style="color:blue;">Validation</span>&nbsp;m)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">.</span>&nbsp;pure &nbsp;&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(Left&nbsp;x)&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(Left&nbsp;y)&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(Left&nbsp;(mappend&nbsp;x&nbsp;y)) &nbsp;&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;f&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;r&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(f&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;r)</pre> </p> <p> This instance is restricted to <code>Monoid</code> <code>Left</code> types. It has special behaviour for the case where both expressions passed to <code>&lt;*&gt;</code> are <code>Left</code> values. In that case, it uses <code>mappend</code> (from <code>Monoid</code>) to 'add' the two <code>Left</code> values together in a new <code>Left</code> value. </p> <p> For all other cases, this instance of <code>Applicative</code> delegates to the behaviour defined for <code>Either</code>. It also uses <code>pure</code> from <code>Either</code> to implement its own <code>pure</code> function. </p> <p> Lists (<code>[]</code>) <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">form a monoid</a>, and since all the above validation functions return lists of errors, it means that you can compose them using this definition of <code>Applicative</code>: </p> <p> <pre><span style="color:#600277;">validateReservation</span>&nbsp;::&nbsp;<span style="color:blue;">ReservationJson</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;[String]&nbsp;<span style="color:blue;">Reservation</span> validateReservation&nbsp;candidate&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;pure&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;vDate&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;vQuantity&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;vName&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;vEmail &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;vDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;validateDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">$</span>&nbsp;jsonDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;vQuantity&nbsp;<span style="color:#666666;">=</span>&nbsp;validateQuantity&nbsp;<span style="color:#666666;">$</span>&nbsp;jsonQuantity&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;vName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;pure&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">$</span>&nbsp;jsonName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;vEmail&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;validateEmail&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">$</span>&nbsp;jsonEmail&nbsp;&nbsp;&nbsp;&nbsp;candidate</pre> </p> <p> The <code>candidate</code> is a <code>ReservationJson</code> value, but each of the validation functions work on either <code>String</code> or <code>Double</code>, so you'll have to use the <code>ReservationJson</code> type's access functions (<code>jsonDate</code>, <code>jsonQuantity</code>, and so on) to pull the relevant values out of it. Once you have those, you can pass them as arguments to the appropriate validation function. </p> <p> Since there's no rule for <code>jsonName</code>, you can use <code>pure</code> to create a <code>Validation</code> value. All four resulting values (<code>vDate</code>, <code>vQuantity</code>, <code>vName</code>, and <code>vEmail</code>) are <code>Validation [String]</code> values; only their <code>Right</code> types differ. </p> <p> The <code>Reservation</code> record constructor is a function of the type <code>ZonedTime -&gt; Int -&gt; String -&gt; String -&gt; Reservation</code>, so when you arrange the four <code>v*</code> values correctly between the <code>&lt;*&gt;</code> operator, you have the desired composition. </p> <p> Try it in GHCi: </p> <p> <pre>λ&gt; validateReservation $ ReservationJson "2017-06-30 19:00:00+02:00" 4 "Jane Doe" "j@example.com" Validation (Right (Reservation { &nbsp;&nbsp;&nbsp;&nbsp;reservationDate = 2017-06-30 19:00:00 +0200, &nbsp;&nbsp;&nbsp;&nbsp;reservationQuantity = 4, &nbsp;&nbsp;&nbsp;&nbsp;reservationName = "Jane Doe", &nbsp;&nbsp;&nbsp;&nbsp;reservationEmail = "j@example.com"})) λ&gt; validateReservation $ ReservationJson "2017/14/12 6pm" 4.1 "Jane Doe" "jane.example.com" Validation (Left ["Not a date.","Not a positive integer.","Not an email address."]) λ&gt; validateReservation $ ReservationJson "2017-06-30 19:00:00+02:00" (-3) "Jane Doe" "j@example.com" Validation (Left ["Not a positive integer."])</pre> </p> <p> The first <code>ReservationJson</code> value passed to <code>validateReservation</code> is valid, so the return value is a <code>Right</code> value. </p> <p> The next <code>ReservationJson</code> value is about as wrong as it can be, so three different error messages are returned in a <code>Left</code> value. This demonstrates that <code>Validation</code> doesn't give up the first time it encounters a <code>Left</code> value, but rather collects them all. </p> <p> The third example demonstrates that even a single invalid value (in this case a negative quantity) is enough to make the entire input invalid, but as expected, there's only a single error message. </p> <h3 id="b9d3fd6c208647dc988ef0ffc64cf061"> Summary <a href="#b9d3fd6c208647dc988ef0ffc64cf061" title="permalink">#</a> </h3> <p> Validation may be the poster child of applicative functors, but it <em>is</em> a convenient way to solve the problem. In this article you saw how to validate a complex data type, collecting and reporting on all problems, if any. </p> <p> In order to collect all errors, instead of immediately short-circuiting on the first error, you have to deviate from the standard <code>Either</code> implementation of <code>&lt;*&gt;</code>. If you go back to read Scott Wlaschin's article, you should be aware that it specifically implements its applicative functor in that way, instead of the normal behaviour of <code>Either</code>. </p> <p> More applicative functors exist. This article series has, I think, room for more examples. </p> <p> <strong>Next:</strong> <a href="/2018/11/26/the-test-data-generator-applicative-functor">The Test Data Generator applicative functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Maybe applicative functor https://blog.ploeh.dk/2018/10/29/the-maybe-applicative-functor 2018-10-29T06:17:00+00:00 Mark Seemann <div id="post"> <p> <em>An introduction to the Maybe applicative functor for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. Previously, in a related series, you got an introduction to <a href="/2018/03/26/the-maybe-functor">Maybe as a functor</a>. Not all functors are applicative, but some are, and Maybe is one of them (like list). </p> <p> In this article, you'll see how to make a C# Maybe class applicative. While I'm going to start with <a href="https://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>, you can skip to the C# section if you'd like. </p> <h3 id="f88a2d0afbe84b7695fe4247e3cfe941"> F# <a href="#f88a2d0afbe84b7695fe4247e3cfe941" title="permalink">#</a> </h3> <p> A few years ago, <a href="/2016/06/28/roman-numerals-via-property-based-tdd">I did the <em>Roman numerals</em> kata</a> in F#. This is an exercise where you have to convert between normal base 10 integers and <a href="https://en.wikipedia.org/wiki/Roman_numerals">Roman numerals</a>. Conversions can fail in both directions, because Roman numerals don't support negative numbers, zero, or numbers greater than 3,999, and Roman numerals represented as strings could be malformed. </p> <p> Some Roman numbers are written in a subtractive style, e.g. "IV" means <em>subtract 1 (I) from 5 (V)</em>. It's easy enough to subtract two numbers, but because parsing isn't guaranteed to succeed, I didn't have two numbers; I had two number <em>options</em> (recall that in F#, Maybe is called <code>option</code>). </p> <p> How do you subtract one <code>int option</code> from another <code>int option</code>? </p> <p> Both of these values could be <code>Some</code>, or they could be <code>None</code>. What should happen in each case? With Maybe, only four combinations are possible, so you can put them in a table: <table> <thead> <tr> <th></th> <th><code>Some x</code></th> <th><code>None</code></th> </tr> </thead> <tbody> <tr> <td><strong><code>Some y</code></strong></td> <td><code>Some (x - y)</code></td> <td><code>None</code></td> </tr> <tr> <td><strong><code>None</code></strong></td> <td><code>None</code></td> <td><code>None</code></td> </tr> </tbody> </table> Only if both values are <code>Some</code> cases should you return a <code>Some</code> case with the result of the subtraction; in all other cases, you should return <code>None</code>. </p> <p> You can do this with regular pattern matching, but it's hardly the most elegant solution: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;difference&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;minuend,&nbsp;subtrahend&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Some</span>&nbsp;m,&nbsp;<span style="color:navy;">Some</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(m&nbsp;-&nbsp;s) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> You <em>could</em> attempt to solve this with a specialised helper function like this: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:teal;">Option</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;a&nbsp;option&nbsp;-&gt;&nbsp;&#39;b&nbsp;option&nbsp;-&gt;&nbsp;&#39;c&nbsp;option</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">map2</span>&nbsp;<span style="color:navy;">f</span>&nbsp;xo&nbsp;yo&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;xo,&nbsp;yo&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Some</span>&nbsp;x,&nbsp;<span style="color:navy;">Some</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;x&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> which you could use like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;difference&nbsp;=&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">map2</span>&nbsp;(-)&nbsp;minuend&nbsp;subtrahend </pre> </p> <p> It doesn't, however, generalise well... What if you need to operate on three option values, instead of two? Or four? Should you add <code>map3</code> and <code>map4</code> functions as well? </p> <p> Making <code>option</code> an applicative functor addresses that problem. Here's one possible implementation of <code>&lt;*&gt;</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;option&nbsp;-&gt;&nbsp;&#39;a&nbsp;option&nbsp;-&gt;&nbsp;&#39;b&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;fo&nbsp;xo&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;fo,&nbsp;xo&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Some</span>&nbsp;<span style="color:navy;">f</span>,&nbsp;<span style="color:navy;">Some</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> This enables you two write the subtraction like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;difference&nbsp;=&nbsp;<span style="color:navy;">Some</span>&nbsp;(-)&nbsp;&lt;*&gt;&nbsp;minuend&nbsp;&lt;*&gt;&nbsp;subtrahend </pre> </p> <p> For a detailed explanation on how that works, see the <a href="/2018/10/08/full-deck">previous explanation for lists</a>; it works the same way for Maybe as it does for List. </p> <p> In the end, however, I didn't think that this was the most readable code, so in the Roman numeral exercise, I chose to use a <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expression</a> instead. </p> <h3 id="264aaa993b7643b692716bed1b3e2b92"> Haskell <a href="#264aaa993b7643b692716bed1b3e2b92" title="permalink">#</a> </h3> <p> In Haskell, <code>Maybe</code> is already <code>Applicative</code> as part of the language. Without further ado, you can simply write: </p> <p> <pre>difference&nbsp;<span style="color:#666666;">=</span>&nbsp;pure&nbsp;<span style="color:#600277;">(-)</span>&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;minuend&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;subtrahend </pre> </p> <p> As is the case with the F# code, I don't consider this the most <em>readable</em> way to express the subtraction of two integers. In F#, I ultimately decided to use a computation expression. In Haskell, that's equivalent to using <code>do</code> notation: </p> <p> <pre><span style="color:#600277;">difference</span>&nbsp;::&nbsp;Maybe&nbsp;Integer difference&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;m&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;minuend &nbsp;&nbsp;s&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;subtrahend &nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;m&nbsp;<span style="color:#666666;">-</span>&nbsp;s</pre> </p> <p> While more verbose, I think it's clearer that one number is being subtracted from another number. </p> <p> This works for <code>Maybe</code> because not only is <code>Maybe</code> <code>Applicative</code>, it's also a <code>Monad</code>. It's its monadness that enables the <code>do</code> notation. Not all applicative functors are monads, but Maybe is. </p> <h3 id="67a752fec39f4944984f505c125632f1"> C# <a href="#67a752fec39f4944984f505c125632f1" title="permalink">#</a> </h3> <p> In a <a href="/2018/03/26/the-maybe-functor">previous article</a> you saw how to implement the Maybe functor in C#. You can extend it so that it also becomes an applicative functor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector.HasItem&nbsp;&amp;&amp;&nbsp;source.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(selector.Item(source.Item)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector.HasItem&nbsp;&amp;&amp;&nbsp;source.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;g&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;selector.Item(source.Item,&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;(g); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;(); }</pre> </p> <p> As was the case for making sequences applicative in C#, you <a href="/2018/10/15/an-applicative-password-list">need overloads of the <code>Apply</code> method</a>, because C#'s type inference is inadequate for this task. </p> <p> If you have two <code>Maybe&lt;int&gt;</code> values, <code>minuend</code> and <code>subtrahend</code>, you can now perform the subtraction: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;subtract&nbsp;=&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;x&nbsp;-&nbsp;y; <span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;difference&nbsp;=&nbsp;subtract.ToMaybe().Apply(minuend).Apply(subtrahend);</pre> </p> <p> Like in F# and Haskell, applicative style is hardly the most readable way to express subtraction. It'd be nice if you could write it like Haskell's <code>do</code> notation. You can, but to do that, you must make Maybe a monad, and this isn't a monad tutorial. <a href="http://mikehadlow.com">Mike Hadlow</a> has a good <a href="http://mikehadlow.blogspot.dk/2011/01/monads-in-c1-introduction.html">monad tutorial for C# developers</a>, the gist of which is that you must implement <code>SelectMany</code> in order to turn your generic type into a monad. For now, I'll leave this as an exercise for you, but if you add an appropriate <code>SelectMany</code> method, you'd be able to write the subtraction like this: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;difference&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;m&nbsp;<span style="color:blue;">in</span>&nbsp;minuend &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;subtrahend &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;m&nbsp;-&nbsp;s;</pre> </p> <p> Again, I think this is more readable, but it does require that the type in question is a monad, and not all applicative functors are (but Maybe is). </p> <h3 id="b03175fcd3a74155a3ceac26fb02aed0"> Summary <a href="#b03175fcd3a74155a3ceac26fb02aed0" title="permalink">#</a> </h3> <p> This article demonstrates that lists or sequences aren't the only applicative functors. Maybe is also an applicative functor, but more exist. The next article will give you another example. </p> <p> <strong>Next:</strong> <a href="/2018/11/05/applicative-validation">Applicative validation</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="d42afea512c243019f500a345638cecb"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#d42afea512c243019f500a345638cecb">#</a></div> <div class="comment-content"> <blockquote> As was the case for making sequences applicative in C#, you <a href="/2018/10/15/an-applicative-password-list">need overloads of the <code>Apply</code> method</a>, because C#'s type inference is inadequate for this task. </blockquote> <p> I think <a href="/2018/10/15/an-applicative-password-list/#b4e76681ea894aa3be1e6b836343c148">we agreed</a> that the issue is not C#'s weak type inference but its lack of default function currying? My guess is that you wrote this quoted part of this article before my comment on your previous article. </p> </div> <div class="comment-date">2018-11-06 02:44 UTC</div> </div> <div class="comment" id="0bb16509bbea463bab48c9e97acea2c5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0bb16509bbea463bab48c9e97acea2c5">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. <blockquote> "My guess is that you wrote this quoted part of this article before my comment on your previous article." </blockquote> Yes, <a href="https://github.com/ploeh/ploeh.github.com/commit/078cb3e938bfb911363cc8ab1139dfc5ad435349">June 27, 2017, in fact</a>... </p> <p> You're correct that this particular issue is related to the uncurried nature of C# methods. </p> <p> I do, however, maintain that C#'s type inference capabilities are weaker than F#'s or Haskell's. To be clear, I view this as the result of priorities. I don't think that the people who designed and wrote the C# compiler are less skilled than the designers of F# or Haskell. The C# compiler has to solve many other problems, such as for example overload resolution, which is a language feature in direct opposition to currying. The C# compiler is excellent at overload resolution, a task with which the F# compiler sometimes struggle (and is not even a language feature in Haskell). </p> <p> Your comment is, however, a reminder that I should consider how I phrase such notions in the future. Thank you for pointing that out. As I'm publishing and get feedback, I constantly learn new things. I'm always grateful when someone like you take the time to educate me. </p> <p> I'll see if I can improve in the future. I do, however, still have a backlog of articles I wrote months, or even more than a year, ago, so it's possible that more errors escape my attention when I proof read them before publication. If that happens, I'll appreciate more corrections. </p> </div> <div class="comment-date">2018-11-06 7:30 UTC</div> </div> <div class="comment" id="01d5086d3f9a4d93ad4bea131521bafa"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#01d5086d3f9a4d93ad4bea131521bafa">#</a></div> <div class="comment-content"> <p> Thank you very much for your kind reply. I agree with everything you said. </p> <p> I will expand my comment a bit to give a clearer picture of my understanding. </p> <p> First, very little is "needed"; most things are merely sufficient. In particular, we don't <i>need</i> to overload your <code>Apply</code> method to achieve your goal. As <a href="/2018/10/15/an-applicative-password-list/#0ded7ac93aad8ba7b1063dd49c2051f1">I mentioned before</a>, it sufficies to have a single <code>Apply</code> method and instead create overloads of a function called <code>curry</code> that explicitly curries a given function. Furthermore, I think there is a sense in which this latter approach to overcome the lack of default currying is somehow minimal or most abstract or most general. </p> <p> Second, compared to languages like F# or Haskell, type inference is definitely weaker in C#. This issue was also present (in a subtle way) in your previous article, but I decided to largely ignore it in order to keep my comment more focused. In your <a href="/2018/10/15/an-applicative-password-list/">previous article</a>, you expliciltly defined the local variable <code>concat</code> like this <blockquote> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;concat&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;(x,&nbsp;y,&nbsp;z,&nbsp;æ,&nbsp;ø,&nbsp;å)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;y&nbsp;+&nbsp;z&nbsp;+&nbsp;æ&nbsp;+&nbsp;ø&nbsp;+&nbsp;å;</pre> </blockquote> In particular, you explicitly told the C# compiler that the type of all of these six variable is <code><span style="color:blue;">string</span></code>. That part was necessary; the type inference in C# is not strong enough to innfer (possibily in some use of <code>concat</code>) that the types could be <code><span style="color:blue;">string</span></code>. </p> <p> Suppose instead of defining <code>concat</code> as a local variable (with <code><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;</code> as its type) you had defined it as a member method on a class. Then its type in C# is some kind "method group". The method group of a method essentially corresponds to the set of methods containing itself and its overloads. Then in order to pass <code>concat</code> into <code>curry</code>, there needs to be a type conversion (or cast) from its method group to <code><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;</code>. This is also something that the C# system cannot do, and so Language Ext has overloads of a <a href="https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/Prelude/Prelude_Func.cs#L24">function called <code>fun</code></a> to do this explicitly. Using it on our hypothetical member function <code>concat</code> would look like <pre>fun&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(concat)</pre> Again, I think there is a sense in which this explicit way to specify non-inferable types is somehow minimal or most abstract or most general. </p> <p> My impression is that there is some low hanging fruit here for strengthing the type inference of the C# compiler. If a method group correpsonds to a singleton set (and that method has no <code>ref</code> or <code>out</code> arguments), then I would think it would be straight forward to consider an implicit cast from the method group to the corresponding <code>Func</code> or <code>Action</code> delegate. </p> </div> <div class="comment-date">2018-11-06 15:31 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative combinations of functions https://blog.ploeh.dk/2018/10/22/applicative-combinations-of-functions 2018-10-22T10:21:00+00:00 Mark Seemann <div id="post"> <p> <em>Applicative lists and sequences enable you to create combinations of functions as well as values.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. In the <a href="/2018/10/15/an-applicative-password-list">previous article</a>, you saw how you can use applicative lists and sequences to generate combinations of values; specifically, the example demonstrated how to generate various password character combinations. </p> <p> People often create passwords by using a common word as basis, and then turn characters into upper- or lower case. Someone feeling particularly tech-savvy may replace certain characters with digits, in an imitation of <a href="https://en.wikipedia.org/wiki/Leet">1337</a>. While this isn't secure, let's look at how to create various combinations of transformations using applicative lists and sequences. </p> <h3 id="302fedff3eed4c09b277bd4ee3523ff3"> List of functions <a href="#302fedff3eed4c09b277bd4ee3523ff3" title="permalink">#</a> </h3> <p> In the previous article, I mentioned that there was a feature of applicative lists that I had, so far, deliberately ignored. </p> <p> If you consider an example like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;passwordCombinations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%s%s%s%s%s%s</span><span style="color:#a31515;">&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;P&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;a&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;ssw&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;o&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;rd&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>]</pre> </p> <p> you may have already noticed that while the left side of the <code>&lt;*&gt;</code> operator is a list of functions, it contains only a single function. What happens if you supply more than a single function? </p> <p> You get a combination of each function and each list element. </p> <p> Assume that you have three functions to convert characters: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:teal;">Char</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;char&nbsp;-&gt;&nbsp;char</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">toUpper</span>&nbsp;c&nbsp;=&nbsp;System.<span style="color:teal;">Char</span>.<span style="color:navy;">ToUpperInvariant</span>&nbsp;c &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;char&nbsp;-&gt;&nbsp;char</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">toLower</span>&nbsp;c&nbsp;=&nbsp;System.<span style="color:teal;">Char</span>.<span style="color:navy;">ToLowerInvariant</span>&nbsp;c &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Not&nbsp;even&nbsp;trying&nbsp;to&nbsp;be&nbsp;complete:</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;char&nbsp;-&gt;&nbsp;char</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">to1337</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;a&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;4&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;b&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;6&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;E&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;3&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;H&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;#&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;i&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;!&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;l&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;1&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;o&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;O&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;0&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;t&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;+&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c</pre> </p> <p> All three are functions that convert one <code>char</code> value to another, although many values could pass through without being modified. Since they all have the same type, you can create a list of them: </p> <p> <pre>&gt; List.map String.map [Char.toUpper; Char.toLower; Char.to1337] &lt;*&gt; ["Hello"; "World"];; val it : string list = ["HELLO"; "WORLD"; "hello"; "world"; "#e110"; "W0r1d"]</pre> </p> <p> There's a bit to unpack there. Recall that all three functions in the <code>Char</code> module have the same type: <code>char -&gt; char</code>. Making a list of them gives you a <code>(char -&gt; char) list</code>, but you really need a <code>(string -&gt; string) list</code>. Fortunately, the built-in <code>String.map</code> function takes a <code>char -&gt; char</code> function and uses it to map each <code>char</code> values in a <code>string</code>. Thus, <code>List.map String.map [Char.toUpper; Char.toLower; Char.to1337]</code> gives you a <code>(string -&gt; string) list</code>. </p> <p> When you apply (<code>&lt;*&gt;</code>) that list of functions with a list of <code>string</code> values, you get all possible combinations of each function used with each string. Both <code>"Hello"</code> and <code>"World"</code> are converted to upper case, lower case, and 1337. </p> <h3 id="fde27240cfaf4479bc31ff50d3f619d0"> Combinations of functions <a href="#fde27240cfaf4479bc31ff50d3f619d0" title="permalink">#</a> </h3> <p> Perhaps you're happy with the above combinations, but can we do better? As an example, you'll notice that <code>to1337</code> only converts an upper-case <code>'E'</code> to <code>'3'</code>, but ignores a lower-case <code>'e'</code>. What if you also want the combination where <code>'e'</code> is first converted to upper case, and then to 1337? You'd like that, but you still want to retain the combinations where each of these transformations are applied without the other. </p> <p> Fear not; functions are values, so you can combine them as well! </p> <p> In the previous article, did you notice how you could model the presence or absence of a particular value? Specifically, the last character in the potential password could be <code>'!'</code>, but <code>'!'</code> could also be omitted. </p> <p> Consider, again, the expression for all password combinations: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;passwordCombinations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%s%s%s%s%s%s</span><span style="color:#a31515;">&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;P&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;a&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;ssw&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;o&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;rd&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>]</pre> </p> <p> Notice that the last list contains two options: <code>"!"</code> and the empty string (<code>""</code>). You can read about this in <a href="/2017/10/06/monoids">another article series</a>, but <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">character strings are monoids</a>, and one of the characteristics of monoids is that they have an <em>identity</em> element - a 'neutral' element, if you will. For strings, it's <code>""</code>; you can append or prepend the empty string as much as you'd like, but it's not going to change the other string. </p> <p> If you have a set of <a href="/2017/11/13/endomorphism-monoid">functions of the type <code>'a -&gt; 'a</code>, then the built-in function <code>id</code> is the identity element</a>. You can compose any <code>'a -&gt; 'a</code> function with <code>id</code>, and it's not going to change the other function. </p> <p> Since functions are values, then, you can create <em>combinations of functions:</em> </p> <p> <pre><span style="color:green;">//&nbsp;(char&nbsp;-&gt;&nbsp;char)&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;maps&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:navy;">g</span>&nbsp;<span style="color:navy;">h</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">g</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">h</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:teal;">Char</span>.<span style="color:navy;">toUpper</span>;&nbsp;<span style="color:navy;">id</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:teal;">Char</span>.<span style="color:navy;">toLower</span>;&nbsp;<span style="color:navy;">id</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:teal;">Char</span>.<span style="color:navy;">to1337</span>;&nbsp;<span style="color:navy;">id</span>]</pre> </p> <p> Here, <code>maps</code> is a list of functions, but it's not only three functions as in the above example. It's <em>eight</em> functions: </p> <p> <pre>&gt; List.length maps;; val it : int = 8</pre> </p> <p> The above applicative composition of <code>maps</code> combines three lists of functions. Each list presents two alternatives: a function (e.g. <code>Char.toUpper</code>), and <code>id</code>. In other words, a choice between doing something, and doing nothing. The lambda expression <code>fun f g h -&gt; f &gt;&gt; g &gt;&gt; h</code> takes three (curried) arguments, and returns the composition of calling <code>f</code>, then passing the result of that to <code>g</code>, and again passing the result of that to <code>h</code>. <code>f</code> is either <code>Char.toUpper</code> or <code>id</code>, <code>g</code> is either <code>Char.toLower</code> or <code>id</code>, and <code>h</code> is either <code>Char.to1337</code> or <code>id</code>. That's eight possible combinations. </p> <p> Combine eight functions with two <code>string</code> values, and you get sixteen alternatives back: </p> <p> <pre>&gt; List.map String.map maps &lt;*&gt; ["Hello"; "World"];; val it : string list = ["he110"; "w0r1d"; "hello"; "world"; "#3LL0"; "W0RLD"; "HELLO"; "WORLD"; "he110"; "w0r1d"; "hello"; "world"; "#e110"; "W0r1d"; "Hello"; "World"]</pre> </p> <p> Notice, for example, how one of the suggested alternatives is <code>"#3LL0"</code>. Previously, there was no translation from <code>'e'</code> to <code>'3'</code>, but now there is, via <code>Char.toUpper &gt;&gt; id &gt;&gt; Char.to1337</code>. </p> <p> Some of the combinations are redundant. For example, <code>"hello"</code> is generated twice, by <code>Char.toUpper &gt;&gt; Char.toLower &gt;&gt; id</code> and <code>id &gt;&gt; Char.toLower &gt;&gt; id</code>, respectively. You can reduce the output with <code>List.distinct</code>: </p> <p> <pre>&gt; List.map String.map maps &lt;*&gt; ["Hello"; "World"] |&gt; List.distinct;; val it : string list = ["he110"; "w0r1d"; "hello"; "world"; "#3LL0"; "W0RLD"; "HELLO"; "WORLD"; "#e110"; "W0r1d"; "Hello"; "World"]</pre> </p> <p> You can write equivalent code in Haskell, but it's so similar to the F# code that there's no reason to show it. </p> <h3 id="2000ef3aa60148e597de68d16048745f"> Translation to C# <a href="#2000ef3aa60148e597de68d16048745f" title="permalink">#</a> </h3> <p> Using the <code>Apply</code> extension methods from the previous article, you can translate the above code to C#. </p> <p> While you can use the .NET Base Class Library's <code>Char.ToUpperInvariant</code> and <code>Char.ToLowerInvariant</code> methods as is, you'll need to supply a <code>to1337</code> function. You can write it as a named static method, but you can also write it as a delegate: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;to1337&nbsp;=&nbsp;c&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">switch</span>&nbsp;(c) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;a&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;4&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;b&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;6&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;E&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;3&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;H&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;#&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;i&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;!&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;l&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;1&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;o&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;O&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;0&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;t&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;+&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">default</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;c; &nbsp;&nbsp;&nbsp;&nbsp;} };</pre> </p> <p> You're also going to need an <code>id</code> function: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;id&nbsp;=&nbsp;c&nbsp;=&gt;&nbsp;c; </pre> </p> <p> In order to compose three functions to one, you can write something like this: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;compose3&nbsp;=&nbsp;(f,&nbsp;g,&nbsp;h)&nbsp;=&gt;&nbsp;x&nbsp;=&gt;&nbsp;h(g(f(x)));</pre> </p> <p> That's going to be a contender for some of the most obscure C# code I've written in a while. By the double use of <code>=&gt;</code>, you can tell that it's a delegate that returns a delegate. That's not even the worst part: check out the type of the thing! In reality, nothing happens here that doesn't also happen in the above F# code, but it's an example of the superiority of <a href="https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system">Hindley–Milner type inference</a>: in F#, you don't have to explicitly type out the type. </p> <p> With a function to compose three other functions, you can now apply the three alternative functions: </p> <p> <pre><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&gt;&nbsp;maps&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;compose3&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">Char</span>.ToUpperInvariant,&nbsp;id&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">Char</span>.ToLowerInvariant,&nbsp;id&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;to1337,&nbsp;id&nbsp;});</pre> </p> <p> Now you have a sequence of functions that translate <code>char</code> values to <code>char</code> values. What you really need, though, is a sequence of functions that translate <code>string</code> values to <code>string</code> values. </p> <p> The F# core library defines the built-in <code>String.map</code> function, but as far as I can tell, there's no equivalent method in the .NET Base Class Library. Therefore, you must implement it yourself: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;stringMap&nbsp;=&nbsp;f&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(s.Select(f).ToArray());</pre> </p> <p> This is a function that takes a <code>Func&lt;char, char&gt;</code> as input and returns a <code>Func&lt;string, string&gt;</code>. Again, the type declaration isn't the prettiest. </p> <p> You can now apply <code>maps</code> to some <code>string</code> values, using the <code>Apply</code> extension method: </p> <p> <pre><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;hellos&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;maps.Select(stringMap).Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;Hello&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;World&quot;</span>&nbsp;});</pre> </p> <p> This produces exactly the same output as the above F# example, even in the same order. </p> <p> Applicative functors are elegant in F# and Haskell, but awkward in a language like C# - mostly because of its inferior type inference engine. </p> <h3 id="001d02c7aa40400391b899f723ef9baa"> Summary <a href="#001d02c7aa40400391b899f723ef9baa" title="permalink">#</a> </h3> <p> Previous articles demonstrated how applicative lists can be used to compose several lists into a list that contains all possible combinations. In this article you saw how this also extends to combinations of functions. </p> <p> The last three articles (including the present) focus on lists as applicative functors, but lists aren't the only type of applicative functor. In the next articles, you'll encounter some other applicative functors. </p> <p> <strong>Next:</strong> <a href="/2018/10/29/the-maybe-applicative-functor">The Maybe applicative functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An applicative password list https://blog.ploeh.dk/2018/10/15/an-applicative-password-list 2018-10-15T05:54:00+00:00 Mark Seemann <div id="post"> <p> <em>How to use the applicative functor capabilities of lists to create a password list, with examples that object-oriented programmers can understand.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. In the <a href="/2018/10/08/full-deck">previous article</a>, you saw how to use <a href="https://www.haskell.org">Haskell</a> and <a href="https://fsharp.org">F#</a> lists as applicative functors to generate combinations of values. In this article, you'll see a similar example, but this time, there will also be a C# example. </p> <h3 id="ab0acd0982a64569a68ba92b041739a9"> Guess the password variation <a href="#ab0acd0982a64569a68ba92b041739a9" title="permalink">#</a> </h3> <p> Years ago, I worked in an organisation that (among other things) produced much demo software. Often, the demo software would include a demonstration of the security features of a product, which meant that, as a user evaluating the software, you had to log in with a user name and password. In order to keep things simple, the password was usually <em>Passw0rd!</em>, or some variation thereof. </p> <p> (Keep in mind that this was demoware. Password strength wasn't a concern. We explicitly wanted the password to be easy to guess, so that users evaluating the software had a chance to test how log in worked. This was long before social login and the like.) </p> <p> We had more than one package of demoware, and over the years, variations of the standard password had snuck in. Sometimes it'd be all lower-case; sometimes it'd use <em>4</em> instead of <em>a</em>, and so on. As the years went on, the number of possible permutations grew. </p> <p> Recently, I had a similar problem, but for security reasons, I don't want to divulge what it was. Let's just pretend that I had to guess one of those old demo passwords. </p> <p> There weren't <em>that</em> many possible variations, but just enough that I couldn't keep them systematically in my head. <ul> <li>The first letter could be upper or lower case.</li> <li>The second letter could be <em>a</em> or <em>4</em>.</li> <li>The <em>o</em> could be replaced with a zero (<em>0</em>).</li> <li>The password could end with an exclamation mark (<em>!</em>), but it might also be omitted.</li> </ul> Having recently discovered the power of lists as applicative functors, I started F# Interactive (FSI), and wrote the following: </p> <p> <pre>&gt; let (&lt;*&gt;) fs l = fs |&gt; List.collect (fun f -&gt; l |&gt; List.map f);; val ( &lt;*&gt; ) : fs:('a -&gt; 'b) list -&gt; l:'a list -&gt; 'b list &gt; [sprintf "%s%s%s%s%s%s"] &lt;*&gt; ["P"; "p"] &lt;*&gt; ["a"; "4"] &lt;*&gt; ["ssw"] &lt;*&gt; ["o"; "0"] &lt;*&gt; ["rd"] &lt;*&gt; [""; "!"];; val it : string list = ["Password"; "Password!"; "Passw0rd"; "Passw0rd!"; "P4ssword"; "P4ssword!"; "P4ssw0rd"; "P4ssw0rd!"; "password"; "password!"; "passw0rd"; "passw0rd!"; "p4ssword"; "p4ssword!"; "p4ssw0rd"; "p4ssw0rd!"]</pre> </p> <p> This produces a list of all the possible password combinations according to the above rules. Since there weren't that many, I could start trying each from the start, until I found the correct variation. </p> <p> The first list contains a single function. Due to the way <code>sprintf</code> works, <code>sprintf "%s%s%s%s%s%s"</code> is a function that takes six (curried) <code>string</code> arguments, and returns a <code>string</code>. The number 6 is no coincidence, because you'll notice that the <code>&lt;*&gt;</code> operator is used six times. </p> <p> There's no reason to repeat the exegesis from the previous article, but briefly: <ol> <li><code>sprintf "%s%s%s%s%s%s"</code> has the type <code>string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string</code>.</li> <li><code>[sprintf "%s%s%s%s%s%s"]</code> has the type <code>(string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string) list</code>.</li> <li><code>[sprintf "%s%s%s%s%s%s"] &lt;*&gt; ["P"; "p"]</code> has the type <code>(string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string) list</code>.</li> <li><code>[sprintf "%s%s%s%s%s%s"] &lt;*&gt; ["P"; "p"] &lt;*&gt; ["a"; "4"]</code> has the type <code>(string -&gt; string -&gt; string -&gt; string -&gt; string) list</code>.</li> <li>...and so on.</li> </ol> Notice that every time you add another list with <code>&lt;*&gt;</code>, an argument is removed from the resulting function contained in the returned list. When you've applied six lists with the <code>&lt;*&gt;</code> operator, the return value is no longer a list of functions, but a list of values. </p> <p> Clearly, then, that's no coincidence. I deliberately shaped the initial function to take six arguments, so that it would match the six segments I wanted to model. </p> <p> Perhaps the most interesting quality of applicative functors is that you can compose an arbitrary number of objects, as long as you have a function to match the number of arguments. </p> <h3 id="25e1617d768a4705a9756f230ddd9b39"> Haskell <a href="#25e1617d768a4705a9756f230ddd9b39" title="permalink">#</a> </h3> <p> This time I started with F#, but in Haskell, <code>&lt;*&gt;</code> is a built-in operator, so obviously this also works there: </p> <p> <pre><span style="color:#600277;">passwordCombinations</span>&nbsp;::&nbsp;[String] passwordCombinations&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;[printf&nbsp;<span style="color:#a31515;">&quot;%s%s%s%s%s%s&quot;</span>] &nbsp;&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;P&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;ssw&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;o&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;rd&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>]</pre> </p> <p> The output is the same as the above F# code. </p> <h3 id="9c3412586a58460c8280212a13351331"> C# <a href="#9c3412586a58460c8280212a13351331" title="permalink">#</a> </h3> <p> While you can translate the concept of lists as an applicative functor to C#, this is where you start testing the limits of the language; or perhaps I've simply forgotten too much C# to do it full justice. </p> <p> Instead of making linked lists an applicative functor, let's consider a type closer to the spirit of the C# language: <code>IEnumerable&lt;T&gt;</code>. The following code attempts to turn <code>IEnumerable&lt;T&gt;</code> into an applicative functor. </p> <p> Consider the above F# implementation of <code>&lt;*&gt;</code> (explained in the previous article). It uses <code>List.collect</code> to flatten what would otherwise had been a list of lists. <code>List.collect</code> has the type <code>('a -&gt; 'b list) -&gt; 'a list -&gt; 'b list</code>. The corresponding method for <code>IEnumerable&lt;T&gt;</code> already exists in the .NET Base Class Library; it's called <a href="https://msdn.microsoft.com/en-us/library/bb534336">SelectMany</a>. (Incidentally, this is also the monadic <em>bind</em> function, but this is <em>still</em> not a monad tutorial.) </p> <p> For an applicative functor, we need a method that takes a sequence of functions, and a sequence of values, and produces a sequence of return values. You can translate the above F# function to this C# extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;selectors.SelectMany(source.Select); }</pre> </p> <p> That's a single line of code! That's not so bad. What's the problem? </p> <p> So far there's no problem. You can, for example, write code like this: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;sl&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Length; <span style="color:blue;">var</span>&nbsp;lengths&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;sl&nbsp;}.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>&nbsp;});</pre> </p> <p> This will return a sequence of the numbers <code>3, 3, 3</code>. That seems, however, like quite a convoluted way of getting the lengths of some strings. A normal <code>Select</code> method would have sufficed. </p> <p> Is it possible to repeat the above password enumeration in C#? In order to do that, you need a function that takes six <code>string</code> arguments and returns a <code>string</code>: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;concat&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;(x,&nbsp;y,&nbsp;z,&nbsp;æ,&nbsp;ø,&nbsp;å)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;y&nbsp;+&nbsp;z&nbsp;+&nbsp;æ&nbsp;+&nbsp;ø&nbsp;+&nbsp;å;</pre> </p> <p> With programmers' penchant to start with the variable name <code>x</code>, and continue with <code>y</code> and <code>z</code>, most people will have a problem with six variables - but not us Danes! Fortunately, we've officially added three extra letters to our alphabet for this very purpose! So with that small problem out of the way, you can now attempt to reproduce the above F# code: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;combinations&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;concat&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;P&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;ssw&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;o&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;rd&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>&nbsp;});</pre> </p> <p> That looks promising, but there's one problem: <em>it doesn't compile</em>. </p> <p> The problem is that <code>concat</code> is a function that takes six arguments, and the above <code>Apply</code> method expects <code>selectors</code> to be functions that take exactly one argument. </p> <p> Alas, while it's not pretty, you can attempt to address the problem with an overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">T6</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">T6</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">T6</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;selectors.SelectMany(f&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.Select(x&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">T6</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;g&nbsp;=&nbsp;(y,&nbsp;z,&nbsp;æ,&nbsp;ø,&nbsp;å)&nbsp;=&gt;&nbsp;f(x,&nbsp;y,&nbsp;z,&nbsp;æ,&nbsp;ø,&nbsp;å); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;g; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})); }</pre> </p> <p> This overload of <code>Apply</code> takes <code>selectors</code> of arity six, and return a sequence of functions with arity five. </p> <p> Does it work now, then? </p> <p> Unfortunately, it still doesn't compile, because <code>new[] { concat }.Apply(new[] { "P", "p" })</code> has the type <code>IEnumerable&lt;Func&lt;string, string, string, string, string, string&gt;&gt;</code>, and no overload of <code>Apply</code> exists that supports <code>selectors</code> with arity five. </p> <p> You'll have to add such an overload as well: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;selectors.SelectMany(f&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.Select(x&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;g&nbsp;=&nbsp;(y,&nbsp;z,&nbsp;æ,&nbsp;ø)&nbsp;=&gt;&nbsp;f(x,&nbsp;y,&nbsp;z,&nbsp;æ,&nbsp;ø); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;g; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})); }</pre> </p> <p> You can probably see where this is going. This overload returns a sequence of functions with arity four, so you'll have to add an <code>Apply</code> overload for such functions as well, plus for functions with arity three and two. Once you've done that, the above <a href="https://martinfowler.com/bliki/FluentInterface.html">fluent</a> chain of <code>Apply</code> method calls work, and you get a sequence containing all the password variations. </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;concat&nbsp;} . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;P&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;ssw&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;o&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;rd&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>&nbsp;}) string[16] { "Password", "Password!", "Passw0rd", "Passw0rd!", "P4ssword", "P4ssword!", "P4ssw0rd", "P4ssw0rd!", "password", "password!", "passw0rd", "passw0rd!", "p4ssword", "p4ssword!", "p4ssw0rd", "p4ssw0rd!" }</pre> </p> <p> In F# and Haskell, the compiler automatically figures out the return type of each application, due to a combination of currying and <a href="https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system">Hindley–Milner type inference</a>. Perhaps I've allowed my C# skills to atrophy, but I don't think there's an easy resolution to this problem in C#. </p> <p> Obviously, you can always write a reusable library with <code>Apply</code> overloads that support up to some absurd arity. Once those methods are written, they're unlikely to change. Still, it seems to me that we're pushing the envelope. </p> <h3 id="5f60025e198f42ecbba731a0869217ed"> Summary <a href="#5f60025e198f42ecbba731a0869217ed" title="permalink">#</a> </h3> <p> In this article, you saw how to turn C# sequences into an applicative functor. While possible, there are some bumps in the road. </p> <p> There's still an aspect of using lists and sequences as applicative functors that I've been deliberately ignoring so far. The next article covers that. After that, we'll take a break from lists and look at some other applicative functors. </p> <p> <strong>Next:</strong> <a href="/2018/10/22/applicative-combinations-of-functions">Applicative combinations of functions</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0ded7ac93aad8ba7b1063dd49c2051f1"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#0ded7ac93aad8ba7b1063dd49c2051f1">#</a></div> <div class="comment-content"> <blockquote> In F# and Haskell, the compiler automatically figures out the return type of each application, due to a combination of currying and Hindley–Milner type inference. Perhaps I've allowed my C# skills to atrophy, but I don't think there's an easy resolution to this problem in C#. </blockquote> <p> I think the difference is that all functions in F# and Haskell are automatically curried, but nothing is automatically curreid in C#. If you explicitly curry <code>concat</code>, then the code complies and works as expected. Here is one way to achieve that. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;combinations&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;LanguageExt.<span style="color:#2b91af;">Prelude</span>.curry(concat)&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;P&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;ssw&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;o&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;rd&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>&nbsp;});</pre> </p> <p> In this example, I curried <code>concat</code> using <a href="https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/Prelude/Prelude_Curry.cs#L65"><code>curry</code></a> from the NuGet package <a href="https://github.com/louthy/language-ext">LanguageExt</a>. It is a base class library for functional programming in C#. </p> <p> So you don't need many overloads of your <code>Apply</code> for varrying numbers of type parameters. You just need many overloads of <code>curry</code>. </p> </div> <div class="comment-date">2018-10-18 01:45 UTC</div> </div> <div class="comment" id="b4e76681ea894aa3be1e6b836343c148"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b4e76681ea894aa3be1e6b836343c148">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Yes, you're right that it's the lack of default currying that makes this sort of style less than ideal in C#. This seems to be clearly demonstrated by your example. </p> </div> <div class="comment-date">2018-10-30 7:43 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Full deck https://blog.ploeh.dk/2018/10/08/full-deck 2018-10-08T06:17:00+00:00 Mark Seemann <div id="post"> <p> <em>An introduction to applicative functors in Haskell, with a translation to F#.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. While (non-applicative) <a href="/2018/03/22/functors">functors</a> can be translated to an object-oriented language like C# in a straightforward manner, applicative functors are more entrenched in functional languages like <a href="https://www.haskell.org">Haskell</a>. This article introduces the concept with a motivating example in Haskell, and also shows a translation to <a href="https://fsharp.org">F#</a>. In the next article, you'll also see how to implement an applicative functor in C#. </p> <h3 id="bdf22a40f9f04b6b9929f46dbafe7911"> Deck of cards in Haskell <a href="#bdf22a40f9f04b6b9929f46dbafe7911" title="permalink">#</a> </h3> <p> Imagine that you want to model a card game. In order to do so, you start by defining data types for suits, faces, and cards: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Suit</span>&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#dd0000;">Diamonds</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Hearts</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Clubs</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Spades</span>&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Enum</span>,&nbsp;<span style="color:#a31515;">Bounded</span>) <span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Face</span>&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Two</span>&nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Three</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Four</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Five</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Six</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Seven</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Eight</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Nine</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Ten</span> &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Jack</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Queen</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">King</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Ace</span> &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Enum</span>,&nbsp;<span style="color:#a31515;">Bounded</span>) <span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Card</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Card</span>&nbsp;{&nbsp;face&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Face</span>,&nbsp;suit&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Suit</span>&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>)</pre> </p> <p> Since both <code>Suit</code> and <code>Face</code> are instances of the <code>Enum</code> and <code>Bounded</code> typeclasses, you can easily enumerate them: </p> <p> <pre><span style="color:#600277;">allFaces</span>&nbsp;::&nbsp;[<span style="color:blue;">Face</span>] allFaces&nbsp;<span style="color:#666666;">=</span>&nbsp;[minBound&nbsp;<span style="color:#666666;">..</span>&nbsp;maxBound] <span style="color:#600277;">allSuits</span>&nbsp;::&nbsp;[<span style="color:blue;">Suit</span>] allSuits&nbsp;<span style="color:#666666;">=</span>&nbsp;[minBound&nbsp;<span style="color:#666666;">..</span>&nbsp;maxBound]</pre> </p> <p> For example, <code>allSuits</code> enumerates all four <code>Suit</code> values: </p> <p> <pre>λ&gt; allSuits [Diamonds,Hearts,Clubs,Spades]</pre> </p> <p> Notice, by the way, how the code for <code>allFaces</code> and <code>allSuits</code> is identical. The behaviour, however, is different, because the types are different. </p> <p> While you can enumerate suits and faces, <em>how do you create a full deck of cards?</em> </p> <p> A full deck of cards should contain one card of every possible combination of suit and face. Here's one way to do it, taking advantage of lists being applicative functors: </p> <p> <pre><span style="color:#600277;">fullDeck</span>&nbsp;::&nbsp;[<span style="color:blue;">Card</span>] fullDeck&nbsp;<span style="color:#666666;">=</span>&nbsp;pure&nbsp;<span style="color:#dd0000;">Card</span>&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;allFaces&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;allSuits</pre> </p> <p> This will give you all the possible cards. Here are the first six: </p> <p> <pre>λ&gt; forM_ (take 6 fullDeck) print Card {face = Two, suit = Diamonds} Card {face = Two, suit = Hearts} Card {face = Two, suit = Clubs} Card {face = Two, suit = Spades} Card {face = Three, suit = Diamonds} Card {face = Three, suit = Hearts}</pre> </p> <p> How does it work? Let's break it down, starting from left: </p> <p> <pre>λ&gt; :type Card Card :: Face -&gt; Suit -&gt; Card λ&gt; :type pure Card pure Card :: Applicative f =&gt; f (Face -&gt; Suit -&gt; Card) λ&gt; :type pure Card &lt;*&gt; allFaces pure Card &lt;*&gt; allFaces :: [Suit -&gt; Card] λ&gt; :type pure Card &lt;*&gt; allFaces &lt;*&gt; allSuits pure Card &lt;*&gt; allFaces &lt;*&gt; allSuits :: [Card]</pre> </p> <p> From the top, <code>Card</code> is a function that takes a <code>Face</code> value and a <code>Suit</code> value and returns a <code>Card</code> value. Object-oriented programmers can think of it as a constructor. </p> <p> Next, <code>pure Card</code> is the <code>Card</code> function, elevated to an applicative functor. At this point, the compiler hasn't decided which particular applicative functor it is; it could be any applicative functor. Specifically, it turns out to be the list type (<code>[]</code>), which means that <code>pure Card</code> has the type <code>[Face -&gt; Suit -&gt; Card]</code>. That is: it's a list of functions, but a list of only a single function. At this point, however, this is still premature. The type doesn't materialise until we apply the second expression. </p> <p> The type of <code>allFaces</code> is clearly <code>[Face]</code>. Since the <code>&lt;*&gt;</code> operator has the type <code>Applicative f =&gt; f (a -&gt; b) -&gt; f a -&gt; f b</code>, the expression on the left must be the same functor as the expression on the right. The list type (<code>[]</code>) is an applicative functor, and because <code>allFaces</code> is a list, then <code>pure Card</code> must also be a list, in the expression <code>pure Card &lt;*&gt; allFaces</code>. In other words, in the definition of <code>&lt;*&gt;</code>, you can substitute <code>f</code> with <code>[]</code>, and <code>a</code> with <code>Face</code>. The interim result is <code>[Face -&gt; b] -&gt; [Face] -&gt; [b]</code>. What is <code>b</code>, then? </p> <p> You already know that <code>pure Card</code> has the type <code>[Face -&gt; Suit -&gt; Card]</code>, so <code>b</code> must be <code>Suit -&gt; Card</code>. That's the reason that <code>pure Card &lt;*&gt; allFaces</code> has the type <code>[Suit -&gt; Card]</code>. It's a list of functions. This means that you can use <code>&lt;*&gt;</code> a second time, this time with <code>allSuits</code>, which has the type <code>[Suit]</code>. </p> <p> Using the same line of reasoning as before, you can substitute <code>Suit</code> for <code>a</code> in the type of <code>&lt;*&gt;</code>, and you get <code>[Suit -&gt; b] -&gt; [Suit] -&gt; [b]</code>. What is <code>b</code> now? From the previous step, you know that the expression on the left has the type <code>[Suit -&gt; Card]</code>, so <code>b</code> must be <code>Card</code>. That's why the entire expression has the type <code>[Card]</code>. </p> <h3 id="570f38cdb46b420e904fb656faa662ff"> Deck of cards in F# <a href="#570f38cdb46b420e904fb656faa662ff" title="permalink">#</a> </h3> <p> You can translate the above Haskell code to F#. The first step is to make F# lists applicative. F# also supports custom operators, so you can add a function called <code>&lt;*&gt;</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;list&nbsp;-&gt;&nbsp;&#39;a&nbsp;list&nbsp;-&gt;&nbsp;&#39;b&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;fs&nbsp;l&nbsp;=&nbsp;fs&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">collect</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;l&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>)</pre> </p> <p> This implementation iterates over all the functions in <code>fs</code>; for each function, it maps the list <code>l</code> with that function. Had you done that with a normal <code>List.map</code>, this would have returned a list of lists, but by using <code>List.collect</code>, you flatten the list. </p> <p> It's worth noting that this isn't the only way you could have implemented <code>&lt;*&gt;</code>, but this is the implementation that behaves like the Haskell function. An alternative implementation could have been this: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;list&nbsp;-&gt;&nbsp;&#39;a&nbsp;list&nbsp;-&gt;&nbsp;&#39;b&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;fs&nbsp;=&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">collect</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;fs&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x))</pre> </p> <p> This variation has the same type as the first example, and still returns all combinations, but the order is different. In the following of this article, as well as in subsequent articles, I'll use the first version. </p> <p> Here's the playing cards example translated to F#: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Suit</span>&nbsp;=&nbsp;<span style="color:navy;">Diamonds</span>&nbsp;|&nbsp;<span style="color:navy;">Hearts</span>&nbsp;|&nbsp;<span style="color:navy;">Clubs</span>&nbsp;|&nbsp;<span style="color:navy;">Spades</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Face</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;<span style="color:navy;">Two</span>&nbsp;|&nbsp;<span style="color:navy;">Three</span>&nbsp;|&nbsp;<span style="color:navy;">Four</span>&nbsp;|&nbsp;<span style="color:navy;">Five</span>&nbsp;|&nbsp;<span style="color:navy;">Six</span>&nbsp;|&nbsp;<span style="color:navy;">Seven</span>&nbsp;|&nbsp;<span style="color:navy;">Eight</span>&nbsp;|&nbsp;<span style="color:navy;">Nine</span>&nbsp;|&nbsp;<span style="color:navy;">Ten</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Jack</span>&nbsp;|&nbsp;<span style="color:navy;">Queen</span>&nbsp;|&nbsp;<span style="color:navy;">King</span>&nbsp;|&nbsp;<span style="color:navy;">Ace</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Card</span>&nbsp;=&nbsp;{&nbsp;Face:&nbsp;<span style="color:teal;">Face</span>;&nbsp;Suit&nbsp;:&nbsp;<span style="color:teal;">Suit</span>&nbsp;} <span style="color:green;">//&nbsp;Face&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;allFaces&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Two</span>;&nbsp;<span style="color:navy;">Three</span>;&nbsp;<span style="color:navy;">Four</span>;&nbsp;<span style="color:navy;">Five</span>;&nbsp;<span style="color:navy;">Six</span>;&nbsp;<span style="color:navy;">Seven</span>;&nbsp;<span style="color:navy;">Eight</span>;&nbsp;<span style="color:navy;">Nine</span>;&nbsp;<span style="color:navy;">Ten</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Jack</span>;&nbsp;<span style="color:navy;">Queen</span>;&nbsp;<span style="color:navy;">King</span>;&nbsp;<span style="color:navy;">Ace</span>] <span style="color:green;">//&nbsp;Suit&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;allSuits&nbsp;=&nbsp;[<span style="color:navy;">Diamonds</span>;&nbsp;<span style="color:navy;">Hearts</span>;&nbsp;<span style="color:navy;">Clubs</span>;&nbsp;<span style="color:navy;">Spades</span>] <span style="color:green;">//&nbsp;Card&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;fullDeck&nbsp;=&nbsp;[<span style="color:blue;">fun</span>&nbsp;f&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;Face&nbsp;=&nbsp;f;&nbsp;Suit&nbsp;=&nbsp;s&nbsp;}]&nbsp;&lt;*&gt;&nbsp;allFaces&nbsp;&lt;*&gt;&nbsp;allSuits</pre> </p> <p> The F# code is slightly more verbose than the Haskell code, because you have to repeat all the cases for <code>Suit</code> and <code>Face</code>. You can't enumerate them automatically, like you can in Haskell. </p> <p> It didn't make much sense to me to attempt to define a <code>pure</code> function, so instead I simply inserted a single lambda expression in a list, using the normal square-bracket syntax. F# doesn't have constructors for record types, so you have to pass a lambda expression, whereas in Haskell, you could simply use the <code>Card</code> function. </p> <p> The result is the same, though: </p> <p> <pre>&gt; fullDeck |&gt; List.take 6 |&gt; List.iter (printfn "%A");; {Face = Two; Suit = Diamonds;} {Face = Two; Suit = Hearts;} {Face = Two; Suit = Clubs;} {Face = Two; Suit = Spades;} {Face = Three; Suit = Diamonds;} {Face = Three; Suit = Hearts;}</pre> </p> <p> While the mechanics of applicative functors translate well to F#, it leaves you with at least one problem. If you add the above operator <code>&lt;*&gt;</code>, you've now 'used up' that operator for lists. While you <em>can</em> define an operator of the same name for e.g. <code>option</code>, you'd have to put them in separate modules or namespaces in order to prevent them from colliding. This also means that you can't easily use them together. </p> <p> For that reason, I wouldn't consider this the most <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> way to create a full deck of cards in F#. Normally, I'd do this instead: </p> <p> <pre><span style="color:green;">//&nbsp;Card&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;fullDeck&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;suit&nbsp;<span style="color:blue;">in</span>&nbsp;allSuits&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;face&nbsp;<span style="color:blue;">in</span>&nbsp;allFaces&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;{&nbsp;Face&nbsp;=&nbsp;face;&nbsp;Suit&nbsp;=&nbsp;suit&nbsp;}&nbsp;]</pre> </p> <p> This alternative syntax takes advantage of F#'s 'extended list comprehension' syntax. FWIW, you could have done something similar in Haskell: </p> <p> <pre><span style="color:#600277;">fullDeck</span>&nbsp;::&nbsp;[<span style="color:blue;">Card</span>] fullDeck&nbsp;<span style="color:#666666;">=</span>&nbsp;[<span style="color:#dd0000;">Card</span>&nbsp;f&nbsp;s&nbsp;<span style="color:#666666;">|</span>&nbsp;f&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;allFaces,&nbsp;s&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;allSuits]</pre> </p> <p> List comprehension, however, is (as the name implies) specific to lists, whereas an <em>applicative functor</em> is a more general concept. </p> <h3 id="cef0030207354d1fbf0ab2c1a791eb84"> Summary <a href="#cef0030207354d1fbf0ab2c1a791eb84" title="permalink">#</a> </h3> <p> This article introduced you to lists as an applicative functor, using the motivating example of having to populate a full deck of cards with all possible combinations of suits and faces. </p> <p> The next article in the series shows another list example. The F# and Haskell code will be similar to the code in the present article, but the next article will also include a translation to C#. </p> <p> <strong>Next:</strong> <a href="/2018/10/15/an-applicative-password-list">An applicative password list</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative functors https://blog.ploeh.dk/2018/10/01/applicative-functors 2018-10-01T06:34:00+00:00 Mark Seemann <div id="post"> <p> <em>An applicative functor is a useful abstraction. While typically associated with functional programming, applicative functors can be conjured into existence in C# as well.</em> </p> <p> This article series is part of <a href="/2018/03/19/functors-applicatives-and-friends">a larger series of articles about functors, applicatives, and other mappable containers</a>. </p> <p> In a <a href="/2018/03/22/functors">former article series</a>, you learned about functors, and how they also exist in object-oriented design. Perhaps the utility of this still eludes you, although, if you've ever had experience with LINQ in C#, you should realise that the abstraction is invaluable. Functors are abundant; <em>applicative functors</em> not quite so much. On the other hand, applicative functors enable you to <em>do</em> more. </p> <p> I find it helpful to think of applicative functors as an abstraction that enable you to express <em>combinations</em> of things. </p> <p> In the functor article series, I mostly focused on the mechanics of implementation. In this article series, I think that you'll find it helpful to slightly change the perspective. In these articles, I'll show you various motivating examples of how applicative functors are useful. <ul> <li><a href="/2018/10/08/full-deck">Full deck</a></li> <li><a href="/2018/10/15/an-applicative-password-list">An applicative password list</a></li> <li><a href="/2018/10/22/applicative-combinations-of-functions">Applicative combinations of functions</a></li> <li><a href="/2018/10/29/the-maybe-applicative-functor">The Maybe applicative functor</a></li> <li><a href="/2018/11/05/applicative-validation">Applicative validation</a></li> <li><a href="/2018/11/26/the-test-data-generator-applicative-functor">The Test Data Generator applicative functor</a></li> <li><a href="/2018/12/10/danish-cpr-numbers-in-f">Danish CPR numbers in F#</a></li> <li><a href="/2018/12/17/the-lazy-applicative-functor">The Lazy applicative functor</a></li> <li><a href="/2019/04/22/applicative-monoids">Applicative monoids</a></li> </ul> You should consider reading one or more of these articles before continuing the present article. </p> <h3 id="6173a96aedaa4e97ad868c159e6d06fa"> A Haskell perspective <a href="#6173a96aedaa4e97ad868c159e6d06fa" title="permalink">#</a> </h3> <p> A normal functor maps objects in an 'elevated world' (like C#'s <code>IEnumerable&lt;T&gt;</code> or <code>IObservable&lt;T&gt;</code>) using a function in the 'normal world'. As a variation, an applicative functor maps objects in an 'elevated world' using functions from the same 'elevated world'. </p> <p> In <a href="https://haskell.org">Haskell</a>, an applicative functor is defined <em>like</em> this: </p> <p> <pre><span style="color:blue;">class</span>&nbsp;Functor&nbsp;f&nbsp;=&gt;&nbsp;<span style="color:#a31515;">Applicative</span>&nbsp;f&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:#600277;">pure</span>&nbsp;&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;a &nbsp;&nbsp;(&lt;*<span style="color:blue;">&gt;</span>)&nbsp;::&nbsp;f&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;b </pre> </p> <p> This is a <strong>simplification</strong>; there's more to the <code>Applicative</code> typeclass than this, but this should highlight the essence. What it says is that an applicative functor must already be a <code>Functor</code>. It could be any sort of <code>Functor</code>, like <code>[]</code> (linked list), <code>Maybe</code>, <code>Either</code>, and so on. Since <code>Functor</code> is an abstraction, it's called <code>f</code>. </p> <p> The definition furthermore says that in order for a functor to be applicative, two functions must exist: <code>pure</code> and <code>&lt;*&gt;</code> (<em>'apply'</em>). </p> <p> <code>pure</code> is easy to understand. It simply 'elevates' a 'normal' value to a functor value. For example, you can elevate the number <code>42</code> to a list value by putting it in a list with a single element: <code>[42]</code>. Or you can elevate <code>"foo"</code> to <code>Maybe</code> by containing it in the <code>Just</code> case: <code>Just "foo"</code>. That is, literally, what <code>pure</code> does for <code>[]</code> (list) and <code>Maybe</code>. </p> <p> The <code>&lt;*&gt;</code> operator applies an 'elevated' function to an 'elevated' value. When <code>f</code> is <code>[]</code>, this literally means that you have a list of functions that you have to apply to a list of values. Perhaps you can already see what I meant by <em>combinations</em> of things. </p> <p> This sounds abstract, but follow the above list of links in order to see several examples. </p> <h3 id="6f9b2f13951e475d8c4fc9682ad96a94"> An F# perspective <a href="#6f9b2f13951e475d8c4fc9682ad96a94" title="permalink">#</a> </h3> <p> Applicative functors aren't explicitly modelled in <a href="https://fsharp.org">F#</a>, but they're easy enough to add if you need them. F# doesn't have typeclasses, so implementing applicative functors tend to be more on a case-by-case basis. </p> <p> If you need <code>list</code> to be applicative, <code>pure</code> should have the type <code>'a -&gt; 'a list</code>, and <code>&lt;*&gt;</code> should have the type <code>('a -&gt; 'b) list -&gt; 'a list -&gt; 'b list</code>. At this point, you already run into the problem that <code>pure</code> is a reserved keyword in F#, so you'll have to find another name, or simply ignore that function. </p> <p> If you need <code>option</code> to be applicative, <code>&lt;*&gt;</code> should have the type <code>('a -&gt; 'b) option -&gt; 'a option -&gt; 'b option</code>. Now you run into your second problem, because which function is <code>&lt;*&gt;</code>? The one for <code>list</code>, or the one for <code>option</code>? It can't be both, so you'll have to resort to all sorts of hygiene to prevent these two versions of the same operator from clashing. This somewhat limits its usefulness. </p> <p> Again, refer to the above list of linked articles for concrete examples. </p> <h3 id="cef395ee19644f30bfd1ad7a84b6f912"> A C# perspective <a href="#cef395ee19644f30bfd1ad7a84b6f912" title="permalink">#</a> </h3> <p> Applicative functors push the limits of what you can express in C#, but the equivalent to <code>&lt;*&gt;</code> would be a method with this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source)</pre> </p> <p> Here, the class <code>Functor&lt;T&gt;</code> is a place-holder for a proper functor class. A concrete example could be for <code>IEnumerable&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source)</pre> </p> <p> As you can see, here you somehow have to figure out how to combine a sequence of functions with a sequence of values. </p> <p> In some of the examples in the above list of linked articles, you'll see how this will stretch the capability of C#. </p> <h3 id="ffa16273a2a0428f90190f8d559cf0a6"> Summary <a href="#ffa16273a2a0428f90190f8d559cf0a6" title="permalink">#</a> </h3> <p> This article only attempts to provide an overview of applicative functors, while examples are given in linked articles. I find it helpful to think of applicative functors as an abstraction that enables you to model arbitrary <em>combinations</em> of objects. This is a core feature in Haskell, occasionally useful in F#, and somewhat alien to C#. </p> <p> <strong>Next:</strong> <a href="/2018/10/08/full-deck">Full deck</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Asynchronous functors https://blog.ploeh.dk/2018/09/24/asynchronous-functors 2018-09-24T03:37:00+00:00 Mark Seemann <div id="post"> <p> <em>Asynchronous computations form functors. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. The previous article covered the <a href="/2018/09/10/the-lazy-functor">Lazy functor</a>. In this article, you'll learn about closely related functors: .NET Tasks and <a href="https://fsharp.org">F#</a> <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/async-expressions">asynchronous workflows</a>. </p> <p> A word of warning is in order. .NET Tasks aren't <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>, whereas F# asynchronous computations are. You could argue, then, that .NET Tasks aren't proper functors, but you mostly observe the difference when you perform impure operations. As a general observation, when impure operations are allowed, the conclusions of <a href="/2017/10/04/from-design-patterns-to-category-theory">this overall article series</a> are precarious. We can't radically change how the .NET languages work, so we'll have to soldier on, pretending that impure operations are delegated to other parts of our system. Under this undue assumption, we can pretend that <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1">Task&lt;T&gt;</a> forms a functor. </p> <h3 id="f82479d93d5d40ed83749a6d9fc309dc"> Task functor <a href="#f82479d93d5d40ed83749a6d9fc309dc" title="permalink">#</a> </h3> <p> You can write an idiomatic <code>Select</code> extension method for <code>Task&lt;T&gt;</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;selector(x); }</pre> </p> <p> With this extension method in scope, you can compose asynchronous computations like this: </p> <p> <pre><span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y&nbsp;=&nbsp;x.Select(i&nbsp;=&gt;&nbsp;i.ToString());</pre> </p> <p> Or, if you prefer <em>query syntax:</em> </p> <p> <pre><span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;x&nbsp;<span style="color:blue;">select</span>&nbsp;i.ToString();</pre> </p> <p> In both cases, you start with a <code>Task&lt;int&gt;</code> which you map into a <code>Task&lt;string&gt;</code>. Perhaps you've noticed that these two examples closely resemble the equivalent <a href="/2018/09/10/the-lazy-functor">Lazy functor examples</a>. The resemblance isn't coincidental. The same <em>abstraction</em> is in use in both places. This is the <em>functor</em> abstraction. That's what this article series is all about, after all. </p> <p> The difference between the Task functor and the Lazy functor is that lazy computations don't run until forced. Tasks, on the other hand, typically start running in the background when created. Thus, when you finally <code>await</code> the value, you may actually not have to wait for it. This does, however, depend on how you created the initial task. </p> <h3 id="8d66efff8d5a4e73b3007878b3ec7227"> First functor law for Task <a href="#8d66efff8d5a4e73b3007878b3ec7227" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual in this article series, actually proving that this is the case belongs to the realm of computer science. Instead of proving that the law holds, you can use property-based testing to demonstrate that it does. The following example shows a single property written with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> 2.11.0 and <a href="https://xunit.net/">xUnit.net</a> 2.4.0. </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;TaskObeysFirstFunctorLaw(<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;&nbsp;left&nbsp;=&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(i).Select(x&nbsp;=&gt;&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(left.Result,&nbsp;right.Result); }</pre> </p> <p> This property accesses the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1.result">Result property</a> of the Tasks involved. This is typically not the preferred way to pull the value of Tasks, but I decided to do it like this since <a href="https://github.com/fscheck/FsCheck/issues/167">FsCheck 2.4.0 doesn't support asynchronous properties</a>. </p> <p> Even though you may feel that a property-based test gives you more confidence than a few hard-coded examples, such a test is nothing but a demonstration of the first functor law. It's no proof, and it only demonstrates that the law holds for <code>Task&lt;int&gt;</code>, not that it holds for <code>Task&lt;string&gt;</code>, <code>Task&lt;Product&gt;</code>, etcetera. </p> <h3 id="12b3ea8d8963414496a6c695f130a4b9"> Second functor law for Task <a href="#12b3ea8d8963414496a6c695f130a4b9" title="permalink">#</a> </h3> <p> As is the case with the first functor law, you can also use a property to demonstrate that the second functor law holds: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;TaskObeysSecondFunctorLaw( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;f, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;g, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;&nbsp;left&nbsp;=&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(i).Select(g).Select(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(i).Select(x&nbsp;=&gt;&nbsp;f(g(x))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(left.Result,&nbsp;right.Result); }</pre> </p> <p> Again the same admonitions apply: that property is no proof. It does show, however, that given two functions, <code>f</code> and <code>g</code>, it doesn't matter if you map a Task in one or two steps. The output is the same in both cases. </p> <h3 id="d87480f7e0124db18b22aaacbff3672a"> Async functor <a href="#d87480f7e0124db18b22aaacbff3672a" title="permalink">#</a> </h3> <p> F# had asynchronous workflows long before C#, so it uses a slightly different model, supported by separate types. Instead of <code>Task&lt;T&gt;</code>, F# relies on a generic type called <a href="https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/control.async%5b%27t%5d-type-%5bfsharp%5d">Async&lt;'T&gt;</a>. It's still a functor, since you can trivially implement a <code>map</code> function for it: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Async&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fromValue&nbsp;x&nbsp;=&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;x&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;x&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f&nbsp;x&#39;&nbsp;}</pre> </p> <p> The <code>map</code> function uses the <code>async</code> <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expression</a> to access the value being computed asynchronously. You can use a <code>let!</code> binding to await the value computed in <code>x</code>, and then use the function <code>f</code> to translate that value. The <code>return</code> keyword turns the result into a new <code>Async</code> value. </p> <p> With the <code>map</code> function, you can write code like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;(x&nbsp;:&nbsp;Async&lt;int&gt;)&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;(y&nbsp;:&nbsp;Async&lt;string&gt;)&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;Async.map&nbsp;string</pre> </p> <p> Once you've composed an asynchronous workflow to your liking, you can run it to compute the value in which you're interested: </p> <p> <pre>&gt; Async.RunSynchronously y;; val it : string = "42"</pre> </p> <p> This is the main difference between F# asynchronous workflows and .NET Tasks. You have to explicitly run an asynchronous workflows, whereas Tasks are usually, implicitly, already running in the background. </p> <h3 id="f572e55512ab484584450dc0af7fe9b3"> First functor law for Async <a href="#f572e55512ab484584450dc0af7fe9b3" title="permalink">#</a> </h3> <p> The <code>Async.map</code> function obeys the first functor law. Like above, you can use FsCheck to demonstrate how that works: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Async&nbsp;obeys&nbsp;first&nbsp;functor&nbsp;law``&nbsp;(i&nbsp;:&nbsp;int)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;left&nbsp;=&nbsp;Async.fromValue&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;right&nbsp;=&nbsp;Async.fromValue&nbsp;i&nbsp;|&gt;&nbsp;Async.map&nbsp;id &nbsp;&nbsp;&nbsp;&nbsp;Async.RunSynchronously&nbsp;left&nbsp;=!&nbsp;Async.RunSynchronously&nbsp;right</pre> </p> <p> In addition to FsCheck and xUnit.net, this property also uses <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> 4.0.0 for the assertion. The <code>=!</code> operator is an assertion that the left-hand side <em>must equal</em> the right-hand side. If it doesn't, then the operator throws a descriptive exception. </p> <h3 id="6fd1ba5d9c0647c49ba06d7580b2c552"> Second functor law for Async <a href="#6fd1ba5d9c0647c49ba06d7580b2c552" title="permalink">#</a> </h3> <p> As is the case with the first functor law, you can also use a property to demonstrate that the second functor law holds: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Async&nbsp;obeys&nbsp;second&nbsp;functor&nbsp;law``&nbsp;(f&nbsp;:&nbsp;string&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;byte)&nbsp;g&nbsp;(i&nbsp;:&nbsp;int)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;left&nbsp;=&nbsp;Async.fromValue&nbsp;i&nbsp;|&gt;&nbsp;Async.map&nbsp;g&nbsp;|&gt;&nbsp;Async.map&nbsp;f &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;right&nbsp;=&nbsp;Async.fromValue&nbsp;i&nbsp;|&gt;&nbsp;Async.map&nbsp;(g&nbsp;&gt;&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;Async.RunSynchronously&nbsp;left&nbsp;=!&nbsp;Async.RunSynchronously&nbsp;right</pre> </p> <p> As usual, the second functor law states that given two arbitrary (but <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>) functions <code>f</code> and <code>g</code>, it doesn't matter if you map an asynchronous workflow in one or two steps. There could be a difference in performance, but the functor laws don't concern themselves with the time dimension. </p> <p> Both of the above properties use the <code>Async.fromValue</code> helper function to create <code>Async</code> values. Perhaps you consider it cheating that it immediately produces a value, but you can add a delay if you want to pretend that actual asynchronous computation takes place. It'll make the tests run slower, but otherwise will not affect the outcome. </p> <h3 id="9ad596ae1e5f483cb6d02ad963a0a378"> Task and Async isomorphism <a href="#9ad596ae1e5f483cb6d02ad963a0a378" title="permalink">#</a> </h3> <p> The reason I've covered both <code>Task&lt;T&gt;</code> and <code>Async&lt;'a&gt;</code> in the same article is that they're equivalent. You can translate a Task to an asynchronous workflow, or the other way around. </p> <p> <img src="/content/binary/equivalence-of-task-and-async.png" alt="Equivalence of Task and Async."> </p> <p> There's performance implications of going back and forth between <code>Task&lt;T&gt;</code> and <code>Async&lt;'a&gt;</code>, but there's no loss of information. You can, therefore, consider these to representations of asynchronous computations <a href="/2018/01/08/software-design-isomorphisms">isomorphic</a>. </p> <h3 id="4bb86c51ae37462b94e278247d33f8f5"> Summary <a href="#4bb86c51ae37462b94e278247d33f8f5" title="permalink">#</a> </h3> <p> Whether you do asynchronous programming with <code>Task&lt;T&gt;</code> or <code>Async&lt;'a&gt;</code>, asynchronous computations form functors. This enables you to piecemeal compose asynchronous workflows. </p> <p> <strong>Next:</strong> <a href="/2021/07/19/the-state-functor">The State functor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="a1b5103feeae4188b3d522ecef9f0475"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#a1b5103feeae4188b3d522ecef9f0475">#</a></div> <div class="comment-content"> <blockquote> As a general observation, when impure operations are allowed, the conclusions of <a href="/2017/10/04/from-design-patterns-to-category-theory">this overall article series</a> are precarious. We can't radically change how the .NET languages work, so we'll have to soldier on, pretending that impure operations are delegated to other parts of our system. Under this undue assumption, we can pretend that <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1">Task&lt;T&gt;</a> forms a functor. </blockquote> <p> I am with you. I also want to be pragmatic here and treat <code>Task&lt;T&gt;</code> as a functor. It is definitely better than the alternative. </p> <p> At the same time, I want to know what prevents <code>Task&lt;T&gt;</code> from actually being a functor so that I can compensate when needed. </p> <blockquote> A word of warning is in order. .NET Tasks aren't <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>, whereas F# asynchronous computations are. You could argue, then, that .NET Tasks aren't proper functors, but you mostly observe the difference when you perform impure operations. </blockquote> <p> Can you elaborate about this? Why is <code>Task&lt;T&gt;</code> not referentially transparent? Why is the situation worse with impure operations? What issue remains when restricting to pure functions? Is this related to how the stack trace is closely related to how many times <code>await</code> appears in the code? </p> </div> <div class="comment-date">2020-07-16 13:23 UTC</div> </div> <div class="comment" id="f0bb917b3af24fbe89aac3f4740fbef6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f0bb917b3af24fbe89aac3f4740fbef6">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. That .NET tasks aren't referentially transparent is a result of memoisation, as <a href="/2016/04/11/async-as-surrogate-io#75cd7e6bef364a328e0135f14dc06ab8">Diogo Castro pointed out to me</a>. </p> <p> The coming article about <em>Task asynchronous programming as an IO surrogate</em> will also discuss this topic. </p> </div> <div class="comment-date">2020-07-23 7:50 UTC</div> </div> <div class="comment" id="d7c77086db2647ec9caf5d4a2cc9cc80"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#d7c77086db2647ec9caf5d4a2cc9cc80">#</a></div> <div class="comment-content"> <p> Ha! So funny. It is the same caching behavior that I pointed out in <a href="https://blog.ploeh.dk/2020/07/13/implementation-of-the-c-io-container/#8fea4b2ac22d4211bd91d352c7b4e55e">this comment</a>. I wish I had known about Diogo's comment. I could have just linked there. </p> <blockquote> You could argue, then, that .NET Tasks aren't proper functors, but you mostly observe the difference when you perform impure operations. </blockquote> <p> I am still not completely following though. Are you saying that this caching behavior prevents .NET Tasks from satisfying one of the functor laws? If so, is it the identity law or the composition law? If it is the composition law, what are the two functions that demonstate the failure? Even when allowing impure functions, I can't think of an example. However, I will keep thinking about this and comment again if I find an example. </p> </div> <div class="comment-date">2020-07-23 13:33 UTC</div> </div> <div class="comment" id="cffa5d2189d54264b6f6cc58fb84afff"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cffa5d2189d54264b6f6cc58fb84afff">#</a></div> <div class="comment-content"> <p> Tyson, I am, in general trying to avoid claiming that any of this applies in the presence of impurity. Take something as trivial as <code>Select(i =&gt; new Random().Next(i))</code>, for Task or any other 'functor'. It's not even going to evaluate to the same outcome between two runs. </p> </div> <div class="comment-date">2020-07-31 10:54 UTC</div> </div> <div class="comment" id="9d9bd7d7f1744553b47d6a8632c44c9b"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#9d9bd7d7f1744553b47d6a8632c44c9b">#</a></div> <div class="comment-content"> <p> I think I understand your hesitation now regarding impurity. I also think I can argue that the existence of impure functions in C# does not prevent <code>Task&lt;T&gt;</code> from being a functor. </p> <p> Before I give that argument and we consider if it works, I want to make sure there isn't a simpler issue only involving pure functions the prevents <code>Task&lt;T&gt;</code> from being a functor. </p> <blockquote> You could argue, then, that .NET Tasks aren't proper functors, but you mostly observe the difference when you perform impure operations. </blockquote> <p> Your use of "mostly" makes me think that you believe there is a counterexample only involving pure functions that shows <code>Task&lt;T&gt;</code> is not a functor. Do I correctly understanding what you meant by that sentence? If so, can you share that counterexample? If not, can you elaborate so that I can better understand what you meant. </p> </div> <div class="comment-date">2020-08-02 18:26 UTC</div> </div> <div class="comment" id="4f366ad5ff224fbf921c61fd379d26ea"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4f366ad5ff224fbf921c61fd379d26ea">#</a></div> <div class="comment-content"> <p> Tyson, I wrote this article two years ago. I can't recall if I had anything specific in mind, or if it was just a throwaway phrase. Even if I had a specific counter-example in mind, I might have been wrong, just like the consensus is about <a href="https://en.wikipedia.org/wiki/Fermat's_Last_Theorem">Fermat's last theorem</a>. </p> <p> If you can produce an example, however, you'd prove me right 😀 </p> </div> <div class="comment-date">2020-08-06 9:07 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Typing is not a programming bottleneck https://blog.ploeh.dk/2018/09/17/typing-is-not-a-programming-bottleneck 2018-09-17T08:16:00+00:00 Mark Seemann <div id="post"> <p> <em>Some developers seem to think that typing is a major bottleneck while programming. It's not.</em> </p> <p> I sometimes give programming advice to people. They approach me with a software design problem, and, to the best of my ability, I suggest a remedy. Despite my best intentions, my suggestions sometimes meet resistance. One common reaction is that <a href="/2015/08/03/idiomatic-or-idiosyncratic">my suggestion isn't idiomatic</a>, but recently, another type of criticism seems to be on the rise. </p> <p> The code that I suggest is too verbose. It involves too much typing. </p> <p> I'll use this article to reflect on that criticism. </p> <h3 id="d1f8398f96584ed09acce1b481b45cde"> The purpose of code <a href="#d1f8398f96584ed09acce1b481b45cde" title="permalink">#</a> </h3> <p> Before we get into the details of the issue, I'd like to start with the big picture. What's the purpose of code? </p> <p> I've discussed this extensively in my <a href="https://cleancoders.com">Clean Coders</a> video on <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">Humane Code</a>. In short, the purpose of source code is to <em>communicate</em> the workings of a piece of software to the next programmer who comes along. This could easily include your future self. </p> <p> Perhaps you disagree with that proposition. Perhaps you think that the purpose of source code is to produce working software. It's that, too, but that's not its only purpose. If that was the only purpose, you might as well write the software in machine code. </p> <p> Why do we have high-level programming languages like C#, Java, JavaScript, Ruby, Python, F#, Visual Basic, Haskell, heck - even C++? </p> <p> As far as I can tell, it's because programmers are all human, and humans have limited cognitive capacity. We can't keep track of hundreds, or thousands, of global variables, or the states of dozens of complex resources. We need tools that help us structure and understand complex systems so that they're broken down into (more) manageable chunks. High-level languages help us do that. </p> <p> The purpose of source code is to be understood. You read source code much more that you write. I'm not aware of any scientific studies, but I think most programmers will agree that over the lifetime of a code base, any line of code will be read orders of magnitude more often that it's edited. </p> <h3 id="b82afa298f114b8c89113ea5f40fc278"> Typing speed <a href="#b82afa298f114b8c89113ea5f40fc278" title="permalink">#</a> </h3> <p> How many lines of code do you produce during a productive working day? </p> <p> To be honest, I can't even answer that question myself. I've never measured it, since I consider it to be irrelevant. <a href="http://bit.ly/mythical-man-month">The Mythical Man-Month</a> gives the number as <em>10</em>, but let's be generous and pretend it's ten times that. This clearly depends on lots of factors, such as the language in which you're writing, the state of the code base, and so on. You'd tend to write more lines in a pristine greenfield code base, whereas you'll write fewer lines of code in a complicated legacy code base. </p> <p> How many characters is a line of code? Let's say it's 80 characters, because <a href="/2019/11/04/the-80-24-rule">that's the maximum width code ought to have</a>. I realise that many people write wider lines, but on the other hand, most developers (fortunately) use indentation, so as a counter-weight, code often has some blank space to the left as well. This is all back-of-the-envelope calculations anyway. </p> <p> When I worked in a Microsoft product group, we typically planned that a productive, 'full' day of coding was five hours. Even on productive days, the rest was used in meetings, administration, breaks, and so on. </p> <p> If you write code for five hours, and produce 100 lines of code, at 80 characters per line, that's 8,000 characters. Your IDE is likely to help you with statement completion and such, but for the sake of argument, let's pretend that you have to type it all in. </p> <p> 8,000 characters in five hours is 1,600 characters per hour, or 27 characters per minute. </p> <p> I'm not a particularly fast typist, but I can type ten times faster than that. </p> <p> Typing isn't a bottleneck. </p> <h3 id="cad0abab133848d98b4aada5e31d35d6"> Is more code worse? <a href="#cad0abab133848d98b4aada5e31d35d6" title="permalink">#</a> </h3> <p> I tend to <a href="/2013/02/04/BewareofProductivityTools">get drawn into these discussions from time to time</a>, but good programming has little to do with how fast you can produce lines of code. </p> <p> To be clear, I entirely accept that statement completion, refactoring support, and such are, in general, benign features. While I spend most of my programming time thinking and reading, I also tend to write code in bursts. The average count of lines per hour may not be great, but that's because averages smooth out the hills and the valleys of my activity. </p> <p> Still, increasingly frequent objection to some of my suggestions is that what I suggest implies 'too much' code. Recently, for example, I had to defend the merits of the <a href="https://martinfowler.com/bliki/FluentInterface.html">Fluent</a> Builder pattern that I suggest in my <a href="/2014/05/19/di-friendly-library">DI-Friendly Library</a> article. </p> <p> As another example, consider two alternatives for modelling a restaurant reservation. First, here's a terse option: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Reservation</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Date&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsAccepted&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> Here's a longer alternative: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Reservation</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>(date,&nbsp;name,&nbsp;email,&nbsp;quantity,&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;isAccepted) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;date; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;email; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;quantity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsAccepted&nbsp;=&nbsp;isAccepted; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Date&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsAccepted&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;WithDate(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;newDate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(newDate,&nbsp;Name,&nbsp;Email,&nbsp;Quantity,&nbsp;IsAccepted); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;WithName(<span style="color:blue;">string</span>&nbsp;newName) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(Date,&nbsp;newName,&nbsp;Email,&nbsp;Quantity,&nbsp;IsAccepted); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;WithEmail(<span style="color:blue;">string</span>&nbsp;newEmail) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(Date,&nbsp;Name,&nbsp;newEmail,&nbsp;Quantity,&nbsp;IsAccepted); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;WithQuantity(<span style="color:blue;">int</span>&nbsp;newQuantity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(Date,&nbsp;Name,&nbsp;Email,&nbsp;newQuantity,&nbsp;IsAccepted); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;Accept() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(Date,&nbsp;Name,&nbsp;Email,&nbsp;Quantity,&nbsp;<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(Date,&nbsp;other.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(Name,&nbsp;other.Name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(Email,&nbsp;other.Email) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(Quantity,&nbsp;other.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(IsAccepted,&nbsp;other.IsAccepted); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date.GetHashCode()&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name.GetHashCode()&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email.GetHashCode()&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity.GetHashCode()&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsAccepted.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Which alternative is better? The short version is eight lines of code, including the curly brackets. The longer version is 78 lines of code. That's ten times as much. </p> <p> I prefer the longer version. While it takes longer to type, it comes with several benefits. The main benefit is that because it's immutable, it can have structural equality. This makes it trivial to compare objects, which, for example, is something you do all the time in unit test assertions. Another benefit is that such <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">Value Objects make better domain models</a>. The above <code>Reservation</code> <a href="https://martinfowler.com/bliki/ValueObject.html">Value Object</a> only shows the slightest sign of emerging domain logic in the <code>Accept</code> method, but once you start modelling like this, such objects seem to attract more domain behaviour. </p> <h3 id="78654bcab6ee4c57a24a5a41adf6c0fa"> Maintenance burden <a href="#78654bcab6ee4c57a24a5a41adf6c0fa" title="permalink">#</a> </h3> <p> Perhaps you're now convinced that typing speed may not be the bottleneck, but you still feel that you don't like the verbose <code>Reservation</code> alternative. More code could be an increased maintenance burden. </p> <p> Consider those <code>With[...]</code> methods, such as <code>WithName</code>, <code>WithQuantity</code>, and so on. Once you make objects immutable, such copy-and-update methods become indispensable. They enable you to change a single property of an object, while keeping all other values intact: </p> <p> <pre>&gt; <span style="color:blue;">var</span>&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span>(<span style="color:#2b91af;">DateTimeOffset</span>.Now,&nbsp;<span style="color:#a31515;">&quot;Foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;foo@example.com&quot;</span>,&nbsp;3); &gt; r.WithQuantity(4) Reservation { Date=[11.09.2018 19:19:29 +02:00], Email="foo@example.com", IsAccepted=false, Name="Foo", Quantity=4 }</pre> </p> <p> While convenient, such methods can increase the maintenance burden. If you realise that you need to change the name of one of the properties, you'll have to remember to also change the name of the copy-and-update method. For example, if you change <code>Quantity</code> to <code>NumberOfGuests</code>, you'll have to also remember to rename <code>WithQuantity</code> to <code>WithNumberOfGuests</code>. </p> <p> I'm not sure that I'm ready to concede that this is a prohibitive strain on the sustainability of a code base, but I do grant that it's a nuisance. This is one of the many reasons that I prefer to use programming languages better equipped for such domain modelling. In <a href="https://fsharp.org">F#</a>, for example, a record type similar to the above immutable <code>Reservation</code> class would be a one-liner: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Date&nbsp;:&nbsp;DateTimeOffset;&nbsp;Name&nbsp;:&nbsp;string;&nbsp;Email&nbsp;:&nbsp;string;&nbsp;Quantity&nbsp;:&nbsp;int;&nbsp;IsAccepted&nbsp;:&nbsp;bool&nbsp;}</pre> </p> <p> Such a declarative approach to types produces an immutable record with the same capabilities as the 78 lines of C# code. </p> <p> That's a different story, though. There's little correlation between the size of code, and how 'good' it is. Sometimes, less code is better; sometimes, more code is better. </p> <h3 id="ec55edfbe2a245b0b56188d4e8c6fbfb"> Summary <a href="#ec55edfbe2a245b0b56188d4e8c6fbfb" title="permalink">#</a> </h3> <p> I'll dispense with the usual Edsger Dijkstra and Bill Gates quotes on lines of code. The point that lines of code is a useless metric in software development has been made already. My point is a corollary. Apparently, it has to be explicitly stated: <em>Programmer productivity has nothing to do with typing speed.</em> </p> <p> Unless you're disabled in some way, you can type fast enough to be a productive programmer. Typing isn't a programming bottleneck. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4bfb1d54e38449c7bc5d30e171d01179"> <div class="comment-author"><a href="http://honestillusion.com">James Curran</a> <a href="#4bfb1d54e38449c7bc5d30e171d01179">#</a></div> <div class="comment-content"> While I accept most of what you say, you are overlooking one key point: Better typists are better typists. Speed of typing (and volume of code produced) is the only statistic you look at. But what about <em>quality</em>? <br> Watch a bad typist code. (It's easier with coding-heavy tutorial videos). It usually go as: type four or five characters, backspace, make a correction, type another six characters, backspace, make another fix, type a few more character, etc. <br> They rarely get more than 10 keystrokes out before have to stop and go back. This reeks havoc with your cognitive flow, and makes it much harder to get into the "groove" of coding. Once you can type fluidly, you can concetrate on the code itself, rather than the mechanics of entering it. </div> <div class="comment-date">2018-09-19 02:02 UTC</div> </div> <div class="comment" id="3074a90b8bdb483dbc47d2364f056254"> <div class="comment-author"><a href="https://davideguida.com">Davide Guida</a> <a href="#3074a90b8bdb483dbc47d2364f056254">#</a></div> <div class="comment-content"> Lines of code has nothing to do with productivity. As professionals I think we all know this. <br> Moreover, in many cases, too much verbosity might only lead to more defects (unless you're a strong advocate of TDD et similia)<br> That said, one thing that I really liked about your article is the version of the Builder pattern you've implemented in the Reservation class. I love immutability in my classes and I often use the Builder pattern implemented as a inner class, just to be able to access the private cTor. <br> One thing that I don't like with your approach is that in case you have a bit parameter list in your cTor (as you do in the example), the consumers are forced to create a "dummy" instance and then call the WithXXX() methods on it when possible. I guess it could be solved adding a static readonly Empty property on the Reservation class that can be used as a starting point. <br> Anyways thank you for sharing this! </div> <div class="comment-date">2018-09-19 11:02 UTC</div> </div> <div class="comment" id="a4f8063cfaf84a10badd28981fac1ac5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a4f8063cfaf84a10badd28981fac1ac5">#</a></div> <div class="comment-content"> <p> James, thank you for writing. I agree that being able to type out your thoughts as fast as possible lowers the barrier between your brain and whatever medium you're working in. I had something similar in mind when I wrote <blockquote> "I also tend to write code in bursts. The average count of lines per hour may not be great, but that's because averages smooth out the hills and the valleys of my activity." </blockquote> I do, however, have mounting concerns about what you call the <em>groove</em> of coding. Flow state is generally characterised by a lack of reflection that I have often found to be counter-productive. These days, when programming, I deliberately use the Pomodoro technique - not to motivate me to code, but to force me to take breaks. Uncountable are the times I've had an important realisation about my work as soon as I'm away from the keyboard. These insights never seem to come to me when I'm in flow. </p> </div> <div class="comment-date">2018-09-21 4:53 UTC</div> </div> <div class="comment" id="8647083ea6c748b6bd2363790a9a69db"> <div class="comment-author">Radek Kapka <a href="#8647083ea6c748b6bd2363790a9a69db">#</a></div> <div class="comment-content"> <blockquote> <p>Perhaps you&#39;re now convinced that typing speed may not be the bottleneck, but you still feel that you don&#39;t like the verbose <code>Reservation</code> alternative. More code could be an increased maintenance burden. Consider those <code>With[...]</code> methods, such as <code>WithName</code>, <code>WithQuantity</code>, and so on. Once you make objects immutable, such copy-and-update methods become indispensable.</p> </blockquote> <p>You could create a more generic <code>With</code> method that would enable modifying any property. Here is an alternative design of the <code>Reservation</code> class including such a method. It is untested, so it might not work properly in every case.</p> <pre><code>public sealed class Reservation { public Reservation( DateTimeOffset date, string name, string email, int quantity) : this(date, name, email, quantity, false) { } private Reservation( DateTimeOffset date, string name, string email, int quantity, bool isAccepted) { Date = date; Name = name; Email = email; Quantity = quantity; IsAccepted = isAccepted; } private Reservation() { } public DateTimeOffset Date { get; private set; } public string Name { get; private set; } public string Email { get; private set; } public int Quantity { get; private set; } public bool IsAccepted { get; private set; } public Reservation With(string propertyName, object value) { if (propertyName == null) { throw new ArgumentNullException(nameof(propertyName)); } var property = typeof(Reservation).GetProperty(propertyName); if (property == null) { throw new ArgumentException($"Property ${propertyName} does not exist."); } if (!IsAssignable(property, value)) { throw new ArgumentException($"Value ${value} is not assignable to property ${propertyName}"); } var result = new Reservation(); foreach (var prop in typeof(Reservation).GetProperties()) { prop.SetValue(result, prop.Name == propertyName ? value : prop.GetValue(this)); } return result; } public Reservation Accept() { return new Reservation(Date, Name, Email, Quantity, true); } public override bool Equals(object obj) { if (!(obj is Reservation other)) return false; return Equals(Date, other.Date) &amp;&amp; Equals(Name, other.Name) &amp;&amp; Equals(Email, other.Email) &amp;&amp; Equals(Quantity, other.Quantity) &amp;&amp; Equals(IsAccepted, other.IsAccepted); } public override int GetHashCode() { return Date.GetHashCode() ^ Name.GetHashCode() ^ Email.GetHashCode() ^ Quantity.GetHashCode() ^ IsAccepted.GetHashCode(); } private static bool IsAssignable(PropertyInfo property, object value) { if (value == null &amp;&amp; property.PropertyType.IsValueType &amp;&amp; Nullable.GetUnderlyingType(property.PropertyType) == null) { return false; } return value == null || property.PropertyType.IsAssignableFrom(value.GetType()); } } </code></pre><p>Of course this design is not type-safe and we are throwing exceptions whenever the types of property and value don&#39;t match. Property names can be passed using <code>nameof</code> which will provide compile-time feedback. I believe it would be possible to write a code analyzer that would help calling the <code>With</code> method by raising compilation errors whenever the types don&#39;t match. The <code>With</code> method could be even designed as an extension method on <code>object</code> and distributed in a Nuget package with the anayler alongside it.</p> </div> <div class="comment-date">2018-09-27 07:34 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Lazy functor https://blog.ploeh.dk/2018/09/10/the-lazy-functor 2018-09-10T05:38:00+00:00 Mark Seemann <div id="post"> <p> <em>Lazy evaluation forms a functor. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. Previous articles have covered <a href="/2018/03/26/the-maybe-functor">Maybe</a>, <a href="/2018/08/06/a-tree-functor">rose trees</a>, and other functors. This article provides another example. </p> <p> Most programming languages are eagerly evaluated. Sometimes, however, you'd like to defer execution of an expensive operation. On .NET, you can use <a href="https://docs.microsoft.com/en-us/dotnet/api/system.lazy-1">Lazy&lt;T&gt;</a> to achieve that goal. This generic type, like so many others, forms a functor. </p> <h3 id="0bf1fee5cdf14792a9d235630dd6fc4c"> Functor <a href="#0bf1fee5cdf14792a9d235630dd6fc4c" title="permalink">#</a> </h3> <p> You can implement an idiomatic <code>Select</code> extension method for <code>Lazy&lt;T&gt;</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(()&nbsp;=&gt;&nbsp;selector(source.Value)); }</pre> </p> <p> This would, for example, enable you to write code like this: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y&nbsp;=&nbsp;x.Select(i&nbsp;=&gt;&nbsp;i.ToString());</pre> </p> <p> Or, using query syntax: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;x&nbsp;<span style="color:blue;">select</span>&nbsp;i.ToString();</pre> </p> <p> You can add more steps to such a pipeline of lazy computation until you finally decide to force evaluation with the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.lazy-1.value">Value</a> property. </p> <p> Notice that while the <code>Select</code> method looks like it might force evaluation as well, by accessing the <code>Value</code> property, this happens inside a new lazily evaluated lambda expression. Thus, all steps in a <code>Lazy</code> pipeline are deferred until evaluation is forced. </p> <h3 id="35bdade6fc5d483da65d7d9623f23aa6"> First functor law <a href="#35bdade6fc5d483da65d7d9623f23aa6" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual in this article series, actually proving that this is the case belongs in the realm of computer science. Instead of proving that the law holds, you can demonstrate that it does using property-based testing. The following example shows a single property written with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> 2.11.0 and <a href="https://xunit.net/">xUnit.net</a> 2.4.0. </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;LazyObeysFirstFunctorLaw(<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;left&nbsp;=&nbsp;<span style="color:#2b91af;">Lazy</span>.FromValue(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;<span style="color:#2b91af;">Lazy</span>.FromValue(i).Select(x&nbsp;=&gt;&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(left.Value,&nbsp;right.Value); }</pre> </p> <p> Even though you may feel that a property-based test gives you more confidence than a few hard-coded examples, such a test is nothing but a demonstration of the first functor law. It's no proof, and it only demonstrates that the law holds for <code>Lazy&lt;int&gt;</code>, not that it holds for <code>Lazy&lt;string&gt;</code>, <code>Lazy&lt;Address&gt;</code>, etcetera. </p> <p> Both this property and the next uses this little static helper method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;FromValue&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T</span>&gt;(()&nbsp;=&gt;&nbsp;value); }</pre> </p> <p> This helper method is only a convenience. You can put in a delay if you want to pretend that actual lazy evaluation takes place. It'll make the tests run slower, but otherwise will not affect the outcome. </p> <h3 id="dcbd7ed68831441d85960a148336d82c"> Second functor law <a href="#dcbd7ed68831441d85960a148336d82c" title="permalink">#</a> </h3> <p> As is the case with the first functor law, you can also use a property to demonstrate that the second functor law holds: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;LazyObeysSecondFunctorLaw( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;f, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;g, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;left&nbsp;=&nbsp;<span style="color:#2b91af;">Lazy</span>.FromValue(i).Select(g).Select(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;<span style="color:#2b91af;">Lazy</span>.FromValue(i).Select(x&nbsp;=&gt;&nbsp;f(g(x))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(left.Value,&nbsp;right.Value); }</pre> </p> <p> Again the same admonitions apply: that property is no proof. It does show, however, that given two functions, <code>f</code> and <code>g</code>, it doesn't matter if you map a <code>Lazy</code> computation in one or two steps. The output is the same in both cases. </p> <h3 id="b7c5e06037a64c07822997dc1ede3921"> F# <a href="#b7c5e06037a64c07822997dc1ede3921" title="permalink">#</a> </h3> <p> <a href="https://fsharp.org">F#</a> has <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/lazy-computations">built-in language support for lazy evaluations</a>, but surprisingly doesn't supply a <code>map</code> function. You can, however, easily implement such a function yourself: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Lazy&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Lazy&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Lazy&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;(x&nbsp;:&nbsp;Lazy&lt;&#39;a&gt;)&nbsp;=&nbsp;<span style="color:blue;">lazy</span>&nbsp;f&nbsp;x.Value</pre> </p> <p> This enables you to write code like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;(x&nbsp;:&nbsp;Lazy&lt;int&gt;)&nbsp;=&nbsp;//&nbsp;... <span style="color:blue;">let</span>&nbsp;(y&nbsp;:&nbsp;Lazy&lt;string&gt;)&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;Lazy.map&nbsp;string</pre> </p> <p> The F# language feature is based on the same <code>Lazy&lt;T&gt;</code> class that you can use from C# (and Visual Basic .NET), so the behaviour is the same as described above. The functor laws hold for the <code>Lazy.map</code> function just like it holds for the above <code>Select</code> method. </p> <h3 id="545d7a227c154f2d89dd3b0abbc0d49d"> Haskell <a href="#545d7a227c154f2d89dd3b0abbc0d49d" title="permalink">#</a> </h3> <p> Unlinke C#, F#, and most other programming languages, <a href="https://www.haskell.org">Haskell</a> is lazily evaluated. All values, whether scalar or complex, are lazy by default. For that reason, there's no explicit <em>Lazy</em> type in Haskell. </p> <p> Haskell values are, in themselves, not functors, but you can put any value into the <a href="/2018/09/03/the-identity-functor">Identity functor</a>. Since all values are already lazy, you could view <code>Identity</code> as Haskell's <em>Lazy</em> functor. </p> <p> The equivalence is only partial, however. .NET <code>Lazy</code> objects are small state machines. Before you've forced evaluation, they carry around the expression to be evaluated. Once you've evaluated the value once, <code>Lazy</code> objects effectively replace the lazy expression with its result. Thus, the next time you access the <code>Value</code> property, you immediately receive the result. </p> <p> Haskell's lazily evaluated values are different. Since they're immutable, they don't 'change state' like that. On the other hand, sometimes the Haskell compiler can identify optimisations that it can apply to make lazily evaluated values more efficient. In other cases, you may have to apply more explicit memoisation. </p> <h3 id="6adbe0c495f14e1eaac983c80df10ac4"> Summary <a href="#6adbe0c495f14e1eaac983c80df10ac4" title="permalink">#</a> </h3> <p> In languages with eager evaluation (which is most of them), you can model deferred execution as a functor. This enables you to compose lazily evaluated expressions piecemeal. </p> <p> <strong>Next:</strong> <a href="/2018/09/24/asynchronous-functors">Asynchronous functors</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Identity functor https://blog.ploeh.dk/2018/09/03/the-identity-functor 2018-09-03T06:46:00+00:00 Mark Seemann <div id="post"> <p> <em>The Identity functor is a data container that doesn't do anything. It's a functor nonetheless. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. In previous articles, you've learned about useful functors such as <a href="/2018/03/26/the-maybe-functor">Maybe</a>. In this article, you'll learn about a (practically) useless functor called <em>Identity</em>. You can skip this article if you want. </p> <p> If Identity is useless, then why bother? </p> <p> The inutility of Identity doesn't mean that it doesn't exist. The Identity functor exists, whether it's useful or not. You can ignore it, but it still exists. In C# or <a href="https://fsharp.org">F#</a> I've never had any use for it (although I've <a href="/2017/09/04/builder-as-identity">described it before</a>), while it turns out to be occasionally useful in <a href="https://www.haskell.org">Haskell</a>, where it's built-in. The value of Identity is language-dependent. </p> <p> You may still derive value from this article. My motivation for writing it is to add another example to the set of functor examples presented in this article series. By presenting a varied selection of functors, it's my hope that you, from examples, gain insight. </p> <h3 id="b0e1913a128445719058616dfdf0f66b"> Identity objects <a href="#b0e1913a128445719058616dfdf0f66b" title="permalink">#</a> </h3> <p> You can implement Identity as a C# class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Identity(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(selector(Item)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(Item,&nbsp;other.Item); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Item?.GetHashCode()&nbsp;??&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class is just a wrapper around any object of the generic type <code>T</code>. The value could even be <code>null</code> if <code>T</code> is a reference type. </p> <p> Since <code>Identity&lt;T&gt;</code> exposes the <code>Select</code> instance method, you can translate wrapped values, as this <em>C# Interactive</em> example demonstrates: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>).Select(s&nbsp;=&gt;&nbsp;s.Length) Identity&lt;int&gt; { Item=3 }</pre> </p> <p> You can also, if you prefer, use query syntax: </p> <p> <pre>&gt; <span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">int</span>&gt;(2)&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:#a31515;">&#39;f&#39;</span>&nbsp;+&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;o&#39;</span>,&nbsp;i) Identity&lt;string&gt; { Item="foo" }</pre> </p> <p> You can also unwrap the value: </p> <p> <pre>&gt; <span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>); &gt; x.Item "bar"</pre> </p> <p> Admittedly, this isn't the most exciting or useful class you could ever write. It is, however, a functor. </p> <h3 id="3cc3d00bdcbc4ad48008dac89fe748eb"> First functor law <a href="#3cc3d00bdcbc4ad48008dac89fe748eb" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual in this article series, actually proving that this is the case belongs in the realm of computer science. I would guess, however, that in this particular case, the proof would be manageable. Instead of proving this, though, you can demonstrate that the law holds using property-based testing. The following example shows a single property written with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> 2.11.0 and <a href="https://xunit.net/">xUnit.net</a> 2.4.0. </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;IdentityObeysFirstFunctorLaw(<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;left&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">int</span>&gt;(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">int</span>&gt;(i).Select(x&nbsp;=&gt;&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(left,&nbsp;right); }</pre> </p> <p> Even though you may feel that a property-based test gives you more confidence than a few hard-coded examples, such a test is nothing but a demonstration of the first functor law. It's no proof, and it only demonstrates that the law holds for <code>Identity&lt;int&gt;</code>, not that it holds for <code>Identity&lt;string&gt;</code>, <code>Identity&lt;Customer&gt;</code>, etcetera. </p> <h3 id="eaa5115fe3a9432d994ddc3580c7f06d"> Second functor law <a href="#eaa5115fe3a9432d994ddc3580c7f06d" title="permalink">#</a> </h3> <p> As is the case with the first functor law, you can also use a property to demonstrate that the second functor law holds: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;IdentityObeysSecondFunctorLaw( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;f, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;g, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;left&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">int</span>&gt;(i).Select(g).Select(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">int</span>&gt;(i).Select(x&nbsp;=&gt;&nbsp;f(g(x))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(left,&nbsp;right); }</pre> </p> <p> Again the same admonitions apply: that property is no proof. It does show, however, that given two functions, <code>f</code> and <code>g</code>, it doesn't matter if you map an <code>Identity</code> object in one or two steps. The output is the same in both cases. </p> <h3 id="adb0f7a1e73042e8b570545de440fb52"> F# <a href="#adb0f7a1e73042e8b570545de440fb52" title="permalink">#</a> </h3> <p> While the <code>Identity</code> functor is built into the Haskell standard library, there's no Identity functor in F#. While it can be occasionally useful in Haskell, Identity is useless in F#, like it is in C#. Again, that doesn't imply that you can't define it. You can: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Identity&lt;&#39;a&gt;&nbsp;=&nbsp;Identity&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a <span style="color:blue;">module</span>&nbsp;Identity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Identity&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;get&nbsp;(Identity&nbsp;x)&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Identity&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Identity&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;(Identity&nbsp;x)&nbsp;=&nbsp;Identity&nbsp;(f&nbsp;x)</pre> </p> <p> With this type, you can wrap, map, and unwrap values to your heart's content: </p> <p> <pre>&gt; Identity "foo" |&gt; Identity.map Seq.length;; val it : Identity&lt;int&gt; = Identity 3 &gt; Identity 2 |&gt; Identity.map (fun i -&gt; "f" + String ('o', i));; val it : Identity&gt;string&gt; = Identity "foo" &gt; let x = Identity "bar";; val x : Identity&lt;string&gt; = Identity "bar" &gt; Identity.get x;; val it : string = "bar"</pre> </p> <p> There's not much point to this, apart from demonstrating that it's possible. </p> <h3 id="3ce0bfe49361464ba4bf143aee3c70e2"> Summary <a href="#3ce0bfe49361464ba4bf143aee3c70e2" title="permalink">#</a> </h3> <p> The Identity functor exists, and you can implement it in C# and F#, although I don't see any use for it. Haskell has a type system that can express abstractions such as <code>Functor</code> in the type system itself. In that language, then, you can define functions that return any type of functor (e.g. <code>Maybe</code>, <code>Tree</code>, and so on). If you need a plain vanilla version of such a function, you can make it return <code>Identity</code>. </p> <p> The point of this article was only to identify the existence of the Identity functor, and to perhaps illustrate that the concept of a functor encompasses various data structures and behaviours. </p> <p> <strong>Next:</strong> <a href="/2018/09/10/the-lazy-functor">The Lazy functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. On Constructor Over-injection https://blog.ploeh.dk/2018/08/27/on-constructor-over-injection 2018-08-27T07:11:00+00:00 Mark Seemann <div id="post"> <p> <em>Constructor Over-injection is a code smell, not an anti-pattern.</em> </p> <p> Sometimes, people struggle with how to deal with the Constructor Over-injection code smell. Often, you can address it by <a href="/2010/02/02/RefactoringtoAggregateServices">refactoring to Facade Services</a>. This is possible when constructor arguments fall in two or more natural clusters. Sometimes, however, that isn't possible. </p> <h3 id="6ef3c9728f4f46e5a0a6c694b115705a"> Cross-cutting concerns <a href="#6ef3c9728f4f46e5a0a6c694b115705a" title="permalink">#</a> </h3> <p> Before we get to the heart of this post, I want to get something out of the way. Sometimes constructors have too many arguments because they request various services that represent cross-cutting concerns: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Foo( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBar</span>&nbsp;bar, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBaz</span>&nbsp;baz, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IQux</span>&nbsp;qux, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IAuthorizationManager</span>&nbsp;authorizationManager, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ILog</span>&nbsp;log, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ICache</span>&nbsp;cache, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ICircuitBreaker</span>&nbsp;breaker)</pre> </p> <p> This <code>Foo</code> class has seven dependencies passed via the constructor. Three of those (<code>bar</code>, <code>baz</code>, and <code>qux</code>) are regular dependencies. The other four are various incarnations of cross-cutting concerns: logging, caching, authorization, stability. As I describe in <a href="http://amzn.to/12p90MG">my book</a>, cross-cutting concerns are better addressed with <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a> or the <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">Chain of Responsibility</a> pattern. Thus, such a <code>Foo</code> constructor ought really to take just three arguments: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Foo( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBar</span>&nbsp;bar, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBaz</span>&nbsp;baz, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IQux</span>&nbsp;qux)</pre> </p> <p> Making cross-cutting concerns 'disappear' like that could decrease the number of constructor arguments to an acceptable level, thereby effectively dealing with the Constructor Over-injection smell. Sometimes, however, that's not the issue. </p> <h3 id="00cc1f6794244596ac665cbc9e0919da"> No natural clusters <a href="#00cc1f6794244596ac665cbc9e0919da" title="permalink">#</a> </h3> <p> Occasionally, a class has many dependencies, and the dependencies form no natural clusters: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Ploeh( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBar</span>&nbsp;bar, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBaz</span>&nbsp;baz, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IQux</span>&nbsp;qux, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IQuux</span>&nbsp;quux, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IQuuz</span>&nbsp;quuz, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ICorge</span>&nbsp;corge, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IGrault</span>&nbsp;grault, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IGarply</span>&nbsp;garply, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IWaldo</span>&nbsp;waldo, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IFred</span>&nbsp;fred, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IPlugh</span>&nbsp;plugh, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IXyzzy</span>&nbsp;xyzzy, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IThud</span>&nbsp;thud) { &nbsp;&nbsp;&nbsp;&nbsp;Bar&nbsp;=&nbsp;bar; &nbsp;&nbsp;&nbsp;&nbsp;Baz&nbsp;=&nbsp;baz; &nbsp;&nbsp;&nbsp;&nbsp;Qux&nbsp;=&nbsp;qux; &nbsp;&nbsp;&nbsp;&nbsp;Quux&nbsp;=&nbsp;quux; &nbsp;&nbsp;&nbsp;&nbsp;Quuz&nbsp;=&nbsp;quuz; &nbsp;&nbsp;&nbsp;&nbsp;Corge&nbsp;=&nbsp;corge; &nbsp;&nbsp;&nbsp;&nbsp;Grault&nbsp;=&nbsp;grault; &nbsp;&nbsp;&nbsp;&nbsp;Garply&nbsp;=&nbsp;garply; &nbsp;&nbsp;&nbsp;&nbsp;Waldo&nbsp;=&nbsp;waldo; &nbsp;&nbsp;&nbsp;&nbsp;Fred&nbsp;=&nbsp;fred; &nbsp;&nbsp;&nbsp;&nbsp;Plugh&nbsp;=&nbsp;plugh; &nbsp;&nbsp;&nbsp;&nbsp;Xyzzy&nbsp;=&nbsp;xyzzy; &nbsp;&nbsp;&nbsp;&nbsp;Thud&nbsp;=&nbsp;thud; }</pre> </p> <p> This seems to be an obvious case of the Constructor Over-injection code smell. If you can't refactor to Facade Services, then what other options do you have? </p> <h3 id="486abcae3b934d958f8c3f0575e58db5"> Introducing a Parameter Object isn't likely to help <a href="#486abcae3b934d958f8c3f0575e58db5" title="permalink">#</a> </h3> <p> Some people, when they run into this type of situation, attempt to resolve it by <a href="https://refactoring.com/catalog/introduceParameterObject.html">introducing a Parameter Object</a>. In its simplest form, the Parameter Object is just a collection of properties that client code can access. In other cases, the Parameter Object is a <a href="https://en.wikipedia.org/wiki/Facade_pattern">Facade</a> over a DI Container. Sometimes, the Parameter Object is defined as an interface with read-only properties. </p> <p> One way to use such a Parameter Object could be like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Ploeh(<span style="color:#2b91af;">DependencyCatalog</span>&nbsp;catalog) { &nbsp;&nbsp;&nbsp;&nbsp;Bar&nbsp;=&nbsp;catalog.Bar; &nbsp;&nbsp;&nbsp;&nbsp;Baz&nbsp;=&nbsp;catalog.Baz; &nbsp;&nbsp;&nbsp;&nbsp;Qux&nbsp;=&nbsp;catalog.Qux; &nbsp;&nbsp;&nbsp;&nbsp;Quux&nbsp;=&nbsp;catalog.Quux; &nbsp;&nbsp;&nbsp;&nbsp;Quuz&nbsp;=&nbsp;catalog.Quuz; &nbsp;&nbsp;&nbsp;&nbsp;Corge&nbsp;=&nbsp;catalog.Corge; &nbsp;&nbsp;&nbsp;&nbsp;Grault&nbsp;=&nbsp;catalog.Grault; &nbsp;&nbsp;&nbsp;&nbsp;Garply&nbsp;=&nbsp;catalog.Garply; &nbsp;&nbsp;&nbsp;&nbsp;Waldo&nbsp;=&nbsp;catalog.Waldo; &nbsp;&nbsp;&nbsp;&nbsp;Fred&nbsp;=&nbsp;catalog.Fred; &nbsp;&nbsp;&nbsp;&nbsp;Plugh&nbsp;=&nbsp;catalog.Plugh; &nbsp;&nbsp;&nbsp;&nbsp;Xyzzy&nbsp;=&nbsp;catalog.Xyzzy; &nbsp;&nbsp;&nbsp;&nbsp;Thud&nbsp;=&nbsp;catalog.Thud; }</pre> </p> <p> Alternatively, some people just store a reference to the Parameter Object and then access the read-only properties on an as-needed basis. </p> <p> None of those alternatives are likely to help. One problem is that such a <code>DependencyCatalog</code> will be likely to violate the <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a>, unless you take great care to make a 'dependency catalogue' per class. For instance, you'd be tempted to add <code>Wibble</code>, <code>Wobble</code>, and <code>Wubble</code> properties to the above <code>DependencyCatalog</code> class, because some other <code>Fnaah</code> class needs those dependencies in addition to <code>Bar</code>, <code>Fred</code>, and <code>Waldo</code>. </p> <h3 id="b6d3204fb4ad4e72a835b28f9aa2305b"> Deodorant <a href="#b6d3204fb4ad4e72a835b28f9aa2305b" title="permalink">#</a> </h3> <p> Fundamentally, Constructor Over-injection is a code smell. This means that it's an indication that something is not right; that there's an area of code that bears investigation. Code smells aren't problems in themselves, but rather symptoms. </p> <p> Constructor Over-injection tends to be a symptom that a class is doing too much: that it's violating the <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>. Reducing thirteen constructor arguments to a single Parameter Object doesn't address the underlying problem. It only applies deodorant to the smell. </p> <p> Address, if you can, the underlying problem, and the symptom is likely to disappear as well. </p> <h3 id="ac21c35680d34943a1827d94e2e18533"> Guidance, not law <a href="#ac21c35680d34943a1827d94e2e18533" title="permalink">#</a> </h3> <p> This is only guidance. There could be cases where it's genuinely valid to have dozens of dependencies. I'm being deliberately vague, because I don't wish to go into an elaborate example. Usually, there's more than one way to solve a particular problem, and occasionally, it's possible that collecting many services in a single class would be appropriate. (This sounds like a case for the <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a> design pattern, but let's assume that there could be cases where that's not possible.) </p> <p> This is similar to using <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> as a guide. Every now and then, a big, flat switch statement is just the best and most maintainable solution to a problem, even when it has a cyclomatic complexity of 37... </p> <p> Likewise, there could be cases where it's genuinely not a problem with dozens of constructor arguments. </p> <h3 id="7032a437458e45e58abd262b882b1442"> Summary <a href="#7032a437458e45e58abd262b882b1442" title="permalink">#</a> </h3> <p> Constructor Over-injection is a code smell, not an <a href="/2019/01/21/some-thoughts-on-anti-patterns">anti-pattern</a>. It's a good idea to be aware of the smell, and address it when you discover it. You should, however, deal with the problem instead of applying deodorant to the smell. The underlying problem is usually that the class with the many dependencies has too many responsibilities. Address that problem, and the smell is likely to evaporate as well. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f9c87aff3c034c138a69e74568b90ce8"> <div class="comment-author">Dominik Jeske <a href="#f9c87aff3c034c138a69e74568b90ce8">#</a></div> <div class="comment-content"> <p>Hi Mark, I have question about “Parameter Object” approach you mentioned. In my previous project we was using this approach heavily. I know disadvantages you described and honestly I was against this approach but after some time I saw some advantages I want to discuss. </p> <p>We have a lot of services that was reading data from some external databases, do some basic validation and fixing and than save to other database – so it was most integration code. Every of our services had bootstrapper attached by attribute that was describing dependencies. Each have only one parameter with so called “catalog” that was façade over DI with read only properties (sometimes even in tree structure to make it simpler to use like Catalog.Repositories.Server.SomeRepository). We have some base class for common dependencies like logging, common infrastructure services etc (we also use interceptor for cross cutting concerns but sometimes we have to log something explicitly). </p> <p>In unit tests we used same bootstrapper that service was using with repositories changed to mocks. Advantage that I’m seeing is that when using lot of unit tests and service like this when we are maintain the code when something is changing in service we don’t had to touch any unit test – it is similar to using parameter object in general even outside the scope of constructor – when using single complex object as input parameter there is also no problem when on 100 unit test break after we change add some method parameter. It is service locator antipattern but only in one place – other code is not using DI. </p> <p>Sometimes when I’m thinking about anti pattern approach in general I have a feeling similar to you when you was explaining other approach to async injection “you cannot do that” – “Oh, really”? Maybe pattern police will hunt me but is this really so bad? </p> </div> <div class="comment-date">2019-08-01 9:07 UTC</div> </div> <div class="comment" id="db9222ef894548369ad2f7ab8c10084c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#db9222ef894548369ad2f7ab8c10084c">#</a></div> <div class="comment-content"> <p> Dominik, thank you for writing. To be clear: there's no <em>pattern police</em>. Design patterns is (or was) an attempt to establish a vocabulary that can act as short-cuts for long, elaborate chains of arguments. The same goes for anti-patterns. </p> <p> Most experienced programmers over time discover that certain ways of doing things yield more benefits than disadvantages (patterns) or yield more disadvantages than benefits (anti-patterns). Instead of having to explain the possible consequences every time we run into such situations, we can reach for a generalised description and refer to it by names such as <em>Composite</em>, <em>Decorator</em>, <em>Service Locator</em>, etc. </p> <p> The same goes for more basic design principles such as the <a href="https://en.wikipedia.org/wiki/SOLID">SOLID</a> principles. If you find yourself writing code such as <code>Catalog.Repositories.Server.SomeRepository</code>, you're already in territory where the <a href="https://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a> (or <a href="https://twitter.com/martinfowler/status/1649793241">the Occasionally Useful Suggestion of Demeter</a>) applies. The Law of Demeter is less than a law and more like a code smell. If you find yourself 'dotting into' a deep object graph like that, you should stop and consider how that's going to impact long-term maintainability. </p> <p> Either an object doesn't need everything that a service catalogue exposes, in which case it'd be at odds with the <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a>, or it <em>does</em> need all of those services, in which case the class in question most likely violates the <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>. </p> <p> You can address the first of these concerns by injecting only the services that a class needs. If you're concerned about the impact on unit tests of changing dependencies, consider using an <a href="/2013/03/11/auto-mocking-container">Auto-mocking Container</a>. </p> <p> You can address the second of the above concerns by <a href="/2010/02/02/RefactoringtoAggregateServices">refactoring to Facade Services</a>. </p> </div> <div class="comment-date">2019-08-03 10:17 UTC</div> </div> <div class="comment" id="49bb0398acb24d9ba84290adaee64c78"> <div class="comment-author">Dominik Jeske <a href="#49bb0398acb24d9ba84290adaee64c78">#</a></div> <div class="comment-content"> <p>Mark, thanks for detailed answer (as always). </p> <p>In matter of catalog approach it was always smelly for me and honestly I was against using it but sometimes I had no good arguments to get rid of it. Automocking container is very promising for me – as matter of fact I saw this approach when analyzing one of your github samples and for start it looked like magic when xunit+autofixture+automoq was used and I have to admit it was love at first sight and I will absolutely deep dive into this topic. </p> <p>When talking about pattern police I was some kind of joking. I understand the source and meaning of the patterns and it is very good that we have them but sometimes they are problematic. Many times some question / discussion on places like stuckoverflow is broken by some “police man” that is saying that “your approach is antipattern”. Even if this maybe is true it sometimes kills some new approaches because people are real believers and they believe in patterns as if they were some religion and they will be fight for it instead of discuss and assume that maybe it is not always true or maybe this case is not really antipattern. </p> <p>Other thing is that people forgot after some time what is the real purpose / source of the pattern – the just use it automatically. My favorite anecdote/joke illustrate this <ul> <li>Mom why you are cutting edges of the cake</li> <li>Becouse we should do this</li> <li>But why</li> <li>Becoue we have to</li> <li>But why</li> <li>Becouse… my mom did this</li> <li>Grandma why you are cutting edges of the cake</li> <li>Becouse… my mom did this</li> <li>Great grandma why you are cutting edges of the cake</li> <li>Becouse I have small stove</li> </ul> After your explanation I’m closer to see why this is antipattern but for understand thing like this for real it is no enough to say that this is antipattern but this ability is not given to all people 😊 </p> </div> <div class="comment-date">2019-08-05 20:50 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Reactive functor https://blog.ploeh.dk/2018/08/20/reactive-functor 2018-08-20T05:58:00+00:00 Mark Seemann <div id="post"> <p> <em>IObservable&lt;T&gt; is (also) a functor.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. While the previous articles showed, in great detail, how to turn various classes into functors, this article mostly serves as a place-holder. The purpose is only to point out that you don't have to create all functors yourself. Sometimes, they come as part of a reusable library. </p> <p> <a href="http://reactivex.io">Rx</a> is such a library, and <code>IObservable&lt;T&gt;</code> is a functor. (It's also a monad, but this is not a monad tutorial; it's a functor tutorial.) Reactive Extensions define a <code>Select</code> method for <code>IObservable&lt;T&gt;</code>, so if <code>source</code> is an <code>IObservable&lt;int&gt;</code>, you can translate it to <code>IObservable&lt;string&gt;</code> like this: </p> <p> <pre><span style="color:#2b91af;">IObservable</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;dest&nbsp;=&nbsp;source.Select(i&nbsp;=&gt;&nbsp;i.ToString()); </pre> </p> <p> Since the <code>Select</code> method is, indeed, called <code>Select</code> and has the signature </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IObservable</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TSource</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IObservable</span>&lt;<span style="color:#2b91af;">TSource</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">TSource</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) </pre> </p> <p> you can also use C#'s query syntax: </p> <p> <pre><span style="color:#2b91af;">IObservable</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;dest&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;source &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;i.ToString();</pre> </p> <p> In both of the above examples, I've explicitly declared the type of <code>dest</code> instead of using the <code>var</code> keyword. There's no practical reason to do this; I only did it to make the type clear to you. </p> <h3 id="3870ba1938904931acd9d36ce3a8ac53"> First functor law <a href="#3870ba1938904931acd9d36ce3a8ac53" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual, it's proper computer-science work to actually prove that, but you can write some tests to demonstrate the first functor law for the <code>IObservable&lt;T&gt;</code> interface. In this article, you'll see a few parametrised tests written with <a href="https://xunit.net">xUnit.net</a>. Here's one that demonstrates that the first functor law holds: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(-101)] [<span style="color:#2b91af;">InlineData</span>(-1)] [<span style="color:#2b91af;">InlineData</span>(0)] [<span style="color:#2b91af;">InlineData</span>(1)] [<span style="color:#2b91af;">InlineData</span>(42)] [<span style="color:#2b91af;">InlineData</span>(1337)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;ObservableObeysFirstFunctorLaw(<span style="color:blue;">int</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;value&nbsp;}.ToObservable(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IObservable</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;projected&nbsp;=&nbsp;sut.Select(x&nbsp;=&gt;&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;projected.FirstAsync(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(value,&nbsp;actual); }</pre> </p> <p> Here, I chose to implement the identity function as an anonymous lambda expression. In contrast, in a <a href="/2018/03/26/the-maybe-functor">previous article</a>, I explicitly declared a function variable and called it <code>id</code>. Those two ways to express the identity function are equivalent. </p> <p> As always, I'd like to emphasise that this test doesn't <em>prove</em> that <code>IObservable&lt;T&gt;</code> obeys the first functor law. It only demonstrates that the law holds for those six examples. </p> <h3 id="e38867cbb29e4fb8839a76091c7db532"> Second functor law <a href="#e38867cbb29e4fb8839a76091c7db532" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that <code>IObservable&lt;T&gt;</code> obeys the second functor law: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(-101)] [<span style="color:#2b91af;">InlineData</span>(-1)] [<span style="color:#2b91af;">InlineData</span>(0)] [<span style="color:#2b91af;">InlineData</span>(1)] [<span style="color:#2b91af;">InlineData</span>(42)] [<span style="color:#2b91af;">InlineData</span>(1337)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;ObservableObeysSecondFunctorLaw(<span style="color:blue;">int</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;i.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;f(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(s.Reverse().ToArray()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;value&nbsp;}.ToObservable(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IObservable</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;projected&nbsp;=&nbsp;sut.Select(i&nbsp;=&gt;&nbsp;f(g(i))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;projected.FirstAsync(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;sut.Select(g).Select(f).FirstAsync(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); }</pre> </p> <p> This test defines two local functions, <code>f</code> and <code>g</code>. Instead of explicitly declaring the functions as <code>Func</code> variables, this test uses a (relatively) new C# feature called <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions">local functions</a>. </p> <p> Again, while the test doesn't prove anything, it demonstrates that for the six test cases, it doesn't matter if you project the observable in one or two steps. </p> <h3 id="74dc9987d2ae40ca94c6cbd5112a3238"> Summary <a href="#74dc9987d2ae40ca94c6cbd5112a3238" title="permalink">#</a> </h3> <p> The point of this article is mostly that functors are commonplace. While you may discover them in your own code, they may also come in a reusable library. If you already know C# LINQ based off <code>IEnumerable&lt;T&gt;</code>, much of Rx will be easy for you to learn. After all, it's the same abstraction, and <em>familiar abstractions make code readable</em>. </p> <p> <strong>Next:</strong> <a href="/2018/09/03/the-identity-functor">The Identity functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A Visitor functor https://blog.ploeh.dk/2018/08/13/a-visitor-functor 2018-08-13T06:56:00+00:00 Mark Seemann <div id="post"> <p> <em>Some Visitors can be functors. Another functor example for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. In the <a href="/2018/08/06/a-tree-functor">previous article</a>, you saw how to implement a generalised tree as a functor. In this article, you'll see another functor example, which will also be an application of the <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor design pattern</a>. </p> <p> The Visitor design pattern is often described in such a way that it's based on mutation; the <code>Visit</code> and <code>Accept</code> methods in those descriptions typically return <code>void</code>. You can, however, also implement immutable variations. This blog already contains <a href="/2011/05/16/TennisKatawithimmutabletypesandacyclomaticcomplexityof1">an older example of this</a>. </p> <h3 id="e58c06335ed34b45a31024e88464c23f"> Visitor <a href="#e58c06335ed34b45a31024e88464c23f" title="permalink">#</a> </h3> <p> In this article, you'll see how to implement a full binary tree using the Visitor design pattern. You can make the tree generic, so that each node can contain values of any generic type. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Accept&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">IBinaryTreeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;visitor); }</pre> </p> <p> As promised, this interface implies an immutable variant where the <code>Accept</code> method doesn't mutate the input Visitor, but rather returns a new value. You can learn how you arrive at this particular generic method signature in my article <a href="/2018/06/25/visitor-as-a-sum-type">Visitor as a sum type</a>. </p> <p> A full binary tree is a tree where each node has either zero or two children. This means that a Visitor must have two methods, one for each case: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IBinaryTreeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Visit(<span style="color:#2b91af;">Node</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;node); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Visit(<span style="color:#2b91af;">Leaf</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;leaf); }</pre> </p> <p> The <code>IBinaryTreeVisitor&lt;T, TResult&gt;</code> interface introduces two new concrete classes: <code>Node&lt;T&gt;</code> and <code>Leaf&lt;T&gt;</code>. A leaf node is a node with zero children: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Leaf</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Leaf(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Accept&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">IBinaryTreeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;visitor) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(visitor&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(visitor)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;visitor.Visit(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Leaf</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(Item,&nbsp;other.Item); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Item.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While a leaf node has no children, it still contains an <code>Item</code> of the generic type <code>T</code>. A leaf node still counts as a binary tree, so it implements the <code>IBinaryTree&lt;T&gt;</code> interface. Complying with the Visitor design pattern, its <code>Accept</code> method is implemented using double dispatch. Thereby, any <code>visitor</code> knows that it's now visiting a concrete <code>Leaf&lt;T&gt;</code> object. </p> <p> Likewise, a node is a (sub)tree: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Node</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Left&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Right&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Node(<span style="color:#2b91af;">T</span>&nbsp;item,&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;left,&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;right) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(left&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(left)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(right&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(right)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Left&nbsp;=&nbsp;left; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Right&nbsp;=&nbsp;right; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Accept&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">IBinaryTreeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;visitor) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(visitor&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(visitor)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;visitor.Visit(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Node</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(Item,&nbsp;other.Item) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(Left,&nbsp;other.Left) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;Equals(Right,&nbsp;other.Right); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Item.GetHashCode()&nbsp;^&nbsp;Left.GetHashCode()&nbsp;^&nbsp;Right.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In addition to an <code>Item</code>, a <code>Node&lt;T&gt;</code> object also contains a <code>Left</code> and a <code>Right</code> sub-tree. Notice that the <code>Accept</code> method is literally identical to <code>Leaf&lt;T&gt;.Accept</code>. Its behaviour differs, though, because <code>this</code> has a different type. </p> <p> A couple of static helper methods makes it a bit easier to create binary tree objects: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">BinaryTree</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Leaf&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Leaf</span>&lt;<span style="color:#2b91af;">T</span>&gt;(item); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Create&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;item, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;left, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;right) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Node</span>&lt;<span style="color:#2b91af;">T</span>&gt;(item,&nbsp;left,&nbsp;right); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The main convenience of these two methods is that C# (limited) type inference enables you to create tree objects without explicitly typing out the generic type argument every time. You'll soon see an example of creating a binary tree of integers. </p> <h3 id="75e8ba18e47049a6809896e0189fe0d9"> Functor <a href="#75e8ba18e47049a6809896e0189fe0d9" title="permalink">#</a> </h3> <p> Since <code>IBinaryTree&lt;T&gt;</code> is a generic type, you should consider whether it's a functor. Given the overall topic of this article, you'd hardly be surprised that it is. </p> <p> In the previous two functor examples (<a href="/2018/03/26/the-maybe-functor">Maybe</a> and <a href="/2018/08/06/a-tree-functor">Tree</a>), the <code>Select</code> methods were instance methods. On the other hand, the .NET Base Class Library implements <code>IEnumerable&lt;T&gt;.Select</code> as an extension method. You can do the same with this binary tree Visitor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;tree, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(tree&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(tree)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(selector)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;visitor&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SelectBinaryTreeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;(selector); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;tree.Accept(visitor); }</pre> </p> <p> This <code>Select</code> method has the right signature for turning <code>IBinaryTree&lt;T&gt;</code> into a functor. It starts by creating a new instance of a private helper class called <code>SelectBinaryTreeVisitor&lt;T, TResult&gt;</code>. Notice that this class has two generic type arguments: the source type <code>T</code> and the destination type <code>TResult</code>. It also contains <code>selector</code>, so that it knows what to do with each <code>Item</code> it encounters. </p> <p> <code>SelectBinaryTreeVisitor&lt;T, TResult&gt;</code> is a Visitor, so you pass it to the <code>tree</code> object's <code>Accept</code> method. The <code>Accept</code> method returns a variable that you can directly return, because, as you'll see below, the return type of <code>SelectBinaryTreeVisitor&lt;T, TResult&gt;</code>'s <code>Visit</code> methods is <code>IBinaryTree&lt;TResult&gt;</code>. </p> <p> <code>SelectBinaryTreeVisitor&lt;T, TResult&gt;</code> is a <code>private</code> helper class, and is the most complex functor implementation you've seen so far. The Visitor design pattern solves a specific problem, but it was never the simplest of design patterns. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SelectBinaryTreeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IBinaryTreeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SelectBinaryTreeVisitor(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(selector)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.selector&nbsp;=&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Visit(<span style="color:#2b91af;">Leaf</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;leaf) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;mappedItem&nbsp;=&nbsp;selector(leaf.Item); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Leaf(mappedItem); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Visit(<span style="color:#2b91af;">Node</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;node) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;mappedItem&nbsp;=&nbsp;selector(node.Item); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;mappedLeft&nbsp;=&nbsp;node.Left.Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;mappedRight&nbsp;=&nbsp;node.Right.Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Create(mappedItem,&nbsp;mappedLeft,&nbsp;mappedRight); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since the class implements <code>IBinaryTreeVisitor&lt;T,&nbsp;IBinaryTree&lt;TResult&gt;&gt;</code>, it must implement the two <code>Visit</code> overloads. The overload for <code>Leaf&lt;T&gt;</code> is simple: use the <code>selector</code> to map the <code>Item</code>, and use the <code>Leaf</code> convenience method to return a new <code>Leaf&lt;TResult&gt;</code> containing the mapped item. Notice that while <code>SelectBinaryTreeVisitor&lt;T, TResult&gt;</code> looks like it has a generic 'return' type argument of <code>TResult</code>, it implements <code>IBinaryTreeVisitor&lt;T,&nbsp;IBinaryTree&lt;TResult&gt;&gt;</code>, which means that the return type of each <code>Visit</code> method must be <code>IBinaryTree&lt;TResult&gt;</code>, and that matches <code>Leaf&lt;TResult&gt;</code>. </p> <p> The overload for a <code>Node&lt;T&gt;</code> object looks twice as big, but it's still simple. Like the leaf overload, it uses <code>selector</code> to map the <code>Item</code>, but in addition to that, it must also recursively map the <code>Left</code> and <code>Right</code> sub-trees. It does this by passing itself, in its role as a Visitor, to the left and right nodes' <code>Accept</code> methods. This returns mapped sub-trees that can be used to create a new mapped tree, using the <code>Create</code> convenience method. </p> <h3 id="05044a8452844974980483246078f0e2"> Usage <a href="#05044a8452844974980483246078f0e2" title="permalink">#</a> </h3> <p> While the implementation of such a Visitor is cumbersome, it's easy enough to use. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;source&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(42, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(1337, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(0), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(-22)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(100));</pre> </p> <p> You can translate this binary tree of integers to a tree of strings using method call syntax: </p> <p> <pre><span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;dest&nbsp;=&nbsp;source.Select(i&nbsp;=&gt;&nbsp;i.ToString()); </pre> </p> <p> or by using query syntax: </p> <p> <pre><span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;dest&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;source &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;i.ToString();</pre> </p> <p> In both of these examples, I've explicitly declared the type of <code>dest</code> instead of using the <code>var</code> keyword. There's no practical reason to do this; I only did it to make the type clear to you. </p> <h3 id="6db319918e384c2e82c64aba8e24d73c"> Haskell <a href="#6db319918e384c2e82c64aba8e24d73c" title="permalink">#</a> </h3> <p> Why would anyone ever do something so complicated as this? </p> <p> The answer to such a question is, I believe, that it's only complicated in <em>some</em> programming languages. In <a href="https://haskell.org">Haskell</a>, <em>all</em> of the above can be reduce to <em>a single</em> type declaration: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">BinaryTree</span>&nbsp;a&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Node</span>&nbsp;a&nbsp;(<span style="color:#dd0000;">BinaryTree</span>&nbsp;a)&nbsp;(<span style="color:#dd0000;">BinaryTree</span>&nbsp;a)&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Leaf</span>&nbsp;a &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Functor</span>)</pre> </p> <p> Notice that the Haskell compiler can automatically derive an implementation of the <code>Functor</code> typeclass, although that does require the <code>DeriveFunctor</code> language extension. </p> <p> This may explain why binary trees aren't part of object-oriented programmers' normal tool box, whereas they are more commonplace in functional programming. </p> <p> While not strictly required, in order to keep the examples equivalent, you can define these two aliases: </p> <p> <pre><span style="color:#600277;">leaf</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">BinaryTree</span>&nbsp;a leaf&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Leaf</span> <span style="color:#600277;">create</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">BinaryTree</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">BinaryTree</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">BinaryTree</span>&nbsp;a create&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Node</span></pre> </p> <p> This enables you to create a binary tree like this: </p> <p> <pre><span style="color:#600277;">source</span>&nbsp;::&nbsp;<span style="color:blue;">BinaryTree</span>&nbsp;Int source&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;create&nbsp;<span style="color:#09885a;">42</span>&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create&nbsp;<span style="color:#09885a;">1337</span>&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf&nbsp;<span style="color:#09885a;">0</span>)&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf&nbsp;(<span style="color:#666666;">-</span><span style="color:#09885a;">22</span>)))&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf&nbsp;<span style="color:#09885a;">100</span>)</pre> </p> <p> As usual you can map the tree using the <code>fmap</code> function: </p> <p> <pre><span style="color:#600277;">dest</span>&nbsp;::&nbsp;<span style="color:blue;">BinaryTree</span>&nbsp;String dest&nbsp;<span style="color:#666666;">=</span>&nbsp;fmap&nbsp;show&nbsp;source</pre> </p> <p> or by using infix notation: </p> <p> <pre><span style="color:#600277;">dest</span>&nbsp;::&nbsp;<span style="color:blue;">BinaryTree</span>&nbsp;String dest&nbsp;<span style="color:#666666;">=</span>&nbsp;show&nbsp;<span style="color:#666666;">&lt;$&gt;</span>&nbsp;source</pre> </p> <p> The <code>&lt;$&gt;</code> operator is an alias for <code>fmap</code>. </p> <h3 id="a0698e17ebc5402a9ce2b9e93dcf581a"> F# <a href="#a0698e17ebc5402a9ce2b9e93dcf581a" title="permalink">#</a> </h3> <p> As usual, <a href="https://fsharp.org">F#</a> lies somewhere between the extremes of C# and Haskell, although it's closer to Haskell in simplicity. The type declaration is similar: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">BinaryTree</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Node</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(&#39;a&nbsp;*&nbsp;<span style="color:teal;">BinaryTree</span>&lt;&#39;a&gt;&nbsp;*&nbsp;<span style="color:teal;">BinaryTree</span>&lt;&#39;a&gt;) |&nbsp;<span style="color:navy;">Leaf</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> Unlike Haskell, however, F# doesn't have any built-in functor awareness, so you'll have to implement the <em>map</em> function yourself: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;BinaryTree&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;BinaryTree&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Node</span>&nbsp;(x,&nbsp;left,&nbsp;right)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Node</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;x,&nbsp;<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>&nbsp;left,&nbsp;<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>&nbsp;right) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Leaf</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Leaf</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;x)</pre> </p> <p> Notice that you have to use the <code>rec</code> keyword in order to make <code>map</code> recursive. Instead of having to create a new helper class, and all the byzantine interactions required by the Visitor design pattern, the implementation uses simple pattern matching to achieve the same goal. In the <code>Node</code> case, it uses <code>f</code> to translate <code>x</code>, and recursively calls itself on <code>left</code> and <code>right</code>. In the <code>Leaf</code> case, it simply returns a new <code>Leaf</code> value with <code>x</code> translated by <code>f</code>. </p> <p> Create helper functions to keep all three examples aligned: </p> <p> <pre><span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;BinaryTree&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">leaf</span>&nbsp;=&nbsp;<span style="color:navy;">Leaf</span> <span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;BinaryTree&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;BinaryTree&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;BinaryTree&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">create</span>&nbsp;x&nbsp;left&nbsp;right&nbsp;=&nbsp;<span style="color:navy;">Node</span>&nbsp;(x,&nbsp;left,&nbsp;right)</pre> </p> <p> You can now create a binary tree of integers: </p> <p> <pre><span style="color:green;">//&nbsp;BinaryTree&lt;int&gt;</span> <span style="color:blue;">let</span>&nbsp;source&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">BinaryTree</span>.<span style="color:navy;">create</span>&nbsp;42&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">BinaryTree</span>.<span style="color:navy;">create</span>&nbsp;1337&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">BinaryTree</span>.<span style="color:navy;">leaf</span>&nbsp;0)&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">BinaryTree</span>.<span style="color:navy;">leaf</span>&nbsp;-22))&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">BinaryTree</span>.<span style="color:navy;">leaf</span>&nbsp;100)</pre> </p> <p> which you can translate like this: </p> <p> <pre><span style="color:green;">//&nbsp;BinaryTree&lt;string&gt;</span> <span style="color:blue;">let</span>&nbsp;dest&nbsp;=&nbsp;source&nbsp;|&gt;&nbsp;<span style="color:teal;">BinaryTree</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">string</span></pre> </p> <p> Here, all of the above functions are defined in a module named <code>BinaryTree</code>. </p> <h3 id="cd79db20d2e24587be91ade6ae956b0e"> First functor law <a href="#cd79db20d2e24587be91ade6ae956b0e" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual, it's proper computer-science work to actually prove that, but you can write some tests to demonstrate the first functor law for the <code>IBinaryTree&lt;T&gt;</code> interface. In this article, you'll see a few parametrised tests written with <a href="https://xunit.net">xUnit.net</a>. First, you can define some reusable trees as test input: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">object</span>[]&gt;&nbsp;Trees { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(0)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(-3, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(2), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(99))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(42, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(1337, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(0), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(-22)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(100))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(-927, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(2), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(211, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(88), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(132)))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(111, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(-336, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(113), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(-432)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Create(1299, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(-32), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BinaryTree</span>.Leaf(773)))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is just a collection of five small binary trees that can be used as input for parametrised tests. The first tree is only a single node - the simplest tree you can make with the <code>IBinaryTree&lt;T&gt;</code> API. </p> <p> You can use this static property as a source of input for parametrised tests. Here's one that demonstrates that the first functor law holds: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(Trees))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;FirstFunctorLaw(<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(tree,&nbsp;tree.Select(x&nbsp;=&gt;&nbsp;x)); }</pre> </p> <p> Here, I chose to implement the identity function as an anonymous lambda expression. In contrast, in a <a href="/2018/03/26/the-maybe-functor">previous article</a>, I explicitly declared a function variable and called it <code>id</code>. Those two ways to express the identity function are equivalent. </p> <p> As always, I'd like to emphasise that this test doesn't <em>prove</em> that <code>IBinaryTree&lt;T&gt;</code> obeys the first functor law. It only demonstrates that the law holds for those five examples. </p> <h3 id="f68059de38e24d0b9745b7e8071354d1"> Second functor law <a href="#f68059de38e24d0b9745b7e8071354d1" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that <code>IBinaryTree&lt;T&gt;</code> obeys the second functor law. You can reuse the <code>Trees</code> test case source for that test: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(Trees))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLaw(<span style="color:#2b91af;">IBinaryTree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;i.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;s.Length&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(tree.Select(g).Select(f),&nbsp;tree.Select(i&nbsp;=&gt;&nbsp;f(g(i)))); }</pre> </p> <p> This test defines two local functions, <code>f</code> and <code>g</code>. Instead of explicitly declaring the functions as <code>Func</code> variables, this test uses a (relatively) new C# feature called <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions">local functions</a>. </p> <p> Again, while the test doesn't prove anything, it demonstrates that for the five test cases, it doesn't matter if you project the <code>tree</code> in one or two steps. </p> <h3 id="cb5749ec27474647b6d96679c5d1d0ce"> Summary <a href="#cb5749ec27474647b6d96679c5d1d0ce" title="permalink">#</a> </h3> <p> Statically typed functional languages like F# and Haskell enable you to define <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a>: types that encode a selection of mutually exclusive cases. Combined with pattern matching, it's easy to deal with values that can be one of several non-polymorphic cases. Object-oriented languages like C# or Java don't have good support for this type of data structure. Object-oriented programmers often resort to using type hierarchies, but this requires down-casting in order to work. It also comes with the disadvantage that with type hierarchies, the hierarchy is extensible, which means that as an implementer, you never know if you've handled all sub-types. <a href="/2018/06/25/visitor-as-a-sum-type">The Visitor design pattern is a way to model sum types in object-oriented programming</a>, although it tends to be verbose. </p> <p> Nevertheless, if you have a generic type that models a set of mutually exclusive cases, it just may be a functor. In Haskell, you can make such a type a <code>Functor</code> with a mere declaration. In C#, you have to write considerable amounts of code. </p> <p> <strong>Next:</strong> <a href="/2018/08/20/reactive-functor">Reactive functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A Tree functor https://blog.ploeh.dk/2018/08/06/a-tree-functor 2018-08-06T06:00:00+00:00 Mark Seemann <div id="post"> <p> <em>A generalised tree object as a functor. Another functor example for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. In a <a href="/2018/03/26/the-maybe-functor">previous article</a>, you saw how to implement the Maybe functor in C#. In this article, you'll get another functor example: a generalised tree (also known as a <em>rose tree</em>). </p> <p> As opposed to a binary tree, any node in a generalised tree can have an arbitrary number of children, including none. </p> <h3 id="19775137ce6b44499b1924c687b1c5e3"> Implementation <a href="#19775137ce6b44499b1924c687b1c5e3" title="permalink">#</a> </h3> <p> The following <code>Tree&lt;T&gt;</code> class can contain objects of the generic type <code>T</code>. Being generic, it's a candidate for a functor, and it does, in fact, turn out to be one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;children; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Tree(<span style="color:#2b91af;">T</span>&nbsp;item,&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;children) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(children&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(children)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.children&nbsp;=&nbsp;children; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;mappedItem&nbsp;=&nbsp;selector(Item); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;mappedChildren&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;t&nbsp;<span style="color:blue;">in</span>&nbsp;children) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mappedChildren.Add(t.Select(selector)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(mappedItem,&nbsp;mappedChildren); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Count &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;children.Count;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerator</span>&lt;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;GetEnumerator() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;children.GetEnumerator(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerator</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>.GetEnumerator() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;children.GetEnumerator(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(Item,&nbsp;other.Item) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:#2b91af;">Enumerable</span>.SequenceEqual(<span style="color:blue;">this</span>,&nbsp;other); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Item.GetHashCode()&nbsp;^&nbsp;children.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As is usually the case, you can implement a tree in more than one way. Here, I chose an approach where <code>Tree&lt;T&gt;</code> contains an <code>Item</code> and <em>is</em> a collection of (sub)trees. Notice that the definition is recursive: in addition to its <code>Item</code>, each <code>Tree&lt;T&gt;</code> object also contains a finite collection of other trees. You create leaf nodes with empty collections. </p> <p> This variation uses finite collections (<code>IReadOnlyCollection&lt;T&gt;</code>), but you could also enable infinite trees by instead using potentially infinite sequences (<code>IEnumerable&lt;T&gt;</code>). This would slightly complicate the implementation of the <code>Select</code> method, though, so I decided to leave that as an exercise to the reader. </p> <p> The <code>Select</code> method first translates the contained <code>Item</code> using the <code>selector</code> function, and then recursively calls itself for each sub-tree in <code>children</code>. This method has the desired signature, and furthermore obeys the functor laws, as you'll see later in the article. <code>Tree&lt;T&gt;</code> is a functor. </p> <h3 id="0bb07dad62be418ca8fa0e0d187c00bc"> Usage <a href="#0bb07dad62be418ca8fa0e0d187c00bc" title="permalink">#</a> </h3> <p> While you can create trees directly with the <code>Tree&lt;T&gt;</code> constructor, a few static helper methods makes it smoother: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Tree</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Leaf&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;(item,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;[0]); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Create&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;item,&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;[]&nbsp;children) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;(item,&nbsp;children); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This enables you to create a tree like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;source&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(42, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(1337, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(-3)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(7, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(-99), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(100), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(0)));</pre> </p> <p> This is a tree containing integers. You can translate it to a tree containing strings like this: </p> <p> <pre><span style="color:#2b91af;">Tree</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;dest&nbsp;=&nbsp;source.Select(i&nbsp;=&gt;&nbsp;i.ToString()); </pre> </p> <p> or like this: </p> <p> <pre><span style="color:#2b91af;">Tree</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;dest&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;source &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;i.ToString();</pre> </p> <p> In both of these examples, I've explicitly declared the type of <code>dest</code> instead of using the <code>var</code> keyword. There's no practical reason to do this; I only did it to make the type clear to you. </p> <h3 id="1a02c785b313419b9731240c0ee205f8"> Haskell <a href="#1a02c785b313419b9731240c0ee205f8" title="permalink">#</a> </h3> <p> <a href="https://haskell.org">Haskell</a> has a more explicit model of functors, and the language features to support it. The Haskell equivalent to the above <code>Tree&lt;T&gt;</code> class is literally one line of code: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Tree</span>&nbsp;a&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Tree</span>&nbsp;a&nbsp;[<span style="color:#dd0000;">Tree</span>&nbsp;a]&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Functor</span>) </pre> </p> <p> Notice that the Haskell compiler can automatically derive an implementation of the <code>Functor</code> typeclass, although that does require the <code>DeriveFunctor</code> language extension. </p> <p> You can expend a few more lines of code for utility functions, so that it looks like you're actually programming: </p> <p> <pre><span style="color:#600277;">leaf</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;a leaf&nbsp;x&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Tree</span>&nbsp;x&nbsp;<span style="color:blue;">[]</span> <span style="color:#600277;">create</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Tree</span>&nbsp;a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;a create&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Tree</span></pre> </p> <p> These functions correspond to the static methods on the above <code>Tree</code> class. With them, you can now create a tree: </p> <p> <pre><span style="color:#600277;">source</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;Int source&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;create&nbsp;<span style="color:#09885a;">42</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;create&nbsp;<span style="color:#09885a;">1337</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf&nbsp;(<span style="color:#666666;">-</span><span style="color:#09885a;">3</span>)], &nbsp;&nbsp;&nbsp;&nbsp;create&nbsp;<span style="color:#09885a;">7</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf&nbsp;(<span style="color:#666666;">-</span><span style="color:#09885a;">99</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf&nbsp;<span style="color:#09885a;">100</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leaf&nbsp;<span style="color:#09885a;">0</span>]]</pre> </p> <p> You can translate such a tree of integers to a tree of strings with the <code>fmap</code> function: </p> <p> <pre><span style="color:#600277;">dest</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;String dest&nbsp;<span style="color:#666666;">=</span>&nbsp;fmap&nbsp;show&nbsp;source</pre> </p> <p> or with the infix operator: </p> <p> <pre><span style="color:#600277;">dest</span>&nbsp;::&nbsp;<span style="color:blue;">Tree</span>&nbsp;String dest&nbsp;<span style="color:#666666;">=</span>&nbsp;show&nbsp;<span style="color:#666666;">&lt;$&gt;</span>&nbsp;source</pre> </p> <p> The <code>&lt;$&gt;</code> operator is an alias for <code>fmap</code>. </p> <h3 id="32fc2e88a44f41a0a924c57b47d41aa6"> F# <a href="#32fc2e88a44f41a0a924c57b47d41aa6" title="permalink">#</a> </h3> <p> In <a href="https://fsharp.org">F#</a>, the type definition has the same structure as in Haskell: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Tree</span>&lt;&#39;a&gt;&nbsp;=&nbsp;<span style="color:navy;">Tree</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(&#39;a&nbsp;*&nbsp;<span style="color:teal;">Tree</span>&lt;&#39;a&gt;&nbsp;<span style="color:teal;">list</span>) </pre> </p> <p> Unlike Haskell, however, F# doesn't have any built-in functor awareness, so you'll have to implement the <em>map</em> function yourself: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Tree&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Tree&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>&nbsp;(<span style="color:navy;">Tree</span>&nbsp;(x,&nbsp;children))&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;mappedX&nbsp;=&nbsp;<span style="color:navy;">f</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;mappedChildren&nbsp;=&nbsp;children&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Tree</span>&nbsp;(mappedX,&nbsp;mappedChildren)</pre> </p> <p> Notice that you have to use the <code>rec</code> keyword in order to make <code>map</code> recursive. The implementation is similar to the C# code: first use <code>f</code> to map the contained item, and then recursively map the children. </p> <p> To keep all three examples equivalent, you can also define utility functions: </p> <p> <pre><span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;Tree&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">leaf</span>&nbsp;x&nbsp;=&nbsp;<span style="color:navy;">Tree</span>&nbsp;(x,&nbsp;<span style="color:teal;">List</span>.empty) <span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;Tree&lt;&#39;a&gt;&nbsp;list&nbsp;-&gt;&nbsp;Tree&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">create</span>&nbsp;x&nbsp;children&nbsp;=&nbsp;<span style="color:navy;">Tree</span>&nbsp;(x,&nbsp;children)</pre> </p> <p> This enables you to create a tree like this: </p> <p> <pre><span style="color:green;">//&nbsp;Tree&lt;int&gt;</span> <span style="color:blue;">let</span>&nbsp;source&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tree</span>.<span style="color:navy;">create</span>&nbsp;42&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tree</span>.<span style="color:navy;">create</span>&nbsp;1337&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tree</span>.<span style="color:navy;">leaf</span>&nbsp;-3] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tree</span>.<span style="color:navy;">create</span>&nbsp;7&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tree</span>.<span style="color:navy;">leaf</span>&nbsp;-99 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tree</span>.<span style="color:navy;">leaf</span>&nbsp;100 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tree</span>.<span style="color:navy;">leaf</span>&nbsp;0]]</pre> </p> <p> which you can translate like this: </p> <p> <pre><span style="color:green;">//&nbsp;Tree&lt;string&gt;</span> <span style="color:blue;">let</span>&nbsp;dest&nbsp;=&nbsp;source&nbsp;|&gt;&nbsp;<span style="color:teal;">Tree</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">string</span></pre> </p> <p> Here, all of the above functions are defined in a module named <code>Tree</code>. </p> <h3 id="583705522ab34d4da714fcf3aa18ca0f"> First functor law <a href="#583705522ab34d4da714fcf3aa18ca0f" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual, it's proper computer-science work to actually prove that, but you can write some tests to demonstrate the first functor law for the <code>Tree&lt;T&gt;</code> class. In this article, you'll see a few parametrised tests written with <a href="https://xunit.net">xUnit.net</a>. First, you can define some reusable trees as test input: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">object</span>[]&gt;&nbsp;Trees { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(42)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">Tree</span>.Create(-32,&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(0))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(99, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(90), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(2))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(99, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(90), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(-3)))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(42, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(1337, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(-3)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Create(7, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(-99), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(100), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tree</span>.Leaf(0)))&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is just a collection of five small trees that can be used as input for parametrised tests. The first tree is only a single node - the simplest tree you can make with the <code>Tree&lt;T&gt;</code> class. </p> <p> You can use this static property as a source of input for parametrised tests. Here's one that demonstrates that the first functor law holds: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(Trees))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;FirstFunctorLaw(<span style="color:#2b91af;">Tree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(tree,&nbsp;tree.Select(x&nbsp;=&gt;&nbsp;x)); }</pre> </p> <p> Here, I chose to implement the identity function as an anonymous lambda expression. In contrast, in the <a href="/2018/03/26/the-maybe-functor">previous article</a>, I explicitly declared a function variable and called it <code>id</code>. Those two ways to express the identity function are equivalent. </p> <p> As always, I'd like to emphasise that this test doesn't <em>prove</em> that <code>Tree&lt;T&gt;</code> obeys the first functor law. It only demonstrates that the law holds for those five examples. </p> <h3 id="6fdcdebc30e5453ea119652b50f2d8f3"> Second functor law <a href="#6fdcdebc30e5453ea119652b50f2d8f3" title="permalink">#</a> </h3> <p> Like the above example, you can also write a parametrised test that demonstrates that <code>Tree&lt;T&gt;</code> obeys the second functor law. You can reuse the <code>Trees</code> test case source for that test: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(Trees))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLaw(<span style="color:#2b91af;">Tree</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;tree) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;i.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;s.Length&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(tree.Select(g).Select(f),&nbsp;tree.Select(i&nbsp;=&gt;&nbsp;f(g(i)))); }</pre> </p> <p> This test defines two local functions, <code>f</code> and <code>g</code>. Instead of explicitly declaring the functions as <code>Func</code> variables, this test uses a (relatively) new C# feature called <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions">local functions</a>. </p> <p> Again, while the test doesn't prove anything, it demonstrates that for the five test cases, it doesn't matter if you project the <code>tree</code> in one or two steps. </p> <h3 id="2a3fb6d8a4c24b759ca234ba5cda64d7"> Summary <a href="#2a3fb6d8a4c24b759ca234ba5cda64d7" title="permalink">#</a> </h3> <p> This was the second example of a functor implemented in a statically typed object-oriented language, but contrasted with implementations in statically typed functional languages. The concept of a functor translates without much loss of fidelity, but you'll have to write more verbose code. A language like C# isn't optimised for functors or their like; Haskell and F# are. </p> <p> The purpose of this article series is to show enough examples of functors that you should start to see a pattern. Keep in mind, though, that a functor isn't a design pattern. It's a mathematical concept. </p> <p> <strong>Next:</strong> <a href="/2019/08/19/a-rose-tree-functor">A rose tree functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Flattening arrow code using a stack of monads https://blog.ploeh.dk/2018/07/30/flattening-arrow-code-using-a-stack-of-monads 2018-07-30T06:05:00+00:00 Mark Seemann <div id="post"> <p> <em>Flatten arrow code with a stack of monads. A horrible example in C#.</em> </p> <p> In the <a href="/2018/07/24/dependency-injection-revisited">previous article</a>, you saw how to refactor an injected dependency to a <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a> that implements a free monad. One remaining problem is that some of the code tends towards the <a href="http://wiki.c2.com/?ArrowAntiPattern">Arrow anti-pattern</a>. In this article, you'll see how elegantly you can deal with this in <a href="https://www.haskell.org">Haskell</a>, how it translates to slightly more verbose <a href="http://fsharp.org">F#</a> code, but how, even though it does translate all the way to C#, it stops being nice along the way. </p> <p> All code for this article is <a href="https://github.com/ploeh/dependency-injection-revisited">available on GitHub</a>. </p> <h3 id="75b2c37430424507a5dd5118796a13eb"> Arrow code <a href="#75b2c37430424507a5dd5118796a13eb" title="permalink">#</a> </h3> <p> This is the problematic code: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.IsReservationInFuture(reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(isInFuture&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!isInFuture) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:blue;">int</span>?&gt;(<span style="color:blue;">null</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(reservation.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(reservations&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:blue;">int</span>?&gt;(<span style="color:blue;">null</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Create(reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>?(x)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> Perhaps it doesn't look <em>that</em> bad, but I think that that's mostly a result of the original example being as simple as it is. After all, the original example code started out like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>?&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!ReservationsRepository.IsReservationInFuture(reservation)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;ReservationsRepository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(reservation.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ReservationsRepository.Create(reservation); }</pre> </p> <p> The <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of this method could be as low as <em>3</em>, so if this was real production code, there'd be no reason to refactor it. As with most of my articles, however, you have to think of this example problem as a stand-in for something more complicated. </p> <p> If you take a second look at the top version (which is actually the later version), I hope you'll agree that the change has harmed the code. In general, it's more noisy, and it shows a clear tendency towards the dreaded Arrow anti-pattern. Again, it may not look that bad here, but if you imagine that we're looking at a stand-in for a much worse problem, I hope you can see how this could quickly become unsustainable. </p> <p> Part of the problem is that while C# has <em>some</em> syntactic sugar for monads, you can't branch inside a query expression, so instead it seems as though you're stuck with such nested closures. </p> <h3 id="b7ed2b8a51d745d580a7ceaea376923a"> First F# attempt <a href="#b7ed2b8a51d745d580a7ceaea376923a" title="permalink">#</a> </h3> <p> F#, on the other hand, doesn't have that limitation. In F#, you <em>can</em> branch inside of <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a>, so would that address the problem? Unfortunately, that's not the whole story. Here's an attempt at writing equivalent code in F#, using a custom <code>reservations</code> computation expression: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;ReservationsProgram&lt;int&nbsp;option&gt;</span> <span style="color:blue;">let</span>&nbsp;tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;reservations&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;isInFuture&nbsp;=&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;not&nbsp;isInFuture &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">return</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;reservations&nbsp;=&nbsp;readReservations&nbsp;reservation.Date &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;List.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">return</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;reservationId&nbsp;=&nbsp;create&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Some&nbsp;reservationId&nbsp;}</pre> </p> <p> While this is, in my opinion, more readable than the C# code, it doesn't successfully address the Arrow anti-pattern. While it's perfectly possible to branch (that is: use <code>if</code>, <code>then</code>, and <code>else</code>) inside a computation expression, we run into another problem. In statement-based languages like C# and Java, you can use <a href="http://wiki.c2.com/?GuardClause">Guard Clauses</a> to return early, as the original, pretty C# example demonstrates. In expression-based languages like F# and Haskell, on the other hand, any <code>if</code> branch must have a corresponding <code>else</code> branch, and both branches must return a value of the same type. This restriction forces the above F# code into the same Arrow shape as the problematic C# code. </p> <p> Languages like F# and Haskell would be poor languages, though, if they didn't have ways to address problems like this one. </p> <h3 id="d1002454f4484ce4aff95a5bec705d71"> Flattening with MaybeT <a href="#d1002454f4484ce4aff95a5bec705d71" title="permalink">#</a> </h3> <p> While it already feels unpleasant to write F# code like the above, writing similar code in Haskell would be figuratively painful. In Haskell, however, you essentially just change the return type of your function, pull in some standard library functions, and before you know it, you have nice flat code, with nary an Arrow in sight: </p> <p> <pre><span style="color:#2b91af;">tryAccept</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeT</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;<span style="color:#2b91af;">Int</span> tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;guard&nbsp;=&lt;&lt;&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;reservations&nbsp;&lt;-&nbsp;readReservations&nbsp;$&nbsp;reservationDate&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;$&nbsp;reservationQuantity&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;guard&nbsp;$&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;create&nbsp;$&nbsp;reservation&nbsp;{&nbsp;reservationIsAccepted&nbsp;=&nbsp;True&nbsp;} </pre> </p> <p> One of the notable traits of Haskell is that, because of its high-level abstractions, changing the type of an expression can change its behaviour. In this case, I decided to add a <code>MaybeT</code> to the <code>ReservationsProgram Int</code> return type. This means that not only does the following code take place inside the <code>ReservationsProgram</code> free monad, it takes place inside a stack of monads. In this case, the stack consists of <code>Maybe</code> and <code>ReservationsProgram</code>. </p> <p> What this means is that you can use the built-in <code>guard</code> function to short-circuit the program if the guards fail. Yes, these are <em>literally</em> guard clauses! </p> <p> Not only does this address the Arrow anti-pattern, it completely flattens the code so that the happy path is emphasised. </p> <h3 id="8fc51002d11d43779d9b3887b9b08e3c"> Stacking monads in F# <a href="#8fc51002d11d43779d9b3887b9b08e3c" title="permalink">#</a> </h3> <p> While Haskell comes with built-in monad transformers that enable you to declaratively stack monads, you'll have to do it manually in F#. It's still possible, though. All it takes to stack <code>ReservationsProgram</code> and <code>option</code> is something like this: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;ReservationsProgram&lt;&#39;b&nbsp;option&gt;)&nbsp;-&gt;&nbsp;ReservationsProgram&lt;&#39;a&nbsp;option&gt;&nbsp;-&gt;</span> <span style="color:green;">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReservationsProgram&lt;&#39;b&nbsp;option&gt;</span> <span style="color:blue;">let</span>&nbsp;bind&nbsp;f&nbsp;x&nbsp;=&nbsp;reservations&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;x&#39;&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;x&#39;&#39;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return!</span>&nbsp;f&nbsp;x&#39;&#39; &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;None&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;None&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">type</span>&nbsp;ReservationsOptionBuilder&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Bind&nbsp;(x,&nbsp;f)&nbsp;=&nbsp;bind&nbsp;f&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Return&nbsp;x&nbsp;=&nbsp;Pure&nbsp;(Some&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.ReturnFrom&nbsp;x&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Zero&nbsp;()&nbsp;=&nbsp;Pure&nbsp;(Some&nbsp;()) <span style="color:blue;">let</span>&nbsp;reservationsOption&nbsp;=&nbsp;ReservationsOptionBuilder&nbsp;()</pre> </p> <p> This stack of monads specifically handles the combination where a <code>ReservationsProgram</code> contains an <code>option</code> value. It considers the continuation value <code>x'</code> produced by the previous step in a <code>ReservationsProgram</code>, and only continues with <code>f</code> if the value is a <code>Some</code> value. Just like <code>option</code> normally works, it short-circuits further processing if the value is a <code>None</code> value. </p> <p> While F# doesn't have a general-purpose <code>guard</code> function, you can easily write one for this particular stack of monads: </p> <p> <pre><span style="color:green;">//&nbsp;bool&nbsp;-&gt;&nbsp;ReservationsProgram&lt;unit&nbsp;option&gt;</span> <span style="color:blue;">let</span>&nbsp;guard&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Pure&nbsp;(Some&nbsp;()) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Pure&nbsp;None</pre> </p> <p> This function takes a Boolean value as input, and returns <code>Pure (Some ())</code> when the value is <code>true</code>, and <code>Pure None</code> otherwise. While this seems weird at first glance, this is essentially what Haskell's <code>guard</code> does in the above code listing. The point is that <code>Pure None</code> short-circuits further processing, while <code>Pure (Some ())</code> allows the program to continue, as per the above <code>bind</code> function. </p> <p> You can now write a flattened version of <code>tryAccept</code> </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;ReservationsProgram&lt;int&nbsp;option&gt;</span> <span style="color:blue;">let</span>&nbsp;tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;reservationsOption&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;ReservationsOption.bind&nbsp;guard&nbsp;&lt;|&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;reservations&nbsp;=&nbsp;readReservations&nbsp;reservation.Date &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;List.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;guard&nbsp;(reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;create&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;}</pre> </p> <p> Notice that the type of the function doesn't change. It still returns a <code>ReservationsProgram&lt;int&nbsp;option&gt;</code>, but the implementation is different. Instead of explicitly dealing with branching in a <code>reservations</code> computation expression, it implicitly deals with it in the composed <code>reservationsOption</code> computation expression. </p> <p> Using the specialised <code>guard</code> function doesn't look as pretty as in Haskell, but it gets the job done. </p> <h3 id="49439df4397f454db0e13e9ffabde989"> Maybe as a Visitor <a href="#49439df4397f454db0e13e9ffabde989" title="permalink">#</a> </h3> <p> Can you do the same in C#? Yes, sort of, but it'll be ugly. </p> <p> As a first step, you'll need a Maybe monad, as this isn't a built-in type in C#. While I'd <a href="/2018/03/26/the-maybe-functor">typically prefer a simpler implementation</a>, since we're already looking at <a href="/2018/05/22/church-encoding">Church-encoded sum types</a>, let's take <a href="/2018/06/04/church-encoded-maybe">the Church-encoded Maybe implementation</a>, and <a href="/2018/06/25/visitor-as-a-sum-type">refactor it to a Visitor</a>. The <code>IMaybe&lt;T&gt;</code> interface is simply this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Accept&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">IMaybeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;visitor); }</pre> </p> <p> The Visitor is defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMaybeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitNothing&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitJust(<span style="color:#2b91af;">T</span>&nbsp;just); }</pre> </p> <p> This is, hopefully, not terribly surprising. There's two cases: <em>just</em> and <em>nothing</em>, and only the <em>just</em> case has a value associated. While I'm not going to walk you through all the details, this version of <code>IMaybe&lt;T&gt;</code> is still a monad: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SelectManyMaybeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;(selector)); }</pre> </p> <p> If you want to see how <code>SelectManyMaybeVisitor&lt;T, TResult&gt;</code> is implemented, you can see it in the code repository, but otherwise, it's also a good exercise to see if you can puzzle it out yourself. </p> <h3 id="2db9614a8e674839b92ffe26aa4fd722"> Stacking Reservations and Maybe <a href="#2db9614a8e674839b92ffe26aa4fd722" title="permalink">#</a> </h3> <p> You already have the <code>IReservationsProgram&lt;T&gt;</code> and <code>IMaybe&lt;T&gt;</code> monads. Now you just need to stack them, just like the above F# code: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectMany(x&nbsp;=&gt;&nbsp;x.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SelectManyMaybeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;(selector))); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SelectManyMaybeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMaybeVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&gt;&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SelectManyMaybeVisitor(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&gt;&nbsp;selector) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.selector&nbsp;=&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;VisitNothing &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;());&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;VisitJust(<span style="color:#2b91af;">T</span>&nbsp;just) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.selector(just); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Just like in the F# code, you can write the code inside of the <code>IReservationsProgram&lt;T&gt;</code> monad. To do that, you call <code>SelectMany</code> on <code>source</code>. The <code>x</code> in that lambda expression is a <code>IMaybe&lt;T&gt;</code> value, so in order to be able to proceed, you'll have to call its <code>Accept</code> method and pass it a Visitor. </p> <p> The overall signature of the outer <code>SelectMany</code> method is fixed. This is, after all, the monadic <em>bind</em> function, so you know that the return type must be <code>IReservationsProgram&lt;IMaybe&lt;TResult&gt;&gt;</code>. Therefore, this must be the second type argument of the Visitor that you pass to <code>Accept</code>, so the Visitor must have the type <code>IMaybeVisitor&lt;T, IReservationsProgram&lt;IMaybe&lt;TResult&gt;&gt;&gt;</code>. From there, it's 'just' a matter of figuring out how to implement <code>VisitNothing</code> and <code>VisitJust</code>. </p> <p> In the <code>VisitNothing</code> case, you simply return a <code>new Nothing&lt;TResult&gt;()</code>, but wrapped in a <code>Pure</code> value, so that it becomes an <code>IReservationsProgram&lt;IMaybe&lt;TResult&gt;&gt;</code>, rather than just an <code>IMaybe&lt;TResult&gt;</code>. </p> <p> In the <code>VisitJust</code> case, you'll need the injected <code>selector</code>, which you can simply call with the input argument and return the result. </p> <p> In order to support query expressions, you'll also need this special <code>SelectMany</code> overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">U</span>&gt;&gt;&gt;&nbsp;k, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;s) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(x&nbsp;=&gt;&nbsp;k(x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(y&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(s(x,&nbsp;y))))); }</pre> </p> <p> This is merely a weird C# technical detail, so I'm not going to tire you with this. It's not interesting. </p> <h3 id="5835e8214476428281aecae397272859"> Guard <a href="#5835e8214476428281aecae397272859" title="permalink">#</a> </h3> <p> Like the above F# code, you can define a specialised <code>Guard</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&gt;&nbsp;Guard(<span style="color:blue;">bool</span>&nbsp;b) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(b) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;(<span style="color:#2b91af;">Unit</span>.Instance)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;()); }</pre> </p> <p> It does the same as its F# counterpart, only is it more verbose, and it required me to define a <em>unit</em> type, because C# doesn't have one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Unit</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Unit</span>&nbsp;Instance&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Unit</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Unit()&nbsp;{&nbsp;} }</pre> </p> <p> This is simply a <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a> that carries no data. It's like <code>void</code>, but can act as a generic return type, which is what we need here. </p> <h3 id="2ea841e8f8e44c0bb48129000568901d"> Do <a href="#2ea841e8f8e44c0bb48129000568901d" title="permalink">#</a> </h3> <p> Finally, in order to be able to set <code>IsAccepted</code> to <code>true</code> and make it look like a function, you can add a <code>Do</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&gt;&nbsp;Do(<span style="color:#2b91af;">Action</span>&nbsp;action) { &nbsp;&nbsp;&nbsp;&nbsp;action(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:#2b91af;">Unit</span>&gt;(<span style="color:#2b91af;">Unit</span>.Instance)); }</pre> </p> <p> This is a nasty piece of impure code, but it'll get the job done. It'd also be possible to refactor to a make the <code>Reservation</code> class immutable, but for this proof of concept code, that's not necessary. It'll be ugly regardless. </p> <p> The point of the method is to enable method chaining in the <a href="https://martinfowler.com/bliki/FluentInterface.html">Fluent style</a>, even while you're mutating state. In general, I'd like to warn against doing something like this, because the entire point of functional programming is to avoid mutating state. It does allow us, however, to reproduce the original behaviour in the top of the article, which also mutates the <code>reservation</code> argument. </p> <h3 id="ce35ac8aae814e00a130ef2370c2f379"> Method chaining <a href="#ce35ac8aae814e00a130ef2370c2f379" title="permalink">#</a> </h3> <p> You can now write <code>TryAccept</code> as an <code>IReservationsProgram&lt;IMaybe&lt;int&gt;&gt;</code> method, instead of <code>IReservationsProgram&lt;int?&gt;</code>. In other words, you replace the <code>int?</code> (<code>Nullable&lt;int&gt;</code>) with <code>IMaybe&lt;int&gt;</code>. This enables you write the entire program as a 'flat' composition, by chaining calls to <code>SelectMany</code> and <code>Select</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.IsReservationInFuture(reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(isInFuture&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.Guard(isInFuture)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany((<span style="color:#2b91af;">Unit</span>&nbsp;_)&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.ReadReservations(reservation.Date)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(reservations&nbsp;=&gt;&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(reservedSeats&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.Guard(reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;Capacity)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany((<span style="color:#2b91af;">Unit</span>&nbsp;_)&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.Do(()&nbsp;=&gt;&nbsp;{&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>;&nbsp;})) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany((<span style="color:#2b91af;">Unit</span>&nbsp;_)&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.Create(reservation)); }</pre> </p> <p> You start with <code>ReservationsProgram.IsReservationInFuture</code> and continue with <code>SelectMany</code> off of its return value. Inside <code>SelectMany</code>, you then call <code>ReservationsProgram.Guard</code> in order to short-circuit if <code>isInFuture</code> is <code>false</code>. In fact, that step can be reduced to <code>SelectMany(ReservationsProgram.Guard)</code>, using <em>method group</em> syntax. </p> <p> While <code>Guard</code> returns a program containing <code>Unit</code>, you can still continue with <code>SelectMany</code> to call <code>ReservationsProgram.ReadReservations</code>. </p> <p> I'm not going to walk you through the rest of this code, but it works. </p> <h3 id="0b61af0e585e411197e6fc373b9f5989"> Query syntax <a href="#0b61af0e585e411197e6fc373b9f5989" title="permalink">#</a> </h3> <p> If you can write an entire program by chaining <code>SelectMany</code> and <code>Select</code>, chances are you can write it using C# query syntax as well. This turns out to be the case: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">int</span>&gt;&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;isInFuture&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.IsReservationInFuture(reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;&nbsp;&nbsp;_&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.Guard(isInFuture) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;reservations&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.ReadReservations(reservation.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;&nbsp;__&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.Guard(reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;Capacity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;___&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.Do(()&nbsp;=&gt;&nbsp;{&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;id&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span>.Create(reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;id; }</pre> </p> <p> This is simply the 'sugared' version of the previous code. It's a little more succinct, but whether it's better is subjective at best. I think you'd be challenged to find anyone who'd consider this <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> C# code. </p> <p> It gets the job done, though. It actually works! </p> <p> To be clear, I'm not particularly impressed with the readability of this. I love the Haskell version, but the C# translation isn't my cup of tea. </p> <h3 id="2d58a676ddba402785078b22f8c43a5b"> Conclusion <a href="#2d58a676ddba402785078b22f8c43a5b" title="permalink">#</a> </h3> <p> When I go to meetups and conferences, I often get the chance to talk to people who have read my articles or seen my talks on functional programming. I often get the question whether it's possible to use some of the wonderful F# and Haskell concepts in C#. This article answers such questions. Yes, it's possible, but what's the point? </p> <p> Such code is brittle, because you're dancing on the edge of what C# can do. I had to accept some compromises just to get this proof-of-concept code to work. To add spite to injury, the code is not as readable as idiomatic C#, and it taps into concepts that most C# developers wouldn't be familiar with. </p> <p> I'd expect most C# programmers to consider a code base like this unreadable. In the amount of time it takes to understand and learn the underlying concepts of monads and their syntactic sugar, one can learn a proper functional programming language like F#. Don't try to make C# do something it wasn't designed to do; just use a functional programming language; you can learn that sooner than you'll be able to make sense of this Frankenstein's monster shown here. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency Injection revisited https://blog.ploeh.dk/2018/07/24/dependency-injection-revisited 2018-07-24T07:26:00+00:00 Mark Seemann <div id="post"> <p> <em>Replace Dependency Injection with a Visitor</em> </p> <p> In a previous article, you saw how you can <a href="/2018/06/25/visitor-as-a-sum-type">model any sum type as a Visitor</a>. Does that mean, then, that you can model a free monad as a <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a>? </p> <p> Yes, it does. </p> <p> In the <a href="/2017/08/07/f-free-monad-recipe">F# free monad recipe</a>, you saw how to refactor any injected, interface-based dependency to a free monad. Since a free monad is nothing but a recursive <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>, this means that you now have the tools at your disposal to refactor your injected dependencies to a Visitor. </p> <p> To be clear, this is an article exploring the boundaries of what's possible with a language like C#. It's not intended to be an endorsement of a particular way to organise code. A <a href="https://youtu.be/qBYVW4ghMi8">conference talk recording</a> that covers the same example also exists. </p> <h3 id="71b11c7e93f04304ade1093a4a68d437"> Dependency Injection example <a href="#71b11c7e93f04304ade1093a4a68d437" title="permalink">#</a> </h3> <p> I'll walk you through how to do this. We'll start with an example, which, as usual, is about developing an online restaurant reservations system. The code base you'll see here implements the business logic that decides whether or not to accept a reservation request. All code from this article is <a href="https://github.com/ploeh/dependency-injection-revisited">available on GitHub</a>. </p> <p> In order to make the example illustrative, but still manageable, we'll look at a single dependency, defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;This&nbsp;method&nbsp;doesn&#39;t&nbsp;belong&nbsp;on&nbsp;a&nbsp;Repository&nbsp;interface.&nbsp;It&nbsp;ought&nbsp;to&nbsp;be</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;on&nbsp;some&nbsp;sort&nbsp;of&nbsp;ITimeProvider&nbsp;interface,&nbsp;or&nbsp;an&nbsp;extension&nbsp;method</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;thereof,&nbsp;but&nbsp;in&nbsp;order&nbsp;to&nbsp;keep&nbsp;the&nbsp;example&nbsp;refactorings&nbsp;as&nbsp;simple&nbsp;as</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;possible,&nbsp;it&#39;ll&nbsp;go&nbsp;here&nbsp;for&nbsp;demo&nbsp;purposes.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;IsReservationInFuture(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;ReadReservations(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;Create(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> As the code comment explains, the <code>IsReservationInFuture</code> method doesn't belong on this <code>IReservationsRepository</code> interface. A dedicated 'time provider' dependency would be more appropriate, but as you'll see when you read on, refactoring just one interface to a free monad Visitor is already complicated. Doing it twice would just repeat the process while adding little additional clarity. </p> <p> Apart from <code>IsReservationInFuture</code>, the <code>IReservationsRepository</code> declares <code>ReadReservations</code> for querying the Repository for existing reservations, and <code>Create</code> for adding a new reservation to the Repository. Notice that the <code>Create</code> method violates the <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> principle. While <a href="/2014/08/11/cqs-versus-server-generated-ids">better alternatives exist</a>, I decided to design the present example like this because it better illustrates how data could flow through a system. </p> <p> The only consumer of the interface that we're going to consider is this <code>MaîtreD</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaîtreD(<span style="color:blue;">int</span>&nbsp;capacity,&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;reservationsRepository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReservationsRepository&nbsp;=&nbsp;reservationsRepository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>?&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!ReservationsRepository.IsReservationInFuture(reservation)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;ReservationsRepository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(reservation.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;ReservationsRepository.Create(reservation); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;ReservationsRepository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> This is straightforward example code. First, it queries whether the reservation is in the past, because it makes no sense accepting a reservation in the past. </p> <p> If it gets past that hurdle, the <code>TryAccept</code> method then queries the injected <code>ReservationsRepository</code> for the reservations already recorded on that date, and calculates the sum of the quantities. This produces <code>reservedSeats</code> - the number of already reserved seats. If the restaurant's <code>Capacity</code> (a <a href="/2012/07/02/PrimitiveDependencies">Primitive Dependency</a>) is less than the already reserved seats, plus the requested quantity, the method again rejects the reservation. This time, the reason is that there's insufficient remaining capacity to accept it. </p> <p> Finally, if the <code>reservation</code> makes it past all the guards, <code>IsAccepted</code> is set to <code>true</code>, and the reservation is added to the Repository. Recall that <code>Create</code> returns the ID of the new reservation (presumably a database ID), so that ID is returned from the method. In all other cases, the method returns <code>null</code>. The return type of <code>TryAccept</code> is <code>int?</code> (<code>Nullable&lt;int&gt;</code>), so the <code>int</code> value returned from <code>Create</code> is implicitly converted to <code>int?</code>; the compiler does that. </p> <h3 id="80b1ee4b685949308a619072d4f6f7ba"> Testability <a href="#80b1ee4b685949308a619072d4f6f7ba" title="permalink">#</a> </h3> <p> My original motivation for learning about Dependency Injection was that I did Test-Driven Development. Dependency Injection enables you to <a href="https://martinfowler.com/articles/nonDeterminism.html">make automated tests deterministic</a> by replacing common sources of non-determinism with <a href="https://martinfowler.com/bliki/TestDouble.html">Test Doubles</a>. This is, predictably, also the case here: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">BookingApiTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;TryAcceptReturnsReservationIdInHappyPathScenario( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IReservationsRepository</span>&gt;&nbsp;td, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;sut, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;excessCapacity, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;td.Setup(r&nbsp;=&gt;&nbsp;r.IsReservationInFuture(reservation)).Returns(<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;td &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.ReadReservations(reservation.Date)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(reservations); &nbsp;&nbsp;&nbsp;&nbsp;td.Setup(r&nbsp;=&gt;&nbsp;r.Create(reservation)).Returns(expected); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;sut&nbsp;=&nbsp;sut.WithCapacity( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;+&nbsp;excessCapacity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.TryAccept(reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(reservation.IsAccepted); }</pre> </p> <p> This tests exercises the happy path scenario where the reservation <em>is</em> in the future, and there <em>is</em> enough remaining capacity to accept the request. It uses <a href="https://xunit.net">xUnit.net</a> and <a href="https://github.com/moq/moq4">Moq</a> with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> gluing it all together. </p> <h3 id="42fb1b18c8ba4c928746a109537b2e38"> Database implementation <a href="#42fb1b18c8ba4c928746a109537b2e38" title="permalink">#</a> </h3> <p> It's nice to be able to test the business logic, but ultimately, you'll need your application to be able to store reservations in some sort of persistent storage like a relational database. That's easy, too. Just implement <code>IReservationsRepository</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SqlReservationsRepository</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SqlReservationsRepository(<span style="color:blue;">string</span>&nbsp;connectionString) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.ConnectionString&nbsp;=&nbsp;connectionString; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;ConnectionString&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsReservationInFuture(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now&nbsp;&lt;&nbsp;reservation.Date; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date.Date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date.Date.AddDays(1).AddTicks(-1)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;min, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;max) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(<span style="color:blue;">this</span>.ConnectionString)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(readByRangeSql,&nbsp;conn)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@MinDate&quot;</span>,&nbsp;min)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@MaxDate&quot;</span>,&nbsp;max)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;rdr&nbsp;=&nbsp;cmd.ExecuteReader()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;(rdr.Read()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;(<span style="color:#2b91af;">DateTimeOffset</span>)rdr[<span style="color:#a31515;">&quot;Date&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;(<span style="color:blue;">string</span>)rdr[<span style="color:#a31515;">&quot;Name&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;(<span style="color:blue;">string</span>)rdr[<span style="color:#a31515;">&quot;Email&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;(<span style="color:blue;">int</span>)rdr[<span style="color:#a31515;">&quot;Quantity&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;result; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;readByRangeSql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;[Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;[dbo].[Reservations] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;YEAR(@MinDate)&nbsp;&lt;=&nbsp;YEAR([Date]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;MONTH(@MinDate)&nbsp;&lt;=&nbsp;MONTH([Date]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;DAY(@MinDate)&nbsp;&lt;=&nbsp;DAY([Date]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;YEAR([Date])&nbsp;&lt;=&nbsp;YEAR(@MaxDate) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;MONTH([Date])&nbsp;&lt;=&nbsp;MONTH(@MaxDate) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;DAY([Date])&nbsp;&lt;=&nbsp;DAY(@MaxDate)&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Create(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(ConnectionString)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(createReservationSql,&nbsp;conn)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;reservation.Date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;reservation.Name)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Email&quot;</span>,&nbsp;reservation.Email)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Quantity&quot;</span>,&nbsp;reservation.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;createReservationSql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INSERT&nbsp;INTO&nbsp;[dbo].[Reservations]&nbsp;([Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(@Date,&nbsp;@Name,&nbsp;@Email,&nbsp;@Quantity)&quot;</span>; }</pre> </p> <p> As has been my position for years, I find it easier to write database implementations using .NET's original ADO.NET API than <a href="http://blogs.tedneward.com/post/the-vietnam-of-computer-science">fighting</a> with an <a href="https://en.wikipedia.org/wiki/Object-relational_mapping">ORM</a>. </p> <h3 id="adb81dfa7394423fb07ea0015c2a8c69"> Free monad in F# <a href="#adb81dfa7394423fb07ea0015c2a8c69" title="permalink">#</a> </h3> <p> In order to refactor <code>IReservationsRepository</code> to a free monad, it's illustrative to first see how it would look in <a href="http://fsharp.org">F#</a>. This step is by no means required, but it offers another perspective of the translation that you have to perform. According to the <a href="/2017/08/07/f-free-monad-recipe">F# free monad recipe</a>, you can represent the <code>IReservationsRepository</code> interface with a sum type that describes the instruction set of the API: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ReservationsInstruction&lt;&#39;a&gt;&nbsp;= |&nbsp;IsReservationInFuture&nbsp;<span style="color:blue;">of</span>&nbsp;(Reservation&nbsp;*&nbsp;(bool&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a)) |&nbsp;ReadReservations&nbsp;<span style="color:blue;">of</span>&nbsp;(DateTimeOffset&nbsp;*&nbsp;(Reservation&nbsp;list&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a)) |&nbsp;Create&nbsp;<span style="color:blue;">of</span>&nbsp;(Reservation&nbsp;*&nbsp;(int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a))</pre> </p> <p> Each case corresponds to a method from the original interface, and each contains a tuple. The first element of the tuple should contain the input to the method, so the first element of <code>IsReservationInFuture</code> is a <code>Reservation</code> value, the first element of <code>ReadReservations</code> is a <code>DateTimeOffset</code>, and the first element of <code>Create</code> is a <code>Reservation</code>, just like <code>IsReservationInFuture</code>. </p> <p> The second tuple element is a <em>continuation</em>. This is a function an interpreter must call once it's produced the value required to continue. This corresponds to the output value from the interface methods, so the input to the continuation associated with <code>IsReservationInFuture</code> is a Boolean value, and so on. </p> <p> The rest of the F# code example follows the recipe to the letter, so it's not important to list it here. You're welcome to look in the Git repository, if you'd like to see the details. </p> <h3 id="82ab72621421406f8c75a9fdb40effb9"> Church-encoded instruction set <a href="#82ab72621421406f8c75a9fdb40effb9" title="permalink">#</a> </h3> <p> From <a href="/2018/05/22/church-encoding">Church encodings</a> we know that we can represent a sum type as an interface with a single <code>Match</code> method. The instruction set is a <a href="/2018/03/22/functors">functor</a>, so it has a generic type argument: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt;</pre> </p> <p> The <code>Match</code> method must take an argument for each case in the sum type. In the above F# code, you can see that there's three cases, corresponding to the three methods in the original <code>IReservationsRepository</code> interface. </p> <p> Furthermore, we know from the previous articles on Church encodings that the <code>Match</code> method must be generic, and return a value of its generic type argument: </p> <p> <pre><span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(</pre> </p> <p> Third, each argument (i.e. each case from the sum type you're encoding) must have this form: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<em>something</em>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;<em>name</em>,</pre> </p> <p> where <em>something</em> is the type of the data associated with the case, and <em>name</em> is the name of the case. </p> <p> The names are easy: they're <code>isReservationInFuture</code>, <code>readReservations</code>, and <code>create</code>, but what are the types associated with each case? </p> <p> The F# free monad recipe gives the answer, which is why I chose to include the above F# code. For instance, the type associated with the <code>IsReservationInFuture</code> case is <code>Reservation * (bool -&gt; 'a)</code>. That's a tuple, where the first element is a <code>Reservation</code> 'object', and the second element is a function. </p> <p> Take it slow for the first case, then. A tuple where the first element is a <code>Reservation</code> has the type: </p> <p> <pre><span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<em>function-type</em>&gt;</pre> </p> <p> where <em>function-type</em> is the type of the continuation function. In F#, that type was <code>bool -&gt; 'a</code>, which means a function that takes a <code>bool</code> as input, and returns a value of the generic type <code>'a</code> as output. In our Church-encoded C# code, we call that type <code>T</code>, so you need a function from <code>bool</code> to <code>T</code>; that is: <code>Func&lt;bool, T&gt;</code>. If you plug that into the above tuple type, you get: </p> <p> <pre><span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;</pre> </p> <p> Again, you can plug that type into the <em>something</em> place-holder further up, to find the type of the input argument that corresponds to the <code>isReservationInFuture</code> case: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;isReservationInFuture</pre> </p> <p> Doing this for the two other cases finally reveals the entire <code>Match</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;isReservationInFuture, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;create); }</pre> </p> <p> That's a worthy candidate for the <em>Ugliest C# method signature of 2018</em> award, I admit, but it's just an intermediate step. This is how the sausage is made. </p> <h3 id="88b4babfff654ff2898165e2a1a635f7"> Implementations of the instruction set <a href="#88b4babfff654ff2898165e2a1a635f7" title="permalink">#</a> </h3> <p> The <code>IReservationsInstruction&lt;T&gt;</code> interface defines an API. You'll need classes that implement the interface in order to do something useful. As you've seen multiple times in the articles series about Church encodings, you must add an implementation per case. Starting from the top: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IsReservationInFuture</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;IsReservationInFuture(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.t&nbsp;=&nbsp;t; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;isReservationInFuture, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;create) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;isReservationInFuture(<span style="color:blue;">this</span>.t); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class simply <a href="https://en.wikipedia.org/wiki/Adapter_pattern">adapts</a> an 'object' of the type <code>Tuple&lt;Reservation, Func&lt;bool, T&gt;&gt;</code> to the <code>IReservationsInstruction&lt;T&gt;</code> interface. Importantly, it unconditionally calls the <code>Match</code> method's <code>isReservationInFuture</code> argument, while ignoring <code>readReservations</code> and <code>create</code>. This is consistent with the previous incarnations of Church-encodings you've seen. It's an automatable process. You implement the two other cases in the same way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReadReservations</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReadReservations(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.t&nbsp;=&nbsp;t; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;isReservationInFuture, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;create) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;readReservations(<span style="color:blue;">this</span>.t); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> and </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Create</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Create(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.t&nbsp;=&nbsp;t; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;isReservationInFuture, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;create) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;create(<span style="color:blue;">this</span>.t); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If you find the class names odd, then that's a fair criticism. I agree that <code>Create</code> isn't the most object-oriented class name. At this point, though, the design is hardly object-oriented, even though the code in use is C#. You can deal with the names later. </p> <h3 id="8e10a1cdd9e54115804e9ce81fa97460"> Functor <a href="#8e10a1cdd9e54115804e9ce81fa97460" title="permalink">#</a> </h3> <p> The <code>IReservationsInstruction&lt;T&gt;</code> interface is generic, and as expected, it's a functor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isReservationInFuture:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IsReservationInFuture</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;=&gt;&nbsp;selector(t.Item2(b)))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReadReservations</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&gt;&nbsp;selector(t.Item2(d)))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Create</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&gt;&nbsp;selector(t.Item2(r))))); }</pre> </p> <p> Yes, this is horrendous. The F# code is neater: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;mapI&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;IsReservationInFuture&nbsp;(x,&nbsp;next)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IsReservationInFuture&nbsp;(x,&nbsp;next&nbsp;&gt;&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ReadReservations&nbsp;(x,&nbsp;next)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ReadReservations&nbsp;(x,&nbsp;next&nbsp;&gt;&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Create&nbsp;(x,&nbsp;next)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Create&nbsp;(x,&nbsp;next&nbsp;&gt;&gt;&nbsp;f)</pre> </p> <p> Again, this is an intermediary step. Things will get better. </p> <h3 id="0dccb33718a14bf89ff82fddee7dcd6e"> Church-encoded free monad <a href="#0dccb33718a14bf89ff82fddee7dcd6e" title="permalink">#</a> </h3> <p> Since <code>IReservationsInstruction&lt;T&gt;</code> is a functor, you can package it as a free monad. This entails creating a wrapper 'program' type for it. This is another sum type, in F# written like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;ReservationsProgram&lt;&#39;a&gt;&nbsp;= |&nbsp;Free&nbsp;<span style="color:blue;">of</span>&nbsp;ReservationsInstruction&lt;ReservationsProgram&lt;&#39;a&gt;&gt; |&nbsp;Pure&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> The direct translation to Church-encoded C#, then, is to a <code>Match</code> method with two arguments: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;,<span style="color:#2b91af;">TResult</span>&gt;&nbsp;free, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;pure); }</pre> </p> <p> While the <code>free</code> case looks intimidating, you arrive at it through the same automatic process as already described. </p> <p> The <code>pure</code> case is implemented by this trivial class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Pure(<span style="color:#2b91af;">T</span>&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.x&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;free, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;pure) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;pure(<span style="color:blue;">this</span>.x); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>free</code> case is slightly, but not much, more complex: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Free</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Free(<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.i&nbsp;=&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;free, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;pure) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;free(<span style="color:blue;">this</span>.i); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Both of them, true to the plan we're following, call their respective method arguments with the objects that they adapt. </p> <h3 id="9749b5ad53364a97bc3d25e5ac2c3c40"> Monad <a href="#9749b5ad53364a97bc3d25e5ac2c3c40" title="permalink">#</a> </h3> <p> In C#, the typical monadic <em>bind</em> function is idiomatically called <code>SelectMany</code>, and for various reasons, you need two overloads: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free:&nbsp;i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Free</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(i.Select(p&nbsp;=&gt;&nbsp;p.SelectMany(selector))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pure:&nbsp;x&nbsp;=&gt;&nbsp;selector(x)); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;SelectMany&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">U</span>&gt;&gt;&nbsp;k, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;s) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(x&nbsp;=&gt;&nbsp;k(x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(y&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(s(x,&nbsp;y)))); }</pre> </p> <p> The bottom of the two overloads is required by C# if you want to be able to support various query syntax language constructs. The top overload utilises <code>Match</code> to dispatch between <code>pure</code> and <code>free</code>. Again, the <code>pure</code> case is easy, because you simply call <code>selector</code> with <code>x</code>, and return its result. </p> <p> The <code>free</code> case is more complicated. While <code>i.Select</code> is the <code>Select</code> method defined for <code>IReservationsInstruction&lt;T&gt;</code>, <code>p.SelectMany</code> is a recursive call to the method itself. </p> <p> That's a fly in the ointment, as C# doesn't handle recursion as well as F# or <a href="https://www.haskell.org">Haskell</a>. It'll work fine as long as you don't blow the stack by producing huge programs that have to be interpreted. </p> <h3 id="6f5a47967f9346fcba8d571826bd3f18"> Lifts <a href="#6f5a47967f9346fcba8d571826bd3f18" title="permalink">#</a> </h3> <p> Following the free monad recipe, you'll need to lift each of the instruction set cases to the 'program' type. The following are plain helper methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;IsReservationInFuture(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Free</span>&lt;<span style="color:blue;">bool</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IsReservationInFuture</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:blue;">bool</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:blue;">bool</span>&gt;&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:blue;">bool</span>&gt;(x)))); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Free</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReadReservations</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;(x)))); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Create(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Free</span>&lt;<span style="color:blue;">int</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Create</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:blue;">int</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:blue;">int</span>&gt;&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:blue;">int</span>&gt;(x)))); }</pre> </p> <p> Again, the (unfortunately required) type information makes this unreadable, but in fact, not much happens. In F#, the same lift functions are three one-liners: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;isReservationInFuture&nbsp;r&nbsp;=&nbsp;Free&nbsp;(IsReservationInFuture&nbsp;(r,&nbsp;Pure)) <span style="color:blue;">let</span>&nbsp;readReservations&nbsp;d&nbsp;=&nbsp;Free&nbsp;(ReadReservations&nbsp;(d,&nbsp;Pure)) <span style="color:blue;">let</span>&nbsp;create&nbsp;r&nbsp;=&nbsp;Free&nbsp;(Create&nbsp;(r,&nbsp;Pure))</pre> </p> <p> These helper methods serve the sole purpose of making it easier to write client code that produces <code>IReservationsProgram&lt;T&gt;</code> 'objects' (they're actually abstract syntax trees). </p> <h3 id="9bf8fc640105499b80ff9a8a469892fc"> MaîtreD <a href="#9bf8fc640105499b80ff9a8a469892fc" title="permalink">#</a> </h3> <p> You now have all the building blocks that enable you to refactor <code>MaîtreD.TryAccept</code> to return an <code>IReservationsProgram&lt;int?&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaîtreD(<span style="color:blue;">int</span>&nbsp;capacity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.IsReservationInFuture(reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(isInFuture&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!isInFuture) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:blue;">int</span>?&gt;(<span style="color:blue;">null</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(reservation.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(reservations&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Pure</span>&lt;<span style="color:blue;">int</span>?&gt;(<span style="color:blue;">null</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">ReservationsProgram</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Create(reservation) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>?(x)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> This is hardly as pretty as the original version that used Dependency Injection, but you should notice an interesting effect: by returning a free monad, you get rid of the injected dependency. While the code still depends on <code>Capacity</code>, that's just a read-only number. </p> <p> Through the so-called <em>query syntax</em>, C# offers some syntactic sugar for monadic composition, similar to F#'s <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a> or Haskell's <code>do</code> notation. Where its utility ends, though, is exactly where you need it. Unfortunately, it doesn't support branching (the use of <code>if</code> statements and such) inside of <code>from x in xs</code> expressions, so while we've supplied the requisite <code>SelectMany</code> methods for <code>IReservationsProgram&lt;T&gt;</code>, you can't use them. </p> <p> Instead, you have to resort to branching inside of each continuation, which, unfortunately, pulls you towards <a href="http://wiki.c2.com/?ArrowAntiPattern">arrow code</a>. </p> <p> The <code>TryAccept</code> method starts by calling the <code>IsReservationInFuture</code> helper method (defined above), which returns an <code>IReservationsProgram&lt;bool&gt;</code> object. You can't easily pull the <code>bool</code> value out of the container, but you can use <code>SelectMany</code> to define what happens next. </p> <p> If the reservations is in the future, an interpreter should call the continuation with <code>true</code>, and otherwise, with <code>false</code>. Thus, you can branch on that Boolean value for the next step. If the reservations turned out to be in the past, the return value ought to be <code>null</code>, but you have to package that in an <code>IReservationsProgram&lt;int?&gt;</code>. The correct way to do that is to wrap it in a <code>Pure</code> object. </p> <p> If the reservation, on the other hand, turned out to be in the future, the program can continue. It proceeds to call the <code>ReadReservations</code> helper method, and again uses <code>SelectMany</code> to define what happens next. An interpreter will supply <code>reservations</code> (presumably read from an actual database), and the code inside the continuation decides what to do next. In the case of insufficient remaining capacity, you return another <code>null</code> wrapped in <code>Pure</code>, but if you decide to accept the reservation, you can finally call the <code>Create</code> helper method and return its return value. Here, however, C#'s implicit conversion no longer works, so you have to explicitly use <code>Select</code> to turn the <code>int</code> into an <code>int?</code>. </p> <h3 id="049e04b81e2f4e3ba39d40f4275038a5"> Interpreters <a href="#049e04b81e2f4e3ba39d40f4275038a5" title="permalink">#</a> </h3> <p> You can still unit test the code by supplying a test-specific interpreter, but in the interest of keeping this article to essentials, I'll refer you to the code repository if you want to see how the tests look at this point. </p> <p> You're probably more interested in seeing how an interpreter actually interacts with a real database, like the above <code>SqlReservationsRepository</code> class did. The scenario is still the same, only the specifics have changed. Instead of implementing an interface, you'll now have to interpret an <code>IReservationsProgram&lt;int?&gt;</code>. How do you do that? </p> <p> You do it like you'd traverse any other Church-encoded sum type: use the <code>Match</code> method and supply a handler for each of the cases: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Interpret&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;program, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;connectionString) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;program.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pure:&nbsp;x&nbsp;=&gt;&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free:&nbsp;i&nbsp;=&gt;&nbsp;i.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isReservationInFuture:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item2(IsReservationInFuture(t.Item1)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Interpret(connectionString), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item2(ReadReservations(t.Item1,&nbsp;connectionString)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Interpret(connectionString), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item2(Create(t.Item1,&nbsp;connectionString)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Interpret(connectionString))); }</pre> </p> <p> Since a free monad is a nested, recursive sum type, you'll first have to supply handlers for the <code>pure</code> and <code>free</code> cases. The <code>pure</code> case is, as always, trivial. This is where you finally encounter a leaf node in the abstract syntax tree you're traversing. This is where you get to return the final value of the program. </p> <p> In the <code>free</code> case, on the other hand, you'll need to handle an <code>IReservationsInstruction&lt;IReservationsProgram&lt;T&gt;&gt;</code> value, which is another Church-encoded sum type. It looks daunting, but is an entirely automatic refactoring: just call <code>Match</code> on that object as well, and supply a handler for each of the cases. </p> <p> Recall that each of the cases of <code>IReservationsInstruction&lt;T&gt;</code> contains a tuple. The first element (<code>t.Item1</code>) is a value to be used as an input argument for the interpreter. The second element (<code>t.Item2</code>) is a function. Notice that in all three cases, this interpreter calls a helper method with <code>t.Item1</code> and then calls the continuation <code>t.Item2</code> with the return value from the helper method; e.g. <code>t.Item2(IsReservationInFuture(t.Item1))</code>. All three continuation functions return a new <code>IReservationsProgram&lt;T&gt;</code>, representing the next step in the program, so you'll have to recursively call <code>Interpret</code> again. </p> <p> The <code>IsReservationInFuture</code> helper method is simple: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsReservationInFuture(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now&nbsp;&lt;&nbsp;reservation.Date; }</pre> </p> <p> Notice that this is an entirely normal static helper method that every C# programmer should know how to write. You're now back on familiar territory. The same goes for the two other helper methods <code>ReadReservations</code> and <code>Create</code>. They're slightly refactored versions of the above <code>SqlReservationsRepository</code> methods, so I'm not going to repeat them here. Again, you're welcome to look at the details in the code repository. </p> <h3 id="4ec6be3a82684d77933b98c6dc625b94"> Refactor instruction arguments to Parameter Object <a href="#4ec6be3a82684d77933b98c6dc625b94" title="permalink">#</a> </h3> <p> As you know from another article, <a href="/2018/06/25/visitor-as-a-sum-type">you can refactor any Church-encoded sum type to a Visitor</a>. If you want to do it step-wise, you start by introducing a Parameter Object, as suggested by <a href="http://amzn.to/YPdQDf">Refactoring</a>. There's two sum types in play in this code base, but you can arbitrarily choose to start with the instruction set. Instead of taking three method arguments, change the <code>Match</code> method so that it takes only a single argument: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ReservationsInstructionParameters</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;parameters); }</pre> </p> <p> The new Parameter Object looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsInstructionParameters</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsInstructionParameters( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;isReservationInFuture, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;create) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.IsReservationInFuture&nbsp;=&nbsp;isReservationInFuture; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.ReadReservations&nbsp;=&nbsp;readReservations; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Create&nbsp;=&nbsp;create; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;IsReservationInFuture&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReadReservations&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Create&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> This change clearly just moves things around, so nothing much is yet gained. You'll need to adjust other parts of the code in order to pass an instance of this Parameter Object to the <code>Match</code> method, but that's trivial and automatable work, so I'll skip showing it. </p> <p> If you've noticed that the <code>ReservationsInstructionParameters&lt;T, TResult&gt;</code> Parameter Object is nothing but a container of three functions, you may not be impressed, but from <a href="/2018/02/12/object-isomorphisms">Object isomorphisms</a> we know that a tuple (or record) of functions is isomorphic to an object, thereby nicely putting everything in place for the next change. </p> <h3 id="9adbd66ce0874681b5a47d27d29770e2"> Refactor instruction set Parameter Object to an interface <a href="#9adbd66ce0874681b5a47d27d29770e2" title="permalink">#</a> </h3> <p> Instead of a record of three functions, refactor the Parameter Object to an interface with three members: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsInstructionParameters</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;IsReservationInFuture(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;ReadReservations(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Create(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t); }</pre> </p> <p> Each function is now a method on the interface. Still not perfect, but better. Again, you have to make all sort of adjustments to code that interacts with the <code>Match</code> method. This is the most laborious refactoring, because in every place where before you could simply pass lambda expressions, you now have to introduce explicit classes that implement the interface. </p> <p> For example, the database interpreter now has to look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Interpret&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;program, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;connectionString) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;program.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pure:&nbsp;x&nbsp;=&gt;&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free:&nbsp;i&nbsp;=&gt;&nbsp;i.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InterpretReservationsInstructionParameters</span>&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connectionString))); }</pre> </p> <p> So far, we've only started refactoring the instruction set, so you still need to handle <code>IReservationsProgram&lt;T&gt;</code> values by supplying two lambda expressions to its <code>Match</code> method. When handling the instruction <code>i</code>, however, you must now supply an implementation of the new <code>IReservationsInstructionParameters&lt;T, TResult&gt;</code> interface. </p> <p> You can do that by creating a new private, nested class: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InterpretReservationsInstructionParameters</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsInstructionParameters</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;</pre> </p> <p> As an example, this class implements the <code>ReadReservations</code> method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&gt;&nbsp;t) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1.Date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1.Date.AddDays(1).AddTicks(-1)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;t.Item2(reservations).Interpret(connectionString); } <span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;min, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;max)</pre> </p> <p> The method first calls a helper method also called <code>ReadReservations</code> to read the reservations from the database. That helper method is a perfectly normal C# method that queries a database. Its implementation is equivalent to the above <code>SqlReservationsRepository.ReadReservations</code> implementation, so while I've included its signature in the above code listing, there's no need to repeat the method body here. </p> <p> Keep in mind that <code>t</code> is the horribly typed tuple declared as the method argument, so <code>t.Item1</code> is just a <code>DateTimeOffset</code> value that you can pass as argument(s) to the <code>ReadReservations</code> helper method. </p> <p> The helper method returns <code>reservations</code>, which is an <code>IReadOnlyCollection&lt;Reservation&gt;</code>. You can now (as before) call the continuation function <code>t.Item2</code> with that object. The continuation returns a new <code>IReservationsProgram&lt;T&gt;</code> that you'll then have to recursively handle by calling <code>Interpret</code> again. This is just like before the refactoring. </p> <h3 id="6a6b11b4983942fea7a6125a95e6adcd"> Rename instruction set API <a href="#6a6b11b4983942fea7a6125a95e6adcd" title="permalink">#</a> </h3> <p> You're simply following the refactoring steps outlined in <a href="/2018/06/25/visitor-as-a-sum-type">Visitor as a sum type</a>, so now you can rename the types involved with the instruction set to follow the naming conventions of the Visitor design pattern: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsInstructionVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitIsReservationInFuture(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitReadReservations(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitCreate(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;t); }</pre> </p> <p> This is the interface previously named <code>IReservationsInstructionParameters&lt;T, TResult&gt;</code> renamed, and with the word <code>Visit</code> affixed. Likewise, you can also make similar changes to the <code>IReservationsInstruction&lt;T&gt;</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Accept&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">IReservationsInstructionVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;visitor); }</pre> </p> <p> These changes are simple rename refactorings, so while they affect much other code, I'm not going to list it all. </p> <h3 id="737d486102ea48e286e4fca81d7ea3f8"> Make program API a Visitor <a href="#737d486102ea48e286e4fca81d7ea3f8" title="permalink">#</a> </h3> <p> Following the same refactoring steps, you can also turn <code>IReservationsProgram&lt;T&gt;</code> into an application of the Visitor design pattern: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Accept&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">IReservationsProgramVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;visitor); }</pre> </p> <p> You simply rename the <code>Match</code> method to <code>Accept</code>, and call the object passed to it <code>visitor</code>. The Visitor itself is defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsProgramVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitFree(<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitPure(<span style="color:#2b91af;">T</span>&nbsp;x); }</pre> </p> <p> While these steps conclude the refactoring steps outlined in the previously mentioned article on how to turn a Church-encoded sum type into a Visitor, the resulting code can still benefit from further clean-up. </p> <h3 id="b362e6cd50dc4f1f855fa83790250997"> Refactor tuple argument to argument list <a href="#b362e6cd50dc4f1f855fa83790250997" title="permalink">#</a> </h3> <p> If you consider the current definition of <code>IReservationsInstructionVisitor&lt;T, TResult&gt;</code> (see above), you'll notice that each method takes a single argument in the shape of a tuple. From <a href="/2018/01/29/argument-list-isomorphisms">Argument list isomorphisms</a> you know that you can refactor such a method into a method that takes a normal argument list: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsInstructionVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitIsReservationInFuture( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;continuation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;continuation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;VisitCreate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;continuation); }</pre> </p> <p> Each of these methods now take an input argument (e.g. a <code>Reservation</code> or a <code>DateTimeOffset</code>) and a continuation function. This already improves the API. </p> <h3 id="824a28cae0224aaf89332305889034f6"> SQL Visitor <a href="#824a28cae0224aaf89332305889034f6" title="permalink">#</a> </h3> <p> In an attempt to make things look proper object-oriented, then, the journey is complete. Instead of a <code>SqlReservationsRepository</code>, you instead need a <code>SqlReservationsProgramVisitor&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SqlReservationsProgramVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsProgramVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsInstructionVisitor</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;connectionString; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SqlReservationsProgramVisitor(<span style="color:blue;">string</span>&nbsp;connectionString) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.connectionString&nbsp;=&nbsp;connectionString; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitPure(<span style="color:#2b91af;">T</span>&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitFree(<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;i.Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitIsReservationInFuture( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;continuation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;isInFuture&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now&nbsp;&lt;&nbsp;reservation.Date; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;continuation(isInFuture).Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;continuation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date.Date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date.Date.AddDays(1).AddTicks(-1)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;continuation(reservations).Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;ReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;min, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;max) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(connectionString)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(readByRangeSql,&nbsp;conn)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@MinDate&quot;</span>,&nbsp;min)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@MaxDate&quot;</span>,&nbsp;max)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;rdr&nbsp;=&nbsp;cmd.ExecuteReader()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;(rdr.Read()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;(<span style="color:#2b91af;">DateTimeOffset</span>)rdr[<span style="color:#a31515;">&quot;Date&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;(<span style="color:blue;">string</span>)rdr[<span style="color:#a31515;">&quot;Name&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;(<span style="color:blue;">string</span>)rdr[<span style="color:#a31515;">&quot;Email&quot;</span>], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;(<span style="color:blue;">int</span>)rdr[<span style="color:#a31515;">&quot;Quantity&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;result; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;readByRangeSql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;[Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;[dbo].[Reservations] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;YEAR(@MinDate)&nbsp;&lt;=&nbsp;YEAR([Date]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;MONTH(@MinDate)&nbsp;&lt;=&nbsp;MONTH([Date]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;DAY(@MinDate)&nbsp;&lt;=&nbsp;DAY([Date]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;YEAR([Date])&nbsp;&lt;=&nbsp;YEAR(@MaxDate) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;MONTH([Date])&nbsp;&lt;=&nbsp;MONTH(@MaxDate) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;DAY([Date])&nbsp;&lt;=&nbsp;DAY(@MaxDate)&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitCreate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;continuation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;continuation(Create(reservation)).Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Create(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(connectionString)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(createReservationSql,&nbsp;conn)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;reservation.Date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;reservation.Name)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Email&quot;</span>,&nbsp;reservation.Email)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Quantity&quot;</span>,&nbsp;reservation.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;createReservationSql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INSERT&nbsp;INTO&nbsp;[dbo].[Reservations]&nbsp;([Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(@Date,&nbsp;@Name,&nbsp;@Email,&nbsp;@Quantity)&quot;</span>; }</pre> </p> <p> Most of this code is similar to the <code>SqlReservationsRepository</code> from the beginning of the article. The <code>IReservationsRepository</code> interface no longer exists; instead this Visitor implements two interfaces: <code>IReservationsProgramVisitor&lt;T, T&gt;</code> and <code>IReservationsInstructionVisitor&lt;IReservationsProgram&lt;T&gt;, T&gt;</code>. A free monad recursively interacts with itself by going back and forth between an 'instruction' and the overall 'program'. Implementing both interfaces in a single class makes it much easier to transition between these two views, as you no longer have to pass e.g. the <code>connectionString</code> around between various objects. </p> <p> This also means that instead of having to rely on an extension method in order to be able to continue, notice that each method can now continue by calling <code>Accept(this)</code>. </p> <p> Instead of injecting <code>IReservationsRepository</code> into objects, you can call methods that return <code>IReservationsProgram&lt;T&gt;</code> objects and then interpret them using the above <code>SqlReservationsProgramVisitor&lt;T&gt;</code>. For example, you could call <code>TryAccept</code> with a <code>Reservation</code> object: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;p&nbsp;=&nbsp;maîtreD.TryAccept(reservation);</pre> </p> <p> The <code>p</code> variable is an <code>IReservationsProgram&lt;int?&gt;</code> object, where the <code>int?</code> represents a potential reservation ID. At this point, <code>p</code> is a 'program', and you can 'run' it by asking it to accept a Visitor: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;id&nbsp;=&nbsp;p.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlReservationsProgramVisitor</span>&lt;<span style="color:blue;">int</span>?&gt;(connectionString)); </pre> </p> <p> When <code>Accept</code> returns, <code>id</code> (an <code>int?</code>) has a value, or is <code>null</code>, according to the exact details of <code>reservation</code> and the state of the database. </p> <h3 id="4cf204a182af44ea8145e16915752e89"> Testability <a href="#4cf204a182af44ea8145e16915752e89" title="permalink">#</a> </h3> <p> Is this design still testable? Yes, indeed, the overall free monad design is <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, and thereby <a href="/2015/05/07/functional-design-is-intrinsically-testable">inherently testable</a>. Instead of using a dynamic mock library like Moq, you can add a test-specific implementation of the free monad Visitor to your unit testing code base: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">StubReservationsVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsProgramVisitor</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsInstructionVisitor</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;,&nbsp;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;isInFuture; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;reservations; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;StubReservationsVisitor( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;isInFuture, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.isInFuture&nbsp;=&nbsp;isInFuture; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.reservations&nbsp;=&nbsp;reservations; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.id&nbsp;=&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitPure(<span style="color:#2b91af;">T</span>&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitFree(<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;i.Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitIsReservationInFuture( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;continuation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;continuation(isInFuture).Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitReadReservations( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;continuation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;continuation(reservations).Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitCreate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">IReservationsProgram</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;continuation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;continuation(id).Accept(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As a true <a href="http://xunitpatterns.com/Test%20Stub.html">Stub</a>, this implementation ignores the input values and returns pre-configured values. When <code>VisitIsReservationInFuture</code> is called, for example, the implementation ignores the <code>reservation</code> argument and instead 'returns' the <code>isInFuture</code> class field by calling <code>continuation(isInFuture)</code>. </p> <p> You can exercise the happy-path test case from the beginning of this article as a unit test using this Stub Visitor: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">BookingApiTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;TryAcceptReturnsReservationIdInHappyPathScenario( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;sut, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;excessCapacity, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;sut&nbsp;=&nbsp;sut.WithCapacity( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;+&nbsp;excessCapacity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.TryAccept(reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expected, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StubReservationsVisitor</span>&lt;<span style="color:blue;">int</span>?&gt;(<span style="color:blue;">true</span>,&nbsp;reservations,&nbsp;expected))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(reservation.IsAccepted); }</pre> </p> <p> This test still uses AutoFixture to create objects such as <code>reservation</code>, <code>reservations</code>, and so on, but instead of relying on dynamic mocks injected into the <code>sut</code>, it uses the <code>StubReservationsVisitor&lt;T&gt;</code> class to interpret the return value from <code>TryAccept</code>. </p> <h3 id="fa5ef38abcb84dee913b517ebcd2bfb5"> Arrow code <a href="#fa5ef38abcb84dee913b517ebcd2bfb5" title="permalink">#</a> </h3> <p> From an external perspective, as a user of <code>MaîtreD.TryAccept</code>, the API looks exotic, but to a degree, fairly object-oriented, with its implementation of the Visitor design pattern. Looking at the above internal implementation of the method, however, reveals the most problematic part of this entire exercise. The code doesn't look nice. </p> <p> Not only do we have nested closures, but it also looks like the dreaded Arrow Code anti-pattern. Partially, the problem is that while C# has some support for monadic syntactic sugar, in the form of query expressions, you can't branch inside a query expression. That's not the whole problem, though. Even in F#, with a computation expression, the equivalent code would look like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;reservations&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;isInFuture&nbsp;=&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;not&nbsp;isInFuture &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">return</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;reservations&nbsp;=&nbsp;readReservations&nbsp;reservation.Date &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;List.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">return</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;reservationId&nbsp;=&nbsp;create&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Some&nbsp;reservationId&nbsp;}</pre> </p> <p> While this looks smoother, the code still exhibits the arrow shape. There are ways to address that problem, but that's a topic for the next article. </p> <h3 id="a1d12d37f6de436cbe2a966eb12f4bc9"> Conclusion <a href="#a1d12d37f6de436cbe2a966eb12f4bc9" title="permalink">#</a> </h3> <p> Even in C#, you can refactor from Dependency Injection to a free monad, implemented as a Visitor. Should you, though? </p> <p> As a rule of thumb, no, I don't think it's a good idea. Free monads are worth considering in Haskell where they are much closer to being 'free', in the sense that there's little programming overhead involved with defining and using them. Already when you translate a free monad to F# do you discover how much boilerplate programming is involved. Still, due to F#'s computation expressions, a free monad may occasionally be worth considering. Both in implementation and at call sites, you can make the API easy to use. </p> <p> In C#, you run into its lack of support for branching inside monadic composition. Not only does a free monad look <a href="/2015/08/03/idiomatic-or-idiosyncratic">non-idiomatic</a> in C#, but writing the 'programs' without good syntactic sugar for monads make this alternative even less compelling. To make matters worse, the boilerplate code you have to write in C# is positively ugly. As this article is a result of an experiment, I admit that I have no production experience with free monads as Visitors in C#. Still, due to the lack of type inference in crucial parts, I'd venture the guess that the implementation would also prove to be frustratingly refactor-resistant. </p> <p> Perhaps, one day, you'll run into an edge case where you have a dependency that could benefit from being refactored to a Visitor, but in general, I'd presume that Dependency Injection in C# is the lesser of two evils. Still, I think it's interesting, and illustrative, that it's possible to refactor an injected dependency to a free monad - even in C#! </p> <p> <strong>Next:</strong> <a href="/2018/07/30/flattening-arrow-code-using-a-stack-of-monads">Flattening arrow code using a stack of monads</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Angular addition monoid https://blog.ploeh.dk/2018/07/16/angular-addition-monoid 2018-07-16T14:40:00+00:00 Mark Seemann <div id="post"> <p> <em>Geometric angles can be added together. Angular addition forms a monoid.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). </p> <p> In geometry, an angle is a measure of how two crossing lines relate to each other. In mathematics, angles are usually represented in radians, but in daily use, they're mostly measured in degrees between 0 and 360. </p> <h3 id="beb83a6d54c6454085e6bf78e381cd9a"> Angular addition <a href="#beb83a6d54c6454085e6bf78e381cd9a" title="permalink">#</a> </h3> <p> You can always draw an angle within a circle. Here's a 45° angle: </p> <p> <img src="/content/binary/angle-45.png" alt="A 45° angle."> </p> <p> If you add another 90° angle to that, you get a 135° angle: </p> <p> <img src="/content/binary/angle-45-90.png" alt="A 45° angle and a 90° angle added to it."> </p> <p> What do you get if you add 90° to 315°? </p> <p> <img src="/content/binary/angle-315-plus-90.png" alt="A 315° angle and a 90° angle next to it."> </p> <p> Well, you get 45°, of course! </p> <p> <img src="/content/binary/angle-315-90.png" alt="A 315° angle and a 90° angle overlaid on the first one."> </p> <p> There's only 360° in a circle, so overflow is handled, in this case, by subtracting 360°. In general, however, angular addition is nothing but modulo 360 addition. </p> <h3 id="53d5cb53bc904c9c8430ed21973fc490"> Angle struct <a href="#53d5cb53bc904c9c8430ed21973fc490" title="permalink">#</a> </h3> <p> You can model a geometric angle as a struct. Here's a simple example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">Angle</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">decimal</span>&nbsp;degrees; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Angle(<span style="color:blue;">decimal</span>&nbsp;degrees) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.degrees&nbsp;=&nbsp;degrees&nbsp;%&nbsp;360m; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.degrees&nbsp;&lt;&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.degrees&nbsp;+=&nbsp;360m; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;FromDegrees(<span style="color:blue;">decimal</span>&nbsp;degrees) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Angle</span>(degrees); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;FromRadians(<span style="color:blue;">double</span>&nbsp;radians) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Angle</span>((<span style="color:blue;">decimal</span>)((180D&nbsp;/&nbsp;<span style="color:#2b91af;">Math</span>.PI)&nbsp;*&nbsp;radians)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;Add(<span style="color:#2b91af;">Angle</span>&nbsp;other) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Angle</span>(<span style="color:blue;">this</span>.degrees&nbsp;+&nbsp;other.degrees); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;Identity&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Angle</span>(0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Angle</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;((<span style="color:#2b91af;">Angle</span>)obj).degrees&nbsp;==&nbsp;<span style="color:blue;">this</span>.degrees; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">base</span>.Equals(obj); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.degrees.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;==(<span style="color:#2b91af;">Angle</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Equals(y); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;!=(<span style="color:#2b91af;">Angle</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;!x.Equals(y); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice the <code>Add</code> method, <a href="/2017/10/06/monoids">which is a binary operation</a>; it's an instance method on <code>Angle</code>, takes another <code>Angle</code> as input, and returns an <code>Angle</code> value. </p> <h3 id="65f48d17d79b4096a8c192ec7675496d"> Associativity <a href="#65f48d17d79b4096a8c192ec7675496d" title="permalink">#</a> </h3> <p> Not only is <code>Add</code> a binary operation; it's also associative. Here's an example: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:#2b91af;">Angle</span>.FromDegrees(135); <span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:#2b91af;">Angle</span>.FromDegrees(180); <span style="color:blue;">var</span>&nbsp;z&nbsp;=&nbsp;<span style="color:#2b91af;">Angle</span>.FromDegrees(300); <span style="color:blue;">var</span>&nbsp;left&nbsp;&nbsp;=&nbsp;x.Add(y).Add(z); <span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;x.Add(y.Add(z));</pre> </p> <p> Notice that <code>left</code> first evaluates <code>x.Add(y)</code>, which is 315°; then it adds 300°, which is 615°, but normalises to 255°. On the other hand, <code>right</code> first evaluates <code>y.Add(z)</code>, which is 480°, but normalises to 120°. It then adds those 120° to <code>x</code>, for a final result of 255°. Since <code>left</code> and <code>right</code> are both 255°, this illustrates that <code>Add</code> is associative. </p> <p> Obviously, this is only a single example, so it's no proof. While still not a proof, you can demonstrate the associativity property with more confidence by writing a property-based test. Here's one using <a href="https://fscheck.github.io/FsCheck">FsCheck</a> and <a href="https://xunit.net">xUnit.net</a>: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AddIsAssociative(<span style="color:#2b91af;">Angle</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Add(y).Add(z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Add(y.Add(z))); }</pre> </p> <p> By default, FsCheck generates 100 test cases, but even when I experimentally change the configuration to run 100,000 test cases, they all pass. For full disclosure, however, I'll admit that I defined the data generators to only use <code>NormalFloat</code> for the radian values, and only <code>decimal</code> values with up to 10 decimal places. If you try to use entirely unconstrained floating points, you'll see test failures caused by rounding errors. </p> <p> Changing the data generator is one way to address rounding errors. Another way is to add a bit of fuzzy tolerance to the assertion. In any case, though, the <code>Add</code> operation is associative. That rounding errors occur is an implementation detail of floating point arithmetic. </p> <h3 id="a912f6c80aeb4619b30476e3183edcc9"> Identity <a href="#a912f6c80aeb4619b30476e3183edcc9" title="permalink">#</a> </h3> <p> The above code listing defines a value called <code>Identity</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Angle</span>&nbsp;Identity&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Angle</span>(0);</pre> </p> <p> <em>As an Angle, I want my Add and Identity members to obey the monoid laws so that I can be a monoid.</em> </p> <p> As an example, both <code>left</code> and <code>right</code> should be <code>true</code> in the following: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:#2b91af;">Angle</span>.FromDegrees(370); <span style="color:blue;">var</span>&nbsp;left&nbsp;&nbsp;=&nbsp;x&nbsp;==&nbsp;<span style="color:#2b91af;">Angle</span>.Identity.Add(x); <span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;x&nbsp;==&nbsp;x.Add(<span style="color:#2b91af;">Angle</span>.Identity);</pre> </p> <p> That does, indeed, turn out to be the case. </p> <p> Again, you can generalise using FsCheck: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AddHasIdentity(<span style="color:#2b91af;">Angle</span>&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;<span style="color:#2b91af;">Angle</span>.Identity.Add(x)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;x.Add(<span style="color:#2b91af;">Angle</span>.Identity)); }</pre> </p> <p> Once more, a reservation identical to the one given above must be given when it comes to floating point arithmetic. </p> <h3 id="c1fd56a6390148b6a74ba539096f8524"> Conclusion <a href="#c1fd56a6390148b6a74ba539096f8524" title="permalink">#</a> </h3> <p> The <code>Add</code> method is an associative, binary operation with identity; it's a monoid. </p> <p> As far as I can tell, any modulo-based addition is a monoid, but while, say, modulo 37 addition probably doesn't have any practical application, modulo 360 addition does, because it's how you do angular addition. </p> <p> <strong>Next:</strong> <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">Strings, lists, and sequences as a monoid</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Typing and testing problem 23 https://blog.ploeh.dk/2018/07/09/typing-and-testing-problem-23 2018-07-09T07:03:00+00:00 Mark Seemann <div id="post"> <p> <em>Yet another reflection on the relationship between types and tests, this time with a simple example.</em> </p> <p> The debate about dynamic typing versus static typing still goes on. If it ever gets resolved, I suppose it'll be in the far future. Until then, one's position is bound to be determined mostly by experience and belief. I openly admit that I prefer statically typed languages like <a href="http://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>. </p> <p> As I've <a href="/2016/02/10/types-properties-software">previously touched on</a>, I can't help seeing types as a <a href="https://en.wikipedia.org/wiki/Slider_(computing)">slider</a>. The more to the right you pull it, the stronger the type system. The more to the left you pull it, the more you'll need automated tests to give you a sense of confidence in your code. </p> <p> In this article, I'll share an small revelation recently given to me. </p> <h3 id="16a20a29aa8842ce94d78fba54b0dad9"> Problem 23 <a href="#16a20a29aa8842ce94d78fba54b0dad9" title="permalink">#</a> </h3> <p> Somewhere, a <a href="https://stackoverflow.com">Stack Overflow</a> user was going through <a href="https://wiki.haskell.org/H-99:_Ninety-Nine_Haskell_Problems">Ninety-Nine Haskell Problems</a>, and <a href="https://stackoverflow.com/q/46791466/126014">asked how to unit test problem 23</a>. </p> <p> The problem is elementary: <blockquote> <a href="https://wiki.haskell.org/99_questions/21_to_28">"Extract a given number of randomly selected elements from a list."</a> </blockquote> Here's an example of the intended use: </p> <p> <pre>λ&gt; rndSelect "abcdefgh" 3 "fag"</pre> </p> <p> The first argument to <code>rndSelect</code> is the candidates from which to pick elements; in this case the letters <em>a</em> to <em>h</em>. The second argument is the number of values to select; in this case the number <em>3</em>. </p> <h3 id="4d98d4998ffd4721b942a03649007f09"> Test plan <a href="#4d98d4998ffd4721b942a03649007f09" title="permalink">#</a> </h3> <p> How does one test a function like that? Clearly, when randomness is involved, you'll need some way to regulate the randomness in order to <a href="https://martinfowler.com/articles/nonDeterminism.html">make tests deterministic</a>. With my blinders on, I assumed that this was the main problem, so I answered with the following plan for a few properties: <ul> <li>The length of the returned list should be equal to the input length.</li> <li>All elements in the returned list should be elements of the list of candidates.</li> </ul> In addition, I also suggested a way to make tests deterministic, but I'll get back to that later. </p> <p> In response to this plan, the user <a href="https://stackoverflow.com/users/3234959">chi</a> commented on my second suggestion: <blockquote> "I think this it is a consequence of the free theorem. If so, no need to test for that!" </blockquote> Sometimes, I find it difficult to shake my object-oriented TDD-influenced way of thinking, but <em>chi</em> is right. Here's why: </p> <h3 id="1b301d9910e3472c9039094e6e737d91"> Parametric polymorphism <a href="#1b301d9910e3472c9039094e6e737d91" title="permalink">#</a> </h3> <p> .NET, including C# and F#, has a platform feature called <em>generics</em>. Haskell has generics as well, although normally, that language feature is called <em>parametric polymorphism</em>. What I had in mind was a set of parametrically polymorphic functions with these types: </p> <p> <pre><span style="color:#2b91af;">rndGenSelect</span>&nbsp;::&nbsp;(<span style="color:blue;">RandomGen</span>&nbsp;g,&nbsp;<span style="color:blue;">Integral</span>&nbsp;i)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;g&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] <span style="color:#2b91af;">rndSelect</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;i&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;[a]</pre> </p> <p> Notice that both functions return lists of <code>a</code> values, where <code>a</code> is a type variable (in C#, you'd call it a <em>generic type argument</em>). It could be <code>Integer</code>, <code>String</code>, <code>Day</code>, or a custom domain type you'd added to the code base two minutes earlier. </p> <p> Given a completely unrestricted type variable, Haskell has no way of creating values. How could it, logically? </p> <p> In C#, you can write <code>default(T)</code>, which tends to mostly produce null references. Haskell doesn't have null, so with that option cut off, how would it be able to produce values of arbitrary types? It can't. </p> <p> When returning a list of <code>a</code> values, the only option open to a parametric polymorphic function is to pick values from its input arguments. For both <code>rndGenSelect</code> and <code>rndSelect</code>, there's only a single source of <code>a</code> values, so there's no reason to test that the functions return values from those lists of candidates. It's the only thing it can do. That's the <em>free theorem</em> for that function. </p> <p> It'd been an entirely different story if the function had had concrete types. If, for example, the function had had the type <code>RandomGen g =&gt; g -&gt; String -&gt; Int -&gt; String</code>, I could have written a function like this one: </p> <p> <pre><span style="color:#2b91af;">rndGenSelect&#39;</span>&nbsp;::&nbsp;<span style="color:blue;">RandomGen</span>&nbsp;g&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;g&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span> rndGenSelect&#39;&nbsp;_&nbsp;_&nbsp;count&nbsp;=&nbsp;<span style="color:blue;">replicate</span>&nbsp;count&nbsp;<span style="color:#a31515;">&#39;s&#39;</span> </pre> </p> <p> Because the type of elements is known at compile-time, we can pick an arbitrary <code>Char</code> value (<code>'s'</code>). This is possible because we know the type, and therefore can come up with a strategy to hard-code known values of that type. When the type argument is unknown, this is no longer possible. To paraphrase <a href="http://thecleancoder.blogspot.dk/2010/11/craftsman-63-specifics-and-generics.html">Robert C. Martin</a>, <em>as the types get more generic, the tests become more redundant</em>. </p> <h3 id="f7624b9d821c40a5b8a26de9f6a74120"> Taming randomness <a href="#f7624b9d821c40a5b8a26de9f6a74120" title="permalink">#</a> </h3> <p> Before we look at automated testing, let's consider how to turn randomness into deterministic behaviour. This is (seemingly) always a problem with unit testing when the desired behaviour contains randomness, because tests should be deterministic. Once again, however, it turns out that <a href="/2015/05/07/functional-design-is-intrinsically-testable">functional design is intrinsically testable</a>. Since Haskell design favours <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>, the core of <code>System.Random</code> is deterministic. </p> <p> This is, in fact, not much different from C#, where <a href="https://msdn.microsoft.com/en-us/library/system.random">the Random class</a> encapsulates an algorithm that computes a series of random-looking values based on an initial seed value. If you give it the same seed, it'll produce the same sequence of random-looking numbers. Haskell works the same way. </p> <p> This led me to a design with a 'core' function that does all the work, and a 'wrapper' function that only adds one extra feature: randomness. </p> <p> Starting my design with types, I wanted a function with this type: </p> <p> <pre><span style="color:#2b91af;">rndGenSelect</span>&nbsp;::&nbsp;(<span style="color:blue;">RandomGen</span>&nbsp;g,&nbsp;<span style="color:blue;">Integral</span>&nbsp;i)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;g&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] </pre> </p> <p> This is the type that I've already discussed above. Because of the free theorem, we already know that the returned list can only contain values selected from the input list. In other words, there's no need to test for that. </p> <p> This function takes a <code>RandomGen</code> argument, which is a type class of pure functions. <code>RandomGen</code> itself is pure; the source of randomness comes from how it's produced. More on that later. This, however, should enable me to write deterministic tests. </p> <h3 id="47f98962da4c42beb96e32d2e1ec92ad"> Properties <a href="#47f98962da4c42beb96e32d2e1ec92ad" title="permalink">#</a> </h3> <p> Before we start adding deterministic tests, let's see how far we can get with property-based testing. First, designing with types, I need to implement the function so that it compiles. This is the simplest implementation I could think of: </p> <p> <pre><span style="color:#2b91af;">rndGenSelect</span>&nbsp;::&nbsp;(<span style="color:blue;">RandomGen</span>&nbsp;g,&nbsp;<span style="color:blue;">Integral</span>&nbsp;i)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;g&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] rndGenSelect&nbsp;_&nbsp;xs&nbsp;_&nbsp;=&nbsp;[<span style="color:blue;">head</span>&nbsp;xs] </pre> </p> <p> This implementation is both incorrect and unsafe, but it compiles. In TDD fashion, then, I found it appropriate to add a test - in this case a <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a> property: </p> <p> <pre><span style="color:#2b91af;">lenProp</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;i&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">NonNegative</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> lenProp&nbsp;seed&nbsp;xs&nbsp;(NonNegative&nbsp;i)&nbsp;= &nbsp;&nbsp;i&nbsp;==&nbsp;genericLength&nbsp;(rndGenSelect&nbsp;(mkStdGen&nbsp;seed)&nbsp;xs&nbsp;i) </pre> </p> <p> This little piece of test code is the only surviving property from my original test plan. It states that for any non-negative count, the list returned from <code>rndGenSelect</code> should have the requested length. </p> <p> Writing this property, however, quickly forced me to deal with the case where the count is negative. It's easy to forget about edge cases when your function is nothing but a pie in the sky, but QuickCheck (and property-based testing in general) is really effective at grounding you in reality. Even with a language like Haskell, I still find the fast feedback loop from tests helpful. </p> <p> The original exercise specification doesn't mention what should happen if the count is negative, so after short deliberation, I decide to write another property: </p> <p> <pre><span style="color:#2b91af;">negLenProp</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;i&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Positive</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> negLenProp&nbsp;seed&nbsp;xs&nbsp;(Positive&nbsp;i)&nbsp;= &nbsp;&nbsp;0&nbsp;==&nbsp;genericLength&nbsp;(rndGenSelect&nbsp;(mkStdGen&nbsp;seed)&nbsp;xs&nbsp;(-i)) </pre> </p> <p> This property simply states that for all negative counts, the returned list should be empty. </p> <p> Both of these properties obviously fail, because of the incorrect implementation. </p> <p> The simplest implementation I could think of that passes both properties is this: </p> <p> <pre><span style="color:#2b91af;">rndGenSelect</span>&nbsp;::&nbsp;(<span style="color:blue;">RandomGen</span>&nbsp;g,&nbsp;<span style="color:blue;">Integral</span>&nbsp;i)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;g&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] rndGenSelect&nbsp;_&nbsp;xs&nbsp;count&nbsp;=&nbsp;genericReplicate&nbsp;count&nbsp;(<span style="color:blue;">head</span>&nbsp;xs) </pre> </p> <p> At this point, I don't see how TDD or property-based testing can help me move forward. The remaining work required is to add randomness to the mix. In this case, I'll need to use the <code>RandomGen</code> argument to produce random values, but since I don't know how its algorithm works, then even if I had a seed value known at compile-time, I wouldn't be able to predict which values it'd produce. </p> <h3 id="9e2a643290b14a53bf8fc82d8773bb08"> Selecting random indices <a href="#9e2a643290b14a53bf8fc82d8773bb08" title="permalink">#</a> </h3> <p> I admit that I don't know how to write the next test a priori. I do know, however, that if I implement what's missing, I have a deterministic function, and I can use it to write regression test. In other words, I'll reverse direction and write the code first, and then the test. What a novel idea. </p> <p> <pre><span style="color:#2b91af;">rndGenSelect</span>&nbsp;::&nbsp;(<span style="color:blue;">RandomGen</span>&nbsp;g,&nbsp;<span style="color:blue;">Integral</span>&nbsp;i)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;g&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] rndGenSelect&nbsp;rnd&nbsp;xs&nbsp;count&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;indices&nbsp;=&nbsp;genericTake&nbsp;count&nbsp;$&nbsp;randomRs&nbsp;(0,&nbsp;<span style="color:blue;">length</span>&nbsp;xs&nbsp;-&nbsp;1)&nbsp;rnd &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">fmap</span>&nbsp;(xs&nbsp;!!)&nbsp;indices </pre> </p> <p> This function first uses <code>randomRs</code> to produce an infinite list of values. These values are indices because they all fall between <code>0</code> and <code>length xs - 1</code>. In other words, they are indices into <code>xs</code>. </p> <p> While the list is infinite, it's lazily evaluated, so infinity itself isn't a problem. We only need <code>count</code> elements, though, so we can simply take the first <code>count</code> elements. </p> <p> Finally, the function maps over the list of indices, and for each index value, selects the element at that position. </p> <p> I could inline <code>indices</code> in the return expression, like this: </p> <p> <pre><span style="color:#2b91af;">rndGenSelect</span>&nbsp;::&nbsp;(<span style="color:blue;">RandomGen</span>&nbsp;g,&nbsp;<span style="color:blue;">Integral</span>&nbsp;i)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;g&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[a] rndGenSelect&nbsp;rnd&nbsp;xs&nbsp;count&nbsp;= &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;(xs&nbsp;!!)&nbsp;$&nbsp;genericTake&nbsp;count&nbsp;$&nbsp;randomRs&nbsp;(0,&nbsp;<span style="color:blue;">length</span>&nbsp;xs&nbsp;-&nbsp;1)&nbsp;rnd </pre> </p> <p> I find that more obscure than the first alternative, though, but both versions pass the properties and do what they're supposed to do. </p> <h3 id="c2e15aa34c4e4cb5bc0f7ad11580b711"> Regression testing <a href="#c2e15aa34c4e4cb5bc0f7ad11580b711" title="permalink">#</a> </h3> <p> How do I know that my code works? Well, that's always difficult with code that contains randomness, but you can load the function into GHCi and perform some <a href="https://en.wikipedia.org/wiki/Sanity_check">sanity testing</a>: </p> <p> <pre>λ&gt; rndGenSelect (mkStdGen 42) "foo" 3 "ofo" λ&gt; rndGenSelect (mkStdGen 1337) "bar" 10 "rabbaarrra" λ&gt; rndGenSelect (mkStdGen (-197221)) ['a'..'z'] 5 "ntfnc"</pre> </p> <p> That looks, I suppose, random enough... What's more important is that this is completely repeatable. This means that I can write <a href="/2018/04/30/parametrised-unit-tests-in-haskell">parametrised tests</a> that protect against regressions: </p> <p> <pre><span style="color:#a31515;">&quot;rndGenSelect&nbsp;of&nbsp;chars&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;~:&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;(seed,&nbsp;xs,&nbsp;count,&nbsp;expected)&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;&nbsp;3,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;ofo&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;&nbsp;&nbsp;1337,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;10,&nbsp;<span style="color:#a31515;">&quot;rabbaarrra&quot;</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(-197221,&nbsp;[<span style="color:#a31515;">&#39;a&#39;</span>..<span style="color:#a31515;">&#39;z&#39;</span>],&nbsp;&nbsp;5,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;ntfnc&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rnd&nbsp;=&nbsp;mkStdGen&nbsp;seed &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;rndGenSelect&nbsp;rnd&nbsp;xs&nbsp;count &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;expected&nbsp;~=?&nbsp;actual </pre> </p> <p> These tests don't drive the design, but they prevent regressions. If, at a later time, I, or someone else, inadvertently revert <code>rndGenSelect</code> to <code>genericReplicate count (head xs)</code>, these tests will fail. </p> <h3 id="2f404b7e4e9c4b22ace4b0e738c58233"> Humble function <a href="#2f404b7e4e9c4b22ace4b0e738c58233" title="permalink">#</a> </h3> <p> The original problem statement is to write a function without an explicit <code>RandomGen</code> argument. In the spirit of <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns'</a> <em>Humble Object</em> pattern, we can now click all our pieces together to a function that does what is required: </p> <p> <pre><span style="color:#2b91af;">rndSelect</span>&nbsp;::&nbsp;<span style="color:blue;">Integral</span>&nbsp;i&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[a]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">IO</span>&nbsp;[a] rndSelect&nbsp;xs&nbsp;count&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;rnd&nbsp;&lt;-&nbsp;newStdGen &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;rndGenSelect&nbsp;rnd&nbsp;xs&nbsp;count </pre> </p> <p> The only thing of interest here is that the function is impure, because it uses <code>newStdGen</code> to produce a random <code>RandomGen</code> value. It then delegates all work to <code>rndGenSelect</code>, which is covered by tests. </p> <p> As you can see, this function does <em>not</em> exhibit repeatable behaviour: </p> <p> <pre>λ&gt; rndSelect "abcdefgh" 3 "add" λ&gt; rndSelect "abcdefgh" 3 "daf"</pre> </p> <p> This should, I think, address the original problem statement. </p> <p> All source code for this article is <a href="https://github.com/ploeh/HasProblem23">available on GitHub</a>. </p> <h3 id="db2d673115f24bd2b4f4cbedae738342"> Summary <a href="#db2d673115f24bd2b4f4cbedae738342" title="permalink">#</a> </h3> <p> The first time I encountered parametric polymorphism was when C# got generics in 2005. Back then it was mostly explained as a mechanism to avoid <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing">boxing</a>, although it also seriously reduced the amount of boilerplate code you'd have to write in order to have type-safe collections. In many years, I mostly understood C# generics as a language feature aimed at efficiency and programmer productivity. </p> <p> It wasn't until I started to program in F#, with its stronger type inference, that it began to dawn on me that parametric polymorphism could also be a design tool. Making a function more generic tightens its contract, so to speak. The more generic a function becomes, the less wriggle room does it have. This may sound like a disadvantage to a programmer, but it's a boon to a <em>code reader</em>. When you, as a reader, encounter a parametrically polymorphic function, you already know that there are things that function can't do. Such functions come with invariants, or 'theorems', for free. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="d17665f7b1504d09b554e03df20c5d10"> <div class="comment-author">Roman Leventov <a href="#d17665f7b1504d09b554e03df20c5d10">#</a></div> <div class="comment-content"> "First, designing with types, I need to implement the function so that it compiles. This is the simplest implementation I could think of:" -- Why wouldn't you just use <a href="https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:undefined">undefined</a> function? </div> <div class="comment-date">2019-06-11 18:09 UTC</div> </div> <div class="comment" id="b21d4e3cb6a04e94a72c52ea6fde2078"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b21d4e3cb6a04e94a72c52ea6fde2078">#</a></div> <div class="comment-content"> <p> Roman, thank you for writing. For no particularly good reason. I tend to automatically disregard <code>undefined</code>, since it's basically one kind of <em>bottom</em> in Haskell. </p> <p> But I admit that that's not a bulletproof reason, since the actual function, at that point in the article, is still <a href="https://en.wikipedia.org/wiki/Partial_function">partial</a>. </p> <p> So honestly, the best answer is that I wrote "the simplest implementation I could think of" because I didn't think of <code>undefined</code>. </p> </div> <div class="comment-date">2019-06-11 18:44 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Terse operators make business code more readable https://blog.ploeh.dk/2018/07/02/terse-operators-make-business-code-more-readable 2018-07-02T12:00:00+00:00 Mark Seemann <div id="post"> <p> <em>Sometimes, terse operators can make code more readable. An article for all, even people who don't read Haskell code.</em> </p> <p> The <a href="https://www.haskell.org">Haskell</a> programming language has a reputation for being terse to the point of being unreadable. That reputation isn't undeserved, but to counter, other languages exist that are verbose to the point of being unreadable. </p> <p> Particularly, <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> Haskell code involves abstruse operators like <code>.</code>, <code>$</code>, <code>&lt;$&gt;</code>, <code>&gt;&gt;=</code>, <code>&lt;*&gt;</code>, <code>&lt;&gt;</code>, and so on. Not only do such operators look scary, but when I started writing Haskell code, it also bothered me that I didn't know how to pronounce these operators. I don't know how you read code, but my brain often tries to 'talk' about the code, silently, inside my head, and when it encounters something like <code>=&lt;&lt;</code>, it tends to stumble. </p> <p> At least, it used to. These days, my brain has accepted that in many cases, Haskell operators are a little like punctuation marks. When I read a piece of prose, my brain doesn't insist on 'saying' <em>comma</em>, <em>semicolon</em>, <em>question mark</em>, <em>period</em>, etcetera. Such symbols assist reading, and often adjust the meaning of a text, but aren't to be read explicitly as themselves. </p> <p> I'm beginning to realise that Haskell operators work like that; sometimes, they fade into the background and assist reading. </p> <p> As a word of caution, don't take this analogy literally. Each Haskell operator means something specific, and they aren't interchangeable. Additionally, Haskell enables you to add your own custom operators, and I'm not sure that e.g. <a href="https://hackage.haskell.org/package/lens/docs/Control-Lens.html">lens</a> operators like <code>.~</code> or <code>%~</code> make code more readable. </p> <h3 id="53a6bd351c814befaa7d32e57a563f5a"> A simple business code example <a href="#53a6bd351c814befaa7d32e57a563f5a" title="permalink">#</a> </h3> <p> Forgetting about the lens operators, though, consider a piece of business code like this: </p> <p> <pre><span style="color:#2b91af;">tryAccept</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeT</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;<span style="color:#2b91af;">Int</span> tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;guard&nbsp;=&lt;&lt;&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;reservations&nbsp;&lt;-&nbsp;readReservations&nbsp;$&nbsp;reservationDate&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;$&nbsp;reservationQuantity&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;guard&nbsp;$&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;create&nbsp;$&nbsp;reservation&nbsp;{&nbsp;reservationIsAccepted&nbsp;=&nbsp;True&nbsp;}</pre> </p> <p> Please read on, even if you don't read Haskell code. I'm not going to walk you through the details of how the operators work. That's not the point of this article. The point is how the operators enable you to focus on the overall picture of what's going on. The full source code is <a href="https://github.com/ploeh/dependency-injection-revisited">available on GitHub</a>. </p> <p> To establish a bit of context, this function determines whether or not to accept a restaurant reservation. Even if you've never read Haskell code before, see if you can get a <em>sense</em> of what's going on. </p> <p> First, there's a <code>guard</code> which seems to involve whether or not the reservation is in the future. Second, there seems to be some calculations involving reservations, reserved seats, culminating in another <code>guard</code>. Third, the function seems to <code>create</code> a reservation by setting <code>reservationIsAccepted</code> to <code>True</code>. </p> <p> Granted, it probably helps if you know that both <code>=</code> and <code>&lt;-</code> bind the left-hand symbol to the expression on the right side. Additionally, after all this talk about special Haskell operators, it may not be immediately apparent that <code>+</code> is the perfectly normal addition operator, and <code>&lt;=</code> is the well-known <em>less-than-or-equal</em> relational operator. What if we keep those operators, and mask the rest with a white rectangle symbol (▯)? </p> <p> <pre><span style="color:#2b91af;">tryAccept</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeT</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;<span style="color:#2b91af;">Int</span> tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;guard&nbsp;▯&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;reservations&nbsp;&lt;-&nbsp;readReservations&nbsp;▯&nbsp;reservationDate&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;▯&nbsp;reservationQuantity&nbsp;▯&nbsp;reservations &nbsp;&nbsp;guard&nbsp;▯&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;create&nbsp;▯&nbsp;reservation&nbsp;{&nbsp;reservationIsAccepted&nbsp;=&nbsp;True&nbsp;}</pre> </p> <p> Finally, you also ought to know that while Haskell code is read from top to bottom, you tend to read each expression from right to left. Armed with this knowledge, and by masking the operators, the business logic begins to stand out. </p> <p> First, it examines whether the <code>reservation</code> is in the future, and it does a <code>guard</code> of that. Again, I don't wish to make any claims that the code is magically self-documenting, because if you don't know what <code>guard</code> does, you don't know if this expression guards <em>against</em> the reservation being in the future, or if, conversely, it ensures that the reservation is in the future. It does the latter. </p> <p> Second, it conjures up some <code>reservations</code> from somewhere, by first getting the <code>reservationDate</code> from <code>reservation</code>, and then passing that value to <code>readReservations</code> (expressions are read from right to left). </p> <p> Moving on, it then calculates <code>reservedSeats</code> by starting with <code>reservations</code>, somehow extracting the <code>reservationQuantity</code> from those, and taking the <code>sum</code>. Since we've masked the operators, you can't tell exactly what's going on, but the gist is, hopefully, clear. </p> <p> The middle block of code concludes with another <code>guard</code>, this time ensuring that the <code>reservedSeats</code> plus the <code>reservationQuantity</code> is less than or equal to the <code>capacity</code>. </p> <p> Finally, the function sets <code>reservationIsAccepted</code> to <code>True</code> and calls <code>create</code>. </p> <p> What I find compelling about this is that the terseness of the Haskell operators enables you, a code reader, to scan the code to first understand the big picture. </p> <h3 id="16ce35c687e04ad1be79942884c27ccd"> Guards <a href="#16ce35c687e04ad1be79942884c27ccd" title="permalink">#</a> </h3> <p> Additionally, some common motifs begin to stand out. For example, there are two <code>guard</code> expressions. Because the operators are terse, the similarities stand out better. Let's juxtapose them: </p> <p> <pre>&nbsp;&nbsp;guard&nbsp;▯&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;guard&nbsp;▯&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> It seems clear that the same sort of thing is going on in both cases. There's a guard ensuring that a Boolean condition is satisfied. If you, however, reconsider the actual code, you'll see that the white rectangle hides two different operators: </p> <p> <pre>&nbsp;&nbsp;guard&nbsp;=&lt;&lt;&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;guard&nbsp;$&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;&lt;=&nbsp;capacity</pre> </p> <p> The reason for this is that it has to, because otherwise it wouldn't compile. <code>isReservationInFuture reservation</code> has the type <code>MaybeT ReservationsProgram Bool</code>. There's a Boolean value hidden in there, but it's buried inside a container. Using <code>=&lt;&lt;</code> enables you to pull out the Boolean value and pass it to <code>guard</code>. </p> <p> In the second <code>guard</code> expression, <code>reservedSeats + reservationQuantity&nbsp;reservation &lt;= capacity</code> is a 'naked' Boolean expression, so in this case you can use the <code>$</code> operator to pass it to <code>guard</code>. </p> <p> Haskellers may wonder why I chose <code>=&lt;&lt;</code> instead of the more common <code>&gt;&gt;=</code> operator in the first of the two <code>guard</code> expressions. I could have, but then the expression would have been this: </p> <p> <pre>&nbsp;&nbsp;isReservationInFuture&nbsp;reservation&nbsp;&gt;&gt;=&nbsp;guard</pre> </p> <p> The resulting behaviour is the same, but I think this obscures how the two <code>guard</code> expressions are variations on the same motif. </p> <p> The use of operators enables you to express code in such a way that motifs stand out. In contrast, I tried writing the same business functionality in <a href="http://fsharp.org">F#</a>, but it didn't come out as readable (in my opinion): </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;ReservationsProgram&lt;int&nbsp;option&gt;</span> <span style="color:blue;">let</span>&nbsp;tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;reservationsOption&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;ReservationsOption.bind&nbsp;guard&nbsp;&lt;|&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;reservations&nbsp;=&nbsp;readReservations&nbsp;reservation.Date &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;List.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r.Quantity)&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;guard&nbsp;(reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;create&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;}</pre> </p> <p> While you can also define custom operators in F#, it's rarely a good idea, for various reasons that, at its core, are related to how F# isn't Haskell. The lack of 'glue' operators in F#, though, obliged me to instead use the more verbose <code>ReservationsOption.bind</code>. This adds noise to the degree that the <code>guard</code> function disappears in the middle of the expression. The motif is fainter. </p> <h3 id="e476da4f87144f1d8b3c4f0766aab85e"> Piping <a href="#e476da4f87144f1d8b3c4f0766aab85e" title="permalink">#</a> </h3> <p> Another motif in Haskell code is <em>piping</em>. This is known from F# as well, where piping is normally done from left to right using the <code>|&gt;</code> operator. You can, as the above example shows, also use the right-to-left pipe operator <code>&lt;|</code>. In Haskell, expressions are idiomatically composed from right to left, often with the <code>$</code> operator, or, when using <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free</a> style, with the <code>.</code> operator. </p> <p> Once you realise that expressions compose from right to left, a masked expression like <code>sum ▯ reservationQuantity ▯ reservations</code> begins to look like a pipeline: start with <code>reservations</code>, somehow pipe them to <code>reservationQuantity</code>, and finally pipe the result of doing that to <code>sum</code>. That's actually not quite what happens, but I think that this compellingly communicates the overall idea: start with some reservations, consider their quantities, and calculate the sum of those. </p> <p> Another way to write that expression would be: </p> <p> <pre>&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;(<span style="color:blue;">fmap</span>&nbsp;reservationQuantity&nbsp;reservations)</pre> </p> <p> This implements the same behaviour as <code>sum $ reservationQuantity &lt;$&gt; reservations</code>, but once you get used to it, I like the operator-based alternative better. The operators fade into the background, enabling the flow of data to stand out better. </p> <h3 id="c78436b5007d4f569b5d619feb090caf"> Conclusion <a href="#c78436b5007d4f569b5d619feb090caf" title="permalink">#</a> </h3> <p> Haskell operators constitute the glue that enables you to compose expressions together. Often, you need to vary how expressions are composed together, because the types are slightly different. Picking an appropriate operator that composes particular expressions enables you to perform the composition with a high signal-to-noise ratio. </p> <p> Once you get used to reading Haskell code, the operators can fade into the background in well-factored code, just like punctuation marks assist you when you read prose. As always, this is no silver bullet. I've seen plenty of examples of obscure Haskell code as well, and copious use of operators is a fast way to obfuscate code. </p> <p> Use; punctuation? marks with. taste! </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Visitor as a sum type https://blog.ploeh.dk/2018/06/25/visitor-as-a-sum-type 2018-06-25T14:31:00+00:00 Mark Seemann <div id="post"> <p> <em>The Visitor design pattern is isomorphic to sum types.</em> </p> <p> This article is part of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">a series of articles about specific design patterns and their category theory counterparts</a>. In it, you'll see how the <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor design pattern</a> is equivalent to a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>. </p> <h3 id="ddc4643ff60949c382a9b631fe427cdd"> Sum types <a href="#ddc4643ff60949c382a9b631fe427cdd" title="permalink">#</a> </h3> <p> I think that the most important advantage of a statically typed programming language is that it gives you <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">immediate feedback</a> on your design and implementation work. Granted, that your code compiles may not be enough to instil confidence that you've done the right thing, but it's obvious that when your code doesn't compile, you still have work to do. </p> <p> A static type system enables you to catch some programming errors at compile time. It prevents you from making obvious mistakes like trying to divide a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">GUID</a> by a date. Some type systems don't offer much more help than that, while others are more articulate; I think that <a href="/2016/02/10/types-properties-software">type systems inhabit a continuous spectrum of capabilities</a>, although that, too, is a simplification. </p> <p> An often-touted advantage of programming languages like <a href="http://fsharp.org">F#</a>, <a href="https://ocaml.org">OCaml</a>, and <a href="https://www.haskell.org">Haskell</a> is that they, in the words of <a href="https://twitter.com/yminsky">Yaron Minsky</a>, enable you to <em>make illegal states unrepresentable</em>. The way these languages differ from languages like C# and Java is that they have <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>. </p> <p> In short, algebraic data types distinguish between <a href="https://en.wikipedia.org/wiki/Product_type">product types</a> and sum types. All statically typed language I've seen have product types, which you can think of as combinations of data. Objects with more than a single class fields would be product types. </p> <p> Sum types (also known as <em>discriminated unions</em>), on the other hand, are types that express mutually exclusive alternatives. Object-oriented programmers might mistake such a statement for sub-classing, but the difference is that object-oriented sub-classing creates a potentially infinite hierarchy of subtypes, while a sum type is statically constrained to a finite number of mutually exclusive cases. This is often useful. </p> <p> In this article, you'll see that a sum type is isomorphic to a corresponding Visitor. </p> <h3 id="dac9a78ee087418193fd776ceeade06b"> Church-encoded payment types <a href="#dac9a78ee087418193fd776ceeade06b" title="permalink">#</a> </h3> <p> In a previous article, you saw how to <a href="/2018/06/18/church-encoded-payment-types">Church-encode a domain-specific sum type</a>. That article, again, demonstrated how to rewrite <a href="/2016/11/28/easy-domain-modelling-with-types">a domain-specific F# discriminated union</a> as a C# API. The F# type was this <code>PaymentType</code> sum type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PaymentType</span>&nbsp;= |&nbsp;<span style="color:navy;">Individual</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentService</span> |&nbsp;<span style="color:navy;">Parent</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentService</span> |&nbsp;<span style="color:navy;">Child</span>&nbsp;<span style="color:blue;">of</span>&nbsp;originalTransactionKey&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;*&nbsp;paymentService&nbsp;:&nbsp;<span style="color:teal;">PaymentService</span></pre> </p> <p> Using Church-encoding in C#, you can arrive at this interface that models the same business problem: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;individual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;parent, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;child); }</pre> </p> <p> In order to use the API, the compiler obligates you to handle all three mutually exclusive cases defined by the three arguments to the <code>Match</code> method. Refer to the previous article for more details and code examples. All <a href="https://github.com/ploeh/ChurchEncoding">the C# code is also available on GitHub</a>. </p> <p> While the C# code works, I think it'd be a fair criticism to say that it doesn't feel object-oriented. Particularly the use of function delegates (<code>Func&lt;PaymentService, T&gt;</code>, etcetera) seems off. These days, C# is a multi-paradigmatic language, and function delegates have been around since 2007, so it's a perfectly fine C# design. Still, if we're trying to understand how object-oriented programming relates to fundamental programming abstractions, it behoves us to consider a more classic form of object-orientation. </p> <h3 id="19be9752ec8a44e18995db6c96c99bfe"> Introduce Parameter Object <a href="#19be9752ec8a44e18995db6c96c99bfe" title="permalink">#</a> </h3> <p> Through a series of refactorings you can transform the Church-encoded <code>IPaymentType</code> interface to a Visitor. The first step is to use <a href="http://amzn.to/YPdQDf">Refactoring</a>'s <em>Introduce Parameter Object</em> to turn the three method arguments of <code>Match</code> into a single object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PaymentTypeParameters</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;PaymentTypeParameters( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;individual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;parent, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;child) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Individual&nbsp;=&nbsp;individual; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Parent&nbsp;=&nbsp;parent; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Child&nbsp;=&nbsp;child; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;Individual&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;Parent&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;Child&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> The modified <code>IPaymentType</code> interface then looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">PaymentTypeParameters</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;parameters); }</pre> </p> <p> Clearly, this change means that you must also adjust each implementation of <code>IPaymentType</code> accordingly. Here's the <code>Match</code> method of <code>Individual</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">PaymentTypeParameters</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;parameters) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;parameters.Individual(paymentService); }</pre> </p> <p> The two other implementations (<code>Parent</code> and <code>Child</code>) change in the same way; the modifications are trivial, so I'm not going to show them here, but all the code is <a href="https://github.com/ploeh/ChurchEncoding/commit/64fa2638ffbdb81a077ac1dc3fbce697b3cba35b">available as a single commit</a>. </p> <p> Likewise, client code that uses the API needs adjustment, like the <code>ToJson</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;ToJson(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span>&nbsp;payment) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;payment.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentTypeParameters</span>&lt;<span style="color:#2b91af;">PaymentJsonModel</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;individual&nbsp;:&nbsp;ps&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;ps.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;ps.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent&nbsp;:&nbsp;ps&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;ps.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;ps.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child&nbsp;:&nbsp;cps&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;cps.PaymentService.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;cps.PaymentService.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">string</span>&gt;(cps.OriginalTransactionKey) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})); }</pre> </p> <p> From <a href="/2018/01/29/argument-list-isomorphisms">argument list isomorphisms</a> we know that an argument list is isomorphic to a Parameter Object, so this step should come as no surprise. We also know that the reverse translation (from Parameter Object to argument list) is possible. </p> <h3 id="496f81b27ad64feaaf0705eab578d9e8"> Add Run prefix <a href="#496f81b27ad64feaaf0705eab578d9e8" title="permalink">#</a> </h3> <p> I think it looks a little strange that the functions comprising <code>PaymentTypeParameters&lt;T&gt;</code> are named <code>Individual</code>, <code>Parent</code>, and <code>Child</code>. Functions <em>do</em> something, so they ought to be named with verbs. This turns out only to be an intermediary step, but I'll add the prefix <code>Run</code> to all three: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PaymentTypeParameters</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;PaymentTypeParameters( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;individual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;parent, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;child) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RunIndividual&nbsp;=&nbsp;individual; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RunParent&nbsp;=&nbsp;parent; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RunChild&nbsp;=&nbsp;child; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;RunIndividual&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;RunParent&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;RunChild&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> This doesn't change the structure of the code in any way, but sets it up for the next step. </p> <h3 id="690e226135a8414fbb122ffe834a5e87"> Refactor to interface <a href="#690e226135a8414fbb122ffe834a5e87" title="permalink">#</a> </h3> <p> The definition of <code>PaymentTypeParameters&lt;T&gt;</code> still doesn't look object-oriented. While it's formally an object, it's an object that composes three function delegates. We've managed to move the function delegates around, but we haven't managed to get rid of them. From <a href="/2018/02/12/object-isomorphisms">object isomorphisms</a>, however, we know that tuples of functions are isomorphic to objects, and that's essentially what we have here. In this particular case, there's no implementation code in <code>PaymentTypeParameters&lt;T&gt;</code> itself - it's nothing but a group of three functions. You can refactor that class to an interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPaymentTypeParameters</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;RunIndividual(<span style="color:#2b91af;">PaymentService</span>&nbsp;individual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;RunParent(<span style="color:#2b91af;">PaymentService</span>&nbsp;parent); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;RunChild(<span style="color:#2b91af;">ChildPaymentService</span>&nbsp;child); }</pre> </p> <p> The implementations of <code>Individual</code>, <code>Parent</code>, and <code>Child</code> don't change; only the signature of <code>Match</code> changes slightly: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">IPaymentTypeParameters</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;parameters); }</pre> </p> <p> Since this change removes the function delegates, it requires client code to change: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;ToJson(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span>&nbsp;payment) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;payment.Match(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentTypeToJsonParameters</span>()); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PaymentTypeToJsonParameters</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IPaymentTypeParameters</span>&lt;<span style="color:#2b91af;">PaymentJsonModel</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;RunIndividual(<span style="color:#2b91af;">PaymentService</span>&nbsp;individual) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;individual.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;individual.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;RunParent(<span style="color:#2b91af;">PaymentService</span>&nbsp;parent) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;parent.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;parent.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;RunChild(<span style="color:#2b91af;">ChildPaymentService</span>&nbsp;child) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;child.PaymentService.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;child.PaymentService.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">string</span>&gt;(child.OriginalTransactionKey) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>ToJson</code> method now has to delegate to a <code>private</code> class that implements <code>IPaymentTypeParameters&lt;PaymentJsonModel&gt;</code>. In Java and F# you'd be able to pass an object expression, but in C# you have to create an explicit class for the purpose. The implementations of the three methods of the interface still correspond to the three functions the previous incarnations of the code used. </p> <h3 id="ca0c6f3576c74786b738f04904d79fb8"> Rename to Visitor <a href="#ca0c6f3576c74786b738f04904d79fb8" title="permalink">#</a> </h3> <p> At this point, the Visitor pattern's structure is already in place. The only remaining step is to rename the various parts of the API so that this becomes clear. You can start by renaming the <code>IPaymentTypeParameters&lt;T&gt;</code> interface to <code>IPaymentTypeVisitor&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPaymentTypeVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitIndividual(<span style="color:#2b91af;">PaymentService</span>&nbsp;individual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitParent(<span style="color:#2b91af;">PaymentService</span>&nbsp;parent); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;VisitChild(<span style="color:#2b91af;">ChildPaymentService</span>&nbsp;child); }</pre> </p> <p> Notice that I've also renamed the methods from <code>RunIndividual</code>, <code>RunParent</code>, and <code>RunChild</code> to <code>VisitIndividual</code>, <code>VisitParent</code>, and <code>VisitChild</code>. </p> <p> Likewise, you can rename the <code>Match</code> method to <code>Accept</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Accept&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">IPaymentTypeVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor); }</pre> </p> <p> In <a href="http://amzn.to/XBYukB">Design Patterns</a>, the Visitor design pattern is only described in such a way that both <code>Accept</code> and <code>Visit</code> methods have <code>void</code> return types, but from <a href="/2018/01/15/unit-isomorphisms">unit isomorphisms</a> we know that this is equivalent to returning <em>unit</em>. Thus, setting <code>T</code> in the above API to a suitable <em>unit</em> type (like the one defined in F#), you arrive at the canonical Visitor pattern. The generic version here is simply a generalisation. </p> <p> For the sake of completeness, client code now looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;ToJson(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span>&nbsp;payment) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;payment.Accept(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentTypeToJsonVisitor</span>()); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PaymentTypeToJsonVisitor</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IPaymentTypeVisitor</span>&lt;<span style="color:#2b91af;">PaymentJsonModel</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;VisitIndividual(<span style="color:#2b91af;">PaymentService</span>&nbsp;individual) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;individual.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;individual.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;VisitParent(<span style="color:#2b91af;">PaymentService</span>&nbsp;parent) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;parent.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;parent.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;VisitChild(<span style="color:#2b91af;">ChildPaymentService</span>&nbsp;child) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;child.PaymentService.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;child.PaymentService.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">string</span>&gt;(child.OriginalTransactionKey) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can refactor all the <a href="/2018/05/22/church-encoding">other Church encoding examples I've shown you</a> to Visitor implementations. It doesn't always make the code more readable, but it's possible. </p> <h3 id="38f9e2c752e64c748d1799c2e32f37c1"> From Visitor to sum types <a href="#38f9e2c752e64c748d1799c2e32f37c1" title="permalink">#</a> </h3> <p> In this article, I've shown how to refactor from a Church-encoded sum type to a Visitor, using the following refactoring steps: <ol> <li>Introduce Parameter Object</li> <li>(Rename Method (by adding a <code>Run</code> prefix))</li> <li>Refactor to interface</li> <li>Rename to Visitor terminology</li> </ol> All those steps are, I believe, isomorphic, in that they have reverse translations. Thus, since (according to <a href="http://amzn.to/13tGJ0f">Conceptual Mathematics</a>) isomorphisms are transitive, the translation from sum type to Visitor must have a reverse translation as well. This also seems to me to be intuitively correct, as it's clear to me how to go the other way. Starting with a Visitor: <ol> <li>Refactor the Visitor interface to a Parameter Object that composes functions</li> <li>Refactor the Parameter Object to an argument list</li> <li>Rename types and members as desired</li> </ol> You can, I think, read this article from the bottom towards the top to get an impression of what such a series of refactorings would look like, so I'm not going to explicitly provide an example. </p> <h3 id="b5ac92e65f5d498289a2f99e2754ec00"> Summary <a href="#b5ac92e65f5d498289a2f99e2754ec00" title="permalink">#</a> </h3> <p> Algebraic data types enable you to <em>make illegal states unrepresentable</em>. Most programming languages have product types, so it's the lack of sum types that seems to make the difference between languages like C# and Java on the one side, and languages like F#, OCaml, or Haskell on the other side. </p> <p> You can, however, achieve the same objective with object-oriented design. The Visitor design pattern is equivalent to sum types, so everything you can express with a sum type in, say, F#, you can express with a Visitor in C#. </p> <p> That's not to say that these two representations are equal in readability or maintainability. F# and Haskell sum types are declarative types that usually only take up a few lines of code. Visitor, on the other hand, is a small object hierarchy; it's a more verbose way to express the idea that a type is defined by mutually exclusive and heterogeneous cases. I know which of these alternatives I prefer, but if I were caught in an object-oriented code base, it's nice to know that it's still possible to model a domain with algebraic data types. </p> <p> <strong>Next:</strong> <a href="/2019/07/22/chain-of-responsibility-as-catamorphisms">Chain of Responsibility as catamorphisms</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="91f945936c774b33aa305eb037d9131e"> <div class="comment-author"><a href="https://github.com/wallymathieu">Oskar Gewalli</a> <a href="#91f945936c774b33aa305eb037d9131e">#</a></div> <div class="comment-content"> <p> I think that it's important to remember the type of abstractions you highlight by showing that the Vistor design pattern is the same as sum types. I appreciate this post. </p> <p> When I read up on <a href="https://en.wikipedia.org/wiki/Entity_component_system">Entity component system</a>, I found it interesting how the pattern arrived from the need to be able to control memory. Perhaps the somewhat odd form of the Visitor pattern has arrived from the same limitations in some common languages? Perhaps there are other constructs that can be expressed more clearly using more modern patterns? </p> </div> <div class="comment-date">2020-08-23 8:59 UTC</div> </div> <div class="comment" id="f72ce0f3c59e474b869acded3b542422"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f72ce0f3c59e474b869acded3b542422">#</a></div> <div class="comment-content"> <p> Oskar, thank you for writing. I didn't know about entity component system. </p> <p> <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">This very article series</a> tries to identify various design patterns and how they relate to more fundamental constructs. Most likely you're already aware of that, so perhaps you meant something else by your questions. If you did, however, I can't glean what. </p> </div> <div class="comment-date">2020-08-25 15:54 UTC</div> </div> <div class="comment" id="c17476fcf1274a9e96ba249b79b87e3d"> <div class="comment-author"><a href="https://github.com/wallymathieu">Oskar Gewalli</a> <a href="#c17476fcf1274a9e96ba249b79b87e3d">#</a></div> <div class="comment-content"> <p> I was not aware of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">that article series</a>. The answer I'm looking for seems to be the identified subset. </p> </div> <div class="comment-date">2020-08-25 17:08 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Church-encoded payment types https://blog.ploeh.dk/2018/06/18/church-encoded-payment-types 2018-06-18T12:04:00+00:00 Mark Seemann <div id="post"> <p> <em>How to translate a domain-specific sum type into a Church-encoded C# API. An article for object-oriented developers.</em> </p> <p> This article is part of <a href="/2018/05/22/church-encoding">a series of articles about Church encoding</a>. In the previous articles, you've seen <a href="/2018/05/24/church-encoded-boolean-values">how to implement Boolean logic without Boolean primitives</a>, as well as <a href="/2018/05/28/church-encoded-natural-numbers">how to model natural numbers</a>, <a href="/2018/06/04/church-encoded-maybe">how to implement a Maybe container</a>, and <a href="/2018/06/11/church-encoded-either">how to implement an Either container</a>. Common to all four examples is that they're based on <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a> with exactly two mutually exclusive cases. </p> <p> <img src="/content/binary/binary-sum-types-and-matching-methods.png" alt="Three binary sum types, and their corresponding match methods."> </p> <p> You may already have noticed that all three translate to <code>Match</code> methods that take two arguments. The translation is so mechanical that you could automate it. Each case in a sum type becomes an argument in a <code>Match</code> method. In this article, you'll see an example of a domain-specific sum type with three cases, translated to Church-encoding. </p> <h3 id="62f77838e7724b8e8b7b3e5361ae13a7"> A payment type model in F# <a href="#62f77838e7724b8e8b7b3e5361ae13a7" title="permalink">#</a> </h3> <p> In <a href="/2016/11/28/easy-domain-modelling-with-types">a previous article</a> I described a particular business problem that was elegantly addressed with a discriminated union (sum type) in F#: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PaymentService</span>&nbsp;=&nbsp;{&nbsp;Name&nbsp;:&nbsp;<span style="color:teal;">string</span>;&nbsp;Action&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;} <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PaymentType</span>&nbsp;= |&nbsp;<span style="color:navy;">Individual</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentService</span> |&nbsp;<span style="color:navy;">Parent</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentService</span> |&nbsp;<span style="color:navy;">Child</span>&nbsp;<span style="color:blue;">of</span>&nbsp;originalTransactionKey&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;*&nbsp;paymentService&nbsp;:&nbsp;<span style="color:teal;">PaymentService</span></pre> </p> <p> In short, this model enables you to model various payments against a third-party payment service. An <em>individual</em> payment is, as the name implies, a single payment. A <em>parent</em> payment can be used to authorise a series of recurring, automated payments, for example to pay for a subscription. A <em>child</em> payment is one of those recurring payments; it must have a parent payment to authorise it, as automation means that no user interaction takes place. </p> <p> One task that is easily addressed with the above <code>PaymentType</code> discriminated union is that you can translate the data to JSON in a type-safe manner. The compiler will tell you whether or not you've handled all three cases. </p> <h3 id="bd590f5f56f7442ca2eddc89ffe07fea"> Auxiliary C# classes <a href="#bd590f5f56f7442ca2eddc89ffe07fea" title="permalink">#</a> </h3> <p> You can Church-encode <code>PaymentType</code> just like Boolean values, natural numbers, Maybe, and Either. Before you do that, however, you need to define the input types involved in each case. These are normal classes, although I prefer to make them immutable: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PaymentService</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;PaymentService(<span style="color:blue;">string</span>&nbsp;name,&nbsp;<span style="color:blue;">string</span>&nbsp;action) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Name&nbsp;=&nbsp;name; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Action&nbsp;=&nbsp;action; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Action&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ChildPaymentService</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ChildPaymentService( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;originalTransactionKey, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">PaymentService</span>&nbsp;paymentService) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.OriginalTransactionKey&nbsp;=&nbsp;originalTransactionKey; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.PaymentService&nbsp;=&nbsp;paymentService; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;OriginalTransactionKey&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>&nbsp;PaymentService&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> These are straightforward translations of the F# <code>PaymentService</code> record type, and the tuple associated with the <code>Child</code> case. In a real code base, I'd override <code>Equals</code> for both classes in order to turn them into proper <a href="https://en.wikipedia.org/wiki/Value_object">Value Objects</a>, but in order to keep the size of the code down, I omitted doing that here. </p> <h3 id="447c7cd04d0447ad99a034972134722b"> Church-encoded payment type <a href="#447c7cd04d0447ad99a034972134722b" title="permalink">#</a> </h3> <p> You can now translate the <code>PaymentType</code> F# discriminated union to a Church-encoded API in C#, starting with the interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;individual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;parent, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;child); }</pre> </p> <p> Since there's three cases in the sum type, that turns into a <code>Match</code> method with three arguments, each corresponding to one of the cases. As was also the case for the previous articles' <code>INaturalNumber</code>, <code>IMaybe&lt;T&gt;</code>, and <code>IEither&lt;L, R&gt;</code> interfaces, the data associated with each case is modelled as a function from the data to the generic return type <code>T</code>. </p> <p> Again, following the recipe implied by the previous examples, you should now add a concrete implementation of the <code>IPaymentType</code> interface for each case. It's natural to start with the first argument to the <code>Match</code> method, <em>individual:</em> </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Individual</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IPaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>&nbsp;paymentService; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Individual(<span style="color:#2b91af;">PaymentService</span>&nbsp;paymentService) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.paymentService&nbsp;=&nbsp;paymentService; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;individual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;parent, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;child) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;individual(paymentService); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Individual</code> class <a href="https://en.wikipedia.org/wiki/Adapter_pattern">adapts</a> a <code>PaymentService</code> value, which it passes as the argument to the <code>individual</code> function argument when <code>Match</code> is called. As you've seen in the previous articles, a particular implementation uses only one of the method arguments, so the two other arguments, <code>parent</code> and <code>child</code>, are simply ignored. </p> <p> The <em>parent</em> implementation is almost identical: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Parent</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IPaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>&nbsp;paymentService; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Parent(<span style="color:#2b91af;">PaymentService</span>&nbsp;paymentService) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.paymentService&nbsp;=&nbsp;paymentService; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;individual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;parent, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;child) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;parent(paymentService); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Parent</code> class also adapts a <code>PaymentService</code> value that it passes to a function when <code>Match</code> is called. The only difference is that it calls the <code>parent</code> function instead of the <code>individual</code> function argument. </p> <p> The third case is handled by the following <code>Child</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Child</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IPaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ChildPaymentService</span>&nbsp;childPaymentService; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Child(<span style="color:#2b91af;">ChildPaymentService</span>&nbsp;childPaymentService) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.childPaymentService&nbsp;=&nbsp;childPaymentService; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;individual, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">PaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;parent, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">ChildPaymentService</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;child) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;child(childPaymentService); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While the two other classes both adapt a <code>PaymentService</code> value, a <code>Child</code> object instead composes a <code>ChildPaymentService</code> value. When <code>Match</code> is called, it calls the <code>child</code> function argument with the composed value. </p> <h3 id="fbacd6c79ee04992a16bf6d81ecb2e3e"> Using the IPaymentType API <a href="#fbacd6c79ee04992a16bf6d81ecb2e3e" title="permalink">#</a> </h3> <p> One important feature that I originally had to implement was to translate a <em>payment type</em> value into a JSON document. For the purposes of this example, imagine that you can model the desired JSON document using this <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Object</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Action&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;StartRecurrent&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;TransactionKey&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> This is a mutable object because most .NET serialisation APIs require that the class in question has a parameterless constructor and writeable properties. Notice, however, that in order to demonstrate that all this code still doesn't rely on any primitive Boolean operators and such, the class' properties are defined as <code>IChurchBoolean</code> and <code>IMaybe&lt;string&gt;</code> values, as well as regular <code>string</code> values. </p> <p> Writing a method that translates any <code>IPaymentType</code> object into a <code>PaymentJsonModel</code> object is now straightforward: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span>&nbsp;ToJson(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IPaymentType</span>&nbsp;payment) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;payment.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;individual&nbsp;:&nbsp;ps&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;ps.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;ps.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent&nbsp;:&nbsp;ps&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;ps.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;ps.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">string</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child&nbsp;:&nbsp;cps&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentJsonModel</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;cps.PaymentService.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Action&nbsp;=&nbsp;cps.PaymentService.Action, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionKey&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">string</span>&gt;(cps.OriginalTransactionKey) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> Because the <code>Match</code> method takes three arguments, you have to supply a 'handler' function for each of them, and they all have to have the same return type. In this case they all return a new <code>PaymentJsonModel</code> object, so that requirement is fulfilled. All three lambda expressions simply copy over <code>Name</code> and <code>Action</code>, but they differ in the values they assign to <code>StartRecurrent</code> and <code>TransactionKey</code>. </p> <h3 id="465851b6143f4e75b1732bcfccdfcb28"> Tests <a href="#465851b6143f4e75b1732bcfccdfcb28" title="permalink">#</a> </h3> <p> In order to show you that it all works, here's a few examples I wrote as <a href="https://xunit.net">xUnit.net</a> tests: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;IndividualToJsonReturnsCorrectResult() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Individual</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>(<span style="color:#a31515;">&quot;MasterCard&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Pay&quot;</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.ToJson(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;MasterCard&quot;</span>,&nbsp;actual.Name); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Pay&quot;</span>,&nbsp;actual.Action); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(actual.StartRecurrent.ToBool()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(actual.TransactionKey.IsNothing().ToBool()); } [<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ParentToJsonReturnsCorrectResult() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Parent</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>(<span style="color:#a31515;">&quot;MasterCard&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Pay&quot;</span>)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.ToJson(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;MasterCard&quot;</span>,&nbsp;actual.Name); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Pay&quot;</span>,&nbsp;actual.Action); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(actual.StartRecurrent.ToBool()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(actual.TransactionKey.IsNothing().ToBool()); } [<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ChildToJsonReturnsCorrectResult() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Child</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChildPaymentService</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;12345&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PaymentService</span>(<span style="color:#a31515;">&quot;MasterCard&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Pay&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.ToJson(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;MasterCard&quot;</span>,&nbsp;actual.Name); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Pay&quot;</span>,&nbsp;actual.Action); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(actual.StartRecurrent.ToBool()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;12345&quot;</span>,&nbsp;actual.TransactionKey.Match(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;x&nbsp;=&gt;&nbsp;x)); }</pre> </p> <p> All three tests pass. </p> <h3 id="eceac90915cb40aab728fae7a36599d4"> Summary <a href="#eceac90915cb40aab728fae7a36599d4" title="permalink">#</a> </h3> <p> The major advantage of sum types in statically typed languages is that you can <em>make illegal states unrepresentable</em> (a maxim attributed to <a href="https://twitter.com/yminsky">Yaron Minsky</a>). Specifically, in the business example of payment types shown here, I need to be able to express that only three out of four combinations of <em>start recurrent</em> and <em>original transaction key</em> is legal. Specifically, I needed to express that the combination of <em>start recurrent = true</em> and the presence of a <em>transaction key</em> is illegal. Making such an illegal state unrepresentable is easy with a sum type, but as this article has shown, you can achieve the same goal in C#. </p> <p> With the API shown here, there's only three possible states (<code>Individual</code>, <code>Child</code>, and <code>Parent</code>). Notice that all three classes hide their data as <code>private</code> class fields, so the only way to extract that data is to call <code>Match</code>. The compiler will make sure that you handle all three cases, because you must supply a function for all three method arguments. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/ChurchEncoding/tree/a015b6f2ce77ca4fbc2fa50c73e03c2bf9686b0c">available on GitHub</a>. </p> <p> This article concludes the little series on how to use Church-encoding in C# to create sum types. You may, however, think that it doesn't really feel object-oriented, with its heavy reliance on function arguments (e.g. <code>Func&lt;PaymentService, T&gt;</code>). It turns out, though, that with only a few refactorings, you'll come to the realisation that what you've seen here is <a href="/2018/01/08/software-design-isomorphisms">isomorphic</a> to a classic design pattern. <a href="/2018/06/25/visitor-as-a-sum-type">Read on!</a> </p> <p> <strong>Next:</strong> <a href="/2019/07/29/church-encoded-rose-tree">Church-encoded rose tree</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Church-encoded Either https://blog.ploeh.dk/2018/06/11/church-encoded-either 2018-06-11T15:43:00+00:00 Mark Seemann <div id="post"> <p> <em>Programming languages don't have to have a built-in notion of error handling. You can implement sane error handling from first principles. An introduction for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2018/05/22/church-encoding">a series of articles about Church encoding</a>. In this series, you'll learn how to re-create various programming language features from first principles. In previous articles, you learned <a href="/2018/05/24/church-encoded-boolean-values">how to implement Boolean logic without Boolean primitives</a>, <a href="/2018/05/28/church-encoded-natural-numbers">how to model natural numbers</a>, as well as <a href="/2018/06/04/church-encoded-maybe">how to implement Maybe</a> (a <a href="/2015/11/13/null-has-no-type-but-maybe-has">type-safe alternative to null</a>). Through these examples, you'll learn how to model <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a> without explicit language support. </p> <p> <h3 id="6fd3652e6b9d462781a7d5191e32f6f0"> Error handling without exceptions <a href="#6fd3652e6b9d462781a7d5191e32f6f0" title="permalink">#</a> </h3> </p> <p> In a previous article, I've discussed how <a href="/2015/04/13/less-is-more-language-features">a language doesn't need to have built-in exceptions</a> in order to support composable and type-safe error handling. In fact, exceptions are noting but <a href="http://wiki.c2.com/?DontUseExceptionsForFlowControl">glorified GOTO statements</a>. A better approach is to use the <em>Either</em> abstraction, which enables you to model values that are either one or another thing. </p> <p> In <a href="https://fsharp.org">F#</a>, this type is known as <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/results">Result&lt;'T, 'TError&gt;</a>, while in <a href="https://www.haskell.org">Haskell</a> it's called <a href="https://hackage.haskell.org/package/base/docs/Data-Either.html">Either</a>. It enables you to model an outcome that is either something (like a success) or something else (such as an error). </p> <p> <a href="https://fsharpforfunandprofit.com">Scott Wlaschin</a> has already brilliantly described <a href="https://fsharpforfunandprofit.com/posts/recipe-part2">how this works in F#</a>, but the <code>Either</code> type can be used for error handling in Haskell in exactly the same way. When we use the terminology related to <em>either</em>, we distinguish between <em>left</em> and <em>right</em>. Typically, <em>right</em> is used to indicate success, via the pun that 'right' is 'correct'. </p> <p> <h3 id="72621c98299f4840b2ec880d254112a2"> Lambda calculus Either <a href="#72621c98299f4840b2ec880d254112a2" title="permalink">#</a> </h3> </p> <p> Church encoding is based on the <a href="https://en.wikipedia.org/wiki/Lambda_calculus">lambda calculus</a>, which defines a universal model of computation based entirely on functions (lambda expressions) and recursion. As far as I can tell, you can define <em>Either</em> in lambda calculus as an expression that takes two arguments, and where there's two fundamental 'implementations' of the contract: </p> <p> <pre> left = λa.λl.λr.l a right = λb.λl.λr.r b</pre> </p> <p> (I admit that I'm going out on a limb here, since I haven't found any source that puts either in the above form, so I'd appreciate feedback if I did it incorrectly.) </p> <p> The contract is that, similar to <em>Maybe</em>, the <code>l</code> function argument represents the <em>left</em> case, whereas the <code>r</code> argument represents the <em>right</em> case. Contrary to <em>Maybe</em>, both <em>l</em> and <em>r</em> are used as functions. (Everything in lambda calculus is a function, but we don't always use the arguments as the function that they are.) </p> <p> The <code>left</code> function is a function that takes three arguments (<code>a</code>, <code>l</code>, and <code>r</code>) and always returns <code>l a</code>. Recall that in lambda calculus, everything is a function, which includes <code>l</code> (and <code>r</code>). In other words, <code>left</code> unconditionally calls <code>l</code> with <code>a</code>, and that's the return value. </p> <p> The <code>right</code> function works like the <code>left</code> function, with the only difference that it always returns <code>r b</code>. </p> <p> The idea, as usual, is that you can partially apply <code>left</code> and <code>right</code>, by, for instance calling <code>left three</code> (where <code>three</code> is the lambda calculus representation of the number 3, as described in <a href="/2018/05/28/church-encoded-natural-numbers">the article on Church-encoded natural numbers</a>). Such a partially applied function is a function that still takes the two arguments <code>l</code> and <code>r</code>. </p> <p> The same is true if you partially apply <code>right</code> with a value, like <code>right one</code>. </p> <p> In both cases, you have a function of the form <code>λl.λr.[...]</code>. If you've been given such a function by an external source, you may not know if it's a <code>left</code> or a <code>right</code> expression, and that's the point. You must supply handlers (<code>l</code> and <code>r</code>) that cover all possible cases. </p> <p> In the lambda calculus, expressions are always curried, so instead of viewing <code>left</code> and <code>right</code> as functions with three arguments, you can view them as functions that take a single element (<code>a</code> or <code>b</code>) and return functions that takes two arguments. This agrees with Haskell's <code>Left</code> and <code>Right</code> data constructors: </p> <p> <pre>Prelude&gt; :t Left Left :: a -&gt; Either a b Prelude&gt; :t Right Right :: b -&gt; Either a b</pre> </p> <p> Haskell tells us that <code>Left</code> is a function that takes an <code>a</code> value and returns an <code>Either a b</code> value. Similarly, <code>Right</code> is a function that takes a <code>b</code> value as input, and returns an <code>Either a b</code> </p> <p> <h3 id="a680dfb8fbb1483391de0ac3fedcdcd6"> Church-encoded Either in C# <a href="#a680dfb8fbb1483391de0ac3fedcdcd6" title="permalink">#</a> </h3> </p> <p> Both lambda calculus and Haskell relies on currying and partial application to make the contract fit. In C#, as you've <a href="/2018/01/08/software-design-isomorphisms">previously</a> seen, you can instead define an interface and rely on class fields for the 'extra' function arguments. Since Church-encoded Either is represented by a function that takes two arguments, we'll once again define an interface with a single method that takes two arguments: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight); }</pre> </p> <p> The <code>Match</code> method takes two functions as arguments, one that handles the <em>left</em> case, and one that handles the <em>right</em> case. They correspond to the <code>l</code> and <code>r</code> variables in the above lambda expressions. The intent, as with other Church-encoded discriminated unions, is that when client code is given an <code>IEither&lt;L, R&gt;</code> object, it can only interact with that object by telling the <code>Match</code> method how to deal with both cases. Only one of the functions will be called, but at compile-time, you don't know which one. Both functions, however, must return a value of the generic type <code>T</code>, and that's how you can translate an <code>IEither&lt;L, R&gt;</code> object to a <code>T</code> value. </p> <p> Following the normal procedure for Church encoding, you must also supply two implementations of the <code>IEither&lt;L, R&gt;</code> interface: one for each case. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">L</span>&nbsp;left; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Left(<span style="color:#2b91af;">L</span>&nbsp;left) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.left&nbsp;=&nbsp;left; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;onLeft(left); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Left&lt;L, R&gt;</code> class is an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> of a value of the generic type <code>L</code> , making it appear as an <code>IEither&lt;L, R&gt;</code> object. </p> <p> It always calls the <code>onLeft</code> method argument with the adapted value <code>left</code>, while it ignores the <code>onRight</code> method argument. Since <code>onLeft</code> returns a <code>T</code> value, you can return the value produced by the function call. </p> <p> The <em>right</em> case is implemented in a similar fashion: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">R</span>&nbsp;right; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Right(<span style="color:#2b91af;">R</span>&nbsp;right) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.right&nbsp;=&nbsp;right; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;onRight(right); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Right&lt;L, R&gt;</code> class is the mirror image of <code>Left&lt;L, R&gt;</code>. Instead of adapting an <code>L</code> value, it adapts an <code>R</code> value. It implements <code>Match</code> by always calling <code>onRight</code> with the <code>right</code> value, which, again, produces a <code>T</code> value that can be immediately returned. </p> <p> Notice that for both implementations, the adapted values <code>left</code> and <code>right</code> are <code>private</code> class fields not exposed as public members. The only way you, as a caller, can potentially extract these values is by calling <code>Match</code>, and that forces you to explicitly deal with both cases. </p> <p> Here's an example of using the API: </p> <p> <pre>&gt; <span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42); &gt; e.Match(s =&gt; s.Length % 2 == 0, i =&gt; i % 2 == 0) true</pre> </p> <p> I've deliberately declared <code>e</code> as a an <code>IEither&lt;string, int&gt;</code> in order to highlight the scenario where, as a client developer, you're often given a value of such a type, and you don't know if it's a <em>left</em> or a <em>right</em> value. Had I, instead, used the <code>var</code> keyword, the compiler would have detected that <code>e</code> is, really, a <code>Right&lt;string, int&gt;</code> variable. You may consider this choice artificial, but the point I'm trying to get across is that, when writing client code, you're often given a polymorphic value, and you don't know the concrete type of the value. According to the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>, your client code must be able to deal with any subtype without changing the correctness of the system. In the case of an Either value, the way you deal with all subtypes is by supplying handlers for both cases to the <code>Match</code> method. </p> <p> In the above example, the return value is <code>true</code> because <code>42</code> is an even number. If, instead, the <code>e</code> object is a <em>left</em> case containing the string <code>"foo"</code>, the return value is <code>false</code> because the length of <code>"foo"</code> is <em>3</em> - an odd number: </p> <p> <pre>&gt; <span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>); &gt; e.Match(s =&gt; s.Length % 2 == 0, i =&gt; i % 2 == 0) false</pre> </p> <p> Notice that the <code>e.Match</code> method call is the same in both examples; the <code>onLeft</code> and <code>onRight</code> functions are the same in both cases. The results differ because the input values represent different cases. </p> <p> If you've been following the overall series on Church encoding, you may think that it's cheating to use C#'s built-in <code>string</code> and <code>int</code> data types, but nothing prevents us from sticking to the data types we've built from scratch: </p> <p> <pre>&gt; <span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">IChurchBoolean</span>,&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&gt;&nbsp;e; &gt; e&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">IChurchBoolean</span>,&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&gt;(<span style="color:#2b91af;">NaturalNumber</span>.Seven); &gt; e.Match(b&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(b),&nbsp;n&nbsp;=&gt;&nbsp;n.IsEven()) ChurchFalse { } &gt; e&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">IChurchBoolean</span>,&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>()); &gt; e.Match(b&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(b),&nbsp;n&nbsp;=&gt;&nbsp;n.IsEven()) ChurchNot(ChurchFalse)</pre> </p> <p> For both the <em>left</em> and the <em>right</em> case, the <code>Match</code> inverts the Boolean expression if it's a <em>left</em> case, and evaluates if the number is even if it's a <em>right</em> case. In the first example, the return value is a <code>ChurchFalse</code> object because <em>7</em> is odd. In the second example, the return value is a <code>ChurchNot</code> object containing a <code>ChurchFalse</code> object (in other words, <em>true</em>), because the negation of <em>false</em> is <em>true</em>. </p> <p> <h3 id="f09ad4ad69ba47b6b9f13b0fcd99d6fe"> Either instead of exceptions <a href="#f09ad4ad69ba47b6b9f13b0fcd99d6fe" title="permalink">#</a> </h3> </p> <p> You can use Either to signal the success or failure of an operation. By convention, the <em>right</em> case is used to signal success, so, by elimination, <em>left</em> means failure. You can signal errors in numerous ways, e.g. by using <code>enum</code> values, but another common strategy is to simply use string values. </p> <p> Consider the following example. You receive a collection of values, where each element represents a vote for that element. For example, the list <em>Sandra, Zoey, Sandra</em> indicates two votes for <em>Sandra</em>, and one for <em>Zoey</em>. You need to write a method that returns the winner of a vote, but at least two distinct errors are possible: the input collection is empty, or there's a tie. </p> <p> You can model the error cases with an <code>enum</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">enum</span>&nbsp;<span style="color:#2b91af;">VoteError</span> { &nbsp;&nbsp;&nbsp;&nbsp;Empty&nbsp;=&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;Tie }</pre> </p> <p> This enables you to write a method to find the winners, with an explicit Either return type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;FindWinner&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;votes) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;countedVotes&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;v&nbsp;<span style="color:blue;">in</span>&nbsp;votes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;v&nbsp;<span style="color:blue;">by</span>&nbsp;v&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;count&nbsp;=&nbsp;g.Count() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">orderby</span>&nbsp;count&nbsp;<span style="color:blue;">descending</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;Vote&nbsp;=&nbsp;g.Key,&nbsp;Count&nbsp;=&nbsp;count&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;c&nbsp;=&nbsp;countedVotes.Take(2).Count(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(c&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">VoteError</span>.Empty); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x0&nbsp;=&nbsp;countedVotes.ElementAt(0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(c&nbsp;==&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(x0.Vote); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x1&nbsp;=&nbsp;countedVotes.ElementAt(1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Equals(x0.Count,&nbsp;x1.Count)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">VoteError</span>.Tie); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(x0.Vote); }</pre> </p> <p> Notice that the return type of the <code>FindWinner</code> method is <code>IEither&lt;VoteError, T&gt;</code>; either you get a <code>VoteError</code> value, or you get a <code>T</code> value, but any client code doesn't know which it'll be, so it must handle both cases. </p> <p> The method uses a C# query expression to group, count, and order the votes. If there's no elements, the return value is a <em>left</em> value containing <code>VoteError.Empty</code>. If there's only a single vote group (e.g. if the votes where all for <em>Sandra</em>), that value is returned in a <em>right</em> case. Otherwise, if the two highest ranked votes have the same count, a <em>left</em> value is returned containing <code>VoteError.Tie</code>. Finally, in all other cases, the highest voted element is returned in a <em>right</em> case. </p> <p> Here's some examples in <em>C# Interactive:</em> </p> <p> <pre>&gt; FindWinner&lt;<span style="color:blue;">int</span>&gt;() Left&lt;VoteError, int&gt;(Empty) &gt; FindWinner(1, 2, 3, 1, 4, 2) Left&lt;VoteError, int&gt;(Tie) &gt; FindWinner(<span style="color:#a31515;">&quot;Sandra&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Zoey&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Sandra&quot;</span>) Right&lt;VoteError, string&gt;("Sandra")</pre> </p> <p> Instead of throwing two different types of exceptions on invalid input, the <code>FindWinner</code> method handles invalid input as <em>left</em> cases, and valid input as the <em>right</em> case. You can do that consistently, and thereby eliminate the need for exceptions. Errors are, instead, reported as <em>left</em> values. </p> <p> <h3 id="fb2ac51484e14b738c2e4af5278912e3"> Summary <a href="#fb2ac51484e14b738c2e4af5278912e3" title="permalink">#</a> </h3> </p> <p> In this article, you saw how it's possible to define the <em>Either</em> container from first principles, using nothing but functions (and, for the C# examples, interfaces and classes in order to make the code easier to understand for object-oriented developers). </p> <p> The code shown in this article is <a href="https://github.com/ploeh/ChurchEncoding/tree/49b0646c38d15648e9145052d3a04954f54c29f8">available on GitHub</a>. </p> <p> <a href="/2018/03/26/the-maybe-functor">Like Maybe</a>, you can also make Either a functor. This'll enable you to compose various error-producing functions in a sane manner. </p> <p> Church-encoding enables you to model sum types as functions. So far in this article series, you've seen how to model Boolean values, natural numbers, Maybe, and Either. Common to all four examples is that the data type in question consists of two mutually exclusive cases. This is the reason they're all modelled as methods that take two arguments. What happens if, instead of two, you have <em>three</em> mutually exclusive cases? Read on. </p> <p> <strong>Next:</strong> <a href="/2018/06/18/church-encoded-payment-types">Church-encoded payment types</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="e66e61ce8a6f4543ae44a6a55945fd3a"> <div class="comment-author">Ciprian Vilcan <a href="#e66e61ce8a6f4543ae44a6a55945fd3a">#</a></div> <div class="comment-content"> Hi Mark,<br> <br> All sources in which I've come accross Either seem to describe it as you did, having two type parameters: left and right.<br> But since it is simply a discriminated/tagged union, why are Eithers limited to two parameters?<br> <br> Say, in F#, it would make perfect sense to have a discriminated union called PaymentType = CreditCard | DebitCard | Cash. <br> Is there any convention in place that suggests against having something like that, but instead using <br> <code>public sealed class PaymentType : IEither&lt;CreditCard, DebitCard, Cash&gt; ?</code> <br> Yes, you could theoretically nest Eithers and get the same result, but that would be awkward both in definition and usage in C# or other languages that don't define such constructs naturally.<br> <code>public sealed class PaymentType : IEither&lt;CreditCard, IEither&lt;DebitCard, Cash&gt;&gt;</code> </div> <div class="comment-date">2019-01-07 13:48 UTC</div> </div> <div class="comment" id="b001f5b4b4cb4b09a13d14063d4c542e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b001f5b4b4cb4b09a13d14063d4c542e">#</a></div> <div class="comment-content"> <p> Ciprian, thank you for writing. Either is <em>a particular</em> discriminated union; by definition it has two type parameters. In F# it's called <code>Result&lt;'T,'TError&gt;</code> and defined as: </p> <p> <pre>type Result&lt;'T,'TError&gt; = | Ok of ResultValue:'T | Error of ErrorValue:'TError</pre> </p> <p> Notice that in this defintion, the 'right' result is to the left, and the error is to the right, which is opposite of the way of the <em>either</em> convention. </p> <p> If you need another type with three mutually exclusive cases, then Either is a poor fit for that (although one <em>can</em> nest them, as you suggest). You can, however, still Church-encode such a type. The <a href="/2018/06/18/church-encoded-payment-types">next article in this article series</a> contains an example of a Church-encoding of a discriminated union with three, instead of two, cases. By coincidence, this type is also called <em>payment type</em>. The cases are, however, not the same as those you suggest, since it models a different scenario. </p> <p> The <code>IEither&lt;L, R&gt;</code> interface shown in this article is not meant to be implemented by any other classes than <code>Left</code> and <code>Right</code>. The only reason these types are <code>public</code> is because this article shows you how the sausage is made, so to speak. If one ever were to put such a type into a reusable library, I think an alternative implementation like the following would be more appropriate: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IEither</span>&nbsp;imp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Either(<span style="color:#2b91af;">IEither</span>&nbsp;imp) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.imp&nbsp;=&nbsp;imp; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;CreateLeft(<span style="color:#2b91af;">L</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>(value)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;CreateRight(<span style="color:#2b91af;">R</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>(value)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;imp.Match(onLeft,&nbsp;onRight); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(imp,&nbsp;other.imp); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;imp.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IEither</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Left</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IEither</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">L</span>&nbsp;left; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Left(<span style="color:#2b91af;">L</span>&nbsp;left) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.left&nbsp;=&nbsp;left; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;onLeft(left); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Left</span>&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(left,&nbsp;other.left); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;left.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Right</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IEither</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">R</span>&nbsp;right; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Right(<span style="color:#2b91af;">R</span>&nbsp;right) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.right&nbsp;=&nbsp;right; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;onRight(right); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(obj&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Right</span>&nbsp;other)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(right,&nbsp;other.right); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;right.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Apart from the object type itself, you're also going to need some static methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;Left&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:#2b91af;">L</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;.CreateLeft(value); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;Right&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:#2b91af;">R</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;.CreateRight(value); }</pre> </p> <p> Additional extension methods like the <code>SelectBoth</code> method described in <a href="/2019/01/07/either-bifunctor">Either bifunctor</a> can be still implemented based on <code>Match</code>. </p> <p> This API is much more locked down, so should leave little doubt about how it's supposed to be used. Apart from the methods inherited from <code>System.Object</code>, this <code>Either&lt;L, R&gt;</code> class only exposes one public method: <code>Match</code>. It's also sealed, and its constructor is marked <code>private</code>, so not only can't you inherit from it, you also can't derive classes from <code>Left</code> or <code>Right</code>. </p> <p> Usage is similar to before; for example, here's the above <code>FindWinner</code> method, changed to consume the encapsulated <code>Either</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;FindWinner&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;votes) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;countedVotes&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;v&nbsp;<span style="color:blue;">in</span>&nbsp;votes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">group</span>&nbsp;v&nbsp;<span style="color:blue;">by</span>&nbsp;v&nbsp;<span style="color:blue;">into</span>&nbsp;g &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;count&nbsp;=&nbsp;g.Count() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">orderby</span>&nbsp;count&nbsp;<span style="color:blue;">descending</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;Vote&nbsp;=&nbsp;g.Key,&nbsp;Count&nbsp;=&nbsp;count&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;c&nbsp;=&nbsp;countedVotes.Take(2).Count(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(c&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Either</span>.Left&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">VoteError</span>.Empty); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x0&nbsp;=&nbsp;countedVotes.ElementAt(0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(c&nbsp;==&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Either</span>.Right&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(x0.Vote); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x1&nbsp;=&nbsp;countedVotes.ElementAt(1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Equals(x0.Count,&nbsp;x1.Count)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Either</span>.Left&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">VoteError</span>.Tie); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Either</span>.Right&lt;<span style="color:#2b91af;">VoteError</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(x0.Vote); }</pre> </p> <p> The only difference is that it no longer explicitly creates <code>new</code> instances of <code>Left</code> or <code>Right</code>, but instead uses the static factories. </p> <p> If I were to publish a reusable C# library with Maybe, Either, and similar types, I'd design them like this so as to leave absolutely no doubt about the intended usage. </p> </div> <div class="comment-date">2019-01-07 18:12 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Church-encoded Maybe https://blog.ploeh.dk/2018/06/04/church-encoded-maybe 2018-06-04T10:08:00+00:00 Mark Seemann <div id="post"> <p> <em>Programming languages don't have to have a built-in notion of null values. Missing or optional values can be created from first principles. An introduction for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2018/05/22/church-encoding">a series of articles about Church encoding</a>. In this series, you'll learn how to re-create various programming language features from first principles. In previous articles, you learned <a href="/2018/05/24/church-encoded-boolean-values">how to implement Boolean logic without Boolean primitives</a>, as well as <a href="/2018/05/28/church-encoded-natural-numbers">how to model natural numbers</a>. Through these examples, you'll learn how to model <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a> without explicit language support. </p> <h3 id="2e26f3d3d84a43b790f7ee5e89dedbff"> The billion-dollar mistake <a href="#2e26f3d3d84a43b790f7ee5e89dedbff" title="permalink">#</a> </h3> <p> All mainstream programming languages have a built-in notion of <em>null</em>: a value that isn't there. There's nothing wrong with the concept; you often run into situations where you need to return a value, but in certain cases, you'll have nothing to return. Division by zero would be one example. Attempting to retrieve the first element from an empty collection would be another. </p> <p> Unfortunately, for fifty years, we've been immersed in environments where null references have been the dominant way to model the absence of data. This, despite the fact that even <a href="https://en.wikipedia.org/wiki/Tony_Hoare">Sir Antony Hoare</a>, the inventor of null references, has publicly called it his <em>billion-dollar mistake</em>. </p> <p> You can, however, model the potential absence of data in saner ways. <a href="https://www.haskell.org">Haskell</a>, for example, has no built-in null support, but it does include a built-in <a href="/2018/03/26/the-maybe-functor">Maybe</a> type. In Haskell (as well as in <a href="http://fsharp.org">F#</a>, where it's called <code>option</code>), <code>Maybe</code> is defined as a sum type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Maybe&nbsp;a&nbsp;=&nbsp;Nothing&nbsp;|&nbsp;Just&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Ord</span>)</pre> </p> <p> If you're not familiar with Haskell syntax, this is a type declaration that states that the <a href="https://en.wikipedia.org/wiki/Parametric_polymorphism">parametrically polymorphic</a> (AKA <em>generic</em>) data type <code>Maybe</code> is inhabited by <code>Just</code> values that contain other values, plus the constant <code>Nothing</code>. </p> <p> This article series, however, examines how to implement sum types with Church encoding. </p> <h3 id="b2a73bd4fc25450a8ba2e5e90f3319dc"> Lambda calculus maybe <a href="#b2a73bd4fc25450a8ba2e5e90f3319dc" title="permalink">#</a> </h3> <p> Church encoding is based on the <a href="https://en.wikipedia.org/wiki/Lambda_calculus">lambda calculus</a>, which defines a universal model of computation based entirely on functions (lambda expressions) and recursion. In lambda calculus, the contract of <em>Maybe</em> is defined as an expression that takes two arguments. <a href="http://programmable.computer/posts/church_encoding.html">There's two fundamental 'implementations' of the contract</a>: </p> <p> <pre>nothing = λn.λj.n just = λx.λn.λj.j x</pre> </p> <p> The contract is that the first function argument (<code>n</code>) represents the <em>nothing</em> case, whereas the second argument (<code>j</code>) represents the <code>just</code> case. </p> <p> The <code>nothing</code> function is a lambda expression that takes two arguments (<code>n</code> and <code>j</code>), and always returns the first, left-most argument (<code>n</code>). </p> <p> The <code>just</code> function is a lambda expression that takes three arguments (<code>x</code>, <code>n</code>, and <code>j</code>), and always returns <code>j x</code>. Recall that in the lambda calculus, everything is a function, including <code>j</code>, so <code>j x</code> means that the function <code>j</code> is called with the argument <code>x</code>. </p> <p> A few paragraphs above, I wrote that the contract of <em>maybe</em> is modelled as an expression that takes two arguments, yet <code>just</code> takes three arguments. How does that fit? </p> <p> In the lambda calculus, expressions are always curried, so instead of viewing <code>just</code> as a function with three arguments, you can view it as a function that takes a single element (<code>x</code>) and returns a function that takes two arguments. This agrees with Haskell's <code>Just</code> data constructor: </p> <p> <pre>Prelude&gt; :t Just Just :: a -&gt; Maybe a</pre> </p> <p> Haskell tells us that <code>Just</code> is a function that takes an <code>a</code> value (corresponding to <code>x</code> in the above <code>just</code> lambda expression) and returns a <code>Maybe a</code> value. </p> <h3 id="01738cd9f37e4067947379b2aaf1735c"> Church-encoded Maybe in C# <a href="#01738cd9f37e4067947379b2aaf1735c" title="permalink">#</a> </h3> <p> Both lambda calculus and Haskell rely on currying and partial application to make the contract fit. In C#, as you've <a href="/2018/01/08/software-design-isomorphisms">previously</a> seen, you can instead define an interface and rely on class fields for the 'extra' function arguments. Since Church-encoded Maybe is represented by a function that takes two arguments, we'll once again define an interface with a single method that takes two arguments: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">TResult</span>&nbsp;nothing,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;just); }</pre> </p> <p> In the first article, about Church-encoded Boolean values, you saw how two mutually exclusive values could be modelled as a method that takes two arguments. Boolean values are simply constants (<em>true</em> and <em>false</em>), where the next example (natural numbers) included a case where one case (<em>successor</em>) contained data. In that example, however, the data was statically typed as another <code>INaturalNumber</code> value. In the current <code>IMaybe&lt;T&gt;</code> example, the data contained in the <em>just</em> case is generic (it's of the type <code>T</code>). </p> <p> Notice that there's two levels of generics in play. <code>IMaybe&lt;T&gt;</code> itself is a container of the generic type <code>T</code>, whereas <code>Match</code> enables you to convert the container into the rank-2 polymorphic type <code>TResult</code>. </p> <p> Once more, the contract of <code>IMaybe&lt;T&gt;</code> is that the first, left-hand argument represents the <em>nothing</em> case, whereas the second, right-hand argument represents the <em>just</em> case. The <em>nothing</em> implementation, then, is similar to the previous <code>ChurchTrue</code> and <code>Zero</code> classes: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">TResult</span>&nbsp;nothing,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;just) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;nothing; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Again, the implementation unconditionally returns <code>nothing</code> while ignoring <code>just</code>. You may, though, have noticed that, as is appropriate for Maybe, <code>Nothing&lt;T&gt;</code> has a distinct type. In other words, <code>Nothing&lt;string&gt;</code> doesn't have the same type as <code>Nothing&lt;int&gt;</code>. This is not only 'by design', but is a fundamental result of how we define <em>Maybe</em>. The code simply wouldn't compile if you tried to remove the type argument from the class. This is in contrast to <a href="/2015/11/13/null-has-no-type-but-maybe-has">C# null, which has no type</a>. </p> <p> You implement the <em>just</em> case like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Just(<span style="color:#2b91af;">T</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">TResult</span>&nbsp;nothing,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;just) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;just(value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> According to the contract, <code>Just&lt;T&gt;</code> ignores <code>nothing</code> and works exclusively with the <code>just</code> function argument. Notice that the <code>value</code> class field is <code>private</code> and not exposed as a public member. The only way you, as a caller, can potentially extract the value is by calling <code>Match</code>. </p> <p> Here are some examples of using the API: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;().Match(nothing:&nbsp;<span style="color:#a31515;">&quot;empty&quot;</span>,&nbsp;just:&nbsp;g&nbsp;=&gt;&nbsp;g.ToString()) "empty" &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">int</span>&gt;(42).Match(nothing:&nbsp;<span style="color:#a31515;">&quot;empty&quot;</span>,&nbsp;just:&nbsp;i&nbsp;=&gt;&nbsp;i.ToString()) "42" &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">int</span>&gt;(1337).Match(nothing:&nbsp;0,&nbsp;just:&nbsp;i&nbsp;=&gt;&nbsp;i) 1337</pre> </p> <p> Notice that the third example shows how to extract the value contained in a <code>Nothing&lt;int&gt;</code> object without changing the output type. All you have to do is to supply a 'fall-back' value that can be used in case the value is <em>nothing</em>. </p> <h3 id="8af13110cd88473c97cc1cee28dd7ee9"> Maybe predicates <a href="#8af13110cd88473c97cc1cee28dd7ee9" title="permalink">#</a> </h3> <p> You can easily implement the standard Maybe predicates <code>IsNothing</code> and <code>IsJust</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsNothing&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;m) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;m.Match&lt;<span style="color:#2b91af;">IChurchBoolean</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing&nbsp;:&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(),&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just&nbsp;:&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>()); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsJust&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;m) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;m.Match&lt;<span style="color:#2b91af;">IChurchBoolean</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing&nbsp;:&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just&nbsp;:&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>()); }</pre> </p> <p> Here, I arbitrarily chose to implement <code>IsJust</code> 'from scratch', but I could also have implemented it by negating the result of calling <code>IsNothing</code>. Once again, notice that the predicates are expressed in terms of Church-encoded Boolean values, instead of the built-in <code>bool</code> primitives. </p> <h3 id="76561f015e4d4da0a590ba247eb4500d"> Functor <a href="#76561f015e4d4da0a590ba247eb4500d" title="permalink">#</a> </h3> <p> From Haskell (and F#) we know that Maybe is a <a href="/2018/03/22/functors">functor</a>. In C#, you turn a container into a functor by implementing an appropriate <code>Select</code> method. You can do this with <code>IMaybe&lt;T&gt;</code> as well: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(selector(x))); }</pre> </p> <p> Notice that this method turns an <code>IMaybe&lt;T&gt;</code> object into an <code>IMaybe&lt;TResult&gt;</code> object, using nothing but the <code>Match</code> method. This is possible because <code>Match</code> has a generic return type; thus, among other types of values, you can make it return <code>IMaybe&lt;TResult&gt;</code>. </p> <p> When <code>source</code> is a <code>Nothing&lt;T&gt;</code> object, <code>Match</code> returns the object in the <em>nothing</em> case, which here becomes a new <code>Nothing&lt;TResult&gt;</code> object. </p> <p> When <code>source</code> is a <code>Just&lt;T&gt;</code> object, <code>Match</code> invokes <code>selector</code> with the value contained in the <em>just</em> object, packages the result in a new <code>Just&lt;TResult&gt;</code> object, and returns it. </p> <p> Because the <code>Select</code> method has the correct signature, you can use it with query syntax, as well as with normal method call syntax: </p> <p> <pre><span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;m&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">int</span>&gt;(42); <span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;i.ToString();</pre> </p> <p> This example simply creates a <em>just</em> value containing the number <code>42</code>, and then maps it to a string. Another way to write the same expression would be with method call syntax: </p> <p> <pre><span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;m&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">int</span>&gt;(42); <span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;actual&nbsp;=&nbsp;m.Select(i&nbsp;=&gt;&nbsp;i.ToString());</pre> </p> <p> In both cases, the result is a <em>just</em> case containing the string <code>"42"</code>. </p> <h3 id="4bdd2b27f1724e3198b95d86642c8a6c"> Summary <a href="#4bdd2b27f1724e3198b95d86642c8a6c" title="permalink">#</a> </h3> <p> In this article, you saw how it's possible to define the <em>Maybe</em> container from first principles, using nothing but functions (and, for the C# examples, interfaces and classes in order to make the code easier to understand for object-oriented developers). </p> <p> The code shown in this article is <a href="https://github.com/ploeh/ChurchEncoding/tree/8d1e7501f486351e748646c915f0bd334332e386">available on GitHub</a>. </p> <p> Church-encoding enables you to model sum types as functions. So far in this article series, you've seen how to model Boolean values, natural numbers, and Maybe. Common to all three examples is that the data type in question consists of two mutually exclusive cases. There's at least one more interesting variation on that pattern. </p> <p> <strong>Next:</strong> <a href="/2018/06/11/church-encoded-either">Church-encoded Either</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="219468451d9a4af99be169a73af898bd"> <div class="comment-author"><a href="https://github.com/adleatherwood">Anthony Leatherwood</a> <a href="#219468451d9a4af99be169a73af898bd">#</a></div> <div class="comment-content"> It's probably not your favorite thing to do anymore, but I thank you so much for continuing to provide C# examples for these concepts. It's invaluable for programmers wishing to adopt these concepts into the OOP languages they work with for a living. It's also a tremendous aid in briding the gap of understanding between OOP and FP. </div> <div class="comment-date">2018-06-04 17:57 UTC</div> </div> <div class="comment" id="0d95b7b291b041a89fca0aa942c56189"> <div class="comment-author"><a href="https://ramblingamblings.wordpress.com/">Nathaniel Bond</a> <a href="#0d95b7b291b041a89fca0aa942c56189">#</a></div> <div class="comment-content"> <p> Hey Mark, thanks so much for writing. Coming at this some 3 years later but it's been an insightful introduction to functional programming. </p> <p> I've been trying Church encoding and algebraic data types in general out for a little while now, and had a question about a potential alternate implementation of <code>Select</code> and friends. I had been reviewing <a href="https://blog.ploeh.dk/2018/05/24/church-encoded-boolean-values/">Church-encoded Booleans</a> and really appreciated the way the <code>And</code>/<code>Or</code>/<code>Not</code> <code>Match</code> implementations managed to defer executing their operator's actual operation until needed. So I figured there might be a way to do something similar with <code>Select</code>. </p> <p> Here's what I came up with: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaybeSelected</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TSelectResult</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TSelectResult</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TSelectResult</span>&gt;&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaybeSelected( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TSelectResult</span>&gt;&nbsp;selector) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.source&nbsp;=&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.selector&nbsp;=&nbsp;selector; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">TResult</span>&nbsp;nothing,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">TSelectResult</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;just) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;nothing, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;x&nbsp;=>&nbsp;just(selector(x))); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Which resulted in the following <code>Select</code> refactoring: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaybeSelected</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;(source,&nbsp;selector); }</pre> </p> <p> After ensuring the refactor passed unit tests, I went on to the monad: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaybeFlattened</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaybeFlattened(<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;source) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.source&nbsp;=&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">TResult</span>&nbsp;nothing,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;just) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;nothing, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;x&nbsp;=>&nbsp;x.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;nothing, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;just)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Which resulted in a similar refactoring of <code>Flatten</code>. I'm pretty happy with the results, especially in the case of <code>Select</code> not needing to execute its <code>selector</code> until <code>Match</code> is called. This seems to resemble the behavior of <code>IEnumerable</code> not actually enumerating until something like <code>ToList</code> is called. The unit tests pass, so I have some demonstration of correct behavior, though I suppose being new to this I may have failed to understand something more essential than the demonstrated behavior they exercise. </p> <p> That said, I am more interested in the nature of the refactoring itself. It seems to be a kind of isomorphism similar to that of <a href="https://blog.ploeh.dk/2018/01/29/argument-list-isomorphisms/">argument lists</a>, but extending beyond the method's parameters to including its behavior as well. In general, the process involved taking, e.g., the <code>Select</code> method's parameters and making corresponding fields in the new <code>MaybeSelected</code> class; and then taking the method's implementation and shuffling it off to some method in the new class (<code>Match</code>) that would eventually perform the original execution. I like seeing how the <code>x => x</code> "phrase" lines up between the original <code>Flatten</code> and <code>MaybeFlattened.Match</code>. So going beyond "it works," would you mind sharing your thoughts on what kind of animal we've got here? </p> P.S. &mdash; I have included the above changes in <a href="https://github.com/nabond251/ChurchEncoding/tree/feature/defurred-cat">this fork</a> of the post's repository. <p> </p> </div> <div class="comment-date">2021-07-02 21:03 UTC</div> </div> <div class="comment" id="b2a77193bcfa48ebb92033fb1dbae2e7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b2a77193bcfa48ebb92033fb1dbae2e7">#</a></div> <div class="comment-content"> <p> Nathaniel, thank you for writing. If you're new to this, I tip my hat to you. As far as I can tell, you've arrived at the insight that lazy evaluation seems to be a 'toggle' you can turn on or off without affecting the lawfulness of the underlying concepts. </p> <p> I admit that I haven't done a full, formal analysis of your approach, but it looks completely legit to me. </p> <p> The reason I feel confident that this is still a lawful Maybe monad is that Haskell is lazy by default. I've learned much of what I know about functors and monads from Haskell. In Haskell, most of the monads are lazy, and you explicitly have to opt in if you want strict evaluation (in Haskell, the opposite of lazy is usually called <em>strict</em>, although <a href="https://wiki.haskell.org/Performance/Strictness">it's a little more complicated than that</a>.) In most other languages (C# included), expressions are usually eagerly evaluated, and you have to explicitly opt into laziness. </p> <p> In those languages, you can use something like <code>Lazy&lt;T&gt;</code> to <a href="/2018/12/17/the-lazy-applicative-functor">form an applicative functor</a> (and monad, but I haven't written that article). You can also show that <a href="/2018/09/10/the-lazy-functor">it obeys the functor laws</a>. It also obeys the applicative functor and monad laws, but again, I haven't written those articles... </p> <p> What you've done here is slightly different, since you haven't explicitly used <code>Lazy&lt;T&gt;</code>, but I'd expect your variation to be isomorphic to <code>Lazy&lt;Maybe&lt;T&gt;&gt;</code> (where <code>Maybe&lt;T&gt;</code> is an eagerly evaluated version of Maybe). If you're interested, you may consider this exercise: </p> <p> <em>Write two functions that convert back and forth between the two representations, and demonstrate that the two implementations behave in the same way.</em> </p> <p> In general, knowing how to enable lazy evaluation can be a useful tool to add to your toolkit, but as all Haskellers have learned, it comes with a disadvantage, too. Instead of evaluating values right away, you have to pass around <em>expressions</em>. Such expressions containing other expressions are called <em>thunks</em>, and they can grow quite large. In Haskell, which relies heavily on this mechanism, if you aren't careful, you can build up thunks that eat up all available memory. </p> <p> In reality, I don't experience this to be a big problem as long as one stays aware of it, but the point is that you can't just forget about it. Laziness isn't a free ride to better performance. You trade more efficient <em>computation</em> for more inefficient <em>memory use</em>. </p> </div> <div class="comment-date">2021-07-06 6:10 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Church-encoded natural numbers https://blog.ploeh.dk/2018/05/28/church-encoded-natural-numbers 2018-05-28T08:24:00+00:00 Mark Seemann <div id="post"> <p> <em>Natural numbers don't have to be built into programming languages. An introduction for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2018/05/22/church-encoding">a series of articles about Church encoding</a>. The previous article, about <a href="/2018/05/24/church-encoded-boolean-values">Church-encoding of Boolean values</a>, concluded with the question: <em>how do you determine whether an integer is even or odd?</em> </p> <p> That sounds easy, but turns out to be more complicated that you might think at first glance. </p> <h3 id="47669f1d5f3c48968e13474ec60a86a0"> Built-in options <a href="#47669f1d5f3c48968e13474ec60a86a0" title="permalink">#</a> </h3> <p> How would you normally check whether a number is even? In some languages, like <a href="https://www.haskell.org">Haskell</a>, it's built into the base library: </p> <p> <pre>Prelude&gt; even 1337 False Prelude&gt; even 42 True</pre> </p> <p> In C#, surprisingly, I don't think it's built-in, but it's easy to implement a method to answer the question: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsEven(<span style="color:blue;">this</span>&nbsp;<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; }</pre> </p> <p> You could implement an <code>IsOdd</code> method either by using the <code>!=</code> operator instead of <code>==</code>, but otherwise copy the implementation of <code>IsEven</code>; or, alternatively, call <code>IsEven</code> and negate the result. </p> <p> This works fine in normal C# code, but in this article, the agenda is different. We're investigating how programming with the <a href="/2018/05/24/church-encoded-boolean-values">previous article's</a> <code>IChurchBoolean</code> API would look. The above built-in options use Boolean language primitives, so that's not really instructive. </p> <h3 id="313b328dd4f2495b852ef2a7aae6b130"> Boolean conversions <a href="#313b328dd4f2495b852ef2a7aae6b130" title="permalink">#</a> </h3> <p> It's easy to convert between Church-encoded Boolean values and built-in Boolean values. For reasons I'll explain shortly, I still don't think that's instructive in this particular context, but for good measure I'll cover how to do it. </p> <p> A method like the above <code>IsEven</code> returns <code>bool</code>. If you, instead, want an <code>IChurchBoolean</code>, you can use this simple conversion method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;ToChurchBoolean(<span style="color:blue;">this</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;b) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(b) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(); }</pre> </p> <p> Alternatively, you can also use the ternary operator, but an ugly cast is necessary to make the C# compiler happy: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;ToChurchBoolean(<span style="color:blue;">this</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;b) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;b&nbsp;?&nbsp;(<span style="color:#2b91af;">IChurchBoolean</span>)<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>()&nbsp;:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(); }</pre> </p> <p> Regardless of which implementation you choose, you'd be able to interact with the result as an <code>IChurchBoolean</code> values, as this small interactive session demonstrates: </p> <p> <pre>&gt; 42.IsEven().ToChurchBoolean().Match(<span style="color:#a31515;">&quot;Even&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Odd&quot;</span>) "Even" &gt; 1337.IsEven().ToChurchBoolean().Match(<span style="color:#a31515;">&quot;Even&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Odd&quot;</span>) "Odd"</pre> </p> <p> Still, converting from <code>bool</code> to <code>IChurchBoolean</code> doesn't address the underlying question: <em>is it possible to write programs without built-in Boolean primitives?</em> </p> <p> The conversion function <code>ToChurchBoolean</code> uses built-in Boolean values and functions, so it doesn't show whether or not it would be possible to make do without those. </p> <p> Before we abandon that line of inquiry, however, I think it's only fair to share a conversion method that goes the other way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;ToBool(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;b) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;b.Match(<span style="color:blue;">true</span>,&nbsp;<span style="color:blue;">false</span>); }</pre> </p> <p> This function enables you to convert an <code>IChurchBoolean</code> value into a primitive C# <code>bool</code>, because when <code>b</code> represents <em>true</em>, the first argument (i.e. <code>true</code>) is returned, and when <code>b</code> represents <em>false</em>, the second argument (i.e. <code>false</code>) is returned. </p> <h3 id="5a59bcde604b4444b7dba56a6ed1989f"> Peano numbers <a href="#5a59bcde604b4444b7dba56a6ed1989f" title="permalink">#</a> </h3> <p> If we can't use built-in primitives or operators that return them (e.g. <code>==</code>), we may not be able to move forward with built-in numbers, either. What we <em>can</em> do, however, is to follow the <a href="https://en.wikipedia.org/wiki/Lambda_calculus">lambda calculus</a> to implement <a href="https://en.wikipedia.org/wiki/Natural_number">natural numbers</a> using Church encoding. This will enable us to determine if a natural number is even or odd. </p> <p> Lambda calculus models natural numbers according to <a href="https://en.wikipedia.org/wiki/Peano_axioms">Peano's model</a>. In short, a natural number is either zero (or one, depending on the specific interpretation), or a successor to another natural number. As an example, using the <code>Successor</code> class that I'll develop later in this article, the number three can be represented as <code>new Successor(new Successor(new Successor(new Zero())))</code> - it's the number after the number after the number after zero. </p> <p> Like Church-encoded Boolean values, a Church-encoded natural number is a function that takes two arguments, corresponding to zero, and a successor function: </p> <p> <pre>zero = λf.λx.x one = λf.λx.f x two = λf.λx.f (f x) three = λf.λx.f (f (f x)) ...</pre> </p> <p> Each of these functions takes an initial value <code>x</code>, as well as a function <code>f</code>. In the lambda calculus, neither <code>x</code> nor <code>f</code> have any implied interpretation; it's the number of applications of <code>f</code> that defines the number. </p> <p> In most translations into programming languages that I've encountered, however, <code>x</code> is usually interpreted as zero, and <code>f</code> as the <a href="https://en.wikipedia.org/wiki/Successor_function">successor function</a>. In Haskell, for example, <a href="https://wiki.haskell.org/Peano_numbers">a common way to model Peano numbers</a> is to use a <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a>: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Peano&nbsp;=&nbsp;Zero&nbsp;|&nbsp;Succ&nbsp;Peano&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Show</span>) </pre> </p> <p> Basically, this means that a value of the <code>Peano</code> type can either be the atom <code>Zero</code>, or a <code>Succ</code> value. Notice that <code>Succ</code> contains another <code>Peano</code> value; the data type is recursive. </p> <p> You can write Haskell values like these: </p> <p> <pre>*Peano&gt; zero = Zero *Peano&gt; one = Succ Zero *Peano&gt; two = Succ (Succ Zero) *Peano&gt; three = Succ (Succ (Succ Zero))</pre> </p> <p> Alternatively, you can also define the numbers based on previous definitions: </p> <p> <pre>*Peano&gt; zero = Zero *Peano&gt; one = Succ zero *Peano&gt; two = Succ one *Peano&gt; three = Succ two</pre> </p> <p> This variation of Peano numbers uses an explicit sum type, but as the lambda calculus representation suggests, you can also use Church encoding to represent the two cases. </p> <h3 id="144967cc5ba04aac979242c5032b7a96"> Church-encoded natural numbers <a href="#144967cc5ba04aac979242c5032b7a96" title="permalink">#</a> </h3> <p> If you recall Church-encoded Boolean values, you may remember that they are functions that take two values: a value to be used in case of <em>true</em>, and a value to be used in the case of <em>false</em>. You can do something similar with natural numbers. <code>Zero</code> is like <em>true</em> and <em>false</em>, in the sense that it's nothing but a label without any associated data. <code>Succ</code>, on the other hand, contains another <code>Peano</code> value. The way to do that is to turn the <em>successor</em> case into a function. Doing that, you'll arrive at an interface like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;zero,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">INaturalNumber</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;succ); }</pre> </p> <p> The first argument, on the left-hand side, is the case to use when an object represents <em>zero</em>. The second argument, on the right-hand side, is a function that will ultimately produce the value associated with a <em>successor</em>. The implied contract here is that the <code>INaturalNumber</code> passed as input to <code>succ</code> is the <em>predecessor</em> to 'the current value'. This may seem counter-intuitive, but hopefully becomes clearer when you see the <code>Successor</code> class below. The crucial insight is that a successor value has no intrinsic value; it's entirely defined by how many predecessors it has. </p> <p> The <em>zero</em> implementation is similar to how Church-encoding implements <em>true</em>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Zero</span>&nbsp;:&nbsp;<span style="color:#2b91af;">INaturalNumber</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;zero,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">INaturalNumber</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;succ) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;zero; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that the <code>Zero</code> class implements <code>INaturalNumber</code> by always returning <code>zero</code>, and consequently always ignoring <code>succ</code>. </p> <p> Another class, <code>Successor</code>, handles the right-hand side of the <code>Match</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Successor</span>&nbsp;:&nbsp;<span style="color:#2b91af;">INaturalNumber</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;predecessor; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Successor(<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.predecessor&nbsp;=&nbsp;n; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;zero,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">INaturalNumber</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;succ) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;succ(predecessor); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that <code>Successor</code> composes its <code>predecessor</code> via Constructor Injection, and unconditionally calls <code>succ</code> with its <code>predecessor</code> when <code>Match</code> is invoked. </p> <h3 id="8ea8a3a6f4b245a3b7375da29ec4c93c"> Working with natural numbers <a href="#8ea8a3a6f4b245a3b7375da29ec4c93c" title="permalink">#</a> </h3> <p> What can you do with this <code>INaturalNumber</code> API, then? </p> <p> Initially, you can define some numbers, like the above Haskell examples: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NaturalNumber</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;&nbsp;Zero&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Zero</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;&nbsp;&nbsp;One&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(Zero); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;&nbsp;&nbsp;Two&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(One); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Three&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(Two); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;&nbsp;Four&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(Three); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;&nbsp;Five&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(Four); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;&nbsp;&nbsp;Six&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(Five); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Seven&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(Six); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Eight&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(Seven); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;&nbsp;Nine&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(Eight); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;memmbers&nbsp;go&nbsp;here...</span> }</pre> </p> <p> Here, I arbitrarily chose to define the numbers from zero to nine, but you could go on for as long as you care. </p> <p> You can also convert these Church-encoded numbers to primitive <code>int</code> values, like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Count(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;=&gt;&nbsp;1&nbsp;+&nbsp;p.Count()); }</pre> </p> <p> Here are some examples from a C# Interactive session: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Zero.Count() 0 &gt; <span style="color:#2b91af;">NaturalNumber</span>.One.Count() 1 &gt; <span style="color:#2b91af;">NaturalNumber</span>.Seven.Count() 7</pre> </p> <p> The implementation of <code>Count</code> is recursive. When <code>n</code> is a <code>Zero</code> instance, it'll return the first argument (<code>0</code>), but when it's a <code>Successor</code>, it'll invoke the lambda expression <code>p =&gt; 1 + p.Count()</code>. Notice that this lambda expression recursively calls <code>Count</code> on <code>p</code>, which is the <code>Successor</code>'s <code>predecessor</code>. It'll keep doing that until it reaches a <code>Zero</code> instance. </p> <p> Recursion is a central part of the lambda calculus; you can't do anything useful without it. If you're a C# or Java programmer, you may be concerned, because recursion tends to be problematic in such languages. Deeply recursive functions will sooner or later crash because of a stack overflow. </p> <p> You shouldn't, however, be concerned. First, I'm not trying to convince you to write all your future C# or Java code using Church-encoded numbers and Boolean values. The point of this article series is to investigate the fundamentals of computations, and to gain a better understanding of sum types. As such, the code examples presented here are only demonstrations of the underlying principles. Lambda calculus itself serves the same purpose: it's a universal model of computation; it wasn't intended to be a practical programming language - in fact, there were no programmable computers in 1936. </p> <p> Furthermore, the problem with recursion causing stack overflow isn't universal. Languages like <a href="http://fsharp.org">F#</a> and Haskell support <a href="https://en.wikipedia.org/wiki/Tail_call">tail recursion</a>, thereby enabling recursive functions to run to arbitrary depths. </p> <h3 id="a13730f3e8c54cd98beb1801b07a4707"> Pattern matching <a href="#a13730f3e8c54cd98beb1801b07a4707" title="permalink">#</a> </h3> <p> In the previous article, I hinted that there's a reason I decided to name the interface method <code>Match</code>. This is because it looks a lot like <a href="https://en.wikipedia.org/wiki/Pattern_matching">pattern matching</a>. In F#, you could write <code>count</code> like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Peano&nbsp;=&nbsp;Zero&nbsp;|&nbsp;Succ&nbsp;<span style="color:blue;">of</span>&nbsp;Peano <span style="color:green;">//&nbsp;Peano&nbsp;-&gt;&nbsp;int</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;count&nbsp;n&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;n&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Zero&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Succ&nbsp;p&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1&nbsp;+&nbsp;count&nbsp;p</pre> </p> <p> This implementation, by the way, isn't tail-recursive, but you can <a href="/2015/12/22/tail-recurse">easily refactor to a tail-recursive implementation</a> like this: </p> <p> <pre><span style="color:green;">//&nbsp;Peano&nbsp;-&gt;&nbsp;int</span> <span style="color:blue;">let</span>&nbsp;count&nbsp;n&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;countImp&nbsp;acc&nbsp;n&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;n&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Zero&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Succ&nbsp;p&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;countImp&nbsp;(1&nbsp;+&nbsp;acc)&nbsp;p &nbsp;&nbsp;&nbsp;&nbsp;countImp&nbsp;0&nbsp;n</pre> </p> <p> Both variations use the <code>match</code> keyword to handle both the <code>Zero</code> and the <code>Succ</code> case for any <code>Peano</code> value <code>n</code>. That's already close to the above C# code, but using the optional C# language feature of <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments">named arguments</a>, you can rewrite the implementation of <code>Count</code> to this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Count(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zero:&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;succ:&nbsp;p&nbsp;=&gt;&nbsp;1&nbsp;+&nbsp;p.Count()); }</pre> </p> <p> This starts to look like pattern matching of sum types in F#. The argument names aren't required, but using them makes it clearer which cases the <code>Match</code> method handles. </p> <h3 id="f4beca2d1da24362b4c43cf1f6528616"> Addition <a href="#f4beca2d1da24362b4c43cf1f6528616" title="permalink">#</a> </h3> <p> You can now start to add features and capabilities to the natural numbers API. An obvious next step is to implement addition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zero:&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;succ:&nbsp;p&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(p.Add(y))); }</pre> </p> <p> Again, the implementation is recursive. When <code>x</code> is <code>zero</code>, you simply return <code>y</code>, because <em>zero + y</em> is <em>y</em>. When <code>x</code> is a <code>Successor</code>, you recursively add <code>y</code> to its <code>predecessor</code>, and put the result in a new <code>Successor</code>. You can think of the predecessor <code>p</code> as one less than the successor. By recursively subtracting one from any <code>Successor</code> object, you'll eventually match the <code>zero</code> case, which will then return <code>y</code>. When the stack unrolls, each stack puts the previous result into a new <code>Successor</code>. This happens exactly the correct number of times corresponding to the value of <code>x</code>, because that's the size of the stack when <code>Add</code> hits <code>zero</code>. </p> <p> Here are some examples: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.One.Add(<span style="color:#2b91af;">NaturalNumber</span>.Two).Count() 3 &gt; <span style="color:#2b91af;">NaturalNumber</span>.Four.Add(<span style="color:#2b91af;">NaturalNumber</span>.Three).Count() 7 &gt; <span style="color:#2b91af;">NaturalNumber</span>.Seven.Add(<span style="color:#2b91af;">NaturalNumber</span>.Six).Count() 13</pre> </p> <p> You can also implement multiplication, but that's a bit more complicated, and not relevant to the topic of this article (which is how to determine if a number is even or odd). </p> <h3 id="ceb31f7122ea4b258b26d038f0d247bf"> Testing for zero <a href="#ceb31f7122ea4b258b26d038f0d247bf" title="permalink">#</a> </h3> <p> In addition to basic arithmetic, you can also define functions that tell you something about a natural number. We'll start gently with a function that tells us whether or not a number is zero: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsZero(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Match&lt;<span style="color:#2b91af;">IChurchBoolean</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zero:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;succ:&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>()); }</pre> </p> <p> The <code>IsZero</code> method simply returns a <code>ChurchTrue</code> object when <code>n</code> is a <code>Zero</code> instance, and a <code>ChurchFalse</code> object for all other numbers. </p> <p> You can see that this works in this C# Interactive session: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Two.IsZero() ChurchFalse { } &gt; <span style="color:#2b91af;">NaturalNumber</span>.Zero.IsZero() ChurchTrue { } &gt; <span style="color:#2b91af;">NaturalNumber</span>.Three.IsZero() ChurchFalse { }</pre> </p> <p> You can also <code>Match</code> on the returned Boolean value to return e.g. a string: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Nine.IsZero().Match(trueCase:&nbsp;<span style="color:#a31515;">&quot;Zero&quot;</span>,&nbsp;falseCase:&nbsp;<span style="color:#a31515;">&quot;Not&nbsp;zero&quot;</span>) "Not zero" &gt; <span style="color:#2b91af;">NaturalNumber</span>.Zero.IsZero().Match(trueCase:&nbsp;<span style="color:#a31515;">&quot;Zero&quot;</span>,&nbsp;falseCase:&nbsp;<span style="color:#a31515;">&quot;Not&nbsp;zero&quot;</span>) "Zero"</pre> </p> <p> This already demonstrates that you can implement predicates and branching logic from first principles, without resorting to built-in Boolean primitives or operators. </p> <h3 id="c131713ecba247f98cc1b93e0fb8451b"> Detecting even numbers <a href="#c131713ecba247f98cc1b93e0fb8451b" title="permalink">#</a> </h3> <p> Testing whether a natural number is even or uneven requires a bit more work. It's probably easiest to understand if we first consider an F# implementation: </p> <p> <pre><span style="color:green;">//&nbsp;Peano&nbsp;-&gt;&nbsp;ChurchBoolean</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;isEven&nbsp;n&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;n&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Zero&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ChurchTrue &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Succ&nbsp;Zero&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ChurchFalse &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Succ&nbsp;(Succ&nbsp;p)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;isEven&nbsp;p</pre> </p> <p> Zero is even, so when <code>n</code> matches <code>Zero</code>, <code>isEven</code> returns <code>ChurchTrue</code>. Conversely, when the input is <code>Succ Zero</code> (i.e. <em>one</em>), the return value is <code>ChurchFalse</code> because <em>one</em> is odd. </p> <p> The <em>zero</em> and <em>one</em> cases serve as exit cases for the recursive algorithm. Since we've handled <code>Zero</code> and <code>Succ Zero</code> (that is, <em>zero</em> and <em>one</em>), we know that any other case must be at least twice nested. This means that the <code>Succ (Succ p)</code> pattern matches all other cases. You can think of <code>p</code> as <em>n - 2</em>. </p> <p> The algorithm proceeds to recursively call <code>isEven</code> with <code>p</code> (i.e. <em>n - 2</em>). Sooner or later, these recursive function calls will match either the <code>Zero</code> or the <code>Succ Zero</code> case, and exit with the appropriate return value. </p> <p> C# doesn't have as sophisticated pattern matching features as F#, so we're going to have to figure out how implement this algorithm without relying on a nested pattern like <code>Succ (Succ p)</code>. As an initial step, we can rewrite the function in F#, using two matches instead of one: </p> <p> <pre><span style="color:green;">//&nbsp;Peano&nbsp;-&gt;&nbsp;ChurchBoolean</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;isEven&nbsp;n&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;n&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Zero&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ChurchTrue &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Succ&nbsp;p1&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;p1&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Zero&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ChurchFalse &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Succ&nbsp;p2&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;isEven&nbsp;p2</pre> </p> <p> This isn't as elegant as the previous implementation, but on the other hand, it's straightforward to translate to C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsEven(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zero:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;0&nbsp;is&nbsp;even,&nbsp;so&nbsp;true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;succ:&nbsp;p1&nbsp;=&gt;&nbsp;p1.Match(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Match&nbsp;previous</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zero:&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(),&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;If&nbsp;0&nbsp;then&nbsp;successor&nbsp;was&nbsp;1</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;succ:&nbsp;p2&nbsp;=&gt;&nbsp;p2.IsEven()));&nbsp;<span style="color:green;">//&nbsp;Eval&nbsp;previous&#39;&nbsp;previous</span> }</pre> </p> <p> Like in the F# example, when <code>n</code> is a <code>Zero</code> object, it'll return the value associated with the <code>zero</code> case. Since zero is even, it returns a <code>ChurchTrue</code> object. </p> <p> In all other cases, a <code>Match</code> on the predecessor <code>p1</code> is required. If that nested match is <code>zero</code>, then we know that <code>n</code> must have been <em>one</em>, since the predecessor turned out to be <em>zero</em>. In that case, then, return a <code>ChurchFalse</code> object, because <em>one</em> isn't even. </p> <p> The nested <code>Match</code> considers the predecessor <code>p1</code>. In the <code>succ</code> case of the nested <code>Match</code>, then, we can consider <code>p2</code>; that is, the predecessor to the predecessor to <code>n</code> - in other words: <em>n - 2</em>. The function recursively calls itself with <em>n - 2</em>, and it'll keep doing so until it matches either the <em>zero</em> or the <em>one</em> case. </p> <p> The implementation works: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Two.IsEven() ChurchTrue { } &gt; <span style="color:#2b91af;">NaturalNumber</span>.Three.IsEven() ChurchFalse { }</pre> </p> <p> <code>IsEven</code> is implemented from first principles. The only language features we need are lambda expressions and recursion, although in order to make these examples slightly more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a>, I've also used interfaces and classes. </p> <h3 id="d4be671469fb4f529db9afd3e2626dd1"> Detecting odd numbers <a href="#d4be671469fb4f529db9afd3e2626dd1" title="permalink">#</a> </h3> <p> You could implement a corresponding <code>IsOdd</code> method similarly to <code>IsEven</code>, but it's easier to use the Boolean operators already in place from the previous article: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsOdd(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(n.IsEven()); }</pre> </p> <p> <code>IsOdd</code> is simply the Boolean negation of <code>IsEven</code>. Like <code>IsEven</code> it also works correctly: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Six.IsOdd().Match(trueCase:&nbsp;<span style="color:#a31515;">&quot;Odd&quot;</span>,&nbsp;falseCase:&nbsp;<span style="color:#a31515;">&quot;Even&quot;</span>) "Even" &gt; <span style="color:#2b91af;">NaturalNumber</span>.Seven.IsOdd().Match(trueCase:&nbsp;<span style="color:#a31515;">&quot;Odd&quot;</span>,&nbsp;falseCase:&nbsp;<span style="color:#a31515;">&quot;Even&quot;</span>) "Odd"</pre> </p> <p> You can implement other operators (like multiplication) and predicates from the building blocks shown here, but I'm not going to cover that here (see <a href="https://github.com/ploeh/ChurchEncoding/tree/a8512fe004dc7da41fa17b069a6c245c31b97d7c">the accompanying GitHub repository for more code</a>). I hope that this article gave you a sense of how a programming language can be designed from the low-level building blocks defined by the lambda calculus. </p> <h3 id="b4be8911648f4dcab6191fd50c03581a"> Summary <a href="#b4be8911648f4dcab6191fd50c03581a" title="permalink">#</a> </h3> <p> <a href="https://en.wikipedia.org/wiki/Giuseppe_Peano">Giuseppe Peano</a> described natural numbers as an initial number (zero) and successors to that number. Church formulated Peano numbers in the lambda calculus. Using Church encoding, you can translate this representation to various programming languages, including, as you've seen in this article, C#. </p> <p> In the previous article, you saw how to model Boolean values as a set of functions with two arguments. In this article, you saw how to model natural numbers with another set of functions that take two arguments. In the next article, you'll see another data type modelled as a set of functions with two arguments. It looks like a patterns is starting to appear. </p> <p> <strong>Next:</strong> <a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Church-encoded Boolean values https://blog.ploeh.dk/2018/05/24/church-encoded-boolean-values 2018-05-24T04:49:00+00:00 Mark Seemann <div id="post"> <p> <em>Boolean values, and logical branching, don't have to be built into programming languages. An introduction for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2018/05/22/church-encoding">a series of articles about Church encoding</a>. </p> <p> Years ago, the so-called <a href="http://www.antiifcampaign.com">Anti-IF Campaign</a> made the rounds on various social media (back then, IIRC, mostly known as 'the blogosphere'). The purpose of the campaign was never to eradicate every single use of <code>if</code> statements or expressions in source code, but rather to educate people about alternatives to the <a href="http://wiki.c2.com/?ArrowAntiPattern">Arrow anti-pattern</a>. </p> <p> One easy way to deal with arrow code is to <a href="https://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html">Replace Nested Conditionals with Guard Clauses</a>, but that's not always possible. Another way is to encapsulate some <code>if</code> blocks in helper methods. Yet another way would be to use polymorphic dispatch, but how does that even work? Don't you, deep down, need at least a few <code>if</code> keywords here and there? </p> <p> It turns out that the answer, surprisingly, is <em>no</em>. </p> <h3 id="3215aad4142e466393c8a533e4bda305"> Untyped Boolean functions <a href="#3215aad4142e466393c8a533e4bda305" title="permalink">#</a> </h3> <p> <code>if/then/else</code> expressions are based on Boolean values (<em>true</em> and <em>false</em>): if some Boolean value is true, then something happens; otherwise, something else happens. Most programming languages, including C, C++, Java, C#, and JavaScript, have a <a href="https://en.wikipedia.org/wiki/%3F:">ternary operator</a>, which in C# looks like this: </p> <p> <pre>isEven&nbsp;?&nbsp;<span style="color:#a31515;">&quot;Probably&nbsp;not&nbsp;a&nbsp;prime.&quot;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&quot;Could&nbsp;be&nbsp;a&nbsp;prime.&quot;</span>;</pre> </p> <p> You can think of an expression like that as a function that takes a Boolean value and two potential return values: one for the <em>true</em> case, and one for the <em>false</em> case. </p> <p> In <a href="https://en.wikipedia.org/wiki/Lambda_calculus">lambda calculus</a>, the only primitive building blocks are functions. There's no built-in Boolean values, but you can define them with functions. Boolean values are functions that take two arguments. By conventions, the first argument (the one to the left) represents the <em>true</em> case, whereas the second argument (to the right) signifies the <em>false</em> case - just like the ternary operator. In the lambda calculus, functions are curried, but we know from <a href="/2018/02/05/uncurry-isomorphisms">uncurry isomorphisms</a> that we can also represent a two-argument function as a function that takes a two-tuple (a <em>pair</em>) as a single argument. Furthermore, we know from <a href="/2018/01/22/function-isomorphisms">function isomorphisms</a> that we can represent a function as an instance method. Therefore, we can declare a Boolean value in C# to be an object that implements this interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>&nbsp;Match(<span style="color:blue;">object</span>&nbsp;trueCase,&nbsp;<span style="color:blue;">object</span>&nbsp;falseCase); }</pre> </p> <p> You'll notice that I've chosen to call the method <code>Match</code>, for reasons that should hopefully become clear as we go along. </p> <p> The intent with such a Church-encoded Boolean is that any object that represents <em>true</em> should return the left argument (<code>trueCase</code>), whereas an object that represents <em>false</em> should return the right argument (<code>falseCase</code>). </p> <p> In other words, <em>true</em> is an interface implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IChurchBoolean</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;Match(<span style="color:blue;">object</span>&nbsp;trueCase,&nbsp;<span style="color:blue;">object</span>&nbsp;falseCase) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;trueCase; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that this implementation always returns <code>trueCase</code> while ignoring <code>falseCase</code>. No explicit <code>if</code> statement is required. </p> <p> Likewise, <em>false</em> is implemented the same way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IChurchBoolean</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;Match(<span style="color:blue;">object</span>&nbsp;trueCase,&nbsp;<span style="color:blue;">object</span>&nbsp;falseCase) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;falseCase; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> So far, this doesn't offer much capability, but it does already give you the ability to choose between two values, as this little C# Interactive session demonstrates: </p> <p> <pre>&gt; var b = new ChurchTrue(); &gt; b.Match("foo", "bar") "foo" &gt; var b = new ChurchFalse(); &gt; b.Match("foo", "bar") "bar"</pre> </p> <p> When 'the Boolean value' is a <code>ChurchTrue</code> instance, then the left argument is returned; otherwise, when <code>b</code> is a <code>ChurchFalse</code> object, the return value is the right-hand value - just like the ternary operator. </p> <h3 id="1ebf901dbc234074a98b809ebeb570b1"> Boolean And <a href="#1ebf901dbc234074a98b809ebeb570b1" title="permalink">#</a> </h3> <p> You can now define the standard Boolean operators <em>and</em>, <em>or</em>, and <em>not</em>. Starting with <em>and:</em> </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ChurchAnd</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IChurchBoolean</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ChurchAnd(<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.x&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.y&nbsp;=&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;Match(<span style="color:blue;">object</span>&nbsp;trueCase,&nbsp;<span style="color:blue;">object</span>&nbsp;falseCase) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Match(y.Match(trueCase,&nbsp;falseCase),&nbsp;falseCase); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>ChurchAnd</code> class is an implementation of <code>IChurchBoolean</code> that composes two other <code>IChurchBoolean</code> values, <code>x</code> and <code>y</code>. You can use it like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;b&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchAnd</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>()); </pre> </p> <p> In this case, <code>b</code> represents <em>false</em>, because it'll always return the right-hand argument when <code>Match</code> is invoked. </p> <p> Notice that the implementation of <code>ChurchAnd.Match</code> first matches on <code>x</code>. Only if <code>x</code> itself is <em>true</em> can the expression passed as the first argument be returned; otherwise, <code>falseCase</code> will be returned. Thus, if <code>x</code> is <em>true</em>, the expression <code>y.Match(trueCase, falseCase)</code> will be returned, and only if that as well evaluates to <em>true</em> is the final result <em>true</em>. The <code>trueCase</code> value is only returned if <code>y</code> represents <em>true</em>, as well as <code>x</code>. </p> <p> In the lambda calculus, Boolean <em>and</em> is defined like this: </p> <p> <pre>and = λx.λy.λt.λf.x (y t f) f</pre> </p> <p> The way to read this is that Boolean <em>and</em> is a function that takes four arguments: <ul> <li><code>x</code>, a Boolean value</li> <li><code>y</code>, another Boolean value</li> <li><code>t</code>, the value to return if the expression is <em>true</em>; the <code>trueCase</code> argument in the above C# implementation.</li> <li><code>f</code>, the value to return if the expression is <em>false</em>; the <code>falseCase</code> argument in the above C# implementation.</li> </ul> Recall that in the lambda calculus, Boolean values are functions that take two arguments, so <code>x</code> and <code>y</code> are functions. <code>and</code> calls <code>x</code> with two arguments. Since Boolean <em>and</em> requires both <code>x</code> and <code>y</code> to be <em>true</em>, it passes <code>f</code> as the second argument to <code>x</code>, because if <code>x</code> represents <em>false</em>, it'll return its right-hand argument. Only if <code>x</code> represents <em>true</em> does it make sense to investigate the Boolean value of <code>y</code>, which is also a function that takes two arguments. Only if <code>y</code> also represents <em>true</em> will <code>t</code> be returned. </p> <p> This is exactly the same implementation as the above C# code. </p> <p> Wait a minute, though, didn't I write that Boolean values are functions that take two arguments? And isn't <code>and</code> a function that takes four arguments? </p> <p> Yes, indeed. That's how currying works. You can view <code>and</code> as a function that takes four arguments, but you can also view it as a function that takes two arguments (<code>x</code> and <code>y</code>) and returns another function that takes two arguments. This becomes clearer with partial application. When translating to C#, the 'contract' (that a Boolean value is a function that takes two arguments) is modelled as the interface <code>IChurchBoolean</code>, while the 'extra arguments' <code>x</code> and <code>y</code> become class fields, injected via the class' constructor. </p> <h3 id="33ba3de3f9bd46fabbc0b44935578c15"> Boolean Or <a href="#33ba3de3f9bd46fabbc0b44935578c15" title="permalink">#</a> </h3> <p> In the lambda calculus, Boolean <em>or</em> is defined like this: </p> <p> <pre>or = λx.λy.λt.λf.x t (y t f)</pre> </p> <p> Translated to C#, this becomes: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ChurchOr</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IChurchBoolean</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ChurchOr(<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.x&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.y&nbsp;=&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;Match(<span style="color:blue;">object</span>&nbsp;trueCase,&nbsp;<span style="color:blue;">object</span>&nbsp;falseCase) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Match(trueCase,&nbsp;y.Match(trueCase,&nbsp;falseCase)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can see that this is another direct translation. Boolean <em>or</em> only requires (at least) one of the Boolean values to be <em>true</em>, so if <code>x</code> is <em>true</em>, you can immediately return <code>trueCase</code>. Otherwise, in the case where <code>x</code> is <em>false</em>, there's still a chance that the entire expression could be <em>true</em>, so you'll have to evaluate <code>y</code> as well. When <code>y</code> represents <em>true</em>, you can still return <code>trueCase</code>. Only when <code>y</code> is also <em>false</em> should you return <code>falseCase</code>. </p> <p> You can use <code>ChurchOr</code> like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;b&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchOr</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>()); </pre> </p> <p> Here, <code>b</code> is <em>true</em> because <em>true or false</em> is <em>true</em>. </p> <h3 id="3238c67454fe4463946b9625c447571a"> Boolean Not <a href="#3238c67454fe4463946b9625c447571a" title="permalink">#</a> </h3> <p> Finally, you can also define Boolean negation. In lambda calculus it's: </p> <p> <pre>not = λx.λt.λf.x f t</pre> </p> <p> Notice how this simply swaps the arguments passed to <code>x</code>. In C#, this translates to: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IChurchBoolean</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;b; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ChurchNot(<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;b) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.b&nbsp;=&nbsp;b; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">object</span>&nbsp;Match(<span style="color:blue;">object</span>&nbsp;trueCase,&nbsp;<span style="color:blue;">object</span>&nbsp;falseCase) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;b.Match(falseCase,&nbsp;trueCase); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You can combine all the Boolean operators like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;b&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchOr</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>(),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>())); </pre> </p> <p> Here, <code>b</code> is <em>false</em> because <em>false or (not true)</em> is <em>false</em>. </p> <h3 id="5a06c7f9d7b04b31a8f4ca6ab9b28ca4"> Typed Boolean functions <a href="#5a06c7f9d7b04b31a8f4ca6ab9b28ca4" title="permalink">#</a> </h3> <p> So far, the <code>IChurchBoolean</code> interface has been untyped, in the sense that it took <code>object</code> arguments and had an <code>object</code> return type. You can, however, easily make the interface strongly typed, using generics: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;trueCase,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;falseCase); }</pre> </p> <p> This doesn't really change the rest of the code you've seen in this article. The method signatures chance, but the implementations remain as shown. You can see the change in <a href="https://github.com/ploeh/ChurchEncoding/commit/2cba6d7625b08054caf6f42cc30898ced704848f">this commit</a>. </p> <h3 id="9821112728f64424b39990eb26ede2b8"> Semigroups and monoids <a href="#9821112728f64424b39990eb26ede2b8" title="permalink">#</a> </h3> <p> The strongly typed signature accentuates that the <code>Match</code> method is a binary operation; it takes two values of the type <code>T</code> and returns a single <code>T</code> value. Is it a <a href="/2017/10/06/monoids">monoid</a>, then? </p> <p> It's not a single monoid, but rather a collection of <a href="/2017/11/27/semigroups">semigroups</a>, some of which are monoids as well. The implementation of <code>ChurchTrue</code> corresponds to the <em>first</em> semigroup, and <code>ChurchFalse</code> to the <em>last</em> semigroup. You can make this explict in <a href="https://www.haskell.org">Haskell</a>: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;Data.Semigroup <span style="color:#2b91af;">churchTrue</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a churchTrue&nbsp;t&nbsp;f&nbsp;=&nbsp;getFirst&nbsp;(First&nbsp;t&nbsp;&lt;&gt;&nbsp;First&nbsp;f)</pre> </p> <p> If you compare this implementation of <code>churchTrue</code> to the <a href="http://programmable.computer/posts/church_encoding.html">Travis Whitaker's <code>true</code> function</a>, his is much simpler. I'm not suggesting that using <code>First</code> is better; I'm only trying to illustrate the connection. </p> <p> If you aren't familiar with how things are done in Haskell, <code>&lt;&gt;</code> is the 'generic semigroup binary operator'. What it does depends on the type of expressions surrounding it. By wrapping both <code>t</code> and <code>f</code> in <code>First</code> containers, the <code>&lt;&gt;</code> operator becomes the operator that always returns the first argument (i.e. <code>First t</code>). Since the result is a <code>First</code> value, you have to unwrap it again by applying <code>getFirst</code>. </p> <p> Likewise, you can define <em>false:</em> </p> <p> <pre><span style="color:#2b91af;">churchFalse</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a churchFalse&nbsp;t&nbsp;f&nbsp;=&nbsp;getLast&nbsp;(Last&nbsp;t&nbsp;&lt;&gt;&nbsp;Last&nbsp;f)</pre> </p> <p> This still uses the <code>&lt;&gt;</code> operator, but now with the <code>Last</code> container, which gives it all the behaviour of the <em>last</em> semigroup. </p> <p> The <em>any</em> and <em>all</em> monoids are implemented as compositions of these two fundamental semigroups. In the C# code in this article, they're implemented by <code>ChurchAnd</code> and <code>ChurchOr</code>, although in neither case have I defined an explicit identity value. This is, however, possible, so let's continue with the Haskell code to see what that would look like. First, you can define the 'naked' operations: </p> <p> <pre>churchAnd&nbsp;x&nbsp;y&nbsp;t&nbsp;f&nbsp;=&nbsp;x&nbsp;(y&nbsp;t&nbsp;f)&nbsp;f churchOr&nbsp;x&nbsp;y&nbsp;t&nbsp;f&nbsp;=&nbsp;x&nbsp;t&nbsp;(y&nbsp;t&nbsp;f)</pre> </p> <p> I have here omitted the type signatures on purpose, as I believe they might confuse rather than help. In both cases, the logic is the same as in the above <code>ChurchAnd</code> and <code>ChurchOr</code> classes, although, as you can see, Haskell code is much terser. </p> <p> These two functions already work as desired, but we can easily turn both into their respective monoids. First, the <em>all</em> monoid: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;ChurchAll&nbsp;=&nbsp;ChurchAll&nbsp;{&nbsp;runAll&nbsp;::&nbsp;forall&nbsp;a.&nbsp;a&nbsp;-&gt;&nbsp;a&nbsp;-&gt;&nbsp;a&nbsp;} <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;<span style="color:blue;">ChurchAll</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;ChurchAll&nbsp;x&nbsp;&lt;&gt;&nbsp;ChurchAll&nbsp;y&nbsp;=&nbsp;ChurchAll&nbsp;(churchAnd&nbsp;x&nbsp;y) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;<span style="color:blue;">ChurchAll</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;mempty&nbsp;=&nbsp;ChurchAll&nbsp;churchTrue &nbsp;&nbsp;&nbsp;&nbsp;mappend&nbsp;=&nbsp;<span style="color:#2b91af;">(&lt;&gt;)</span></pre> </p> <p> In order for this code to compile, you must enable the <em>RankNTypes</em> language extension, which I did by adding the <code>{-# LANGUAGE RankNTypes #-}</code> pragma to the top of my code file. The <code>forall a</code> declaration corresponds to the <code>&lt;T&gt;</code> type annotation on the C# <code>Match</code> method. You can think of this as that the type argument is scoped to the function instead of the type. </p> <p> The <code>Semigroup</code> instance simply delegates its behaviour to <code>churchAnd</code>, and the <code>Monoid</code> instance returns <code>churchTrue</code> as the identity (<code>mempty</code>). </p> <p> Similarly, you can implement the <em>any</em> monoid: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;ChurchAny&nbsp;=&nbsp;ChurchAny&nbsp;{&nbsp;runAny&nbsp;::&nbsp;forall&nbsp;a.&nbsp;a&nbsp;-&gt;&nbsp;a&nbsp;-&gt;&nbsp;a&nbsp;} <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;<span style="color:blue;">ChurchAny</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;ChurchAny&nbsp;x&nbsp;&lt;&gt;&nbsp;ChurchAny&nbsp;y&nbsp;=&nbsp;ChurchAny&nbsp;(churchOr&nbsp;x&nbsp;y) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;<span style="color:blue;">ChurchAny</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;mempty&nbsp;=&nbsp;ChurchAny&nbsp;churchFalse &nbsp;&nbsp;&nbsp;&nbsp;mappend&nbsp;=&nbsp;<span style="color:#2b91af;">(&lt;&gt;)</span></pre> </p> <p> As is also the case with <code>ChurchAll</code>, the <code>ChurchAny</code> instance of <code>Semigroup</code> simply delegates to a 'naked' function (in this case <code>churchOr</code>), and the <code>Monoid</code> instance again delegates <code>mappend</code> to <code>&lt;&gt;</code> and returns <code>churchFalse</code> as the identity. </p> <p> The following brief GHCi session demonstrates that it all works as intended: </p> <p> <pre>λ&gt; runAny (ChurchAny churchTrue &lt;&gt; ChurchAny churchFalse) "foo" "bar" "foo" λ&gt; runAny (ChurchAny churchFalse &lt;&gt; ChurchAny churchFalse) "foo" "bar" "bar" λ&gt; runAll (ChurchAll churchFalse &lt;&gt; ChurchAll churchTrue) "foo" "bar" "bar" λ&gt; runAll (ChurchAll churchTrue &lt;&gt; ChurchAll churchTrue) "foo" "bar" "foo"</pre> </p> <p> Recall that a Church-encoded Boolean is a function that takes two values - in all the four above examples <code>"foo"</code> and <code>"bar"</code>. When the expression represents <em>true</em> it returns the left-hand value (<code>"foo"</code>); otherwise, it returns the right-hand value (<code>"bar"</code>). </p> <p> In summary, the Church-encoded Boolean values <em>true</em> and <em>false</em> correspond to the <em>first</em> and <em>last</em> semigroups. You can compose the well-known monoids over Boolean values using these two basic building blocks. </p> <h3 id="b0ac2fd3255c4c61af0f6ddb4f95b365"> Summary <a href="#b0ac2fd3255c4c61af0f6ddb4f95b365" title="permalink">#</a> </h3> <p> You'd normally think of Boolean values as language primitives. <em>True</em> and <em>false</em> are built into most languages, as well as common operators like <em>and</em>, <em>or</em>, and <em>not</em>. While this is convenient, it doesn't <em>have</em> to be like this. Even in languages that already have built-in support for Boolean values, like Haskell or C#, you can define Church-encoded Boolean values from first principles. </p> <p> In the lambda calculus, a Boolean value is function that takes two arguments and returns the left-hand argument when <em>true</em>, and the right-hand argument when <em>false</em>. </p> <p> At this point, it may seem like you can't do much with the <code>IChurchBoolean</code> API. How could you, for instance, determine whether an integer is even or odd? </p> <p> This innocuous-looking question is harder to answer than you may think, so that's worthy of its own article. </p> <p> <strong>Next:</strong> <a href="/2018/05/28/church-encoded-natural-numbers">Church-encoded natural numbers</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Church encoding https://blog.ploeh.dk/2018/05/22/church-encoding 2018-05-22T06:28:00+00:00 Mark Seemann <div id="post"> <p> <em>Church encoding is a unified way to model data and functions. An introduction for object-oriented developers.</em> </p> <p> This article series is part of <a href="/2017/10/04/from-design-patterns-to-category-theory">an even larger series of articles about the relationship between design patterns and category theory.</a> </p> <p> When asked why I like functional programming so much, I often emphasise the <a href="/2016/02/10/types-properties-software-designing-with-types">superior modelling ability</a> that I get from <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>. Particularly, languages like <a href="http://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a> have <a href="https://en.wikipedia.org/wiki/Tagged_union">sum types</a> in addition to the <a href="https://en.wikipedia.org/wiki/Product_type">product types</a> that most statically typed languages seem to have. </p> <p> In short, a <em>sum type</em> gives you the ability to declare, as part of the type system, that a particular data type must be exactly one of a <em>finite</em> list of mutually exclusive options. This differs from common object-oriented sub-typing because class inheritance or interface implementation offers conceptually infinite extensibility. Sometimes, unconstrained extensibility is exactly what you need, but in other cases, the ability to define a closed set of cases can be an effective modelling tool. If you need an easy-to-read introduction to algebraic data types, I recommend <a href="http://tomasp.net">Tomas Petricek</a>'s fine article <a href="http://tomasp.net/blog/types-and-math.aspx">Power of mathematics: Reasoning about functional types</a>. </p> <p> Interestingly, <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html">TypeScript has sum types</a>, so they don't have to belong exclusively in the realm of functional programming. In this article series, you'll see an alternative way to represent sum types in C# using <em>Church encoding</em>. </p> <h3 id="981c0967cd414784968a1ceded3f9f45"> Lambda calculus <a href="#981c0967cd414784968a1ceded3f9f45" title="permalink">#</a> </h3> <p> In the 1930s, several mathematicians were investigating the foundations of mathematics. One of them, <a href="https://en.wikipedia.org/wiki/Alonzo_Church">Alonzo Church</a>, developed <a href="https://en.wikipedia.org/wiki/Lambda_calculus">lambda calculus</a> as a universal model of computation. In a sense, you can think of lambda calculus as a sort of hypothetical programming language, although it was never designed to be a practical programming language. Even so, you can learn a lot from it. </p> <p> In the untyped lambda calculus, the only primitive data type is a function. There are no primitive numbers, Boolean values, branching instructions, loops, or anything else you'd normally consider as parts of a programming language. Instead, there's only functions, written as <em>lambda expressions:</em> </p> <p> <pre>λf.λx.f x</pre> </p> <p> This looks opaque and mathematical, but most modern programmers should be familiar with lambda (λ) expressions. The above expression is an anonymous function that takes a single argument: <code>f</code>. The body of the function is the return value; here, another lambda expression: <code>λx.f x</code>. This lambda expression also takes a single argument: <code>x</code>. </p> <p> In the untyped lambda calculus, everything is a function, so that includes <code>f</code> and <code>x</code>. The return value of the entire expression is <code>f x</code>, which means that the function <code>f</code> is applied to the value (in fact: function) <code>x</code>. The entire expression is therefore a <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order function</a>. </p> <p> In C#, the corresponding lambda expression would be: </p> <p> <pre>f =&gt; x =&gt; f(x)</pre> </p> <p> This is a lambda expression that returns another lambda expression, which again returns the result of calling the function <code>f</code> with the value <code>x</code>. </p> <p> In F#, it would be: </p> <p> <pre>fun f -&gt; fun x -&gt; f x</pre> </p> <p> and in Haskell, it would be: </p> <p> <pre>\f -&gt; \x -&gt; f x</pre> </p> <p> In both Haskell and F#, functions are already curried, so you can shorten that Haskell lambda expression to: </p> <p> <pre>\f x -&gt; f x</pre> </p> <p> and the F# lambda expression to: </p> <p> <pre>fun f x -&gt; f x</pre> </p> <p> This looks more like a function that takes two arguments, so alternatively, via <a href="/2018/02/05/uncurry-isomorphisms">uncurry isomorphisms</a>, you can also write the C# representation like this: </p> <p> <pre>(f, x) =&gt; f(x)</pre> </p> <p> Those six lambda expressions, however, are statically typed, even though they're generic (or, as Haskellers would put it, <a href="https://en.wikipedia.org/wiki/Parametric_polymorphism">parametric polymorphic</a>). This means that they're not entirely equal to <code>λf.λx.f x</code>, but it should give you a sense of what a lambda expression is. </p> <p> It turns out that using nothing but lambda expressions, one can express any computation; lambda calculus is Turing-complete. </p> <h3 id="a921177fdc9e485389b476ab7f599aa7"> Church encoding <a href="#a921177fdc9e485389b476ab7f599aa7" title="permalink">#</a> </h3> <p> Since languages like C#, F#, Haskell, and others, include lambda expressions, you can reproduce as much of the lambda calculus as you'd like. In this article series, I'll mainly use it to show you how to represent sum types in C#. Later, you'll see how it relates to design patterns. </p> <p> <ul> <li><a href="/2018/05/24/church-encoded-boolean-values">Church-encoded Boolean values</a></li> <li><a href="/2018/05/28/church-encoded-natural-numbers">Church-encoded natural numbers</a></li> <li><a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a></li> <li><a href="/2018/06/11/church-encoded-either">Church-encoded Either</a></li> <li><a href="/2018/06/18/church-encoded-payment-types">Church-encoded payment types</a></li> <li><a href="/2019/07/29/church-encoded-rose-tree">Church-encoded rose tree</a></li> </ul> </p> <p> These articles give you examples in C#. For Haskell examples, I found <a href="http://programmable.computer">Travis Whitaker</a>'s article <a href="http://programmable.computer/posts/church_encoding.html">Scrap Your Constructors: Church Encoding Algebraic Types</a> useful. </p> <p> All C# code for these articles is <a href="https://github.com/ploeh/ChurchEncoding">available on GitHub</a>. </p> <h3 id="36ea9ca3c38842ed906e2b02d175d116"> Summary <a href="#36ea9ca3c38842ed906e2b02d175d116" title="permalink">#</a> </h3> <p> You can use lambda expressions to define all sorts of data types and computations. Because lambda calculus is a universal model of computation, you can learn about fundamental representations of computation. Particularly, lambda calculus offers a model of logical branching, which again teaches us how to model sum types. </p> <p> <strong>Next:</strong> <a href="/2018/05/24/church-encoded-boolean-values">Church-encoded Boolean values</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e42f37bb7e4440609b922f07211c591c"> <div class="comment-author"><a href="http://github.com/jamesfoster">James Foster</a> <a href="#e42f37bb7e4440609b922f07211c591c">#</a></div> <div class="comment-content">Hey Mark, Just watched your Humane Code series so far on <a href="https://cleancoders.com">cleancoders.com</a>. Really enjoying it. Looking forward to the next episode with much anticipation!<br> <br> James</div> <div class="comment-date">2018-05-24 12:42 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Composite as a monoid - a business rules example https://blog.ploeh.dk/2018/05/17/composite-as-a-monoid---a-business-rules-example 2018-05-17T06:45:00+00:00 Mark Seemann <div id="post"> <p> <em>Composites are monoids. An example in C#, F#, and Haskell.</em> </p> <p> Towards the end of the first decade of the third millennium, I'd been writing object-oriented code for about ten years, and I'd started to notice some patterns in my code. I'd read <a href="http://amzn.to/XBYukB">Design Patterns</a> 6-7 years earlier, but I noticed that I tended to use only a small subset of the patterns from the book - particularly <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a>, <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a>, <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">Chain of Responsibility</a>, and a few others. </p> <p> In particular, I noticed that modelling seemed to be easier, and the code better structured, when I could apply the Composite design pattern. It was also clear, however, that I couldn't always use the Composite pattern, so I started to speculate on what could be the distinguishing factors. In 2010, I made <a href="/2010/12/03/Towardsbetterabstractions">a first attempt at identifying when a Composite is possible</a>, and when it isn't. Unfortunately, while it was a fine attempt (which I'll return to later), it didn't lead anywhere. Ultimately, I gave up on the subject and moved on to other things. </p> <h3 id="84b7af9ddccf4ea7a113f356039d582d"> A revelation <a href="#84b7af9ddccf4ea7a113f356039d582d" title="permalink">#</a> </h3> <p> One of my interests in the next decade became functional programming. One day in late 2016 I came across <a href="https://codereview.stackexchange.com/q/149559/3878">this Code Review question</a> by <a href="https://bizmonger.wordpress.com">Scott Nimrod</a>. It was an solution to <a href="http://codekata.com/kata/kata16-business-rules">the Business Rules kata</a>, which, briefly told, is about implementing changing business rules in a sustainable manner. </p> <p> In my answer to the question, I gave an outline (repeated below) of how I would address the problem in <a href="http://fsharp.org">F#</a>. As a comment to my answer, Scott wrote: </p> <p> "Feels like the Decorator Pattern..." </p> <p> I responded, </p> <p> "Not really; it's the Composite pattern..." </p> <p> A few days later, as I was doing something else, it suddenly dawned on me that not only was a few lines of F# code equivalent to the Composite design pattern, but those lines of code were also manifestations of fundamental abstractions from <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>. Originally, I thought Composite was a combination of <a href="/2018/10/01/applicative-functors">applicative functors</a> and <a href="/2017/10/06/monoids">monoids</a>, but as I investigated, I discovered that <a href="/2018/03/12/composite-as-a-monoid">Composites are simply monoids</a>. </p> <p> This article shows a concrete example of that discovery, starting with my original F# code, subsequently translating it to C# to demonstrate that it's a Composite, and concluding with a translation to <a href="https://www.haskell.org">Haskell</a> in order to demonstrate that it all fits with the formalisation of <code>Monoid</code> there. </p> <h3 id="b2833023b804484db532f3626bad6b0c"> Original F# solution outline <a href="#b2833023b804484db532f3626bad6b0c" title="permalink">#</a> </h3> <p> The kata is about modelling volatile business rules in a sustainable manner. Particularly, you must implement various business rules associated with payments for products and services. Making a rough outline of a model, I started by introducing some types in F#: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Membership&nbsp;=&nbsp;Basic&nbsp;|&nbsp;Gold <span style="color:blue;">type</span>&nbsp;Good&nbsp;= |&nbsp;PhysicalProduct&nbsp;<span style="color:blue;">of</span>&nbsp;string |&nbsp;Book&nbsp;<span style="color:blue;">of</span>&nbsp;string |&nbsp;Video&nbsp;<span style="color:blue;">of</span>&nbsp;string |&nbsp;Membership&nbsp;<span style="color:blue;">of</span>&nbsp;Membership |&nbsp;Upgrade <span style="color:blue;">type</span>&nbsp;Command&nbsp;= |&nbsp;Slip&nbsp;<span style="color:blue;">of</span>&nbsp;string&nbsp;*&nbsp;(Good&nbsp;list) |&nbsp;Activate&nbsp;<span style="color:blue;">of</span>&nbsp;Membership |&nbsp;Upgrade |&nbsp;PayAgent</pre> </p> <p> This basically states that there's a closed hierarchy of goods, and a closed hierarchy of business commands, as well as a <code>Membership</code> enumeration. A <em>good</em> can be a physical product with a name, a book with a name, a membership or upgrade, and so on. A <em>command</em> can be a packing slip, a membership activation, and so on. </p> <p> Since I was only interested in a rough outline of a solution, I only sketched four business rules, all implemented as functions. The first creates a packing slip for certain goods: </p> <p> <pre><span style="color:green;">//&nbsp;Good&nbsp;-&gt;&nbsp;Command&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;slipForShipping&nbsp;=&nbsp;<span style="color:blue;">function</span> |&nbsp;PhysicalProduct&nbsp;name&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[Slip&nbsp;(<span style="color:#a31515;">&quot;Shipping&quot;</span>,&nbsp;[PhysicalProduct&nbsp;name])] |&nbsp;Book&nbsp;name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[Slip&nbsp;(<span style="color:#a31515;">&quot;Shipping&quot;</span>,&nbsp;[Book&nbsp;name])] |&nbsp;Video&nbsp;name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[Slip&nbsp;(<span style="color:#a31515;">&quot;Shipping&quot;</span>,&nbsp;[Video&nbsp;name])] |&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[]</pre> </p> <p> This function takes a <code>Good</code> value as input and returns a list of <code>Command</code> values as output. If the <code>Good</code> is a <code>PhysicalProduct</code>, <code>Book</code>, or <code>Video</code>, it returns a packing slip command; otherwise, it returns an empty list of commands. </p> <p> The next business rule is a similar function: </p> <p> <pre><span style="color:green;">//&nbsp;Good&nbsp;-&gt;&nbsp;Command&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;slipForRoyalty&nbsp;=&nbsp;<span style="color:blue;">function</span> |&nbsp;Book&nbsp;name&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[Slip&nbsp;(<span style="color:#a31515;">&quot;Royalty&quot;</span>,&nbsp;[Book&nbsp;name])] |&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[]</pre> </p> <p> This business rule generates a royalty slip for any <code>Book</code>, but does nothing for any other <code>Good</code>. </p> <p> The third business rule activates a membership: </p> <p> <pre><span style="color:green;">//&nbsp;Good&nbsp;-&gt;&nbsp;Command&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;activate&nbsp;=&nbsp;<span style="color:blue;">function</span>&nbsp;|&nbsp;Membership&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[Activate&nbsp;x]&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[]</pre> </p> <p> If the <code>Good</code> is a <code>Membership</code>, the <code>activate</code> function returns a list containing a single <code>Activate</code> command; otherwise, it returns an empty list. </p> <p> Finally, the last rule upgrades a membership: </p> <p> <pre><span style="color:green;">//&nbsp;Good&nbsp;-&gt;&nbsp;Command&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;upgrade&nbsp;=&nbsp;<span style="color:blue;">function</span>&nbsp;|&nbsp;Good.Upgrade&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[Upgrade]&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[]</pre> </p> <p> Similar to the previous functions, this one looks at the type of <code>Good</code>, and returns an <code>Upgrade</code> command when the input is an <code>Upgrade</code> good, and an empty list otherwise. </p> <p> Notice that all four functions share the same type: <code>Good -&gt; Command list</code>. I designed them like that on purpose, because this enables you to compose a list of business rules to a function that looks like a single rule: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;list)&nbsp;list&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;handle&nbsp;rules&nbsp;good&nbsp;=&nbsp;List.collect&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;r&nbsp;good)&nbsp;rules</pre> </p> <p> This <code>handle</code> function takes a list of business rules (<code>rules</code>) and returns a new function with the type <code>Good -&gt; Command list</code> (or, actually, a function with the type <code>'a -&gt; 'b list</code> - once again I've fallen into the trap of <a href="/2015/08/17/when-x-y-and-z-are-great-variable-names">using too descriptive names</a>). Notice that this is the same type as the individual rules. </p> <p> You can now compose the four specific business rules: </p> <p> <pre><span style="color:green;">//&nbsp;Good&nbsp;-&gt;&nbsp;Command&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;handleAll&nbsp;=&nbsp;handle&nbsp;[slipForShipping;&nbsp;slipForRoyalty;&nbsp;activate;&nbsp;upgrade]</pre> </p> <p> This function also has the type <code>Good -&gt; Command list</code> although it's a composition of four rules. </p> <p> You can use it like this <em>F# Interactive</em> example: </p> <p> <pre>&gt; handleAll (Book "The Annotated Turing");; val it : Command list = [Slip ("Shipping",[Book "The Annotated Turing"]); Slip ("Royalty",[Book "The Annotated Turing"])]</pre> </p> <p> (Yes, I like <a href="http://amzn.to/2n9MFGh">The Annotated Turing</a> - read <a href="https://www.goodreads.com/review/show/1731926050">my review on Goodreads</a>.) </p> <p> Notice that while each of the business rules produces only zero or one <code>Command</code> values, in this example <code>handleAll</code> returns two <code>Command</code> values. </p> <p> This design, where a composition looks like the things it composes, sounds familiar. </p> <h3 id="3d1128189a4a4e6ebbb5b1358e85a11f"> Business rules in C# <a href="#3d1128189a4a4e6ebbb5b1358e85a11f" title="permalink">#</a> </h3> <p> You can translate the above F# model to an object-oriented model in C#. Translating <a href="https://en.wikipedia.org/wiki/Tagged_union">discriminated unions</a> like <code>Good</code> and <code>Command</code> to C# always involves compromises. In order to keep the example as simple as possible, I decided to translate each of those to a <a href="https://en.wikipedia.org/wiki/Marker_interface_pattern">marker interface</a>, although I loathe that 'pattern': </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IGood</span>&nbsp;{&nbsp;}</pre> </p> <p> While the interface doesn't afford any behaviour, various classes can still implement it, like, for example, <code>Book</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Book</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IGood</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Book(<span style="color:blue;">string</span>&nbsp;name) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;name; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> Other <code>IGood</code> 'implementations' looks similar, and there's a comparable class hierarchy for <code>ICommand</code>, which is another marker interface. </p> <p> The above F# code used a shared function type of <code>Good -&gt; Command list</code> as a polymorphic type for a business rule. You can <a href="/2018/01/08/software-design-isomorphisms">translate that to a C# interface</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRule</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">ICommand</span>&gt;&nbsp;Handle(<span style="color:#2b91af;">IGood</span>&nbsp;good); }</pre> </p> <p> The above <code>slipForShipping</code> function becomes a class that implements the <code>IRule</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SlipForShippingRule</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IRule</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">ICommand</span>&gt;&nbsp;Handle(<span style="color:#2b91af;">IGood</span>&nbsp;good) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(good&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">PhysicalProduct</span>&nbsp;|| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;good&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Book</span>&nbsp;|| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;good&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:#2b91af;">Video</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SlipCommand</span>(<span style="color:#a31515;">&quot;Shipping&quot;</span>,&nbsp;good)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ICommand</span>[0]; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Instead of pattern matching on a discriminated union, the <code>Handle</code> method examines the subtype of <code>good</code> and only returns a <code>SlipCommand</code> if the <code>good</code> is either a <code>PhysicalProduct</code>, a <code>Book</code>, or a <code>Video</code>. </p> <p> The other implementations are similar, so I'm not going to show all of them, but here's one more: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ActivateRule</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IRule</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">ICommand</span>&gt;&nbsp;Handle(<span style="color:#2b91af;">IGood</span>&nbsp;good) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;good&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:#2b91af;">MembershipGood</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(m&nbsp;!=&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ActivateCommand</span>(m.Membership)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ICommand</span>[0]; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since 'all' members of <code>IRule</code> return <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">collections, which form monoids over concatenation</a>, the interface itself gives rise to a monoid. This means that you can create a Composite: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CompositeRule</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IRule</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IRule</span>[]&nbsp;rules; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;CompositeRule(<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">IRule</span>[]&nbsp;rules) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.rules&nbsp;=&nbsp;rules; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">ICommand</span>&gt;&nbsp;Handle(<span style="color:#2b91af;">IGood</span>&nbsp;good) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;commands&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">ICommand</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;rule&nbsp;<span style="color:blue;">in</span>&nbsp;rules) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;commands.AddRange(rule.Handle(good)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;commands; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice how the implementation of <code>Handle</code> follows the <a href="/2017/11/20/monoids-accumulate">template for monoid accumulation</a>. It starts with the <em>identity</em>, which, for the collection concatenation monoid is the empty collection. It then loops through all the composed <code>rules</code> and updates the accumulator <code>commands</code> in each iteration. Here, I used <code>AddRange</code>, which mutates <code>commands</code> instead of returning a new value, but the result is the same. Finally, the method returns the accumulator. </p> <p> You can now compose all the business rules and use the composition as though it was a single object: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;rule&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CompositeRule</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SlipForShippingRule</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SlipForRoyaltyRule</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ActivateRule</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UpgradeRule</span>()); <span style="color:blue;">var</span>&nbsp;book&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Book</span>(<span style="color:#a31515;">&quot;The&nbsp;Annotated&nbsp;Turing&quot;</span>); <span style="color:blue;">var</span>&nbsp;commands&nbsp;=&nbsp;rule.Handle(book);</pre> </p> <p> When the method returns, <code>commands</code> contains two <code>SlipCommand</code> objects - a packing slip, and a royalty slip. </p> <h3 id="e890108dc2534b97a57a843f60e5a01f"> Business rules in Haskell <a href="#e890108dc2534b97a57a843f60e5a01f" title="permalink">#</a> </h3> <p> You can also port the F# code to Haskell, which is usually easy as long as the F# is written in a 'functional style'. Since Haskell has an explicit notion of monoids, you'll be able to see how the two above solutions are monoidal. </p> <p> The data types are easy to translate to Haskell - you only have to adjust the syntax a bit: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;Membership&nbsp;=&nbsp;Basic&nbsp;|&nbsp;Gold&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Enum</span>,&nbsp;<span style="color:#2b91af;">Bounded</span>) <span style="color:blue;">data</span>&nbsp;Good&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;PhysicalProduct&nbsp;String &nbsp;&nbsp;|&nbsp;Book&nbsp;String &nbsp;&nbsp;|&nbsp;Video&nbsp;String &nbsp;&nbsp;|&nbsp;Membership&nbsp;Membership &nbsp;&nbsp;|&nbsp;UpgradeGood &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>) <span style="color:blue;">data</span>&nbsp;Command&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;Slip&nbsp;String&nbsp;[Good] &nbsp;&nbsp;|&nbsp;Activate&nbsp;Membership &nbsp;&nbsp;|&nbsp;Upgrade &nbsp;&nbsp;|&nbsp;PayAgent &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>)</pre> </p> <p> The business rule functions are also easy to translate: </p> <p> <pre><span style="color:#2b91af;">slipForShipping</span>&nbsp;::&nbsp;<span style="color:blue;">Good</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Command</span>] slipForShipping&nbsp;pp@(PhysicalProduct&nbsp;_)&nbsp;=&nbsp;[Slip&nbsp;<span style="color:#a31515;">&quot;Shipping&quot;</span>&nbsp;[pp]] slipForShipping&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b@(Book&nbsp;_)&nbsp;=&nbsp;[Slip&nbsp;<span style="color:#a31515;">&quot;Shipping&quot;</span>&nbsp;&nbsp;[b]] slipForShipping&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v@(Video&nbsp;_)&nbsp;=&nbsp;[Slip&nbsp;<span style="color:#a31515;">&quot;Shipping&quot;</span>&nbsp;&nbsp;[v]] slipForShipping&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;&nbsp;=&nbsp;<span style="color:blue;">[]</span> <span style="color:#2b91af;">slipForRoyalty</span>&nbsp;::&nbsp;<span style="color:blue;">Good</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Command</span>] slipForRoyalty&nbsp;b@(Book&nbsp;_)&nbsp;=&nbsp;[Slip&nbsp;<span style="color:#a31515;">&quot;Royalty&quot;</span>&nbsp;[b]] slipForRoyalty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;&nbsp;=&nbsp;<span style="color:blue;">[]</span> <span style="color:#2b91af;">activate</span>&nbsp;::&nbsp;<span style="color:blue;">Good</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Command</span>] activate&nbsp;(Membership&nbsp;m)&nbsp;=&nbsp;[Activate&nbsp;m] activate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;&nbsp;=&nbsp;<span style="color:blue;">[]</span> <span style="color:#2b91af;">upgrade</span>&nbsp;::&nbsp;<span style="color:blue;">Good</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Command</span>] upgrade&nbsp;(UpgradeGood)&nbsp;=&nbsp;[Upgrade] upgrade&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;&nbsp;=&nbsp;<span style="color:blue;">[]</span></pre> </p> <p> Notice that all four business rules share the same type: <code>Good -&gt; [Command]</code>. This is conceptually the same type as in the F# code; instead of writing <code>Command list</code>, which is the F# syntax, the Haskell syntax for a list of <code>Command</code> values is <code>[Command]</code>. </p> <p> All those functions <a href="/2017/11/06/function-monoids">are monoids because their return types form a monoid</a>, so in Haskell, you can compose them without further ado: </p> <p> <pre><span style="color:#2b91af;">handleAll</span>&nbsp;::&nbsp;<span style="color:blue;">Good</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Command</span>] handleAll&nbsp;=&nbsp;mconcat&nbsp;[slipForShipping,&nbsp;slipForRoyalty,&nbsp;activate,&nbsp;upgrade]</pre> </p> <p> <code>mconcat</code> is a built-in function that aggregates any list of monoidal values to a single value: </p> <p> <pre>mconcat :: Monoid a =&gt; [a] -&gt; a</pre> </p> <p> Since all four functions are monoids, this just works out of the box. A Composite is just a monoid. Here's an example of using <code>handleAll</code> from GHCi: </p> <p> <pre>*BusinessRules&gt; handleAll $ Book "The Annotated Turing" [Slip "Shipping" [Book "The Annotated Turing"],Slip "Royalty" [Book "The Annotated Turing"]]</pre> </p> <p> The result is as you'd come to expect. </p> <p> Notice that not only don't you have to write a <code>CompositeRule</code> class, you don't even have to write a <code>handle</code> helper function. Haskell already understands monoids, so composition happens automatically. </p> <p> If you prefer, you could even skip the <code>handle</code> function too: </p> <p> <pre>*BusinessRules&gt; mconcat [slipForShipping, slipForRoyalty, activate, upgrade] $ Book "Blindsight" [Slip "Shipping" [Book "Blindsight"],Slip "Royalty" [Book "Blindsight"]]</pre> </p> <p> (Yes, you should also read <a href="/ref/blindsight">Blindsight</a>.) </p> <p> It's not that composition as such is built into Haskell, but rather that the language is designed around a powerful ability to model abstractions, and one of the built-in abstractions just happens to be monoids. You could argue, however, that many of Haskell's fundamental abstractions are built from category theory, and one of the fundamental characteristics of a category is how morphisms compose. </p> <h3 id="2b8ab9b2722940d093de8f7b4db7868d"> Summary <a href="#2b8ab9b2722940d093de8f7b4db7868d" title="permalink">#</a> </h3> <p> Composite are monoids. This article shows an example, starting with a solution in F#. You can translate the F# code to object-oriented C# and model the composition of business rules as a Composite. You can also translate the F# code 'the other way', to the strictly functional language Haskell, and thereby demonstrate that the solution is based on a monoid. </p> <p> <em>This article is a repost of <a href="https://blog.ndcconferences.com/composite-as-a-monoid-a-business-rules-example">a guest post on the NDC blog</a>, reproduced here with kind permission.</em> </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Project Arbitraries with view patterns https://blog.ploeh.dk/2018/05/14/project-arbitraries-with-view-patterns 2018-05-14T08:07:00+00:00 Mark Seemann <div id="post"> <p> <em>Write expressive property-based test with QuickCheck and view patterns.</em> </p> <p> Recently, I was writing some <a href="https://hackage.haskell.org/package/QuickCheck/docs/Test-QuickCheck.html">QuickCheck</a>-based tests of some business logic, and since the business logic in question involved a custom domain type called <code>Reservation</code>, I had to write an <code>Arbitrary</code> instance for it. Being a dutiful <a href="https://www.haskell.org">Haskell</a> programmer, I wrapped it in a <code>newtype</code> in order to prevent warnings about orphaned instances: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;ArbReservation&nbsp;=&nbsp;ArbReservation&nbsp;{&nbsp;getReservation&nbsp;::&nbsp;Reservation&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Arbitrary</span>&nbsp;<span style="color:blue;">ArbReservation</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;arbitrary&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;(d,&nbsp;e,&nbsp;n,&nbsp;Positive&nbsp;q,&nbsp;b)&nbsp;&lt;-&nbsp;arbitrary &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;ArbReservation&nbsp;$&nbsp;Reservation&nbsp;d&nbsp;e&nbsp;n&nbsp;q&nbsp;b </pre> </p> <p> This is all fine as long as you just need one <code>Reservation</code> in a test, because in that case, you can simply pattern-match it out of <code>ArbReservation</code>: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;tryAccept&nbsp;reservation&nbsp;in&nbsp;the&nbsp;past&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(Positive&nbsp;capacity)&nbsp;(ArbReservation&nbsp;reservation) &nbsp;&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stub&nbsp;(IsReservationInFuture&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;False &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub&nbsp;(ReadReservations&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;<span style="color:blue;">[]</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub&nbsp;(Create&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;iter&nbsp;stub&nbsp;$&nbsp;runMaybeT&nbsp;$&nbsp;tryAccept&nbsp;capacity&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;&nbsp;isNothing&nbsp;actual </pre> </p> <p> Here, <code>reservation</code> is a <code>Reservation</code> value because it was pattern-matched out of <code>ArbReservation reservation</code>. That's just like <code>capacity</code> is an <code>Int</code>, because it was pattern-matched out of <code>Positive capacity</code>. </p> <p> Incidentally, in the spirit of <a href="/2018/05/07/inlined-hunit-test-lists">the previous article</a>, I'm here using in-lined properties implemented as lambda expressions. The lambda expressions use non-idiomatic formatting in order to make the tests more readable (and to prevent horizontal scrolling), but the gist of the matter is that the entire expression has the type <code>Positive Int -&gt; ArbReservation -&gt; Bool</code>. This is a <code>Testable</code> property because all the input types have <code>Arbitrary</code> instances. </p> <h3 id="e8b8ac40cade45db95315ebb8fa19ebc"> Discommodity creeps in <a href="#e8b8ac40cade45db95315ebb8fa19ebc" title="permalink">#</a> </h3> <p> That's fine for that test case, but for the next, I needed not only a single <code>Reservation</code> value, but also a list of <code>Reservation</code> values. Again, with QuickCheck, you can't write a property with a type like <code>Positive Int -&gt; [Reservation] -&gt; ArbReservation -&gt; Bool</code>, because there's no <code>Arbitrary</code> instance for <code>[Reservation]</code>. Instead, you'll need a property with the type <code>Positive Int -&gt; [ArbReservation] -&gt; ArbReservation -&gt; Bool</code>. </p> <p> One way to do that is to write the property like this: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;tryAccept&nbsp;reservation&nbsp;when&nbsp;capacity&nbsp;is&nbsp;insufficient&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(Positive&nbsp;i) &nbsp;&nbsp;reservations &nbsp;&nbsp;(ArbReservation&nbsp;reservation) &nbsp;&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stub&nbsp;(IsReservationInFuture&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub&nbsp;(ReadReservations&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;$&nbsp;getReservation&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub&nbsp;(Create&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;$&nbsp;reservationQuantity&nbsp;&lt;$&gt;&nbsp;getReservation&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;capacity&nbsp;=&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;-&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;iter&nbsp;stub&nbsp;$&nbsp;runMaybeT&nbsp;$&nbsp;tryAccept&nbsp;capacity&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;&nbsp;isNothing&nbsp;actual </pre> </p> <p> Here, <code>reservations</code> has type <code>[ArbReservation]</code>, so every time the test needs to operate on the values, it first has to pull the <code>Reservation</code> values out of it using <code>getReservation &lt;$&gt; reservations</code>. That seems unnecessarily verbose and repetitive, so it'd be nice if a better option was available. </p> <h3 id="49c9bc081e7b41598fa7bb9052b747d0"> View pattern <a href="#49c9bc081e7b41598fa7bb9052b747d0" title="permalink">#</a> </h3> <p> Had I been writing <a href="http://fsharp.org">F#</a> code, I'd immediately be reaching for an <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/active-patterns">active pattern</a>, but this is Haskell. If there's one thing, though, I've learned about Haskell so far, it's that, if F# can do something, there's a very good chance Haskell can do it too - only, it may be called something else. </p> <p> With a vague sense that I'd seen something similar in some Haskell code somewhere, I went looking, and about fifteen minutes later I'd found what I was looking for: a little language extension called <em>view patterns</em>. Just add the language extension to the top of the file where you want to use it: </p> <p> <pre>{-#&nbsp;<span style="color:gray;">LANGUAGE</span>&nbsp;ViewPatterns&nbsp;#-}</pre> </p> <p> You can now change the property to pattern-match <code>reservations</code> out of a function call, so to speak: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;tryAccept&nbsp;reservation&nbsp;when&nbsp;capacity&nbsp;is&nbsp;insufficient&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(Positive&nbsp;i) &nbsp;&nbsp;(<span style="color:blue;">fmap</span>&nbsp;getReservation&nbsp;-&gt;&nbsp;reservations) &nbsp;&nbsp;(ArbReservation&nbsp;reservation) &nbsp;&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stub&nbsp;(IsReservationInFuture&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub&nbsp;(ReadReservations&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub&nbsp;(Create&nbsp;_&nbsp;next)&nbsp;=&nbsp;next&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;$&nbsp;reservationQuantity&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;capacity&nbsp;=&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;-&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual&nbsp;=&nbsp;iter&nbsp;stub&nbsp;$&nbsp;runMaybeT&nbsp;$&nbsp;tryAccept&nbsp;capacity&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;&nbsp;isNothing&nbsp;actual </pre> </p> <p> The function <code>getReservation</code> has the type <code>ArbReservation -&gt; Reservation</code>, so <code>fmap getReservation</code> is a partially applied function with the type <code>[ArbReservation] -&gt; [Reservation]</code>. In order to be able to call the overall lambda expression, the caller must supply an <code>[ArbReservation]</code> value to the view pattern, which means that the type of that argument must be <code>[ArbReservation]</code>. The view pattern then immediately unpacks the result of the function and gives you <code>reservations</code>, which is the return value of calling <code>fmap getReservation</code> with the input value(s). Thus, <code>reservations</code> has the type <code>[Reservation]</code>. </p> <p> The type of the entire property is now <code>Positive Int -&gt; [ArbReservation] -&gt; ArbReservation -&gt; Bool</code>. </p> <p> This removes some noise from the body of the property, so I find that this is a useful trick in this particular situation. </p> <h3 id="02cafa3f94eb4fe19e1ad19f7448d258"> Summary <a href="#02cafa3f94eb4fe19e1ad19f7448d258" title="permalink">#</a> </h3> <p> You can use the <em>view patterns</em> GHC language extension when you need to write a function that takes an argument of a particular type, but you never care about the original input, but instead immediately wish to project it to a different value. </p> <p> I haven't had much use for it before, but it seems to be useful in the context of QuickCheck properties. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="81bcbcc0ff9142018fce53c744264ee2"> <div class="comment-author"><a href="https://benjamin.pizza/">Benjamin Hodgson</a> <a href="#81bcbcc0ff9142018fce53c744264ee2">#</a></div> <div class="comment-content"> <p> I've seen folks wrap up the view pattern in a <a href="https://downloads.haskell.org/~ghc/8.4.2/docs/html/users_guide/glasgow_exts.html#pattern-synonyms">pattern synonym</a>: </p> <p> <pre><code> pattern ArbReservations :: [Reservation] -> [ArbReservation] pattern ArbReservations rs <- (coerce -> rs) where ArbReservations rs = coerce rs foo :: [ArbReservation] -> IO () foo (ArbReservations rs) = traverse print rs </code></pre> </p> <p> (<code>coerce</code> is usually more efficient than <code>fmap</code>.) </p> <p> OTOH I don't think orphan instances of <code>Arbitrary</code> are very harmful. It's unlikely that they'll get accidentally imported or overlap, because <code>Arbitrary</code> is purely used for testing. So in this specific case I'd probably just stick with an orphan instance and turn off the warning for that file. </p> </div> <div class="comment-date">2018-05-26 10:55 UTC</div> </div> <div class="comment" id="85761f5529fd4806a4e4202ed4b319c5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#85761f5529fd4806a4e4202ed4b319c5">#</a></div> <div class="comment-content"> <p> Benjamin, thank you for the <em>pattern synonyms</em> tip; I'll have to try that next time. </p> <p> Regarding orphaned instances, your point is something I've been considering myself, but I'm still at the point of my Haskell journey where I'm trying to understand the subtleties of the ecosystem, so I wasn't sure whether or not it'd be a good idea to allow orphaned <code>Arbitrary</code> instances. </p> <p> When you suggest turning off the warning for a file, do you mean adding an <code>{-# OPTIONS_GHC -fno-warn-orphans #-}</code> pragma, or did you have some other method in mind? </p> </div> <div class="comment-date">2018-05-27 7:54 UTC</div> </div> <div class="comment" id="dc078779dfd04a8a8411322269516ae0"> <div class="comment-author"><a href="https://benjamin.pizza/">Benjamin Hodgson</a> <a href="#dc078779dfd04a8a8411322269516ae0">#</a></div> <div class="comment-content"> <p> Yep I meant <code>OPTIONS_GHC</code>. </p> <p> Orphan instances are problematic because of the possibility that they'll be imported unintentionally or overlap with another orphan instance. If you import two modules which both define orphan instances for the same type then there's no way for GHC to know which one you meant when you attempt to use them. Since instances aren't named you can't even specify it manually, and the compiler can't check for this scenario ahead of time because of separate compilation. (Non-orphans are guaranteed unique by the fact that you can't import the parent type/class without importing the instance.) </p> <p> In the case of <code>Arbitrary</code> these problems typically don't apply because the class is intended to be used for testing. <code>Arbitrary</code> instances are usually internal to your test project and not exported, so the potential for damage is small. </p> </div> <div class="comment-date">2018-05-27 14:08 UTC</div> </div> <div class="comment" id="295d2df911f1465eb14118085f47b3b1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#295d2df911f1465eb14118085f47b3b1">#</a></div> <div class="comment-content"> <p> Benjamin, thank you for elaborating. That all makes sense to me. </p> </div> <div class="comment-date">2018-05-27 16:06 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Inlined HUnit test lists https://blog.ploeh.dk/2018/05/07/inlined-hunit-test-lists 2018-05-07T12:41:00+00:00 Mark Seemann <div id="post"> <p> <em>An alternative way to organise tests lists with HUnit.</em> </p> <p> In the <a href="/2018/04/30/parametrised-unit-tests-in-haskell">previous article</a> you saw how to write <a href="http://xunitpatterns.com/Parameterized%20Test.html">parametrised test</a> with <a href="https://hackage.haskell.org/package/HUnit">HUnit</a>. While the tests themselves were elegant and readable (in my opinion), the composition of test lists left something to be desired. This article offers a different way to organise test lists. </p> <h3 id="d7a929c8507641759a43055ee6c4659f"> Duplication <a href="#d7a929c8507641759a43055ee6c4659f" title="permalink">#</a> </h3> <p> The main problem is one of duplication. Consider the <code>main</code> function for the test library, as defined in the previous article: </p> <p> <pre>main&nbsp;<span style="color:#666666;">=</span>&nbsp;defaultMain&nbsp;<span style="color:#666666;">$</span>&nbsp;hUnitTestToTests&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">TestList</span>&nbsp;[ &nbsp;&nbsp;<span style="color:#a31515;">&quot;adjustToBusinessHours&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;<span style="color:#666666;">~:</span>&nbsp;adjustToBusinessHoursReturnsCorrectResult, &nbsp;&nbsp;<span style="color:#a31515;">&quot;adjustToDutchBankDay&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;<span style="color:#666666;">~:</span>&nbsp;adjustToDutchBankDayReturnsCorrectResult, &nbsp;&nbsp;<span style="color:#a31515;">&quot;Composed&nbsp;adjust&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;<span style="color:#666666;">~:</span>&nbsp;composedAdjustReturnsCorrectResult&nbsp;]</pre> </p> <p> It annoys me that I have a function with a (somewhat) descriptive name, like <code>adjustToBusinessHoursReturnsCorrectResult</code>, but then I also have to give the test a label - in this case <code>"adjustToBusinessHours returns correct result"</code>. Not only is this duplication, but it also adds an extra maintenance overhead, because if I decide to rename the test, should I also rename the label? </p> <p> Why do you even need the label? When you run the test, that label is printed during the test run, so that you can see what happens: </p> <p> <pre>$ stack test --color never --ta "--plain" ZonedTimeAdjustment-0.1.0.0: test (suite: ZonedTimeAdjustment-test, args: --plain) :adjustToDutchBankDay returns correct result: : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] :adjustToBusinessHours returns correct result: : [OK] : [OK] : [OK] :Composed adjust returns correct result: : [OK] : [OK] : [OK] : [OK] : [OK] Test Cases Total Passed 20 20 Failed 0 0 Total 20 20</pre> </p> <p> I considered it redundant to give each test case in the parametrised tests their own labels, but I could have done that too, if I'd wanted to. </p> <p> What happens if you remove the labels? </p> <p> <pre>main&nbsp;<span style="color:#666666;">=</span>&nbsp;defaultMain&nbsp;<span style="color:#666666;">$</span>&nbsp;hUnitTestToTests&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">TestList</span>&nbsp;<span style="color:#666666;">$</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adjustToBusinessHoursReturnsCorrectResult &nbsp;&nbsp;<span style="color:#666666;">++</span>&nbsp;&nbsp;adjustToDutchBankDayReturnsCorrectResult &nbsp;&nbsp;<span style="color:#666666;">++</span>&nbsp;&nbsp;composedAdjustReturnsCorrectResult</pre> </p> <p> That compiles, but produces output like this: </p> <p> <pre>$ stack test --color never --ta "--plain" ZonedTimeAdjustment-0.1.0.0: test (suite: ZonedTimeAdjustment-test, args: --plain) : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] : [OK] Test Cases Total Passed 20 20 Failed 0 0 Total 20 20</pre> </p> <p> If you don't care about the labels, then that's a fine solution. On the other hand, if you <em>do</em> care about the labels, then a different approach is warranted. </p> <h3 id="9801f758146a43939d1347b64ab092f4"> Inlined test lists <a href="#9801f758146a43939d1347b64ab092f4" title="permalink">#</a> </h3> <p> Looking at an expression like <code>"Composed adjust returns correct result" ~: composedAdjustReturnsCorrectResult</code>, I find <code>"Composed adjust returns correct result"</code> more readable than <code>composedAdjustReturnsCorrectResult</code>, so if I want to reduce duplication, I want to go after a solution that names a test with a label, instead of a solution that names a test with a function name. </p> <p> What is <code>composedAdjustReturnsCorrectResult</code>? It's just the name of a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a> (because its type is <code>[Test]</code>). Since it's <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>, it means that in the test list in <code>main</code>, I can replace the function with its body! I can do this with all three functions, although, in order to keep things simplified, I'm only going to show you two of them: </p> <p> <pre><span style="color:#600277;">main</span>&nbsp;::&nbsp;IO&nbsp;() main&nbsp;<span style="color:#666666;">=</span>&nbsp;defaultMain&nbsp;<span style="color:#666666;">$</span>&nbsp;hUnitTestToTests&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">TestList</span>&nbsp;[ &nbsp;&nbsp;<span style="color:#a31515;">&quot;adjustToBusinessHours&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;<span style="color:#666666;">~:</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;(dt,&nbsp;expected)&nbsp;<span style="color:#666666;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">6</span>,&nbsp;<span style="color:#09885a;">59</span>,&nbsp;&nbsp;<span style="color:#09885a;">4</span>)&nbsp;<span style="color:#09885a;">0</span>,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">9</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">9</span>,&nbsp;<span style="color:#09885a;">42</span>,&nbsp;<span style="color:#09885a;">41</span>)&nbsp;<span style="color:#09885a;">0</span>,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">9</span>,&nbsp;<span style="color:#09885a;">42</span>,&nbsp;<span style="color:#09885a;">41</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">19</span>,&nbsp;<span style="color:#09885a;">1</span>,&nbsp;<span style="color:#09885a;">32</span>)&nbsp;<span style="color:#09885a;">0</span>,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">3</span>)&nbsp;(<span style="color:#09885a;">9</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;<span style="color:#09885a;">0</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;<span style="color:#666666;">=</span>&nbsp;adjustToBusinessHours&nbsp;dt &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;expected&nbsp;<span style="color:#666666;">~=?</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;actual &nbsp;&nbsp;, &nbsp;&nbsp;<span style="color:#a31515;">&quot;Composed&nbsp;adjust&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;<span style="color:#666666;">~:</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;(dt,&nbsp;expected)&nbsp;<span style="color:#666666;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">1</span>,&nbsp;<span style="color:#09885a;">31</span>)&nbsp;(&nbsp;<span style="color:#09885a;">7</span>,&nbsp;<span style="color:#09885a;">45</span>,&nbsp;<span style="color:#09885a;">55</span>)&nbsp;&nbsp;&nbsp;<span style="color:#09885a;">2</span>&nbsp;,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">2</span>,&nbsp;<span style="color:#09885a;">28</span>)&nbsp;(&nbsp;<span style="color:#09885a;">7</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">2</span>,&nbsp;&nbsp;<span style="color:#09885a;">6</span>)&nbsp;(<span style="color:#09885a;">10</span>,&nbsp;&nbsp;<span style="color:#09885a;">3</span>,&nbsp;&nbsp;<span style="color:#09885a;">2</span>)&nbsp;&nbsp;&nbsp;<span style="color:#09885a;">1</span>&nbsp;,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">3</span>,&nbsp;&nbsp;<span style="color:#09885a;">6</span>)&nbsp;(&nbsp;<span style="color:#09885a;">9</span>,&nbsp;&nbsp;<span style="color:#09885a;">3</span>,&nbsp;&nbsp;<span style="color:#09885a;">2</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">2</span>,&nbsp;&nbsp;<span style="color:#09885a;">9</span>)&nbsp;(&nbsp;<span style="color:#09885a;">4</span>,&nbsp;<span style="color:#09885a;">20</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;&nbsp;&nbsp;<span style="color:#09885a;">0</span>&nbsp;,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">3</span>,&nbsp;&nbsp;<span style="color:#09885a;">9</span>)&nbsp;(&nbsp;<span style="color:#09885a;">9</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">2</span>,&nbsp;<span style="color:#09885a;">12</span>)&nbsp;(<span style="color:#09885a;">16</span>,&nbsp;&nbsp;<span style="color:#09885a;">2</span>,&nbsp;<span style="color:#09885a;">11</span>)&nbsp;&nbsp;&nbsp;<span style="color:#09885a;">0</span>&nbsp;,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">3</span>,&nbsp;<span style="color:#09885a;">10</span>)&nbsp;(<span style="color:#09885a;">16</span>,&nbsp;&nbsp;<span style="color:#09885a;">2</span>,&nbsp;<span style="color:#09885a;">11</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">3</span>,&nbsp;<span style="color:#09885a;">14</span>)&nbsp;(<span style="color:#09885a;">13</span>,&nbsp;<span style="color:#09885a;">48</span>,&nbsp;<span style="color:#09885a;">29</span>)&nbsp;(<span style="color:#666666;">-</span><span style="color:#09885a;">1</span>),&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">4</span>,&nbsp;<span style="color:#09885a;">13</span>)&nbsp;(<span style="color:#09885a;">14</span>,&nbsp;<span style="color:#09885a;">48</span>,&nbsp;<span style="color:#09885a;">29</span>)&nbsp;<span style="color:#09885a;">0</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;adjustments&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reverse&nbsp;[adjustToNextMonth,&nbsp;adjustToBusinessHours,&nbsp;adjustToDutchBankDay,&nbsp;adjustToUtc] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;adjust&nbsp;<span style="color:#666666;">=</span>&nbsp;appEndo&nbsp;<span style="color:#666666;">$</span>&nbsp;mconcat&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">Endo</span>&nbsp;<span style="color:#666666;">&lt;$&gt;</span>&nbsp;adjustments &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;<span style="color:#666666;">=</span>&nbsp;adjust&nbsp;dt &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;expected&nbsp;<span style="color:#666666;">~=?</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;actual &nbsp;&nbsp;]</pre> </p> <p> In order to keep the code listing to a reasonable length, I didn't include the third test <code>"adjustToDutchBankDay returns correct result"</code>, but it works in exactly the same way. </p> <p> This is a list with two values. You can see that the values are separated by a <code>,</code>, just like list elements normally are. What's unusual, however, is that each element in the list is defined with a multi-line <code>do</code> block. </p> <p> In C# and F#, I'm used to being able to just write new test functions, and they're automatically picked up by convention and executed by the test runner. I wouldn't be at all surprised if there was a mechanism using Template Haskell that enables something similar, but I find that there's something appealing about treating tests as first-class values all the way. </p> <p> By inlining the tests, I can retain my F# and C# workflow. Just add a new test within the list, and it's automatically picked up by the <code>main</code> function. Not only that, but it's no longer possible to write a test that compiles, but is never executed by the test runner because it has the wrong type. This occasionally happens to me in F#, but with the technique outlined here, if I accidentally give the test the wrong type, it's not going to compile. </p> <h3 id="c9303b8865dc42b0a60bac2b9471fc13"> Conclusion <a href="#c9303b8865dc42b0a60bac2b9471fc13" title="permalink">#</a> </h3> <p> Since HUnit tests are first-class values, you can define them inlined in test lists. For larger code bases, I'd assume that you'd want to spread your unit tests across multiple modules. In that case, I suppose that you could have each test module export a <code>[Test]</code> value. In the test library's <code>main</code> function, you'd need to manually concatenate all the exported test lists together, so a small maintenance burden remains. When you add a new test module, you'd have to add its exported tests to <code>main</code>. </p> <p> I wouldn't be surprised, however, if a clever reader could point out to me how to avoid that as well. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Parametrised unit tests in Haskell https://blog.ploeh.dk/2018/04/30/parametrised-unit-tests-in-haskell 2018-04-30T07:04:00+00:00 Mark Seemann <div id="post"> <p> <em>Here's a way to write parametrised unit tests in Haskell.</em> </p> <p> Sometimes you'd like to execute the same (unit) test for a number of test cases. The only thing that varies is the input values, and the expected outcome. The actual test code is the same for all test cases. Among object-oriented programmers, this is known as a <a href="http://xunitpatterns.com/Parameterized%20Test.html">parametrised test</a>. </p> <p> When I recently searched the web for how to do parametrised tests in <a href="https://www.haskell.org">Haskell</a>, I could only find articles that talked about property-based testing, mostly with <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a>. I normally prefer property-based testing, but sometimes, I'd rather like to run a test with some deterministic test cases that are visible and readable in the code itself. </p> <p> Here's one way I found that I could do that in Haskell. </p> <h3 id="1717430a5a3048629121ab7831c1339d"> Testing date and time adjustments in C# <a href="#1717430a5a3048629121ab7831c1339d" title="permalink">#</a> </h3> <p> In <a href="/2017/11/13/endomorphism-monoid">an earlier article</a>, I discussed how to model date and time adjustments as a <a href="/2017/10/06/monoids">monoid</a>. The example code was written in C#, and I used a few tests to demonstrate that the composition of adjustments work as intended: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-01-31T07:45:55+2&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-02-28T07:00:00Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-06T10:03:02+1&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-06T09:03:02Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-09T04:20:00Z&quot;</span>&nbsp;,&nbsp;<span style="color:#a31515;">&quot;2017-03-09T09:00:00Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-12T16:02:11Z&quot;</span>&nbsp;,&nbsp;<span style="color:#a31515;">&quot;2017-03-10T16:02:11Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-14T13:48:29-1&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-04-13T14:48:29Z&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AccumulatedAdjustReturnsCorrectResult( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;dtS, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;expectedS) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dt&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Parse(dtS); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffsetAdjustment</span>.Accumulate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NextMonthAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BusinessHoursAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DutchBankDayAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UtcAdjustment</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Adjust(dt); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#2b91af;">DateTimeOffset</span>.Parse(expectedS),&nbsp;actual); }</pre> </p> <p> The above parametrised test uses <a href="https://xunit.net">xUnit.net</a> (particularly its <code>Theory</code> feature) to execute the same test code for five test cases. Here's another example: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-10-02T06:59:04Z&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-10-02T09:00:00Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-10-02T09:42:41Z&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-10-02T09:42:41Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-10-02T19:01:32Z&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-10-03T09:00:00Z&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AdjustReturnsCorrectResult(<span style="color:blue;">string</span>&nbsp;dts,&nbsp;<span style="color:blue;">string</span>&nbsp;expectedS) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dt&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Parse(dts); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BusinessHoursAdjustment</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Adjust(dt); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#2b91af;">DateTimeOffset</span>.Parse(expectedS),&nbsp;actual); }</pre> </p> <p> This one covers the three code paths through <code>BusinessHoursAdjustment.Adjust</code>. </p> <p> These tests are similar, so they share some good and bad qualities. </p> <p> On the positive side, I like how <em>readable</em> such tests are. The test code is only a few lines of code, and the test cases (input, and expected output) are in close proximity to the code. Unless you're on a phone with a small screen, you can easily see all of it at once. </p> <p> For a problem like this, I felt that I preferred examples rather than using property-based testing. If the date and time is this, then the adjusted result should be that, and so on. When we <em>read</em> code, we tend to prefer examples. Good documentation often contains examples, and for those of us who consider tests documentation, including examples in tests should advance that cause. </p> <p> On the negative side, tests like these still contain noise. Most of it relates to the problem that xUnit.net tests aren't first-class values. These tests actually ought to take a <code>DateTimeOffset</code> value as input, and compare it to another, expected <code>DateTimeOffset</code> value. Unfortunately, <code>DateTimeOffset</code> values aren't constants, so you can't use them in attributes, like the <code>[InlineData]</code> attribute. </p> <p> There are other workarounds than the one I ultimately chose, but none that are as simple (that I'm aware of). Strings are constants, so you can put formatted date and time strings in the <code>[InlineData]</code> attributes, but the cost of doing this is two calls to <code>DateTimeOffset.Parse</code>. Perhaps this isn't a big price, you think, but it does make me wish for something prettier. </p> <h3 id="df8f3de4ba284893b551b470a6e7c579"> Comparing date and time <a href="#df8f3de4ba284893b551b470a6e7c579" title="permalink">#</a> </h3> <p> In order to port the above tests to Haskell, I used <a href="http://haskellstack.org">Stack</a> to create a new project with <a href="https://hackage.haskell.org/package/HUnit">HUnit</a> as the unit testing library. </p> <p> The Haskell equivalent to <code>DateTimeOffset</code> is called <code>ZonedTime</code>. One problem with <code>ZonedTime</code> values is that you can't readily compare them; the type isn't an <code>Eq</code> instance. There are good reasons for that, but for the purposes of unit testing, I wanted to be able to compare them, so I defined this helper data type: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">ZonedTimeEq</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;<span style="color:#dd0000;">ZonedTime</span>&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Eq</span>&nbsp;<span style="color:blue;">ZonedTimeEq</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;(<span style="color:#dd0000;">ZonedTime</span>&nbsp;lt1&nbsp;tz1)&nbsp;<span style="color:#666666;">==</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;(<span style="color:#dd0000;">ZonedTime</span>&nbsp;lt2&nbsp;tz2)&nbsp;<span style="color:#666666;">=</span>&nbsp;lt1&nbsp;<span style="color:#666666;">==</span>&nbsp;lt2&nbsp;<span style="color:#666666;">&amp;&amp;</span>&nbsp;tz1&nbsp;<span style="color:#666666;">==</span>&nbsp;tz2</pre> </p> <p> This enables me to compare two <code>ZonedTimeEq</code> values, which are only considered equal if they represent the same date, the same time, and the same time zone. </p> <h3 id="57e07c4d36a644889271d392581ae0f3"> Test Utility <a href="#57e07c4d36a644889271d392581ae0f3" title="permalink">#</a> </h3> <p> I also added a little function for creating <code>ZonedTime</code> values: </p> <p> <pre>zt&nbsp;(y,&nbsp;mth,&nbsp;d)&nbsp;(h,&nbsp;m,&nbsp;s)&nbsp;tz&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#dd0000;">ZonedTime</span>&nbsp;(<span style="color:#dd0000;">LocalTime</span>&nbsp;(fromGregorian&nbsp;y&nbsp;mth&nbsp;d)&nbsp;(<span style="color:#dd0000;">TimeOfDay</span>&nbsp;h&nbsp;m&nbsp;s))&nbsp;(hoursToTimeZone&nbsp;tz)</pre> </p> <p> The motivation is simply that, as you can tell, creating a <code>ZonedTime</code> value requires a verbose expression. Clearly, the <code>ZonedTime</code> API is flexible, but in order to define some test cases, I found it advantageous to trade readability for flexibility. The <code>zt</code> function enables me to compactly define some <code>ZonedTime</code> values for my test cases. </p> <h3 id="941af2b4f00f4fa7b0683f1590879e61"> Testing business hours in Haskell <a href="#941af2b4f00f4fa7b0683f1590879e61" title="permalink">#</a> </h3> <p> In HUnit, a test is either a <code>Test</code>, a list of <code>Test</code> values, or an impure assertion. For a parametrised test, a <code>[Test]</code> sounded promising. At the beginning, I struggled with finding a readable way to express the tests. I wanted to be able to start with a list of test cases (inputs and expected outputs), and then <code>fmap</code> them to an executable test. At first, the readability goal seemed elusive, until I realised that I can also use <code>do</code> notation with lists (since they're monads): </p> <p> <pre><span style="color:#600277;">adjustToBusinessHoursReturnsCorrectResult</span>&nbsp;::&nbsp;[<span style="color:blue;">Test</span>] adjustToBusinessHoursReturnsCorrectResult&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;(dt,&nbsp;expected)&nbsp;<span style="color:#666666;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">6</span>,&nbsp;<span style="color:#09885a;">59</span>,&nbsp;&nbsp;<span style="color:#09885a;">4</span>)&nbsp;<span style="color:#09885a;">0</span>,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">9</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">9</span>,&nbsp;<span style="color:#09885a;">42</span>,&nbsp;<span style="color:#09885a;">41</span>)&nbsp;<span style="color:#09885a;">0</span>,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">9</span>,&nbsp;<span style="color:#09885a;">42</span>,&nbsp;<span style="color:#09885a;">41</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">2</span>)&nbsp;(<span style="color:#09885a;">19</span>,&nbsp;<span style="color:#09885a;">1</span>,&nbsp;<span style="color:#09885a;">32</span>)&nbsp;<span style="color:#09885a;">0</span>,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">10</span>,&nbsp;<span style="color:#09885a;">3</span>)&nbsp;(<span style="color:#09885a;">9</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;<span style="color:#09885a;">0</span>) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;<span style="color:#666666;">=</span>&nbsp;adjustToBusinessHours&nbsp;dt &nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;expected&nbsp;<span style="color:#666666;">~=?</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;actual</pre> </p> <p> This is the same test as the above C# test named <code>AdjustReturnsCorrectResult</code>, and it's about the same size as well. Since the test is written using <code>do</code> notation, you can take a list of test cases and operate on each test case at a time. Although the test creates a list of tuples, the <code>&lt;-</code> arrow pulls each <code>(ZonedTime, ZonedTime)</code> tuple out of the list and binds it to <code>(dt, expected)</code>. </p> <p> This test literally consists of only three expressions, so according to my normal <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">heuristic for test formatting</a>, I don't even need white space to indicate the three phases of the <a href="http://wiki.c2.com/?ArrangeActAssert">AAA pattern</a>. The first expression sets up the test case <code>(dt, expected)</code>. </p> <p> The next expression exercises the System Under Test - in this case the <code>adjustToBusinessHours</code> function. That's simply a function call. </p> <p> The third expression verifies the result. It uses HUnit's <code>~=?</code> operator to compare the expected and the actual values. Since <code>ZonedTime</code> isn't an <code>Eq</code> instance, both values are converted to <code>ZonedTimeEq</code> values. The <code>~=?</code> operator returns a <code>Test</code> value, and since the entire test takes place inside a <code>do</code> block, you must <code>return</code> it. Since this particular <code>do</code> block takes place inside the list monad, the type of <code>adjustToBusinessHoursReturnsCorrectResult</code> is <code>[Test]</code>. I added the type annotation for the benefit of you, dear reader, but technically, it's not required. </p> <h3 id="45bb86711997438386258c9ddc5d94dc"> Testing the composed function <a href="#45bb86711997438386258c9ddc5d94dc" title="permalink">#</a> </h3> <p> Translating the <code>AccumulatedAdjustReturnsCorrectResult</code> C# test to Haskell follows the same recipe: </p> <p> <pre><span style="color:#600277;">composedAdjustReturnsCorrectResult</span>&nbsp;::&nbsp;[<span style="color:blue;">Test</span>] composedAdjustReturnsCorrectResult&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;(dt,&nbsp;expected)&nbsp;<span style="color:#666666;">&lt;-</span> &nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">1</span>,&nbsp;<span style="color:#09885a;">31</span>)&nbsp;(&nbsp;<span style="color:#09885a;">7</span>,&nbsp;<span style="color:#09885a;">45</span>,&nbsp;<span style="color:#09885a;">55</span>)&nbsp;&nbsp;&nbsp;<span style="color:#09885a;">2</span>&nbsp;,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">2</span>,&nbsp;<span style="color:#09885a;">28</span>)&nbsp;(&nbsp;<span style="color:#09885a;">7</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">2</span>,&nbsp;&nbsp;<span style="color:#09885a;">6</span>)&nbsp;(<span style="color:#09885a;">10</span>,&nbsp;&nbsp;<span style="color:#09885a;">3</span>,&nbsp;&nbsp;<span style="color:#09885a;">2</span>)&nbsp;&nbsp;&nbsp;<span style="color:#09885a;">1</span>&nbsp;,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">3</span>,&nbsp;&nbsp;<span style="color:#09885a;">6</span>)&nbsp;(&nbsp;<span style="color:#09885a;">9</span>,&nbsp;&nbsp;<span style="color:#09885a;">3</span>,&nbsp;&nbsp;<span style="color:#09885a;">2</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">2</span>,&nbsp;&nbsp;<span style="color:#09885a;">9</span>)&nbsp;(&nbsp;<span style="color:#09885a;">4</span>,&nbsp;<span style="color:#09885a;">20</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;&nbsp;&nbsp;<span style="color:#09885a;">0</span>&nbsp;,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">3</span>,&nbsp;&nbsp;<span style="color:#09885a;">9</span>)&nbsp;(&nbsp;<span style="color:#09885a;">9</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>,&nbsp;&nbsp;<span style="color:#09885a;">0</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">2</span>,&nbsp;<span style="color:#09885a;">12</span>)&nbsp;(<span style="color:#09885a;">16</span>,&nbsp;&nbsp;<span style="color:#09885a;">2</span>,&nbsp;<span style="color:#09885a;">11</span>)&nbsp;&nbsp;&nbsp;<span style="color:#09885a;">0</span>&nbsp;,&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">3</span>,&nbsp;<span style="color:#09885a;">10</span>)&nbsp;(<span style="color:#09885a;">16</span>,&nbsp;&nbsp;<span style="color:#09885a;">2</span>,&nbsp;<span style="color:#09885a;">11</span>)&nbsp;<span style="color:#09885a;">0</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">3</span>,&nbsp;<span style="color:#09885a;">14</span>)&nbsp;(<span style="color:#09885a;">13</span>,&nbsp;<span style="color:#09885a;">48</span>,&nbsp;<span style="color:#09885a;">29</span>)&nbsp;(<span style="color:#666666;">-</span><span style="color:#09885a;">1</span>),&nbsp;zt&nbsp;(<span style="color:#09885a;">2017</span>,&nbsp;<span style="color:#09885a;">4</span>,&nbsp;<span style="color:#09885a;">13</span>)&nbsp;(<span style="color:#09885a;">14</span>,&nbsp;<span style="color:#09885a;">48</span>,&nbsp;<span style="color:#09885a;">29</span>)&nbsp;<span style="color:#09885a;">0</span>) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;adjustments&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reverse&nbsp;[adjustToNextMonth,&nbsp;adjustToBusinessHours,&nbsp;adjustToDutchBankDay,&nbsp;adjustToUtc] &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;adjust&nbsp;<span style="color:#666666;">=</span>&nbsp;appEndo&nbsp;<span style="color:#666666;">$</span>&nbsp;mconcat&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">Endo</span>&nbsp;<span style="color:#666666;">&lt;$&gt;</span>&nbsp;adjustments &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;<span style="color:#666666;">=</span>&nbsp;adjust&nbsp;dt &nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;expected&nbsp;<span style="color:#666666;">~=?</span>&nbsp;<span style="color:#dd0000;">ZT</span>&nbsp;actual</pre> </p> <p> The only notable difference is that this unit test consists of five expressions, so according to my formatting heuristic, I inserted some blank lines in order to make it easier to distinguish the three AAA phases from each other. </p> <h3 id="fb38a830884b4a1ba15b29e0b5dffbfb"> Running tests <a href="#fb38a830884b4a1ba15b29e0b5dffbfb" title="permalink">#</a> </h3> <p> I also wrote a third test called <code>adjustToDutchBankDayReturnsCorrectResult</code>, but that one is so similar to the two you've already seen that I see no point showing it here. In order to run all three tests, I define the tests' <code>main</code> function as such: </p> <p> <pre>main&nbsp;<span style="color:#666666;">=</span>&nbsp;defaultMain&nbsp;<span style="color:#666666;">$</span>&nbsp;hUnitTestToTests&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">TestList</span>&nbsp;[ &nbsp;&nbsp;<span style="color:#a31515;">&quot;adjustToBusinessHours&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;<span style="color:#666666;">~:</span>&nbsp;adjustToBusinessHoursReturnsCorrectResult, &nbsp;&nbsp;<span style="color:#a31515;">&quot;adjustToDutchBankDay&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;<span style="color:#666666;">~:</span>&nbsp;adjustToDutchBankDayReturnsCorrectResult, &nbsp;&nbsp;<span style="color:#a31515;">&quot;Composed&nbsp;adjust&nbsp;returns&nbsp;correct&nbsp;result&quot;</span>&nbsp;<span style="color:#666666;">~:</span>&nbsp;composedAdjustReturnsCorrectResult&nbsp;]</pre> </p> <p> This uses <code>defaultMain</code> from <a href="https://hackage.haskell.org/package/test-framework">test-framework</a>, and <code>hUnitTestToTests</code> from <a href="https://hackage.haskell.org/package/test-framework-hunit">test-framework-hunit</a>. </p> <p> I'm not happy about the duplication of text and test names, and the maintenance burden implied by having to explicitly add every test function to the test list. It's too easy to forget to add a test after you've written it. In the next article, I'll demonstrate an alternative way to compose the tests so that duplication is reduced. </p> <h3 id="fd998b62c43942c7901f496b1e4205e0"> Conclusion <a href="#fd998b62c43942c7901f496b1e4205e0" title="permalink">#</a> </h3> <p> Since HUnit tests are first-class values, you can manipulate and compose them just like any other value. That includes passing them around in lists and binding them with <code>do</code> notation. Once you figure out how to write parametrised tests with HUnit, it's easy, readable, and elegant. </p> <p> The overall configuration of the test runner, however, leaves a bit to be desired, so that's the topic for the next article. </p> <p> <strong>Next:</strong> <a href="/2018/05/07/inlined-hunit-test-lists">Inlined HUnit test lists</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Null Object as identity https://blog.ploeh.dk/2018/04/23/null-object-as-identity 2018-04-23T07:55:00+00:00 Mark Seemann <div id="post"> <p> <em>When can you implement a Null Object? When the return types of your methods are monoids.</em> </p> <p> This article is part of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">a series of articles about design patterns and their category theory counterparts</a>. In a <a href="/2018/03/12/composite-as-a-monoid">previous article</a> you learned how there's a strong relationship between the <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a> design pattern and <a href="/2017/10/06/monoids">monoids</a>. In this article you'll see that the <a href="https://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a> pattern is essentially a special case of the Composite pattern. </p> <p> I also think that there's a relationship between monoidal <em>identity</em> and the Null Object pattern similar to the relationship between Composite and monoids in general: </p> <p> <img src="/content/binary/null-object-identity-set-diagram.png" alt="Set diagram showing identity as a subset of Null Object."> </p> <p> Once more, I don't claim that an isomorphism exists. You may be able to produce Null Object examples that aren't monoidal, but on the other hand, I believe that all identities are Null Objects. </p> <h3 id="b082690114af4768ac6171438c7c7657"> Null Object <a href="#b082690114af4768ac6171438c7c7657" title="permalink">#</a> </h3> <p> While the Null Object design pattern isn't one of the patterns covered in <a href="http://amzn.to/XBYukB">Design Patterns</a>, I consider it a <em>structural pattern</em> because Composite is a structural pattern, and Null Object is a special case of Composite. </p> <p> Bobby Woolf's <a href="http://amzn.to/1dEKjcj">original text</a> contains an example in <a href="https://en.wikipedia.org/wiki/Smalltalk">Smalltalk</a>, and while I'm no Smalltalk expert, I think a fair translation to C# would be an interface like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;IsControlWanted(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;IsControlActive(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Startup(); }</pre> </p> <p> The idea behind the Null Object pattern is to add an implementation that 'does nothing', which in the example would be this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NullController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsControlActive() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsControlWanted() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Startup() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Woolf calls his implementation <code>NoController</code>, but I find it more intent-revealing to call it <code>NullController</code>. Both of the Boolean methods return <code>false</code>, and the <code>Startup</code> method literally does nothing. </p> <h3 id="862afa3095a847ba82c234ee1371dd70"> Doing nothing <a href="#862afa3095a847ba82c234ee1371dd70" title="permalink">#</a> </h3> <p> What exactly does it mean to 'do nothing'? In the case of the <code>Startup</code> method, it's clear, because it's a bona fide <a href="https://en.wikipedia.org/wiki/NOP">no-op</a>. This is possible for all methods without return values (i.e. methods that return <code>void</code>), but for all other methods, the compiler will insist on a return value. </p> <p> For <code>IsControlActive</code> and <code>IsControlWanted</code>, Woolf solves this by returning <code>false</code>. </p> <p> Is <code>false</code> always the correct 'do nothing' value for Booleans? And what should you return if a method returns an integer, a string, or a custom type? The original text is vague on that question: <blockquote> "Exactly what "do nothing" means is subjective and depends on the sort of behavior the Client is expecting." </blockquote> Sometimes, you can't get any closer than that, but I think that often it's possible to be more specific. </p> <h3 id="786916d104f949ce913ab94e6a0fafdc"> Doing nothing as identity <a href="#786916d104f949ce913ab94e6a0fafdc" title="permalink">#</a> </h3> <p> From <a href="/2018/01/15/unit-isomorphisms">unit isomorphisms</a> you know that methods without return values are isomorphic to methods that return <em>unit</em>. You also know that <em>unit</em> is a monoid. What does <em>unit</em> and <code>bool</code> have in common? They both form monoids; <code>bool</code>, in fact, forms four different monoids, of which <em>all</em> and <em>any</em> are the best-known. </p> <p> In my experience, you can implement the Null Object pattern by returning various 'do nothing' values, depending on their types: <ul> <li>For <code>bool</code>, return a constant value. Usually, <code>false</code> is the appropriate 'do nothing' value, but it does depend on the semantics of the operation.</li> <li>For <code>string</code>, return <code>""</code>.</li> <li>For collections, return an empty collection.</li> <li>For numbers, return a constant value, such as <code>0</code>.</li> <li>For <code>void</code>, do nothing, which is equivalent to returning <em>unit</em>.</li> </ul> What all of these have in common is that they return the <em>identity</em> of the monoid in question. Keep in mind that for some types, such as <code>bool</code> and <code>int</code>, more than one monoid exist, and the identity depends on which one you pick: <ul> <li>The identity for the <em>any</em> monoid is <code>false</code>.</li> <li>The <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">identity for <code>string</code> is <code>""</code></a>.</li> <li>The identity for collections is the empty collection.</li> <li>The identity for the <em>addition</em> monoid is <code>0</code>.</li> <li>The identity for <em>unit</em> is <em>unit</em>.</li> </ul> Recall what the <em>identity</em> of a monoid is: it's the value that, when applied to another value, doesn't change the other value: </p> <p> <pre>foo.Op(<span style="color:#2b91af;">Foo</span>.Identity)&nbsp;==&nbsp;foo</pre> </p> <p> In other words: the <em>identity does nothing!</em> </p> <p> As a preliminary result, then, when all return values of your interface form monoids, you can create a Null Object. </p> <h3 id="ec81b9f4281e4c4c909d1dc606056967"> Relationship with Composite <a href="#ec81b9f4281e4c4c909d1dc606056967" title="permalink">#</a> </h3> <p> In the previous article, you saw how the Composite design pattern was equivalent with the <a href="https://www.haskell.org">Haskell</a> function <code>mconcat</code>: </p> <p> <pre>mconcat :: Monoid a =&gt; [a] -&gt; a</pre> </p> <p> This function, however, seems more limited than it has to be. It says that if you have a linked list of monoidal values, you can reduce them to a single monoidal value. Is this only true for linked lists? </p> <p> After all, in a language like C#, you'd typically express a Composite as a container of 'some collection' of objects, typically modelled by the <code>IReadOnlyCollection&lt;T&gt;</code> interface. There are several implementations of that interface, including lists, arrays, collections, and so on. </p> <p> It seems as though we ought to be able to generalise <code>mconcat</code>, and, indeed, we can. The <code>Data.Foldable</code> module defines a function called <code>fold</code>: </p> <p> <pre>fold :: (Monoid m, Foldable t) =&gt; t m -&gt; m</pre> </p> <p> Let me decipher that for you. It says that any monoid <code>m</code> contained in a 'foldable' container <code>t</code> can be reduced to a single value (still of the type <code>m</code>). You can read <code>t m</code> as 'a foldable container that contains monoids'. In C#, you could attempt to express it as <code>IFoldable&lt;TMonoid&gt;</code>. </p> <p> In other words, the Composite design pattern is equivalent to <code>fold</code>. Here's how it relates to the Null Object pattern: </p> <p> As a first step, consider methods like those defined by the above <code>IController</code> interface. In order to implement <code>NullController</code>, all of those methods ignore their (non-existent) input and return an identity value. In other words, we're looking for a Haskell function of the type <code>Monoid m =&gt; a -&gt; m</code>; that is: a function that takes input of the type <code>a</code> and returns a monoidal value <code>m</code>. </p> <p> You can do that using <code>fold</code>: </p> <p> <pre><span style="color:#600277;">nullify</span>&nbsp;::&nbsp;<span style="color:blue;">Monoid</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m nullify&nbsp;<span style="color:#666666;">=</span>&nbsp;fold&nbsp;(<span style="color:#dd0000;">Identity</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;const&nbsp;mempty)</pre> </p> <p> Recall that the input to <code>fold</code> must be a <code>Foldable</code> container. The good old <code>Identity</code> type is, among other capabilities, <code>Foldable</code>. That takes care of the container. The value that we put into the container is a single function that ignores its input (<code>const</code>) and always returns the identity value (<code>mempty</code>). The result is a function that always returns the identity value. </p> <p> This demonstrates that Null Object is a special case of Composite, because <code>nullify</code> is a special application of <code>fold</code>. </p> <p> There's no reason to write <code>nullify</code> in such a complicated way, though. You can simplify it like this: </p> <p> <pre><span style="color:#600277;">nullify</span>&nbsp;::&nbsp;<span style="color:blue;">Monoid</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m nullify&nbsp;<span style="color:#666666;">=</span>&nbsp;const&nbsp;mempty</pre> </p> <p> Once you recall, however, that <a href="/2017/11/06/function-monoids">functions are monoids if their return values are monoids</a>, you can simplify it even further: </p> <p> <pre><span style="color:#600277;">nullify</span>&nbsp;::&nbsp;<span style="color:blue;">Monoid</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m nullify&nbsp;<span style="color:#666666;">=</span>&nbsp;mempty</pre> </p> <p> The identity element for monoidal functions is exactly a function that ignores its input and returns <code>mempty</code>. </p> <h3 id="11c3a2cefcb74f7096e588c530a699a8"> Controller identity <a href="#11c3a2cefcb74f7096e588c530a699a8" title="permalink">#</a> </h3> <p> Consider the <code>IController</code> interface. According to <a href="/2018/02/12/object-isomorphisms">object isomorphisms</a>, you can represent this interface as a tuple of three functions: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#dd0000;">Controller</span>&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;(<span style="color:#dd0000;">ControllerState</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Any</span>,&nbsp;<span style="color:#dd0000;">ControllerState</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Any</span>,&nbsp;<span style="color:#dd0000;">ControllerState</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:blue;">()</span>)</pre> </p> <p> This is cheating a little, because the third tuple element (the one that corresponds to <code>Startup</code>) is <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, although I strongly suspect that the intent is that it should mutate the state of the Controller. Two alternatives are to change the function to either <code>ControllerState -&gt; ControllerState</code> or <code>ControllerState -&gt; IO ()</code>, but I'm going to keep things simple. It doesn't change the conclusion. </p> <p> Notice that I've used <code>Any</code> as the return type for the two first tuples. As I've previously covered, Booleans form monoids like <em>any</em> and <em>all</em>. Here, we need to use <code>Any</code>. </p> <p> This tuple is a monoid because all three functions are monoids, and <a href="/2017/10/30/tuple-monoids">a tuple of monoids is itself a monoid</a>. This means that you can easily create a Null Object using <code>mempty</code>: </p> <p> <pre>λ&gt; nullController = mempty :: Controller</pre> </p> <p> The <code>nullController</code> is a triad of functions. You can access them by pattern-matching them, like this: </p> <p> <pre>λ&gt; (isControlWanted, isControlActive, startup) = nullController</pre> </p> <p> Now you can try to call e.g. <code>isControlWanted</code> with various values in order to verify that it always returns false. In this example, I cheated and simply made <code>ControllerState</code> an alias for <code>String</code>: </p> <p> <pre>λ&gt; isControlWanted "foo" Any {getAny = False} λ&gt; isControlWanted "bar" Any {getAny = False}</pre> </p> <p> You'll get similar behaviour from <code>isControlActive</code>, and <code>startup</code> always returns <code>()</code> (<em>unit</em>). </p> <h3 id="e8bd7e04148143039708a179441b7da5"> Relaxation <a href="#e8bd7e04148143039708a179441b7da5" title="permalink">#</a> </h3> <p> As Woolf wrote in the original text, what 'do nothing' means is subjective. I think it's perfectly reasonable to say that monoidal identity fits the description of doing nothing, but you could probably encounter APIs where 'doing nothing' means something else. </p> <p> As an example, consider avatars for online forums, such as Twitter. If you don't supply a picture when you create your profile, you get a default picture. One way to implement such a feature could be by having a 'null' avatar, which is used in place of a proper avatar. Such a default avatar object could (perhaps) be considered a Null Object, but wouldn't necessarily be monoidal. There may not even be a binary operation for avatars. </p> <p> Thus, it's likely that you could encounter or create Null Objects that aren't monoids. That's the reason that I don't claim that a Null Object always is monoidal identity, although I do think that the reverse relationship holds: if it's <em>identity</em>, then it's a Null Object. </p> <h3 id="91d9a7608c884a0d9fe5d01b61d539f5"> Summary <a href="#91d9a7608c884a0d9fe5d01b61d539f5" title="permalink">#</a> </h3> <p> Monoids are associative binary operations with identity. The identity is the value that 'does nothing' in that binary operation, like, for example, <em>0</em> doesn't 'do' anything under addition. Monoids, however, can be more complex than operations on primitive values. Functions, for example, can be monoids as well, as can tuples of functions. </p> <p> The implication of that is that objects can be monoids as well. When you have a monoidal object, then its <em>identity</em> is the 'natural' Null Object. </p> <p> The question was: <em>when can you implement the Null Object pattern?</em> The answer is that you can do that when all involved methods return monoids. </p> <p> <strong>Next:</strong> <a href="/2020/02/17/builder-as-a-monoid">Builder as a monoid</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="37c2c03b75684696bb16afebcbe2458d"> <div class="comment-author"> Ciprian Vilcan <a href="#37c2c03b75684696bb16afebcbe2458d">#</a></div> <div class="comment-content"> <p> All the examples of using Maybe I recall seeing always wrapped a data type that had a neutral element. So Maybe&lt;int&gt; would resolve to 0 or 1, while Maybe&lt;string&gt; to string.Empty. But what about data types that do not have a neutral element? </p> <p> Suppose we have this DDD value object, NonEmptyString, written in C#. It has no public constructor and it is instantiated through a static factory method that returns a Maybe&lt;NonEmptyString&gt; containing an initialized NonEmptyString if the given input is not null or whitespace and None otherwise. </p> <p> How do you treat the None case when calling the maybe.Match method? Since the neutral element for string concatenation is string.empty, an invalid value for this value object, this type has no possibility of having a Null Object.<br> Can this situation be resolved in the functional way (without throwing an exception) or does it warrant throwing an exception? </p> </div> <div class="comment-date">2018-05-07 08:21 UTC</div> </div> <div class="comment" id="be75b93e68a8441f9bf9cea62a1d94a5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#be75b93e68a8441f9bf9cea62a1d94a5">#</a></div> <div class="comment-content"> <p> Ciprian, thank you for writing. I'm not sure I understand what you man by <em>Maybe</em> wrapping a neutral element. I hope that's not how <a href="/2018/03/26/the-maybe-functor">my introduction to Maybe</a> comes across. Could you point to specific examples? </p> <p> If <code>NonEmptyString</code> is, as the name implies, a <code>string</code> guaranteed to be non-empty, isn't it just a specialisation of <a href="/2017/12/11/semigroups-accumulate/#68b942712506472ebe4c6933ca7dbd56">NotEmptyCollection&lt;T&gt;</a>? If so, indeed, there's no <em>identity</em> for <code>NonEmptyString</code> (but it does form a <a href="/2017/11/27/semigroups">Semigroup</a>). </p> <p> Since it's a semigroup, though, <a href="/2018/04/03/maybe-monoids/#79ed6bcdafca4afd91cd22f6f9cbc4a2">you can lift it to a monoid my wrapping it in Maybe</a>. If you do that, the identity of <code>Maybe&lt;NonEmptyString&gt;</code> would be <em>nothing</em>. </p> </div> <div class="comment-date">2018-05-08 1:53 UTC</div> </div> <div class="comment" id="f78e2713d0634dd39c1674ebcda763dd"> <div class="comment-author"> Ciprian Vilcan <a href="#f78e2713d0634dd39c1674ebcda763dd">#</a></div> <div class="comment-content"> <p> You are right, Mark. The <code>NonEmptyString</code> class is indeed a semigroup, thus has no neutral element. <br> This is not what confuses me, but what function to supply in the None case of a <code>Maybe&lt;SomeSemigroup&gt;</code> when calling the <code>.Match</code> method. In the case of <code>Maybe&lt;SomeMonoid&gt;</code>, it's simple and intuitive, as you simply supply a function that returns the neutral element of that monoid. <br> But what about semigroups? </p> <p> Here's a couple of generalized examples to better illustrate the question I'm having: <br> <code> Maybe&lt;SomeMonoid&gt; firstMaybe = someService.GetSomeMonoid(); <br> SomeMonoid value = firstMaybe.Match(Some: containedValue => containedValue, None: () => SomeMonoid.NeutralElement); <br> <br> Maybe&lt;SomeSemigroup&gt; secondMaybe = someService.GetSomeSemigroup(); <br> SomeSemigroup someSemigroup = secondMaybe.Match(Some: containedValue => containedValue, None: () => /*What to do here? Is it appropriate to throw an exception?*/); </code> </p> <p> I hope this time my question became clearer. I'm still in the process of wrapping my head around functional and category theory concepts, so my terminology may not be pinpoint accurate. </p> </div> <div class="comment-date">2018-05-11 06:44 UTC</div> </div> <div class="comment" id="2cae69987a0443a191f21d1bf4930ab1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2cae69987a0443a191f21d1bf4930ab1">#</a></div> <div class="comment-content"> <p> Ciprian, that's the problem with semigroups, isn't it? There's no single natural element to use in the absence of data. </p> <p> Lifting a semigroup to a Maybe is one way to resolve that problem. Since <a href="/2018/03/26/the-maybe-functor">Maybe is a functor</a>, you can map the contents of the Maybe until you've mapped it into a value for which an identity exists. </p> <p> In some cases, you can also fold a Maybe value by supplying a default value that makes sense in the specific context. A default value can be an appropriate fall-back value in a given context, even if it isn't a general identity. </p> </div> <div class="comment-date">2018-05-12 8:20 UTC</div> </div> <div class="comment" id="9640402dd2834ba0b75d4c1f810dfeaa"> <div class="comment-author"> Ciprian Vilcan <a href="#9640402dd2834ba0b75d4c1f810dfeaa">#</a></div> <div class="comment-content"> <p> I think I got it!<br> If you want to process that <code>Maybe&lt;SomeSemigroup&gt;</code> in a functional way, using the .Match(Some: ..., None: ...) method, you actually have to transform the method using it from a mostly statement based one, to a mostly expression based one. You have to pretend you don't know what's inside that Maybe for as long as possible, similar to using Lazy (or lazy evaluation in general). </p> <p> In <a href="https://dotnetfiddle.net/8vP47X">this</a> fiddle I've played around and created both an imperative and functional query method for retrieving a book by title and author, in order to prove myself that they can be made isomorphic. These two methods are GetBookFunctional and GetBookImperative.<br> However, I'm now trying to transform the GetBookImperativeWithoutElses into something functional using that .Match method, but I don't think it's possible.<br> The .Match method's signature is, practically speaking, equivalent to the if-else statement, whereas the GetBookImperativeWithoutElses method uses the if statement, meaning the functional approach will be forced to treat the elses, whereas the imperative one won't. </p> <p> Wow, so if you want to use this Maybe of semigroup and/or go fully functional, you really have to go deep down the functional rabbit hole.<br> Also, my guess is there is no guarantee that going from imperative to functional does not introduce more redundancy (like the elses in the second set of methods) into your system.<br> </p> <p> Am I right in these regards? </p> </div> <div class="comment-date">2018-05-12 13:45 UTC</div> </div> <div class="comment" id="fc2325a9be294305916c8bfbdac119bf"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fc2325a9be294305916c8bfbdac119bf">#</a></div> <div class="comment-content"> <p> Ciprian, thank you for the link to the code. This explains why you're running into trouble. You absolutely can address the scenario that causes you trouble in a nice functional style, but once you start having the need to keep track of error data as well as happy-path data, <em>Maybe</em> is no longer the data structure you need. </p> <p> <em>Maybe</em> enables you to model a case where data may or may not be available. It enables you distinguish something from nothing. On the other hand, in the absence of data, you have no information about the reason that data is missing. </p> <p> In order to keep track of such information, you need <em>Either</em>, which models data that's either this or that. </p> <p> I'll cover <em>Either</em> in future posts. </p> </div> <div class="comment-date">2018-05-13 14:44 UTC</div> </div> <div class="comment" id="8afc1e9bec810034dafd45c6854f1dd9"> <div class="comment-author">Edward Wang <a href="#8afc1e9bec810034dafd45c6854f1dd9">#</a></div> <div class="comment-content"> <p> Hi Mark, your posts are very impressive. I started to learn category theory and try to use it to understand programming. </p> <p> However, the idea of treating neutral elements as "do nothing" is not so helpful from my point of view. The neutral element is defined under a binary operation. But there is no clue that the return value of methods in the Null Object will be used in such a binary operation. </p> <p> Also, this idea doens't give us a unique choice of "do nothing". Given a type, there could be more than one binary operations that makes the type a monoid. Why do we choose the neutral element under this binary operation instead of the other binary operation? </p> </div> <div class="comment-date">2023-10-29 13:28 UTC</div> </div> <div class="comment" id="c72b8d0217164104a1a962b6476dea11"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c72b8d0217164104a1a962b6476dea11">#</a></div> <div class="comment-content"> <p> Edward, thank you for writing. If you don't find this useful, you are free to ignore it. </p> <p> Personally, I find the Null Object pattern interesting, because it's a alternative to an <code>if</code> branch. Sometimes, in various programming contexts, you may run into situations where you need to do nothing on various special occasions. By employing the Null Object pattern, you may be able to avoid <a href="https://en.wikipedia.org/wiki/Shotgun_surgery">Shotgun Surgery</a> by replacing scattered <code>if</code> checks with a Null Object. </p> <p> This, however, begs the question: When is it possible to make a Null Object implementation of a polymorphic type? </p> <p> This isn't always possible. For example, if the type has a method that returns a date and time, then which value do you return in order to 'do nothing'? </p> <p> I can't easily answer that question, so a Null Object probably isn't possible with a design like that. </p> <p> On the other hand, if the polymorphic type only defines monoidal operations, you know that a Null Object is possible. </p> <p> In reality, to be honest, in API design, I'm more interested in the Composite pattern, but if I can make something a Composite, then the Null Object just comes along for the ride. </p> <p> Thinking about monoids is, for me, mostly an analysis tool. It gives me guidance on what may be a good design. I don't presume to claim that in a language like C#, having <code>bool</code> og <code>int</code> as return types makes the monoidal operation unambiguous. </p> <p> Haskell gets around that issue by wrapping the primitive types in distinguishable wrappers. Thus, neither <code>Bool</code> nor <code>Int</code> are <code>Monoid</code> instances, but <code>Any</code>, <code>All</code>, <code>Sum</code>, and <code>Product</code> are. </p> </div> <div class="comment-date">2023-11-02 21:16 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Endomorphic Composite as a monoid https://blog.ploeh.dk/2018/04/16/endomorphic-composite-as-a-monoid 2018-04-16T08:16:00+00:00 Mark Seemann <div id="post"> <p> <em>A variation of the Composite design pattern uses endomorphic composition. That's still a monoid.</em> </p> <p> This article is part of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">a series of articles about design patterns and their category theory counterparts</a>. In a <a href="/2018/03/12/composite-as-a-monoid">previous article</a>, you learned that the <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a> design pattern is simply a <a href="/2017/10/06/monoids">monoid</a>. </p> <p> There is, however, a variation of the Composite design pattern where the return value from one step can be used as the input for the next step. </p> <h3 id="e0d1e07a05e14c34aa72e67a2e5fae61"> Endomorphic API <a href="#e0d1e07a05e14c34aa72e67a2e5fae61" title="permalink">#</a> </h3> <p> Imagine that you have to implement some scheduling functionality. For example, you may need to schedule something to happen a month from now, but it should happen on a bank day, during business hours, and you want to know what the resulting date and time will be, expressed in <a href="https://en.wikipedia.org/wiki/Coordinated_Universal_Time">UTC</a>. I've <a href="/2017/11/13/endomorphism-monoid">previously covered the various objects for performing such steps</a>. The common behaviour is this interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value); }</pre> </p> <p> The <code>Adjust</code> method is an <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a>; that is: the input type is the same as the return type, in this case <code>DateTimeOffset</code>. A <a href="/2017/11/13/endomorphism-monoid">previous article</a> already established that that's a monoid. </p> <h3 id="24dd8cb072b8434890ddb71fbb5e3ec9"> Composite endomorphism <a href="#24dd8cb072b8434890ddb71fbb5e3ec9" title="permalink">#</a> </h3> <p> If you have various implementations of <code>IDateTimeOffsetAdjustment</code>, you can make a Composite from them, like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CompositeDateTimeOffsetAdjustment</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&gt;&nbsp;adjustments; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;CompositeDateTimeOffsetAdjustment( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&gt;&nbsp;adjustments) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(adjustments&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(adjustments)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.adjustments&nbsp;=&nbsp;adjustments; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;acc&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;adjustment&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">this</span>.adjustments) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;adjustment.Adjust(acc); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Adjust</code> method simply starts with the input <code>value</code> and loops over all the composed <code>adjustments</code>. For each <code>adjustment</code> it adjusts <code>acc</code> to produce a new <code>acc</code> value. This goes on until all <code>adjustments</code> have had a chance to adjust the value. </p> <p> Notice that if <code>adjustments</code> is empty, the <code>Adjust</code> method simply returns the input value. In that degenerate case, the behaviour is similar to the identity function, which is the identity for the endomorphism monoid. </p> <p> You can now compose the desired behaviour, as this parametrised <a href="https://xunit.net">xUnit.net</a> test demonstrates: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-01-31T07:45:55+2&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-02-28T07:00:00Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-06T10:03:02+1&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-06T09:03:02Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-09T04:20:00Z&quot;</span>&nbsp;,&nbsp;<span style="color:#a31515;">&quot;2017-03-09T09:00:00Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-12T16:02:11Z&quot;</span>&nbsp;,&nbsp;<span style="color:#a31515;">&quot;2017-03-10T16:02:11Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-14T13:48:29-1&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-04-13T14:48:29Z&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AdjustReturnsCorrectResult( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;dtS, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;expectedS) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dt&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Parse(dtS); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CompositeDateTimeOffsetAdjustment</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NextMonthAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BusinessHoursAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DutchBankDayAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UtcAdjustment</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Adjust(dt); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#2b91af;">DateTimeOffset</span>.Parse(expectedS),&nbsp;actual); }</pre> </p> <p> You can see the implementation for all four composed classes in the <a href="/2017/11/13/endomorphism-monoid">previous article</a>. <code>NextMonthAdjustment</code> adjusts a date by a month into its future, <code>BusinessHoursAdjustment</code> adjusts a time to business hours, <code>DutchBankDayAdjustment</code> takes bank holidays and weekends into account in order to return a bank day, and <code>UtcAdjustment</code> convert a date and time to UTC. </p> <h3 id="5743387ec7f94a00861517d399756ab2"> Monoidal accumulation <a href="#5743387ec7f94a00861517d399756ab2" title="permalink">#</a> </h3> <p> As you've learned in that previous article that I've already referred to, an endomorphism is a monoid. In this particular example, the binary operation in question is called <code>Append</code>. From another article, you know that <a href="/2017/11/20/monoids-accumulate">monoids accumulate</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;Accumulate( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&gt;&nbsp;adjustments) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;acc&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdentityDateTimeOffsetAdjustment</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;adjustment&nbsp;<span style="color:blue;">in</span>&nbsp;adjustments) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;Append(acc,&nbsp;adjustment); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; }</pre> </p> <p> This implementation follows the template previously described: <ul> <li>Initialize a variable <code>acc</code> with the identity element. In this case, the identity is a class called <code>IdentityDateTimeOffsetAdjustment</code>.</li> <li>For each <code>adjustment</code> in <code>adjustments</code>, <code>Append</code> the <code>adjustment</code> to <code>acc</code>.</li> <li>Return <code>acc</code>.</li> </ul> This is an entirely automatable process, and it's only C#'s lack of higher-kinded types that prevents us from writing that code once and for all. In <a href="https://www.haskell.org">Haskell</a>, this general-purpose function exists; it's called <code>mconcat</code>. We'll get back to that in a moment, but first, here's another parametrised unit test that exercises the same test cases as the previous test, only against a composition created by <code>Accumulate</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-01-31T07:45:55+2&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-02-28T07:00:00Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-06T10:03:02+1&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-06T09:03:02Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-09T04:20:00Z&quot;</span>&nbsp;,&nbsp;<span style="color:#a31515;">&quot;2017-03-09T09:00:00Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-02-12T16:02:11Z&quot;</span>&nbsp;,&nbsp;<span style="color:#a31515;">&quot;2017-03-10T16:02:11Z&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-14T13:48:29-1&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-04-13T14:48:29Z&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AccumulatedAdjustReturnsCorrectResult( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;dtS, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;expectedS) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dt&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Parse(dtS); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffsetAdjustment</span>.Accumulate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NextMonthAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BusinessHoursAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DutchBankDayAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UtcAdjustment</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Adjust(dt); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#2b91af;">DateTimeOffset</span>.Parse(expectedS),&nbsp;actual); }</pre> </p> <p> While the implementation is different, this monoidal composition has the same behaviour as the above <code>CompositeDateTimeOffsetAdjustment</code> class. This, again, emphasises that Composites are simply monoids. </p> <h3 id="45b98ea80cb143f2af692a7cd7ac2a4b"> Endo <a href="#45b98ea80cb143f2af692a7cd7ac2a4b" title="permalink">#</a> </h3> <p> For comparison, this section demonstrates how to implement the above behaviour in Haskell. The code here passes the same test cases as those above. You can skip to the next section if you want to get to the conclusion. </p> <p> Instead of classes that implement interfaces, in Haskell you can define functions with the type <code>ZonedTime -&gt; ZonedTime</code>. You can compose such functions using the <code>Endo</code> <code>newtype</code> 'wrapper' that turns endomorphisms into monoids: </p> <p> <pre>λ&gt; adjustments = reverse [adjustToNextMonth, adjustToBusinessHours, adjustToDutchBankDay, adjustToUtc] λ&gt; :type adjustments adjustments :: [ZonedTime -&gt; ZonedTime] λ&gt; adjust = appEndo $ mconcat $ Endo &lt;$&gt; adjustments λ&gt; :type adjust adjust :: ZonedTime -&gt; ZonedTime</pre> </p> <p> In this example, I'm using GHCi (the Haskell REPL) to show the composition in two steps. The first step creates <code>adjustments</code>, which is a list of functions. In case you're wondering about the use of the <code>reverse</code> function, it turns out that <code>mconcat</code> composes from right to left, which I found counter-intuitive in this case. <code>adjustToNextMonth</code> should execute first, followed by <code>adjustToBusinessHours</code>, and so on. Defining the functions in the more intuitive left-to-right direction and then reversing it makes the code easier to understand, I hope. </p> <p> (For the Haskell connoisseurs, you can also achieve the same result by composing <code>Endo</code> with the <code>Dual</code> monoid, instead of reversing the list of adjustments.) </p> <p> The second step composes <code>adjust</code> from <code>adjustments</code>. It first maps <code>adjustments</code> to <code>Endo</code> values. While <code>ZonedTime -&gt; ZonedTime</code> isn't a <code>Monoid</code> instances, <code>Endo ZonedTime</code> is. This means that you can reduce a list of <code>Endo ZonedTime</code> with <code>mconcat</code>. The result is a single <code>Endo ZonedTime</code> value, which you can then unwrap to a function using <code>appEndo</code>. </p> <p> <code>adjust</code> is a function that you can call: </p> <p> <pre>λ&gt; dt 2017-01-31 07:45:55 +0200 λ&gt; adjust dt 2017-02-28 07:00:00 +0000</pre> </p> <p> In this example, I'd already prepared a <code>ZonedTime</code> value called <code>dt</code>. Calling <code>adjust</code> returns a new <code>ZonedTime</code> adjusted by all four composed functions. </p> <h3 id="675b6bd1899746c8963eb97a8580fb24"> Conclusion <a href="#675b6bd1899746c8963eb97a8580fb24" title="permalink">#</a> </h3> <p> In general, you implement the Composite design pattern by calling all composed functions with the original input value, collecting the return value of each call. In the final step, you then reduce the collected return values to a single value that you return. This requires the return type to form a monoid, because otherwise, you can't reduce it. </p> <p> In this article, however, you learned about an alternative implementation of the Composite design pattern. If the method that you compose has the same output type as its input, you can pass the output from one object as the input to the next object. In that case, you can escape the requirement that the return value is a monoid. That's the case with <code>DateTimeOffset</code> and <code>ZonedTime</code>: neither are monoids, because you can't add two dates and times together. </p> <p> At first glance, then, it seems like a falsification of the original claim that Composites are monoids. As you've learned in this article, however, endomorphisms are monoids, so the claim still stands. </p> <p> <strong>Next:</strong> <a href="/2018/04/23/null-object-as-identity">Null Object as identity</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Coalescing Composite as a monoid https://blog.ploeh.dk/2018/04/09/coalescing-composite-as-a-monoid 2018-04-09T08:15:00+00:00 Mark Seemann <div id="post"> <p> <em>A variation of the Composite design pattern uses coalescing behaviour to return non-composable values. That's still a monoid.</em> </p> <p> This article is part of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">a series of articles about design patterns and their category theory counterparts</a>. In <a href="/2018/03/12/composite-as-a-monoid">a previous article</a>, you learned that the <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a> design pattern is simply a <a href="/2017/10/06/monoids">monoid</a>. </p> <h3 id="ee5e76abfc9c4fe58a5e232579e397c5"> Monoidal return types <a href="#ee5e76abfc9c4fe58a5e232579e397c5" title="permalink">#</a> </h3> <p> When all methods of an interface return monoids, you can create a Composite. This is fairly intuitive once you understand what a monoid is. Consider this example interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ICustomerRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Create(<span style="color:#2b91af;">Customer</span>&nbsp;customer); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Customer</span>&nbsp;Read(<span style="color:#2b91af;">Guid</span>&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Update(<span style="color:#2b91af;">Customer</span>&nbsp;customer); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Delete(<span style="color:#2b91af;">Guid</span>&nbsp;id); }</pre> </p> <p> While this interface is, in fact, not readily composable, most of the methods are. It's easy to compose the three <code>void</code> methods. Here's a composition of the <code>Create</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Create(<span style="color:#2b91af;">Customer</span>&nbsp;customer) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;repository&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">this</span>.repositories) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repository.Create(customer); }</pre> </p> <p> In this case it's easy to compose multiple repositories, because <code>void</code> (<a href="/2018/01/15/unit-isomorphisms">or, rather, <em>unit</em></a>) forms a monoid. If you have methods that return numbers, you can add the numbers together (a monoid). If you have methods that return strings, you can concatenate the strings (a monoid). If you have methods that return Boolean values, you can <em>or</em> or <em>and</em> them together (more monoids). </p> <p> What about the above <code>Read</code> method, though? </p> <h3 id="05eec84ac1514c10bdbb0dbc6b36a957"> Picking the first Repository <a href="#05eec84ac1514c10bdbb0dbc6b36a957" title="permalink">#</a> </h3> <p> Why would you even want to compose two repositories? One scenario is where you have an old data store, and you want to move to a new data store. For a while, you wish to write to both data stores, but one of them stays the 'primary' data store, so this is the one from which you read. </p> <p> Imagine that the old repository saves customer information as JSON files on disk. The new data store, on the other hand, saves customer data as JSON documents in <a href="https://azure.microsoft.com/en-us/services/storage/blobs">Azure Blob Storage</a>. You've already written two implementations of <code>ICustomerRepository</code>: <code>FileCustomerRepository</code> and <code>AzureCustomerRepository</code>. How do you compose them? </p> <p> The three methods that return <code>void</code> are easy, as the above <code>Create</code> implementation demonstrates. The <code>Read</code> method, however, is more tricky. </p> <p> One option is to only query the first repository, and return its return value: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Customer</span>&nbsp;Read(<span style="color:#2b91af;">Guid</span>&nbsp;id) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.repositories.First().Read(id); }</pre> </p> <p> This works, but doesn't generalise. It works if you know that you have a non-empty collection of repositories, but if you want to adhere to the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>, you should be able to handle the case where there's no repositories. </p> <p> A Composite should be able to compose an arbitrary number of other objects. This includes a collection of no objects. The <code>CompositeCustomerRepository</code> class has this constructor: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">ICustomerRepository</span>&gt;&nbsp;repositories; <span style="color:blue;">public</span>&nbsp;CompositeCustomerRepository( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">ICustomerRepository</span>&gt;&nbsp;repositories) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(repositories&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(repositories)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repositories&nbsp;=&nbsp;repositories; }</pre> </p> <p> It uses standard Constructor Injection to inject an <code>IReadOnlyCollection&lt;ICustomerRepository&gt;</code>. Such a collection is finite, but can be empty. </p> <p> Another problem with blindly returning the value from the first repository is that the return value may be empty. </p> <p> In C#, people often use <code>null</code> to indicate a missing value, and while I find such practice unwise, I'll pursue this option for a bit. </p> <p> A more robust Composite would return the first non-null value it gets: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Customer</span>&nbsp;Read(<span style="color:#2b91af;">Guid</span>&nbsp;id) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;repository&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">this</span>.repositories) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;customer&nbsp;=&nbsp;repository.Read(id); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(customer&nbsp;!=&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;customer; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; }</pre> </p> <p> This implementation loops through all the injected <code>repositories</code> and calls <code>Read</code> on each until it gets a result that is not <code>null</code>. This will often be the first value, but doesn't have to be. If all repositories return <code>null</code>, then the Composite also returns <code>null</code>. To emphasise my position, I would never design C# code like this, but at least it's consistent. </p> <p> If you've ever worked with relational databases, you may have had an opportunity to use the <code>COALESCE</code> function, which works in exactly the same way. This is the reason I call such an implementation a <em>coalescing Composite</em>. </p> <h3 id="ae3e089ecda84bd299e44c22554f5b85"> The First monoid <a href="#ae3e089ecda84bd299e44c22554f5b85" title="permalink">#</a> </h3> <p> The <a href="https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql">T-SQL documentation for COALESCE</a> describes the operation like this: <blockquote> "Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to <code>NULL</code>." </blockquote> The <a href="https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions023.htm#SQLRF00617">Oracle documentation</a> expresses it as: <blockquote> "<code>COALESCE</code> returns the first non-null <code>expr</code> in the expression list." </blockquote> This may not be apparent, but that's a monoid. </p> <p> <a href="https://www.haskell.org">Haskell</a>'s base library comes with a monoidal type called <code>First</code>, which is a <blockquote> "Maybe monoid returning the leftmost non-Nothing value." </blockquote> Sounds familiar? </p> <p> Here's how you can use it in GHCi: </p> <p> <pre>λ&gt; First (Just (Customer id1 "Joan")) &lt;&gt; First (Just (Customer id2 "Nigel")) First {getFirst = Just (Customer {customerId = 1243, customerName = "Joan"})} λ&gt; First (Just (Customer id1 "Joan")) &lt;&gt; First Nothing First {getFirst = Just (Customer {customerId = 1243, customerName = "Joan"})} λ&gt; First Nothing &lt;&gt; First (Just (Customer id2 "Nigel")) First {getFirst = Just (Customer {customerId = 5cd5, customerName = "Nigel"})} λ&gt; First Nothing &lt;&gt; First Nothing First {getFirst = Nothing}</pre> </p> <p> (To be clear, the above examples uses <code>First</code> from <code>Data.Monoid</code>, not <code>First</code> from <code>Data.Semigroup</code>.) </p> <p> The operator <code>&lt;&gt;</code> is an infix alias for <code>mappend</code> - Haskell's polymorphic binary operation. </p> <p> As long as the left-most value is present, that's the return value, regardless of whether the right value is <code>Just</code> or <code>Nothing</code>. Only when the left value is <code>Nothing</code> is the right value returned. Notice that this value may also be <code>Nothing</code>, causing the entire expression to be <code>Nothing</code>. </p> <p> That's exactly the same behaviour as the above implementation of the <code>Read</code> method. </p> <h3 id="c83d6ec43ba94073b151841289050fba"> First in C# <a href="#c83d6ec43ba94073b151841289050fba" title="permalink">#</a> </h3> <p> It's easy to port Haskell's <code>First</code> type to C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;hasItem; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;First() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.hasItem&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;First(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.hasItem&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;FindFirst(<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;other) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.hasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;other; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Instead of nesting <code>Maybe</code> inside of <code>First</code>, as Haskell does, I simplified a bit and gave <code>First&lt;T&gt;</code> two constructor overloads: one that takes a value, and one that doesn't. The <code>FindFirst</code> method is the binary operation that corresponds to Haskell's <code>&lt;&gt;</code> or <code>mappend</code>. </p> <p> This is only one of several alternative implementations of <a href="/2018/04/03/maybe-monoids">the <em>first</em> monoid</a>. </p> <p> In order to make <code>First&lt;T&gt;</code> a monoid, it must also have an identity, which is just an empty value: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Identity&lt;<span style="color:#2b91af;">T</span>&gt;() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;(); }</pre> </p> <p> This enables you to <a href="/2017/11/20/monoids-accumulate">accumulate</a> an arbitrary number of <code>First&lt;T&gt;</code> values to a single value: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Accumulate&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">IReadOnlyList</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;firsts) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;acc&nbsp;=&nbsp;Identity&lt;<span style="color:#2b91af;">T</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;first&nbsp;<span style="color:blue;">in</span>&nbsp;firsts) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;acc.FindFirst(first); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; }</pre> </p> <p> You start with the identity, which is also the return value if <code>firsts</code> is empty. If that's not the case, you loop through all <code>firsts</code> and update <code>acc</code> by calling <code>FindFirst</code>. </p> <h3 id="d90234ded89e483bbc6eb8db9ffb0a19"> A composable Repository <a href="#d90234ded89e483bbc6eb8db9ffb0a19" title="permalink">#</a> </h3> <p> You can formalise such a design by changing the <code>ICustomerRepository</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ICustomerRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Create(<span style="color:#2b91af;">Customer</span>&nbsp;customer); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">Customer</span>&gt;&nbsp;Read(<span style="color:#2b91af;">Guid</span>&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Update(<span style="color:#2b91af;">Customer</span>&nbsp;customer); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Delete(<span style="color:#2b91af;">Guid</span>&nbsp;id); }</pre> </p> <p> In this modified version, <code>Read</code> explicitly returns <code>First&lt;Customer&gt;</code>. The rest of the methods remain as before. </p> <p> The reusable API of <code>First</code> makes it easy to implement a Composite version of <code>Read</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">Customer</span>&gt;&nbsp;Read(<span style="color:#2b91af;">Guid</span>&nbsp;id) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;candidates&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">First</span>&lt;<span style="color:#2b91af;">Customer</span>&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;repository&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">this</span>.repositories) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidates.Add(repository.Read(id)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">First</span>.Accumulate(candidates); }</pre> </p> <p> You could argue that this seems to be wasteful, because it calls <code>Read</code> on all repositories. If the first Repository returns a value, all remaining queries are wasted. You can address that issue with lazy evaluation. </p> <p> You can see (a recording of) a live demo of the example in this article in my <a href="https://cleancoders.com">Clean Coders</a> video <a href="https://cleancoders.com/episode/humane-code-real-episode-2/show">Composite as Universal Abstraction</a>. </p> <h3 id="8c301e16ccf34fd099e4f97503c005c5"> Summary <a href="#8c301e16ccf34fd099e4f97503c005c5" title="permalink">#</a> </h3> <p> While the typical Composite is implemented by directly aggregating the return values from the composed objects, variations exist. One variation picks the first non-empty value from a collection of candidates, reminiscent of the SQL <code>COALESCE</code> function. This is, however, still a monoid, so the overall conjecture that Composites are monoids still holds. </p> <p> Another Composite variation exists, but that one turns out to be a monoid as well. Read on! </p> <p> <strong>Next:</strong> <a href="/2018/04/16/endomorphic-composite-as-a-monoid">Endomorphic Composite as a monoid</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="d5cb5c03f4af4f70928674c774f09058"> <div class="comment-author"><a href="http://www.nikolamilekic.com">Nikola Milekic</a> <a href="#d5cb5c03f4af4f70928674c774f09058">#</a></div> <div class="comment-content"> <blockquote>When all methods of an interface return monoids, you can create a Composite. This is fairly intuitive once you understand what a monoid is.</blockquote> <p> I've been struggling a bit with the terminology and I could use a bit of help. </p> <p> I understand a <code>Monoid&lt;a&gt;</code> to be of type <code>a -> a -> a</code>. I further understand that it can be said that <code>a</code> "forms a monoid over" the specific <code>a -> a -> a</code> implementation. We can then say that an int forms two different monoids over addition and multiplication. </p> <p> Is my understanding above accurate and if so wouldn't it be more accurate to say that you can create a composite from an interface only when all of its methods <em>return values that form monoids</em>? Saying that they have to return monoids (instead of values that form them) goes against my understanding that a monoid is a binary operation. </p> <p> Thanks a lot for an amazing article series, and for this blog overall. Keep up the good work! </p> </div> <div class="comment-date">2018-08-07 21:04 UTC</div> </div> <div class="comment" id="de5c75b6e2374a03bb3301cd670b9720"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#de5c75b6e2374a03bb3301cd670b9720">#</a></div> <div class="comment-content"> <p> Nikola, thank you for writing. You're correct: it <em>would</em> be more correct to say that you can create a Composite from an interface when all of its methods return types that form monoids. Throughout this article series, I've been struggling to keep my language as correct and specific as possible, but I sometimes slip up. </p> <p> This has come up before, so perhaps you'll find <a href="/2017/10/30/tuple-monoids#68cbe481b0e74c3284e45d4e8dd51da4">this answer</a> helpful. </p> <p> By the way, there's one exception to the rule that in order to be able to create a Composite, all methods must return types that form monoids. This is <a href="/2018/04/16/endomorphic-composite-as-a-monoid">when the return type is the same as the input type</a>. The resulting Composite is still a monoid, so the overall conclusion holds. </p> </div> <div class="comment-date">2018-08-08 6:32 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Maybe monoids https://blog.ploeh.dk/2018/04/03/maybe-monoids 2018-04-03T12:58:00+00:00 Mark Seemann <div id="post"> <p> <em>You can combine Maybe objects in several ways. An article for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a monoid is an associative binary operation with a neutral element (also known as identity). </p> <p> You can combine <a href="/2018/03/26/the-maybe-functor">Maybe</a> objects in various ways, thereby turning them into monoids. There's at least two unconstrained monoids over Maybe values, as well as some constrained monoids. By <em>constrained</em> I mean that the monoid only exists for Maybe objects that contain certain values. You'll see such an example first. </p> <h3 id="7a7573db11bf4e73b49aa84b7c9d1253"> Combining Maybes over semigroups <a href="#7a7573db11bf4e73b49aa84b7c9d1253" title="permalink">#</a> </h3> <p> If you have two Maybe objects, and they both (potentially) contain values that form a <a href="/2017/11/27/semigroups">semigroup</a>, you can combine the Maybe values as well. Here's a few examples. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;CombineMinimum(<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem&nbsp;&amp;&amp;&nbsp;y.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;(<span style="color:#2b91af;">Math</span>.Min(x.Item,&nbsp;y.Item)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y; }</pre> </p> <p> In this first example, the semigroup operation in question is the <em>minimum</em> operation. Since C# doesn't enable you to write generic code over mathematical operations, the above method just gives you an example implemented for <code>Maybe&lt;int&gt;</code> values. If you also want to support e.g. <code>Maybe&lt;decimal&gt;</code> or <code>Maybe&lt;long&gt;</code>, you'll have to add overloads for those types. </p> <p> If both <code>x</code> and <code>y</code> have values, you get the minimum of those, still wrapped in a Maybe container: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;(42); <span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;(1337); <span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:#2b91af;">Maybe</span>.CombineMinimum(x,&nbsp;y);</pre> </p> <p> Here, <code>m</code> is a <code>new Maybe&lt;int&gt;(42)</code>. </p> <p> It's possible to combine any two Maybe objects as long as you have a way to combine the contained values in the case where both Maybe objects contain values. In other words, you need a binary operation, so the contained values must form a semigroup, like, for example, the <em>minimum</em> operation. Another example is <em>maximum:</em> </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">decimal</span>&gt;&nbsp;CombineMaximum(<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">decimal</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">decimal</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem&nbsp;&amp;&amp;&nbsp;y.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">decimal</span>&gt;(<span style="color:#2b91af;">Math</span>.Max(x.Item,&nbsp;y.Item)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y; }</pre> </p> <p> In order to vary the examples, I chose to implement this operation for <code>decimal</code> instead of <code>int</code>, but you can see that the implementation code follows the same template. When both <code>x</code> and <code>y</code> contains values, you invoke the binary operation. If, on the other hand, <code>y</code> is empty, then right identity still holds: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">decimal</span>&gt;(42); <span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">decimal</span>&gt;(); <span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:#2b91af;">Maybe</span>.CombineMaximum(x,&nbsp;y);</pre> </p> <p> Since <code>y</code> in the above example is empty, the resulting object <code>m</code> is a <code>new Maybe&lt;decimal&gt;(42)</code>. </p> <p> You don't have to constrain yourself to semigroups exclusively. You can use a monoid as well, such as the <em>sum</em> monoid: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">long</span>&gt;&nbsp;CombineSum(<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">long</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">long</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem&nbsp;&amp;&amp;&nbsp;y.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">long</span>&gt;(x.Item&nbsp;+&nbsp;y.Item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y; }</pre> </p> <p> Again, notice how most of this code is boilerplate code that follows the same template as above. In C#, unfortunately, you have to write out all the combinations of operations and contained types, but in <a href="https://www.haskell.org">Haskell</a>, with its stronger type system, it all comes in the base library: </p> <p> <pre>Prelude Data.Semigroup&gt; Option (Just (Min 42)) &lt;&gt; Option (Just (Min 1337)) Option {getOption = Just (Min {getMin = 42})} Prelude Data.Semigroup&gt; Option (Just (Max 42)) &lt;&gt; mempty Option {getOption = Just (Max {getMax = 42})} Prelude Data.Semigroup&gt; mempty &lt;&gt; Option (Just (Sum 1337)) Option {getOption = Just (Sum {getSum = 1337})}</pre> </p> <p> That particular monoid over Maybe, however, does require as a minimum that the contained values form a semigroup. There are other monoids over Maybe that don't have any such constraints. </p> <h3 id="a21e44f2039d440f9e9469770a47777e"> First <a href="#a21e44f2039d440f9e9469770a47777e" title="permalink">#</a> </h3> <p> As you can read in <a href="/2017/11/27/semigroups">the introductory article about semigroups</a>, there's two semigroup operations called <em>first</em> and <em>last</em>. Similarly, there's two operations by the same name defined over monoids. They behave a little differently, although they're related. </p> <p> The <em>first</em> monoid operation <em>returns the left-most non-empty value</em> among candidates. You can view <em>nothing</em> as being a <a href="/2015/11/13/null-has-no-type-but-maybe-has">type-safe equivalent to null</a>, in which case this monoid is equivalent to a <a href="https://en.wikipedia.org/wiki/Null_coalescing_operator">null coalescing operator</a>. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;First&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y; }</pre> </p> <p> As long as <code>x</code> contains a value, <code>First</code> returns it. The contained values don't have to form monoids or semigroups, as this example demonstrates: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;03C2ECDBEF1D46039DE94A9994BA3C1E&quot;</span>)); <span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;A1B7BC82928F4DA892D72567548A8826&quot;</span>)); <span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:#2b91af;">Maybe</span>.First(x,&nbsp;y);</pre> </p> <p> While I'm not aware of any reasonable way to combine <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">GUIDs</a>, you can still pick the left-most non-empty value. In the above example, <code>m</code> contains <code>03C2ECDBEF1D46039DE94A9994BA3C1E</code>. If, on the other hand, the first value is empty, you get a different result: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(); <span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;2A2D19DE89D84EFD9E5BEE7C4ADAFD90&quot;</span>)); <span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:#2b91af;">Maybe</span>.First(x,&nbsp;y);</pre> </p> <p> In this case, <code>m</code> contains <code>2A2D19DE89D84EFD9E5BEE7C4ADAFD90</code>, even though it comes from <code>y</code>. </p> <p> Notice that there's no guarantee that <code>First</code> returns a non-empty value. If both <code>x</code> and <code>y</code> are empty, then the result is also empty. The <code>First</code> operation is an associative binary operation, and the identity is the empty value (often called <em>nothing</em> or <em>none</em>). It's a monoid. </p> <h3 id="119e8d969d5e4f35a47c6c17947a28f9"> Last <a href="#119e8d969d5e4f35a47c6c17947a28f9" title="permalink">#</a> </h3> <p> Since you can define a binary operation called <code>First</code>, it's obvious that you can also define one called <code>Last</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Last&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(y.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; }</pre> </p> <p> This operation <em>returns the right-most non-empty value</em>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;1D9326CDA0B3484AB495DFD280F990A3&quot;</span>)); <span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Guid</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;FFFC6CE263C7490EA0290017FE02D9D4&quot;</span>)); <span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:#2b91af;">Maybe</span>.Last(x,&nbsp;y);</pre> </p> <p> In this example, <code>m</code> contains <code>FFFC6CE263C7490EA0290017FE02D9D4</code>, but while <code>Last</code> favours <code>y</code>, it'll still return <code>x</code> if <code>y</code> is empty. Notice that, like <code>First</code>, there's no guarantee that you'll receive a populated Maybe. If both <code>x</code> and <code>y</code> are empty, the result will be empty as well. </p> <p> Like <code>First</code>, <code>Last</code> is an associative binary operation with <em>nothing</em> as the identity. </p> <h3 id="79ed6bcdafca4afd91cd22f6f9cbc4a2"> Generalisation <a href="#79ed6bcdafca4afd91cd22f6f9cbc4a2" title="permalink">#</a> </h3> <p> The first examples you saw in this article (<code>CombineMinimum</code>, <code>CombineMaximum</code>, and so on), came with the constraint that the contained values form a semigroup. The <code>First</code> and <code>Last</code> operations, on the other hand, seem unconstrained. They work even on GUIDs, which notoriously can't be combined. </p> <p> If you recall, though, <em>first</em> and <em>last</em> are both associative binary operations. They are, in fact, unconstrained semigroups. Recall the <code>Last</code> semigroup: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Last&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y; }</pre> </p> <p> This binary operation operates on any unconstrained type <code>T</code>, including <code>Guid</code>. It unconditionally returns <code>y</code>. </p> <p> You could implement the <code>Last</code> monoid over Maybe using the same template as above, utilising the underlying semigroup: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Last&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem&nbsp;&amp;&amp;&nbsp;y.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;(Last(x.Item,&nbsp;y.Item)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y; }</pre> </p> <p> This implementation has exactly the same behaviour as the previous implementation of <code>Last</code> shown earlier. You can implement <code>First</code> in the same way. </p> <p> That's exactly how Haskell works: </p> <p> <pre>Prelude Data.Semigroup Data.UUID.Types&gt; x = sequence $ Last $ fromString "03C2ECDB-EF1D-4603-9DE9-4A9994BA3C1E" Prelude Data.Semigroup Data.UUID.Types&gt; x Just (Last {getLast = 03c2ecdb-ef1d-4603-9de9-4a9994ba3c1e}) Prelude Data.Semigroup Data.UUID.Types&gt; y = sequence $ Last $ fromString "A1B7BC82-928F-4DA8-92D7-2567548A8826" Prelude Data.Semigroup Data.UUID.Types&gt; y Just (Last {getLast = a1b7bc82-928f-4da8-92d7-2567548a8826}) Prelude Data.Semigroup Data.UUID.Types&gt; Option x &lt;&gt; Option y Option {getOption = Just (Last {getLast = a1b7bc82-928f-4da8-92d7-2567548a8826})} Prelude Data.Semigroup Data.UUID.Types&gt; Option x &lt;&gt; mempty Option {getOption = Just (Last {getLast = 03c2ecdb-ef1d-4603-9de9-4a9994ba3c1e})}</pre> </p> <p> The <code>&lt;&gt;</code> operator is the generic binary operation, and the way Haskell works, it changes behaviour depending on the type upon which it operates. <code>Option</code> is a wrapper around <code>Maybe</code>, and <code>Last</code> represents the <em>last</em> semigroup. When you stack <code>UUID</code> values inside of <code>Option Last</code>, you get the behaviour of selecting the right-most non-empty value. </p> <p> In fact, <blockquote> <a href="https://en.wikipedia.org/wiki/Monoid">Any semigroup <em>S</em> may be turned into a monoid simply by adjoining an element <em>e</em> not in <em>S</em> and defining <em>e</em> • <em>s</em> = <em>s</em> = <em>s</em> • <em>e</em> for all <em>s</em> ∈ <em>S</em>.</a> </blockquote> </p> <p> <img src="/content/binary/semigroup-to-monoid.png" alt="semigroup-to-monoid diagram"> </p> <p> That's just a mathematical way of saying that if you have a semigroup, you can add an extra value <em>e</em> and make <em>e</em> behave like the identity for the monoid you're creating. That extra value is <em>nothing</em>. The way Haskell's <code>Data.Semigroup</code> module models a monoid over Maybe instances aligns with the underlying mathematics. </p> <h3 id="908819d511be489e8f02f7751b666804"> Conclusion <a href="#908819d511be489e8f02f7751b666804" title="permalink">#</a> </h3> <p> Just as there's more than one monoid over numbers, and more than one monoid over Boolean values, there's more than one monoid over Maybe values. The most useful one may be the one that elevates any semigroup to a monoid by adding <em>nothing</em> as the identity, but others exist. While, at first glance, the <em>first</em> and <em>last</em> monoids over Maybes look like operations in their own right, they're just applications of the general rule. They elevate the <em>first</em> and <em>last</em> semigroups to monoids by 'wrapping' them in Maybes, and using <em>nothing</em> as the identity. </p> <p> <strong>Next:</strong> <a href="/2019/04/15/lazy-monoids">Lazy monoids</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Maybe functor https://blog.ploeh.dk/2018/03/26/the-maybe-functor 2018-03-26T05:19:00+00:00 Mark Seemann <div id="post"> <p> <em>An introduction to the Maybe functor for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. </p> <p> One of the simplest, and easiest to understand, functors is <em>Maybe</em>. It's also sometimes known as the <a href="/2022/04/25/the-maybe-monad">Maybe monad</a>, but this is not a monad tutorial; it's a functor tutorial. Maybe is many things; one of them is a functor. In <a href="http://fsharp.org">F#</a>, Maybe is called <code>option</code>. </p> <h3 id="e9a74dc1e498466a9f8bf63224867176"> Motivation <a href="#e9a74dc1e498466a9f8bf63224867176" title="permalink">#</a> </h3> <p> Maybe enables you to model a value that may or may not be present. Object-oriented programmers typically have a hard time grasping the significance of Maybe, since it essentially does the same as <em>null</em> in mainstream object-oriented languages. There are differences, however. In languages like C# and Java, most things can be null, which can lead to much <a href="/2013/07/08/defensive-coding">defensive coding</a>. What happens more frequently, though, is that programmers forget to check for null, with run-time exceptions as the result. </p> <p> A Maybe value, on the other hand, makes it explicit that a value may or may not be present. In statically typed languages, it also forces you to deal with the case where no data is present; if you don't, your code will not compile. </p> <p> Finally, in a language like C#, <a href="/2015/11/13/null-has-no-type-but-maybe-has">null has no type</a>, but a Maybe value always has a type. </p> <p> If you appreciate the tenet that <a href="https://www.python.org/dev/peps/pep-0020">explicit is better than implicit</a>, then you should favour Maybe over null. </p> <h3 id="09da3571d64a4a77b94ffccacd59e3ed"> Implementation <a href="#09da3571d64a4a77b94ffccacd59e3ed" title="permalink">#</a> </h3> <p> If you've read <a href="/2018/03/22/functors">the introduction</a>, then you know that <code>IEnumerable&lt;T&gt;</code> is a functor. In many ways, Maybe is like <code>IEnumerable&lt;T&gt;</code>, but it's a particular type of collection that can only contain zero or one element(s). There are various ways in which you can implement Maybe in an object-oriented language like C#; here's one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;HasItem&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Maybe() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.HasItem&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Maybe(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.HasItem&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(selector)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(selector(<span style="color:blue;">this</span>.Item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;GetValueOrFallback(<span style="color:#2b91af;">T</span>&nbsp;fallbackValue) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(fallbackValue&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(fallbackValue)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Item; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;fallbackValue; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;other&nbsp;=&nbsp;obj&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.Item,&nbsp;other.Item); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.HasItem&nbsp;?&nbsp;<span style="color:blue;">this</span>.Item.GetHashCode()&nbsp;:&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is a generic class with two constructors. The parameterless constructor indicates the case where no value is present, whereas the other constructor overload indicates the case where exactly one value is available. Notice that a guard clause prevents you from accidentally passing null as a value. </p> <p> The <code>Select</code> method has the correct signature for a functor. If a value is present, it uses the <code>selector</code> method argument to map <code>item</code> to a new value, and return a new <code>Maybe&lt;TResult&gt;</code> value. If no value is available, then a new empty <code>Maybe&lt;TResult&gt;</code> value is returned. </p> <p> This class also override <code>Equals</code>. This isn't necessary in order for it to be a functor, but it makes it easier to compare two <code>Maybe&lt;T&gt;</code> values. </p> <p> A common question about such generic <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">containers</a> is: <em>how do you get the value out of the container?</em> </p> <p> The answer depends on the particular container, but in this example, I decided to enable that functionality with the <code>GetValueOrFallback</code> method. The only way to get the item out of a <code>Maybe</code> value is by supplying a fall-back value that can be used if no value is available. This is one way to guarantee that you, as a client developer, always remember to deal with the empty case. </p> <h3 id="b95ee3d38a35472c949b3cea4c5b0216"> Usage <a href="#b95ee3d38a35472c949b3cea4c5b0216" title="permalink">#</a> </h3> <p> It's easy to use this <code>Maybe</code> class: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;source&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;(42); </pre> </p> <p> This creates a new <code>Maybe&lt;int&gt;</code> object that contains the value <code>42</code>. If you need to change the value inside the object, you can, for example, do this: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;dest&nbsp;=&nbsp;source.Select(x&nbsp;=&gt;&nbsp;x.ToString()); </pre> </p> <p> Since C# natively understands functors through its query syntax, you could also have written the above translation like this: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;dest&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;x&nbsp;<span style="color:blue;">in</span>&nbsp;source &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;x.ToString();</pre> </p> <p> It's up to you and your collaborators whether you prefer one or the other of those alternatives. In both examples, though, <code>dest</code> is a new populated <code>Maybe&lt;string&gt;</code> object containing the string <code>"42"</code>. </p> <p> A more realistic example could be as part of a line-of-business application. Many <a href="/2012/12/18/RangersandZookeepers">enterprise developers</a> are familiar with the <a href="https://martinfowler.com/eaaCatalog/repository.html">Repository pattern</a>. Imagine that you'd like to query a repository for a <code>Reservation</code> object. If one is found in the database, you'd like to convert it to a view model, so that you can display it. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;viewModel&nbsp;=&nbsp;repository.Read(id) &nbsp;&nbsp;&nbsp;&nbsp;.Select(r&nbsp;=&gt;&nbsp;r.ToViewModel()) &nbsp;&nbsp;&nbsp;&nbsp;.GetValueOrFallback(<span style="color:#2b91af;">ReservationViewModel</span>.Null);</pre> </p> <p> The repository's <code>Read</code> method returns <code>Maybe&lt;Reservation&gt;</code>, indicating that it's possible that no object is returned. This will happen if you're querying the repository for an <code>id</code> that doesn't exist in the underlying database. </p> <p> While you can translate the (potential) <code>Reservation</code> object to a view model (using the <code>ToViewModel</code> extension method), you'll have to supply a default view model to handle the case when the reservation wasn't found. </p> <p> <code>ReservationViewModel.Null</code> is a static read-only class field implementing the <a href="https://en.wikipedia.org/wiki/Null_Object_pattern">Null Object pattern</a>. Here, it's used for the fall-back value, in case no object was returned from the repository. </p> <p> Notice that while you need a fall-back value at the end of your <a href="https://martinfowler.com/bliki/FluentInterface.html">fluent interface</a> pipeline, you don't need fall-back values for any intermediate steps. Specifically, you don't need a Null Object implementation for your domain model (<code>Reservation</code>). Furthermore, no defensive coding is required, because <code>Maybe&lt;T&gt;</code> guarantees that the object passed to <code>selector</code> is never <code>null</code>. </p> <h3 id="e5b5aead20eb4f62947a5e0b100b1beb"> First functor law <a href="#e5b5aead20eb4f62947a5e0b100b1beb" title="permalink">#</a> </h3> <p> A <code>Select</code> method with the right signature isn't enough to be a functor. It must also obey the functor laws. Maybe obeys both laws, which you can demonstrate with a few examples. Here's some test cases for a populated Maybe: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;corge&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;antidisestablishmentarianism&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PopulatedMaybeObeysFirstFunctorLaw(<span style="color:blue;">string</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;id&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;(value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(m,&nbsp;m.Select(id)); }</pre> </p> <p> This parametrised unit test uses <a href="https://xunit.net">xUnit.net</a> to demonstrate that a populated Maybe value doesn't change when translated with the local <code>id</code> function, since <code>id</code> returns the input unchanged. </p> <p> The first functor law holds for an empty Maybe as well: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;EmptyMaybeObeysFirstFunctorLaw() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;id&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(m,&nbsp;m.Select(id)); }</pre> </p> <p> When a Maybe starts empty, translating it with <code>id</code> doesn't change that it's empty. It's worth noting, however, that the original and the translated objects are considered equal because <code>Maybe&lt;T&gt;</code> overrides <code>Equals</code>. Even in the case of the empty Maybe, the value returned by <code>Select(id)</code> is a new object, with a memory address different from the original value. </p> <h3 id="1f17f61c26f9481aa3eea8d033ed3afc"> Second functor law <a href="#1f17f61c26f9481aa3eea8d033ed3afc" title="permalink">#</a> </h3> <p> You can also demonstrate the second functor law with some examples, starting with some test cases for the populated case: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:blue;">true</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:blue;">false</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:blue;">false</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;corge&quot;</span>,&nbsp;<span style="color:blue;">false</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;antidisestablishmentarianism&quot;</span>,&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PopulatedMaybeObeysSecondFunctorLaw(<span style="color:blue;">string</span>&nbsp;value,&nbsp;<span style="color:blue;">bool</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;g&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;&nbsp;&nbsp;f&nbsp;=&nbsp;i&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;(value); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(m.Select(g).Select(f),&nbsp;m.Select(s&nbsp;=&gt;&nbsp;f(g(s)))); }</pre> </p> <p> In this parametrised test, <code>f</code> and <code>g</code> are two local functions. <code>g</code> returns the length of a string (for example, the length of <em>antidisestablishmentarianism</em> is <em>28</em>). <code>f</code> evaluates whether or not a number is even. </p> <p> Whether you decide to first translate <code>m</code> with <code>g</code>, and then translate the return value with <code>f</code>, or you decide to translate the composition of those functions in a single <code>Select</code> method call, the result should be the same. </p> <p> The second functor law holds for the empty case as well: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;EmptyMaybeObeysSecondFunctorLaw() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;g&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;&nbsp;&nbsp;f&nbsp;=&nbsp;i&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;m&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">string</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(m.Select(g).Select(f),&nbsp;m.Select(s&nbsp;=&gt;&nbsp;f(g(s)))); }</pre> </p> <p> Since <code>m</code> is empty, applying the translations doesn't change that fact - it only changes the type of the resulting object, which is an empty <code>Maybe&lt;bool&gt;</code>. </p> <h3 id="9468d48e71ea45c8b17540919be78b0f"> Haskell <a href="#9468d48e71ea45c8b17540919be78b0f" title="permalink">#</a> </h3> <p> In <a href="https://www.haskell.org">Haskell</a>, Maybe is built in. You can create a <code>Maybe</code> value containing an integer like this (the type annotations are optional): </p> <p> <pre><span style="color:#600277;">source</span>&nbsp;::&nbsp;Maybe&nbsp;Int source&nbsp;<span style="color:#666666;">=</span>&nbsp;Just&nbsp;<span style="color:#09885a;">42</span></pre> </p> <p> Mapping <code>source</code> to a <code>String</code> can be done like this: </p> <p> <pre><span style="color:#600277;">dest</span>&nbsp;::&nbsp;Maybe&nbsp;String dest&nbsp;<span style="color:#666666;">=</span>&nbsp;fmap&nbsp;show&nbsp;source</pre> </p> <p> The function <code>fmap</code> corresponds to the above C# <code>Select</code> method. </p> <p> It's also possible to use infix notation: </p> <p> <pre><span style="color:#600277;">dest</span>&nbsp;::&nbsp;Maybe&nbsp;String dest&nbsp;<span style="color:#666666;">=</span>&nbsp;show&nbsp;<span style="color:#666666;">&lt;$&gt;</span>&nbsp;source</pre> </p> <p> The <code>&lt;$&gt;</code> operator is an alias for <code>fmap</code>. </p> <p> Whether you use <code>fmap</code> or <code>&lt;$&gt;</code>, the resulting <code>dest</code> value is <code>Just "42"</code>. </p> <p> If you want to create an empty <code>Maybe</code> value, you use the <code>Nothing</code> data constructor. </p> <h3 id="76073b1acf7f4b9ab72f873738dec4f4"> F# <a href="#76073b1acf7f4b9ab72f873738dec4f4" title="permalink">#</a> </h3> <p> Maybe is also a built-in type in F#, but here it's called <code>option</code> instead of <code>Maybe</code>. You create an option containing an integer like this: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;source&nbsp;=&nbsp;<span style="color:navy;">Some</span>&nbsp;42</pre> </p> <p> While the case where a value is present was denoted with <code>Just</code> in Haskell, in F# it's called <code>Some</code>. </p> <p> You can translate option values using the <code>map</code> function from the <code>Option</code> module: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;dest&nbsp;=&nbsp;source&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">string</span></pre> </p> <p> Finally, if you want to create an empty <code>option</code> value, you can use the <code>None</code> case constructor. </p> <h3 id="5c14e63d3b50485f8252718eae911d04"> Summary <a href="#5c14e63d3b50485f8252718eae911d04" title="permalink">#</a> </h3> <p> Together with a functor called <em>Either</em>, Maybe is one of the workhorses of statically typed functional programming. You aren't going to write much F# or Haskell before you run into it. In C# I've used variations of the above <code>Maybe&lt;T&gt;</code> class for years, with much success. </p> <p> In this article, I only discussed Maybe in its role of being a functor, but it's so much more than that! It's also an applicative functor, a monad, and traversable (enumerable). Not all functors are that rich. </p> <p> <strong>Next:</strong> <a href="/2019/01/14/an-either-functor">An Either functor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9192932dd15943228021ca6fcba20525"> <div class="comment-author"><a href="https://github.com/bert2">Robert Hofmann</a> <a href="#9192932dd15943228021ca6fcba20525">#</a></div> <div class="comment-content"> <p> I think it's interesting to note that since C# 8.0 we don't require an extra generic type like <code>Maybe&lt;T&gt;</code> anymore in order to implement the maybe functor. Because C# 8.0 added nullable reference types, everything can be nullable now. By adding the right extension methods we can make <code>T?</code> a maybe functor and use its beautifully succinct syntax. </p> <p> Due to the C#'s awkward dichotomy between value and reference types this involves some busy work, so I created a <a href="https://github.com/bert2/Nullable.Extensions">small nuget package</a> for interested parties. </p> </div> <div class="comment-date">2020-03-28 08:25 UTC</div> </div> <div class="comment" id="4f93894d232b485489e9dbba430d510c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4f93894d232b485489e9dbba430d510c">#</a></div> <div class="comment-content"> <p> Robert, thank you for writing. That feature indeed seems like an improvement. I'm currently working in a code base with that feature enabled, and I definitely think that it's better than C# without it, but <code>Maybe&lt;T&gt;</code> it isn't. There's too many edge cases and backwards compatibility issues to make it as good. </p> <p> Pragmatically, this is now what C# developers have to work with. Since it's now part of the language, it's likely that use of it will become more widespread than we could ever hope that <code>Maybe&lt;T&gt;</code> would be, so that's fine. On the other hand, <code>Maybe&lt;T&gt;</code> is now an impossible sell to C# developers. </p> <p> Because of all the edge cases that the compiler could overlook, however, I don't see how this is ever going to be as good as a language without null references in the first place. </p> </div> <div class="comment-date">2020-03-28 8:54 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functors https://blog.ploeh.dk/2018/03/22/functors 2018-03-22T16:57:00+00:00 Mark Seemann <div id="post"> <p> <em>A functor is a common abstraction. While typically associated with functional programming, functors exist in C# as well.</em> </p> <p> This article series is part of <a href="/2018/03/19/functors-applicatives-and-friends">a larger series of articles about functors, applicatives, and other mappable containers</a>. </p> <p> Programming is about abstraction, since you can't manipulate individual sub-atomic particles on your circuit boards. Some abstractions are well-known because they're rooted in mathematics. Particularly, <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a> has proven to be fertile ground for functional programming. Some of the concepts from category theory apply to object-oriented programming as well; all you need is generics, which is a feature of both C# and Java. </p> <p> In previous articles, you got an introduction to the specific <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">Test Data Builder</a> and <a href="/2017/09/18/the-test-data-generator-functor">Test Data Generator</a> functors. Functors are more common than you may realise, although in programming, we usually work with a subset of functors called <em>endofunctors</em>. In daily speak, however, we just call them <em>functors</em>. </p> <p> In the next series of articles, you'll see plenty of examples of functors, with code examples in both C#, F#, and Haskell. These articles are mostly aimed at object-oriented programmers curious about the concept. <ul> <li><a href="/2018/03/26/the-maybe-functor">The Maybe functor</a></li> <li><a href="/2019/01/14/an-either-functor">An Either functor</a></li> <li><a href="/2018/08/06/a-tree-functor">A Tree functor</a></li> <li><a href="/2019/08/19/a-rose-tree-functor">A rose tree functor</a></li> <li><a href="/2018/08/13/a-visitor-functor">A Visitor functor</a></li> <li><a href="/2018/08/20/reactive-functor">Reactive functor</a></li> <li><a href="/2018/09/03/the-identity-functor">The Identity functor</a></li> <li><a href="/2018/09/10/the-lazy-functor">The Lazy functor</a></li> <li><a href="/2018/09/24/asynchronous-functors">Asynchronous functors</a></li> <li><a href="/2021/07/19/the-state-functor">The State functor</a></li> <li><a href="/2021/08/30/the-reader-functor">The Reader functor</a></li> <li><a href="/2020/06/22/the-io-functor">The IO functor</a></li> <li><a href="/2020/10/19/monomorphic-functors">Monomorphic functors</a></li> <li><a href="/2018/12/03/set-is-not-a-functor">Set is not a functor</a></li> </ul> This list is far from exhaustive; more functors exist. Perhaps the most well-known of all functors is List, a.k.a. Sequence. C# query syntax can handle any functor, but most people only think of it as a language feature related to <code>IEnumerable&lt;T&gt;</code>. Since the combination of <code>IEnumerable&lt;T&gt;</code> and query syntax is already well-described, I'm not going to cover it explicitly here. </p> <p> If you understand how LINQ, <code>IEnumerable&lt;T&gt;</code>, and C# query syntax works, however, all other functors should feel intuitive. That's the power of abstractions. </p> <h3 id="3053c67ef9dd4d21bd68ccc1ddbd208a"> Overview <a href="#3053c67ef9dd4d21bd68ccc1ddbd208a" title="permalink">#</a> </h3> <p> The purpose of this article isn't to give you a comprehensive introduction to the category theory of functors. Rather, the purpose is to give you an opportunity to learn how it translates to object-oriented code like C#. For a great introduction to functors, see <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s <a href="https://bartoszmilewski.com/2015/01/20/functors">explanation with illustrations</a>. </p> <p> In short, a functor is a mapping between two categories. A functor maps not only objects, but also functions (called <em>morphisms</em>) between objects. For instance, a functor <em>F</em> may be a mapping between the categories <em>C</em> and <em>D</em>: </p> <p> <img src="/content/binary/functor-diagram.png" alt="Functor diagram."> </p> <p> Not only does <em>F</em> map <em>a</em> from <em>C</em> to <em>F a</em> in <em>D</em> (and likewise for <em>b</em>), it also maps the function <em>f</em> to <em>F f</em>. Functors preserve the structure between objects. You'll often hear the phrase that a functor is a <em>structure-preserving map</em>. One example of this regards lists. You can translate a <code>List&lt;int&gt;</code> to a <code>List&lt;string&gt;</code>, but the translation preserves the structure. This means that the resulting object is also a list, and the order of values within the lists doesn't change. </p> <p> In category theory, categories are often named <em>C</em>, <em>D</em>, and so on, but an example of a category could be <code>IEnumerable&lt;T&gt;</code>. If you have a function that translates integers to strings, the source <em>object</em> (that's what it's called, but it's not the same as an OOP object) could be <code>IEnumerable&lt;int&gt;</code>, and the destination object could be <code>IEnumerable&lt;string&gt;</code>. A functor, then, represents the ability to go from <code>IEnumerable&lt;int&gt;</code> to <code>IEnumerable&lt;string&gt;</code>, and since the <a href="https://msdn.microsoft.com/en-us/library/bb548891">Select</a> method gives you that ability, <code>IEnumerable&lt;T&gt;.Select</code> is a functor. In this case, you sort of 'stay within' the category of <code>IEnumerable&lt;T&gt;</code>, only you change the generic type argument, so this functor is really an endofunctor (the <em>endo</em> prefix is from Greek, meaning <em>within</em>). </p> <p> As a rule of thumb, if you have a type with a generic type argument, it's a candidate to be a functor. Such a type is not always a functor, because it also depends on where the generic type argument appears, and some other rules. </p> <p> Fundamentally, you must be able to implement a method for your generic type that looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) </pre> </p> <p> Here, I've defined the <code>Select</code> method as an instance method on a class called <code>Functor&lt;T&gt;</code>, but often, as is the case with <code>IEnumerable&lt;T&gt;</code>, the method is instead implemented as an extension method. You don't have to name it <code>Select</code>, but doing so enables query syntax in C#: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;dest&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;x&nbsp;<span style="color:blue;">in</span>&nbsp;source &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;x.ToString();</pre> </p> <p> Here, <code>source</code> is a <code>Functor&lt;int&gt;</code> object. </p> <p> If you don't name the method <code>Select</code>, it could still be a functor, but then query syntax wouldn't work. Instead, normal method-call syntax would be your only option. This is, however, a specific C# language feature. F#, for example, has no particular built-in awareness of functors, although most libraries name the central function <code>map</code>. In Haskell, <code>Functor</code> is a typeclass that defines a function called <code>fmap</code>. </p> <p> The common trait is that there's an input value (<code>Functor&lt;T&gt;</code> in the above C# code snippet), which, when combined with a mapping function (<code>Func&lt;T, TResult&gt;</code>), returns an output value of the same generic type, but with a different generic type argument (<code>Functor&lt;TResult&gt;</code>). </p> <h3 id="314a59e80ab4476381e0ed3205d35491"> Laws <a href="#314a59e80ab4476381e0ed3205d35491" title="permalink">#</a> </h3> <p> Defining a <code>Select</code> method isn't enough. The method must also obey the so-called <em>functor laws</em>. These are quite intuitive laws that govern that a functor behaves correctly. </p> <p> The first law is that mapping the identity function returns the functor unchanged. The <em>identity function</em> is a function that returns all input unchanged. (It's called the <em>identity function</em> because it's the <em>identity</em> for the <a href="/2017/11/13/endomorphism-monoid">endomorphism monoid</a>.) In F# and Haskell, this is simply a built-in function called <code>id</code>. </p> <p> In C#, you can write a demonstration of the law as a unit test: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(-101)] [<span style="color:#2b91af;">InlineData</span>(-1)] [<span style="color:#2b91af;">InlineData</span>(0)] [<span style="color:#2b91af;">InlineData</span>(1)] [<span style="color:#2b91af;">InlineData</span>(42)] [<span style="color:#2b91af;">InlineData</span>(1337)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;FunctorObeysFirstFunctorLaw(<span style="color:blue;">int</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;id&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:blue;">int</span>&gt;(value); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(sut,&nbsp;sut.Select(id)); }</pre> </p> <p> While this doesn't <em>prove</em> that the first law holds for all values and all generic type arguments, it illustrates what's going on. </p> <p> Since C# doesn't have a built-in identity function, the test creates a specialised identity function for integers, and calls it <code>id</code>. It simply returns all input values unchanged. Since <code>id</code> doesn't change the value, then <code>Select(id)</code> shouldn't change the functor, either. There's nothing more to the first law than this. </p> <p> The second law states that if you have two functions, <code>f</code> and <code>g</code>, then mapping over one after the other should be the same as mapping over the composition of <code>f</code> and <code>g</code>. In C#, you can illustrate it like this: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(-101)] [<span style="color:#2b91af;">InlineData</span>(-1)] [<span style="color:#2b91af;">InlineData</span>(0)] [<span style="color:#2b91af;">InlineData</span>(1)] [<span style="color:#2b91af;">InlineData</span>(42)] [<span style="color:#2b91af;">InlineData</span>(1337)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;FunctorObeysSecondFunctorLaw(<span style="color:blue;">int</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;&nbsp;&nbsp;&nbsp;g&nbsp;=&nbsp;i&nbsp;=&gt;&nbsp;i.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;f&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(s.Reverse().ToArray()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:blue;">int</span>&gt;(value); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(sut.Select(g).Select(f),&nbsp;sut.Select(i&nbsp;=&gt;&nbsp;f(g(i)))); }</pre> </p> <p> Here, <code>g</code> is a function that translates an <code>int</code> to a <code>string</code>, and <code>f</code> reverses a string. Since <code>g</code> returns <code>string</code>, you can compose it with <code>f</code>, which takes <code>string</code> as input. </p> <p> As the assertion points out, it shouldn't matter if you call <code>Select</code> piecemeal, first with <code>g</code> and then with <code>f</code>, or if you call <code>Select</code> with the composed function <code>f(g(i))</code>. </p> <h3 id="eb061f6fdd094731a1c52850eec7feac"> Summary <a href="#eb061f6fdd094731a1c52850eec7feac" title="permalink">#</a> </h3> <p> This is not a monad tutorial; it's a functor tutorial. Functors are commonplace, so it's worth keeping an eye out for them. If you already understand how LINQ (or similar concepts in Java) work, then functors should be intuitive, because they are all based on the same underlying maths. </p> <p> While this article is an overview article, it's also a part of <a href="/2017/10/04/from-design-patterns-to-category-theory">a larger series of articles</a> that explore what object-oriented programmers can learn from category theory. </p> <p> <strong>Next:</strong> <a href="/2018/03/26/the-maybe-functor">The Maybe functor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="419959edeb1c45b785bb732a83c5134d"> <div class="comment-author"><a href="https://twitter.com/philip_schwarz">Philip Schwarz</a> <a href="#419959edeb1c45b785bb732a83c5134d">#</a></div> <div class="comment-content">For anyone interested in an aid to internalising functor laws, check out the first four diagrams of the following slide deck, which summarise the laws (in category-theoretic terms) and show an example of the laws in action for a functor from one monoid to another: <a href="https://www.slideshare.net/pjschwarz/functor-laws">Functor Laws</a><br></div> <div class="comment-date">2018-08-18 11:00 UTC</div> </div> <div class="comment" id="dee7ec216a464248b6103ea0979948ab"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#dee7ec216a464248b6103ea0979948ab">#</a></div> <div class="comment-content"> <blockquote> As a rule of thumb, if you have a type with a generic type argument, it's a candidate to be a functor. </blockquote> <p> My guess is that you were thinking of Haskell's <code>Functor</code> typeclass when you wrote that. As <a href="https://wiki.haskell.org/Functor#Description">Haskell documentation for <code>Functor</code></a> says, </p> <blockquote> An abstract datatype <code>f a</code>, which has the ability for its value(s) to be mapped over, can become an instance of the <code>Functor</code> typeclass. </blockquote> <p> To explain the Haskell syntax for anyone unfamiliar, <code>f</code> is a generic type with type parameter <code>a</code>. So indeed, it is necessary to be a Haskell type to be generic to be an instance of the <code>Functor</code> typeclass. However, the concept of a functor in programming is not limited to Haskell's <code>Functor</code> typeclass. </p> <blockquote> In category theory, categories are often named <em>C</em>, <em>D</em>, and so on, but an example of a category could be <code>IEnumerable&lt;T&gt;</code>. If you have a function that translates integers to strings, the source <em>object</em> (that's what it's called, but it's not the same as an OOP object) could be <code>IEnumerable&lt;int&gt;</code>, and the destination object could be <code>IEnumerable&lt;string&gt;</code>. A functor, then, represents the ability to go from <code>IEnumerable&lt;int&gt;</code> to <code>IEnumerable&lt;string&gt;</code>, and since the <a href="https://msdn.microsoft.com/en-us/library/bb548891">Select</a> method gives you that ability, <code>IEnumerable&lt;T&gt;.Select</code> is a functor. In this case, you sort of 'stay within' the category of <code>IEnumerable&lt;T&gt;</code>, only you change the generic type argument, so this functor is really an endofunctor (the <em>endo</em> prefix is from Greek, meaning <em>within</em>). </blockquote> <p> For any type <code>T</code> including non-generic types, the category containing only a single object representing <code>T</code> can have (nontrivial) endofunctors. For example, consider a record of type <code>R</code> in F# with a field of both name and type <code>F</code>. Then <code>let mapF f r = { r with F = f r.F }</code> is the <code>map</code> function the satisfies the functor laws for the field <code>F</code> for record <code>R</code>. This holds even when <code>F</code> is not a type parameter of <code>R</code>. I would say that <code>R</code> is a functor in <code>F</code>. </p> <p> I have realized within just the past three weeks how valuable it is to have a <code>map</code> function for every field of every record that I define. For example, I was able to greatly <a href="https://github.com/elmish/Elmish.WPF/pull/241">simplify the <code>SubModelSeq</code> sample in Elmish.WPF</a> in part because of such <code>map</code> functions. I am especially pleased with the main <code>update</code> function, which heavily uses these <code>map</code> functions. It went from <a href="https://github.com/elmish/Elmish.WPF/blob/99e66723f9990a29889c4a50ba25e0004906a8e8/src/Samples/SubModelSeq/Program.fs#L130-L166">this</a> to <a href="https://github.com/elmish/Elmish.WPF/blob/51ea9f210244c17e0d54773e46cc824cc7457d03/src/Samples/SubModelSeq/Program.fs#L175-L181">this</a>. </p> <p> In particular, I was able to eta-reduce the <code>Model</code> argument. To be clear, the goal is not point-free style for its own sake. Instead, it is one of perspective. In the previous implementation, the body explicitly depended on both the <code>Msg</code> and <code>Model</code> arguments and explicitly returned a <code>Model</code> instance. In the new implementation, the body only explicitly depends on the <code>Msg</code> argument and explicitly returns a function with type <code>Model -&gt; Model</code>. By only having to pattern match on the <code>Msg</code> argument (and not having to worry about how to exactly map from one <code>Model</code> instance to the next), the implementation is much simpler. Such is the expressive power of high-order functions. </p> <p> All this (and much more in my application at work) from realizing that a functor doesn't have to be generic. </p> <p> I am looking into the possibility of generating these <code>map</code> functions using <a href="https://github.com/MoiraeSoftware/myriad">Myrid</a>. </p> </div> <div class="comment-date">2020-08-03 15:19 UTC</div> </div> <div class="comment" id="2e15f5fc319840b98e2868dc3cb4953f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2e15f5fc319840b98e2868dc3cb4953f">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I think that you're right. </p> <p> To be honest, I still find the category theory underpinnings of functional programming concepts to be a little hazy. For instance, in category theory, a <em>functor</em> is a mapping <em>between</em> categories. When category theory is translated into Haskell, people usually discuss Haskell as constituting a single large category called <strong>Hask</strong>. In that light, everything that goes on in Haskell just maps from objects in Hask to objects in Hask. In that light, I still find it difficult to distinguish between morphisms and functors. </p> <p> I'm going to need to think about this some more, but my intuition is that one can also find 'smaller' categories embedded in Hask, e.g. the category of Boolean values, or the category of strings, and so on. </p> <p> What I'm trying to get across is that I haven't thought enough about the boundaries of a concept such as <em>functor</em>. It's clear that some containers with generic types form functors, but are generics <em>required?</em> You argue that they aren't required, and I think that you are right. </p> <p> Generics are, however, required at the language level, as you've also pointed out. I think that this is also the case for F# and C#, but I admit that since the thought hadn't crossed my mind until now, I actually don't know. If you define a <code>Select</code> method that takes a <code>Func&lt;T, T&gt;</code> instead of a <code>Func&lt;T, TResult&gt;</code>, does query syntax still work? </p> <p> For what it's worth, there's <a href="https://hackage.haskell.org/package/mono-traversable/docs/Data-MonoTraversable.html">a Haskell library</a> that defines some type classes for monomorphic containers, including <code>MonoFunctor</code>. As it claims: <blockquote> <p> "All of the laws for the polymorphic typeclasses apply to their monomorphic cousins." </p> </blockquote> This strongly suggests that <code>MonoFunctor</code> instances are functors, too. </p> <p> I agree that despite the brevity of <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/copy-and-update-record-expressions">copy-and-update expressions</a>, the particular use case that you describe can still seem verbose to sensitive souls. It doesn't look too bad with the example you give (<code>r.F</code>), but usually, value and field names are longer, and then reading the field, applying a function, and setting the field again starts to feel inconvenient. I've been there. </p> <p> Once you start down the path of wanting to make it easier to 'reach into' a record structure to update parts of it, you quickly encounter <em>lenses</em>. As far as I can tell, your <code>map</code> function looks equivalent to a function that the <a href="http://hackage.haskell.org/package/lens-4.19.2">lens library</a> calls <code>over</code>. </p> <p> I agree that lenses can solve some real problems, and it seems that your use case was one of them. </p> </div> <div class="comment-date">2020-08-11 8:26 UTC</div> </div> <div class="comment" id="8e9d6ebde47d44adbb49b2ac3c33498b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8e9d6ebde47d44adbb49b2ac3c33498b">#</a></div> <div class="comment-content"> <p> FWIW, I recently <a href="https://stackoverflow.com/a/63611532/126014">answered a Stack Overflow question</a> about the presence of general functors in programming. Perhaps you'll find it useful. </p> </div> <div class="comment-date">2020-09-02 5:33 UTC</div> </div> <div class="comment" id="90364937188b44378608284b1933301d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#90364937188b44378608284b1933301d">#</a></div> <div class="comment-content"> <p> Also, I've now added <a href="/2020/10/19/monomorphic-functors">an article about monomorphic functors</a>. </p> </div> <div class="comment-date">2020-10-22 16:28 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functors, applicatives, and friends https://blog.ploeh.dk/2018/03/19/functors-applicatives-and-friends 2018-03-19T08:35:00+00:00 Mark Seemann <div id="post"> <p> <em>Functors and related data structures are containers of values. It's a family of abstractions. An overview for object-oriented programmers.</em> </p> <p> This article series is part of <a href="/2017/10/04/from-design-patterns-to-category-theory">an even larger series of articles about the relationship between design patterns and category theory.</a> </p> <p> If you've worked with C# or Java recently, you've most likely encountered types such as <code>Foo&lt;T&gt;</code> or <code>Bar&lt;T&gt;</code> (specifically, on .NET, e.g. <a href="https://msdn.microsoft.com/en-us/library/6sh2ey19">List&lt;T&gt;</a>). Perhaps you've also noticed that often, you can translate the type inside of <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">the container</a>. For example, if you have a <code>Foo&lt;string&gt;</code>, perhaps you can call some method on it that returns a <code>Foo&lt;int&gt;</code>. If so, it may be a <em>functor</em>. </p> <p> Not all generic types are functors. In order to be a functor, a generic type must obey a couple of intuitive laws. You'll learn about those in future articles. </p> <p> Some functors have extra capabilities, and you'll learn about some of those as well. Some are called <em>applicative functors</em>, and some are called <em>bifunctors</em>. There are others, as well. </p> <p> <img src="/content/binary/functors-applicatives-bifunctors.png" alt="Functors, applicative functors, and bifunctors as subsets of each other."> </p> <p> All applicative functors are functors, and this is true for bifunctors as well. </p> <p> In this article series, you'll learn about the following categories: <ul> <li><a href="/2018/03/22/functors">Functors</a> <ul> <li><a href="/2018/03/26/the-maybe-functor">The Maybe functor</a></li> <li><a href="/2019/01/14/an-either-functor">An Either functor</a></li> <li><a href="/2018/08/06/a-tree-functor">A Tree functor</a></li> <li><a href="/2019/08/19/a-rose-tree-functor">A rose tree functor</a></li> <li><a href="/2018/08/13/a-visitor-functor">A Visitor functor</a></li> <li><a href="/2018/08/20/reactive-functor">Reactive functor</a></li> <li><a href="/2018/09/03/the-identity-functor">The Identity functor</a></li> <li><a href="/2018/09/10/the-lazy-functor">The Lazy functor</a></li> <li><a href="/2018/09/24/asynchronous-functors">Asynchronous functors</a></li> <li><a href="/2021/07/19/the-state-functor">The State functor</a></li> <li><a href="/2021/08/30/the-reader-functor">The Reader functor</a></li> <li><a href="/2020/06/22/the-io-functor">The IO functor</a></li> <li><a href="/2020/10/19/monomorphic-functors">Monomorphic functors</a></li> <li><a href="/2018/12/03/set-is-not-a-functor">Set is not a functor</a></li> </ul> </li> <li><a href="/2018/10/01/applicative-functors">Applicative functors</a> <ul> <li><a href="/2018/10/08/full-deck">Full deck</a></li> <li><a href="/2018/10/15/an-applicative-password-list">An applicative password list</a></li> <li><a href="/2018/10/22/applicative-combinations-of-functions">Applicative combinations of functions</a></li> <li><a href="/2018/10/29/the-maybe-applicative-functor">The Maybe applicative functor</a></li> <li><a href="/2018/11/05/applicative-validation">Applicative validation</a></li> <li><a href="/2018/11/26/the-test-data-generator-applicative-functor">The Test Data Generator applicative functor</a></li> <li><a href="/2018/12/10/danish-cpr-numbers-in-f">Danish CPR numbers in F#</a></li> <li><a href="/2018/12/17/the-lazy-applicative-functor">The Lazy applicative functor</a></li> <li><a href="/2019/04/22/applicative-monoids">Applicative monoids</a></li> </ul> </li> <li><a href="/2018/12/24/bifunctors">Bifunctors</a> <ul> <li><a href="/2018/12/31/tuple-bifunctor">Tuple bifunctor</a></li> <li><a href="/2019/01/07/either-bifunctor">Either bifunctor</a></li> <li><a href="/2019/08/12/rose-tree-bifunctor">Rose tree bifunctor</a></li> </ul> </li> <li><a href="/2021/09/02/contravariant-functors">Contravariant functors</a> <ul> <li><a href="/2021/09/06/the-command-handler-contravariant-functor">The Command Handler contravariant functor</a></li> <li><a href="/2021/09/09/the-specification-contravariant-functor">The Specification contravariant functor</a></li> <li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li> <li><a href="/2021/10/04/reader-as-a-contravariant-functor">Reader as a contravariant functor</a></li> <li><a href="/2021/10/25/functor-variance-compared-to-cs-notion-of-variance">Functor variance compared to C#'s notion of variance</a></li> <li><a href="/2022/03/21/contravariant-dependency-injection">Contravariant Dependency Injection</a></li> </ul> </li> <li><a href="/2021/11/01/profunctors">Profunctors</a> <ul> <li><a href="/2021/11/08/reader-as-a-profunctor">Reader as a profunctor</a></li> </ul> </li> <li><a href="/2022/08/01/invariant-functors">Invariant functors</a> <ul> <li><a href="/2022/08/08/endomorphism-as-an-invariant-functor">Endomorphism as an invariant functor</a></li> <li><a href="/2022/08/29/natural-transformations-as-invariant-functors">Natural transformations as invariant functors</a></li> <li><a href="/2022/12/26/functors-as-invariant-functors">Functors as invariant functors</a></li> <li><a href="/2023/02/06/contravariant-functors-as-invariant-functors">Contravariant functors as invariant functors</a></li> </ul> </li> <li><a href="/2022/03/28/monads">Monads</a> <ul> <li><a href="/2022/04/04/kleisli-composition">Kleisli composition</a></li> <li><a href="/2022/04/11/monad-laws">Monad laws</a></li> <li><a href="/2022/04/19/the-list-monad">The List monad</a></li> <li><a href="/2022/04/25/the-maybe-monad">The Maybe monad</a></li> <li><a href="/2022/05/09/an-either-monad">An Either monad</a></li> <li><a href="/2022/05/16/the-identity-monad">The Identity monad</a></li> <li><a href="/2022/05/30/the-lazy-monad">The Lazy monad</a></li> <li><a href="/2022/06/06/asynchronous-monads">Asynchronous monads</a></li> <li><a href="/2022/06/20/the-state-monad">The State monad</a></li> <li><a href="/2022/11/14/the-reader-monad">The Reader monad</a></li> <li><a href="/2023/01/09/the-io-monad">The IO monad</a></li> <li><a href="/2023/02/27/test-data-generator-monad">Test Data Generator monad</a></li> </ul> </li> <li><a href="/2022/07/11/functor-relationships">Functor relationships</a> <ul> <li><a href="/2022/07/18/natural-transformations">Natural transformations</a></li> <li>Traversals</li> <li>Functor compositions</li> <li>Functor products</li> <li>Functor sums</li> </ul> </li> </ul> You'll see plenty of examples along the way. Most examples will be in C#, but some articles will have code examples in <a href="http://fsharp.org">F#</a> or <a href="https://www.haskell.org">Haskell</a>. You can read or skip those articles as you prefer. </p> <p> <strong>Next:</strong> <a href="/2018/03/22/functors">Functors</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Composite as a monoid https://blog.ploeh.dk/2018/03/12/composite-as-a-monoid 2018-03-12T09:39:00+00:00 Mark Seemann <div id="post"> <p> <em>When can you use the Composite design pattern? When the return types of your methods form monoids.</em> </p> <p> This article is part of <a href="/2018/03/05/some-design-patterns-as-universal-abstractions/">a series of articles about design patterns and their universal abstraction counterparts</a>. </p> <p> The <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a> design pattern is a powerful way to structure code, but not all objects are composable. When is an object composable? This article explores that question. </p> <p> In short, Composites are <a href="/2017/10/06/monoids">monoids</a>. </p> <p> <img src="/content/binary/composite-as-subset-of-monoid.png" alt="Composite shown as a subset of the set of monoids."> </p> <p> Not all monoids are Composites, but as far as I can tell, all Composites are monoids. </p> <h3 id="ab8f3501700042419375b91315249a77"> Composite <a href="#ab8f3501700042419375b91315249a77" title="permalink">#</a> </h3> <p> First, I'll use various <a href="/2018/01/08/software-design-isomorphisms">software design isomorphisms</a> to put Composite in a canonical form. From <a href="/2018/01/15/unit-isomorphisms">unit isomorphisms</a>, <a href="/2018/01/22/function-isomorphisms">function isomorphisms</a>, and <a href="/2018/01/29/argument-list-isomorphisms">argument list isomorphisms</a>, we know that we can represent any method as a method or function that takes a single argument, and returns a single output value. From <a href="/2018/02/19/abstract-class-isomorphism">abstract class isomorphism</a> we know that we can represent an abstract class with interfaces. Thus, you can represent the interface for a Composite like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IInterface1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Out1</span>&nbsp;Op1(<span style="color:#2b91af;">In1</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Out2</span>&nbsp;Op2(<span style="color:#2b91af;">In2</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Out3</span>&nbsp;Op3(<span style="color:#2b91af;">In3</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;operations...</span> }</pre> </p> <p> In order to create a Composite, we must be able to take an arbitrary number of implementations and make them look like a single object. </p> <h3 id="30b860e1b0904901a41b1ead2424a74d"> Composite as monoid <a href="#30b860e1b0904901a41b1ead2424a74d" title="permalink">#</a> </h3> <p> You have a set of implementations of <code>IInterface1</code>. In order to create a Composite, you loop over all of those implementations in order to produce an aggregated result. Imagine that you have to implement a <code>CompositeInterface1</code> class that composes <code>imps</code>, an <code>IReadOnlyCollection&lt;IInterface1&gt;</code>. In order to implement <code>Op1</code>, you'd have to write code like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Out1</span>&nbsp;Op1(<span style="color:#2b91af;">In1</span>&nbsp;arg) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;imp&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">this</span>.imps) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;out1&nbsp;=&nbsp;imp.Op1(arg); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Somehow&nbsp;combine&nbsp;this&nbsp;out1&nbsp;value&nbsp;with&nbsp;previous&nbsp;values</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Return&nbsp;combined&nbsp;Out1&nbsp;value</span> }</pre> </p> <p> This implies that we have an ordered, finite sequence of implementations: <em>imp1, imp2, imp3, ...</em>. In C#, we could represent such a sequence with the type <code>IReadOnlyCollection&lt;IInterface1&gt;</code>. Somehow, we need to turn that collection into a single <code>IInterface1</code> value. In other words, we need a translation of the type <code>IReadOnlyCollection&lt;IInterface1&gt; -&gt; IInterface1</code>. </p> <p> If we look to <a href="https://www.haskell.org">Haskell</a> for inspiration for a moment, let's replace <code>IReadOnlyCollection&lt;T&gt;</code> with Haskell's built-in linked list. This means that we need a function of the type <code>[IInterface1] -&gt; IInterface1</code>, or, more generally, <code>[a] -&gt; a</code>. This function exists for all <code>a</code> as long as <code>a</code> forms a monoid; it's called <code>mconcat</code>: </p> <p> <pre>mconcat :: Monoid a =&gt; [a] -&gt; a</pre> </p> <p> We also know from a previous article that <a href="/2017/11/20/monoids-accumulate">a collection of monoids can be reduced to a single monoid</a>. Notice how the above outline of a composite implementation of <code>Op1</code> looks similar to the <code>Accumulate</code> method shown in the linked article. If <code>IInterface1</code> can form a monoid, then you can make a Composite. </p> <h3 id="79bbc5e304734bb180724a77929e23b6"> Objects as monoids <a href="#79bbc5e304734bb180724a77929e23b6" title="permalink">#</a> </h3> <p> When can an object (like <code>IInterface1</code>) form a monoid? </p> <p> From <a href="/2018/02/12/object-isomorphisms">object isomorphisms</a> we know that we can decompose an object with <em>n</em> members to <em>n</em> static methods. This means that instead of analysing all of <code>IInterface1</code>, we can consider the properties of each method in isolation. The properties of an object is the consolidation of the properties of all the methods. </p> <p> Recall, still from <em>object isomorphisms</em>, that we can represent an object as a tuple of functions. Moreover, <a href="/2017/10/30/tuple-monoids">if you have a tuple of monoids, then the tuple also forms monoid</a>! </p> <p> In order to make an object a monoid, then, you have to make each method a monoid. When is a method a monoid? <a href="/2017/11/06/function-monoids">A method is a monoid when its return type forms a monoid</a>. </p> <p> That's it. An interface like <code>IInterface1</code> is a monoid when <code>Out1</code>, <code>Out2</code>, <code>Out3</code>, and so on, form monoids. If that's the case, you can make a Composite. </p> <h3 id="75ca550ef1ef42b3a559bece1ff6b52d"> Examples <a href="#75ca550ef1ef42b3a559bece1ff6b52d" title="permalink">#</a> </h3> <p> From <em>unit isomorphism</em>, we know that we can represent C#'s and Java's <em>void</em> keywords with methods returning <em>unit</em>, and <em>unit</em> is a monoid. All methods that return <code>void</code> can be part of a Composite, but we already knew that <a href="/2011/03/22/CommandsareComposable">Commands are composable</a>. If you search for examples of the Composite design pattern, you'll find more than one variation involving drawing shapes on a digital canvas, with the central operation being a <code>Draw</code> method with a <code>void</code> return type. </p> <p> Another example could be calculation of the price of a shopping basket. If you have an interface method of the type <code>decimal Calculate(Basket basket)</code>, you could have several implementations: <ul> <li>Add all the item prices together</li> <li>Apply a discount (a negative number)</li> <li>Calculate sales tax</li> </ul> These could be three implementations of the same interface method, and since decimal numbers form a monoid over addition, then you can make a Composite basket calculator out of the three implementations. For a detailed example, see the <a href="/2018/05/17/composite-as-a-monoid-a-business-rules-example">coda containing a business rules example</a>. </p> <p> Boolean values also form at least two monoids (<em>any</em> and <em>all</em>), so any method you have that returns a Boolean value can be used in a Composite. You could, for example, have a list of criteria for granting a loan. Each such business rule returns <code>true</code> if it evaluates that the loan should be granted, and <code>false</code> otherwise. If you have more than one business rule, you can create a Composite that returns <code>true</code> only if all the individual rules return <code>true</code>. </p> <p> If you have a method that returns a string, then that is also a candidate for inclusion in a Composite, if string concatenation makes sense in the domain in question. </p> <p> You probably find it fairly mundane that you can create a Composite if all the methods involved return numbers, strings, Booleans, or nothing. The result generalises, however, to all monoids, including more complex types, including methods that return other interfaces that themselves form monoids, and so on recursively. </p> <h3 id="3d21d64a1ec64432abd38ebcd09332a6"> Granularity <a href="#3d21d64a1ec64432abd38ebcd09332a6" title="permalink">#</a> </h3> <p> The result, then, is that you can make a Composite when <em>all</em> methods in your interface have monoidal return types. If only a single method has a return type that isn't a monoid, you can't aggregate that value, and you can't make a Composite. </p> <p> Your interface can have as many methods you like, but they must all be monoids. Even one rogue method will prevent you from being able to create a Composite. This is another argument for <a href="https://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a>. The smaller an interface is, the more likely it is that you can make a Composite out of it. If you follow that line of reasoning to its ultimate conclusion, you'll <a href="/2014/03/10/solid-the-next-step-is-functional">design your interfaces with a single member each</a>. </p> <h3 id="f98c1cf3efa24c919cc10fba5d672435"> Relaxation <a href="#f98c1cf3efa24c919cc10fba5d672435" title="permalink">#</a> </h3> <p> There can be some exceptions to the rule that all return values must be monoids. If you have at least one implementation of your interface, then a <a href="/2017/11/27/semigroups">semigroup</a> may be enough. Recall that monoids accumulate like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Accumulate(<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Foo</span>&gt;&nbsp;foos) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;acc&nbsp;=&nbsp;Identity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;f&nbsp;<span style="color:blue;">in</span>&nbsp;foos) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;acc.Op(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; }</pre> </p> <p> You only need <code>Identity</code> in order to start the accumulation, and to have something to return in case you have no implementations. If you have at least one implementation, you don't need the identity, and then <a href="/2017/12/11/semigroups-accumulate">a semigroup is enough to accumulate</a>. Consider the <a href="/2017/12/04/bounding-box-semigroup">bounding box example</a>. If you have a method that returns <code>BoundingBox</code> values, you can still make a Composite out of such an interface, as long as you have at least one implementation. There's no 'identity' bounding box, but it makes intuitive sense that you can still compose bounding boxes into bigger bounding boxes. </p> <p> Haskell formalises the rule for semigroups: </p> <p> <pre>sconcat :: Semigroup a =&gt; Data.List.NonEmpty.NonEmpty a -&gt; a</pre> </p> <p> The <code>sconcat</code> function reduces any non-empty list of any semigroup <code>a</code> to a single <code>a</code> value. </p> <p> If you have a non-empty list of implementations, then perhaps you don't even need a semigroup. Perhaps any <a href="/2017/12/27/magmas">magma</a> will work. Be aware, however, that the lack of associativity will cause the order of implementations to matter. </p> <p> Technically, you may be able to program a Composite from a magma, but I'd suggest caution. The monoid and semigroup laws are intuitive. A magma without those properties may not form an intuitive Composite. While it may compile, it may have surprising, or counter-intuitive, behaviour. I'd favour sticking to monoids or semigroups. </p> <h3 id="b30a2eafacfb4ddf98caec0c2030bf49"> Summary <a href="#b30a2eafacfb4ddf98caec0c2030bf49" title="permalink">#</a> </h3> <p> When is an object-oriented design composable? Composition could mean more than one thing, but this article has focused exclusively on the Composite design pattern. When can you use the Composite pattern? When all method return types are monoids. </p> <p> <strong>Next: </strong> <a href="/2018/04/09/coalescing-composite-as-a-monoid">Coalescing Composite as a monoid</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Some design patterns as universal abstractions https://blog.ploeh.dk/2018/03/05/some-design-patterns-as-universal-abstractions 2018-03-05T08:10:00+00:00 Mark Seemann <div id="post"> <p> <em>Some design patterns can be formalised by fundamental abstractions.</em> </p> <p> This article series submits results based on the work presented in <a href="/2017/10/04/from-design-patterns-to-category-theory">an even larger series of articles about the relationship between design patterns and category theory</a>. </p> <p> Wouldn't it be wonderful if you could assemble software from predefined building blocks? This idea is <em>old</em>, and has been the driving force behind object-oriented programming (OOP). In <a href="https://www.coupland.com">Douglas Coupland</a>'s 1995 novel <a href="http://bit.ly/microserfs">Microserfs</a>, the characters attempt to reach that goal through a project called <em>Oop!</em>. <a href="https://en.wikipedia.org/wiki/Lego">Lego bricks</a> play a role as a metaphor as well. </p> <p> <img src="/content/binary/lego-bricks-as-connections.png" alt="Lego bricks."> </p> <p> Decades later, it doesn't look like we're much nearer that goal than before, but I believe that we'd made at least two (rectifiable) mistakes along the way: <ul> <li>Granularity</li> <li>Object-orientation</li> </ul> While I'm going to discuss both of these briefly, my principal message is one of hope. I still think we can assemble software from predefined 'things', but I believe that these 'things' are small and 'objects' in a different sense than normal. </p> <h3 id="e1bedfebfd5d4292a18a5a2eecf763c9"> Granularity <a href="#e1bedfebfd5d4292a18a5a2eecf763c9" title="permalink">#</a> </h3> <p> Over the years, I've seen several attempts at reducing software development to a matter of putting things together. These attempts have invariably failed. </p> <p> I believe that one of the reasons for failure is that such projects tend to aim at coarse-grained building blocks. As I explain in the <em>Interface Segregation Principle</em> module of my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID</a> Pluralsight course, granularity is a crucial determinant for your ability to create. The coarser-grained the building blocks, the harder it is to create something useful. </p> <p> Most attempts at software-as-building-blocks have used big, specialised building blocks aimed at non-technical users (<em>"Look! Create an entire web site without writing a single line of code!"</em>). That's just like <a href="https://en.wikipedia.org/wiki/Lego_Duplo">Duplo</a>. You can create exactly what the blocks were designed for, but as soon as you try to create something new and original, you can't. </p> <h3 id="11a18062d294481bb092e2d4c364b4a4"> Object-orientation <a href="#11a18062d294481bb092e2d4c364b4a4" title="permalink">#</a> </h3> <p> OOP is another attempt at software-as-building-blocks. In .NET (the OOP framework with which I'm most familiar) the Base Class Library (BCL) is enormous. Many of the reusable objects in the BCL are fine-grained, so at least it's often possible to put them together to create something useful. The problem with an object-oriented library like the .NET BCL, however, is that all the objects are <em>special</em>. </p> <p> The vision was always that software 'components' would be able to 'click' together, just like Lego bricks. The BCL isn't like that. Typically, objects have nothing in common apart from the useless <code>System.Object</code> base class. There's no system. In order to learn how the BCL works, you have to learn the ins and outs of every single class. </p> <p> Better know a framework. </p> <p> It doesn't help that OOP was never formally defined. Every time you see or hear a discussion about what 'real' object-orientation is, you can be sure that sooner or later, someone will say: "...but that's not what <a href="https://en.wikipedia.org/wiki/Alan_Kay">Alan Kay</a> had in mind." </p> <p> What Alan Kay had in mind is still unclear to me, but it seems safe to say that it wasn't what we have now (C++, Java, C#). </p> <h3 id="58ae1b0666de46d890675b19f525d42d"> Building blocks from category theory <a href="#58ae1b0666de46d890675b19f525d42d" title="permalink">#</a> </h3> <p> While we (me included) have been on an a thirty-odd year long detour around object-orientation, I don't think all is lost. I still believe that a Lego-brick-like system exists for software development, but I think that it's a system that we have to <em>discover</em> instead of invent. </p> <p> As I already covered in the <a href="/2017/10/04/from-design-patterns-to-category-theory">introductory article</a>, <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a> does, in fact, discuss 'objects'. It's not the same type of object that you know from C# or Java, but some of them do consist of data and behaviour - <a href="/2017/10/06/monoids">monoids</a>, for example, or <a href="/2018/03/22/functors">functors</a>. Such object are more like types than objects in the OOP sense. </p> <p> Another, more crucial, difference to object-oriented programming is that these objects are lawful. An object is only a monoid if it obeys the monoid laws. An object is only a functor if it obeys the functor laws. </p> <p> Such objects are still fine-grained building blocks, but they fit into a system. You don't have to learn tens of thousands of specific objects in order to get to know a framework. You need to understand the system. You need to understand monoids, functors, <a href="/2018/10/01/applicative-functors">applicatives</a>, and a few other universal abstractions (yes: monads too). </p> <p> Many of these universal abstractions were almost discovered by the Gang of Four twenty years ago, but they weren't quite in place then. Much of that has to do with the fact that functional programming didn't seem like a realistic alternative back then, because of hardware limitations. This has all changed to the better. </p> <h3 id="ae905a16cecc41ffb316a9f291ed5d78"> Specific patterns <a href="#ae905a16cecc41ffb316a9f291ed5d78" title="permalink">#</a> </h3> <p> In the <a href="/2017/10/04/from-design-patterns-to-category-theory">introductory article about the relationship between design patterns and category theory</a>, you learned that some design patterns significantly overlap concepts from category theory. In this article series, we'll explore the relationships between some of the classic patterns and category theory. I'm not sure that all the patterns from <a href="http://amzn.to/XBYukB">Design Patterns</a> can be reinterpreted as universal abstractions, but the following subset seems promising: <ul> <li><a href="/2018/03/12/composite-as-a-monoid">Composite as a monoid</a> <ul> <li><a href="/2018/04/09/coalescing-composite-as-a-monoid">Coalescing Composite as a monoid</a></li> <li><a href="/2018/04/16/endomorphic-composite-as-a-monoid">Endomorphic Composite as a monoid</a></li> </ul> </li> <li><a href="/2018/04/23/null-object-as-identity">Null Object as identity</a></li> <li><a href="/2020/02/17/builder-as-a-monoid">Builder as a monoid</a></li> <li><a href="/2018/06/25/visitor-as-a-sum-type">Visitor as a sum type</a></li> <li><a href="/2019/07/22/chain-of-responsibility-as-catamorphisms">Chain of Responsibility as catamorphisms</a></li> <li><a href="/2022/09/05/the-state-pattern-and-the-state-monad">The State pattern and the State monad</a> <ul> <li><a href="/2022/09/26/refactoring-the-tcp-state-pattern-example-to-pure-functions">Refactoring the TCP State pattern example to pure functions</a></li> <li><a href="/2022/10/10/refactoring-a-saga-from-the-state-pattern-to-the-state-monad">Refactoring a saga from the State pattern to the State monad</a></li> </ul> </li> <li><a href="/2021/11/29/postels-law-as-a-profunctor">Postel's law as a profunctor</a></li> <li><a href="/2021/12/06/the-liskov-substitution-principle-as-a-profunctor">The Liskov Substitution Principle as a profunctor</a></li> <li><a href="/2021/12/13/backwards-compatibility-as-a-profunctor">Backwards compatibility as a profunctor</a></li> </ul> Granted, Null Object is actually not from <em>Design Patterns</em>, but as we shall see, it's a special case of Composite, so it fits well into that group. </p> <h3 id="cecda3b2021848f2b68874c1ae80ce63"> Summary <a href="#cecda3b2021848f2b68874c1ae80ce63" title="permalink">#</a> </h3> <p> Some design patterns closely resemble categorical objects. This article provides an overview, whereas the next articles in the series will dive into specifics. </p> <p> <strong>Next:</strong> <a href="/2018/03/12/composite-as-a-monoid">Composite as a monoid</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Inheritance-composition isomorphism https://blog.ploeh.dk/2018/02/26/inheritance-composition-isomorphism 2018-02-26T07:24:00+00:00 Mark Seemann <div id="post"> <p> <em>Reuse via inheritance is isomorphic to composition.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. </p> <p> Chapter 1 of <a href="http://amzn.to/XBYukB">Design Patterns</a> admonishes: <blockquote> Favor object composition over class inheritance </blockquote> People sometimes struggle with this, because they use inheritance as a means to achieve reuse. That's not necessary, because you can use object composition instead. </p> <p> In the <a href="/2018/02/19/abstract-class-isomorphism">previous article</a>, you learned that an abstract class can be refactored to a concrete class with injected dependencies. </p> <p> Did you notice that there was an edge case that I didn't cover? </p> <p> I didn't cover it because I think it deserves its own article. The case is when you want to reuse a base class' functionality in a derived class. How do you do that with Dependency Injection? </p> <h3 id="dfaf463837d64ea1918d71ea7ed7d98a"> Calling base <a href="#dfaf463837d64ea1918d71ea7ed7d98a" title="permalink">#</a> </h3> <p> Imagine a virtual method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:#2b91af;">OutVirt1</span>&nbsp;Virt1(<span style="color:#2b91af;">InVirt1</span>&nbsp;arg) </pre> </p> <p> In C#, a method is virtual when explicitly marked with the <code>virtual</code> keyword, whereas this is the default in Java. When you override a virtual method in a derived class, you can still invoke the parent class' implementation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:#2b91af;">OutVirt1</span>&nbsp;Virt1(<span style="color:#2b91af;">InVirt1</span>&nbsp;arg) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this&nbsp;and&nbsp;arg</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;baseResult&nbsp;=&nbsp;<span style="color:blue;">base</span>.Virt1(arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;return&nbsp;an&nbsp;OutVirt1&nbsp;value</span> }</pre> </p> <p> When you override a virtual method, you can use the <code>base</code> keyword to invoke the parent implementation of the method that you're overriding. The enables you to reuse the base implementation, while at the same time adding new functionality. </p> <h3 id="842b27b75f9b420ca41e7c40b15b9ad5"> Virtual method as interface <a href="#842b27b75f9b420ca41e7c40b15b9ad5" title="permalink">#</a> </h3> <p> If you perform the refactoring to Dependency Injection shown in the previous article, you now have an interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IVirt1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">OutVirt1</span>&nbsp;Virt1(<span style="color:#2b91af;">InVirt1</span>&nbsp;arg); }</pre> </p> <p> as well as a default implementation. In the previous article, I showed an example where a single class implements several 'virtual member interfaces'. In order to make this particular example clearer, however, here you instead see a variation where the default implementation of <code>IVirt1</code> is in a class that only implements that interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DefaultVirt1</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IVirt1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">OutVirt1</span>&nbsp;Virt1(<span style="color:#2b91af;">InVirt1</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutVirt1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> <code>DefaultVirt1.Virt1</code> corresponds to the original virtual method on the abstract class. How can you 'override' this default implementation, while still make use of it? </p> <h3 id="c2a7f21e019740d59b132b017f7e5822"> From base to composition <a href="#c2a7f21e019740d59b132b017f7e5822" title="permalink">#</a> </h3> <p> You have a default implementation, and instead of replacing all of it, you want to somehow enhance it, but still use the 'base' implementation. Instead of inheritance, you can use composition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">OverridingVirt1</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IVirt1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IVirt1</span>&nbsp;@base&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultVirt1</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">OutVirt1</span>&nbsp;Virt1(<span style="color:#2b91af;">InVirt1</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this&nbsp;and&nbsp;arg</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;baseResult&nbsp;=&nbsp;@base.Virt1(arg); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;return&nbsp;an&nbsp;OutVirt1&nbsp;value</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In order to drive home the similarity, I named the class field <code>@base</code>. I couldn't use <code>base</code> as a name, because that's a keyword in C#, but you can use the prefix <code>@</code> in order to use a keyword as a legal C# name. Notice that the body of <code>OverridingVirt1.Virt1</code> is almost identical to the above, inheritance-based overriding method. </p> <p> As a variation, you can inject <code>@base</code> via the constructor of <code>OverridingVirt1</code>, in which case you have a <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a>. </p> <h3 id="6425171e658e474cbe2ab0634b5dbb40"> Isomorphism <a href="#6425171e658e474cbe2ab0634b5dbb40" title="permalink">#</a> </h3> <p> If you already have an interface with a 'default implementation', and you want to reuse the default implementation, then you can use object composition as shown above. At its core, it's reminiscent of the Decorator design pattern, but instead of receiving the inner object via its constructor, it creates the object itself. You can, however, also use a Decorator in order to achieve the same effect. This will make your code more flexible, but possibly also more error-prone, because you no longer have any guarantee what the 'base' is. This is where the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> becomes important, but that's a digression. </p> <p> If you're using the previous <a href="/2018/02/19/abstract-class-isomorphism">abstract class isomorphism</a> to refactor to Dependency Injection, you can refactor any use of <code>base</code> to object composition as shown here. </p> <p> This is a special case of <em>Replace Inheritance with Delegation</em> from <a href="http://amzn.to/YPdQDf">Refactoring</a>, which also describes the inverse refactoring <em>Replace Delegation with Inheritance</em>, thereby making these two refactorings an isomorphism. </p> <h3 id="6ce9849ca5744a50afeea686a40f0e71"> Summary <a href="#6ce9849ca5744a50afeea686a40f0e71" title="permalink">#</a> </h3> <p> This article focuses on a particular issue that you may run into if you try to avoid the use of abstract classes. Many programmers use inheritance in order to achieve reuse, but this is in no way necessary. Favour composition over inheritance. </p> <p> <strong>Next:</strong> <a href="/2019/07/15/tester-doer-isomorphisms">Tester-Doer isomorphisms</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Abstract class isomorphism https://blog.ploeh.dk/2018/02/19/abstract-class-isomorphism 2018-02-19T13:10:00+00:00 Mark Seemann <div id="post"> <p> <em>Abstract classes are isomorphic to Dependency Injection.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. </p> <p> The introduction to <a href="http://amzn.to/XBYukB">Design Patterns</a> states: <blockquote> Program to an interface, not an implementation. </blockquote> When I originally read that, I took it quite literally, so I wrote all my C# code using interfaces instead of abstract classes. There are several reasons why, in general, that turns out to be a good idea, but that's not the point of this article. It turns out that it doesn't really matter. </p> <p> If you have an abstract class, you can refactor to an object model composed from interfaces without loss of information. You can also refactor back to an abstract class. These two refactorings are each others' inverses, so together, they form an isomorphism. </p> <p> <img src="/content/binary/abstract-class-to-interfaces-isomorphism.png" alt="Abstract class on the left, concrete class with injected interfaces on the right; arrow between boxes."> </p> <p> When refactoring an abstract class, you extract all its pure virtual members to an interface, each of its virtual members to other interfaces, and inject them into a concrete class. The inverse refactoring involves going back to an abstract class. </p> <p> This is an important result, because upon closer inspection, the Gang of Four didn't have C# or Java interfaces in mind. The book pre-dates both Java and C#, and its examples are mostly in C++. Many of the examples involve abstract classes, but more than ten years of experience has taught me that I can always write a variant that uses C# interfaces. That is, I believe, not a coincidence. </p> <h3 id="d7a0264c0f134c30a15963ee8216e518"> Abstract class <a href="#d7a0264c0f134c30a15963ee8216e518" title="permalink">#</a> </h3> <p> An abstract class in C# has this general shape: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Class1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Data1</span>&nbsp;Data&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:#2b91af;">OutPureVirt1</span>&nbsp;PureVirt1(<span style="color:#2b91af;">InPureVirt1</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:#2b91af;">OutPureVirt2</span>&nbsp;PureVirt2(<span style="color:#2b91af;">InPureVirt2</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:#2b91af;">OutPureVirt3</span>&nbsp;PureVirt3(<span style="color:#2b91af;">InPureVirt3</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;pure&nbsp;virtual&nbsp;members...</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:#2b91af;">OutVirt1</span>&nbsp;Virt1(<span style="color:#2b91af;">InVirt1</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutVirt1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:#2b91af;">OutVirt2</span>&nbsp;Virt2(<span style="color:#2b91af;">InVirt2</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutVirt2&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:#2b91af;">OutVirt3</span>&nbsp;Virt3(<span style="color:#2b91af;">InVirt3</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutVirt3&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;virtual&nbsp;members...</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">OutConc1</span>&nbsp;Op1(<span style="color:#2b91af;">InConc1</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutConc1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">OutConc2</span>&nbsp;Op2(<span style="color:#2b91af;">InConc2</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutConc2&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">OutConc3</span>&nbsp;Op3(<span style="color:#2b91af;">InConc3</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutConc3&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;concrete&nbsp;members...</span> }</pre> </p> <p> Like in <a href="/2018/02/12/object-isomorphisms">the previous article</a>, I've deliberately kept the naming abstract (but added a more concrete example towards the end). The purpose of this article series is to look at the shape of code, instead of what it does, or why. From <a href="/2018/01/29/argument-list-isomorphisms">argument list isomorphisms</a> we know that we can represent any method as taking a single input value, and returning a single output value. </p> <p> An abstract class can have non-virtual members. In C#, this is the default, whereas in Java, you'd explicitly have to use the <code>final</code> keyword. In the above generalised representation, I've named these non-virtual members <code>Op1</code>, <code>Op2</code>, and so on. </p> <p> An abstract class can also have virtual members. In C#, you must explicitly use the <code>virtual</code> keyword in order to mark a method as overridable, whereas this is the default for Java. In the above representation, I've called these methods <code>Virt1</code>, <code>Virt2</code>, etcetera. </p> <p> Some virtual members are <em>pure virtual</em> members. These are members without an implementation. Any concrete (that is: non-abstract) class inheriting from an abstract class must provide an implementation for such members. In both C# and Java, you must declare such members using the <code>abstract</code> keyword. In the above representation, I've called these methods <code>PureVirt1</code>, <code>PureVirt2</code>, and so on. </p> <p> Finally, an abstract class can contain data, which you can represent as a single data object, here of the type <code>Data1</code>. </p> <p> The concrete and virtual members could, conceivably, call other members in the class - both concrete, virtual, and pure virtual. In fact, this is how many of the design patterns in the book work, for example <a href="https://en.wikipedia.org/wiki/Strategy_pattern">Strategy</a>, <a href="https://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a>, and <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder</a>. </p> <h3 id="b22dc0035a4c44948369c7cda63646ad"> From abstract class to Dependency Injection <a href="#b22dc0035a4c44948369c7cda63646ad" title="permalink">#</a> </h3> <p> Apart from its <code>Data</code>, an abstract class contains three types of members: <ul> <li>Those that <em>must</em> be implemented by derived classes: pure virtual members</li> <li>Those that <em>optionally</em> can be overriden by derived classes: virtual members</li> <li>Those that cannot be overridden by derived classes: concrete, sealed, or final, members</li> </ul> When refactoring to interfaces, you do the following: <ol> <li>Extract an interface from the pure virtual members.</li> <li>Extract an interface from each of the virtual members.</li> <li>Implement each of the 'virtual member interfaces' with the implementation from the virtual member.</li> <li>Add a constructor to the abstract class that takes all these new interfaces as arguments. Save the arguments as class fields.</li> <li>Change all code in the abstract class to talk to the injected interfaces instead of direct class members.</li> <li>Remove the virtual and pure virtual members from the class, or make them non-virtual. If you keep them around, their implementation should be one line of code, delegating to the corresponding interface.</li> <li>Change the class to a concrete (non-abstract) class.</li> </ol> If you apply this refactoring to the above class, you should arrive at something like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Class1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IInterface1</span>&nbsp;pureVirts; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IVirt1</span>&nbsp;virt1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IVirt2</span>&nbsp;virt2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IVirt3</span>&nbsp;virt3; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;virt&nbsp;fields...</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Data1</span>&nbsp;Data&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Class1( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInterface1</span>&nbsp;pureVirts,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IVirt1</span>&nbsp;virt1,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IVirt2</span>&nbsp;virt2,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IVirt3</span>&nbsp;virt3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">/*&nbsp;More&nbsp;virt&nbsp;arguments...&nbsp;*/</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.pureVirts&nbsp;=&nbsp;pureVirts; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.virt1&nbsp;=&nbsp;virt1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.virt2&nbsp;=&nbsp;virt2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.virt3&nbsp;=&nbsp;virt3; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;field&nbsp;assignments</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">OutConc1</span>&nbsp;Op1(<span style="color:#2b91af;">InConc1</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutConc1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">OutConc2</span>&nbsp;Op2(<span style="color:#2b91af;">InConc2</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutConc2&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">OutConc3</span>&nbsp;Op3(<span style="color:#2b91af;">InConc3</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;OutConc3&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;concrete&nbsp;members...</span> }</pre> </p> <p> While not strictly necessary, I've marked the class <code>sealed</code> (<code>final</code> in Java) in order to drive home the point that this is no longer an abstract class. </p> <p> This is an example of the Constructor Injection design pattern. (This is not a Gang of Four pattern; you can find a description in <a href="http://amzn.to/12p90MG">my book about Dependency Injection</a>.) </p> <p> Since it's optional to override virtual members, any class originally inheriting from an abstract class can choose to override only one, or two, of the virtual members, while leaving other virtual members with their default implementations. In order to support such piecemeal redefinition, you can extract each virtual member to a separate interface, like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IVirt1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">OutVirt1</span>&nbsp;Virt1(<span style="color:#2b91af;">InVirt1</span>&nbsp;arg); }</pre> </p> <p> Notice that each of these 'virtual interfaces' are injected into <code>Class1</code> as a separate argument. This enables you to pass your own implementation of exactly those you wish to change, while you can pass in the default implementation for the rest. The default implementations are the original code from the virtual members, but moved to a class that implements the interfaces: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DefaultVirt</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IVirt1</span>,&nbsp;<span style="color:#2b91af;">IVirt2</span>,&nbsp;<span style="color:#2b91af;">IVirt3</span> </pre> </p> <p> When inheriting from the original abstract class, however, you must implement all the pure virtual members, so you can extract a single interface from all the pure virtual members: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IInterface1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">OutPureVirt1</span>&nbsp;PureVirt1(<span style="color:#2b91af;">InPureVirt1</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">OutPureVirt2</span>&nbsp;PureVirt2(<span style="color:#2b91af;">InPureVirt2</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">OutPureVirt3</span>&nbsp;PureVirt3(<span style="color:#2b91af;">InPureVirt3</span>&nbsp;arg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;pure&nbsp;virtual&nbsp;members...</span> }</pre> </p> <p> This forces anyone who wants to use the refactored (sealed) <code>Class1</code> to provide an implementation of all of those members. There's an edge case where you inherit from the original <code>Class1</code> in order to create a new <em>abstract</em> class, and implement only one or two of the pure virtual members. If you want to support that edge case, you can define an interface for each pure virtual member, instead of one big interface, similar to <code>IVirt1</code>, <code>IVirt2</code>, and so on. </p> <h3 id="7647f5cced134b24b4df7ec37d61fbf7"> From Dependency Injection to abstract class <a href="#7647f5cced134b24b4df7ec37d61fbf7" title="permalink">#</a> </h3> <p> I hope it's clear how to perform the inverse refactoring. Assume that the above sealed <code>Class1</code> is the starting point: <ol> <li>Mark <code>Class1</code> as <code>abstract</code>.</li> <li>For each of the members of <code>IInterface1</code>, add a pure virtual member.</li> <li>For each of the members of <code>IVirt1</code>, <code>IVirt2</code>, and so on, add a virtual member.</li> <li>Move the code from the default implementation of the 'virtual interfaces' to the new virtual members.</li> <li>Delete the dependency fields and remove the corresponding arguments from the constructor.</li> <li>Clean up orphaned interfaces and implementations.</li> </ol> This refactoring assumes a class using Dependency Injection like the one shown in this article, above. The example code is the same as the above example code, although the order is reversed: you start with the Dependency Injection class and end with the abstract class. </p> <h3 id="6dae326271714421b35d3acb4c08b99d"> Example: Gang of Four maze Builder as an abstract class <a href="#6dae326271714421b35d3acb4c08b99d" title="permalink">#</a> </h3> <p> As an example, consider the original Gang of Four example of the <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder</a> pattern. The example in the book is based on an abstract class called <code>MazeBuilder</code>. Translated to C#, it looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MazeBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BuildMaze()&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BuildRoom(<span style="color:blue;">int</span>&nbsp;room)&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BuildDoor(<span style="color:blue;">int</span>&nbsp;roomFrom,&nbsp;<span style="color:blue;">int</span>&nbsp;roomTo)&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:#2b91af;">Maze</span>&nbsp;GetMaze(); }</pre> </p> <p> In the book, all four methods are virtual, because: <blockquote> "They're not declared pure virtual to let derived classes override only those methods in which they're interested." </blockquote> When it comes to the <code>GetMaze</code> method, this means that the method in the book returns a null reference by default. Since this seems like poor API design, and also because the example becomes more illustrative if the class has both abstract and virtual members, I changed it to be abstract (i.e. pure virtual). </p> <p> In general, there are various other issues with this design, the most glaring of which is the implied <a href="/2011/05/24/DesignSmellTemporalCoupling">sequence coupling</a> between members: you're expected to call <code>BuildMaze</code> before any of the other methods. A better design would be to remove that explicit step entirely, or else turn it into a factory that you have to call in order to be able to call the other methods. That's not the topic of the present article, so I'll leave the API like this. </p> <p> The book also shows a simple usage example of the abstract <code>MazeBuilder</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MazeGame</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maze</span>&nbsp;CreateMaze(<span style="color:#2b91af;">MazeBuilder</span>&nbsp;builder) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder.BuildMaze(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder.BuildRoom(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder.BuildRoom(2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder.BuildDoor(1,&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;builder.GetMaze(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You use it with e.g. a <code>StandardMazeBuilder</code> like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;game&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MazeGame</span>(); <span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StandardMazeBuilder</span>(); <span style="color:blue;">var</span>&nbsp;maze&nbsp;=&nbsp;game.CreateMaze(builder);</pre> </p> <p> You could also, again following the book's example as closely as possible, use it with a <code>CountingMazeBuilder</code>, like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;game&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MazeGame</span>(); <span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CountingMazeBuilder</span>(); game.CreateMaze(builder); <span style="color:blue;">var</span>&nbsp;msg&nbsp;=&nbsp;<span style="color:#a31515;">$&quot;The&nbsp;maze&nbsp;has&nbsp;</span>{builder.RoomCount}<span style="color:#a31515;">&nbsp;rooms&nbsp;and&nbsp;</span>{builder.DoorCount}<span style="color:#a31515;">&nbsp;doors.&quot;</span>;</pre> </p> <p> This would produce <code>"The maze has 2 rooms and 1 doors."</code>. </p> <p> Both <code>StandardMazeBuilder</code> and <code>CountingMazeBuilder</code> are concrete classes that derive from the abstract <code>MazeBuilder</code> class. </p> <h3 id="dffe775a502b4e0eb55ec6d9705c3dcb"> Maze Builder refactored to interfaces <a href="#dffe775a502b4e0eb55ec6d9705c3dcb" title="permalink">#</a> </h3> <p> If you follow the refactoring outline in this article, you can refactor the above <code>MazeBuilder</code> class to a set of interfaces. The first should be an interface extracted from all the pure virtual members of the class. In this example, there's only one such member, so the interface becomes this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMazeBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maze</span>&nbsp;GetMaze(); }</pre> </p> <p> The three virtual members each get their own interface, so that you can pick and choose which of them you want to override, and which of them you prefer to keep with their default implementation (which, in this particular case, is to do nothing). </p> <p> The first one was difficult to name: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMazeInitializer</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;BuildMaze(); }</pre> </p> <p> An interface with a single method called <code>BuildMaze</code> would naturally have a name like <code>IMazeBuilder</code>, but unfortunately, I just used that name for the previous interface. The reason I named the above interface <code>IMazeBuilder</code> is because this is an interface extracted from the <code>MazeBuilder</code> abstract class, and I consider the pure virtual API to be the core API of the abstraction, so I think it makes most sense to keep the name for that interface. Thus, I had to come up with a <a href="/2011/05/24/DesignSmellTemporalCoupling">smelly</a> name like <code>IMazeInitializer</code>. </p> <p> Fortunately, the two remaining interfaces are a little better: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRoomBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;BuildRoom(<span style="color:blue;">int</span>&nbsp;room); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IDoorBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;BuildDoor(<span style="color:blue;">int</span>&nbsp;roomFrom,&nbsp;<span style="color:blue;">int</span>&nbsp;roomTo); }</pre> </p> <p> The three virtual members all had default implementations, so you need to keep those around. You can do that by moving the methods' code to a new class that implements the new interfaces: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DefaultMazeBuilder</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMazeInitializer</span>,&nbsp;<span style="color:#2b91af;">IRoomBuilder</span>,&nbsp;<span style="color:#2b91af;">IDoorBuilder</span> </pre> </p> <p> In this example, there's no reason to show the implementation of the class, because, as you may recall, all three methods are no-ops. </p> <p> Instead of inheriting from <code>MazeBuilder</code>, implementers now implement the appropriate interfaces: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">StandardMazeBuilder</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMazeBuilder</span>,&nbsp;<span style="color:#2b91af;">IMazeInitializer</span>,&nbsp;<span style="color:#2b91af;">IRoomBuilder</span>,&nbsp;<span style="color:#2b91af;">IDoorBuilder</span> </pre> </p> <p> This version of <code>StandardMazeBuilder</code> implements all four interfaces, since, before, it overrode all four methods. <code>CountingMazeBuilder</code>, on the other hand, never overrode <code>BuildMaze</code>, so it doesn't have to implement <code>IMazeInitializer</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CountingMazeBuilder</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IRoomBuilder</span>,&nbsp;<span style="color:#2b91af;">IDoorBuilder</span>,&nbsp;<span style="color:#2b91af;">IMazeBuilder</span> </pre> </p> <p> All of these changes leaves the original <code>MazeBuilder</code> class defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MazeBuilder</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMazeBuilder</span>,&nbsp;<span style="color:#2b91af;">IMazeInitializer</span>,&nbsp;<span style="color:#2b91af;">IRoomBuilder</span>,&nbsp;<span style="color:#2b91af;">IDoorBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IMazeBuilder</span>&nbsp;mazeBuilder; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IMazeInitializer</span>&nbsp;mazeInitializer; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IRoomBuilder</span>&nbsp;roomBuilder; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IDoorBuilder</span>&nbsp;doorBuilder; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MazeBuilder( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMazeBuilder</span>&nbsp;mazeBuilder, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMazeInitializer</span>&nbsp;mazeInitializer, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRoomBuilder</span>&nbsp;roomBuilder, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDoorBuilder</span>&nbsp;doorBuilder) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.mazeBuilder&nbsp;=&nbsp;mazeBuilder; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.mazeInitializer&nbsp;=&nbsp;mazeInitializer; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.roomBuilder&nbsp;=&nbsp;roomBuilder; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.doorBuilder&nbsp;=&nbsp;doorBuilder; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BuildMaze() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.mazeInitializer.BuildMaze(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BuildRoom(<span style="color:blue;">int</span>&nbsp;room) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.roomBuilder.BuildRoom(room); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BuildDoor(<span style="color:blue;">int</span>&nbsp;roomFrom,&nbsp;<span style="color:blue;">int</span>&nbsp;roomTo) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.doorBuilder.BuildDoor(roomFrom,&nbsp;roomTo); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maze</span>&nbsp;GetMaze() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.mazeBuilder.GetMaze(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> At this point, you may decide to keep the old <code>MazeBuilder</code> class around, because you may have other code that relies on it. Notice, however, that it's now a concrete class that has dependencies injected into it via its constructor. All four members only delegate to the relevant dependencies in order to do actual work. </p> <p> <code>MazeGame</code> looks like before, but calling <code>CreateMaze</code> looks more complicated: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;game&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MazeGame</span>(); <span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StandardMazeBuilder</span>(); <span style="color:blue;">var</span>&nbsp;maze&nbsp;=&nbsp;game.CreateMaze(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MazeBuilder</span>(builder,&nbsp;builder,&nbsp;builder,&nbsp;builder));</pre> </p> <p> Notice that while you're passing four dependencies to the <code>MazeBuilder</code> constructor, you can reuse the same <code>StandardMazeBuilder</code> object for all four roles. </p> <p> If you want to count the rooms and doors, however, <code>CountingMazeBuilder</code> doesn't implement <code>IMazeInitializer</code>, so for that role, you'll need to use the default implementation: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;game&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MazeGame</span>(); <span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CountingMazeBuilder</span>(); game.CreateMaze(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MazeBuilder</span>(builder,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultMazeBuilder</span>(),&nbsp;builder,&nbsp;builder)); <span style="color:blue;">var</span>&nbsp;msg&nbsp;=&nbsp;<span style="color:#a31515;">$&quot;The&nbsp;maze&nbsp;has&nbsp;</span>{builder.RoomCount}<span style="color:#a31515;">&nbsp;rooms&nbsp;and&nbsp;</span>{builder.DoorCount}<span style="color:#a31515;">&nbsp;doors.&quot;</span>;</pre> </p> <p> If, at this point, you're beginning to wonder what value <code>MazeBuilder</code> adds, then I think that's a legitimate concern. What often happens, then, is that you simply remove that extra layer. </p> <h3 id="dbb69051eeb1482c9a2bee079df977ab"> Mazes without MazeBuilder <a href="#dbb69051eeb1482c9a2bee079df977ab" title="permalink">#</a> </h3> <p> When you delete the <code>MazeBuilder</code> class, you'll have to adjust <code>MazeGame</code> accordingly: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MazeGame</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maze</span>&nbsp;CreateMaze( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMazeInitializer</span>&nbsp;initializer,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRoomBuilder</span>&nbsp;roomBuilder,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDoorBuilder</span>&nbsp;doorBuilder, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMazeBuilder</span>&nbsp;mazeBuilder) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initializer.BuildMaze(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roomBuilder.BuildRoom(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roomBuilder.BuildRoom(2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doorBuilder.BuildDoor(1,&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;mazeBuilder.GetMaze(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>CreateMaze</code> method now simply takes the four interfaces on which it relies as individual arguments. This simplifies the client code as well: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;game&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MazeGame</span>(); <span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StandardMazeBuilder</span>(); <span style="color:blue;">var</span>&nbsp;maze&nbsp;=&nbsp;game.CreateMaze(builder,&nbsp;builder,&nbsp;builder,&nbsp;builder);</pre> </p> <p> You can still reuse a single <code>StandardMazeBuilder</code> in all roles, but again, if you only want to count the rooms and doors, you'll have to rely on <code>DefaultMazeBuilder</code> for the behaviour that <code>CountingMazeBuilder</code> doesn't define: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;game&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MazeGame</span>(); <span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CountingMazeBuilder</span>(); game.CreateMaze(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultMazeBuilder</span>(),&nbsp;builder,&nbsp;builder,&nbsp;builder); <span style="color:blue;">var</span>&nbsp;msg&nbsp;=&nbsp;<span style="color:#a31515;">$&quot;The&nbsp;maze&nbsp;has&nbsp;</span>{builder.RoomCount}<span style="color:#a31515;">&nbsp;rooms&nbsp;and&nbsp;</span>{builder.DoorCount}<span style="color:#a31515;">&nbsp;doors.&quot;</span>;</pre> </p> <p> The order in which dependencies are passed to <code>CreateMaze</code> is different than the order they were passed to the now-deleted <code>MazeBuilder</code> constructor, so you'll have to pass a <code>new DefaultMazeBuilder()</code> as the first argument in order to fill the role of <code>IMazeInitializer</code>. Another way to address this issue is to supply various overloads of the <code>CreateMaze</code> method that uses <code>DefaultMazeBuilder</code> for the behaviour that you don't want to override. </p> <h3 id="011d61051d3c4340bbd3d9582b6dc482"> Summary <a href="#011d61051d3c4340bbd3d9582b6dc482" title="permalink">#</a> </h3> <p> Many of the original design patterns in <em>Design Patterns</em> are described with examples in C++, and many of these examples use abstract classes as the programming interfaces that the Gang of Four really had in mind when they wrote that we should be programming to interfaces instead of implementations. </p> <p> The most important result of this article is that you can reinterpret the original design patterns with C# or Java interfaces and Dependency Injection, instead of using abstract classes. I've done this in C# for more than ten years, and in my experience, you never need abstract classes in a greenfield code base. There's always an equivalent representation that involves composition of interfaces. </p> <p> <strong>Next:</strong> <a href="/2018/02/26/inheritance-composition-isomorphism">Inheritance-composition isomorphism</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="b8cb5baf6ccd4cc98412c229d3dadb06"> <div class="comment-author"> <a href="https://github.com/MaxKot">Max Kiselev</a> <a href="#b8cb5baf6ccd4cc98412c229d3dadb06">#</a></div> <div class="comment-content"> <p> While the idea is vey interesting I think it is not exactly an isomorphism. </p> <p> The first reason I think it is not an isomorphism is language-specific since Java and C# allow implementing multiple interfaces but not multiple abstract classes. It can make a reverse transformation from interfaces back to an abstract class non-trivial. </p> <p> The second reason is that abstract class guarantees that whatever class implements the pure virtual members and overrides virtual members share the same state between all its methods and also with the abstract base class. With the maze Builder example there must be a state shared between GetMaze, BuildMaze, BuildRoom and BuildDoor methods but the dependency injection does not seem to reflect it. </p> <p> Perhaps there should be some kind of Data parameter passed to all injected interfaces. </p> </div> <div class="comment-date">2018-03-05 19:03 UTC</div> </div> <div class="comment" id="13cc2c9a28114b52add8942d3ac3c94d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#13cc2c9a28114b52add8942d3ac3c94d">#</a></div> <div class="comment-content"> <p> Max, thank you for writing, and particularly for applying critique to this post. One of my main motivations for writing <a href="/2017/10/04/from-design-patterns-to-category-theory">the entire article series</a> is that I need to subject my thoughts to peer review. I've been thinking about these things for years, but in order to formalise them, I need to understand whether I'm completely wrong (I hope not), of, if I'm not, what are the limits of my findings. </p> <p> I think you've just pointed at one such limit, and for that I'm grateful. The rest of this answer, then, is not an attempt to refute your comment, but rather an effort to identify some <em>constraints</em> within which what I wrote may still hold. </p> <p> Your second objection doesn't worry me that much, because you also suggest a way around it. I admit that I faked the Maze Builder code somewhat, so that the state isn't explicit. I feel that fudging the code example is acceptable, as the Gang of Four code example is also clearly incomplete. In any case, you're right that an abstract class could easily contain some shared state. When refactoring to interfaces, the orchestrating class could instead pass around that state as an argument to all methods, as you suggest. Would it be reasonable to conclude that this, then, doesn't prevent the translations from being isomorphic? </p> <p> There's still your first objection, which I think is significant. That's the reason I decided to cover your second objection first, because I think it'll require more work to address the first objection. </p> <p> First, I think we need to delimit the problem, since your comment slightly misrepresents my article. The claim in the article is that you can refactor an abstract class to a concrete class with injected dependencies. Furthermore, the article claims that this translation is isomorphic; i.e. that you can refactor a concrete class with injected dependencies to an abstract class. </p> <p> If I read your comment right, you're saying that a class can implement more than one interface, like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MyClass</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IFoo</span>,&nbsp;<span style="color:#2b91af;">IBar</span></pre> </p> <p> I agree that you can't use the transformations described in this article to refactor <code>MyClass</code> to an abstract class, because that's not the transformation that the article describes. </p> <p> That doesn't change that your comment is uncomfortably close to an inconvenient limitation. You're right that there seems to be a limitation when it comes to C#'s and Java's lack of multiple inheritance. As your comment implies, if a translation is isomorphic, one has to be able to start at either end, and round-trip to the other end and back. Thus, one has to be able to start with a concrete class with injected dependencies, and refactor to an abstract class; for example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MyClass</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MyClass(<span style="color:#2b91af;">IFoo</span>&nbsp;foo,&nbsp;<span style="color:#2b91af;">IBar</span>&nbsp;bar) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As far as I can tell, that's exactly the shape of the <code>sealed</code> version of <code>Class1</code>, above, so I'm not convinced that that's a problem, but something like the following does look like a problem to me: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MyClass</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MyClass(<span style="color:#2b91af;">IFoo</span>&nbsp;foo1,&nbsp;<span style="color:#2b91af;">IFoo</span>&nbsp;foo2) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> It's not clear to me how one can refactor something like that to an abstract class, and still retain the distinction between <code>foo1</code> and <code>foo2</code>. My claim is not that this is impossible, but only that it's not immediately clear to me how to do that. Thus, we may have to put a constraint on the original claim, and instead say something like this: <blockquote> An abstract class is isomorphic to a concrete class with injected dependencies, given that all the injected dependencies are of different types. </blockquote> We can attempt to illustrate the claim like this: </p> <p> <img src="/content/binary/abstract-class-to-constrained-dependency-injection-isomorphism.png" alt="The set of abstract classes juxtaposed with the set of dependency injection, the latter with a subset for which arrows go both ways between the subset and the set of abstract classes."> </p> <p> This is still an isomorphism, I think, although I invite further criticism of that claim. <a href="http://amzn.to/13tGJ0f">Conceptual Mathematics</a> defines an isomorphism in terms of categories <em>A</em> and <em>B</em>, and as far as I can tell, <em>A</em> and <em>B</em> can be as specific as we want them to be. Thus, we can say that <em>A</em> is the set of abstract classes, and <em>B</em> is the subset of concrete classes with injected dependencies, for which no dependency share the same type. </p> <p> If we have to constrain the isomorphism in this way, though, is it still interesting? Why should we care? </p> <p> To be perfectly honest, what motivated me to write this particular article is that I wanted to describe the translation from an abstract class to dependency injection. The inverse interests me less, but I thought that if the inverse translation exists, I could fit this article in with the other articles in this article series about software design isomorphisms. </p> <p> The reason I care about the translation from abstract class to dependency injection is that I often see code where the programmers misuse inheritance. My experience with C# is that one can completely avoid inheritance. The way to do that is to use dependency injection instead. This article shows how to do that. </p> <p> The result that one can write real, complex code bases in C# without inheritance is important to me, because one of my current goals is to teach people the advantages of functional programming, and one barrier I run into is that people who come from object-oriented programming run into problems when they no longer can use inheritance. Thus, this article shows an object-oriented alternative to inheritance, so that people can get used to the idea of designing without inheritance, even before they start looking at functional programming. </p> <p> Another motivation for this article is that it's part of <a href="/2017/10/04/from-design-patterns-to-category-theory">a much larger article series</a> about design patterns, and how they relate to fundamental abstractions. In <em>Design Patterns</em>, all the (C++) patterns are described in terms of inheritance, so I wrote this article series on isomorphisms in order to be able to represent various design patterns in other forms than they appear in the book. </p> </div> <div class="comment-date">2018-03-08 9:57 UTC</div> </div> <div class="comment" id="93410202470d4406a698fa4ffb6d3c27"> <div class="comment-author"> Ciprian Vilcan <a href="#93410202470d4406a698fa4ffb6d3c27">#</a></div> <div class="comment-content"> <p> This idea is a very interesting and useful one, but as I found out in one of my toy projects and as Max stated above, it is unfortunately not language agnostic.<br> As far as C# is concerned, you can have operator overloading in an abstract class, which is a bit of logic that I see no way of extracting to an interface and thus remove the need for inheritance. Example below.<br> (You could define Add, Subtract, Multiply and Divide methods, but to me they seem like reinventing the square wheel. They seem much less convenient than +-*/) </p> <p> I tried creating some nice Temperature value objects, similar to <a href="/2017/10/16/money-monoid/">the money monoid you presented</a> and I came up with 5 classes:<br> Temperature, Kelvin, Celsius, Fahrenheit and TemperatureExpression.<br> Temperature is the abstract class and its +Temperature and -Temperature operators are overloaded so that they return a TemperatureExpression, which can then be evaluated to a Maybe&lt;TTemperature&gt; where TTemperature : Temperature. <br> A TemperatureExpression is nothing more than a lazily evaluated mathematical expression (for example: 12K + 24C - 32F).<br> Also, it's a Maybe because 0K - 1C won't be a valid temperature, so we have to also take this case into consideration. <br> </p> <p> For further convenience, the Kelvin class has its own overloaded + operator, because no matter what you do, when adding together two Kelvin values you'll always end up with something that is greater than 0. </p> <p> These being said, if you want to leverage the C# operators there are some limitations regarding this transformation that keep you from having this abstract class -> DI isomorphism. <br> That is, unless you're willing to add the methods I've spoken of in the first paragraph. <br> But, in an ideal programming language, IMHO, arithmetic and boolean operators should be part of some public interfaces like, ISubtractable, IDivideable and thus allow for a smooth transition between abstract classes and interface DI. </p> </div> <div class="comment-date">2018-03-08 10:16 UTC</div> </div> <div class="comment" id="7aac063a684047ae9561c6666a42f908"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7aac063a684047ae9561c6666a42f908">#</a></div> <div class="comment-content"> <p> Ciprian, thank you for writing. I agree that this isn't language agnostic. It's possible that we need to add further constraints to the conjecture, but I still anticipate that, with appropriate constraints, it holds for statically typed 'mainstream' object-oriented languages (i.e. C# and Java). It may also hold for other languages, but it requires detailed knowledge of a language to claim that it does. For instance, it's been too long since I wrote C++ code, and I can't remember how its object-oriented language features work. Likewise, it'd be interesting to investigate if the conjecture holds when applied to JavaScript, Ruby, Python, etc., but I've been careful not to claim that, as I know too little about those languages. </p> <p> Regarding your temperature example, I <em>think</em> that perhaps I understand what the issue is, but I'm not sure I've guessed right. In the interest of being able to have an unambiguous discussion about a code example, could you please post the pertinent code that illustrates your point? </p> </div> <div class="comment-date">2018-03-09 8:07 UTC</div> </div> <div class="comment" id="52d98574756f471482303e34dc02df33"> <div class="comment-author">Ciprian Vilcan <a href="#52d98574756f471482303e34dc02df33">#</a></div> <div class="comment-content"> <p> I've added the code to <a href="https://github.com/ciprian-vilcan/CQRS.CoffeeMaker">github</a>. I think it's better than copy-pasting here as you can gain some extra context. </p> <p> It's a playground project where I put to practice various ideas I find on the web (so if you see something you consider should be done otherwise, I don't mind you saying it). I've added a few comments to make it a bit easier to understand and also removed anything that doesn't pertain to the issue with abstract class -> DI.<br> </p> <p> In short, the bit of logic that I can't see a way of extracting to some sort of injectable dependency are the +- operators on the Temperature class due to the fact that they are static methods, thus cannot be part of an interface (at least in C#, I don't know about programming languages). </p> </div> <div class="comment-date">2018-03-09 11:14 UTC</div> </div> <div class="comment" id="b9a9d7e657804832b4adcc6e71aa0d7f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b9a9d7e657804832b4adcc6e71aa0d7f">#</a></div> <div class="comment-content"> <p> Ciprian, thank you for elaborating. I'd forgotten about C#'s ability to overload arithmetic operators, which is, I believe, what you're referring to. To be clear, I do believe that it's a fair enough critique, so that we'll have to once again restrict this article's conjecture to something like: <blockquote> There exists a subset <em>A</em> of all abstract classes, and a subset of all concrete classes with injected dependencies <em>I</em>, such that an isomorphism <em>A &lt;-&gt; I</em> exists. </blockquote> In diagram form, it would look like this: </p> <p> <img src="/content/binary/subset-of-abstract-classes-to-subset-of-dependency-injection-isomorphism.png" alt=""> </p> <p> By its shape, the diagram suggests that the size of abstract classes <em>not</em> isomorphic with Dependency Injection is substantial, but that's not really my intent; I just had to leave room for the text. </p> <p> My experience suggests to me that most abstract classes can be refactored as I've described in this article, but clearly, as you've shown, there are exceptions. C#'s support for operator overloading is one such exception, but there may be others of which I'm currently ignorant. </p> <p> That said, I would like to make the case that arithmetic operators aren't object-oriented in the first place. Had the language been purely object-oriented from the outset, addition would more appropriately have had a syntax like <code>40.Add(2)</code>. This would imply that the language would have been based on the concept of objects as data with behaviour, and the behaviour would exclusively have been defined by class members. </p> <p> Even at the beginning, though, C# was a hybrid language. It had (and still has) a subset of language features focused on more low-level programming. It has value types in addition to reference types, it has arithmetic operators, it has special support for bitwise Boolean operators, and it even has pointers. </p> <p> There are practical reasons that all of those features exist, but I would claim that none of those features have anything to do with object-orientation. </p> <p> Specifically when it comes to arithmetic operators, the operators are all special cases baked into the language. The selection of operators is sensible, but when you get to the bottom of it, arbitrary. For instance, there's a modulo operator, but no power operator. Why? </p> <p> As an aside, languages do exist where arithmetic is an abstraction instead of a language feature. The one I'm most familiar with is <a href="https://www.haskell.org">Haskell</a>, where arithmetic is defined in terms of type classes. It's worth noting that the operators <code>+</code>, <code>*</code>, and <code>-</code> are defined in an abstraction called <a href="http://hackage.haskell.org/package/base/docs/Prelude.html#t:Num">Num</a>, whereas the 'fourth' arithmetic operator <code>/</code> is defined in a more specialised abstraction called <a href="http://hackage.haskell.org/package/base/docs/Prelude.html#t:Fractional">Fractional</a>. </p> <p> Not that Haskell's model of arithmetic is perfect, but there's a rationale behind this distinction. Division is special, because it can translate two integers (e.g. <code>2</code> and <code>3</code>) into a rational number (e.g. <code>2/3</code>), while both addition and multiplication are <a href="/2017/10/06/monoids">monoids</a>. This is where Haskell starts to fall apart itself, though, because subtraction can also translate two numbers out of the set in which they originally belonged. For example, given two <em>natural</em> numbers <code>2</code> and <code>3</code>, <code>2 - 3</code> is no longer a natural number, since it's negative. </p> <p> But all of that is an aside. Even in C#, one has to deal with low-level exceptional cases such as integer overflow, so even addition isn't truly monoidal, unless you use <a href="https://msdn.microsoft.com/en-us/library/system.numerics.biginteger">BigInteger</a>. </p> </div> <div class="comment-date">2018-03-11 9:35 UTC</div> </div> <div class="comment" id="5b00a6edefbf4a82925083eeac5d7768"> <div class="comment-author"><a href="https://github.com/MaxKot">Max Kiselev</a> <a href="#5b00a6edefbf4a82925083eeac5d7768">#</a></div> <div class="comment-content"> <p> Mark, thank you for the detailed response. I didn't mean to refute the usefulness of the refactoring you described. I now see how I tried to apply the refactoring of DI to abstact class where it was not claimed to be possible. </p> <p> I've been thinking about the example with the injection of distinct instances of the same type. I think we can use simple wrapper types for this kind of problem: </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Foo1</span>&nbsp;:&nbsp;IFoo { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;IFoo&nbsp;impl_; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Foo1&nbsp;(IFoo&nbsp;impl) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;impl_&nbsp;=&nbsp;impl; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Out1&nbsp;Op1&nbsp;(In1&nbsp;in1) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;impl_.Op1&nbsp;(in1); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Foo1 implements IFoo to keep the ability to e.g. create a list of foo1 and foo2. </p> <p> Given such wrappers are created for all dependencies of type IFoo, MyClass can be rewritten like this: </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MyClass</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MyClass&nbsp;(Foo1&nbsp;foo1,&nbsp;Foo2&nbsp;foo2) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Now MyClass can be refactored into an abstract class without losing the distinction between foo1 and foo2. </p> <p> I also belive that, given the wrappers are used only in the context of MyClass, the use of wrappers and diffent fields or parameters is an isomorphism. While its usefulness as a stand-alone refactoing is limited, it may come handy for reasoning similar to the one you did in <a href="/2018/03/12/composite-as-a-monoid/">Composite as a monoid</a>. </p> <p> Thin wrappers can be used to create an <em>almost</em> convenient way to define operators on interfaces. Unfortunately C# does not allow user-defined conversions to or from an interface so one have to explicitly wrap all instances of the interface in an expression that do not have wrapper on one side of an expression. While it can be acceptable in some cases (like <span>Wrap(a) + b + c + d ...</span>), it can make more complex expressions very cumbersome (like <span>(Wrap(a) + b) + (Wrap(c) + d) ...</span> hence it does not solve the problem that Ciprian described. </p> </div> <div class="comment-date">2018-03-22 21:33 UTC</div> </div> <div class="comment" id="2e3ab0c8da334d5cb066eab2f492c50e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2e3ab0c8da334d5cb066eab2f492c50e">#</a></div> <div class="comment-content"> <p> Max, thank you for writing back. That's an ingenious resolution to some of the problems you originally pointed out. Thank you! </p> <p> As far as I can tell, this seems to strengthen the original argument, although there's still some corner cases, like the one pointed out by Ciprian. We can use Decorators as <a href="/2012/08/31/ConcreteDependencies">concrete dependencies</a> as you point out, as an argument that even two (or <em>n</em>) identical polymorphic dependencies can be treated as though they were distinct dependencies. </p> <p> What if we have an arbitrary number of dependencies? One example would be of a Composite, but it doesn't have to be. Consider the <code>ShippingCostCalculatorFactory</code> class from <a href="/2013/01/11/PartialTypeNameRoleHint">this example</a>. It depends on a list of <code>IBasketCalculator</code> candidates. Could such a class, too, be refactored to an abstract class? </p> <p> I suppose it could, since the dependency then really isn't an arbitrary number of <code>IBasketCalculator</code>, but rather the dependency is on <em>a collection</em>. Would it be enough to refactor to an abstract class with a single <a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory Method</a> that returns the candidates? </p> </div> <div class="comment-date">2018-03-27 5:50 UTC</div> </div> <div class="comment" id="ae9139f637184901bd681f5a28ddecb4"> <div class="comment-author"><a href="https://github.com/MaxKot">Max Kiselev</a> <a href="#ae9139f637184901bd681f5a28ddecb4">#</a></div> <div class="comment-content"> <p> Mark, you're welcome! Admittedly, my solution is heavily inspired by the strong-typing as promoted by "if a Haskell program compiles, it probably works" and the <a href="https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/">original purpose of Hungarian notation</a>. </p> <p> As for an abrbitrary number of dependencies, you have already pointed out that the dependency is the collection itself, not its elements. So I think ShippingCostCalculatorFactory can be refactored to an abstract class with an abstract factory method to provide a collection of IBasketCalculator. </p> <p> While abstract class would be more complex and less elegant than DI implementation, I find the reversibility of refactorings very important. Reversability means that the changes to the code are not changing the behavior of the compiled program. It allows to refactor even obscure and undertested legacy code without fear of breaking it. I find the two-way nature of changes to the code the most interesting about your concept of software isomorphisms. </p> <p> I think the reason abstract classes are usually difficult to reason about and often considered to be a bad choice is that an abstract class and it's inheritors create an implicit composition and the relationships of the parts of this composition can be very different. A base class can serve as a collection of helper methods, or derived classes can serve as dependencies or specify dependencies like in the ShippingCostCalculatorFactory example, or inheitors can serve as a configuration to the base class like custom configuration element classes in .NET derived from ConfigurationElement. Abstract base class can be even used to implement disciminated unions (and in fact F# compiler does). </p> <p> Perhaps different kinds of hierarchies can be enumerated with some formal ways to recognize a specific kind of hierarchy and refactor it into an explicit compistion? </p> <p> P.S. One way to implement discriminated unions with C# abstract base classes and guarantee exhaustive matching: </p> <pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IOptionVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Some(<span style="color:#2b91af;">T</span>&nbsp;value); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;None(); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Option</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SomeImpl</span>&nbsp;:&nbsp;<span style="color:#2b91af;">Option</span>&lt;<span style="color:#2b91af;">T</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;_value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SomeImpl(<span style="color:#2b91af;">T</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AcceptVisitor(<span style="color:#2b91af;">IOptionVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;visitor.Some(_value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NoneImpl</span>&nbsp;:&nbsp;<span style="color:#2b91af;">Option</span>&lt;<span style="color:#2b91af;">T</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AcceptVisitor(<span style="color:#2b91af;">IOptionVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;visitor.None(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Option&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AcceptVisitor(<span style="color:#2b91af;">IOptionVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Option</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Some(<span style="color:#2b91af;">T</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SomeImpl</span>(value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Option</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;None&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NoneImpl</span>(); } </pre> <p> While I don't see how this kind of abstract base class can be refactored to DI, I can not call this a constraint on the abstact class isomorphism because semantically it is not an asbtract class in first place. </p> </div> <div class="comment-date">2018-04-03 18:20 UTC</div> </div> <div class="comment" id="bbc66b98b21d438d98733bf264edba18"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bbc66b98b21d438d98733bf264edba18">#</a></div> <div class="comment-content"> <p> Max, once again thank you for writing. I've never seen that article by Joel Spolsky before, but I particularly liked your enumeration of the various different roles an abstract class can have. </p> <p> It's seems that we're generally in agreement about the constraints of the described refactoring. </p> <p> When it comes to your <em>option</em> implementation, I think you could fairly easy split up <code>Option&lt;T&gt;</code> into an interface that defines the <code>AcceptVisitor</code> method, and two classes that implements that interface. This is, however, closely related to a series of articles I'll publish in the future. </p> </div> <div class="comment-date">2018-04-04 19:20 UTC</div> </div> <div class="comment" id="72285cb33eb5424cb0c82f5239d02bfe"> <div class="comment-author"><a href="https://github.com/MaxKot">Max Kiselev</a> <a href="#72285cb33eb5424cb0c82f5239d02bfe">#</a></div> <div class="comment-content"> <p> Mark, thank you for pointing out the alternative option implementation. </p> <p> The key trick in my Option implementation is the use of private constructor in an abstract class with nested sealed implementation classes. Nested classes can access the private contructor while any class "outside" Option would be unable to call the base constructor. Now I think that enforcing that there are no implementation of Option except for SomeImpl and NoneImpl is redundant as long as the implementations are correct. </p> <p> Perhaps I should have made an example with public nested classes which can be matched by their type but then it could be refactored into Visitor pattern too. Does it mean that Visitor is isomorphic to discriminated unions then? </p> </div> <div class="comment-date">2018-04-05 18:50 UTC</div> </div> <div class="comment" id="eea4079cf3794de2a3645fb68436eca9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#eea4079cf3794de2a3645fb68436eca9">#</a></div> <div class="comment-content"> <p> Max, I agree that using a nested, private, sealed class is a good way to ensure that no-one else can add rogue implementations of an interface like <code>IOptionVisitor&lt;T&gt;</code>. </p> <p> Additionally, I think that you're correct that it isn't possible to lock down the API to the same degree if you redefine <code>Option&lt;T&gt;</code> to an interface. Just to be clear, I'm thinking about something like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IOptionVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Some(<span style="color:#2b91af;">T</span>&nbsp;value); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;None(); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IOption</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;AcceptVisitor(<span style="color:#2b91af;">IOptionVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Option</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IOption</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Some&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SomeImpl</span>&lt;<span style="color:#2b91af;">T</span>&gt;(value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SomeImpl</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IOption</span>&lt;<span style="color:#2b91af;">T</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SomeImpl(<span style="color:#2b91af;">T</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AcceptVisitor(<span style="color:#2b91af;">IOptionVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;visitor.Some(value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IOption</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;None&lt;<span style="color:#2b91af;">T</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NoneImpl</span>&lt;<span style="color:#2b91af;">T</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NoneImpl</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IOption</span>&lt;<span style="color:#2b91af;">T</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AcceptVisitor(<span style="color:#2b91af;">IOptionVisitor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;visitor) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;visitor.None(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> With a design like that, rogue implementations of <code>IOption&lt;T&gt;</code> are possible, and I admit that I can't think of a way to prevent that. </p> <p> Usually, that doesn't concern me that much, but if one were to publish a type like that as, say, a public NuGet package, that degree of lock-down could, in fact, be desirable. So, it looks like you've identified another constraint on the isomorphism. I admit that I may have been too focused on the ability to implement <em>behaviour</em> in various different ways, whereas I haven't given too much thought to accessibility. </p> <p> To be frank, one of the reasons for that is that I tend to not consider accessibility modifiers too much in C#, as I tend to design classes in such a way that they protect their invariants. When classes do that, I'm happy to make most methods <code>public</code>. </p> <p> Another reason that I've been vague on accessibility is that this could easily get implementation-specific. The way C#'s access modifiers work is different from Java's and C++'s. </p> <p> That doesn't change the fact, though, that it looks like you've identified another constraint on the isomorphism, and for that I'm very grateful. Perhaps we ought to say something like: <blockquote> Abstract classes are isomorphic with dependency injection up to accessibility. </blockquote> Again, there may be other constraints than that (and operator overloads), but I'm beholden to you for fleshing out those that you've already identified. </p> <p> About the relationship between discriminated unions and the Visitor design pattern, then yes: those are isomorphic. That's a known property, but I'm going to publish a whole (sub-)series of articles about that particular topic in the future, so I think it'd be better to discuss that when we get there. I've already written those articles, but it'll take months before I publish them, according to the publishing schedule that I currently have in mind. Very prescient of you, though. </p> </div> <div class="comment-date">2018-04-06 7:36 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Object isomorphisms https://blog.ploeh.dk/2018/02/12/object-isomorphisms 2018-02-12T19:34:00+00:00 Mark Seemann <div id="post"> <p> <em>An object is equivalent to a product of functions. Alternative ways to look at objects.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. So far, you've seen how to represent a single method or function in many different ways, but we haven't looked much at <em>objects</em> (in the object-oriented interpretation of the word). </p> <p> While this article starts by outlining the abstract concepts involved, an example is included towards the end. </p> <h3 id="40bc65be9e9f4c67af814a0b1983304d"> Objects as data with behaviour <a href="#40bc65be9e9f4c67af814a0b1983304d" title="permalink">#</a> </h3> <p> I often use the phrase that <em>objects are data with behaviour</em>. (I'm sure I didn't come up with this myself, but the source of the phrase escapes me.) In languages like C# and Java, objects are described by classes, and these often contain <em>class fields</em>. These fields constitute an instance's data, whereas its methods implement its behaviour. </p> <p> A class can contain an arbitrary number of fields, just like a method can take an arbitrary number of arguments. As demonstrated by the <a href="/2018/01/29/argument-list-isomorphisms">argument list isomorphisms</a>, you can also represent an arbitrary number of arguments as a Parameter Object. The same argument can be applied to class fields. Instead of <em>n</em> fields, you can add a single 'data class' that holds all of these fields. In <a href="http://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a> these are called <em>records</em>. You could also dissolve such a record to individual fields. That would be the inverse refactoring, so these representations are isomorphic. </p> <p> In other words, a class looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Class1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Data1</span>&nbsp;Data&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Out1</span>&nbsp;Op1(<span style="color:#2b91af;">In1</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;Out1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Out2</span>&nbsp;Op2(<span style="color:#2b91af;">In2</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;Out1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Out3</span>&nbsp;Op3(<span style="color:#2b91af;">In3</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;this,&nbsp;Data,&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;Out1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members...</span> }</pre> </p> <p> Instead of an arbitrary number of fields, I've used the above isomorphism to represent data in a single <code>Data</code> property (Java developers: a C# property is a class field with public getter and setter methods). </p> <p> In this code example, I've deliberately kept the naming abstract. The purpose of this article series is to look at the shape of code, instead of what it does, or why. From argument list isomorphisms we know that we can represent any method as taking a single input value, and returning a single output value. The remaining work to be done in this article is to figure out what to do when there's more than a single method. </p> <h3 id="ec92878346c240baa60b3aa7e85b3c6e"> Module <a href="#ec92878346c240baa60b3aa7e85b3c6e" title="permalink">#</a> </h3> <p> From <a href="/2018/01/22/function-isomorphisms">function isomorphisms</a> we know that static methods are isomorphic to instance methods, as long as you include the original object as an extra argument. In this case, all data in <code>Class1</code> is contained in a single (mutable) <code>Data1</code> record, so we can eliminate <code>Class1</code> from the argument list in favour of <code>Data1</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Class1</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Out1</span>&nbsp;Op1(<span style="color:#2b91af;">Data1</span>&nbsp;data,&nbsp;<span style="color:#2b91af;">In1</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;data&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;Out1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Out2</span>&nbsp;Op2(<span style="color:#2b91af;">Data1</span>&nbsp;data,&nbsp;<span style="color:#2b91af;">In2</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;data&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;Out1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Out3</span>&nbsp;Op3(<span style="color:#2b91af;">Data1</span>&nbsp;data,&nbsp;<span style="color:#2b91af;">In3</span>&nbsp;arg) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;data&nbsp;and&nbsp;arg;&nbsp;return&nbsp;an&nbsp;Out1&nbsp;value.</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members...</span> }</pre> </p> <p> Notice that <code>Class1</code> is now a <code>static</code> class. This simply means that it has no instance members, and if you try to add one, the C# compiler will complain. </p> <p> This is, in essence, a <em>module</em>. In F#, for example, a <code>module</code> is a static class that contains a collection of values and functions. </p> <h3 id="ba6804b2be7f48f989041fd684b55faf"> Closures as behaviour with data <a href="#ba6804b2be7f48f989041fd684b55faf" title="permalink">#</a> </h3> <p> As <em>data with behaviour</em>, objects are often passed around as input to methods. It's a convenient way to pass both data and associated behaviour (perhaps even with polymorphic dispatch) as a single thing. You'd be forgiven if you've looked at the above module-style refactoring and found it lacking in that regard. </p> <p> Nevertheless, function isomorphisms already demonstrated that you can solve this problem with closures. Imagine that you want to package all the static methods of <code>Class1</code> with a particular <code>Data1</code> value, and pass that 'package' as a single argument to another method. You can do that by closing over the value: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Data1</span>&nbsp;{&nbsp;<span style="color:green;">/*&nbsp;initialize&nbsp;members&nbsp;here&nbsp;*/</span>&nbsp;}; <span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">In1</span>,&nbsp;<span style="color:#2b91af;">Out1</span>&gt;&nbsp;op1&nbsp;=&nbsp;arg&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Class1</span>.Op1(data,&nbsp;arg); <span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">In2</span>,&nbsp;<span style="color:#2b91af;">Out2</span>&gt;&nbsp;op2&nbsp;=&nbsp;arg&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Class1</span>.Op2(data,&nbsp;arg); <span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">In3</span>,&nbsp;<span style="color:#2b91af;">Out3</span>&gt;&nbsp;op3&nbsp;=&nbsp;arg&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Class1</span>.Op3(data,&nbsp;arg); <span style="color:green;">//&nbsp;More&nbsp;closures...</span></pre> </p> <p> First, you create a <code>Data1</code> value, and initialise it with your desired values. You then create <code>op1</code>, <code>op2</code>, and so on. These are functions that close over <code>data</code>; A.K.A. <em>closures</em>. Notice that they all close over the same variable. Also keep in mind here that I'm in no way pretending that <code>data</code> is immutable. That's not a requirement. </p> <p> Now you have <em>n</em> closures that all close over the same <code>data</code>. All you need to do is to package them into a single 'object': </p> <p> <pre><span style="color:blue;">var</span>&nbsp;objEq&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(op1,&nbsp;op2,&nbsp;op3&nbsp;<span style="color:green;">/*&nbsp;more&nbsp;closures...&nbsp;*/</span>); </pre> </p> <p> Once again, tuples are workhorses of software design isomorphisms. <code>objEq</code> is an 'object equivalent' consisting of closures; it's <em>behaviour with data</em>. You can now pass <code>objEq</code> as an argument to another method, if that's what you need to do. </p> <h3 id="6152f0c00f044f02afa4720affe09616"> Isomorphism <a href="#6152f0c00f044f02afa4720affe09616" title="permalink">#</a> </h3> <p> One common variation that I sometimes see is that instead of a tuple of functions, you can create a <em>record</em> of functions. This enables you to give each function a statically enforced name. In the theory of <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>, tuples and records are both <em>product types</em>, so when looking at the shape of code, these are closely related. Records also enable you to preserve the name of each method, so that this mapping from object to record of functions becomes lossless. </p> <p> The inverse mapping also exists. If you have a record of functions, you can refactor it to a class. You use the name of each record element as a method name, and the arguments and return types to further flesh out the methods. </p> <h3 id="0805ec33f9504153a7bc4173f5189479"> Example: simplified Turtle <a href="#0805ec33f9504153a7bc4173f5189479" title="permalink">#</a> </h3> <p> As an example, consider this (over-)simplified <a href="https://en.wikipedia.org/wiki/Turtle_graphics">Turtle</a> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Turtle</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">double</span>&nbsp;X&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">double</span>&nbsp;Y&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">double</span>&nbsp;AngleInDegrees&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Turtle() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Turtle(<span style="color:blue;">double</span>&nbsp;x,&nbsp;<span style="color:blue;">double</span>&nbsp;y,&nbsp;<span style="color:blue;">double</span>&nbsp;angleInDegrees) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.X&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Y&nbsp;=&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.AngleInDegrees&nbsp;=&nbsp;angleInDegrees; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Turn(<span style="color:blue;">double</span>&nbsp;angleInDegrees) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.AngleInDegrees&nbsp;=&nbsp;(<span style="color:blue;">this</span>.AngleInDegrees&nbsp;+&nbsp;angleInDegrees)&nbsp;%&nbsp;360; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Move(<span style="color:blue;">double</span>&nbsp;distance) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Convert&nbsp;degrees&nbsp;to&nbsp;radians&nbsp;with&nbsp;180.0&nbsp;degrees&nbsp;=&nbsp;1&nbsp;pi&nbsp;radian</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;angleInRadians&nbsp;=&nbsp;<span style="color:blue;">this</span>.AngleInDegrees&nbsp;*&nbsp;(<span style="color:#2b91af;">Math</span>.PI&nbsp;/&nbsp;180); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.X&nbsp;=&nbsp;<span style="color:blue;">this</span>.X&nbsp;+&nbsp;(distance&nbsp;*&nbsp;<span style="color:#2b91af;">Math</span>.Cos(angleInRadians)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Y&nbsp;=&nbsp;<span style="color:blue;">this</span>.Y&nbsp;+&nbsp;(distance&nbsp;*&nbsp;<span style="color:#2b91af;">Math</span>.Sin(angleInRadians)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In order to keep the example simple, the only operations offered by the <code>Turtle</code> class is <code>Turn</code> and <code>Move</code>. With this simplified API, you can create a <code>turtle</code> object and interact with it: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;turtle&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Turtle</span>(); turtle.Move(2); turtle.Turn(90); turtle.Move(1);</pre> </p> <p> This sequence of operations will leave <code>turtle</code> as position <em>(2, 1)</em> and an angle of 90°. </p> <p> Instead of modelling a turtle as an object, you can instead model it as a data structure and a set of (impure) functions: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TurtleData</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">double</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">double</span>&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">double</span>&nbsp;angleInDegrees; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TurtleData() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TurtleData(<span style="color:blue;">double</span>&nbsp;x,&nbsp;<span style="color:blue;">double</span>&nbsp;y,&nbsp;<span style="color:blue;">double</span>&nbsp;angleInDegrees) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.x&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.y&nbsp;=&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.angleInDegrees&nbsp;=&nbsp;angleInDegrees; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Turn(<span style="color:#2b91af;">TurtleData</span>&nbsp;data,&nbsp;<span style="color:blue;">double</span>&nbsp;angleInDegrees) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.angleInDegrees&nbsp;=&nbsp;(data.angleInDegrees&nbsp;+&nbsp;angleInDegrees)&nbsp;%&nbsp;360; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Move(<span style="color:#2b91af;">TurtleData</span>&nbsp;data,&nbsp;<span style="color:blue;">double</span>&nbsp;distance) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Convert&nbsp;degrees&nbsp;to&nbsp;radians&nbsp;with&nbsp;180.0&nbsp;degrees&nbsp;=&nbsp;1&nbsp;pi&nbsp;radian</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;angleInRadians&nbsp;=&nbsp;data.angleInDegrees&nbsp;*&nbsp;(<span style="color:#2b91af;">Math</span>.PI&nbsp;/&nbsp;180); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.x&nbsp;=&nbsp;data.x&nbsp;+&nbsp;(distance&nbsp;*&nbsp;<span style="color:#2b91af;">Math</span>.Cos(angleInRadians)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.y&nbsp;=&nbsp;data.y&nbsp;+&nbsp;(distance&nbsp;*&nbsp;<span style="color:#2b91af;">Math</span>.Sin(angleInRadians)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">double</span>&nbsp;GetX(<span style="color:#2b91af;">TurtleData</span>&nbsp;data) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;data.x; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">double</span>&nbsp;GetY(<span style="color:#2b91af;">TurtleData</span>&nbsp;data) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;data.y; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">double</span>&nbsp;GetAngleInDegrees(<span style="color:#2b91af;">TurtleData</span>&nbsp;data) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;data.angleInDegrees; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that all five static methods take a <code>TurtleData</code> value as their first argument, just as the above abstract description suggests. The implementations are almost identical; you simply replace <code>this</code> with <code>data</code>. If you're a C# developer, you may be wondering about the accessor functions <code>GetX</code>, <code>GetY</code>, and <code>GetAngleInDegrees</code>. These are, however, the static equivalents to the <code>Turtle</code> class <code>X</code>, <code>Y</code>, and <code>AngleInDegrees</code> properties. Keep in mind that in C#, a property is nothing but syntactic sugar over one (or two) IL methods (e.g. <code>get_X()</code>). </p> <p> You can now create a pentuple (a five-tuple) of closures over those five static methods and a single <code>TurtleData</code> object. While you can always do that from scratch, it's illuminating to transform a <code>Turtle</code> into such a tuple, thereby illustrating how that morphism looks: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&gt;&nbsp;ToTuple() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TurtleData</span>(<span style="color:blue;">this</span>.X,&nbsp;<span style="color:blue;">this</span>.Y,&nbsp;<span style="color:blue;">this</span>.AngleInDegrees); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;turn&nbsp;=&nbsp;angle&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.Turn(data,&nbsp;angle); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;move&nbsp;=&nbsp;distance&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.Move(data,&nbsp;distance); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;getX&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.GetX(data); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;getY&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.GetY(data); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;getAngle&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.GetAngleInDegrees(data); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(turn,&nbsp;move,&nbsp;getX,&nbsp;getY,&nbsp;getAngle); }</pre> </p> <p> This <code>ToTuple</code> method is an instance method on <code>Turtle</code> (I just held it back from the above code listing, in order to list it here instead). It creates a new <code>TurtleData</code> object from its current state, and proceeds to close over it five times - each time delegating the closure implementation to the corresponding static method. Finally, it creates a pentuple of those five closures. </p> <p> You can interact with the pentuple just like it was an object: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;turtle&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Turtle</span>().ToTuple(); turtle.Item2(2); turtle.Item1(90); turtle.Item2(1);</pre> </p> <p> The syntax is essentially the same as before, but clearly, this isn't as readable. You have to remember that <code>Item2</code> contains the <code>move</code> closure, <code>Item1</code> the <code>turn</code> closure, and so on. Still, since they are all delegates, you can call them as though they are methods. </p> <p> I'm not trying to convince you that this sort of design is better, or even equivalent, in terms of readability. Clearly, it isn't - at least in C#. The point is, however, that from a perspective of structure, these two models are equivalent. Everything you can do with an object, you can also do with a tuple of closures. </p> <p> So far, you've seen that you can translate a <code>Turtle</code> into a tuple of closures, but in order to be an isomorphism, the reverse translation should also be possible. </p> <p> One way to translate from <code>TurtleData</code> to <code>Turtle</code> is with this static method (i.e. function): </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Turtle</span>&nbsp;ToTurtle(<span style="color:#2b91af;">TurtleData</span>&nbsp;data) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Turtle</span>(data.x,&nbsp;data.y,&nbsp;data.angleInDegrees); }</pre> </p> <p> Another option for making the pentuple of closures look like an object is to extract an interface from the original <code>Turtle</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ITurtle</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Turn(<span style="color:blue;">double</span>&nbsp;angleInDegrees); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Move(<span style="color:blue;">double</span>&nbsp;distance); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">double</span>&nbsp;X&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">double</span>&nbsp;Y&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">double</span>&nbsp;AngleInDegrees&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> Not only can you let <code>Turtle</code> implement this interface (<code>public class Turtle : ITurtle</code>), but you can also define an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">TupleTurtle</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ITurtle</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;TupleTurtle( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&gt;&nbsp;imp) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.imp&nbsp;=&nbsp;imp; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Turn(<span style="color:blue;">double</span>&nbsp;angleInDegrees) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.imp.Item1(angleInDegrees); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Move(<span style="color:blue;">double</span>&nbsp;distance) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.imp.Item2(distance); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">double</span>&nbsp;X &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.imp.Item3();&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">double</span>&nbsp;Y &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.imp.Item4();&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">double</span>&nbsp;AngleInDegrees &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.imp.Item5();&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class simply delegates all its behaviour to the implementing pentuple. It can be used like this with no loss of readability: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;turtle&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TupleTurtle</span>(<span style="color:#2b91af;">TurtleData</span>.CreateDefault()); turtle.Move(2); turtle.Turn(90); turtle.Move(1);</pre> </p> <p> This example utilises this creation function: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;CreateDefault() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;data&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TurtleData</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;turn&nbsp;=&nbsp;angle&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.Turn(data,&nbsp;angle); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Action</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;move&nbsp;=&nbsp;distance&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.Move(data,&nbsp;distance); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;getX&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.GetX(data); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;getY&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.GetY(data); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">double</span>&gt;&nbsp;getAngle&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TurtleData</span>.GetAngleInDegrees(data); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(turn,&nbsp;move,&nbsp;getX,&nbsp;getY,&nbsp;getAngle); }</pre> </p> <p> This function is almost identical to the above <code>ToTuple</code> method, and those two could easily be refactored to a single method. </p> <p> This example demonstrates how an object can also be viewed as a tuple of closures, and that translations exist both ways between those two views. </p> <h3 id="74da051e30dc457dac7c3bcd9c11686a"> Conclusion <a href="#74da051e30dc457dac7c3bcd9c11686a" title="permalink">#</a> </h3> <p> To be clear, I'm not trying to convince you that it'd be great if you wrote all of your C# or Java using tuples of closures; it most likely wouldn't be. The point is that a class is isomorphic to a tuple of functions. </p> <p> From <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, and particular its application to Haskell, we know quite a bit about the properties of certain functions. Once we start to look at objects as tuples of functions, we may be able to say something about the properties of objects, because category theory also has something to say about the properties of tuples (for example that a tuple of <a href="/2017/10/06/monoids">monoids</a> is <a href="/2017/10/30/tuple-monoids">itself a monoid</a>). </p> <p> <strong>Next:</strong> <a href="/2018/02/19/abstract-class-isomorphism">Abstract class isomorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Uncurry isomorphisms https://blog.ploeh.dk/2018/02/05/uncurry-isomorphisms 2018-02-05T07:54:00+00:00 Mark Seemann <div id="post"> <p> <em>Curried functions are isomorphic to tupled functions.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. <strong>Nota bene:</strong> it's <em>not</em> about <a href="https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence">Curry–Howard isomorphism</a>. In order to prevent too much confusion, I chose the title <em>Uncurry isomorphism</em> over <em>Curry isomorphism</em>. </p> <p> The <a href="https://www.haskell.org">Haskell</a> base library includes two functions called <code>curry</code> and <code>uncurry</code>, and for anyone aware of them, it should be no surprise that they are each others' inverses. This is another important software design isomorphism, because in the previous article, you saw that <a href="/2018/01/29/argument-list-isomorphisms">all methods can be represented in tupled form</a>. The current isomorphism then extends that result because tupled and curried forms are isomorphic. </p> <h3 id="802dc6627a004cdcb47f727165f6cef0"> An F# introduction to curry and uncurry <a href="#802dc6627a004cdcb47f727165f6cef0" title="permalink">#</a> </h3> <p> While Haskell programmers are likely to be familiar with <code>curry</code> and <code>uncurry</code>, developers more familiar with other languages may not know them well. In this section follows an introduction in <a href="http://fsharp.org">F#</a>. Haskellers can skip it if they like. </p> <p> In F#, you often have to interoperate with code written in C#, and as the previous article explained, all such methods look to F# like functions taking a single tuple as input. Sometimes, however, you'd wish they were curried. </p> <p> This little function can help with that: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;*&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">curry</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;y&nbsp;=&nbsp;<span style="color:navy;">f</span>&nbsp;(x,&nbsp;y)</pre> </p> <p> You'll probably have to look at it for a while, and perhaps play with it, before it clicks, but it does this: it takes a function (<code>f</code>) that takes a tuple (<code>'a * 'b</code>) as input, and returns a new function that does the same, but instead takes the arguments in curried form: <code>'a -&gt; 'b -&gt; 'c</code>. </p> <p> It can be useful in interoperability scenarios. Imagine, as a toy example, that you have to list the powers of two from 0 to 10. You can use <a href="https://msdn.microsoft.com/en-us/library/system.math.pow">Math.Pow</a>, but since it was designed with C# in mind, its argument is a single tuple. <code>curry</code> to the rescue: </p> <p> <pre>&gt; List.map (curry Math.Pow 2.) [0.0..10.0];; val it : float list = [1.0; 2.0; 4.0; 8.0; 16.0; 32.0; 64.0; 128.0; 256.0; 512.0; 1024.0]</pre> </p> <p> While <code>Math.Pow</code> has the type <code>float * float -&gt; float</code>, <code>curry Math.Pow</code> turns it into a function with the type <code>float -&gt; float -&gt; float</code>. Since that function is curried, it can be partially applied with the value <code>2.</code>, which returns a function of the type <code>float -&gt; float</code>. That's a function you can use with <code>List.map</code>. </p> <p> You'd hardly be surprised that you can also <code>uncurry</code> a function: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;a&nbsp;*&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">uncurry</span>&nbsp;<span style="color:navy;">f</span>&nbsp;(x,&nbsp;y)&nbsp;=&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;y</pre> </p> <p> This function takes a curried function <code>f</code>, and returns a new function that does the same, but instead takes a tuple as input. </p> <h3 id="276c1e476f7c4f9fadff82bca9617e0f"> Pair isomorphism <a href="#276c1e476f7c4f9fadff82bca9617e0f" title="permalink">#</a> </h3> <p> Haskell comes with <code>curry</code> and <code>uncurry</code> as part of its standard library. It hardly comes as a surprise that they form an isomorphism. You can demonstrate this with some <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a> properties. </p> <p> If you have a curried function, you should be able to first <code>uncurry</code> it, then <code>curry</code> that function, and that function should be the same as the original function. In order to demonstrate that, I chose the <code>(&lt;&gt;)</code> operator from <code>Data.Semigroup</code>. Recall that Haskell operators are curried functions. This property function demonstrates the round-trip property of <code>uncurry</code> and <code>curry</code>: </p> <p> <pre><span style="color:#600277;">semigroup2RoundTrips</span>&nbsp;::&nbsp;(<span style="color:blue;">Semigroup</span>&nbsp;a,&nbsp;<span style="color:blue;">Eq</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Bool semigroup2RoundTrips&nbsp;x&nbsp;y&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;x&nbsp;<span style="color:#666666;">&lt;&gt;</span>&nbsp;y&nbsp;<span style="color:#666666;">==</span>&nbsp;curry&nbsp;(uncurry&nbsp;<span style="color:#600277;">(&lt;&gt;)</span>)&nbsp;x&nbsp;y </pre> </p> <p> This property states that the result of combining two <a href="/2017/11/27/semigroups">semigroup</a> values is the same as first uncurrying <code>(&lt;&gt;)</code>, and then 'recurry' it. It passes for various <code>Semigroup</code> instances: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;All&nbsp;round-trips&quot;</span>&nbsp;(semigroup2RoundTrips&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">All</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">All</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Bool</span>), testProperty&nbsp;<span style="color:#a31515;">&quot;Any&nbsp;round-trips&quot;</span>&nbsp;(semigroup2RoundTrips&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Any</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Any</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Bool</span>), testProperty&nbsp;<span style="color:#a31515;">&quot;First&nbsp;round-trips&quot;</span>&nbsp;(semigroup2RoundTrips&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">First</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">First</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Bool</span>), testProperty&nbsp;<span style="color:#a31515;">&quot;Last&nbsp;round-trips&quot;</span>&nbsp;(semigroup2RoundTrips&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Last</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Last</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Bool</span>), testProperty&nbsp;<span style="color:#a31515;">&quot;Sum&nbsp;round-trips&quot;</span>&nbsp;(semigroup2RoundTrips&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Sum</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Sum</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Bool</span>), testProperty&nbsp;<span style="color:#a31515;">&quot;Product&nbsp;round-trips&quot;</span>&nbsp;(semigroup2RoundTrips&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Product</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Product</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Bool</span>) </pre> </p> <p> It's not a formal proof that all of these properties pass, but it does demonstrate the isomorphic nature of these two functions. In order to be truly isomorphic, however, you must also be able to start with a tupled function. In order to have a similar tupled function, I defined this: </p> <p> <pre><span style="color:#600277;">t2sg</span>&nbsp;::&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(a,&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a t2sg&nbsp;(x,&nbsp;y)&nbsp;<span style="color:#666666;">=</span>&nbsp;x&nbsp;<span style="color:#666666;">&lt;&gt;</span>&nbsp;y</pre> </p> <p> The <em>t2</em> in the name stands for <em>tuple-2</em>, and <em>sg</em> means <em>semigroup</em>. It really only exposes <code>(&lt;&gt;)</code> in tupled form. With it, though, you can write another property that demonstrates that the mapping starting with a tupled form is also an isomorphism: </p> <p> <pre><span style="color:#600277;">pairedRoundTrips</span>&nbsp;::&nbsp;(<span style="color:blue;">Semigroup</span>&nbsp;a,&nbsp;<span style="color:blue;">Eq</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Bool pairedRoundTrips&nbsp;x&nbsp;y&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;t2sg&nbsp;(x,&nbsp;y)&nbsp;<span style="color:#666666;">==</span>&nbsp;uncurry&nbsp;(curry&nbsp;t2sg)&nbsp;(x,&nbsp;y) </pre> </p> <p> You can create properties for the same instances of <code>Semigroup</code> as the above list for <code>semigroup2RoundTrips</code>, and they all pass as well. </p> <h3 id="5011c10f550d44a78b5fbafd12df999f"> Triplet isomorphism <a href="#5011c10f550d44a78b5fbafd12df999f" title="permalink">#</a> </h3> <p> <code>curry</code> and <code>uncurry</code> only works for pairs (two-tuples) and functions that take exactly two curried arguments. What if you have a function that takes three curried arguments, or a function that takes a triplet (three-tuple) as an argument? </p> <p> First of all, while they aren't built-in, you can easily define corresponding mappings for those as well: </p> <p> <pre><span style="color:#600277;">curry3</span>&nbsp;::&nbsp;((a,&nbsp;b,&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;d)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;d curry3&nbsp;f&nbsp;x&nbsp;y&nbsp;z&nbsp;<span style="color:#666666;">=</span>&nbsp;f&nbsp;(x,&nbsp;y,&nbsp;z) <span style="color:#600277;">uncurry3</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;d)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a,&nbsp;b,&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;d uncurry3&nbsp;f&nbsp;(x,&nbsp;y,&nbsp;z)&nbsp;<span style="color:#666666;">=</span>&nbsp;f&nbsp;x&nbsp;y&nbsp;z </pre> </p> <p> These form an isomorphism as well. </p> <p> More generally, though, you can represent a triplet <code>(a, b, c)</code> as a nested pair: <code>(a, (b, c))</code>. These two representations are also isomorphic, as is <code>(a, b, c, d)</code> with <code>(a, (b, (c, d)))</code>. In other words, you can represent any n-tuple as a nested pair, and you already know that a function taking a pair as input is isomorphic to a curried function. </p> <h3 id="16541c1b69804d76a725715547ece986"> Summary <a href="#16541c1b69804d76a725715547ece986" title="permalink">#</a> </h3> <p> From <a href="https://en.wikipedia.org/wiki/Abstract_algebra">abstract algebra</a>, and particularly its application to a language like Haskell, we have mathematical abstractions over computation - semigroups, for example! In Haskell, these abstractions are often represented in curried form. If we wish to learn about such abstractions, and see if we can use them in object-oriented programming as well, we need to translate the curried representations into something more closely related to object-oriented programming, such as C# or Java. </p> <p> The present article describes how functions in curried form are equivalent to functions that take a single tuple as argument, and in a <a href="/2018/01/22/function-isomorphisms">previous article</a>, you saw how such functions are isomorphic to C# or Java methods. These equivalences provide a bridge that enables us to take what we've learned about abstract algebra and <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, and bring them to object-oriented programming. </p> <p> <strong>Next:</strong> <a href="/2018/02/12/object-isomorphisms">Object isomorphisms</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Argument list isomorphisms https://blog.ploeh.dk/2018/01/29/argument-list-isomorphisms 2018-01-29T07:23:00+00:00 Mark Seemann <div id="post"> <p> <em>There are many ways to represent an argument list. An overview for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. </p> <p> Most programming languages enable you to pass arguments to operations. In C# and Java, you declare methods with a list of arguments: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Bar(<span style="color:#2b91af;">Baz</span>&nbsp;baz,&nbsp;<span style="color:#2b91af;">Qux</span>&nbsp;qux)</pre> </p> <p> Here, <code>baz</code> and <code>qux</code> are arguments to the <code>Bar</code> method. Together, the arguments for a method is called an <em>argument list</em>. To be clear, this isn't universally adopted terminology, but is what I'll be using in this article. Sometimes, people (including me) say <em>parameter</em> instead of <em>argument</em>, and I'm not aware of any formal specification to differentiate the two. </p> <p> While you can pass arguments as a flat list, you can also model them as parameter objects or tuples. These representations are equivalent, because lossless translations between them exist. We say that they are isomorphic. </p> <h3 id="3d3062e7768547348c90892b94b025a8"> Isomorphisms <a href="#3d3062e7768547348c90892b94b025a8" title="permalink">#</a> </h3> <p> In theory, you can declare a method that takes thousands of arguments. In practice, you should constrain your design to as few arguments as possible. As <a href="http://amzn.to/YPdQDf">Refactoring</a> demonstrates, one way to do that is to <em>Introduce Parameter Object</em>. That, already, teaches us that there's a mapping from a flat argument list to a Parameter Object. Is there an inverse mapping? Do other representations exist? </p> <p> <img src="/content/binary/argument-list-isomorphisms.png" alt="Isomorphisms between the general concept of a product type, and three types of representation: argument lists, parameter objects, and tuples."> </p> <p> There's at least three alternative representations of a group of arguments: <ul> <li>Argument list</li> <li>Parameter Object</li> <li>Tuple</li> </ul> The central concept that they all seem to orbit is what in <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a> is known as a <em>product</em>. In the theory of <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>, this is known as a <em>product type</em>. In short, a product type is a composite of elements, where each element can vary independently of the other elements. For more details, I can recommend <a href="http://tomasp.net">Tomáš Petříček</a>'s introduction to the subject: <a href="http://tomasp.net/blog/types-and-math.aspx">Power of mathematics: Reasoning about functional types</a>. </p> <h3 id="a50c52d2cd9b448b8a9f9213a8eba24f"> Argument list/Parameter Object isomorphism <a href="#a50c52d2cd9b448b8a9f9213a8eba24f" title="permalink">#</a> </h3> <p> Perhaps the best-known mapping from an argument list is the <em>Introduce Parameter Object</em> refactoring described in <em>Refactoring</em>. </p> <p> Since the refactoring is described in detail in the book, I don't want to repeat it all here, but in short, assume that you have a method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Bar</span>&nbsp;Baz(<span style="color:#2b91af;">Qux</span>&nbsp;qux,&nbsp;<span style="color:#2b91af;">Corge</span>&nbsp;corge)</pre> </p> <p> In this case, the method only has two arguments, so the refactoring may not be necessary, but that's not the point. The point is that it's possible to refactor the code to this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Bar</span>&nbsp;Baz(<span style="color:#2b91af;">BazParameter</span>&nbsp;arg) </pre> </p> <p> where <code>BazParameter</code> looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">BazParameter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Qux</span>&nbsp;Qux&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Corge</span>&nbsp;Corge&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> In <em>Refactoring</em>, the recipe states that you should make the class immutable, and while that's a good idea (I recommend it!), it's technically not necessary in order to perform the translation, so I omitted it here in order to make the code simpler. </p> <p> You're probably able to figure out how to translate back again. We could call this refactoring <em>Dissolve Parameter Object:</em> <ol> <li>For each field or property in the Parameter Object, add a new method argument to the target method.</li> <li>At all call sites, pass the Parameter Object's field or property value as each of those new arguments.</li> <li>Change the method body so that it uses each new argument, instead of the Parameter Object.</li> <li>Remove the Parameter Object argument, and update call sites accordingly.</li> </ol> You can go back and forth between a 'flat' argument list and a Parameter Object without loss of information, so these two refactorings together form an isomorphism. </p> <p> As an example, consider the <a href="/2017/10/30/tuple-monoids">Roster example from a previous article</a>. The <code>Combine</code> method on the <code>Roster</code> class is implemented like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;Combine(<span style="color:#2b91af;">Roster</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Girls&nbsp;+&nbsp;other.Girls, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Boys&nbsp;+&nbsp;other.Boys, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Exemptions.Concat(other.Exemptions).ToArray()); }</pre> </p> <p> This method takes an object as a single argument. You can think of this <code>Roster</code> object as a Parameter Object. </p> <p> If you like, you can add a method overload that dissolves the <code>Roster</code> object to its constituent values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;Combine( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;otherGirls, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;otherBoys, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:blue;">string</span>[]&nbsp;otherExemptions) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Combine( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>(otherGirls,&nbsp;otherBoys,&nbsp;otherExemptions)); }</pre> </p> <p> In this incarnation, the dissolved method overload creates a new <code>Roster</code> from its argument list and delegates to the other overload. This is, however, an arbitrary implementation detail. You could just as well implement the two methods the other way around: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;Combine(<span style="color:#2b91af;">Roster</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Combine( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.Girls, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.Boys, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.Exemptions.ToArray()); } <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;Combine( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;otherGirls, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;otherBoys, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:blue;">string</span>[]&nbsp;otherExemptions) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Girls&nbsp;+&nbsp;otherGirls, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Boys&nbsp;+&nbsp;otherBoys, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Exemptions.Concat(otherExemptions).ToArray()); }</pre> </p> <p> In this variation, the overload that takes three arguments contains the implementation, whereas the <code>Combine(Roster)</code> overload simply delegates to the <code>Combine(int, int, string[])</code> overload. </p> <p> In order to illustrate the idea that both APIs are equivalent, in this example I show two method overloads side by side. The overall point, however, is that you can translate between such two representations without changing the behaviour of the system. You don't have to keep both method overloads in place together. </p> <h3 id="97ea8f87723f4b1bb995b0824b1caba4"> Argument list/tuple isomorphism <a href="#97ea8f87723f4b1bb995b0824b1caba4" title="permalink">#</a> </h3> <p> In relationship to statically typed functional programming, the term <em>argument list</em> is confounding. In the functional programming languages I've so far dabbled with (<a href="http://fsharp.org">F#</a>, <a href="https://www.haskell.org">Haskell</a>, <a href="http://www.purescript.org">PureScript</a>, <a href="https://clojure.org">Clojure</a>, <a href="https://www.erlang.org">Erlang</a>), the word <em>list</em> is used synonymously with <a href="https://en.wikipedia.org/wiki/Linked_list">linked list</a>. </p> <p> As a data type, a linked list can hold an arbitrary number of elements. (In Haskell, it can even be infinite, because Haskell is lazily evaluated.) Statically typed languages like F# and Haskell add the constraint that all elements must have the same type. </p> <p> An argument list like <code>(Qux qux, Corge corge)</code> isn't at all a statically typed linked list. Neither does it have an arbitrary size nor does it contain elements of the same type. On the contrary, it has a fixed length (two), and elements of different types. The first element must be a <code>Qux</code> value, and the second element must be a <code>Corge</code> value. </p> <p> That's not a list; that's a tuple. </p> <p> Surprisingly, Haskell may provide the most digestible illustration of that, even if you don't know how to read Haskell syntax. Suppose you have the values <code>qux</code> and <code>corge</code>, of similarly named types. Consider a C# method call <code>Baz(qux, corge)</code>. What's the type of the 'argument list'? </p> <p> <pre>λ&gt; :type (qux, corge) (qux, corge) :: (Qux, Corge)</pre> </p> <p> <code>:type</code> is a <a href="https://wiki.haskell.org/GHC/GHCi">GHCi</a> command that displays the type of an expression. By coincidence (or is it?), the C# argument list <code>(qux, corge)</code> is also valid Haskell syntax, but it is syntax for a tuple. In this example, the tuple is a <em>pair</em> where the first element has the type <code>Qux</code>, and the second element has the type <code>Corge</code>, but <code>(foo, qux, corge)</code> would be a triple, <code>(foo, qux, corge, grault)</code> would be a quadruple, and so on. </p> <p> We know that the argument list/tuple isomorphism exists, because that's how the F# compiler works. F# is a multi-paradigmatic language, and it can interact with C# code. It does that by treating all C# argument lists as tuples. Consider this example of calling <a href="https://msdn.microsoft.com/en-us/library/system.math.pow">Math.Pow</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;<span style="color:teal;">Math</span>.<span style="color:navy;">Pow</span>(2.,&nbsp;4.)</pre> </p> <p> Programmers who still write more C# than F# often write it like that, because it looks like a method call, but I prefer to insert a space between the method and the arguments: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;<span style="color:teal;">Math</span>.<span style="color:navy;">Pow</span>&nbsp;(2.,&nbsp;4.)</pre> </p> <p> The reason is that in F#, function calls are delimited with space. The brackets are there in order to override the normal operator precedence, just like you'd write <code>(1 + 2) * 3</code> in order to get <code>9</code> instead of <code>7</code>. This is better illustrated by introducing an intermediate value: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;t&nbsp;=&nbsp;(2.,&nbsp;4.) <span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;<span style="color:teal;">Math</span>.<span style="color:navy;">Pow</span>&nbsp;t</pre> </p> <p> or even </p> <p> <pre><span style="color:blue;">let</span>&nbsp;t&nbsp;=&nbsp;2.,&nbsp;4. <span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;<span style="color:teal;">Math</span>.<span style="color:navy;">Pow</span>&nbsp;t</pre> </p> <p> because the brackets are now redundant. In the last two examples, <code>t</code> is a tuple of two floating-point numbers. All four code examples are equivalent and compile, thereby demonstrating that a translation exist from F# tuples to C# argument lists. </p> <p> The inverse translation exists as well. You can see a demonstration of this in the (dormant) <a href="https://github.com/ploeh/Numsense">Numsense</a> code base, which includes an object-oriented <a href="https://en.wikipedia.org/wiki/Facade_pattern">Façade</a>, which defines (among other things) an interface where the <code>TryParse</code> method takes a tuple argument. Here's the declaration of that method: </p> <p> <pre><span style="color:blue;">abstract</span>&nbsp;<span style="color:navy;">TryParse</span>&nbsp;:&nbsp;s&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;*&nbsp;[&lt;<span style="color:teal;">Out</span>&gt;]result&nbsp;:&nbsp;<span style="color:teal;">int</span>&nbsp;<span style="color:teal;">byref</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">bool</span></pre> </p> <p> That looks cryptic, but if you remove the <code>[&lt;Out&gt;]</code> annotation and the argument names, the method is declared as taking <em>single</em> input value of the type <code>string * int byref</code>. It's a single value, but it's a tuple (a pair). </p> <p> Perhaps it's easier to understand if you see an implementation of this interface method, so here's the English implementation: </p> <p> <pre><span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">TryParse</span>&nbsp;(s,&nbsp;result)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Helper</span>.<span style="color:navy;">tryParse</span>&nbsp;<span style="color:teal;">Numeral</span>.<span style="color:navy;">tryParseEnglish</span>&nbsp;(s,&nbsp;&amp;result)</pre> </p> <p> You can see that, as I've described above, I've inserted a space between <code>this.TryParse</code> and <code>(s, result)</code>, in order to highlight that this is an F# function that takes a single tuple as input. </p> <p> In C#, however, you can use the method as though it had a standard C# argument list: </p> <p> <pre><span style="color:blue;">int</span>&nbsp;i; <span style="color:blue;">var</span>&nbsp;success&nbsp;=&nbsp;<span style="color:#2b91af;">Numeral</span>.English.TryParse( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;one-thousand-three-hundred-thirty-seven&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">out</span>&nbsp;i);</pre> </p> <p> You'll note that this is an advanced example that involves an <code>out</code> parameter, but even in this edge case, the translation is possible. </p> <p> C# argument lists and F# tuples are isomorphic. I'd be surprised if this result doesn't extend to other languages. </p> <h3 id="7302293f4f3947cfbf97b328edc34e54"> Parameter Object/tuple isomorphism <a href="#7302293f4f3947cfbf97b328edc34e54" title="permalink">#</a> </h3> <p> The third isomorphism that I claim exists is the one between Parameter Objects and tuples. If, however, we assume that the two above isomorphisms hold, then this third isomorphism exists as well. I know from my copy of <a href="http://amzn.to/13tGJ0f">Conceptual Mathematics</a> that isomorphisms are transitive. If you can translate from Parameter Object to argument list, and from argument list to tuple, then you can translate from Parameter Object to tuple; and vice versa. </p> <p> Thus, I'm not going to use more words on this isomorphism. </p> <h3 id="2afad24d481348ecbc0bc08c4e5b82bb"> Summary <a href="#2afad24d481348ecbc0bc08c4e5b82bb" title="permalink">#</a> </h3> <p> Argument lists, Parameter Objects, and tuples are isomorphic. This has a few interesting implications, first of which is that because all these refactorings exist, you can employ them. If a method's argument list is inconvenient, consider introducing a Parameter Object. If your Parameter Object starts to look <a href="/2015/08/17/when-x-y-and-z-are-great-variable-names">so generic that you have a hard time coming up with good names for its elements</a>, perhaps a tuple is more appropriate. On the other hand, if you have a tuple, but it's unclear what role each unnamed element plays, consider refactoring to an argument list or Parameter Object. </p> <p> Another important result is that since these three ways to model arguments are isomorphic, we can treat them as interchangeable in analysis. For instance, from category theory we can learn about the properties of tuples. These properties, then, also apply to C# and Java argument lists. </p> <p> <strong>Next:</strong> <a href="/2018/02/05/uncurry-isomorphisms">Uncurry isomorphisms</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Function isomorphisms https://blog.ploeh.dk/2018/01/22/function-isomorphisms 2018-01-22T14:37:00+00:00 Mark Seemann <div id="post"> <p> <em>Instance methods are isomorphic to functions.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. </p> <p> While I have already, in <a href="/2014/03/10/solid-the-next-step-is-functional">an earlier article</a>, quoted the following parable about Anton, Qc Na, objects, and closures, it's too good a fit to the current topic to pass up, so please pardon the duplication. </p> <blockquote> <p> The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures." </p> <p> Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress. </p> <p> On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened. </p> <p> - <a href="http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html">Anton van Straaten</a> </p> </blockquote> <p> The point is that objects and closures are two ways of looking at a thing. In a nutshell, objects are data with behaviour, whereas closures are behaviour with data. I've <a href="/2014/03/10/solid-the-next-step-is-functional">already shown an elaborate C# example of this</a>, so in this article, you'll get a slightly more formal treatment of the subject. </p> <h3 id="a9dcce40867242509b9ed82b3bb71481"> Isomorphism <a href="#a9dcce40867242509b9ed82b3bb71481" title="permalink">#</a> </h3> <p> In object-oriented design, you often bundle operations as methods that belong to objects. These are isomorphic to static methods, because a lossless translation exists. We can call such static methods <em>functions</em>, although they aren't guaranteed to be <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>. </p> <p> <img src="/content/binary/function-isomorphism.png" alt="Diagram showing isomorphism between instance method and function."> </p> <p> In the spirit of <a href="http://amzn.to/YPdQDf">Refactoring</a>, we can describe each translation as a refactoring, because that's what it is. I don't think the book contains a specific refactoring that describes how to translate from an instance method to a static method, but we could call it <em>Replace Object with Argument</em>. </p> <p> Going the other way is, on the other hand, already described, so we can use Refactoring's <em>Move Method</em>. </p> <h3 id="e8be3efbe29e44dc97d5dd8ecd2928c2"> Replace Object with Argument <a href="#e8be3efbe29e44dc97d5dd8ecd2928c2" title="permalink">#</a> </h3> <p> While the concept of refactoring ought to be neutral across paradigms, the original book is about object-oriented programming. In object-oriented programming, objects are the design ideal, so it didn't make much sense to include, in the book, a refactoring that turns an instance method into a static method. </p> <p> Nevertheless, it's straightforward: <ol> <li>Add an argument to the instance method. Declare the argument as the type of the hosting class.</li> <li>In the method body, change all calls to <code>this</code> and <code>base</code> to the new argument.</li> <li>Make the method static.</li> </ol> As an example, imagine that you start with an instance method like this <code>Baz</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Foo</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Bar</span>&nbsp;Baz(<span style="color:#2b91af;">Qux</span>&nbsp;qux,&nbsp;<span style="color:#2b91af;">Corge</span>&nbsp;corge) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff,&nbsp;return&nbsp;bar</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Other&nbsp;members...</span> }</pre> </p> <p> You'll first introduce a new <code>Foo foo</code> argument to <code>Baz</code>, change the method body to use <code>foo</code> instead of <code>this</code>, and then make the method static. The result is this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Foo</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Bar</span>&nbsp;Baz(<span style="color:#2b91af;">Foo</span>&nbsp;foo,&nbsp;<span style="color:#2b91af;">Qux</span>&nbsp;qux,&nbsp;<span style="color:#2b91af;">Corge</span>&nbsp;corge) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff,&nbsp;return&nbsp;bar</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Other&nbsp;members...</span> }</pre> </p> <p> Once you have a static method, you can always move it to another class, if you'd like. This can, however, cause some problems with accessibility. In C#, for example, you'd no longer be able to access <code>private</code> or <code>protected</code> members from a method outside the class. You can choose to leave the static method reside on the original class, or you can make the member in question available to more clients (make it <code>internal</code> or <code>public</code>). </p> <h3 id="aa46d8dc81e844f6bb0691c0fcb4db91"> Move Method <a href="#aa46d8dc81e844f6bb0691c0fcb4db91" title="permalink">#</a> </h3> <p> The book Refactoring doesn't contain a recipe like the above, because the goal of that book is better object-oriented design. It would consider a static method, like the second variation of <code>Baz</code> above, a code smell named <em>Feature Envy</em>. You have this code smell when it looks as if a method is more interested in one of its arguments than in its host object. In that case, the book suggests using the <em>Move Method</em> refactoring. </p> <p> The book already describes this refactoring, so I'm not going to repeat it here. Also, there's no sense in showing you the code example, because it's literally the same two code examples as above, only in the opposite order. You start with the static method and end with the instance method. </p> <p> C# developers are most likely already aware of this relationship between static methods and objects, because you can use the <code>this</code> keyword in a static method to make it look like an instance method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Bar</span>&nbsp;Baz(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;foo,&nbsp;<span style="color:#2b91af;">Qux</span>&nbsp;qux,&nbsp;<span style="color:#2b91af;">Corge</span>&nbsp;corge) </pre> </p> <p> The addition of <code>this</code> in front of the <code>Foo foo</code> argument enables the C# compiler to treat the <code>Baz</code> method as though it's an instance method on a <code>Foo</code>object: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;bar&nbsp;=&nbsp;foo.Baz(qux,&nbsp;corge);</pre> </p> <p> This is only syntactic sugar. The method is still compiled as a static method, and if you, as a client developer, wish to use it as a static method, that's still possible. </p> <h3 id="cc678b065323453f96a9a16b8576a14d"> Functions <a href="#cc678b065323453f96a9a16b8576a14d" title="permalink">#</a> </h3> <p> A static method like <code>public static Bar Baz(Foo foo, Qux qux, Corge corge)</code> looks a lot like a function. If refactored from object-oriented design, that function is likely to be impure, but its shape is function-like. </p> <p> In C#, for example, you could model it as a variable of a delegate type: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Foo</span>,&nbsp;<span style="color:#2b91af;">Qux</span>,&nbsp;<span style="color:#2b91af;">Corge</span>,&nbsp;<span style="color:#2b91af;">Bar</span>&gt;&nbsp;baz&nbsp;=&nbsp;(foo,&nbsp;qux,&nbsp;corge)&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff,&nbsp;return&nbsp;bar</span> };</pre> </p> <p> Here, <code>baz</code> is a function with the same signature as the above static <code>Baz</code> method. </p> <p> Have you ever noticed something odd about the various <code>Func</code> delegates in C#? </p> <p> They take the return type as the <em>last</em> type argument, which is contrary to C# syntax, where you have to declare the return type before the method name and argument list. Since C# is the dominant .NET language, that's surprising, and even counter-intuitive. </p> <p> It does, however, nicely align with an ML language like <a href="http://fsharp.org">F#</a>. As we'll see in a future article, the above <code>baz</code> function translates to an F# function with the type <code>Foo * Qux * Corge -&gt; Bar</code>, and to <a href="https://www.haskell.org">Haskell</a> as a function with the type <code>(Foo, Qux, Corge) -&gt; Bar</code>. Notice that the return type comes last, just like in the C# <code>Func</code>. </p> <h3 id="daef5921b96646258260c0c18cee856f"> Closures <a href="#daef5921b96646258260c0c18cee856f" title="permalink">#</a> </h3> <p> <em>...but,</em> you may say, <em>what about data with behaviour?</em> One of the advantages, after all, of objects is that you can associate a particular collection of data with some behaviour. The above <code>Foo</code> class could have data members, and you may sometimes have a need for passing both data and behaviour around as a single... well... <em>object</em>. </p> <p> That seems to be much harder with a static <code>Baz</code> method. </p> <p> Don't worry, write a closure: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;foo&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;{&nbsp;<span style="color:green;">/*&nbsp;initialize&nbsp;members&nbsp;here&nbsp;*/</span>&nbsp;}; <span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Qux</span>,&nbsp;<span style="color:#2b91af;">Corge</span>,&nbsp;<span style="color:#2b91af;">Bar</span>&gt;&nbsp;baz&nbsp;=&nbsp;(qux,&nbsp;corge)&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Do&nbsp;stuff&nbsp;with&nbsp;foo,&nbsp;qux,&nbsp;and&nbsp;corge;&nbsp;return&nbsp;bar</span> };</pre> </p> <p> In this variation, <code>baz</code> closes over <code>foo</code>. Inside the function body, you can use <code>foo</code> like you can use <code>qux</code> and <code>corge</code>. As I've <a href="/2014/03/10/solid-the-next-step-is-functional">already covered in an earlier article</a>, the C# compiler compiles this to an IL class, making it even more obvious that objects and closures are two sides of the same coin. </p> <h3 id="73c4fa8bdc3240608d80bb0817af80a2"> Summary <a href="#73c4fa8bdc3240608d80bb0817af80a2" title="permalink">#</a> </h3> <p> Object-oriented instance methods are isomorphic to both static methods and function values. The translations that transforms your code from one to the other are <em>refactorings</em>. Since you can move in both directions without loss of information, these refactorings constitute an isomorphism. </p> <p> This is another important result about the relationship between object-oriented design and functional programming, because this enables us to reduce any method to a canonical form, in the shape of a function. From a language like Haskell, we know a lot about the relationship between category theory and functional programming. With isomorphisms like the present, we can begin to extend that knowledge to object-oriented design. </p> <p> <strong>Next:</strong> <a href="/2018/01/29/argument-list-isomorphisms">Argument list isomorphisms</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9f8cc866b3ec420f838940bb55908c8b"> <div class="comment-author">Matt Heuer <a href="#9f8cc866b3ec420f838940bb55908c8b">#</a></div> <div class="comment-content"> <p> This Isomorphism applies to non-polymorphic Methods. Polymorphic Functions need to be mapped to a Set is static Methods with the same Signature. Is there a functional Construct for this? </p> </div> <div class="comment-date">2021-02-20 17:57 UTC</div> </div> <div class="comment" id="13a381feb0924780bcd4ee228616befd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#13a381feb0924780bcd4ee228616befd">#</a></div> <div class="comment-content"> <p> Matt, thank you for writing. What makes you write that this isomorphism applies (only, I take it) to non-polymorphic methods. The view here is on the implementation. In C# (and all other statically typed languages that I know that support functions), functions are polymorphic based on signature. </p> <p> A consumer that depends on a function with the type <code>Func&lt;Foo, Qux, Corge, Bar&gt;</code> can interact with any function with that type. </p> </div> <div class="comment-date">2021-02-21 17:19 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Unit isomorphisms https://blog.ploeh.dk/2018/01/15/unit-isomorphisms 2018-01-15T07:33:00+00:00 Mark Seemann <div id="post"> <p> <em>The C# and Java keyword 'void' is isomorphic to a data type called 'unit'.</em> </p> <p> This article is part of <a href="/2018/01/08/software-design-isomorphisms">a series of articles about software design isomorphisms</a>. </p> <p> Many programming languages, such as C# and Java, distinguish between methods that return something, and methods that don't return anything. In C# and Java, a method must be declared with a return type, but if it doesn't return anything, you can use the special keyword <code>void</code> to indicate that this is the case: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Foo(<span style="color:blue;">int</span>&nbsp;bar)</pre> </p> <p> This is a C# example, but it would look similar (isomorphic?) in Java. </p> <h3 id="ed0cfa4cffe640fb91d0ee6eb8513408"> Two kinds of methods <a href="#ed0cfa4cffe640fb91d0ee6eb8513408" title="permalink">#</a> </h3> <p> In C# and Java, <code>void</code> isn't a type, but a keyword. This means that there are two distinct types of methods: <ul> <li>Methods that return something</li> <li>Methods that return nothing</li> </ul> In C#, methods that return something declare their return type before the method name: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Bar(<span style="color:#2b91af;">Baz</span>&nbsp;baz,&nbsp;<span style="color:#2b91af;">Qux</span>&nbsp;qux)</pre> </p> <p> On the other hand, methods that return nothing must use the special <code>void</code> keyword: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Bar(<span style="color:#2b91af;">Baz</span>&nbsp;baz,&nbsp;<span style="color:#2b91af;">Qux</span>&nbsp;qux)</pre> </p> <p> If you want to generalise, you can use generics like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Foo&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y)</pre> </p> <p> Such a method could return <em>anything</em>, but, surprisingly, not <em>nothing</em>. In C# and Java, <em>nothing</em> is special. You can't generalise all methods to a common set. Even with generics, you must model methods that return nothing in a different way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Foo&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y)</pre> </p> <p> In C#, for example, this leads to the distinction between <a href="https://msdn.microsoft.com/en-us/library/bb549151">Func</a> and <a href="https://msdn.microsoft.com/en-us/library/018hxwa8">Action</a>. You can't reconcile these two fundamentally distinct types of methods into one. </p> <p> Visual Basic .NET makes the same distinction, but uses the keywords <code>Sub</code> and <code>Function</code> instead of <code>void</code>. </p> <p> Sometimes, particularly when writing code with generics, this dichotomy is really annoying. Wouldn't it be nice to be able to generalise all methods? </p> <h3 id="f2edc83a82bb40bbaf52c5e313b54770"> Unit <a href="#f2edc83a82bb40bbaf52c5e313b54770" title="permalink">#</a> </h3> <p> While I don't recall the source, I've read somewhere the suggestion that the keyword <code>void</code> was chosen to go with <code>null</code>, because <em>null and void</em> is an English (legal) idiom. That choice is, however, unfortunate. </p> <p> In <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, the term <em>void</em> denotes a type or set with <em>no</em> inhabitants (e.g. an empty set). That sounds like the same concept. The problem, from a programming perspective, is that if you have a (static) type with no inhabitants, you can't create an instance of it. See <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s article <a href="https://bartoszmilewski.com/2014/11/24/types-and-functions">Types and Functions</a> for a great explanation and more details. </p> <p> Functional programming languages like <a href="http://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a> instead model <em>nothing</em> by a type called <em>unit</em> (often rendered as empty brackets: <code>()</code>). This type is a type with exactly one inhabitant, a bit like a <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a>, but with the extra restriction that the inhabitant carries no information. It simply is. </p> <p> It may sound strange and counter-intuitive that a singleton value represents <em>nothing</em>, but it turns out that this is, indeed, isomorphic to C# or Java's <code>void</code>. </p> <p> This is admirably illustrated by F#, which consistently uses <code>unit</code> instead of <code>void</code>. F# is a multi-paradigmatic language, so you can write classes with methods as well as functions: </p> <p> <pre><span style="color:blue;">member</span>&nbsp;<span style="color:#9d9d9d;">this</span>.<span style="color:navy;">Bar</span>&nbsp;(<span style="color:#9d9d9d;">x</span>&nbsp;:&nbsp;<span style="color:teal;">int</span>)&nbsp;=&nbsp;()</pre> </p> <p> This <code>Bar</code> method has the return type <code>unit</code>. When you compile F# code, it becomes <a href="https://en.wikipedia.org/wiki/Common_Intermediate_Language">Intermediate Language</a>, which you can decompile into C#. If you do that, the above F# code becomes: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Bar(<span style="color:blue;">int</span>&nbsp;x)</pre> </p> <p> The inverse translation works as well. When you use F#'s interoperability features to interact with objects written in C# or Visual Basic, the F# compiler interprets <code>void</code> methods as if they return <code>unit</code>. For example, calling <a href="https://msdn.microsoft.com/en-us/library/y46kxc5e">GC.Collect</a> returns <code>unit</code> in F#, although C# sees it as 'returning' <code>void</code>: </p> <p> <pre>&gt; GC.Collect 0;; val it : unit = ()</pre> </p> <p> F#'s <code>unit</code> is isomorphic to C#'s <code>void</code> keyword, but apart from that, there's nothing special about it. It's a value like any other, and can be used in generically typed functions, like the built-in <code>id</code> function: </p> <p> <pre>&gt; id 42;; val it : int = 42 &gt; id "foo";; val it : string = "foo" &gt; id ();; val it : unit = ()</pre> </p> <p> The built-in function <code>id</code> simply returns its input argument. It has the type <code>'a -&gt; 'a</code>, and as the above F# Interactive session demonstrates, you can call it with <code>unit</code> as well as with <code>int</code>, <code>string</code>, and so on. </p> <h3 id="4657dbd4b5fc4abda6e8dee2cac67ea9"> Monoid <a href="#4657dbd4b5fc4abda6e8dee2cac67ea9" title="permalink">#</a> </h3> <p> Unit, by the way, forms a <a href="/2017/10/06/monoids">monoid</a>. This is most evident in Haskell, where this is encoded into the type. In general, a monoid is a binary operation, and not a type, but what could the combination of two <code>()</code> (unit) values be, other than <code>()</code>? </p> <p> <pre>λ&gt; mempty :: () () λ&gt; mappend () () ()</pre> </p> <p> In fact, the above (rhetorical) question is imprecise, since there aren't <em>two</em> unit values. There's only one, but used twice. </p> <p> Since only a single <em>unit</em> value exists, any binary operation is automatically associative, because, after all, it can only return <em>unit</em>. Likewise, <em>unit</em> is the identity (<code>mempty</code>) for the operation, because it doesn't change the output. Thus, the monoid laws hold, and <em>unit</em> forms a monoid. </p> <p> This result is interesting when you start to think about composition, because a monoid can always be used to reduce (aggregate) multiple values to a single value. With this result, and unit isomorphism, we've already explained why <a href="/2011/03/22/CommandsareComposable">Commands are composable</a>. </p> <h3 id="8677103c217347c6b40cec336a581aff"> Summary <a href="#8677103c217347c6b40cec336a581aff" title="permalink">#</a> </h3> <p> Since <em>unit</em> is a type only inhabited by a single value, people (including me) often use the word <em>unit</em> about both the type and its only value. Normally, the context surrounding such use is enough to dispel any ambiguity. </p> <p> <em>Unit</em> is isomorphic to C# or Java <code>void</code>. This is an important result, because if we're to study software design and code structure, we don't have to deal with two distinct cases (methods that return nothing, and methods that return something). Instead, we can ignore methods that return nothing, because they can instead be modelled as methods that return <em>unit</em>. </p> <p> The reason I've titled this article in the plural is that you could view the isomorphism between F# <code>unit</code> and C# <code>void</code> as a different isomorphism than the one between C# and Java <code>void</code>. Add Haskell's <code>()</code> (unit) type and Visual Basic's <code>Sub</code> keyword to the mix, and it should be clear that there are many translations to the category theory concept of <em>Unit</em>. </p> <p> <img src="/content/binary/unit-isomorphisms.png" alt="Isormorphisms between the Unit concept and constructs in selected languages: C#, Java, Visual Basic, F#, Haskell."> </p> <p> Unit isomorphism is an example of an interlingual isomorphism, in the sense that C# <code>void</code> maps to F# <code>unit</code>, and vice versa. In the next example, you'll see an isomorphism that mostly stays within a single language, although a translation between languages is also pertinent. </p> <p> <strong>Next:</strong> <a href="/2018/01/22/function-isomorphisms">Function isomorphisms</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Software design isomorphisms https://blog.ploeh.dk/2018/01/08/software-design-isomorphisms 2018-01-08T08:35:00+00:00 Mark Seemann <div id="post"> <p> <em>When programming, you can often express the same concept in multiple ways. If you can losslessly translate between two alternatives, it's an isomorphism. An introduction for object-oriented programmers.</em> </p> <p> This article series is part of <a href="/2017/10/04/from-design-patterns-to-category-theory">an even larger series of articles about the relationship between design patterns and category theory.</a> </p> <p> There's a school of functional programming that looks to <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a> for inspiration, verification, abstraction, and cross-pollination of ideas. Perhaps you're put off by terms like <a href="https://twitter.com/jamesiry/status/598547781515485184">zygohistomorphic prepromorphism</a> (a joke), but you shouldn't be. There are often <a href="/2015/08/17/when-x-y-and-z-are-great-variable-names">good reasons for using abstract naming</a>. In any case, one term from category theory that occasionally makes the rounds is <em>isomorphism</em>. </p> <h3 id="656b60e2fac7490f833dd2fde9709173"> Equivalence <a href="#656b60e2fac7490f833dd2fde9709173" title="permalink">#</a> </h3> <p> Don't let the terminology daunt you. An <em>isomorphism</em> is an easy enough concept to grasp. In essence, two things are isomorphic if you can translate losslessly back and forth between them. It's a formalisation of <em>equivalence</em>. </p> <p> Many programming languages, like C# and Java, offer a multitude of alternative ways to do things. Just consider this C# example from <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">my Humane Code video</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsSatisfiedBy(<span style="color:#2b91af;">Customer</span>&nbsp;candidate) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;retVal; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(candidate.TotalPurchases&nbsp;&gt;=&nbsp;10000) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retVal&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retVal&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;retVal; }</pre> </p> <p> which is equivalent to this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsSatisfiedBy(<span style="color:#2b91af;">Customer</span>&nbsp;candidate) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;candidate.TotalPurchases&nbsp;&gt;=&nbsp;10000; }</pre> </p> <p> An outside observer can't tell the difference between these two implementations, because they have exactly the same externally visible behaviour. You can always refactor from one implementation to the other without loss of information. Thus, we could claim that they're isomorphic. </p> <h3 id="44e9ca73a4374c188991f292d6c59f0a"> Terminology <a href="#44e9ca73a4374c188991f292d6c59f0a" title="permalink">#</a> </h3> <p> If you're an object-oriented programmer, then you already know the word <a href="https://en.wikipedia.org/wiki/Polymorphism_(computer_science)">polymorphism</a>, which sounds similar to <em>isomorphism</em>. Perhaps you've also heard the word <a href="https://en.wikipedia.org/wiki/Xenomorph">xenomorph</a>. It's all Greek. <em>Morph</em> means <em>form</em> or <em>shape</em>, and while <em>poly</em> means <em>many</em>, <em>iso</em> means <em>equal</em>. So <em>isomorphism</em> means 'being of equal shape'. </p> <p> This is most likely the explanation for the term <a href="https://en.wikipedia.org/wiki/Isomorphic_JavaScript">Isomorphic JavaScript</a>. The people who came up with that term knew (enough) Greek, but apparently not mathematics. In mathematics, and particularly category theory, an isomorphism is a translation with an inverse. That's still not a formal definition, but just my attempt at presenting it without too much jargon. </p> <p> Category theory uses the word <em>object</em> to describe a member of a category. I'm going to use that terminology here as well, but you should be aware that <em>object</em> doesn't imply object-oriented programming. It just means 'thing', 'item', 'element', 'entity', etcetera. </p> <p> In category theory, a <em>morphism</em> is a mapping or translation of an object to another object. If, for all objects, there's an inverse morphism that leads back to the origin, then it's an isomorphism. </p> <p> <img src="/content/binary/isomorphism.png" alt="Isomorphism diagram."> </p> <p> In this illustration, the blue arrows going from left to right indicate a single morphism. It's a mapping of objects on the blue left side to objects on the green right side. The green arrows going from right to left is another morphism. In this case, the green right-to-left morphism is an inverse of the blue left-to-right morphism, because by applying both morphisms, you end where you started. It doesn't matter if you start at the blue left side or the green right side. </p> <p> Another way to view this is to say that a lossless translation exists. When a translation is lossless, it means that you don't lose information by performing the translation. Since all information is still present after a translation, you can go back to the original representation. </p> <h3 id="ccf56c166177427785d71baf7cb7158f"> Software design isomorphisms <a href="#ccf56c166177427785d71baf7cb7158f" title="permalink">#</a> </h3> <p> When programming, you can often solve the same problem in different ways. Sometimes, the alternatives are isomorphic: you can go back and forth between two alternatives without loss of information. </p> <p> <a href="https://martinfowler.com">Martin Fowler</a>'s book <a href="http://amzn.to/YPdQDf">Refactoring</a> contains several examples. For instance, you can apply <em>Extract Method</em> followed by <em>Inline Method</em> and be back where you started. </p> <p> There are many other isomorphisms in programming. Some are morphisms in the same language, as is the case with the above C# example. This is also the case with the isomorphisms in <em>Refactoring</em>, because a refactoring, by definition, is a change applied to a particular code base, be it C#, Java, Ruby, or Python. </p> <p> Other programming isomorphisms go between languages, where a concept can be modelled in one way in, say, C++, but in another way in Clojure. The <a href="">present blog</a>, for instance, contains several examples of translating between C# and <a href="http://fsharp.org">F#</a>, and between F# and <a href="https://www.haskell.org">Haskell</a>. </p> <p> Being aware of software design isomorphisms can make you a better programmer. It'll enable you to select the best alternative for solving a particular problem. Identifying programming isomorphisms is also important because it'll enable us to formally think about code structure by reducing many alternative representations to a canonical representation. For these reasons, this article presents a catalogue of software design isomorphisms: <ul> <li><a href="/2018/01/15/unit-isomorphisms">Unit isomorphisms</a></li> <li><a href="/2018/01/22/function-isomorphisms">Function isomorphisms</a></li> <li><a href="/2018/01/29/argument-list-isomorphisms">Argument list isomorphisms</a></li> <li><a href="/2018/02/05/uncurry-isomorphisms">Uncurry isomorphisms</a></li> <li><a href="/2018/02/12/object-isomorphisms">Object isomorphisms</a></li> <li><a href="/2018/02/19/abstract-class-isomorphism">Abstract class isomorphism</a></li> <li><a href="/2018/02/26/inheritance-composition-isomorphism">Inheritance-composition isomorphism</a></li> <li><a href="/2019/07/15/tester-doer-isomorphisms">Tester-Doer isomorphisms</a></li> <li><a href="/2020/02/10/builder-isomorphisms">Builder isomorphisms</a></li> </ul> In general, I've tried to name each isomorphism after its canonical representation. For instance, by <em>unit isomorphisms</em>, I mean isomorphisms to the unit value. It is, however, not an entirely consistent naming strategy. </p> <p> Many more software design isomorphisms exist, so if you revisit this article in the future, I may have added more items to this catalogue. In no way should you consider this catalogue exhaustive. </p> <h3 id="d23d37f6eea548c7b7873b19e7d79fe4"> Summary <a href="#d23d37f6eea548c7b7873b19e7d79fe4" title="permalink">#</a> </h3> <p> An isomorphism is a mapping for which an inverse mapping also exists. It's a way to describe equivalence. </p> <p> In programming, you often have the choice to implement a particular feature in more than one way. These alternatives may be equivalent, in which case they're isomorphic. That's one of the reasons that many code bases come with a style guide. </p> <p> Understanding how code is isomorphic to other code enables us to reduce many alternatives to a canonical representation. This makes analysis easier, because we can narrow our analysis to the canonical form, and generalise from there. </p> <p> <strong>Next:</strong> <a href="/2018/01/15/unit-isomorphisms">Unit isomorphisms</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b8af8786402b4fcab0ffffa883304ba2"> <div class="comment-author"><a href="https://github.com/srogovtsev">Sergey Rogovtsev</a> <a href="#b8af8786402b4fcab0ffffa883304ba2">#</a></div> <div class="comment-content"> Funny thing, I had a discussion about refactoring as an isomorphism a bit ago. While I like the idea of using isomorphisms to reason about code, I still stand by the point that refactoring is an isomorphism <em>limited to functionality</em>; i.e. refactoring the code may change its other aspects (readability is the first one to come to mind, performance is the second), so two different representations are no longer totally equal. Or, as another argument, using Inline Method in fact loses some information (the descriptive method name, limited variable scopes), so the translation is not (sorry) lossless. </div> <div class="comment-date">2018-01-13 08:39 UTC</div> </div> <div class="comment" id="d32e657ba46c48c4a5df7664d24b5321"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d32e657ba46c48c4a5df7664d24b5321">#</a></div> <div class="comment-content"> <p> Sergey, thank you for writing. Good point, you're right that viewed as a general-purpose translation, <em>Inline Method</em> is indeed lossy. When you look at the purpose of refactoring code, the motivation is mostly (if not always) to make the code better in some way. Particularly when the purpose is make the code more readable, a refactoring introduces clarity. Thus, going the opposite way would remove that clarity, so I think it's fair to argue that such a change would be lossy. </p> <p> It's perfectly reasonable to view a refactoring like <em>Inline Method</em> as a general-purpose algorithm, in which case you're right that it's lossy. I don't dispute that. </p> <p> My agenda with this article series, however, is different. I'm not trying to make multiple angels dance on a pinhead; it's not my intent to try to redefine the word <em>refactoring</em>. The purpose with this series of articles is to describe how the same behaviour can be implemented in many different ways. The book <em>Refactoring</em> is one source of such equivalent representations. </p> <p> One quality of morphisms is that there can be several translations between two objects. One such translation could be the general-purpose refactoring that you so rightly point out is lossy. Another translation could be one that 'remembers' the name of the original method. </p> <p> Take, as an example, the isomorphism described under the heading <em>Roster isomorphism</em> in my article <a href="/2017/10/30/tuple-monoids">Tuple monoids</a>. When you consider the method <code>ToTriple</code>, you could, indeed, argue that it's lossy, because it 'forgets' that the label associated with the first integer is <em>Girls</em>, that the label associated with the second integer is <em>Boys</em>, and so on. The reverse translation, however, 'remembers' that information, as you can see in the implementation of <code>FromTriple</code>. </p> <p> This isn't necessarily a 'safe' translation. You could easily write a method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;FromTriple(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>[]&gt;&nbsp;triple) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>(triple.Item2,&nbsp;triple.Item1,&nbsp;triple.Item3); }</pre> </p> <p> This compiles, but it translates triples created by <code>ToTriple</code> the wrong way. </p> <p> On the other hand, the pair of translations that I do show in the article <em>is</em> an isomorphism. The point is that an isomorphism exists; not that it's the only possible set of morphisms. </p> <p> The same argument can be applied to specific pairs of <em>Extract Method</em> and <em>Inline Method</em>. As a general-purpose algorithm, I still agree that <em>Inline Method</em> is lossy. That doesn't preclude that specific pairs of translations exist. For instance, in <a href="/2014/08/07/why-dry">an article</a>, I discuss how some people refactor Guard Clauses like this: </p> <p> <pre><span style="color:blue;">if</span>&nbsp;(subject&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject&quot;</span>);</pre> </p> <p> to something like this: </p> <p> <pre><span style="color:#2b91af;">Guard</span>.AgainstNull(subject,&nbsp;<span style="color:#a31515;">&quot;subject&quot;</span>);</pre> </p> <p> Again, an isomorphism exists: a translation from a Null Guard to <code>Guard.AgainstNull</code>, and another from <code>Guard.AgainstNull</code> to a Null Guard. Those are specific incarnations of <em>Extract Method</em> and <em>Inline Method</em>. </p> <p> This may not be particularly useful as a refactoring, I admit, but that's also not the agenda of these articles. The programme is to show how particular software behaviour can be expressed in various different ways that are equivalent to each other. </p> </div> <div class="comment-date">2018-01-14 14:26 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Colour-mixing magma https://blog.ploeh.dk/2018/01/02/colour-mixing-magma 2018-01-02T08:36:00+00:00 Mark Seemann <div id="post"> <p> <em>Mixing RGB colours forms a magma. An example for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2017/10/05/monoids-semigroups-and-friends">a larger series about monoids, semigroups, and other group-like algebraic structures</a>. In this article, you'll see an example of a <a href="/2017/12/27/magmas">magma</a>, which is a binary operation without additional constraints. </p> <h3 id="9970a480d34249568b228407c5f1a506"> RGB colours <a href="#9970a480d34249568b228407c5f1a506" title="permalink">#</a> </h3> <p> The opening article about monoids, semigroups, and their friends emphasised Eric Evans' pigment mixing example from <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>. The following article series then promptly proceeded to ignore that example. The reason is that while the example has <em>Closure of Operations</em>, it exhibits precious few other properties. It's neither <a href="/2017/10/06/monoids">monoid</a>, <a href="/2017/11/27/semigroups">semigroup</a>, <a href="/2017/12/18/quasigroups">quasigroup</a>, nor any other named binary operation, apart from being a magma. </p> <p> Instead of <em>pigments</em>, consider a more primitive, but well-understood colour model: that of <a href="https://en.wikipedia.org/wiki/RGB_color_model">RGB colours</a>. In C#, you can model RGB colours using a <code>struct</code> that holds three <code>byte</code> fields. In my final code base, I ended up implementing <code>==</code>, <code>!=</code>, <code>Equals</code>, and so on, but I'm not going to bore you with all of those details. Here's the <code>RgbColor</code> constructor, so that you can get a sense of the type: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;red; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;green; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">byte</span>&nbsp;blue; <span style="color:blue;">public</span>&nbsp;RgbColor(<span style="color:blue;">byte</span>&nbsp;red,&nbsp;<span style="color:blue;">byte</span>&nbsp;green,&nbsp;<span style="color:blue;">byte</span>&nbsp;blue) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.red&nbsp;=&nbsp;red; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.green&nbsp;=&nbsp;green; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.blue&nbsp;=&nbsp;blue; }</pre> </p> <p> As you can see, <code>RgbColor</code> holds three <code>byte</code> fields, one for red, green, and blue. If you want to mix two colours, you can use the <code>MixWith</code> instance method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>&nbsp;MixWith(<span style="color:#2b91af;">RgbColor</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newRed&nbsp;=&nbsp;((<span style="color:blue;">int</span>)<span style="color:blue;">this</span>.red&nbsp;+&nbsp;(<span style="color:blue;">int</span>)other.red)&nbsp;/&nbsp;2m; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newGreen&nbsp;=&nbsp;((<span style="color:blue;">int</span>)<span style="color:blue;">this</span>.green&nbsp;+&nbsp;(<span style="color:blue;">int</span>)other.green)&nbsp;/&nbsp;2m; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newBlue&nbsp;=&nbsp;((<span style="color:blue;">int</span>)<span style="color:blue;">this</span>.blue&nbsp;+&nbsp;(<span style="color:blue;">int</span>)other.blue)&nbsp;/&nbsp;2m; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">byte</span>)<span style="color:#2b91af;">Math</span>.Round(newRed), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">byte</span>)<span style="color:#2b91af;">Math</span>.Round(newGreen), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">byte</span>)<span style="color:#2b91af;">Math</span>.Round(newBlue)); }</pre> </p> <p> <a href="/2017/10/06/monoids">This is a binary operation</a>, because it's an instance method on <code>RgbColor</code>, taking another <code>RgbColor</code> as input, and returning <code>RgbColor</code>. Since it's a binary operation, it's a magma, but could it be another, stricter category of operation? </p> <h3 id="ace361b86ee249599ede4d43098e6329"> Lack of associativity <a href="#ace361b86ee249599ede4d43098e6329" title="permalink">#</a> </h3> <p> Could <code>MixWith</code>, for instance, be a semigroup? In order to be a semigroup, the binary operation must be associative, and while it can be demanding to prove that an operation is <em>always</em> associative, it only takes a single counter-example to prove that it's not: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;MixWithIsNotAssociative() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Counter-example</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>(&nbsp;67,&nbsp;108,&nbsp;&nbsp;13); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>(&nbsp;33,&nbsp;114,&nbsp;130); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;z&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>(&nbsp;38,&nbsp;104,&nbsp;245); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.NotEqual( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.MixWith(y).MixWith(z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.MixWith(y.MixWith(z))); }</pre> </p> <p> This <a href="https://xunit.net">xUnit.net</a> unit test passes, thereby demonstrating that <code>MixWith</code> is <em>not</em> associative. When you mix <code>x</code> with <code>y</code>, you get <span style="color:#326F48">#326F48</span>, and when you mix that with <code>z</code> you get <span style="color:#2C6C9E">#2C6C9E</span>. On the other hand, when you mix <code>y</code> with <code>z</code> you get <span style="color:#246DBC">#246DBC</span>, which, combined with <code>x</code>, gives <span style="color:#346C64">#346C64</span>. <span style="color:#2C6C9E">#2C6C9E</span> is not equal to <span style="color:#346C64">#346C64</span>, so the <code>NotEqual</code> assertion passes. </p> <p> Because of this counter-example, <code>MixWith</code> isn't associative, and therefore not a semigroup. Since monoid requires associativity as well, we can also rule out that <code>MixWith</code> is a monoid. </p> <h3 id="22eeac6a82124dcf9b39da28134322bc"> Lack of invertibility <a href="#22eeac6a82124dcf9b39da28134322bc" title="permalink">#</a> </h3> <p> While <code>MixWith</code> isn't a semigroup, could it be a quasigroup? In order to be a quasigroup, a binary operation must be invertible. This means that for <em>any</em> two elements <code>a</code> and <code>b</code>, there must exist two other elements <code>x</code> and <code>y</code> that turns <code>a</code> into <code>b</code>. </p> <p> This property must hold for all values involved in the binary operation, so again, a single counter-example suffices to demonstrate that <code>MixWith</code> isn't invertible, either: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;MixWithIsNotInvertible() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Counter-example</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;a&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>(&nbsp;94,&nbsp;&nbsp;35,&nbsp;172); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;b&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>(151,&nbsp;185,&nbsp;&nbsp;&nbsp;7); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:#2b91af;">RgbColor</span>.All.Any(x&nbsp;=&gt;&nbsp;a.MixWith(x)&nbsp;==&nbsp;b)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:#2b91af;">RgbColor</span>.All.Any(y&nbsp;=&gt;&nbsp;y.MixWith(a)&nbsp;==&nbsp;b)); }</pre> </p> <p> This xUnit.net-based test also passes. It uses brute force to demonstrate that for all <code>RgbColor</code> values, there's no <code>x</code> and <code>y</code> that satisfy the invertibility property. The test actually takes a while to execute, because <code>All</code> returns all 16,777,216 possible <code>RgbColor</code> values: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>[]&nbsp;all; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">object</span>&nbsp;syncLock&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">object</span>(); <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">RgbColor</span>&gt;&nbsp;All { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(all&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">lock</span>&nbsp;(syncLock) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(all&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;max&nbsp;=&nbsp;256&nbsp;*&nbsp;256&nbsp;*&nbsp;256; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>[max]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Enumerable</span>.Range(0,&nbsp;max)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all[i]&nbsp;=&nbsp;(<span style="color:#2b91af;">RgbColor</span>)i; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;all; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> For performance reasons, the <code>All</code> property uses lazy initialisation with <a href="https://en.wikipedia.org/wiki/Double-checked_locking">double-checked locking</a>. It simply counts from <code>0</code> to <code>256 * 256 * 256</code> (16,777,216) and converts each integer to an <code>RgbColor</code> value using this explicit conversion: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">explicit</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>(<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;red&nbsp;=&nbsp;(i&nbsp;&amp;&nbsp;0xFF0000)&nbsp;/&nbsp;0x10000; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;green&nbsp;=&nbsp;(i&nbsp;&amp;&nbsp;0xFF00)&nbsp;/&nbsp;0x100; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;blue&nbsp;=&nbsp;i&nbsp;&amp;&nbsp;0xFF; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>((<span style="color:blue;">byte</span>)red,&nbsp;(<span style="color:blue;">byte</span>)green,&nbsp;(<span style="color:blue;">byte</span>)blue); }</pre> </p> <p> The bottom line, though, is that the test passes, thereby demonstrating that for the chosen counter-example, no <code>x</code> and <code>y</code> satisfies the invertibility property. Therefore, <code>MixWith</code> isn't a quasigroup. </p> <h3 id="fe4afba417e64d38b454d409e8ce4098"> Lack of identity <a href="#fe4afba417e64d38b454d409e8ce4098" title="permalink">#</a> </h3> <p> Since <code>MixWith</code> is neither associative nor invertible, it's not really any named algebraic construct, other than a magma. It's neither group, semigroup, quasigroup, monoid, loop, groupoid, etc. Does it have <em>any</em> properties at all, apart from being a binary operation? </p> <p> It doesn't have identity either, which you can illustrate with another counter-example: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;MixWithHasNoIdentity() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;nearBlack&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>(1,&nbsp;1,&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;identityCandidates&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;e&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>.All &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;nearBlack.MixWith(e)&nbsp;==&nbsp;nearBlack &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;e; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Verify&nbsp;that&nbsp;there&#39;s&nbsp;only&nbsp;a&nbsp;single&nbsp;candidate:</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;identityCandidate&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.Single(identityCandidates); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Demonstrate&nbsp;that&nbsp;the&nbsp;candidate&nbsp;does&nbsp;behave&nbsp;like&nbsp;identity&nbsp;for</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;nearBlack:</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(nearBlack,&nbsp;nearBlack.MixWith(identityCandidate)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(nearBlack,&nbsp;identityCandidate.MixWith(nearBlack)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Counter-example</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;counterExample&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RgbColor</span>(3,&nbsp;3,&nbsp;3); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.NotEqual( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counterExample,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counterExample.MixWith(identityCandidate)); }</pre> </p> <p> The counter-example starts with a near-black colour. The reason I didn't pick absolute black (<code>new RgbColor(0, 0, 0)</code>) is that, due to rounding when mixing, there are eight candidates for absolute black, but only one for <code>nearBlack</code>. This is demonstrated by the <code>Assert.Single</code> assertion. <code>identityCandidate</code>, by the way, is also <code>new RgbColor(1, 1, 1)</code>, and further <a href="http://xunitpatterns.com/Guard%20Assertion.html">Guard Assertions</a> demonstrate that <code>identityCandidate</code> behaves like the identity for <code>nearBlack</code>. </p> <p> You can now pick another colour, such as <code>new RgbColor(3, 3, 3)</code> and demonstrate that <code>identityCandidate</code> does <em>not</em> behave like the identity for the counter-example. Notice that the assertion is <code>Assert.NotEqual</code>. </p> <p> If an identity exists for a magma, it must behave as the identity for all possible values. That's demonstrably not the case for <code>MixWith</code>, so it doesn't have identity. </p> <h3 id="a8d8c29298fc4ee0b2be40d6ec94c609"> Commutativity <a href="#a8d8c29298fc4ee0b2be40d6ec94c609" title="permalink">#</a> </h3> <p> While <code>MixWith</code> is neither associative, invertible, nor has identity, it does have at least one property: it's commutative. This means that the order of the input values doesn't matter. In other words, for any two <code>RgbColor</code> values <code>x</code> and <code>y</code>, this assertion always passes: </p> <p> <pre><span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;x.MixWith(y), &nbsp;&nbsp;&nbsp;&nbsp;y.MixWith(x));</pre> </p> <p> Since <code>x.MixWith(y)</code> is equal to <code>y.MixWith(x)</code>, <code>MixWith</code> is commutative. </p> <h3 id="87d46ebbea434bd1bfb77ea49399feb3"> Summary <a href="#87d46ebbea434bd1bfb77ea49399feb3" title="permalink">#</a> </h3> <p> The <code>MixWith</code> operation is a commutative magma, but while, for example, we call an associative magma a <em>semigroup</em>, there's no fancy word for a commutative magma. </p> <p> In this article, you got another, fairly realistic, example of a binary operation. Throughout the overall article series on monoids, semigroup, and other group-like algebraic structures, you've seen many examples, and you've learned how to analyse binary operations for the presence or absence of various properties. The present article concludes the series. You can, however, continue reading the <a href="/2017/10/04/from-design-patterns-to-category-theory">even more overall article series</a>. </p> <p> <strong>Next:</strong> <a href="/2018/03/19/functors-applicatives-and-friends">Functors, applicatives, and friends</a> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1aeef4b398384f2b928585600eee6ca0"> <div class="comment-author"><a href="http://linkedin.com/in/markwiemer">Mark Wiemer</a> <a href="#1aeef4b398384f2b928585600eee6ca0">#</a></div> <div class="comment-content"> <p> At first, the lack of associativity felt counterintuitive: If I take equals parts of three colors, it shouldn't matter in which order I mix them. Then I realized this function doesn't take equal parts of all three. Basically the first mixture mixes one unit of each of two colors, resulting in two units of mixture. Then the second mixture takes <strong>one</strong> unit of the first mixture and one unit of the third color. That's why it's not associative! </p> </div> <div class="comment-date">2022-04-20 07:30 UTC</div> </div> <div class="comment" id="11459c2a1d10452aaa0e4fcc82934360"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#11459c2a1d10452aaa0e4fcc82934360">#</a></div> <div class="comment-content"> <p> Mark, thank you for writing. I never gave that question that much attention when I wrote the article, but that makes total sense. Thank you for explaining it. </p> </div> <div class="comment-date">2022-04-20 07:56 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Rock Paper Scissors magma https://blog.ploeh.dk/2017/12/28/rock-paper-scissors-magma 2017-12-28T11:22:00+00:00 Mark Seemann <div id="post"> <p> <em>The Rock Paper Scissors game forms a magma. An example for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2017/10/05/monoids-semigroups-and-friends">a larger series about monoids, semigroups, and other group-like algebraic structures</a>. In this article, you'll see an example of a <a href="/2017/12/27/magmas">magma</a>, which is a binary operation without additional constraints. </p> <h3 id="74a24da2f36d413a9882eb9ecd648b05"> Rock Paper Scissors <a href="#74a24da2f36d413a9882eb9ecd648b05" title="permalink">#</a> </h3> <p> When my first child was born, my wife and I quickly settled on a first name, but we couldn't agree on the surname, since we don't share the same surname. She wanted our daughter to have her surname, and I wanted her to have mine. We couldn't think of any rational arguments for one or the other, so we decided to settle the matter with a game of <a href="https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors">Rock Paper Scissors</a>. I lost, so my daughter has my wife's surname. </p> <p> Despite that outcome, I still find that Rock Paper Scissors is a great way to pick between two equally valid alternatives. You could also flip a coin, or throw a die, but most people have their hands handy, so to speak. </p> <p> In C#, you can model the three shapes of rock, paper, and scissors like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Rps</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Rps</span>&nbsp;Rock&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">R</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Rps</span>&nbsp;Paper&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">P</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Rps</span>&nbsp;Scissors&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">S</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Rps()&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">R</span>&nbsp;:&nbsp;<span style="color:#2b91af;">Rps</span>&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">P</span>&nbsp;:&nbsp;<span style="color:#2b91af;">Rps</span>&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">S</span>&nbsp;:&nbsp;<span style="color:#2b91af;">Rps</span>&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members...</span> }</pre> </p> <p> I've seen more than one example where people use an <code>enum</code> to model the three shapes, but I believe that this is wrong, because <code>enum</code>s have an order to them, including a maximum and a minimum value (by default, <code>enum</code> is implemented with a 32-bit integer). That's not how Rock Paper Scissors work, so instead, I chose a different model where <code>Rock</code>, <code>Paper</code>, and <code>Scissors</code> are <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singletons</a>. </p> <p> This design works for the example, although I'm not entirely happy with it. The problem is that Rock, Paper, and Scissors should be a finite set, but by making <code>Rps</code> abstract, another developer could, conceivably, create additional derived classes. A finite <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type</a> would have been better, but this isn't easily done in C#. In a language with <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>, you can make a prettier implementation, like <a href="https://math.stackexchange.com/a/2342043/250481">this Haskell example</a>. F# would be another good language option. </p> <h3 id="466fc4446f774405ba31f8659915c577"> Binary operation <a href="#466fc4446f774405ba31f8659915c577" title="permalink">#</a> </h3> <p> When playing the game of Rock Paper Scissors, each round is a <em>throw</em> that compares two shapes. You can model a throw as a binary operation that returns the winning shape: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Rps</span>&nbsp;Throw(<span style="color:#2b91af;">Rps</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">Rps</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x&nbsp;==&nbsp;Rock&nbsp;&amp;&amp;&nbsp;y&nbsp;==&nbsp;Rock) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Rock; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x&nbsp;==&nbsp;Rock&nbsp;&amp;&amp;&nbsp;y&nbsp;==&nbsp;Paper) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Paper; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x&nbsp;==&nbsp;Rock&nbsp;&amp;&amp;&nbsp;y&nbsp;==&nbsp;Scissors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Rock; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x&nbsp;==&nbsp;Paper&nbsp;&amp;&amp;&nbsp;y&nbsp;==&nbsp;Paper) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Paper; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x&nbsp;==&nbsp;Paper&nbsp;&amp;&amp;&nbsp;y&nbsp;==&nbsp;Scissors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Scissors; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x&nbsp;==&nbsp;Paper&nbsp;&amp;&amp;&nbsp;y&nbsp;==&nbsp;Rock) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Paper; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x&nbsp;==&nbsp;Scissors&nbsp;&amp;&amp;&nbsp;y&nbsp;==&nbsp;Scissors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Scissors; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(x&nbsp;==&nbsp;Scissors&nbsp;&amp;&amp;&nbsp;y&nbsp;==&nbsp;Rock) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Rock; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Scissors; }</pre> </p> <p> To a C# programmer, perhaps the method name <code>Throw</code> is bewildering, because you might expect the method to throw an exception, but I chose to use the domain language of the game. </p> <p> Because this method takes two <code>Rps</code> values as input and returns an <code>Rps</code> value as output, <a href="/2017/10/06/monoids">it's a binary operation</a>. Thus, you already know it's a magma, but could it, also, be another, stricter binary operations, such as a <a href="/2017/11/27/semigroups">semigroup</a> or <a href="/2017/12/18/quasigroups">quasigroup</a>? </p> <h3 id="04e968f2575d419cb519f72a7985aafd"> Lack of associativity <a href="#04e968f2575d419cb519f72a7985aafd" title="permalink">#</a> </h3> <p> In order to be a semigroup, the binary operation must be associative. You can easily demonstrate that <code>Throw</code> isn't associative by use of a counter-example: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ThrowIsNotAssociative() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Counter-example</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.NotEqual( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(<span style="color:#2b91af;">Rps</span>.Throw(<span style="color:#2b91af;">Rps</span>.Paper,&nbsp;<span style="color:#2b91af;">Rps</span>.Rock),&nbsp;<span style="color:#2b91af;">Rps</span>.Scissors), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(<span style="color:#2b91af;">Rps</span>.Paper,&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(<span style="color:#2b91af;">Rps</span>.Rock,&nbsp;<span style="color:#2b91af;">Rps</span>.Scissors))); }</pre> </p> <p> This <a href="https://xunit.net">xUnit.net</a> unit test passes, thereby demonstrating that <code>Throw</code> is <em>not</em> associative. The result of paper versus rock is paper, which, pitted against scissors yields scissors. On the other hand, paper versus the result of rock versus scissors is paper, because rock versus scissors is rock, and rock versus paper is paper. </p> <p> Since <code>Throw</code> isn't associative, it's not a semigroup (and, by extension, not a <a href="/2017/10/06/monoids">monoid</a>). Could it be a quasigroup? </p> <h3 id="ee92870510dc4a449aec03048f2356ba"> Lack of invertibility <a href="#ee92870510dc4a449aec03048f2356ba" title="permalink">#</a> </h3> <p> A binary operation must be invertible in order to be a quasigroup. This means that for <em>any</em> two elements <code>a</code> and <code>b</code>, there must exist two other elements <code>x</code> and <code>y</code> that turns <code>a</code> into <code>b</code>. </p> <p> This property must hold for all values involved in the binary operation - in this case <code>Rock</code>, <code>Paper</code>, and <code>Scissors</code>. A single counter-example is enough to show that <code>Throw</code> is <em>not</em> invertible: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ThrowIsNotInvertible() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Counter-example</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;a&nbsp;=&nbsp;<span style="color:#2b91af;">Rps</span>.Rock; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;b&nbsp;=&nbsp;<span style="color:#2b91af;">Rps</span>.Scissors; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:#2b91af;">Rps</span>.All.Any(x&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(a,&nbsp;x)&nbsp;==&nbsp;b)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:#2b91af;">Rps</span>.All.Any(y&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(y,&nbsp;a)&nbsp;==&nbsp;b)); }</pre> </p> <p> This (passing) unit test utilises an <code>All</code> property on <code>Rps</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Rps</span>&gt;&nbsp;All { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;Rock,&nbsp;Paper,&nbsp;Scissors&nbsp;};&nbsp;} }</pre> </p> <p> For a counter-example, pick <code>Rock</code> as <code>a</code> and <code>Scissors</code> as <code>b</code>. There's no value in <code>All</code> that satisfies the invertibility property. Therefore, <code>Throw</code> is not a quasigroup, either. </p> <h3 id="c09067193472434cb292fd2cc97e4e09"> Lack of identity <a href="#c09067193472434cb292fd2cc97e4e09" title="permalink">#</a> </h3> <p> Since <code>Throw</code> is neither associative nor invertible, it's not really any named algebraic construct, other than a magma. It's neither group, semigroup, quasigroup, monoid, loop, groupoid, etc. Does it have <em>any</em> properties at all, apart from being a binary operation? </p> <p> It doesn't have identity either, which you can illustrate with another counter-example: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ThrowHasNoIdentity() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Find&nbsp;all&nbsp;identity&nbsp;candidates&nbsp;for&nbsp;Rock</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;rockIdentities&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;e&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Rps</span>.All &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(e,&nbsp;<span style="color:#2b91af;">Rps</span>.Rock)&nbsp;==&nbsp;<span style="color:#2b91af;">Rps</span>.Rock &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(<span style="color:#2b91af;">Rps</span>.Rock,&nbsp;e)&nbsp;==&nbsp;<span style="color:#2b91af;">Rps</span>.Rock &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;e; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Narrow&nbsp;for&nbsp;Paper</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;paperIdentities&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;e&nbsp;<span style="color:blue;">in</span>&nbsp;rockIdentities &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(e,&nbsp;<span style="color:#2b91af;">Rps</span>.Paper)&nbsp;==&nbsp;<span style="color:#2b91af;">Rps</span>.Paper &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(<span style="color:#2b91af;">Rps</span>.Paper,&nbsp;e)&nbsp;==&nbsp;<span style="color:#2b91af;">Rps</span>.Paper &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;e; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;None&nbsp;of&nbsp;those&nbsp;candidates&nbsp;are&nbsp;the&nbsp;identity&nbsp;for&nbsp;Scissors</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;scissorIdentities&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;e&nbsp;<span style="color:blue;">in</span>&nbsp;paperIdentities &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(e,&nbsp;<span style="color:#2b91af;">Rps</span>.Scissors)&nbsp;==&nbsp;<span style="color:#2b91af;">Rps</span>.Scissors &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(<span style="color:#2b91af;">Rps</span>.Scissors,&nbsp;e)&nbsp;==&nbsp;<span style="color:#2b91af;">Rps</span>.Scissors &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;e; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Empty(scissorIdentities); }</pre> </p> <p> First, you use <code>Rps.All</code> to find all the values that behave as an identity element for <code>Rps.Rock</code>. Recall that the identity is an element that doesn't change the input. In other words it's a value that when combined with <code>Rps.Rock</code> in <code>Throw</code> still returns <code>Rps.Rock</code>. There are two values that fulfil that property: <code>Rps.Rock</code> and <code>Rps.Scissors</code>. Those are the two values contained in <code>rockIdentities</code>. </p> <p> In order to be an identity, the value must behave as a neutral element for <em>all</em> possible values, so next, filter <code>rockIdentities</code> to find those elements that also behave as identities for <code>Rps.Paper</code>. Between <code>Rps.Rock</code> and <code>Rps.Scissors</code>, only <code>Rps.Rock</code> behaves like an identity for <code>Rps.Paper</code>, so <code>paperIdentities</code> is a collection containing only the single value <code>Rps.Rock</code>. </p> <p> Is <code>Rps.Rock</code> an identity for <code>Rps.Scissors</code>, then? It's not. <code>scissorIdentities</code> is empty. There's no element in <code>Rps.All</code> that acts an identity for all values in <code>Rps.All</code>. Therefore, by brute force, the test <code>ThrowHasNoIdentity</code> demonstrates (as it says on the tin) that throw has no identity. </p> <h3 id="b519901e883b4aea815fe682f6f04d9f"> Commutativity <a href="#b519901e883b4aea815fe682f6f04d9f" title="permalink">#</a> </h3> <p> While <code>Throw</code> is neither associative, invertible, nor has identity, it does have at least one property: it's commutative. This means that the order of the input values doesn't matter. In other words, for any two <code>Rps</code> values <code>x</code> and <code>y</code>, this assertion always passes: </p> <p> <pre><span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(x,&nbsp;y), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Rps</span>.Throw(y,&nbsp;x));</pre> </p> <p> Since <code>Rps.Throw(x, y)</code> is equal to <code>Rps.Throw(y, x)</code>, <code>Throw</code> is commutative. </p> <h3 id="3ff6230db4c8449c819f757a3c87a4ac"> Summary <a href="#3ff6230db4c8449c819f757a3c87a4ac" title="permalink">#</a> </h3> <p> The Rock Paper Scissors <code>Throw</code> operation is a commutative magma, but while, for example, we call an associative magma a <em>semigroup</em>, there's no fancy word for a commutative magma. </p> <p> <strong>Next:</strong> <a href="/2018/01/02/colour-mixing-magma">Colour-mixing magma</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Magmas https://blog.ploeh.dk/2017/12/27/magmas 2017-12-27T08:32:00+00:00 Mark Seemann <div id="post"> <p> <em>A binary operation with no constraints on its behaviour is called a magma. An introduction for object-oriented programmers.</em> </p> <p> In the <a href="/2017/10/05/monoids-semigroups-and-friends">overall article series about group-like algebraic structures</a>, you've so far seen examples of <a href="/2017/10/06/monoids">monoids</a>, <a href="/2017/11/27/semigroups">semigroups</a>, and <a href="/2017/12/18/quasigroups">quasigroups</a>. Common to all of these structures is that they are binary operations governed by at least one law. The laws are different for the different categories, but there <em>are</em> rules. </p> <p> What if you have a binary operation that follows none of those rules? </p> <p> <img src="/content/binary/magmas-quasigroups-semigroups-monoids.png" alt="Monoids are a subset of semigroups, which are subsets of magmas. Quasigroups are also a subset of magmas, but can overlap semigroups and monoids."> </p> <p> All binary operations are <a href="https://en.wikipedia.org/wiki/Magma_(algebra)">magmas</a>. If they have additional properties, we may call them <em>quasigroups</em>, or <em>monoids</em>, or some such, depending on the specific properties, but they're still all magmas. This is the most inclusive category. </p> <p> You've already seen examples of monoids and semigroups, but what about magma examples? In a sense, you've already seen those as well, because all the examples you've seen so far have also been magma examples. After all, since all monoids are magmas, all the monoid examples you've seen have also been magma examples. </p> <p> Still, it's not that hard to come up with some programming examples of magmas that aren't semi- or quasigroups. In the next articles, you'll see some examples. <ul> <li><a href="/2017/12/28/rock-paper-scissors-magma">Rock Paper Scissors magma</a></li> <li><a href="/2018/01/02/colour-mixing-magma">Colour-mixing magma</a></li> </ul> Particularly the second example is fairly realistic, which demonstrates that as programmers, we can benefit from having vocabulary that enables us to describe any binary operation that doesn't obey any particular laws. In fact, establishing a vocabulary has been my primary motivation for writing this article series. </p> <p> <strong>Next: </strong> <a href="/2017/12/28/rock-paper-scissors-magma">Rock Paper Scissors magma</a> </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Quasigroups https://blog.ploeh.dk/2017/12/18/quasigroups 2017-12-18T13:31:00+00:00 Mark Seemann <div id="post"> <p> <em>A brief introduction to quasigroups for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2017/10/05/monoids-semigroups-and-friends">a larger series about monoids, semigroups, and other group-like algebraic structures</a>. In this article, you'll get acquainted with the concept of a quasigroup. I don't think it plays that big of a role in software design, but it <em>is</em> a thing, and I thought that I'd cover it briefly with a well known-example. </p> <p> During all this talk of <a href="/2017/10/06/monoids">monoids</a> and <a href="/2017/11/27/semigroups">semigroups</a>, you've seen that normal arithmetic operations like addition and multiplication form monoids. Perhaps you've been wondering where subtraction fits in. </p> <p> Subtraction forms a quasigroup. </p> <p> What's a quasigroup? It's an invertible binary operation. </p> <h3 id="bfc345d0a2c34954a254e4de21253380"> Inversion <a href="#bfc345d0a2c34954a254e4de21253380" title="permalink">#</a> </h3> <p> What does it mean for a binary operation to be invertible? It means that for any two elements <code>a</code> and <code>b</code>, there must exist two other elements <code>x</code> and <code>y</code> that turns <code>a</code> into <code>b</code>. </p> <p> This is true for subtraction, as this <a href="https://fscheck.github.io/FsCheck">FsCheck</a>-based test demonstrates: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SubtractionIsInvertible(<span style="color:blue;">int</span>&nbsp;a,&nbsp;<span style="color:blue;">int</span>&nbsp;b) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;a&nbsp;-&nbsp;b; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;a&nbsp;+&nbsp;b; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(a&nbsp;-&nbsp;x&nbsp;==&nbsp;b); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(y&nbsp;-&nbsp;a&nbsp;==&nbsp;b); }</pre> </p> <p> This example uses the <a href="https://www.nuget.org/packages/FsCheck.Xunit">FsCheck.Xunit</a> glue library for <a href="https://xunit.net">xUnit.net</a>. Notice that although FsCheck is written in F#, you can also use it from C#. This test (as well as all other tests in this article) passes. </p> <p> For any <code>a</code> and <code>b</code> generated by FsCheck, we can calculate unique <code>x</code> and <code>y</code> that satisfy <code>a - x == b</code> and <code>y - a == b</code>. </p> <p> Subtraction isn't the only invertible binary operation. In fact, addition is also invertible: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AdditionIsInvertible(<span style="color:blue;">int</span>&nbsp;a,&nbsp;<span style="color:blue;">int</span>&nbsp;b) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;b&nbsp;-&nbsp;a; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;b&nbsp;-&nbsp;a; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(a&nbsp;+&nbsp;x&nbsp;==&nbsp;b); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(y&nbsp;+&nbsp;a&nbsp;==&nbsp;b); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;y); }</pre> </p> <p> Here I added a third assertion that demonstrates that for addition, the inversion is symmetric; <code>x</code> and <code>y</code> are equal. </p> <p> Not only is integer addition a monoid - it's also a quasigroup. In fact, it's a <a href="https://en.wikipedia.org/wiki/Group_(mathematics)">group</a>. Being associative or having identity doesn't preclude a binary operation from being a quasigroup, but these properties aren't required. </p> <h3 id="5b8c084b14f5432a89e26a34a7654e27"> No identity <a href="#5b8c084b14f5432a89e26a34a7654e27" title="permalink">#</a> </h3> <p> No identity element exists for integer subtraction. For instance, <em>3 - 0</em> is <em>3</em>, but <em>0 - 3</em> is <em>not 3</em>. Therefore, subtraction can't be a monoid. </p> <h3 id="c568fdbfa6de4c65a99a97bd82cba4e5"> No associativity <a href="#c568fdbfa6de4c65a99a97bd82cba4e5" title="permalink">#</a> </h3> <p> Likewise, subtraction is not an associative operation. You can easily convince yourself of that by coming up with a counter-example, such as <em>(3 - 2) - 1</em>, which is <em>0</em>, but different from <em>3 - (2 - 1)</em>, which is <em>2</em>. Therefore, it can't be a semigroup either. </p> <h3 id="697353c1095c446380f28ef029663238"> Summary <a href="#697353c1095c446380f28ef029663238" title="permalink">#</a> </h3> <p> A quasigroup is an invertible binary operation. Invertibility is the only <em>required</em> property of a quasigroup (apart from being a binary operation), but if it has other properties (like associativity), it's still a quasigroup. </p> <p> I haven't had much utility from thinking about software design in terms of quasigroups, but I wanted to include it in case you were wondering how subtraction fits into all of this. </p> <p> What if, however, you have a binary operation with <em>no other</em> properties? </p> <p> <strong>Next:</strong> <a href="/2017/12/27/magmas">Magmas</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="azvm7fpdzjtgzfbxo0pnf50nad0n0nk4"> <div class="comment-author">Onur Sahin <a href="#azvm7fpdzjtgzfbxo0pnf50nad0n0nk4">#</a></div> <div class="comment-content"> <p> Is division over real numbers also a quasigroup? I think so, for any number a and b we have x and y such that: <br> a / x = b <br> y / a = b <br> I think division over integers is not a quasigroup since for 5 and 10 there is no x such that 5 / x = 10 since 0.5 is not an integer. </p> </div> <div class="comment-date">2022-03-27 13:45 UTC</div> </div> <div class="comment" id="c5a8abbb257d40c6bd3d612b77d0b8af"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c5a8abbb257d40c6bd3d612b77d0b8af">#</a></div> <div class="comment-content"> <p> Onur, thank you for writing. In general, when pondering pure mathematics rather than their programming aspects, I'm no authority. You'd be better off researching somewhere else. <a href="https://en.wikipedia.org/wiki/Quasigroup">The Wikipedia article on quasigroups</a> isn't too bad for that purpose, I find. </p> <p> According to that article, division of non-zero rational or real numbers is an invertible binary operation. </p> <p> As far as I can tell (I'm not a mathematician) you're correct that integer division doesn't form a quasigroup. When, as you suggestion, you pick <em>a = 5</em> and <em>b = 10</em>, there's no integer <em>x</em> for which <em>5 / x = 10</em>. </p> </div> <div class="comment-date">2022-03-29 7:00 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Semigroups accumulate https://blog.ploeh.dk/2017/12/11/semigroups-accumulate 2017-12-11T08:28:00+00:00 Mark Seemann <div id="post"> <p> <em>You can accumulate an arbitrary, non-zero number of semigroup values to a single value. An article for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/11/27/semigroups">series about semigroups</a>. In short, a <em>semigroup</em> is an associative binary operation. </p> <p> As you've learned in <a href="/2017/11/20/monoids-accumulate">a previous article</a>, you can accumulate an arbitrary number of monoidal values to a single value. A corresponding property holds for semigroups. </p> <h3 id="bea7cebd9f2246f0a55ddcaaa708961d"> Monoid accumulation <a href="#bea7cebd9f2246f0a55ddcaaa708961d" title="permalink">#</a> </h3> <p> When an instance method <code>Op</code> forms a monoid, you can easily write a function that accumulates an arbitrary number of <code>Foo</code> values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Accumulate(<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Foo</span>&gt;&nbsp;foos) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;acc&nbsp;=&nbsp;Identity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;f&nbsp;<span style="color:blue;">in</span>&nbsp;foos) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;acc.Op(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; }</pre> </p> <p> Notice how this generally applicable algorithm starts with the <code>Identity</code> value. One implication of this is that when <code>foos</code> is empty, the return value will be <code>Identity</code>. When <code>Op</code> is a semigroup, however, there's no identity, so this doesn't quite work. You need a value to start the accumulation; something you can return if the collection is empty. </p> <h3 id="e2aadaf2b69c48a88a3f0b1ac0459fd4"> Semigroup accumulation <a href="#e2aadaf2b69c48a88a3f0b1ac0459fd4" title="permalink">#</a> </h3> <p> From <a href="https://www.haskell.org">Haskell</a> you can learn that if you have a semigroup, you can accumulate any non-empty collection: </p> <p> <code>sconcat :: Semigroup a =&gt; NonEmpty a -&gt; a</code> </p> <p> You can read this as: for any generic type <code>a</code>, when <code>a</code> forms a <code>Semigroup</code>, the <code>sconcat</code> function takes a non-empty list of <code>a</code> values, and reduces them to a single <code>a</code> value. <code>NonEmpty a</code> is a list with at least one element. </p> <h3 id="68b942712506472ebe4c6933ca7dbd56"> NotEmptyCollection <a href="#68b942712506472ebe4c6933ca7dbd56" title="permalink">#</a> </h3> <p> You can also define a <code>NotEmptyCollection&lt;T&gt;</code> class in C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;NotEmptyCollection(<span style="color:#2b91af;">T</span>&nbsp;head,&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">T</span>[]&nbsp;tail) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(head&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(head)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Head&nbsp;=&nbsp;head; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Tail&nbsp;=&nbsp;tail; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Head&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Tail&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Count&nbsp;{&nbsp;<span style="color:blue;">get</span>&nbsp;=&gt;&nbsp;<span style="color:blue;">this</span>.Tail.Count&nbsp;+&nbsp;1;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IEnumerator</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;GetEnumerator() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Head; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;item&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">this</span>.Tail) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerator</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>.GetEnumerator() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.GetEnumerator(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Because of the way the constructor is defined, you <em>must</em> supply at least one element in order to create an instance. You can provide any number of extra elements via the <code>tail</code> array, but one is minimum. </p> <p> The <code>Count</code> property returns the number of elements in <code>Tail</code>, plus one, because there's always a <code>Head</code> value. </p> <p> The <code>GetEnumerator</code> method returns an iterator that always starts with the <code>Head</code> value, and proceeds with all the <code>Tail</code> values, if there are any. </p> <h3 id="260996225d1249a9847065f4e4b23f40"> Finding the maximum of a non-empty collection <a href="#260996225d1249a9847065f4e4b23f40" title="permalink">#</a> </h3> <p> As you've already learned, <code>Math.Max</code> is a semigroup. Although the .NET Base Class Library has built-in methods for this, you can use a generally applicable algorithm to find the maximum value in a non-empty list of integers. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Accumulate(<span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;numbers) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;acc&nbsp;=&nbsp;numbers.Head; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;n&nbsp;<span style="color:blue;">in</span>&nbsp;numbers.Tail) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;<span style="color:#2b91af;">Math</span>.Max(acc,&nbsp;n); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; }</pre> </p> <p> Notice how similar this algorithm is to monoid accumulation! The only difference is that instead of using <code>Identity</code> to get started, you can use <code>Head</code>, and then loop over all elements of <code>Tail</code>. </p> <p> You can use it like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;nec&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:blue;">int</span>&gt;(42,&nbsp;1337,&nbsp;123); <span style="color:blue;">var</span>&nbsp;max&nbsp;=&nbsp;Accumulate(nec);</pre> </p> <p> Here, <code>max</code> is obviously <code>1337</code>. </p> <p> As usual, however, this is much nicer, and more succinct in Haskell: </p> <p> <pre>Prelude Data.Semigroup Data.List.NonEmpty&gt; getMax $ sconcat $ fmap Max $ 42 :| [1337, 123] 1337</pre> </p> <p> That's hardly <a href="/2015/08/03/idiomatic-or-idiosyncratic">the idiomatic</a> way of getting a maximum element in Haskell, but it does show how you can 'click together' concepts in order to achieve a goal. </p> <h3 id="f80749af4347478e905f55c0169687b4"> Aggregate <a href="#f80749af4347478e905f55c0169687b4" title="permalink">#</a> </h3> <p> Perhaps the observant reader will, at this point, have recalled to him- or herself that the .NET Base Class Library already includes an <code>Aggregate</code> extension method, with an overload that takes a seed. In general, the simpliest <code>Aggregate</code> method doesn't gracefully handle empty collections, but using the overload that takes a seed is more robust. You can rewrite the above <code>Accumulate</code> method using <code>Aggregate</code>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Accumulate(<span style="color:#2b91af;">NotEmptyCollection</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;numbers) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;numbers.Tail.Aggregate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numbers.Head, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Math</span>.Max(x,&nbsp;y)); }</pre> </p> <p> Notice how you can pass <code>Head</code> as the seed, and accumulate the <code>Tail</code> using that starting point. The <code>Aggregate</code> method is more like Haskell's <code>sconcat</code> for semigroups than <code>mconcat</code> for monoids. </p> <h3 id="722406b862374867afdcbb71671c80bf"> Summary <a href="#722406b862374867afdcbb71671c80bf" title="permalink">#</a> </h3> <p> A semigroup operation can be used to reduce values to a single value, just like a monoid can. The only difference is that a semigroup operation can't reduce an empty collection, whereas a monoid can. </p> <p> <strong>Next:</strong> <a href="/2017/12/18/quasigroups">Quasigroups</a> </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Bounding box semigroup https://blog.ploeh.dk/2017/12/04/bounding-box-semigroup 2017-12-04T08:40:00+00:00 Mark Seemann <div id="post"> <p> <em>A semigroup example for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2017/10/05/monoids-semigroups-and-friends">a larger series about monoids, semigroups, and other group-like algebraic structures</a>. In this article, you'll see a non-trivial example of a <a href="/2017/11/27/semigroups">semigroup</a> that's <em>not</em> a <a href="/2017/10/06/monoids">monoid</a>. In short, a semigroup is an associative binary operation. </p> <h3 id="199878a42aa845749aa23d6b728bf452"> Shapes <a href="#199878a42aa845749aa23d6b728bf452" title="permalink">#</a> </h3> <p> Imagine that you're developing a library of two-dimensional shapes, and that, for various reasons, each shape should have a <em>bounding box</em>. For example, here's a blue circle with a green bounding box: </p> <p> <img src="/content/binary/circle-with-bounding-box.png" alt="Circle with bounding box."> </p> <p> The code for a circle shape could look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Circle</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ICanHasBox</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;X&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Y&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Radius&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Circle(<span style="color:blue;">int</span>&nbsp;x,&nbsp;<span style="color:blue;">int</span>&nbsp;y,&nbsp;<span style="color:blue;">int</span>&nbsp;radius) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.X&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Y&nbsp;=&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Radius&nbsp;=&nbsp;<span style="color:#2b91af;">Math</span>.Abs(radius); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">BoundingBox</span>&nbsp;BoundingBox &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BoundingBox</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.X&nbsp;-&nbsp;<span style="color:blue;">this</span>.Radius, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Y&nbsp;-&nbsp;<span style="color:blue;">this</span>.Radius, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Radius&nbsp;*&nbsp;2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Radius&nbsp;*&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In addition to the <code>Circle</code> class, you could have other shapes, such as rectangles, triangles, or even irregular shapes, each of which have a bounding box. </p> <h3 id="ae4945c2dffa4c34b8707c1fc03cdac4"> Bounding box unions <a href="#ae4945c2dffa4c34b8707c1fc03cdac4" title="permalink">#</a> </h3> <p> If you have two shapes, you also have two (green) bounding boxes, but perhaps you'd like to find the (orange) bounding box of the union of both shapes. </p> <p> <img src="/content/binary/union-of-bounding-boxes.png" alt="Union of two shapes with bounding boxes."> </p> <p> That's fairly easy to do: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">BoundingBox</span>&nbsp;Unite(<span style="color:#2b91af;">BoundingBox</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newX&nbsp;=&nbsp;<span style="color:#2b91af;">Math</span>.Min(<span style="color:blue;">this</span>.X,&nbsp;other.X); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newY&nbsp;=&nbsp;<span style="color:#2b91af;">Math</span>.Min(<span style="color:blue;">this</span>.Y,&nbsp;other.Y); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newRightX&nbsp;=&nbsp;<span style="color:#2b91af;">Math</span>.Max(<span style="color:blue;">this</span>.rightX,&nbsp;other.rightX); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newTopY&nbsp;=&nbsp;<span style="color:#2b91af;">Math</span>.Max(<span style="color:blue;">this</span>.topY,&nbsp;other.topY); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BoundingBox</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newX, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newY, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newRightX&nbsp;-&nbsp;newX, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newTopY&nbsp;-&nbsp;newY); }</pre> </p> <p> The <code>Unite</code> method is an instance method on the <code>BoundingBox</code> class, so <a href="/2017/10/06/monoids">it's a binary operation</a>. It's also associative, because for all <code>x</code>, <code>y</code>, and <code>z</code>, <code>isAssociative</code> is <code>true</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;isAssociative&nbsp;=&nbsp;x.Unite(y).Unite(z)&nbsp;==&nbsp;x.Unite(y.Unite(z));</pre> </p> <p> Since the operation is associative, it's at least a semigroup. </p> <h3 id="9c3f6fd178b641008b45d61dc7ca0e5a"> Lack of identity <a href="#9c3f6fd178b641008b45d61dc7ca0e5a" title="permalink">#</a> </h3> <p> Is <code>Unite</code> also a monoid? In order to be a monoid, a binary operation must not only be associative, but also have an identity element. In <a href="/2017/10/23/convex-hull-monoid">a previous article</a>, you saw how the union of two <a href="https://en.wikipedia.org/wiki/Convex_hull">convex hulls</a> formed a monoid. A bounding box seems to be conceptually similar to a convex hull, so you'd be excused to think that our previous experience applies here as well. </p> <p> It doesn't. </p> <p> There's no <em>identity bounding box</em>. The difference between a convex hull and a bounding box is that it's possible to define an empty hull as an empty set of coordinates. A bounding box, on the other hand, always has a coordinate and a size. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">struct</span>&nbsp;<span style="color:#2b91af;">BoundingBox</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;rightX; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;topY; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;X&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Y&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Width&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Height&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;More&nbsp;members,&nbsp;including&nbsp;Unite...</span> }</pre> </p> <p> An identity element, if one exists, is one where if you <code>Unite</code> it with another <code>BoundingBox</code> object, the return value will be the other object. </p> <p> Consider, then, a (green) <code>BoundingBox x</code>. Any other <code>BoundingBox</code> inside of <code>x</code>, including <code>x</code> itself, is a candidate for an identity element: </p> <p> <img src="/content/binary/bounding-box-with-identity-candidates.png" alt="Bounding box with candidates for an identity element."> </p> <p> In a <a href="https://en.wikipedia.org/wiki/Real_number">real</a> coordinate system, there's infinitely many candidates contained in <code>x</code>. As long as a candidate is wholly contained within <code>x</code>, then the union of the candidate and <code>x</code> will return <code>x</code>. </p> <p> In the code example, however, coordinates are 32-bit integers, so for any bounding box <code>x</code>, there's only a finite number of candidates. Even for the smallest possible bounding box, though, the box itself is an identity candidate. </p> <p> In order to be an identity element, however, the <em>same</em> element must behave as the identity element for <em>all</em> bounding boxes. It is, therefore, trivial to find a counter-example: </p> <p> <img src="/content/binary/bounding-box-identity-counter-example.png" alt="Bounding box identity element counter-example."> </p> <p> Just pick any other <code>BoundingBox y</code> outside of <code>x</code>. Every identity candidate must be within <code>x</code>, and therefore the union of the candidate and <code>y</code> cannot be <code>y</code>. </p> <p> In code, you can demonstrate the lack of identity with an <a href="https://fscheck.github.io/FsCheck">FsCheck</a>-based test like this: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Property</span>&nbsp;UniteHasNoIdentity(<span style="color:#2b91af;">PositiveInt</span>&nbsp;w,&nbsp;<span style="color:#2b91af;">PositiveInt</span>&nbsp;h) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;genCandidate&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;xi&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.Choose(1,&nbsp;w.Get) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;yi&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.Choose(1,&nbsp;h.Get) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;wi&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.Choose(1,&nbsp;w.Get&nbsp;-&nbsp;xi&nbsp;+&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;hi&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.Choose(1,&nbsp;h.Get&nbsp;-&nbsp;yi&nbsp;+&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BoundingBox</span>(xi,&nbsp;yi,&nbsp;wi,&nbsp;hi); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Prop</span>.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;genCandidate.ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;identityCandidate&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BoundingBox</span>(1,&nbsp;1,&nbsp;w.Get,&nbsp;h.Get); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Show&nbsp;that&nbsp;the&nbsp;candidate&nbsp;behaves&nbsp;like&nbsp;identity&nbsp;for&nbsp;x</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;x.Unite(identityCandidate)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;identityCandidate.Unite(x)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Counter-example</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BoundingBox</span>(0,&nbsp;0,&nbsp;1,&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.NotEqual(y,&nbsp;y.Unite(identityCandidate)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> This example uses the <a href="https://www.nuget.org/packages/FsCheck.Xunit">FsCheck.Xunit</a> glue library for <a href="https://xunit.net">xUnit.net</a>. Notice that although FsCheck is written in F#, you can also use it from C#. This test passes. </p> <p> It follows the above 'proof' by first generating an identity candidate for <code>x</code>. This is any <code>BoundingBox</code> contained within <code>x</code>, including <code>x</code> itself. In order to keep the code as simple as possible, <code>x</code> is always placed at the coordinate <em>(1, 1)</em>. </p> <p> The test proceeds to utilise two <a href="http://xunitpatterns.com/Guard%20Assertion.html">Guard Assertions</a> to show that <code>identityCandidate</code> does, indeed, behave like an identity for <code>x</code>. </p> <p> Finally, the test finds a trivial counter-example in <code>y</code>, and verifies that <code>y.Unite(identityCandidate)</code> is not equal to <code>y</code>. Therefore, <code>identityCandidate</code> is <em>not</em> the identity for <code>y</code>. </p> <p> <code>Unite</code> is a semigroup, but not a monoid, because no identity element exists. </p> <h3 id="9e0e4b0fbb4f4852a121f5239964a779"> Summary <a href="#9e0e4b0fbb4f4852a121f5239964a779" title="permalink">#</a> </h3> <p> This article demonstrates (via an example) that non-trivial semigroups exist in normal object-oriented programming. </p> <p> <strong>Next:</strong> <a href="/2017/12/11/semigroups-accumulate">Semigroups accumulate</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="3e9d723101964a1d87ade48be37aa841"> <div class="comment-author"><a href="http://criticalsoftwareblog.com/">Yacoub Massad</a> <a href="#3e9d723101964a1d87ade48be37aa841">#</a></div> <div class="comment-content"> Thank you for writing about category theory. I just have a small note. "Just pick any other BoundingBox y partially or wholly outside of x." I think that one should pick a BoundingBox y wholly outside of x. Other wise the intersection between x and y would return x or y when we pass it to x.Unite or y.Unite respectively. <br> Thanks <br> </div> <div class="comment-date">2017-12-08 16:04 UTC</div> </div> <div class="comment" id="436c4737eef246bdb6c3fa6e2a74b70d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#436c4737eef246bdb6c3fa6e2a74b70d">#</a></div> <div class="comment-content"> <p> Yacoub, thank you for writing. The operation used here isn't the <em>intersection</em>, but rather the <em>union</em> of two bounding boxes; that's the reason I called the method <code>Unite</code>. </p> </div> <div class="comment-date">2017-12-09 12:55 UTC</div> </div> <div class="comment" id="fcb5896e9ff24747b0a1486b96352abd"> <div class="comment-author"><a href="http://criticalsoftwareblog.com/">Yacoub Massad</a> <a href="#fcb5896e9ff24747b0a1486b96352abd">#</a></div> <div class="comment-content"> <p> Hello Mark. I am aware of this, but maybe I did not explain my self correctly. <br> What I am trying to say is that when coming up with a counter-example, we should choose a BoundingBox y wholly outside of x (not just partially outside of x). <br> If we choose a BoundingBox y partially outside of x, then the intersection between x and y (the BoundingBox z = the area shared between x and y) is a valid identity element. <br> </p> </div> <div class="comment-date">2017-12-09 13:05 UTC</div> </div> <div class="comment" id="67c835a3e5074adfa82d0d2305dd6042"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#67c835a3e5074adfa82d0d2305dd6042">#</a></div> <div class="comment-content"> <p> Yacoub, I think you're right; sorry about that! </p> <p> Perhaps I should have written <em>Just pick any other <code>BoundingBox y</code> partially or wholly outside of the candidate.</em> Would that have been correct? </p> </div> <div class="comment-date">2017-12-09 13:57 UTC</div> </div> <div class="comment" id="555484d5e0344147aa106a60236a9b7c"> <div class="comment-author"><a href="http://criticalsoftwareblog.com/">Yacoub Massad</a> <a href="#555484d5e0344147aa106a60236a9b7c">#</a></div> <div class="comment-content"> <p> That would be correct. I am not sure though if this is the best way to explain it. <br> <br> y being wholly ourside of x seems better to me. <br> </p> </div> <div class="comment-date">2017-12-09 14:15 UTC</div> </div> <div class="comment" id="0b61842b0c0d4dcfb10d883145428847"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0b61842b0c0d4dcfb10d883145428847">#</a></div> <div class="comment-content"> <p> Yacoub, I've corrected the text in the article. Thank you for the feedback! </p> </div> <div class="comment-date">2017-12-09 15:21 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Semigroups https://blog.ploeh.dk/2017/11/27/semigroups 2017-11-27T12:39:00+00:00 Mark Seemann <div id="post"> <p> <em>Introduction to semigroups for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2017/10/05/monoids-semigroups-and-friends">a larger series about monoids, semigroups, and other group-like algebraic structures</a>. In this article, you'll learn what a semigroup is, and what distinguishes it from a <a href="/2017/10/06/monoids">monoid</a>. </p> <p> <img src="/content/binary/monoids-subset-of-semigroups.png" alt="Monoids are a subset of semigroups."> </p> <p> Semigroups form a superset of monoids. They are associative binary operations. While monoids additionally require that an identity element exists, no such requirement exist for semigroups. In other words, all monoids are semigroups, but not all semigroups are monoids. </p> <p> This article gives you an overview of semigroups, as well as a few small examples. A supplemental article will show a more elaborate example. </p> <h3 id="e9c36eb52d394b8989aacd1b6c783cb9"> Minimum <a href="#e9c36eb52d394b8989aacd1b6c783cb9" title="permalink">#</a> </h3> <p> An operation that returns the smallest of two values form a semigroup. In the .NET Base Class Library, such an operation is already defined for many numbers, for example 32-bit integers. Since associativity is a property of a semigroup, it makes sense to demonstrate it with a property-based test, here using <a href="https://fscheck.github.io/FsCheck">FsCheck</a>: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;IntMinimumIsAssociative(<span style="color:blue;">int</span>&nbsp;x,&nbsp;<span style="color:blue;">int</span>&nbsp;y,&nbsp;<span style="color:blue;">int</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Math</span>.Min(<span style="color:#2b91af;">Math</span>.Min(x,&nbsp;y),&nbsp;z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Math</span>.Min(x,&nbsp;<span style="color:#2b91af;">Math</span>.Min(y,&nbsp;z))); }</pre> </p> <p> This example uses the <a href="https://www.nuget.org/packages/FsCheck.Xunit">FsCheck.Xunit</a> glue library for <a href="https://xunit.net">xUnit.net</a>. Notice that although FsCheck is written in F#, you can also use it from C#. This test (as well as all other tests in this article) passes. </p> <p> For mathematical integers, no identity element exists, so the minimum operation doesn't form a monoid. In practice, however, .NET 32-bit integers <em>do</em> have an identity element: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;MimimumIntHasIdentity(<span style="color:blue;">int</span>&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;<span style="color:#2b91af;">Math</span>.Min(<span style="color:blue;">int</span>.MaxValue,&nbsp;x)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;<span style="color:#2b91af;">Math</span>.Min(x,&nbsp;<span style="color:blue;">int</span>.MaxValue)); }</pre> </p> <p> <a href="https://msdn.microsoft.com/en-us/library/system.int32.maxvalue">Int32.MaxValue</a> is the maximum possible 32-bit integer value, so it effectively behaves as the identity for the 32-bit integer minimum operation. All 32-bit numbers are smaller than, or equal to, <code>Int32.MaxValue</code>. This effectively makes <code>Math.Min(int, int)</code> a monoid, but conceptually, it's not. </p> <p> This may be clearer if, instead of 32-bit integers, you consider <a href="https://msdn.microsoft.com/en-us/library/system.numerics.biginteger">BigInteger</a>, which is an arbitrarily large (or small) integer. The <em>minimum</em> operation is still associative: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BigIntMinimumIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>.Min(<span style="color:#2b91af;">BigInteger</span>.Min(x,&nbsp;y),&nbsp;z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>.Min(x,&nbsp;<span style="color:#2b91af;">BigInteger</span>.Min(y,&nbsp;z))); }</pre> </p> <p> No identity element exists, however, because no matter which integer you have, you can always find one that's bigger: no maximum value exists. This makes <code>BigInteger.Min</code> a semigroup, but not a monoid. </p> <h3 id="0424d23ed0714d70a3c2cfe5e4660aa5"> Maximum <a href="#0424d23ed0714d70a3c2cfe5e4660aa5" title="permalink">#</a> </h3> <p> Like <em>minimum</em>, the <em>maximum</em> operation forms a semigroup, here demonstrated by <code>BigInteger.Max</code>: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BigIntMaximumIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>.Max(<span style="color:#2b91af;">BigInteger</span>.Max(x,&nbsp;y),&nbsp;z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">BigInteger</span>.Max(x,&nbsp;<span style="color:#2b91af;">BigInteger</span>.Max(y,&nbsp;z))); }</pre> </p> <p> Again, like minimum, no identity element exists because the set of integers is infinite; you can always find a bigger or smaller number. </p> <p> Minimum and maximum operations aren't limited to primitive numbers. If values can be compared, you can always find the smallest or largest of two values, here demonstrated with <code>DateTime</code> values: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;DateTimeMaximumIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">DateTime</span>,&nbsp;<span style="color:#2b91af;">DateTime</span>,&nbsp;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;dtMax&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(dt1,&nbsp;dt2)&nbsp;=&gt;&nbsp;dt1&nbsp;&gt;&nbsp;dt2&nbsp;?&nbsp;dt1&nbsp;:&nbsp;dt2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dtMax(dtMax(x,&nbsp;y),&nbsp;z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dtMax(x,&nbsp;dtMax(y,&nbsp;z))); }</pre> </p> <p> As was the case with 32-bit integers, however, the presence of <a href="https://msdn.microsoft.com/en-us/library/system.datetime.minvalue">DateTime.MinValue</a> effectively makes <code>dtMax</code> a monoid, but <em>conceptually</em>, no identity element exists, because dates are infinite. </p> <h3 id="79a13456ce5d48468169ba9985bf767f"> First <a href="#79a13456ce5d48468169ba9985bf767f" title="permalink">#</a> </h3> <p> Another binary operation simply returns the first of two values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;First&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; }</pre> </p> <p> This may seem pointless, but <code>First</code> <em>is</em> associative: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;FirstIsAssociative(<span style="color:#2b91af;">Guid</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">Guid</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">Guid</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;First(First(x,&nbsp;y),&nbsp;z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;First(x,&nbsp;First(y,&nbsp;z))); }</pre> </p> <p> On the other hand, there's no identity element, because there's no <em>left identity</em>. The <em>left identity</em> is an element <code>e</code> such that <code>First(e, x) == x</code> for any <code>x</code>. Clearly, for any generic type <code>T</code>, no such element exists because <code>First(e, x)</code> will only return <code>x</code> when <code>x</code> is equal to <code>e</code>. (There are, however, degenerate types for which an identity exists for <code>First</code>. Can you find an example?) </p> <h3 id="023edbd730b64bef9f8358bd95d4a9bd"> Last <a href="#023edbd730b64bef9f8358bd95d4a9bd" title="permalink">#</a> </h3> <p> Like <code>First</code>, a binary operation that returns the last (second) of two values also forms a semigroup: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Last&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y; }</pre> </p> <p> Similar to <code>First</code>, <code>Last</code> is associative: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;LastIsAssociative(<span style="color:#2b91af;">String</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">String</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">String</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last(Last(x,&nbsp;y),&nbsp;z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last(x,&nbsp;Last(y,&nbsp;z))); }</pre> </p> <p> As is also the case for <code>First</code>, no identity exists for <code>Last</code>, but here the problem is the lack of a <em>right identity</em>. The <em>right identity</em> is an element <code>e</code> for which <code>Last(x, e) == x</code> for all <code>x</code>. Clearly, <code>Last(x, e)</code> can only be equal to <code>x</code> if <code>e</code> is equal to <code>x</code>. </p> <h3 id="15d6faf006fe4b95a34b2d83c713151e"> Aggregation <a href="#15d6faf006fe4b95a34b2d83c713151e" title="permalink">#</a> </h3> <p> Perhaps you think that operations like <code>First</code> and <code>Last</code> seem useless in practice, but when you have a semigroup, you can reduce any non-empty sequence to a single value. In C#, you can use the <a href="https://msdn.microsoft.com/en-us/library/bb548651">Aggregate</a> LINQ method for this. For example </p> <p> <pre><span style="color:blue;">var</span>&nbsp;a&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;1,&nbsp;0,&nbsp;1337,&nbsp;-10,&nbsp;42&nbsp;}.Aggregate(<span style="color:#2b91af;">Math</span>.Min); </pre> </p> <p> returns <code>-10</code>, while </p> <p> <pre><span style="color:blue;">var</span>&nbsp;a&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;1,&nbsp;0,&nbsp;1337,&nbsp;-10,&nbsp;42&nbsp;}.Aggregate(<span style="color:#2b91af;">Math</span>.Max); </pre> </p> <p> returns <code>1337</code>. Notice that the input sequence is the same in both examples, but the semigroup differs. Likewise, you can use <code>Aggregate</code> with <code>First</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;a&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;1,&nbsp;0,&nbsp;1337,&nbsp;-10,&nbsp;42&nbsp;}.Aggregate(First); </pre> </p> <p> Here, <code>a</code> is <code>1</code>, while </p> <p> <pre><span style="color:blue;">var</span>&nbsp;a&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;1,&nbsp;0,&nbsp;1337,&nbsp;-10,&nbsp;42&nbsp;}.Aggregate(Last); </pre> </p> <p> returns <code>42</code>. </p> <p> LINQ has specialised methods like <a href="https://msdn.microsoft.com/en-us/library/bb352408">Min</a>, <a href="https://msdn.microsoft.com/en-us/library/bb358775">Last</a>, and so on, but from the perspective of behaviour, <code>Aggregate</code> would have been enough. While there may be performance reasons why some of those specialised methods exist, you can think of all of them as being based on the same abstraction: that of a semigroup. </p> <p> <code>Aggregate</code>, and many of the specialised methods, throw an exception if the input sequence is empty. This is because there's no identity element in a semigroup. The method doesn't know how to create a value of the type <code>T</code> from an empty list. </p> <p> If, on the other hand, you have a monoid, you can return the identity element in case of an empty sequence. <a href="https://www.haskell.org">Haskell</a> explicitly makes this distinction with <code>sconcat</code> and <code>mconcat</code>, but I'm not going to go into that now. </p> <h3 id="4ae02c6fbede45b0829ce9bed19603d0"> Summary <a href="#4ae02c6fbede45b0829ce9bed19603d0" title="permalink">#</a> </h3> <p> Semigroups are associative binary operations. In the previous <a href="/2017/10/06/monoids">article series about monoids</a> you saw plenty of examples, and since all monoids are semigroups, you've already seen more than one semigroup example. In this article, however, you saw four examples of semigroups that are <em>not</em> monoids. </p> <p> All four examples in this article are simple, and may not seem like 'real-world' examples. In the next article, then, you'll get a more realistic example of a semigroup that's not a monoid. </p> <p> <strong>Next:</strong> <a href="/2017/12/04/bounding-box-semigroup">Bounding box semigroup</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Monoids accumulate https://blog.ploeh.dk/2017/11/20/monoids-accumulate 2017-11-20T08:00:00+00:00 Mark Seemann <div id="post"> <p> <em>You can accumulate an arbitrary number of monoidal values to a single value. An article for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). </p> <p> Recall that a binary operation is an operation involving two arguments of the same type, and returning a value of that type. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Op(<span style="color:#2b91af;">Foo</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;y)</pre> </p> <p> Notice that such an operation reduces two <code>Foo</code> values to a single <code>Foo</code> value. </p> <h3 id="a7ab99d4b96c4f8d8a57bf5dde29714b"> Accumulation <a href="#a7ab99d4b96c4f8d8a57bf5dde29714b" title="permalink">#</a> </h3> <p> Since you have an operation that can reduce two values to a single value, you can use that single value as the input for yet another binary operation. This enables you to accumulate, or aggregate, an arbitrary number of values. </p> <p> Consider the instance variation of the above <code>Op</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Op(<span style="color:#2b91af;">Foo</span>&nbsp;foo)</pre> </p> <p> This is another representation of the operation, but instead of being a static method, it's an instance method on the <code>Foo</code> class. </p> <p> When <code>Op</code> is a monoid, you can easily write a function that accumulates an arbitrary number of <code>Foo</code> values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Accumulate(<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Foo</span>&gt;&nbsp;foos) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;acc&nbsp;=&nbsp;Identity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;f&nbsp;<span style="color:blue;">in</span>&nbsp;foos) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;acc.Op(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; }</pre> </p> <p> You start with the <code>Identity</code> value, which also becomes the return value if <code>foos</code> is empty. Then you simply loop over each value in <code>foos</code> and use <code>Op</code> with the value accumulated so far (<code>acc</code>) and the current element in the sequence. </p> <p> Once you're done looping, you return the accumulator. </p> <p> This implementation isn't always guaranteed to be the most efficient, but you can <em>always</em> write accumulation like that. Sometimes, a more efficient algorithm exists, but that doesn't change the overall result that you can always reduce an arbitrary number of values whenever a monoid exists for those values. </p> <h3 id="4d836a93139e4b94918378a7f605392e"> Generalisation <a href="#4d836a93139e4b94918378a7f605392e" title="permalink">#</a> </h3> <p> You can do this with any monoid. In <a href="https://www.haskell.org">Haskell</a>, this function is called <code>mconcat</code>, and it has this type: </p> <p> <pre>mconcat :: Monoid a =&gt; [a] -&gt; a</pre> </p> <p> The way you can read this is that for any monoid <code>a</code>, <code>mconcat</code> is a function that takes a linked list of <code>a</code> values as input, and returns a single <code>a</code> value as output. </p> <p> This function seems both more general, and more constrained, than the above C# example. It's more general than the C# example because it works on any monoid, instead of just <code>Foo.Op</code>. On the other hand, it seems more limited because it works only on Haskell lists. The C# example, in contrast, can accumulate any <code>IReadOnlyCollection&lt;Foo&gt;</code>. Could you somehow combine those two generalisations? </p> <p> Nothing stops you from doing that, but it's already in Haskell's <code>Data.Foldable</code> module: </p> <p> <pre>fold :: (Monoid m, Foldable t) =&gt; t m -&gt; m</pre> </p> <p> The way to read this is that there's a function called <code>fold</code>, and it accumulates any monoid <code>m</code> contained in any 'foldable' data container <code>t</code>. That a data container is 'foldable' means that there's a way to somehow fold, or aggregate, the element(s) in the container into a value. </p> <p> Linked lists, arrays, and other types of sequences are foldable, as are <a href="/2018/03/26/the-maybe-functor">Maybe</a> and <a href="/2018/08/06/a-tree-functor">trees</a>. </p> <p> In fact, there's little difference between Haskell's <code>Foldable</code> type class and .NET's <code>IEnumerable&lt;T&gt;</code>, but as the names suggest, their foci are different. In Haskell, the focus is being able to fold, accumulate, or aggregate a data structure, whereas on .NET the focus is on being able to enumerate the values inside the data structure. Ultimately, though, both abstractions afford the same capabilities. </p> <p> In .NET, the focal abstraction is the <a href="https://en.wikipedia.org/wiki/Iterator_pattern">Iterator pattern</a>, which enables you to enumerate the values in the data container. On top of that abstraction, you can derive other behaviour, such as the ability to <a href="https://msdn.microsoft.com/en-us/library/system.linq.enumerable.aggregate">Aggregate</a> data. </p> <p> In Haskell, the focus is on the ability to fold, but from that central abstraction follows the ability to convert the data container into a linked list, which you can then enumerate. </p> <h3 id="3a34228e6ed44c1d957284c4e3f94831"> Summary <a href="#3a34228e6ed44c1d957284c4e3f94831" title="permalink">#</a> </h3> <p> You can accumulate an arbitrary number of monoidal values as long as they're held in a container that enables you to 'fold' it. This includes all sorts of lists and arrays. </p> <p> This article concludes the article series about monoids. In the next series of articles, you'll learn about a related category of operations. </p> <p> <strong>Next: </strong> <a href="/2017/11/27/semigroups">Semigroups</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="db96e1b2e6fd43c88285a9c299570dd0"> <div class="comment-author"><a href="https://about.me/carsten.koenig">Carsten König</a> <a href="#db96e1b2e6fd43c88285a9c299570dd0">#</a></div> <div class="comment-content"> <p> <a href="https://twitter.com/ploeh">@ploeh</a> as always I loved your blog post but I don't 100% agree on your comparison of the iterator pattern with Foldable - the iterator pattern allows usually sideeffects and you have Traversable for that - you might also like this: <a href="http://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf">http://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf</a> … </p> <p> (Comment <a href="https://twitter.com/CarstenK_Dev/status/932597257584676864">submitted via Twitter</a>) </p> </div> <div class="comment-date">2017-11-20 13:11 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Endomorphism monoid https://blog.ploeh.dk/2017/11/13/endomorphism-monoid 2017-11-13T07:10:00+00:00 Mark Seemann <div id="post"> <p> <em>A method that returns the same type of output as its input forms a monoid. An article for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). Methods that return the same type of value as their input form monoids over composition. The formal term for such an operation is an <a href="https://en.wikipedia.org/wiki/Endomorphism">endomorphism</a>. </p> <h3 id="691b9a4afaab4c63b648f6891bcf7f1d"> Scheduling example <a href="#691b9a4afaab4c63b648f6891bcf7f1d" title="permalink">#</a> </h3> <p> Imagine that you have to develop some functionality for scheduling events in the future. As a concrete example, I recently wrote about adjusting dates while <a href="/2017/04/24/simple-holidays">taking bank holidays into account</a>. For instance, if you want to find the latest bank day <em>before</em> a given date, you could call the <code>AdjustToLatestPrecedingDutchBankDay</code> method. If you give it a normal bank day (say, a Thursday), it'll simply return the input date, but if you give it a Sunday, it'll return the preceding Friday. That is, unless that particular Friday is a bank holiday, in which case it'll return the Thursday before - as long as that's not also a bank holiday, and so on. </p> <p> In that previous article, the <code>AdjustToLatestPrecedingDutchBankDay</code> method is an extension method, but you can also model it as an instance method, like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;candidate&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;(!(IsDutchBankDay(candidate.DateTime))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate&nbsp;=&nbsp;candidate.AddDays(-1); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;candidate; }</pre> </p> <p> This method would be part of a class that implements an interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value); }</pre> </p> <p> You can make other implementations of this interface. Here's one that adjusts a date and time to business hours: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">BusinessHoursAdjustment</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;startOfBussiness&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(9); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;endOfBusiness&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(17); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Warning:&nbsp;May&nbsp;not&nbsp;handle&nbsp;DST&nbsp;changes&nbsp;appropriately!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;It&#39;s&nbsp;only&nbsp;example&nbsp;code...</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(value.TimeOfDay&nbsp;&lt;&nbsp;startOfBussiness) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;value&nbsp;-&nbsp;value.TimeOfDay&nbsp;+&nbsp;startOfBussiness; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(endOfBusiness&nbsp;&lt;&nbsp;value.TimeOfDay) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;(value&nbsp;-&nbsp;value.TimeOfDay&nbsp;+&nbsp;startOfBussiness).AddDays(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> To keep the example simple, business hours are hard-coded to 9-17. </p> <p> You could also <a href="https://en.wikipedia.org/wiki/Adapter_pattern">adapt</a> conversion to UTC: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">UtcAdjustment</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;value.ToUniversalTime(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Or add a month: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">NextMonthAdjustment</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;value.AddMonths(1); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that the <code>Adjust</code> method returns a value of the same type as its input. So far when discussing monoids, we've been looking at binary operations, but <code>Adjust</code> is a <em>unary</em> operation. </p> <p> An operation that returns the same type as its input is called an <em>endomorphism</em>. Those form monoids. </p> <h3 id="cc6d8ea6464e4730953c692c262151bf"> Composing adjustments <a href="#cc6d8ea6464e4730953c692c262151bf" title="permalink">#</a> </h3> <p> It's easy to connect two adjustments. Perhaps, for example, you'd like to first use <code>BusinessHoursAdjustment</code>, followed by the bank day adjustment. This will adjust an original input date and time to a date and time that falls on a bank day, within business hours. </p> <p> You can do this in a general-purpose, reusable way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;Append( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AppendedAdjustment</span>(x,&nbsp;y); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AppendedAdjustment</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;AppendedAdjustment( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.x&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.y&nbsp;=&nbsp;y; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;y.Adjust(x.Adjust(value)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Append</code> method takes two <code>IDateTimeOffsetAdjustment</code> values and combines them by wrapping them in a private implementation of <code>IDateTimeOffsetAdjustment</code>. When <code>AppendedAdjustment.Adjust</code> is called, it first calls <code>Adjust</code> on <code>x</code>, and then calls <code>Adjust</code> on <code>y</code> with the return value from the first call. </p> <p> In order to keep the example simple, I omitted null guards, but apart from that, <code>Append</code> should work with any two implementations of <code>IDateTimeOffsetAdjustment</code>. In other words, it obeys the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>. </p> <h3 id="45a1de760dbb4b99aca3ee90515b173e"> Associativity <a href="#45a1de760dbb4b99aca3ee90515b173e" title="permalink">#</a> </h3> <p> The <code>Append</code> method is a binary operation. It takes two <code>IDateTimeOffsetAdjustment</code> values and returns an <code>IDateTimeOffsetAdjustment</code>. It's also associative, as a test like this demonstrates: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AppendIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Append(y).Append(z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Append(y.Append(z))); }</pre> </p> <p> As usual in this article series, such a test doesn't <em>prove</em> that <code>Append</code> is associative for all values of <code>IDateTimeOffsetAdjustment</code>, but if you run it as a property-based test, it demonstrates that it's quite likely. </p> <h3 id="d29cfb1ecd4949f3a1e60462963ce19f"> Identity <a href="#d29cfb1ecd4949f3a1e60462963ce19f" title="permalink">#</a> </h3> <p> In true monoidal fashion, <code>IDateTimeOffsetAdjustment</code> also has an identity element: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IdentityDateTimeOffsetAdjustment</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Adjust(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation simply returns the input value without modifying it. That's a neutral operation, as a test like this demonstrates: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AppendHasIdentity(<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Append(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdentityDateTimeOffsetAdjustment</span>()),&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdentityDateTimeOffsetAdjustment</span>().Append(x),&nbsp;x); }</pre> </p> <p> These two assertions verify that left and right identity holds. </p> <p> Since <code>Append</code> is associative and has identity, it's a monoid. </p> <p> This holds generally for any method (or function) that returns the same type as it takes as input, i.e. <code>T SomeOperation(T x)</code>. This matches the built-in library in <a href="https://www.haskell.org">Haskell</a>, where <code>Endo</code> is a <code>Monoid</code>. </p> <h3 id="5e1a85ad892b4cd5b9ef269038c1d992"> Conclusion <a href="#5e1a85ad892b4cd5b9ef269038c1d992" title="permalink">#</a> </h3> <p> A method that returns a value of the same type as its (singular) input argument is called an endomorphism. You can compose two such unary operations together in order to get a composed operation. You simply take the output of the first method and use it as the input argument for the second method. That composition is a monoid. Endomorphisms form monoids. </p> <p> <strong>Next:</strong> <a href="/2018/04/03/maybe-monoids">Maybe monoids</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="u4pzbkj17huk7mrp3g6ghv42dpy6k9qb"> <div class="comment-author"><a href="http://blog.hovland.xyz/">Tor Hovland</a> <a href="#u4pzbkj17huk7mrp3g6ghv42dpy6k9qb">#</a></div> <div class="comment-content"> <p> Hi, Mark! </p> <p> I'm really enjoing your series on category theory. I think you're doing a great job in making such abstract concepts accessible to us programmers. However, I found this particular episode puzzling. You claim that any composition of endomorphisms is a monoid, and you also claim to have tested that your <code>Append</code> method is associative. However, it is not hard to come up with a counter-example: </p> <p> <pre>[Fact] public void CounterExample() { IDateTimeOffsetAdjustment weekend = new WeekendAdjustment(); IDateTimeOffsetAdjustment nextMonth = new NextMonthAdjustment(); var a = new AppendedAdjustment(weekend, nextMonth); var b = new AppendedAdjustment(nextMonth, weekend); var startDate = new DateTimeOffset(2019, 1, 5, 0, 0, 0, TimeSpan.Zero); Assert.Equal(a.Adjust(startDate), b.Adjust(startDate)); } </pre> </p> <p> Here, <code>WeekendAdjustment</code> is just a simplified <code>DutchBankDayAdjustment</code>. </p> <p> It would also be interesting to see how you can do property based testing on this, i.e. how to automatically generate meaningful instances of <code>IDateTimeOffsetAdjustment</code>. When I try to run your test, I get: "IDateTimeOffsetAdjustment is not handled automatically by FsCheck. Consider using another type or writing and registering a generator for it." </p> </div> <div class="comment-date">2018-12-30 18:33 UTC</div> </div> <div class="comment" id="b7f9c1d06c004938b563c5f365ff5af7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b7f9c1d06c004938b563c5f365ff5af7">#</a></div> <div class="comment-content"> <p> Tor, thank you for writing, and for your kind words. I suppose that the <code>CounterExample</code> test fails when one executes it; you don't explicitly write that, but that's what I expect would happen. </p> <p> The <code>Append</code> operation is, indeed, not <a href="https://en.wikipedia.org/wiki/Commutative_property">commutative</a>. This is, however, not a requirement for monoids, or even for <a href="https://en.wikipedia.org/wiki/Group_(mathematics)">groups</a>. Some monoids, such as addition and multiplication, are also commutative, while others, like <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">list concatenation</a> or, here, the endomorphism monoid, aren't. </p> <p> When it comes to the FsCheck properties, I admit that I cheated slightly with the code listing in the article. I did this because the properties are a bit more complicated than what I show, and I was concerned that the extra infrastructure surrounding those tests would detract from the overall message. </p> <p> The associativity property in its entirety looks like this: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Property</span>&nbsp;AppendIsAssociative() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Prop</span>.ForAll( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenerateAdjustment().Three().ToArbitrary(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;=&gt;&nbsp;AppendIsAssociative(t.Item1,&nbsp;t.Item2,&nbsp;t.Item3)); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AppendIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Append(y).Append(z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Append(y.Append(z)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTimeOffsetAdjustmentComparer</span>()); }</pre> </p> <p> Where <code>GenerateAdjustment</code> is defined like this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Gen</span>&lt;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&gt;&nbsp;GenerateAdjustment() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Gen</span>.Elements&lt;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdentityDateTimeOffsetAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BusinessHoursAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DutchBankDayAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NextMonthAdjustment</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UtcAdjustment</span>()); }</pre> </p> <p> Not only did I omit all that extra scaffolding, I also pretended that <code>IDateTimeOffsetAdjustment</code> objects could be compared using their default implementations of <code>Equals</code>, which they meaningfully can't. Notice that the full property listed here also asserts using a custom equality comparer: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DateTimeOffsetAdjustmentComparer</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IEqualityComparer</span>&lt;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;rnd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;System.<span style="color:#2b91af;">Random</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;dt&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(rnd.Next(),&nbsp;<span style="color:#2b91af;">TimeSpan</span>.Zero); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Adjust(dt)&nbsp;==&nbsp;y.Adjust(dt); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode(<span style="color:#2b91af;">IDateTimeOffsetAdjustment</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The custom comparer's <code>Equals</code> method creates a random number of <a href="https://docs.microsoft.com/dotnet/api/system.datetime.ticks">ticks</a> and uses it to create a <code>DateTimeOffset</code> value. It then calls <code>Adjust</code> on both objects, which are considered equal if they produce the same result. </p> <p> The test for identity is defined in a similar fashion; it also uses <code>GenerateAdjustment</code> and <code>DateTimeOffsetAdjustmentComparer</code>. </p> </div> <div class="comment-date">2018-12-30 19:44 UTC</div> </div> <div class="comment" id="94de6t8v0jlesfbf8byd0cbup5119kni"> <div class="comment-author"><a href="http://blog.hovland.xyz/">Tor Hovland</a> <a href="#94de6t8v0jlesfbf8byd0cbup5119kni">#</a></div> <div class="comment-content"> <p> Thank you for such a prompt and thorough reply. You are of course correct, I have been confusing <a href="https://en.wikipedia.org/wiki/Associative_property">associativity</a> with <a href="https://en.wikipedia.org/wiki/Commutative_property">commutativity</a>. I didn't run into the same mistake during the <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid/">list concatenation article</a>, though, maybe because list concatenation more obviously is associative and not commutative. In the current article, however, I intuitively felt that the operations needed to be commutative, but your reply clears that up. </p> <p> It is also helpful to see the extra scaffolding around your property based test. The article itself seemed to imply that instances of <code>IDateTimeOffsetAdjustment</code> could be automatically generated. Your approach to set up such a test will come in handy now that I'm convinced that I should let more of my tests be property based, even in C#! </p> </div> <div class="comment-date">2018-12-31 15:36 UTC</div> </div> <div class="comment" id="26908112a4ed4825affb1cab0c055a7a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#26908112a4ed4825affb1cab0c055a7a">#</a></div> <div class="comment-content"> <p> Tor, I made the same mistake several times when I started researching all of this. I think that there's something intuitive and fundamental about commutativity, so at a superficial glance, it seems odd that we can do without it, while at the same time requiring a less intuitive property like associativity. </p> <p> When it comes to computation, though, I think that it's exactly the nature of associativity that enables us to decompose big problems into several small problems. The order of how you deal with those small problems doesn't matter, as long as you apply the intermediate results in the correct order. </p> </div> <div class="comment-date">2019-01-01 11:55 UTC</div> </div> <div> <div class="comment" id="66f3bb26f9554d4d94c1f4d56e72f9a8"> <div class="comment-author">mnorbi <a href="#66f3bb26f9554d4d94c1f4d56e72f9a8">#</a></div> <div class="comment-content"> <p>Dear Mark!</p> <p>These additional explanations and contextual information in the comment sections deserve a place in the original text. To keep the text uncluttered this site uses clickable popups: <a href="https://www.painscience.com/bibliography.php">. If you click on some of the numbers, you'll see what I mean. You might consider adding this feature to the text.</a></p> <p>Best regards, Norbert.</p> </div> <div class="comment-date">2019-03-31 15:11 UTC</div> </div> </div> <div class="comment" id="3f097e70d4a445aa9799f51e58db782a"> <div class="comment-author">Michael Arnoldus <a href="#3f097e70d4a445aa9799f51e58db782a">#</a></div> <div class="comment-content"> <p> Dear Mark, </p> <p> Although late to the game I really enjoy and appreciate your series here explaining monoids. Your examples are quite helful in getting to a deeper understanding. </p> <p> This one however have me puzzled. </p> <p> As I see it, it's fairly easy to produce a counterexample to your claim (if I understand it correctly) that composition of functions with similar domain and codomain forms a monoid. To make it simple I'll use the domain of integers and define a function f(x) = x + 1 and g(x) = x * 6. They both return the same type as they take as input and yet, the composition is clearly not associative. </p> <p> The possible explanation I've been able to dig out, is if we assume the set of functions we talk about and the composition operator forms a category. However in this case, it's part of the definition of a category that an identity element exist and composition is associative. But then the claim seems circular. </p> <p> Am I missing anything here? </p> </div> <div class="comment-date">2022-01-01 20:43 UTC</div> </div> <div class="comment" id="ae5e51ac453c4accb970a91372346786"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ae5e51ac453c4accb970a91372346786">#</a></div> <div class="comment-content"> <p> Michael, thank you for writing. Can you share a counterexample? </p> <p> I've tried some casual examples with your <code>f</code> and <code>g</code> functions, but associativity seems to hold: </p> <p> <pre>Prelude&gt; (f . (g . f)) (-1) 1 Prelude&gt; ((f . g) . f) (-1) 1 Prelude&gt; (f . (g . f)) 0 7 Prelude&gt; ((f . g) . f) 0 7 Prelude&gt; (f . (g . f)) 1 13 Prelude&gt; ((f . g) . f) 1 13 Prelude&gt; (f . (g . f)) 2 19 Prelude&gt; ((f . g) . f) 2 19</pre> </p> <p> Casual experiments with <code>f . f . g</code> also indicates associativity. </p> <p> I've also tried with <a href="https://hackage.haskell.org/package/QuickCheck/docs/Test-QuickCheck.html">QuickCheck</a>: </p> <p> <pre>Prelude&gt; import Test.QuickCheck Prelude Test.QuickCheck&gt; f x = x + 1 Prelude Test.QuickCheck&gt; g x = x * 6 Prelude Test.QuickCheck&gt; :{ Prelude Test.QuickCheck| endoIsAssociative :: Int -&gt; Bool Prelude Test.QuickCheck| endoIsAssociative x = ((f . g) . f) x == (f . (g . f)) x Prelude Test.QuickCheck| :} Prelude Test.QuickCheck&gt; quickCheck $ withMaxSuccess 100000 endoIsAssociative +++ OK, passed 100000 tests.</pre> </p> <p> The composition <code>f . g . f</code> doesn't seem to easily produce any counterexamples. I haven't, however, tried all possible permutations of <code>f</code> and <code>g</code>, so that's no proof. </p> <p> If I recall correctly, however, associativity of endomorphisms is a property that one <em>can</em> prove with equational reasoning, so I'd be surprised if a counterexample exists. </p> </div> <div class="comment-date">2022-01-02 11:03 UTC</div> </div> <div class="comment" id="2bc55fbfe8254401b6b5f00fd533dd46"> <div class="comment-author">Michael Arnoldus <a href="#2bc55fbfe8254401b6b5f00fd533dd46">#</a></div> <div class="comment-content"> <p> Mark, thanks for a quick and quite elaborate reply. </p> <p> I'm slightly embarrased. First of all, my example attempting to disprove associativity should of course contain 3 functions, not 2 as I did. And second of all I could have / should have done exactly what you did and coded up an example - and then seen the error of my assumptions without taking your time. I apologise for the inconvenience. Lesson learned! </p> <p> I do appreciate your answer - and clearly I was wrong. </p> <p> Something still bothers my intuition around this. I trust your statement that it actually can be proven, so clearly my intuition have got it wrong. I'll continue to work on understanding this "associativity of endomorphisms", so I can develop a stronger intuition around monoids as well as associativity. </p> <p> Thank you for your help :) </p> </div> <div class="comment-date">2022-01-03 12:21 UTC</div> </div> <div class="comment" id="cb1e6fe0c27f470fb98ff79a3a42793a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cb1e6fe0c27f470fb98ff79a3a42793a">#</a></div> <div class="comment-content"> <p> Michael, being wrong is rarely a pleasant experience, but it might be a symptom that you're learning. Admitting one's mistake in public is, I believe, a sign of maturity and personal integrity. Thank you for doing that. </p> <p> I don't know if I can help your intuition along, but the proof isn't that hard. I originally learned <em>equational reasoning</em> from <a href="https://bartoszmilewski.com/2015/01/20/functors/">this article by Bartosz Milewski</a>, so I'm using that notation. The goal is to prove that: </p> <p> <pre>(f . g) . h == f . (g . h)</pre> </p> <p> for any three (<a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>) functions <code>f</code>, <code>g</code>, and <code>h</code>, where the binary operation is question is the composition operator <code>(.)</code> given as: </p> <p> <pre>(f . g) x = f (g x)</pre> </p> <p> This is Haskell syntax, where a function call is simply written as <code>f x</code> or <code>g x</code>, meaning that you call the function <code>f</code> or <code>g</code> with the value <code>x</code>. Brackets in Haskell work the same way as <a href="https://stackoverflow.com/a/39296035/126014">brackets in F#</a>, which again work like in mathematics. Thus, <code>(f . g) x</code> means <em>'the composed function <code>(f . g)</code> called with the argument <code>x</code>'</em>. </p> <p> The proof might go like this: </p> <p> <pre> (f . g) . h = { eta expansion } (\x -&gt; (f . g) x) . h = { definition of (.) } (\x -&gt; f (g x)) . h = { eta expansion } \y -&gt; ((\x -&gt; f (g x)) . h) y = { definition of (.) } \y -&gt; (\x -&gt; f (g x)) (h y) = { substitution (x = (h y)) } \y -&gt; f (g (h y)) = { definition of (.) } \y -&gt; f ((g . h) y) = { definition of (.) } \y -&gt; (f . (g . h)) y = { eta reduction } f . (g . h)</pre> </p> <p> Writing out a proof like this isn't something I do every day, so I may have made a mistake or two. Also, I can't shake the feeling that a <em>simpler</em> proof is available, but on the other hand, I found it a good exercise. </p> </div> <div class="comment-date">2022-01-04 9:13 UTC</div> </div> <div class="comment" id="478c16ca069248f98be35d19f579b379"> <div class="comment-author">Michael Arnoldus <a href="#478c16ca069248f98be35d19f579b379">#</a></div> <div class="comment-content"> <p> Mark, thank you for the kind words! </p> <p> And thank you for taking the time to write down a proof. Yes, the proof is indeed helpful. Seeing it made it clear that all function composition is associative. I think your comment about this being about pure functions (which really is quite obvious) also helped. It triggered one of the original insights that has moved me towards functional programming, which is <a href="https://en.wikipedia.org/wiki/Referential_transparency">Referential Transparency</a>. Suddenly it was blinding obvious that functional composition of pure functions really is just term substitution - so of course it's associative. </p> <p> It feels good to have my intuition up to speed :) </p> <p> This has been quite a fun and valuable exchange. Thank you for taking your time. I've learned something new and that's always a source of joy. </p> <p> Onwards and upwards! </p> </div> <div class="comment-date">2022-01-05 15:39 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Function monoids https://blog.ploeh.dk/2017/11/06/function-monoids 2017-11-06T06:11:00+00:00 Mark Seemann <div id="post"> <p> <em>Methods are monoids if they return monoids. An article for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). </p> <h3 id="31733b9bd9914ae68fc18afbe8b79e10"> Functions <a href="#31733b9bd9914ae68fc18afbe8b79e10" title="permalink">#</a> </h3> <p> In statically typed C-languages, like C# or Java, methods are typically declared like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Bar(<span style="color:#2b91af;">Baz</span>&nbsp;baz,&nbsp;<span style="color:#2b91af;">Qux</span>&nbsp;qux)</pre> </p> <p> As you'll see in another article, however, <a href="/2018/01/29/argument-list-isomorphisms">you can refactor any method to a method that takes a single argument as input</a>, and returns a single (possibly complex) value as output. In abstract form, we can write it like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Out1</span>&nbsp;Op1(<span style="color:#2b91af;">In1</span>&nbsp;arg) </pre> </p> <p> Another way to abstract a method is by using generics: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Op1&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T1</span>&nbsp;x) </pre> </p> <p> Another article demonstrates how <a href="/2018/01/22/function-isomorphisms">this is similar to a generic function</a>. In <a href="http://fsharp.org">F#</a>, for instance, the type of the function would be written as <code>'a -&gt; 'b</code>, whereas in <a href="https://www.haskell.org">Haskell</a> it'd be written <code>a -&gt; b</code>. The way to read this is that the function takes a value of the generic type <code>T1</code>/<code>'a</code>/<code>a</code> as input, and returns a value of the generic type <code>T</code>/<code>'b</code>/<code>b</code> as output. For the rest of this article, I'll favour the Haskell syntax, since it has minimal noise. </p> <p> To be clear, however, although I favour the Haskell syntax because of its succinctness, I don't mean to imply that the functions that I discuss are exclusively <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> - think of an F# function <code>'a -&gt; 'b</code> which may or may not be pure. </p> <h3 id="6fad691a152f45858592605dd2f9de76"> Binary combination of functions <a href="#6fad691a152f45858592605dd2f9de76" title="permalink">#</a> </h3> <p> A function <code>a -&gt; b</code> is a monoid if <code>b</code> is a monoid. This means that you can combine two functions with the same type. In an object-oriented context, it means that you can combine two methods with the same signature into one method as long as the return type forms a monoid. </p> <p> Consider the following (facetious) C# example. You're trying to establish how secure a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">GUID</a> is. Primes are important in cryptography, so the more primes a GUID contains, the better... right? </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;primes&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2357bd&quot;</span>; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;CountPrimes(<span style="color:#2b91af;">Guid</span>&nbsp;g) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;g.ToString(<span style="color:#a31515;">&quot;N&quot;</span>).Count(primes.Contains); }</pre> </p> <p> The <code>CountPrimes</code> method counts the number of prime digits in a given GUID. So far so good, but you also think that hexadecimal notation is more exotic than decimal notation, so surely, the digits A-F are somehow more secure, being more unfamiliar. Thus, you have a method to count those as well: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;letters&nbsp;=&nbsp;<span style="color:#a31515;">&quot;abcdef&quot;</span>; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;CountLetters(<span style="color:#2b91af;">Guid</span>&nbsp;g) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;g.ToString(<span style="color:#a31515;">&quot;N&quot;</span>).Count(letters.Contains); }</pre> </p> <p> Good, but both of these numbers are, clearly, excellent indicators of how secure a GUID is. Which one should you choose? Both of them, of course! </p> <p> Can you combine <code>CountPrimes</code> and <code>CountLetters</code>? Yes, you can, because, while GUIDs don't form a monoid, the return type <code>int</code> forms a monoid over addition. This enables you to write a <code>Combine</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;Combine( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;g) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x&nbsp;=&gt;&nbsp;f(x)&nbsp;+&nbsp;g(x); }</pre> </p> <p> Notice that <code>Combine</code> takes two <code>Func&lt;Guid, int&gt;</code> values and return a new <code>Func&lt;Guid, int&gt;</code> value. It's a binary operation. Here's an example of how to use it: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;calculateSecurity&nbsp;=&nbsp;Combine(CountPrimes,&nbsp;CountLetters); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;calculateSecurity(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Guid</span>(<span style="color:#a31515;">&quot;10763FF5-E3C8-46D1-A70F-6C1D9EDA8120&quot;</span>)); <span style="color:#2b91af;">Assert</span>.Equal(21,&nbsp;actual);</pre> </p> <p> Now you have an excellent measure of the security strength of GUIDs! 21 isn't <em>that</em> good, though, is it? </p> <p> Antics aside, <code>Combine</code> is a binary function. Is it a monoid? </p> <h3 id="e30b61e234e042efae981ff82df5cc5a"> Monoid laws <a href="#e30b61e234e042efae981ff82df5cc5a" title="permalink">#</a> </h3> <p> In order to be a monoid, <code>Combine</code> must be associative, and it is, as the following <a href="https://fscheck.github.io/FsCheck">FsCheck</a> property demonstrates: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CombineIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;g, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;h, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Guid</span>&nbsp;guid) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Combine(Combine(f,&nbsp;g),&nbsp;h)(guid), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Combine(f,&nbsp;Combine(g,&nbsp;h))(guid)); }</pre> </p> <p> In this property-based test, FsCheck generates three functions with the same signature. Since functions don't have structural equality, the easiest way to compare them is to call them and see whether they return the same result. This explains why the assertion invokes both associative combinations with <code>guid</code>. The test passes. </p> <p> In order to be a monoid, <code>Combine</code> must also have an identity element. It does: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;FuncIdentity&nbsp;=&nbsp;_&nbsp;=&gt;&nbsp;0; </pre> </p> <p> This is simply a function that ignores its input and always returns <code>0</code>, which is the identity value for addition. The following test demonstrates that it behaves as expected: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CombineHasIdentity(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f,&nbsp;<span style="color:#2b91af;">Guid</span>&nbsp;guid) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(f(guid),&nbsp;Combine(FuncIdentity,&nbsp;f)(guid)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(f(guid),&nbsp;Combine(f,&nbsp;FuncIdentity)(guid)); }</pre> </p> <p> As was the case with <code>CombineIsAssociative</code>, in order to assert that the combined functions behave correctly, you have to call them. This, again, explains why the assertion invokes the combined functions with <code>guid</code>. This test passes as well. </p> <p> <code>Combine</code> is a monoid. </p> <h3 id="7d0d5f317cee49f8aee21bcbbebac971"> Generalisation <a href="#7d0d5f317cee49f8aee21bcbbebac971" title="permalink">#</a> </h3> <p> While the above C# code is only an example, the general rule is that any function that returns a monoid is itself a monoid. In Haskell, this rule is articulated in the standard library: </p> <p> <pre>instance Monoid b =&gt; Monoid (a -&gt; b)</pre> </p> <p> This means that for any monoid <code>b</code>, a function <code>a -&gt; b</code> is also (automatically) a monoid. </p> <h3 id="b61569502c924c7598c714de7899eef8"> Summary <a href="#b61569502c924c7598c714de7899eef8" title="permalink">#</a> </h3> <p> A function or method with a return type that forms a monoid is itself a monoid. </p> <p> <strong>Next: </strong> <a href="/2017/11/13/endomorphism-monoid">Endomorphism monoid</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tuple monoids https://blog.ploeh.dk/2017/10/30/tuple-monoids 2017-10-30T07:01:00+00:00 Mark Seemann <div id="post"> <p> <em>Tuples of monoids form monoids. Data objects of monoids also form monoids. An article for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). This article starts off with some easy-to-understand, but abstract results. Once these are established, however, you'll see how to use them in a relatable example, so keep reading! </p> <h3 id="a56b7cb4bbd746de894138116455911f"> Tuples <a href="#a56b7cb4bbd746de894138116455911f" title="permalink">#</a> </h3> <p> A tuple is a group of elements. In statically typed programming languages, each element has a type, and the types don't have to be the same. As an example, in C#, you can create a tuple like this: </p> <p> <pre><span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;pair&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(42,&nbsp;<span style="color:#a31515;">&quot;Foo&quot;</span>); </pre> </p> <p> This creates a tuple where the first element must be an <code>int</code> and the second element a <code>string</code>. In the example, I've explicitly declared the type instead of using the <code>var</code> keyword, but this is only to make the type clearer (since you don't have an IDE in which to read the code). </p> <p> The <code>pair</code> tuple is a two-tuple, which means that it must have exactly two elements, of the types given, but you can also create larger tuples: </p> <p> <pre><span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;triple&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;Bar&quot;</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;42); </pre> </p> <p> This is a three-tuple, but conceptually, tuples can have any size. </p> <h3 id="aff87c48a64047bebf1025c0be3f3f42"> Pairs of monoids <a href="#aff87c48a64047bebf1025c0be3f3f42" title="permalink">#</a> </h3> <p> A <em>pair</em> (a two-tuple) forms a monoid if both elements form a monoid. <a href="https://www.haskell.org">Haskell</a> formalises this by stating: </p> <p> <code>instance (Monoid a, Monoid b) =&gt; Monoid (a, b)</code> </p> <p> The way to read this is that for any monoid <code>a</code> and any monoid <code>b</code>, the pair <code>(a, b)</code> is also a monoid. </p> <p> Perhaps this is easiest to understand with a C# example. Consider a tuple of the type <code>Tuple&lt;int, string&gt;</code>. Integers form monoids under both addition and multiplication, and <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">strings are monoids under concatenation</a>. Thus, you can make <code>Tuple&lt;int, string&gt;</code> form a monoid as well. For instance, use the multiplication monoid to define this binary operation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;CombinePair( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(x.Item1&nbsp;*&nbsp;y.Item1,&nbsp;x.Item2&nbsp;+&nbsp;y.Item2); }</pre> </p> <p> For this particular example, I've chosen multiplication as the binary operation for <code>int</code>, and the string concatenation operator <code>+</code> for <code>string</code>. The point is that since both elements are monoids, you can use their respective binary operations to return a new tuple with the combined values. </p> <p> This operation is associative, as the following <a href="https://fscheck.github.io/FsCheck">FsCheck</a> property demonstrates: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CombinePairIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CombinePair(CombinePair(x,&nbsp;y),&nbsp;z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CombinePair(x,&nbsp;CombinePair(y,&nbsp;z))); }</pre> </p> <p> This property passes for all the <code>x</code>, <code>y</code>, and <code>z</code> values that FsCheck generates. </p> <p> The <code>CombinePair</code> operation has identity as well: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;PairIdentity&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(1,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>);</pre> </p> <p> Again, you can use the identity value for each of the elements in the tuple: <code>1</code> for the multiplication monoid, and <code>""</code> for string concatenation. </p> <p> This value behaves as the identity for <code>CombinePair</code>, at least for all non-null string values: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CombinePairHasIdentity(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">NonNull</span>&lt;<span style="color:blue;">string</span>&gt;&gt;&nbsp;seed) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(seed.Item1,&nbsp;seed.Item2.Get); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(CombinePair(PairIdentity,&nbsp;x),&nbsp;CombinePair(x,&nbsp;PairIdentity)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;CombinePair(x,&nbsp;PairIdentity)); }</pre> </p> <p> Again, this test passes for all <code>seed</code> values generated by FsCheck. </p> <p> The C# code here is only an example, but I hope it's clear how the result generalises. </p> <h3 id="8f42879abb564cf1842c1ce5e45efc54"> Triples of monoids <a href="#8f42879abb564cf1842c1ce5e45efc54" title="permalink">#</a> </h3> <p> In the above section, you saw how pairs of monoids form a monoid. Not surprisingly, triples of monoids also form monoids. Here's another C# example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;CombineTriple( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Item1&nbsp;+&nbsp;y.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Item2&nbsp;||&nbsp;y.Item2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Item3&nbsp;*&nbsp;y.Item3); }</pre> </p> <p> The <code>CombineTriple</code> method is another binary operation. This time it combines two triples to a single triple. Since both <code>string</code>, <code>bool</code>, and <code>int</code> form monoids, it's possible to combine each element in the two tuples to create a new tuple. There's more than one monoid for integers, and the same goes for Boolean values, but here I've chosen multiplication and Boolean <em>or</em>, so the identity is this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;TripleIdentity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;1);</pre> </p> <p> This triple simply contains the identities for string concatenation, Boolean <em>or</em>, and multiplication. The operation is associative, but I'm not going to show this with a property-based test. Both tests for associativity and identity are similar to the above tests; you could consider writing them as an exercise, if you'd like. </p> <p> This triple example only demonstrates a particular triple, but you can find the generalisation in Haskell: </p> <p> <pre>instance (Monoid a, Monoid b, Monoid c) =&gt; Monoid (a, b, c)</pre> </p> <p> This simply states that for monoids <code>a</code>, <code>b</code>, and <code>c</code>, the tuple <code>(a, b, c)</code> is also a monoid. </p> <h3 id="a2644b148c4b4d4bb2da0a4687ae0b24"> Generalisation <a href="#a2644b148c4b4d4bb2da0a4687ae0b24" title="permalink">#</a> </h3> <p> At this point, it can hardly come as a surprise that quadruples and pentuples of monoids are also monoids. From Haskell: </p> <p> <pre>instance (Monoid a, Monoid b, Monoid c, Monoid d) =&gt; Monoid (a, b, c, d) instance (Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) =&gt; Monoid (a, b, c, d, e)</pre> </p> <p> The Haskell standard library stops at pentuples (five-tuples), because it has to stop somewhere, but I'm sure you can see how this is a general rule. </p> <h3 id="19e30b0295a14958af0dc868750ec01b"> Data objects as monoids <a href="#19e30b0295a14958af0dc868750ec01b" title="permalink">#</a> </h3> <p> If you're an object-oriented programmer, you probably don't use tuples much in your day-to-day work. I'd even suggest that you shouldn't, because tuples carry too little information to make good domain objects. For example, if you have <code>Tuple&lt;int, string, string&gt;</code>, what do the elements mean? A better design would be to introduce a small <a href="https://martinfowler.com/bliki/ValueObject.html">Value Object</a> called <code>Customer</code>, with <code>Id</code>, <code>FirstName</code>, and <code>LastName</code> properties. </p> <p> (In functional programming, you frequently use tuples, because they're useful for 'gluing' generic functions together. A Haskell programmer may instead say that they are useful for composing parametrically polymorphic functions, but the meaning would be the same.) </p> <p> As on object-oriented developer, then why should you care that tuples of monoids are monoids? </p> <p> The reason this is interesting in object-oriented programming is that there's a strong relationship between tuples and data objects (Value Objects or <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects</a>). Consider the <code>Customer</code> examples that I sketched out a few paragraphs above. As you'll learn in <a href="/2018/02/12/object-isomorphisms">a future article</a>, you can refactor a tuple to a class, or a class to a tuple. </p> <h3 id="2f9a7b476013439181668ce72d8d4a91"> Example: Roster <a href="#2f9a7b476013439181668ce72d8d4a91" title="permalink">#</a> </h3> <p> In Denmark, where I live, learning to swim is a mandatory part of the school curriculum. Teachers take the children to the nearby swimming stadium and teach them to swim. Since this is an activity outside of school, teachers would be prudent to keep a roster of the children. Modelled as a class, it might look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Roster</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Girls&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Boys&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;Exemptions&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Roster(<span style="color:blue;">int</span>&nbsp;girls,&nbsp;<span style="color:blue;">int</span>&nbsp;boys,&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:blue;">string</span>[]&nbsp;exemptions) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Girls&nbsp;=&nbsp;girls; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Boys&nbsp;=&nbsp;boys; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exemptions&nbsp;=&nbsp;exemptions; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span> }</pre> </p> <p> Some children may be temporarily exempt from a swimming lesson, perhaps because of a passing medical condition. This changes from lesson to lesson, so the roster keeps track of them separately. Additionally, the boys will need to go in the men's changing rooms, and the girls in the women's changing rooms. This is the reason the roster keeps track of the number of boys and girls separately. </p> <p> This, however, presents a logistical problem, because there's only one teacher for a class. The children are small, so need the company of an adult. </p> <p> The way my children's school solved that problem was to combine two groups of children (in Danish, <em>en klasse</em>, a <a href="https://en.wikipedia.org/wiki/Class_(education)">class</a>), each with their own teacher - one female, and one male. </p> <p> To model that, the <code>Roster</code> class should have a <code>Combine</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;Combine(<span style="color:#2b91af;">Roster</span>&nbsp;other) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Girls&nbsp;+&nbsp;other.Girls, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Boys&nbsp;+&nbsp;other.Boys, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Exemptions.Concat(other.Exemptions).ToArray()); }</pre> </p> <p> Clearly, this is easy to implement. Just add the number of girls together, add the number of boys together, and concatenate the two lists of exemptions. </p> <p> Here's an example of using the method: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsageExample() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>(11,&nbsp;10,&nbsp;<span style="color:#a31515;">&quot;Susan&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;George&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>(12,&nbsp;9,&nbsp;<span style="color:#a31515;">&quot;Edward&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;roster&nbsp;=&nbsp;x.Combine(y); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>(23,&nbsp;19,&nbsp;<span style="color:#a31515;">&quot;Susan&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;George&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Edward&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;roster); }</pre> </p> <p> The <code>Combine</code> method is an instance method on the <code>Roster</code> class, taking a second <code>Roster</code> as input, and returning a new <code>Roster</code> value. <a href="/2017/10/06/monoids">It's a binary operation</a>. Does it also have identity? </p> <p> Yes, it does: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;Identity&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>(0,&nbsp;0);</pre> </p> <p> Notice that the <code>exemptions</code> constructor argument is a <code>params</code> array, so omitting it means passing an empty array as the third argument. </p> <p> The following properties demonstrate that the <code>Combine</code> operation is both associative and has identity: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CombineIsAssociative(<span style="color:#2b91af;">Roster</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Combine(y).Combine(z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Combine(y.Combine(z))); } [<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CombineHasIdentity(<span style="color:#2b91af;">Roster</span>&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;<span style="color:#2b91af;">Roster</span>.Identity.Combine(x)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;x.Combine(<span style="color:#2b91af;">Roster</span>.Identity)); }</pre> </p> <p> In other words, <code>Combine</code> is a monoid. </p> <p> This shouldn't surprise us in the least, since we've already established that tuples of monoids are monoids, and that a data class is more or less 'just' a tuple with named elements. Specifically, the <code>Roster</code> class is a 'tuple' of two addition monoids and the sequence concatenation monoid, so it follows that the <code>Combine</code> method is a monoid as well. </p> <h3 id="42febe992c6b496a8afc9d4d641af38a"> Roster isomorphism <a href="#42febe992c6b496a8afc9d4d641af38a" title="permalink">#</a> </h3> <p> In <a href="/2018/01/08/software-design-isomorphisms">future articles</a>, you'll learn more about isomorphisms between various representations of objects, but in this context, I think it's relevant to show how the <code>Roster</code> example is isomorphic to a tuple. It's trivial, really: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>[]&gt;&nbsp;ToTriple() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:blue;">this</span>.Girls,&nbsp;<span style="color:blue;">this</span>.Boys,&nbsp;<span style="color:blue;">this</span>.Exemptions.ToArray()); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Roster</span>&nbsp;FromTriple(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>[]&gt;&nbsp;triple) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Roster</span>(triple.Item1,&nbsp;triple.Item2,&nbsp;triple.Item3); }</pre> </p> <p> This pair of methods turn a <code>Roster</code> into a triple, and a corresponding triple back into a <code>Roster</code> value. As the following two FsCheck properties demonstrate, these methods form an isomorphism: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ToTripleRoundTrips(<span style="color:#2b91af;">Roster</span>&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;triple&nbsp;=&nbsp;x.ToTriple(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;<span style="color:#2b91af;">Roster</span>.FromTriple(triple)); } [<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;FromTripleRoundTrips(<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>[]&gt;&nbsp;triple) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;roster&nbsp;=&nbsp;<span style="color:#2b91af;">Roster</span>.FromTriple(triple); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(triple,&nbsp;roster.ToTriple()); }</pre> </p> <p> This isn't the only possible isomorphism between triples and <code>Roster</code> objects. You could create another one where the <code>string[]</code> element goes first, instead of last; where boys go before girls; and so on. </p> <h3 id="18e30a88404f4e448e69fd7fe1da3209"> Summary <a href="#18e30a88404f4e448e69fd7fe1da3209" title="permalink">#</a> </h3> <p> Tuples of monoids are also monoids. This holds for tuples of any size, but all of the elements have to be monoids. By isomorphism, this result also applies to data objects. </p> <p> <strong>Next:</strong> <a href="/2017/11/06/function-monoids">Function monoids</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="76584e1afadb4f9a81c45255ad0dacc1"> <div class="comment-author"> <a href="https://github.com/PunkIslamist">Punkislamist</a> <a href="#76584e1afadb4f9a81c45255ad0dacc1">#</a></div> <div class="comment-content"> <p>Hi Mark, I have trouble understanding your usage of the term 'monoid' in this post. You apply it to the types string, bool, and int when you say that a tuple of those "monoids" is a monoid as well. But up to this point you made it very clear, that a type is NOT a monoid. A function can be a monoid. So, would it be more correct to say that a tuple of certain functions, which are monoids, is a monoid as well?</p> </div> <div class="comment-date">2018-01-26 18:50 UTC</div> </div> <div class="comment" id="68cbe481b0e74c3284e45d4e8dd51da4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#68cbe481b0e74c3284e45d4e8dd51da4">#</a></div> <div class="comment-content"> <p> Punkislamist, thank you for writing. You're entirely correct that a monoid is an associative binary operation with identity. It's a function, not a type. If this article is unclear, the fault is all mine. </p> <p> Not surprisingly, this topic is difficult to write about. The text has to be exact in order to avoid confusion, but since I'm only human, I sometimes make mistakes in how I phrase my explanations. While I've tried to favour the phrase that <em>a type forms a monoid</em>, I can see that I've slipped up once or twice in this article. </p> <p> Some types form more than a single monoid. Boolean values, for instance, form exactly four monoids. Other types, like integers, form an infinite set of monoids, but the most commonly used integer monoids are addition and multiplication. Other types, particularly <em>unit</em>, only form a single monoid. </p> <p> Why do I talk about types, then? There's at least two reasons. The first is the practical reason that most statically typed languages naturally come with a notion of types embedded. One could argue, I think, that types are a more fundamental concept than functions, since even functions have types (for instance, in Haskell, we'd characterise a binary operation with the type <code>a -&gt; a -&gt; a</code>). </p> <p> A more abstract reason is that <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a> mostly operates with the concepts of <em>objects</em> and <em>morphisms</em>. Such objects aren't objects in the sense of object-oriented programming, but rather correspond to types in programming languages. (Actually, a category theory <em>object</em> is a more fluffy concept than that, but that's the closest analogy that I'm aware of.) </p> <p> In category theory, a particular monoid is an object in the category of monoids. For example, the integer addition monoid is an object in the category of monoids, as is the string concatenation monoid, etcetera. </p> <p> When you consider a 'raw' programming language type like C#'s <code>int</code>, you're correct that it's not a monoid. It's just a type. The same goes for Haskell's corresponding <code>Int32</code> type. As primitive values, we could say that the type of 32-bit integers is an object in some category (for example, the category of number representations). Such an object is not a monoid. </p> <p> There exists, however, a morphism (a 'map') from the 32-bit integer object to the addition monoid (which is an object in the category of monoids). In Haskell, this morphism is the data constructor <code>Sum</code>: </p> <p> <pre>Prelude Data.Monoid&gt; :t Sum Sum :: a -&gt; Sum a</pre> </p> <p> What this states is that <code>Sum</code> is a function (i.e. a morphism) that takes an object <code>a</code> and turns it into an object <code>Sum a</code>. We have to be careful here, because <code>Sum a</code> is a Haskell <em>type</em>, whereas <code>Sum</code> is the function that 'elevates' an object <code>a</code> to <code>Sum a</code>. The names are similar, but the roles are different. This is a common idiom in Haskell, and has some mnemonic advantages, but it may be confusing until you get the hang of it. </p> <p> We can think of <code>Sum a</code> as equivalent to the category theory object <em>addition in the category of monoids</em>. That's also how it works in Haskell: <code>Sum a</code> is a monoid: </p> <p> <pre>Prelude Data.Monoid&gt; Sum 40 &lt;&gt; Sum 2 Sum {getSum = 42}</pre> </p> <p> In Haskell, <code>&lt;&gt;</code> is the polymorphic binary operation; exactly what it does depends on the object (that is: the type) on which it operates. When applied to two values of <code>Sum a</code>, the result of combining 40 and 2 is 42. </p> <p> To be clear, <code>Sum</code> isn't the only morphism from the category of number representations to the category of monoids. <code>Product</code> is another: </p> <p> <pre>Prelude Data.Monoid&gt; :t Product Product :: a -&gt; Product a Prelude Data.Monoid&gt; Product 6 &lt;&gt; Product 7 Product {getProduct = 42}</pre> </p> <p> Thus, there <em>is</em> a relationship between types and monoids, but it's most apparent in programming languages that are geared towards that way of thinking (like Haskell). In C#, it's difficult to translate some of these concepts into code, because C#'s type system isn't up to the task. Instead, when we consider a type like <code>int</code>, I think it's pragmatic to state that the type forms one or more monoids. I've also encountered the phrase that <em>it gives rise to a monoid</em>. </p> <p> While <a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-2-monoid">you can represent a monoid with a C# interface</a>, I've so far tried to avoid doing so, as I'm not sure whether or not it's helpful. </p> </div> <div class="comment-date">2018-01-28 11:57 UTC</div> </div> <div class="comment" id="ef117ef7ed594018af47f5efca556f1a"> <div class="comment-author"> <a href="https://github.com/PunkIslamist">Punkislamist</a> <a href="#ef117ef7ed594018af47f5efca556f1a">#</a></div> <div class="comment-content"> <p>Hi Mark, I did not expect to recieve such an exhaustive answer. That is incredible, thank you so much! It did clear up my confusion as well. Since most of these terms and concepts are new to me, even a slight inconsistency can be really confusing. But with your additional explanation I think I got a good understanding of the terms again.</p> <p>Your explanations of these concepts in general are very well written and make it easy for people unfamiliar with this topic to understand the terms and their significance. Thanks again for writing!</p> </div> <div class="comment-date">2018-01-28 12:47 UTC</div> </div> <div class="comment" id="02782e9f2d39461ba403e377007adaf0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#02782e9f2d39461ba403e377007adaf0">#</a></div> <div class="comment-content"> <p> Punkislamist, thank you for those kind words. I'm happy to hear that what I wrote made sense to you; it makes sense to me, but I forgot to point out that I'm hardly an expert in category theory. Writing out the above answer helped clarify some things for me as well; as is common wisdom: you only really understand a topic when you teach it. </p> </div> <div class="comment-date">2018-01-28 20:59 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Convex hull monoid https://blog.ploeh.dk/2017/10/23/convex-hull-monoid 2017-10-23T12:32:00+00:00 Mark Seemann <div id="post"> <p> <em>The union of convex hulls form a monoid. Yet another non-trivial monoid example, this time in F#.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). </p> <p> If you're reading the series as an object-oriented programmer, I apologise for the digression, but this article exclusively contains F# code. The next article will return with more C# examples. </p> <h3 id="8c0f3785d40942459136636131392671"> Convex hull <a href="#8c0f3785d40942459136636131392671" title="permalink">#</a> </h3> <p> In <a href="/2015/10/19/visual-value-verification">a past article</a> I've described my adventures with finding <a href="https://en.wikipedia.org/wiki/Convex_hull">convex hulls</a> in F#. The convex hulls I've been looking at form the external convex boundary of a set of two-dimensional points. While you can generalise the concept of convex hulls to <em>n</em> dimensions, we're going to stick to two-dimensional hulls here. </p> <p> <img src="/content/binary/convex-hull-example-01.png" alt="A 2D convex hull example."> </p> <p> If you have two convex hulls, you can find the convex hull of both: </p> <p> <img src="/content/binary/convex-hull-union.png" alt="A union of two convex hulls."> </p> <p> Here, the dark green outline is the convex hull of the two lighter-coloured hulls. </p> <p> Finding the convex hull of two other hulls is a binary operation. Is it a monoid? </p> <p> In order to examine that, I'm going to make some changes to my <a href="https://github.com/ploeh/Hull">existing code base</a>, the most important of which is that I'm going to introduce a <code>Hull</code> type. The intent is that if points are contained within this type, then only the convex hull remains. It'd be better if it was possible to make the case constructor private, but if one does that, then the <code>hull</code> function can no longer be inlined and generic. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Hull</span>&lt;&#39;a&gt;&nbsp;=&nbsp;<span style="color:navy;">Hull</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(&#39;a&nbsp;*&nbsp;&#39;a)&nbsp;<span style="color:teal;">list</span> </pre> </p> <p> With the addition of the <code>Hull</code> type, you can now add a binary operation: </p> <p> <pre><span style="color:green;">//&nbsp;Hull&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Hull&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Hull&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">inline</span>&nbsp;(+)&nbsp;(<span style="color:navy;">Hull</span>&nbsp;x)&nbsp;(<span style="color:navy;">Hull</span>&nbsp;y)&nbsp;=&nbsp;<span style="color:navy;">hull</span>&nbsp;(x&nbsp;@&nbsp;y)</pre> </p> <p> This operation explicitly uses the <code>+</code> operator, so I'm clearly anticipating the turn of events here. Nothing much is going on, though. The function pattern-matches the points out of two <code>Hull</code> values. <code>x</code> and <code>y</code> are two lists of points. The <code>+</code> function concatenates the two lists with the <code>@</code> operator, and finds the convex hull of this new list of points. </p> <h3 id="e500f30a1a784527be7267d5bb5b0b14"> Associativity <a href="#e500f30a1a784527be7267d5bb5b0b14" title="permalink">#</a> </h3> <p> My choice of operator strongly suggests that the <code>+</code> operation is a monoid. If you have three hulls, the order in which you find the hulls doesn't matter. One way to demonstrate that property is with property-based testing. In this article, I'm using <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a>. </p> <p> <pre>[&lt;<span style="color:teal;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Hull&nbsp;addition&nbsp;is&nbsp;associative``</span>&nbsp;()&nbsp;=&nbsp;<span style="color:teal;">Property</span>.<span style="color:navy;">check</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">property</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;(x,&nbsp;y,&nbsp;z)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Range</span>.<span style="color:navy;">linear</span>&nbsp;-10000&nbsp;10000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">int</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">tuple</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">list</span>&nbsp;(<span style="color:teal;">Range</span>.<span style="color:navy;">linear</span>&nbsp;0&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">tuple3</span> &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:navy;">hull</span>&nbsp;x&nbsp;+&nbsp;<span style="color:navy;">hull</span>&nbsp;y)&nbsp;+&nbsp;<span style="color:navy;">hull</span>&nbsp;z&nbsp;=!&nbsp;<span style="color:navy;">hull</span>&nbsp;x&nbsp;+&nbsp;(<span style="color:navy;">hull</span>&nbsp;y&nbsp;+&nbsp;<span style="color:navy;">hull</span>&nbsp;z)&nbsp;}</pre> </p> <p> This automated test generates three lists of points, <code>x</code>, <code>y</code>, and <code>z</code>. The <code>hull</code> function uses the <a href="https://en.wikipedia.org/wiki/Graham_scan">Graham Scan</a> algorithm to find the hull, and part of that algorithm includes calculating the cross product of three points. For large enough integers, the cross product will overflow, so the property constrains the point coordinates to stay within -10,000 and 10,000. The implication of that is that although <code>+</code> is associative, it's only associative for a subset of all 32-bit integers. I could probably change the internal implementation so that it calculates the cross product using <a href="https://msdn.microsoft.com/en-us/library/system.numerics.biginteger">bigint</a>, but I'll leave that as an exercise to you. </p> <p> For performance reasons, I also arbitrarily decided to constrain the size of each set of points to between 0 and 100 elements. If I change the maximum count to 1,000, it takes my laptop 9 seconds to run the test. </p> <p> In addition to Hedgehog, this test also uses <a href="https://xunit.net">xUnit.net</a>, and <a href="http://www.swensensoftware.com/unquote">Unquote</a> for assertions. The <code>=!</code> operator is the Unquote way of saying <em>must equal</em>. It's an assertion. </p> <p> This property passes, which demonstrates that the <code>+</code> operator for convex hulls is associative. </p> <h3 id="29277c663b7f48e480bd240353054ebb"> Identity <a href="#29277c663b7f48e480bd240353054ebb" title="permalink">#</a> </h3> <p> Likewise, you can write a property-based test that demonstrates that an identity element exists for the <code>+</code> operator: </p> <p> <pre>[&lt;<span style="color:teal;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``&nbsp;Hull&nbsp;addition&nbsp;has&nbsp;identity``</span>&nbsp;()&nbsp;=&nbsp;<span style="color:teal;">Property</span>.<span style="color:navy;">check</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">property</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Range</span>.<span style="color:navy;">linear</span>&nbsp;-10000&nbsp;10000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">int</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">tuple</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">list</span>&nbsp;(<span style="color:teal;">Range</span>.<span style="color:navy;">linear</span>&nbsp;0&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasIdentity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Hull</span>.identity&nbsp;+&nbsp;<span style="color:navy;">hull</span>&nbsp;x&nbsp;=&nbsp;<span style="color:navy;">hull</span>&nbsp;x&nbsp;+&nbsp;<span style="color:teal;">Hull</span>.identity&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">hull</span>&nbsp;x&nbsp;+&nbsp;<span style="color:teal;">Hull</span>.identity&nbsp;=&nbsp;<span style="color:navy;">hull</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">hasIdentity</span><span style="background:yellow;">&nbsp;@&gt;</span>&nbsp;}</pre> </p> <p> This test generates a list of integer pairs (<code>x</code>) and applies the <code>+</code> operator to <code>x</code> and <code>Hull.identity</code>. The test passes for all <code>x</code> that Hedgehog generates. </p> <p> What's <code>Hull.identity</code>? </p> <p> It's simply the empty hull: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:teal;">Hull</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;identity&nbsp;=&nbsp;<span style="color:navy;">Hull</span>&nbsp;[]</pre> </p> <p> If you have a set of zero 2D points, then the convex hull is empty as well. </p> <p> The <code>+</code> operator for convex hulls is a monoid for the set of coordinates where the cross product doesn't overflow. </p> <h3 id="7ac4918259344f4d8fb9d41875af35d8"> Summary <a href="#7ac4918259344f4d8fb9d41875af35d8" title="permalink">#</a> </h3> <p> If you consider that the <code>Hull</code> type is nothing but a container for a list, it should come as no surprise that a monoid exists. After all, <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">list concatenation is a monoid</a>, and the <code>+</code> operator shown here is a combination of list concatenation (<code>@</code>) and a Graham Scan. </p> <p> The point of this article was mostly to demonstrate that monoids exist not only for primitive types, but also for (some) more complex types. The <code>+</code> operator shown here is really a <a href="https://en.wikipedia.org/wiki/Union_(set_theory)">set union</a> operation. What about <a href="https://en.wikipedia.org/wiki/Intersection_(set_theory)">intersections</a> of convex hulls? Is that a monoid as well? I'll leave that as an exercise. </p> <p> <strong>Next:</strong> <a href="/2017/10/30/tuple-monoids">Tuple monoids</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="d81ab108a3b34e2283dba3744d7365d7"> <div class="comment-author"> <a href="https://mikhail.io/about/">Mikhail Shilkov</a> <a href="#d81ab108a3b34e2283dba3744d7365d7">#</a></div> <div class="comment-content"> <p>Is that true that you could replace hull with any other function, and (+) operator would still be a monoid? Since the operator is based on list concatenation, the "monoidness" is probably derived from there, not from function implementation.</p> </div> <div class="comment-date">2017-10-23 15:35 UTC</div> </div> <div class="comment" id="a66a18cac7e9421aa29cea1112e3bb1e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a66a18cac7e9421aa29cea1112e3bb1e">#</a></div> <div class="comment-content"> <p> Mikhail, thank you for writing. You can't replace <code>hull</code> with any other function and expect list concatenation to remain a monoid. I'm sorry if my turn of phrase gave that impression. I can see how one could interpret my summary in that way, but it wasn't my intention to imply that this relationship holds in general. It doesn't, and it's not hard to show, because we only need to come up with a single counter-example. </p> <p> One counter example is a function that always removes the first element in a list - unless the list is empty, in which case it simply returns the empty list. In Haskell, we can define a <code>newtype</code> with this behaviour in mind: </p> <p> <pre>Prelude Data.Monoid Data.List&gt; newtype Drop1 a = Drop1 [a] deriving (Show, Eq)</pre> </p> <p> For my own convenience, I wrote the entire counter-example in GHCi (the Haskell REPL), but imagine that the <code>Drop1</code> data constructor is hidden from clients. The normal way to do that is to not export the data constructor from the module. In GHCi, we can't do that, but just pretend that the <code>Drop1</code> data constructor is unavailable to clients. Instead, we'll have to use this function: </p> <p> <pre>Prelude Data.Monoid Data.List&gt; drop1 = Drop1 . drop 1</pre> </p> <p> The <code>drop1</code> function has the type <code>[a] -&gt; Drop1 a</code>; it takes a list, and returns a <code>Drop1</code> value, which contains the input list, apart from its first element. </p> <p> We can attempt to make <code>Drop 1</code> a monoid: </p> <p> <pre>Prelude Data.Monoid Data.List&gt; :{ Prelude Data.Monoid Data.List| instance Monoid (Drop1 a) where Prelude Data.Monoid Data.List| mempty = drop1 [] Prelude Data.Monoid Data.List| mappend (Drop1 xs) (Drop1 ys) = drop1 (xs ++ ys) Prelude Data.Monoid Data.List| :}</pre> </p> <p> Hopefully, you can see that the implementation of <code>mappend</code> is similar to the above F# implementation of <code>+</code> for convex hulls. In F#, the list concatenation operator is <code>@</code>, whereas in Haskell, it's <code>++</code>. </p> <p> This compiles, but it's easy to come up with some counter-examples that demonstrate that the monoid laws don't hold. First, associativity: </p> <p> <pre>Prelude Data.Monoid Data.List&gt; (drop1 [1..3] &lt;&gt; drop1 [4..6]) &lt;&gt; drop1 [7..9] Drop1 [5,6,8,9] Prelude Data.Monoid Data.List&gt; drop1 [1..3] &lt;&gt; (drop1 [4..6] &lt;&gt; drop1 [7..9]) Drop1 [3,6,8,9]</pre> </p> <p> (The <code>&lt;&gt;</code> operator is an infix alias for <code>mappend</code>.) </p> <p> Clearly, <code>[5,6,8,9]</code> is different from <code>[3,6,8,9]</code>, so the operation isn't associative. </p> <p> Equivalently, identity fails as well: </p> <p> <pre>Prelude Data.Monoid Data.List&gt; mempty &lt;&gt; drop1 [1..3] Drop1 [3] Prelude Data.Monoid Data.List&gt; drop1 [1..3] Drop1 [2,3]</pre> </p> <p> Again, <code>[3]</code> is different from <code>[2,3]</code>, so <code>mempty</code> isn't a proper identity element. </p> <p> It was easy to come up with this counter-example. I haven't attempted to come up with more, but I'd be surprised if I accidentally happened to pick the only counter-example there is. Rather, I conjecture that there are infinitely many counter-examples that each proves that there's no general rule about 'wrapped' lists operations being monoids. </p> </div> <div class="comment-date">2017-10-25 8:04 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Money monoid https://blog.ploeh.dk/2017/10/16/money-monoid 2017-10-16T07:28:00+00:00 Mark Seemann <div id="post"> <p> <em>Kent Beck's money TDD example has some interesting properties.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). </p> <p> In the first half of <a href="http://bit.ly/tddbe">Test-Driven Development By Example</a> Kent Beck explores how to develop a simple and flexible Money API using test-driven development. Towards the end, he arrives at a design that warrants further investigation. </p> <h3 id="06edd7bdd4054cedbcf9982c50019828"> Kent Beck's API <a href="#06edd7bdd4054cedbcf9982c50019828" title="permalink">#</a> </h3> <p> The following treatment of Kent Beck's code is based on <a href="http://yawar.blogspot.com">Yawar Amin</a>'s <a href="https://github.com/yawaramin/TDDMoney">C# reproduction</a> of Kent Beck's original Java code, further <a href="https://github.com/ploeh/TDDMoney">forked and manipulated</a> by me. </p> <p> The goal of Kent Beck's exercise is to develop an object-oriented API able to handle money of multiple currencies, and for example be able to express operations such as <em>5 USD + 10 CHF</em>. Towards the end of the example, he arrives at an interface that, translated to C#, looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IExpression</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Money</span>&nbsp;Reduce(<span style="color:#2b91af;">Bank</span>&nbsp;bank,&nbsp;<span style="color:blue;">string</span>&nbsp;to); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Plus(<span style="color:#2b91af;">IExpression</span>&nbsp;addend); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Times(<span style="color:blue;">int</span>&nbsp;multiplier); }</pre> </p> <p> The <code>Reduce</code> method reduces an <code>IExpression</code> object to a single currency (<code>to</code>), represented as a <code>Money</code> object. This is useful if you have an <code>IExpression</code> object that contains several currencies. </p> <p> The <code>Plus</code> method adds another <code>IExpression</code> object to the current object, and returns a new <code>IExpression</code>. This could be money in a single currency, but could also represent money held in more than one currency. </p> <p> The <code>Times</code> method multiplies an <code>IExpression</code> with a multiplier. You'll notice that, throughout this example code base, both multiplier and amounts are modelled as integers. I think that Kent Beck did this as a simplification, but a more realistic example should use <code>decimal</code> values. </p> <p> The metaphor is that you can model money as one or more <em>expressions</em>. A simple expression would be <em>5 USD</em>, but you could also have <em>5 USD + 10 CHF</em> or <em>5 USD + 10 CHF + 10 USD</em>. While you can reduce some expressions, such as <em>5 CHF + 7 CHF</em>, you can't reduce an expression like <em>5 USD + 10 CHF</em> unless you have an exchange rate. Instead of attempting to reduce monetary values, this particular design builds an expression tree until you decide to evaluate it. (<a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">Sounds familiar?</a>) </p> <p> Kent Beck implements <code>IExpression</code> twice: <ul> <li><code>Money</code> models an amount in a single currency. It contains an <code>Amount</code> and a <code>Currency</code> read-only property. It's the quintessential <a href="https://en.wikipedia.org/wiki/Value_object">Value Object</a>.</li> <li><code>Sum</code> models the sum of two other <code>IExpression</code> objects. It contains two other <code>IExpression</code> objects, called <code>Augend</code> and <code>Addend</code>.</li> </ul> If you want to express <em>5 USD + 10 CHF</em>, you can write: </p> <p> <pre><span style="color:#2b91af;">IExpression</span>&nbsp;sum&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Sum</span>(<span style="color:#2b91af;">Money</span>.Dollar(5),&nbsp;<span style="color:#2b91af;">Money</span>.Franc(10)); </pre> </p> <p> where <code>Money.Dollar</code> and <code>Money.Franc</code> are two static factory methods that return <code>Money</code> values. </p> <h3 id="6aa595d729b44bc2bd0c205c8bc805bc"> Associativity <a href="#6aa595d729b44bc2bd0c205c8bc805bc" title="permalink">#</a> </h3> <p> Did you notice that <code>Plus</code> <a href="/2017/10/06/monoids">is a binary operation</a>? Could it be a monoid as well? </p> <p> In order to be a monoid, it must obey the <em>monoid laws</em>, the first of which is that the operation must be associative. This means that for three <code>IExpression</code> objects, <code>x</code>, <code>y</code>, and <code>z</code>, <code>x.Plus(y).Plus(z)</code> must be equal to <code>x.Plus(y.Plus(z))</code>. How should you interpret equality here? The return value from <code>Plus</code> is another <code>IExpression</code> value, and interfaces don't have custom equality behaviour. Either, it's up to the individual implementations (<code>Money</code> and <code>Sum</code>) to override and implement equality, or you can use <a href="http://xunitpatterns.com/test-specific%20equality.html">test-specific equality</a>. </p> <p> The <a href="https://xunit.net">xUnit.net</a> assertion library supports test-specific equality via custom comparers (for more details, see my <a href="https://blog.ploeh.dk/advanced-unit-testing">Advanced Unit Testing</a> Pluralsight course). The original Money API does, however, already include a way to compare expressions! </p> <p> The <code>Reduce</code> method can reduce any <code>IExpression</code> to a single <code>Money</code> object (that is, to a single currency), and since <code>Money</code> is a Value Object, it has structural equality. You can use this to compare the values of <code>IExpression</code> objects. All you need is an exchange rate. </p> <p> In the book, Kent Beck uses a 2:1 exchange rate between CHF and USD. As I'm writing this, the exchange rate is 0.96 Swiss Franc to a Dollar, but since the example code consistently models money as integers, that rounds to a 1:1 exchange rate. This is, however, a degenerate case, so instead, I'm going to stick to the book's original 2:1 exchange rate. </p> <p> You can now add an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> between <code>Reduce</code> and xUnit.net in the form of an <code>IEqualityComparer&lt;IExpression&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IEqualityComparer</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Bank</span>&nbsp;bank; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ExpressionEqualityComparer() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bank&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Bank</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bank.AddRate(<span style="color:#a31515;">&quot;CHF&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>,&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:#2b91af;">IExpression</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;xm&nbsp;=&nbsp;bank.Reduce(x,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ym&nbsp;=&nbsp;bank.Reduce(y,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(xm,&nbsp;ym); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode(<span style="color:#2b91af;">IExpression</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;bank.Reduce(obj,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>).GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You'll notice that this custom equality comparer uses a <code>Bank</code> object with a 2:1 exchange rate. <code>Bank</code> is another object from the <em>Test-Driven Development</em> example. It doesn't implement any interface itself, but it does appear as an argument in the <code>Reduce</code> method. </p> <p> In order to make your test code more readable, you can add a static helper class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Compare</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>&nbsp;UsingBank&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>(); }</pre> </p> <p> This enables you to write an assertion for associativity like this: </p> <p> <pre><span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;x.Plus(y).Plus(z), &nbsp;&nbsp;&nbsp;&nbsp;x.Plus(y.Plus(z)), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank);</pre> </p> <p> In my fork of Yawar Amin's code base, I added this assertion to an <a href="https://fscheck.github.io/FsCheck">FsCheck</a>-based automated test, and it holds for all the <code>Sum</code> and <code>Money</code> objects that FsCheck generates. </p> <p> In its present incarnation, <code>IExpression.Plus</code> is associative, but it's worth noting that this isn't guaranteed to last. An interface like <code>IExpression</code> is an extensibility point, so someone could easily add a third implementation that would violate associativity. We can tentatively conclude that <code>Plus</code> is currently associative, but that the situation is delicate. </p> <h3 id="b189bd6ff2704428960624b3c228c135"> Identity <a href="#b189bd6ff2704428960624b3c228c135" title="permalink">#</a> </h3> <p> If you accept that <code>IExpression.Plus</code> is associative, it's a monoid candidate. If an identity element exists, then it's a monoid. </p> <p> Kent Beck never adds an identity element in his book, but you can add one yourself: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Plus</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Identity&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PlusIdentity</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PlusIdentity</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IExpression</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Plus(<span style="color:#2b91af;">IExpression</span>&nbsp;addend) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;addend; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Money</span>&nbsp;Reduce(<span style="color:#2b91af;">Bank</span>&nbsp;bank,&nbsp;<span style="color:blue;">string</span>&nbsp;to) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Money</span>(0,&nbsp;to); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Times(<span style="color:blue;">int</span>&nbsp;multiplier) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> There's only a single identity element, so it makes sense to make it a <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a>. The private <code>PlusIdentity</code> class is a new <code>IExpression</code> implementation that deliberately doesn't do anything. </p> <p> In <code>Plus</code>, it simply returns the input expression. This is the same behaviour as zero has for integer addition. When adding numbers together, zero is the identity element, and the same is the case here. This is more explicitly visible in the <code>Reduce</code> method, where the identity expression simply reduces to zero in the requested currency. Finally, if you multiply the identity element, you still get the identity element. <del datetime="2022-02-08T10:03:44.6554395+01:00">Here, interestingly, <code>PlusIdentity</code> behaves similar to the identity element for multiplication (<em>1</em>).</del> </p> <p> You can now write the following assertions for any <code>IExpression x</code>: </p> <p> <pre><span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;x.Plus(<span style="color:#2b91af;">Plus</span>.Identity),&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank); <span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;<span style="color:#2b91af;">Plus</span>.Identity.Plus(x),&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank);</pre> </p> <p> Running this as a property-based test, it holds for all <code>x</code> generated by FsCheck. The same caution that applies to associativity also applies here: <code>IExpression</code> is an extensibility point, so you can't be sure that <code>Plus.Identity</code> will be the identity element for all <code>IExpression</code> implementations someone could create, but for the three implementations that now exist, the monoid laws hold. </p> <p> <code>IExpression.Plus</code> is a monoid. </p> <h3 id="f2a0b897ba164649af3171e33f504f2d"> Multiplication <a href="#f2a0b897ba164649af3171e33f504f2d" title="permalink">#</a> </h3> <p> In basic arithmetic, the multiplication operator is called <em>times</em>. When you write <em>3 * 5</em>, it literally means that you have 3 five times (or do you have 5 three times?). In other words: </p> <p> <pre>3 * 5 = 3 + 3 + 3 + 3 + 3</pre> </p> <p> Does a similar relationship exist for <code>IExpression</code>? </p> <p> Perhaps, we can take a hint from <a href="https://www.haskell.org">Haskell</a>, where monoids and semigroups are explicit parts of the core library. You're going to learn about semigroups later, but for now, it's interesting to observe that the <code>Semigroup</code> typeclass defines a function called <code>stimes</code>, which has the type <code>Integral b =&gt; b -&gt; a -&gt; a</code>. Basically, what this means that for any integer type (16-bit integer, 32-bit integer, etc.) <code>stimes</code> takes an integer and a value <code>a</code> and 'multiplies' the value. Here, <code>a</code> is a type for which a binary operation exists. </p> <p> In C# syntax, <code>stimes</code> would look like this as an instance method on a <code>Foo</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Times(<span style="color:blue;">int</span>&nbsp;multiplier) </pre> </p> <p> I named the method <code>Times</code> instead of <code>STimes</code>, since I strongly suspect that the <em>s</em> in Haskell's <code>stimes</code> stands for <code>Semigroup</code>. </p> <p> Notice how this is the same type of signature as <code>IExpression.Times</code>. </p> <p> If it's possible to define a universal implementation of such a function in Haskell, could you do the same in C#? In <code>Money</code>, you can implement <code>Times</code> based on <code>Plus</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Times(<span style="color:blue;">int</span>&nbsp;multiplier) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Enumerable</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Repeat((<span style="color:#2b91af;">IExpression</span>)<span style="color:blue;">this</span>,&nbsp;multiplier) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;x.Plus(y)); }</pre> </p> <p> The static <code>Repeat</code> LINQ method returns <code>this</code> as many times as requested by <code>multiplier</code>. The return value is an <code>IEnumerable&lt;IExpression&gt;</code>, but according to the <code>IExpression</code> interface, <code>Times</code> must return a single <code>IExpression</code> value. You can use the <code>Aggregate</code> LINQ method to repeatedly combine two <code>IExpression</code> values (<code>x</code> and <code>y</code>) to one, using the <code>Plus</code> method. </p> <p> This implementation is hardly as efficient as the previous, individual implementation, but the point here isn't about efficiency, but about a common, reusable abstraction. The exact same implementation can be used to implement <code>Sum.Times</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Times(<span style="color:blue;">int</span>&nbsp;multiplier) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Enumerable</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Repeat((<span style="color:#2b91af;">IExpression</span>)<span style="color:blue;">this</span>,&nbsp;multiplier) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;x.Plus(y)); }</pre> </p> <p> This is literally the same code as for <code>Money.Times</code>. You can also copy and paste this code to <code>PlusIdentity.Times</code>, but I'm not going to repeat it here, because it's the same code as above. </p> <p> This means that you can remove the <code>Times</code> method from <code>IExpression</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IExpression</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Money</span>&nbsp;Reduce(<span style="color:#2b91af;">Bank</span>&nbsp;bank,&nbsp;<span style="color:blue;">string</span>&nbsp;to); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Plus(<span style="color:#2b91af;">IExpression</span>&nbsp;addend); }</pre> </p> <p> Instead, you can implement it as an extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Expression</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;Times(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;exp,&nbsp;<span style="color:blue;">int</span>&nbsp;multiplier) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Enumerable</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Repeat(exp,&nbsp;multiplier) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;x.Plus(y)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This works because any <code>IExpression</code> object has a <code>Plus</code> method. </p> <p> As I've already admitted, this is likely to be less efficient than specialised implementations of <code>Times</code>. In Haskell, this is addressed by making <code>stimes</code> part of the typeclass, so that implementers can implement a more efficient algorithm than the default implementation. In C#, the same effect could be achieved by refactoring <code>IExpression</code> to an abstract base class, with <code>Times</code> as a public virtual (overridable) method. </p> <h3 id="37f5a9d3ddcc49dc85824f5a67e1e0c2"> Haskell sanity check <a href="#37f5a9d3ddcc49dc85824f5a67e1e0c2" title="permalink">#</a> </h3> <p> Since Haskell has a more formal definition of a monoid, you may want to try to port Kent Beck's API to Haskell, as a proof of concept. In its final modification, my C# fork has three implementations of <code>IExpression</code>: <ul> <li><code>Money</code></li> <li><code>Sum</code></li> <li><code>PlusIdentity</code></li> </ul> While interfaces are extensible, we were rightfully uneasy about this, so in Haskell, it seems safer to model these three subtypes as a sum type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Expression</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Money</span>&nbsp;{&nbsp;amount&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Int</span>,&nbsp;currency&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">String</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Sum</span>&nbsp;{&nbsp;augend&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Expression</span>,&nbsp;addend&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Expression</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">MoneyIdentity</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>)</pre> </p> <p> You can formally make this a <code>Monoid</code>: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;<span style="color:blue;">Expression</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;mempty&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">MoneyIdentity</span> &nbsp;&nbsp;mappend&nbsp;<span style="color:#dd0000;">MoneyIdentity</span>&nbsp;y&nbsp;<span style="color:#666666;">=</span>&nbsp;y &nbsp;&nbsp;mappend&nbsp;x&nbsp;<span style="color:#dd0000;">MoneyIdentity</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;x &nbsp;&nbsp;mappend&nbsp;x&nbsp;y&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Sum</span>&nbsp;x&nbsp;y</pre> </p> <p> The C# <code>Plus</code> method is here implemented by the <code>mappend</code> function. The only remaining member of <code>IExpression</code> is <code>Reduce</code>, which you can implement like this: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;Data.Map.Strict&nbsp;(<span style="color:blue;">Map</span>,&nbsp;<span style="color:#2b91af;">(!)</span>) <span style="color:#600277;">reduce</span>&nbsp;::&nbsp;<span style="color:blue;">Ord</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Map</span>&nbsp;(String,&nbsp;a)&nbsp;Int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Expression</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Int reduce&nbsp;bank&nbsp;to&nbsp;(<span style="color:#dd0000;">Money</span>&nbsp;amt&nbsp;cur)&nbsp;<span style="color:#666666;">=</span>&nbsp;amt&nbsp;<span style="color:#666666;">`div`</span>&nbsp;rate &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;rate&nbsp;<span style="color:#666666;">=</span>&nbsp;bank&nbsp;<span style="color:#666666;">!</span>&nbsp;(cur,&nbsp;to) reduce&nbsp;bank&nbsp;to&nbsp;(<span style="color:#dd0000;">Sum</span>&nbsp;x&nbsp;y)&nbsp;<span style="color:#666666;">=</span>&nbsp;reduce&nbsp;bank&nbsp;to&nbsp;x&nbsp;<span style="color:#666666;">+</span>&nbsp;reduce&nbsp;bank&nbsp;to&nbsp;y reduce&nbsp;_&nbsp;_&nbsp;<span style="color:#dd0000;">MoneyIdentity</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#09885a;">0</span></pre> </p> <p> Haskell's typeclass mechanism takes care of the rest, so that, for example, you can reproduce one of Kent Beck's original tests like this: </p> <p> <pre>λ&gt; let bank = fromList [(("CHF","USD"),2), (("USD", "USD"),1)] λ&gt; let sum = stimesMonoid 2 $ MoneyPort.Sum (Money 5 "USD") (Money 10 "CHF") λ&gt; reduce bank "USD" sum 20</pre> </p> <p> Just like <code>stimes</code> works for any <code>Semigroup</code>, <code>stimesMonoid</code> is defined for any <code>Monoid</code>, and therefore you can also use it with <code>Expression</code>. </p> <p> With the historical 2:1 exchange rate, 5 Dollars + 10 Swiss Franc, times 2, is equivalent to 20 Dollars. </p> <h3 id="a01b43c498a749bf83451cc0ca378c77"> Summary <a href="#a01b43c498a749bf83451cc0ca378c77" title="permalink">#</a> </h3> <p> In chapter 17 of his book, Kent Beck describes that he'd been TDD'ing a Money API many times before trying out the expression-based API he ultimately used in the book. In other words, he had much experience, both with this particular problem, and with programming in general. Clearly this is a highly skilled programmer at work. </p> <p> I find it interesting that he seems to intuitively arrive at a design involving a monoid and an interpreter. If he did this on purpose, he doesn't say so in the book, so I rather speculate that he arrived at the design simply because he recognised its superiority. This is the reason that I find it interesting to identify this, <em>an existing example</em>, as a monoid, because it indicates that there's something supremely comprehensible about monoid-based APIs. It's conceptually 'just like addition'. </p> <p> In this article, we returned to a decade-old code example in order to identify it as a monoid. In the next article, I'm going to revisit an example code base of mine from 2015. </p> <p> <strong>Next: </strong> <a href="/2017/10/23/convex-hull-monoid">Convex hull monoid</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="92a4aca2698146fc898bdaf0ab31c16b"> <div class="comment-author"> <a href="https://github.com/baic989">Hrvoje Baic</a> <a href="#92a4aca2698146fc898bdaf0ab31c16b">#</a></div> <div class="comment-content"> <q>You'll notice that, throughout this example code base, both multiplier and amounts are modelled as integers. I think that Kent Beck did this as a simplification, but a more realistic example should use decimal values.</q> <p>Actually, in a lot of financial systems money is stored in cents, and therefore as integers, because it avoids rounding errors.</p> <p>Great articles btw! :)</p> </div> <div class="comment-date">2017-10-20 7:09 UTC</div> </div> <div class="comment" id="1f68a269e47c47bb951de3a3e2101309"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1f68a269e47c47bb951de3a3e2101309">#</a></div> <div class="comment-content"> <p> Hrvoje, thank you for writing. Yes, it's a good point that you could model the values as cents and rappen, but I think I recall that Kent Beck's text distinctly discusses <em>dollars</em> and <em>francs</em>. I am, however, currently travelling, without access to the book, so I can't check. </p> <p> The scenario, as simplistic as it may be, involves currency exchange, and exchange rates tend to involve much smaller fractions. As an example, right now, one currency exchange web site reports that 1 CHF is 1.01950 USD. Clearly, representing the U.S. currency with cents would incur a loss of precision, because that would imply an exchange rate of 102 cents to 100 rappen. I'm sure arbitrage opportunities would be legion if you ever wrote code like that. </p> <p> If I remember number theory correctly, you can always scale any rational number to an integer. I.e. in this case, you could scale 1.01950 to 101,950. There's little reason to do that, because you have the <code>decimal</code> struct for that purpose: <blockquote> <a href="https://docs.microsoft.com/en-us/dotnet/api/system.decimal">"The Decimal value type is appropriate for financial calculations that require large numbers of significant integral and fractional digits and no round-off errors."</a> </blockquote> All of this, however, is just idle speculation on my point. I admit that I've never had to implement complex financial calculations, so there may be some edge cases of which I'm not aware. For all the run-of-the-mill eCommerce and payment solutions I've implemented over the years, <code>decimal</code> has always been more than adequate. </p> </div> <div class="comment-date">2017-10-20 8:14 UTC</div> </div> <div class="comment" id="8473296ae4474a9d8a38fec2ff661338"> <div class="comment-author"><a href="https://www.interact-sw.co.uk/iangblog/">Ian Griffiths</a> <a href="#8473296ae4474a9d8a38fec2ff661338">#</a></div> <div class="comment-content"> <p> Although exchange rates are typically represented as decimal fractions, it does not follow that amounts of money should be, even if the amounts were determined by calculations involving that exchange rate. </p> <p> The oversimplified representation of foreign exchange (FX) in Kent Beck's money examples has always struck me as a particularly weak aspect (and not simply because they are integers; that's the least of the problems). You could argue that the very poor modelling of FX is tolerable because that aspect of the problem domain is not the focus in his example. But I think it's problematic because it can lead you to the wrong conclusion about the design of the central parts of the model. Your conclusion that it might be a good idea not to represent a money amount as an integer is an example - I believe it's the wrong conclusion, and that you've been led to it by the completely wrong-headed way his example represents FX. </p> <p> The nature of foreign exchange is that it is a transaction with a third party. Some entity (perhaps a bank, or the FX trading desk within an company that may or may not be a financial institution (large multinational firms sometimes have their own FX desks) or maybe a friend who has some of the kind of currency you need in her purse) agrees to give you a specific amount of one currency if you give them a specific amount of some other currency, and there is usually an accompanying agreement on the timescale in which the actual monies are to be transferred. (There will sometimes be more than two currencies involved, either because you're doing something complex, or just because you agree to pay a commission fee in some currency that is different from either the 'to' or 'from' currency.) The amounts of actual money that changes hands will invariably be some integer multiple of the smallest available denomination of the currencies in question. </p> <p> There may well be a published exchange rate. It might even form part of some contract, although such an advertised rate is very often not binding because markets can move fast, and the exchange rate posted when you started negotiation could change at any moment, and might not be available by the time you attempt to reach an agreement. In cases where a published exchange rate has some reliable meaning, it will necessarily come with a time limit (and unless this time limit is pretty short, the time window itself may come at a price - if someone has agreed to sell you currency for a specific price within some time window, what you have there is in effect either a future or an option, depending on whether you are allowed to decide not to complete the transaction). </p> <p> One very common case where a 'current' exchange rate does in fact apply is when using a credit or debit card abroad. In this case, somewhere in the terms and conditions that you agreed to at some point in the past, it will say that the bank gets to apply the current rate for some definition of current. (The bank will generally have freedom to define what it means by 'current', which is one of the reasons you tend not to get a very good deal on such transactions.) And there will be rules (often generally accepted conventions, instead of being explicitly set out in the contract) about how the rate is applied. It will necessarily involve some amount of rounding. When you bought something on your credit card in a foreign currency, it will have been for a precise amount in that currency - merchants don't get to charge you Pi dollars for something. And when the bank debits your account, they will also do so by a precise amount - if you've ever used a card in this way you'll know that you didn't end up with some fractional number of cents or pennies or whatever in your account afterwards. So the exchange rate you got in practice will very rarely be exactly the advertised one (unless it's such a large transaction that the amounts involved have more decimal places than the 'current' exchange rate, or, by sheer coincidence, the numbers worked out in such a way that you happened to get the exact exchange rate advertised.). </p> <p> So although you will often see published exchange rates with multiple decimal places, the actual exchange rate depends entirely on the agreement you strike with whoever it is that is going to give you money in the currency you want in exchange for money in the currency you have. The actual exchanges that result from such agreements do not involve fractional amounts. </p> <p> Where does this leave Kent's example? Fundamentally, 'reducing' a multi-currency expression to a single-currency result will need to create at least one FX transaction (possibly several). So you'll need some sort of mechanism for agreeing the terms of those transactions with the other party or parties. And realistically you'd want to do something to minimize transaction costs (e.g., if you perform multiple USD to GBP conversions, you'll want to handle that with a single FX transaction), so you'll need some sort of logic for managing that too. It's certainly not going to be as simple as looking up the bank's rate. </p> </div> <div class="comment-date">2018-04-13 9:51 UTC</div> </div> <div class="comment" id="2488dac03ee64cd1844d2a4a88374c26"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2488dac03ee64cd1844d2a4a88374c26">#</a></div> <div class="comment-content"> <p> Ian, thank you for writing. Much of what you write about foreign exchange matches the little I know. What interested me about Kent Beck's example was that his intuition about good programming lead him to a monoidal design. </p> <p> It seems to me that your criticism mostly targets how the exchange itself is implemented, i.e. the <code>Reduce</code> method, or rather, its <code>bank</code> argument. In its current form, the <code>Bank</code> implementation is indisputably naive. </p> <p> Would a more sophisticated <code>Bank</code> implementation address some of the problems? What if, instead of calling it <code>Bank</code>, we called it <code>Exchange</code>? </p> <p> Already in its current form, the <code>Bank</code> implementation is nothing but a dictionary of exchange rates, defined by a <code>from</code> and a <code>to</code> currency. It follow that the <em>USD/CHF</em> entry isn't the same as the <em>CHF/USD</em> entry. They don't have to be each others' inverses. Doesn't this, already, enable arbitrage? </p> <p> Another change that we could add to a hypothetical more sophisticated <code>Exchange</code> class would be to subtract a fee from the returned value. Would that address one of the other concerns? </p> <p> Furthermore, we could add a time limit to each dictionary of exchange rates. </p> <p> It's not my intent to claim that such a model would be sufficient to implement an international bank's foreign exchange business, but that's not the scenario that Kent Beck had in mind. The introduction to <em>Test-Driven Development By Example</em> explicitly explains that the scenario is a bond portfolio management system. Doesn't the overall API he outlines sufficiently address that? </p> </div> <div class="comment-date">2018-04-14 9:51 UTC</div> </div> <div class="comment" id="21eae2960ebb45489e7ca1a61d021474"> <div class="comment-author"><a href="https://www.linkedin.com/in/markwiemer/">Mark Wiemer</a> <a href="#21eae2960ebb45489e7ca1a61d021474">#</a></div> <div class="comment-content"> <p> Hi Mark, thanks for the code examples here. I do have a few clarifying questions: </p> <ol> <li> When you mention the <a href="https://blog.ploeh.dk/2017/10/16/money-monoid/#b189bd6ff2704428960624b3c228c135">identity element</a>, you write, "Finally, if you multiply the identity element, you still get the identity element. Here, interestingly, <code>PlusIdentity</code> behaves similar to the identity element for multiplication (<em>1</em>)". But with multiplication, when you multiply the identity element with another factor, you get <em>the other factor</em>, not the identity element. Am I misreading you here? </li> <li> Your C# example has <code>Money Reduce(Bank bank, string to);</code>, but your Haskell example has <code>reduce :: Ord a =&gt; Map (String, a) Int -&gt; a -&gt; Expression -&gt; Int</code>. The return types here are different, right? C# returns a <code>Money</code> object. Haskell seems to return an <code>Int</code> from the code signature and sample output. Was this intentional? </li> </ol> <p> I know I'm often focused on little details, I just want to make sure it's not a sign of me misunderstanding the main concept. The rest of the article is very clear :) </p> </div> <div class="comment-date">2022-02-07 5:35 UTC</div> </div> <div class="comment" id="fbc3f8cdfebb4743a0571fefbf4654d9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fbc3f8cdfebb4743a0571fefbf4654d9">#</a></div> <div class="comment-content"> <p> Mark, thank you for writing. You're right about the first quote - it does look a little odd. The first sentence, however, looks good enough to me. The <code>Times</code> method does, indeed, return <code>this</code> - itself. The second sentence, on the other hand, looks wrong. I'm not sure what I had in mind when I wrote that four years ago, but now that you ask, it does look incorrect. It still behaves like zero. I think I'm going to strike out that sentence. Thank you for pointing that out. </p> <p> You're also right about the Haskell example. For better parity, I should have wrapped the result of <code>reduce</code> in a new <code>Expression</code> value. This is trivially possible like this: </p> <p> <pre><span style="color:#2b91af;">reduce&#39;</span>&nbsp;::&nbsp;<span style="color:blue;">Map</span>&nbsp;(<span style="color:#2b91af;">String</span>,&nbsp;<span style="color:#2b91af;">String</span>)&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Expression</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Expression</span> reduce&#39;&nbsp;bank&nbsp;to&nbsp;exp&nbsp;=&nbsp;Money&nbsp;(reduce&nbsp;bank&nbsp;to&nbsp;exp)&nbsp;to</pre> </p> <p> This new 'overload' calls the above <code>reduce</code> function and wraps the resulting <code>Int</code> in a new <code>Expression</code> value. </p> </div> <div class="comment-date">2022-02-08 9:01 UTC</div> </div> <div class="comment" id="915d063aa2264854ba51f69f160c8683"> <div class="comment-author"><a href="https://nikosbaxevanis.com/">Nikos Baxevanis</a> <a href="#915d063aa2264854ba51f69f160c8683">#</a></div> <div class="comment-content"> <p> After the article was written, a <a href="https://gitlab.haskell.org/ghc/ghc/-/wikis/proposal/semigroup-monoid">proposal to make Semigroup as a superclass of Monoid</a> came out and eventually made it into GHC 8+. The changes so that the Haskell part of the article compiles (with GHC 8+) are: </p> <p> <pre> {-# LANGUAGE CPP #-} import Data.Semigroup (stimesMonoid) #if !MIN_VERSION_base(4,11,0) import qualified Data.Semigroup as Semigroup #endif instance Monoid Expression where #if !MIN_VERSION_base(4,11,0) mappend = (Semigroup.<>) #endif mempty = MoneyIdentity instance Semigroup Expression where MoneyIdentity <> y = y x <> MoneyIdentity = x x <> y = Sum x y </pre> </p> </div> <div class="comment-date">2022-08-12 5:35 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Strings, lists, and sequences as a monoid https://blog.ploeh.dk/2017/10/10/strings-lists-and-sequences-as-a-monoid 2017-10-10T09:37:00+00:00 Mark Seemann <div id="post"> <p> <em>Strings, lists, and sequences are essentially the same monoid. An introduction for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). </p> <h3 id="25320f40cab34f9cb8dbaecad4c2e68f"> Sequences <a href="#25320f40cab34f9cb8dbaecad4c2e68f" title="permalink">#</a> </h3> <p> C# models a lazily evaluated sequence of values as <code>IEnumerable&lt;T&gt;</code>. You can combine two sequences by appending one to the other: </p> <p> <pre>xs.Concat(ys);</pre> </p> <p> Here, <code>xs</code> and <code>ys</code> are instances of <code>IEnumerable&lt;T&gt;</code>. The <a href="https://msdn.microsoft.com/en-us/library/bb302894">Concat</a> extension method concatenates two sequences together. It has the signature <code>IEnumerable&lt;T&gt; Concat&lt;T&gt;(IEnumerable&lt;T&gt;, IEnumerable&lt;T&gt;)</code>, so <a href="/2017/10/06/monoids">it's a binary operation</a>. If it's also associative and has identity, then it's a monoid. </p> <p> Sequences are associative, because the order of evaluation doesn't change the outcome. Associativity is a <em>property</em> of a monoid, so one way to demonstrate this is with <a href="https://blog.ploeh.dk/property-based-testing-intro">property-based testing</a>: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConcatIsAssociative(<span style="color:blue;">int</span>[]&nbsp;xs,&nbsp;<span style="color:blue;">int</span>[]&nbsp;ys,&nbsp;<span style="color:blue;">int</span>[]&nbsp;zs) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xs.Concat(ys).Concat(zs), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xs.Concat(ys.Concat(zs))); }</pre> </p> <p> This automated test uses <a href="https://fscheck.github.io/FsCheck">FsCheck</a> (yes, it also works from C#!) to demonstrate that <code>Concat</code> is associative. For simplicity's sake, the test declares <code>xs</code>, <code>ys</code>, and <code>zs</code> as <em>arrays</em>. This is because FsCheck natively knows how to create arrays, whereas it doesn't have built-in support for <code>IEnumerable&lt;T&gt;</code>. While you can use FsCheck's API to define how <code>IEnumerable&lt;T&gt;</code> objects should be created, I didn't want to add this extra complexity to the example. The associativity property holds for other <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> implementations of <code>IEnumerable&lt;T&gt;</code> as well. Try it, if you need to convince yourself. </p> <p> The <code>Concat</code> operation also has identity. The identity element is the empty sequence, as this FsCheck-based test demonstrates: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConcatHasIdentity(<span style="color:blue;">int</span>[]&nbsp;xs) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Enumerable</span>.Empty&lt;<span style="color:blue;">int</span>&gt;().Concat(xs), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xs.Concat(<span style="color:#2b91af;">Enumerable</span>.Empty&lt;<span style="color:blue;">int</span>&gt;())); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xs, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xs.Concat(<span style="color:#2b91af;">Enumerable</span>.Empty&lt;<span style="color:blue;">int</span>&gt;())); }</pre> </p> <p> Appending an empty sequence before or after another sequence doesn't change the other sequence. </p> <p> Since <code>Concat</code> is an associative binary operation with identity, it's a monoid. </p> <h3 id="fe283c0f890e4567b263b751ccd18041"> Linked lists and other collections <a href="#fe283c0f890e4567b263b751ccd18041" title="permalink">#</a> </h3> <p> The above FsCheck-based tests demonstrate that <code>Concat</code> is a monoid for arrays. The properties hold for all pure implementations of <code>IEnumerable&lt;T&gt;</code>. </p> <p> In <a href="https://www.haskell.org">Haskell</a>, lazily evaluated sequences are modelled as linked lists. These are lazy because all Haskell expressions are lazily evaluated by default. The monoid laws hold for Haskell lists as well: </p> <p> <pre>λ&gt; ([1,2,3] ++ [4,5,6]) ++ [7,8,9] [1,2,3,4,5,6,7,8,9] λ&gt; [1,2,3] ++ ([4,5,6] ++ [7,8,9]) [1,2,3,4,5,6,7,8,9] λ&gt; [] ++ [1,2,3] [1,2,3] λ&gt; [1,2,3] ++ [] [1,2,3]</pre> </p> <p> In Haskell, <code>++</code> is the operator that corresponds to <code>Concat</code> in C#, but the operation is normally called <em>append</em> instead of <em>concat</em>. </p> <p> In <a href="http://fsharp.org">F#</a>, linked lists are eagerly evaluated, because all F# expressions are eagerly evaluated by default. Lists are still monoids, though, because the monoid laws still hold: </p> <p> <pre>&gt; ([1; 2; 3] @ [4; 5; 6]) @ [7; 8; 9];; val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9] &gt; [1; 2; 3] @ ([4; 5; 6] @ [7; 8; 9]);; val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9] &gt; [] @ [1; 2; 3];; val it : int list = [1; 2; 3] &gt; [1; 2; 3] @ [];; val it : int list = [1; 2; 3]</pre> </p> <p> In F#, the list concatenation operator is <code>@</code>, instead of <code>++</code>, but the behaviour is the same. </p> <h3 id="6722c1885f754430ba38cec2dd042ae2"> Strings <a href="#6722c1885f754430ba38cec2dd042ae2" title="permalink">#</a> </h3> <p> Have you ever wondered why text values are called <em>strings</em> in most programming languages? After all, for most people, a string is <a href="https://en.wikipedia.org/wiki/String_(structure)">a long flexible structure made from fibres</a>. What does that have to do with text? </p> <p> In programming, text is often arranged in memory as a consecutive block of characters, one after the other. Thus, you could think of text as characters like pearls on a string. A program often reads such a consecutive block of memory until it reaches a terminator of some kind. Thus, strings of characters have an order to them. They are similar to sequences and lists. </p> <p> In fact, in Haskell, the type <code>String</code> is nothing but a synonym for <code>[Char]</code> (meaning: a list of <code>Char</code> values). Thus, anything you can do with lists of other values, you can do with <code>String</code> values: </p> <p> <pre>λ&gt; "foo" ++ [] "foo" λ&gt; [] ++ "foo" "foo" λ&gt; ("foo" ++ "bar") ++ "baz" "foobarbaz" λ&gt; "foo" ++ ("bar" ++ "baz") "foobarbaz"</pre> </p> <p> Clearly, <code>++</code> over <code>String</code> is a monoid in Haskell. </p> <p> Likewise, in .NET, <code>System.String</code> implements <code>IEnumerable&lt;char&gt;</code>, so you'd expect it to be a monoid here as well - and it <em>almost</em> is. It's certainly associative: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PlusIsAssociative(<span style="color:blue;">string</span>&nbsp;x,&nbsp;<span style="color:blue;">string</span>&nbsp;y,&nbsp;<span style="color:blue;">string</span>&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;+&nbsp;z, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;+&nbsp;(y&nbsp;+&nbsp;z)); }</pre> </p> <p> In C#, the <code>+</code> operator is actually defined for <code>string</code>, and as the FsCheck test demonstrates, it's associative. It almost also has identity. What's the equivalent of an empty list for strings? The empty string: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PlusHasIdentity(<span style="color:#2b91af;">NonNull</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;&quot;</span>&nbsp;+&nbsp;x.Get,&nbsp;x.Get&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x.Get,&nbsp;x.Get&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&quot;</span>); }</pre> </p> <p> Here, I had to tell FsCheck to avoid <code>null</code> values, because, as usual, <code>null</code> throws a big wrench into our attempts at being able to reason about the code. </p> <p> The problem here is that <code>"" + null</code> and <code>null + ""</code> both return <code>""</code>, which is not equal to the input value (<code>null</code>). In other words, <code>""</code> is not a true identity element for <code>+</code>, because of this single special case. (And by the way, <code>null</code> isn't the identity element either, because <code>null + null</code> returns... <code>""</code>! Of course it does.) This is, however, an implementation detail. As an exercise, consider writing an (extension) method in C# that makes <code>string</code> a proper monoid, even for <code>null</code> values. If you can do that, you'll have demonstrated that string concatenation is a monoid in .NET, just as it is in Haskell. </p> <h3 id="435d8062b6a7465f84c31e61a72cdd64"> Free monoid <a href="#435d8062b6a7465f84c31e61a72cdd64" title="permalink">#</a> </h3> <p> Recall that in the previous article, you learned how both addition and multiplication of numbers form monoids. There's at least one more monoid for numbers, and that's a sequence. If you have a generic sequence (<code>IEnumerable&lt;T&gt;</code>), it can contain anything, including numbers. </p> <p> Imagine that you have two numbers, 3 and 4, and you want to combine them, but you haven't yet made up your mind about <em>how</em> you want to combine them. In order to postpone the decision, you can put both numbers in a singleton array (that is, an array with a single element, not to be confused with the <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Singleton design pattern</a>): </p> <p> <pre><span style="color:blue;">var</span>&nbsp;three&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;3&nbsp;}; <span style="color:blue;">var</span>&nbsp;four&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;4&nbsp;};</pre> </p> <p> Since sequences are monoids, you can combine them: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;combination&nbsp;=&nbsp;three.Concat(four); </pre> </p> <p> This gives you a new sequence that contains both numbers. At this point, you haven't lost any information, so once you've decided how to combine the numbers, you can <em>evaluate</em> the data that you've collected so far. This is called the <a href="https://bartoszmilewski.com/2015/07/21/free-monoids">free monoid</a>. </p> <p> If you need the sum of the numbers, you can add them together: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;sum&nbsp;=&nbsp;combination.Aggregate(0,&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;y); </pre> </p> <p> (Yes, I'm aware that the <a href="https://msdn.microsoft.com/en-us/library/bb338442">Sum</a> method exists, but I want you to see the details.) This <a href="https://msdn.microsoft.com/en-us/library/bb549218">Aggregate</a> overloads takes a <code>seed</code> value as the first argument, and a function to combine two values as the second. </p> <p> Here's how to get the product: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;product&nbsp;=&nbsp;combination.Aggregate(1,&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;x&nbsp;*&nbsp;y); </pre> </p> <p> Notice how in both cases, the <code>seed</code> value is the identity for the monoidal operation: 0 for addition, and 1 for multiplication. Likewise, the aggregator function uses the binary operation associated with that particular monoid. </p> <p> I think it's interesting that this is called the free monoid, similar to <a href="/2017/07/10/pure-interactions">free monads</a>. In both cases, you collect data without initially interpreting it, and then later you can submit the collected data to one of several evaluators. </p> <h3 id="8fe0a19c16954954878862b948284db3"> Summary <a href="#8fe0a19c16954954878862b948284db3" title="permalink">#</a> </h3> <p> Various collection types, like .NET sequences, arrays, or Haskell and F# lists, are monoids over concatenation. In Haskell, strings <em>are</em> lists, so string concatenation is a monoid as well. In .NET, the <code>+</code> operator for strings is a monoid if you pretend that <code>null</code> strings don't exist. Still, all of these are essentially variations of the same monoid. </p> <p> It makes sense that C# uses <code>+</code> for string concatenation, because, as the <a href="/2017/10/06/monoids">previous article</a> described, addition is the most intuitive and 'natural' of all monoids. Because you know first-grade arithmetic, you can immediately grasp the concept of addition as a metaphor. A monoid, however, is more than a metaphor; it's an abstraction that describes well-behaved binary operations, where one of those operations just happen to be addition. It's a <em>generalisation</em> of the concept. It's an abstraction that you already understand. </p> <p> <strong>Next: </strong> <a href="/2017/10/16/money-monoid">Money monoid</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="babede1c1fc0496f874b123eb0ee7063"> <div class="comment-author"><a href="http://lestard.eu/">Manuel Mauky</a> <a href="#babede1c1fc0496f874b123eb0ee7063">#</a></div> <div class="comment-content">Hi, I have a question regarding the free monoid part. Can you concretize what exactly "is called the free monoid"? What I understand is that in your example the `Aggregate` method basically gets a monoid as argument (first the identity element and second the operation). Is `Aggregate` a free monoid here? If so, is `Aggregate` the only possible free monoid for this data type or are there other examples of free monoids for numbers? Or is the two-element sequence `combination` the free monoid? Is "free monoid" a special sort of monoids? <br> Thanks for this article series! Best regards, Manuel </div> <div class="comment-date">2017-11-15 17:33 UTC</div> </div> <div class="comment" id="9618db368ee941e69cc94db64e086782"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9618db368ee941e69cc94db64e086782">#</a></div> <div class="comment-content"> <p> Manuel, thank you for writing. The confusion is entirely caused by my sloppy writing. A monoid is an associative binary operation with identity. Since the free monoid essentially elevates each number to a singleton list, the binary operation in question is <em>list concatenation</em>. </p> <p> The <code>Aggregate</code> method is a built-in BCL method that aggregates values. I'll have more to say about that in later articles, but aggregation in itself is not a monoid; it follows from monoids. </p> <p> I've yet to find a source that explains the etymology of the 'free' terminology, but as far as I can tell, free monoids, as well as free monads, are instances of a particular abstraction that you 'get for free', so to speak. You can <em>always</em> put values into singleton lists, just like you can <em>always</em> create a free monad from any functor. These instances are lossless in the sense that performing operations on them never erase data. For the free monoid, you just keep on concatenating more values to your list of values. </p> <p> This decouples the collection of data from evaluation. Data collection is lossless. Only when you want to evaluate the result must you decide on a particular type of evaluation. For integers, for example, you could choose between addition and multiplication. Once you perform the evaluation, the result is lossy. </p> <p> In Haskell, the <code>Data.Monoid</code> module defines an <code>&lt;&gt;</code> infix operator that you can use as the binary operation associated with a particular type. For lists, you can use it like this: </p> <p> <pre>Prelude Data.Monoid Data.Foldable&gt; xs = [3] &lt;&gt; [4] &lt;&gt; [5] Prelude Data.Monoid Data.Foldable&gt; xs [3,4,5]</pre> </p> <p> Notice how the operation isn't lossy. This means you can defer the decision on how to evaluate it until later: </p> <p> <pre>Prelude Data.Monoid Data.Foldable&gt; getSum $ fold $ Sum &lt;$&gt; xs 12 Prelude Data.Monoid Data.Foldable&gt; getProduct $ fold $ Product &lt;$&gt; xs 60</pre> </p> <p> Notice how you can choose to evaluate <code>xs</code> to calculate the sum, or the product. </p> </div> <div class="comment-date">2017-11-16 15:56 UTC</div> </div> <div class="comment" id="084172b224c94ea5b9c7dd6ea404d6e3"> <div class="comment-author"><a href="https://github.com/giacomociti">Giacomo Citi</a> <a href="#084172b224c94ea5b9c7dd6ea404d6e3">#</a></div> <div class="comment-content"> <p> I think the word <em>free</em> is used in algebraic structures to suggest that all possible interpretations are left open. This is because they are not constrained by additional specific laws which would allow to further evaluate (reduce, simplify) expressions. </p> <p> For example, <pre>2+0</pre> can be simplified to <pre>2</pre> due to Monoid laws (identity) while <pre>2+3</pre> can be reduced to <pre>5</pre> due to specific arithmetic laws. </p> <p> Freedom from further constraints also mean that we can always devise automatically (hence free as in <q>free beer</q>) an instance from a signature. This construction is called <a href="https://en.wikipedia.org/wiki/Term_algebra/">term algebra</a>; its values are essentially the syntactic structures (AST) of the expressions allowed by the signature and the sole simplifications permitted are those specified by the general laws. </p> <p> In the case of a Monoid, thanks to associativity (which is a Monoid law, not specific to any particular instance), if we consider complex expressions like <pre>(1+3)+2</pre> we can flatten their AST to a list <pre>[1,3,2]</pre> without losing information and still without committing yet to any specific interpretation. And for atomic expressions like <pre>3</pre> the single node AST becomes a singleton list. </p> </div> <div class="comment-date">2017-11-23 19:52 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Monoids https://blog.ploeh.dk/2017/10/06/monoids 2017-10-06T07:38:00+00:00 Mark Seemann <div id="post"> <p> <em>Introduction to monoids for object-oriented programmers.</em> </p> <p> This article is part of <a href="/2017/10/05/monoids-semigroups-and-friends">a larger series about monoids, semigroups, and related concepts</a>. In this article, you'll learn what a monoid is, and what distinguishes it from a semigroup. </p> <p> <img src="/content/binary/monoids-subset-of-semigroups.png" alt="Monoids are a subset of semigroups."> </p> <p> Monoids form a subset of semigroups. The rules that govern monoids are stricter than those for semigroups, so you'd be forgiven for thinking that it would make sense to start with semigroups, and then build upon that definition to learn about monoids. From a strictly hierarchical perspective, that would make sense, but I think that monoids are more intuitive. When you see the most obvious monoid example, you'll see that they cover operations from everyday life. It's easy to think of examples of monoids, while you have to think harder to find some good semigroup examples. That's the reason I think that you should start with monoids. </p> <h3 id="20a3fb75b8f54a088d0a736351d96072"> Monoid laws <a href="#20a3fb75b8f54a088d0a736351d96072" title="permalink">#</a> </h3> <p> What do addition (<code>40 + 2</code>) and multiplication (<code>6 * 7</code>) have in common? </p> <p> They're both <ul> <li>associative</li> <li>binary operations</li> <li>with a neutral element.</li> </ul> That's all it takes to form a monoid. Associativity and the existence of a neutral element is sometimes referred to as the <em>monoid laws</em>. It's worth noting that a monoid is a combination of a data type (or set) and an operation. It's not a data type in itself, but rather a function (or method) that operates on that data type. For example, addition and multiplication are two different monoids that both work on numbers. </p> <h3 id="433ca2a2b7e74e028f943d8bd680c2db"> Binary operation <a href="#433ca2a2b7e74e028f943d8bd680c2db" title="permalink">#</a> </h3> <p> Let's start with the most basic property. That an operation is <em>binary</em> means that it works on <em>two</em> values. Perhaps you mostly associate the word <em>binary</em> with binary numbers, such as 101010, but the word originates from Latin and means something like <em>of two</em>. Astronomers talk about <em>binary stars</em>, but the word is dominantly used in computing context: apart from binary numbers, you may also have heard about binary trees. When talking about binary operations, it's implied that both input values are of the same type, and that the return type is the same as the input type. In other words, a C# method like this is a proper binary operation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Op(<span style="color:#2b91af;">Foo</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;y) </pre> </p> <p> Sometimes, if <code>Op</code> is an instance method on the <code>Foo</code> class, it can also look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Op&nbsp;(<span style="color:#2b91af;">Foo</span>&nbsp;foo) </pre> </p> <p> On the other hand, this isn't a binary operation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Baz</span>&nbsp;Op(<span style="color:#2b91af;">Foo</span>&nbsp;f,&nbsp;<span style="color:#2b91af;">Bar</span>&nbsp;b) </pre> </p> <p> Although it takes two input arguments, they're of different types, and the return type is a third type. </p> <p> Since all involved arguments and return values are of the same type, a binary operation exhibits what Eric Evans in <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> calls <em>Closure of Operations</em>. </p> <h3 id="12111ce32bb74a0a82543276787ce7de"> Associative <a href="#12111ce32bb74a0a82543276787ce7de" title="permalink">#</a> </h3> <p> In order to form a monoid, the binary operation must be <em>associative</em>. This simply means that the order of evaluation doesn't matter. For example, for addition, it means that </p> <p> <pre>(2 + 3) + 4 = 2 + (3 + 4) = 2 + 3 + 4 = 9</pre> </p> <p> Likewise, for multiplication </p> <p> <pre>(2 * 3) * 4 = 2 * (3 * 4) = 2 * 3 * 4 = 24</pre> </p> <p> Expressed as the above <code>Op</code> instance method, associativity would require that <code>areEqual</code> is <code>true</code> in the following code: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;areEqual&nbsp;=&nbsp;foo1.Op(foo2).Op(foo3)&nbsp;==&nbsp;foo1.Op(foo2.Op(foo3)); </pre> </p> <p> On the left-hand side, <code>foo1.Op(foo2)</code> is evaluated first, and the result then evaluated with <code>foo3</code>. On the right-hand side, <code>foo2.Op(foo3)</code> is evaluated first, and then used as an input argument to <code>foo1.Op</code>. Since the left-hand side and the right-hand side are compared with the <code>==</code> operator, associativity requires that <code>areEqual</code> is <code>true</code>. </p> <p> In C#, if you have a custom monoid like <code>Foo</code>, you'll have to override <code>Equals</code> and implement the <code>==</code> operator in order to make all of this work. </p> <h3 id="33d7cca9a8f34cf482d9ced07856c735"> Neutral element <a href="#33d7cca9a8f34cf482d9ced07856c735" title="permalink">#</a> </h3> <p> The third rule for monoids is that there must exist a neutral value. In the normal jargon, this is called the <em>identity</em> element, and this is what I'm going to be calling it from now on. I only wanted to introduce the concept using a friendlier name. </p> <p> The identity element is a value that doesn't 'do' anything. For addition, for example, it's zero, because adding zero to a value doesn't change the value: </p> <p> <pre>0 + 42 = 42 + 0 = 42</pre> </p> <p> As an easy exercise, see if you can figure out the identity value for multiplication. </p> <p> As implied by the above sum, the identity element must act neutrally both when applied to the left-hand side and the right-hand side of another value. For our <code>Foo</code> objects, it could look like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;hasIdentity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Foo</span>.Identity.Op(foo)&nbsp;==&nbsp;foo.Op(<span style="color:#2b91af;">Foo</span>.Identity)&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;foo.Op(<span style="color:#2b91af;">Foo</span>.Identity)&nbsp;==&nbsp;foo;</pre> </p> <p> Here, <code>Foo.Identity</code> is a static read-only field of the type <code>Foo</code>. </p> <h3 id="0ed3ca9c70344fb7afce27f988202f21"> Examples <a href="#0ed3ca9c70344fb7afce27f988202f21" title="permalink">#</a> </h3> <p> There are plenty of examples of monoids. The most obvious examples are addition and multiplication, but there are more. Depending on your perspective, you could even say that there's more than one addition monoid, because there's one for integers, one for real numbers, and so on. The same can be said for multiplication. </p> <p> There are also two monoids over boolean values called <em>all</em> and <em>any</em>. If you have a binary operation over boolean values called <em>all</em>, how do you think it works? What would be the identity value? What about <em>any?</em> </p> <p> I'll leave you to ponder (or look up) <em>all</em> and <em>any</em>, and instead, in the next articles, show you some slightly more interesting monoids. <ul> <li><a href="/2018/07/16/angular-addition-monoid">Angular addition monoid</a></li> <li><a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">Strings, lists, and sequences as a monoid</a></li> <li><a href="/2017/10/16/money-monoid">Money monoid</a></li> <li><a href="/2017/10/23/convex-hull-monoid">Convex hull monoid</a></li> <li><a href="/2017/10/30/tuple-monoids">Tuple monoids</a></li> <li><a href="/2017/11/06/function-monoids">Function monoids</a></li> <li><a href="/2017/11/13/endomorphism-monoid">Endomorphism monoid</a></li> <li><a href="/2018/04/03/maybe-monoids">Maybe monoids</a></li> <li><a href="/2019/04/15/lazy-monoids">Lazy monoids</a></li> <li><a href="/2017/11/20/monoids-accumulate">Monoids accumulate</a></li> </ul> In essence, if you have a data type that 'behaves like a number', you can probably make it a monoid. Addition is often the best candidate, because it doesn't mess with the dimensions of what you're keeping track of. As an example, the .NET Base Class Library defines a <a href="https://msdn.microsoft.com/en-us/library/system.timespan">TimeSpan structure</a> with an <a href="https://msdn.microsoft.com/en-us/library/system.timespan.add">Add method</a>. It also comes with a <code>==</code> operator. On the other hand, there's no <code>Multiply</code> method for <code>TimeSpan</code>, because what does it mean to multiply two durations? What would the dimension be? <a href="https://en.wikipedia.org/wiki/Time_Squared_(Star_Trek:_The_Next_Generation)">Time squared</a>? </p> <h3 id="3b10b1afa0d0469392bf89fec90ea371"> Summary <a href="#3b10b1afa0d0469392bf89fec90ea371" title="permalink">#</a> </h3> <p> A monoid (not to be confused with a monad) is a set (a type) equipped with a binary operation that satisfies the two monoid laws: that the operation is associative, and that an identity element exists. Addition and multiplication are prime examples, but several others exist. </p> <p> (By the way, the identity element for multiplication is <em>one</em> (<em>1</em>), the <em>all</em> monoid is boolean <em>and</em>, and the <em>any</em> monoid is boolean <em>or</em>.) </p> <p> <strong>Next: </strong> <a href="/2018/07/16/angular-addition-monoid">Angular addition monoid</a> </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="2ab261e0a1324dfa8cb28dec51a9e8ca"> <div class="comment-author"> <a href="https://github.com/khaledh">Khaled Hammouda</a> <a href="#2ab261e0a1324dfa8cb28dec51a9e8ca">#</a></div> <div class="comment-content"> <p>Great series! I'm a big fan of intuitive abstractions and composition. Can't wait for the remaining parts.</p> <p>I first heard of the <i>closure property</i> in <a href="http://sarabander.github.io/sicp/html/2_002e2.xhtml#g_t2_002e2">SICP</a>, where it's mentioned that: <blockquote>In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can themselves be combined using the same operation.</blockquote> Also, a reference to the algebraic origin of this concept is made in the foot note for this sentence: <blockquote>The use of the word "closure" here comes from abstract algebra, where a set of elements is said to be closed under an operation if applying the operation to elements in the set produces an element that is again an element of the set.</blockquote> </p> <p>It's interesting to see this concept come up over and over, although it hasn't been widely socialized as a formal construct to software composition.</p> </div> <div class="comment-date">2017-10-06 15:38 UTC</div> </div> <div class="comment" id="55c1d75e2de44a5fafac3647b91ca2e2"> <div class="comment-author"> <a href="http://kearon.blogspot.co.uk/">Sean Kearon</a> <a href="#55c1d75e2de44a5fafac3647b91ca2e2">#</a></div> <div class="comment-content"> <p>This looks like it's going to be a fantastic series - I'm really looking forwards to reading the rest!</p> <p>So, as we are talking about <a href="/2017/10/04/from-design-patterns-to-category-theory/">forming a vocabulary and reducing ambiguity</a>, I have a question about the use of the word <i>closure</i>, which I think has more than one common meaning in this context.</p> <p>In Eric Evans' "Closure of Operations", <i><i>closure</i></i> refers to the fact that the operation is "closed" over it's set of possible values - in other words, the set is closed under the operation.</p> <p><i>Closure</i> is also used to describe a <a href="https://simple.wikipedia.org/wiki/Closure_(computer_science)">function with a bound value</a> (as in the <a href="http://wiki.c2.com/?ClosuresAndObjectsAreEquivalent">poor man's object"</a>).</p> <p>These are two separate concepts as far as I am aware. Also, I suspect that the latter meaning is likely more well known to C# devs reading this series, especially ReSharper users who have come across it's "implicitly captured closure" detection. So, if I am correct, do you think it is worth making this distinction clear to avoid potential confusion?</p> </div> <div class="comment-date">2017-10-18 07:30 UTC</div> </div> <div class="comment" id="7b992d0884cf45bdb96d7cb57584082e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7b992d0884cf45bdb96d7cb57584082e">#</a></div> <div class="comment-content"> <p> Sean, thank you for writing. That's a great observation, and one that I frankly admit that I hadn't made myself. In an ideal world, one of those concepts would have a different name, so that we'd be able to distinguish them from each other. </p> <p> In my experience, I find that the context in which I'm using those words tend to make the usage unambiguous, but I think that you have a good point that some readers may be more familiar with <em>closure</em> as a captured outer value, rather than the concept of an operation where the domain and the codomain is the same. I'll see if I can make this clearer when I revisit Evans' example. </p> </div> <div class="comment-date">2017-10-18 12:02 UTC</div> </div> <div class="comment" id="d5940c43c69b4e8987c27bf5321d1395"> <div class="comment-author"><a href="https://github.com/vitrun">Vitrun</a> <a href="#d5940c43c69b4e8987c27bf5321d1395">#</a></div> <div class="comment-content"> <p>I'm recently learning category theory, and happened to read this blog. Great post! I'll follow up the series.</p> <p>I find it a little confusing:</p> <p> <blockquote>(By the way, the identity element for multiplication is one (1), all is boolean and, and any is boolean or.)</blockquote> </p> <p>Identity element should be the element of the collection rather than operation, right? So, the id for all should be True, and that of any should be False.</p> </div> <div class="comment-date">2017-12-03 03:36 UTC</div> </div> <div class="comment" id="30cc6dd090a64d48aff8d98260d84c91"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#30cc6dd090a64d48aff8d98260d84c91">#</a></div> <div class="comment-content"> <p> Vitrun, thank you for writing. Yes, the identity for <em>any</em> is <em>false</em>, and for <em>all</em> it's <em>true</em>. There are two other monoids over Boolean values. Can you figure out what they are? </p> <p> I don't understand this: <blockquote>"Identity element should be the element of the collection rather than operation"</blockquote> Can you elaborate what you mean by that? </p> </div> <div class="comment-date">2017-12-03 18:56 UTC</div> </div> <div class="comment" id="8817d35816c640bc958096d9f8336228"> <div class="comment-author"><a href="https://github.com/vitrun">Vitrun</a> <a href="#8817d35816c640bc958096d9f8336228">#</a></div> <div class="comment-content"> <p>A monoid is a sequence (M, e, ⋆), where M is a set, e ∈ M is the identity, and ⋆ is the function/operator.</p> <p>To be clear. I mean, the identity should be the element of the set, rather than the operator</p> <p>Are the other two <em>and</em> and <em>or</em>?</p> <p>I found you good at bridging the gap between programming practice and ivory-tower concepts. How do you do that?</p> </div> <div class="comment-date">2017-12-04 04:25 UTC</div> </div> <div class="comment" id="745bc9069ae245298aaa7ba3630aa91a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#745bc9069ae245298aaa7ba3630aa91a">#</a></div> <div class="comment-content"> <p> Vitrun, thank you for your kind words. I don't know if I have a particular way of 'bridging the gap'; I try to identify patterns in the problems I run into, and then communicate those patterns in as simple a language as I can, with as helpful examples as I can think of... </p> <p> <blockquote>the identity should be the element of the set</blockquote> Yes. </p> <p> Regarding monoids over Boolean values, <em>any</em> is another name for Boolean <em>or</em>, and <em>all</em> is another name for Boolean <em>and</em>. That's two monoids (<em>any</em> and <em>all</em>); in addition to those, there are two more monoids over Booleans. I could tell you what they are, but it's a good exercise if you can figure them out by yourself. If not, you can easily Google them. </p> </div> <div class="comment-date">2017-12-05 08:27 UTC</div> </div> <div class="comment" id="a34f2476e7ad46069621acd46e814380"> <div class="comment-author"><a href="https://jeremiahflaga.github.io">Jboy Flaga</a> <a href="#a34f2476e7ad46069621acd46e814380">#</a></div> <div class="comment-content"> <p> Hi Mark. Thank you for these articles. </p> <p> Are the other two boolean monoids <em>not</em> and <em>xor</em>? ... And the identity value for <em>not</em> is the input value. And the identity value for <em>xor</em> is any of the two input values. I did not google for them. I will just wait for your answer so that there will be thrill, and so I remember what the answer is :) </p> </div> <div class="comment-date">2018-03-06 04:28 UTC</div> </div> <div class="comment" id="4bb9ec869ba148e08b609595dd7237ab"> <div class="comment-author"><a href="https://jeremiahflaga.github.io">Jboy Flaga</a> <a href="#4bb9ec869ba148e08b609595dd7237ab">#</a></div> <div class="comment-content"> <p> I just realized that <em>not</em> is not a monoid because it does not operate on two values hehe. Sorry about that. </p> </div> <div class="comment-date">2018-03-06 07:18 UTC</div> </div> <div class="comment" id="75bc5917e44e45a69f33c0d3ea3ce2b0"> <div class="comment-author"><a href="https://jeremiahflaga.github.io">Jboy Flaga</a> <a href="#75bc5917e44e45a69f33c0d3ea3ce2b0">#</a></div> <div class="comment-content"> <p> I googled it already :) </p> <p> I gave answers too soon. I just realized that I was confused about the definition of an <em>identity</em> value. </p> <p> This is another lesson for me to read a technical writing at least two or three times before thinking that I already understood it. </p> </div> <div class="comment-date">2018-03-07 06:17 UTC</div> </div> <div class="comment" id="f609685def5e4f8c9f0ba6a4693cf0c7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f609685def5e4f8c9f0ba6a4693cf0c7">#</a></div> <div class="comment-content"> <p> Jeremiah, thank you for writing, and please accept my apologies that I didn't respond right away. Not only do I write technical content, but I also read a fair bit of it, and my experience is that I often have to work with the topic in question in order to fully grasp it. Reading a text more than once is one way of doing it. When it comes to Boolean monoids, another way is to draw up some truth tables. A third way would be to simply play with Boolean expressions in your programming language of choice. Whatever it takes; if you learned something, then I'm happy. </p> </div> <div class="comment-date">2018-03-07 08:15 UTC</div> </div> <div class="comment" id="a8592dfc91b94bbd8e3656ceaacd4bb5"> <div class="comment-author"><a href="https://www.linkedin.com/in/markwiemer/">Mark Wiemer</a> <a href="#a8592dfc91b94bbd8e3656ceaacd4bb5">#</a></div> <div class="comment-content"> <p> Thanks for this great series. I know you've specified twice that a monoid is a set equipped with a binary operation, and that's consistent with other sources. However, I'm confused when you say addition and multiplication are monoids. Is it more technically correct to say "the set of integers under addition is a monoid"? </p> </div> <div class="comment-date">2022-02-03 04:10 UTC</div> </div> <div class="comment" id="425e4785aa494e078b2fbb8b416700f9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#425e4785aa494e078b2fbb8b416700f9">#</a></div> <div class="comment-content"> <p> Mark, thank you for writing. It's almost impossible to be explicitly correct all the time when discussing matters like these. It'd tend to make the text verbose, bordering on unreadable. I'm no mathematician, but I think that you're right that it's technically more correct to say that the set of integers under addition forms a monoid, or gives rise to a monoid. </p> <p> If you were to insist on precision, however, then I believe that your formulation is also incorrect. Mathematically, monoids are triples consisting of 1. a set, 2. a binary, associative operation, and 3. an identity. You didn't mention the identity, which under addition is zero. </p> <p> I'm not writing this to insist that 'you're wrong, and therefore I'm right'. My language is imprecise, too. I do point this out, however, to highlight just how difficult it is to be absolutely precise when discussing such concepts in prose. </p> <p> Other aspects to be aware of are these: <ul> <li>Addition also gives rise to a monoid on the set of rational numbers and real numbers. On complex numbers, too, I think...</li> <li>In programming, addition isn't really addition because of overflow/underflow. Exceptions to that rule are unbounded numbers like .NET's <a href="https://docs.microsoft.com/dotnet/api/system.numerics.biginteger">BigInteger</a> or Haskell's <a href="https://hackage.haskell.org/package/base/docs/GHC-Integer.html">Integer</a> and <a href="https://hackage.haskell.org/package/base/docs/Prelude.html#t:Rational">Rational</a>. (Interestingly, even in the face of overflow, 'addition' may still form a monoid, but I'll leave that as an exercise.)</li> <li>Floating-point arithmetic, as yet another consideration, in general requires some hand-waving. As far as I recall, floating-point addition isn't associative. Is it really <em>addition</em>, then? In practice, however, we still call it <em>addition</em>. Would we say that it forms a monoid?</li> </ul> The point that I'm trying to make is that some abstract concepts are useful in programming, even though, mathematically speaking, they may not quite fit the bill. </p> <p> Being precise in language can be useful when trying to learn an unfamiliar concept, but my experience with writing this series of articles is that I've been unable to keep up the rigour in my prose at all times. </p> </div> <div class="comment-date">2022-02-03 07:15 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Monoids, semigroups, and friends https://blog.ploeh.dk/2017/10/05/monoids-semigroups-and-friends 2017-10-05T08:24:00+00:00 Mark Seemann <div id="post"> <p> <em>Introduction to monoids, semigroups, and similar concepts, for object-oriented programmers.</em> </p> <p> This article series is part of <a href="/2017/10/04/from-design-patterns-to-category-theory">an even larger series of articles about the relationship between design patterns and category theory.</a> </p> <p> Functional programming has often been criticised for its abstruse jargon. Terminology like <em>zygohistomorphic prepromorphism</em> doesn't help sell the message, but before we start throwing stones, we should first exit our own glass house. In object-oriented design, we have names like <a href="https://en.wikipedia.org/wiki/Bridge_pattern">Bridge</a>, <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a>, <a href="https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)">SOLID</a>, <a href="https://en.wikipedia.org/wiki/Cohesion_(computer_science)">cohesion</a>, and so on. The words sound familiar, but can you actually explain or implement the Visitor design pattern, or characterise cohesion? </p> <p> That <em>Bridge</em> is a word you know doesn't make object-oriented terminology better. Perhaps it even makes it worse. After all, now the word has become ambiguous: did you mean a physical structure connecting two places, or are you talking about the design pattern? Granted, in practical use, it will often be clear from the context, but it doesn't change that if someone talks about the Bridge pattern, you'll have no idea what it is, unless you've actually learned it. Thus, that the word is familiar doesn't make it better. </p> <p> More than one object-oriented programmer have extolled the virtues of 'operations whose return type is the same as the type of its argument(s)'. Such vocabulary, however, is inconvenient. Wouldn't it be nice to have a single, well-defined word for this? Perhaps <em>monoid</em>, or <em>semigroup?</em> </p> <h3 id="7abd9ab636814b2d87e0165998461da2"> Object-oriented hunches <a href="#7abd9ab636814b2d87e0165998461da2" title="permalink">#</a> </h3> <p> In <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>, Eric Evans discusses the notion of <em>Closure of Operations</em>, that is, operations "whose return type is the same as the type of its argument(s)." In C#, it could be a method with the signature <code>public Foo Bar(Foo f1, Foo f2)</code>. This method takes two <code>Foo</code> objects as input, and returns a new <code>Foo</code> object as output. </p> <p> As Evans points out, object designs with that quality begins to look like arithmetic. If you have an operation that takes two <code>Foo</code> and returns a <code>Foo</code>, what could it be? Could it be like addition? Multiplication? Another mathematical operation? </p> <p> Some <a href="/2012/12/18/RangersandZookeepers">enterprise developers</a> just 'want to get stuff done', and don't care about mathematics. To them, the value of making code more mathematical is disputable. Still, even if you 'don't like maths', you understand addition, multiplication, and so on. Arithmetic is a powerful metaphor, because all programmers understand it. </p> <p> In his book <a href="http://bit.ly/tddbe">Test-Driven Development: By Example</a>, Kent Beck <a href="/2017/10/16/money-monoid">seems to have the same hunch</a>, although I don't think he ever explicitly calls it out. </p> <p> What Evans describes are monoids, semigroups, and similar concepts from abstract algebra. To be fair, I recently had the opportunity to discuss the matter with him, and he's perfectly aware of those concepts today. Whether he was aware of them when he wrote DDD in 2003 I don't know, but I certainly wasn't; my errand isn't to point fingers, but to point out that clever people have found this design principle valuable in object-oriented design long before they gave it a distinct name. </p> <h3 id="a79adfb65c234c1bb9e2039d6c60d1b1"> Relationships <a href="#a79adfb65c234c1bb9e2039d6c60d1b1" title="permalink">#</a> </h3> <p> Monoids and semigroups belong to a larger group of operations called <em>magmas</em>. You'll learn about those later, but we'll start with monoids, move on to semigroups, and then explore other magmas. All monoids are semigroups, while the inverse doesn't hold. In other words, monoids form a subset of semigroups. </p> <p> <img src="/content/binary/magmas-quasigroups-semigroups-monoids.png" alt="Monoids are a subset of semigroups, and part of the larger magma set."> </p> <p> All magmas describe binary operations of the form: an operation takes two <code>Foo</code> values as input and returns a <code>Foo</code> value as output. Both categories are governed by (intuitive) laws. The difference is that the laws governing monoids are stricter than the laws governing semigroups. Don't be put off by the terminology; 'law' may sound like you have to get involved in complicated maths, but these laws are simple and intuitive. You'll learn them as you read on. <ul> <li><a href="/2017/10/06/monoids">Monoids</a> <ul> <li><a href="/2018/07/16/angular-addition-monoid">Angular addition monoid</a></li> <li><a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">Strings, lists, and sequences as a monoid</a></li> <li><a href="/2017/10/16/money-monoid">Money monoid</a></li> <li><a href="/2017/10/23/convex-hull-monoid">Convex hull monoid</a></li> <li><a href="/2017/10/30/tuple-monoids">Tuple monoids</a></li> <li><a href="/2017/11/06/function-monoids">Function monoids</a></li> <li><a href="/2017/11/13/endomorphism-monoid">Endomorphism monoid</a></li> <li><a href="/2018/04/03/maybe-monoids">Maybe monoids</a></li> <li><a href="/2019/04/15/lazy-monoids">Lazy monoids</a></li> <li><a href="/2017/11/20/monoids-accumulate">Monoids accumulate</a></li> </ul> </li> <li><a href="/2017/11/27/semigroups">Semigroups</a> <ul> <li><a href="/2017/12/04/bounding-box-semigroup">Bounding box semigroup</a></li> <li><a href="/2017/12/11/semigroups-accumulate">Semigroups accumulate</a></li> </ul> </li> <li><a href="/2017/12/18/quasigroups">Quasigroups</a></li> <li><a href="/2017/12/27/magmas">Magmas</a> <ul> <li><a href="/2017/12/28/rock-paper-scissors-magma">Rock Paper Scissors magma</a></li> <li><a href="/2018/01/02/colour-mixing-magma">Colour-mixing magma</a></li> </ul> </li> </ul> While they seem firmly rooted in mathematics, these categories offer insights into object-oriented design as well. More on that later. </p> <h3 id="aec4792d19b14f2aa6586479a91bb05a"> Summary <a href="#aec4792d19b14f2aa6586479a91bb05a" title="permalink">#</a> </h3> <p> To the average object-oriented programmer, terms like <em>monoid</em> and <em>semigroup</em> smell of mathematics, academia, and ivory-tower astronaut architects, but they're plain and simple concepts that anyone can understand, if they wish to invest 15 minutes of their time. </p> <p> Whether or not an object is a <em>magma</em> tells us whether Evans' <em>Closure of Operations</em> is possible. It might teach us other things about our code, as well. </p> <p> <strong>Next: </strong> <a href="/2017/10/06/monoids">Monoids</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="539bf9c9cc3c4eb8a33b9d1a7b5c8229"> <div class="comment-author"> <a href="https://megakemp.com">Enrico Campidoglio</a> <a href="#539bf9c9cc3c4eb8a33b9d1a7b5c8229">#</a></div> <div class="comment-content"> <p>Hi Mark,</p> <p>Thank you for taking the time to write such interesting articles. I'm personally fascinated by the relationship between ancient subjects like algebra and modern ones like programming. I can't wait to read more.</p> <p>That said, I understand the feeling of being put off by some of the terms used in functional programming (I'm looking at you, "<em>zygohistomorphic</em>"). I think the reason for it is that the vast majority of those words come from Greek or Latin, and to many people (me included) Greek is exactly what it sounds like — <a href="https://en.wikipedia.org/wiki/Greek_to_me">Greek</a>.</p> <p>Granted, things aren't much better in the object-oriented programming world, where a <em>Visitor</em> isn't necessarily what you think it is, even if you recognize the word.</p> <p>However, in my experience, knowing the etymology of a word is the first step in understanding it. I think that including a translation for every new term would make the subjects of these articles feel less alien. It would be a way to "break the ice", so to speak.</p> <p>One example I came to think of is the word <a href="https://en.wikipedia.org/wiki/Polymorphism_(computer_science)"><em>polymorphism</em></a> — perhaps one of the most "academic-sounding" words thrown around in object-oriented programming conversations. It may feel intimidating at first, but it quickly falls off the ivory tower once you know that it literally means "when things can take many shapes" (from the Greek <em>polys</em>, "many", <em>morphē</em>, "shape" and "<em>ismós</em>", the general concept).</p> <p>/Enrico</p> </div> <div class="comment-date">2017-10-05 12:24 UTC</div> </div> <div class="comment" id="6e0fe7ca211243ed8b0cbc30618b70e6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6e0fe7ca211243ed8b0cbc30618b70e6">#</a></div> <div class="comment-content"> <p> Enrico, thank you for writing. Funny you should write that, because leading with an explanation of <em>monoid</em> is exactly what I do in my new <a href="https://cleancoders.com">Clean Coders</a> episode <a href="https://cleancoders.com/episode/humane-code-real-episode-2/show">Composite as Universal Abstraction</a>. In short, <em>monoid</em> means 'one-like'. In the video, I go into more details on why that's a useful name. </p> </div> <div class="comment-date">2017-10-08 8:06 UTC</div> </div> <div class="comment" id="d8d9f8c8eb7341af8b7e9ae5681c3eff"> <div class="comment-author"><a href="http://avpinzur.com">Av Pinzur</a> <a href="#d8d9f8c8eb7341af8b7e9ae5681c3eff">#</a></div> <div class="comment-content"> <p> Hey, Mark, what a great start on a very promising series! One more accessibility suggestion along the same lines as Enrico's: You might consider including pronunciation for new terms that aren't obvious. </p> <p> Eagerly anticipating future installments! </p> </div> <div class="comment-date">2017-10-09 14:43 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. From design patterns to category theory https://blog.ploeh.dk/2017/10/04/from-design-patterns-to-category-theory 2017-10-04T10:43:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you design good abstractions? By using abstractions that already exist.</em> </p> <p> When I was a boy, I had a <a href="https://en.wikipedia.org/wiki/Cassette_deck">cassette tape player</a>. It came with playback controls like these: </p> <p> <img src="/content/binary/playback-controls.png" alt="Rewind, play/pause, and fast forward symbols."> </p> <p> Soon after cassette players had become widely adopted, <a href="https://en.wikipedia.org/wiki/Videocassette_recorder">VCR</a> manufacturers figured out that they could reuse those symbols to make their machines easier to use. Everyone could play a video tape, but 'no one' could 'program' them, because, while playback controls were already universally understood by consumers, each VCR came with its own proprietary interface for 'programming'. </p> <p> Then came <a href="https://en.wikipedia.org/wiki/CD_player">CD players</a>. Same controls. </p> <p> MP3 players. Same controls. </p> <p> Streaming audio and video players. Same controls. </p> <p> If you download an app that plays music, odds are that you'll find it easy to get started playing music. One reason is that all playback apps seem to have the same common set of controls. It's an abstraction that you already know. </p> <h3 id="b07a5341dd654a529ea45517c4e80aff"> Understanding source code <a href="#b07a5341dd654a529ea45517c4e80aff" title="permalink">#</a> </h3> <p> As I explain in my <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">Humane Code</a> video, you can't program without abstractions. To summarise, in <a href="http://amzn.to/19W4JHk">the words of Robert C. Martin</a> <blockquote> "Abstraction is the elimination of the irrelevant and the amplification of the essential" </blockquote> With such abstractions, source code becomes easier to understand. Like everything else, there's no silver bullet, but good coding abstractions can save you much grief, and make it easier to understand big and complex code bases. </p> <p> Not only can a good abstraction shield you from having to understand all the details in a big system, but if you're familiar with the abstraction, you may be able to quickly get up to speed. </p> <p> While the above definition is great for identifying a good abstraction, it doesn't tell you how to create one. </p> <h3 id="d0a8ec8fa11e4455bbed098d9a61c24c"> Design patterns <a href="#d0a8ec8fa11e4455bbed098d9a61c24c" title="permalink">#</a> </h3> <p> <a href="http://amzn.to/XBYukB">Design Patterns</a> explains that a design pattern is a general reusable solution to a commonly occurring problem. As I interpret the original intent of the Gang of Four, the book was an attempt to collect and abstract solutions that were repeatedly observed 'in the wild'. The design patterns in the book are <em>descriptive</em>, not prescriptive. </p> <p> Design patterns are useful in two ways: <ul> <li>They offer solutions</li> <li>They form a vocabulary</li> </ul> In my opinion, however, people often overlook the second advantage. Programmers are often eager to find <em>solutions</em>. "I have a problem; what's the solution? Oh, here's a design pattern that fits!" </p> <p> I have no problems with ready-made solutions, but I think that the other advantage may be even bigger. When you're looking at unfamiliar source code, you struggle to understand how it's structured, and what it does. If, hypothetically, you discover that pieces of that unfamiliar source code follows a design pattern that you know, then understanding the code becomes much easier. </p> <p> There are two criteria for this to happen: <ul> <li>The reader (you) must already know the pattern</li> <li>The original author (also you?) must have implemented the pattern without any surprising deviations</li> </ul> As a programmer (code author), you can help readers (users) of your code. Don't use every design pattern in the book, but when you use one, make it as obvious to the reader as you can: Use the terminology, class names, and so on from the book. Add comments where your naming deviates. Add links that the novice user can follow to learn more. </p> <h3 id="3f4cb22e7a8642cd9938c89b22c098bf"> Ambiguous specification <a href="#3f4cb22e7a8642cd9938c89b22c098bf" title="permalink">#</a> </h3> <p> Programming to a well-known abstraction is a force multiplier, but it does require that those two conditions are satisfied: prior knowledge, and correct implementation. </p> <p> I don't know how to solve the <em>prior knowledge</em> requirement, other than to tell you to study. I do, however, think that it's possible to formalise some of the known design patterns. </p> <p> Most design patterns are described in some depth. They come with sections on motivation, when to use and not to use, diagrams, and example code. Furthermore, they also come with an overview of <em>variations</em>. </p> <p> Picture this: as a reader, you've just identified that the code you're looking at is an implementation of a design pattern. Then you realise that it isn't structured like you'd expect, or that its behaviour surprises you. Was the author incompetent, after all? </p> <p> While you're inclined to believe the worst about your fellow (wo)man, you look up the original pattern, and there it is: the author is using a variation of the pattern. </p> <p> Design patterns are ambiguous. </p> <h3 id="a90284d241c4463583401504395a4a8a"> Universal abstractions <a href="#a90284d241c4463583401504395a4a8a" title="permalink">#</a> </h3> <p> <em>Design Patterns</em> was a great effort in 1994, and I've personally benefited from it. The catalogue was an attempt to discover good abstractions. </p> <p> What's a good abstraction? As already quoted, it's a model that amplifies the essentials, etcetera. I think a good abstraction should also be <em>intuitive</em>. </p> <p> What's the most intuitive abstractions ever? </p> <p> Mathematics. </p> <p> Stay with me, please. If you're a normal reader of my blog, you're most likely an 'industry programmer' or <a href="/2012/12/18/RangersandZookeepers">enterprise developer</a>. You're not interested in mathematics. Perhaps mathematics even turns you off, and at the very least, you never had use for mathematics in programming. </p> <p> You may not find <em>n</em>-dimensional <a href="https://en.wikipedia.org/wiki/Differential_topology">differential topology</a>, or <a href="https://en.wikipedia.org/wiki/Stochastic_calculus">stochastic calculus</a>, intuitive, but that's not the kind of mathematics I have in mind. </p> <p> Basic arithmetic is intuitive. You know: <em>1 + 3 = 4</em>, or <em>3 * 4 = 12</em>. In fact, it's <em>so intuitive</em> that you can't formally prove it -without <a href="https://en.wikipedia.org/wiki/Axiom">axioms</a>, that is. <a href="https://en.wikipedia.org/wiki/Peano_axioms">These axioms</a> are unprovable; you must take them at face value, but you'll readily do that because they're <em>so intuitive</em>. </p> <p> Mathematics is a big structure, but it's all based on intuitive axioms. Mathematics is intuitive. </p> <p> Writers before me have celebrated the power of mathematical abstraction in programming. For instance, in <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> Eric Evans discusses how <em>Closure of Operations</em> leads to object models reminiscent of arithmetic. If you can design <a href="https://martinfowler.com/bliki/ValueObject.html">Value Objects</a> in such a way that you can somehow 'add' them together, you have an intuitive and powerful abstraction. </p> <p> Notice that there's more than one way to combine numbers. You can add them together, but you can also multiply them. Could there be a common abstraction for that? What about objects that can somehow be combined, even if they aren't 'number-like'? The generalisation of such operations is a branch of mathematics called <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, and it has turned out to be productive when applied to functional programming. <a href="https://www.haskell.org">Haskell</a> is the most prominent example. </p> <p> By an interesting coincidence, the 'things' in category theory are called <em>objects</em>, and while they aren't objects in the sense that we think of in object-oriented design, there <em>is</em> some equivalence. Category theory concerns itself with how objects map to other objects. A functional programmer would interpret such <em>morphisms</em> as functions, but in a sense, you can also think of them as well-defined behaviour that's associated with data. </p> <p> The objects of category theory are universal abstractions. Some of them, it turns out, coincide with known design patterns. The difference is, however, that category theory concepts are governed by specific laws. In order to be a functor, for example, an object must obey certain simple and intuitive laws. This makes the category theory concepts more specific, and less ambiguous, than design patterns. </p> <p> The coming article series is an exploration of this space: <ul> <li><a href="/2017/10/05/monoids-semigroups-and-friends">Monoids, semigroups, and friends</a> <ul> <li><a href="/2017/10/06/monoids">Monoids</a> <ul> <li><a href="/2018/07/16/angular-addition-monoid">Angular addition monoid</a></li> <li><a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">Strings, lists, and sequences as a monoid</a></li> <li><a href="/2017/10/16/money-monoid">Money monoid</a></li> <li><a href="/2017/10/23/convex-hull-monoid">Convex hull monoid</a></li> <li><a href="/2017/10/30/tuple-monoids">Tuple monoids</a></li> <li><a href="/2017/11/06/function-monoids">Function monoids</a></li> <li><a href="/2017/11/13/endomorphism-monoid">Endomorphism monoid</a></li> <li><a href="/2018/04/03/maybe-monoids">Maybe monoids</a></li> <li><a href="/2019/04/15/lazy-monoids">Lazy monoids</a></li> <li><a href="/2017/11/20/monoids-accumulate">Monoids accumulate</a></li> </ul> </li> <li><a href="/2017/11/27/semigroups">Semigroups</a> <ul> <li><a href="/2017/12/04/bounding-box-semigroup">Bounding box semigroup</a></li> <li><a href="/2017/12/11/semigroups-accumulate">Semigroups accumulate</a></li> </ul> </li> <li><a href="/2017/12/18/quasigroups">Quasigroups</a></li> <li><a href="/2017/12/27/magmas">Magmas</a> <ul> <li><a href="/2017/12/28/rock-paper-scissors-magma">Rock Paper Scissors magma</a></li> <li><a href="/2018/01/02/colour-mixing-magma">Colour-mixing magma</a></li> </ul> </li> </ul> </li> <li><a href="/2018/03/19/functors-applicatives-and-friends">Functors, applicatives, and friends</a> <ul> <li><a href="/2018/03/22/functors">Functors</a> <ul> <li><a href="/2018/03/26/the-maybe-functor">The Maybe functor</a></li> <li><a href="/2019/01/14/an-either-functor">An Either functor</a></li> <li><a href="/2018/08/06/a-tree-functor">A Tree functor</a></li> <li><a href="/2019/08/19/a-rose-tree-functor">A rose tree functor</a></li> <li><a href="/2018/08/13/a-visitor-functor">A Visitor functor</a></li> <li><a href="/2018/08/20/reactive-functor">Reactive functor</a></li> <li><a href="/2018/09/03/the-identity-functor">The Identity functor</a></li> <li><a href="/2018/09/10/the-lazy-functor">The Lazy functor</a></li> <li><a href="/2018/09/24/asynchronous-functors">Asynchronous functors</a></li> <li><a href="/2021/07/19/the-state-functor">The State functor</a></li> <li><a href="/2021/08/30/the-reader-functor">The Reader functor</a></li> <li><a href="/2020/06/22/the-io-functor">The IO functor</a></li> <li><a href="/2020/10/19/monomorphic-functors">Monomorphic functors</a></li> <li><a href="/2018/12/03/set-is-not-a-functor">Set is not a functor</a></li> </ul> </li> <li><a href="/2018/10/01/applicative-functors">Applicative functors</a> <ul> <li><a href="/2018/10/08/full-deck">Full deck</a></li> <li><a href="/2018/10/15/an-applicative-password-list">An applicative password list</a></li> <li><a href="/2018/10/22/applicative-combinations-of-functions">Applicative combinations of functions</a></li> <li><a href="/2018/10/29/the-maybe-applicative-functor">The Maybe applicative functor</a></li> <li><a href="/2018/11/05/applicative-validation">Applicative validation</a></li> <li><a href="/2018/11/26/the-test-data-generator-applicative-functor">The Test Data Generator applicative functor</a></li> <li><a href="/2018/12/10/danish-cpr-numbers-in-f">Danish CPR numbers in F#</a></li> <li><a href="/2018/12/17/the-lazy-applicative-functor">The Lazy applicative functor</a></li> <li><a href="/2019/04/22/applicative-monoids">Applicative monoids</a></li> </ul> </li> <li><a href="/2018/12/24/bifunctors">Bifunctors</a> <ul> <li><a href="/2018/12/31/tuple-bifunctor">Tuple bifunctor</a></li> <li><a href="/2019/01/07/either-bifunctor">Either bifunctor</a></li> <li><a href="/2019/08/12/rose-tree-bifunctor">Rose tree bifunctor</a></li> </ul> </li> <li><a href="/2021/09/02/contravariant-functors">Contravariant functors</a> <ul> <li><a href="/2021/09/06/the-command-handler-contravariant-functor">The Command Handler contravariant functor</a></li> <li><a href="/2021/09/09/the-specification-contravariant-functor">The Specification contravariant functor</a></li> <li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li> <li><a href="/2021/10/04/reader-as-a-contravariant-functor">Reader as a contravariant functor</a></li> <li><a href="/2021/10/25/functor-variance-compared-to-cs-notion-of-variance">Functor variance compared to C#'s notion of variance</a></li> <li><a href="/2022/03/21/contravariant-dependency-injection">Contravariant Dependency Injection</a></li> </ul> </li> <li><a href="/2021/11/01/profunctors">Profunctors</a> <ul> <li><a href="/2021/11/08/reader-as-a-profunctor">Reader as a profunctor</a></li> </ul> </li> <li><a href="/2022/08/01/invariant-functors">Invariant functors</a> <ul> <li><a href="/2022/08/08/endomorphism-as-an-invariant-functor">Endomorphism as an invariant functor</a></li> <li><a href="/2022/08/29/natural-transformations-as-invariant-functors">Natural transformations as invariant functors</a></li> <li><a href="/2022/12/26/functors-as-invariant-functors">Functors as invariant functors</a></li> <li><a href="/2023/02/06/contravariant-functors-as-invariant-functors">Contravariant functors as invariant functors</a></li> </ul> </li> <li><a href="/2022/03/28/monads">Monads</a> <ul> <li><a href="/2022/04/04/kleisli-composition">Kleisli composition</a></li> <li><a href="/2022/04/11/monad-laws">Monad laws</a></li> <li><a href="/2022/04/19/the-list-monad">The List monad</a></li> <li><a href="/2022/04/25/the-maybe-monad">The Maybe monad</a></li> <li><a href="/2022/05/09/an-either-monad">An Either monad</a></li> <li><a href="/2022/05/16/the-identity-monad">The Identity monad</a></li> <li><a href="/2022/05/30/the-lazy-monad">The Lazy monad</a></li> <li><a href="/2022/06/06/asynchronous-monads">Asynchronous monads</a></li> <li><a href="/2022/06/20/the-state-monad">The State monad</a></li> <li><a href="/2022/11/14/the-reader-monad">The Reader monad</a></li> <li><a href="/2023/01/09/the-io-monad">The IO monad</a></li> <li><a href="/2023/02/27/test-data-generator-monad">Test Data Generator monad</a></li> </ul> </li> <li><a href="/2022/07/11/functor-relationships">Functor relationships</a> <ul> <li><a href="/2022/07/18/natural-transformations">Natural transformations</a></li> <li>Traversals</li> <li>Functor compositions</li> <li>Functor products</li> <li>Functor sums</li> </ul> </li> </ul> </li> <li><a href="/2018/01/08/software-design-isomorphisms">Software design isomorphisms</a> <ul> <li><a href="/2018/01/15/unit-isomorphisms">Unit isomorphisms</a></li> <li><a href="/2018/01/22/function-isomorphisms">Function isomorphisms</a></li> <li><a href="/2018/01/29/argument-list-isomorphisms">Argument list isomorphisms</a></li> <li><a href="/2018/02/05/uncurry-isomorphisms">Uncurry isomorphisms</a></li> <li><a href="/2018/02/12/object-isomorphisms">Object isomorphisms</a></li> <li><a href="/2018/02/19/abstract-class-isomorphism">Abstract class isomorphism</a></li> <li><a href="/2018/02/26/inheritance-composition-isomorphism">Inheritance-composition isomorphism</a></li> <li><a href="/2019/07/15/tester-doer-isomorphisms">Tester-Doer isomorphisms</a></li> <li><a href="/2020/02/10/builder-isomorphisms">Builder isomorphisms</a></li> </ul> </li> <li><a href="/2018/05/22/church-encoding">Church encoding</a> <ul> <li><a href="/2018/05/24/church-encoded-boolean-values">Church-encoded Boolean values</a></li> <li><a href="/2018/05/28/church-encoded-natural-numbers">Church-encoded natural numbers</a></li> <li><a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a></li> <li><a href="/2018/06/11/church-encoded-either">Church-encoded Either</a></li> <li><a href="/2018/06/18/church-encoded-payment-types">Church-encoded payment types</a></li> <li><a href="/2019/07/29/church-encoded-rose-tree">Church-encoded rose tree</a></li> </ul> </li> <li><a href="/2019/04/29/catamorphisms">Catamorphisms</a> <ul> <li><a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a></li> <li><a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a></li> <li><a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a></li> <li><a href="/2019/05/27/list-catamorphism">List catamorphism</a></li> <li><a href="/2023/08/07/nonempty-catamorphism">NonEmpty catamorphism</a></li> <li><a href="/2019/06/03/either-catamorphism">Either catamorphism</a></li> <li><a href="/2019/06/10/tree-catamorphism">Tree catamorphism</a></li> <li><a href="/2019/08/05/rose-tree-catamorphism">Rose tree catamorphism</a></li> <li><a href="/2019/06/24/full-binary-tree-catamorphism">Full binary tree catamorphism</a></li> <li><a href="/2019/07/08/payment-types-catamorphism">Payment types catamorphism</a></li> </ul> </li> <li><a href="/2018/03/05/some-design-patterns-as-universal-abstractions">Some design patterns as universal abstractions</a> <ul> <li><a href="/2018/03/12/composite-as-a-monoid">Composite as a monoid</a> <ul> <li><a href="/2018/04/09/coalescing-composite-as-a-monoid">Coalescing Composite as a monoid</a></li> <li><a href="/2018/04/16/endomorphic-composite-as-a-monoid">Endomorphic Composite as a monoid</a></li> </ul> </li> <li><a href="/2018/04/23/null-object-as-identity">Null Object as identity</a></li> <li><a href="/2020/02/17/builder-as-a-monoid">Builder as a monoid</a></li> <li><a href="/2018/06/25/visitor-as-a-sum-type">Visitor as a sum type</a></li> <li><a href="/2019/07/22/chain-of-responsibility-as-catamorphisms">Chain of Responsibility as catamorphisms</a></li> <li><a href="/2022/09/05/the-state-pattern-and-the-state-monad">The State pattern and the State monad</a> <ul> <li><a href="/2022/09/26/refactoring-the-tcp-state-pattern-example-to-pure-functions">Refactoring the TCP State pattern example to pure functions</a></li> <li><a href="/2022/10/10/refactoring-a-saga-from-the-state-pattern-to-the-state-monad">Refactoring a saga from the State pattern to the State monad</a></li> </ul> </li> <li><a href="/2021/11/29/postels-law-as-a-profunctor">Postel's law as a profunctor</a></li> <li><a href="/2021/12/06/the-liskov-substitution-principle-as-a-profunctor">The Liskov Substitution Principle as a profunctor</a></li> <li><a href="/2021/12/13/backwards-compatibility-as-a-profunctor">Backwards compatibility as a profunctor</a></li> </ul> </li> </ul> I believe that learning about these universal abstractions is the next step in software design. If you know design patterns, you have a vocabulary, but the details are still open to interpretation. If you know category theory, you have a better vocabulary. Just like design patterns, you have to learn these things, but once you've learned them, you've learned something that transcends a particular software library, a particular framework, a particular programming language. Learning about functors, monoids, and so on, is a good investment, because these concepts are rooted in mathematics, not any particular technology. </p> <h3 id="4c0cdfcad93b4112a7241abd9b9df556"> Motivation <a href="#4c0cdfcad93b4112a7241abd9b9df556" title="permalink">#</a> </h3> <p> The purpose of this article series is two-fold. Depending on your needs and interests, you can use it to <ul> <li>learn better abstractions</li> <li>learn how functional programming is a real alternative to object-oriented programming</li> </ul> You've already read how it's in your interest to learn universal abstractions. It'll make your code clearer, more concise, and you'll have a better software design vocabulary. </p> <p> The other goal of these articles may be less clear. Object-oriented programming (OOP) is the dominant software design <a href="https://en.wikipedia.org/wiki/Paradigm">paradigm</a>. It wasn't always so. When OOP was new, many veteran programmers couldn't see how it could be useful. They were schooled in one paradigm, and it was difficult for them to shift to the new paradigm. They were used to do things in one way (typically, procedural), and it wasn't clear how to achieve the same goals with idiomatic object-oriented design. </p> <p> The same sort of resistance applies to functional programming. Tasks that are easy in OOP seem impossible in functional programming. How do you make a <em>for</em> loop? How do you change state? How do you break out of a routine? </p> <p> This leads to both frustration, and dismissal of functional programming, which is still seen as either academic, or something only interesting in computation-heavy domains like science or finance. </p> <p> It's my secondary goal with these articles to show that: <ol> <li>There are clear equivalences between known design patterns and concepts from category theory</li> <li>Thus, functional programming is as universally useful as OOP</li> <li>Since equivalences exist, there's a learning path</li> </ol> If you're an object-oriented programmer, you can use this catalogue as a learning path. If you'd normally use a <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite</a>, you can look it up and realise that it's the same as a monoid. </p> <h3 id="44a9afc2e9874072ac40747c958f93f3"> Work in progress <a href="#44a9afc2e9874072ac40747c958f93f3" title="permalink">#</a> </h3> <p> I've been thinking about these topics for years. What's a good abstraction? When do abstractions compose? </p> <p> My <a href="/2010/12/03/Towardsbetterabstractions">first attempt at answering these questions</a> was in 2010, but while I had the experience that certain abstractions composed better than others, I lacked the vocabulary. I've been wanting to write a better treatment of the topic ever since, but I've been constantly learning as I've grappled with the concepts. </p> <p> I believe that I now have the vocabulary to take a stab at this again. This is hardly the ultimate treatment. A year from now, I hope to have learned even more, and perhaps that'll lead to further insights or refinement. Still, I can't postpone writing this article until I've stopped learning, because at that time I'll either be dead or senile. </p> <p> I'll write these articles in an authoritative voice, because a text that constantly moderates and qualifies its assertions easily becomes unreadable. Don't consider the tone an indication that I'm certain that I'm right. I've tried to be as rigorous in my arguments as I could, but I don't have a formal education in computer science. I welcome feedback on any article, both if it's to corroborate my findings, or if it's to refute them. If you have any sort of feedback, then <a href="https://github.com/ploeh/ploeh.github.com#readme">please leave a comment</a>. </p> <p> I consider the publication of these articles as though I submit them to peer review. If you can refute them, they deserve to be refuted. If not, they just may be valuable to other people. </p> <h3 id="dee0b130c2184c4da1dbb73b8e6b6fca"> Summary <a href="#dee0b130c2184c4da1dbb73b8e6b6fca" title="permalink">#</a> </h3> <p> Category theory generalises some intuitive relations, such as how numbers combine (e.g. via addition or multiplication). Instead of discussing numbers, however, category theory considers abstract 'objects'. This field of mathematics explore how object relate and compose. </p> <p> Some category theory concepts can be translated to code. These universal abstractions can form the basis of a powerful and concise software design vocabulary. </p> <p> The design patterns movement was an early attempt to create such a vocabulary. I think using category theory offers the chance of a better vocabulary, but fortunately, all the work that went into design patterns isn't wasted. It seems to me that some design patterns are essentially ad-hoc, informally specified, specialised instances of basic category theory concepts. There's quite a bit of overlap. This should further strengthen the argument that category theory is valuable in programming, because some of the concepts are equivalent to design patterns that have already proven useful. </p> <p> <strong>Next:</strong> <a href="/2017/10/05/monoids-semigroups-and-friends">Monoids, semigroups, and friends</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="4ef6608d77ea49e7b8335562a9a6fa19"> <div class="comment-author"> <a href="https://hettomei.github.io/">Tim</a> <a href="#4ef6608d77ea49e7b8335562a9a6fa19">#</a></div> <div class="comment-content"> <p>What a perfect introduction !</p> <p>I heard about category theory more than one year ago. But it was from a PhD who code in 'haskell' and I thought it was too hard for me to understand.</p> <p>And then, this post.</p> <p>Thank you a lot! (you aleardy published the follow up ! yeah)</p> </div> <div class="comment-date">2017-10-05 21:39 UTC</div> </div> <div class="comment" id="dbf2ac567c5543138b02d6f9f26538d1"> <div class="comment-author"> <a href="https://github.com/rutgersc">Rutger Schoorstra</a> <a href="#dbf2ac567c5543138b02d6f9f26538d1">#</a></div> <div class="comment-content"> <p>Thanks for writing these articles, it's nice to have some reference material that is approachable for us as dotnet programmers.</p> <p>One thing I was kind of expecting to find here was something about the two building blocks of combining types: products and coproducts. Is this something you have written about, or are considering writing about? It gets mentioned in the <a href="/2018/05/22/church-encoding">Church encoding</a> series and obviously those about visitors, but not really as a concept on its own.</p> <p>What triggered me to come here this time, was reading about the much requested <a href="https://github.com/dotnet/csharplang/issues/113">Champion "Discriminated Unions"</a>. Not only in those comments, but also when looking at other C# code, lots of people seem to not realize how fundamental of a concept sum types are. IF they are, I could be wrong ofcourse.</p> <p>I liked the way <a href="https://bartoszmilewski.com/2015/01/07/products-and-coproducts/">bartosz milewski</a> explained this by visualizing them as graphs. Or how <a href="https://fsharpforfunandprofit.com/posts/discriminated-unions/">Scott Wlaschin</a> relates it back to other concepts we also take for granted: <ul> <li>products, *, AND, classes, records, tuples</li> <li>coproducts, +, OR, discriminated unions, ...</li> </ul> </p> <p>Anyway, I don't want to ramble on too much. Just curious if it's something you think fits the list of universal abstractions.</p> </div> <div class="comment-date">2023-04-10 09:42 UTC</div> </div> <div class="comment" id="c38957cc3d5344308fd0e2dbe63fca18"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c38957cc3d5344308fd0e2dbe63fca18">#</a></div> <div class="comment-content"> <p> Rutger, thank you for writing. I agree that the notion of algebraic data types are, in some sense, quite fundamental. Despite that, I was never planning on covering that topic in this series. The main reason is that I think that other people have already done a great job of it. The first time I encountered the concept was in <a href="https://tomasp.net/">Tomas Petricek</a>'s exemplarily well-written article <a href="https://tomasp.net/blog/types-and-math.aspx/">Power of mathematics Reasoning about functional types</a>, but, as you demonstrate, there are plenty of other good resources. Another favourite of mine is <a href="https://thinkingwithtypes.com/">Thinking with Types</a>. </p> <p> That's not to say that this is the right decision, or that I might not write such an article. When I started this massive article series, I had a general idea about the direction I'd like to go, but I learned a lot along the way that slightly changed the plans. For example, despite the title, there's not that much category theory here. The reason for that is that I found that most of the concepts didn't really require category theory. For example, monoids originate (as far as I can tell) from abstract algebra, and you don't need more than that to explain the concept. </p> <p> So, to answer your direct question: No, this isn't something that I've given an explicit treatment. On one hand, I think there's already enough good material on the topic that the world doesn't need my contribution. On the other hand, perhaps there's a dearth of treatment that puts this in a C# context. </p> <p> I'm not adverse to writing such an article, but I have so many other topics I'd also like to cover. </p> </div> <div class="comment-date">2023-04-14 7:10 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Interception vis-à-vis Pure DI https://blog.ploeh.dk/2017/09/25/interception-vis-a-vis-pure-di 2017-09-25T07:27:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you do AOP with Pure DI?</em> </p> <p> One of my readers, Nick Ball, asks me this question: </p> <blockquote> <p> "Just spent the last couple of hours reading chapter 9 of your book about Interceptors. The final few pages show how to use Castle Windsor to make the code DRYer. That's cool, but I'm quite a fan of Pure DI as I tend to think it keeps things simpler. Plus I'm working in a legacy C++ application which limits the tooling available to me. </p> <p> "So, I was wondering if you had any suggestions on how to DRY up an interceptor in Pure DI? I know in your book you state that this is where DI containers come into their own, but I also know through reading your blog that you prefer going the Pure DI route too. Hence I wondered whether you'd had any further insight since the book publication?" </p> </blockquote> <p> It's been more than 15 years since I last did C++, so I'm going to give an answer based on C#, and hope it translates. </p> <h3 id="101b992185744b0595bb0e0c50ef1e81"> Position <a href="#101b992185744b0595bb0e0c50ef1e81" title="permalink">#</a> </h3> <p> I do, indeed, prefer <a href="/2014/06/10/pure-di">Pure DI</a>, but there may be <a href="/2012/11/06/WhentouseaDIContainer">cases where a DI Container is warranted</a>. Interception, or Aspect-Oriented Programming (AOP), is one such case, but obviously that doesn't help if you can't use a DI Container. </p> <p> Another option for AOP is some sort of post-processor of your code. As I briefly cover in chapter 9 of <a href="http://amzn.to/12p90MG">my book</a>, in .NET this is typically done by a custom tool using 'IL-weaving'. As I also outline in the book, I'm not a big fan of this approach, but perhaps that could be an option in C++ as well. In any case, I'll proceed under the assumption that you want a strictly code-based solution, involving no custom tools or build steps. </p> <p> All that said, I doubt that this is as much of a problem than one would think. AOP is typically used for cross-cutting concerns such as logging, caching, instrumentation, authorization, metering, or auditing. As an alternative, you can also <a href="/2010/04/07/DependencyInjectionisLooseCoupling">use Decorators for such cross-cutting concerns</a>. This seems daunting if you truly need to decorate hundreds, or even thousands, of classes. In such a case, convention-based interception seems like a <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>er option. </p> <p> You'd think. </p> <p> In my experience, however, this is rarely the case. Typically, even when applying caching, logging, or authorisation logic, I've only had to create a handful of <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a>. Perhaps it's because I tend to keep my code bases to a manageable size. </p> <p> If you only need a dozen Decorators, I don't think that the loss of compile-time safety and the added dependency warrants the use of a DI Container. That doesn't mean, however, that I can't aim for as DRY code as possible. </p> <h3 id="0eb07d36746c458ea6a3c95e83f109b8"> Instrument <a href="#0eb07d36746c458ea6a3c95e83f109b8" title="permalink">#</a> </h3> <p> If you don't have a DI Container or an AOP tool, I believe that a Decorator is the best way to address cross-cutting concerns, and I don't think there's any way around adding those Decorator classes. The aim, then, becomes to minimise the effort involved in creating and maintaining such classes. </p> <p> As an example, I'll revisit <a href="/2010/09/20/InstrumentationwithDecoratorsandInterceptors">an old blog post</a>. In that post, the task was to instrument an <code>OrderProcessor</code> class. The solution shown in that article was to use <a href="http://www.castleproject.org/projects/windsor">Castle Windsor</a> to define an <code>IInterceptor</code>. </p> <p> To recapitulate, the code for the Interceptor looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InstrumentingInterceptor</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IInterceptor</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IRegistrar</span>&nbsp;registrar; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;InstrumentingInterceptor(<span style="color:#2b91af;">IRegistrar</span>&nbsp;registrar) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(registrar&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(registrar)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.registrar&nbsp;=&nbsp;registrar; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Intercept(<span style="color:#2b91af;">IInvocation</span>&nbsp;invocation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;correlationId&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.registrar.Register(correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&quot;{0}&nbsp;begins&nbsp;({1})&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invocation.Method.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invocation.TargetType.Name)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invocation.Proceed(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.registrar.Register(correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&quot;{0}&nbsp;ends&nbsp;&nbsp;&nbsp;({1})&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invocation.Method.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invocation.TargetType.Name)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> While, in the new scenario, you can't use Castle Windsor, you can still take the code and make a similar class out of it. Call it <code>Instrument</code>, because classes should have noun names, and <em>instrument</em> is a noun (right?). </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Instrument</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IRegistrar</span>&nbsp;registrar; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Instrument(<span style="color:#2b91af;">IRegistrar</span>&nbsp;registrar) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(registrar&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(registrar)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.registrar&nbsp;=&nbsp;registrar; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Intercept&lt;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;methodName, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;typeName, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;proceed) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;correlationId&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.registrar.Register( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&quot;{0}&nbsp;begins&nbsp;({1})&quot;</span>,&nbsp;methodName,&nbsp;typeName)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;proceed(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.registrar.Register( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&quot;{0}&nbsp;ends&nbsp;&nbsp;&nbsp;({1})&quot;</span>,&nbsp;methodName,&nbsp;typeName)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;result; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Intercept( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;methodName, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;typeName, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Action</span>&nbsp;proceed) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;correlationId&nbsp;=&nbsp;<span style="color:#2b91af;">Guid</span>.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.registrar.Register( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&quot;{0}&nbsp;begins&nbsp;({1})&quot;</span>,&nbsp;methodName,&nbsp;typeName)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proceed(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.registrar.Register( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&quot;{0}&nbsp;ends&nbsp;&nbsp;&nbsp;({1})&quot;</span>,&nbsp;methodName,&nbsp;typeName)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Instead of a single <code>Intercept</code> method, the <code>Instrument</code> class exposes two <code>Intercept</code> overloads; one for methods without a return value, and one for methods that return a value. Instead of an <code>IInvocation</code> argument, the overload for methods without a return value takes an <a href="https://msdn.microsoft.com/en-us/library/system.action">Action</a> delegate, whereas the other overload takes a <a href="https://msdn.microsoft.com/en-us/library/bb534960">Func&lt;T&gt;</a>. </p> <p> Both overload also take <code>methodName</code> and <code>typeName</code> arguments. </p> <p> Most of the code in the two methods is similar. While you could refactor to a <a href="https://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a>, I invoke the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">Rule of three</a> and let the duplication stay for now. </p> <h3 id="e2c6d4463875415ab68dadd271638b67"> Decorators <a href="#e2c6d4463875415ab68dadd271638b67" title="permalink">#</a> </h3> <p> The <code>Instrument</code> class isn't going to magically create Decorators for you, but it reduces the effort of creating one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InstrumentedOrderProcessor2</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IOrderProcessor</span>&nbsp;orderProcessor; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Instrument</span>&nbsp;instrument; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;InstrumentedOrderProcessor2( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IOrderProcessor</span>&nbsp;orderProcessor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Instrument</span>&nbsp;instrument) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(orderProcessor&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(orderProcessor)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(instrument&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(instrument)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.orderProcessor&nbsp;=&nbsp;orderProcessor; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.instrument&nbsp;=&nbsp;instrument; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">SuccessResult</span>&nbsp;Process(<span style="color:#2b91af;">Order</span>&nbsp;order) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.instrument.Intercept( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(Process), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.orderProcessor.GetType().Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:blue;">this</span>.orderProcessor.Process(order)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I called this class <code>InstrumentedOrderProcessor2</code> with the <code>2</code> postfix because the previous article already contains a <code>InstrumentedOrderProcessor</code> class, and I wanted to make it clear that this is a new class. </p> <p> Notice that <code>InstrumentedOrderProcessor2</code> is a Decorator of <code>IOrderProcessor</code>. It both implements the interface, and takes one as a dependency. It also takes an <code>Instrument</code> object as a <a href="/2012/08/31/ConcreteDependencies">Concrete Dependency</a>. This is mostly to enable reuse of a single <code>Instrument</code> object; no polymorphism is implied. </p> <p> The decorated <code>Process</code> method simply delegates to the <code>instrument</code>'s <code>Intercept</code> method, passing as parameters the name of the method, the name of the decorated class, and a lambda expression that closes over the outer <code>order</code> method argument. </p> <p> For simplicity's sake, the <code>Process</code> method invokes <code>this.orderProcessor.GetType().Name</code> every time it's called, which may not be efficient. Since the <code>orderProcessor</code> class field is <code>readonly</code>, though, you could optimise this by getting the name once and for all in the constructor, and assign the string to a third class field. I didn't want to complicate the example with irrelevant code, though. </p> <p> Here's another Decorator: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InstrumentedOrderShipper</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IOrderShipper</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IOrderShipper</span>&nbsp;orderShipper; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Instrument</span>&nbsp;instrument; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;InstrumentedOrderShipper( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IOrderShipper</span>&nbsp;orderShipper, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Instrument</span>&nbsp;instrument) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(orderShipper&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(orderShipper)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(instrument&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(instrument)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.orderShipper&nbsp;=&nbsp;orderShipper; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.instrument&nbsp;=&nbsp;instrument; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Ship(<span style="color:#2b91af;">Order</span>&nbsp;order) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.instrument.Intercept( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(Ship), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.orderShipper.GetType().Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:blue;">this</span>.orderShipper.Ship(order)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As you can tell, it's similar to <code>InstrumentedOrderProcessor2</code>, but instead of <code>IOrderProcessor</code> it decorates <code>IOrderShipper</code>. The most significant difference is that the <code>Ship</code> method doesn't return any value, so you have to use the <code>Action</code>-based overload of <code>Intercept</code>. </p> <p> For completeness sake, here's a third interesting example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InstrumentedUserContext</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IUserContext</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IUserContext</span>&nbsp;userContext; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Instrument</span>&nbsp;instrument; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;InstrumentedUserContext( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserContext</span>&nbsp;userContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Instrument</span>&nbsp;instrument) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(userContext&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(userContext)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(instrument&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(instrument)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.userContext&nbsp;=&nbsp;userContext; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.instrument&nbsp;=&nbsp;instrument; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">User</span>&nbsp;GetCurrentUser() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.instrument.Intercept( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(GetCurrentUser), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.userContext.GetType().Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.userContext.GetCurrentUser); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Currency</span>&nbsp;GetSelectedCurrency(<span style="color:#2b91af;">User</span>&nbsp;currentUser) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.instrument.Intercept( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(GetSelectedCurrency), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.userContext.GetType().Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:blue;">this</span>.userContext.GetSelectedCurrency(currentUser)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This example demonstrates that you can also decorate an interface that defines more than a single method. The <code>IUserContext</code> interface defines both <code>GetCurrentUser</code> and <code>GetSelectedCurrency</code>. The <code>GetCurrentUser</code> method takes no arguments, so instead of a lambda expression, you can pass the delegate using method group syntax. </p> <h3 id="b7bb77d6f6f7434dbfaa78b7472d3662"> Composition <a href="#b7bb77d6f6f7434dbfaa78b7472d3662" title="permalink">#</a> </h3> <p> You can add such instrumenting Decorators for all appropriate interfaces. It's trivial (and automatable) work, but it's easy to do. While it seems repetitive, I can't come up with a more DRY way to do it without resorting to some sort of run-time Interception or AOP tool. </p> <p> There's some repetitive code, but I don't think that the maintenance overhead is particularly great. The Decorators do minimal work, so it's unlikely that there are many defects in that area of your code base. If you need to change the instrumentation implementation in itself, the <code>Instrument</code> class has that (single) responsibility. </p> <p> Assuming that you've added all desired Decorators, you can use Pure DI to compose an object graph: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;instrument&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Instrument</span>(registrar); <span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InstrumentedOrderProcessor2</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">OrderProcessor</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InstrumentedOrderValidator</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TrueOrderValidator</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instrument), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InstrumentedOrderShipper</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">OrderShipper</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instrument), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InstrumentedOrderCollector</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">OrderCollector</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InstrumentedAccountsReceivable</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AccountsReceivable</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instrument), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InstrumentedRateExchange</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RateExchange</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instrument), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InstrumentedUserContext</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UserContext</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instrument)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instrument)), &nbsp;&nbsp;&nbsp;&nbsp;instrument);</pre> </p> <p> This code fragment is from a unit test, which explains why the object is called <code>sut</code>. In case you're wondering, this is also the reason for the existence of the curiously named class <code>TrueOrderValidator</code>. This is a test-specific <a href="http://xunitpatterns.com/Test%20Stub.html">Stub</a> of <code>IOrderValidator</code> that always returns <code>true</code>. </p> <p> As you can see, each leaf implementation of an interface is contained within an <code>InstrumentedXyz</code> Decorator, which also takes a shared <code>instrument</code> object. </p> <p> When I call the <code>sut</code>'s <code>Process</code> method with a proper <code>Order</code> object, I get output like this: </p> <p> <pre>4ad34380-6826-440c-8d81-64bbd1f36d39 2017-08-25T17:49:18.43 Process begins (OrderProcessor) c85886a7-1ce8-4096-8a30-5f87bf0014e3 2017-08-25T17:49:18.52 Validate begins (TrueOrderValidator) c85886a7-1ce8-4096-8a30-5f87bf0014e3 2017-08-25T17:49:18.52 Validate ends (TrueOrderValidator) 8f7606b6-f3f7-4231-808d-d5e37f1f2201 2017-08-25T17:49:18.53 Collect begins (OrderCollector) 28250a92-6024-439e-b010-f66c63903673 2017-08-25T17:49:18.55 GetCurrentUser begins (UserContext) 28250a92-6024-439e-b010-f66c63903673 2017-08-25T17:49:18.56 GetCurrentUser ends (UserContext) 294ce552-201f-41d2-b7fc-291e2d3720d6 2017-08-25T17:49:18.56 GetCurrentUser begins (UserContext) 294ce552-201f-41d2-b7fc-291e2d3720d6 2017-08-25T17:49:18.56 GetCurrentUser ends (UserContext) 96ee96f0-4b95-4b17-9993-33fa87972013 2017-08-25T17:49:18.57 GetSelectedCurrency begins (UserContext) 96ee96f0-4b95-4b17-9993-33fa87972013 2017-08-25T17:49:18.58 GetSelectedCurrency ends (UserContext) 3af884e5-8e97-44ea-aa0d-2c9e0418110b 2017-08-25T17:49:18.59 Convert begins (RateExchange) 3af884e5-8e97-44ea-aa0d-2c9e0418110b 2017-08-25T17:49:18.59 Convert ends (RateExchange) b8bd0701-515b-44fe-949f-5f5fb5a4590d 2017-08-25T17:49:18.60 Collect begins (AccountsReceivable) b8bd0701-515b-44fe-949f-5f5fb5a4590d 2017-08-25T17:49:18.60 Collect ends (AccountsReceivable) 8f7606b6-f3f7-4231-808d-d5e37f1f2201 2017-08-25T17:49:18.60 Collect ends (OrderCollector) beadabc4-df17-468f-8553-34ae4e3bdbfc 2017-08-25T17:49:18.60 Ship begins (OrderShipper) beadabc4-df17-468f-8553-34ae4e3bdbfc 2017-08-25T17:49:18.61 Ship ends (OrderShipper) 4ad34380-6826-440c-8d81-64bbd1f36d39 2017-08-25T17:49:18.61 Process ends (OrderProcessor)</pre> </p> <p> This is similar to the output from the previous article. </p> <h3 id="7ade5137341745a9896111d0ebaf5915"> Summary <a href="#7ade5137341745a9896111d0ebaf5915" title="permalink">#</a> </h3> <p> When writing object-oriented code, I still prefer Pure DI over using a DI Container, but if I absolutely needed to decorate many services, I'd seriously consider using a DI Container with run-time Interception capabilities. The need rarely materialises, though. </p> <p> As an intermediate solution, you can use a delegation-based design like the one shown here. As always, it's all a matter of balancing the constraints and goals of the specific situation. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Test Data Generator functor https://blog.ploeh.dk/2017/09/18/the-test-data-generator-functor 2017-09-18T07:55:00+00:00 Mark Seemann <div id="post"> <p> <em>A Test Data Generator modelled as a functor.</em> </p> <p> In a <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">previous article series</a>, you learned that while it's possible to model <a href="http://www.natpryce.com/articles/000714.html">Test Data Builders</a> as a <a href="/2018/03/22/functors">functor</a>, it adds little value. You shouldn't, however, dismiss the value of functors. It's an abstraction that applies broadly. </p> <p> Closely related to Test Data Builders is the concept of a generator of random test data. You could call it a Test Data Generator instead. Such a generator can be modelled as a functor. </p> <h3 id="172a41b93205462daf7421126d5d929a"> A C# Generator <a href="#172a41b93205462daf7421126d5d929a" title="permalink">#</a> </h3> <p> At its core, the idea behind a Test Data Generator is to create random test data. Still, you'll like to be able control various parts of the process, because you'd often need to pin parts of the generated data to deterministic values, while allowing other parts to vary randomly. </p> <p> In C#, you can write a generic Generator like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;generate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Generator(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;generate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(generate&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(generate)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.generate&nbsp;=&nbsp;generate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T1</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(f&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(f)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;newGenerator&nbsp;=&nbsp;r&nbsp;=&gt;&nbsp;f(<span style="color:blue;">this</span>.generate(r)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T1</span>&gt;(newGenerator); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Generate(<span style="color:#2b91af;">Random</span>&nbsp;random) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(random&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(random)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.generate(random); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Generate</code> method takes a <code>Random</code> object as input, and produces a value of the generic type <code>T</code> as output. This enables you to deterministically reproduce a particular randomly generated value, if you know the seed of the <code>Random</code> object. </p> <p> Notice how <code>Generator&lt;T&gt;</code> is a simple <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> over a (lazily evaluated) function. This function also takes a <code>Random</code> object as input, and produces a <code>T</code> value as output. (For the FP enthusiasts, this is simply the <em>Reader functor</em> in disguise.) </p> <p> The <code>Select</code> method makes <code>Generator&lt;T&gt;</code> a functor. It takes a map function <code>f</code> as input, and uses it to define a new <code>generate</code> function. The return value is a <code>Generator&lt;T1&gt;</code>. </p> <h3 id="8560380430794488bca9f6cdef8846e6"> General-purpose building blocks <a href="#8560380430794488bca9f6cdef8846e6" title="permalink">#</a> </h3> <p> Functors are immanently composable. You can compose complex Test Data Generators from simpler building blocks, like the following. </p> <p> For instance, you may need a generator of alphanumeric strings. You can write it like this: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;alphaNumericCharacters&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span>; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;AlphaNumericString&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:blue;">string</span>&gt;(r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;length&nbsp;=&nbsp;r.Next(25);&nbsp;<span style="color:green;">//&nbsp;Arbitrarily&nbsp;chosen&nbsp;max&nbsp;length</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;chars&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">char</span>[length]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;(<span style="color:blue;">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;length;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;idx&nbsp;=&nbsp;r.Next(alphaNumericCharacters.Length); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chars[i]&nbsp;=&nbsp;alphaNumericCharacters[idx]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(chars); &nbsp;&nbsp;&nbsp;&nbsp;});</pre> </p> <p> This <code>Generator&lt;string&gt;</code> can generate a random string with alphanumeric characters. It randomly picks a length between 0 and 24, and fills it with randomly selected alphanumeric characters. The maximum length of 24 is arbitrarily chosen. The generated string may be empty. </p> <p> Notice that the argument passed to the constructor is a function. It's not evaluated at initialisation, but only if <code>Generate</code> is called. </p> <p> The <code>r</code> argument is the <code>Random</code> object passed to <code>Generate</code>. </p> <p> Another useful general-purpose building block is a generator that can use a single-object generator to create many objects: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;Many&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;generator) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;(r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;length&nbsp;=&nbsp;r.Next(25);&nbsp;<span style="color:green;">//&nbsp;Arbitrarily&nbsp;chosen&nbsp;max&nbsp;length</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;elements&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">T</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;(<span style="color:blue;">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;length;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elements.Add(generator.Generate(r)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;elements; &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> This method takes a <code>Generator&lt;T&gt;</code> as input, and uses it to generate zero or more <code>T</code> objects. Again, the maximum length of 24 is arbitrarily chosen. It could have been a method argument, but in order to keep the example simple, I hard-coded it. </p> <h3 id="ec28447ed46c4776a3b70bf5a6c526b0"> Domain-specific generators <a href="#ec28447ed46c4776a3b70bf5a6c526b0" title="permalink">#</a> </h3> <p> From such general-purpose building blocks, you can define custom generators for your domain model. This enables you to use such generators in your unit tests. </p> <p> In order to generate post codes, you can combine the <code>AlphaNumericString</code> and the <code>Many</code> generators: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">PostCode</span>&gt;&nbsp;PostCode&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">PostCode</span>&gt;(r&nbsp;=&gt;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;postCodes&nbsp;=&nbsp;Many(AlphaNumericString).Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCode</span>(postCodes.ToArray()); &nbsp;&nbsp;&nbsp;&nbsp;});</pre> </p> <p> The <code>PostCode</code> class is part of your domain model; it takes an array of strings as input to its constructor. The <code>PostCode</code> generator uses the <code>AlphaNumericString</code> generator as input to the <code>Many</code> method. This generates zero or many alphanumeric strings, which you can pass to the <code>PostCode</code> constructor. </p> <p> This, in turn, gives you all the building blocks you need to generate <code>Address</code> objects: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">Address</span>&gt;&nbsp;Address&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">Address</span>&gt;(r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;street&nbsp;=&nbsp;AlphaNumericString.Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;city&nbsp;=&nbsp;AlphaNumericString.Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;postCode&nbsp;=&nbsp;PostCode.Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(street,&nbsp;city,&nbsp;postCode); &nbsp;&nbsp;&nbsp;&nbsp;});</pre> </p> <p> This <code>Generator&lt;Address&gt;</code> uses the <code>AlphaNumericString</code> generator to generate street and city strings. It uses the <code>PostCode</code> generator to generate a <code>PostCode</code> object. All these objects are passed to the <code>Address</code> constructor. </p> <p> Keep in mind that all of this logic is defined in lazily evaluated functions. Only when you invoke the <code>Generate</code> method on a generator does the code execute. </p> <h3 id="2ea62405b8034c3c83a4b2c179f1abd4"> Generating values <a href="#2ea62405b8034c3c83a4b2c179f1abd4" title="permalink">#</a> </h3> <p> You can now write tests similar to the tests shown in the <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">article series about Test Data Builders</a>. If, for example, you need an address in Paris, you can generate it like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;rnd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Random</span>(); <span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:#2b91af;">Gen</span>.Address.Select(a&nbsp;=&gt;&nbsp;a.WithCity(<span style="color:#a31515;">&quot;Paris&quot;</span>)).Generate(rnd);</pre> </p> <p> <code>Gen.Address</code> is the <code>Address</code> generator shown above; I put all those generators in a static class called <code>Gen</code>. If you don't modify it, <code>Gen.Address</code> will generate a random <code>Address</code> object, but by using <code>Select</code>, you can pin the city to Paris. </p> <p> You can also start with one type of generator and use <code>Select</code> to map to another type of generator, like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;rnd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Random</span>(); <span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:#2b91af;">Gen</span>.PostCode &nbsp;&nbsp;&nbsp;&nbsp;.Select(pc&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;pc)) &nbsp;&nbsp;&nbsp;&nbsp;.Generate(rnd);</pre> </p> <p> You use <code>Gen.PostCode</code> as the initial generator, and then <code>Select</code> a new <code>Address</code> in Rue Morgue, Paris, with a randomly generated post code. </p> <h3 id="8dc8f7e99ebe4ab294487d1573b908d7"> Functor <a href="#8dc8f7e99ebe4ab294487d1573b908d7" title="permalink">#</a> </h3> <p> Such a Test Data Generator is a functor. One way to see that is to use query syntax instead of the <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent API</a>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;rnd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Random</span>(); <span style="color:blue;">var</span>&nbsp;address&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">from</span>&nbsp;a&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.Address&nbsp;<span style="color:blue;">select</span>&nbsp;a.WithCity(<span style="color:#a31515;">&quot;Paris&quot;</span>)).Generate(rnd);</pre> </p> <p> Likewise, you can also translate the Rue Morgue generator to query syntax: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;pc&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.PostCode &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;pc)).Generate(rnd);</pre> </p> <p> This is, however, awkward, because you have to enclose the query expression in brackets in order to be able to invoke the <code>Generate</code> method. Alternatively, you can separate the query from the generation, like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;g&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;a&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.Address&nbsp;<span style="color:blue;">select</span>&nbsp;a.WithCity(<span style="color:#a31515;">&quot;Paris&quot;</span>); <span style="color:blue;">var</span>&nbsp;rnd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Random</span>(); <span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;g.Generate(rnd);</pre> </p> <p> Or this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;g&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;pc&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Gen</span>.PostCode &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;pc); <span style="color:blue;">var</span>&nbsp;rnd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Random</span>(); <span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;g.Generate(rnd);</pre> </p> <p> You'd probably still prefer the fluent API over this syntax. The reason I show this alternative is to demonstrate that the functor gives you the ability to separate the definition of data generation from the actual generation. In order to emphasise this point, I defined the <code>g</code> variables <em>before</em> creating the <code>Random</code> object <code>rnd</code>. </p> <h3 id="bfe9194e114c4ae3b042277cd38b5ca7"> Property-based testing <a href="#bfe9194e114c4ae3b042277cd38b5ca7" title="permalink">#</a> </h3> <p> The above <code>Generator&lt;T&gt;</code> is only a crude example of a Test Data Generator. In order to demonstrate how such a generator is a functor, I left out several useful features. Still, this should have given you a sense for how the <code>Generator&lt;T&gt;</code> class itself, as well as such general-purpose building blocks as <code>Many</code> and <code>AlphaNumericString</code>, could be packaged in a reusable library. </p> <p> The examples above show how to use a generator to create a single random object. You could, however, easily generate many (say, 100) random objects, and run unit tests for each object created. This is the idea behind property-based testing. </p> <p> There's more to property-based testing than generation of random values, but the implementations I've seen are all based on Test Data Generators as functors (and monads). </p> <h3 id="1c3e32c703d54258ab665d17450f2657"> FsCheck <a href="#1c3e32c703d54258ab665d17450f2657" title="permalink">#</a> </h3> <p> <a href="https://fscheck.github.io/FsCheck">FsCheck</a> is an open source F# library for property-based testing. It defines a <code>Gen</code> functor (and monad) that you can use to generate <code>Address</code> values, just like the above examples: </p> <p> <pre><span style="color:blue;">let!</span>&nbsp;address&nbsp;=&nbsp;<span style="color:teal;">Gen</span>.address&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;a&nbsp;<span style="color:blue;">with</span>&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>}&nbsp;) </pre> </p> <p> Here, <code>Gen.address</code> is a <code>Gen&lt;Address&gt;</code> value. By itself, it'll generate random <code>Address</code> values, but by using <code>Gen.map</code>, you can pin the city to Paris. </p> <p> The <code>map</code> function corresponds to the C# <code>Select</code> method. In functional programming, <em>map</em> is the most common name, although Haskell calls the function <code>fmap</code>; the <code>Select</code> name is, in fact, the odd man out. </p> <p> Likewise, you can map from one generator type to another: </p> <p> <pre><span style="color:blue;">let!</span>&nbsp;address&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Gen</span>.postCode &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;pc&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Street&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>;&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>;&nbsp;PostCode&nbsp;=&nbsp;pc&nbsp;})</pre> </p> <p> This example uses <code>Gen.postCode</code> as the initial generator. This is, as the name implies, a <code>Gen&lt;PostCode&gt;</code> value. For every random <code>PostCode</code> value generated, <code>map</code> turns it into an address in Rue Morgue, Paris. </p> <p> There's more going on here than I'd like to cover in this article. The use of <code>let!</code> syntax actually requires <code>Gen&lt;'a&gt;</code> to be a monad (which it is), but that's a topic for another day. Both of these examples are contained in a computation expression, and the implication of that is that the <code>address</code> values represent a multitude of randomly generated <code>Address</code> values. </p> <h3 id="5bd990290ff048c2a7b55b740053831d"> Hedgehog <a href="#5bd990290ff048c2a7b55b740053831d" title="permalink">#</a> </h3> <p> <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> is another open source F# library for property-based testing. With Hedgehog, the <code>Address</code> code examples look like this: </p> <p> <pre><span style="color:blue;">let!</span>&nbsp;address&nbsp;=&nbsp;<span style="color:teal;">Gen</span>.address&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;a&nbsp;<span style="color:blue;">with</span>&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>}&nbsp;) </pre> </p> <p> And: </p> <p> <pre><span style="color:blue;">let!</span>&nbsp;address&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Gen</span>.postCode &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;pc&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Street&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>;&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>;&nbsp;PostCode&nbsp;=&nbsp;pc&nbsp;})</pre> </p> <p> Did you notice something? </p> <p> This is <em>literally</em> the same syntax as FsCheck! This isn't because Hedgehog is copying FsCheck, but because both are based on the same underlying abstraction: functor (and monad). There are other parts of the API where Hedgehog differs from FsCheck, but their generators are similar. </p> <p> This is one of the most important advantages of using well-known abstractions like functors. Once you understand such an abstraction, it's easy to learn a new library. With professional experience with FsCheck, it only took me a few minutes to figure out how to use Hedgehog. </p> <h3 id="0105d5b72e6a4161ac88395a1406530b"> Summary <a href="#0105d5b72e6a4161ac88395a1406530b" title="permalink">#</a> </h3> <p> Functors are well-defined objects from category theory. It may seem abstract, and far removed from 'real' programming, but it's extraordinarily useful. Many category theory abstractions can be applied to a host of different situations. Once you've learned what a functor is, you'll find it easy to learn to use new libraries that build on that abstraction. </p> <p> In this article you saw a sketch of how the functor abstraction can be used to model Test Data Generators. Contrary to Test Data Builders, which turned out to be a redundant abstraction, a Test Data Generator is truly useful. </p> <p> Many years ago, I had the idea to create a Test Data Generator for unit testing purposes. I called it <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>, and although it's had some success, the API isn't as clean as it could be. Back then, I didn't know about functors, so I had to invent an API for AutoFixture. This API is proprietary to AutoFixture, so anyone learning AutoFixture must learn this particular API, and its abstractions. It would have been so much easier for all involved if I had designed AutoFixture as a functor instead. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="ee546e7d05a14126be1139d1230b3031"> <div class="comment-author"> <a href="http://www.temporalcohesion.co.uk">Stuart Grassie</a> <a href="#ee546e7d05a14126be1139d1230b3031">#</a></div> <div class="comment-content"> <p>I'm curious as to what the "useful features" are that that you left out of the Test Data Generator?</p> </div> <div class="comment-date">2017-10-23 16:36 UTC</div> </div> <div class="comment" id="42fe8e335f7d422e96e953c540f996e2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#42fe8e335f7d422e96e953c540f996e2">#</a></div> <div class="comment-content"> <p> Stuart, thank you for writing. Test Data Generators like the one described here are rich data structures that you can do a lot of interesting things with. As described here, the generator only generates a single value every time you invoke its <code>Generate</code> method. What property-based testing libraries like <a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a>, <a href="https://fscheck.github.io/FsCheck">FsCheck</a>, and <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> do is that instead of a single random value, they generate <em>many</em> values (the default number seems to be 100). </p> <p> These property-based testing libraries tend to then 'elevate' their generators into another type of data structure called <em>Arbitraries</em>, and these again into <em>Properties</em>. What typically happens is that they use the Generators to generate values, but for each generated value, they evaluate the associated Property. If all Properties succeed, nothing more happens, but in the case of a test failure, no more values are generated. Instead, the libraries switch to a state where they attempt to <em>shrink</em> the counter-example to a simpler counter-example. It uses a <em>Shrinker</em> associated with the Arbitrary to do this. The end result is that if your test doesn't hold, you'll get an easy-to-understand example of the input that caused the test to fail. </p> <p> Apart from that, there are many other features of Test Data Generators that I left out. Some of these include ways to combine several Generators to a single Generator. It turns out that Test Data Generators are also <em>Applicative Functors</em> and <em>Monads</em>, and you can use these traits to define powerful combinators. In the future, I'll publish more articles on this topic, but it'll take months, because my article queue has quite a few other articles in front of those. </p> <p> If you want to explore this topic, I'd recommend playing with FsCheck. While it's written in F#, it also works from C#, and its documentation includes C# examples as well. Hedgehog may also work from C#, but being a newer, more experimental library, its documentation is still sparse. </p> </div> <div class="comment-date">2017-10-24 7:53 UTC</div> </div> <div class="comment" id="75fe5e668f7d422e96e953c540f996e5"> <div class="comment-author"><a href="http://blog.nikosbaxevanis.com">Nikos Baxevanis</a> <a href="#75fe5e668f7d422e96e953c540f996e5">#</a></div> <div class="comment-content"> <blockquote> Hedgehog may also work from C# </blockquote> <p> That's right. Hedgehog <a href="https://github.com/moodmosaic/fsharp-hedgehog/blob/master/tests/Hedgehog.CSharp.Tests/LinqTests.cs">may be used from C#</a> as well. </p> </div> <div class="comment-date">2018-11-13 09:53 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Test data without Builders https://blog.ploeh.dk/2017/09/11/test-data-without-builders 2017-09-11T07:28:00+00:00 Mark Seemann <div id="post"> <p> <em>We don't need no steenkin' Test Data Builders!</em> </p> <p> This is the fifth and final in <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">a series of articles about the relationship between the Test Data Builder design pattern, and the identity functor</a>. In the <a href="/2017/09/04/builder-as-identity">previous article</a>, you learned why a Builder <a href="/2018/03/22/functors">functor</a> adds little value. In this article, you'll see what to do instead. </p> <h3 id="605d0002b1cc4051a7f5597b82694353"> From Identity to naked values <a href="#605d0002b1cc4051a7f5597b82694353" title="permalink">#</a> </h3> <p> While you can define <a href="http://www.natpryce.com/articles/000714.html">Test Data Builders</a> with Haskell's <code>Identity</code> functor, it adds little value: </p> <p> <pre><span style="color:#dd0000;">Identity</span>&nbsp;address&nbsp;<span style="color:#666666;">=</span>&nbsp;fmap&nbsp;(<span style="color:#666666;">\</span>a&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;a&nbsp;{&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;})&nbsp;addressBuilder </pre> </p> <p> That's nothing but an overly complicated way to create a data value from another data value. You can simplify the code from the previous article. First, instead of calling them 'Builders', we should be honest and name them as the default values they are: </p> <p> <pre><span style="color:#600277;">defaultPostCode</span>&nbsp;::&nbsp;<span style="color:blue;">PostCode</span> defaultPostCode&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">PostCode</span>&nbsp;<span style="color:blue;">[]</span> <span style="color:#600277;">defaultAddress</span>&nbsp;::&nbsp;<span style="color:blue;">Address</span> defaultAddress&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Address</span>&nbsp;{&nbsp;street&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;postCode&nbsp;<span style="color:#666666;">=</span>&nbsp;defaultPostCode&nbsp;}</pre> </p> <p> <code>defaultPostCode</code> is nothing but an empty <code>PostCode</code> value, and <code>defaultAddress</code> is an <code>Address</code> value with empty constituent values. Notice that <code>defaultAddress</code> uses <code>defaultPostCode</code> for the <code>postCode</code> value. </p> <p> If you need a value in Paris, you can simply write it like this: </p> <p> <pre>address&nbsp;<span style="color:#666666;">=</span>&nbsp;defaultAddress&nbsp;{&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;} </pre> </p> <p> Likewise, if you need a more specific address, but you don't care about the post code, you can write it like this: </p> <p> <pre>address&#39;&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#dd0000;">Address</span>&nbsp;{&nbsp;street&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>,&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;postCode&nbsp;<span style="color:#666666;">=</span>&nbsp;defaultPostCode&nbsp;}</pre> </p> <p> Notice how much simpler this is. There's no need to call <code>fmap</code> in order to pull the 'underlying value' out of the functor, transform it, and put it back in the functor. Haskell's 'copy and update' syntax gives you this ability for free. It's built into the language. </p> <h3 id="42f9a1bfa88649d9a9f1ac610b1e74a8"> Building F# values <a href="#42f9a1bfa88649d9a9f1ac610b1e74a8" title="permalink">#</a> </h3> <p> Haskell isn't the only language with 'copy and update' syntax. F# has it as well, and in fact, it's from <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/copy-and-update-record-expressions">the F# documentation</a> that I've taken the 'copy and update' term. </p> <p> The code corresponding to the above Haskell code looks like this in F#: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;defaultPostCode&nbsp;=&nbsp;<span style="color:navy;">PostCode</span>&nbsp;[] <span style="color:blue;">let</span>&nbsp;defaultAddress&nbsp;=&nbsp;{&nbsp;Street&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>;&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>;&nbsp;PostCode&nbsp;=&nbsp;defaultPostCode&nbsp;} <span style="color:blue;">let</span>&nbsp;address&nbsp;=&nbsp;{&nbsp;defaultAddress&nbsp;<span style="color:blue;">with</span>&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;} <span style="color:blue;">let</span>&nbsp;address&#39;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Street&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>;&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>;&nbsp;PostCode&nbsp;=&nbsp;defaultPostCode&nbsp;}</pre> </p> <p> The syntax is a little different, but the concepts are the same. F# adds the keyword <code>with</code> to 'copy and update' expressions, which translates easily back to C# <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent interfaces</a>. </p> <h3 id="346c3baa49734607a336424f16d614a8"> Building C# objects <a href="#346c3baa49734607a336424f16d614a8" title="permalink">#</a> </h3> <p> In a <a href="/2017/08/21/generalised-test-data-builder">previous article</a>, you saw how to refactor your domain model to a model of <a href="https://martinfowler.com/bliki/ValueObject.html">Value Objects</a> with fluent interfaces. </p> <p> In your unit tests, you can define natural default values for testing purposes: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Natural</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">PostCode</span>&nbsp;PostCode&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCode</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;Address&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;PostCode); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">InvoiceLine</span>&nbsp;InvoiceLine&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvoiceLine</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#2b91af;">PoundsShillingsPence</span>.Zero); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Recipient</span>&nbsp;Recipient&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Recipient</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;Address); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Invoice</span>&nbsp;Invoice&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Invoice</span>(Recipient,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvoiceLine</span>[0]); }</pre> </p> <p> This static <code>Natural</code> class is a test-specific container of 'good' default values. Notice how, once more, the <code>Address</code> value uses the <code>PostCode</code> value to fill in the <code>PostCode</code> property of the default <code>Address</code> value. </p> <p> With these default test values, and the fluent interface of your domain model, you can easily build a test address in Paris: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:#2b91af;">Natural</span>.Address.WithCity(<span style="color:#a31515;">&quot;Paris&quot;</span>); </pre> </p> <p> Because <code>Natural.Address</code> is an <code>Address</code> object, you can use its <code>WithCity</code> method to build a test address in Paris, and where all other constituent values remain the default values. </p> <p> Likewise, you can create an address on Rue Morgue, but with a default post code: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;<span style="color:#2b91af;">Natural</span>.PostCode); </pre> </p> <p> Here, you can simply create a new <code>Address</code> object, but with <code>Natural.PostCode</code> as the post code value. </p> <h3 id="38af881d8cbe4846a348e889e7f8ecd5"> Conclusion <a href="#38af881d8cbe4846a348e889e7f8ecd5" title="permalink">#</a> </h3> <p> Using a fluent domain model obviates the need for Test Data Builders. There's a tendency among functional programmers to overbearingly state that design patterns are nothing but recipes to overcome deficiencies in particular programming languages or paradigms. If you believe such a claim, at least it ought to go both ways, but at the conclusion of this article series, I hope I've been able to demonstrate that this is true for the Test Data Builder pattern. You only need it for 'classic', mutable, object-oriented domain models. <ol> <li>For mutable object models, use Test Data Builders.</li> <li>Consider, however, modelling your domain with Value Objects and 'copy and update' instance methods.</li> <li>Even better, consider using a programming language with built-in 'copy and update' expressions.</li> </ol> If you're stuck with a language like C# or Java, you don't get language-level support for 'copy and update' expressions. This means that you'll still need to incur the cost of adding and maintaining all those <code>With[...]</code> methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Invoice</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Recipient</span>&nbsp;Recipient&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;&nbsp;Lines&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Invoice( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Recipient</span>&nbsp;recipient, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;&nbsp;lines) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(recipient&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(recipient)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(lines&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(lines)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Recipient&nbsp;=&nbsp;recipient; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Lines&nbsp;=&nbsp;lines; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Invoice</span>&nbsp;WithRecipient(<span style="color:#2b91af;">Recipient</span>&nbsp;newRecipient) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Invoice</span>(newRecipient,&nbsp;<span style="color:blue;">this</span>.Lines); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Invoice</span>&nbsp;WithLines(<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;&nbsp;newLines) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Invoice</span>(<span style="color:blue;">this</span>.Recipient,&nbsp;newLines); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;other&nbsp;=&nbsp;obj&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:#2b91af;">Invoice</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">base</span>.Equals(obj); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.Recipient,&nbsp;other.Recipient) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:#2b91af;">Enumerable</span>.SequenceEqual( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Lines.OrderBy(l&nbsp;=&gt;&nbsp;l.Name), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.Lines.OrderBy(l&nbsp;=&gt;&nbsp;l.Name)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Recipient.GetHashCode()&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Lines.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> That may seem like quite a maintenance burden (and it is), but consider that it has the same degree of complexity and overhead as defining a Test Data Builder for each domain object. At least, by putting this extra code in your domain model, you make all of that API (all the <code>With[...]</code> methods, and the structural equality) available to other production code. In my experience, that's a better return of investment than isolating such useful features only to test code. </p> <p> Still, once you've tried using a language like F# or Haskell, where 'copy and update' expressions come with the language, you realise how much redundant code you're writing in C# or Java. The Test Data Builder design pattern truly is a recipe that addresses deficiencies in particular languages. </p> <p> <strong>Next:</strong> <a href="/2017/09/18/the-test-data-generator-functor">The Test Data Generator functor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="284f9c7eb52f499394c834b832897871"> <div class="comment-author"><a href="https://www.linkedin.com/in/romainvasseur/">Romain Vasseur</a> <a href="#284f9c7eb52f499394c834b832897871">#</a></div> <div>Hi Marks, thanks for the whole serie. I personally tend to split my class into 2: 'core' feature and syntactic sugar one.<br>Leveraging extension methods to implement 'With' API is relatively straightforward and you have both developper friendly API and a great separation of concern namely definition and usage.<br>If you choose to implement extensions in another assembly you could manage who have access to it: unit test only, another assembly, whole project.<br>You can split API according to context/user too. It can also be useful to enforce some guidelines.</div> <div class="comment-date">2017-09-12 09:20 UTC</div> </div> <div class="comment" id="b0d26173257c41eeb31fba25c1ceabd5"> <div class="comment-author"><a href="https://github.com/bigdnf">Dominik Jeske</a> <a href="#b0d26173257c41eeb31fba25c1ceabd5">#</a></div> <div>Hi Marks, what do you think about using Roslyn to generate builders? Using helpers like this <a href="https://github.com/AArnott/CodeGeneration.Roslyn">CodeGeneration.Roslyn</a> you can generate all (With*) in compile time so there is no IL injection magic. <br> I have some ugly POC code in my branch <a href="https://github.com/bigdnf/HomeCenter/blob/master/Core/HomeCenter.CodeGeneration/TestBuilerGenerator.cs">Roslyn builder generator</a> - it is only a starting point but I think it has some potential.</div> <div class="comment-date">2019-03-19 18:18 UTC</div> </div> <div class="comment" id="26e12a445257444cb8ccbcfffb3aee9b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#26e12a445257444cb8ccbcfffb3aee9b">#</a></div> <div class="comment-content"> <p> Dominik, thank you for writing. I admit that I haven't given this much thought, but it strikes me as one of those 'interesting problems' that programmers are keen to solve. It looks to me like a bit of a red herring, as I tend to be sceptical of schemes to generate code. What problem does it address? That one has to type? <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">That's rarely the bottleneck in software development.</a> </p> <p> Granted, it gets tedious to manually add all those <code>With[...]</code> methods, but there's a lot of things about C# that's tedious. There's a reason I prefer F# instead. </p> </div> <div class="comment-date">2019-03-20 7:16 UTC</div> </div> <div class="comment" id="af975bc3fba448eab33801449cd63e5a"> <div class="comment-author"><a href="https://github.com/bigdnf">Dominik Jeske</a> <a href="#af975bc3fba448eab33801449cd63e5a">#</a></div> <div> <p> Thanks for respond - I think that for each comment you now have 1+ blog post to respond ;). Despite the fact that I should consider learning new language like F# to open my mind I will focus on c# aspect. </p> <p> I understand your consideration about code generation but I thing that when we repeat some actions over and over we automatically think about some automations - this is the source of computers I think. Currently I'm working in project where we use Test Builder Pattern heavily and every time I think about writing another builder my motivation is decreasing because psychologically is not interesting anymore and I would be happy to give that to someone else or machine. </p> <p> When I started to understand what is Roslyn and what it can do it just open my eyes to new opportunities. Generating some simple but frequently repeating code give me more time on focusing on real domain problems and keep my frustration level on low position :) </p> <p> Of course this is not BIG problem solver but only new approach for simplification of daily tasks - another advantage is that Roslyn I creating normal c# code file that can be navigated from code, can be seen in debugger (in contrast to IL injectors), so there is no magical black boxes. Disadvantage is that currently generating code is very simple - it involves some external nugets and I feel that writing generator in Roslyn could be simplified; </p> <p> ps. Commenting via pull request is interesting experience - feels like pro ;) </p> </div> <div class="comment-date">2019-03-20 08:48 UTC</div> </div> <div class="comment" id="f657b7db01d24d6cbc1b358ed6a13a20"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f657b7db01d24d6cbc1b358ed6a13a20">#</a></div> <div class="comment-content"> <p> Dominik, while it isn't based on Roslyn, are you aware of <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>? </p> </div> <div class="comment-date">2019-03-21 7:01 UTC</div> </div> <div class="comment" id="1f2ff161eeb3490eb80ff6547b0fc134"> <div class="comment-author"><a href="https://github.com/bigdnf">Dominik Jeske</a> <a href="#1f2ff161eeb3490eb80ff6547b0fc134">#</a></div> <div> <p> Yes, I discovered this tool together with your blog ;) I think it is good enough - Roslyn approach is only alternative not basing on reflection or IL injection. </p> <p> I will try to use AutoFixture in next project so I will see it will survive my requirements. </p> </div> <div class="comment-date">2019-03-26 21:59 UTC</div> </div> <div id="cb79773e74624514a1801af115f52f6b" class="comment"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#cb79773e74624514a1801af115f52f6b">#</a></div> <div> <p> If I understand correctly, one of your claims is that a fluent C# syntax for expressing change (i.e. "with" methods for an immutable value object) is equivalent to F#'s copy and update syntax for records in the sense that any code written with one can be written with the other. I agree with that. Then you pointed out some advantages with the F# syntax. Among the advantages of F#'s syntax is that there is less code to write in the first place and less code to maintain. </p> <p> I see an advantage with C#'s syntax. Suppose the only constructor of the value object is internal but all its properties and "with" methods are public. Then adding a new (public) property and corresponding (public) "with" method is not a breaking change. As far as I know, this is not possible with F#.* Either the record consturctor is public or it is not public. If the record's constructor is public, then the copy and update syntax is also public but adding a proprty to the record is a breaking change. Otherwise, the record's constructor is not public, so the copy and update syntax is not available. </p> <p> I have an extremely short list of advantages of C# over F#, and this is one of them. </p> <p> *It is possible to put an access modifier immediately after the equals sign when defining a record. However, the <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/records#syntax">documentation for record syntax</a> is <a href="https://github.com/dotnet/docs/issues/14564">missing this information</a>. When I try to put an access modifier before a field identifier, I get a compiler error that says <blockquote> FS0575 Accessibility modifiers are not permitted on record fields. Use 'type R = internal ...' or 'type R = private ...' to give an accessibility to the whole representation. </blockquote> </p> <p> P.S. For those that want to write functionally in C#, I recommend using <a href="https://github.com/louthy/language-ext">Langage Ext</a>. in particular, a somewhat recently added feature is <a href="https://github.com/louthy/language-ext#transformation-of-immutable-types">auto-generated "with" methods</a>. </p> </div> <div class="comment-date">2019-09-21 01:08 UTC</div> </div> <div class="comment" id="cb5fa591f062499db86d428fe4a02e6c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cb5fa591f062499db86d428fe4a02e6c">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Let's get the uncontroversial part of this discussion out of the way first: F# record types compile to IL that's equivalent to what a properly-written C# <a href="https://en.wikipedia.org/wiki/Value_object">Value Object</a> compiles to. At the IL level, there's no difference. </p> <p> At the language level, it's true that F# records is a specialised syntax that enables you to succinctly define static types to model data. It's not a general-purpose syntax, so there's definitely things it doesn't allow you to express. F# has normal class syntax for those needs. </p> <p> That record types aren't refactoring-safe is a known issue. This is true not only for F# records, but for Haskell <code>data</code> types as well. In Haskell public APIs, you sometimes see that combination that you describe. The type has a private constructor, but the library then provides functions to manipulate it (essentially copy-and-update functions). You sometimes see that in F# as well, but here a class would often have been a better choice. Haskell doesn't have object-oriented classes, so it has to resort to that sort of hack to keep APIs backwards compatible. </p> <p> When you write a public API in F#, choosing between a record and a class as a data carrier is an important choice. When APIs are published (e.g. on <a href="https://www.nuget.org">nuget.org</a>), you'll have little success with your library if you regularly introduce breaking changes. </p> <p> For internal use, the story is different. You can use F# records to express domain models with a few lines of code. If you later find out that you have to change the model, then you do that, and fix the ensuing compilation errors. </p> <p> Public APIs represent more work, regardless of the language in which they're written. Yes, you need to carefully and deliberately design a public library's API and data structures. I don't think, however, that that should detract us from using productive language features for application-specific use. </p> </div> <div class="comment-date">2019-09-21 18:28 UTC</div> </div> <div id="b30ddcf557964bf2a7b63c0e247294e1" class="comment"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#b30ddcf557964bf2a7b63c0e247294e1">#</a></div> <div> <p> <blockquote> Let's get the uncontroversial part of this discussion out of the way first... </blockquote> I am right with you. Your entire comment was uncontroversial to me :) </p> <p> <blockquote> When you write a public API in F#, choosing between a record and a class as a data carrier is an important choice. ... here a class would often have been a better choice. </blockquote> (I quoted you out of order there. I hope this doesn't misrepresent what you were saying. I don't think it does.) I am really interested to learn more about that. </p> <p> I found the series that includes this blog post when I searched on Google for "builder pattern F#". This series is primarily about the test data builder design pattern. As I understand it, I would describe this pattern as a special case of the (general case) builder design pattern in which all arguments have reasonable defaults. </p> <p> Have you ever written a builder that accepted multiple arguments one at a time none of which have reasonable defaults? Have you ever blogged about this (more general) build design pattern? </p> <p> As a good student of your ;) I wonder if the builder design pattern corresponds to some universal abstraction. Among the fluent interfaces that I am most impressed with are <a href="https://www.learnentityframeworkcore.com/configuration/fluent-api">configuration in Entity Framework</a> and <a href="https://fluentassertions.com/introduction">Fluent Assertions</a>. Of course I could try to make my own fluent interface by copying them, and that would probably work out reasonably well. At the same time, I would like to learn from you and your frustration (if that description is accurate) that you expressed (at the end of the <a href="https://blog.ploeh.dk/2017/09/18/the-test-data-generator-functor/">next and last post in this series</a>) with the API of your AutoFixture project failing to use a potential universal abstraction (namely functors). </p> </div> <div class="comment-date">2019-09-22 01:50 UTC</div> </div> <div class="comment" id="ba26e928d57d4b649942deb1e1ae18f1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ba26e928d57d4b649942deb1e1ae18f1">#</a></div> <div class="comment-content"> <p> Tyson, thank you for bringing the Builder pattern to my attention. I haven't written much about it yet, but I believe that it'd be a perfect fit for my <a href="/2018/03/05/some-design-patterns-as-universal-abstractions">article series on how certain design patterns relate to universal abstractions</a>. When I get some time, I'll have to write one or more articles about that topic. </p> <p> In short, though, I think that the Builder pattern as described in <a href="http://amzn.to/XBYukB">Design Patterns</a> is <a href="/2018/01/08/software-design-isomorphisms">isomorphic</a> to the Fluent Builder pattern, as you also imply. It remains for me to more formally argue that case, but in short, the Builder pattern is described as a set of virtual methods that return <code>void</code>. Since all these methods return <code>void</code>, each method could, instead, return the object to which it belongs, and that's what a Fluent Builder does. </p> <p> Once you return the Builder object, you could, instead of mutating and returning the instance, return a new object. That new object is a near-copy of the previous Builder, with only one change applied to it. Now you have a function that essentially takes a Builder as input, plus some other input, and returns a Builder. That's just <a href="/2019/01/28/better-abstractions-revisited/#047886dcfa5a4a1398965138669e0ddc">a curried endomorphism</a>. </p> <p> Once again, every time we run into a composable design pattern, it turns out to be a <a href="/2017/10/06/monoids">monoid</a>. It shouldn't surprise us much, though, since the original Builder pattern as described in <em>Design Patterns</em> has <code>void</code> methods, and <a href="/2018/03/12/composite-as-a-monoid">such methods compose</a>. </p> </div> <div class="comment-date">2019-09-29 20:31 UTC</div> </div> <div id="be62acdf925841e09485f4253ab0c5fe" class="comment"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#be62acdf925841e09485f4253ab0c5fe">#</a></div> <div> <p> The most formal treatment I have seen about fluent APIs was in <a href="https://blog.jooq.org/2012/01/05/the-java-fluent-api-designer-crash-course/">this blog post</a>. The context is that we are trying to create a word in some language specified by a grammar, and the methods in the fluent API correspond to production rules in the grammar. The company behind that blog post seems to able to generate a fluent API (in Java) given as input the produciton rules of a grammar. Their main use case appears to be creating a fluent API for constructing SQL queries against a database (presumably by first converting a database schema into corresponding grammar production rules). The end result reminds me of <a href="https://fsprojects.github.io/SQLProvider/">F#'s SQL type provider</a>. </p> </div> <div class="comment-date">2019-09-30 13:22 UTC</div> </div> <div class="comment" id="042e8f7aeba042659d9cf626f78f8636"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#042e8f7aeba042659d9cf626f78f8636">#</a></div> <div class="comment-content"> <p> Tyson, I've now published <a href="/2020/02/17/builder-as-a-monoid">an article</a> that hopefully answers some of your questions. I must admit that I'm still puzzled by this question: <blockquote> <p> "Have you ever written a builder that accepted multiple arguments one at a time none of which have reasonable defaults?" </p> </blockquote> If I left that unanswered, then at least I hope that I've managed to put enough building blocks into position to be able to address it. Can you elaborate? </p> </div> <div class="comment-date">2020-02-17 7:34 UTC</div> </div> <div id="2f7d14c6f1194c5e8b6380199e3e8581" class="comment"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#2f7d14c6f1194c5e8b6380199e3e8581">#</a></div> <div> <p> I have now elaborated in <a href="https://blog.ploeh.dk/2020/03/09/polymorphic-builder/#c1ea86ea39814f278e1a19f82b45622b">this comment</a>. Thanks for waiting :) </p> </div> <div class="comment-date">2020-03-11 18:31 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Builder as Identity https://blog.ploeh.dk/2017/09/04/builder-as-identity 2017-09-04T07:41:00+00:00 Mark Seemann <div id="post"> <p> <em>In which the Builder functor turns out to be nothing but the Identity functor in disguise.</em> </p> <p> This is the fourth in <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">a series of articles about the relationship between the Test Data Builder design pattern, and the identity functor</a>. In the <a href="/2017/08/28/the-builder-functor">previous article</a>, you saw how a generic <a href="http://www.natpryce.com/articles/000714.html">Test Data Builder</a> can be modelled as a <a href="/2018/03/22/functors">functor</a>. </p> <p> You may, however, be excused if you're slightly underwhelmed. Modelling a Test Data Builder as a functor doesn't seem to add much value. </p> <h3 id="95197f3fdf314e30a85733f8d33e7e85"> Haskell's Identity functor <a href="#95197f3fdf314e30a85733f8d33e7e85" title="permalink">#</a> </h3> <p> In the previous article, you saw the Builder functor implemented in various languages, including Haskell: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">Builder</span>&nbsp;a&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Builder</span>&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">Builder</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;fmap&nbsp;f&nbsp;(<span style="color:#dd0000;">Builder</span>&nbsp;a)&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Builder</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;f&nbsp;a</pre> </p> <p> The <code>fmap</code> implementation is literally a one-liner: pattern match the value <code>a</code> out of the <code>Builder</code>, call <code>f</code> with <code>a</code>, and package the result in a new <code>Builder</code> value. </p> <p> For many trivial functors, it turns out that the Glasgow Haskell Compiler (GHC) can automatically implement <code>fmap</code> with a language extension: </p> <p> <pre>{-#&nbsp;<span style="color:blue;">LANGUAGE</span>&nbsp;DeriveFunctor&nbsp;#-} <span style="color:blue;">module</span>&nbsp;Builder&nbsp;<span style="color:blue;">where</span> <span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">Builder</span>&nbsp;a&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Builder</span>&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Functor</span>)</pre> </p> <p> Notice the <code>DeriveFunctor</code> language extension. This enables the compiler to automatically implement <code>fmap</code> by adding <code>Functor</code> to the <code>deriving</code> list. </p> <p> Perhaps we should take this as a hint. If the compiler can automatically make <code>Builder</code> a <code>Functor</code>, perhaps it doesn't add that much value. </p> <p> This particular <code>Builder</code> is equivalent to Haskell's built-in <code>Identity</code> functor. <code>Identity</code> is a 'no-op' functor, if you will. While it's a functor, it doesn't 'do' anything. It's similar to the <a href="https://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a> design pattern, in the sense that the only value it adds is that it enables you to turn any naked value into a functor. This can occasionally be useful if you need to pass a functor to an API. </p> <h3 id="9d7794179fb7459bb5280570e47b6baa"> PostCode and Address builders <a href="#9d7794179fb7459bb5280570e47b6baa" title="permalink">#</a> </h3> <p> You can rewrite the previous <code>PostCode</code> and <code>Address</code> Test Data Builders as <code>Identity</code> values: </p> <p> <pre><span style="color:#600277;">postCodeBuilder</span>&nbsp;::&nbsp;<span style="color:blue;">Identity</span>&nbsp;<span style="color:blue;">PostCode</span> postCodeBuilder&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Identity</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">PostCode</span>&nbsp;<span style="color:blue;">[]</span> <span style="color:#600277;">addressBuilder</span>&nbsp;::&nbsp;<span style="color:blue;">Identity</span>&nbsp;<span style="color:blue;">Address</span> addressBuilder&nbsp;&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#dd0000;">Identity</span>&nbsp;<span style="color:#dd0000;">Address</span>&nbsp;{&nbsp;street&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;postCode&nbsp;<span style="color:#666666;">=</span>&nbsp;pc&nbsp;} &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;<span style="color:#dd0000;">Identity</span>&nbsp;pc&nbsp;<span style="color:#666666;">=</span>&nbsp;postCodeBuilder</pre> </p> <p> As in the previous examples, <code>postCodeBuilder</code> is nothing but a 'good' default <code>PostCode</code> value. This time, it's turned into an <code>Identity</code> value, instead of a <code>Builder</code> value. The same is true for <code>addressBuilder</code> - notice that it uses <code>postCodeBuilder</code> for the <code>postCode</code> value. </p> <p> This enables you to build an address in Paris, like previous examples: </p> <p> <pre><span style="color:#dd0000;">Identity</span>&nbsp;address&nbsp;<span style="color:#666666;">=</span>&nbsp;fmap&nbsp;(<span style="color:#666666;">\</span>a&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;a&nbsp;{&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;})&nbsp;addressBuilder </pre> </p> <p> This builds an address with <code>city</code> bound to <code>"Paris"</code>, but with all other values still at their default values: </p> <p> <pre><span style="color:#dd0000;">Address</span>&nbsp;{street&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;postCode&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">PostCode</span>&nbsp;<span style="color:blue;">[]</span>} </pre> </p> <p> You can also build an address from an <code>Identity</code> of a different generic type: </p> <p> <pre><span style="color:#dd0000;">Identity</span>&nbsp;address&#39;&nbsp;<span style="color:#666666;">=</span>&nbsp;fmap&nbsp;newAddress&nbsp;postCodeBuilder &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;newAddress&nbsp;pc&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Address</span>&nbsp;{&nbsp;street&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>,&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;postCode&nbsp;<span style="color:#666666;">=</span>&nbsp;pc&nbsp;}</pre> </p> <p> Notice that this example uses <code>postCodeBuilder</code> as an origin, but creates a new <code>Address</code> value. In this expression, <code>newAddress</code> is a local function that takes a <code>PostCode</code> value as input, and returns an <code>Address</code> value as output. </p> <h3 id="b9e52277f5384ec9b4764b322b748be0"> Summary <a href="#b9e52277f5384ec9b4764b322b748be0" title="permalink">#</a> </h3> <p> Neither F# nor C# comes with a built-in identity functor, but it'd be as <a href="/2018/09/03/the-identity-functor">trivial to create them</a> as the code you've already seen. In the previous article, you saw how to define a <code>Builder&lt;'a&gt;</code> type in F#. All you have to do is to change its name to <code>Identity&lt;'a&gt;</code>, and you have the identity functor. You can perform a similar rename for the C# code in the previous articles. </p> <p> Since the Identity functor doesn't really 'do' anything, there's no reason to use it for building test values. In the next article, you'll see how to discard the functor and in the process make your code simpler. </p> <p> <strong>Next:</strong> <a href="/2017/09/11/test-data-without-builders">Test data without Builders</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Builder functor https://blog.ploeh.dk/2017/08/28/the-builder-functor 2017-08-28T11:19:00+00:00 Mark Seemann <div id="post"> <p> <em>The Test Data Builder design pattern as a functor.</em> </p> <p> This is the third in <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">a series of articles about the relationship between the Test Data Builder design pattern, and the identity functor</a>. The <a href="/2017/08/21/generalised-test-data-builder">previous article</a> introduced this generic Builder class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Builder(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T1</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newItem&nbsp;=&nbsp;f(<span style="color:blue;">this</span>.item); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T1</span>&gt;(newItem); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Build() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;other&nbsp;=&nbsp;obj&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T</span>&gt;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">base</span>.Equals(obj); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.item,&nbsp;other.item); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.item.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The workhorse is the <code>Select</code> method. As I previously promised to explain, there's a reason I chose that particular name. </p> <h3 id="bc063738346a415586162fa7e8263908"> Query syntax <a href="#bc063738346a415586162fa7e8263908" title="permalink">#</a> </h3> <p> C# comes with a small DSL normally known as <em>query syntax</em>. People mostly think of it in relation to ORMs such as Entity Framework, but it's a general-purpose language feature. Still, most developers probably associate it with the <code>IEnumerable&lt;T&gt;</code> interface, but it's more general than that. In fact, any type that comes with a <code>Select</code> method with a compatible signature supports query syntax. </p> <p> I deliberately designed the Builder's <code>Select</code> method to support query syntax: </p> <p> <pre><span style="color:#2b91af;">Address</span>&nbsp;address&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;a&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Builder</span>.Address &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;a.WithCity(<span style="color:#a31515;">&quot;Paris&quot;</span>);</pre> </p> <p> <code>Builder.Address</code> is a <code>Builder&lt;Address&gt;</code> object that contains a 'good' default <code>Address</code> value. Since <code>Builder&lt;T&gt;</code> has a compatible <code>Select</code> method, you can 'query it'. In this example, you use the <code>WithCity</code> method to explicitly pin the <code>Address</code> object's <code>City</code> property, while all the other <code>Address</code> values remain the default values. </p> <p> There's an extra bit (pun intended) of compiler magic at work. Did you wonder how a <code>Builder&lt;Address&gt;</code> automatically turns into an <code>Address</code> value? After all, <code>address</code> is of the type <code>Address</code>, not <code>Builder&lt;Address&gt;</code>. </p> <p> I specifically added an implicit conversion so that I didn't have to surround the query expression with brackets in order to call the <code>Build</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">implicit</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;<span style="color:#2b91af;">T</span>(<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;b) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;b.item; }</pre> </p> <p> This conversion is defined on <code>Builder&lt;T&gt;</code>. It's the reason I explicitly use the type name when I declare the <code>address</code> variable above, instead of using the <code>var</code> keyword. Declaring the type forces the implicit conversion. </p> <p> You can also use query syntax to map one constructed Builder type into another (and ultimately to the value it contains): </p> <p> <pre><span style="color:#2b91af;">Address</span>&nbsp;address&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;pc&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Builder</span>.PostCode &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;pc);</pre> </p> <p> This expression starts with a <code>Builder&lt;PostCode&gt;</code> object, transforms it into a <code>Builder&lt;Address&gt;</code> object, and then finally uses the implicit conversion to turn the <code>Builder&lt;Address&gt;</code> into an <code>Address</code> object. </p> <p> Even a more complex 'query' looks almost palatable: </p> <p> <pre><span style="color:#2b91af;">Invoice</span>&nbsp;invoice&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Builder</span>.Invoice &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;i.WithRecipient( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;r&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#2b91af;">Builder</span>.Recipient &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;r.WithAddress(<span style="color:#2b91af;">Builder</span>.Address.WithNoPostCode()));</pre> </p> <p> Again, the implicit type conversion makes the syntax much cleaner. </p> <h3 id="794ba14a3049498a8ed366184ceb0f02"> Functor <a href="#794ba14a3049498a8ed366184ceb0f02" title="permalink">#</a> </h3> <p> Isn't it amazing that the C# designers were able to come up with such a generally useful language feature? It certainly is a nice piece of work, but it's based on a an existing body of knowledge. </p> <p> A type like <code>Builder&lt;T&gt;</code> with a suitable <code>Select</code> method is a <a href="/2018/03/22/functors">functor</a>. This is a term from category theory, but I'll try to avoid turning this article into a category theory lecture. Likewise, I'm not going to talk specifically about monads here, although it's a closely related topic. A functor is a mapping between categories; it maps an object from one category into an object of another category. </p> <p> Although I've never seen Microsoft explicitly acknowledge the connection to functors and monads, it's clear that it's there. One of the principal designers of LINQ was <a href="https://en.wikipedia.org/wiki/Erik_Meijer_(computer_scientist)">Erik Meijer</a>, who definitely knows his way around category theory and functional programming. A functor is a simple, but widely applicable abstraction. </p> <p> In order to be a functor, a type must have an associated mapping. In C#'s query syntax, this is a method named <code>Select</code>, but more often it's called <code>map</code>. </p> <h3 id="0ca4a15d3cb0421f99ec8e41260a45c0"> Haskell Builder <a href="#0ca4a15d3cb0421f99ec8e41260a45c0" title="permalink">#</a> </h3> <p> In Haskell, the mapping is called <code>fmap</code>, and you can define the Builder functor like this: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">Builder</span>&nbsp;a&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Builder</span>&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">Builder</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;fmap&nbsp;f&nbsp;(<span style="color:#dd0000;">Builder</span>&nbsp;a)&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Builder</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;f&nbsp;a</pre> </p> <p> Notice how terse the definition is, compared to the C# version. Despite the difference in size, they accomplish the same goal. The first line of code defines the <code>Builder</code> type, complete with structural equality (<code>Eq</code>) and the ability to convert a <code>Builder</code> value to a string (<code>Show</code>). </p> <p> This <code>Builder</code> type is explicitly defined as a <code>Functor</code> in the second expression, where the <code>fmap</code> function is implemented. The code is similar to the <code>Select</code> method in the above C# example: <code>f</code> is a function that takes the generic type <code>a</code> (corresponding to <code>T</code> in the C# example) as input, and returns a value of the generic type <code>b</code> (corresponding to <code>T1</code> in the C# example). The mapping pulls the underlying value out of the input Builder, calls <code>f</code> with that value, and puts the return value into a new Builder. </p> <p> In Haskell, a functor is part of the language itself, so <code>Builder</code> is explicitly declared to be a <code>Functor</code> instance. </p> <p> If you define some default <code>Builder</code> values, corresponding to the above <code>Builder.Address</code>, you can use them to build addresses in the same way: </p> <p> <pre><span style="color:#dd0000;">Builder</span>&nbsp;address&nbsp;<span style="color:#666666;">=</span>&nbsp;fmap&nbsp;(<span style="color:#666666;">\</span>a&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;a&nbsp;{&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;})&nbsp;addressBuilder </pre> </p> <p> Here, <code>addressBuilder</code> is a <code>Builder Address</code> value, corresponding to the C# <code>Builder.Address</code> value. <code>\a -&gt; a { city = "Paris" }</code> is a lambda expression that takes an <code>Address</code> value as input, and return a similar value as output, only with <code>city</code> explicitly bound to <code>"Paris"</code>. </p> <h3 id="687b4edadb264016b1979626b58c22f9"> F# example <a href="#687b4edadb264016b1979626b58c22f9" title="permalink">#</a> </h3> <p> Unlike Haskell, F# doesn't treat functors as an explicit construct, but you can still define a Builder functor: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Builder</span>&lt;&#39;a&gt;&nbsp;=&nbsp;<span style="color:navy;">Builder</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a <span style="color:blue;">module</span>&nbsp;<span style="color:teal;">Builder</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Builder&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Builder&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>&nbsp;(<span style="color:navy;">Builder</span>&nbsp;x)&nbsp;=&nbsp;<span style="color:navy;">Builder</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;x)</pre> </p> <p> You can see how similar this is to the Haskell example. In F#, it's common to define a module with the same name as a generic type. This example defines a generic <code>Builder&lt;'a&gt;</code> type and a supporting <code>Builder</code> module. Normally, a module would contain other functions, in addition to <code>map</code>. </p> <p> Just like in C# and Haskell, you can build an address in Paris with a predefined Builder value as a start: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;(<span style="color:navy;">Builder</span>&nbsp;address)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;addressBuilder&nbsp;|&gt;&nbsp;<span style="color:teal;">Builder</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;a&nbsp;<span style="color:blue;">with</span>&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;})</pre> </p> <p> Again, <code>addressBuilder</code> is a <code>Builder&lt;Address&gt;</code> that contains a 'default' <code>Address</code> (test) value. You use <code>Builder.map</code> with a lambda expression to map the default value into a new <code>Address</code> value where <code>City</code> is bound to <code>"Paris"</code>. </p> <h3 id="813b760b821649298719e19aaca335d8"> Functor laws <a href="#813b760b821649298719e19aaca335d8" title="permalink">#</a> </h3> <p> In order to be a proper functor, an object must obey two simple laws. It's not enough that a mapping function exists, it must also obey the laws. While that sounds uncomfortably like mathematics, the laws are simple and intuitive. </p> <p> The first law is that when the mapping returns the input, the functor returned is also the input functor. There's only one (generic) function that returns its input unmodified. It's called the <em>identity</em> function (often abbreviated <em>id</em>). </p> <p> Here's an example test case that illustrates the first functor law for the C# <code>Builder&lt;T&gt;</code>: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BuilderObeysFirstFunctorLaw() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;id&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:blue;">int</span>&gt;(42); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Select(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(sut,&nbsp;actual); }</pre> </p> <p> The .NET Base Class Library doesn't come with a built-in identity function, so the test case first defines it as <code>id</code>. Normally, the identity function would be defined as a function that takes a value of the generic type <code>T</code> as input, and returns the same value (still of type <code>T</code>) as output. This test is only an example for the type <code>int</code>, so it also defines the identity function as constrained to <code>int</code>. </p> <p> The test creates a new <code>Builder&lt;int&gt;</code> with the value <code>42</code>, and calls <code>Select</code> with <code>id</code>. Since the first functor law says that mapping with the identity function must return the input functor, the expected value is the <code>sut</code> variable. </p> <p> This test is only an <em>example</em> of the first functor law. It doesn't prove that <code>Builder&lt;T&gt;</code> obeys the law for all generic types (<code>T</code>) and for all values. It only proves that it holds for the integer 42. You get the idea, though, I'm sure. </p> <p> The second functor law says that if you chain two functions to make a third function, and map your functor using that third function, the result should be equal to the result you get if you chain two mappings made out of those two functions. Here's an example: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;BuilderObeysSecondFunctorLaw() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;g&nbsp;=&nbsp;i&nbsp;=&gt;&nbsp;i.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;f&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(s.Reverse().ToArray()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:blue;">int</span>&gt;(1337); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Select(i&nbsp;=&gt;&nbsp;f(g(i))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;expected&nbsp;=&nbsp;sut.Select(g).Select(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); }</pre> </p> <p> This test case (which is, again, only an example) first defines two functions, <code>f</code> and <code>g</code>. It then creates a new <code>Builder&lt;int&gt;</code> and calls <code>Select</code> with the combined function <code>f(g)</code>. This returns the <code>actual</code> result, which is a <code>Builder&lt;string&gt;</code>. </p> <p> This result should be equal to first calling <code>Select</code> with <code>g</code> (which returns a <code>Builder&lt;string&gt;</code>), and then calling <code>Select</code> with <code>f</code> (which returns another <code>Builder&lt;string&gt;</code>). These two Builder objects should be equal to each other, which they are. </p> <p> Both these tests compare an expected Builder to an actual Builder, which is the reason that <code>Builder&lt;T&gt;</code> overrides <code>Equals</code> in order to have structural equality. In Haskell, the above Builder type has structural equality because it uses the default instance of <code>Eq</code>, and in F#, <code>Builder&lt;'a&gt;</code> has structural equality because that's the default equality for the immutable F# data types. </p> <p> We can't easily talk about the functor laws without being able to talk about functor values being (or not being) equal to each other, so structural equality is an important element in the discussion. </p> <h3 id="aa9273866eaa4b39ac534119dce3cb12"> Summary <a href="#aa9273866eaa4b39ac534119dce3cb12" title="permalink">#</a> </h3> <p> You can define a Test Data Builder as a functor by defining a generic Builder type with a <code>Select</code> method. In order to be a proper functor, it must also obey the functor laws, but these laws are quite natural; you almost have to go out of your way in order to violate them. </p> <p> A functor is a well-known abstraction. Instead of trying to come up with a half-baked, ad-hoc abstraction, modelling an API based on already known and understood abstractions such as functors will make the API easier to learn. Everyone who knows what a functor is, will automatically have a good understanding of the API. Even if you didn't know about functors until now, you only have to learn about them once. </p> <p> This can often be beneficial, but for Test Data Builders, it turns out to be a red herring. The Builder functor is nothing but the Identity functor in disguise. </p> <p> <strong>Next:</strong> <a href="/2017/09/04/builder-as-identity">Builder as Identity</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="20df28ded8014f0ab9d9aaf7a7fb21d9"> <div class="comment-author"><a href="https://github.com/andresmoschini">Andrés Moschini</a> <a href="#20df28ded8014f0ab9d9aaf7a7fb21d9">#</a></div> <div class="comment-content"> <p>Maybe I am missing something, so could you explain with few words what is the advantage of having this _generic builder_?</p> <p>I mean, inmutable entities and _with methods_ seems to be enough to easily create test data without builders, for example:</p> <p><pre><code> var invoice = DefaultTestObjects.Invoice .WithRecipient(DefaultTestObjects.Recipient .WithAddress(DefaultTestObjects.Address .WithNoPostCode() .WithCity("Paris")) .WithDate(new DateTimeOffset(2017, 8, 29))); </code></pre></p> </div> <div class="comment-date">2017-08-29 12:50 UTC</div> </div> <div class="comment" id="8126755f03374b128d38317e410e0dbc"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8126755f03374b128d38317e410e0dbc">#</a></div> <div class="comment-content"> <p> Andrés, thank you for writing. I hope that the next two articles in <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">this article series</a> will answer your question. It seems, however, that you've already predicted where this is headed. A fine display of critical thinking! </p> </div> <div class="comment-date">2017-08-29 17:48 UTC</div> </div> <div class="comment" id="c9e9796e7cc54da688336c0c97e29390"> <div class="comment-author"><a href="https://www.linkedin.com/in/romainvasseur/">Romain Vasseur</a> <a href="#c9e9796e7cc54da688336c0c97e29390">#</a></div> <div class="comment-content"> <p>Just for the DSL and implicit conversion laius it's worth reading.<br> _Implicit/explicit_ is a must-have to lighten _value object_ usage and contributes to deliver proper API. <br>Thank you Mark. </p> </div> <div class="comment-date">2017-08-31 12:44 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Generalised Test Data Builder https://blog.ploeh.dk/2017/08/21/generalised-test-data-builder 2017-08-21T06:09:00+00:00 Mark Seemann <div id="post"> <p> <em>This article presents a generalised Test Data Builder.</em> </p> <p> This is the second in <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">a series of articles about the relationship between the Test Data Builder design pattern, and the identity functor</a>. The <a href="/2017/08/15/test-data-builders-in-c">previous article was a review</a> of the <a href="http://www.natpryce.com/articles/000714.html">Test Data Builder</a> pattern. </p> <h3 id="28183b56ef7d444aac37420035e7aba2"> Boilerplate <a href="#28183b56ef7d444aac37420035e7aba2" title="permalink">#</a> </h3> <p> While the Test Data Builder is an incredibly versatile and useful design pattern, it has a problem. In languages like C# and Java, it's difficult to generalise. This leads to an excess of boilerplate code. </p> <p> Expanding on <a href="http://www.natpryce.com">Nat Pryce</a>'s original example, an <code>InvoiceBuilder</code> is composed of other builders: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">InvoiceBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Recipient</span>&nbsp;recipient; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;&nbsp;lines; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;InvoiceBuilder() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.recipient&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RecipientBuilder</span>().Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.lines&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvoiceLineBuilder</span>().Build()&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">InvoiceBuilder</span>&nbsp;WithRecipient(<span style="color:#2b91af;">Recipient</span>&nbsp;newRecipient) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.recipient&nbsp;=&nbsp;newRecipient; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">InvoiceBuilder</span>&nbsp;WithInvoiceLines( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;&nbsp;newLines) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.lines&nbsp;=&nbsp;newLines; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Invoice</span>&nbsp;Build() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Invoice</span>(recipient,&nbsp;lines); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In order to create a <code>Recipient</code>, a <code>RecipientBuilder</code> is used. Likewise, in order to create a single <code>InvoiceLine</code>, an <code>InvoiceLineBuilder</code> is used. This pattern repeats in the <code>RecipientBuilder</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">RecipientBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">string</span>&nbsp;name; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;address; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;RecipientBuilder() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.address&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>().Build(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">RecipientBuilder</span>&nbsp;WithName(<span style="color:blue;">string</span>&nbsp;newName) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.name&nbsp;=&nbsp;newName; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">RecipientBuilder</span>&nbsp;WithAddress(<span style="color:#2b91af;">Address</span>&nbsp;newAddress) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.address&nbsp;=&nbsp;newAddress; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Recipient</span>&nbsp;Build() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Recipient</span>(<span style="color:blue;">this</span>.name,&nbsp;<span style="color:blue;">this</span>.address); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In order to create an <code>Address</code> object, an <code>AddressBuilder</code> is used. </p> <h3 id="611bd2d4ffa74ad8acfe9b29e72f3e1f"> Generalisation attempts <a href="#611bd2d4ffa74ad8acfe9b29e72f3e1f" title="permalink">#</a> </h3> <p> You can describe the pattern in a completely automatable manner: <ol> <li>For each domain class, create a corresponding Builder class.</li> <li>For each class field or property in the domain class, define a corresponding field or property in the Builder.</li> <li> In the Builder's constructor, initialise each field or property with a 'good' default value. <ul> <li>If the field is a primitive value, such as a string or integer, hard-code an appropriate value.</li> <li>If the field is a complex domain type, use that type's corresponding Builder to create the default value.</li> </ul> </li> <li>For each class field or property, add a <code>With[...]</code> method that changes the field and returns the Builder itself.</li> <li>Add a <code>Build</code> method that returns a new instance of the domain class with the constituent values collected so far.</li> </ol> When you can deterministically descrbe an automatable process, you can write code to automate it. </p> <p> People have already done that. After having written individual Test Data Builders for a couple of months, I got tired of it and wrote <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. It uses Reflection to build objects at run-time, but I've also witnessed attempts to automate Test Data Builders via automated code generation. </p> <p> AutoFixture has been moderately successful, but some people find its API difficult to learn. Correspondingly, code generation comes with its own issues. </p> <p> In languages like C# or Java, it's difficult to identify a better generalisation. </p> <h3 id="9e8cd5eae8ef4f12afdce65f53fb72b4"> Generic Builder <a href="#9e8cd5eae8ef4f12afdce65f53fb72b4" title="permalink">#</a> </h3> <p> Instead of trying to automate the Test Data Builder pattern, you can pursue a different strategy. At first, it doesn't look all that promising, but if you soldier on, it'll reveal meaningful insights. </p> <p> As an alternative to replicating the Test Data Builder pattern exactly, you can define a single generically typed Builder class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Builder(<span style="color:#2b91af;">T</span>&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T1</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;newItem&nbsp;=&nbsp;f(<span style="color:blue;">this</span>.item); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T1</span>&gt;(newItem); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Build() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.item; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;other&nbsp;=&nbsp;obj&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">T</span>&gt;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">base</span>.Equals(obj); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.item,&nbsp;other.item); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.item.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Builder&lt;T&gt;</code> class reduces the Test Data Builder design patterns to the essentials: <ul> <li>A constructor that initialises the Builder with default data.</li> <li>A single <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a> <code>Select</code> method, which returns a new Builder object.</li> <li>A <code>Build</code> method, which returns the built object.</li> </ul> Perhaps you wonder about the name of the <code>Select</code> method, but there's a good reason for that; you'll learn about it later. </p> <p> This example of a generic Builder class overrides <code>Equals</code> (and, therefore, also <code>GetHashCode</code>). It doesn't have to do that, but there's a good reason to do this that we'll also <a href="/2017/08/28/the-builder-functor">come back to later</a>. </p> <p> It doesn't seem particularly useful, and a first attempt at using it seems to confirm such scepticism: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:#2b91af;">Build</span>.Address().Select(a&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;a.City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;a; }).Build();</pre> </p> <p> This example first uses <code>Build.Address()</code> to create an initial Builder object with appropriate defaults. This static method is defined on the static <code>Build</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Address</span>&gt;&nbsp;Address() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Address</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;PostCode().Build())); }</pre> </p> <p> Contrary to <code>Builder&lt;T&gt;</code>, which is a reusable, general-purpose class, the static <code>Build</code> class is an example of a collection of <a href="http://xunitpatterns.com/Test%20Utility%20Method.html">Test Utility Methods</a> specific to the domain model you're testing. Notice how the <code>Build.Address()</code> method uses <code>Build.PostCode().Build()</code> to create a default value for the initial <code>Address</code> object's post code. </p> <p> The above example passes a C# code block to the <code>Select</code> method. It takes the <code>a</code> (<code>Address</code>) object as input, specifically mutates its <code>City</code> property, and returns it. This syntax is crude, but works. It may look acceptable when pinning a single <code>City</code> property, but it quickly becomes awkward: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;invoice&nbsp;=&nbsp;<span style="color:#2b91af;">Build</span>.Invoice().Select(i&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i.Recipient&nbsp;=&nbsp;<span style="color:#2b91af;">Build</span>.Recipient().Select(r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.Address&nbsp;=&nbsp;<span style="color:#2b91af;">Build</span>.Address().WithNoPostCode().Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;r; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}).Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;}).Build();</pre> </p> <p> Not only is it difficult to get right when writing such nested statements, it's also hard to read. You can, however, correct that problem, as you'll see in a little while. </p> <p> Before we commence on making the code prettier, you may have noticed that the <code>Select</code> method returns a Builder with a different generic type argument than it contains. The <code>Select</code> method on a <code>Builder&lt;T&gt;</code> object has the signature <code>public Builder&lt;T1&gt; Select&lt;T1&gt;(Func&lt;T, T1&gt; f)</code>. Until now, however, all the examples you've seen return the input object. In those examples, <code>T</code> is the same as <code>T1</code>. For completeness' sake, here's an example of a proper change of type: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:#2b91af;">Build</span>.PostCode() &nbsp;&nbsp;&nbsp;&nbsp;.Select(pc&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;Rue&nbsp;Morgue&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>,&nbsp;pc)) &nbsp;&nbsp;&nbsp;&nbsp;.Build();</pre> </p> <p> This example uses a <code>Builder&lt;PostCode&gt;</code> to create a new <code>Address</code> object. Plugging in the types, <code>T</code> becomes <code>PostCode</code>, and <code>T1</code> becomes <code>Address</code>. </p> <p> Perhaps you noticed that this example looks a little better than the previous examples. Instead of having to supply a C# code block, with <code>return</code> statement and all, this call to <code>Select</code> passes a proper (lambda) expression. </p> <h3 id="f03e044e3a064e5f9f58ffb38a0e6d82"> Expressions from extensions <a href="#f03e044e3a064e5f9f58ffb38a0e6d82" title="permalink">#</a> </h3> <p> It'd be nice if you could use expressions, instead of full code blocks, with the <code>Select</code> method. As a first step, you could write some test-specific extension methods for your domain model, like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;WithCity(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;address,&nbsp;<span style="color:blue;">string</span>&nbsp;newCity) { &nbsp;&nbsp;&nbsp;&nbsp;address.City&nbsp;=&nbsp;newCity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;address; }</pre> </p> <p> This is same code as one of the code blocks above, only refactored to a named extension method. It simplifies use of the generic Builder, though: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:#2b91af;">Build</span>.Address().Select(a&nbsp;=&gt;&nbsp;a.WithCity(<span style="color:#a31515;">&quot;Paris&quot;</span>)).Build(); </pre> </p> <p> That looks good in such a simple example, but unfortunately isn't much of an improvement when it comes to a more complex case: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;invoice&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Build</span>.Invoice() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(i&nbsp;=&gt;&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithRecipient(<span style="color:#2b91af;">Build</span>.Recipient() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(r&nbsp;=&gt;&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithAddress(<span style="color:#2b91af;">Build</span>.Address() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithNoPostCode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build();</pre> </p> <p> If, at this point, you're tempted to give up on the overall strategy with a single generic Builder, you'd be excused. It will, however, turn out to be beneficial to carry on. There are more obstacles, but eventually, things will start to fall into place. </p> <h3 id="540eb0f0d1eb44aab2060cf57f8e1acf"> Copy and update <a href="#540eb0f0d1eb44aab2060cf57f8e1acf" title="permalink">#</a> </h3> <p> The above <code>WithCity</code> extension method mutates the input object, which can lead to surprising behaviour. While it's a common way to implement fluent interfaces in object-oriented languages, nothing prevents you from making the code saner. Instead of mutating the input object, create a new object with the single value changed: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;WithCity(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;address,&nbsp;<span style="color:blue;">string</span>&nbsp;newCity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(address.Street,&nbsp;newCity,&nbsp;address.PostCode); }</pre> </p> <p> Some people will immediately be concerned about the performance implications of doing this, but you're not one of those people, are you? </p> <p> Granted, there's allocation and garbage collection overhead by creating new objects like this, but I'd digress if I started to discuss this here. In most cases, the impact is insignificant. </p> <h3 id="b989fe7225924655947dff96af276f21"> Fluent domain model <a href="#b989fe7225924655947dff96af276f21" title="permalink">#</a> </h3> <p> Using extension methods enables you to use a more elegant syntax with the <code>Select</code> method, but there's still some maintenance overhead. If, for now, we accept such maintenance overhead, you could ask: given that we have to define and maintain all those <code>With[...]</code> methods, why limit them to your test code? </p> <p> Would there be any harm in defining them as proper methods on your domain model? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;WithCity(<span style="color:blue;">string</span>&nbsp;newCity) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:blue;">this</span>.Street,&nbsp;newCity,&nbsp;<span style="color:blue;">this</span>.PostCode); }</pre> </p> <p> The above example shows the <code>WithCity</code> method as an instance method on the <code>Address</code> class. Here's the entire <code>Address</code> class, refactored to an immutable class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Address</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Street&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;City&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PostCode</span>&nbsp;PostCode&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Address(<span style="color:blue;">string</span>&nbsp;street,&nbsp;<span style="color:blue;">string</span>&nbsp;city,&nbsp;<span style="color:#2b91af;">PostCode</span>&nbsp;postCode) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(street&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(street)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(city&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(city)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(postCode&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(postCode)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Street&nbsp;=&nbsp;street; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.City&nbsp;=&nbsp;city; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.PostCode&nbsp;=&nbsp;postCode; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;WithStreet(<span style="color:blue;">string</span>&nbsp;newStreet) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(newStreet,&nbsp;<span style="color:blue;">this</span>.City,&nbsp;<span style="color:blue;">this</span>.PostCode); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;WithCity(<span style="color:blue;">string</span>&nbsp;newCity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:blue;">this</span>.Street,&nbsp;newCity,&nbsp;<span style="color:blue;">this</span>.PostCode); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;WithPostCode(<span style="color:#2b91af;">PostCode</span>&nbsp;newPostCode) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:blue;">this</span>.Street,&nbsp;<span style="color:blue;">this</span>.City,&nbsp;newPostCode); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;other&nbsp;=&nbsp;obj&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:#2b91af;">Address</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">base</span>.Equals(obj); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.Street,&nbsp;other.Street) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.City,&nbsp;other.City) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.PostCode,&nbsp;other.PostCode); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Street.GetHashCode()&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.City.GetHashCode()&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.PostCode.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Technically, you could introduce instance methods like <code>WithCity</code> even if you kept the class itself mutable, but once you start down that path, it makes sense to make the class immutable. As Eric Evans recommends in <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>, modelling your domain with (immutable) <a href="https://martinfowler.com/bliki/ValueObject.html">Value Objects</a> has many benefits. Such objects should also have structural equality, which is the reason that this version of <code>Address</code> also overrides <code>Equals</code> and <code>GetHashCode</code>. </p> <p> While it looks like more work in a language like C# or Java, there are many benefits to be derived from modelling your domain with Value Objects. As an interim result, then, observe that working with unit testing (in this case a general-purpose Test Data Builder) has prompted a better design of the System Under Test. </p> <p> You may still think that this seems unnecessarily verbose, and I'd agree. This is one of the many reasons I prefer languages like F# and Haskell over C# or Java. The former have such a <em>copy and update</em> feature built-in. Here's an F# example of updating an <code>Address</code> record with a specific city: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;address&nbsp;=&nbsp;{&nbsp;a&nbsp;<span style="color:blue;">with</span>&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;} </pre> </p> <p> This capability is built into the language. You don't have to add or maintain any code in order to be able to write code like that. Notice, even, how <code>with</code> is a keyword. I'm not sure about the etymology of the word <em>with</em> used in this context, but I find the similarity compelling. </p> <p> In Haskell, it looks similar: </p> <p> <pre>address&nbsp;<span style="color:#666666;">=</span>&nbsp;a&nbsp;{&nbsp;city&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;} </pre> </p> <p> In other words, domain models created from immutable Value Objects are laborious in some languages, but that only suggests a deficiency in such a language. </p> <h3 id="bf60f37e23d945c6a6494c49352f2bf6"> Default Builders as values <a href="#bf60f37e23d945c6a6494c49352f2bf6" title="permalink">#</a> </h3> <p> Now that the domain model is immutable, you can define default builders as values. Previously, to start building e.g. an <code>Address</code> value, you had to call the <code>Build.Address()</code> method. When the domain model was mutable, containing a single default value inside of a Builder would enable tests to mutate that default value. Now that domain classes are immutable, this is no longer a concern, and you can instead define test-specific default builders as values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Builder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Address</span>&gt;&nbsp;Address; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Invoice</span>&gt;&nbsp;Invoice; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;&nbsp;InvoiceLine; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">PostCode</span>&gt;&nbsp;PostCode; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">PoundsShillingsPence</span>&gt;&nbsp;PoundsShillingsPence; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Recipient</span>&gt;&nbsp;Recipient; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;Builder() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PoundsShillingsPence&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">PoundsShillingsPence</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DomainModel.<span style="color:#2b91af;">PoundsShillingsPence</span>.Zero); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PostCode&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">PostCode</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCode</span>()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Address&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Address</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;PostCode.Build())); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Recipient&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Recipient</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Recipient</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;Address.Build())); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Invoice&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Invoice</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Invoice</span>(Recipient.Build(),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;())); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InvoiceLine&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvoiceLine</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;PoundsShillingsPence.Build())); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Address</span>&gt;&nbsp;WithNoPostCode(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Builder</span>&lt;<span style="color:#2b91af;">Address</span>&gt;&nbsp;b) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;b.Select(a&nbsp;=&gt;&nbsp;a.WithPostCode(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCode</span>())); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This enables you to write expressions like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:#2b91af;">Builder</span>.Address.Select(a&nbsp;=&gt;&nbsp;a.WithCity(<span style="color:#a31515;">&quot;Paris&quot;</span>)).Build(); </pre> </p> <p> To be clear: such a static <code>Builder</code> class is a Test Utility API specific to your unit tests. It would often be defined in a completely different file than the <code>Builder&lt;T&gt;</code> class, perhaps even in separate libraries. </p> <h3 id="e85be1dc5b074f85932f3c4c47b5b4d7"> Summary <a href="#e85be1dc5b074f85932f3c4c47b5b4d7" title="permalink">#</a> </h3> <p> Instead of trying to automate Test Data Builders to the letter of the original design pattern description, you can define a single, reusable, generic <code>Builder&lt;T&gt;</code> class. It enables you to achieve some of the expressivity of Test Data Builders. </p> <p> If you still don't find this strategy's prospects fertile, I understand. We're not done, though. In the next article, you'll see why <code>Select</code> is an appropriate name for the Builder's most important method, and how it relates to good abstractions. </p> <p> <strong>Next:</strong> <a href="/2017/08/28/the-builder-functor">The Builder functor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="d867b3b2864741a6bcbdfdbd9142b74e"> <div class="comment-author"><a href="https://mikhail.io/about/">Mikhail Shilkov</a> <a href="#d867b3b2864741a6bcbdfdbd9142b74e">#</a></div> <div class="comment-content"> <p>When I found myself writing too many With() methods, I created an extension to <a href="https://github.com/Fody/Fody">Fody</a> code weaving tool: <a href="https://github.com/mikhailshilkov/With.Fody">Fody.With</a>.</p> <p>Basically I declare the With() methods without body implementation, and then Fody does the implementation for me. It can also convert a generic version to N overloads with an implementation per each public property.</p> <p>The link about has some usage examples, that hopefully make the idea clear.</p> </div> <div class="comment-date">2017-08-21 12:32 UTC</div> </div> <div class="comment" id="a5e3e4463fa24504b66a39769c06fb23"> <div class="comment-author"><a href="https://github.com/harshdeep21">Harshdeep Mehta</a> <a href="#a5e3e4463fa24504b66a39769c06fb23">#</a></div> <div class="comment-content"> <p>C# does have <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer"> Object Initializer</a> to build "address" with specified "city", similar to F# and Haskell.</p> </div> <div class="comment-date">2017-08-22 12:40 UTC</div> </div> <div class="comment" id="666a5de4c54541fc8e353a7ad526305a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#666a5de4c54541fc8e353a7ad526305a">#</a></div> <div class="comment-content"> <p> Harshdeep, thank you for writing. C# object initialisers aren't the same as <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/copy-and-update-record-expressions">F# Copy and Update Record Expressions</a>. Unless I misunderstand what you mean, when you write </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;{&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;}; </pre> </p> <p> <code>address</code> will have <code>"Paris"</code> as <code>City</code>, but all other properties, such as <code>Street</code> and <code>PostCode</code> will be null. That's not what I want. That's the problem the Test Data Builder pattern attempts to address. Test values should be populated with 'good' values, not null. </p> <p> I admit that I'm not keeping up with the latest developments in C#, but if I try to use the C# object initializer syntax with an existing value, like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;defaultAddress&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;{&nbsp;Street&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;PostCode&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;DomainModel.<span style="color:#2b91af;">PostCode</span>(),&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>&nbsp;}; <span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;defaultAddress&nbsp;{&nbsp;City&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Paris&quot;</span>&nbsp;};</pre> </p> <p> it doesn't compile. </p> <p> I'm still on Visual Studio 2015, though, so that may be it... </p> </div> <div class="comment-date">2017-08-22 13:27 UTC</div> </div> <div class="comment" id="aba5f0ed69e44f17acd1b31a2ed278d7"> <div class="comment-author"><a href="https://github.com/harshdeep21">Harshdeep Mehta</a> <a href="#aba5f0ed69e44f17acd1b31a2ed278d7">#</a></div> <div class="comment-content"> <p>Aah. Now I get it. Thanks for explaining. I am from C# world and certainly not into F# yet so I missunderstood "Copy &amp; Update Expression" with "Object Initializer".</p> </div> <div class="comment-date">2017-08-23 5:22 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Test Data Builders in C# https://blog.ploeh.dk/2017/08/15/test-data-builders-in-c 2017-08-15T06:20:00+00:00 Mark Seemann <div id="post"> <p> <em>A brief recap of the Test Data Builder design pattern with examples in C#.</em> </p> <p> This is the first in <a href="/2017/08/14/from-test-data-builders-to-the-identity-functor">a series of articles about the relationship between the Test Data Builder design pattern, and the identity functor</a>. </p> <p> In 2007 <a href="http://www.natpryce.com">Nat Pryce</a> described the <a href="http://www.natpryce.com/articles/000714.html">Test Data Builder</a> design pattern. The original article is easy to read, but in case you don't want to read it, here's a quick summary, with some of Nat Pryce's examples translated to C#. </p> <p> The purpose of a Test Data Builder is to make it easy to create input data (or objects) for unit tests. Imagine, for example, that for a particular test case, you need an address in Paris; no other values matter. With a Test Data Builder, you can write an expression that gives you such a value: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>().WithCity(<span style="color:#a31515;">&quot;Paris&quot;</span>).Build(); </pre> </p> <p> The <code>address</code> object explicity has a <code>City</code> value of <code>"Paris"</code>. Any other values are default values defined by <code>AddressBuilder</code>. The values are there, but when they're unimportant to a particular test case, you don't have to specify them. To <a href="http://amzn.to/19W4JHk">paraphrase Robert C. Martin</a>, this eliminates the irrelevant, and amplifies the essentials of the test. </p> <h3 id="aae3468437314471b7eb750dd6f50960"> Address Builder <a href="#aae3468437314471b7eb750dd6f50960" title="permalink">#</a> </h3> <p> An <code>AddressBuilder</code> could look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">string</span>&nbsp;street; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">string</span>&nbsp;city; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">PostCode</span>&nbsp;postCode; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;AddressBuilder() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.street&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.city&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.postCode&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCodeBuilder</span>().Build(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>&nbsp;WithStreet(<span style="color:blue;">string</span>&nbsp;newStreet) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.street&nbsp;=&nbsp;newStreet; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>&nbsp;WithCity(<span style="color:blue;">string</span>&nbsp;newCity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.city&nbsp;=&nbsp;newCity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>&nbsp;WithPostCode(<span style="color:#2b91af;">PostCode</span>&nbsp;newPostCode) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.postCode&nbsp;=&nbsp;newPostCode; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>&nbsp;WithNoPostcode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.postCode&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCode</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Address</span>&nbsp;Build() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:blue;">this</span>.street,&nbsp;<span style="color:blue;">this</span>.city,&nbsp;<span style="color:blue;">this</span>.postCode); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Address</code> class is simpler than the Builder: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Address</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Street&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;City&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">PostCode</span>&nbsp;PostCode&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Address(<span style="color:blue;">string</span>&nbsp;street,&nbsp;<span style="color:blue;">string</span>&nbsp;city,&nbsp;<span style="color:#2b91af;">PostCode</span>&nbsp;postCode) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Street&nbsp;=&nbsp;street; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.City&nbsp;=&nbsp;city; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.PostCode&nbsp;=&nbsp;postCode; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Clearly, this class could contain some behaviour, but in order to keep the example as simple as possible, it's only a simple Data Transfer Object. </p> <h3 id="de2e6fb74f6f4319a0fef86dcd9b839e"> Composition <a href="#de2e6fb74f6f4319a0fef86dcd9b839e" title="permalink">#</a> </h3> <p> Given that <code>AddressBuilder</code> is more complicated than <code>Address</code> itself, the benefit of the pattern may seem obscure, but one of the benefits is that Test Data Builders easily compose: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;invoice&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvoiceBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;.WithRecipient(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RecipientBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithAddress(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AddressBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithNoPostcode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build()) &nbsp;&nbsp;&nbsp;&nbsp;.Build();</pre> </p> <p> Perhaps that looks verbose, but in general, the alternative is worse. If you didn't have a <a href="http://xunitpatterns.com/Test%20Utility%20Method.html">Test Utility Method</a>, you'd have to fill in <em>all</em> the required data for the object: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;invoice&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Invoice</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Recipient</span>(<span style="color:#a31515;">&quot;Sherlock&nbsp;Holmes&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Address</span>(<span style="color:#a31515;">&quot;221b&nbsp;Baker&nbsp;Street&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;London&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostCode</span>())), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">InvoiceLine</span>&gt;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvoiceLine</span>(<span style="color:#a31515;">&quot;Deerstalker&nbsp;Hat&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PoundsShillingsPence</span>(0,&nbsp;3,&nbsp;10)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">InvoiceLine</span>(<span style="color:#a31515;">&quot;Tweed&nbsp;Cape&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PoundsShillingsPence</span>(0,&nbsp;4,&nbsp;12))});</pre> </p> <p> Here, the important detail drowns in data. The post code is empty because the <code>PostCode</code> constructor is called without arguments. This hardly jumps out when you see it. Such code neither eliminates the irrelevant, nor amplifies the essential. </p> <h3 id="79fc8bb1d06b4d9fb90bd48b678ead93"> Summary <a href="#79fc8bb1d06b4d9fb90bd48b678ead93" title="permalink">#</a> </h3> <p> Test Data Builders are useful because they are good abstractions. They enable you to write <a href="/2013/04/02/why-trust-tests">unit tests that you can trust</a>. </p> <p> The disadvantage, as you shall see, is that in languages like C# and Java, much boilerplate code is required. </p> <p> <strong>Next:</strong> <a href="/2017/08/21/generalised-test-data-builder">Generalised Test Data Builder</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f6799440202b4936b76c69e01e56d963"> <div class="comment-author"><a href="http://blog.developers.win">Mike-EEE</a> <a href="#f6799440202b4936b76c69e01e56d963">#</a></div> <div class="comment-content">You got me to finally figure out how to post comments. :) Hope everything looks alright.<br><br> So first off, great article as always. You totally hit a subject which has been driving me nuts, personally and lately. I have been developing my first FluentAPI and have been running up against both aspects of immutability and query/command separation that you have done an excellent job of presenting here on your blog. It does seem that FluentAPI design and the builder pattern you present above deviate from these principles, so it would be great to hear a little more context and valuable insight from you on how you reconcile this. Is this perhaps a C# issue that is easily remedied in F#? Thank you in advance for any assistance and for providing such a valuable technical resource here. It's been my favorite for many years now. </div> <div class="comment-date">2017-08-15 06:52 UTC</div> </div> <div class="comment" id="1e99b816d35e4c03a827bd8255121a7d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1e99b816d35e4c03a827bd8255121a7d">#</a></div> <div class="comment-content"> <p> Mike, thank you for writing. The <a href="https://martinfowler.com/bliki/FluentInterface.html">fluent interface</a> that I show in this article is the most common form you see in C#. While it's not my preferred variation, I use it in this article because it's a direct translation of the style used in Nat Pryce's Java code. </p> <p> Ordinarily, I prefer an immutable variant, but in C# this leads to even more boilerplate code, and I didn't want to sidetrack the topic by making this recap article more complicated than absolutely necessary. </p> <p> You may be pleased to learn that future articles in this very article series will show alternatives in both C#, F#, and Haskell. </p> </div> <div class="comment-date">2017-08-15 07:30 UTC</div> </div> <div class="comment" id="ccc8783967b84e898ace816222a93a87"> <div class="comment-author"><a href="https://gist.github.com/jand187">JanD</a> <a href="#ccc8783967b84e898ace816222a93a87">#</a></div> <div class="comment-content"> <p>Hi Mark, A while ago I made a generic builder for this exact purpose. I also made som helper extension methods, that could act as sort of an Object Mother. I quite like how it work and I have used it quite a few times. So, reading this post, I thought I'd put in a link to it, as it might be usefull to other readers.</p> <p><a href="https://gist.github.com/jand187/cabd16971097b5beb4c3">Generic Builder with Object Mother Gist</a></p> <p>It's all in one big gist and probably not very weel structured, but if you look at the class GenericBuilder it should be quite easily understood. The examples of extensionmethods can be seen towards the end of the file.</p> </div> <div class="comment-date">2017-08-15 07:42 UTC</div> </div> <div class="comment" id="56dff1c822274ed69065c8a84579a2a9"> <div class="comment-author"><a href="https://github.com/mrmorcs">James Morcom</a> <a href="#56dff1c822274ed69065c8a84579a2a9">#</a></div> <div class="comment-content"> <p>I've used test data builders in C# just like this in the past, and couldn't decide whether I liked them or not, due to all the boilerplate.</p> <p>I'm looking forward to the next few posts, thanks for doing this.</p> </div> <div class="comment-date">2017-08-18 11:43 UTC</div> </div> <div class="comment" id="3469b9d363f145e39211f02f569ff5b2"> <div class="comment-author"><a href="https://www.linkedin.com/in/rpajak/">Robert Pajak</a> <a href="#3469b9d363f145e39211f02f569ff5b2">#</a></div> <div class="comment-content"> <p>Hi, Mark</p> <p>In C# I starated to prefer to use a "parameterized object mother". Please take a look and tell me what out think about it: <a href="https://gist.github.com/Pellared/a31b6955af5c61a56a277a50606c3410">Address Object Mother Gist</a>.</p> <p>From my experience it is less and simplier code. It is also a bid easier to debug. Personally, the Object Mother is the first pattern when refactoring test data creationg and I use Fluent Test Data Builder only in more complex scenarios.</p> <p>@JanD: Unfortunately, your solution would not work for immutable data structures (which I prefer).</p> </div> <div class="comment-date">2017-08-19 19:25 UTC</div> </div> <div class="comment" id="fce82904cf6b4e07b8275559b7f8768b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fce82904cf6b4e07b8275559b7f8768b">#</a></div> <div class="comment-content"> <p> Robert, thank you for writing. I haven't seen that particular C# variation before, but it looks useful. I hope that as this article series progresses, it should become increasingly clear to the reader that the Test Data Builder pattern addresses various language deficiencies. (It has, by the way, for some time been a common criticism of design patterns in general that they are nothing but patches on language deficiencies. I don't think that I agree with that 100 percent, but I certainly understand the argument.) </p> <p> Nat Pryce's original article about the Test Data Builder pattern is from 2007 with example code in Java. I don't know that much about Java, but back then, I don't think C# had optional arguments (as far as I can tell, that language feature was added in 2010). My point is that the pattern described a good way to model code given the language features that were available at the time. </p> <p> As a general rule, I'm not a fan of C#'s optional argument feature (because I'm concerned what it does to forwards and backwards compatibility of my APIs), but used in the way you suggest it does look useful. Perhaps it does, indeed, address all the concerns that the Test Data Builder pattern addresses. I haven't tried it, so I can't really evaluate it (yet), but it looks like it'd be worth trying out. </p> <p> My overall goal with this article series is, however, slightly different. In fact, I'm not trying to sell the Test Data Builder pattern to anyone. Rather, the point is that with better API design, and with better languages, it'd be largely redundant. </p> </div> <div class="comment-date">2017-08-21 06:33 UTC</div> </div> <div class="comment" id="a8f1d2c0b5704ff4a79ae941d1fb567b"> <div class="comment-author"><a href="https://www.linkedin.com/in/romainvasseur/">Romain Vasseur</a> <a href="#a8f1d2c0b5704ff4a79ae941d1fb567b">#</a></div> <div class="comment-content"> <p>Hi, Mark<br>Thank you for this post</p> <p>I personally leverage <a href="https://github.com/ekonbenefits/impromptu-interface">Impromptu Interface</a>. It could be also verbose but as you only provide meaningful data it fits to Robert C. Martin credo. And it avoids creating a lot of one-shot boilerplate code and/or noising existing classes with UT specific stuff.</p> </div> <div class="comment-date">2017-08-21 09:12 UTC</div> </div> <div class="comment" id="29cac3d7dc9a4777bc5dc49ad59ae353"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#29cac3d7dc9a4777bc5dc49ad59ae353">#</a></div> <div class="comment-content"> <p> Romain, do you have an example of that, that you could share? </p> </div> <div class="comment-date">2017-08-21 09:49 UTC</div> </div> <div class="comment" id="78d9a0ceced94ae699cb6e39d67cd8c7"> <div class="comment-author"><a href="https://www.linkedin.com/in/romainvasseur/">Romain Vasseur</a> <a href="#78d9a0ceced94ae699cb6e39d67cd8c7">#</a></div> <div class="comment-content"> <p>Partial <code>IAddress</code> with <code>City</code> value only:</p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;City = <span style="color:#a31515;">&quot;Paris&quot;</span> &nbsp;&nbsp;}.ActLike&lt;<span style="color:#2b91af;">IAddress&gt;</span>(); </pre> <p>Partial <code>IAddress</code> with <code>City</code> value and partial <code>IPostCode</code> with <code>ISO</code> value only:</p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;City = <span style="color:#a31515;">&quot;Paris&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;PostCode = <span style="color:blue;">new</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ISO = <span style="color:#a31515;">&quot;FR&quot;</span> &nbsp;&nbsp;&nbsp;}.ActLike&lt;<span style="color:#2b91af;">IPostCode&gt;</span>() &nbsp;&nbsp;}.ActLike&lt;<span style="color:#2b91af;">IAddress&gt;</span>(); </pre> <p>Main drawback is verbosity but intent is pretty clear.<br>We could reduce nested code by splitting <code>IAddress</code> and <code>IPostCode</code> declarations but it also reduces intent: we do not care about <code>IPostCode</code>, we care about <code>IAddress</code> and <code>IPostCode</code> is only an implementation detail.</p> <p>I heavily leverage <code>region</code> to cope with C# verbosity and to highlight common pattern - AAA in this case - so all this code is usually hidden in one <code>ARRANGE</code> region.<br>When I need multiple declaration I used <code>sut</code> (System Under Test) marker to highlight main actor.</p> </div> <div class="comment-date">2017-08-21 21:09 UTC</div> </div> <div class="comment" id="49af3727e143427d994d563467ada3a7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#49af3727e143427d994d563467ada3a7">#</a></div> <div class="comment-content"> <p> Do I understand it correctly that you'd have an interface like the following, then? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IAddress</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;City&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> I'm not sure that I quite follow... </p> </div> <div class="comment-date">2017-08-22 11:43 UTC</div> </div> <div class="comment" id="f15f2c94df744f2bb0eca2995aef0d58"> <div class="comment-author"><a href="https://www.linkedin.com/in/romainvasseur/">Romain Vasseur</a> <a href="#f15f2c94df744f2bb0eca2995aef0d58">#</a></div> <div class="comment-content"> <p> Mark, I tend to avoid <code>setter</code> in my interfaces so my domain objects usually are immutable and only expose <code>getter</code>.<br> My implementation are mainly internal which prevent them to be used directly from within UT assembly (without using InternalsVisibleTo attribute).<br> I have factories - which implementation are also internal - to build my objects.<br>I then use an IoC container to access factories and create my objects. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IAddress</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;City&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;Street&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IPostCode</span>&nbsp;PostCode&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> <code>AddressBuilder</code> lives in UT world so must be in another assembly to avoid noising my model. <br>To cope with my internal visibility constraint I have at least 2 options I can live with: <ol><li>Using InternalsVisibleTo attribute for my UT assembly to be able to seamlessly use my types</li> <li>Leveraging a test container to resolve my factory and then create my objects. </li></ol> To deal with the immutable constraint I can create new ones within <code>With</code> methods. I can live with this too. <br><br>The main drawback remains the verbosity/burden of those methods. <br>Using <a href="https://github.com/ekonbenefits/impromptu-interface">Impromptu Interface</a> to generate partial test data spares builder classes creation while keeping verbosity acceptable and intent clear. <br>Does it make sense? </p> </div> <div class="comment-date">2017-08-22 13:24 UTC</div> </div> <div class="comment" id="234e0beada294425aa5303a2c5dd3ae2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#234e0beada294425aa5303a2c5dd3ae2">#</a></div> <div class="comment-content"> <p> That helps clarify things, thank you. </p> <p> I know that obviously, I could try for myself, but when you write </p> <p> <pre><span style="color:blue;">var</span>&nbsp;address&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;City = <span style="color:#a31515;">&quot;Paris&quot;</span> &nbsp;&nbsp;}.ActLike&lt;<span style="color:#2b91af;">IAddress&gt;</span>();</pre> </p> <p> then what will be the value of <code>address.PostCode</code>? </p> </div> <div class="comment-date">2017-08-22 13:40 UTC</div> </div> <div class="comment" id="cd35dc42188b4388a22d84f1df3fa39c"> <div class="comment-author"><a href="https://www.linkedin.com/in/romainvasseur/">Romain Vasseur</a> <a href="#cd35dc42188b4388a22d84f1df3fa39c">#</a></div> <div class="comment-content"> <u>It throws an exception if accessed but live peacefully otherwise</u>. It is why I talked about <span style="color:#a31515;">partial</span> data.<br> You have to be aware of this. When your test focus on a single aspect of your class you can safely use it.<br> Imagine you are testing a <code>City</code> centric algorithm: you do not care about Street, Street number, Floor, and so on.<br> No need to create heavy/costly objects you can safely use a partial object which is only compliant with a part of the original interface.<br> The way you would have deal with if you had split <span style="color:#2b91af;">IAddress</span> interface into several parts namely <span style="color:#2b91af;">IHaveACity</span>, <span style="color:#2b91af;">IHaveAStreet</span>, ...<br> As it only declares what it needs to work the UT intent is pretty clear. As test builder it removes noisy stuff. </div> <div class="comment-date">2017-08-22 14:22 UTC</div> </div> <div class="comment" id="4a9cb187b36147f8b3a9fd439385c4e0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4a9cb187b36147f8b3a9fd439385c4e0">#</a></div> <div class="comment-content"> <p> Now I think I get it! Thank you for taking the time to explain. </p> </div> <div class="comment-date">2017-08-22 14:50 UTC</div> </div> <div class="comment" id="647593cbc5ca4f339c1c3cbd665933f3"> <div class="comment-author"><a href="https://stackoverflow.com/users/129073/gebb">Otto Gebb</a> <a href="#647593cbc5ca4f339c1c3cbd665933f3">#</a></div> <div class="comment-content"> <p>A slight variation on Robert Pajak's approach that allows writing <code>an.Address()</code> instead of unwieldy <code>AddressObjectMother.Create()</code>: <a href="https://softwareengineering.stackexchange.com/a/286403/105014">Mother Factory</a>.</p> <p>Another usage sample: <a href="https://gist.github.com/M0ns1gn0r/72dc43187418118c68d413d0eedb881d">gist</a>. </div> <div class="comment-date">2017-09-12 9:16 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. From Test Data Builders to the identity functor https://blog.ploeh.dk/2017/08/14/from-test-data-builders-to-the-identity-functor 2017-08-14T11:34:00+00:00 Mark Seemann <div id="post"> <p> <em>The Test Data Builder unit testing design pattern is closely related to the identity functor.</em> </p> <p> The <a href="http://www.natpryce.com/articles/000714.html">Test Data Builder</a> design pattern is a valuable technique for managing data for unit testing. It enables you to express test cases in such a way that the important parts of the test case stands out in your code, while the unimportant parts disappear. It perfectly fits <a href="http://amzn.to/19W4JHk">Robert C. Martin's definition</a> of an <em>abstraction:</em> <blockquote> "Abstraction is the elimination of the irrelevant and the amplification of the essential" </blockquote> Not only are Test Data Builders great abstractions, but they're also eminently composable. You can use fine-grained Test Data Builders as building blocks for more complex Test Data Builders. This turns out to be more than a coincidence. In this series of articles, you'll learn how Test Data Builders are closely related to the <em>identity functor</em>. If you don't know what a <a href="/2018/03/22/functors">functor</a> is, then keep reading; you'll learn about functors as well. <ol> <li><a href="/2017/08/15/test-data-builders-in-c">Test Data Builders in C#</a></li> <li><a href="/2017/08/21/generalised-test-data-builder">Generalised Test Data Builder</a></li> <li><a href="/2017/08/28/the-builder-functor">The Builder functor</a></li> <li><a href="/2017/09/04/builder-as-identity">Builder as Identity</a></li> <li><a href="/2017/09/11/test-data-without-builders">Test data without Builders</a></li> <li>(<a href="/2017/09/18/the-test-data-generator-functor">The Test Data Generator functor</a>)</li> </ol> By reading these articles, you'll learn the following: <ul> <li>How to make your code easier to use in unit tests.</li> <li>What a functor is.</li> <li>How Test Data Builders generalise.</li> <li>Why Test Data Builders are composable.</li> </ul> If you've ever struggled with defining good abstractions, learning about functors (and some related concepts) will help. </p> <p> For readers wondering if this is 'yet another monad tutorial', it's not; it's a functor tutorial. </p> <p> <strong>Next:</strong> <a href="/2017/08/15/test-data-builders-in-c">Test Data Builders in C#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. F# free monad recipe https://blog.ploeh.dk/2017/08/07/f-free-monad-recipe 2017-08-07T08:11:00+00:00 Mark Seemann <div id="post"> <p> <em>How to create free monads in F#.</em> </p> <p> This is not a design pattern, but it's something related. Let's call it a <em>recipe</em>. A design pattern should, in my opinion, be fairly language-agnostic (although <a href="/2012/05/25/Designpatternsacrossparadigms">hardly universally applicable</a>). This article, on the contrary, specifically addresses a problem in F#: </p> <p class="text-center"> <em>How do you create a free monad in F#?</em> </p> <p class="text-center"> <strong>By following the present recipe.</strong> </p> <p> The recipe here is a step-by-step process, but be sure to first read the sections on motivation and when to use it. A free monads isn't a goal in itself. </p> <p> This article doesn't attempt to <em>explain</em> the details of free monads, but instead serve as a reference. For an introduction to free monads, I think my article <a href="/2017/06/27/pure-times">Pure times</a> is a good place to start. See also the <em>Motivating examples</em> section, below. </p> <h3 id="4bb1752adeff4eca8d69d52643c9c048"> Motivation <a href="#4bb1752adeff4eca8d69d52643c9c048" title="permalink">#</a> </h3> <p> A frequently asked question about F# is: <a href="https://stackoverflow.com/q/34011895/126014">what's the F# equivalent to an interface?</a> There's no single answer to this question, because, as always, It Depends&trade;. Why do you need an interface in the first place? What is its intended use? </p> <p> Sometimes, in OOP, an interface can be used for a <a href="https://en.wikipedia.org/wiki/Strategy_pattern">Strategy</a>. This enables you to dynamically replace or select between different (sub)algorithms at run-time. If the algorithm is <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, then an <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> F# equivalent would be <em>a function</em>. </p> <p> At other times, though, the person asking the question has Dependency Injection in mind. In OOP, dependencies are often modelled as interfaces with several members. Such <a href="/2017/01/30/partial-application-is-dependency-injection">dependencies are systematically impure</a>, and thereby not part of functional design. If at all possible, <a href="/2017/07/10/pure-interactions">prefer impure/pure/impure sandwiches over interactions</a>. Sometimes, however, you'll need something that works like an interface or abstract base class. Free monads can address such situations. </p> <p> In general, a <a href="https://twitter.com/hmemcpy/status/771359835514368000">free monad allows you to build a monad from any functor</a>, but why would you want to do that? The most common reason I've encountered is exactly in order to model impure interactions in a pure manner; in other words: Dependency Injection. </p> <h3 id="cbacb5e8cc7846f1b0a07a61c89dbc33"> Refactor interface to functor <a href="#cbacb5e8cc7846f1b0a07a61c89dbc33" title="permalink">#</a> </h3> <p> This recipe comes in three parts: <ol> <li>A recipe for refactoring interfaces to a functor.</li> <li>The core recipe for creating a monad from any functor.</li> <li>A recipe for adding an interpreter.</li> </ol> The universal recipe for creating a monad from any functor follows in a later section. In this section, you'll see how to refactor an interface to a functor. </p> <p> Imagine that you have an interface that you'd like to refactor. In C# it might look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFace</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Out1</span>&nbsp;Member1(<span style="color:#2b91af;">In1</span>&nbsp;input); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Out2</span>&nbsp;Member2(<span style="color:#2b91af;">In2</span>&nbsp;input); }</pre> </p> <p> In F#, it'd look like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">IFace</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:navy;">Member1</span>&nbsp;:&nbsp;input:<span style="color:teal;">In1</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">Out1</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">abstract</span>&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:navy;">Member2</span>&nbsp;:&nbsp;input:<span style="color:teal;">In2</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">Out2</span></pre> </p> <p> I've deliberately kept the interface vague and abstract in order to showcase the <em>recipe</em> instead of a particular example. For realistic examples, refer to the <em>examples</em> section, further down. </p> <p> To refactor such an interface to a functor, do the following: <ol> <li>Create a discriminated union. Name it after the interface name, but append the word <em>instruction</em> as a suffix.</li> <li>Make the union type generic.</li> <li> For each member in the interface, add a case. <ol> <li>Name the case after the name of the member.</li> <li>Declare the type of data contained in the case as a <em>pair</em> (a two-element tuple).</li> <li>Declare the type of the first element in that tuple as the type of the input argument(s) to the interface member. If the member has more than one input argument, declare it as a (nested) tuple.</li> <li>Declare the type of the second element in the tuple as a function. The <em>input</em> type of that function should be the output type of the original interface member, and the output type of the function should be the generic type argument for the union type.</li> </ol> </li> <li>Add a <em>map</em> function for the union type. I'd recommend making this function private and avoid naming it <code>map</code> in order to prevent naming conflicts. I usually name this function <code>mapI</code>, where the <em>I</em> stands for <em>instruction</em>.</li> <li>The <em>map</em> function should take a function of the type <code>'a -&gt; 'b</code> as its first (curried) argument, and a value of the union type as its second argument. It should return a value of the union type, but with the generic type argument changed from <code>'a</code> to <code>'b</code>.</li> <li>For each case in the union type, map it to a value of the same case. Copy the (non-generic) first element of the pair over without modification, but compose the function in the second element with the input function to the <em>map</em> function.</li> </ol> Following that recipe, the above interface becomes this union type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">FaceInstruction</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Member1</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">In1</span>&nbsp;*&nbsp;(<span style="color:teal;">Out1</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a)) |&nbsp;<span style="color:navy;">Member2</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">In2</span>&nbsp;*&nbsp;(<span style="color:teal;">Out2</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a))</pre> </p> <p> The <em>map</em> function becomes: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;FaceInstruction&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;FaceInstruction&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#9b9b9b;">mapI</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Member1</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Member1</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Member2</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Member2</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>)</pre> </p> <p> Such a combination of union type and <em>map</em> function satisfies the functor laws, so that's how you refactor an interface to a functor. </p> <h3 id="451c4dc5cf794e5a92177913c039d71a"> Free monad recipe <a href="#451c4dc5cf794e5a92177913c039d71a" title="permalink">#</a> </h3> <p> Given any functor, you can create a monad. The monad will be a new type that contains the functor; you will not be turning the functor itself into a monad. (Some functors can be turned into monads themselves, but if that's the case, you don't need to create a free monad.) </p> <p> The recipe for turning any functor into a monad is as follows: <ol> <li>Create a generic discriminated union. You can name it after the underlying functor, but append a suffix such as <em>Program</em>. In the following, this is called the 'program' union type.</li> <li>Add two cases to the union: <code>Free</code> and <code>Pure</code>.</li> <li>The <code>Free</code> case should contain a single value of the contained functor, generically typed to the 'program' union type itself. This is a recursive type definition.</li> <li>The <code>Pure</code> case should contain a single value of the union's generic type.</li> <li>Add a <code>bind</code> function for the new union type. The function should take two arguments:</li> <li>The first argument to the <code>bind</code> function should be a function that takes the generic type argument as input, and returns a value of the 'program' union type as output. In the rest of this recipe, this function is called <code>f</code>.</li> <li>The second argument to the <code>bind</code> function should be a 'program' union type value.</li> <li>The return type of the <code>bind</code> function should be a 'program' union type value, with the same generic type as the return type of the first argument (<code>f</code>).</li> <li>Declare the <code>bind</code> function as recursive by adding the <code>rec</code> keyword.</li> <li>Implement the <code>bind</code> function by pattern-matching on the <code>Free</code> and <code>Pure</code> cases:</li> <li>In the <code>Free</code> case, pipe the contained functor value to the functor's <em>map</em> function, using <code>bind f</code> as the mapper function; then pipe the result of that to <code>Free</code>.</li> <li>In the <code>Pure</code> case, return <code>f x</code>, where <code>x</code> is the value contained in the <code>Pure</code> case.</li> <li>Add a computation expression builder, using <code>bind</code> for <code>Bind</code> and <code>Pure</code> for <code>Return</code>.</li> </ol> Continuing the above example, the 'program' union type becomes: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">FaceProgram</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Free</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">FaceInstruction</span>&lt;<span style="color:teal;">FaceProgram</span>&lt;&#39;a&gt;&gt; |&nbsp;<span style="color:navy;">Pure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> It's worth noting that the <code>Pure</code> case always looks like that. While it doesn't take much effort to write it, you could copy and paste it from another free monad, and no changes would be required. </p> <p> According to the recipe, the <code>bind</code> function should be implemented like this: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;FaceProgram&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;FaceProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;FaceProgram&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> |&nbsp;<span style="color:navy;">Free</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:navy;">mapI</span>&nbsp;(<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>)&nbsp;|&gt;&nbsp;<span style="color:navy;">Free</span> |&nbsp;<span style="color:navy;">Pure</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x</pre> </p> <p> Apart from one small detail, the <code>bind</code> function always looks like that, so you can often copy and paste it from here and use it in your code, if you will. The only variation is that the underlying functor's <em>map</em> function isn't guaranteed to be called <code>mapI</code> - but if it is, you can use the above <code>bind</code> function as is. No modifications will be necessary. </p> <p> In F#, a monad is rarely a goal in itself, but once you have a monad, you can add a <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expression builder</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;FaceBuilder&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Bind&nbsp;(x,&nbsp;f)&nbsp;=&nbsp;bind&nbsp;f&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Return&nbsp;x&nbsp;=&nbsp;Pure&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.ReturnFrom&nbsp;x&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Zero&nbsp;()&nbsp;=&nbsp;Pure&nbsp;()</pre> </p> <p> While you could add more members (such as <code>Combine</code>, <code>For</code>, <code>TryFinally</code>, and so on), I find that usually, those four methods are all I need. </p> <p> Create an instance of the builder object, and you can start writing computation expressions: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;face&nbsp;=&nbsp;FaceBuilder&nbsp;() </pre> </p> <p> Finally, as an optional step, if you've refactored an interface to an instruction set, you can add convenience functions that lift each instruction case to the free monad type: <ol> <li>For each case, add a function of the same name, but camelCased instead of PascalCased.</li> <li>Each function should have input arguments that correspond to the first element of the case's contained tuple (i.e. the input argument for the original interface). I usually prefer the arguments in curried form, but that's not a requirement.</li> <li>Each function should return the corresponding instruction union case inside of the <code>Free</code> case. The case constructor must be invoked with the pair of data it requires. Populate the first element with values from the input arguments to the convenience function. The second element should be the <code>Pure</code> case constructor, passed as a function.</li> </ol> In the current example, that would be two functions, one for each case of <code>FaceInstruction&lt;'a&gt;</code>: </p> <p> <pre><span style="color:green;">//&nbsp;In1&nbsp;-&gt;&nbsp;FaceProgram&lt;Out1&gt;</span> <span style="color:blue;">let</span>&nbsp;member1&nbsp;in1&nbsp;=&nbsp;Free&nbsp;(Member1&nbsp;(in1,&nbsp;Pure)) <span style="color:green;">//&nbsp;In2&nbsp;-&gt;&nbsp;FaceProgram&lt;Out2&gt;</span> <span style="color:blue;">let</span>&nbsp;member2&nbsp;in2&nbsp;=&nbsp;Free&nbsp;(Member2&nbsp;(in2,&nbsp;Pure))</pre> </p> <p> Such functions are conveniences that make it easier to express what the underlying functor expresses, but in the context of the free monad. </p> <h3 id="2bd224b81c644c85b2809a35085f1d9d"> Interpreters <a href="#2bd224b81c644c85b2809a35085f1d9d" title="permalink">#</a> </h3> <p> A free monad is a recursive type, and values are trees. The leafs are the <code>Pure</code> values. Often (if not always), the point of a free monad is to evaluate the tree in order to pull the leaf values out of it. In order to do that, you must add an interpreter. This is a function that recursively pattern-matches over the free monad value until it encounters a <code>Pure</code> case. </p> <p> At least in the case where you've refactored an interface to a functor, writing an interpreter also follows a recipe. This is equivalent to writing a concrete class that implements an interface. <ol> <li>For each case in the instruction-set functor, write an implementation function that takes the case's 'input' tuple element type as input, and returns a value of the type used in the case's second tuple element. Recall that the second element in the pair is a function; the output type of the implementation function should be the input type for that function.</li> <li>Add a function to implement the interpreter; I often call it <code>interpret</code>. Make it recursive by adding the <code>rec</code> keyword.</li> <li>Pattern-match on <code>Pure</code> and each case contained in <code>Free</code>.</li> <li>In the <code>Pure</code> case, simply return the value contained in the case.</li> <li>In the <code>Free</code> case, pattern-match the underlying pair out if each of the instruction-set functor's cases. The first element of that tuple is the 'input value'. Pipe that value to the corresponding implementation function, pipe the return value of that to the function contained in the second element of the tuple, and pipe the result of that recursively to the interpreter function.</li> </ol> Assume that two implementation functions <code>imp1</code> and <code>imp2</code> exist. According to the recipe, <code>imp1</code> has the type <code>In1 -&gt; Out1</code>, and <code>imp2</code> has the type <code>In2 -&gt; Out2</code>. Given these functions, the running example becomes: </p> <p> <pre><span style="color:green;">//&nbsp;FaceProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;interpret&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Pure&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;(Member1&nbsp;(x,&nbsp;next))&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;|&gt;&nbsp;imp1&nbsp;|&gt;&nbsp;next&nbsp;|&gt;&nbsp;interpret &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;(Member2&nbsp;(x,&nbsp;next))&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;|&gt;&nbsp;imp2&nbsp;|&gt;&nbsp;next&nbsp;|&gt;&nbsp;interpret</pre> </p> <p> The <code>Pure</code> case always looks like that. Each of the <code>Free</code> cases use a different implementation function, but apart from that, they are, as you can tell, the spitting image of each other. </p> <p> Interpreters like this are often impure because the implementation functions are impure. Nothing prevents you from defining pure interpreters, although they often have limited use. They do have their place in unit testing, though. </p> <p> <pre><span style="color:green;">//&nbsp;Out1&nbsp;-&gt;&nbsp;Out2&nbsp;-&gt;&nbsp;FaceProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;interpretStub&nbsp;out1&nbsp;out2&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Pure&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;(Member1&nbsp;(_,&nbsp;next))&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;out1&nbsp;|&gt;&nbsp;next&nbsp;|&gt;&nbsp;interpretStub&nbsp;out1&nbsp;out2 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;(Member2&nbsp;(_,&nbsp;next))&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;out2&nbsp;|&gt;&nbsp;next&nbsp;|&gt;&nbsp;interpretStub&nbsp;out1&nbsp;out2</pre> </p> <p> This interpreter effectively ignores the input value contained within each <code>Free</code> case, and instead uses the pure values <code>out1</code> and <code>out2</code>. This is essentially a <a href="http://xunitpatterns.com/Test%20Stub.html">Stub</a> - an 'implementation' that always returns pre-defined values. </p> <p> The point is that you can have more than a single interpreter, pure or impure, just like you can have more than one implementation of an interface. </p> <h3 id="8948bae068174df2ac21bbea8d296281"> When to use it <a href="#8948bae068174df2ac21bbea8d296281" title="permalink">#</a> </h3> <p> Free monads are often used instead of Dependency Injection. Note, however, that while the free monad values themselves are pure, they <em>imply</em> impure behaviour. In my opinion, the main benefit of pure code is that, as a code reader and maintainer, I don't have to worry about side-effects if I know that the code is pure. With a free monad, I <em>do</em> have to worry about side-effects, because, although the ASTs are pure, an impure interpreter will cause side-effects to happen. At least, however, the side-effects are <em>known</em>; they're restricted to a small subset of operations. Haskell enforces this distinction, but F# doesn't. The question, then, is how valuable you find this sort of design. </p> <p> I think it still has <em>some</em> value, because a free monad explicitly communicates an intent of doing something impure. This intent becomes encoded in the types in your code base, there for all to see. Just as I prefer that functions return <code>'a option</code> values if they may fail to produce a value, I like that I can tell from a function's return type that a delimited set of impure operations may result. </p> <p> Clearly, creating free monads in F# requires some boilerplate code. I hope that this article has demonstrated that writing that boilerplate code isn't <em>difficult</em> - just follow the recipe. You almost don't have to think. Since a monad is a universal abstraction, once you've written the code, it's unlikely that you'll need to deal with it much in the future. After all, mathematical abstractions don't change. </p> <p> Perhaps a more significant concern is how familiar free monads are to developers of a particular code base. Depending on your position, you could argue that free monads come with high cognitive overhead, or that they specifically <em>lower</em> the cognitive overhead. </p> <p> Insights are obscure until you grasp them; after that, they become clear. </p> <p> This applies to free monads as well. You have to put effort into understanding them, but once you do, you realise that they are more than a pattern. They are universal abstractions, governed by laws. Once you <a href="/ref/stranger-in-a-strange-land">grok</a> free monads, their cognitive load wane. </p> <p> Consider, then, the developers who will be interacting with the free monad. If they already know free monads, or have enough of a grasp of monads that this might be their next step, then using free monads could be beneficial. On the other hand, if most developers are new to F# or functional programming, free monads should probably be avoided for the time being. </p> <p> This flowchart summarises the above reflections: </p> <p> <img src="/content/binary/decision-flowchart-for-free-monads.png" alt="Decision flowchart for whether or not to choose free monads as a design principle."> </p> <p> Your first consideration should be whether your <a href="/2017/07/10/pure-interactions">context enables an impure/pure/impure sandwich</a>. If so, there's no reason to make things more complicated than they have to be. To use <a href="http://bit.ly/mythical-man-month">Fred Brooks' terminology</a>, this should go a long way to avoid accidental complexity. </p> <p> If you can't avoid long-running, impure interactions, then consider whether purity, or strictly functional design, is important to you. F# is a multi-paradigmatic language, and it's perfectly possible to write code that's impure, yet still well-structured. You can use <a href="/2017/01/30/partial-application-is-dependency-injection">partial application as an idiomatic alternative to Dependency Injection.</a> </p> <p> If you prefer to keep your code functional and explicit, you may consider using free monads. In this case, I still think you should consider the maintainers of the code base in question. If everyone involved are comfortable with free monads, or willing to learn, then I believe it's a viable option. Otherwise, I'd recommend falling back to partial application, even though Dependency Injection makes everything impure. </p> <h3 id="68a926dc27374f4faa9d4dc2f7f5e02b"> Motivating examples <a href="#68a926dc27374f4faa9d4dc2f7f5e02b" title="permalink">#</a> </h3> <p> The strongest motivation, I believe, for introducing free monads into a code base is to model long-running, impure interactions in a functional style. </p> <p> Like most other software design considerations, the overall purpose of application architecture is to deal with (essential) complexity. Thus, any example must be complex enough to warrant the design. There's little point in a Dependency Injection <em>hello world</em> example in C#. Likewise, a <em>hello world</em> example using free monads hardly seems justified. For that reason, examples are provided in separate articles. </p> <p> A good place to start, I believe, is with the small <a href="/2017/06/27/pure-times">Pure times</a> article series. These articles show how to address a particular, authentic problem using strictly functional programming. The focus of these articles is on problem-solving, so they sometimes omit detailed explanations in order to keep the narrative moving. </p> <p> If you need detailed explanations about all elements of free monads in F#, the <a href="/2017/07/10/pure-interactions">present article series offers just that</a>, particularly the <a href="/2017/07/11/hello-pure-command-line-interaction">Hello, pure command-line interaction</a> article. </p> <h3 id="f018b55231d5486185e64ea6fbdab544"> Variations <a href="#f018b55231d5486185e64ea6fbdab544" title="permalink">#</a> </h3> <p> The above recipes describe the regular scenario. Variations are possible. Obviously, you can choose different naming strategies and so on, but I'm not going to cover this in greater detail. </p> <p> There are, however, various degenerate cases that deserve a few words. An interaction may return no data, or take no input. In F#, you can always model the lack of data as <code>unit</code> (<code>()</code>), so it's definitely possible to define an instruction case like <code>Foo of (unit * Out1 -&gt; 'a)</code>, or <code>Bar of (In2 * unit -&gt; 'a)</code>, but since <code>unit</code> doesn't contain any data, you can remove it without changing the abstraction. </p> <p> The <a href="/2017/07/11/hello-pure-command-line-interaction">Hello, pure command-line interaction</a> article contains a single type that exemplifies both degenerate cases. It defines this instruction set: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CommandLineInstruction</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">ReadLine</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">string</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a) |&nbsp;<span style="color:navy;">WriteLine</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span>&nbsp;*&nbsp;&#39;a</pre> </p> <p> The <code>ReadLine</code> case takes no input, so instead of containing a pair of input and continuation, this case contains only the continuation function. Likewise, the <code>WriteLine</code> case is also degenerate, but here, there's no output. This case <em>does</em> contain a pair, but the second element isn't a function, but a value. </p> <p> This has some superficial consequences for the implementation of functor and monad functions. For example, the <code>mapI</code> function becomes: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;CommandLineInstruction&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;CommandLineInstruction&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">mapI</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">ReadLine</span>&nbsp;<span style="color:navy;">next</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">ReadLine</span>&nbsp;(<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">WriteLine</span>&nbsp;(x,&nbsp;next)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">WriteLine</span>&nbsp;(x,&nbsp;next&nbsp;|&gt;&nbsp;<span style="color:navy;">f</span>)</pre> </p> <p> Notice that in the <code>ReadLine</code> case, there's no tuple on which to pattern-match. Instead, you can directly access <code>next</code>. </p> <p> In the <code>WriteLine</code> case, the return value changes from function composition (<code>next &gt;&gt; f</code>) to a regular function call (<code>next |&gt; f</code>, which is equivalent to <code>f next</code>). </p> <p> The <em>lift</em> functions also change: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;string&gt;</span> <span style="color:blue;">let</span>&nbsp;readLine&nbsp;=&nbsp;Free&nbsp;(ReadLine&nbsp;Pure) <span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;CommandLineProgram&lt;unit&gt;</span> <span style="color:blue;">let</span>&nbsp;writeLine&nbsp;s&nbsp;=&nbsp;Free&nbsp;(WriteLine&nbsp;(s,&nbsp;Pure&nbsp;()))</pre> </p> <p> Since there's no input, <code>readLine</code> degenerates to a value, instead of a function. On the other hand, while <code>writeLine</code> remains a function, you'll have to pass a <em>value</em> (<code>Pure ()</code>) as the second element of the pair, instead of the regular function (<code>Pure</code>). </p> <p> Apart from such minor changes, the omission of <code>unit</code> values for input or output has little significance. </p> <p> Another variation from the above recipe that you may see relates to interpreters. In the above recipe, I described how, for each instruction, you should create an implementation function. Sometimes, however, that function is only a few lines of code. When that happens, I occasionally inline the function directly in the interpreter. Once more, the <code>CommandLineProgram</code> API provides an example: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">interpret</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Pure</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">ReadLine</span>&nbsp;&nbsp;<span style="color:navy;">next</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">Console</span>.<span style="color:navy;">ReadLine</span>&nbsp;()&nbsp;|&gt;&nbsp;<span style="color:navy;">next</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">WriteLine</span>&nbsp;(s,&nbsp;next))&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Console</span>.<span style="color:navy;">WriteLine</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span></pre> </p> <p> Here, no custom implementation functions are required, because <code>Console.ReadLine</code> and <code>Console.WriteLine</code> already exist and serve the desired purpose. </p> <h3 id="3155d057fe424d5eb0eaccfd3f25566a"> Summary <a href="#3155d057fe424d5eb0eaccfd3f25566a" title="permalink">#</a> </h3> <p> This article describes a repeatable, and automatable, process for refactoring an interface to a free monad. I've done this enough times now that I believe that this process is always possible, but I have no formal proof for this. </p> <p> I also strongly suspect that the reverse process is possible. For any instruction set elevated to a free monad, I think you should be able to define an object-oriented interface. If this is true, then object-oriented interfaces and AST-based free monads are isomorphic. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b88620b15b1a491d8e5e0c81364a8966"> <div class="comment-author"><a href="http://criticalsoftwareblog.com">Yacoub Massad</a> <a href="#b88620b15b1a491d8e5e0c81364a8966">#</a></div> <div class="comment-content"> <p> Hello Mark. I am trying to understand what is going on. <br> So basically the Free Moand allows us to separate pure code from impure code even when the impure/pure/impure sandwish idea is not possible to implement. Right? <br> We want to separate pure and impure code for these reasons: (1) Easier testing (2) Reasoning about pure code is easier than impure code (3) making impure code explicit makes it easier to understand programs. Is this correct? <br> What I am still trying to figure out is why we can't simply do this with Dependency Injection? <br> We can separate all units of behavior into pure ones and impure ones (e.g. functions), and then compose them all in the Composition Root. Pure units take no dependencies, they take in "direct input" and give back "direct output" as you describe in one of your blog posts. <br> To make the impure code explicit and clear, we can make the root method in the Composition Root construct all impure units of behavior first (e.g. adapters to the external world) and then inject them into a method that bakes these dependencies with the rest of pure code. E.g.: <code>public static IApplication CreateApplication(IImpureDependency1 dependency1, IImpureDependency2 dependency2) => { //compose graph here} </code> <br> If you have sub methods that the CreateApplication method uses for modularizing the Composition Root, they will also take any impurities they need as parameters. <br> So in summary, only the Composition Root knows about the impure parts of the application and they are explicitly stated as parameters in the Composition Root methods. <br> Doesn't this solve the impure/pure separation issue? <br> For example, to test, you can easilly call the CreateApplication method and pass the fake (pure) dependencies. This will make the whole graph pure in the test. <br> Also, the Composition Root would make it clear which impure dependencies each component in the system depends on. <br> Am I missing something? </p> </div> <div class="comment-date">2018-05-03 21:42 UTC</div> </div> <div class="comment" id="9b5fb4c0c2cb42169b121bd48808d7f7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9b5fb4c0c2cb42169b121bd48808d7f7">#</a></div> <div class="comment-content"> <p> Hello Yacoub, thank you for writing. Your summary of the motivations covers most of them. The reason that purity interests me is that it forces me (and everyone else) to consider decoupling. One day, I should write a more explicit article about this, but I believe that the general problem with programming today has little to do with writing code, but with <em>reading</em> it. Until I get such an article written, I can only refer to my <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">Humane Code</a> video, and perhaps my <a href="https://www.dotnetrocks.com/?show=1542"> recent appearance on .NET Rocks!</a>. What fundamentally interests me is how to break down code into small enough chunks that they fit in our brains at all levels of abstraction. Purity, and functional programming in general, attracts me because it offers a principled way of doing that. </p> <p> If we forget about functional programming and free monads for a while, we could ask a question similar to yours about Dependency Injection (DI). Why should we use Dependency Injection? Can't we just, say, call a database when we need some data? Technically, we can, but we deliberately invert the control of our code so that it becomes easier to break apart into smaller chunks. You may find this observation trivial, but it wasn't ten years ago, and I made much effort in <a href="http://amzn.to/12p90MG">my book</a> to explain the benefits of DI. </p> <p> The problem with DI is that at detailed levels of abstractions, DI-based code may fit in our brains, but at higher levels of abstraction the complexity still increases. Put another way, understanding a single class that receives a few dependencies is easy. Getting a high-level, big-picture understanding of a DI-based code base can still be quite the challenge. At a high level of abstraction, the moving parts in underlying components are still too visible, you could say. </p> <p> Strictly functional programming interests me because, by pushing impure behaviours to the boundaries of the application, the pure core of an application becomes easier to treat as a hierarchy of abstractions. (I really need to write an article with diagrams about this some day.) </p> <p> What's strictly functional programming? It's code that obeys the rule that pure code can't call impure code. The reason I find Haskell so interesting is that the compiler enforces that rule. Code isn't pure if it calls impure functions, and in Haskell, the code simply will not compile if you attempt to do that. </p> <p> F#, on the other hand, doesn't work like that. There's no compile-time check of whether the code is pure or impure. Thus, when you pass functions to other functions, your higher-order function could look pure, but since you don't know what an 'injected' function does, you really don't know if it's pure or not. In F#, all it takes is a single call to, say, <code>DateTime.Now</code>, <code>Guid.NewGuid()</code>, or similar, deep in your system, and that makes the entire code base impure! </p> <p> The only way to prevent that in F# is by diligence. </p> <p> That's a roundabout answer to your question. The gist of it, though, is that in F#, you rarely need free monads. If you find yourself in the situation where a free monad would be required in Haskell, you could just as well use DI, or rather, partial application. <a href="/2017/01/30/partial-application-is-dependency-injection">My article on that approach</a> explains how this works in F#, but also why it doesn't work in Haskell. When you inject impure behaviour into an 'otherwise' pure function, then everything becomes impure. </p> <p> This is where F# differs from Haskell. In Haskell, such an attempt simply doesn't compile. In F#, an otherwise pure function suddenly becomes impure. If you mostly care about that distinction because of, say, testability, then that's not a problem, because when you 'inject' pure behaviour, then the composed function is still pure, and thus trivial to unit test. </p> <p> The entire system is still impure with that design, though, and that can make it difficult to fit the entire application behaviour in our brains. </p> <p> I'm afraid this answer doesn't help. I'll have to write a more coherent article on this some day, but I wanted to leave this here because, realistically, a more coherent article isn't part of my immediate plans. </p> </div> <div class="comment-date">2018-05-06 12:41 UTC</div> </div> <div class="comment" id="7941557e4597479e95748e0f91c54b1c"> <div class="comment-author"><a href="http://criticalsoftwareblog.com">Yacoub Massad</a> <a href="#7941557e4597479e95748e0f91c54b1c">#</a></div> <div class="comment-content"> <p> Hello Mark. Thanks for the reply and for providing the links. I have already watched your Humane Code videos at clean coders before. Will listen to the podcast too. </p> <p> I understand that with the free monad, you can maintain the rule that pure code will never call impure code. </p> <p> This is one goal. </p> <p> However, as you describe, this by itself is not the final goal. We want to achieve this goal as a mean to achieve other goals. For example, we want our code to be easier to reason about. </p> <p> As you describe, we cannot achieve the first goal using DI (or partial application). And in Haskell, the compiler will prevent us from even trying. </p> <p> However, I think you agree with me that there is still some great value in separating "pure" and impure code in different functions or classes, and then combining them in the Composition Root. This is basically Command Query Separation + DI. Although the graph as a whole is impure, some benefit (e.g. easier to reason about code) is still there as a result of the separation. </p> <p> What I am trying to argue (or let me say think about and discuss) is that if one does the following: <ol> <li>Separate impure and pure behavior at the level of individual units of behavior (e.g. functions or classes).</li> <li>Compose these units at the Composition Root (only the Composition Root knows about the impure units).</li> <li>In the Composition Root, first all impure units are created/prepared, and then injected into "pure" (now not pure) units.</li> <li>Make impure dependencies explicit as parameters in the "Create" methods of the Composition Root. (Basically "Create" methods are a way to modularize the Composition Root. I describe what I mean in more details <a href="http://www.dotnetcurry.com/patterns-practices/1285/clean-composition-roots-dependency-injection">here</a></li> </ol> then there is not much value in moving from what I just described to using Free monads just to make the Haskell compiler happy :). </p> <p> Or is there something that I am missing? </p> <p> Basically, if we forget for a moment about the first goal (since it is only a mean to other goals), what goals will we be not achieving? </p> <p> In your reply, I can find the following that might answer these questions: <br> "Getting a high-level, big-picture understanding of a DI-based code base can still be quite the challenge. At a high level of abstraction, the moving parts in underlying components are still too visible" </p> <p> But I can't understand what you mean here. What is the problem here? and how does the Free monad fix it? </p> <p> I hope I was able to explain my ideas correctly. </p> </div> <div class="comment-date">2018-05-06 21:19 UTC</div> </div> <div class="comment" id=""> <div class="comment-author"><a href="http://criticalsoftwareblog.com">Yacoub Massad</a> <a href="#">#</a></div> <div class="comment-content"> <p> Reading my comment again, I would like to add/update a few things. </p> <p> Regarding CQS, this is not exactly the same as separating impure and pure code. Still, a query can be impure (like one that reads from the database). Such query can be separated into a set of pure and impure queries. Also, a command can have some pure logic in it that can be extracted into a separate pure query (or queries). But, CQS is a step in the right direction towards this and it is a good example of how separation at some level has benefits of its own. </p> <p> I would like to explain also that the steps I describe in my comment aim basically to delay the composition of pure and impure code to the last possible moment. So basically, all pure logic is composed first (parameterized with functions/delegates/interfaces representing possibly impure code). After that, impure code will be injected into such pure graph rendering it impure of course. </p> <p> So basically, imagine an imaginary version of Haskell that would allow the root method of an application to allow “pure” code to call impure code. </p> </div> <div class="comment-date">2018-05-10 19:59 UTC</div> </div> <div class="comment" id="582bd312aa0049c3aaf003e5de5b3eaa"> <div class="comment-author"><a href="http://criticalsoftwareblog.com">Yacoub Massad</a> <a href="#582bd312aa0049c3aaf003e5de5b3eaa">#</a></div> <div class="comment-content"> <p> Here is a concrete example. Imagine these three pure functions: </p> <p> (A, Func&lt;C,D&gt; dep1, Func&lt;E,F&gt; dep2) =&gt; B (1) </p> <p> (C, Func&lt;G,H&gt; dep3) =&gt; D (2) </p> <p> (G, Func&lt;I,J&gt; dep4) =&gt; H (3) </p> <p> Now, in the Composition Root, we "compose" these together to get the following: </p> <p> (A, Func&lt;I,J&gt; dep4, Func&lt;E,F&gt; dep2) =&gt;B </p> <p> So far, this is a pure function, we havn't injected any impurities in it. Thinking about this, this might be a special case of dependency injection. We might call it dependency replacing or something like that. <br> What I have done is "inject" function #2 as dep1 in function #1. But this is not fully injected. I replaced "dep1" with "dep3". <br> Then, I "inject" function #3 as dep3. Again, this is not full injection as I replace it with "dep4". </p> <p> Now, after all "pure" functions have been baked together, I inject the impure "dep4" and "dep2" to get this: </p> <p> A =&gt; B </p> <p> I hope the code gets displayed correctly in the comment. </p> </div> <div class="comment-date">2018-05-14 07:30 UTC</div> </div> <div class="comment" id="57af39a3aff045feaeac211222d6e365"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#57af39a3aff045feaeac211222d6e365">#</a></div> <div class="comment-content"> <p> Yacoub, thank you for the pseudo-code. That makes it easier to discuss things. </p> <p> Your premise is that functions <em>1</em>, <em>2</em>, and <em>3</em> are pure. The rest of the argument rests on whether or not they are. Just to be sure that we share the same terminology, I take <em>pure</em> to mean <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>. Nothing you've written gives me any indication that this isn't your interpretation as well, so I mostly include this as an explicit definition for the benefit of other readers who may happen upon this discussion in the future. </p> <p> It's clear that a function (or method) that adds two numbers together is pure. This also applies to any other first-order function with <em>isolation</em>. I use the word <em>isolation</em> as described by <a href="http://jessitron.com">Jessica Kerr</a>: A function has the property of isolation when the only information it has about the external word is passed into it via arguments. </p> <p> You can write arbitrarily complex isolated functions in, say, C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;Foo(<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">string</span>&nbsp;month) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(year&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">DateTime</span>.MinValue; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(9999&nbsp;&lt;&nbsp;year) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">DateTime</span>.MaxValue; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:blue;">int</span>.TryParse(month,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">int</span>&nbsp;imonth)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imonth&nbsp;=&nbsp;7; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(imonth&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imonth&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(12&nbsp;&lt;&nbsp;imonth) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imonth&nbsp;=&nbsp;12; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;day&nbsp;=&nbsp;month.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(day&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day&nbsp;=&nbsp;10; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(28&nbsp;&lt;&nbsp;day) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day&nbsp;=&nbsp;20; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(year,&nbsp;imonth,&nbsp;day); }</pre> </p> <p> To be clear, this <code>Foo</code> method makes no sense, but it is, as far as I can tell, pure; it operates entirely on its input. </p> <p> Consider, however, this variation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;Foo(<span style="color:blue;">int</span>&nbsp;year,&nbsp;<span style="color:blue;">string</span>&nbsp;month) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(year&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">DateTime</span>.MinValue; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(9999&nbsp;&lt;&nbsp;year) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">DateTime</span>.MaxValue; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:blue;">int</span>.TryParse(month,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">int</span>&nbsp;imonth)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imonth&nbsp;=&nbsp;7; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(imonth&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imonth&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(12&nbsp;&lt;&nbsp;imonth) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imonth&nbsp;=&nbsp;12; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;day&nbsp;=&nbsp;month.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(day&nbsp;&lt;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day&nbsp;=&nbsp;10; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:#2b91af;">DateTime</span>.DaysInMonth(year,&nbsp;imonth)&nbsp;&lt;&nbsp;day) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day&nbsp;=&nbsp;20; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(year,&nbsp;imonth,&nbsp;day); }</pre> </p> <p> Notice that <code>DateTime.DaysInMonth(year, imonth)</code> replaces the hard-coded value <code>28</code>. Is this variation pure? </p> <p> I don't know. In order to figure that out, we'd need to understand if <code>DateTime.DaysInMonth</code> is pure. Does it use a hard-coded table or algorithm of leap years, or does it use a call to the operating system (OS)? If the latter, does the OS base its functionality on a pure implementation, or does it look up the information in some resource (like the Windows Registry)? </p> <p> With leap years, and for the Gregorian calendar, a pure algorithm exists, but imagine that we create a similar nonsense function that creates <code>DateTimeOffset</code> values, including time and time-zone offsets. In this case, figuring out if a value is valid relies on external data, since rules about daylight saving time are political and subject to change. </p> <p> My point is that without a machine tool (such as a type system) to guide us, it's practically impossible to reason about the purity of code. </p> <p> To make matters worse, as soon as you pass a function as an argument to another function, all bets are off. Even if you've diligently reviewed functions like <em>1</em>, <em>2</em>, and <em>3</em> above for purity, they're only pure if <code>dep2</code> and <code>dep4</code> are pure as well. </p> <p> Haskell takes away all that angst related to purity by enforcing it via its type system. This liberates us to worry about other things, because the compiler has our backs regarding purity. </p> <p> In C#, F#, Java, and most other languages, we get no such guarantees. As I've tried to demonstrate above, I'd regard all non-trivial code to be impure. All it takes is one system call, <code>Guid.NewGuid()</code>, <code>random.Next()</code>, <code>DateTime.Now</code>, <code>log.Warning("foo")</code>, etc. to make all code transitively calling such a statement impure. This is, realistically, impossible to prevent. </p> <p> Do we care, then? What if the functions <em>1</em>, <em>2</em>, and <em>3</em> are 'pure enough'? </p> <p> In an analogy to this discussion, in RESTful design, <code>GET</code> requests should be side-effect free. Almost all web servers, however, log HTTP requests, so <code>GET</code> requests are never side-effect free. The interpretation used in that context, therefore, is that <code>GET</code> requests should be free of side effects <em>for which the client is responsible</em>. </p> <p> You can have a similar discussion about functional programming. What if a function logs debug information? Does that change the observable state of the system? </p> <p> In any case, before even beginning to discuss whether dependency injection or partial application is functional, we need to make it clear why we care about purity. </p> <p> I care about purity because it eliminates entire classes of bugs. It also means that I don't have to log what happens inside my pure code; as long as I log what happens at the impure boundary, I can always reproduce the result of a pure computation. All this makes the overall code simpler. Logging, caching, instrumentation. Many cross-cutting concerns either disappear or greatly simplify. </p> <p> Returning to the overall discussion related to this article, free monads are one way to separate pure code from impure code. What you suggest, though, isn't pure, because all it takes to make the entire composition impure is that <code>dep2</code> or <code>dep4</code> are impure (or one of the 'pure' functions turning out to be impure after all). <a href="/2017/01/27/dependency-injection-is-passing-an-argument">It's Dependency Injection</a>, only you <a href="/2009/05/28/DelegatesAreAnonymousInterfaces">replace interfaces with delegates</a>. </p> <p> Does it matter? Probably not. Trying to keep things 'as pure as possible' in C# and similar languages could still provide benefits. That's how I approach F#. Ultimately, the goal is to make the code sustainable. If you can do that with <a href="/2017/01/30/partial-application-is-dependency-injection">Dependency Injection or partial application</a>, then the mission is accomplished. </p> <p> In Haskell, free monads are sometimes required, but in F#, it's a specialised design I'd only reach for in niche situations. </p> </div> <div class="comment-date">2018-05-15 8:36 UTC</div> </div> <div class="comment" id="582bd312aa0049c3aaf003e5de5b3e55"> <div class="comment-author">Nikolay Terletskyi <a href="#582bd312aa0049c3aaf003e5de5b3e55">#</a></div> <div class="comment-content"> <p> Hello! I just want to add my humble optinion to Mark and Yacoub disscussion. There is something that you could not achieve with partial application. </p> <p> Imagine that you have pipeline that process some entity. And if some conditions are met you need another one. Id of second entity is the field of first. </p> <p> So you can not just pass second entity as parameter. Because you do not sure if it is needed. You can pass function that give you an entity. </p> <p> But what is return type of this function? SecondEntetyType or Async&lt;SecondEntetyType&gt; or Task&lt;SecondEntetyType&gt;? What if you use library with callback interface to load this entity? <p> <p> Should you care about it to declare relations between first and second entities? </p> <p> Without free monad answer is yes !!! </p> <p> It is main achievement from free monads for me. </p> </div> <div class="comment-date">2019-06-06 5:30 UTC</div> </div> <div class="comment" id="2ddb4eea8ad94b60a3b184b373327b10"> <div class="comment-author">Nick Dunets <a href="#2ddb4eea8ad94b60a3b184b373327b10">#</a></div> <div class="comment-content"> <p> Hi, I had the same questions as Yacoub i.e. how is Free any better than raw Dependency Injection? </p> <p> After some research I can see at least couple of advantages. Even if code is messy and pure/impure parts interleaved chaotically, and function doesn't reduce to a simple tree and therefore can't serve as a convincing test case without being further interpreted etc. - there are still at least two advantages over DI: </p> <p> 1. No need to pass extra parameters representing the abstraction of impure code all over the place </p> <p> 2. Async aspect doesn't leak: e.g. WriteLine case from the article's example could have been interpreted as Console.Out.WriteLineAsync() - why not? but the "pure" core would still be decopuled from async aspect. </p> </div> <div class="comment-date">2019-07-09 20:15 UTC</div> </div> <div class="comment" id="0282a2e492c3423b9932e50a50a73b66"> <div class="comment-author">Romain Deneau <a href="https://twitter.com/DeneauRomain">@DeneauRomain</a> <a href="#0282a2e492c3423b9932e50a50a73b66">#</a></div> <div class="comment-content"> <p> Thank you Mark for these high quality articles. I was wondering if it wouldn't be more relevant to talk about <em>Operations</em> rather than <em>Members</em> in the interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFace</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Out1</span>&nbsp;Operation1(<span style="color:#2b91af;">In1</span>&nbsp;input); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Out2</span>&nbsp;Operation2(<span style="color:#2b91af;">In2</span>&nbsp;input); } </pre> </p> <p> Indeed, a dependency is needed in order to perform some <em>(impure?)</em> operations to be delegated to another object, in another layer or to follow the <em>Single Responsibility Principle</em>. Also it makes more sense to have operations rather than "members" in an instruction: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">FaceInstruction</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Operation1</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">In1</span>&nbsp;*&nbsp;(<span style="color:teal;">Out1</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a)) |&nbsp;<span style="color:navy;">Operation2</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">In2</span>&nbsp;*&nbsp;(<span style="color:teal;">Out2</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a))</pre> </p> <p> On the other hand, being extreme in the application of another <em>SOLID</em> principle, the <em>Segragation Principle Interface</em>, each operation may be splitted in as many different interfaces to be injected into the object. I think it doesn't change your recipe: putting all operations in the same instruction set / union type. What do you think of about it? </p> </div> <div class="comment-date">2019-10-11 20:48 UTC</div> </div> <div class="comment" id="37360879d2ff437fa4fbd481e0c5eb30"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#37360879d2ff437fa4fbd481e0c5eb30">#</a></div> <div class="comment-content"> <p> Romain, thank you for writing. In addition to <em>members</em>, we could call them <em>operations</em>, or <em>actions</em>. I chose <em>member</em> because it's established C# terminology when you're talking about the united set of methods, properties, and events defined by a type such as an interface. </p> <p> If you approach free monads from functional programming, we wouldn't call them <em>members</em>, but rather <em>functions</em>. </p> <p> I chose to start with the term <em>member</em> because I surmised that this would be the term with which most readers would be familiar. Since the article starts with those names, I chose to keep the same terms all the way through so that the reader would be able to follow the various steps in the recipe. </p> <p> With regards to the SOLID principles, the logical conclusion is to have lots of one-method interfaces. You can have one-function free monads as well, but combining them involves much plumbing work in F#. This is much easier in Haskell. </p> </div> <div class="comment-date">2019-10-12 23:20 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Combining free monads in F# https://blog.ploeh.dk/2017/07/31/combining-free-monads-in-f 2017-07-31T12:30:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of how to compose free monads in F#.</em> </p> <p> This article is an instalment in a <a href="/2017/07/10/pure-interactions">series of articles</a> about modelling long-running interactions with <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, functional code. In the <a href="/2017/07/24/combining-free-monads-in-haskell">previous article</a>, you saw how to combine a pure command-line API with an HTTP-client API in <a href="https://www.haskell.org">Haskell</a>. In this article, you'll see how to translate the Haskell proof of concept to F#. </p> <h3 id="265efdf00d9d469387c4675809de47af"> HTTP API client module <a href="#265efdf00d9d469387c4675809de47af" title="permalink">#</a> </h3> <p> You've already seen how to model command-line interactions as pure code in <a href="/2017/07/11/hello-pure-command-line-interaction">a previous article</a>. You can define interactions with the online restaurant reservation HTTP API in the same way. First, define some types required for input and output to the API: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Slot</span>&nbsp;=&nbsp;{&nbsp;Date&nbsp;:&nbsp;<span style="color:teal;">DateTimeOffset</span>;&nbsp;SeatsLeft&nbsp;:&nbsp;<span style="color:teal;">int</span>&nbsp;} <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Reservation</span>&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;:&nbsp;<span style="color:teal;">DateTimeOffset</span> &nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;:&nbsp;<span style="color:teal;">string</span> &nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;:&nbsp;<span style="color:teal;">string</span> &nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;:&nbsp;<span style="color:teal;">int</span>&nbsp;}</pre> </p> <p> The <code>Slot</code> type contains information about how many available seats are left on a particular date. The <code>Reservation</code> type contains the information required in order to make a reservation. It's the same <code>Reservation</code> F# record type you saw in <a href="/2017/07/17/a-pure-command-line-wizard">a previous article</a>, but now it's moved here. </p> <p> The online restaurant reservation HTTP API may afford more functionality than you need, but there's no reason to model more instructions than required: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">ReservationsApiInstruction</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">GetSlots</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">DateTimeOffset</span>&nbsp;*&nbsp;(<span style="color:teal;">Slot</span>&nbsp;<span style="color:teal;">list</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a)) |&nbsp;<span style="color:navy;">PostReservation</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">Reservation</span>&nbsp;*&nbsp;&#39;a</pre> </p> <p> This instruction set models two interactions. The <code>GetSlots</code> case models an instruction to request, from the HTTP API, the slots for a particular date. The <code>PostReservation</code> case models an instruction to make a POST HTTP request with a <code>Reservation</code>, thereby making a reservation. </p> <p> While Haskell can automatically make this type a <code>Functor</code>, in F# you have to write the code yourself: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;ReservationsApiInstruction&lt;&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;ReservationsApiInstruction&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">mapI</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">GetSlots</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">GetSlots</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">PostReservation</span>&nbsp;(x,&nbsp;next)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PostReservation</span>&nbsp;(x,&nbsp;next&nbsp;|&gt;&nbsp;<span style="color:navy;">f</span>)</pre> </p> <p> This turns <code>ReservationsApiInstruction&lt;'a&gt;</code> into a functor, which is, however, not the ultimate goal. The final objective is to enable syntactic sugar, so that you can write pure <code>ReservationsApiInstruction&lt;'a&gt;</code> Abstract Syntax Trees (ASTs) in standard F# syntax. In order to fulfil that ambition, you need a <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expression builder</a>, and to create one of those, you need a monad. </p> <p> You can turn <code>ReservationsApiInstruction&lt;'a&gt;</code> into a monad using <a href="/2017/08/07/f-free-monad-recipe">the free monad recipe</a> that you've <a href="/2017/07/11/hello-pure-command-line-interaction">already seen</a>. Creating a free monad, however, involves adding another type that will become both monad and functor, so I deliberately make <code>mapI</code> private in order to prevent confusion. This is also the reason I didn't name the function <code>map</code>: you'll need that name for a different type. The <em>I</em> in <code>mapI</code> stands for <em>instruction</em>. </p> <p> The <code>mapI</code> function pattern-matches on the (implicit) <code>ReservationsApiInstruction</code> argument. In the <code>GetSlots</code> case, it returns a new <code>GetSlots</code> value, but composes the <code>next</code> continuation with <code>f</code>. In the <code>PostReservation</code> case, it returns a new <code>PostReservation</code> value, but pipes <code>next</code> to <code>f</code>. The reason for the difference is that <code>PostReservation</code> is degenerate: <code>next</code> isn't a function, but a value. </p> <p> Now that <code>ReservationsApiInstruction&lt;'a&gt;</code> is a functor, you can create a free monad from it. The first step is to introduce a new type for the monad: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">ReservationsApiProgram</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Free</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">ReservationsApiInstruction</span>&lt;<span style="color:teal;">ReservationsApiProgram</span>&lt;&#39;a&gt;&gt; |&nbsp;<span style="color:navy;">Pure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> This is a recursive type that enables you to assemble ASTs that ultimately can return a value. The <code>Pure</code> case enables you to return a value, while the <code>Free</code> case lets you describe what should happen next. </p> <p> Using <code>mapI</code>, you can make a monad out of <code>ReservationsApiProgram&lt;'a&gt;</code> by adding a <code>bind</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;ReservationsApiProgram&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;ReservationsApiProgram&lt;&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;ReservationsApiProgram&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;bind&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;instruction&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;instruction&nbsp;|&gt;&nbsp;mapI&nbsp;(bind&nbsp;f)&nbsp;|&gt;&nbsp;Free &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Pure&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;x</pre> </p> <p> If you <a href="/2017/07/11/hello-pure-command-line-interaction">refer back to the <code>bind</code> implementation for <code>CommandLineProgram&lt;'a&gt;</code></a>, you'll see that <em>it's the exact same code</em>. In Haskell, creating a free monad from a functor is automatic. In F#, it's boilerplate. </p> <p> Likewise, you can make <code>ReservationsApiProgram&lt;'a&gt;</code> a functor: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;ReservationsApiProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;ReservationsApiProgram&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;=&nbsp;bind&nbsp;(f&nbsp;&gt;&gt;&nbsp;Pure)</pre> </p> <p> Again, this is the same code as in the <code>CommandLine</code> module. You can copy and paste it. It is, however, not the same function, because the types are different. </p> <p> Finally, to round off the reservations HTTP client API, you can supply functions that lift instructions to programs: </p> <p> <pre><span style="color:green;">//&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;ReservationsApiProgram&lt;Slot&nbsp;list&gt;</span> <span style="color:blue;">let</span>&nbsp;getSlots&nbsp;date&nbsp;=&nbsp;Free&nbsp;(GetSlots&nbsp;(date,&nbsp;Pure)) <span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;ReservationsApiProgram&lt;unit&gt;</span> <span style="color:blue;">let</span>&nbsp;postReservation&nbsp;r&nbsp;=&nbsp;Free&nbsp;(PostReservation&nbsp;(r,&nbsp;Pure&nbsp;()))</pre> </p> <p> That's everything you need to create a small computation expression builder: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">ReservationsApiBuilder</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Bind</span>&nbsp;(x,&nbsp;<span style="color:navy;">f</span>)&nbsp;=&nbsp;<span style="color:teal;">ReservationsApi</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Return</span>&nbsp;x&nbsp;=&nbsp;<span style="color:navy;">Pure</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">ReturnFrom</span>&nbsp;x&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Zero</span>&nbsp;()&nbsp;=&nbsp;<span style="color:navy;">Pure</span>&nbsp;()</pre> </p> <p> Create an instance of the <code>ReservationsApiBuilder</code> class in order to use <code>reservationsApi</code> computation expressions: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;reservationsApi&nbsp;=&nbsp;<span style="color:teal;">ReservationsApiBuilder</span>&nbsp;() </pre> </p> <p> This, in total, defines a pure API for interacting with the online restaurant reservation system, including all the syntactic sugar you'll need to stay sane. As usual, some boilerplate code is required, but I'm not too worried about its maintenance overhead, as it's unlikely to change much, once you've added it. If you've followed <a href="/2017/08/07/f-free-monad-recipe">the recipe</a>, the API obeys the category, functor, and monad laws, so it's not something you've invented; it's an instance of a universal abstraction. </p> <h3 id="13417c5caff944fcbeb2350ccb2bc7bb"> Monad stack <a href="#13417c5caff944fcbeb2350ccb2bc7bb" title="permalink">#</a> </h3> <p> The addition of the above <code>ReservationsApi</code> module is only a step towards the overall goal, which is to write a command-line wizard you can use to make reservations against the online API. In order to do so, you must combine the two monads <code>CommandLineProgram&lt;'a&gt;</code> and <code>ReservationsApiProgram&lt;'a&gt;</code>. In Haskell, you get that combination for free via the built-in generic <code>FreeT</code> type, which enables you to stack monads. In F#, you have to explicitly declare the type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CommandLineReservationsApiT</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Run</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">CommandLineProgram</span>&lt;<span style="color:teal;">ReservationsApiProgram</span>&lt;&#39;a&gt;&gt;</pre> </p> <p> This is a single-case discriminated union that stacks <code>ReservationsApiProgram</code> and <code>CommandLineProgram</code>. In this incarnation, it defines a single case called <code>Run</code>. The reason for this is that it enables you to follow the <a href="/2017/08/07/f-free-monad-recipe">free monad recipe</a> without having to do much thinking. Later, you'll see that it's possible to simplify the type. </p> <p> The naming is inspired by Haskell. This type is a piece of the puzzle corresponding to Haskell's <code>FreeT</code> type. The <em>T</em> in <code>FreeT</code> stands for <em>transformer</em>, because <code>FreeT</code> is actually something called a <em>monad transformer</em>. That's not terribly important in an F# context, but that's the reason I also tagged on the <em>T</em> in <code>CommandLineReservationsApiT&lt;'a&gt;</code>. </p> <p> <code>FreeT</code> is actually only a 'wrapper' around another monad. In order to extract the contained monad, you can use a function called <code>runFreeT</code>. That's the reason I called the F# case <code>Run</code>. </p> <p> You can easily make your stack of monads a functor: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;CommandLineProgram&lt;ReservationsApiProgram&lt;&#39;a&gt;&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineProgram&lt;ReservationsApiProgram&lt;&#39;b&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">mapStack</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">commandLine</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:teal;">ReservationsApi</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&#39;&nbsp;}</pre> </p> <p> The <code>mapStack</code> function uses the <code>commandLine</code> computation expression to access the <code>ReservationsApiProgram</code> contained within the <code>CommandLineProgram</code>. Thanks to the <code>let!</code> binding, <code>x'</code> is a <code>ReservationsApiProgram&lt;'a&gt;</code> value. You can use <code>ReservationsApi.map</code> to map <code>x'</code> with <code>f</code>. </p> <p> It's now trivial to make <code>CommandLineReservationsApiT&lt;'a&gt;</code> a functor as well: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;CommandLineReservationsApiT&lt;&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineReservationsApiT&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;mapT&nbsp;f&nbsp;(Run&nbsp;p)&nbsp;=&nbsp;mapStack&nbsp;f&nbsp;p&nbsp;|&gt;&nbsp;Run</pre> </p> <p> The <code>mapT</code> function simply pattern-matches the monad stack out of the <code>Run</code> case, calls <code>mapStack</code>, and pipes the return value into another <code>Run</code> case. </p> <p> By now, it's should be fairly clear that we're following the same recipe as before. You have a functor; make a monad out of it. First, define a type for the monad: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CommandLineReservationsApiProgram</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Free</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">CommandLineReservationsApiT</span>&lt;<span style="color:teal;">CommandLineReservationsApiProgram</span>&lt;&#39;a&gt;&gt; |&nbsp;<span style="color:navy;">Pure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> Then add a <code>bind</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;b&gt;)</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;bind&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;instruction&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;instruction&nbsp;|&gt;&nbsp;mapT&nbsp;(bind&nbsp;f)&nbsp;|&gt;&nbsp;Free &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Pure&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;x</pre> </p> <p> This is almost the same code as the above <code>bind</code> function for <code>ReservationsApi</code>. The only difference is that the underlying map function is named <code>mapT</code> instead of <code>mapI</code>. The types involved, however, are different. </p> <p> You can also add a <code>map</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(CommandLineReservationsApiProgram&lt;&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;b&gt;)</span> <span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;=&nbsp;bind&nbsp;(f&nbsp;&gt;&gt;&nbsp;Pure)</pre> </p> <p> This is another copy-and-paste job. Such repeatable. Wow. </p> <p> When you create a monad stack, you need a way to lift values from each of the constituent monads up to the combined monad. In Haskell, this is done with the <code>lift</code> and <code>liftF</code> functions, but in F#, you must explicitly add such functions: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;ReservationsApiProgram&lt;&#39;a&gt;&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;wrap&nbsp;x&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;Run&nbsp;|&gt;&nbsp;mapT&nbsp;Pure&nbsp;|&gt;&nbsp;Free <span style="color:green;">//&nbsp;CommandLineProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;liftCL&nbsp;x&nbsp;=&nbsp;wrap&nbsp;&lt;|&nbsp;CommandLine.map&nbsp;ReservationsApiProgram.Pure&nbsp;x <span style="color:green;">//&nbsp;ReservationsApiProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;liftRA&nbsp;x&nbsp;=&nbsp;wrap&nbsp;&lt;|&nbsp;CommandLineProgram.Pure&nbsp;x</pre> </p> <p> The private <code>wrap</code> function takes the underlying 'naked' monad stack (<code>CommandLineProgram&lt;ReservationsApiProgram&lt;'a&gt;&gt;</code>) and turns it into a <code>CommandLineReservationsApiProgram&lt;'a&gt;</code> value. It first wraps <code>x</code> in <code>Run</code>, which turns <code>x</code> into a <code>CommandLineReservationsApiT&lt;'a&gt;</code> value. By piping that value into <code>mapT Pure</code>, you get a <code>CommandLineReservationsApiT&lt;CommandLineReservationsApiProgram&lt;'a&gt;&gt;</code> value that you can finally pipe into <code>Free</code> in order to produce a <code>CommandLineReservationsApiProgram&lt;'a&gt;</code> value. Phew! </p> <p> The <code>liftCL</code> function lifts a <code>CommandLineProgram</code> (<em>CL</em>) to <code>CommandLineReservationsApiProgram</code> by first using <code>CommandLine.map</code> to lift <code>x</code> to a <code>CommandLineProgram&lt;ReservationsApiProgram&lt;'a&gt;&gt;</code> value. It then pipes that value to <code>wrap</code>. </p> <p> Likewise, the <code>liftRA</code> function lifts a <code>ReservationsApiProgram</code> (<em>RA</em>) to <code>CommandLineReservationsApiProgram</code>. It simply elevates <code>x</code> to a <code>CommandLineProgram</code> value by using <code>CommandLineProgram.Pure</code>. Subsequently, it pipes that value to <code>wrap</code>. </p> <p> In both of these functions, I used the slightly unusual backwards pipe operator <code>&lt;|</code>. The reason for that is that it emphasises the similarity between <code>liftCL</code> and <code>liftRA</code>. This is easier to see if you remove the type comments: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;liftCL&nbsp;x&nbsp;=&nbsp;wrap&nbsp;&lt;|&nbsp;CommandLine.map&nbsp;ReservationsApiProgram.Pure&nbsp;x <span style="color:blue;">let</span>&nbsp;liftRA&nbsp;x&nbsp;=&nbsp;wrap&nbsp;&lt;|&nbsp;CommandLineProgram.Pure&nbsp;x</pre> </p> <p> This is how I normally write my F# code. I only add the type comments for the benefit of you, dear reader. Normally, when you have an IDE, you can always inspect the types using the built-in tools. </p> <p> Using the backwards pipe operator makes it immediately clear that both functions depend in the <code>wrap</code> function. This would have been muddied by use of the normal forward pipe operator: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;liftCL&nbsp;x&nbsp;=&nbsp;CommandLine.map&nbsp;ReservationsApiProgram.Pure&nbsp;x&nbsp;|&gt;&nbsp;wrap <span style="color:blue;">let</span>&nbsp;liftRA&nbsp;x&nbsp;=&nbsp;CommandLineProgram.Pure&nbsp;x&nbsp;|&gt;&nbsp;wrap</pre> </p> <p> The behaviour is the same, but now <code>wrap</code> doesn't align, making it harder to discover the kinship between the two functions. My use of the backward pipe operator is motivated by readability concerns. </p> <p> Following the <a href="/2017/08/07/f-free-monad-recipe">free monad recipe</a>, now create a computation expression builder: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CommandLineReservationsApiBuilder</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Bind</span>&nbsp;(x,&nbsp;<span style="color:navy;">f</span>)&nbsp;=&nbsp;<span style="color:teal;">CommandLineReservationsApi</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Return</span>&nbsp;x&nbsp;=&nbsp;<span style="color:navy;">Pure</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">ReturnFrom</span>&nbsp;x&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Zero</span>&nbsp;()&nbsp;=&nbsp;<span style="color:navy;">Pure</span>&nbsp;()</pre> </p> <p> Finally, create an instance of the class: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;commandLineReservationsApi&nbsp;=&nbsp;<span style="color:teal;">CommandLineReservationsApiBuilder</span>&nbsp;() </pre> </p> <p> Putting the <code>commandLineReservationsApi</code> value in a module will enable you to use it for computation expressions whenever you open that module. I normally put it in a module with the <code>[&lt;AutoOpen&gt;]</code> attribute so that it automatically becomes available as soon as I open the containing namespace. </p> <h3 id="64a04c8e37d843089cc7cb160d474c92"> Simplification <a href="#64a04c8e37d843089cc7cb160d474c92" title="permalink">#</a> </h3> <p> While there <em>can</em> be good reasons to introduce single-case discriminated unions in your F# code, they're isomorphic with their contained type. (This means that there's a lossless conversion between the union type and the contained type, in both directions.) Following the <a href="/2017/08/07/f-free-monad-recipe">free monad recipe</a>, I introduced <code>CommandLineReservationsApiT</code> as a discriminated union, but since it's a single-case union, you can refactor it to its contained type. </p> <p> If you delete the <code>CommandLineReservationsApiT</code> type, you'll first have to change the definition of the program type to this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CommandLineReservationsApiProgram</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Free</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">CommandLineProgram</span>&lt;<span style="color:teal;">ReservationsApiProgram</span>&lt;<span style="color:teal;">CommandLineReservationsApiProgram</span>&lt;&#39;a&gt;&gt;&gt; |&nbsp;<span style="color:navy;">Pure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> You simply replace <code>CommandLineReservationsApiT&lt;_&gt;</code> with <code>CommandLineProgram&lt;ReservationsApiProgram&lt;_&gt;&gt;</code>, effectively promoting the type contained in the <code>Run</code> case to be the container in the <code>Free</code> case. </p> <p> Once <code>CommandLineReservationsApiT</code> is gone, you'll also need to delete the <code>mapT</code> function, and amend <code>bind</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;b&gt;)</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineReservationsApiProgram&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;bind&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;instruction&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;instruction&nbsp;|&gt;&nbsp;mapStack&nbsp;(bind&nbsp;f)&nbsp;|&gt;&nbsp;Free &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Pure&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;x</pre> </p> <p> Likewise, you must also adjust the <code>wrap</code> function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">wrap</span>&nbsp;x&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:navy;">mapStack</span>&nbsp;<span style="color:navy;">Pure</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">Free</span> </pre> </p> <p> The rest of the above code stays the same. </p> <h3 id="744ef6bccb1f4fcbac5bc508387f92ba"> Wizard <a href="#744ef6bccb1f4fcbac5bc508387f92ba" title="permalink">#</a> </h3> <p> In Haskell, you get combinations of monads for free via the <code>FreeT</code> type, whereas in F#, you have to work for it. Once you have the combination in monadic form as well, you can write programs with that combination. Here's the wizard that collects your data and attempts to make a restaurant reservation on your behalf: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineReservationsApiProgram&lt;unit&gt;</span> <span style="color:blue;">let</span>&nbsp;tryReserve&nbsp;=&nbsp;commandLineReservationsApi&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;count&nbsp;=&nbsp;<span style="color:navy;">liftCL</span>&nbsp;readQuantity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;date&nbsp;&nbsp;=&nbsp;<span style="color:navy;">liftCL</span>&nbsp;readDate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;availableSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">ReservationsApi</span>.<span style="color:navy;">getSlots</span>&nbsp;date &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">ReservationsApi</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:teal;">List</span>.<span style="color:navy;">sumBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;slot&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;slot.SeatsLeft)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">liftRA</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;availableSeats&nbsp;&lt;&nbsp;count &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:blue;">do!</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;Only&nbsp;</span><span style="color:teal;">%i</span><span style="color:#a31515;">&nbsp;remaining&nbsp;seats.&quot;</span>&nbsp;availableSeats &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">writeLine</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">liftCL</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;name&nbsp;&nbsp;=&nbsp;<span style="color:navy;">liftCL</span>&nbsp;readName &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;email&nbsp;=&nbsp;<span style="color:navy;">liftCL</span>&nbsp;readEmail &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;{&nbsp;Date&nbsp;=&nbsp;date;&nbsp;Name&nbsp;=&nbsp;name;&nbsp;Email&nbsp;=&nbsp;email;&nbsp;Quantity&nbsp;=&nbsp;count&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">ReservationsApi</span>.<span style="color:navy;">postReservation</span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">liftRA</span> &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Notice that <code>tryReserve</code> is a <em>value</em>, and not a function. It's a pure value that contains an AST - a small program that describes the impure interactions that you'd like to take place. It's defined entirely within a <code>commandLineReservationsApi</code> computation expression. </p> <p> It starts by using the <code>readQuantity</code> and <code>readDate</code> program values you saw in the <a href="/2017/07/17/a-pure-command-line-wizard">previous F# article</a>. Both of these values are <code>CommandLineProgram</code> values, so you have to use <code>liftCL</code> to lift them to <code>CommandLineReservationsApiProgram</code> values - only then can you <code>let!</code> bind them to an <code>int</code> and a <code>DateTimeOffset</code>, respectively. This is just like the use of <code>lift</code> in the previous article's Haskell example. </p> <p> Once the program has collected the desired <code>date</code> from the user, it calls <code>ReservationsApi.getSlots</code> and calculates the sum over all the returned <code>SeatsLeft</code> labels. The <code>ReservationsApi.getSlots</code> function returns a <code>ReservationsApiProgram&lt;Slot list&gt;</code>, the <code>ReservationsApi.map</code> turns it into a <code>ReservationsApiProgram&lt;int&gt;</code> value that you must <code>liftRA</code> in order to be able to <code>let!</code> bind it to an <code>int</code> value. Let me stress once again: the program actually doesn't <em>do</em> any of that; it constructs an AST with instructions to that effect. </p> <p> If it turns out that there's too few seats left, the program writes that on the command line and exits. Otherwise, it continues to collect the user's name and email address. That's all the data required to create a <code>Reservation</code> record and pipe it to <code>ReservationsApi.postReservation</code>. </p> <h3 id="82fca50be3654150970a6cc8ee5e673f"> Interpreters <a href="#82fca50be3654150970a6cc8ee5e673f" title="permalink">#</a> </h3> <p> The <code>tryReserve</code> wizard is a pure value. It contains an AST that can be interpreted in such a way that impure operations happen. You've already seen the <code>CommandLineProgram</code> interpreter <a href="/2017/07/11/hello-pure-command-line-interaction">in a previous article</a>, so I'm not going to repeat it here. I'll only note that I renamed it to <code>interpretCommandLine</code> because I want to use the name <code>interpret</code> for the combined interpreter. </p> <p> The interpreter for <code>ReservationsApiProgram</code> values is similar to the <code>CommandLineProgram</code> interpreter: </p> <p> <pre><span style="color:green;">//&nbsp;ReservationsApiProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">interpretReservationsApi</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:teal;">ReservationsApiProgram</span>.<span style="color:navy;">Pure</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:teal;">ReservationsApiProgram</span>.<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">GetSlots</span>&nbsp;(d,&nbsp;<span style="color:navy;">next</span>))&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">ReservationHttpClient</span>.<span style="color:navy;">getSlots</span>&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Async</span>.<span style="color:navy;">RunSynchronously</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">next</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">interpretReservationsApi</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:teal;">ReservationsApiProgram</span>.<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">PostReservation</span>&nbsp;(r,&nbsp;next))&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">ReservationHttpClient</span>.<span style="color:navy;">postReservation</span>&nbsp;r&nbsp;|&gt;&nbsp;<span style="color:teal;">Async</span>.<span style="color:navy;">RunSynchronously</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;|&gt;&nbsp;<span style="color:navy;">interpretReservationsApi</span></pre> </p> <p> The <code>interpretReservationsApi</code> function pattern-matches on its (implicit) <code>ReservationsApiProgram</code> argument, and performs the appropriate actions according to each instruction. In all <code>Free</code> cases, it delegates to implementations defined in a <code>ReservationHttpClient</code> module. The code in that module isn't shown here, but you can see it in <a href="https://github.com/ploeh/PureInteractionsInFSharp">the GitHub repository that accompanies this article</a>. </p> <p> You can combine the two 'leaf' interpreters in an interpreter of <code>CommandLineReservationsApiProgram</code> values: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineReservationsApiProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">interpret</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:teal;">CommandLineReservationsApiProgram</span>.<span style="color:navy;">Pure</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:teal;">CommandLineReservationsApiProgram</span>.<span style="color:navy;">Free</span>&nbsp;p&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;|&gt;&nbsp;<span style="color:navy;">interpretCommandLine</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">interpretReservationsApi</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span></pre> </p> <p> As usual, in the <code>Pure</code> case, it simply returns the contained value. In the <code>Free</code> case, <code>p</code> is a <code>CommandLineProgram&lt;ReservationsApiProgram&lt;CommandLineReservationsApiProgram&lt;'a&gt;&gt;&gt;</code>. Since it's a <code>CommandLineProgram</code> value, you can interpret it with <code>interpretCommandLine</code>, which returns a <code>ReservationsApiProgram&lt;CommandLineReservationsApiProgram&lt;'a&gt;&gt;</code>. Since that's a <code>ReservationsApiProgram</code>, you can pipe it to <code>interpretReservationsApi</code>, which then returns a <code>CommandLineReservationsApiProgram&lt;'a&gt;</code>. An interpreter exists for that type as well, namely the <code>interpret</code> function itself, so recursively invoke it again. In other words, <code>interpret</code> will keep recursing until it hits a <code>Pure</code> case. </p> <h3 id="669fc51c47324f6899ac9b70d89c4d0f"> Execution <a href="#669fc51c47324f6899ac9b70d89c4d0f" title="permalink">#</a> </h3> <p> Everything is now in place so that you can execute your program. This is the program's entry point: </p> <p> <pre>[&lt;<span style="color:teal;">EntryPoint</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">main</span>&nbsp;_&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">interpret</span>&nbsp;<span style="color:teal;">Wizard</span>.tryReserve &nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;<span style="color:green;">//&nbsp;return&nbsp;an&nbsp;integer&nbsp;exit&nbsp;code</span></pre> </p> <p> When you run it, you'll be able to have an interaction like this: </p> <p> <pre>Please enter number of diners: 4 Please enter your desired date: 2017-11-25 Please enter your name: Mark Seemann Please enter your email address: mark@example.net OK</pre> </p> <p> If you want to run this code sample yourself, you're going to need an appropriate HTTP API with which you can interact. I hosted the API on my local machine, and afterwards verified that the record was, indeed, written in the reservations database. </p> <h3 id="c17d576209c745729d5b1f20f3f908f1"> Summary <a href="#c17d576209c745729d5b1f20f3f908f1" title="permalink">#</a> </h3> <p> As expected, you can combine free monads in F#, although it requires more boilerplate code than in Haskell. </p> <p> <strong>Next:</strong> <a href="/2017/08/07/f-free-monad-recipe">F# free monad recipe</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Combining free monads in Haskell https://blog.ploeh.dk/2017/07/24/combining-free-monads-in-haskell 2017-07-24T15:33:00+00:00 Mark Seemann <div id="post"> <p> <em>An example on how to compose free monads in Haskell.</em> </p> <p> In the <a href="/2017/07/17/a-pure-command-line-wizard">previous article</a> in this <a href="/2017/07/10/pure-interactions">series on pure interactions</a>, you saw how to write a command-line wizard in F#, using a free monad to build an Abstract Syntax Tree (AST). The example collects information about a potential restaurant reservations you'd like to make. That example, however, didn't do more than that. </p> <p> For a more complete experience, you'd like your command-line interface (CLI) to not only collect data about a reservation, but actually <em>make</em> the reservation, using the available HTTP API. This means that you'll also need to model interaction with the HTTP API as an AST, but a different AST. Then, you'll have to figure out how to compose these two APIs into a combined API. </p> <p> In order to figure out how to do this in F#, I first had to do it in <a href="https://www.haskell.org">Haskell</a>. In this article, you'll see how to do it in Haskell, and in the next article, you'll see how to translate this Haskell prototype to F#. This should ensure that you get a functional F# code base as well. </p> <h3 id="94c9e80804f349efae9a5fb36f785284"> Command line API <a href="#94c9e80804f349efae9a5fb36f785284" title="permalink">#</a> </h3> <p> Let's make an easy start of it. In a previous article, you saw <a href="/2017/07/11/hello-pure-command-line-interaction">how to model command-line interactions as ASTs</a>, complete with syntactic sugar provided by a <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expression</a>. That took a fair amount of boilerplate code in F#, but in Haskell, it's declarative: </p> <p> <pre><span style="color:blue;">import</span>&nbsp;Control.Monad.Trans.Free&nbsp;(<span style="color:blue;">Free</span>,&nbsp;<span style="color:#600277;">liftF</span>) <span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">CommandLineInstruction</span>&nbsp;next&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">ReadLine</span>&nbsp;(<span style="color:#dd0000;">String</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;next) &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">WriteLine</span>&nbsp;<span style="color:#dd0000;">String</span>&nbsp;next &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Functor</span>) <span style="color:blue;">type</span>&nbsp;<span style="color:#dd0000;">CommandLineProgram</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;<span style="color:#dd0000;">CommandLineInstruction</span> <span style="color:#600277;">readLine</span>&nbsp;::&nbsp;<span style="color:blue;">CommandLineProgram</span>&nbsp;String readLine&nbsp;<span style="color:#666666;">=</span>&nbsp;liftF&nbsp;(<span style="color:#dd0000;">ReadLine</span>&nbsp;id) <span style="color:#600277;">writeLine</span>&nbsp;::&nbsp;String&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">CommandLineProgram</span>&nbsp;() writeLine&nbsp;s&nbsp;<span style="color:#666666;">=</span>&nbsp;liftF&nbsp;(<span style="color:#dd0000;">WriteLine</span>&nbsp;s&nbsp;<span style="color:blue;">()</span>)</pre> </p> <p> This is <em>all</em> the code required to define your AST and make it a monad in Haskell. Contrast that with <a href="/2017/07/11/hello-pure-command-line-interaction">all the code you have to write in F#</a>! </p> <p> The <code>CommandLineInstruction</code> type defines the instruction set, and makes use of a language extension called <code>DeriveFunctor</code>, which enables Haskell to automatically create a <code>Functor</code> instance from the type. </p> <p> The type alias <code>type CommandLineProgram = Free CommandLineInstruction</code> creates a monad from <code>CommandLineInstruction</code>, since <code>Free</code> is a <code>Monad</code> when the underlying type is a <code>Functor</code>. </p> <p> The <code>readLine</code> value and <code>writeLine</code> function are conveniences that lift the instructions from <code>CommandLineInstruction</code> into <code>CommandLineProgram</code> values. These were also one-liners in F#. </p> <h3 id="58b38b5dcb4e499b8cb0ff32c1ba57a3"> HTTP client API <a href="#58b38b5dcb4e499b8cb0ff32c1ba57a3" title="permalink">#</a> </h3> <p> You can write a small wizard to collect restaurant reservation data with the <code>CommandLineProgram</code> API, but the new requirement is to make HTTP calls so that the CLI program actually makes the reservation against the back-end system. You could extend <code>CommandLineProgram</code> with more instructions, but that would be to mix concerns. It'd be more appropriate to define a new instruction set for making the required HTTP requests. </p> <p> This API will send and receive more complex values than simple <code>String</code> values, so you can start by defining their types: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Slot</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Slot</span>&nbsp;{&nbsp;slotDate&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">ZonedTime</span>,&nbsp;seatsLeft&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>) <span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;{&nbsp;reservationDate&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">ZonedTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;reservationName&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">String</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;reservationEmail&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">String</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;reservationQuantity&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Int</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>)</pre> </p> <p> The <code>Slot</code> type contains information about how many available seats are left on a particular date. The <code>Reservation</code> type contains the information required in order to make a reservation. It's similar to the <code>Reservation</code> F# record type you saw in the previous article. </p> <p> The online restaurant reservation HTTP API may afford more functionality than you need, but there's no reason to model more instructions than required: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">ReservationsApiInstruction</span>&nbsp;next&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">GetSlots</span>&nbsp;<span style="color:#dd0000;">ZonedTime</span>&nbsp;([<span style="color:#dd0000;">Slot</span>]&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;next) &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">PostReservation</span>&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;next &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Functor</span>)</pre> </p> <p> This instruction set models two interactions. The <code>GetSlots</code> case models an instruction to request, from the HTTP API, the slots for a particular date. The <code>PostReservation</code> case models an instruction to make a POST HTTP request with a <code>Reservation</code>, thereby making a reservation. </p> <p> Like the above <code>CommandLineInstruction</code>, this type is (automatically) a <code>Functor</code>, which means that we can create a <code>Monad</code> from it: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#dd0000;">ReservationsApiProgram</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;<span style="color:#dd0000;">ReservationsApiInstruction</span> </pre> </p> <p> Once again, the monad is nothing but a type alias. </p> <p> Finally, you're going to need the usual lifts: </p> <p> <pre><span style="color:#600277;">getSlots</span>&nbsp;::&nbsp;<span style="color:blue;">ZonedTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsApiProgram</span>&nbsp;[<span style="color:blue;">Slot</span>] getSlots&nbsp;d&nbsp;<span style="color:#666666;">=</span>&nbsp;liftF&nbsp;(<span style="color:#dd0000;">GetSlots</span>&nbsp;d&nbsp;id) <span style="color:#600277;">postReservation</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ReservationsApiProgram</span>&nbsp;() postReservation&nbsp;r&nbsp;<span style="color:#666666;">=</span>&nbsp;liftF&nbsp;(<span style="color:#dd0000;">PostReservation</span>&nbsp;r&nbsp;<span style="color:blue;">()</span>)</pre> </p> <p> This is all you need to write a wizard that interleaves <code>CommandLineProgram</code> and <code>ReservationsApiProgram</code> instructions in order to create a more complex AST. </p> <h3 id="04fde53d3a384ea18904fc4bdb211cfb"> Wizard <a href="#04fde53d3a384ea18904fc4bdb211cfb" title="permalink">#</a> </h3> <p> The wizard should do the following: <ul> <li>Collect the number of diners, and the date for the reservation.</li> <li>Query the HTTP API about availability for the requested date. If insufficient seats are available, it should exit.</li> <li>If sufficient capacity remains, collect name and email.</li> <li>Make the reservation against the HTTP API.</li> </ul> Like in the previous F# examples, you can factor some of the work that the wizard performs into helper functions. The first is one that prompts the user for a value and tries to parse it: </p> <p> <pre><span style="color:#600277;">readParse</span>&nbsp;::&nbsp;<span style="color:blue;">Read</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;String&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;String&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">CommandLineProgram</span>&nbsp;a readParse&nbsp;prompt&nbsp;errorMessage&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;writeLine&nbsp;prompt &nbsp;&nbsp;l&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;readLine &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;readMaybe&nbsp;l&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;dt&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;return&nbsp;dt &nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeLine&nbsp;errorMessage &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readParse&nbsp;prompt&nbsp;errorMessage</pre> </p> <p> It first uses <code>writeLine</code> to write <code>prompt</code> to the command line - or rather, it creates an <em>instruction</em> to do so. The instruction is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> value. No side-effects are involved until an interpreter evaluates the AST. </p> <p> The next line uses <code>readLine</code> to read the user's input. While <code>readLine</code> is a <code>CommandLineProgram String</code> value, due to Haskell's <code>do</code> notation, <code>l</code> is a <code>String</code> value. You can now attempt to parse that <code>String</code> value with <code>readMaybe</code>, which returns a <code>Maybe a</code> value that you can handle with pattern matching. If <code>readMaybe</code> returns a <code>Just</code> value, then return the contained value; otherwise, write <code>errorMessage</code> and recursively call <code>readParse</code> again. </p> <p> Like in the previous F# example, the only way to continue is to write something that <code>readMaybe</code> can parse. There's no other way to exit; there probably should be an option to quit, but it's not important for this demo purpose. </p> <p> You may also have noticed that, contrary to the previous F# example, I here succumbed to the temptation to break the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">rule of three</a>. It's easier to define a reusable function in Haskell, because you can leave it generic, with the proviso that the generic value must be an instance of the <code>Read</code> typeclass. </p> <p> The <code>readParse</code> function returns a <code>CommandLineProgram a</code> value. It doesn't combine <code>CommandLineProgram</code> with <code>ReservationsApiProgram</code>. That's going to happen in another function, but before we look at that, you're also going to need another little helper: </p> <p> <pre><span style="color:#600277;">readAnything</span>&nbsp;::&nbsp;String&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">CommandLineProgram</span>&nbsp;String readAnything&nbsp;prompt&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;writeLine&nbsp;prompt &nbsp;&nbsp;readLine</pre> </p> <p> The <code>readAnything</code> function simply writes a prompt, reads the user's input, and unconditionally returns it. You could also have written it as a one-liner like <code>readAnything prompt = writeLine prompt &gt;&gt; readLine</code>, but I find the above code more readable, even though it's slightly more verbose. </p> <p> That's all you need to write the wizard: </p> <p> <pre><span style="color:#600277;">tryReserve</span>&nbsp;::&nbsp;<span style="color:blue;">FreeT</span>&nbsp;<span style="color:blue;">ReservationsApiProgram</span>&nbsp;<span style="color:blue;">CommandLineProgram</span>&nbsp;() tryReserve&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;q&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;lift&nbsp;<span style="color:#666666;">$</span>&nbsp;readParse&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;number&nbsp;of&nbsp;diners:&quot;</span>&nbsp;<span style="color:#a31515;">&quot;Not&nbsp;an&nbsp;Integer.&quot;</span> &nbsp;&nbsp;d&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;lift&nbsp;<span style="color:#666666;">$</span>&nbsp;readParse&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;your&nbsp;desired&nbsp;date:&quot;</span>&nbsp;<span style="color:#a31515;">&quot;Not&nbsp;a&nbsp;date.&quot;</span> &nbsp;&nbsp;availableSeats&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;liftF&nbsp;<span style="color:#666666;">$</span>&nbsp;(sum&nbsp;<span style="color:#666666;">.</span>&nbsp;fmap&nbsp;seatsLeft)&nbsp;<span style="color:#666666;">&lt;$&gt;</span>&nbsp;getSlots&nbsp;d &nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;availableSeats&nbsp;<span style="color:#666666;">&lt;</span>&nbsp;q &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;lift&nbsp;<span style="color:#666666;">$</span>&nbsp;writeLine&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#a31515;">&quot;Only&nbsp;&quot;</span>&nbsp;<span style="color:#666666;">++</span>&nbsp;show&nbsp;availableSeats&nbsp;<span style="color:#666666;">++</span>&nbsp;<span style="color:#a31515;">&quot;&nbsp;remaining&nbsp;seats.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;lift&nbsp;<span style="color:#666666;">$</span>&nbsp;readAnything&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;your&nbsp;name:&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;lift&nbsp;<span style="color:#666666;">$</span>&nbsp;readAnything&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;your&nbsp;email&nbsp;address:&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liftF&nbsp;<span style="color:#666666;">$</span>&nbsp;postReservation&nbsp;<span style="color:#dd0000;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;reservationDate&nbsp;<span style="color:#666666;">=</span>&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;reservationName&nbsp;<span style="color:#666666;">=</span>&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;reservationEmail&nbsp;<span style="color:#666666;">=</span>&nbsp;e &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;reservationQuantity&nbsp;<span style="color:#666666;">=</span>&nbsp;q&nbsp;}</pre> </p> <p> The <code>tryReserve</code> program first prompt the user for a number of diners and a date. Once it has the date <code>d</code>, it calls <code>getSlots</code> and calculates the sum of the remaining seats. <code>availableSeats</code> is an <code>Int</code> value like <code>q</code>, so you can compare those two values with each other. If the number of available seats is less than the desired quantity, the program writes that and exits. </p> <p> This interaction demonstrates how to interleave <code>CommandLineProgram</code> and <code>ReservationsApiProgram</code> instructions. It would be a bad user experience if the program would ask the user to input all information, and only then discover that there's insufficient capacity. </p> <p> If, on the other hand, there's enough remaining capacity, the program continues collecting information from the user, by prompting for the user's name and email address. Once all data is collected, it creates a new <code>Reservation</code> value and invokes <code>postReservation</code>. </p> <p> Consider the type of <code>tryReserve</code>. It's a combination of <code>CommandLineProgram</code> and <code>ReservationsApiProgram</code>, contained within a type called <code>FreeT</code>. This type is also a <code>Monad</code>, which is the reason the <code>do</code> notation still works. This also begins to explain the various <code>lift</code> and <code>liftF</code> calls sprinkled over the code. </p> <p> Whenever you use a <code>&lt;-</code> arrow to 'pull the value out of the monad' within a <code>do</code> block, the right-hand side of the arrow must have the same type as the return type of the overall function (or value). In this case, the return type is <code>FreeT ReservationsApiProgram CommandLineProgram ()</code>, whereas <code>readParse</code> returns a <code>CommandLineProgram a</code> value. As an example, <code>lift</code> turns <code>CommandLineProgram Int</code> into <code>FreeT ReservationsApiProgram CommandLineProgram Int</code>. </p> <p> The way the type of <code>tryReserve</code> is declared, when you have a <code>CommandLineProgram a</code> value, you use <code>lift</code>, but when you have a <code>ReservationsApiProgram a</code>, you use <code>liftF</code>. This depends on the order of the monads contained within <code>FreeT</code>. If you swap <code>CommandLineProgram</code> and <code>ReservationsApiProgram</code>, you'll also need to use <code>lift</code> instead of <code>liftF</code>, and vice versa. </p> <h3 id="9305079d6b0e4a659a2092638d88d040"> Interpreters <a href="#9305079d6b0e4a659a2092638d88d040" title="permalink">#</a> </h3> <p> <code>tryReserve</code> is a pure value. It's an Abstract Syntax Tree that combines two separate instruction sets to describe a complex interaction between user, command line, and an HTTP client. The program doesn't do anything until interpreted. </p> <p> You can write an impure interpreter for each of the APIs, and a third one that uses the other two to interpret <code>tryReserve</code>. </p> <p> Interpreting <code>CommandLineProgram</code> values is similar to the previous F# example: </p> <p> <pre><span style="color:#600277;">interpretCommandLine</span>&nbsp;::&nbsp;<span style="color:blue;">CommandLineProgram</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;a interpretCommandLine&nbsp;program&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;runFree&nbsp;program&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Pure</span>&nbsp;r&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;return&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">ReadLine</span>&nbsp;next)&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;getLine &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interpretCommandLine&nbsp;<span style="color:#666666;">$</span>&nbsp;next&nbsp;line &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">WriteLine</span>&nbsp;line&nbsp;next)&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putStrLn&nbsp;line &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interpretCommandLine&nbsp;next</pre> </p> <p> This interpreter is a recursive function that pattern-matches all the cases in any <code>CommandLineProgram a</code>. When it encounters a <code>Pure</code> case, it simply returns the contained value. </p> <p> When it encounters a <code>ReadLine</code> value, it calls <code>getLine</code>, which returns an <code>IO String</code> value read from the command line, but thanks to the <code>do</code> block, <code>line</code> is a <code>String</code> value. The interpreter then calls <code>next</code> with <code>line</code>, and passes the return value of that recursively to itself. </p> <p> A similar treatment is given to the <code>WriteLine</code> case. <code>putStrLn line</code> writes <code>line</code> to the command line, where after <code>next</code> is used as an input argument to <code>interpretCommandLine</code>. </p> <p> Thanks to Haskell's type system, you can easily tell that <code>interpretCommandLine</code> is impure, because for every <code>CommandLineProgram a</code> it returns <code>IO a</code>. That was the intent all along. </p> <p> Likewise, you can write an interpreter for <code>ReservationsApiProgram</code> values: </p> <p> <pre><span style="color:#600277;">interpretReservationsApi</span>&nbsp;::&nbsp;<span style="color:blue;">ReservationsApiProgram</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;a interpretReservationsApi&nbsp;program&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;runFree&nbsp;program&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Pure</span>&nbsp;x&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;return&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">GetSlots</span>&nbsp;zt&nbsp;next)&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slots&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;<span style="color:#dd0000;">HttpClient</span><span style="color:#666666;">.</span>getSlots&nbsp;zt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interpretReservationsApi&nbsp;<span style="color:#666666;">$</span>&nbsp;next&nbsp;slots &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">PostReservation</span>&nbsp;r&nbsp;next)&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">HttpClient</span><span style="color:#666666;">.</span>postReservation&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interpretReservationsApi&nbsp;next</pre> </p> <p> The structure of <code>interpretReservationsApi</code> is similar to <code>interpretCommandLine</code>. It delegates its implementation to an <code>HttpClient</code> module that contains the impure interactions with the HTTP API. This module isn't shown in this article, but you can see it in <a href="https://github.com/ploeh/PureInteractionsInHaskell">the GitHub repository that accompanies this article</a>. </p> <p> From these two interpreters, you can create a combined interpreter: </p> <p> <pre><span style="color:#600277;">interpret</span>&nbsp;::&nbsp;<span style="color:blue;">FreeT</span>&nbsp;<span style="color:blue;">ReservationsApiProgram</span>&nbsp;<span style="color:blue;">CommandLineProgram</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;a interpret&nbsp;program&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;r&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;interpretCommandLine&nbsp;<span style="color:#666666;">$</span>&nbsp;runFreeT&nbsp;program &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;r&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Pure</span>&nbsp;x&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;return&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;p&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;interpretReservationsApi&nbsp;p &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interpret&nbsp;y</pre> </p> <p> This function has the required type: it evaluates any <code>FreeT ReservationsApiProgram CommandLineProgram a</code> and returns an <code>IO a</code>. <code>runFreeT</code> returns the <code>CommandLineProgram</code> part of the combined program. Passing this value to <code>interpretCommandLine</code>, you get the underlying type - the <code>a</code> in <code>CommandLineProgram a</code>, if you will. In this case, however, the <code>a</code> is quite a complex type that I'm not going to write out here. Suffice it to say that, at the container level, it's a <code>FreeF</code> value, which can be either a <code>Pure</code> or a <code>Free</code> case that you can use for pattern matching. </p> <p> In the <code>Pure</code> case, you're done, so you can simply return the underlying value. </p> <p> In the <code>Free</code> case, the <code>p</code> contained inside is a <code>ReservationsApiProgram</code> value, which you can interpret with <code>interpretReservationsApi</code>. That returns an <code>IO a</code> value, and due to the <code>do</code> block, <code>y</code> is the <code>a</code>. In this case, however, <code>a</code> is <code>FreeT ReservationsApiProgram CommandLineProgram a</code>, but that means that the function can now recursively call itself with <code>y</code> in order to interpret the next instruction. </p> <h3 id="510fe91857ae44d5b696e658af124afd"> Execution <a href="#510fe91857ae44d5b696e658af124afd" title="permalink">#</a> </h3> <p> Armed with both an AST and an interpreter, executing the program is trivial: </p> <p> <pre><span style="color:#600277;">main</span>&nbsp;::&nbsp;IO&nbsp;() main&nbsp;<span style="color:#666666;">=</span>&nbsp;interpret&nbsp;tryReserve</pre> </p> <p> When you run the program, you could produce an interaction like this: </p> <p> <pre>Please enter number of diners: 4 Please enter your desired date: 2017-11-25 18-30-00Z Not a date. Please enter your desired date: 2017-11-25 18:30:00Z Please enter your name: Mark Seemann Please enter your email address: mark@example.org Status {statusCode = 200, statusMessage = "OK"}</pre> </p> <p> You'll notice that I initially made a mistake on the date format, which caused <code>readParse</code> to prompt me again. </p> <p> If you want to run this code sample yourself, you're going to need an appropriate HTTP API with which you can interact. I hosted the API on my local machine, and afterwards verified that the record was, indeed, written in the reservations database. </p> <h3 id="f16d309bd5ad406fa356ca191b38a00f"> Summary <a href="#f16d309bd5ad406fa356ca191b38a00f" title="permalink">#</a> </h3> <p> This proof of concept proves that it's possible to combine separate free monads. Now that we know that it works, and the overall outline of it, it should be possible to translate this to F#. You should, however, expect more boilerplate code. </p> <p> <strong>Next:</strong> <a href="/2017/07/31/combining-free-monads-in-f">Combining free monads in F#</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="170646d461f94410b582e0ce0ce02801"> <div class="comment-author"><a href="https://twitter.com/porges">George Pollard</a> <a href="#170646d461f94410b582e0ce0ce02801">#</a></div> <div class="comment-content"> <p>Here's an additional simplification. Rather than writing <code>FreeT ReservationsApiProgram CommandLineProgram</code> which requires you to lift, you can instead form the sum (coproduct) of both functors:</p> <pre> import Data.Functor.Sum type Program = Free (Sum CommandLineInstruction ReservationsApiInstruction) liftCommandLine :: CommandLineInstruction a -> Program a liftCommandLine = liftF . InL liftReservation :: ReservationsApiInstruction a -> Program a liftReservation = liftF . InR </pre> <p>Now you can lift the helpers directly to <code>Program</code>, like so:</p> <pre> readLine :: Program String readLine = liftCommandLine (ReadLine id) writeLine :: String -> Program () writeLine s = liftCommandLine (WriteLine s ()) getSlots :: ZonedTime -> Program [Slot] getSlots d = liftReservation (GetSlots d id) postReservation :: Reservation -> Program () postReservation r = liftReservation (PostReservation r ()) </pre> <p>Then (after you change the types of the <code>read*</code> helpers), you can drop all <code>lift</code>s from <code>tryReserve</code>:</p> <pre> tryReserve :: Program () tryReserve = do q &lt;- readParse "Please enter number of diners:" "Not an Integer." d &lt;- readParse "Please enter your desired date:" "Not a date." availableSeats &lt;- (sum . fmap seatsLeft) &lt;$> getSlots d if availableSeats &lt; q then writeLine $ "Only " ++ show availableSeats ++ " remaining seats." else do n &lt;- readAnything "Please enter your name:" e &lt;- readAnything "Please enter your email address:" postReservation Reservation { reservationDate = d , reservationName = n , reservationEmail = e , reservationQuantity = q } </pre> <p>And finally your interpreter needs to dispatch over <code>InL</code>/<code>InR</code> (this is using functions from <code>Control.Monad.Free</code>, you can actually drop the <code>Trans</code> import at this point):</p> <pre> interpretCommandLine :: CommandLineInstruction (IO a) -> IO a interpretCommandLine (ReadLine next) = getLine >>= next interpretCommandLine (WriteLine line next) = putStrLn line >> next interpretReservationsApi :: ReservationsApiInstruction (IO a) -> IO a interpretReservationsApi (GetSlots zt next) = HttpClient.getSlots zt >>= next interpretReservationsApi (PostReservation r next) = HttpClient.postReservation r >> next interpret :: Program a -> IO a interpret program = iterM go program where go (InL cmd) = interpretCommandLine cmd go (InR res) = interpretReservationsApi res </pre> <p>I find this to be quite clean!</p> </div> <div class="comment-date">2017-07-27 3:58 UTC</div> </div> <div class="comment" id="e41e69b7263645c7b3df5be1e459203e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e41e69b7263645c7b3df5be1e459203e">#</a></div> <div class="comment-content"> <p> George, thank you for writing. That alternative does, indeed, look simpler and cleaner than mine. Thank you for sharing. </p> <p> FWIW, one reason I write articles on this blog is to learn and become better. I publish what I know and have learned so far, and sometimes, people tell me that there's a better way. That's great, because it makes me a better programmer, and hopefully, it may make other readers better as well. </p> <p> In case you'll be puzzling over my next blog post, however, I'm going to share a little secret (which is not a secret if you look at the blog's commit history): I wrote this article series more than a month ago, which means that all the remaining articles are already written. While I agree that using the sum of functors instead of <code>FreeT</code> simplifies the Haskell code, I don't think it makes that much of a difference when translating to F#. I may be wrong, but I haven't tried yet. My point, though, is that the next article in the series is going to ignore this better alternative, because, when it was written, I didn't know about it. I invite any interested reader to post, as a comment to that future article, their better alternatives :) </p> </div> <div class="comment-date">2017-07-27 7:31 UTC</div> </div> <div class="comment" id="eeb9980665de4603a533b4d36459b614"> <div class="comment-author"><a href="https://stackoverflow.com/users/1523776/benjamin-hodgson?tab=profile">Benjamin Hodgson</a> <a href="#eeb9980665de4603a533b4d36459b614">#</a></div> <div class="comment-content"> <p> Hi Mark, </p> <p> I think you'll enjoy <a href="http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesALaCarte.pdf"><i>Data Types a la Carte</i></a>. It's the definitive introduction to the style that George Pollard demonstrates above. Swierstra covers how to build datatypes with initial algebras over coproducts, compose them abstracting over the concrete functor, and tear them down generically. It's well written, too 😉 </p> <p> Benjamin </p> </div> <div class="comment-date">2017-07-23 28:40 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A pure command-line wizard https://blog.ploeh.dk/2017/07/17/a-pure-command-line-wizard 2017-07-17T12:04:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of a small Abstract Syntax Tree written with F# syntactic sugar.</em> </p> <p> In the <a href="/2017/07/11/hello-pure-command-line-interaction">previous article</a>, you got an introduction to a functional command-line API in F#. The example in that article, however, was too simple to highlight its composability. In this article, you'll see a fuller example. </p> <h3 id="51d1bac377c84af88f383b189f22393a"> Command-line wizard for on-line restaurant reservations <a href="#51d1bac377c84af88f383b189f22393a" title="permalink">#</a> </h3> <p> In <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">previous articles</a>, you can see variations on an HTTP-based back-end for an on-line restaurant reservation system. In this article, on the other hand, you're going to see a first attempt at a command-line <em>client</em> for the API. </p> <p> Normally, an on-line restaurant reservation system would have GUIs hosted in web pages or mobile apps, but with an open HTTP API, a self-respecting geek would prefer a command-line interface (CLI)... right?! </p> <p> <pre>Please enter number of diners: four Not an integer. Please enter number of diners: 4 Please enter your desired date: My next birthday Not a date. Please enter your desired date: 2017-11-25 Please enter your name: Mark Seemann Please enter your email address: mark@example.com {Date = 25.11.2017 00:00:00 +01:00; Name = "Mark Seemann"; Email = "mark@example.com"; Quantity = 4;}</pre> </p> <p> In this incarnation, the CLI only collects information in order to dump a rendition of an F# record on the command-line. In a future article, you'll see how to combine this with an HTTP client in order to make a reservation with the back-end system. </p> <p> Notice that the CLI is a wizard. It leads you through a series of questions. You have to give an appropriate answer to each question before you can move on to the next question. For instance, you must type an integer for the number of guests; if you don't, the wizard will repeatedly ask you for an integer until you do. </p> <p> You can develop such an interface with the <code>commandLine</code> computation expression from <a href="/2017/07/11/hello-pure-command-line-interaction">the previous article</a>. </p> <h3 id="bf51af8fa0b349e3941570b1c9392868"> Reading quantities <a href="#bf51af8fa0b349e3941570b1c9392868" title="permalink">#</a> </h3> <p> There are four steps in the wizard. The first is to read the desired quantity from the command line: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;int&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;readQuantity&nbsp;=&nbsp;<span style="color:blue;">commandLine</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">writeLine</span>&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;number&nbsp;of&nbsp;diners:&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;l&nbsp;=&nbsp;<span style="color:teal;">CommandLine</span>.readLine &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:teal;">Int32</span>.<span style="color:navy;">TryParse</span>&nbsp;l&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;dinerCount&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;dinerCount &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">writeLine</span>&nbsp;<span style="color:#a31515;">&quot;Not&nbsp;an&nbsp;integer.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;readQuantity&nbsp;}</pre> </p> <p> This small piece of interaction is defined entirely within a <code>commandLine</code> expression. This enables you to use <code>do!</code> expressions and <code>let!</code> bindings to compose smaller <code>CommandLineProgram</code> values, such as <code>CommandLine.writeLine</code> and <code>CommandLine.readLine</code> (both shown in the previous article). </p> <p> After prompting the user to enter a number, the program reads the user's input from the command line. While <code>CommandLine.readLine</code> is a <code>CommandLineProgram&lt;string&gt;</code> value, the <code>let!</code> binding turns <code>l</code> into a <code>string</code> value. If you can parse <code>l</code> as an integer, you return the integer; otherwise, you recursively return <code>readQuantity</code>. </p> <p> The <code>readQuantity</code> program will continue to prompt the user for an integer. It gives you no option to cancel the wizard. This is a deliberate simplification I did in order to keep the example as simple as possible, but a real program should offer an option to abort the wizard. </p> <p> The function returns a <code>CommandLineProgram&lt;int&gt;</code> value. This is a <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> value containing an Abstract Syntax Tree (AST) that describes the interactions to perform. It doesn't do anything until interpreted. Contrary to designing with Dependency Injection and interfaces, however, you can immediately tell, from the type, that explicitly delimited impure interactions may take place within that part of your code base. </p> <h3 id="45c7d53d59ae407ba0dda4d63e2e41fd"> Reading dates <a href="#45c7d53d59ae407ba0dda4d63e2e41fd" title="permalink">#</a> </h3> <p> When you've entered a proper number of diners, you proceed to enter a date. The program for that looks similar to <code>readQuantity</code>: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;DateTimeOffset&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;readDate&nbsp;=&nbsp;commandLine&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">writeLine</span>&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;your&nbsp;desired&nbsp;date:&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;l&nbsp;=&nbsp;<span style="color:teal;">CommandLine</span>.readLine &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:teal;">DateTimeOffset</span>.<span style="color:navy;">TryParse</span>&nbsp;l&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;dt&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;dt &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">writeLine</span>&nbsp;<span style="color:#a31515;">&quot;Not&nbsp;a&nbsp;date.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;readDate&nbsp;}</pre> </p> <p> The <code>readDate</code> value is so similar to <code>readQuantity</code> that you might be tempted to refactor both into a single, reusable function. In this case, however, I chose to stick to the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">rule of three</a>. </p> <h3 id="fc63d6e544954a0abe94a715006a9474"> Reading strings <a href="#fc63d6e544954a0abe94a715006a9474" title="permalink">#</a> </h3> <p> Reading the customer's name and email address from the command line is easy, as no parsing is required: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;string&gt;</span> <span style="color:blue;">let</span>&nbsp;readName&nbsp;=&nbsp;commandLine&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;CommandLine.writeLine&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;your&nbsp;name:&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;CommandLine.readLine&nbsp;} <span style="color:green;">//&nbsp;CommandLineProgram&lt;string&gt;</span> <span style="color:blue;">let</span>&nbsp;readEmail&nbsp;=&nbsp;commandLine&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;CommandLine.writeLine&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;your&nbsp;email&nbsp;address:&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;CommandLine.readLine&nbsp;}</pre> </p> <p> Both of these values unconditionally accept whatever you write when prompted. From a security standpoint, <em>all input is evil</em>, so in a production code base, you should still perform some validation. This, on the other hand, is demo code, so with that caveat, it accepts all strings you might type. </p> <p> These values are similar to each other, but once again I invoke the rule of three and keep them as separate values. </p> <h3 id="45aafd76a67241eb93256e713c6470ab"> Composing the wizard <a href="#45aafd76a67241eb93256e713c6470ab" title="permalink">#</a> </h3> <p> Together with the general-purpose command line API, the above values are all you need to compose the wizard. In this incarnation, the wizard should collect the information you type, and create a single record with those values. This is the type of record it must create: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Reservation</span>&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;:&nbsp;<span style="color:teal;">DateTimeOffset</span> &nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;:&nbsp;<span style="color:teal;">string</span> &nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;:&nbsp;<span style="color:teal;">string</span> &nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;:&nbsp;<span style="color:teal;">int</span>&nbsp;}</pre> </p> <p> You can easily compose the wizard like this: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;Reservation&gt;</span> <span style="color:blue;">let</span>&nbsp;readReservationRequest&nbsp;=&nbsp;commandLine&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;count&nbsp;=&nbsp;readQuantity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;date&nbsp;&nbsp;=&nbsp;readDate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;name&nbsp;&nbsp;=&nbsp;readName &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;email&nbsp;=&nbsp;readEmail &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;{&nbsp;Date&nbsp;=&nbsp;date;&nbsp;Name&nbsp;=&nbsp;name;&nbsp;Email&nbsp;=&nbsp;email;&nbsp;Quantity&nbsp;=&nbsp;count&nbsp;}&nbsp;}</pre> </p> <p> There's really nothing to it. As all the previous code examples in this article, you compose the <code>readReservationRequest</code> value entirely inside a <code>commandLine</code> expression. You use <code>let!</code> bindings to collect the four data elements you need, and once you have all four, you can return a <code>Reservation</code> value. </p> <h3 id="e52e8a907e41492db260bd0f257d3152"> Running the program <a href="#e52e8a907e41492db260bd0f257d3152" title="permalink">#</a> </h3> <p> You may have noticed that no code so far shown define functions; they are all <em>values</em>. They are small program fragments, expressed as ASTs, composed into slightly larger programs that are still ASTs. So far, all the code is pure. </p> <p> In order to run the program, you need an interpreter. You can reuse the interpreter from the <a href="/2017/07/11/hello-pure-command-line-interaction">previous article</a> when composing your <code>main</code> function: </p> <p> <pre>[&lt;<span style="color:teal;">EntryPoint</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">main</span>&nbsp;_&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Wizard</span>.readReservationRequest &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">bind</span>&nbsp;(<span style="color:teal;">CommandLine</span>.<span style="color:navy;">writeLine</span>&nbsp;&lt;&lt;&nbsp;(<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%A</span><span style="color:#a31515;">&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span> &nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;<span style="color:green;">//&nbsp;return&nbsp;an&nbsp;integer&nbsp;exit&nbsp;code</span></pre> </p> <p> Notice that most of the behaviour is defined by the above <code>Wizard.readReservationRequest</code> value. That program, however, returns a <code>Reservation</code> value that you should also print to the command line, using the <code>CommandLine</code> module. You can achieve that behaviour by composing <code>Wizard.readReservationRequest</code> with <code>CommandLine.writeLine</code> using <code>CommandLine.bind</code>. Another way to write the same composition would be by using a <code>commandLine</code> computation expression, but in this case, I find the small pipeline of functions easier to read. </p> <p> When you bind two <code>CommandLineProgram</code> values to each other, the result is a third <code>CommandLineProgram</code>. You can pipe that to <code>interpret</code> in order to run the program. The result is an interaction like the one shown in the beginning of this article. </p> <h3 id="46429e5615cb440dbcd274407311f161"> Summary <a href="#46429e5615cb440dbcd274407311f161" title="permalink">#</a> </h3> <p> In this article, you've seen how you can create larger ASTs from smaller ASTs, using the syntactic sugar that F# computation expressions afford. The point, so far, is that you can make side-effects and non-deterministic behaviour <em>explicit</em>, while retaining the 'normal' F# development experience. </p> <p> In Haskell, impure code can execute within an <code>IO</code> context, but inside <code>IO</code>, <em>any</em> sort of side-effect or non-deterministic behaviour could take place. For that reason, even in Haskell, it often makes sense to define an explicitly delimited set of impure operations. In the previous article, you can see a small Haskell code snippet that defines a command-line instruction AST type using <code>Free</code>. When you, as a code reader, encounter a value of the type <code>CommandLineProgram String</code>, you know more about the potential impurities than if you encounter a value of the type <code>IO String</code>. The same argument applies, with qualifications, in F#. </p> <p> When you encounter a value of the type <code>CommandLineProgram&lt;Reservation&gt;</code>, you know what sort of impurities to expect: the program will only write to the command line, or read from the command line. What if, however, you'd like to combine those particular interactions with other types of interactions? </p> <p> Read on. </p> <p> <strong>Next:</strong> <a href="/2017/07/24/combining-free-monads-in-haskell">Combining free monads in Haskell</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Hello, pure command-line interaction https://blog.ploeh.dk/2017/07/11/hello-pure-command-line-interaction 2017-07-11T12:48:00+00:00 Mark Seemann <div id="post"> <p> <em>A gentle introduction to modelling impure interactions with pure code.</em> </p> <p> Dependency Injection is a <a href="http://amzn.to/12p90MG">well-described</a> concept in object-oriented programming, but as I've <a href="/2017/01/30/partial-application-is-dependency-injection">explained earlier</a>, its not functional, because it makes everything impure. In general, you should <a href="/2017/02/02/dependency-rejection">reject the notion of dependencies</a> by instead designing your application on the concept of an <a href="/2020/03/02/impureim-sandwich">impure/pure/impure sandwich</a>. This is possible more often than you'd think, but <a href="/2017/07/10/pure-interactions">there's still a large group of applications where this will not work</a>. If your application needs to interact with the impure world for an extended time, you need a way to model such interactions in a pure way. </p> <p> This article introduces a way to do that. </p> <h3 id="83b64173940447b489beeed943c8c140"> Command line API <a href="#83b64173940447b489beeed943c8c140" title="permalink">#</a> </h3> <p> Imagine that you have to write a command-line program that can ask a series of questions and print appropriate responses. In the general case, this is a (potentially) long-running series of interactions between the user and the program. To keep it simple, though, in this article we'll start by looking at a degenerate example: </p> <p> <pre>Please enter your name. Mark Hello, Mark!</pre> </p> <p> The program is simply going to request that you enter your name. Once you've done that, it prints the greeting. In object-oriented programming, using Dependency Injection, you might introduce an interface. Keeping it simple, you can restrict such an interface to two methods: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ICommandLine</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;ReadLine(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;WriteLine(<span style="color:blue;">string</span>&nbsp;text); }</pre> </p> <p> Please note that this is clearly a toy example. In later articles, you'll see how to expand the example to cover some more complex interactions, but you could also <a href="/2017/06/27/pure-times">read a more realistic example already</a>. Initially, the example is degenerate, because there's only a single interaction. In this case, an impure/pure/impure sandwich is still possible, but such a design wouldn't scale to more complex interactions. </p> <p> The problem with defining and injecting an interface is that it isn't functional. What's the functional equivalent, then? </p> <h3 id="ab5167a9316d4dd6bba887eddfa7a889"> Instruction set <a href="#ab5167a9316d4dd6bba887eddfa7a889" title="permalink">#</a> </h3> <p> Instead of defining an interface, you can define a discriminated union that describes a limited instruction set for command-line interactions: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CommandLineInstruction</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">ReadLine</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">string</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;a) |&nbsp;<span style="color:navy;">WriteLine</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span>&nbsp;*&nbsp;&#39;a</pre> </p> <p> You may notice that it looks a bit like the above C# interface. Instead of defining two methods, it defines two cases, but the names are similar. </p> <p> The <code>ReadLine</code> case is an <em>instruction</em> that an interpreter can evaluate. The data contained in the case is a continuation function. After evaluating the instruction, an interpreter must invoke this function with a string. It's up to the interpreter to figure out which string to use, but it could, for example, come from reading an input string from the command line. The continuation function is the next step in whatever program you're writing. </p> <p> The <code>WriteLine</code> case is another instruction for interpreters. The data contained in this case is a tuple. The first element of the tuple is input for the interpreter, which can choose to e.g. print the value on the command line, or ignore it, and so on. The second element of the tuple is a value used to continue whatever program this case is a part of. </p> <p> This enables you to write a small, specialised Abstract Syntax Tree (AST), but there's currently no way to return from it. One way to do that is to add a third 'stop' case. If you're interested in that option, <a href="https://fsharpforfunandprofit.com">Scott Wlaschin</a> covers this as one iteration in his <a href="https://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle-2/#way13">excellent explanation of the AST design</a>. </p> <p> Instead of adding a third 'stop' case to <code>CommandLineInstruction&lt;'a&gt;</code>, another option is to add a new wrapper type around it: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CommandLineProgram</span>&lt;&#39;a&gt;&nbsp;= |&nbsp;<span style="color:navy;">Free</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">CommandLineInstruction</span>&lt;<span style="color:teal;">CommandLineProgram</span>&lt;&#39;a&gt;&gt; |&nbsp;<span style="color:navy;">Pure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;a</pre> </p> <p> The <code>Free</code> case contains a <code>CommandLineInstruction</code> that always continues to a new <code>CommandLineProgram</code> value. The only way you can escape the AST is via the <code>Pure</code> case, which simply contains the 'return' value. </p> <h3 id="2e0ec21965a54fe78144a97ffd09c090"> Abstract Syntax Trees <a href="#2e0ec21965a54fe78144a97ffd09c090" title="permalink">#</a> </h3> <p> With these two types you can write specialised programs that contain instructions for an interpreter. Notice that the types are pure by intent, although in F# we can't really tell. You can, however, repeat this exercise in <a href="https://www.haskell.org">Haskell</a>, where the instruction set looks like this: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">CommandLineInstruction</span>&nbsp;next&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">ReadLine</span>&nbsp;(<span style="color:#dd0000;">String</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;next) &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">WriteLine</span>&nbsp;<span style="color:#dd0000;">String</span>&nbsp;next &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Functor</span>) <span style="color:blue;">type</span>&nbsp;<span style="color:#dd0000;">CommandLineProgram</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;<span style="color:#dd0000;">CommandLineInstruction</span></pre> </p> <p> Both of these types are <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, because <code>IO</code> is nowhere in sight. In Haskell, functions are pure by default. This also applies to the <code>String -&gt; next</code> function contained in the <code>ReadLine</code> case. </p> <p> Back in F# land, you can write an AST that implements the command-line interaction shown in the beginning of the article: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;unit&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:#9b9b9b;">program</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">WriteLine</span>&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;your&nbsp;name.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">ReadLine</span>&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">fun</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">WriteLine</span>&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;Hello,&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">!&quot;</span>&nbsp;s, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Pure</span>&nbsp;()))))))</pre> </p> <p> This AST defines a little program. The first step is a <code>WriteLine</code> instruction with the input value <code>"Please enter your name."</code>. The <code>WriteLine</code> case constructor takes a tuple as input argument. The first tuple element is that prompt, and the second element is the continuation, which has to be a new <code>CommandLineInstruction&lt;CommandLineProgram&lt;'a&gt;&gt;</code> value. </p> <p> In this example, the continuation value is a <code>ReadLine</code> case, which takes a continuation function as input. This function should return a new program value, which it does by returning a <code>WriteLine</code>. </p> <p> This second <code>WriteLine</code> value creates a <code>string</code> from the outer value <code>s</code>. The second tuple element for the <code>WriteLine</code> case must, again, be a new program value, but now the program is done, so you can use the 'stop' value <code>Pure ()</code>. </p> <p> You probably think that I should quit the mushrooms. No one in their right mind will want to write code like this. Neither would I. Fortunately, you can make the coding experience <em>much</em> better, but you'll see how to do that later. </p> <h3 id="6d0c09724041434396fd9d83a9756dfc"> Interpretation <a href="#6d0c09724041434396fd9d83a9756dfc" title="permalink">#</a> </h3> <p> The above <code>program</code> value is a small <code>CommandLineProgram&lt;unit&gt;</code>. It's a pure value. In itself, it doesn't do anything. </p> <p> Clearly, we'd like it to <em>do</em> something. In order to make that happen, you can write an interpreter: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">interpret</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Pure</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">ReadLine</span>&nbsp;&nbsp;<span style="color:navy;">next</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">Console</span>.<span style="color:navy;">ReadLine</span>&nbsp;()&nbsp;|&gt;&nbsp;<span style="color:navy;">next</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">WriteLine</span>&nbsp;(s,&nbsp;next))&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Console</span>.<span style="color:navy;">WriteLine</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span></pre> </p> <p> This interpreter is a recursive function that pattern-matches all the cases in any <code>CommandLineProgram&lt;'a&gt;</code>. When it encounters a <code>Pure</code> case, it simply returns the contained value. </p> <p> When it encounters a <code>ReadLine</code> value, it calls <code>Console.ReadLine ()</code>, which returns a <code>string</code> value read from the command line. It then pipes that input value to its <code>next</code> continuation function, which produces a new <code>CommandLineInstruction&lt;CommandLineProgram&lt;'a&gt;&gt;</code> value. Finally, it pipes that continuation value recursively to itself. </p> <p> A similar treatment is given to the <code>WriteLine</code> case. <code>Console.WriteLine s</code> writes <code>s</code> to the command line, where after <code>next</code> is recursively piped to <code>interpret</code>. </p> <p> When you run <code>interpret program</code>, you get an interaction like this: </p> <p> <pre>Please enter your name. ploeh Hello, ploeh!</pre> </p> <p> The <code>program</code> is pure; the <code>interpret</code> function is impure. </p> <h3 id="7dfea04e30524bbe9626f3bd346cb23b"> Syntactic sugar <a href="#7dfea04e30524bbe9626f3bd346cb23b" title="permalink">#</a> </h3> <p> Clearly, you don't want to write programs as ASTs like the above. Fortunately, you don't have to. You can add syntactic sugar in the form of <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expressions</a>. The way to do that is to turn your AST types into a monad. In Haskell, you'd already be done, because <code>Free</code> is a monad. In F#, some code is required. </p> <h3 id="7770d8455b644f2ead7b5ed1c20ec5e9"> Source functor <a href="#7770d8455b644f2ead7b5ed1c20ec5e9" title="permalink">#</a> </h3> <p> The first step is to define a map function for the underlying instruction set union type. Conceptually, when you can define a map function for a type, you've created a <em>functor</em> (if it obeys the functor laws, that is). Functors are common, so it often pays off being aware of them. </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;CommandLineInstruction&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;CommandLineInstruction&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">mapI</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">ReadLine</span>&nbsp;<span style="color:navy;">next</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">ReadLine</span>&nbsp;(<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">WriteLine</span>&nbsp;(x,&nbsp;next)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">WriteLine</span>&nbsp;(x,&nbsp;next&nbsp;|&gt;&nbsp;<span style="color:navy;">f</span>)</pre> </p> <p> The <code>mapI</code> function takes a <code>CommandLineInstruction&lt;'a&gt;</code> value and maps it to a new value by mapping the 'underlying value'. I decided to make the function <code>private</code> because later, I'm also going to define a map function for <code>CommandLineProgram&lt;'a&gt;</code>, and I don't want to confuse users of the API with two different map functions. This is also the reason that the name of the function isn't simply <code>map</code>, but rather <code>mapI</code>, where the <em>I</em> stands for <em>instruction</em>. </p> <p> <code>mapI</code> pattern-matches on the (implicit) input argument. If it's a <code>ReadLine</code> case, it returns a new <code>ReadLine</code> value, but it uses the mapper function <code>f</code> to translate the <code>next</code> function. Recall that <code>next</code> is a function of the type <code>string -&gt; 'a</code>. When you compose it with <code>f</code> (which is a function of the type <code>'a -&gt; 'b</code>), you get <code>(string -&gt; 'a) &gt;&gt; ('a -&gt; 'b)</code>, or <code>string -&gt; 'b</code>. You've transformed the <code>'a</code> to a <code>'b</code> for the <code>ReadLine</code> case. If you can do the same for the <code>WriteLine</code> case, you'll have a functor. </p> <p> Fortunately, the <code>WriteLine</code> case is similar, although a small tweak is required. This case contains a tuple of data. The first element (<code>x</code>) isn't a generic type (it's a <code>string</code>), so there's nothing to map. You can use it as-is in the new <code>WriteLine</code> value that you'll return. The <code>WriteLine</code> case is degenerate because <code>next</code> isn't a function, but rather a value. It has a type of <code>'a</code>, and <code>f</code> is a function of the type <code>'a -&gt; 'b</code>, so piping <code>next</code> to <code>f</code> returns a <code>'b</code>. </p> <p> That's it: now you have a functor. </p> <p> (In order to keep the category theorists happy, I should point out that such functors are really a sub-type of functors called <em>endo-functors</em>. Additionally, functors must obey some simple and intuitive laws in order to be functors, but that's all I'll say about that here.) </p> <h3 id="589b0a42a0b74df49fd2b1b7c348b395"> Free monad <a href="#589b0a42a0b74df49fd2b1b7c348b395" title="permalink">#</a> </h3> <p> There's a reason I spend so much time talking about functors. The goal is syntactic sugar. You can get that with computation expressions. In order to create a computation expression builder, you need a monad. </p> <p> You need a recipe for creating a monad. Fortunately, there's a type of monad called a <em>free monad</em>. It has the virtue that it enables you to create a monad from any functor. </p> <p> Just what you need. </p> <p> In Haskell, this happens automatically when you declare <code>type CommandLineProgram = Free CommandLineInstruction</code>. Thanks to Haskell's type system, <code>Free</code> is automatically a <code>Monad</code> when the underlying type is a <code>Functor</code>. In F#, you have to work for your monads, but the fact that Haskell can automate this means that <a href="/2017/08/07/f-free-monad-recipe">there's a recipe that you can follow</a>. </p> <p> Earlier in this article, I mentioned in passing that there are alternative ways in which you can define a 'stop' case for your instruction set. The reason I chose to separate the API into two types (an 'instruction set', and a 'program') is that the instruction set is the underlying functor. The 'program' is part of the free monad. The other part is a <code>bind</code> function (that obeys the monad laws). </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;CommandLineProgram&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;CommandLineProgram&lt;&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;CommandLineProgram&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;bind&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;instruction&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;instruction&nbsp;|&gt;&nbsp;mapI&nbsp;(bind&nbsp;f)&nbsp;|&gt;&nbsp;Free &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Pure&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;x</pre> </p> <p> This recursive function pattern-matches on the (implicit) <code>CommandLineProgram&lt;'a&gt;</code> argument. In the <code>Pure</code> case, the 'return' value <code>x</code> has the type <code>'a</code>, which fits as input for the <code>f</code> function. The result is a value of type <code>CommandLineProgram&lt;'b&gt;</code>. </p> <p> In the <code>Free</code> case, the <code>instruction</code> is a functor with the map function <code>mapI</code>. The first argument to the <code>mapI</code> function must be a function with the type <code>'a -&gt; 'b</code>. How can you compose such a function? </p> <p> If you partially apply the recursive <code>bind</code> function with <code>f</code> (that is: <code>bind f</code>), you get a function of the type <code>CommandLineProgram&lt;'a&gt; -&gt; CommandLineProgram&lt;'b&gt;</code>. This fits with <code>mapI</code>, because <code>instruction</code> has the type <code>CommandLineInstruction&lt;CommandLineProgram&lt;'a&gt;&gt;</code> (refer back to the definition of the <code>Free</code> case if need to convince yourself of that). The result of calling <code>mapI</code> with <code>instruction</code> is a <code>CommandLineInstruction&lt;CommandLineProgram&lt;'b&gt;&gt;</code> value. In order to turn it into a <code>CommandLineProgram&lt;'b&gt;</code> value, you wrap it in a new <code>Free</code> case. </p> <p> Although this required a bit of explanation, defining a <code>bind</code> function for a free monad is a repeatable process. After all, in Haskell it's automated. In F#, you have to explicitly write the code, but it follows a recipe. Once you get the hang of it, there's not much to it. </p> <h3 id="9caef86d777840debdce29e615fb1a19"> Functor <a href="#9caef86d777840debdce29e615fb1a19" title="permalink">#</a> </h3> <p> You'll occasionally need to explicitly use the <code>bind</code> function, but often it'll 'disappear' into a computation expression. There are other building blocks to an API than a <code>bind</code> function, though. You may, for example, need a <code>map</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;CommandLineProgram&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;CommandLineProgram&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;=&nbsp;bind&nbsp;(f&nbsp;&gt;&gt;&nbsp;Pure)</pre> </p> <p> This makes <code>CommandLineProgram&lt;'a&gt;</code> a functor as well. This is the reason I made <code>mapI</code> private, because <code>mapI</code> makes the instruction set a functor, but the API is expressed in terms of AST programs, and it should be consistent. Within the same module, <code>map</code> should work on the same data type as <code>bind</code>. </p> <p> Notice that <code>map</code> can be defined as a composition of <code>bind</code> and <code>Pure</code>. This is part of <a href="/2017/08/07/f-free-monad-recipe">the recipe</a>. For a free monad, the <code>map</code> function always looks like that. The <code>f</code> function is a function with the type <code>'a -&gt; 'b</code>, and <code>Pure</code> is a case constructor with the type <code>'b -&gt; CommandLineProgram&lt;'b&gt;</code>. Notice that I've used <code>'b</code> for the generic type argument instead of the usual <code>'a</code>. Hopefully, this makes it clear that when you compose these two functions together (<code>f &gt;&gt; Pure</code>), you get a function of the type <code>('a -&gt; 'b) &gt;&gt; ('b -&gt; CommandLineProgram&lt;'b&gt;)</code>, or <code>'a -&gt; CommandLineProgram&lt;'b&gt;</code>. That's just the type of function needed for the <code>bind</code> function, so the whole composition turns out to type-check and work as intended. </p> <h3 id="f65a5cd264b14088999db8b2d4542bcb"> API <a href="#f65a5cd264b14088999db8b2d4542bcb" title="permalink">#</a> </h3> <p> In order to work with an API, you need the ability to create values of the API's type(s). In this case, you must be able to create <code>CommandLineProgram&lt;'a&gt;</code> values. While you can create them explicitly using the <code>ReadLine</code>, <code>WriteLine</code>, <code>Free</code>, and <code>Pure</code> case constructors, it'll be more convenient if you have some predefined functions and values for that. </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;string&gt;</span> <span style="color:blue;">let</span>&nbsp;readLine&nbsp;=&nbsp;Free&nbsp;(ReadLine&nbsp;Pure) <span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;CommandLineProgram&lt;unit&gt;</span> <span style="color:blue;">let</span>&nbsp;writeLine&nbsp;s&nbsp;=&nbsp;Free&nbsp;(WriteLine&nbsp;(s,&nbsp;Pure&nbsp;()))</pre> </p> <p> In the <code>ReadLine</code> case, there's no input to the instruction, so you can define <code>readLine</code> as a predefined <code>CommandLineProgram&lt;string&gt;</code> value. </p> <p> The <code>WriteLine</code> case, on the other hand, takes as an input argument a string to write, so you can define <code>writeLine</code> as a function that returns a <code>CommandLineProgram&lt;unit&gt;</code> value. </p> <h3 id="ebd27a0211314954b6c4a86b095a63b6"> Computation expression <a href="#ebd27a0211314954b6c4a86b095a63b6" title="permalink">#</a> </h3> <p> The addition of <code>map</code> and supporting API is, to be honest, a bit of digression. You're going to use these functions later, but they aren't required in order to create a computation expression builder. All you need is a <code>bind</code> function and a way to lift a raw value into the monad. All of these are in place, so the builder is a matter of delegation: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CommandLineBuilder</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Bind</span>&nbsp;(x,&nbsp;<span style="color:navy;">f</span>)&nbsp;=&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Return</span>&nbsp;x&nbsp;=&nbsp;<span style="color:navy;">Pure</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">ReturnFrom</span>&nbsp;x&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Zero</span>&nbsp;()&nbsp;=&nbsp;<span style="color:navy;">Pure</span>&nbsp;()</pre> </p> <p> This is a fairly minimal builder, but in my experience, most of times, this is all you need. </p> <p> Create an instance of the <code>CommandLineBuilder</code> class, and you can write computation expressions: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;commandLine&nbsp;=&nbsp;<span style="color:teal;">CommandLineBuilder</span>&nbsp;() </pre> </p> <p> I usually put such an object in a module with an <code>[&lt;AutoOpen&gt;]</code> attribute, so that it's available as a global object. </p> <h3 id="4b52a11876594bbab6c50ee2f579c3b6"> Producing ASTs with pretty code <a href="#4b52a11876594bbab6c50ee2f579c3b6" title="permalink">#</a> </h3> <p> Using the <code>commandLine</code> computation expression is like using the built-in <code>async</code> or <code>seq</code> expressions. You can use it to rewrite the above AST as readable code: </p> <p> <pre><span style="color:green;">//&nbsp;CommandLineProgram&lt;unit&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:#9b9b9b;">program</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">commandLine</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">writeLine</span>&nbsp;<span style="color:#a31515;">&quot;Please&nbsp;enter&nbsp;your&nbsp;name.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;name&nbsp;=&nbsp;<span style="color:teal;">CommandLine</span>.readLine &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;Hello,&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">!&quot;</span>&nbsp;name&nbsp;|&gt;&nbsp;<span style="color:teal;">CommandLine</span>.<span style="color:navy;">writeLine</span>&nbsp;}</pre> </p> <p> This produces the same AST as before, but with much more readable syntax. The AST is the same, and you can use the above <code>interpret</code> function to run it. The interaction is the same as before: </p> <p> <pre>Please enter your name. Free Hello, Free!</pre> </p> <p> This is, obviously, a toy example, but in coming articles, you'll see how to gradually enhance the code to perform some more complex interactions. </p> <h3 id="0af25c9d53624c8ba1b63ce2581aac01"> Summary <a href="#0af25c9d53624c8ba1b63ce2581aac01" title="permalink">#</a> </h3> <p> Functional programming emphasises pure functions, and a separation of pure and impure code. The simplest way to achieve such a separation is to design your code as an impure/pure/impure sandwich, but sometimes this isn't possible. When it's not possible, an alternative is to define an instruction set for an AST, and turn it into a free monad in order to enable enough syntactic sugar to keep the code readable. </p> <p> While this may seem complicated, it has the benefit of making impurities explicit in the code. Whenever you see a <code>CommandLineProgram</code> value, you know that, at run-time, something impure is likely to happen. It's not <em>uncontrolled</em> impurity, though. Inside a <code>CommandLineProgram</code>, only reading from, and writing to, the command line will happen. It's not going to generate random values, change global variables, send an email, or any other unpredictable operation - that is, unless the interpreter does that... </p> <p> <strong>Next:</strong> <a href="/2017/07/17/a-pure-command-line-wizard">A pure command-line wizard</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pure interactions https://blog.ploeh.dk/2017/07/10/pure-interactions 2017-07-10T14:29:00+00:00 Mark Seemann <div id="post"> <p> <em>Long-running, non-deterministic interactions can be modelled in a pure, functional way.</em> </p> <p> In a <a href="/2017/01/30/partial-application-is-dependency-injection">previous article</a>, you can read why Dependency Injection and (strict) functional programming are mutually exclusive. Dependency Injection makes everything impure, and if nothing is <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, then it's hardly functional. In <a href="/2017/02/02/dependency-rejection">Dependency rejection</a>, you can see how you can often separate impure and pure code into an <a href="/2020/03/02/impureim-sandwich">impure/pure/impure sandwich</a>. </p> <h3 id="2d0ec1b9ac0c47e08cb94122c1822d6f"> Micro-operation-based architectures <a href="#2d0ec1b9ac0c47e08cb94122c1822d6f" title="permalink">#</a> </h3> <p> The impure/pure/impure sandwich architecture works well in scenarios with limited interaction. Some data arrives at the boundary of the system, the system responds, and that's it. That, however, describes a significant fraction of all software running in the world today. </p> <p> Any HTTP-based application (web site, REST API, most SOAP services) fits the description: an HTTP request arrives, and the server responds with an HTTP response. In a well-designed and well-running system, you should return the response within seconds, if not faster. Everything the software needs in order to run to completion is either part of the request, or part of the application state. You may need to query a database to gather more data based on the incoming request, but you can still gather most data from impure sources, pass it all to your pure core implementation, get the pure values back and return the response. </p> <p> Likewise, asynchronous message-based systems, such as pub/sub, Pipes and Filters, Actor-based systems, 'SOA done right', CQRS/Event Sourcing, and so on, are based on short-lived, stateless interactions. Similar to HTTP-based applications, there's often (persisted) application state, but once a message arrives at a message handler, the software should process it as quickly as possible. Again, it can read extra (impure) data from a database, pass everything to a pure function, and finally do something impure with the return value. </p> <p> Common for all such systems is that while they can handle large volumes of data, they do so as the result of a multitude of parallel, distinct, and isolated micro-operations. </p> <h3 id="8b8f3dbfbdc149e5be6a0e3620affc50"> Interactive software <a href="#8b8f3dbfbdc149e5be6a0e3620affc50" title="permalink">#</a> </h3> <p> There is, however, another category of software. We could call it 'interactive software'. As the name implies, this includes everything with a user interface, but can also be a long-running batch job, or, as you've <a href="/2017/06/27/pure-times">already seen</a>, time-sensitive software. </p> <p> For such software, the impure/pure/impure sandwich architecture is no longer possible. Just think of a UI-based program, like an email client. You compose and send an email, receive a response, then compose a reply, and so on. Every send and receive is impure, as is all the user interface rendering. What happens next depends on what happened before, and everything that happens in the real world is impure. </p> <p> Have we finally identified the limitations of functional programming? </p> <p> Hardly. In this series of articles, I'm going to show you how to model pure interactions: <ul> <li><a href="/2017/07/11/hello-pure-command-line-interaction">Hello, pure command-line interaction</a></li> <li><a href="/2017/07/17/a-pure-command-line-wizard">A pure command-line wizard</a></li> <li><a href="/2017/07/24/combining-free-monads-in-haskell">Combining free monads in Haskell</a></li> <li><a href="/2017/07/31/combining-free-monads-in-f">Combining free monads in F#</a></li> <li><a href="/2017/08/07/f-free-monad-recipe">F# free monad recipe</a></li> </ul> You can skip the Haskell article if you only want to read the F# articles. </p> <p> This series of articles gives you a comprehensive walkthrough of pure interactions and free monads in F#. For a motivating example, see <a href="/2017/06/27/pure-times">Pure times</a>, which presents a more realistic example that, on the other hand, doesn't go to the same level of detail. </p> <h3 id="83c348001eb6406b8a13b89952ac0302"> Summary <a href="#83c348001eb6406b8a13b89952ac0302" title="permalink">#</a> </h3> <p> The solution to the problem of continuous impure interactions is to model them as a instructions in a (domain-specific) Abstract Syntax Tree (AST), and then using an impure interpreter for the pure AST. You can model the AST as a (free) monad in order to make the required syntax nice. </p> <p> <strong>Next:</strong> <a href="/2017/07/11/hello-pure-command-line-interaction">Hello, pure command-line interaction</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pure times in F# https://blog.ploeh.dk/2017/07/04/pure-times-in-f 2017-07-04T07:07:00+00:00 Mark Seemann <div id="post"> <p> <em>A Polling Consumer implementation written in F#.</em> </p> <p> <a href="/2017/06/28/pure-times-in-haskell">Previously</a>, you saw how to implement a <a href="http://www.enterpriseintegrationpatterns.com/PollingConsumer.html">Polling Consumer</a> in <a href="https://www.haskell.org">Haskell</a>. This proves that it's possible to write <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> functional code modelling long-running interactions with the (impure) world. In this article, you'll see how to port the Haskell code to F#. </p> <p> For reference, I'll repeat the state transition diagram here: </p> <p> <img src="/content/binary/polling-consumer-finite-state-machine.png" alt="Polling Consumer state machine transition diagram"> </p> <p> For a complete description of the goals and constraints of this particular Polling Consumer implementation, see my earlier <a href="/2015/08/10/type-driven-development">Type Driven Development</a> article, or, even better, watch my Pluralsight course <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a>. </p> <h3 id="badc1de0d52541b9a5f7b0bb04e5bc0a"> State data types <a href="#badc1de0d52541b9a5f7b0bb04e5bc0a" title="permalink">#</a> </h3> <p> The program has to keep track of various durations. You can model these as naked <code>TimeSpan</code> values, but in order to add extra type safety, you can, instead, define them as separate types: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PollDuration</span>&nbsp;=&nbsp;<span style="color:navy;">PollDuration</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">TimeSpan</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">IdleDuration</span>&nbsp;=&nbsp;<span style="color:navy;">IdleDuration</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">TimeSpan</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">HandleDuration</span>&nbsp;=&nbsp;<span style="color:navy;">HandleDuration</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">TimeSpan</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CycleDuration</span>&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;PollDuration&nbsp;:&nbsp;<span style="color:teal;">PollDuration</span> &nbsp;&nbsp;&nbsp;&nbsp;HandleDuration&nbsp;:&nbsp;<span style="color:teal;">HandleDuration</span>&nbsp;}</pre> </p> <p> This is a straightforward port of the Haskell code. See the <a href="/2017/06/28/pure-times-in-haskell">previous article</a> for more details about the motivation for doing this. </p> <p> You can now define the states of the finite state machine: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">State</span>&lt;&#39;msg&gt;&nbsp;= |&nbsp;<span style="color:navy;">ReadyState</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">CycleDuration</span>&nbsp;<span style="color:teal;">list</span> |&nbsp;<span style="color:navy;">ReceivedMessageState</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">CycleDuration</span>&nbsp;<span style="color:teal;">list</span>&nbsp;*&nbsp;<span style="color:teal;">PollDuration</span>&nbsp;*&nbsp;&#39;msg) |&nbsp;<span style="color:navy;">NoMessageState</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">CycleDuration</span>&nbsp;<span style="color:teal;">list</span>&nbsp;*&nbsp;<span style="color:teal;">PollDuration</span>) |&nbsp;<span style="color:navy;">StoppedState</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">CycleDuration</span>&nbsp;<span style="color:teal;">list</span></pre> </p> <p> Again, this is a straight port of the Haskell code. </p> <h3 id="67527dd94b2144ec9eedafb9cefef309"> From instruction set to syntactic sugar <a href="#67527dd94b2144ec9eedafb9cefef309" title="permalink">#</a> </h3> <p> The Polling Consumer must interact with its environment in various ways: <ol> <li>Query the system clock</li> <li>Poll for messages</li> <li>Handle messages</li> <li>Idle</li> </ol> You can model these four cases of interactions as a single discriminated union that describe a small instruction set: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PollingInstruction</span>&lt;&#39;msg,&nbsp;&#39;next&gt;&nbsp;= |&nbsp;<span style="color:navy;">CurrentTime</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">DateTimeOffset</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;next) |&nbsp;<span style="color:navy;">Poll</span>&nbsp;<span style="color:blue;">of</span>&nbsp;((&#39;msg&nbsp;<span style="color:teal;">option</span>&nbsp;*&nbsp;<span style="color:teal;">PollDuration</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;next) |&nbsp;<span style="color:navy;">Handle</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(&#39;msg&nbsp;*&nbsp;(<span style="color:teal;">HandleDuration</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;next)) |&nbsp;<span style="color:navy;">Idle</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">IdleDuration</span>&nbsp;*&nbsp;(<span style="color:teal;">IdleDuration</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;&#39;next))</pre> </p> <p> Once more, this is a direct translation of the Haskell code, but from here, this is where your F# code will have to deviate from Haskell. In Haskell, you can, with a single line of code, declare that such a type is a functor. This isn't possible in F#. Instead, you have to explicitly write a <em>map</em> function. This isn't difficult, though. There's a reason that the Haskell compiler can automate this: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;PollingInstruction&lt;&#39;c,&#39;a&gt;&nbsp;-&gt;&nbsp;PollingInstruction&lt;&#39;c,&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">mapI</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">CurrentTime</span>&nbsp;<span style="color:navy;">next</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">CurrentTime</span>&nbsp;(<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Poll</span>&nbsp;<span style="color:navy;">next</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Poll</span>&nbsp;(<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Handle</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Handle</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Idle</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Idle</span>&nbsp;(x,&nbsp;<span style="color:navy;">next</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">f</span>)</pre> </p> <p> The function is named <code>mapI</code>, where the <code>I</code> stands for <em>instruction</em>. It's <code>private</code> because the next step is to package the functor in a monad. From that monad, you can define a new functor, so in order to prevent any confusion, I decided to hide the underlying functor from any consumers of the API. </p> <p> Defining a map function for a generic type like <code>PollingInstruction&lt;'msg, 'next&gt;</code> is well-defined. Pattern-match each union case and return the same case, but with the <code>next</code> function composed with the input function argument <code>f</code>: <code>next &gt;&gt; f</code>. In later articles, you'll see more examples, and you'll see how <a href="/2017/08/07/f-free-monad-recipe">this recipe is entirely repeatable</a> and automatable. </p> <p> While a <em>functor</em> isn't an explicit concept in F#, this is how <code>PollingInstruction msg next</code> is a <code>Functor</code> in Haskell. Given a functor, you can produce a free monad. The reason you'd want to do this is that once you have a monad, you can get syntactic sugar. Currently, <code>PollingInstruction&lt;'msg, 'next&gt;</code> only enables you to create Abstract Syntax Trees (ASTs), but the programming experience would be cumbersome and alien. Monads give you automatic <code>do</code> notation in Haskell; in F#, it enables you to write a <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expression builder</a>. </p> <p> Haskell's type system enables you to make a monad from a functor with a one-liner: <code>type PollingProgram msg = Free (PollingInstruction msg)</code>. In F#, you'll have to write some boilerplate code. First, you have to define the monadic type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PollingProgram</span>&lt;&#39;msg,&nbsp;&#39;next&gt;&nbsp;= |&nbsp;<span style="color:navy;">Free</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PollingInstruction</span>&lt;&#39;msg,&nbsp;<span style="color:teal;">PollingProgram</span>&lt;&#39;msg,&nbsp;&#39;next&gt;&gt; |&nbsp;<span style="color:navy;">Pure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;next</pre> </p> <p> You already saw a hint of such a type in the previous article. The <code>PollingProgram&lt;'msg, 'next&gt;</code> discriminated union defines two cases: <code>Free</code> and <code>Pure</code>. The <code>Free</code> case is a <code>PollingInstruction</code> that produces a new <code>PollingProgram</code> as its next step. In essence, this enables you to build an AST, but you also need a signal to stop and return a value from the AST. That's the purpose of the <code>Pure</code> case. </p> <p> Such a type is only a monad if it defines a <code>bind</code> function (that obey the monad laws): </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;b,&#39;c&gt;)&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;b,&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;b,&#39;c&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;bind&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Free&nbsp;instruction&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;instruction&nbsp;|&gt;&nbsp;mapI&nbsp;(bind&nbsp;f)&nbsp;|&gt;&nbsp;Free &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Pure&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;x</pre> </p> <p> This <code>bind</code> function pattern-matches on <code>Free</code> and <code>Pure</code>, respectively. In the <code>Pure</code> case, it simply uses the underlying result value <code>x</code> as an input argument to <code>f</code>. In the <code>Free</code> case, it composes the underlying functor (<code>mapI</code>) with itself recursively. If you find this step obscure, I will not blame you. Just like the implementation of <code>mapI</code> is a bit of boilerplate code, then so is this. It always seems to work this way. If you want to dig deeper into the inner workings of this, then <a href="https://fsharpforfunandprofit.com">Scott Wlaschin</a> has a <a href="https://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle-2/#way13">detailed explanation</a>. </p> <p> With the addition of <code>bind</code> <code>PollingProgram&lt;'msg, 'next&gt;</code> becomes a monad (I'm not going to show that the monad laws hold, but they do). Making it a functor is trivial: </p> <p> <pre><span style="color:green;">//&nbsp;&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;c,&#39;a&gt;&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;c,&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;=&nbsp;bind&nbsp;(f&nbsp;&gt;&gt;&nbsp;Pure)</pre> </p> <p> The underlying <code>PollingInstruction</code> type was already a functor. This function makes <code>PollingProgram</code> a functor as well. </p> <p> It'll be convenient with some functions that lifts each <code>PollingInstruction</code> case to a corresponding <code>PollingProgram</code> value. In Haskell, you can use the <code>liftF</code> function for this, but in F# you'll have to be slightly more explicit: </p> <p> <pre><span style="color:green;">//&nbsp;PollingProgram&lt;&#39;a,DateTimeOffset&gt;</span> <span style="color:blue;">let</span>&nbsp;currentTime&nbsp;=&nbsp;Free&nbsp;(CurrentTime&nbsp;Pure) <span style="color:green;">//&nbsp;PollingProgram&lt;&#39;a,(&#39;a&nbsp;option&nbsp;*&nbsp;PollDuration)&gt;</span> <span style="color:blue;">let</span>&nbsp;poll&nbsp;=&nbsp;Free&nbsp;(Poll&nbsp;Pure) <span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;a,HandleDuration&gt;</span> <span style="color:blue;">let</span>&nbsp;handle&nbsp;msg&nbsp;=&nbsp;Free&nbsp;(Handle&nbsp;(msg,&nbsp;Pure)) <span style="color:green;">//&nbsp;IdleDuration&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;a,IdleDuration&gt;</span> <span style="color:blue;">let</span>&nbsp;idle&nbsp;duration&nbsp;=&nbsp;Free&nbsp;(Idle&nbsp;(duration,&nbsp;Pure))</pre> </p> <p> <code>currentTime</code> and <code>poll</code> aren't even functions, but <em>values</em>. They are, however, small <code>PollingProgram</code> values, so while they look like values (as contrasted to functions), they represent singular executable instructions. </p> <p> <code>handle</code> and <code>idle</code> are both functions that return <code>PollingProgram</code> values. </p> <p> You can now implement a small computation expression builder: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PollingBuilder</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Bind</span>&nbsp;(x,&nbsp;<span style="color:navy;">f</span>)&nbsp;=&nbsp;<span style="color:teal;">Polling</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Return</span>&nbsp;x&nbsp;=&nbsp;<span style="color:navy;">Pure</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">ReturnFrom</span>&nbsp;x&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:#9b9b9b;">this</span>.<span style="color:navy;">Zero</span>&nbsp;()&nbsp;=&nbsp;this.<span style="color:navy;">Return</span>&nbsp;()</pre> </p> <p> As you can tell, not much is going on here. The <code>Bind</code> method simply delegates to the above <code>bind</code> function, and the rest are trivial one-liners. </p> <p> You can create an instance of the <code>PollingBuilder</code> class so that you can write <code>PollingProgram</code>s with syntactic sugar: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;polling&nbsp;=&nbsp;<span style="color:teal;">PollingBuilder</span>&nbsp;() </pre> </p> <p> This enables you to write <code>polling</code> computation expressions. You'll see examples of this shortly. </p> <p> Most of the code you've seen here is automated in Haskell. This means that while you'll have to explicitly write it in F#, it follows <a href="/2017/08/07/f-free-monad-recipe">a recipe</a>. Once you get the hang of it, it doesn't take much time. The maintenance overhead of the code is also minimal, because you're essentially implementing a universal abstraction. It's not going to change. </p> <h3 id="aac7611a87824bc8a86bc341208c9d15"> Support functions <a href="#aac7611a87824bc8a86bc341208c9d15" title="permalink">#</a> </h3> <p> Continuing the port of the previous article's Haskell code, you can write a pair of support functions. These are small <code>PollingProgram</code> values: </p> <p> <pre><span style="color:green;">//&nbsp;IdleDuration&nbsp;-&gt;&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;a,bool&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">shouldIdle</span>&nbsp;(<span style="color:navy;">IdleDuration</span>&nbsp;d)&nbsp;stopBefore&nbsp;=&nbsp;<span style="color:blue;">polling</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;now&nbsp;=&nbsp;<span style="color:teal;">Polling</span>.currentTime &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;now&nbsp;+&nbsp;d&nbsp;&lt;&nbsp;stopBefore&nbsp;}</pre> </p> <p> This <code>shouldIdle</code> function uses the <code>polling</code> computation expression defined above. It first uses the above <code>Polling.currentTime</code> value to get the current time. While <code>Polling.currentTime</code> is a value of the type <code>PollingProgram&lt;'b,DateTimeOffset&gt;</code>, the <code>let!</code> binding makes <code>now</code> a simple <code>DateTimeOffset</code> value. Computation expressions give you the same sort of syntactic sugar that <code>do</code> notation does in Haskell. </p> <p> If you add <code>now</code> to <code>d</code>, you get a new <code>DateTimeOffset</code> value that represents the time that the program will resume, <em>if</em> it decides to suspend itself for the idle duration. If this time is before <code>stopBefore</code>, the return value is <code>true</code>; otherwise, it's <code>false</code>. Similar to the Haskell example, the return value of <code>shouldIdle</code> isn't just <code>bool</code>, but rather <code>PollingProgram&lt;'a,bool&gt;</code>, because it all takes place inside the <code>polling</code> computation expression. </p> <p> The function looks impure, but it <em>is</em> pure. </p> <p> In the same vein, you can implement a <code>shouldPoll</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;CycleDuration&nbsp;-&gt;&nbsp;TimeSpan</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">toTotalCycleTimeSpan</span>&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(<span style="color:navy;">PollDuration</span>&nbsp;pd)&nbsp;=&nbsp;x.PollDuration &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(<span style="color:navy;">HandleDuration</span>&nbsp;hd)&nbsp;=&nbsp;x.HandleDuration &nbsp;&nbsp;&nbsp;&nbsp;pd&nbsp;+&nbsp;hd <span style="color:green;">//&nbsp;TimeSpan&nbsp;-&gt;&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;CycleDuration&nbsp;list&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;a,bool&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">shouldPoll</span>&nbsp;estimatedDuration&nbsp;stopBefore&nbsp;statistics&nbsp;=&nbsp;<span style="color:blue;">polling</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expectedHandleDuration&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statistics &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">toTotalCycleTimeSpan</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Statistics</span>.<span style="color:navy;">calculateExpectedDuration</span>&nbsp;estimatedDuration &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;now&nbsp;=&nbsp;<span style="color:teal;">Polling</span>.currentTime &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;now&nbsp;+&nbsp;expectedHandleDuration&nbsp;&lt;&nbsp;stopBefore&nbsp;}</pre> </p> <p> This function uses two helper functions: <code>toTotalCycleTimeSpan</code> and <code>Statistics.calculateExpectedDuration</code>. I've included <code>toTotalCycleTimeSpan</code> in the code shown here, while I'm skipping <code>Statistics.calculateExpectedDuration</code>, because it hasn't changed since the code I show in <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">my Pluralsight course</a>. You can also see the function in the <a href="https://github.com/ploeh/PollingConsumer">GitHub repository accompanying this article</a>. </p> <p> Compared to <code>shouldIdle</code>, the <code>shouldPoll</code> function needs an extra (pure) step in order to figure out the <code>expectedHandleDuration</code>, but from there, the two functions are similar. </p> <h3 id="9831e5727b654bacb37a5cfb46b97d5a"> Transitions <a href="#9831e5727b654bacb37a5cfb46b97d5a" title="permalink">#</a> </h3> <p> All building blocks are now ready for the finite state machine. In order to break the problem into manageable pieces, you can write a function for each state. Such a function should take as input the data associated with a particular state, and return the next state, based on the input. </p> <p> The simplest transition is when the program reaches the <em>end</em> state, because there's no way out of that state: </p> <p> <pre><span style="color:green;">//&nbsp;CycleDuration&nbsp;list&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;a,State&lt;&#39;b&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">transitionFromStopped</span>&nbsp;s&nbsp;=&nbsp;<span style="color:blue;">polling</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">StoppedState</span>&nbsp;s&nbsp;}</pre> </p> <p> The data contained in a <code>StoppedState</code> case has the type <code>CycleDuration list</code>, so the <code>transitionFromStopped</code> function simply lifts such a list to a <code>PollingProgram</code> value by returning a <code>StoppedState</code> value from within a <code>polling</code> computation expression. </p> <p> Slightly more complex, but still simple, is the transition out of the <em>received</em> state. There's no branching logic involved. You just have to handle the message, measure how much time it takes, append the measurements to previous statistics, and return to the <em>ready</em> state: </p> <p> <pre><span style="color:green;">//&nbsp;CycleDuration&nbsp;list&nbsp;*&nbsp;PollDuration&nbsp;*&nbsp;&#39;a&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;a,State&lt;&#39;b&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">transitionFromReceived</span>&nbsp;(statistics,&nbsp;pd,&nbsp;msg)&nbsp;=&nbsp;<span style="color:blue;">polling</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;hd&nbsp;=&nbsp;<span style="color:teal;">Polling</span>.<span style="color:navy;">handle</span>&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;PollDuration&nbsp;=&nbsp;pd;&nbsp;HandleDuration&nbsp;=&nbsp;hd&nbsp;}&nbsp;<span style="color:navy;">::</span>&nbsp;statistics &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">ReadyState</span>&nbsp;}</pre> </p> <p> This function uses the <code>Polling.handle</code> convenience function to handle the input message. Although the <code>handle</code> function returns a <code>PollingProgram&lt;'a,HandleDuration&gt;</code> value, the <code>let!</code> binding inside of a <code>polling</code> computation expression makes <code>hd</code> a <code>HandleDuration</code> value. </p> <p> The data contained within a <code>ReceivedMessageState</code> case is a <code>CycleDuration list * PollDuration * 'msg</code> tuple. That's the input argument to the <code>transitionFromReceived</code> function, which immediately pattern-matches the tuple's three elements into <code>statistics</code>, <code>pd</code>, and <code>msg</code>. </p> <p> The <code>pd</code> element is the <code>PollDuration</code> - i.e. the time it took to reach the <em>received</em> state. The <code>hd</code> value returned by <code>Polling.handle</code> gives you the time it took to handle the message. From those two values you can create a new <code>CycleDuration</code> value, and cons (<code>::</code>) it onto the previous <code>statistics</code>. This returns an updated list of statistics that you can pipe to the <code>ReadyState</code> case constructor. </p> <p> <code>ReadyState</code> in itself creates a new <code>State&lt;'msg&gt;</code> value, but since all of this takes place inside a <code>polling</code> computation expression, the return type of the function becomes <code>PollingProgram&lt;'a,State&lt;'b&gt;&gt;</code>. </p> <p> The <code>transitionFromReceived</code> function handles the state when the program has received a message, but you also need to handle the state when no message was received: </p> <p> <pre><span style="color:green;">//&nbsp;IdleDuration&nbsp;-&gt;&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;CycleDuration&nbsp;list&nbsp;*&nbsp;&#39;a</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;b,State&lt;&#39;c&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">transitionFromNoMessage</span>&nbsp;d&nbsp;stopBefore&nbsp;(statistics,&nbsp;_)&nbsp;=&nbsp;<span style="color:blue;">polling</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;b&nbsp;=&nbsp;<span style="color:navy;">shouldIdle</span>&nbsp;d&nbsp;stopBefore &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;b&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">do!</span>&nbsp;<span style="color:teal;">Polling</span>.<span style="color:navy;">idle</span>&nbsp;d&nbsp;|&gt;&nbsp;<span style="color:teal;">Polling</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">ignore</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">ReadyState</span>&nbsp;statistics &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">StoppedState</span>&nbsp;statistics&nbsp;}</pre> </p> <p> This function first calls the <code>shouldIdle</code> support function. Similar to Haskell, you can see how you can compose larger <code>PollingProgram</code>s from smaller <code>PollingProgram</code> values - just like you can compose 'normal' functions from smaller functions. </p> <p> With the syntactic sugar in place, <code>b</code> is simply a <code>bool</code> value that you can use in a standard <code>if/then/else</code> expression. If <code>b</code> is <code>false</code>, then return a <code>StoppedState</code> value; otherwise, continue with the next steps. </p> <p> <code>Polling.idle</code> returns the duration of the suspension, but you don't actually need this data, so you can <code>ignore</code> it. When <code>Polling.idle</code> returns, you can return a <code>ReadyState</code> value. </p> <p> It may look as though that <code>do!</code> expression is a blocking call, but it really isn't. The <code>transitionFromNoMessage</code> function only builds an Abstract Syntax Tree, where one of the instructions suggests that an interpreter could block. Unless evaluated by an impure interpreter, <code>transitionFromNoMessage</code> is pure. </p> <p> The final transition is the most complex, because there are three possible outcomes: </p> <p> <pre><span style="color:green;">//&nbsp;TimeSpan&nbsp;-&gt;&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;CycleDuration&nbsp;list</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;a,State&lt;&#39;a&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">transitionFromReady</span>&nbsp;estimatedDuration&nbsp;stopBefore&nbsp;statistics&nbsp;=&nbsp;<span style="color:blue;">polling</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;b&nbsp;=&nbsp;<span style="color:navy;">shouldPoll</span>&nbsp;estimatedDuration&nbsp;stopBefore&nbsp;statistics &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;b&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;pollResult&nbsp;=&nbsp;<span style="color:teal;">Polling</span>.poll &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;pollResult&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Some</span>&nbsp;msg,&nbsp;pd&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">ReceivedMessageState</span>&nbsp;(statistics,&nbsp;pd,&nbsp;msg) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">None</span>,&nbsp;pd&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">NoMessageState</span>&nbsp;(statistics,&nbsp;pd) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">StoppedState</span>&nbsp;statistics&nbsp;}</pre> </p> <p> In the same way that <code>transitionFromNoMessage</code> uses <code>shouldIdle</code>, the <code>transitionFromReady</code> function uses the <code>shouldPoll</code> support function to decide whether or not to keep going. If <code>b</code> is <code>false</code>, it returns a <code>StoppedState</code> value. </p> <p> Otherwise, it goes on to <code>poll</code>. Thanks to all the syntactic sugar, <code>pollResult</code> is an <code>'a option * PollDuration</code> value. As always, when you have a discriminated union, you can handle all cases with pattern matching (and the compiler will help you keep track of whether or not you've handled all of them). </p> <p> In the <code>Some</code> case, you have a message, and the duration it took to poll for that message. This is all the data you need to return a <code>ReceivedMessageState</code> value. </p> <p> In the <code>None</code> case, you also have the poll duration <code>pd</code>; return a <code>NoMessageState</code> value. </p> <p> That's four transition functions that you can combine in a single function that, for any state, returns a new state: </p> <p> <pre><span style="color:green;">//&nbsp;TimeSpan&nbsp;-&gt;&nbsp;IdleDuration&nbsp;-&gt;&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;State&lt;&#39;a&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;PollingProgram&lt;&#39;a,State&lt;&#39;a&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">transition</span>&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">ReadyState</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">transitionFromReady</span>&nbsp;estimatedDuration&nbsp;stopBefore&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">ReceivedMessageState</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">transitionFromReceived</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">NoMessageState</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">transitionFromNoMessage</span>&nbsp;idleDuration&nbsp;stopBefore&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">StoppedState</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">transitionFromStopped</span>&nbsp;s</pre> </p> <p> You simply pattern-match the (implicit) input argument with the four state cases, and call the appropriate transition function for each case. </p> <h3 id="d5f1000dfe394815b86960611c41b2f0"> Interpretation <a href="#d5f1000dfe394815b86960611c41b2f0" title="permalink">#</a> </h3> <p> The <code>transition</code> function is pure. It returns a <code>PollingProgram</code> value. How do you turn it into something that performs real work? </p> <p> You write an interpreter: </p> <p> <pre><span style="color:green;">//&nbsp;PollingProgram&lt;Msg,&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">interpret</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Pure</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">CurrentTime</span>&nbsp;<span style="color:navy;">next</span>)&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">DateTimeOffset</span>.Now&nbsp;|&gt;&nbsp;<span style="color:navy;">next</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">Poll</span>&nbsp;<span style="color:navy;">next</span>)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">Imp</span>.<span style="color:navy;">poll</span>&nbsp;()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">next</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">Handle</span>&nbsp;(msg,&nbsp;<span style="color:navy;">next</span>))&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">Imp</span>.<span style="color:navy;">handle</span>&nbsp;msg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">next</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Free</span>&nbsp;(<span style="color:navy;">Idle</span>&nbsp;(d,&nbsp;<span style="color:navy;">next</span>))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">Imp</span>.<span style="color:navy;">idle</span>&nbsp;d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">next</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span></pre> </p> <p> A <code>PollingProgram</code> is either a <code>Pure</code> or a <code>Free</code> case. In the <code>Free</code> case, the contained data is a <code>PollingInstruction</code> value, which can be one of four separate cases. With pattern matching, the interpreter handles all five cases. </p> <p> In the <code>Pure</code> case, it returns the value, but in all the <code>Free</code> cases, it recursively calls itself after having first followed the instruction in each <code>PollingInstruction</code> case. For instance, when the instruction is <code>CurrentTime</code>, it invokes <code>DateTimeOffset.Now</code>, passes the return value (a <code>DateTimeOffset</code> value) to the <code>next</code> continuation, and then recursively calls <code>interpret</code>. The next instruction, then, could be another <code>Free</code> case, or it could be <code>Pure</code>. </p> <p> The other three instruction cases delegate to implementation functions defined in an <code>Imp</code> module. I'm not going to show them here. They're normal, although impure, F# functions. </p> <h3 id="323b3ab95c484a4e9c11934a93e9b58d"> Execution <a href="#323b3ab95c484a4e9c11934a93e9b58d" title="permalink">#</a> </h3> <p> You're almost done. You have a function that returns a new state for any given input state, as well as an interpreter. You need a function that can repeat this in a loop until it reaches <code>StoppedState</code>: </p> <p> <pre><span style="color:green;">//&nbsp;TimeSpan&nbsp;-&gt;&nbsp;IdleDuration&nbsp;-&gt;&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;State&lt;Msg&gt;&nbsp;-&gt;&nbsp;State&lt;Msg&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">run</span>&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;s&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ns&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">PollingConsumer</span>.<span style="color:navy;">transition</span>&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">interpret</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;ns&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:teal;">PollingConsumer</span>.<span style="color:navy;">StoppedState</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;ns &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">run</span>&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;ns</pre> </p> <p> This function calls <code>PollingConsumer.transition</code> with the input state <code>s</code>, which returns a new <code>PollingProgram&lt;Msg,PollingConsumer.State&lt;Msg&gt;&gt;</code> value that you can pipe to the <code>interpret</code> function. That gives you the new state <code>ns</code>. If <code>ns</code> is a <code>StoppedState</code>, you return; otherwise, you recurse into <code>run</code> for another round. </p> <p> Finally, you can write the entry point for the application: </p> <p> <pre>[&lt;<span style="color:teal;">EntryPoint</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">main</span>&nbsp;_&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;timeAtEntry&nbsp;=&nbsp;<span style="color:teal;">DateTimeOffset</span>.Now &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">printOnEntry</span>&nbsp;timeAtEntry &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stopBefore&nbsp;=&nbsp;timeAtEntry&nbsp;+&nbsp;limit &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;estimatedDuration&nbsp;=&nbsp;<span style="color:teal;">TimeSpan</span>.<span style="color:navy;">FromSeconds</span>&nbsp;2. &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;idleDuration&nbsp;=&nbsp;<span style="color:teal;">TimeSpan</span>.<span style="color:navy;">FromSeconds</span>&nbsp;5.&nbsp;|&gt;&nbsp;<span style="color:navy;">IdleDuration</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;durations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">PollingConsumer</span>.<span style="color:navy;">ReadyState</span>&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">run</span>&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">PollingConsumer</span>.<span style="color:navy;">durations</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:teal;">PollingConsumer</span>.<span style="color:navy;">toTotalCycleTimeSpan</span> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">printOnExit</span>&nbsp;timeAtEntry&nbsp;durations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Return&nbsp;0.&nbsp;This&nbsp;indicates&nbsp;success.</span> &nbsp;&nbsp;&nbsp;&nbsp;0</pre> </p> <p> This defines an estimated duration of 2 seconds, an idle duration of 5 seconds, and a maximum run time of 60 seconds (<code>limit</code>). The initial state is <code>ReadyState</code> with no prior statistics. Pass all these arguments to the <code>run</code> function, and you have a running program. </p> <p> This function also uses a few printout functions that I'm not going to show here. When you run the program, you should see output like this: </p> <p> <pre>Started polling at 11:18:28. Polling <span style="color:green;">Handling</span> Polling <span style="color:green;">Handling</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:green;">Handling</span> Polling <span style="color:green;">Handling</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:green;">Handling</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:orange;">Sleeping</span> Polling <span style="color:green;">Handling</span> Stopped polling at 11:19:26. Elapsed time: <span style="color:green;">00:00:58.4428980</span>. Handled 6 message(s). Average duration: 00:00:01.0550346 Standard deviation: 00:00:00.3970599</pre> </p> <p> It does, indeed, exit before 60 seconds have elapsed. </p> <h3 id="bcc4d398721b427ea6b3b2e839527417"> Summary <a href="#bcc4d398721b427ea6b3b2e839527417" title="permalink">#</a> </h3> <p> You can model long-running interactions with an Abstract Syntax Tree. Without computation expressions, writing programs as 'raw' ASTs would be cumbersome, but turning the AST into a (free) monad makes it all quite palatable. </p> <p> Haskell code with a free monad can be ported to F#, although some boilerplate code is required. That code, however, is unlikely to be much of a burden, because it follows a well-known recipe that implements a universal abstraction. </p> <p> For more details on how to write free monads in F#, see <a href="/2017/07/10/pure-interactions">Pure interactions</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pure times in Haskell https://blog.ploeh.dk/2017/06/28/pure-times-in-haskell 2017-06-28T07:54:00+00:00 Mark Seemann <div id="post"> <p> <em>A Polling Consumer implementation written in Haskell.</em> </p> <p> As you can read in the <a href="/2017/06/27/pure-times">introductory article</a>, I've come to realise that the <a href="http://www.enterpriseintegrationpatterns.com/PollingConsumer.html">Polling Consumer</a> that I <a href="/2015/08/10/type-driven-development">originally wrote in F#</a> isn't particularly functional. Being the friendly and productive language that it is, F# doesn't protect you from mixing pure and impure code, but <a href="https://www.haskell.org">Haskell</a> does. For that reason, you can develop a prototype in Haskell, and later port it to F#, if you want to learn how to solve the problem in a strictly functional way. </p> <p> To recapitulate, the task is to implement a Polling Consumer that runs for a predefined duration, after which it exits (so that it can be restarted by a scheduler). </p> <p> <img src="/content/binary/polling-consumer-finite-state-machine.png" alt="Polling Consumer state machine transition diagram"> </p> <p> The program is a finite state machine that moves between four states. From the <em>ready</em> state, it'll need to decide whether to poll for a new message or exit. Polling and handling takes time (and at compile-time we don't know how long), and the program ought to stop at a pre-defined time. If it gets too close to that time, it should exit, but otherwise, it should attempt to handle a message (and keep track of how long this takes). You can read a more elaborate description of the problem in the <a href="/2015/08/10/type-driven-development">original article</a>. </p> <h3 id="99ac8ff6c56c46d6a7346e23b9e4b3e1"> State data types <a href="#99ac8ff6c56c46d6a7346e23b9e4b3e1" title="permalink">#</a> </h3> <p> The premise in that initial article was that F#'s type system is so powerful that it can aid you in designing a good solution. Haskell's type system is even more powerful, so it can give you even better help. </p> <p> The Polling Consumer program must measure and keep track of how long it takes to poll, handle a message, or idle. All of these are durations. In Haskell, we can represent them as <code>NominalDiffTime</code> values. I'm a bit concerned, though, that if I represent all of these durations as <code>NominalDiffTime</code> values, I may accidentally use a poll duration where I really need a handle duration, and so on. Perhaps I'm being overly cautious, but I like to get help from the type system. In the words of <a href="http://hmemcpy.com">Igal Tabachnik</a>, <em><a href="https://twitter.com/hmemcpy/status/867647943108681728">types prevent typos</a>:</em> </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">PollDuration</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">PollDuration</span>&nbsp;<span style="color:#dd0000;">NominalDiffTime</span>&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Show</span>) <span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">IdleDuration</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">IdleDuration</span>&nbsp;<span style="color:#dd0000;">NominalDiffTime</span>&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Show</span>) <span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">HandleDuration</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">HandleDuration</span>&nbsp;<span style="color:#dd0000;">NominalDiffTime</span>&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Show</span>) <span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">CycleDuration</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">CycleDuration</span> &nbsp;&nbsp;{&nbsp;pollDuration&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">PollDuration</span>,&nbsp;handleDuration&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">HandleDuration</span>&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Show</span>)</pre> </p> <p> This simply declares that <code>PollDuration</code>, <code>IdleDuration</code>, and <code>HandleDuration</code> are all <code>NominalDiffTime</code> values, but you can't mistakenly use a <code>PollDuration</code> where a <code>HandleDuration</code> is required, and so on. </p> <p> In addition to those three types of duration, I also define a <code>CycleDuration</code>. This is the data that I actually need to keep track of: <em>how long does it take to handle a single message?</em> I'm assuming that polling for a message is an I/O-bound operation, so it may take significant time. Likewise, handling a message may take time. When deciding whether to exit or handle a new message, both durations count. Instead of defining <code>CycleDuration</code> as a <code>newtype</code> alias for <code>NominalDiffTime</code>, I decided to define it as a record type comprised of a <code>PollDuration</code> and a <code>HandleDuration</code>. It's not that I'm really interested in keeping track of these two values individually, but it protects me from making stupid mistakes. I can only create a <code>CycleDuration</code> value if I have both a <code>PollDuration</code> and a <code>HandleDuration</code> value. </p> <p> In short, I'm trying to combat <a href="/2011/05/25/DesignSmellPrimitiveObsession">primitive obsession</a>. </p> <p> With these duration types in place, you can define the states of the finite state machine: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">PollingState</span>&nbsp;msg&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Ready</span>&nbsp;[<span style="color:#dd0000;">CycleDuration</span>] &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">ReceivedMessage</span>&nbsp;[<span style="color:#dd0000;">CycleDuration</span>]&nbsp;<span style="color:#dd0000;">PollDuration</span>&nbsp;msg &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">NoMessage</span>&nbsp;[<span style="color:#dd0000;">CycleDuration</span>]&nbsp;<span style="color:#dd0000;">PollDuration</span> &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Stopped</span>&nbsp;[<span style="color:#dd0000;">CycleDuration</span>] &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>)</pre> </p> <p> Like the original F# code, state data can be represented as a sum type, with a case for each state. In all four cases, a <code>CycleDuration</code> list keeps track of the observed message-handling statistics. This is the way the program should attempt to calculate whether it's safe to handle another message, or exit. Two of the cases (<code>ReceivedMessage</code> and <code>NoMessage</code>) also contain a <code>PollDuration</code>, which informs the program about the duration of the poll operation that caused it to reach that state. Additionally, the <code>ReceivedMessage</code> case contains a message of the generic type <code>msg</code>. This makes the entire <code>PollingState</code> type generic. A message can be of any type: a string, a number, or a complex data structure. The Polling Consumer program doesn't care, because it doesn't handle messages; it only schedules the polling. </p> <p> This is reminiscent of the previous F# attempt, with the most notable difference that it doesn't attempt to capture durations as <code>Timed&lt;'a&gt;</code> values. It <em>does</em> capture durations, but not when the operations started and stopped. So how will it know what time it is? </p> <h3 id="4fdfc9e11c134fe8ad9296bc1d686b1f"> Interactions as pure values <a href="#4fdfc9e11c134fe8ad9296bc1d686b1f" title="permalink">#</a> </h3> <p> This is the heart of the matter. The Polling Consumer must constantly look at the clock. It's under a deadline, and it must also measure durations of poll, handle, and idle operations. All of this is non-deterministic, so not <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>. The program has to interact with impure operations during its entire lifetime. In fact, its ultimate decision to exit will be based on impure data. How can you model this in a pure fashion? </p> <p> You can model long-running (impure) interactions by defining a small instruction set for an Abstract Syntax Tree (AST). That sounds intimidating, but once you get the hang of it, it becomes routine. In <a href="/2017/07/10/pure-interactions">later articles</a>, I'll expand on this, but for now I'll refer you to <a href="https://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle-2/#way13">an excellent article</a> by <a href="https://fsharpforfunandprofit.com">Scott Wlaschin</a>, who explains the approach in F#. </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">PollingInstruction</span>&nbsp;msg&nbsp;next&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">CurrentTime</span>&nbsp;(<span style="color:#dd0000;">UTCTime</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;next) &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Poll</span>&nbsp;((<span style="color:#dd0000;">Maybe</span>&nbsp;msg,&nbsp;<span style="color:#dd0000;">PollDuration</span>)&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;next) &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Handle</span>&nbsp;msg&nbsp;(<span style="color:#dd0000;">HandleDuration</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;next) &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Idle</span>&nbsp;<span style="color:#dd0000;">IdleDuration</span>&nbsp;(<span style="color:#dd0000;">IdleDuration</span>&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;next) &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Functor</span>)</pre> </p> <p> This <code>PollingInstruction</code> sum type defines four cases of interaction. Each case is <ol> <li>named after the interaction</li> <li>defines the type of data used as input arguments for the interaction</li> <li>and also defines a <em>continuation</em>; that is: a function that will be executed with the return value of the interaction</li> </ol> Half of the above cases are degenerate, but the <code>Handle</code> case contains all three elements: the interaction is named <code>Handle</code>, the input to the interaction is of the generic type <code>msg</code>, and the continuation is a function that takes a <code>HandleDuration</code> value as input, and returns a value of the generic type <code>next</code>. In other words, the interaction takes a <code>msg</code> value as input, and returns a <code>HandleDuration</code> value as output. That duration is the time it took to handle the input message. (The intent is that the operation that 'implements' this interaction also actually <em>handles</em> the message, whatever that means.) </p> <p> Likewise, the <code>Idle</code> interaction takes an <code>IdleDuration</code> as input, and also returns an <code>IdleDuration</code>. The intent here is that the 'implementation' of the interaction suspends itself for the duration of the input value, and returns the time it actually spent in suspension (which is likely to be slightly longer than the requested duration). </p> <p> Both <code>CurrentTime</code> and <code>Poll</code>, on the other hand, are degenerate, because they take no input. You don't need to supply any input argument to read the current time. You could model that interaction as taking <code>()</code> ('unit') as an input argument (<code>CurrentTime () (UTCTime -&gt; next)</code>), but the <code>()</code> is redundant and can be omitted. The same is the case for the <code>Poll</code> case, which returns a <code>Maybe msg</code> and how long the poll took. </p> <p> (The <code>PollingInstruction</code> sum type defines four cases, which is also the number of cases defined by <code>PollingState</code>. This is a coincidence; don't read anything into it.) </p> <p> The <code>PollingInstruction</code> type is generic in a way that you can make it a <code>Functor</code>. Haskell can do this for you automatically, using the <code>DeriveFunctor</code> language extension; that's what <code>deriving (Functor)</code> does. If you'd like to see how to explicitly make such a data structure a functor, please refer to the F# example; F# can't automatically derive functors, so you'll have to do it manually. </p> <p> Since <code>PollingInstruction</code> is a <code>Functor</code>, we can make a <code>Monad</code> out of it. <a href="https://twitter.com/hmemcpy/status/771359835514368000">You use a free monad, which allows you to build a monad from any functor</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#dd0000;">PollingProgram</span>&nbsp;msg&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">PollingInstruction</span>&nbsp;msg) </pre> </p> <p> In Haskell, it's literally a one-liner, but in F# you'll have to write the code yourself. Thus, if you're interested in learning how this magic happens, I'm going to dissect this step in the next article. </p> <p> The motivation for defining a <code>Monad</code> is that we get automatic syntactic sugar for our <code>PollingProgram</code> ASTs, via Haskell's <code>do</code> notation. In F#, we're going to write a computation expression builder to achieve the same effect. </p> <p> The final building blocks for the specialised <code>PollingProgram</code> API is a convenience function for each case: </p> <p> <pre><span style="color:#600277;">currentTime</span>&nbsp;::&nbsp;<span style="color:blue;">PollingProgram</span>&nbsp;msg&nbsp;<span style="color:blue;">UTCTime</span> currentTime&nbsp;<span style="color:#666666;">=</span>&nbsp;liftF&nbsp;(<span style="color:#dd0000;">CurrentTime</span>&nbsp;id) <span style="color:#600277;">poll</span>&nbsp;::&nbsp;<span style="color:blue;">PollingProgram</span>&nbsp;msg&nbsp;(Maybe&nbsp;msg,&nbsp;<span style="color:blue;">PollDuration</span>) poll&nbsp;<span style="color:#666666;">=</span>&nbsp;liftF&nbsp;(<span style="color:#dd0000;">Poll</span>&nbsp;id) <span style="color:#600277;">handle</span>&nbsp;::&nbsp;msg&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">PollingProgram</span>&nbsp;msg&nbsp;<span style="color:blue;">HandleDuration</span> handle&nbsp;msg&nbsp;<span style="color:#666666;">=</span>&nbsp;liftF&nbsp;(<span style="color:#dd0000;">Handle</span>&nbsp;msg&nbsp;id) <span style="color:#600277;">idle</span>&nbsp;::&nbsp;<span style="color:blue;">IdleDuration</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">PollingProgram</span>&nbsp;msg&nbsp;<span style="color:blue;">IdleDuration</span> idle&nbsp;d&nbsp;<span style="color:#666666;">=</span>&nbsp;liftF&nbsp;(<span style="color:#dd0000;">Idle</span>&nbsp;d&nbsp;id)</pre> </p> <p> More one-liners, as you can tell. These all use <code>liftF</code> to turn <code>PollingInstruction</code> cases into <code>PollingProgram</code> values. The degenerate cases <code>CurrentTime</code> and <code>Poll</code> simply become values, whereas the complete cases become (pure) functions. </p> <h3 id="30b1b5917ae944a88715541b0ca0b83d"> Support functions <a href="#30b1b5917ae944a88715541b0ca0b83d" title="permalink">#</a> </h3> <p> You may have noticed that until now, I haven't written much 'code' in the sense that most people think of it. It's mostly been type declarations and a few one-liners. A strong and sophisticated type system like Haskell's enable you to shift some of the programming burden from 'real programming' to type definitions, but you'll still have to write some code. </p> <p> Before we get to the state transitions proper, we'll look at some support functions. These will, I hope, serve as a good introduction to how to use the <code>PollingProgram</code> API. </p> <p> One decision the Polling Consumer program has to make is to decide whether it should suspend itself for a short time. That's easy to express using the API: </p> <p> <pre><span style="color:#600277;">shouldIdle</span>&nbsp;::&nbsp;<span style="color:blue;">IdleDuration</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">UTCTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">PollingProgram</span>&nbsp;msg&nbsp;Bool shouldIdle&nbsp;(<span style="color:#dd0000;">IdleDuration</span>&nbsp;d)&nbsp;stopBefore&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;now&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;currentTime &nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;d&nbsp;<span style="color:#666666;">`addUTCTime`</span>&nbsp;now&nbsp;<span style="color:#666666;">&lt;</span>&nbsp;stopBefore</pre> </p> <p> The <code>shouldIdle</code> function returns a small program that, when evaluated, will decide whether or not to suspend itself. It first reads the current time using the above <code>currentTime</code> value. While <code>currentTime</code> has the type <code>PollingProgram msg UTCTime</code>, due to Haskell's <code>do</code> notation, the <code>now</code> value simply has the type <code>UTCTime</code>. This enables you to use the built-in <code>addUTCTime</code> function (here written using infix notation) to add <code>now</code> to <code>d</code> (a <code>NominalDiffTime</code> value, due to pattern matching into <code>IdleDuration</code>). </p> <p> Adding the idle duration <code>d</code> to the current time <code>now</code> gives you the time the program would resume, were it to suspend itself. The <code>shouldIdle</code> function compares that time to the <code>stopBefore</code> argument (another <code>UTCTime</code> value). If the time the program would resume is before the time it ought to stop, the return value is <code>True</code>; otherwise, it's <code>False</code>. </p> <p> Since the entire function is defined within a <code>do</code> block, the return type isn't just <code>Bool</code>, but rather <code>PollingProgram msg Bool</code>. It's a little <code>PollingProgram</code> AST, but it looks like imperative code. </p> <p> You sometimes hear the bon mot that <em>Haskell is the world's greatest imperative language</em>. The combination of free monads and <code>do</code> notation certainly makes it easy to define small grammars (dare I say <a href="https://en.wikipedia.org/wiki/Domain-specific_language">DSL</a>s?) that look like imperative code, while still being strictly functional. </p> <p> The crux is that <code>shouldIdle</code> is pure. It looks impure, but it's not. It's an Abstract Syntax Tree, and it only becomes non-deterministic if interpreted by an impure interpreter (more on that later). </p> <p> The purpose of <code>shouldIdle</code> is to decide whether or not to idle or exit. If the program decides to idle, it should return to the <em>ready</em> state, as per the above state diagram. In this state, it needs to decide whether or not to poll for a message. If there's a message, it should be handled, and all of that takes time. In the <em>ready</em> state, then, the program must figure out how much time it thinks that handling a message will take. </p> <p> One way to do that is to consider the observed durations so far. This helper function calculates the expected duration based on the average and standard deviation of the previous durations: </p> <p> <pre><span style="color:#600277;">calculateExpectedDuration</span>&nbsp;::&nbsp;<span style="color:blue;">NominalDiffTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;[<span style="color:#dd0000;">CycleDuration</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">NominalDiffTime</span> calculateExpectedDuration&nbsp;estimatedDuration&nbsp;<span style="color:blue;">[]</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;estimatedDuration calculateExpectedDuration&nbsp;_&nbsp;statistics&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;toEnum&nbsp;<span style="color:#666666;">$</span>&nbsp;fromEnum&nbsp;<span style="color:#666666;">$</span>&nbsp;avg&nbsp;<span style="color:#666666;">+</span>&nbsp;stdDev&nbsp;<span style="color:#666666;">*</span>&nbsp;<span style="color:#09885a;">3</span> &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#600277;">fromCycleDuration</span>&nbsp;::&nbsp;<span style="color:blue;">CycleDuration</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Float &nbsp;&nbsp;&nbsp;&nbsp;fromCycleDuration&nbsp;(<span style="color:#dd0000;">CycleDuration</span>&nbsp;(<span style="color:#dd0000;">PollDuration</span>&nbsp;pd)&nbsp;(<span style="color:#dd0000;">HandleDuration</span>&nbsp;hd))&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;toEnum&nbsp;<span style="color:#666666;">$</span>&nbsp;fromEnum&nbsp;<span style="color:#666666;">$</span>&nbsp;pd&nbsp;<span style="color:#666666;">+</span>&nbsp;hd &nbsp;&nbsp;&nbsp;&nbsp;durations&nbsp;<span style="color:#666666;">=</span>&nbsp;fmap&nbsp;fromCycleDuration&nbsp;statistics &nbsp;&nbsp;&nbsp;&nbsp;l&nbsp;<span style="color:#666666;">=</span>&nbsp;toEnum&nbsp;<span style="color:#666666;">$</span>&nbsp;length&nbsp;durations &nbsp;&nbsp;&nbsp;&nbsp;avg&nbsp;<span style="color:#666666;">=</span>&nbsp;sum&nbsp;durations&nbsp;<span style="color:#666666;">/</span>&nbsp;l &nbsp;&nbsp;&nbsp;&nbsp;stdDev&nbsp;<span style="color:#666666;">=</span>&nbsp;sqrt&nbsp;(sum&nbsp;(fmap&nbsp;(<span style="color:#666666;">\</span>x&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;(x&nbsp;<span style="color:#666666;">-</span>&nbsp;avg)&nbsp;<span style="color:#666666;">**</span>&nbsp;<span style="color:#09885a;">2</span>)&nbsp;durations)&nbsp;<span style="color:#666666;">/</span>&nbsp;l)</pre> </p> <p> I'm not going to dwell much on this function, as it's a normal, pure, mathematical function. The only feature I'll emphasise is that in order to call it, you must pass an <code>estimatedDuration</code> that will be used when <code>statistics</code> is empty. This is because you can't calculate the average of an empty list. This estimated duration is simply your wild guess at how long you think it'll take to handle a message. </p> <p> With this helper function, you can now write a small <code>PollingProgram</code> that decides whether or not to poll: </p> <p> <pre><span style="color:#600277;">shouldPoll</span>&nbsp;::&nbsp;<span style="color:blue;">NominalDiffTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">UTCTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;[<span style="color:#dd0000;">CycleDuration</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">PollingProgram</span>&nbsp;msg&nbsp;<span style="color:#dd0000;">Bool</span> shouldPoll&nbsp;estimatedDuration&nbsp;stopBefore&nbsp;statistics&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expectedHandleDuration&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculateExpectedDuration&nbsp;estimatedDuration&nbsp;statistics &nbsp;&nbsp;now&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;currentTime &nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;expectedHandleDuration&nbsp;<span style="color:#666666;">`addUTCTime`</span>&nbsp;now&nbsp;<span style="color:#666666;">&lt;</span>&nbsp;stopBefore</pre> </p> <p> Notice that the <code>shouldPoll</code> function looks similar to <code>shouldIdle</code>. As an extra initial step, it first calculates <code>expectedHandleDuration</code> using the above <code>calculateExpectedDuration</code> function. With that, it follows the same two steps as <code>shouldIdle</code>. </p> <p> This function is also pure, because it returns an AST. While it <em>looks</em> impure, it's not, because it doesn't actually <code>do</code> anything. </p> <h3 id="cb4725bdb617425b8885140832942103"> Transitions <a href="#cb4725bdb617425b8885140832942103" title="permalink">#</a> </h3> <p> Those are all the building blocks required to write the state transitions. In order to break down the problem in manageable chunks, you can write a transition function for each state. Such a function would return the next state, given a particular input state. </p> <p> While it'd be intuitive to begin with the <em>ready</em> state, let's instead start with the simplest transition. In the <em>end</em> state, nothing should happen, so the transition is a one-liner: </p> <p> <pre><span style="color:#600277;">transitionFromStopped</span>&nbsp;::&nbsp;<span style="color:blue;">Monad</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;[<span style="color:blue;">CycleDuration</span>]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;m&nbsp;(<span style="color:blue;">PollingState</span>&nbsp;msg) transitionFromStopped&nbsp;statistics&nbsp;<span style="color:#666666;">=</span>&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">Stopped</span>&nbsp;statistics</pre> </p> <p> Once stopped, the program stays in the <code>Stopped</code> state. This function simply takes a list of <code>CycleDuration</code> values and elevates them to a monad type. Notice that the return value isn't specifically a <code>PollingProgram</code>, but any monad. Since <code>PollingProgram</code> is a monad, that'll work too, though. </p> <p> Slightly more complicated than <code>transitionFromStopped</code> is the transition from the <em>received</em> state. There's no branching in that case; simply handle the message, measure how long it took, add the observed duration to the statistics, and transition back to <em>ready:</em> </p> <p> <pre><span style="color:#600277;">transitionFromReceived</span>&nbsp;::&nbsp;[<span style="color:blue;">CycleDuration</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">PollDuration</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">PollingProgram</span>&nbsp;msg&nbsp;(<span style="color:#dd0000;">PollingState</span>&nbsp;msg) transitionFromReceived&nbsp;statistics&nbsp;pd&nbsp;msg&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;hd&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;handle&nbsp;msg &nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">Ready</span>&nbsp;(<span style="color:#dd0000;">CycleDuration</span>&nbsp;pd&nbsp;hd&nbsp;<span style="color:#666666;">:</span>&nbsp;statistics)</pre> </p> <p> Again, this <em>looks</em> impure, but the return type is <code>PollingProgram msg (PollingState msg)</code>, indicating that the return value is an AST. As is not uncommon in Haskell, the type declaration is larger than the implementation. </p> <p> Things get slightly more interesting in the <em>no message</em> state. Here you get to use the above <code>shouldIdle</code> support function: </p> <p> <pre><span style="color:#600277;">transitionFromNoMessage</span>&nbsp;::&nbsp;<span style="color:blue;">IdleDuration</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">UTCTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;[<span style="color:#dd0000;">CycleDuration</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">PollingProgram</span>&nbsp;msg&nbsp;(<span style="color:#dd0000;">PollingState</span>&nbsp;msg) transitionFromNoMessage&nbsp;d&nbsp;stopBefore&nbsp;statistics&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;b&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;shouldIdle&nbsp;d&nbsp;stopBefore &nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;b &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;idle&nbsp;d&nbsp;<span style="color:#666666;">&gt;&gt;</span>&nbsp;return&nbsp;(<span style="color:#dd0000;">Ready</span>&nbsp;statistics) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">Stopped</span>&nbsp;statistics</pre> </p> <p> The first step in <code>transitionFromNoMessage</code> is calling <code>shouldIdle</code>. Thanks to Haskell's <code>do</code> notation, the <code>b</code> value is a simple <code>Bool</code> value that you can use to branch. If <code>b</code> is <code>True</code>, then first call <code>idle</code> and then return to the <code>Ready</code> state; otherwise, exit to the <code>Stopped</code> state. </p> <p> Notice how <code>PollingProgram</code> values are <em>composable</em>. For instance, <code>shouldIdle</code> defines a small <code>PollingProgram</code> that can be (re)used in a bigger program, such as in <code>transitionFromNoMessage</code>. </p> <p> Finally, from the <em>ready</em> state, the program can transition to three other states, so this is the most complex transition: </p> <p> <pre><span style="color:#600277;">transitionFromReady</span>&nbsp;::&nbsp;<span style="color:blue;">NominalDiffTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">UTCTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;[<span style="color:#dd0000;">CycleDuration</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">PollingProgram</span>&nbsp;msg&nbsp;(<span style="color:#dd0000;">PollingState</span>&nbsp;msg) transitionFromReady&nbsp;estimatedDuration&nbsp;stopBefore&nbsp;statistics&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;b&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;shouldPoll&nbsp;estimatedDuration&nbsp;stopBefore&nbsp;statistics &nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;b &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pollResult&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;poll &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;pollResult&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Just&nbsp;msg,&nbsp;pd)&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">ReceivedMessage</span>&nbsp;statistics&nbsp;pd&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Nothing&nbsp;,&nbsp;pd)&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">NoMessage</span>&nbsp;statistics&nbsp;pd &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">Stopped</span>&nbsp;statistics</pre> </p> <p> Like <code>transitionFromNoMessage</code>, the <code>transitionFromReady</code> function first calls a supporting function (this time <code>shouldPoll</code>) in order to make a decision. If <code>b</code> is <code>False</code>, the next state is <code>Stopped</code>; otherwise, the program moves on to the next step. </p> <p> The program polls for a message using the <code>poll</code> helper function defined above. While <code>poll</code> is a <code>PollingProgram msg (Maybe msg, PollDuration)</code> value, thanks to <code>do</code> notation, <code>pollResult</code> is a <code>Maybe msg, PollDuration</code> value. Matching on that value requires you to handle two separate cases: If a message was received (<code>Just msg</code>), then return a <code>ReceivedMessage</code> state with the message. Otherwise (<code>Nothing</code>), return a <code>NoMessage</code> state. </p> <p> With those four functions you can now define a function that can transition from any input state: </p> <p> <pre><span style="color:#600277;">transition</span>&nbsp;::&nbsp;<span style="color:blue;">NominalDiffTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">IdleDuration</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">UTCTime</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">PollingState</span>&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">PollingProgram</span>&nbsp;msg&nbsp;(<span style="color:#dd0000;">PollingState</span>&nbsp;msg) transition&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;state&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;state&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Ready</span>&nbsp;stats&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;transitionFromReady&nbsp;estimatedDuration&nbsp;stopBefore&nbsp;stats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">ReceivedMessage</span>&nbsp;stats&nbsp;pd&nbsp;msg&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;transitionFromReceived&nbsp;stats&nbsp;pd&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">NoMessage</span>&nbsp;stats&nbsp;_&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;transitionFromNoMessage&nbsp;idleDuration&nbsp;stopBefore&nbsp;stats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Stopped</span>&nbsp;stats&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;transitionFromStopped&nbsp;stats</pre> </p> <p> The <code>transition</code> function simply pattern-matches on the input <code>state</code> and delegates to each of the four above transition functions. </p> <h3 id="e5aa7ddb26a34933aa8e45ecbb3db208"> A short philosophical interlude <a href="#e5aa7ddb26a34933aa8e45ecbb3db208" title="permalink">#</a> </h3> <p> All code so far has been pure, although it may not look that way. At this stage, it may be reasonable to pause and consider: <em>what's the point, even?</em> </p> <p> After all, when interpreted, a <code>PollingProgram</code> can (and, in reality, almost certainly <em>will</em>) have impure behaviour. If we create an entire executable upon this abstraction, then we've essentially developed a big program with impure behaviour... </p> <p> Indeed we have, but the alternative would have been to write it all in the context of <code>IO</code>. If you'd done that, then you'd allow <em>any</em> non-deterministic, side-effecty behaviour anywhere in your program. At least with a <code>PollingProgram</code>, any reader will quickly learn that only a maximum of four impure operations can happen. In other words, you've managed to control and restrict the impurity to exactly those interactions you want to model. </p> <p> Not only that, but the type of impurity is immediately visible as part of a value's type. In a later article, you'll see how different impure interaction APIs can be composed. </p> <h3 id="05de325a6598458b89b65d31cd0a56fc"> Interpretation <a href="#05de325a6598458b89b65d31cd0a56fc" title="permalink">#</a> </h3> <p> At this point, you have a program in the form of an AST. How do you execute it? </p> <p> You write an interpreter: </p> <p> <pre><span style="color:#600277;">interpret</span>&nbsp;::&nbsp;<span style="color:blue;">PollingProgram</span>&nbsp;<span style="color:blue;">Message</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;a interpret&nbsp;program&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;runFree&nbsp;program&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Pure</span>&nbsp;r&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;return&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">CurrentTime</span>&nbsp;next)&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;getCurrentTime&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;interpret&nbsp;<span style="color:#666666;">.</span>&nbsp;next &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">Poll</span>&nbsp;next)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;pollImp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;interpret&nbsp;<span style="color:#666666;">.</span>&nbsp;next &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">Handle</span>&nbsp;msg&nbsp;next)&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;handleImp&nbsp;msg&nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;interpret&nbsp;<span style="color:#666666;">.</span>&nbsp;next &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Free</span>&nbsp;(<span style="color:#dd0000;">Idle</span>&nbsp;d&nbsp;next)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;idleImp&nbsp;d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;interpret&nbsp;<span style="color:#666666;">.</span>&nbsp;next</pre> </p> <p> When you turn a functor into a monad using the <code>Free</code> constructor (see above), your functor is wrapped in a general-purpose sum type with two cases: <code>Pure</code> and <code>Free</code>. Your functor is always contained in the <code>Free</code> case, whereas <code>Pure</code> is the escape hatch. This is where you return the value of the entire computation. </p> <p> An interpreter must match both <code>Pure</code> and <code>Free</code>. <code>Pure</code> is easy, because you simply return the result value. </p> <p> In the <code>Free</code> case, you'll need to match each of the four cases of <code>PollingInstruction</code>. In all four cases, you invoke an impure implementation function, pass its return value to <code>next</code>, and finally recursively invoke <code>interpret</code> with the value returned by <code>next</code>. </p> <p> Three of the implementations are details that aren't of importance here, but if you want to review them, the entire source code for this article is <a href="https://gist.github.com/ploeh/3965c5df81f2a16f41a81cffeaae35c5">available as a gist</a>. The fourth implementation is the built-in <code>getCurrentTime</code> function. They are all impure; all return <code>IO</code> values. This also implies that the return type of the entire <code>interpret</code> function is <code>IO a</code>. </p> <p> This particular interpreter is impure, but nothing prevents you from writing a pure interpreter, for example for use in unit testing. </p> <h3 id="0a0b68970a474e63903a0f54df4ae126"> Execution <a href="#0a0b68970a474e63903a0f54df4ae126" title="permalink">#</a> </h3> <p> You're almost done. You have a function that returns a new state for any given input state, as well as an interpreter. You need a function that can repeat this in a loop until it reaches the <code>Stopped</code> state: </p> <p> <pre><span style="color:#600277;">run</span>&nbsp;::&nbsp;<span style="color:blue;">NominalDiffTime</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">IdleDuration</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">UTCTime</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">PollingState</span>&nbsp;<span style="color:#dd0000;">Message</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">IO</span>&nbsp;(<span style="color:#dd0000;">PollingState</span>&nbsp;<span style="color:#dd0000;">Message</span>) run&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;state&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;ns&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;interpret&nbsp;<span style="color:#666666;">$</span>&nbsp;transition&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;state &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;ns&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Stopped</span>&nbsp;_&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;return&nbsp;ns &nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;run&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;ns</pre> </p> <p> This recursive function calls <code>transition</code> with the input <code>state</code>. You may recall that <code>transition</code> returns a <code>PollingProgram msg (PollingState msg)</code> value. Passing this value to <code>interpret</code> returns an <code>IO (PollingState Message)</code> value, and because of the <code>do</code> notation, the new state (<code>ns</code>) is a <code>PollingState Message</code> value. </p> <p> You can now pattern match on <code>ns</code>. If it's a <code>Stopped</code> value, you return the value. Otherwise, you recursively call <code>run</code> once more. </p> <p> The <code>run</code> function keeps doing this until it reaches the <code>Stopped</code> state. </p> <p> <em>Finally</em>, then, you can write the entry point for the program: </p> <p> <pre><span style="color:#600277;">main</span>&nbsp;::&nbsp;IO&nbsp;() main&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;timeAtEntry&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;getCurrentTime &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;estimatedDuration&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#09885a;">2</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;idleDuration&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">IdleDuration</span>&nbsp;<span style="color:#09885a;">5</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stopBefore&nbsp;<span style="color:#666666;">=</span>&nbsp;addUTCTime&nbsp;<span style="color:#09885a;">60</span>&nbsp;timeAtEntry &nbsp;&nbsp;s&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;run&nbsp;estimatedDuration&nbsp;idleDuration&nbsp;stopBefore&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#dd0000;">Ready</span>&nbsp;<span style="color:blue;">[]</span> &nbsp;&nbsp;timeAtExit&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;getCurrentTime &nbsp;&nbsp;putStrLn&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#a31515;">&quot;Elapsed&nbsp;time:&nbsp;&quot;</span>&nbsp;<span style="color:#666666;">++</span>&nbsp;show&nbsp;(diffUTCTime&nbsp;timeAtExit&nbsp;timeAtEntry) &nbsp;&nbsp;putStrLn&nbsp;<span style="color:#666666;">$</span>&nbsp;printf&nbsp;<span style="color:#a31515;">&quot;%d&nbsp;message(s)&nbsp;handled.&quot;</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;report&nbsp;s</pre> </p> <p> It defines the initial input parameters: <ul> <li>My wild guess about the handle duration is 2 seconds</li> <li>I'd like the idle duration to be 5 seconds</li> <li>The program should run for 60 seconds</li> </ul> The initial state is <code>Ready []</code>. These are all the arguments you need to call <code>run</code>. </p> <p> Once <code>run</code> returns, you can print the number of messages handled using a (trivial) <code>report</code> function that I haven't shown (but which is available in the gist). </p> <p> If you run the program, it'll produce output similar to this: </p> <p> <pre>Polling Handling Polling Handling Polling Handling Polling Sleeping Polling Handling Polling Sleeping Polling Handling Polling Handling Polling Sleeping Polling Sleeping Polling Sleeping Polling Sleeping Polling Sleeping Polling Handling Polling Handling Polling Handling Polling Handling Polling Sleeping Polling Elapsed time: 56.6835022s 10 message(s) handled.</pre> </p> <p> It does, indeed, exit before 60 seconds have elapsed. </p> <h3 id="2d095e5e51bd492487b644d045060b5a"> Summary <a href="#2d095e5e51bd492487b644d045060b5a" title="permalink">#</a> </h3> <p> You can model long-running interactions with an Abstract Syntax Tree. Without <code>do</code> notation, writing programs as 'raw' ASTs would be cumbersome, but turning the AST into a (free) monad makes it all quite palatable. </p> <p> Haskell's sophisticated type system makes this a fairly low-hanging fruit, once you understand how to do it. You can also port this type of design to F#, although, as you shall see next, more boilerplate is required. </p> <p> <strong>Next:</strong> <a href="/2017/07/04/pure-times-in-f">Pure times in F#</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="afc6b129176b4de099a4974a56c9626d"> <div class="comment-author"><a href="http://stackoverflow.com/users/1523776/benjamin-hodgson?tab=profile">Benjamin Hodgson</a> <a href="#afc6b129176b4de099a4974a56c9626d">#</a></div> <div class="comment-content"> <p> Good introduction to the notion of programs-as-embedded-languages here, thanks for writing it! </p> <p> In my experience a majority of <code>Free</code> interpreters fit into the <a href="https://hackage.haskell.org/package/free-4.12.4/docs/Control-Monad-Free.html#v:foldFree"><code>foldFree</code></a> pattern. Saves you the repetitous bits of your <code>interpret</code> function: </p> <pre><code>interpret = foldFree eta where eta (CurrentTime k) = k &lt;$&gt; getCurrentTime eta (Poll k) = k &lt;$&gt; pollImp eta (Handle msg k) = k &lt;$&gt; handleImp msg eta (Idle d k) = k &lt;$&gt; idleImp d</code></pre> <p> Anyway, I just wanted to give an alternative viewpoint on the topic of <code>Free</code> which will hopefully be some food for thought. I'm generally not an advocate of the <code>Free</code> approach to modelling effectful computation. I don't think it has much of an advantage over the old fashioned <code>mtl</code> style, especially since you have only one effect and only one interpreter. I'd have written your interface like this: </p> <pre><code>class Monad m =&gt; MonadPoll msg m | m -> msg where currentTime :: m UTCTime poll :: m (Maybe msg, PollDuration) handle :: msg -&gt; m HandleDuration idle :: m IdleDuration transitionFromNoMessage :: MonadPoll msg m =&gt; IdleDuration -&gt; UTCTime -&gt; [CycleDuration] -&gt; m (PollingState msg) transitionFromNoMessage d stopBefore statistics = do b &lt;- shouldIdle d stopBefore if b then idle d &gt;&gt; return (Ready statistics) else return $ Stopped statistics</code></pre> <p> It's a clearer, more direct expression of the monadic interface, in my opinion, and it admits simpler implementations (and it's faster because GHC can specialise and inline everything). Computations with access to only a <code>MonadPoll</code> context can only perform polling actions, so it's still pure, and you can swap out different implementations of <code>MonadPoll</code> (eg, for testing) by writing types with different instances. You can do eg <a href="https://gist.github.com/ocharles/6b1b9440b3513a5e225e">this</a> if you need "decorator"-style interpreters. The main downside of the <code>mtl</code> style is the "n^2 instances problem" (though <code>GeneralizedNewtypeDeriving</code> does somewhat ease the pain). </p> <p> Kiselyov has some good <a href="http://okmij.org/ftp/tagless-final/course/lecture.pdf">lecture notes</a> about using the <code>mtl</code> style to model abstract syntax trees and compositional interpreters. I probably wouldn't go that far if I were building a compiler! Type classes are good at effect systems and algebraic data types are good at syntax trees, and while each job can be done by either it pays to pick your tools carefully. </p> <p> Having said all that, the <code>Free</code> approach is probably more attractive in F#, because it doesn't feature type classes or higher kinds. And <code>Free</code> has other uses outside of the world of effect systems. </p> <p> Hope all the above is interesting to you! </p> <p> Benjamin </p> </div> <div class="comment-date">2017-06-29 2:13 UTC</div> </div> <div class="comment" id="f4c747f726474f5b9e1c996351f65c53"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f4c747f726474f5b9e1c996351f65c53">#</a></div> <div class="comment-content"> <p> Benjamin, thank you for writing. It is, indeed, interesting to me, and I appreciate that you took the time to write such a helpful and concise comment. </p> <p> I wasn't aware of <code>foldFree</code>, but I can see that I'll have to look into it. </p> <p> One day (soon), I'll have to try writing a small Haskell program using the <code>mtl</code> style instead. It looks as though the code would be quite similar, although the types are different. Are these approaches isomorphic? </p> <p> In any case, I hope that I'm not coming off as being too authoritative. In some sense, this blog often serves as my own elaborate journal documenting what I've been learning recently. I hope that what I write is mostly correct, but I don't presume that what I write is the one and only truth; it's bound by my knowledge at the time of writing. I still have much to learn, and I'm always happy when people help me expand my horizon. </p> <p> I think that you hit the nail concerning F#. One of my motivations for exploring this space was to figure out what can be done in F#. As far as I can tell, the <code>mtl</code> style doesn't translate well to F#. You can debate whether or not free monads translate <em>well</em> to F#, but at least the concept does carry over. </p> </div> <div class="comment-date">2017-06-29 6:47 UTC</div> </div> <div class="comment" id="3092767cdd514c84ba25acc0516f56c5"> <div class="comment-author"><a href="http://stackoverflow.com/users/1523776/benjamin-hodgson?tab=profile">Benjamin Hodgson</a> <a href="#3092767cdd514c84ba25acc0516f56c5">#</a></div> <div class="comment-content"> <p> Yep, they're isomorphic, in that you can round-trip in either direction between the two representations - <code>to . from = from . to = id</code>: </p> <pre>instance MonadPoll msg (Free (PollingInstruction msg)) where currentTime = liftF (CurrentTime id) poll = liftF (Poll id) handle msg = liftF (Handle msg id) idle d = liftF (Idle d id) to :: (forall m. MonadPoll msg m =&gt; m a) -&gt; Free (PollingInstruction msg) a to x = x from :: MonadPoll msg m =&gt; Free (PollingInstruction msg) a -&gt; m a from x = foldFree eta where eta (CurrentTime k) = k &lt;$&gt; currentTime eta (Poll k) = k &lt;$&gt; poll eta (Handle msg k) = k &lt;$&gt; handle msg eta (Idle d k) = k &lt;$&gt; idle d</pre> <p> But the representations being isomorphic doesn't mean they're equally convenient. (Another example of this would be lenses: van Laarhoven lenses (à la <code>lens</code>) are isomorphic to "costate comonad coalgebra" (ie <code>get</code>/<code>set</code>) lenses, but they're much more composable.) </p> <p> Benjamin </p> </div> <div class="comment-date">2017-06-29 16:39 UTC</div> </div> <div class="comment" id="4a8d6f25556741f8921bd9f6aa33893f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4a8d6f25556741f8921bd9f6aa33893f">#</a></div> <div class="comment-content"> <p> Benjamin, thank you once again for writing. It's amazing that not only are they isomorphic, but you can actually prove it with code. I have to admit, though, that I haven't tried compiling or running your code yet. First, I need to digest this. </p> <p> I was never under the impression that I knew most of what there was to know, but <em>by Jove!</em>, poking at Haskell unearths fathomless depths of knowledge of which I still only glance the surface. It's occasionally frustrating, but mostly exhilarating. </p> </div> <div class="comment-date">2017-06-29 20:26 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pure times https://blog.ploeh.dk/2017/06/27/pure-times 2017-06-27T09:11:00+00:00 Mark Seemann <div id="post"> <p> <em>How to interact with the system clock using strict functional programming.</em> </p> <p> A couple of years ago, I published an article called <a href="/2014/12/17/good-times-with-f">Good times with F#</a>. Unfortunately, that article never lived up to my expectations. Not that I don't have a good time with F# (I do), but the article introduced an attempt to model execution durations of operations in a functional manner. The article introduced a <code>Timed&lt;'a&gt;</code> generic type that I had high hopes for. </p> <p> Later, I published a Pluralsight course called <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a>, in which I used <code>Timed&lt;'a&gt;</code> to implement a <a href="http://www.enterpriseintegrationpatterns.com/PollingConsumer.html">Polling Consumer</a>. It's a good course that teaches you how to let F#'s type system give you rapid feedback. You can read <a href="/2015/08/10/type-driven-development">a few articles</a> that highlight the important parts of the course. </p> <p> There's a problem with the implementation, though. It's not functional. </p> <p> It's nice F# code, but F# is this friendly, forgiving, multi-paradigmatic language that enables you to get real work done. If you want to do this using <a href="/2017/01/30/partial-application-is-dependency-injection">partial application as a replacement for Dependency Injection</a>, it'll let you. It is, however, <a href="/2017/01/30/partial-application-is-dependency-injection">not functional</a>. </p> <p> Consider, as an example, this function: </p> <p> <pre><span style="color:green;">//&nbsp;(Timed&lt;TimeSpan&nbsp;list&gt;&nbsp;-&gt;&nbsp;bool)&nbsp;-&gt;&nbsp;(unit&nbsp;-&gt;&nbsp;Timed&lt;&#39;a&gt;)&nbsp;-&gt;&nbsp;Timed&lt;TimeSpan&nbsp;list&gt;</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;State</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">transitionFromNoMessage</span>&nbsp;<span style="color:navy;">shouldIdle</span>&nbsp;<span style="color:navy;">idle</span>&nbsp;nm&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:navy;">shouldIdle</span>&nbsp;nm &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">idle</span>&nbsp;()&nbsp;|&gt;&nbsp;<span style="color:teal;">Untimed</span>.<span style="color:navy;">withResult</span>&nbsp;nm.Result&nbsp;|&gt;&nbsp;<span style="color:navy;">ReadyState</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">StoppedState</span>&nbsp;nm.Result</pre> </p> <p> The <code>idle</code> function has the type <code>unit -&gt; Timed&lt;'a&gt;</code>. This can't possibly be a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, since a deterministic function can't produce a value from nothing when it doesn't know the type of the value. (In F#, this is technically not true, since we could return null for all reference types, and 'zero' for all value types, but even so, it should be clear that we can't produce any <em>useful</em> return value in a deterministic manner.) </p> <p> The same argument applies, in weaker form, to the <code>shouldIdle</code> function. While it <em>is</em> possible to write more than one pure function with the type <code>Timed&lt;TimeSpan list&gt; -&gt; bool</code>, the <em>intent</em> is that it should look at the time statistics and the current time, and decide whether or not it's 'safe' to poll again. Getting the current time from the system clock is a non-deterministic operation. </p> <p> Ever since I discovered that Dependency Injection is impossible in functional programming, I knew that I had to return to the Polling Consumer example and show how to implement it in a truly functional style. In order to be sure that I don't accidentally call an impure function from a 'pure' function, I'll first <a href="/2017/06/28/pure-times-in-haskell">rewrite the Polling Consumer in Haskell</a>, and afterwards <a href="/2017/07/04/pure-times-in-f">translate the Haskell code to F#</a>. When reading, you can skip the Haskell article and go directly to the F# article, or vice versa, if you like. </p> <p> <strong>Next:</strong> <a href="/2017/06/28/pure-times-in-haskell">Pure times in Haskell</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fractal trees with PureScript https://blog.ploeh.dk/2017/06/06/fractal-trees-with-purescript 2017-06-06T08:10:00+00:00 Mark Seemann <div id="post"> <p> <em>A fractal tree drawn with PureScript</em> </p> <p> Last week, I attended <a href="http://brandewinder.com">Mathias Brandewinder's</a> F# (and <a href="https://clojure.org">Clojure</a>) dojo in Copenhagen, and had great fun drawing a fractal tree in F# together with other attendees. Afterwards, I started thinking that it'd be fairly easy to port the F# code to <a href="https://www.haskell.org">Haskell</a>, but then I reconsidered. The combination of Haskell, Windows, and drawing sounded intimidating. This seemed a good opportunity to take <a href="http://www.purescript.org">PureScript</a> for a spin, because that would enable me to draw the tree on an HTML canvas. </p> <p> In case you're wondering, a fractal tree is simply a tree that branches infinitely in (I suppose) a deterministic fashion. Here's an example of the output of the code of this article: </p> <p> <img src="/content/binary/symmetric-fractal-tree.png" alt="A symmetric fractal tree."> </p> <p> This is my first attempt at PureScript, and I think I spent between five and ten hours in total. Most of them I used to figure out how to install PureScript, how to set up a development environment, and so on. All in all I found the process pleasing. </p> <p> While it's a separate, independent language, PureScript is clearly a descendant of Haskell, and the syntax is similar. </p> <h3 id="9ade5aaea20c4243aa4ebb0b1a2d79ad"> Separating data from effects <a href="#9ade5aaea20c4243aa4ebb0b1a2d79ad" title="permalink">#</a> </h3> <p> In functional programming, you routinely separate data from effects. Instead of trying to both draw and calculate branches of a tree in a single operation, you figure out how to first define a fractal tree as data, and then subsequently you can draw it. </p> <p> A generic binary tree is a staple of functional programming. Here's one way to do it: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Tree</span>&nbsp;a&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Leaf</span>&nbsp;a&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Node</span>&nbsp;a&nbsp;(<span style="color:#dd0000;">Tree</span>&nbsp;a)&nbsp;(<span style="color:#dd0000;">Tree</span>&nbsp;a) </pre> </p> <p> Such a tree is either a leaf node with a generically typed value, or an (intermediate) node with a value and two branches, which are themselves (sub)trees. </p> <p> In a sense, this definition is an approximation, because a 'real' fractal tree has no leafs. In Haskell you can easily define infinite trees, because Haskell is lazily evaluated. PureScript, on the other hand, is eagerly evaluated, so infinite recursion would require jumping through some hoops, and I don't think it's important in this exercise. </p> <p> While the above tree type can contain values of any type, in this exercise, it should contain line segments. One way to do this is to define a <code>Line</code> record type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Line</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Line</span>&nbsp;{ &nbsp;&nbsp;<span style="color:#600277;">x</span>&nbsp;::&nbsp;<span style="color:blue;">Number</span>, &nbsp;&nbsp;<span style="color:#600277;">y</span>&nbsp;::&nbsp;<span style="color:blue;">Number</span>, &nbsp;&nbsp;<span style="color:#600277;">angle</span>&nbsp;::&nbsp;<span style="color:blue;">Number</span>, &nbsp;&nbsp;<span style="color:#600277;">length</span>&nbsp;::&nbsp;<span style="color:blue;">Number</span>, &nbsp;&nbsp;<span style="color:#600277;">width</span>&nbsp;::&nbsp;<span style="color:blue;">Number</span>&nbsp;} </pre> </p> <p> This is a type with five labelled values, all of them numbers. <code>x</code> and <code>y</code> are coordinates for the origin of the line, <code>angle</code> defines the angle (measured in <a href="https://en.wikipedia.org/wiki/Radian">radians</a>) from the origin, and <code>length</code> the length of the line. In a similarly obvious vein, <code>width</code> denotes the width of the line, although this data element has no impact on the calculation of the tree. It's purely a display concern. </p> <p> Given the first four numbers in a <code>Line</code> value, you can calculate the endpoint of a line: </p> <p> <pre><span style="color:#600277;">endpoint</span>&nbsp;::&nbsp;forall&nbsp;r. &nbsp;&nbsp;{&nbsp;x&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Number</span> &nbsp;&nbsp;,&nbsp;y&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Number</span> &nbsp;&nbsp;,&nbsp;angle&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Number</span> &nbsp;&nbsp;,&nbsp;length&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Number</span> &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;r&nbsp;} &nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Tuple</span>&nbsp;<span style="color:#dd0000;">Number</span>&nbsp;<span style="color:#dd0000;">Number</span> endpoint&nbsp;line&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:green;">--&nbsp;Flip&nbsp;the&nbsp;y&nbsp;value&nbsp;because&nbsp;Canvas&nbsp;coordinate&nbsp;system&nbsp;points&nbsp;down&nbsp;from&nbsp;upper </span>&nbsp;&nbsp;<span style="color:green;">--&nbsp;left&nbsp;corner </span>&nbsp;&nbsp;<span style="color:#dd0000;">Tuple</span> &nbsp;&nbsp;&nbsp;&nbsp;(line<span style="color:#666666;">.</span>x&nbsp;<span style="color:#666666;">+</span>&nbsp;line<span style="color:#666666;">.</span>length&nbsp;<span style="color:#666666;">*</span>&nbsp;cos&nbsp;line<span style="color:#666666;">.</span>angle) &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#666666;">-</span>(<span style="color:#666666;">-</span>line<span style="color:#666666;">.</span>y&nbsp;<span style="color:#666666;">+</span>&nbsp;line<span style="color:#666666;">.</span>length&nbsp;<span style="color:#666666;">*</span>&nbsp;sin&nbsp;line<span style="color:#666666;">.</span>angle)) </pre> </p> <p> This may look more intimidating than it really is. The first seven lines are simply the (optional) type declaration; ignore that for a minute. The function itself is a one-liner, although I've formatted it on several lines in order to stay within an 80 characters line width. It simply performs a bit of <a href="https://en.wikipedia.org/wiki/Trigonometry">trigonometry</a> in order to find the endpoint of a line with an origin, angle, and length. As the code comment states, it negates the <code>y</code> value because the HTML canvas coordinate system points down instead of up (larger <code>y</code> values are further towards the bottom of the screen than smaller values). </p> <p> The function calculates a new set of coordinates for the endpoint, and returns them as a tuple. In PureScript, tuples are explicit and created with the <code>Tuple</code> data constructor. </p> <p> In general, as far as I can tell, you have less need of tuples in PureScript, because instead, you have <em>row type polymorphism</em>. This is still a new concept to me, but as far as I can tell, it's a sort of static duck typing. You can see it in the type declaration of the <code>endpoint</code> function. The function takes a <code>line</code> argument, the type of which is <em>any</em> record type that contains <code>x</code>, <code>y</code>, <code>angle</code>, and <code>length</code> labels of the <code>Number</code> type. For instance, as you'll soon see, you can pass a <code>Line</code> value to the <code>endpoint</code> function. </p> <h3 id="73975e453f484d598da20c21e9ac84e4"> Creating branches <a href="#73975e453f484d598da20c21e9ac84e4" title="permalink">#</a> </h3> <p> In a fractal tree, you calculate two branches for any given branch. Typically, in order to draw a pretty picture, you make the sub-branches smaller than the parent branch. You also incline each branch an angle from the parent branch. While you can hard-code the values for these operations, you can also pass them as arguments. In order to prevent an explosion of primitive function arguments, I collected all such parameters in a single data type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">FractalParameters</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">FractalParameters</span>&nbsp;{ &nbsp;&nbsp;<span style="color:#600277;">leftAngle</span>&nbsp;::&nbsp;<span style="color:blue;">Number</span>, &nbsp;&nbsp;<span style="color:#600277;">rightAngle</span>&nbsp;::&nbsp;<span style="color:blue;">Number</span>, &nbsp;&nbsp;<span style="color:#600277;">shrinkFactor</span>&nbsp;::&nbsp;<span style="color:blue;">Number</span>&nbsp;} </pre> </p> <p> This is a record type similar to the <code>Line</code> type you've already seen. </p> <p> When you have a <code>FractalParameters</code> value and a (parent) line, you can calculate its branches: </p> <p> <pre><span style="color:#600277;">createBranches</span>&nbsp;::&nbsp;<span style="color:blue;">FractalParameters</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Line</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tuple</span>&nbsp;<span style="color:blue;">Line</span>&nbsp;<span style="color:blue;">Line</span> createBranches&nbsp;(<span style="color:#dd0000;">FractalParameters</span>&nbsp;p)&nbsp;(<span style="color:#dd0000;">Line</span>&nbsp;line)&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#dd0000;">Tuple</span>&nbsp;left&nbsp;right &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Tuple</span>&nbsp;x&nbsp;y&nbsp;<span style="color:#666666;">=</span>&nbsp;endpoint&nbsp;line &nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Line</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x<span style="color:#666666;">:</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y<span style="color:#666666;">:</span>&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;angle<span style="color:#666666;">:</span>&nbsp;pi&nbsp;<span style="color:#666666;">*</span>&nbsp;(line<span style="color:#666666;">.</span>angle&nbsp;<span style="color:#666666;">/</span>&nbsp;pi&nbsp;<span style="color:#666666;">+</span>&nbsp;p<span style="color:#666666;">.</span>leftAngle), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length<span style="color:#666666;">:</span>&nbsp;(line<span style="color:#666666;">.</span>length&nbsp;<span style="color:#666666;">*</span>&nbsp;p<span style="color:#666666;">.</span>shrinkFactor), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width<span style="color:#666666;">:</span>&nbsp;(line<span style="color:#666666;">.</span>width&nbsp;<span style="color:#666666;">*</span>&nbsp;p<span style="color:#666666;">.</span>shrinkFactor)&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;right&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Line</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x<span style="color:#666666;">:</span>&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y<span style="color:#666666;">:</span>&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;angle<span style="color:#666666;">:</span>&nbsp;pi&nbsp;<span style="color:#666666;">*</span>&nbsp;(line<span style="color:#666666;">.</span>angle&nbsp;<span style="color:#666666;">/</span>&nbsp;pi&nbsp;<span style="color:#666666;">-</span>&nbsp;p<span style="color:#666666;">.</span>rightAngle), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length<span style="color:#666666;">:</span>&nbsp;(line<span style="color:#666666;">.</span>length&nbsp;<span style="color:#666666;">*</span>&nbsp;p<span style="color:#666666;">.</span>shrinkFactor), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width<span style="color:#666666;">:</span>&nbsp;(line<span style="color:#666666;">.</span>width&nbsp;<span style="color:#666666;">*</span>&nbsp;p<span style="color:#666666;">.</span>shrinkFactor)&nbsp;} </pre> </p> <p> The <code>createBranches</code> function returns a tuple of <code>Line</code> values, one for the left branch, and one for the right branch. First, it calls <code>endpoint</code> with <code>line</code>. Notice that <code>line</code> is a <code>Line</code> value, and because <code>Line</code> defines <code>x</code>, <code>y</code>, <code>angle</code>, and <code>length</code> labels, it can be used as an input argument. This type-checks because of row type polymorphism. </p> <p> Given the endpoint of the parent line, <code>createBranches</code> then creates two new <code>Line</code> values (<code>left</code> and <code>right</code>) with that endpoint as their origins. Both of these values are modified with the <code>FractalParameters</code> argument, so that they branch off to the left and the right, and also shrink in an aesthetically pleasing manner. </p> <h3 id="f0e6d987fa084b73b7e5f7c3039a3093"> Creating a tree <a href="#f0e6d987fa084b73b7e5f7c3039a3093" title="permalink">#</a> </h3> <p> Now that you can calculate the branches of a line, you can create a tree using recursion: </p> <p> <pre><span style="color:#600277;">createTree</span>&nbsp;::&nbsp;Int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">FractalParameters</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Line</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:blue;">Line</span> createTree&nbsp;depth&nbsp;p&nbsp;line&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;depth&nbsp;<span style="color:#666666;">&lt;=</span>&nbsp;<span style="color:#09885a;">0</span> &nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;<span style="color:#dd0000;">Leaf</span>&nbsp;line &nbsp;&nbsp;<span style="color:#af00db;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:#dd0000;">Tuple</span>&nbsp;leftLine&nbsp;rightLine&nbsp;<span style="color:#666666;">=</span>&nbsp;createBranches&nbsp;p&nbsp;line &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;createTree&nbsp;(depth&nbsp;<span style="color:#666666;">-</span>&nbsp;<span style="color:#09885a;">1</span>)&nbsp;p&nbsp;leftLine &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;right&nbsp;<span style="color:#666666;">=</span>&nbsp;createTree&nbsp;(depth&nbsp;<span style="color:#666666;">-</span>&nbsp;<span style="color:#09885a;">1</span>)&nbsp;p&nbsp;rightLine &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:#dd0000;">Node</span>&nbsp;line&nbsp;left&nbsp;right </pre> </p> <p> The <code>createTree</code> function takes a <code>depth</code> argument, which specifies the depth (or is it the height?) of the tree. The reason I called it <code>depth</code> is because <code>createTree</code> is recursive, and <code>depth</code> controls the depth of the recursion. If <code>depth</code> is zero or less, the function returns a <code>Leaf</code> node containing the input <code>line</code>. </p> <p> Otherwise, it calls <code>createBranches</code> with the input <code>line</code>, and recursively calls <code>createTree</code> for each of these branches, but with a decremented <code>depth</code>. It then returns a <code>Node</code> containing the input <code>line</code> and the two sub-trees <code>left</code> and <code>right</code>. </p> <p> This implementation isn't tail-recursive, but the above image was generated with a recursion depth of only 10, so running out of stack space wasn't my biggest concern. </p> <h3 id="f427cc1ec56240a48eb2a5fbe799848a"> Drawing the tree <a href="#f427cc1ec56240a48eb2a5fbe799848a" title="permalink">#</a> </h3> <p> With the <code>createTree</code> function you can create a fractal tree, but it's no fun if you can't draw it. You can use the <code>Graphics.Canvas</code> module in order to draw on an HTML canvas. First, here's how to draw a single line: </p> <p> <pre><span style="color:#600277;">drawLine</span>&nbsp;::&nbsp;<span style="color:blue;">Context2D</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Line</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Eff</span>&nbsp;(canvas&nbsp;::&nbsp;<span style="color:blue;">CANVAS</span>)&nbsp;<span style="color:blue;">Unit</span> drawLine&nbsp;ctx&nbsp;(<span style="color:#dd0000;">Line</span>&nbsp;line)&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:#dd0000;">Tuple</span>&nbsp;x&#39;&nbsp;y&#39;&nbsp;<span style="color:#666666;">=</span>&nbsp;endpoint&nbsp;line &nbsp;&nbsp;void&nbsp;<span style="color:#666666;">$</span>&nbsp;strokePath&nbsp;ctx&nbsp;<span style="color:#666666;">$</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;<span style="color:#666666;">$</span>&nbsp;moveTo&nbsp;ctx&nbsp;line<span style="color:#666666;">.</span>x&nbsp;line<span style="color:#666666;">.</span>y &nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;<span style="color:#666666;">$</span>&nbsp;setLineWidth&nbsp;line<span style="color:#666666;">.</span>width&nbsp;ctx &nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;<span style="color:#666666;">$</span>&nbsp;lineTo&nbsp;ctx&nbsp;x&#39;&nbsp;y&#39; &nbsp;&nbsp;&nbsp;&nbsp;closePath&nbsp;ctx </pre> </p> <p> While hardly unmanageable, I was surprised that I wasn't able to find a pre-defined function that would let me draw a line. Perhaps I was looking in the wrong place. </p> <p> With this helper function, you can now draw a tree using pattern matching: </p> <p> <pre><span style="color:#600277;">drawTree</span>&nbsp;::&nbsp;<span style="color:blue;">Context2D</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Tree</span>&nbsp;<span style="color:blue;">Line</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Eff</span>&nbsp;(canvas&nbsp;::&nbsp;<span style="color:blue;">CANVAS</span>)&nbsp;<span style="color:blue;">Unit</span> drawTree&nbsp;ctx&nbsp;(<span style="color:#dd0000;">Leaf</span>&nbsp;line)&nbsp;<span style="color:#666666;">=</span>&nbsp;drawLine&nbsp;ctx&nbsp;line drawTree&nbsp;ctx&nbsp;(<span style="color:#dd0000;">Node</span>&nbsp;line&nbsp;left&nbsp;right)&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;drawLine&nbsp;ctx&nbsp;line &nbsp;&nbsp;drawTree&nbsp;ctx&nbsp;left &nbsp;&nbsp;drawTree&nbsp;ctx&nbsp;right </pre> </p> <p> If the input tree is a <code>Leaf</code> value, the first line matches and the function simply draws the line, using the <code>drawLine</code> function. </p> <p> When the input tree is a <code>Node</code> value, the function first draws the <code>line</code> associated with that node, and then recursively calls itself with the <code>left</code> and <code>right</code> sub-trees. </p> <h3 id="d586e221b996436099ba9b177b79850a"> Execution <a href="#d586e221b996436099ba9b177b79850a" title="permalink">#</a> </h3> <p> The <code>drawTree</code> function enables you to draw using a <code>Context2D</code> value, which you can create from an HTML canvas: </p> <p> <pre>mcanvas&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;getCanvasElementById&nbsp;<span style="color:#a31515;">&quot;canvas&quot;</span> <span style="color:blue;">let</span>&nbsp;canvas&nbsp;<span style="color:#666666;">=</span>&nbsp;unsafePartial&nbsp;(fromJust&nbsp;mcanvas) ctx&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;getContext2D&nbsp;canvas </pre> </p> <p> From where does the <code>"canvas"</code> element come? Ultimately, PureScript compiles to JavaScript, and you can put the compiled script in an HTML file together with a <code>canvas</code> element: </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:maroon;">body</span><span style="color:blue;">&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">&lt;</span><span style="color:maroon;">canvas</span>&nbsp;<span style="color:red;">id</span><span style="color:blue;">=</span><span style="color:blue;">&quot;canvas&quot;</span>&nbsp;<span style="color:red;">width</span><span style="color:blue;">=</span><span style="color:blue;">&quot;800&quot;</span>&nbsp;<span style="color:red;">height</span><span style="color:blue;">=</span><span style="color:blue;">&quot;800&quot;</span><span style="color:blue;">&gt;&lt;/</span><span style="color:maroon;">canvas</span><span style="color:blue;">&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">&lt;</span><span style="color:maroon;">script</span>&nbsp;<span style="color:red;">src</span><span style="color:blue;">=</span><span style="color:blue;">&quot;index.js&quot;</span>&nbsp;<span style="color:red;">type</span><span style="color:blue;">=</span><span style="color:blue;">&quot;text/javascript&quot;</span><span style="color:blue;">&gt;&lt;/</span><span style="color:maroon;">script</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:maroon;">body</span><span style="color:blue;">&gt;</span></pre> </p> <p> Once you have a <code>Context2D</code> value, you can draw a tree. Here's the whole entry point, including the above canvas-finding code: </p> <p> <pre><span style="color:#600277;">main</span>&nbsp;::&nbsp;<span style="color:blue;">Eff</span>&nbsp;(canvas&nbsp;::&nbsp;<span style="color:blue;">CANVAS</span>)&nbsp;<span style="color:blue;">Unit</span> main&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;mcanvas&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;getCanvasElementById&nbsp;<span style="color:#a31515;">&quot;canvas&quot;</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;canvas&nbsp;<span style="color:#666666;">=</span>&nbsp;unsafePartial&nbsp;(fromJust&nbsp;mcanvas) &nbsp;&nbsp;ctx&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;getContext2D&nbsp;canvas &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;trunk&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Line</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;x<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">300.0</span>,&nbsp;y<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">600.0</span>,&nbsp;angle<span style="color:#666666;">:</span>&nbsp;(pi&nbsp;<span style="color:#666666;">/</span>&nbsp;<span style="color:#09885a;">2.0</span>),&nbsp;length<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">100.0</span>,&nbsp;width<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">4.0</span>&nbsp;} &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;p&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">FractalParameters</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;leftAngle<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">0.1</span>,&nbsp;rightAngle<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">0.1</span>,&nbsp;shrinkFactor<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">0.8</span>&nbsp;} &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;tree&nbsp;<span style="color:#666666;">=</span>&nbsp;createTree&nbsp;<span style="color:#09885a;">10</span>&nbsp;p&nbsp;trunk &nbsp;&nbsp;drawTree&nbsp;ctx&nbsp;tree </pre> </p> <p> After it finds the <code>Context2D</code> value, it hard-codes a <code>trunk</code> <code>Line</code> and a set of <code>FractalParameters</code>. From these, it creates a tree of size 10 and draws the tree in the beginning of this article. </p> <p> You can fiddle with the parameters to your liking. For example, you can make the right angle wider than the left angle: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;p&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">FractalParameters</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;leftAngle<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">0.1</span>,&nbsp;rightAngle<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">0.2</span>,&nbsp;shrinkFactor<span style="color:#666666;">:</span>&nbsp;<span style="color:#09885a;">0.8</span>&nbsp;} </pre> </p> <p> This produces an asymmetric tree: </p> <p> <img src="/content/binary/asymmetric-fractal-tree.png" alt="An asymmetric fractal tree."> </p> <p> In order to compile the code, I used this command: </p> <p> <pre>$ pulp browserify -t html/index.js</pre> </p> <p> This compiles the PureScript code to a single <code>index.js</code> file, which is output to the <code>html</code> directory. This directory also contains the HTML file with the canvas. </p> <p> You can find the entire PureScript file in <a href="https://gist.github.com/ploeh/d9d207906adf44713e302ba7d3654f11">this Gist</a>. </p> <h3 id="0e73afbe0c7948ba892adb691b9dacd4"> Summary <a href="#0e73afbe0c7948ba892adb691b9dacd4" title="permalink">#</a> </h3> <p> It was fun to try PureScript. I've been staying away from JavaScript-based development for many years now, but if I ever have to do some client-side development, I may consider it. So far, I've found that PureScript seems viable for drawing. How good it is if you need to interact with 'normal' web-pages or SPAs, I don't know (yet). </p> <p> If you have some experience with Haskell, it looks like it's easy to get started with PureScript. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Using Polly with F# async workflows https://blog.ploeh.dk/2017/05/30/using-polly-with-f-async-workflows 2017-05-30T12:03:00+00:00 Mark Seemann <div id="post"> <p> <em>How to use Polly as a Circuit Breaker in F# async workflows.</em> </p> <p> <a href="http://amzn.to/11Kmrb2">Release It!</a> describes a stability design pattern called <a href="https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern">Circuit Breaker</a>, which is used to fail fast if a downstream service is experiencing problems. </p> <p> I recently had to add a Circuit Breaker to an F# async workflow, and although Circuit Breaker isn't that difficult to implement (<a href="http://amzn.to/12p90MG">my book</a> contains an example in C#), I found it most prudent to use an existing implementation. <a href="http://www.thepollyproject.org">Polly</a> seemed a good choice. </p> <p> In my F# code base, I was already working with an 'abstraction' of the type <code>HttpRequestMessage -> Async&lt;HttpResponseMessage&gt;</code>: given an <code>HttpClient</code> called <code>client</code>, the implementation is as simple as <code>client.SendAsync &gt;&gt; Async.AwaitTask</code>. Since <code>SendAsync</code> can throw <code>HttpRequestException</code> or <code>WebException</code>, I wanted to define a Circuit Breaker policy for these two exception types. </p> <p> While Polly supports policies for Task-based APIs, it doesn't automatically work with F# async workflows. The problem is that whenever you convert an async workflow into a Task (using <code>Async.AwaitTask</code>), or a Task into an async workflow (using <code>Async.StartAsTask</code>), any exceptions thrown will end up buried within an <code>AggregateException</code>. In order to dig them out again, I first had to write this function: </p> <p> <pre><span style="color:green;">//&nbsp;Exception&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">isNestedHttpException</span>&nbsp;(e&nbsp;:&nbsp;<span style="color:teal;">Exception</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;e&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;:?&nbsp;<span style="color:teal;">AggregateException</span>&nbsp;<span style="color:blue;">as</span>&nbsp;ae&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ae.InnerException&nbsp;<span style="color:navy;">::</span>&nbsp;<span style="color:teal;">Seq</span>.<span style="color:navy;">toList</span>&nbsp;ae.InnerExceptions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Seq</span>.<span style="color:navy;">exists</span>&nbsp;<span style="color:navy;">isNestedHttpException</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;:?&nbsp;<span style="color:teal;">HttpRequestException</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;:?&nbsp;<span style="color:teal;">WebException</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span></pre> </p> <p> This function recursively searches through all inner exceptions of an <code>AggregateException</code> and returns <code>true</code> if it finds one of the exception types I'm interested in handling; otherwise, it returns <code>false</code>. </p> <p> This predicate enabled me to write the Polly policy I needed: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;Polly <span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;TimeSpan&nbsp;-&gt;&nbsp;CircuitBreaker.CircuitBreakerPolicy</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">createPolicy</span>&nbsp;exceptionsAllowedBeforeBreaking&nbsp;durationOfBreak&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Policy</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:navy;">Handle</span>&lt;<span style="color:teal;">AggregateException</span>&gt;(<span style="color:blue;">fun</span>&nbsp;ae&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">isNestedHttpException</span>&nbsp;ae) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<span style="color:navy;">CircuitBreakerAsync</span>(exceptionsAllowedBeforeBreaking,&nbsp;durationOfBreak)</pre> </p> <p> Since Polly exposes an object-oriented API, I wanted a curried alternative, so I also wrote this curried helper function: </p> <p> <pre><span style="color:green;">//&nbsp;Policy&nbsp;-&gt;&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">execute</span>&nbsp;(policy&nbsp;:&nbsp;<span style="color:teal;">Policy</span>)&nbsp;<span style="color:navy;">f</span>&nbsp;&nbsp;req&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;policy.<span style="color:navy;">ExecuteAsync</span>(<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;req&nbsp;|&gt;&nbsp;<span style="color:teal;">Async</span>.<span style="color:navy;">StartAsTask</span>)&nbsp;|&gt;&nbsp;<span style="color:teal;">Async</span>.<span style="color:navy;">AwaitTask</span></pre> </p> <p> The <code>execute</code> function executes any function of the type <code>'a -> Async&lt;'b&gt;</code> with a Polly policy. As you can see, there's some back-and-forth between Tasks and async workflows, so this is probably not the most efficient Circuit Breaker ever configured, but I wagered that since the underlying operation was going to involve an HTTP request and response, the overhead would be insignificant. No one has complained yet. </p> <p> When Polly opens the Circuit Breaker, it throws an exception of the type <code>BrokenCircuitException</code>. Again because of all the marshalling, this also gets wrapped within an <code>AggregateException</code>, so I had to write another function to unwrap it: </p> <p> <pre><span style="color:green;">//&nbsp;Exception&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">isNestedCircuitBreakerException</span>&nbsp;(e&nbsp;:&nbsp;<span style="color:teal;">Exception</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;e&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;:?&nbsp;<span style="color:teal;">AggregateException</span>&nbsp;<span style="color:blue;">as</span>&nbsp;ae&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ae.InnerException&nbsp;<span style="color:navy;">::</span>&nbsp;<span style="color:teal;">Seq</span>.<span style="color:navy;">toList</span>&nbsp;ae.InnerExceptions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Seq</span>.<span style="color:navy;">exists</span>&nbsp;<span style="color:navy;">isNestedCircuitBreakerException</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;:?&nbsp;CircuitBreaker.<span style="color:teal;">BrokenCircuitException</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span></pre> </p> <p> The <code>isNestedCircuitBreakerException</code> is similar to <code>isNestedHttpException</code>, so it'd be tempting to refactor. I decided, however, to rely on the <a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">rule of three</a> and leave both functions as they were. </p> <p> In my F# code I prefer to handle application errors using Either values instead of relying on exceptions, so I wanted to translate any <code>BrokenCircuitException</code> to a particular <a href="/2017/01/03/decoupling-application-errors-from-domain-models">application error</a>. With the above <code>isNestedCircuitBreakerException</code> predicate, this was now possible with a <code>try/with</code> expression: </p> <p> <pre><span style="color:green;">//&nbsp;Policy&nbsp;-&gt;&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;Async&lt;Result&lt;&#39;b,&nbsp;BoundaryFailure&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">sendUsingPolicy</span>&nbsp;policy&nbsp;<span style="color:navy;">send</span>&nbsp;req&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">async</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;resp&nbsp;=&nbsp;req&nbsp;|&gt;&nbsp;<span style="color:navy;">execute</span>&nbsp;policy&nbsp;<span style="color:navy;">send</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">succeed</span>&nbsp;resp &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">with</span>&nbsp;e&nbsp;<span style="color:blue;">when</span>&nbsp;<span style="color:navy;">isNestedCircuitBreakerException</span>&nbsp;e&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">fail</span>&nbsp;<span style="color:teal;">Controllers</span>.<span style="color:navy;">StabilityFailure</span>&nbsp;}</pre> </p> <p> This function takes a policy, a <code>send</code> function that actually sends the request, and the request to send. If all goes well, the <code>resp</code>onse is lifted into a <code>Success</code> case and returned. If the Circuit Breaker is open, a <code>StabilityFailure</code> value is returned instead. </p> <p> Since the <code>with</code> expression uses an exception filter, all other exceptions will still be thrown from this function. </p> <p> It might still be worthwhile to look into options for a more F#-friendly Circuit Breaker. One option would be to <a href="https://github.com/App-vNext/Polly/issues/252">work with the Polly maintainers to add such an API to Polly itself</a>. Another option would be to write a separate F# implementation. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="29f3a92a86b44113a95556109355cff9"> <div class="comment-author">Johannes Egger <a href="#29f3a92a86b44113a95556109355cff9">#</a></div> <div class="comment-content"> Nice post, thanks.<br> There are two minor points I want to address: * I think instead of the recursive search for a nested exception you can use <a href="https://msdn.microsoft.com/en-US/library/system.aggregateexception.flatten%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396">AggregateException.Flatten()</a> |> Seq.exists ...<br> * And I know that you know that a major difference between <code>Async</code> and <code>Task</code> is that a <code>Task</code> is typically started whereas an <code>Async</code> is not. So it might be irritating that calling <code>execute</code> already starts the execution. If you wrapped the body of <code>execute</code> inside another <code>async</code> block it would be lazy as usual, I think. </div> <div class="comment-date">2017-06-03 21:21 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Simple holidays https://blog.ploeh.dk/2017/04/24/simple-holidays 2017-04-24T13:42:00+00:00 Mark Seemann <div id="post"> <p> <em>A story about arriving at the simplest solution that could possibly work.</em> </p> <p> The Zen of Python states: <a href="https://www.python.org/dev/peps/pep-0020">Simple is better than complex</a>. If I've met a programmer who disagrees with that, I'm not aware of it. It's hardly a controversial assertion, but what does 'simplicity' mean? Can you even identify a simple solution? </p> <p> I often see software developers defaulting to complex solutions, because a simpler solution isn't immediately obvious. In retrospect, a simple solution often <em>is</em> obvious, but only once you've found it. Until then, it's elusive. </p> <p> I'd like to share a story in which I arrived at a simple solution after several false starts. I hope it can be an inspiration. </p> <h3 id="fd56a87e158e4a3a8e44a89565ccf506"> Dutch holidays <a href="#fd56a87e158e4a3a8e44a89565ccf506" title="permalink">#</a> </h3> <p> Recently, I had to write some code that takes into account Dutch holidays. (In order to address any confusion that could arise from this: No, I'm not Dutch, I'm Danish, but currently, I'm doing some work on a system targeting a market in the Netherlands.) Specifically, given a date, I had to find the latest possible Dutch bank day on or before that date. </p> <p> For normal week days (Monday to Friday), it's easy, because such a date is already a bank date. In other words, in that case, you can simply return the input date. Also, in normal weeks, given a Saturday or Sunday, you should return the preceding Friday. The problem is, however, that some Fridays are holidays, and therefore not bank days. </p> <p> Like many other countries, the Netherlands have complicated rules for determining official holidays. Here are some tricky parts: <ul> <li> Some holidays always fall on the same date. One example is <em>Bevrijdingsdag (Liberation Day)</em>, which always falls on May 5. This holiday celebrates a historic event (the end of World War II in Europe), so if you wanted to calculate bank holidays in the past, you'd have to figure out in which year this became a public holiday. Surely, at least, it must have been 1945 or later.</li> <li> Some holidays fall on specific week days. The most prominent example is Easter, where <em>Goede Vrijdag (Good Friday)</em> always (as the name implies) falls on a Friday. Which Friday exactly can be calculated using a complicated algorithm. </li> <li> One holiday (<em>Koningsdag (King's Day)</em>) celebrates the king's birthday. The date is determined by the currently reigning monarch's birthday, and it's called <em>Queen's Day</em> when the reigning monarch is a queen. Obviously, the exact date changes depending on who's king or queen, and you can't predict when it's going to change. And what will happen if the current monarch abdicates or dies <em>before</em> his or her birthday, but <em>after</em> the new monarch's birthday? Does that mean that there will be no such holiday that year? Or what about the converse? Could there be <em>two</em> such holidays if a monarch abdicates <em>after</em> his or her birthday, and the new monarch's birthday falls later the same year? </li> </ul> Such problems aren't particular to the Netherlands. In Denmark, we can find similar examples, as I think you can do in many other countries. Ultimately, what constitutes an official holiday is a political decision. </p> <p> Figuring out if a date is a bank day, then, is what you might call an 'interesting' problem. How would you solve it? Before you read on, take a moment to consider how you'd attempt to solve the problem. If you will, you can consider the test cases immediately below to get a better sense of the problem. </p> <h3 id="d26d174c4c4e49bb8f3e5ed166805bfd"> Test cases <a href="#d26d174c4c4e49bb8f3e5ed166805bfd" title="permalink">#</a> </h3> <p> Here's a small set of test cases that I wrote in order to describe the problem: <table> <thead> <tr> <th>Test case</th> <th>Input date</th> <th>Expected output</th> </tr> </thead> <tbody> <tr><td>Monday</td><td>2017-03-06</td><td>2017-03-06</td></tr> <tr><td>Tuesday</td><td>2017-03-07</td><td>2017-03-07</td></tr> <tr><td>Wednesday</td><td>2017-03-08</td><td>2017-03-08</td></tr> <tr><td>Thursday</td><td>2017-03-09</td><td>2017-03-09</td></tr> <tr><td>Friday</td><td>2017-03-10</td><td>2017-03-10</td></tr> <tr><td>Saturday</td><td>2017-03-11</td><td>2017-03-10</td></tr> <tr><td>Sunday</td><td>2017-03-12</td><td>2017-03-10</td></tr> <tr><td>Good Friday</td><td>2017-04-14</td><td>2017-04-13</td></tr> <tr><td>Saturday after Good Friday</td><td>2017-04-15</td><td>2017-04-13</td></tr> <tr><td>Sunday after Good Friday</td><td>2017-04-16</td><td>2017-04-13</td></tr> <tr><td>Easter Monday</td><td>2017-04-17</td><td>2017-04-13</td></tr> <tr><td>Ascension Day - Thursday</td><td>2017-05-25</td><td>2017-05-24</td></tr> <tr><td>Whit Monday</td><td>2110-05-26</td><td>2110-05-23</td></tr> <tr><td>Liberation Day</td><td>9713-05-05</td><td>9713-05-04</td></tr> </tbody> </table> You'll notice that while I wrote most of my test cases in the near future (they're actually already in the past, now), I also added some far future dates for good measure. This assumes that the Netherlands will still celebrate Christian religious holidays in a hundred years from now, or their liberation day in 9713. That the Netherlands still exist then as the country we know today is a more than dubious assumption. </p> <h3 id="8f64f61daf7c4ed1aebf418be1e9a390"> Option: query a third-party service <a href="#8f64f61daf7c4ed1aebf418be1e9a390" title="permalink">#</a> </h3> <p> How would you solve the problem? The first solution that occurred to me was to use a third-party service. My guess is that most developers would consider this option. After all, it's essentially third-part data. The official holidays are determined by a third party, in this case the Dutch state. Surely, some Dutch official organisation would publish the list of official holidays somewhere. Perhaps, if you're lucky, there's even an on-line service you can query in order to download the list of holidays in some machine-readable format. </p> <p> There are, however, problems with this alternative: if you query such a service each time you need to find an appropriate bank date, how are you going to handle network errors? What if the third-part service is (temporarily) unavailable? </p> <p> Since I'm trying to figure out bank dates, you may already have guessed that I'm handling money, so it's not desirable to simple throw an exception and say that a caller would have to try again later. This could lead to loss of revenue. </p> <p> Querying a third-party service <em>every time</em> you need to figure out a Dutch bank holiday is out of the question for that reason. It's also likely to be inefficient. </p> <h3 id="63542ee55d0645eb92b801e58d863de2"> Option: cache third-party data <a href="#63542ee55d0645eb92b801e58d863de2" title="permalink">#</a> </h3> <p> Public holidays rarely change, so your next attempt could be a variation of the previous. Use third-party data, but instead of querying a third-party service every time you need the information, cache it. </p> <p> The problem with caching is that you're not guaranteed that the data you seek is in the cache. At application start, caches are usually empty. You'd have to rely on making one good query to the third-party data source in order to put the data in the cache. Only if that succeeds can you use the cache. This, again, leaves you vulnerable to the normal failure modes of distributed computing. If you can't reach the third-party data source, you have nothing to put in the cache. </p> <p> This can be a problem at application start, or when the cache data expires. </p> <p> Using a cache <em>reduces</em> the risk that the data is unavailable, but it doesn't eliminate it. It also adds complexity in the form of a cache that has to be configured and managed. Granted, you can use a reusable cache library or service to minimise that cost, so it may not be a big deal. Still, when making a decision about application architecture, I think it helps to explicitly identify advantages and disadvantages. </p> <p> Using a cache felt better to me, but I still wasn't happy. Too many things could go wrong. </p> <h3 id="ae5c8bbbdba0402c80a37dd9520ba216"> Option: persistent cache <a href="#ae5c8bbbdba0402c80a37dd9520ba216" title="permalink">#</a> </h3> <p> An incremental improvement on the previous option would be to write the cache data to persistent storage. This takes care of the issue with the cache being empty at application start-up. You can even deal with cache expiry by keep using stale data if you can't reach the 'official' source of the data. </p> <p> It leaves me a bit concerned, though, because if you allow the system to continue working with stale data, perhaps the application could enter a state where the data <em>never</em> updates. This could happen if the official data source moves, or changes format. In such a case, your application would keep trying to refresh the cache, and it would permanently fail. It would permanently run with stale data. Would you ever discover that problem? </p> <p> My concern is that the application could silently fail. You could counter that by logging a warning somewhere, but that would introduce a permanent burden on the team responsible for operating the application. This isn't impossible, but it does constitute an extra complexity. This alternative still didn't feel good to me. </p> <h3 id="98cd311bc3ed4e48942cc4c30eef7fe2"> Option: cron <a href="#98cd311bc3ed4e48942cc4c30eef7fe2" title="permalink">#</a> </h3> <p> Because I wasn't happy with any of the above alternatives, I started looking for different approaches to the problem. For a short while, I considered using a .NET implementation of <a href="https://en.wikipedia.org/wiki/Cron">cron</a>, with a crontab file. As far as I can tell, though there's no easy way to define Easter using cron, so I quickly abandoned that line of inquiry. </p> <h3 id="6500a1bffc114066b969b3f64aff86ae"> Option: Nager.Date <a href="#6500a1bffc114066b969b3f64aff86ae" title="permalink">#</a> </h3> <p> I wasn't entirely done with idea of calculating holidays on the fly. While calculating Easter is complicated, it can be done; there <em>is</em> a well-defined algorithm for it. Whenever I run into a general problem like this, I assume that someone has already done the required work, and this is also the case here. I quickly found an open source library called <a href="https://github.com/tinohager/Nager.Date">Nager.Date</a>; I'm sure that there are other alternatives, but Nager.Date looks like it's more than good enough. </p> <p> Such a library would be able to calculate all holidays for a given year, based on the algorithms embedded in it. That looked really promising. </p> <p> And yet... again, I was concerned. Official holidays are, as we've already established, politically decided. Using an algorithmic approach is <em>fundamentally</em> wrong, because that's not really how the holidays are determined. Holidays are defined by decree; it just so happens that some of the decrees take the form of an algorithm (such as Easter). </p> <p> What would happen if the Dutch state decides to add a new holiday? Or to take one away? Of when a new monarch is crowned? In order to handle such changes, we'd now have to hope that Nager.Date would be updated. We could try to make that more likely to happen by sending a pull request, but we'd still be vulnerable to a third party. What if the maintainer of Nager.Date is on vacation? </p> <p> Even if you can get a change into a library like Nager.Date, how is the algorithmic approach going to deal with historic dates? If the monarch changes, you can update the library, but does it correctly handle dates in the past, where the King's Day was different? </p> <p> Using an algorithm to determine a holiday seemed promising, but ultimately, I decided that I didn't like this option either. </p> <h3 id="4337d1ee6ee44bff95fcdf7873982274"> Option: configuration file <a href="#4337d1ee6ee44bff95fcdf7873982274" title="permalink">#</a> </h3> <p> My main concern about using an algorithm is that it'd make it difficult to handle arbitrary changes and exceptional cases. If we'd use a configuration file, on the other hand, we could always edit the configuration file in order to add or remove holidays for a given year. </p> <p> In essence, I was envisioning a configuration file that simply contained a list of holidays for each year. </p> <p> That sounds fairly simple and maintainable, but from where should the data come? </p> <p> You could probably download a list of official holidays for the next few years, like 2017, 2018, 2019, and so on, but the list would be finite, and probably not cover more than a few years into the future. </p> <p> What if, for example, I'd only be able to find an official list that goes to 2020? What will happen, then, when our application enters 2021? To the rest of the code base, it'd look like there were no holidays in 2021. </p> <p> At this time we can expect that new official lists have been published, so a programmer could obtain such a list and update the configuration file when it's time. This, unfortunately, is easy to forget. Four years in the future, perhaps none of the original programmers are left. It's more than likely that no one will remember to do this. </p> <h3 id="f64e6b6da9bb485bac286c72f7b9c2e2"> Option: algorithm-generated configuration file <a href="#f64e6b6da9bb485bac286c72f7b9c2e2" title="permalink">#</a> </h3> <p> The problem that the configuration data could 'run out' can be addressed by initialising the configuration file with data generated algorithmically. You could, for example, ask Nager.Date to generate all the holidays for the next many years. In fact, the year 9999 is the maximum year handled by .NET's <code>System.DateTime</code>, so you could ask it to generate all the holidays until 9999. </p> <p> That sounds like a lot, but it's only about half a megabyte of data... </p> <p> This solves the problem of 'running out' of holiday data, but still enables you to edit the holiday data when it changes in the future. For example, if the King's Day changes in 2031, you can change all the King's Day values from 2031 onward, while retaining the correct values for the previous years. </p> <p> This seems promising... </p> <h3 id="1c48323a5dfd4a60b848f72a5e039313"> Option: hard-coded holidays <a href="#1c48323a5dfd4a60b848f72a5e039313" title="permalink">#</a> </h3> <p> I almost decided to use the previous, configuration-based solution, and I was getting ready to invent a configuration file format, and a reader for it, and so on. Then I recalled <a href="http://mikehadlow.blogspot.com">Mike Hadlow</a>'s article about the <a href="http://mikehadlow.blogspot.com/2012/05/configuration-complexity-clock.html">configuration complexity clock</a>. </p> <p> I'm fairly certain that the only people who would be editing a hypothetical holiday configuration file would be programmers. In that case, why put the configuration in a proprietary format? Why deal with the hassle of reading and parsing such a file? Why not put the data in code? </p> <p> That's what I decided to do. </p> <p> It's not a perfect solution. It's still necessary to go and change that code file when the holiday rules change. For example, when the King's Day changes, you'd have to edit the file. </p> <p> Still, it's the <em>simplest</em> solution I could come up with. It has no moving parts, and uses a 'best effort' approach in order to guarantee that holidays will always be present. If you can come up with a better alternative, please leave a comment. </p> <h3 id="8de8be6568d046a3bd3e0cdf324bb95e"> Data generation <a href="#8de8be6568d046a3bd3e0cdf324bb95e" title="permalink">#</a> </h3> <p> Nager.Date seemed useful for generating the initial set of holidays, so I wrote a small F# script that generated the necessary C# code snippets: </p> <p> <pre><span style="color:blue;">#r</span>&nbsp;<span style="color:#a31515;">@&quot;packages/Nager.Date.1.3.0/lib/net45/Nager.Date.dll&quot;</span> <span style="color:blue;">open</span>&nbsp;System.IO <span style="color:blue;">open</span>&nbsp;Nager.Date <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">formatHoliday</span>&nbsp;(h&nbsp;:&nbsp;Model.<span style="color:teal;">PublicHoliday</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;y,&nbsp;m,&nbsp;d&nbsp;=&nbsp;h.Date.Year,&nbsp;h.Date.Month,&nbsp;h.Date.Day &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;new&nbsp;DateTime(</span><span style="color:teal;">%i</span><span style="color:#a31515;">,&nbsp;</span><span style="color:teal;">%2i</span><span style="color:#a31515;">,&nbsp;</span><span style="color:teal;">%2i</span><span style="color:#a31515;">),&nbsp;//&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">/</span><span style="color:teal;">%s</span><span style="color:#a31515;">&quot;</span>&nbsp;y&nbsp;m&nbsp;d&nbsp;h.Name&nbsp;h.LocalName <span style="color:blue;">let</span>&nbsp;holidays&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[2017..9999] &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Seq</span>.<span style="color:navy;">collect</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">DateSystem</span>.<span style="color:navy;">GetPublicHoliday</span>&nbsp;(<span style="color:teal;">CountryCode</span>.NL,&nbsp;y)) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Seq</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">formatHoliday</span> <span style="color:teal;">File</span>.<span style="color:navy;">WriteAllLines</span>&nbsp;(<span style="color:blue;">__SOURCE_DIRECTORY__</span>&nbsp;+&nbsp;<span style="color:#a31515;">&quot;/dutch-holidays.txt&quot;</span>,&nbsp;holidays)</pre> </p> <p> This script simply asks Nager.Date to calculate all Dutch holidays for the years 2017 to 9999, format them as C# code snippets, and write the lines to a text file. The size of that file is 4 MB, because the auto-generated code comments also take up some space. </p> <h3 id="8748cc7b4c5f4accb342b3b190c06e4f"> First implementation attempt <a href="#8748cc7b4c5f4accb342b3b190c06e4f" title="permalink">#</a> </h3> <p> The next thing I did was to copy the text from <code>dutch-holidays.txt</code> to a C# code file, which I had already prepared with a class and a few methods that would query my generated data. The result looked like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DateTimeExtensions</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;AdjustToLatestPrecedingDutchBankDay( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;candidate&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;(!(IsDutchBankDay(candidate.DateTime))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate&nbsp;=&nbsp;candidate.AddDays(-1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;candidate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsDutchBankDay(<span style="color:#2b91af;">DateTime</span>&nbsp;date) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(date.DayOfWeek&nbsp;==&nbsp;<span style="color:#2b91af;">DayOfWeek</span>.Saturday) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(date.DayOfWeek&nbsp;==&nbsp;<span style="color:#2b91af;">DayOfWeek</span>.Sunday) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dutchHolidays.Contains(date.Date)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">#region</span>&nbsp;Dutch&nbsp;holidays &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">DateTime</span>[]&nbsp;dutchHolidays&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;&nbsp;1,&nbsp;&nbsp;1),&nbsp;<span style="color:green;">//&nbsp;New&nbsp;Year&#39;s&nbsp;Day/Nieuwjaarsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;&nbsp;4,&nbsp;14),&nbsp;<span style="color:green;">//&nbsp;Good&nbsp;Friday/Goede&nbsp;Vrijdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;&nbsp;4,&nbsp;17),&nbsp;<span style="color:green;">//&nbsp;Easter&nbsp;Monday/&nbsp;Pasen</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;&nbsp;4,&nbsp;27),&nbsp;<span style="color:green;">//&nbsp;King&#39;s&nbsp;Day/Koningsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;&nbsp;5,&nbsp;&nbsp;5),&nbsp;<span style="color:green;">//&nbsp;Liberation&nbsp;Day/Bevrijdingsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;&nbsp;5,&nbsp;25),&nbsp;<span style="color:green;">//&nbsp;Ascension&nbsp;Day/Hemelvaartsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;&nbsp;6,&nbsp;&nbsp;5),&nbsp;<span style="color:green;">//&nbsp;Whit&nbsp;Monday/Pinksteren</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;12,&nbsp;25),&nbsp;<span style="color:green;">//&nbsp;Christmas&nbsp;Day/Eerste&nbsp;kerstdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2017,&nbsp;12,&nbsp;26),&nbsp;<span style="color:green;">//&nbsp;St.&nbsp;Stephen&#39;s&nbsp;Day/Tweede&nbsp;kerstdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;&nbsp;1,&nbsp;&nbsp;1),&nbsp;<span style="color:green;">//&nbsp;New&nbsp;Year&#39;s&nbsp;Day/Nieuwjaarsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2018,&nbsp;&nbsp;3,&nbsp;30),&nbsp;<span style="color:green;">//&nbsp;Good&nbsp;Friday/Goede&nbsp;Vrijdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Lots&nbsp;and&nbsp;lots&nbsp;of&nbsp;dates...</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(9999,&nbsp;&nbsp;5,&nbsp;&nbsp;6),&nbsp;<span style="color:green;">//&nbsp;Ascension&nbsp;Day/Hemelvaartsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(9999,&nbsp;&nbsp;5,&nbsp;17),&nbsp;<span style="color:green;">//&nbsp;Whit&nbsp;Monday/Pinksteren</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(9999,&nbsp;12,&nbsp;25),&nbsp;<span style="color:green;">//&nbsp;Christmas&nbsp;Day/Eerste&nbsp;kerstdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(9999,&nbsp;12,&nbsp;26),&nbsp;<span style="color:green;">//&nbsp;St.&nbsp;Stephen&#39;s&nbsp;Day/Tweede&nbsp;kerstdag</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">#endregion</span> }</pre> </p> <p> My old computer isn't happy about having to compile 71,918 lines of C# in a single file, but it's doable, and as far as I can tell, Visual Studio caches the result of compilation, so as long as I don't change the file, there's little adverse effect. </p> <h3 id="236a9d22699c4c33b840ef997a6e1fc0"> Unit tests <a href="#236a9d22699c4c33b840ef997a6e1fc0" title="permalink">#</a> </h3> <p> In order to verify that the implementation works, I wrote this parametrised test: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DateTimeExtensionsTests</span> { &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Theory</span>] &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-06&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-06&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Monday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-07&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-07&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Tuesday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-08&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-08&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Wednesday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-09&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-09&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Thursday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-10&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-10&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Friday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-11&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-10&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Saturday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-03-12&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-03-10&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Sunday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-04-14&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-04-13&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Good&nbsp;Friday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-04-15&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-04-13&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Saturday&nbsp;after&nbsp;Good&nbsp;Friday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-04-16&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-04-13&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Sunday&nbsp;after&nbsp;Good&nbsp;Friday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-04-17&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-04-13&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Easter&nbsp;Monday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2017-05-25&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2017-05-24&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Ascension&nbsp;Day&nbsp;-&nbsp;Thursday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;2110-05-26&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;2110-05-23&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Whit&nbsp;Monday</span> &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;9713-05-05&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;9713-05-04&quot;</span>)]&nbsp;<span style="color:green;">//&nbsp;Liberation&nbsp;Day</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AdjustToLatestPrecedingDutchBankDayReturnsCorrectResult( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;sutS, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;expectedS) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Parse(sutS); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.AdjustToLatestPrecedingDutchBankDay(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#2b91af;">DateTimeOffset</span>.Parse(expectedS),&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> All test cases pass. This works in <code>[Theory]</code>, but unfortunately, it turns out, it doesn't work in practice. </p> <p> When used in an ASP.NET Web API application, <code>AdjustToLatestPrecedingDutchBankDay</code> throws a <code>StackOverflowException</code>. It took me a while to figure out why, but it turns out that the stack size is smaller in IIS than when you run a 'normal' .NET process, such as an automated test. </p> <p> <code>System.DateTime</code> is a value type, and as far as I can tell, it uses some stack space during initialisation. When the <code>DateTimeExtensions</code> class is first used, the static <code>dutchHolidays</code> array is initialised, and that uses enough stack space to exhaust the stack when running in IIS. </p> <h3 id="9b3a41ee005c4a0291929802f0d3a3a0"> Final implementation <a href="#9b3a41ee005c4a0291929802f0d3a3a0" title="permalink">#</a> </h3> <p> The stack space problem seems to be related to <code>DateTime</code> initialisation. If I store a similar number of 64-bit integers in an array, it seems that there's no problem. </p> <p> First, I had to modify the <code>formatHoliday</code> function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">formatHoliday</span>&nbsp;(h&nbsp;:&nbsp;Model.PublicHoliday)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;t,&nbsp;y,&nbsp;m,&nbsp;d&nbsp;=&nbsp;h.Date.Ticks,&nbsp;h.Date.Year,&nbsp;h.Date.Month,&nbsp;h.Date.Day &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%19i</span><span style="color:#a31515;">L,&nbsp;//&nbsp;</span><span style="color:teal;">%i</span><span style="color:#a31515;">-</span><span style="color:teal;">%02i</span><span style="color:#a31515;">-</span><span style="color:teal;">%02i</span><span style="color:#a31515;">,&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">/</span><span style="color:teal;">%s</span><span style="color:#a31515;">&quot;</span>&nbsp;t&nbsp;y&nbsp;m&nbsp;d&nbsp;h.Name&nbsp;h.LocalName</pre> </p> <p> This enabled me to generate a new file with C# code fragments, but now containing ticks instead of <code>DateTime</code> values. Copying those C# fragments into my file gave me this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DateTimeExtensions</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;AdjustToLatestPrecedingDutchBankDay( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;candidate&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;(!(IsDutchBankDay(candidate.DateTime))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate&nbsp;=&nbsp;candidate.AddDays(-1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;candidate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsDutchBankDay(<span style="color:#2b91af;">DateTime</span>&nbsp;date) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(date.DayOfWeek&nbsp;==&nbsp;<span style="color:#2b91af;">DayOfWeek</span>.Saturday) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(date.DayOfWeek&nbsp;==&nbsp;<span style="color:#2b91af;">DayOfWeek</span>.Sunday) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dutchHolidays.Contains(date.Date.Ticks)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">#region</span>&nbsp;Dutch&nbsp;holidays &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">long</span>[]&nbsp;dutchHolidays&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636188256000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-01-01,&nbsp;New&nbsp;Year&#39;s&nbsp;Day/Nieuwjaarsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636277248000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-04-14,&nbsp;Good&nbsp;Friday/Goede&nbsp;Vrijdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636279840000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-04-17,&nbsp;Easter&nbsp;Monday/&nbsp;Pasen</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636288480000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-04-27,&nbsp;King&#39;s&nbsp;Day/Koningsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636295392000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-05-05,&nbsp;Liberation&nbsp;Day/Bevrijdingsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636312672000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-05-25,&nbsp;Ascension&nbsp;Day/Hemelvaartsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636322176000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-06-05,&nbsp;Whit&nbsp;Monday/Pinksteren</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636497568000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-12-25,&nbsp;Christmas&nbsp;Day/Eerste&nbsp;kerstdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636498432000000000L,&nbsp;<span style="color:green;">//&nbsp;2017-12-26,&nbsp;St.&nbsp;Stephen&#39;s&nbsp;Day/Tweede&nbsp;kerstdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636503616000000000L,&nbsp;<span style="color:green;">//&nbsp;2018-01-01,&nbsp;New&nbsp;Year&#39;s&nbsp;Day/Nieuwjaarsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;636579648000000000L,&nbsp;<span style="color:green;">//&nbsp;2018-03-30,&nbsp;Good&nbsp;Friday/Goede&nbsp;Vrijdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Lots&nbsp;and&nbsp;lots&nbsp;of&nbsp;dates...</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3155171616000000000L,&nbsp;<span style="color:green;">//&nbsp;9999-05-06,&nbsp;Ascension&nbsp;Day/Hemelvaartsdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3155181120000000000L,&nbsp;<span style="color:green;">//&nbsp;9999-05-17,&nbsp;Whit&nbsp;Monday/Pinksteren</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3155372928000000000L,&nbsp;<span style="color:green;">//&nbsp;9999-12-25,&nbsp;Christmas&nbsp;Day/Eerste&nbsp;kerstdag</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3155373792000000000L,&nbsp;<span style="color:green;">//&nbsp;9999-12-26,&nbsp;St.&nbsp;Stephen&#39;s&nbsp;Day/Tweede&nbsp;kerstdag</span> &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">#endregion</span> }</pre> </p> <p> That implementation still passes all tests, <em>and</em> works at in practice as well. </p> <h3 id="4944b52d147c4cd9b09ee41d419b7e3b"> Conclusion <a href="#4944b52d147c4cd9b09ee41d419b7e3b" title="permalink">#</a> </h3> <p> It took me some time to find a satisfactory solution. I had more than once false start, until I ultimately arrived at the solution I've described here. I consider it simple because it's self-contained, deterministic, easy to understand, and fairly easy to maintain. I even left a comment in the code (not shown here) that described how to recreate the configuration data using the F# script shown here. </p> <p> The first solution that comes into your mind may not be the simplest solution, but if you take some time to consider alternatives, you may save yourself and your colleagues some future grief. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="43714419a4cd449db2bd8ac504c6062d"> <div class="comment-author"><a href="http://stackoverflow.com/users/861565/jadarnel27">jadarnel27</a> <a href="#43714419a4cd449db2bd8ac504c6062d">#</a></div> <div class="comment-content"> <p> What do you think about creating an "admin page" that would allow users to configure the bank holidays themselves (which would then be persisted in the application database)? This moves the burden of correctness to the administrators of the application, who I'm sure are highly motivated to get this right - as well as maintain it. It also removes the need for a deployment in the face of changing holidays. </p> <p> For the sake of convenience, you could still "seed" the database with the values generated by your F# script </p> </div> <div class="comment-date">2017-04-25 20:00 UTC</div> </div> <div class="comment" id="725d41b6291241699cd9ce050ed9afcd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#725d41b6291241699cd9ce050ed9afcd">#</a></div> <div class="comment-content"> <p> jadarnel27, thank you for writing. Your suggestion could be an option as well, but is hardly the simplest solution. In order to implement that, I'd need to add an administration web site for my application, program the user interface, connect the administration site and my original application (a REST API) to a persistent data source, write code for input validation, etcetera. </p> <p> Apart from all that work, the bank holidays would have to be stored in an out-of-process data store, such as a database or NoSQL data store, because the REST API that needs this feature is running in a server farm. This adds latency to each lookup, as well as a potential error source. What should happen if the connection to the data store is broken? Additionally, such a data store should be backed up, so we'd also need to establish an operational procedure to ensure that that happens. </p> <p> It was never a requirement that the owners of the application should be able to administer this themselves. It's certainly an option, but it's so much more complex than the solution outlined above that I think one should start by making a return-on-investment analysis. </p> </div> <div class="comment-date">2017-04-26 7:55 UTC</div> </div> <div class="comment" id="d3593eafc6e2407098ff496dbf49af8d"> <div class="comment-author"><a href="http://www.lefdal.cc/info">Alf Kåre Lefdal</a> <a href="#d3593eafc6e2407098ff496dbf49af8d">#</a></div> <div class="comment-content"> <p> Another option: Calculate the holidays! I think you might find som useful code in my <a href="https://github.com/aklefdal/HolidaysApi/tree/master/HolidaysApi">HolidaysAPI</a>. It is even in F#, and the Web project is based upon a course or blog post by you. </p> </div> <div class="comment-date">2017-04-26 19:26 UTC</div> </div> <div class="comment" id="fa79c6bf93714c5bbe7f1886c57600e5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fa79c6bf93714c5bbe7f1886c57600e5">#</a></div> <div class="comment-content"> <p> Alf, thank you for writing. Apart from the alternative library, how is that different from the option I covered under the heading <em>Option: Nager.Date?</em> </p> </div> <div class="comment-date">2017-04-27 5:38 UTC</div> </div> <div class="comment" id="e416103fcf044d2c845bfbabf97e0370"> <div class="comment-author"><a href="http://blog.enazarov.ru">EQR</a> <a href="#e416103fcf044d2c845bfbabf97e0370">#</a></div> <div class="comment-content"> <p> Great post, thanks. The minor point here is that it is probably not so effective to do Contains over long[]. I'd consider using something that can check the value existance faster. </p> </div> <div class="comment-date">2017-04-30 19:00 UTC</div> </div> <div class="comment" id="068048c6a2af4d71826915b6d20f2d9a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#068048c6a2af4d71826915b6d20f2d9a">#</a></div> <div class="comment-content"> <p> EQR, thank you for writing. The performance profile of the implementation wasn't my main concern with this article, so it's likely that it can be improved. </p> <p> I did do some lackadaisical performance testing, but didn't detect any measurable difference between the implementation shown here, and one using a HashSet. On the other hand, there are other options I didn't try at all. One of these could be to perform a binary search, since the array is already ordered. </p> </div> <div class="comment-date">2017-05-15 11:03 UTC</div> </div> <div class="comment" id="6ef7d6e045264e939b740f75a9dd4cb8"> <div class="comment-author"><a href="https://evilpilaf.io/">Thomas</a> <a href="#6ef7d6e045264e939b740f75a9dd4cb8">#</a></div> <div class="comment-content"> <p> Hi Mark, thanks for this post. One small note on your selection of bank holidays. Liberation day is an official bank holiday, but only for civil servants, for us 'normal' people this only happens every 5 years. This is reflected in Nager.Date wrong <a href="https://github.com/tinohager/Nager.Date/blob/master/Nager.Date/PublicHolidays/NetherlandsProvider.cs#L51">here</a> </p> </div> <div class="comment-date">2017-07-25 13:35 UTC</div> </div> <div class="comment" id="85c260d645e249cf9ee6f1f730b6f32e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#85c260d645e249cf9ee6f1f730b6f32e">#</a></div> <div class="comment-content"> <p> Thomas, thank you for writing. That just goes to show, I think, that holiday calculation is as complicated as any other 'business logic'. It should be treated as such, and not as an algorithmic calculation. </p> </div> <div class="comment-date">2017-07-25 15:12 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A reusable ApiController Adapter https://blog.ploeh.dk/2017/03/30/a-reusable-apicontroller-adapter 2017-03-30T10:17:00+00:00 Mark Seemann <div id="post"> <p> <em>Refactor ApiController Adapters to a single, reusable base class.</em> </p> <p> Regular readers of this blog will know that I write many RESTful APIs in F#, using ASP.NET Web API. Since I like to write functional F#, but ASP.NET Web API is an object-oriented framework, I prefer to escape the object-oriented framework as soon as possible. (In general, it makes good architectural sense to write most of your code as framework-independent as possible.) </p> <p> (Regular readers will also have <a href="/2017/01/03/decoupling-application-errors-from-domain-models">seen the above paragraph before</a>.) </p> <p> To bridge the gap between the object-oriented framework and my functional code, I implement Controller classes like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CreditCardController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;portalId&nbsp;req&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;(resp&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;resp&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">RouteFailure</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">NotFound</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_</pre> </p> <p> The above example is a Controller that handles <a href="/2016/12/16/from-rest-to-algebraic-data">incoming payment data</a>. It immediately delegates all work to an injected <code>imp</code> function, and pattern matches on the return value in order to return correct responses. </p> <p> This <code>CreditCardController</code> extends ApiController in order to work nicely with ASP.NET Web API, while the injected <code>imp</code> function is written using functional programming. If you want to be charitable, you could say that the Controller is an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> between ASP.NET Web API and the functional F# API that actually implements the service. If you want to be cynical, you could also call it an <a href="http://wiki.c2.com/?AnticorruptionLayer">anti-corruption layer</a>. </p> <p> This works well, but tends to become repetitive: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;System <span style="color:blue;">open</span>&nbsp;System.Web.Http <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">BoundaryFailure</span>&nbsp;= |&nbsp;<span style="color:navy;">RouteFailure</span> |&nbsp;<span style="color:navy;">ValidationFailure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span> |&nbsp;<span style="color:navy;">IntegrationFailure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">HomeController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;[&lt;<span style="color:teal;">AllowAnonymous</span>&gt;] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Get</span>&nbsp;()&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;()&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;(resp&nbsp;:&nbsp;<span style="color:teal;">HomeDtr</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;resp&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">RouteFailure</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">NotFound</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_ <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CreditCardController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;portalId&nbsp;req&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;(resp&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;resp&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">RouteFailure</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">NotFound</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_ <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CreditCardRecurrentStartController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;portalId&nbsp;req&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;(resp&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;resp&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">RouteFailure</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">NotFound</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_ <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CreditCardRecurrentController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;transactionKey&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;:&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;portalId&nbsp;transactionKey&nbsp;req&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;(resp&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;resp&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">RouteFailure</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">NotFound</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_ <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PushController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PushRequestDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;portalId&nbsp;req&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">RouteFailure</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">NotFound</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_</pre> </p> <p> At this point in our code base, we had five Controllers, and they all had similar implementations. They all pass their input parameters to their injected <code>imp</code> functions, and they all pattern match in exactly the same way. There are, however, small variations. Notice that one of the Controllers expose an HTTP GET operation, whereas the other four expose a POST operation. Conceivably, there could also be Controllers that allow both GET and POST, and so on, but this isn't the case here. </p> <p> The parameter list for each of the action methods also vary. Some take two arguments, one takes three, and the GET method takes none. </p> <p> Another variation is that <code>HomeController.Get</code> is annotated with an <code>[&lt;AllowAnonymous&gt;]</code> attribute, while the other action methods aren't. </p> <p> Despite these variations, it's possible to make the code less repetitive. </p> <p> You'd think you could easily refactor the code by turning the identical pattern matches into a reusable function. Unfortunately, it's not so simple, because the methods used to return HTTP responses are all <code>protected</code> (to use a C# term for something that F# doesn't even have). You can call <code>this.Ok ()</code> or <code>this.NotFound ()</code> from a derived class, but not from a 'free' <code>let</code>-bound function. </p> <p> After much trial and error, I finally arrived at this reusable base class: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:teal;">Imp</span>&lt;&#39;inp,&nbsp;&#39;out&gt;&nbsp;=&nbsp;&#39;inp&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">Result</span>&lt;&#39;out,&nbsp;<span style="color:teal;">BoundaryFailure</span>&gt; [&lt;<span style="color:teal;">AbstractClass</span>&gt;] <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">FunctionController</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Imp&lt;&#39;a,&#39;b&gt;&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;IHttpActionResult</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Execute</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;req&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;resp&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;resp&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">RouteFailure</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">NotFound</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_</pre> </p> <p> It's been more than a decade, I think, since I last used inheritance to enable reuse, but in this case I could find no other way because of the design of ApiController. It gets the job done, though: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">HomeController</span>&nbsp;(<span style="color:navy;">imp</span>&nbsp;:&nbsp;<span style="color:teal;">Imp</span>&lt;_,&nbsp;<span style="color:teal;">HomeDtr</span>&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">FunctionController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;[&lt;<span style="color:teal;">AllowAnonymous</span>&gt;] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Get</span>&nbsp;()&nbsp;=&nbsp;this.<span style="color:navy;">Execute</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;() <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CreditCardController</span>&nbsp;(<span style="color:navy;">imp</span>&nbsp;:&nbsp;<span style="color:teal;">Imp</span>&lt;_,&nbsp;<span style="color:teal;">PaymentDtr</span>&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">FunctionController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">Execute</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(portalId,&nbsp;req) <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CreditCardRecurrentStartController</span>&nbsp;(<span style="color:navy;">imp</span>&nbsp;:&nbsp;<span style="color:teal;">Imp</span>&lt;_,&nbsp;<span style="color:teal;">PaymentDtr</span>&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">FunctionController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">Execute</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(portalId,&nbsp;req) <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CreditCardRecurrentController</span>&nbsp;(<span style="color:navy;">imp</span>&nbsp;:&nbsp;<span style="color:teal;">Imp</span>&lt;_,&nbsp;<span style="color:teal;">PaymentDtr</span>&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">FunctionController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;transactionKey&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PaymentDtr</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">Execute</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(portalId,&nbsp;transactionKey,&nbsp;req) <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PushController</span>&nbsp;(<span style="color:navy;">imp</span>&nbsp;:&nbsp;<span style="color:teal;">Imp</span>&lt;_,&nbsp;<span style="color:teal;">unit</span>&gt;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">FunctionController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PushRequestDtr</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">Execute</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(portalId,&nbsp;req)</pre> </p> <p> Notice that all five Controllers now derive from <code>FunctionController</code> instead of <code>ApiController</code>. The <code>HomeController.Get</code> method is still annotated with the <code>[&lt;AllowAnonymous&gt;]</code> attribute, and it's still a GET operation, whereas all the other methods still implement POST operations. You'll also notice that the various <code>Post</code> methods have retained their varying number of parameters. </p> <p> In order to make this work, I had to make one trivial change: previously, all the <code>imp</code> functions were curried, but that doesn't fit into a single reusable base implementation. If you consider the type of <code>FunctionController.Execute</code>, you can see that the <code>imp</code> function is expected to take a single input value of the type <code>'a</code> (and return a value of the type <code>Result&lt;'b, BoundaryFailure&gt;</code>). Since any <code>imp</code> function can only take a single input value, I had to uncurry them all. You can see that now all the <code>Post</code> methods pass their input parameters as a single tuple to their injected <code>imp</code> function. </p> <p> You may be wondering about the <code>Imp&lt;&#39;inp,&nbsp;&#39;out&gt;</code> type alias. It's not strictly necessary, but helps keep the code clear. As I've attempted to indicate with the code comment above the <code>Execute</code> method, it's generic. When used in a derived Controller, the compiler can infer the type of <code>'a</code> because it already knows the types of the input parameters. For example, in <code>CreditCardController.Post</code>, the input parameters are already annotated as <code>string</code> and <code>PaymentDtr</code>, so the compiler can easily infer the type of <code>(portalId, req)</code>. </p> <p> On the other hand, the compiler can't infer the type of <code>'b</code>, because that value doesn't relate to <code>IHttpActionResult</code>. To help the compiler, I introduced the <code>Imp</code> type, because it enabled me to concisely annotate the type of the output value, while using a wild-card type for the input type. </p> <p> I wouldn't mind getting rid of Controllers altogether, but this is as close as I've been able to get with ASP.NET Web API. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="91a69484cd77434c921dcf86535245a8"> <div class="comment-author"><a href="https://github.com/Alxandr">Aleksander Heintz</a> <a href="#91a69484cd77434c921dcf86535245a8">#</a></div> <div class="comment-content"> <p> I find a few issues with your blog post. Technically, I don't think anything you write in it is wrong, but there are a few of the statements I find problematic in the sense that people will probably read the blog post and conclude that "this is the only solution because of X" when in fact X is trivial to circumvent. </p> <p> The main issue I have with your post is the following: </p> <p> You'd think you could easily refactor the code by turning the identical pattern matches into a reusable function. Unfortunately, it's not so simple, because the methods used to return HTTP responses are all <code>protected</code> (to use a C# term for something that F# doesn't even have). You can call <code>this.Ok ()</code> or <code>this.NotFound ()</code> from a derived class, but not from a 'free' let-bound function. </p> <p> As stated earlier, there is nothing in your statement that is technically wrong, but I'd argue that you've reached the wrong conclusion due to lack of data. Or in this case familiarity with Web API and it's source code. If you go look at the source for <a href="https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http/ApiController.cs"><code>ApiController</code></a>, you will notice that while true, the methods are <code>protected</code>, they are also trivial one-liners (helper methods) calling public APIs. It is completely possible to create a helper ala: </p> <p> <pre>// NOTE: Untested let handle (ctrl: ApiController) = function | Success of result -&gt; OkNegotiatedContentResult (result, ctrl) | Failure RouteFailure -&gt; NotFoundResult (ctrl) | Failure (ValidationFailure msg) -&gt; BadRequestErrorMessageResult (msg, ctrl) | Failure (IntegrationFailure msg) -&gt; ExceptionResult ((InvalidOperationException msg), ctrl)</pre> </p> <p> Another thing you can do is implement IHttpActionResult on your discriminate union, so you can just return it directly from you controller, in which case you'd end up with code like this: </p> <p> <pre>member this.Post (portalId : string, req : PaymentDtr) = imp portalId req</pre> </p> <p> It's definitely a bit more work, but in no way is it undoable. Thirdly, Web API (and especially the new MVC Core which has taken over for it) is incredibly pluggable through it's DI. You don't have to use the base <code>ApiController</code> class if you don't want to. You can override the resolution logic, the handing of return types, routing, you name it. It should for instance be entirely doable to write a handler for "controllers" that look like this: </p> <p> <pre>[&lt;Controller&gt;] module MyController = [&lt;AllowAnonymous&gt;] // assume impl is imported and in scope here - this controller has no DI let post (portalId : string) (req : PaymentDtr) = impl portalId req</pre> </p> <p> This however would probably be a bit of work requiring quite a bit of reflection voodoo. But somebody could definitely do it and put it in a library, and it would be nuget-installable for all. </p> <p> Anyways, I hope this shows you that there are more options to solve the problem. And who knows, you may have considered all of them and concluded them unviable. I've personally dug through the source code of both MVC and Web API a few times which is why I know a bunch of this stuff, and I just figured you might want to know some of it too :). </p> </div> <div class="comment-date">2017-03-30 22:06 UTC</div> </div> <div class="comment" id="e2120b23cd184cd5ac86dc142a16f1f6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e2120b23cd184cd5ac86dc142a16f1f6">#</a></div> <div class="comment-content"> <p> Aleksander, thank you for writing. I clearly have some scars from working with ASP.NET Web API since version 0.6 (or some value in that neighbourhood). In general, if a method is defined on <code>ApiController</code>, I've learned the hard way that such methods tend to be tightly coupled to the Controller. Often, such methods even look into the untyped context dictionary in the request message object, so I've gotten used to use them whenever they're present. </p> <p> These scars have prevented me from pushing the envelope on the 'new' methods that return various <code>IHttpActionResult</code> objects, but as you write, they truly are thin wrappers over constructors. </p> <p> This does, indeed, enable us to write the mapping of result values as a let-bound function. Thank you for pointing that out! </p> <p> Mine ended up looking like the following. We've added a few more success and error cases since I originally wrote this article, so it looks more complex than the initial example. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">toHttpResult</span>&nbsp;(controller&nbsp;:&nbsp;<span style="color:teal;">ApiController</span>)&nbsp;result&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;result&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;(<span style="color:navy;">OK</span>&nbsp;resp)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">OkNegotiatedContentResult</span>&nbsp;(resp,&nbsp;controller)&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;(<span style="color:navy;">Created</span>&nbsp;(resp,&nbsp;location))&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">CreatedNegotiatedContentResult</span>&nbsp;(location,&nbsp;resp,&nbsp;controller)&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">RouteFailure</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:teal;">NotFoundResult</span>&nbsp;controller&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">BadRequestErrorMessageResult</span>&nbsp;(msg,&nbsp;controller)&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;resp&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controller.Request.<span style="color:navy;">CreateErrorResponse</span>&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">HttpStatusCode</span>.InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">ResponseMessageResult</span>&nbsp;resp&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">StabilityFailure</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;resp&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:teal;">HttpResponseMessage</span>&nbsp;(<span style="color:teal;">HttpStatusCode</span>.ServiceUnavailable) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resp.Headers.RetryAfter&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">RetryConditionHeaderValue</span>&nbsp;(<span style="color:teal;">TimeSpan</span>.<span style="color:navy;">FromMinutes</span>&nbsp;5.) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">ResponseMessageResult</span>&nbsp;resp&nbsp;:&gt;&nbsp;_</pre> </p> <p> Letting my <code>BoundaryFailure</code> discriminated union implement <code>IHttpActionResult</code> sounds promising, but how would that be possible? </p> <p> The interface has a single method with the type <code>CancellationToken -&gt; Task&lt;HttpResponseMessage&gt;</code>. In order to create e.g. <code>NotFoundResult</code>, you need either an <code>HttpRequestMessage</code> or an <code>ApiController</code>, and none of those are available when <code>IHttpActionResult.ExecuteAsync</code> is invoked. </p> <p> When it comes to not having to derive from <code>ApiController</code>, I'm aware that even on the full .NET framework (we're not on .NET Core), 'all' the framework needs are <code>IHttpController</code> instances. As you imply, plugging into the framework is likely to be non-trivial, and so far, I haven't found that such an effort would be warranted. I might use it if someone else wrote it, though :) </p> </div> <div class="comment-date">2017-03-31 13:13 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency rejection https://blog.ploeh.dk/2017/02/02/dependency-rejection 2017-02-02T08:56:00+00:00 Mark Seemann <div id="post"> <p> <em>In functional programming, the notion of dependencies must be rejected. Instead, applications should be composed from pure and impure functions.</em> </p> <p> This is the third article in a small article series called <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">from dependency injection to dependency rejection</a>. In the <a href="/2017/01/30/partial-application-is-dependency-injection">previous article in the series</a>, you learned that dependency injection can't be functional, because it makes everything impure. In this article, you'll see what to do instead. </p> <h3 id="198f33430dcd4d7381cdb9993c0ed8ee"> Indirect input and output <a href="#198f33430dcd4d7381cdb9993c0ed8ee" title="permalink">#</a> </h3> <p> One of the first concepts you learned when you learned to program was that units of operation (functions, methods, procedures) take input and produce output. Input is in the form of input parameters, and output is in the form of return values. (Sometimes, though, a method returns nothing, but we know from category theory that nothing is also a value (called <em>unit</em>).) </p> <p> In addition to such input and output, a unit with dependencies also take indirect input, and produce indirect output: </p> <p> <img src="/content/binary/unit-with-dependencies-and-direct-and-indirect-input-and-output.png" alt="A unit with dependencies and direct and indirect input and output."> </p> <p> When a unit queries a dependency for data, the data returned from the dependency is indirect input. In the restaurant reservation example used in this article series, when <code>tryAccept</code> calls <code>readReservations</code>, the returned reservations are indirect input. </p> <p> Likewise, when a unit invokes a dependency, all arguments passed to that dependency constitute indirect output. In the example, when <code>tryAccept</code> calls <code>createReservation</code>, the reservation value it uses as input argument to that function call becomes output. The intent, in this case, is to save the reservation in a database. </p> <h3 id="3cb914d1dcf84ff79fc333362927a19c"> From indirect output to direct output <a href="#3cb914d1dcf84ff79fc333362927a19c" title="permalink">#</a> </h3> <p> Instead of producing indirect output, you can refactor functions to produce direct output. </p> <p> <img src="/content/binary/unit-with-dependencies-and-direct-input-and-output-but-no-indirect-output.png" alt="A unit with dependencies and direct input and output, but no indirect output."> </p> <p> Such a refactoring is often problematic in mainstream object-oriented languages like C# and Java, because you wish to control the circumstances in which the indirect output must be produced. Indirect output often implies side-effects, but perhaps the side-effect must only happen when certain conditions are fulfilled. In the restaurant reservation example, the desired side-effect is to add a reservation to a database, but this must only happen when the restaurant has sufficient remaining capacity to serve the requested number of people. Since languages like C# and Java are statement-based, it can be difficult to separate the decision from the action. </p> <p> In expression-based languages like F# and Haskell, <a href="/2016/09/26/decoupling-decisions-from-effects">it's trivial to decouple decisions from effects</a>. </p> <p> In the <a href="/2017/01/30/partial-application-is-dependency-injection">previous article</a>, you saw a version of <code>tryAccept</code> with this signature: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;(DateTimeOffset&nbsp;-&gt;&nbsp;Reservation&nbsp;list)&nbsp;-&gt;&nbsp;(Reservation&nbsp;-&gt;&nbsp;int)&nbsp;-&gt;&nbsp;Reservation</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;int&nbsp;option</span></pre> </p> <p> The second function argument, with the type <code>Reservation&nbsp;-&gt;&nbsp;int</code>, produces indirect output. The <code>Reservation</code> value is the output. The function even violates Command Query Separation and returns the database ID of the added reservation, so that's additional indirect input. The overall function returns <code>int option</code>: the database ID if the reservation was added, and <code>None</code> if it wasn't. </p> <p> Refactoring the indirect output to direct output is easy, then: just remove the <code>createReservation</code> function and return the <code>Reservation</code> value instead: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;(DateTimeOffset&nbsp;-&gt;&nbsp;Reservation&nbsp;list)&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;Reservation&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;capacity&nbsp;<span style="color:navy;">readReservations</span>&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">readReservations</span>&nbsp;reservation.Date&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">sumBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;|&gt;&nbsp;<span style="color:navy;">Some</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> Notice that this refactored version of <code>tryAccept</code> returns a <code>Reservation option</code> value. The implication is that the reservation was accepted if the return value is a <code>Some</code> case, and rejected if the value is <code>None</code>. The decision is embedded in the value, but decoupled from the side-effect of writing to the database. </p> <p> This function clearly never writes to the database, so at the boundary of your application, you'll have to connect the decision to the effect. To keep the example consistent with the previous article, you can do this in a <code>tryAcceptComposition</code> function, like this: </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryAcceptComposition</span>&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;10&nbsp;(<span style="color:teal;">DB</span>.<span style="color:navy;">readReservations</span>&nbsp;connectionString) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:teal;">DB</span>.<span style="color:navy;">createReservation</span>&nbsp;connectionString)</pre> </p> <p> Notice that the type of <code>tryAcceptComposition</code> remains <code>Reservation&nbsp;-&gt;&nbsp;int&nbsp;option</code>. This is a true refactoring. The overall API remains the same, as does the behaviour. The reservation is added to the database only if there's sufficient remaining capacity, and in that case, the ID of the reservation is returned. </p> <h3 id="4e81006bbf974c1797ba57935681f938"> From indirect input to direct input <a href="#4e81006bbf974c1797ba57935681f938" title="permalink">#</a> </h3> <p> Just as you can refactor from indirect output to direct output can you refactor from indirect input to direct input. </p> <p> <img src="/content/binary/unit-with-dependencies-and-direct-input-and-output.png" alt="A unit with dependencies and direct input and output."> </p> <p> Again, in statement-based languages like C# and Java, this may be problematic, because you may wish to defer a query, or base it on a decision inside the unit. In expression-based languages you can decouple decisions from effects, and deferred execution can always be done by lazy evaluation, if that's required. In the case of the current example, however, the refactoring is easy: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;Reservation&nbsp;list&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;Reservation&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;capacity&nbsp;reservations&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">sumBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;|&gt;&nbsp;<span style="color:navy;">Some</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> Instead of calling a (potentially impure) function, this version of <code>tryAccept</code> takes a list of existing reservations as input. It still sums over all the quantities, and the rest of the code is the same as before. </p> <p> Obviously, the list of existing reservations must come from somewhere, like a database, so <code>tryAcceptComposition</code> will still have to take care of that: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;c</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">flip</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;y&nbsp;=&nbsp;<span style="color:navy;">f</span>&nbsp;y&nbsp;x <span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryAcceptComposition</span>&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;reservation.Date &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">DB</span>.<span style="color:navy;">readReservations</span>&nbsp;connectionString &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">flip</span>&nbsp;(<span style="color:navy;">tryAccept</span>&nbsp;10)&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:teal;">DB</span>.<span style="color:navy;">createReservation</span>&nbsp;connectionString)</pre> </p> <p> The type and behaviour of this composition is still the same as before, but the data flow is different. First, the function queries the database, which is an impure operation. Then, it pipes the resulting list of reservations to <code>tryAccept</code>, which is now a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>. It returns a <code>Reservation option</code> that's finally mapped to another impure operation, which writes the reservation to the database if the reservation was accepted. </p> <p> You'll notice that I also added a <code>flip</code> function in order to make the composition more concise, but I could also have used a lambda expression when invoking <code>tryAccept</code>. The <code>flip</code> function is a part of Haskell's standard library, but isn't in F#'s core library. It's not crucial to the example, though. </p> <h3 id="5e098486e1bd4d6daaffdab01c8e7e86"> Evaluation <a href="#5e098486e1bd4d6daaffdab01c8e7e86" title="permalink">#</a> </h3> <p> Did you notice that in the previous diagram, above, all arrows between the unit and its dependencies were gone? This means that the unit no longer has any dependencies: </p> <p> <img src="/content/binary/unit-with-direct-input-and-output.png" alt="A unit with direct input and output, but no dependencies."> </p> <p> Dependencies are, by their nature, impure, and since pure functions can't call impure functions, functional programming must reject the notion of dependencies. Pure functions can't <em>depend</em> on impure functions. </p> <p> Instead, pure functions must take direct input and produce direct output, and the impure boundary of an application must compose impure and pure functions together in order to achieve the desired behaviour. </p> <p> In the previous article, you saw how Haskell can be used to evaluate whether or not an implementation is functional. You can port the above F# code to Haskell to verify that this is the case. </p> <p> <pre><span style="color:#600277;">tryAccept</span>&nbsp;::&nbsp;Int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Reservation</span>]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Maybe&nbsp;<span style="color:blue;">Reservation</span> tryAccept&nbsp;capacity&nbsp;reservations&nbsp;reservation&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;<span style="color:#666666;">=</span>&nbsp;sum&nbsp;<span style="color:#666666;">$</span>&nbsp;map&nbsp;quantity&nbsp;reservations &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;reservedSeats&nbsp;<span style="color:#666666;">+</span>&nbsp;quantity&nbsp;reservation&nbsp;<span style="color:#666666;">&lt;=</span>&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;Just&nbsp;<span style="color:#666666;">$</span>&nbsp;reservation&nbsp;{&nbsp;isAccepted&nbsp;<span style="color:#666666;">=</span>&nbsp;True&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;Nothing</pre> </p> <p> This version of <code>tryAccept</code> is pure, and compiles, but as you learned in the previous article, that's not the crucial question. The question is whether the composition compiles? </p> <p> <pre><span style="color:#600277;">tryAcceptComposition</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;(Maybe&nbsp;Int) tryAcceptComposition&nbsp;reservation&nbsp;<span style="color:#666666;">=</span>&nbsp;runMaybeT&nbsp;<span style="color:#666666;">$</span> &nbsp;&nbsp;liftIO&nbsp;(<span style="color:#dd0000;">DB</span><span style="color:#666666;">.</span>readReservations&nbsp;connectionString&nbsp;<span style="color:#666666;">$</span>&nbsp;date&nbsp;reservation) &nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;<span style="color:#dd0000;">MaybeT</span>&nbsp;<span style="color:#666666;">.</span>&nbsp;return&nbsp;<span style="color:#666666;">.</span>&nbsp;flip&nbsp;(tryAccept&nbsp;<span style="color:#09885a;">10</span>)&nbsp;reservation &nbsp;&nbsp;<span style="color:#666666;">&gt;&gt;=</span>&nbsp;liftIO&nbsp;<span style="color:#666666;">.</span>&nbsp;<span style="color:#dd0000;">DB</span><span style="color:#666666;">.</span>createReservation&nbsp;connectionString</pre> </p> <p> This version of <code>tryAcceptComposition</code> compiles, and works as desired. The code exhibits a common pattern for Haskell: First, gather data from impure sources. Second, pass pure data to pure functions. Third, take the pure output from the pure functions, and do something impure with it. </p> <p> It's like a <a href="/2020/03/02/impureim-sandwich">sandwich</a>, with the best parts in the middle, and some necessary stuff surrounding it. </p> <h3 id="2768ff7f9aa94e7f962ba706d2cf30e6"> Summary <a href="#2768ff7f9aa94e7f962ba706d2cf30e6" title="permalink">#</a> </h3> <p> Dependencies are, by nature, impure. They're either non-deterministic, have side-effects, or both. Pure functions can't call impure functions (because that would make them impure as well), so pure functions can't have dependencies. Functional programming must reject the notion of dependencies. </p> <p> Obviously, software is only useful with impure behaviour, so instead of injecting dependencies, functional programs must be composed in impure contexts. Impure functions can call pure functions, so at the boundary, an application must gather impure data, and use it to call pure functions. This <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">automatically leads to the ports and adapters architecture</a>. </p> <p> This style of programming is surprisingly often possible, but <a href="/2017/07/10/pure-interactions">it's not a universal solution; other alternatives exist</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="b76e91ee831445549c6ca25358dda23a"> <div class="comment-author"><a href="https://dusted.codes/">Dustin Moris Gorski</a> <a href="#b76e91ee831445549c6ca25358dda23a">#</a></div> <div class="comment-content"> <p>Hi, Thank you for this blog post series. I also read your other posts on ports and adapters and the proposed architecture makes sense in terms of how it works, but I struggle to see the benefit in a real world application. Maybe let me explain my question with a quick example.</p> <p>In the 2nd blog post of this series you demonstrated this function:</p> <pre><code>// int -&gt; (DateTimeOffset -&gt; Reservation list) -&gt; (Reservation -&gt; int) -&gt; Reservation // -&gt; int option let tryAccept capacity readReservations createReservation reservation = let reservedSeats = readReservations reservation.Date |&gt; List.sumBy (fun x -&gt; x.Quantity) if reservedSeats + reservation.Quantity &lt;= capacity then createReservation { reservation with IsAccepted = true } |&gt; Some else None</code></pre> <p>If I understand it correctly this function is pure if <code>readReservations</code> and <code>createReservation</code> are both pure otherwise it is impure.</p> <p>I also understand the benefit of having a pure function, because it is a lot easier to understand the code, test the code and reason about it. That makes sense as well :).</p> <p>So in the 3rd blog post you make <code>tryAccept</code> a pure function, by removing the function dependencies and replacing it with simple values:</p> <pre><code>// int -&gt; Reservation list -&gt; Reservation -&gt; Reservation option let tryAccept capacity reservations reservation = let reservedSeats = reservations |&gt; List.sumBy (fun x -&gt; x.Quantity) if reservedSeats + reservation.Quantity &lt;= capacity then { reservation with IsAccepted = true } |&gt; Some else None</code></pre> <p>However this was only possible because you essentially moved the impure code into another new function:</p> <pre><code>// Reservation -&gt; int option let tryAcceptComposition reservation = reservation.Date |&gt; DB.readReservations connectionString |&gt; flip (tryAccept 10) reservation |&gt; Option.map (DB.createReservation connectionString)</code></pre> <p>So after all the application hasn't really reduced the total number of impure functions (still 3 in each case - <code>readReservations</code>, <code>createReservation</code> and <code>tryAccept[Composition]</code>).</p> <p>The only difference I see is that one impure function has been refactored into 2 functions - one pure and one impure. Considering that the original <code>tryAccept</code> function was already fully testable from a unit testing point of view and quite readable what is the benefit of this additional step? I would almost argue that the original <code>tryAccept</code> function was even easier to read/understand than the combination of <code>tryAccept</code> and <code>tryAcceptComposition</code>. I understand that impure functions like this are not truly functional, but in a real world application you must have some impure functions and I would like to better understand where trade-off benefit of that additional step is? Am I missing something else?</p> </div> <div class="comment-date">2017-02-03 10:34 UTC</div> </div> <div class="comment" id="bd82d4b27fdd4ce39f335f0cf1b12f1f"> <div class="comment-author"> <a href="">Mark Seemann</a> <a href="#bd82d4b27fdd4ce39f335f0cf1b12f1f">#</a></div> <div class="comment-content"> </div> <p> Dustin, thank you for writing. There are several answers to your question, depending on the perspective one is interested in. I'll see if I can cover the most important ones. </p> <h3 id="a0a9544721774f97afbaccc6643f18e1"> Is it functional? <a href="#a0a9544721774f97afbaccc6643f18e1" title="permalink">#</a> </h3> <p> On the most fundamental level, I'm interested in learning functional programming. In order to do this, I seek out strictly functional solutions to problems. Haskell is a great help in that endeavour, because it's not a hybrid language. It only allows you to do functional programming. </p> <p> Does it make sense to back-port Haskell solutions to F#, then? That depends on what one is trying to accomplish, but if the goal is nothing but learning how to do it functionally, then that goal is accomplished. </p> <h3 id="2409da61d4554493a88971d6c0205e89"> Toy examples <a href="#2409da61d4554493a88971d6c0205e89" title="permalink">#</a> </h3> <p> On another level, the example I've presented here is obviously nothing but a toy example. It's simplified, because if I presented readers with a more realistic example, the complexity of the real problem could easily drown out the message of the example. Additionally, most readers would probably give up reading. </p> <p> I'm asking my readers to pretend that the problem is more complex than the one I present here; pretend that this problem is a stand-in for a harder problem. </p> <p> In this particular context, there could be all sorts of complications: <ul> <li> Reservations could be for time slots instead of whole dates. In order to keep the example simple, I treat each reservation as simply blocking out an entire date. I once dined at a restaurant where they started serving at 19:00, and if you weren't there on time, you'd miss the first courses. Most restaurants, though, allow you to make reservations for a particular time, and many have more than one serving on a single evening. </li> <li> Most restaurants have tables, not seats. Again, the same restaurant I mentioned above seated 12 people at a bar-like arrangement facing the kitchen, but most restaurants have tables of varying sizes. If they get a reservation for three people, they may have to reserve a table for four. </li> <li> Perhaps the restaurant would like to implement a feature where, if it receives a reservation that doesn't fill out a table (like a reservation for three people, and only four-people tables are left), it'd defer the decision to see if a 'better' reservation arrives later. </li> <li> Some people make reservations, but never show up. For that reason, a restaurant may want to allow a degree of overbooking, just like airlines. How much overbooking to allow is a business decision. </li> <li> A further wrinkle on the overbooking business rule is that you may have a different overbooking policy for Fridays than for, say, Wednesdays. </li> <li> Perhaps the restaurant would like to implement a waiting-list feature as well. </li> </ul> As you can see, we could easily imagine that the business logic could be more convoluted. Keeping all of that decision logic pure would be beneficial. </p> <h3 id="176d6f6f7b164416a624d3940b4bcc3f"> Separation of concerns <a href="#176d6f6f7b164416a624d3940b4bcc3f" title="permalink">#</a> </h3> <p> In my experience, there's an entire category of software defects that occur because of state mutation in business logic. You could have an area of your code that calls other code, which calls other code, and so on, for several levels of nesting. Somewhere, deep in the bowels of such a system, a conditional statement flips a boolean flag that consequently impact how the rest of the program runs. I've seen plenty of examples of such software, and it's inhumane; it doesn't fit within human cognitive limits. </p> <p> Code that allows arbitrary side-effects is difficult to reason about. </p> <p> Knowing that an subgraph of your call tree is pure reduces defects like that. This is nothing but another way to restate the <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">command-query separation</a> principle. In F#, we still can't be sure unless we exert some discipline, but in Haskell, all it takes is a look at the type of a function or value. If it doesn't include <code>IO</code>, you know that it's pure. </p> <p> Separating pure code from impure code is separation of concern. Business logic is one concern, and I/O is another concern, and the better you can separate these, the fewer sources of defects you'll have. True, I haven't reduced the amount of code by much, but I've <em>separated</em> concerns by separating the code that contains (side) effects from the pure code. </p> <h3 id="d811d2b8b22743de869c47691b042086"> Testability <a href="#d811d2b8b22743de869c47691b042086" title="permalink">#</a> </h3> <p> It's true that the partial application version of <code>tryAccept</code> is <a href="/2015/05/07/functional-design-is-intrinsically-testable">testable, because it has isolation</a>, but the tests are more complicated than they have to be: </p> <p> <pre>[&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``tryAccept&nbsp;behaves&nbsp;correctly&nbsp;when&nbsp;it&nbsp;can&nbsp;accept``</span> &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:navy;">NonNegativeInt</span>&nbsp;excessCapacity) &nbsp;&nbsp;&nbsp;&nbsp;(expected&nbsp;:&nbsp;<span style="color:teal;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tuple2</span>.<span style="color:navy;">curry</span>&nbsp;<span style="color:navy;">id</span> &nbsp;&nbsp;&nbsp;&nbsp;&lt;!&gt;&nbsp;<span style="color:teal;">Gen</span>.reservation &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">listOf</span>&nbsp;<span style="color:teal;">Gen</span>.reservation &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;&nbsp;<span style="color:teal;">Arb</span>.<span style="color:navy;">fromGen</span>&nbsp;|&gt;&nbsp;<span style="color:teal;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;(reservation,&nbsp;reservations)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capacity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;excessCapacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;(reservations&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">sumBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;reservation.Quantity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">readReservations</span>&nbsp;=&nbsp;((=!)&nbsp;reservation.Date)&nbsp;&gt;&gt;!&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">createReservation</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((=!)&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;})&nbsp;&gt;&gt;!&nbsp;expected &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;capacity&nbsp;<span style="color:navy;">readReservations</span>&nbsp;<span style="color:navy;">createReservation</span>&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;expected&nbsp;=!&nbsp;actual [&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``tryAccept&nbsp;behaves&nbsp;correctly&nbsp;when&nbsp;it&nbsp;can&#39;t&nbsp;accept``</span> &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:navy;">PositiveInt</span>&nbsp;lackingCapacity)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tuple2</span>.<span style="color:navy;">curry</span>&nbsp;<span style="color:navy;">id</span> &nbsp;&nbsp;&nbsp;&nbsp;&lt;!&gt;&nbsp;<span style="color:teal;">Gen</span>.reservation &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">listOf</span>&nbsp;<span style="color:teal;">Gen</span>.reservation &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;&nbsp;<span style="color:teal;">Arb</span>.<span style="color:navy;">fromGen</span>&nbsp;|&gt;&nbsp;<span style="color:teal;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;(reservation,&nbsp;reservations)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capacity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(reservations&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">sumBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity))&nbsp;-&nbsp;lackingCapacity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">readReservations</span>&nbsp;_&nbsp;=&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">createReservation</span>&nbsp;_&nbsp;=&nbsp;<span style="color:navy;">failwith</span>&nbsp;<span style="color:#a31515;">&quot;Mock&nbsp;shouldn&#39;t&nbsp;be&nbsp;called.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;capacity&nbsp;<span style="color:navy;">readReservations</span>&nbsp;<span style="color:navy;">createReservation</span>&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">None</span>&nbsp;=!&nbsp;actual</pre> </p> <p> (You can find these tests in <a href="https://github.com/ploeh/dependency-rejection-samples/commit/d2387cceb81eabc349a63ab7df1249236e9b1d13">commit d2387cceb81eabc349a63ab7df1249236e9b1d13 in the accompanying sample code repository</a>.) Contrast those dependency-injection style tests to these tests against the pure version of <code>tryAccept</code>: </p> <p> <pre>[&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``tryAccept&nbsp;behaves&nbsp;correctly&nbsp;when&nbsp;it&nbsp;can&nbsp;accept``</span> &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:navy;">NonNegativeInt</span>&nbsp;excessCapacity)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tuple2</span>.<span style="color:navy;">curry</span>&nbsp;<span style="color:navy;">id</span> &nbsp;&nbsp;&nbsp;&nbsp;&lt;!&gt;&nbsp;<span style="color:teal;">Gen</span>.reservation &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">listOf</span>&nbsp;<span style="color:teal;">Gen</span>.reservation &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;&nbsp;<span style="color:teal;">Arb</span>.<span style="color:navy;">fromGen</span>&nbsp;|&gt;&nbsp;<span style="color:teal;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;(reservation,&nbsp;reservations)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capacity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;excessCapacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;(reservations&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">sumBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;reservation.Quantity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;capacity&nbsp;reservations&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;=!&nbsp;actual [&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``tryAccept&nbsp;behaves&nbsp;correctly&nbsp;when&nbsp;it&nbsp;can&#39;t&nbsp;accept``</span> &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:navy;">PositiveInt</span>&nbsp;lackingCapacity)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Tuple2</span>.<span style="color:navy;">curry</span>&nbsp;<span style="color:navy;">id</span> &nbsp;&nbsp;&nbsp;&nbsp;&lt;!&gt;&nbsp;<span style="color:teal;">Gen</span>.reservation &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">listOf</span>&nbsp;<span style="color:teal;">Gen</span>.reservation &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;&nbsp;<span style="color:teal;">Arb</span>.<span style="color:navy;">fromGen</span>&nbsp;|&gt;&nbsp;<span style="color:teal;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;(reservation,&nbsp;reservations)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capacity&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(reservations&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">sumBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity))&nbsp;-&nbsp;lackingCapacity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;capacity&nbsp;reservations&nbsp;reservation &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">None</span>&nbsp;=!&nbsp;actual</pre> </p> <p> They're simpler, and since they don't use <a href="http://xunitpatterns.com/Mock%20Object.html">mocks</a>, they're more robust. They were easier to write, and I subscribe to the spirit of <a href="http://amzn.to/SM8Yv0">GOOS</a>: <em>if test are difficult to write, the system under test should be simplified</em>. </p> <div class="comment-date">2017-02-05 20:09 UTC</div> </div> <div class="comment" id="fc271f644282434b8f9e440af126985a"> <div class="comment-author"><a href="http://stackoverflow.com/users/1523776/benjamin-hodgson?tab=profile">Benjamin Hodgson</a> <a href="#fc271f644282434b8f9e440af126985a">#</a></div> <div class="comment-content"> <p>Hi Mark,</p> <p>Thanks for your talk at NDC last month, and for writing this series! I feel that the functional community (myself included) has a habit of using examples that aren't obviously relevant to the sort of line-of-business programming most of us do in our day jobs, so articles like this are sorely needed.</p> <p>We talked a little about this in person after your talk at the conference: I wanted to highlight a potential criticism of this style of programming. Namely, there's still some important business logic being carried out by your <code>tryAcceptComposition</code> function, like checking the capacity <i>on the requested reservation date</i>. How do you unit test that <code>readReservations</code> is called with the correct date? Likewise, how do you unit test that rejected reservations don't get saved? Real world business logic isn't always purely functional in nature. Sometimes the side effects that your code performs are part of the requirements.</p> <p>The Haskell philosophy isn't about rejecting side effects outright - it's about measuring and controlling them. I wouldn't write <code>tryAcceptComposition</code> using <code>IO</code>. Instead I'd program to the interface, not the implementation, using an mtl-style class to abstract over monads which support saving and loading reservations.</p> <pre><code>class Monad m =&gt; MonadReservation m where readReservations :: ConnectionString -&gt; Date -&gt; m [Reservation] createReservation :: ConnectionString -&gt; Reservation -&gt; m ReservationId tryAcceptComposition :: MonadReservation m =&gt; Reservation -&gt; m (Maybe ReservationId) tryAcceptComposition r = runMaybeT $ do reservations &lt;- lift $ readReservations connectionString (date r) accepted &lt;- MaybeT $ return $ tryAccept 10 reservations r lift $ createReservation connectionString accepted</code></pre> <p>Code that lives in a <code>MonadReservation</code> context can read and create reservations in the database but nothing else; it doesn't have all the power of <code>IO</code>. During unit testing I can use an instance of <code>MonadReservation</code> that returns canned values, and in production I can use a monad that actually talks to the database.</p> <p>Since type classes are syntactic sugar for passing an argument, this is really just a nicer way of writing your original DI-style code. I don't advocate the "free monad" style that's presently trendy in Scala-land because I find it unnecessarily complex. 90% of the purported advantages of free monads are already supported by simpler language features.</p> <p>I suppose the main downside of this design is that you can't express it in F#, at least not cleanly. It relies on type classes and higher-kinded types.</p> <p>Hope you find this interesting, I'd love to hear what you think!</p> <p>Benjamin</p> </div> <div class="comment-date">2017-02-06 16:28 UTC</div> </div> <div class="comment" id="be3fcba2cea040f6b153b1d296e87fc7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#be3fcba2cea040f6b153b1d296e87fc7">#</a></div> <div class="comment-content"> <p> Benjamin, thank you for writing. The alternative you propose looks useful in Haskell, but, as you've already suggested, it doesn't translate well into F#. </p> <p> I write F# code professionally, whereas so far, I've only used Haskell to critique my F# code. (If someone who reads this comment would offer to pay me to write some Haskell code, please get in touch.) In other words, I still have much to learn about Haskell. I think I understand as much, however, that I'd be able to use your suggested design to unit test <code>tryAcceptComposition</code> using the <code>Identity</code> monad for Stubs, or perhaps <code>MonadWriter</code> or <code>MonadState</code> for Mocks. I'll have to try that one day... </p> <p> In F#, I write integration tests. Such tests are important regardless, and often they more closely relate to actual requirements, so I find this a worthwhile effort anyway. </p> </div> <div class="comment-date">2017-02-11 22:42 UTC</div> </div> <div class="comment" id="f02bb13a98954a67bb74adbcc7906f08"> <div class="comment-author"><a href="https://twitter.com/la_yumba">Enrico Buonanno</a> <a href="#f02bb13a98954a67bb74adbcc7906f08">#</a></div> <div class="comment-content"> <p> Hi Mark, </p> <p> thanks for the post series, which I find interesting and needed. There is one part of your post that I find deserves further exploration. You write: <blockquote> in statement-based languages like C# and Java, this may be problematic, because you may wish to defer a query, or base it on a decision inside the unit. In expression-based languages you can decouple decisions from effects, and deferred execution can always be done by lazy evaluation, if that's required. </blockquote> Firstly, I would say that you can write expression-based programs in any language that has expressions, which naturally includes C# and Java. But that's not particularly relevant to this discussion. </p> <p> More to the point, you're glossing over this as though it were a minor detail, when in fact I don't think it is. Let's explore the case in which "you may wish to defer a query, or base it on a decision inside the unit". The way you do this "by lazy evaluation" would be - I assume - by passing a function as an argument to your unit. But this is then effectively dependency injection, because you're passing in a function which has side effects, which will be called (or not) from the unit. </p> <p> So, it seems to me that your technique of extracting side effects out of the unit provides a good general guideline, but not a completely general way to replace dependency injection. </p> </div> <div class="comment-date">2017-02-16 11:47 UTC</div> </div> <div class="comment" id="36c724b49f614104842c47909cd9c916"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#36c724b49f614104842c47909cd9c916">#</a></div> <div class="comment-content"> <p> Enrico, thank you for writing. There's a lot to unpack in that quote, which was one of the reasons I didn't expand it. It would have made the article too long, and wandered off compared to its main point. I don't mind going into these details here, though. </p> <h3 id="09c47198877a4e0899efa958ccf94ba8"> Direction of data <a href="#09c47198877a4e0899efa958ccf94ba8" title="permalink">#</a> </h3> <p> In order to get the obvious out of the way first, the issue you point out is with my refactoring of indirect input to direct input. Refactoring from indirect to output to direct output is, as far as I can tell, not on your agenda. Designing with direct input in mind seems uncontroversial to me, so that makes sense. </p> <h3 id="dc3183b1cc9745c498978e8a7b3c7ac7"> No hard rules <a href="#dc3183b1cc9745c498978e8a7b3c7ac7" title="permalink">#</a> </h3> <p> On this blog, I often write articles as I figure out how to deal with problems. Sometimes, I report on my discoveries at a time where I've yet to accumulate years of experience. What I've learned so far is that dependency injection isn't functional. What I'm <em>still</em> exploring is what to do instead. </p> <p> It's my experience that the type of refactoring I demonstrate here can surprisingly often be performed. I don't want to claim that it's always possible to do it like this. In fact, I'm still looking for good examples where this will not be possible. Whenever I think of a simple enough example that I could share it here, I always realise that if only I simplify the problem, I can put it into the shape seen here. </p> <p> My thinking is, however, constrained by my professional experience. I've been doing web (service) development for so many years now that it constraints my imagination. When you execution scope is exclusively a single HTTP request at a time, you tend to keep things simple. I'd welcome a simplified, but still concrete example where the impure/pure/impure sandwich described here isn't going to be possible. </p> <p> This may seem like a digression, but my point is that I don't claim to be the holder of a single, undeniable truth. Still, I find that this article describes a broadly applicable design and implementation technique. </p> <h3 id="206fd472730d42328519b46cd0b8856a"> Language specifics <a href="#206fd472730d42328519b46cd0b8856a" title="permalink">#</a> </h3> <p> The next topic we need to consider is our choice of language. When I wrote that deferred execution can always be done by lazy evaluation, that's exactly how Haskell works. Haskell is lazily evaluated, so any value passed as direct input can be unevaluated until required. That goes for <code>IO</code> as well, but then, as we've learned, you can't pass impure data to a pure function. </p> <p> All execution is, in that sense, deferred, unless explicitly forced. Thus, any potential need for deferred execution has no design implications. </p> <p> F#, on the other hand, is an eagerly evaluated language, so there, deferred execution may have design implications. </p> <h3 id="e9aa174b04c14fb9850f2acb03e14f0b"> Performance <a href="#e9aa174b04c14fb9850f2acb03e14f0b" title="permalink">#</a> </h3> <p> Perhaps it's my lack of imagination again, but I can't think of a well-designed system where deferred execution is required for purposes of correctness. As far as I can tell, deferred execution is a performance concern. You wish to defer execution of a query because that operation takes significant time. </p> <p> That's a real concern, but I often find that people worry too much about performance. Again, this is probably my lack of wider experience, as I realise that performance can be important in smart phone apps, games, and the like. Clearly, performance is also important in the world of REST APIs, but I've met a lot of people who worry about performance without ever measuring it. </p> <p> When you start measuring performance, you'll often be surprised to discover where your code spends actual time. So my design approach is always to prioritise making the system work first, and then, if there are performance problems, figure out how to tweak it so that it becomes satisfactory. In my experience, such tweaking is only necessary now and then. I'm not claiming that my code is the fastest it could be, but it's often fast enough, and as easy to maintain as I can make it. </p> <h3 id="8adfc672b35b413eabfc1fdd97550322"> The need for data <a href="#8adfc672b35b413eabfc1fdd97550322" title="permalink">#</a> </h3> <p> Another concern is the need for data. If you consider the above <code>tryAccept</code> function, it <em>always</em> uses <code>reservations</code>. Thus, there's no gain in deferring the database query, because you'll always need the data. </p> <p> Deferred execution is only required in those cases where you have conditional branching, and only in certain cases do you need to read a particular piece of data. </p> <p> Even conditional branching isn't enough of a criterion, though, because you could have branching where, in 99.9 % of the cases, you'd be performing the query anyway. Would you, then, need deferred execution for the remaining 0.1 % of the cases? </p> <h3 id="6892843a12bc4079ad962a5e952dfe87"> Lazy sequences <a href="#6892843a12bc4079ad962a5e952dfe87" title="permalink">#</a> </h3> <p> Still, let's assume that we've implemented a system using pure functions that take pure data, but to our dismay we discover that there's one query that takes time to execute, and that we truly only need it some of the time. In .NET, there are two distinct situations: <ul> <li>We need a scalar value</li> <li>We need a collection of values</li> </ul> If we need a collection of values, we only need to make a minuscule change to the design of our function. Instead of taking an F# list, or an array, as direct input, we can make the function take a sequence (<code>IEnumerable&lt;T&gt;</code> in C#) as input. These can be implemented as lazily evaluated sequences, which gives us the deferred execution we need. </p> <h3 id="27d702bf2900420ab636149be0140216"> Lazy scalar values <a href="#27d702bf2900420ab636149be0140216" title="permalink">#</a> </h3> <p> This leaves the corner case where we need a lazily evaluated scalar value. In such cases, I may have to make a concession to performance in my function design, but I wouldn't change the argument to a function, but rather to a <a href="https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/lazy-computations">lazy</a> value. </p> <p> Lazy values are deferred, but memoised, which is the reason I'd prefer them over function arguments. </p> </div> <div class="comment-date">2017-02-18 19:54 UTC</div> </div> <div class="comment" id="ade3787e6e3c4e569854e2c2bd038e29"> <div class="comment-author"><a href="https://www.relativisticramblings.com/">Christer van der Meeren</a> <a href="#ade3787e6e3c4e569854e2c2bd038e29">#</a></div> <div class="comment-content"> <p> In a previous comment, you said: </p> <p> <blockquote> I'd welcome a simplified, but still concrete example where the impure/pure/impure sandwich described here isn't going to be possible. </blockquote> </p> <p> I have on two occasions stumbled into cases where I can't find a good way to pull this off. The reason may be that there's a workflow seemingly consisting of several impure steps interleaved with pure decisions. The cases are similar, so I will share one of them as an example. </p> <p> We have an API for allowing users to register. We also have a separate API for two-factor authentication (2FA). When users register, they have to complete a "proof" using the 2FA API to verify ownership of their mobile number. </p> <p> The 2FA API has two relevant endpoints used internally by our other APIs: One is used for creating a proof, and returns a proof ID that can be passed on to the API client. This endpoint is used when a client makes a request without a proof ID, or with an invalid proof ID. The other endpoint is used for verifying that a proof has been completed. This endpoint is used when a client supplies a proof ID in the request. </p> <p> (The 2FA API also has endpoints the client uses to complete the proof, but that is not relevant here.) </p> <p> When a user registers, this is the workflow: </p> <p> <img src="/content/binary/complete-registration-workflow-with-2fa-difficult-to-sandwich.png" alt="A flowchart describing the workflow for completing a registration."> </p> <p> Here is a simple implementation of the workflow using "dependency injection" (I will skip the actual composition function, similar to your <code>tryAcceptComposition</code>, which is not interesting here): </p> <pre>let completeRegistrationWorkflow (createProof: Mobile -&gt; Async&lt;ProofId&gt;) (verifyProof: Mobile -&gt; ProofId -&gt; Async&lt;bool&gt;) (completeRegistration: Registration -&gt; Async&lt;unit&gt;) (proofId: ProofId option) (registration: Registration) : Async&lt;CompleteRegistrationResult&gt; = async { match proofId with | None -&gt; let! proofId = createProof registration.Mobile return ProofRequired proofId | Some proofId -&gt; let! isValid = verifyProof registration.Mobile proofId if isValid then do! completeRegistration registration return RegistrationCompleted else let! proofId = createProof registration.Mobile return ProofRequired proofId }</pre> <p> There are two decisions that are pure and could conceivably be extracted to a pure (non-DI) function, indicated by blue in the flowchart: </p> <ul> <li> Initially: Should we create a new proof or verify an existing proof? (based on whether the client supplied a proof ID) </li> <li> After verifying a supplied proof: Should we complete the registration or create a new proof? (based on whether the supplied proof ID was valid) </li> </ul> <p> My question then, is this: Is it possible to refactor this to direct input/output, in a way that actually reduces complexity where it matters? (In other words, in a way where the decisions mentioned above are part of a pure, non-DI function?) </p> <p> Otherwise, I just want to say that this series has helped me become a better F# programmer. Since originally reading it, I have consistently tried to design for direct input/output as opposed to using "dependency injection", and my code has become better for it. Thanks! </p> </div> <div class="comment-date">2019-11-06 10:06 UTC</div> </div> <div class="comment" id="9510e21f27e74685858f5b8d538a79a3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9510e21f27e74685858f5b8d538a79a3">#</a></div> <div class="comment-content"> <p> Christer, thank you for writing. That was such a fruitful question that I wrote <a href="/2019/12/02/refactoring-registration-flow-to-functional-architecture">a new article</a> in order to answer it. I hope you find it useful, but if not, let's continue the discussion there or here, dependening on what makes most sense. </p> </div> <div class="comment-date">2019-12-02 13:22 UTC</div> </div> <div class="comment" id="0f1a8f0eaac9ea0812a91c6db65e3e37"> <div class="comment-author"><a href="https://twitter.com/danilom_">Danilo S. Morães</a> <a href="#0f1a8f0eaac9ea0812a91c6db65e3e37">#</a></div> <div class="comment-content"> <p> Hi Mark, excelent post series, thank you. Right after finishing this post I had the same questions Dustin Moris Gorski had and I think I still have some, even after your answer. </p> <p> For simplicity's sake, I'll represent TryAccept as 3 operations: read, calculate and maybe create. Both read and create are injected so that I can change their implementations at runtime, if necessary. That is the same exact representation for both the C# and F# codes that use DI, and also the tryAcceptComposition code, with the difference that tryAcceptComposition depends on the actual implementation of DB, so it's relatively more brittle than the DI alternatives. </p> <p> Although TryAccept and TryAcceptComposition are doing the same thing, I'm still trying to think why the latter looks more functional than the first and whether DI is really not necessary. I got to 2 conclusions and I wanted to know your opinion about them: </p> <p> 1) The difference between the 2 implementations (TryAccept and TryAcceptComposition) is that the first is following an imperative style, while the second is purely declaritive, with one big composition. Both implementations perform the exact same operations, evoking the same dependencies, but with different code styles. </p> <p> 2) If we try and take your style of sandwich to extremes and push dependencies to the very edges of the application, to the point where it's just a simple big composition with dependencies at the beginning and end, we my replace "injection" with import statements, importing our dependencies in the same place the composition is written. I don't think this would work in every scenario as operations in the middle of this composition would need access to dependencies too (which could them be pushed out like TryAccept, but could make the code less readable). Do you think this is doable? </p> </div> <div class="comment-date">2020-01-14 20:37 UTC</div> </div> <div class="comment" id="5bfb598be4d34755b4c2272dc1814df4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5bfb598be4d34755b4c2272dc1814df4">#</a></div> <div class="comment-content"> <p> Danilo, thank you for writing. Regarding your first point, I'm not sure I follow. Both functions are written in F#, which is an expression-based language (at least when you ignore the interop parts). Imperative code is usually defined as coding with <em>assignments</em> rather than <em>expressions</em>. There's no assignments here. </p> <p> Also, <code>tryAccept</code> and <code>tryAcceptComposition</code> aren't equivalent. One is a specialisation of the other, so to speak. You can't change the behaviour of <code>tryAcceptComposition</code> unless you edit the actual code. You can, on the other hand, change the observable behaviour of <code>tryAccept</code> by composing it with different impure actions. </p> <p> As to your second observation: <blockquote> "I don't think this would work in every scenario as operations in the middle of this composition would need access to dependencies too" </blockquote> I'm still keen on getting some compelling examples of this. Christer van der Meeren kindly <a href="#ade3787e6e3c4e569854e2c2bd038e29">tried to supply such an example</a>, but it <a href="/2019/12/02/refactoring-registration-flow-to-functional-architecture">turned out as another fine sandwich</a>. This happens conspicuously often. </p> <p> I've never claimed that the sandwich architecture is always possible, but every time I try to find a compelling counter-example, it usually turns out to be refactorable into a sandwich after all. Do, however, refer to the comments to <a href="/2019/12/02/refactoring-registration-flow-to-functional-architecture">that later article</a>. </p> </div> <div class="comment-date">2020-01-19 21:07 UTC</div> </div> <div class="comment" id="ac27689e0b794aee9c611dd6bd7f31f1"> <div class="comment-author"><a href="https://github.com/ledbutter">Sven Grosen</a> <a href="#ac27689e0b794aee9c611dd6bd7f31f1">#</a></div> <div class="comment-content"> <p> Hi Mark, I found myself thinking about this early this morning and wondering about scenarios where injecting dependencies may still be "functional" (i.e. pure) and would like your input. It also incorporates TDD (or at least testing strategies). </p> <p> Here's my example: I was recently refactoring some code that was structured like this (C#-ish pseudo-code): <pre> if (shouldNotify(record, supportingRecord, settings)) { generateEmail(...); record.status = calculateStatusAfterEmail(record, supportingRecord); } else { record.status = calculateStatusWithoutEmail(record, supportingRecord); } </pre> Let's gloss over the side-effects all over the place here and just focus on the flow. </p> <p> To ease unit testing, I extracted these private methods into a separate interface and am injecting it into this flow. Call me lazy, but I wanted to just mock/stub the results of these pure calls when testing the overall flow and wrote separate tests on the implementation of the new interface that could focus on all the different combinations of values. Is that a strategy you would support/recommend, or would you hesitate to extract pure functions from an implementation just to ease testing? I also convinced myself that I could justify this on the grounds of following the Single Responsibility Principle (i.e. this flow should not need to be modified if the status calculation logic changes). </p> <p> Your thoughts? By the way, your new dependency injection strategies is on its way to me, perhaps that book will provide me with your views on that, but I couldn't resist posting it here. </p> </div> <div class="comment-date">2020-05-28 14:30 UTC</div> </div> <div class="comment" id="d0185b75323c4028b04f7dc24e3f127f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d0185b75323c4028b04f7dc24e3f127f">#</a></div> <div class="comment-content"> <p> Sven, thank you for writing. I think that your question deserves a longer answer than what you'll get here. It's a topic that for some time I've been contemplating giving a more detailed treatment. It's a rich enough subject that a couple of articles would be required. I'm still pondering how to organise such content. </p> <p> The short answer is that I'm increasingly looking for alternatives to the sort of interaction-based testing you imply. I think that my <a href="/2019/02/18/from-interaction-based-to-state-based-testing">article series on the benefits of state-based testing</a> outlines most of my current thinking on the topic. </p> </div> <div class="comment-date">2020-06-03 5:46 UTC</div> </div> <div class="comment" id="a24bf916c0e941b1b932b39284ea1d53"> <div class="comment-author"><a href="https://github.com/chrischen">Chris Chen</a> <a href="#a24bf916c0e941b1b932b39284ea1d53">#</a></div> <div class="comment-content"> <p> I read over all the comments and I would like clarification on when logic is required for resolving the dependency being provided. Benjamin Hodgson asked this: </p> <blockquote>Namely, there's still some important business logic being carried out by your tryAcceptComposition function, like checking the capacity on the requested reservation date. How do you unit test that readReservations is called with the correct date?</blockquote> <p>Suppose the readRervations didn't just get <em>all</em> reservations from the dabatase just to process for one restaurant or on one day. Thus the restaurant ID and date may be needed to fetch the correct reservations, and this logic would have to happen outside of the unit. I think you touched on this complication in a response above, but I'd like to confirm if I am understanding your responses correctly: you simply perform this logic outside of the unit and have an integration test for the entire workflow?</p> </div> <div class="comment-date">2021-12-15 22:11 UTC</div> </div> <div class="comment" id="a0c775671d224b48b894293eeb05dff9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a0c775671d224b48b894293eeb05dff9">#</a></div> <div class="comment-content"> <p> Chris, thank you for writing. There are more nuances to that question than might be immediately apparent. At least, I'll answer the question in several ways. </p> <p> First, I'd like to challenge the implicit assumption that one must always test everything. <a href="/2018/11/12/what-to-test-and-not-to-test">What to test and what not to test</a> depend on multiple independent forces, such as how costly an error might be, how likely an error is to occur, and so on. </p> <p> Take, as an example, the code base that accompanies my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. This code base is also a restaurant reservation system, but has a realistic level of complexity, so I think it might better highlight the sort of considerations that might be warranted. The code that is most relevant to the question is this snippet from <code>ReservationsController.TryCreate</code>: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(restaurant.Id,&nbsp;reservation.At) &nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">now</span>&nbsp;=&nbsp;Clock.GetCurrentDateTime(); <span style="color:#8f08c4;">if</span>&nbsp;(!restaurant.MaitreD.WillAccept(now,&nbsp;reservations,&nbsp;reservation)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;NoTables500InternalServerError(); <span style="color:blue;">await</span>&nbsp;Repository.Create(restaurant.Id,&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>);</pre> </p> <p> How likely is it that the code changes in such a way that it calls <code>ReadReservations</code> with the <em>wrong</em> input? Either because the original programmer who wrote the code made a mistake, or because a later developer introduced a change in which this code calls <code>ReadReservations</code> with incorrect input? </p> <p> Now, such errors certainly do occur. Programmers are human, and to err is human. </p> <p> Still, it's important to keep in mind that the risk of introducing an error into a code base is proportional to the amount of code. The more code, the more errors you'll have. This goes for test code as well. You can also, inadvertently, introduce errors into a test. </p> <p> You can counteract some of that by writing the test first. That's the reasons it's so important to see a test fail. Only by seeing it fail can you feel confident that it verifies something real. </p> <p> So, depending on circumstances, it may be relevant and important to write a test that verifies that <code>ReadReservations</code> is called with the correct arguments. Still, I think it's important to weigh advantages and disadvantages before blindly insisting that it's necessary to protect against such a contingency. </p> <p> When I originally wrote the above code, I wrote no unit test that explicitly verify whether <code>ReadReservations</code> is called with the correct arguments. After all, in that context, there's only one <code>DateTime</code> value in scope: <code>reservation.At</code>. (This is not entirely true, because <code>now</code> is also a <code>DateTime</code> value, but as the code is written, it's not available for <code>ReadReservations</code> because it's retrieved <em>after</em> the <code>ReadReservations</code> call. But see below.) </p> <p> In a context like this, you'd have to go out of your way to call <code>ReadReservations</code> with the wrong date. You <em>could</em> create a value on the spot, like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(restaurant.Id,&nbsp;<span style="color:blue;">new</span>&nbsp;DateTime(2021,&nbsp;12,&nbsp;19,&nbsp;18,&nbsp;30,&nbsp;0)) &nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>);</pre> </p> <p> or this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(restaurant.Id,&nbsp;reservation.At.AddDays(1)) &nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>);</pre> </p> <p> None of these are <em>impossible</em>, but I do consider them to be <em>conspicuous</em>. You don't easily make those kinds of mistakes. You'd almost have to go out of your way to make these mistakes, and a good pairing partner or code review ought to catch such a mistake. </p> <p> Another option is if someone finds a good reason to reorder the code: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">now</span>&nbsp;=&nbsp;Clock.GetCurrentDateTime(); <span style="color:blue;">var</span>&nbsp;<span style="color:#1f377f;">reservations</span>&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;Repository &nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(restaurant.Id,&nbsp;now) &nbsp;&nbsp;&nbsp;&nbsp;.ConfigureAwait(<span style="color:blue;">false</span>);</pre> </p> <p> In this example, <code>now</code> is available to <code>ReadReservations</code>, and perhaps a later edit might introduce the error of calling it with <code>now</code>. </p> <p> This is perhaps a more realistic, honest mistake. </p> <p> None of the 171 unit tests in the code base catch any of the above three mistakes. </p> <p> Is the code base unsafe, then? </p> <p> No, because as luck would have it, an integration test (<code>NoOverbookingRace</code>) fails on any of these three edits. I admit that this is just a fortunate accident. I added that particular integration test for another reason: to reproduce an unrelated bug that occurred in 'production'. </p> <p> I usually try to base the amount of testing I ask from a team on the quality of the team. The more I trust that they pair-program or perform regular code reviews, the less I insist on test coverage, and vice versa. </p> <p> In summary, though, if I find that it's important to verify that <code>ReadReservations</code> was called with the correct arguments, I'd favour a <a href="/2019/02/18/from-interaction-based-to-state-based-testing">state-based integration test</a>. </p> </div> <div class="comment-date">2021-12-19 12:24 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Partial application is dependency injection https://blog.ploeh.dk/2017/01/30/partial-application-is-dependency-injection 2017-01-30T12:40:00+00:00 Mark Seemann <div id="post"> <p> <em>The equivalent of dependency injection in F# is partial function application, but it isn't functional.</em> </p> <p> This is the second article in a small article series called <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">from dependency injection to dependency rejection</a>. </p> <p> People often ask me how to do dependency injection in F#. That's only natural, since I wrote <a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a> some years ago, and also since I've increasingly focused my energy on F# and other functional programming languages. </p> <p> Over the years, I've seen other F# experts respond to that question, and often, the answer is that partial function application is the F# way to do dependency injection. For some years, I believed that as well. It turns out to be true in one sense, but incorrect in another. Partial application <em>is</em> equivalent to dependency injection. It's just not a functional solution to dealing with dependencies. </p> <p> (To be as clear as I can be: I'm not claiming that partial application isn't functional. What I claim is that partial application used for dependency injection isn't functional.) </p> <h3 id="95e560cdf3314192916a83a5e33b9dd5"> Attempted dependency injection using functions <a href="#95e560cdf3314192916a83a5e33b9dd5" title="permalink">#</a> </h3> <p> Returning to the example from the <a href="/2017/01/27/dependency-injection-is-passing-an-argument">previous article</a>, you could try to rewrite <code>MaîtreD.TryAccept</code> as a function: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;(DateTimeOffset&nbsp;-&gt;&nbsp;Reservation&nbsp;list)&nbsp;-&gt;&nbsp;(Reservation&nbsp;-&gt;&nbsp;int)&nbsp;-&gt;&nbsp;Reservation</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;capacity&nbsp;<span style="color:navy;">readReservations</span>&nbsp;<span style="color:navy;">createReservation</span>&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">readReservations</span>&nbsp;reservation.Date&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">sumBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">createReservation</span>&nbsp;{&nbsp;reservation&nbsp;<span style="color:blue;">with</span>&nbsp;IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}&nbsp;|&gt;&nbsp;<span style="color:navy;">Some</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> You could imagine that this <code>tryAccept</code> function is part of a module called <code>MaîtreD</code>, just to keep the examples as equivalent as possible. </p> <p> The function takes four arguments. The first is the capacity of the restaurant in question; a primitive integer. The next two arguments, <code>readReservations</code> and <code>createReservation</code> fill the role of the injected <code>IReservationsRepository</code> in the previous article. In the object-oriented example, the <code>TryAccept</code> method used two methods on the repository: <code>ReadReservations</code> and <code>Create</code>. Instead of using an interface, in the F# function, I make the function take two independent functions. They have (almost) the same types as their C# counterparts. </p> <p> The first three arguments correspond to the injected dependencies in the previous <code>MaîtreD</code> class. The fourth argument is a <code>Reservation</code> value, which corresponds to the input to the previous <code>TryAccept</code> method. </p> <p> Instead of returning a nullable integer, this F# version returns an <code>int option</code>. </p> <p> The implementation is also equivalent to the C# example: Read the relevant reservations from the database using the <code>readReservations</code> function argument, and sum over their quantities. Based on the number of already reserved seats, decide whether or not to accept the reservation. If you can accept the reservation, set <code>IsAccepted</code> to <code>true</code>, call the <code>createReservation</code> function argument, and pipe the returned ID (integer) to <code>Some</code>. If you can't accept the reservation, then return <code>None</code>. </p> <p> Notice that the first three arguments are 'dependencies', whereas the last argument is the 'actual input', if you will. This means that you can use partial function application to compose this function. </p> <h3 id="906f6599196b4fb08401dbc3c8c70819"> Application <a href="#906f6599196b4fb08401dbc3c8c70819" title="permalink">#</a> </h3> <p> If you recall the definition of the previous <code>IMaîtreD</code> interface, the <code>TryAccept</code> method was defined like this (C# code snippet): </p> <p> <pre><span style="color:blue;">int</span>?&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation);</pre> </p> <p> You could attempt to define a similar function with the type <code>Reservation -&gt; int option</code>. Normally, you'd want to do this closer to the boundary of the application, but the following example demonstrates how to 'inject' real database operations into the function. </p> <p> Imagine that you have a <code>DB</code> module with these functions: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:teal;">DB</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;Reservation&nbsp;list</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">readReservations</span>&nbsp;<span style="color:#9b9b9b;">connectionString</span>&nbsp;<span style="color:#9b9b9b;">date</span>&nbsp;=&nbsp;<span style="color:green;">//&nbsp;..</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;int</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">createReservation</span>&nbsp;<span style="color:#9b9b9b;">connectionString</span>&nbsp;<span style="color:#9b9b9b;">reservation</span>&nbsp;=&nbsp;<span style="color:green;">//&nbsp;..</span></pre> </p> <p> The <code>readReservations</code> function takes a connection string and a date as arguments, and returns a list of reservations for that date. The <code>createReservation</code> function also takes a connection string, as well as a reservation. When invoked, it creates a new record for the reservation and returns the ID of the newly created row. (This sort of API violates CQS, so you should <a href="/2016/05/06/cqs-and-server-generated-entity-ids">consider alternatives</a>.) </p> <p> If you partially apply these functions with a valid connection string, both have the type desired for their roles in <code>tryAccept</code>. This means that you can create a function from these elements: </p> <p> <pre><span style="color:green;">//&nbsp;Reservation&nbsp;-&gt;&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryAcceptComposition</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">read</span>&nbsp;&nbsp;&nbsp;=&nbsp;<span style="color:teal;">DB</span>.<span style="color:navy;">readReservations</span>&nbsp;&nbsp;connectionString &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">create</span>&nbsp;=&nbsp;<span style="color:teal;">DB</span>.<span style="color:navy;">createReservation</span>&nbsp;connectionString &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">tryAccept</span>&nbsp;10&nbsp;<span style="color:navy;">read</span>&nbsp;<span style="color:navy;">create</span></pre> </p> <p> Notice how <code>tryAccept</code> itself is partially applied. Only the arguments corresponding to the C# dependencies are passed to it, so the return value is a function that 'waits' for the last argument: the reservation. As I've attempted to indicate by the code comment above the function, it has the desired type of <code>Reservation -&gt; int option</code>. </p> <h3 id="8f2ef3a813184fbabd9af61a62227623"> Equivalence <a href="#8f2ef3a813184fbabd9af61a62227623" title="permalink">#</a> </h3> <p> Partial application used like this is equivalent to dependency injection. To see how, consider the generated Intermediate Language (IL). </p> <p> F# is a .NET language, so it compiles to IL. You can decompile that IL to C# to get a sense of what's going on. If you do that with the above <code>tryAcceptComposition</code>, you get something like this: </p> <p> <pre><span style="color:blue;">internal</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">tryAcceptComposition@17</span>&nbsp;:&nbsp;<span style="color:#2b91af;">FSharpFunc</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">FSharpOption</span>&lt;<span style="color:blue;">int</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FSharpFunc</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;createReservation; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">FSharpFunc</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">FSharpList</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&nbsp;readReservations; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;tryAcceptComposition@17( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;capacity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">FSharpFunc</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">FSharpList</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&nbsp;readReservations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">FSharpFunc</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;createReservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.readReservations&nbsp;=&nbsp;readReservations; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.createReservation&nbsp;=&nbsp;createReservation; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:#2b91af;">FSharpOption</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Invoke(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>.tryAccept&lt;<span style="color:blue;">int</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.capacity,&nbsp;<span style="color:blue;">this</span>.readReservations,&nbsp;<span style="color:blue;">this</span>.createReservation,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> I've cleaned it up a bit, mostly by removing all attributes from the various elements. Notice how this is a class, with class fields, and a constructor that takes values for the fields and assigns them. <em>It's constructor injection!</em> </p> <p> Partial application is dependency injection. </p> <p> It compiles, works as expected, but is it functional? </p> <h3 id="4caca165fd224fbca6c0acdec910f846"> Evaluation <a href="#4caca165fd224fbca6c0acdec910f846" title="permalink">#</a> </h3> <p> People sometimes ask me: <em>How do I know whether my F# code is functional?</em> </p> <p> I sometimes wonder about that myself, but unfortunately, as nice a language as F# is, it doesn't offer much help in that regard. Its emphasis is on functional programming, but it allows mutation, object-oriented programming, and even procedural programming. It's a friendly and forgiving language. (This also makes it a great 'beginner' functional language, because you can learn functional concepts piecemeal.) </p> <p> Haskell, on the other hand, is a strictly functional language. In Haskell, you can only write your code in the functional way. </p> <p> Fortunately, F# and Haskell are similar enough that it's easy to port F# code to Haskell, as long as the F# code already is 'sufficiently functional'. In order to evaluate if my F# code is properly functional, I sometimes port it to Haskell. If I can get it to compile and run in Haskell, I take that as confirmation that my code is functional. </p> <p> I've <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">previously shown an example similar to this one</a>, but I'll repeat the experiment here. Will porting <code>tryAccept</code> and <code>tryAcceptComposition</code> to Haskell work? </p> <p> It's easy to port <code>tryAccept</code>: </p> <p> <pre><span style="color:#600277;">tryAccept</span>&nbsp;::&nbsp;Int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(<span style="color:blue;">ZonedTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[<span style="color:blue;">Reservation</span>])&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Int)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Maybe</span>&nbsp;<span style="color:#dd0000;">Int</span> tryAccept&nbsp;capacity&nbsp;readReservations&nbsp;createReservation&nbsp;reservation&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;<span style="color:#666666;">=</span>&nbsp;sum&nbsp;<span style="color:#666666;">$</span>&nbsp;map&nbsp;quantity&nbsp;<span style="color:#666666;">$</span>&nbsp;readReservations&nbsp;<span style="color:#666666;">$</span>&nbsp;date&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;reservedSeats&nbsp;<span style="color:#666666;">+</span>&nbsp;quantity&nbsp;reservation&nbsp;<span style="color:#666666;">&lt;=</span>&nbsp;capacity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;Just&nbsp;<span style="color:#666666;">$</span>&nbsp;createReservation&nbsp;<span style="color:#666666;">$</span>&nbsp;reservation&nbsp;{&nbsp;isAccepted&nbsp;<span style="color:#666666;">=</span>&nbsp;True&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;Nothing</pre> </p> <p> Clearly, there are differences, but I'm sure that you can also see the similarities. The most important feature of this function is that it's <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>. All Haskell functions are pure by default, unless explicitly declared to be impure, and that's not the case here. This function is pure, and so are both <code>readReservations</code> and <code>createReservation</code>. </p> <p> The Haskell version of <code>tryAccept</code> compiles, but what about <code>tryAcceptComposition</code>? </p> <p> Like the F# code, the experiment is to see if it's possible to 'inject' functions that actually operate against a database. Equivalent to the F# example, imagine that you have this <code>DB</code> module: </p> <p> <pre><span style="color:#600277;">readReservations</span>&nbsp;::&nbsp;<span style="color:blue;">ConnectionString</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">ZonedTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;[<span style="color:blue;">Reservation</span>] readReservations&nbsp;connectionString&nbsp;date&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:green;">--&nbsp;.. </span> <span style="color:#600277;">createReservation</span>&nbsp;::&nbsp;<span style="color:blue;">ConnectionString</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;Int createReservation&nbsp;connectionString&nbsp;reservation&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:green;">--&nbsp;..</span></pre> </p> <p> Database operations are, by definition, impure, and Haskell admirably models that with the type system. Notice how both functions return <code>IO</code> values. </p> <p> If you partially apply both functions with a valid connection string, the <code>IO</code> context remains. The type of <code>DB.readReservations connectionString</code> is <code>ZonedTime -&gt; IO [Reservation]</code>, and the type of <code>DB.createReservation connectionString</code> is <code>Reservation -&gt; IO Int</code>. You can try to pass them to <code>tryAccept</code>, but the types don't match: </p> <p> <pre><span style="color:#600277;">tryAcceptComposition</span>&nbsp;::&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;IO&nbsp;(Maybe&nbsp;Int) tryAcceptComposition&nbsp;reservation&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;read&nbsp;&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">DB</span><span style="color:#666666;">.</span>readReservations&nbsp;&nbsp;connectionString &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">DB</span><span style="color:#666666;">.</span>createReservation&nbsp;connectionString &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;tryAccept&nbsp;<span style="color:#09885a;">10</span>&nbsp;read&nbsp;create&nbsp;reservation</pre> </p> <p> <em>This doesn't compile.</em> </p> <p> It doesn't compile, because the database operations are impure, and <code>tryAccept</code> wants pure functions. </p> <p> In short, partial application used for dependency injection isn't functional. </p> <h3 id="3d7fab0e638942e584b3bd1bde81e59d"> Summary <a href="#3d7fab0e638942e584b3bd1bde81e59d" title="permalink">#</a> </h3> <p> Partial application in F# can be used to achieve a result equivalent to dependency injection. It compiles and works as expected, but it's not functional. The reason it's not functional is that (most) dependencies are, by their very nature, impure. They're either non-deterministic, have side-effects, or both, and that's often the underlying reason that they are factored into dependencies in the first place. </p> <p> Pure functions, however, can't call impure functions. If they could, they would become impure themselves. This rule is enforced by Haskell, but not by F#. </p> <p> When you inject impure operations into an F# function, that function becomes impure as well. Dependency injection makes everything impure, which explains why it isn't functional. </p> <p> Functional programming solves the problem of decoupling (side) effects from program logic another way. That's the topic of the next article. </p> <p> <strong>Next:</strong> <a href="/2017/02/02/dependency-rejection">Dependency rejection</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="c9dc91eef46747ea9729e7cbfb356a32"> <div class="comment-author">Kurren Nischal <a href="#c9dc91eef46747ea9729e7cbfb356a32">#</a></div> <div class="comment-content"> A very useful read as always.<br> A couple of questions: If you're porting your code from F# to Haskell and back into F#, why not just use Haskell in the first place? <br> Also, why would you want to mark a function as having side effects in the first place? <br> Thanks </div> <div class="comment-date">2017-07-17 14:10 UTC</div> </div> <div class="comment" id="96c4fa37e1d14aa9a8d07a8117654058"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#96c4fa37e1d14aa9a8d07a8117654058">#</a></div> <div class="comment-content"> <p> Kurren, thank you for writing. Why not use Haskell in the first place? In some situations, if that's an option, I'd go for it. In most of my professional work, however, it's not. A bit of background is required, I think. I've spent most of my professional career working with Microsoft technologies. Most of my clients use .NET. The majority of my clients use C#, but some are interested in adopting functional programming. F# is a great bridge to functional programming, because it integrates so well with existing C# code. </p> <p> Perhaps most importantly is that while these organisations learn F# as a new programming language, and a new paradigm, all other dimensions of software development remain the same. You can use familiar libraries and frameworks, you can use the same Continuous Integration and build tools as you've been used to, you can deploy like you always do, and you can operate and monitor your software like before. </p> <p> If you start using Haskell, not only will developers have to learn an entire new ecosystem, but if you have a separate operations team, they would have be on board with that as well. </p> <p> If you can't get the entire organisation to accept Haskell-based software, then F# is a great choice for a .NET shop. Developers will obviously know the difference, but the rest of the organisation can be oblivious to the choice of language. </p> <p> When it comes to side-effects, that's one of the main motivations behind pure code in the first place. A major source of bugs is that often, programs have unintended side-effects. It's easy to introduce defects in a code base when side-effects are uncontrolled. That's the case for languages like C# and Java, and, unfortunately, F# as well. When you look at a method or function, there's no easy way to determine whether it has side-effects. In other words: all methods or functions could have side effects. (Also: all methods could return null.) </p> <p> This slows you down when you work on an existing code base, because there's only one way to determine if side-effects or non-deterministic behaviour are part of the code you're about to call: you have to <em>read it all</em>. </p> <p> On the other hand, if you can tell, just by looking at a function's type, whether or not it has side-effects, you can save yourself a lot of time. By definition, all the pure functions have no side-effect. You don't need to read the code in order to detect that. </p> <p> In Haskell, this is guaranteed. In F#, you can design your system that way, but some discipline is required in order to uphold such a guarantee. </p> </div> <div class="comment-date">2017-07-18 09:31 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency injection is passing an argument https://blog.ploeh.dk/2017/01/27/dependency-injection-is-passing-an-argument 2017-01-27T09:27:00+00:00 Mark Seemann <div id="post"> <p> <em>Is dependency injection really just passing an argument? A brief review.</em> </p> <p> This is the first article in a small article series called <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">from dependency injection to dependency rejection</a>. </p> <p> In a talk at the 2012 Northeast Scala Symposium, <a href="https://t.co/oD1oQYZwbz">Rúnar Bjarnason casually remarked</a> that dependency injection is "really just a pretentious way to say 'taking an argument'". Given that I've written a 500+ pages <a href="http://amzn.to/12p90MG">book about dependency injection</a>, you might expect me to disagree with that. Yet, there's some truth to that statement, although it's not quite as simple as that. </p> <p> In this article, I'll show you some simple examples and explain why, on the one hand, Rúnar Bjarnason is right, but also, on the other hand, why there's a bit more to it. </p> <h3 id="8a528069afb34c979d862fe09147623b"> Restaurant reservation example <a href="#8a528069afb34c979d862fe09147623b" title="permalink">#</a> </h3> <p> Like the other articles in this series, the example scenario is on-line restaurant reservation. Imagine that you've been asked to develop an HTTP-based API that accepts JSON documents containing restaurant reservations. Furthermore, assume that you're using ASP.NET Web API with C# for the job, and that you're aspiring to use <a href="http://amzn.to/WBCwx7">domain-driven design</a>. </p> <p> In order to handle the incoming POST request, you could write an action method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:#2b91af;">ReservationRequestDto</span>&nbsp;dto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;validationMsg&nbsp;=&nbsp;validator.Validate(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(validationMsg&nbsp;!=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.BadRequest(validationMsg); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;r&nbsp;=&nbsp;mapper.Map(dto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;id&nbsp;=&nbsp;maîtreD.TryAccept(r); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.StatusCode(<span style="color:#2b91af;">HttpStatusCode</span>.Forbidden); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Ok(); }</pre> </p> <p> This method follows a simple and familiar path: validate input, map to a domain model, delegate to said model, examine posterior state, and return a result. </p> <p> You may have noticed, though, that this method doesn't do all the work itself. It delegates some of the work to collaborators: <code>validator</code>, <code>mapper</code>, and <code>maîtreD</code>. Where do these collaborators come from? </p> <p> They are <em>dependencies</em>. Could you make the <code>Post</code> method take them as arguments? </p> <p> Unfortunately, you can't. The <code>Post</code> method constitutes part of the boundary of the HTTP API. ASP NET Web API routes and dispatches incoming HTTP requests by convention, and <em>action methods</em> must follow that convention. You can't just make the function take any argument you'd like, so you have to find another place to pass those dependencies to the object. </p> <p> The second-best option (after the <code>Post</code> method itself) is via the constructor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IValidator</span>&nbsp;validator, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMapper</span>&nbsp;mapper, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMaîtreD</span>&nbsp;maîtreD) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.validator&nbsp;=&nbsp;validator; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.mapper&nbsp;=&nbsp;mapper; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.maîtreD&nbsp;=&nbsp;maîtreD; }</pre> </p> <p> This is the application of a design pattern called <em>constructor injection</em>. It captures the dependencies in class fields, making them available for members (like <code>Post</code>) of the class. </p> <p> This turns out to be a regular pattern. </p> <h3 id="422dad6aa02e4f3aaf20f495c7ed1f17"> Turtles all the way down <a href="#422dad6aa02e4f3aaf20f495c7ed1f17" title="permalink">#</a> </h3> <p> You could argue that the <code>Post</code> method is a special case, since it's part of the boundary of the system, and therefore must adhere to specific rules. On the other hand, these rule don't apply deeper in the implementation, so could you implement other objects by simply passing in dependencies as arguments? </p> <p> Consider, as an example, the implementation of <code>IMaîtreD.TryAccept</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>?&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservationsRepository &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ReadReservations(reservation.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(reservedSeats&nbsp;+&nbsp;reservation.Quantity&nbsp;&lt;=&nbsp;capacity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reservationsRepository.Create(reservation); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; }</pre> </p> <p> This method has another collaborator: <code>reservationsRepository</code>. It's another dependency. Where does it come from? </p> <p> Could you make the <code>TryAccept</code> method take <code>reservationsRepository</code> as an argument? </p> <p> Unfortunately, that's not possible either, because the method is defined by the <code>IMaîtreD</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>?&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> You may recall that the above <code>Post</code> method is programmed against the <code>IMaîtreD</code> interface, and not the concrete class. It'd be a leaky abstraction to add <code>IReservationsRepository</code> as an argument to <code>IMaîtreD.TryAccept</code>, because not all implementations of the interface may need that dependency. Or perhaps another implementation has another dependency. Should we add that to the parameter list of <code>IMaîtreD.TryAccept</code>as well? </p> <p> Surely, that's not a tenable design principle. On the other hand, by using constructor injection, you can decouple implementation details from your abstractions: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;MaîtreD(<span style="color:blue;">int</span>&nbsp;capacity,&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;reservationsRepository) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.reservationsRepository&nbsp;=&nbsp;reservationsRepository; }</pre> </p> <p> This constructor not only takes an <code>IReservationsRepository</code> object, but also an integer that represents the capacity of the restaurant in question. This demonstrates that <a href="/2012/07/02/PrimitiveDependencies">dependencies can also be primitive values</a>. </p> <h3 id="8ccdc1fe4c464d9da6dce10d08755b16"> Summary <a href="#8ccdc1fe4c464d9da6dce10d08755b16" title="permalink">#</a> </h3> <p> Dependency injection is, in a sense, only a specific way for objects to take arguments. Often, however, objects have roles defined by the interfaces they implement. Such objects may need collaborators that are not available via the APIs defined by these interfaces, so you'll have to supply dependencies via members that belong to the concrete class in question. Passing dependencies via a class' constructor is the best way to do that. </p> <p> <strong>Next:</strong> <a href="/2017/01/30/partial-application-is-dependency-injection">Partial application is dependency injection</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="47b89e9dd39641e993c7b9a101763f19"> <div class="comment-author"><a href="https://github.com/julealgon">Juliano Leal Goncalves</a> <a href="#47b89e9dd39641e993c7b9a101763f19">#</a></div> <div class="comment-content"> <p> Hey Mark. You may want to update the article slightly to cover the use of the <code>[FromServices]</code> attribute, which allows you to inject any service into a controller action as method-level injection. The main article on it <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/dependency-injection?view=aspnetcore-5.0#action-injection-with-fromservices">is this one</a>. </p> <p> I'm personally not a fan of using it, but it is an option that transforms standard injection into parameter-passing for controllers. </p> </div> <div class="comment-date">2020-02-23 15:11 UTC</div> </div> <div class="comment" id="289838d84c45437ba3d401164f572e37"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#289838d84c45437ba3d401164f572e37">#</a></div> <div class="comment-content"> <p> Juliano, thank you for writing. I wasn't aware of that particular capability, so thank you for bringing it to my attention. </p> <p> My article attempts to explain a general software design problem, as well as a possible solution. While it uses ASP.NET as an example context, the main point is independent of any particular framework, or language, for that matter. </p> </div> <div class="comment-date">2021-02-24 14:09 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. From dependency injection to dependency rejection https://blog.ploeh.dk/2017/01/27/from-dependency-injection-to-dependency-rejection 2017-01-27T07:55:00+00:00 Mark Seemann <div id="post"> <p> <em>The problem typically solved by dependency injection in object-oriented programming is solved in a completely different way in functional programming.</em> </p> <p> Several years ago, I wrote a book called <a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a>, which was published in 2011. The book contains examples in C#, but since then I've increasingly become interested in functional programming to the extend that I now consider F# my primary language. </p> <p> With that combination, it's no wonder that people often ask me how to do dependency injection in functional programming. </p> <p> I've seen more than one answer, from other people, explaining how partial function application is equivalent to dependency injection. In a small series of articles, I'll explain both why this is true, but also why it's <em>not functional</em>. I'll conclude by showing a functional alternative to decoupling logic and (side) effects. </p> <p> <img src="/content/binary/dependency-injection-in-scala-comic.jpg" alt="Bob: How do I do dependency injection in Scala? Other man: You don't, because Scala is a functional language. Bob: Fine, it's functional. How do I inject dependencies? Other man: You use a free monad which allows you to build a monad from any Functor. Bob: Did you just tell me to go fuck myself? Other man: I believe I did, Bob."> </p> <p> (<a href="https://twitter.com/hmemcpy/status/771359835514368000">Comic</a> courtesy of <a href="http://thinkdifferent.ly">John Muellerleile</a> and <a href="http://hmemcpy.com">Igal Tabachnik</a>.) </p> <p> There's another school of functional programmers who believe that dependency injection in functional programming involves a Free monad. </p> <p> You can often make do with less, though. </p> <p> In my experience, it's usually enough to refactor a unit to take only direct input and output, and then compose an <a href="/2020/03/02/impureim-sandwich">impure/pure/impure 'sandwich'</a>. You'll see an example later. </p> <p> This article series contains the following parts: <ol> <li><a href="/2017/01/27/dependency-injection-is-passing-an-argument">Dependency injection is passing an argument</a></li> <li><a href="/2017/01/30/partial-application-is-dependency-injection">Partial application is dependency injection</a></li> <li><a href="/2017/02/02/dependency-rejection">Dependency rejection</a></li> <li><a href="/2017/07/10/pure-interactions">Pure interactions</a></li> </ol> The first three articles revolve around a common example, which is one of my favourite scenarios: on-line restaurant reservation. You can see an actual example client in my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">Functional Architecture with F#</a> Pluralsight course. The (somewhat dated) client source code is <a href="https://github.com/ploeh/booking-web-ui">available on GitHub</a>. The server-side F# and Haskell example code for this article series is <a href="https://github.com/ploeh/dependency-rejection-samples">available on GitHub</a>. </p> <p> The scenario is to implement an HTTP-based API that can accept incoming JSON documents that represent restaurant reservations. </p> <p> The fourth article on <em>pure interactions</em> is a gateway to another article series on free monads. </p> <p> I should point out that nowhere in this article series do I reject dependency injection as a set of object-oriented patterns. In object-oriented programming, dependency injection is a well-known and comprehensively described way to achieve decoupling and testability. In the next article, you'll see a brief review of dependency injection in C#. </p> <p> <strong>Next:</strong> <a href="/2017/01/27/dependency-injection-is-passing-an-argument">Dependency injection is passing an argument</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Decoupling application errors from domain models https://blog.ploeh.dk/2017/01/03/decoupling-application-errors-from-domain-models 2017-01-03T12:26:00+00:00 Mark Seemann <div id="post"> <p> <em>How to prevent application-specific error cases from infecting your domain models.</em> </p> <p> Functional error-handling is often done with the Either monad. If all is good, the <em>right</em> case is returned, but if things go wrong, you'll want to return a value that indicates the error. In an application, you'll often need to be able to distinguish between different kinds of errors. </p> <h3 id="00bb2d7c896649ec9f11959758c15600"> From application errors to HTTP responses <a href="#00bb2d7c896649ec9f11959758c15600" title="permalink">#</a> </h3> <p> When an application encounters an error, it should respond appropriately. A GUI-based application should inform the user about the error, a batch job should log it, and a <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> API should return the appropriate HTTP status code. </p> <p> Regular readers of this blog will know that I write many RESTful APIs in F#, using ASP.NET Web API. Since I like to write functional F#, but ASP.NET Web API is an object-oriented framework, I prefer to escape the object-oriented framework as soon as possible. (In general, it makes good architectural sense to write most of your code as framework-independent as possible.) </p> <p> In my <a href="https://blog.ploeh.dk/tdd-with-fsharp">Test-Driven Development with F#</a> Pluralsight course (a <a href="http://www.infoq.com/presentations/mock-fsharp-tdd">free, condensed version is also available</a>), I demonstrate how to handle various error cases in a Controller class: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">ReservationsController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(dtr&nbsp;:&nbsp;<span style="color:teal;">ReservationDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;dtr&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationError</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">CapacityExceeded</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">StatusCode</span>&nbsp;<span style="color:teal;">HttpStatusCode</span>.Forbidden&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;:&gt;&nbsp;_</pre> </p> <p> The injected <code>imp</code> function is a complete, composed, vertical feature implementation that performs both input validation, business logic, and data access. If input validation fails, it'll return <code>Failure (ValidationError msg)</code>, and that value is translated to a <code>400 Bad Request</code> response. Likewise, if the business logic returns <code>Failure CapacityExceeded</code>, the response becomes <code>403 Forbidden</code>, and a success is returned as <code>200 OK</code>. </p> <p> Both <code>ValidationError</code> and <code>CapacityExceeded</code> are cases of an <code>Error</code> type. This is only a simple example, so these are the only cases defined by that type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Error</span>&nbsp;= |&nbsp;<span style="color:navy;">ValidationError</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span> |&nbsp;<span style="color:navy;">CapacityExceeded</span></pre> </p> <p> This seems reasonable, but there's a problem. </p> <h3 id="c93acdc94ef14b79b6a550660b11ab58"> Error infection <a href="#c93acdc94ef14b79b6a550660b11ab58" title="permalink">#</a> </h3> <p> In F#, a function can't use a type unless that type is already defined. This is a problem because the <code>Error</code> type defined above mixes different concerns. If you seek to <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">make illegal states unrepresentable</a>, it follows that validation is not a concern in your domain model. Validation is still important at the boundary of an application, so you can't just ignore it. The <code>ValidationError</code> case relates to the application boundary, while <code>CapacityExceeded</code> relates to the domain model. </p> <p> Still, when implementing your domain model, you may want to return a <code>CapacityExceeded</code> value from time to time: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;Result&lt;Reservation,Error&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">checkCapacity</span>&nbsp;capacity&nbsp;reservedSeats&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;capacity&nbsp;&lt;&nbsp;reservation.Quantity&nbsp;+&nbsp;reservedSeats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">CapacityExceeded</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;reservation</pre> </p> <p> Notice how the return type of this function is <code>Result&lt;Reservation,Error&gt;</code>. In order to be able to implement your domain model, you've now pulled in the <code>Error</code> type, which also defines the <code>ValidationError</code> case. Your domain model is now polluted by an application boundary concern. </p> <p> I think many developers would consider this trivial, but in my experience, failure to manage dependencies is the dominant reason for <a href="https://en.wikipedia.org/wiki/Software_rot">code rot</a>. It makes the code less general, and less reusable, because it's now coupled to something that may not fit into a different context. </p> <p> Particularly, the situation in the example looks like this: </p> <p> <img src="/content/binary/error-infection-dependency-diagram.png" alt="Dependency diagram"> </p> <p> Boundary and data access modules depend on the domain model, <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">as they should</a>, but everything depends on the <code>Error</code> type. This is wrong. Modules or libraries should be able to define their own error types. </p> <p> The <code>Error</code> type belongs in the <a href="/2011/07/28/CompositionRoot">Composition Root</a>, but it's impossible to put it there because F# prevents circular dependencies (a <a href="/2015/04/13/less-is-more-language-features">treasured language feature</a>). </p> <p> Fortunately, the fix is straightforward. </p> <h3 id="3f9b40635e9b4c90b6be22cb139f4632"> Mapped Either values <a href="#3f9b40635e9b4c90b6be22cb139f4632" title="permalink">#</a> </h3> <p> A domain model should be self-contained. As Robert C. Martin puts it in <a href="http://amzn.to/19W4JHk">APPP</a>: <blockquote> Abstractions should not depend upon details. Details should depend upon abstractions. </blockquote> Your domain model is an abstraction of the real world (that's why it's called a <em>model</em>), and is the reason you're developing a piece of software in the first place. So start with the domain model: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">BookingError</span>&nbsp;=&nbsp;<span style="color:navy;">CapacityExceeded</span> <span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;Result&lt;Reservation,BookingError&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">checkCapacity</span>&nbsp;capacity&nbsp;reservedSeats&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;capacity&nbsp;&lt;&nbsp;reservation.Quantity&nbsp;+&nbsp;reservedSeats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">CapacityExceeded</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;reservation</pre> </p> <p> In this example, there's only a single type of domain error (<code>CapacityExceeded</code>), but that's mostly because this is an example. Real production code could define a domain error union with several cases. The crux of the matter is that <code>BookingError</code> isn't infected with irrelevant implementation details like validation error types. </p> <p> You're still going to need an exhaustive discriminated union to model all possible error cases for your particular application, but that type belongs in the Composition Root. Accordingly, you also need a way to return validation errors in your validation module. Often, a <code>string</code> is all you need: </p> <p> <pre><span style="color:green;">//&nbsp;ReservationDtr&nbsp;-&gt;&nbsp;Result&lt;Reservation,string&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">validateReservation</span>&nbsp;(dtr&nbsp;:&nbsp;<span style="color:teal;">ReservationDtr</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;dtr.Date&nbsp;|&gt;&nbsp;<span style="color:teal;">DateTimeOffset</span>.<span style="color:navy;">TryParse</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;(<span style="color:blue;">true</span>,&nbsp;date)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Reservation</span>.Date&nbsp;=&nbsp;date &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;dtr.Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;dtr.Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;dtr.Quantity&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span></pre> </p> <p> The <code>validateReservation</code> function returns a <code>Reservation</code> value when validation succeeds, and a simple <code>string</code> with an error message if it fails. </p> <p> You could, conceivably, return <code>string</code> values for errors from many different places in your code, so you're going to map them into an appropriate error case that makes sense in your application. </p> <p> In this particular example, the Controller shown above should still look like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Error</span>&nbsp;= |&nbsp;<span style="color:navy;">ValidationError</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span> |&nbsp;<span style="color:navy;">DomainError</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">ReservationsController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(dtr&nbsp;:&nbsp;<span style="color:teal;">ReservationDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;dtr&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationError</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">DomainError</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">StatusCode</span>&nbsp;<span style="color:teal;">HttpStatusCode</span>.Forbidden&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;:&gt;&nbsp;_</pre> </p> <p> Notice how similar this is to the initial example. The important difference, however, is that <code>Error</code> is defined in the same module that also implements <code>ReservationsController</code>. This is part of the composition of the specific application. </p> <p> In order to make that work, you're going to need to map from one failure type to another. This is trivial to do with an extra function belonging to your Result (or Either) module: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Result&lt;&#39;c,&#39;a&gt;&nbsp;-&gt;&nbsp;Result&lt;&#39;c,&#39;b&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">mapFailure</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;x&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;succ&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;succ &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;fail&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;fail)</pre> </p> <p> This function takes any <code>Result</code> value and maps the failure case instead of the success case. It enables you to transform e.g. a <code>BookingError</code> into a <code>DomainError</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;candidate&nbsp;=&nbsp;<span style="color:blue;">either</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;r&nbsp;=&nbsp;<span style="color:navy;">validateReservation</span>&nbsp;candidate&nbsp;|&gt;&nbsp;<span style="color:navy;">mapFailure</span>&nbsp;<span style="color:navy;">ValidationError</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;i&nbsp;=&nbsp;<span style="color:teal;">SqlGateway</span>.<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString&nbsp;r.Date &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;r&nbsp;=&nbsp;<span style="color:navy;">checkCapacity</span>&nbsp;10&nbsp;i&nbsp;r&nbsp;|&gt;&nbsp;<span style="color:navy;">mapFailure</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">DomainError</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:teal;">SqlGateway</span>.<span style="color:navy;">saveReservation</span>&nbsp;connectionString&nbsp;r&nbsp;}</pre> </p> <p> This composition is a variation of the composition I've <a href="/2016/03/21/composition-with-an-either-computation-expression">previously published</a>. The only difference is that the error cases are now mapped into the application-specific <code>Error</code> type. </p> <h3 id="5b359d03cef942659061ed528ab96e75"> Conclusion <a href="#5b359d03cef942659061ed528ab96e75" title="permalink">#</a> </h3> <p> Errors can occur in diverse places in your code base: when validating input, when making business decisions, when writing to, or reading from, databases, and so on. </p> <p> When you use the Either monad for error handling, in a strongly typed language like F#, you'll need to define a discriminated union that models all the error cases you care about in the specific application. You can map module-specific error types into such a comprehensive error type using a function like <code>mapFailure</code>. In Haskell, it would be the <code>first</code> function of the <code>Bifunctor</code> typeclass, so this is a well-known function. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="bfbf973e911e41f2bf6566c5691be9e3"> <div class="comment-author"><a href="https://github.com/wortho">David Worthington</a> <a href="#bfbf973e911e41f2bf6566c5691be9e3">#</a></div> <div class="comment-content"> <p> Mark, </p> <p> Why is it a problem to use <code>HttpStatusCode</code> in the domain model. They appear to be a standard way of categorizing errors. </p> </div> <div class="comment-date">2017-02-07 13:37 UTC</div> </div> <div class="comment" id="38974d76e09246be88c463895d278526"> <div class="comment-author"> <a href="">Mark Seemann</a> <a href="#38974d76e09246be88c463895d278526">#</a></div> <div class="comment-content"> </div> <p> David, thank you for writing. The answer depends on your goals and definition of <em>domain model</em>. </p> <p> I usually think of domain models in terms of separation of concerns. The purpose of a domain model is to model the business logic, and as Martin Fowler writes in <a href="http://bit.ly/patternsofeaa">PoEAA</a> about the Domain Model pattern, "you'll want the minimum of coupling from the <em>Domain Model</em> to other layers in the system. You'll notice that a guiding force of many layering patterns is to keep as few dependencies as possible between the domain model and the other parts of the system." </p> <p> In other words, you're separating the concern of implementing the business rules from the concerns of being able to save data in a database, render it on a screen, send emails, and so on. While also important, these are separate concerns, and I want to be able to vary those independently. </p> <p> People often hear statements like that as though I want to reserve myself the right to replace my SQL Server database with Neo4J (more on that later, though!). That's actually not my main goal, but I find that if concerns are mixed, <em>all</em> change becomes harder. It becomes more difficult to change how data is saved in a database, <em>and</em> it becomes harder to change business rules. </p> <p> The Dependency Inversion Principle tries to address such problems by advising that abstractions shouldn't depend on implementation details, but instead, implementation details should depend on abstractions. </p> <p> This is where the goals come in. I find Robert C. Martin's definition of software architecture helpful. Paraphrased from memory, he defines a software architect's role as enabling change; not predicting change, but making sure that when change has to happen, it's as economical as possible. </p> <p> As an architect, one of the heuristics I use is that I try to imagine how easily I can replace one component with another. It's not that I really believe that I may have to replace the SQL Server database with Neo4J, but thinking about how hard it would be gives me some insights about how to structure a software solution. </p> <p> I also imagine what it'd be like to port an application to another environment. Can I port my web site's business rules to a batch job? Can I port my desktop client to a smart phone app? Again, it's not that I necessarily predict that I'll have to do this, but it tells me something about the degrees of freedom offered by the architecture. </p> <p> If not explicitly addressed, the opposite of freedom tends to happen. In <a href="http://amzn.to/19W4JHk">APPP</a>, Robert C. Martin describes a number of design smells, one of them <em>Immobility</em>: "A design is immobile when it contains parts that could be useful in other systems, but the effort and risk involved with separating those parts from the original system are too great. This is an unfortunate, but very common occurrence." </p> <p> Almost as side-effect, an immobile system is difficult to test. A unit test is a different environment than the intended environment. Well-architected systems are easy to unit test. </p> <p> HTTP is a communications protocol. Its purpose is to enable exchange of information over networks. While it does that well, it's specifically concerned with that purpose. This includes HTTP status code. </p> <p> If you use the heuristic of imagining that you'd have to move the heart of your application to a batch job, status codes like <code>301 Moved Permanently</code>, <code>404 Not Found</code>, or <code>405 Method Not Allowed</code> make little sense. </p> <p> Using HTTP status codes in a domain model couples the model to a particular environment, at least conceptually. It has little to do with the <em>ubiquitous language</em> that Eric Evans discusses in <a href="http://amzn.to/WBCwx7">DDD</a>. </p> <div class="comment-date">2017-02-07 17:08 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. From REST to algebraic data https://blog.ploeh.dk/2016/12/16/from-rest-to-algebraic-data 2016-12-16T07:23:00+00:00 Mark Seemann <div id="post"> <p> <em>Mapping RESTful HTTP requests to values of algebraic data types is easy.</em> </p> <p> In previous articles, you've seen <a href="/2016/11/28/easy-domain-modelling-with-types">how to easily model a simple domain model with algebraic data types</a>, and <a href="/2016/12/07/domain-modelling-with-rest">how to use RESTful API design to surface such a model at the boundary of an application</a>. In this article, you'll see how trivial it is to map incoming HTTP requests back to values of <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>. </p> <p> The advantage of <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> is that you can make illegal states unrepresentable. Clients follow links, and while clients are supposed to treat links as opaque values, URLs still contain information your API can use. </p> <h3 id="b1812bb9c6d6488ab58c506b5a642983"> Routing and dispatching <a href="#b1812bb9c6d6488ab58c506b5a642983" title="permalink">#</a> </h3> <p> Continuing where the <a href="/2016/12/07/domain-modelling-with-rest">previous article</a> left off, clients can issue POST requests against a URL like <code>https://example.com/credit-card</code>. On the server, a well-known piece of code handles such requests. (In the example code base I've used so far, I've been using ASP.NET Web API, so the code that handles such a request is a Controller.) Since you know that URLs like that are always routed to that particular piece of code, you can create a new <code>PaymentType</code> value that specifically represents an individual payment with a credit card: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;paymentType&nbsp;=&nbsp;<span style="color:navy;">Individual</span>&nbsp;{&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;credit-card&quot;</span>;&nbsp;Action&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Pay&quot;</span>&nbsp;}</pre> </p> <p> If, on the other hand, the client is using a provided link to POST a representation against the URL <code>https://example.com/recurrent/start/credit-card</code>, your server-side dispatcher will route the request to a different handler (Controller), in which case you can create a <code>PaymentType</code> value like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;paymentType&nbsp;=&nbsp;<span style="color:navy;">Parent</span>&nbsp;{&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;credit-card&quot;</span>;&nbsp;Action&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Pay&quot;</span>&nbsp;}</pre> </p> <p> Finally, if the client has already created a parent payment and is now using the resulting link to create child payments, it may be POSTing to a URL like <code>https://example.com/recurrent/42</code>. Your server-side dispatcher will route that request to a third handler. Most web frameworks, including ASP.NET Web API, will be able to pull values out of URLs. In this case, you can configure it so that it pulls the value <code>42</code> out of the URL and binds it to a value called <code>transactionKey</code>. With this, again it's trivial to create a <code>PaymentType</code> value: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;paymentType&nbsp;=&nbsp;<span style="color:navy;">Child</span>&nbsp;(transactionKey,&nbsp;{&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;credit-card&quot;</span>;&nbsp;Action&nbsp;=&nbsp;<span style="color:#a31515;">&quot;PayRecurrent&quot;</span>&nbsp;})</pre> </p> <p> Notice that, despite containing different data, and being created three different places in the code base, they all have the same type: <code>PaymentType</code>. This means that you can pass these values to a common <code>pay</code> function, which handles the actual communication with the third-party payment service. </p> <h3 id="61b27ff3b1bd4e13ac0c33da2313d088"> Code reuse <a href="#61b27ff3b1bd4e13ac0c33da2313d088" title="permalink">#</a> </h3> <p> Independent of the route the data arrived at, a central, reusable function named <code>pay</code> handles all such payments. This is still an impure boundary function that takes various other input apart from <code>PaymentType</code>. Without going into too much detail, it has a type like <code>Config -&gt; PaymentType -&gt; Result&lt;PaymentDtr,BoundaryFailure&gt;</code>. Don't worry if some of the details look obscure; the important point is that <code>pay</code> is a function that takes a <code>PaymentType</code> value as input. You can visualise the transition from HTTP requests to a function call like this: </p> <p> <img src="/content/binary/from-http-to-payment-type-values.png" alt="Three different URLs mapped to three different PaymentType values, which are again passed to the single pay function"> </p> <p> The <code>pay</code> function is composed from various smaller functions, some <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> and some impure. Ultimately, it transforms all the input data to the format required by the third-party payment service, and forwards the transaction information. Inside that function you'll find the pattern match that you saw in <a href="/2016/12/07/domain-modelling-with-rest">my previous article</a>. </p> <h3 id="e7237c48a18749e2aa4ff3a989e4b82f"> Summary <a href="#e7237c48a18749e2aa4ff3a989e4b82f" title="permalink">#</a> </h3> <p> By making good use of routing and dispatching, you can easily map incoming HTTP requests to values of algebraic data types. This enables you to close the loop on exposing your domain model at the boundary of your system. Not only can clients request data from your API in terms of your model, but when clients send data to your API, you can translate that data back to your model. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Domain modelling with REST https://blog.ploeh.dk/2016/12/07/domain-modelling-with-rest 2016-12-07T09:15:00+00:00 Mark Seemann <div id="post"> <p> <em>Make illegal states unrepresentable by using hyperlinks as the engine of application state.</em> </p> <p> Every piece of software, whether it's a web service, smart phone app, batch job, or speech recognition system, interfaces with the world in some way. Sometimes, that interface is a user interface, sometimes it's a machine-readable interface; sometimes it involves rendering pixels on a screen, and sometimes it involves writing to files, selecting records from a database, sending emails, and so on. </p> <p> Programmers often struggle with how to model these interactions. This is particularly difficult because at the boundaries, systems no longer adhere to popular programming paradigms. Previously, I've explained why, <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">at the boundaries, applications aren't object-oriented</a>. By the same type of argument, neither are they functional (as in 'functional programming'). </p> <p> If that's the case, why should you even bother with 'domain modelling'? Particularly, does it even matter that, with <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>, you can <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">make illegal states unrepresentable</a>? If you need to compromise once you hit the boundary of your application, is it worth the effort? </p> <p> It is, if you structure your application correctly. Proper (<a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a>) <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> architecture gives you one way to structure applications in such a way that you can surface the constraints of your domain model to the interface layer. When done correctly, you can also make illegal states unrepresentable at the boundary. </p> <h3 id="075711be27cb40939c560e11797e0dbd"> A payment example <a href="#075711be27cb40939c560e11797e0dbd" title="permalink">#</a> </h3> <p> In my <a href="/2016/11/28/easy-domain-modelling-with-types">previous article</a>, I demonstrated how to use (static) types to model an on-line payment domain. To summarise, my task was to model three types of payments: <ul> <li>Individual payments, which happen only once.</li> <li>Parent payments, which start a long-term payment relationship.</li> <li>Child payments, which are automated payments originally authorised by an initial parent payment.</li> </ul> The constraint I had to model is that a child payment requires a <em>transaction key</em> that identifies the original parent payment. When making a payment, however, it's only valid to supply a transaction key for a child payment. It'd be invalid to supply a transaction key to a parent or an individual payment. On the other hand, you have to distinguish individual payments from parent payments, because only parent payments can be used to start a long-term payment relationship. All this leads to this table of possible combinations: <table> <thead> <tr> <th></th> <th><code>"StartRecurrent" : "false"</code></th> <th><code>"StartRecurrent" : "true"</code></th> </tr> </thead> <tbody> <tr> <td><code>"OriginalTransactionKey" : null</code></td> <td><em>Individual</em></td> <td><em>Parent</em></td> </tr> <tr> <td><code>"OriginalTransactionKey" : "1234ABCD"</code></td> <td><em>Child</em></td> <td><strong>(Illegal)</strong></td> </tr> </tbody> </table> The table shows that it's illegal to simultaneously provide a transaction key and set <code>StartRecurrent</code> to <code>true</code>. The other three combinations, on the other hand, are valid. </p> <p> As I demonstrated in my <a href="/2016/11/28/easy-domain-modelling-with-types">previous article</a>, you can easily model this with algebraic data types. </p> <p> At the boundary, however, there are no static types, so how could you model something like that as a web service? </p> <h3 id="bafc873c4dd34bd28f8fd23ef75723c3"> A RESTful solution <a href="#bafc873c4dd34bd28f8fd23ef75723c3" title="permalink">#</a> </h3> <p> A major advantage of REST is that it gives you a way to realise your domain model at the application boundary. It does require, though, that you design the API according to <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3 of the Richardson maturity model</a>. In other words, it's not REST if you're merely tunnelling JSON (or XML) through HTTP. It's still not REST if you publish URL templates and expect clients to fill data into specific place-holders of those URLs. </p> <p> It's REST if the only way a client can interact with your API is by following hyperlinks. </p> <p> If you follow those design principles, however, it's easy to model the above payment domain as a RESTful API. </p> <p> In the following, I will show examples in XML, but it could as well have been JSON. After all, <a href="/2015/06/22/rest-implies-content-negotiation">a true REST API <em>must</em> support content negotiation</a>. One of the reasons that I prefer XML is that I can use <a href="https://en.wikipedia.org/wiki/XPath">XPath</a> to point out various nodes. </p> <p> A client must begin at a pre-published 'home' resource, just like the home page of a web site. This resource presents <a href="https://en.wikipedia.org/wiki/Affordance">affordances</a> in the shape of hyperlinks. As recommended by the <a href="http://amzn.to/YFnkRg">RESTful Web Services Cookbook</a>, I always use <a href="https://en.wikipedia.org/wiki/Atom_(standard)">ATOM</a> links: </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:#a31515;">home</span><span style="color:blue;">&nbsp;</span><span style="color:red;">xmlns</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://example.com/payment</span>&quot; <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:red;">xmlns:atom</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://www.w3.org/2005/Atom</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">payment-methods</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">payment-method</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">links</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">atom:link</span><span style="color:blue;">&nbsp;</span><span style="color:red;">rel</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">example:pay-individual</span>&quot; <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:red;">href</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">https://example.com/gift-card</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">links</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">id</span><span style="color:blue;">&gt;</span>gift-card<span style="color:blue;">&lt;/</span><span style="color:#a31515;">id</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">payment-method</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">payment-method</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">links</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">atom:link</span><span style="color:blue;">&nbsp;</span><span style="color:red;">rel</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">example:pay-individual</span>&quot; <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:red;">href</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">https://example.com/credit-card</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">atom:link</span><span style="color:blue;">&nbsp;</span><span style="color:red;">rel</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">example:pay-parent</span>&quot; <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:red;">href</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">https://example.com/recurrent/start/credit-card</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">links</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">id</span><span style="color:blue;">&gt;</span>credit-card<span style="color:blue;">&lt;/</span><span style="color:#a31515;">id</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">payment-method</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">payment-methods</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">home</span><span style="color:blue;">&gt;</span> </pre> </p> <p> A client receiving the above response is effectively presented with a choice. It can choose to pay with a gift card or credit card, and nothing else, however much it'd like to pay with, say, PayPal. For each of these payment methods, zero or more links are available. </p> <p> Specifically, there are links to create both an individual or a parent payment with a credit card, but it's only possible to make an individual payment with a gift card. You can't set up a long-term, automated payment relationship with a gift card. (This may or may not make sense, depending on how you look at it, but it's fundamentally a business decision.) </p> <p> Links are defined by <em>relationship types</em>, which are unique identifiers present in the <code>rel</code> attributes. You can think of them as equivalent to the human-readable text in an HTML <code>a</code> tag that suggests to a human user what to expect from clicking the link; only, <code>rel</code> attribute values are machine-readable and part of the contract between client and service. </p> <p> Notice how the above XML representation only gives a client the option of making an individual or a parent payment with a credit card. A client can't make a child payment at this point. This follows the domain model, because you can't make a child payment without first having made a parent payment. </p> <h3 id="9db3418b30fa4599b7e939b23409769d"> RESTful individual payments <a href="#9db3418b30fa4599b7e939b23409769d" title="permalink">#</a> </h3> <p> If a client wishes to make an individual payment, it follows the link identified by </p> <p> <pre>/home/payment-methods/payment-method[id = 'credit-card']/links/atom:link[@rel = 'example:pay-individual']/@href</pre> </p> <p> In the above XPath query, I've ignored the default document namespace in order to make the expression more readable. The query returns <code>https://example.com/credit-card</code>, and the client can now make an HTTP POST request against that URL with a JSON or XML document containing details about the payment (not shown here, because it's not important for this particular discussion). </p> <h3 id="b928d876b51149698d9057e881a50ff2"> RESTful parent payments <a href="#b928d876b51149698d9057e881a50ff2" title="permalink">#</a> </h3> <p> If a client wishes to make a parent payment, the initial procedure is the same. First, it follows the link identified by </p> <p> <pre>/home/payment-methods/payment-method[id = 'credit-card']/links/atom:link[@rel = 'example:pay-parent']/@href</pre> </p> <p> The result of that XPath query is <code>https://example.com/recurrent/start/credit-card</code>, so the client can make an HTTP POST request against that URL with the payment details. Unlike the response for an individual payment, the response for a parent payment contains <em>another</em> link: </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:#a31515;">payment</span><span style="color:blue;">&nbsp;</span><span style="color:red;">xmlns</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://example.com/payment</span>&quot; <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:red;">xmlns:atom</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://www.w3.org/2005/Atom</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">links</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">atom:link</span><span style="color:blue;">&nbsp;</span><span style="color:red;">rel</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">example:pay-child</span>&quot; <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:red;">href</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">https://example.com/recurrent/42</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">atom:link</span><span style="color:blue;">&nbsp;</span><span style="color:red;">rel</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">example:payment-details</span>&quot; <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:red;">href</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">https://example.com/42</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">links</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">amount</span><span style="color:blue;">&gt;</span>13.37<span style="color:blue;">&lt;/</span><span style="color:#a31515;">amount</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">currency</span><span style="color:blue;">&gt;</span>EUR<span style="color:blue;">&lt;/</span><span style="color:#a31515;">currency</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">invoice</span><span style="color:blue;">&gt;</span>1234567890<span style="color:blue;">&lt;/</span><span style="color:#a31515;">invoice</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">payment</span><span style="color:blue;">&gt;</span></pre> </p> <p> This response echoes the details of the payment: this is a payment of 13.37 EUR for invoice 1234567890. It also includes some links that a client can use to further interact with the payment: <ul> <li>The <code>example:payment-details</code> link can be used to query the API for details about the payment, for example its status.</li> <li>The <code>example:pay-child</code> link can be used to make a child payment.</li> </ul> The <code>example:pay-child</code> link is only returned if the previous payment was a parent payment. When a client makes an individual payment, this link isn't present in the response, but when the client makes a parent payment, it is. </p> <p> Another design principle of REST is that <a href="https://www.w3.org/Provider/Style/URI.html.en">cool URIs don't change</a>; once the API has shown a URL like <code>https://example.com/recurrent/42</code> to a client, it should honour that URL indefinitely. The upshot of that is that a client can <em>save</em> that URL for later use. If a client wants to, say, renew a subscription, it can make a new HTTP POST request to that URL <em>a month later</em>, and that's going to be a child payment. Clients don't have to <a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls">hack the URL</a> in order to figure out what the transaction key is; they can simply store the complete URL as is and use it later. </p> <h3 id="6f6ff72dd57d4b0e861256723577d01e"> A network of options <a href="#6f6ff72dd57d4b0e861256723577d01e" title="permalink">#</a> </h3> <p> Using a design like the one sketched above, you can make illegal states unrepresentative. There's no way for a client to make a payment with <code>StartRecurrent = true</code> and a non-null transaction key; there's no link to that combination. Such an API uses <a href="https://en.wikipedia.org/wiki/HATEOAS">hypermedia as the engine of application state</a>. </p> <p> It shouldn't be surprising that proper RESTful design works that way. After all, REST is essentially a distillate of the properties that make the World Wide Web work. On a human-readable web page, the user follows links to other pages, and a well-designed web site will only enable a link if the destination exists. </p> <p> You can even draw a graph of the API I've sketched above: </p> <p> <img src="/content/binary/graph-of-payment-paths.png" alt="Graph of payment options, including a start node, and end node, and a node for each of the three payment types"> </p> <p> In this diagram, you can see that when you make an individual payment, that's all you can do. You can also see that the only way to make a child payment is by first making a parent payment. There's also a path from parent payments directly to the end node, because a client doesn't <em>have</em> to make a child payment just because it made a parent payment. </p> <p> If you think that this looks like a finite state machine, then that's no coincidence. That's exactly what it is. You have states (the nodes) and paths between them. If a state is illegal, then don't add that node; only add nodes for legal states, then add links between the nodes that model legal transitions. </p> <p> Incidentally, languages like F# excel at implementing finite state machines, so it's no wonder I like to implement RESTful APIs in F#. </p> <h3 id="ec57e98dd1e049c88ee9d10c92686d9b"> Summary <a href="#ec57e98dd1e049c88ee9d10c92686d9b" title="permalink">#</a> </h3> <p> Truly RESTful design enables you to make illegal states unrepresentable by using hypermedia as the engine of application state. This gives you a powerful design tool to ensure that clients can only perform correct operations. </p> <p> As I also wrote in <a href="/2016/11/28/easy-domain-modelling-with-types">my previous article</a>, this, too, is no silver bullet. You can turn an API into a pit of success, but there are still many fault scenarios that you can't prevent. </p> <p> If you were intrigued by this article, but are having trouble applying these design techniques to your own field, I'm <a href="/hire-me">available for hire</a> for short or long-term engagements. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Easy domain modelling with types https://blog.ploeh.dk/2016/11/28/easy-domain-modelling-with-types 2016-11-28T07:21:00+00:00 Mark Seemann <div id="post"> <p> <em>Algebraic data types make domain modelling easy.</em> </p> <p> People often ask me if I think that F# is a good general-purpose language, and when I emphatically answer <em>yes!</em>, the natural next question is: <em>why?</em> </p> <p> For years, I've been able to answer this question in the abstract, but I've been looking for a good concrete example with which I could illustrate the answer. I believe that I've now found such an example. </p> <p> The abstract answer, by the way, is that F# has <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>, which makes domain modelling much easier than in languages that don't have such types. Don't worry if the word 'algebraic' sounds scary, though. It's not at all difficult to understand, and I'll show you a simple example. </p> <h3 id="603929780a5e4135aabfaa0ede10a393"> Payment types <a href="#603929780a5e4135aabfaa0ede10a393" title="permalink">#</a> </h3> <p> At the moment, I'm working on an integration project: I'm developing a RESTful API that serves as Facade in front of a third-party payment provider. The third-party provider exposes their own API and web-based GUI that enable our end users to pay for services using credit cards, PayPal, and so on. The API that I'm developing presents a simplified, RESTful API to other clients in our organisation. </p> <p> The example you're going to see here is real code that I'm writing in order to implement the desired functionality. </p> <p> The system must be able to handle several different types of payment: <ul> <li>Sometimes, a user pays for a single thing, and that's the end of that transaction.</li> <li>Other times, however, a user engages into a long-term payment relationship. This could be, for example, a subscription, or an 'auto-fill' style of relationship. This is handled in two distinct phases:</li> <ul> <li>An initial payment (can sometimes be for a zero amount) that authorises the merchant to make further transactions.</li> <li>Subsequent payments, based off that initial payment. These payments can be automated, because they require no further user interaction than the initial authorisation.</li> </ul> </ul> The third-party service calls these 'long-term relationship' payments for <em>recurring</em> payments, but in order to distinguish between the first and the subsequent payments in such a relationship, I decided to call them <em>parent</em> and <em>child</em> payments; accordingly, I call the one-off payments <em>individual</em> payments. </p> <p> You can indicate the type of payment when interacting with the payment service's JSON-based API, like this: </p> <p> <pre>{ &nbsp;&nbsp;... &nbsp;&nbsp;"StartRecurrent": "false" &nbsp;&nbsp;... }</pre> </p> <p> Obviously, as the (illegal) ellipses suggests, there's much more data associated with a payment, but that's not important in this example. Since <code>StartRecurrent</code> is <code>false</code>, this is either an individual payment, or a child payment. If you want to start a long-term relationship, you must create a parent payment and set <code>StartRecurrent</code> to <code>true</code>. </p> <p> Child payments, however, are a bit different, because you have to tell the payment service about the parent payment: </p> <p><pre>{ &nbsp;&nbsp;... &nbsp;&nbsp;"OriginalTransactionKey": "1234ABCD", &nbsp;&nbsp;"StartRecurrent": "false" &nbsp;&nbsp;... }</pre> </p> <p> As you can see, when making a child payment, you supply the transaction ID for the parent payment. (This ID is given to you by the payment service when you initiate the parent payment.) </p> <p> In this case, you're clearly <em>not</em> starting a <em>new</em> recurrent transaction. </p> <p> There are two dimensions of variation in this example: <code>StartRecurrent</code> and <code>OriginalTransactionKey</code>. Let's put them in a table: <table> <thead> <tr> <th></th> <th><code>"StartRecurrent" : "false"</code></th> <th><code>"StartRecurrent" : "true"</code></th> </tr> </thead> <tbody> <tr> <td><code>"OriginalTransactionKey" : null</code></td> <td><em>Individual</em></td> <td><em>Parent</em></td> </tr> <tr> <td><code>"OriginalTransactionKey" : "1234ABCD"</code></td> <td><em>Child</em></td> <td><strong>(Illegal)</strong></td> </tr> </tbody> </table> As the table suggests, the combination of an <code>OriginalTransactionKey</code> and setting <code>StartRecurrent</code> to <code>true</code> is illegal, or, in best case, meaningless. </p> <p> How would you model the rules laid out in the above table? In languages like C#, it's difficult, but in F# it's easy. </p> <h3 id="97937a8bcdc742899a18945bf3a15814"> C# attempts <a href="#97937a8bcdc742899a18945bf3a15814" title="permalink">#</a> </h3> <p> Most C# developers would, I think, attempt to model a payment transaction with a class. If they aim for <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">poka-yoke design</a>, they might come up with a design like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PaymentType</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;PaymentType(<span style="color:blue;">bool</span>&nbsp;startRecurrent) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.StartRecurrent&nbsp;=&nbsp;startRecurrent; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;PaymentType(<span style="color:blue;">string</span>&nbsp;originalTransactionKey) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(originalTransactionKey&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(originalTransactionKey)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.StartRecurrent&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.OriginalTransactionKey&nbsp;=&nbsp;originalTransactionKey; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;StartRecurrent&nbsp;{&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;OriginalTransactionKey&nbsp;{&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">set</span>;&nbsp;<span style="color:blue;">get</span>;&nbsp;} }</pre> </p> <p> This goes a fair way towards <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">making illegal states unrepresentable</a>, but it doesn't communicate to a fellow programmer how it should be used. </p> <p> Code that <em>uses</em> instances of this <code>PaymentType</code> class could attempt to read the <code>OriginalTransactionKey</code>, which, depending on the type of payment, could return null. That sort of design leads to <a href="/2013/07/08/defensive-coding">defensive coding</a>. </p> <p> Other people might attempt to solve the problem by designing a class hierarchy: </p> <p> <img src="/content/binary/hypothetical-payment-class-hierarchy.png" alt="A hypothetical payment class hierarchy, showing a Payment base class, and three derived classes: IndividualPayment, ParentPayment, and ChildPayment."> </p> <p> (A variation on this design is to define an <code>IPayment</code> interface, and three concrete classes that implement that interface.) </p> <p> This design trades better protection of invariants for violations of the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>. Clients will have to (attempt to) downcast to subtypes in order to access all relevant data (particularly <code>OriginalTransactionKey</code>). </p> <p> For completeness sake, I can think of at least one other option with significantly different trade-offs: applying the <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor design pattern</a>. This is, however, quite a complex solution, and most people will find the disadvantages greater than the benefits. </p> <p> Is it such a big deal, then? After all, it's only a single data value (<code>OriginalTransactionKey</code>) that may or may not be there. Surely, most programmers will be able to deal with that. </p> <p> This may be true in this isolated case, but keep in mind that this is only a motivating <em>example</em>. In many other situations, the domain you're trying to model is much more intricate, with many more exceptions to general rules. The more dimensions you add, the more difficult it becomes to reason about the code. </p> <h3 id="d69170257e3148a2aa280c6a02b0bf3e"> F# model <a href="#d69170257e3148a2aa280c6a02b0bf3e" title="permalink">#</a> </h3> <p> F#, on the other hand, makes dealing with such problems so simple that it's almost anticlimactic. The reason is that F#'s type system enables you to model <em>alternatives</em> of data, in addition to the combinations of data that C# (or Java) enables. Such alternatives are called <a href="https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/discriminated-unions">discriminated unions</a>. </p> <p> In the code base I'm currently developing, I model the various payment types like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PaymentService</span>&nbsp;=&nbsp;{&nbsp;Name&nbsp;:&nbsp;<span style="color:teal;">string</span>;&nbsp;Action&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;} <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PaymentType</span>&nbsp;= |&nbsp;<span style="color:navy;">Individual</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentService</span> |&nbsp;<span style="color:navy;">Parent</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentService</span> |&nbsp;<span style="color:navy;">Child</span>&nbsp;<span style="color:blue;">of</span>&nbsp;originalTransactionKey&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;*&nbsp;paymentService&nbsp;:&nbsp;<span style="color:teal;">PaymentService</span></pre> </p> <p> Here, <code>PaymentService</code> is a record type with some data about the payment (e.g. which credit card to use). </p> <p> Even if you're not used to reading F# code, you can see three alternatives outlined on each of the three lines of code that start with a vertical bar (<code>|</code>). The <code>PaymentType</code> type has <em>exactly</em> three 'subtypes' (they're called <em>cases</em>, though). The illegal state of a non-null <code>OriginalTransactionKey</code> combined with <code>StartRecurrent</code> value of <code>true</code> is not possible. It can't be compiled. </p> <p> Not only that, but all clients given a <code>PaymentType</code> value <em>must</em> deal with all three cases (or the compiler will issue a warning). Here's one example where our code is creating the JSON document to send to the payment service: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;name,&nbsp;action,&nbsp;startRecurrent,&nbsp;transaction&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;req.PaymentType&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Individual</span>&nbsp;{&nbsp;Name&nbsp;=&nbsp;name;&nbsp;Action&nbsp;=&nbsp;action&nbsp;}&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name,&nbsp;action,&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Parent</span>&nbsp;{&nbsp;Name&nbsp;=&nbsp;name;&nbsp;Action&nbsp;=&nbsp;action&nbsp;}&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;name,&nbsp;action,&nbsp;<span style="color:blue;">true</span>,&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Child</span>&nbsp;(transactionKey,&nbsp;{&nbsp;Name&nbsp;=&nbsp;name;&nbsp;Action&nbsp;=&nbsp;action&nbsp;})&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name,&nbsp;action,&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:navy;">Some</span>&nbsp;transactionKey</pre> </p> <p> This code example also extracts <code>name</code> and <code>action</code> from the <code>PaymentType</code> value, but the relevant values to be aware of are <code>startRecurrent</code> and <code>transaction</code>. <ul> <li>For an individual payment, <code>startRecurrent</code> becomes <code>false</code> and <code>transaction</code> becomes <code>None</code> (meaning that the value is missing).</li> <li>For a parent payment, <code>startRecurrent</code> becomes <code>true</code> and <code>transaction</code> becomes <code>None</code>.</li> <li>For a child payment, <code>startRecurrent</code> becomes <code>false</code> and <code>transaction</code> becomes <code>Some transactionKey</code>.</li> </ul> Notice that the (parent) <code>transactionKey</code> is only available when the payment is a child payment. </p> <p> The values <code>startRecurrent</code> and <code>transaction </code> (as well as <code>name</code> and <code>action</code>) are then used to create a JSON document. I'm not showing that part of the code here, since there's actually a lot going on in the real code base, and it's not related to how to model the domain. Imagine that these values are passed to a constructor. </p> <p> This is a real-world example that, I hope, demonstrates why I prefer F# over C# for domain modelling. The type system enables me to model alternatives as well as combinations of data, and thereby <em>making illegal states unrepresentable</em> - all in only a few lines of code. </p> <h3 id="2ee04a5799964fa0a4e21bdda69ec4c7"> Summary <a href="#2ee04a5799964fa0a4e21bdda69ec4c7" title="permalink">#</a> </h3> <p> Classes, in languages like C# and Java, enable you to model <em>combinations</em> of data. The more fields or properties you add to a class, the more combinations are possible. This is often useful, but sometimes you need to be able to model <em>alternatives</em>, rather than combinations. </p> <p> Some languages, like F#, Haskell, OCaml, Elm, Kotlin, and many others, have type systems that give you the power to model both combinations and alternatives. Such types systems are said to have <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>, but while the word sounds 'mathy', such types make it much easier to model complex domains. </p> <p> There are more reasons to love F# than only its algebraic data types, but this is the foremost reason I find it a better language for mainstream development work than C#. </p> <p> If you want to see a more complex example of modelling with types, a good next step would be <a href="/2016/02/10/types-properties-software-designing-with-types">the first article</a> in my <a href="/2016/02/10/types-properties-software">Types + Properties = Software</a> article series. </p> <p> Finally, I should be careful that I don't oversell the idea of making illegal states unrepresentable. Algebraic data types give you an extra dimension in which you can model domains, but there are still rules that they can't enforce. As an example, you can't state that integers must only fall in a certain range (e.g. only positive integers allowed). There are other type systems, such as <a href="https://en.wikipedia.org/wiki/Dependent_type">dependent types</a>, that give you even more power to embed domain rules into types, but as far as I know, there are no type systems that can fully model all rules as types. You'll still have to write some code as well. </p> <p> The article is an instalment in the <a href="https://sergeytihon.wordpress.com/2016/10/23/f-advent-calendar-in-english-2016">2016 F# Advent calendar</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="a11c91c9cead47ea821109066bc02b8e"> <div class="comment-author"><a href="https://lambdaklub.wordpress.com">Botond Balázs</a> <a href="#a11c91c9cead47ea821109066bc02b8e">#</a></div> <div class="comment-content"> <p>Mark,</p> <p>I must be missing something important but it seems to me that the only advantage of using F# in this case is that the match is enforced to be exhaustive by the compiler. And of course the syntax is also nicer than a bunch of if's. In all other respects, the solution is basically equivalent to the C# class hierarchy approach.</p> <p>Am I mistaken?</p> </div> <div class="comment-date">2016-12-03 08:38 UTC</div> </div> <div class="comment" id="f123fa3ac11b4814975bbc48424f7e3d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f123fa3ac11b4814975bbc48424f7e3d">#</a></div> <div class="comment-content"> <p> Botond, thank you for writing. The major advantage is that enumeration of all possible cases is available at compile-time. One derived advantage of that is that the compiler can check whether a piece of code handles all cases. That's already, in my experience, a big deal. The sooner you can get feedback on your work, the better, and <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">it doesn't get faster than compile-time feedback</a>. </p> <p> Another advantage of having all cases encoded in the type system is that it gives you better tool support. Imagine that you're looking at the return value of a function, and that this is the first time you're encountering that return type. If the return value is an abstract base class (or interface), you'll need to resort to either the documentation or reflection in order to figure out which subtypes exist. There can be arbitrarily many subtypes, and they can be scattered over arbitrarily many libraries (assemblies). Figuring out what to do with an abstract base class introduces a context switch that could have been avoided. </p> <p> This is exactly another advantage offered by discriminated unions: when a function returns a discriminated union, you can immediately get tool support to figure out what to do with it, even if you've never encountered the type before. </p> <p> The problem with examples such as the above is that I'm trying to explain how a language feature can help you with modelling <em>complex</em> domains, but if I try to present a really complex problem, no-one will have the patience to read the article. Instead, I have to come up with an example that's so simple that the reader doesn't give up, and hopefully still complex enough that the reader can imagine how it's a stand-in for a more complex problem. </p> <p> When you look at the problem presented above, it's not <em>that</em> complex, so you can still keep a C# implementation in your head. As you add more variability to the problem, however, you can easily find yourself in a situation where the combinatorial explosion of possible values make it difficult to ensure that you've dealt with all edge cases. This is one of the main reasons that C# and Java code often throws run-time exceptions (particularly null-reference exceptions). </p> <p> It did, in fact, turn out that the above example domain became more complex as I learned more about the entire range of problems I had to solve. When I described the problem above, I thought that all payments would have pre-selected payment methods. In other words, when a user is presented with a check-out page, he or she selects the payment method (PayPal, direct debit, and so on), and only then, when we know payment method, do we initiate the payment flow. It turns out, though, that in some cases, we should start the payment flow first, and then let the user pick the payment method from a list of options. It should be noted, however, that user-selection only makes sense for interactive payments, so a <em>child</em> payment can never be user-selectable (since it's automated). </p> <p> It was trivial to extend the domain model with that new requirement: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PaymentService</span>&nbsp;=&nbsp;{&nbsp;Name&nbsp;:&nbsp;<span style="color:teal;">string</span>;&nbsp;Action&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;} <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PaymentMethod</span>&nbsp;= |&nbsp;<span style="color:navy;">PreSelected</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentService</span> |&nbsp;<span style="color:navy;">UserSelectable</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span>&nbsp;<span style="color:teal;">list</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">TransactionKey</span>&nbsp;=&nbsp;<span style="color:navy;">TransactionKey</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">override</span>&nbsp;this.<span style="color:navy;">ToString</span>&nbsp;()&nbsp;=&nbsp;<span style="color:blue;">match</span>&nbsp;this&nbsp;<span style="color:blue;">with</span>&nbsp;<span style="color:navy;">TransactionKey</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;s <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PaymentType</span>&nbsp;= |&nbsp;<span style="color:navy;">Individual</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentMethod</span> |&nbsp;<span style="color:navy;">Parent</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">PaymentMethod</span> |&nbsp;<span style="color:navy;">Child</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">TransactionKey</span>&nbsp;*&nbsp;<span style="color:teal;">PaymentService</span></pre> </p> <p> This effectively uses the static type system to state that both the <code>Individual</code> and <code>Parent</code> cases can be defined in one of two ways: <code>PreSelected</code> or <code>UserSelectable</code>, each of which, again, contains heterogeneous data (<code>PaymentService</code> versus <code>string list</code>). Child payments, on the other hand, can't be user-selectable, but must be defined by a <code>PaymentService</code> value, as well as a transaction key (at this point, I'd also created a single-case union for the transaction key, but that's a different topic; it's still a string). </p> <p> Handling all the different combinations was equally easy, and the compiler guarantees that I've handled all possible combinations: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;services,&nbsp;selectables,&nbsp;startRecurrent,&nbsp;transaction&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;req.PaymentType&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Individual</span>&nbsp;(<span style="color:navy;">PreSelected</span>&nbsp;ps)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">service</span>&nbsp;ps,&nbsp;<span style="color:navy;">None</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Individual</span>&nbsp;(<span style="color:navy;">UserSelectable</span>&nbsp;us)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[||],&nbsp;us&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">concat</span>&nbsp;<span style="color:#a31515;">&quot;,&nbsp;&quot;</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">Some</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Parent</span>&nbsp;(<span style="color:navy;">PreSelected</span>&nbsp;ps)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">service</span>&nbsp;ps,&nbsp;<span style="color:navy;">None</span>,&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Parent</span>&nbsp;(<span style="color:navy;">UserSelectable</span>&nbsp;us)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[||],&nbsp;us&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">concat</span>&nbsp;<span style="color:#a31515;">&quot;,&nbsp;&quot;</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">Some</span>,&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Child</span>&nbsp;(<span style="color:navy;">TransactionKey</span>&nbsp;transactionKey,&nbsp;ps)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">service</span>&nbsp;ps,&nbsp;<span style="color:navy;">None</span>,&nbsp;<span style="color:blue;">false</span>,&nbsp;<span style="color:navy;">Some</span>&nbsp;transactionKey</pre> </p> <p> How would you handle this with a class hierarchy, and what would the consuming code look like? </p> </div> <div class="comment-date">2016-12-06 10:50 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. When variable names are in the way https://blog.ploeh.dk/2016/10/25/when-variable-names-are-in-the-way 2016-10-25T06:20:00+00:00 Mark Seemann <div id="post"> <p> <em>While Clean Code recommends using good variable names to communicate the intent of code, sometimes, variable names can be in the way.</em> </p> <p> Good guides to more readable code, like <a href="http://amzn.to/XCJi9X">Clean Code</a>, explain how explicitly introducing variables with descriptive names can make the code easier to understand. There's much literature on the subject, so I'm not going to reiterate it here. It's not the topic of this article. </p> <p> In the majority of cases, introducing a well-named variable will make the code more readable. There are, however, no rules without exceptions. After all, <a href="http://martinfowler.com/bliki/TwoHardThings.html">one of the hardest tasks in programming is naming things</a>. In this article, I'll show you an example of such an exception. While the example is slightly elaborate, it's a <em>real-world</em> example I recently ran into. </p> <h3 id="0a00705faee3475e9041b83c7f94271a"> Escaping object-orientation <a href="#0a00705faee3475e9041b83c7f94271a" title="permalink">#</a> </h3> <p> Regular readers of this blog will know that I write many <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">RESTful APIs</a> in F#, but using ASP.NET Web API. Since I like to write functional F#, but ASP.NET Web API is an object-oriented framework, I prefer to escape the object-oriented framework as soon as possible. (In general, it makes good architectural sense to write most of your code as framework-independent as possible.) </p> <p> ASP.NET Web API expects you handle HTTP requests using <em>Controllers</em>, so I use Constructor Injection to inject a function that will do all the actual work, and delegate each request to a function call. It often looks like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PushController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(<span style="color:#9b9b9b;">portalId</span>&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">PushRequestDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;req&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_</pre> </p> <p> This particular Controller only handles HTTP POST requests, and it does it by delegating to the injected <code>imp</code> function and translating the return value of that function call to various HTTP responses. This enables me to compose <code>imp</code> from F# functions, and thereby escape the object-oriented design of ASP.NET Web API. In other words, each Controller is an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> over a functional implementation. </p> <p> For good measure, though, this Controller implementation ought to be unit tested. </p> <h3 id="6ec56c765c0e4fe1afcee96969651622"> A naive unit test attempt <a href="#6ec56c765c0e4fe1afcee96969651622" title="permalink">#</a> </h3> <p> Each HTTP request is handled at the boundary of the system, and <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">the boundary of the system is always impure</a> - even in Haskell. This is particularly clear in the case of the above <code>PushController</code>, because it handles <code>Success ()</code>. In success cases, the result is <code>()</code> (<code>unit</code>), which strongly implies a side effect. Thus, a unit test ought to care not only about what <code>imp</code> returns, but also the input to the function. </p> <p> While you could write a unit test like the following, it'd be naive. </p> <p> <pre>[&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Post&nbsp;returns&nbsp;correct&nbsp;result&nbsp;on&nbsp;validation&nbsp;failure``</span>&nbsp;req&nbsp;(<span style="color:navy;">NonNull</span>&nbsp;msg)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;_&nbsp;=&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">fail</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:teal;">PushController</span>&nbsp;(<span style="color:navy;">imp</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut.<span style="color:navy;">Post</span>&nbsp;req &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">convertsTo</span><span style="background:yellow;">&lt;</span><span style="background:yellow;">Results</span><span style="background:yellow;">.</span><span style="color:teal;background:yellow;">BadRequestErrorMessageResult</span><span style="background:yellow;">&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">map</span><span style="background:yellow;">&nbsp;(</span><span style="color:blue;background:yellow;">fun</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">&nbsp;</span><span style="color:blue;background:yellow;">-&gt;</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">.</span><span style="background:yellow;">Message</span><span style="background:yellow;">)</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">exists</span><span style="background:yellow;">&nbsp;((</span><span style="background:yellow;">=</span><span style="background:yellow;">)&nbsp;</span><span style="background:yellow;">msg</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> This unit test uses <a href="https://fscheck.github.io/FsCheck">FsCheck</a>'s integration for <a href="https://xunit.net">xUnit.net</a>, and <a href="http://www.swensensoftware.com/unquote">Unquote</a> for assertions. Additionally, it uses a <code>convertsTo</code> function that I've <a href="/2014/03/21/composed-assertions-with-unquote">previously described</a>. </p> <p> The <code>imp</code> function for <code>PushController</code> must have the type <code>PushRequestDtr -&gt; Result&lt;unit, BoundaryFailure&gt;</code>. In the unit test, it uses a wild-card (<code>_</code>) for the input value, so its type is <code>'a -&gt; Result&lt;'b, BoundaryFailure&gt;</code>. That's a wider type, but it matches the required type, so the test compiles (and passes). </p> <p> FsCheck populates the <code>req</code> argument to the test function itself. This value is passed to <code>sut.Post</code>. If you look at the definition of <code>sut.Post</code>, you may wonder what happened to the individual (and currently unused) <code>portalId</code> argument. The explanation is that the <code>Post</code> method can be viewed as a method with two parameters, but it can also be viewed as an impure function that takes a <em>single</em> argument of the type <code>string * PushRequestDtr</code> - a tuple. In other words, the test function's <code>req</code> argument is a tuple. The test is not only concise, but also robust against refactorings. If you change the signature of the <code>Post</code> method, odds are that the test will still compile. (This is one of the many benefits of type inference.) </p> <p> The problem with the test is that it doesn't verify the data flow into <code>imp</code>, so this version of <code>PushController</code> <em>also</em> passes the test: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">PushController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(<span style="color:#9b9b9b;">portalId</span>&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;<span style="color:#9b9b9b;">req</span>&nbsp;:&nbsp;<span style="color:teal;">PushRequestDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;minimalReq&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Transaction&nbsp;=&nbsp;{&nbsp;Invoice&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>;&nbsp;Status&nbsp;=&nbsp;{&nbsp;Code&nbsp;=&nbsp;{&nbsp;Code&nbsp;=&nbsp;0&nbsp;}&nbsp;}&nbsp;}&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;minimalReq&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_</pre> </p> <p> As the name implies, the <code>minimalReq</code> value is the 'smallest' value I can create of the <code>PushRequestDtr</code> type. As you can see, this implementation ignores the <code>req</code> method argument and instead passes <code>minimalReq</code> to <code>imp</code>. This is clearly wrong, but it passes the unit test test. </p> <h3 id="0464f07fb4ba4361ada9153a5c81924c"> Data flow testing <a href="#0464f07fb4ba4361ada9153a5c81924c" title="permalink">#</a> </h3> <p> Not only should you care about the output of <code>imp</code>, but you should also care about the input. This is because <code>imp</code> is inherently impure, so it'd be conceivable that the input values matter in some way. </p> <p> As <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> explains, automated tests should contain no branching, so I don't think it's a good idea to define a test-specific <code>imp</code> function using conditionals. Instead, we can use guard assertions to verify that the input is as expected: </p> <p> <pre>[&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Post&nbsp;returns&nbsp;correct&nbsp;result&nbsp;on&nbsp;validation&nbsp;failure``</span>&nbsp;req&nbsp;(<span style="color:navy;">NonNull</span>&nbsp;msg)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate&nbsp;=!&nbsp;<span style="color:navy;">snd</span>&nbsp;req &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">fail</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:teal;">PushController</span>&nbsp;(<span style="color:navy;">imp</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut.<span style="color:navy;">Post</span>&nbsp;req &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">convertsTo</span><span style="background:yellow;">&lt;</span><span style="background:yellow;">Results</span><span style="background:yellow;">.</span><span style="color:teal;background:yellow;">BadRequestErrorMessageResult</span><span style="background:yellow;">&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">map</span><span style="background:yellow;">&nbsp;(</span><span style="color:blue;background:yellow;">fun</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">&nbsp;</span><span style="color:blue;background:yellow;">-&gt;</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">.</span><span style="background:yellow;">Message</span><span style="background:yellow;">)</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">exists</span><span style="background:yellow;">&nbsp;((</span><span style="background:yellow;">=</span><span style="background:yellow;">)&nbsp;</span><span style="background:yellow;">msg</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> The <code>imp</code> function is now implemented using Unquote's custom <code>=!</code> operator, which means that <code>candidate</code> must equal <code>req</code>. If not, Unquote will throw an exception, and thereby fail the test. </p> <p> If <code>candidate</code> is equal to <code>snd req</code>, the <code>=!</code> operator does nothing, enabling the <code>imp</code> function to return the value <code>Result.fail (ValidationFailure msg)</code>. </p> <p> This version of the test verifies the entire data flow through <code>imp</code>: both input and output. </p> <p> There is, however, a small disadvantage to writing the <code>imp</code> code this way. It isn't a big issue, but it annoys me. </p> <p> Here's the heart of the matter: I had to come up with a name for the local <code>PushRequestDtr</code> value that the <code>=!</code> operator evaluates against <code>snd req</code>. I chose to call it <code>candidate</code>, which may seem reasonable, but that naming strategy doesn't scale. </p> <p> In order to keep the introductory example simple, I chose a Controller method that doesn't (yet) use its <code>portalId</code> argument, but the code base contains other Controllers, for example this one: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">IdealController</span>&nbsp;(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:teal;">ApiController</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>&nbsp;(portalId&nbsp;:&nbsp;<span style="color:teal;">string</span>,&nbsp;req&nbsp;:&nbsp;<span style="color:teal;">IDealRequestDtr</span>)&nbsp;:&nbsp;<span style="color:teal;">IHttpActionResult</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;portalId&nbsp;req&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;(resp&nbsp;:&nbsp;<span style="color:teal;">IDealResponseDtr</span>)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;resp&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;_ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">IntegrationFailure</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.<span style="color:navy;">InternalServerError</span>&nbsp;(<span style="color:teal;">InvalidOperationException</span>&nbsp;msg)&nbsp;:&gt;&nbsp;_</pre> </p> <p> This Controller's <code>Post</code> method passes <em>both</em> <code>portalId</code> and <code>req</code> to <code>imp</code>. In order to perform data flow verification of that implementation, the test has to look like this: </p> <p> <pre>[&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Post&nbsp;returns&nbsp;correct&nbsp;result&nbsp;on&nbsp;success``</span>&nbsp;portalId&nbsp;req&nbsp;resp&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;pid&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pid&nbsp;=!&nbsp;portalId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate&nbsp;=!&nbsp;req &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">succeed</span>&nbsp;resp &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:teal;">IdealController</span>&nbsp;(<span style="color:navy;">imp</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut.<span style="color:navy;">Post</span>&nbsp;(portalId,&nbsp;req) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">convertsTo</span><span style="background:yellow;">&lt;</span><span style="background:yellow;">Results</span><span style="background:yellow;">.</span><span style="color:teal;background:yellow;">OkNegotiatedContentResult</span><span style="background:yellow;">&lt;</span><span style="color:teal;background:yellow;">IDealResponseDtr</span><span style="background:yellow;">&gt;&gt;</span><span style="background:yellow;"></span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">map</span><span style="background:yellow;">&nbsp;(</span><span style="color:blue;background:yellow;">fun</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">&nbsp;</span><span style="color:blue;background:yellow;">-&gt;</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">.</span><span style="background:yellow;">Content</span><span style="background:yellow;">)</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">exists</span><span style="background:yellow;">&nbsp;((</span><span style="background:yellow;">=</span><span style="background:yellow;">)&nbsp;</span><span style="background:yellow;">resp</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> This is where I began to run out of good argument names. You need names for the <code>portalId</code> and <code>req</code> arguments of <code>imp</code>, but you can't use those names because they're already in use. You can't even shadow the names of the outer values, because the test-specific <code>imp</code> function has to close over those outer values in order to compare them to their expected values. </p> <p> While I decided to call the local <em>portal ID</em> argument <code>pid</code>, it's hardly helpful. Explicit arguments have become a burden rather than a help to the reader. If only we could get rid of those explicit arguments. </p> <h3 id="04dbe35c9ec14776a3743e40bb1b6257"> Point free <a href="#04dbe35c9ec14776a3743e40bb1b6257" title="permalink">#</a> </h3> <p> Functional programming offers a well-known alternative to explicit arguments, commonly known as <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free programming</a>. Some people find point-free style unreadable, but sometimes it can make the code more readable. Could this be the case here? </p> <p> If you look at the test-specific <code>imp</code> functions in both of the above examples with explicit arguments, you may notice that they follow a common pattern. First they invoke one or more guard assertions, and then they return a value. You can model this with a custom operator: </p> <p> <pre><span style="color:green;">//&nbsp;&#39;Guard&#39;&nbsp;composition.&nbsp;Returns&nbsp;the&nbsp;return&nbsp;value&nbsp;if&nbsp;``assert``&nbsp;doesn&#39;t&nbsp;throw.</span> <span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;unit)&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;b</span> <span style="color:blue;">let</span>&nbsp;(&gt;&gt;!)&nbsp;<span style="color:navy;">``assert``</span>&nbsp;returnValue&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">``assert``</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;returnValue</pre> </p> <p> The first argument, <code>``assert``</code>, is a function with the type <code>'a -&gt; unit</code>. This is the assertion function: it takes any value as input, and returns <code>unit</code>. The implication is that it'll throw an exception if the assertion fails. </p> <p> After invoking the assertion, the function returns the <code>returnValue</code> argument. </p> <p> The reason I designed it that way is that it's <em>composable</em>, which you'll see in a minute. The reason I named it <code>&gt;&gt;!</code> was that I wanted some kind of arrow, and I thought that the exclamation mark relates nicely to Unquote's use of exclamation marks. </p> <p> This enables you to compose the first <code>imp</code> example (for <code>PushController</code>) in point-free style: </p> <p> <pre>[&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Post&nbsp;returns&nbsp;correct&nbsp;result&nbsp;on&nbsp;validation&nbsp;failure``</span>&nbsp;req&nbsp;(<span style="color:navy;">NonNull</span>&nbsp;msg)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;=&nbsp;((=!)&nbsp;(<span style="color:navy;">snd</span>&nbsp;req))&nbsp;&gt;&gt;!&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">fail</span>&nbsp;(<span style="color:navy;">ValidationFailure</span>&nbsp;msg) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:teal;">PushController</span>&nbsp;(<span style="color:navy;">imp</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut.<span style="color:navy;">Post</span>&nbsp;req &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">convertsTo</span><span style="background:yellow;">&lt;</span><span style="background:yellow;">Results</span><span style="background:yellow;">.</span><span style="color:teal;background:yellow;">BadRequestErrorMessageResult</span><span style="background:yellow;">&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">map</span><span style="background:yellow;">&nbsp;(</span><span style="color:blue;background:yellow;">fun</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">&nbsp;</span><span style="color:blue;background:yellow;">-&gt;</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">.</span><span style="background:yellow;">Message</span><span style="background:yellow;">)</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">exists</span><span style="background:yellow;">&nbsp;((</span><span style="background:yellow;">=</span><span style="background:yellow;">)&nbsp;</span><span style="background:yellow;">msg</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> At first glance, most people would be likely to consider this to be <em>less</em> readable than before, and clearly, that's a valid standpoint. On the other hand, once you get used to identify the <code>&gt;&gt;!</code> operator, this becomes a concise shorthand. A data-flow-verifying <code>imp</code> mock function is composed of an assertion on the left-hand side of <code>&gt;&gt;!</code>, and a return value on the right-hand side. </p> <p> Most importantly, those hard-to-name arguments are gone. </p> <p> Still, let's dissect the expression <code>((=!) (snd req)) &gt;&gt;! Result.fail (ValidationFailure msg)</code>. </p> <p> The expression on the left-hand side of the <code>&gt;&gt;!</code> operator is the assertion. It uses Unquote's <em>must equal</em> <code>=!</code> operator as a function. (In F#, infix operators are functions, and you can use them as functions by surrounding them by brackets.) While you can write an assertion as <code>candidate =! snd req</code> using infix notation, you can also write the same expression as a function call: <code>(=!) (snd req) candidate</code>. Since this is a function, it can be partially applied: <code>(=!) (snd req)</code>; the type of that expression is <code>PushRequestDtr -&gt; unit</code>, which matches the required type <code>'a -&gt; unit</code> that <code>&gt;&gt;!</code> expects from its <code>``assert``</code> argument. That explains the left-hand side of the <code>&gt;&gt;!</code> operator. </p> <p> The right-hand side is easier, because that's the return value of the composed function. In this case the value is <code>Result.fail (ValidationFailure msg)</code>. </p> <p> You already know that the type of <code>&gt;&gt;!</code> is <code>('a -&gt; unit) -&gt; 'b -&gt; 'a -&gt; 'b</code>. Replacing the generic type arguments with the actual types in use, <code>'a</code> is <code>PushRequestDtr</code> and <code>'b</code> is <code>Result&lt;'a ,BoundaryFailure&gt;</code>, so the type of <code>imp</code> is <code>PushRequestDtr -&gt; Result&lt;'a ,BoundaryFailure&gt;</code>. When you set <code>'a</code> to <code>unit</code>, this fits the required type of <code>PushRequestDtr -&gt; Result&lt;unit, BoundaryFailure&gt;</code>. </p> <p> This works because in its current incarnation, the <code>imp</code> function for <code>PushController</code> only takes a single value as input. Will this also work for <code>IdealController</code>, which passes both <code>portalId</code> and <code>req</code> to its <code>imp</code> function? </p> <h3 id="1d00ff20f155452c9bb766a0c15fcec5"> Currying <a href="#1d00ff20f155452c9bb766a0c15fcec5" title="permalink">#</a> </h3> <p> The <code>imp</code> function for <code>IdealController</code> has the type <code>string -&gt; IDealRequestDtr -&gt; Result&lt;IDealResponseDtr, BoundaryFailure&gt;</code>. Notice that it takes two arguments instead of one. Is it possible to compose an <code>imp</code> function with the <code>&gt;&gt;!</code> operator? </p> <p> Consider the above example that exercises the success case for <code>IdealController</code>. What if, instead of writing </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;pid&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;pid&nbsp;=!&nbsp;portalId &nbsp;&nbsp;&nbsp;&nbsp;candidate&nbsp;=!&nbsp;req &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">succeed</span>&nbsp;resp</pre> </p> <p> you write the following? </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;=&nbsp;((=!)&nbsp;req)&nbsp;&gt;&gt;!&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">succeed</span>&nbsp;resp </pre> </p> <p> Unfortunately, that does work, because the type of that function is <code>string * IDealRequestDtr -&gt; Result&lt;IDealResponseDtr, 'a&gt;</code>, and not <code>string -&gt; IDealRequestDtr -&gt; Result&lt;IDealResponseDtr, BoundaryFailure&gt;</code>, as it should be. It's almost there, but the input values are tupled, instead of curried. </p> <p> You can easily correct that with a standard <a href="https://gist.github.com/ploeh/6d8050e121a5175fabb1d08ef5266cd7">curry function</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;=&nbsp;((=!)&nbsp;req)&nbsp;&gt;&gt;!&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">succeed</span>&nbsp;resp&nbsp;|&gt;&nbsp;<span style="color:teal;">Tuple2</span>.<span style="color:navy;">curry</span> </pre> </p> <p> The <code>Tuple2.curry</code> function takes as input a function that has tupled arguments, and turns it into a curried function. Exactly what we need here! </p> <p> The entire test is now: </p> <p> <pre>[&lt;<span style="color:teal;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Post&nbsp;returns&nbsp;correct&nbsp;result&nbsp;on&nbsp;success``</span>&nbsp;req&nbsp;resp&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;=&nbsp;((=!)&nbsp;req)&nbsp;&gt;&gt;!&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">succeed</span>&nbsp;resp&nbsp;|&gt;&nbsp;<span style="color:teal;">Tuple2</span>.<span style="color:navy;">curry</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:teal;">IdealController</span>&nbsp;(<span style="color:navy;">imp</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;sut.<span style="color:navy;">Post</span>&nbsp;req &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">convertsTo</span><span style="background:yellow;">&lt;</span><span style="background:yellow;">Results</span><span style="background:yellow;">.</span><span style="color:teal;background:yellow;">OkNegotiatedContentResult</span><span style="background:yellow;">&lt;</span><span style="color:teal;background:yellow;">IDealResponseDtr</span><span style="background:yellow;">&gt;&gt;</span><span style="background:yellow;"></span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">map</span><span style="background:yellow;">&nbsp;(</span><span style="color:blue;background:yellow;">fun</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">&nbsp;</span><span style="color:blue;background:yellow;">-&gt;</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">r</span><span style="background:yellow;">.</span><span style="background:yellow;">Content</span><span style="background:yellow;">)</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">exists</span><span style="background:yellow;">&nbsp;((</span><span style="background:yellow;">=</span><span style="background:yellow;">)&nbsp;</span><span style="background:yellow;">resp</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> Whether or not you find this more readable than the previous example is, as always, subjective, but I like it because it's a succinct, composable way to address data flow verification. Once you get over the initial shock of partially applying Unquote's <code>=!</code> operator, as well as the cryptic-looking <code>&gt;&gt;!</code> operator, you may begin to realise that the same idiom is repeated throughout. In fact, it's more than an idiom. It's an implementation of a design pattern. </p> <h3 id="bcb78c933ed94044ab47faf59ba2f058"> Mocks <a href="#bcb78c933ed94044ab47faf59ba2f058" title="permalink">#</a> </h3> <p> When talking about unit testing, I prefer the vocabulary of <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a>, because of its unrivalled consistent terminology. Using Gerard Meszaros' nomenclature, a <a href="http://martinfowler.com/bliki/TestDouble.html">Test Double</a> with built-in verification of interaction is called a <em>Mock</em>. </p> <p> Most people (including me) dislike Mocks because they tend to lead to brittle unit tests. They <em>tend</em> to, but <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">sometimes you need them</a>. Mocks are useful when you care about side-effects. </p> <p> Functional programming emphasises <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>, which, by definition, are free of side-effects. <a href="/2015/05/07/functional-design-is-intrinsically-testable">In pure functional programming, you don't need Mocks</a>. </p> <p> Since F# is a multi-paradigmatic language, you sometimes have to write code in a more object-oriented style. In the example you've seen here, I've shown you how to unit test that Controllers correctly work as Adapters over (impure) functions. Here, Mocks are useful, even if they have no place in the rest of the code base. </p> <p> Being able to express a Mock with a couple of minimal functions is, in my opinion, preferable to adding a big dependency to a 'mocking library'. </p> <h3 id="2a58a5491f4842d8b4fec0dab5f736b4"> Concluding remarks <a href="#2a58a5491f4842d8b4fec0dab5f736b4" title="permalink">#</a> </h3> <p> Sometimes, explicit values and arguments are in the way. By their presence, they force you to name them. Often, naming is good, because it compels you to make tacit knowledge explicit. In rare cases, though, the important detail isn't a value, or an argument, but instead an <em>activity</em>. An example of this is when verifying data flow. While the values are obviously present, the focus ought to be on the comparison. Thus, by making the local function arguments implicit, you can direct the reader's attention to the interaction - in this case, Unquote's <code>=!</code> <em>must equal</em> comparison. </p> <p> In the introduction to this article, I told you that the code you've seen here is a real-life example. This is true. </p> <p> I submitted my refactoring to point-free style as an internal pull request on the project I'm currently working. When I did that, I was genuinely in doubt about the readability improvement this would give, so I asked my reviewers for their opinions. I was genuinely ready to accept if they wanted to reject the pull request. </p> <p> My reviewers disagreed internally, ultimately had a vote, and decided to reject the pull request. I don't blame them. We had a civil discussion about the pros and cons, and while they understood the advantages, they felt that the disadvantages weighed heavier. </p> <p> In their context, I understand why they decided to decline the change, but that doesn't mean that I don't find this an interesting experiment. I expect to use something like this in the future in some contexts, while in other contexts, I'll stick with the more verbose (and harder to name) test-specific functions with explicit arguments. </p> <p> Still, I like to solve problems using well-known compositions, which is the reason I prefer a composable, idiomatic approach over ad-hoc code. </p> <p> If you'd like to learn more about unit testing and property-based testing in F# (and C#), you can watch some of <a href="https://blog.ploeh.dk/pluralsight-courses">my Pluralsight courses</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Decoupling decisions from effects https://blog.ploeh.dk/2016/09/26/decoupling-decisions-from-effects 2016-09-26T21:51:00+00:00 Mark Seemann <div id="post"> <p> <em>Functional programming emphasises pure functions, but sometimes decisions must be made based on impure data. The solution is to decouple decisions and effects.</em> </p> <p> Functional programmers love <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. Not only do they tend to be easy to reason about, they are also <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. It'd be wonderful if we could build entire systems only from pure functions, but every functional programmer knows that the world is impure. Instead, we strive towards implementing as much of our code base as pure functions, so that an application is <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">impure only at its boundaries</a>. </p> <p> The more you can do this, the more testable the system becomes. One rule of thumb about unit testing that I often use is that if a particular candidate for unit testing has a <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of 1, it may be acceptable to skip unit testing it. Instead, we can consider such a unit a <a href="http://xunitpatterns.com/Humble%20Object.html">humble unit</a>. If you can separate decisions from <em>effects</em> (which is what functional programmers often call impurities), you can often make the impure functions humble. </p> <p> In other words: put all logic in pure functions that can be unit tested, and implement impure effects as humble functions that you don't need to unit test. </p> <p> You want to see an example. So do I! </p> <h3 id="bf682f77972e4c5ab8a08b8f565f8f68"> Example: conditional reading from file <a href="#bf682f77972e4c5ab8a08b8f565f8f68" title="permalink">#</a> </h3> <p> In a <a href="https://gist.github.com/jcansdale/de58c2b2c389851f72b00ef525be820c">recent discussion</a>, Jamie Cansdale asks how I'd design and unit test something like the following C# method if I could instead redesign it in F#. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;GetUpperText(<span style="color:blue;">string</span>&nbsp;path) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:#2b91af;">File</span>.Exists(path))&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&quot;DEFAULT&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;text&nbsp;=&nbsp;<span style="color:#2b91af;">File</span>.ReadAllText(path); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;text.ToUpperInvariant(); }</pre> </p> <p> Notice how this method contains two impure operations: <code>File.Exists</code> and <code>File.ReadAllText</code>. Decision logic seems interleaved with IO. How can decisions be separated from effects? </p> <p> (For good measure I ought to point out that obviously, the above example is so simple that by itself, it almost doesn't warrant testing. Think of it as a stand-in for a more complex problem.) </p> <p> With a statement-based language like C#, it can be difficult to see how to separate decision logic from effects without introducing interfaces, but with expression-based languages like F#, it becomes close to trivial. In this article, I'll show you three alternatives. </p> <p> All three alternatives, however, make use of the same function for turning text into upper case: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;string</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">getUpper</span>&nbsp;(text&nbsp;:&nbsp;<span style="color:teal;">string</span>)&nbsp;=&nbsp;text.<span style="color:navy;">ToUpperInvariant</span>&nbsp;()</pre> </p> <p> Obviously, this function is so trivial that it's hardly worth testing, but remember to think about it as a stand-in for a more complex problem. It's a pure function, so it's easy to unit test: </p> <p> <pre>[&lt;<span style="color:teal;">Theory</span>&gt;] [&lt;<span style="color:teal;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;FOO&quot;</span>)&gt;] [&lt;<span style="color:teal;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;BAR&quot;</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``getUpper&nbsp;returns&nbsp;correct&nbsp;value``</span>&nbsp;input&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">getUpper</span>&nbsp;input &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> This test uses <a href="https://xunit.net">xUnit.net 2.1.0</a> and <a href="http://www.swensensoftware.com/unquote">Unquote 3.1.2</a>. The <code>=!</code> operator is a custom Unquote operator; you can read it as <em>must equal</em>; that is: <em>expected must equal actual</em>. It'll throw an exception if this isn't the case. </p> <h3 id="37b8066d2752429392cc8bc7845c59d3"> Custom unions <a href="#37b8066d2752429392cc8bc7845c59d3" title="permalink">#</a> </h3> <p> Languages like F# come with <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>, which means that in addition to complex structures, they also enable you to express types as <em>alternatives</em>. This means that you can represent a decision as one or more alternative <em>pure</em> values. </p> <p> Although the examples you'll see later in this article are simpler, I think it'll be helpful to start with an ad hoc solution to the problem. Here, the decision is to either read from a file, or return a default value. You can express that using a custom discriminated union: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Action</span>&nbsp;=&nbsp;<span style="color:navy;">ReadFromFile</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span>&nbsp;|&nbsp;<span style="color:navy;">UseDefault</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:teal;">string</span> </pre> </p> <p> This type models two mutually exclusive cases: either you decide to read from the file identified by a file path (<code>string</code>), or your return a default value (also modelled as a <code>string</code>). </p> <p> Using this <code>Action</code> type, you can write a <em>pure</em> function that makes the decision: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;bool&nbsp;-&gt;&nbsp;Action</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">decide</span>&nbsp;path&nbsp;fileExists&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;fileExists &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">ReadFromFile</span>&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">UseDefault</span>&nbsp;<span style="color:#a31515;">&quot;DEFAULT&quot;</span></pre> </p> <p> This function takes two arguments: <code>path</code> (a <code>string</code>) and <code>fileExists</code> (a <code>bool</code>). If <code>fileExists</code> is <code>true</code>, it returns the <code>ReadFromFile</code> case; otherwise, it returns the <code>UseDefault</code> case. </p> <p> Notice how this function neither checks whether the file exists, nor does it attempt to read the contents of the file. It only makes a decision based on input, and returns information about this decision as output. This function is <em>pure</em>, so (as I've claimed numerous times) is easy to unit test: </p> <p> <pre>[&lt;<span style="color:teal;">Theory</span>&gt;] [&lt;<span style="color:teal;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh.txt&quot;</span>)&gt;] [&lt;<span style="color:teal;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah.txt&quot;</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``decide&nbsp;returns&nbsp;correct&nbsp;result&nbsp;when&nbsp;file&nbsp;exists``</span>&nbsp;path&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">decide</span>&nbsp;path&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">ReadFromFile</span>&nbsp;path&nbsp;=!&nbsp;actual [&lt;<span style="color:teal;">Theory</span>&gt;] [&lt;<span style="color:teal;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh.txt&quot;</span>)&gt;] [&lt;<span style="color:teal;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah.txt&quot;</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``decide&nbsp;returns&nbsp;correct&nbsp;result&nbsp;when&nbsp;file&nbsp;doesn&#39;t&nbsp;exist``</span>&nbsp;path&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">decide</span>&nbsp;path&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">UseDefault</span>&nbsp;<span style="color:#a31515;">&quot;DEFAULT&quot;</span>&nbsp;=!&nbsp;actual</pre> </p> <p> One unit test function exercises the code path where the file exists, whereas the other test exercises the code path where it doesn't. Straightforward. </p> <p> There's still some remaining work, because you need to somehow compose your pure functions with <code>File.Exists</code> and <code>File.ReadAllText</code>. You also need a way to extract the string value from the two cases of <code>Action</code>. One way to do that is to introduce another pure function: </p> <p> <pre><span style="color:green;">//&nbsp;(string&nbsp;-&gt;&nbsp;string)&nbsp;-&gt;&nbsp;Action&nbsp;-&gt;&nbsp;string</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">getValue</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">ReadFromFile</span>&nbsp;path&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">UseDefault</span>&nbsp;value&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;value</pre> </p> <p> This is a function that returns the <code>UseDefault</code> data for that case, but invokes a function <code>f</code> in the <code>ReadFromFile</code> case. Again, since this function is pure it's easy to unit test it, but I'll leave that as an exercise. </p> <p> You now have all the building blocks required to compose a function similar to the above <code>GetUpperText</code> C# method: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;string</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">getUpperText</span>&nbsp;path&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">File</span>.<span style="color:navy;">Exists</span> &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">decide</span>&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">getValue</span>&nbsp;(<span style="color:teal;">File</span>.<span style="color:navy;">ReadAllText</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">getUpper</span>)</pre> </p> <p> This implementation pipes <code>path</code> into <code>File.Exists</code>, which returns a Boolean value indicating whether the file exists. This Boolean value is then piped into <code>decide path</code>, which (as you may recall) returns an <code>Action</code>. That value is finally piped into <code>getValue (File.ReadAllText &gt;&gt; getUpper)</code>. Recall that <code>getValue</code> will only invoke the function argument when the <code>Action</code> is <code>ReadFromFile</code>, so <code>File.ReadAllText &gt;&gt; getUpper</code> is only executed in this case. </p> <p> Notice how decisions and effectful functions are interleaved. All the decision functions are covered by unit tests; only <code>File.Exists</code> and <code>File.ReadAllText</code> aren't covered, but I find it reasonable to treat these as humble functions. </p> <h3 id="bc6db60810a9437f97e80305989263e3"> Either <a href="#bc6db60810a9437f97e80305989263e3" title="permalink">#</a> </h3> <p> Normally, decisions often involve a choice between two alternatives. In the above example, you saw how the alternatives were named <code>ReadFromFile</code> and <code>UseDefault</code>. Since a choice between two alternatives is so common, there's a well-known 'pattern' that gives you general-purpose tools to model decisions. This is known as the <em>Either</em> monad. </p> <p> The F# core library doesn't (<a href="https://github.com/fsharp/FSharpLangDesign/blob/master/RFCs/FS-1004-result-type.md">yet</a>) come with an implementation of the Either monad, but it's easy to add. In this example, I'm using code from <a href="https://fsharpforfunandprofit.com">Scott Wlaschin's</a> <a href="https://fsharpforfunandprofit.com/posts/recipe-part2">railway-oriented programming</a>, although slightly modified, and including only the most essential building blocks for the example: </p> <p> <pre> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Result<'TSuccess, 'TFailure></span>&nbsp;= | Success <span style="color:blue;">of</span> 'TSuccess | Failure <span style="color:blue;">of</span> 'TFailure <span style="color:blue;">module</span>&nbsp;<span style="color:teal;">Result</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Result&lt;&#39;b,&nbsp;&#39;c&gt;)&nbsp;-&gt;&nbsp;Result&lt;&#39;a,&nbsp;&#39;c&gt;&nbsp;-&gt;&nbsp;Result&lt;&#39;b,&nbsp;&#39;c&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;succ&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;succ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;fail&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;fail &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Result&lt;&#39;a,&nbsp;&#39;c&gt;&nbsp;-&gt;&nbsp;Result&lt;&#39;b,&nbsp;&#39;c&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;succ&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;succ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;fail&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;fail &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;bool)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;Result&lt;&#39;a,&nbsp;&#39;a&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">split</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;x&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;(&#39;c&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Result&lt;&#39;a,&nbsp;&#39;c&gt;&nbsp;-&gt;&nbsp;&#39;b</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">either</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:navy;">g</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;succ&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;succ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;fail&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">g</span>&nbsp;fail</pre> </p> <p> In fact, the <code>bind</code> and <code>map</code> functions aren't even required for this particular example, but I included them anyway, because otherwise, readers already familiar with the Either monad would wonder why they weren't there. </p> <p> All these functions are generic and pure, so they are easy to unit test. I'm not going to show you the unit tests, however, as I consider the functions belonging to that <code>Result</code> module as reusable functions. This is a module that would ship as part of a well-tested library. In fact, <a href="https://github.com/fsharp/FSharpLangDesign/blob/master/RFCs/FS-1004-result-type.md">it'll soon be added to the F# core library</a>. </p> <p> With the already tested <code>getUpper</code> function, you now have all the building blocks required to implement the desired functionality: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;string</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">getUpperText</span>&nbsp;path&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">split</span>&nbsp;<span style="color:teal;">File</span>.<span style="color:navy;">Exists</span> &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Result</span>.<span style="color:navy;">either</span>&nbsp;(<span style="color:teal;">File</span>.<span style="color:navy;">ReadAllText</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">getUpper</span>)&nbsp;(<span style="color:blue;">fun</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;DEFAULT&quot;</span>)</pre> </p> <p> This composition pipes <code>path</code> into <code>Result.split</code>, which uses <code>File.Exists</code> as a predicate to decide whether the path should be packaged into a <code>Success</code> or <code>Failure</code> case. The resulting <code>Result&lt;string, string&gt;</code> is then piped into <code>Result.either</code>, which invokes <code>File.ReadAllText &gt;&gt; getUpper</code> in the <code>Success</code> case, and the anonymous function in the <code>Failure</code> case. </p> <p> Notice how, once again, the impure functions <code>File.Exists</code> and <code>File.ReadAllText</code> are used as humble functions, but interleaved with testable, pure functions that make all the decisions. </p> <h3 id="87fdc87c015a49159c6b22616bc4cf89"> Maybe <a href="#87fdc87c015a49159c6b22616bc4cf89" title="permalink">#</a> </h3> <p> Sometimes, a decision isn't so much between two alternatives as it's a decision between something that may exist, but also may not. You can model this with the <em>Maybe</em> monad, which in F# comes in the form of the built-in <code>option</code> type. </p> <p> In fact, so much is already built in (and tested by the F# development team) that you almost don't need to add anything yourself. The only function you could consider adding is this: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:teal;">Option</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;a&nbsp;option&nbsp;-&gt;&nbsp;&#39;a</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">defaultIfNone</span>&nbsp;def&nbsp;x&nbsp;=&nbsp;<span style="color:navy;">defaultArg</span>&nbsp;x&nbsp;def</pre> </p> <p> As you can see, this function simply swaps the arguments for the built-in <code>defaultArg</code> function. This is done to make it more pipe-friendly. This function <a href="https://github.com/fsharp/FSharpLangDesign/blob/master/RFCs/FS-1007-additional-Option-module-functions.md">will most likely be added in a future version of F#</a>. </p> <p> That's all you need: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;string</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">getUpperText</span>&nbsp;path&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;path &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">Some</span> &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">filter</span>&nbsp;<span style="color:teal;">File</span>.<span style="color:navy;">Exists</span> &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:teal;">File</span>.<span style="color:navy;">ReadAllText</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">getUpper</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">defaultIfNone</span>&nbsp;<span style="color:#a31515;">&quot;DEFAULT&quot;</span></pre> </p> <p> This composition starts with the <code>path</code>, puts it into a <code>Some</code> case, and pipes that <code>option</code> value into <code>Option.filter File.Exists</code>. This means that the <code>Some</code> case will only stay a <code>Some</code> value if the file exists; otherwise, it will be converted to a <code>None</code> value. Whatever the <code>option</code> value is, it's then piped into <code>Option.map (File.ReadAllText &gt;&gt; getUpper)</code>. The composed function <code>File.ReadAllText &gt;&gt; getUpper</code> is only executed in the <code>Some</code> case, so if the file doesn't exist, the function will not attempt to read it. Finally, the <code>option</code> value is piped into <code>Option.defaultIfNone</code>, which returns the mapped value, or <code>"DEFAULT"</code> if the value was <code>None</code>. </p> <p> Like in the two previous examples, the decision logic is implemented by pure functions, whereas the impure functions <code>File.Exists</code> and <code>File.ReadAllText</code> are handled as humble functions. </p> <h3 id="26a726428ff14591a2e9bf9db9ebc4d6"> Summary <a href="#26a726428ff14591a2e9bf9db9ebc4d6" title="permalink">#</a> </h3> <p> Have you noticed a pattern in all the three examples? Decisions are separated from effects using discriminated unions (both the above <code>Action</code>, <code>Result&lt;'TSuccess, 'TFailure&gt;</code>, and the built-in <code>option</code> are discriminated unions). In my experience, as long as you need to decide between two alternatives, the Either or Maybe monads are often sufficient to decouple decision logic from effects. Often, I don't even need to write any tests, because I compose my functions from the known, well-tested functions that belong to the respective monads. </p> <p> If your decision has to branch between three or more alternatives, you can consider a custom discriminated union. For this particular example, though, I think I prefer the third, Maybe-based composition, but closely followed by the Either-based composition. </p> <p> In this article, you saw three examples of how to decouple decision from effects; and I didn't even show you the <em>Free</em> monad! </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="8b6fe71718224024bec35ffb32816668"> <div class="comment-author"><a href="https://github.com/blainne">Grzegorz Sławecki</a> <a href="#8b6fe71718224024bec35ffb32816668">#</a></div> <div class="comment-content"> <p> Mark, </p> <p> I can't understand how can the <code>getValue</code> function be pure. While I agree that it's easy to test, it's still the higher order function and it's purity depends on the purity of function passed as the argument. Even in Your example it takes <code>File.ReadAllText&nbsp;&gt;&gt;&nbsp;getUpper</code> which actually reaches to a file on the disk and I perceive it as reaching to an external shared state. Is there something I misunderstood? </p> </div> <div class="comment-date">2016-10-14 09:06 UTC</div> </div> <div class="comment" id="f39c03655c0a4470a3697a50d0a07904"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f39c03655c0a4470a3697a50d0a07904">#</a></div> <div class="comment-content"> <p> Grzegorz, thank you for writing. You make a good point, and in a sense you're correct. F# doesn't enforce purity, and this is both an advantage and a disadvantage. It's an advantage because it makes it easier for programmers migrating from C# to make a gradual transition to a more functional programming style. It's also an advantage exactly because it relies on the programmer's often-faulty reasoning to ensure that code is properly functional. </p> <p> Functions in F# are only pure if they're implemented to be pure. For any given function type (signature) you can always create an impure function that fits the type. (If nothing else, you can always write "Hello, world!" to the console, before returning a value.) </p> <p> The result of this is that few parts of F# are pure in the sense that you imply. Even <code>List.map</code> could be impure, if passed an impure function. In other words, higher-order functions in F# are only pure if composed of exclusively pure parts. </p> <p> Clearly, this is in stark contrast to Haskell, where purity is enforced at the type level. In Haskell, a throw-away, poorly designed mini-API like the <code>Action</code> type and associated functions shown here wouldn't even compile. The Either and Maybe examples, on the other hand, would. </p> <p> My assumption here is that function composition happens at the edge of the application - that is, in an impure (<code>IO</code>) context. </p> </div> <div class="comment-date">2016-10-15 09:02 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Untyped F# HTTP route defaults for ASP.NET Web API https://blog.ploeh.dk/2016/08/09/untyped-f-http-route-defaults-for-aspnet-web-api 2016-08-09T04:24:00+00:00 Mark Seemann <div id="post"> <p> <em>In ASP.NET Web API, route defaults can be provided by a dictionary in F#.</em> </p> <p> When you define a route in ASP.NET Web API 2, you most likely use the <a href="https://msdn.microsoft.com/en-us/library/system.web.http.httproutecollectionextensions.maphttproute">MapHttpRoute</a> overload where you have to supply default values for the route template: </p> <p> <pre><span style="color:blue;">public static</span> IHttpRoute MapHttpRoute( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span> HttpRouteCollection routes, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span> name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span> routeTemplate, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span> defaults)</pre> </p> <p> The <code>defaults</code> arguments has the type <code>object</code>, but while the compiler will allow you to put any value here, the implicit <em>intent</em> is that in C#, you should pass an anonymous object with the route defaults. A standard route looks like this: </p> <p> <pre>configuration.Routes.MapHttpRoute( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;DefaultAPI&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;{controller}/{id}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;Controller&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Home&quot;</span>,&nbsp;Id&nbsp;=&nbsp;<span style="color:#2b91af;">RouteParameter</span>.Optional&nbsp;});</pre> </p> <p> Notice how the name of the properties (<code>Controller</code> and <code>Id</code>) (case-insensitively) match the place-holders in the route template (<code>{controller}</code> and <code>{id}</code>). </p> <p> While it's not clear from the type of the argument that this is what you're supposed to do, once you've learned it, it's easy enough to do, and rarely causes problems in C#. </p> <h3 id="3d834547f6ad45ef8ee7f60a1451267e"> Flexibility <a href="#3d834547f6ad45ef8ee7f60a1451267e" title="permalink">#</a> </h3> <p> You can debate the soundness of this API design, but as far as I can tell, it attempts to strike a balance between flexibility and syntax easy on the eyes. It does, for example, enable you to define a list of routes like this: </p> <p> <pre>configuration.Routes.MapHttpRoute( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;AvailabilityYear&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;availability/{year}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;Controller&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Availability&quot;</span>&nbsp;}); configuration.Routes.MapHttpRoute( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;AvailabilityMonth&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;availability/{year}/{month}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;Controller&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Availability&quot;</span>&nbsp;}); configuration.Routes.MapHttpRoute( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;AvailabilityDay&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;availability/{year}/{month}/{day}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;Controller&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Availability&quot;</span>&nbsp;}); configuration.Routes.MapHttpRoute( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;DefaultAPI&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;{controller}/{id}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;{&nbsp;Controller&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Home&quot;</span>,&nbsp;Id&nbsp;=&nbsp;<span style="color:#2b91af;">RouteParameter</span>.Optional&nbsp;});</pre> </p> <p> In this example, there are three alternative routes to an <em>availability</em> resource, keyed on either an entire year, a month, or a single date. Since the route <em>templates</em> (e.g. <code>availability/{year}/{month}</code>) don't specify an <code>id</code> place-holder, there's no reason to provide a default value for it. On the other hand, it would have been possible to define defaults for the custom place-holders <code>year</code>, <code>month</code>, or <code>day</code>, if you had so desired. In this example, however, there are no defaults for these place-holders, so if values aren't provided, none of the <em>availability</em> routes are matched, and the request falls through to the <code>DefaultAPI</code> route. </p> <p> Since you can supply an anonymous object in C#, you can give it any property you'd like, and the code will still compile. There's no type safety involved, but using an anonymous object enables you to use a compact syntax. </p> <h3 id="2e3685beea5743459fb0f73580025651"> Route defaults in F# <a href="#2e3685beea5743459fb0f73580025651" title="permalink">#</a> </h3> <p> The API design of the MapHttpRoute method seems forged with C# in mind. I don't know how it works in Visual Basic .NET, but in F# there are no anonymous objects. How do you supply route defaults, then? </p> <p> As I described in my article on <a href="/2013/08/23/how-to-create-a-pure-f-aspnet-web-api-project">creating an F# Web API project</a>, you can define a record type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">HttpRouteDefaults</span>&nbsp;=&nbsp;{&nbsp;Controller&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>;&nbsp;Id&nbsp;:&nbsp;<span style="color:#4ec9b0;">obj</span>&nbsp;} </pre> </p> <p> You can use it like this: </p> <p> <pre><span style="color:#4ec9b0;">GlobalConfiguration</span>.Configuration.Routes.<span style="color:navy;">MapHttpRoute</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;DefaultAPI&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;{controller}/{id}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Controller&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Home&quot;</span>;&nbsp;Id&nbsp;=&nbsp;<span style="color:#4ec9b0;">RouteParameter</span>.Optional&nbsp;})&nbsp;|&gt;&nbsp;<span style="color:navy;">ignore</span></pre> </p> <p> That works fine for <code>DefaultAPI</code>, but it's hardly flexible. You <em>must</em> supply both a <code>Controller</code> and a <code>Id</code> value. If you need to define routes like the <em>availability</em> routes above, you can't use this HttpRouteDefaults type, because you can't omit the <code>Id</code> value. </p> <p> While defining <em>another</em> record type is only a one-liner, you're confronted with the problem of naming these types. </p> <p> In C#, the use of anonymous objects is, despite appearances, an untyped approach. Could something similar be possible with F#? </p> <p> It turns out that the MapHttpRoute also works if you pass it an <code>IDictionary&lt;string, object&gt;</code>, which is possible in F#: </p> <p> <pre>config.Routes.MapHttpRoute( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;DefaultAPI&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;{controller}/{id}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;dict&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#a31515;">&quot;Controller&quot;</span>,&nbsp;box&nbsp;<span style="color:#a31515;">&quot;Home&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#a31515;">&quot;Id&quot;</span>,&nbsp;box&nbsp;RouteParameter.Optional)])&nbsp;|&gt;&nbsp;<span style="color:navy;">ignore</span></pre> </p> <p> While this looks more verbose than the previous alternative, it's more flexible. It's also <a href="http://c2.com/cgi/wiki?StringlyTyped">stringly typed</a>, which normally isn't an endorsement, but in this case is <em>honest</em>, because it's as strongly typed as the MapHttpRoute method. <a href="https://www.python.org/dev/peps/pep-0020">Explicit is better than implicit</a>. </p> <p> The complete route configuration corresponding to the above example would look like this: </p> <p> <pre>config.Routes.<span style="color:navy;">MapHttpRoute</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;AvailabilityYear&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;availability/{year}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">dict</span>&nbsp;[(<span style="color:#a31515;">&quot;Controller&quot;</span>,&nbsp;<span style="color:navy;">box</span>&nbsp;<span style="color:#a31515;">&quot;Availability&quot;</span>)])&nbsp;|&gt;&nbsp;<span style="color:navy;">ignore</span> config.Routes.<span style="color:navy;">MapHttpRoute</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;AvailabilityMonth&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;availability/{year}/{month}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">dict</span>&nbsp;[(<span style="color:#a31515;">&quot;Controller&quot;</span>,&nbsp;<span style="color:navy;">box</span>&nbsp;<span style="color:#a31515;">&quot;Availability&quot;</span>)])&nbsp;|&gt;&nbsp;<span style="color:navy;">ignore</span> config.Routes.<span style="color:navy;">MapHttpRoute</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;AvailabilityDay&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;availability/{year}/{month}/{day}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">dict</span>&nbsp;[(<span style="color:#a31515;">&quot;Controller&quot;</span>,&nbsp;<span style="color:navy;">box</span>&nbsp;<span style="color:#a31515;">&quot;Availability&quot;</span>)])&nbsp;|&gt;&nbsp;<span style="color:navy;">ignore</span> config.Routes.<span style="color:navy;">MapHttpRoute</span>( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;DefaultAPI&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;{controller}/{id}&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">dict</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#a31515;">&quot;Controller&quot;</span>,&nbsp;<span style="color:navy;">box</span>&nbsp;<span style="color:#a31515;">&quot;Home&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#a31515;">&quot;Id&quot;</span>,&nbsp;<span style="color:navy;">box</span>&nbsp;<span style="color:teal;">RouteParameter</span>.Optional)])&nbsp;|&gt;&nbsp;<span style="color:navy;">ignore</span></pre> </p> <p> If you're interested in learning more about developing ASP.NET Web API services in F#, watch my Pluralsight course <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">A Functional Architecture with F#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Conditional composition of functions https://blog.ploeh.dk/2016/07/04/conditional-composition-of-functions 2016-07-04T06:53:00+00:00 Mark Seemann <div id="post"> <p> <em>A major benefit of Functional Programming is the separation of decisions and (side-)effects. Sometimes, however, one decision ought to trigger an impure operation, and then proceed to make more decisions. Using functional composition, you can succinctly conditionally compose functions.</em> </p> <p> In my <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">article on how Functional Architecture falls into the Ports and Adapters pit of success</a>, I describe how Haskell forces you to separate concerns: <ul> <li> Your Domain Model should be <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, with business decisions implemented by pure functions. Not only does it make it easier for you to reason about the business logic, it also has the side-benefit that <a href="/2015/05/07/functional-design-is-intrinsically-testable">pure functions are intrinsically testable</a>.</li> <li> Side-effects, and other impure operations (such as database queries) can be isolated and implemented as <a href="http://xunitpatterns.com/Humble%20Object.html">humble</a> functions. </li> </ul> A common pattern that you can often use is: <ol> <li>Read some data using an impure query.</li> <li>Pass that data to a pure function.</li> <li>Use the return value from the pure function to perform various side-effects. You could, for example, write data to a database, send an email, or update a user interface.</li> </ol> Sometimes, however, things are more complex. Based on the answer from one pure function, you may need to query an additional data source to gather extra data, call a second pure function, and so on. In this article, you'll see one way to accomplish this. </p> <h3 id="88807671a2e24398959b57a712cff5b1"> Caravans for extra space <a href="#88807671a2e24398959b57a712cff5b1" title="permalink">#</a> </h3> <p> Based on my <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">previous restaurant-booking example</a>, <a href="https://github.com/MartinRykfors">Martin Rykfors</a> suggests "a new feature request. The restaurant has struck a deal with the local caravan dealership, allowing them to rent a caravan to park outside the restaurant in order to increase the seating capacity for one evening. Of course, sometimes there are no caravans available, so we'll need to query the caravan database to see if there is a big enough caravan available that evening:" </p> <p> <pre>findCaravan :: ServiceAddress -&gt; Int -&gt; ZonedTime -&gt; IO (Maybe Caravan)</pre> </p> <p> The above <code>findCaravan</code> is a slight modification of the function Martin suggests, because I imagine that the caravan dealership exposes their caravan booking system as a web service, so the function needs a service address as well. This change doesn't impact the proposed solution, though. </p> <p> This problem definition fits the above general problem statement: you'd only want to call the <code>findCaravan</code> function if <code>checkCapacity</code> returns <code>Left CapacityExceeded</code>. </p> <p> That's still a business decision, so you ought to implement it as a pure function. If you (for a moment) imagine that you have a <code>Maybe Caravan</code> instead of an <code>IO (Maybe Caravan)</code>, you have all the information required to make that decision: </p> <p> <pre>checkCaravanCapacityOnError :: Error -&gt; Maybe Caravan -&gt; Reservation -&gt; Either Error Reservation checkCaravanCapacityOnError CapacityExceeded (Just caravan) reservation = if caravanCapacity caravan &lt; quantity reservation then Left CapacityExceeded else Right reservation checkCaravanCapacityOnError err _ _ = Left err</pre> </p> <p> Notice that this function not only takes <code>Maybe Caravan</code>, it also takes an <code>Error</code> value. This encodes into the function's type that you should only call it if you have an <code>Error</code> that originates from a previous step. This <code>Error</code> value also enables the function to only check the caravan's capacity if the previous <code>Error</code> was a <code>CapacityExceeded</code>. <code>Error</code> can also be <code>ValidationError</code>, in which case there's no reason to check the caravan's capacity. </p> <p> This takes care of the Domain Model, but you still need to figure out how to get a <code>Maybe Caravan</code> value. Additionally, if <code>checkCaravanCapacityOnError</code> returns <code>Right Reservation</code>, you'd probably want to reserve the caravan for the evening. You can imagine that this is possible with the following function: </p> <p> <pre>reserveCaravan :: ServiceAddress -&gt; ZonedTime -&gt; Caravan -&gt; IO ()</pre> </p> <p> This function reserves the caravan at the supplied time. In order to keep the example simple, you can imagine that the provided <code>ZonedTime</code> indicates an entire day (or evening), and not just an instant. </p> <h3 id="d2b21349b1a449709878d7a874d1c0b1"> Composition of caravan-checking <a href="#d2b21349b1a449709878d7a874d1c0b1" title="permalink">#</a> </h3> <p> As a first step, you can compose an impure function that <ol> <li>Queries the caravan dealership for a caravan</li> <li>Calls the pure <code>checkCaravanCapacityOnError</code> function</li> <li>Reserves the caravan if the return value was <code>Right Reservation</code></li> </ol> Notice how these steps follow the impure-pure-impure pattern I described above. You can compose such a function like this: </p> <p> <pre>import Control.Monad (forM_) import Control.Monad.Trans (liftIO) import Control.Monad.Trans.Either (EitherT(..), hoistEither) checkCaravan :: Reservation -&gt; Error -&gt; EitherT Error IO Reservation checkCaravan reservation err = do c &lt;- liftIO $ findCaravan svcAddr (quantity reservation) (date reservation) newRes &lt;- hoistEither $ checkCaravanCapacityOnError err c reservation liftIO $ forM_ c $ reserveCaravan svcAddr (date newRes) return newRes</pre> </p> <p> It starts by calling <code>findCaravan</code> by closing over <code>svcAddr</code> (a <code>ServiceAddress</code> value). This is an impure operation, but you can use <code>liftIO</code> to make <code>c</code> a <code>Maybe Caravan</code> value that can be passed to <code>checkCaravanCapacityOnError</code> on the next line. This function returns <code>Either Error Reservation</code>, but since this function is defined in an <code>EitherT Error IO Reservation</code> context, <code>newRes</code> is a <code>Reservation</code> value. Still, it's important to realise that exactly because of this context, execution will short-circuit at that point if the return value from <code>checkCaravanCapacityOnError</code> is a <code>Left</code> value. In other words, all subsequent expression are only evaluated if <code>checkCaravanCapacityOnError</code> returns <code>Right</code>. This means that the <code>reserveCaravan</code> function is only called if a caravan with enough capacity is available. </p> <p> The <code>checkCaravan</code> function will <em>unconditionally</em> execute if called, so as the final composition step, you'll need to figure out how to compose it into the overall <code>postReservation</code> function in such a way that it's only called if <code>checkCapacity</code> returns <code>Left</code>. </p> <h3 id="53cc4c297e3f413e81e248531a37a784"> Conditional composition <a href="#53cc4c297e3f413e81e248531a37a784" title="permalink">#</a> </h3> <p> In the previous incarnation of this example, the overall entry point for the HTTP request in question was this <code>postReservation</code> function: </p> <p> <pre>postReservation :: ReservationRendition -&gt; IO (HttpResult ()) postReservation candidate = fmap toHttpResult $ runEitherT $ do r &lt;- hoistEither $ validateReservation candidate i &lt;- liftIO $ getReservedSeatsFromDB connStr $ date r hoistEither $ checkCapacity 10 i r &gt;&gt;= liftIO . saveReservation connStr</pre> </p> <p> Is it possible to compose <code>checkCaravan</code> into this function in such a way that it's only going to be executed if <code>checkCapacity</code> returns <code>Left</code>? Yes, by adding to the <code>hoistEither $ checkCapacity 10 i r</code> pipeline: </p> <p> <pre>import Control.Monad.Trans (liftIO) import Control.Monad.Trans.Either (EitherT(..), hoistEither, right, eitherT) postReservation :: ReservationRendition -&gt; IO (HttpResult ()) postReservation candidate = fmap toHttpResult $ runEitherT $ do r &lt;- hoistEither $ validateReservation candidate i &lt;- liftIO $ getReservedSeatsFromDB connStr $ date r eitherT (checkCaravan r) right $ hoistEither $ checkCapacity 10 i r &gt;&gt;= liftIO . saveReservation connStr</pre> </p> <p> Contrary to F#, you have to read Haskell pipelines from right to left. In the second-to-last line of code, you can see that I've added <code>eitherT (checkCaravan r) right</code> to the left of <code>hoistEither $ checkCapacity 10 i r</code>, which was already there. This means that, instead of binding the result of <code>hoistEither $ checkCapacity 10 i r</code> directly to the <code>saveReservation</code> composition (via the monadic <code>&gt;&gt;=</code> <em>bind</em> operator), that result is first passed to <code>eitherT (checkCaravan r) right</code>. </p> <p> The <code>eitherT</code> function composes two other functions: the leftmost function is invoked if the input is <code>Left</code>, and the right function is invoked if the input is <code>Right</code>. In this particular example, <code>(checkCaravan r)</code> is the closure being invoked in the <code>Left</code> - and only in the <code>Left</code> - case. In the <code>Right</code> case, the value is passed on unmodified, but elevated back into the <code>EitherT</code> context using the <code>right</code> function. </p> <p> (BTW, the above composition has a subtle bug: the capacity is still hard-coded as <code>10</code>, even though reserving extra caravans actually increases the overall capacity of the restaurant for the day. I'll leave it as an exercise for you to make the capacity take into account any reserved caravans. You can download <a href="https://gist.github.com/ploeh/c999e2ae2248bd44d775">all of the source code</a>, if you want to give it a try.) </p> <h3 id="0b562cd186f640fea8ab648c721d955f"> Interleaving <a href="#0b562cd186f640fea8ab648c721d955f" title="permalink">#</a> </h3> <p> Haskell has strong support for composition of functions. Not only can you interleave pure and impure code, but you can also do it <em>conditionally</em>. In the above example, the <code>eitherT</code> function holds the key to that. The overall flow of the <code>postReservation</code> function is: <ol> <li>Validate the input</li> <li>Get already reserved seats from the database</li> <li>Check the reservation request against the restaurant's remaining capacity</li> <li>If the capacity is exceeded, attempt to reserve a caravan of sufficient capacity</li> <li>If one of the previous steps decided that the restaurant has enough capacity, then save the reservation in the database</li> <li>Convert the result (whether it's <code>Left</code> or <code>Right</code>) to an HTTP response</li> </ol> Programmers who are used to implementing such solutions using C#, Java, or similar languages, may feel hesitant by delegating a branching decision to a piece of composition code, instead of something they can unit test. </p> <p> Haskell's type system is remarkably helpful here. Haskell programmers often joke that <em>if it compiles, it works</em>, and there's a morsel of truth in that sentiment. Both functions used with <code>eitherT</code> must return a value of the same type, but the left function <em>must</em> be a function that takes the <code>Left</code> type as input, whereas the right function <em>must</em> be a function that takes the <code>Right</code> type as input. In the above example, <code>(checkCaravan r)</code> is a partially applied function with the type <code>Error -&gt; EitherT Error IO Reservation</code>; that is: the input type is <code>Error</code>, so it can only be composed with an <code>Either Error a</code> value. That matches the return type of <code>checkCapacity 10 i r</code>, so the code compiles, but if I accidentally switch the arguments to <code>eitherT</code>, it doesn't compile. </p> <p> I find it captivating to figure out how to 'click' together such interleaving functions using the composition functions that Haskell provides. Often, when the composition compiles, it works as intended. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="1fd6730b3995443f9074d2dbec00dfc3"> <div class="comment-author"><a href="http://stackoverflow.com/users/1523776/benjamin-hodgson?tab=profile">Benjamin Hodgson</a> <a href="#1fd6730b3995443f9074d2dbec00dfc3">#</a></div> <div class="comment-content"> <p> This is something of a tangent, but I wanted to hint to you that Haskell can help reduce the boilerplate it takes to compose monadic computations like this. </p> <p> The <a href="https://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Except.html#t:MonadError"><code>MonadError</code></a> class abstracts monads which support throwing and catching errors. If you don't specify the concrete monad (transformer stack) a computation lives in, it's easier to compose it into a larger environment. </p> <p> <pre>checkCaravanCapacityOnError :: MonadError Error m =&gt; Error -&gt; Maybe Caravan -&gt; Reservation -&gt; m Reservation checkCaravanCapacityOnError CapacityExceeded (Just caravan) reservation | caravanCapacity caravan &lt; quantity reservation = throwError CapacityExceeded | otherwise = return reservation checkCaravanCapacityOnError err _ _ = throwError err</pre> </p> <p> I'm programming to the interface, not the implementation, by using <code>throwError</code> and <code>return</code> instead of <code>Left</code> and <code>Right</code>. This allows me to dispense with calls to <code>hoistEither</code> when I come to call my function in the context of a bigger monad: </p> <p> <pre>findCaravan :: MonadIO m =&gt; ServiceAddress -&gt; Int -&gt; ZonedTime -&gt; m (Maybe Caravan) reserveCaravan :: MonadIO m =&gt; ServiceAddress -&gt; ZonedTime -&gt; m () checkCaravan :: (MonadIO m, MonadError Error m) =&gt; Reservation -&gt; Error -&gt; m Reservation checkCaravan reservation err = do c &lt;- findCaravan svcAddr (quantity reservation) (date reservation) newRes &lt;- checkCaravanCapacityOnError err c reservation traverse_ (reserveCaravan svcAddr (date newRes)) c return newRes</pre> </p> <p> Note how <code>findCaravan</code> and <code>reserveCaravan</code> only declare a <code>MonadIO</code> constraint, whereas <code>checkCaravan</code> needs to do both IO and error handling. The type class system lets you declare the capabilities you need from your monad without specifying the monad in question. The elaborator figures out the right number of calls to <code>lift</code> when it builds the <code>MonadError</code> dictionary, which is determined by the concrete type you choose for <code>m</code> at the edge of your system. </p> <p> A logical next step here would be to further constrain the effects that a given IO function can perform. In this example, I'd consider writing a separate class for monads which support calling the caravan service: <code>findCaravan :: MonadCaravanService m =&gt; ServiceAddress -&gt; Int -&gt; ZonedTime -&gt; m (Maybe Caravan)</code>. This ensures that <code>findCaravan</code> can <i>only</i> call the caravan service, and not perform any other IO. It also makes it easier to mock functions which call the caravan service by writing a fake instance of <code>MonadCaravanService</code>. </p> <p> F# doesn't support this style of programming because it lacks higher-kinded types. You can't abstract over <code>m</code>; you have to pick a concrete monad up-front. This is bad for code reuse: if you need to add (for example) runtime configuration to your application you have to rewrite the implementation of your monad, and potentially every function which uses it, rather than just tacking on a <code>MonadReader</code> constraint to the functions that need it and adding a call to <code>runReaderT</code> at the entry point to your application. </p> <p> Finally, monad transformers are but one style of effect typing; <a href="http://okmij.org/ftp/Haskell/extensible/more.pdf"><code>extensible-effects</code></a> is an alternative which is gaining popularity. </p> </div> <div class="comment-date">2016-07-04 12:05 UTC</div> </div> <div class="comment" id="8c0ff9f99503458bbeb08f45f4772d23"> <div class="comment-author"><a href="https://github.com/MartinRykfors">Martin Rykfors</a> <a href="#8c0ff9f99503458bbeb08f45f4772d23">#</a></div> <div class="comment-content"> <p> Hi Mark, </p> <p> Thank you very much for the elaborate explanation. I'm also delighted that you stuck with my admittedly contrived example of using caravans to compensate for the restaurant's lack of space. Or is that nothing compared to some of the stranger real-life feature requests some of us have seen? </p> <p> I agree with your point on my previous comment that my suggestion could be considered a leaky abstraction and would introduce unnecessary requirements to the implementation. It just feels strange to let go of the idea that the domain logic is to be unconditionally pure, and not just mostly pure with the occasional impure function passed in as an argument. It's like what you say towards the end of the post - I feel hesitant to mix branching code and composition code together. The resulting solution you propose here is giving me second thoughts though. The <code>postReservation</code> function didn't become much more complex as I'd feared, with the branching logic nicely delegated to the <code>eitherT</code> function. The caravan logic also gets its own composition function that is easy enough to understand on its own. I guess I've got some thinking to do. </p> <p> So, a final question regarding this example: To what extent would you apply this technique when solving the same problem in F#? It seems that we are using an increasing amount of Haskell language features not present in F#, so maybe not everything would translate over cleanly. </p> </div> <div class="comment-date">2016-07-04 19:05 UTC</div> </div> <div class="comment" id="d795766026854896abd9c040e8bd92ff"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d795766026854896abd9c040e8bd92ff">#</a></div> <div class="comment-content"> <p> Martin, I'm still experimenting with how that influences my F# code. I'd like to at least attempt to back-port something like this to F# using computation expressions, but it may turn out that it's not worth the effort. </p> </div> <div class="comment-date">2016-07-04 20:46 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Roman numerals via property-based TDD https://blog.ploeh.dk/2016/06/28/roman-numerals-via-property-based-tdd 2016-06-28T07:28:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of doing the Roman numerals kata with property-based test-driven development.</em> </p> <p> The <em>Roman numerals</em> kata is a simple programming exercise. You should implement conversion to and from <a href="https://en.wikipedia.org/wiki/Roman_numerals">Roman numerals</a>. This always struck me as the ultimate case for example-driven development, but I must also admit that I've managed to get stuck on the exercise doing exactly that: throwing more and more examples at the problem. Prompted by previous successes with property-based testing, I wondered whether the problem would be more tractable if approached with property-based testing. This turned out to be the case. </p> <h3 id="0e88815de35b4edba4bf39b92b99c79f"> Single values <a href="#0e88815de35b4edba4bf39b92b99c79f" title="permalink">#</a> </h3> <p> When modelling a problem with property-based testing, you should attempt to express it in terms of general rules, but even so, the fundamental rule of Roman numerals is that there are certain singular symbols that have particular numeric values. There are no overall principles guiding these relationships; they simply are. Therefore, you'll still need to express these singular values as particular values. This is best done with a simple parametrised test, here using <a href="http://xunit.github.io">xUnit.net 2.1</a>: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Theory</span>&gt;] [&lt;<span style="color:#4ec9b0;">InlineData</span>(<span style="color:#a31515;">&quot;I&quot;</span>,&nbsp;&nbsp;&nbsp;&nbsp;1)&gt;] [&lt;<span style="color:#4ec9b0;">InlineData</span>(<span style="color:#a31515;">&quot;V&quot;</span>,&nbsp;&nbsp;&nbsp;&nbsp;5)&gt;] [&lt;<span style="color:#4ec9b0;">InlineData</span>(<span style="color:#a31515;">&quot;X&quot;</span>,&nbsp;&nbsp;&nbsp;10)&gt;] [&lt;<span style="color:#4ec9b0;">InlineData</span>(<span style="color:#a31515;">&quot;L&quot;</span>,&nbsp;&nbsp;&nbsp;50)&gt;] [&lt;<span style="color:#4ec9b0;">InlineData</span>(<span style="color:#a31515;">&quot;C&quot;</span>,&nbsp;&nbsp;100)&gt;] [&lt;<span style="color:#4ec9b0;">InlineData</span>(<span style="color:#a31515;">&quot;D&quot;</span>,&nbsp;&nbsp;500)&gt;] [&lt;<span style="color:#4ec9b0;">InlineData</span>(<span style="color:#a31515;">&quot;M&quot;</span>,&nbsp;1000)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``elemental&nbsp;symbols&nbsp;have&nbsp;correct&nbsp;values``</span>&nbsp;(symbol&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>)&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;expected&nbsp;=!&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">tryParseRoman</span>&nbsp;symbol</pre> </p> <p> The <code>=!</code> operator is a custom operator defined by <a href="http://www.swensensoftware.com/unquote">Unquote</a> (3.1.1), an assertion library. You can read it as <em>must equal</em>; that is, you can read this particular assertion as <em>some expected must equal tryParseRoman symbol</em>. </p> <p> As you can see, this simple test expresses the relationship between the singular Roman numeral values and their decimal counterparts. You might still consider this automated test as example-driven, but I more think about it as establishing the ground rules for how Roman numerals work. If you look at <a href="https://en.wikipedia.org/wiki/Roman_numerals#Roman_numeric_system">the Wikipedia article</a>, for instance, it also starts explaining the system by listing the values of these seven symbols. </p> <h3 id="7539ed1043884b22a0372e7193d3eaba"> Round-tripping <a href="#7539ed1043884b22a0372e7193d3eaba" title="permalink">#</a> </h3> <p> A common technique when applying property-based testing to parsing problems is to require that values can round-trip. This should also be the case here: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;romanRange&nbsp;=&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;[1..3999]&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> [&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``tryParse&nbsp;is&nbsp;the&nbsp;inverse&nbsp;of&nbsp;toRoman``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;romanRange&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="color:navy;background:yellow;">Some</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">i</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">=</span><span style="background:yellow;">&nbsp;(</span><span style="background:yellow;">i</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:#4ec9b0;background:yellow;">Numeral</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">toRoman</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:#4ec9b0;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">bind</span><span style="background:yellow;">&nbsp;</span><span style="color:#4ec9b0;background:yellow;">Numeral</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">tryParseRoman</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> This property uses <a href="http://fscheck.github.io/FsCheck">FsCheck</a> (2.2.4). First, it expresses a range of relevant integers. For various reasons (that we'll return to) we're only going to attempt conversion of the integers between 1 and 3,999. The value <code>romanRange</code> has the type Arbitrary&lt;int&gt;, where Arbitrary&lt;'a&gt; is a type defined by FsCheck. You can think of it as a generator of random values. In this case, <code>romanRange</code> generates random integers between 1 and 3,999. </p> <p> When used with Prop.forAll, the property states that for all values drawn from romanRange, the anonymous function should succeed. The <code>i</code> argument within that anonymous function is populated by <code>romanRange</code>, and the function is executed 100 times (by default). </p> <p> The <code>test</code> function is another Unquote function. It evaluates and reports on the quoted boolean expression; if it evaluates to true, nothing happens, but it throws an exception if it evaluates to false. </p> <p> The particular expression states that if you call toRoman with <code>i</code>, and then call tryParseRoman with the return value of that function call, the result should be equal to <code>i</code>. Both sides should be wrapped in a Some case, though, since both toRoman and tryParseRoman might also return None. For the values in romanRange, however, you'd expect that the round-trip always succeeds. </p> <h3 id="01b708fc05b84a119ed6450b81c75935"> Additivity <a href="#01b708fc05b84a119ed6450b81c75935" title="permalink">#</a> </h3> <p> The fundamental idea about Roman numerals is that they are additive: I means 1, II means (1 + 1 =) 2, XXX means (10 + 10 + 10 =) 30, and MLXVI means (1000 + 50 + 10 + 5 + 1 =) 1066. You simply count and add. Yes, there are special rules for subtractive shorthand, but forget about those for a moment. If you have a Roman numeral with symbols in strictly descending order, you can simply add the symbol values together. </p> <p> You can express this with FsCheck. It looks a little daunting, but actually isn't that bad. I'll show it first, and then walk you through it: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``symbols&nbsp;in&nbsp;descending&nbsp;order&nbsp;are&nbsp;additive``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">stringRepeat</span>&nbsp;char&nbsp;count&nbsp;=&nbsp;<span style="color:#4ec9b0;">String</span>&nbsp;(char,&nbsp;count) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">genSymbols</span>&nbsp;count&nbsp;symbol&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0..count]&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">List</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:navy;">stringRepeat</span>&nbsp;symbol)&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;thousands&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">genSymbols</span>&nbsp;3&nbsp;<span style="color:#a31515;">&#39;M&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fiveHundreds&nbsp;=&nbsp;<span style="color:navy;">genSymbols</span>&nbsp;1&nbsp;<span style="color:#a31515;">&#39;D&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hundreds&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">genSymbols</span>&nbsp;3&nbsp;<span style="color:#a31515;">&#39;C&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fifties&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">genSymbols</span>&nbsp;1&nbsp;<span style="color:#a31515;">&#39;L&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;tens&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">genSymbols</span>&nbsp;3&nbsp;<span style="color:#a31515;">&#39;X&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fives&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">genSymbols</span>&nbsp;1&nbsp;<span style="color:#a31515;">&#39;V&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;ones&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">genSymbols</span>&nbsp;3&nbsp;<span style="color:#a31515;">&#39;I&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;symbols&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[thousands;&nbsp;fiveHundreds;&nbsp;hundreds;&nbsp;fifties;&nbsp;tens;&nbsp;fives;&nbsp;ones] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">sequence</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:#4ec9b0;">String</span>.<span style="color:navy;">Concat</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;symbols&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">tryParseRoman</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:navy;">string</span>&nbsp;&gt;&gt;&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">tryParseRoman</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">choose</span>&nbsp;<span style="color:navy;">id</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">sum</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">Some</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> The first two lines are two utility functions. The function <code>stringRepeat</code> has the type <code>char -&gt; int -&gt; string</code>. It simply provides a curried form of the String constructor overload that enables you to repeat a particular char value. As an example, <code>stringRepeat 'I' 0</code> is "", <code>stringRepeat 'X' 2</code> is "XX", and so on. </p> <p> The function <code>genSymbols</code> has the type <code>int -&gt; char -&gt; Gen&lt;string&gt;</code>. It returns a generator that produces a repeated string no longer than the specified length. Thus, <code>genSymbols 3 'M'</code> is a generator that draws random values from the set [""; "M"; "MM"; "MMM"], <code>genSymbols 1 'D'</code> is a generator that draws random values from the set [""; "D"], and so on. Notice that the empty string is one of the values that the generator may use. This is by design. </p> <p> Using genSymbols, you can define generators for all the symbols: up to three thousands, up to one five hundreds, up to three hundreds, etc. <code>thousands</code>, <code>fiveHundreds</code>, <code>hundreds</code>, and so on, are all values of the type Gen&lt;string&gt;. </p> <p> You can combine all these string generators to a single generator using Gen.sequence, which takes a seq&lt;Gen&lt;'a&gt;&gt; as input and returns a Gen&lt;'a list&gt;. In this case, the input is [thousands; fiveHundreds; hundreds; fifties; tens; fives; ones], which has the type <code>Gen&lt;string&gt; list</code>, so the output is a <code>Gen&lt;string list&gt;</code> value. Values generated could include ["MM"; ""; ""; ""; "X"; ""; ""], [""; "D"; "CC"; "L"; "X"; "V"; ""], and so on. </p> <p> Instead of lists of strings, you need single string values. These are easy to create using the built-in method String.Concat. You have to do it within a Gen value, though, so it's <code>Gen.map String.Concat</code>. Finally, you can convert the resulting Gen&lt;string&gt; to an Arbitrary using Arb.fromGen. The final <code>symbols</code> value has the type Arbitrary&lt;string&gt;. It'll generate values such as "MMX" and "DCCLXV". </p> <p> This is a bit of work to ensure that proper Roman numerals are generated, but the rest of the property is tractable. You can use FsCheck's Prop.forAll to express the property that when tryParseRoman is called with any of the generated numerals, the return value is equal to the expected value. </p> <p> The expected value is the sum of the value of each of the symbols in the input string, <code>s</code>. The <code>string</code> type implements the interface <code>char seq</code>, so you can map each of the characters by invoking tryParseRoman. That gives you a <cdata>seq&lt;int option&gt;</cdata>, because tryParseRoman returns <code>int option</code> values. You can use <code>Seq.choose id</code> to throw away any None values there may be, and then <code>Seq.sum</code> to calculate the sum of the integers. Finally, you can pipe the sum into the Some case constructor to turn <code>expected</code> into an <code>int option</code>, which matches the type of <code>actual</code>. </p> <p> Now that you have <code>expected</code> and <code>actual</code> values, you can assert that these two values must be equal to each other. This property states that for all strictly descending numerals, the return value must be the sum of the constituent symbols. </p> <h3 id="151d6efa45574320a3c33ad4aedd8a59"> Subtractiveness <a href="#151d6efa45574320a3c33ad4aedd8a59" title="permalink">#</a> </h3> <p> The principal rule for Roman numerals is that of additivity, but if you only apply the above rules, you'd allow numerals such as IIII for 4, LXXXX for 90, etc. While there's historical precedent for such notation, it's not allowed in 'modern' Roman numerals. If there are more than three repeated characters, you should instead prefer subtractive notation: IV for 4, XC for 90, and so on. </p> <p> Subtractive notation is, however, only allowed within adjacent groups. Logically, you could write 1999 as MIM, but this isn't allowed. The symbol I can only be used to subtract from V and X, X can only subtract from L and C, and C can only subtract from D and M. </p> <p> Within these constraints, you have to describe the property of subtractive notation. Here's one way to do it: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">OptionBuilder</span>()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Bind</span>(v,&nbsp;<span style="color:navy;">f</span>)&nbsp;=&nbsp;<span style="color:#4ec9b0;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;v &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Return</span>&nbsp;v&nbsp;=&nbsp;<span style="color:navy;">Some</span>&nbsp;v <span style="color:blue;">let</span>&nbsp;opt&nbsp;=&nbsp;<span style="color:#4ec9b0;">OptionBuilder</span>() [&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``certain&nbsp;symbols&nbsp;in&nbsp;ascending&nbsp;are&nbsp;subtractive``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">subtractive</span>&nbsp;(subtrahend&nbsp;:&nbsp;<span style="color:#4ec9b0;">char</span>)&nbsp;(minuends&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>)&nbsp;=&nbsp;<span style="color:blue;">gen</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;minuend&nbsp;=&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;minuends &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;subtrahend,&nbsp;minuend&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;symbols&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">oneof</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">subtractive</span>&nbsp;<span style="color:#a31515;">&#39;I&#39;</span>&nbsp;<span style="color:#a31515;">&quot;VX&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">subtractive</span>&nbsp;<span style="color:#a31515;">&#39;X&#39;</span>&nbsp;<span style="color:#a31515;">&quot;LC&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">subtractive</span>&nbsp;<span style="color:#a31515;">&#39;C&#39;</span>&nbsp;<span style="color:#a31515;">&quot;DM&quot;</span>&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;symbols&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;(subtrahend,&nbsp;minuend)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;originalRoman&nbsp;=&nbsp;<span style="color:#4ec9b0;">String</span>&nbsp;[|&nbsp;subtrahend;&nbsp;minuend&nbsp;|] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">tryParseRoman</span>&nbsp;originalRoman &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;roundTrippedRoman&nbsp;=&nbsp;actual&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">toRoman</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:blue;">opt</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;m&nbsp;=&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">tryParseRoman</span>&nbsp;(<span style="color:navy;">string</span>&nbsp;minuend) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;s&nbsp;=&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">tryParseRoman</span>&nbsp;(<span style="color:navy;">string</span>&nbsp;subtrahend) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;m&nbsp;-&nbsp;s&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;originalRoman&nbsp;=!&nbsp;roundTrippedRoman</pre> </p> <p> Like the previous property, this looks like a mouthful, but isn't too bad. I'll walk you through it. </p> <p> Initially, ignore the OptionBuilder type and the opt value; we'll return to them shortly. The property itself starts by defining a function called <code>subtractive</code>, which has the type <code>char -&gt; string -&gt; Gen&lt;char * char&gt;</code>. The first argument is a symbol representing <a href="https://en.wikipedia.org/wiki/Subtraction">the subtrahend</a>; that is: the number being subtracted. The next argument is a sequence of minuends; that is: numbers from which the subtrahend will be subtracted. </p> <p> The <code>subtractive</code> function is implemented with a <code>gen</code> computation expression. It first uses a <code>let!</code> binding to define that a singular minuend is a random value drawn from <code>minuends</code>. As usual, Gen.elements is the workhorse: it defines a generator that randomly draws from a sequence of values, and because <code>minuends</code> is a string, and the string type implements <code>char seq</code>, it can be used with Gen.elements to define a generator of char values. While <code>Gen.elements minuends</code> returns a Gen&lt;char&gt; value, the use of a <code>let!</code> binding within a computation expression causes <code>minuend</code> to have the type <code>char</code>. </p> <p> The second line of code in <code>subtractive</code> returns a tuple of two char values: the subtrahend first, and the minuend second. Normally, when subtracting with the arithmetic minus operator, you'd write a difference as <em>minuend - subtrahend</em>; the minuends comes first, followed by the subtrahend. The Roman subtractive notation, however, is written with the subtrahend before the minuend, which is the reason that the <code>subtractive</code> function returns the symbols in that order. It's easier to think about that way. </p> <p> The <code>subtractive</code> function enables you to define generators of Roman numerals using subtractive notation. Since I can only be used before V and X, you can define a generator using <code>subtractive 'I' "VX"</code>. This is a Gen&lt;char * char&gt; value. Likewise, you can define <code>subtractive 'X' "LC"</code> and <code>subtractive 'C' "DM"</code> and use Gen.oneOf to define a generator that randomly selects one of these generators, and uses the selected generator to produce a value. As always, the last step is to convert the generator into an Arbitrary with Arb.fromGen, so that <code>symbols</code> has the type Arbitrary&lt;char * char&gt;. </p> <p> Equipped with an Arbitrary, you can again use Prop.forAll to express the desired property. First, <code>originalRoman</code> is created from the subtrahend and minuend. Due to the way <code>symbols</code> is defined, <code>originalRoman</code> will have values like "IV", "XC", and so on. </p> <p> The property then proceeds to invoke tryParseRoman. It also uses the <code>actual</code> value to produce a round-tripped value. Not only should the parser correctly understand subtractive notation, but the integer-to-Roman conversion should also prefer this notation. </p> <p> The last part of the property is the assertion. Here, you need <code>opt</code>, which is a <a href="https://msdn.microsoft.com/en-us/library/dd233182.aspx">computation builder</a> for option values. </p> <p> In the assertion, you need to calculate the expected value. Both minuend and subtrahend are char values; in order to find their corresponding decimal values, you'll need to call tryParseRoman. The problem is that tryParseRoman returns an <code>int option</code>. For example, <code>tryParseRoman "I"</code> returns <code>Some 1</code>, so you may need to subtract <code>Some 1</code> from <code>Some 5</code>. The most readable way I've found is to use a computation expression. Using <code>let!</code> bindings, both <code>m</code> and <code>s</code> are <code>int</code> values, which you can easily subtract using the normal <code>-</code> operator. </p> <p> <code>expected</code> and <code>actual</code> are both <code>int option</code> values, so can be compared using Unquote's <em>must equal</em> operator. </p> <p> Finally, the property also asserts that the original value must be equal to the round-tripped value. If not, you could have an implementation that correctly parses "IV" as 4, but converts 4 to "IIII". </p> <h3 id="49291a911a82471d825d690b5a47f2b4"> Limited repetition <a href="#49291a911a82471d825d690b5a47f2b4" title="permalink">#</a> </h3> <p> The previous property only ensures that subtractive notation is used in simple cases, like IX or CD. It doesn't verify that composite numerals are correctly written. As an example, the converter should convert 1893 to MDCCCXCIII, not MDCCCLXXXXIII. The second alternative is incorrect because it uses LXXXX to represent 90, instead of XC. </p> <p> The underlying property is that any given symbol can only be repeated at most three times. A symbol can appear more than thrice in total, as demonstrated by the valid numeral MDCCCXCIII, in which C appears four times. For any group of repeated characters, however, a character must only be repeated once, twice, or thrice. </p> <p> This also explain why the maximum Roman numeral is MMMCMXCIX, or 3,999. </p> <p> In order to express this property in code, you first need a function to group characters. Here, I've chosen to reuse <a href="http://stackoverflow.com/a/35055165/126014">one of my own creation</a>: </p> <p> <pre><span style="color:green;">//&nbsp;seq&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;&#39;a&nbsp;list&nbsp;list&nbsp;when&nbsp;&#39;a&nbsp;:&nbsp;equality</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">group</span>&nbsp;xs&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">folder</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[[x]] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;(h<span style="color:navy;">::</span>t)<span style="color:navy;">::</span>ta&nbsp;<span style="color:blue;">when</span>&nbsp;h&nbsp;=&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(x<span style="color:navy;">::</span>h<span style="color:navy;">::</span>t)<span style="color:navy;">::</span>ta &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;acc&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[x]<span style="color:navy;">::</span>acc &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">foldBack</span>&nbsp;<span style="color:navy;">folder</span>&nbsp;xs&nbsp;[]</pre> </p> <p> This function will, for example, group "MDCCCXCIII" like this: </p> <p> <pre>&gt; "MDCCCXCIII" |&gt; group;; val it : char list list = [['M']; ['D']; ['C'; 'C'; 'C']; ['X']; ['C']; ['I'; 'I'; 'I']]</pre> </p> <p> All you need to do is to find the length of all such sub-lists, and assert that the maximum is at most 3: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``there&nbsp;can&nbsp;be&nbsp;no&nbsp;more&nbsp;than&nbsp;three&nbsp;identical&nbsp;symbols&nbsp;in&nbsp;a&nbsp;row``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;romanRange&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">toRoman</span>&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:#4ec9b0;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">map</span><span style="background:yellow;">&nbsp;(</span><span style="color:navy;background:yellow;">group</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">&gt;&gt;</span><span style="background:yellow;">&nbsp;(</span><span style="color:#4ec9b0;background:yellow;">List</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">map</span><span style="background:yellow;">&nbsp;</span><span style="color:#4ec9b0;background:yellow;">List</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">length</span><span style="background:yellow;">)&nbsp;</span><span style="background:yellow;">&gt;&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:#4ec9b0;background:yellow;">List</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">max</span><span style="background:yellow;">)</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:#4ec9b0;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">exists</span><span style="background:yellow;">&nbsp;(</span><span style="color:blue;background:yellow;">fun</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">x</span><span style="background:yellow;">&nbsp;</span><span style="color:blue;background:yellow;">-&gt;</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">x</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">&lt;=</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">3</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> Since <code>actual</code> is a <code>string option</code>, you need to express the assertion within the Maybe (Option) monad. First, you can use Option.map to map any value (should there be one) to find the maximum length of any repeated character group. This returns an <code>int option</code>. </p> <p> Finally, you can pipe that <code>int option</code> into Option.exists, which will evaluate to false if there's no value, or if the boolean expression <code>x &lt;= 3</code> evaluates to false. </p> <h3 id="1f26f442edc742b48a3c85a2832709b7"> Input range <a href="#1f26f442edc742b48a3c85a2832709b7" title="permalink">#</a> </h3> <p> At this point, you're almost done. The only remaining properties you'll need to specify is that the maximum value is 3,999, and the minimum value is 1. Negative numbers, or zero, are not allowed: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``negative&nbsp;numbers&nbsp;and&nbsp;zero&nbsp;are&nbsp;not&nbsp;supported``</span>&nbsp;i&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;-(<span style="color:navy;">abs</span>&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">toRoman</span>&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="color:#4ec9b0;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">isNone</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;@&gt;</span></pre> </p> <p> In this property, the function argument <code>i</code> can be <em>any</em> number, but calling <code>abs</code> ensures that it's positive (or zero), and the unary <code>-</code> operator then converts that positive value to a negative value. </p> <p> Notice that the new <code>i</code> value shadows the input argument of the same name. This is <a href="/2016/02/15/types-properties-software-properties-for-the-forties">a common trick when writing properties</a>. It prevents me from accidentally using the input value provided by FsCheck. While the input argument is useful as a seed value, it isn't guaranteed to model the particular circumstances of this property. Here, you only care to assert what happens if the input is negative or zero. Specifically, you always want the return value to be None. </p> <p> Likewise for too large input values: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``numbers&nbsp;too&nbsp;big&nbsp;are&nbsp;not&nbsp;supported``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">choose</span>&nbsp;(4000,&nbsp;<span style="color:#4ec9b0;">Int32</span>.MaxValue)&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span>&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#4ec9b0;">Numeral</span>.<span style="color:navy;">toRoman</span>&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="color:#4ec9b0;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">isNone</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;@&gt;</span></pre> </p> <p> Here, Gen.choose is used to define an Arbitrary&lt;int&gt; that only produces numbers between 4000 and Int32.MaxValue (including both boundary values). </p> <p> This test is similar to the one that exercises negative values, so you could combine them to a single function if you'd like. I'll leave this as an exercise, though. </p> <h3 id="28ffa8a5e8694aa7b0752175b5e2f2aa"> Implementation <a href="#28ffa8a5e8694aa7b0752175b5e2f2aa" title="permalink">#</a> </h3> <p> The interesting part of this exercise is, I think, how to define the properties. There are many ways you can implement the functions to pass all properties. Here's one of them: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;System <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryParseRoman</span>&nbsp;candidate&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">add</span>&nbsp;x&nbsp;=&nbsp;<span style="color:#4ec9b0;">Option</span>.<span style="color:navy;">map</span>&nbsp;((+)&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;acc&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;I&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;X&#39;</span><span style="color:navy;">::</span>xs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;&nbsp;&nbsp;9)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;I&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;V&#39;</span><span style="color:navy;">::</span>xs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;&nbsp;&nbsp;4)&nbsp;xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;I&#39;</span><span style="color:navy;">::</span>xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;&nbsp;&nbsp;1)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;V&#39;</span><span style="color:navy;">::</span>xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;&nbsp;&nbsp;5)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;X&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;C&#39;</span><span style="color:navy;">::</span>xs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;&nbsp;90)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;X&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;L&#39;</span><span style="color:navy;">::</span>xs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;&nbsp;40)&nbsp;xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;X&#39;</span><span style="color:navy;">::</span>xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;&nbsp;10)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;L&#39;</span><span style="color:navy;">::</span>xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;&nbsp;50)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;C&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;M&#39;</span><span style="color:navy;">::</span>xs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;900)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;C&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;D&#39;</span><span style="color:navy;">::</span>xs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;400)&nbsp;xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;C&#39;</span><span style="color:navy;">::</span>xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;100)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;D&#39;</span><span style="color:navy;">::</span>xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;&nbsp;500)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;M&#39;</span><span style="color:navy;">::</span>xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:navy;">add</span>&nbsp;1000)&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;candidate&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">toList</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:navy;">Some</span>&nbsp;0) <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">toRoman</span>&nbsp;i&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;acc&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;1000&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;M&#39;</span><span style="color:navy;">::</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc)&nbsp;(x&nbsp;-&nbsp;1000) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;900&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;M&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;C&#39;</span><span style="color:navy;">::</span>acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;900) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;500&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;D&#39;</span><span style="color:navy;">::</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;500) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;400&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;D&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;C&#39;</span><span style="color:navy;">::</span>acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;400) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;100&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;C&#39;</span><span style="color:navy;">::</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;100) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;&nbsp;90&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;C&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;X&#39;</span><span style="color:navy;">::</span>acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;&nbsp;90) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;&nbsp;50&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;L&#39;</span><span style="color:navy;">::</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;&nbsp;50) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;&nbsp;40&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;L&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;X&#39;</span><span style="color:navy;">::</span>acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;&nbsp;40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;&nbsp;10&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;X&#39;</span><span style="color:navy;">::</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;&nbsp;10) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;&nbsp;&nbsp;9&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;X&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;I&#39;</span><span style="color:navy;">::</span>acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;9) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;V&#39;</span><span style="color:navy;">::</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;5) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;V&#39;</span><span style="color:navy;">::</span><span style="color:#a31515;">&#39;I&#39;</span><span style="color:navy;">::</span>acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;4) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;x&nbsp;<span style="color:blue;">when</span>&nbsp;x&nbsp;&gt;=&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;(<span style="color:#a31515;">&#39;I&#39;</span><span style="color:navy;">::</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc)&nbsp;(x&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;0&nbsp;&lt;&nbsp;i&nbsp;&amp;&amp;&nbsp;i&nbsp;&lt;&nbsp;4000 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;[]&nbsp;i&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">List</span>.<span style="color:navy;">rev</span>&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">List</span>.<span style="color:navy;">toArray</span>&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">String</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">Some</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> Both functions use <a href="/2015/12/22/tail-recurse">tail-recursive inner <code>imp</code> functions</a> in order to accumulate the appropriate answer. </p> <p> One of the nice properties (that I didn't test for) of this implementation is that the tryParseRoman function is a <a href="http://martinfowler.com/bliki/TolerantReader.html">Tolerant Reader</a>. While toRoman would never create such a numeral, tryParseRoman correctly understands some alternative renderings: </p> <p> <pre>&gt; "MDCCCLXXXXIII" |&gt; tryParseRoman;; val it : int option = Some 1893 &gt; 1893 |&gt; toRoman;; val it : String option = Some "MDCCCXCIII"</pre> </p> <p> In other words, the implementation follows <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. tryParseRoman is liberal in what it accepts, while toRoman is conservative in what it returns. </p> <h3 id="0e7b6d96301e4d84a1a218b2caa7aca7"> Summary <a href="#0e7b6d96301e4d84a1a218b2caa7aca7" title="permalink">#</a> </h3> <p> Some problems look, at first glance, as obvious candidates for example-driven development. In my experience, this particularly happen when obvious examples abound. It's not difficult to come up with examples of Roman numerals, so it seems intuitive that you should just start writing some test cases with various examples. In my experience, though, that doesn't guarantee that you're led towards a good implementation. </p> <p> The more a problem description is based on examples, the harder it can be to identify the underlying properties. Still, they're often there, once you start looking. As I've <a href="/2015/01/10/diamond-kata-with-fscheck">previously reported</a>, using property-based test-driven development enables you to proceed in a more incremental fashion, because properties describe only parts of the desired solution. </p> <p> If you're interested in learning more about Property-Based Testing, you can watch my <a href="https://blog.ploeh.dk/property-based-testing-intro">introduction to Property-based Testing with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. SUT Double https://blog.ploeh.dk/2016/06/15/sut-double 2016-06-15T18:01:00+00:00 Mark Seemann <div id="post"> <p> <em>While it's possible to rely on Extract and Override for unit testing, Dependency Injection can make code and tests simpler and easier to maintain.</em> </p> <p> In object-oriented programming, many people still struggle with the intersection of design and testability. If a unit test is <em>an automated test of a unit in isolation of its dependencies</em>, then how do you isolate an object from its dependencies, most notably databases, web services, and the like? </p> <p> One technique, described in <a href="http://amzn.to/1ygZ4Jv">The Art of Unit Testing</a>, is called <em>Extract and Override</em>. The idea is that you write a class, but use a <a href="https://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a>, <a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory Method</a>, or other sort of virtual class method to expose extensibility points that a unit test can use to achieve the desired isolation. </p> <p> People sometimes ask me whether that isn't good enough, and why <a href="http://amzn.to/12p90MG">I advocate the (ostensibly) more complex technique of Dependency Injection</a>? </p> <p> It's easiest to understand the advantages and disadvantages if we have an example to discuss. </p> <h3 id="9109c10b5e8d4f989ca510ec09974372"> Example: Extract and Override <a href="#9109c10b5e8d4f989ca510ec09974372" title="permalink">#</a> </h3> <p> Imagine that you're creating a reservation system for a restaurant. Clients (web sites, smart phone apps, etcetera) display a user interface where you can fill in details about your reservation: your name, email address, the number of guests, and the date of your reservation. When you submit your reservation request, the client POSTs a JSON document to a web service. </p> <p> In this example, the web service is implemented by a Controller class, and the Post method handles the incoming request: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:#2b91af;">ReservationDto</span>&nbsp;reservationDto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;requestedDate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>(!<span style="color:#2b91af;">DateTime</span>.TryParse(reservationDto.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;requestedDate)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.BadRequest(<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">this</span>.ReadReservedSeats(requestedDate); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>(<span style="color:blue;">this</span>.Capacity&nbsp;&lt;&nbsp;reservationDto.Quantity&nbsp;+&nbsp;reservedSeats) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.StatusCode(<span style="color:#2b91af;">HttpStatusCode</span>.Forbidden); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.SaveReservation(requestedDate,&nbsp;reservationDto); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Ok(); }</pre> </p> <p> The implementation is simple: It first attempts to validate the incoming document, and returns an error message if the document is invalid. Second, it reads the number of already reserved seats via a helper method, and rejects the request if the remaining capacity is insufficient. On the other hand, if the remaining capacity is sufficient, it saves the reservation and returns 200 OK. </p> <p> The Post method relies on two helper methods that handles communication with the database: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">int</span>&nbsp;ReadReservedSeats(<span style="color:#2b91af;">DateTime</span>&nbsp;date) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;sql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;COALESCE(SUM([Quantity]),&nbsp;0)&nbsp;FROM&nbsp;[dbo].[Reservations] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;YEAR([Date])&nbsp;=&nbsp;YEAR(@Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;MONTH([Date])&nbsp;=&nbsp;MONTH(@Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;DAY([Date])&nbsp;=&nbsp;DAY(@Date)&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connStr&nbsp;=&nbsp;<span style="color:#2b91af;">ConfigurationManager</span>.ConnectionStrings[<span style="color:#a31515;">&quot;booking&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConnectionString; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(connStr)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(sql,&nbsp;conn)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;(<span style="color:blue;">int</span>)cmd.ExecuteScalar(); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SaveReservation( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;dateTime, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;reservationDto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;sql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INSERT&nbsp;INTO&nbsp;[dbo].[Reservations]&nbsp;([Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(@Date,&nbsp;@Name,&nbsp;@Email,&nbsp;@Quantity)&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connStr&nbsp;=&nbsp;<span style="color:#2b91af;">ConfigurationManager</span>.ConnectionStrings[<span style="color:#a31515;">&quot;booking&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConnectionString; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(connStr)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(sql,&nbsp;conn)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;reservationDto.Date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;reservationDto.Name)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Email&quot;</span>,&nbsp;reservationDto.Email)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Quantity&quot;</span>,&nbsp;reservationDto.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In this example, both helper methods are <code>public</code>, but they could have been <code>protected</code> without changing any conclusions; by being <code>public</code>, though, they're easier to override using <a href="http://www.moqthis.com">Moq</a>. The important detail is that both of these methods are overridable. In C#, you declare that with the <code>virtual</code> keyword; in Java, methods are overridable by default. </p> <p> Both of these methods use elemental ADO.NET to communicate with the database. You can also use an ORM, or any other database access technique you prefer - it doesn't matter for this discussion. What matters is that the methods can be overridden by unit tests. </p> <p> Here's a unit test of the happy path: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PostReturnsCorrectResultAndHasCorrectStateOnAcceptableRequest() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;json&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2016-05-31&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">ReservationsController</span>&gt;&nbsp;{&nbsp;CallBase&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(s&nbsp;=&gt;&nbsp;s.ReadReservedSeats(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2016,&nbsp;5,&nbsp;31))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(0); &nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(s&nbsp;=&gt;&nbsp;s.SaveReservation(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2016,&nbsp;5,&nbsp;31),&nbsp;json)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Verifiable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Object.Post(json); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;sut.Verify(); }</pre> </p> <p> This example uses the Extract and Override technique, but instead of creating a test-specific class that derives from ReservationsController, it uses Moq to create a dynamic <a href="http://www.martinfowler.com/bliki/TestDouble.html">Test Double</a> for the System Under Test (<a href="https://en.wikipedia.org/wiki/System_under_test">SUT</a>) - a SUT Double. </p> <p> Since both <code>ReadReservedSeats</code> and <code>SaveReservation</code> are virtual, Moq can override them with test-specific behaviour. In this test, it defines the behaviour of <code>ReadReservedSeats</code> in such a way that it returns <code>0</code> when the input is a particular date. </p> <p> While the test is simple, it has a single blemish. It ought to follow the Arrange Act Assert pattern, so it shouldn't have to configure the <code>SaveReservation</code> method <em>before</em> it calls the <code>Post</code> method. After all, the <code>SaveReservation</code> method is a Command, and you should use <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Mocks for Commands</a>. In other words, the test ought to verify the interaction with the <code>SaveReservation</code> method in the Assert phase; not configure it in the Arrange phase. </p> <p> Unfortunately, if you don't configure the <code>SaveReservation</code> method before calling <code>Post</code>, Moq will use the base implementation, which will attempt to interact with the database. The database isn't available in the unit test context, so without that override, the base method will throw an exception, causing the test to fail. </p> <p> Still, that's a minor issue. In general, the test is easy to follow, and the design of the ReservationsController class itself is also straightforward. Are there any downsides? </p> <h3 id="ead87b423b1d4e8aae9f1a4498e2ea08"> Example: shared connection <a href="#ead87b423b1d4e8aae9f1a4498e2ea08" title="permalink">#</a> </h3> <p> From a design perspective, the above version is fine, but you may find it inefficient that <code>ReadReservedSeats</code> and <code>SaveReservation</code> both open and close a connection to the database. Wouldn't it be more efficient if they could share a single connection? </p> <p> If (<a href="https://ericlippert.com/2012/12/17/performance-rant">by measuring</a>) you decide that you'd like to refactor the ReservationsController class to use a shared connection, your first attempt might look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:#2b91af;">ReservationDto</span>&nbsp;reservationDto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;requestedDate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.TryParse(reservationDto.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;requestedDate)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.BadRequest(<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">this</span>.OpenDbConnection()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">this</span>.ReadReservedSeats(conn,&nbsp;requestedDate); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.Capacity&nbsp;&lt;&nbsp;reservationDto.Quantity&nbsp;+&nbsp;reservedSeats) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.StatusCode(<span style="color:#2b91af;">HttpStatusCode</span>.Forbidden); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.SaveReservation(conn,&nbsp;requestedDate,&nbsp;reservationDto); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Ok(); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>&nbsp;OpenDbConnection() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connStr&nbsp;=&nbsp;<span style="color:#2b91af;">ConfigurationManager</span>.ConnectionStrings[<span style="color:#a31515;">&quot;booking&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConnectionString; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(connStr); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">catch</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Dispose(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;conn; } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">int</span>&nbsp;ReadReservedSeats(<span style="color:#2b91af;">SqlConnection</span>&nbsp;conn,&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;date) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;sql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;COALESCE(SUM([Quantity]),&nbsp;0)&nbsp;FROM&nbsp;[dbo].[Reservations] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;YEAR([Date])&nbsp;=&nbsp;YEAR(@Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;MONTH([Date])&nbsp;=&nbsp;MONTH(@Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;DAY([Date])&nbsp;=&nbsp;DAY(@Date)&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(sql,&nbsp;conn)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;(<span style="color:blue;">int</span>)cmd.ExecuteScalar(); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SaveReservation( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">SqlConnection</span>&nbsp;conn, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;dateTime, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;reservationDto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;sql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INSERT&nbsp;INTO&nbsp;[dbo].[Reservations]&nbsp;([Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(@Date,&nbsp;@Name,&nbsp;@Email,&nbsp;@Quantity)&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(sql,&nbsp;conn)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;reservationDto.Date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;reservationDto.Name)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Email&quot;</span>,&nbsp;reservationDto.Email)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Quantity&quot;</span>,&nbsp;reservationDto.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> You've changed both <code>ReadReservedSeats</code> and <code>SaveReservation</code> to take an extra parameter: the connection to the database. That connection is created by the <code>OpenDbConnection</code> method, but you also have to make that method overridable, because otherwise, it'd attempt to connect to a database during unit testing, and thereby causing the tests to fail. </p> <p> You can still unit test using the Extract and Overide technique, but the test becomes more complicated: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PostReturnsCorrectResultAndHasCorrectStateOnAcceptableRequest() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;json&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2016-05-31&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">ReservationsController</span>&gt;&nbsp;{&nbsp;CallBase&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;sut.Setup(s&nbsp;=&gt;&nbsp;s.OpenDbConnection()).Returns((<span style="color:#2b91af;">SqlConnection</span>)<span style="color:blue;">null</span>); &nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(s&nbsp;=&gt;&nbsp;s.ReadReservedSeats( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">SqlConnection</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2016,&nbsp;5,&nbsp;31))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(0); &nbsp;&nbsp;&nbsp;&nbsp;sut &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(s&nbsp;=&gt;&nbsp;s.SaveReservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">SqlConnection</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2016,&nbsp;5,&nbsp;31),&nbsp;json)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Verifiable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Object.Post(json); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;sut.Verify(); }</pre> </p> <p> Not only must you override <code>ReadReservedSeats</code> and <code>SaveReservation</code>, but you must also supply a <a href="http://xunitpatterns.com/Dummy%20Object.html">Dummy Object</a> for the connection object, as well as override <code>OpenDbConnection</code>. Still manageable, perhaps, but the design indisputably deteriorated. </p> <p> You can summarise the flaw by a single design smell: <a href="http://c2.com/cgi/wiki?FeatureEnvySmell">Feature Envy</a>. Both the <code>ReadReservedSeats</code> and the <code>SaveReservation</code> methods take an argument of the type SqlConnection. On the other hand, they don't use any instance members of the ReservationsController class that currently hosts them. These methods seem like they ought to belong to SqlConnection instead of ReservationsController. That's not possible, however, since SqlConnection is a class from the Base Class Library, but you can, instead, create a new Repository class. </p> <h3 id="615f20e730ad40c9ab9e1be8d9198196"> Example: Repository <a href="#615f20e730ad40c9ab9e1be8d9198196" title="permalink">#</a> </h3> <p> A common design pattern is the Repository pattern, although the way it's commonly implemented today has diverged somewhat from the original description in <a href="http://bit.ly/patternsofeaa">PoEAA</a>. Here, I'm going to apply it like people often do. You start by defining a new class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SqlReservationsRepository</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IDisposable</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">SqlConnection</span>&gt;&nbsp;lazyConn; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SqlReservationsRepository() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.lazyConn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">SqlConnection</span>&gt;(<span style="color:blue;">this</span>.OpenSqlConnection); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>&nbsp;OpenSqlConnection() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connStr&nbsp;=&nbsp;<span style="color:#2b91af;">ConfigurationManager</span>.ConnectionStrings[<span style="color:#a31515;">&quot;booking&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConnectionString; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlConnection</span>(connStr); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">catch</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.Dispose(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;conn; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">int</span>&nbsp;ReadReservedSeats(<span style="color:#2b91af;">DateTime</span>&nbsp;date) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;sql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;COALESCE(SUM([Quantity]),&nbsp;0)&nbsp;FROM&nbsp;[dbo].[Reservations] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;YEAR([Date])&nbsp;=&nbsp;YEAR(@Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;MONTH([Date])&nbsp;=&nbsp;MONTH(@Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;DAY([Date])&nbsp;=&nbsp;DAY(@Date)&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(sql,&nbsp;<span style="color:blue;">this</span>.lazyConn.Value)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;(<span style="color:blue;">int</span>)cmd.ExecuteScalar(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SaveReservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;dateTime, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;reservationDto) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">const</span>&nbsp;<span style="color:blue;">string</span>&nbsp;sql&nbsp;=&nbsp;<span style="color:maroon;">@&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INSERT&nbsp;INTO&nbsp;[dbo].[Reservations]&nbsp;([Date],&nbsp;[Name],&nbsp;[Email],&nbsp;[Quantity]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(@Date,&nbsp;@Name,&nbsp;@Email,&nbsp;@Quantity)&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlCommand</span>(sql,&nbsp;<span style="color:blue;">this</span>.lazyConn.Value)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;reservationDto.Date)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;reservationDto.Name)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Email&quot;</span>,&nbsp;reservationDto.Email)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlParameter</span>(<span style="color:#a31515;">&quot;@Quantity&quot;</span>,&nbsp;reservationDto.Quantity)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Dispose() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Dispose(<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">GC</span>.SuppressFinalize(<span style="color:blue;">this</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Dispose(<span style="color:blue;">bool</span>&nbsp;disposing) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(disposing) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.lazyConn.Value.Dispose(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The new SqlReservationsRepository class contains the two <code>ReadReservedSeats</code> and <code>SaveReservation</code> methods, and because you've now moved them to a class that contains a shared database connection, the methods don't need the connection as a parameter. </p> <p> The ReservationsController can use the new SqlReservationsRepository class to do its work, while keeping connection management efficient. In order to make it <em>testable</em>, however, you must make it overridable. The SqlReservationsRepository class' methods are already virtual, but that's not the class you're testing. The System Under Test is the ReservationsController class, and you have to make its use of SqlReservationsRepository overridable as well. </p> <p> If you wish to avoid Dependency Injection, you can use a <a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory Method</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:#2b91af;">ReservationDto</span>&nbsp;reservationDto) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;requestedDate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.TryParse(reservationDto.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;requestedDate)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.BadRequest(<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">using</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;repo&nbsp;=&nbsp;<span style="color:blue;">this</span>.CreateRepository()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;=&nbsp;repo.ReadReservedSeats(requestedDate); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.Capacity&nbsp;&lt;&nbsp;reservationDto.Quantity&nbsp;+&nbsp;reservedSeats) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.StatusCode(<span style="color:#2b91af;">HttpStatusCode</span>.Forbidden); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repo.SaveReservation(requestedDate,&nbsp;reservationDto); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Ok(); &nbsp;&nbsp;&nbsp;&nbsp;} } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">virtual</span>&nbsp;<span style="color:#2b91af;">SqlReservationsRepository</span>&nbsp;CreateRepository() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlReservationsRepository</span>(); }</pre> </p> <p> The Factory Method in the above example is the <code>CreateRepository</code> method, which is virtual, and thereby overridable. You can override it in a unit test like this: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PostReturnsCorrectResultAndHasCorrectStateOnAcceptableRequest() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;json&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2016-05-31&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;repo&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">SqlReservationsRepository</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;repo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.ReadReservedSeats(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2016,&nbsp;5,&nbsp;31))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(0); &nbsp;&nbsp;&nbsp;&nbsp;repo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.SaveReservation(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2016,&nbsp;5,&nbsp;31),&nbsp;json)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Verifiable(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">ReservationsController</span>&gt;&nbsp;{&nbsp;CallBase&nbsp;=&nbsp;<span style="color:blue;">true</span>&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;sut.Setup(s&nbsp;=&gt;&nbsp;s.CreateRepository()).Returns(repo.Object); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Object.Post(json); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;repo.Verify(); }</pre> </p> <p> You'll notice that not only did the complexity increase of the System Under Test, but the test itself became more complicated as well. In the previous version, at least you only needed to create a single <code>Mock&lt;T&gt;</code>, but now you have to create two different test doubles and connect them. This is a typical example demonstrating the shortcomings of the Extract and Override technique. It doesn't scale well as complexity increases. </p> <h3 id="1a73933056204b57a2d9e72a4c2afaf3"> Example: Dependency Injection <a href="#1a73933056204b57a2d9e72a4c2afaf3" title="permalink">#</a> </h3> <p> In 1994 we <a href="http://amzn.to/XBYukB">we were taught</a> to <em>favor object composition over class inheritance</em>. You can do that, and still keep your code loosely coupled with <a href="http://amzn.to/12p90MG">Dependency Injection</a>. Instead of relying on virtual methods, inject a polymorphic object into the class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ApiController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController(<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(repository&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(repository)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Capacity&nbsp;=&nbsp;12; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:#2b91af;">ReservationDto</span>&nbsp;reservationDto) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;requestedDate; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:#2b91af;">DateTime</span>.TryParse(reservationDto.Date,&nbsp;<span style="color:blue;">out</span>&nbsp;requestedDate)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.BadRequest(<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservedSeats&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Repository.ReadReservedSeats(requestedDate); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.Capacity&nbsp;&lt;&nbsp;reservationDto.Quantity&nbsp;+&nbsp;reservedSeats) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.StatusCode(<span style="color:#2b91af;">HttpStatusCode</span>.Forbidden); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Repository.SaveReservation(requestedDate,&nbsp;reservationDto); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Ok(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In this version of ReservationsController, an IReservationsRepository object is injected into the object via the constructor and saved in a class field for later use. When the <code>Post</code> method executes, it calls <code>Repository.ReadReservedSeats</code> and <code>Repository.SaveReservation</code> without further ado. </p> <p> The IReservationsRepository interface is defined like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;ReadReservedSeats(<span style="color:#2b91af;">DateTime</span>&nbsp;date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;SaveReservation(<span style="color:#2b91af;">DateTime</span>&nbsp;dateTime,&nbsp;<span style="color:#2b91af;">ReservationDto</span>&nbsp;reservationDto); }</pre> </p> <p> Perhaps you're surprised to see that it merely defines the two <code>ReadReservedSeats</code> and <code>SaveReservation</code> methods, but makes no attempt at making the interface disposable. </p> <p> Not only is IDisposable an implementation detail, but it also keeps the implementation of ReservationsController simple. Notice how it doesn't attempt to control the lifetime of the injected repository, which may or may not be disposable. In a few paragraphs, we'll return to this matter, but first, witness how the unit test became simpler as well: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;PostReturnsCorrectResultAndHasCorrectStateOnAcceptableRequest() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;json&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationDto</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2016-05-31&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;repo&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IReservationsRepository</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;repo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.ReadReservedSeats(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2016,&nbsp;5,&nbsp;31))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(0); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(repo.Object); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(json); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;repo.Verify( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&gt;&nbsp;r.SaveReservation(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(2016,&nbsp;5,&nbsp;31),&nbsp;json)); }</pre> </p> <p> With this test, you can finally use the Arrange Act Assert structure, instead of having to configure the <code>SaveReservation</code> method call in the Arrange phase. This test arranges the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Test Fixture</a> by creating a Test Double for the IReservationsRepository interface and injecting it into the ReservationsController. You only need to configure the <code>ReadReservedSeats</code> method, because there's no default behaviour that you need to suppress. </p> <p> You may be wondering about the potential memory leak when the SqlReservationsRepository is in use. After all, ReservationsController doesn't dispose of the injected repository. </p> <p> You address this concern when you compose the dependency graph. This example uses ASP.NET Web API, which has <a href="/2012/09/28/DependencyInjectionandLifetimeManagementwithASP.NETWebAPI">an extensibility point for this exact purpose</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PureCompositionRoot</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpController</span>&nbsp;Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpControllerDescriptor</span>&nbsp;controllerDescriptor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;controllerType) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>(controllerType&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ReservationsController</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;repo&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlReservationsRepository</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.RegisterForDispose(repo); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>(repo); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Unexpected&nbsp;controller&nbsp;type:&nbsp;&quot;</span>&nbsp;+&nbsp;controllerType, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(controllerType)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The call to <code>request.RegisterForDispose</code> tells the ASP.NET Web API framework to dispose of the concrete <code>repo</code> object once it has handled the request and served the response. </p> <h3 id="5be2d55540e84b199e1a0504ced1a225"> Conclusion <a href="#5be2d55540e84b199e1a0504ced1a225" title="permalink">#</a> </h3> <p> At first glance, it seems like the overall design would be easier if you make your SUT testable via inheritance, but once things become more complex, favouring composition over inheritance gives you more freedom to design according to well-known design patterns. </p> <p> In case you want to dive into the details of the code presented here, it's <a href="https://github.com/ploeh/SUTDouble">available on GitHub</a>. You can follow the progression of the code in the repository's commit history. </p> <p> If you're interested in learning more about advanced unit testing techniques, you can watch <a href="https://blog.ploeh.dk/advanced-unit-testing">my popular Pluralsight course</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. TIE fighter FsCheck properties https://blog.ploeh.dk/2016/05/17/tie-fighter-fscheck-properties 2016-05-17T10:55:00+00:00 Mark Seemann <div id="post"> <p> <em>Use the F# TIE fighter operator combination to eliminate a hard-to-name intermediary value.</em> </p> <p> A doctrine of <a href="http://amzn.to/XCJi9X">Clean Code</a> is the <em>Boy Scout Rule: leave the code cleaner than you found it</em>. Attempting to live by that rule, I'm always looking for ways to improve my code. </p> <p> Writing properties with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> is enjoyable, but I've been struggling with expressing ad-hoc Arbitraries in a clean style. Although I've already <a href="/2016/03/01/ad-hoc-arbitraries-now-with-pipes">twice written about this</a>, I've recently found another improvement. </p> <h3 id="fe4a86e755f14fffb35cc1ce85ebd2ca"> Cleaner, but not clean enough <a href="#fe4a86e755f14fffb35cc1ce85ebd2ca" title="permalink">#</a> </h3> <p> <a href="/2016/03/01/ad-hoc-arbitraries-now-with-pipes">Previously</a>, I've described how you can use the backward pipe operator to avoid enclosing a multi-line expression in brackets: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;&gt;&nbsp;3&nbsp;live&nbsp;neighbors&nbsp;dies``</span>&nbsp;(cell&nbsp;:&nbsp;<span style="color:#4ec9b0;">int</span>&nbsp;*&nbsp;<span style="color:#4ec9b0;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;nc&nbsp;=&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;[4..8]&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;nc&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;neighborCount&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;liveNeighbors&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">findNeighbors</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">take</span>&nbsp;neighborCount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">toList</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">State</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">calculateNextState</span>&nbsp;(cell&nbsp;<span style="color:navy;">::</span>&nbsp;liveNeighbors&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">set</span>)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Dead</span>&nbsp;=!&nbsp;actual</pre> </p> <p> This property uses the custom Arbitrary <code>nc</code> to express a property about <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's Game of Life</a>. The value <code>nc</code> is a <code>Arbitrary&lt;int&gt;</code>, which is a generator of integer values between 4 and 8 (both included). </p> <p> There's a problem with that code, though: I don't care about <code>nc</code>; I care about the values it creates. That's the important value in my property, and this is the reason I reserved the descriptive name <code>neighborCount</code> for that role. The property cares about the neighbour count: <em>for any neighbour count in the range 4-8, it should hold that</em> blah blah blah, and so on... </p> <p> Unfortunately, I was still left with the need to pass an <code>Arbitrary&lt;'a&gt;</code> to <code>Prop.forAll</code>, so I named it the best I could: <code>nc</code> (short for <em>Neighbour Count</em>). Following <a href="http://amzn.to/XCJi9X">Clean Code's</a> heuristics for short symbol scope, I considered it an acceptable name, albeit a bit cryptic. </p> <h3 id="cffcccca589341cd8c00f81cb67ae863"> TIE Fighters to the rescue! <a href="#cffcccca589341cd8c00f81cb67ae863" title="permalink">#</a> </h3> <p> One day, I was looking at a property like the one above, and I thought: if you consider the expression <code>Prop.forAll nc</code> in isolation, you could also write it <code>nc |&gt; Prop.forAll</code>. Does it work in this context? Yes it does: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;&gt;&nbsp;3&nbsp;live&nbsp;neighbors&nbsp;dies``</span>&nbsp;(cell&nbsp;:&nbsp;<span style="color:#4ec9b0;">int</span>&nbsp;*&nbsp;<span style="color:#4ec9b0;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;nc&nbsp;=&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;[4..8]&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;(nc&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>)&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;neighborCount&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;liveNeighbors&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">findNeighbors</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">take</span>&nbsp;neighborCount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">toList</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">State</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">calculateNextState</span>&nbsp;(cell&nbsp;<span style="color:navy;">::</span>&nbsp;liveNeighbors&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">set</span>)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Dead</span>&nbsp;=!&nbsp;actual</pre> </p> <p> This code compiles, and is equivalent to the first example. I deliberately put the expression <code>nc |&gt; Prop.forAll</code> in brackets to be sure that I'd made a correct replacement. Pipes are left-associative, though, so in this case, the brackets are redundant: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;&gt;&nbsp;3&nbsp;live&nbsp;neighbors&nbsp;dies``</span>&nbsp;(cell&nbsp;:&nbsp;<span style="color:#4ec9b0;">int</span>&nbsp;*&nbsp;<span style="color:#4ec9b0;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;nc&nbsp;=&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;[4..8]&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;nc&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;neighborCount&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;liveNeighbors&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">findNeighbors</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">take</span>&nbsp;neighborCount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">toList</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">State</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">calculateNextState</span>&nbsp;(cell&nbsp;<span style="color:navy;">::</span>&nbsp;liveNeighbors&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">set</span>)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Dead</span>&nbsp;=!&nbsp;actual</pre> </p> <p> This still works (and fails if the system under test has a defect). </p> <p> Due to <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>, though, the value <code>nc</code> is equal to the expression <code>Gen.elements [4..8] |&gt; Arb.fromGen</code>, so you can replace it: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;&gt;&nbsp;3&nbsp;live&nbsp;neighbors&nbsp;dies``</span>&nbsp;(cell&nbsp;:&nbsp;<span style="color:#4ec9b0;">int</span>&nbsp;*&nbsp;<span style="color:#4ec9b0;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;[4..8] &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;neighborCount&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;liveNeighbors&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">findNeighbors</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">take</span>&nbsp;neighborCount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">toList</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">State</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">calculateNextState</span>&nbsp;(cell&nbsp;<span style="color:navy;">::</span>&nbsp;liveNeighbors&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">set</span>)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Dead</span>&nbsp;=!&nbsp;actual</pre> </p> <p> In the above example, I've also slightly reformatted the expression, so that each expression composed with a forward pipe is on a separate line. That's not required, but I find it more readable. </p> <p> Notice how <code>Prop.forAll</code> is now surrounded by pipes: <code>|&gt; Prop.forAll &lt;|</code>. This is <a href="https://twitter.com/MattDrivenDev/status/415433890502033408">humorously called <em>TIE fighter infix</em></a>. </p> <h3 id="6f488392426e4e2f844e55011fa675a7"> Summary <a href="#6f488392426e4e2f844e55011fa675a7" title="permalink">#</a> </h3> <p> Sometimes, giving an intermediary value a descriptive name can improve code readability, but in other cases, such a value is only in the way. This was the case with the <code>nc</code> value in the first example above. Using TIE fighter infix notation enables you to get rid of a redundant, hard-to-name symbol. In my opinion, because <code>nc</code> didn't add any information to the code, I find the refactored version easier to read. I've left the code base cleaner than I found it. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. CQS and server-generated Entity IDs https://blog.ploeh.dk/2016/05/06/cqs-and-server-generated-entity-ids 2016-05-06T17:36:00+00:00 Mark Seemann <div id="post"> <p> <em>Is it a violation of Command Query Separation to update an ID property when saving an Entity to a database?</em> </p> <p> In my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID</a> course on Pluralsight, I explain how the elusive object-oriented quality <em>encapsulation</em> can be approximated by the actionable principles of <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> (CQS) and <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. </p> <p> One of the questions that invariably arise when people first learn about CQS is how to deal with (database) server-generated IDs. While I've <a href="/2014/08/11/cqs-versus-server-generated-ids">already covered that question</a>, I recently came upon <a href="http://enterprisecraftsmanship.com/2014/11/15/cqs-with-database-generated-ids/#comment-2650708342">a variation of the question</a>: <blockquote> "The Create method is a command, then if the object passed to this method have some changes in their property, does it violate any rule? I mean that we can always get the new id from the object itself, so we don't need to return another integer. Is it good or bad practice?" </blockquote> In this article, I'll attempt to answer this question. </p> <h3 id="0a85c24e528a4778aa351d2345ff0c8f"> Returning an ID by mutating input <a href="#0a85c24e528a4778aa351d2345ff0c8f" title="permalink">#</a> </h3> <p> I interpret the question like this: an Entity (as described in <a href="http://amzn.to/WBCwx7">DDD</a>) can have a mutable <code>Id</code> property. A <cdata>Create</cdata> method could save the Entity in a database, and then update the input value's <code>Id</code> property with the newly created record's ID. </p> <p> As an example, consider this User class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">User</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;FirstName&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;LastName&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> In order to create a new User in your database, you could define an API like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Create(<span style="color:#2b91af;">User</span>&nbsp;user); }</pre> </p> <p> An implementation of <code>IUserRepository</code> based on a relational database could perform an <code>INSERT</code> into the appropriate database, get the ID of the created record, and update the User object's <code>Id</code> property. </p> <p> This test snippet demonstrates that behaviour: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;u&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">User</span>&nbsp;{&nbsp;FirstName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Jane&quot;</span>,&nbsp;LastName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Doe&quot;</span>&nbsp;}; <span style="color:#2b91af;">Assert</span>.Equal(0,&nbsp;u.Id); repository.Create(u); <span style="color:#2b91af;">Assert</span>.NotEqual(0,&nbsp;u.Id);</pre> </p> <p> When you create the <code>u</code> object, by not assigning a value to the <code>Id</code> property, it will have the default value of 0. Only after the <code>Create</code> method returns does <code>Id</code> hold a proper value. </p> <h3 id="5018d830da5246b69a28e43fd21cbb1f"> Evaluation <a href="#5018d830da5246b69a28e43fd21cbb1f" title="permalink">#</a> </h3> <p> Does this design adhere to CQS? Yes, it does. The <code>Create</code> method doesn't return a value, but rather changes the state of the system. Not only does it create a record in your database, but it also changes the state of the <code>u</code> object. Nowhere does CQS state that an operation can't change more than a single part of the system. </p> <p> Is it good design, then? I don't think that it is. </p> <p> First, the design violates another part of encapsulation: protection of invariants. The invariants of any Entity is that is has an ID. It is the single defining feature of Entities that they have enduring and stable identities. When you change the identity of an Entity, it's no longer the same Entity. Yet this design allows exactly that: </p> <p> <pre>u.Id&nbsp;=&nbsp;42; <span style="color:green;">//&nbsp;...</span> u.Id&nbsp;=&nbsp;1337;</pre> </p> <p> Second, such a design puts considerable trust in the implicit protocol between any client and the implementation of <code>IUserRepository</code>. Not only must the <code>Create</code> method save the <code>User</code> object in a data store, but it must also update the <code>Id</code>. </p> <p> What happens, though, if you replay the method call: </p> <p> <pre>repository.Create(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">User</span>&nbsp;{&nbsp;FirstName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Ada&quot;</span>,&nbsp;LastName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Poe&quot;</span>&nbsp;}); repository.Create(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">User</span>&nbsp;{&nbsp;FirstName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Ada&quot;</span>,&nbsp;LastName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Poe&quot;</span>&nbsp;});</pre> </p> <p> This will result in duplicated entries, because the repository can't detect whether this is a replay, or simply two new users with the same name. You may find this example contrived, but in these days of cloud-based storage, it's common to apply retry strategies to clients. </p> <p> If you use <a href="/2014/08/11/cqs-versus-server-generated-ids">one of the alternatives I previously outlined</a>, you will not have this problem. </p> <h3 id="379c0a435dc041d29413a56c6bfbf736"> Invariants <a href="#379c0a435dc041d29413a56c6bfbf736" title="permalink">#</a> </h3> <p> Even if you don't care about replays or duplicates, you should still consider the invariants of Entities. As a minimum, you shouldn't be able to change the ID of an Entity. For a class like <code>User</code>, it'll also make client developers' job easier if it can provide some useful guarantees. This is part of Postel's law applied: be conservative in what you send. In this case, at least guarantee that no values will be null. Thus, a better (but by no means perfect) User class could be: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">User</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;User(<span style="color:blue;">int</span>&nbsp;id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;&lt;=&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentOutOfRangeException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(id), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;The&nbsp;ID&nbsp;must&nbsp;be&nbsp;a&nbsp;(unique)&nbsp;positive&nbsp;value.&quot;</span>);; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Id&nbsp;=&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.firstName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.lastName&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Id&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">string</span>&nbsp;firstName;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;FirstName &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.firstName;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">set</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">value</span>&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="color:blue;">value</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.firstName&nbsp;=&nbsp;<span style="color:blue;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">string</span>&nbsp;lastName; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;LastName &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.lastName;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">set</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">value</span>&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(<span style="color:blue;">value</span>)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.lastName&nbsp;=&nbsp;<span style="color:blue;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The constructor initialises all class fields, and ensure that <code>Id</code> is a positive integer. It can't ensure that the ID is unique, though - at least, not without querying the database, which would introduce other problems. </p> <p> How do you create a new <code>User</code> object and save it in the database, then? </p> <p> One option is this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Create(<span style="color:blue;">string</span>&nbsp;firstName,&nbsp;<span style="color:blue;">string</span>&nbsp;lastName); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;Read(<span style="color:blue;">int</span>&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Update(<span style="color:#2b91af;">User</span>&nbsp;user); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Delete(<span style="color:blue;">int</span>&nbsp;id); }</pre> </p> <p> The <code>Create</code> method doesn't use the <code>User</code> class at all, because at this time, the Entity doesn't yet exist. It only exists once it has an ID, and in this scenario, this only happens once the database has stored it and assigned it an ID. </p> <p> Other methods can still use the <code>User</code> class as either input or output, because once the Entity has an ID, its invariants are satisfied. </p> <p> There are other problems with this design, though, so I still prefer <a href="/2014/08/11/cqs-versus-server-generated-ids">the alternatives I originally sketched</a>. </p> <h3 id="8e46171ac6574e3098141dfd4c44c3e6"> Conclusion <a href="#8e46171ac6574e3098141dfd4c44c3e6" title="permalink">#</a> </h3> <p> Proper object-oriented design should, as a bare minimum, consider <em>encapsulation</em>. This includes protecting the invariants of objects. For Entities, it means that an Entity must always have a stable and enduring identity. Well-designed Entities guarantee that identity values are always present and immutable. This requirement tend to be at odds with most popular Object-Relational Mappers (ORM), which require that ID fields are externally assignable. In order to use ORMs, most programmers compromise on encapsulation, ending up with systems that are relational in nature, but far from object-oriented. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="34a1cc1aebf84e169f518352d3bf19e8"> <div class="comment-author"><a href="http://enterprisecraftsmanship.com/">Vladimir Khorikov</a> <a href="#34a1cc1aebf84e169f518352d3bf19e8">#</a></div> <div class="comment-content"> <p> >" This includes protecting the invariants of objects. For Entities, it means that an Entity must always have a stable and enduring identity." </p> <p> Great points. </p> <p> However, how would you get from <em>void Create(string firstName, string lastName)</em> to <em>User Read(int id)</em> especially if an entity doesn't have a natural key? </p> </div> <div class="comment-date">2016-05-06 18:26 UTC</div> </div> <div class="comment" id="b46a79d34936452d84421cc8759afe75"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b46a79d34936452d84421cc8759afe75">#</a></div> <div class="comment-content"> <p> Vladimir, thank you for writing. Exactly how you get the ID depends on the exact requirements of your application. As an example, if you're developing a web application, you may simply be creating the user, and then that's the end of the HTTP request. Meanwhile, asynchronously, a background process is performing the actual database insertion and subsequently sends an email to the user with a link to click in order to verify the email. In that link is the generated ID, so when your server receives the next request, you already know the ID. </p> <p> That may not always be a practical strategy, but I started to describe this technique, because my experience with CQRS and REST tells me that you have lots of options for communicating state and identity. Still, if you need an ID straight away, you can always pass a GUID as a correlation ID, so that you can later find the Entity you created. That's the technique I <a href="/2014/08/11/cqs-versus-server-generated-ids">originally described</a>. </p> </div> <div class="comment-date">2016-05-06 19:04 UTC</div> </div> <div class="comment" id="1ce9248e341c444a8978b8d79cf37476"> <div class="comment-author"><a href="http://enterprisecraftsmanship.com/">Vladimir Khorikov</a> <a href="#1ce9248e341c444a8978b8d79cf37476">#</a></div> <div class="comment-content"> <p> Thank you for the reply. </p> <p> BTW, ORMs don't require you to make Ids externally assignable. You can have ORMs assign Ids for you internally, and that's a preferred design in most cases. Also, you can make the Id setters non-public and thus protect entities' encapsulation. There still are issues, of course. Namely, entities don't have established identities until they are saved to the DB or added to the Context/Session. But this issue isn't that big comparing to what you described in the post. </p> </div> <div class="comment-date">2016-05-06 19:25 UTC</div> </div> <div class="comment" id="56a92a0784fa45d1af8f538a548d464b"> <div class="comment-author">Ethan Nelson <a href="#56a92a0784fa45d1af8f538a548d464b">#</a></div> <div class="comment-content"> <p> I was skimming through the comments and had this nagging suspicion that the entire topic of, "how do I get my ID with CQS", was a false dichotomy. The premise that the "ID" stored in the database is the "identity" of the object or entity I think can be challenged. </p> <p> I prefer to look at the "ID" as purely database implementation details. Assuming the repository is SQL, the key type preferred by db admins will be the smallest-reasonable monotonically increasing ID... namely INT. But switch to Amazon DynamoDB and that conclusion flies out the window. With their totally managed and distributed indexes and hashing algorithms... GUID's would be fantastic. </p> <p> What I WISH developers would do is take the time to think about their Domain and form their own understanding of what truly defines the identity of the entity. Whatever decision they make, it translates to a "natural key" for persistence. </p> <p> Natural keys are great because a developer has promised, "these combinations of properties uniquely identify entities of type T". The result is unique indexes that can have dramatic performance benefits... outside the application benefit of being able to construct your repository like this: <pre style="overflow:auto;font-family:consolas;font-size:13px;padding:9.5px;margin-top:0px;margin-bottom:10px;line-height:1.42857;word-break:break-all;word-wrap:break-word;border:1px solid rgb(204,204,204);border-radius:4px;background-color:rgb(245,245,245)"><span style="color:blue">public</span>&nbsp;<span style="color:blue">interface</span>&nbsp;<span style="color:rgb(43,145,175)">IRepository</span>&lt;<wbr>T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue">void</span>&nbsp;Create(T&nbsp;item); }</pre> <p> As for the "ID" field? Who cares?... that's up to the database. The developer already knows exactly how to find this T. I wouldn't even include it on the domain model object. </p> <p> This would give db admins options. We can look at the natural key size, evaluate how it is queried and order it appropriately. We can decide if the nature of typical joins merit clustering (as is common with Header -> Detail style records). If needed, we can optimize lookup of large keys through stored procedures and computed persisted checksum columns. Nullability and default values become part of design discussions on the front end. Finally, if we feel like it... we can assign a surrogate key, or use a GUID. The developer doesn't care, our dba's do. </p> <p> Thinking back to my last 6 or so development projects, I've never had to "get the id". The next operation is always a matter of query or command on unique criteria I was responsible for creating in the first place. This approach makes my dba's happy. </p> </div> <div class="comment-date">2016-05-10 15:06 UTC</div> </div> <div class="comment" id="0dee5ebc7f9a48658630359df990520e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0dee5ebc7f9a48658630359df990520e">#</a></div> <div class="comment-content"> <p> Ethan, thank you for your comment; I agree with 90 percent of what you wrote, which is also the reason I prefer the options I <a href="/2014/08/11/cqs-versus-server-generated-ids">previously outlined</a>. </p> <p> Entities don't always have 'natural IDs', though, and even when they have, experience has taught me not to use them. Is a person's email address a natural ID? It is, but people change email addresses. </p> <p> Here in Denmark, for many years many public software systems have used the <a href="https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)">Danish personal identification numbers</a> as natural keys, as they were assumed to be nationally unique and stable. These numbers encode various information about a person, such as birth-date and sex. A few inhabitants, however, get sex change operations, and then desire a new number that corresponds to their new sex. Increasingly, these requests are being granted. New ID, same person. </p> <p> I've seen such changes happen so many times during my career that I've become distrustful of 'natural IDs'. If I need to identify an Entity, I usually attach a GUID to it. </p> </div> <div class="comment-date">2016-05-10 20:04 UTC</div> </div> <div class="comment" id="33151ad98fad4e8ab4e778197ec6303f"> <div class="comment-author">Ethan Nelson <a href="#33151ad98fad4e8ab4e778197ec6303f">#</a></div> <div class="comment-content"> <p> With regard to entity identification, I'm not sure stability matters. Who cares if the email address changes... as long as it is unique? Uniqueness is sufficient for the application to identify the entity. I agree from a strictly database standpoint; I'm not thrilled about assigning a primary key to a natural key due to the stability issue across relationships. But that does not invalidate the key for use as an identity in my mind. Both indexes can exist, both be unique, and each with their own purpose. </p> <p> Also, it is difficult for me to envision a scenario where there is no natural key. If there are two rows where the only difference is an auto-incrementing integer, or different guids... what is the meaning of one row versus the next? That meaning cannot, by definition, be contained in the surrogate keys... they add nothing to the business meaning of the entity. </p> <p> I feel I've derailed the discussion into database modeling theory, and we are talking about CQS with "create the entity" as a stimulating use case. Suffice it to say, IMHO, if (a, b) is unique today according to business rule, than it should be my unique identity from a developer perspective. By leaving row identifiers up to DBA's and dropping them from my domain model entirely, I minimize impact to those technical implementations when the business rules change such that (a, b, c) becomes the new unique, and, preserve CQS in the process. </p> </div> <div class="comment-date">2016-05-10 23:41 UTC</div> </div> <div class="comment" id="1188ea0d07674d2e862727cdac07c21b"> <div class="comment-author">Endy Tjahjono <a href="#1188ea0d07674d2e862727cdac07c21b">#</a></div> <div class="comment-content"> <p> SQL Server has a feature called 'sequence' (from 2012 and above if I'm not mistaken). We can use sequence to generate unique int ID instead of using identity column. The sequence can be called without inserting row to the table. We can create a service that call this sequence to generate the next running number. The UI code can call this running number generator service to get a new ID, and then send the new entity complete with ID to the create service. </p> </div> <div class="comment-date">2017-02-06 07:19:00 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Async as surrogate IO https://blog.ploeh.dk/2016/04/11/async-as-surrogate-io 2016-04-11T12:38:00+00:00 Mark Seemann <div id="post"> <p> <em>Haskell defines IO as an explicit context for handling effects. In F#, you can experiment with using Async as a surrogate.</em> </p> <p> As a reaction to <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">my article on the relationship between Functional architecture and Ports and Adapters</a>, <a href="http://simon-says-architecture.com">Szymon Pobiega</a> suggested on Twitter: <blockquote> <a href="https://twitter.com/SzymonPobiega/status/713001580077965312">random idea: in c# “IO Int” translates to “Task&lt;int&gt;”. By consistently using this we can make type system help you even in c#</a> </blockquote> If you know Haskell, you might object to that statement, but I don't think Szymon meant it literally. It's an idea worth exploring, I think. </p> <h3 id="aa255ffa2808474d82ba51b478aa822d"> Async as an IO surrogate <a href="#aa255ffa2808474d82ba51b478aa822d" title="permalink">#</a> </h3> <p> Functions in Haskell are, by default, <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>. Whenever you need to do something impure, like querying a database or sending an email, you need to explicitly do this in an impure context: <code>IO</code>. Don't let the name <code>IO</code> mislead you: it's not only for input and output, but for all impure actions. As an example, random number generation is impure as well, because a function that returns a new value every time isn't pure. (There are ways to model random number generation with pure functions, but let's forget about that for now.) </p> <p> F#, on the other hand, doesn't make the distinction between pure and impure functions in its type system, so nothing 'translates' from <code>IO</code> in Haskell into an equivalent type in F#. Still, it's worth looking into Szymon's suggestion. </p> <p> In 2013, C# finally caught up with F#'s support for asynchronous work-flows, and since then, many IO-bound .NET APIs have gotten asynchronous versions. The underlying API for C#'s <code>async</code>/<code>await</code> support is the <a href="https://msdn.microsoft.com/en-us/library/dd460717">Task Parallel Library</a>, which isn't particularly constrained to IO-bound operations. The use of <code>async</code>/<code>await</code>, however, makes most sense for IO-bound operations. It improves resource utilisation because it can free up threads while waiting for IO-bound operations to complete. </p> <p> It seems to be a frequent observation that once you start using asynchronous operations, they tend to be 'infectious'. A common rule of thumb is <a href="https://msdn.microsoft.com/en-us/magazine/jj991977.aspx">async all the way</a>. You can easily call synchronous methods from asynchronous methods, but it's harder the other way around. </p> <p> This is similar to the distinction between pure and impure functions. You can call pure functions from impure functions, but not the other way around. If you call an impure function from a 'pure' function in F#, the caller also automatically becomes impure. In Haskell, it's not even possible; you can call a pure function from an <code>IO</code> context, but not the other way around. </p> <p> In F#, Async&lt;'a&gt; is more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> than using Task&lt;T&gt;. While Async&lt;'a&gt; doesn't translate directly to Haskell's <code>IO</code> either, it's worth experimenting with the analogy. </p> <h3 id="c397291ccee4424c885f301122f96caa"> Async I/O <a href="#c397291ccee4424c885f301122f96caa" title="permalink">#</a> </h3> <p> In <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">the previous Haskell example</a>, communication with the database was implemented by functions returning <code>IO</code>: </p> <p> <pre>getReservedSeatsFromDB :: ConnectionString -> ZonedTime -> IO Int saveReservation :: ConnectionString -> Reservation -> IO ()</pre> </p> <p> By applying the analogy of <code>IO</code> to <code>Async&lt;'a&gt;</code>, you can implement your F# data access module to return Async work-flows: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:#4ec9b0;">SqlGateway</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;DateTimeOffset&nbsp;-&gt;&nbsp;Async&lt;int&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString&nbsp;(date&nbsp;:&nbsp;<span style="color:#4ec9b0;">DateTimeOffset</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;Reservation&nbsp;-&gt;&nbsp;Async&lt;unit&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">saveReservation</span>&nbsp;connectionString&nbsp;(reservation&nbsp;:&nbsp;<span style="color:#4ec9b0;">Reservation</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span></pre> </p> <p> This makes it harder to compose the desired <code>imp</code> function, because you need to deal with both asynchronous work-flows and the Either monad. In Haskell, the building blocks are already there in the shape of the <code>EitherT</code> <a href="https://en.wikipedia.org/wiki/Monad_transformer">monad transformer</a>. F# doesn't have monad transformers, but in the spirit of the <a href="/2016/03/21/composition-with-an-either-computation-expression">previous article</a>, you can define a computation expression that combines Either and Async: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">AsyncEitherBuilder</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Async&lt;Result&lt;&#39;a,&#39;c&gt;&gt;&nbsp;*&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Async&lt;Result&lt;&#39;b,&#39;c&gt;&gt;)</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;-&gt;&nbsp;Async&lt;Result&lt;&#39;b,&#39;c&gt;&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Bind</span>(x,&nbsp;<span style="color:navy;">f</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">async</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;x&#39;&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return!</span>&nbsp;<span style="color:navy;">f</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;f&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;f&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;a</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">ReturnFrom</span>&nbsp;x&nbsp;=&nbsp;x <span style="color:blue;">let</span>&nbsp;asyncEither&nbsp;=&nbsp;<span style="color:#4ec9b0;">AsyncEitherBuilder</span>&nbsp;()</pre> </p> <p> This is the minimal implementation required for the following composition. A more complete implementation would also define a Return method, as well as <a href="https://msdn.microsoft.com/en-us/library/dd233182">other useful methods</a>. </p> <p> In the Haskell example, you saw how I had to use <code>hoistEither</code> and <code>liftIO</code> to 'extract' the values within the <code>do</code> block. This is necessary because sometimes you need to pull a value out of an Either value, and sometimes you need to get the value inside an <code>IO</code> context. </p> <p> In an <code>asyncEither</code> expression, you need similar functions: </p> <p> <pre><span style="color:green;">//&nbsp;Async&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Async&lt;Result&lt;&#39;a,&#39;b&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">liftAsync</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">async</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;x&#39;&nbsp;} <span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">asyncReturn</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">async</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;x&nbsp;}</pre> </p> <p> I chose to name the first one <code>liftAsync</code> as a counterpart to Haskell's <code>liftIO</code>, since I'm using <code>Async&lt;'a&gt;</code> as a surrogate for <code>IO</code>. The other function I named <code>asyncReturn</code> because it returns a pure value wrapped in an asynchronous work-flow. </p> <p> You can now compose the desired <code>imp</code> function from the above building blocks: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;candidate&nbsp;=&nbsp;asyncEither&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;r&nbsp;=&nbsp;<span style="color:#4ec9b0;">Validate</span>.<span style="color:navy;">reservation</span>&nbsp;candidate&nbsp;|&gt;&nbsp;<span style="color:navy;">asyncReturn</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;i&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString&nbsp;r.Date &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">liftAsync</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;r&nbsp;=&nbsp;<span style="color:#4ec9b0;">Capacity</span>.<span style="color:navy;">check</span>&nbsp;10&nbsp;i&nbsp;r&nbsp;|&gt;&nbsp;<span style="color:navy;">asyncReturn</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">saveReservation</span>&nbsp;connectionString&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">liftAsync</span>&nbsp;}</pre> </p> <p> Notice how, in contrast with the <a href="/2016/03/21/composition-with-an-either-computation-expression">previous example</a>, this expression uses <code>let!</code> and <code>return!</code> throughout, but that you have to compose each line with <code>asyncReturn</code> or <code>liftAsync</code> in order to pull out the appropriate values. </p> <p> As an example, the original, unmodified <code>Validate.reservation</code> function has the type <code>ReservationRendition -&gt; Result&lt;Reservation, Error&gt;</code>. Inside an <code>asyncEither</code> expression, however, all <code>let!</code>-bound values must be of the type <code>Async&lt;Result&lt;'a, 'b&gt;&gt;</code>, so you need to wrap the return value <code>Result&lt;Reservation, Error&gt;</code> in Async. That's what <code>asyncReturn</code> does. Since this expression is <code>let!</code>-bound, the <code>r</code> value has the type <code>Reservation</code>. It's 'double-unwrapped', if you will. </p> <p> Likewise, <code>SqlGateway.getReservedSeats</code> returns a value of the type <code>Async&lt;int&gt;</code>, so you need to pipe it into <code>liftAsync</code> in order to turn it into an <code>Async&lt;Result&lt;int, Error&gt;&gt;</code>. When that value is <code>let!</code>-bound, then, <code>i</code> has the type <code>int</code>. </p> <p> There's one change compared to the previous examples: the type of <code>imp</code> changed. It's now <code>ReservationRendition -&gt; Async&lt;Result&lt;unit, Error&gt;&gt;</code>. While you could write an adapter function that executes this function synchronously, it's better to recall the mantra: <em>async all the way!</em> </p> <h3 id="099efd8e660d457abdc83d240ac848b7"> Async Controller <a href="#099efd8e660d457abdc83d240ac848b7" title="permalink">#</a> </h3> <p> Instead of coercing the <code>imp</code> function to be synchronous, you can change the ReservationsController that uses it to be asynchronous as well: </p> <p> <pre><span style="color:green;">//&nbsp;ReservationRendition&nbsp;-&gt;&nbsp;Task&lt;IHttpActionResult&gt;</span> <span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>(rendition&nbsp;:&nbsp;<span style="color:#4ec9b0;">ReservationRendition</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;res&nbsp;=&nbsp;<span style="color:navy;">imp</span>&nbsp;rendition &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;res&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationError</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">CapacityExceeded</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;this.<span style="color:navy;">StatusCode</span>&nbsp;<span style="color:#4ec9b0;">HttpStatusCode</span>.Forbidden &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Async</span>.<span style="color:navy;">StartAsTask</span></pre> </p> <p> Notice that the method body uses an <code>async</code> computation expression, instead of the new <code>asyncEither</code>. This is because, when returning a result, you finally need to explicitly deal with the error cases as well as the success case. Using a standard <code>async</code> expression enables you to <code>let!</code>-bind <code>res</code> to the result of invoking the asynchronous <code>imp</code> function, and pattern match against it. </p> <p> Since ASP.NET Web API support asynchronous controllers, this works when you convert the async work-flow to a Task with <code>Async.StartAsTask</code>. <em>Async all the way</em>. </p> <p> In order to get this to work, I had to overload the BadRequest, StatusCode, and Ok methods, because they are protected, and you can't use protected methods from within a closure. Here's the entire code for the Controller, including the above Post method for completeness sake: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">ReservationsController</span>(<span style="color:navy;">imp</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:#4ec9b0;">ApiController</span>() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:blue;">private</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;(msg&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">base</span>.<span style="color:navy;">BadRequest</span>&nbsp;msg&nbsp;:&gt;&nbsp;<span style="color:#4ec9b0;">IHttpActionResult</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:blue;">private</span>&nbsp;this.<span style="color:navy;">StatusCode</span>&nbsp;statusCode&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">base</span>.<span style="color:navy;">StatusCode</span>&nbsp;statusCode&nbsp;:&gt;&nbsp;<span style="color:#4ec9b0;">IHttpActionResult</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:blue;">private</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;=&nbsp;<span style="color:blue;">base</span>.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;:&gt;&nbsp;<span style="color:#4ec9b0;">IHttpActionResult</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;ReservationRendition&nbsp;-&gt;&nbsp;Task&lt;IHttpActionResult&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Post</span>(rendition&nbsp;:&nbsp;<span style="color:#4ec9b0;">ReservationRendition</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">async</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;res&nbsp;=&nbsp;<span style="color:navy;">imp</span>&nbsp;rendition &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;res&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;(<span style="color:navy;">ValidationError</span>&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;this.<span style="color:navy;">BadRequest</span>&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">CapacityExceeded</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;this.<span style="color:navy;">StatusCode</span>&nbsp;<span style="color:#4ec9b0;">HttpStatusCode</span>.Forbidden &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">return</span>&nbsp;this.<span style="color:navy;">Ok</span>&nbsp;()&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Async</span>.<span style="color:navy;">StartAsTask</span></pre> </p> <p> As you can see, apart from the overloaded methods, the Post method is all there is. It uses the injected <code>imp</code> function to perform the actual work, asynchronously translates the result into a proper HTTP response, and returns it as a <code>Task&lt;IHttpActionResult&gt;</code>. </p> <h3 id="fa4cc3479e9c41b697fc50c3caef3b63"> Summary <a href="#fa4cc3479e9c41b697fc50c3caef3b63" title="permalink">#</a> </h3> <p> The initial idea was to experiment with using Async as a surrogate for IO. If you want to derive any value from this convention, you'll need to be disciplined and make all impure functions return Async&lt;'a&gt; - even those not IO-bound, but impure for other reasons (like random number generators). </p> <p> I'm not sure I'm entirely convinced that this would be a useful strategy to adopt, but on the other hand, I'm not deterred either. At least, the outcome of this experiment demonstrates that you can combine asynchronous work-flows with the Either monad, and that the code is still manageable, with good separation of concerns. Since it <em>is</em> a good idea to access IO-bound resources using asynchronous work-flows, it's nice to know that they can be combined with sane error handling. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="75cd7e6bef364a328e0135f14dc06ab8"> <div class="comment-author"><a href="http://diogocastro.com">Diogo Castro</a> <a href="#75cd7e6bef364a328e0135f14dc06ab8">#</a></div> <div class="comment-content"> <p> In my opinion, the true purpose of <code>IO</code> is to allow you to write <em>pure</em> effectful functions. For example, Haskell's <code>putStrLn</code> is referentially transparent - i.e., <code>[putStrLn "hello", putStrLn "hello"]</code> has the same meaning as <code>replicate 2 $ putStrLn "hello"</code>. This means you can treat a function that returns an IO the same way you'd treat any other function. </p> <p> This is possible for two reasons: 1) <code>IO</code> actions are not run until they hit the "edge of the universe" where <code>unsafePerformIO</code> is called and 2) they don't memoize the result - if an <code>IO</code> action is executed twice, it will calculate two possibly distinct values. </p> <p> The problem with <code>Task</code> is that it's not referentially transparent. </p> <p> <code>new[] { Task.Run(() => WriteLine("hello")), Task.Run(() => WriteLine("hello")) }</code> is not the same as <code>var t = Task.Run(() => WriteLine("hello"))</code> + <code>new [] { t, t }</code>. Even if you use <code>new Task()</code> instead of <code>Task.Run</code> to delay its execution, you still wouldn't be able to factor out the task because it memoizes the result (in this case, void), so you'd still see "hello" only once on the screen. </p> <p> Since Task cannot be used to model pure functions, I don't think it's a suitable replacement for IO. </p> </div> <div class="comment-date">2016-04-16 15:55 UTC</div> </div> <div class="comment" id="093a7032a754405a80bc4382d1e2cab9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#093a7032a754405a80bc4382d1e2cab9">#</a></div> <div class="comment-content"> <p> Diogo, thank you for writing. The point about Tasks not being referentially transparent is well taken. You are, indeed, correct. </p> <p> F# asynchronous work-flows work differently, though. Extending the experiment outlined in this article, you can attempt to 'translate' <code>putStrLn</code> to F#. Since the return type of <code>putStrLn</code> is <code>IO ()</code>, you'll need a function that returns <code>Async&lt;unit&gt;</code>. Here's my naive attempt: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">aprintfn</span>&nbsp;fmt&nbsp;=&nbsp;<span style="color:blue;">async</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:navy;">printfn</span>&nbsp;fmt&nbsp;} </pre> </p> <p> Corresponding to your two Haskell examples, consider, then, these two F# values: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;explicitSeq&nbsp;=&nbsp;<span style="color:blue;">seq</span>&nbsp;{&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:navy;">aprintfn</span>&nbsp;<span style="color:#a31515;">&quot;hello&quot;</span>;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:navy;">aprintfn</span>&nbsp;<span style="color:#a31515;">&quot;hello&quot;</span>&nbsp;} <span style="color:blue;">let</span>&nbsp;replicatedSeq&nbsp;=&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">replicate</span>&nbsp;2&nbsp;&lt;|&nbsp;<span style="color:navy;">aprintfn</span>&nbsp;<span style="color:#a31515;">&quot;hello&quot;</span></pre> </p> <p> Both of these values have the type <code>seq&lt;Async&lt;unit&gt;&gt;</code>, and I'd expect them to be equivalent. </p> <p> In Haskell, I used <a href="http://hackage.haskell.org/package/base-4.8.2.0/docs/Data-Traversable.html#v:sequence"><code>sequence</code></a> to run your code in GHCI, turning <code>[IO ()]</code> into <code>IO [()]</code>. In F#, you can do something similar: </p> <p> <pre><span style="color:green;">//&nbsp;seq&lt;Async&lt;&#39;a&gt;&gt;&nbsp;-&gt;&nbsp;Async&lt;seq&lt;&#39;a&gt;&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">asequence</span>&nbsp;xs&nbsp;=&nbsp;<span style="color:blue;">async</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:#4ec9b0;">Async</span>.<span style="color:navy;">RunSynchronously</span>&nbsp;xs&nbsp;}</pre> </p> <p> Using this function, you can evaluate both expressions: </p> <p> <pre>> asequence explicitSeq |> Async.RunSynchronously;; hello hello val it : seq&lt;unit&gt; = seq [null; null] > asequence replicatedSeq |> Async.RunSynchronously;; hello hello val it : seq&lt;unit&gt; = seq [null; null]</pre> </p> <p> As you can see, the result is the same in both cases, like you'd expect them to be. </p> <p> I'm still not claiming that <code>Async&lt;'a&gt;</code> is a direct translation of Haskell's <code>IO</code>, but there are similarities. Admittedly, I've never considered whether F# asynchronous work-flows satisfy the monad laws, but it wouldn't surprise me if they did. </p> </div> <div class="comment-date">2016-04-20 14:55 UTC</div> </div> <div class="comment" id="ccc43e922bd545b7bb268c30ec15b7b8"> <div class="comment-author"><a href="http://diogocastro.com">Diogo Castro</a> <a href="#ccc43e922bd545b7bb268c30ec15b7b8">#</a></div> <div class="comment-content"> <p> Ah, I see! It wasn't clear to me that async workflows were referentially transparent. That being the case, it seems like they are, indeed, a good substitute for <code>IO a</code>. </p> <p> It seems like the F# team got async right :) The C# team should take a page from their book... </p> </div> <div class="comment-date">2016-05-04 23:05 UTC</div> </div> <div class="comment" id="eedafbbb1e8249a8b4ac56fd3a34a5ad"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#eedafbbb1e8249a8b4ac56fd3a34a5ad">#</a></div> <div class="comment-content"> <p> I know what functional programmers generally and Mark specially mean when they say that function is (or is not) referentially transparent. Namely, a function is referentially transparent if and only if its behavior is unchanged when <a href="https://en.wikipedia.org/wiki/Memoization">memozied</a>. </p> <p> I see how caching behavior varies among Haskell's IO, F#'s Async, and C#'s Task. However, the previous comments are the only times that I have anyone say that a type is (or is not) referentially transparent. In general, what does it mean for a type to be (or not to be) referentially transparent? </p> </div> <div class="comment-date">2020-08-02 15:02 UTC</div> </div> <div class="comment" id="17fe9fd1f1f84ae59348954b4ef328da"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#17fe9fd1f1f84ae59348954b4ef328da">#</a></div> <div class="comment-content"> <p> Tyson, language is imprecise. I take it that we meant that the behaviour that the type (here: an <em>object</em>) affords helps preserve referential transparency. It's fairly clear to me that it makes sense to say that <code>Task&lt;T&gt;</code> isn't referentially transparent, because of the behaviour shown by the above examples. </p> <p> In C# or F#, we can't <em>universally</em> state that <code>Async</code> is always referentially transparent, because these languages allow impure actions to be passed to the type's members. Still, if we pass pure functions, the members are referentially transparent too. </p> <p> In object-oriented programming, we often think of a type as an object: <em>data with behaviour</em>. In Haskell, you often have a type and an associated type class. In F#, some containers (like <code>Async</code>) have an associated computation expression builder; again, that's a set of functions. </p> <p> If a set of associated functions or members are all referentially transparent, it makes sense to me to talk about the type as being referentially transparent, but all this is a view I've just retconned as I'm writing. You may be able to poke holes in it, so don't take it for more than it is. </p> </div> <div class="comment-date">2020-08-06 8:48 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Composition with an Either computation expression https://blog.ploeh.dk/2016/03/21/composition-with-an-either-computation-expression 2016-03-21T08:34:00+00:00 Mark Seemann <div id="post"> <p> <em>A simple example of a composition using an F# computation expression.</em> </p> <p> In <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">a previous article</a>, you saw how to compose an application feature from functions: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Validate</span>.<span style="color:navy;">reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString&nbsp;r.Date,&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">bind</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;(i,&nbsp;r)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#4ec9b0;">Capacity</span>.<span style="color:navy;">check</span>&nbsp;10&nbsp;i&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">map</span>&nbsp;(<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">saveReservation</span>&nbsp;connectionString)</pre> </p> <p> This is awkward because of the need to carry the result of Validate.reservation (<code>r</code>) on to Capacity.check, while also needing it for SqlGateway.getReservedSeats. In this article, you'll see a neater, alternative syntax. </p> <h3 id="235ccce90e5342d39f8bc12400797938"> Building blocks <a href="#235ccce90e5342d39f8bc12400797938" title="permalink">#</a> </h3> <p> To recapitulate the context, you have this <code>Result</code> discriminated union from <a href="http://fsharpforfunandprofit.com">Scott Wlaschin's</a> article about <a href="http://fsharpforfunandprofit.com/posts/recipe-part2">railway-oriented programming</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Result</span>&lt;&#39;TSuccess,&#39;TFailure&gt;&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;TSuccess &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:blue;">of</span>&nbsp;&#39;TFailure</pre> </p> <p> A type with this structure is also sometimes known as <em>Either</em>, because the result can be either a success or a failure. </p> <p> The above example also uses bind and map functions, which are implemented this way: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Result&lt;&#39;b,&#39;c&gt;)&nbsp;-&gt;&nbsp;Result&lt;&#39;a,&#39;c&gt;&nbsp;-&gt;&nbsp;Result&lt;&#39;b,&#39;c&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;x&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;f&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;f <span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Result&lt;&#39;a,&#39;c&gt;&nbsp;-&gt;&nbsp;Result&lt;&#39;b,&#39;c&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;x&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Success</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Success</span>(<span style="color:navy;">f</span>&nbsp;s) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Failure</span>&nbsp;f&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;f</pre> </p> <p> With these building blocks, it's trivial to implement a <a href="https://msdn.microsoft.com/en-us/library/dd233182.aspx">computation expression</a> for the type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">EitherBuilder</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Bind</span>(x,&nbsp;<span style="color:navy;">f</span>)&nbsp;=&nbsp;<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">ReturnFrom</span>&nbsp;x&nbsp;=&nbsp;x</pre> </p> <p> You can add more members to EitherBuilder if you need support for more advanced syntax, but this is the minimal implementation required for the following example. </p> <p> You also need a value of the EitherBuilder type: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;either&nbsp;=&nbsp;<span style="color:#4ec9b0;">EitherBuilder</span>&nbsp;() </pre> </p> <p> All this code is entirely generic, so you could put it (or a more comprehensive version) in a reusable library. </p> <h3 id="7785c22451e0487688292b8d3f481961"> Composition using the Either computation expression <a href="#7785c22451e0487688292b8d3f481961" title="permalink">#</a> </h3> <p> Since this Either computation expression is for general-purpose use, you can also use it to compose the above <code>imp</code> function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;candidate&nbsp;=&nbsp;either&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;r&nbsp;=&nbsp;<span style="color:#4ec9b0;">Validate</span>.<span style="color:navy;">reservation</span>&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString&nbsp;r.Date &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return!</span>&nbsp;<span style="color:#4ec9b0;">Capacity</span>.<span style="color:navy;">check</span>&nbsp;10&nbsp;i&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">map</span>&nbsp;(<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">saveReservation</span>&nbsp;connectionString)&nbsp;}</pre> </p> <p> Notice how the <code>r</code> value can be used by both the SqlGateway.getReservedSeats and Capacity.check functions, because they are all within the same scope. </p> <p> This example looks more like the Haskell composition in my <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">previous article</a>. It even looks cleaner in F#, but to be fair to Haskell, in F# you don't have to explicitly deal with IO as a monadic context, so that in itself makes things look simpler. </p> <h3 id="020ee98670e94a5ea43585937ec6bdaa"> A variation using shadowing <a href="#020ee98670e94a5ea43585937ec6bdaa" title="permalink">#</a> </h3> <p> You may wonder why I chose to use <code>return!</code> with an expression using the <code>map</code> function. One reason was that I wanted to avoid having to bind a named value to the result of calling Capacity.check. Naming is always difficult, and while I find the names <code>r</code> and <code>i</code> <a href="/2015/08/17/when-x-y-and-z-are-great-variable-names">acceptable because the scope is so small</a>, I prefer composing functions without having to declare intermediary values. </p> <p> Another reason was that I wanted to show you an example that resembles the previous Haskell example as closely as possible. </p> <p> You may, however, find the following variation more readable. In order to use <code>return</code> instead of <code>return!</code>, you need to furnish a <code>Return</code> method on your computation builder: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">EitherBuilder</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Bind</span>(x,&nbsp;<span style="color:navy;">f</span>)&nbsp;=&nbsp;<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.<span style="color:navy;">Return</span>&nbsp;x&nbsp;=&nbsp;<span style="color:navy;">Success</span>&nbsp;x</pre> </p> <p> Nothing prevents you from adding both <code>Return</code> and <code>ReturnFrom</code> (as well as other) methods to the EitherBuilder class, but I'm showing you the minimal implementation required for the example to work. </p> <p> Using this variation of EitherBuilder, you can alternatively write the <code>imp</code> function like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;candidate&nbsp;=&nbsp;either&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;r&nbsp;=&nbsp;<span style="color:#4ec9b0;">Validate</span>.<span style="color:navy;">reservation</span>&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;i&nbsp;=&nbsp;<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString&nbsp;r.Date &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;r&nbsp;=&nbsp;<span style="color:#4ec9b0;">Capacity</span>.<span style="color:navy;">check</span>&nbsp;10&nbsp;i&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">saveReservation</span>&nbsp;connectionString&nbsp;r&nbsp;}</pre> </p> <p> In such an alternative, you can use a <code>let!</code>-bound value to capture the result of calling Capacity.check, but that obligates you to devise a name for the value. You'll often see names like <code>r'</code>, but I always feel uneasy when I resort to such naming. Here, I circumvented the problem by <em>shadowing</em> the original <code>r</code> value with a new value also called <code>r</code>. This is possible, and even desirable, because once the reservation has been checked, you <em>should</em> use that new, checked value, and not the original value. That's what the original composition, above, does, so shadowing is both equivalent and appropriate. </p> <h3 id="67202e176d4c4dba869a8dcff7d4c5ed"> Summary <a href="#67202e176d4c4dba869a8dcff7d4c5ed" title="permalink">#</a> </h3> <p> Computation expressions give you an opportunity to compose functions in a readable way. It's an alternative to using the underlying bind and map functions. Whether you prefer one over the other is partly subjective, but I tend to favour the underlying functions until they become inconvenient. The initial version of the <code>imp</code> function in this article is an example of map and bind becoming unwieldy, and where a computation expression affords a cleaner, more readable syntax. </p> <p> The disadvantage of computation expressions is that you need to use <code>return</code> or <code>return!</code> to return a value. That forces you to write an extra line of code that isn't necessary with a simple <code>bind</code> or <code>map</code>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functional architecture is Ports and Adapters https://blog.ploeh.dk/2016/03/18/functional-architecture-is-ports-and-adapters 2016-03-18T08:53:00+00:00 Mark Seemann <div id="post"> <p> <em>Functional architecture tends to fall into a pit of success that looks a lot like Ports and Adapters.</em> </p> <p> In object-oriented architecture, we often struggle towards the ideal of the Ports and Adapters architecture, although we often <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">call it something else: layered architecture, onion architecture, hexagonal architecture, and so on</a>. The goal is to decouple the business logic from technical implementation details, so that we can vary each independently. </p> <p> This creates value because it enables us to manoeuvre nimbly, responding to changes in business or technology. </p> <h3 id="fdb379c02efd4cf6b193ed193e514e04"> Ports and Adapters <a href="#fdb379c02efd4cf6b193ed193e514e04" title="permalink">#</a> </h3> <p> The idea behind the Ports and Adapters architecture is that <em>ports</em> make up the boundaries of an application. A <em>port</em> is something that interacts with the outside world: user interfaces, message queues, databases, files, command-line prompts, etcetera. While the ports constitute the interface to the rest of the world, <em>adapters</em> translate between the ports and the application model. </p> <p> <img src="/content/binary/ports-and-adapters-conceptual-diagram.png" alt="A conceptual diagram of Ports and Adapters architecture: coloured boxes in concentric circles."> </p> <p> The word <em>adapter</em> is aptly chosen, because the role of the <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter design pattern</a> is exactly to translate between two different interfaces. </p> <p> You ought to arrive at some sort of variation of Ports and Adapters if you apply Dependency Injection, as <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">I've previously attempted to explain</a>. </p> <p> The problem with this architecture, however, is that it seems to take a lot of explaining: <ul> <li><a href="http://amzn.to/12p90MG">My book about Dependency Injection</a> is 500 pages long.</li> <li>Robert C. Martin's <a href="http://amzn.to/19W4JHk">book about the SOLID principles, package and component design, and so on</a> is 700 pages long.</li> <li><a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> is 500 pages long.</li> <li>and so on...</li> </ul> In my experience, implementing a Ports and Adapters architecture is a <a href="https://en.wikipedia.org/wiki/Sisyphus">Sisyphean task</a>. It requires much diligence, and if you look away for a moment, the boulder rolls downhill again. </p> <p> <img src="/content/binary/sisyphos-boulder-rolling-downhill.png" alt="A schematic showing a ball rolling down a slope."> </p> <p> It's possible to implement a Ports and Adapters architecture with object-oriented programming, but it takes <em>so much effort</em>. Does it have to be that difficult? </p> <h3 id="9025e957aeff4989ac840edaed80f11d"> Haskell as a learning aid <a href="#9025e957aeff4989ac840edaed80f11d" title="permalink">#</a> </h3> <p> Someone recently asked me: <em>how do I know I'm being sufficiently Functional?</em> </p> <p> I was wondering that myself, so I decided to learn Haskell. Not that Haskell is the only Functional language out there, but it enforces <a href="https://en.wikipedia.org/wiki/Pure_function">purity</a> in a way that neither F#, Clojure, nor Scala does. In Haskell, a function <em>must</em> be pure, unless its type indicates otherwise. This forces you to be deliberate in your design, and to separate pure functions from functions with (side) effects. </p> <p> If you don't know Haskell, code with side effects can only happen inside of a particular 'context' called <code>IO</code>. It's a monadic type, but that's not the most important point. The point is that you can tell by a function's type whether or not it's pure. A function with the type <code>ReservationRendition -&gt; Either Error Reservation</code> is pure, because <code>IO</code> appears nowhere in the type. On the other hand, a function with the type <code>ConnectionString -&gt; ZonedTime -&gt; IO Int</code> is impure because its return type is <code>IO Int</code>. This means that the return value is an integer, but that this integer originates from a context where it could change between function calls. </p> <p> There's a fundamental distinction between a function that returns <code>Int</code>, and one that returns <code>IO Int</code>. Any function that returns <code>Int</code> is, in Haskell, <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>. This means that you're guaranteed that the function will always return the same value given the same input. On the other hand, a function returning <code>IO Int</code> doesn't provide such a guarantee. </p> <p> In Haskell programming, you should strive towards maximising the amount of pure functions you write, pushing the impure code to the edges of the system. A good Haskell program has a big core of pure functions, and a shell of <code>IO</code> code. Does that sound familiar? </p> <p> It basically means that Haskell's type system <em>enforces</em> the Ports and Adapters architecture. The <em>ports</em> are all your <code>IO</code> code. The application's core is all your pure functions. The type system automatically creates a <em>pit of success</em>. </p> <p> <img src="/content/binary/pit-of-success.png" alt="A 'boulder' lying in the bottom of a schematised pit of success."> </p> <p> Haskell is a great learning aid, because it forces you to explicitly make the distinction between pure and impure functions. You can even use it as a verification step to figure out whether your F# code is 'sufficiently Functional'. F# is a <em>Functional first</em> language, but it also allows you to write object-oriented or imperative code. If you write your F# code in a Functional manner, though, it's easy to translate to Haskell. If your F# code is difficult to translate to Haskell, it's probably because it isn't Functional. </p> <p> Here's an example. </p> <h3 id="f3f51ca71ba94729abc0f39fc0c44af8"> Accepting reservations in F#, first attempt <a href="#f3f51ca71ba94729abc0f39fc0c44af8" title="permalink">#</a> </h3> <p> In my <a href="https://blog.ploeh.dk/tdd-with-fsharp">Test-Driven Development with F#</a> Pluralsight course (a <a href="http://www.infoq.com/presentations/mock-fsharp-tdd">free, condensed version is also available</a>), I demonstrate how to implement an HTTP API that accepts reservation requests for an on-line restaurant booking system. One of the steps when handling the reservation request is to check whether the restaurant has enough remaining capacity to accept the reservation. The function looks like this: </p> <p> <pre><span style="color:green;">//&nbsp;int</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;(DateTimeOffset&nbsp;-&gt;&nbsp;int)</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;Reservation</span> <span style="color:green;">//&nbsp;-&gt;&nbsp;Result&lt;Reservation,Error&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">check</span>&nbsp;capacity&nbsp;<span style="color:navy;">getReservedSeats</span>&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:navy;">getReservedSeats</span>&nbsp;reservation.Date &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;capacity&nbsp;&lt;&nbsp;reservation.Quantity&nbsp;+&nbsp;reservedSeats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">CapacityExceeded</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;reservation</pre> </p> <p> As the comment suggests, the second argument, <code>getReservedSeats</code>, is a function of the type <code>DateTimeOffset -&gt; int</code>. The <code>check</code> function calls this function to retrieve the number of already reserved seats on the requested date. </p> <p> When unit testing, you can supply a pure function as a Stub; for example: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">getReservedSeats</span>&nbsp;_&nbsp;=&nbsp;0 <span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#4ec9b0;">Capacity</span>.<span style="color:navy;">check</span>&nbsp;capacity&nbsp;<span style="color:navy;">getReservedSeats</span>&nbsp;reservation</pre> </p> <p> When finally composing the application, instead of using a pure function with a hard-coded return value, you can compose with an impure function that queries a database for the desired information: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Validate</span>.<span style="color:navy;">reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">bind</span>&nbsp;(<span style="color:#4ec9b0;">Capacity</span>.<span style="color:navy;">check</span>&nbsp;10&nbsp;(<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString)) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">map</span>&nbsp;(<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">saveReservation</span>&nbsp;connectionString)</pre> </p> <p> Here, <code>SqlGateway.getReservedSeats connectionString</code> is a partially applied function, the type of which is <code>DateTimeOffset -&gt; int</code>. In F#, you can't tell by its type that it's impure, but I know that this is the case because I wrote it. It queries a database, so isn't referentially transparent. </p> <p> This works well in F#, where it's up to you whether a particular function is pure or impure. Since that <code>imp</code> function is composed in the application's <a href="/2011/07/28/CompositionRoot">Composition Root</a>, the impure functions SqlGateway.getReservedSeats and SqlGateway.saveReservation are only pulled in at the edge of the system. The rest of the system is nicely protected against side-effects. </p> <p> It feels Functional, but is it? </p> <h3 id="0b86e95c4d7348249967e1f1e2dcf126"> Feedback from Haskell <a href="#0b86e95c4d7348249967e1f1e2dcf126" title="permalink">#</a> </h3> <p> In order to answer that question, I decided to re-implement the central parts of this application in Haskell. My first attempt to check the capacity was this direct translation: </p> <p> <pre>checkCapacity :: Int -&gt; (ZonedTime -&gt; Int) -&gt; Reservation -&gt; Either Error Reservation checkCapacity capacity getReservedSeats reservation = let reservedSeats = getReservedSeats $ date reservation in if capacity &lt; quantity reservation + reservedSeats then Left CapacityExceeded else Right reservation</pre> </p> <p> This compiles, and at first glance seems promising. The type of the <code>getReservedSeats</code> function is <code>ZonedTime -&gt; Int</code>. Since <code>IO</code> appears nowhere in this type, Haskell guarantees that it's pure. </p> <p> On the other hand, when you need to implement the function to retrieve the number of reserved seats from a database, this function must, by its very nature, be impure, because the return value could change between two function calls. In order to enable that in Haskell, the function must have this type: </p> <p> <pre>getReservedSeatsFromDB :: ConnectionString -&gt; ZonedTime -&gt; IO Int</pre> </p> <p> While you can partially apply the first ConnectionString argument, the return value is <code>IO Int</code>, not <code>Int</code>. </p> <p> A function with the type <code>ZonedTime -&gt; IO Int</code> isn't the same as <code>ZonedTime -&gt; Int</code>. Even when executing inside of an IO context, you can't convert <code>ZonedTime -&gt; IO Int</code> to <code>ZonedTime -&gt; Int</code>. </p> <p> You can, on the other hand, <em>call</em> the impure function inside of an IO context, and extract the <code>Int</code> from the <code>IO Int</code>. That doesn't quite fit with the above checkCapacity function, so you'll need to reconsider the design. While it was 'Functional enough' for F#, it turns out that this design isn't <em>really</em> Functional. </p> <p> If you consider the above checkCapacity function, though, you may wonder why it's necessary to pass in a function in order to determine the number of reserved seats. Why not simply pass in this number instead? </p> <p> <pre>checkCapacity :: Int -&gt; Int -&gt; Reservation -&gt; Either Error Reservation checkCapacity capacity reservedSeats reservation = if capacity &lt; quantity reservation + reservedSeats then Left CapacityExceeded else Right reservation</pre> </p> <p> That's much simpler. At the edge of the system, the application executes in an IO context, and that enables you to compose the pure and impure functions: </p> <p> <pre>import Control.Monad.Trans (liftIO) import Control.Monad.Trans.Either (EitherT(..), hoistEither) postReservation :: ReservationRendition -&gt; IO (HttpResult ()) postReservation candidate = fmap toHttpResult $ runEitherT $ do r &lt;- hoistEither $ validateReservation candidate i &lt;- liftIO $ getReservedSeatsFromDB connStr $ date r hoistEither $ checkCapacity 10 i r &gt;&gt;= liftIO . saveReservation connStr</pre> </p> <p> (Complete source code is available <a href="https://gist.github.com/ploeh/c999e2ae2248bd44d775">here</a>.) </p> <p> Don't worry if you don't understand all the details of this composition. The highlights are these: </p> <p> The postReservation function takes a ReservationRendition (think of it as a JSON document) as input, and returns an <code>IO (HttpResult ())</code> as output. The use of <code>IO</code> informs you that this entire function is executing within the IO monad. In other words: it's impure. This shouldn't be surprising, since this is the edge of the system. </p> <p> Furthermore, notice that the function <code>liftIO</code> is called twice. You don't have to understand exactly what it does, but it's necessary to use in order to 'pull out' a value from an <code>IO</code> type; for example pulling out the <code>Int</code> from an <code>IO Int</code>. This makes it clear where the pure code is, and where the impure code is: the liftIO function is applied to the functions getReservedSeatsFromDB and saveReservation. This tells you that these two functions are impure. By exclusion, the rest of the functions (validateReservation, checkCapacity, and toHttpResult) are pure. </p> <p> It's interesting to observe how you can interleave pure and impure functions. If you squint, you can almost see how the data flows from the pure validateReservation function, to the impure getReservedSeatsFromDB function, and then both output values (<code>r</code> and <code>i</code>) are passed to the pure checkCapacity function, and finally to the impure saveReservation function. All of this happens within an <code>(EitherT Error IO) () do</code> block, so if any of these functions return <code>Left</code>, the function short-circuits right there and returns the resulting error. See e.g. Scott Wlaschin's excellent article on <a href="http://fsharpforfunandprofit.com/posts/recipe-part2">railway-oriented programming</a> for an exceptional, lucid, clear, and visual introduction to the Either monad. </p> <p> The value from this expression is composed with the built-in runEitherT function, and again with this pure function: </p> <p> <pre>toHttpResult :: Either Error () -&gt; HttpResult () toHttpResult (Left (ValidationError msg)) = BadRequest msg toHttpResult (Left CapacityExceeded) = StatusCode Forbidden toHttpResult (Right ()) = OK ()</pre> </p> <p> The entire postReservation function is impure, and sits at the edge of the system, since it handles IO. The same is the case of the getReservedSeatsFromDB and saveReservation functions. I deliberately put the two database functions in the bottom of the below diagram, in order to make it look more familiar to readers used to looking at layered architecture diagrams. You can imagine that there's a cylinder-shaped figure below the circles, representing a database. </p> <p> <img src="/content/binary/haskell-post-reservation-ports-adapters.png" alt="Haskell solution with functions placed in a Ports and Adapters diagram."> </p> <p> You can think of the validateReservation and toHttpResult functions as belonging to the <em>application model</em>. While pure functions, they translate between the external and internal representation of data. Finally, the checkCapacity function is part of the application's Domain Model, if you will. </p> <p> Most of the design from my first F# attempt survived, apart from the Capacity.check function. Re-implementing the design in Haskell has taught me an important lesson that I can now go back and apply to my F# code. </p> <h3 id="6737c15f02dd4ab49362c6e491ea5bab"> Accepting reservations in F#, even more Functionally <a href="#6737c15f02dd4ab49362c6e491ea5bab" title="permalink">#</a> </h3> <p> Since the required change is so little, it's easy to apply the lesson learned from Haskell to the F# code base. The culprit was the Capacity.check function, which ought to instead be implemented like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">check</span>&nbsp;capacity&nbsp;reservedSeats&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;capacity&nbsp;&lt;&nbsp;reservation.Quantity&nbsp;+&nbsp;reservedSeats &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Failure</span>&nbsp;<span style="color:navy;">CapacityExceeded</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;reservation</pre> </p> <p> This simplifies the implementation, but makes the composition slightly more involved: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Validate</span>.<span style="color:navy;">reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString&nbsp;r.Date,&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">bind</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;(i,&nbsp;r)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#4ec9b0;">Capacity</span>.<span style="color:navy;">check</span>&nbsp;10&nbsp;i&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">map</span>&nbsp;(<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">saveReservation</span>&nbsp;connectionString)</pre> </p> <p> This almost looks more complicated than the Haskell function. Haskell has the advantage that you can automatically use any type that implements the <code>Monad</code> typeclass inside of a <code>do</code> block, and since <code>(EitherT Error IO) ()</code> is a Monad instance, the <code>do</code> syntax is available for free. </p> <p> You could do something similar in F#, but then you'd have to implement a custom computation expression builder for the Result type. Perhaps I'll do this in <a href="/2016/03/21/composition-with-an-either-computation-expression">a later blog post</a>... </p> <h3 id="622b9d08938f49d389bab5763118f6e3"> Summary <a href="#622b9d08938f49d389bab5763118f6e3" title="permalink">#</a> </h3> <p> Good Functional design is equivalent to the Ports and Adapters architecture. If you use Haskell as a yardstick for 'ideal' Functional architecture, you'll see how its explicit distinction between pure and impure functions creates a pit of success. Unless you write your entire application to execute <em>within</em> the <code>IO</code> monad, Haskell will automatically enforce the distinction, and push all communication with the external world to the edges of the system. </p> <p> Some Functional languages, like F#, don't explicitly enforce this distinction. Still, in F#, it's easy to <em>informally</em> make the distinction and compose applications with impure functions pushed to the edges of the system. While this isn't enforced by the type system, it still feels natural. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="8f4046a5c74f42e29daf500e295872c0"> <div class="comment-author"><a href="https://baks.github.io">Arkadiusz K</a> <a href="#8f4046a5c74f42e29daf500e295872c0">#</a></div> <div class="comment-content"> <p>Great post as always. However, I saw that you are using <em>IO monad</em>. I wonder if you shouldn't use <em>IO action</em> instead? To be honest, I'm not proficient in Haskell. But according to that <a href="http://blog.jle.im/entry/io-monad-considered-harmful.html">post</a> they are encouraging to use <em>IO action</em> rather than <em>IO monad</em> if we are not talking about monadic properties.</p> </div> <div class="comment-date">2016-03-18 23:00 UTC</div> </div> <div class="comment" id="266ab9f7cbe84d11a2045b92c5b370d5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#266ab9f7cbe84d11a2045b92c5b370d5">#</a></div> <div class="comment-content"> <p> Arkadiusz, thank you for writing. It's the first time I see <a href="http://blog.jle.im/entry/io-monad-considered-harmful.html">that post</a>, but if I understand the argument correctly, it argues against the (over)use of the monad <em>terminology</em> when talking about IO actions that aren't intrinsically monadic in nature. As far as I can tell, it doesn't argue against the use of the <code>IO</code> <em>type</em> in Haskell. </p> <p> It seems a reasonable point that it's unhelpful to newcomers to throw them straight into a discussion about monads, when all they want to see is a <em>hello world</em> example. </p> <p> As some of the comments to that post point out, however, you'll soon need to compose IO actions in Haskell, and it's the monadic nature of the <code>IO</code> type that enables the use of <code>do</code> blocks. (..or, if you want to be more specific, it seems that sometimes, all you need is a Functor. It hardly helps to talk about the <em>IO functor</em> instead of the <em>IO monad</em>, though...) </p> <p> I don't claim to be a Haskell expert, so there may be more subtle nuances in that article that simply went over my head. Still, within my current understanding, the discussion in this particular post of mine does relate to the IO <em>monad</em>. </p> <p> When I wrote the article, I chose my words with some caution. Notice that when I introduce the <code>IO</code> type my article, I mostly talk about it as a 'context'. </p> <p> When it comes to the discussion about the postReservation function, however, I pull out the stops and increasingly use the word <em>monad</em>. The reason is that this composition wouldn't be possible without the monadic properties of <code>IO</code>. Most of this function executes within a monadic 'stack': <code>EitherT Error IO</code>. EitherT is a monad transformer, and in order to be useful (e.g. composable in <code>do</code> blocks), the type it transforms must be a monad as well (or perhaps, as hinted above, a functor would be sufficient). </p> <p> I agree with Justin Le's article that overuse of <em>monad</em> is likely counterproductive. On the other hand, one can also fall into the opposite side. There are some educators who seem to avoid the word at any cost. That's not my policy. I try to use as precise language as possible. That means that I'll use <em>monad</em> when I talk about monads, and I'll avoid it when it isn't necessary. That's my goal, but I may not have achieved it. Do you think I used it incorrectly here? </p> </div> <div class="comment-date">2016-03-19 08:06 UTC</div> </div> <div class="comment" id="eafbee138fdd406c8ac580584f6b7f03"> <div class="comment-author"><a href="https://baks.github.io">Arkadiusz K</a> <a href="#eafbee138fdd406c8ac580584f6b7f03">#</a></div> <div class="comment-content"> <p>Mark, thank you for your comprehensive explanation. I was rather just asking you about opinion on this topic. I didn't try to claim that you had used it incorrectly. Now, I see that as you wrote you are first talking about IO context and then about IO monad when it comes to use monadic properties.</p> </div> <div class="comment-date">2016-03-19 10:30 UTC</div> </div> <div class="comment" id="a311a5928e7b460fb8b804b0ff576dbc"> <div class="comment-author">John Dailey <a href="#a311a5928e7b460fb8b804b0ff576dbc">#</a></div> <div class="comment-content"> <p> Hello, thanks so much for your website and pluralsight library -- it's been a tremendous help to me getting started with F# and functional programming (and TDD to a degree). I'm having some problems with the big picture question of how to structure applications, and this post is leaving me even more confused. </p> <p> I can't understand how some of the original code, and especially the changes made in this post could possibly scale to a larger or more complicated application. Many real-life domain operations are going to need to query some data, make decisions based on that data, map that data into different domain objects, persist something, and so on. </p> <p> It seems like the end result is that we end up copying much of the meaningful internal logic of the domain out to this boundary area in the Web API project. In this code we've extracted: <ul> <li>The logic of only checking existing reservations on the same day as the day on the request</li> <li>The logic of not saving a new reservation if the request failed the capacity check</li> </ul> </p> <p> Does that logic not properly belong 'behind the wall' inside the domain? I would have expected to see a simple 'makeReserveration r' function as the public face to the domain. Suppose the restaurant also had a desktop/kiosk application in their building to manage their reservation system. Wouldn't we now be forced to duplicate this internal logic in the Composition Root of that application? </p> <p> What I instinctively want to gravitate towards is something like this: </p> <p> <pre><span style="color:blue;">let internal</span>&nbsp;<span style="color:navy;">makeReservation' getReservedSeats saveReservation check r</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">(getReservedSeats</span>&nbsp;r.Date,&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;|>&nbsp;<span style="color:navy;">bind</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;(i,&nbsp;r)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">check</span>&nbsp;10&nbsp;i&nbsp;r) &nbsp;&nbsp;&nbsp;&nbsp;|>&nbsp;<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">saveReservation</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">makeReservation</span>&nbsp;= makeReservation' SqlGateway.getReservedSeats SqlGateway.saveReservation Capacity.check </pre> </p> <p> With this, the SqlGateway code needs be visible to the reservation code, putting it at the center of the app not on the edges. However, are there any practical disadvantages to this if the code is disciplined to only use it within this sort of dependency injection? What problems would I be likely to run into? </p> <p> At least as a beginner, this seems to me to have a couple advantages: <ul> <li>From the perspective of the unit tests (which test makeReservation') it is a pure function, as you noted above. Testability is no problem here.</li> <li>Code at the UI/Presentation level is nice and tight -- just make sure you can successfully transform the JSON request into a domain request, then process the command result.</li> </ul> </p> <p> But most notable to me, dependencies are 'injected' right where they were needed. Imagine a much more complicated aggregate operation like 'performOvernightCloseout' that needs to do many things like read cash register totals and send them to accounting, send timeclock info to HR, write order statistics to a reporting database, check inventory and create new orders for the following day. Of course we'd break this down into a hierarchy of functions, with top level orchestration calling down into increasingly specific functions, no function individually being too complicated. </p> <p> The original demo application would have to approach this scenario by passing in a huge number of functions to the operation root, which then parcels them out level by level where they are needed. This would seem very brittle since any change in a low level function would require changes to the parameter list all the way up. </p> <p> With newer code shown in this post, it would seem impossible. There might be a handful of small functions that can be in the central library, but the bulk of the logic is going to need to be extracted into a now-500-line imp method in the Web API project, or whatever service is launching this operation. </p> </div> <div class="comment-date">2016-03-24 23:30 UTC</div> </div> <div class="comment" id="b689745f9234491a8bb667742d67b44e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b689745f9234491a8bb667742d67b44e">#</a></div> <div class="comment-content"> <p> John, thank you for writing. I'm not sure I can answer everything to your satisfaction in single response, but we can always view this as a beginning of longer interchange. Some of the doubts you express are ambiguous, so I'm not sure how to interpret them, but I'll do my best to be as explicit as possible. </p> <p> In the beginning of your comment, you write: <blockquote> "Many real-life domain operations are going to need to query some data, make decisions based on that data, map that data into different domain objects, persist something, and so on." </blockquote> This is exactly the reason why I like the reservation request example so much, because it does all of that. It queries data, because it reads the number of already reserved seats from a database. It makes decisions based on that data, because it decides whether or not it can accept the reservation based on the number of remaining seats. It maps from an external JSON (or XML) data format to a Reservation record type (belonging to the Domain Model, if you will); it also maps back to an HTTP response. Finally, the example also saves the reservation to the database, if it was accepted. </p> <p> When you say that we've extracted the logic, it's true, but I'm not sure what you mean by "behind the wall", but let's look at each of your bullet points in turn. </p> <p> <strong>Querying existing reservations</strong> </p> <p> You write that I've extracted the "logic of only checking existing reservations on the same day as the day on the request". It's a simple database query. In F#, one implementation may look like this: </p> <p> <pre><span style="color:green;">//&nbsp;In&nbsp;module&nbsp;SqlGateway:</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString&nbsp;(date&nbsp;:&nbsp;<span style="color:#4ec9b0;">DateTimeOffset</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;min&nbsp;=&nbsp;<span style="color:#4ec9b0;">DateTimeOffset</span>(date.Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;date.Offset) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;max&nbsp;=&nbsp;<span style="color:#4ec9b0;">DateTimeOffset</span>(date.Date.<span style="color:navy;">AddDays</span>&nbsp;1.,&nbsp;date.Offset) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sql&nbsp;=&nbsp;<span style="color:#a31515;">&quot;</span> <span style="color:#a31515;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;ISNULL(SUM(Quantity),&nbsp;0)</span> <span style="color:#a31515;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;[dbo].[Reservations]</span> <span style="color:#a31515;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;@min&nbsp;&lt;=&nbsp;Date&nbsp;AND&nbsp;Date&nbsp;&lt;&nbsp;@max&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#4ec9b0;">SqlConnection</span>(connectionString) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#4ec9b0;">SqlCommand</span>(sql,&nbsp;conn) &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.<span style="color:navy;">AddWithValue</span>(<span style="color:#a31515;">&quot;@min&quot;</span>,&nbsp;min)&nbsp;|&gt;&nbsp;<span style="color:navy;">ignore</span> &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.<span style="color:navy;">AddWithValue</span>(<span style="color:#a31515;">&quot;@max&quot;</span>,&nbsp;max)&nbsp;|&gt;&nbsp;<span style="color:navy;">ignore</span> &nbsp;&nbsp;&nbsp;&nbsp;conn.<span style="color:navy;">Open</span>() &nbsp;&nbsp;&nbsp;&nbsp;cmd.<span style="color:navy;">ExecuteScalar</span>()&nbsp;:?&gt;&nbsp;<span style="color:#4ec9b0;">int</span></pre> </p> <p> In my opinion, there's no <em>logic</em> in this function, but this is an example where terminology can be ambiguous. To me, however, <em>logic</em> implies that decisions are being made. That's not the case here. This getReservedSeats function has a cyclomatic complexity of 1. </p> <p> This function is an Adapter: it adapts the ADO.NET SDK (the <em>port</em> to SQL Server) to something the Domain Model can use. In this case, the answer is a simple integer. </p> <p> <strong>To save, or not to save</strong> </p> <p> You also write that I've extracted out the "logic of not saving a new reservation if the request failed the capacity check". Yes, that logic is extracted out to the <code>check</code> function above. Since that's a pure function, it's part of the Domain Model. </p> <p> The function that saves the reservation in the database, again, contains no logic (as I interpret the word <em>logic</em>): </p> <p> <pre><span style="color:green;">//&nbsp;In&nbsp;module&nbsp;SqlGateway:</span> <span style="color:blue;">let</span>&nbsp;saveReservation&nbsp;connectionString&nbsp;(reservation&nbsp;:&nbsp;Reservation)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sql&nbsp;=&nbsp;<span style="color:#a31515;">&quot;</span> <span style="color:#a31515;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INSERT&nbsp;INTO&nbsp;Reservations(Date,&nbsp;Name,&nbsp;Email,&nbsp;Quantity)</span> <span style="color:#a31515;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES(@Date,&nbsp;@Name,&nbsp;@Email,&nbsp;@Quantity)&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlConnection(connectionString) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;cmd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;SqlCommand(sql,&nbsp;conn) &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Date&quot;</span>,&nbsp;reservation.Date)&nbsp;|&gt;&nbsp;ignore &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Name&quot;</span>,&nbsp;reservation.Name)&nbsp;|&gt;&nbsp;ignore &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Email&quot;</span>,&nbsp;reservation.Email)&nbsp;|&gt;&nbsp;ignore &nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.AddWithValue(<span style="color:#a31515;">&quot;@Quantity&quot;</span>,&nbsp;reservation.Quantity)&nbsp;|&gt;&nbsp;ignore &nbsp;&nbsp;&nbsp;&nbsp;conn.Open() &nbsp;&nbsp;&nbsp;&nbsp;cmd.ExecuteNonQuery()&nbsp;|&gt;&nbsp;ignore</pre> </p> <p> Due to the way the Either monad works, however, this function is only going to be called if all previous functions returned Success (or Right in Haskell). That logic is an entirely reusable abstraction. It's also part of the glue that composes functions together. </p> <p> <strong>Reuse of composition</strong> </p> <p> Further down, you ask: <blockquote> "Suppose the restaurant also had a desktop/kiosk application in their building to manage their reservation system. Wouldn't we now be forced to duplicate this internal logic in the Composition Root of that application?" </blockquote> That's an excellent question! I'm glad you asked. </p> <p> A desktop or kiosk application is noticeably different from a web application. It may have a different application flow, and it certainly exposes a different interface to the outside world. Instead of handling incoming HTTP requests, and translating results to HTTP responses, it'll need to respond to click events and transform results to on-screen UI events. This means that validation may be different, and that you don't need to map results back to HTTP response values. Again, this may be clearer in the Haskell implementation, where the <code>toHttpResult</code> function would obviously be superfluous. Additionally, a UI may present a native date and time picker control, which can directly produce strongly typed date and time values, negating the need for validation. </p> <p> Additionally, a kiosk may need to be able to work in off-line, or occasionally connected, mode, so perhaps you'd prefer a different data store implementation. Instead of connecting directly to a database, such an application would need to write to a message queue, and read from an off-line snapshot of reservations. </p> <p> That's a sufficiently different application that it warrants creating applications from <a href="/2011/06/07/SOLIDCodeisnt">fine-grained building blocks instead of course-grained building blocks</a>. Perhaps the only part you'll end up reusing is the core business logic contained in the check[Capacity] function. <a href="/2015/01/06/composition-root-reuse">Composition Roots aren't reusable</a>. </p> <p> (As an aside, you may think that if the check[Capacity] function is the only reusable part, then what's the point? This function is only a couple of lines long, so why even bother? That'd be a fair concern, if it wasn't for the context that all of this is only example code. Think of this example as a stand-in for much more complex business logic. Perhaps the restaurant will allow a certain percentage of over-booking, because some people never show up. Perhaps it will allow different over-booking percentages on different (week) days. Perhaps it'll need to handle reservations on per-table basis, instead of per-seat. Perhaps it'll need to handle multiple seatings on the same day. There are lots of variations you can throw into this business domain that will make it much more complex, and worthy of reuse.) </p> <p> <strong>Brittleness of composition</strong> </p> <p> Finally, you refer to a more realistic scenario, called <em>performOvernightCloseout</em>, and ask if a fine-grained composition wouldn't be brittle? In my experience, it wouldn't be <em>more</em> brittle than any alternatives I've identified. Whether you 'inject' functions into other functions, or you compose them in the Composition Root, doesn't change the underlying forces that act on your code. If you make substantial changes to the dependencies involved, it will break your code, and you'll need to address that. This is true for any manual composition, including <a href="/2014/06/10/pure-di">Pure DI</a>. The only way to avoid compilation errors when you redefine your dependency graphs is to use a DI Container, but that only <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">makes the feedback loop worse, because it'd change a compile-time error into a run-time error</a>. </p> <p> This is a long answer, and even so, I'm not sure I've sufficiently addressed all of your concerns. If not, please write again for clarifications. </p> </div> <div class="comment-date">2016-03-30 13:46 UTC</div> </div> <div class="comment" id="a34d8ef976e44caeb83de6bd69b3814b"> <div class="comment-author">John Dailey <a href="#a34d8ef976e44caeb83de6bd69b3814b">#</a></div> <div class="comment-content"> <p>I believe there was a bit of miscommunication. When I talked about the 'logic of only checking existing reservations on the same day', I wasn't talking about the implementation of the SqlGateway. I was talking about the 'imp' function: </p> <pre>let imp = Validate.reservation >> map (fun r -> SqlGateway.getReservedSeats connectionString r.Date, r) >> bind (fun (i, r) -> Capacity.check 10 i r) >> map (SqlGateway.saveReservation connectionString)</pre> <p> This implements the logic of 'Validation comes first. If that checks out, then get the number of reserved seats from the data store. Use the date on the request for that. Feed this into the capacity check routine and only if everything checks out should we persist the reservation. </p> <p> That's all internal logic that in my mind ought to be encapsulated within the domain. That's what I meant by 'behind the wall'. There should simply be a function 'makeReservation' with the signature ReservationRequest -> ReservationResponse. That's it. Clients should not be worrying themselves with these extra details. That 'imp' function just seems deeply inappropriate as something that lives externally to the reservations module, and that must be supplied individually by each client accessing the module. </p> <p> I guess I'm rejecting the idea of Onion/Hexagonal architecture, or at least the idea that all I/O always belongs at the outside. To me, a reservations module must depend on a reservations data store, because if you don't persist information to the db, <em>you haven't actually made the reservation</em>. Of course, I still want to depend on interfaces, not implementations (perhaps naming the reference ReservationDataStore.saveReservation). The data store IS a separate layer and should have a well defined interface. </p> <p> I just don't understand why anyone would find this design more desirable than the straightforward UI -> Logic -> Storage chain of dependencies. Clearly there's some sort of appeal -- lots of smart people show excitement for it. But it's a bit mystifying to me. </p> </div> <div class="comment-date">2016-04-06 01:45 UTC</div> </div> <div class="comment" id="6715f343503f4b3e8f528bd75487b47b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6715f343503f4b3e8f528bd75487b47b">#</a></div> <div class="comment-content"> <p> John, thank you for writing. It's OK to reject the concept of <em>ports and adapters</em>; it's not a one-size-fits-all architecture. While I <em>often</em> use it, I don't <em>always</em> use it. One of my most successful customer engagements involved building a set of REST APIs on top of an existing IT infrastructure. All the business logic was already implemented elsewhere, so I deliberately chose a simpler, more coupled architecture, because there'd be no benefit from a full <em>ports and adapters</em> architecture. The code bases involved still run in production four years later, even though they're constantly being changed and enhanced. </p> <p> If, on the other hand, you choose to adopt the ports and adapters architecture, you'll need to do it right so that you can harvest the benefits. </p> <p> A Façade function with the type <code>ReservationRequest -&gt; ReservationResponse</code> only makes sense in an HTTP request/response scenario, because both the ReservationRequest and ReservationResponse types are specific to the web context. You can't reuse such a function in a different setting. </p> <p> You still have such a function, because that's the function that handles the web request, but you'll need to <em>compose</em> it from smaller components, unless you want to write a <a href="http://martinfowler.com/eaaCatalog/transactionScript.html">Transaction Script</a> within that function (which is also sometimes OK). </p> <p> Granted, there's some 'logic' involved in this composition, in the sense that it might be possible to create a faulty composition, but that logic has to go <em>somewhere</em>. </p> </div> <div class="comment-date">2016-04-08 9:57 UTC</div> </div> <div class="comment" id="7f7fc08635bb471f95d314e999f14164"> <div class="comment-author"><a href="https://github.com/MartinRykfors">Martin Rykfors</a> <a href="#7f7fc08635bb471f95d314e999f14164">#</a></div> <div class="comment-content"> <p> Hi Mark, thanks for this post and the discussion. </p> <p> I feel that I share some of the concerns that John is bringing up. The <code>postReservation</code> function is pretty simple now, but eventually we might want to make external calls based on some conditional logic from the domain model. </p> <p> Let's say there is a new feature request. The restaurant has struck a deal with the local caravan dealership, allowing them to rent a caravan to park outside the restaurant in order to increase the seating capacity for one evening. Of course, sometimes there are no caravans available, so we'll need to query the caravan database to see if there is a big enough caravan available that evening: </p> <p> <pre> findCaravan :: Int -> ZoneTime -> IO (Maybe Caravan) findCaravan minimumCapacity date = ... -- database stuff </pre> </p> <p> How would we integrate this? We could put a call to <code>findCaravan</code> inside the IO context of <code>postReservation</code>, but since we only want to query the caravan database if the first capacity check failed, we'll need to add branching logic to <code>postReservation</code>. We want to have tests for this logic, but since we are coupled to the IO context, we are in trouble. </p> <p> The original issue was that the F# version of <code>checkCapacity</code> could be made impure by injecting the impure <code>getReservedSeats</code> function to it, which is something that Haskell disallows. What if we changed the Haskell version of <code>checkCapacity</code> to the following instead? </p> <p> <pre> checkCapacity :: Monad m => Int -> (ZonedTime -> m Int) -> Reservation -> EitherT Error m Reservation checkCapacity capacity getReservedSeats reservation = EitherT $ do reservedSeats <- getReservedSeats $ date reservation if capacity < quantity reservation + reservedSeats then return $ Left CapacityExceeded else return $ Right reservation </pre> </p> <p> Since we know that <code>getReservedSeats</code> will be effectful, we declare it so in its type signature. We change <code>postReservation</code> to become: </p> <p> <pre> postReservation :: ReservationRendition -> IO (HttpResult ()) postReservation candidate = fmap toHttpResult $ runEitherT $ hoistEither $ validateReservation candidate >>= checkCapacity 10 (getReservedSeatsFromDB connStr) >>= liftIO . saveReservation connStr </pre> </p> <p> (Or something like that. Monad transformers are the current step on my Haskell learning journey, so I'm not sure this will typecheck.) </p> <p> This way, we can once more inject an IO typed function as the <code>getReservedSeats</code> argument of <code>checkCapacity</code> like we did in F#. If we want to test it, we can create a stub database call by using the monad instance of <code>Identity</code>, for example: </p> <p> <pre> stubGetReservedSeats :: ZonedTime -> Identity Int stubGetReservedSeats date = return 8 </pre> </p> <p> The new feature request also becomes easy to implement. Just pass <code>findCaravan</code> as a new argument to <code>checkCapacity</code>, and make the type of that argument to be <code>ZonedTime -> m (Maybe Caravan)</code>. The <code>checkCapacity</code> function can now do the initial capacity check, and if that fails, call <code>findCaravan</code> to see if it is still possible to allow the booking by renting a suitable caravan. The branching logic that is needed is now inside <code>checkCapacity</code> and can be tested outside of the IO context. </p> <p> I don't know if this would work well in practice, or if it would cause more trouble than what it's worth. What do you think? </p> </div> <div class="comment-date">2016-06-09 10:56 UTC</div> </div> <div class="comment" id="b5d099f9327a4449a5775f6819a335a4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b5d099f9327a4449a5775f6819a335a4">#</a></div> <div class="comment-content"> <p> Martin, thank you for writing. Your question prompted me to write a <a href="/2016/07/04/conditional-composition-of-functions">new article</a>. It takes a slightly different approach to the one you suggest. </p> <p> I haven't tried defining <code>checkCapacity</code> in the way you suggest, but that may be an exercise for another day. In general, though, I'm hesitant to introduce constraints not <em>required</em> by the implementation. In this case, I think it'd be a fair question to ask why the <code>getReservedSeats</code> argument has to have the type <code>ZonedTime -&gt; m Int</code>? Nothing in its implementation seems to indicate the need for that, so it looks like a leaky abstraction to me. </p> </div> <div class="comment-date">2016-07-04 7:06 UTC</div> </div> <div class="comment" id="30363520336e4fd4bd3e2948d82a2e9a"> <div class="comment-author"><a href="https://giuliohome.wordpress.com/">Giulio</a> <a href="#30363520336e4fd4bd3e2948d82a2e9a">#</a></div> <div class="comment-content"> <p> I'd like to follow up my <a href="https://twitter.com/giuliohome/status/822373175983935488">twitter comments</a> here </p><p> Very interesting article! I'm sorry that some Haskell details are quite difficult for me at the moment and the F# code looks a bit too simplified or maybe just incomplete </p><p> So I'd prefer your <a href="/2016/04/11/async-as-surrogate-io/">approach here</a> : it better translates Haskell to F# but I still have to study its implications. Anyway the concrete benefits of Async are evident, like the ones of F# computation expressions (to wrap exceptions etc...) </p><p> On the other hand I fail to see the benefit of purity vs other IoC patterns. Besides async and exceptions wrapping, purity seems an unnecessarily strict enforcement of Haskell (isn't it?): original F# was testable in isolation </p><p> What's missing if I only mock/stub/inject the dependency? When you say <blockquote>"ability to reason is eroded"</blockquote> and <blockquote>"it breaks encapsulation"</blockquote> about DI, do you mean something related to <a href="http://stackoverflow.com/questions/1005473/must-dependency-injection-come-at-the-expense-of-encapsulation">this topic</a>? </p><p> Thank you so much for your interesting insights!</p></div> <div class="comment-date">2017-01-20 10:45 UTC</div> </div> <div class="comment" id="ef3ccfcfcc6d415ca9209cb0b10992ba"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ef3ccfcfcc6d415ca9209cb0b10992ba">#</a></div> <div class="comment-content"> Giulio, thank you for writing. Why do you think that the only goal of pure functions is testability? </div> <div class="comment-date">2017-01-20 18:23 UTC</div> </div> <div class="comment" id="834c19c46a454740ab96d40af397fa71"> <div class="comment-author"><a href="https://giuliohome.wordpress.com/">Giulio</a> <a href="#834c19c46a454740ab96d40af397fa71">#</a></div> <div class="comment-content"> <p> <blockquote>Why do you think that the only goal of pure functions is testability?</blockquote> My bad, actually I don't know. I assume that purity's goal is to avoid side effects. It is very clear that this can be useful when I have a <em>hidden</em> side effect in the function, but it is <em>less obvious</em> (to me) what it does mean when the <i>side effect</i> is as evident as an argument (?). </p> <p> Anyway, I'll follow your implicit suggestion that with a pure function I can reach a more important goal than testability. Ok, practical example. Today I spent one hour to fix a bug that I introduced with a previous enhancement. My goal is to avoid this next time, by learning a better programming style from your article, but first I need to understand what went wrong. I can spot 3 problems: <ul> <li>1) I didn't see the bug when I wrote the code changes</li> <li>2) I didn't go through that part of my app when I tested the upgrade</li> <li>3) I needed a whole hour to fix that stupid bug, because I could not immediately find the point where it was</li> </ul> When I think that testability is important, it's because I hope it saves me from the issues 2 and 3, but let's take for granted that with a pure function I can avoid even point 1. </p> <p> Now unfortunately I struggle to interpret Haskell's details and I can't find enough F# to recover the whole meaning, but the main point I get is that a part of code (that could be bugged because <i>the ability to reason is eroded</i> there) has been moved from a finally 'pure' function to a new 'impure' one, that is introduced by a special 'liftIO' keyword (sorry for my poor recap) </p> <p> Finally my question is, but what about that all the possible bugs (often at the interface between persistence and model layers) that have simply been migrated around this critical IO land? are they going to counterbalance the above said benefits? where is exactly the net, overall improvement? </p> </div> <div class="comment-date">2017-01-20 20:02:00 UTC</div> </div> <div class="comment" id="0f59ad9b036e49459123344326158a11"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0f59ad9b036e49459123344326158a11">#</a></div> <div class="comment-content"> <p> First, I should make it specific that when I discuss object-oriented code in general, and encapsulation specifically, I tend to draw heavily on the work of Bertrand Meyer in <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a>. Some people may regard Alan Kay as the original inventor of object-orientation, but C# and Java's interpretation of object-oriented programming seems closer to Meyer's than to Kay's. </p> <p> To Meyer, the most important motivation for object-orientation was reuse. He wanted to be able to reuse software that other people had written, instead of, as had been the norm until then, importing other people's source code into new code bases. I'm sure you can see how that goal was important. </p> <p> In order to make binary libraries reusable, Meyer wanted to establish some techniques that would enable developers to (re)use the code without having access to the source code. His vision was that every object should come with a set of invariants, pre-, and post-conditions. He even invented a programming language, Eiffel, that included the ability to define such rules as programmable and enforceable checks. Furthermore, such rules could be exported as automatic documentation. This concept, which Meyer called <em>contracts</em>, is what we now know as <em>encapsulation</em> (I have <a href="https://blog.ploeh.dk/encapsulation-and-solid">a Pluralsight course that explains this in more details</a>). </p> <p> While the idea of formal contracts seemed like a good idea, neither Java nor C# have them. In C# and Java, you can still implement invariants with guard clauses, assertions, and so on, but there's no formal, externally visible way to communicate such invariants. </p> <p> In other words, in modern object-oriented languages, you can't reason about the code from its API alone. Meyer's goal was that you should be able to reuse an object that someone else had written, <em>without reading the source code</em>. In a nutshell: if you have to read the source code, it means that encapsulation is broken. </p> <p> One of the other concepts that Meyer introduced was <a href="http://en.wikipedia.org/wiki/Command-query_separation">Command-Query Separation</a> (CQS). To Meyer, it was important to be able to distinguish between operations that had side-effects, and operations that didn't. </p> <p> In my experience, a big category of defects are caused by unintended side-effects. One developer doesn't realise that the method call (s)he invokes has a side-effect that another developer put there. Languages like C# and Java have few constraints, so any method could potentially hide a side-effect. The only way you can know is by reading the code. </p> <p> What if, then, there was a programming language where you could tell, at a glance, whether an operation had a side-effect? Such programming languages exist. Haskell is one of them. In Haskell, all functions are pure by default. This means that they are deterministic and have no side-effects. If you want to write an impure function, you must explicitly declare that in the function's type (which is done with the <code>IO</code> type). </p> <p> In Haskell, you don't have to read the source code in order to know whether a function is impure or not. You can simply look at the type! </p> <p> This is one of the many benefits of pure functions: we know, by definition, that they have no side-effects. This property holds not only for the function itself, but for all functions that that pure function calls. This follows from the definition of purity. A pure function can call other pure functions, but it can't call impure functions, because, if it did, it would become impure itself. Haskell enforces this rule, so when you're looking at a pure function, you know that that entire subgraph of your code is pure. Otherwise, it wouldn't compile. </p> <p> Not only does that relationship force us to push impure functions to the boundary of applications, as described in the present article. It also makes <a href="/2015/05/07/functional-design-is-intrinsically-testable">functions intrinsically testable</a>. There are other advantages of pure functions besides these, such as easy composability, but in general, pure functions are attractive because it becomes easier to figure out what a function does without having to understand all the details. In other words, pure functions are better abstractions. </p> <p> To be clear: functional programming doesn't protect you from defects, but in my experience, it helps you to avoid entire classes of bugs. I still write automated tests of my F# code, but I write fewer tests than when I wrote C# code. Despite that, I also seem to be producing fewer defects, and I rarely need to debug the functions. </p> <p> What's the overall net improvement? I don't know. How would you measure that? </p> </div> <div class="comment-date">2017-01-20 21:53 UTC</div> </div> <div class="comment" id="ec8e27cca5984e2c82647b79f1e0df35"> <div class="comment-author">Abel Quiros <a href="#ec8e27cca5984e2c82647b79f1e0df35">#</a></div> <div class="comment-content"> <p>Consider a logic that, depending on a condition, performs one IO operation or another. In Java (I don’t know F# or Haskell):</p> <pre>int foo(boolean condition, IntSupplier io1, IntSupplier io2) { if (condition) { return io1.getAsInt(); } else { return io2.getAsInt(); } }</pre> <p>Would you recommend pre-fetching the two values prior to calling the <code>foo</code> function even though only one would ultimately be required?</p> </div> <div class="comment-date">2017-03-12 20:47 UTC</div> </div> <div class="comment" id="f555a56f148f4598bc885a467867fe4a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f555a56f148f4598bc885a467867fe4a">#</a></div> <div class="comment-content"> <p> Abel, thank you for writing. There's a discussion closely related to that question in the <em>remarks</em> sections of my <a href="/2017/02/02/dependency-rejection">dependency rejection</a> article. It's the remark from 2017-02-18 19:54 UTC (I really ought to get around to <a href="https://github.com/ploeh/ploeh.github.com/issues/267">add permalinks to comments</a>...). </p> <p> Additionally, there's a discussion on this page that lead to <a href="/2016/07/04/conditional-composition-of-functions">a new article</a> that goes into some further details. </p> </div> <div class="comment-date">2017-03-13 06:42 UTC</div> </div> <div class="comment" id="1d53d15fb2b04405935252c551b16419"> <div class="comment-author"><a href="https://github.com/Porges">George Pollard</a> <a href="#1d53d15fb2b04405935252c551b16419">#</a></div> <div class="comment-content"> <p>(This is rather a delayed reply, but...) @Martin Rykfors, you mentioned monad transformers as an option. A few months before your comment I recorded <a href="https://www.youtube.com/watch?v=9Af2ZUK3Z4U">a livecoded session of applying MTL-style monad transformers to this code</a>.</p> <p>I'm not sure if you're interested (it's nearly an hour of me talking to myself), but just in case... 🙂</p> </div> <div class="comment-date">2017-07-10 21:54 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Ad-hoc Arbitraries - now with pipes https://blog.ploeh.dk/2016/03/01/ad-hoc-arbitraries---now-with-pipes 2016-03-01T08:46:00+00:00 Mark Seemann <div id="post"> <p> <em>Slightly improved syntax for defining ad-hoc, in-line Arbitraries for FsCheck.Xunit.</em> </p> <p> Last year, I described <a href="/2015/09/08/ad-hoc-arbitraries-with-fscheckxunit">how to define and use ad-hoc, in-line Arbitraries with FsCheck.Xunit</a>. The final example code looked like this: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;&gt;&nbsp;3&nbsp;live&nbsp;neighbors&nbsp;dies``</span>&nbsp;(cell&nbsp;:&nbsp;<span style="color:#4ec9b0;">int</span>&nbsp;*&nbsp;<span style="color:#4ec9b0;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;nc&nbsp;=&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;[4..8]&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;nc&nbsp;(<span style="color:blue;">fun</span>&nbsp;neighborCount&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;liveNeighbors&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">findNeighbors</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">take</span>&nbsp;neighborCount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">toList</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">State</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">calculateNextState</span>&nbsp;(cell&nbsp;<span style="color:navy;">::</span>&nbsp;liveNeighbors&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">set</span>)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Dead</span>&nbsp;=!&nbsp;actual)</pre> </p> <p> There's one tiny problem with this way of expressing properties using Prop.forAll: the use of brackets to demarcate the anonymous multi-line function. Notice how the opening bracket appears to the left of the <code>fun</code> keyword, while the closing bracket appears eleven lines down, after the end of the expression <code>Dead =! actual</code>. </p> <p> While it isn't pretty, I haven't found it that bad for readability, either. When you're writing the property, however, this syntax is cumbersome. </p> <h3 id="2f198728b272499b83d3afae9373af69"> Issues writing the code <a href="#2f198728b272499b83d3afae9373af69" title="permalink">#</a> </h3> <p> After having written <code>Prop.forAll nc</code>, you start writing <code>(fun neighborCount -> )</code>. After you've typed the closing bracket, you have to move your cursor one place to the left. When entering new lines, you have to keep diligent, making sure that the closing bracket is always to the right of your cursor. </p> <p> If you ever need to use a nested level of brackets within that anonymous function, your vigilance will be tested to its utmost. When I wrote the above property, for example, when I reached the point where I wanted to call the calculateNextState function, I wrote: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;State&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;calculateNextState&nbsp;(</pre> </p> <p> The editor detects that I'm writing an opening bracket, but since I have the closing bracket to the immediate right of the cursor, this is what happens: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;State&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;calculateNextState&nbsp;()</pre> </p> <p> What normally happens when you type an opening bracket is that the editor automatically inserts a closing bracket to the right of your cursor, but in this case, it doesn't do that. There's already a closing bracket immediately to the right of the cursor, and the editor assumes that this bracket belongs to the opening bracket I just typed. What it really should have done was this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">State</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">calculateNextState</span>&nbsp;())</pre> </p> <p> The editor doesn't have a chance, though, because at this point, I'm still typing, and the code doesn't compile. None of the alternatives compile at this point. You can't really blame the editor. </p> <p> To make a long story short, enclosing a multi-line anonymous function in brackets is a source of errors. If only there was a better alternative. </p> <h3 id="2a7984c64e7440c1a9408d258cf1c5d6"> Backward pipe <a href="#2a7984c64e7440c1a9408d258cf1c5d6" title="permalink">#</a> </h3> <p> The solution started to dawn on me because I've been learning Haskell for the last half year, and in Haskell, you often use the <code>$</code> operator to get rid of unwanted brackets. <a href="http://stackoverflow.com/q/7183903/126014">F# has an equivalent operator</a>: <code>&lt;|</code>, the backward pipe operator. </p> <p> I rarely use the <code>&lt;|</code> operator in F#, but in this case, it works really well: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;&gt;&nbsp;3&nbsp;live&nbsp;neighbors&nbsp;dies``</span>&nbsp;(cell&nbsp;:&nbsp;<span style="color:#4ec9b0;">int</span>&nbsp;*&nbsp;<span style="color:#4ec9b0;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;nc&nbsp;=&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;[4..8]&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;nc&nbsp;&lt;|&nbsp;<span style="color:blue;">fun</span>&nbsp;neighborCount&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;liveNeighbors&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">findNeighbors</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">take</span>&nbsp;neighborCount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">toList</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">State</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">calculateNextState</span>&nbsp;(cell&nbsp;<span style="color:navy;">::</span>&nbsp;liveNeighbors&nbsp;|&gt;&nbsp;<span style="color:navy;">shuffle</span>&nbsp;|&gt;&nbsp;<span style="color:navy;">set</span>)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Dead</span>&nbsp;=!&nbsp;actual</pre> </p> <p> Notice that there's no longer any need for a closing bracket after <code>Dead =! actual</code>. </p> <h3 id="946811968a50469d9f862d57ecb8a7cc"> Summary <a href="#946811968a50469d9f862d57ecb8a7cc" title="permalink">#</a> </h3> <p> When the last argument passed to a function is another function, you can replace the brackets with a single application of the <code>&lt;|</code> operator. I only use this operator sparingly, but in the case of in-line ad-hoc FsCheck Arbitraries, I find it useful. </p> <p> <strong>Addendum 2016-05-17:</strong> You can <a href="/2016/05/17/tie-fighter-fscheck-properties">get rid of the <code>nc</code> value with <em>TIE fighter infix</em> notation</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software: other properties https://blog.ploeh.dk/2016/02/19/types--properties--software-other-properties 2016-02-19T08:59:00+00:00 Mark Seemann <div id="post"> <p> <em>Even simple functions may have properties that can be expressed independently of the implementation.</em> </p> <p> This article is the eighth in <a href="/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In previous articles, you've seen an extensive example of how to solve <a href="http://www.codingdojo.org/cgi-bin/index.pl?KataTennis">the Tennis Kata</a> with type design and Property-Based Testing. Specifically, in <a href="/2016/02/12/types-properties-software-properties-for-the-advantage-state">the third article</a>, you saw the introduction of a function called <code>other</code>. In that article we didn't cover that function with automatic tests, but this article rectifies that omission. </p> <p> The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>. </p> <h3 id="9c1fcc48caa94a12a2b681fb94b7c6d9"> Implementation duplication <a href="#9c1fcc48caa94a12a2b681fb94b7c6d9" title="permalink">#</a> </h3> <p> The <code>other</code> function is used to find the opponent of a player. The implementation is trivial: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">other</span>&nbsp;=&nbsp;<span style="color:blue;">function</span>&nbsp;<span style="color:navy;">PlayerOne</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PlayerTwo</span>&nbsp;|&nbsp;<span style="color:navy;">PlayerTwo</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PlayerOne</span> </pre> </p> <p> Unless you're developing software where lives, or extraordinary amounts of money, are at stake, you probably need not test such a trivial function. I'm still going to show you how you could do this, mostly because it's a good example of how Property-Based Testing can sometimes help you test the <em>behaviour</em> of a function, instead of the implementation. </p> <p> Before I show you that, however, I'll show you why example-based unit tests are inadequate for testing this function. </p> <p> You could test this function using examples, and because the space of possible input is so small, you can even cover it in its entirety: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``other&nbsp;than&nbsp;playerOne&nbsp;returns&nbsp;correct&nbsp;result``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">PlayerTwo</span>&nbsp;=!&nbsp;<span style="color:navy;">other</span>&nbsp;<span style="color:navy;">PlayerOne</span> [&lt;<span style="color:#4ec9b0;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``other&nbsp;than&nbsp;playerTwo&nbsp;returns&nbsp;correct&nbsp;result``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">PlayerOne</span>&nbsp;=!&nbsp;<span style="color:navy;">other</span>&nbsp;<span style="color:navy;">PlayerTwo</span></pre> </p> <p> The <code>=!</code> operator is a custom operator defined by <a href="http://www.swensensoftware.com/unquote">Unquote</a>, an assertion library. You can read the first expression as <em>player two must equal other than player one</em>. </p> <p> The problem with these tests, however, is that they don't state anything not already stated by the implementation itself. Basically, what these tests state is that <em>if the input is PlayerOne, the output is PlayerTwo, and if the input is PlayerTwo, the output is PlayerOne</em>. </p> <p> That's exactly what the implementation does. </p> <p> The only important difference is that the implementation of <code>other</code> states this relationship more succinctly than the tests. </p> <p> It's as though the tests duplicate the information already in the implementation. How does that add value? </p> <p> Sometimes, this can be the only way to cover functionality with tests, but in this case, there's an alternative. </p> <h3 id="794f544598b343be91ad3b4d4db18479"> Behaviour instead of implementation <a href="#794f544598b343be91ad3b4d4db18479" title="permalink">#</a> </h3> <p> Property-Based Testing inspires you to think about the observable behaviour of a system, rather than the implementation. Which properties do the <code>other</code> function have? </p> <p> If you call it with a Player value, you'd expect it to return a Player value that's not the input value: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``other&nbsp;returns&nbsp;a&nbsp;different&nbsp;player``</span>&nbsp;player&nbsp;=&nbsp;player&nbsp;&lt;&gt;!&nbsp;<span style="color:navy;">other</span>&nbsp;player</pre> </p> <p> The <code>&lt;&gt;!</code> operator is another custom operator defined by Unquote. You can read the expression as <em>player must not equal other player</em>. </p> <p> This property alone doesn't completely define the behaviour of <code>other</code>, but combined with the next property, it does: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``other&nbsp;other&nbsp;returns&nbsp;same&nbsp;player``</span>&nbsp;player&nbsp;=&nbsp;player&nbsp;=!&nbsp;<span style="color:navy;">other</span>&nbsp;(<span style="color:navy;">other</span>&nbsp;player)</pre> </p> <p> If you call <code>other</code>, and then you take the output of that call and use it as input to call <code>other</code> again, you should get the original Player value. This property is an excellent example of what <a href="http://fsharpforfunandprofit.com/posts/property-based-testing-2">Scott Wlaschin calls <em>there and back again</em></a>. </p> <p> These two properties, together, describe the behaviour of the <code>other</code> function, without going into details about the implementation. </p> <h3 id="8a1a6fc27f90470ca293e231109beb73"> Summary <a href="#8a1a6fc27f90470ca293e231109beb73" title="permalink">#</a> </h3> <p> You've seen a simple example of how to describe the properties of a function without resorting to duplicating the implementation in the tests. You may not always be able to do this, but it always feels right when you can. </p> <p> If you're interested in learning more about Property-Based Testing, you can watch my <a href="https://blog.ploeh.dk/property-based-testing-intro">introduction to Property-based Testing with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software: finite state machine https://blog.ploeh.dk/2016/02/18/types--properties--software-finite-state-machine 2016-02-18T08:24:00+00:00 Mark Seemann <div id="post"> <p> <em>How to compose the tennis game code into a finite state machine.</em> </p> <p> This article is the seventh in <a href="/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In the <a href="/2016/02/17/types-properties-software-initial-state">previous article</a>, you saw how to define the initial state of a tennis game. In this article, you'll see how to define the tennis game as a finite state machine. </p> <p> The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>. </p> <h3 id="d01344733f4442f3a79a26a8d48760bb"> Scoring a sequence of balls <a href="#d01344733f4442f3a79a26a8d48760bb" title="permalink">#</a> </h3> <p> Previously, you saw how to score a game in an ad-hoc fashion: </p> <p> <pre>&gt; let firstBall = score newGame PlayerTwo;; val firstBall : Score = Points {PlayerOnePoint = Love; PlayerTwoPoint = Fifteen;} &gt; let secondBall = score firstBall PlayerOne;; val secondBall : Score = Points {PlayerOnePoint = Fifteen; PlayerTwoPoint = Fifteen;}</pre> </p> <p> You'll quickly get tired of that, so you may think that you can do something like this instead: </p> <p> <pre>&gt; newGame |&gt; (fun s -> score s PlayerOne) |&gt; (fun s -> score s PlayerTwo);; val it : Score = Points {PlayerOnePoint = Fifteen; PlayerTwoPoint = Fifteen;}</pre> </p> <p> That does seem a little clumsy, though, but it's not really what you need to be able to do either. What you'd probably appreciate more is to be able to calculate the score given a sequence of wins. A sequence of wins is a list of Player values; for instance, [PlayerOne; PlayerOne; PlayerTwo] means that player one won the first two balls, and then player two won the third ball. </p> <p> Since we already know the initial state of a game, and how to calculate the score for each ball, it's a one-liner to calculate the score for a sequence of balls: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreSeq</span>&nbsp;wins&nbsp;=&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">fold</span>&nbsp;<span style="color:navy;">score</span>&nbsp;newGame&nbsp;wins </pre> </p> <p> This function has the type <code>Player seq -&gt; Score</code>. It uses <code>newGame</code> as the initial state of a left fold over the wins. The aggregator function is the <code>score</code> function. </p> <p> You can use it with a sequence of wins, like this: </p> <p> <pre>&gt; scoreSeq [PlayerOne; PlayerOne; PlayerTwo];; val it : Score = Points {PlayerOnePoint = Thirty; PlayerTwoPoint = Fifteen;}</pre> </p> <p> Since player one won the first two balls, and player two then won the third ball, the score at that point is thirty-fifteen. </p> <h3 id="d7c9207b92424276a04208be109261de"> Properties for the state machine <a href="#d7c9207b92424276a04208be109261de" title="permalink">#</a> </h3> <p> Can you think of any properties for the <code>scoreSeq</code> function? </p> <p> There are quite a few, it turns out. It may be a good exercise if you pause reading for a couple of minutes, and try to think of some properties. </p> <p> If you have a list of properties, you can compare them with the ones I though of. </p> <p> Before we start, you may find the following helper functions useful: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isPoints</span>&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">function</span>&nbsp;<span style="color:navy;">Points</span>&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span>&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isForty</span>&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">function</span>&nbsp;<span style="color:navy;">Forty</span>&nbsp;&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span>&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isDeuce</span>&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">function</span>&nbsp;<span style="color:navy;">Deuce</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span>&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isAdvantage</span>&nbsp;=&nbsp;<span style="color:blue;">function</span>&nbsp;<span style="color:navy;">Advantage</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span>&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isGame</span>&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">function</span>&nbsp;<span style="color:navy;">Game</span>&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">true</span>&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span></pre> </p> <p> These can be useful to express some properties, but I don't think that they are generally useful, so I put them together with the test code. </p> <h3 id="32c26e9107e744a0aa5221a0f190b04d"> Limited win sequences <a href="#32c26e9107e744a0aa5221a0f190b04d" title="permalink">#</a> </h3> <p> If you look at short win sequences, you can already say something about them. For instance, it takes at least four balls to finish a game, so you know that if you have fewer wins than four, the game must still be on-going: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``A&nbsp;game&nbsp;with&nbsp;less&nbsp;then&nbsp;four&nbsp;balls&nbsp;isn&#39;t&nbsp;over``</span>&nbsp;(wins&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>&nbsp;<span style="color:#4ec9b0;">list</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;=&nbsp;wins&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">truncate</span>&nbsp;3&nbsp;|&gt;&nbsp;<span style="color:navy;">scoreSeq</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;(</span><span style="color:navy;background:yellow;">not</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">&lt;&lt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">isGame</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> The built-in function <a href="https://msdn.microsoft.com/en-us/library/ee370325.aspx">Seq.truncate</a> returns only the <em>n</em> (in this case: 3) first elements of a sequence. If the sequence is shorter than that, then the entire sequence is returned. The use of <code>Seq.truncate 3</code> guarantees that the sequence passed to scoreSeq is no longer than three elements long, no matter what <a href="http://fscheck.github.io/FsCheck">FsCheck</a> originally generated. </p> <p> Likewise, you can't reach <em>deuce</em> in less than six balls: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``A&nbsp;game&nbsp;with&nbsp;less&nbsp;than&nbsp;six&nbsp;balls&nbsp;can&#39;t&nbsp;be&nbsp;Deuce``</span>&nbsp;(wins&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>&nbsp;<span style="color:#4ec9b0;">list</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;wins&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">truncate</span>&nbsp;5&nbsp;|&gt;&nbsp;<span style="color:navy;">scoreSeq</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;(</span><span style="color:navy;background:yellow;">not</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">&lt;&lt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">isDeuce</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> This is similar to the previous property. There's one more in that family: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``A&nbsp;game&nbsp;with&nbsp;less&nbsp;than&nbsp;seven&nbsp;balls&nbsp;can&#39;t&nbsp;have&nbsp;any&nbsp;player&nbsp;with&nbsp;advantage``</span> &nbsp;&nbsp;&nbsp;&nbsp;(wins&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>&nbsp;<span style="color:#4ec9b0;">list</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;wins&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">truncate</span>&nbsp;6&nbsp;|&gt;&nbsp;<span style="color:navy;">scoreSeq</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;(</span><span style="color:navy;background:yellow;">not</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">&lt;&lt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">isAdvantage</span><span style="background:yellow;">)&nbsp;@&gt;</span></pre> </p> <p> It takes at least seven balls before the score can reach a state where one of the players have the advantage. </p> <h3 id="92f0f8fdbd924a9986c889f92bd67322"> Longer win sequences <a href="#92f0f8fdbd924a9986c889f92bd67322" title="permalink">#</a> </h3> <p> Conversely, you can also express some properties related to win sequences of a minimum size. This one is more intricate to express with FsCheck, though. You'll need a sequence of Player values, but the sequence should be guaranteed to have a minimum length. There's no built-in in function for that in FsCheck, so you need to define it yourself: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;Gen&lt;Player&nbsp;list&gt;</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">genListLongerThan</span>&nbsp;n&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;playerGen&nbsp;=&nbsp;<span style="color:#4ec9b0;">Arb</span>.generate&lt;<span style="color:#4ec9b0;">Player</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;nPlayers&nbsp;=&nbsp;playerGen&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">listOfLength</span>&nbsp;(n&nbsp;+&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;morePlayers&nbsp;=&nbsp;playerGen&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">listOf</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">map2</span>&nbsp;(@)&nbsp;nPlayers&nbsp;morePlayers</pre> </p> <p> This function takes an exclusive minimum size, and returns an FsCheck generator that will generate Player lists longer than <code>n</code>. It does that by combining two of FsCheck's built-in generators. Gen.listOfLength generates lists of the requested length, and Gen.listOf generates all sorts of lists, including the empty list. </p> <p> Both <code>nPlayers</code> and <code>morePlayers</code> are values of the type Gen&lt;Player list&gt;. Using Gen.map2, you can concatenate these two list generators using F#'s built-in list concatenation operator <code>@</code>. (All operators in F# are also functions. The <code>(@)</code> function has the type <code>'a list -&gt; 'a list -&gt; 'a list</code>.) </p> <p> With the genListLongerThan function, you can now express properties related to longer win sequences. As an example, if the players have played more than four balls, the score can't be a Points case: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``A&nbsp;game&nbsp;with&nbsp;more&nbsp;than&nbsp;four&nbsp;balls&nbsp;can&#39;t&nbsp;be&nbsp;Points``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;moreThanFourBalls&nbsp;=&nbsp;<span style="color:navy;">genListLongerThan</span>&nbsp;4&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;moreThanFourBalls&nbsp;(<span style="color:blue;">fun</span>&nbsp;wins&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">scoreSeq</span>&nbsp;wins &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;(</span><span style="color:navy;background:yellow;">not</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">&lt;&lt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">isPoints</span><span style="background:yellow;">)&nbsp;@&gt;</span>)</pre> </p> <p> As <a href="/2016/02/15/types-properties-software-properties-for-the-forties">previously explained</a>, Prop.forAll expresses a property that must hold for all lists generated by <code>moreThanFourBalls</code>. </p> <p> Correspondingly, if the players have played more than five balls, the score can't be a Forty case: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``A&nbsp;game&nbsp;with&nbsp;more&nbsp;than&nbsp;five&nbsp;balls&nbsp;can&#39;t&nbsp;be&nbsp;Forty``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;moreThanFiveBalls&nbsp;=&nbsp;<span style="color:navy;">genListLongerThan</span>&nbsp;5&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;moreThanFiveBalls&nbsp;(<span style="color:blue;">fun</span>&nbsp;wins&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">scoreSeq</span>&nbsp;wins &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;(</span><span style="color:navy;background:yellow;">not</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">&lt;&lt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">isForty</span><span style="background:yellow;">)&nbsp;@&gt;</span>)</pre> </p> <p> This property is, as you can see, similar to the previous example. </p> <h3 id="9dd248c5743546e1948438bb4f1630c6"> Shortest completed game <a href="#9dd248c5743546e1948438bb4f1630c6" title="permalink">#</a> </h3> <p> Do you still have your list of tennis game properties? Let's see if you thought of this one. The shortest possible way to complete a tennis game is when one of the players wins four balls in a row. This property should hold for all (two) players: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``A&nbsp;game&nbsp;where&nbsp;one&nbsp;player&nbsp;wins&nbsp;all&nbsp;balls&nbsp;is&nbsp;over&nbsp;in&nbsp;four&nbsp;balls``</span>&nbsp;(player)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fourWins&nbsp;=&nbsp;<span style="color:#4ec9b0;">Seq</span>.<span style="color:navy;">init</span>&nbsp;4&nbsp;(<span style="color:blue;">fun</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;player) &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">scoreSeq</span>&nbsp;fourWins &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:navy;">Game</span>&nbsp;player &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> This test first defines <code>fourWins</code>, of the type <code>Player seq</code>. It does that by using the <code>player</code> argument created by FsCheck, and repeating it four times, using Seq.init. </p> <p> The <code>=!</code> operator is a custom operator defined by <a href="http://www.swensensoftware.com/unquote">Unquote</a>, an assertion library. You can read the expression as <em>expected must equal actual</em>. </p> <h3 id="88d92a148e834c42954b5aa1b3d437e6"> Infinite games <a href="#88d92a148e834c42954b5aa1b3d437e6" title="permalink">#</a> </h3> <p> The tennis scoring system turns out to be a rich domain. There are still more properties. We'll look at a final one, because it showcases another way to use FsCheck's API, and then we'll call it a day. </p> <p> An interesting property of the tennis scoring system is that a game isn't guaranteed to end. It may, theoretically, continue forever, if players alternate winning balls: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``A&nbsp;game&nbsp;where&nbsp;players&nbsp;alternate&nbsp;never&nbsp;ends``</span>&nbsp;firstWinner&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;alternateWins&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstWinner &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">constant</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;p&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[p;&nbsp;<span style="color:navy;">other</span>&nbsp;p]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">listOf</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:#4ec9b0;">List</span>.<span style="color:navy;">concat</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;alternateWins&nbsp;(<span style="color:blue;">fun</span>&nbsp;wins&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">scoreSeq</span>&nbsp;wins &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;(</span><span style="color:navy;background:yellow;">not</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">&lt;&lt;</span><span style="background:yellow;">&nbsp;</span><span style="color:navy;background:yellow;">isGame</span><span style="background:yellow;">)&nbsp;@&gt;</span>)</pre> </p> <p> This property starts by creating <code>alternateWins</code>, an Arbitrary&lt;Player list&gt;. It creates lists with alternating players, like [PlayerOne; PlayerTwo], or [PlayerTwo; PlayerOne; PlayerTwo; PlayerOne; PlayerTwo; PlayerOne]. It does that by using the <code>firstWinner</code> argument (of the type Player) as a constant starting value. It's only constant within each test, because <code>firstWinner</code> itself varies. </p> <p> Using that initial Player value, it then proceeds to map that value to a list with two elements: the player, and the other player, using the <code>other</code> function. This creates a Gen&lt;Player list&gt;, but piped into Gen.listOf, it becomes a Gen&lt;Player list list&gt;. An example of the sort of list that would generate could be [[PlayerTwo; PlayerOne]; [PlayerTwo; PlayerOne]]. Obviously, you need to flatten such lists, which you can do with List.concat; you have to do it inside of a Gen, though, so it's <code>Gen.map List.concat</code>. That gives you a Gen&lt;Player list&gt;, which you can finally turn into an Arbitrary&lt;Player list&gt; with Arb.fromGen. </p> <p> This enables you to use Prop.forAll with alternateWins to express that regardless of the size of such alternating win lists, the game never reaches a Game state. </p> <h3 id="3379d6418f6e4d77bf9cf45d36660fb3"> Summary <a href="#3379d6418f6e4d77bf9cf45d36660fb3" title="permalink">#</a> </h3> <p> In this article, you saw how to implement a finite state machine for calculating tennis scores. It's a left fold over the <code>score</code> function, always starting in the initial state where both players are at love. You also saw how you can express various properties that hold for a game of tennis. </p> <p> In this article series, you've seen an extensive example of how to design with types and properties. You may have noticed that out of the six example articles so far, only the first one was about designing with types, and the next five articles were about Property-Based Testing. It looks as though not much is gained from designing with types. </p> <p> On the contrary, much is gained by designing with types, but you don't see it, exactly because it's efficient. If you don't <a href="/2016/02/10/types-properties-software-designing-with-types">design with types</a> in order to <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">make illegal states unrepresentable</a>, you'd have to write even more automated tests in order to test what happens when input is invalid. Also, if illegal states are representable, it would have been much harder to write Property-Based Tests. Instead of trusting that FsCheck generates only valid values based exclusively on the types, you'd have to write custom Generators or Arbitraries for each type of input. That would have been even more work than you've seen in this articles series. </p> <p> Designing with types makes Property-Based Testing a comfortable undertaking. Together, they enable you to develop software that you can trust. </p> <p> If you're interested in learning more about Property-Based Testing, you can watch my <a href="https://blog.ploeh.dk/property-based-testing-intro">introduction to Property-based Testing with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software: initial state https://blog.ploeh.dk/2016/02/17/types--properties--software-initial-state 2016-02-17T08:51:00+00:00 Mark Seemann <div id="post"> <p> <em>How to define the initial state in a tennis game, using F#.</em> </p> <p> This article is the sixth in <a href="/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In the <a href="/2016/02/16/types-properties-software-composition">previous article</a>, you saw how to compose a function that returns a new score based on a previous score, and information about which player won the ball. In this article, you'll see how to define the initial state of a tennis game. </p> <p> The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>. </p> <h3 id="38fb90e0f9b342758719dddb210e8eb8"> Initial state <a href="#38fb90e0f9b342758719dddb210e8eb8" title="permalink">#</a> </h3> <p> You may recall from <a href="/2016/02/10/types-properties-software-designing-with-types">the article on designing with types</a> that a Score is a discriminated union. One of the union cases is the Points case, which you can use to model the case where both players have either Love, Fifteen, or Thirty points. </p> <p> The game starts with both players at love. You can define this as a value: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;newGame&nbsp;=&nbsp;<span style="color:navy;">Points</span>&nbsp;{&nbsp;PlayerOnePoint&nbsp;=&nbsp;<span style="color:navy;">Love</span>;&nbsp;PlayerTwoPoint&nbsp;=&nbsp;<span style="color:navy;">Love</span>&nbsp;} </pre> </p> <p> Since this is a value (that is: not a function), it would make no sense to attempt to test it. Thus, you can simply add it, and move on. </p> <p> You can use this value to calculate ad-hoc scores from the beginning of a game, like this: </p> <p> <pre>&gt; let firstBall = score newGame PlayerTwo;; val firstBall : Score = Points {PlayerOnePoint = Love; PlayerTwoPoint = Fifteen;} &gt; let secondBall = score firstBall PlayerOne;; val secondBall : Score = Points {PlayerOnePoint = Fifteen; PlayerTwoPoint = Fifteen;}</pre> </p> <p> You'll soon get tired of defining <code>firstBall</code>, <code>secondBall</code>, <code>thirdBall</code>, and so on, so a more general way to handle and calculate scores is warranted. </p> <h3 id="886ded4bf4394a0296935af9cfbdaf06"> To be continued... <a href="#886ded4bf4394a0296935af9cfbdaf06" title="permalink">#</a> </h3> <p> In this article, you saw how to define the initial state for a tennis game. There's nothing to it, but armed with this value, you now have half of the requirements needed to turn the tennis score function into a finite state machine. You'll see how to do that <a href="/2016/02/18/types-properties-software-finite-state-machine">in the next article</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software: composition https://blog.ploeh.dk/2016/02/16/types--properties--software-composition 2016-02-16T14:23:00+00:00 Mark Seemann <div id="post"> <p> <em>In which a general transition function is composed from specialised transition functions.</em> </p> <p> This article is the fifth in <a href="/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In the <a href="/2016/02/15/types-properties-software-properties-for-the-forties">previous article</a>, you witnessed the continued walk-through of <a href="http://www.codingdojo.org/cgi-bin/index.pl?KataTennis">the Tennis Kata</a> done with Property-Based Test-Driven Development. In these articles, you saw how to define small, specific functions that model the transition out of particular states. In this article, you'll see how to compose these functions to a more general function. </p> <p> The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>. </p> <h3 id="269e58d73dc34a549e6382b1e65a997f"> Composing the general function <a href="#269e58d73dc34a549e6382b1e65a997f" title="permalink">#</a> </h3> <p> If you recall the <a href="/2016/02/11/types-properties-software-state-transition-properties">second article in this series</a>, what you need to implement is a state transition of the type <code>Score -&gt; Player -&gt; Score</code>. What you have so far are the following functions: <ul> <li><code>scoreWhenPoints : PointsData -&gt; Player -&gt; Score</code></li> <li><code>scoreWhenForty : FortyData -&gt; Player -&gt; Score</code></li> <li><code>scoreWhenDeuce : Player -&gt; Score</code></li> <li><code>scoreWhenAdvantage : Player -&gt; Player -&gt; Score</code></li> <li><code>scoreWhenGame : Player -&gt; Score</code></li> </ul> You've seen the development of <a href="/2016/02/11/types-properties-software-state-transition-properties">scoreWhenDeuce</a>, <a href="/2016/02/12/types-properties-software-properties-for-the-advantage-state">scoreWhenAdvantage</a>, and <a href="/2016/02/15/types-properties-software-properties-for-the-forties">scoreWhenForty</a> in previous articles, but you haven't seen scoreWhenGame or scoreWhenPoints. The development of these remaining functions follow similar principles, and use similar techniques. If you're interested in the details, you can always peruse <a href="https://github.com/ploeh/KataTennis">the source code repository</a>. </p> <p> These five functions are all the building blocks you need to implement the desired function of the type <code>Score -&gt; Player -&gt; Score</code>. You may recall that Score is a discriminated union defined as: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;= |&nbsp;<span style="color:navy;">Points</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">PointsData</span> |&nbsp;<span style="color:navy;">Forty</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">FortyData</span> |&nbsp;<span style="color:navy;">Deuce</span> |&nbsp;<span style="color:navy;">Advantage</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">Player</span> |&nbsp;<span style="color:navy;">Game</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">Player</span></pre> </p> <p> Notice how these cases align with the five functions above. That's not a coincidence. The driving factor behind the design of these five function was to match them with the five cases of the Score type. <a href="/2015/08/10/type-driven-development">In another article series, I've previously shown this technique, applied to a different problem.</a> </p> <p> You can implement the desired function by clicking the pieces together: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">score</span>&nbsp;current&nbsp;winner&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;current&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Points</span>&nbsp;p&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">scoreWhenPoints</span>&nbsp;p&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Forty</span>&nbsp;f&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">scoreWhenForty</span>&nbsp;f&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Deuce</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">scoreWhenDeuce</span>&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Advantage</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">scoreWhenAdvantage</span>&nbsp;a&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Game</span>&nbsp;g&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">scoreWhenGame</span>&nbsp;g</pre> </p> <p> There's not a lot to it, apart from matching on <code>current</code>. If, for example, <code>current</code> is a Forty value, the match is the Forty case, and <code>f</code> represents the FortyData value in that case. The destructured <code>f</code> can be passed as an argument to scoreWhenForty, together with <code>winner</code>. The scoreWhenForty function returns a Score value, so the <code>score</code> function has the type <code>Score -&gt; Player -&gt; Score</code> - exactly what you want! </p> <p> Here's an example of using the function: </p> <p> <pre>&gt; score Deuce PlayerOne;; val it : Score = Advantage PlayerOne</pre> </p> <p> When the score is deuce and player one wins the ball, the resulting score is advantage to player one. </p> <h3 id="83aefbae5f1146c1b4e6daec0fc69717"> Properties for the score function <a href="#83aefbae5f1146c1b4e6daec0fc69717" title="permalink">#</a> </h3> <p> Can you express some properties for the score function? Yes and no. You can't state particularly interesting properties about the function in isolation, but you <em>can</em> express meaningful properties about sequences of scores. We'll return to that in a later article. For now, let's focus on the function in itself. </p> <p> You can, for example, state that the function can handle all input: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``score&nbsp;returns&nbsp;a&nbsp;value``</span>&nbsp;(current&nbsp;:&nbsp;<span style="color:#4ec9b0;">Score</span>)&nbsp;(winner&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;=&nbsp;<span style="color:navy;">score</span>&nbsp;current&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">true</span>&nbsp;<span style="color:green;">//&nbsp;Didn&#39;t&nbsp;crash&nbsp;-&nbsp;this&nbsp;is&nbsp;mostly&nbsp;a&nbsp;boundary&nbsp;condition&nbsp;test</span></pre> </p> <p> This property isn't particularly interesting. It's mostly a <a href="https://en.wikipedia.org/wiki/Smoke_testing_(software)">smoke test</a> that I added because I thought that it might flush out boundary issues, if any exist. That doesn't seem to be the case. </p> <p> You <em>can</em> also add properties that examine each case of input: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``score&nbsp;Points&nbsp;returns&nbsp;correct&nbsp;result``</span>&nbsp;points&nbsp;winner&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">score</span>&nbsp;(<span style="color:navy;">Points</span>&nbsp;points)&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:navy;">scoreWhenPoints</span>&nbsp;points&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> Such a property is unlikely to be of much use, because it mostly reproduces the implementation details of the score function. Unless you're writing high-stakes software (e.g. for medical purposes), such properties are likely to have little or negative value. After all, tests are also code; <a href="/2013/04/02/why-trust-tests">do you trust the test code more than the production code?</a> Sometimes, you may, but if you look at the source code for the score function, it's easy to review. </p> <p> You can write four other properties, similar to the one above, but I'm going to skip them here. They <em>are</em> in <a href="https://github.com/ploeh/KataTennis">the source code repository</a>, though, so you're welcome to look there if you want to see them. </p> <h3 id="072445703d36414cb2f11144ad06ba15"> To be continued... <a href="#072445703d36414cb2f11144ad06ba15" title="permalink">#</a> </h3> <p> In this article, you saw how to compose the five specific state transition functions into an overall state transition function. This is only a single function that calculates a score based on another score. In order to turn this function into a finite state machine, you must define an initial state and a way to transition based on a sequence of events. </p> <p> <a href="/2016/02/17/types-properties-software-initial-state">In the next article, you'll see how to define the initial state</a>, and <a href="/2016/02/18/types-properties-software-finite-state-machine">in the article beyond that, you'll see how to move through a sequence of transitions</a>. </p> <p> If you're interested in learning more about designing with types, you can watch my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software: properties for the Forties https://blog.ploeh.dk/2016/02/15/types--properties--software-properties-for-the-forties 2016-02-15T09:08:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of how to constrain generated input with FsCheck.</em> </p> <p> This article is the fourth in <a href="/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In the <a href="/2016/02/12/types-properties-software-properties-for-the-advantage-state">previous article</a>, you saw how to express properties for a simple state transition: finding the next tennis score when the current score is advantage to a player. These properties were simple, because they had to hold for all input of the given type (Player). In this article, you'll see how to constrain the input that <a href="http://fscheck.github.io/FsCheck">FsCheck</a> generates, in order to express properties about tennis scores where one of the players have forty points. </p> <p> The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>. </p> <h3 id="920a6a4acf564d34a357656e53fd07d2"> Winning the game <a href="#920a6a4acf564d34a357656e53fd07d2" title="permalink">#</a> </h3> <p> When one of the players have forty points, there are three possible outcomes of the next ball: <ul> <li>If the player with forty points wins the ball, (s)he wins the game.</li> <li>If the other player has thirty points, and wins the ball, the score is <em>deuce</em>.</li> <li>If the other player has less than thirty points, and wins the ball, his or her points increases to the next level (from <em>love</em> to fifteen, or from fifteen to thirty).</li> </ul> The first property is the easiest to express, because it doesn't depend on the other player's points. </p> <p> You may recall that when one of the players have forty points, you express that score with the FortyData record type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">FortyData</span>&nbsp;=&nbsp;{&nbsp;Player&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>;&nbsp;OtherPlayerPoint&nbsp;:&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;} </pre> </p> <p> Since Point is defined as <code>Love | Fifteen | Thirty</code>, it's clear that the Player who has forty points has a higher score than the other player - regardless of the OtherPlayerPoint value. This means that if that player wins, (s)he wins the game. It's easy to express that property with FsCheck: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;player:&nbsp;40&nbsp;when&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span> &nbsp;&nbsp;&nbsp;&nbsp;(current&nbsp;:&nbsp;<span style="color:#4ec9b0;">FortyData</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">scoreWhenForty</span>&nbsp;current&nbsp;current.Player &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:navy;">Game</span>&nbsp;current.Player &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> Notice that this test function takes a single function argument called <code>current</code>, of the type FortyData. Since <a href="/2016/02/10/types-properties-software-designing-with-types">illegal states are unrepresentable</a>, FsCheck can only generate legal values of that type. </p> <p> The scoreWhenForty function is a function that explicitly models what happens when the score is in the Forty case. The first argument is the data associated with the current score: <code>current</code>. The second argument is the winner of the next ball. In this test case, you want to express the case where the winner is the player who already has forty points: <code>current.Player</code>. </p> <p> The expected outcome of this is always that <code>current.Player</code> wins the game. </p> <p> The <code>=!</code> operator is a custom operator defined by <a href="http://www.swensensoftware.com/unquote">Unquote</a>, an assertion library. You can read the expression as <em>expected must equal actual</em>. </p> <p> In order to pass this property, you must implement a scoreWhenForty function. This is the simplest implementation that could possible work: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreWhenForty</span>&nbsp;current&nbsp;winner&nbsp;=&nbsp;<span style="color:navy;">Game</span>&nbsp;winner </pre> </p> <p> While this passes all tests, it's obviously not a complete implementation. </p> <h3 id="174c8aef94d24ed69b2d49988730bbd3"> Getting even <a href="#174c8aef94d24ed69b2d49988730bbd3" title="permalink">#</a> </h3> <p> Another outcome of a Forty score is that if the other player has thirty points, and wins the ball, the new score is <em>deuce</em>. Expressing this as a property is only slightly more involved: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;player:&nbsp;40&nbsp;-&nbsp;other:&nbsp;30&nbsp;when&nbsp;other&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span> &nbsp;&nbsp;&nbsp;&nbsp;(current&nbsp;:&nbsp;<span style="color:#4ec9b0;">FortyData</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;current&nbsp;=&nbsp;{&nbsp;current&nbsp;<span style="color:blue;">with</span>&nbsp;OtherPlayerPoint&nbsp;=&nbsp;<span style="color:navy;">Thirty</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">scoreWhenForty</span>&nbsp;current&nbsp;(<span style="color:navy;">other</span>&nbsp;current.Player) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Deuce</span>&nbsp;=!&nbsp;actual</pre> </p> <p> In this test case, the other player specifically has thirty points. There's no variability involved, so you can simply set OtherPlayerPoint to Thirty. </p> <p> Notice that instead of creating a new FortyData value from scratch, this property takes <code>current</code> (which is generated by FsCheck) and uses a copy-and-update expression to explicitly bind OtherPlayerPoint to Thirty. This ensures that all other values of <code>current</code> can vary, but OtherPlayerPoint is fixed. </p> <p> The first line of code shadows the <code>current</code> argument by binding the result of the copy-and-update expression to a new value, also called <code>current</code>. Shadowing means that the original <code>current</code> argument is no longer available in the rest of the scope. This is exactly what you want, because the function argument isn't guaranteed to model the test case where the other player has forty points. Instead, it can be any FortyData value. You can think of the argument provided by FsCheck as a seed used to arrange the <a href="https://en.wikipedia.org/wiki/Test_fixture">Test Fixture</a>. </p> <p> The property proceeds to invoke the scoreWhenForty function with the current score, and indicating that the <code>other</code> player wins the ball. You saw the <code>other</code> function in <a href="/2016/02/12/types-properties-software-properties-for-the-advantage-state">the previous article</a>, but it's so small that it can be repeated here without taking up much space: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">other</span>&nbsp;=&nbsp;<span style="color:blue;">function</span>&nbsp;<span style="color:navy;">PlayerOne</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PlayerTwo</span>&nbsp;|&nbsp;<span style="color:navy;">PlayerTwo</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PlayerOne</span> </pre> </p> <p> Finally, the property asserts that <em>deuce must equal actual</em>. In other words, the expected result is <em>deuce</em>. </p> <p> This property fails until you fix the implementation: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreWhenForty</span>&nbsp;current&nbsp;winner&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;current.Player&nbsp;=&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Game</span>&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Deuce</span></pre> </p> <p> This is a step in the right direction, but still not the complete implementation. If the other player has only love or fifteen points, and wins the ball, the new score shouldn't be deuce. </p> <h3 id="1aa80fb8953c48bb949fec9f2b0b31da"> Incrementing points <a href="#1aa80fb8953c48bb949fec9f2b0b31da" title="permalink">#</a> </h3> <p> The last test case is where it gets interesting. The situation you need to test is that one of the players has forty points, and the other player has <em>either</em> love or fifteen points. This feels like the previous case, but has more variable parts. In the previous test case (above), the other player <em>always</em> had thirty points, but in this test case, the other player's points can vary within a constrained range. </p> <p> Perhaps you've noticed that so far, you haven't seen any examples of using FsCheck's API. Until now, we've been able to express properties from values generated by FsCheck without constraints. This is no longer possible, but fortunately, FsCheck comes with an excellent API that enables you to configure it. Here, you'll see how to configure it to create values from a proper subset of all possible values: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;player:&nbsp;40&nbsp;-&nbsp;other:&nbsp;&lt;&nbsp;30&nbsp;when&nbsp;other&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span> &nbsp;&nbsp;&nbsp;&nbsp;(current&nbsp;:&nbsp;<span style="color:#4ec9b0;">FortyData</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;opp&nbsp;=&nbsp;<span style="color:#4ec9b0;">Gen</span>.<span style="color:navy;">elements</span>&nbsp;[<span style="color:navy;">Love</span>;&nbsp;<span style="color:navy;">Fifteen</span>]&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Arb</span>.<span style="color:navy;">fromGen</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Prop</span>.<span style="color:navy;">forAll</span>&nbsp;opp&nbsp;(<span style="color:blue;">fun</span>&nbsp;otherPlayerPoint&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;current&nbsp;=&nbsp;{&nbsp;current&nbsp;<span style="color:blue;">with</span>&nbsp;OtherPlayerPoint&nbsp;=&nbsp;otherPlayerPoint&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">scoreWhenForty</span>&nbsp;current&nbsp;(<span style="color:navy;">other</span>&nbsp;current.Player) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">incrementPoint</span>&nbsp;current.OtherPlayerPoint &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Option</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;np&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;current&nbsp;<span style="color:blue;">with</span>&nbsp;OtherPlayerPoint&nbsp;=&nbsp;np&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Option</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">Forty</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;<span style="color:navy;">Some</span>&nbsp;actual)</pre> </p> <p> That's only nine lines of code, and some of it is similar to the previous property you saw (above). Still, F# code can have a high degree of information density, so I'll walk you through it. </p> <p> FsCheck's API is centred around <em>Generators</em> and <em>Arbitraries</em>. Ultimately, when you need to configure FsCheck, you'll need to define an Arbitrary&lt;'a&gt;, but you'll often use a Gen&lt;'a&gt; value to do that. In this test case, you need to tell FsCheck to use only the values Love and Fifteen when generating Point values. </p> <p> This is done in the first line of code. <code>Gen.elements</code> creates a Generator that creates random values by drawing from a sequence of possible values. Here, we pass it a list of two values: Love and Fifteen. Because both of these values are of the type Point, the result is a value of the type Gen&lt;Point&gt;. This Generator is piped to Arb.fromGen, which turns it into an Arbitrary (for now, you don't have to worry about exactly what that means). Thus, <code>opp</code> is a value of type Arbitrary&lt;Point&gt;. </p> <p> You can now take that Arbitrary and state that, for all values created by it, a particular property must hold. This is what Prop.forAll does. The first argument passed is <code>opp</code>, and the second argument is a function that expresses the property. When the test runs, FsCheck call this function 100 times (by default), each time passing a random value generated by <code>opp</code>. </p> <p> The next couple of lines are similar to code you've seen before. As in the case where the other player had thirty points, you can shadow the <code>current</code> argument with a new value where the other player's points is set to a value drawn from <code>opp</code>; that is, Love or Fifteen. </p> <p> Notice how the original <code>current</code> value comes from an argument to the containing test function, whereas <code>otherPlayerPoint</code> comes from the <code>opp</code> Arbitrary. FsCheck is smart enough to enable this combination, so you still get the variation implied by combining these two sources of random data. </p> <p> The <code>actual</code> value is bound to the result of calling scoreWhenForty with the current score, and indicating that the other player wins the ball. </p> <p> The expected outcome is a new Forty value that originates from <code>current</code>, but with the other player's points incremented. There is, however, something odd-looking going on with Option.map - and where did that <code>incrementPoint</code> function come from? </p> <p> In the previous article, you saw how sometimes, a test triggers the creation of a new helper function. Sometimes, such a helper function is of such general utility that it makes sense to put it in the 'production code'. Previously, it was the <code>other</code> function. Now, it's the <code>incrementPoint</code> function. </p> <p> Before I show you the implementation of the <code>incrementPoint</code> function, I would like to suggest that you reflect on it. The purpose of this function is to return the point that comes after a given point. Do you remember how, in <a href="/2016/02/10/types-properties-software-designing-with-types">the article on designing with types</a>, we quickly realised that love, fifteen, thirty, and forty are mere labels; that we don't need to do arithmetic on these values? </p> <p> There's one piece of 'arithmetic' you need to do with these values, after all: you must be able to 'add one' to a value, in order to get the next value. That's the purpose of the <code>incrementPoint</code> function: given Love, it'll return Fifteen, and so on - but with a twist! </p> <p> What should it return when given Thirty as input? Forty? That's not possible. There's no Point value higher than Thirty. Forty doesn't exist. </p> <p> The object-oriented answer would be to throw an exception, but in functional programming, we don't like such arbitrary jump statements in our code. GOTO is, after all, considered harmful. </p> <p> Instead, we can return None, but that means that we must wrap all the other return values in Some: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">incrementPoint</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Love</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;<span style="color:navy;">Fifteen</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Fifteen</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;<span style="color:navy;">Thirty</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Thirty</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> This function has the type <code>Point -&gt; Point option</code>, and it behaves like this: </p> <p> <pre>&gt; incrementPoint Love;; val it : Point option = Some Fifteen &gt; incrementPoint Fifteen;; val it : Point option = Some Thirty &gt; incrementPoint Thirty;; val it : Point option = None</pre> </p> <p> Back to the property: the <code>expected</code> value is that the other player's points are incremented, and this new points value (<code>np</code>, for <em>New Points</em>) is bound to OtherPlayerPoint in a copy-and-update expression, using <code>current</code> as the original value. In other words, this expression returns the <code>current</code> score, with the only change that OtherPlayerPoint now has the incremented Point value. </p> <p> This has to happen inside of an Option.map, though, because incrementPoint may return None. Furthermore, the new value created from <code>current</code> is of the type FortyData, but you need a Forty value. This can be achieved by piping the option value into another map that composes the Forty case constructor. </p> <p> The <code>expected</code> value has the type <code>Score option</code>, so in order to be able to compare it to <code>actual</code>, which is 'only' a <code>Score</code> value, you need to make <code>actual</code> a <code>Score option</code> value as well. This is the reason <code>expected</code> is compared to <code>Some actual</code>. </p> <p> One implementation that passes this and all previous properties is: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreWhenForty</span>&nbsp;current&nbsp;winner&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;current.Player&nbsp;=&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Game</span>&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;<span style="color:navy;">incrementPoint</span>&nbsp;current.OtherPlayerPoint&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Some</span>&nbsp;p&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Forty</span>&nbsp;{&nbsp;current&nbsp;<span style="color:blue;">with</span>&nbsp;OtherPlayerPoint&nbsp;=&nbsp;p&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">None</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Deuce</span></pre> </p> <p> Notice that the implementation also uses the incrementPoint function. </p> <h3 id="31d25a78e0e0431dafd3fa25b0c9ef9e"> To be continued... <a href="#31d25a78e0e0431dafd3fa25b0c9ef9e" title="permalink">#</a> </h3> <p> In this article, you saw how, even when illegal states are unrepresentable, you may need to further constrain the input into a property in order to express a particular test case. The FsCheck combinator library can be used to do that. It's flexible and well thought-out. </p> <p> While I could go on and show you how to express properties for more state transitions, you've now seen the most important techniques. If you want to see more of the tennis state transitions, you can always check out <a href="https://github.com/ploeh/KataTennis">the source code accompanying this article series</a>. </p> <p> <a href="/2016/02/16/types-properties-software-composition">In the next article, instead, you'll see how to compose all these functions</a> into a system that implements the tennis scoring rules. </p> <p> If you're interested in learning more about Property-Based Testing, you can watch my <a href="https://blog.ploeh.dk/property-based-testing-intro">introduction to Property-based Testing with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software: properties for the advantage state https://blog.ploeh.dk/2016/02/12/types--properties--software-properties-for-the-advantage-state 2016-02-12T08:41:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of Property-Based Test-Driven Development.</em> </p> <p> This article is the third in <a href="/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In the <a href="/2016/02/11/types-properties-software-state-transition-properties">previous article</a>, you saw how to get started with Property-Based Testing, using a Test-Driven Development tactic. In this article, you'll see the previous Tennis Kata example continued. This time, you'll see how to express properties for the state when one of the players have the advantage. </p> <p> The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>. </p> <h3 id="352c24dd4e0848b084d08dec67dd7da1"> Winning the game <a href="#352c24dd4e0848b084d08dec67dd7da1" title="permalink">#</a> </h3> <p> When one of the players have the advantage in tennis, the result can go one of two ways: either the player with the advantage wins the ball, in which case he or she wins the game, or the other player wins, in which case the next score is <em>deuce</em>. This implies that you'll have to write at least two properties: one for each situation. Let's start with the case where the advantaged player wins the ball. Using FsCheck, you can express that property like this: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;advantage&nbsp;when&nbsp;advantaged&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span> &nbsp;&nbsp;&nbsp;&nbsp;(advantagedPlayer&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;=&nbsp;<span style="color:navy;">scoreWhenAdvantage</span>&nbsp;advantagedPlayer&nbsp;advantagedPlayer &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:navy;">Game</span>&nbsp;advantagedPlayer &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> As explained in the previous article, <a href="http://fscheck.github.io/FsCheck">FsCheck</a> will interpret this function and discover that it'll need to generate arbitrary Player values for the <code>advantagedPlayer</code> argument. Because <a href="/2016/02/10/types-properties-software-designing-with-types">illegal states are unrepresentable</a>, you're guaranteed valid values. </p> <p> This property calls the <code>scoreWhenAdvantage</code> function (that doesn't yet exist), passing <code>advantagedPlayer</code> as argument twice. The first argument is an indication of the current score. The scoreWhenAdvantage function only models how to transition out of the Advantage case. The data associated with the Advantage case is the player currently having the advantage, so passing in advantagedPlayer as the first argument describes the current state to the function. </p> <p> The second argument is the winner of the ball. In this test case, the same player wins again, so advantagedPlayer is passed as the second argument as well. The exact value of advantagedPlayer doesn't matter; this property holds for all (two) players. </p> <p> The <code>=!</code> operator is a custom operator defined by <a href="http://www.swensensoftware.com/unquote">Unquote</a>, an assertion library. You can read the expression as <em>expected must equal actual</em>. </p> <p> In order to pass this property, you can implement the function in the simplest way that could possibly work: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreWhenAdvantage</span>&nbsp;advantagedPlayer&nbsp;winner&nbsp;=&nbsp;<span style="color:navy;">Game</span>&nbsp;advantagedPlayer </pre> </p> <p> Here, I've arbitrarily chosen to return <code>Game advantagedPlayer</code>, but as an alternative, <code>Game winner</code> also passes the test. </p> <h3 id="ced50fef87864f3abd0d0c07849f24d1"> Back to deuce <a href="#ced50fef87864f3abd0d0c07849f24d1" title="permalink">#</a> </h3> <p> The above implementation of scoreWhenAdvantage is obviously incorrect, because it always claims that the advantaged player wins the game, regardless of who wins the ball. You'll need to describe the other test case as well, which is slightly more involved, yet still easy: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;advantage&nbsp;when&nbsp;other&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span> &nbsp;&nbsp;&nbsp;&nbsp;(advantagedPlayer&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:navy;">scoreWhenAdvantage</span>&nbsp;advantagedPlayer&nbsp;(<span style="color:navy;">other</span>&nbsp;advantagedPlayer) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Deuce</span>&nbsp;=!&nbsp;actual</pre> </p> <p> The first argument to the scoreWhenAdvantage function describes the current score. The test case is that the other player wins the ball. In order to figure out who the other player is, you can call the <code>other</code> function. </p> <p> Which <code>other</code> function? </p> <p> The function you only now create for this express purpose: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">other</span>&nbsp;=&nbsp;<span style="color:blue;">function</span>&nbsp;<span style="color:navy;">PlayerOne</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PlayerTwo</span>&nbsp;|&nbsp;<span style="color:navy;">PlayerTwo</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PlayerOne</span> </pre> </p> <p> As the name suggests, this function returns the other player, for any given player. In the previous article, I promised to avoid <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free style</a>, but here I broke that promise. This function is equivalent to this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">other</span>&nbsp;player&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;player&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">PlayerOne</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PlayerTwo</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">PlayerTwo</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">PlayerOne</span></pre> </p> <p> I decided to place this function in the same module as the scoreWhenGame function, because it seemed like a generally useful function, more than a test-specific function. It turns out that the tennis score module, indeed, does need this function later. </p> <p> Since the <code>other</code> function is part of the module being tested, shouldn't you test it as well? For now, I'll leave it uncovered by directed tests, because it's so simple that I'm confident that it works, just by looking at it. Later, I can <a href="/2016/02/19/types-properties-software-other-properties">return to it in order to add some properties</a>. </p> <p> With the <code>other</code> function in place, the new property fails, so you need to change the implementation of scoreWhenAdvantage in order to pass all tests: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreWhenAdvantage</span>&nbsp;advantagedPlayer&nbsp;winner&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;advantagedPlayer&nbsp;=&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Game</span>&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Deuce</span></pre> </p> <p> This implementation deals with all combinations of input. </p> <p> It turned out that, in this case, two properties are all we need in order to describe which tennis score comes after <em>advantage</em>. </p> <h3 id="93442914c6ec4b04a32c2bdf0a2fb7f5"> To be continued... <a href="#93442914c6ec4b04a32c2bdf0a2fb7f5" title="permalink">#</a> </h3> <p> In this article, you saw how to express the properties associated with the <em>advantage</em> state of a tennis game. These properties were each simple. You can express each of them based on any arbitrary input of the given type, as shown here. </p> <p> Even when all test input values are guaranteed to be valid, sometimes you need to manipulate an arbitrary test value in order to describe a particular test case. You'll see how to do this in <a href="/2016/02/15/types-properties-software-properties-for-the-forties">the next article</a>. </p> <p> If you're interested in learning more about Property-Based Testing, you can watch my <a href="https://blog.ploeh.dk/property-based-testing-intro">introduction to Property-based Testing with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software: state transition properties https://blog.ploeh.dk/2016/02/11/types--properties--software-state-transition-properties 2016-02-11T08:54:00+00:00 Mark Seemann <div id="post"> <p> <em>Specify valid state transitions as properties.</em> </p> <p> This article is the second in <a href="/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In the <a href="/2016/02/10/types-properties-software-designing-with-types">previous article</a>, you saw how to design with types so that <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">illegal states are unrepresentable</a>. In this article, you'll see an example of how to express properties for transitions between legal states. </p> <p> The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>. </p> <p> This article continues the Tennis Kata example from the previous article. It uses <a href="http://fscheck.github.io/FsCheck">FsCheck</a> to generate test values, and <a href="http://xunit.github.io">xUnit.net</a> as the overall test framework. It also uses <a href="http://www.swensensoftware.com/unquote">Unquote</a> for assertions. </p> <h3 id="f6474cd8fbe249389b9879b00a6ab4b7"> State transitions <a href="#f6474cd8fbe249389b9879b00a6ab4b7" title="permalink">#</a> </h3> <p> While the types defined in the previous article make illegal states unrepresentable, they don't enforce any rules about how to transition from one state into another. There's yet no definition of what a <em>state transition</em> is, in the tennis domain. Let's make a definition, then. </p> <p> A state transition should be a function that takes a current Score and the winner of a ball and returns a new Score. More formally, it should have the type <code>Score -&gt; Player -&gt; Score</code>. </p> <p> (If you're thinking that all this terminology sounds like we're developing a finite state machine, you're bang on; that's exactly the case.) </p> <p> For a simple domain like tennis, it'd be possible to define properties directly for such a function, but I often prefer to define a smaller function for each case, and test the properties of each of these functions. When you have all these small functions, you can easily combine them into the desired state transition function. This is the strategy you'll see in use here. </p> <h3 id="bf87a520664f428693363f1600e1fa6e"> Deuce property <a href="#bf87a520664f428693363f1600e1fa6e" title="permalink">#</a> </h3> <p> The tennis types defined in the previous article guarantee that when you ask FsCheck to generate values, you will get only legal values. This makes it easy to express properties for transitions. Let's write the property first, and let's start with the simplest state transition: the transition out of <em>deuce</em>. </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Given&nbsp;deuce&nbsp;when&nbsp;player&nbsp;wins&nbsp;then&nbsp;score&nbsp;is&nbsp;correct``</span> &nbsp;&nbsp;&nbsp;&nbsp;(winner&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;=&nbsp;<span style="color:navy;">scoreWhenDeuce</span>&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:navy;">Advantage</span>&nbsp;winner &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> This test exercises a transition function called <code>scoreWhenDeuce</code>. The case of <em>deuce</em> is special, because there's no further data associated with the state; when the score is deuce, it's deuce. This means that when calling scoreWhenDeuce, you don't have to supply the current state of the game; it's implied by the function itself. </p> <p> You do need, however, to pass a Player argument in order to state which player won the ball. Instead of coming up with some hard-coded examples, the test simply requests Player values from FsCheck (by requiring the <code>winner</code> function argument). </p> <p> Because the Player type makes illegal states unrepresentable, you're guaranteed that only valid Player values will be passed as the <code>winner</code> argument. </p> <p> (In this particular example, Player can only be the values PlayerOne and PlayerTwo. FsCheck will, because of its default settings, run the property function 100 times, which means that it will generate 100 Player values. With an even distribution, that means that it will generate approximately 50 PlayerOne values, and 50 PlayerTwo values. Wouldn't it be easier, and faster, to use a <code>[&lt;Theory&gt;]</code> that deterministically generates only those two values, without duplication? Yes, in this case it would; <a href="/2015/02/23/property-based-testing-without-a-property-based-testing-framework">This sometimes happens, and it's okay</a>. In this example, though, I'm going to keep using FsCheck, because I think this entire example is a good stand-in for a more complex business problem.) </p> <p> Regardless of the value of the <code>winner</code> argument, the property should hold that the return value of the <code>scoreWhenDeuce</code> function is that the winner now has the advantage. </p> <p> The <code>=!</code> operator is a custom operator defined by Unquote. You can read it as a <em>must equal</em> operator: <em>expected must equal actual</em>. </p> <h3 id="f9df785f6ded441c94dba7aff262dda0"> Red phase <a href="#f9df785f6ded441c94dba7aff262dda0" title="permalink">#</a> </h3> <p> When you apply Test-Driven Development, you should follow the Red/Green/Refactor cycle. In this example, we're doing just that, but at the moment the code doesn't even compile, because there's no scoreWhenDeuce function. </p> <p> In the Red phase, it's important to observe that the test fails as expected before moving on to the Green phase. In order to make that happen, you can create this temporary implementation of the function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreWhenDeuce</span>&nbsp;winner&nbsp;=&nbsp;<span style="color:navy;">Deuce</span> </pre> </p> <p> With this definition, the code compiles, and when you run the test, you get this test failure: </p> <p> <pre>Test 'Ploeh.Katas.TennisProperties.Given deuce when player wins then score is correct' failed: FsCheck.Xunit.PropertyFailedException : Falsifiable, after 1 test (0 shrinks) (StdGen (427100699,296115298)): Original: PlayerOne ---- Swensen.Unquote.AssertionFailedException : Test failed: Advantage PlayerOne = Deuce false</pre> </p> <p> This test failure is the expected failure, so you should now feel confident that the property is correctly expressed. </p> <h3 id="cc318eb5b5b2413b90cf0a114e783f1e"> Green phase <a href="#cc318eb5b5b2413b90cf0a114e783f1e" title="permalink">#</a> </h3> <p> With the Red phase properly completed, it's time to move on to the Green phase: make the test(s) pass. For deuce, this is trivial: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreWhenDeuce</span>&nbsp;winner&nbsp;=&nbsp;<span style="color:navy;">Advantage</span>&nbsp;winner </pre> </p> <p> This passes 'all' tests: </p> <p> <pre>Output from Ploeh.Katas.TennisProperties.Given deuce when player wins then score is correct: Ok, passed 100 tests.</pre> </p> <p> The property holds. </p> <h3 id="2c3950c44950422992211bf09fd81b61"> Refactor <a href="#2c3950c44950422992211bf09fd81b61" title="permalink">#</a> </h3> <p> When you follow the Red/Green/Refactor cycle, you should now refactor the implementation, but there's little to do at this point </p> <p> You could, in fact, perform an <em>eta reduction</em>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">scoreWhenDeuce</span>&nbsp;=&nbsp;<span style="color:navy;">Advantage</span> </pre> </p> <p> This would be <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> in Haskell, but not quite so much in F#. In my experience, many people find the <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free style</a> less readable, so I'm not going to pursue this type of refactoring for the rest of this article series. </p> <h3 id="394ff992a9f042fea703dfe2d5061818"> To be continued... <a href="#394ff992a9f042fea703dfe2d5061818" title="permalink">#</a> </h3> <p> In this article, you've seen how to express the single property that when the score is deuce, the winner of the next ball will have the advantage. Because illegal states are unrepresentable, you can declaratively state the type of value(s) the property requires, and FsCheck will have no choice but to give you valid values. </p> <p> <a href="/2016/02/12/types-properties-software-properties-for-the-advantage-state">In the next article, you'll see how to express properties for slightly more complex state transitions</a>. In this article, I took care to spell out each step in the process, but in the next article, I promise to increase the pace. </p> <p> If you're interested in learning more about Property-Based Testing, you can watch my <a href="https://blog.ploeh.dk/property-based-testing-intro">introduction to Property-based Testing with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software: designing with types https://blog.ploeh.dk/2016/02/10/types--properties--software-designing-with-types 2016-02-10T12:27:00+00:00 Mark Seemann <div id="post"> <p> <em>Design types so that illegal states are unrepresentable.</em> </p> <p> This article is the first in <a href="/2016/02/10/types-properties-software">a series of articles that demonstrate how to develop software using types and properties</a>. In this article, you'll see an example of how to design with algebraic data types, and in future articles, you'll see how to specify properties that must hold for the system. This example uses F#, but you can design similar types in Haskell. </p> <p> The source code for this article series is <a href="https://github.com/ploeh/KataTennis">available on GitHub</a>. </p> <h3 id="bf92e5167cf648988de5a68f7e9c48d7"> Tennis <a href="#bf92e5167cf648988de5a68f7e9c48d7" title="permalink">#</a> </h3> <p> The example used in this series of articles is <a href="http://www.codingdojo.org/cgi-bin/index.pl?KataTennis">the Tennis Kata</a>. Although a tennis match consists of multiple <em>sets</em> that again are played as several <em>games</em>, in the kata, you only have to implement the scoring system for a single game: <ul> <li>Each player can have either of these points in one game: Love, 15, 30, 40.</li> <li>If you have 40 and you win the ball, you win the game. There are, however, special rules.</li> <li>If both have 40, the players are <em>deuce</em>.</li> <ul> <li>If the game is in deuce, the winner of a ball will have advantage and game ball.</li> <li>If the player with advantage wins the ball, (s)he wins the game.</li> <li>If the player without advantage wins, they are back at deuce.</li> </ul> </ul> This problem is easy enough that it's fun to play with, but difficult enough that it's fun to play with. </p> <p> You can take on this problem in many different ways, but in this article, you'll see how F#'s type system can be used to <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">make illegal states unrepresentable</a>. Perhaps you think this is overkill for such a simple problem, but think of the Tennis Kata as a stand-in for a more complex domain problem. </p> <h3 id="aea6478fe7f94d04aa408163f5551204"> Players <a href="#aea6478fe7f94d04aa408163f5551204" title="permalink">#</a> </h3> <p> In tennis, there are two players, which we can easily model with a discriminated union: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Player</span>&nbsp;=&nbsp;<span style="color:navy;">PlayerOne</span>&nbsp;|&nbsp;<span style="color:navy;">PlayerTwo</span> </pre> </p> <p> When designing with types, I often experiment with values in FSI (the F# REPL) to figure out if they make sense. For such a simple type as Player, that's not strictly necessary, but I'll show you anyway, take illustrate the point: </p> <p> <pre>&gt; PlayerOne;; val it : Player = PlayerOne &gt; PlayerTwo;; val it : Player = PlayerTwo &gt; PlayerZero;; PlayerZero;; ^^^^^^^^^^ error FS0039: The value or constructor 'PlayerZero' is not defined &gt; PlayerThree;; PlayerThree;; ^^^^^^^^^^^ error FS0039: The value or constructor 'PlayerThree' is not defined</pre> </p> <p> As you can see, both <code>PlayerOne</code> and <code>PlayerTwo</code> are values inferred to be of the type Player, whereas both <code>PlayerZero</code> and <code>PlayerThree</code> are illegal expressions. </p> <p> Not only is it possible to represent all valid values, but illegal values are unrepresentable. Success. </p> <h3 id="97c6f15e772e4a96a7384562dd3fe981"> Naive point attempt with a type alias <a href="#97c6f15e772e4a96a7384562dd3fe981" title="permalink">#</a> </h3> <p> If you're unfamiliar with designing with types, you may briefly consider using a type alias to model players' points: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;=&nbsp;<span style="color:#4ec9b0;">int</span> </pre> </p> <p> This easily enables you to model some of the legal point values: </p> <p> <pre>&gt; let p : Point = 15;; val p : Point = 15 &gt; let p : Point = 30;; val p : Point = 30</pre> </p> <p> It looks good so far, but how do you model <em>love</em>? It's not really an integer. </p> <p> Still, both players start with <em>love</em>, so it's intuitive to try to model <em>love</em> as 0: </p> <p> <pre>&gt; let p : Point = 0;; val p : Point = 0</pre> </p> <p> It's a hack, but it works. </p> <p> Are illegal values unrepresentable? Hardly: </p> <p> <pre>&gt; let p : Point = 42;; val p : Point = 42 &gt; let p : Point = -1337;; val p : Point = -1337</pre> </p> <p> With a type alias, it's possible to assign every value that the 'real' type supports. For a 32-bit integer, this means that we have four legal representations (0, 15, 30, 40), and 4,294,967,291 illegal representations of a tennis point. Clearly this doesn't meet the goal of making illegal states unrepresentable. </p> <h3 id="8409c0f2733f4000a0ad7464bf6a3d23"> Second point attempt with a discriminated Union <a href="#8409c0f2733f4000a0ad7464bf6a3d23" title="permalink">#</a> </h3> <p> If you think about the problem for a while, you're likely to come to the realisation that love, 15, 30, and 40 aren't numbers, but rather <em>labels</em>. No arithmetic is performed on them. It's easy to constrain the domain of points with a discriminated union: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;=&nbsp;<span style="color:navy;">Love</span>&nbsp;|&nbsp;<span style="color:navy;">Fifteen</span>&nbsp;|&nbsp;<span style="color:navy;">Thirty</span>&nbsp;|&nbsp;<span style="color:navy;">Forty</span> </pre> </p> <p> You can play around with values of this Point type in FSI if you will, but there should be no surprises. </p> <p> A Point value isn't a score, though. A score is a representation of a state in the game, with (amongh other options) a point to each player. You can model this with a record: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">PointsData</span>&nbsp;=&nbsp;{&nbsp;PlayerOnePoint&nbsp;:&nbsp;<span style="color:#4ec9b0;">Point</span>;&nbsp;PlayerTwoPoint&nbsp;:&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;} </pre> </p> <p> You can experiment with this type in FSI, like you can with the other types: </p> <p> <pre>&gt; { PlayerOnePoint = Love; PlayerTwoPoint = Love };; val it : PointsData = {PlayerOnePoint = Love; PlayerTwoPoint = Love;} &gt; { PlayerOnePoint = Love; PlayerTwoPoint = Thirty };; val it : PointsData = {PlayerOnePoint = Love; PlayerTwoPoint = Thirty;} &gt; { PlayerOnePoint = Thirty; PlayerTwoPoint = Forty };; val it : PointsData = {PlayerOnePoint = Thirty; PlayerTwoPoint = Forty;}</pre> </p> <p> That looks promising. What happens if players are evenly matched? </p> <p> <pre>&gt; { PlayerOnePoint = Forty; PlayerTwoPoint = Forty };; val it : PointsData = {PlayerOnePoint = Forty; PlayerTwoPoint = Forty;}</pre> </p> <p> That works as well, but it shouldn't! </p> <p> <em>Forty-forty</em> isn't a valid tennis score; it's called <em>deuce</em>. </p> <p> Does it matter? you may ask. Couldn't we 'just' say that forty-forty 'means' deuce and get on with it? </p> <p> You could. You should try. I've tried doing this (I've done the Tennis Kata several times, in various languages), and it turns out that it's possible, but it <em>makes the code more complicated</em>. One of the reasons is that deuce is more than a synonym for forty-forty. The score can also be deuce after <em>advantage</em> to one of the players. You can, for example, have a score progression like this: <ul> <li>...</li> <li>Forty-thirty</li> <li>Deuce</li> <li>Advantage to player one</li> <li>Deuce</li> <li>Advantage to player two</li> <li>Deuce</li> <li>...</li> </ul> While you may prefer to think of the first occurrence of <em>deuce</em> in that list as <em>forty-forty</em>, the next occurrences clearly don't correspond to <em>forty-forty</em>. </p> <p> Additionally, if you're into <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>, you prefer using the ubiquitous language of the domain. When the tennis domain language says that it's not called <em>forty-forty</em>, but <em>deuce</em>, the code should reflect that. </p> <h3 id="76dfc9607ef44891b0a45c77e4dcac63"> Final attempt at a point type <a href="#76dfc9607ef44891b0a45c77e4dcac63" title="permalink">#</a> </h3> <p> Fortunately, you don't have to throw away everything you've done so far. The love-love, fifteen-love, etc. values that you can represent with the above PointsData type are all valid. Only when you approach the boundary value of forty do problems appear. </p> <p> A solution is to remove the offending Forty case from Point. The updated definition of Point is this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;=&nbsp;<span style="color:navy;">Love</span>&nbsp;|&nbsp;<span style="color:navy;">Fifteen</span>&nbsp;|&nbsp;<span style="color:navy;">Thirty</span> </pre> </p> <p> You can still represent the 'early' scores using PointsData: </p> <p> <pre>&gt; { PlayerOnePoint = Love; PlayerTwoPoint = Love };; val it : PointsData = {PlayerOnePoint = Love; PlayerTwoPoint = Love;} &gt; { PlayerOnePoint = Love; PlayerTwoPoint = Thirty };; val it : PointsData = {PlayerOnePoint = Love; PlayerTwoPoint = Thirty;}</pre> </p> <p> Additionally, the illegal forty-forty state is now unrepresentable: </p> <p> <pre>&gt; { PlayerOnePoint = Forty; PlayerTwoPoint = Forty };; { PlayerOnePoint = Forty; PlayerTwoPoint = Forty };; -------------------^^^^^ error FS0039: The value or constructor 'Forty' is not defined</pre> </p> <p> This is much better, apart from the elephant in the room: you also lost the ability to model valid states where only one of the players have forty points: </p> <p> <pre>&gt; { PlayerOnePoint = Thirty; PlayerTwoPoint = Forty };; { PlayerOnePoint = Thirty; PlayerTwoPoint = Forty };; --------------------------------------------^^^^^ error FS0039: The value or constructor 'Forty' is not defined</pre> </p> <p> Did we just throw the baby out with the bathwater? </p> <p> Not really, because we weren't close to being done anyway. While we were making progress on modelling the score as a pair of Point values, remaining work includes modelling deuce, advantage and winning the game. </p> <h3 id="c1dc48e87323450d8c0adf030da366f9"> Life begins at forty <a href="#c1dc48e87323450d8c0adf030da366f9" title="permalink">#</a> </h3> <p> At this point, it may be helpful to recap what we have: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Player</span>&nbsp;=&nbsp;<span style="color:navy;">PlayerOne</span>&nbsp;|&nbsp;<span style="color:navy;">PlayerTwo</span> <span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;=&nbsp;<span style="color:navy;">Love</span>&nbsp;|&nbsp;<span style="color:navy;">Fifteen</span>&nbsp;|&nbsp;<span style="color:navy;">Thirty</span> <span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">PointsData</span>&nbsp;=&nbsp;{&nbsp;PlayerOnePoint&nbsp;:&nbsp;<span style="color:#4ec9b0;">Point</span>;&nbsp;PlayerTwoPoint&nbsp;:&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;}</pre> </p> <p> While this enables you to keep track of the score when both players have less than forty points, the following phases of a game still remain: <ul> <li>One of the players have forty points.</li> <li>Deuce.</li> <li>Advantage to one of the players.</li> <li>One of the players won the game.</li> </ul> You can design the first of these with another record type: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">FortyData</span>&nbsp;=&nbsp;{&nbsp;Player&nbsp;:&nbsp;<span style="color:#4ec9b0;">Player</span>;&nbsp;OtherPlayerPoint&nbsp;:&nbsp;<span style="color:#4ec9b0;">Point</span>&nbsp;} </pre> </p> <p> This is a record that specifically keeps track of the situation where <em>one</em> of the players have forty points. The Player element keeps track of which player has forty points, and the OtherPlayerPoint element keeps track of the other player's score. For instance, this value indicates that PlayerTwo has forty points, and PlayerOne has thirty: </p> <p> <pre>&gt; { Player = PlayerTwo; OtherPlayerPoint = Thirty };; val it : FortyData = {Player = PlayerTwo; OtherPlayerPoint = Thirty;}</pre> </p> <p> This is a legal score. Other values of this type exist, but none of them are illegal. </p> <h3 id="6f21f30092f14241ba59effee1cf1225"> Score <a href="#6f21f30092f14241ba59effee1cf1225" title="permalink">#</a> </h3> <p> Now you have two distinct types, PointsData and FortyData, that keep track of the score at two different phases of a tennis game. You still need to model the remaining three phases, and somehow turn all of these into a single type. This is an undertaking that can be surprisingly complicated in C# or Java, but is trivial to do with a discriminated union: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Score</span>&nbsp;= |&nbsp;<span style="color:navy;">Points</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">PointsData</span> |&nbsp;<span style="color:navy;">Forty</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">FortyData</span> |&nbsp;<span style="color:navy;">Deuce</span> |&nbsp;<span style="color:navy;">Advantage</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">Player</span> |&nbsp;<span style="color:navy;">Game</span>&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#4ec9b0;">Player</span></pre> </p> <p> This Score type states that a score is a value in one of these five cases. As an example, the game starts with both players at (not necessarily <em>in</em>) love: </p> <p> <pre>&gt; Points { PlayerOnePoint = Love; PlayerTwoPoint = Love };; val it : Score = Points {PlayerOnePoint = Love; PlayerTwoPoint = Love;}</pre> </p> <p> If, for example, PlayerOne has forty points, and PlayerTwo has thirty points, you can create this value: </p> <p> <pre>&gt; Forty { Player = PlayerOne; OtherPlayerPoint = Thirty };; val it : Score = Forty {Player = PlayerOne; OtherPlayerPoint = Thirty;}</pre> </p> <p> Notice how both values are of the same type (Score), even though they don't share the same data structure. Other example of valid values are: </p> <p> <pre>&gt; Deuce;; val it : Score = Deuce &gt; Advantage PlayerTwo;; val it : Score = Advantage PlayerTwo &gt; Game PlayerOne;; val it : Score = Game PlayerOne</pre> </p> <p> This model of the tennis score system enables you to express all legal values, while making illegal states unrepresentable. </p> <p> It's impossible to express that the score is seven-eleven: </p> <p> <pre>&gt; Points { PlayerOnePoint = Seven; PlayerTwoPoint = Eleven };; Points { PlayerOnePoint = Seven; PlayerTwoPoint = Eleven };; --------------------------^^^^^ error FS0039: The value or constructor 'Seven' is not defined</pre> </p> <p> It's impossible to state that the score is fifteen-thirty-fifteen: </p> <p> <pre>&gt; Points { PlayerOnePoint = Fifteen; PlayerTwoPoint = Thirty; PlayerThreePoint = Fifteen };; Points { PlayerOnePoint = Fifteen; PlayerTwoPoint = Thirty; PlayerThreePoint = Fifteen };; ------------------------------------------------------------^^^^^^^^^^^^^^^^ error FS1129: The type 'PointsData' does not contain a field 'PlayerThreePoint'</pre> </p> <p> It's impossible to express that both players have forty points: </p> <p> <pre>&gt; Points { PlayerOnePoint = Forty; PlayerTwoPoint = Forty };; Points { PlayerOnePoint = Forty; PlayerTwoPoint = Forty };; --------------------------^^^^^ error FS0001: This expression was expected to have type Point but here has type FortyData -> Score &gt; Forty { Player = PlayerOne; OtherPlayerPoint = Forty };; Forty { Player = PlayerOne; OtherPlayerPoint = Forty };; -----------------------------------------------^^^^^ error FS0001: This expression was expected to have type Point but here has type FortyData -&gt; Score</pre> </p> <p> In the above example, I even attempted to express forty-forty in two different ways, but none of them work. </p> <h3 id="8a5f261903f74f4c9e4f75d93c395e62"> Summary <a href="#8a5f261903f74f4c9e4f75d93c395e62" title="permalink">#</a> </h3> <p> You now have a model of the domain that enables you to express valid values, but that makes illegal states unrepresentable. This is half the battle won, without any moving parts. These types govern what can be stated in the domain, but they don't provide any rules for how values can transition from one state to another. </p> <p> This is a task you can take on with Property-Based Testing. Since all values of these types are valid, it's easy to express the properties of a tennis game score. In <a href="/2016/02/11/types-properties-software-state-transition-properties">the next article, you'll see how to start that work</a>. </p> <p> If you're interested in learning more about designing with types, you can watch my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Types + Properties = Software https://blog.ploeh.dk/2016/02/10/types--properties--software 2016-02-10T11:50:00+00:00 Mark Seemann <div id="post"> <p> <em>Combine a strong type system with Property-Based Testing to specify software.</em> </p> <p> When Kent Beck <a href="https://www.quora.com/Why-does-Kent-Beck-refer-to-the-rediscovery-of-test-driven-development">rediscovered Test-Driven Development</a> (TDD) some twenty years ago, the original context was SmallTalk programming. When the concept of TDD began to catch on, it seemed to me to proliferate particularly in dynamic language communities like Python and Ruby. </p> <p> It makes sense that if you can't get fast feedback from a compilation step, you seek another way to get feedback on your work. <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">Unit testing is such a fast feedback mechanism</a>. Watching the NodeJS community from the side, it always seemed to me that TDD is an integral way of working with that stack. This makes sense. </p> <p> This also explains why e.g. C# programmers were more reluctant to adopt TDD. That your code compiles gives a smidgen of confidence. </p> <p> C# and Java programmers may joke that <em>if it compiles, it works</em>, but also realise that that's all it is: a joke. Automated testing adds another layer of safety on top of compilation. On the other hand, you may get by with writing fewer tests than in a dynamic language, because the static type system does provide <em>some</em> guarantees. </p> <p> What if you had a stronger type system than what C# or Java provides? Then you'd have to write even fewer tests. </p> <h3 id="4aa13342e7cc469e8b1135ae63e9d72e"> Type spectrum <a href="#4aa13342e7cc469e8b1135ae63e9d72e" title="permalink">#</a> </h3> <p> It can be useful to think about typing as a spectrum. </p> <p> <img src="/content/binary/type-spectrum.png" alt="A spectrum of languages, from most dynamic on the left, to most static on the right."> </p> <p> To the left, we have dynamically typed languages like JavaScript and Ruby. Python <em>can</em> be compiled, and Clojure <em>is</em> compiled, so we can think of them as being a little closer to the middle. Purists would want to point out that whether or not a language is compiled and how statically typed it is are two independent concerns, but the point of this spectrum is to illustrate the degree of confidence the type system gives you. </p> <p> Java and C# are statically typed, compiled languages, but a lot of things can still go wrong at run-time. Just consider all the null pointer exceptions you've encountered over the years. </p> <p> Further to the right are languages with <a href="https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system">Hindley–Milner type systems</a>, such as F# and Haskell. These offer more safety from static typing alone. Such types are the subject of this and future articles. </p> <p> I've put Haskell a bit to the right of F# because it has higher-order types. To the right of Haskell sits dependently-typed languages like Idris. To the far right, we have a hypothetical language with such a strong type system that, indeed, <em>if it compiles, it works</em>. As far as I'm aware, no such language exist. </p> <h3 id="c2e012be45f6462ba9f25311943e265f"> Creating software with Hindley-Milner type systems <a href="#c2e012be45f6462ba9f25311943e265f" title="permalink">#</a> </h3> <p> It turns out that when you shift from a Java/C#-style type system to the stronger type system offered by F# or Haskell, you can further shift your design emphasis towards designing with types. You still need to cover behaviour with automated tests, but fewer are necessary. </p> <p> With the <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a> available in F# or Haskell, you can design your types so that <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">illegal states are unrepresentable</a>. In other words, all values of a particular type are guaranteed to be valid within the domain they model. This makes it much easier to test the behaviour of your system with Property-Based Testing, because you can declaratively state that you want your Property-Based Testing framework (FsCheck, QuickCheck, etc.) to provide random values of a given type. The framework will give you random values, but it can only give you valid values. </p> <p> In this series of articles, you'll see an example of this in F#: <ul> <li><a href="/2016/02/10/types-properties-software-designing-with-types">Types + Properties = Software: designing with types</a></li> <li><a href="/2016/02/11/types-properties-software-state-transition-properties">Types + Properties = Software: state transition properties</a></li> <li><a href="/2016/02/12/types-properties-software-properties-for-the-advantage-state">Types + Properties = Software: properties for the advantage state</a></li> <li><a href="/2016/02/15/types-properties-software-properties-for-the-forties">Types + Properties = Software: properties for the Forties</a></li> <li><a href="/2016/02/16/types-properties-software-composition">Types + Properties = Software: composition</a></li> <li><a href="/2016/02/17/types-properties-software-initial-state">Types + Properties = Software: initial state</a></li> <li><a href="/2016/02/18/types-properties-software-finite-state-machine">Types + Properties = Software: finite state machine</a></li> <li><a href="/2016/02/19/types-properties-software-other-properties">Types + Properties = Software: other properties</a></li> </ul> If you're interested in learning more, my <a href="/2015/08/10/type-driven-development">Type Driven Development</a> article showcases another example of designing with types. My Pluralsight course <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> dives even deeper than the article. If you need an <a href="https://blog.ploeh.dk/property-based-testing-intro">introduction to Property-based Testing with F#</a>, that's also available. </p> <h3 id="ede6dde5b56340b0bcd55ceeaed939c3"> Summary <a href="#ede6dde5b56340b0bcd55ceeaed939c3" title="permalink">#</a> </h3> <p> The stronger a language's type system is, the more you can use static types to model your application's problem domain. With a sufficiently strong type system, you can make illegal states unrepresentable. While states are guaranteed to be legal, transitions between states need not be. You can often use Property-Based Testing to ensure that the state transitions are correct. The combination of types and properties give you a simple, but surprisingly powerful way of specifying the behaviour of the software you create. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Are shuffled plays random? https://blog.ploeh.dk/2016/02/06/are-shuffled-plays-random 2016-02-06T00:09:00+00:00 Mark Seemann <div id="post"> <p> <em>Have you ever asked your music player to shuffle all your music, only to wonder: didn't it play that song only yesterday?</em> </p> <p> When I work, I often listen to music. Sometimes, I make a deliberate choice, but often, I just ask my music player to shuffle my entire music library. </p> <p> It invariably happens that it plays a song it played only a couple of days before; I often notice when it's a song I don't like, because then it annoys me that it plays that stupid song <em>again</em>. Given that I have more than 16,000 tracks, and approximately 5,000 tracks on my laptop, it seems unlikely that the music player should pick the same track within only a few days. </p> <p> This is a well-known phenomenon known as <em>frequency illusion</em>, or more colloquially as the <em>Baader-Meinhof Phenomenon</em>. Your brain seeks patterns, even where no patterns exist. This is a really strong feeling, it turns out. </p> <p> When tracks are played in truly random fashion, it's as unlikely that it should play two tracks far apart as it should play them close together. Therefore, many media players come with shuffle algorithms that are <em>not</em> random, but designed to <em>feel</em> random. </p> <p> With my thousands of tracks, I couldn't shake the feeling that my player's shuffle algorithm was one of those non-random algorithms, so I decided to analyse my plays. </p> <h3 id="9ea877bbec4546069b83cff49da07e48"> Getting the data in shape <a href="#9ea877bbec4546069b83cff49da07e48" title="permalink">#</a> </h3> <p> Fortunately, I've been <a href="http://www.last.fm/user/ploeh">scrobbling my plays to Last.fm</a> since 2007, so I had comprehensive statistics to work with. </p> <p> There's <a href="http://benjaminbenben.com/lastfm-to-csv/">a third-party service where you can download all your Last.fm scrobbles</a> as a CSV file, so I did that; all 53,700 scrobbles. </p> <p> This file contains all my scrobbles since 2007, but I had to clean it up: <ul> <li>I haven't used the same music player in all those years, so I decide to only look at the entries since January 1, 2014. I know I've only used one player in that period.</li> <li>It often happens that I put on a particular artist, album, or playlist, and I had to remove those, because I wanted to look at only the entries when I was listening in shuffle mode.</li> </ul> Opening the file in Excel, and manually cleaning it up according to the above criteria was easy, and took perhaps 15 minutes. What was left was approximately 8,500 rows in a CSV file. </p> <h3 id="3e7d8600e6284419867590a8430bc9bb"> Analysing with F# <a href="#3e7d8600e6284419867590a8430bc9bb" title="permalink">#</a> </h3> <p> Next, I created an F# script file, and read in the file's contents: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;System <span style="color:blue;">open</span>&nbsp;System.IO <span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Track</span>&nbsp;=&nbsp;{&nbsp;Artist&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>;&nbsp;Album&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>;&nbsp;Name&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>&nbsp;} <span style="color:blue;">type</span>&nbsp;<span style="color:#4ec9b0;">Scrobble</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Track&nbsp;:&nbsp;<span style="color:#4ec9b0;">Track</span>;&nbsp;Time&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>&nbsp;} <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">parse</span>&nbsp;(line&nbsp;:&nbsp;<span style="color:#4ec9b0;">string</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;line.<span style="color:navy;">Split</span>&nbsp;<span style="color:#a31515;">&#39;|&#39;</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[|&nbsp;artist;&nbsp;album;&nbsp;track;&nbsp;time&nbsp;|]&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Track&nbsp;=&nbsp;{&nbsp;Artist&nbsp;=&nbsp;artist;&nbsp;Album&nbsp;=&nbsp;album;&nbsp;Name&nbsp;=&nbsp;track&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time&nbsp;=&nbsp;time&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span> <span style="color:blue;">let</span>&nbsp;scrobbles&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">File</span>.<span style="color:navy;">ReadAllLines</span>&nbsp;<span style="color:#a31515;">@&quot;Last.fm_ploeh_shuffled.csv&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Array</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">parse</span></pre> </p> <p> This script reads the file, with each line an element in an array. It then parses each line into Scrobble values. There may be parse errors, if a line contains the wrong number of elements, but this turned out to not be the case: </p> <p> <pre>&gt; let hasParseErrors = scrobbles |> Array.exists (Option.isNone);; val hasParseErrors : bool = false</pre> </p> <p> The next step was to group the scrobbles into distinct tracks, with a count of each track: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;counts&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;scrobbles &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Array</span>.<span style="color:navy;">choose</span>&nbsp;<span style="color:navy;">id</span> &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Array</span>.<span style="color:navy;">countBy</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;s.Track) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">Array</span>.<span style="color:navy;">sortByDescending</span>&nbsp;<span style="color:navy;">snd</span></pre> </p> <p> This gave me a sorted 'hit list', showing me the songs played the most in the top of the list. Here's the top 10: </p> <p> <pre>DJ Zen, Peace Therapy, Anamatha (Intro) : ....... Abakus, That Much Closer to the Sun, Igmatik : ....... Yoji Biomehanika, Technicolor Nrg Show, seduction (sleep tonight remix): ...... Mindsphere, Patience for Heaven. CD 2: Inner Cyclone, inner cyclone : ...... Massive Attack, Mezzanine, Black Milk : ...... Dimension 5, TransAddendum, Strange Phenomena : ...... Dire Straits, Brothers in Arms, Your Latest Trick : ...... Digicult, Out Of This World, Star Travel : ...... Infected Mushroom, Classical Mushroom, Bust A Move : ...... Brother Brown feat. Frank'ee, Under The Water, Under The Water (Or[...]: ......</pre> </p> <p> None of these, with the exception of <em>Under The Water</em>, are particular favourites of mine, so I don't think these were tracks I'd definitely selected; remember: I'd made a deliberate effort to remove such tracks before starting the analysis. </p> <p> This list tells <em>me</em> something, because I know what I like and don't like, but it doesn't tell me much about the distribution of tracks. The two most 'popular' tracks had been played 7 times over the last two years, and the remaining tracks 6 times. How about the other 5,155 entries in <code>counts</code>? How were they distributed? </p> <p> It's easy to count how many tracks were played 7 times, how many were played 6 times, 5 times, etc.: </p> <p> <pre>&gt; let frequencyCounts = counts |> Array.countBy snd;; val frequencyCounts : (int * int) [] = [|(7, 2); (6, 9); (5, 43); (4, 159); (3, 561); (2, 1475); (1, 2916)|]</pre> </p> <p> It looks as though the 'popular' tracks are rare occurrences. Most tracks (2,916) were played only once, and 1,475 only twice. This is perhaps easier to understand when visualised. </p> <h3 id="a7d6c5d3e1704f6eb957aea2419dc4c3"> Visualisation <a href="#a7d6c5d3e1704f6eb957aea2419dc4c3" title="permalink">#</a> </h3> <p> While there are great visualisation packages for F#, for this ad-hoc task, I found it easier to use Excel. After all, I only had 7 observations to plot: </p> <p> <img src="/content/binary/scrobble-frequency-plot.png" alt="A scatter plot showing the frequency of plays."> </p> <p> This figure shows that almost 3,000 tracks were scrobbled only once, approximately 1,500 were scrobbled twice, 500 or so were scrobbled thrice, and so on. </p> <p> Even if we expect the distribution to be entirely uniform, we should expect to see duplicates. While I have more than 16,000 tracks on my home system, I only have some 5,000 tracks on my laptop, and these are the tracks for which I have data. Since I had 8,500 scrobbles to analyse, there <em>must</em> be duplicates, but we shouldn't expect to see two plays being more common than one, because even with an even distribution, you can't play all tracks twice when you have 5,000 tracks, but 'only' 8,500 plays. </p> <p> That there are some 500 tracks that have been played three times don't seem unlikely in this light. Furthermore, outliers are to be expected as well; while they're there, the figure also shows that they promptly taper off to insignificance. </p> <p> Although I took theoretical statistics in university (I had to), I never liked it, and got the lowest passing grade. In other words: don't trust any of this. I haven't formally analysed it with proper statistical tools. </p> <h3 id="2255451f787d4b01a9ad51fa45dfb4ab"> Conclusion <a href="#2255451f787d4b01a9ad51fa45dfb4ab" title="permalink">#</a> </h3> <p> Still, based on this little Friday exercise, I now feel renewed confidence that my music player does, indeed, pick tracks at random when I tell it to play all songs shuffled. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Make pre-conditions explicit in Property-Based Tests https://blog.ploeh.dk/2016/01/18/make-pre-conditions-explicit-in-property-based-tests 2016-01-18T12:00:00+00:00 Mark Seemann <div id="post"> <p> <em>Pre-conditions are important in Property-Based Tests.</em> </p> <p> Last week, I was giving a workshop on Property-Based Testing, and we were looking at this test: </p> <p> <pre>[&lt;Property&gt;] <span style="color:blue;">let</span>&nbsp;``Validate.reservation&nbsp;returns&nbsp;right&nbsp;result&nbsp;on&nbsp;invalid&nbsp;date`` &nbsp;&nbsp;&nbsp;&nbsp;(rendition&nbsp;:&nbsp;ReservationRendition)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Validate.reservation&nbsp;rendition &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;:&nbsp;Result&lt;Reservation,&nbsp;Error&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Failure(ValidationError(<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">expected</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">=</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;@&gt;</span></pre> </p> <p> The test case being exercised by this test is to verify what happens when the input (essentially a representation of an incoming JSON document) is invalid. </p> <p> The System Under Test is this function: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:#4ec9b0;">Validate</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">reservation</span>&nbsp;(rendition&nbsp;:&nbsp;<span style="color:#4ec9b0;">ReservationRendition</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;rendition.Date&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">DateTimeOffset</span>.<span style="color:navy;">TryParse</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;(<span style="color:blue;">true</span>,&nbsp;date)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Success</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;date &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp;rendition.Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp;=&nbsp;rendition.Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity&nbsp;=&nbsp;rendition.Quantity&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Failure</span>(<span style="color:navy;">ValidationError</span>&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span>)</pre> </p> <p> The validation rule is simple: if the rendition.Date string can be parsed into a DateTimeOffset value, then the input is valid; otherwise, it's not. </p> <p> If you run the test, if passes: </p> <p> <pre>Output from Ploeh.Samples.BookingApi.UnitTests.ValidatorTests.Validate.reservation returns right result on invalid date: Ok, passed 100 tests.</pre> </p> <p> Only, this test isn't <em>guaranteed</em> to pass. Can you spot the problem? </p> <p> There's a tiny, but real, risk that the randomly generated rendition.Date string <em>can</em> be parsed as a DateTimeOffset value. While it's not particularly likely, it will happen once in a blue moon. This can lead to spurious <a href="http://xunitpatterns.com/false%20positive.html">false positives</a>. </p> <p> In order to see this, you can increase the number of test cases generated by FsCheck: </p> <p> <pre>[&lt;Property(MaxTest&nbsp;=&nbsp;100000)&gt;] <span style="color:blue;">let</span>&nbsp;``Validate.reservation&nbsp;returns&nbsp;right&nbsp;result&nbsp;on&nbsp;invalid&nbsp;date`` &nbsp;&nbsp;&nbsp;&nbsp;(rendition&nbsp;:&nbsp;ReservationRendition)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Validate.reservation&nbsp;rendition &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;:&nbsp;Result&lt;Reservation,&nbsp;Error&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Failure(ValidationError(<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">expected</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">=</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;@&gt;</span></pre> </p> <p> In my experience, when you ask for 100,000 test cases, the property usually fails: </p> <p> <pre>Test 'Ploeh.Samples.BookingApi.UnitTests.ValidatorTests.Validate.reservation returns right result on invalid date' failed: FsCheck.Xunit.PropertyFailedException : Falsifiable, after 9719 tests (11 shrinks) (StdGen (1054829489,296106988)): Original: {Date = "7am"; Name = "6WUx;"; Email = "W"; Quantity = -5;} Shrunk: {Date = "7am"; Name = ""; Email = ""; Quantity = 0;} ---- Swensen.Unquote.AssertionFailedException : Test failed: Failure (ValidationError "Invalid date.") = Success {Date = 15.01.2016 07:00:00 +00:00; Name = ""; Email = ""; Quantity = 0;} false</pre> </p> <p> The problem is that the generated date "7am" <em>can</em> be parsed as a DateTimeOffset value: </p> <p> <pre>&gt; open System;; &gt; DateTimeOffset.Now;; val it : DateTimeOffset = 15.01.2016 21:09:25 +00:00 &gt; "7am" |&gt; DateTimeOffset.TryParse;; val it : bool * DateTimeOffset = (true, 15.01.2016 07:00:00 +00:00)</pre> </p> <p> The above test is written with the implicit assumption that the generated value for rendition.Date will always be an invalidly formatted string. As <a href="https://www.python.org/dev/peps/pep-0020">the Zen of Python</a> reminds us, <em>explicit is better than implicit</em>. You should make that assumption explicit: </p> <p> <pre>[&lt;Property&gt;] <span style="color:blue;">let</span>&nbsp;``Validate.reservation&nbsp;returns&nbsp;right&nbsp;result&nbsp;on&nbsp;invalid&nbsp;date`` &nbsp;&nbsp;&nbsp;&nbsp;(rendition&nbsp;:&nbsp;ReservationRendition)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;not(fst(DateTimeOffset.TryParse&nbsp;rendition.Date))&nbsp;==&gt;&nbsp;<span style="color:blue;">lazy</span> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Validate.reservation&nbsp;rendition &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;:&nbsp;Result&lt;Reservation,&nbsp;Error&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Failure(ValidationError(<span style="color:#a31515;">&quot;Invalid&nbsp;date.&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">expected</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">=</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;@&gt;</span></pre> </p> <p> This version of the test uses FsCheck's built-in custom operator <code>==&gt;</code>. This operator will only evaluate the expression on the right-hand side if the boolean expression on the left-hand side evaluates to <code>true</code>. In the unlikely event that the above condition evaluates to <code>false</code>, FsCheck is going to ignore that randomly generated value, and try with a new one. </p> <p> This version of the test succeeds, even if you set MaxTest to 1,000,000. </p> <p> The <code>==&gt;</code> operator effectively discards candidate values that don't match the 'guard' condition. You shouldn't always use this feature to constrain the input, as it can be wasteful. If the condition isn't satisfied, FsCheck will attempt to generate a new value that satisfies the condition. In this particular example, only in extremely rare cases will FsCheck be forced to discard a candidate value, so this use of the feature is appropriate. </p> <p> In other cases, you should consider other options. Imagine that you want only even numbers. While you could write a 'guard' condition that ensures that only even numbers are used, that would cause FsCheck to throw away half of the generated candidates. In such cases, you should instead consider defining a custom Arbitrary, using FsCheck's API. </p> <p> Another discussion entirely is whether the current behaviour is a good idea. If we consider tests as a feedback mechanism about software design, then perhaps we should explicitly specify the expected format. At the moment, the implementation is an extremely <a href="http://martinfowler.com/bliki/TolerantReader.html">Tolerant Reader</a> (because DateTimeOffset.TryParse is a Tolerant Reader). Perhaps it'd make sense to instead use <a href="https://msdn.microsoft.com/en-us/library/system.datetimeoffset.tryparseexact">TryParseExact</a>. That's an API modelling decision, though, and might have to involve various other stakeholders than only programmers. </p> <p> If you wish to learn more about Property-Based Testing with FsCheck, consider watching <a href="https://blog.ploeh.dk/property-based-testing-intro">my Pluralsight course</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tail Recurse https://blog.ploeh.dk/2015/12/22/tail-recurse 2015-12-22T09:11:00+00:00 Mark Seemann <div id="post"> <p> <em>Tips on refactoring recursive functions to tail-recursive functions.</em> </p> <p> In a recent article, I described <a href="/2015/12/01/recurse">how to refactor an imperative loop to a recursive function</a>. If you're coming from C# or Java, however, you may have learned to avoid recursion, since it leads to stack overflows when the recursion is too deep. </p> <p> In some Functional languages, like F# and Haskell, such stack overflows can be prevented with <a href="https://en.wikipedia.org/wiki/Tail_call">tail recursion</a>. If the <em>last</em> function call being made is a recursive call to the function itself, the current stack frame can be eliminated because execution is effectively completed. This allows a tail-recursive function to keep recursing without ever overflowing. </p> <p> When you have a recursive function that's <em>not</em> tail recursive, however, it can sometimes be difficult to figure out how to refactor it so that it becomes tail recursive. In this article, I'll outline some techniques I've found to be effective. </p> <h3 id="4f80428a94344fe1bf99245749f2ff69"> Introduce an accumulator <a href="#4f80428a94344fe1bf99245749f2ff69" title="permalink">#</a> </h3> <p> It seems as though the universal trick related to recursion is to introduce an accumulator argument. This is <a href="/2015/12/01/recurse">how you refactor an imperative loop to a recursive function</a>, but it can also be used to make a non-tail-recursive function tail recursive. This will also require you to introduce an 'implementation function' that does the actual work. </p> <h3 id="5169722dc6aa4e8b98800ee6d1ce99e0"> Example: discarding candidate points when finding a convex hull <a href="#5169722dc6aa4e8b98800ee6d1ce99e0" title="permalink">#</a> </h3> <p> In <a href="/2015/10/19/visual-value-verification">previous articles</a> I've described various problems I had when implementing the <a href="https://en.wikipedia.org/wiki/Graham_scan">Graham Scan</a> algorithm to find the <a href="https://en.wikipedia.org/wiki/Convex_hull">convex hull</a> of a set of points. </p> <p> One of my functions examined the three last points in a sequence of points in order to determine if the next-to-last point is in the interior of the set, or if that point could potentially be on the hull. If the point is positively known to be in the interior of the set, it should be discarded. At one stage, the function was implemented this way: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryDiscard</span>&nbsp;points&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">tryDiscardImp</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[p1;&nbsp;p2;&nbsp;p3]&nbsp;<span style="color:blue;">when</span>&nbsp;<span style="color:navy;">turn</span>&nbsp;p1&nbsp;p2&nbsp;p3&nbsp;=&nbsp;<span style="color:#4ec9b0;">Direction</span>.Right&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[p1;&nbsp;p3] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[p1;&nbsp;p2;&nbsp;p3]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[p1;&nbsp;p2;&nbsp;p3] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;p&nbsp;<span style="color:navy;">::</span>&nbsp;ps&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;p&nbsp;<span style="color:navy;">::</span>&nbsp;<span style="color:navy;">tryDiscardImp</span>&nbsp;ps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newPoints&nbsp;=&nbsp;<span style="color:navy;">tryDiscardImp</span>&nbsp;points &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;newPoints.Length&nbsp;&lt;&gt;&nbsp;points.Length &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;newPoints &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> This function was earlier called <code>check</code>, which is the name used in <a href="/2015/12/01/recurse">the article about refactoring to recursion</a>. The tryDiscard function is actually an inner function in a more complex function that's defined with the <code>inline</code> keyword, so the type of tryDiscard is somewhat complicated, but think of it as having the type <code>(int * int) list -&gt; (int * int) list option</code>. If a point was discarded, the new, reduced list of points is returned in a Some case; otherwise, None is returned. </p> <p> The tryDiscard function already has an 'implementation function' called tryDiscardImp, but while tryDiscardImp is recursive, it isn't <em>tail</em> recursive. The problem is that in the <code>p :: ps</code> case, the recursive call to tryDiscardImp isn't the tail call. Rather, the stack frame has to wait for the recursive call <code>tryDiscardImp ps</code> to complete, because only then can it cons <code>p</code> onto its return value. </p> <p> Since an 'implementation function' already exists, you can make it tail recursive by adding an accumulator argument to tryDiscardImp: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryDiscard</span>&nbsp;points&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">tryDiscardImp</span>&nbsp;acc&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[p1;&nbsp;p2;&nbsp;p3]&nbsp;<span style="color:blue;">when</span>&nbsp;<span style="color:navy;">turn</span>&nbsp;p1&nbsp;p2&nbsp;p3&nbsp;=&nbsp;<span style="color:#4ec9b0;">Direction</span>.Right&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;@&nbsp;[p1;&nbsp;p3] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[p1;&nbsp;p2;&nbsp;p3]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;acc&nbsp;@&nbsp;[p1;&nbsp;p2;&nbsp;p3] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;p&nbsp;<span style="color:navy;">::</span>&nbsp;ps&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">tryDiscardImp</span>&nbsp;(acc&nbsp;@&nbsp;[p])&nbsp;ps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newPoints&nbsp;=&nbsp;<span style="color:navy;">tryDiscardImp</span>&nbsp;[]&nbsp;points &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;newPoints.Length&nbsp;&lt;&gt;&nbsp;points.Length &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;newPoints &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> As you can see, I added the <code>acc</code> argument to tryDiscardImp; it has the type <code>(int * int) list</code> (again: not really, but close enough). Instead of returning from each case, the tryDiscardImp function now appends points to <code>acc</code> until it reaches the end of the list, which is when it returns the accumulator. The <code>p :: ps</code> case now <em>first</em> appends the point in consideration to the accumulator (<code>acc @ [p]</code>), and only then recursively calls tryDiscardImp. This puts the call to tryDiscardImp in the tail position. </p> <p> The repeated use of the append operator (<code>@</code>) is terribly inefficient, though, but I'll return to this issue later in this article. For now, let's take a step back. </p> <h3 id="f15281234b484c6380b2139515536a55"> Example: implementing map with recursion <a href="#f15281234b484c6380b2139515536a55" title="permalink">#</a> </h3> <p> A common exercise for people new to Functional Programming is to implement a map function (C# developers will know it as <a href="https://msdn.microsoft.com/library/bb548891">Select</a>) using recursion. This function <a href="https://msdn.microsoft.com/en-us/library/ee370378.aspx">already exists in the List module</a>, but it can be enlightening to do the exercise. </p> <p> An easy, but naive implementation is only two lines of code, using pattern matching: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">mapNaive</span>&nbsp;<span style="color:navy;">f</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;h<span style="color:navy;">::</span>t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;h&nbsp;<span style="color:navy;">::</span>&nbsp;<span style="color:navy;">mapNaive</span>&nbsp;<span style="color:navy;">f</span>&nbsp;t</pre> </p> <p> Is mapNaive tail recursive? No, it isn't. The last operation happening is that <code>f h</code> is consed unto the return value of <code>mapNaive f t</code>. While <code>mapNaive f t</code> is a recursive call, it's not in the tail position. For long lists, this will create a stack overflow. </p> <p> How can you create a tail-recursive map implementation? </p> <h3 id="7e4e30b794594de2895d126f8a0ef622"> Example: inefficient tail-recursive map implementation <a href="#7e4e30b794594de2895d126f8a0ef622" title="permalink">#</a> </h3> <p> According to my introduction, adding an accumulator and an 'implementation' function should do the trick. Here's the straightforward application of that technique: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">mapTailRecursiveUsingAppend</span>&nbsp;<span style="color:navy;">f</span>&nbsp;xs&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;acc&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;h<span style="color:navy;">::</span>t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;(acc&nbsp;@&nbsp;[<span style="color:navy;">f</span>&nbsp;h])&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;[]&nbsp;xs</pre> </p> <p> The mapImp function does the actual work, and it's tail recursive. It appends the result of mapping the head of the list unto the accumulator: <code>acc @ [f h]</code>. Only then does it recursively call itself with the new accumulator and the tail of the list. </p> <p> While this version is tail recursive, it's horribly inefficient, because appending to the tail of a (linked) list is inefficient. In theory, this implementation would never result in a stack overflow, but the question is whether anyone has the patience to wait for that to happen? </p> <p> <pre>&gt; mapTailRecursiveUsingAppend ((*) 2) [1..100000];; Real: 00:02:46.104, CPU: 00:02:44.750, GC gen0: 13068, gen1: 6380, gen2: 1 val it : int list = [2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; ...]</pre> </p> <p> Doubling 100,000 integers this way takes nearly 3 minutes on my (admittedly mediocre) laptop. A better approach is required. </p> <h3 id="09a1591c7d8743dca23abf5c7a863096"> Example: efficient tail-recursive map implementation <a href="#09a1591c7d8743dca23abf5c7a863096" title="permalink">#</a> </h3> <p> The problem with mapTailRecursiveUsingAppend is that appending to a list is slow when the left-hand list is long. This is because lists are linked lists, so the append operation has to traverse the entire list and copy all the element to link to the right-hand list. </p> <p> Consing a single item unto an existing list, on the other hand, is efficient: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">mapTailRecursiveUsingRev</span>&nbsp;<span style="color:navy;">f</span>&nbsp;xs&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;acc&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;h<span style="color:navy;">::</span>t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;h&nbsp;<span style="color:navy;">::</span>&nbsp;acc)&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;[]&nbsp;xs&nbsp;|&gt;&nbsp;<span style="color:#4ec9b0;">List</span>.<span style="color:navy;">rev</span></pre> </p> <p> This function conses unto the accumulator (<code>f h :: acc</code>) instead of appending to the accumulator. The only problem with this is that <code>acc</code> is in reverse order compared to the input, so the final step must be to reverse the output of mapImp. While there's a cost to reversing a list, you pay it only once. In practice, it turns out to be efficient: </p> <p> <pre>&gt; mapTailRecursiveUsingRev ((*) 2) [1..100000];; Real: 00:00:00.017, CPU: 00:00:00.015, GC gen0: 1, gen1: 0, gen2: 0 val it : int list = [2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; ...]</pre> </p> <p> From nearly three minutes to 17 milliseconds! That's a nice performance improvement. </p> <p> The only problem, from a point of view where <em>learning</em> is in focus, is that it feels a bit like cheating: we've delegated an important step in the algorithm to <a href="https://msdn.microsoft.com/en-us/library/ee340277.aspx">List.rev</a>. If we think it's OK to use the library functions, we could have directly used <a href="https://msdn.microsoft.com/en-us/library/ee370378.aspx">List.map</a>. The whole point of this exercise, however, is to learn something about how to write tail-recursive functions. </p> <p> At this point, we have two options: <ul> <li>Learn how to write an efficient, tail-recursive implementation of <code>rev</code>.</li> <li>Consider alternatives.</li> </ul> Ultimately, it turns out that List.rev is <a href="https://github.com/Microsoft/visualfsharp/blob/master/src/fsharp/FSharp.Core/local.fs">implemented in a fashion similar to above, using an 'implementation function' and an accumulator</a>. Let us, therefore, turn our attention to another alternative. </p> <h3 id="36ebbcd8edf1428cbd8894cccbdb1fb5"> Example: efficient tail-recursive map using a difference list <a href="#36ebbcd8edf1428cbd8894cccbdb1fb5" title="permalink">#</a> </h3> <p> The mapTailRecursiveUsingAppend function is attractive because of its simplicity. If only there was an efficient way to append a single item to the tail of a (long) list, like <code>acc appendSingle (f h)</code>! (appendSingle is a hypothetical function that we wish existed.) </p> <p> So far, we've treated data as data, and functions as functions, but in Functional Programming, functions are data as well! </p> <p> What if we could <em>partially</em> apply a cons operation? </p> <p> Unfortunately, the cons operator (<code>::</code>) <a href="http://stackoverflow.com/q/3821472/126014">can't be used as a function</a>, so you'll have to introduce a little helper function: </p> <p> <pre><span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;a&nbsp;list&nbsp;-&gt;&nbsp;&#39;a&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">cons</span>&nbsp;x&nbsp;xs&nbsp;=&nbsp;x&nbsp;<span style="color:navy;">::</span>&nbsp;xs</pre> </p> <p> This enables you to partially apply a cons operation: </p> <p> <pre>&gt; cons 1;; val it : (int list -&gt; int list) = &lt;fun:it@4-5&gt;</pre> </p> <p> The expression <code>cons 1</code> is a <em>function</em> that awaits an <code>int list</code> argument. You can, for example, call it with the empty list, or another list: </p> <p> <pre>&gt; (cons 1) [];; val it : int list = [1] &gt; (cons 1) [2];; val it : int list = [1; 2]</pre> </p> <p> That hardly seems useful, but what happens if you start <em>composing</em> such partially applied functions? </p> <p> <pre>&gt; cons 1 &gt;&gt; cons 2;; val it : (int list -&gt; int list) = &lt;fun:it@7-8&gt;</pre> </p> <p> Notice that the result is another function with the same signature as <code>cons 1</code>. A way to read it is: <code>cons 1</code> is a function that takes a list as input, appends the list after <code>1</code>, and returns that new list. The return value of <code>cons 1</code> is passed to <code>cons 2</code>, which takes that input, appends that list after <code>2</code>, and returns that list. Got it? Try it out: </p> <p> <pre>&gt; (cons 1 &gt;&gt; cons 2) [];; val it : int list = [2; 1]</pre> </p> <p> Not what you expected? Try going through the data flow again. The input is the empty list (<code>[]</code>), which, when applied to <code>cons 1</code> produces <code>[1]</code>. That value is then passed to <code>cons 2</code>, which puts <code>2</code> at the head of <code>[1]</code>, yielding the final result of <code>[2; 1]</code>. </p> <p> This still doesn't seem to help, because it still reverses the list. True, but you can <em>reverse the composition:</em> </p> <p> <pre>&gt; (cons 2 &gt;&gt; cons 1) [];; val it : int list = [1; 2] &gt; (cons 1 &lt;&lt; cons 2) [];; val it : int list = [1; 2]</pre> </p> <p> Notice that in the first line, I reversed the composition by changing the order of partially applied functions. This, however, is equivalent to keeping the order, but using the reverse composition operator (<code>&lt;&lt;</code>). </p> <p> You can repeat this composition: </p> <p> <pre>&gt; (cons 1 &lt;&lt; cons 2 &lt;&lt; cons 3 &lt;&lt; cons 4) [];; val it : int list = [1; 2; 3; 4]</pre> </p> <p> That's exactly what you need, enabling you to write <code>acc &lt;&lt; (cons (f h))</code> in order to efficiently append a single element to the tail of a list! </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">mapTailRecursiveUsingDifferenceList</span>&nbsp;<span style="color:navy;">f</span>&nbsp;xs&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">cons</span>&nbsp;x&nbsp;xs&nbsp;=&nbsp;x&nbsp;<span style="color:navy;">::</span>&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:navy;">acc</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">acc</span>&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;h<span style="color:navy;">::</span>t&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;(<span style="color:navy;">acc</span>&nbsp;&lt;&lt;&nbsp;(<span style="color:navy;">cons</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;h)))&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">mapImp</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:navy;">id</span>&nbsp;xs</pre> </p> <p> This implementation of <em>map</em> uses this technique, introduced by <a href="https://en.wikipedia.org/wiki/John_Hughes_(computer_scientist)">John Hughes</a> in 1985. <a href="http://bit.ly/real-world-haskell">Real World Haskell</a> calls the technique a <em>difference list</em>, without explaining why. </p> <p> This mapImp function's accumulator is no longer a list, but a <em>function</em>. For every item, it composes a new accumulator function from the old one, effectively appending the mapped item to the tail of the accumulated list. Yet, because <code>acc</code> isn't a list, but rather a function, the 'append' operation doesn't trigger a list traversal. </p> <p> When the recursive function finally reaches the end of the list (the <code>[]</code> case), it invokes the <code>acc</code> function with the empty list (<code>[]</code>) as the initial input. </p> <p> This implementation is also tail recursive, because the accumulator is being completely composed (<code>acc &lt;&lt; (cons (f h))</code>) <em>before</em> mapImp is recursively invoked. </p> <p> Is it efficient, then? </p> <p> <pre>&gt; mapTailRecursiveUsingDifferenceList ((*) 2) [1..100000];; Real: 00:00:00.024, CPU: 00:00:00.031, GC gen0: 1, gen1: 0, gen2: 0 val it : int list = [2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; ...]</pre> </p> <p> 24 milliseconds is decent. It's not as good as mapTailRecursiveUsingRev (17 milliseconds), but it's close. </p> <p> In practice, you'll probably find that mapTailRecursiveUsingRev is not only more efficient, but also easier to understand. The advantage of using the difference list technique, however, is that now mapImp has a shape that almost screams to be refactored to a fold. </p> <h3 id="e4c2abcda9194f63944d4982a0672b70"> Example: implementing map with fold <a href="#e4c2abcda9194f63944d4982a0672b70" title="permalink">#</a> </h3> <p> The mapImp function in mapTailRecursiveUsingDifferenceList almost has the shape required by the accumulator function in <a href="https://msdn.microsoft.com/en-us/library/ee353894.aspx">List.fold</a>. This enables you to rewrite mapImp using fold: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">mapUsingFold</span>&nbsp;<span style="color:navy;">f</span>&nbsp;xs&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">cons</span>&nbsp;x&nbsp;xs&nbsp;=&nbsp;x&nbsp;<span style="color:navy;">::</span>&nbsp;xs &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">mapImp</span>&nbsp;=&nbsp;<span style="color:#4ec9b0;">List</span>.<span style="color:navy;">fold</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">acc</span>&nbsp;h&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">acc</span>&nbsp;&lt;&lt;&nbsp;(<span style="color:navy;">cons</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;h)))&nbsp;<span style="color:navy;">id</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">mapImp</span>&nbsp;xs&nbsp;[]</pre> </p> <p> As usual in Functional Programming, the ultimate goal seems to be to avoid writing recursive functions after all! </p> <p> The mapUsingFold function is as efficient as mapTailRecursiveUsingDifferenceList: </p> <p> <pre>&gt; mapUsingFold ((*) 2) [1..100000];; Real: 00:00:00.025, CPU: 00:00:00.031, GC gen0: 2, gen1: 1, gen2: 1 val it : int list = [2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; ...]</pre> </p> <p> Not only does 25 milliseconds seem fast, but it's also comparable with the performance of the built-in map function: </p> <p> <pre>&gt; List.map ((*) 2) [1..100000];; Real: 00:00:00.011, CPU: 00:00:00.015, GC gen0: 0, gen1: 0, gen2: 0 val it : int list = [2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; ...]</pre> </p> <p> Granted, List.map seems to be twice as fast, but it's also been the subject of more scrutiny than the above fun exercise. </p> <h3 id="1fad6f0b7cfd4e568fd0c3c30e1e1db1"> Summary <a href="#1fad6f0b7cfd4e568fd0c3c30e1e1db1" title="permalink">#</a> </h3> <p> In Functional Programming, recursive functions take the place of imperative loops. In order to be efficient, they must be tail recursive. </p> <p> You can make a function tail recursive by introducing an accumulator argument to an 'implementation function'. This also tends to put you in a position where you can ultimately refactor the implementation to use a fold instead of explicit recursion. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="eb1bb3250ca243119873d890f3952979"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#eb1bb3250ca243119873d890f3952979">#</a></div> <div class="comment-content"> <p> I verified the behavior you showed for <code>mapTailRecursiveUsingDifferenceList</code> and <code>mapUsingFold</code>: when executed in the F# Interactive window, both functions have the correct behavior. I further verified that both functions have the same correct behavior when executed as a unit test compiled in <code>RELEASE</code> mode. However, when executed as a unit test compiled in <code>DEBUG</code> mode, both tests fail (technically aborted when using XUnit) because each overflows the stack. In contrast, <code>List.map</code> has the correct behavior in all three situations. </p> <p> I haven't tested the same in Haskell. I don't even know if Haskell has complication modes like <code>DEBUG</code> and <code>RELEASE</code>. </p> <p> Do you know how to implement <code>map</code> for <code>list</code> in F# so that it does not stack overflow the stack even when executed in <code>DEBUG</code> mode? Of course your function <code>mapTailRecursiveUsingRev</code> achieves that goal. The problem that I actually want to solve is figure out how to implement <code>map</code> for the type <code>'a -&gt; 'b</code> as a (covariant) functor in <code>'b</code> in such a way that the stack does not overflow. See <a href="https://github.com/hedgehogqa/fsharp-hedgehog/issues/289">this issue</a> for more details. I am thinking that seeing such a <code>map</code> function implemented for <code>list</code> without using <code>List.rev</code> will help me figure out how to correctly implement this for the type <code>'a -&gt; 'b</code> as a (covariant) functor in <code>'b</code>. </p> </div> <div class="comment-date">2021-01-24 17:21 UTC</div> </div> <div class="comment" id="5fca9ec14f5742dcbccfb2b75778296d"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#5fca9ec14f5742dcbccfb2b75778296d">#</a></div> <div class="comment-content"> <blockquote> Ultimately, it turns out that List.rev is <a href="https://github.com/Microsoft/visualfsharp/blob/master/src/fsharp/FSharp.Core/local.fs">implemented in a fashion similar to above, using an 'implementation function' and an accumulator</a>. </blockquote> <p> Oh, that code partially answers my question. First, that is legacy code now. The current version is hosted in the <a href="https://github.com/dotnet/fsharp">dotnet/fsharp repository</a>. Then <a href="https://github.com/dotnet/fsharp/blob/d416b6b07b6b914232581c4913648d6656091a32/src/fsharp/FSharp.Core/list.fs#L75"><code>List.map</code></a> delegates to <a href="https://github.com/fsharp/fsharp/blob/d6244d4a1b7bc4efce42bc050ad08384cdce0320/src/fsharp/FSharp.Core/local.fs#L247-L254"><code>Microsoft.FSharp.Primitives.Basics.List.map</code></a>, which, by way of <a href="https://github.com/fsharp/fsharp/blob/d6244d4a1b7bc4efce42bc050ad08384cdce0320/src/fsharp/FSharp.Core/local.fs#L238-L245"><code>mapToFreshConsTail</code></a> calls the impure function <a href="https://github.com/fsharp/fsharp/blob/d6244d4a1b7bc4efce42bc050ad08384cdce0320/src/fsharp/FSharp.Core/local.fs#L93-L95"><code>setFreshConsTail</code></a> that is allowed to mutate the tail pointer of a cons cell. </p> <blockquote> <p> Not only does 25 milliseconds seem fast [for <code>mapUsingFold</code>], but it's also comparable with the performance of the built-in map function: </p> <p> [...] </p> <p> Granted, List.map seems to be twice as fast, but it's also been the subject of more scrutiny than the above fun exercise. </p> </blockquote> <p> <code>List.map</code> is "twice as fast" because it does "half as much work". <code>mapUsingFold</code> calls <code>List.fold</code>, which executes in linear time, to essentially compute the elements that will be in the final list. Then it calls <code>mapImp</code>, which also executes in linear time, to construct that final list. In contrast, <code>List.map</code> does both of those in one pass with its ability to mutate the tail pointer of a cons cell. </p> <p> I wonder if I can use mutability in a similarly pure way to achieve my goal. <i>**Goes away to think**</i> </p> </div> <div class="comment-date">2021-01-24 19:20 UTC</div> </div> <div class="comment" id="409ece5f355040a68ece3cd9af492cc5"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#409ece5f355040a68ece3cd9af492cc5">#</a></div> <div class="comment-content"> <p> To avoid overflowing the stack, it is a necessary condition that all recursive calls are tail calls. However, this is not sufficient. In <code>mapTailRecursiveUsingDifferenceList</code>, it is crucial (at least in F#) that the function composition <code>acc &lt;&lt; (cons (f h))</code> (which I prefer to write as <code>(cons (f h)) &gt;&gt; acc</code>) happened in that order. If John Hughes had designed the code differently so that the composition was <code>acc &gt;&gt; (cons (f h))</code> (which, as you demonstrated, would be the correct output if only this change were made except with the order reversed), then the stack would still overflow even though all recursive fucntions only involve tail calls. See <a href="https://github.com/hedgehogqa/fsharp-hedgehog/issues/289#issuecomment-766530121">this comment</a> for more information. </p> </div> <div class="comment-date">2021-01-25 04:43 UTC</div> </div> <div class="comment" id="e9289d008277475c8571af430949e69b"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#e9289d008277475c8571af430949e69b">#</a></div> <div class="comment-content"> <p> I figured out how to use the difference list technique to avoid overflowing the stack when composing on the right. See <a href="https://github.com/hedgehogqa/fsharp-hedgehog/issues/289#issuecomment-767299791">this comment</a>. I wonder if my implementation can be simplified. This problem of avoiding stack overflows when composing on the right has to be a standard problem with a well-known answer. </p> <p> Thanks for being a great place for me to share this partial stream of consciousness that I have had over the last couple days. </p> <p> P.S. You can simplify <code>mapTailRecursiveUsingDifferenceList</code> a bit. The local function <code>mapImp</code> is always given the same value of <code>f</code>. Therefore, this arguemnt can be removed (since the same <code>f</code> is also in scope). The resulting code does not overflow the stack (which, again, is a stronger statement than staying that all recursive calls in <code>mapImp</code> are tail calls). You essentially made this simplification when you implemented <code>mapUsingFold</code>. </p> </div> <div class="comment-date">2021-01-26 05:34 UTC</div> </div> <div class="comment" id="ce882ece457a42d0b770f2765a2e21ab"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ce882ece457a42d0b770f2765a2e21ab">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I have to admit that I've now lost track... Did you manage to unravel all you wanted to know by yourself, or is there still a question left? </p> </div> <div class="comment-date">2021-01-26 16:41 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Integration Testing composed functions https://blog.ploeh.dk/2015/12/21/integration-testing-composed-functions 2015-12-21T09:05:00+00:00 Mark Seemann <div id="post"> <p> <em>When you build a system from small functions that you subsequently compose, how do you know that the composition works? Integration testing is one option.</em> </p> <p> Despite its reputation as a niche language for scientific or finance computing, F# is a wonderful language for 'mainstream' software development. You can compose applications from small, <a href="/2015/05/07/functional-design-is-intrinsically-testable">inherently testable</a> functions. </p> <h3 id="14143134a58a4bbebcf5f202ee2556ca"> Composition <a href="#14143134a58a4bbebcf5f202ee2556ca" title="permalink">#</a> </h3> <p> Once you have your functions as building blocks, you compose them. This is best done in an application's <a href="/2011/07/28/CompositionRoot">Composition Root</a> - no different from Dependency Injection in Object-Oriented Programming. </p> <p> In my <a href="http://www.infoq.com/presentations/mock-fsharp-tdd">(free) Build Stuff talk on Functional TDD with F#</a> (expanded version <a href="https://blog.ploeh.dk/tdd-with-fsharp">available on Pluralsight</a>), I demonstrate how to compose such a function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">imp</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">Validate</span>.<span style="color:navy;">reservation</span> &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">bind</span>&nbsp;(<span style="color:#4ec9b0;">Capacity</span>.<span style="color:navy;">check</span>&nbsp;10&nbsp;(<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">getReservedSeats</span>&nbsp;connectionString)) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">map</span>&nbsp;(<span style="color:#4ec9b0;">SqlGateway</span>.<span style="color:navy;">saveReservation</span>&nbsp;connectionString)</pre> </p> <p> How can you be sure that this composition is correct? </p> <h3 id="df532918c3d74eab830a494361ccb5bb"> Integration Testing <a href="#df532918c3d74eab830a494361ccb5bb" title="permalink">#</a> </h3> <p> The answer to that question isn't different from its Object-Oriented counterpart. When you implement small building blocks, you can test them. Call these small building blocks <em>units</em>, and it should be clear that such tests are unit tests. It doesn't matter if units are (small) classes or functions. </p> <p> Unit testing ought to give you confidence that each unit behaves correctly, but unit tests don't tell you how they integrate. Are there issue that only surface when units interact? Are units correctly composed? </p> <p> In my experience, you can develop entire systems based exclusively on unit tests, and the final application can be stable and sturdy without the need for further testing. This depends on circumstances, though. In other cases, you need further testing to gain confidence that the application is correctly composed from its building blocks. </p> <p> You can use a small set of integration tests for that. </p> <h3 id="a453757ce95146c3b692fc3e1478827b"> Example <a href="#a453757ce95146c3b692fc3e1478827b" title="permalink">#</a> </h3> <p> In my <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a> Pluralsight course, I demonstrate how to apply the <a href="http://bit.ly/growingoos">GOOS</a> approach to an HTTP API built with <a href="http://www.asp.net/web-api">ASP.NET Web API</a>. One of the techniques I describe is <a href="http://stackoverflow.com/a/22294059/126014">how to integration test the API against its HTTP boundary</a>. </p> <p> In that course, you learn how to test an API implemented in C#, but since the tests are made against the HTTP boundary, the implementation language doesn't matter. Even so, you can also write the tests themselves in F#. Here's an example that exercises the Controller that uses the above <code>imp</code> function: </p> <p> <pre>[&lt;<span style="color:#4ec9b0;">Fact</span>;&nbsp;<span style="color:#4ec9b0;">UseDatabase</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Post&nbsp;returns&nbsp;success``</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">use</span>&nbsp;client&nbsp;=&nbsp;<span style="color:navy;">createClient</span>() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;json&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#4ec9b0;">ReservationJson</span>.<span style="color:#4ec9b0;">Root</span>(<span style="color:#a31515;">&quot;2014-10-21&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;x@example.com&quot;</span>,&nbsp;4) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;response&nbsp;=&nbsp;client.<span style="color:navy;">PostAsJsonAsync</span>(<span style="color:#a31515;">&quot;reservations&quot;</span>,&nbsp;json).Result &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">response</span><span style="background:yellow;">.</span><span style="background:yellow;">IsSuccessStatusCode</span><span style="background:yellow;">&nbsp;@&gt;</span></pre> </p> <p> This test creates a new <a href="https://msdn.microsoft.com/en-us/library/system.net.http.httpclient">HttpClient</a> object called <code>client</code>. It then creates a JSON document with some reservation data, and POSTs it to the <em>reservations</em> resource. Finally, it verifies that the response indicated a success. </p> <p> The ReservationJson type was created from a sample JSON document using the <a href="http://fsharp.github.io/FSharp.Data/library/JsonProvider.html">JSON Type Provider</a>. The createClient function is a bit more involved, but follows the same recipe I describe in my course: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:navy;">createClient</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;baseAddress&nbsp;=&nbsp;<span style="color:#4ec9b0;">Uri</span>&nbsp;<span style="color:#a31515;">&quot;http://localhost:8765&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;config&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#4ec9b0;">HttpSelfHostConfiguration</span>(baseAddress) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">configure</span>&nbsp;connStr&nbsp;config &nbsp;&nbsp;&nbsp;&nbsp;config.IncludeErrorDetailPolicy&nbsp;&lt;-&nbsp;<span style="color:#4ec9b0;">IncludeErrorDetailPolicy</span>.Always &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;server&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#4ec9b0;">HttpSelfHostServer</span>(config) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;client&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#4ec9b0;">HttpClient</span>(server) &nbsp;&nbsp;&nbsp;&nbsp;client.BaseAddress&nbsp;&lt;-&nbsp;baseAddress &nbsp;&nbsp;&nbsp;&nbsp;client</pre> </p> <p> The (impure) <code>configure</code> function is a function defined by the application implementation. Among many other things, it creates the above <code>imp</code> composition. When the test passes, you can trust that <code>imp</code> is correctly composed. </p> <h3 id="883f6dec670a427b910d12a31ce147b4"> Smoke Testing <a href="#883f6dec670a427b910d12a31ce147b4" title="permalink">#</a> </h3> <p> You may already have noticed that the ``Post returns success`` test is course-grained and vague. It doesn't attempt to make strong assertions about the posterior state of the system; if the response indicates success, the test passes. </p> <p> The reason for this is that all important behaviour is already covered by unit tests. <ul> <li>Is the response specifically a 201 (Created) response? Covered by unit tests.</li> <li>Does the response have a Location header indicating the address of a newly created resource? Covered by unit test.</li> <li>What happens if the input is malformed? Covered by unit tests.</li> <li>What happens if the system can't accept the request due to business rules? Covered by unit tests.</li> <li>...and so on.</li> </ul> The purpose of the integration test isn't to instil confidence in the implementation; the purpose is to give you confidence that the application is correctly integrated (hence the name). In my experience, a few <a href="https://en.wikipedia.org/wiki/Smoke_testing_(software)">Smoke Tests</a> are enough to achieve that goal. </p> <p> Specifically, it has been my experience that the most common integration issues are related to various configuration errors: <ul> <li>Missing configuration values</li> <li>Wrong configuration values</li> <li>Network errors</li> <li>Security errors</li> <li>... etc.</li> </ul> Common to most of such errors is that they tend to manifest themselves uniformly. For example, in a database-driven application, if the database server is unreachable, <em>nothing</em> works. Therefore, only a single Smoke Test is required in order to tell you whether the application can reach the server or not. </p> <p> In other words: you should have a legion of unit tests covering specific <em>behaviour</em>, and a few integration tests covering common integration issues. You may already have recognised this principle as <a href="http://martinfowler.com/bliki/TestPyramid.html">the Test Pyramid</a>. </p> <h3 id="ed36041fffa648c288c5da6c0db78cbf"> Summary <a href="#ed36041fffa648c288c5da6c0db78cbf" title="permalink">#</a> </h3> <p> In this article, you saw an example of an integration test against an HTTP API, written in F#. The principle is universal, though. You can compose applications from units. These units can be functions written in F#, or Haskell, or Scala, or they can be classes written in C#, Java, Python, and so on. Composition can be done on functions, or on objects using Dependency Injection. </p> <p> In all of these cases, you can cover the units with unit tests. Issues with composition can be screened by a few smoke tests at the integration level. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Rules of Attraction: Location https://blog.ploeh.dk/2015/12/04/the-rules-of-attraction-location 2015-12-04T08:57:00+00:00 Mark Seemann <div id="post"> <p> <em>Widen your hiring pool to the entire world; you won't believe what happens next.</em> </p> <p> Every week in my Twitter stream, there's some enticing offer: <blockquote> NoWare is looking for strong F# or Haskell developers with experience in TDD, property-based testing, object-oriented design, REST, and software architecture. </blockquote> For half a second I go: "Oh, man, that's <em>so much</em> a description of me! This sounds terrific!" But then, with small letters, it says: "Must be willing to relocate to <a href="https://en.wikipedia.org/wiki/Sauda">Sauda</a>." </p> <p> Oh. </p> <p> If your company is based in London, New York, Silicon Valley, Seattle, and perhaps a few other places, I can even understand that attitude. The local 'talent pool' is already big in those places, and more people are willing to move there for work. </p> <p> In most other areas, I find that attitude towards hiring programmers lazy and self-centred. </p> <p> As an example, consider Copenhagen, where I live. The <a href="https://en.wikipedia.org/wiki/%C3%98resund_Region">greater metropolitan area is home to some 3.5 million people</a>, so you'd think it was possible to find programmers to hire. Yet, every employer here whines that it's too hard to find developers. </p> <p> While Copenhagen is nice enough, I can understand if the majority of the world's programmers aren't itching to move here. Apparently, this place is only <em>attractive</em> if you live in Afghanistan, Somalia, Nigeria, Syria, or some other horrible place. </p> <h3 id="8def12ec53ea45ce81eab87db25cc185"> All digital <a href="#8def12ec53ea45ce81eab87db25cc185" title="permalink">#</a> </h3> <p> The outcome of programming is software, a 100% immaterial, digital product. Source code, as well, is entirely digital. Documentation, if it exists, tends to be digital. Source control systems are networked. Developers shouldn't have physical access to production servers. </p> <p> Why do programmers need to be physically present? </p> <h3 id="4387baeac6434fd298c1a2acc95711d4"> Tacit knowledge <a href="#4387baeac6434fd298c1a2acc95711d4" title="permalink">#</a> </h3> <p> I think I know why programmers need to be physically present in many organisations. It's because all the knowledge of 'how things are done' is in the heads of people. <ul> <li><em>How do I configure my development machine?</em> Ask Joe; he can tell you.</li> <li><em>What's the password to the CI server?</em> Call Kathy in the satellite office. Maybe she knows.</li> <li><em>How do I find the logs related to this bug I was asked to fix?</em> Go and talk to Jane. She can show you.</li> <li><em>Why did we build our own ORM, instead of using Entity Framework?</em> Uhm, yeah, there was this lead developer, Rick... He's no longer with the company...</li> <li><em>Would anyone mind if I add an extra argument to the BloobedlyBloo method?</em> Sure, go right ahead!</li> <li><em>How are we going to address this new feature request?</em> I don't know. Let's have a meeting!</li> <li><em>What's the status of the Foo component?</em> I don't know. Let's have a meeting!</li> <li><em>Is there any way to prevent the back-office developers from accessing our database directly?</em> Probably not, but let's have a meeting!</li> </ul> I've seen plenty of organisations that work that way. Most of the communication that happens tends to be ephemeral and unorganised. If you're a remote worker, you don't have a chance. </p> <p> Perhaps organisations that work that way are effective, but it doesn't scale. Every time a new team member or stakeholder joins the project, someone needs to take time bringing him or her up to speed. The more people in the organisation, the worse it becomes. </p> <p> At one time I worked in an organisation that operated like that. I'd have meeting upon meeting about particular topics. The next week, I'd have <em>the same meetings again</em>, because a new stakeholder wanted to get involved. Nothing new happened; nothing new was decided, but it was necessary, because nothing was written down. Lots of time was wasted. </p> <p> That's when I started writing documentation and meeting minutes. Not that I enjoyed doing that, but it saved me time. The next time someone came and asked me to sit in on a meeting, I'd forward the minutes and documentation and ask people to read that instead. </p> <h3 id="1af4634e2c3e4d7fbc3c6d478701720b"> Conway's law <a href="#1af4634e2c3e4d7fbc3c6d478701720b" title="permalink">#</a> </h3> <p> Imagine that you could hire programmers from all over the world. Not only within driving distance of your offices, but from the entire world. Just think of the supply of developers you'd be able to choose from. You can literally select from among the world's best programmers, if you're willing to pay the price. Some companies, like <a href="http://stackoverflow.com">Stack Overflow</a>, <a href="https://github.com">GitHub</a>, <a href="https://basecamp.com">Basecamp</a>, and <a href="http://basho.com">Basho</a>, already do this, with great success. </p> <p> It's only going to work, though, if your organisation is set up for it. Processes must be written down. Decisions must be documented. Work must be asynchronous, in order to accommodate people in different time zones. You can't easily have meetings, so you'll need to discuss strategy, architecture, design, etc. using collaborative online tools. </p> <p> If your organisation doesn't already work like that, you'll have to transform it. Software development with distributed teams can work, but only if you enable it. The good news is that there's plenty of publicly available examples on how to do this: just find your favourite open source project, and study how it's organised. Most open source is written by programmers from all over the world, at various times, and yet it still works. </p> <p> As a modest example, my own experience with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> has taught me a thing or two about running a distributed project. Contributors have submitted code from Australia, South Korea, USA, Greece, England, Denmark, Sweden, Russia, and lots of other countries. Discussions happen in the open, in <a href="https://github.com/AutoFixture/AutoFixture/issues">GitHub issues</a>. </p> <p> You'll most likely find that if you can get distributed development to work, your software architecture, too, will become more and more distributed and asynchronous in nature. This is <a href="https://en.wikipedia.org/wiki/Conway%27s_law">Conway's law</a>: <em>organizations which design systems ... are constrained to produce designs which are copies of the communication structures of these organizations</em>. </p> <p> If your organisation is based on ad hoc ephemeral communication and impromptu meetings, your software will have no clear communication structures either. There's <a href="https://en.wikipedia.org/wiki/Spaghetti_code">a word</a> for that kind of software architecture. </p> <p> If, on the other hand, you set yourself up for distributed development teams, you'll be forced to make the communication channels clearer - both in your organisation, and in your software. This will enable you to scale your development organisation. </p> <h3 id="868f373612684fb6acc05d32b6591c45"> Summary <a href="#868f373612684fb6acc05d32b6591c45" title="permalink">#</a> </h3> <p> You can <em>claim</em> that you're a 'world-class' organisation that hires only the best of the best (within a radius of 100 km). </p> <p> Or you can <em>be</em> a world-class organisation by literally having developers working all over the world. You still need to be able to find the best, though. Perhaps you should try to <a href="/2015/12/03/the-rules-of-attraction-language">let them find you instead</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Rules of Attraction: Language https://blog.ploeh.dk/2015/12/03/the-rules-of-attraction-language 2015-12-03T15:05:00+00:00 Mark Seemann <div id="post"> <p> <em>How to attract the best developers to your organisation, with one weird trick.</em> </p> <p> In a <a href="http://broadcast.oreilly.com/2009/03/an-interview-with-anders-hejls.html">2009 interview</a>, <a href="https://en.wikipedia.org/wiki/Anders_Hejlsberg">Anders Hejlsberg</a>, the inventor of C#, Turbo Pascal, TypeScript, etc. said: <blockquote> "Well, you know, platforms live maybe 10, 15 years and then they cave in under their own weight, one way or the other." </blockquote> C# is now 15 years old; Java: 20 years. </p> <p> You don't have to believe that Anders Hejlsberg is right, though. After all, COBOL is still in use, 56 years after its invention. One of the world's most popular languages, C++, is 32 years old, and C is 43 years old. Still, it's food for thought. </p> <p> When I consult and advise, I often encounter organisations that standardise on C# or Java. When I try to tell CTOs and development leads about the benefits of adopting 'new' languages like F# (10 years), Haskell (25 years), Clojure (8 years), Erlang (29 years), or Scala (11 years), the response is always the same: <blockquote> "How will I find developers?" </blockquote> That's the easiest thing in the world! </p> <p> In the early 2000s, Java was already getting close to 10 years old, and some programmers were beginning to look for the next cool technology. They found it in Python, and for a while, Python was perceived as the cutting edge. </p> <p> In the late 2000s, C# 'alpha developers' migrated to Ruby en masse. It became so bad that <em>I'm-leaving-.NET</em> blog posts became a cliché. </p> <p> In the early 2010s, the main attraction has been Node.js, and JavaScript in general. </p> <p> Let's not forget those of us who have fallen in love with F#, Clojure, Haskell, Elixir, etc. </p> <p> The most curious developers eventually get tired of using the same programming language year in and year out. Those first-movers that migrated to Python 10 years ago are already on to the next language. The same goes for the Rubyists. </p> <p> Finding F#, Clojure, Elixir, etc. developers is the easiest thing in the world. The most important thing you can do as an organisation is to say: <blockquote> "We wish to hire F# developers!" </blockquote>, or Clojure developers, Haskell developers, etc. </p> <p> You don't have to <em>find</em> such developers; make them find you. </p> <p> Although there are few of these developers out there, they are easy to attract. This is called <a href="http://paulgraham.com/pypar.html">the Python Paradox</a>, after the early 2000 Python migration. </p> <p> Not only is it easy to attract developers for such 'new' languages, you also get the most progressive, curious, motivated, enthusiastic programmers. That's the 'talent' all companies seem to be pining for these days. </p> <p> Some programmers will even accept a decrease in income, only for the chance to get to work with a technology they love. </p> <p> You'll probably also get some difficult-to-work-with primadonnas who are gone again after three years... <a href="https://en.wikipedia.org/wiki/There_ain%27t_no_such_thing_as_a_free_lunch">TANSTAAFL</a>. </p> <p> The crux of the matter is that the argument that you can't find developers for a particular cool language doesn't hold. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Recurse https://blog.ploeh.dk/2015/12/01/recurse 2015-12-01T09:12:00+00:00 Mark Seemann <div id="post"> <p> <em>How to refactor from a loop using mutable state to a recursive implementation.</em> </p> <p> One of the most compelling reasons to adopt Functional Programming (FP) is the emphasis on immutable values. All the dread and angst associated with state that can implicitly change while you're not looking, is gone. </p> <p> One of the most frustrating aspects of FP for people coming from other paradigms is the emphasis on immutable values. You know that you ought to be able to implement a given algorithm using immutable values, but given your background in Object-Oriented Programming, you can't quite figure out how to do it. </p> <p> In FP, loops are implemented with recursion, and mutable values are replaced with accumulator arguments. This article describes how to refactor from a procedural, mutable loop implementation to a pure, tail-recursive implementation. </p> <h3 id="704b31021861470cb51e90c8c06d0748"> Motivation <a href="#704b31021861470cb51e90c8c06d0748" title="permalink">#</a> </h3> <p> You want your loop implementation to be 'Functional' instead of procedural. There can be many reasons for this. Perhaps you want to learn FP. Perhaps you want to eliminate mutable state in order to make your implementation thread-safe. Perhaps you think that getting rid of mutation will make the code more readable and maintainable. Perhaps you want to port your implementation to a language that doesn't support mutability at all (like Haskell). </p> <h3 id="5db38669ebe44b5da79a45f6f68af356"> Mechanics <a href="#5db38669ebe44b5da79a45f6f68af356" title="permalink">#</a> </h3> <p> Start with a procedural implementation, using a mutable loop variable. This obviously only works in multi-paradigmatic languages where mutable variables are possible, even if they're not ideal. Examples include F#, Scala, and Clojure, but not Haskell, which isn't multi-paradigmatic. <ol> <li>Instead of your imperative loop, introduce a recursive function.</li> <li>Replace each mutable loop variable with an argument for the recursive function.</li> </ol> Ultimately, your refactored implementation should be tail-recursive, but you can always address that concern once you've refactored to recursion. </p> <h3 id="e152b14ff2e04149b2842c1482db1470"> Example: backspace characters <a href="#e152b14ff2e04149b2842c1482db1470" title="permalink">#</a> </h3> <p> Imagine that you receive a stream of characters from someone typing on a keyboard. Sometimes, the typist mistypes and uses the backspace key, which sends the character '\b'. Whenever you encounter '\b', you should remove the preceding character, as well as the backspace character itself. This example is based on <a href="http://stackoverflow.com/q/32801109/126014">this Stack Overflow question</a>. </p> <p> The original F# implementation is procedural, using a for loop and a single mutable variable: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;System <span style="color:blue;">open</span>&nbsp;System.Collections.Generic <span style="color:blue;">let</span>&nbsp;handleBackspaces&nbsp;textToProcess&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stack&nbsp;=&nbsp;<span style="color:#2b91af;">Stack</span>&lt;<span style="color:#2b91af;">char</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;c&nbsp;<span style="color:blue;">in</span>&nbsp;textToProcess&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;c&nbsp;=&nbsp;<span style="color:#a31515;">&#39;\b&#39;</span>&nbsp;<span style="color:blue;">then</span>&nbsp;stack.Pop()&nbsp;|&gt;&nbsp;ignore &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;stack.Push&nbsp;c &nbsp;&nbsp;&nbsp;&nbsp;stack&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.rev&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toArray&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">String</span></pre> </p> <p> While this implementation doesn't explicitly use the <code>mutable</code> keyword, the <code>stack</code> variable is mutable because Stack&lt;T&gt; is mutable. Since <code>textToProcess</code> is a string, and string implements IEnumerable&lt;char&gt;, you can loop over each char value, pushing the value on the stack unless it's a backspace character; in that case, the previous value is instead popped and thrown away. </p> <p> According to the rules of the <em>Recurse</em> refactoring, you should introduce a recursive function instead of the loop, and add an argument that will replace the stack. To make it easy, call the recursive function <code>imp</code>, and the function argument <code>acc</code>. The name <code>acc</code> is popular; it's short for <em>accumulator</em>. This argument is used to accumulate the final value, just like <code>stack</code> in the above example. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;handleBackspaces&#39;&nbsp;textToProcess&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;imp&nbsp;acc&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;acc &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;\b&#39;</span>::cs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;imp&nbsp;(acc&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.tail)&nbsp;cs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;c::cs&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;imp&nbsp;(c::acc)&nbsp;cs &nbsp;&nbsp;&nbsp;&nbsp;textToProcess&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList&nbsp;|&gt;&nbsp;imp&nbsp;[]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.toArray&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">String</span></pre> </p> <p> The <code>imp</code> function is declared with the <code>rec</code> keyword to mark it as a recursive function. It has the type <code>char list -&gt; char list -&gt; char list</code>. <code>acc</code> is a <code>char list</code>, which is also the case for the second argument implied by the <code>function</code> keyword. The function returns the accumulator if the input list is empty; otherwise, it matches on the head of the list. If the head is explicitly the backspace character, the <code>imp</code> function calls itself recursively, but replaces <code>acc</code> with the tail of <code>acc</code>; it uses List.tail to get the tail. This effectively removes the most recent character from the accumulator. In all other cases, the function also calls itself by consing <code>c</code> on <code>acc</code>. </p> <p> The <code>imp</code> function is tail-recursive, since all other values are already computed when a recursive call to <code>imp</code> takes place. </p> <p> You can <a href="http://stackoverflow.com/a/32801993/126014">further refactor this implementation to use a fold</a> instead of a recursive function. </p> <h3 id="4c3a68be5e054d1b9e51f50814ea93b9"> Example: Graham Scan <a href="#4c3a68be5e054d1b9e51f50814ea93b9" title="permalink">#</a> </h3> <p> This second example is a more complex example that further illustrates how to apply the <em>Recurse</em> refactoring. If you already feel that you understand how to apply this refactoring, you can skip reading the following example. </p> <p> Some time ago, I was attempting to implement the <a href="https://en.wikipedia.org/wiki/Graham_scan">Graham Scan</a> algorithm to find the <a href="https://en.wikipedia.org/wiki/Convex_hull">convex hull</a> for a set of points. As I've <a href="/2015/10/19/visual-value-verification">described before</a>, this turned out to be quite difficult for me. One of my problems was that I was trying to implement the algorithm in Haskell, and while I understood the algorithm, I couldn't figure out how to implement it in a functional way - and when you can't do it the functional way, you can't do it at all in Haskell. </p> <p> In F#, on the other hand, you can implement algorithms using procedural code if you must, so I decided to implement the algorithm in F# first, using a procedural approach, and then subsequently figure out how to refactor to immutable values. Once I had a pure F# implementation, I could always back-port it to Haskell. </p> <p> This is one of the reasons I think F# is a better language for learning FP if you come from an Object-Oriented background: you can gradually refactor towards more Functional implementations as you become better at FP. </p> <p> The part that caused me particular difficulty was the <em>scan</em> part, where the algorithm examines all points to identify which points to discard from the hull (because they're in the interior of the hull, and not <em>on</em> the hull). </p> <p> After sorting all candidates according to special rules, the algorithm must consider each point in turn. If that new point is 'to the right' of the previous two points, the <em>previous</em> point is in the interior of the hull and should be discarded. The previous-to-previous point could also be 'to the right' of the new point, so the algorithm needs to check again, and so on. </p> <p> My imperative solution looked like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">inline</span>&nbsp;hullPoints&nbsp;points&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;ps&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;p&nbsp;<span style="color:blue;">in</span>&nbsp;points&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ps&nbsp;&lt;-&nbsp;ps&nbsp;@&nbsp;[p] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;shouldCheck&nbsp;=&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">while</span>&nbsp;shouldCheck&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;wasDiscarded,&nbsp;newPoints&nbsp;=&nbsp;check&nbsp;ps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shouldCheck&nbsp;&lt;-&nbsp;wasDiscarded &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;wasDiscarded&nbsp;<span style="color:blue;">then</span>&nbsp;ps&nbsp;&lt;-&nbsp;newPoints &nbsp;&nbsp;&nbsp;&nbsp;ps</pre> </p> <p> (You can see <a href="https://github.com/ploeh/Hull">the full code base on GitHub</a>. The start is at <a href="https://github.com/ploeh/Hull/tree/5290abd3c31c162ee6c4b21b82494ce97ecf7fa5">5290abd3c31c162ee6c4b21b82494ce97ecf7fa5</a>, and the end state that this post describes is at <a href="https://github.com/ploeh/Hull/tree/e3efd1b457a46112cff6f06b8cbb100d153f0ef1">e3efd1b457a46112cff6f06b8cbb100d153f0ef1</a>.) </p> <p> Due to the <code>inline</code> keyword, the hullPoints function has a complex type, but for practical purposes, think of it as having the type <code>(int * int) seq -&gt; (int * int) list</code>. The <code>points</code> argument is a sequence of coordinates: <code>(int * int) seq</code>. </p> <p> As you can see, this implementation has a nested loop. The outer loop traverses all <code>points</code> and appends the point in consideration to the mutable list variable <code>ps</code>. At this stage, <code>p</code> is only a <em>candidate</em>. What the algorithm must now determine is whether <code>p</code> is in the interior or might be a hull point. </p> <p> In order to do that, it calls another function called <code>check</code>. The check function is another inline function, but you can think about it as having the type <code>(int * int) list -&gt; bool * (int * int) list</code>. The return type is peculiar, but the idea is that it returns true in the first tuple element if points were discarded from the input, and false if no points were discarded. The second tuple element contains the points (that may or may not have had points removed compared to the input points). (I later refactored this function to a function called tryDiscard with the type <code>(int * int) list -&gt; (int * int) list option</code>.) </p> <p> If points were discarded, the algorithm must check again, as there may be more points to discard. Only when no more points were discarded can the outer loop move on to the next candidate. </p> <p> According to the <em>Recurse</em> refactoring, you need to define a recursive function for each loop. There are two loops here, but do the inner loop first. Each mutable variable should be replaced with a function argument, but fortunately there's only one: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">inline</span>&nbsp;hullPoints&nbsp;points&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;update&nbsp;candidates&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;wasDiscarded,&nbsp;newCandidates&nbsp;=&nbsp;check&nbsp;candidates &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;wasDiscarded &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;update&nbsp;newCandidates &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;candidates &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;candidates&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;p&nbsp;<span style="color:blue;">in</span>&nbsp;points&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidates&nbsp;&lt;-&nbsp;candidates&nbsp;@&nbsp;[p] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidates&nbsp;&lt;-&nbsp;update&nbsp;candidates &nbsp;&nbsp;&nbsp;&nbsp;candidates</pre> </p> <p> The new <code>update</code> function calls the <code>check</code> function. If <code>wasDiscarded</code> is true, it calls itself recursively with the new candidates; otherwise, it returns the input candidates. </p> <p> The update function is now a recursive function without mutable variables, but the containing hullPoints function still has a mutable <code>candidates</code> variable. You'll need to apply the <em>Recursive</em> refactoring again: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;hullPoints&nbsp;points&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;update&nbsp;candidates&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;wasDiscarded,&nbsp;newCandidates&nbsp;=&nbsp;check&nbsp;candidates &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;wasDiscarded &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;update&nbsp;newCandidates &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;candidates &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;hpImp&nbsp;candidates&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;candidates &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;p&nbsp;::&nbsp;tail&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;cs&nbsp;=&nbsp;candidates&nbsp;@&nbsp;[p] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;updatedCandidates&nbsp;=&nbsp;update&nbsp;cs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hpImp&nbsp;updatedCandidates&nbsp;tail &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;hpImp&nbsp;[]&nbsp;points</pre> </p> <p> The hpImp function replaces the remaining loop, and <code>candidates</code> is now a function argument instead of mutable variable. </p> <p> As long as <code>candidates</code> has contents, the head of the list <code>p</code> is appended to <code>candidates</code>, and <code>update</code> is invoked. Subsequently, hpImp is invoked recursively with the updated candidates and the tail of the list. </p> <p> The hullPoints function returns the value of calling hpImp with empty hull candidates, and the <code>points</code> argument. This implementation has no mutable variables. </p> <p> You can refactor this implementation to make it more readable, but that's not the point of this article. You can see what I then did in <a href="https://github.com/ploeh/Hull">the GitHub repository</a>. </p> <p> (Now that I had a pure implementation, I could also port it to Haskell, which met my original goal.) </p> <h3 id="454a98ae8ae846809ac39660bf5f67d6"> Summary <a href="#454a98ae8ae846809ac39660bf5f67d6" title="permalink">#</a> </h3> <p> For programmers used to imperative programming, it can be difficult to apply pure Functional techniques. When you have loops that update a mutable variable in each step of the loop, you can refactor to use a recursive function; the mutable variable is replaced with a function argument. When you've tried it a couple of times, you'll get the hang of it. </p> <p> Once you have a recursive function with an accumulator argument, you can often further refactor it to use a fold, instead of a recursive function. </p> <p> (This post is the December 1st entry in the <a href="https://sergeytihon.wordpress.com/2015/10/25/f-advent-calendar-in-english-2015">2015 F# Advent Calendar</a>.) </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. To log or not to log https://blog.ploeh.dk/2015/11/30/to-log-or-not-to-log 2015-11-30T08:45:00+00:00 Mark Seemann <div id="post"> <p> <em>There's no reason to make logging any harder than it has to be. Here's a compositional approach in F#.</em> </p> <p> Logging seems to be one of those cross-cutting concerns on which people tend to spend a lot of effort. For programmers coming from an object-oriented language like C#, finding a sane approach to logging seems to be particularly difficult. </p> <p> In <a href="http://amzn.to/12p90MG">my book about Dependency Injection</a>, I made an effort to explain that logging and other cross-cutting concerns are best addressed by <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a> (or dynamic interception). You can use the same design with F# functions. </p> <h3 id="2c16cb510eab45b8b58d0c3144ccee25"> Example scenario <a href="#2c16cb510eab45b8b58d0c3144ccee25" title="permalink">#</a> </h3> <p> Consider, as an example, an HTTP API for a restaurant booking system. This example is taken from my <a href="http://bit.ly/1Hhru7l">Test-Driven Development with F#</a> Pluralsight course, but you can see an almost identical example for free in <a href="http://www.infoq.com/presentations/mock-fsharp-tdd">this recording of a BuildStuff talk</a>. </p> <p> The application is composed of small, mostly pure functions: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;imp&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Validate</span>.reservationValid &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.bind&nbsp;(<span style="color:#2b91af;">Capacity</span>.check&nbsp;10&nbsp;<span style="color:#2b91af;">SqlGateway</span>.getReservedSeats) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.map&nbsp;<span style="color:#2b91af;">SqlGateway</span>.saveReservation</pre> </p> <p> Exactly what each function does isn't important in this context, but here are the types involved: <table> <thead> <tr> <th>Function</th> <th>Type</th> </tr> </thead> <tbody> <tr> <td>imp</td> <td><code>ReservationRendition -&gt; Rop.Result&lt;unit, Error&gt;</code></td> </tr> <tr> <td>Validate.reservationValid</td> <td><code>ReservationRendition -&gt; Rop.Result&lt;Reservation, Error&gt;</code></td> </tr> <tr> <td>Capacity.check</td> <td><code>int -&gt; (DateTimeOffset -&gt; int) -&gt; Reservation -&gt; Rop.Result&lt;Reservation, Error&gt;</code></td> </tr> <tr> <td>SqlGateway.saveReservation</td> <td><code>Reservation -&gt; unit</code></td> </tr> </tbody> </table> where Rop.Result&lt;'success, 'error&gt;, Rop.bind, and Rop.map are as described in <a href="http://fsharpforfunandprofit.com/rop">Scott Wlaschin's Railway Oriented Programming</a>. </p> <p> In short, the <code>imp</code> function validates the input, applies some business rules if the input was valid, and saves the reservation to a database if the business rules allow it. </p> <p> I strongly believe that in any well-designed code base, the core implementation should be independent of cross-cutting concerns such as logging. If the above reservation system is well-designed, it should be possible to retrofit logging onto it without changing the existing functions. Indeed, that turns out to be possible. </p> <h3 id="562d52cfab4f4eea9fe8fe11f80331c2"> Adding logs <a href="#562d52cfab4f4eea9fe8fe11f80331c2" title="permalink">#</a> </h3> <p> You should use an existing logging library such as <a href="http://serilog.net">Serilog</a>, <a href="https://logging.apache.org/log4net">log4net</a>, <a href="http://nlog-project.org">NLog</a>, etc. instead of rolling your own. In this example, imagine that you're using the well-known <em>SomeExternalLoggingLibrary</em>. In order to protect yourself against changes etc. in the external library, you first define your own, application-specific logging module: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:#2b91af;">BookingLog</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;logError&nbsp;=&nbsp;<span style="color:#2b91af;">SomeExternalLoggingLibrary</span>.logError &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;logInformation&nbsp;=&nbsp;<span style="color:#2b91af;">SomeExternalLoggingLibrary</span>.logInformation</pre> </p> <p> Both functions have the type <code>fileName:string -&gt; msg:string -&gt; unit</code>. </p> <p> As a beginning, you can start by logging the final result of executing the <code>imp</code> function. Since it has the type <code>ReservationRendition -&gt; Rop.Result&lt;unit, Error&gt;</code>, if you implement a log function that both accepts and returns <code>Rop.Result&lt;unit, Error&gt;</code>, you can append that to the composition of <code>imp</code>. Start with the logging function itself: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:#2b91af;">BookingLog</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;logReservationsPost&nbsp;logFile&nbsp;result&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;result&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Failure(ValidationError&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;logError&nbsp;logFile&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Failure&nbsp;CapacityExceeded&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;logError&nbsp;logFile&nbsp;<span style="color:#a31515;">&quot;Capacity&nbsp;exceeded.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Success&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;logInformation&nbsp;logFile&nbsp;<span style="color:#a31515;">&quot;Reservation&nbsp;saved.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result</pre> </p> <p> This function has the type <code>string -&gt; Rop.Result&lt;unit, Error&gt; -&gt; Rop.Result&lt;unit, Error&gt;</code>. It matches on the cases of <code>result</code> and logs something relevant for each case; then it returns <code>result</code> without modifying it. </p> <p> Since the logReservationsPost function both accepts and returns the same type, you can easily append it to the other functions while composing <code>imp</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;imp&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Validate</span>.reservationValid &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.bind&nbsp;(<span style="color:#2b91af;">Capacity</span>.check&nbsp;10&nbsp;<span style="color:#2b91af;">SqlGateway</span>.getReservedSeats) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.map&nbsp;<span style="color:#2b91af;">SqlGateway</span>.saveReservation &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">BookingLog</span>.logReservationsPost&nbsp;logFile</pre> </p> <p> Notice how BookingLog.logReservationsPost is simply added as the last line of composition. This compiles because that function returns its input. </p> <p> Running the application with various input demonstrates that logging works as intended: </p> <p> <pre>Information: Reservation saved. Error: Invalid date. Information: Reservation saved. Error: Capacity exceeded.</pre> </p> <p> You've seen that you can append high-level logging of the final value, but can you also add logging deeper in the guts of the implementation? </p> <h3 id="e4df3a42eab64f978566cb8bee78857a"> Logging business behaviour <a href="#e4df3a42eab64f978566cb8bee78857a" title="permalink">#</a> </h3> <p> Imagine that you need to also log what happens before and after Capacity.check is called. One option is to add a logging function with the same type as Capacity.check, that also Decorates Capacity.check, but I think it's simpler to add two functions that log the values before and after Capacity.check. </p> <p> The type of Capacity.check is <code>int -&gt; (DateTimeOffset -&gt; int) -&gt; Reservation -&gt; Rop.Result&lt;Reservation, Error&gt;</code>, but after partial application, it's only <code>Reservation -&gt; Rop.Result&lt;Reservation, Error&gt;</code>. In order to log what happens before Capacity.check is called, you can add a function that both accepts and returns a Reservation: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;logBeforeCapacityCheck&nbsp;logFile&nbsp;reservation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;logInformation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logFile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(sprintf &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Checking&nbsp;capacity&nbsp;for&nbsp;%s...&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(reservation.Date.ToString&nbsp;<span style="color:#a31515;">&quot;d&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;reservation</pre> </p> <p> This function has the type <code>string -&gt; Reservation -&gt; Reservation</code>, and is placed within the BookingLog module. The logInformation function is used to log the input, which is then returned. </p> <p> Likewise, you can also log what happens after Capacity.check is invoked. Since Capacity.check returns Rop.Result&lt;Reservation, Error&gt;, your log file must take that type as both input and output: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;logAfterCapacityCheck&nbsp;logFile&nbsp;result&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;result&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Failure(ValidationError&nbsp;msg)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;logError&nbsp;logFile&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Failure&nbsp;CapacityExceeded&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logError&nbsp;logFile&nbsp;(sprintf&nbsp;<span style="color:#a31515;">&quot;Capacity&nbsp;exceeded.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Success&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logInformation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logFile &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(sprintf&nbsp;<span style="color:#a31515;">&quot;All&nbsp;is&nbsp;good&nbsp;for&nbsp;%s.&quot;</span>&nbsp;(r.Date.ToString&nbsp;<span style="color:#a31515;">&quot;d&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;result</pre> </p> <p> The logAfterCapacityCheck function has the type <code>string -&gt; Rop.Result&lt;Reservation, Error&gt; -&gt; Rop.Result&lt;Reservation, Error&gt;</code>, and is also placed within the BookingLog module. Like the logReservationsPost function, it matches on <code>result</code> and logs accordingly; then it returns <code>result</code>. Do you see a pattern? </p> <p> Because of these types, you can compose them into <code>imp</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;imp&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Validate</span>.reservationValid &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.map&nbsp;(<span style="color:#2b91af;">BookingLog</span>.logBeforeCapacityCheck&nbsp;logFile) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.bind&nbsp;(<span style="color:#2b91af;">Capacity</span>.check&nbsp;10&nbsp;<span style="color:#2b91af;">SqlGateway</span>.getReservedSeats) &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">BookingLog</span>.logAfterCapacityCheck&nbsp;logFile &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.map&nbsp;<span style="color:#2b91af;">SqlGateway</span>.saveReservation &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">BookingLog</span>.logReservationsPost&nbsp;logFile</pre> </p> <p> Notice that BookingLog.logBeforeCapacityCheck and BookingLog.logAfterCapacityCheck are composed around Capacity.check. The final BookingLog.logReservationsPost is also still in effect. Running the application shows that logging still works: </p> <p> <pre>Information: Checking capacity for 27.11.2015... Information: All is good for 27.11.2015. Information: Reservation saved.</pre> </p> <p> The first two log entries are created by the logs around Capacity.check, whereas the last line is written by BookingLog.logReservationsPost. </p> <h3 id="7983a559adb14d5fbae5566aac51df56"> Conditional logging <a href="#7983a559adb14d5fbae5566aac51df56" title="permalink">#</a> </h3> <p> Some programmers are concerned about the performance implications of logging. You may wish to be able to control whether or not to log. </p> <p> The easiest way to do that is to make logging itself conditional: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;logError&nbsp;fileName&nbsp;msg&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;log &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;SomeExternalLoggingLibrary.logError&nbsp;fileName&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;() <span style="color:blue;">let</span>&nbsp;logInformation&nbsp;fileName&nbsp;msg&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;log &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;SomeExternalLoggingLibrary.logInformation&nbsp;fileName&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;()</pre> </p> <p> where <code>log</code> is a boolean value. If <code>log</code> is false, the above two functions simply return <code>()</code> (unit) without doing anything. This prevents costly IO from happening, so may already be enough of a performance optimisation. As always when performance is the topic: <em>don't assume anything; measure</em>. </p> <p> In reality, you probably want to use more granular flags than a single <code>log</code> flag, so that you can control informational logging independently from error logging, but I'm sure you get the overall idea. </p> <h3 id="862a77131fc040e0bfbf2a6527047122"> Conditional compilation <a href="#862a77131fc040e0bfbf2a6527047122" title="permalink">#</a> </h3> <p> Even with boolean flags, you may be concerned that logging adds overhead even when the <code>log</code> flag is false. After all, you still have a function like logBeforeCapacityCheck above: it uses sprintf to format a string, and that may still be too much if it happens too often (again: <em>measure</em>). </p> <p> For the sake of argument, imagine that you've measured the cost of leaving the logging functions logReservationsPost, logBeforeCapacityCheck, and logAfterCapacityCheck in place when <code>log</code> is false, and that you find that you'll need to turn them off in production. That's not a problem. Recall that before you added these functions, the application worked fine without logging. You compose these functions into <code>imp</code> in order to add logging, but you don't have to. You can even make this decision at compile time: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;imp&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Validate</span>.reservationValid <span style="color:blue;">#if</span>&nbsp;LOG <span style="color:gray;">&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;Rop.map&nbsp;(BookingLog.logBeforeCapacityCheck&nbsp;logFile)</span> <span style="color:blue;">#endif</span> &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.bind&nbsp;(<span style="color:#2b91af;">Capacity</span>.check&nbsp;10&nbsp;<span style="color:#2b91af;">SqlGateway</span>.getReservedSeats) <span style="color:blue;">#if</span>&nbsp;LOG <span style="color:gray;">&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;BookingLog.logAfterCapacityCheck&nbsp;logFile</span> <span style="color:blue;">#endif</span> &nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;<span style="color:#2b91af;">Rop</span>.map&nbsp;<span style="color:#2b91af;">SqlGateway</span>.saveReservation <span style="color:blue;">#if</span>&nbsp;LOG <span style="color:gray;">&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&nbsp;BookingLog.logReservationsPost&nbsp;logFile</span> <span style="color:blue;">#endif</span></pre> </p> <p> Notice the presence of the conditional compilation flag <code>LOG</code>. Only if the application is compiled with the <code>LOG</code> flag will the logging code be compiled into the application; otherwise, it runs without any logging overhead at all. </p> <p> Personally, I've never needed to control logging at this level, so this isn't a recommendation; it's only a demonstration that it's possible. What's much more important to me is that everything you've seen here has required zero changes of the application code. The only code being modified is the <a href="/2011/07/28/CompositionRoot">Composition Root</a>, and I regard the Composition Root as a <a href="http://mikehadlow.blogspot.dk/2012/05/configuration-complexity-clock.html">configuration file</a>. </p> <h3 id="3ccc8739d2bd42e9af43c5cc9d6d34e9"> Summary <a href="#3ccc8739d2bd42e9af43c5cc9d6d34e9" title="permalink">#</a> </h3> <p> In a well-designed application, you should be able to append logging without impacting the core implementation. You can do that by taking a Decorator-like approach to logging, even in a Functional application. Due to the compositional nature of a well-designed code base, you can simply slide log functions in where you need them. </p> <p> Even if you're concerned about the performance implications of logging, there are various ways by which you can easily turn off logging overhead if you don't need it. Only do this if you've measured the performance of your application and found that you need to do so. The point is that if you design the application to be composed from small functions, you can always fine-tune logging performance if you need to. You don't have to do a lot of up-front design to cater specifically to logging, though. </p> <p> In my examples, I deliberately kept things crude in order to make it clear how to approach the problem, but I'm sure <a href="http://fsharpforfunandprofit.com">Scott Wlaschin</a> could teach us how to refactor such code to a sophisticated monadic design. </p> <p> It should also be noted that the approach outlined here leverages F#'s support for impure functions. All the log functions shown here return unit, which is a strong indicator of side-effects. In Haskell, logging would have to happen in an IO context, but that wouldn't impact the overall approach. The boundary of a Haskell application <em>is</em> an IO context, and the Composition Root belongs there. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f024330545d14540bec3229df4de13c4"> <div class="comment-author">Ben Lappin <a href="#f024330545d14540bec3229df4de13c4">#</a></div> <div class="comment-content"> Everything you say here makes sense, but I have a question.<br> It's implicit in these strategies that "application-specific logging module" will be a singleton, right? Otherwise, it would be necessary to pass a logger instance into every method.<br> In a multi-threaded context, this will result in all logs going to the same destination, with potential for jumbles if multiple threads are executing simultaneously, relatively unpredictable results if execution flows through multiple threads, etc.<br> I've never been able to come up with a way around the "jumble" problem other than passing a logger in every function call (or, when using OOP, giving every class a Logger property). But having every function take a "logger" parameter is not ideal either, for obvious reasons.<br> Do you have any thoughts on how to allow logging to be specific to each flow of execution, other than generating a logger at the entry point and passing it as a parameter to everything?<br> </div> <div class="comment-date">2018-10-04 18:31 UTC</div> </div> <div class="comment" id="e04ac5169abd4b58a1e5dd0ac42878ca"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e04ac5169abd4b58a1e5dd0ac42878ca">#</a></div> <div class="comment-content"> <p> Ben, thank you for writing. It's not at all implicit that Singletons are involved. This article discusses a semi-functional design where there's no <em>objects</em>, just functions. It seems, however, that some of your concerns relate to object-oriented design. </p> <p> In object-oriented design, I recommend modelling logging and other cross-cutting concerns as applications of the <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> or <a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">Chain of Responsibility</a> design patterns. As mentioned in this article, I discuss this in <a href="http://amzn.to/12p90MG">my book</a>, but this blog also contains <a href="/2010/09/20/InstrumentationwithDecoratorsandInterceptors">an example of this</a>, although it's about instrumentation instead of logging. These two cross-cutting concerns are closely related in structure, though, so I hope you still find it informative. Such an approach to design eliminates the need for passing <code>log</code> dependencies around in business logic and other places where it doesn't belong. </p> <p> The way I've seen people address the problem with multi-threaded logging is to have a logger object per thread (or, safer this day, per HTTP request, or similar). This object simply collects all log data in memory until flushed. Some designs require client developers to explicitly call a <code>Flush</code> method, but typically you can automate this so that it happens automatically when the thread or HTTP context is disposed of. </p> <p> When <code>Flush</code> is called, the infrastructure writes the entire log message to a queue. This can happen concurrently, but then you have a single-threaded subscriber on that queue that handles each message one at a time. This serialises the log messages, and you avoid interleaving of data. </p> </div> <div class="comment-date">2018-10-05 15:40 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Code coverage is a useless target measure https://blog.ploeh.dk/2015/11/16/code-coverage-is-a-useless-target-measure 2015-11-16T08:38:00+00:00 Mark Seemann <div id="post"> <p> <em>Aiming for a particular percentage of code coverage is counter-productive.</em> </p> <p> It's the end of 2015, and here I thought that it was common knowledge that using code coverage as a metric for code quality is useless at best. After all, Martin Fowler wrote <a href="http://martinfowler.com/bliki/TestCoverage.html">a good article on the subject in 2012</a>, but the fundamental realisation is much older than that. Apparently, it's one of those insights that one assumes that everyone else already knows, but in reality, that's not the case. </p> <p> Let's make it clear, then: <em>don't set goals for code coverage.</em> </p> <p> You may think that it could make your code base better, but asking developers to reach a certain code coverage goal will only make your code worse. </p> <p> I'll show you some examples, but on a general level, the reason is that as with all other measurements, you get what you measure. Unfortunately, <a href="http://martinfowler.com/bliki/CannotMeasureProductivity.html">we can't measure productivity</a>, so measuring code coverage will produce results that are entirely unrelated to software quality. <blockquote> "People respond to incentives, although not necessarily in ways that are predictable or manifest. Therefore, one of the most powerful laws in the universe is the law of unintended consequences." - <a href="http://amzn.to/1MNoami">Super Freakonomics</a> </blockquote> Incentives with negative consequences are called <em>perverse incentives</em>; asking developers to reach a particular code coverage goal is clearly a perverse incentive. </p> <p> It doesn't matter whether you set the target at 100% code coverage, 90%, 80%, or some other number. </p> <h3 id="1d542f6f714d49d589a8a439096030d0"> Reaching 100% coverage is easy <a href="#1d542f6f714d49d589a8a439096030d0" title="permalink">#</a> </h3> <p> Here's a simple code example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">GoldCustomerSpecification</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ICustomerSpecification</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsSatisfiedBy(<span style="color:#2b91af;">Customer</span>&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;candidate.TotalPurchases&nbsp;&gt;=&nbsp;10000; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Imagine that you have been asked to reach a high level of code coverage, and that this class is still not covered by tests. Not only that, but you have bugs to fix, meetings to go to, new features to implement, documentation to write, time sheets to fill out, and the project is already behind schedule, over budget, and your family is complaining that you're never home. </p> <p> Fortunately, it's easy to achieve 100% code coverage of the GoldCustomerSpecification class: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;MeaninglessTestThatStillGivesFullCoverage() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GoldCustomerSpecification</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sut.IsSatisfiedBy(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Customer</span>()); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">catch</span>&nbsp;{&nbsp;} }</pre> </p> <p> This test achieves 100% code coverage of the GoldCustomerSpecification class, but is completely useless. Because of the try/catch block and the lack of assertions, this test will never fail. This is what Martin Fowler calls <a href="http://martinfowler.com/bliki/AssertionFreeTesting.html">Assertion-Free Testing</a>. </p> <p> If you can declare a rule that your code base must have so-and-so test coverage, however, you can also declare that all unit tests must have assertions, and must not have try/catch blocks. </p> <p> Despite of this new policy, you still have lots of other things you need to attend to, so instead, you write this test: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SlightlyMoreInnocuousLookingTestThatGivesFullCoverage() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GoldCustomerSpecification</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.IsSatisfiedBy(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Customer</span>()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(actual); }</pre> </p> <p> This test also reaches 100% coverage of the GoldCustomerSpecification class. </p> <p> What's wrong with this test? Nothing, as such. It looks like a fine test, but in itself, it doesn't prevent regressions, or proves that the System Under Test works as intended. In fact, this alternative implementation also passes the test: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">GoldCustomerSpecification</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ICustomerSpecification</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsSatisfiedBy(<span style="color:#2b91af;">Customer</span>&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If you want your tests to demonstrate that the software works as intended, particularly at boundary values, you'll need to add more tests: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(100,&nbsp;<span style="color:blue;">false</span>)] [<span style="color:#2b91af;">InlineData</span>(9999,&nbsp;<span style="color:blue;">false</span>)] [<span style="color:#2b91af;">InlineData</span>(10000,&nbsp;<span style="color:blue;">true</span>)] [<span style="color:#2b91af;">InlineData</span>(20000,&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;IsSatisfiedReturnsCorrectResult( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;totalPurchases, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GoldCustomerSpecification</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;candidate&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Customer</span>&nbsp;{&nbsp;TotalPurchases&nbsp;=&nbsp;totalPurchases&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.IsSatisfiedBy(candidate); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); }</pre> </p> <p> This is a much better test, but <em>it doesn't increase code coverage!</em> Code coverage was already 100% with the SlightlyMoreInnocuousLookingTestThatGivesFullCoverage test, and it's still 100% with this test. There's no correlation between code coverage and the quality of the test(s). </p> <h3 id="b171bf079fe64d518e3d33aeeea156d5"> Code coverage objectives inhibit quality improvement <a href="#b171bf079fe64d518e3d33aeeea156d5" title="permalink">#</a> </h3> <p> Not only is test coverage percentage a meaningless number in itself, but setting a goal that must be reached actually hinders improvement of quality. Take another look at the GoldCustomerSpecification class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">GoldCustomerSpecification</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ICustomerSpecification</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsSatisfiedBy(<span style="color:#2b91af;">Customer</span>&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;candidate.TotalPurchases&nbsp;&gt;=&nbsp;10000; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Is the implementation good? Can you think of any improvements to this code? </p> <p> What happens if <code>candidate</code> is null? In that case, a NullReferenceException will be thrown. In other words, the IsSatisfiedBy method doesn't properly check that its preconditions are satisfied (which means that encapsulation is broken). </p> <p> A better implementation would be to explicitly check for null: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">GoldCustomerSpecification</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ICustomerSpecification</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsSatisfiedBy(<span style="color:#2b91af;">Customer</span>&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(candidate&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;candidate.TotalPurchases&nbsp;&gt;=&nbsp;10000; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The problem, though, is that if you do this, coverage drops! That is, unless you write another test case... </p> <p> Developers in a hurry often refrain from making the code better, because it would hurt their coverage target - and they don't feel they have time to also write the tests that go with the improvement in question. </p> <p> Instituting a code coverage target - any percentage - will have that effect. Not only does the coverage number (e.g. 87%) tell you nothing, but setting it as a target will make the code base worse. </p> <h3 id="45491fe07f0246cfb6e955bfbe51c9ef"> Attitude <a href="#45491fe07f0246cfb6e955bfbe51c9ef" title="permalink">#</a> </h3> <p> You may argue that I'm taking too dim a view on developers, but I've seen examples of the behaviour I describe. People mostly have good intentions, but if you put enough pressure on them, they'll act according to that pressure. This is the reason we need to be aware of perverse incentives. </p> <p> You may also argue that if a team is already doing Test Driven Development, and in general prioritise code quality, then coverage will already be high. In that case, will it hurt setting a target? Perhaps not, but it's not going to <em>help</em> either. At best, the target will be irrelevant. </p> <p> This article, however, doesn't discuss teams that already do everything right; it describes the negative consequences that code coverage targets will have on teams where managers or lead developers mistakenly believe that setting such goals is a good idea. </p> <h3 id="0752ebb6a0494547af0a9be8338ffcbb"> Code coverage is still useful <a href="#0752ebb6a0494547af0a9be8338ffcbb" title="permalink">#</a> </h3> <p> While it's dangerous to use code coverage for target setting, collecting coverage metrics can still be useful. </p> <p> Some people use it to <a href="https://twitter.com/yreynhout/status/664820726709551104">find areas where coverage is weak</a>. There may be good reasons that some parts of a code base are sparsely covered by tests, but doing a manual inspection once in a while is a good idea. Perhaps you find that all is good, but you may also discover that a quality effort is overdue. </p> <p> In some projects, I've had some success watching the code coverage <em>trend</em>. When I review pull requests, I first review the changes by looking at them. If the pull request needs improvement, I work with the contributor to get the pull request to an acceptable quality. Once that is done, I've already made my decision on merging the code, and <em>then</em> I measure code coverage. It doesn't influence my decision to merge, but it tells me about the trend. On some projects, I've reported that trend back to the contributor while closing the pull request. I wouldn't report the exact number, but I'd remark that coverage went up, or down, or remained the same, by 'a little', 'much', etc. The point of that is to make team members aware that testing is important. </p> <p> Sometimes, coverage goes down. There are many good reasons that could happen. Watching how coverage evolves over time doesn't mean that you have to pounce on developers every time it goes down, but it means that if something looks odd, it may be worth investigating. </p> <h3 id="df970421b35c4016bee0c37bb51f2824"> Summary <a href="#df970421b35c4016bee0c37bb51f2824" title="permalink">#</a> </h3> <p> Don't use code coverage as an objective. Code coverage has no correlation with code quality, and setting a target can easily make the quality worse. </p> <p> On the other hand, it can be useful to measure code coverage once in a while, but it shouldn't be your main source of information about the status of your source code. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Null has no type, but Maybe has https://blog.ploeh.dk/2015/11/13/null-has-no-type-but-maybe-has 2015-11-13T08:24:00+00:00 Mark Seemann <div id="post"> <p> <em>In C#, null has no type, but most variables can be null; you can't really trust the type system. A Maybe, on the other hand, always has a type, which means that Maybe is a saner approach to the question of values that may or may not be present.</em> </p> <p> A few days ago, I was looking at some C# code that, reduced to essentials, looked like this: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;foo&nbsp;=&nbsp;<span style="color:blue;">null</span>; <span style="color:blue;">var</span>&nbsp;isNullAString&nbsp;=&nbsp;foo&nbsp;<span style="color:blue;">is</span>&nbsp;<span style="color:blue;">string</span>;</pre> </p> <p> What is the value of <code>isNullAString</code> after execution? </p> <p> Since <code>foo</code> is declared as a string, I thought that the answer clearly had to be <code>true</code>. Much to my surprise, it turns out that it's <code>false</code>. </p> <p> Wondering if I was exceptionally bad at predicting the type of null values, <a href="https://twitter.com/ploeh/status/663981498144526337">I created a Twitter poll</a>. 235 votes later, the outcome was this: </p> <p> <img src="/content/binary/poll-results-for-is-a-null-string-a-string.png" alt="Poll results showing that 44% thought that the answer is true, and 56% thought that the answer is false."> </p> <p> Forty-four percent of respondents (some 103 people) were as wrong as I was! At one point, while the poll was still open and some 100 people had responded, the distribution was even fifty-fifty. Ultimately, I believe that the final results are artificially skewed toward <code>false</code>, because people could try the code first, before answering, and there's <a href="https://twitter.com/mishochu/status/664127000890609664">evidence that at least one person did that</a>. </p> <p> In short, that a null string isn't a string doesn't make much sense to a lot of people. </p> <p> It's not a bug, though. It's explicitly stated in section 7.10.10 of the C# language specification: <blockquote> "If E is [...] the null literal, of if the type of E is a reference type or a nullable type and the value of E is null, the result is false." </blockquote> The specification doesn't offer much of an explanation, but Eric Lippert shares <a href="http://ericlippert.com/2013/07/25/what-is-the-type-of-the-null-literal">some information on the topic</a>. </p> <p> It still doesn't make any <em>sense</em> to me... </p> <p> Apparently, the rules of C#'s type system is: <em>a variable is guaranteed to be of a certain type, except when it isn't</em>. Once again, <code>null</code> throws a wrench into any attempt to reason sanely about code. </p> <p> <a href="http://www.dotnetrocks.com/default.aspx?showNum=1170">The .NET Rocks! episode</a> about <a href="/2015/04/13/less-is-more-language-features">less is more</a> sparked a ton of comments; most of them in defence of <code>null</code>. People don't seem to understand just how malicious null references are. That null has no type is yet another example. </p> <p> I think that the main reason that people defend null is that they have a hard time imagining other ways of modelling situations where a value may or may not be present. Even when introduced to the Maybe monad, most people remain unconvinced, because it's difficult to understand how Maybe is better than null. </p> <p> The difference is clear: only values explicitly declared as Maybes can be Maybes, and Maybe values <em>always have a type!</em> </p> <p> In F#, Maybe is called <code>option</code>, and it's always typed. The logical equivalent of the above type check would be this in F#: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;foo&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>&nbsp;<span style="color:#2b91af;">option</span>&nbsp;=&nbsp;None <span style="color:blue;">let</span>&nbsp;isNoneAStringOption&nbsp;=&nbsp;foo&nbsp;:?&nbsp;<span style="color:#2b91af;">string</span>&nbsp;<span style="color:#2b91af;">option</span></pre> </p> <p> Only, this doesn't even compile! </p> <p> If you try this in F#, the compiler will complain: <blockquote> "error FS0016: The type 'string option' does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion." </blockquote> That expression doesn't even make sense in F#. Of course <code>foo</code> is a <code>string option</code>, because it's the only thing it can be! </p> <p> You'll have to upcast <code>foo</code> to <code>obj</code> in order to be able to perform the type check: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;foo&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>&nbsp;<span style="color:#2b91af;">option</span>&nbsp;=&nbsp;None <span style="color:blue;">let</span>&nbsp;isNoneAStringOption&nbsp;=&nbsp;box&nbsp;foo&nbsp;:?&nbsp;<span style="color:#2b91af;">string</span>&nbsp;<span style="color:#2b91af;">option</span></pre> </p> <p> As expected, this evaluates to <code>true</code>. Of course <code>isNoneAStringOption</code> is <code>true</code>, even when it's <code>None</code>! What else could it possibly be? </p> <p> In Haskell, it doesn't even make sense to ask such a question, because there's no type hierarchy. In Haskell, you can't upcast a value to its base type, because there's no inheritance. </p> <p> In short, null values invalidate all rules and guarantees that the C# type system attempts to make. It's truly a toxic language 'feature'. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="e48274cf40f64e52bb656f92e49950a6"> <div class="comment-author"><a href="http://davidarno.org">David Arno</a> <a href="#e48274cf40f64e52bb656f92e49950a6">#</a></div> <div class="comment-content"> <p><code>null</code> certainly is a toxic feature of C#. It was one of the key reasons behind me creating the <a href="https://github.com/DavidArno/SuccincT">Succinc&lt;T&gt; library</a>. It brings all the wonderfulness of F#'s options to C#. It let's one write equivalent code to your example:</p> <p><pre>var foo = Option&lt;string&gt;.None();<br>var isNoneAStringOption = foo is Option&lt;string&gt;;</pre></p> <p>Which gives the same <code>true</code> result as the F# code.</p> <p>Just because a language has a feature, doesn't mean we have to use it. The same applies just as much to <code>null</code> as to <code>switch</code> and <code>goto</code>. Alternatives exist to these features.</p> </div> <div class="comment-date">2015-11-17 10:00 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Service Locator violates encapsulation https://blog.ploeh.dk/2015/10/26/service-locator-violates-encapsulation 2015-10-26T08:39:00+00:00 Mark Seemann <div id="post"> <p> <em>Service Locator violates encapsulation in statically typed languages because it doesn't clearly communicate preconditions.</em> </p> <p> The horse has been long dead, but some people still want to ride it, so I'll beat it yet again. Over the years, I've made various attempts to explain why <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator is an anti-pattern</a> (e.g. that it <a href="/2014/05/15/service-locator-violates-solid">violates SOLID</a>), but recently it struck me that most of my arguments have been focused on <em>symptoms</em> without ever addressing the fundamental problem. </p> <p> As an example of discussing symptoms, in my <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">original article</a>, I described how IntelliSense is hurt by the use of Service Locator. In 2010, it never occurred to me that the underlying problem is that <em>encapsulation</em> is violated. </p> <p> Consider my original example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">OrderProcessor</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Process(<span style="color:#2b91af;">Order</span>&nbsp;order) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;validator&nbsp;=&nbsp;<span style="color:#2b91af;">Locator</span>.Resolve&lt;<span style="color:#2b91af;">IOrderValidator</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(validator.Validate(order)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;shipper&nbsp;=&nbsp;<span style="color:#2b91af;">Locator</span>.Resolve&lt;<span style="color:#2b91af;">IOrderShipper</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shipper.Ship(order); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is C# code, but it'd be similar in Java or another comparable statically typed language. </p> <h3 id="1e01931d4d414763a46bfbacd4ff52b0"> Pre- and postconditions <a href="#1e01931d4d414763a46bfbacd4ff52b0" title="permalink">#</a> </h3> <p> One of the major benefits of encapsulation is <em>abstraction</em>: relieving you of the burden of having to understand <em>every</em> implementation detail of <em>every</em> piece of code in your code base. Well-designed encapsulation enables you to use a class without knowing all the intricate details of how it's implemented. This is done by establishing a contract for interaction. </p> <p> As <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a> explains, a contract consists of a set of pre- and postconditions for interaction. If the client satisfies the preconditions, the object promises to satisfy the postconditions. </p> <p> In statically typed languages like C# and Java, many preconditions can be expressed with the type system itself, as I've <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">previously demonstrated</a>. </p> <p> If you look at the public API for the above OrderProcessor class, then what would you think its preconditions are? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">OrderProcessor</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Process(<span style="color:#2b91af;">Order</span>&nbsp;order) }</pre> </p> <p> As far as we can tell, there aren't many preconditions. The only one I can identify from the API is that there ought to be an Order object before you can call the Process method. </p> <p> Yet, if you attempt to use OrderProcessor using <em>only</em> that precondition, it's going to fail at run-time: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;op&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">OrderProcessor</span>(); op.Process(order);&nbsp;<span style="color:green;">//&nbsp;throws</span></pre> </p> <p> The actual preconditions are: <ul> <li>There ought to be an Order object (this one we already identified).</li> <li>There ought to be an IOrderValidator service in some Locator global directory.</li> <li>There ought to be an IOrderShipper service in some Locator global directory.</li> </ul> Two out of three preconditions were invisible at compile-time. </p> <p> As you can see, Service Locator violates encapsulation because it hides the preconditions for correct use. </p> <h3 id="9f067acaeffa4424bc48f22976bf6f68"> Passing arguments <a href="#9f067acaeffa4424bc48f22976bf6f68" title="permalink">#</a> </h3> <p> Several people have jokingly identified Dependency Injection as a glorified term for <em>passing arguments</em>, and there may be some truth to that. The easiest way to make the preconditions apparent would be to use the type system to advertise the requirements. After all, we already figured out that an Order object is required. This was evident because Order is an argument to the Process method. </p> <p> Can you make the need for IOrderValidator and IOrderShipper as apparent as the need for the Order object using the same technique? Is the following a possible solution? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Process( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Order</span>&nbsp;order, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IOrderValidator</span>&nbsp;validator, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IOrderShipper</span>&nbsp;shipper)</pre> </p> <p> In some circumstances, this could be all you need to do; now the three preconditions are equally apparent. </p> <p> Unfortunately, often this isn't possible. In this case, OrderProcessor implements the IOrderProcessor interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Process(<span style="color:#2b91af;">Order</span>&nbsp;order); }</pre> </p> <p> Since the shape of the Process method is already defined, you can't add more arguments to it. You can still make the preconditions visible via the type system by requiring the caller to pass the required objects as arguments, but you'll need to pass them via some other member. The constructor is the safest channel: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">OrderProcessor</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IOrderValidator</span>&nbsp;validator; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IOrderShipper</span>&nbsp;shipper; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;OrderProcessor(<span style="color:#2b91af;">IOrderValidator</span>&nbsp;validator,&nbsp;<span style="color:#2b91af;">IOrderShipper</span>&nbsp;shipper) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(validator&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;validator&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(shipper&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;shipper&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.validator&nbsp;=&nbsp;validator; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.shipper&nbsp;=&nbsp;shipper; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Process(<span style="color:#2b91af;">Order</span>&nbsp;order) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.validator.Validate(order)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.shipper.Ship(order); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> With this design, the public API now looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">OrderProcessor</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;OrderProcessor(<span style="color:#2b91af;">IOrderValidator</span>&nbsp;validator,&nbsp;<span style="color:#2b91af;">IOrderShipper</span>&nbsp;shipper) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Process(<span style="color:#2b91af;">Order</span>&nbsp;order) }</pre> </p> <p> Now it's clear that all three object are required before you can call the Process method; this version of the OrderProcessor class advertises its preconditions via the type system. You can't even compile client code unless you pass arguments to constructor and method (you can pass null, but that's another discussion). </p> <h3 id="8bb504fe49cf478c9124997cd7bbc0fe"> Summary <a href="#8bb504fe49cf478c9124997cd7bbc0fe" title="permalink">#</a> </h3> <p> Service Locator is an anti-pattern in statically typed, object-oriented languages because it violates encapsulation. The reason is that it hides preconditions for proper usage. </p> <p> If you need an accessible introduction to encapsulation, you should consider watching my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID</a> Pluralsight course. If you wish to learn more about Dependency Injection, you can read my <a href="/2013/10/02/di-in-net-receives-a-jolt-productivity-award">award-winning</a> book <a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="8ccaa76bc78144fe9a3d25841fd64846"> <div class="comment-author"><a href="https://baks.github.io">Arkadiusz K</a> <a href="#8ccaa76bc78144fe9a3d25841fd64846">#</a></div> <div class="comment-content"> <p>If we take a look at the original example, we should notice that terms from multiple domains are interleaving. Therefore, the OrderProcessor is violating <em>context independence</em> as described in <a href="http://amzn.com/0321503627">GOOS</a> book. To become <em>context independent</em> OrderProcessor should make his relationships explicit by allowing to pass them in constructor.</p> <p>It is a slightly different perspective of the problem, but conclusion is the same, because <em>context independence</em> also concerns <em>encapsulation</em>.</p> </div> <div class="comment-date">2015-10-27 20:00 UTC</div> </div> <div class="comment" id="0c18009c2c274c3784e9f839963addd5"> <div class="comment-author"><a href="https://stackoverflow.com/users/945456/jeff-bridgman">Jeffrey Bridgman</a> <a href="#0c18009c2c274c3784e9f839963addd5">#</a></div> <div class="comment-content"> <p>Is it only the usage of a Service Locator within a class that's an anti-pattern? That is, as long as OrderProcessor makes its dependencies explicit via the constructor, there's nothing wrong with using a Service Locator to get those dependencies when creating a OrderProcessor instance?</p> </div> <div class="comment-date">2015-11-19 23:21 UTC</div> </div> <div class="comment" id="5c9f14e25fe84b20a861342290ec65aa"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5c9f14e25fe84b20a861342290ec65aa">#</a></div> <div class="comment-content"> <p> Jeffrey, thank you for writing. I'm not sure I fully understand, but perhaps you are considering whether the use of a DI Container as a composition engine is also an anti-pattern? </p> <p> If so, <a href="/2011/08/25/ServiceLocatorrolesvs.mechanics">you can use a DI Container from within your Composition Root</a>, but personally, I still prefer <a href="/2014/06/10/pure-di">Pure DI</a>. </p> </div> <div class="comment-date">2015-11-20 7:06 UTC UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Visual Value Verification https://blog.ploeh.dk/2015/10/19/visual-value-verification 2015-10-19T08:08:00+00:00 Mark Seemann <div id="post"> <p> <em>Sometimes, the most efficient way to verify the outcome of executing a piece of code is to visualise it.</em> </p> <p> Recently, I've been working my way through <a href="http://bit.ly/real-world-haskell">Real World Haskell</a>, and although some of the exercises in the book are exasperating, others are stimulating and engaging. One of the good exercises is to use the <a href="https://en.wikipedia.org/wiki/Graham_scan">Graham Scan</a> algorithm to find the <a href="https://en.wikipedia.org/wiki/Convex_hull">convex hull</a> for a set of points. </p> <p> This proved to be unexpectedly difficult for me, but I also found the exercise captivating, so I kept at it. My main problems turned out to be related to the algorithm itself, so during the exercise, I temporarily switched to F# in order to work out the kinks of my implementation. This enabled me to focus on the problem itself without also having to fight with an unfamiliar programming language. </p> <p> Surprisingly, it turned out that one of my biggest problems was that I didn't have a good way to verify my implementation. </p> <h3 id="68568f2d199e4f14b0c5d256ac8df08f"> Return values <a href="#68568f2d199e4f14b0c5d256ac8df08f" title="permalink">#</a> </h3> <p> Since I was approaching the problem with Functional Programming, it ought to be easy to unit test. After all, <a href="/2015/05/07/functional-design-is-intrinsically-testable">Functional design is intrinsically testable</a>. My overall function to find the convex hull looks like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">inline</span>&nbsp;hull&nbsp;points&nbsp;= <span style="color:green;">//&nbsp;...</span> </pre> </p> <p> In simplified form, the type of this function is <code>(^a * ^d) list -&gt; (^a * ^d) list</code> where the <code>^a</code> and <code>^d</code> generic type arguments have a whole lot of constraints that I don't want to bother you with. In practice, both <code>^a</code> and <code>^d</code> can be integers, so that the hull function gets the type <code>(int * int) list -&gt; (int * int) list</code>. In other words: you supply a list of integer points, and you get a list of integer points back. </p> <p> Here's a simple example: </p> <p> <pre>&gt; hull [(3, 1); (2, 3); (2, 4); (2, 5); (3, 7); (1, 2); (1, 6)];; val it : (int * int) list = [(3, 1); (3, 7); (2, 5); (1, 6); (1, 2)]</pre> </p> <p> Quick! At a glance: <em>is this result correct or incorrect?</em> </p> <p> How about this result? </p> <p> <pre>&gt; hull [(5, -2); (5, 6); (-4, 7); (-6, 0); (-8, 0); (-2, 5); (-3, -4); (-2, -2); (-9, -7); (2, -9); (4, -2); (2, -10); (4, -10); (4, -9); (2, -10); (3, -9); (8, 2); (-8, -5); (-9, -4); (5, -6); (6, 4); (8, -10); (-5, 0); (5, 9); (-5, -4); (-6, 8); (0, -9); (7, -4); (6, 4); (-8, -5); (-7, -7); (8, -9); (7, -3); (6, 4); (-6, -8); (-4, 4); (-2, -2); (-6, -10); (0, 1); (5, -7); (-5, 4); (5, -5); (6, 4); (0, 7); (5, 5); (-1, -4); (-6, 0); (-9, 3); (5, 6); (-7, 7); (4, -10); (5, -8); (9, -1); (0, -9); (6, 6); (6, -6); (9, 8); (-10, -2); (-3, 2); (-5, -7)];; val it : (int * int) list = [(-6, -10); (2, -10); (4, -10); (8, -10); (9, -1); (9, 8); (5, 9); (-6, 8); (-7, 7); (-9, 3); (-10, -2); (-9, -7)]</pre> </p> <p> (In the first example, the output is incorrect, but in the second, it's correct.) </p> <p> It's easy enough to write automated unit tests <em>once you know what the expected outcome should be</em>. In this case, my problem was that I didn't have an easy way to calculate if a given list of points was the correct answer or not. After all, I was trying to implement a function that could be used for this purpose, but I needed to know if the function returned the correct values. </p> <p> In the beginning, I tried to plot the values into Excel, in order to draw them as diagrams, but that soon turned out to be tedious and inefficient. </p> <p> Then I considered Property-Based Testing, but I couldn't come up with a good set of properties that didn't involve half of the algorithm I was trying to implement. </p> <h3 id="f4f2ecb7d66142e98a1fb0a365143eb1"> Visual Value Verification <a href="#f4f2ecb7d66142e98a1fb0a365143eb1" title="permalink">#</a> </h3> <p> The concept of a convex hull is simple, and easy to verify if you can visualise it. That's what I tried to do with Excel, but here my problem was that the process was too cumbersome. </p> <p> Instead, I decided to pull in <a href="http://fslab.org/FSharp.Charting">FSharp.Charting</a>, because it enabled me to easily visualise the result of calling the hull function. This is all it takes: </p> <p> <pre><span style="color:blue;">open</span>&nbsp;System <span style="color:blue;">open</span>&nbsp;FSharp.Charting <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">inline</span>&nbsp;hullChart&nbsp;points&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hullPoints&nbsp;=&nbsp;hull&nbsp;points &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hullChart&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;closedHull&nbsp;=&nbsp;hullPoints&nbsp;@&nbsp;[hullPoints.Head] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Chart</span>.Line(closedHull,&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Hull&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Chart</span>.WithStyling(Color&nbsp;=&nbsp;Drawing.<span style="color:#2b91af;">Color</span>.Blue) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;pointsChart&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Chart</span>.Point(points,&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Points&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Chart</span>.WithStyling(Color&nbsp;=&nbsp;Drawing.<span style="color:#2b91af;">Color</span>.Black) &nbsp;&nbsp;&nbsp;&nbsp;[hullChart;&nbsp;pointsChart] &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Chart</span>.Combine &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Chart</span>.WithYAxis(MajorGrid&nbsp;=&nbsp;<span style="color:#2b91af;">ChartTypes</span>.<span style="color:#2b91af;">Grid</span>(Enabled&nbsp;=&nbsp;<span style="color:blue;">false</span>)) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Chart</span>.WithXAxis(MajorGrid&nbsp;=&nbsp;<span style="color:#2b91af;">ChartTypes</span>.<span style="color:#2b91af;">Grid</span>(Enabled&nbsp;=&nbsp;<span style="color:blue;">false</span>))</pre> </p> <p> The signature of the hullChart function is <code>(^a * ^d) list -&gt; FSharp.Charting.ChartTypes.GenericChart</code> (where, again, the <code>^a</code> and <code>^d</code> types are generic type arguments with various type constraints; think of them as numbers). It first calls the <code>hull</code> function with <code>points</code>. Then it creates a line chart to draw the hull, and a point chart to plot in all the input points. Finally, it combines both charts into a single chart. </p> <p> With the hullChart function, it's easy to do ad-hoc testing in F# Interactive and visually inspect the results of calling the hull function with various input. At one point, I had a <a href="http://math.stackexchange.com/q/1338540/250481">particular problem with my interpretation of the Graham Scan algorithm</a>, and this was easy to see using the hullChart function, which would produce this chart: </p> <p> <img src="/content/binary/concave-hull.png" alt="A hull diagram that shows the calculated hull to be concave."> </p> <p> With this chart it's easy to see, at a glance, that the calculated hull is concave, and thus not a convex hull. There's an error in the implementation. (This is the first result set that I asked you to evaluate above.) </p> <p> Struggling on with the exercise, I was able to solve my immediate problem and produce a convex hull from that particular input. Did that mean that I now had the correct implementation, or could there be other errors? I needed more test results before I felt that I had confidence in my implementation. </p> <p> This, on the other hand, was now easy to get. </p> <p> First, I could randomly generate points like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;randomPoints&nbsp;(r&nbsp;:&nbsp;<span style="color:#2b91af;">Random</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[1..r.Next(1,&nbsp;100)] &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(r.Next(-10,&nbsp;10),&nbsp;r.Next(-10,&nbsp;10)))</pre> </p> <p> For ad-hoc testing, I could now create a random set of points and show the calculated hull: </p> <p> <pre>&gt; (randomPoints (Random()) |> hullChart).ShowChart();;</pre> </p> <p> Immediately, a window would pop up, enabling me to visually verify the calculated hull value. Literally, verification at a glance. </p> <h3 id="ed63addf97db4fd4965aa10b9e12ad06"> From Visual Value Verification to automated tests <a href="#ed63addf97db4fd4965aa10b9e12ad06" title="permalink">#</a> </h3> <p> You may object to this approach because such testing isn't sustainable. Ultimately, we'd like to have a suite of automated tests that can give us a <em>succeed</em> or <em>failure</em> result. </p> <p> Still, the ability to visually verify the calculated hull values enabled me to produce a set of input points, as well as calculated hull points that I <em>knew</em> to be correct. I could, for example, use the randomPoints function to produce 100 input sets. For each of these 100 input sets, I could visually inspect the diagrams. </p> <p> Here's an example of six diagrams, instead of 100, just to give you an idea about how quickly you can verify such a set: </p> <p> <img src="/content/binary/hull-grid.png" alt="Six individual hull diagrams arranged in a 2x3 grid, each of them displaying convex hulls."> </p> <p> If all of the generated diagrams look okay, you know that for at least these 100 sets, the output of the hull function is correct. You can now capture those input values and the corresponding (correct) output values as a parametrised test. Here's an xUnit.net example with five test cases: </p> <p> <pre><span style="color:green;">//&nbsp;No&nbsp;[&lt;ClassData&gt;]&nbsp;attribute&nbsp;in&nbsp;xUnit.net&nbsp;2.0&nbsp;:(</span> <span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">HullDataAttribute</span>()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;Xunit.Sdk.<span style="color:#2b91af;">DataAttribute</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">override</span>&nbsp;this.GetData&nbsp;testMethod&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;The&nbsp;following&nbsp;input&nbsp;data&nbsp;comes&nbsp;from&nbsp;randomly&nbsp;generated&nbsp;points.</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;The&nbsp;expected&nbsp;values&nbsp;come&nbsp;from&nbsp;a&nbsp;prototype&nbsp;of&nbsp;the&nbsp;hull&nbsp;function&nbsp;where</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;the&nbsp;output&nbsp;was&nbsp;subsequently&nbsp;visually&nbsp;inspected&nbsp;by&nbsp;drawing&nbsp;the&nbsp;input</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;points&nbsp;and&nbsp;the&nbsp;calculated&nbsp;hull&nbsp;on&nbsp;a&nbsp;coordinate&nbsp;system&nbsp;to&nbsp;verify&nbsp;that</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;the&nbsp;hull&nbsp;prototype&nbsp;function&nbsp;calculated&nbsp;the&nbsp;correct&nbsp;values.</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">seq</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Points&nbsp;(input):</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(3,&nbsp;1);&nbsp;(3,&nbsp;7);&nbsp;(2,&nbsp;5);&nbsp;(2,&nbsp;4);&nbsp;(1,&nbsp;6);&nbsp;(2,&nbsp;3);&nbsp;(1,&nbsp;2)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Expected:</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(3,&nbsp;1);&nbsp;(3,&nbsp;7);&nbsp;(1,&nbsp;6);&nbsp;(1,&nbsp;2)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(1,&nbsp;-4);&nbsp;(2,&nbsp;5);&nbsp;(1,&nbsp;3);&nbsp;(1,&nbsp;-3);&nbsp;(1,&nbsp;-2);&nbsp;(0,&nbsp;4)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(1,&nbsp;-4);&nbsp;(2,&nbsp;5);&nbsp;(0,&nbsp;4)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(1,&nbsp;1);&nbsp;(0,&nbsp;3);&nbsp;(-2,&nbsp;1);&nbsp;(-4,&nbsp;3);&nbsp;(5,&nbsp;2);&nbsp;(3,&nbsp;2);&nbsp;(5,&nbsp;5);&nbsp;(2,&nbsp;5);&nbsp;(1,&nbsp;3);&nbsp;(1,&nbsp;-3);&nbsp;(1,&nbsp;-2);&nbsp;(7,&nbsp;-4);&nbsp;(-1,&nbsp;1);&nbsp;(-3,&nbsp;0);&nbsp;(-5,&nbsp;-2);&nbsp;(1,&nbsp;-4);&nbsp;(0,&nbsp;1);&nbsp;(0,&nbsp;4);&nbsp;(3,&nbsp;-3);&nbsp;(6,&nbsp;1)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(1,&nbsp;-4);&nbsp;(7,&nbsp;-4);&nbsp;(6,&nbsp;1);&nbsp;(5,&nbsp;5);&nbsp;(2,&nbsp;5);&nbsp;(-4,&nbsp;3);&nbsp;(-5,&nbsp;-2)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(-7,&nbsp;-7);&nbsp;(4,&nbsp;-7);&nbsp;(2,&nbsp;3);&nbsp;(4,&nbsp;4);&nbsp;(3,&nbsp;1);&nbsp;(2,&nbsp;-1);&nbsp;(-3,&nbsp;-5);&nbsp;(4,&nbsp;-2);&nbsp;(-1,&nbsp;-7);&nbsp;(-6,&nbsp;9);&nbsp;(4,&nbsp;4);&nbsp;(-8,&nbsp;-2);&nbsp;(9,&nbsp;4);&nbsp;(3,&nbsp;0);&nbsp;(7,&nbsp;0);&nbsp;(-7,&nbsp;3);&nbsp;(0,&nbsp;9);&nbsp;(4,&nbsp;-7);&nbsp;(-7,&nbsp;-6);&nbsp;(-1,&nbsp;7);&nbsp;(6,&nbsp;5);&nbsp;(7,&nbsp;-3);&nbsp;(-8,&nbsp;-8);&nbsp;(-6,&nbsp;-2);&nbsp;(3,&nbsp;5);&nbsp;(-5,&nbsp;7);&nbsp;(8,&nbsp;1);&nbsp;(3,&nbsp;-2);&nbsp;(-9,&nbsp;-4);&nbsp;(-7,&nbsp;8)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(-8,&nbsp;-8);&nbsp;(4,&nbsp;-7);&nbsp;(7,&nbsp;-3);&nbsp;(9,&nbsp;4);&nbsp;(0,&nbsp;9);&nbsp;(-6,&nbsp;9);&nbsp;(-7,&nbsp;8);&nbsp;(-9,&nbsp;-4)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(3,&nbsp;-3);&nbsp;(-9,&nbsp;-3);&nbsp;(0,&nbsp;7);&nbsp;(3,&nbsp;8);&nbsp;(3,&nbsp;-9);&nbsp;(1,&nbsp;3);&nbsp;(-9,&nbsp;5);&nbsp;(-4,&nbsp;9);&nbsp;(-2,&nbsp;-10);&nbsp;(8,&nbsp;-2);&nbsp;(-4,&nbsp;2);&nbsp;(-7,&nbsp;-9);&nbsp;(-5,&nbsp;-10);&nbsp;(0,&nbsp;2);&nbsp;(9,&nbsp;-7);&nbsp;(6,&nbsp;-4);&nbsp;(4,&nbsp;7);&nbsp;(-9,&nbsp;-7);&nbsp;(2,&nbsp;1);&nbsp;(-3,&nbsp;-5);&nbsp;(-5,&nbsp;-1);&nbsp;(9,&nbsp;6);&nbsp;(-3,&nbsp;1);&nbsp;(6,&nbsp;-6);&nbsp;(-5,&nbsp;-4);&nbsp;(-6,&nbsp;5);&nbsp;(0,&nbsp;9);&nbsp;(-2,&nbsp;-9);&nbsp;(-6,&nbsp;-10);&nbsp;(-8,&nbsp;-1);&nbsp;(-4,&nbsp;-9);&nbsp;(8,&nbsp;-1);&nbsp;(-5,&nbsp;-5);&nbsp;(9,&nbsp;-6);&nbsp;(4,&nbsp;-8);&nbsp;(-3,&nbsp;7);&nbsp;(2,&nbsp;3);&nbsp;(-8,&nbsp;6);&nbsp;(3,&nbsp;-4);&nbsp;(3,&nbsp;4);&nbsp;(-6,&nbsp;-5);&nbsp;(-4,&nbsp;3);&nbsp;(9,&nbsp;-10);&nbsp;(5,&nbsp;4);&nbsp;(-1,&nbsp;9);&nbsp;(9,&nbsp;1);&nbsp;(-1,&nbsp;7);&nbsp;(8,&nbsp;-7);&nbsp;(1,&nbsp;-1);&nbsp;(0,&nbsp;-9);&nbsp;(2,&nbsp;1);&nbsp;(0,&nbsp;-8);&nbsp;(8,&nbsp;-3);&nbsp;(-8,&nbsp;7);&nbsp;(7,&nbsp;1);&nbsp;(-2,&nbsp;8);&nbsp;(-4,&nbsp;-2);&nbsp;(-5,&nbsp;-10);&nbsp;(4,&nbsp;-6);&nbsp;(0,&nbsp;-5);&nbsp;(-1,&nbsp;-6);&nbsp;(5,&nbsp;4);&nbsp;(-7,&nbsp;6);&nbsp;(-3,&nbsp;4);&nbsp;(4,&nbsp;8);&nbsp;(-6,&nbsp;-7);&nbsp;(5,&nbsp;2);&nbsp;(-9,&nbsp;2);&nbsp;(5,&nbsp;-6);&nbsp;(4,&nbsp;2);&nbsp;(7,&nbsp;8);&nbsp;(7,&nbsp;7)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(-6,&nbsp;-10);&nbsp;(-5,&nbsp;-10);&nbsp;(-2,&nbsp;-10);&nbsp;(9,&nbsp;-10);&nbsp;(9,&nbsp;-7);&nbsp;(9,&nbsp;-6);&nbsp;(9,&nbsp;1);&nbsp;(9,&nbsp;6);&nbsp;(7,&nbsp;8);&nbsp;(0,&nbsp;9);&nbsp;(-1,&nbsp;9);&nbsp;(-4,&nbsp;9);&nbsp;(-8,&nbsp;7);&nbsp;(-9,&nbsp;5);&nbsp;(-9,&nbsp;2);&nbsp;(-9,&nbsp;-3);&nbsp;(-9,&nbsp;-7)] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} [&lt;<span style="color:#2b91af;">Theory</span>;&nbsp;<span style="color:#2b91af;">HullData</span>&gt;] <span style="color:blue;">let</span>&nbsp;``hull&nbsp;returns&nbsp;correct&nbsp;result`` &nbsp;&nbsp;&nbsp;&nbsp;(points&nbsp;:&nbsp;(<span style="color:#2b91af;">int</span>&nbsp;*&nbsp;<span style="color:#2b91af;">int</span>)&nbsp;<span style="color:#2b91af;">list</span>) &nbsp;&nbsp;&nbsp;&nbsp;(expected&nbsp;:&nbsp;(<span style="color:#2b91af;">int</span>&nbsp;*&nbsp;<span style="color:#2b91af;">int</span>)&nbsp;<span style="color:#2b91af;">list</span>)&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;hull&nbsp;points &nbsp;&nbsp;&nbsp;&nbsp;expected&nbsp;=!&nbsp;actual</pre> </p> <p> (The <code>=!</code> operator is an assertion operator from <a href="http://www.swensensoftware.com/unquote">Unquote</a>; read it as <em>should equal</em> - i.e. <em>expected should equal actual</em>.) </p> <p> This gives you a deterministic test suite you can run repeatedly to protect the hull function against regressions. </p> <h3 id="67da2f1a8ff14986a32936d10b45d9b6"> Summary <a href="#67da2f1a8ff14986a32936d10b45d9b6" title="permalink">#</a> </h3> <p> Sometimes, the nature of the problem is such that the easiest way to verify that the System Under Test (SUT) produces the correct results, is to visually verify the resulting value of exercising the SUT. We can call this Visual Value Verification (VVV). </p> <p> In this article, I used the problem of finding the convex hull for a set of points as an example, but I've encountered other problems where this technique has proven useful. The most prominent that comes to mind is when implementing <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's Game of Life</a>; that's another problem where, just by looking at lists of numbers, you can't easily verify that your implementation is correct. </p> <p> Once you've visually verified that output looks correct, you can capture the known input and output into a test case that you can run repeatedly. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Command Query Separation when Queries should have side-effects https://blog.ploeh.dk/2015/10/08/command-query-separation-when-queries-should-have-side-effects 2015-10-08T15:50:00+00:00 Mark Seemann <div id="post"> <p> <em>How can you adhere to Command Query Separation if your Domain Model requires Queries to have side-effects?</em> </p> <p> <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> (CQS) can be difficult until you get the hang of it; then it's not so difficult - just like most other things in life :) </p> <p> In a previous article, I covered <a href="/2014/08/11/cqs-versus-server-generated-ids">how to retrieve server-generated IDs after Create operations</a>. That article discussed how to prevent Commands from turning into Queries. In the present article, you'll see some examples of how to prevent Queries from turning into Commands. </p> <h3 id="a853b94ae98248f095d42dca5f438cc4"> Context <a href="#a853b94ae98248f095d42dca5f438cc4" title="permalink">#</a> </h3> <p> This article was triggered by a viewer's question related to my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID</a> Pluralsight course. As I interpret it, the hypothetical scenario is some school or university exam taking software: <blockquote> "If a student has not submitted a solution to an exercise yet, when and if they look at the exercise hint for the first time, flag that hint as viewed. The points granted to a student's solution will be subtracted by 5 points, if the related hint is flagged as viewed." </blockquote> As stated here, it sounds like a Query (reading the exercise hint) <em>must</em> have a side-effect. This time, we can't easily wave it away by saying that <a href="/2015/09/23/applications-and-their-side-effects">the side-effect is one that the client isn't responsible for, so it'll be OK</a>. If the side-effect had been an audit log, we could have gotten away with that, but here the side-effect is <em>within</em> the Domain Model itself. </p> <p> How can you implement this business requirement while still adhering to CQS? Perhaps you'd like to pause reading for a moment to reflect on this question; then you can compare your notes to mine. </p> <p> Is it even worth applying CQS to this problem, or should we simply give up? After all, the Domain Model seems to <em>inherently</em> associate certain Queries with side-effects. </p> <p> In my opinion, it's exactly in such scenarios that CQS really shines. Otherwise, you're looking at the code as a team developer, and you go: <em>Why did the score just go down? I didn't change anything!</em> You can waste hours when side-effects are implicit. Applying CQS makes side-effects explicit, and as the <a href="https://www.python.org/dev/peps/pep-0020">Zen of Python</a> goes: <blockquote> Explicit is better than implicit. </blockquote> There are various ways to address this apparently impossible problem. You don't <em>have to</em> use any of them, but the first key to choosing your tools is to have something to choose <em>from</em>. </p> <h3 id="ba125b019453408b80c0749b6b28a4d0"> Contextual types <a href="#ba125b019453408b80c0749b6b28a4d0" title="permalink">#</a> </h3> <p> With the requirements given above, we don't know what we're building. Is it a web-based application? An app? A desktop application? Let's, for a while, imagine that we're developing an app or desktop application. In my fevered imagination, this sort of application may have all the questions and hints preloaded in memory, or in files, and continually displays the current score on the screen. There may not be further persistent storage, or perhaps the application publishes the final scores for the exam to a central server once the exam is over. Think occasionally connected clients. </p> <p> In this type of scenario, the most important point is to keep the score up-to-date in memory. This can easily be done with a contextual or 'amplified' type. In this case, we can call it Scored&lt;T&gt;: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Scored</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Score; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Scored(<span style="color:#2b91af;">T</span>&nbsp;item,&nbsp;<span style="color:blue;">int</span>&nbsp;score) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(item&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(item)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Item&nbsp;=&nbsp;item; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Score&nbsp;=&nbsp;score; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Scored</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Add(<span style="color:blue;">int</span>&nbsp;scoreDelta) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Scored</span>&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>.Item,&nbsp;<span style="color:blue;">this</span>.Score&nbsp;+&nbsp;scoreDelta); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;other&nbsp;=&nbsp;obj&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:#2b91af;">Scored</span>&lt;<span style="color:#2b91af;">T</span>&gt;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">base</span>.Equals(obj); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.Item,&nbsp;other.Item) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.Score,&nbsp;other.Score); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Item.GetHashCode()&nbsp;^ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.Score.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The Scored&lt;T&gt; class enables you to carry a score value around within a computation. In order to keep the example as simple as possible, I modelled the score as an integer, but perhaps you should consider <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">refactoring from Primitive Obsession to Domain Modelling</a>; that's a different story, though. </p> <p> This means you can model your API in such a way that a client <em>must</em> supply the current score in order to retrieve a hint, and the new score is returned together with the hint: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IHintQuery</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Scored</span>&lt;<span style="color:#2b91af;">Hint</span>&gt;&nbsp;Read(<span style="color:blue;">int</span>&nbsp;hintId,&nbsp;<span style="color:blue;">int</span>&nbsp;currentScore); }</pre> </p> <p> The Read method is a Query, and there's no implied side-effect by calling it. Since the return type is Scored&lt;Hint&gt;, it should be clear to the client that the score may have changed. </p> <p> An implementation could look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">HintQuery</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IHintQuery</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IHints</span>&nbsp;hints; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;HintQuery(<span style="color:#2b91af;">IHints</span>&nbsp;hints) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(hints&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(hints)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.hints&nbsp;=&nbsp;hints; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Scored</span>&lt;<span style="color:#2b91af;">Hint</span>&gt;&nbsp;Read(<span style="color:blue;">int</span>&nbsp;hintId,&nbsp;<span style="color:blue;">int</span>&nbsp;currentScore) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;valFromInner&nbsp;=&nbsp;<span style="color:blue;">this</span>.hints.FirstById(hintId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Scored</span>&lt;<span style="color:#2b91af;">Hint</span>&gt;(valFromInner,&nbsp;currentScore).Add(-5); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The Read method uses an injected (<a href="/2015/09/21/public-types-hidden-in-plain-sight">lower-level</a>) Query interface to read the answer hint, packages the result in a Scored&lt;Hint&gt; value, and subtracts 5 points from the score. </p> <p> Both the score type (int) and Scored&lt;T&gt; are immutable. No side-effects occur while the client reads the answer hint, but the score is nonetheless adjusted. </p> <p> In this scenario, the score travels around in the memory of the application. Perhaps, after the exam is over, the final score can be sent to a central repository for record-keeping. This architecture could work well in client-side implementations, but may be less suitable in stateless web scenarios. </p> <h3 id="73896e8bbbda4c57a1ea180f18f9f0f4"> Pessimistic locking <a href="#73896e8bbbda4c57a1ea180f18f9f0f4" title="permalink">#</a> </h3> <p> If you're developing a web-based exam-taking system, you may want to be able to use stateless web servers for scalability or redundancy reasons. In such cases, perhaps keeping the score in memory isn't a good idea. </p> <p> You could still use the above model, but the client must remember to save the updated score before returning an HTTP response to the browser. Perhaps you find this unsatisfactorily fail-safe, so here's an alternative: use pessimistic locking. </p> <p> Essentially, you can expose an interface like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IHintRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;UnlockHint(<span style="color:blue;">int</span>&nbsp;hintId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Hint</span>&nbsp;Read(<span style="color:blue;">int</span>&nbsp;hintId); }</pre> </p> <p> If a client attempts to call the Read method without first unlocking the hint, the method will throw an exception. First, you'll have to unlock the hint using the UnlockHint method, which is clearly a Command. </p> <p> This is less discoverable, because you can't tell by the type signature of the Read method that it may fail for that particular reason, but it safely protects the system from accidentally reading the hint without impacting the score. </p> <p> (In type systems with <a href="https://en.wikipedia.org/wiki/Tagged_union">Sum types</a>, you can make the design clearer by statically modelling the return type to be one of several mutually exclusive cases: hint, no hint (<code>hintId</code> doesn't refer to an existing hint), or hint is still locked.) </p> <p> This sort of interface might in fact align well with a good User Experience, because you might want to ask the user if he or she is sure that (s)he wants to see the hint, given the cost. Such a user interface warning would be followed by a call to UnlockHint if the user agrees to the score deduction. </p> <p> An implementation of UnlockHint would leave behind a permanent record that the answer hint was unlocked by a particular user, and that record can then subsequently be used when calculating the final score. </p> <h3 id="c2ca7b7df80546b3ac1cc9c4c179d331"> Summary <a href="#c2ca7b7df80546b3ac1cc9c4c179d331" title="permalink">#</a> </h3> <p> Sometimes, it can be difficult to see how to both follow CQS <em>and</em> implement the desired business logic. In my experience, it's always possible to recast the problem in such a way that this is possible, but it may take some deliberation before it clicks. </p> <p> Must you always follow CQS? Not necessarily, but if you understand what your options are, then you know what you're saying no to if you decide not to do it. That's quite a different situation from not having any idea about how to apply the principle. </p> <p> In this article, I showed two options for reconciling CQS with a Domain Model where a Query seems to have side-effects. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4c572e9c91ca40beb9bed9a0c52c0534"> <div class="comment-author"><a href="https://pvlerick.github.io/">Philippe</a> <a href="#4c572e9c91ca40beb9bed9a0c52c0534">#</a></div> <div class="comment-content">Hi Mark, don't you think that the pessimistic locking is a case of <a href="/2011/05/24/DesignSmellTemporalCoupling/">temporal coupling</a>?</div> <div class="comment-date">2015-10-09 07:08 UTC</div> </div> <div class="comment" id="0b57df664fd54fbb891f83597e2b2c2f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0b57df664fd54fbb891f83597e2b2c2f">#</a></div> <div class="comment-content"> <p> Philippe, thank you for writing. That's a great observation, and one that I must admit that I hadn't considered myself! </p> <p> At least, in this case <em>encapsulation</em> is still intact because pre- and post-conditions are preserved. You can't leave the system in an incorrect state. </p> <p> The reason I described the option using Scored&lt;T&gt; before the pessimistic locking alternative is that I like the first option best. Among other benefits, it doesn't suffer from temporal coupling. </p> </div> <div class="comment-date">2015-10-09 07:59 UTC</div> </div> <div class="comment" id="7eae7b908d9944138344744cde9289b6"> <div class="comment-author"><a href="http://gnugat.github.io/">Loïc Faugeron</a> <a href="#7eae7b908d9944138344744cde9289b6">#</a></div> <div class="comment-content"> <p> Hi Mark, those are all nice solutions! </p> <p> I think there are also other options, for example sending a "Excercice hint viewed" notification which could then be handled by a subscriber calling a command.<br> But this is at the cost of some indirection, so it's nice to have other choices. </p> </div> <div class="comment-date">2015-10-09 12:01 UTC</div> </div> <div class="comment" id="b20723d284384210bfd985be678ef5de"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b20723d284384210bfd985be678ef5de">#</a></div> <div class="comment-content"> <p> Loïc, thank you for writing. I'm sure there are other alternatives than the ones I've outlined. The purpose of the article wasn't to provide an exhaustive list of options, but rather explain that it <em>is</em> possible to adhere to the CQS, even though sometimes it seems difficult. </p> <p> Specifically, are you suggesting to send a notification when the Query is made? Isn't that a side-effect? </p> </div> <div class="comment-date">2015-10-09 12:23 UTC</div> </div> <div class="comment" id="f6d7fc95670240a9a67f1ef9b96b6b51"> <div class="comment-author"><a href="http://iamkoch.com">Antony Koch</a> <a href="#f6d7fc95670240a9a67f1ef9b96b6b51">#</a></div> <div class="comment-content"> <p> There are some alternatives way in which I would consider handling this if I'm being honest. We <i>always</i> want to retrieve the hint. We <i>singularly</i> want to reduce the person's score by 5 points if they have not seen this hint before. This depreciation in points is idempotent and should only be executed if the hint hasn't been viewed before. Contextual information associated to the returned hint, such as last time viewed by current user, would inform the triggering of the command. </p> <p>I think this is OK, because we care whether a user has viewed a hint. A hint having been viewed by a user means something, so returning it from the query feels valid. Acting up on this accordingly also feels valid, but the command itself becomes nicely idempotent as it understand the single-hit decrease in the points. </div> <div class="comment-date">2015-10-09 13:18 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applications and their side effects https://blog.ploeh.dk/2015/09/23/applications-and-their-side-effects 2015-09-23T09:30:00+00:00 Mark Seemann <div id="post"> <p> <em>All applications have side-effects, but you can isolate those side-effects at the boundaries.</em> </p> <p> In my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID Pluralsight course</a>, I introduce <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command-Query Separation</a> (CQS) as a fundamental tool that will help you think about encapsulation. (I didn't come up with this myself, but rather picked it up from Bertrand Meyer's <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a>.) </p> <p> Despite the age of the CQS principle, it's still news to many people, and I get lots of questions about it; I attempt to <a href="/2014/08/11/cqs-versus-server-generated-ids">answer them as well as I can</a>. Most questions are about specific situations where the inquirer can't see a way out of issuing a Query and at the same time producing a side-effect. </p> <p> Perhaps the most common situation comes up when auditing comes into play. In some domains, auditing is a legal requirement. Asking any question must produce the side-effect that an audit record is created. </p> <p> How can you reconcile such requirements with CQS? </p> <h3 id="c87531edea44447ea76f35e3feacc7a6"> Definition <a href="#c87531edea44447ea76f35e3feacc7a6" title="permalink">#</a> </h3> <p> It may be helpful to first take a step back and attempt to answer the question: <em>what's a side-effect, anyway?</em> </p> <p> Perhaps we can learn from Functional Programming (gee, who'd have thunk!?), because Functional Programming is all about taming side-effects. Apparently, <a href="https://en.wikipedia.org/wiki/Simon_Peyton_Jones">Simon Peyton-Jones</a> once said during an introduction to Haskell, that if your program has no side-effects, it cannot print a result, it cannot ask for input, it cannot interact with the network or the file system. All it does is heat up the CPU, after which someone from the audience interjected that heating up the CPU is also a side-effect, so, technically speaking, if you want your program to have no side-effects, you cannot even run it. (I only have this story on <a href="http://programmers.stackexchange.com/a/293889/19115">second hand</a>, but it makes an important point.) </p> <p> Clearly, there's no such thing as a truly side-effect free program, so how do we define what a side-effect is? </p> <p> In strictly Functional languages, a side-effect occurs whenever a function isn't <a href="https://en.wikipedia.org/wiki/Referential_transparency_(computer_science)">referentially transparent</a>. This fancy term means that you can replace a function call with its return value. If a function call isn't equivalent to its return value, it's either because the function call also has a side-effect, or because some side-effect caused the return value to change. </p> <p> This is closely related to the popular definition of a Query in CQS: <em>Asking the question mustn't change the answer.</em> This is, though, a weaker statement, because it allows a change in global state (e.g. another process updating a database record) to change the answer between two identical queries. </p> <p> In a completely different context, in <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> it's often helpful to distinguish between <em>safe</em> and <em>idempotent</em> requests. The term <em>safe</em> is closely related to a side-effect free Query. As <a href="http://amzn.to/18aNOla">REST in Practice</a> states (p. 38): "By safe, we mean a GET request generates no server-side effects <em>for which the client can be held responsible</em>" (my emphasis). That can often be a useful distinction when thinking about CQS. A Query may cause a side-effect to happen (such as an audit record being written), but that side-effect doesn't concern the client. </p> <h3 id="d026ad59b17e4ea3a34646570678b771"> Applications are never side-effect free <a href="#d026ad59b17e4ea3a34646570678b771" title="permalink">#</a> </h3> <p> All of the above is useful because there's a large set of side-effects we can ignore in practice. We can ignore that the CPU heats up. We can ignore that web servers log HTTP requests. We can (probably) ignore that audit records are written. Such side-effects don't change the answer of a Query. </p> <p> There may still be cases where you need to deal explicitly with side-effects. You may wish to acknowledge to a user that a file was written. You may want to return a receipt to a client that your service received a document. </p> <p> It's important to realise that at the application level, applications are all about side-effects. <ul> <li>Applications may have GUIs; every time the application updates the screen, that's a side-effect.</li> <li>An application may be a REST service; it handles HTTP, which is modelled on the <a href="https://en.wikipedia.org/wiki/Request%E2%80%93response">Request-Response pattern</a>. Even POST requests have responses. Everything in HTTP looks like Queries, because responses are return values.</li> <li>Applications may write to a database; clearly, side-effects are involved.</li> </ul> CQS, or referentially transparent functions, are principles we apply at the source code level to make the code easier to reason about and maintain. It's a question of separation of concerns: we separate pure computation from side-effects. </p> <p> Applications, on the other hand, are compositions of all relevant concerns. As I've previously described, <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">at the boundaries, applications aren't Object-Oriented</a>. Neither are they Functional. All applications must have a boundary where Encapsulation, or FP, or CQS, doesn't apply. The trick is to keep that boundary as thin as possible. </p> <p> Thus, if you <em>must</em> violate CQS, do it in the boundary of the application. As an example, perhaps you're creating a REST service that enables clients to create new resources with POST requests. As a response, you want to return the address of the new resource. That violates CQS, but if you keep that violation at the entry point of the request, you've isolated the violation to the boundary of the application. </p> <h3 id="8ceb76c2ce154590851066b4e6b46f48"> Summary <a href="#8ceb76c2ce154590851066b4e6b46f48" title="permalink">#</a> </h3> <p> It can be difficult to follow CQS, until you get your head around it, but it's always possible to apply it - except at the application boundary. </p> <p> How do we know that we can always apply CQS? We know this from Functional Programming. Strict Functional languages like <a href="https://www.haskell.org">Haskell</a> model <em>everything</em> as Queries (except at the boundaries), and Haskell is a Turing-complete language. If you can model everything as Queries, it should be clear that you can also separate Commands and Queries: if in doubt, eliminate the Command; FP shows how to do that. </p> <p> Even if you're working in an Object-Oriented language, learn some Functional Programming. It'll teach you how to apply CQS, which is a cornerstone of Encapsulation. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="b41e46ea148b40488a5c4b16eba0e9ed"> <div class="comment-author"><a href="http://architectureposts.wordpress.com">Mani</a> <a href="#b41e46ea148b40488a5c4b16eba0e9ed">#</a></div> <div class="comment-content">While I agree most part of this, I thought I would also point out that the CQS brings quite a bit of complications that doesn’t justify the benefit it brings. Hence CQS in my opinion should not be the default reference architecture for the entire application. If it has to be used, it must be with in a bounded context. Udi Dahan the early advocate of CRQS caution on its usage http://udidahan.com/2011/04/22/when-to-avoid-cqrs/ And more details from Martin Fowler http://martinfowler.com/bliki/CQRS.html </div> <div class="comment-date">2015-09-29 09:44 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Unit testing internals https://blog.ploeh.dk/2015/09/22/unit-testing-internals 2015-09-22T11:56:00+00:00 Mark Seemann <div id="post"> <p> <em>FAQ: How should you unit test internals? A: Through the public API.</em> </p> <p> This question seems to come up repeatedly: <em>I have some <a href="https://msdn.microsoft.com/en-us/library/7c5ka91b">internal</a> (<a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html">package-private</a> in Java) code. How do I unit test it?</em> </p> <p> The short answer is: you unit test it as you unit test all other code: through the public API of the <a href="http://xunitpatterns.com/SUT.html">System Under Test</a> (SUT). </p> <h3 id="d9e170a67c6f461ba693979a87c39ede"> Purpose of automated testing <a href="#d9e170a67c6f461ba693979a87c39ede" title="permalink">#</a> </h3> <p> Details can be interesting, but don't lose sight of the big picture. <em>Why</em> do you test your software with automated tests? </p> <p> Automated testing (as opposed to manual testing) only serves a single purpose: it prevents regressions. Some would say that it demonstrates that the software works correctly, but that's inaccurate. Automated tests can only demonstrate that the software works correctly if the tests are written correctly, but <a href="/2013/04/02/why-trust-tests">that's a different discussion</a>. </p> <p> Assuming that all automated tests are correct, then yes: automated tests also demonstrate that the software works, but it's still regression testing. The tests were written to demonstrate that the software worked correctly once. Running the tests repeatedly only demonstrates that it <em>still</em> works correctly. </p> <p> What does it mean that the software works correctly? When it comes to automated testing, the verification is only as good as the tests. If the tests are good, the verification is strong. If the tests are weak, the verification is weak. </p> <p> Consider the purpose of writing the software you're currently being paid to produce. Is the purpose of the software to pass all tests? Hardly. The purpose of the software is to solve some problem, to make it easier to perform some task, or (if you're writing games) to entertain. The tests are proxies of the actual purpose. </p> <p> It ought to be evident, then, that automated tests should be aligned with the purpose of the software. There's nothing new in this: <a href="http://dannorth.net/blog">Dan North</a> introduced <a href="https://en.wikipedia.org/wiki/Behavior-driven_development">Behaviour Driven Development</a> (BDD) in order to emphasise that testing should be done with the purpose of the software in mind. You should test the <em>behaviour</em> of the software, not its implementation. </p> <p> Various software can have different purposes. The software typically emphasised by BDD tends to be business software that solves a business problem. Often, these are full applications. Other types of software include reusable libraries. These exist to be reusable. Common to all is that they have a reason to exist: they have <em>externally visible</em> behaviour that some consumer cares about. </p> <p> If you want to test a piece of software to prevent regressions, you should make sure that you're testing the externally visible behaviour of the software. </p> <h3 id="2d2d2fdbe815471d9254f0d8260320bf"> Combinatorics <a href="#2d2d2fdbe815471d9254f0d8260320bf" title="permalink">#</a> </h3> <p> In an ideal world, then, all automated testing should be done against the public interface of the SUT. If the application is a web application, testing should be done against the HTML and JavaScript. If the application is a mobile app, testing should be done somehow by automating user interaction against its GUI. In reality, these approaches to testing tend to be brittle, so instead, you can resort to <a href="http://martinfowler.com/bliki/SubcutaneousTest.html">subcutaneous testing</a>. </p> <p> Even if you're developing a reusable library, or a command-line executable, if you're doing something even moderately complex, you run into another problem: a combinatorial explosion of possible paths through the code. As <a href="http://www.infoq.com/presentations/integration-tests-scam">J.B. Rainsberger explains much better than I can do</a>, if you combine software modules (e.g. validation, business logic, data access, authentication and authorisation, caching, logging, etc) you can easily have <em>tens of thousands</em> distinct paths through a particular part of your software - all via a single entry point. </p> <p> This isn't related to BDD, or business problems, or agile... It's a <em>mathematical</em> result. There's a reason the <a href="http://martinfowler.com/bliki/TestPyramid.html">Test Pyramid</a> looks like it does. </p> <p> When you combine a procedure with four distinct paths with another with five paths, the number of possible paths isn't (4 + 5 =) nine; it's (4 * 5 =) twenty. As you combine units together, you easily reach tens of thousands distinct paths through your software. (This is basic <a href="https://en.wikipedia.org/wiki/Combinatorics">combinatorics</a>). </p> <p> You aren't going to write tens of thousands of automated tests. </p> <p> In the ideal world, we would like to only test the behaviour of software against its public interface. In the real world, we have to separate any moderately complex software into modules, and instead test the behaviour of those modules in isolation. This prevents the combinatorial explosion. </p> <p> If you cut up your software in an appropriate manner, you'll get modules that are independent of each other. Many books and articles have been written about how to do this. You've probably heard about Layered Application Architecture, Hexagonal Architecture, or Ports and Adapters. The recent interest related to microservices is another (promising) attempt at factoring code into smaller units. </p> <p> You still need to care about the <em>behaviour</em> of those modules. Splitting up your software doesn't change the overall purpose of the software (whatever that is). </p> <h3 id="f718f41494a1434fb4b638f5a65f5c72"> Internals <a href="#f718f41494a1434fb4b638f5a65f5c72" title="permalink">#</a> </h3> <p> When I do code reviews, often the code is already factored into separate concerns, but internal classes are everywhere. What could be the reason for that? </p> <p> When I ask, answers always fall into one of two categories: <ul> <li>Members (or entire classes) are poorly encapsulated, so the developers don't want to expose these internals for fear of destabilising the system.</li> <li>Types or members are kept hidden in order to protect the code base from backwards compatibility issues.</li> </ul> The first issue is easy to deal with. Consider <a href="/2015/09/18/temporary-field-code-smell">a recent example</a>: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CalculateAverage() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.average&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">long</span>)<span style="color:blue;">this</span>.durations.Average(ts&nbsp;=&gt;&nbsp;ts.Ticks)); }</pre> </p> <p> This CalculateAverage method is marked private because it's unsafe to call it. <code>this.durations</code> can be null, in which case the method would throw an exception. It may <em>feel</em> like a solution to lock down the method with an access modifier, but really it only smells of poor encapsulation. Instead, <a href="/2015/09/18/temporary-field-code-smell">refactor the code to a proper, robust design</a>. </p> <p> There <em>are</em> valid use cases for the <code>private</code> and <code>internal</code> access modifiers, but the majority of the time I see private and internal code, it merely smells of poor design. If you change the design, you could make types and members public, <em>and</em> feel good about it. </p> <p> The other issue, concerns about compatibility, <a href="/2015/09/21/public-types-hidden-in-plain-sight">can be addressed as well</a>. In any case, for most developers, this is essentially a theoretical issue, because most code written isn't for public consumption anyway. If you also control all consumers of your API, you may not need to worry about compatibility. If you need to change the name of a class, just let your favourite refactoring tool do this for you, and all is still good. (I'd still <a href="/2012/12/18/ZookeepersmustbecomeRangers">recommend that you should adopt a Ranger approach to your Zoo software</a>, but that's a different topic.) </p> <p> The bottom line: you don't <em>have</em> to hide most of your code behind access modifiers. There are good alternatives that lead to better designs. </p> <h3 id="77a7651aff9f4f259bc7e2c024233469"> Testing internals <a href="#77a7651aff9f4f259bc7e2c024233469" title="permalink">#</a> </h3> <p> That was a long detour around reasons for testing, as well as software design in general. How, then, do you test internal code? </p> <p> Through the public API. Remember: the reason for testing is to verify that the <em>observable behaviour</em> of the SUT is correct. If a class or member is internal, it isn't visible; it's not <em>observable</em>. It may exist in order to support the public API, but if you explicitly chose to not make it visible, you also stated that it can't be directly observable. You can't have it both ways. (Yes, I know that .NET developers will point the <a href="https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute">[InternalsVisibleTo] attribute</a> out to me, but this attribute isn't a solution; it's part of the problem.) </p> <p> Why would you test something that has no 'official' existence? </p> <p> I think I know the answer to that question. It's because of the combinatorial explosion problem. You want the software to solve a particular problem, and to keep everything else 'below the surface'. Unfortunately, as the complexity of the software grows, you realise (explicitly or implicitly) that you can't cover the entire code base with high-level BDD-style tests. You want to unit test the internals. </p> <p> The problem with doing this is that it's just as brittle as testing through a GUI. Every time you change the internals, you'll have to change your tests. </p> <p> A well-designed system tends to be more stable when it comes to unit testing. A poorly designed system is often characterised by tests that are too coupled to implementation details. As soon as you decide to change something in such a system, you'll need to change the tests as well. </p> <p> Recall that automated testing is regression testing. The only information we get from running tests is whether or not existing tests passed or failed. We don't learn if the tests are correct. How do we know that tests are correct? Essentially, we know because we <a href="/2013/04/02/why-trust-tests">review them, see them fail, and never touch them again</a>. If you constantly have to fiddle with your tests, how do you know they still test the right behaviour? </p> <p> Design your sub-modules well, on the other hand, and test maintenance tends to be much lower. If you have well-designed sub-modules, though, you don't have to make all types internal. Make them public. They are part of your solution. </p> <h3 id="2491c7cb89ee443faa5783b9a6777be2"> Summary <a href="#2491c7cb89ee443faa5783b9a6777be2" title="permalink">#</a> </h3> <p> A system's public API should enable you to exercise and verify its observable behaviour. That's what you should care about, because that's the SUT's reason to exist. It may have internal types and members, but these are implementation details. They exist to support the system's observable behaviour, so test them through the public API. </p> <p> If you can't get good test coverage of the internal parts through the public API, then why do these internal parts exist? If they exhibit no observable behaviour, couldn't you delete them? </p> <p> If they do exist for a reason, but you somehow can't reach them through the public API, it's a design smell. Address that smell instead of trying to test internals. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="dcf4fefda9be47a082d39039a68fbc8b"> <div class="comment-author"><a href="http://feo2x.azurewebsites.net/">Kenny Pflug</a> <a href="#dcf4fefda9be47a082d39039a68fbc8b">#</a></div> <div class="comment-content"> <p>Dear Mark,</p> <p> Thank you for this excellent article. I got one question: in the "Internals" section, you state that there are valid use cases for the internal access modifier - can you name some of them for me? </p> <p> I'm also a proponent of keeping nearly all types public in reusable code bases even if most of them are not considered to be part of the API that a client usually consumes - therefore I would only think about marking a class internal if it cannot protect it's invariants properly and fail fast when it's used in the wrong way. But you also mentioned that in this article, too. </p> <p> When I did a Google search on the topic, I couldn't find any useful answers either. The best one is probably from <a href="http://stackoverflow.com/a/3016168/1560623">Eric Lippert on a Stack Overflow question</a>, stating that big important classes that are hard to verify in the development process should be marked internal. But one can easily counter that by not designing code in such a way. </p> <p> Anyways, it would be very kind of you if you could provide some beneficial use cases for the use of internal. </p> <p> Sincerely,<br>Kenny </p> </div> <div class="comment-date">2015-10-03 08:14 UTC</div> </div> <div class="comment" id="e7539a17d13c410795d6c71a535252a2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e7539a17d13c410795d6c71a535252a2">#</a></div> <div class="comment-content"> <p> Kenny, thank you for writing. The answer given by Eric Lippert does, in my opinion, still paint an appropriate picture. There's always a cost to making types or members public, and I don't always wish to pay that cost. The point that I'm trying to make in the present article is that while this cost exists, it's not so high that it justifies unit testing internals via mechanisms like Reflections or [InternalsVisibleTo]. The cost of doing that is higher than making types or members public. </p> <p> Still, there are many cases where it's possible to cover internal or private code though a public API. Sometimes, I may be in doubt that the way I've modelled the internal code is the best way to do so, and then I'd rather avoid the cost of making it public. After all, making code public means that you're making a promise that it'll be around without breaking changes for a long time. </p> <p> Much of my code has plenty of private or internal code. An example is <a href="https://github.com/ploeh/Hyprlinkr">Hyprlinkr</a>, which has <a href="https://github.com/ploeh/Hyprlinkr/blob/6d3d95aef5d47587845460fc0f3e71314c204bd5/Hyprlinkr/RouteLinker.cs#L345-L437">private helper methods for one of its central features</a>. These private helpers only exist in order to make the feature implementation more readable, but are not meant for public consumption. They're all covered by tests that exercise the various public members of the class. </p> <p> Likewise, you can consider the private DurationStatistics class from my <a href="/2015/09/18/temporary-field-code-smell">Temporary Field code smell article</a>. At first, you may want to keep this class private, because you're not sure that it's stable (i.e. that it isn't going to change). Later, you may realise that you'll need that code in other parts of your code base, so you move it to an internal class. If you're still not convinced that it's stable, you may feel better keeping it internal rather than making it public. Ultimately, you <em>may</em> realise that it is, indeed, stable, in which case you may decide to make it public - or you may decide that no one has had the need for it, so it doesn't really matter, after all. </p> <p> My goal with this article wasn't to advise against internal code, but only to advise against trying to <em>directly call</em> such code for unit testing purposes. In my experience, when people ask how to unit test internal code, it's a problem they have because they haven't used Test-Driven Development (TDD). If you do TDD, you'll have sufficient coverage, and then it doesn't matter how you choose to organise the internals of your code base. </p> </div> <div class="comment-date">2015-10-03 11:45 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Public types hidden in plain sight https://blog.ploeh.dk/2015/09/21/public-types-hidden-in-plain-sight 2015-09-21T14:12:00+00:00 Mark Seemann <div id="post"> <p> <em>Instead of making types internal to a package, you can hide them in plain sight as public types.</em> </p> <p> When I review object-oriented code, I often see lots of <a href="https://msdn.microsoft.com/en-us/library/7c5ka91b">internal</a> (<a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html">package-private</a> in Java) classes and members. Sometimes, most of a code base is written at that access level. </p> <p> When I ask for the reasoning behind this, the answer is invariably: <em>encapsulation</em>. </p> <p> Encapsulation is one of the most misunderstood concepts in programming. If you think it's about making everything inaccessible, then there's only one logical conclusion: all your code must have this API: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Program</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Everything&nbsp;else&nbsp;goes&nbsp;here!</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If you've seen my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Pluralsight course about encapsulation</a>, you'll know that I prefer Bertrand Meyer's view, as outlined in <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a>. Essentially, it's about <em>design by contract</em> (pre- and post-conditions). </p> <p> Once I start to ask more pointed questions about enthusiastic use of internal-only access, it turns out that often, the underlying reason is that the developers want to be able to change the design of their internal types and members, without breaking existing clients. A noble goal. </p> <p> Imagine that such a code base has existing clients. While the maintainers can't change the public types without breaking these clients, they can change all internal types as much as they want. </p> <p> Unfortunately, there are disadvantages to this design strategy. You can't easily unit test such code bases. There are ways, but none of them are good. </p> <p> What if, instead of making types internal, you made them public? Are there ways to prevent clients from relying on such types? Yes, plenty of ways. </p> <h3 id="f244343b96d84cd881146f0097fc18fd"> Public, append-only <a href="#f244343b96d84cd881146f0097fc18fd" title="permalink">#</a> </h3> <p> My preferred approach to this problem is to <em>make types public anyway</em>. If my overall feeling about a type is that "it seems okay", I make it public, even if I know that there's a risk involved with this. </p> <p> Consider, as an example, the private DurationStatistics class I extracted in <a href="/2015/09/18/temporary-field-code-smell">a recent article</a>. In that article, I kept the class private, because I wanted to discuss visibility in a separate article. </p> <p> You can easily promote the DurationStatistics class to a public class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DurationStatistics</span> </pre> </p> <p> Here you only see the declaration of the class, because the implementation is exactly the same as in <a href="/2015/09/18/temporary-field-code-smell">the other article</a>. The code is moved out of the Estimator class and put into its own file, though. </p> <p> How can you be sure that DurationStatistics is 'done'? How can you be sure that you're not going to need to change it later? </p> <p> You can't. </p> <p> You can, however, deal with that when it occurs. You can still add new members to the class, but you can't remove members or change the type's name. </p> <p> If you need to <em>change</em> something (thereby breaking compatibility), then <em>don't</em> change it. Instead, <em>add</em> the new type or member, but leave the old artefact in place and <em>deprecate it</em>. In .NET, you can do this by adorning the member or type with the <a href="https://msdn.microsoft.com/en-us/library/system.obsoleteattribute">[Obsolete] attribute</a>. You can even add a message that points clients to the new, preferred way of doing things: </p> <p> <pre>[<span style="color:#2b91af;">Obsolete</span>(<span style="color:#a31515;">&quot;DurationStatistics&nbsp;is&nbsp;being&nbsp;retired&nbsp;(our&nbsp;fault).&nbsp;Use&nbsp;Foo&nbsp;instead.&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DurationStatistics</span></pre> </p> <p> This will cause a compiler warning in all client code using the DurationStatistics class. You should <a href="/2014/12/23/exception-messages-are-for-programmers">make the message as helpful and friendly</a> as possible. </p> <p> This may leave your code with compiler warnings. This is good, because you should work to remove these warnings from your own code base. The only place you should leave them in place is in your unit tests. As long as you have a deprecated type in your published code base, you should also keep it covered by unit tests. This will cause compiler warnings in your unit tests, but you can suppress such warnings explicitly there: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;1,&nbsp;1,&nbsp;1&nbsp;},&nbsp;1)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;2,&nbsp;3,&nbsp;4&nbsp;},&nbsp;3)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AverageIsCorrect(<span style="color:blue;">int</span>[]&nbsp;seconds,&nbsp;<span style="color:blue;">int</span>&nbsp;expectedSeconds) { <span style="color:blue;">#pragma</span>&nbsp;<span style="color:blue;">warning</span>&nbsp;<span style="color:blue;">disable</span>&nbsp;618 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DurationStatistics</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seconds.Select(s&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromSeconds(s)).ToArray()); <span style="color:blue;">#pragma</span>&nbsp;<span style="color:blue;">warning</span>&nbsp;<span style="color:blue;">restore</span>&nbsp;618 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Average; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expectedSeconds,&nbsp;actual.TotalSeconds); }</pre> </p> <p> Keep the 'disable scope' as small as possible, and always remember to restore the warning after disabling it. </p> <p> Perhaps you think that this approach of just making everything public and dealing with the consequences later is an unprofessional, unhygienic, and ugly way to evolve code, but it's really <a href="/2012/01/03/SOLIDisAppend-only">the logical way to produce and publish SOLID code</a>. </p> <h3 id="f902487f0f824e65a169d478f9f08e10"> Obsolete by default <a href="#f902487f0f824e65a169d478f9f08e10" title="permalink">#</a> </h3> <p> A variant of the above technique is that instead of making a type or member internal, you can deprecate it right away. This will immediately warn clients that they're doing something they aren't supposed to do. </p> <p> If you ever decide to 'promote' that type to a bona fide member of your API, you can simply remove the [Obsolete] attribute. </p> <h3 id="4a8b8ab9fbca42c998b38eb188d36c86"> Separate namespaces <a href="#4a8b8ab9fbca42c998b38eb188d36c86" title="permalink">#</a> </h3> <p> You can also hide public types in separate namespaces. If you don't document the types in those 'hidden' namespaces, clients will again have to do something explicit to use them. </p> <p> This is a softer way of hiding public types in plain sight, because clients get no compiler warnings if they use those 'hidden' types. Still, it can be quite effective. </p> <p> My experience with maintaining public software (e.g. the <a href="/2009/03/22/AnnouncingAutoFixture">now-more-than-six-year-old</a> <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> project) is that the most common problem is that users don't even find the public types I <em>want</em> them to find! If you knew how many times I've seen people reinvent a feature already available in the API... And that's even when all the interesting stuff is in the same namespace. </p> <p> Putting types in separate namespaces is, in my experience, quite an effective way of hiding them. </p> <h3 id="0e541012f886449392418c6f4de87232"> Separate libraries <a href="#0e541012f886449392418c6f4de87232" title="permalink">#</a> </h3> <p> Ultimately, you can put your volatile types in a separate library. In the Estimation example, you can ship the Estimator class (your 'real' public API) in one library, but put DurationStatistics in another library: </p> <p> <img src="/content/binary/estimation-statistics-in-another-library.png" alt="The Estimator class is in the Estimation library, whereas the DurationStatistics are in another library named Estimation.Statistics."> </p> <p> The Estimation library references the Estimation.Statistics library, so it can use all the public types in that library. You can unit test the public types in the Estimation library, but you can also unit test the public types in the Estimation.Statistics library. </p> <p> When you publish your API, you give clients a reference to Estimation, but <em>not</em> to Estimation.Statistics. You still ship Estimation.Statistics as a dependency of your API, but clients shouldn't reference it. </p> <p> Specifically, if you wish to publish your API as a NuGet package, you can use the <a href="https://docs.nuget.org/create/nuspec-reference"><code>&lt;references&gt;</code> element</a> to ensure that only the 'official' library is added to a project: </p> <p> <img src="/content/binary/estimation-library-is-referenced-but-statistics-is-not.png" alt="The library Ploeh.Samples.Estimation is referenced, but Ploeh.Samples.Estimation.Statistics isn't."> </p> <p> In this library, I installed the (local) <em>Estimation</em> NuGet package, and as you can see, only Ploeh.Samples.Estimation is referenced, whereas Ploeh.Samples.Estimation.Statistics isn't. This means that the client can easily use the official API, e.g. to create a new instance of the Estimator class: </p> <p> <pre><span style="color:blue;">this</span>.estimator&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Estimator</span>(<span style="color:#2b91af;">TimeSpan</span>.FromMinutes(1)); </pre> </p> <p> On the other hand, if a client developer attempts to use the public DurationStatistics class, that's not possible: </p> <p> <img src="/content/binary/duration-statistics-not-available.png" alt="The DurationStatistics class isn't available, and Visual Studio can't even suggest pulling in the appropriate reference."> </p> <p> Not only is DurationStatistics not available, all Visual Studio can suggest is to create it; it can't suggest pulling in the appropriate assembly reference, which is a good thing. </p> <p> The library is still there, it's just not referenced, so Estimator still works at run-time. </p> <p> The trick to set up a NuGet package in this way is to use the <code>&lt;references&gt;</code> element in the .nuspec file: </p> <p> <pre><span style="color:blue;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;">&nbsp;</span><span style="color:red;">version</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">1.0</span>&quot;<span style="color:blue;">?&gt;</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">package</span><span style="color:blue;">&nbsp;&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">metadata</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">id</span><span style="color:blue;">&gt;</span>Estimation<span style="color:blue;">&lt;/</span><span style="color:#a31515;">id</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">version</span><span style="color:blue;">&gt;</span>1.0.0<span style="color:blue;">&lt;/</span><span style="color:#a31515;">version</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">authors</span><span style="color:blue;">&gt;</span>Mark&nbsp;Seemann<span style="color:blue;">&lt;/</span><span style="color:#a31515;">authors</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">owners</span><span style="color:blue;">&gt;</span>Mark&nbsp;Seemann<span style="color:blue;">&lt;/</span><span style="color:#a31515;">owners</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">requireLicenseAcceptance</span><span style="color:blue;">&gt;</span>false<span style="color:blue;">&lt;/</span><span style="color:#a31515;">requireLicenseAcceptance</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">description</span><span style="color:blue;">&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;is&nbsp;an&nbsp;example&nbsp;that&nbsp;demonstrates&nbsp;how&nbsp;dependent&nbsp;libraries&nbsp;can &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hide&nbsp;public&nbsp;types&nbsp;in&nbsp;plain&nbsp;sight. &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">&lt;/</span><span style="color:#a31515;">description</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">copyright</span><span style="color:blue;">&gt;</span>Copyright&nbsp;Mark&nbsp;Seemann&nbsp;2015<span style="color:blue;">&lt;/</span><span style="color:#a31515;">copyright</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">references</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">reference</span><span style="color:blue;">&nbsp;</span><span style="color:red;">file</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">Ploeh.Samples.Estimation.dll</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">references</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">metadata</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">files</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">file</span><span style="color:blue;">&nbsp;</span><span style="color:red;">src</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">..\Estimation\bin\Debug\Ploeh.Samples.Estimation.dll</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">target</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">lib\net45</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">file</span><span style="color:blue;">&nbsp;</span><span style="color:red;">src</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">..\Estimation\bin\Debug\Ploeh.Samples.Estimation.Statistics.dll</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">target</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">lib\net45</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;/</span><span style="color:#a31515;">files</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">package</span><span style="color:blue;">&gt;</span></pre> </p> <p> Notice that both Ploeh.Samples.Estimation.dll and Ploeh.Samples.Estimation.Statistics.dll are included in the NuGet package, but that only Ploeh.Samples.Estimation.dll should be referenced when the NuGet package is added. </p> <p> For good measure I should point out that in order to create these demo files, I only installed the <em>Estimation</em> NuGet package from my local disk (using the -Source switch), so don't go and look for it on <a href="https://www.nuget.org">nuget.org</a>. </p> <h3 id="d4447075109a44c3ae2ac1ed9d53c21d"> Summary <a href="#d4447075109a44c3ae2ac1ed9d53c21d" title="permalink">#</a> </h3> <p> There are plenty of ways to hide public types in plain sight. Often, one or more of these options are better than misusing the internal/package-private access modifier. This doesn't mean that you must never make a class internal, but it means that you should consider the advantages and disadvantages of all options before making such a decision. </p> <p> The main disadvantage of using the <em>internal</em> access modifier is that you can't easily unit test such a class. The options provided here should give you plenty of alternatives that will enable you to easily unit test classes while still hiding them from client developers. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f8fb39c78b5d4330a77628416b9dc182"> <div class="comment-author">Børge Nordli <a href="#f8fb39c78b5d4330a77628416b9dc182">#</a></div> <div class="comment-content"> Your main argument is the non-testability of internal classes. What is wrong with the InternalsVisibleTo assembly attribute? Isn't this one of the main use cases for this attribute? </div> <div class="comment-date">2015-08-21 17:05 UTC</div> </div> <div class="comment" id="2705121a5ffe4848ab7864e6badc254a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2705121a5ffe4848ab7864e6badc254a">#</a></div> <div class="comment-content"> <p> The problem with the [InternalsVisibleTo] attribute is exactly that it enables you to unit test internals, <a href="/2015/09/22/unit-testing-internals">which you shouldn't</a>. </p> </div> <div class="comment-date">2015-09-22 12:24 UTC</div> </div> <div class="comment" id="8ddc31c766e44f68bda168f484c801ae"> <div class="comment-author"><a href="http://feo2x.azurewebsites.net/">Kenny Pflug</a> <a href="#8ddc31c766e44f68bda168f484c801ae">#</a></div> <div class="comment-content"> <p>Dear Mark,</p> <p> Thanks for another great article. I totally agree with your point of view, but I can also see why Børge might be somewhat unsatisfied with the answer to not use the InternalsVisibleTo attribute in the context of automated testing. </p> <p> I think that using internal to hide certain types of a reusable code base <a href="http://feo2x.azurewebsites.net/posts/2015-09-27-avoid-the-internal-modifier-by-default">is a violation of the Open/Closed Principle</a>, because there is no easy way to extend the internal types without touching the source code (you can use reflection to call internal methods, but that's annoying, and you simply cannot derive from an internal class or implement an internal interface.) </p> <p> What are your thoughts on this argument? </p> <p> With regards,<br>Kenny </p> </div> <div class="comment-date">2015-10-03 04:37 UTC</div> </div> <div class="comment" id="e28b631e1ee743ea95101c44aee3708a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e28b631e1ee743ea95101c44aee3708a">#</a></div> <div class="comment-content"> <p> Kenny, thank you for writing. Overall, I like that additional argument. In order to keep this discussion balanced, I think it's worth pointing out that your argument certainly applies to <em>reusable</em> software, but that lots of software isn't (or rather, shouldn't be) reusable. Udi Dahan has pointed out that <a href="http://udidahan.com/2009/06/07/the-fallacy-of-reuse/">reuse may be overrated, or even have negative consequences</a>, and while I've been <a href="/2011/11/10/TDDimprovesreusability">disagreeing with certain parts of that claim</a>, in general I think there's much wisdom in that observation. </p> <p> Some software is written to be reusable, but much software shouldn't be. This is the sort of software I call <a href="/2012/12/18/RangersandZookeepers">Zoo software</a>. Your argument about violation of the OCP certainly applies to Wildlife Software, but only <a href="/2012/12/18/ZookeepersmustbecomeRangers">indirectly</a> to Zoo software. </p> </div> <div class="comment-date">2015-10-03 09:30 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Temporary Field code smell https://blog.ploeh.dk/2015/09/18/temporary-field-code-smell 2015-09-18T11:52:00+00:00 Mark Seemann <div id="post"> <p> <em>Temporary Field is a well-known code smell. Here's an example and remedy.</em> </p> <p> The <em>Temporary Field</em> code smell was described more than a decade ago, but I keep encountering it when doing code reviews. Despite its vintage, I couldn't find a good example, so I decided to provide one. </p> <p> The code smell is described in <a href="http://amzn.to/YPdQDf">Refactoring</a>: </p> <blockquote> <p> "Sometimes you see an object in which an instance variable is set only in certain circumstances. Such code is difficult to understand, because you expect an object to need all of its variables. [...] </p> <p> "A common case of temporary field occurs when a complicated algorithm needs several variables. Because the programmer didn't want to pass around a huge parameter list (who does?), he put them in fields." </p> <p> - <em><a href="http://amzn.to/YPdQDf">Refactoring</a></em>, Martin Fowler et al., Addison-Wesley 1999. p. 84 </p> </blockquote> <p> Unfortunately, <em>Refactoring</em> doesn't provide an example, and I couldn't find a good, self-contained example on the web either. </p> <h3 id="bd3e3620f2eb4cd0825328e51c9f3abb"> Example: estimate a duration <a href="#bd3e3620f2eb4cd0825328e51c9f3abb" title="permalink">#</a> </h3> <p> In this example, a developer was asked to provide an estimate of a duration, based on a collection of previously observed durations. The requirements are these: <ul> <li>There's a collection of previously observed durations. These must be used as statistics upon which to base the estimate.</li> <li>It's better to estimate too high than too low.</li> <li>Durations are assumed to be <a href="https://en.wikipedia.org/wiki/Normal_distribution">normal distributed</a>.</li> <li>The estimate should be higher than the actual duration in more than 99% of the times.</li> <li>If there are no previous observations, a default estimate must be used as a fall-back mechanism.</li> </ul> Does this sound like a <em>complicated algorithm?</em> </p> <p> It's not that bad, actually. Because the distribution is assumed to be normal, you can find a good estimate by calculating the average, and add three times the <a href="https://en.wikipedia.org/wiki/Standard_deviation">standard deviation</a>. </p> <p> Here's how our developer attempted to solve the problem: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Estimator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;average; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;standardDeviation; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Estimator(<span style="color:#2b91af;">TimeSpan</span>&nbsp;defaultEstimate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.defaultEstimate&nbsp;=&nbsp;defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;CalculateEstimate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(durations)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations.Count&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.durations&nbsp;=&nbsp;durations; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.CalculateAverage(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.CalculateStandardDeviation(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;margin&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks(<span style="color:blue;">this</span>.standardDeviation.Ticks&nbsp;*&nbsp;3); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.average&nbsp;+&nbsp;margin; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CalculateAverage() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.average&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">long</span>)<span style="color:blue;">this</span>.durations.Average(ts&nbsp;=&gt;&nbsp;ts.Ticks)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CalculateStandardDeviation() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;variance&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.durations.Average(ts&nbsp;=&gt;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Math</span>.Pow( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ts&nbsp;-&nbsp;<span style="color:blue;">this</span>.average).Ticks, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.standardDeviation&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks((<span style="color:blue;">long</span>)<span style="color:#2b91af;">Math</span>.Sqrt(variance)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The CalculateEstimate method directly uses the temporary field <code>durations</code>, as well as implicitly the fields <code>average</code> and <code>standardDeviation</code>. The reason for this is to avoid passing parameters around. Both the average and the standard deviation depend on the <code>durations</code>, but the standard deviation also depends on the average. </p> <p> These dependencies are difficult to see in this code snippet: </p> <p> <pre><span style="color:blue;">this</span>.durations&nbsp;=&nbsp;durations; <span style="color:blue;">this</span>.CalculateAverage(); <span style="color:blue;">this</span>.CalculateStandardDeviation();</pre> </p> <p> What if I wanted to switch the order around? </p> <p> <pre><span style="color:blue;">this</span>.durations&nbsp;=&nbsp;durations; <span style="color:blue;">this</span>.CalculateStandardDeviation(); <span style="color:blue;">this</span>.CalculateAverage();</pre> </p> <p> <em>This compiles!</em> It also produces a result if you invoke the CalculateEstimate method. No exception is thrown, but the result returned is incorrect! </p> <p> Not only is this code difficult to understand, it's also brittle. Furthermore, it's not thread-safe. </p> <p> This was most likely done with the best of intentions. After all, in <a href="http://amzn.to/XCJi9X">Clean Code</a> you learn that methods with zero arguments are better than methods with one argument (which are better than methods with two arguments, and so on). </p> <p> There are better ways to factor the code, though. </p> <h3 id="6bfaadf3bcbf4d2988ddb8e4ae1010df"> Extract Class <a href="#6bfaadf3bcbf4d2988ddb8e4ae1010df" title="permalink">#</a> </h3> <p> In <em>Refactoring</em>, the suggested cure is to extract a class that contains only the temporary fields. This refactoring is called <em>Extract Class</em>. Your first step can be to introduce a private, nested class within the containing class. In the case of the example, you might call this class <em>DurationStatistics</em>. It can be used from CalculateEstimate in this way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;CalculateEstimate( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(durations)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations.Count&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;stats&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DurationStatistics</span>(durations); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;margin&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks(stats.StandardDeviation.Ticks&nbsp;*&nbsp;3); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;stats.Average&nbsp;+&nbsp;margin; }</pre> </p> <p> It's now much clearer what's going on. You have some statistics based on the <code>durations</code>, and those contain both the average and the standard deviation. The flow of data is also clear: you need the <code>stats</code> to get the average and the standard deviation, and you need <code>stats.StandardDeviation</code> in order to calculate the <code>margin</code>, and the <code>margin</code> before you can calculate the return value. If you attempt to move those lines of code around, it's no longer going to compile. </p> <p> This solution is also thread-safe. </p> <p> Here's the full code of the Estimator class, after the Extract Class refactoring: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Estimator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Estimator(<span style="color:#2b91af;">TimeSpan</span>&nbsp;defaultEstimate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.defaultEstimate&nbsp;=&nbsp;defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;CalculateEstimate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(durations)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations.Count&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;stats&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DurationStatistics</span>(durations); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;margin&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks(stats.StandardDeviation.Ticks&nbsp;*&nbsp;3); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;stats.Average&nbsp;+&nbsp;margin; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">DurationStatistics</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;average; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;standardDeviation; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;DurationStatistics(<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(durations)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations.Count&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Empty&nbsp;collection&nbsp;not&nbsp;allowed.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">nameof</span>(durations)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.durations&nbsp;=&nbsp;durations; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.average&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;(<span style="color:blue;">this</span>.CalculateAverage); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.standardDeviation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;(<span style="color:blue;">this</span>.CalculateStandardDeviation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;Average &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.average.Value;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;StandardDeviation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.standardDeviation.Value;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;CalculateAverage() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">long</span>)<span style="color:blue;">this</span>.durations.Average(ts&nbsp;=&gt;&nbsp;ts.Ticks)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;CalculateStandardDeviation() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;variance&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.durations.Average(ts&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Math</span>.Pow( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ts&nbsp;-&nbsp;<span style="color:blue;">this</span>.average.Value).Ticks, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks((<span style="color:blue;">long</span>)<span style="color:#2b91af;">Math</span>.Sqrt(variance)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As you can see, the Estimator class now only has a single read-only field, which ensures that it isn't temporary. </p> <p> The original intent of not passing parameters around is still preserved, so this solution still adheres to that Clean Code principle. </p> <p> The DurationStatistics class lazily calculates the average and standard deviation, and memoizes their results (or rather, that's what Lazy&lt;T&gt; does). </p> <p> Instances of DurationStatistics have a shorter lifetime than the containing Estimator object. </p> <p> The DurationStatistics class is a private, nested class, but a next step might be to pull it out to a public class in its own right. You don't have to do this. Sometimes I leave such classes as private classes, because they only exist to organise the code better; their purpose and behaviour are still narrow in scope, and associated with the containing class. In other cases, such a refactoring may uncover a great way to model a particular type of problem. If that's the case, making it a public class could make the entire code base better, because you've now introduced a reusable concept. </p> <h3 id="7929ad6daed54f7581d4bdb0a7098662"> Summary <a href="#7929ad6daed54f7581d4bdb0a7098662" title="permalink">#</a> </h3> <p> Class fields are intended to be used by the <em>class</em>. They constitute the data part of <em>data with behaviour</em>. If you have temporary values, either pass them around as arguments, or extract a new class to contain them. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c636c27d107340fd9fcddbc8a08d846b"> <div class="comment-author"><a href="https://tomkerkhove.ghost.io/">Tom Kerkhove</a> <a href="#c636c27d107340fd9fcddbc8a08d846b">#</a></div> <div class="comment-content">The refactored example is better because we are no longer sharing the fields across methods but wrapping it in a extraced class. Is this a good way of summarizing the changes?<br>How about passing everything as parameters and returning the calculated value as result, would this be considered dirty code?<br>Just trying to get the most out of this example.</div> <div class="comment-date">2015-09-25 22:20 UTC</div> </div> <div class="comment" id="1a01d6e60b244962850f6da972d5277d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1a01d6e60b244962850f6da972d5277d">#</a></div> <div class="comment-content"> <p> Tom, thank you for writing. Great observation about passing arguments around instead! In this case, passing the necessary arguments around would also be a good solution. It would look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Estimator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Estimator(<span style="color:#2b91af;">TimeSpan</span>&nbsp;defaultEstimate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.defaultEstimate&nbsp;=&nbsp;defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;CalculateEstimate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(durations)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(durations.Count&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.defaultEstimate; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;average&nbsp;=&nbsp;CalculateAverage(durations); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;standardDeviation&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CalculateStandardDeviation(durations,&nbsp;average); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;margin&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks(standardDeviation.Ticks&nbsp;*&nbsp;3); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;average&nbsp;+&nbsp;margin; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;CalculateAverage( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks((<span style="color:blue;">long</span>)durations.Average(ts&nbsp;=&gt;&nbsp;ts.Ticks)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;CalculateStandardDeviation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&gt;&nbsp;durations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;average) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;variance&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;durations.Average(ts&nbsp;=&gt;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Math</span>.Pow( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ts&nbsp;-&nbsp;average).Ticks, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks((<span style="color:blue;">long</span>)<span style="color:#2b91af;">Math</span>.Sqrt(variance)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This refactoring also eliminates the temporary fields, and is probably even easier to understand. </p> <p> To be quite honest, that's what I would have done with this particular example if it had been production code. The reason I wanted to show the <em>Extract Class</em> refactoring instead is that passing arguments doesn't scale well when you add more intermediate values. In this example, the maximum number of arguments you have to pass is two, which is easily within acceptable limits, but what if you have 15 intermediate values? </p> <p> Passing 15 method arguments around is well beyond most people's threshold, so instead they sometimes resort to temporary fields. The point here is that this isn't necessary, because you have the alternative of extracting classes. </p> <p> Why didn't I show an example with 15 intermediate values, then? Well, first, I couldn't think of a realistic example, and even if I could, it would be so complicated that no one would read the article :) </p> </div> <div class="comment-date">2015-09-26 07:31 UTC</div> </div> <div class="comment" id="98aea2dcda3d4eb79be91c8e117ae629"> <div class="comment-author"><a href="https://tomkerkhove.ghost.io/">Tom Kerkhove</a> <a href="#98aea2dcda3d4eb79be91c8e117ae629">#</a></div> <div class="comment-content"> <p> Sounds reasonable - Passing one or two around is okay but at a certain point an <em>Extract Class</em> would make more sense. </p> <p> No, it's a good example otherwise it would be too confusing as you mentioned. Interesting post, thanks! </p> </div> <div class="comment-date">2015-09-26 12:00 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Ad hoc Arbitraries with FsCheck.Xunit https://blog.ploeh.dk/2015/09/08/ad-hoc-arbitraries-with-fscheckxunit 2015-09-08T11:11:00+00:00 Mark Seemann <div id="post"> <p> <em>When using FsCheck with xUnit.net, you can define ad hoc Arbitraries in-line in your test functions.</em> </p> <p> Writing properties with <a href="http://fscheck.github.io/FsCheck">FsCheck</a> and using <a href="https://xunit.net">xUnit.net</a> as a test host is a nice combination. Properties are written as normal functions annotated with the Property attribute: </p> <p> <pre>[&lt;<span style="color:#2b91af;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;``findNeighbors&nbsp;returns&nbsp;8&nbsp;cells``&nbsp;(cell&nbsp;:&nbsp;<span style="color:#2b91af;">int</span>&nbsp;*&nbsp;<span style="color:#2b91af;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#2b91af;">Set</span>&lt;<span style="color:#2b91af;">int</span>&nbsp;*&nbsp;<span style="color:#2b91af;">int</span>&gt;&nbsp;=&nbsp;findNeighbors&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;=!&nbsp;actual.Count</pre> </p> <p> FsCheck takes care of generating values for the <code>cell</code> argument. This works well in many cases, but sometimes you need a bit more control over the range of values being generated by FsCheck. </p> <h3 id="0f2527f536a54c619263f0fbd7fe8069"> Motivating example <a href="#0f2527f536a54c619263f0fbd7fe8069" title="permalink">#</a> </h3> <p> You may already have guessed it from the above code snippet, but the example for this article is a property-based approach to <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's Game of Life</a>. One of the properties of the game is that <em>any live cell with more than three live neighbours dies</em>. </p> <p> This means that you have to write a property where one of the values is the number of live neighbours. That number must be a (randomly generated) number between 4 and 8 (including both ends), because the maximum number of neighbours in a cell grid is eight. How do you get FsCheck to give you a number between 4 and 8? </p> <h3 id="603e1ab5c6aa464d9b88b569516d6bfe"> Crude solution: conditional property <a href="#603e1ab5c6aa464d9b88b569516d6bfe" title="permalink">#</a> </h3> <p> There are various solutions to the problem of getting fine control over certain values generated by FsCheck, but the ones I first learned turn out to be problematic. </p> <p> One attempt is to use <em>conditional properties</em>, where you supply a boolean expression, and FsCheck will throw away all properties that fail the boolean test: </p> <p> <pre>[&lt;<span style="color:#2b91af;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;more&nbsp;than&nbsp;three&nbsp;live&nbsp;neighbors&nbsp;dies`` &nbsp;&nbsp;&nbsp;&nbsp;(cell&nbsp;:&nbsp;<span style="color:#2b91af;">int</span>&nbsp;*&nbsp;<span style="color:#2b91af;">int</span>) &nbsp;&nbsp;&nbsp;&nbsp;(neighborCount&nbsp;:&nbsp;<span style="color:#2b91af;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;(3&nbsp;&lt;&nbsp;neighborCount&nbsp;&amp;&amp;&nbsp;neighborCount&nbsp;&lt;=&nbsp;8)&nbsp;==&gt;&nbsp;<span style="color:blue;">lazy</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;neighborCells&nbsp;=&nbsp;findNeighbors&nbsp;cell&nbsp;|&gt;&nbsp;pickRandom&nbsp;neighborCount &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;calculateNextState&nbsp;(cell&nbsp;::&nbsp;neighborCells)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;Dead&nbsp;=!&nbsp;actual</pre> </p> <p> The <code>==></code> operator is an FsCheck-specific operator that indicates that FsCheck should disregard all properties where the input arguments don't satisfy the boolean condition on the left side. The use of <a href="https://msdn.microsoft.com/en-us/library/dd233247.aspx">lazy</a> ensures that FsCheck will only attempt to evaluate the property when the condition is true. </p> <p> That's simple and tidy, but unfortunately doesn't work. If you attempt to run this property, you'll get a result like this: </p> <p> <pre>Test 'Ploeh.Katas.GameOfLifeProperties.Any live cell with more than three live neighbors dies' failed: Arguments exhausted after 34 tests.</pre> </p> <p> The reason is that FsCheck generates random integers for <code>neighborCount</code>, and most of these values (statistically) fall outside of the range 4-8. After a (default) maximum of 1000 attempts, FsCheck gives up, because at that time, it's only managed to find 34 values that satisfy the condition, but it wants to find 100. </p> <p> What a disappointment. </p> <h3 id="1ec85929fe5b490e8d172a3e03875443"> Crude solution: custom Arbitrary <a href="#1ec85929fe5b490e8d172a3e03875443" title="permalink">#</a> </h3> <p> Another apparent solution is to define a custom Arbitrary for <a href="http://www.nuget.org/packages/FsCheck.Xunit">FsCheck.Xunit</a>. The mechanism is to define a static class with your custom rule, and register that with the Property attribute. The class must have a static method that returns an Arbitrary&lt;'a&gt;. </p> <p> In this particular example, you'll need to define a custom Arbitrary that only picks random numbers between 4 and 8. That's easy, but there's a catch: if you change the way <code>int</code> values are generated, you're also going to impact the generated <code>cell</code> values, because a cell here is an <code>int * int</code> tuple. </p> <p> Since you only need a <em>small</em> number, you can cheat and customize byte values instead: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">ByteBetween1and8</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">member</span>&nbsp;Byte&nbsp;()&nbsp;=&nbsp;<span style="color:#2b91af;">Gen</span>.elements&nbsp;[1uy&nbsp;..&nbsp;8uy]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Arb</span>.fromGen [&lt;<span style="color:#2b91af;">Property</span>(Arbitrary&nbsp;=&nbsp;[|&nbsp;typeof&lt;<span style="color:#2b91af;">ByteBetween1and8</span>&gt;&nbsp;|])&gt;] <span style="color:blue;">let</span>&nbsp;``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;more&nbsp;than&nbsp;three&nbsp;live&nbsp;neighbors&nbsp;dies`` &nbsp;&nbsp;&nbsp;&nbsp;(cell&nbsp;:&nbsp;<span style="color:#2b91af;">int</span>&nbsp;*&nbsp;<span style="color:#2b91af;">int</span>) &nbsp;&nbsp;&nbsp;&nbsp;(neighborCount&nbsp;:&nbsp;<span style="color:#2b91af;">byte</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;neighborCount&nbsp;&gt;&nbsp;3uy&nbsp;==&gt;&nbsp;<span style="color:blue;">lazy</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;neighborCells&nbsp;=&nbsp;findNeighbors&nbsp;cell&nbsp;|&gt;&nbsp;pickRandom&nbsp;(int&nbsp;neighborCount) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;calculateNextState&nbsp;(cell&nbsp;::&nbsp;neighborCells)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;Dead&nbsp;=!&nbsp;actual</pre> </p> <p> The ByteBetween1and8 type is a static class with a single static member that returns Arbitrary&lt;byte&gt;. By using the Arbitrary property of the Property attribute (!), you can register this custom Arbitrary with the Property. </p> <p> This 'solution' side-steps the issue by using a substitute data type instead of the desired data type. There are several problems with this: <ul> <li>You have to convert the byte value back to an integer in order to use it with the System Under Test: <code>int neighborCount</code>.</li> <li><code>3uy</code> is less readable than <code>3</code>.</li> <li>A newcomer will wonder why <code>neighborCount</code> is a byte instead of an int.</li> <li>You can't generalise this solution. It works because F# has more than one number type, but if you need to generate strings, you're out of luck: there's only one (normal) type of string in .NET.</li> </ul> Next to these issues with using bytes instead of ints, there's the larger problem that it's awkward to have to define an entire new type (ByteBetween1and8) for this purpose. Finally, this mechanism isn't type-safe. ByteBetween1and8 doesn't implement any interface. It doesn't have to, because the Arbitrary property on the Property attribute is just an array of Type instances. They can be any type, and your code will compile, but if one of those Types don't meet the requirements, an exception will be thrown at run-time. </p> <p> This way works, but is hardly elegant or safe. </p> <h3 id="3ba01a03bc3c453d8b12134a7b433aa2"> Ad hoc, in-line Arbitraries <a href="#3ba01a03bc3c453d8b12134a7b433aa2" title="permalink">#</a> </h3> <p> There's a safe way to define ad hoc, in-line Arbitraries with FsCheck.Xunit: </p> <p> <pre>[&lt;<span style="color:#2b91af;">Property</span>&gt;] <span style="color:blue;">let</span>&nbsp;``Any&nbsp;live&nbsp;cell&nbsp;with&nbsp;&gt;&nbsp;3&nbsp;live&nbsp;neighbors&nbsp;dies``&nbsp;(cell&nbsp;:&nbsp;<span style="color:#2b91af;">int</span>&nbsp;*&nbsp;<span style="color:#2b91af;">int</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;nc&nbsp;=&nbsp;<span style="color:#2b91af;">Gen</span>.elements&nbsp;[4..8]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Arb</span>.fromGen &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Prop</span>.forAll&nbsp;nc&nbsp;(<span style="color:blue;">fun</span>&nbsp;neighborCount&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;liveNeighbors&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;findNeighbors &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;shuffle &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.take&nbsp;neighborCount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;:&nbsp;<span style="color:#2b91af;">State</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculateNextState&nbsp;(cell&nbsp;::&nbsp;liveNeighbors&nbsp;|&gt;&nbsp;shuffle&nbsp;|&gt;&nbsp;set)&nbsp;cell &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dead&nbsp;=!&nbsp;actual)</pre> </p> <p> Using Prop.forAll enables you to execute your property with a custom Arbitrary, but mixed with the 'normally' generated values that the function receives via its arguments. In this example, <code>nc</code> is an Arbitrary&lt;int&gt;. Notice how it's explicitly used to populate the <code>neighborCount</code> value of the property, whereas the <code>cell</code> value arrives via normal means. </p> <p> This is type-safe, because <code>nc</code> is an Arbitrary&lt;int&gt;, which means that <code>neighborCount</code> is statically inferred to be an <code>int</code>. </p> <p> If you need more than a single ad hoc Arbitrary, you can always create Arbitraries for each of them, and then use Gen.map2, Gen.map3, and so on, to turn those individual Arbitraries into a single Arbitrary of a tuple. You can then use that tuple with Prop.forAll. </p> <h3 id="443fea5d012a4a2fac7ff3660b0cfe32"> Summary <a href="#443fea5d012a4a2fac7ff3660b0cfe32" title="permalink">#</a> </h3> <p> FsCheck is a well-designed library that you can combine in lots of interesting ways. In this article you learned how to use Prop.forAll to evaluate a property with a mix of normal, arbitrarily generated values, and an ad hoc, in-line Arbitrary. </p> <p> <strong>Addendum 2016-03-01:</strong> You can <a href="/2016/03/01/ad-hoc-arbitraries-now-with-pipes">write such properties slightly better using the backward pipe operator</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. When x, y, and z are great variable names https://blog.ploeh.dk/2015/08/17/when-x-y-and-z-are-great-variable-names 2015-08-17T07:43:00+00:00 Mark Seemann <div id="post"> <p> <em>A common complaint against Functional Programming is the terse naming: x and y for variables, and f for functions. There are good reasons behind these names, though. Learn to love them here.</em> </p> <p> One of the facets of Function Programming that bothered my when I first started to look into it, is that often in examples, variables and functions have terribly terse names like <code>x</code>, <code>y</code>, <code>f</code>, and so on. I wasn't alone, feeling like that, either: <blockquote> <a href="https://twitter.com/jamesiry/status/598547781515485184">"Functional programmer: (noun) One who names variables "x", names functions "f", and names code patterns "zygohistomorphic prepromorphism""</a> - <a href="https://twitter.com/jamesiry">James Iry</a> </blockquote> In this article, I'm not going to discuss zygohistomorphic prepromorphism, but I am going to discuss names like <code>x</code> and <code>f</code>. </p> <h3 id="aaf1705eea81450683208b7daf8c8c73"> Descriptive names <a href="#aaf1705eea81450683208b7daf8c8c73" title="permalink">#</a> </h3> <p> When I started my Functional Programming journey, I came from a <a href="https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)">SOLID Object-Oriented</a> background, and I had read and internalised <a href="http://amzn.to/XCJi9X">Clean Code</a> - or so I thought. </p> <p> Readable code should have <em>descriptive</em> names, and <code>f</code> and <code>x</code> hardly seem descriptive. </p> <p> For a while, I thought that the underlying reason for those 'poor' names was that the people writing all that Functional example code were academics with little practical experience in <em>software development</em>. <a href="https://twitter.com/danvanderboom/status/598548930926772224">It seems I'm not the only person who had that thought.</a> </p> <p> It may be true that Functional Programming has a root in mathematics, and that it has grown out of academia rather than industry, but there are good reasons that some names seem rather generic. </p> <h3 id="424259d76879476ca0cbd40ded707ad1"> Generics <a href="#424259d76879476ca0cbd40ded707ad1" title="permalink">#</a> </h3> <p> In statically typed Functional languages like F# or Haskell, you rarely declare the types of functions and arguments. Instead, types are inferred, based on usage or implementation. It often turns out that functions are more generic than you first thought when you started writing it. </p> <p> Here's a simple example. When I did the <a href="/2015/01/10/diamond-kata-with-fscheck">Diamond kata with Property-Based Testing</a>, I created <a href="https://github.com/ploeh/DiamondFsCheck/blob/59df27709025ea070decef068080ed4731d037b6/DiamondFsCheck/DiamondProperties.fs#L76-L79">this little helper function</a> along the way: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;isTwoIdenticalLetters&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasIdenticalLetters&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.distinct&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.length&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasTwoLetters&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.length&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;hasIdenticalLetters&nbsp;&amp;&amp;&nbsp;hasTwoLetters</pre> </p> <p> As the name of the function suggests, it tells us if <code>x</code> is a string of two identical letters. It returns true for strings such as "ff", "AA", and "11", but false for values like "ab", "aA", and "TTT". </p> <p> Okay, so there's already an <code>x</code> there, but this function works on any string, so what else should I have called it? In C#, I'd probably called it <code>text</code>, but that's at best negligibly better than <code>x</code>. </p> <p> Would you say that, based on the nice, descriptive name <em>isTwoIdenticalLetters</em>, you understand what the function does? </p> <p> That may not be the case. </p> <p> Consider the function's type: <code>seq&lt;'a&gt; -&gt;&nbsp;bool when 'a : equality</code>. What!? That's not what we expected! Where's the <code>string</code>? </p> <p> This function is more generic than I had in mind when I wrote it. System.String implements seq&lt;char&gt;, but this function can accept <em>any</em> seq&lt;'a&gt; (IEnumerable&lt;T&gt;), as long as the type argument <code>'a</code> supports equality comparison. </p> <p> So it turns out that <code>text</code> would have been a bad argument name after all. Perhaps <code>xs</code> would have been better than <code>x</code>, in order to indicate the plural nature of the argument, but that's about as much meaning as we can put into it. After all, this all works as well: </p> <p> <pre>> isTwoIdenticalLetters [1; 1];; val it : bool = true > isTwoIdenticalLetters [TimeSpan.FromMinutes 1.; TimeSpan.FromMinutes 1.];; val it : bool = true > isTwoIdenticalLetters [true; true; true];; val it : bool = false</pre> </p> <p> That function name is misleading, so you'd want to rename it: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;isTwoIdenticalElements&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasIdenticalLetters&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.distinct&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.length&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasTwoLetters&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.length&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;hasIdenticalLetters&nbsp;&amp;&amp;&nbsp;hasTwoLetters</pre> </p> <p> That's better, but now the names of the values <em>hasIdenticalLetters</em> and <em>hasTwoLetters</em> are misleading as well. Both are boolean values, but they're not particularly about letters. </p> <p> This may be more honest: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;isTwoIdenticalElements&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasIdenticalElements&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.distinct&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.length&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasTwoElements&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.length&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;hasIdenticalElements&nbsp;&amp;&amp;&nbsp;hasTwoElements</pre> </p> <p> This is better, but now I'm beginning to realize that I've been thinking too much about strings and letters, and not about the more <em>general</em> question this function apparently answers. A more straightforward (<a href="/2015/08/03/idiomatic-or-idiosyncratic">depending on your perspective</a>) implementation may be this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;isTwoIdenticalElements&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.truncate&nbsp;3&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[y;&nbsp;z]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;y&nbsp;=&nbsp;z &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span></pre> </p> <p> This may be slightly more efficient, because it doesn't have to traverse the sequence twice, but most importantly, I think it looks <a href="/2015/08/03/idiomatic-or-idiosyncratic">more idiomatic</a>. </p> <p> Notice the return of 'Functional' names like <code>y</code> and <code>z</code>. Although terse, these are <em>appropriate names</em>. Both <code>y</code> and <code>z</code> are values of the generic type argument <code>'a</code>. If not <code>y</code> and <code>z</code>, then what would you call them? <code>element1</code> and <code>element2</code>? How would those names be better? </p> <p> Because of F#'s strong type inference, you'll frequently experience that if you use as few type annotations as possible, the functions often turn out to be generic, both in the technical sense of the word, but also in the English interpretation of it. </p> <p> Likewise, when you create <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order functions</a>, functions passed in as arguments are often generic as well. Such a function could sometimes be <em>any</em> function that matches the required type, which means that <code>f</code> is often the most appropriate name for it. </p> <h3 id="e00cf3272fbf41a28a590d88a70bf3dd"> Scope <a href="#e00cf3272fbf41a28a590d88a70bf3dd" title="permalink">#</a> </h3> <p> Another problem I had with the Functional naming style when I started writing F# code was that names were often short. Having done Object-Oriented Programming for years, I'd learned that names should be sufficiently long to be descriptive. As <a href="http://amzn.to/1xioCmw">Code Complete</a> explains, <code>teamMemberCount</code> is better than <code>tmc</code>. </p> <p> Using that argument, you'd think that <code>element1</code> and <code>element2</code> are better names than <code>y</code> and <code>z</code>. Let's try: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;isTwoIdenticalElements&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.truncate&nbsp;3&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[element1;&nbsp;element2]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;element1&nbsp;=&nbsp;element2 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span></pre> </p> <p> At this point, the discussion becomes subjective, but I don't think this change is helpful. Quite contrary, these longer names only seem to add more noise to the code. Originally, the distance between where <code>y</code> and <code>z</code> are introduced and where they're used was only a few characters. In the case of <code>z</code>, that distance was 9 characters. After the rename, the distance between where <code>element2</code> is introduced and used is now 16 characters. </p> <p> There's nothing new about this. Remarkably, I can find support for my dislike of long names in small scopes in <a href="http://amzn.to/XCJi9X">Clean Code</a> (which isn't about Functional Programming at all). In the last chapter about smells and heuristics, Robert C. Martin has this to say about scope and naming: </p> <blockquote> <p> "The length of a name should be related to the length of the scope. You can use very short variable names for tiny scopes, but for big scopes you should use longer names. </p> <p> "Variable names like <code>i</code> and <code>j</code> are just fine if their scope is five lines long." </p> </blockquote> <p> Do you use variable names like <code>i</code> in <code>for</code> loops in C# or Java? I do, so I find it appropriate to also use short names in small functions in F# and Haskell. </p> <p> Well-factored Functional code consists of small, concise functions, just as <a href="/2011/06/07/SOLIDCodeisnt">well-factored SOLID code consists of small classes with clear responsibilities</a>. When functions are small, scopes are small, so it's only natural that we encounter many tersely named variables like <code>x</code>, <code>y</code>, and <code>f</code>. </p> <p> It's more readable that way. </p> <h3 id="a4ed9b5bed4d4a6a82dce551c9fca32e"> Summary <a href="#a4ed9b5bed4d4a6a82dce551c9fca32e" title="permalink">#</a> </h3> <p> There are at least two good reasons for naming values and functions with short names like <code>f</code>, <code>x</code>, and <code>y</code>. <ul> <li>Functions are sometimes so generic that we can't say anything more meaningful about such values.</li> <li>Scopes are small, so short names are more readable than long names.</li> </ul> I don't have a defence for <em>zygohistomorphic prepromorphism</em>, though. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="737df8b0827844c89a7c7453aade9581"> <div class="comment-author"><a href="http://taeguk.co.uk">Dave Shaw</a> <a href="#737df8b0827844c89a7c7453aade9581">#</a></div> <div class="comment-content"> This is something I've noticed whilst learning F# and I have to agree that as someone who has used Clean Code as a basis for most of my C# programming, the terse names are a bit unexpected at first. I don't have a problem when reading idoimatic F# code (like on fsharpforfunandprofit.com or this blog) with single letter names; it's writing F# code using short names I struggle with. I put this down to my inexperience with functional languanges and writing code that is clear and readable enough to get away with using a single letter where I would have normally used a number of camel cased words in C#. </div> <div class="comment-date">2015-08-20 20:44 UTC</div> </div> <div class="comment" id="18fd4009deaa45c3a437cd03ad46209e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#18fd4009deaa45c3a437cd03ad46209e">#</a></div> <div class="comment-content"> <p> Dave, thank you for writing. FWIW, I don't think there's anything wrong with longer camel-cased names when a function or a value is more explicit. As an example, I still kept the name of the example function fairly long and explicit: <code>isTwoIdenticalElements</code>. </p> <p> When I started with F#, I had many years of experience with writing C# code, and in the beginning, my F# code was more verbose than it is today. What I'm trying to explain with this article isn't that the short names are terse for the sake of being terse, but rather because <em>sometimes</em>, the functions and values are so generic that they could be anything. When that happens, <code>f</code> and <code>x</code> are good names. When functions and values are less generic, the names still ought to be more descriptive. </p> </div> <div class="comment-date">2015-08-21 8:23 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Type Driven Development: composition https://blog.ploeh.dk/2015/08/12/type-driven-development-composition 2015-08-12T07:24:00+00:00 Mark Seemann <div id="post"> <p> <em>When you develop a system with an outside-in technique like Type Driven Development, you'll eventually have all the required building blocks. Now you need to compose them into an application. This post shows an example.</em> </p> <p> In <a href="/2015/08/10/type-driven-development">my article about Type Driven Development</a>, I demonstrated how to approach a problem in an iterative fashion, using the F# type system to do outside-in development, and in a <a href="/2015/08/11/type-driven-development-implementing-shouldidle">follow-up article</a>, I showed you how to implement one of the inferred methods. In this article, you'll see how to compose all the resulting building blocks into an application. </p> <h3 id="590ad1dd77af44df96456b3c1e5f9ed3"> Building blocks <a href="#590ad1dd77af44df96456b3c1e5f9ed3" title="permalink">#</a> </h3> <p> In the <a href="/2015/08/10/type-driven-development">first article</a>, you learned that apart from the functions defined in that article itself, you'd need four other functions: <ul> <li>ReadyData&nbsp;-&gt;&nbsp;bool</li> <li>unit&nbsp;-&gt;&nbsp;Timed&lt;MessageHandler option&gt;</li> <li>NoMessageData&nbsp;-&gt;&nbsp;bool</li> <li>unit&nbsp;-&gt;&nbsp;Timed&lt;'a&gt;</li> </ul> In the <a href="/2015/08/11/type-driven-development-implementing-shouldidle">article about implementation</a>, you saw how to implement one of these functions: the shouldIdle function. </p> <p> As you can see in my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> Pluralsight course, some of the other implementations turn out to have function arguments themselves. It's not quite enough with only those four functions. Still, the final number of implementation functions is only 9. </p> <p> Here are all the building blocks (excluding the <a href="/2015/08/10/type-driven-development">types and functions related to Timed&lt;'a&gt;</a>): <ul> <li>run : (PollingConsumer -&gt;&nbsp;PollingConsumer) -&gt;&nbsp;PollingConsumer -&gt;&nbsp;PollingConsumer</li> <li>transition : (ReadyData -&gt;&nbsp;bool) -&gt;&nbsp;(unit -&gt;&nbsp;Timed&lt;MessageHandler option&gt;) -&gt;&nbsp;(NoMessageData -&gt;&nbsp;bool) -&gt;&nbsp;(unit -&gt;&nbsp;Timed&lt;'a&gt;) -&gt;&nbsp;PollingConsumer -&gt;&nbsp;PollingConsumer</li> <li>shouldIdle : TimeSpan -&gt;&nbsp;DateTimeOffset -&gt;&nbsp;NoMessageData -&gt;&nbsp;bool</li> <li>idle : TimeSpan -&gt;&nbsp;unit -&gt;&nbsp;Timed&lt;unit&gt;</li> <li>shouldPoll : (TimeSpan list -&gt;&nbsp;TimeSpan) -&gt;&nbsp;DateTimeOffset -&gt;&nbsp;ReadyData -&gt;&nbsp;bool</li> <li>poll : (unit -&gt;&nbsp;'a option) -&gt;&nbsp;('a -&gt;&nbsp;'b) -&gt;&nbsp;(unit -&gt;&nbsp;DateTimeOffset) -&gt;&nbsp;unit -&gt;&nbsp;Timed&lt;MessageHandler option&gt;</li> <li>calculateExpectedDuration : TimeSpan -&gt;&nbsp;TimeSpan list -&gt;&nbsp;TimeSpan</li> <li>simulatedPollForMessage : Random -&gt;&nbsp;unit -&gt;&nbsp;unit option</li> <li>simulatedHandle : Random -&gt;&nbsp;unit -&gt;&nbsp;unit</li> </ul> You may have noticed that some of these function names are prefixed with <em>simulated</em>. This is because I wrote some functions that only <em>simulate</em> that messages arrive and are handled. That's also the reason for the Random value here and there. </p> <h3 id="0a9dd0abc3a64af292336a59347de746"> Composition <a href="#0a9dd0abc3a64af292336a59347de746" title="permalink">#</a> </h3> <p> When the Polling Consumer starts, it can start by figuring out the current time, and calculate some derived values from that and some configuration values, <a href="/2015/08/11/type-driven-development-implementing-shouldidle">as previously explained</a>. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;now&#39;&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now <span style="color:blue;">let</span>&nbsp;stopBefore&#39;&nbsp;=&nbsp;now&#39;&nbsp;+&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromMinutes&nbsp;1. <span style="color:blue;">let</span>&nbsp;estimatedDuration&#39;&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromSeconds&nbsp;2. <span style="color:blue;">let</span>&nbsp;idleDuration&#39;&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromSeconds&nbsp;5.</pre> </p> <p> The <code>estimatedDuration'</code> value is a TimeSpan containing a (conservative) estimate of how long time it takes to handle a single message. It's only used if there are no already observed message handling durations, as the algorithm then has no statistics about the average execution time for each message. This value could come from a configuration system, or a command-line argument. <a href="/2014/09/25/faking-a-continuously-polling-consumer-with-scheduled-tasks">In a recent system, I just arbitrarily set it to 2 seconds</a>. </p> <p> Given these initial values, we can compose all other required functions: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;shouldPoll&#39;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;shouldPoll&nbsp;(calculateExpectedDuration&nbsp;estimatedDuration&#39;)&nbsp;stopBefore&#39; <span style="color:blue;">let</span>&nbsp;r&#39;&nbsp;=&nbsp;<span style="color:#2b91af;">Random</span>() <span style="color:blue;">let</span>&nbsp;handle&#39;&nbsp;=&nbsp;simulatedHandle&nbsp;r&#39; <span style="color:blue;">let</span>&nbsp;pollForMessage&#39;&nbsp;=&nbsp;simulatedPollForMessage&nbsp;r&#39; <span style="color:blue;">let</span>&nbsp;poll&#39;&nbsp;=&nbsp;poll&nbsp;pollForMessage&#39;&nbsp;handle&#39;&nbsp;<span style="color:#2b91af;">Clocks</span>.machineClock <span style="color:blue;">let</span>&nbsp;shouldIdle&#39;&nbsp;=&nbsp;shouldIdle&nbsp;idleDuration&#39;&nbsp;stopBefore&#39; <span style="color:blue;">let</span>&nbsp;idle&#39;&nbsp;=&nbsp;idle&nbsp;idleDuration&#39; <span style="color:blue;">let</span>&nbsp;transition&#39;&nbsp;=&nbsp;transition&nbsp;shouldPoll&#39;&nbsp;poll&#39;&nbsp;shouldIdle&#39;&nbsp;idle&#39; <span style="color:blue;">let</span>&nbsp;run&#39;&nbsp;=&nbsp;run&nbsp;transition&#39;</pre> </p> <p> The composed <code>run'</code> function has the type <code>PollingConsumer -&gt;&nbsp;PollingConsumer</code>. The input PollingConsumer value is the start state, and the function will return another PollingConsumer when it's done. Due to <a href="/2015/08/10/type-driven-development">the way the <code>run</code> function is implemented</a>, this return value is always going to be StoppedState. </p> <h3 id="c575c3450abc4ce2bed16a3e3eb6de47"> Execution <a href="#c575c3450abc4ce2bed16a3e3eb6de47" title="permalink">#</a> </h3> <p> All that's left to do is to execute the <code>run'</code> function with a ReadyState as the initial state: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;result&#39;&nbsp;=&nbsp;run&#39;(ReadyState([]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Timed</span>.capture&nbsp;<span style="color:#2b91af;">Clocks</span>.machineClock)) </pre> </p> <p> A ReadyState value is required as input, and the ReadyState case constructor takes a ReadyData value as input. ReadyData is an alias for Timed&lt;TimeSpan list&gt;. An the beginning, the Polling Consumer hasn't observed any messages, so the TimeSpan list should be empty. </p> <p> The empty <code>TimeSpan list</code> must be converted to <code>Timed&lt;TimeSpan list&gt;</code>, which can be done by piping it into <code>Timed.capture</code>, using the machine clock. </p> <p> When you execute the run' function, it may produce output like this: </p> <p> <pre>Polling Sleeping Polling Sleeping Polling Handling Polling Sleeping Polling Sleeping Polling Sleeping Polling Handling Polling Sleeping Polling Sleeping Polling Sleeping Polling Sleeping Polling Handling Polling Handling Polling Sleeping Real: 00:00:59.392, CPU: 00:00:00.031, GC gen0: 0, gen1: 0, gen2: 0</pre> </p> <p> This is because I set up some of the functions to print to the console so that we can see what's going on. </p> <p> Notice that the state machine ran for 59 seconds and 392 milliseconds, exiting just before the minute was up. </p> <h3 id="57f9f0b1dfcd4c00abc500d025fabae0"> Summary <a href="#57f9f0b1dfcd4c00abc500d025fabae0" title="permalink">#</a> </h3> <p> Once you have all the appropriate building blocks, you can compose your desired system and run it. Notice how much this resembles the <a href="/2011/07/28/CompositionRoot">Composition Root</a> pattern, only with functions instead of objects. </p> <p> If you want to see more details about this example, or get access to the full source code, you can watch my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> Pluralsight course. Please be aware that only certain subscription levels will give you source code access. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Type Driven Development: implementing shouldIdle https://blog.ploeh.dk/2015/08/11/type-driven-development-implementing-shouldidle 2015-08-11T11:04:00+00:00 Mark Seemann <div id="post"> <p> <em>Type Driven Development is an outside-in technique. Once you have the overall behaviour defined, you need to implement the details. Here's an example.</em> </p> <p> In <a href="/2015/08/10/type-driven-development">my article about Type Driven Development</a>, I demonstrated how to approach a problem in an iterative fashion, using the F# type system to do outside-in development. With the overall behaviour in place, there's still work to be done. </p> <p> From type inference of the <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order functions'</a> arguments, we know that we still need to implement functions with these signatures: <ul> <li>ReadyData&nbsp;-&gt;&nbsp;bool</li> <li>unit&nbsp;-&gt;&nbsp;Timed&lt;MessageHandler option&gt;</li> <li>NoMessageData&nbsp;-&gt;&nbsp;bool</li> <li>unit&nbsp;-&gt;&nbsp;Timed&lt;'a&gt;</li> </ul> In this article, I'll show you how to implement the <code>NoMessageData&nbsp;-&gt;&nbsp;bool</code> function. If you want to see how to implement the other three functions, you can watch my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> Pluralsight course. </p> <p> The <code>NoMessageData&nbsp;-&gt;&nbsp;bool</code> function is defined as the <code>shouldIdle</code> argument to the transitionFromNoMessage higher-order function. The purpose of the shouldIdle function is to determine whether there's enough remaining time to idle. </p> <h3 id="63f2f06d65814c509832085e4fc728c8"> Development <a href="#63f2f06d65814c509832085e4fc728c8" title="permalink">#</a> </h3> <p> Since we know the signature of the function, we can start by declaring it like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;shouldIdle&nbsp;(nm&nbsp;:&nbsp;<span style="color:#2b91af;">NoMessageData</span>)&nbsp;:&nbsp;<span style="color:#2b91af;">bool</span>&nbsp;= </pre> </p> <p> Although it doesn't <em>have</em> to be called <em>shouldIdle</em>, in this case, I think the name is appropriate. </p> <p> In order to determine if there's enough time left to idle, the function must know what time it is right now. Recall that, by design, PollingConsumer states are instantaneous, while transitions take time. The time a transition starts and stops is captured by a Timed&lt;'a&gt; value. </p> <p> The <code>nm</code> argument has the type <code>NoMessageData</code>, which is an alias for <code>Timed&lt;TimeSpan list&gt;</code>. The <code>Timed</code> part contains information about when the transition into the No message state started and stopped. Since being in a state has no duration, <code>nm.Stopped</code> represents the time when shouldIdle executes. That's part of the solution, so we can start typing: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;shouldIdle&nbsp;(nm&nbsp;:&nbsp;<span style="color:#2b91af;">NoMessageData</span>)&nbsp;:&nbsp;<span style="color:#2b91af;">bool</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;nm.Stopped</pre> </p> <p> This doesn't yet compile, because <code>nm.Stopped</code> is a DateTimeOffset value, but the function is declared as returning bool. </p> <p> If we imagine that we add the idle duration to the current time, it should gives us the time it'd be when idling is done. That time should be less than the time the Polling Consumer must exit: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;shouldIdle&nbsp;(nm&nbsp;:&nbsp;<span style="color:#2b91af;">NoMessageData</span>)&nbsp;:&nbsp;<span style="color:#2b91af;">bool</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;nm.Stopped&nbsp;+&nbsp;idleDuration&nbsp;&lt;&nbsp;stopBefore</pre> </p> <p> This still doesn't compile because <code>idleDuration</code> and <code>stopBefore</code> are undefined, but this is easy to fix: promote them to arguments: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;shouldIdle&nbsp;idleDuration&nbsp;stopBefore&nbsp;(nm&nbsp;:&nbsp;<span style="color:#2b91af;">NoMessageData</span>)&nbsp;:&nbsp;<span style="color:#2b91af;">bool</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;nm.Stopped&nbsp;+&nbsp;idleDuration&nbsp;&lt;&nbsp;stopBefore</pre> </p> <p> If you paid attention to the <a href="/2015/08/10/type-driven-development">previous article</a>, you'll notice that this is exactly the same technique I used for the transitionFromNoMessage function. Apparently, we're not done with outside-in development yet. </p> <h3 id="bcab476842e64ae18bf7420be2d07583"> Type inference <a href="#bcab476842e64ae18bf7420be2d07583" title="permalink">#</a> </h3> <p> The function now compiles, and has the type <code>TimeSpan -&gt;&nbsp;DateTimeOffset -&gt;&nbsp;NoMessageData -&gt;&nbsp;bool</code>. </p> <p> Once again, I've used the trick of promoting an undefined value to a function argument, and let type inference take care of the rest. This also works here. Since <code>nm.Stopped</code> is a DateTimeOffset value, and we're adding something to it with the <code>+</code> operator, <code>idleDuration</code> has to be of a type that supports adding to DateTimeOffset. The only thing you can add to DateTimeOffset is a TimeSpan, so <code>idleDuration</code> is inferred to be a TimeSpan value. </p> <p> When you add a TimeSpan value to a DateTimeOffset value, you get another DateTimeOffset value back, so the type of the expression <code>nm.Stopped + idleDuration</code> is DateTimeOffset. The entire return expression compares that DateTimeOffset value with the <code>&lt;</code> operator, which requires that both the left-hand and the right-hand expressions have the same type. Ergo must <code>stopBefore</code> also be a DateTimeOffset value. </p> <p> While we set out to implement a function with the type <code>NoMessageData&nbsp;-&gt;&nbsp;bool</code>, we eventually created a function with the type <code>TimeSpan -&gt;&nbsp;DateTimeOffset -&gt;&nbsp;NoMessageData -&gt;&nbsp;bool</code>, which isn't quite what we need. </p> <h3 id="1f7c3bdd56b0499b967ef5665b727cd5"> Partial application <a href="#1f7c3bdd56b0499b967ef5665b727cd5" title="permalink">#</a> </h3> <p> The extra arguments can be removed again with partial function application. <a href="/2015/08/12/type-driven-development-composition">When the Polling Consumer application starts, it can easily calculate when it ought to stop</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;now&#39;&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now <span style="color:blue;">let</span>&nbsp;stopBefore&#39;&nbsp;=&nbsp;now&#39;&nbsp;+&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromMinutes&nbsp;1.</pre> </p> <p> This assumes that the Polling Consumer should run for a maximum of 1 minute. </p> <p> Likewise, we can create an idle duration value: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;idleDuration&#39;&nbsp;=&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromSeconds&nbsp;5. </pre> </p> <p> Here, the value is hard-coded, but it could have gone in a configuration file instead, or be passed in as a command-line argument. </p> <p> Given these values, we can now partially apply the function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;shouldIdle&#39;&nbsp;=&nbsp;shouldIdle&nbsp;idleDuration&#39;&nbsp;stopBefore&#39; </pre> </p> <p> Since we're not supplying the third NoMessageData argument for the function, the return value of this partial application is a <em>new</em> function with the type <code>NoMessageData&nbsp;-&gt;&nbsp;bool</code> - exactly what we need. </p> <h3 id="185657836cc04fcb91dce5b2773502fb"> Summary <a href="#185657836cc04fcb91dce5b2773502fb" title="permalink">#</a> </h3> <p> In this article, you saw how to approach the implementation of one of the functions identified with the outside-in <a href="/2015/08/10/type-driven-development">Type Driven Development</a> technique. If you want to see the other three functions implemented, a much more detailed discussion of the technique, as well as the entire code base with commit messages, you can watch my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> Pluralsight course. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Type Driven Development https://blog.ploeh.dk/2015/08/10/type-driven-development 2015-08-10T12:44:00+00:00 Mark Seemann <div id="post"> <p> <em>A strong type system can not only prevent errors, but also guide you and provide feedback in your design process.</em> </p> <p> Have you ever worked in a statically typed language (e.g. C# or Java), only to wish that you'd be allowed to focus on <em>what you're doing</em>, instead of having to declare types of arguments and create new classes all the time? </p> <p> Have you, on the other hand, ever worked in a dynamic language (e.g. Javascript) and wished you could get static type checking to prevent a myriad of small, but preventable errors? </p> <p> You can have the best of both worlds, and more! F#'s type system is strong, but non-obtrusive. It enables you to focus on the behaviour of the code you're writing, while still being statically typed. </p> <p> Not only can it prevent syntax and usage errors, but it can even provide guidance on how to proceed with a given problem. </p> <p> This is best explained with an example. </p> <h3 id="50244671281d4be5b27f371cd58aab42"> Example problem: simulate a continuously running process as a series of discrete processes <a href="#50244671281d4be5b27f371cd58aab42" title="permalink">#</a> </h3> <p> A couple of years ago I had a very particular problem: I <a href="/2014/09/25/faking-a-continuously-polling-consumer-with-scheduled-tasks">needed to simulate a continuously running task using a sequence of discrete processes</a>. </p> <p> In short, I needed to start a process (in reality a simple .exe file) that would act as a <a href="http://www.enterpriseintegrationpatterns.com/PollingConsumer.html">Polling Consumer</a>, but with the twist that it would keep track of time. It would need to run for one minute, and then shut down so that an overall scheduler could start the process again. This was to guarantee that the process would be running on <em>at most</em> one server in a farm. </p> <p> The Polling Consumer should pull a message off a queue and hand it to some dispatcher, which would then make sure that the message was handled by an appropriate handler. This takes time, so makes the whole process more complex. </p> <p> If the Polling Consumer estimates that receiving and handling a message takes 200 milliseconds, and it's 100 milliseconds from shutting down, it shouldn't poll for a message. It would take too long, and it wouldn't be able to shut down in time. </p> <p> My problem was that I didn't know how long it took to handle a message, so the Polling Consumer would have to measure that as well, constantly updating its estimates of how long it takes to handle a message. </p> <p> This wasn't a harder problem that I could <a href="/2014/09/25/faking-a-continuously-polling-consumer-with-scheduled-tasks">originally solve it in a highly coupled imperative fashion</a>. I wasn't happy with that implementation, though, so I'm happy that there's a much more <em>incremental</em> approach to the problem. </p> <h3 id="5a96c9e039a84adabc5b6b79263bb061"> A Finite State Machine <a href="#5a96c9e039a84adabc5b6b79263bb061" title="permalink">#</a> </h3> <p> The first breakthrough came when I realised that I could model the problem as a finite state machine: </p> <p> <img src="/content/binary/polling-consumer-finite-state-machine.png" alt="Polling Consumer state machine transition diagram"> </p> <p> The implicit start state is always the <em>Ready</em> state, because when the Polling Consumer starts, it has no message, but plenty of time, so therefore ready for a new message. </p> <p> From the <em>Ready</em> state, the Polling Consumer may decide to poll for a message; if one is received, the new state is the <em>Received</em> state. </p> <p> From the <em>Received</em> state, the only legal transition is back into the <em>Ready</em> state by handling the message. </p> <p> From the <em>Ready</em> state, it may also turn out that there's currently no message in the queue, in which case the new state is the <em>No message</em> state. </p> <p> In the <em>No message</em> state, the Polling Consumer may decide to idle for perhaps five seconds before transitioning back into the <em>Ready</em> state. </p> <p> When in the <em>Ready</em> or <em>No message</em> states, the Polling Consumer may also realise that time is running out, and so decide to quit, in which case the (final) state is the <em>End</em> state. </p> <p> This means that the Polling Consumer needs to measure what it does, and those measurements influence what it decides to do. </p> <h3 id="0fda922abd8443eb9669cd874c6b5666"> Separation of concerns <a href="#0fda922abd8443eb9669cd874c6b5666" title="permalink">#</a> </h3> <p> It sounds like we have a time concern (do I dare say <em>dimension</em>?), and a concern related to <em>doing</em> something. Given multiple concerns, we should separate them </p> <p> The first thing I did was to introduce a <a href="/2014/12/17/good-times-with-f">Timed&lt;'a&gt; generic record type</a>. This represents the result of performing a computation, as well as the times the computation started and stopped. I changed it slightly from the linked article, so here's the updated code for that: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">Timed</span>&lt;&#39;a&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Started&nbsp;:&nbsp;<span style="color:#2b91af;">DateTimeOffset</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Stopped&nbsp;:&nbsp;<span style="color:#2b91af;">DateTimeOffset</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:&nbsp;&#39;a&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Duration&nbsp;=&nbsp;this.Stopped&nbsp;-&nbsp;this.Started <span style="color:blue;">module</span>&nbsp;<span style="color:#2b91af;">Untimed</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Started&nbsp;=&nbsp;x.Started;&nbsp;Stopped&nbsp;=&nbsp;x.Stopped;&nbsp;Result&nbsp;=&nbsp;f&nbsp;x.Result&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;withResult&nbsp;newResult&nbsp;x&nbsp;=&nbsp;map&nbsp;(<span style="color:blue;">fun</span>&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;newResult)&nbsp;x <span style="color:blue;">module</span>&nbsp;<span style="color:#2b91af;">Timed</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capture&nbsp;clock&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;now&nbsp;=&nbsp;clock() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Started&nbsp;=&nbsp;now;&nbsp;Stopped&nbsp;=&nbsp;now;&nbsp;Result&nbsp;=&nbsp;x&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;clock&nbsp;f&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;result&nbsp;=&nbsp;f&nbsp;x.Result &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stopped&nbsp;=&nbsp;clock&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Started&nbsp;=&nbsp;x.Started;&nbsp;Stopped&nbsp;=&nbsp;stopped;&nbsp;Result&nbsp;=&nbsp;result&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;timeOn&nbsp;clock&nbsp;f&nbsp;x&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;capture&nbsp;clock&nbsp;|&gt;&nbsp;map&nbsp;clock&nbsp;f</pre> </p> <p> This enables me to capture timing information about any operation, including the transitions between various states in a finite state machine. </p> <p> The way I modelled the finite state machine for the Polling Consumer explicitly models all states as being instantaneous, while transitioning between states takes time. This means that we can model all transitions as functions that return Timed&lt;'something&gt;. </p> <h3 id="c35d8756733c4a5db36552e6ea6fe94c"> State data types <a href="#c35d8756733c4a5db36552e6ea6fe94c" title="permalink">#</a> </h3> <p> The first step is to model the data associated with each state in the finite state machine. This is an iterative process, but here I'll just show you the final result. If you're interested in seeing this design process in more details, you can watch my Pluralsight course <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a>. </p> <p> <pre><span style="color:green;">//&nbsp;Auxiliary&nbsp;types</span> <span style="color:blue;">type</span>&nbsp;MessageHandler&nbsp;=&nbsp;<span style="color:#2b91af;">unit</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Timed</span>&lt;<span style="color:#2b91af;">unit</span>&gt; <span style="color:green;">//&nbsp;State&nbsp;data</span> <span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">ReadyData</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Timed</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#2b91af;">list</span>&gt; <span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">ReceivedMessageData</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Timed</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#2b91af;">list</span>&nbsp;*&nbsp;MessageHandler&gt; <span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">NoMessageData</span>&nbsp;=&nbsp;<span style="color:#2b91af;">Timed</span>&lt;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#2b91af;">list</span>&gt;</pre> </p> <p> All of these types are simply type aliases, so in fact they aren't strictly necessary. It's just helpful to give things a name from time to time. </p> <p> The auxiliary MessageHandler type is a function that takes nothing (unit) as input, and returns 'timed nothing' as output. You may wonder how that handles a message. The intent is that any MessageHandler function is going to close over a real message: when a client calls it, it handles the message it closes over, and returns the time it took. The reason for this design is that the Polling Consumer doesn't need to 'see' the message; it only needs to know that it was handled, and how long it took. This design keeps the Polling Consumer decoupled from any particular message types. </p> <p> There are three state data types: one for each state. </p> <p> Hey! What about the End state? </p> <p> It turns out that the End state doesn't need any associated data, because once the End state is reached, no more decisions should be made. </p> <p> As promised, the three other states all contain a Timed&lt;'something&gt;. The timing information tells us when the transition into the given state started and stopped. </p> <p> The data for the Ready and No message states are the same: Timed&lt;TimeSpan list&gt;, but notice that the TimeSpan list also appears in the Received state. This list of durations contains the statistics that the Polling Consumer measures. Every time it handles a message, it needs to measure how long it took. All such measurements are collected in this TimeSpan list, which must be passed around in all states so that the data isn't lost. </p> <p> The data for the Received message state is different, because it also contains a MessageHandler. Every time the Polling Consumer receives a message, this message must be composed into a MessageHandler, and the MessageHandler passed as the second element of the state's tuple of data. </p> <p> With all four states defined, we can now define a discriminated union that models a snapshot of the state machine: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">PollingConsumer</span>&nbsp;= |&nbsp;ReadyState&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#2b91af;">ReadyData</span> |&nbsp;ReceivedMessageState&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#2b91af;">ReceivedMessageData</span> |&nbsp;NoMessageState&nbsp;<span style="color:blue;">of</span>&nbsp;<span style="color:#2b91af;">NoMessageData</span> |&nbsp;StoppedState</pre> </p> <p> That is, a PollingConsumer value represents a state of the Polling Consumer state machine. </p> <p> Already at this stage, it should be apparent that F#'s type system is a great thinking tool, because it enables you to define type aliases declaratively, with only a single line of code here and there. Still, the advantage of the type system becomes much more apparent once you start to use these types. </p> <h3 id="8eb73371a4294d67b6f267d5a8cc2092"> Transitions <a href="#8eb73371a4294d67b6f267d5a8cc2092" title="permalink">#</a> </h3> <p> With data types defined for each state, the next step of implementing a finite state machine is to define a function for each state. These functions are called <em>transitions</em>, and they should take a concrete state as input, and return a new PollingConsumer value as output. Since there are four concrete states in this example, there must be four transitions. Each should have the type <code>'concreteData&nbsp;-&gt;&nbsp;PollingConsumer</code>, e.g. <code>ReadyData&nbsp;-&gt;&nbsp;PollingConsumer</code>, <code>ReceivedMessageData&nbsp;-&gt;&nbsp;PollingConsumer</code>, etc. </p> <p> As you can see, we're already getting guidance from the type system, because we now know the <em>types</em> of the four transitions we must implement. </p> <p> Let's begin with the simplest one. When the Polling Consumer is Stopped, it should stay Stopped. That's easy. The transition should have the type <code>unit&nbsp;-&gt;&nbsp;PollingConsumer</code>, because there's no data associated with the StoppedState. </p> <p> Your first attempt might look like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transitionFromStopped&nbsp;()&nbsp;:&nbsp;<span style="color:#2b91af;">PollingConsumer</span>&nbsp;=&nbsp;?? </pre> </p> <p> This obviously doesn't compile because of the question marks (which aren't proper F# syntax). Knowing that you must return a PollingConsumer value, which one (of ReadyState, ReceivedMessageState, NoMessageState, or StoppedState) should you return? </p> <p> Once the Polling Consumer is in the Stopped state, it should stay in the Stopped state, so the answer is easy: this transition should always return the Stopped state: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transitionFromStopped&nbsp;()&nbsp;:&nbsp;<span style="color:#2b91af;">PollingConsumer</span>&nbsp;=&nbsp;StoppedState </pre> </p> <p> It's as easy as that. </p> <p> Since the (unit) input into the function doesn't do anything, we can remove it, effectively turning this particular function into a value: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transitionFromStopped&nbsp;:&nbsp;<span style="color:#2b91af;">PollingConsumer</span>&nbsp;=&nbsp;StoppedState </pre> </p> <p> This is a degenerate case, and not something that always happens. </p> <h3 id="070b716200f04db691951f82ec8fffe5"> Another transition <a href="#070b716200f04db691951f82ec8fffe5" title="permalink">#</a> </h3> <p> Let's do another transition! </p> <p> Another good example is the transition out of the No message state. This transition must have the type <code>NoMessageData&nbsp;-&gt;&nbsp;PollingConsumer</code>, so you can start typing: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transitionFromNoMessage&nbsp;(nm&nbsp;:&nbsp;<span style="color:#2b91af;">NoMessageData</span>)&nbsp;:&nbsp;<span style="color:#2b91af;">PollingConsumer</span>&nbsp;= </pre> </p> <p> This function takes NoMessageData as input, and returns a PollingConsumer value as output. Now you only need to figure out how to implement it. What should it do? </p> <p> If you look at the state transition diagram, you can see that from the No message state, the Polling Consumer should either decide to quit, or transition back to the Ready state after idling. That's the high-level behaviour we're aiming for, so let's try to put it into code: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transitionFromNoMessage&nbsp;(nm&nbsp;:&nbsp;<span style="color:#2b91af;">NoMessageData</span>)&nbsp;:&nbsp;<span style="color:#2b91af;">PollingConsumer</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;shouldIdle&nbsp;nm &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;idle&nbsp;()&nbsp;|&gt;&nbsp;ReadyState &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;StoppedState</pre> </p> <p> This doesn't compile, because neither <code>shouldIdle</code> nor <code>idle</code> are defined, but this is the overall behaviour we're aiming for. </p> <p> Let's keep it high-level, so we'll simply promote the undefined values to arguments: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transitionFromNoMessage&nbsp;shouldIdle&nbsp;idle&nbsp;(nm&nbsp;:&nbsp;<span style="color:#2b91af;">NoMessageData</span>)&nbsp;:&nbsp;<span style="color:#2b91af;">PollingConsumer</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;shouldIdle&nbsp;nm &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;idle&nbsp;()&nbsp;|&gt;&nbsp;ReadyState &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;StoppedState</pre> </p> <p> This compiles! Notice how type inference enables you to easily introduce new arguments without breaking your flow. In statically typed languages like C# or Java, you'd have to stop and declare the type of the arguments. If the types you need for those arguments doesn't exist, you'd have to go and create them first. That'd often mean creating new interfaces or classes in new files. That breaks your flow when you're actually trying to figure out how to model the high-level behaviour of a system. </p> <p> Often, figuring out how to model the behaviour of a system is an exploratory process, so perhaps you don't get it right in the first attempt. Again, with languages like C# or Java, you'd have to waste a lot of time fiddling with the argument declarations, and perhaps the types you'd have to define in order to declare those arguments. </p> <p> In F#, you can stay focused on the high-level behaviour, and take advantage of type inference to subsequently contemplate if all looks good. </p> <p> In the transitionFromNoMessage function, the <code>shouldIdle</code> argument is inferred to be of the type <code>NoMessageData&nbsp;-&gt;&nbsp;bool</code>. That seems reasonable. It's a function that determines whether or not to idle, based on a NoMessageData value. Recall that NoMessageData is an alias for Timed&lt;TimeSpan&nbsp;list&gt;, and that all transition functions take time and return Timed&lt;'something&gt; in order to capture the time spent in transition. This means that the time data in NoMessageData contains information about when the transition <em>into the No message state</em> started and stopped. That should be plenty of information necessary to make the decision on whether there's time to idle or not. In a future article, you'll see how to implement a shouldIdle function. </p> <p> What about the <code>idle</code> argument, then? As the transitionFromNoMessage function is currently written, this argument is inferred to be of the type <code>unit&nbsp;-&gt;&nbsp;ReadyData</code>. Recall that ReadyData is an alias for Timed&lt;TimeSpan&nbsp;list&gt;; what we're really looking at here, is a function of the type <code>unit&nbsp;-&gt;&nbsp;Timed&lt;TimeSpan&nbsp;list&gt;</code>. In other words, a function that produces a Timed&lt;TimeSpan&nbsp;list&gt; out of thin air! That doesn't sound right. Which TimeSpan&nbsp;list should such a function return? Recall that this list contains the statistics for all the previously handled messages. How can a function produce these statistics given nothing (unit) as input? </p> <p> This seems extra strange, because the statistics are already there, contained in the <code>nm</code> argument, which is a NoMessageData (that is: a Timed&lt;TimeSpan&nbsp;list&gt;) value. The inferred signature of <code>idle</code> suggests that the statistics contained in <code>nm</code> are being ignored. </p> <p> Notice how the type inference gives us an opportunity to <em>contemplate</em> the current implementation. In this case, just by looking at inferred types, we realise that something is wrong. </p> <p> Instead, let's change the transitionFromNoMessage function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transitionFromNoMessage&nbsp;shouldIdle&nbsp;idle&nbsp;(nm&nbsp;:&nbsp;<span style="color:#2b91af;">NoMessageData</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;shouldIdle&nbsp;nm &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;idle&nbsp;()&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Untimed</span>.withResult&nbsp;nm.Result&nbsp;|&gt;&nbsp;ReadyState &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;StoppedState</pre> </p> <p> This seems more reasonable: the function idles, but then takes the timing information from idling, but replaces it with the statistics from <code>nm</code>. The inferred type of <code>idle</code> is now <code>unit&nbsp;-&gt;&nbsp;Timed&lt;'a&gt;</code>. That seems more reasonable. It's any function that returns Timed&lt;'a&gt;, where the timing information indicates when idling started and stopped. </p> <p> This still doesn't look like a pure function, because it relies on the side effect that time passes, but it turns out to be good enough for this purpose. </p> <h3 id="f56434ea6e4c46a8a5df756653a3ae1f"> Higher-order functions <a href="#f56434ea6e4c46a8a5df756653a3ae1f" title="permalink">#</a> </h3> <p> Perhaps one thing is bothering you: I said that the transition out of the No message state should have the type <code>NoMessageData&nbsp;-&gt;&nbsp;PollingConsumer</code>, but the final version of transitionFromNoMessage has the type <code>(NoMessageData&nbsp;-&gt;&nbsp;bool)&nbsp;-&gt;&nbsp;(unit&nbsp;-&gt;&nbsp;Timed&lt;'a&gt;)&nbsp;-&gt;&nbsp;NoMessageData&nbsp;-&gt;&nbsp;PollingConsumer</code>! </p> <p> The transitionFromNoMessage function has turned out to be a <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order function</a>, because it takes other functions as arguments. </p> <p> Even though it doesn't exactly have the desired type, it can be partially applied. Imagine that you have two functions named <code>shouldIdle'</code> and <code>idle'</code>, with the appropriate types, you can use them to partially apply the transitionFromNoMessage function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transitionFromNoMessage&#39;&nbsp;=&nbsp;transitionFromNoMessage&nbsp;shouldIdle&#39;&nbsp;idle&#39; </pre> </p> <p> The transitionFromNoMessage' function has the type <code>NoMessageData&nbsp;-&gt;&nbsp;PollingConsumer</code> - exactly what we need! </p> <h3 id="18da991180184ea6b16c7d83264dc43d"> All transitions <a href="#18da991180184ea6b16c7d83264dc43d" title="permalink">#</a> </h3> <p> In this article, you've seen two of the four transitions necessary for defining the behaviour of the Polling Consumer. In total, all four are required: <ul> <li>ReadyData&nbsp;-&gt;&nbsp;PollingConsumer</li> <li>ReceivedMessageData&nbsp;-&gt;&nbsp;PollingConsumer</li> <li>NoMessageData&nbsp;-&gt;&nbsp;PollingConsumer</li> <li>(StoppedData)&nbsp;-&gt;&nbsp;PollingConsumer</li> </ul> In this list, I put StoppedData in parentheses, because this type doesn't actually exist; instead of the fourth function, we have the degenerate transitionFromStopped <em>value</em>. </p> <p> In this article, I will leave it as an exercise to you to implement <code>ReadyData&nbsp;-&gt;&nbsp;PollingConsumer</code> and <code>ReceivedMessageData&nbsp;-&gt;&nbsp;PollingConsumer</code>. If you want to see full implementations of these, as well as a more detailed discussion of this general topic, please watch my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> Pluralsight course. </p> <p> Imagine that we now have all four transitions. This makes it easy to implement the overall state machine. </p> <h3 id="5894f97441a840899ec5c9b0fde19a59"> State machine <a href="#5894f97441a840899ec5c9b0fde19a59" title="permalink">#</a> </h3> <p> Here's one way to execute the state machine: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;run&nbsp;trans&nbsp;state&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;nextState&nbsp;=&nbsp;trans&nbsp;state &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;nextState&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;StoppedState&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;StoppedState &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;run&nbsp;trans&nbsp;nextState</pre> </p> <p> This run function has the inferred type <code>(PollingConsumer&nbsp;-&gt;&nbsp;PollingConsumer)&nbsp;-&gt;&nbsp;PollingConsumer&nbsp;-&gt;&nbsp;PollingConsumer</code>. It takes a <code>trans</code> function that turns one PollingConsumer into another, as well as an initial PollingConsumer value. It then proceeds to recursively call the trans function and itself, until it reaches a StoppedState value. </p> <p> How can we implement a <code>PollingConsumer&nbsp;-&gt;&nbsp;PollingConsumer</code> function? </p> <p> That's easy, because we have all four transition functions, so we can use them: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;transition&nbsp;shouldPoll&nbsp;poll&nbsp;shouldIdle&nbsp;idle&nbsp;state&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;state&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ReadyState&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;transitionFromReady&nbsp;shouldPoll&nbsp;poll&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;ReceivedMessageState&nbsp;rm&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;transitionFromReceived&nbsp;rm &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;NoMessageState&nbsp;nm&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;transitionFromNoMessage&nbsp;shouldIdle&nbsp;idle&nbsp;nm &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;StoppedState&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;transitionFromStopped</pre> </p> <p> The <code>transition</code> function has the type <code>(ReadyData&nbsp;-&gt;&nbsp;bool) -&gt;&nbsp;(unit&nbsp;-&gt;&nbsp;Timed&lt;messagehandler option&gt;) -&gt;&nbsp;(NoMessageData&nbsp;-&gt;&nbsp;bool) -&gt;&nbsp;(unit&nbsp;-&gt;&nbsp;Timed&lt;'a&gt;) -&gt;&nbsp;PollingConsumer -&gt;&nbsp;PollingConsumer</code>. That looks positively horrendous, but it's not so bad; you can partially apply it in order to get a function with the desired type <code>PollingConsumer&nbsp;-&gt;&nbsp;PollingConsumer</code>. </p> <h3 id="82ba8ad0110149f4bca2eecfb27f0f3a"> Implicit to-do list <a href="#82ba8ad0110149f4bca2eecfb27f0f3a" title="permalink">#</a> </h3> <p> Even though we now have the <code>run</code> and <code>transition</code> functions, we only have the high-level behaviour in place. We still have a lot of implementation details left. </p> <p> This is, in my opinion, one of the benefits of this approach to using the low-friction type system: First, you can focus on the desired <em>behaviour</em> of the system. Then, you address various implementation concerns. It's outside-in development. </p> <p> Another advantage is that at this point, it's quite clear what to do next. </p> <p> The transitionFromNoMessage function clearly states that it needs the functions <code>shouldIdle</code> and <code>idle</code> as arguments. You can't call the function without these arguments, so it's clear that you must supply them. </p> <p> Not only did the type system allow us to introduce these function arguments with low friction, but <em>it also tells us the types they should have</em>. </p> <p> In my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Pluralsight course</a> you can see how the transition out of the Ready state also turns out to be a higher-order function that takes two other functions as arguments. </p> <p> In all, that's four functions we still need to implement before we can use the state machine. It's not going to be possible to partially apply the <code>transition</code> function before these four functions are available. </p> <p> The type system thereby tells us that we still need to implement these four functions: <ul> <li>ReadyData&nbsp;-&gt;&nbsp;bool</li> <li>unit&nbsp;-&gt;&nbsp;Timed&lt;MessageHandler option&gt;</li> <li>NoMessageData&nbsp;-&gt;&nbsp;bool</li> <li>unit&nbsp;-&gt;&nbsp;Timed&lt;'a&gt;</li> </ul> It's almost as though the type system implicitly provides a to-do list. There's no reason to keep such a list on a piece of paper on the side. As long as we still have work to do, we're not going to be able to compile a composition of the <code>run</code> function. Once we can compile a composition of the <code>run</code> function, there are no implementation details left. </p> <h3 id="36f7216883e04c7d9540a091f80fe7f8"> Summary <a href="#36f7216883e04c7d9540a091f80fe7f8" title="permalink">#</a> </h3> <p> Although this turned out to be quite a lengthy article, it only provides a sketch of the technique. You can see more code details, and a more in-depth discussion of the approach, in my <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a> Pluralsight course. </p> <p> The F# type system can be used in ways that C# and Java's type systems cant: <ul> <li><strong>Dependencies</strong> can be introduced <em>just-in-time</em> as function arguments.</li> <li>You can <strong>contemplate</strong> the inferred types to evaluate the soundness of the design.</li> <li>The type system implicitly keeps a <strong>to-do list</strong> for you.</li> </ul> In future articles, I'll demonstrate <a href="/2015/08/11/type-driven-development-implementing-shouldidle">how to implement some of the missing functions</a>, as well as <a href="/2015/08/12/type-driven-development-composition">how to compose the Polling Consumer</a>. </p> <p> <strong>Update 2017-07-10:</strong> See <a href="/2017/06/27/pure-times">Pure times</a> for a more functional design. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Idiomatic or idiosyncratic? https://blog.ploeh.dk/2015/08/03/idiomatic-or-idiosyncratic 2015-08-03T12:39:00+00:00 Mark Seemann <div id="post"> <p> <em>A strange programming construct may just be a friendly construct you haven't yet met.</em> </p> <p> Take a look at this fragment of JavaScript, and reflect on how you feel about it. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;nerdCapsToKebabCase&nbsp;=&nbsp;<span style="color:blue;">function</span>&nbsp;(text)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;text.length;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;c&nbsp;=&nbsp;text.charAt(i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(i&nbsp;&gt;&nbsp;0&nbsp;&amp;&amp;&nbsp;c&nbsp;==&nbsp;c.toUpperCase())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;result&nbsp;+&nbsp;<span style="color:#a31515;">&quot;-&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;result&nbsp;+&nbsp;c.toLowerCase(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;result; };</pre> </p> <p> Do you understand what it does? Does it feel appropriate for the language? Does it communicate its purpose? </p> <p> Most likely, you answered yes to all three questions - at least if you know what <a href="http://c2.com/cgi/wiki?CapitalizationRules">NerdCaps</a> and <a href="http://c2.com/cgi/wiki?KebabCase">kebab-case</a> means. </p> <p> Would your father, or your 12-year old daughter, or your English teacher, also answer <em>yes</em> to all of these questions? Probably not, assuming they don't know programming. </p> <p> Everything is easy once you know how to do it; everything is hard if you don't. </p> <h3 id="4f9be9fd77024195bacd70e45f197e9c"> Beauty is in the eye of the beholder <a href="#4f9be9fd77024195bacd70e45f197e9c" title="permalink">#</a> </h3> <p> Writing software is hard. Writing maintainable software - code that you and your team can keep working on year after year - is extraordinarily hard. The most prominent sources of problems seem to be accidental complexity and coupling. </p> <p> During my career, I've advised thousands of people on how to write maintainable code: how to reduce coupling, how to simplify, how to express code that a human can easily reason about. </p> <p> The most common reaction to my suggestions? <blockquote> "But, that's even more unreadable than the code we already had!" </blockquote> What that really means is usually: <em>"I don't understand this, and I feel very uncomfortable about that."</em> </p> <p> That's perfectly natural, but it doesn't mean that my suggestions are <em>really</em> unreadable. It just means that you may have to work a little before it becomes readable, but once that happens, you will understand why it's not only readable, but also better. </p> <p> Let's take a step back and examine what it means when source code is readable. </p> <h3 id="c793fb5dc1dc40f2bc176eafa31683fa"> Imperative code for beginners <a href="#c793fb5dc1dc40f2bc176eafa31683fa" title="permalink">#</a> </h3> <p> Imagine that you're a beginner programmer, and that you have to implement the <em>nerd caps to kebab case converter</em> from the introduction. </p> <p> This converter should take as input a string in <em>NerdCaps</em>, and convert it to <em>kebab-case</em>. Here's some sample data: </p> <p> <table> <thead> <tr> <th>Input</th> <th>Expected output</th> </tr> </thead> <tbody> <tr> <td>Foo</td> <td>foo</td> </tr> <tr> <td>Bar</td> <td>bar</td> </tr> <tr> <td>FooBar</td> <td>foo-bar</td> </tr> <tr> <td>barBaz</td> <td>bar-baz</td> </tr> <tr> <td>barBazQuux</td> <td>bar-baz-quux</td> </tr> <tr> <td>garplyGorgeFoo</td> <td>garply-gorge-foo</td> </tr> </tbody> </table> </p> <p> Since (in this imaginary example) you're a beginner, you should choose to use a beginner's language, so perhaps you select Visual Basic .NET. That seems appropriate, as BASIC was originally an acronym for <em>Beginner's</em> All-purpose Symbolic Instruction Code. </p> <p> Perhaps you write the function like this: </p> <p> <pre><span style="color:blue;">Function</span>&nbsp;Convert(text&nbsp;<span style="color:blue;">As</span>&nbsp;<span style="color:blue;">String</span>)&nbsp;<span style="color:blue;">As</span>&nbsp;<span style="color:blue;">String</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Dim</span>&nbsp;result&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">For</span>&nbsp;index&nbsp;=&nbsp;0&nbsp;<span style="color:blue;">To</span>&nbsp;text.Length&nbsp;-&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Dim</span>&nbsp;c&nbsp;=&nbsp;text(index) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">If</span>&nbsp;index&nbsp;&gt;&nbsp;0&nbsp;<span style="color:blue;">And</span>&nbsp;<span style="color:blue;">Char</span>.IsUpper(c)&nbsp;<span style="color:blue;">Then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;result&nbsp;+&nbsp;<span style="color:#a31515;">&quot;-&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">End</span>&nbsp;<span style="color:blue;">If</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;result&nbsp;+&nbsp;c.ToString().ToLower() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Next</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Return</span>&nbsp;result <span style="color:blue;">End</span>&nbsp;<span style="color:blue;">Function</span></pre> </p> <p> Is this appropriate Visual Basic code? Yes, it is. </p> <p> Is it readable? That completely depends on who's doing the reading. You may find it readable, but does your father, or your 12-year old daughter? </p> <p> I asked my 12-year old daughter if she understood, and she didn't. However, she understands this: </p> <p> <em>Funktionen omformer en stump tekst ved at først at dele den op i stumper hver gang der optræder et stort bogstav. Derefter sætter den bindestreg mellem hver tekststump, og laver hele teksten om til små bogstaver - altså nu med bindestreger mellem hver tekststump.</em> </p> <p> Did you understand that? If you understand Danish (or another Scandinavian language), you probably did; otherwise, you probably didn't. </p> <p> Readability is evaluated based on what you already know. However, what you know isn't constant. </p> <h3 id="445a7f25a4fb478fbb65d19f9ca834fd"> Imperative code for seasoned programmers <a href="#445a7f25a4fb478fbb65d19f9ca834fd" title="permalink">#</a> </h3> <p> Once you've gained some experience, you may find Visual Basic's syntax too verbose. For instance, once you understand the idea about <em>scope</em>, you may find End If, End Function etc. to be noisy rather than helpful. </p> <p> Perhaps you're ready to replace those keywords with curly braces. Here's the same function in C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Convert(<span style="color:blue;">string</span>&nbsp;text) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;result&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;(<span style="color:blue;">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;text.Length;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;c&nbsp;=&nbsp;text[i]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(i&nbsp;&gt;&nbsp;0&nbsp;&amp;&amp;&nbsp;<span style="color:#2b91af;">Char</span>.IsUpper(c)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;result&nbsp;+&nbsp;<span style="color:#a31515;">&quot;-&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;result&nbsp;+&nbsp;c.ToString().ToLower(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;result; }</pre> </p> <p> Is this proper C# code? Yes. Is it readable? Again, it really depends on what you already know, but most programmers familiar with the C language family would probably find it fairly approachable. Notice how close it is to the original JavaScript example. </p> <h3 id="4552f7e8bcfb4ee9a4d30ecf3f9a7a33"> Imperative code for experts <a href="#4552f7e8bcfb4ee9a4d30ecf3f9a7a33" title="permalink">#</a> </h3> <p> Once you've gained lots of experience, you will have learned that although the curly braces <em>formally</em> delimit scopes, in order to make the code readable, you have to make sure to indent each scope appropriately. That seems to violate the DRY principle. Why not let the indentation make the code readable, as well as indicate the scope? </p> <p> Various languages have so-called <em>significant whitespace</em>. The most widely known may be Python, but since we've already looked at two .NET languages, why not look at a third? </p> <p> Here's the above function in F#: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;nerdCapsToKebabCase&nbsp;(text&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>)&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:blue;">mutable</span>&nbsp;result&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;i&nbsp;=&nbsp;0&nbsp;<span style="color:blue;">to</span>&nbsp;text.Length&nbsp;-&nbsp;1&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;c&nbsp;=&nbsp;text.Chars&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(i&nbsp;&gt;&nbsp;0&nbsp;&amp;&amp;&nbsp;<span style="color:#2b91af;">Char</span>.IsUpper&nbsp;c)&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;&lt;-&nbsp;result&nbsp;+&nbsp;<span style="color:#a31515;">&quot;-&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;&lt;-&nbsp;result&nbsp;+&nbsp;c.ToString().ToLower() &nbsp;&nbsp;&nbsp;&nbsp;result</pre> </p> <p> Is this appropriate F# code? </p> <p> Not really. While it compiles and works, it goes against the grain of the language. F# is a <em>Functional First</em> language, but the above implementation is as imperative as all the previous examples. </p> <h3 id="ce69ea31c6fb4f979699dc2930fb8ea8"> Functional refactoring <a href="#ce69ea31c6fb4f979699dc2930fb8ea8" title="permalink">#</a> </h3> <p> A more Functional approach in F# could be this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;nerdCapsToKebabCase&nbsp;(text&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>)&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;addSkewer&nbsp;index&nbsp;c&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;s&nbsp;=&nbsp;c.ToString().ToLower() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;index,&nbsp;<span style="color:#2b91af;">Char</span>.IsUpper&nbsp;c&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;0,&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_,&nbsp;<span style="color:blue;">true</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;-&quot;</span>&nbsp;+&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_,&nbsp;<span style="color:blue;">false</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;text&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.mapi&nbsp;addSkewer&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">String</span>.concat&nbsp;<span style="color:#a31515;">&quot;&quot;</span></pre> </p> <p> This implementation uses a private, inner function called <code>addSkewer</code> to convert each character to a string, based on the value of the character, as well as the position it appears. This conversion is applied to each character, and the resulting sequence of strings is finally concatenated and returned. </p> <p> Is this good F#? Yes, I would say that it is. There are lots of different ways you could have approached this problem. This is only one of them, but I find it quite passable. </p> <p> Is it readable? Again, if you don't know F#, you probably don't think that it is. However, the F# programmers I asked found it readable. </p> <p> This solution has the advantage that it doesn't rely on mutation. Since the conversion problem in this article is a bit of a toy problem, the use of imperative code with heavy use of mutation probably isn't a problem, but as the size of your procedures grow, mutation makes it harder to understand what happens in the code. </p> <p> Another improvement over the imperative version is that this implementation uses a higher level of abstraction. Instead of stating <em>how</em> to arrive at the desired result, it states <em>what</em> to do. This means that it becomes easier to change the composition of it in order to change other characteristics. As an example, this implementation is what is known as <em>embarrassingly parallel</em>, although it probably wouldn't pay to parallelise this implementation (it depends on how big you expect the input to be). </p> <h3 id="b2d9b0e6b2b6434ea890e7f0c7fd4623"> Back-porting the functional approach <a href="#b2d9b0e6b2b6434ea890e7f0c7fd4623" title="permalink">#</a> </h3> <p> While F# is a multi-paradigmatic language with an emphasis on Functional Programming, C# and Visual Basic are multi-paradigmatic languages with emphasis on Object-Oriented Programming. However, they still have some Functional capabilities, so you can back-port the F# approach. Here's one way to do it in C# using LINQ: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Convert(<span style="color:blue;">string</span>&nbsp;text) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">String</span>.Concat(text.Select(AddSkewer)); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;AddSkewer(<span style="color:blue;">char</span>&nbsp;c,&nbsp;<span style="color:blue;">int</span>&nbsp;index) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;s&nbsp;=&nbsp;c.ToString().ToLower(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(index&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:#2b91af;">Char</span>.IsUpper(c)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&quot;-&quot;</span>&nbsp;+&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;s; }</pre> </p> <p> The LINQ Select method is equivalent to F#'s mapi function, and the AddSkewer function must rely on individual conditionals instead of pattern matching, but apart from that, it's structurally the same implementation. </p> <p> You can do the same in Visual Basic: </p> <p> <pre><span style="color:blue;">Function</span>&nbsp;Convert(text&nbsp;<span style="color:blue;">As</span>&nbsp;<span style="color:blue;">String</span>)&nbsp;<span style="color:blue;">As</span>&nbsp;<span style="color:blue;">String</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Return</span>&nbsp;<span style="color:blue;">String</span>.Concat(text.Select(<span style="color:blue;">AddressOf</span>&nbsp;AddSkewer)) <span style="color:blue;">End</span>&nbsp;<span style="color:blue;">Function</span> <span style="color:blue;">Private</span>&nbsp;<span style="color:blue;">Function</span>&nbsp;AddSkewer(c&nbsp;<span style="color:blue;">As</span>&nbsp;<span style="color:blue;">Char</span>,&nbsp;index&nbsp;<span style="color:blue;">As</span>&nbsp;<span style="color:blue;">Integer</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Dim</span>&nbsp;s&nbsp;=&nbsp;c.ToString().ToLower() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">If</span>&nbsp;index&nbsp;=&nbsp;0&nbsp;<span style="color:blue;">Then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Return</span>&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">End</span>&nbsp;<span style="color:blue;">If</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">If</span>&nbsp;<span style="color:blue;">Char</span>.IsUpper(c)&nbsp;<span style="color:blue;">Then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Return</span>&nbsp;<span style="color:#a31515;">&quot;-&quot;</span>&nbsp;+&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">End</span>&nbsp;<span style="color:blue;">If</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">Return</span>&nbsp;s <span style="color:blue;">End</span>&nbsp;<span style="color:blue;">Function</span></pre> </p> <p> Are these back-ported implementations examples of appropriate C# or Visual Basic code? This is where the issue becomes more controversial. </p> <h3 id="b517cf44653e49238d38cafbaac6e995"> Idiomatic or idiosyncratic <a href="#b517cf44653e49238d38cafbaac6e995" title="permalink">#</a> </h3> <p> Apart from reactions such as "that's unreadable," one of the most common reactions I get to such suggestions is: </p> <p> "That's not idiomatic C#" (or Visual Basic). </p> <p> Perhaps, but could it <em>become</em> idiomatic? </p> <p> Think about what an idiom is. In language, it just means a figure of speech, like "jumping the shark". Once upon a time, no-one said "jump the shark". Then Jon Hein came up with it, other people adopted it, and it became an idiom. </p> <p> It's a bit like that with idiomatic C#, idiomatic Visual Basic, idiomatic Ruby, idiomatic JavaScript, etc. Idioms are adopted because they're deemed beneficial. It doesn't mean that the set of idioms for any language is finite or complete. </p> <p> That something isn't idiomatic C# may only mean that it isn't idiomatic yet. </p> <p> Or perhaps it is idiomatic, but it's just an idiom you haven't seen yet. We all have idiosyncrasies, but we should try to see past them. If a language construct is strange, it may be a friendly construct you just haven't met. </p> <h3 id="2ade085bb59942d19689179bdb9913d4"> Constant learning <a href="#2ade085bb59942d19689179bdb9913d4" title="permalink">#</a> </h3> <p> One of my friends once told me that he'd been giving a week-long programming course to a group of professional software developers, and as the week was winding down, one of the participants asked my friend how he knew so much. </p> <p> My friend answered that he constantly studied and practised programming, mostly in his spare time. </p> <p> The course participant incredulously replied: "You mean that there's <em>more</em> to learn?" </p> <p> As my friend told me, he really wanted to reply: “If that's a problem for you, then you've picked the wrong profession." However, he found something more bland, but polite, to answer. </p> <p> There's no way around it: if you want to be (and stay) a programmer, you'll have to keep learning - not only new languages and technologies, but also new ways to use the subjects you already think you know. </p> <p> If you ask me about advice about a particular problem, I will often suggest something that seems foreign to you. That doesn't make it bad, or unreadable. Give it a chance even if it makes you uncomfortable right now. Being uncomfortable often means that you're learning. </p> <p> Luckily, in these days of internet, finding materials that will help you learn is easier and more accessible than ever before in history. There are tutorials, blog posts, books, and video services like <a href="http://bit.ly/1SVvTlS">Pluralsight</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="fcd05856c5764a7d97404d364d8c5496"> <div class="comment-author">Craig Main <a href="#fcd05856c5764a7d97404d364d8c5496">#</a></div> <div class="comment-content"> <p>I would have done this... <pre>Regex.Replace(s, "(?<=.)[A-Z]", _ => "-" + _.Value).ToLower();</pre> </p> </div> <div class="comment-date">2015-08-04 06:01 UTC</div> </div> <div class="comment" id="a519a61dd1e94a9facdcbe9bc0e1bde1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a519a61dd1e94a9facdcbe9bc0e1bde1">#</a></div> <div class="comment-content"> <p> Craig, fair enough :) </p> </div> <div class="comment-date">2015-08-04 06:37 UTC</div> </div> <div class="comment" id="9c14480f924c4394bba3295703aa7f52"> <div class="comment-author"><a href="http://blog.alphasmanifesto.com">Alpha</a> <a href="#9c14480f924c4394bba3295703aa7f52">#</a></div> <div class="comment-content"> <p> In addition to Craig's proposal (which I immediately though of too hehe), think of this one: </p> <pre> const string MATCH_UPPERCASE_LETTERS = "(?<=.)[A-Z]"; // ... Regex.Replace(s, MATCH_UPPERCASE_LETTERS, letterFound => "-" + letterFound.Value).ToLower(); </pre> <p> It probably does not follow the whole conventions of the language, but the great thing about it is that it can be easily understood <em>without knowledge of the language</em>. This brings it to another complete level of maintainability. </p> <p> Of course, the meta-information that you convey through the code is subject to another complete analysis and discussion, since there are way too many options on how to approach that, and while most of us will lean on "readable code", it all comes down to what the code itself is telling you about itself. </p> <p>Finally, as a side note, something that I found quite interesting is that most of the introductory guides to Ruby as a language will talk about how Ruby was design to read like english.</p> <pre> door.close unless door.is_closed? </pre> <p>(based on <a href="http://mislav.uniqpath.com/poignant-guide/book/chapter-3.html">Why's Poignant Guide to Ruby</a>.)</p> <p>While I found that pretty appealing when learning Ruby, some other constructs will take it far away from how English works, pretty much what happened to your example with F#. Not that they cannot be <em>made</em> to read easier, but that extra effort is not always performed.</p> <p>My conclusion is: readability is not about the code or the algorithm (which definitely can help), but about the information that the code itself gives you about what it does. Prefer to use pre-built language functions rather than implementing them yourself.</p> </div> <div class="comment-date">2015-08-04 11:42 UTC</div> </div> <div class="comment" id="386fd14eca2c4ec8af96cdd166b33ae9"> <div class="comment-author"><a href="http://smartflow.wordpress.com">Fendy</a> <a href="#386fd14eca2c4ec8af96cdd166b33ae9">#</a></div> <div class="comment-content"> <p> In under than 30 seconds of seeing the code blocks, I can't follow any of the process. But I know what the operation is doing thanks to the operation name, nerdCapsToKebabCase. Surely that the terms nerdCaps and KebabCase is uncommon, but I only need some minutes to know what are those using internet.</p> <p>So for me, no matter how good you write your code, you can't make it commonly readable by using the code itself. Won't the readability can be enhanced by using comments / documentation? Ex: /* convert thisKindOfPhrase to this-kind-of-phrase */. I've also used unit tests to improve the readability to some extent too. </p> </div> <div class="comment-date">2015-08-05 04:35 UTC</div> </div> <div class="comment" id="575e427b726540e3a2f94b7f5360a5fa"> <div class="comment-author"><a href="https://github.com/oteku">Thomas</a> <a href="#575e427b726540e3a2f94b7f5360a5fa">#</a></div> <div class="comment-content"> <p>To end the loop it may be fair to give a JavaScript function sample which became more idiomatic since 2015 :) <br> ES5 syntax to be consistent with the first example. </p> <pre> var nerdCapsToKebabCase = function (text) { return text.split('') .map( function(c){ return (c === c.toUpperCase()) ? '-' + c.toLowerCase() : c ; }) .join(''); }; </pre> <p>I know you can use String.prototype.replace with a regex in this case but it don't illustrate well the idiom from Imperative to functional (even it's a High-Order Function).</p> </div> <div class="comment-date">2017-08-25 08:14 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Type Driven Development with F# Pluralsight course https://blog.ploeh.dk/2015/07/17/type-driven-development-with-f-pluralsight-course 2015-07-17T16:42:00+00:00 Mark Seemann <div id="post"> <p> <em>The F# type system can do much more than C# or Java's type system. Learn how to use it as a design feedback technique in my new Pluralsight course.</em> </p> <p> Not only can the F# type system help you catch basic errors in your code, but it can also help you by providing rapid feedback, and make suggestions on how to proceed. All you have to do is learn to listen. </p> <p> My new Pluralsight course, <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a>, first walks you through three modules of exploration of a real problem, showing various ways you can take advantage of the type system in an <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-in</a> fashion. As you can tell, it involves some celebration related to Finite State Machines: </p> <p> <img src="/content/binary/type-driven-development-with-fsharp-screen-shot.png" alt="Course screenshot, man and woman cheering over Finite State Machine"> </p> <p> It then shows you how to stabilize the prototype using <a href="https://blog.ploeh.dk/property-based-testing-intro">Property-based Testing</a> with FsCheck. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. REST implies Content Negotiation https://blog.ploeh.dk/2015/06/22/rest-implies-content-negotiation 2015-06-22T09:10:00+00:00 Mark Seemann <div id="post"> <p> <em>If you're building REST APIs, you will eventually have to deal with Content Negotiation.</em> </p> <p> Some REST services support both JSON and XML, in which case it's evident that <a href="https://en.wikipedia.org/wiki/Content_negotiation">Content Negotiation</a> is required. These days, though, more and more services forego XML, and serve only JSON. Does that mean that Content Negotiation is no longer relevant? </p> <p> If you're building <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3 REST APIs</a> this isn't true. You'll still need Content Negotiation in order to be able to evolve your service without breaking existing clients. </p> <p> In a level 3 API, the distinguishing feature is that the service exposes <em>Hypermedia Controls</em>. In practice, it means that representations also contain links, like in this example: </p> <p> <pre>{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;users&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;displayName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Jane&nbsp;Doe&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;user&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/1234&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;friends&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/1234/friends&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;displayName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;John&nbsp;Doe&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;user&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/5678&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;friends&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/5678/friends&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;] }</pre> </p> <p> This representation gives you a list of users. Notice that each user has an array of links. One type of link, identified by the relationship type "user", will take you to the user resource. Another link, identified by the relationship type "friends", will take you to that particular user's friends: another array of users. </p> <p> When you follow the "user" link for the first user, you'll get this representation: </p> <p> <pre>{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;displayName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Jane&nbsp;Doe&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;address&quot;</span>:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;street&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Any&nbsp;Boulevard&nbsp;42&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;zip&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;12345&nbsp;Anywhere&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This user representation is richer, because you also get the user's address. (In a real API, this representation should also have links, but I omitted them here in order to make the example more concise.) </p> <h3 id="febfcb8371d144798a422a9a8fec1e2f"> Breaking changes <a href="#febfcb8371d144798a422a9a8fec1e2f" title="permalink">#</a> </h3> <p> As long as you have no breaking changes, all is good. Things change, though, and now you discover that a user can have more than one address: </p> <p> <pre>{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;displayName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Jane&nbsp;Doe&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;addresses&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;street&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Any&nbsp;Boulevard&nbsp;42&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;zip&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;12345&nbsp;Anywhere&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;street&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Some&nbsp;Avenue&nbsp;1337&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;zip&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;67890&nbsp;Nowhere&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;] }</pre> </p> <p> This is a breaking change. If the service returns this representation to a client that expects only a single "address" property, that client will break. </p> <p> How can you introduce this new version of a user representation without breaking old clients? You'll need to keep both the old and the new version around, and return the old version to the old clients, and the new version to new clients. </p> <h3 id="c4bb0f6f2f25492697284d855eb57b66"> Versioning attempt: in the URL <a href="#c4bb0f6f2f25492697284d855eb57b66" title="permalink">#</a> </h3> <p> There are various suggested ways to version REST APIs. Some people suggest including the version in the URL, so that you'd be able to access the new, breaking representation at <code>"/users/v2/1234"</code>, while the original representation is still available at <code>"/users/1234"</code>. There are several problems with this suggestion; the worst is that it doesn't work well with Hypermedia Controls (links, if you recall). </p> <p> Consider the list of users in the first example, above. Each user has some links, and one of the links is a "user" link, which will take the client to the original representation. This can never change: if you change the "user" links to point to the new representation, you will break old clients. You also can't remove the "user" links, for the same reason. </p> <p> The only way out of this conundrum is to add another link for the new clients: </p> <p> <pre>{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;users&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;displayName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Jane&nbsp;Doe&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;user&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/1234&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV2&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v2/1234&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;friends&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/1234/friends&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;displayName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;John&nbsp;Doe&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;user&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/5678&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV2&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v2/5678&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;friends&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/5678/friends&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;] }</pre> </p> <p> In this way, old clients can still follow "user" links, and updated clients can follow "userV2" links. </p> <p> It may work, but isn't nice. Every time you introduce a new version, you'll have to add new links <em>in all the places where the old link appears</em>. Imagine the 'link bloat' when you have more than two versions of a resource: </p> <p> <pre>{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;users&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;displayName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Jane&nbsp;Doe&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;user&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/1234&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV2&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v2/1234&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV3&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v3/1234&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV4&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v4/1234&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV5&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v5/1234&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;friends&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/1234/friends&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;friendsV2&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/1234/V2/friends&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;displayName&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;John&nbsp;Doe&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2e75b6;">&quot;links&quot;</span>:&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;user&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/5678&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV2&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v2/5678&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV3&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v3/5678&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV4&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v4/5678&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;userV5&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/v5/5678&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;friends&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/5678/friends&quot;</span>&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:#2e75b6;">&quot;rel&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;friendsV2&quot;</span>,&nbsp;<span style="color:#2e75b6;">&quot;href&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;/users/5678/V2/friends&quot;</span>&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;] }</pre> </p> <p> Remember: we're talking about Level 3 REST APIs here. Clients must follow the links provided; clients are <em>not</em> expected to assemble URLs from templates, as will often be the case in Level 1 and 2. </p> <h3 id="3d4b11e620e846c5b88d2856b5375cae"> Versioning through Content Negotiation <a href="#3d4b11e620e846c5b88d2856b5375cae" title="permalink">#</a> </h3> <p> As is often the case, the root cause of the above problem is <em>coupling</em>. The attempted solution of putting the version in the URL couples the identity (the URL) of the resource to its representation. That was never the intent of REST. </p> <p> Instead, you should use Content Negotiation to version representations. If clients don't request a specific version, the service should return the original representation. </p> <p> New clients that understand the new representation can explicitly request it in the Accept header: </p> <p> <pre>GET /users/1234 HTTP/1.1 Accept: application/vnd.ploeh.user+json; version=2</pre> </p> <p> <a href="http://liddellj.com">Jim Liddell</a> has <a href="http://liddellj.com/using-media-type-parameters-to-version-an-http-api">a more detailed description</a> of this approach to versioning. </p> <p> It enables you to keep the user list stable, without link bloat. It'll simply remain as it was. This means less work for you as API developer, and fewer bytes over the wire. </p> <h3 id="bbdff3b6eeeb4414959a455d76e71c3d"> Disadvantages <a href="#bbdff3b6eeeb4414959a455d76e71c3d" title="permalink">#</a> </h3> <p> The most common criticisms against using Content Negotiation for versioning is that it makes the API less approachable. The argument goes that with version information in the URL, you can still test and explore the API using your browser. Once you add the requirement that HTTP headers should be used, you can no longer test and explore the API with your browser. </p> <p> Unless the API in question is an anonymously accessible, read-only API, I think that this argument misses the mark. </p> <p> Both Level 2 and 3 REST APIs utilise HTTP verbs. Unless the API is a read-only API, a client must use POST, PUT, and DELETE as well as GET. This is not possible from a browser. </p> <p> Today, many APIs are secured with modern authentication and authorisation formats like OAuth, where the client has to provide a security token in the Authorization header. This is not possible from a browser. </p> <p> Most APIs will already be impossible to test from the browser; it's irrelevant that versioning via the Accept header also prevents you from using the browser to explore and test the API. Get friendly with curl or Fiddler instead. </p> <h3 id="4269662ebd024c7695dd0d9faa8868be"> Summary <a href="#4269662ebd024c7695dd0d9faa8868be" title="permalink">#</a> </h3> <p> If you want to build a true REST API, you should seriously consider using Content Negotiation for versioning. In this way, you prevent link bloat, and effectively decouple versioning from the identity of each resource. </p> <p> If you've said REST, you must also say Content Negotiation. Any technology you use to build or consume REST services must be able to play that game. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="e99434df14c149b583631fb1e548c4f6"> <div class="comment-author"><a href="http://lookonmyworks.co.uk/">Graham Hay</a> <a href="#e99434df14c149b583631fb1e548c4f6">#</a></div> <div class="comment-content"> <p>For that example, it's only a breaking change because you also removed the old "address" property. It might have been easier to leave that in and populate it with the first address from the collection. Older clients could continue to use that, and newer ones could use the new "addresses" field. Obviously at some point you will need to deal with versioning, but it can be better to avoid it until absolutely necessary.</p> </div> <div class="comment-date">2015-06-30 13:37 UTC</div> </div> <div class="comment" id="c02355c2d6c548d38a1191719d78c63a"> <div class="comment-author">Nik Petroff <a href="#c02355c2d6c548d38a1191719d78c63a">#</a></div> <div class="comment-content"> <p> I've been catching up on REST, and discovered the <a href="http://byterot.blogspot.co.uk/2012/12/5-levels-of-media-type-rest-csds.html">5 levels of media type</a> just a few minutes before reading your post. This keeps it approachable, you can still use your browser to explore, and the higher levels progressively enhance the API for smarter clients. </p> <p> An example can be found <a href="https://github.com/aliostad/m-r">here</a> where Ali Kheyrollahi exposes Greg Young's CQRS sample though a REST inferface. </p> </div> <div class="comment-date">2015-07-01 11:37 UTC</div> </div> <div class="comment" id="ee0550dd3c774d429d381bb88eb19b7b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ee0550dd3c774d429d381bb88eb19b7b">#</a></div> <div class="comment-content"> <p> Graham, thank you for writing. You are completely right: if it's possible to evolve the API without introducing a new version, that's always preferable. Your suggestion for doing that with my particular example demonstrates how it could be done. </p> <p> As you also correctly point out, sooner or later, one will have to deal with a truly breaking change. My purpose of writing this article was to explain how the need to do that implies that content negotiation is required. This is the reason I made up an example of a breaking change; unfortunately, that example didn't convince you that versioning was necessary ;) </p> </div> <div class="comment-date">2015-07-09 12:03 UTC</div> </div> <div class="comment" id="7ee9eea7f07b485d832a19ffa90fac74"> <div class="comment-author">Evan <a href="#7ee9eea7f07b485d832a19ffa90fac74">#</a></div> <div class="comment-content"> <p>I think I would consider going one step further, and mandate a version in the Accept header right from the beginning.</p> <p>i.e., <code>Accept: application/json</code> would return a 406 Not Acceptable HTTP response. You have to use <code>Accept: application/vnd.ploeh.user+json; version=1</code></p> <p>Forces clients to be upgrade friendly to begin with, and easier to retire old versions when they are not being called any more.</p> <p>It strikes me though, that having to know these MIME types for versioning is a bit like needing to know the right incantation to open the magic door. We don't want clients hard-coding the URL to a particular API call as part of HATEOAS, but still need to know the magic MIME type to get an appropriate version of the data.</p> </div> <div class="comment-date">2015-07-16 4:31 UTC</div> </div> <div class="comment" id="7d58fddb88464af4b8f8c34297f92d97"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7d58fddb88464af4b8f8c34297f92d97">#</a></div> <div class="comment-content"> <p> Evan, thank you for writing. As I also <a href="http://liddellj.com/using-media-type-parameters-to-version-an-http-api">wrote in a comment to Jim Liddel's article</a>, I would consider the context before making any hard rules. If you control both API and all clients, requiring specific Accept headers may make managing the entire ecosystem a little easier. </p> <p> On the other hand, if you make an API available to arbitrary clients, you should follow <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> in order to make it easy for client developers to use your API. </p> <p> Ultimately, even if you can somehow solve the problem of not having to know specific MIME types, you'll still need to know how to interpret the representations served by the API. For example, as a client developer, you still need to know that a user representation will have <code>displayName</code> and <code>address</code> properties. As far as I can see, you can push that problem around, but if it can be solved, I've yet to learn how. </p> </div> <div class="comment-date">2015-07-25 17:05 UTC</div> </div> <div class="comment" id="72ca9f5464e843528b6c90a37be526c2"> <div class="comment-author">Ben Brown <a href="#72ca9f5464e843528b6c90a37be526c2">#</a></div> <div class="comment-content"> <p> Mark, I've been doing some preliminary research on API versioning for a personal project and I like this approach. Thanks for the post and accompanying links! </p> <p> I read a few of Jim Liddel's posts and can see that Nancy has this sort of content negotiation baked in. Being more familiar with Web Api, I've been looking for an existing way of doing this using that framework instead. Web Api's support for <a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#constraints">attribute routing</a> seems promising, but as of yet, I haven't seen any examples of this in action using Web Api 2. The flexibility and simplicity of Nancy is really attractive, and I may end up going that route anyway, but I'm hesitant to pick a framework based solely on something like this. Have you come across anything similar for Web API 2? </p> </div> <div class="comment-date">2015-10-10 15:40 UTC</div> </div> <div class="comment" id="f51d062615744605a9f2c9d4ce368fcf"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f51d062615744605a9f2c9d4ce368fcf">#</a></div> <div class="comment-content"> <p> Ben, thank you for writing. Web API has supported Content Negotiation since back when it was still a WCF Web API CTP. Out of the box it supports XML and JSON, but you can also make it support Content Negotiation-based versioning as described in this article. </p> </div> <div class="comment-date">2015-10-13 08:13 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. stringf https://blog.ploeh.dk/2015/05/08/stringf 2015-05-08T05:40:00+00:00 Mark Seemann <div id="post"> <p> <em>stringf is an F# function that invokes any ToString(string) method.</em> </p> <p> F# comes with the built-in <a href="https://msdn.microsoft.com/en-us/library/ee340491.aspx">string</a> function, which is essentially an adapter over <a href="https://msdn.microsoft.com/en-us/library/system.object.tostring.aspx">Object.ToString</a>. That often comes in handy, because it lets you compose functions without having to resort to lambda expressions all the time. </p> <p> Instead of writing this (to produce the string "A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z"): </p> <p> <pre>[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;<span style="color:#a31515;">&#39;Z&#39;</span>]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.ToString())&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">String</span>.concat&nbsp;<span style="color:#a31515;">&quot;,&nbsp;&quot;</span> </pre> </p> <p> You can write this: </p> <p> <pre>[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;<span style="color:#a31515;">&#39;Z&#39;</span>]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;string&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">String</span>.concat&nbsp;<span style="color:#a31515;">&quot;,&nbsp;&quot;</span> </pre> </p> <p> That's nice, but some .NET types provide an overloaded ToString function taking a format string as argument: <ul> <li><a href="https://msdn.microsoft.com/en-us/library/bb346136.aspx">DateTimeOffset.ToString : format:string&nbsp;-&gt;&nbsp;string</a></li> <li><a href="https://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx">DateTime.ToString : format:string&nbsp;-&gt;&nbsp;string</a></li> <li><a href="https://msdn.microsoft.com/en-us/library/dd992632.aspx">TimeSpan.ToString : format:string&nbsp;-&gt;&nbsp;string</a></li> <li><a href="https://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx">Decimal.ToString : format:string&nbsp;-&gt;&nbsp;string</a></li> </ul> and so on. </p> <p> While these methods can be useful, it would be nice to be able to use them as functions, like the <code>string</code> function. The problem, it seems, is that this ToString overload is part of no interface or base class. </p> <h3 id="3272ae5deb9d4ce6883ce2ba588671da"> Statically typed duck typing <a href="#3272ae5deb9d4ce6883ce2ba588671da" title="permalink">#</a> </h3> <p> That's not a problem for F#, because it enables us to do <a href="http://www.atrevido.net/blog/2008/08/31/Statically+Typed+Duck+Typing+In+F.aspx">statically typed duck typing</a>! </p> <p> In this case, you can define a <code>stringf</code> function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">inline</span>&nbsp;stringf&nbsp;format&nbsp;(x&nbsp;:&nbsp;^a)&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;(^a&nbsp;:&nbsp;(<span style="color:blue;">member</span>&nbsp;ToString&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">string</span>)&nbsp;(x,&nbsp;format))</pre> </p> <p> You can use the function like this: </p> <p> <pre>&gt;&nbsp;DateTimeOffset.Now&nbsp;|&gt;&nbsp;stringf&nbsp;<span style="color:#a31515;">&quot;o&quot;</span>;; <span style="color:blue;">val</span>&nbsp;it&nbsp;:&nbsp;string&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2015-05-07T14:24:09.8422893+02:00&quot;</span> &gt;&nbsp;DateTimeOffset.Now&nbsp;|&gt;&nbsp;stringf&nbsp;<span style="color:#a31515;">&quot;T&quot;</span>;; <span style="color:blue;">val</span>&nbsp;it&nbsp;:&nbsp;string&nbsp;=&nbsp;<span style="color:#a31515;">&quot;14:24:18&quot;</span> &gt;&nbsp;DateTime.Now&nbsp;|&gt;&nbsp;stringf&nbsp;<span style="color:#a31515;">&quot;t&quot;</span>;; <span style="color:blue;">val</span>&nbsp;it&nbsp;:&nbsp;string&nbsp;=&nbsp;<span style="color:#a31515;">&quot;14:24&quot;</span> &gt;&nbsp;TimeSpan.FromDays&nbsp;42.&nbsp;|&gt;&nbsp;stringf&nbsp;<span style="color:#a31515;">&quot;c&quot;</span>;; <span style="color:blue;">val</span>&nbsp;it&nbsp;:&nbsp;string&nbsp;=&nbsp;<span style="color:#a31515;">&quot;42.00:00:00&quot;</span> &gt;&nbsp;0.42m&nbsp;|&gt;&nbsp;stringf&nbsp;<span style="color:#a31515;">&quot;p0&quot;</span>;; <span style="color:blue;">val</span>&nbsp;it&nbsp;:&nbsp;string&nbsp;=&nbsp;<span style="color:#a31515;">&quot;42&nbsp;%&quot;</span></pre> </p> <p> Perhaps it would be better to define more domain-specific functions like <code>percent</code>, <code>roundTrippable</code>, <code>time</code>, etc., but it's interesting that such a function as stringf is possible. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functional design is intrinsically testable https://blog.ploeh.dk/2015/05/07/functional-design-is-intrinsically-testable 2015-05-07T06:13:00+00:00 Mark Seemann <div id="post"> <p> <em>TDD with Functional Programming doesn't lead to test-induced damage. Here's why.</em> </p> <p> Over the years, there's been much criticism of Test-Driven Development (TDD). Perhaps <a href="http://david.heinemeierhansson.com">David Heinemeier Hansson</a> best condensed this criticism by claiming that <a href="http://david.heinemeierhansson.com/2014/test-induced-design-damage.html">TDD leads to test-induced design damage</a>. This isn't a criticism you can just brush away; it hits a sore point. </p> <p> Personally, I don't believe that TDD <em>has</em> to lead to test-induced damage (not even in Object-Oriented Programming), but I'm the first to admit that <a href="/2010/12/22/TheTDDApostate">it's not a design methodology</a>. </p> <p> In this article, though, you're going to learn about the fundamental reason that TDD with <em>Functional Programming</em> doesn't lead to test-induced damage. </p> <p> In Functional Programming, the ideal function is a <a href="http://en.wikipedia.org/wiki/Pure_function">Pure function</a>. A Pure function is a function that always returns the same value given the same input, and has no side-effects. </p> <h3 id="a20359945e5c406a8dbf3bd692dc2a1b"> Isolation <a href="#a20359945e5c406a8dbf3bd692dc2a1b" title="permalink">#</a> </h3> <p> The first characteristic of a Pure function means that an ideal function can't depend on any implicit knowledge about the external world. Only the input into the function can influence the evaluation of the function. </p> <p> This is what <a href="http://jessitron.com">Jessica Kerr</a> <a href="http://www.functionalgeekery.com/episode-8-jessica-kerr">calls <em>Isolation</em></a>. A function has the property of Isolation when the only information it has about the external word is passed into it via arguments. </p> <p> You can think about Isolation as the <em>dual</em> of Encapsulation. </p> <p> In Object-Oriented Programming, <a href="http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29">Encapsulation</a> is a very important concept. It means that while an object contains state, the external world doesn't know about that state, unless the object <strong>explicitly makes it available</strong>. </p> <p> In Functional Programming, a function is Isolated when it knows nothing about the state of the external world, unless it's <strong>explicitly made available to it</strong>. </p> <p> A Pure function, the ideal of Functional Programming, is Isolated. </p> <h3 id="55f5f9040e4b434cad839390ec1f038c"> Unit testing <a href="#55f5f9040e4b434cad839390ec1f038c" title="permalink">#</a> </h3> <p> Why is this interesting? </p> <p> It's interesting if you start to think about what unit testing means. There are tons of conflicting definitions of what exactly constitutes a unit test, but most experts seem to be able to agree on this broad definition: <blockquote> A unit test is an automated test that tests a unit in isolation from its dependencies. </blockquote> Notice the use of the word <em>Isolation</em> in that definition. In order to unit test, you'll have to be able to isolate the unit from its dependencies. This is the requirement that tends to lead to Test-Induced Damage in Object-Oriented Programming. While there's nothing about Encapsulation that explicitly states that it's forbidden to isolate an object from its dependencies, it offers no help on the matter either. Programmers are on their own, because this concern isn't ingrained into Object-Oriented Programming. </p> <p> <img src="/content/binary/encapsulation-isolation-venn.png" alt="Venn diagram showing that while there's an intersection between Encapsulation and Isolation, it's only here that Object-Oriented Programming is also testable."> </p> <p> You can do TDD with Object-Oriented Programming, and as long as you stay within the intersection of Encapsulation and Isolation, you may be able to stay clear of test-induced damage. However, that zone of testability isn't particularly big, so it's easy to stray. You have to be very careful and know what you're doing. Not surprisingly, many books and articles have been written about TDD, including <a href="/tags.html#Unit Testing-ref">quite a few on this blog</a>. </p> <h3 id="05e982d1b87b46e6a4b963ac8280c99b"> The best of both worlds <a href="#05e982d1b87b46e6a4b963ac8280c99b" title="permalink">#</a> </h3> <p> In Functional Programming, on the other hand, Isolation is the ideal. An ideal function is already isolated from its dependencies, so no more design work is required to make it testable. </p> <p> <img src="/content/binary/ideal-function-isolation-testability-stacked-venn.png" alt="Stacked Venn diagram that show that an ideal function is a subset of isolated functions, which is again a subset of testable functions."> </p> <p> Ideal Functional design is not only ideal, but also perfectly testable, so there's no conflict. This is the underlying reason that TDD doesn't lead to test-induced damage with Functional Programming. </p> <h3 id="f042d2da84424c76a424b70faea121c9"> Summary <a href="#f042d2da84424c76a424b70faea121c9" title="permalink">#</a> </h3> <p> Isolation is an important quality of Functional Programming. An ideal function is Isolated, and that means that it's intrinsically testable. You don't have to tweak any design principles in order to make a function testable - in fact, if a function isn't testable, it's a sign that it's poorly designed. Thus, <strong>TDD doesn't lead to Test-Induced Damage in Functional Programming</strong>. </p> <p> If you want to learn more about this, as well as see lots of code examples, you can watch my <a href="https://blog.ploeh.dk/tdd-with-fsharp">Test-Driven Development with F#</a> Pluralsight course. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e1992711a2074e068fd3372e65cddbf7"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#e1992711a2074e068fd3372e65cddbf7">#</a></div> <div class="comment-content"> <blockquote> A Pure function is a function that always returns the same value given the same input, and has no side-effects. </blockquote> <p> What do you mean by "value"? Can an exception instance be a value? More specifically, would you say that the C# function <code>int Foo() =&gt; new Exception();</code> is pure? </p> <p> Many of your posts mention pure funcitons and at least a few of them include your own definition. I decided to comment on this post since it was the oldest post I found that included your own definition of a pure function. </p> </div> <div class="comment-date">2020-03-06 22:50 UTC</div> </div> <div class="comment" id="8357b5ab8ada4f44bf17dcacd9b9b079"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8357b5ab8ada4f44bf17dcacd9b9b079">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I don't think that <code>int Foo() =&gt; new Exception();</code> compiles... </p> <p> Apart from that, how do you find that this is my own definition of a pure function? It seems to me to be a standard and non-controversial definition. I even <a href="https://en.wikipedia.org/wiki/Pure_function">link to the Wikipedia definition</a> in the beginning of the article. </p> </div> <div class="comment-date">2020-03-07 8:49 UTC</div> </div> <div class="comment" id="f7819c9f3a304c86a4f14b2833674a34"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#f7819c9f3a304c86a4f14b2833674a34">#</a></div> <div class="comment-content"> <blockquote> Apart from that, how do you find that this is my own definition of a pure function? It seems to me to be a standard and non-controversial definition. I even <a href="https://en.wikipedia.org/wiki/Pure_function">link to the Wikipedia definition</a> in the beginning of the article. </blockquote> <p> I am not trying claim that any particular definition of a pure function is non-standard or is controversial. I also don't mean that the text I quoted is "your definition" in the sense that it semantically differs from the one on Wikipedia. I just mean that it is "your definition" in the sense that you have syntactically included in your post the text that I quoted. </p> <p> However, I am unsure about the precise meaning the defintion for a pure function that you have syntactically included in your post and that I quoted. To help me improve my understanding of that defintion, I tried to ask you if a particular C# function is pure. </p> <blockquote> I don't think that <code>int Foo() =&gt; new Exception();</code> compiles... </blockquote> <p> Ah, yes. Thanks for alerting me to my mistake. I meant to include the <code>throws</code> keyword as well. For clarity, I now repeat that whole paragraph but with the prose "thrown" and the keyword <code>throws</code> added. </p> <p> What do you mean by "value"? Can a thrown exception instance be a value? More specifically, would you say that the C# function <code>int Foo() =&gt; throws new Exception();</code> is pure? </p> </div> <div class="comment-date">2020-03-07 12:27 UTC</div> </div> <div class="comment" id="97e4ae29436e4828813bf445ee1c37dc"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#97e4ae29436e4828813bf445ee1c37dc">#</a></div> <div class="comment-content"> <p> Tyson, your code still doesn't compile, but I think I understand the question 😜 </p> <p> Yes, <code>int Foo() =&gt; throw new Exception();</code> is still a pure function, but it isn't <em>total</em>. Rather, it's a <a href="https://en.wikipedia.org/wiki/Partial_function">partial function</a>. This is an independent quality of functions. </p> <p> Purity relates to determinism and the lack of side effects. A total function, on the other hand, is a function that returns a proper value for every possible value in its <em>domain</em>. What do I mean by <em>proper?</em> </p> <p> There's two ways in which a function can fail to return a value. One is if the function never returns. Due to the <a href="https://en.wikipedia.org/wiki/Halting_problem">halting problem</a> there's no general-purpose way to determine whether or not this is the case for a Turing-complete language. </p> <p> Another way a function can fail to return a value is if it throws an exception. Most languages (even Haskell!) allows exception-throwing. This isn't considered a 'proper' value because, using the type system, you declared that <code>Foo</code> returns an <code>int</code>. It doesn't. It 'returns' an exception. </p> <p> Both non-termination and exceptions are typically considered a special value termed <em>bottom</em>, often written with the symbol <code>⊥</code>. </p> <p> Functions can be pure, but partial. Your <code>Foo</code> function is an example of that. The holy grail in statically typed functional programming is pure and total functions. It's up to the programmer to provide the totality guarantee, though, since the type system can't enforce termination (due to the halting problem). You can, on the other hand, easily program without exceptions once you get the hang of it. </p> </div> <div class="comment-date">2020-03-07 13:21 UTC</div> </div> <div class="comment" id="eacb540e91fb47e6bf6296c520522aaa"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#eacb540e91fb47e6bf6296c520522aaa">#</a></div> <div class="comment-content"> <p> Thanks for seeing pass my second compile error and understanding my question. </p> <p> Ah, yes. I definitely know about partial and total functions from my experience with mathematics, and I am pretty sure I have previously called a function that throws an exception partial, but I completely forgot about this connection. (I think that is because I have so focused on purity.) Thank you for bringing this concept (back) to my attention. </p> <blockquote> [The definition of a pure funciton in question] seems to me to be a standard and non-controversial definition. </blockquote> <p> I was asking about pure functions and exception throwing because I was thinking about the definition for a pure function given by Enrico Buonanno in <a href="https://www.manning.com/books/functional-programming-in-c-sharp">Functional Programming in C#</a>. He considers throwing an exception a side effect and includes this paragraph about this decision. </p> <blockquote> Some will argue that a function can be considered pure despite throwing exceptions. However, in throwing exceptions it will cause indeterminism to appear in code that makes some decisions based on exception handling, or in the absence of exception handling, in the side effect of the program crashing. </blockquote> <p> What do you think about Enrico's choice to define exception throwing as a side effect? </p> <p> It might be worth considering <code>async void Foo() =&gt; throw new Exception();</code> because it produces an unhandled exception, which crashes the executing process. </p> </div> <div class="comment-date">2020-03-09 03:59 UTC</div> </div> <div class="comment" id="7bdf9f1597334a40afe4b8488456d7b2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7bdf9f1597334a40afe4b8488456d7b2">#</a></div> <div class="comment-content"> <p> I haven't seen anyone take that position before, so I can only evaluate it based on what you wrote. With that limited context, however, I don't find the argument convincing. First, that throwing exceptions will cause indeterminism to appear in code that handles exceptions says nothing about the function that throws the exception. It says something about the code that handles the exception. </p> <p> Making decisions based on data is itself not non-deterministic. If it was, <code>if/else</code> blocks or pattern matching couldn't be pure. If the exception handler does something impure while handling an exception, then it's just an impure action. The <a href="/2018/11/19/functional-architecture-a-definition">functional interaction law</a> explicitly allows this. </p> <p> Keep in mind that the definition of purity that we're discussing is really only a checklist to figure out whether a function is <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>. That's the core definition: Can you replace a function call with its result? </p> <p> Yes, if the function is pure. This includes a function that throws an exception. It basically just returns <code>⊥</code>. If you have code that handles the exception, it'll do that based on the exception that was thrown. It doesn't really matter if the function 'actually executed' or not. We can replace the function call with the <em>bottom</em> value. </p> <p> If you don't handle the exception, then yes: the program crashes. It'll do so, however, regardless of whether you 'run' the function, or you just replace it with a thrown exception. </p> </div> <div class="comment-date">2020-03-11 18:50 UTC</div> </div> <div class="comment" id="e118a437e8a84a9d95bd117bbe597084"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#e118a437e8a84a9d95bd117bbe597084">#</a></div> <div class="comment-content"> <p> Yes, this helps. I agree with you. Thanks for your explanation. </p> <blockquote> Due to the <a href="https://en.wikipedia.org/wiki/Halting_problem">halting problem</a> there's no general-purpose way to determine whether or not this is the case for a Turing-complete language.<br> ...<br> It's up to the programmer to provide the totality guarantee, though, since the type system can't enforce termination (due to the halting problem). </blockquote> <p> The respective problems of deciding if a given function is total or pure are equally difficult; both are undecidable by <a href="https://en.wikipedia.org/wiki/Rice%27s_theorem">Rice's theorem</a>. A compiler for Haskell is not a general-purpose algorithm for deciding the purity of a function. It follows from the syntax of Haskell that all functions in Haskell with a return type different from the IO monad are pure (and technically all the others as well). Rice's theorem doesn't apply when the property being checked is syntactic. </p> <p> In the same way, it is possible to design a programming language with two contexts: one in which partial functions can be defined and another in which only total functions can be defined. As before, the partial function context could be expressed by the syntactic requirement that the return type is some monad. </p> </div> <div class="comment-date">2020-03-11 21:08 UTC</div> </div> <div class="comment" id="34436c76e8274a0db7d28d70ee1f2321"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#34436c76e8274a0db7d28d70ee1f2321">#</a></div> <div class="comment-content"> <p> I'm not familiar with Rice's theorem, so I'll have to take your word on that. Haskell, however, seems to be doing a fairly good job of distinguishing between pure and impure, but this could be because the impure actions ultimately aren't implemented in Haskell (IIRC, they're written in C or C++). This might be analogous to the following escape hatch for partiality versus totality. </p> <p> You can't define a <em>Turing-complete</em> language where you generally distinguish between total and partial functions. That's what Turing, Gödel, and Church proved in the 1930's. The escape hatch is that if you define a language that's <em>not</em> Turing-complete, you can distinguish between total and partial functions. If I remember correctly, that's the underlying design philosophy of <a href="https://www.idris-lang.org">Idris</a>. I believe that Edwin Brady once called the concept <em>Pac-Man-complete</em>; while not Turing-complete, he was aiming for a language powerful enough that you could still implement Pac Man in it. I do believe that Idris also comes with an option where you can escape into the wider, Turing-complete part of the language by giving up on the compiler checking of totality versus partiality. </p> </div> <div class="comment-date">2020-03-11 21:47 UTC</div> </div> <div class="comment" id="5958a2e4ad61481782da313906bfe81e"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#5958a2e4ad61481782da313906bfe81e">#</a></div> <div class="comment-content"> <p> Ah, great. Your comment motivated me to read more about Idris, and I have learned some things. </p> <p> First, Idris includes a totality checker for functions. By default, a function is not checked for totality. Putting the keyword <code>total</code> above a function defintion enables the checker. Here are <a href="https://en.wikipedia.org/wiki/Idris_(programming_language)#Dependent_types">some examples</a> of this. The code then compiles if and only if the checker (which is essentially a theorem prover) is able to prove that the function is total. So the two contexts are distinguished by the presence or absence of the keyword <code>total</code>. This is a completely different approach to creating the two contexts than what I was vaguely suggesting. </p> <p> Second, Idris is Turing-complete as <a href="https://cs.stackexchange.com/questions/19577/what-can-idris-not-do-by-giving-up-turing-completeness/23916#23916">confirmed by Edwin Brady himself</a>. Furthermore, both Edwin and <a href="http://adam.chlipala.net/cpdt/html/Coinductive.html">this page about coinductive</a> that he cites say (paraphrased into my words) that one could also separate the partial and total contexts using a monad. Though the linked page goes onto say that "this is a heavyweight solution, and so we would like to avoid it whenever possible." </p> <p> I am really glad to have learned those things. Thanks for the great conversation :) </p> </div> <div class="comment-date">2020-03-12 03:25 UTC</div> </div> <div class="comment" id="dd76d156c25b4b8e99b9018f3aa2939e"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#dd76d156c25b4b8e99b9018f3aa2939e">#</a></div> <div class="comment-content"> <p> Would you say that <code>int Foo() { if (new Random().Next() % 2 == 0) throw new Exception(); else throw new NotImplementedException(); }</code> is pure? </p> </div> <div class="comment-date">2020-03-17 03:44 UTC</div> </div> <div class="comment" id="904c09a013c144e48a7d322e75f70d94"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#904c09a013c144e48a7d322e75f70d94">#</a></div> <div class="comment-content"> <p> No, that variation of <code>Foo</code> is impure because of the non-deterministic behaviour. You can't replace a call to <code>Foo</code> with a corresponding <em>bottom</em> value. </p> </div> <div class="comment-date">2020-03-17 7:27 UTC</div> </div> <div class="comment" id="644b577fc60c46b58349d8878c19bbac"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#644b577fc60c46b58349d8878c19bbac">#</a></div> <div class="comment-content"> <p> Idris is very interesting. Have you seen attempts to test totality in other languages? </p> </div> <div class="comment-date">2022-05-21 00:43 UTC</div> </div> <div class="comment" id="aee72ce959654d9388b448023f469cbc"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#aee72ce959654d9388b448023f469cbc">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. Usually, the names <a href="https://en.wikipedia.org/wiki/Agda_(programming_language)">Agda</a> and <a href="https://en.wikipedia.org/wiki/Coq">Coq</a> are thrown around when Idris is mentioned. As far as I understand things, however, both Agda and Coq are mostly intended to be additional tools that you use together with another language, whereas the vision for Idris is to be a complete programming language. I could be wrong, though. It's a rabbit hole that I've yet to explore. </p> <p> Another language worth mentioning might be <a href="https://en.wikipedia.org/wiki/F*_(programming_language)">F*</a>, but again, I know little about it, and have never written a line of code in it. </p> </div> <div class="comment-date">2022-05-21 5:58 UTC</div> </div> <div class="comment" id="2c0403d1e69d43a496ee04c16bea282c"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#2c0403d1e69d43a496ee04c16bea282c">#</a></div> <div class="comment-content"> <p> Thank you for the leads! These languages are very interesting. I'd never heard of "proof-oriented programming" or depdendant types before. It's going to a take a while to unpack these new approaches. </p> <p> I notice all these languages take a proof-based approach to verifying totality. I'm curious about your take on a possible experimental approach. </p> <p> I've been trying to meld ideas from <a href="https://clojure.org/about/spec">Clojure.spec</a> into a type-driven approach in F#, in line with Scott Wlaschin's <a href="https://fsharpforfunandprofit.com/series/designing-with-types/">Designing with Types</a> and your <a href="https://blog.ploeh.dk/2016/02/10/types-properties-software/">Types + Properties = Software</a>. Clojure Spec, in case you're unfamiliar, is the Specification pattern as an optional type system. Types are built up through combinations of constraints (e.g. InventoryCount must be an int of at least 0 and no more than 10000). Spec can then automatically test type-annotated functions to verify that any input that fits constraints produces output that fits constraints. It's basically property testing with generators automatically created from the in-code type specifications. </p> <p> I think this kind of testing is effectively a measure of totality; a measure of how well a function's actual domain matches its advertized domain. Clojure does not make that claim, and I haven't found a similar approach from another language or framework yet. </p> <p> While less thorough than proofs, such a technique could be applied to most any system with static typing through meta-programming and conventions to find type constraints (i.e. via factory functions). The proportion of inputs that error or timeout could be a consistent measure for migrating systems to more total, and less surprising, functions. </p> <p> Thoughts? </p> </div> <div class="comment-date">2022-05-23 2:12 UTC</div> </div> <div class="comment" id="9e0edebafc604154b4472d0a3d978894"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9e0edebafc604154b4472d0a3d978894">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. While I know that Clojure.spec exists, I don't know more about it than what you've described. It's not quite the same, but it sounds not too dissimilar from <a href="http://hackage.haskell.org/package/QuickCheck">QuickCheck</a> <code>Arbitrary</code> instances. They don't describe predicates, but they do implement (pseudo-random) generators of values. Those generators then drive data generation for property-based tests. </p> <p> Parametrised tests (including properties) are essentially predicates. They take input and return a binary result. Granted, the result isn't <em>true</em> or <em>false</em>, but rather <em>pass</em> or <em>fail</em>, but I hope it's evident that these are isomorphic. </p> <p> How does that relate to testing totality? It's better than nothing, but in practice we must suffer the reality of combinatorics. You can easily use brute force to prove total a function that takes a single Boolean value as input. A byte input requires 256 test cases, so that's also within the realm of the possible. Many input types, however, <a href="/2021/11/15/types-as-sets">represent conceptually infinite sets</a>. A string, for example, represents a conceptually infinite set. </p> <p> Imagine an adversarial function that loops forever if the input string is a very particular value. This value might be a one-million-character string generated randomly at compile time. The odds that any random test case generator ever hits that particular value are effectively nil. </p> <p> While I like property-based testing a lot, I don't consider it a measure of totality. If the domain of a function is infinite, it makes little difference if you exercise a hundred or a million test cases - you've still covered practically zero percent of the domain. </p> </div> <div class="comment-date">2022-05-24 6:27 UTC</div> </div> <div class="comment" id="12fc077b72914a93828887a2c17a9ccc"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#12fc077b72914a93828887a2c17a9ccc">#</a></div> <div class="comment-content"> <p> You make an excellent point. If I understand correctly, many domains are effectively infinite so coverage of random testing is effectively negligable. </p> <p> My connection to totality came from trying to understand what Clojure spec's instrument function is really testing. It's a property, but the property only knows the shape of the domain and range. It knows nothing about relationships between specific input and output values. Therefore, it doesn't verify business domain correctness. Rather, it's checking that the range holds true for a sampling of valid input values. </p> <p> In light of your insight, it may be more fair to say this is a coverage "surprise" check. It can run common edge cases and input scenarios to make sure they return values from the advertized range rather than exceptions (or fail to terminate). As you discerned, it cannot generally detect adversarial implementations that would violate totality. </p> <p> Thank you for taking the time to consider this idea and share your expertise! </p> </div> <div class="comment-date">2022-05-24 14:22 UTC</div> </div> <div class="comment" id="50ebefc5e650494fa5401e03dc7f50d9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#50ebefc5e650494fa5401e03dc7f50d9">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. If I understand your description of Clojure.spec correctly, this makes sense in a dynamically typed language like Clojure. It's seems reminiscent of <a href="https://en.wikipedia.org/wiki/Fuzzing">fuzz testing</a> in the sense that you throw arbitrary input at a System Under Test and then monitor what happens. This makes sense in a system where input is mostly (or exclusively) checked at run time. </p> <p> In a language like Haskell, on the other hand, that kind of testing is generally impossible. You can't just throw random, unconstrained input at a Haskell function, because if the input doesn't type-check, the code will not even compile. Languages like C# and Java seem to fall more on that side than on the Clojure side, even though <a href="/2019/12/16/zone-of-ceremony">their types systems involve more ceremony</a>. </p> <p> To be clear: You could imagine something akin to a 'totality check' even in Haskell. After all, we know from the papers of Church, Gödel, and Turing that no type system can prove arbitrary Turing-complete code total (i.e. the halting problem). Even with the best-designed Haskell API, one might conceivably want to write a QuickCheck property that throws random (typed) input at a function only to assert that it returns a value. In essence, this would be an assertion-free test. The implicit assertion is that if the function returns without exceptions or timing out, then it may be total. </p> <p> While one could do that, once you've gone through the trouble of writing such a property, you can often think of some sort of explicit assertion to write. If so, why not do that? But if you do, you've now ventured into the territory of genuine property-based testing. There's nothing wrong with that - quite the contrary - but I think it explains why you tend to not see the above kind of totality checking in statically typed languages. </p> <p> Another point to consider is this: With a decent static type system (and I count C# in that category), you can often design APIs so that the types take care of most concerns regarding totality. It helps to realise that in most languages, functions can be partial in two ways: </p> <ul> <li>A function may throw an exception</li> <li>A function may never terminate</li> </ul> <p> You can usually eliminate the first kind of partiality by carefully choosing input and output types. Thus, in a statically typed language, you're often left with the question: Will this function always terminate? </p> <p> While the halting problem says that there's no <em>general</em> algorithm that will answer that question, it's often obvious for specific functions. For instance, if a function has no recursion or looping, it obviously will terminate. This, to be clear, involves 'white-box' analysis. You have to look at the code and convince yourself that the function will terminate. </p> <p> I've possibly strayed off your original agenda, but I found your comments and responses inspiring. I hope you or someone else find these notes useful. </p> </div> <div class="comment-date">2022-05-26 11:15 UTC</div> </div> <div class="comment" id="a90e947d377348a6abb64aa3d3a7e3fa"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#a90e947d377348a6abb64aa3d3a7e3fa">#</a></div> <div class="comment-content"> <p>I'm glad you've found this line of thought interesting! I've definitely enjoyed and benefitted from our discourse.</p> <p>I agree this kind of testing bears a strong resemblance to fuzzing and that static types eliminate the majority of invalid inputs up front.</p> <p>I'm going to dig deeper into my journey to see if it can explain why I think these tests are valuable.</p> <p>It may not be clear, but the best thing about Clojure Spec's fuzz-like tests is that they don't have to be written at all. All type information is present in the type annotation, including primitive constraints like bounded integers. An introspective function can generically infer generators and assertions from type annotations to create the tests. All annotated functions get these fuzz tests for free.</p> <p>In static languages, we get the structural component of these tests for free from the compiler. However, we're still missing out on verifying constrained values.</p> <p>Approaches to combat primitive obsession go a long way toward eliminating such errors and defensive programming from our domains by centralizing validation and coupling it to a type.</p> <p>Scott Wlaschin demonstrates constrained types <a href="https://fsharpforfunandprofit.com/posts/designing-with-types-single-case-dus/">using unions</a></p> <pre> <code class="language-fsharp"> type String50 = private String50 of string module String50 = let create str = if String.IsNullOrEmpty(str) then None elif String.length str > 50 then None else Some (String50 str) </code> </pre> <p>More traditionally, it'd be a constructor</p> <pre> <code class="language-cs"> class String50 { private string value; public String50(string str) { if (String.IsNullOrEmpty(str)) { throw new ArgumentNullException("str"); } else if (str.Length > 50) { throw new ArgumentException("String50 cannot be longer than 50 characters"); } else { this.value = str; } } } </code> </pre> <p>These approaches implement constrained types as a pattern rather than a language construct. A programmer could undermine them by forgetting to keep constructors private, adding new constructors, assigning via reflection, etc.</p> <p>I spent a while trying to figure out how to add constrained values to F#'s types system and understand what it could do for us. It ultimately failed, but you can check out my attempts and my reasoning in the <a href="https://github.com/farlee2121/fsspec">github repo</a>. This was prior to me learning about dependent type systems, which are the more mature realization of my line of thought.</p> <p>I <a href="https://github.com/farlee2121/FsSpec/blob/main/doc/2022-04-20%20Test-only%20approaches.html">later realized</a> that the constraints don't need to be part of the type to be useful. The constraints are represented in the code as factories or constructors. The code itself will enforce the expectations at runtime. At test time it's ok to rely on slow introspective approaches to identify constraints. In that way, we too have all the information we need to mechanically property test a system just like clojure spec does. In fact, we can test every function in the system, intentionally constrained or not, because static languages require typing.</p> <p>What do these tests do? Like with spec, we can derive some confidence that our system handles the values it says it does. Though not total certainty, which requires a white-box approach as you mentioned.</p> <p>If a system is migrating from heavy use of exceptions for defensive programming, or from general lack of defensive programming, then this kind of test can track progress away from exceptions.</p> <p>It can also help consistency of our constrained types. For example, we can always select the most weakly constrained factory or constructor. This prevents accidental public constructors (which, admittedly, could also be accomplished with an annotation and a simple analyzer). We can also pressure towards conventions like naming the factory a certain way, or only using results over exceptions for communicating errors from factories.</p> <p>As a side benefit, we can also leverage the introspected generators to write our own properties. This reduces duplicated knowledge of constraints between the system and tests, keeping them in line automatically.</p> <p>Thanks for reading this far. This is all pretty conceptual at this point. I hope it's fun food for thought.</p> </div> <div class="comment-date">2022-05-26 17:02 UTC</div> </div> <div class="comment" id="572fccaa3486484fa355b47c4cb6bf33"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#572fccaa3486484fa355b47c4cb6bf33">#</a></div> <div class="comment-content"> <p> A bit more rumination and I realized my thoughts above can be summarized in a few key points </p> <ul> <li>Clojure Spec tests I refer to aren't written, they're programmatically inferred from code</li> <li>Static languages encode all the information needed to programmatically generate and assert constrained value types like Clojure Spec, it's just that part of information lives in functions rather than types.</li> <li>Constrained values are effectively a design pattern in most static languages. The language can't guarantee a consistent approach or consistent application, so there's value in adding natural pressures to be consistent. It's kind of like how you once explained the <a href="https://blog.ploeh.dk/2012/11/06/WhentouseaDIContainer/">value of DI containers</a> </li> <li> These tests can smoke out defensive programming issues and misadvertized function signatures. This is only generalizable if we can differentiate expectable failures from unintended issues, thus why these tests mostly measure movement away from exceptions. </li> </ul> Please let me know if you'd like anything clarified. These are far from fully formed ideas. </div> <div class="comment-date">2022-05-26 18:16 UTC</div> </div> <div class="comment" id="5c4d67c21b7048f2a423dc67f2d04caa"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5c4d67c21b7048f2a423dc67f2d04caa">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. I already got the sense that the Clojure.spec tests weren't written, but thank you for confirming that. I don't know the implementation details of how that works, but I may have an inkling. </p> <p> Clojure is, as I'm sure that you're aware, <a href="https://en.wikipedia.org/wiki/Homoiconicity">homoiconic</a>. Thus, I suppose that the specs are written in Clojure as well. A function or macro can extract the details of each spec and generate test data that are likely to fit. </p> <p> Consider, as a though experiement, a type like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Narrows</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">long</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">Narrows</span>(<span style="color:blue;">long</span>&nbsp;<span style="color:#1f377f;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;Maybe&lt;Narrows&gt;&nbsp;<span style="color:#74531f;">TryCreate</span>(<span style="color:blue;">long</span>&nbsp;<span style="color:#1f377f;">candidate</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">if</span>&nbsp;(candidate&nbsp;&lt;&nbsp;2_993_329_992&nbsp;||&nbsp;3_001_322_003&nbsp;&lt;&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;Narrows&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#8f08c4;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;Maybe&lt;Narrows&gt;(<span style="color:blue;">new</span>&nbsp;Narrows(candidate)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If this was, instead, Clojure, then from perusing the <a href="https://clojure.org/guides/spec">Clojure.spec documentation</a> it looks as though you'd use a predefined specification called <code>int-in</code>, but even if you'd be using normal less-than and greater-than operators, I suppose it wouldn't be beyond the abilities of a sophisticated function to tease apart such a predicate and compose a useful generator. </p> <p> In languages like C#, F#, Java, or Haskell, however, predicates and types are typically black boxes. For the <code>Narrows</code> class, you have a static factory method. This factory method is total, but if you throw random 64-bit integers at it, the chance of ever getting a populated Maybe value is vanishingly small. </p> <p> If you now have a function that takes that type as input, you can't really exercise it because of the statistics involved. This is why most property-based testing frameworks that I've seen (<a href="https://hackage.haskell.org/package/QuickCheck">QuickCheck</a>, <a href="https://hedgehog.qa/">Hedgehog</a>, <a href="https://fscheck.github.io/FsCheck/">FsCheck</a>, <a href="https://github.com/AnthonyLloyd/CsCheck">CsCheck</a>) are based on a <em>generator</em> API (usually called either <code>Arbitrary</code> or <code>Gen</code>). </p> <p> While you can compose a type-specific generator from other generators, you have to explicitly do that work. The testing framework can't infer the set of valid values to use. </p> <p> I'm not disagreeing with what you wrote. I'm only trying to outline how I see such efforts interact with the capabilities of various languages. </p> </div> <div class="comment-date">2022-05-27 15:10 UTC</div> </div> <div class="comment" id="09b276f0717b445f953b64be22e36790"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#09b276f0717b445f953b64be22e36790">#</a></div> <div class="comment-content"> <p> If I interpret this correctly, you're saying languages like C# and similar would have to throw random values at the Narrows TryCreate function to find instances. However, Clojure could use its macro system to understand the functions symbolically and semantically. </p> <p> I think that languages like C# and F# can also semantically analyze code, but it has to be done through the compiler platforms (i.e. Roslyn, FSharp Compiler Services) or similar meta-programming. These are performance heavy, not every language has one, and only work when we have access to code, but they do make semantic analysis possible for functions. </p> <p> We could use the compiler platform to parse constraints from factories. For example, analyze Narrows.TryCreate to find comparison operators, and that those comparisons lead to a "None" value. The other conditional branch is "Some", thus Narrows requres a long greater than 2_993_329_992 and less than 3_001_322_003. Then we use the inferred constraints to create typical property test generators and arbitraries. </p> <p> However, I think your musing from a few posts back are now clicking better. You mused that static languages can effectively eliminate exception-based partiality through careful choice of types. I see two main categories of exceptions <ul> <li> <em>Exceptions intentionally used to assert function pre- and post-conditions</em> -> Shouldn't be needed when using constrained types like Narrows alongside results and options. Limiting unsanctioned use of exceptions for assertions may be easier to solve with a simple static analyzer. </li> <li> <em>Exceptions from mishandled data</em> -> These should be caught by the properties we should already be writing. As you pointed out, the spec-like assumptions are essentially a weaker form of normal property tests. And, unlike dynamic structural languages, we have the compiler to verify functions compose together correctly. </li> </ul> </p> <p> This is really interesting. Thank you for pushing my thinking. </p> </div> <div class="comment-date">2022-05-27 19:07 UTC</div> </div> <div class="comment" id="9209b510035645399e2e720228af9271"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9209b510035645399e2e720228af9271">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. I didn't meant to imply that we should make a general-purpose code analyser - this seems to me to move perilously close to the halting problem; that is, I don't think you this is generally possible. (I could be wrong, though, I haven't thought deeply about this.) </p> <p> What I meant was more that since Clojure.spec is written in Clojure, and Clojure code is also data, it's fairly easy to search a spec for easy-to-detect predicates. Something like a specification is fair game for analysis exactly because of the role it plays. I wouldn't contemplate analysing the function <em>body</em> of a Clojure function like that. </p> <p> In .NET a typical approach to these kinds of problems is to use attributes. There's been quite a few attempts at that over the years - <a href="/2011/05/27/DesignSmellRedundantRequiredAttribute">I've previously engaged with that kind of design</a>, but in summary, I'm not a fan. It does, however, provide some metadata that's fair game for analysis. </p> <p> The distinction, then, seems more related to the nature of the languages. With a language like C#, if you need metadata, it seems as though you need to invent a language feature for that purpose: attributes. In Clojure, on the other hand, because it's homoiconic, you don't need a separate language feature for metadata. You just write the metadata in Clojure. This doesn't, however, change the <em>role</em> of the spec. Even though you're writing it in Clojure, it plays the role of metadata. One of the implications of that is that it's fair game for an analyser, whereas I think a Clojure function body is still off limits for the Spec engine. </p> </div> <div class="comment-date">2022-06-02 12:36 UTC</div> </div> <div class="comment" id="3b5ff36ffd9343bbb33d92fc97eac255"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#3b5ff36ffd9343bbb33d92fc97eac255">#</a></div> <div class="comment-content"> <p> Ah, I misunderstood. I hadn't thought about the relationship between metadata and analyzers. Clojure certainly has the edge for elegant and powerful metadata. </p> <p> I suppose I feel hope for analyzing function bodies for constraints in this context because type factories tend to contain predicates similar to what we'd use in a spec. Unlike Clojure, this relies on some unenforced expectations on the code and wouldn't always come up with an answer. </p> <p> Even with that hope, the value proposition seems shaky considering our previous comments. </p> </div> <div class="comment-date">2022-06-03 19:37 UTC</div> </div> <div class="comment" id="3ea6094fc9e3458e840aac7dd7bd87c8"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#3ea6094fc9e3458e840aac7dd7bd87c8">#</a></div> <div class="comment-content"> <p> Ruminating on our discussion, I decided it was worth creating an experimental constraints-as-data library, <a href="https://github.com/farlee2121/fsspec">FsSpec</a>, that leans into the advantages of a static type system. </p> <p> It doesn't attempt to prove totality or be part of the type system, like is possible in Idris or F*. It normalizes the common activity of constraining primitive values and exposes those constraints as data that can be programmatically accessed. </p> <p> It'll take use and feedback to determine if this approach really saves complexity, but it was very fun to write. The most fun part was realizing how boolean trees can be normalized to reliably interpret them for data generation. </p> <p> I'd love to hear your thoughts if it interests you. </p> </div> <div class="comment-date">2022-07-06 17:53 UTC</div> </div> <div class="comment" id="bb7af790a7d1420d8a2ce30e6eb076d4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bb7af790a7d1420d8a2ce30e6eb076d4">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. FsSpec looks interesting. It strikes me as one of those things where I'd need to try it out on a real code base before I could pass an informed verdict. </p> </div> <div class="comment-date">2022-07-17 6:41 UTC</div> </div> <div class="comment" id="03871093ca904f4e9909433e18319886"> <div class="comment-author">Daniel Tartaglia <a href="#03871093ca904f4e9909433e18319886">#</a></div> <div class="comment-content"> <p> Very early in the discussion, Tyson Williams asked about Exceptions and purity. In one answer you talked about exceptions being a kind of bottom value. Does that mean that there are multiple values of type Bottom that can potentially be equatable? That doesn't feel right to me... </p> </div> <div class="comment-date">2022-09-17 10:44 UTC</div> </div> <div class="comment" id="c0b030d90a57457bab4a02eb2fdc0564"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c0b030d90a57457bab4a02eb2fdc0564">#</a></div> <div class="comment-content"> <p> Daniel, thank you for writing. At this point, I'm basing my incomplete knowledge on all sorts of different sources, not all of which I can readily remember or identify. The <a href="https://wiki.haskell.org/Bottom">Haskell wiki article on bottom</a>, however, also discusses two kinds of bottom. That may have been where I've picked it up. </p> <p> In general, bottom values originating from invalid input are avoidable in statically typed languages. Instead of throwing exceptions, it's possible to return a Maybe or Either value. In practice, we don't always do that. Division (which is not defined by zero) may be the most infamous example. </p> <p> Infinite loops, on the other hand, can't be type-checked away, as already discussed here. </p> </div> <div class="comment-date">2022-09-18 16:13 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Test-Driven Development with F# Pluralsight course https://blog.ploeh.dk/2015/05/06/test-driven-development-with-f-pluralsight-course 2015-05-06T07:21:00+00:00 Mark Seemann <div id="post"> <p> <em>Test-Driven Development and Functional Programming is a match made in heaven. Learn how and why in my new Pluralsight course.</em> </p> <p> A common criticism against Test-Driven Development (TDD) is that it <a href="http://david.heinemeierhansson.com/2014/test-induced-design-damage.html">leads to <em>Test-Induced Damage</em></a>. However, it doesn't have to be that way, and it turns out that with Functional Programming (FP), the design ideals of FP coincide with TDD. </p> <p> <img src="/content/binary/test-driven-development-with-fsharp-screen-shot.png" alt="Course screenshot"> </p> <p> In my new Pluralsight course on <a href="https://blog.ploeh.dk/tdd-with-fsharp">Test-Driven Development with F#</a>, you'll learn how the intersection between TDD and F# presents opportunities for better design and better testability. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Introduction to Property based Testing with F# Pluralsight course https://blog.ploeh.dk/2015/04/17/introduction-to-property-based-testing-with-f-pluralsight-course 2015-04-17T06:23:00+00:00 Mark Seemann <div id="post"> <p> <em>My latest Pluralsight course is an introduction to Property-Based Testing with F#.</em> </p> <p> Soon after the release of my <a href="https://blog.ploeh.dk/unit-testing-with-fsharp">Unit Testing with F#</a> Pluralsight course, it gives me great pleasure to announce my new course, which is an <a href="https://blog.ploeh.dk/property-based-testing-intro">Introduction to Property-based Testing with F#</a>. </p> <p> In this course, you'll get an introduction to the concept of Property-Based Testing, and see a comprehensive example that demonstrates how to incrementally implement a feature that otherwise would have been hard to address in an iterative fashion. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. C# will eventually get all F# features, right? https://blog.ploeh.dk/2015/04/15/c-will-eventually-get-all-f-features-right 2015-04-15T08:32:00+00:00 Mark Seemann <div id="post"> <p> <em>C# will never get the important features that F# has. Here's why.</em> </p> <p> The relationship between C# and F# is interesting, no matter if you look at it from the C# or the F# perspective: <ul> <li> Before releasing F# to the world, Don Syme, its inventor, was <a href="http://blogs.msdn.com/b/dsyme/archive/2011/03/15/net-c-generics-history-some-photos-from-feb-1999.aspx">instrumental in getting generics into C# and .NET</a>. Had it not been for his and his Cambridge colleagues' effort, we wouldn't have generics today, or we would have the subpar generics that Java has. (Okay, so this point isn't strictly about F#.) </li> <li> F# has had async workflows <a href="http://blogs.msdn.com/b/dsyme/archive/2007/10/11/introducing-f-asynchronous-workflows.aspx">since 2007</a>. In F#, this is simply one of many implementations of a more general language feature called <a href="https://msdn.microsoft.com/en-us/library/dd233182.aspx">Computation Expressions</a> - other common examples are <a href="https://msdn.microsoft.com/en-us/library/dd233209.aspx">Sequence Expressions</a> and <a href="https://msdn.microsoft.com/en-us/library/hh225374.aspx">Query Expressions</a>, but you can also create your own. When async/await was added to C# in 2012, it was a port of that particular implementation, but turned into a one-shot language feature. </li> <li> C# 6 gets <a href="http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx">a lot of small language features</a>, some of which F# already has: auto-property initializers, exception filters, expression-bodied function members... </li> <li> For C# 7, <a href="https://github.com/dotnet/roslyn/issues/98">the design team is considering many Functional language features</a> that F# already has: pattern matching, records, immutability, tuples... </li> </ul> <strong>There's nothing wrong with this.</strong> F# is a great language, so obviously it makes sense to look there for inspiration. Some features also go the other way, such as F# Query Expressions, which were inspired by LINQ. </p> <p> It's not some hidden cabal that I'm trying to expose, either. <a href="http://www.dotnetrocks.com/default.aspx?showNum=935">Mads Torgersen has been quite open about this relationship</a>. </p> <h3 id="5412866cca9348789ebc0a183adaefc7"> Why care about F#, then? <a href="#5412866cca9348789ebc0a183adaefc7" title="permalink">#</a> </h3> <p> A common reaction to all of this seems to be that if C# eventually gets all the best F# features, there's no reason to care about F#. Just stick with C#, and get those features in the future. </p> <p> The most obvious answer to such statements is that F# already has those features, while you'll have to wait for a long time to get them in C#. While C# 6 gets a few features, they are hardly killer features. So perhaps you'll get the good F# features in C#, but they could be years away, and some features might be postponed to later versions again. </p> <p> In my experience, that argument mostly falls on deaf ears. Many programmers are content to wait, perhaps because they feel that the language choice is out of their hands anyway. </p> <h3 id="2c00c5e8c7fa4465abf65bf9762af46a"> What F# features could C# get? <a href="#2c00c5e8c7fa4465abf65bf9762af46a" title="permalink">#</a> </h3> <p> Often, when F# enthusiasts attempt to sell the language to other programmers, they have a long list of language features that F# has, and that (e.g.) C# doesn't have. However, in the future, C# could hypothetically have those features too: <ul> <li> <strong>Records.</strong> C# could have those as well, and they're being considered for C# 7. Implementation-wise, F# records compile to immutable classes anyway. </li> <li> <strong>Discriminated Unions.</strong> Nothing in principle prevents C# from getting those. After all, F# Discriminated Unions compile to a class hierarchy. </li> <li> <strong>Pattern matching</strong> Also being considered for C# 7. </li> <li> <strong>No nulls.</strong> It's a common myth that F# doesn't have nulls. It does. It's even <a href="https://msdn.microsoft.com/en-us/library/dd233249.aspx">a keyword</a>. It's true that F# doesn't allow its <em>Functional</em> data types (records, unions, tuples, etc.) to have null values, but it's only a compiler trick. At run-time, these types can have null values too, and you can provide null values via Reflection. C# could get such a compiler trick as well. </li> <li> <strong>Immutability.</strong> F#'s immutability 'feature' is similar to how it deals with nulls. Lots of F# can be mutable (everything that interacts with C# code), but the special <em>Functional</em> data types can't. Again, it's mostly in how these specific data types are implemented under the hood that provides this feature, and C# could get that as well. </li> <li> <strong>Options.</strong> These are really just specialised Discriminated Unions, so C# could get those too. </li> <li> <strong>Object Expressions.</strong> Java has had those for years, so there's no reason C# couldn't get them as well. </li> <li> <strong>Partial Function Application.</strong> You <a href="/2014/03/10/solid-the-next-step-is-functional">can already do this in C# today, but the syntax for it is really awkward</a>. Thus, there's <em>no technical</em> reason C# can't have that, but the C# language designers would have to come up with a better syntax. </li> <li> <strong>Scripting.</strong> F# is great for scripting, but as the success of <a href="http://scriptcs.net">scriptcs</a> has shown, nothing prevents C# from being a scripting language either. </li> <li> <strong>REPL.</strong> A <a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a> is a really nice tool, but scriptcs already comes with a REPL, again demonstrating that C# could have that too. </li> </ul> This list <em>in no way</em> implies that C# will get any or all of these features. While <a href="https://mvp.microsoft.com/en-us/mvp/Mark%20Seemann-5000205">I'm an MVP</a>, I have no inside insight; I'm only speculating. My point is that I see no fundamental reason C# couldn't eventually get those features. </p> <h3 id="147499af754b4cc8862bfa0762768549"> What F# features can C# never get? <a href="#147499af754b4cc8862bfa0762768549" title="permalink">#</a> </h3> <p> There are a few F# features that many people point to as their favourite, that C# is unlikely to get. A couple of them are: <ul> <li> <strong>Type Providers.</strong> Someone that I completely trust on this issue told me quite authoritatively that "C# will <em>never</em> get Type Providers", and then laughed quietly. While I don't know enough about the technical details of Type Providers to be able to evaluate that statement, I trust this person completely on this issue. </li> <li> <strong>Units of Measure.</strong> Here, I simply have to confess ignorance. While I haven't seen talk about units of measure for C#, I have no idea whether it's doable or not. </li> </ul> These are some loved features of F# that look unlikely to be ported to C#, but there's one quality of F# that I'm absolutely convinced will <em>never</em> make it to C#, and this is <strong>one of the killer features of F#</strong>: it's what you <em>can't do</em> in the language. </p> <p> In a recent article, I explained how <a href="/2015/04/13/less-is-more-language-features">less is more when it comes to language features</a>. Many languages come with redundant features (often for historical reasons), but the fewer redundant features a language has, the better. </p> <p> The <strong>F# compiler doesn't allow circular dependencies</strong>. You can't use a type or a function before you've defined it. This may seem like a restriction, but is perhaps the most important quality of F#. Cyclic dependencies are closely correlated with coupling, and coupling is the deadliest maintainability killer of code bases. </p> <p> In C# and most other languages, you can define dependency cycles, and the compiler makes it easy for you. In F#, the compiler makes it impossible. </p> <p> <a href="http://fsharpforfunandprofit.com/posts/cycles-and-modularity-in-the-wild">Studies show that F# projects have fewer and smaller cycles</a>, and that <a href="http://evelinag.com/blog/2014/06-09-comparing-dependency-networks">there are types of cycles (motifs) you don't see at all in F# code bases</a>. </p> <p> The F# compiler protects you from making cycles. <strong>C# will never be able to do that</strong>, because it would be a <em>massive breaking change</em>: if the C# compiler was changed to protect you as well, most existing C# code wouldn't be able to compile. </p> <p> Microsoft has never been in the habit of introducing breaking changes, so I'm quite convinced that this will never happen. </p> <h3 id="63bfbeefb76b45c7ae99e91a4041282c"> Summary <a href="#63bfbeefb76b45c7ae99e91a4041282c" title="permalink">#</a> </h3> <p> C# could, theoretically, get a lot of the features that F# has, but not the 'feature' that really matters: protection against coupling. Since coupling is one of the most common reasons for <a href="http://en.wikipedia.org/wiki/Software_rot">code rot</a>, this is one of the most compelling reasons to switch to F# today. </p> <p> F# is a great language, not only because of the features it has, but even more so because if the undesirable traits it <em>doesn't</em> have. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="c572bc4b29394f918192d5022ea2f93a"> <div class="comment-author"><a href="http://enterprisecraftsmanship.com/">Vladimir Khorikov</a> <a href="#c572bc4b29394f918192d5022ea2f93a">#</a></div> <div class="comment-content"> <p>I would say C# won't get non-nullable reference types either, even in the form of a compiler trick. It would either introduce too much of breaking changes or be very limited and thus not especially usefull.</p> </div> <div class="comment-date">2015-04-18 15:36 UTC</div> </div> <div class="comment" id="8dba4b8c735b4f759331f6064def16ad"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8dba4b8c735b4f759331f6064def16ad">#</a></div> <div class="comment-content"> <p> Vladimir, thank you for writing. You're probably correct. Many years ago, I overheard Anders Hejlsberg say that it wouldn't be possible to introduce non-nullable reference types into the .NET platform without massive breaking changes. I can't say I ever understood the reasoning behind this (nor was it ever explained to me), but when Anders Hejlsberg tells you that, you sort of have to accept it :) </p> <p> FWIW, there's a bit of discussion about non-nullable reference types in the <a href="https://github.com/dotnet/roslyn/issues/98">C# Design Meeting Notes for Jan 21, 2015</a>, but I have to admit that I didn't follow the link to Eric Lippert's blog :$ </p> </div> <div class="comment-date">2015-04-18 18:57 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Less is more: language features https://blog.ploeh.dk/2015/04/13/less-is-more-language-features 2015-04-13T08:16:00+00:00 Mark Seemann <div id="post"> <p> <em>Many languages have redundant features; progress in language design includes removing those features.</em> </p> <p> (This post is also <a href="http://postd.cc/less-is-more">available in Japanese</a>.) </p> <p> There are many programming languages, and new ones are being introduced all the time. Are these languages better than previous languages? Obviously, that's impossible to answer, since there's no clear measurement of what constitutes a 'better' programming language. </p> <p> Still, if you look at a historical trend, it looks as though one way to make a better language is to identify a redundant language feature, and design a new language that doesn't have that feature. </p> <p> <blockquote> "perfection is attained not when there is nothing more to add, but when there is nothing more to remove." - Antoine de Saint Exupéry </blockquote> </p> <p> In this article, you'll see various examples of language features that have already proven to be redundant, and other features where we are seeing strong indications that they are redundant. </p> <h3 id="993da38f209948348585042540e2b722"> Limitless ways to shoot yourself in the foot. <a href="#993da38f209948348585042540e2b722" title="permalink">#</a> </h3> <p> When the first computers were created, programs had to be written in machine code or assembly language. In machine code, you can express everything the CPU can do, because the machine code is written it terms of a CPU's instruction set. While it's possible to write correct programs in machine code, you can also write a lot of incorrect programs, including programs that crash horribly, or perhaps even destroy the machine on which they are running. </p> <p> You're most likely used to writing code in a higher-level language, but even so, if you share my experience with this, you'll agree that it takes a lot of trial and error to get things right. For every correct program, there are many incorrect variations. The set of incorrect programs is much bigger than the set of correct programs. </p> <p> With machine code, you have limitless ways to create incorrect programs. Yes: you can express everything the CPU can execute, but most of it will be incorrect. Thus, the set of valid programs is a small subset of the set of all possible instructions. </p> <p> <img src="/content/binary/less-is-more-language-features-figure-01.png" alt="The set of all valid programs, inside the much larger set of all possible instructions."> </p> <p> Early computer programmers quickly discovered that writing in machine code was extremely error-prone, and the code was also as unreadable as it could be. One way to address this problem was to introduce assembly language, but it didn't solve the underlying problems: most assembly languages were just 'human-readable' one-to-one mappings over machine code, so you still have unlimited ways to express incorrect programs. </p> <h3 id="fa660777233346c4a8ecfea427c9ca50"> High-level languages <a href="#fa660777233346c4a8ecfea427c9ca50" title="permalink">#</a> </h3> <p> After having suffered with machine code and assembly language for some time, programmers figured out how to express programs in high-level languages. The first programming languages aren't in much use today, but a good example of a 'low-level' high-level language still in use today is C. </p> <p> <img src="/content/binary/less-is-more-language-features-figure-02.png" alt="The set of all valid programs, inside the much larger set of all possible instructions, with the overlay of all possible instructions in a high-level language."> </p> <p> In C, you can express almost all valid programs. There are still tons of ways to write incorrect programs that will crash the program (or the computer), but since the language is an abstraction over machine code, there are instructions you can't express. Most of these are invalid instruction sequences anyway, so it's for the better. </p> <p> Notice what happened: moving from machine code to a high-level programming language removes a feature of the language. In machine code, you can express <em>anything</em>; in a high-level language, there are things you can't express. You're okay with that, because the options that were taken away from you were bad for you. </p> <h3 id="e54d5ce75248435dbad9097fb314f56d"> GOTO <a href="#e54d5ce75248435dbad9097fb314f56d" title="permalink">#</a> </h3> <p> In 1968, Edsger Dijkstra published his (in)famous paper <em>Go To Statement Considered Harmful</em>. In it, he argued that the GOTO statement was an bad idea, and that programs would be 'better' without the GOTO statement. This sparked a decade-long controversy, but these days we've come to understand that Dijkstra was right. Some of the most popular languages in use today (e.g. Java and JavaScript) don't have GOTO at all. </p> <p> <img src="/content/binary/less-is-more-language-features-figure-03.png" alt="The set of all valid programs, inside the much larger set of all possible instructions, with the overlay of all possible instructions in a high-level language without GOTO."> </p> <p> Since GOTO has turned out to be an entirely redundant language feature, a language without it doesn't limit your ability to express <em>valid</em> programs, but it does limit your ability to express <em>invalid</em> programs. </p> <p> Do you notice a pattern? </p> <p> Take something away, and make an improvement. </p> <p> There's nothing new about this; <a href="https://skillsmatter.com/skillscasts/2323-bobs-last-language">Robert C. Martin told us that years ago</a>. </p> <p> You can't just arbitrarily take away <em>anything</em>, because that may constrain you in such a way that there are valid programs you can no longer write: </p> <p> <img src="/content/binary/less-is-more-language-features-figure-04.png" alt="The set of all valid programs, inside the much larger set of all possible instructions, with the overlay of a language that takes away the wrong features."> </p> <p> You have to take away the <em>right</em> features. </p> <h3 id="84b361cc469a4a148d091306d278c57a"> Exceptions <a href="#84b361cc469a4a148d091306d278c57a" title="permalink">#</a> </h3> <p> Today, everyone seems to agree that errors should be handled via some sort of exception mechanisms; at least, everyone can agree that <em>error codes</em> are <em>not</em> the way to go (and I agree). We need something richer than error codes, and something that balances usefulness with robustness. </p> <p> The problem with exceptions is that this mechanism is really only <a href="http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl">GOTO statements in disguise</a> - and we've already learned that GOTO is considered harmful. </p> <p> A better approach is to use a <a href="http://en.wikipedia.org/wiki/Tagged_union">sum type</a> to indicate <em>either</em> <a href="http://fsharpforfunandprofit.com/posts/recipe-part2">success or failure in a composable form</a>. </p> <h3 id="1ea49d48e46046bd93c1932187671d67"> Pointers <a href="#1ea49d48e46046bd93c1932187671d67" title="permalink">#</a> </h3> <p> As Robert C. Martin also pointed out, in older languages, including C and C++, you can manipulate pointers, but as soon as you introduce polymorphism, you no longer need 'raw' pointers. Java doesn't have pointers; JavaScript doesn't do pointers; C# <em>does</em> allow you to use pointers, but I've personally never needed them outside of interop with the Windows API. </p> <p> As these languages have demonstrated, you don't need access to pointers in order to be able to pass values by reference. Pointers can be abstracted away. </p> <h3 id="401c3a536c364b3dbd0175985ce724b6"> Numbers <a href="#401c3a536c364b3dbd0175985ce724b6" title="permalink">#</a> </h3> <p> Most strongly typed languages give you an opportunity to choose between various different number types: bytes, 16-bit integers, 32-bit integers, 32-bit <em>unsigned</em> integers, single precision floating point numbers, etc. That made sense in the 1950s, but is rarely important these days; we waste time worrying about the micro-optimization it is to pick the right number type, while we lose sight of the bigger picture. As <a href="http://hanselminutes.com/396/bugs-considered-harmful-with-douglas-crockford">Douglas Crockford explains, in JavaScript there's only a <em>single</em> number type, which is a great idea - just too bad it's the wrong single number type</a>. </p> <p> <img src="/content/binary/less-is-more-language-features-figure-05.png" alt="The set of all valid programs, inside the much larger set of all possible instructions, with the overlay of a language with a single, sane number type."> </p> <p> With the modern computer resources we have today, a programming language that would restrict you to a single, sane number type would be superior to the mess we have today. </p> <h3 id="7874496bf5134a66ae5a6350cbb6c04f"> Null pointers <a href="#7874496bf5134a66ae5a6350cbb6c04f" title="permalink">#</a> </h3> <p> Nulls are one of the most misunderstood language constructs. There's nothing wrong with the concept of a value that may or may not be present. Many great programming languages have this concept. In Haskell, it's called <em>Maybe</em>; in F#, it's called <em>option</em>; in T-SQL it's called <em>null</em>. What's common in all these languages is that it's an <em>opt-in</em> language feature: you can declare a value as being 'nullable', but by default, <em>it isn't</em> (nullable). </p> <p> However, due to <a href="http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare">Tony Hoare's self-admitted billion-dollar mistake</a>, mainstream languages have null pointers: C, C++, Java, C#. The problem isn't the <em>concept</em> of 'null', but rather that everything <em>can</em> be null, which makes it impossible to distinguish between the cases where null is an appropriate and expected value, from the cases where null is a defect. </p> <p> Design a language without null pointers, and you take away the ability to produce null pointer exceptions. </p> <p> <img src="/content/binary/less-is-more-language-features-figure-06.png" alt="The set of all valid programs, inside the much larger set of all possible instructions, with the overlay of a language without null pointers."> </p> <p> If Tony Hoare is correct that null pointers cost billions of dollars in the last few decades, getting rid of that source of defects can save you lots of money. From Turing-complete languages like T-SQL, Haskell, and F# we know that you can express all valid programs without null pointers, while a huge source of defects is removed. </p> <h3 id="68650468d124404aacaa4916a8b2a51a"> Mutation <a href="#68650468d124404aacaa4916a8b2a51a" title="permalink">#</a> </h3> <p> A central concept in Procedural, Imperative, and Object-Oriented languages is that you can change the value of a variable while executing your program. That's the reason it's called a <em>variable</em>. This seems intuitive, since a CPU contains registers, and what you actually do when you execute a program is that you move values in and out of those registers. It's also intuitive because the purpose of most programs is to change the state of the world: Store a record in a database. Send an email. Repaint the screen. Print a document. Etc. </p> <p> However, it turns out that mutation is also a large source of defects in software, because it makes it much harder to reason about code. Consider a line of C# code like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;r&nbsp;=&nbsp;<span style="color:blue;">this</span>.mapper.Map(rendition);</pre> </p> <p> When the <code>Map</code> method returns, has <code>rendition</code> been modified? Well, if you follow <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a>, it shouldn't, but the only way you can be sure is to review the implementation of the Map method. What if that method exhibits the same problem, by calling into other methods that <em>could</em> mutate the state of the application? There's nothing in C# (or Java, or JavaScript, etc.) that prevents this from happening. </p> <p> In a complicated program with a big call stack, it's impossible to reason about the code, because <em>everything</em> could mutate, and once you have tens or hundreds of variables in play, you can no longer keep track of them. Did the <code>isDirty</code> flag change? Where? What about the <code>customerStatus</code>? </p> <p> Imagine taking away the ability to mutate state: </p> <p> <img src="/content/binary/less-is-more-language-features-figure-07.png" alt="The set of all valid programs, inside the much larger set of all possible instructions, with the overlay of the set of possible instructions in a language without mutation."> </p> <p> Most languages don't completely take away this ability, but as Haskell (a Turing complete language) demonstrates, it's possible to write any program without implicit state mutation. </p> <p> At this point, many people might object that Haskell is too difficult and unintuitive, but to me, that kind of argumentation is reminiscent of the resistance to removing GOTO. If you are used to relying on GOTO, you have to learn alternative ways to model the same behaviour without GOTO. Likewise, if you are used to relying on state mutation, you have to learn alternative ways to model the same behaviour without state mutation. </p> <h3 id="12bf767e331e4096b486435b7a2804e7"> Reference Equality <a href="#12bf767e331e4096b486435b7a2804e7" title="permalink">#</a> </h3> <p> In Object-Oriented languages like C# and Java, the default equality comparison is Reference Equality. If two variables point to the same memory address, the variables are considered equal. If two variables have all identical constituent values, but point to two different memory addresses, they are considered different. That's not intuitive, and many software defects are caused by this. </p> <p> What if you take away Reference Equality from a language? </p> <p> <img src="/content/binary/less-is-more-language-features-figure-08.png" alt="The set of all valid programs, inside the much larger set of all possible instructions, with the overlay of the set of possible instructions in a language without Reference Equality."> </p> <p> What if all data structures instead had <a href="http://en.wikipedia.org/wiki/Relational_operator">Structural Equality</a>? </p> <p> This one I'm not entirely sure about, but in my experience, I almost never need Reference Equality. For Basic Correctness you never need it, but I wonder if you may need the occasional reference comparison in order to implement some performance optimizations... Still, it wouldn't hurt to switch the defaults so that Structural Equality was the default, and there was a special function you could invoke to compare two variables for Reference Equality. </p> <h3 id="c6e9cc0f405240aa97a57f9811069cab"> Inheritance <a href="#c6e9cc0f405240aa97a57f9811069cab" title="permalink">#</a> </h3> <p> Even in 2015, inheritance is everywhere, although it's more than 20 years ago the Gang of Four taught us to <a href="http://amzn.to/XBYukB">favor object composition over class inheritance</a>. It turns out that there's nothing you can do with inheritance that you can't also do with composition with interfaces. The converse doesn't hold in languages with single inheritance: there are things you can do with interfaces (such as implement more than one), that you can't do with inheritance. Composition is a superset of inheritance. </p> <p> This isn't just theory: in my experience, I've been able to avoid inheritance in the way I've designed my code for many years. Once you get the hang of it, it's not even difficult. </p> <h3 id="50ab619ffb854091998e46f31cec0f7d"> Interfaces <a href="#50ab619ffb854091998e46f31cec0f7d" title="permalink">#</a> </h3> <p> Many strongly typed languages (for example Java and C#) have interfaces, which is a mechanism to implement polymorphism. In this way, you can bundle various operations together as methods on an interface. However, one of <a href="https://blog.ploeh.dk/encapsulation-and-solid">the consequences of SOLID</a> is that you should favour <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a> over <a href="http://martinfowler.com/bliki/HeaderInterface.html">Header Interfaces</a>, and as I've previously explained, <a href="/2014/03/10/solid-the-next-step-is-functional">the logical conclusion is that all interfaces should only have a single member</a>. </p> <p> When interfaces only have a single member, the interface declaration itself tends to mostly be in the way - the only thing that matters is the operation. <a href="/2009/05/28/DelegatesAreAnonymousInterfaces">In C#, you could just use a delegate instead</a>, and even Java has lambdas now. </p> <p> There's nothing new about this. Functional languages have used <em>functions</em> as the basic compositional unit for years. </p> <p> In my experience, you can model everything with single-member interfaces, which also implies that you can model everything with functions. Again, this isn't really surprising, since functional languages like Haskell are Turing complete too. </p> <p> It's possible to get by without interfaces. In fact, <a href="http://blog.cleancoder.com/uncle-bob/2015/01/08/InterfaceConsideredHarmful.html">Robert C. Martin finds them harmful</a>. </p> <h3 id="20938ffec3794d8cb21741ddea6c2fba"> Reflection <a href="#20938ffec3794d8cb21741ddea6c2fba" title="permalink">#</a> </h3> <p> If you've ever done any sort of meta-programming in .NET or Java, you probably know what Reflection is. It's a set of APIs and language or platform features that enable you to inspect, query, manipulate, or emit code. </p> <p> <em>Meta-programming</em> is an indispensable tool, so I'd be sorry to lose it. However, Reflection isn't the <em>only</em> way to enable meta-programming. Some languages are <a href="http://en.wikipedia.org/wiki/Homoiconicity">homoiconic</a>, which means that a program in such languages is structured as data, which itself can be queried and manipulated like any other data. Such languages don't need Reflection as a feature, because meta-programming is baked into the language, so to speak. </p> <p> In other words: Reflection is a language feature aimed at the goal of meta-programming. If meta-programming can be achieved via homoiconicity instead, it would imply that Reflection is a redundant feature. </p> <h3 id="e6390845d0304f6d9d2271092b06d397"> Cyclic Dependencies <a href="#e6390845d0304f6d9d2271092b06d397" title="permalink">#</a> </h3> <p> While it may be true that null pointers are the biggest single source of software defects, in my experience the greatest single source of unmaintainable code is <em>coupling</em>. One of the most problematic types of coupling is cyclic dependencies. In languages like C# and Java, cyclic dependencies are almost impossible to avoid. </p> <p> Here's one of my own mistakes that I only discovered because I started to look for it: in the otherwise nice and maintainable <a href="https://github.com/GreanTech/AtomEventStore">AtomEventStore</a>, there's an interface called <a href="https://github.com/GreanTech/AtomEventStore/blob/master/AtomEventStore/IXmlWritable.cs">IXmlWritable</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IXmlWritable</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;WriteTo(<span style="color:#2b91af;">XmlWriter</span>&nbsp;xmlWriter,&nbsp;<span style="color:#2b91af;">IContentSerializer</span>&nbsp;serializer); }</pre> </p> <p> As you can tell, the WriteTo method takes an <a href="https://github.com/GreanTech/AtomEventStore/blob/master/AtomEventStore/IContentSerializer.cs">IContentSerializer</a> argument. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IContentSerializer</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Serialize(<span style="color:#2b91af;">XmlWriter</span>&nbsp;xmlWriter,&nbsp;<span style="color:blue;">object</span>&nbsp;value); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">XmlAtomContent</span>&nbsp;Deserialize(<span style="color:#2b91af;">XmlReader</span>&nbsp;xmlReader); }</pre> </p> <p> Notice that the Deserialize method returns an <a href="https://github.com/GreanTech/AtomEventStore/blob/master/AtomEventStore/XmlAtomContent.cs">XmlAtomContent</a> value. How is XmlAtomContent defined? Like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">XmlAtomContent</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IXmlWritable</span></pre> </p> <p> Notice that it implements IXmlWritable. Oh, dear! </p> <p> Although I'm <em>constantly</em> on the lookout for these kinds of things, that one slipped right past me. </p> <p> In F# (and, I believe, OCaml), on the other hand, this wouldn't even have compiled! </p> <p> While F# has a way to introduce small cycles <em>within</em> a module using the <code>and</code> and <code>rec</code> keywords, there are no accidental cycles. You have to explicitly use those keywords to enable limited cycles - and there's no way to define cycles that span modules or libraries. </p> <p> What an excellent protection against tightly coupled code! Take away the ability to (inadvertently) introduce Cyclic Dependencies, and get a better language! </p> <p> <img src="/content/binary/less-is-more-language-features-figure-09.png" alt="The set of all valid programs, inside the much larger set of all possible instructions, with the overlay of the set of possible instructions in a language that disallows cycles."> </p> <p> This has even been shown to work in the wild: <a href="http://fsharpforfunandprofit.com/posts/cycles-and-modularity-in-the-wild">Scott Wlaschin examined C# and F# projects for cycles</a>, and found that F# projects have fewer and smaller cycles than C# projects. This analysis was later <a href="http://evelinag.com/blog/2014/06-09-comparing-dependency-networks">enhanced and corroborated by Evelina Gabasova</a>. </p> <h3 id="79a4d185a2c54c5b8eba0bfdae0ebe24"> Summary <a href="#79a4d185a2c54c5b8eba0bfdae0ebe24" title="permalink">#</a> </h3> <p> What I've tried to illustrate in this article is that there are many ways in which you could make a better language by taking away a particular feature from an existing language. Take away a redundant feature, and you'll still have a Turing complete language that can do (close to) anything, but with fewer options for shooting yourself in the foot. </p> <p> Perhaps the ultimate programming language is a language <em>without</em>: <ul> <li>GOTO</li> <li>Exceptions</li> <li>Pointers</li> <li>Lots of specialized number types</li> <li>Null pointers</li> <li>Mutation</li> <li>Reference equality</li> <li>Inheritance</li> <li>Interfaces</li> <li>Reflection</li> <li>Cyclic dependencies</li> </ul> </p> <p> Have I identified all possible redundant features? Most likely not, so here's a great opportunity for language designers to define an even better language, by finding something new to take away! </p> <p> <strong>Update September 14 2015:</strong> This article sparked a <a href="http://www.dotnetrocks.com/default.aspx?showNum=1170">.NET Rocks! episode with even more discussion about this topic</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="9a45df4990da40fe93465fabb32fc3e7"> <div class="comment-author"><a href="https://github.com/chrisdew">Chris Dew</a> <a href="#9a45df4990da40fe93465fabb32fc3e7">#</a></div> <div class="comment-content"> <p>The idea that Javascript is a superset of "valid programs", but that C is not, could do with more explanation.</p> <p>My background is in server code (Haskell/Java/Python/Javascript/Rust), web code (Javascript) and embedded code (Rust/C) (with some ARM assembly for when Rust/C can't produce the needed program unaided). This makes the idea that I could express programs in Javascript, which I couldn't in C, very interesting.</p> <p>I think that Rust is going to be very important in embedded development, in a few years.</p> <p>P.S. I hope I have commented in the correct way.</p> </div> <div class="comment-date">2015-04-14 8:07 UTC</div> </div> <div class="comment" id="bd7ef0ea31f74ddf8f4ac07eda0b9c16"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bd7ef0ea31f74ddf8f4ac07eda0b9c16">#</a></div> <div class="comment-content"> <p> Chris, thank you for writing. Where in this article do I claim that there are programs you could express in JavaScript, but not in C? </p> </div> <div class="comment-date">2015-04-14 14:41 UTC</div> </div> <div class="comment" id="f2eb1b1b96bb42f08a158d41e6be6020"> <div class="comment-author"><a href="http://lachlanap.me/">Lachlan Phillips</a> <a href="#f2eb1b1b96bb42f08a158d41e6be6020">#</a></div> <div class="comment-content"> <p> Thanks for the post Mark. Some great points; I like especially the idea of disallowing cyclic dependencies. That'd be awesome on a legacy Java project I'm working on now! </p> <p> Having working in Scala and a little Haskell, I can say I love the Maybe type. One thing I have trouble visualising still is Exceptions. I think I need to find some more examples on doing without exceptions in real applications. </p> <p> I guess there's somewhat of a middle ground between having the "safest" language, vs the most performant language. In a lot of cases you don't need awesome performance so the safest language is the better one... but in some you do. You occasionally need control over how your bits are packed. </p> <p> Let's hope for the future! </p> </div> <div class="comment-date">2015-04-16 6:24 UTC</div> </div> <div class="comment" id="f058e0fc8ab44ce6b26acad5a0396a46"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f058e0fc8ab44ce6b26acad5a0396a46">#</a></div> <div class="comment-content"> <p> Lachlan, thank you for writing. When it comes to working without exceptions, the point is to replace them with something stronger. Scott Wlashin's post and presentation about <a href="http://fsharpforfunandprofit.com/posts/recipe-part2">Railway Oriented Programming</a> is a great place to start. You can see another example in my <a href="http://www.infoq.com/presentations/mock-fsharp-tdd">No Mocks presentation</a>, and in one of my up-coming Pluralsight courses. </p> <p> When it comes to the balance between safe and performant, there will always be room for languages that sacrifice safety for speed, but in my experience, this shouldn't be a common concern. As soon as you're doing any sort of significant I/O, the cost of that tends to be so much higher than any potential imperfections in pure CPU processing. </p> <p> Apart from that, I don't see why, in principle, a safe language couldn't also be performant. These two traits don't seem to me to be intrinsically mutually exclusive. </p> </div> <div class="comment-date">2015-04-17 8:55 UTC</div> </div> <div class="comment" id="3e279ddd75714fa89f236d19854869b8"> <div class="comment-author"><a href="http://practical-scheme.net/">Shiro Kawai</a> <a href="#3e279ddd75714fa89f236d19854869b8">#</a></div> <div class="comment-content"> <p> As one of less-is-more advocates, I feel your article makes sense instinctively. However, one thing keeps bugging me: <emph>How do you define "valid programs"?</emph> </p> <p> I think I know what you try to mean with it, but once we try to be more specific, validity can only be defined in terms of a certain framework of semantics. If we have such a framework, then we can derive a programming language from it, which would be a language that only accepts valid programs and reject others. Problem solved. </p> <p> In reality we don't have such a framework. We don't know what is a valid program precisely. There are programs that doesn't seem to make sense, and there are programs that are obviously useful, but there are huge gray area inbetween. It's actually a moving target---the context, the environment, the user, and other outside factors will greatly affect what's valid or not. But the fact that we don't know what valid programs shakes the ground of this whole discussion, doesn't it? Or it can end up tautology---"valid programs are defined in terms of this language, so this language fit the best to cover the valid programs." </p> </div> <div class="comment-date">2015-05-15 03:09 UTC</div> </div> <div class="comment" id="eefc51ad816d4970979e39c164ba5df1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#eefc51ad816d4970979e39c164ba5df1">#</a></div> <div class="comment-content"> <p> Shiro Kawai, thank you for writing. In this article, I deliberately left the definition of <em>validity</em> vague. Why do we write software? If we exclude Katas and the like (which we do for training), software is ultimately written in order to solve problems; in order to be used. A valid program is a program that's useful. </p> <p> This doesn't change the discussion, which is pragmatic. From <em>experience</em>, we know that we don't need GOTO; from experience, we know that we don't need pointers; from experience, we know that we don't need null pointers; from experience, we know that we don't need inheritance; etc. </p> </div> <div class="comment-date">2015-05-15 5:47 UTC</div> </div> <div class="comment" id="4c65c72d5eab4912bfce76cf321c07bb"> <div class="comment-author"><a href="http://practical-scheme.net/">Shiro Kawai</a> <a href="#4c65c72d5eab4912bfce76cf321c07bb">#</a></div> <div class="comment-content"> <p> Hi, thanks for the reply. I believe I get your intent. What I wanted was to point out a danger of this kind of argument. Because when you write software to solve problems, you need to articulate the problem, and the way you frame the problem is inevitably influenced by the frame of your language of choice. In other words, when you think you tighten the circle of the language features, it's not necessarily that you get it close to supposed set of "valid programs", but you may just as well start ignoring useful programs outside of your langauge circle and you don't even realize that. You just think "ok, there may be some programs that falls outside but it's not that important." How do you verify that the excluded gray area isn't that important? </p> <p> (I wrote "you", but actually this is something I constanly try to remind myself, since I tend to be dragged toward feature-minimalism. I love Scheme.) </p> <p> For example, you talk single-member inheritance and you brought up Haskell, but you didn't mention type classes. Isn't type classe sort of interface, in a sense that it defines a protocol consists of multiple functions? Do you say it's actually redundant? Or will you dismiss programs that can be expressed well using type classes "unimportant"? </p> <p> Another controversial (and probably obscure) feature is <tt>change-class</tt> in CL. This one is such a beast that it's difficult to cope with many modern features (obviously it invalidates strict typecheckers). However, in the context where the feature is needed, I don't know if there's a replacement. If you get rid of it, you just have to give up the kind of software that require the feature. That's a reasonable trade-off, but what you do is that you cut off a part of "valid programs" in order to tighten the circle. </p> <p> There's no solid circle of valid programs, and as you tighten the circle of the language, you actually shape the circle of valid programs that suit to the circle of language you draw. That's not necessarily a bad thing, but the language designers need to be aware of it, that's what I wanted to say. </p> </div> <div class="comment-date">2015-05-15 7:40 UTC</div> </div> <div class="comment" id="7bdcebbb6c524e9791000e2eb38db00d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7bdcebbb6c524e9791000e2eb38db00d">#</a></div> <div class="comment-content"> <p> It's a good point that a language shapes how you approach problems. In this discussion, I assume that all languages (both existing and hypothetical) are Turing complete, but even under that assumption, there will be problems that are difficult, or perhaps even impossible, to address in a particular language. </p> <p> The question is whether that isn't true for all languages? </p> <p> In a sense, the most 'powerful' languages are all the dialects of machine code. By definition, they should be able to express everything a particular CPU can do, so by corollary, they should be the most complete languages available. Despite being complete, these dialects don't have inheritance, exceptions, Reflection, interfaces, or a lot of other things. Such features are features that have been <em>added</em> to some languages. Logically, I don't see how 'removing' such a feature constrains one's ability to express 'all valid programs'. </p> <p> To be fair, that argument of mine doesn't apply to all the language features I've described. For example, CPU instruction sets most certainly allow immutability. </p> <p> What I was aiming at with my article was never a formal discussion about how a hypothetical language would, or wouldn't, be able to express 'all valid programs'. The purpose was more to point out that decades of experience should by now have taught the overall programmer community that there are many language features that we can do without. </p> <p> As I pointed out, you can't arbitrarily remove features. Some features are extremely important within a particular language, while it may be irrelevant in another language. While I don't know much about Type Classes, it seems to be an example of this: it's very important in Haskell, but doesn't exist at all in C# or F#. </p> <p> This is also the reason why I don't think we'll ever have a single, perfect programming language. In the future, we'll also have many different languages, and they'll be good at different types of problems. </p> <p> Just as it is today, it'll be important to know more than a single programming language, because, exactly as you wrote, a problem may 'fall outside' a given language, but then, if you know another language, you may realise that you can use it to solve that particular problem. </p> </div> <div class="comment-date">2015-05-16 14:29 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Unit Testing with F# Pluralsight course https://blog.ploeh.dk/2015/04/02/unit-testing-with-f-pluralsight-course 2015-04-02T13:38:00+00:00 Mark Seemann <div id="post"> <p> <em>My latest Pluralsight course is an introduction to unit testing with F#.</em> </p> <p> Perhaps you already know all about unit testing. Perhaps you already know all about F#. But do you know how to write unit tests in F#? </p> <p> <img src="/content/binary/unit-testing-fsharp-venn.png" alt="Unit testing and F# Venn diagram."> </p> <p> <a href="https://blog.ploeh.dk/unit-testing-with-fsharp">My new Pluralsight course</a> explains how to write unit tests with F#. If you already know F# and unit testing on .NET, it's quite straightforward. This is my first <em>beginner</em>-level course on Pluralsight, so regular readers of this blog may find it too basic. </p> <p> Still, if you don't know what <a href="http://www.swensensoftware.com/unquote">Unquote</a> is and can do for you, you may want to consider watching module four, which introduces this great assertion library, and provides many examples. </p> <p> This entire course will, together with some of <a href="https://blog.ploeh.dk/pluralsight-courses">my existing Pluralsight courses</a>, serve as a basis for more courses on F# and Test-Driven Development. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. POSTing JSON to an F# Web API https://blog.ploeh.dk/2015/03/19/posting-json-to-an-f-web-api 2015-03-19T16:02:00+00:00 Mark Seemann <div id="post"> <p> <em>How to write an ASP.NET Web API service that accepts JSON in F#.</em> </p> <p> It seems that many people have problems with accepting JSON as input to a POST method when they attempt to implement an <a href="http://www.asp.net/web-api">ASP.NET Web API</a> service in F#. </p> <p> It's really quite easy, with one weird trick :) </p> <p> You can follow my <a href="/2013/08/23/how-to-create-a-pure-f-aspnet-web-api-project">recipe for creating a pure F# Web API project</a> to get started. Then, you'll need to add a Data Transfer Record and a Controller to accept your data: </p> <p> <pre>[&lt;<span style="color:#2b91af;">CLIMutable</span>&gt;] <span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">MyData</span>&nbsp;=&nbsp;{&nbsp;MyText&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>;&nbsp;MyNumber&nbsp;:&nbsp;<span style="color:#2b91af;">int</span>&nbsp;} <span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">MyController</span>()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:#2b91af;">ApiController</span>() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Post(myData&nbsp;:&nbsp;<span style="color:#2b91af;">MyData</span>)&nbsp;=&nbsp;this.Ok&nbsp;myData</pre> </p> <p> That's quite easy; there's only one problem with this: the incoming <code>myData</code> value is always null. </p> <h3 id="0ceb82fe949f4690a3328d5fe5a86365"> The weird trick <a href="#0ceb82fe949f4690a3328d5fe5a86365" title="permalink">#</a> </h3> <p> In addition to routes etc. you'll need to add this to your Web API configuration: </p> <p> <pre>GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()</pre> </p> <p> You add this in your Application_Start method in your Global class, so you only have to add it once for your entire project. </p> <h3 id="03bb3b6ce3bd4289ac91ac595768bda7"> The explanation <a href="#03bb3b6ce3bd4289ac91ac595768bda7" title="permalink">#</a> </h3> <p> Why does this work? Part of the reason is that when you add the [&lt;CLIMutable&gt;] attribute to your record, it causes the record type to be compiled with auto-generated internal mutable fields, and these are named by appending an @ character - in this case, the field names become <code>MyText@</code> and <code>MyNumber@</code>. </p> <p> Apparently, the default JSON Contract Resolver (whatever that is) goes for those fields, even though they're internal, but the CamelCasePropertyNamesContractResolver doesn't. It goes for the properly named <code>MyText</code> and <code>MyNumber</code> writeable public properties that the compiler <em>also</em> generates. </p> <p> As the name implies, the CamelCasePropertyNamesContractResolver converts the names to camel case, so that the JSON properties become <code>myText</code> and <code>myNumber</code> instead, but I only find this appropriate anyway, since this is the convention for JSON. </p> <h3 id="9314e31f1bea4a3487c30fbfb68a8bad"> Example HTTP interaction <a href="#9314e31f1bea4a3487c30fbfb68a8bad" title="permalink">#</a> </h3> <p> You can now start your service and make a POST request against it: </p> <p> <pre>POST http://localhost:49378/my HTTP/1.1 Content-Type: application/json { "myText": "ploeh", "myNumber": 42 }</pre> </p> <p> This request creates this response: </p> <p> <pre>HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 {"myText":"ploeh","myNumber":42}</pre> </p> <p> That's all there is to it. </p> <p> You can also <a href="/2013/10/15/easy-aspnet-web-api-dtos-with-f-climutable-records">receive XML instead of JSON using a similar trick</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c2a3937230a54142b8a5964156e4b3d6"> <div class="comment-author">Tom R <a href="#c2a3937230a54142b8a5964156e4b3d6">#</a></div> <div class="comment-content">Thanks for the tip, I've now found that applying the attribute <code>[&lt;JsonObject(MemberSerialization=MemberSerialization.OptOut)&gt;]</code> to the type also works</div> <div class="comment-date">2015-11-16 10:07 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Property Based Testing without a Property Based Testing framework https://blog.ploeh.dk/2015/02/23/property-based-testing-without-a-property-based-testing-framework 2015-02-23T20:03:00+00:00 Mark Seemann <div id="post"> <p> <em>Sometimes, you don't need a Property-Based Testing framework to do Property-Based Testing.</em> </p> <p> In my <a href="/2015/02/23/a-simpler-arbitrary-for-the-diamond-kata">previous post</a>, I showed you how to configure <a href="https://fsharp.github.io/FsCheck">FsCheck</a> so that it creates char values exclusively from the list of the upper-case letters A-Z. This is because the only valid input for the Diamond kata is the set of these letters. </p> <p> By default, FsCheck generates 100 random values for each property, and runs each property with those 100 values. <a href="https://github.com/ploeh/DiamondFsCheck">My kata code</a> has 9 properties, so that means 900 function calls (taking just over 1 second on my Lenovo X1 Carbon). </p> <p> However, why would we want to select 100 random values from a set of 26 valid values? Why not simply invoke each property (which is a function) with those 26 values? </p> <p> That's not so hard to do, but if there's a way to do it with FsCheck, I haven't figured it out yet. It's fairly easy to do with <a href="http://xunit.github.io">xUnit.net</a>, though. </p> <p> What you'll need to do is to change the Letters type to an instance class implementing seq&lt;obj[]&gt; (IEnumerable&lt;object[]&gt; for the single C# reader still reading): </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">Letters</span>&nbsp;()&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;letters&nbsp;=&nbsp;<span style="color:blue;">seq</span>&nbsp;{<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;<span style="color:#a31515;">&#39;Z&#39;</span>}&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.cast&lt;<span style="color:#2b91af;">obj</span>&gt;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;[|x|]) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">seq</span>&lt;<span style="color:#2b91af;">obj</span>[]&gt;&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.GetEnumerator&nbsp;()&nbsp;=&nbsp;letters.GetEnumerator() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.GetEnumerator&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;letters.GetEnumerator()&nbsp;:&gt;&nbsp;Collections.<span style="color:#2b91af;">IEnumerator</span></pre> </p> <p> This is simply a class that enumerates the char values 'A' to 'Z' in ascending order. </p> <p> You can now use xUnit.net's Theory and ClassData attributes to make each Property execute exactly 26 times - one for each letter: </p> <p> <pre>[&lt;<span style="color:#2b91af;">Theory</span>;&nbsp;<span style="color:#2b91af;">ClassData</span>(typeof&lt;<span style="color:#2b91af;">Letters</span>&gt;)&gt;] <span style="color:blue;">let</span>&nbsp;``Diamond&nbsp;is&nbsp;as&nbsp;wide&nbsp;as&nbsp;it&#39;s&nbsp;high``&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;rows.Length &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;<span style="background:#fff2df;">&lt;@&nbsp;</span><span style="background:#fff2df;">rows</span><span style="background:#fff2df;">&nbsp;</span><span style="background:#fff2df;">|&gt;</span><span style="background:#fff2df;">&nbsp;</span><span style="color:#2b91af;background:#fff2df;">Array</span><span style="background:#fff2df;">.</span><span style="background:#fff2df;">forall</span><span style="background:#fff2df;">&nbsp;(</span><span style="color:blue;background:#fff2df;">fun</span><span style="background:#fff2df;">&nbsp;</span><span style="background:#fff2df;">x</span><span style="background:#fff2df;">&nbsp;</span><span style="color:blue;background:#fff2df;">-&gt;</span><span style="background:#fff2df;">&nbsp;</span><span style="background:#fff2df;">x</span><span style="background:#fff2df;">.</span><span style="background:#fff2df;">Length</span><span style="background:#fff2df;">&nbsp;</span><span style="background:#fff2df;">=</span><span style="background:#fff2df;">&nbsp;</span><span style="background:#fff2df;">expected</span><span style="background:#fff2df;">)&nbsp;@&gt;</span></pre> </p> <p> Instead of 900 tests executing in just over 1 second, I now have 234 tests executing in just <em>under</em> 1 second. A marvellous speed improvement, and, in general, a triumph for mankind. </p> <p> The point is that if the set of valid input values (the <em>domain</em>) is small enough, you may consider simply using <em>all</em> of them, in which case you don't need a Property-Based Testing framework. However, I still think this is probably a rare occurrence, so I'll most likely reach for FsCheck again next time I need to write some tests. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A simpler Arbitrary for the Diamond kata https://blog.ploeh.dk/2015/02/23/a-simpler-arbitrary-for-the-diamond-kata 2015-02-23T19:50:00+00:00 Mark Seemann <div id="post"> <p> <em>There's a simple way to make FsCheck generate letters in a particular range.</em> </p> <p> In my <a href="/2015/01/10/diamond-kata-with-fscheck">post about the Diamond kata with FsCheck</a>, I changed the way <a href="https://fsharp.github.io/FsCheck">FsCheck</a> generates char values, using this custom Arbitrary (essentially a random value generator): </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">Letters</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">member</span>&nbsp;Char()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Arb</span>.<span style="color:#2b91af;">Default</span>.Char() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Arb</span>.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;&lt;=&nbsp;c&nbsp;&amp;&amp;&nbsp;c&nbsp;&lt;=&nbsp;<span style="color:#a31515;">&#39;Z&#39;</span>)</pre> </p> <p> This uses the default, built-in Arbitrary for char values, but filters its values so that most of them are thrown away, and only the letters 'A'-'Z' are left. This works, but isn't particularly efficient. Why generate a lot of values only to throw them away? </p> <p> It's also possible to instruct FsCheck to generate values from a particular set of valid values, which seems like an appropriate action to take here: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">Letters</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">member</span>&nbsp;Char()&nbsp;=&nbsp;<span style="color:#2b91af;">Gen</span>.elements&nbsp;[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;<span style="color:#a31515;">&#39;Z&#39;</span>]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Arb</span>.fromGen</pre> </p> <p> Instead of using <code>Arb.Default.Char()</code> and filtering the values generated by it, this implementation uses <code>Gen.elements</code> to create a Generator of the values 'A'-'Z', and then an Arbitrary from that Generator. </p> <p> Much simpler, but now it's also clear that this custom Arbitrary will be used to generate 100 test cases (for each property) from a set of 26 values; that <a href="/2015/02/23/property-based-testing-without-a-property-based-testing-framework">doesn't seem right...</a> </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Library Bundle Facade https://blog.ploeh.dk/2015/01/21/library-bundle-facade 2015-01-21T19:22:00+00:00 Mark Seemann <div id="post"> <p> <em>Some people want to define a Facade for a bundle of libraries. Is that a good idea?</em> </p> <p> My recent article on <a href="/2015/01/06/composition-root-reuse">Composition Root reuse</a> generated some comments: <blockquote> <p> "What do you think about pushing these factories and builders to a library so that they can be reused by different composition roots?" </p> <p> "We want to share the composition root, because otherwise when a component needs a new dependency and a constructor parameter is added, we'd have to change the same code in two different places." </p> </blockquote> These comments are from two different people, but they provide a decent summary of the concerns being voiced. </p> <p> Is it a good idea to provide one or more <a href="http://en.wikipedia.org/wiki/Facade_pattern">Facades</a>, for example in the form of Factories or Builders, for the libraries making up a <a href="/2011/07/28/CompositionRoot">Composition Root</a>? More specifically, is it a good idea to provide a Factory or Builder that can compose a complete object graph spanning multiple libraries? </p> <p> In this article, I will attempt to answer that question for various cases of library bundles. To make the terminology a bit more streamlined, I'll refer to any Factory or Builder that composes object graphs as a <em>Composer</em>. </p> <h3 id="bbf155d855e84e64921471599f62458a"> Single library <a href="#bbf155d855e84e64921471599f62458a" title="permalink">#</a> </h3> <p> In the case of a single library, I think I've <a href="/2014/05/19/di-friendly-library">already answered the question in the affirmative</a>. There's nothing wrong with including a Facade in the form of a Factory or Builder in order to make that single library easier to use. </p> <h3 id="24cf2979ca8c4c0b8e890d6927bf1c75"> Two libraries <a href="#24cf2979ca8c4c0b8e890d6927bf1c75" title="permalink">#</a> </h3> <p> When you introduce a second library, things start becoming interesting. If we consider the case of two libraries, for example a Domain Model and a Data Access Library, the Composition Root will need to compose an object graph where some of the objects in the graph are from the Domain Model, and some of the objects are from the Data Access Library. </p> <p> <img src="/content/binary/domain-model-and-data-access-library.png" alt="Domain Model and Data Access Libraries."> </p> <p> In the spirit of <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices</a> (APPP), it turns out that simply drawing dependency diagrams can be helpful. From the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> (DIP) we know that the "clients [...] own the abstract interfaces" (APPP, chapter 11), which means that for our two example libraries, the dependency graph <em>must</em> look like this: </p> <p> <img src="/content/binary/domain-model-and-data-access-library-dependency-graph.png" alt="Domain Model and Data Access Libraries dependency graph."> </p> <p> At least, if you follow the <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">most common architectures for loosely couple code</a>, the Domain Model is the 'client', so it gets to define the interfaces it needs. Thus, it follows that the Data Access Library, in order to implement those interfaces, must have a compile-time dependency on the Domain Model. That's what the arrow means. </p> <p> From this diagram, it should be clear that you can't put a Factory or Builder in the Domain Model library. If the Composer should compose object graphs from both libraries, it would need to reference both of those libraries, and the Domain Model can't reference the Data Access Library, since that would result in a circular reference. </p> <p> You <em>could</em> put the Composer in the Data Access Library, but that somehow doesn't feel right, and in any case, as we shall see later, this solution can't be generalised to <em>n</em> libraries. </p> <p> A solution that many people reach for, then, is to pull the interfaces out into a separate library, like this: </p> <p> <img src="/content/binary/domain-model-and-data-access-and-interface-libraries.png" alt="Domain Model, Data Access, and interface libraries dependency graph."> </p> <p> It's a bit like cheating, according to the DIP, but it's as decoupled as before. In this diagram, the Domain Model depends on the interfaces because it uses them, while the Data Access Library depends on the interface library because it implements the interfaces. Unfortunately, this doesn't solve the problem at all, because there's still no place to place a Composer without getting into a problem with either the DIP, or circular references (exercise: try it!). </p> <p> A possible option is to keep the libraries as the DIP dictates, and then add a third Composer library: </p> <p> <img src="/content/binary/domain-model-and-data-access-and-composer-libraries.png" alt="Domain Model, Data Access, and Composer libraries dependency graph."> </p> <p> The Composer library references both the Domain Model and the Data Access Library, so it's possible for it to compose object graphs with objects from both libraries. The only purpose of this library, then, is to compose those object graphs, so it'll likely only contain a single class. </p> <h3 id="adcae211e80b4dfcbdaef95239692117"> Multiple libraries <a href="#adcae211e80b4dfcbdaef95239692117" title="permalink">#</a> </h3> <p> Does the above conclusions change if you have more than two libraries? Only in the sense that it further restricts your options. As the analysis of the special case with two libraries demonstrated, you only have two options for adding a Composer to your bundle of libraries: <ul> <li>Put the Composer in the Data Access Library</li> <li>Put the Composer in a new dedicated library</li> </ul> A single counter-example with three libraries is enough to demonstrate that the first of these options (putting the Composer in the Data Access Library) isn't a generally available option. </p> <p> Imagine that you have two Data Access Libraries instead of one: </p> <p> <img src="/content/binary/domain-model-and-two-data-access-libraries.png" alt="Domain Model and two Data Access libraries dependency graph."> </p> <p> For instance, the SQL Access Library may implement various interfaces defined by the Domain Model, based on a SQL Server database; and the Web Service Access Library may implement some other interfaces by calling out to some web service. </p> <p> If the Composer must be able to compose object graphs with object from all three libraries, it must reside in a library that references all of the relevant libraries. The Domain Model is still out of the question because you can't have circular references. That leaves one of the two Data Access libraries. It'd be technically possible to e.g. add a reference from the SQL Access Library to the Web Service Access Library, and put the Composer in the SQL Access Library: </p> <p> <img src="/content/binary/domain-model-and-two-data-access-libraries-where-one-data-access-library-references-the-other.png" alt="Domain Model and two Data Access libraries dependency graph, where one Data Access Library references the other."> </p> <p> However, why would you ever do that? It's clearly wrong to let one Data Access library depend on another, and it doesn't help if you reverse the arrow. </p> <p> Thus, the only option left is to add a new Composer library: </p> <p> <img src="/content/binary/domain-model-and-two-data-access-libraries-and-composer.png" alt="Domain Model, two Data Access libraries, and a Composer library dependency graph."> </p> <p> As before, the Composer library has references to all other libraries, and contains a single class that composes object graphs. </p> <h3 id="b8fb549e95484773bf3ffa7bdf5240ba"> Over-engineering <a href="#b8fb549e95484773bf3ffa7bdf5240ba" title="permalink">#</a> </h3> <p> The point of this analysis is to arrive at the conclusion that no matter how you twist and turn, you'll have to add a completely new library with the only purpose of composing object graphs. Is it warranted? </p> <p> If the only motivation for doing this is to avoid duplicated code, I would argue that this looks like over-engineering. In the questions quoted above, it sounds as if the <a href="http://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">Rule of Three</a> isn't even satisfied. It's always important to <a href="/2014/08/07/why-dry">question your motivations for avoiding duplication</a>. In this case, I'd be wary of introducing so much extra complexity only in order to avoid writing the same lines of code twice - particularly when it's likely that the <a href="http://verraes.net/2014/08/dry-is-about-knowledge">duplication is accidental</a>. </p> <h3 id="b99bb3014209471aa8f101c14cdde362"> TL;DR <a href="#b99bb3014209471aa8f101c14cdde362" title="permalink">#</a> </h3> <p> Attempting to provide a reusable Facade to compose object graphs across multiple libraries is hardly worth the trouble. Think twice before you do it. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="748b7931b40540b2bb1479d0f72dbb14"> <div class="comment-author">Kenny Pflug <a href="#748b7931b40540b2bb1479d0f72dbb14">#</a></div> <div class="comment-content"> <p>Dear Mark,</p> <p>Thanks again for another wonderful post. I'm one of the guys you mentioned at the beginning of this text and I totally agree with you on this subject.</p> <p>Maybe I wasn't specific enough in my <a href="/2015/01/06/composition-root-reuse/">comment</a> that I didn't mean to introduce factories and builders handling dependencies from two or more assemblies, but only from a single one. The composition root wires the independent subgraphs from the different assemblies together.</p> <p>However, I want to reemphasize the Builder Pattern in this context: it provides default dependencies for the object graph to be created and (usually) a fluent API (i.e. some sort of method chaining) to exchange these dependencies. This has the following consequences for the client programmer using the builder(s): he or she can easily create a default object graph by calling <span style="color:black; font-family: Consolas">new <span style="color: #2b91af">Builder</span>().Build()</span> and still exchange dependencies he or she cares about. This can keep the composition root clean (at least for Arrange phases of tests this is true).</p> <p>Why I'm so excited about this? Because I use this all the time in my automated tests, but haven't really used it in my production composition roots (or seen it in others). After reading this post and <a href="/2015/01/06/composition-root-reuse/">Composition Root Reuse</a>, I will try to incorporate builders more often in my production code.</p> <p>Mark - thank you for making me think about this.</p> </div> <div class="comment-date">2015-01-21 21:20 UTC</div> </div> <div class="comment" id="619f5194366a4d52a91ead5628403fd9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#619f5194366a4d52a91ead5628403fd9">#</a></div> <div class="comment-content"> <p> Kenny, thank you for writing. The <a href="/2014/05/19/di-friendly-library">Builder pattern is indeed a good way to implement Facades for a single library</a>. </p> </div> <div class="comment-date">2015-01-22 07:40 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. From Primitive Obsession to Domain Modelling https://blog.ploeh.dk/2015/01/19/from-primitive-obsession-to-domain-modelling 2015-01-19T08:15:00+00:00 Mark Seemann <div id="post"> <p> <em>A string is sometimes not a string. Model it accordingly.</em> </p> <p> Recently, I was reviewing some code that looked like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Get(<span style="color:blue;">string</span>&nbsp;userName) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(userName)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.BadRequest(<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;name.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;user&nbsp;=&nbsp;<span style="color:blue;">this</span>.repository.FindUser(userName.ToUpper()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Ok(user); }</pre> </p> <p> There was a few things with this that struck me as a bit odd; most notably the use of IsNullOrWhiteSpace. When I review code, IsNullOrWhiteSpace is one of the many things I look for, because most people <a href="/2014/11/18/the-isnullorwhitespace-trap">use it incorrectly</a>. </p> <p> This made me ask the author of the code why he had chosen to use IsNullOrWhiteSpace, or, more specifically, what was wrong with a string with white space in it? </p> <p> The answer wasn't what I expected, though. The answer was that it was a <em>business rule</em> that the user name can't be all white space. </p> <p> You can't really argue about business logic. </p> <p> In this case, the rule even seems quite reasonable, but I was just <em>so</em> ready to have a discussion about invariants (pre- and postconditions) that I didn't see that one coming. It got me thinking, though. </p> <h3 id="d41d5e10b2f64e74a3d782d2c88bed62"> Where should business rules go? <a href="#d41d5e10b2f64e74a3d782d2c88bed62" title="permalink">#</a> </h3> <p> It seems like a reasonable business rule that a user name can't consist entirely of white space, but is it reasonable to put that business rule in a Controller? Does that mean that everywhere you have a user name string, you <em>must</em> remember to add the business rule to that code, in order to validate it? That sounds like the kind of duplication that actually hurts. </p> <p> Shouldn't business rules go in a proper Domain Model? </p> <p> This is where many programmers would start to write extension methods for strings, but that's just putting lipstick on a pig. What if you forget to call the appropriate extension method? What if a new developer on the team doesn't know about the appropriate extension method to use? </p> <p> The root problem here is <a href="/2011/05/25/DesignSmellPrimitiveObsession">Primitive Obsession</a>. Just because you <em>can</em> represent a value as a string, it doesn't mean that you always should. </p> <p> The <code>string</code> data type literally can represent <em>any</em> text. A <em>user name</em> (in the example domain above) can <em>not</em> be any text - we've already established that. </p> <h3 id="71902c06c9594f53b248ed9b6f70aa98"> Make it a type <a href="#71902c06c9594f53b248ed9b6f70aa98" title="permalink">#</a> </h3> <p> Instead of string, you can (and should) make <em>user name</em> a type. That type can encapsulate all the business rules in a single place, without violating DRY. This is what is meant by a <a href="http://martinfowler.com/eaaCatalog/domainModel.html">Domain Model</a>. In <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> terminology, a primitive like a string or a number can be turned into a Value Object. <a href="http://lostechies.com/jimmybogard">Jimmy Bogard</a> already <a href="http://grabbagoft.blogspot.dk/2007/12/dealing-with-primitive-obsession.html">covered that ground years ago</a>, but here's how I would define a UserName class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">UserName</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">string</span>&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;UserName(<span style="color:blue;">string</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(value&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;value&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:#2b91af;">UserName</span>.IsValid(value)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;Invalid&nbsp;value.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;value&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.value&nbsp;=&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsValid(<span style="color:blue;">string</span>&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrEmpty(candidate)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;candidate.Trim().ToUpper()&nbsp;==&nbsp;candidate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;TryParse(<span style="color:blue;">string</span>&nbsp;candidate,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">UserName</span>&nbsp;userName) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userName&nbsp;=&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(candidate)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userName&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UserName</span>(candidate.Trim().ToUpper()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">implicit</span>&nbsp;<span style="color:blue;">operator</span>&nbsp;<span style="color:blue;">string</span>(<span style="color:#2b91af;">UserName</span>&nbsp;userName) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;userName.value; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">string</span>&nbsp;ToString() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.value.ToString(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:blue;">object</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;other&nbsp;=&nbsp;obj&nbsp;<span style="color:blue;">as</span>&nbsp;<span style="color:#2b91af;">UserName</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(other&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">base</span>.Equals(obj); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(<span style="color:blue;">this</span>.value,&nbsp;other.value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.value.GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As you can tell, this class protects its invariants. In case you were wondering about the use of ToUpper, it turns out that there's also another business rule that states that user names are case-insensitive, and one of the ways you can implement that is by converting the value to upper case letters. All business rules pertaining to user names are now nicely encapsulated in this single class, so you don't need to remember where to apply them to strings. </p> <p> If you want to know the underlying string, you can either invoke ToString, or take advantage of the implicit conversion from UserName to string. You can also compare two UserName instances, because the class overrides Equals. If you have a string, and want to convert it to a UserName, you can use TryParse. </p> <p> The original code example above can be refactored to use the UserName class instead: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Get(<span style="color:blue;">string</span>&nbsp;candidate) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserName</span>&nbsp;userName; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:#2b91af;">UserName</span>.TryParse(candidate,&nbsp;<span style="color:blue;">out</span>&nbsp;userName)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.BadRequest(<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;name.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;user&nbsp;=&nbsp;<span style="color:blue;">this</span>.repository.FindUser(userName); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.Ok(user); }</pre> </p> <p> This code has the same complexity as the original example, but now it's much clearer what's going on. You don't have to wonder about what looks like arbitrary rules; they're all nicely encapsulated in the UserName class. </p> <p> Furthermore, as soon as you've left the <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">not Object-Oriented boundary of your system</a>, you can express the rest of your code in terms of the Domain Model; in this case, the UserName class. Here's the IUserRepository interface's Find method: </p> <p> <pre><span style="color:#2b91af;">User</span>&nbsp;FindUser(<span style="color:#2b91af;">UserName</span>&nbsp;userName); </pre> </p> <p> As you can tell, it's expressed in terms of the Domain Model, so you can't accidentally pass it a string. From that point, since you're receiving a UserName instance, you <em>know</em> that it conforms to all business rules encapsulated in the UserName class. </p> <h3 id="39e98690413d4675888c69f77f82421d"> Not only for OOP <a href="#39e98690413d4675888c69f77f82421d" title="permalink">#</a> </h3> <p> While I've used the term <em>encapsulation</em> once or twice here, this way of thinking is in no way limited to Object-Oriented Programming. <a href="http://fsharpforfunandprofit.com">Scott Wlaschin</a> describes how to <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-single-case-dus">wrap primitives in meaningful types in F#</a>. The motivation and the advantages you gain are the same in Functional F# as what I've described here. </p> <h3 id="7172fd9ca69c467e8123a20f43ea76c2"> Over-engineering? <a href="#7172fd9ca69c467e8123a20f43ea76c2" title="permalink">#</a> </h3> <p> Isn't this over-engineering? A 56 lines of code class instead of a string? Really? The answer to such a question is always context-dependent, but I rarely find that it is (over-engineering). You can create a Value Object like the above UserName class in less than half an hour - even if you use Test-Driven Development. When I created this example, I wrote 27 test cases distributed over five test methods in order to make sure that I hadn't done something stupid. It took me 15 minutes. </p> <p> You may argue that 15 minutes is a lot, compared to the 0 minutes it would take you if you'd 'just' use a string. On the surface, that seems like a valid counter-argument, but perhaps you're forgetting that with a primitive string, you still need to write validation and business logic 'around' the string, and you have to remember to apply that logic consistently across your entire code base. My guess is that you'll spend more than 15 minutes on doing this, and troubleshooting defects that occur when someone forgets to apply one of those rules to a string in some other part of the code base. </p> <h3 id="71a8b13b5dc346beb0203e5c63e1ddcc"> Summary <a href="#71a8b13b5dc346beb0203e5c63e1ddcc" title="permalink">#</a> </h3> <p> Primitive values such as strings, integers, decimal numbers, etc. often represent concepts that are constrained in some ways; they're not just <em>any</em> string, <em>any</em> integer, or <em>any</em> decimal number. Ask yourself if extreme values (like the entire <a href="http://amzn.to/19W4JHk">APPP</a> manuscript, <a href="http://msdn.microsoft.com/en-us/library/system.int32.minvalue.aspx">Int32.MinValue</a>, and so on) are suitable for such variables. If that's not the case, consider introducing a Value Object instead. </p> <p> If you want to learn more about Encapsulation, you can watch my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID Pluralsight course</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2e2ffc9b7d104c118a84ae218a7beea2"> <div class="comment-author">Johannes <a href="#2e2ffc9b7d104c118a84ae218a7beea2">#</a></div> <div class="comment-content"> <p> Mark, I enjoyed your article. I feel that this is the way to go. Too often have I seen people using arrays if bytes for images and strings for email addresses. </p> <p> An important aspect of DDD is within the language and when there are people talking about user names all the time, that usually indicates that usernames are an important aspect of what is going on and should be modeled explicitly and not use primitives. </p> <p> The solution doesn't look overengineered to me, although I had to defend similar code before for the same reason. Not only is it now a good place to put further validation, but also allows further distinction using other types and a polymorphism (if carefully used, they can be a delight), each of which covers different invariants. </p> <p> A UserName can be moved around and used to retrieve objects. But maybe you can't do that with a class of the type InvalidUsername. In another example, maybe there are functions that deal with email eddresses - which I have often seen modeled as strings. Some accept InvalidEmailAddress objects, others accept ValidEmail addresses. While it doesn't reduce the amount of boilerplate null checks, a SendEmail function wouldn't have to do </p> <pre><code> ... SendEmail(EmailAddress email) { if(email.IsValid()) { email.Send() } } </code></pre> <p> One could simply make it to only accept valid email addresses. Their mere existence would guarantee, that the email address is valid. One could then write functions dealing with valid email addresses and some dealing with invalid ones. You can log and analyze bad account creation requests with the invalid ones, but not send email to them and prevent everyone else from doing so, depending on what kind of email address is being passed around. </p> </div> <div class="comment-date">2015-01-20 08:16 UTC</div> </div> <div class="comment" id="a639df29250f4dcd8431008be665bd8b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a639df29250f4dcd8431008be665bd8b">#</a></div> <div class="comment-content"> <p> Johannes, thank you for writing. You are right, it's possible to take the idea further in the way you describe. Again, such techniques aren't even limited to OOP; for instance, Scott Wlaschin explains <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">how to make illegal states unrepresentable</a> in F#. </p> <p> In fact, the Functional approach using Sum Types (Discriminated Unions) is nicer, because it doesn't rely on inheritance :) </p> </div> <div class="comment-date">2015-01-20 08:52 UTC</div> </div> <div class="comment" id="c54d27e6cfcd4a87b97369fc11b2d24b"> <div class="comment-author"><a href="http://www.scrambledbrains.net">Mike McG</a> <a href="#c54d27e6cfcd4a87b97369fc11b2d24b">#</a></div> <div class="comment-content"> <p> Interesting article. I have no qualms about creating domain types around primitives per se, but IMHO there should be a bit more complexity in the invariants than a single condition before resorting to that. In this case, I would think that the username constraint should be implemented in the User class. If other entities shared the constraint (e.g. Company.AccountName), the logic might be refactored into a NameValidator, and then referenced concretely by the entities. </p> <p> A string holding a strong password would be a good candidate for promoting to something smarter. </p> </div> <div class="comment-date">2015-01-20 23:10 UTC</div> </div> <div class="comment" id="fc8ca9be75c94e9d96d7e50580a0bc27"> <div class="comment-author">Suamere <a href="#fc8ca9be75c94e9d96d7e50580a0bc27">#</a></div> <div class="comment-content"> <p> Mark,</p> <p> This is exactly right. I honestly have no comment on the concept because I have changed primitives to value objects a number of times and use something similar. </p> <p> Quick side-note, and this may just be a preference: In the TryParse, a Trim()'d value is being passed into the constructor. So if I want to create two usernames &quot;&nbsp;Suamere&nbsp;&quot; and &quot;Suamere&quot;, the second will fail since, unbeknownst to me, the first had been successfully created with Trim(). It seems to me that &quot;Do not allow pre/post white-space&quot; is a business rule. Therefore, if there is white-space, I consider that to be breaking a business rule. </p> <p> My main motivation for this comment is that this UserName Object shows a perfect example of a pattern I've seen when using the TryParse pattern (or Monads). That is: The logic in the TryParse is frequently an exact duplicate of logic in another method such as the constructor. Though instead of throwing exceptions, TryParse gracefully informs the consumer of success/failure. </p> <p> So business logic is duplicated in the CTOR(), IsValid(), and the TryParse(). Instead, I consider this pattern: </p> <p> <pre> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;UserName(<span style="color:blue;">string</span>&nbsp;<span style="color:blue;">value</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CheckIsValid(<span style="color:blue;">value</span>,&nbsp;<span style="color:blue;">true</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.<span style="color:blue;">value</span>&nbsp;=&nbsp;<span style="color:blue;">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;TryParse(<span style="color:blue;">string</span>&nbsp;candidate,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:#2b91af;">UserName</span>&nbsp;userName) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userName&nbsp;=&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!CheckIsValid(candidate,&nbsp;<span style="color:blue;">false</span>))&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userName&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">UserName</span>(candidate); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsValid(<span style="color:blue;">string</span>&nbsp;candidate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;CheckIsValid(candidate,&nbsp;<span style="color:blue;">false</span>); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;CheckIsValid(<span style="color:blue;">string</span>&nbsp;candidate,&nbsp;<span style="color:blue;">bool</span>&nbsp;throwExceptions) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#008000;">//Brevity&nbsp;follows,&nbsp;but&nbsp;can&nbsp;be&nbsp;split&nbsp;up&nbsp;into&nbsp;more&nbsp;granular&nbsp;rules&nbsp;and&nbsp;exceptions</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(candidate)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(throwExceptions)&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(&quot;candidate&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:blue;">string</span>.Equals(candidate,&nbsp;candidate.Trim(),&nbsp;<span style="color:#2b91af;">StringComparison</span>.OrdinalIgnoreCase)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(throwExceptions)&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>(&quot;Invalid&nbsp;value.&quot;,&nbsp;&quot;candidate&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} </pre> </p> <p> Let me know what you think!<br> ~Suamere, Steven Fletcher </p> </div> <div class="comment-date">2015-01-20 22:00 UTC</div> </div> <div class="comment" id="f51a309f5b4441d09cd108665cf81474"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f51a309f5b4441d09cd108665cf81474">#</a></div> <div class="comment-content"> <p> Mike, thank you for writing. A business rule doesn't have to be complex in order to be important. The point isn't only to encapsulate complex logic, but to make sure that (any) business logic is applied <em>consistently</em>. </p> <p> In my experience, things often start simple, but evolve (or do they devolve?) into something more complex; when do you draw the line? </p> </div> <div class="comment-date">2015-01-21 12:57 UTC</div> </div> <div class="comment" id="4a2c83372fd949d0a5b2c7524d557025"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4a2c83372fd949d0a5b2c7524d557025">#</a></div> <div class="comment-content"> <p> Steven, thank you for writing. You exhibit traits of critical thinking, which is always good when dealing with business logic :) In the end, it all boils down to what exactly the business rule is. As you interpret the business rule, you rephrase it as "Do not allow pre/post white-space", but that's not the business rule I had in mind. </p> <p> Since I'm the author of the blog post, I have the luxury of coming up with the example, and thus, in this case, defining the business rule. The business rule I had in mind is that user names are case-insensitive, and they're also insensitive to leading and trailing white space. Thus, " Suamere " and "Suamere" are considered to be <em>two representations</em> of the same user name, just as "FOO" and "foo" represent the same user name. The <em>canonicalised</em> representation of these two user names are "SUAMERE" and "FOO", respectively. </p> <p> That's also the reason I chose the name TryParse; it's equivalent to <a href="https://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx">DateTime.TryParse</a>, which should be a well-known .NET idiom. If I parse the two strings "2015-01-21" and " 21. januar 2015 " on my machine (which is running with the da-DK locale), I get the exact same value from both strings. Notice that DateTime.TryParse also blissfully ignores leading and trailing white space. This is all correct according to <a href="http://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. </p> <p> If all I'd wanted to do was to convert an already valid string into a UserName instance, I'd have implemented an explicit conversion, which is <a href="http://grabbagoft.blogspot.dk/2007/12/dealing-with-primitive-obsession.html">what Jimmy Bogard did in his article</a>. In fact, such an explicit conversion doesn't conflict with the current TryParse method, so I'd find it quite reasonable to add that as well, if any client had the need for it. </p> </div> <div class="comment-date">2015-01-21 13:35 UTC</div> </div> <div class="comment" id="cbfc02fd9bb64455b3e835d2a7c3fb89"> <div class="comment-author">Jeff Soper <a href="#cbfc02fd9bb64455b3e835d2a7c3fb89">#</a></div> <div class="comment-content"> <p> This question might be slightly besides the point, but I'm having trouble with the bit about UserName values being case-insensitive. I take that to mean that you can create a UserName with all uppercase, all lowercase, or any combination thereof, and they'll all be treated equally. </p> <p> To me, that doesn't mean that you have to pass in an uppercase-only value to the constructor (or TryParse() method, for that matter) in order to successfully instantiate a UserName. Was that your intent? </p> <p> I created a <a href="https://gist.github.com/Lumirris/fefc77e7abc655630124">Gist</a> with some tests to illustrate what I'm talking about: I can instantiate with "JEFF", but not with "jeff", using either the constructor directly or the TryParse method. </p> </div> <div class="comment-date">2015-01-23 17:27 UTC</div> </div> <div class="comment" id="63ead916bc6942c08248818c55998851"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#63ead916bc6942c08248818c55998851">#</a></div> <div class="comment-content"> <p> Jeff, I attempted to run your unit tests, and the only one that fails is, as expected, if you attempt to use the constructor with "jeff"; TryParse with "jeff" works. </p> <p> Would it have helped if the constructor was private? </p> </div> <div class="comment-date">2015-01-23 19:08 UTC</div> </div> <div class="comment" id="2b9eb0e8e339498a999292a0bd7f4ffd"> <div class="comment-author">Jeff Soper <a href="#2b9eb0e8e339498a999292a0bd7f4ffd">#</a></div> <div class="comment-content"> <p> I stand corrected regarding the failing tests; I worked with the tests again, and indeed, only the constructor method failed passing in "jeff" in lowercase. </p> <p> Making the constructor private would clarify things in that there would only be one way for a client to instantiate UserName, which happens to ensure anything passed into the private constructor is all upper-case. </p> <p> That seems to better match the stated business rule, that UserName values are case-insensitive, rather than a rule that prohibits anything but upper-case values. In any case, it's not quite central to the point of your article, so I thank you for taking the time to reply anyway. </p> </div> <div class="comment-date">2015-01-28 19:42 UTC</div> </div> <div class="comment" id="e76fb4746ebb40cf90a322d4f6f8f39c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e76fb4746ebb40cf90a322d4f6f8f39c">#</a></div> <div class="comment-content"> <p> Jeff, I think we fundamentally agree, and I <em>do</em> understand why you'd find it surprising that the constructor accepts "JEFF", but not "jeff". FWIW, I follow (a lot) of rules (of thumb) when I design APIs, and one of them is that the purpose of constructors is to initialise objects. A constructor may perform validation, but must not perform work in the sense of transforming the input or producing side-effects. In other words, constructors are used to initialise objects with <em>valid</em> values. </p> <p> Whenever I need to transform data as part of initialisation, I often create some sort of factory method for that explicit purpose. A static TryParse method is one of many options available when such a need arises. The point is exactly that the input may not start out being 'valid', but it can be transformed into a valid representation. Going from "jeff" to UserName("JEFF") is a <em>conversion</em>. </p> <p> This idea of constructors only accepting valid values may seem a bit foreign at first, but if you follow it as a general principle, you get a code base that's easier to reason about, because it's <em>consistent</em>. It's probably because I'm so used to this principle that I just left the constructor public, following another principle of mine, which is that there's no reason to make anything internal or private if the class or member can properly protect its invariants. </p> <p> Sorry to rant; you just inspired me to explain some of my underlying design thoughts that caused me to arrive at this particular solution. </p> </div> <div class="comment-date">2015-01-28 21:19 UTC</div> </div> <div class="comment" id="4399eda07fd0412a9a90326f0f0bfffa"> <div class="comment-author">Julien Vulliet <a href="#4399eda07fd0412a9a90326f0f0bfffa">#</a></div> <div class="comment-content"> <p> Thanks for your post, very informative as usual. I would add a few insights. </p> <p>As much as it can feel Over engineering, I would totally agree and say it doesn't. I deal with GPU resource creation (Direct3D11) on a daily basis and this has helped me so much and saved me headaches (for example a Buffer size must have more than 0 elements, failing to do so gives you a cryptic runtime error than you can only catch error message by enabling Debug Device and looking at Debug output window!</p> <p>Using a simple BufferElementCount in that case enforces the fact that I provide a correct value, and throw a meaningful exception early in the process. By passing a BufferElementCount I can (almost) safely expect that my buffer gets created.</p> <p>On the lines of code, I would expect that the initial 50 lines would largely be overtaken by rewriting the same logic, but scattered across the whole code (plus likely adding try/catch blocks in many places on top of it), so I'm pretty sure doing the 0 lines version would lead to more code at the end. Plus 50 lines of code to save be a deep debug session on half million lines code base is a trade-off I'll happily take!</p> <p>Also as a little bonus, for simple cases I wrote a little snippet so I guess I would share it here <a href="https://gist.github.com/mrvux/fd4d8db7e950727afa46">Domain Primitive</a>. Now it takes even less than 2 minutes in most simple cases.</p> </div> <div class="comment-date">2015-03-18 14:41 UTC</div> </div> <div class="comment" id="de671e0ed74148b28561f37f0c1a4818"> <div class="comment-author">Alan Evans <a href="#de671e0ed74148b28561f37f0c1a4818">#</a></div> <div class="comment-content"> <p> Your Equals is fragile. Because the class is not sealed, one could descend from it and break the equality contract. </p> <p>Example of how to break through descending: <pre> &nbsp;&nbsp;&nbsp;&nbsp;public class Domain​UserName : UserName{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private string domain; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Equals written to compare both username and domain &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;UserName u1 = new DomainUserName("BOB", "GITHUB"); &nbsp;&nbsp;&nbsp;&nbsp;UserName u2 = new UserName("BOB"); &nbsp;&nbsp;&nbsp;&nbsp;u1.Equals(u2) //will use DomainUserName's Equals and return false &nbsp;&nbsp;&nbsp;&nbsp;u2.Equals(u1) //will use UserName's Equals and return true </pre> </p> <p> But equality contract must be symmetrical. So either seal the class or else compare GetTypes() as example here: <a href="https://msdn.microsoft.com/en-us/library/vstudio/336aedhh%28v=vs.100%29.aspx">msdn.microsoft.com/en-us/library/vstudio/336aedhh%28v=vs.100%29.aspx</a> </p> </div> <div class="comment-date">2015-03-18 14:41 UTC</div> </div> <div class="comment" id="89e3584c29524090968dbabc26003f35"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#89e3584c29524090968dbabc26003f35">#</a></div> <div class="comment-content"> <p> Alan, thank you for writing. You're right; I'd never considered this - most likely because I never use inheritance. Therefore, my default coping strategy would most likely be to make the class sealed by default, but otherwise, explicitly comparing GetType() return values would have to do. </p> <p> Thank you for teaching me something today! </p> </div> <div class="comment-date">2015-04-10 16:08 UTC</div> </div> <div class="comment" id="ef864dfebdb14e7386ff2feedcbb8d67"> <div class="comment-author"><a href="http://www.rahulpnath.com">Rahul Nath</a> <a href="#ef864dfebdb14e7386ff2feedcbb8d67">#</a></div> <div class="comment-content"> <p> Great article and completely agree on having primitive types wrapped up into its own class. I had few questions around how this would be applied to provide a good validation feedback to consuming clients. If we want to provide more details on why the 'user name is invalid', like say 'User name should not be small letters' and 'User name should not have trailing spaces' how would we handle this in the class. How would the class communicate out the details of the rules that it is abstracting to its consuming clients. </p> <p> When accepting multiple such 'classes' in a endpoint, what would be a suggested validation approach. For instance if we are to look at a similar endpoint, which takes in 'Name' and 'Phone' number(or even more in the case of POST endpoint where we are creating a new user), wouldnt the controller soon be overladed with a lot of such TryParse calls. </p> <p> From the above comment of constructor only accepting valid values, where would it make sense to use the constructor as opposed to TryParse. Is it at the persistence boundary of the application? </p> </div> <div class="comment-date">2015-11-21 0:51 UTC</div> </div> <div class="comment" id="5afdaffc886343cf85fbb4a082842a82"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5afdaffc886343cf85fbb4a082842a82">#</a></div> <div class="comment-content"> <p> Rahul, thank you for writing. As a general rule, at the boundary of an application, all input must be considered evil until proven otherwise. That means that if you try to pass unvalidated input values into constructors, you should expect exceptions to be thrown on a regular basis. Since exceptions are for <em>exceptional</em> cases, that's most likely not the best way to go forward. </p> <p> Instead, domain objects could offer Validate methods. These would be a bit like TryParse methods, but instead of returning a primitive boolean value, they'd have to return a list of messages. If there are no messages, the input is good. If there are messages, the input is bad. You can encapsulate such a validation result in an object if you think the rule about messages/no messages is too implicit. Such Validate methods could still take <code>out</code> parameters like TryParse methods if you want to validate and potentially convert into a domain object in one go. </p> <p> This isn't particularly elegant in languages like C# or Java, but due to the lack of sum types in these languages, that's the best we can do. </p> <p> In languages with sum types, you can use the Either monad and applicative composition to address this type of problem much more elegantly. A good place to start would be <a href="http://fsharpforfunandprofit.com">Scott Wlaschin</a>'s article on <a href="http://fsharpforfunandprofit.com/posts/recipe-part2">Railway Oriented Programming</a>. </p> </div> <div class="comment-date">2015-11-21 11:44 UTC</div> </div> <div class="comment" id="e69240be7d4f41e68cf5a170e2a4e467"> <div class="comment-author">Eugene Grebenyuk <a href="#e69240be7d4f41e68cf5a170e2a4e467">#</a></div> <div class="comment-content"> <p> Mark, implicit conversion operator should never throw exception. In the example, it can throw NullReferenceException when UserName is null. What would you recommend to fix that issue? </p> </div> <div class="comment-date">2016-05-25 14:54 UTC</div> </div> <div class="comment" id="e25d87a1d37c4ead8b51857c430a97ff"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e25d87a1d37c4ead8b51857c430a97ff">#</a></div> <div class="comment-content"> <p> Eugene, thank you for writing. That's probably a good rule, but I admit I hadn't thought about it. One option is to convert it to an explicit conversion instead. Another option is to forgo the conversion operators all together. The conversion ability isn't the important point in this article. </p> <p> A third option is to leave it as is. In the code bases I control, null is never an appropriate value, so if null appears anywhere, I'd consider it a bug somewhere else. </p> <p> To be realistic, though, I think I'd prefer one of the two first options, as I tend to agree with the <a href="https://www.python.org/dev/peps/pep-0020">Zen of Python</a>: <em>Explicit is better than implicit.</em> </p> </div> <div class="comment-date">2016-05-25 16:15 UTC</div> </div> <div class="comment" id="9a75fa8ea2214f848bd7591b6d23b70c"> <div class="comment-author">Moiz <a href="#9a75fa8ea2214f848bd7591b6d23b70c">#</a></div> <div class="comment-content"> <p> Insightful post Mark! <br><br> I have a question, if we are implementing a class that implements Parse and TryParse menthods and this class requires dependency, how should that be passed? <br><br> Consider following example - MobileNumber requires a list of valid area codes, I am passing this dependency in the constructor and Parse and TryParse method: <pre> public class MobileNumber { &nbsp;&nbsp;&nbsp;&nbsp;public MobileNumber(IEnumerable&lt;string&gt; areaCodes, string countryCode, string phoneNumber) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Something */ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public static MobileNumber Parse(IEnumerable&lt;string&gt; areaCodes, string countryCode, string phoneNumber) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Something */ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public static bool TryParse(IEnumerable&lt;string&gt; areaCodes, string countryCode, string phoneNumber, out MobileNumber mobileNumber) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Something */ &nbsp;&nbsp;&nbsp;&nbsp;} } </pre> Is it a right design? </p> </div> <div class="comment-date">2018-02-14 11:46 UTC</div> </div> <div class="comment" id="99b5036b792844dea2a5291883270831"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#99b5036b792844dea2a5291883270831">#</a></div> <div class="comment-content"> <p> Moiz, thank you for writing. Is it the right design? As usual, It Depends™. Which problem are you trying to solve? What does the input look like? What should the result be of attempting to parse various examples? </p> <p> It can often be illuminating to write a couple of parametrised tests. What would such tests look like? </p> </div> <div class="comment-date">2018-02-14 18:20 UTC</div> </div> <div class="comment" id="0bc3789035754663aa52d7e1f5ff11a0"> <div class="comment-author"><a href="http://blog.peterritchie.com">Peter Ritchie</a> <a href="#0bc3789035754663aa52d7e1f5ff11a0">#</a></div> <div class="comment-content"> <p> A benefit of this technique is that it doesn't prescribe <i>where</i> the business logic should be; only where <ul>access to it</ul> should be. And in Domain Modelling that's through a Domain type (entity). That entity can certainly delegate to something else, but the entity controls the unit of work in which that business rule applies. </p> </div> <div class="comment-date">2019-11-26 14:14 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. 10 tips for better Pull Requests https://blog.ploeh.dk/2015/01/15/10-tips-for-better-pull-requests 2015-01-15T10:06:00+00:00 Mark Seemann <div id="post"> <p> <em>Making a good Pull Request involves more than writing good code.</em> </p> <p> The Pull Request model has turned out to be a great way to build software in teams - particularly for distributed teams; not only for open source development, but also in enterprises. Since some time around 2010, I've been reviewing Pull Requests both for my open source projects, but also as a team member for some of my customers, doing closed-source software, but still using the Pull Request work flow internally. </p> <p> During all of that time, I've seen many great Pull Requests, and some that needed some work. </p> <p> A good Pull Request involves more than just some code. In most cases, there's one or more reviewer(s) involved, who will have to review your Pull Request in order to evaluate whether it's a good fit for inclusion in the code base. Not only must you produce good code, but you must also cater to the person(s) doing the review. </p> <p> Here's a list of tips to make your Pull Request better. It isn't exhaustive, but I think it addresses some of the more important aspects of creating a good Pull Request. </p> <h3 id="e54d3e63e198471bb34b92606326d0e3"> 1. Make it small <a href="#e54d3e63e198471bb34b92606326d0e3" title="permalink">#</a> </h3> <p> A small, focused Pull Request gives you the best chance of having it accepted. </p> <p> The first thing I do when I get a notification about a Pull Request is that I look it over to get an idea about its size. It takes time to properly review a Pull Request, and in my experience, the time it takes is exponential to the size; the relationship certainly isn't linear. </p> <p> If I get a big Pull Request for an open source project, I do realize that the submitter has most likely already put in substantial work in his or her spare time, so I do go to some lengths to review a big Pull Request, even if I think it's too big - particularly when it looks like it's a first-time contributor. Still, if the Pull Request is big, I'll need to <em>schedule</em> time to review it: I can't review a big chunk of code using five minutes here and five minutes there; I need contiguous time to do that. This already introduces a delay into the review process. </p> <p> If I get a big Pull Request in a professional setting (i.e. where the submitter is being paid to write the code), I often reject the Pull Request simply because of the size of it. </p> <p> How small is small enough? Obviously, it depends on what the Pull Request is about, but a Pull Request that touches less than a dozen files isn't too bad. </p> <h3 id="9456245213574e5b879092ad3f658fc0"> 2. Do only one thing <a href="#9456245213574e5b879092ad3f658fc0" title="permalink">#</a> </h3> <p> Just as the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> states that a class should have only one responsibility, so should a Pull Request address only a single concern. </p> <p> Imagine, as a counter-example, that you submit a Pull Request that addresses three independent, separate concerns (let's call them A, B, and C). The reviewer may immediately agree with you that A and C are valid concerns, and that your solution is correct. However, the reviewer has issues with your B concern. Perhaps he or she thinks it's not a concern at all, or she disagrees with the way you've addressed it. </p> <p> This becomes the start of a lengthy discussion about concern B, and how it's being addressed. This discussion can go on for days (particularly if you're in different time zones), while you attempt to come to agreement; perhaps you'll need to make changes to your Pull Request to address the reviewer's concerns. This all takes time. </p> <p> It may, in fact, take so much time that other commits have been merged into <em>master</em> in the meantime, and your Pull Request has fallen so much behind that it no longer can be automatically merged. Welcome to Merge Hell. </p> <p> All that time, your perfectly acceptable solutions to the A and C concerns are sitting idly in your Pull Request, adding absolutely no value to the overall code base. </p> <p> Instead, submit three independent Pull Requests that address respectively A, B, and C. If you do that, the reviewer who agrees with A and C will immediately accept two of those three Pull Requests. In this way, your non-controversial contributions can immediately add value to the code base. </p> <p> The more concerns you address in a single Pull Request, the bigger the risk that at least one of them will block acceptance of your contribution. Do only one thing per Pull Request. It also helps you make each Pull Request smaller. </p> <h3 id="dc285209d34e43f2bfe6061c7d6b8597"> 3. Watch your line width <a href="#dc285209d34e43f2bfe6061c7d6b8597" title="permalink">#</a> </h3> <p> The reviewer of your Pull Request will most likely be reviewing your contribution using a diff tool. Both <a href="https://github.com">GitHub</a> and <a href="https://www.atlassian.com/software/stash">Stash</a> provide browser-based diff views for reviewing. A reviewer can even configure the diff view to be side-by-side; it makes it much easier to understand what changes are included in the contribution, but it also means that the code must be readable on half a screen. </p> <p> If you have wide lines, you force the reviewer to scroll horizontally. </p> <p> There are <a href="http://richarddingwall.name/2008/05/31/is-the-80-character-line-limit-still-relevant">many reasons to keep line width below 80 characters</a>; making your code easy to review just adds another reason to that list. </p> <h3 id="856ba43cbf9148a384bea7e15f2ca563"> 4. Avoid re-formatting <a href="#856ba43cbf9148a384bea7e15f2ca563" title="permalink">#</a> </h3> <p> You may feel the urge to change the formatting of the existing code to fit 'your' style. Please abstain. </p> <p> Every byte you change in the source code shows up in the diff views. Some diff viewers have options to ignore changes of white space, but even with this option on, there are limits to what those diff viewers can ignore. Particularly, they can't ignore if you move code around, so please don't do that. </p> <p> If you really need to address white space issues, move code around within files, change formatting, or do other stylistic changes to the code, please do so in an isolated pull request that does only that, and state so in your Pull Request comment. </p> <h3 id="b48faf1499cf4038b9bf2abead031f5e"> 5. Make sure the code builds <a href="#b48faf1499cf4038b9bf2abead031f5e" title="permalink">#</a> </h3> <p> Before submitting a Pull Request, build it on your own machine. True, <em>works on my machine</em> isn't particularly useful, but it's a minimum bar. If it <em>doesn't work on your machine</em>, it's unlikely to work on other machines as well. </p> <p> Watch out for compiler warnings. They may not prevent you from compiling, so you may not notice them if you don't explicitly look for them. However, if your Pull Request causes (more) compiler warnings, a reviewer may reject it; I do. </p> <p> If the project has a build script, try to run that, and only submit your pull request if the build succeeds. In many of my open source projects, I have a build script that (among other things) treats warnings as errors. Such a build script may automate or implement various rules for that particular code base. Use it before submitting, because the reviewer most likely will use it before merging your branch. </p> <h3 id="5efded28438149de80acde5ebd2ca87f"> 6. Make sure all tests pass <a href="#5efded28438149de80acde5ebd2ca87f" title="permalink">#</a> </h3> <p> Assuming that the code base in question has automated tests, make sure all tests pass before submitting a Pull Request. </p> <p> This should go without saying, but I regularly receive Pull Requests where one or more tests are failing. </p> <h3 id="fcd3da7df1294380b0b50b092ae098a9"> 7. Add tests <a href="#fcd3da7df1294380b0b50b092ae098a9" title="permalink">#</a> </h3> <p> Again, assuming that the code in question already has automated (unit) tests, do add tests for the code you submit. </p> <p> It doesn't often happen that I receive a Pull Request without tests, but when I do, I often reject it. </p> <p> This isn't a hard rule. There are various cases where you may need to add code without test coverage (e.g. when adding a <a href="http://xunitpatterns.com/Humble%20Object.html">Humble Object</a>), but if it can be tested, it should be tested. </p> <p> You'll need to follow the testing strategy already established for the code base in question. </p> <h3 id="698cd08fcaa04ef0bed29be8a2e2634d"> 8. Document your reasoning <a href="#698cd08fcaa04ef0bed29be8a2e2634d" title="permalink">#</a> </h3> <p> Self-documenting code rarely is. </p> <p> Yes, <a href="http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode">code comments are apologies</a>, and I definitely prefer well-named operations, types, and values over comments. Still, when writing code, you often have to make decisions that aren't self-evident (particularly when dealing with Business 'Logic'). </p> <p> Document <em>why</em> you wrote the code in the way you did; not what it does. </p> <p> My preferred priority is this: <ol> <li> <strong>Self-documenting code:</strong> You <em>can</em> make some decisions about the code self-documenting. <a href="http://amzn.to/XCJi9X">Clean Code</a> is literally a book on how to do that. </li> <li> <strong>Code comments:</strong> If you can't make the code sufficiently self-documenting, add a code comment. At least, the comment is co-located with the code, so even in the unlikely event that you decide to change version control system, the comment is still preserved. <a href="https://github.com/GreanTech/AtomEventStore/blob/e5fe679c08abb6fe108509117651d29dce17e270/AtomEventStore/AtomEventStorage.cs#L68-L71">Here's an example where I found a comment more appropriate than attempting to design my way out of the problem.</a> </li> <li> <strong>Commit messages:</strong> Most version control systems give you the opportunity to write a commit message. Most people don't bother putting anything other than a bare minimum into these, but you can document your reasoning here as well. Sometimes, you'll need to explain why you're doing things in a certain order. This doesn't fit well in code comments, but is a good fit for a commit message. As long as you keep using the same version control system, you preserve these commit messages, but they're once removed from the actual source code, and you may loose the messages if you change to another source control system. <a href="https://github.com/GreanTech/AtomEventStore/commit/615cdee2c4d675d412e6669bcc0678655376c4d1">Here's an example where I felt the need to write an extensive commit message</a>, but <a href="https://github.com/GreanTech/AtomEventStore/commit/e5fe679c08abb6fe108509117651d29dce17e270">I don't always do that</a>. </li> <li> <strong>Pull Request comments:</strong> Rarely, you may find yourself in a situation where none of the above options are appropriate. In Pull Request management systems such as GitHub or Stash, you can also add custom messages to the Pull Request itself. This message is twice removed from the actual source code, and will only persist as long as you keep using the same host. If you move from e.g. CodePlex to GitHub, you'll loose those Pull Request messages. Still, occasionally, I find that I need to explain myself to the reviewer, but the explanation involves something external to the source code anyway. <a href="https://github.com/GreanTech/AtomEventStore/pull/70">Here's an example where I found that a reasonable approach.</a> </li> </ol> You don't need to explain the obvious, but do consider erring on the side of caution. What's obvious to you today may not be obvious to anyone else, or to you in three months. </p> <h3 id="e5bfc9498c704d31b45bd111599764bd"> 9. Write well <a href="#e5bfc9498c704d31b45bd111599764bd" title="permalink">#</a> </h3> <p> Write good code, but also write good prose. This is partly subjective, but there are rules for both code and prose. Code has correctness rules: if you break them, it doesn't compile (or, for interpreted languages, it fails at run-time). </p> <p> The same goes for the prose you may add: Code comments. Commit messages. Pull Request messages. </p> <p> Please use correct spelling, grammar, and punctuation. If you don't, your prose is harder to understand, and your reviewer is a human being. </p> <h3 id="ee2c4948822641c5b31d5c6918550d15"> 10. Avoid thrashing <a href="#ee2c4948822641c5b31d5c6918550d15" title="permalink">#</a> </h3> <p> Sometimes, a reviewer will point out various issues with your Pull Request, and you'll agree to address them. </p> <p> This may cause you to add more commits to your Pull Request branch. There's nothing wrong with that per se. However, this <em>can</em> lead to unwarranted thrashing. </p> <p> As an example, your pull request may contain five commits: A, B, C, D, and E. The reviewer doesn't like what you did in commits B and C, so she asks you to remove that code. Most people do that by checking out their pull request branch and deleting the offending code, adding yet another commit (F) to the commit list: [A, B, C, D, E, F] </p> <p> Why should we have to merge a series of commits that first adds unwanted code, and then removes it again? It's just thrashing; it doesn't add any value. </p> <p> Instead, remove the offending commits, and force push your modified branch: [A, D, E]. While under review, you're the sole owner of that branch, so you can modify and force push it all you want. </p> <p> Another example of thrashing that I see a lot is when a Pull Request is becoming old (often due to lengthy discussions): in these cases, the author regularly merges his or her branch with <em>master</em> to keep the Pull Request branch up to date. </p> <p> Again: why do I have to look at all those merge commits? You are the sole owner of that branch. Just <a href="http://git-scm.com/book/en/v2/Git-Branching-Rebasing">rebase</a> your Pull Request branch and force push it. The resulting commit history will be cleaner. </p> <h3 id="a2341eb3d9e140e993343d6d007d0d26"> Summary <a href="#a2341eb3d9e140e993343d6d007d0d26" title="permalink">#</a> </h3> <p> One or more persons will review your Pull Request. Don't make your reviewer work. </p> <p> The more you make your reviewer work, the greater the risk is that your Pull Request will be rejected. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="44dc4274a8ac474d9e094bb79de26a57"> <div class="comment-author"><a href="https://samfrances.co.uk/">Sam Frances</a> <a href="#44dc4274a8ac474d9e094bb79de26a57">#</a></div> <div class="comment-content"> <p> How do you balance the advice to write small, focused Pull Requests with the practical necessity of sometimes bundling refactoring in with features? Especially given the fact that most workplaces inevitably prioritise merging features. </p> </div> <div class="comment-date">2021-06-23 16:03 UTC</div> </div> <div class="comment" id="2ffe7f88503d495ba8b5292eaa004cbb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2ffe7f88503d495ba8b5292eaa004cbb">#</a></div> <div class="comment-content"> <p> Sam, thank you for writing. Even without refactoring, it's common that a feature is so large that you can't implement it as a single, focused pull request. The best way to address that issue is to hide the work in progress behind a feature flag. You can do the same with refactoring. </p> <p> As Kent Beck puts it: <blockquote> <p> "for each desired change, make the change easy (warning: this may be hard), then make the easy change" </p> <footer><cite><a href="https://twitter.com/kentbeck/status/250733358307500032">Kent Beck</a></cite></footer> </blockquote> You may need to first refactor to 'make room' for the new feature. I'd often put that in an isolated pull request and send that first. If anyone complains that I'm doing refactoring work instead of feature work, I'd truthfully respond that I'm doing the refactoring in order to be able to implement the feature. </p> <p> I consider this to be part of being professional. It's how software should be developed, and <a href="/2019/03/18/the-programmer-as-decision-maker">I think that non-technical stakeholders should have little to say about <em>how</em> things are done</a>. You don't have to tell them every little detail about how you write code. You shouldn't have to ask for permission to do this, and you shouldn't have to inform them that that's what you're doing. </p> <p> <a href="/2021/06/14/new-book-code-that-fits-in-your-head">My new book</a> contains a realistic and practical example of a feature developed behind a feature flag. </p> </div> <div class="comment-date">2021-06-27 9:26 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Diamond kata with FsCheck https://blog.ploeh.dk/2015/01/10/diamond-kata-with-fscheck 2015-01-10T22:10:00+00:00 Mark Seemann <div id="post"> <p> <em>This post is a walk-through of doing the Diamond kata with FsCheck.</em> </p> <p> Recently, <a href="http://natpryce.com">Nat Pryce</a> tweeted: <blockquote> <a href="https://twitter.com/natpryce/status/551905182280585216">The diamond kata, TDD'd only with property-based tests. https://github.com/npryce/property-driven-diamond-kata One commit for each step: add test/make test pass/refactor</a> </blockquote> This made me curious. First, I'd never heard about the <em>Diamond kata</em>, and second, I find Property-Based Testing quite interesting these days. </p> <p> Digging a bit lead me to <a href="http://claysnow.co.uk/recycling-tests-in-tdd">a blog post</a> by <a href="http://claysnow.co.uk/author/sebrose">Seb Rose</a>; the Diamond kata is extremely easy to explain: <blockquote> <p> Given a letter, print a diamond starting with ‘A’ with the supplied letter at the widest point. </p> <p> For example: print-diamond ‘C’ prints </p> <pre> A B B C C B B A </pre> </blockquote> After having thought about it a little, I couldn't even begin to see how one could approach this problem using Property-Based Testing. It struck me as a problem inherently suited for Example-Driven Development, so I decided to do that first. </p> <h3 id="e911864d51774d7f835f955dc199b18d"> Example-Driven Development <a href="#e911864d51774d7f835f955dc199b18d" title="permalink">#</a> </h3> <p> The no-brain approach to Example-Driven Development is to start with 'A', then 'B', and so on. Exactly as Seb Rose predicts, when you approach the problem like this, when you reach 'C', it no longer seems reasonable to hard-code the responses, but then the entire complexity of the problem hits you all at once. It's quite hard to do incremental development by going through the 'A', 'B', 'C' progression. </p> <p> This annoyed me, but I was curious about the implementation, so I spent an hours or so toying with making the 'C' case pass. After this, on the other hand, I had an implementation that works for all letters A-Z. </p> <h3 id="77a5caf1aeaf4f878dfc0585d4346366"> Property-Driven Development <a href="#77a5caf1aeaf4f878dfc0585d4346366" title="permalink">#</a> </h3> <p> On my commute it subsequently struck me that solving the Diamond kata with Example-Driven Development taught me a lot about the problem itsef, and I could easily come up with the first 10 properties about it. </p> <p> Therefore, I decided to give the kata another try, this time with <a href="https://github.com/fsharp/FsCheck">FsCheck</a>. I also wanted to see if it would be possible to make the development more incremental; while I didn't follow the <a href="http://blog.8thlight.com/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html">Transformation Priority Premise</a> (TPP) to the letter, I was inspired by it. My third 'rule' was to use <a href="https://blog.ploeh.dk/outside-in-tdd">Devil's Advocate</a> to force me to write properties that completely describe the problem. </p> <h3 id="4c98113dd01641569ae918e2b094f42f"> Ice Breaker <a href="#4c98113dd01641569ae918e2b094f42f" title="permalink">#</a> </h3> <p> To get started, it can be a good idea to write a simple (ice breaker) test, because there's always a little work involved in getting everything up and running. To meet that goal, I wrote this almost useless property: </p> <p> <pre>[&lt;<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Diamond&nbsp;is&nbsp;non-empty``&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;not&nbsp;(<span style="color:#2b91af;">String</span>.IsNullOrWhiteSpace&nbsp;actual)</pre> </p> <p> It only states that the string returned from the Diamond.make function isn't an empty string. Using Devil's Advocate, I created this implementation: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;make&nbsp;letter&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Devil&#39;s&nbsp;advocate.&quot;</span> </pre> </p> <p> This hard-coded result satisfies the single property. However, the only reason it works is because it ignores the input. </p> <h3 id="94837c0fd8a046b2b62fa290efe7d968"> Constraining the input <a href="#94837c0fd8a046b2b62fa290efe7d968" title="permalink">#</a> </h3> <p> The kata only states what should happen for the inputs A-Z, but as currently written, FsCheck will serve all sorts of char values, including white space and funny characters like '&lt;', ']', '?', etc. While I could write a run-time check in the make function, and return <code>None</code> upon invalid input, I am, after all, only doing a kata, so I'd rather want to tell FsCheck to give me only the letters A-Z. Here's one way to do that: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">Letters</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">member</span>&nbsp;Char()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Arb</span>.<span style="color:#2b91af;">Default</span>.Char() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Arb</span>.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;&lt;=&nbsp;c&nbsp;&amp;&amp;&nbsp;c&nbsp;&lt;=&nbsp;<span style="color:#a31515;">&#39;Z&#39;</span>) <span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">DiamondPropertyAttribute</span>()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:#2b91af;">PropertyAttribute</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arbitrary&nbsp;=&nbsp;[|&nbsp;typeof&lt;<span style="color:#2b91af;">Letters</span>&gt;&nbsp;|], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>) [&lt;<span style="color:#2b91af;">DiamondProperty</span>&gt;] <span style="color:blue;">let</span>&nbsp;``Diamond&nbsp;is&nbsp;non-empty``&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;not&nbsp;(<span style="color:#2b91af;">String</span>.IsNullOrWhiteSpace&nbsp;actual)</pre> </p> <p> The Letters type redefines how char values are generated, using the default generator of char, but then filtering the values so that they only fall in the range [A-Z]. </p> <p> To save myself from a bit of typing, I also defined the custom DiamondPropertyAttribute that uses the Letters type, and used it to adorn the test function instead of FsCheck's built-in PropertyAttribute. </p> <h3 id="367c2ba1ac02450b84ca1d2d2408819d"> Top and bottom <a href="#367c2ba1ac02450b84ca1d2d2408819d" title="permalink">#</a> </h3> <p> Considering the TPP, I wondered which property I should write next, since I wanted to define a property that would force me to change my current implementation in the right direction, but only by a small step. </p> <p> A good candidate seemed to state something about the top and bottom of the diamond: the first and the last line of the diamond must always contain a single 'A'. Here's how I expressed that in code: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;split&nbsp;(x&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;x.Split([|&nbsp;<span style="color:#2b91af;">Environment</span>.NewLine&nbsp;|],&nbsp;<span style="color:#2b91af;">StringSplitOptions</span>.None) <span style="color:blue;">let</span>&nbsp;trim&nbsp;(x&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>)&nbsp;=&nbsp;x.Trim() [&lt;<span style="color:#2b91af;">DiamondProperty</span>&gt;] <span style="color:blue;">let</span>&nbsp;``First&nbsp;row&nbsp;contains&nbsp;A``&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;rows&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.head&nbsp;|&gt;&nbsp;trim&nbsp;=&nbsp;<span style="color:#a31515;">&quot;A&quot;</span> [&lt;<span style="color:#2b91af;">DiamondProperty</span>&gt;] <span style="color:blue;">let</span>&nbsp;``Last&nbsp;row&nbsp;contains&nbsp;A``&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;rows&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.last&nbsp;|&gt;&nbsp;trim&nbsp;=&nbsp;<span style="color:#a31515;">&quot;A&quot;</span></pre> </p> <p> Notice that I wrote the test-specific helper functions <code>split</code> and <code>trim</code> in order to make the code a bit more readable, and that I also decided to define the property for the top of the diamond separately from the property for the bottom. </p> <p> In the degenerate case where the input is 'A', the first and the last rows are identical, but the properties still hold. </p> <p> Using the Devil's Advocate, this implementation passes all properties defined so far: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;make&nbsp;letter&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;</span> </pre> </p> <p> This is slightly better, but I purposely placed the 'A' slightly off-centre. In fact, the entire hard-coded string is 16 characters wide, so it can never have a single, centred letter. The next property should address this problem. </p> <h3 id="2acffba1f9f246a1a722025d1592a89e"> Vertical symmetry <a href="#2acffba1f9f246a1a722025d1592a89e" title="permalink">#</a> </h3> <p> A fairly important property of the diamond is that it must be symmetric. Here's how I defined symmetry over the vertical axis: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;leadingSpaces&nbsp;(x&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;indexOfNonSpace&nbsp;=&nbsp;x.IndexOfAny&nbsp;[|&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;<span style="color:#a31515;">&#39;Z&#39;</span>&nbsp;|] &nbsp;&nbsp;&nbsp;&nbsp;x.Substring(0,&nbsp;indexOfNonSpace) <span style="color:blue;">let</span>&nbsp;trailingSpaces&nbsp;(x&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;lastIndexOfNonSpace&nbsp;=&nbsp;x.LastIndexOfAny&nbsp;[|&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;<span style="color:#a31515;">&#39;Z&#39;</span>&nbsp;|] &nbsp;&nbsp;&nbsp;&nbsp;x.Substring(lastIndexOfNonSpace&nbsp;+&nbsp;1) [&lt;<span style="color:#2b91af;">DiamondProperty</span>&gt;] <span style="color:blue;">let</span>&nbsp;``All&nbsp;rows&nbsp;must&nbsp;have&nbsp;a&nbsp;symmetric&nbsp;contour``&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;rows&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Array</span>.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;r&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(leadingSpaces&nbsp;r)&nbsp;=&nbsp;(trailingSpaces&nbsp;r)) </pre> </p> <p> Using the two new helper functions, this property states that the diamond should have a symmetric contour; that is, that it's <em>external</em> shape should be symmetric. The property doesn't define what's inside of the diamond. </p> <p> Again, using the Devil's Advocate technique, this implementations passes all tests: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;make&nbsp;letter&nbsp;=&nbsp;<span style="color:#a31515;">&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;</span> </pre> </p> <p> At least the string is now symmetric, but it feels like we aren't getting anywhere, so it's time to define a property that will force me to <em>use</em> the input letter. </p> <h3 id="77c8f03bb3c748f89e401cd1895f488a"> Letters, in correct order <a href="#77c8f03bb3c748f89e401cd1895f488a" title="permalink">#</a> </h3> <p> When considering the shape of the required diamond, we know that the first line should contain an 'A', the next line should contain a 'B', the third line a 'C', and so on, until the input letter is reached, after which the order is reversed. Here's my way of stating that: </p> <p> <pre>[&lt;<span style="color:#2b91af;">DiamondProperty</span>&gt;] <span style="color:blue;">let</span>&nbsp;``Rows&nbsp;must&nbsp;contain&nbsp;the&nbsp;correct&nbsp;letters,&nbsp;in&nbsp;the&nbsp;correct&nbsp;order`` &nbsp;&nbsp;&nbsp;&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;letters&nbsp;=&nbsp;[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;letter] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expectedLetters&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;letters&nbsp;@&nbsp;(letters&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.tail)&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.toArray &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;expectedLetters&nbsp;=&nbsp;(rows&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Array</span>.map&nbsp;trim&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Array</span>.map&nbsp;<span style="color:#2b91af;">Seq</span>.head)</pre> </p> <p> The expression <code>let&nbsp;letters&nbsp;=&nbsp;[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;letter]</code> produces a list of letters up to, and including, the input letter. As an example, if <code>letter</code> is 'D', then <code>letters</code> will be <code>['A';&nbsp;'B';&nbsp;'C';&nbsp;'D']</code>. That's only the top and middle parts of the diamond, but we can use <code>letters</code> again: we just have to reverse it (<code>['D';&nbsp;'C';&nbsp;'B';&nbsp;'A']</code>) and throw away the first element ('D') in order to remove the duplicate in the middle. </p> <p> This property is still quite loosely stated, because it only states that each row's first non-white space character should be the expected letter, but it doesn't say anything about subsequent letters. The reason I defined this property so loosely was that I didn't want to force too many changes on the implementation at once. The simplest implementation I could think of was this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;make&nbsp;letter&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;letters&nbsp;=&nbsp;[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;letter] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;letters&nbsp;=&nbsp;letters&nbsp;@&nbsp;(letters&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.tail) &nbsp;&nbsp;&nbsp;&nbsp;letters &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;string&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.reduce&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%s%s</span><span style="color:#a31515;">&quot;</span>&nbsp;x&nbsp;System.<span style="color:#2b91af;">Environment</span>.NewLine&nbsp;y)</pre> </p> <p> It duplicates the test code a bit, because it reuse the algorithm that generates the desired sequence of letters. However, <a href="/2014/08/07/why-dry">I'm not too concerned about the occasional DRY violation</a>. </p> <p> For the input 'D', this implementation produces this output: </p> <p> <pre>A B C D C B A</pre> </p> <p> All properties still hold. Obviously this isn't correct yet, but I was happy that I was able to define a property that led me down a path where I could take a small, controlled step towards a more correct solution. </p> <h3 id="0f1b3aaf4f1b4b49b01622c15c7ad00e"> As wide as it's high <a href="#0f1b3aaf4f1b4b49b01622c15c7ad00e" title="permalink">#</a> </h3> <p> While I already have various properties that examine the white space around the letters, I've now temporarily arrived at an implementation entirely without white space. This made me consider how I could take advantage of those, and combine them with a new property, to re-introduce the second dimension to the figure. </p> <p> It's fairly clear that the figure must be as wide as it's high, if we count both width and height in number of letters. This property is easy to define: </p> <p> <pre>[&lt;DiamondProperty&gt;] <span style="color:blue;">let</span>&nbsp;``Diamond&nbsp;is&nbsp;as&nbsp;wide&nbsp;as&nbsp;it&#39;s&nbsp;high``&nbsp;(letter&nbsp;:&nbsp;char)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;Diamond.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;rows.Length &nbsp;&nbsp;&nbsp;&nbsp;rows&nbsp;|&gt;&nbsp;Array.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Length&nbsp;=&nbsp;expected)</pre> </p> <p> It simply verifies that each row has exactly the same number of letters as there are rows in the figure. My implementation then became this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;make&nbsp;letter&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;makeLine&nbsp;width&nbsp;letter&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;letter&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;padding&nbsp;=&nbsp;String(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;(width&nbsp;-&nbsp;1)&nbsp;/&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%c%s</span><span style="color:#a31515;">&quot;</span>&nbsp;padding&nbsp;letter&nbsp;padding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;String(letter,&nbsp;width) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;letters&nbsp;=&nbsp;[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;letter] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;letters&nbsp;=&nbsp;letters&nbsp;@&nbsp;(letters&nbsp;|&gt;&nbsp;List.rev&nbsp;|&gt;&nbsp;List.tail) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;width&nbsp;=&nbsp;letters.Length &nbsp;&nbsp;&nbsp;&nbsp;letters &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.map&nbsp;(makeLine&nbsp;width) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;List.reduce&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%s%s</span><span style="color:#a31515;">&quot;</span>&nbsp;x&nbsp;Environment.NewLine&nbsp;y)</pre> </p> <p> This prompted me to introduce a private makeLine function, which produces the line for a single letter. It has a special case to handle the 'A', since this value is the only value where there's only a single letter on a line. For all other letters, there will be two letters - eventually with spaces between them. </p> <p> This seemed a reasonable rationale for introducing a branch in the code, but after having completed the kata, I can see that <a href="http://natpryce.com/articles/000807.html">Nat Pryce has a more elegant solution</a>. </p> <p> If the input is 'D' the output now looks like this: </p> <p> <pre> A BBBBBBB CCCCCCC DDDDDDD CCCCCCC BBBBBBB A </pre> </p> <p> There's still not much white space in the implementation, but at least we regained the second dimension of the figure. </p> <h3 id="3f6cc532b1a14515842adf9742bed959"> Inner space <a href="#3f6cc532b1a14515842adf9742bed959" title="permalink">#</a> </h3> <p> The next incremental change I wanted to introduce was the space between two letters. It seemed reasonable that this would be a small step for the makeLine function. </p> <p> <pre>[&lt;<span style="color:#2b91af;">DiamondProperty</span>&gt;] <span style="color:blue;">let</span>&nbsp;``All&nbsp;rows&nbsp;except&nbsp;top&nbsp;and&nbsp;bottom&nbsp;have&nbsp;two&nbsp;identical&nbsp;letters`` &nbsp;&nbsp;&nbsp;&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;isTwoIdenticalLetters&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasIdenticalLetters&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.distinct&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.length&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;hasTwoLetters&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.length&nbsp;=&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hasIdenticalLetters&nbsp;&amp;&amp;&nbsp;hasTwoLetters &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;rows &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Array</span>.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;not&nbsp;(x.Contains(<span style="color:#a31515;">&quot;A&quot;</span>))) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Array</span>.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Replace(<span style="color:#a31515;">&quot;&nbsp;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Array</span>.forall&nbsp;isTwoIdenticalLetters</pre> </p> <p> The property itself simply states that each row must consist of exactly two identical letters, and then white space to fill out the shape. The way to verify this is to first replace all spaces with the empty string, and then examine the remaining string. Each remaining string must contain exactly two letters, so its length must be 2, and if you perform a <code>distinct</code> operation on its constituent char values, the resulting sequence of chars should have a length of 1. </p> <p> This property only applies to the 'internal' rows, but not the top and bottom rows that contain a single 'A', so these rows are filtered out. </p> <p> The new property itself only states that apart from the 'A' rows, each row must have exactly two identical letters. Because the tests for the 'A' rows, together with the tests for symmetric contours, already imply that each row must have a width of an uneven number, and again because of the symmetric contour requirement, I had to introduce <em>at least</em> a single space between the two characters. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;make&nbsp;letter&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;makeLine&nbsp;width&nbsp;letter&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;letter&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;padding&nbsp;=&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;(width&nbsp;-&nbsp;1)&nbsp;/&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%c%s</span><span style="color:#a31515;">&quot;</span>&nbsp;padding&nbsp;letter&nbsp;padding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;innerSpace&nbsp;=&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;width&nbsp;-&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%c%s%c</span><span style="color:#a31515;">&quot;</span>&nbsp;letter&nbsp;innerSpace&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;letters&nbsp;=&nbsp;[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;letter] &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;letters&nbsp;=&nbsp;letters&nbsp;@&nbsp;(letters&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.tail) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;width&nbsp;=&nbsp;letters.Length &nbsp;&nbsp;&nbsp;&nbsp;letters &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;(makeLine&nbsp;width) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.reduce&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%s%s</span><span style="color:#a31515;">&quot;</span>&nbsp;x&nbsp;<span style="color:#2b91af;">Environment</span>.NewLine&nbsp;y)</pre> </p> <p> Using the Devil's Advocate technique, it seems that the simplest way of passing all tests is to fill out the inner space completely. Here's an example of calling Diamond.make 'D' with the current implementation: </p> <p> <pre> A B B C C D D C C B B A </pre> </p> <p> Again, I like how this new property enabled me to do an incremental change to the implementation. Visually, we can see that the figure looks 'more correct' than it previously did. </p> <h3 id="a12dfe230d3443e3bc611beb5b6224f0"> Bottom triangle <a href="#a12dfe230d3443e3bc611beb5b6224f0" title="permalink">#</a> </h3> <p> At this point I thought that it was appropriate to begin to address the diamond shape of the figure. After having spent some time considering how to express that without repeating the implementation code, I decided that the easiest step would be to verify that the lower left space forms a triangle. </p> <p> <pre>[&lt;<span style="color:#2b91af;">DiamondProperty</span>&gt;] <span style="color:blue;">let</span>&nbsp;``Lower&nbsp;left&nbsp;space&nbsp;is&nbsp;a&nbsp;triangle``&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;lowerLeftSpace&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rows &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.skipWhile&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;not&nbsp;(x.Contains(string&nbsp;letter))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.map&nbsp;leadingSpaces &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;spaceCounts&nbsp;=&nbsp;lowerLeftSpace&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Length) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:#2b91af;">Seq</span>.initInfinite&nbsp;id &nbsp;&nbsp;&nbsp;&nbsp;spaceCounts &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.zip&nbsp;expected &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.forall&nbsp;(<span style="color:blue;">fun</span>&nbsp;(x,&nbsp;y)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x&nbsp;=&nbsp;y)</pre> </p> <p> This one is a bit tricky. It examines the shape of the lower left white space. Getting that shape itself is easy enough, using the previously defined leadingSpaces helper function. For each row, <code>spaceCounts</code> contains the number of leading spaces. </p> <p> The <code>expected</code> value contains an infinite sequence of numbers, {0; 1; 2; 3; 4; ...} because, due to the random nature of Property-Based Testing, I don't know exactly how many numbers to expect. </p> <p> Zipping an infinite sequence with a finite sequence matches elements in each sequence, until the shortest sequence (that would be the finite sequence) ends. Each resulting element is a tuple, and if the lower left space forms a triangle, the sequence of tuples should look like this: {(0, 0); (1, 1); (2, 2); ...}. The final step in the property is therefore to verify that all of those tuples have identical elements. </p> <p> The implementation uses Devil's Advocate, and goes quite a bit out of its way to make the top of the figure wrong. As you'll see shortly, it will actually be a simpler implementation to keep the figure symmetric around the horizontal axis as well, but we should have that as an explicit property. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;make&nbsp;letter&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;makeLine&nbsp;width&nbsp;(letter,&nbsp;letterIndex)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;letter&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;padding&nbsp;=&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;(width&nbsp;-&nbsp;1)&nbsp;/&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%c%s</span><span style="color:#a31515;">&quot;</span>&nbsp;padding&nbsp;letter&nbsp;padding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;innerSpaceWidth&nbsp;=&nbsp;letterIndex&nbsp;*&nbsp;2&nbsp;-&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;padding&nbsp;=&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;(width&nbsp;-&nbsp;2&nbsp;-&nbsp;innerSpaceWidth)&nbsp;/&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;innerSpace&nbsp;=&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;innerSpaceWidth) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%c%s%c%s</span><span style="color:#a31515;">&quot;</span>&nbsp;padding&nbsp;letter&nbsp;innerSpace&nbsp;letter&nbsp;padding &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;indexedLetters&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;letter]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.mapi&nbsp;(<span style="color:blue;">fun</span>&nbsp;i&nbsp;l&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;l,&nbsp;i)&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;indexedLetters&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;&nbsp;&nbsp;indexedLetters &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;(l,&nbsp;_)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;l,&nbsp;1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.tail &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@&nbsp;(indexedLetters&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;width&nbsp;=&nbsp;indexedLetters.Length &nbsp;&nbsp;&nbsp;&nbsp;indexedLetters &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;(makeLine&nbsp;width) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.reduce&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%s%s</span><span style="color:#a31515;">&quot;</span>&nbsp;x&nbsp;<span style="color:#2b91af;">Environment</span>.NewLine&nbsp;y)</pre> </p> <p> The main change here is that now each letter is being indexed, but then I deliberately throw away the indexes for the top part, in order to force myself to add yet another property later. While I could have skipped this step, and gone straight for the correct solution at this point, I was, after all, doing a kata, so I also wanted to write one last property. </p> <p> The current implementation produces the figure below when Diamond.make is called with 'D': </p> <p> <pre> A B B C C D D C C B B A </pre> </p> <p> The shape is almost there, but obviously, the top is wrong, because I deliberately made it so. </p> <h3 id="e5ca655a760d4380bf061851ee733471"> Horizontal symmetry <a href="#e5ca655a760d4380bf061851ee733471" title="permalink">#</a> </h3> <p> Just as the figure must be symmetric over its vertical axis, it must also be symmetric over its horizontal axis: </p> <p> <pre>[&lt;<span style="color:#2b91af;">DiamondProperty</span>&gt;] <span style="color:blue;">let</span>&nbsp;``Figure&nbsp;is&nbsp;symmetric&nbsp;around&nbsp;the&nbsp;horizontal&nbsp;axis``&nbsp;(letter&nbsp;:&nbsp;<span style="color:#2b91af;">char</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">Diamond</span>.make&nbsp;letter &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;rows&nbsp;=&nbsp;split&nbsp;actual &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;topRows&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rows &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.takeWhile&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;not&nbsp;(x.Contains(string&nbsp;letter)))&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;bottomRows&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rows &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.skipWhile&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;not&nbsp;(x.Contains(string&nbsp;letter))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.skip&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev &nbsp;&nbsp;&nbsp;&nbsp;topRows&nbsp;=&nbsp;bottomRows</pre> </p> <p> This property finally 'allows' me to simplify my implementation: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;make&nbsp;letter&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;makeLine&nbsp;width&nbsp;(letter,&nbsp;letterIndex)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;letter&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;padding&nbsp;=&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;(width&nbsp;-&nbsp;1)&nbsp;/&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%c%s</span><span style="color:#a31515;">&quot;</span>&nbsp;padding&nbsp;letter&nbsp;padding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;innerSpaceWidth&nbsp;=&nbsp;letterIndex&nbsp;*&nbsp;2&nbsp;-&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;padding&nbsp;=&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;(width&nbsp;-&nbsp;2&nbsp;-&nbsp;innerSpaceWidth)&nbsp;/&nbsp;2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;innerSpace&nbsp;=&nbsp;<span style="color:#2b91af;">String</span>(<span style="color:#a31515;">&#39;&nbsp;&#39;</span>,&nbsp;innerSpaceWidth) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%c%s%c%s</span><span style="color:#a31515;">&quot;</span>&nbsp;padding&nbsp;letter&nbsp;innerSpace&nbsp;letter&nbsp;padding &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;indexedLetters&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;..&nbsp;letter]&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.mapi&nbsp;(<span style="color:blue;">fun</span>&nbsp;i&nbsp;l&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;l,&nbsp;i)&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;indexedLetters&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indexedLetters&nbsp;@&nbsp;(indexedLetters&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.rev&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.tail) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;width&nbsp;=&nbsp;indexedLetters.Length &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;indexedLetters &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;(makeLine&nbsp;width) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.reduce&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;sprintf&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:#2b91af;">%s%s%s</span><span style="color:#a31515;">&quot;</span>&nbsp;x&nbsp;<span style="color:#2b91af;">Environment</span>.NewLine&nbsp;y)</pre> </p> <p> Calling Diamond.make with 'D' now produces: </p> <p> <pre> A B B C C D D C C B B A </pre> </p> <p> It works with other letters, too. </p> <h3 id="c525f4e19c544e679cf63d6e4f650fcf"> Summary <a href="#c525f4e19c544e679cf63d6e4f650fcf" title="permalink">#</a> </h3> <p> It turned out to be an interesting exercise to do this kata with Property-Based Testing. To me, the most surprising part was that it was much easier to approach the problem in an incremental fashion than it was with Example-Driven Development. </p> <p> If you're interested in perusing the source code, including my detailed, step-by-step commit remarks, it's <a href="https://github.com/ploeh/DiamondFsCheck">on GitHub</a>. If you want to learn more about Property-Based Testing, you can watch my <a href="https://blog.ploeh.dk/property-based-testing-intro">Introduction to Property-based Testing with F#</a> Pluralsight course. There are more examples in some of my other F# Pluralsight courses - particularly <a href="https://blog.ploeh.dk/type-driven-development-with-fsharp">Type-Driven Development with F#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Composition Root Reuse https://blog.ploeh.dk/2015/01/06/composition-root-reuse 2015-01-06T10:40:00+00:00 Mark Seemann <div id="post"> <p> <em>A Composition Root is application-specific. It makes no sense to reuse it across code bases.</em> </p> <p> At regular intervals, I get questions about how to reuse <a href="/2011/07/28/CompositionRoot">Composition Roots</a>. The short answer is: <strong>you don't</strong>. </p> <p> Since it appears to be a Frequently Asked Question, it seems that a more detailed answer is warranted. </p> <h3 id="b30d7abe83504ce3ae14eb803ee2bb0f"> Composition Roots define applications <a href="#b30d7abe83504ce3ae14eb803ee2bb0f" title="permalink">#</a> </h3> <p> A Composition Root is application-specific; it's what <em>defines</em> a single application. After having written nice, decoupled code throughout your code base, the Composition Root is where you finally <em>couple</em> everything, from data access to (user) interfaces. </p> <p> Here's a simplified example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CompositionRoot</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpController</span>&nbsp;Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpControllerDescriptor</span>&nbsp;controllerDescriptor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;controllerType) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>(controllerType&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ReservationsController</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ctx&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsContext</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;repository&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlReservationRepository</span>(ctx); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.RegisterForDispose(ctx); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.RegisterForDispose(repository); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ApiValidator</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repository, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(10), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReservationMapper</span>()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Handle&nbsp;more&nbsp;controller&nbsp;types&nbsp;here...</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Unknown&nbsp;Controller&nbsp;type:&nbsp;&quot;</span>&nbsp;+&nbsp;controllerType, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;controllerType&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is an excerpt of a Composition Root for an ASP.NET Web API service, but the hosting framework isn't particularly important. What's important is that this piece of code pulls in objects from all over an application's code base in order to compose the application. </p> <p> The ReservationsContext class derives from <a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.aspx">DbContext</a>; an instance of it is injected into a SqlReservationRepository object. From this, it's clear that this application uses SQL Server for it data persistence. This decision is hard-coded into the Composition Root. You can't change this unless you recompile the Composition Root. </p> <p> At another boundary of this <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">hexagonal/onion/whatever architecture</a> is a ReservationsController, which derives from <a href="http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.aspx">ApiController</a>. From this, it's clear that this application is a REST API that uses ASP.NET Web API for its implementation. This decision, too, is hard-coded into the Composition Root. </p> <p> From this, it should be clear that the Composition Root is what <em>defines</em> the application. It combines all the loosely coupled components into an application: software that can be executed in an OS process. </p> <p> It makes no more sense to attempt to reuse a Composition Root than it does attempting to 'reuse' an application. </p> <h3 id="33df8d334be8440d90ca524357b79ac4"> Container-based Composition Roots <a href="#33df8d334be8440d90ca524357b79ac4" title="permalink">#</a> </h3> <p> The above example uses <a href="/2014/06/10/pure-di">Pure DI</a> in order to explain how a Composition Root is equivalent to an application definition. What about DI Container-based Composition Roots, then? </p> <p> It's essentially the same story, but with a twist. Imagine that you want to use Castle Windsor to compose your Web API; I've <a href="/2012/10/03/DependencyInjectioninASP.NETWebAPIwithCastleWindsor">already previously explained how to do that</a>, but here's the IHttpControllerActivator implementation repeated: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">WindsorCompositionRoot</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IWindsorContainer</span>&nbsp;container; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;WindsorCompositionRoot(<span style="color:#2b91af;">IWindsorContainer</span>&nbsp;container) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.container&nbsp;=&nbsp;container; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpController</span>&nbsp;Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpControllerDescriptor</span>&nbsp;controllerDescriptor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;controllerType) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;controller&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:#2b91af;">IHttpController</span>)<span style="color:blue;">this</span>.container.Resolve(controllerType); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.RegisterForDispose( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Release</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:blue;">this</span>.container.Release(controller))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;controller; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Release</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IDisposable</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Action</span>&nbsp;release; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Release(<span style="color:#2b91af;">Action</span>&nbsp;release) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.release&nbsp;=&nbsp;release; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Dispose() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.release(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Indeed, there's no application-specific code there; this class is completely reusable. However, this reusability is accomplished because the container is injected into the object. The container itself must be configured in order to work, and configuration code is just as application-specific as the above Pure DI example. </p> <p> Thus, with a DI Container, you may be able to decouple and reuse the part of the Composition Root that performs the <em>run-time</em> composition. However, the <em>design-time</em> composition is where you put the configuration code that select which concrete types should be used in which cases. This is the part that specifies the application, and that's not reusable. </p> <h3 id="bf27392c85cf4fb78a2beadd3aa7b7a4"> Summary <a href="#bf27392c85cf4fb78a2beadd3aa7b7a4" title="permalink">#</a> </h3> <p> Composition Roots aren't reusable. </p> <p> Sometimes, you might be building a suite of applications that share a substantial subset of dependencies. Imagine, for example, that you're building a REST API and a batch job that together solve a business problem. The Domain Model and the Data Access Layer may be shared between these two applications, but the boundary isn't. The batch job isn't going to need any Controllers, and the REST API isn't going to need whatever is going to kick off the batch job (e.g. a scheduler). These two applications may have a lot in common, but they're still <em>different</em>. Thus, they can't share a Composition Root. </p> <p> What about the DRY principle, then? </p> <p> Different applications have differing Composition Roots. Even if they start with substantial amounts of duplicated Composition Root code, in my experience, the 'common' code tends to diverge the more the applications are allowed to evolve. That duplication <a href="http://verraes.net/2014/08/dry-is-about-knowledge">may be accidental</a>, and <a href="http://programmer.97things.oreilly.com/wiki/index.php/Beware_the_Share">attempting to eliminate it may increase coupling</a>. It's better to <a href="/2014/08/07/why-dry">understand why you want to avoid duplication in the first place</a>. </p> <p> When it comes to Composition Roots, duplication tends to be accidental. Composition Roots aren't reusable. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="8b8dc78270e04675857065f6175c8662"> <div class="comment-author"><a href="http://www.kenneth-truyers.net">Kenneth Truyers</a> <a href="#8b8dc78270e04675857065f6175c8662">#</a></div> <div class="comment-content"> I agree that composition roots should not be reused in general. However, there are exceptions to the rule. <br> In the example where the domain layer and the data access layer are shared between applications, it really depends whether they will have a future need to be wired differently according to the application. <br> If that's not the case, you could argue that there's no need for two components either. <br> One solution would be to use a Facade which internally wires them up and exposes it as a single interface. This allows for reuse of the domain and the DAL as a single component, opaque to the client. </div> <div class="comment-date">2015-01-06 13:06 UTC</div> </div> <div class="comment" id="bc6f9fcf731343ecba4190d4e2888b78"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bc6f9fcf731343ecba4190d4e2888b78">#</a></div> <div class="comment-content"> <p> Kenneth, thank you for writing. If you create a Facade that internally wires up a Domain Model and DAL, you've created a tightly coupled bundle. What if you later discover that you need to <a href="/2010/09/20/InstrumentationwithDecoratorsandInterceptors">instrument your DAL</a>? You can't do that if you prematurely compose object graphs. </p> <p> Clearly, there are disadvantages to doing such a thing. What are the advantages? </p> </div> <div class="comment-date">2015-01-06 13:56 UTC</div> </div> <div class="comment" id="7cf5a06478694042a09ad56ec15c21fd"> <div class="comment-author">Kenny Pflug <a href="#7cf5a06478694042a09ad56ec15c21fd">#</a></div> <div class="comment-content"> <p>Dear Mark,</p> <p>Thank you for another excellent post.</p> <p>Some of the composition roots I wrote became really large, too, especially the more complex the application got. In these cases I often introduced factories and builders that created parts of the final object graph and the imperative statements in the composition root just wired these subgraphs together. And this leads me to my actual question:</p> <p>What do you think about pushing these factories and builders to a library so that they can be reused by different composition roots? This would result in much less code that wires the object graph. I know that <a href="/2014/12/24/placement-of-abstract-factories">abstract factories belong to the client</a>, but one could provide Simple Factories / Factory Methods or, even better I think, the Builder Pattern is predestined for these situations, because it provides default dependencies that can be exchanged before the <em>Build</em> method is called.</p> <p>If I think about automated tests, I can see that the concept I just described is already in use: every test has an arrange phase which in my opinion is the composition root for the test. To share arrange code between tests, one can use e.g. builders or fixture objects (or of course AutoFixture ;-) ). This reduces the code needed for the arrange phase and improves readability.</p> <p>What is your opinion on pushing Simple Factories / Builders to a reusable library?</p> <p>With regards,<br>Kenny</p> </div> <div class="comment-date">2015-01-06 14:26 UTC</div> </div> <div class="comment" id="c447b3ada521484f9935481c7a6b22fc"> <div class="comment-author">Daniel Sklenitzka <a href="#c447b3ada521484f9935481c7a6b22fc">#</a></div> <div class="comment-content"> While I also agree with Mark in general, I think that Kenneth has a point and that there are some exceptions. For example, we have a several projects that consist of a WCF Service and some client (WPF or Windows Store apps).<br> In the production environment, the service is deployed as a Windows Service, but for easier testing and development, we also have a console application that hosts the same service. Both entry points are basically the same composition root and should use the same components. We want to share the composition root, because otherwise when a component needs a new dependency and a constructor parameter is added, we'd have to change the same code in two different places. </div> <div class="comment-date">2015-01-06 15:05 UTC</div> </div> <div class="comment" id="cae2dcf15a6a4874814f633ce741107b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cae2dcf15a6a4874814f633ce741107b">#</a></div> <div class="comment-content"> <p> Kenny, Daniel, thank you for writing. In an attempt to answer your questions, I wrote a completely <a href="/2015/01/21/library-bundle-facade">new blog post on the subject</a>; I hope it answers some of your questions, although I suppose you aren't going to like the answer. </p> <p> If you truly have a big Composition Root, it may be one of the cases where you should <a href="/2012/11/06/WhentouseaDIContainer">consider adopting a DI Container with a convention-based configuration</a>. </p> <p> Another option is to make your 'boundary library' (e.g. your UI, or your RESTful endpoints) a library in itself, and host that library in various contexts. The Composition Root resides in that boundary library, so various hosts can 'reuse' it, as they reuse the entire application. You can see an example of this in my <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a> Pluralsight course. </p> </div> <div class="comment-date">2015-01-21 19:39 UTC</div> </div> <div class="comment" id="28c25cf9762d4a3db0ef7c33c0b11485"> <div class="comment-author">Daniel Sklenitzka <a href="#28c25cf9762d4a3db0ef7c33c0b11485">#</a></div> <div class="comment-content"> <p> Dear Mark, thank you for writing the other blog post. Now I finally have time to answer.<br> I think in our specific situation there is a subtle but important difference: Rather than reusing a composition root between two libraries, we essentially have two different ways of hosting <strong>the same</strong> library (and thus composition root). One is used primarily during development, while the other is used in production, but essentially they are (and always will be) the same thing. So in my opinion it would even be dangerous to not use the same composition root here, as it would just add a source of errors if you change the dependency graph setup in one project but forget to do so in the other.<br> But I guess this is what you meant with creating a boundary library, isn't it? </p> </div> <div class="comment-date">2015-02-01 20:30 UTC</div> </div> <div class="comment" id="c03c84c38d844f4eaed1ec9b9f845cb1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c03c84c38d844f4eaed1ec9b9f845cb1">#</a></div> <div class="comment-content"> <p> Daniel, if I understand you correctly, that's indeed the same technique that I demonstrate in my <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a> Pluralsight course. Although it may only be me splitting hairs, I don't consider that as reusing the Composition Root as much as it's deploying the same application to two different hosts; one of the hosts just happens to be a test harnes :) </p> </div> <div class="comment-date">2015-02-01 21:24 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Placement of Abstract Factories https://blog.ploeh.dk/2014/12/24/placement-of-abstract-factories 2014-12-24T10:18:00+00:00 Mark Seemann <div id="post"> <p> <em>Where should you define an Abstract Factory? Where should you implement it? Not where you'd think.</em> </p> <p> An <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a> is <a href="http://stackoverflow.com/a/2280289/126014">one of the workhorses of Dependency Injection</a>, although it's a somewhat blunt instrument. As I've described in chapter 6 of <a href="http://amzn.to/12p90MG">my book</a>, whenever you need to map a run-time value to an abstraction, you can use an Abstract Factory. However, often there are <a href="/2013/01/11/PartialTypeNameRoleHint">more sophisticated options available</a>. </p> <p> Still, it can be a useful pattern, but you have to understand where to define it, and where to implement it. One of my readers ask: <blockquote> <p> <a href="https://twitter.com/rahulpnath/status/547603091919368192">"how would deal with abstract factories if a module needs to be reused across multiple hosts/agg roots?Should each root have its fact?</a> </p> <p> <a href="https://twitter.com/rahulpnath/status/547603789918646272">"this link /2012/03/15/ImplementinganAbstractFactory/ talks about having the container injected only if the factory belongs in the same composition root"</a> </p> </blockquote> These are good question that deserve a more thorough treatment than a tweet in reply. </p> <h3 id="0c040a26eebb4371bbb1eaa2a5fda0c5"> Situation <a href="#0c040a26eebb4371bbb1eaa2a5fda0c5" title="permalink">#</a> </h3> <p> Based on the above questions, I imagine that the situation can be depicted like this: </p> <p> <img src="/content/binary/two-applications-sharing-a-library.png" alt="Two applications sharing a library."> </p> <p> Two or more applications share a common library. These applications may be a web service and a batch job, a web site and a mobile app, or any other combination that makes sense to you. </p> <h3 id="8e56dfcd22224a3fbe5b520daa7b8ad8"> Defining the Abstract Factory <a href="#8e56dfcd22224a3fbe5b520daa7b8ad8" title="permalink">#</a> </h3> <p> Where should an Abstract Factory be defined? In order to answer this question, first you must understand <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">what an Abstract Factory <em>is</em></a>. Essentially, it's an interface (or Abstract Base Class - it's not important) that looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;Create(<span style="color:blue;">object</span>&nbsp;context); }</pre> </p> <p> Sometimes, the Abstract Factory is a non-generic interface; sometimes it takes more than a single parameter; often, the parameter(s) have stronger types than <code>object</code>. </p> <p> Where do interfaces go? </p> <p> From <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices</a>, chapter 11, we know that the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> means that "clients [...] own the abstract interfaces". This makes sense, if you think about it. </p> <p> Imagine that the (shared) library defines a concrete class Foo that takes three values in its constructor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;Foo(<span style="color:#2b91af;">Guid</span>&nbsp;bar,&nbsp;<span style="color:blue;">int</span>&nbsp;baz,&nbsp;<span style="color:blue;">string</span>&nbsp;qux)</pre> </p> <p> Why would the library ever need to define an interface like the following? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFooFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Create(<span style="color:#2b91af;">Guid</span>&nbsp;bar,&nbsp;<span style="color:blue;">int</span>&nbsp;baz,&nbsp;<span style="color:blue;">string</span>&nbsp;qux); }</pre> </p> <p> It could, but what would be the purpose? The library itself doesn't <em>need</em> the interface, because the Foo class has a nice constructor it can use. </p> <p> Often, the Abstract Factory pattern is most useful if you have <em>some</em> of the values available at composition time, but can't fully compose the object because you're waiting for one of the values to materialize at run-time. If you think this sounds abstract, my article series on <a href="/2013/01/07/RoleHints">Role Hints</a> contains some realistic examples - particularly the articles <a href="/2013/01/09/MetadataRoleHint">Metadata Role Hint</a>, <a href="/2013/01/10/RoleInterfaceRoleHint">Role Interface Role Hint</a>, and <a href="/2013/01/11/PartialTypeNameRoleHint">Partial Type Name Role Hint</a>. </p> <p> A client may, for example, wait for the <code>bar</code> value before it can fully compose an instance of Foo. Thus, the <em>client</em> can define an interface like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFooFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Create(<span style="color:#2b91af;">Guid</span>&nbsp;bar); }</pre> </p> <p> This makes sense to the client, but not to the library. From the library's perspective, what's so special about <code>bar</code>? Why should the library define the above Abstract Factory, but not the following? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">partial</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFooFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Foo</span>&nbsp;Create(<span style="color:blue;">int</span>&nbsp;baz); }</pre> </p> <p> Such Abstract Factories make no sense to the library; they are meaningful only to their clients. Since the client owns the interfaces, they should be defined together with their clients. A more detailed diagram illustrates these relationships: </p> <p> <img src="/content/binary/each-client-defines-its-own-abstract-factory.png" alt="Each client defines its own Abstract Factory as it needs it."> </p> <p> As you can see, although both definitions of IFooFactory depend on the shared library (since they both return instances of Foo), they are <em>two different interfaces</em>. In Client 1, apparently, the run-time value to be mapped to Foo is <code>bar</code> (a Guid), whereas in Client 2, the run-time value to be mapped to Foo is <code>baz</code> (an int). </p> <p> The bottom line is: by default, libraries shouldn't define Abstract Factories for their own concrete types. Clients should define the Abstract Factories they need (if any). </p> <h3 id="9909213bff6f49c5a7ef7ac384eaca78"> Implementing the Abstract Factory <a href="#9909213bff6f49c5a7ef7ac384eaca78" title="permalink">#</a> </h3> <p> While I've previously described <a href="/2012/03/15/ImplementinganAbstractFactory">how to implement an Abstract Factory</a>, I may then have given short shrift to the topic of <em>where</em> to put the implementation. </p> <p> Perhaps a bit surprising, this (at least partially) depends on the <em>return type</em> of the Abstract Factory's Create method. In the case of both IFooFactory definitions above, the return type of the Create methods is the concrete Foo class, defined in the shared library. This means that both Client 1 and Client 2 already depends on the shared library. In such situations, they can each implement their Abstract Factories as <a href="/2012/03/15/ImplementinganAbstractFactory/">Manually Coded Factories</a>. They don't have to do this, but at least it's an option. </p> <p> On the other hand, if the return type of an Abstract Factory is another interface defined by the client itself, it's a different story. Imagine, as an alternative scenario, that a client depends on an IPloeh interface that it has defined by itself. It may also define an Abstract Factory like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IPloehFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IPloeh</span>&nbsp;Create(<span style="color:#2b91af;">Guid</span>&nbsp;bar); }</pre> </p> <p> This has nothing to do with the library that defines the Foo class. However, another library, somewhere else, may implement an <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> of IPloeh over Foo. If this is the case, that implementing third party could also implement IPloehFactory. In such cases, the library that defines IPloeh and IPloehFactory <em>must not</em> implement either interface, because that creates the coupling it works so hard to avoid. </p> <p> The third party that ultimately implements these interfaces is often the <a href="/2011/07/28/CompositionRoot">Composition Root</a>. If this is the case, other implementation options are <a href="/2012/03/15/ImplementinganAbstractFactory">Container-based Factories or Dynamic Proxies</a>. </p> <h3 id="9a2cd3aa0bb348f49ae8aa6af3ea16ed"> Summary <a href="#9a2cd3aa0bb348f49ae8aa6af3ea16ed" title="permalink">#</a> </h3> <p> In order to answer the initial question: my default approach would be to implement the Abstract Factory multiple times, <a href="/2014/08/07/why-dry">favouring decoupling over DRY</a>. These days I prefer <a href="/2014/06/10/pure-di">Pure DI</a>, so I'd tend to go with the <a href="/2012/03/15/ImplementinganAbstractFactory/">Manually Coded Factory</a>. </p> <p> Still, this answer of mine presupposes that Abstract Factory is the correct answer to a design problem. Often, it's not. It can lead to horrible interfaces like IFooManagerFactoryStrategyFactoryFactory, so I consider Abstract Factory as a last resort. Often, the <a href="/2013/01/07/RoleHints">Metadata, Role Interface, or Partial Type Name Role Hints</a> are better options. In the degenerate case where there's no argument to the Abstract Factory's Create method, a <a href="/2014/08/24/decoraptor">Decoraptor</a> is worth considering. </p> <p> This article explains the matter in terms of relatively simple dependency graphs. In general, <a href="http://stackoverflow.com/a/9503612/126014">dependency graphs should be shallow</a>, but if you want to learn about principles for composing more complex dependency graphs, <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices</a> contains a chapter on <em>Principles of Package and Component Design</em> (chapter 28) that I recommend. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Exception messages are for programmers https://blog.ploeh.dk/2014/12/23/exception-messages-are-for-programmers 2014-12-23T08:24:00+00:00 Mark Seemann <div id="post"> <p> <em>Exception messages should be aimed at other developers, not end users.</em> </p> <p> Once in a while, I come across fellow programmers with uncertain understanding of the purpose of exception messages. In my experience, when it comes to writing exception messages, most developers approach the job with one of these mindsets: <ul> <li>Write the shortest possible exception message; if possible, ignore good grammar, punctuation, and proper spelling.</li> <li>Write lovingly crafted error messages for the end users.</li> </ul> Both approaches are wrong. </p> <h3 id="9479d0d1b0f4461ab94bc38eb0a6640b"> Damn the torpedoes <a href="#9479d0d1b0f4461ab94bc38eb0a6640b" title="permalink">#</a> </h3> <p> As far as I can tell, the most common approach to writing error messages is to get it over with as quickly as possible. Apparently, writing a good exception message is such tedious work that a developer can't possibly be bothered with it. Here are some examples: </p> <p> <pre><span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Exception</span>(<span style="color:#a31515;">&quot;Unknown&nbsp;FaileType&quot;</span>); </pre> </p> <p> or: </p> <p> <pre><span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Exception</span>(<span style="color:#a31515;">&quot;Unecpected&nbsp;workingDirectory&quot;</span>); </pre> </p> <p> These aren't particularly helpful. At the very least, is it too much to ask that they apply correct spelling and punctuation? Would it have been a horrible experience for the developer to write "Unknown FailureType.", or "Unexpected workingDirectory."? </p> <p> Even if spelling, grammar, and punctuation are corrected, these exception messages aren't particularly helpful to anyone. Fair enough that the FailureType or workingDirectory values were unknown or unexpected, but what <em>were</em> they, then? </p> <p> If you're seeing such exceptions while working with the code in your IDE, you may be able to attach a debugger and figure out what was the wrong value, but if you're looking at an error log from a production system, you'd really like to know the exact value that was unexpected. </p> <p> Unfortunately, most developers apparently can't be bothered with supplying even such rudimentary information. </p> <h3 id="9e86127520694653b364974cb85553d4"> Exception messages for end users? <a href="#9e86127520694653b364974cb85553d4" title="permalink">#</a> </h3> <p> Developers who <em>do</em> care about exception messages often seem to have end users in mind. They may spell exception messages correctly, but shy away from providing technical information in the messages, because they are considering the user experience for the end users. They would argue that the "Unknown FailureType" shouldn't be included in the exception message, because it's too technical. </p> <p> It is, indeed, too technical for end users, but the misconception here is that exception messages should be exposed to end users at all. These developers seems to have a plan for exception handling, which essentially involves catching all exceptions at the boundary of the application, and simply show the exception message to the end user. </p> <p> That's unlikely to provide a good user experience. </p> <p> It should be clear that "Unknown FailureType." isn't particularly helpful to a fellow programmer, but it's even more useless to a non-technical end user. Thus, in an effort to shield end users from the technical failure modes of our program, we make exception messages useless to end users and fellow programmers alike. </p> <h3 id="005fcb618cf04c81a62bc6ed86c5a7ad"> Security <a href="#005fcb618cf04c81a62bc6ed86c5a7ad" title="permalink">#</a> </h3> <p> Another argument I often encounter is one of security - in this case Information Disclosure. According to the argument, exception messages shouldn't include run-time values, because that may leak information about how the system works. Again, this argument seems to hinge on the implied usage of exception messages shown to end users. </p> <h3 id="a238d1fcd3b14df0af4acf563a1456e0"> Language <a href="#a238d1fcd3b14df0af4acf563a1456e0" title="permalink">#</a> </h3> <p> Exception messages aren't for end users. End users don't speak your language. They don't speak 'Technical'. They may not even speak English. What if your target demographic is all Dutch? Are you going to write all of your exception messages in Dutch? Even if you do, what about exceptions thrown by your Base Class Library or third-party libraries? These are likely to be in English, so if you catch and display all exceptions, you may occasionally display an English message to your Dutch users. </p> <p> What if your application is designed for a Swiss audience? In which language are you going to write your exception messages? German? French? Italian? Romansh? </p> <h3 id="f221aacfa1374fb9bf2dec6d64551323"> Conceptual coupling <a href="#f221aacfa1374fb9bf2dec6d64551323" title="permalink">#</a> </h3> <p> All of the above arguments even implicitly assume that you're writing exception messages for a known system. What if you're writing a reusable library? If you're writing a reusable library, you don't know the context in which it's going to be used. If you don't know the context, then how can you write an appropriate message for an end user? </p> <p> It should be clear that when writing a reusable library, you're unlikely to know the <em>language</em> of the end user, but it's worse than that: you don't know <em>anything</em> about the end user. You don't even know if there's going to be an end user at all; your library may as well be running inside a batch job. </p> <p> The converse is true as well. Even if you aren't writing a reusable library, end user-targeted exception messages <em>increases the coupling</em> in the system, because exception messages would be coupled to a particular user interface. This sort of coupling doesn't occur in the type system, but is conceptual. Exactly because it's not tied to any type system, an automated tool can't detect it, so it's much harder to notice, and more insidious as a result. </p> <p> Exception messages are not for end users. Applications can catch known exception <em>types</em> and translate them to context-aware error messages, but this is a user interface concern - not a technical concern. </p> <h3 id="7f2b46e42b914955a24c6a0082ae5dcb"> Exception messages for client developers <a href="#7f2b46e42b914955a24c6a0082ae5dcb" title="permalink">#</a> </h3> <p> Now that you understand that exception messages shouldn't be aimed at end users, who's left? Your fellow programmers, including your future self. As I explain in <a href="https://blog.ploeh.dk/encapsulation-and-solid">my encapsulation course</a>, exception messages can be used as documentation of the system. </p> <p> Imagine that you're using a new library that you don't yet know well. Which exception message would you, as a programmer, prefer to encounter? <blockquote> "Unecpected workingDirectory" </blockquote> or this: <blockquote> <p> "You tried to provide a working directory string that doesn't represent a working directory. It's not your fault, because it wasn't possible to design the FileStore class in such a way that this is a statically typed pre-condition, but please supply a valid path to an existing directory. </p> <p> "The invalid value was: "fllobdedy"." </p> </blockquote> How do those two messages make you feel? </p> <p> The first type of exception message often make me feel that I suck. The second is mostly an annoyance that my program doesn't work, followed by delight that the object I'm using makes it easy for me to <em>solve my problem</em>. </p> <p> If you encounter the first exception message, you'll often need to attach a debugger to figure out what was wrong, or at least open the offending code to understand what made it fail. In the second example, the object you're using does its best to provide you with all the information you need in order to solve the problem. </p> <h3 id="b667f38a68b54fdbbbfaa13e11dc8ee0"> Call to action <a href="#b667f38a68b54fdbbbfaa13e11dc8ee0" title="permalink">#</a> </h3> <p> Do write useful exception messages aimed at other developers. Use correct grammar, spelling, and punctuation. Try to phrase the message in a non-blaming way. Supply information about the context or the offending value. If possible, suggest how to resolve the error. </p> <p> It will take you an extra five minutes to write such an exception message, but will save your colleagues, your clients, and your future self hours of troubleshooting and debugging. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0d8ceb0f8200409a88f6862e893318b4"> <div class="comment-author"><a href="http://mookid.dk/oncode">Mogens Heller Grabe</a> <a href="#0d8ceb0f8200409a88f6862e893318b4">#</a></div> <div class="comment-content"> <p>Exceptions with exceptionally great error messages is a pet peeve of mine, and I've always had the personal philosophy that exceptions should not be a slap in the face; they should be a step forward. Therefore, I usually follow the rule that exceptions must provide both a) details about the error (including, of course, the wrong value and information about the expected domain), and b) one or more suggestions for a way forward (which may have been done implicitly already by elaborating on the domain, but may sometimes require extra effort like e.g. <a href="https://github.com/rebus-org/Rebus/blob/master/src/Rebus/Transports/Encrypted/EncryptionAndCompressionConfigurationExtensions.cs#L129">helping with coming up with a proper value</a>).</p> <p>I have a question for you though, regarding exceptions: I usually introduce a special exception when I'm building software for clients: a <code>DomainException</code>! As you might have guessed, it's a special exception that is meant to be consumed by humanoids, i.e. the end user. This means that it is crafted to be displayed without a stack trace, providing all the necessary context through its <code>Message</code> alone. This is the exception that the domain model may throw from deep down, telling the user intimate details about a failed validation that could only be checked at that time and place.</p> <p>As far as I can see, there's two problems with my <code>DomainException</code>: 1) It's not that <a href="https://pragprog.com/the-pragmatic-programmer/extracts/tips">exceptional</a> since it's used as a fallback validation that can be used when the UI is too lazy to provide the validation (or has higher prioritized work to do), and 2) I wouldn't know how to introduce a second language in my application with this approach.</p> <p>The 1st "problem" isn't really a problem in my book - I get that it's silly to use exceptions for control flow, but I bet there's no way you can provide a clearer and more succinct way of guarding the domain model's integrity AND provide an excellent error message at the same time than you can by throwing an appropriate <code>DomainException</code>.</p> <p>The 2nd problem <em>could</em> be a problem though - luckily, I haven't yet tried building something that didn't use either Danish or English as its only language, but I guess I could make the <code>DomainException</code> localizable somehow...</p> <p>I'm curious, though, as to how other people solve the problem of deep-domain validation, and now - since you wrote this blog post - I'm asking you: how do you do it? :)</p> </div> <div class="comment-date">2014-12-23 09:56 UTC</div> </div> <div class="comment" id="3d68142c5de34bd6979dc8bafc5714e2"> <div class="comment-author"><a href="http://softwarepassion.eu">Torben Rahbek Koch</a> <a href="#3d68142c5de34bd6979dc8bafc5714e2">#</a></div> <div class="comment-content"> <p>I wholeheartedly agree with you, Mark. Users shouldn't see technical error messages and they really don't care about them. They just want it to work and when it doesn't, they want to feel sure that somebody is looking into it. </p> <p> Exceptions and error handling are, obviously, tightly connected and <a href="http://softwarepassion.eu/error-handling-the-easy-way/">I have written about this</a>. Its a long piece, but the gist of it is that there are three general classes of errors: <ul> <li>Unrecoverable errors - the computers and the users involved cannot do anything about.</li> <li>Recoverable errors requiring user intervention - e.g. spelling mistakes.</li> <li>Recoverable errors not requiring user intervention - e.g. time outs, which can be automatically retried.</li> </ul> </p> <p> What <em>is</em> important for an exception is for it to contain enough information for the UI-layer (be it web, desktop, whatever), to ensure the user that somebody is actually on the go to solve the problem. It could be something as simple as a link to where the user can be updated about progress. This of course means that exceptions should be logged and meticulously followed up upon, which takes time and resources. </p> <p> This leads on to the general concept of application logging, which I have not written about, yet ;) </p> <p>To Mogens: I think your <code>DomainException</code> should not contain any text, per say, just the data. The text should then be stored in resources, the same way as any other localizable text should be. </div> <div class="comment-date">2014-12-23 15:32 UTC</div> </div> <div class="comment" id="ee940feef0394ca1bdd448638bfeee01"> <div class="comment-author"><a href="http://j2jensen.blogspot.com">James Jensen</a> <a href="#ee940feef0394ca1bdd448638bfeee01">#</a></div> <div class="comment-content"> <p>I sympathize with Mogens. I, too, have found situations where an exception wants to be able to provide a user-facing message. However, I think I've found a better pattern for this that avoids a lot of problems. I posted about it <a href="http://j2jensen.blogspot.com/2014/12/userfacingexceptionsmd.html">here.</a> </p> </div> <div class="comment-date">2014-12-25 22:31 UTC</div> </div> <div class="comment" id="d48cfd65bbe540f8b93635d8ff58868d"> <div class="comment-author"><a href="http://serlock-works.blogspot.com">Fendy</a> <a href="#d48cfd65bbe540f8b93635d8ff58868d">#</a></div> <div class="comment-content"> <p> It's a good article which mostly forgotten by developers. The points are well delivered. I also have some of those badly-formed exceptions. Which I just fixed after reading this article. </p> <p> As for Mogens, I kinda agree with Torben. The <code>DomainException</code> may contain text or raw data, and let the top-level exception handler at your application handle the message format for you. It can decrease coupling as you can handle the message to different UI (batch, web, desktop, mobile). It also provide more properly formatted text. </p> </div> <div class="comment-date">2015-01-08 04:02 UTC</div> </div> <div class="comment" id="aaa85feee1ea4e4c9aa51012ec9018b9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#aaa85feee1ea4e4c9aa51012ec9018b9">#</a></div> <div class="comment-content"> <p> Mogens, thank you for writing. I know that you have a passion for well-written exception messages; in fact, you are one of the many people who inspired me to make exception messages more and more helpful. </p> <p> How do I deal with 'deep' exceptions? First of all, I try hard to avoid them in the first place. One of the ways to do that is to validate input as close to the application boundary as possible, and transform it into something that properly protects its invariants; I recently wrote <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">an article with a simple example of doing that</a> (you may also want to read the comments, which contain a few gems). </p> <p> Even if you can eliminate many 'deep' exceptions, you can't get rid of all of them. Particularly if you integrate with legacy systems, it can be difficult to validate everything up front, because the 'rules' of the legacy system may not be clear or public. In such cases, I prefer to throw custom exceptions. In each place where I need to throw an exception, I create a new Exception class and throw that. This means that any client can catch that particular exception type, and convert it to a friendly user message. If you need to provide additional information to the user, the custom Exception class can have extra properties with the required information. </p> <p> Typically, if you start with the approach of validating input, and protecting invariants of objects, you aren't going to need a ton of custom Exception classes, so I find this approach manageable, although still not 'nice'. </p> <p> The solution described by James Jensen above is one that I've never seen before, but it sounds like a good alternative. </p> <p> As an overall comment, it's true that we shouldn't use exceptions for control flow, but unfortunately, most object-oriented languages give us no nice alternatives. However, in languages with Sum Types (Discriminated Unions in F#), there are <a href="http://fsharpforfunandprofit.com/posts/recipe-part2">much better options for error handling</a>. That's what I use when I write F# code, which is my preferred language these days. </p> </div> <div class="comment-date">2015-01-20 09:22 UTC</div> </div> <div class="comment" id="3880ca7c30db49ebaf5c2dfbd4c67d46"> <div class="comment-author"><a href="https://github.com/joeyhub/">Joey</a> <a href="#3880ca7c30db49ebaf5c2dfbd4c67d46">#</a></div> <div class="comment-content"> <p> I agree with this post at a basic level. Starting out this is the right way to handle exceptions. The thing is, you haven't really addressed the problems this restriction leads on to. </p> <p> Internally caught exceptions are for the programmer to handle and write code for. You may want different types to indicate the handling. For example, I have a piece of code that has to communicate over an unreliable network connection. When this fails, it tends to be more efficient to catch to exception and make certain decisions about retrying than to have the error bounce all the way back up the chain of commands where all that will happen in most circumstances anyway is that a retry attempt will be made. </p> <p> What happens when the programmer doesn't catch the exception? Why can't it be for the user? What if there is something more you can do to help the user beyond "computer said no"? There might be a reason that meme exists. Something unexpected can happen in the code and IO, the user is one of those components. Why can't it be handled there or in another system the exception propagates to? This is a weird one if you think about it. For any other IO fault I can just throw an exception saying what is wrong but when it comes to the user, I can't use exceptions for this? </p> <p> I believe that in any large multiple interface project that exceptions can and potentially should be for everyone. However I do not believe this should be ambiguous. </p> <p> I solve this by creating special "UserDoneBadExceptions" so to speak (not literally called that). A few other names and approaches come to mind such as safe exception and friendly exception (hi *wave*). I'll stick to safe exception from now on. Different languages or frameworks will influence which approach for this is best. Personally I've found myself having to monkey patch some nasty kludges for this. </p> <p> If something reaches the top level without being caught, then it will be forwarded to the next system if it is a safe exception otherwise the current exception will be consumed (such a to the log) and a safe exception will be sent. A safe exception is no different really to those special exceptions that help tell the programmer how to handle them or what to do (retry, fallback, log and ignore, abort, reopen, reconnect, clean up in a certain way, etc). I tend to describe exceptions that need to be converted as give up, complain to support, prey, retry, figure out a workaround, do what you want or shrug exceptions. Basically they're an ouch, the error has been logged, the developer *may* look at the logs and fix it at some date but if it's really important, workaround, go to support, etc. </p> <p> Establishing that it might be a good idea for an exception system to indicate action at every level raises the question of what to indicate for an exception exiting the system to another system. You always assume at some point it will reach a user so the default is typically that a safe exception triggered by any other exception should explain to the user that an error has occured, not much can be done, perhaps a link to support, definitely some means of linking to the error log (at least a timestamp) and in extreme cases the error log encrypted so they can copy and paste it to support. </p> <p> There is a repeat ambiguity with safe exceptions because the receiving system may be able to handle the exception. In fact it may be handled both in how it deals with the user and through automation. It is common to extend safe exceptions so that they contain action for the user in the sense of providing them with feedback about what they can do as well as sufficient data for the system so that if it is capable it can take action. I've found the two most common cases for this is to either be able to provide auxilery updated data if the front end is out of date or to indicate a redirect (which can double as a refresh, be careful, very careful). A simple way to look at it is if you cared for using HTTP status codes then which codes and messages should you exceptions indicate the use of? </p> <p> What this really comes down to is that you need to define separate domains for your exceptions. Are they internal or external? Are they some combination of both (if you are poking your own RPC service then it's technically internal external but really just internal, you can propagate it as-is)? The concept could be called intersystem exceptions. The benefit I find for this is better code reuse and generally less coded needed. I can use all of my permissions systems for RPC for anything with sometimes a thin layer of conversion for different output types and it works pretty well as a one size fits all. Whether my exceptions are poking out into AJAX, websocket XML RPC, JSON RPC, HTML full page load, etc in each case a thin layer of conversion, handling, etc manages it all. </p> <p> With that it is important to make special safe exceptions reasonably abstract avoiding specifics unless specifics are needed or work better. It's something to think about. Should a login required safe exception simply be a redirect exception to the login page or should it be down to the system to interpret the error type/code and to then be self aware about where the login prompt should be and where to display it? I would personally opt for the latter in a lot of cases but it really depends on what systems you're deailing with. This approach wont be for everyone. Not that many projects become large enough to justify architecting a complex inter system exception system. </p> </div> <div class="comment-date">2016-09-20 21:00 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Good times with F# https://blog.ploeh.dk/2014/12/17/good-times-with-f 2014-12-17T08:11:00+00:00 Mark Seemann <div id="post"> <p> <em>Some operations take time to perform, and you may want to capture that information. This post describes one way to do it.</em> </p> <p> Occasionally, you may find yourself in the situation where your software must make a decision based on how much time it has left. Is there time to handle one more message before shutting down? How much time did the last operation take? What was the time when it started? </p> <p> It can be tempting to simply use <a href="http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx">DateTime.Now</a> or <a href="http://msdn.microsoft.com/en-us/library/system.datetimeoffset.now.aspx">DateTimeOffset.Now</a>, but the problem is that it couples your code to the machine clock. It also tends to interleave timing logic with other logic, so that it becomes difficult to vary these independently. Incidentally, it also tends to make it difficult to unit test the time-dependent code. </p> <h3 id="a892fd6aa592401ca4496a9ac0b00468"> Timed&lt;'a&gt; <a href="#a892fd6aa592401ca4496a9ac0b00468" title="permalink">#</a> </h3> <p> One alternative to DateTimeOffset.Now that I've recently found useful is to define a Timed&lt;'a&gt; generic type, like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">Timed</span>&lt;&#39;a&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Started&nbsp;:&nbsp;<span style="color:#2b91af;">DateTimeOffset</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Stopped&nbsp;:&nbsp;<span style="color:#2b91af;">DateTimeOffset</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:&nbsp;&#39;a&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Duration&nbsp;=&nbsp;this.Stopped&nbsp;-&nbsp;this.Started</pre> </p> <p> A value of Timed&lt;'a&gt; not only carries with it the result of some computation, but also information about when it started and stopped. It also has a calculated property called Duration, which you can use to examine how long the operation took. </p> <h3 id="5e7a94f80a88467e8b46554be5c212ac"> Untimed transformations <a href="#5e7a94f80a88467e8b46554be5c212ac" title="permalink">#</a> </h3> <p> Timed&lt;'a&gt; looks like a standard monadic type, and you can also implement standard Bind and Map functions for it: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:#2b91af;">Untimed</span>&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;bind&nbsp;f&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;t&nbsp;=&nbsp;f&nbsp;x.Result &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;t&nbsp;<span style="color:blue;">with</span>&nbsp;Started&nbsp;=&nbsp;x.Started&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Started&nbsp;=&nbsp;x.Started;&nbsp;Stopped&nbsp;=&nbsp;x.Stopped;&nbsp;Result&nbsp;=&nbsp;f&nbsp;x.Result&nbsp;}</pre> </p> <p> Notice, though, that these functions only transforms the Result (and, in the case of Bind, Stopped), but no actual time measurement is taking place; that's the reason I put both functions in a module called Untimed. </p> <p> In the case of the Bind function, the time measurement is implicitly taking place when the <code>f</code> function is executed, because it returns a Timed&lt;'b&gt;. In the Map function, on the other hand, no time measurement takes place at all. The function simply maps Result from one value to another using the <code>f</code> function. </p> <p> So far, I've found no use for the Bind function, and only occasional use for the Untimed.map function. What really matters, after all, is to measure time. </p> <h3 id="d8f6f10cc13f429db55f2253e9a3661c"> Timing functions <a href="#d8f6f10cc13f429db55f2253e9a3661c" title="permalink">#</a> </h3> <p> Ultimately, Timed&lt;'a&gt; is only interesting if you measure the time computations take. Here are a few functions I've found useful to do that: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:#2b91af;">Timed</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;capture&nbsp;clock&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;now&nbsp;=&nbsp;clock() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Started&nbsp;=&nbsp;now;&nbsp;Stopped&nbsp;=&nbsp;now;&nbsp;Result&nbsp;=&nbsp;x&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;clock&nbsp;f&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;result&nbsp;=&nbsp;f&nbsp;x.Result &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stopped&nbsp;=&nbsp;clock&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Started&nbsp;=&nbsp;x.Started;&nbsp;Stopped&nbsp;=&nbsp;stopped;&nbsp;Result&nbsp;=&nbsp;result&nbsp;}</pre> </p> <p> Notice that both functions take a <code>clock</code> argument. This is a function with the signature <span style="white-space: nowrap"><code>unit -&gt; DateTimeOffset</code></span>, enabling the capture and map functions to get the time. As the name indicates, the capture function is used to capture a value and 'promote' it to a Timed&lt;'a&gt; value. This value can subsequently be used with the map function to measure the time it took to exectute the <code>f</code> function. </p> <h3 id="29ebe811d4954ddfad3190bca02ee865"> Clocks <a href="#29ebe811d4954ddfad3190bca02ee865" title="permalink">#</a> </h3> <p> Using the computer's clock is easy: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;machineClock&nbsp;()&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now </pre> </p> <p> As you can tell, this function returns the value of DateTimeOffset.Now every time it's invoked. </p> <p> You can also implement other clocks, e.g. for unit testing purposes. Here's one (not particularly Functional) way of doing it: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;qlock&nbsp;(q&nbsp;:&nbsp;System.Collections.Generic.<span style="color:#2b91af;">Queue</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>&gt;)&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;q.Dequeue()</pre> </p> <p> Obviously, since this clock is based on a Queue, I decided to call it qlock. Using that, you can define an easier-to-use function based on a sequence of DateTimeOffset values: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;seqlock&nbsp;(l&nbsp;:&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;<span style="color:#2b91af;">seq</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;qlock&nbsp;(System.Collections.Generic.<span style="color:#2b91af;">Queue</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>&gt;(l))</pre> </p> <p> Here's a fake clock that counts up all the days before Christmas in December (so obviously, I had to name it <em>xlock</em>): </p> <p> <pre><span style="color:blue;">let</span>&nbsp;xlock&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[1&nbsp;..&nbsp;24] &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;d&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(<span style="color:#2b91af;">DateTime</span>(2014,&nbsp;12,&nbsp;d))) &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;seqlock</pre> </p> <p> The first time you execute xlock: </p> <p> <pre>(xlock&nbsp;()).ToString(<span style="color:#a31515;">&quot;d&quot;</span>)</pre> </p> <p> you get "01.12.2014", the next time you get "02.12.2014", the third time "03.12.2014", and so on. </p> <p> Such fake clocks are mostly useful for testing, but you could also use them to perform simulations in accelerated time - or perhaps even move time backwards! (Last time I used it, <a href="http://www.worldwidetelescope.org">WorldWide Telescope</a> enabled you to look at the solar system in accelerated time. I don't know how that feature is implemented, but it sounds like a good case for a custom clock. The point of this digression is that depending on your application's need, being able to manipulate time may not be as exotic as it first sounds.) </p> <h3 id="5edc0d15035c45c4b45da1ce69bdce97"> Measuring the time of execution <a href="#5edc0d15035c45c4b45da1ce69bdce97" title="permalink">#</a> </h3> <p> These are all the building blocks you need to measure the time it takes to perform an operation. There are various ways to do it, but given a <code>clock</code> of the type <span style="white-space: nowrap"><code>unit -&gt; DateTimeOffset</code></span>, you can close over the clock like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;time&nbsp;f&nbsp;x&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Timed</span>.capture&nbsp;clock&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Timed</span>.map&nbsp;clock&nbsp;f</pre> </p> <p> The <code>time</code> function has the type <span style="white-space: nowrap"><code>('a -&gt; 'b) -&gt; 'a -&gt; Timed&lt;'b&gt;</code></span>, so, if for example you have a function called <code>readCustomerFromDb</code> of the type <span style="white-space: nowrap"><code>int -&gt; Customer option</code></span>, you can time it like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;c&nbsp;=&nbsp;time&nbsp;readCustomerFromDb&nbsp;42</pre> </p> <p> This might give you a result like this: </p> <p> <pre>{Started&nbsp;=&nbsp;17.12.2014&nbsp;11:24:38&nbsp;+01:00; &nbsp;Stopped&nbsp;=&nbsp;17.12.2014&nbsp;11:24:39&nbsp;+01:00; &nbsp;Result&nbsp;=&nbsp;Some&nbsp;{Id&nbsp;=&nbsp;42;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Foo&quot;</span>;};}</pre> </p> <p> As you can tell, the result of querying the database for customer 42 is the customer data, as well as information about when the operation started and stopped. </p> <h3 id="19eab295f1384b54b5d6273c410d5982"> Concluding remarks <a href="#19eab295f1384b54b5d6273c410d5982" title="permalink">#</a> </h3> <p> The nice quality of Timed&lt;'a&gt; is that it decouples timing of operations from the result of those operations. This enables you to compose functions without explicitly having to consider how to measure their execution times. </p> <p> It also helps with unit testing, because if you want to test what happens in a function if a previous function ran on a Sunday, or if it took more than three seconds to run, you can simply create a value of Timed&lt;'a&gt; that captures that information: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;c&#39;&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Started&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(<span style="color:#2b91af;">DateTime</span>(2014,&nbsp;12,&nbsp;14,&nbsp;11,&nbsp;43,&nbsp;11)) &nbsp;&nbsp;&nbsp;&nbsp;Stopped&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(<span style="color:#2b91af;">DateTime</span>(2014,&nbsp;12,&nbsp;14,&nbsp;11,&nbsp;43,&nbsp;15)) &nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;=&nbsp;Some&nbsp;{&nbsp;Id&nbsp;=&nbsp;1337;&nbsp;Name&nbsp;=&nbsp;<span style="color:#a31515;">&quot;Ploeh&quot;</span>&nbsp;}&nbsp;}</pre> </p> <p> This value can now be used in a unit test to verify the behaviour in that particular case. </p> <p> In a future post, I will demonstrate how to compose more complex time-sensitive behaviour using Timed&lt;'a&gt; and the associated modules. </p> <p> <em>This post is number 17 in the <a href="http://sergeytihon.wordpress.com/2014/11/24/f-advent-calendar-in-english-2014">2014 F# Advent Calendar</a>.</em> </p> <p> <b>Update 2017-05-25:</b> A more recent treatment, including a more realistic usage scenario, is available in the article <a href="/2015/08/10/type-driven-development">Type Driven Development</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Name this operation https://blog.ploeh.dk/2014/12/16/name-this-operation 2014-12-16T12:54:00+00:00 Mark Seemann <div id="post"> <p> <em>A type of operation seems to be repeatedly appearing in my code, but I can't figure out what to call it; can you help me?</em> </p> <p> Across various code bases that I work with, it seems like operations (methods or functions) like these keep appearing: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IDictionary</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;Foo( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDictionary</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;bar,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Baz</span>&nbsp;baz)</pre> </p> <p> In order to keep the example general, I've renamed the method to <code>Foo</code>, and the arguments to <code>bar</code> and <code>baz</code>; in this article throughout, I'll make heavy use of <a href="http://en.wikipedia.org/wiki/Metasyntactic_variable">metasyntactic variables</a>. The types involved aren't particularly important; in this example they are IDictionary&lt;string,&nbsp;string&gt; and Baz, but they could be anything. The crux of the matter is that the output type is the same as one of the inputs (in this case, IDictionary&lt;string,&nbsp;string&gt; is both input and output). </p> <p> Another C# example could be: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Qux(<span style="color:blue;">int</span>&nbsp;corge,&nbsp;<span style="color:blue;">string</span>&nbsp;grault)</pre> </p> <p> Notice that the types involved don't have to be interfaces or complex types. The only recurring motif seems to be that the type of one of the arguments is identical to the return type. </p> <p> Although the return value shares its <em>type</em> with one of the input arguments, the implementation doesn't have to echo the <em>value</em> as output. In fact, this is often not the case. </p> <p> At this point, there's also no implicit assumption that the operation is referentially transparent. Thus, we may have implementation where some database is queried. Another possibility is that the operation is a closure over additional values. Here's an F# example: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sign&nbsp;calculateSignature&nbsp;issuer&nbsp;(expiry&nbsp;:&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>)&nbsp;claims&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;claims&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.append&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Type&nbsp;=&nbsp;<span style="color:#a31515;">&quot;signature&quot;</span>;&nbsp;Value&nbsp;=&nbsp;calculateSignature&nbsp;claims&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Type&nbsp;=&nbsp;<span style="color:#a31515;">&quot;issuer&quot;</span>;&nbsp;Value&nbsp;=&nbsp;issuer&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Type&nbsp;=&nbsp;<span style="color:#a31515;">&quot;expiry&quot;</span>;&nbsp;Value&nbsp;=&nbsp;expiry.ToString&nbsp;<span style="color:#a31515;">&quot;o&quot;</span>&nbsp;}]</pre> </p> <p> You could imagine that the purpose of this function is to sign a set of security claims, by calculating a signature and add this and other issuer claims to the input list of claims. The signature of this particular function is <span style="white-space: nowrap;"><code>(Claim list -&gt; string) -&gt; string -&gt; DateTimeOffset -&gt; Claim list -&gt; Claim list</code></span>. However, the leftmost arguments aren't going to vary much, so it would make sense to partially apply the function. Imagine that you have a correctly implemented signature function called calculateHMacSha, and that the name of the issuer should be "Ploeh": </p> <p> <pre><span style="color:blue;">let</span>&nbsp;sign&#39;&nbsp;=&nbsp;sign&nbsp;calculateHMacSha&nbsp;<span style="color:#a31515;">&quot;Ploeh&quot;</span> </pre> </p> <p> The type of the sign' function is <span style="white-space: nowrap;"><code>DateTimeOffset -> Claim list -> Claim list</code></span>, which is equivalent to the above Foo and Qux methods. </p> <p> The question is: <strong>What should we call such an operation?</strong> </p> <p> <strong>Why care?</strong> </p> <p> In its general form, such an operation would have the shape </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IGrault</span>&lt;T1,&nbsp;T2&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T1&nbsp;Garply(T1&nbsp;waldo,&nbsp;T2&nbsp;fred); }</pre> </p> <p> in C# syntax, or <span style="white-space: nowrap;"><code>'a -&gt; 'b -&gt; 'b</code></span> type in F# syntax. </p> <p> Why should you care about operations like these? </p> <p> It turns out that they are rather composable. For instance, it's trivial to implement the <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a> pattern, because you can always simply return the matching argument. Often, it also makes sense to make <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composites</a> out of them, or <a href="http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern">chain them</a> in various ways. </p> <p> In Functional Programming, you may just do this by composing functions together (while not particularly considering the terminology of what you're doing), but in Object-Oriented languages like C#, we have to <em>name</em> the abstraction before we can use it (because we have to define the type). </p> <p> If <code>T2</code> or <code>'a</code> is a predicate, the operation might be a <em>filter</em>, but as illustrated with the above sign' function, the operation may also enrich the input. In some cases, it may even replace the original input and return a completely different value (of the same type). Thus, such an operation could be a 'filter', an 'expansion', or a 'replacement'; those are quite vague properties, so any name for such operations are likely to be vague or broad as well. </p> <p> My question to you is: <em>what do we call this type of operation?</em> </p> <p> While I do have a short-list of candidate names, I encounter these constructs so often that I'd be surprised if such operations don't already have a conceptual name. If so, what is it? If not, what should we call them? </p> <p> (I'd hoped that this type of operation was already a 'thing' in functional programming, so I <a href="https://twitter.com/ploeh/status/544793537288892416">asked on Twitter</a>, but the context is often lost on Twitter, so therefore this blog post.) </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="65151fb46e9e48ef8150e5aa4475c89f"> <div class="comment-author">Kenny Pflug <a href="#65151fb46e9e48ef8150e5aa4475c89f">#</a></div> <div class="comment-content"> <p>Hey Mark,</p> <p>In a mathematical sense, I would say that this operation is an accumulation: you take a (complex or simple) value and according to the second parameter (and possible other contextual information), this value is transformed, which could mean that either subvalues are changed on the (mutable) target or a new value (which is probably immutable) is created from the given information. This whole process could imply that information is added or stripped from the first parameter.</p> <p>What I found interesting is that there is always exactly one other parameter (not two or more), and it feels like the information from two is incorporated into parameter one.</p> <p>Thus I would call this operation a <b>Junction</b> or <b>Consolidation</b> because information of parameter 2 is joined with parameter 1.</p> </div> <div class="comment-date">2015-01-01 10:47 UTC</div> </div> <div class="comment" id="6011d7788f064c5f8d50170c3bebc1f3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6011d7788f064c5f8d50170c3bebc1f3">#</a></div> <div class="comment-content"> <p> Kenny, thank you for your suggestions. Others have pointed out the similarities with accumulation, so that looks like the most popular candidate. <em>Junction</em> and <em>Consolidation</em> are new to me, but I've added both to my list. </p> <p> When it comes to the second argument, I decided to present the problem with only a second argument in order to keep it simple. You could also have variations with two or three arguments, but since you can rephrase any parameter list to a <a href="http://c2.com/cgi/wiki?ParameterObject">Parameter Object</a>, it's not particularly important. </p> </div> <div class="comment-date">2015-01-02 12:28 UTC</div> </div> <div class="comment" id="0b48994ca7e349759f6bc3ba3da8c89d"> <div class="comment-author">Kenny Pflug <a href="#0b48994ca7e349759f6bc3ba3da8c89d">#</a></div> <div class="comment-content"> <p>Dear Mark,</p> <p>I choose <em>Junction</em> over accumulation because it is a word that (almost) everybody is familiar with: a junction of rivers. When two or more rivers meet, they form a new river or the tributary rivers flow into the main river. I think this natural phenomenon is a reasonable metaphor for the operation your describing in your post.</p> <p><em>Accumulation</em> in contrast is specific to mathematics and IMHO I don't think everybody would understand the concept immediately (especially when lacking academic math education). Thus I think the name Junction can be grasped more easily - especially if you think of other OOP patterns like <em>Factory, Command, Memento</em> or <em>Decorator</em>: the pattern name provides a metaphor to a thing or phenomena (almost) everybody knows, which make them easy to learn and remember.</p> </div> <div class="comment-date">2015-01-02 15:14 UTC</div> </div> <div class="comment" id="68cebcb5681f444ba07e9fe580a57f5f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#68cebcb5681f444ba07e9fe580a57f5f">#</a></div> <div class="comment-content"> <p> It was never my intention to imply that I necessarily considered <em>Accumulation</em> the best option, but it has been mentioned independently multiple times. </p> <p> At first, I didn't make the connection to river junctions, but now that you've explained the metaphor, I <em>really</em> like it. It's like a tributary joining a major river. The Danube is still the Danube after the Inn has joined it. Thank you. </p> </div> <div class="comment-date">2015-01-02 15:53 UTC</div> </div> <div class="comment" id="89c60d7ef345445f8811e45fc754c8e5"> <div class="comment-author">Kenny Pflug <a href="#89c60d7ef345445f8811e45fc754c8e5">#</a></div> <div class="comment-content"> <p>Sorry, I didn't want to sound like a teacher :-)</p> </div> <div class="comment-date">2015-01-03 11:49 UTC</div> </div> <div class="comment" id="4fc84109b5f9458cb4d090b2467072e8"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#4fc84109b5f9458cb4d090b2467072e8">#</a></div> <div class="comment-content"> <p> Not sure if you came to a conclusion here. Here are some relevant terms I've witnessed other places. </p> <ul> <li>In f#, these kinds of functions are often composed by piping (|>), so maybe IPipeable</li> <li>Clojure has the <a href="https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/-%3e">threading macro</a>. Unfortunately, thread has interference with <a href="https://en.wikipedia.org/wiki/Thread_%28computing%29">scheduling threads</a></li> <li>The clojure library <a href="http://pedestal.io/">pedestal</a> operates around a pattern that is effectively lists of strategies. They call it interceptor, but that has interference with the more AOP concept of <a href="https://en.wikipedia.org/wiki/Interceptor_pattern">interceptor</a></li> </ul> <p> I'm curious, did you experiment with this pattern in a language without partial application? It seems like shepherding operations into the shared type would be verbose. </p> </div> <div class="comment-date">2022-02-22 02:37 UTC</div> </div> <div class="comment" id="9f730b4b70bb4de5ad043b8da0ec1281"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9f730b4b70bb4de5ad043b8da0ec1281">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. You're certainly reaching back into the past! </p> <p> Identifying a name for this kind of API was something that I struggled with <a href="/2010/12/03/Towardsbetterabstractions">at least as far back as 2010</a>, where, for lack of better terms at the time I labelled them <em>Closure of Operations</em> and <em>Reduction of Input</em>. As <a href="/2019/01/28/better-abstractions-revisited">I learned much later</a>, these are essentially <a href="/2017/11/13/endomorphism-monoid">endomorphisms</a>. </p> </div> <div class="comment-date">2022-02-23 8:55 UTC</div> </div> <div class="comment" id="2d042d8ffa704bf6b9265c27db0cf60b"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#2d042d8ffa704bf6b9265c27db0cf60b">#</a></div> <div class="comment-content"> <p> I've learned a lot from your content! I decided to start at the beginning to experience your evolution of thought over time. I love how deeply you think and your clear explanations of complex ideas. Thank you for sharing your experience! </p> <p> Endomorphism makes sense. I got too focused on my notion of piping and forgot the key highlighted value was closure. </p> </div> <div class="comment-date">2022-02-23 15:11 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The IsNullOrWhiteSpace trap https://blog.ploeh.dk/2014/11/18/the-isnullorwhitespace-trap 2014-11-18T19:10:00+00:00 Mark Seemann <div id="post"> <p> <em>The IsNullOrWhiteSpace method may seem like a useful utility method, but poisons your design perspective.</em> </p> <p> The <a href="http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx">string.IsNullOrWhiteSpace</a> method, together with its older sibling <a href="http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx">string.IsNullOrEmpty</a>, may seem like useful utility methods. In reality, they aren't. In fact, they trick your mind into thinking that null is equivalent to white space, which it isn't. </p> <p> Null isn't equivalent to <em>anything</em>; it's the absence of a value. </p> <p> Various empty and white space strings ("", "&nbsp;", etc), on the other hand, <em>are</em> values, although, perhaps, not particularly interesting values. </p> <h3 id="9257379c895a48fe827ea57b138c266f"> Example: search canonicalization <a href="#9257379c895a48fe827ea57b138c266f" title="permalink">#</a> </h3> <p> Imagine that you have to write a simple search canonicalization algorithm for a music search service. The problem you're trying to solve is that when users search for music, the may use variations of upper and lower case letters, as well as type the artist name before the song title, or vice versa. In order to make your system as efficient as possible, you may want to cache popular search results, but it means that you'll need to transform each search term into a <a href="http://en.wikipedia.org/wiki/Canonical_form">canonical form</a>. </p> <p> In order to keep things simple, let's assume that you only need to convert all letters to upper case, and order words alphabetically. </p> <p> Here are five test cases, represented as a <a href="http://xunitpatterns.com/Parameterized%20Test.html">Parameterized Test</a>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;Seven&nbsp;Lions&nbsp;Polarized&quot;</span>&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;LIONS&nbsp;POLARIZED&nbsp;SEVEN&quot;</span>&nbsp;&nbsp;)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;seven&nbsp;lions&nbsp;polarized&quot;</span>&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;LIONS&nbsp;POLARIZED&nbsp;SEVEN&quot;</span>&nbsp;&nbsp;)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;Polarized&nbsp;seven&nbsp;lions&quot;</span>&nbsp;&nbsp;,&nbsp;<span style="color:#a31515;">&quot;LIONS&nbsp;POLARIZED&nbsp;SEVEN&quot;</span>&nbsp;&nbsp;)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;Au5&nbsp;Crystal&nbsp;Mathematics&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;AU5&nbsp;CRYSTAL&nbsp;MATHEMATICS&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;crystal&nbsp;mathematics&nbsp;au5&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;AU5&nbsp;CRYSTAL&nbsp;MATHEMATICS&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CanonicalizeReturnsCorrectResult( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;searchTerm, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">SearchTerm</span>.Canonicalize(searchTerm); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); }</pre> </p> <p> Here's one possible implementation that passes all five test cases: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Canonicalize(<span style="color:blue;">string</span>&nbsp;searchTerm) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;searchTerm &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Split(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&#39;&nbsp;&#39;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(x&nbsp;=&gt;&nbsp;x.ToUpper()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OrderBy(x&nbsp;=&gt;&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>&nbsp;+&nbsp;y); }</pre> </p> <p> This implementation uses the space character to split the string into an array, then converts each sub-string to upper case letters, sorts the sub-strings in ascending order, and finally concatenates them all together to a single string, which is returned. </p> <h3 id="2fa3d719c3034aa98d7b85e35ab37a54"> Continued example: making the implementation more robust <a href="#2fa3d719c3034aa98d7b85e35ab37a54" title="permalink">#</a> </h3> <p> The above implementation is quite naive, because it doesn't properly canonicalize if the user entered extra white space, such as in these extra test cases: </p> <p> <pre>[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;Seven&nbsp;&nbsp;Lions&nbsp;&nbsp;&nbsp;Polarized&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;LIONS&nbsp;POLARIZED&nbsp;SEVEN&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;&nbsp;Seven&nbsp;&nbsp;Lions&nbsp;Polarized&nbsp;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;LIONS&nbsp;POLARIZED&nbsp;SEVEN&quot;</span>)]</pre> </p> <p> Notice that these new test cases don't pass with the above implementation, because it doesn't properly remove all the white spaces. Here's a more robust implementation that passes all test cases: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Canonicalize(<span style="color:blue;">string</span>&nbsp;searchTerm) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;searchTerm &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Split(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&#39;&nbsp;&#39;</span>&nbsp;},&nbsp;<span style="color:#2b91af;">StringSplitOptions</span>.RemoveEmptyEntries) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(x&nbsp;=&gt;&nbsp;x.ToUpper()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OrderBy(x&nbsp;=&gt;&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>&nbsp;+&nbsp;y); }</pre> </p> <p> Notice the addition of <code>StringSplitOptions.RemoveEmptyEntries</code>. </p> <h3 id="5008ef2593b94c1582a52046d3c62ca4"> Testing for null <a href="#5008ef2593b94c1582a52046d3c62ca4" title="permalink">#</a> </h3> <p> If you consider the above implementation, does it have any other problems? </p> <p> One, fairly obvious, problem is that if <code>searchTerm</code> is null, the method is going to throw a NullReferenceException, because you can't invoke the Split method on null. </p> <p> Therefore, in order to protect the invariants of the method, you must test for null: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;CanonicalizeNullThrows() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Throws&lt;<span style="color:#2b91af;">ArgumentNullException</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;()&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">SearchTerm</span>.Canonicalize(<span style="color:blue;">null</span>)); }</pre> </p> <p> In this case, you've decided that null is simply invalid input, and I agree. Searching for null (the absence of a value) isn't meaningful; it must be a defect in the calling code. </p> <p> Often, I see programmers implement their null checks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Canonicalize(<span style="color:blue;">string</span>&nbsp;searchTerm) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(searchTerm)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;searchTerm&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;searchTerm &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Split(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&#39;&nbsp;&#39;</span>&nbsp;},&nbsp;<span style="color:#2b91af;">StringSplitOptions</span>.RemoveEmptyEntries) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(x&nbsp;=&gt;&nbsp;x.ToUpper()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OrderBy(x&nbsp;=&gt;&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>&nbsp;+&nbsp;y); }</pre> </p> <p> Notice the use of IsNullOrWhiteSpace. While it passes all tests so far, <em>it's wrong</em> for a number of reasons. </p> <h3 id="31691bd21ecc43abaabf314f637c7dab"> Problems with IsNullOrWhiteSpace <a href="#31691bd21ecc43abaabf314f637c7dab" title="permalink">#</a> </h3> <p> The first problem with this use of IsNullOrWhiteSpace is that it may give client programmers wrong messages. For example, if you pass the empty string ("") as <code>searchTerm</code>, you'll still get an ArgumentNullException. This is misleading, because it gives the wrong message: it states that <code>searchTerm</code> was null when it wasn't (it was ""). </p> <p> You may then argue that you could change the implementation to throw an ArgumentException. </p> <p> <pre><span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">string</span>.IsNullOrWhiteSpace(searchTerm)) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;Empty&nbsp;or&nbsp;null.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;searchTerm&quot;</span>);</pre> </p> <p> This isn't incorrect per se, but not as explicit as it could have been. In other words, it's not as helpful to the client developer as it could have been. While it may not seem like a big deal in a single method like this, it's sloppy code like this that eventually wear client developers down; it's death by a thousand paper cuts. </p> <p> Moreover, this implementation doesn't follow the <a href="http://en.wikipedia.org/wiki/Robustness_principle">Robustness Principle</a>. Is there any rational reason to reject white space strings? </p> <p> Actually, with a minor tweak, we can make the implementation work with white space as well. Consider these new test cases: </p> <p> <pre>[<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;&nbsp;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;&nbsp;&nbsp;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>)]</pre> </p> <p> These currently fail because of the use of IsNullOrWhiteSpace, but they ought to succeed. </p> <p> The correct implementation of the Canonicalize method is this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Canonicalize(<span style="color:blue;">string</span>&nbsp;searchTerm) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(searchTerm&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;searchTerm&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;searchTerm &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Split(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&#39;&nbsp;&#39;</span>&nbsp;},&nbsp;<span style="color:#2b91af;">StringSplitOptions</span>.RemoveEmptyEntries) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(x&nbsp;=&gt;&nbsp;x.ToUpper()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OrderBy(x&nbsp;=&gt;&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Aggregate(<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>&nbsp;+&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Trim(); }</pre> </p> <p> First of all, the correct Guard Clause is to test only for null; null is the only invalid value. Second, the method uses another overload of the Aggregate method where an initial seed (in this case "") is used to initialize the <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Fold</a> operation. Third, the final call to the Trim method ensures that there's no leading or trailing white space. </p> <h3 id="cab84d7021064908ae79cc10abce8e42"> The IsNullOrWhiteSpace mental model <a href="#cab84d7021064908ae79cc10abce8e42" title="permalink">#</a> </h3> <p> The overall problem with IsNullOrWhiteSpace and IsNullOrEmpty is that they give you the impression that null is equivalent to white space strings. This is the wrong mental model: white space strings are proper string values that you can very often manipulate just as well as any other string. </p> <p> If you insist on the mental model that white space strings are equivalent to null, you'll tend to put them in the same bucket of 'invalid' data. However, if you take a hard look at the <em>preconditions</em> for your classes, methods, or functions, you'll find that often, a white space string is going to be perfectly acceptable input. Why reject input you can understand? That will only make your code more difficult to use. </p> <p> In testing terms, it's my experience that null rarely falls in the same <a href="http://en.wikipedia.org/wiki/Equivalence_partitioning">Equivalence Class</a> as white space strings. Therefore, it's wrong to implicitly treat them as if they do. </p> <p> The IsNullOrWhiteSpace and IsNullOrEmpty methods imply that null and white space strings are equivalent, and this will often give you the wrong mental model of the boundary cases of your software. Be careful when using these methods. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="c27c7ce91ecf4e6db5d8f38652abf163"> <div class="comment-author"> <a href="http://serlock-works.blogspot.com">Fendy</a> <a href="#c27c7ce91ecf4e6db5d8f38652abf163">#</a></div> <div class="comment-content"> <p> I agree if this is used at code library, which will be used by other programmer. However when directly used at application level layer, it is common to use them, at least the IsNullOrEmpty one, and they are quite powerful. And I don't find any problem in using that. </p> </div> <div class="comment-date">2014-11-28 06:47 UTC</div> </div> <div class="comment" id="c27c7ce91ecf4e6db5d8f38652abf165"> <div class="comment-author"> <a href="https://stackoverflow.com/users/16391/stingyjack">StingyJack</a> <a href="#c27c7ce91ecf4e6db5d8f38652abf165">#</a></div> <div class="comment-content"> <h4>"Dog" means "DOG" means "dog"</h4> <p> I'm pretty sure you are hating ("hate-ing") on the wrong statement in this code, and have taken the idea past the point of practicality. </p> <pre> if (string.IsNullOrWhiteSpace(searchTerm)) //this is not the part that is wrong (in this context*) throw new ArgumentNullException("searchTerm"); //this is </pre> Its wrong for two (<i>or three</i>) reasons... <ul> <li> It is throwing an exception type that does not match the conditional statement that it comes from. The condition could be <strong>null</strong> or <strong>whitespace </strong>, but the exception is specific to <strong>null</strong> </li> <li> The message it tells the caller is misinforming the them about the problem </li> <li> <i>* The lack of bracing is kind of wrong too - its so easy to avoid potential bugs and uninteded side effects by just adding braces. More on this later</i> </li> </ul> <p> That code should look more like this, where the ex and message match the conditional, and aligns with both how humans (even programmers) understand written language and one of your versions of the function (but with braces =D) </p> <pre> if (string.IsNullOrWhiteSpace(searchTerm)) { throw new ArgumentException("A search term must be provided"); } </pre> <p> For most (if not all) human written languages, there is no semantic difference between words based on casing of letters. "Dog" means "DOG" means "dog". Likewise, for a function that accepts user input in order to arrange it for use in a search, there is no need to differentiate between values like <strong>null</strong>, "", " ", "\r\n\t", <strong>&lt;GS&gt;</strong>, and other unprintable whitespace characters. None of those inputs are acceptable except for very rare and specific use cases (not when the user is expected to provide some search criteria), so we only need to inform the caller correctly and not mislead them. </p> <p> It is definitely useful to have <strong>null</strong> and empty be different values so that we can represent "not known" or "not set" for datum, but that does not apply in this case as the user has set the value by providing the function's argument. And I get what you are trying to say about robustness but in most cases the end result is the same for the user - they get no results. Catching the bad inputs earlier would save the resources needed to execute a futile search and would avoid wasting the user's time waiting on search results for data entry that was most likely a mistake. </p> <h4>RE: Bracing</h4> If you object to the bracing being needed and do want to shorten the above further, use an extension method like this.. <pre> public static string NullIfWhiteSpace(this string value) { return string.IsNullOrWhiteSpace(value) ? null : value; } </pre> <p> ... then your function would could look like this... </p> <pre> public static string Canonicalize(string searchTerm) { _ = searchTerm.NullIfWhiteSpace() ?? throw new ArgumentException("A search term must be provided"); // etc... </pre> <p> This can be made into a Template (resharper) or snippet so you can have even less to type. </p> </div> <div class="comment-date">2023-04-23 17:04 UTC</div> </div> <div class="comment" id="b46c94c7646f4227a60248d38a3de831"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b46c94c7646f4227a60248d38a3de831">#</a></div> <div class="comment-content"> <p> StingyJack, thank you for writing. You bring up more than one point. I'll try to address them in order. </p> <p> You seem to infer from the example that the showcased <code>Canonicalize</code> function implements the <em>search</em> functionality. It doesn't. It canonicalises a search term. A question a library designer must consider is this: What is the contract of the <code>Canonicalize</code> function? What are the preconditions? What are the postconditions? Are there any invariants? </p> <p> The point that <a href="https://www.goodreads.com/review/show/5498011140">I found most interesting about <em>A Philosophy of Software Design</em></a> was to favour generality over specialisation. As I also hint at in my review of that book, I think that I identified a connection with <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> that isn't explicitly mentioned there. The upshot, in any case, is that it's a good idea to make a function like <code>Canonicalize</code> as robust as possible. </p> <p> When considering preconditions, then, what are the <em>minimum requirements</em> for it to be able to produce an output? </p> <p> It turns out that it can, indeed, canonicalise whitespace strings. Since this is possible, it would artificially constrain the function to disallow that. This seems redundant. </p> <p> When considering Postel's law, there's often a tension between allowing degenerate input and also allowing null input. It wouldn't be hard to handle a null <code>searchTerm</code> by also returning the empty string in that case. This would arguably be more robust, so why not do that? </p> <p> That's never easy to decide, and someone might be able to convince me that this would, in fact, be more appropriate. On the other hand, one might ask: <em>Is there a conceivable scenario where the search term is validly null?</em> </p> <p> In most code bases, I'd tend to consider null values as symptomatic of a bug in the calling code. If so, silently accepting null input might entail that defects go undetected. Thus, I tend to favour throwing exceptions on null input. </p> <p> The bottom line is that there's a fundamental difference between null strings and whitespace strings, just like there's a fundamental difference between null integers and zero integers. It's often dangerous to conflate the two. </p> <p> Regarding the bracing style, I'm well aware of the argument that omitting the braces can be dangerous. See e.g. the discussion on page 749 in <a href="/ref/code-complete">Code Complete</a>. </p> <p> Impressed by such arguments, I used to insist on always including curly braces even for one-liners, until one day, someone (I believe it was <a href="https://en.wikipedia.org/wiki/Robert_C._Martin">Robert C. Martin</a>) pointed out that these kinds of potential bugs are easily detected by unit tests. Since I routinely have test coverage of my code, I consider the extra braces redundant safety. If there was no cost to having them, I'd leave them, but in practice, they take up space. </p> </div> <div class="comment-date">2023-04-26 9:01 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Unit Testing methods with the same implementation https://blog.ploeh.dk/2014/10/09/unit-testing-methods-with-the-same-implemenation 2014-10-09T18:45:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you comprehensibly unit test two methods that are supposed to have the same behaviour?</em> </p> <p> Occasionally, you may find yourself in situations where you need to have two (or more) public methods with the same behaviour. How do you properly cover them with unit tests? </p> <p> The obvious answer is to copy and paste all the tests, but that leads to test duplication. People with a superficial knowledge of DAMP (Descriptive And Meaningful Phrases) would argue that test duplication isn't a problem, but it <em>is</em>, simply because it's more code that you need to maintain. </p> <p> Another approach may be to simply ignore one of those methods, effectively not testing it, but if you're writing <a href="/2012/12/18/RangersandZookeepers">Wildlife Software</a>, you have to have mechanisms in place that can protect you from introducing breaking changes. </p> <p> This article outlines one approach you can use if your unit testing framework supports <a href="http://xunitpatterns.com/Parameterized%20Test.html">Parameterized Tests</a>. </p> <h3 id="e5902beafaaf40298a0de41157c8a300"> Example problem <a href="#e5902beafaaf40298a0de41157c8a300" title="permalink">#</a> </h3> <p> Recently, I was working on a class with a complex AppendAsync method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&nbsp;AppendAsync(T&nbsp;@event) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Lots&nbsp;of&nbsp;stuff&nbsp;going&nbsp;on&nbsp;here...</span> }</pre> </p> <p> To incrementally define the behaviour of that AppendAsync method, I had used Test-Driven Development to implement it, ending with 13 test methods. None of those test methods were simple one-liners; they ranged from 3 to 18 statements, and averaged 8 statements per test. </p> <p> After having fully implemented the AppendAsync method, I wanted to make the containing class implement <a href="http://msdn.microsoft.com/en-us/library/dd783449.aspx">IObserver&lt;T&gt;</a>, with the OnNext method implemented like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnNext(T&nbsp;value) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.AppendAsync(value).Wait(); }</pre> </p> <p> That was my plan, but how could I provide appropriate coverage with unit tests of that OnNext implementation, given that I didn't want to copy and paste those 13 test methods? </p> <h3 id="ecd7fb0041c341a38b74316f327ab18a"> Parameterized Tests with varied behaviour <a href="#ecd7fb0041c341a38b74316f327ab18a" title="permalink">#</a> </h3> <p> In this code base, I was already using <a href="http://xunit.github.io">xUnit.net</a> with its nice support for Parameterized Tests; in fact, I was using <a href="http://www.nuget.org/packages/AutoFixture.Xunit">AutoFixture.Xunit</a> with attribute-based test input, so my tests looked like this: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">AutoAtomData</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;AppendAsyncFirstEventWritesPageBeforeIndex( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(As&nbsp;=&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ITypeResolver</span>))]<span style="color:#2b91af;">TestEventTypeResolver</span>&nbsp;dummyResolver, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(As&nbsp;=&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IContentSerializer</span>))]<span style="color:#2b91af;">XmlContentSerializer</span>&nbsp;dummySerializer, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(As&nbsp;=&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IAtomEventStorage</span>))]<span style="color:#2b91af;">SpyAtomEventStore</span>&nbsp;spyStore, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">AtomEventObserver</span>&lt;<span style="color:#2b91af;">XmlAttributedTestEventX</span>&gt;&nbsp;sut, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">XmlAttributedTestEventX</span>&nbsp;@event) { &nbsp;&nbsp;&nbsp;&nbsp;sut.AppendAsync(@event).Wait(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;feed&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">AtomFeed</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spyStore.ObservedArguments.Last()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(sut.Id,&nbsp;feed.Id); }</pre> </p> <p> This is the sort of test I needed to duplicate, but instead of calling <code>sut.AppendAsync(@event).Wait();</code> I needed to call <code>sut.OnNext(@event);</code>. The only variation in the test should be in the <a href="http://xunitpatterns.com/exercise%20SUT.html">exercise SUT</a> phase of the test. How can you do that, while maintaining the terseness of using attributes for defining test cases? The problem with using attributes is that you can only supply primitive values as input. </p> <p> First, I briefly experimented with applying the <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a> pattern, and while I got it working, I didn't like having to rely on inheritance. Instead, I devised this little, test-specific type hierarchy: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IAtomEventWriter</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;WriteTo(<span style="color:#2b91af;">AtomEventObserver</span>&lt;T&gt;&nbsp;observer,&nbsp;T&nbsp;@event); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">enum</span>&nbsp;<span style="color:#2b91af;">AtomEventWriteUsage</span> { &nbsp;&nbsp;&nbsp;&nbsp;AppendAsync&nbsp;=&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;OnNext } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AppendAsyncAtomEventWriter</span>&lt;T&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IAtomEventWriter</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;WriteTo(<span style="color:#2b91af;">AtomEventObserver</span>&lt;T&gt;&nbsp;observer,&nbsp;T&nbsp;@event) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;observer.AppendAsync(@event).Wait(); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">OnNextAtomEventWriter</span>&lt;T&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IAtomEventWriter</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;WriteTo(<span style="color:#2b91af;">AtomEventObserver</span>&lt;T&gt;&nbsp;observer,&nbsp;T&nbsp;@event) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;observer.OnNext(@event); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AtomEventWriterFactory</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IAtomEventWriter</span>&lt;T&gt;&nbsp;Create(<span style="color:#2b91af;">AtomEventWriteUsage</span>&nbsp;use) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">switch</span>&nbsp;(use) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#2b91af;">AtomEventWriteUsage</span>.AppendAsync: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AppendAsyncAtomEventWriter</span>&lt;T&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#2b91af;">AtomEventWriteUsage</span>.OnNext: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">OnNextAtomEventWriter</span>&lt;T&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">default</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentOutOfRangeException</span>(<span style="color:#a31515;">&quot;Unexpected&nbsp;value.&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is test-specific code that I added to my unit tests, so there's no test-induced damage here. These types are defined in the unit test library, and are not available in the SUT library at all. </p> <p> Notice that I defined an interface to abstract how to use the SUT for this particular test purpose. There are two small classes implementing that interface: one using AppendAsync, and one using OnNext. However, this small polymorphic type hierarchy can't be supplied from attributes, so I also added an enum, and a concrete factory to translate from the enum to the writer interface. </p> <p> This test-specific type system enabled me to refactor my unit tests to something like this: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineAutoAtomData</span>(<span style="color:#2b91af;">AtomEventWriteUsage</span>.AppendAsync)] [<span style="color:#2b91af;">InlineAutoAtomData</span>(<span style="color:#2b91af;">AtomEventWriteUsage</span>.OnNext)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;WriteFirstEventWritesPageBeforeIndex( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">AtomEventWriteUsage</span>&nbsp;usage, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(As&nbsp;=&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ITypeResolver</span>))]<span style="color:#2b91af;">TestEventTypeResolver</span>&nbsp;dummyResolver, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(As&nbsp;=&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IContentSerializer</span>))]<span style="color:#2b91af;">XmlContentSerializer</span>&nbsp;dummySerializer, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(As&nbsp;=&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IAtomEventStorage</span>))]<span style="color:#2b91af;">SpyAtomEventStore</span>&nbsp;spyStore, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">AtomEventWriterFactory</span>&lt;<span style="color:#2b91af;">XmlAttributedTestEventX</span>&gt;&nbsp;writerFactory, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">AtomEventObserver</span>&lt;<span style="color:#2b91af;">XmlAttributedTestEventX</span>&gt;&nbsp;sut, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">XmlAttributedTestEventX</span>&nbsp;@event) { &nbsp;&nbsp;&nbsp;&nbsp;writerFactory.Create(usage).WriteTo(sut,&nbsp;@event); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;feed&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">AtomFeed</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spyStore.ObservedArguments.Last()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(sut.Id,&nbsp;feed.Id); }</pre> </p> <p> Notice that this is the same test case as before, but because of the two occurrences of the [InlineAutoAtomData] attribute, each test method is being executed twice: once with AppendAsync, and once with OnNext. </p> <p> This gave me coverage, and protection against breaking changes of both methods, without making any test-induced damage. </p> <p> The entire source code in this article is available <a href="https://github.com/GreanTech/AtomEventStore">here</a>. The commit just before the refactoring is <a href="https://github.com/GreanTech/AtomEventStore/commit/9521a38e2f0d502ebca0be5177b555ea7b01c0ec">this one</a>, and the commit with the completed refactoring is <a href="https://github.com/GreanTech/AtomEventStore/commit/1d4f79cd56cdca7efdce070fcdb6e407adeb144f">this one</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="19e600d1ad1f4936813f4ab86b703f5e"> <div class="comment-author"><a href="http://twitter.com/ricmrodrigues">Ricardo Rodrigues</a> <a href="#19e600d1ad1f4936813f4ab86b703f5e">#</a></div> <div class="comment-content">I'm not familiar with xUnit, but does it now allow to use something along the lines of nUnit's TestCaseSource to provide the different implementations? From an OOD your implementation is flawless, but I just believe that for tests, simplicity is key so that readability is not affected and intent is instantly captured.</div> <div class="comment-date">2014-10-29 14:34 UTC</div> </div> <div class="comment" id="86c76e3222244adbbf102f0c4db903c2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#86c76e3222244adbbf102f0c4db903c2">#</a></div> <div class="comment-content"> <p> Ricardo, thank you for writing. xUnit.net's equivalent to [TestCaseSource] is to combine the [Theory] attribute with one of the built-in [PropertyData], [ClassData], etc. attributes. However, in this particular case, there's a couple of reasons that I didn't find those attractive: <ul> <li> When supplying test data in this way, you can write any code you'd like, but you have to supply values for <em>all</em> the parameters for <em>all</em> the test cases. In the above example, I wanted to vary the System Under Test (SUT), while I didn't care about varying any of the other parameters. The above approach enabled my to do so in a fairly DRY manner. </li> <li> In general, I don't like using [PropertyData], [ClassData] (or NUnit's [TestCaseSource]) unless I truly have a set of <em>reusable</em> test cases. Having a member or type encapsulating a set of test cases implies that these test cases are reusable - but they rarely are. </li> </ul> Perhaps a better approach would be to use something like <a href="https://github.com/GreanTech/Exude">Exude</a>, but while I wish such semantics were built into xUnit.net or NUnit directly, I didn't want to take a dependency on Exude only for a couple of tests. </p> </div> <div class="comment-date">2014-10-30 12:26 UTC</div> </div> <div class="comment" id="ae19ced9238941f6b4b944397da8d865"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#ae19ced9238941f6b4b944397da8d865">#</a></div> <div class="comment-content"> <p> That's an interesting take on test reuse. Exude also reminds me of F#'s Expecto. I'd be curious what you think of a few other reuse approaches. </p> <p> First, I've found <a href="https://github.com/haf/expecto">expecto's</a> composable tests lists make <a href="https://spencerfarley.com/2021/10/08/testapi-in-fsharp-revised/">reuse quite intuitive</a>. </p> <p> Second, some dev friends of mine have been contemplating test reuse. </p> <p> The root idea sourced from a friend thinking about the fragile test problem. <a href="https://codewithspoon.com/2019/12/stop-corrupting-yourself-test-against-abstractions/">He concluded that tests should be decoupled from the system</a>. Effectively, the test creates it's own anti-corruption layer which allows tests to stay clean even if the system is messy. This pushes tests to reflect requirements over implementations, which are much more stable. Initially skeptical, I've <a href="https://spencerfarley.com/2020/08/21/test-api-inpractice/">found significant value</a> in the approach. Systems with <a href="https://martinfowler.com/bliki/RoleInterface.html">role-based interfaces</a> (e.g. ports and adapters) can write one core test suite verifying behaviors for all implementations of that interface. </p> <p> I've been using this paradigm to map test reuse back to C#. The test list is a class with facts/theories and a constructor that takes the test list's anti-corruption abstraction as a constructor argument. Then I can make implementation-specific tests by inheriting the list and passing an implementation. The same could be done against a role interface instead of a test-specific abstraction if desired. </p> <p> Taking it even further, I've been wondering if such tests could practically <a href="https://spencerfarley.com/2021/10/31/efficient-inter-team-contracts-with-acceptance-tests/">act as acceptance tests between teams</a> as a lower cost alternative to gherkin. </p> <p> That ended up long. Any depth of feedback would be appreciated. </p> </div> <div class="comment-date">2022-02-22 02:26 UTC</div> </div> <div class="comment" id="c9f45b24cb5944e5934acd86921f4511"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c9f45b24cb5944e5934acd86921f4511">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. Tests as first-class values are certainly much to be preferred. That's <a href="/2018/05/07/inlined-hunit-test-lists">how I write parametrised tests in Haskell</a>, because <a href="https://hackage.haskell.org/package/HUnit">HUnit</a> models tests as values. I'm aware that Expecto does the same. </p> <p> I also agree with you that decoupling tests from implementation details is beneficial. People tend to think of this as only possible with Acceptance Tests or BDD-style tests, but I find that it's also <a href="/2021/05/03/structural-equality-for-better-tests">possible in the small</a>. In general, I've found property-based testing to be useful as a technique to add some distance between the test and the system under test. </p> <p> The idea about using Acceptance Tests between teams sounds like a technique that Ian Robinson calls <a href="https://martinfowler.com/articles/consumerDrivenContracts.html">Consumer-Driven Contracts</a>. </p> </div> <div class="comment-date">2022-02-25 14:45 UTC</div> </div> <div class="comment" id="4f05782279ce4e1097ade558fa3722cf"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#4f05782279ce4e1097ade558fa3722cf">#</a></div> <div class="comment-content"> <p> Thank you for your feedback. I'm glad you've also found value in tests as values and decoupling tests from implementations. </p> <p> Consumer-Driven Contracts does seem to share a lot in common with my idea for using implementation-decoupled tests as acceptance tests. Ian's idea starts from thinking about the provider and how it can manage message compatibility with consumers. Mine started from the consumer and testing multiple implementations of a port abstraction. Then, realizing those tests could also be reused by external consumers (extension contributors, upstream teams). </p> <p> I think the test-focused approach is able to partially mitigate a few of his concerns. First, the overall added work may be less because the tests already need to be defined for the consumer whether or not there is a complex externally-owned provider. Second, package systems have reached general adoption since his post and greatly improve coordination of shared code. Unit tests [can already be shared with a package manager](https://spencerfarley.com/2021/11/05/acceptance-test-logisitcs/), though there is definitely room for improvement. </p> <p> I'm a smidge sad because I thought I'd had an original idea, but I'm very glad to discover another author to follow. Thanks for connecting me! </p> </div> <div class="comment-date">2022-02-28 17:51 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AtomEventStore https://blog.ploeh.dk/2014/10/07/atomeventstore 2014-10-07T16:35:00+00:00 Mark Seemann <div id="post"> <p> <em>Announcing AtomEventStore: an open source, server-less .NET Event Store.</em> </p> <p> <a href="http://grean.com">Grean</a> has kindly open-sourced a library we've used internally for several projects: <a href="https://github.com/GreanTech/AtomEventStore">AtomEventStore</a>. It's pretty nice, if I may say so. </p> <p> It comes with quite a bit of <a href="https://github.com/GreanTech/AtomEventStore/wiki">documentation</a>, and is also <a href="https://www.nuget.org/packages/AtomEventStore">available on NuGet</a>. </p> <p> The original inspiration came from <a href="http://seabites.wordpress.com">Yves Reynhout</a>'s article <a href="http://bit.ly/AqearV">Your EventStream is a linked list</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Faking a continuously Polling Consumer with scheduled tasks https://blog.ploeh.dk/2014/09/25/faking-a-continuously-polling-consumer-with-scheduled-tasks 2014-09-25T06:33:00+00:00 Mark Seemann <div id="post"> <p> <em>How to (almost) continuously poll a queue when you can only run a task at discrete intervals.</em> </p> <p> In a previous article, I described <a href="/2014/04/30/single-writer-web-jobs-on-azure">how you set up Azure Web Jobs to run only a single instance of a background process</a>. However, the shortest time interval you can currently configure is to run a scheduled task every minute. If you want to use this trick to set up a <a href="http://www.eaipatterns.com/PollingConsumer.html">Polling Consumer</a>, it may seem limiting that you'll have to wait up to a minute before the scheduled task can pull new messages off the queue. </p> <h3 id="9d3514af81bc4f9aafdc0877d7164acf"> The problem <a href="#9d3514af81bc4f9aafdc0877d7164acf" title="permalink">#</a> </h3> <p> The problem is this: a Web Job is a command-line executable you can schedule. The most frequent schedule you can set up is once per minute. Thus, once per minute, the executable can start and query a queue in order to see if there are new messages since it last looked.</p> <p> If there are new messages, it can pull these messages off the queue and handle them one by one, until the queue is empty. </p> <p> If there are no new messages, the executable exits. It will then be up to a minute before it will run again. This means that if a message arrives just after the executable exits, it will sit in the queue up to a minute before being handled. </p> <p> At least, that's the naive implementation. </p> <h3 id="f23365c358354d9f88727108db6cdd14"> A more sophisticated approach <a href="#f23365c358354d9f88727108db6cdd14" title="permalink">#</a> </h3> <p> Instead of exiting immediately, what if the executable was to wait for a small period, and then check again? This means that you'd be able to increase the polling frequency to run much faster than once per minute. </p> <p> If the executable also keeps track of when it started, it can gracefully exit slightly before the minute is up, enabling the task scheduler to start a new process soon after. </p> <h3 id="9bb1798ab1d4445a915b6d0801bc95bf"> Example: a recursive F# implementation <a href="#9bb1798ab1d4445a915b6d0801bc95bf" title="permalink">#</a> </h3> <p> You can implement the above strategy in any language. Here's an F# version. The example code below is the main 'loop' of the program, but a few things have been put into place before that. Most of these are configuration values pulled from the configuration system: <ul> <li> <code>timeout</code> is a TimeSpan value that specifies for how long the executable should run before exiting. This value comes from the configuration system. Since the minimum schedule frequency for Azure Web Jobs is 1 minute, it makes sense to set this value to 1 minute. </li> <li> <code>stopBefore</code> is essentially <code>DateTimeOffset.Now + timeout</code>. </li> <li> <code>estimatedDuration</code> is a TimeSpan containing your (conservative) estimate of how long time it takes to handle a single message. It's only used if there are no messages to be handled in the queue, as the algorithm then has no statistics about the average execution time for each message. This value comes from the configuration system. In a recent system, I just arbitrarily set it to 2 seconds. </li> <li> <code>toleranceFactor</code> is a decimal used as a multiplier in order to produce a margin for when the executable should exit, so that it can exit before <code>stopBefore</code>, instead of idling for too long. This value comes from the configuration system. You'll have to experiment a bit with this value. When I originally deployed the code below, I had it set to <em>2</em>, but it seems like the Azure team changed how Web Jobs are initialized, so currently I have it set to <em>5</em>. </li> <li> <code>idleTime</code> is a TimeSpan that controls how long the executable should sit idly waiting, if there are no messages in the queue. This value comes from the configuration system. In a recent system, I set it to 5 seconds, which means that you may experience up to 5 seconds delay from a message arrives on an empty queue, until it's picked up and handled. </li> <li> <code>dequeue</code> is the function that actually pulls a message off the queue. It has the signature <code>unit&nbsp;->&nbsp;(unit&nbsp;->&nbsp;unit)&nbsp;option</code>. That looks pretty weird, but it means that it's a function that takes no input arguments. If there's a message in the queue, it returns a handler function, which is used to handle the message; otherwise, it returns None. </li> </ul> Obviously, there's quite a bit of code behind that dequeue function (it's actually the heart of the system), but exactly what it does, and how it does it, isn't particularly important in this context. If you want to see an example of how to implement a background process in F#, see my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">Pluralsight course on Functional Architecture with F#</a>. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">rec</span>&nbsp;handleUntilTimedOut&nbsp;(durations&nbsp;:&nbsp;<span style="color:#2b91af;">TimeSpan</span>&nbsp;<span style="color:#2b91af;">list</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;avgDuration&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;durations&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;[]&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;estimatedDuration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(durations&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">List</span>.sumBy&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Ticks))&nbsp;/ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(int64&nbsp;durations.Length) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;margin&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(decimal&nbsp;avgDuration.Ticks)&nbsp;*&nbsp;toleranceFactor &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;int64 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromTicks &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now&nbsp;+&nbsp;margin&nbsp;&lt;&nbsp;stopBefore&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;dequeue()&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some&nbsp;handle&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;before&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;after&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;duration&nbsp;=&nbsp;after&nbsp;-&nbsp;before &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;newDurations&nbsp;=&nbsp;duration&nbsp;::&nbsp;durations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handleUntilTimedOut&nbsp;newDurations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now&nbsp;+&nbsp;idleTime&nbsp;&lt;&nbsp;stopBefore&nbsp;<span style="color:blue;">then</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Async</span>.Sleep&nbsp;(int&nbsp;idleTime.TotalMilliseconds) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Async</span>.RunSynchronously &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handleUntilTimedOut&nbsp;durations handleUntilTimedOut&nbsp;[]</pre> </p> <p> As you can see, the first thing the handleUntilTimedOut function does is to attempt to calculate the average duration of handling a message. Calculating the average is easy enough if you have at least one observation, but if you have no observations at all, you can't calculate the average. This is the reason the algorithm needs the estimatedDuration to get started. </p> <p> Based on the the average (or estimated) duration, the algorithm next calculates a safety margin. This margin is intended to give the executable a chance to exit in time: if it can see that it's getting too close to the timeout, it'll exit instead of attempting to handle another message, since handling another message may take so much time that it may push it over the timeout limit. </p> <p> If it decides that, based on the margin, it still have time to handle a message, it'll attempt to dequeue a message. </p> <p> If there's a message, it'll measure the time it takes to handle the message, and then append the measured duration to the list of already observed durations, and recursively call itself. </p> <p> If there's no message in the queue, the algorithm will idle for the configured period, and then call itself recursively. </p> <p> Finally, the <code>handleUntilTimedOut []</code> expression kicks off the polling cycle with an empty list of observed durations. </p> <h3 id="7f077b5307084deb8d9a3896f7c88752"> Observations <a href="#7f077b5307084deb8d9a3896f7c88752" title="permalink">#</a> </h3> <p> When appropriately configured, a typical Azure Web Job log sequence looks like this: <ul> <li>1 minute ago (55 seconds running time)</li> <li>2 minutes ago (55 seconds running time)</li> <li>3 minutes ago (56 seconds running time)</li> <li>4 minutes ago (55 seconds running time)</li> <li>5 minutes ago (56 seconds running time)</li> <li>6 minutes ago (58 seconds running time)</li> <li>7 minutes ago (55 seconds running time)</li> <li>8 minutes ago (55 seconds running time)</li> <li>9 minutes ago (58 seconds running time)</li> <li>10 minutes ago (56 seconds running time)</li> </ul> Notice that all Web Jobs complete within 1 minute, leaving time for the next scheduled job to start. In all of those 55ish seconds, the job can continuously pull messages of the queue, if any are present. </p> <h3 id="60915c2883064546843554f2898d8ea5"> Summary <a href="#60915c2883064546843554f2898d8ea5" title="permalink">#</a> </h3> <p> If you have a situation where you need to run a scheduled job (as opposed to a continuously running service or daemon), but you want it to behave like it was a continuously running service, you can make it wait until just before a new scheduled job is about to start, and then exit. This is an approach you can use anywhere you find yourself in that situation - not only on Azure. </p> <p> <strong>Update 2015 August 11:</strong> See my <a href="/2015/08/10/type-driven-development">article about Type Driven Development</a> for an example of how to approach this problem in a well-designed, systematic, iterative fashion. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="6d945c302d9f406f823926d86d5a8869"> <div class="comment-author"> <a href="http://blog.nikosbaxevanis.com">Nikos Baxevanis</a> <a href="#6d945c302d9f406f823926d86d5a8869">#</a></div> <div class="comment-content"> <p>Indeed, this technique really shines when the task runs at discrete intervals and the actual job takes <em>less</em> time than each interval.</p> <p>For example, if a job takes 20 seconds to complete, then inside a single task of 1 minute it's possible to run <em>2</em> jobs, using the technique described in this article. – This is <em>very</em> nice!</p> <p><em>FWIW, <a href="https://skillsmatter.com/skillscasts/6289-type-driven-development">this</a> talk and <a href="/2014/12/17/good-times-with-f/">this</a> post seem to be related with the technique shown in this article.</em></p> <p>OTOH, in the context of Windows Services, or similar, it's possible to run a task at even smaller intervals than 1 minute.</p> <p>In this case, the trick is to measure how much time it takes to run the actual job. – Then, schedule the task to run in any period <em>less</em> than the measured time.</p> <hr> <p>As an example, if a job takes 1 seconds to complete, schedule the task to run every 0.5 seconds on a background thread while blocking the foreground thread:</p> <pre><code>open System.Threading open System.Threading.Tasks module Scheduler = let run job (period : TimeSpan) = // Start let cancellationTokenSource = new CancellationTokenSource() (new Task(fun () -&gt; let token = cancellationTokenSource.Token while not token.IsCancellationRequested do job() token.WaitHandle.WaitOne period |&gt; ignore)).Start() // Loop Console.WriteLine "Scheduler running. Type \"quit\" or \"exit\" to stop." let stop = let line = Console.ReadLine().ToUpperInvariant() line = "QUIT" || line = "EXIT" while not stop do () // Stop cancellationTokenSource.Cancel() module Main = let job () = Console.Write "[." Async.Sleep 1000 |&gt; Async.RunSynchronously Console.WriteLine ".] - OK" Scheduler.run job (TimeSpan.FromSeconds 0.5) </code></pre> </div> <div class="comment-date">2015-05-18 05:57 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Decommissioning Decoraptors https://blog.ploeh.dk/2014/08/25/decommissioning-decoraptors 2014-08-25T10:08:00+00:00 Mark Seemann <div id="post"> <p> <em>How to release objects from within a Decoraptor.</em> </p> <p> In my article about the <a href="/2014/08/24/decoraptor">Decoraptor</a> design pattern, I deliberately ignored the question of decommissioning: what if the short-lived object created inside of the Decoraptor contains one or more <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx">disposable</a> objects? </p> <p> A Decoraptor creates instances of object graphs, and such object graphs may contain disposable objects. These disposable objects may be deeply buried inside of the object graph, and the root object itself may not implement IDisposable. </p> <p> This is a know problem, and the solution is known as well: the object that composes the object graph is the only object with enough knowledge to dispose of any disposable objects within the object graph that it created, so an Abstract Factory should also be able to release objects again: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;Create(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Release(T&nbsp;item); }</pre> </p> <p> This is similar to advice I've already given in conjunction with designing <a href="/2014/05/19/di-friendly-framework">DI-Friendly frameworks</a>. </p> <h3 id="b75e19d2610f4df998585d01698ff017"> Example: refactored Decoraptor <a href="#b75e19d2610f4df998585d01698ff017" title="permalink">#</a> </h3> <p> With the new version of the IFactory&lt;T&gt; interface, you can refactor the Observeraptor class from the <a href="/2014/08/24/decoraptor">Decoraptor article</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Observeraptor</span>&lt;T&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IObserver</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IObserver</span>&lt;T&gt;&gt;&nbsp;factory; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Observeraptor(<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IObserver</span>&lt;T&gt;&gt;&nbsp;factory) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.factory&nbsp;=&nbsp;factory; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnCompleted() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;obs&nbsp;=&nbsp;<span style="color:blue;">this</span>.factory.Create(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obs.OnCompleted(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.factory.Release(obs); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnError(<span style="color:#2b91af;">Exception</span>&nbsp;error) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;obs&nbsp;=&nbsp;<span style="color:blue;">this</span>.factory.Create(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obs.OnError(error); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.factory.Release(obs); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnNext(T&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;obs&nbsp;=&nbsp;<span style="color:blue;">this</span>.factory.Create(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obs.OnNext(value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.factory.Release(obs); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As you can see, each method implementation now performs three steps: <ol> <li>Use the factory to create an instance of IObserver&lt;T&gt;</li> <li>Delegate the method call to the new instance</li> <li>Release the short-lived instance</li> </ol> This informs the factory that the client is done with the instance, and it can let it go out of scope, as well as dispose of any disposable objects that object graph may contain. </p> <p> Some people prefer letting the factory return something that implements IDisposable, so that the Decoraptor can employ a <code>using</code> statement in order to manage the lifetime of the returned object. However, IObserver&lt;T&gt; itself doesn't derive from IDisposable, so if you want to do something like that, you'd need to invent some sort of Scope&lt;T&gt; class, which would implement IDisposable; that's another alternative. </p> <h3 id="cc91ac086e514eefa75596ab41841422"> Example: refactored factory <a href="#cc91ac086e514eefa75596ab41841422" title="permalink">#</a> </h3> <p> Refactoring the Decoraptor itself to take decommissioning into account is fairly easy, but you also need to implement the Release method on any factory implementations. Furthermore, the factory should be thread-safe, because often, the entire point of introducing a Decoraptor in the first place is to deal with Services that aren't thread-safe. </p> <p> Implementing a thread-safe Composer isn't too difficult, but certainly increases the complexity of it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SqlMeterFactory</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConcurrentDictionary</span>&lt;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;,&nbsp;<span style="color:#2b91af;">MeteringContext</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;observers; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SqlMeterFactory() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.observers&nbsp;=&nbsp;<span style="color:blue;">new</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConcurrentDictionary</span>&lt;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;,&nbsp;<span style="color:#2b91af;">MeteringContext</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;&nbsp;Create() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ctx&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MeteringContext</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;obs&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlMeter</span>(ctx); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.observers[obs]&nbsp;=&nbsp;ctx; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;obs; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Release(<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;&nbsp;item) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">MeteringContext</span>&nbsp;ctx; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:blue;">this</span>.observers.TryRemove(item,&nbsp;<span style="color:blue;">out</span>&nbsp;ctx)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctx.Dispose(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Fortunately, instead of having to handle the complexity of a thread-safe implementation, you can delegate that part to a ConcurrentDictionary. </p> <h3 id="a00897cb55a642ec88a8c903f45eb023"> Disposable graphs <a href="#a00897cb55a642ec88a8c903f45eb023" title="permalink">#</a> </h3> <p> The astute reader may have been wondering about this: shouldn't SqlMeter implement IDisposable? And if so, wouldn't it be easier to simply dispose of it directly, instead of having to deal with a dictionary of root objects as keys, and disposable objects as values? </p> <p> The short answer is that, in this particular example, this would indeed have been a more correct, as well as a simpler, solution. Consider the SqlMeter class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SqlMeter</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">MeteringContext</span>&nbsp;ctx; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SqlMeter(<span style="color:#2b91af;">MeteringContext</span>&nbsp;ctx) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.ctx&nbsp;=&nbsp;ctx; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnNext(<span style="color:#2b91af;">MeterRecord</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.ctx.Records.Add(value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.ctx.SaveChanges(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnCompleted()&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnError(<span style="color:#2b91af;">Exception</span>&nbsp;error)&nbsp;{&nbsp;} }</pre> </p> <p> Since SqlMeter has a class field of the type MeteringContext, and MeteringContext, via deriving from <a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.aspx">DbContext</a>, is a disposable object, SqlMeter itself also ought to implement IDisposable. </p> <p> Yes, indeed, it should, and the only reason I didn't do that was for the sake of the example. If SqlMeter had implemented IDisposable, you'd be tempted to implement SqlMeterFactory.Release by simply attempting to downcast the incoming <code>item</code> from IObserver&lt;MeterRecord&gt; to IDisposable, and then, if the cast succeeds, invoke its Dispose method. </p> <p> However, the only reason we know that SqlMeter ought to implement IDisposable is because it has a <a href="/2012/08/31/ConcreteDependencies">concrete dependency</a>. Concrete types can implement IDisposable, but interfaces should not, since IDisposable is an implementation detail. (I do realize that some interfaces defined by the .NET Base Class Library derives from IDisposable, but I consider those Leaky Abstractions. As Nicholas Blumhardt <a href="https://cca.codeplex.com/discussions/82987">put it</a>: "an interface [...] generally shouldn't be disposable. There's no way for the one defining an interface to foresee all possible implementations of it - you can always come up with a disposable implementation of practically any interface.") </p> <p> The point of this discussion is that as soon as a class has an abstract dependency (as opposed to a concrete dependency), the class may or may not be relying on something that is disposable. That's not enough to warrant the class itself to implement the IDisposable interface. For this reason, when an Abstract Factory creates an object graph, the root object of that graph should rarely implement IDisposable, and that's the reason it's not enough to attempt a downcast. </p> <p> This was the reason that I intentionally didn't implement IDisposable on SqlMeter, even though I should have: to demonstrate how a Release method should deal with object graphs where the disposable objects are deeply buried as leaf nodes in the graph. A Release method may not always be able to traverse the graph, so this is the reason that the factory must also have the decommissioning responsibility: only the Composer knows what it composed, so only it knows how to safely dismantle the graph again. </p> <h3 id="7087f50fc8a445d8b0909cd5f6d0ffd0"> Summary <a href="#7087f50fc8a445d8b0909cd5f6d0ffd0" title="permalink">#</a> </h3> <p> Throwing IDisposable into the mix always makes things much more complicated, so if you can, try to implement your classes in such a way that they aren't disposable. This is often possible, but if you can't avoid it, you must add a Release method to your Abstract Factory and handle the additional complexity of decommissioning object graphs. </p> <p> Some (but not all!) DI Containers already do this, so that may be one <a href="/2012/11/06/WhentouseaDIContainer">reason to use a DI Container</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Decoraptor https://blog.ploeh.dk/2014/08/24/decoraptor 2014-08-24T13:04:00+00:00 Mark Seemann <div id="post"> <p> <em>A Dependency Injection design pattern</em> </p> <p> This article describes a Dependency Injection design pattern called Decoraptor. It can be used as a solution to the problem: </p> <p class="text-center"> <em>How can you address lifetime mismatches without introducing a Leaky Abstraction?</em> </p> <p class="text-center"> <strong>By adding a combination of a Decorator and Adapter between the Client and its Service.</strong> </p> <p class="text-center"> <img src="/content/binary/decoraptor.png" alt="Decoraptor"> </p> <p> Sometimes, when a Client wishes to talk to a Service (<em>Component</em> in the figure; in practice often an interface), the Client may have a long lifetime, whereas, because of technical constraints in the implementation, the Service must only have a short lifetime. If not addressed, this may lead to a <a href="/2014/06/02/captive-dependency">Captive Dependency</a>: the long-lived Client holds onto the Service for too long. </p> <p> One solution to this is a <em>Decoraptor</em> - a combination of a <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> and an <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a>. A Decoraptor is a long-lived object that adapts an <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a>, and uses the factory to create a short-lived instance of the interface that it, itself, implements. </p> <h3 id="8fe1f37237ae4c73942664f6c821cbc1"> How it works <a href="#8fe1f37237ae4c73942664f6c821cbc1" title="permalink">#</a> </h3> <p> A Decoraptor is an Adapter that looks almost like a Decorator. It implements an interface (<em>Component</em> in the above figure) by adapting an Abstract Factory. The factory creates other instances of the implemented interface, so the Decoraptor implements each operation defined by the interface by <ol> <li>Invoking the factory to create a new instance of the same interface</li> <li>Delegating the operation to the new instance</li> </ol> This enables the Decoraptor to resolve the lifetime mismatch between a long-lived Client and a short-lived Service. The Decoraptor itself is as long-lived as its Client, but ensures that each Service instance is as short-lived as a single operation. </p> <p> While not strictly a Decorator, a Decoraptor is closely related because it ultimately delegates all implementation to another implementation of the interface it, itself, implements. The only purpose of a Decoraptor is to match two otherwise incompatible lifetimes. </p> <h3 id="cd4e656d1c9c4f43a4342aeb44492d39"> When to use it <a href="#cd4e656d1c9c4f43a4342aeb44492d39" title="permalink">#</a> </h3> <p> While a Decoraptor is a fairly simple piece of infrastructure code, it still adds complexity to a code base, so it should only be used if necessary. The simplest solution to the Captive Dependency problem is to align the Client's lifetime to the Service's lifetime; in other words, make the Client's lifetime as short as the Service's lifetime. This doesn't require a Decoraptor: it only requires you to compose the Client/Service object graph every time you want to create a new instance of the Service. </p> <p> Another alternative to a Decoraptor is to consider whether it's possible to refactor the Service so that it can have a longer lifetime. Often, the reason why a Service should have a short life span is because it isn't thread-safe. Sometimes, making a Service thread-safe isn't that difficult; in such cases, this may be a better solution (but sometimes, making something thread-safe is extremely difficult, in which case Decoraptor may be a much simpler solution). </p> <p> Still, there may be cases where creating a new Client every time you want to use it isn't desirable, or even possible. The most <em>common</em> concern is often performance, although in my experience, that's rarely a real problem. More substantial is a concern that sometimes, due to technical constraints, it isn't possible to make the Client shorter-lived. In these cases, a Decoraptor is a good solution. </p> <p> A Decoraptor is a better alternative to <a href="http://stackoverflow.com/a/4648343/126014">injecting an Abstract Factory directly into the Client</a>, because doing that is a Leaky Abstraction. The Client is programming against an interface, and, according to the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>, <blockquote> <p> "clients [...] own the abstract interfaces" </p> <p> - <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices</a>, chapter 11 </p> </blockquote> Therefore, it would be a Leaky Abstraction if the Client were to define the interface based on the requirements of a particular implementation of that interface. A Decoraptor is a better alternative, because it enables the Client to define the interface it <em>needs</em>, and the Service to implement the interface as it <em>can</em>, and the Decoraptor's <em>single responsibility</em> is to make those two ends meet. </p> <h3 id="6de2416065174140a10574cb97891861"> Implementation details <a href="#6de2416065174140a10574cb97891861" title="permalink">#</a> </h3> <p> Let the Client define the interface it <em>needs</em>, without taking lifetime issues into consideration. Implement the interface in a separate class, again disregarding specific lifetime issues. </p> <p> Define an Abstract Factory that creates new instances of the interface required by the Client. Create a new class that implements the interface, but takes the factory as a dependency. This is the Decoraptor. In the Decoraptor, implement each method by invoking the factory to create the short-lived instance of the interface, and delegate the method implementation to that instance. </p> <p> Create a class that implements the Abstract Factory by creating new instances of the short-lived Service. </p> <p> Inject the factory into the Decoraptor, and inject the Decoraptor into the long-lived Client. The Decoraptor has the same lifetime as the Client, but each Service instance only exists for the duration of a single method call. </p> <h3 id="0046e8633e96473ebc856a8f236d1f73"> Motivating example <a href="#0046e8633e96473ebc856a8f236d1f73" title="permalink">#</a> </h3> <p> When using <a href="/2014/06/13/passive-attributes">Passive Attributes</a> with <a href="http://www.asp.net/web-api">ASP.NET Web API</a>, the Filter containing all the behaviour must be added to the overall collection of Filters: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;filter&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MeteringFilter</span>(observer); config.Filters.Add(filter);</pre> </p> <p> Due to the way ASP.NET Web API works, this configuration happens during Application_Start, so there's only going to be a single instance of MeteringFilter around. In other work, the MeteringFilter is a long-lived Client; it effectively has Singleton lifetime scope. </p> <p> MeteringFilter depends on IObserver&lt;MeterRecord&gt; (See the article about <a href="/2014/06/13/passive-attributes">Passive Attributes</a> for the full code of MeteringFilter). The observer injected into the MeteringFilter object will have the same lifetime as the MeteringFilter object. The Filter will be used from concurrent threads, and while MeteringFilter itself is thread-safe (it has immutable state), the injected observer may not be. </p> <p> To be more concrete, <a href="http://stackoverflow.com/q/25433608/126014">this question</a> was recently posted on Stack Overflow: <blockquote> "The filter I'm implementing has a dependency on a repository, which itself has a dependency on a custom DbContext. [...] I'm not sure how to implement this, while taking advantage of the DI container's lifetime management capabilities (so that a new DbContext will be used per request)." </blockquote> It sounds like the DbContext in question is probably an <a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.aspx">Entity Framework DbContext</a>, which makes sense: last time I looked, DbContext wasn't thread-safe. Imagine that the IObserver&lt;MeterRecord&gt; implementation looks like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SqlMeter</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">MeteringContext</span>&nbsp;ctx; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SqlMeter(<span style="color:#2b91af;">MeteringContext</span>&nbsp;ctx) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.ctx&nbsp;=&nbsp;ctx; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnNext(<span style="color:#2b91af;">MeterRecord</span>&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.ctx.Records.Add(value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.ctx.SaveChanges(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnCompleted()&nbsp;{&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnError(<span style="color:#2b91af;">Exception</span>&nbsp;error)&nbsp;{&nbsp;} }</pre> </p> <p> The SqlMeter class here takes the place of the Service in the more abstract language used above. In the Stack Overflow question, it sounds like there's a <a href="http://martinfowler.com/eaaCatalog/repository.html">Repository</a> between the Client and the DbContext, but I think this example captures the essence of the problem. </p> <p> This is exactly the issue outlined above. Due to technical constraints, the MeteringFilter <em>must</em> be a Singleton, while (again due to technical constraints) each Observer <em>must</em> be either Transient or scoped to each request. </p> <h3 id="841dff54be5b43f39d457c2e3c687748"> Refactoring notes <a href="#841dff54be5b43f39d457c2e3c687748" title="permalink">#</a> </h3> <p> In order to resolve a problem like the example above, you can introduce a Decoraptor, which sits between the Client (MeteringFilter) and the Service (SqlMeter). The Decoraptor relies on an Abstract Factory to create new instances of SqlMeter: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;Create(); }</pre> </p> <p> In this case, the Abstract Factory is a generic interface, but it could also be a non-generic interface that only creates instances of IObserver&lt;MeterRecord&gt;. Some people prefer delegates instead, so would use e.g. Func&lt;IObserver&lt;MeterRecord&gt;&gt;. You could also define an Abstract Base Class instead of an interface, if that's more to your liking. </p> <p> This particular example also disregards decommissioning concerns. Effectively, the Abstract Factory will create instances of SqlMeter, but since these instances simply go out of scope, the contained MeteringContext isn't being disposed of in a deterministic manner. In <a href="/2014/08/25/decommissioning-decoraptors">a future article</a>, I will remedy this situation. </p> <h3 id="5aef5d78f64b4a3ea54163d12e1a2d11"> Example: generic Decoraptor for Observers <a href="#5aef5d78f64b4a3ea54163d12e1a2d11" title="permalink">#</a> </h3> <p> In order to resolve the lifetime mismatch between MeteringFilter and SqlMeter you can introduce a generic Decoraptor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Observeraptor</span>&lt;T&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IObserver</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IObserver</span>&lt;T&gt;&gt;&nbsp;factory; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Observeraptor(<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IObserver</span>&lt;T&gt;&gt;&nbsp;factory) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.factory&nbsp;=&nbsp;factory; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnCompleted() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.factory.Create().OnCompleted(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnError(<span style="color:#2b91af;">Exception</span>&nbsp;error) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.factory.Create().OnError(error); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;OnNext(T&nbsp;value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.factory.Create().OnNext(value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that Observeraptor&lt;T&gt; can adapt any IObserver&lt;T&gt; because it depends on the generic IFactory&lt;IObserver&lt;T&gt;&gt; interface. In each method defined by IObserver&lt;T&gt;, it first uses the factory to create an instance of a short-lived IObserver&lt;T&gt;, and then delegates the implementation to that object. </p> <p> Since MeteringFilter depends on IObserver&lt;MeterRecord&gt;, you also need an implementation of IFactory&lt;IObserver&lt;MeterRecord&gt;&gt; in order to compose the MeteringFilter object graph: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SqlMeterFactory</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;&nbsp;Create() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlMeter</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MeteringContext</span>()); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation simply creates a new instance of SqlMeter and MeteringContext every time the Create method is invoked; hardly surprising, I should hope. </p> <p> You now have all building blocks to correctly compose MeteringFilter in Application_Start: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;factory&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlMeterFactory</span>(); <span style="color:blue;">var</span>&nbsp;observer&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Observeraptor</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;(factory); <span style="color:blue;">var</span>&nbsp;filter&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MeteringFilter</span>(observer); config.Filters.Add(filter);</pre> </p> <p> Because SqlMeterFactory creates new instances of SqlMeter every time MeteringFilter invokes IObserver&lt;MeterRecord&gt;.OnNext, the lifetime requirements of the DbContext are satisfied. Moreover, MeteringFilter only depends on IObserver&lt;MeterRecord&gt;, so is perfectly shielded from that implementation detail, so no Leaky Abstraction was introduced. </p> <h3 id="a84c54b861584132be2bac6d520e7854"> Known examples <a href="#a84c54b861584132be2bac6d520e7854" title="permalink">#</a> </h3> <p> Questions related to the issue of lifetime mismatches are regularly being asked on Stack Overflow, and I've had a bit of success recommending Decoraptor as a solution, although at that time, I had yet to come up with the name: <ul> <li><a href="http://stackoverflow.com/a/4650050/126014">Dependency Injection - new instance required in several of a classes methods</a></li> <li><a href="http://stackoverflow.com/a/22854525/126014">IOC DI Multi-Threaded Lifecycle Scoping in Background Tasks</a></li> </ul> In addition, I've also <a href="/2010/01/20/EnablingDIforLazyComponents">previously touched upon the subject</a> in relation to using a Virtual Proxy for lazy dependencies. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c0b2e0bd501a4d5c8407c60bf11bff69"> <div class="comment-author"><a href="https://github.com/antmdvs">Tony Davis</a> <a href="#c0b2e0bd501a4d5c8407c60bf11bff69">#</a></div> <div class="comment-content"> <p> Hi Mark! I'm implementing a global filter in ASP.NET WebApi 2, which depends on a service that in turn requires access to the current request's <code>HttpContext</code>. Would a decoraptor-like pattern around <code>HttpContext.Current</code> be appropriate for this or is that asking for trouble with regards to thread-safety or other potential gotchas? Thank you! </p> <p> I put together <a href="https://gist.github.com/antmdvs/867206f620d24fb282d409124577fc8f#file-usage-cs-L1">this Gist</a> to show what I'm after. </p> </div> <div class="comment-date">2020-02-17 18:45 UTC</div> </div> <div class="comment" id="7249754c663d451ea9772141603dece1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7249754c663d451ea9772141603dece1">#</a></div> <div class="comment-content"> <p> Tony, thank you for writing. I always managed to stay away from <code>HttpContext.Current</code>. I don't like the Ambient Context design, and I don't trust that I can reason about how it actually works with concurrent requests. If you're writing a filter, can't you get what you need from the <code>HttpActionContext</code> that's passed as an argument to <code>IActionFilter.ExecuteActionFilterAsync</code>? </p> <p> I don't know exactly what it is that you need from <code>HttpContext</code>, and I'm also not sure that I'm looking at the correct version of documentation for ASP.NET Web API, so I could be missing the point here. </p> </div> <div class="comment-date">2020-02-17 20:15 UTC</div> </div> <div class="comment" id="c0b2e0bd502a4d5c8407c60bf11bff69"> <div class="comment-author"><a href="https://github.com/antmdvs">Tony Davis</a> <a href="#c0b2e0bd502a4d5c8407c60bf11bff69">#</a></div> <div class="comment-content"> <p>Mark, thanks for the quick reply. The service in question is meant to be responsible for plucking off certain properties out of the Request and mapping them to a context object used in downstream processing of <code>OnActionExecutionAsync</code>. It is meant to be an extension point for users of the filter. Anyway, I was striving to make <code>HttpContextBase</code> <a href="https://gist.github.com/antmdvs/867206f620d24fb282d409124577fc8f#file-usage-cs-L4">an explicit constructor dependency</a> of the service. Needless to say, I couldn't do what you're proposing <a href="https://gist.github.com/antmdvs/867206f620d24fb282d409124577fc8f#file-usage-global-asax-cs">in the composition root</a>.</p> <p>However, instead of constructor injection, I suppose I could float the context from the filter's <code>ActionExecutingContext.HttpContext</code>, into an instance method of the service like <code>_myService.MapRequestInfo(ActionExecutingContext.HttpContext)</code>. This appears to be what ASP.NET Core does for custom middleware components: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-3.1#use-httpcontext-from-middleware.</p><p>Would you favor this approach or do you think there might be a design smell going on?</p></div> <div class="comment-date">2020-02-17 22:14 UTC</div> </div> <div class="comment" id="1333c55a0f0446529eb211729302aafd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1333c55a0f0446529eb211729302aafd">#</a></div> <div class="comment-content"> <p> Tony, it sounds like you're designing a model for middleware. It's not clear to me why you're doing this; filters are already middleware. </p> <p> As I wrote, I'm in the dark. It's been years since I worked with ASP.NET Web API 2, and I'm not even sure that <a href="http://msdn.microsoft.com/en-us/library/system.web.http.filters.iactionfilter.aspx">I'm looking at the right API documentation</a>. Unless I'm looking at the wrong version of the API, though, doesn't <code>ExecuteActionFilterAsync</code> provide the objects you need? For example, you can get the request object from <code>actionContext.Request</code>. </p> </div> <div class="comment-date">2020-02-18 6:05 UTC</div> </div> <div class="comment" id="c0b2e0bd591a4d5c8407c60bf11bff69"> <div class="comment-author"><a href="https://github.com/antmdvs">Tony Davis</a> <a href="#c0b2e0bd591a4d5c8407c60bf11bff69">#</a></div> <div class="comment-content"> <p>Mark, thanks for your input.</p> <p>Yes, you are correct about having access to the request via <code>actionContext.Request</code> (though the Headers property doesn't seem mockable for unit testing) -- I'm multi-targeting netcore and netfx and mistakenly pasted the code for the wrong framework in my previous comment. Sorry for the confusion.</p> <p>I guess it *is* kind of like middleware for my filter, where each middleware contributes data that the filter aggregates as a dictionary and sends off to a SaaS service as the context for evaluating a feature flag. The thing is, this is for an abstraction over the vendor's SDK and developers who use this abstraction need the flexibility to add whatever KVPs are necessary for the evaluation of any given feature flag being introduced. The idea is, if a developer is implementing toggle point code for a new feature flag that requires some contextual info, they can create and register a new service/"middleware" that provides that contextual info. I added <a href="https://gist.github.com/antmdvs/867206f620d24fb282d409124577fc8f#file-featureacontextprovider-cs">an example class</a> to the Gist for clarity.</p> <p>Also, the flag context might not ALWAYS be derived from the HTTP request alone; <a href="https://gist.github.com/antmdvs/867206f620d24fb282d409124577fc8f#file-featurebcontextprovider-cs-L5">it could come from configuration info or some other source I suppose</a>. Hopefully, that sheds more light on the subject.</p> </div> <div class="comment-date">2020-02-18 17:02 UTC</div> </div> <div class="comment" id="731f42900c4c411b9faf45c636e1b5f1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#731f42900c4c411b9faf45c636e1b5f1">#</a></div> <div class="comment-content"> <p> Tony, it's still not quite clear to me what you're trying to do (e.g. what's <code>MyCustomFilter</code>?), but if you truly have a class that depends on <code>HttpContextBase</code>, I think that you're supposed to use <a href="https://docs.microsoft.com/dotnet/api/system.web.httpcontextwrapper">HttpContextWrapper</a>. </p> </div> <div class="comment-date">2020-02-18 17:29 UTC</div> </div> <div class="comment" id="731f42900c4c411b9faf46c636e1b5f1"> <div class="comment-author"><a href="https://github.com/antmdvs">Tony Davis</a> <a href="#731f42900c4c411b9faf46c636e1b5f1">#</a></div> <div class="comment-content"> <p> Agreed. <code>HttpContextWrapper</code> is <a href="https://gist.github.com/antmdvs/867206f620d24fb282d409124577fc8f#file-usage-global-asax-cs-L2">in the decoraptor's factory method.</a> :) </p> <p> Apologies. I renamed <code>MyCustomFilter</code> to <code>FeatureGateFilter</code> -- it's inspired by <a href="https://github.com/microsoft/FeatureManagement-Dotnet/blob/master/src/Microsoft.FeatureManagement.AspNetCore/FeatureGateAttribute.cs">Microsoft.FeatureManagement</a> but I'm using the <a href="https://blog.ploeh.dk/2014/06/13/passive-attributes/">passive attributes</a> pattern so this is the guy who does the work of locating [FeatureGate] attributes, calling the feature context providers, and finally talking to the SaaS service (passing the accumulated context along for evaluation). </p> <p> In any case, I get your point about avoiding HttpContext. I may look to pass the <code>Request</code> into the <code>GetContext</code> method (method injection?) instead of constructor injection for that reason, though <a href="https://gist.github.com/antmdvs/867206f620d24fb282d409124577fc8f#file-featurebcontextprovider-cs">some of the context providers may not use it</a>. </p> </div> <div class="comment-date">2020-02-18 18:00 UTC</div> </div> <div class="comment" id="43de668b9eaf40a78fe41c884cafb8a0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#43de668b9eaf40a78fe41c884cafb8a0">#</a></div> <div class="comment-content"> <p> Tony, what I meant with <code>HttpContextWrapper</code> was that it already represents the abstraction you need (<code>HttpContextBase</code>). Why would you need a Decoraptor around it? </p> </div> <div class="comment-date">2020-02-18 20:44 UTC</div> </div> <div class="comment" id="731f42900c5c411b9faf46c636e1b5f1"> <div class="comment-author"><a href="https://github.com/antmdvs">Tony Davis</a> <a href="#731f42900c5c411b9faf46c636e1b5f1">#</a></div> <div class="comment-content"> <p> Oh, the answer is because <code>HttpContextWrapper</code> doesn't behave reliably with a singleton lifetime and it can't be anything else (even when registered with a transient lifetime in a DI container) because it always gets promoted to a singleton due to the object graph being added to the global filters collection. Unlike ASP.NET Core, which supports type activation of filters via <code>FilterCollection.Add&lt;TFilterType&gt;()</code> and friends, it seems ASP.NET MVC 5 filters can only be added by instance, effectively making the entire dependency graph singleton, as you pointed out in the "Motivating Example" section above. </p> <p> For thoroughness, what I meant by "doesn't behave reliably" is that each property access, <code>httpContextWrapper.Request</code> for example, yields a new object instance (which seems promising), but its nested property values, say <code>httpContextWrapper.Request.Headers</code>, are referentially equal to the instances of their initial evaluation. Therefore, I also observed that <code>httpContextWrapper.Request.Path</code> is always <code>"/"</code> assuming the initial request was for the root, regardless of the actual URLs associated with successive requests. (At least these were my obversations. 🤷.) So that's how I landed here on decoraptor. </p> </div> <div class="comment-date">2020-02-18 23:17 UTC</div> </div> <div class="comment" id="09c28d183fed47c9b01ef24fd93efc7c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#09c28d183fed47c9b01ef24fd93efc7c">#</a></div> <div class="comment-content"> <p> Tony, if a Decoraptor around <code>HttpContextBase</code> addresses your problem, then by all means go for it 🙂 </p> </div> <div class="comment-date">2020-02-19 8:04 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. CQS versus server generated IDs https://blog.ploeh.dk/2014/08/11/cqs-versus-server-generated-ids 2014-08-11T19:40:00+00:00 Mark Seemann <div id="post"> <p> <em>How can you both follow Command Query Separation and assign unique IDs to Entities when you save them? This post examines some options.</em> </p> <p> In my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID</a> Pluralsight course, I explain why <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> (CQS) is an important component of Encapsulation. When first exposed to CQS, many people start to look for edge cases, so a common question is something like this: <blockquote> What would you do if you had a service that saves to a database and returns the ID (which is set by the database)? The Save operation is a Command, but if it returns void, how do I get the ID? </blockquote> This is actually an exercise I've given participants when I've given the course as a workshop, so before you read my proposed solutions, consider taking a moment to see if you can come up with a solution yourself. </p> <h3 id="3f97f73518a54378965837bdbbc0d3a9"> Typical, CQS-violating design <a href="#3f97f73518a54378965837bdbbc0d3a9" title="permalink">#</a> </h3> <p> The problem is that in many code bases, you occasionally need to save a brand-new Entity to persistent storage. Since the object is an Entity (as defined in <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>), it must have a unique ID. If your system is a concurrent system, you can't just pick a number and expect it to be unique... or can you? </p> <p> Instead, many programmers rely on their underlying database to add a unique ID to the Entity when it saves it, and then return the ID to the client. Accordingly, they introduce designs like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRepository</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;Create(T&nbsp;item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;other&nbsp;members</span> }</pre> </p> <p> The problem with this design, obviously, is that it violates CQS. The Create method ought to be a Command, but it returns a value. From the perspective of Encapsulation, this is problematic, because if we look only at the method signature, we could be tricked into believing that since it returns a value, the Create method is a Query, and therefore has no side-effects - which it clearly has. That makes it difficult to reason about the code. </p> <p> It's also a leaky abstraction, because most developers or architects implicitly rely on a database implementation to provide the ID. What if an implementation doesn't have the ability to come up with a unique ID? This sounds like a <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> violation just waiting to happen! </p> <h3 id="d95d9b6941594aff8b100757eaddf732"> The easiest solution <a href="#d95d9b6941594aff8b100757eaddf732" title="permalink">#</a> </h3> <p> When I wrote "you can't just pick a number and expect it to be unique", I almost gave away the easiest solution to this problem: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRepository</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Create(<span style="color:#2b91af;">Guid</span>&nbsp;id,&nbsp;T&nbsp;item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;other&nbsp;members</span> }</pre> </p> <p> Just change the ID from an integer to a GUID (or UUID, if you prefer). This <em>does</em> enable clients to come up with a unique ID for the Entity - after all, that's the whole point of GUIDs. </p> <p> Notice how, in this alternative design, the Create method returns void, making it a proper Command. From an encapsulation perspective, this is important because it enables you to immediately identify that this is a method with side-effects - just by looking at the method signature. </p> <p> But wait: what if you <em>do</em> need the ID to be an integer? That's not a problem; there's also a solution for that. </p> <h3 id="a3e2a969a331467897153c1dbbecf71e"> A solution with integer IDs <a href="#a3e2a969a331467897153c1dbbecf71e" title="permalink">#</a> </h3> <p> You may not like the easy solution above, but it <em>is</em> a good solution that fits in a wide variety in situations. Still, there can be valid reasons that using a GUID isn't an acceptable solution: <ul> <li> Some databases (e.g. SQL Server) don't particularly like if you use GUIDs as table keys. You can, but integer keys often perform better, and they are also shorter. </li> <li> Sometimes, the Entity ID isn't only for internal use. Once, I worked on a customer support ticket system, and my suggestion of using a GUID as an ID wasn't met with enthusiasm. When a customer calls on the phone about an existing support case, it isn't reasonable to ask him or her to read an entire GUID aloud; it's simply too error-prone. </li> </ul> In some cases, you just need some human-readable integers as IDs. What can you do? </p> <p> Here's my modified solution: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRepository</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Create(<span style="color:#2b91af;">Guid</span>&nbsp;id,&nbsp;T&nbsp;item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;GetHumanReadableId(<span style="color:#2b91af;">Guid</span>&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;other&nbsp;members</span> }</pre> </p> <p> Notice that the Create method is the same as before. The client must still supply a GUID for the Entity, and that GUID may or may not be the 'real' ID of the Entity. However, in addition to the GUID, the system also associates a (human-readable) integer ID with the Entity. Which one is the 'real' ID isn't particularly important; the important part is that there's <em>another</em> method you can use to get the integer ID associated with the GUID: the GetHumanReadableId method. </p> <p> The Create method is a Command, which is only proper, because creating (or saving) the Entity mutates the state of the system. The GetHumanReadableId method, on the other hand, is a Query: you can call it as many times as you like: it doesn't change the state of the system. </p> <p> If you want to store your Entities in a database, you can still do that. When you save the Entity, you save it with the GUID, but you can also let the database engine assign an integer ID; it might even be a monotonically increasing ID (1, 2, 3, 4, etc.). At the same time, you could have a secondary index on the GUID. </p> <p> When a client invokes the GetHumanReadableId method, the database can use the secondary index on the GUID to quickly find the Entity and return its integer ID. </p> <h3 id="35e605d8fee9493ba3f2be53747802fa"> Performance <a href="#35e605d8fee9493ba3f2be53747802fa" title="permalink">#</a> </h3> <p> "But," you're likely to say, "I don't like that! It's bad for performance!" </p> <p> Perhaps. My first impulse is to <a href="http://c2.com/cgi/wiki?PrematureOptimization">quote Donald Knuth</a> at you, but in the end, I may have to yield that my proposed design may result in two out-of-process calls instead of one. Still, I never promised that my solution wouldn't involve a trade-off. Most software design decisions involve trade-offs, and so does this one. You gain better encapsulation for potentially worse performance. </p> <p> Still, the performance drawback may not involve <em>problems</em> as such. First, while having to make two round trips to the database may perform worse than a single one, it may still be fast enough. Second, even if you need to know the (human-readable) integer ID, you <em>may not need to know it when you create it</em>. Sometimes you can get away with saving the Entity in one action, and then you may only need to know what the ID is seconds or minutes later. In this case, separating reads and writes may actually turn out to be an advantage. </p> <h3 id="f53b34ca8e4742a19b78eb4d72f7369b"> Must all code be designed like this? <a href="#f53b34ca8e4742a19b78eb4d72f7369b" title="permalink">#</a> </h3> <p> Even if you disregard your concerns about performance, you may find this design overly complex, and more difficult to use. Do I really recommend that <em>all</em> code should be designed like that? </p> <p> No, I don't recommend that <em>all</em> code should be designed according to the CQS principle. As Martin Fowler <a href="http://martinfowler.com/bliki/CommandQuerySeparation.html">points out</a>, in <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a>, Bertrand Meyer explains how to design an API for a Stack with CQS. It involves a Pop Command, and a Top Query: in order to use it, you would first have to invoke the Top Query to get the item on the top of the stack, and then subsequently invoke the Pop Command to modify the state of the stack. </p> <p> One of the problems with such a design is that it isn't thread-safe. It's also more unwieldy to use than e.g. the standard <a href="http://msdn.microsoft.com/en-us/library/9say334k.aspx">Stack&lt;T&gt;.Pop</a> method. </p> <p> My point with all of this isn't to dictate to you that you should <em>always</em> follow CQS. The point is that you can always come up with a design that does. </p> <p> In my career, I've met many programmers who write a poorly designed class or interface, and then put a little <a href="http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode">apology</a> on top of it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRepository</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;CQS&nbsp;is&nbsp;impossible&nbsp;here,&nbsp;because&nbsp;we&nbsp;need&nbsp;the&nbsp;ID</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;Create(T&nbsp;item); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;other&nbsp;members</span> }</pre> </p> <p> Many times, we don't know of a better way to do things, but it doesn't mean that such a way doesn't exist; we just haven't learned about it yet. In this article, I've shown you how to solve a seemingly impossible design challenge with CQS. </p> <p> Knowing that even a design problem such as this <em>can</em> be solved with CQS should put you in a better position to make an <em>informed</em> decision. You may still decide to violate CQS and define a Create method that returns an integer, but if you've done that, it's because you've weighed the alternatives; not because you thought it impossible. </p> <h3 id="17ff00b34a9644dea4cb292e8cf85f55"> Summary <a href="#17ff00b34a9644dea4cb292e8cf85f55" title="permalink">#</a> </h3> <p> It's possible to apply CQS to most problems. As always, there are trade-offs involved, but knowing how to apply CQS, even if, at first glance, it seems impossible, is an important design skill. </p> <p> Personally, I tend to adhere closely to CQS when I do Object-Oriented Design, but I also, occasionally, decide to break that principle. When I do, I do so realising that I'm making a trade-off, and I don't do it lightly. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="601039fd761c49c5be0f23707ef82955"> <div class="comment-author"><a href="http://www.tonyabell.com">Tony Abell</a> <a href="#601039fd761c49c5be0f23707ef82955">#</a></div> <div class="comment-content"> How would you model a REST End Point, for example: <br><br> POST /api/users <br><br> This should return the Location of the created url. Would you suggest, the location be the status of the create user command. <br><br> GET /api/users/createtoken/abc123 <br><br> UI Developers do not like this pattern. It creates extra work for them. </div> <div class="comment-date">2014-08-12 4:24 UTC</div> </div> <div class="comment" id="1ba94a4d1aa34e7b893966306cd9abcc"> <div class="comment-author">Steve Byrne <a href="#1ba94a4d1aa34e7b893966306cd9abcc">#</a></div> <div class="comment-content"> Would raising an event of some kind from the Repository be a valid solution in this case?<br><br> (Bear in mind I'm pretty ignorant when it comes to CQRS but I figured that it would get around not being able to return anything directly from a command) <br><br>But then again I suppose you'd still need the GetHumanReadableId method for later on... </div> <div class="comment-date">2014-08-14 00:18 UTC</div> </div> <div class="comment" id="38e51d071833421897f0b8cd508a923a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#38e51d071833421897f0b8cd508a923a">#</a></div> <div class="comment-content"> <p> Tony, thank you for writing. What you suggest is one way to model it. Essentially, if a client starts with: </p> <p> <pre>POST /api/users</pre> </p> <p> One option for a response is, as you suggest, something like: </p> <p> <pre>HTTP/1.1 202 Accepted Location: /api/status/1234</pre> </p> <p> A client can poll on that URL to get the status of the task. When the resource is ready, it will include a link to the user resource that was finally created. This is essentially a workflow just like the RESTbucks example in <a href="http://amzn.to/18aNOla">REST in Practice</a>. You'll also see an example of this approach in my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">Pluralsight course on Functional Architecture</a>, and yes, this makes it more complicated to implement the client. One example is the <a href="https://github.com/ploeh/booking-web-ui">publicly available sample client code</a> for that course. </p> <p> While it puts an extra burden on the client developer, it's a very robust implementation because it's based on asynchronous workflows, which not only makes it horizontally scalable, but also makes it much easier to implement robust occasionally connected clients. </p> <p> However, this isn't the only RESTful option. Here's an alternative. The request is the same as before: </p> <p> <pre>POST /api/users</pre> </p> <p> But now, the response is instead: </p> <p> <pre>HTTP/1.1 201 Created Location: /api/users/8765 { "name" : "Mark Seemann" }</pre> </p> <p> Notice that this immediately creates the resource, as well as returns a representation of it in the response (consider the JSON in the example a placeholder). This should be easier on the poor UI Developers :) </p> <p> In any case, this discussion is entirely orthogonal to CQS, because <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">at the boundaries, applications aren't Object-Oriented</a> - thus, CQS doesn't apply to REST API design. Both POST, PUT, and DELETE verbs imply the intention to modify a resource's state, yet the HTTP specification describes how such requests should return proper responses. These verbs violate CQS because they both involve state mutation and returning a response. </p> </div> <div class="comment-date">2014-08-15 17:57 UTC</div> </div> <div class="comment" id="45f6bd29680746f089a252ccab8e1122"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#45f6bd29680746f089a252ccab8e1122">#</a></div> <div class="comment-content"> <p> Steve, thank you for writing. Yes, raising an event from a Command is definitely valid, since a Command is all about side-effects, and an event is also a side effect. The corollary of this is that you can't raise events from Queries if you want to adhere to CQS. In <a href="https://blog.ploeh.dk/encapsulation-and-solid">my Pluralsight course on encapsulation</a>, I briefly touch on this subject in the <em>Encapsulation</em> module (module 2), in the clip called <em>Queries</em>, at 1:52. </p> <p> (BTW, in this article, and the course as well, I'm discussing CQS (<a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a>), not CQRS (Command Query Responsibility Segregation). Although these ideas are related, it's important to realise that they aren't synonymous. CQS dates back to the 1980s, while CQRS is a more recent invention, from the 2000s - the earliest <em>dated</em> publication that's easy to find is <a href="http://codebetter.com/gregyoung/2010/02/16/cqrs-task-based-uis-event-sourcing-agh/">from 2010</a>, but it discusses CQRS as though people already know what it is, so the idea must be a bit older than that.) </p> </div> <div class="comment-date">2014-08-16 7:37 UTC</div> </div> <div class="comment" id="e6627a42d0454367a4fb618a6c5562f1"> <div class="comment-author">Kenny Pflug <a href="#e6627a42d0454367a4fb618a6c5562f1">#</a></div> <div class="comment-content"> <p>Mark, thank you for this post and sorry for my late response, but I've got some questions about it.</p> <p>First of all, I would not have designed the repository the way you showed at the beginning of your post - instead I would have opted for something like this:</p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRepository</span>&lt;T&gt; { T CreateEntity(); <span style="color:blue;">void</span>&nbsp;AttachEntity(T entity); }</pre> <p>The <span style="font-family:Consolas">CreateEntity</span> method behaves like a factory in that it creates a new instance of the specified entity without an ID assigned (the entity is also not attached to the repository). The <span style="font-family:Consolas">AttachEntity</span> method takes an existing entity and assigns a valid ID to it. After a call to this method, the client can get the new ID by using the corresponding <span style="font-family:Consolas;color:blue">get</span> method on the entity. This way the repository acts like a service facade as it provides creational, querying and persistance services to the client for a single type of entities.</p> <p>What do you think about this? From my point of view, the <span style="font-family:Consolas">AttachEntity</span> method is clearly a command, but what about the <span style="font-family:Consolas">CreateEntity</span> method? I would say it is a query because it does not change the state of the underlying repository when it is called - but is this really the case? If not, could we say that factories always violate CQS? What about a <span style="font-family:Consolas">CreateEntity</span> implementation that attaches the newly created entity to the repository?</p> </div> <div class="comment-date">2014-08-25 7:55 UTC</div> </div> <div class="comment" id="682723a87e7048f8a9eaaa4131b0f2ce"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#682723a87e7048f8a9eaaa4131b0f2ce">#</a></div> <div class="comment-content"> <p> Kenny, thank you for writing. FWIW, I wouldn't have designed a Repository like I show in the beginning of the article either, but I've seen lots of Repositories designed like that. </p> <p> Your proposed solution looks like it respects CQS as well, so that's another alternative to my suggested solutions. Based on your suggested method signatures, at least, CreateEntity looks like a Query. If a factory only creates a new object and returns it, it doesn't change the observable state of the system (no class fields mutated, no files were written to disk, no bytes were sent over the network, etc.). </p> <p> If, on the other hand, the CreateEntity method also attaches the newly created entity to the repository, then it would violate CQS, because <em>that</em> would change the observable state of the system. </p> <p> Your design isn't too far from the one I suggest in this very article - I just don't include the CreateEntity method in my interface definition, because I consider the creation of an Entity (in memory) to be a separate concern from being able to persist it. </p> <p> Then, if you omit the CreateEntity method, you'll see that your AttachEntity method plays the same role is my Create method; the only difference is that I keep the ID separate from the Entity, while you have the ID as a writable property on the Entity. Instead of invoking a separate Query (GetHumanReadableId) to figure out what the server-generated ID is, you invoke a Query attached to the Entity (its ID property). </p> <p> The reason I didn't chose this design is because it messes with the invariants of the Entity. If you consider an Entity as defined in <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>, an Entity is an object that has a long-lasting identity. This is most often implemented by giving the Entity an Id property; I do that too, but make sure that the id is mandatory by requiring clients to supply it through the constructor. As I explain in <a href="https://blog.ploeh.dk/encapsulation-and-solid">my Pluralsight course about encapsulation</a>, a very important part about encapsulation is to make sure that it's impossible (or at least difficult) to put an object into an invalid state. If you allow an Entity to be put into a state where it has no ID, I would consider that an invalid state. </p> </div> <div class="comment-date">2014-08-26 15:10 UTC</div> </div> <div class="comment" id="1f57272d0e6e4e528095788be3ec0c7f"> <div class="comment-author">Kenny Pflug <a href="#1f57272d0e6e4e528095788be3ec0c7f">#</a></div> <div class="comment-content"> <p>Thank you for your comprehensive answer, Mark. I also like the idea of injecting the ID of an entity directly into its constructor - but I haven't actually applied this 'pattern' in my projects yet. Although it's a bit off-topic, I have to ask: the ID injection would result in something like this:</p> <pre><span style="color:blue">public abstract class</span> <span style="color:#2b91af;">Entity</span>&lt;T&gt; { <span style="color:blue">private readonly</span> T _id; <span style="color:blue">protected</span> <span style="color:#2b91af;">Entity</span>(T id) { _id = id; } <span style="color:blue">public</span> T ID { <span style="color:blue">get</span> { <span style="color:blue">return</span> _id; } } <span style="color:blue">public override int</span> GetHashCode() { <span style="color:blue">return</span> _id.GetHashCode(); } <span style="color:blue">public override bool</span> Equals(object other) { <span style="color:blue">var</span> otherEntity = other as <span style="color:#2b91af;">Entity</span>; <span style="color:blue">if</span> (otherEntity == <span style="color:blue">null</span>) <span style="color:blue">return false</span>; <span style="color:blue">return</span> _id == otherEntity._id; } }</pre> <p>I know it is not perfect, so please think about it as a template.</p> <p>Anyway, I prefer this design but often this results in unnecessarily complicated code, e.g. when the user is able to create an entity object temporarily in a dialog and he or she can choose whether to add it or discard of it at the end of the dialog. In this case I would use a DTO / View Model etc. that only exists because I cannot assign a valid ID to an entity yet.</p> <p>Do you have a pattern that circumvents this problem? E.g. clone an existing entity with an temporary / invalid ID and assign a valid one to it (which I would find very reasonable)?</p> </div> <div class="comment-date">2014-08-26 22:30 UTC</div> </div> <div class="comment" id="518d961032204c10b806f58c8c3a62f7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#518d961032204c10b806f58c8c3a62f7">#</a></div> <div class="comment-content"> <p> Kenny, thank you for writing. Yes, you <em>could</em> model it like that Entity&lt;T&gt; class, although there are various different concerns mixed here. Whether or not you want to override Equals and GetHashCode is independent of the discussion about protecting invariants. This may already be clear to you, but I just wanted to get that out of the way. </p> <p> The best answer to your question, then, is to make it simpler, if at all possible. The first question you should ask yourself is: can the ID really be <em>any</em> type? Can you have an Entity&lt;object&gt;? Entity&lt;Random&gt;? Entity&lt;UriBuilder&gt;? That seems strange to me, and probably not what you had in mind. </p> <p> The simplest solution to the problem you pose is simply to settle on GUIDs as IDs. That would make an Entity look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Foo</span><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Guid</span>&nbsp;id;<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Foo(<span style="color:#2b91af;">Guid</span>&nbsp;id)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;==&nbsp;<span style="color:#2b91af;">Guid</span>.Empty)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;Empty&nbsp;GUIDs&nbsp;not&nbsp;allowed.&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;id&quot;</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.id&nbsp;=&nbsp;id;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Guid</span>&nbsp;Id<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.id;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;other&nbsp;members&nbsp;go&nbsp;here...</span><br>}</pre> </p> <p> Obviously, this makes the simplification that the ID is a GUID, which makes it easy to create a 'temporary' instance if you need to do that. You'll also note that it protects its invariants by guarding against the empty GUID. </p> <p> (Although not particularly relevant for this discussion, you'll also notice that I made this a concrete class. In general, I favour composition over inheritance, and I see no reason to introduce an abstract base class only in order to take and expose an ID - <a href="/2014/08/07/why-dry">such code is unlikely to change much</a>. There's no behaviour here because I also don't override Equals or GetHashCode, because I've come to realise that doing so with the implementation you suggest is counter-productive... but that, again, is another discussion.) </p> <p> Then what if you can't use a GUID. Well, you could still resort to the solution that I outline above, and <em>still</em> use a GUID, and then have another Query that can give you an integer that corresponds to the GUID. </p> <p> However, ultimately, this isn't how I tend to architect my systems these days. As <a href="http://goodenoughsoftware.net">Greg Young</a> has pointed out in a white paper draft (sadly) no longer available on the internet, you can't really expect to make Distributed Domain-Driven Design work with the 'classic' n-layer architecture. However, once you separate reads and writes (AKA CQRS) you no longer have the sort of problem you ask about, because your write model should be modelling commands and events, instead of Entities. FWIW, in my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">Functional Architecture with F# Pluralsight course</a>, I also touch on some of those concepts. </p> </div> <div class="comment-date">2014-08-29 17:37 UTC</div> </div> <div class="comment" id="7c1a93c731c14d08bd5e18f21775b9b4"> <div class="comment-author">Vladimir <a href="#7c1a93c731c14d08bd5e18f21775b9b4">#</a></div> <div class="comment-content"> <p> It's a great topic. There's another approach used for this problem in some ORMs, particularly in NHibernate: Hi/Lo algorithm. </p> <p> It is possible to preload a batch of IDs and distribute them among new objects. I've written a blog post about it: <a href="http://enterprisecraftsmanship.com/2014/11/15/cqs-with-database-generated-ids/">link</a> </p> </div> <div class="comment-date">2014-11-15 14:52 UTC</div> </div> <div class="comment" id="ff2fa54be8dd4ceabc995b0bb083ba95"> <div class="comment-author">Irfan Charania <a href="#ff2fa54be8dd4ceabc995b0bb083ba95">#</a></div> <div class="comment-content"> <p>In the proposed solution that uses both integer IDs and Guids, would you expect child tables to use GUIDs as foreign keys also? </p> <p>If the aggregate is passed in its entirety into a stored procedure, then scope_identity() can come into play. When a merge/replication scenario is not of concern, then you would be able to make use of integer IDs everywhere internally. </p> </div> <div class="comment-date">2017-10-13 3:51 UTC</div> </div> <div class="comment" id="405ab29ce4bb41ea90fdf10725a72425"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#405ab29ce4bb41ea90fdf10725a72425">#</a></div> <div class="comment-content"> <p> Irfan, thank you for writing. To the degree you choose to use GUIDs as described in this article, it's in order to obey the CQS principle. Thus, it's entirely a concern of the client(s). How you relate tables inside of a relational database is an implementation detail. </p> <p> If by 'aggregate' you mean an <em>Aggregate Root</em> as described in <a href="http://amzn.to/WBCwx7">DDD</a>, then the root is the root of a tree. Translated to typical relational database design, it'd be a row in a table. By definition, any children of such a root don't have individual identity, so I don't see how it'd make sense to associate a GUID with the children; you'd never query them individually, because if you do, then the Aggregate Root is no longer an Aggregate Root. </p> <p> How you associate the children with the root is an implementation detail. Again, in typical, normalised database design, you'd typically find children via foreign key relationship. Since this is an implementation detail, you can use any key type you'd like for that purpose; most likely, that'll be some sort of integer. </p> </div> <div class="comment-date">2017-10-14 8:06 UTC</div> </div> <div class="comment" id="84b8f1a295f643118a01b7fd0a45dc1b"> <div class="comment-author">Ben Bedinghaus <a href="#84b8f1a295f643118a01b7fd0a45dc1b">#</a></div> <div class="comment-content"> <p> Hi Mark. I noticed in a comment to one of Kenny's questions you said you would not design a repository the way it was originally designed in this example. How would you design a repository layer? I've always found this type of repository design to not quite feel right but see it all over the place. <br> Thanks, <br> Ben </p> </div> <div class="comment-date">2018-06-05 16:12 UTC</div> </div> <div class="comment" id="2f8cdd1eaf0c4697aeabd8c0a6325ab7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2f8cdd1eaf0c4697aeabd8c0a6325ab7">#</a></div> <div class="comment-content"> <p> Ben, thank you for writing. To be clear, the Repository pattern is one of the most misunderstood and misrepresented design patterns that I can think of. It was originally described in <a href="https://martinfowler.com">Martin Fowler's</a> book <a href="http://bit.ly/patternsofeaa">Patterns of Enterprise Application Architecture</a>, but if you look up the pattern description there, it has nothing to do with how people normally use it. It's a prime example of design patterns being ambiguous and open not only to interpretation, but to semantic shift. </p> <p> Regardless of the interpretation, I don't use the Repository design pattern. Following the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>, "clients […] own the abstract interfaces" (<a href="http://amzn.to/19W4JHk">APPP</a>, chapter 11), so if I need to, say, read some data from a database, I define a data reader interface in the client library. <a href="/2014/03/10/solid-the-next-step-is-functional">Following other SOLID principles as well, I'd typically end up with most interfaces having only a single member</a>. These days, though, if I do Dependency Injection at all, I mostly <a href="/2017/01/30/partial-application-is-dependency-injection">use partial application of higher-order functions</a>. </p> </div> <div class="comment-date">2018-06-06 6:32 UTC</div> </div> <div class="comment" id="30bb5f13f69346daa473ec3637b4f01e"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#30bb5f13f69346daa473ec3637b4f01e">#</a></div> <div class="comment-content"> <p> Mark, when you get to choose the database design, do most of your tables include both a (supplied) <code>Guid</code> ID and a (database-generated) <code>int</code> ID? </p> </div> <div class="comment-date">2020-12-03 01:22 UTC</div> </div> <div class="comment" id="d2db67591fec400a8f0cd7cec41c3a62"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d2db67591fec400a8f0cd7cec41c3a62">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. There are a couple of options. </p> <p> You could just use a GUID as the table key. DBAs typically dislike that option because, if you use that as the table's clustered index, it'll force the database to physically rearrange the table every time you insert a row (since GUIDs aren't, in general, increasing). Notice, however, that this argument is entirely concerned with performance. Using a GUID as a table key has no impact on database design in general. It is, after all, just an integer. </p> <p> Thus, if the table in question is likely to remain small (i.e. have few rows) and with only the occasional insert, I see no reason to have the standard <code>INT IDENTITY</code> column. </p> <p> For busier tables, you can have both columns. For example, the code base I'm currently working with defines its <code>Reservations</code> table like this: </p> <p> <pre><span style="color:blue;">CREATE</span>&nbsp;<span style="color:blue;">TABLE</span>&nbsp;[dbo]<span style="color:gray;">.</span>[Reservations]<span style="color:blue;">&nbsp;</span><span style="color:gray;">(</span> &nbsp;&nbsp;&nbsp;&nbsp;[Id]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">IDENTITY</span><span style="color:gray;">,</span> &nbsp;&nbsp;&nbsp;&nbsp;[At]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">DATETIME2&nbsp;</span><span style="color:gray;">(</span>7<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Name]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">NVARCHAR&nbsp;</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Email]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">NVARCHAR&nbsp;</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[Quantity]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL,</span> &nbsp;&nbsp;&nbsp;&nbsp;[PublicId]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">UNIQUEIDENTIFIER</span>&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span>&nbsp;<span style="color:blue;">UNIQUE</span><span style="color:gray;">,</span> &nbsp;&nbsp;&nbsp;&nbsp;[RestaurantId]&nbsp;<span style="color:blue;">INT</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:gray;">NOT</span>&nbsp;<span style="color:gray;">NULL</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">PRIMARY</span>&nbsp;<span style="color:blue;">KEY</span>&nbsp;<span style="color:blue;">CLUSTERED&nbsp;</span><span style="color:gray;">(</span>[Id]&nbsp;<span style="color:blue;">ASC</span><span style="color:gray;">)</span> <span style="color:gray;">)</span></pre> </p> <p> The <code>Id</code> column exclusively exists to keep the database happy. The code that interacts with the database never uses it; it uses <code>PublicId</code>. </p> <p> To be honest, I used to have a decent grasp on database design, SQL Server, and related performance considerations, but it's about a decade back. I'm sure you could produce some cases where such a design would be detrimental, but on the other hand, I believe that the majority of code out there isn't even near the extremes where you need to compromise on the design. </p> </div> <div class="comment-date">2020-12-03 7:27 UTC</div> </div> <div class="comment" id="29385868c075445bb75c08a368edaec6"> <div class="comment-author">Zach Robinson <a href="#29385868c075445bb75c08a368edaec6">#</a></div> <div class="comment-content"> <p> Hello, Mark! Thank you for the encyclopedia of knowledge and experience you have shared and continue to share with us through this blog and your books! </p> <p> I've recently read <a href="https://www.amazon.com/Domain-Modeling-Made-Functional-Domain-Driven/dp/1680502549">Domain Modeling Made Functional by Scott Wlaschin</a> and am currently reading your new book, <a href="https://www.amazon.com/Code-That-Fits-Your-Head/dp/0137464401">Code That Fits in Your Head</a>. I've taken quite a liking to the idea that not just entities, but also events should be modeled within a domain. In addition to common events such as queries/commands, any expected error or success cases can also be wrapped in a type. This manner of extensive typing seems to help improve the "What you see is all there is" factor when learning/understanding a code base. Given that we have some expected events that occur when interacting with a database through commands and queries, it might make sense to encapsulate that domain model in the return types of these methods. </p> <p> The query portion is straightforward. As you've discussed in your book, we can use <code>Maybe&lt;T&gt;</code> to indicate a successful or unsuccessful query. We might also be so crazy as to use an <code>Either&lt;T, TException&gt;</code> to propagate some exception message&sol;data to be logged&sol;returned at the bread-end of our impure&sol;pure workflow. </p> <p> Would it violate CQS to do the same for commands? We're doing a side effect on the database, no question. We're also probably returning some data that is relevant to the incoming command parameter within the resulting message from either the IOSuccess or IOFailure. A pattern I've been using for a web API project is something similar to the following: </p> <p> <pre> <span style="color:blue;">public sealed</span> record IOSuccess(string Value); <span style="color:blue;">public abstract</span> record Failure(string Value); <span style="color:blue;">public sealed</span> record ValidationFailure(string Value) : Failure(Value); <span style="color:blue;">public sealed</span> record IOFailure(string Value) : Failure(Value); <span style="color:blue;">public sealed</span> record GetBalloonQuery(BalloonId Id); <span style="color:blue;">public sealed</span> record UpdateBalloonCommand(Balloon Balloon); <span style="color:blue;">public sealed</span> record PopBalloonCommand(BalloonId Id, IPointyObject Needle); <span style="color:blue;">public interface</span> IBalloonRepository { Task&lt;Either&lt;IReadOnlyCollection&lt;Balloon&gt;, IOFailure&gt;&gt; GetBalloonsAsync(); Task&lt;Either&lt;Balloon, IOFailure&gt;&gt; GetBalloonAsync(GetBalloonQuery query); Task&lt;Either&lt;IOSuccess, IOFailure&gt;&gt; UpdateBalloonAsync(UpdateBalloonCommand command); Task&lt;Either&lt;IOSuccess, IOFailure&gt;&gt; DeleteBalloonAsync(PopBalloonCommand command); } </pre> </p> <p> I think this fits quite well with the idea that we can XXX out the name of the method and the types will tell the full story, no question at all. I also like that this seems to follow from Domain Driven Design, where we are being super explicit about the domain event because of the specificity of the object parameters we're sending down the wire. </p> <p> Does this idea hold any merit when looking to design APIs that follow the CQS philosophy? Where might you direct a greenhorn developer to learn more about this topic? </p> </div> <div class="comment-date">2022-03-31 04:30 UTC</div> </div> <div class="comment" id="3080cfd9318440a3896691c51f4a2987"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3080cfd9318440a3896691c51f4a2987">#</a></div> <div class="comment-content"> <p> Zach, thank you for writing. </p> <p> As <a href="#f53b34ca8e4742a19b78eb4d72f7369b">I write above</a>, it's up to you if you want to follow CQS. If you do, however, you have to play by the rules, and the rules are that Commands don't return data. </p> <p> We may have to make some concessions to the language in which we work. In C#, for example, we have to accept that <a href="https://docs.microsoft.com/dotnet/api/system.threading.tasks.task">Task</a> (without a generic type argument) is 'asynchronous <code>void</code>', even though it looks like a return value. </p> <p> If you want to be explicit about errors, then yes, there's always a risk that a state mutation may fail. Being explicit about that, too, might compel you to model a Command as having a return type like <code>Task&lt;Either&lt;MyError, Unit&gt;&gt;</code> (customarily, for Either values, errors go to the left, contrary to the isomorphic Result, where they go to the right). I don't, however, design side-effecting actions like that in C#. </p> <p> Even so, in none of these variations does a Command return data in the happy-path scenario. </p> <p> The API you suggest violate the rule that Commands mustn't return data. You are, of course, free to design APIs as you like, but CQS it isn't. As a counterexample, <code>UpdateBalloonAsync</code> returns an <code>IOSuccess</code> in the happy-path scenario, and that value again contains a <code>string</code>. </p> <p> If you removed the <code>string Value</code> from <code>IOSuccess</code> it'd be isomorphic to <a href="/2018/01/15/unit-isomorphisms">unit</a>. Transitively, then, the return type of <code>UpdateBalloonAsync</code> would be isomorphic to <code>Task&lt;Either&lt;IOFailure, Unit&gt;&gt;</code> (again, errors go to the left, because 'success is right'). If you allow Commands to throw exceptions, that again would be isomorphic to <code>Task</code>, with the implied understanding that the Command may throw an exception. </p> <p> Where would I direct someone who want to learn more about the topic? </p> <p> That's not that easy to answer. As Bertrand Meyer writes: </p> <blockquote> <p> "Side-effect-producing functions, which have been elevated by some languages (C seems to be the most extreme example) to the status of an institution, conflict with the classical notion of function in mathematics." </p> <footer><cite>Bertrand Meyer, <a href="/ref/oosc">Object-oriented Software Construction</a></cite>, p. 134</footer> </blockquote> <p> As far as I can tell, that 'institution' continues with most mainstream languages like C#, Java, JavaScript, etcetera. This implies that you're unlikely to find strong treatment of the topic of CQS in 'traditional' object-oriented material. </p> <p> As the above quote also implies by its use of the word <em>function</em>, you'll find that this distinction between <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a> and impure actions is <a href="/2018/11/19/functional-architecture-a-definition">more prevalent and explicit in functional programming</a>. <p> </div> <div class="comment-date">2022-04-02 8:22 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Why DRY? https://blog.ploeh.dk/2014/08/07/why-dry 2014-08-07T20:11:00+00:00 Mark Seemann <div id="post"> <p> <em>Code duplication is often harmful - except when it isn't. Learn how to think about the trade-offs involved.</em> </p> <p> Good programmers know that code duplication should be avoided. There's a cost associated with duplicated code, so we have catchphrases like <em>Don't Repeat Yourself</em> (DRY) in order to remind ourselves that code duplication is evil. </p> <p> It seems to me that some programmers see themselves as Terminators: out to eliminate all code duplication with extreme prejudice; sometimes, perhaps, without even considering the trade-offs involved. Every time you remove duplicated code, you add a level of indirection, and as you've probably heard before, all problems in computer science can be solved by another level of indirection, except for the problem of too many levels of indirection. </p> <p> Removing code duplication is important, but it tends to add a cognitive overhead. Therefore, it's important to understand <em>why</em> code duplication is harmful - or rather: <em>when</em> it's harmful. </p> <h3 id="b869385971174e588b11961e3f51e2a1"> Rates of change <a href="#b869385971174e588b11961e3f51e2a1" title="permalink">#</a> </h3> <p> Imagine that you copy a piece of code and paste it into ten other code bases, and then <em>never</em> touch that piece of code again. Is that harmful? </p> <p> Probably not. </p> <p> Here's one of my favourite examples. When protecting the invariants of objects, I always add Guard Clauses against nulls: </p> <p> <pre><span style="color:blue;">if</span>&nbsp;(subject&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject&quot;</span>);</pre> </p> <p> In fact, I have a Visual Studio code snippet for this; I've been using this code snippet for <em>years</em>, which means that I have code like this Guard Clause duplicated all over my code bases. Most likely, there are thousands of examples of such Guard Clauses on my hard drive, with the only variation being the name of the parameter. I don't mind, because, in my experience, these two lines of code <em>never change</em>. </p> <p> Yet many programmers see that as a violation of DRY, so instead, they introduce something like this: </p> <p> <pre><span style="color:#2b91af;">Guard</span>.AgainstNull(subject,&nbsp;<span style="color:#a31515;">&quot;subject&quot;</span>); </pre> </p> <p> The end result of this is that you've <em>slightly</em> increased the cognitive overhead, but what have you gained? As far as I can tell: nothing. The code still has the same number of Guard Clauses. Instead of idiomatic <code>if</code> statements, they are now method calls, but it's hardly DRY when you have to repeat those calls to Guard.AgainstNull all over the place. You'd still be repeating yourself. </p> <p> The point here is that DRY is a catchphrase, but shouldn't be an excuse for avoiding thinking explicitly about any given problem. </p> <p> If the duplicated code is likely to change a lot, the cost of duplication is likely to be high, because you'll have to spend time making the same change in lots of different places - and if you forget one, you'll be introducing bugs in the system. If the duplicated code is unlikely to change, perhaps the cost is low. As with all other risk management, you conceptually multiply the risk of the adverse event happening with the cost of the damage associated with that event. If the product is low, don't bother addressing the risk. </p> <h3 id="fe19846ad6d54a059f8339264642f709"> The Rule of Three <a href="#fe19846ad6d54a059f8339264642f709" title="permalink">#</a> </h3> <p> It's not a new observation that unconditional elimination of duplicated code can be harmful. The <a href="http://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)">Rule of Three</a> exists for this reason: <ol> <li>Write a piece of code.</li> <li>Write the same piece of code again. Resist the urge to generalise.</li> <li>Write the same piece of code again. Now you are allowed to consider generalising it.</li> </ol> There are a couple of reasons why this is a valuable rule of thumb. One is that the fewer examples you have of the duplication, the less evidence you have that the duplication is real, <a href="http://verraes.net/2014/08/dry-is-about-knowledge">instead of simply a coincidence</a>. </p> <p> Another reason is that even if the duplication is 'real' (and not coincidental), you may not have enough examples to enable you to make the correct refactoring. Often, even duplicated code comes with small variations: <ul> <li>The logic is the same, but a string value differs.</li> <li>The logic is almost the same, but one duplicate performs an extra small step.</li> <li>The logic looks similar, but operates on two different types of object.</li> <li>etc.</li> </ul> How should you refactor? Should you introduce a helper method? Should the helper method take a method argument? Should you extract a class? Should you add an interface? Should you apply the <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method pattern</a>? Or the <a href="http://en.wikipedia.org/wiki/Strategy_pattern">Strategy pattern</a>? </p> <p> If you refactor too prematurely, you may perform the wrong refactoring. Often, people introduce helper methods, and then when they realize that the axis of variability was not what they expected, they add more and more parameters to the helper method, and more and more complexity to its implementation. This leads to ripple effects. Ripple effects lead to thrashing. Thrashing leads to poor maintainability. Poor maintainability leads to low productivity. </p> <p> This is, in my experience, the most important reason to follow the Rule of Three: wait, until you have more facts available to you. You don't have to take the rule literally either. You can wait until you have four, five, or six examples of the duplication, if the rate of change is low. </p> <h3 id="2d121fba4d3b466399668c048cde1616"> The parallel to statistics <a href="#2d121fba4d3b466399668c048cde1616" title="permalink">#</a> </h3> <p> If you've ever taken a course in statistics, you would have learned that the less data you have, the less confidence you can have in any sort of analysis. Conversely, the more samples you have, the more confidence can you have if you are trying to find or verify some sort of correlation. </p> <p> The same holds true for code duplication, I believe. The more samples you have of duplicated code, the better you understand what is truly duplicated, and what varies. The better you understand the axes of variability, the better a refactoring you can perform in order to get rid of the duplication. </p> <h3 id="03b9a86283844ef0886233e7e5aa5689"> Summary <a href="#03b9a86283844ef0886233e7e5aa5689" title="permalink">#</a> </h3> <p> Code duplication is costly - but only if the code changes. The cost of code duplication, thus, is <em>C*p</em>, where <em>C</em> is the cost incurred, <em>when</em> you need to change the code, and <em>p</em> is the probability that you'll need to change the code. In my experience, for example, the Null Guard Clause in this article has a cost of duplication of 0, because the probability that I'll need to change it is 0. </p> <p> There's a cost associated with removing duplication - particularly if you make the wrong refactoring. Thus, depending on the values of <em>C</em> and <em>p</em>, you may be better off allowing a bit of duplication, instead of trying to eradicate it as soon as you see it. </p> <p> You may not be able to quantify <em>C</em> and <em>p</em> (I'm not), but you should be able to estimate whether these values are small or large. This should help you decide if you need to eliminate the duplication right away, or if you'd be better off waiting to see what happens. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="318786c1117d4ceeb7b0be786b7cf4e0"> <div class="comment-author">Markus Bullmann <a href="#318786c1117d4ceeb7b0be786b7cf4e0">#</a></div> <div class="comment-content"> <p>I would add one more aspect to this article: Experience.</p> <p>The more experience you gain the more likely you will perceive parts of your code which could possibly benefit from a generalization; but not yet because it would be premature.</p> <p>I try to keep these places in mind and try to simplify the potential later refactoring. When your code meets the rule of three, you’re able to adopt your preparations to efficiently refactor your code. This leads to better code even if the code isn't repeated for the third time.</p> <p>Needless to say that experience is always crucial but I like to show people, who are new to programing, which decisions are based on experience and which are based on easy to follow guidelines like the rule of three.</p> </div> <div class="comment-date">2014-08-20 22:03 UTC</div> </div> <div class="comment" id="2e1c6bab37134310b986d38effb82225"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2e1c6bab37134310b986d38effb82225">#</a></div> <div class="comment-content"> <p> Markus, thank you for writing. Yes, I agree that experience is always helpful. </p> </div> <div class="comment-date">2014-08-21 8:48 UTC</div> </div> <div class="comment" id="0f4b6778b1c54292ae9f5d9f28178a61"> <div class="comment-author">Sergey Telshevsky <a href="#0f4b6778b1c54292ae9f5d9f28178a61">#</a></div> <div class="comment-content"> <p>Wonderful article but I'd like to say that I prefer sticking to the <a href="http://en.wikipedia.org/wiki/Zero_one_infinity_rule">Zero one infinity</a> rule instead of the Rule Of Three</p> <p>That way I do overcome my laziness of searching for duplicates and extracting them to a procedure. Also it does help to keep code DRY without insanity to use it only on 2 or more lines of code. If it's a non-ternary oneliner I'm ok with that. If it's 2 lines of code I may consider making a procedure out of it and 3 lines and more are extracted every time I need to repeat them.</p> </div> <div class="comment-date">2014-12-23 14:00 UTC</div> </div> <div class="comment" id="0f4b6778b1c54292ae9f5d9f28178a62"> <div class="comment-author"><a href="https://github.com/julealgon">Juliano Goncalves</a> <a href="#0f4b6778b1c54292ae9f5d9f28178a62">#</a></div> <div class="comment-content"> <p>I generally agree with the points raised here, but I'd like to point out another component to the example used of argument validation. I realize this post is fairly old now, so I'm not entirely sure if you'd agree or not considering new development since then.</p> <p>While it is fair to say that argument validation is simple enough not to warrant some sort of encapsulation like the <span style="color:#2b91af;">Guard</span> class, this can quickly change once you add other factors into the mix, like number of arguments, and project-specific style rules. Take this sample code as an example:</p> <p> <pre><span style="color:blue;">if</span>&nbsp;(subject1&nbsp;==&nbsp;<span style="color:blue;">null</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject1&quot;</span>); } <span style="color:blue;">if</span>&nbsp;(subject2&nbsp;==&nbsp;<span style="color:blue;">null</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject2&quot;</span>); } <span style="color:blue;">if</span>&nbsp;(subject3&nbsp;==&nbsp;<span style="color:blue;">null</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject3&quot;</span>); } <span style="color:blue;">if</span>&nbsp;(subject4&nbsp;==&nbsp;<span style="color:blue;">null</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject4&quot;</span>); } <span style="color:blue;">if</span>&nbsp;(subject5&nbsp;==&nbsp;<span style="color:blue;">null</span>) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject5&quot;</span>); }</pre> </p> <p>This uses "standard" StyleCop rules with 5 parameters (probably the very limit of what is acceptable before having to refactor the code). Every block has the explicit scope added, and a line break after the scope terminator, as per style guidelines. This can get incredibly verbose and distract from the main logic of the method, which in cases like this, can become smaller than the argument validation code itself! Now, compare that to a more modern approach using discards:</p> <p> <pre>_&nbsp;=&nbsp;subject1&nbsp;??&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject1&quot;</span>); _&nbsp;=&nbsp;subject2&nbsp;??&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject2&quot;</span>); _&nbsp;=&nbsp;subject3&nbsp;??&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject3&quot;</span>); _&nbsp;=&nbsp;subject4&nbsp;??&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject4&quot;</span>); _&nbsp;=&nbsp;subject5&nbsp;??&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;subject5&quot;</span>); </pre> </p> <p>Or even better, using C#6 expression name deduction and the new, native guard method:</p> <p> <pre><span style="color:blue;">ArgumentNullException</span>.ThrowIfNull(subject1); <span style="color:blue;">ArgumentNullException</span>.ThrowIfNull(subject2); <span style="color:blue;">ArgumentNullException</span>.ThrowIfNull(subject3); <span style="color:blue;">ArgumentNullException</span>.ThrowIfNull(subject4); <span style="color:blue;">ArgumentNullException</span>.ThrowIfNull(subject5); </pre> </p> <p>Now, even that can be improved upon, and <a href="https://github.com/dotnet/csharplang/issues/2145">C# is moving in the direction of making contract checks like this even easier.</a></p> <p>I only wanted to make the point that, in this particular example, there are other benefits to the refactor besides just DRY, and those can be quite important as well since we read code much more than we write it: any gains in clarity and conciseness can net substantial productivity improvements.</p> </div> <div class="comment-date">2022-01-13 21:54 UTC</div> </div> <div class="comment" id="d6608e95d8d349fe802d5e0ba02f0c31"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d6608e95d8d349fe802d5e0ba02f0c31">#</a></div> <div class="comment-content"> <p> Juliano, thank you for writing. I agree that the first example is too verbose, but when addressing it, I'd start from the position that it's just a symptom. The underlying problem, here, is that the style guide is counterproductive. For the same reason, <a href="/2020/09/07/add-null-check-without-brackets-in-visual-studio">I don't write null guards that way</a>. </p> <p> I'm not sure that the following was your intent, so please excuse me as I take this topic in the direction of one of my pet peeves. In general, I think that 'we' have a tendency towards action bias. Instead of taking a step back and consider whether a problem even <em>ought</em> to be a problem, we often launch directly into producing a solution. </p> <p> In the spirit of <a href="https://en.wikipedia.org/wiki/Five_whys">five whys</a>, we may first find that the underlying explanation of the problem is the style guide. </p> <p> Asking one more time, we may begin to consider the need for null guards altogether. Why do we need them? Because objects may be null. Is there a way to avoid that? In modern C#, you can turn on the <a href="https://docs.microsoft.com/dotnet/csharp/nullable-references">nullable reference types feature</a>. Other languages like <a href="https://www.haskell.org/">Haskell</a> famously have no nulls. </p> <p> I realise that this line of inquiry is unproductive when you have existing C# code bases. </p> <p> Your point, however, remains valid. It's perfectly fine to extract a helper method when the motivation is to make the code more readable. And while my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a> is an attempt to instrumentalise <em>readability</em> to some extend, a subjective component will always linger. </p> </div> <div class="comment-date">2022-01-15 13:29 UTC</div> </div> <div class="comment" id="d6608e95d8d349fe802d5e0ba02f0c32"> <div class="comment-author"><a href="https://github.com/julealgon">Juliano Goncalves</a> <a href="#d6608e95d8d349fe802d5e0ba02f0c32">#</a></div> <div class="comment-content"> <p>I don't agree that the style guide is counter productive per se. There are fair justifications for prefering the explicit braces and the blank lines in the StyleCop documentation. They might not be the easiest in the eyes in this particular scenario, but we have to take into consideration the ease of enforcement in the entire project of a particular style. If you deviate from the style only in certain scenarios, it becomes much harder to manage on a big team. Applying the single rule everywhere is much easier. I do understand however that this is fairly subjective, so I won't pursue the argument here.</p> <p>Now, regarding nullable reference types, I'd just like to point out that one cannot only rely on that feature for argument validation. As you are aware, that's an optional feature, that might not be enabled on consumer code. If you are writing a reusable library for example, and you rely solely on Nullable Reference Types as your argument validation strategy, this will still cause runtime exceptions if a consumer that does not have NRTs enabled calls you passing null, and this would be a very bad developer experience for them as the stack trace will be potentially very cryptic. For that reason, it is a best practice to keep the runtime checks in place even when using nullable reference types.</p> </div> <div class="comment-date">2022-01-17 15:28 UTC</div> </div> <div class="comment" id="f5027533ce2e474cb0a0fd019546bb3f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f5027533ce2e474cb0a0fd019546bb3f">#</a></div> <div class="comment-content"> <p> Juliano, thank you for writing. I'm sure that there are reasons for that style guide. I admit that I haven't read them (you didn't link to them), but I can think of some arguments myself. Unless you're interested in that particular discussion, I'll leave it at that. Ultimately, this blog post is about DRY in general, and not particularly about Guard Clauses. </p> <p> I'm aware of the limitations of nullable reference types. This once again highlights the difference between developing and maintaining a reusable library versus a complete application. </p> <p> Understanding the forces that apply in a particular context is, I believe, a key to making good decisions. </p> </div> <div class="comment-date">2022-01-18 10:20 UTC</div> </div> <div class="comment" id="cb40c8655f534b96ab55024df19e36f5"> <div class="comment-author"><a href="https://spencerfarley.com">Spencer Farley</a> <a href="#cb40c8655f534b96ab55024df19e36f5">#</a></div> <div class="comment-content"> <p> I love the call out of <em>risk * probability</em>. I use threat matrixes to guide decisions all the time. </p> <img title="threat matrix" src="https://spencerfarley.com/post-media/threat-matrix.png"> <p> I'd also like to elaborate that duplication is determined not just by similar code, but by similar forces that change the code. If code looks similar, but changes for different reasons, then centralizing will cause tension between the different cases. </p> <p> Using the example above, guard clauses are unlikely to change together. Most likely, the programmmer changes a parameter and just the single guard clause is effected. Since the clauses don't change for the same reason, the effect of duplication is very low. </p> <p> Different forces might effect all guard clauses and justify centralizing. For example, if a system wanted to change its global null handling strategy at debug-time versus production. </p> <p> However, I often see semantically separate code struggling to share an implementation. I think <a href="https://blog.ploeh.dk/2010/12/02/Interfacesarenotabstractions/">header interfaces</a> and <a href="https://blog.ploeh.dk/2014/05/19/conforming-container/">conforming container-like library wrappers</a> are companion smells of this scenario. For example, I frequently see multiple flows trying to wrap email notifications behind some generic email notification abstraction. The parent flow is still coupled to the idea of email, the two notifications still change separately, and there's an extra poor abstraction between us and sending emails. </p> </div> <div class="comment-date">2022-02-19 21:00 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Encapsulation and SOLID Pluralsight course https://blog.ploeh.dk/2014/08/06/encapsulation-and-solid-pluralsight-course 2014-08-06T12:19:00+00:00 Mark Seemann <div id="post"> <p> <em>My latest Pluralsight course is now available. This time it's about fundamental programming techniques.</em> </p> <p> Most programmers I meet know about <em>encapsulation</em> and <em>Object-Oriented Design</em> (OOD) - at least, until I start drilling them on specifics. Apparently, the way most people have been taught OOD is at odds with my view on the subject. For a long time, I've wanted to teach OOD the way I think it should be taught. My perspective is chiefly based on Bertrand Meyer's <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a> and Robert C. Martin's SOLID principles (presented, among other sources, in <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices in C#</a>). My focus is on <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a>, protection of invariants, and favouring Composition over Inheritance. </p> <p> <img src="/content/binary/encapsulation-solid-screen-shot.jpg" alt="Course screenshot"> </p> <p> In <a href="https://blog.ploeh.dk/encapsulation-and-solid">my new Pluralsight course</a>, I'm happy to be able to teach OOD the way I think it should be taught. The course is aimed at professional developers with a couple of years of experience, but it's based on decades of experience (mine and others'), so I hope that even seasoned programmers can learn a thing or two from watching it. </p> <h3 id="bfb677910e6842528247f1ad001063d2"> What you will learn <a href="#bfb677910e6842528247f1ad001063d2" title="permalink">#</a> </h3> <p> How do you make your code easily usable for other people? By following the actionable, measurable rules laid forth by Command/Query Separation as well as Postel’s law. </p> <p> Learn how to write maintainable software that can easily respond to changing requirements, using object-oriented design principles. First, you'll learn about the fundamental object-oriented design principle of Encapsulation, and then you'll learn about the five SOLID principles - also known as <em>the principles of object-oriented design</em>. There are plenty of code examples along the way; they are in C#, but written in such a way that they should be easily understandable to readers of Java, or other curly-brace-based languages. </p> <h3 id="f52deb62d40c42f794dff91fa1a7640b"> Is OOD still relevant? <a href="#f52deb62d40c42f794dff91fa1a7640b" title="permalink">#</a> </h3> <p> Functional Programming is becoming increasingly popular, and regular readers of this blog will have noticed that I, myself, write <a href="/tags.html#F#-ref">a fair amount of F# code</a>. In light of this, is OOD still relevant? </p> <p> There's a lot of Object-Oriented code out there, or at least, code written in a nominally Object-Oriented language, and it's not going to go away any time soon. Getting better at maintaining and evolving Object-Oriented code is, in my opinion, still important. </p> <p> Some of the principles I cover in this course are also relevant in Functional Programming. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="fde481a3770846ccb6b0e7369743dfea"> <div class="comment-author"><a href="http://www.danschnau.com">Dan Schnau</a> <a href="#fde481a3770846ccb6b0e7369743dfea">#</a></div> <div class="comment-content">I'm really enjoying your presentation! It is easy to follow and understand your explanations. I have a bit of a tangent question about your TryRead method in the Encapsulation module. You mention that your final implementation has a race condition in it. Can you explain the race condition?</div> <div class="comment-date">2014-08-06 06:55 UTC</div> </div> <div class="comment" id="b376e8c19f7f4d218f0073578bbc6cc9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b376e8c19f7f4d218f0073578bbc6cc9">#</a></div> <div class="comment-content"> <p> Dan, thank you for writing. You are talking about this implementation, I assume: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;TryRead(<span style="color:blue;">int</span>&nbsp;id,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">string</span>&nbsp;message) { &nbsp;&nbsp;&nbsp;&nbsp;message&nbsp;=&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;path&nbsp;=&nbsp;<span style="color:blue;">this</span>.GetFileName(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!<span style="color:#2b91af;">File</span>.Exists(path)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;message&nbsp;=&nbsp;<span style="color:#2b91af;">File</span>.ReadAllText(path); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; }</pre> </p> <p> This implementation isn't thread-safe, because File.Exists(path) may return true, and then, <em>before</em> File.ReadAllText(path) is invoked, another thread or process could delete the file, causing an exception to be thrown when File.ReadAllText(path) is invoked. </p> <p> It's possible to <em>make</em> the TryRead method thread-safe (I know of at least two alternatives), but I usually leave that as an exercise for the reader :) </p> </div> <div class="comment-date">2014-08-10 10:51 UTC</div> </div> <div class="comment" id="881adeb6c9d64db98effd31d8a956661"> <div class="comment-author">Bart van Nierop <a href="#881adeb6c9d64db98effd31d8a956661">#</a></div> <div class="comment-content"> <p> The only <em>safe</em> implementation that I am aware of would be something along the lines of: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;TryRead(<span style="color:blue;">int</span>&nbsp;id,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">string</span>&nbsp;message) { &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;path&nbsp;=&nbsp;<span style="color:blue;">this</span>.GetFileName(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">try</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message&nbsp;=&nbsp;<span style="color:#2b91af;">File</span>.ReadAllText(path); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">catch</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message&nbsp;=&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Am I missing something? </p> </div> <div class="comment-date">2014-08-13 16:43 UTC</div> </div> <div class="comment" id="0768bbea71e140028ff781f2fa9f42e4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0768bbea71e140028ff781f2fa9f42e4">#</a></div> <div class="comment-content"> <p> Bart, no, you aren't missing anything; that looks about right :) The other alternative is just a variation on your solution. </p> </div> <div class="comment-date">2014-08-13 17:56 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Drain https://blog.ploeh.dk/2014/07/23/drain 2014-07-23T14:25:00+00:00 Mark Seemann <div id="post"> <p> <em>A Drain is a filter abstraction over an Iterator, with the purpose of making out-of-process queries more efficient.</em> </p> <p> For some years now, the <a href="http://codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle</a> has pushed me towards software architectures based upon fewer and fewer abstractions, where each abstraction, on the other hand, is reused over and over again. </p> <p> In my Pluralsight course about <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">A Functional Architecture with F#</a>, I describe how to build an entire mainstream application based on only two abstractions: <ul> <li><a href="http://en.wikipedia.org/wiki/Iterator_pattern">Iterator</a></li> <li><a href="http://en.wikipedia.org/wiki/Observer_pattern">Observer</a></li> </ul> More specifically, I use <a href="https://rx.codeplex.com/">Reactive Extensions</a> for <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Commands</a>, and the <a href="http://msdn.microsoft.com/en-us/library/ee353635.aspx">Seq module</a> for <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Queries</a> (but if you're on C#, you can use LINQ instead). </p> <h3 id="dd9c6e9e6c4b414d93ab98535cf87c68"> The problem <a href="#dd9c6e9e6c4b414d93ab98535cf87c68" title="permalink">#</a> </h3> <p> It turns out that these two abstractions are enough to build an entire, mainstream system, but in practice, there's a performance problem. If you have <em>only</em> Iterators, you'll have to read <em>all</em> your data into memory, and then filter in memory. If you have lots of data on storage, this is obviously going to be prohibitively slow, so you'll need a way to select only a subset out of your persistent store. </p> <p> This problem should be familiar to every student of software development. Pure abstractions tend not to survive contact with reality (there are examples in both Object-Oriented, Functional, and Logical or Relational programming), but we should still strive towards keeping abstractions as pure as possible. </p> <p> One proposed solution to the Query Problem is to use something like IQueryable, but unfortunately, <a href="/2012/03/26/IQueryableTisTightCoupling">IQueryable is an extremely poor abstraction</a> (and so are F# query expressions, too). </p> <p> In my experience, the most important feature of IQueryable is the ability to <em>filter</em> before loading data; normally, you can perform projections in memory, but when you read from persistent storage, you need to select your desired subset <em>before</em> loading it into memory. </p> <p> Inspired by talks by <a href="http://bartdesmet.net/bart">Bart De Smet</a>, in my Pluralsight course, I define custom filter interfaces like: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">IReservations</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:#2b91af;">seq</span>&lt;<span style="color:#2b91af;">Envelope</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">abstract</span>&nbsp;Between&nbsp;:&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">seq</span>&lt;<span style="color:#2b91af;">Envelope</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;</pre> </p> <p> or </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">INotifications</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:#2b91af;">seq</span>&lt;<span style="color:#2b91af;">Envelope</span>&lt;<span style="color:#2b91af;">Notification</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">abstract</span>&nbsp;About&nbsp;:&nbsp;<span style="color:#2b91af;">Guid</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">seq</span>&lt;<span style="color:#2b91af;">Envelope</span>&lt;<span style="color:#2b91af;">Notification</span>&gt;&gt;</pre> </p> <p> Both of these interfaces derive from IEnumerable&lt;T&gt; and add a single extra method that defines a custom filter. Storage-aware implementations can implement this method by returning a new sequence of only those items on storage that match the filter. Such a method may <ul> <li>make a SQL query against a database</li> <li>make a query against a document database</li> <li>read only some files from the file system</li> <li>etc.</li> </ul> For more details, examples, and full source code, see <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">my Pluralsight course</a>. </p> <h3 id="5fe26f63951447fa890cb753d62e8fef"> Generalized interface <a href="#5fe26f63951447fa890cb753d62e8fef" title="permalink">#</a> </h3> <p> The custom interfaces shown above follow a common template: the interface derives from IEnumerable&lt;T&gt; and adds a single 'filter' method, which filters the sequence based on the input argument(s). In the above examples, IReservations define a Between method with two arguments, while INotifications defines an About method with a single argument. </p> <p> In order to generalize, it's necessary to find a common name for the interface and its single method, as well as deal with variations in method arguments. </p> <p> All the really obvious names like <em>Filter</em>, <em>Query</em>, etc. are already 'taken', so I hit a thesaurus and settled on the name <em>Drain</em>. A Drain can potentially drain a sequence of elements to a smaller sequence. </p> <p> When it comes to variations in input arguments, the solution is to use generics. The Between method that takes two arguments could also be modelled as a method taking a single tuple argument. Eventually, I've arrived at this general definition: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:#2b91af;">Drain</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">IDrainable</span>&lt;&#39;a,&nbsp;&#39;b&gt;&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:#2b91af;">seq</span>&lt;&#39;a&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">abstract</span>&nbsp;On&nbsp;:&nbsp;&#39;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">seq</span>&lt;&#39;a&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;on&nbsp;x&nbsp;(d&nbsp;:&nbsp;<span style="color:#2b91af;">IDrainable</span>&lt;&#39;a,&nbsp;&#39;b&gt;)&nbsp;=&nbsp;d.On&nbsp;x</pre> </p> <p> As you can see, I decided to name the extra method <em>On</em>, as well as add an <em>on</em> function, which enables clients to use a Drain like this: </p> <p> <pre><span style="color:blue;">match</span>&nbsp;tasks&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Drain</span>.on&nbsp;id&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList&nbsp;<span style="color:blue;">with</span> </pre> </p> <p> In the above example, <code>tasks</code> is defined as IDrainable&lt;TaskRendition,&nbsp;string&gt;, and <code>id</code> is a string, so the result of draining on the ID is a sequence of TaskRendition records. </p> <p> Here's another example: </p> <p> <pre><span style="color:blue;">match</span>&nbsp;statuses&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Drain</span>.on(id,&nbsp;conversationId)&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Seq</span>.toList&nbsp;<span style="color:blue;">with</span> </pre> </p> <p> Here, <code>statuses</code> is defined as IDrainable&lt;string&nbsp;*&nbsp;Guid,&nbsp;string&nbsp;*&nbsp;string&gt; - not the most well-designed instance, I admit: I should really introduce some well-named records instead of those tuples, but the point is that you can also drain on multiple values by using a tuple (or a record type) as the value on which to drain. </p> <h3 id="2d9de416f79546ad8025213d0fc9ad91"> In-memory implementation <a href="#2d9de416f79546ad8025213d0fc9ad91" title="permalink">#</a> </h3> <p> One of the great features of Drains is that an in-memory implementation is easy, so you can add this function to the Drain module: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;ofSeq&nbsp;areEqual&nbsp;s&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;IDrainable&lt;&#39;a,&nbsp;&#39;b&gt;&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.On&nbsp;x&nbsp;=&nbsp;s&nbsp;|&gt;&nbsp;Seq.filter&nbsp;(<span style="color:blue;">fun</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;areEqual&nbsp;y&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.GetEnumerator()&nbsp;=&nbsp;s.GetEnumerator() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.GetEnumerator()&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(this&nbsp;:&gt;&nbsp;&#39;a&nbsp;seq).GetEnumerator()&nbsp;:&gt;&nbsp;System.Collections.IEnumerator&nbsp;}</pre> </p> <p> This enables you to take any IEnumerable&lt;T&gt; (seq&lt;'a&gt;) and turn it into an in-memory Drain by supplying an equality function. Here's an example: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;toDrainableTasks&nbsp;(tasks&nbsp;:&nbsp;<span style="color:#2b91af;">TaskRendition</span>&nbsp;<span style="color:#2b91af;">seq</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;tasks &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Drain</span>.ofSeq&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.Id&nbsp;=&nbsp;y)</pre> </p> <p> This little helper function takes a sequence of TaskRendition records and defines the equality function as a comparison on each TaskRendition record's Id property. The result is a drain that you can use to select one or more TaskRendition records based on their IDs. </p> <p> I use this a lot for unit testing. </p> <h3 id="e938401258734a238fc1719cf26f21c4"> Empty Drains <a href="#e938401258734a238fc1719cf26f21c4" title="permalink">#</a> </h3> <p> It's also easy to define an empty drain, by adding this value to the Drain module: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;empty&lt;&#39;a,&nbsp;&#39;b&gt;&nbsp;=&nbsp;<span style="color:#2b91af;">Seq</span>.empty&lt;&#39;a&gt;&nbsp;|&gt;&nbsp;ofSeq&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;(y&nbsp;:&nbsp;&#39;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">false</span>) </pre> </p> <p> Here's a usage example: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;mappedUsers&nbsp;=&nbsp;<span style="color:#2b91af;">Drain</span>.empty&lt;<span style="color:#2b91af;">UserMapped</span>,&nbsp;<span style="color:#2b91af;">string</span>&gt; </pre> </p> <p> Again, this can be handy when unit testing. </p> <h3 id="6f96cc505a4a411db01299de762acb7d"> Other implementations <a href="#6f96cc505a4a411db01299de762acb7d" title="permalink">#</a> </h3> <p> While the in-memory implementation is useful when unit testing, the entire purpose of the Drain abstraction is to enable various implementations to implement the On method to perform a custom selection against a well-known data source. As an example, you could imagine an implementation that translates the input arguments of the On method into a SQL query. </p> <p> If you want to see examples of this, my Pluralsight course demonstrates how to implement IReservations and INotifications with various data stores - I trust you can extrapolate from those examples. </p> <h3 id="f77ba66b272540339557a9a902f2a1c0"> Summary <a href="#f77ba66b272540339557a9a902f2a1c0" title="permalink">#</a> </h3> <p> You can base an entire mainstream application on the two abstractions of Iterator and Observer. However, the problem when it comes to Iterators is that conceptually, you'll need to iterate over <em>all</em> potentially relevant elements in your system - and that may be millions of records! </p> <p> However impure it is to introduce a third interface into the mix, I still prefer to introduce <em>a single</em> generic interface, instead of multiple custom interfaces, because once you and your co-workers understand the Drain abstraction, the cognitive load is still quite low. A Drain is an Iterator with a twist, so in the end, you'll have a system built on 2½ abstractions. </p> <p> <strong>P.S. 2018-06-20.</strong> While this article is a decent attempt to generalise the query side of a fundamentally object-oriented approach to software architecture, I've later realised that <a href="/2017/01/30/partial-application-is-dependency-injection">dependency injection, even when disguised as partial application, isn't functional</a>. The problem is that querying a database, reading files, and so on, is essentially non-deterministic, even when no side effects are incurred. The functional approach is to altogether <a href="/2017/02/02/dependency-rejection">reject the notion of dependencies</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="a7c5d54f70694f059866c5d2f154f82c"> <div class="comment-author"><a href="http://nikosbaxevanis.com">Nikos Baxevanis</a> <a href="#a7c5d54f70694f059866c5d2f154f82c">#</a></div> <div class="comment-content">A diff-output from the <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">A Functional Architecture with F#</a> master branch, after applying the <a href="/2014/07/23/drain/">Drain</a> abstraction, is available <a href="http://nikosbaxevanis.com/blog/2014/07/28/in-memory-drain-abstractions-applied-to-a-functional-architecture-with-f-number/">here</a>. Notice how Drain cuts the maintenance of multiple homogenous abstractions, and makes the code cleaner and easier to reason about.</div> <div class="comment-date">2014-07-28 09:00 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Hire me https://blog.ploeh.dk/2014/07/22/hire-me 2014-07-22T08:30:00+00:00 Mark Seemann <div id="post"> <p> <em>July-October 2014 I have some time available, if you'd like to hire me.</em> </p> <p> Since I became self-employed in 2011, I've been as busy as always, but it looks like I have some time available in the next months. If you'd like to hire me for small or big tasks, please contact me. See <a href="/hire-me">here for details</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Passive Attributes https://blog.ploeh.dk/2014/06/13/passive-attributes 2014-06-13T09:59:00+00:00 Mark Seemann <div id="post"> <p> <em>Passive Attributes are Dependency Injection-friendly.</em> </p> <p> In my article about <a href="/2014/05/19/di-friendly-framework">Dependency Injection-friendly frameworks</a>, towards the end I touched on the importance of defining attributes <em>without</em> behaviour, but I didn't provide a constructive example of how to do this. In this article, I'll outline how to write a Dependency Injection-friendly attribute for use with <a href="http://www.asp.net/web-api">ASP.NET Web API</a>, but as far as I recall, you can do something similar with ASP.NET MVC. </p> <h3 id="8e939bc21a154490abc20a9d60418849"> Problem statement <a href="#8e939bc21a154490abc20a9d60418849" title="permalink">#</a> </h3> <p> In ASP.NET Web API, you can adorn your Controllers and their methods with various <a href="http://msdn.microsoft.com/en-us/library/system.web.http.filters.filterattribute.aspx">Filter attributes</a>, which is a way to implement cross-cutting concerns, such as <a href="http://msdn.microsoft.com/en-us/library/system.web.http.filters.authorizationfilterattribute.aspx">authorization</a> or <a href="http://msdn.microsoft.com/en-us/library/system.web.http.filters.exceptionfilterattribute.aspx">error handling</a>. The problem with this approach is that attribute instances are created by the run-time, so you can't use proper Dependency Injection (DI) patterns such as Constructor Injection. If an attribute defines behaviour (which many of the Web API attributes do), the most common attempt at writing loosely coupled code is to resort to a static <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator (an anti-pattern)</a>. </p> <p> This again seems to lead framework designers towards attempting to make their frameworks 'DI-friendly' by introducing a <a href="/2014/05/19/conforming-container">Conforming Container (another anti-pattern)</a>. </p> <p> The solution is simple: define attributes without behaviour. </p> <h3 id="0d03d024f6784cd29c68aae10b81945f"> Metering example <a href="#0d03d024f6784cd29c68aae10b81945f" title="permalink">#</a> </h3> <p> Common examples of cross-cutting concerns are authentication, authorization, error handling, logging, and caching. In these days of multi-tenant on-line services, another example would be metering, so that you can bill each user based on consumption. </p> <p> Imagine that you're writing an HTTP API where some actions must be metered, whereas others shouldn't. It might be nice to adorn the metered actions with an attribute to indicate this: </p> <p> <pre>[<span style="color:#2b91af;">Meter</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Get(<span style="color:blue;">int</span>&nbsp;id)</pre> </p> <p> Metering is a good example of a cross-cutting concern with behaviour, because, in order to be useful, you'd need to <em>store</em> the metering records somewhere, so that you can bill your users based on these records. </p> <p> A passive Meter attribute would simply look like this: </p> <p> <pre>[<span style="color:#2b91af;">AttributeUsage</span>(<span style="color:#2b91af;">AttributeTargets</span>.Method,&nbsp;AllowMultiple&nbsp;=&nbsp;<span style="color:blue;">false</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MeterAttribute</span>&nbsp;:&nbsp;<span style="color:#2b91af;">Attribute</span> { }</pre> </p> <p> In order to keep the example simple, this attribute defines no data, and can only be used on methods, but nothing prevents you from adding (primitive) values to it, or extend its usage to classes as well as methods. </p> <p> As you can tell from the example, the MeterAttribute has no behaviour. </p> <p> In order to implement a metering cross-cutting concern, you'll need to define an <a href="http://msdn.microsoft.com/en-us/library/system.web.http.filters.iactionfilter.aspx">IActionFilter</a> implementation, but that's a 'normal' class that can take dependencies: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MeteringFilter</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IActionFilter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;&nbsp;observer; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MeteringFilter(<span style="color:#2b91af;">IObserver</span>&lt;<span style="color:#2b91af;">MeterRecord</span>&gt;&nbsp;observer) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(observer&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;observer&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.observer&nbsp;=&nbsp;observer; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;&nbsp;ExecuteActionFilterAsync( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpActionContext</span>&nbsp;actionContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">CancellationToken</span>&nbsp;cancellationToken, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;&gt;&nbsp;continuation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;meterAttribute&nbsp;=&nbsp;actionContext &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ActionDescriptor &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GetCustomAttributes&lt;<span style="color:#2b91af;">MeterAttribute</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SingleOrDefault(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(meterAttribute&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;continuation(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;operation&nbsp;=&nbsp;actionContext.ActionDescriptor.ActionName; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;user&nbsp;=&nbsp;actionContext.RequestContext.Principal.Identity.Name; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;started&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;continuation().ContinueWith(t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;completed&nbsp;=&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.Now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;duration&nbsp;=&nbsp;completed&nbsp;-&nbsp;started; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;record&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MeterRecord</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Operation&nbsp;=&nbsp;operation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;=&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Started&nbsp;=&nbsp;started, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration&nbsp;=&nbsp;duration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.observer.OnNext(record); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;t.Result; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;AllowMultiple &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">true</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This MeteringFilter class implements IActionFilter. It looks for the <code>[Meter]</code> attribute. If it doesn't find the attribute on the method, it immediately returns; otherwise, it starts collecting data about the invoked action: <ol> <li>From <code>actionContext.ActionDescriptor</code> it retrieves the name of the operation. If you try this out for yourself, you may find that <code>ActionName</code> alone doesn't provide enough information to uniquely identify the method - it basically just contains the value "Get". However, the <code>actionContext</code> contains enough information about the action that you can easily build up a better string; I just chose to skip doing that in order to keep the example simple.</li> <li>From <code>actionContext.RequestContext.Principal</code> you can get information about the current user. In order to be useful, the user must be authenticated, but if you need to meter the usage of your service, you'll probably not allow anonymous access.</li> <li>Before invoking the <code>continuation</code>, the MeteringFilter records the current time.</li> <li>After the <code>continuation</code> has completed, the MeteringFilter again records the current time and calculates the duration.</li> <li>Finally, it publishes a MeterRecord to an injected dependency.</li> </ol> Notice that MeteringFilter uses normal Constructor Injection, which means that it can <a href="/2011/05/30/DesignSmellDefaultConstructor">protect its invariants</a>. In this example, I'm <a href="/2013/09/11/di-and-events-composition">using IObserver&lt;T&gt; as a dependency</a>, but obviously, you could use any dependency you'd like. </p> <h3 id="8123477447fe4287b6b667423efc45a0"> Configuring the service <a href="#8123477447fe4287b6b667423efc45a0" title="permalink">#</a> </h3> <p> MeteringFilter is a normal class with behaviour, which you can register as a cross-cutting concern in your Web API service as easily as this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;filter&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MeteringFilter</span>(observer); config.Filters.Add(filter);</pre> </p> <p> where <code>observer</code> is your preferred implementation of IObserver&lt;MeterRecord&gt;. This example illustrates the <a href="/2014/06/10/pure-di">Pure DI</a> approach, but if you rather prefer to resolve MeteringFilter with your DI Container of choice, you can obviously do this as well. </p> <p> The above code typically goes into your Global.asax file, or at least a class directly or indirectly invoked from Application_Start. This constitutes (part of) the <a href="/2011/07/28/CompositionRoot">Composition Root</a> of your service. </p> <h3 id="7097ad83d716407fac1098afec352c6e"> Summary <a href="#7097ad83d716407fac1098afec352c6e" title="permalink">#</a> </h3> <p> Both ASP.NET Web API and ASP.NET MVC supports cross-cutting concerns in the shape of filters that you can add to the service. Such filters can look for passive attributes in order to decide whether or not to trigger. The advantage of this approach is that you can use normal Constructor Injection with these filters, which completely eliminates the need for a Service Locator or Conforming Container. </p> <p> The programming model remains the same as with active attributes: if you want a particular cross-cutting concern to apply to a particular method or class, you adorn it with the appropriate attribute. Passive attributes have all the advantages of active attributes, but none of the disadvantages. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="6071edaa16fe4bf887deea0bc7334b42"> <div class="comment-author">Jonathan Ayoub <a href="#6071edaa16fe4bf887deea0bc7334b42">#</a></div> <div class="comment-content"> Thanks for the article. I have a simple filter that I need to add, and I was just going to use service locator to get my dependency, but realized that would force me to do things I don't want to when writing a test for the filter. <br> <br> What if the dependency I'm injecting needs to be a transient dependency? Injecting a transient service into a singleton (the filter), would cause issues. My initial idea is to create an abstract factory as a dependency, then when the filter action executes, create the transient dependency, use it, and dispose. Do you have any better ideas? </div> <div class="comment-date">2016-01-21 18:34 UTC</div> </div> <div class="comment" id="0842455b7c6b4119ac121ec42c1cfe02"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0842455b7c6b4119ac121ec42c1cfe02">#</a></div> <div class="comment-content"> <p> Jonathan, thank you for writing. Does <a href="/2014/08/24/decoraptor">this article on the Decoraptor pattern</a> (and this <a href="http://stackoverflow.com/a/25489672/126014">Stack Overflow answer</a>) answer your question? </p> </div> <div class="comment-date">2016-01-21 19:51 UTC</div> </div> <div class="comment" id="893f189b3ca74fa998c013d1cb4c9563"> <div class="comment-author">Jonathan Ayoub <a href="#893f189b3ca74fa998c013d1cb4c9563">#</a></div> <div class="comment-content"> <p> Yes, that's a good solution for this situation. Thanks! </p> </div> <div class="comment-date">2016-01-26 14:32 UTC</div> </div> <div class="comment" id="0cc223c3c22b480895eb83728976ec30"> <div class="comment-author"><a href="http://blog.tech-fellow.net">Valdis Iljuconoks</a> <a href="#0cc223c3c22b480895eb83728976ec30">#</a></div> <div class="comment-content"> <p> Reading through Asp.Net Core <a href="https://docs.asp.net/en/latest/mvc/controllers/filters.html#configuring-filters">Type and Service Filters</a>, do you think it's sufficient to go that way (I know that TypeFilter is a bit clumsy), but let's assume that I need simple injection in my filter - ServiceFilterAttribute looks promising. Or you still would recomment to implement logic via traditional filter pipeline: `services.AddMvc(o => o.Filters.Add(...));`? </p> </div> <div class="comment-date">2016-08-21 17:20 UTC</div> </div> <div class="comment" id="8775195818304f0a800e18c66431c97f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8775195818304f0a800e18c66431c97f">#</a></div> <div class="comment-content"> <p> Valdis, thank you for writing. I haven't looked into the details of ASP.NET Core recently, but even so: on a more basic level, I don't understand the impulse to put behaviour into attributes. An attribute is an annotation. It's a declaration that a method, or type, is somehow special. Why not keep the annotation decoupled from the behaviour it 'triggers'? This would enable you to reuse the behaviour in other scenarios than by putting an attribute on a method. </p> </div> <div class="comment-date">2016-08-22 16:21 UTC</div> </div> <div class="comment" id="beaa0debd0a84987aef95ce216b7b181"> <div class="comment-author"><a href="http://blog.tech-fellow.net">Valdis Iljuconoks</a> <a href="#beaa0debd0a84987aef95ce216b7b181">#</a></div> <div class="comment-content"> <p>I got actually very similar feelings, just wanted to get your opinion. And by the way - there is a catch, you can mismatch type and will notice that only during runtime. For instance: `[ServiceFilter(typeof(HomeController))]` will generate exception, because given type is not derived from `IFilterMetadata`</p> </div> <div class="comment-date">2016-08-22 20:46 UTC</div> </div> <div class="comment" id="391e4f540b284e17a5d620e1af7c4ea1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#391e4f540b284e17a5d620e1af7c4ea1">#</a></div> <div class="comment-content"> <p> Indeed, one of the most problematic aspects of <a href="/2014/05/19/conforming-container">container-based DI</a> (as opposed to <a href="/2014/06/10/pure-di">Pure DI</a>) is the <a href="/2012/11/06/WhentouseaDIContainer">loss of compile-time safety</a> - to little advantage, I might add. </p> </div> <div class="comment-date">2016-08-23 05:49 UTC</div> </div> <div class="comment" id="c85f9fc53aec48bb8c92c447c2e5424a"> <div class="comment-author"><a href="http://tyreejackson.com">Tyree Jackson</a> <a href="#c85f9fc53aec48bb8c92c447c2e5424a">#</a></div> <div class="comment-content"> <p> I'm concerned with using attributes for AOP at all in these cases. Obviously using attributes that define behaviors that rely on external depedencies is a bad idea as you and others have already previously covered. But is using attributes that define metadata for custom behaviors all that much better? For example, if one provides a framework with libraries containing common controllers, and another pulls those controllers into their composition root in order to host them, there is no indication at compile time that these custom attributes may be present. Would it not be better to require that behavior be injected into the constructor and simply consume the behavior at the relevant points within the controller? Or if attributes must be used, would it not be better for the component that implements the behavior to somehow be injected into the controller and given the opportunity to intercept requests to the controller earlier in the execution pipeline so that it can check for the attributes? Due to the nature of the Web API and .Net MVC engines, attributes defined with behavior can enforce their behaviors to be executed by default. And while attributes without behavior do indicate the need for a behavior to be executed for the class they are decorating, it does not appear that they can enforce said behavior to be executed by default. They are too easy to miss or ignore. There has got to be a better way. I have encountered this problem while refactoring some code that I'm working on right now (retro fitting said code with more modern, DI based code). I'm hoping to come up with a solution that informs the consuming developer in the composition root that this behavior is required, and still be able enforce the behavior with something closer to a decoration rather than a function call. </p> </div> <div class="comment-date">2016-11-16 23:10 UTC</div> </div> <div class="comment" id="94cd1e6d9e2f4db3ac7b5693d3e14450"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#94cd1e6d9e2f4db3ac7b5693d3e14450">#</a></div> <div class="comment-content"> <p> Tyree, thank you for writing. That's a valid concern, but I don't think it's isolated to passive attributes. The problem you outline is also present if you attempt to address cross-cutting concerns with the <a href="https://en.wikipedia.org/wiki/Decorator_pattern">Decorator design pattern</a>, or with dynamic interception as I describe in chapter 9 of <a href="http://amzn.to/12p90MG">my book</a>. You also have this problem with the <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite pattern</a>, because you can't have any compile-time guarantee that you've remembered to compose all required elements into the Composite, or that they're defined in the appropriate order (if that matters). </p> <p> In fact, you can extend this argument to any use of polymorphism: how can you guarantee, at compile-time, that a particular polymorphic object contains the behaviour that you desire, instead of, say, being a <a href="https://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a>? You can't. That's the entire point of polymorphism. </p> <p> Even with attributes, how can you guarantee that the attributes stay there? What if another developer comes by and removes an attribute? The code is still going to compile. </p> <p> Ultimately, code exists in order to implement some desired behaviour. There are guarantees you can get from the type system, but static typing can't provide <em>all</em> guarantees. If it could, you be in the situation where, <em>'if it compiles, it works'</em>. No programming language I've heard of provides that guarantee, although <a href="/2016/02/10/types-properties-software">there's a spectrum of languages with stronger or weaker type systems</a>. Instead, we'll have to <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">get feedback from multiple sources</a>. Attributes often define cross-cutting concerns, and I find that these are often best verified with a set of integration tests. </p> <p> As always in software development, you have to find the balance that's right for a particular scenario. In some cases, it's catastrophic if something is incorrectly configured; in other cases, it's merely unfortunate. More rigorous verification is required in the first case. </p> </div> <div class="comment-date">2016-11-19 9:39 UTC</div> </div> <div class="comment" id="71a13527719f430c8d140cfde6f50d0f"> <div class="comment-author"><a href="http://crispinto.com">Cristian Pinto</a> <a href="#71a13527719f430c8d140cfde6f50d0f">#</a></div> <div class="comment-content"> <p> Thanks for the post, I tried to do the same for a class attribute (AttributeTargets.Class) and I am getting a null object every time I get the custom attributes. Does this only work for Methods? Or how can I make it work with classes? Thanks. </p> </div> <div class="comment-date">2017-04-05 13:03 UTC</div> </div> <div class="comment" id="c81e478c5d5f480fba09438a54f989b8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c81e478c5d5f480fba09438a54f989b8">#</a></div> <div class="comment-content"> <p> Cristian, thank you for writing. The example code shown in this article only looks in the action context's <code>ActionDescriptor</code>, which is an object that describes the action method. If you want to look for the attribute on the class, you should look in the action context's <code>ControllerDescriptor</code> instead, like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;meterAttribute&nbsp;=&nbsp;actionContext &nbsp;&nbsp;&nbsp;&nbsp;.ControllerContext &nbsp;&nbsp;&nbsp;&nbsp;.ControllerDescriptor &nbsp;&nbsp;&nbsp;&nbsp;.GetCustomAttributes&lt;<span style="color:#2b91af;">MeterAttribute</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;.SingleOrDefault();</pre> </p> <p> Obviously, if you want to support putting the attribute <em>both</em> on the class and the method, you'd need to look in both places, and decide which one to use if you find more than one. </p> </div> <div class="comment-date">2017-04-25 6:12 UTC</div> </div> <div class="comment" id="fba09438a54f989b8c81e478c5d5f480"> <div class="comment-author">Ian Thomas <a href="#fba09438a54f989b8c81e478c5d5f480">#</a></div> <div class="comment-content"> <p> I'm really struggling to get everything hooked up so it's initialised in the correct order, with one DbContext created per web request or scheduled task. The action filter is my biggest sticking point. </p> <p> I had previously split an IAuthorizationFilter off from a WebAPI AuthorizationFilterAttribute following the advice of this article. I create my filter in WebApiConfig.Register, which is called from MvcApplication.Application_Start(). My problem is that I can't inject my UserService here, because this code is shared across requests, and I want to use one DbContext per request (UserService depends on DbContext). It works if I inject the DependencyResolver, but I realise that's an anti-pattern. </p> <p> What am I missing? Should I new up a DbContext and UserService just for my AuthorizationFilter, that then gets shared acorss requests? Is there somewhere else I can add my ActionFilter, that gets called once per request? I can inject my service into the created AuthorizationFilterAttribute using the steps described at https://michael-mckenna.com/dependency-injection-for-asp-net-web-api-action-filters-in-3-easy-steps/ but would that just make it a captive dependency of an AuthorizationFilterAttribute that gets shared across multiple requests? </p> </div> <div class="comment-date">2019-07-26 17:28 UTC</div> </div> <div class="comment" id="440097c5ad1e47ac8e6fe1d24abffeee"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#440097c5ad1e47ac8e6fe1d24abffeee">#</a></div> <div class="comment-content"> <p> Ian, thank you for writing. It sounds like you need a <a href="/2014/08/24/decoraptor">Decoraptor</a>. </p> </div> <div class="comment-date">2019-07-28 12:58 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Web API Raygun error handler https://blog.ploeh.dk/2014/06/12/web-api-raygun-error-handler 2014-06-12T06:38:00+00:00 Mark Seemann <div id="post"> <p> <em>Adding a Raygun error handler to ASP.NET Web API is easy.</em> </p> <p> In my Pluralsight course on <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">a Functional Architecture with F#</a>, I show you how to add a global error handler to an ASP.NET Web API site. The error handler you see in the course just saves the error as a text file in Azure BLOB storage, which is fine for a demo. For production software, though, you may want something a bit more sophisticated, like <a href="http://raygun.io/">Raygun</a>. </p> <p> Here's how to add a Raygun exception filter to an <a href="http://www.asp.net/web-api">ASP.NET Web API</a> site: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;raygunHandler&nbsp;=&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;System.Web.Http.Filters.IExceptionFilter&nbsp;<span style="color:blue;">with</span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.AllowMultiple&nbsp;=&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.ExecuteExceptionFilterAsync(actionExecutedContext,&nbsp;cancellationToken)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;raygunKey&nbsp;=&nbsp;CloudConfigurationManager.GetSetting&nbsp;<span style="color:#a31515;">&quot;raygunKey&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;raygunClient&nbsp;=&nbsp;Mindscape.Raygun4Net.RaygunClient&nbsp;raygunKey &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Threading.Tasks.Task.Factory.StartNew( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">fun</span>&nbsp;()&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;raygunClient.Send&nbsp;actionExecutedContext.Exception)&nbsp;}</pre> </p> <p> This creates an <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> from the ASP.NET Web API IExceptionFilter interface to a RaygunClient instance. As you can see, <a href="/2014/05/16/configuring-azure-web-jobs">I use CloudConfigurationManager.GetSetting</a> to get the Raygun API key from the configuration store. </p> <p> The only remaining step is to add the error handler to an HttpConfiguration instance: </p> <p> <pre>config.Filters.Add&nbsp;raygunHandler </pre> </p> <p> That's it. Now you can use the Raygun service to manage your errors. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pure DI https://blog.ploeh.dk/2014/06/10/pure-di 2014-06-10T06:10:00+00:00 Mark Seemann <div id="post"> <p> <em>Pure DI is Dependency Injection without a DI Container.</em> </p> <p> <strong>TL;DR: the term <em>Pure DI</em> replaces the term <em>Poor Man's DI</em>.</strong> </p> <p> This post essentially proposes a change of terminology. In <a href="http://amzn.to/12p90MG">my book about Dependency Injection</a> (DI), I was careful to explain the principles and patterns of DI in the pure form, without involving DI Containers. Only in Part 4 do you get extensive coverage of various DI Containers, and even here, what you learn is how the DI principles and patterns map to the various containers. </p> <p> DI is a set of principles and patterns; DI Containers are <em>optional</em> helper libraries. </p> <p> However, when I wrote the book, I made a mistake (I probably made many, but here, I'll address a single, specific mistake): in the book, DI without DI Containers is called <em>Poor Man's DI</em>. There are <a href="http://stackoverflow.com/a/7102651/126014">reasons for that</a>, but eventually, I've learned that Poor Man's DI is poor terminology (pun intended). The problem is that it sounds slightly derogatory, or at least unattractive; it also doesn't communicate the message that DI without a DI Container is, in many cases, <em>better</em> than DI with a DI Container - on the contrary, it sounds like it's not as good. </p> <p> Apart from my book, I've attempted to describe the trade-off involved in going from Poor Man's DI to using a DI Container in various articles: <ul> <li><a href="/2012/11/06/WhentouseaDIContainer">When to use a DI Container</a></li> <li><a href="/2014/06/03/compile-time-lifetime-matching">Compile Time Lifetime Matching</a></li> <li><a href="http://www.infoq.com/articles/Succeeding-Dependency-Injection">Succeeding with Dependency Injection</a></li> </ul> Based on the reactions I've received, it seems like my readers really like their DI Containers. Perhaps they're just afraid of the alternative, because it's called Poor Man's DI. </p> <p> For these reasons, from now on, I'll retire the term <em>Poor Man's DI</em>, and instead start using the term <em><strong>Pure DI</strong></em>. Pure DI is when you use the DI principles and patterns, but not a DI Container; it's what I've been doing for the last 1½ years, as well as many years before I wrote my book. </p> <p> <strong>P.S. 2018-04-15.</strong> Naming is hard. When I came up with the alternative name of <em>Pure DI</em>, I was already in the process of changing my focus to functional programming, but at that time, I'd still not realised how big a role <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a> plays in strictly functional languages like <a href="https://www.haskell.org">Haskell</a>, with its emphasis on <a href="https://en.wikipedia.org/wiki/Pure_function">pure functions</a>. </p> <p> The purity implied by <em>Pure DI</em>, unfortunately, has nothing to do with purity in the functional sense of the word. In fact, <a href="/2017/01/30/partial-application-is-dependency-injection">DI makes everything impure</a>. </p> <p> When I decided on the term <em>Pure DI</em>, I did it because it sounds vaguely like <em>Poor Man's DI</em>, so that it'd be easy to remember. Additionally, I picked the word <em>pure</em> because it can mean <em>essence</em>, and I thought that, in a sense, <em>Pure DI</em> can be viewed as the <a href="https://en.wikipedia.org/wiki/Platonic_idealism">Platonic ideal</a> of DI. Finally, <em>pure</em> sounds like something desirable, so because of those reasons, I believed that it'd be a good term. People seem to have picked it up, so in that sense I think that I chose the right word, but it can be confusing if viewed through the lens of functional programming terminology. </p> <p> <em>Pure DI</em> has nothing to do with pure functions. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Compile-Time Lifetime Matching https://blog.ploeh.dk/2014/06/03/compile-time-lifetime-matching 2014-06-03T10:06:00+00:00 Mark Seemann <div id="post"> <p> <em>When using hand-coded object composition, the compiler can help you match service lifetimes.</em> </p> <p> In my previous post, you learned how easy it is to accidentally misconfigure a DI Container to produce <a href="/2014/06/02/captive-dependency">Captive Dependencies</a>, which are dependencies that are being kept around after they should have been released. This can lead to subtle or catastrophic bugs. </p> <p> This problem is associated with DI Containers, because Container registration APIs let you register services out of order, and with any particular lifestyle you'd like: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ContainerBuilder</span>(); builder.RegisterType&lt;<span style="color:#2b91af;">ProductService</span>&gt;().SingleInstance(); builder.RegisterType&lt;<span style="color:#2b91af;">CommerceContext</span>&gt;().InstancePerDependency(); builder.RegisterType&lt;<span style="color:#2b91af;">SqlProductRepository</span>&gt;().As&lt;<span style="color:#2b91af;">IProductRepository</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;.InstancePerDependency(); <span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;builder.Build();</pre> </p> <p> In this <a href="http://autofac.org">Autofac</a> example, CommerceContext is registered before SqlProductRepository, even though SqlProductRepository is a 'higher-level' service, but ProductService is registered first, and it's even 'higher-level' than SqlProductRepository. A DI Container doesn't care; it'll figure it out. </p> <p> The compiler doesn't care if the various lifetime configurations make sense. As you learned in my <a href="/2014/06/02/captive-dependency">previous article</a>, this particular configuration combination <em>doesn't</em> make sense, but the compiler can't help you. </p> <h3 id="e9a0ff4615a84c9888d2c6ffb621b3d2"> Compiler assistance <a href="#e9a0ff4615a84c9888d2c6ffb621b3d2" title="permalink">#</a> </h3> <p> The overall message in my <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design article series</a> is that you can often design your types in such a way that they are less forgiving of programming mistakes; this enables the <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">compiler to give you feedback faster than you could otherwise have gotten feedback</a>. </p> <p> If, <a href="/2012/11/06/WhentouseaDIContainer/">instead of using a DI Container, you'd simply hand-code the required object composition</a> (also <a href="http://stackoverflow.com/a/7102651/126014">called <em>Poor Man's DI</em> in my book</a>, but <a href="/2014/06/10/pure-di">now called Pure DI</a>), the compiler will make it much harder for you to mismatch object lifetimes. Not impossible, but more difficult. </p> <p> As an example, consider a web-based <a href="/2011/07/28/CompositionRoot">Composition Root</a>. Here, the particular IHttpControllerActivator interface belongs to <a href="http://www.asp.net/web-api">ASP.NET Web API</a>, but it could be any Composition Root: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SomeCompositionRoot</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Singleton-scoped&nbsp;services&nbsp;are&nbsp;declared&nbsp;here...</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">SomeThreadSafeService</span>&nbsp;singleton; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SomeCompositionRoot() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...&nbsp;and&nbsp;(Singleton-scoped&nbsp;services)&nbsp;are&nbsp;initialised&nbsp;here.</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.singleton&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SomeThreadSafeService</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpController</span>&nbsp;Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpControllerDescriptor</span>&nbsp;controllerDescriptor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;controllerType) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Per-Request-scoped&nbsp;services&nbsp;are&nbsp;declared&nbsp;and&nbsp;initialized&nbsp;here</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;perRequestService&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SomeThreadUnsafeService</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>(controllerType&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">FooController</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Transient&nbsp;services&nbsp;are&nbsp;created&nbsp;and&nbsp;directly&nbsp;injected&nbsp;into</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;FooController&nbsp;here:</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FooController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SomeServiceThatMustBeTransient</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SomeServiceThatMustBeTransient</span>()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>(controllerType&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">BarController</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Transient&nbsp;service&nbsp;is&nbsp;created&nbsp;and&nbsp;directly&nbsp;injected&nbsp;into</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;BarController&nbsp;here,&nbsp;but&nbsp;Per-Request-scoped&nbsp;services&nbsp;or</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Singleton-scoped&nbsp;services&nbsp;can&nbsp;be&nbsp;used&nbsp;too.</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BarController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.singleton, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perRequestService, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perRequestService, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SomeServiceThatMustBeTransient</span>()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;Unexpected&nbsp;type!&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;controllerType&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice the following: <ul> <li>There's only going to be a single instance of the SomeCompositionRoot class around, so any object you assign to a <code>readonly</code> field is effectively going to be a Singleton.</li> <li>The Create method is invoked for each request, so if you create objects at the beginning of the Create method, you can reuse them as much as you'd like, but only within that single request. This means that even if you have a service that isn't thread-safe, it's safe to create it at this time. In the example, the BarController depends on two arguments where the Per-Request Service fits, and the instance can be reused. This may seem contrived, but isn't at all if SomeThreadUnsafeService implements more that one (<a href="http://martinfowler.com/bliki/RoleInterface.html">Role</a>) interface.</li> <li>If you need to make a service truly Transient (i.e. it <em>must not</em> be reused at all), you can create it within the constructor of its client. You see an example of this when returning the FooController instance: this example <em>is</em> contrived, but it makes the point: for some unfathomable reason, FooController needs two instances of the same type, but the SomeServiceThatMustBeTransient class must never be shared. It's actually quite rare to have this requirement, but it's easy enough to meet it, if you encounter it.</li> </ul> It's easy to give each service the correct lifetime. Singleton services share the lifetime of the Composition Root, Per-Request services are created each time the Create method is called, and Transient services are created Just-In-Time. All services go out of scope at the correct time, too. </p> <h3 id="320aaf2dc9114f27bb7bc171895678ff"> Commerce example <a href="#320aaf2dc9114f27bb7bc171895678ff" title="permalink">#</a> </h3> <p> In the <a href="/2014/06/02/captive-dependency">previous article</a>, you saw how easy it is to misconfigure a ProductService, because you'd like it to be a Singleton. When you hand-code the composition, it becomes much easier to spot the mistake. You may start like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CommerceCompositionRoot</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ProductService</span>&nbsp;productService; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;CommerceCompositionRoot() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.productService&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ProductService</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpController</span>&nbsp;Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpControllerDescriptor</span>&nbsp;controllerDescriptor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;controllerType) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Implementation&nbsp;follows&nbsp;here...</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> <em>Fortunately</em>, that doesn't even compile, because ProductService doesn't have a parameterless constructor. With a DI Container, you could define ProductService as a Singleton without a compilation error: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StandardKernel</span>(); container.Bind&lt;<span style="color:#2b91af;">ProductService</span>&gt;().ToSelf().InSingletonScope();</pre> </p> <p> If you attempt to do the same with hand-coded composition, it doesn't compile. This is an excellent example of Poka-Yoke Design: design your system in such a way that the compiler can give you as much feedback as possible. </p> <p> Intellisense will tell you that ProductService has dependencies, so your next step may be this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;CommerceCompositionRoot() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.productService&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ProductService</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlProductRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CommerceContext</span>()));&nbsp;<span style="color:green;">//&nbsp;Alarm&nbsp;bell!</span> }</pre> </p> <p> This <em>will</em> compile, but at this point, an alarm bell should go off. You <em>know</em> that you mustn't share CommerceContext across threads, but you're currently creating a single instance. Now it's much clearer that you're on your way to doing something wrong. In the end, you realise, simply by trial and error, that you can't make any part of the ProductService sub-graph a class field, because the leaf node (CommerceContext) isn't thread-safe. </p> <p> Armed with that knowledge, the next step is to create the entire object graph in the Create method, because that's the only safe implementation left: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpController</span>&nbsp;Create( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpControllerDescriptor</span>&nbsp;controllerDescriptor, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;controllerType) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>(controllerType&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">HomeController</span>)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HomeController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ProductService</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlProductRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CommerceContext</span>()))); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Handle&nbsp;other&nbsp;controller&nbsp;types&nbsp;here...</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;Unexpected&nbsp;type!&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;controllerType&quot;</span>); }</pre> </p> <p> In this example, you create the object graph in a single statement, theoretically giving all services the Transient lifestyle. In practice, there's no difference between the Per Request and the Transient lifestyle as long as there's only a single instance of each service for each object graph. </p> <h3 id="6bb5592d6d2740338d1060d3922da49f"> Concluding remarks <a href="#6bb5592d6d2740338d1060d3922da49f" title="permalink">#</a> </h3> <p> Some time ago, I wrote an article on <a href="/2012/11/06/WhentouseaDIContainer">when to use a DI Container</a>. In that article, I attempted to explain how going from Pure DI (hand-coded composition) to a DI Container meant loss of compile-time safety, but I may have made an insufficient job of providing enough examples of this effect. The <a href="/2014/06/02/captive-dependency">Captive Dependency</a> configuration error, and this article together, describe one such effect: with Pure DI, lifetime matching is compiler-assisted, but if you refactor to use a DI Container, you lose the compiler's help. </p> <p> Since I wrote the article on when to use a DI Container, I've only <a href="http://stackoverflow.com/a/23312402/126014">strengthened my preference for Pure DI</a>. Unless I'm writing a very complex code base that could benefit from Convention over Configuration, I don't use a DI Container, but since I explicitly architect my systems to be non-complex these days, I haven't used a DI Container in production code for more than 1½ years. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="53af2eac896244f6a97290fd9c1bfc0a"> <div class="comment-author"><a href="http://stevesspace.com">Steve Leigh</a> <a href="#53af2eac896244f6a97290fd9c1bfc0a">#</a></div> <div class="comment-content"> <p> I don't think it's a problem with the container, but a problem with the registrations. I use a Autofac as my DI Container registration, and I always have a root application lifetime scope, and a separate scope for each request. If the product service is registered in the root scope as single instance, it will throw a <code>DependencyResolutionException</code> </p> <p> In this case, I would have the ProductService registered in the root scope as single instance, and the other types in the request scope. </p> <p> If <code>ProductService</code> is resolved, a <code>DependencyResolutionException</code> is thrown, and the app is unusable - "fail fast" is followed. To fix the issue, the registration needs to be moved to to the request scope. </p> <p>Here's an example of a safe MVC Controller Factory using Autofac.</p> <pre> public class AutofacControllerFactory : DefaultControllerFactory { private readonly IContainer container; private Dictionary&lt;IController, ILifetimeScope&gt; scopes = new Dictionary&lt;IController, ILifetimeScope&gt;(); public AutofacControllerFactory() { var builder = new ContainerBuilder(); RegisterRootTypes(builder); this.container = builder.Build(); } private void RegisterRootTypes(ContainerBuilder builder) { builder.RegisterType&lt;ProductService&gt;().SingleInstance(); builder.RegisterAssemblyTypes(GetType().Assembly) .Where(t =&gt; t.Name.Contains("Controller")) .InstancePerLifetimeScope(); } protected internal override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { var requestScope = container.BeginLifetimeScope(RegisterRequestTypes); var controller = (IController)requestScope.Resolve(controllerType); scopes[controller] = requestScope; return controller; } private void RegisterRequestTypes(ContainerBuilder builder) { builder.RegisterType&lt;CommerceContext&gt;().InstancePerDependency(); builder.RegisterType&lt;SqlProductRepository&gt;().As&lt;IProductRepository&gt;() .InstancePerDependency(); } public override void ReleaseController(IController controller) { scopes[controller].Dispose(); scopes.Remove(controller); } } </pre> <p> Sorry for the lack of code formatting - I'm not sure what you use to format code </p> </div> <div class="comment-date">2014-06-04 13:20 UTC</div> </div> <div class="comment" id="00eecc563091435988ee9b15e821a06a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#00eecc563091435988ee9b15e821a06a">#</a></div> <div class="comment-content"> <p> Steve, thank you for writing. Indeed, you can make a DI Container detect the <a href="/2014/06/02/captive-dependency">Captive Dependency</a> error at run-time. I pointed that out in the defining article about the Captive Dependency problem, and as <em>qujck</em> points out in the comments, <a href="https://simpleinjector.org">Simple Injector</a> has this feature, too. </p> <p> The point with the present article is that, instead of waiting until run-time, you get a chance to learn about potential lifetime mismatches already at design-time. In C#, F#, and other compiled languages, you can't perform a run-time test until you've compiled. While I'm all for <em>fail fast</em>, <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">I don't think it'd be failing fast enough, if you can catch the problem at compile time, but then deliberately wait until run-time</a>. </p> <p> Another concern is to go for the simplest thing that could possibly work. Why use a complex piece of code like your AutofacControllerFactory above, instead of just writing the code directly? It's no secret that I'm not a big fan of the Lifetime Scope idiom, and your code provides an excellent example of how complicated it is. You may have omitted this for the sake of the example, but that code isn't thread-safe; in order to make it thread-safe, you'd need to make it even more complicated. </p> <p> You probably know how to make it thread-safe, as do I, so this isn't an attempt at pointing fingers. The point I'm attempting to make is that, by using a DI Container, you <ul> <li>Add complexity</li> <li>Get slower feedback</li> </ul> There are costs associated with using a DI Container; what are the benefits? </p> </div> <div class="comment-date">2014-06-04 16:45 UTC</div> </div> <div class="comment" id="43fae0345e6d4ab58c5353748bb150d4"> <div class="comment-author"><a href="http://stevesspace.com">Steve Leigh</a> <a href="#43fae0345e6d4ab58c5353748bb150d4">#</a></div> <div class="comment-content"> <p> Thanks for the speedy response, Mark - I can't keep up. I think an issue I have with poor-man's DI is that I'm yet to see it in anything more than a trivial example. Indeed, the only time I have seen it used in a professional context is a 300 line file, 280 lines of which have the 'new' keyword in it, with plenty of repetition. </p> <p> Do you know of any medium sized code bases around that use it to good effect? I'm thinking an application with at least 100 types, I'd like to see how the complexity of the graph is managed. </p> <p> To answer your question, here's the advantages I see of using a container and lifetime scopes. </p> <ul> <li> <p> <strong>Clearer lifetimes: </strong> Your statement that the compiler is detecting the captive dependency isn't quite correct - it's still the developer doing that at design time. They have to see that <code>new CommerceContext()</code> is not a smart thing to do at application start, and move it accordingly. The compiler has nothing to do with that - either way, the check is happening at <em>coding time</em>. Whether that's while typing <code>new CommerceContext()</code> or when typing <code>builder.Register&lt;CommerceContext&gt;()</code>, it's the same thing. </p> <p> I'd argue that the code that registers <code>CommerceContext</code> in an application scope is a much clearer alarm bell. After fixing the issue, you'll end up with the registration appearing in a <code>RegisterRequestScopedStuff()</code> method, which is a much better way to tell future developers to be careful about this guy in the future. </p> </li> <li> <p> <strong>Simplicity: </strong> I would argue that the Autofac controller factory is simpler than the poor mans one. Using poor man style, you have a <code>switch</code> (or bunch of <code>if</code> statements) on the controller type, and need keep track of correct lifetimes in a deeply-nested object graph. I think a (thread safe) dictionary and disposal is significantly simpler that those things - at the very least, has fewer branches - and provides great access points to define expected lifetimes of objects. It probably seems more complicated because there's only a few types, mine has no syntax highlighting (very important for readability!) and I've documented which bits are app-wide and which are request-wide lifetimes, via method names and registration types. </p> </li> <li> <p> <strong>Speed of development: </strong> I find the overall development speed is faster using a container, as you don't have to micromanage the dependencies. While you do get slower feedback times on dependency failures, you have far fewer failures overall. It's been several months since I've seen a <code>DependencyResolutionException</code>. On the flip side, the javascript development I've done (which doesn't use a container) often has a missing a dependency or 2 - which would be equivalent to a compile error in a strongly typed language. </p> <p> What's more, I can write my classes and tests without having to worry about composition until it's time to run the application. To be fair, this is also achieved with good domain/application separation - since the app failing to compile does not prevent the tests from running - but I still like to write tests for my application project. </p> </li> <li> <p> <strong>Disposables: </strong> As you mentioned, my simple example was not thread safe, due to having to store the lifetime scope for disposal when the controller is released. The only reason I need to store that is so Autofac can clean up any IDisposable dependencies I may have, and trivially at that - how do you do this with poor man's DI, while keeping it simple? </p> </li> </ul> <p> If I can wire up Autofac in my application in 10 minutes, have the computer do all the heavy lifting, while making it clearer to myself and future people what I want the lifetimes of things to be, why would I want to manage a dependency graph myself? </p> </div> <div class="comment-date">2014-06-05 13:30 UTC</div> </div> <div class="comment" id="c25de0badb1c456e8f6f913571c4d5f4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c25de0badb1c456e8f6f913571c4d5f4">#</a></div> <div class="comment-content"> <p> Before we continue this discussion, I think it's important to establish <em>how</em> you use a DI Container. If you <a href="/2012/11/06/WhentouseaDIContainer">refer to my article on the <em>benefits</em> of using a DI Container</a>, which approach are you using? </p> </div> <div class="comment-date">2014-06-05 14:22 UTC</div> </div> <div class="comment" id="1d6fb29bd2bf4c3b9e92ae51f4681a6d"> <div class="comment-author"><a href="http://stevesspace.com">Steve Leigh</a> <a href="#1d6fb29bd2bf4c3b9e92ae51f4681a6d">#</a></div> <div class="comment-content"> <p> I'd say I sit somewhere between convention and explicit register, but I guess I disagree about the "pointless" value for it, and place less importance on the value of strong/weak typing. As I said, I very rarely have the dependency exceptions be thrown anyway. In practice, I have a class of services that are wired up by convention (type name ends in "Factory" or "Controller", for example), and explicitly register others. No hard and fast rules about it. </p> </div> <div class="comment-date">2014-06-06 14:00 UTC</div> </div> <div class="comment" id="169b41f3e9ef4991b5e221d4cd8a7dad"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#169b41f3e9ef4991b5e221d4cd8a7dad">#</a></div> <div class="comment-content"> <p> That makes the discussion a little less clear-cut, because you <em>are</em> getting some of the benefits out of Convention over Configuration, but perhaps not as much as you could... Depending on how you put the balance between these two, I would agree with you that using a DI Container is beneficial. </p> <p> My point isn't that there are no benefits from using a DI Container, but that there are also serious disadvantages. The benefits should outweigh the disadvantages, and that is, in my experience, far from given that they do. YMMV. </p> <p> Do I know of any medium-sized code bases that use Pure DI to good effect? Perhaps... I don't know what a 'medium-sized' code base is to you. In any case, while I may know of such code bases, I know of none where the source code is public. </p> <p> 300-odd lines of code for composition sounds like a lot, but as <a href="/2012/11/06/WhentouseaDIContainer">I have previously demonstrated</a>, using Explicit Register will only <em>increase</em> the line count. </p> <p> Another criticism of manual composition is that every time you change something, you'll need to edit the composition code. That's true, but this is equally as true for Explicit Register. The difference is that with manual composition, you learn about this at compile-time, while with Explicit Register, you don't learn about changes until run-time. This, in isolation, is a clear win for manual composition. </p> <p> Now, if you move to Convention over Configuration, this particular criticism of using a DI Container disappears again, but I never claimed anything else. </p> </div> <div class="comment-date">2014-06-07 7:15 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Captive Dependency https://blog.ploeh.dk/2014/06/02/captive-dependency 2014-06-02T13:01:00+00:00 Mark Seemann <div id="post"> <p> <em>A Captive Dependency is a dependency with an incorrectly configured lifetime. It's a typical and dangerous DI Container configuration error.</em> </p> <p> This post is the sixth in a series about <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a>. </p> <p> When you <a href="/2012/11/06/WhentouseaDIContainer">use a Dependency Injection (DI) Container</a>, you should configure it according to the <a href="/2010/09/29/TheRegisterResolveReleasepattern">Register Resolve Release</a> pattern. One aspect of configuration is to manage the lifetime of various services. If you're not careful, though, you may misconfigure lifetimes in such a way that a longer-lived service holds a shorter-lived service captive - often with subtle, but disastrous results. You could call this misconfiguration a <em>Captive Dependency</em>. </p> <p> A major step in applying DI is to <a href="/2011/03/04/Composeobjectgraphswithconfidence">compose object graphs</a>, and service lifetimes in object graphs are hierarchical: </p> <p> <img src="/content/binary/hierarchical-lifetime-nature-of-object-graphs.png" alt="Hierarchical lifetime nature of object graphs"> </p> <p> This figure illustrates the configured and <em>effective</em> lifetimes of an object graph. Node A1 should have a Transient lifetime, which is certainly possible. A new instance of C1 should be created Per Request (if the object graph is part of a web application), which is also possible, because A1 has a shorter lifetime than Per Request. Similarly, only a single instance of B3 should ever be created, which is also possible, because the various instances of C1 can reuse the same B3 instance. </p> <p> The A2 node also has a Singleton lifetime, which means that only a single instance should exist of this object. Because A2 holds references to B1 and A3, these two object are also <em>effectively</em> Singletons. It doesn't matter how you'd like the lifetimes of B1 and A3 to be: the fact is that the <em>single instance</em> of A2 holds on to its injected instances of B1 and A3 means that these instances are going to stick around as long as A2. This effect is transitive, so A2 also causes B2 to have an <em>effective</em> Singleton lifetime. </p> <p> This can be problematic if, for example, B1, A3, or B2 aren't thread-safe. </p> <h3 id="f802b536de9444af95a300c9083983b2"> Commerce example <a href="#f802b536de9444af95a300c9083983b2" title="permalink">#</a> </h3> <p> This may make more sense if you see this in a more concrete setting than just an object graph with A1, A2, B1, etc. nodes, so consider the introductory example from <a href="http://amzn.to/12p90MG">my book</a>. It has a ProductService, which depends on an IProductRepository interface (actually, in the book, the Repository is an Abstract Base Class): </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ProductService</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IProductRepository</span>&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ProductService(<span style="color:#2b91af;">IProductRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Other&nbsp;members&nbsp;go&nbsp;here...</span> }</pre> </p> <p> One implementation of IProductRepository is SqlProductRepository, which itself depends on an Entity Framework context: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SqlProductRepository</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IProductRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">CommerceContext</span>&nbsp;context; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SqlProductRepository(<span style="color:#2b91af;">CommerceContext</span>&nbsp;context) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.context&nbsp;=&nbsp;context; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;IProductRepository&nbsp;members&nbsp;go&nbsp;here...</span> }</pre> </p> <p> The CommerceContext class derives from the Entity Framework <a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.aspx">DbContext</a> class, which, last time I looked, isn't thread-safe. Thus, when used in a web application, it's very important to create a new instance of the CommerceContext class for every request, because otherwise you may experience errors. What's worse is that these errors will be threading errors, so you'll not discover them when you test your web application on your development machine, but when in production, you'll have multiple concurrent requests, and <em>then</em> the application will crash (or perhaps 'just' lose data, which is even worse). </p> <p> (As a side note I should point out that I've used neither Entity Framework nor the Repository pattern for years now, but the example explains the problem well, in a context familiar to most people.) </p> <p> The ProductService class is a stateless service, and therefore thread-safe, so it's an excellent candidate for the Singleton lifestyle. However, as it turns out, that's not going to work. </p> <h3 id="276ed6a0683646a881ac1feff2d3c859"> NInject example <a href="#276ed6a0683646a881ac1feff2d3c859" title="permalink">#</a> </h3> <p> If you want to configure ProductService and its dependencies using <a href="http://www.ninject.org">Ninject</a>, you might accidentally do something like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StandardKernel</span>(); container.Bind&lt;<span style="color:#2b91af;">ProductService</span>&gt;().ToSelf().InSingletonScope(); container.Bind&lt;<span style="color:#2b91af;">IProductRepository</span>&gt;().To&lt;<span style="color:#2b91af;">SqlProductRepository</span>&gt;();</pre> </p> <p> With Ninject you don't need to register concrete types, so there's no reason to register the CommerceContext class; it wouldn't be necessary to register the ProductService either, if it wasn't for the fact that you'd like it to have the Singleton lifestyle. Ninject's default lifestyle is Transient, so that's the lifestyle of both SqlProductRepository and CommerceContext. </p> <p> As you've probably already predicted, the Singleton lifestyle of ProductService captures both the direct dependency IProductRepository, and the indirect dependency CommerceContext: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;actual1&nbsp;=&nbsp;container.Get&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); <span style="color:blue;">var</span>&nbsp;actual2&nbsp;=&nbsp;container.Get&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); <span style="color:green;">//&nbsp;You&#39;d&nbsp;want&nbsp;this&nbsp;assertion&nbsp;to&nbsp;pass,&nbsp;but&nbsp;it&nbsp;fails</span> <span style="color:#2b91af;">Assert</span>.NotEqual(actual1.Repository,&nbsp;actual2.Repository);</pre> </p> <p> The repositories are the same because <code>actual1</code> and <code>actual2</code> are the same instance, so naturally, their constituent components are also the same. </p> <p> This is problematic because CommerceContext (deriving from DbContext) isn't thread-safe, so if you resolve ProductService from multiple concurrent requests (which you could easily do in a web application), you'll have a problem. </p> <p> The immediate fix is to make this entire sub-graph Transient: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StandardKernel</span>(); container.Bind&lt;<span style="color:#2b91af;">ProductService</span>&gt;().ToSelf().InTransientScope(); container.Bind&lt;<span style="color:#2b91af;">IProductRepository</span>&gt;().To&lt;<span style="color:#2b91af;">SqlProductRepository</span>&gt;();</pre> </p> <p> Actually, since Transient is the default, stating the lifetime is redundant, and can be omitted: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StandardKernel</span>(); container.Bind&lt;<span style="color:#2b91af;">ProductService</span>&gt;().ToSelf(); container.Bind&lt;<span style="color:#2b91af;">IProductRepository</span>&gt;().To&lt;<span style="color:#2b91af;">SqlProductRepository</span>&gt;();</pre> </p> <p> Finally, since you don't <em>have</em> to register concrete types with Ninject, you can completely omit the ProductService registration: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">StandardKernel</span>(); container.Bind&lt;<span style="color:#2b91af;">IProductRepository</span>&gt;().To&lt;<span style="color:#2b91af;">SqlProductRepository</span>&gt;();</pre> </p> <p> This works: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;actual1&nbsp;=&nbsp;container.Get&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); <span style="color:blue;">var</span>&nbsp;actual2&nbsp;=&nbsp;container.Get&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); <span style="color:#2b91af;">Assert</span>.NotEqual(actual1.Repository,&nbsp;actual2.Repository);</pre> </p> <p> While the Captive Dependency error is intrinsically tied to using a DI Container, it's by no means particular to Ninject. </p> <h3 id="a7cbdcffa66d476dbd238d2669419b25"> Autofac example <a href="#a7cbdcffa66d476dbd238d2669419b25" title="permalink">#</a> </h3> <p> It would be unfair to leave you with the impression that this problem is a problem with Ninject; it's not. All DI Containers I know of have this problem. <a href="http://autofac.org">Autofac</a> is just another example. </p> <p> Again, you'd like ProductService to have the Singleton lifestyle, because it's thread-safe, and it would be more efficient that way: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ContainerBuilder</span>(); builder.RegisterType&lt;<span style="color:#2b91af;">ProductService</span>&gt;().SingleInstance(); builder.RegisterType&lt;<span style="color:#2b91af;">SqlProductRepository</span>&gt;().As&lt;<span style="color:#2b91af;">IProductRepository</span>&gt;(); builder.RegisterType&lt;<span style="color:#2b91af;">CommerceContext</span>&gt;(); <span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;builder.Build();</pre> </p> <p> Like Ninject, the default lifestyle for Autofac is Transient, so you don't have to explicitly configure the lifetimes of SqlProductRepository or CommerceContext. On the other hand, Autofac requires you to register all services in use, even when they're concrete classes; this is the reason you see a registration statement for CommerceContext as well. </p> <p> The problem is exactly the same as with Ninject: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;actual1&nbsp;=&nbsp;container.Resolve&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); <span style="color:blue;">var</span>&nbsp;actual2&nbsp;=&nbsp;container.Resolve&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); <span style="color:green;">//&nbsp;You&#39;d&nbsp;want&nbsp;this&nbsp;assertion&nbsp;to&nbsp;pass,&nbsp;but&nbsp;it&nbsp;fails</span> <span style="color:#2b91af;">Assert</span>.NotEqual(actual1.Repository,&nbsp;actual2.Repository);</pre> </p> <p> The reason is the same as before, as is the solution: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;builder&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ContainerBuilder</span>(); builder.RegisterType&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); builder.RegisterType&lt;<span style="color:#2b91af;">SqlProductRepository</span>&gt;().As&lt;<span style="color:#2b91af;">IProductRepository</span>&gt;(); builder.RegisterType&lt;<span style="color:#2b91af;">CommerceContext</span>&gt;(); <span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;builder.Build(); <span style="color:blue;">var</span>&nbsp;actual1&nbsp;=&nbsp;container.Resolve&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); <span style="color:blue;">var</span>&nbsp;actual2&nbsp;=&nbsp;container.Resolve&lt;<span style="color:#2b91af;">ProductService</span>&gt;(); <span style="color:#2b91af;">Assert</span>.NotEqual(actual1.Repository,&nbsp;actual2.Repository);</pre> </p> <p> Notice that, because the default lifetime is Transient, you don't have to state it while registering any of the services. </p> <h3 id="489c77f181a14c22ac1bddd8c031d759"> Concluding remarks <a href="#489c77f181a14c22ac1bddd8c031d759" title="permalink">#</a> </h3> <p> You can re-create this problem with any major DI Container. The problem isn't associated with any particular DI Container, but simply the fact that there <a href="/2012/11/06/WhentouseaDIContainer">are trade-offs associated with using a DI Container</a>, and one of the trade-offs is a reduction in <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">compile-time feedback</a>. The way typical DI Container registration APIs work, they can't easily detect this lifetime configuration mismatch. </p> <p> It's been a while since I last did a full survey of the .NET DI Container landscape, and back then (when I wrote my book), no containers could detect this problem. Since then, I believe <a href="http://docs.castleproject.org/Windsor.MainPage.ashx">Castle Windsor</a> has got some Captive Dependency detection built in, but I admit that I'm not up to speed; other containers may have this feature as well. </p> <p> When I wrote my book some years ago, I considered including a description of the Captive Dependency configuration error, but for various reasons, it never made it into the book: <ul> <li>As far as I recall, it was <a href="http://kozmic.net">Krzysztof Koźmic</a> who originally made me aware of this problem. In emails, we debated various ideas for a name, but we couldn't really settle on something catchy. Since I don't like to describe something I can't name, it never really made it into the book.</li> <li>One of the major goals of the book was to explain DI as a set of principles and patterns decoupled from DI Containers. The Captive Dependency problem is specifically associated with DI Containers, so it didn't really fit into the book.</li> </ul> Since then, I've thought of the name <em>Captive Dependency</em>, which may not be super-catchy, but at least accurately describes the problem. A longer-lived object (e.g. a Singleton) holds a shorter-lived object captive, past its due release time. Although the shorter-lived object should be released, it's not, because of a bureaucratic error. </p> <p> In a follow-up post to this, I'll demonstrate why <a href="/2014/06/03/compile-time-lifetime-matching">you don't have the same problem when you hand-code your object graphs</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="a66899b6fca7472e88877e9aa7bd42b5"> <div class="comment-author"><a href="https://simpleinjector.org/">qujck</a> <a href="#a66899b6fca7472e88877e9aa7bd42b5">#</a></div> <div class="comment-content"> <p><a href="https://simpleinjector.org/">Simple Injector</a> has built in support for a number of container verifications including lifestyle mismatches (Captive Dependency is a lifestyle mismatch) through its <a href="http://simpleinjector.readthedocs.io/en/latest/diagnostics.html">Diagnostic Services</a>.</p> <p>The configuration for Simple Injector looks like this:</p> <pre><span style="color:blue;">var</span>&nbsp;container&nbsp;=&nbsp;<span style="color:blue;">new</span> Container(); container.RegisterSingle&lt;<span style="color:#2b91af;">ProductService</span>&gt(); container.Register&lt;<span style="color:#2b91af;">IProductRepository</span>,&nbsp;<span style="color:#2b91af;">SqlProductRepository</span>&gt;(); container.Register&lt;<span style="color:#2b91af;">CommerceContext</span>&gt;();</pre> <p>The crucial difference with Simple Injector is that once you have finished configuring the container you make a call to the <code>Verify()</code> method to catch misconfigurations such as Captive Dependency.</p> <p>Here's an example test to demonstrate that the container correctly identifies the lifestyle mismatch:</p> <pre>container.Verify(); <span style="color:blue;">var</span>&nbsp;results&nbsp;=&nbsp;<span style="color:#2b91af;">Analyzer</span>.Analyze(container); <span style="color:#2b91af;">Assert</span>.That(results[0].Description, Is.StringContaining(<span style="color:#a31515;">&quot;CaptiveDependency&quot;</span>)); </pre></div> <div class="comment-date">2014-06-02 20:07 UTC</div> </div> <div class="comment" id="67049c8576aa4ce0b567b8005b5d09b6"> <div class="comment-author">bitbonk <a href="#67049c8576aa4ce0b567b8005b5d09b6">#</a></div> <div class="comment-content"> <p> And for completeness we should also mention how to solve the captive dependency problem. From the really awsome <a href="http://simpleinjector.readthedocs.io/en/latest/LifestyleMismatches.html">SimpleInjector documentation:</a> </p> <ul style="font-style: italic;"> <li>Change the lifestyle of the component to a lifestyle that is as short or shorter than that of the dependency.</li> <li>Change the lifestyle of the dependency to a lifestyle as long or longer than that of the component.</li> <li>Instead of injecting the dependency, inject a factory for the creation of that dependency and call that factory every time an instance is required.</li> </ul> <p> For the above example you would probably want to introduce a factory for the DbContexts. </p> </div> <div class="comment-date">2017-02-28 08:30 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Feedback on ASP.NET vNext Dependency Injection https://blog.ploeh.dk/2014/05/26/feedback-on-aspnet-vnext-dependency-injection 2014-05-26T20:26:00+00:00 Mark Seemann <div id="post"> <p> <em>ASP.NET vNext includes a Dependency Injection API. This post offers feedback on the currently available code.</em> </p> <p> As part of Microsoft's new openness, the ASP.NET team have made the <a href="https://github.com/aspnet/home">next version of ASP.NET available on GitHub</a>. Obviously, it's not yet done, but I suppose that the reasons for this move is to get early feedback, as well as perhaps take contributions. This is an extremely positive move for the ASP.NET team, and I'm very grateful that they have done this, because it enables me to provide early feedback, and offer my help. </p> <p> It looks like one of the proposed new features of the next version of ASP.NET is a library or API simply titled <a href="https://github.com/aspnet/DependencyInjection">Dependency Injection</a>. In this post, I will provide feedback to the team on that particular sub-project, in the form of an open blog post. <a href="http://forums.asp.net/post/5704684.aspx">The contents of this blog post is also cross-posted</a> to the <a href="http://forums.asp.net/1255.aspx/1?ASP+NET+vNext">official ASP.NET vNext forum</a>. </p> <h3 id="d4c8caf5fb064451b807aec43e7f89e1"> Dependency Injection support <a href="#d4c8caf5fb064451b807aec43e7f89e1" title="permalink">#</a> </h3> <p> The details on the motivation for the <em>Dependency Injection</em> library are sparse, but I assume that the purpose is provide 'Dependency Injection support' to ASP.NET. If so, that motivation is laudable, because <a href="http://www.infoq.com/articles/Succeeding-Dependency-Injection">Dependency Injection (DI) is the proper way to write loosely coupled code</a> when using Object-Oriented Design. </p> <p> Some parts of the ASP.NET family already have DI support; personally, I'm most familiar with ASP.NET MVC and ASP.NET Web API. Other parts have proven rather hostile towards DI - most notably ASP.NET Web Forms. The problem with Web Forms is that Constructor Injection is impossible, because the Web Forms framework doesn't provide a hook for creating new Page objects. </p> <h3 id="d4217bf33c5f46418fc948f7bbc9acb8"> My interpretation <a href="#d4217bf33c5f46418fc948f7bbc9acb8" title="permalink">#</a> </h3> <p> As far as I can tell, the current <em>ASP.NET Dependency Injection</em> code defines an interface for creating objects: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ITypeActivator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>&nbsp;CreateInstance( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IServiceProvider</span>&nbsp;services, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;instanceType, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:blue;">object</span>[]&nbsp;parameters); }</pre> </p> <p> In addition to this central interface, there are other interfaces that enable you to configure a 'service collection', and then there are <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapters</a> for <ul> <li>Autofac</li> <li>Ninject</li> <li>StructureMap</li> <li>Unity</li> <li>Caste Windsor</li> </ul> As far as I can tell, there's no <em>web</em> code in the <em>ASP.NET Dependency Injection</em> code base. In other words, this is a poster example of a <a href="/2014/05/19/conforming-container">Conforming Container</a>. </p> <h3 id="748faedeed7b4516bd6f71aa91b09d75"> My recommendations <a href="#748faedeed7b4516bd6f71aa91b09d75" title="permalink">#</a> </h3> <p> It's an excellent idea to add 'Dependency Injection support' to ASP.NET, for the few places where it's not already present. However, as I've previously explained, a Conforming Container isn't the right solution. The right solution is to <a href="/2014/05/19/di-friendly-framework">put the necessary extensibility points</a> into the framework: <ul> <li>ASP.NET MVC already has a good extensibility point in the IControllerFactory interface. I recommend keeping this interface, and other interfaces in MVC that play a similar role.</li> <li>ASP.NET Web API already has a good extensibility point in the IHttpControllerActivator interface. I recommend keeping this interface, and other interfaces in Web API that play a similar role.</li> <li>ASP.NET Web Forms have no extensibility point that enables you to create custom Page objects. I recommend adding an IPageFactory interface, as described in my article about <a href="/2014/05/19/di-friendly-framework">DI-Friendly frameworks</a>. Other object types related to Web Forms, such as Object Data Sources, suffer from the same shortcoming, and should have similar factory interfaces.</li> <li>There may be other parts of ASP.NET with which I'm not particularly familiar (SignalR?), but they should all follow the same pattern of defining <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factories</a> for user classes, in the cases where these don't already exist.</li> </ul> In addition to adding these required extensibility points, I recommend completely abandoning the project of defining a Conforming Container. The extensibility points should be added where they're used - the MVC Factories as part of MVC, the Web Form Factories as part of Web Forms, etc. This will have the added benefit of making the <em>ASP.NET Dependency Injection</em> project redundant. Less code is better than more code. <blockquote> "perfection is attained not when there is nothing more to add, but when there is nothing more to remove." - Antoine de Saint Exupéry </blockquote> These are my general recommendations to the team, but if desired, I'd also like to offer my assistance with any particular issues the team might encounter. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. DI-Friendly Framework https://blog.ploeh.dk/2014/05/19/di-friendly-framework 2014-05-19T09:10:00+00:00 Mark Seemann <div id="post"> <p> <em>How to create a Dependency Injection-friendly software framework.</em> </p> <p> It seems to me that every time a development organisation wants to add 'Dependency Injection support' to a framework, all too often, the result is a <a href="/2014/05/19/conforming-container">Conforming Container</a>. In this article I wish to describe good alternatives to this anti-pattern. </p> <p> In a previous article I covered <a href="/2014/05/19/di-friendly-library">how to design a Dependency Injection-friendly library</a>; in this article, I will deal with frameworks. The <a href="http://stackoverflow.com/a/2190658/126014">distinction I usually make</a> is: <ul> <li>A <strong>Library</strong> is a reusable set of types or functions you can use from a wide variety of applications. The application code initiates communication with the library and invokes it.</li> <li>A <strong>Framework</strong> consists of one or more libraries, but the difference is that <a href="http://stackoverflow.com/a/3227404/126014">Inversion of Control</a> applies. The application registers with the framework (often by implementing one or more interfaces), and the framework calls into the application, which may call back into the framework. A framework often exists to address a particular general-purpose Domain (such as web applications, mobile apps, workflows, etc.).</li> </ul> In my article about the <a href="/2014/05/19/conforming-container">Conforming Container anti-pattern</a>, I already covered some general reason why attempting to create an abstraction over DI Containers is a bad idea, but when it comes to frameworks, some extra concerns arise. </p> <h3 id="15b8d9b2820a4ad698061c92d42f0ddd"> The composition challenge <a href="#15b8d9b2820a4ad698061c92d42f0ddd" title="permalink">#</a> </h3> <p> One of the most challenging aspects of writing a framework is that the framework designers can't predict what users will want to do. Often, a framework defines a way for you to interact with it: <ul> <li>Implement an interface</li> <li>Derive from a base class</li> <li>Adorn your classes or methods with a particular attribute</li> <li>Name your classes according to some naming convention</li> </ul> Common for all these approaches is, however, that the user of the framework develops some classes, and the framework then has to create instances of those classes. Obviously, the framework doesn't know anything about custom user classes, so it'll need some way of creating those instances. </p> <p> <img src="/content/binary/framework-sequence-diagram.png" alt="Framework sequence diagram"> </p> <p> Once the framework has an instance of the custom user class, it can easily start using it by invoking methods defined by the interface the class implements, etc. The difficult part is creating the instance. By default, most frameworks require that a custom class has a default (parameterless) constructor, but <a href="/2011/05/30/DesignSmellDefaultConstructor">that may be a design smell</a>, and doesn't fit with the Constructor Injection pattern. Such a requirement is a sensible default, but isn't Dependency Injection-friendly; in fact, it's an example of the Constrained Construction anti-pattern, which you can read about in <a href="http://amzn.to/12p90MG">my book</a>. </p> <p> Most framework designers realize this and resolve to add Dependency Injection support to the framework. Often, in the first few iterations, they get it right! </p> <h3 id="e7496d9d45cd4140a8fd60729068cef4"> Abstractions and ownership <a href="#e7496d9d45cd4140a8fd60729068cef4" title="permalink">#</a> </h3> <p> If you examine the sequence diagram above, you should realize one thing: the framework is the <em>client</em> of the custom user code; the custom user code provides the services for the framework. In most cases, the custom user code exposes itself as a service to the framework. Some examples may be in order: <ul> <li>In ASP.NET MVC, user code implements the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.icontroller.aspx">IController</a> interface, although this is most commonly done by deriving from the abstract <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.aspx">Controller</a> base class.</li> <li>In ASP.NET Web API, user code implements the <a href="http://msdn.microsoft.com/en-us/library/system.web.http.controllers.ihttpcontroller.aspx">IHttpController</a> interface, although this is most commonly done by deriving from the abstract <a href="http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.aspx">ApiController</a> class.</li> <li>In Windows Presentation Foundation, user code derives from the <a href="http://msdn.microsoft.com/en-us//library/System.Windows.Window.aspx">Window</a> class.</li> </ul> The framework code doesn't know anything about custom user classes, but when they implement the appropriate interface, the framework talks to those interfaces. </p> <p> There's an extremely important point hidden here: although it looks like a framework has to deal with the unknown, all the <em>requirements</em> of the framework are known: <ul> <li>The <em>framework</em> defines the interface or base class</li> <li>The <em>framework</em> creates instances of the custom user classes</li> <li>The <em>framework</em> invokes methods on the custom user objects</li> </ul> The <em>framework</em> is the client, and the framework defines the interface. That's exactly how it should be. In <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices</a>, Robert C. Martin defines interface ownership as <blockquote> "clients [...] own the abstract interfaces" </blockquote> This is a quote from chapter 11, which is about the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>, so it all fits. </p> <p> Notice what the framework <em>does</em> in the list above. Not only does it <em>use</em> the custom user objects, it also <em>creates</em> instances of the custom user classes. This is the tricky part, where many framework designers have a hard time seeing past the fact that the custom user code is unknown. However, from the perspective of the framework, the concrete type of a custom user class is irrelevant; it just needs to create an instance of it, but treat it as the well-known interface it implements. <ul> <li>The client owns the interface</li> <li>The <em>framework</em> is the client</li> <li>The framework knows what <em>it</em> needs, not what user code needs</li> <li>Thus, framework interfaces should be defined by what the framework needs, not as a general-purpose interface to deal with user code</li> <li>Users know much better what user code needs than the framework can ever hope to do</li> </ul> The framework owns the interface for creating those objects, and it shouldn't be complicated; in essence, it should look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFrameworkControllerFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IFrameworkController</span>&nbsp;Create(<span style="color:#2b91af;">Type</span>&nbsp;controllerType); }</pre> </p> <p> assuming that the interface that the user code must implement is called IFrameworkController. </p> <p> The custom user class may contain one or more disposable objects, so in order to prevent resource leaks, the framework must also provide a hook for decommissioning: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFrameworkControllerFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IFrameworkController</span>&nbsp;Create(<span style="color:#2b91af;">Type</span>&nbsp;controllerType); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Release(<span style="color:#2b91af;">IFrameworkController</span>&nbsp;controller); }</pre> </p> <p> In this expanded iteration of the Abstract Factory, the contract is that the framework will invoke the Release method when it's finished with a particular IFrameworkController instance. </p> <p> <img src="/content/binary/framework-sequence-diagram-with-release-hook.png" alt="Framework sequence diagram with release hook"> </p> <p> Some framework designers attempt to introduce a 'more sophisticated' lifetime model, but there's no reason for that. This Create/Release design is simple, easy to understand, works very well, and fits perfectly into the <a href="/2010/09/29/TheRegisterResolveReleasepattern">Register Resolve Release</a> pattern, since it provides hooks for the Resolve and Release phases. </p> <p> ASP.NET MVC 1 and 2 provided <em>flawless</em> examples of such Abstract Factories in the form of the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.icontrollerfactory(v=vs.100).aspx">IControllerFactory</a> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IControllerFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IController</span>&nbsp;CreateController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RequestContext</span>&nbsp;requestContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;controllerName); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;ReleaseController(<span style="color:#2b91af;">IController</span>&nbsp;controller); }</pre> </p> <p> Unfortunately, in ASP.NET MVC 3, a completely unrelated third method was added to that interface; it's still useful, but not as clean as before. </p> <p> Framework designers ought to stop here. With such an Abstract Factory, they have <em>perfect</em> Dependency Injection support. If a user wants to hand-code the composition, he or she can implement the Abstract Factory interface. Here's an ASP.NET 1 example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">PoorMansCompositionRoot</span>&nbsp;:&nbsp;<span style="color:#2b91af;">DefaultControllerFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">IController</span>,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IDisposable</span>&gt;&gt;&nbsp;disposables; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:blue;">object</span>&nbsp;syncRoot; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;PoorMansCompositionRoot() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.syncRoot&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">object</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.disposables&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">IController</span>,&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IDisposable</span>&gt;&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:#2b91af;">IController</span>&nbsp;GetControllerInstance( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RequestContext</span>&nbsp;requestContext,&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;controllerType) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(controllerType&nbsp;==&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">HomeController</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connStr&nbsp;=&nbsp;<span style="color:#2b91af;">ConfigurationManager</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ConnectionStrings[<span style="color:#a31515;">&quot;postings&quot;</span>].ConnectionString; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ctx&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PostingContext</span>(connStr); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sqlChannel&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlPostingChannel</span>(ctx); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sqlReader&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SqlPostingReader</span>(ctx); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;validator&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultPostingValidator</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;validatingChannel&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ValidatingPostingChannel</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validator,&nbsp;sqlChannel); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;controller&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HomeController</span>(sqlReader,&nbsp;validatingChannel); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">lock</span>&nbsp;(<span style="color:blue;">this</span>.syncRoot) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.disposables.Add(controller, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IDisposable</span>[]&nbsp;{&nbsp;sqlChannel,&nbsp;sqlReader&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;controller; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">base</span>.GetControllerInstance(requestContext,&nbsp;controllerType); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ReleaseController(<span style="color:#2b91af;">IController</span>&nbsp;controller) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">lock</span>&nbsp;(<span style="color:blue;">this</span>.syncRoot) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;d&nbsp;<span style="color:blue;">in</span>&nbsp;<span style="color:blue;">this</span>.disposables[controller]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d.Dispose(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In this example, I derive from <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultcontrollerfactory(v=vs.100).aspx">DefaultControllerFactory</a>, which implements the IControllerFactory interface - it's a little bit easier than implementing the interface directly. </p> <p> In this example, the Composition Root only handles a single user Controller type (HomeController), but I'm sure you can extrapolate from the example. </p> <p> If a developer rather prefers using a DI Container, that's also perfectly possible with the Abstract Factory approach. Here's another ASP.NET 1 example, this time with Castle Windsor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">WindsorCompositionRoot</span>&nbsp;:&nbsp;<span style="color:#2b91af;">DefaultControllerFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IWindsorContainer</span>&nbsp;container; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;WindsorCompositionRoot(<span style="color:#2b91af;">IWindsorContainer</span>&nbsp;container) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(container&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;container&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.container&nbsp;=&nbsp;container; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">protected</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:#2b91af;">IController</span>&nbsp;GetControllerInstance( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">RequestContext</span>&nbsp;requestContext,&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;controllerType) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;(<span style="color:#2b91af;">IController</span>)<span style="color:blue;">this</span>.container.Resolve(controllerType); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">override</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ReleaseController(<span style="color:#2b91af;">IController</span>&nbsp;controller) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.container.Release(controller); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice how seamless the framework's Dependency Injection support is: the framework has no knowledge of Castle Windsor, and Castle Windsor has no knowledge of the framework. The small WindsorCompositionRoot class acts as an <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> between the two. </p> <h3 id="89661e776ccd4707b53c5925caed5413"> Resist the urge to generalize <a href="#89661e776ccd4707b53c5925caed5413" title="permalink">#</a> </h3> <p> If frameworks would only come with the appropriate hooks in the form of Abstract Factories with Release methods, they'd be perfect. </p> <p> Unfortunately, as a framework becomes successful and grows, more and more types are added to it. Not only (say) Controllers, but Filters, Formatters, Handlers, and whatnot. A hypothetical XYZ framework would have to define Abstract Factories for each of these extensibility points: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IXyzControllerFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IXyzController</span>&nbsp;Create(<span style="color:#2b91af;">Type</span>&nbsp;controllerType); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Release(<span style="color:#2b91af;">IXyzController</span>&nbsp;controller); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IXyzFilterFactory</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IXyzFilter</span>&nbsp;Create(<span style="color:#2b91af;">Type</span>&nbsp;fiterType); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Release(<span style="color:#2b91af;">IXyzFilter</span>&nbsp;filter); } <span style="color:green;">//&nbsp;etc.</span></pre> </p> <p> Clearly, that seems repetitive, so it's no wonder that framework designers look at that repetition and wonder if they can generalize. The appropriate responses to this urge, are, in prioritised order: <ol> <li>Resist the urge to generalise, and define each Abstract Factory as a separate interface. That design is easy to understand, and users can implement as many or as few of these Abstract Factories as they want. In the end, frameworks are designed for the framework users, not for the framework developers.</li> <li>If absolutely unavoidable, define a generic Abstract Factory.</li> </ol> Under no circumstance is a <a href="/2014/05/19/conforming-container">Conforming Container</a> the appropriate response. </p> <p> Many distinct, but similar Abstract Factory interfaces may be repetitive, but that's unlikely to hurt the user. A good framework provides optional extensibility points - it doesn't force users to relate to all of them at once. As an example, I'm a fairly satisfied user of the <a href="http://www.asp.net/web-api">ASP.NET Web API</a>, but while I create lots of Controllers, and the occasional Exception Filter, I've yet to write my first custom Formatter. I only <a href="/2012/09/28/DependencyInjectionandLifetimeManagementwithASP.NETWebAPI">add a custom IHttpControllerActivator</a> for my Controllers. Although (unfortunately) ASP.NET Web API has had a Conforming Container in the form of the <a href="http://msdn.microsoft.com/en-us/library/system.web.http.dependencies.idependencyresolver.aspx">IDependencyResolver</a> interface since version 1, I've never used it. In a properly designed framework, a Conforming Container is utterly redundant. </p> <p> If the framework <em>must</em> address the apparent DRY violation of multiple similar Abstract Factory definitions, an acceptable solution is a generic interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;Create(<span style="color:#2b91af;">Type</span>&nbsp;itemType); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Release(T&nbsp;item); }</pre> </p> <p> This type of generic Factory is <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">generally benign</a>, although it may hurt discoverability, because a generic type looks as though you can use anything for the type argument T, where, in fact, the framework only needs a finite set of Abstract Factories, like <ul> <li>IFactory&lt;IXyzController&gt;</li> <li>IFactory&lt;IXyzFilter&gt;</li> <li>IFactory&lt;IXyzFormatter&gt;</li> <li>IFactory&lt;IXyzHandler&gt;</li> </ul> </p> <p> In the end, though, users will need to inform the framework about their custom factories, so this discoverability issue can be addressed. A framework usually defines an extensibility point where users can tell it about their custom extensions. An example of that is ASP.NET MVC's <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbuilder.aspx">ControllerBuilder</a> class. Although I'm not too happy about the use of a Singleton, it's hard to do something wrong: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;controllerFactory&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">PoorMansCompositionRoot</span>(); <span style="color:#2b91af;">ControllerBuilder</span>.Current.SetControllerFactory(controllerFactory);</pre> </p> <p> Unfortunately, some frameworks attempt to generalize this extensibility point. As an example, in ASP.NET Web API, you'll have to use <a href="http://msdn.microsoft.com/en-us/library/system.web.http.controllers.servicescontainer.replace.aspx">ServicesContainer.Replace</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Replace(<span style="color:#2b91af;">Type</span>&nbsp;serviceType,&nbsp;<span style="color:blue;">object</span>&nbsp;service) </pre> </p> <p> Although it's easy enough to <em>use</em>: </p> <p> <pre>configuration.Services.Replace( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IHttpControllerActivator</span>), &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CompositionRoot</span>(<span style="color:blue;">this</span>.eventStore,&nbsp;<span style="color:blue;">this</span>.eventStream,&nbsp;<span style="color:blue;">this</span>.imageStore));</pre> </p> <p> It's not particularly discoverable, because you'll have to resort to the documentation, or trawl through the (fortunately open source) code base, in order to discover that there's an IHttpControllerActivator interface you'd like to replace. The Replace method gives the impression that you can replace any Type, but in practice, it only makes sense to replace a few well-known interfaces, like IHttpControllerActivator. </p> <p> Even with a generic Abstract Factory, a much more discoverable option would be to expose all extensible services as strongly-typed members of a configuration object. As an example, the hypothetical XYZ framework could define its configuration API like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">XyzConfiguration</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IXyzController</span>&gt;&nbsp;ControllerFactory&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IXyzFilter</span>&gt;&nbsp;FilterFactory&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;etc.</span> }</pre> </p> <p> Such use of Property Injection enables users to override only those Abstract Factories they care about, and leave the rest at their defaults. Additionally, it's easy to enumerate all extensibility options, because the XyzConfiguration class provides a one-stop place for all extensibility points in the framework. </p> <h3 id="8a46865eec2e4194a326ddee5a1c47dd"> Define attributes without behaviour <a href="#8a46865eec2e4194a326ddee5a1c47dd" title="permalink">#</a> </h3> <p> Some frameworks provide extensibility points in the form of attributes. ASP.NET MVC, for example, defines various <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.filterattribute.aspx">Filter</a> attributes, such as <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx">[Authorize]</a>, <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx">[HandleError]</a>, <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute.aspx">[OutputCache]</a>, etc. Some of these attributes contain <em>behaviour</em>, because they implement interfaces such as <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.iauthorizationfilter.aspx">IAuthorizationFilter</a>, <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.iexceptionfilter.aspx">IExceptionFilter</a>, and so on. </p> <p> Attributes with behaviour is a bad idea. Due to compiler limitations (at least in both C# and F#), you can only provide constants and literals to an attribute. That effectively rules out Dependency Injection, but if an attribute contains behaviour, it's guaranteed that some user comes by and wants to add some custom behaviour in an attribute. The only way to add 'Dependency Injection support' to attributes is through a <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">static Service Locator</a> - an exceptionally toxic design. Attribute designers should avoid this. This <a href="http://www.infoq.com/articles/Succeeding-Dependency-Injection">is not Dependency Injection support; it's Service Locator support</a>. There's no reason to bake in Service Locator support in a framework. People who deliberately want to hurt themselves can always add a static Service Locator by themselves. </p> <p> Instead, attributes should be designed without behaviour. Instead of putting the behaviour in the attribute itself, a custom attribute should only provide <em>metadata</em> - after all, that's the original raison d'être of attributes. </p> <p> Attributes with metadata can then be detected and handled by normal services, which enable normal Dependency Injection. See <a href="http://stackoverflow.com/a/7194467/126014">this Stack Overflow answer</a> for an ASP.NET MVC example, or my article on <a href="/2014/06/13/passive-attributes">Passive Attributes</a> for a Web API example. </p> <h3 id="fdf370973a4b4a02b04224950d4bfd3b"> Summary <a href="#fdf370973a4b4a02b04224950d4bfd3b" title="permalink">#</a> </h3> <p> A framework must expose appropriate extensibility points in order to be useful. The best way to support Dependency Injection is to <strong>provide an Abstract Factory with a corresponding Release method</strong> for each custom type users are expected to create. This is the simplest solution. It's extremely versatile. It has few moving parts. It's easy to understand. It enables gradual customisation. </p> <p> Framework users who don't care about Dependency Injection at all can simply ignore the whole issue and use the framework with its default services. Framework users who <a href="http://stackoverflow.com/a/23312402/126014">prefer to hand-code object composition</a>, can implement the appropriate Abstract Factories by writing custom code. Framework users who prefer to use their DI Container of choice can implement the appropriate Abstract Factories as Adapters over the container. </p> <p> That's all. There's no reason to make it more complicated than that. There's particularly no reason to force a <a href="/2014/05/19/conforming-container">Conforming Container</a> upon the users. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2b7096ad0cc14321ac0d258b167b49be"> <div class="comment-author"><a href="http://www.shiningtreasures.com/">Shad Storhaug</a> <a href="#2b7096ad0cc14321ac0d258b167b49be">#</a></div> <div class="comment-content">This is a very good post, and I am glad you finally created it because after reading your book on how to configure applications, it was still unclear how to tackle the task of creating a DI-friendly framework. I ended up creating a Conforming Container of some sort. Although it is not required because there is an internal poor man's DI container, when you replace the internal container, you must provide the entire DI configuration using a 3rd party DI container. Although this article is helping to steer me back on track, there are a few things that are still unclear that your post didn't address.<br> <br> 1. A framework generally must have some sort of initializer, particularly if it must do something like add route values to MVC (which must be done during a particular point in the application lifecycle). This startup code must be placed in the composition root of the application. Considering that the framework should have no knowledge of the composition root of the application, how best can this requirement be met? The only thing I have come up with is to add a static method that must be in the application startup code and using WebActivator to get it running.<br> 2. Sort of related to the first issue, how would it be possible to address the extension point where abstract factories can be injected without providing a static method? I am considering expanding the static method from #1 to include an overload that accepts an Action&lt;IConfiguration&gt; as a parameter. The developer can then use that overload to create a method Configure(IConfiguration configuration) in their application to set the various abstract factory (in the IConfiguration instance, of course). The IConfiguration interface would contain well named members to set specific factories, so it is easy to discover what factory types can be provided. Could this idea be improved upon?<br> 3. Considering that my framework relies on the .NET garbage collector to dispose of objects that were created by a given abstract factory, what pattern can I adapt to ensure the framework always calls Release() at the right time? A concrete example would seem to be in order.</div> <div class="comment-date">2014-08-10 08:54 UTC</div> </div> <div class="comment" id="628da205902e43658d41cc56d55f0fb6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#628da205902e43658d41cc56d55f0fb6">#</a></div> <div class="comment-content"> <p> Shad, thank you for writing. From your questions it's a bit unclear to me whether you're writing a framework or a library. Although you write <em>framework</em>, your questions sound like it's a library... or at least, <em>if</em> you're writing a framework, it sounds like you're writing a sub-framework for another framework (MVC). Is that correct? </p> <p> Re: <strong>1</strong>. It's true that a framework needs some sort of initializer. In the ideal world, it would look something like <code>new MyFrameworkRunner().Run();</code>, and you would put this single line of code in the entry point of your application (its Main method). Unfortunately, ASP.NET doesn't work that way, so we have to work with the cards we're dealt. Here, the entry point is Application_Start, so if you need to initialise something, this is where you do it. </p> <p> The initialisation method can be a static or instance method. </p> <p> Re: <strong>2</strong>. That sounds reasonable, but it depends upon where your framework stores the custom configuration. If you add a method overload to a static method, it almost indicates to me that the framework's configuration is stored in shared state, which is never attractive. An alternative is to utilise the Dependency Inversion Principle, and instead inject any custom configuration into the root of the framework itself: <code>new MyFrameworkRunner(someCustomCoonfiguration).Run();</code> </p> <p> Re: <strong>3</strong>. A framework is responsible for the lifetime of the objects it creates. If it creates objects, it must also provide an extensibility point for decommissioning them after use. This is the reason I strongly recommend (in this very article) that an Abstract Factory for a framework must always have a Release method in addition to the Create method. </p> <p> A concrete example is difficult to provide when the question is abstract... </p> </div> <div class="comment-date">2014-08-14 14:35 UTC</div> </div> <div class="comment" id="d567c4eb624a4f32b316118221c962c2"> <div class="comment-author"><a href="http://www.shiningtreasures.com/">Shad Storhaug</a> <a href="#d567c4eb624a4f32b316118221c962c2">#</a></div> <div class="comment-content"> <p> If you want to take a look, the framework I am referring to is called <a href="https://github.com/maartenba/MvcSiteMapProvider/">MvcSiteMapProvider</a>. I would definitely categorize it as a sub-framework of MVC because it <em>can</em> rely on the host application to provide service instances (although it doesn't <em>have</em> to). It has a static entry point to launch it's composition root (primarily because WebActivator requires there to be a static method, and WebActivator can launch the application without the need for the NuGet package to modify the Global.asax file directly), but the inner workings rely (almost) entirely on instances and constructor injection. There is still some refactoring to be done on the HTML helpers to put all of the logic into replaceable instances, which I plan to do in a future major version. </p> <p> Since it is about 90% of the way there already, my plan is to modify the internal poor-man's DI container to accept injected factory instances to provide the alternate implementations. A set of default factories will be created during initialization, and then it will pass these instances (through the IConfiguration variable) out to the host application where it can replace or wrap the factories. After the host does what it needs to, the services will be wired up in the poor man's DI container and then its off to the races. I think this can be done <em>without</em> dropping support for the existing Conforming Container, meaning I don't need to wait for a major release to implement it. </p> <p> Anyway, you have adequately answered my 2 questions about initialization and I think I am now on the right track. You also gave me some food for thought as how to accomplish this without making it static (although ultimately <em>some</em> wrapper method will need to be static in order to make it work with WebActivator). </p> <p> As for my 3rd question, you didn't provide a sufficient answer. However, I took a peek at the MVC source code to see how the default IControllerFactory ReleaseController() method was implemented, and it is similar to your PoorMansCompositionRoot example above (sort of). They just check to see if IController will cast to IDisposable and call Dispose() if it does. I guess that was the general pattern I was asking for, and from your example it looks like you are in agreement with Microsoft on the approach. </p> </div> <div class="comment-date">2015-08-14 20:27 UTC</div> </div> <div class="comment" id="e3c321a38f1949fbb52f30c8648c62d2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e3c321a38f1949fbb52f30c8648c62d2">#</a></div> <div class="comment-content"> <p> Shad, I don't exactly recall how DefaultControllerFactory.ReleaseController is implemented, but in general, only type-checking for IDisposable and calling Dispose may be too simplistic a view to take in the general case. As I explain in chapter 8 in <a href="http://amzn.to/12p90MG">my book</a>, <em>releasing</em> an object graph isn't always equivalent to disposal. In the general case, you also need to take into account the extra dimension of the various lifetimes of objects in an object graph. Some object may have a longer lifetime scope, so even when you invoke Release on them, their time isn't up yet, and therefore it would be a bug to dispose of them. </p> <p> This is one of the reasons a properly design Abstract Factory interface (for use with .NET) must have a Release method paired with its Create method. Only the factory knows if it actually created an object (as opposed to reused an existing object), so only the factory knows when it's appropriate to dispose of it again. </p> </div> <div class="comment-date">2014-08-17 14:41 UTC</div> </div> <div class="comment" id="cb55955aec824c578093a303a112cf31"> <div class="comment-author"><a href="http://rpajak.com">Robert Pajak</a> <a href="#cb55955aec824c578093a303a112cf31">#</a></div> <div class="comment-content"> <p> Dear Mark, </p> <p> Concering the the type of generic IFactory, I think that aplying a Marker Interface for T whould make the implementation very discoverable and also type-safe. </p> <p> What is your opinion about it? </p> </div> <div class="comment-date">2014-09-25 21:14 UTC</div> </div> <div class="comment" id="9f94abd030554c65b0c9523a59fd2ee7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9f94abd030554c65b0c9523a59fd2ee7">#</a></div> <div class="comment-content"> <p> Robert, thank you for writing. What advantage would a Marker Interface provide? </p> </div> <div class="comment-date">2014-09-26 14:27 UTC</div> </div> <div class="comment" id="3ca0e32a9cd042f08c4b919ff95395dd"> <div class="comment-author"><a href="http://rpajak.com">Robert Pajak</a> <a href="#3ca0e32a9cd042f08c4b919ff95395dd">#</a></div> <div class="comment-content"> <p> 1. From framework perspective - less code. 2. From client perspective - more consistent API. We can see all the possible hooks just by investigating which interfaces of the Framwork implements the marker interface - even the IntelliSence would show the possibilities of the factories which can be implemented. As I write the comment, I see also some drawbacks - the IFactory should generally not work with generic which is the marker interface itself and also for some client interfaces that would implement the marker interface. However still I find it much better ans safer than just having a generic IFactory without ANY constraints. </p> <p> Additionally maybe you could also enchance this post with information how to Bootstrap the framwork? For example I extremely like <a href="https://github.com/thecodejunkie">thecodejunkie's</a> way which he presented on his <a href="http://www.youtube.com/watch?v=7jg0u-YaRxQ#t=1915">Guerilla Framework Design presentation</a>. </p> <p> Or also maybe you could give some examples how could the framework developers use the container? Personally the only way I see that it could be done is to use some "embedded" container like TinyIoC. This is the way how <a href="http://nancyfx.org/">Nancy</a> is done. </p> </div> <div class="comment-date">2014-09-26 20:48 UTC</div> </div> <div class="comment" id="6ce7f33a54bf437aaa6ae824578f473a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6ce7f33a54bf437aaa6ae824578f473a">#</a></div> <div class="comment-content"> <p> How does a Marker Interface lead to less framework code? It has no behaviour, so it's hard for me to imagine how it leads to less code. From a client perspective, it also sounds rather redundant to me. How can we see the possible hooks in the framework by looking for a Marker Interface? Aren't all public interfaces defined by a framework hooks? </p> <p> What do you mean: "how could the framework developers use the container"? Which container? The whole point of this very post is to explain to programmers how to design a DI Friendly framework without relying on a container - any container. </p> </div> <div class="comment-date">2014-09-30 17:08 UTC</div> </div> <div class="comment" id="14ae6a78a6a0488287f0df22d15e0cea"> <div class="comment-author"><a href="https://twitter.com/bitbonk">bitbonk</a> <a href="#14ae6a78a6a0488287f0df22d15e0cea">#</a></div> <div class="comment-content"> <p> Hi Mark, thank you for this very helpful blog post. I still have two questions though: <ol type="1"> <li>To create the default implementations (that are provided with the framework) of the factories exposed in XyzConfiguration, it might be necessary to compose a complex dependency graph first, because the the default implementations of the factories themselves have dependencies. So there must be code in the <i>framework</i> that does this composition. At the same time this composition code should be extensible and it should be possible let the DI container of the customers choice to this composition. Can you sektch out how one would design for such a scenario or are you aware of a framework that does this well?</li> <li>Once the factories in XyzConfiguration are configured and initialized, it seems that all framework classes that need one of those factories get a dependency on the XyzConfiguration, because that's the place where to get the factories from. This would be <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern/">Service Locator antipattern</a> how would I avoid this?</li> </ol> </p> </div> <div class="comment-date">2015-09-04 13:40 UTC</div> </div> <div class="comment" id="4d58dec423fc49d09c924a9876d0fbbf"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4d58dec423fc49d09c924a9876d0fbbf">#</a></div> <div class="comment-content"> <p> bitbonk, thank you for writing. </p> <p> <strong>Re 1:</strong> A framework can compose any default complex object graph it wants to. If a framework developer is worried about performance, he or she can always make the default configuration a lazily initialized Singleton: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">XyzConfiguration</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IXyzController</span>&gt;&nbsp;ControllerFactory&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IXyzFilter</span>&gt;&nbsp;FilterFactory&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;etc.</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">XyzConfiguration</span>&gt;&nbsp;defaultConfiguration&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">XyzConfiguration</span>&gt;(()&nbsp;=&gt;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">XyzConfiguration</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ControllerFactory&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">XyzControllerFactory</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Foo</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Bar</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Baz</span>(<span style="color:green;">/*&nbsp;Etc.&nbsp;*/</span>)))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Assign&nbsp;FilterFactory&nbsp;in&nbsp;the&nbsp;same&nbsp;manner</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">XyzConfiguration</span>&nbsp;Default &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;defaultConfiguration.Value;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Since the default value is only initialized if it's used, there's no cost associated with it. The XyzConfiguration class still has a public constructor, so if a framework user doesn't want to use the default, he or she can always create a new instance of the class, and pass that configuration to the framework when it's bootstrapping. The user can even use XyzConfiguration.Default as a starting point, and only tweak the properties he or she wants to tweak. </p> <p> While XyzConfiguration.Default defines default graphs, all the constituent elements (XyzControllerFactory, Foo, Bar, Baz, etc.) are public classes with public constructors, so a framework user can always duplicate the graph him- or herself. If the framework developers want to make it easier to tweak the default graph, they can supply a facade using one of the options outlines in my <a href="/2014/05/19/di-friendly-library">companion article about DI-friendly libraries</a>. </p> <p> <strong>Re 2:</strong> Such an XyzConfiguration class <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">isn't a Service Locator</a>. It's a concrete, well-known, <em>finite</em> collection of Abstract Factories, whereas <a href="/2014/05/15/service-locator-violates-solid">a Service Locator is an unknown, <em>infinite</em> set of Abstract Factories</a>. </p> <p> Still, I would recommend that framework developers adhere to the <a href="https://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> and only take dependencies on those Abstract Factories they need. If a framework feature needs an IFactory&lt;IXyzController&gt;, then that's what it should take in via its constructor. The framework should pass to that constructor <code>configuration.ControllerFactory</code> instead of the entire <code>configuration</code> object. </p> </div> <div class="comment-date">2015-09-06 14:41 UTC</div> </div> <div class="comment" id="7262eaa446f5410f8c1ede769be15ef2"> <div class="comment-author"><a href="https://twitter.com/bitbonk">bitbonk</a> <a href="#7262eaa446f5410f8c1ede769be15ef2">#</a></div> <div class="comment-content"> <p> Thanks Mark for your answer, it all makes sense to me but my first question was more geared towards the DI container. For the composition of the default dependency graphs, the (my) framework needs to provide three things <i>at the same time</i>: <ol type="1"> <li>Means that helps the user build default combination(s) of dependencies for common scenarios without forcing the user to use (or depend on) <i>any</i> DI container. I can see how this can easily be achieved by using the factory approach you mentioned or by using facade or constructor chaining as you mentioned and in your <a href="/2014/05/19/di-friendly-library">companion article about DI-friendly libraries</a></li> <li>Means that helps the user build the default combination(s) of dependencies for common scenarios using the DI container of the user's choice (i.e. an existing container instance that was already created and is used for the rest of the application dependencies too). The user might want to do this because she wants to resolve some the dependencies that have been registered by the framework using the DI container of her application. I can see how this could be achieved by introducing some sort of framework-specific DI container abstraction and provide implementations for common containers (like XyzFramework.Bootstrappers.StructureMap or XyzFramework.Bootstrappers.SimpleInjector ...). It looks like this is how it is done in <a href="https://github.com/NancyFx/Nancy.Bootstrappers.StructureMap">NancyFx</a>.</li> <li>Means that helps the user modify and adapt the default combination(s) of dependencies for common scenarios using that DI container. The user might want to modify just parts of a default dependency graph or wants to intercept just some of the default dependencies. The user should be able to do this without having to reimplement the whole construction logic. Again NancyFx seems to do this by introducing a <a href="https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Bootstrapper/ContainerRegistration.cs">DI container abstraction</a>.</li> </ol> </p> <p> I find it particularly challenging to come up with a good design that meets all three of these requirements. I don't really have a question for you here, because the answer will most likely have to be: "it depends". But feel free to comment, if you have any additional thoughts that are worth sharing. </p> </div> <div class="comment-date">2015-09-07 13:45 UTC</div> </div> <div class="comment" id="e8e68fc2c9584546bbd396cfdd0e9b33"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e8e68fc2c9584546bbd396cfdd0e9b33">#</a></div> <div class="comment-content"> <p> Before we continue this discussion, I find myself obliged to point out that you ought have a compelling reason to create a <em>framework</em>. Tomas Petricek has a great article that explains why you should <a href="http://tomasp.net/blog/2015/library-frameworks">favour libraries over frameworks</a>. The article uses F# for code examples, but the arguments apply equally to C# and Object-Oriented Design. </p> <p> I have a hard time coming up with a single use case where a framework would be the correct design decision, but perhaps your use case is a genuine case for making a framework... </p> <p> That said, I don't understand your second bullet point. If all the classes involved in building those default object graphs are public, a user can always register them with their DI Container of choice. Why would you need a container abstraction (which is <a href="/2014/05/19/conforming-container">a poor idea</a>) for that? </p> <p> The third bullet point makes me uneasy as well. It seems to me that the entire premise in this, and your previous, comment is that the default object graphs are deep and complex. Is that really necessary? </p> <p> Still, if you want to make it easy for a user to modify the default factories, you can supply a Facade or Fluent Builder as <a href="/2014/05/19/di-friendly-library">already outlined</a>. The user can use that API to tweak the defaults. Why would the user even wish to involve a DI Container in that process? </p> <p> For the sake of argument, let's assume that this is somehow necessary. The user can still hook into the provided API and combine that with a DI Container. Here's a Castle Windsor example: </p> <p> <pre>container.Register(<span style="color:#2b91af;">Component</span> &nbsp;&nbsp;&nbsp;&nbsp;.For&lt;<span style="color:#2b91af;">IFactory</span>&lt;<span style="color:#2b91af;">IXyzController</span>&gt;&gt;() &nbsp;&nbsp;&nbsp;&nbsp;.UsingFactoryMethod(k&nbsp;=&gt;&nbsp;k &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Resolve&lt;<span style="color:#2b91af;">XyzControllerFactoryBuilder</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WithBar(k.Resolve&lt;<span style="color:#2b91af;">IBar</span>&gt;()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build()));</pre> </p> <p> This utilises the API provided by the framework, but still enables you to resolve a custom IBar instance with the DI Container. All other objects built by XyzControllerFactoryBuilder are still handled by that Fluent Builder. </p> </div> <div class="comment-date">2015-09-09 10:52 UTC</div> </div> <div class="comment" id="6f2ce92f95fd45a8aeb570286fdc30b2"> <div class="comment-author"><a href="https://www.cuttingedge.it/blogs/steven/">Steven van Deursen</a> <a href="#6f2ce92f95fd45a8aeb570286fdc30b2">#</a></div> <div class="comment-content"> <p> I like to respond to bitbonk's comment where he states that "this could be achieved by introducing some sort of framework-specific DI container abstraction" where he mentions that NancyFx takes this approach. </p> <p> It is important to note that NancyFx does not contain an adapter for Simple Injector and it has proven to be impossible to create an adapter for NancyFx's Conforming Container. The Conforming Container always expects certain behavior of the underlying container, and there will always be at least one container that fails to comply to this contract. In the case of NancyFx, it is Simple Injector. This is proof that the Conforming Container <em>is</em> an anti-pattern. </p> <p> Besides the Conforming Container design, Nancy does however contain the right abstractions to intercept the creation of Nancy's root types (modules). This has actually made it <a href="https://github.com/NancyFx/Nancy/issues/1227#issuecomment-163279040">very straightforward</a> to plugin Simple Injector into NancyFx. It's just a matter of implementing a custom <code>INancyModuleCatalog</code> and optionally an <code>INancyContextFactory</code>. </p> <p> Although we might think that the Conforming Container and having proper abstractions can live side-by-side, there is a serious downside. In the case of NancyFx for instance, the designers were so focussed on their Conforming Container design, that they had a hard time imagining that anyone would want to do without it. This lead to the situation that they <a href="https://github.com/NancyFx/Nancy/issues/1227#issuecomment-163020810">themselves didn't even realize</a> that a container could actually be plugged-in without the use of the Conforming Container. This caused me figure this out by myself. </p> <p> So the risk is that the focus on the Conforming Container causes the designers of the framework to forget about developers that can't comply with their Conforming Container abstraction. This makes working without such adapter an afterthought to the designers. We are seeing this exact thing happening within Microsoft with their new .NET Core framework. Although at the moment of writting (which is 6 months after .NET Core has been released) only one of the leading DI containers has official support for the .NET Core Conforming Container, Microsoft makes little efforts in trying to improve the situation for the rest of the world, because, as the lead architect behind their Conforming Container <a href="https://github.com/aspnet/Mvc/issues/5403#issuecomment-254769973">quite accurately described</a> it himself: "it's just my bias towards conforming containers clouding my vision". </p> </div> <div class="comment-date">2016-12-04 10:21 UTC</div> </div> <div class="comment" id="5d2ce92f75ed45a8beb570286fdc30b2"> <div class="comment-author">John Softmore <a href="#5d2ce92f75ed45a8beb570286fdc30b2">#</a></div> <div class="comment-content"> <p> Dear Mark, all, <br> I would appreciate some advice. Thanks in advance. <br> I am refactoring a small real-time graphics framework, and am unsure the best way to structure the code at the top level. <br> I doubt I have described things very well, but fingers crossed... <br> I provide a bit of background on how I got to this point, but ultimately my question is one about where exact a framework's own internal wiring happens, particularly the registering or creation of internal components that the USER should not have knowledge of. <br> </p> <p> Framework or Library?: <br> The first advice I read was actually to avoid using a FRAMEWORK structure where possible. However, although it is not of sufficient breadth to describe my code as an "Engine", it needs to control the looping of the application (and entire lifetime). I do NOT deem it realistic to refactor into a LIBRARY. I particularly do not believe the "solution" to structuring the update() part of a game engine, as described here, is simple to use or elegant. <br> <a href="http://tomasp.net/blog/2015/library-frameworks/">Article Advocating Libraries over Frameworks</a> <br> Therefore, I decided to stick with the FRAMEWORK approach. <br> </p> <p> Framework Aims: <br> The Framework Accepts (or will Create, as per your recommendation) an object implementating IApplication <br> The Framework calls methods in IApplication such as Load(), Update(), Draw() <br> The Framework provides Functions to the Application via a number of interfaces: IGraphics, IInput etc <br> The Framework should follow DI principles to ease Testing <br> The USER should NOT be required to wire-up the internal components of the framework. <br> The API / Interface usage be simple for the USER: i.e. Framework.Run(Application); <br> The API should be clean - i.e. I would prefer if the USER were unable to see / instantiate internal framework objects (i.e. preference is for INTERNAL rather than PUBLIC preference) <br> I would like to use an IOC container <br> </p> <p> My Initial Approach: <br> My approach before finding this article was therefore to have something like: <br> Assembly0: Application <br> Assembly1: Contains Interfaces, & Framework incl. a Static Loader Method <br> Assembly2: Framework Tests <br> The application would create IApplication interface, and feed it via the Static Loader Method into Assembly1 <br> In Assembly1 (the framework), there would be a framework composition root that would spool up the engine, either by using an IOC container and registering components, or manual DI <br> </p> <p> Problems Encountered First, in relation to testing (how I got to the issue, rather than the issuer itself): <br> It was at this point, whilst trying to ensure the API was clean, that I realised I was unable to use INTERNAL only classes and still test them without add "Internals Visible Attribute". This felt a bit dirty (BUT might be the right solution). I thought I could pull out the interface to another third assembly, but given the references needed i always ended up being visible to the user. Anyway, that's besides the point slightly... <br> It was at this point I started to look for some advice on how to structure a framework, and I ran across this article... <br> So it became clear that I should make the Framework Create the Application via a Factory... which OK is fine <br> but i get stuck when trying to figure out how to adhere too - DI containers / composition roots should only exist in the application. There should only be one container / composition root. It should not live in the framework <br> Where i am now stuck: <br> I don't have any clue how the approach presented in your article solves the internal setup and wiring of the framework itself? <br> I do not see how I can rely on the FRAMEWORK USER to choose either simple DI or using an IOC container, without them needing to know how to wire up different components inside my framework. This does NOT seem EASY to me. Should the user really have to register things like the Graphics Implementation against the IGraphics interface, etc? <br> Is it really that bad to use two IOCs? One only internal to the framework itself, used in a static set up method? Am I missing something obvious about how I should structure my code? <br> It seems one option (at least on the Library-form side) is to use a facade that offers default wiring options. I assume this sits in the framework assembly, containers a factory method, and is OK to use a DI container within? I am then unsure exactly how that would pair with the abstract factories in this advice that create the applications. <br> Perhaps there is a good example of a framework (or trivial example) that can be pointed too? <br> Thanks again. I assume there is no perfect solution but appreciate your advice. </p> </div> <div class="comment-date">2019-01-20 16:34 UTC</div> </div> <div class="comment" id="5d5ce62f85ec45a8beb570286fdc40b2"> <div class="comment-author">John Softmore <a href="#5d5ce62f85ec45a8beb570286fdc40b2">#</a></div> <div class="comment-content"> <p> Dear Mark,<br><br> My apologies for the somewhat rambled query above. <br><br> I believe it might be more simple if I show you my simplified version of a proposed structure for a framework, that takes a user defined application.<br><br> <a href="https://github.com/quietjohn/frameworkstructuring">Repo With Simple Framework Example</a><br><br> The Assemblies involved are (named more simply in the repo):<br> UserApplication<br> ApiInterface<br> Framework<br> FrameworkTesting<br><br> I would be very interested to know if you see this framework structure as misguided or plain smelly.<br><br> I am finding this topic quite interesting, and would be facinated to see an improved approach. <br><br> If it turns out not to stink too badly, I will press on and use this structure<br><br> I have read your DI book (potentially missing some key points given I am asking these questions...), and so would really appreciate and respect your opinion. <br><br> This structure features:<br> - A user created application object inheriting the application interface <br> - A static method in a Facade class to help wire up the framework (is a static compoisiton route that could include an IoC container)<br> - Allows application -side composition root by user NOT using the Facade helper<br> - A somewhat 'clean' public interface / API whereby internal framework components are atleast deeper in the namespace tree<br> - Full testability of internal components and external services<br> - Constructor injection to achieve DI<br><br> How this structure breaks advice:<br> - Using the Helper Facade could result in any IoC container not being in only composition route (and perhaps the application employs another container)<br> - The Framework does not instantiate the Custom Application Objects<br><br> For the latter, I am unsure how to create something Framework Side that is a factory to create a custom user class without referencing the application namespace and adding bi-drectional dependency on App -> Framework<br><br> I am sure I am missing some obvious stuff.<br><br> Thanks again<br><br> John<br><br> </p> </div> <div class="comment-date">2019-01-23 22:11 UTC</div> </div> <div class="comment" id="bb2028866881498c91a06ba622b16497"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bb2028866881498c91a06ba622b16497">#</a></div> <div class="comment-content"> <p> John, thank you for writing. When I look at your GitHub repository, I think that it looks fine. I particularly like that if I had to work with something like this, I get to control the lifetime of all my object graphs. I just have to implement <code>IApp</code> and call <code>CompositionFacade.Run</code> with it. </p> <p> This makes it more like a library than a framework, although I do understand that the intent is that <code>Run</code> executes for the lifetime of the application. </p> <p> This means that if I need to implement <code>IApp</code> with a complex graph of objects, I can do that. In my <code>Main</code> method, I can compose my object graph with <a href="/2014/06/10/pure-di">Pure DI</a>, or I can leverage a DI Container if I so choose. </p> <p> What happens inside of <code>CompositionFacade.Run</code> is completely opaque to me, which implies good encapsulation. </p> <p> I do, however, consider a separate <code>interface</code> library redundant. What purpose does it serve? </p> </div> <div class="comment-date">2019-01-27 12:45 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. DI-Friendly Library https://blog.ploeh.dk/2014/05/19/di-friendly-library 2014-05-19T08:20:00+00:00 Mark Seemann <div id="post"> <p> <em>How to create a Dependency Injection-friendly software library.</em> </p> <p> In <a href="http://amzn.to/12p90MG">my book</a>, I go to great lengths to explain how to develop loosely coupled <em>applications</em> using various Dependency Injection (DI) patterns, including the <a href="/2011/07/28/CompositionRoot">Composition Root</a> pattern. With the great emphasis on applications, I didn't particularly go into details about making DI-friendly libraries. Partly this was because I didn't think it was necessary, but since <a href="http://stackoverflow.com/a/2047657/126014">one of my highest voted Stack Overflow answers deal with this question</a>, it may be worth expanding on. </p> <p> In this article, I will cover libraries, and in <a href="/2014/05/19/di-friendly-framework">a later article I will deal with frameworks</a>. The <a href="http://stackoverflow.com/a/2190658/126014">distinction I usually make</a> is: <ul> <li>A <strong>Library</strong> is a reusable set of types or functions you can use from a wide variety of applications. The application code initiates communication with the library and invokes it.</li> <li>A <strong>Framework</strong> consists of one or more libraries, but the difference is that <a href="http://stackoverflow.com/a/3227404/126014">Inversion of Control</a> applies. The application registers with the framework (often by implementing one or more interfaces), and the framework calls into the application, which may call back into the framework. A framework often exists to address a particular general-purpose Domain (such as web applications, mobile apps, workflows, etc.).</li> </ul> </p> <p> Most well-designed libraries are already DI-friendly - particularly if they follow the <a href="http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)">SOLID principles</a>, because the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> (the <em>D</em> in SOLID) is the guiding principle behind DI. </p> <p> Still, it may be valuable to distil a few recommendations. </p> <h3 id="2a9335d4857f43f5bc3d25347a915711"> Program to an interface, not an implementation <a href="#2a9335d4857f43f5bc3d25347a915711" title="permalink">#</a> </h3> <p> If your library consists of several collaborating classes, define proper interfaces between these collaborators. This enables clients to redefine part of your library's behaviour, or to slide cross-cutting concerns in between two collaborators, using a <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a>. </p> <p> Be sure to define these interfaces as <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a>. </p> <p> An example of a small library that follows this principle is <a href="https://github.com/ploeh/Hyprlinkr">Hyprlinkr</a>, which defines two interfaces used by the main RouteLinker class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDictionary</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">object</span>&gt;&nbsp;GetRouteValues( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">MethodCallExpression</span>&nbsp;methodCallExpression); }</pre> </p> <p> and </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Rouple</span>&nbsp;Dispatch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">MethodCallExpression</span>&nbsp;method, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDictionary</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">object</span>&gt;&nbsp;routeValues); }</pre> </p> <p> This not only makes it easier to develop and maintain the library itself, but also makes it more flexible for users. </p> <h3 id="08a62bdad9044fa2aba2f2069fee64ec"> Use Constructor Injection <a href="#08a62bdad9044fa2aba2f2069fee64ec" title="permalink">#</a> </h3> <p> Favour the Constructor Injection pattern over other injection patterns, because of its simplicity and <a href="/2011/05/30/DesignSmellDefaultConstructor">degree of encapsulation</a>. </p> <p> As an example, Hyprlinkr's main class, RouteLinker, has this primary constructor: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;valuesQuery; <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span>&nbsp;dispatcher; <span style="color:blue;">public</span>&nbsp;RouteLinker( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;routeValuesQuery, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span>&nbsp;dispatcher) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(request&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;request&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(routeValuesQuery&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;routeValuesQuery&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dispatcher&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;dispatcher&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.request&nbsp;=&nbsp;request; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.valuesQuery&nbsp;=&nbsp;routeValuesQuery; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.dispatcher&nbsp;=&nbsp;dispatcher; }</pre> </p> <p> Notice that it follows <a href="https://vuscode.wordpress.com">Nikola Malovic</a>'s <a href="https://vuscode.wordpress.com/2009/10/16/inversion-of-control-single-responsibility-principle-and-nikola-s-laws-of-dependency-injection">4th law of IoC</a> that <a href="/2011/03/03/InjectionConstructorsshouldbesimple">Injection Constructors should be simple</a>. </p> <p> Although not strictly required in order to make a library DI-friendly, expose every injected dependency as an <a href="/2013/04/04/structural-inspection">Inspection Property</a> - it will make the library easier to use when composed in one place, but used in another place. Again, Hyprlinkr does that: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;RouteValuesQuery { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.valuesQuery;&nbsp;} }</pre> </p> <p> and so on for its other dependencies, too. </p> <h3 id="63c51703322f4e12bbc4cb95ebfc416f"> Consider an Abstract Factory for short-lived objects <a href="#63c51703322f4e12bbc4cb95ebfc416f" title="permalink">#</a> </h3> <p> Sometimes, your library must create short-lived objects in order to do its work. Other times, the library can only create a required object at run-time, because only at run-time is all required information available. You can use an <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">Abstract Factory</a> for that. </p> <p> The Abstract Factory doesn't always <em>have</em> to be named XyzFactory; in fact, Hyprlinkr's IRouteDispatcher interface is an Abstract Factory, although it's in disguise because it has a different name. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Rouple</span>&nbsp;Dispatch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">MethodCallExpression</span>&nbsp;method, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IDictionary</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">object</span>&gt;&nbsp;routeValues); }</pre> </p> <p> Notice that the return value of an Abstract Factory <em>doesn't have to</em> be another interface instance; in this case, it's an instance of the concrete class Rouple, which is a data structure without behaviour. </p> <h3 id="fdebfaa652534344a5277d98917f6850"> Consider a Facade <a href="#fdebfaa652534344a5277d98917f6850" title="permalink">#</a> </h3> <p> If some objects are difficult to construct, because their classes have complex constructors, consider supplying a <a href="http://en.wikipedia.org/wiki/Facade_pattern">Facade</a> with a good default combination of appropriate dependencies. Often, a simple alternative to a Facade is <a href="http://stackoverflow.com/a/6739953/126014">Constructor Chaining</a>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;RouteLinker(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<span style="color:blue;">this</span>(request,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultRouteDispatcher</span>()) { } <span style="color:blue;">public</span>&nbsp;RouteLinker(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request,&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;routeValuesQuery) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<span style="color:blue;">this</span>(request,&nbsp;routeValuesQuery,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultRouteDispatcher</span>()) { } <span style="color:blue;">public</span>&nbsp;RouteLinker(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request,&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span>&nbsp;dispatcher) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<span style="color:blue;">this</span>(request,&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ScalarRouteValuesQuery</span>(),&nbsp;dispatcher) { } <span style="color:blue;">public</span>&nbsp;RouteLinker( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;routeValuesQuery, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span>&nbsp;dispatcher) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(request&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;request&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(routeValuesQuery&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;routeValuesQuery&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(dispatcher&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;dispatcher&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.request&nbsp;=&nbsp;request; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.valuesQuery&nbsp;=&nbsp;routeValuesQuery; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.dispatcher&nbsp;=&nbsp;dispatcher; }</pre> </p> <p> Notice how the Routelinker class provides appropriate default values for those dependencies it can. </p> <p> However, a Library with a more complicated API could potentially benefit from a proper Facade. One way to make the API's extensibility points discoverable is by implementing the Facade as a Fluent Builder. The following RouteLinkerBuilder <em>isn't</em> part of Hyprlinkr, because I consider the Constructor Chaining alternative simpler, but it <em>could</em> look like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">RouteLinkerBuilder</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;valuesQuery; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span>&nbsp;dispatcher; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;RouteLinkerBuilder() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<span style="color:blue;">this</span>(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ScalarRouteValuesQuery</span>(),&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultRouteDispatcher</span>()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;RouteLinkerBuilder( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;valuesQuery, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span>&nbsp;dispatcher) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.valuesQuery&nbsp;=&nbsp;valuesQuery; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.dispatcher&nbsp;=&nbsp;dispatcher; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">RouteLinkerBuilder</span>&nbsp;WithValuesQuery(<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;newValuesQuery) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RouteLinkerBuilder</span>(newValuesQuery,&nbsp;<span style="color:blue;">this</span>.dispatcher); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">RouteLinkerBuilder</span>&nbsp;WithDispatcher(<span style="color:#2b91af;">IRouteDispatcher</span>&nbsp;newDispatcher) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RouteLinkerBuilder</span>(<span style="color:blue;">this</span>.valuesQuery,&nbsp;newDispatcher); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">RouteLinker</span>&nbsp;Create(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RouteLinker</span>(request,&nbsp;<span style="color:blue;">this</span>.valuesQuery,&nbsp;<span style="color:blue;">this</span>.dispatcher); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IRouteValuesQuery</span>&nbsp;ValuesQuery &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.valuesQuery;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IRouteDispatcher</span>&nbsp;Dispatcher &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span>&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.dispatcher;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This has the advantage that it's easy to get started with the library: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;linker&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RouteLinkerBuilder</span>().Create(request); </pre> </p> <p> This API is also discoverable, because Intellisense helps users discover how to deviate from the default values: </p> <p> <img src="/content/binary/fluent-builder-intellisense-discoverability.png" alt="Intellisense and Fluent Builder combined enhances discoverability"> </p> <p> It enables users to override only those values they care about: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;linker&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RouteLinkerBuilder</span>().WithDispatcher(customDispatcher).Create(request);</pre> </p> <p> If I had wanted to <em>force</em> users of Hyprlinkr to use the (hypothetical) RouteLinkerBuilder, I could make the RouteLinker constructor <code>internal</code>, but I don't much care for that option; I prefer to empower my users, not constrain them. </p> <h3 id="e1d10ddc3425466a9ef4616a57259892"> Composition <a href="#e1d10ddc3425466a9ef4616a57259892" title="permalink">#</a> </h3> <p> Any application that uses your library can compose objects from it in its Composition Root. Here's a hand-coded example from one of <a href="http://grean.com">Grean's</a> code bases: </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">RouteLinker</span>&nbsp;CreateDefaultRouteLinker(<span style="color:#2b91af;">HttpRequestMessage</span>&nbsp;request) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RouteLinker</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ModelFilterRouteDispatcher</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultRouteDispatcher</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;); }</pre> </p> <p> This example is just a small helper method in the Composition Root, but as you can see, it composes a RouteLinker instance using <em>our custom</em> ModelFilterRouteDispatcher class as a Decorator for Hyprlinkr's built-in DefaultRouteDispatcher. </p> <p> However, it would also be easy to configure a DI Container to do this instead. </p> <h3 id="0dc90645d62b4b9abea87c241380aaef"> Summary <a href="#0dc90645d62b4b9abea87c241380aaef" title="permalink">#</a> </h3> <p> If you follow SOLID, and normal rules for encapsulation, your library is likely to be DI-friendly. No special infrastructure is required to add 'DI support' to a library. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="eb364b0e94ce434797b6d95967653ded"> <div class="comment-author">Maris Krivtezs <a href="#eb364b0e94ce434797b6d95967653ded">#</a></div> <div class="comment-content"> <p> I found great library for in process messaging made by Jimmy Bogard - <a href="https://github.com/jbogard/MediatR">MediatR</a>, but it uses service locator. Implemented mediator uses service locator to lookup for handlers matching message type registered in container. <a href="https://github.com/jbogard/MediatR/blob/master/src/MediatR/Mediator.cs#L84">Source.</a> </p> <p> What would be best approach to eliminate service locator in this case? Would it be better to pass all handler instances in mediator constructor and then lookup for matching one? </p> </div> <div class="comment-date">2014-06-02 20:10 UTC</div> </div> <div class="comment" id="475829539c754b759bef404ba03d97cb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#475829539c754b759bef404ba03d97cb">#</a></div> <div class="comment-content"> <p> Maris, thank you for writing. Hopefully, <a href="/2011/09/19/MessageDispatchingwithoutServiceLocation">this article</a> answers your question. </p> </div> <div class="comment-date">2014-06-03 9:20 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Conforming Container https://blog.ploeh.dk/2014/05/19/conforming-container 2014-05-19T07:54:00+00:00 Mark Seemann <div id="post"> <p> <em>A Dependency Injection anti-pattern.</em> </p> <p> Once in a while, someone comes up with the idea that it would be great to introduce a common abstraction over various DI Containers in .NET. My guess is that part of the reason for this is that there are so many DI Containers to choose from on .NET: <ul> <li><a href="http://autofac.org/">Autofac</a></li> <li><a href="http://docs.castleproject.org/Windsor.MainPage.ashx">Castle Windsor</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/dd460648.aspx">Managed Extensibility Framework</a></li> <li><a href="http://www.ninject.org">Ninject</a></li> <li><a href="https://simpleinjector.codeplex.com">Simple Injector</a></li> <li><a href="http://springframework.net">Spring.NET</a></li> <li><a href="http://docs.structuremap.net">StructureMap</a></li> <li><a href="https://unity.codeplex.com">Unity</a></li> </ul> ... and these are just the major ones; there are many more! Hiding all these different libraries behind a common interface sounds like a smashing idea, but isn't. </p> <h3 id="77bec0dedcfe48c38af21713987f1cdf"> General form <a href="#77bec0dedcfe48c38af21713987f1cdf" title="permalink">#</a> </h3> <p> At its core, a Conforming Container introduces a central interface, often called IContainer, IServiceLocator, IServiceProvider, ITypeActivator, IServiceFactory, or something in that vein. The interface defines one or more methods called Resolve, Create, GetInstance, or similar: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IContainer</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>&nbsp;Resolve(<span style="color:#2b91af;">Type</span>&nbsp;type); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">object</span>&nbsp;Resolve(<span style="color:#2b91af;">Type</span>&nbsp;type,&nbsp;<span style="color:blue;">params</span>&nbsp;<span style="color:blue;">object</span>[]&nbsp;arguments); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;Resolve&lt;T&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;Resolve&lt;T&gt;(<span style="color:blue;">params</span>&nbsp;<span style="color:blue;">object</span>[]&nbsp;arguments); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;T&gt;&nbsp;ResolveAll&lt;T&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;etc.</span> }</pre> </p> <p> Sometimes, the interface defines only a single of those methods; sometimes, it defines even more variations of methods to create objects based on a Type. </p> <p> Some Conforming Containers stop at this point, so that the interface only exposes <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Queries</a>, which means that they only cover the Resolve phase of the <a href="/2010/09/29/TheRegisterResolveReleasepattern">Register Resolve Release</a> pattern. Other efforts attempt to address Register phase too: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IContainer</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;AddService(<span style="color:#2b91af;">Type</span>&nbsp;serviceType,&nbsp;<span style="color:#2b91af;">Type</span>&nbsp;implementationType); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;AddService&lt;TService,&nbsp;TImplementation&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;etc.</span> }</pre> </p> <p> The intent is to enable configuration of the container using some sort of metadata. Sometimes, the methods have more advanced configuration parameters that also enable you to specify the lifestyle of the service, etc. </p> <p> Finally, a part of a typical Conforming Container ecosystem is various published <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapters</a> to concrete DI Containers. A hypothetical <em>Confainer</em> project may publish the following Adapter packages: <ul> <li>Confainer.Autofac</li> <li>Confainer.Windsor</li> <li>Confainer.Ninject</li> <li>Confainer.Unity</li> </ul> Notice that in this example, not all major .NET DI Containers are listed. This is a typical situation. Obviously, since the entire effort is to define an interface, contributors are often invited to provide Adapters for missing DI Containers. </p> <h3 id="68e62f6ecc7643c18c0616c2dc405a94"> Symptoms and consequences <a href="#68e62f6ecc7643c18c0616c2dc405a94" title="permalink">#</a> </h3> <p> A Conforming Container is an <a href="http://en.wikipedia.org/wiki/Anti-pattern">anti-pattern</a>, because it's <blockquote> <a href="http://amzn.to/1gDifab">a commonly occurring solution to a problem that generates decidedly negative consequences,</a> </blockquote> such as: <ul> <li>Calls to the Conforming Container are likely to be sprinkled liberally over an entire code base.</li> <li>It pushes novice users towards the <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator anti-pattern</a>. Most people encountering Dependency Injection for the first time <a href="http://www.infoq.com/articles/Succeeding-Dependency-Injection">mistake it for the Service Locator anti-pattern</a>, despite the entirely opposite natures of these two approaches to loose coupling.</li> <li>It attempts to relieve symptoms of bad design, instead of addressing the underlying problem. Too many 'loosely coupled' designs attempt to rely on the Service Locator anti-pattern, which, by default, introduces a dependency to a concrete Service Locator throughout a code base. However, exclusively using the Constructor Injection and <a href="/2011/07/28/CompositionRoot">Composition Root</a> design patterns eliminate the problem altogether, resulting in a simpler design with fewer moving parts.</li> <li>It pulls in the direction of the lowest common denominator.</li> <li>It stifles innovation, because new, creative, but radical ideas may not fit into the narrow view of the world a Conforming Container defines.</li> <li>It makes it more difficult to avoid using a DI Container. A DI Container can be useful in certain scenarios, but often, <a href="/2012/11/06/WhentouseaDIContainer">hand-coded composition is better than using a DI Container</a>. However, if a library or framework depends on a Conforming Container, it may be difficult to harvest the benefits of hand-coded composition.</li> <li>It may introduce versioning hell. Imagine that you need to use a library that depends on Confainer 1.3.7 in an application that also uses a framework that depends on Confainer 2.1.7. Since a Conforming Container is intended as an infrastructure component, this is <em>likely</em> to happen, and to cause much grief.</li> <li>A Conforming Container is often a product of <a href="http://c2.com/cgi/wiki?SpeculativeGenerality">Speculative Generality</a>, instead of a product of need. As such, the API is likely to be poorly suited to address real-world scenarios, be difficult to extent, and may exhibit churn in the form of frequent breaking changes.</li> <li>If Adapters are supplied by contributors (often the DI Container maintainers themselves), the Adapters may have varying quality levels, and may not support the latest version of the Conforming Container.</li> </ul> </p> <p> A code base using a Conforming Container may have code like this all over the place: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;foo&nbsp;=&nbsp;container.Resolve&lt;<span style="color:#2b91af;">IFoo</span>&gt;(); <span style="color:green;">//&nbsp;...&nbsp;use&nbsp;foo&nbsp;for&nbsp;something...</span> <span style="color:blue;">var</span>&nbsp;bar&nbsp;=&nbsp;container.Resolve&lt;<span style="color:#2b91af;">IBar</span>&gt;(); <span style="color:green;">//&nbsp;...&nbsp;use&nbsp;bar&nbsp;for&nbsp;something&nbsp;else...</span> <span style="color:blue;">var</span>&nbsp;baz&nbsp;=&nbsp;container.Resolve&lt;<span style="color:#2b91af;">IBaz</span>&gt;(); <span style="color:green;">//&nbsp;...&nbsp;use&nbsp;baz&nbsp;for&nbsp;something&nbsp;else&nbsp;again...</span></pre> </p> <p> This breaks encapsulation, because it's impossible to identify a class' collaborators without reading its entire code base. </p> <p> Additionally, concrete DI Containers have distinct feature sets. Although likely to be out of date by now, this feature comparison chart from <a href="http://amzn.to/12p90MG">my book</a> illustrate this point: </p> <p> <table> <thead> <tr><th></th><th>Castle Windsor</th><th>StructureMap</th><th>Spring.NET</th><th>Autofac</th><th>Unity</th><th>MEF</th></tr> </thead> <tbody> <tr> <td>Code as Configuration</td> <td>x</td> <td>x</td> <td></td> <td>x</td> <td>x</td> <td></td> </tr> <tr> <td>Auto-registration</td> <td>x</td> <td>x</td> <td></td> <td>x</td> <td></td> <td></td> </tr> <tr> <td>XML configuration</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td></td> </tr> <tr> <td>Modular configuration</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> <tr> <td>Custom lifetimes</td> <td>x</td> <td>x</td> <td></td> <td>(x)</td> <td>x</td> <td></td> </tr> <tr> <td>Decommissioning</td> <td>x</td> <td></td> <td></td> <td>x</td> <td>(x)</td> <td>x</td> </tr> <tr> <td>Interception</td> <td>x</td> <td></td> <td>x</td> <td></td> <td>x</td> <td></td> </tr> </tbody> </table> </p> <p> This is only a simple chart that plots the most common features of DI Containers. Each DI Container has dozens of features - many of them unique to that particular DI Container. A Conforming Container can either support an intersection or union of all those features. </p> <p> <img src="/content/binary/conforming-container-intersection.png" alt="Intersection and union of containers"> </p> <p> A Conforming Container that targets only the intersection of all features will be able to support only a small fraction of all available features, diminishing the value of the Conforming Container to the point where it becomes gratuitous. </p> <p> A Conforming Container that targets the union of all features is guaranteed to consist mostly of a multitude of NotImlementedExceptions, or, put in another way, massively breaking the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>. </p> <h3 id="187e210471d94170aeb34bfa92dfac5a"> Typical causes <a href="#187e210471d94170aeb34bfa92dfac5a" title="permalink">#</a> </h3> <p> The typical causes of the Conforming Container anti-pattern are: <ul> <li>Lack of understanding of Dependency Injection. Dependency Injection is a set of patterns driven by the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>. A DI Container is an optional library, not a required part.</li> <li>A fear of letting an entire code base depend on a concrete DI Container, if that container turns out to be a poor choice. Few programmers have thouroughly surveyed all available DI Containers before picking one for a project, so architects desire to have the ability to replace e.g. StructureMap with Ninject.</li> <li>Library designers mistakenly thinking that <em>Dependency Injection support</em> involves defining a Conforming Container.</li> <li>Framework designers mistakenly thinking that <em>Dependency Injection support</em> involves defining a Conforming Container.</li> </ul> The root cause is always a lack of awareness of a simpler solution. </p> <h3 id="9e393668c62840a787cac1282287132f"> Known exceptions <a href="#9e393668c62840a787cac1282287132f" title="permalink">#</a> </h3> <p> There are no cases known to me where a Conforming Container is a good solution to the problem at hand. There's always a better and simpler solution. </p> <h3 id="6b4a92388c0f4b759621974fa6e4eff7"> Refactored solution <a href="#6b4a92388c0f4b759621974fa6e4eff7" title="permalink">#</a> </h3> <p> Instead of relying on the Service Locator anti-pattern, all collaborating classes should rely on the Constructor Injection pattern: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CorrectClient</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IFoo</span>&nbsp;foo; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IBar</span>&nbsp;bar; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IBaz</span>&nbsp;baz; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;CorrectClient(<span style="color:#2b91af;">IFoo</span>&nbsp;foo,&nbsp;<span style="color:#2b91af;">IBar</span>&nbsp;bar,&nbsp;<span style="color:#2b91af;">IBaz</span>&nbsp;baz) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.foo&nbsp;=&nbsp;foo; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.bar&nbsp;=&nbsp;bar; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.baz&nbsp;=&nbsp;baz; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;DoSomething() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...&nbsp;use&nbsp;this.foo&nbsp;for&nbsp;something...</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...&nbsp;use&nbsp;this.bar&nbsp;for&nbsp;something&nbsp;else...</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;...&nbsp;use&nbsp;this.baz&nbsp;for&nbsp;something&nbsp;else&nbsp;again...</span> &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This leaves all options open for any code consuming the CorrectClient class. The only exception to relying on Constructor Injection is when you need to compose all these collaborating classes. The <a href="/2011/07/28/CompositionRoot">Composition Root</a> has the single responsibility of composing all the objects into a working object graph: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CompositionRoot</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CorrectClient</span>&nbsp;ComposeClient() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">CorrectClient</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RealFoo</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RealBar</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RealBaz</span>()); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> In this example, the final graph is rather shallow, but it <a href="/2011/03/04/Composeobjectgraphswithconfidence">can be as complex and deep as necessary</a>. This Composition Root uses hand-coded composition, but <em>if</em> you want to use a DI Container, the Composition Root is where you put it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">WindsorCompositionRoot</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">WindsorContainer</span>&nbsp;container; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;WindsorCompositionRoot() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.container&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">WindsorContainer</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Configure&nbsp;the&nbsp;container&nbsp;here,</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;or&nbsp;better&nbsp;yet:&nbsp;use&nbsp;a&nbsp;WindsorInstaller</span> &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">CorrectClient</span>&nbsp;ComposeClient() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.container.Resolve&lt;<span style="color:#2b91af;">CorrectClient</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This class (and perhaps a few auxiliary classes, such as a Windsor Installer) is the only class that uses a concrete DI Container. <em>This is the <a href="http://en.wikipedia.org/wiki/Hollywood_principle">Hollywood Principle</a> in action.</em> There's no reason to hide the DI Container behind an interface, because it has <em>no clients</em>. The DI Containers knows about the application; the application knows nothing about the DI Container. </p> <p> In all but the most trivial of applications, the Composition Root is only an extremely small part of the entire application. </p> <p> <img src="/content/binary/composition-root-is-only-a-small-part-of-an-application.png" alt="A Composition Root is only a small part of an application"> </p> <p> (The above picture is meant to illustrate an arbitrary application architecture; it could be layered, onion, hexagonal, or something else - <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">it doesn't really matter</a>.) If you want to replace one DI Container with another DI Container, you only replace the Composition Root; the rest of the application will never notice the difference. </p> <p> Notice that only applications should have Composition Roots. Libraries and frameworks should not. <ul> <li>Library classes should be defined with Constructor Injection throughout. If the library object model is very complex, a few <a href="http://en.wikipedia.org/wiki/Facade_pattern">Facades</a> can be supplied to make it easier for library users to get started. See <a href="/2014/05/19/di-friendly-library">my article on DI-friendly libraries</a> for more details.</li> <li>Frameworks should have appropriate hooks built in. These hooks should not be designed as Service Locators, but rather as Abstract Factories. See <a href="/2014/05/19/di-friendly-framework">my article on DI-friendly frameworks</a> for more details.</li> </ul> These solutions are better than a Conforming Container because they are simpler, have fewer moving parts, are easier to understand, and easier to reason about. </p> <h3 id="034877fae29348fb99c5b8d8c6fdde34"> Variations <a href="#034877fae29348fb99c5b8d8c6fdde34" title="permalink">#</a> </h3> <p> Sometimes the Conforming Container only defines a Service Locator-like API, and sometimes it also defines a configuration API. That configuration API may include various axes of configurability, most notably lifetime management and decommisioning. </p> <p> Decommissioning is often designed around the concept of a disposable 'context' scope, but as I explain in <a href="http://amzn.to/12p90MG">my book</a>, that's not an extensible pattern. </p> <h3 id="29ef28a2a2344c38af60e3fdb25973fc"> Known examples <a href="#29ef28a2a2344c38af60e3fdb25973fc" title="permalink">#</a> </h3> <p> There are various known examples of Conforming Containers for .NET: <ul> <li><a href="https://commonservicelocator.codeplex.com">Common Service Locator</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.dependencyresolver.aspx">ASP.NET MVC Dependency Resolver</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.web.http.dependencies.idependencyresolver.aspx">ASP.NET Web API Dependency Resolver</a></li> <li><a href="http://docs.particular.net/nservicebus/containers">NServiceBus IContainer</a></li> <li><a href="https://github.com/rebus-org/Rebus/wiki/Container-adapters">Rebus container adapters</a></li> </ul> Additionally, it looks like the new <a href="https://github.com/aspnet/DependencyInjection">Dependency Injection support for ASP.NET</a> is taking this route as well, although hopefully, it's not too late to change that. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Configuring Azure Web Jobs https://blog.ploeh.dk/2014/05/16/configuring-azure-web-jobs 2014-05-16T17:58:00+00:00 Mark Seemann <div id="post"> <p> <em>It's easy to configure Azure Web Jobs written in .NET.</em> </p> <p> Azure <a href="http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs">Web Jobs</a> is a nice feature for Azure Web Sites, because it enables you to bundle a background worker, scheduled batch job, etc. together with your Web Site. It turns out that this feature works pretty well, but it's not particularly well-documented, so I wanted to share a few nice features I've discovered while using them. </p> <p> You can write a Web Job as a simple Command Line executable, but if you can supply command-line arguments to it, I have yet to discover how to do that. A good alternative is an app.config file with configuration settings, but it <em>can</em> be a hassle to deal with various configuration settings across different deployment environments. There's a simple solution to that. </p> <h3 id="bf18abb5342e44f3ab8c91ea1ee644ae"> CloudConfigurationManager <a href="#bf18abb5342e44f3ab8c91ea1ee644ae" title="permalink">#</a> </h3> <p> If you use CloudConfigurationManager.GetSetting, configuration settings are read using various fallback mechanisms. The CloudConfigurationManager class is poorly documented, and I couldn't find documentation for the current version, but <a href="http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.cloudconfigurationmanager.getsetting.aspx">one documentation page about a deprecated version</a> sums it up well enough: <blockquote> "The GetSetting method reads the configuration setting value from the appropriate configuration store. If the application is running as a .NET Web application, the GetSetting method will return the setting value from the Web.config or app.config file. If the application is running in Windows Azure Cloud Service or in a Windows Azure Website, the GetSetting will return the setting value from the ServiceConfiguration.cscfg." </blockquote> That is probably still true, but I've found that it actually does more than that. As far as I can tell, it attempts to read configuration settings in this prioritized order: <ol> <li>Try to find the configuration value in the Web Site's online configuration (see below).</li> <li>Try to find the configuration value in the .cscfg file.</li> <li>Try to find the configuration value in the app.config file or web.config file.</li> </ol> By the Web Site's online configuration, I mean the configuration settings you can edit on a deployment-by-deployment basis using the Azure management portal, under the <em>Configure</em> 'tab': </p> <p> <img src="/content/binary/azure-web-sites-app-settings-ui.png" alt="Online configuration for an Azure Web Site"> </p> <p> (It's possible that, under the hood, this UI actually maintains an auto-generated .cscfg file, in which case the first two bullet points above turn out to be one and the same.) </p> <p> This is a really nice feature, because it means that you can push your deployments directly from your source control system (I use Git), and leave your configuration files empty in source control: </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:#a31515;">appSettings</span><span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;">&nbsp;</span><span style="color:red;">key</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">timeout</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">0:01:00</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;">&nbsp;</span><span style="color:red;">key</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">estimatedDuration</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">0:00:02</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;">&nbsp;</span><span style="color:red;">key</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">toleranceFactor</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">2</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;">&nbsp;</span><span style="color:red;">key</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">idleTime</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">0:00:05</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;">&nbsp;</span><span style="color:red;">key</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">storageMode</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">value</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">files</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;">&nbsp;</span><span style="color:red;">key</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">storageConnectionString</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">value</span><span style="color:blue;">=</span>&quot;&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;">&nbsp;</span><span style="color:red;">key</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">raygunKey</span>&quot;<span style="color:blue;">&nbsp;</span><span style="color:red;">value</span><span style="color:blue;">=</span>&quot;&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">appSettings</span><span style="color:blue;">&gt;</span></pre> </p> <p> Instead of having to figure out how to manage or merge those super-secret keys in the build system, you can simply shortcut the whole issue by not involving those keys in your build system; they're <em>only</em> stored in Azure - where you can't avoid having them anyway, because your system needs them in order to work. </p> <h3 id="48865ed433a740c2babc04e10696f29b"> Usage <a href="#48865ed433a740c2babc04e10696f29b" title="permalink">#</a> </h3> <p> It's easy to use CloudConfigurationManager: instead of getting your configuration values with <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx">ConfigurationManager.AppSettings</a>, you use CloudConfigurationManager.GetSetting: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;clientId&nbsp;=&nbsp;<span style="color:#2b91af;">CloudConfigurationManager</span>.GetSetting&nbsp;<span style="color:#a31515;">&quot;clientId&quot;</span> </pre> </p> <p> The CloudConfigurationManager class isn't part of the .NET Base Class Library, but you can easily add it from NuGet; it's called <a href="http://www.nuget.org/packages/Microsoft.WindowsAzure.ConfigurationManager/">Microsoft.WindowsAzure.ConfigurationManager</a>. The Azure SDK isn't required - it's just a stand-alone library with no dependencies, so I happily add it to my <a href="/2011/07/28/CompositionRoot">Composition Root</a> when I know I'm going to deploy to an Azure Web Site. </p> <h3 id="b639a0517357401d8ebec1943f963db8"> Web Jobs <a href="#b639a0517357401d8ebec1943f963db8" title="permalink">#</a> </h3> <p> Although I haven't found any documentation to that effect yet, a .NET console application running as an Azure Web Job will pick up configuration settings in the way described above. On other words, it shares configuration values with the web site that it's part of. That's darn useful. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9a938beb59d346b68812270eb1e95c4f"> <div class="comment-author">Howard Camp <a href="#9a938beb59d346b68812270eb1e95c4f">#</a></div> <div class="comment-content">Any secrets that I have must be in the app.config in order for me to run locally during development, correct? How do I keep them out of the build system?</div> <div class="comment-date">2017-12-05 02:29 UTC</div> </div> <div class="comment" id="2bf1eb438a9b4745a2cfd4f2d5bd45fc"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2bf1eb438a9b4745a2cfd4f2d5bd45fc">#</a></div> <div class="comment-content"> <p> Howard, thank you for writing. You should always keep your secrets out of source control. In some projects, I've used <a href="https://msdn.microsoft.com/en-us/library/dd465318">web.config transforms</a> for that purpose. Leave your checked-in <code>.config</code> files empty, and have local (not checked-in) <code>.config</code> files on development machines, production servers, and so on. </p> <p> As far as I know, on most other platforms, people simply use environment variables instead of configuration files. To me, that sounds like a simple solution to the problem. </p> </div> <div class="comment-date">2017-12-05 08:16 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Service Locator violates SOLID https://blog.ploeh.dk/2014/05/15/service-locator-violates-solid 2014-05-15T18:51:00+00:00 Mark Seemann <div id="post"> <p> <em>Yet another reason to avoid the Service Locator anti-pattern is that it violates the principles of Object-Oriented Design.</em> </p> <p> Years ago, I wrote an article about <a href="http://martinfowler.com/articles/injection.html">Service Locator</a>. Regular readers of this blog may already know that <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">I consider Service Locator an anti-pattern</a>. That hasn't changed, but I recently realized that there's another way to explain why Service Locator is the inverse of good Object-Oriented Design (OOD). My original article didn't include that perspective at all, so perhaps this is a clearer way of explaining it. </p> <p> In this article, I'll assume that you're familiar with the <a href="http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)">SOLID principles</a> (also known as <em>the Principles of OOD</em>), and that you accept them as generally correct. It's not because I wish to argue by an appeal to authority, but rather because threre's already a large body of work that explains why these principles are beneficial to software design. </p> <p> In short, Service Locator violates SOLID because it violates the <a href="http://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> (ISP). That's because a Service Locator effectively has <em>infinitely</em> many members. </p> <h3 id="9e85d0c9b4424f5981e2bb1acdd8380b"> Service Locator deconstructed <a href="#9e85d0c9b4424f5981e2bb1acdd8380b" title="permalink">#</a> </h3> <p> In order to understand why a Service Locator has infinitely many members, you'll need to understand what a Service Locator is. Often, it's a class or interface with various members, but it all boils down to a single member: </p> <p> <pre>T&nbsp;Create&lt;T&gt;(); </pre> </p> <p> Sometimes the method <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">takes one or more parameters</a>, but that doesn't change the conclusion, so I'm leaving out those input parameters to keep things simple. </p> <p> A common variation is the untyped, non-generic variation: </p> <p> <pre><span style="color:blue;">object</span>&nbsp;Create(<span style="color:#2b91af;">Type</span>&nbsp;type); </pre> </p> <p> Since my overall argument relies on the generic version, first I'll need to show you why those two methods are equivalent. If you imagine that all you have is the non-generic version, you can easily write a generic extension method for it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ServiceLocatorEnvy</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;T&nbsp;Create&lt;T&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IServiceLocator</span>&nbsp;serviceLocator) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;(T)serviceLocator.Create(<span style="color:blue;">typeof</span>(T)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> As you can see, this extension method has exactly the same signature as the generic version; you can always create a generic Service Locator based on a non-generic Service Locator. Thus, while my main argument (coming up next) is based on a generic Service Locator, it also applies to non-generic Service Locators. </p> <h3 id="af2607487fd64bbfb9fc4e72a335c761"> Infinite methods <a href="#af2607487fd64bbfb9fc4e72a335c761" title="permalink">#</a> </h3> <p> From a client's perspective, there's no limit to how many variations of the Create method it can invoke: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;foo&nbsp;=&nbsp;serviceLocator.Create&lt;<span style="color:#2b91af;">IFoo</span>&gt;(); <span style="color:blue;">var</span>&nbsp;bar&nbsp;=&nbsp;serviceLocator.Create&lt;<span style="color:#2b91af;">IBar</span>&gt;(); <span style="color:blue;">var</span>&nbsp;baz&nbsp;=&nbsp;serviceLocator.Create&lt;<span style="color:#2b91af;">IBaz</span>&gt;(); <span style="color:green;">//&nbsp;etc.</span></pre> </p> <p> Again, from the client's perspective, that's equivalent to multiple method definitions like: </p> <p> <pre><span style="color:#2b91af;">IFoo</span>&nbsp;CreateFoo(); <span style="color:#2b91af;">IBar</span>&nbsp;CreateBar(); <span style="color:#2b91af;">IBaz</span>&nbsp;CreateBaz(); <span style="color:green;">//&nbsp;etc.</span></pre> </p> <p> However, the client can <em>keep coming up with new types to request</em>, so effectively, the number of Create methods is infinite! </p> <h3 id="6ddef7c7a5f0461f9ea1674dbfd250cf"> Relation to the Interface Segregation Principle <a href="#6ddef7c7a5f0461f9ea1674dbfd250cf" title="permalink">#</a> </h3> <p> By now, you understand that a Service Locator is an interface or class with effectively an infinite number of methods. That violates the ISP, which states: <blockquote> <a href="http://amzn.to/19W4JHk">Clients should not be forced to depend on methods they do not use.</a> </blockquote> However, since a Service Locator exposes an infinite number of methods, any client using it is forced to depend on infinitely many methods it doesn't use. </p> <h3 id="fd996a05d9774565bbcb65821deb8009"> Quod Erat Demonstrandum <a href="#fd996a05d9774565bbcb65821deb8009" title="permalink">#</a> </h3> <p> The Service Locator anti-pattern violates the ISP, and thus it also violates SOLID as a whole. SOLID is also known as <em>the Principles of OOD</em>. Therefore, Service Locator is bad Objected-Oriented Design. </p> <p> <strong>Update 2015-10-26:</strong> The fundamental problem with Service Locator is that <a href="/2015/10/26/service-locator-violates-encapsulation">it violates encapsulation</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="40d8890f708f4bd5994eb8005302c93e"> <div class="comment-author">Nelson LaQuet <a href="#40d8890f708f4bd5994eb8005302c93e">#</a></div> <div class="comment-content"> Doesn't this argument also apply to any method that takes a generic parameter? Meaning any use of generic methods also violates ISP? </div> <div class="comment-date">2014-05-15 21:06 UTC</div> </div> <div class="comment" id="b7b4a2bf0e4c4ddfb4cff0159ae361a6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b7b4a2bf0e4c4ddfb4cff0159ae361a6">#</a></div> <div class="comment-content"> <p> Nelson, thank you for writing. First, it's important to realize that this overall argument applies to methods with 'free' generic type parameters; that is, a method where the type in itself isn't generic, but the method is. One example of the difference I mean is that a <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">generic Abstract Factory is benign, whereas a Server Locator isn't</a>. </p> <p> Second, that still leaves the case where you may have a generic parameter that determines the return type of the method. The LINQ <a href="http://msdn.microsoft.com/en-us/library/bb548891.aspx">Select</a> method is an example of such a method. These tend not to be problematic, but <a href="https://plus.google.com/u/0/+MarkSeemann/posts/dELcbR54zYK">I had some trouble explaining why that is</a> until <a href="http://dotnetdom.blogspot.dk/2014/05/service-locators-and-interface.html">James Jensen explained it to me</a>. </p> </div> <div class="comment-date">2014-05-16 11:33 UTC</div> </div> <div class="comment" id="acb6985852ff43b08bd4c4cdf3d2f42e"> <div class="comment-author"><a href="http://www.feo2x.com">Kenny Pflug</a> <a href="#acb6985852ff43b08bd4c4cdf3d2f42e">#</a></div> <div class="comment-content"> <p>Dear Mark,</p> <p> I totally agree with your advise on Service Locator being an anti-pattern, specifically if it is used within the core logic. However, I don't think that your argumentation in this post is correct. I think that you apply Object-Oriented Programming Principles to Metaprogramming, which should not be done, but I'm not quite sure if my argument is completely reasonable. </p> <p> All .NET DI Containers that I know of use the <a href="https://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx">Reflection API</a> to solve the problem of dynamically composing an object graph. The very essence of this API is it's ability to inspect and call members of any .NET type, even the ones that the code was not compiled against. Thus you do not use Strong Object-Oriented Typing any longer, but access the members of a e.g. a class indirectly using a model that relies on the <a href="https://msdn.microsoft.com/en-us/library/system.type.aspx">Type</a> class and its associated types. And this is the gist of it: code is treated as data, this is <a href="https://en.wikipedia.org/wiki/Metaprogramming">Metaprogramming</a>, as we all know. </p> <p> Without these capabilities, DI containers wouldn't be able to do their job because they couldn't e.g. analyze the arguments of a class's constructor to further instantiate other objects needed. Thus we can say that <em>DI containers are just an abstraction over the Metaprogramming API of .NET</em>. And of course, these containers offer an API that is shaped by their Metaprogramming foundation. This can be seen in your post: although you discuss the generic variation <code>T Create&lt;T&gt;()</code>, this is just syntactic sugar for the actual important method: <code>object Create(Type type)</code>. </p> <p> Metaprogramming in C# is totally resolved at runtime, and therefore one shouldn't apply the Interface Segregation Principle to APIs that are formed by it. These are designed to help you improve the Object-Oriented APIs which particularly incorporate Static Typing enforced by the compiler. A DI container does not have an unlimited number of Create methods, it has a single one and it receives a Type argument - the generic version just creates the Type object for you. And the parameter has to be as "weak" as Type, because we cannot use Static Typing - this technically allows the client to pass in types that the container is not configured for, but you cannot prevent this using the compiler because of the dynamic nature of the Reflection API. </p> <p> What is your opinion on that? </p> </div> <div class="comment-date">2016-01-06 21:55 UTC</div> </div> <div class="comment" id="af735817fa054ad5b60110502bc524a1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#af735817fa054ad5b60110502bc524a1">#</a></div> <div class="comment-content"> <p> Kenny, thank you for writing. The point that this post is making is mainly that Service Locator violates the Interface Segregation Principle (ISP). The appropriate perspective on ISP (and LSP and DIP as well) is from a client. The client of a Service Locator effectively sees an API with infinitely many methods. That's where the damage is done. </p> <p> How the Service Locator is implemented isn't important to ISP, LSP, or DIP. (The SRP and OCP, on the other hand, relate to implementations.) You may notice that this article doesn't use the word <em>container</em> a single time. </p> </div> <div class="comment-date">2016-01-07 19:16 UTC</div> </div> <div class="comment" id="9bbf8fd4a7e04574a623660aa025844d"> <div class="comment-author"><a href="http://www.feo2x.com">Kenny Pflug</a> <a href="#9bbf8fd4a7e04574a623660aa025844d">#</a></div> <div class="comment-content"> <p>Dear Mark,</p> <p> I get the point that you are talking from the client's perspective - but even so, a client programming against a Service Locator should be aware that it is programming against an abstraction of a Metaprogramming API (and not an Object-Oriented API). If you think about the call to the Create method, then you basically say "Give me an object graph with the specified type as the object graph root" as a client - how do you implement this with the possibilities that OOP provides? You can't model this with classes, interfaces, and the means of Procedural and Structural Programming that are integrated in OOP - because these techniques do not allow you to treat code as data. </p> <p> And again, your argument is based on the generic version of the Create method, but that shouldn't be the point of focus. It is the non-generic version <code>object Create (Type type)</code> which clearly indicates that it is a Metaprogramming API because of the Type parameter and the object return type - <a href="https://msdn.microsoft.com/en-us/library/system.type.aspx">Type</a> is the entry point to .NET Reflection and object is the only type the Service Locator can guarantee as the object graph is dynamically resolved at runtime - no Strong Typing involved. The existence of the generic Create variation is merely justified because software developers are lazy - they don't want to manually downcast the returned object to the type they actually need. Well, one could argue that this comforts the Single Point of Truth / Don't repeat yourself principles, too, as all information to create the object graph and to downcast the root object are derived from the generic type argument, but that doesn't change the fact that Service Locator is a Metaprogramming API. </p> <p> And that's why I solely used the term DI container throughout my previous comment, because Service Locator is just the part of the API of a DI container that is concerned with resolving object graphs (and the Metainformation to create these object graphs was registered beforehand). Sure, you can implement Service Locators as a hard-wired registry of Singleton objects (or even Factories that create objects on the fly) to circumvent the use of the Reflection API (although one probably had to use some sort of Type ID in this case, maybe in form of a string or GUID). But honestly, these are half-baked solutions that do not solve the problem in a reusable way. A reusable Service Locator must treat code as data, especially if you want additional features like lifetime management. </p> <p> Another point: take for example the <a href="https://msdn.microsoft.com/en-us/library/system.web.security.membershipprovider(v=vs.110).aspx">MembershipProvider</a> class - this polymorphic abstraction is truly a violation of the Interface Segregation Principle because it offers way too many members that a client probably won't need. But notice that each of these members has a different meaning, which is not the case with the Create methods of the Service Locator. The generic Create method is just another abstraction over the non-generic version to simplify the access to the Service Locator. </p> <p> Long story short: Service Locator is a Metaprogramming API, the SOLID principles target Object-Oriented APIs, thus the latter shouldn't be used to assess the former. There's is no real way to hide the fact that clients need to be aware that they are calling a Metaprogramming API if they directly reference a Service Locator (which shouldn't be done in core logic). </p> </div> <div class="comment-date">2016-01-08 09:40 UTC</div> </div> <div class="comment" id="351cbdc09c704ed2964533f7ddf0ad38"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#351cbdc09c704ed2964533f7ddf0ad38">#</a></div> <div class="comment-content"> <p> Kenny, thank you for writing. While I don't agree with <em>everything</em> you wrote, your arguments are well made, and I have no problems following them. If we disagree, I think we disagree about semantics, because the way I read your comment, I think it eventually leads you to the same conclusions that I have arrived at. </p> <p> Ultimately, you also state that Service Locator isn't an Object-Oriented Design, and in that, I entirely agree. The SOLID principles are also known as the <em>principles of Object-Oriented Design</em>, so when I'm stating that I think that Service Locator violates SOLID, my more general point is that Service Locator isn't Object-Oriented because it violates a fundamental principle of OOD. You seem to have arrived at the same conclusion, although via a different route. I always like when that happens, because it confirms that the conclusion may be true. </p> <p> To be honest, though, I don't consider the arguments I put forth in the present article as my strongest ever. Sometimes, I write articles on topics that I've thought about for years, but I also often write articles that are half-baked ideas; I put these articles out in order to start a discussion, so I appreciate your comments. </p> <p> I'm much happier with the article that argues that <a href="/2015/10/26/service-locator-violates-encapsulation">Service Locator violates Encapsulation</a>. </p> </div> <div class="comment-date">2016-01-08 10:55 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture conventions with Albedo https://blog.ploeh.dk/2014/05/01/autofixture-conventions-with-albedo 2014-05-01T21:40:00+00:00 Mark Seemann <div id="post"> <p> <em>You can use Albedo with AutoFixture to build custom conventions.</em> </p> <p> In a question to one of <a href="/2010/10/19/Convention-basedCustomizationswithAutoFixture">my previous posts</a>, Jeff Soper asks about using custom, string-based conventions for AutoFixture: <blockquote> "I always wince when testing for the ParameterType.Name value [...] It seems like it makes a test that would use this implementation very brittle." </blockquote> Jeff's concern is that when you're explicitly looking for a parameter (or property or field) with a particular name (like "currencyCode"), the unit test suite may become brittle, because if you change the parameter name, the string may retain the old name, and the Customization no longer works. </p> <p> Jeff goes on to say: <blockquote> "This makes me think that I shouldn't need to be doing this, and that a design refactoring of my SUT would be a better option." </blockquote> His concerns can be addressed on several different levels, but in this post, I'll show you how you can leverage <a href="https://github.com/ploeh/Albedo">Albedo</a> to address some of them. </p> <p> If you often find yourself in a situation where you're writing an AutoFixture Customization based on string matching of parameters, properties or fields, you should ask yourself if you're targeting <em>one</em> specific class, or if you're writing a convention? If you often target individual specific classes, you probably need to rethink your strategy, but you can easily run into situations where you need to introduce true conventions in your code base. This can be beneficial, because it'll make your code more consistent. </p> <p> Here's an example from the code base in which I'm currently working. It's a REST service written in F#. To model the JSON going in and out, I've defined some <a href="/2013/10/15/easy-aspnet-web-api-dtos-with-f-climutable-records">Data Transfer Records</a>, and some of them contain dates. However, JSON doesn't deal particularly well with dates, so they're treated as strings. Here's a JSON representation of a comment: </p> <p> <pre>{ "author": { "id": "1234", "name": "Mark Seemann", "email": "1234@ploeh.dk" }, "createdDate": "2014-04-30T18:14:08.1051775+00:00", "text": "Is this a comment?" }</pre> </p> <p> The record is defined like this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">CommentRendition</span>&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Author&nbsp;:&nbsp;<span style="color:#2b91af;">PersonRendition</span> &nbsp;&nbsp;&nbsp;&nbsp;CreatedDate&nbsp;:&nbsp;<span style="color:#2b91af;">string</span> &nbsp;&nbsp;&nbsp;&nbsp;Text&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>&nbsp;}</pre> </p> <p> This is a problem for AutoFixture, because it sees <code>CreatedDate</code> as a string, and populates it with an anonymous string. However, much of the code base expects the CreatedDate to be a proper date and time value, which can be parsed into a DateTimeOffset value. This would cause many tests to fail if I didn't change the behaviour. </p> <p> Instead of explicitly targeting the <code>CreatedDate</code> property on the CommentRendition record, I defined a conventions: <em>any parameter, field, or property that ends with "date" and has the type string, should be populated with a valid string representation of a date and time.</em> </p> <p> This is easy to write as a one-off Customization, but then it turned out that I needed an almost similar Customization for IDs: <em>any parameter, field, or property that ends with "id" and has the type string, should be populated with a valid GUID string formatted in a certain way.</em> </p> <p> Because ParameterInfo, PropertyInfo, and FieldInfo share little polymorphic behaviour, it's time to pull out Albedo, which was created for situations like this. Here's a reusable convention which can check any parameter, proeprty, or field for a given name suffix: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">TextEndsWithConvention</span>(value,&nbsp;found)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;<span style="color:#2b91af;">ReflectionVisitor</span>&lt;<span style="color:#2b91af;">bool</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;proceed&nbsp;x&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TextEndsWithConvention</span>&nbsp;(value,&nbsp;x&nbsp;||&nbsp;found)&nbsp;:&gt;&nbsp;<span style="color:#2b91af;">IReflectionVisitor</span>&lt;<span style="color:#2b91af;">bool</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;isMatch&nbsp;t&nbsp;(name&nbsp;:&nbsp;<span style="color:#2b91af;">string</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;=&nbsp;typeof&lt;<span style="color:#2b91af;">string</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;name.EndsWith(value,&nbsp;<span style="color:#2b91af;">StringComparison</span>.OrdinalIgnoreCase) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">override</span>&nbsp;this.Value&nbsp;=&nbsp;found &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">override</span>&nbsp;this.Visit&nbsp;(pie&nbsp;:&nbsp;<span style="color:#2b91af;">ParameterInfoElement</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;pi&nbsp;=&nbsp;pie.ParameterInfo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isMatch&nbsp;pi.ParameterType&nbsp;pi.Name&nbsp;|&gt;&nbsp;proceed &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">override</span>&nbsp;this.Visit&nbsp;(pie&nbsp;:&nbsp;<span style="color:#2b91af;">PropertyInfoElement</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;pi&nbsp;=&nbsp;pie.PropertyInfo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isMatch&nbsp;pi.PropertyType&nbsp;pi.Name&nbsp;|&gt;&nbsp;proceed &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">override</span>&nbsp;this.Visit&nbsp;(fie&nbsp;:&nbsp;<span style="color:#2b91af;">FieldInfoElement</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fi&nbsp;=&nbsp;fie.FieldInfo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isMatch&nbsp;fi.FieldType&nbsp;fi.Name&nbsp;|&gt;&nbsp;proceed &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">member</span>&nbsp;Matches&nbsp;value&nbsp;request&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;refraction&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">CompositeReflectionElementRefraction</span>&lt;<span style="color:#2b91af;">obj</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ParameterInfoElementRefraction</span>&lt;<span style="color:#2b91af;">obj</span>&gt;()&nbsp;:&gt;&nbsp;<span style="color:#2b91af;">IReflectionElementRefraction</span>&lt;<span style="color:#2b91af;">obj</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">PropertyInfoElementRefraction</span>&lt;<span style="color:#2b91af;">obj</span>&gt;()&nbsp;&nbsp;:&gt;&nbsp;<span style="color:#2b91af;">IReflectionElementRefraction</span>&lt;<span style="color:#2b91af;">obj</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">FieldInfoElementRefraction</span>&lt;<span style="color:#2b91af;">obj</span>&gt;()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&gt;&nbsp;<span style="color:#2b91af;">IReflectionElementRefraction</span>&lt;<span style="color:#2b91af;">obj</span>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;r&nbsp;=&nbsp;refraction.Refract&nbsp;[request] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.Accept(<span style="color:#2b91af;">TextEndsWithConvention</span>(value,&nbsp;<span style="color:blue;">false</span>)).Value</pre> </p> <p> It simply aggregates a boolean value (<code>found</code>), based on the name and type of various properties, fields, and parameters that comes its way. If there's a match, <code>found</code> will be true; otherwise, it'll be false. </p> <p> The date convention is now trivial: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">DateStringCustomization</span>()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;builder&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ISpecimenBuilder</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Create(request,&nbsp;context)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;request&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">TextEndsWithConvention</span>.Matches&nbsp;<span style="color:#a31515;">&quot;date&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;box&nbsp;((context.Resolve&nbsp;typeof&lt;<span style="color:#2b91af;">DateTimeOffset</span>&gt;).ToString()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:#2b91af;">NoSpecimen</span>&nbsp;request&nbsp;|&gt;&nbsp;box&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ICustomization</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Customize&nbsp;fixture&nbsp;=&nbsp;fixture.Customizations.Add&nbsp;builder</pre> </p> <p> The ID convention is very similar: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:#2b91af;">IdStringCustomization</span>()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;builder&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ISpecimenBuilder</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Create(request,&nbsp;context)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;request&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">TextEndsWithConvention</span>.Matches&nbsp;<span style="color:#a31515;">&quot;id&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;box&nbsp;((context.Resolve&nbsp;typeof&lt;<span style="color:#2b91af;">Guid</span>&gt;&nbsp;:?&gt;&nbsp;<span style="color:#2b91af;">Guid</span>).ToString&nbsp;<span style="color:#a31515;">&quot;N&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:#2b91af;">NoSpecimen</span>&nbsp;request&nbsp;|&gt;&nbsp;box&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ICustomization</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Customize&nbsp;fixture&nbsp;=&nbsp;fixture.Customizations.Add&nbsp;builder</pre> </p> <p> With these conventions in place in my entire test suite, I can simply follow them and get correct values. What happens if I refactor one of my fields so that they no longer have the correct suffix? That's likely to break my tests, but that's <em>a good thing</em>, because it alerts me that I deviated from the conventions, and (inadvertently, I should hope) made the production code less consistent. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Single Writer Web Jobs on Azure https://blog.ploeh.dk/2014/04/30/single-writer-web-jobs-on-azure 2014-04-30T06:39:00+00:00 Mark Seemann <div id="post"> <p> <em>How to ensure a Single Writer in load-balanced Azure deployments</em> </p> <p> In my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">Functional Architecture with F#</a> Pluralsight course, I describe how using the <a href="http://en.wikipedia.org/wiki/Actor_model">Actor model</a> (F# Agents) can make a concurrent system much simpler to implement, because the Agent can ensure that the system only has a Single Writer. Having a Single Writer eliminates much complexity, because while the writer decides what to write (if at all), nothing changes. Multiple readers can still read data, but as long as the Single Writer can keep up with input, this is a much simpler way to deal with concurrency than the alternatives. </p> <p> However, the problem is that while F# Agents work well on a single machine, they don't (currently) scale. This is particularly notable on Azure, because in order get the guaranteed SLA, you'll need to deploy your application to two or more nodes. If you have an F# Agent running on both nodes, obviously you no longer have a Single Writer, and everything just becomes much more difficult. If only there was a way to ensure a Single Writer in a distributed environment... </p> <p> Fortunately, it looks like the (in-preview) Azure feature <a href="http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs">Web Jobs</a> (inadvertently) solves this major problem for us. Web Jobs come in three flavours: <ul> <li>On demand</li> <li>Continuously running</li> <li>Scheduled</li> </ul> If you were to implement your writer as a web Job, the <em>On demand</em> option isn't particularly helpful, because you want the writer to run continuously, or at least very often. How about <em>Continuously running</em>, then? </p> <p> That turns out not to be a particularly useful option as well, because <blockquote> <a href="http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#CreateContinuous">"If your website runs on more than one instance, a continuously running task will run on all of your instances."</a> </blockquote> On the other hand <blockquote> <a href="http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#CreateContinuous">"On demand and scheduled tasks run on a single instance selected for load balancing by Microsoft Azure."</a> </blockquote> It sounds like <em>Scheduled</em> Web Jobs is just what we need! </p> <p> There's just one concern that we need to address: what happens if a Scheduled Web Job is taking too long running, in such a way that it hasn't completed when it's time to start it again. For example, what if you run a Scheduled Web Job every minute, but it sometimes takes 90 seconds to complete? If a new process starts executing while the first one is running, you would no longer have a Single Writer. </p> <p> Reading the <a href="http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs">documentation</a>, I couldn't find any information about how Azure handles this scenario, so I decided to perform some tests. </p> <p> The <a href="https://github.com/GreanTech/Qaiain">Qaiain</a> email micro-service proved to be a fine tool for the experiment. I slightly modified the code to wait for 90 seconds before exiting: </p> <p> <pre>[&lt;<span style="color:#2b91af;">EntryPoint</span>&gt;] <span style="color:blue;">let</span>&nbsp;main&nbsp;argv&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;queue&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">AzureQ</span>.dequeue&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some(msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg.AsString&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Mail</span>.deserializeMailData&nbsp;|&gt;&nbsp;send &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queue.DeleteMessage&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Async</span>.Sleep&nbsp;90000&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Async</span>.RunSynchronously &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;queue&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">AzureQ</span>.dequeue&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Some(msg)&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg.AsString&nbsp;|&gt;&nbsp;<span style="color:#2b91af;">Mail</span>.deserializeMailData&nbsp;|&gt;&nbsp;send &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queue.DeleteMessage&nbsp;msg &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;<span style="color:green;">//&nbsp;return&nbsp;an&nbsp;integer&nbsp;exit&nbsp;code</span></pre> </p> <p> In addition to that, I also changed how the subject of the email that I would receive would look, in order to capture the process ID of the running application, as well as the time it sent the email: </p> <p> <pre>smtpMsg.Subject&nbsp;&lt;- &nbsp;&nbsp;&nbsp;&nbsp;sprintf &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#a31515;">&quot;Process&nbsp;ID:&nbsp;%i,&nbsp;Time:&nbsp;%O&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Process.GetCurrentProcess().Id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTimeOffset.Now</pre> </p> <p> My hypothesis was that if Scheduled Web Jobs are well-behaved, a new job wouldn't start if an existing job was already running. Here are the results: </p> <p> <table> <thead> <tr><th>Time</th><th>Process</th></tr> </thead> <tbody> <tr><td>17:31:39</td><td>37936</td></tr> <tr><td>17:33:10</td><td>37936</td></tr> <tr><td>17:33:43</td><td>50572</td></tr> <tr><td>17:35:14</td><td>50572</td></tr> <tr><td>17:35:44</td><td>47632</td></tr> <tr><td>17:37:15</td><td>47632</td></tr> <tr><td>17:37:46</td><td>14260</td></tr> <tr><td>17:39:17</td><td>14260</td></tr> <tr><td>17:39:50</td><td>38464</td></tr> <tr><td>17:41:21</td><td>38464</td></tr> <tr><td>17:41:51</td><td>46052</td></tr> <tr><td>17:43:22</td><td>46052</td></tr> <tr><td>17:43:54</td><td>52488</td></tr> <tr><td>17:45:25</td><td>52488</td></tr> <tr><td>17:45:56</td><td>46816</td></tr> <tr><td>17:47:27</td><td>46816</td></tr> <tr><td>17:47:58</td><td>30244</td></tr> <tr><td>17:49:29</td><td>30244</td></tr> <tr><td>17:50:00</td><td>30564</td></tr> <tr><td>17:51:31</td><td>30564</td></tr> </tbody> </table> </p> <p> This looks great, but it's easier to see if I visualize it: </p> <p> <img src="/content/binary/scheduled-delayed-web-job-observations.png" alt="Scheduled delayed Web Job observations"> </p> <p> As you can see, processes do not overlap in time. This is a <strong>highly desirable result</strong>, because it seems to guarantee that we can have a Single Writer running in a distributed, load-balanced system. </p> <p> Azure Web Jobs are currently in preview, so let's hope the Azure team preserve this functionality in the final version. If you care about this, please let the team know. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Composed assertions with Unquote https://blog.ploeh.dk/2014/03/21/composed-assertions-with-unquote 2014-03-21T08:52:00+00:00 Mark Seemann <div id="post"> <p> <em>With F# and Unquote, you can write customized, composable assertions.</em> </p> <p> Yesterday, I wrote this unit test: </p> <p> <pre>[&lt;Theory;&nbsp;UnitTestConventions&gt;] <span style="color:blue;">let</span>&nbsp;PostReturnsCorrectResult &nbsp;&nbsp;&nbsp;&nbsp;(sut&nbsp;:&nbsp;TasksController) &nbsp;&nbsp;&nbsp;&nbsp;(task&nbsp;:&nbsp;TaskRendition)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;result&nbsp;:&nbsp;IHttpActionResult&nbsp;=&nbsp;sut.Post&nbsp;task &nbsp;&nbsp;&nbsp;&nbsp;verify&nbsp;&lt;@&nbsp;result&nbsp;:?&nbsp;Results.StatusCodeResult&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;verify&nbsp;&lt;@&nbsp;HttpStatusCode.Accepted&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(result&nbsp;:?&gt;&nbsp;Results.StatusCodeResult).StatusCode&nbsp;@&gt;</pre> </p> <p> For the record, here's the <a href="http://xunitpatterns.com/SUT.html">SUT</a>: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;TasksController()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;ApiController() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Post(task&nbsp;:&nbsp;TaskRendition)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.StatusCode&nbsp;HttpStatusCode.Accepted&nbsp;:&gt;&nbsp;IHttpActionResult</pre> </p> <p> There's not much to look at yet, because at that time, I was just getting started, and as always, I was using Test-Driven Development. The TasksController class is an <a href="http://www.asp.net/web-api">ASP.NET Web API</a> 2 Controller. In this incarnation, it merely accepts an HTTP POST, ignores the input, and returns <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3">202 (Accepted)</a>. </p> <p> The unit test uses <a href="http://www.nuget.org/packages/AutoFixture.Xunit">AutoFixture.Xunit</a> to create an instance of the SUT and a <a href="/2013/10/15/easy-aspnet-web-api-dtos-with-f-climutable-records">DTO record</a>, but that's not important in this context. It also uses <a href="http://www.swensensoftware.com/unquote">Unquote</a> for assertions, although I've aliased the <code>test</code> function to <code>verify</code>. Although Unquote is an extremely versatile assertion module, I wasn't happy with the assertions I wrote. </p> <h3 id="a76706907f384c1c9ae2f4cea143bb44"> What's the problem? <a href="#a76706907f384c1c9ae2f4cea143bb44" title="permalink">#</a> </h3> <p> The problem is the duplication of logic. First, it verifies that <code>result</code> is, indeed, an instance of StatusCodeResult. Second, if that's the case, it casts <code>result</code> to StatusCodeResult in order to access its concrete StatusCode property; it feels like I'm almost doing the same thing twice. </p> <p> You may say that this isn't a big deal in a test like this, but in my experience, this is a smell. The example looks innocuous, but soon, I'll find myself writing slightly more complicated assertions, where I need to type check and cast more than once. This can rapidly lead to <a href="http://xunitpatterns.com/Assertion%20Roulette.html">Assertion Roulette</a>. </p> <h3 id="b534f830babf4d9db337e39e4a8f9580"> The xUnit.net approach <a href="#b534f830babf4d9db337e39e4a8f9580" title="permalink">#</a> </h3> <p> For a minute there, I caught myself missing <a href="https://xunit.codeplex.com">xUnit.net</a>'s <code>Assert.IsAssignableFrom&lt;T&gt;</code> method, because it returns a value of type <code>T</code> if the conversion is possible. That would have enabled me to write something like: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;scr&nbsp;=&nbsp;Assert.IsAssignableFrom&lt;Results.StatusCodeResult&gt;&nbsp;result Assert.Equal(HttpStatusCode.Accepted,&nbsp;scr.StatusCode)</pre> </p> <p> It <em>seems</em> a little nicer, although in my experience, this quickly turns to spaghetti, too. Still, I found myself wondering if I could do something similar with Unquote. </p> <h3 id="521302fad41d4aef811b342d17da902d"> A design digression <a href="#521302fad41d4aef811b342d17da902d" title="permalink">#</a> </h3> <p> At this point, you are welcome to pull <a href="http://amzn.to/VI81bP">GOOS</a> at me and quote: <em>listen to your tests!</em> If the tests are difficult to write, you should reconsider your design; I agree, but I can't change the API of ASP.NET Web API. In Web API 1, my preferred return type for Controller actions were <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage.aspx">HttpResponseMessage</a>, but it was actually a bit inconvenient to work with in unit tests. Web API 2 introduces various IHttpActionResult implementations that are easier to unit test. Perhaps this could be better, but it seems like a step in the right direction. </p> <p> In any case, I can't change the API, so coming up with a better way to express the above assertion is warranted. </p> <h3 id="a17e365cc22e489c8f90885f1142d53d"> Composed assertions <a href="#a17e365cc22e489c8f90885f1142d53d" title="permalink">#</a> </h3> <p> To overcome this little obstacle, I wrote this function: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;convertsTo&lt;&#39;a&gt;&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;box&nbsp;candidate&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;:?&nbsp;&#39;a&nbsp;<span style="color:blue;">as</span>&nbsp;converted&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Some&nbsp;converted &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None</pre> </p> <p> (You have to love a language that let's you write <code>match&nbsp;box</code>! There's also a hint of <a href="http://en.wikipedia.org/wiki/Doge_(meme)">such nice</a> over <code>Some&nbsp;converted</code>...) </p> <p> The convertsTo function takes any object as input, and returns an Option containing the converted value, if the conversion is possible; otherwise, it returns None. In other words, the signature of the convertsTo function is <code>obj&nbsp;->&nbsp;'a&nbsp;option</code>. </p> <p> This enables me to write the following Unquote assertion: </p> <p> <pre>[&lt;Theory;&nbsp;UnitTestConventions&gt;] <span style="color:blue;">let</span>&nbsp;PostReturnsCorrectResult &nbsp;&nbsp;&nbsp;&nbsp;(sut&nbsp;:&nbsp;TasksController) &nbsp;&nbsp;&nbsp;&nbsp;(task&nbsp;:&nbsp;TaskRendition)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;result&nbsp;:&nbsp;IHttpActionResult&nbsp;=&nbsp;sut.Post&nbsp;task &nbsp;&nbsp;&nbsp;&nbsp;verify&nbsp;&lt;@ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;convertsTo&lt;Results.StatusCodeResult&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;x.StatusCode) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.exists&nbsp;((=)&nbsp;HttpStatusCode.Accepted)&nbsp;@&gt;</pre> </p> <p> While this looks more verbose than my two original assertions, this approach is more <em>composable</em>. </p> <p> The really beautiful part of this is that Unquote can still tell me what goes wrong, if the test doesn't pass. As an example, if I change the SUT to: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;TasksController()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;ApiController() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Post(task&nbsp;:&nbsp;TaskRendition)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Ok()&nbsp;:&gt;&nbsp;IHttpActionResult</pre> </p> <p> The assertion message is: </p> <p> <pre>System.Web.Http.Results.OkResult |> Dsl.convertsTo |> Option.map (fun x -> x.StatusCode) |> Option.exists ((=) Accepted) None |> Option.map (fun x -> x.StatusCode) |> Option.exists ((=) Accepted) None |> Option.exists ((=) Accepted) false</pre> </p> <p> Notice how, in a <em>series of reductions</em>, Unquote breaks down for me exactly what went wrong. The top line is my original expression. The next line shows me the result of evaluating <code>System.Web.Http.Results.OkResult&nbsp;|&gt;&nbsp;Dsl.convertsTo</code>; the result is None. Already at this point, it should be quite evident what the problem is, but in the next line again, it shows the result of evaluating <code>None&nbsp;|&gt;&nbsp;Option.map&nbsp;(fun&nbsp;x&nbsp;-&gt;&nbsp;x.StatusCode)</code>; again, the result is None. Finally, it shows the result of evaluating <code>None&nbsp;|&gt;&nbsp;Option.exists&nbsp;((=)&nbsp;Accepted)</code>, which is false. </p> <p> Here's another example. Assume that I change the SUT to this: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;TasksController()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">inherit</span>&nbsp;ApiController() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.Post(task&nbsp;:&nbsp;TaskRendition)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.StatusCode&nbsp;HttpStatusCode.OK&nbsp;:&gt;&nbsp;IHttpActionResult</pre> </p> <p> In this example, instead of returning the wrong implementation of IHttpActionResult, the SUT does return a StatusCodeResult instance, but with the wrong status code. Unquote is still very helpful: </p> <p> <pre>System.Web.Http.Results.StatusCodeResult |> Dsl.convertsTo |> Option.map (fun x -> x.StatusCode) |> Option.exists ((=) Accepted) Some System.Web.Http.Results.StatusCodeResult |> Option.map (fun x -> x.StatusCode) |> Option.exists ((=) Accepted) Some OK |> Option.exists ((=) Accepted) false</pre> </p> <p> Notice that it still uses a series of reductions to show how it arrives at its conclusion. Again, the first line is the original expression. The next line shows the result of evaluating <code>System.Web.Http.Results.StatusCodeResult&nbsp;|&gt;&nbsp;Dsl.convertsTo</code>, which is <code>Some&nbsp;System.Web.Http.Results.StatusCodeResult</code>. So far so good; this is as required. The third line shows the result of evaluating <code>Some&nbsp;System.Web.Http.Results.StatusCodeResult&nbsp;|&gt;&nbsp;Option.map&nbsp;(fun&nbsp;x&nbsp;-&gt;&nbsp;x.StatusCode)</code>, which is <code>Some&nbsp;OK</code>. Still good. Finally, it shows the result of evaluating <code>Some&nbsp;OK&nbsp;|&gt;&nbsp;Option.exists&nbsp;((=)&nbsp;Accepted)</code>, which is false. The value in the option was HttpStatusCode.OK, but should have been HttpStatusCode.Accepted. </p> <h3 id="873a84d9cc0045c28247e153e3ffa032"> Summary <a href="#873a84d9cc0045c28247e153e3ffa032" title="permalink">#</a> </h3> <p> Unquote is a delight to work with. As <a href="http://www.swensensoftware.com/unquote">the project site</a> explains, it's not an API or a DSL. It just evaluates and <em>reports</em> on the expressions <em>you</em> write. If you already know F#, you already know how to use Unquote, and you can write your assertion expressions as expressive and complex as you want. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Exude https://blog.ploeh.dk/2014/03/14/exude 2014-03-14T14:50:00+00:00 Mark Seemann <div id="post"> <p> <em>Announcing Exude, an extension to xUnit.net providing test cases as first-class, programmatic citizens.</em> </p> <p> Sometimes, when writing <a href="http://xunitpatterns.com/Parameterized%20Test.html">Parameterized Tests</a> with <a href="https://xunit.codeplex.com">xUnit.net</a>, you need to provide parameters that don't lend themselves easily to be defined as constants in <code>[InlineData]</code> attributes. </p> <p> In <a href="http://grean.com">Grean</a>, we've let ourselves inspire by Mauricio Scheffer's blog post <a href="http://bugsquash.blogspot.dk/2012/05/first-class-tests-in-mbunit.html">First-class tests in MbUnit</a>, but ported the concept to xUnit.net and released it as open source. </p> <p> It's called <strong>Exude</strong> and is <a href="https://github.com/GreanTech/Exude">available on GitHub</a> and <a href="http://www.nuget.org/packages/Exude">on NuGet</a>. </p> <p> Here's a small example: </p> <p> <pre>[<span style="color:#2b91af;">FirstClassTests</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">ITestCase</span>&gt;&nbsp;YieldFirstClassTests() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TestCase</span>(_&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(1,&nbsp;1)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TestCase</span>(_&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(2,&nbsp;2)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">TestCase</span>(_&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(3,&nbsp;3)); }</pre> </p> <p> More examples and information is available on <a href="https://github.com/GreanTech/Exude">the project site</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Arbitrary Version instances with FsCheck https://blog.ploeh.dk/2014/03/11/arbitrary-version-instances-with-fscheck 2014-03-11T10:01:00+00:00 Mark Seemann <div id="post"> <p> <em>This post explains how to configure FsCheck to create arbitrary Version values.</em> </p> <p> When I <a href="/2011/05/09/GenericunittestingwithxUnit.net">unit test generic classes or methods</a>, I often like to use <a href="http://msdn.microsoft.com/en-us/library/system.version.aspx">Version</a> as one of the type arguments. The Version class is a great test type because <ul> <li>it's readily available, as it's defined in the System namespace in mscorlib</li> <li>it overrides Equals so that it's easy to compare two values</li> <li>it's a complex class, because it composes four integers, so it's a good complement to String, Int32, Object, Guid, and other primitive types</li> </ul> </p> <p> Recently, I've been picking up <a href="https://github.com/fsharp/FsCheck">FsCheck</a> to do Property-Based Testing, but out of the box it doesn't know how to create arbitrary Version instances. </p> <p> It turns out that you can easily and elegantly tell FsCheck how to create arbitrary Version instances, but since I haven't seen it documented, I thought I'd share my solution: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;Generators&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">member</span>&nbsp;Version()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arb.generate&lt;byte&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Gen.map&nbsp;int &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Gen.four &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Gen.map&nbsp;(<span style="color:blue;">fun</span>&nbsp;(ma,&nbsp;mi,&nbsp;bu,&nbsp;re)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Version(ma,&nbsp;mi,&nbsp;bu,&nbsp;re)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Arb.fromGen</pre> </p> <p> As the <a href="https://github.com/fsharp/FsCheck/blob/master/Docs/Documentation.md">FsCheck documentation</a> explains, you can create custom Generator by defining a static class that exposes members that return Arbitrary&lt;'a&gt; - in this case Arbitrary&lt;Version&gt;. </p> <p> If you'd like me to walk you through what happens here, read on, and I'll break it down for you. </p> <p> First, <code>Arb.generate&lt;byte&gt;</code> is a Generator of Byte values. While FsCheck doesn't know how to create arbitrary Version values, it <em>does</em> know how to create arbitrary values of various primitive types, such as Byte, Int32, String, and so on. The Version constructors expect components as Int32 values, so why did I select Byte values instead? Because Version doesn't accept negative numbers, and if I had kicked off my Generator with <code>Arb.generate&lt;int&gt;</code>, it would have created all sorts of integers, including negative values. While it's possible to filter or modify the Generator, I thought it was easier to simply kick off the Generator with Byte values, because they are never negative. </p> <p> Second, <code>Gen.map int</code> converts the initial Gen&lt;byte&gt; to Gen&lt;int&gt; by invoking F#'s built-in <code>int</code> conversion function. </p> <p> Third, <code>Gen.four</code> is a built-in FsCheck Generator Combinator that converts a Generator into a Generator of four-element tuples; in this case it converts Get&lt;int&gt; to Gen&lt;int&nbsp;*&nbsp;int&nbsp;*&nbsp;int&nbsp;*&nbsp;int&gt;: a Generator of a four-integer tuple. </p> <p> Fourth, <code>Gen.map (fun (ma, mi, bu, re) -> Version(ma, mi, bu, re))</code> converts Gen&lt;int&nbsp;*&nbsp;int&nbsp;*&nbsp;int&nbsp;*&nbsp;int&gt; to Gen&lt;Version&gt; by another application of Gen.map. The function supplied to Gen.map takes the four-element tuple of integers and invokes the Version constructor with the major, minor, build, and revision integer values. </p> <p> Finally, <code>Arb.fromGen</code> converts Gen&lt;Version&gt; to Arbitrary&lt;Version&gt;, which is what the member must return. </p> <p> To register the Generators custom class with FsCheck, I'm currently doing this: </p> <p> <pre><span style="color:blue;">do</span>&nbsp;Arb.register&lt;Generators&gt;()&nbsp;|&gt;&nbsp;ignore </pre> </p> <p> You can see this entire code in context <a href="https://github.com/GreanTech/Champagne/blob/86e893c923786e62e3a6091dc1c8618f531eacaa/Src/Champagne.FSharp.UnitTests/Tests.fs">here</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. SOLID: the next step is Functional https://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional 2014-03-10T08:33:00+00:00 Mark Seemann <div id="post"> <p> <em>If you take the SOLID principles to their extremes, you arrive at something that makes Functional Programming look quite attractive.</em> </p> <p> You may have seen this one before, but bear with me :) </p> <blockquote> <p> The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures." </p> <p> Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress. </p> <p> On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened. </p> <p> - <a href="http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html">Anton van Straaten</a> </p> </blockquote> <p> While this is a lovely parable, it's not a new observation that objects and closures seem closely related, and <a href="http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent">there has been much discussion back and forth about this already</a>. Still, in light of a recent question and answer about <a href="http://stackoverflow.com/q/22263779/126014">how to move from Object-Oriented Composition to Functional Composition</a>, I'd still like to explain how, in my experience, the <a href="http://en.wikipedia.org/wiki/Solid_%28object-oriented_design%29">SOLID</a> principles lead to a style of design that makes Functional Programming quite attractive. </p> <h3 id="24686477b4784643be220c51a9326cfc"> A SOLID road map <a href="#24686477b4784643be220c51a9326cfc" title="permalink">#</a> </h3> <p> In a previous article, I've described how <a href="/2011/06/07/SOLIDCodeisnt">application of the Single Responsibility Principle (SRP) leads to many small classes</a>. Furthermore, if you rigorously apply the <a href="http://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> (ISP), you'll understand that you should favour <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a> over <a href="http://martinfowler.com/bliki/HeaderInterface.html">Header Interfaces</a>. </p> <p> If you keep driving your design towards smaller and smaller interfaces, you'll eventually arrive at the ultimate Role Interface: an interface with a single method. This happens to me a lot. Here's an example: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMessageQuery</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;Read(<span style="color:blue;">int</span>&nbsp;id); }</pre> </p> <p> If you apply the SRP and ISP like that, you're likely to evolve a code base with <strong>many fine-grained classes that each have a single method</strong>. That has happened to me more than once; <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> is an example of a big and complex code base that looks like that, but <a href="https://github.com/ploeh">my other publicly available code bases</a> tend to have the same tendency. In general, this works well; the most consistent problem is that it tends to be a bit verbose. </p> <h3 id="8c84c8afb08b451b8ef01f8ce7e0c052"> Objects as data with behaviour <a href="#8c84c8afb08b451b8ef01f8ce7e0c052" title="permalink">#</a> </h3> <p> One way to characterise objects is that they are <em>data with behaviour</em>; that's a good description. In practice, when you have many fine-grained classes with a single method, you may have classes like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FileStore</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMessageQuery</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">DirectoryInfo</span>&nbsp;workingDirectory; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;FileStore(<span style="color:#2b91af;">DirectoryInfo</span>&nbsp;workingDirectory) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.workingDirectory&nbsp;=&nbsp;workingDirectory; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Read(<span style="color:blue;">int</span>&nbsp;id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;path&nbsp;=&nbsp;<span style="color:#2b91af;">Path</span>.Combine( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.workingDirectory.FullName, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id&nbsp;+&nbsp;<span style="color:#a31515;">&quot;.txt&quot;</span>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">File</span>.ReadAllText(path); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This FileStore class is a simple example of data with behaviour. <ul> <li> The <strong>behaviour</strong> is the Read method, which figures out a file path for a given ID and returns the contents of the file. </li> <li> The <strong>data</strong> (also sometimes known as the <em>state</em>) is the <code>workingDirectory</code> field. </li> </ul> In this example, the data is immutable and <a href="/2011/05/30/DesignSmellDefaultConstructor">passed in via the constructor</a>, but it could also have been a public, writeable property, or even a public field. </p> <p> The <code>workingDirectory</code> field is a <a href="/2012/08/31/ConcreteDependencies">Concrete Dependency</a>, but it could also have been a <a href="/2012/07/02/PrimitiveDependencies">primitive value</a> or an interface or abstract base class. In the last case, we would often call the pattern Constructor Injection. </p> <p> Obviously, the data could be multiple values, instead of a single value. </p> <p> The FileStore example class implements the IMessageQuery interface, so it's a very representative example of what happens when you take the SRP and ISP to their logical conclusions. It's a fine class, although a little verbose. </p> <p> When designing like this, not only do you have to come up with a name for the interface itself, but also for the method, and for each concrete class you create to implement the interface. Naming is difficult, and in such cases, you have to name the same concept twice or more. This often leads to interfaces named IFooer with a method called Foo, IBarer with a method called Bar, etc. You get the picture. This is a smell (that also seems vaguely reminiscent of the <a href="http://codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle</a>). There must be a better way. </p> <p> Hold that thought. </p> <h3 id="e68a3b8ef483430a849d44cefbf44aa7"> Functions as pure behaviour <a href="#e68a3b8ef483430a849d44cefbf44aa7" title="permalink">#</a> </h3> <p> As the introductory parable suggests, perhaps Functional Programming offers an alternative. Before you learn about Closures, though, you'll need to understand Functions. In Functional Programming, a Function is often defined as a Pure Function - that is: a deterministic operation without side-effects. </p> <p> Since C# has some Functional language support, I'll first show you the FileStore.Read method as a Pure Function in C#: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">DirectoryInfo</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;read&nbsp;=&nbsp;(workingDirectory,&nbsp;id)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;path&nbsp;=&nbsp;<span style="color:#2b91af;">Path</span>.Combine(workingDirectory.FullName,&nbsp;id&nbsp;+&nbsp;<span style="color:#a31515;">&quot;.txt&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">File</span>.ReadAllText(path); &nbsp;&nbsp;&nbsp;&nbsp;};</pre> </p> <p> This Function does the same as the FileStore.Read method, but it has no data. You must pass in the working directory as a function argument just like the ID. This doesn't seem equivalent to an object. </p> <h3 id="c1bc2a664edd49a3a57c466c2ad77b04"> Closures as behaviour with data <a href="#c1bc2a664edd49a3a57c466c2ad77b04" title="permalink">#</a> </h3> <p> A Closure is an important concept in Functional Programming. In C# it looks like this: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;workingDirectory&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DirectoryInfo</span>(<span style="color:#2b91af;">Environment</span>.CurrentDirectory); <span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;read&nbsp;=&nbsp;id&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;path&nbsp;=&nbsp;<span style="color:#2b91af;">Path</span>.Combine(workingDirectory.FullName,&nbsp;id&nbsp;+&nbsp;<span style="color:#a31515;">&quot;.txt&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">File</span>.ReadAllText(path); &nbsp;&nbsp;&nbsp;&nbsp;};</pre> </p> <p> This is called a Closure because the Function <em>closes over</em> the <em>Outer Variable</em> <code>workingDirectory</code>. Effectively, the function captures the value of the Outer Variable. </p> <p> What does that compile to? </p> <p> Obviously, the above C# code compiles to IL, but if you reverse-engineer the IL back to C#, this is what it looks like: </p> <p> <pre>[<span style="color:#2b91af;">CompilerGenerated</span>] <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;&lt;&gt;c__DisplayClass3 { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DirectoryInfo</span>&nbsp;workingDirectory; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;&lt;UseClosure&gt;b__2(<span style="color:blue;">int</span>&nbsp;id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">File</span>.ReadAllText( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Path</span>.Combine(<span style="color:blue;">this</span>.workingDirectory.FullName,&nbsp;id&nbsp;+&nbsp;<span style="color:#a31515;">&quot;.txt&quot;</span>)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> It's a class with a field and a method! Granted, the names look somewhat strange, and the field is a public, mutable field, but it's essentially identical to the FileStore class! </p> <p> Closures are behaviour with data, whereas objects are data with behaviour. Hopefully, the opening parable makes sense to you now. This is an example of one of <a href="http://en.wikipedia.org/wiki/Erik_Meijer_(computer_scientist)">Erik Meijer's</a> favourite design concepts called <em>duality</em>. </p> <h3 id="c2ca258562064d16bbcd1cb06664457e"> Partial Function Application <a href="#c2ca258562064d16bbcd1cb06664457e" title="permalink">#</a> </h3> <p> Another way to close over data is called Partial Function Application, but the result is more or less the same. Given the original pure function: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">DirectoryInfo</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;read&nbsp;=&nbsp;(workingDirectory,&nbsp;id)&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;path&nbsp;=&nbsp;<span style="color:#2b91af;">Path</span>.Combine(workingDirectory.FullName,&nbsp;id&nbsp;+&nbsp;<span style="color:#a31515;">&quot;.txt&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">File</span>.ReadAllText(path); &nbsp;&nbsp;&nbsp;&nbsp;};</pre> </p> <p> you can create a <em>new</em> Function from the first Function by only invoking it with some of the arguments: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;wd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DirectoryInfo</span>(<span style="color:#2b91af;">Environment</span>.CurrentDirectory); <span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;r&nbsp;=&nbsp;id&nbsp;=&gt;&nbsp;read(wd,&nbsp;id);</pre> </p> <p> The <code>r</code> function also closes over the <code>wd</code> variable, and the compiled IL is very similar to before. </p> <h3 id="514e66db3f664a1a876ded2f27988910"> Just use F#, then! <a href="#514e66db3f664a1a876ded2f27988910" title="permalink">#</a> </h3> <p> If SOLID leads you to many fine-grained classes with a single method, C# starts to be in the way. A class like the above FileStore class is proper Object-Oriented Code, but is quite verbose; the Closures and Partially Applied Functions compile, but are hardly idiomatic C# code. </p> <p> On the other hand, in F#, the above Closure is simply written as: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;workingDirectory&nbsp;=&nbsp;DirectoryInfo(Environment.CurrentDirectory) <span style="color:blue;">let</span>&nbsp;read&nbsp;id&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;path&nbsp;=&nbsp;Path.Combine(workingDirectory.FullName,&nbsp;id.ToString()&nbsp;+&nbsp;<span style="color:#a31515;">&quot;.txt&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;File.ReadAllText(path)</pre> </p> <p> The <code>read</code> value is a Function with the signature <code>'a -&gt; string</code>, which means that it takes a value of the generic type <code>'a</code> (in C#, it would typically have been named <code>T</code>) and returns a string. This is just a more general version of the IMessageQuery.Read method. When <code>'a</code> is <code>int</code>, it's the same signature, but in F#, I only had to bother naming the Function itself. <a href="/2009/05/28/DelegatesAreAnonymousInterfaces">Functions are anonymous interfaces</a>, so these are also equivalent. </p> <p> Likewise, if you have a Pure Function like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;read&nbsp;(workingDirectory&nbsp;:&nbsp;DirectoryInfo)&nbsp;id&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;path&nbsp;=&nbsp;Path.Combine(workingDirectory.FullName,&nbsp;id.ToString()&nbsp;+&nbsp;<span style="color:#a31515;">&quot;.txt&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;File.ReadAllText(path)</pre> </p> <p> the Partially Applied Function is written like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;wd&nbsp;=&nbsp;DirectoryInfo(Environment.CurrentDirectory) <span style="color:blue;">let</span>&nbsp;r&nbsp;=&nbsp;read&nbsp;wd</pre> </p> <p> The <code>r</code> Function is another Function that takes an ID as input, and returns a string, but notice how much less ceremony is involved. </p> <h3 id="fcfd814a70784f268359397d7d8fa74a"> Summary <a href="#fcfd814a70784f268359397d7d8fa74a" title="permalink">#</a> </h3> <p> SOLID, particularly the SRP and ISP, leads you towards code bases with many fine-grained classes with a single method. Such objects represent data with behaviour, but can also be modelled as behaviour with data: Closures. When that happens repeatedly, it's time to make the switch to a Functional Programming Language like F#. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e2bd84c1ebc44be99c7965be756de7be"> <div class="comment-author"><a href="http://neildanson.wordpress.com">Neil Danson</a> <a href="#e2bd84c1ebc44be99c7965be756de7be">#</a></div> <div class="comment-content">Looks like we had similar thoughts at the same time - <a href="http://neildanson.wordpress.com/2014/03/04/it-just-works/">mine are here</a> <br> It's surprising to me that we've not moved more to the functional paradigm as an industry, when so many pieces of evidence point to it working more effectively than OO. <br> It feels like people can't seem to break away from those curly braces, which is perhaps why Scala is doing so well on the JVM. </div> <div class="comment-date">2014-03-10 11:51 UTC</div> </div> <div class="comment" id="31afeb6f39ca4a1fbd62e44c9fa1b448"> <div class="comment-author"><a href="http://richiban.wordpress.com">Richard Gibson</a> <a href="#31afeb6f39ca4a1fbd62e44c9fa1b448">#</a></div> <div class="comment-content"> I like where you're going with this post, but I just can't get my head round how you would consume the closure you've written. Most of the time you would consume the IMessageQuery by taking it in your class's constructor and letting your DI framework new it up for you: <pre>public class MyService { ... public MyService(IMessageQuery messageQuery) {...} }</pre> How would you do this with a closure? Your function no longer has a type that we can use (it's just <code>int&nbsp;->&nbsp;string</code>). Surely your service doesn't look like this? <pre>type MyService (messageQuery: int -> string) = ...</pre> How would you register the types for injection in this example? </div> <div class="comment-date">2014-03-11 10:05 UTC</div> </div> <div class="comment" id="ea8b3c93fd2f4acdacf08c3d053533db"> <div class="comment-author"><a href="http://gorodinski.com">Lev Gorodinski</a> <a href="#ea8b3c93fd2f4acdacf08c3d053533db">#</a></div> <div class="comment-content"> Great explanation and justification! I believe the story goes further beyond SOLID into many other patterns. I wrote a post about <a href="http://gorodinski.com/blog/2013/09/18/oop-patterns-from-a-functional-perspective/">OOP patterns from Functional Perspective.</a> </div> <div class="comment-date">2014-03-11 13:18 UTC</div> </div> <div class="comment" id="4ba062e6300b4379a6cf95cdeea89b9a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4ba062e6300b4379a6cf95cdeea89b9a">#</a></div> <div class="comment-content"> <p> Richard, thank you for writing. You ask "Surely your service doesn't look like this? <code>type&nbsp;MyService&nbsp;(messageQuery:&nbsp;int&nbsp;->&nbsp;string)&nbsp;=&nbsp;...</code>" </p> <p> Probably not. Why even have a class? A client consuming the closure would just take it as a function argument: </p> <p> <pre>let myClient f = let message = f 42 // Do something else interesting... // Return a result...</pre> </p> <p> Here, <code>f</code> is a function with the <code>int&nbsp;->&nbsp;string</code> signature, and <code>myClient</code> is another function. Just as you can keep on composing classes using the Composite, Decorator, and Adapter patterns, you can keep on composing functions with other functions by taking functions as function arguments. </p> <p> At the top level of your application, you may have to implement a class to fit into a framework. For an example of integrating with the ASP.NET Web API, see my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">A Functional Architecture with F#</a> Pluralsight course. </p> <p> When it comes to integrating with a DI Container, I tend to <a href="/2012/11/06/WhentouseaDIContainer">not care about that these days</a>. I prefer composing the application with Poor Man's DI, and that works beautifully with F#. </p> </div> <div class="comment-date">2014-03-11 16:57 UTC</div> </div> <div class="comment" id="5b56a0b2f1f04d36a9722c36761e78da"> <div class="comment-author"><a href="http://www.truewill.net/">Bill Sorensen</a> <a href="#5b56a0b2f1f04d36a9722c36761e78da">#</a></div> <div class="comment-content"> <p>Excellent post!</p> <p>Under "Partial Function Application", you state "Given the original pure function" - the file I/O would appear to make that impure. Similarly under "Just use F#, then!" with "Likewise, if you have a Pure Function like this".</p> </div> <div class="comment-date">2014-03-12 17:40 UTC</div> </div> <div class="comment" id="56b06b0b8b774ab4b4c5ef94ee6cce91"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#56b06b0b8b774ab4b4c5ef94ee6cce91">#</a></div> <div class="comment-content"> <p> Bill, you are correct! I may have gotten a little carried away at that point. The method is side-effect-free, and deterministic (unless someone comes by and changes the file), but it <em>does</em> depend on state on disk. Thank you for pointing that out; I stand corrected. Hopefully, that mistake of mine doesn't detract from the overall message. </p> </div> <div class="comment-date">2014-03-12 19:26 UTC</div> </div> <div class="comment" id="6bc95324f8b5493e83fc51512569b456"> <div class="comment-author">Leif Battermann <a href="#6bc95324f8b5493e83fc51512569b456">#</a></div> <div class="comment-content"> <p> Hey Mark, obviously switching to F# is not always that easy. I currently have a very similar situation like the one you describe in this post. I refactored the code to using partial application and a functional programming style with C# which works fine. You are saying that the two approaches are actually more or less the same thing which I can see. I am wondering now what the benefit is from refactoring to a functional style with partial application? Does it make sense to do that using C#? The dependencies that I inject are repositories with DB access. So I don't get the true benefits of FP because of the state of the DB. Is it still reasonable to switch to the FP approach? Personally I just like the style and I think it is a littel bit cleaner to have no constructors and private fields. Any thoughts on that? Thanks, Leif. </p> </div> <div class="comment-date">2014-04-02 11:21 UTC</div> </div> <div class="comment" id="c3da80fbc5664122904c3d71d8e22e6e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c3da80fbc5664122904c3d71d8e22e6e">#</a></div> <div class="comment-content"> <p> Leif, thank you for writing. Is there value in adopting a functional style in C#? Yes, I think so, but not (in my opinion) from closures or partial function application. While it's possible to do this in C#, the syntax is awkward compared to F#. It also goes somewhat against the grain of C#. </p> <p> The main benefit from FP is immutable state, which makes it much easier to reason about the code and the state of the application. Once you understand how to model a problem around immutable data, even C# code becomes much easier to reason about, so I definitely think it makes sense to adopt patterns for working with immutable data in C#. </p> <p> For years, I've written C# code like that. Not only is it possible, but I strongly prefer it over more 'normal' C# with mutable state. Still, there's a lot of boilerplate code you have to write in C#, such as constructors and read-only property pairs, copy-and-update methods, structural equality, etc. After having done that for a couple of years, I got tired of writing all that boilerplate code, when I get it for free in F#. </p> <p> Like you, I still have a large body of C# code that I have to maintain, so while I choose F# for most new development, I write 'functional C#' in my C# code bases. Even if there are small pockets of mutable state here and there (like you describe), I still think it makes sense to keep as much as possible immutable. </p> </div> <div class="comment-date">2014-04-03 17:34 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Using NuGet with autonomous repositories https://blog.ploeh.dk/2014/02/03/using-nuget-with-autonomous-repositories 2014-02-03T16:06:00+00:00 Mark Seemann <div id="post"> <p> <em>NuGet is a great tool if used correctly. Here's one way to do it.</em> </p> <p> In my <a href="/2014/01/29/nuget-package-restore-considered-harmful">recent post about NuGet</a>, I described why the Package Restore feature is insidious. As expected, this provoked some readers, who didn't like my recommendation of adding NuGet packages to source control. That's understandable; the problem with a rant like my previous post is that while it tells you what <em>not</em> to do, it's not particularly constructive. While I told you to store NuGet packages in your source control system, I didn't describe patterns for doing it effectively. My impression was that it's trivial to do this, but based on the reactions I got, I realize that this may not be the case. Could it be that some readers react strongly because they don't know what else to do (than to use NuGet Package Restore)? In this post, I'll describe a way to use and organize NuGet packages that have worked well for me in several organizations. </p> <h3 id="a4142d751602487d9958d355cd077b48"> Publish/Subscribe <a href="#a4142d751602487d9958d355cd077b48" title="permalink">#</a> </h3> <p> In <a href="http://grean.com">Grean</a> we use NuGet in <em>a sort</em> of Publish/Subscribe style. This is a style I've also used in other organizations, to great effect. It's easy: create reusable components as autonomous libraries, and publish them as NuGet packages. If you don't feel like sharing your internal building blocks with the rest of the world, you can use a custom, internal package repository, or you can use <a href="http://www.myget.org">MyGet</a> (that's what we do in Grean). </p> <p> A reusable component may be some package you've created for internal use. Something that packages the way you authenticate, log, instrument, render, etc. in your organization. </p> <p> Every time you have a new version of one of your components (let's call it <em>C1</em>), you publish the NuGet package. </p> <p> <img src="/content/binary/pub-sub-repositories.png" alt="Diagram showing pull and push from repositories."> </p> <p> Just like other Publish/Subscribe systems, the only other party that you rely on at this moment is the queue/bus/broker - in this case the package repository, like NuGet.org or MyGet.org. No other systems need to be available to do this. </p> <p> You do this for every reusable component you want to publish. Each is independent of other components. </p> <h3 id="3f263a3838a9406082e55abc1794c118"> Pull based on need <a href="#3f263a3838a9406082e55abc1794c118" title="permalink">#</a> </h3> <p> In addition to reusable components, you probably also build <em>systems</em>; that is, applications that actually <em>do</em> something. You probably build those systems on top of reusable components - yours, and other publicly available NuGet packages. Let's call one such system <em>S1</em>. </p> <p> Whenever you need a NuGet package (C1), you add it to the Visual Studio project where you need it, and then you <em>commit your changes</em> to that system's source control. It effectively means checking in the NuGet package, including all the binaries, to source control. However, the S1 repository is not the same repository as the C1 repository. Both are autonomous systems. </p> <p> The only system you need to be available when you add the NuGet package C1 is the NuGet package source (NuGet.org, MyGet.org, etc.). The only system you need to be available to commit the changes to S1 is your source control system, and if you use a <a href="http://en.wikipedia.org/wiki/Distributed_version_control_system">Distributed Version Control System</a> (DVCS), it's always going to be available. </p> <p> Pretty trivial so far. </p> <p> "This isn't pub/sub," you'll most likely say. That's right, not in the traditional sense. Still, if you adopt the pattern language of <a href="http://amzn.to/11Xr8Pn">Enterprise Integration Patterns</a>, you can think of yourself (and your colleagues) as a <a href="http://www.eaipatterns.com/PollingConsumer.html">Polling Consumer</a>. </p> <p> "But," I suppose you'll say, "I'm not polling the repository and pulling down every package ever published." </p> <p> True, but you could, and if you did, you'd most likely be filtering away most package updates, because they don't apply to your system. That corresponds to applying a <a href="http://www.eaipatterns.com/Filter.html">Message Filter</a>. </p> <p> This last part is important, so let me rephrase it: </p> <p> <blockquote> Just because your system uses a particular NuGet package, it doesn't mean that you have to install <em>every single version</em> ever published. </blockquote> </p> <p> It seems to me that at least some of the resistance to adding packages to your repository is based on something like that. As <a href="https://twitter.com/ursenzler">Urs Enzler</a> writes: <blockquote> [Putting packages in source control is] <a href="https://twitter.com/ursenzler/status/428969958228656128">"not an option if your repo grows > 100GB per month due to monthly updates of BIG nuget packages"</a> </blockquote> While I'm not at all in possession of all the facts regarding Urs Enzler's specific problems, it just got me thinking: do you really need to update your local packages every time a new package is published? You shouldn't have to, I think. </p> <p> As an example, consider my own open source project <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>, which keeps a fairly high release cadence. It's released according to the principles of <a href="http://en.wikipedia.org/wiki/Continuous_delivery">Continuous Delivery</a>, so every time there's a new feature or fix, we release a new NuGet package. In 2013, we released 47 versions of the <a href="https://www.nuget.org/packages/AutoFixture">AutoFixture NuGet package</a>, including one major release. That's almost a release every week, but while I use AutoFixture in many other projects, I don't try to keep up with it. I just install AutoFixture when I start a new project, and then I mostly update the package if I need one of the new features or bug fixes. Occasionally, I also update packages in order to not fall too much behind. </p> <p> As a publicly visible case, consider <a href="https://github.com/ploeh/Hyprlinkr">Hyprlinkr</a>, which uses AutoFixture as one of its dependencies. While going though Hyprlinkr's NuGet packages recently, I discovered that the Hyprlinkr code base was using AutoFixture 2.12.0 - an 18 months old version! I simply hadn't needed to update the package during that time. AutoFixture follows <a href="http://semver.org">Semantic Versioning</a>, and we go to great lengths to ensure that we don't break existing functionality (unless we do a major release). </p> <p> Use the NuGet packages you need, commit them to source control, and update them <em>as necessary</em>. For all well-designed packages, you should be able to skip versions without ill effects. This enables you to treat the code bases for each system (S1, S2, etc.) as <em>autonomous systems</em>. Everything you need in order to work with that code base is right there in the source code repository. </p> <h3 id="0368eb5d70414a5aae9b229378b40b77"> Stable Dependency Principle <a href="#0368eb5d70414a5aae9b229378b40b77" title="permalink">#</a> </h3> <p> What if you need to keep up-to-date with a package that rapidly evolves? From Urs Enzler's <a href="https://twitter.com/ursenzler/status/428969958228656128">tweet</a>, I get the impression that this is the case not only for Urs, but for other people too. Imagine that the creator of such a package frequently publishes new versions, and that you <em>have</em> to keep up to date. If that's the case, it must imply that the package isn't stable, because otherwise, you'd be able to skip updates. </p> <p> Let me repeat that: </p> <p> <blockquote> If you depend on a NuGet package, and you <em>have</em> to stay up-to-date, it implies that the package is unstable. </blockquote> </p> <p> If this is the case, you have an entirely other problem on your hand. It has nothing to do with NuGet Package Restore, or whether you're keeping packages in source control or not. It means that you're violating the <a href="http://c2.com/cgi/wiki?StableDependenciesPrinciple">Stable Dependencies Principle</a> (SDP). If you feel pain in that situation, that's expected, but the solution isn't Package Restore, but a better dependency hierarchy. </p> <p> If you can invert the dependency, you can solve the problem. If you can't invert the dependency, you'd probably benefit from an <a href="http://amzn.to/WBCwx7">Anti-corruption Layer</a>. There are plenty of better solution that address the root cause of your problems. NuGet Package Restore, on the other hand, is only symptomatic relief. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9c28053ec9a04a29ae033cccf1272240"> <div class="comment-author"> <a href="http://stackoverflow.com/users/28298/chester89">Chermennov Gleb</a> <a href="#9c28053ec9a04a29ae033cccf1272240">#</a></div> <div class="comment-content"> <p> Can you elaborate a bit on not breaking existing functionality in newer versions (as long as they have one major version)? What tools are you using to achieve that? I read your post on Semantic Versioning from couple months ago. I manage OSS project and it has quite a big public API - each release I try hard to think of anything I or other contributors might have broken. Are you saying that you relay strictly on programmer deep knowledge of the project when deciding on a new version number? Also, do you build AutoFixture or any other .NET project of yours for Linux/Mono? </p> </div> <div class="comment-date">2014-02-03 19:00 UTC</div> </div> <div class="comment" id="d38373917d054831adf8a811fe47e525"> <div class="comment-author"> <a href="">Mark Seemann</a> <a href="#d38373917d054831adf8a811fe47e525">#</a></div> <div class="comment-content"> <p> For AutoFixture, as well as other OSS projects I maintain, we rely almost exclusively on unit tests, keeping in mind that <a href="/2013/04/02/why-trust-tests">trustworthy tests are append-only</a>. AutoFixture has some 4000+ unit tests, so if none of those break, I feel confident that a release doesn't contain breaking changes. </p> <p> For my other OSS projects, the story is the same, although the numbers differ. <ul> <li><a href="https://github.com/ploeh/Albedo">Albedo</a> has 354 tests.</li> <li><a href="https://github.com/ploeh/ZeroToNine">ZeroToNine</a> has 200 tests.</li> <li><a href="https://github.com/ploeh/Hyprlinkr">Hyprlinkr</a> has 88 tests.</li> </ul> These are much smaller projects than AutoFixture, but since they were all built with TDD, they have excellent code coverage. </p> <p> Currently, I don't build any of these .NET projects for Mono, as I've never had the need. </p> </div> <div class="comment-date">2014-02-04 8:48 UTC</div> </div> <div class="comment" id="3f4a947005ee481487c20b5face63df9"> <div class="comment-author"> <a href="http://stackoverflow.com/users/28298/chester89">Chermennov Gleb</a> <a href="#3f4a947005ee481487c20b5face63df9">#</a></div> <div class="comment-content"> <p> So you verify behaviour didn't change with a help of automated tests and a good test coverage. What I had in mind is some technique to verify not only the desired behaviour is in place, but also a public API (method signatures, class constructors, set of public types). I should probably clarify that in one of my projects public API is not fully covered by unit-tests. Most critical parts of it are covered, but not all of it. Let's say that upcoming release contains bugfixes as well as new features. I also decided that couple of public API methods are obsolete and deleted them. That makes a breaking change. Let's say I had a lot on my mind and I forgot about the fact that I made those changes. Some time goes by, I'd like to push a new version with all these changes to NuGet, but I'd like to double-check that the public API is still in place compared to the last release. Are there some tools that help with that, may be the ones you use? Or do you rely fully on the tests and your process in that regard? My approach to releases and versioning is a LOT more error prone than yours, clearly, that's the part of my projects that I'd like to improve. </p> </div> <div class="comment-date">2014-02-05 23:20 UTC</div> </div> <div class="comment" id="5b3707b39f17423da6766cbfe0d3f337"> <div class="comment-author"> <a href="">Mark Seemann</a> <a href="#5b3707b39f17423da6766cbfe0d3f337">#</a></div> <div class="comment-content"> <p> The only technique I rely on apart from automated tests is code reviews. When I write code myself, I always keep in mind if I'm breaking anything. When I receive Pull Requests (PR), I always review them with an eye towards breaking changes. Basically, if a PR changes an existing test, I review it very closely. Obviously, any change that involves renaming of types or members, or that changes public method signatures, are out of the question. </p> <p> While I'm not aware of any other technique than discipline that will protect against breaking changes, you could always try to check out the tests you have against a previous version, and see if they all pass against the new version. If they don't, you have a breaking change. </p> <p> You can also make a diff of everything that's happened since your last release, and then meticulously look through all types and members to see if anything was renamed, or method signatures changed. This will also tell you if you have breaking changes. </p> <p> However, in the end, if you find no breaking changes using these approaches, it's still not a guarantee that you have no breaking changes, because you may have changed the <em>behaviour</em> of some methods. Since you don't have full test coverage, it's hard to tell. </p> <p> What you <em>could</em> try to do, is to have <a href="http://research.microsoft.com/en-us/projects/Pex">Pex</a> create a full test suite for your latest released version. This test suite will give you a full snapshot of the behaviour of that release. You could then try to run that test suite on your release candidate to see if anything changed. I haven't tried this myself, and I presume that there's still a fair bit of work involved, but perhaps it's worth a try. </p> </div> <div class="comment-date">2014-02-06 14:51 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. How to use FSharp.Core 4.3.0 when all you have is 4.3.1 https://blog.ploeh.dk/2014/01/30/how-to-use-fsharpcore-430-when-all-you-have-is-431 2014-01-30T18:39:00+00:00 Mark Seemann <div id="post"> <p> <em>If you only have F# 3.1 installed on a machine, but need to use a compiled application that requires F# 3.0, here's what you can do.</em> </p> <p> This post uses a particular application, <a href="https://github.com/ploeh/ZeroToNine">Zero29</a>, as an example in order to explain a problem and one possible solution. However, the post isn't about Zero29, but rather about a particular F# DLL hell. </p> <p> Currently, I'm repaving one of my machines, which is always a good idea to do regularly, because it's a great remedy against <em>works on my machine syndrome</em>. This machine doesn't yet have a lot of software, but it does have Visual Studio 2013 and F# 3.1. </p> <p> Working with a code base, I wanted to use Zero29 to incement the version number of the code, so first I executed: </p> <p> <pre>$ packages/Zero29.0.4.1/tools/Zero29 -l</pre> </p> <p> which promptly produced this error message: </p> <p> <pre>Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'FSharp.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. at Ploeh.ZeroToNine.Program.main(String[] argv)</pre> </p> <p> On one level, this makes sense, because <a href="https://www.nuget.org/packages/Zero29/0.4.1">Zero29 0.4.1</a> was compiled against F# 3.0 (which <a href="http://stackoverflow.com/a/20362049/126014">corresponds to FSharp.Core 4.3.0.0</a>). </p> <p> On another level, this is surprising, since I do have F# 3.1 (FSharp.Core 4.3.1.0) on my machine. Until the error message appeared, I had lived with the naïve assumption that when you install F# 3.1, it would automatically add redirects from FSharp.Core 4.3.0.0 to 4.3.1.0, or perhaps make sure that FSharp.Core 4.3.0.0 was <em>also</em> available. Apparently, I've become too used to <a href="http://semver.org">Semantic Versioning</a>, which is <a href="http://stackoverflow.com/a/20362049/126014">definitely not the versioning scheme used for F#</a>. </p> <p> Here's one way to address the issue. </p> <p> Although Zero29 is my own (and <a href="http://www.ohloh.net/p/ZeroToNine/contributors/summary">contributors'</a>) creation, I didn't want to recompile it just to deal with this issue; it should also be usable for people with F# 3.0 on their machines. </p> <p> Even though it's a compiled program, you can still add an application configuration file to it, so I created an XML file called <em>Zero29.exe.config</em>, placed it alongside <em>Zero29.exe</em>, and added this content: </p> <p> <pre><span style="color: blue;">&lt;?</span><span style="color: #a31515;">xml</span><span style="color: blue;"> </span><span style="color: red;">version</span><span style="color: blue;">=</span>"<span style="color: blue;">1.0</span>"<span style="color: blue;"> </span><span style="color: red;">encoding</span><span style="color: blue;">=</span>"<span style="color: blue;">utf-8</span>"<span style="color: blue;">?&gt;</span> <span style="color: blue;">&lt;</span><span style="color: #a31515;">configuration</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">runtime</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">assemblyBinding</span><span style="color: blue;"> </span><span style="color: red;">xmlns</span><span style="color: blue;">=</span>"<span style="color: blue;">urn:schemas-microsoft-com:asm.v1</span>"<span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">dependentAssembly</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">assemblyIdentity</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">FSharp.Core</span>" <span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">publicKeyToken</span><span style="color: blue;">=</span>"<span style="color: blue;">b03f5f7f11d50a3a</span>" <span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">culture</span><span style="color: blue;">=</span>"<span style="color: blue;">neutral</span>"<span style="color: blue;">/&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">bindingRedirect</span><span style="color: blue;"> </span><span style="color: red;">oldVersion</span><span style="color: blue;">=</span>"<span style="color: blue;">4.3.0.0</span>" <span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">newVersion</span><span style="color: blue;">=</span>"<span style="color: blue;">4.3.1.0</span>"<span style="color: blue;">/&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">dependentAssembly</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">assemblyBinding</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">runtime</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">configuration</span><span style="color: blue;">&gt;</span> </pre> </p> <p> This solved the problem, although I now have the derived problem that this new file isn't part of the Zero29 NuGet package, and I don't know if it's going to ruin my colleagues' ability to use Zero29 if I check it into source control... </p> <p> Another option <em>may</em> be to add the redirect to machine.config, instead of an application-specific redirect, but I have no desire to manipulate my machine.config files if I can avoid it, so I didn't try that. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. NuGet Package Restore considered harmful https://blog.ploeh.dk/2014/01/29/nuget-package-restore-considered-harmful 2014-01-29T20:06:00+00:00 Mark Seemann <div id="post"> <p> <em>The NuGet Package Restore feature is a really bad idea; this post explains why.</em> </p> <p> One of the first things I do with a new installation of Visual Studio is to disable the NuGet Package Restore feature. There are many reasons for that, but it all boils down to this: </p> <p> <blockquote>NuGet Package Restore introduces more problems than it solves.</blockquote> </p> <p> Before I tell you about all those problems, I'll share the solution with you: <em>check your NuGet packages into source control.</em> Yes, it's that simple. </p> <h3 id="ce35015955414f0b90cda98e1c09a47c"> Storage implications <a href="#ce35015955414f0b90cda98e1c09a47c" title="permalink">#</a> </h3> <p> If you're like most other people, you don't like that solution, because it <em>feels</em> inefficient. And so what? Let's look at some numbers. <ul> <li>The <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> repository is 28.6 MB, and that's a pretty big code base (<a href="https://www.ohloh.net/p/autofixture/analyses/latest/languages_summary">181,479 lines of code</a>).</li> <li>The <a href="https://github.com/ploeh/Hyprlinkr">Hyprlinkr</a> repository is 32.2 MB.</li> <li>The <a href="https://github.com/ploeh/Albedo">Albedo</a> repository is 8.85 MB.</li> <li>The <a href="https://github.com/ploeh/ZeroToNine">ZeroToNine</a> repository is 4.91 MB.</li> <li>The <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">sample code repository for my new Pluralsight course</a> is 69.9 MB.</li> <li>The repository for <a href="http://grean.com">Grean's</a> largest production application is 32.5 MB.</li> <li>Last year I helped one of my clients build a big, scalable REST API. We had several repositories, of which the largest one takes up 95.3 MB on my disk.</li> </ul> All of these repositories contain NuGet packages, but keep in mind that even if we'd used Package Restore instead, these repositories wouldn't have been <em>empty</em> - they would still have taken up <em>some</em> space on disk. </p> <p> On my laptops I'm using Lenovo-supported SSDs, so they're fairly expensive drives. Looking up current prices, it seems that a rough estimates of prices puts those disks at approximately 1 USD per GB. </p> <p> On average, each of my repositories containing NuGet packages cost me <strong>four cents</strong> of disk drive space. </p> <p> Perhaps I could have saved <em>some</em> of this money with Package Restore... </p> <h3 id="b8c510ad098b40d089442b15d50467de"> Clone time <a href="#b8c510ad098b40d089442b15d50467de" title="permalink">#</a> </h3> <p> Another problem that the Package Restore feature seems to address, is the long time it takes to clone a repository - <a href="https://twitter.com/tims/status/383118946909818880">if you're on a shaky internet connection in a train</a>. While it can be annoying to wait for a repository to clone, how often do you do that, compared to normal synchronization operations such as <em>pull</em>, <em>push</em> or <em>fetch</em>? </p> <p> What should you be optimizing for? Cloning, which you do once in a while? Or fetch, pull, and push, which you do several times a day? </p> <p> In most cases, the amount of time it takes to clone a repository is irrelevant. </p> <p> To summarize so far: the problems that Package Restore solves are a couple of cents of disk cost, as well as making a rarely performed operation faster. From where I stand, it doesn't take a lot of problems before they outweigh the benefits - and there are plenty of problems with this feature. </p> <h3 id="eb3965b2b3764a14b38867bb75552994"> Fragility <a href="#eb3965b2b3764a14b38867bb75552994" title="permalink">#</a> </h3> <p> The more moving parts you add to a system, the greater the risk of failure. If you use a <a href="http://en.wikipedia.org/wiki/Distributed_version_control_system">Distributed Version Control System</a> (DVCS) and keep all NuGet packages in the repository, you can work when you're off-line. With Package Restore, you've added a dependency on <em>at least</em> one package source. <ul> <li>What happens if you have no network connection?</li> <li>What happens if your package source (e.g. <a href="http://www.nuget.org">NuGet.org</a>) is down?</li> <li>What happens if you use multiple package sources (e.g. both NuGet.org and <a href="http://www.myget.org">MyGet.org</a>)?</li> </ul> You may think that this is unlikely to happen, but apparently, NuGet.org was down today: </p> <p> <img src="/content/binary/nuget-down-tweets.png"> </p> <p> This is a well-known trait of any distributed system: The system is only as strong as its weakest link. The more services you add, the higher is the risk that something breaks. </p> <h3 id="9583d6e784bc47548fafb666117b1d04"> Custom package sources <a href="#9583d6e784bc47548fafb666117b1d04" title="permalink">#</a> </h3> <p> NuGet itself is a nice system, and I often encourage organizations to adopt it for internal use. You may have reusable components that you want to share <em>within</em> your organization, but not with the whole world. In <a href="http://grean.com">Grean</a>, we have such components, and we use <a href="http://www.myget.org">MyGet</a> to host the packages. This is great, but if you use Package Restore, now you depend on <em>multiple</em> services (NuGet.org and MyGet.org) to be available at the same time. </p> <p> While Myget is a nice and well-behaved NuGet host, I've also worked with internal NuGet package sources, set up as an internal service in an organization. Some of these are not as well-behaved. In one case, 'old' packages were deleted from the package source, which had the consequence that when I later wanted to use an older version of the source code, I couldn't complete a Package Restore because the package with the desired version number was no longer available. There was simply no way to build that version of the code base! </p> <h3 id="762d25e0e47744779a70bfafdd72a0fa"> Portability <a href="#762d25e0e47744779a70bfafdd72a0fa" title="permalink">#</a> </h3> <p> One of the many nice things about a DVCS is that you can xcopy your repository and move it to another machine. You can also copy it and give it to someone else. You could, for example, zip it and hand it over to an external consultant. If you use Package Restore and internal package sources, the consultant will not be able to compile the code you gave him or her. </p> <h3 id="b13f530f00984c12b596c47981bcd738"> Setup <a href="#b13f530f00984c12b596c47981bcd738" title="permalink">#</a> </h3> <p> Perhaps you don't use external consultants, but maybe you set up a new developer machine once in a while. Perhaps you occasionally get a new colleague, who needs help with setting up the development environment. Particularly if you use custom package feeds, making it all work is yet another custom configuration step you need to remember. </p> <h3 id="9b52bedb29e04b3ca8bc62b73c16d6ed"> Bandwidth cost <a href="#9b52bedb29e04b3ca8bc62b73c16d6ed" title="permalink">#</a> </h3> <p> As far as I've been able to tell, the purpose of Package Restore is efficiency. However, every time you compile with Package Restore enabled, you're using the network. </p> <p> Consider a Build Server. Every time it makes a build, it should start with a clean slate. It can get the latest deltas from the shared source control repository, but it should start with a clean working folder. This means that <em>every time it builds</em>, it'll need to download all the NuGet packages via Package Restore. This not only wastes bandwidth, but takes time. In contrast, if you keep NuGet packages in the repository itself, the Build Server has everything it needs as soon as it has the latest version of the repository. </p> <p> The same goes for your own development machine. Package Restore <em>will</em> make your compile process slower. </p> <h3 id="0bc52012986443aebff7bd155740b3fb"> Glitches <a href="#0bc52012986443aebff7bd155740b3fb" title="permalink">#</a> </h3> <p> Finally, Package Restore simply doesn't <em>work</em> very well. Personally, I've wasted many hours troubleshooting problems that turned out to be related to Package Restore. Allow me to share one of these stories. </p> <p> Recently, I encountered this sight when I opened a solution in Visual Studio: </p> <p> <img src="/content/binary/nuget-package-restore-problem.png"> </p> <p> My problem was that at first, I didn't understand what was wrong. Even though I store NuGet packages in my repositories, all of a sudden I got this error message. It turned out that this happened at the time when NuGet switched to enabling Package Restore by default, and I hadn't gotten around to disable it again. </p> <p> The strange thing was the everything compiled and worked just great, so why was I getting that error message? </p> <p> After much digging around, it turned out that the <a href="http://www.nuget.org/packages/ImpromptuInterface.FSharp">ImpromptuInterface.FSharp</a> package was missing a .nuspec file. You may notice that ImpromptuInterface.FSharp is also missing in the package list above. All binaries, as well as the .nupkg file, was in the repository, but the ImpromptuInterface.FSharp.1.2.13.nuspec was missing. I hadn't noticed for weeks, because I didn't need it, but NuGet complained. </p> <p> After I added the appropriate .nuspec file, the error message went away. </p> <p> The resolution to this problem turned out to be easy, and benign, but I wasted an hour or two troubleshooting. It didn't make me feel productive at all. </p> <p> This story is just one among many run-ins I've had with NuGet Package Restore, before I decided to ditch it. </p> <h3 id="66024aa831204d6fbfb55d6c08d0c012"> Just say no <a href="#66024aa831204d6fbfb55d6c08d0c012" title="permalink">#</a> </h3> <p> The Package Restore feature solves these problems: <ul> <li>It saves <a href="http://en.wikipedia.org/wiki/Nickel_(United_States_coin)">a nickel</a> per repository in storage costs.</li> <li>It saves time when you clone a new repository, which you shouldn't be doing that often.</li> </ul> On the other hand, it <ul> <li>adds complexity</li> <li>makes it harder to use custom package sources</li> <li>couples your ability to compile to having a network connection</li> <li>makes it more difficult to copy a code base</li> <li>makes it more difficult to set up your development environment</li> <li>uses <em>more</em> bandwidth</li> <li>leads to slower build times</li> <li>just overall wastes your time</li> </ul> </p> <p> For me, the verdict is clear. The benefits of Package Restore don't warrant the disadvantages. Personally, I always disable the feature and instead check in all packages in my repositories. This <em>never</em> gives me any problems. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f3eaabfbfe0143f9be29716fcae9fc90"> <div class="comment-author"><a href="mailto:bstephens@ztyo.com">Blake Stephens</a> <a href="#f3eaabfbfe0143f9be29716fcae9fc90">#</a></div> <div class="comment-content"> "Hah, not sure I'm doing this commenting thing right, but here it goes anyways." <br> "So going on two years from when you wrote this post, is this still how you feel about nuget packages being included in the repository? I have to say, all the points do seem to still apply, and I found myself agreeing with many of them, but I havne't been able to find many oppinions that mirror it. Most advice on the subject seems to be firmly in the other camp (not including nuget packages in the repo), though, as you note, the tradeoff doesn't seem to be a favorable one. <br> </div> <div class="comment-date">2015-12-03 15:36 UTC</div> </div> <div class="comment" id="ffbd0caef28243bcbcd806aa6d2d4c59"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ffbd0caef28243bcbcd806aa6d2d4c59">#</a></div> <div class="comment-content"> <p> Blake, thank you for writing. Yes, this is still how I feel; nothing has changed. </p> </div> <div class="comment-date">2015-12-03 21:17 UTC</div> </div> <div class="comment" id="80a01cbf338349d9a5419639a8ed7dc0"> <div class="comment-author">Peter <a href="#80a01cbf338349d9a5419639a8ed7dc0">#</a></div> <div class="comment-content"> <p> Mark, completely agree with all your points, however in the future, not using package restore will no longer be an option. See <a href="https://oren.codes/2016/02/08/project-json-all-the-things/">Project.json all the things</a>, most notably "Packages are now stored in a per-user cache instead of alongside the solution". </p> </div> <div class="comment-date">2015-02-10 00:38 UTC</div> </div> <div class="comment" id="ac6649d2769e454781f1d0af85d66792"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#ac6649d2769e454781f1d0af85d66792">#</a></div> <div class="comment-content"> <p> Like Peter, I am also interested in what you do now. </p> <p> When you wrote that post, NuGet package dependencies were specificed (in part) by <a href="https://docs.microsoft.com/en-us/nuget/reference/packages-config"><code>packages.config</code></a> files. Then came <a href="https://docs.microsoft.com/en-us/nuget/archive/project-json"><code>project.json</code></a>. The Microsoft-recommened approach these days is <a href="https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files"><code>PackageReference</code></a>. The first approach caches the "restored" NuGet packages in the respository, but the latter two (as Peter said) only cache in a global location (namely <code>%userprofile%\.nuget\packages</code>). I expect that you are using the <code>PackageReference</code> approach now, is that correct? </p> <p> I see where Peter is coming from. It does seem at first like NuGet restore is now "necessary". Of course it is still possible to commit the NuGet packages in the respository. Then I could add this directory as a local NuGet package source and restore the NuGet packages, which will copy them from the respository to the global cache (so that the build can copy the corresponding DLLs from the global cache to the output directory). </p> <p> However, maybe it is possible to specify the location of the cached NuGet packages when building the solution. I just thought of this possibility while writing this, so I haven't been able to fully investiagate it. This seems reasonable to me, and my initial searches also seem to point toward this being possible. </p> <p> So how do you handle NuGet dependencies now? Does your build obtain them from the global cache or have you found a way to point the build to a directory of your choice? </p> </div> <div class="comment-date">2019-08-04 03:38 UTC</div> </div> <div class="comment" id="2de063c09884401f9ef22fd1662c74c0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2de063c09884401f9ef22fd1662c74c0">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Currently, I use the standard tooling. I've lost that battle. </p> <p> My opinion hasn't changed, but while it's possible to avoid package restore on .NET, I'm not aware of how to do that on .NET Core. I admit, however, that I haven't investigated this much. </p> <p> I haven't done that much .NET Core development, and when I do, I typically do it to help other people. The things I help with typically relate to architecture, modelling, or testing. It can be hard for people to learn new things, so I aim at keeping the level of new things people have to absorb as low as possible. </p> <p> Since people rarely come to me to learn package management, I don't want to rock that boat while I attempt to help people with something completely different. Therefore, I let them use their preferred approach, which is almost always the standard way. </p> </div> <div class="comment-date">2019-08-04 9:26 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A Functional architecture with F# https://blog.ploeh.dk/2014/01/22/a-functional-architecture-with-f 2014-01-22T22:48:00+00:00 Mark Seemann <div id="post"> <p> <em>My new Pluralsight course, A Functional Architecture with F#, is now available.</em> </p> <p> Whenever I've talked to object-oriented developers about F#, a common reaction has been that it looks enticing, but that they don't see how they'd be able to build a 'normal' application with it. F# has gained a reputation for being a 'niche' language, good for scientific computation and financial calculations, but not useful for mainstream applications. </p> <p> Not only is F# a Turing-complete, general purpose programming language, but it has <em>many</em> advantages to offer compared to, say, C#. That said, though, building a 'normal' application with F# will only make sense if you know how to work <em>with</em> the language, and define an architecture that takes advantage of all it has to offer. Therefore, I thought that it would be valuable to show one possible way to do this, through a comprehensive example. </p> <p> This was the motivation behind my new <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">A Functional Architecture with F#</a>, which is now available! In it, you'll see extensive code demos of a web application written entirely in F# (and a bit of GUI in JavaScript, but the course only shows the F# code). </p> <p> If you don't already have a Pluralsight account, you can get a <a href="https://pluralsight.com/training/Subscribe/Step1?isTrial=True">free trial</a> of up to 200 minutes. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="d43df5af8e6345f38969181cf6843d80"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#d43df5af8e6345f38969181cf6843d80">#</a></div> <div class="comment-content"> <blockquote> ...you'll see extensive code demos of a web application written entirely in F#... </blockquote> <p> What about a desktop applications written entirely in F#? When you create a desktop applciation in F#, what do you use to create the GUI? </p> <p> I am currently writting my first application in F# and need to decide what we will use to create the GUI. There seems to be many ways this could be done. </p> <ol> <li>For simplicity, I started using WPF with the code-behind written in C#. I am satisifed with this initial (temporary) GUI for now, but the C#/F# interop is ugly to read and painful to write.</li> <li>I could sitck with WPF but write the code-behind in F#. I found two ways to do this:</li> <ol> <li><a href="https://github.com/fsprojects/FsXaml">FsXaml</a> and</li> <li><a href="https://github.com/elmish/Elmish.WPF">Elmish.WPF</a>.</li> </ol> <li>Another <a href="https://www.davideaversa.it/2018/04/quick-look-at-f-in-unity/">possibility</a> is the video game engine <a href="https://unity.com/">Unity</a>.</li> <li>I also found a XAML-based approach called <a href="http://avaloniaui.net/">Avalonia</a>. However, their website says they are "currently in a beta phase, which means that the framework is generally usable for writing applications, but there may be some bugs and breaking changes as we continue development."</li> </ol> <p> There are probably many more that I missed as well. </p> <p> Among these, Elmish.WPF stands out to me. Their page commnicates a strong ethos, which I find both enticing and convincing. The core idea seems to be the <a href="https://guide.elm-lang.org/architecture/">Elm Architecture</a>, which I have also seen expressed as the MVU architecture, where MVU stands for Model, View, Update. </p> <p> Have you come across that architecture before? I am very interested to hear your opinion about it. </p> <p> P.S. Also worth mentioning is <a href="https://github.com/fsprojects/Fabulous">Fabulous</a>, which uses <a href="https://docs.microsoft.com/en-us/xamarin/xamarin-forms/">Xamarin.Forms</a>, so this seems like a good choice for mobile app development. </p> </div> <div class="comment-date">2019-08-04 01:15 UTC</div> </div> <div class="comment" id="66d2b843a43341ad821f17e18d426b52"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#66d2b843a43341ad821f17e18d426b52">#</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I haven't done any desktop application development in ten years, so I don't think I'm qualified to make recommendations. </p> </div> <div class="comment-date">2019-08-04 9:34 UTC</div> </div> <div class="comment" id="50aa0a989bfc4ed59a2dbb5566e1a952"> <div class="comment-author"><a href="https://about.me/tysonwilliams">Tyson Williams</a> <a href="#50aa0a989bfc4ed59a2dbb5566e1a952">#</a></div> <div class="comment-content"> <p> Do your web applications include GUIs? If so, what UI framework(s) do you like to use there (such as Angular, React, Elm, etc.)? </p> <p> P.S. I have been investigating Elmish.WPF and love what I have found. The Elm / MVU / Model, View, Update architecture seems to be a specific (or the ultimate?) applicaiton of the functional programming prinicple of pushing impure behavior to the boundry of the applicaiton. </p> </div> <div class="comment-date">2019-08-22 18:40 UTC</div> </div> <div class="comment" id="7830ba29e87a46eebb7aba4f456e63ea"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7830ba29e87a46eebb7aba4f456e63ea">#</a></div> <div class="comment-content"> <p> Tyson, seriously, I think that the last time I wrote any GUI code was with Angular some time back in 2014, after which I walked away in disgust. Since then, I've mostly written REST APIs, with the occasional daemon and console application thrown in here and there. </p> <p> Many of the REST APIs I've helped develop are consumed by apps with GUIs, but someone else developed those, and I wasn't involved. </p> </div> <div class="comment-date">2019-08-22 20:35 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. REST efficiency https://blog.ploeh.dk/2014/01/20/rest-efficiency 2014-01-20T07:26:00+00:00 Mark Seemann <div id="post"> <p> <em>A fully RESTful API often looks inefficient from a client perspective, until you learn to change that perspective.</em> </p> <p> One of my readers, <a href="https://twitter.com/xima">Filipe Ximenes</a>, asks the following question of me: <blockquote> <p> "I read you <a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls">post about avoiding hackable urls</a> and found it very interesting. I'm currently studying about REST and I'm really interested on building true RESTful API's. One thing that is bothering me is how to access resources that are not in the API root. Eg: consider the following API flow: </p> <p> "<code>root &gt; users &gt; user details &gt; user messages</code> </p> <p> "Now consider that one client wants to retrieve all the messages from a user. Does it need to "walk" the whole API (from it's root to "user messages")? This does not seem very efficient to me. Am I missing something? What would be a better solution for this?" </p> </blockquote> </p> <p> This is a common question which isn't particularly tied to avoiding hackable URLs, but simply to the hypermedia nature of a <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a> RESTful API. </p> <p> The short answer is that it's probably not particularly inefficient. There are several reasons for that. </p> <h3 id="d273d662563740aaadd3deff03541e16"> HTTP caching <a href="#d273d662563740aaadd3deff03541e16" title="permalink">#</a> </h3> <p> One of the great advantages of RESTful design is that instead of abstracting HTTP away, it very explicitly leverages the protocol. HTTP has bulit-in caching, so even if an API forces a client to walk the API as in the question above, it could conceivably result in only a single HTTP request: </p> <p> <img src="/content/binary/http-caching-sequence-diagram.png" alt="HTTP caching sequence diagram."> </p> <p> This cache could be anywhere between the client and the service. It could be a proxy server, a reverse proxy, or it could even be a local cache on the client machine; think of a Browser's local cache. It could be a combination of all of those caches. Conceivably, if a local cache is involved, a client could walk the API as described above with only a single (or even no) network request involved, because most of the potential requests would be cache hits. </p> <p> This is one of the many beautiful aspects of REST. By leveraging the HTTP protocol, you can use the internet as your caching infrastructure. Even if you want a greater degree of control, you can use off-the-shelf software for your caching purposes. </p> <h3 id="24e7166ea37547cf80a52a8eeb36d234"> Cool URLs <a href="#24e7166ea37547cf80a52a8eeb36d234" title="permalink">#</a> </h3> <p> As the <a href="http://amzn.to/YFnkRg">RESTful Web Services Cookbook</a> describes, URLs should be <a href="http://www.w3.org/Provider/Style/URI.html">cool</a>. This means that once you've given a URL to a client, you should honour requests for that URL in the future. This means that clients can 'bookmark' URLs if they like. That includes the final URL in the flow above. </p> <h3 id="7ea138b1511d4c15a204cae2573e8d84"> Short-cut links <a href="#7ea138b1511d4c15a204cae2573e8d84" title="permalink">#</a> </h3> <p> Finally, an API can provide short-cut links to a client. Imagine, for example, that when you ask for a list of users, you get this: <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">users</span><span style="color: blue;"> </span><span style="color: red;">xmlns:atom</span><span style="color: blue;">=</span>"<span style="color: blue;">http://www.w3.org/2005/Atom</span>"<span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">user</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">links</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">atom:link</span><span style="color: blue;"> </span><span style="color: red;">rel</span><span style="color: blue;">=</span>"<span style="color: blue;">user-details</span>"<span style="color: blue;"> </span><span style="color: red;">href</span><span style="color: blue;">=</span>"<span style="color: blue;">/users/1234</span>"<span style="color: blue;"> /&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">atom:link</span><span style="color: blue;"> </span><span style="color: red;">rel</span><span style="color: blue;">=</span>"<span style="color: blue;">user-messages</span>"<span style="color: blue;"> </span><span style="color: red;">href</span><span style="color: blue;">=</span>"<span style="color: blue;">/users/1234/messages</span>"<span style="color: blue;"> /&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">links</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span>Foo<span style="color: blue;">&lt;/</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">user</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">user</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">links</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">atom:link</span><span style="color: blue;"> </span><span style="color: red;">rel</span><span style="color: blue;">=</span>"<span style="color: blue;">user-details</span>"<span style="color: blue;"> </span><span style="color: red;">href</span><span style="color: blue;">=</span>"<span style="color: blue;">/users/5678</span>"<span style="color: blue;"> /&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">atom:link</span><span style="color: blue;"> </span><span style="color: red;">rel</span><span style="color: blue;">=</span>"<span style="color: blue;">user-messages</span>"<span style="color: blue;"> </span><span style="color: red;">href</span><span style="color: blue;">=</span>"<span style="color: blue;">/users/5678/messages</span>"<span style="color: blue;"> /&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">links</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span>Bar<span style="color: blue;">&lt;/</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">user</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">user</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">links</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">atom:link</span><span style="color: blue;"> </span><span style="color: red;">rel</span><span style="color: blue;">=</span>"<span style="color: blue;">user-details</span>"<span style="color: blue;"> </span><span style="color: red;">href</span><span style="color: blue;">=</span>"<span style="color: blue;">/users/9876</span>"<span style="color: blue;"> /&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">atom:link</span><span style="color: blue;"> </span><span style="color: red;">rel</span><span style="color: blue;">=</span>"<span style="color: blue;">user-messages</span>"<span style="color: blue;"> </span><span style="color: red;">href</span><span style="color: blue;">=</span>"<span style="color: blue;">/users/9876/messages</span>"<span style="color: blue;"> /&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">links</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span>Baz<span style="color: blue;">&lt;/</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">user</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">users</span><span style="color: blue;">&gt;</span></pre> </p> <p> As you can see in this example, a list of users can provide a short-cut to a user's messages, enabling a client to follow a more direct path: </p> <p> <code>root &gt; users &gt; user messages</code> </p> <p> The client would have to prioritize links of the relationship type <em>user-messages</em> over links of the <em>user-details</em> type. </p> <h3 id="96cf2db921774d50933416a21d045efc"> Summary <a href="#96cf2db921774d50933416a21d045efc" title="permalink">#</a> </h3> <p> Efficiency is a common concern about <a href="http://en.wikipedia.org/wiki/HATEOAS">HATEOAS</a> systems, particularly because a client should always start at published URL. Often, the only published URL is the root URL, which forces the client to walk the rest of the API. This seems inefficient, but doesn't have to be because of all the other built-in mechanisms that work to effectively counter what at first looks like an inefficiency. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Hyprlinkr 1.0.0 https://blog.ploeh.dk/2014/01/17/hyprlinkr-100 2014-01-17T19:10:00+00:00 Mark Seemann <div id="post"> <p> <em>Hyprlinkr 1.0.0 is released.</em> </p> <p> According to the definition of <a href="http://semver.org">Semantic Versioning</a>, <a href="https://github.com/ploeh/Hyprlinkr">Hyprlinkr</a> has been in pre-release in more than a year. With the release of <a href="http://www.asp.net/web-api">ASP.NET Web API</a> 2, I thought it was a good occasion to look at a proper release version. </p> <p> I've tested Hyprlinkr against Web API 2, and apart from some required assembly redirects, it passes all tests against Web API 2 as well as Web API 1. Being able to support both Web API 1 and 2 is important, I think, because not everyone will be able to migrate to Web API 2 right away. </p> <p> Since Hyprlinkr is finally out of pre-release mode, it also means that no breaking changes will be introduced before Hyprlinkr 2, which isn't even on the drawing board yet. Since this constitutes a contract, I also trimmed down the API a bit before releasing Hyprlinkr 1.0.0, but all the essential methods are still available. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. ZeroToNine https://blog.ploeh.dk/2013/12/11/zerotonine 2013-12-11T12:37:00+00:00 Mark Seemann <div id="post"> <p> <em>Introducing ZeroToNine, a tool for maintaining .NET Assembly versions across multiple files.</em> </p> <p> When working with <a href="http://semver.org">Semantic Versioning</a> in my .NET projects, <a href="/2013/12/10/semantic-versioning-with-continuous-deployment">I prefer to explicitly update the version information in all relevant AssemblyInfo files</a>. However, doing that by hand is quite tedious when you have many AssemblyInfo files, so instead, I rely on an automated tool. </p> <p> For years, I used a PowerShell script, but recently, I decided to start over and write a 'real' tool, deployable via <a href="http://www.nuget.org">NuGet</a>. It's called <a href="https://github.com/ploeh/ZeroToNine">ZeroToNine</a>, is free, and open source. Using it looks like this: <pre>Zero29 -i minor</pre> This increments the minor version in all AssemblyInfo files in all subdirectories beneath your present working directory. </p> <p> This is great, because it enables me to do a complete pull of a pull request, build it and run all tests, assign a new version, and push it, without ever leaving the command-line. Since I already do all my <a href="http://git-scm.com">Git</a> work in Git Bash, modifying the AssemblyVersion files was the last step I needed to make available from the command line. The main logic is implemented in a library, so if you don't like command-line tools, but would like to build another tool based on ZeroToNine, you can do that too. </p> <p> It's available <a href="https://www.nuget.org/packages/Zero29">via NuGet</a>, and is written in F#. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="adf28150a22d4763bfe6bc7dd9de9c07"> <div class="comment-author">Jeff Soper <a href="#adf28150a22d4763bfe6bc7dd9de9c07">#</a></div> <div class="comment-content"> <p>Can you clarify where one would install this when adding the NuGet package to a solution of several projects?</p> <p>Your documentation says that it will update AssemblyInfo files in all subdirectories beneath the present working directory, but I thought that NuGet packages are applied at a project level, not at a solution level. So, wouldn't this mean that I would be running your tool from one of the many project directories, in which only that project's AssemblyInfo file would be affected?</p> <p>I'm sure I'm not grasping something simple, but I'm anxious to incorporate this into my workflow!</p> </div> <div class="comment-date">2014-01-23 19:26 UTC</div> </div> <div class="comment" id="1531cb31261446ceb2578339d5e06105"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1531cb31261446ceb2578339d5e06105">#</a></div> <div class="comment-content"> <p> NuGet packages <a href="http://stackoverflow.com/q/9494144/126014">can contain executable tools</a> as well as, or instead of, libraries. These executables can be found in the package's <em>tools</em> folder. This is what the <a href="http://www.nuget.org/packages/Zero29">Zero29</a> package does. It's not associated with any particular Visual Studio project. </p> <p> As an example, using Zero29 from the root of the <a href="https://github.com/ploeh/Albedo">Albedo</a> folder, you can do this: </p> <p> <code>$ Src/packages/Zero29.0.4.0/tools/Zero29.exe -l</code> </p> <p> There are other NuGet packages that work in the same way; e.g. <a href="http://www.nuget.org/packages/NuGet.CommandLine">NuGet.CommandLine</a> and <a href="http://www.nuget.org/packages/xunit.runners">xunit.runners</a>. </p> <p> The <a href="http://www.nuget.org/packages/ZeroToNine">ZeroToNine NuGet package</a>, on the other hand, is a 'normal' library, so installs as a reference to a particular Visual Studio project. </p> </div> <div class="comment-date">2014-01-24 19:42 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Semantic Versioning with Continuous Deployment https://blog.ploeh.dk/2013/12/10/semantic-versioning-with-continuous-deployment 2013-12-10T15:19:00+00:00 Mark Seemann <div id="post"> <p> <em>When you use Semantic Versioning with Continuous Deployment, version numbers must be checked into source control systems by programmers.</em> </p> <p> If you aren't already using <a href="http://semver.org">Semantic Versioning</a>, you should. It makes it <em>much</em> easier to figure out how to version your releases. Even if you're 'just' building software for your internal organization, or a single customer, <a href="/2012/12/18/ZookeepersmustbecomeRangers">you should still care about versioning of the software you release</a>. Instead of an ad-hoc versioning scheme, Semantic Versioning offers a set of easy-to-understand rules about when to increment which version number. </p> <p> In short, you <ul> <li>increment the patch version (e.g. from 2.3.4 to 2.3.5) when you only release bug fixes and the like</li> <li>increment the minor version (e.g. from 1.3.2 to 1.4.0) when you add new features</li> <li>increment the major version (e.g. from 3.2.9 to 4.0.0) when you introduce breaking changes</li> </ul> This makes it much easier for you when you need to make a decision on a new version number, and it also makes it much easier for consumers of your software to understand when an update is 'safe', and when they should set aside some time to test compatibility. </p> <h3 id="4d69270dacf44904a67e785b6bad89d4"> Continuous Deployment <a href="#4d69270dacf44904a67e785b6bad89d4" title="permalink">#</a> </h3> <p> While Semantic Versioning is great, it requires a bit of consideration when combined with <a href="http://puppetlabs.com/blog/continuous-delivery-vs-continuous-deployment-whats-diff">Continuous Deployment</a>. Every time you deploy a new version, you should increment the version number. </p> <p> <a href="http://en.wikipedia.org/wiki/Continuous_delivery">Continuous Delivery</a> and Continuous Deployment rely on automation. A code check-in triggers an automated build, which is subsequently processed by a Deployment Pipeline, and potentially released to end-users. Each released (or releasable) build should have a unique version. </p> <p> Traditionally, Build Servers have had the responsibility of incrementing version numbers - typically by incrementing a <em>build</em> number, like this: <ol> <li>3.7.11.942</li> <li>3.7.12.958</li> <li>3.7.13.959</li> <li>3.7.14.979</li> <li>3.7.15.987</li> </ol> where the fourth number is a <em>revision</em> number, that may correspond to a revision ID in a source control system (whether or not that makes sense, depends on which version control system you use). </p> <p> Unfortunately, this versioning scheme is <em>wrong</em> if you combine Semantic Versioning with Continuous Deployment. Even if you throw away the fourth <em>build</em> number, you're left with a sequence like this: <ol> <li>3.7.11 (bug fix)</li> <li>3.7.12 (partial new feature, hidden behind a <a href="http://martinfowler.com/bliki/FeatureToggle.html">Feature Toggle</a>.)</li> <li>3.7.13 (performance improvement)</li> <li>3.7.14 (completed feature initiated in 3.7.12)</li> <li>3.7.15 (breaking changes in public API)</li> </ol> That's not Semantic Versioning. </p> <p> Semantic Versioning might look like this: <ol> <li>3.7.11 (bug fix)</li> <li>3.7.12 (partial new feature, hidden behind a <a href="http://martinfowler.com/bliki/FeatureToggle.html">Feature Toggle</a>.)</li> <li>3.7.13 (performance improvement)</li> <li>3.8.0 (completed feature initiated in 3.7.12)</li> <li>4.0.0 (breaking changes in public API)</li> </ol> This doesn't work well with automatically incrementing the version number. </p> <h3 id="0db8ce54ad294235adfc11e92da8c139"> Versioning is a programmer decision <a href="#0db8ce54ad294235adfc11e92da8c139" title="permalink">#</a> </h3> <p> With Continuous Deployment, every time you integrate code (check in, merge, rebase, whatever), you produce a version of the software that will be deployed. This means that every time you integrate, <em>something</em> or <em>somebody</em> should assign a new version to the software. </p> <p> The rules of Semantic Versioning require explicit decisions to be made. Only the development team understands what a particular commit contains. Is it a fix? Is it a new feature? Is it a breaking change? A Build Server doesn't know how to answer these questions, but <em>you</em> do. </p> <p> A few years ago, I changed the delivery scheme for my open source project <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> to use <a href="/2011/09/06/AutoFixturegoesContinuousDeliverywithSemanticVersioning">Semantic Versioning with Continuous Deployment</a>. When I did that, I realised that I could no longer rely on a Build Server for controlling the version. Instead, I would have to explicitly control the versioning as part of the commit process. </p> <p> Because AutoFixture is a .NET project, I decided to use the version assignment mechanism already present in the framework: The <a href="http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.aspx">[AssemblyVersion]</a> and <a href="http://msdn.microsoft.com/en-us/library/system.reflection.assemblyfileversionattribute.aspx">[AssemblyFileVersion]</a> attributes that you typically put in AssemblyInfo files. </p> <p> The version control system used for AutoFixture is <a href="http://git-scm.com">Git</a>, so it works like this in practice: <ol> <li>A programmer adds one or more commits to a branch.</li> <li>The programmer sends a pull request.</li> <li>I pull down the commits from the pull request.</li> <li>I increment <em>all</em> the version attributes in <em>all</em> the AssemblyInfo files, and commit that change.</li> <li>I push the commits to <em>master</em>.</li> <li>The Build Server picks up the new commits, and the Deployment Pipeline kicks in.</li> </ol> This works well. You can see an example of this process if you examine <a href="https://github.com/AutoFixture/AutoFixture/commits/master">the commit log for AutoFixture</a>. The only problem is that AutoFixture has 28 AssemblyInfo files (each with two version attributes) that I must update and keep in sync. That's a lot of work, so obviously a target for automation, but that's the subject for <a href="/2013/12/11/zerotonine">another blog post</a>. </p> <p> After more than two years of experience with this way of controlling software versions, I'm consistently using this approach for all <a href="https://github.com/ploeh">my open source software</a>, as well as the internal software we create in <a href="http://grean.com">Grean</a>. </p> <h3 id="080025496ea140b98bba612f9651fc55"> Summary <a href="#080025496ea140b98bba612f9651fc55" title="permalink">#</a> </h3> <p> If you want to use Continuous Deployment (or Delivery) with Semantic Versioning, the assignment of a new version number is a programmer decision. Only a human understands when a commit constitutes a bug fix, a new feature, or a breaking change. The new version number must be committed to the version control system, so that whomever or whatever compiles and/or releases the software will always use the same version number for the same version of the source code. </p> <p> The version number is kept in the source control system, together with the source code. It's not the realm of a Build Server. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4826b4ebbc04458ab7618594cc19de72"> <div class="comment-author"><a href="http://www.augi.cz">Augi</a> <a href="#4826b4ebbc04458ab7618594cc19de72">#</a></div> <div class="comment-content">You wrote Build Server doesn't know how to answer some questions. I think that it could - if every commit contains link to issue ID and Build Server is able to check type, state, etc. of issues related to given build then the Build Server could theoreticaly make the decision about version incrementing.</div> <div class="comment-date">2013-12-10 17:02 UTC</div> </div> <div class="comment" id="dcefe24f0be548798f7db78bcb4b6adb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dcefe24f0be548798f7db78bcb4b6adb">#</a></div> <div class="comment-content"> <p> Augi, it's true that you can create other approaches in order to attempt to address the issue, but the bottom line remains that a <em>human being</em> must make the decision about how to increment the version number. As you suggest, you can put information guiding that decision outside the source code itself, but then you'd be introducing another movable part that can break. If you do something like you suggest, you'll still need to add some machine-readable metadata to the linked issue ID. To add spite to injury, this also makes it more difficult to reproduce what the Build Server does on your local machine - particularly if you're attempting to build while offline. </p> <p> While it sounds like it would be possible, what do you gain by doing something like that? </p> </div> <div class="comment-date">2013-12-10 19:16 UTC</div> </div> <div class="comment" id="f895893358e941be82256ca74fc631ef"> <div class="comment-author"><a href="https://github.com/nuggetboy">Chris Simmons</a> <a href="#f895893358e941be82256ca74fc631ef">#</a></div> <div class="comment-content"> <p> Mark, I also have a Visual Studio solution or two with multiple AssemblyInfo.cs files (although not as many as you) and wish to use a common version number for each contained project. I came up with the following approach, which doesn't require any automation. It only uses the Visual Studio/MSBuild &lt;Link /&gt; functionality. The key is simply to use the <a href="http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714082(v=vs.105).aspx">Add As Link</a> functionality for common attributes. </p> <p> Simply put, I split out the common information (Version info and company/copyright/trademark info) from projects' AssemblyInfo.cs files into another file called SolutionAssemblyInfo.cs. I place that file at the root of the solution (outside of any project folders). Then, for each project, remove the version information from the AssemblyInfo.cs file and use the 'Add As Link' function in the 'Add Existing Item' function in Visual Studio to link to the SolutionAssemblyInfo.cs file. With that, you have only one place to update the version information: the SolutionAssemblyInfo.cs file. Any change to that version information will be included in each project. </p> <p> That might be enough information to get you going, but if not, I'll expand and outline the specific process. The basic idea is to look at the AssemblyInfo.cs file as having two sets of metadata: <ul> <li>Metadata specific to the project itself: <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.assemblytitleattribute.aspx">[AssemblyTitle]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.AssemblyDescriptionattribute.aspx">[AssemblyDescription]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.AssemblyConfigurationattribute.aspx">[AssemblyConfiguration]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.AssemblyProductattribute.aspx">[AssemblyProduct]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.AssemblyCultureattribute.aspx">[AssemblyCulture]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/System.Runtime.InteropServices.ComVisibleAttribute.aspx">[ComVisible]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/System.Runtime.InteropServices.GuidAttribute.aspx">[Guid]</a></li> </ul> </li> <li>Metadata that really should be shared amongst all projects in the solution. Specifically: <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.aspx">[AssemblyVersion]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.assemblyfileversionattribute.aspx">[AssemblyFileVersion]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.assemblyinformationalversionattribute.aspx">[AssemblyInformationalVersion]</a> (Although, depending on your needs, you may choose to leave this attribute in the project-specifics AssemblyInfo.cs)</li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.AssemblyCompanyattribute.aspx">[AssemblyCompany]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.AssemblyCopyrightattribute.aspx">[AssemblyCopyright]</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.reflection.AssemblyTrademarkattribute.aspx">[AssemblyTrademark]</a></li> </ul> </li> </ul> </p> <p> You can separate the shared metadata into a common AssemblyInfo.cs file. Then, by linking to that common file in each project (as opposed to including), you won't need to update 28 files; you'll only need to update the common one. </p> <p> Assume I have the following AssemblyInfo.cs file for one of my projects: <blockquote><pre> // AssemblyInfo.cs using System.Reflection; using System.Runtime.InteropServices; [assembly: AssemblyTitle("SampleProject")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Company Name")] [assembly: AssemblyProduct("SampleProject")] [assembly: AssemblyCopyright("Copyright (c) Company Name 2013")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("7ae5f3ab-e519-4c44-bb65-489305fc36b0")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] </pre></blockquote> I split this out into two files: <blockquote><pre> // AssemblyInfo.cs using System.Reflection; using System.Runtime.InteropServices; [assembly: AssemblyTitle("SampleProject")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyProduct("SampleProject")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("7ae5f3ab-e519-4c44-bb65-489305fc36b0")] </pre></blockquote> and <blockquote><pre> // SolutionAssemblyInfo.cs using System.Reflection; [assembly: AssemblyCompany("Company Name")] [assembly: AssemblyCopyright("Copyright (c) Company Name 2013")] [assembly: AssemblyTrademark("")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] // Depending on your needs, AssemblyInformationalVersion as well? </pre></blockquote> </p> <p> The SolutionAssemblyInfo.cs goes in the root of your solution and should initially not be included in any projects. Then, for each project: <ul> <li>Remove all attributes that went into SolutionAssemblyInfo.cs</li> <li>Right-click the project and &quot;Add..., Existing Item...&quot;</li> <li>Navigate to the SolutionAssemblyInfo.cs file</li> <li>Instead of clicking the &quot;Add&quot; button, click the little drop-down on it and select &quot;Add As Link&quot;</li> <li>If you want the new linked SolutionAssemblyInfo.cs file to show up under the Properties folder (like the AssesmblyInfo.cs file), just drag it from the project root into the Properties folder. Unfortunately, you can't simply add the link to the Properties folder directly (at least not in VS 2012).</li> </ul> (Note: it looks like there may be an easier method in VS 2012+ only to get the &quot;Add As Link&quot; function, mentioned <a href="http://stackoverflow.com/a/12858818/208990">on StackOverflow</a>, by simply dragging and dropping while holding the Alt key.) </p> <p> That's it. Now, you will be able to access this SolutionAssemblyInfo.cs file from any of your projects and any changes you make to that file will persist into the linked file, being shared with all projects. </p> <p> The downside to this, as opposed to an automation solution, is that you need to repeat this process (starting with &quot;Remove all attributes...&quot;) for all new projects you add. However, in my opinion, that's a small, one-time-per-project price to pay. With the above, you let the established tool work for you with built-in features. </p> </div> <div class="comment-date">2013-12-10 18:00 UTC</div> </div> <div class="comment" id="a9a88b94f4e44f06819425e73e453931"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a9a88b94f4e44f06819425e73e453931">#</a></div> <div class="comment-content"> <p> Chris, thank you for your comment. That may actually be a good way to do it, too. While I did know about the <em>add as link</em> feature of Visual Studio, I've had bad experiences with it in the past. This may actually be a <a href="http://en.wikipedia.org/wiki/Classical_conditioning">Pavlovian reaction</a> on my part, because I must admit that I can no longer remember what those bad experiences were :$ </p> </div> <div class="comment-date">2013-12-10 20:38 UTC</div> </div> <div class="comment" id="42b5485f59e945e5923f956d9c232954"> <div class="comment-author">Laurence Evans <a href="#42b5485f59e945e5923f956d9c232954">#</a></div> <div class="comment-content"> <p> I had been thinking about this a bit myself and I believe an easy solution to this is to just make use of a modified branching structure in the same/similar setup as the versioning. So you'd have your major/minor/build branches and your build server could increment numbers differently depending on which branch you update which would fully take care of the automation side of things for you. This would be rather trivial to setup and maintain but fulfill your requirements set out in the post. </p> <p> Of course you would have to be quite disciplined as to which branch you commit your code to but I don't see that being too much of an overhead, you usually know when you're going to be patching/creating new featuresd or introducing breaking changes before you start working. Worst case make use of git stash save/pop to move work between branches. </p> <p> Could call this semantic branching? </p> </div> <div class="comment-date">2013-12-10 23:54 UTC</div> </div> <div class="comment" id="f0cb6cbe392a470998c8334ffa5b43e0"> <div class="comment-author"><a href="https://github.com/serra">marijn van der Zee</a> <a href="#f0cb6cbe392a470998c8334ffa5b43e0">#</a></div> <div class="comment-content"> <blockquote> <p> Could call this semantic branching? </p> </blockquote> <p> You might be interested in <a href="https://github.com/Particular/GitFlowVersion">the GitFlowVersion project</a>, which leverages some of the concepts you mention. </p> </div> <div class="comment-date">2013-12-11 09:54 UTC</div> </div> <div class="comment" id="f27c2a785c9d49cd9fa406a47107bc6a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f27c2a785c9d49cd9fa406a47107bc6a">#</a></div> <div class="comment-content"> <p> Laurence, Marijn, thank you for your comments. As cool as Git is (currently, I'm not aware of anything better), I don't like coupling a process unnecessarily to a particular tool. What if, some day, something better than Git arrives? </p> <p> Additionally, I agree with Martin Fowler, Jez Humble, and others, that branching is essentially evil, so I don't think it's a good idea building an entire versioning scheme around branches. </p> <p> As an alternative, I've <a href="/2013/12/11/zerotonine">introduced ZeroToNine</a>, a command-line tool (and library) that works independently of any version control system. </p> </div> <div class="comment-date">2013-12-11 12:55 UTC</div> </div> <div class="comment" id="ba51505868d348ee9e9fd2206946e113"> <div class="comment-author"><a href="http://grenade.github.io">Rob Thijssen</a> <a href="#ba51505868d348ee9e9fd2206946e113">#</a></div> <div class="comment-content"> <p> <quote>you'd be introducing another movable part that can break...<br> ...sounds like it would be possible, what do you gain by doing something like that?</quote> </p> <p> Humans are/have moving parts that can break too ;). In large organisations there are often as many differences of opinion as there are people. One developer's "breaking change" or "feature" is another's "improvement" or "bugfix". Human decision making also introduces arbitrarily variable logic. Software projects rotate developers in and out all the time. Will they all apply consistent logic to their versioning decisions? </p> <p> Developers can make a decision about releases without incrementing a number in a file. They can for example click on a "Push to NuGet" or "Release to GitHub" button (which is a human, developer decision). It's then trivial for a CI server to calculate or increment a PATCH number based on the last NuGet or GitHub Push/Release. A MINOR version can be easily determined by linking to an issue tracker with issues that are linked to milestones. A MAJOR version is probably simplest when a human increments it, but I see no reason why it couldn't also be triggered by monitoring changes or breakages to existing unit tests (for example). Considering the clarity of the semver MAJOR.MINOR.PATCH definitions, I think an algorithm determining the version number is more consistent than a human decision. For example (in pseudo-code): </p> <pre>while (a release request is in progress) if (app has previously been 'released' to some repository AND has subsequently changed in any way) increment PATCH... unless (all issues and features in next milestone have been marked closed, subsequent to last release) increment MINOR and reset PATCH to zero... unless (unit tests that predate release have changed OR dependent application unit tests are failing OR some other determination of breaking change) increment MAJOR and reset MINOR and PATCH to zero...</pre> </div> <div class="comment-date">2014-04-02 17:25 UTC</div> </div> <div class="comment" id="06ee47201d8a4fb9b8459be2c2b36dde"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#06ee47201d8a4fb9b8459be2c2b36dde">#</a></div> <div class="comment-content"> <p> Rob, thank you for writing. If I could have defined a trustworthy automated system to figure out semantic versioning, I would have done it. Is your proposed algorithm sufficiently robust? <ul> <li>How does the algorithm determine if all issues and features in the 'next milestone' have been marked closed? What if your issue tracking system is off-line?</li> <li>Considering that the context here is <em>Continuous Delivery</em>, would one really have to create and maintain a 'milestone' for every update?</li> <li>When an 'issue' is resolved, how does the algorithm know if it was a new feature, or a bug fix?</li> <li>How does the algorithm determine if a unit test has changed in a breaking fashion? A unit test could have been refactored simply to make it easier to read, without actually changing the behaviour of the system.</li> <li>How does the algorithm determine if a failing test was due to a breaking change, or that the actual reason was a badly written test?</li> </ul> </p> </div> <div class="comment-date">2014-04-04 18:11 UTC</div> </div> <div class="comment" id="27b27185ba774f4c82189f8eb3b9fe1b"> <div class="comment-author"><a href="http://gusvmx.blogspot.mx/">Gus Vargas</a> <a href="#27b27185ba774f4c82189f8eb3b9fe1b">#</a></div> <div class="comment-content"> <p> What about versioning based on the branch names. I mean, what if we name a branch regarding to what it is suposed to do at the end. For instance, naming branches as feature-xxx, major-xxx, patch-xxx. Where I want to go is to automate the semantic versioning everytime a pull/merge request is accepted. So then the CI/CD tool, through a shell for instance, can just look at the last commit comment which is usually 'Merge branch xxx into master' (where xxx can be feature-yyy, major-yyy, patch-yyy) and increment the version acording to the branch merged. If it's a feature it increases the digit in the middle and resets the last one. On the other hand it it's a patch it only increases the last digit. Would it work? I mean the assignment of the new version is still a programmer decision which is done when they branch from master. </p> </div> <div class="comment-date">2015-08-06 00:02 UTC</div> </div> <div class="comment" id="ab32464380184f268c8dd8b0e372acbf"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ab32464380184f268c8dd8b0e372acbf">#</a></div> <div class="comment-content"> <p> Gus, thank you for writing. I think your suggestion could work as well, although I haven't tried it. </p> <p> The advantage of your suggestion is that it's more declarative. You address the question of <em>what</em> should happen (major, minor, patch), instead of <em>how</em> it should happen (which number). That's usually a good thing. </p> <p> The disadvantage is that you push the burden of this to a central build step (on a build server, I presume), so it introduces more moving parts into the process, as well as a single point of failure. </p> <p> Fortunately, the evaluation of advantages versus disadvantages can be a personal (or team) decision, so one can choose the option one likes best. It's always good to have options to choose from in the first place, so thank you for sharing! </p> </div> <div class="comment-date">2015-08-06 06:06 UTC</div> </div> <div class="comment" id="5e1cee2478184a9a9befe237fc503099"> <div class="comment-author"><a href="https://github.com/jwstric2/">Jonathan Strickland</a> <a href="#5e1cee2478184a9a9befe237fc503099">#</a></div> <div class="comment-content"> <p> Mark, I feel a bit outdated responding to a post that has a 4th birthday coming up. I completely agree with Semantic versioning, even for cloud application deployments which my team is working with at this moment. I am intrigued to how your workflow is working. </p> <p> In our current workflow, we are forcing a version and changelog to be associated with a Pull Request. Thus, the developer is incrementing this as part of the PR and our auditor pipeline is ensuring that the version/changelog is updated. The team of course still have to ensure this version is correct, ie are you sure this is a micro change, look like a new feature to me or looks to me like you broke API compatibility and this is a major increment. The issue we are starting to hit with this early model is our team is growing and we are facing constant merge conflicts with our version file and Changelog (its a ruby on rails project thus we use a config.yml for the version which is read in at runtime by our app and displayed properly as a link on the apps's page back to our Changelog version) </p> <p> It appears in your workflow that you have hooks set up so that these are initiated by the person merging the code such these files are only changed post-merge and commited then. If this is elaborated on in one of your books, let me know and my team could take a "work" break to go do some reading. I appreciate your time on the matter. </p> </div> <div class="comment-date">2017-08-09 17:54 UTC</div> </div> <div class="comment" id="7ae4b9e0bc9e44738e62ab8430a45ec5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7ae4b9e0bc9e44738e62ab8430a45ec5">#</a></div> <div class="comment-content"> <p> Jonathan, thank you for writing, and don't worry about the age of the post. It only pleases me that apparently I've managed to produce something durable enough to be of interest four years later. </p> <p> In the interest of full disclosure, the busiest code base on which I've ever used this technique is <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>, and to be clear, <a href="https://github.com/AutoFixture/AutoFixture/issues/703">I've handed off the reigns of that project</a>. I've worked on bigger and more busy code bases than that, but these were internal enterprise code bases that didn't use Semantic Versioning. </p> <p> Based on my experience with AutoFixture, I'd still use the process I used back then. It went something like this: <ol> <li>If a code review resulted in my decision to accept a pull request, I'd pull it down on my laptop.</li> <li>Once on my laptop, I'd run the entire build locally. While I do realise that GitHub has a <em>merge</em> button, I regarded this as an extra verification step. While we had CI servers running, I think it never hurts to verify that it also builds on a developer's machine. Otherwise, you'd just have a problem the next time someone pulls <em>master</em>.</li> <li>If the build passed, I'd merge the branch locally.</li> <li>I'd then run <em>a single</em> <a href="https://github.com/ploeh/ZeroToNine">Zero29</a> command to update all version information in all appropriate files.</li> <li>This single command would modify a set of text files, which I'd then check in. If you look at the AutoFixture commit history, you'll see lots of those check-ins.</li> <li>Once checked in, I'd tag the commit with the version. Often I'd use a cryptic bash command that I no longer remember to first read the current version with Zero29, then pipe that number to some other utility that could produce the appropriate tag, and then pipe that to <code>git tag</code>. The point is: that could be an automated step as well.</li> <li>Then I'd build the release binaries. That would be one other command.</li> <li>Penultimately, I'd publish the release by pushing all binaries to NuGet.org. That would be one other bash command.</li> <li>Finally, I'd push <em>master</em> and the new tag to GitHub.</li> </ol> As you can tell, that's less than a dozen bash commands. I could have automated most of it to one or two shell scripts, but I never got around to do that because I rather enjoyed the process. Consider that I didn't do this every day. If I had to do it several times a day, I would probably automate it more. </p> <p> I'm sure you could even write a server-side script with a Web UI that could do this, if you wanted to, but I've always preferred doing a local build as part of the verification process. </p> <p> I don't think I've written much more about this, rather than the <a href="/2013/12/11/zerotonine">announcement post for ZeroToNine</a>, as well as the documentation for it. </p> </div> <div class="comment-date">2017-08-09 20:09 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Layers, Onions, Ports, Adapters: it's all the same https://blog.ploeh.dk/2013/12/03/layers-onions-ports-adapters-its-all-the-same 2013-12-03T18:59:00+00:00 Mark Seemann <div id="post"> <p> <em>If you apply the Dependency Inversion Principle to Layered Architecture, you end up with Ports and Adapters.</em> </p> <p> One of my readers, <a href="http://it.linkedin.com/pub/giorgio-sala/14/6b2/51a">Giorgio Sala</a>, asks me: <blockquote> In his book "<a href="http://amzn.to/IDXcnx">Implementing DDD</a>" mr Vernon talks a lot about the Ports and Adapter architecture as a next level step of the Layered architecture. I would like to know your thinking about it. </blockquote> </p> <p> The short answer is that this is more or less the architecture I describe in <a href="http://amzn.to/12p90MG">my book</a>, although in the book, I never explicitly call it out by that name. </p> <h3 id="db5adf784deb4c9b92a656627283c9da"> Layered architecture <a href="#db5adf784deb4c9b92a656627283c9da" title="permalink">#</a> </h3> <p> In my book, I describe the common pitfalls of a typical layered architecture. For example, in chapter 2, I analyse a typical approach to layered architecture; it's an example of what not to do. Paraphrased from the book's figure 2.13, the erroneous implementation creates this dependency graph: </p> <p> <img src="/content/binary/typical-layered-dependency-graph.png"> </p> <p> The arrows show the direction of dependencies; i.e. the User Interface library depends on the Domain library, which in turn depends on the Data Access library. This violates the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> (DIP), because the Domain library depends on the Data Access library, and the DIP says that: </p> <blockquote> <p> Abstractions should not depend upon details. Details should depend upon abstractions. </p> <p> - <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices in C#</a>, p. 154 </p> </blockquote> <p> Later in chapter 2, and throughout the rest of my book, I demonstrate how to invert the dependencies. Paraphrased, figure 2.12 looks like this: </p> <p> <img src="/content/binary/inverted-layered-dependency-graph.png"> </p> <p> This is almost the same figure as the previous, but notice that the direction of dependency has changed, so that the Data Access library now depends on the Domain library, instead of the other way around. This is the DIP applied: the details (UI, Data Access) depend on the abstractions (the Domain Model). </p> <h3 id="1b81d8e79ef946ba90872ba8cb007ee5"> Onion layers <a href="#1b81d8e79ef946ba90872ba8cb007ee5" title="permalink">#</a> </h3> <p> The example from chapter 2 in my book is obviously simplified, with only three libraries involved. Imagine a generalized architecture following the DIP: </p> <p> <img src="/content/binary/inverted-layered-complex-dependency-graph.png"> </p> <p> While there are many more libraries, notice that all dependencies still point inwards. If you're still thinking in terms of <em>layers</em>, you can draw concentric layers around the boxes: </p> <p> <img src="/content/binary/onion-layers-dependency-graph.png" alt="Ports and adapters architecture diagram."> </p> <p> These concentric layers resemble the layers of an onion, so it's not surprising that <a href="http://jeffreypalermo.com">Jeffrey Palermo</a> calls this type of architecture for <a href="http://jeffreypalermo.com/blog/the-onion-architecture-part-1/">Onion Architecture</a>. </p> <p> The DIP still applies, so dependencies can only go in one direction. However, it would seem that I've put the UI components (the orange boxes) and the Data Access components (the blue boxes) in the same layer. (Additionally, I've added some yellow boxes that might symbolise unit tests.) This may seem unfamiliar, but actually makes sense, because the components in the outer layer are all at the boundaries of the application. Some boundaries (such as UI, RESTful APIs, message systems, etc.) face outward (to the internet, extranets, etc.), while other boundaries (e.g. databases, file systems, dependent web services, etc.) face inward (to the OS, database servers, etc.). </p> <p> As the diagram implies, components can depend on other components within the same layer, but does that mean that UI components can talk directly to Data Access components? </p> <h3 id="ea9c6b5f960243409091e9b9dc379f61"> Hexagonal architecture <a href="#ea9c6b5f960243409091e9b9dc379f61" title="permalink">#</a> </h3> <p> While traditional Layered Architecture is no longer the latest fad, it doesn't mean that all of its principles are wrong. It's still not a good idea to allow UI components to depend directly on the Data Access layer; it would couple such components together, and you might accidentally bypass important business logic. </p> <p> You have probably noticed that I've grouped the orange, yellow, and blue boxes into separate clusters. This is because I still want to apply the old rule that UI components must not depend on Data Access components, and vice versa. Therefore, I introduce bulkheads between these groups: </p> <p> <img src="/content/binary/onion-layers-with-bulkheads-dependency-graph.png"> </p> <p> Although it may seem a bit accidental that I end up with exactly <em>six</em> sections (three of them empty), it <em>does</em> nicely introduce <a href="http://alistair.cockburn.us">Alistair Cockburn</a>'s closely related concept of <a href="http://alistair.cockburn.us/Hexagonal+architecture">Hexagonal Architecture</a>: </p> <p> <img src="/content/binary/hexagonal-architecture-dependency-graph.png"> </p> <p> You may feel that I cheated a bit in order to make my diagram hexagonal, but that's okay, because there's really nothing inherently <em>hexagonal</em> about Hexagonal Architecture; it's not a particularly descriptive name. Instead, I prefer the alternative name <em>Ports and Adapters</em>. </p> <h3 id="495be4292c8948fb97eb68fc0cd61625"> Ports and Adapters <a href="#495be4292c8948fb97eb68fc0cd61625" title="permalink">#</a> </h3> <p> The only thing still bothering me with the above diagram is that the dependency hierarchy is too deep (at least conceptually). When the diagram consisted of concentric circles, it had three (onion) layers. The hexagonal dependency graph above still has those intermediary (grey) components, but as I've <a href="http://stackoverflow.com/a/9503612/126014">previously attempted to explain, the flatter the dependency hierarchy, the better</a>. </p> <p> The last step, then, is to flatten the dependency hierarchy of the inner hexagon: </p> <p> <img src="/content/binary/ports-and-adapters-dependency-graph.png"> </p> <p> The components in the inner hexagon have few or no dependencies on each other, while components in the outer hexagon act as <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapters</a> between the inner components, and the application boundaries: its <em>ports</em>. </p> <h3 id="f7121ec3ea5f448ab99a5b3a1859fd42"> Summary <a href="#f7121ec3ea5f448ab99a5b3a1859fd42" title="permalink">#</a> </h3> <p> In my book, I never explicitly named the architecture I describe, but essentially, it <em>is</em> the Ports and Adapters architecture. There are other possible application architectures than the variations described here, and some of them still work well with Dependency Injection, but the main architectural emphasis in <a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a> is Ports and Adapters, because I judged it to be the least foreign for the majority of the book's readers. </p> <p> The reason I never explicitly called attention to Ports and Adapters or Onion Architecture in my book is that I only became aware of these pattern names as I wrote the book. At that time, I didn't feel confident that what I did matched those patterns, but the more I've learned, the more I've become convinced that this was what I'd been doing all along. This just confirms that Ports and Adapters is a bona fide <em>pattern</em>, because one of the attributes of patterns is that they materialize independently in different environments, and are then <em>subsequently discovered</em> as patterns. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Not going to NDC London after all https://blog.ploeh.dk/2013/11/19/not-going-to-ndc-london-after-all 2013-11-19T15:25:00+00:00 Mark Seemann <div id="post"> <p> <em>Unfortunately, I've had to cancel my speaking engagement at NDC London 2013.</em> </p> <p> Ever since I was accepted as a speaker at <a href="http://www.ndc-london.com">NDC London 2013</a>, I've really looked forward to it. Unfortunately, due to serious illness in my family, I've decided to cancel my appearance at the conference. This has been a difficult decision to make, but now that I've made it, I can feel that it's the right decision, even though it pains me. </p> <p> I hope to be able to return to NDC another time, both in Oslo and in London. </p> <p> If you visit my <a href="http://lanyrd.com/profile/ploeh">Lanyrd profile</a>, you will see that while I have removed myself from NDC London, I'm still scheduled to speak at the <a href="http://thewcdc.net">Warm Crocodile Developer Conference</a> in January 2014. I still hope to be able to attend and speak here. Not only is it realistic to hope that my family situation is better in January, but because the conference is in my home town, it also means that it puts less of a strain on my family. This may change, though... </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Albedo https://blog.ploeh.dk/2013/11/15/albedo 2013-11-15T08:24:00+00:00 Mark Seemann <div id="post"> <p> <em>Albedo is a .NET library targeted at making Reflection programming more consistent, using a common set of abstractions and utilities.</em> </p> <p> For the last five weeks, <a href="http://nikosbaxevanis.com">Nikos Baxevanis</a>, <a href="http://rarelyupdated.azurewebsites.net">Adam Chester</a>, and I have been working on a new, small Open Source project: <a href="https://github.com/ploeh/Albedo">Albedo</a>. </p> <p> It's a .NET library targeted at making Reflection programming more consistent, using a common set of abstractions and utilities. The <a href="https://github.com/ploeh/Albedo">project site</a> may actually contain more details than you'd care to read, but here's a small code sample to either whet your appetite, or scare you away: </p> <p> <pre style="margin: 0px;"><span style="color: #2b91af;">PropertyInfo</span> pi = <span style="color: blue;">from</span> v <span style="color: blue;">in</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">Properties</span>&lt;<span style="color: #2b91af;">Version</span>&gt;() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span style="color: blue;">select</span> v.Minor; <span style="color: blue;">var</span> version = <span style="color: blue;">new</span> <span style="color: #2b91af;">Version</span>(2, 7); <span style="color: blue;">var</span> visitor = <span style="color: blue;">new</span> <span style="color: #2b91af;">ValueCollectingVisitor</span>(version); &nbsp; <span style="color: blue;">var</span> actual = <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertyInfoElement</span>(pi).Accept(visitor); &nbsp; <span style="color: #2b91af;">Assert</span>.Equal(version.Minor, actual.Value.OfType&lt;<span style="color: blue;">int</span>&gt;().First());</pre> </p> <p> Albedo is <a href="http://www.nuget.org/packages/Albedo">available via NuGet</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Mocks for Commands, Stubs for Queries https://blog.ploeh.dk/2013/10/23/mocks-for-commands-stubs-for-queries 2013-10-23T21:45:00+00:00 Mark Seemann <div id="post"> <p> <em>When unit testing, use Mocks for Commands, and Stubs for Queries.</em> </p> <p> A few years ago, I helped an organization adopt Test-Driven Development. One question that kept coming up was when to use <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a>, and when to use <a href="http://xunitpatterns.com/Mock%20Object.html">Mocks</a>. (First, you need to understand the <em>conceptual</em> difference between Mocks and Stubs, so go read <a href="/ref/xunit-patterns">xUnit Test Patterns</a>, and then resume reading this blog post once you're done. If you need a shorter introduction, you can read <a href="http://msdn.microsoft.com/en-us/magazine/cc163358.aspx">my MSDN Magazine article on the subject</a>.) </p> <p> After having answered the question on when to use what, a couple of times, I arrived at this simple rule, based on the language of <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> (CQS): <ul> <li>Use Mocks for Commands</li> <li>Use Stubs for Queries</li> </ul> This makes lots of sense, because Commands are all about side effects, and Mocks are all about <a href="http://xunitpatterns.com/Behavior%20Verification.html">Behaviour Verification</a>: that is, that side effects occurred. Stubs, on the other hand, mainly exist to 'make happy noises', and one of the ways they have to do that, is to return data from dependencies, when return data is required. </p> <p> This discovery made me really happy, and I planned to blog about it. Then, soon after, I saw the exact same advice in <a href="http://amzn.to/VI81bP">GOOS</a> (ch. 24, p. 278): <blockquote> Allow Queries; Expect Commands </blockquote> That verified that I was on the right track, but on the other hand, since the rule was already described, I didn't see the value of restating what was already public knowledge. </p> <p> A couple of years later, it's clear to me that this rule may be public knowledge, but is certainly isn't <em>common</em> knowledge. This is, without comparison, the most common mistake I see people make, when I review unit tests. Thus, although it has already been said in GOOS, I'll repeat it here. <blockquote> Use Mocks for Commands; Stubs for Queries </blockquote> Through an example, you'll learn why Mocks for Queries are dangerous, and how a technique I call <em>Data Flow Verification</em> can help. </p> <h3 id="c335fe5e1afa4e2886d26cd221b4acee"> (Anti-)Example: Mocking Queries <a href="#c335fe5e1afa4e2886d26cd221b4acee" title="permalink">#</a> </h3> <p> In this example, you'll see how easily things can go wrong when you use Mocks for Queries. Assume that you want to test SomeController, that uses an IUserRepository: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">User</span> Read(<span style="color: blue;">int</span> userId); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">void</span> Create(<span style="color: blue;">int</span> userId); }</pre> </p> <p> In the first test, you want to verify that when you invoke SomeController.GetUser, it returns the user from the injected Repository, so you write this unit test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetUserReturnsCorrectResult() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">User</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IUserRepository</span>&gt;(); &nbsp;&nbsp;&nbsp; td.Setup(r =&gt; r.Read(<span style="color: #2b91af;">It</span>.IsAny&lt;<span style="color: blue;">int</span>&gt;())).Returns(expected); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SomeController</span>(td.Object); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.GetUser(1234); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(expected, actual); }</pre> </p> <p> This test uses <a href="https://code.google.com/p/moq">Moq</a>, but it could have used another dynamic mock library as well, or even hand-written <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>. </p> <p> The most remarkable characteristic of this test is the <em>unconditional</em> return of a value from the Test Double, as implemented by the use of <code>It.IsAny&lt;int&gt;()</code>. Why you'd do this is a small mystery to me, but I see it time and again when I review unit tests. This is the source of many problems. </p> <p> Using a technique called <a href="https://blog.ploeh.dk/outside-in-tdd">Devil's Advocate</a>, when reviewing, I usually tell the author that I can make the test pass with this (obviously degenerate) implementation: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">User</span> GetUser(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.userRepository.Read(0); }</pre> </p> <p> In my experience, this will most likely prompt you to <em>add another</em> test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(1234)] [<span style="color: #2b91af;">InlineData</span>(9876)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetUserCallsRepositoryWithCorrectValue(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IUserRepository</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SomeController</span>(td.Object); &nbsp; &nbsp;&nbsp;&nbsp; sut.GetUser(userId); &nbsp; &nbsp;&nbsp;&nbsp; td.Verify(r =&gt; r.Read(userId)); }</pre> </p> <p> Jolly good attempt, sport! Regrettably, it doesn't protect you against my Devil's Advocate technique; I can implement the GetUser method like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">User</span> GetUser(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository.Read(userId); &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.userRepository.Read(0); }</pre> </p> <p> This is so obviously wrong that you'd be likely to argue that no-one in their right mind would do something like that. While I agree, the fact that this (obviously incorrect) implementation passes all unit tests should inform you about the quality of your unit tests. </p> <h3 id="b21f7525b41045108fae1c20c0328518"> Strict Mocks are not the solution <a href="#b21f7525b41045108fae1c20c0328518" title="permalink">#</a> </h3> <p> The problem is that the second test method (GetUserCallsRepositoryWithCorrectValue) attempts to use a Mock to verify a Query. This is wrong, as indicated by GOOS. </p> <p> In desperation, some people attempt to resolve their conundrum by tightening the constraints of the Mock. This road leads towards Strict Mocks, and there lies madness. Soon, you'll learn the reason for this. </p> <p> First, you may attempt to constrain the number of times the Read method can be invoked: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(1234)] [<span style="color: #2b91af;">InlineData</span>(9876)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetUserCallsRepositoryWithCorrectValue(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IUserRepository</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SomeController</span>(td.Object); &nbsp; &nbsp;&nbsp;&nbsp; sut.GetUser(userId); &nbsp; &nbsp;&nbsp;&nbsp; td.Verify(r =&gt; r.Read(userId), <span style="color: #2b91af;">Times</span>.Once()); }</pre> </p> <p> Notice the addition of the <code>Times.Once()</code> clause in the last line. This instructs Moq that <code>Read(userId)</code> can only be invoked once, and that an exception should be thrown if it's called more than once. Surprisingly, the Devil's Advocate implementation still passes all tests: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">User</span> GetUser(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository.Read(userId); &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.userRepository.Read(0); }</pre> </p> <p> While this may seem surprising, the reason it passes is that the assertion only states that <code>Read(userId)</code> should be invoked exactly once; it doesn't state anything about <code>Read(0)</code>, as long as <code>userId</code> isn't 0. </p> <p> Second, you attempt to resolve <em>that</em> problem by stating that no matter the input, the Read method must be invoked exactly once: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(1234)] [<span style="color: #2b91af;">InlineData</span>(9876)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetUserCallsRepositoryWithCorrectValue(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IUserRepository</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SomeController</span>(td.Object); &nbsp; &nbsp;&nbsp;&nbsp; sut.GetUser(userId); &nbsp; &nbsp;&nbsp;&nbsp; td.Verify(r =&gt; r.Read(<span style="color: #2b91af;">It</span>.IsAny&lt;<span style="color: blue;">int</span>&gt;()), <span style="color: #2b91af;">Times</span>.Once()); }</pre> </p> <p> Notice that the input constraint is loosened to <code>It.IsAny&lt;int&gt;()</code>; combined with <code>Times.Once()</code>, it should ensure that the Read method is invoked exactly once. It does, but the devil is still mocking you (pun intended): </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">User</span> GetUser(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.userRepository.Read(0); }</pre> </p> <p> This is the first degenerate implementation I added, so now you're back where you began: it passes all tests. The new test case (that uses a Mock against a Query) has added no value at all. </p> <p> Third, in utter desperation, you turn to Strict Mocks: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(1234)] [<span style="color: #2b91af;">InlineData</span>(9876)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetUserCallsRepositoryWithCorrectValue(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IUserRepository</span>&gt;(<span style="color: #2b91af;">MockBehavior</span>.Strict); &nbsp;&nbsp;&nbsp; td.Setup(r =&gt; r.Read(userId)).Returns(<span style="color: blue;">new</span> <span style="color: #2b91af;">User</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SomeController</span>(td.Object); &nbsp; &nbsp;&nbsp;&nbsp; sut.GetUser(userId); &nbsp; &nbsp;&nbsp;&nbsp; td.Verify(); }</pre> </p> <p> Notice the use of <code>MockBehavior.Strict</code> in the Mock constructor, as well as the explicit Setup in the <a href="http://xunitpatterns.com/fixture%20setup.html">Fixture Setup</a> phase. </p> <p> Finally, this reigns in the devil; I have no other recourse than to implement the Read method correctly: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">User</span> GetUser(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.userRepository.Read(userId); }</pre> </p> <p> If the Devil's Advocate technique indicates that a faulty implementation implies a bad test suite, then a correct implementation must indicate a good test suite, right? </p> <p> Not quite, because the use of a Strict Mock makes your tests much less maintainable. You should already know this, but I'll show you an example. </p> <p> Assume that you have a rule that if the User returned by the Read method has an ID of 0, it means that the user doesn't exist, and should be created. (There are various problems with this design, most notably that it violates CQS, but that's another story...) </p> <p> In order to verify that a non-existing User is created during reading, you add this unit test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> UserIsSavedIfItDoesNotExist() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IUserRepository</span>&gt;(); &nbsp;&nbsp;&nbsp; td.Setup(r =&gt; r.Read(1234)).Returns(<span style="color: blue;">new</span> <span style="color: #2b91af;">User</span> { Id = 0 }); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SomeController</span>(td.Object); &nbsp; &nbsp;&nbsp;&nbsp; sut.GetUser(1234); &nbsp; &nbsp;&nbsp;&nbsp; td.Verify(r =&gt; r.Create(1234)); }</pre> </p> <p> This is a fine test that verifies a Command (Create) with a Mock. This implementation passes the test: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">User</span> GetUser(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> u = <span style="color: blue;">this</span>.userRepository.Read(userId); &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (u.Id == 0) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository.Create(1234); &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> u; }</pre> </p> <p> Alas! Although this implementation passes the new test, it <em>breaks an existing test!</em> Can you guess which one? Yes: the test that verifies a Query with a Strict Mock. It breaks because it explicitly states that the only method call allowed on the Repository is the Read method. </p> <p> You can resolve the problem by editing the test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(1234)] [<span style="color: #2b91af;">InlineData</span>(9876)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetUserCallsRepositoryWithCorrectValue(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IUserRepository</span>&gt;(<span style="color: #2b91af;">MockBehavior</span>.Strict); &nbsp;&nbsp;&nbsp; td.Setup(r =&gt; r.Read(userId)).Returns(<span style="color: blue;">new</span> <span style="color: #2b91af;">User</span> { Id = userId }); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SomeController</span>(td.Object); &nbsp; &nbsp;&nbsp;&nbsp; sut.GetUser(userId); &nbsp; &nbsp;&nbsp;&nbsp; td.Verify(); }</pre> </p> <p> Notice that <code>userId</code> was added to the returned User in the Setup for the Strict Mock. </p> <p> Having to edit existing tests is a genuine unit test smell. Not only does it add an unproductive overhead (imagine that many tests break instead of a single one), but it also <a href="/2013/04/02/why-trust-tests">decreases the trustworthiness of your test suite</a>. </p> <h3 id="c85896ee69734e69a99c78e2cb71f40c"> Data Flow Verification <a href="#c85896ee69734e69a99c78e2cb71f40c" title="permalink">#</a> </h3> <p> The solution is really simple: use a <em>conditional</em> Stub to verify the <em>data flow</em> through the <a href="http://xunitpatterns.com/SUT.html">SUT</a>. Here's the single test you need to arrive at a correct implementation: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(1234)] [<span style="color: #2b91af;">InlineData</span>(9876)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetUserReturnsCorrectValue(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">User</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IUserRepository</span>&gt;(); &nbsp;&nbsp;&nbsp; td.Setup(r =&gt; r.Read(userId)).Returns(expected); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SomeController</span>(td.Object); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.GetUser(userId); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(expected, actual); }</pre> </p> <p> Even when I attempt to use the Devil's Advocate technique, there's no way I can provide a faulty implementation. The method <em>must</em> invoke <code>Read(userId)</code>, because otherwise it can't return <code>expected</code>. If it invokes the Read method with any other value, it may get a User instance, but not <code>expected</code>. </p> <p> The <em>Data Flow Verification</em> test uses a (conditional) Stub to verify a Query. The distinction may seem subtle, but it's important. The Stub doesn't <em>require</em> that <code>Read(userId)</code> is invoked, but is configured in such a way that <em>if</em> (and only if) <code>Read(userId)</code> is invoked, it'll return <code>expected</code>. </p> <p> With the Stub verifying the Query (GetUserReturnsCorrectValue), and the other test that uses a Mock to verify a Command (UserIsSavedIfItDoesNotExist), the final implementation is: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">User</span> GetUser(<span style="color: blue;">int</span> userId) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> u = <span style="color: blue;">this</span>.userRepository.Read(userId); &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (u.Id == 0) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository.Create(1234); &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> u; }</pre> </p> <p> These two tests correctly specify the behaviour of the system in a terse and maintainable way. </p> <h3 id="9b2087f795dc443ebd1c81fafab954ca"> Conclusion <a href="#9b2087f795dc443ebd1c81fafab954ca" title="permalink">#</a> </h3> <p> <em>Data Flow Verification</em> is remarkably simple, so I'm continually taken aback that people seem to go out of their way to avoid it. Use it to verify Queries with Stubs, and keep the Mocks with the Commands. </p> <ins datetime="2023-08-14"><strong>P.S. 2023-08-14.</strong> See the article <a href="/2023/08/14/replacing-mock-and-stub-with-a-fake">Replacing Mock and Stub with a Fake</a> for a decade-later follow-up to this post.</ins> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c0b2e0bd500a4d5c8407c62bf11bff69"> <div class="comment-author"><a href="http://marcinjahn.com">Marcin Jahn</a> <a href="#c0b2e0bd500a4d5c8407c62bf11bff69">#</a></div> <div class="comment-content"> <p> Hi Mark, I wonder if the statement "Use Queries with Stubs" intentionally leaves out Fakes, or is it just a simplified view on the topic, and the statement should actually be "Use Queries with Stubs or Fakes"? </p> </div> <div class="comment-date">2023-04-23 8:59 UTC</div> </div> <div class="comment" id="3e74a22c05db4bbe83f863d9d017ff0b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3e74a22c05db4bbe83f863d9d017ff0b">#</a></div> <div class="comment-content"> <p> Marcin, thank you for writing. The beginning of this article starts by establishing that it uses the pattern language from <a href="/ref/xunit-patterns">xUnit Test Patterns</a>. In that language, a <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> is not the same as a <a href="http://xunitpatterns.com/Test%20Stub.html">Stub</a>. </p> <p> Depending on the interface, a Fake may implement methods that are both Commands and Queries, so when you use a Fake, you'll be using it for both. The article <a href="/2019/04/01/an-example-of-state-based-testing-in-c">An example of state-based testing in C#</a> shows an example of that. </p> <p> These days I rarely use Stubs and Mocks, instead <a href="/2019/02/18/from-interaction-based-to-state-based-testing">favouring state-based testing</a>. </p> </div> <div class="comment-date">2023-04-25 13:40 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Replace overloading with Discriminated Unions https://blog.ploeh.dk/2013/10/21/replace-overloading-with-discriminated-unions 2013-10-21T07:15:00+00:00 Mark Seemann <div id="post"> <p> <em>In F#, Discriminated Unions provide a good alternative to overloading.</em> </p> <p> When you're learning a new programming language, you may experience these phases: <ol> <li>[language x] is the coolest thing ever :D</li> <li>[language x] sucks, because it can't do [xyz], like [language y], which I know really well :(</li> <li>Now I finally understand that in [language x] I don't <em>need</em> [xyz], because it has different idioms o_O</li> </ol> Recently, I had this experience with F# and overloading. </p> <p> From C#, I'm used to method overloading as a design technique to provide an API with easy-to-learn default methods, and more complicated, but more flexible, methods - all in the same family of methods. Alas, in F#, function overloading isn't possible. (Well, <em>method</em> overloading is still possible, because you can create .NET classes in F#, but if you're creating a module with free-standing functions, you can't have two functions with the same name, but different parameters.) </p> <p> This really bothered me until I realized that F# has other language constructs that enable you to approach that problem differently. <a href="http://msdn.microsoft.com/en-us/library/dd233226.aspx">Discriminated Unions</a> is one such language construct. </p> <h3 id="4a181496a97d4a04ad20c74c2b1889e6"> Multiple related functions <a href="#4a181496a97d4a04ad20c74c2b1889e6" title="permalink">#</a> </h3> <p> Recently, I was working with a little module that enabled me to list a range of dates. As an example, I wanted to be able to list all dates in a given year, or all dates in a given month. </p> <p> My first attempt looked like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">module</span> Dates = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> InitInfinite (date : DateTime) = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; date |&gt; Seq.unfold (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> Some(d, d.AddDays 1.0)) &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> InYear year = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DateTime(year, 1, 1) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; InitInfinite &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.takeWhile (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.Year = year) &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> InMonth year month = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DateTime(year, month, 1) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; InitInfinite &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.takeWhile (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.Month = month)</pre> </p> <p> These functions did what I wanted them to do, but I found the names awkward. The InYear and InMonth functions are closely related, so I would have liked to call them both <em>In</em>, as I would have been able to do in C#. That would have enabled me to write code such as: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> actual = Dates.In year</pre> </p> <p> and </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> actual = Dates.In year month</pre> </p> <p> where <code>year</code> and <code>month</code> are integer values. Alas, that's not possible, so instead I had to settle for the slightly more clumsy InYear: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> actual = Dates.InYear year</pre> </p> <p> and InMonth: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> actual = Dates.InMonth year month</pre> </p> <p> That is, until I realized that I could model this better with a Discriminated Union. </p> <h3 id="a580e852d10944fd8c97ca77fffbcb9d"> One function <a href="#a580e852d10944fd8c97ca77fffbcb9d" title="permalink">#</a> </h3> <p> After a day or so, I had one of those small revelations described in <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>: implicitly, I was working with a concept of a <em>period</em>. Time to make the implicit concept explicit: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">type</span> Period = &nbsp;&nbsp;&nbsp; | Year <span style="color: blue;">of</span> int &nbsp;&nbsp;&nbsp; | Month <span style="color: blue;">of</span> int * int &nbsp; <span style="color: blue;">module</span> Dates = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> InitInfinite (date : DateTime) = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; date |&gt; Seq.unfold (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> Some(d, d.AddDays 1.0)) &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> <span style="color: blue;">private</span> InYear year = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DateTime(year, 1, 1) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; InitInfinite &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.takeWhile (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.Year = year) &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> <span style="color: blue;">private</span> InMonth year month = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DateTime(year, month, 1) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; InitInfinite &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.takeWhile (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.Month = month) &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> In period = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> period <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Year(y) <span style="color: blue;">-&gt;</span> InYear y &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Month(y, m) <span style="color: blue;">-&gt;</span> InMonth y m</pre> </p> <p> Notice that I defined a <em>Period</em> Discriminated Union outside the module, because it enables me to write client code like: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> actual = Dates.In(Year(year))</pre> </p> <p> and </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> actual = Dates.In(Month(year, month))</pre> </p> <p> This syntax requires slightly more characters than the previous alternative, but is (subjectively) more elegant. </p> <p> If you prefer, you can refactor the In function to: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> In = <span style="color: blue;">function</span> &nbsp;&nbsp;&nbsp; | Year(y) <span style="color: blue;">-&gt;</span> InYear y &nbsp;&nbsp;&nbsp; | Month(y, m) <span style="color: blue;">-&gt;</span> InMonth y m</pre> </p> <p> You may have noticed that this implementation still relies on the two private functions InYear and InMonth, but it's easy to refactor the In function to a single function: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> In period = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> generate dt predicate = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dt |&gt; InitInfinite |&gt; Seq.takeWhile predicate &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> period <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; | Year(y) <span style="color: blue;">-&gt;</span> generate (DateTime(y, 1, 1)) (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.Year = y) &nbsp;&nbsp;&nbsp; | Month(y, m) <span style="color: blue;">-&gt;</span> generate (DateTime(y, m, 1)) (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.Month = m)</pre> </p> <p> As you can see, the introduction of a Period Discriminated Union enabled me to express the API in a way that closely resembles what I originally envisioned. </p> <h3 id="57ba069992184951b9d72c11a52df34e"> A richer API <a href="#57ba069992184951b9d72c11a52df34e" title="permalink">#</a> </h3> <p> Once I made the change to a Discriminated Union, I discovered that I could make my API richer. Soon, I had this Dates module: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">type</span> Period = &nbsp;&nbsp;&nbsp; | Year <span style="color: blue;">of</span> int &nbsp;&nbsp;&nbsp; | Month <span style="color: blue;">of</span> int * int &nbsp;&nbsp;&nbsp; | Day <span style="color: blue;">of</span> int * int * int &nbsp; <span style="color: blue;">module</span> Dates = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> InitInfinite (date : DateTime) = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; date |&gt; Seq.unfold (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> Some(d, d.AddDays 1.0)) &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> In period = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> generate dt predicate = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dt |&gt; InitInfinite |&gt; Seq.takeWhile predicate &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> period <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Year(y) <span style="color: blue;">-&gt;</span> generate (DateTime(y, 1, 1)) (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.Year = y) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Month(y, m) <span style="color: blue;">-&gt;</span> generate (DateTime(y, m, 1)) (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.Month = m) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Day(y, m, d) <span style="color: blue;">-&gt;</span> DateTime(y, m, d) |&gt; Seq.singleton &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> BoundariesIn period = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> getBoundaries firstTick (forward : DateTime <span style="color: blue;">-&gt;</span> DateTime) = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> lastTick = forward(firstTick).AddTicks -1L &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (firstTick, lastTick) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> period <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Year(y) <span style="color: blue;">-&gt;</span> getBoundaries (DateTime(y, 1, 1)) (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.AddYears 1) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Month(y, m) <span style="color: blue;">-&gt;</span> getBoundaries (DateTime(y, m, 1)) (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.AddMonths 1) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Day(y, m, d) <span style="color: blue;">-&gt;</span> getBoundaries (DateTime(y, m, d)) (<span style="color: blue;">fun</span> d <span style="color: blue;">-&gt;</span> d.AddDays 1.0)</pre> </p> <p> Notice that I added a <code>Day</code> case. Originally, I didn't think it would be valuable, as <code>Dates.In(Day(year, month, day))</code> seemed like quite a convoluted way of saying <code>DateTime(year, month, day)</code>. However, it turned out that the <code>In</code> abstraction was valuable, also with a single day - simply because it's an abstraction. </p> <p> Additionally, I then discovered the utility of a function called <em>BoundariesIn</em>, which gives me the boundaries of a Period - that is, the very first and last tick of the Period. </p> <h3 id="4bbf37e7b7e04a88874092f8eb408913"> Summary <a href="#4bbf37e7b7e04a88874092f8eb408913" title="permalink">#</a> </h3> <p> It's easy to become frustrated while learning a new programming language. In F#, I was annoyed by the lack of function overloading, until I realized that a single function taking a Discriminated Union might actually be a richer idiom. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Easy ASP.NET Web API DTOs with F# CLIMutable records https://blog.ploeh.dk/2013/10/15/easy-aspnet-web-api-dtos-with-f-climutable-records 2013-10-15T22:01:00+00:00 Mark Seemann <div id="post"> <p> <em>With F#, it's easy to create DTOs for use with the ASP.NET Web API, using record types.</em> </p> <p> When writing HTTP (or RESTful) web services with the <a href="http://www.asp.net/web-api">ASP.NET Web API</a>, the most normal approach is to define <a href="http://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects</a> (DTOs), which represents the data structures that go on the wire in the form of JSON or XML. While I usually call these <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">boundary objects</a> for <em>renditions</em> (a bit punny), a more normal terminology is, indeed, DTOs. </p> <p> To enable the default .NET serializers (and particularly, <em>de</em>serializers) to do their magic, these DTOs must be mutable and have a default constructor. Not particularly something that seems to fit nicely with F#. </p> <p> Until recently, I've been declaring such DTOs <a href="/2013/08/23/how-to-create-a-pure-f-aspnet-web-api-project">like this</a> in F#: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">type</span> HomeRendition() = &nbsp;&nbsp;&nbsp; [&lt;DefaultValue&gt;] <span style="color: blue;">val</span> <span style="color: blue;">mutable</span> Message : string &nbsp;&nbsp;&nbsp; [&lt;DefaultValue&gt;] <span style="color: blue;">val</span> <span style="color: blue;">mutable</span> Time : string</pre> </p> <p> However, then I discovered the <a href="http://msdn.microsoft.com/en-us/library/hh289724.aspx">[&lt;CLIMutable&gt;] attribute</a>. This effectively turn a standard F# record type into something that can be serialized with the normal .NET serializers. This means that it's possible to redefine the above HomeRendition like this: </p> <p> <pre style="margin: 0px;">[&lt;CLIMutable&gt;] <span style="color: blue;">type</span> HomeRendition = { &nbsp;&nbsp;&nbsp; Message : string &nbsp;&nbsp;&nbsp; Time : string }</pre> </p> <p> This is much better, because this looks like a proper immutable record type from the perspective of the F# compiler. However, from a CLI perspective, the HomeRendition class has a default constructor, and mutable properties. </p> <p> A DTO defined like this still works with the ASP.NET Web API, although by default, the serialization looks a bit strange: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">HomeRendition</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">Message_x0040_</span><span style="color: blue;">&gt;</span> &nbsp; &nbsp; This message is served by a pure F# ASP.NET Web API implementation. <span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">Message_x0040_</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">Time_x0040_</span><span style="color: blue;">&gt;</span>2013-10-15T23:32:42.6725088+02:00<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Time_x0040_</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">HomeRendition</span><span style="color: blue;">&gt;</span></pre> </p> <p> The reason for that is that the [&lt;CLIMutable&gt;] attribute causes the record type to be compile with auto-generated internal mutable fields, and these are named by appending an @ character - in this case, the field names become <code>Message@</code> and <code>Time@</code>. Since the unicode value for the @ character is x0040, these field names become <code>Message_x0040_</code> and <code>Time_x0040_</code>. </p> <p> Wait a minute! Did I say <em>internal fields?</em> Yes, I did. Then why are the internal fields being serialized instead of the public properties? Well, what can I say, other than the DataContractSerializer once again proves to be a rather poor choice of default serializer. Yet another reason to use a sane XML serializer. </p> <p> No doubt, one of my readers can come up with a good solution for the DataContractSerializer too, but since I always switch my Web API services to use a proper XML serializer, I don't really care: </p> <p> <pre style="margin: 0px;">GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer &lt;- <span style="color: blue;">true</span></pre> </p> <p> Now all is well again: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">HomeRendition</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">Message</span><span style="color: blue;">&gt;</span> &nbsp; &nbsp; This message is served by a pure F# ASP.NET Web API implementation. <span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">Message</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">Time</span><span style="color: blue;">&gt;</span>2013-10-15T23:50:06.9025322+02:00<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Time</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">HomeRendition</span><span style="color: blue;">&gt;</span></pre> </p> <p> That's it: much easier, and more robust, Web API DTOs with F# record types. Just apply the [&lt;CLIMutable&gt;] attribute. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="842a1d035fdb4a0fb455a9556de3ed2c"> <div class="comment-author"> <a href="http://blog.patrickmcdonald.org">Patrick McDonald</a> <a href="#842a1d035fdb4a0fb455a9556de3ed2c">#</a></div> <div class="comment-content"> <p> Hi Mark, thanks for this, I blogged about serialization of record types <a href="http://blog.patrickmcdonald.org/2012/10/f-record-serialization-in-asp-net-web-api/">last year</a>. the only way I could get XML serialization to work nicely with the DataContractSerializer was to put a DataMember attribute on each field of the record and a DataContract attribute on the record itself. </p> <p> JSON serialization can be handled nicely by default in WEP API however by annotating the class with JsonObject(MemberSerialization=MemberSerialization.OptOut). I have checked and by combining our 2 methods the WEB API can serialize nicely as both JSON and XML. </p> </div> <div class="comment-date">2013-10-16 12:54 UTC</div> </div> <div class="comment" id="1f2ac58745794d4f8a163806411d4eef"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1f2ac58745794d4f8a163806411d4eef">#</a></div> <div class="comment-content"> <p> Hi Patrick, thank you for writing. For JSON serialization, I usually just add this configuration: </p> <p> <div style="font-family: Consolas; font-size: 10pt; color: black;"> <pre style="margin: 0px;">config.Formatters.JsonFormatter.SerializerSettings.ContractResolver &lt;- &nbsp;&nbsp;&nbsp; Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()</pre> </div> </p> <p> This not only gives the JSON idiomatic casing, but also renders CLIMutable F# records nicely. </p> </div> <div class="comment-date">2013-10-16 19:38 UTC</div> </div> <div class="comment" id="09c8130f50aa40afaccbf93a78bf3bc0"> <div class="comment-author"> <a href="http://evolutionarydeveloper.blogspot.co.uk/">Nick Blair</a> <a href="#09c8130f50aa40afaccbf93a78bf3bc0">#</a></div> <div class="comment-content"> <p> Any thoughts on trying to return a Discriminated Union with an F# WebAPI controller? I notice that asp.net throws a runtime InvalidOperationException when you try to do this. Furthermore, the CLIMutable Attribute isn't even allowed on a DU. I find this counter intuitve, however, as you can work with F# DUs in C# code and determine the type using the Tag propery that is visible only in C#/VB.net. Why can't this Tag property be serialized and returned as part of a DU DTO? </p> </div> <div class="comment-date">2013-12-27 20:10 UTC</div> </div> <div class="comment" id="097f384c35274adb8a1f70abfa943be1"> <div class="comment-author"> <a href="">Mark Seemann</a> <a href="#097f384c35274adb8a1f70abfa943be1">#</a></div> <div class="comment-content"> <p> Nick, I haven't tried returning a Discriminated Union from a Web API Controller. Why would I want to do that? As I've previously described, <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">at the boundaries, applications aren't object-oriented</a>, and similarly, they aren't functional either. What would it mean to return a Discriminated Union? How would it render as JSON or XML? </p> <p> Apart from that, I have no idea how a Discriminated Union compiles to IL... </p> </div> <div class="comment-date">2013-12-29 9:07 UTC</div> </div> <div class="comment" id="804948cd285443348eaa77ae4d6d675d"> <div class="comment-author"> <a href="https://blog.markvincze.com/">Mark Vincze</a> <a href="#804948cd285443348eaa77ae4d6d675d">#</a></div> <div class="comment-content"> <p> Hey Mark, It's interesting that Json serialization works even without `CLIMutable`, both when reading or writing data. </p> <p> For example this works properly: </p> <pre>type HomeRecord = { Message : string Time : string } type InputModel = { Message : string } type HomeController() = inherit ApiController() member this.Get() = this.Ok({ Message = "Hello from F#!"; Time = DateTime.Now.ToString() }) member this.Post(input : InputModel) = this.Ok(input.Message)</pre> <p> But only for the Json serializer, the XML one does not work without `CLIMutable`. Do you know what makes the Json serialization work? Is there special support implemented in the Json serializer for the immutable F# record types? </p> <p> Thanks, Mark </p> </div> <div class="comment-date">2017-02-04 17:00 UTC</div> </div> <div class="comment" id="3ac5c3042972439995171f031f73d902"> <div class="comment-author"> <a href="">Mark Seemann</a> <a href="#3ac5c3042972439995171f031f73d902">#</a></div> <div class="comment-content"> <p> Mark, thank you for writing. ASP.NET Web API uses <a href="http://www.newtonsoft.com/json">JSON.NET</a> for JSON serialization, and, as far as know, it has built-in F# support. It can also serialize discriminated unions. </p> <p> The .NET XML serializer doesn't have built-in F# support. </p> </div> <div class="comment-date">2017-02-04 18:07 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Verifying every single commit in a Git branch https://blog.ploeh.dk/2013/10/07/verifying-every-single-commit-in-a-git-branch 2013-10-07T08:38:00+00:00 Mark Seemann <div id="post"> <p> <em>You can run a validation task for every commit in a Git branch in order to verify the integrity of a Git branch's history.</em> </p> <p> Just like the Soviet Union, I occasionally prefer to rewrite history... although I limit myself to rewriting the history of one or more Git branches. When I do that, I'd like to verify that I didn't break anything after an interactive rebase. It actually turns out that this can easily happen. Some commits are left in a state where they either don't compile, or tests fail. Git is happy, but the code isn't. To deal with such situations, I wanted a tool that could verify each and every commit in a branch like this: <ol> <li>Check out a commit.</li> <li>Compile the code.</li> <li>Run all tests.</li> <li>Repeat for the next commit, or stop if there's a failure.</li> </ol> The tool should do this from the first to the last commit in my current branch (e.g. <em>master</em>). </p> <h3 id="0a38c1d3abce498cbc19102e94aab8dc"> Motivation <a href="#0a38c1d3abce498cbc19102e94aab8dc" title="permalink">#</a> </h3> <p> If you don't care about my motivation for doing this, you can skip this section and move on to the solution. However, some people might like to point out to me that I shouldn't rewrite the history of my Git repositories - and particularly not of the <em>master</em> branch. I agree, for all repositories where I actually collaborate with other programmers. </p> <p> However, I also use Git locally to produce teaching materials. As an example, if you download the example code for <a href="https://blog.ploeh.dk/pluralsight-courses">my Pluralsight courses</a>, you'll see that it's all available as Git repositories. I think that's an added benefit for the student, because not only can you see the final result, but you can also see all the steps that lead me there. I even occasionally write commit messages longer than a single line, explaining my thinking as I check in code. </p> <p> Like everyone else, I'm fallible, so often, when I prepare such materials, I end up taking some detours that I feel will confuse students more than it will help. Thus, I have a need to be able to edit my Git repositories. Right now, as an example, I'm working with a repository with 120 commits on the <em>master</em> branch, and I need to make some changes in the beginning of the history. </p> <h3 id="561a9ae0504c4b839aa6df7b507328ae"> Solution <a href="#561a9ae0504c4b839aa6df7b507328ae" title="permalink">#</a> </h3> <p> With <em>much</em> help from <a href="https://github.com/zeroem">Darrell Hamilton</a>, who ended up providing <a href="https://gist.github.com/zeroem/6822534">this gist</a>, I was able to piece together this bash script: </p> <p> <pre>#!/bin/sh COMMITS=$(git --git-dir=BookingApi/.git --work-tree=BookingApi log --oneline --reverse | cut -d " " -f 1) CODE=0 git --git-dir=BookingApi/.git --work-tree=BookingApi reset --hard git --git-dir=BookingApi/.git --work-tree=BookingApi clean -xdf for COMMIT in $COMMITS do git --git-dir=BookingApi/.git --work-tree=BookingApi checkout $COMMIT # run-tests ./build.sh if [ $? -eq 0 ] then echo $COMMIT - passed else echo $COMMIT - failed exit fi git --git-dir=BookingApi/.git --work-tree=BookingApi reset --hard git --git-dir=BookingApi/.git --work-tree=BookingApi clean -xdf done git --git-dir=BookingApi/.git --work-tree=BookingApi checkout master</pre> </p> <p> As you can tell, it follows the algorithm outlined above. First, it does a <code>git log</code> to get all the commits in the branch, and then it loops through all of them, one by one. For each commit, it compiles and runs all tests by calling out to a separate <em>build.sh</em> script. If the build and test step succeeds, it cleans up the working directory and moves on to the next step. If the verification step fails, it stops, so that I can examine the problem. </p> <p> (The reason for the use of <code>--git-dir</code> and <code>--work-tree</code> is that I need to run the script from outside the Git repository itself; otherwise, the <code>git clean -xdf</code> step would delete the script files!) </p> <p> This has turned out to work beautifully, and has already caught quite a number of commits that Git could happily rebase, but afterwards either couldn't compile, or had failing tests. </p> <p> Running those tests in a tight loop has also finally provided some work for my multi-core processor: </p> <p> <img src="/content/binary/all-cpus-busy-while-checking-git-branch-history.png"> </p> <p> Each run through those 120 commits takes about 20 minutes, so, even though today we have fast compilers, once again, we have <a href="http://xkcd.com/303">an excuse for slacking off</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c0c585e879284ecfb17ef2f3777c84ae"> <div class="comment-author">Steven King <a href="#c0c585e879284ecfb17ef2f3777c84ae">#</a></div> <div class="comment-content"> Hi Mark,<br><br> A talk on some of Git's lesser known features that I recently saw suggested that there is a built-in mechanism for what you are trying to achieve. The <code>git bisect</code> command allows you to run a script for each of a number of commits. The exact details on how to perform this are provided in <a href="http://git-scm.com/book/en/Git-Tools-Debugging-with-Git">Pro Git</a>, so I won't repeat them here.<br><br> Thanks,<br> Steven </div> <div class="comment-date">2013-10-11 12:04 UTC</div> </div> <div class="comment" id="e3782c70dd0c4d369a1ae7a2910ac9b7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e3782c70dd0c4d369a1ae7a2910ac9b7">#</a></div> <div class="comment-content"> <p> Hi Steven </p> <p> Thank you for writing. As far as I can tell from the documentation, <code>git bisect</code> doesn't quite do what I'd like to do. The <em>bisect</em> feature basically performs a search through the repository to figure out which commit introduces a particular error. The search algorithm is attempting to be as efficient as possible, so it keeps bisecting the list of commits to figure out on which side the error occurs - hence the name. Thus, it very explicitly doesn't check each and every commit. </p> <p> My scenario is different. While I'm not looking for any particular error, I just want to verify that <em>every</em> commit is OK. </p> </div> <div class="comment-date">2013-10-11 12:56 UTC</div> </div> <div class="comment" id="21cefadbbd604727a15fa3a016d213d2"> <div class="comment-author"><a href="http://feedmechocolate.com/">Chris Mear</a> <a href="#21cefadbbd604727a15fa3a016d213d2">#</a></div> <div class="comment-content"> <p> As of <a href="https://github.com/git/git/blob/master/Documentation/RelNotes/1.7.12.txt#L62">Git 1.7.12</a>, the interactive rebase command can take an <code>--exec &lt;cmd&gt;</code> option, which will make it execute <code>&lt;cmd&gt;</code> after each commit. If the command fails, the interactive rebase will stop so you can fix the problem. </p> </div> <div class="comment-date">2014-04-12 09:36 UTC</div> </div> <div class="comment" id="f25826f2d5fa4c6595766a518eae2ad4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f25826f2d5fa4c6595766a518eae2ad4">#</a></div> <div class="comment-content"> <p> I didn't know about the <code>--exec</code> option, but I'll try it out the next time I perform a rebase. Thank you for the tip. </p> </div> <div class="comment-date">2014-04-12 11:29 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. DI in .NET receives a Jolt Productivity Award https://blog.ploeh.dk/2013/10/02/di-in-net-receives-a-jolt-productivity-award 2013-10-02T14:19:00+00:00 Mark Seemann <div id="post"> <p> <em>My book </em>Dependency Injection in .NET<em> has received a Jolt Productivity Award!</em> </p> <p> It turns out that my book <a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a> has received a <a href="http://www.drdobbs.com/joltawards/jolt-awards-the-best-books/240162065?pgno=5">Jolt Productivity Award 2013</a> in the <a href="http://www.drdobbs.com/joltawards/jolt-awards-the-best-books/240162065?pgno=1">Best Books category</a>. This is a completely unexpected (but obviously very pleasant) surprise :D </p> <p> <img src="/content/binary/seemann_cover150.jpg"> </p> <p> The motivation for giving the award states that it: </p> <p> <blockquote> "provide[s] a brilliant and remarkably readable explanation of DI" </blockquote> </p> <p> and concludes that </p> <p> <blockquote> "It's the definitive guide to DI and the first place architects and implementers should turn for a truly deep and complete understanding of the pattern — regardless of whether .NET is part of their toolkit."" </blockquote> </p> <p> Although it's already been selling well, the book is still available either <a href="http://www.manning.com/seemann">directly from Manning</a>, or via <a href="http://amzn.to/12p90MG">Amazon.com</a> - and, in case you didn't know, every time you buy a Manning book, you automatically also get the electronic version, which (in the case of my book), includes a Kindle version, as well as a PDF. You can also skip the physical copy and buy only the electronic version, but this option is (AFAIK) only available <a href="http://www.manning.com/seemann">from Manning</a>. </p> <p> What an honor! </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Karma from bash on Windows https://blog.ploeh.dk/2013/09/13/karma-from-bash-on-windows 2013-09-13T07:02:00+00:00 Mark Seemann <div id="post"> <p> <em>Note to future self: how I got Karma running from bash in Windows.</em> </p> <p> Yesterday, I spent quite a bit of time getting <a href="http://karma-runner.github.io">Karma</a> to run from bash on my Windows 7 (x64) laptop. Since, a month from now, I'll have absolutely no recollection of how I achieved this, I'm writing it down here for the benefit of my future self, as well as any other people who may find this valuable. </p> <h3 id="9920c4962cc8416ea025c7f378d9aa2c"> Add karma to PATH <a href="#9920c4962cc8416ea025c7f378d9aa2c" title="permalink">#</a> </h3> <p> My first problem was that 'karma' wasn't a recognized command. Although I'd used <a href="http://chocolatey.org">Chocolatey</a> to install <a href="http://nodejs.org">node.js</a>, used <a href="https://npmjs.org">npm</a> to install Karma, and 'karma' was a recognized command from PowerShell, bash didn't recognize it. While I don't yet know if the following is the 100 % correct solution, I managed to make bash recognize karma by adding my karma directory to my bash path: </p> <p> <pre>PATH=$PATH:~/appdata/roaming/npm/node_modules/karma/bin</pre> </p> <p> This works in the session itself, but will be forgotten by bash unless you add it to ~/.bash_profile. BTW, that file doesn't exist by default in a new Git Bash installation on Windows, so you'll have to add it yourself. </p> <h3 id="b0b3c667792b4154acd6782874ee1552"> Chrome path <a href="#b0b3c667792b4154acd6782874ee1552" title="permalink">#</a> </h3> <p> My next problem was that, while karma could be found, I got the error message that it couldn't find Chrome, and that I should set the CHROME_BIN environment variable. After much experimentation, I managed to make it work by setting: </p> <p> <pre>CHROME_BIN=/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe export CHROME_BIN</pre> </p> <p> That looks really simple, so why did I waste so much time shaving that yak? Well, setting the environment variable was one thing, but it didn't work before I also figured out to <code>export</code> it. </p> <p> Once again, I added these lines to my .bash_profile file. Now I can run Karma from bash on Windows. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. DI and events: Composition https://blog.ploeh.dk/2013/09/11/di-and-events-composition 2013-09-11T09:35:00+00:00 Mark Seemann <div id="post"> <p> <em>With Reactive Extensions, and a bit of composition, you can publish and subscribe to events in a structurally safe way.</em> </p> <p> Previously, in my <a href="/2013/09/06/dependency-injection-and-events">series about Dependency Injection and events</a>, you learned <a href="/2013/09/11/di-and-events-reactive-extensions">how to connect a publisher and a subscriber</a> via a third party (often the <a href="/2011/07/28/CompositionRoot">Composition Root</a>). </p> <p> The problem with that approach is that while it's loosely coupled, it's too easy to forget to connect the publisher and the subscriber. It's also possible to forget to unsubscribe. In neither case can the compiler help you. </p> <p> However, the advantage of using <a href="https://rx.codeplex.com">Reactive Extensions</a> over .NET events is that IObserver&lt;T&gt; is <em>composable</em>. That turns out to be quite an important distinction! </p> <h3 id="fe3a00a3433445af9badf61c008052bc"> The problem with IObservable&lt;T&gt; <a href="#fe3a00a3433445af9badf61c008052bc" title="permalink">#</a> </h3> <p> While I consider IObserver&lt;T&gt; to be an extremely versatile interface, I consider IObservable&lt;T&gt; to be of limited usefulness. Consider its definition: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IObservable</span>&lt;<span style="color: blue;">out</span> T&gt; { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IDisposable</span> Subscribe(<span style="color: #2b91af;">IObserver</span>&lt;T&gt; observer); }</pre> </p> <p> The idea is that the publisher (the <em>Observable</em>) receives a subscriber (the <em>Observer</em>) via Method Injection. When the method completes, the subscriber is considered subscribed to the publisher's events, until the subscriber disposes of the returned subscription reference. </p> <p> There's a couple of problems with this design: <ul> <li>It's too easy to forget to invoke the Subscribe method. This is not a problem if you're writing a system in which publishers dynamically subscribe to event streams, but it's problematic if your system <em>relies</em> on certain publishers and subscribers to be connected.</li> <li>It implies mutation in the publisher, because the publisher must somehow keep a list of all its subscribers.</li> <li>It breaks <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> (CQS).</li> <li>Since it implies mutation, it's not thread-safe by default.</li> </ul> Fortunately, it's possible to work with IObserver&lt;T&gt; while completely ignoring IObservable&lt;T&gt;. </p> <h3 id="9fa03700423446339f1829c21925d829"> From Method Injection to Constructor Injection <a href="#9fa03700423446339f1829c21925d829" title="permalink">#</a> </h3> <p> As you learned in the <a href="/2013/09/11/di-and-events-reactive-extensions">last</a> <a href="/2013/09/08/di-and-events-third-party-connect">couple of articles</a>, the subscriber should <em>not</em> require any dependency in order to react to events. Yet, if Method Injection via IObservable&lt;T&gt; isn't a good approach either, then what's left? </p> <p> Good old Constructor Injection. </p> <p> The important realization is that it's not the <em>subscriber</em> (NeedyClass, in previous examples) that requires a dependency - it's the <em>publisher!</em> </p> <p> Imagine that until now, you've had a publisher implementing IObservable&lt;T&gt;. In keeping with the running example throughout <a href="/2013/09/06/dependency-injection-and-events">this series</a>, this was the publisher that originally implemented IDependency. Thus, it's still called RealDependency. For simplicity's sake, assume that its implementation is as simple as this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">RealDependency</span> : <span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">Subject</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; subject; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> RealDependency() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subject = <span style="color: blue;">new</span> <span style="color: #2b91af;">Subject</span>&lt;<span style="color: #2b91af;">Unit</span>&gt;(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> MakeItHappen() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subject.OnNext(<span style="color: #2b91af;">Unit</span>.Default); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IDisposable</span> Subscribe(<span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; observer) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.subject.Subscribe(observer); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> What if, instead of implementing IObservable&lt;Unit&gt;, this class would use Constructor Injection to request an IObserver&lt;Unit&gt;? Then it would look like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">RealDependency</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; observer; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> RealDependency(<span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; observer) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.observer = observer; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> MakeItHappen() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.observer.OnNext(<span style="color: #2b91af;">Unit</span>.Default); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> That's much simpler, and you just got rid of an entire type (IObservable&lt;Unit&gt;)! Even better, you've also eliminated all use of IDisposable. Oh, and it also conforms to CQS, and is thread-safe. </p> <h3 id="50871367ca564e6cb2095246d38eb4c0"> Connection <a href="#50871367ca564e6cb2095246d38eb4c0" title="permalink">#</a> </h3> <p> The names of the concrete classes are completely off by now, but you can connect publisher (RealDependency) with its subscriber (NeedyClass) from a third party (Composition Root): </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> subscriber = <span style="color: blue;">new</span> <span style="color: #2b91af;">NeedyClass</span>(); <span style="color: blue;">var</span> publisher = <span style="color: blue;">new</span> <span style="color: #2b91af;">RealDependency</span>(subscriber);</pre> </p> <p> Not only is this easy, the statically typed structure of both classes helps you do the right thing: the compiler will issue an error if you don't supply a subscriber to the publisher. </p> <p> But wait, you say: now the publisher is <em>forced</em> to have a single observer. Isn't the whole idea about publish/subscribe that you can have an arbitrary number of subscribers for a given publisher? Yes, it is, and that's still possible. </p> <h3 id="2f6e7c56760144bda7de78c02fcab14c"> Composition <a href="#2f6e7c56760144bda7de78c02fcab14c" title="permalink">#</a> </h3> <p> More than a single subscriber is easy if you introduce a <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composite</a>: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CompositeObserver</span>&lt;T&gt; : <span style="color: #2b91af;">IObserver</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IObserver</span>&lt;T&gt;&gt; observers; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> CompositeObserver(<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IObserver</span>&lt;T&gt;&gt; observers) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.observers = observers; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnCompleted() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> observer <span style="color: blue;">in</span> <span style="color: blue;">this</span>.observers) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; observer.OnCompleted(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnError(<span style="color: #2b91af;">Exception</span> error) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> observer <span style="color: blue;">in</span> <span style="color: blue;">this</span>.observers) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; observer.OnError(error); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnNext(T value) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> observer <span style="color: blue;">in</span> <span style="color: blue;">this</span>.observers) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; observer.OnNext(value); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> While it looks like a bit of work, this class is <em>so reusable</em> that I wonder why it's not included in the Rx library itself... It enables you to subscribe any number of subscribers to the publisher, e.g. two: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> sub1 = <span style="color: blue;">new</span> <span style="color: #2b91af;">NeedyClass</span>(); <span style="color: blue;">var</span> sub2 = <span style="color: blue;">new</span> <span style="color: #2b91af;">AnotherObserver</span>(); <span style="color: blue;">var</span> publisher = &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">RealDependency</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">CompositeObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt;[] { sub1, sub2 }));</pre> </p> <p> I'll leave it as an exercise to the reader to figure out how to implement the scenario with <em>no</em> subscribers :) </p> <h3 id="55de9bd2dc024d28bf7c9020f2e4ee37"> Conclusion <a href="#55de9bd2dc024d28bf7c9020f2e4ee37" title="permalink">#</a> </h3> <p> Sticking to IObserver&lt;T&gt; and simply injecting it into the publishers is much simpler than any other alternative I've described so far. Nonetheless, keep in mind that the reason this simplification works so well is because it assumes that you know all subscribers when you compose your object graph. </p> <p> There's a reason the IObservable&lt;T&gt; interface exists, and that's to support scenarios where publishers and subscribers come and go during the lifetime of an application. The simplification described here doesn't handle that scenario, but if you don't need that flexibility, you can greatly simplify your eventing infrastructure by disposing of IObservable&lt;T&gt; ;) </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ed1133e6938d4712878614e5a6c13d6b"> <div class="comment-author"><a href="http://msmvps.com/blogs/calinoiu/">Nicole Calinoiu</a> <a href="#ed1133e6938d4712878614e5a6c13d6b">#</a></div> <div class="comment-content"> This also happens to be a very clean solution for avoiding leaking a partially constructed instance in multi-threaded scenarios (see, for example, the &quot;Initialization safety risks&quot; section at <a href="http://www.ibm.com/developerworks/java/library/j-jtp07265/index.html">http://www.ibm.com/developerworks/java/library/j-jtp07265/index.html</a>, which applies equally to C#). The only alternative to passing fully constructed listeners to the publisher is to use post-construction method invocation (<i>&agrave; la</i> IStartable) for registration, which is much less elegant.</div> <div class="comment-date">2014-01-08 16:00 UTC</div> </div> <div class="comment" id="be7a1ab5ae8c4ec4af271dc96365dc6e"> <div class="comment-author">Tony Johnson <a href="#be7a1ab5ae8c4ec4af271dc96365dc6e">#</a></div> <div class="comment-content"> Isn't there a big downside here that you lose the ability to use all of the rx extension methods since they all apply to to IObservable instead of IObserver? Would you end up re-implementing them from an IObserver viewpoint?</div> <div class="comment-date">2015-11-06 14:00 UTC</div> </div> <div class="comment" id="4ac86a36d46e44aab25622fd6e9b732c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4ac86a36d46e44aab25622fd6e9b732c">#</a></div> <div class="comment-content"> <p> Tony, thank you for writing. That's a good observation, and you're right: if you need to make substantial transformation, filtering, aggregation, and so on, on your event streams, then it would be valuable to be able to leverage Reactive Extensions. Not being able to do that would be a loss. </p> <p> Using IObserver, as I suggest here, does come with that disadvantage, so as always, it's important to weigh the advantages and disadvantages against each other. If you're doing lots of event stream processing, then it would most likely be best to go with idiomatic Reactive Extensions (and not the solution proposed in this article). If, on the other hand, you mostly need to make sure that some Command is executed once and only once (or at least once, depending on your delivery guarantees), then my proposed solution may be more appropriate. </p> <p> At its heart, the choice is between pub/sub systems on one side, and point-to-point systems on the other side. If it's important that exactly one destination system receives the messages, it's a point-to-point channel in action. If, on the other hand, zero to any arbitrary number of subscribers are welcome to consume the messages, it's a pub/sub system. </p> </div> <div class="comment-date">2015-11-07 12:14 UTC</div> </div> <div class="comment" id="0b0d07bf102d490bb01781bb328bcc1a"> <div class="comment-author">SpencerJB <a href="#0b0d07bf102d490bb01781bb328bcc1a">#</a></div> <div class="comment-content"> <p>I have briefly experimenting with this idea over the last couple of days.</p> <p>In my experiemnt I found that rather than implement the IObserver interface on my subscribers it was easier to use Observer.Create and pass in an the Action I wanted to call to that.</p> <p>This has left me wondering wether I could dispense with the whole IObserver interfacea and simply pass the Action that I wanted to call into the publisher.</p> </div> <div class="comment-date">2016-06-09 12:58 UTC</div> </div> <div class="comment" id="2546a0c837bb4872806b8dc96877f1c3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2546a0c837bb4872806b8dc96877f1c3">#</a></div> <div class="comment-content"> <p> Spencer, thank you for writing. Indeed, <a href="/2009/05/28/DelegatesAreAnonymousInterfaces">delegates are equivalent to single-method interfaces</a>. Once you realise that, <a href="/2014/03/10/solid-the-next-step-is-functional">functional programming begins to look more and more attractive</a>. </p> <p> Actions, with their void return type, don't seem particularly functional, however, but <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">you can build up an entire architecture on that concept</a>. </p> <p> Personally, in C#, I prefer to stick with interfaces for dependency injection, since I find it more <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a>, but other people have different opinions on that. </p> </div> <div class="comment-date">2016-06-09 16:35 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. DI and events: Reactive Extensions https://blog.ploeh.dk/2013/09/11/di-and-events-reactive-extensions 2013-09-11T08:37:00+00:00 Mark Seemann <div id="post"> <p> <em>With Reactive Extensions, you can convert event subscription to something that looks more object-oriented, but all is still not quite as it should be...</em> </p> <p> In my <a href="/2013/09/06/dependency-injection-and-events">series about Dependency Injection and events</a>, you <a href="/2013/09/08/di-and-events-third-party-connect">previously saw how to let a third party connect publisher and subscriber</a>. I think that approach is valuable in all those cases where you connect publishers and subscribers in a narrow and well-defined area of your application, such as in the <a href="/2011/07/28/CompositionRoot">Composition Root</a>. However, it's not a good fit if you need to connect and disconnect publishers and subscribers throughout your application's code base. </p> <p> This article examines alternatives based on <a href="https://rx.codeplex.com/">Reactive Extensions</a>. </p> <h3 id="274db6c7b6d64ed797ac1c3090883bcd"> Re-design <a href="#274db6c7b6d64ed797ac1c3090883bcd" title="permalink">#</a> </h3> <p> For the rest of this article, I'm going to assume that you're in a position where you can change the design - particularly the design of the <a href="/2013/09/06/di-and-events-constructor-subscription">IDependency interface</a>. If not, you can always convert a standard .NET event into the appropriate IObservable&lt;T&gt;. </p> <p> Using small iterations, you can first make IDependency implement IObservable&lt;Unit&gt;: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IDependency</span> : <span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; { &nbsp;&nbsp;&nbsp; <span style="color: blue;">event</span> <span style="color: #2b91af;">EventHandler</span> ItHappened; }</pre> </p> <p> This enables you to change the implementation of NeedyClass to subscribe to IObservable&lt;Unit&gt; instead of the ItHappened event: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">NeedyClass</span> : <span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt;, <span style="color: #2b91af;">IDisposable</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IDisposable</span> subscription; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> NeedyClass(<span style="color: #2b91af;">IDependency</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (dependency == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"dependency"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subscription = dependency.Subscribe(<span style="color: blue;">this</span>); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnCompleted() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnError(<span style="color: #2b91af;">Exception</span> error) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnNext(<span style="color: #2b91af;">Unit</span> value) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Handle event here</span> &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Dispose() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subscription.Dispose(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Because IObservable&lt;T&gt;.Subscribe returns an IDisposable, NeedyClass still needs to store that object in a field and dipose of it when it's done, which also means that it must implement IDisposable itself. Thus, you seem to be no better off than with Constructor Subscription. Actually, you're slightly worse off, because now NeedyClass gained three new public methods (OnCompleted, OnError, and OnNext), compared to a single public method with Constructor Subscription. </p> <p> What's even worse is that you're still violating <a href="https://vuscode.wordpress.com/">Nikola Malovic</a>'s <a href="https://vuscode.wordpress.com/2009/10/16/inversion-of-control-single-responsibility-principle-and-nikola-s-laws-of-dependency-injection">4th law of IoC</a>: <a href="/2011/03/03/InjectionConstructorsshouldbesimple">Injection Constructors should perform no work</a>. </p> <p> This doesn't seem promising. </p> <h3 id="a736774e8cd2468c8392aa4cead52b90"> Simplification <a href="#a736774e8cd2468c8392aa4cead52b90" title="permalink">#</a> </h3> <p> While you seem to have gained little from introducing Reactive Extensions, at least you can simplify IDependency a bit. No classes should have to subscribe to the old-fashioned .NET event, so you can remove that from IDependency: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IDependency</span> : <span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; { }</pre> </p> <p> That leaves IDependency degenerate, so you might as well dispense entirely with it and let NeedyClass subscribe directly to IObservable&lt;Unit&gt;: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">NeedyClass</span> : <span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt;, <span style="color: #2b91af;">IDisposable</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IDisposable</span> subscription; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> NeedyClass(<span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (dependency == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"dependency"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subscription = dependency.Subscribe(<span style="color: blue;">this</span>); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnCompleted() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnError(<span style="color: #2b91af;">Exception</span> error) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnNext(<span style="color: #2b91af;">Unit</span> value) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Handle event here</span> &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Dispose() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subscription.Dispose(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> At least you got rid of a custom type in favor of a well-known abstraction, so that will have to count for something. </p> <h3 id="7201fbed01d545d7bd4c67940786ffbf"> Injection disconnect <a href="#7201fbed01d545d7bd4c67940786ffbf" title="permalink">#</a> </h3> <p> If you refer back to <a href="/2013/09/06/di-and-events-constructor-subscription">the discussion about Constructor Subscription</a>, you may recall an inkling that NeedyClass requests the wrong type of dependency via the constructor. If it's saving an IDisposable as its class field, then <em>why is it requesting an IObservable&ltUnit&gt;?</em> Shouldn't it simply request an IDisposable? </p> <p> This sounds promising, but in the end turns out to be a false lead. Still, this actually compiles: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">NeedyClass</span> : <span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt;, <span style="color: #2b91af;">IDisposable</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IDisposable</span> subscription; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> NeedyClass(<span style="color: #2b91af;">IDisposable</span> subscription) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (subscription == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"subscription"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subscription = subscription; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnCompleted() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnError(<span style="color: #2b91af;">Exception</span> error) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnNext(<span style="color: #2b91af;">Unit</span> value) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Handle event here</span> &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Dispose() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subscription.Dispose(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The problem with this is that while it compiles, <em>it doesn't work</em>. Considering the implementation, you should also be suspicious: it's basically a degenerate <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> of IDisposable. That <code>subscription</code> field doesn't seem to add anything to NeedyClass... </p> <p> Examining why it doesn't work should be enlightening, though. A third party can attempt to connect NeedyClass with the observable. One attempt might look like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> observable = <span style="color: blue;">new</span> <span style="color: #2b91af;">FakyDependency</span>(); <span style="color: blue;">var</span> nc = <span style="color: blue;">new</span> <span style="color: #2b91af;">NeedyClass</span>(observable.Subscribe());</pre> </p> <p> However, this <em>doesn't work</em>, because <a href="http://msdn.microsoft.com/en-us/library/ff403171.aspx">that overload of the Subscribe method</a> only exists to evaluate an event stream for side effects. The overload you'd need is the Subscribe(IObserver&lt;T&gt;) overload. However, the IObserver&lt;Unit&gt; you'd like to supply is an instance of NeedyClass, but you can't supply an instance of NeedyClass before you've supplied an IDisposable to it (a <a href="http://amzn.to/17Wnw15">Catch-22</a>)! </p> <h3 id="c7e7d86d7f2248c1a71b02292597a1a7"> Third-party Connect <a href="#c7e7d86d7f2248c1a71b02292597a1a7" title="permalink">#</a> </h3> <p> Once more, this suggests that NeedyClass really shouldn't have a dependency in order to react to events. You can simply let it be a stand-alone implementation of IObserver&lt;Unit&gt;: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">NeedyClass</span> : <span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Unit</span>&gt; { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnCompleted() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnError(<span style="color: #2b91af;">Exception</span> error) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnNext(<span style="color: #2b91af;">Unit</span> value) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Handle event here</span> &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Once again, you have a nice, stand-alone class you can connect to a publisher: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> nc = <span style="color: blue;">new</span> <span style="color: #2b91af;">NeedyClass</span>(); <span style="color: blue;">var</span> subscription = observable.Subscribe(nc);</pre> </p> <p> That pretty much puts you back at the <a href="/2013/09/08/di-and-events-third-party-connect">Third-party Connect</a> solution, so that didn't seem to buy you much. </p> <h3 id="9385387134ef4825b649ce86c4c2679e"> (Preliminary) conclusion <a href="#9385387134ef4825b649ce86c4c2679e" title="permalink">#</a> </h3> <p> So far, using Reactive Extensions instead of .NET events seems to have provided little value. You're able to replace a custom IDependency interface with a BCL interface, and that's always good. Apart from that, there seems to be little value to be gained from Reactive Extensions in this scenario. </p> <p> The greatest advantage gained so far is that, hopefully you've learned something. You've learned (through two refactoring attempts) that NeedyClass isn't needy at all, and should <em>not</em> have a dependency in order to react to events. </p> <p> The last advantage gained by using Reactive Extensions may seem like a small thing, but actually turns out to the most important of them all: by using IObservable&lt;T&gt;/IObserver&lt;T&gt; instead of .NET events, you've converted your code to work with <em>objects</em>. You know, .NET events are <em>not</em> real objects, so they can't be passed around, but IObservable&lt;T&gt; and IObserver&lt;T&gt; instances <em>are</em> real objects. This means that now that you know that NeedyClass shouldn't have a dependency, perhaps some other class should. Remember what I <a href="/2013/09/06/dependency-injection-and-events">originally said about Inversion of Inversion of Control</a>? In the <a href="/2013/09/11/di-and-events-composition">next article in the series, I'll address that issue, and arrive at a more idiomatic DI solution</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. DI and events: Third-party Connect https://blog.ploeh.dk/2013/09/08/di-and-events-third-party-connect 2013-09-08T08:08:00+00:00 Mark Seemann <div id="post"> <p> <em>Instead of using Constructor Injection to subscribe to events on a dependency, you can let a third party connect the subscriber to the publisher.</em> </p> <p> In the <a href="/2013/09/06/di-and-events-constructor-subscription">previous article</a> in my <a href="/2013/09/06/dependency-injection-and-events">series about Dependency Injection and events</a>, you saw an example of how injecting a dependency that raises events violates <a href="https://vuscode.wordpress.com/">Nikola Malovic</a>'s <a href="https://vuscode.wordpress.com/2009/10/16/inversion-of-control-single-responsibility-principle-and-nikola-s-laws-of-dependency-injection">4th law of IoC</a>: <a href="/2011/03/03/InjectionConstructorsshouldbesimple">Injection Constructors should perform no work</a>. </p> <p> In this article, you'll see the first of several alternatives. </p> <h3 id="6b542efcb2614fe5b8575be67424a4f6"> Third-party Connect <a href="#6b542efcb2614fe5b8575be67424a4f6" title="permalink">#</a> </h3> <p> Take a step back and recall why you're using Dependency Injection in the first place. Hopefully, you use Dependency Injection because it provides the decoupling necessary to make your code more maintainable (and thus, you and your colleagues more productive). However, events are <em>already</em> a mechanism for decoupling. In .NET, events are simply a (limited) baked-in implementation of the <a href="http://en.wikipedia.org/wiki/Observer_pattern">Observer pattern</a>. </p> <p> Perhaps it's helpful if we consider alternative names for Dependency Injection. <a href="http://www.natpryce.com">Nat Pryce</a> prefers the term <a href="http://www.natpryce.com/articles/000783.html">Third-party Connect</a>, and I think that there's much sense in that name. Instead of focusing on the <em>injection</em> part, or even <em>Inversion of Control</em>, the term <em>Third-party Connect</em> focuses on the fact that when you decouple two objects, you need a third party to connect them with each other. In a well-designed application, this third party will often be the <a href="/2011/07/28/CompositionRoot">Composition Root</a>. </p> <p> If you already have a third party connecting NeedyClass with IDependency, <em>must</em> you use Constructor Injection? </p> <h3 id="6e23b69cb5374a93a07b03a66a2c872b"> Further decoupling <a href="#6e23b69cb5374a93a07b03a66a2c872b" title="permalink">#</a> </h3> <p> Apparently, if you consider the code in <a href="/2013/09/06/di-and-events-constructor-subscription">the previous article</a>, NeedyClass is required to do something whenever a particular IDependency instance raises its ItHappened event. What if, instead of injecting IDependency and subscribing a private event handler, you were to expose a public method that implements the same logic as that private event handler? </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">NeedyClass</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> DoSomethingInteresting() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Handle event here</span> &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Notice how much simpler this implementation is, compared with the previous version. Nothing is injected, there are no interfaces in play. Such a class is very easy to unit test as well, so I think this looks very promising. </p> <p> Doesn't this design break encapsulation? Not more than before. Remember, in the previous implementation, you could always inject an implementation of IDependency that enabled you to raise the ItHappened event, and thereby invoke the private event handler in NeedyClass. This new design just makes it a bit easier to invoke the method. Keep in mind that encapsulation isn't about public versus private members; <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">encapsulation is about invariants</a>. </p> <p> This version of NeedyClass doesn't actually expose a public 'event handler', since the DoSomethingInteresting method doesn't match the event handler signature. Thus, if you use a static code analysis tool, it's not going to complain about a public event handler. The DoSomethingInteresting method is a method just like any other method. This design is much more decoupled than before, because NeedyClass knows <em>nothing</em> about IDependency. Hopefully, it makes sense as a stand-alone class with its own API. This makes it more reusable. </p> <p> At this point, <em>NeedyClass</em> is no longer an appropriate name, but let's keep it for now. </p> <h3 id="4a26ad03f4bf40258d38eb0c180f3ad8"> Subscription <a href="#4a26ad03f4bf40258d38eb0c180f3ad8" title="permalink">#</a> </h3> <p> In order to connect NeedyClass with IDependency, the third party (e.g. the Composition Root) can subscribe the DoSomethingInteresting method to the ItHappened event: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> nc = <span style="color: blue;">new</span> <span style="color: #2b91af;">NeedyClass</span>(); dependency.ItHappened += (s, e) =&gt; nc.DoSomethingInteresting();</pre> </p> <p> The advantage of this design is that it's much more decoupled than before. NeedyClass knows nothing about IDependency, and IDependency knows nothing about NeedyClass. However, one disadvantage is that it's easy to forget to connect these two objects with each other; the compiler no longer offers any help. </p> <p> If both objects are long-lived objects (i.e. have the <em>Singleton</em> lifetime style), you probably only need to write and invoke that connection code once, in the Composition Root. In this case, one or two <a href="http://xunitpatterns.com/Smoke%20Test.html">Smoke Tests</a> should prevent any regressions. </p> <p> However, if one or both of these objects have shorter lifetimes, you may want to encapsulate the creation of NeedyClass in some sort of Factory. Still, unless you make the NeedyClass constructor internal or private, programmers may forget to use the Factory, so this can still be a fragile solution. </p> <h3 id="4c501ab6991e4dbb94b11353b79c746f"> Unsubscription <a href="#4c501ab6991e4dbb94b11353b79c746f" title="permalink">#</a> </h3> <p> Another problem that this Third-party Connect solution shares with Constructor Subscription is that you still need to think about disconnecting the two objects, once you're done with them. This isn't hard to do. </p> <p> <pre style="margin: 0px;"><span style="color: #2b91af;">EventHandler</span> handler = (s, e) =&gt; nc.DoSomethingInteresting(); dependency.ItHappened += handler; &nbsp; <span style="color: green;">// Do something interesting here</span> &nbsp; dependency.ItHappened -= handler;</pre> </p> <p> While not particularly difficult, it does require you to take the extra step of assigning the event handler to a variable you can later use to unsubscribe. Still, the worst part of this attempt at a solution is probably that, just like you'll need to remember to subscribe NeedyClass to the event, you must also remember to unsubscribe it. At least, in this case, there's a better symmetry, because you must remember to both subscribe and unsubscribe, whereas with Constructor Subscription, you only needed to remember to unsubscribe (or dispose, as it were). </p> <h3 id="74e07f6bd01249b0905d110774de40b5"> Conclusion <a href="#74e07f6bd01249b0905d110774de40b5" title="permalink">#</a> </h3> <p> Using Third-party Connect leads to a simpler, but more <a href="http://amzn.to/19W4JHk">Fragile design</a>. Still, I often find that the extreme simplicity of the involved classes trumps the fragility of the design; if I <em>had</em> to choose between Third-party Connect and Constructor Subscription, I'd select Third-party Connect. Fortunately, these aren't the only options available to you; in <a href="/2013/09/11/di-and-events-reactive-extensions">future articles, I'll approach a better alternative</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. DI and events: Constructor Subscription https://blog.ploeh.dk/2013/09/06/di-and-events-constructor-subscription 2013-09-06T09:07:00+00:00 Mark Seemann <div id="post"> <p> <em>Using Constructor Injection, you can subscribe to an event within the constructor, but should you?</em> </p> <p> This post is the first in <a href="/2013/09/06/dependency-injection-and-events">a series of articles about Dependency Injection and events</a>. </p> <p> Imagine that you have an interface that defines an event: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IDependency</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">event</span> <span style="color: #2b91af;">EventHandler</span> ItHappened; }</pre> </p> <p> In order to keep the example simple, the ItHappened event carries no information; it just raises an event with an <a href="http://msdn.microsoft.com/en-us/library/system.eventargs.aspx">EventArgs</a> instance. Thus, subscribers can react to the fact that this particular event happened, but there's no data contained in the event. However, the following discussion doesn't change if the event carries information. </p> <p> A class must react to the events raised by IDependency implementations. A common approach is to subscribe to the event in the constructor. We can call this pattern <em>Constructor Subscription</em>: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">NeedyClass</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IDependency</span> dependency; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> NeedyClass(<span style="color: #2b91af;">IDependency</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (dependency == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"dependency"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.dependency = dependency; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.dependency.ItHappened += <span style="color: blue;">this</span>.OnItHappened; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">void</span> OnItHappened(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Handle event here</span> &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The concern is that, by subscribing to an event, the constructor violates <a href="https://vuscode.wordpress.com/">Nikola Malovic</a>'s <a href="https://vuscode.wordpress.com/2009/10/16/inversion-of-control-single-responsibility-principle-and-nikola-s-laws-of-dependency-injection">4th law of IoC</a>: <a href="/2011/03/03/InjectionConstructorsshouldbesimple">Injection Constructors should perform no work</a>. </p> <p> This rule about Dependency Injection explains why you can <a href="/2011/03/04/Composeobjectgraphswithconfidence">compose big object graphs with confidence</a>. Still, the most compelling reason for conforming strictly to the rule is most likely performance considerations. So, what if you subscribe to a single event or two during construction? Will it adversely (and noticeably) affect performance of your overall system? </p> <p> As always, the answer to such questions is: <em>measure</em>. However, I'd be quite surprised if it turns out that a single event subscription has a <em>huge</em> impact on performance... </p> <h3 id="e1a1a0c3b6214b2aad8ec4c41cf064d9"> Smell <a href="#e1a1a0c3b6214b2aad8ec4c41cf064d9" title="permalink">#</a> </h3> <p> Consider the implementation of NeedyClass: it contains a design smell. Can you spot it? </p> <p> Given the definition of the IDependency interface, the ItHappened event is the only member defined by the interface. If this is the case, then why is NeedyClass holding on to a reference of the interface? </p> <p> You can remove the <code>dependency</code> field from NeedyClass, and nothing is going to break: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">NeedyClass</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> NeedyClass(<span style="color: #2b91af;">IDependency</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (dependency == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"dependency"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dependency.ItHappened += <span style="color: blue;">this</span>.OnItHappened; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">void</span> OnItHappened(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Handle event here</span> &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> From the perspective of an outside observer, that's really strange. Why are we required to pass in an object that's not going to be used as is? Just like in a <a href="/2013/07/20/linq-versus-the-lsp">previous discussion about the implications of injecting IEnumerable&lt;T&gt;</a>, if you're injecting an abstraction, and the constructor then starts querying, modifying, examining, or otherwise fidget with the injected object, then you're probably injecting the wrong object. </p> <p> Keep in mind that Constructor Injection is a declaration of requirements. It's the declaring class that advertises to the world: <em>these are (some of) my invariants</em>. If the class can't use the injected dependency as is, it suggests that it requested the wrong object from its clients. The class with the Injection Constructor should declare what it <em>really</em> needs. In this case, it needs to react to events. </p> <p> In the <a href="/2013/09/08/di-and-events-third-party-connect">next post, I'll show you a better (i.e. simpler) design</a> for reacting to events. </p> <h3 id="68a0584d99684c15a2bc6ff5334638bb"> Unsubscription (Update, September 9, 2013, 11:48 UTC) <a href="#68a0584d99684c15a2bc6ff5334638bb" title="permalink">#</a> </h3> <p> Some readers <a href="https://twitter.com/GeertvanHorrik/status/375915573034909696">have commented</a> that NeedyClass <a href="https://twitter.com/clausndk/status/375931324436992000">must keep the reference to IDependency in order to unsubscribe from the event</a>. This is true, and something I overlooked when I originally banged together the sample code yesterday evening. Apparently, three unit tests were at least a test too few... :$ </p> <p> From a perspective of basic correctness, NeedyClass works appropriately, but as <a href="https://twitter.com/GeertvanHorrik">Geert van Horrik</a> and <a href="https://twitter.com/clausndk">Claus Nielsen</a> point out, this could easily lead to resource leaks (although in practice, that depends on the effective lifetime of NeedyClass). </p> <p> What happens when we start taking resource management into account? </p> <p> Well, NeedyClass must be sure to unsubscribe from the event when it goes out of scope. The most correct way of making sure this happens is to implement IDisposable: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">NeedyClass</span> : <span style="color: #2b91af;">IDisposable</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IDependency</span> dependency; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> NeedyClass(<span style="color: #2b91af;">IDependency</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (dependency == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"dependency"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.dependency = dependency; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.dependency.ItHappened += <span style="color: blue;">this</span>.OnItHappened; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">void</span> OnItHappened(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Handle event here</span> &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Dispose() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.dependency.ItHappened -= <span style="color: blue;">this</span>.OnItHappened; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> While this weakens the argument that NeedyClass is requesting the wrong kind of dependency, this is a lot of work just to consume an event. Furthermore, it isn't particularly safe, because you'll <em>have</em> to remember to dispose of all of your NeedyClass instances. </p> <p> Thus, <a href="/2013/09/08/di-and-events-third-party-connect">a better approach</a> is still warranted. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="3ca28fbb70f74051a1f9d2c032f15c61"> <div class="comment-author"><a href="http://www.joonapekka.fi/">Jope</a> <a href="#3ca28fbb70f74051a1f9d2c032f15c61">#</a></div> <div class="comment-content"> <script src="https://gist.github.com/anonymous/6483457.js"></script> <p> Or if you want to be explicit about the dependencies, then go for something like <i>NeedyClass : IHandle&lt;ItHappened&gt;</i>. Nevertheless, dependency on ItHappened within NeedyClass would still easily be found with static analysis. A few lines of CQLinq with NDepend. </p> </div> <div class="comment-date">2013-09-09 16:34 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency Injection and events https://blog.ploeh.dk/2013/09/06/dependency-injection-and-events 2013-09-06T08:38:00+00:00 Mark Seemann <div id="post"> <p> <em>Overview of articles about Dependency Injection and events.</em> </p> <p> One of my readers, Kenny Pflug, asks me about subscribing to events in the constructor of a class using Constructor Injection: </p> <blockquote> <p> I want to register to a plain old .NET event of a dependency that is supplied via constructor injection. I read your book "<a href="http://amzn.to/12p90MG">DI in .NET</a>" and in it you mention that injection constructors should be simple and not perform any other work than guarding for null values and assigning the dependencies to (read-only) fields. I also read on your blog that <a href="/2011/03/03/InjectionConstructorsshouldbesimple">this is only a pattern and might be broken gently if the need would arise</a>. </p> <p> [...] do you know and prefer any patterns to solve this "registering to events of dependencies in a constructor" problem? </p> </blockquote> <p> In short, I think that it's a code smell if a design requires you to subscribe to an event in the constructor; it's a smell that the dependency chain is slightly inverted. Inversion of Inversion of Control - how about that? </p> <p> In the next series of posts, I'll attempt to provide various perspectives on this situation, starting with the original context. </p> <p> <ul> <li><a href="/2013/09/06/di-and-events-constructor-subscription">DI and events: Constructor Subscription</a>.</li> <li><a href="/2013/09/08/di-and-events-third-party-connect">DI and events: Third-party Connect</a>.</li> <li><a href="/2013/09/11/di-and-events-reactive-extensions">DI and events: Reactive Extensions</a>.</li> <li><a href="/2013/09/11/di-and-events-composition">DI and events: Composition</a>.</li> </ul> </p> <p> In summary, it's probably not going to be a big problem if you subscribe to an event in a constructor, but it's a code smell, because it betrays a design issue. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Running a pure F# Web API on Azure Web Sites https://blog.ploeh.dk/2013/08/26/running-a-pure-f-web-api-on-azure-web-sites 2013-08-26T08:26:00+00:00 Mark Seemann <div id="post"> <p> <em>This post explains how to make a pure F# implementation of an ASP.NET Web API run in an Azure Web Site.</em> </p> <p> In my <a href="/2013/08/23/how-to-create-a-pure-f-aspnet-web-api-project">previous post</a>, I explained how to create an <a href="http://www.asp.net/web-api">ASP.NET Web API</a> project entirely in F#, without relying on a C# or VB host project. At the end of that article, I had a service which can be launched from Visual Studio 2012 and run in IIS Express, but it didn't run in <a href="http://www.windowsazure.com/en-us/services/web-sites">Azure Web Sites</a>. In this post, I'll explain how to make this possible. </p> <h3 id="b85b52709d124defa9abb4f9a66d6aa9"> Deploying to Azure <a href="#b85b52709d124defa9abb4f9a66d6aa9" title="permalink">#</a> </h3> <p> The first problem to solve is how to even deploy the Web Project to Azure in the first place. Because the project is a 'real' Visual Studio Web Project, it's possible to right-click on the project and select "Publish..." This brings up the dialog for publishing a Web Project to Azure, so that seems promising. </p> <p> However, actually attempting to do so soon produces this error message: <blockquote>Exception in executing publishing : The method or operation is not implemented.</blockquote> Apparently, someone in Microsoft chose to violate the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>... </p> <p> Another, and, as it turns out, ultimately more productive, deployment options is to deploy via Git. Fortunately, I already kept a Git repository for the code, in order to make it easier for me to back out, if my experiments took me in wrong directions (which did happen a couple of times). Thus, I created the Web Site on the Azure portal, and configured it with a Git repository to which I can push. </p> <p> <img src="/content/binary/setting-up-local-git-repository-for-feb-api.png"> </p> <p> This should enable me to simply go </p> <p> <pre>$ git push azure master</pre> </p> <p> in order to deploy to my new Azure Web Site. Unfortunately, it didn't quite work: </p> <p> <blockquote> <pre>$ git push azure master Counting objects: 113, done. Delta compression using up to 4 threads. Compressing objects: 100% (101/101), done. Writing objects: 100% (113/113), 1.41 MiB | 22.00 KiB/s, done. Total 113 (delta 34), reused 0 (delta 0) remote: Updating branch 'master'. remote: Updating submodules. remote: Preparing deployment for commit id 'dd501baeaa'. remote: Generating deployment script. remote: . remote: info: Executing command site deploymentscript remote: info: Project file path: .\FebApi\FebApi.fsproj remote: info: Solution file path: .\FebApi.sln remote: info: Generating deployment script for .NET Web Application remote: info: Generated deployment script files remote: info: site deploymentscript command OK remote: Running deployment command... remote: Handling .NET Web Application deployment. remote: ..... remote: FebApi -> C:\DWASFiles\Sites\FebApi\VirtualDirectory0\site\repository\FebApi\bin\Release\FebApi.dll remote: C:\DWASFiles\Sites\FebApi\VirtualDirectory0\site\repository\FebApi\FebApi.fsproj : error MSB4057: The target "pipelinePreDeployCopyAllFilesToOneFolder" does not exist in the project. remote: An error has occurred during web site deployment. remote: remote: Error - Changes committed to remote repository but your website not updated. To https://******@febapi.scm.azurewebsites.net:443/FebApi.git * [new branch] master -> master</pre> </blockquote> </p> <p> More work apparently remained. </p> <h3 id="77aa53e2d32d49168b0481149ad0339f"> Import MSBuild projects <a href="#77aa53e2d32d49168b0481149ad0339f" title="permalink">#</a> </h3> <p> As you can tell from the error message, the "target "pipelinePreDeployCopyAllFilesToOneFolder" does not exist in the project." Knowing that Visual Studio project files are actually MSBuild files with another extension, this sounds like an MSBuild issue. To figure out what to do, I opened a C# Web Project and began looking for various <code>Import</code> elements. </p> <p> After copying a couple of <code>Import</code> elements from a C# Web Project, and a bit of experimentation, I ended up with this in my .fsproj file: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">Import</span><span style="color: blue;"> </span><span style="color: red;">Project</span><span style="color: blue;">=</span>"<span style="color: blue;">$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets</span>"<span style="color: blue;"> </span> <span style="color: red;">Condition</span><span style="color: blue;">=</span>"<span style="color: blue;">'$(VSToolsPath)' != ''</span>"<span style="color: blue;"> /&gt;</span> <span style="color: blue;">&lt;</span><span style="color: #a31515;">Import</span><span style="color: blue;"> </span><span style="color: red;">Project</span><span style="color: blue;">=</span>"<span style="color: blue;">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets</span>"<span style="color: blue;"> </span> <span style="color: red;">Condition</span><span style="color: blue;">=</span>"<span style="color: blue;">true</span>"<span style="color: blue;"> /&gt;</span></pre> </p> <p> My .fsproj file already had an existing <code>Import</code> element, so I added these two just below the existing element. I haven't experimented with removing one of these elements, so it may be possible to simplify this, or somehow make it more robust. What mattered to me was that this enabled me to move on: </p> <p> <blockquote> <pre>$ git push azure master Counting objects: 7, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 404 bytes | 0 bytes/s, done. Total 4 (delta 3), reused 0 (delta 0) remote: Updating branch 'master'. remote: Updating submodules. remote: Preparing deployment for commit id 'd3625cfef0'. remote: Generating deployment script. remote: Running deployment command... remote: Handling .NET Web Application deployment. remote: .... remote: FebApi -> C:\DWASFiles\Sites\FebApi\VirtualDirectory0\site\repository\FebApi\bin\Release\FebApi.dll remote: Copying all files to temporary location below for package/publish: remote: C:\DWASFiles\Sites\FebApi\Temp\7a1a548e-d5fe-48d6-94ec-99146f20676a. remote: KuduSync.NET from: 'C:\DWASFiles\Sites\FebApi\Temp\7a1a548e-d5fe-48d6-94ec-99146f20676a' to: 'C:\DWASFiles\Sites\FebApi\VirtualDirectory0\site\wwwroot' remote: Deleting file: 'hostingstart.html' remote: Copying file: 'bin\FebApi.dll' remote: Copying file: 'bin\FebApi.XML' remote: Copying file: 'bin\FSharp.Core.dll' remote: Copying file: 'bin\Microsoft.Web.Infrastructure.dll' remote: Copying file: 'bin\Newtonsoft.Json.dll' remote: Copying file: 'bin\System.Net.Http.Formatting.dll' remote: Copying file: 'bin\System.Web.Http.dll' remote: Copying file: 'bin\System.Web.Http.WebHost.dll' remote: Finished successfully. remote: Deployment successful. To https://ploeh@febapi.scm.azurewebsites.net:443/FebApi.git 658e859..d3625cf master -> master</pre> </blockquote> </p> <p> Alas, while deployment succeeded, I wasn't out of the woods yet. </p> <h3 id="2c232a71acb44be084aad4ecb2a0e7ac"> Build actions <a href="#2c232a71acb44be084aad4ecb2a0e7ac" title="permalink">#</a> </h3> <p> Browsing to the (successfully deployed) site gave me this (rather disappointing) message: <blockquote>You do not have permission to view this directory or page.</blockquote> </p> <p> After digging around for a while, I discovered that neither Global.asax nor web.config were deployed to the actual site. The way to resolve that is to change the <em>Build Action</em> for these files to <em>Content</em>. </p> <p> This was the last hurdle. Pushing those changes to the remote Git repository updated the API, which now works! For the time being, you can see it running <a href="http://febapi.azurewebsites.net">here</a>, although I will not promise to keep it around forever. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. How to create a pure F# ASP.NET Web API project https://blog.ploeh.dk/2013/08/23/how-to-create-a-pure-f-aspnet-web-api-project 2013-08-23T09:13:00+00:00 Mark Seemann <div id="post"> <p> <em>How to create a single F# project for an ASP.NET Web API, without using an auxiliary C# or VB host project.</em> </p> <p> Implementing an <a href="http://www.asp.net/web-api">ASP.NET Web API</a> service in F# is <a href="http://blogs.msdn.com/b/fsharpteam/archive/2012/09/04/exploring-the-online-templates-creating-a-web-api-with-f-and-asp-net.aspx">a relatively well-described process</a>, but everywhere I look, it seems that people are relying on a (Humble) C# Web Project to act as a host for a library written in F#. You can use an online Visual Studio template, or you can do this by hand - it's not particularly difficult. Where's the challenge in that? </p> <p> Partly in a quest to demonstrate that F# is a general-purpose language, and partly just to see if it can be done, I wanted to create a pure F# ASP.NET Web API project, without relying on a C# host project. Most people on Twitter advised against it because <a href="https://twitter.com/tomaspetricek/status/370598327361863680">there's no F# support for Razor, *.aspx, or *.cshtml</a>. However, that's not a concern for a Web API, since it's not going to generate HTML anyway. </p> <p> As <a href="https://twitter.com/dotnetnerd">@dotnetnerd</a> puts it: <blockquote>"<a href="https://twitter.com/dotnetnerd/status/370546345360515072">not trivial but doable</a>"</blockquote> That turns out to be quite an accurate description. Here's how. </p> <h3 id="4965dbf097d84c218c844b09ecfcce8e"> Create an F# library project <a href="#4965dbf097d84c218c844b09ecfcce8e" title="permalink">#</a> </h3> <p> The first thing I did was to create a normal F# library project. </p> <p> <img src="/content/binary/initial-fsharp-project.png"> </p> <p> As you can see, there's nothing in that project yet. </p> <h3 id="1f577784fa5c45e78758e6a3abce62a8"> Turn the project into a Web Project <a href="#1f577784fa5c45e78758e6a3abce62a8" title="permalink">#</a> </h3> <p> In contrast to C# and VB, there's no built-in F# Web Project template. However, in order to work with a web project in Visual Studio 2012, it helps if the project is a 'real' Web Project, because it means that you can launch it in IIS Express, and such things. </p> <p> Because there's no built-in Web Project template for F#, I'm not aware of a nice way to do this through the Visual Studio UI, but you can open your .fsproj file in an text editor and hand-edit it. In order to turn my project into a Web Project, I added this line of code to my .fsproj file (just below the Project/PropertyGroup/ProjectGuid element): </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">ProjectTypeGuids</span><span style="color: blue;">&gt;</span>{349c5851-65df-11da-9384-00065b846f21};{F2A71F9B-5D33-465A-A702-920D77279786}<span style="color: blue;">&lt;/</span><span style="color: #a31515;">ProjectTypeGuids</span><span style="color: blue;">&gt;</span></pre> </p> <p> The 349c5851-65df-11da-9384-00065b846f21 GUID tells Visual Studio that this is a Web Project, while F2A71F9B-5D33-465A-A702-920D77279786 indicates an F# project. </p> <p> This isn't entirely without drawbacks (that I'll get to in a minute), but you can now reload the project in Visual Studio, and you actually have an F# Web Project. </p> <h3 id="04a94f03ae794ae1965da7fd970c4413"> Turn on IIS Express <a href="#04a94f03ae794ae1965da7fd970c4413" title="permalink">#</a> </h3> <p> One of the things you can do with a Web Project is to right-click in Solution Explorer and select "Use IIS Express...", so I did that. </p> <h3 id="bac43335a9d24aa5a01efddca14fa20c"> Add Web API dependencies <a href="#bac43335a9d24aa5a01efddca14fa20c" title="permalink">#</a> </h3> <p> In order to use the ASP.NET Web API, you'll need to add the appropriate dependencies. I did that by adding the <a href="http://www.nuget.org/packages/Microsoft.AspNet.WebApi.WebHost/4.0.30506.0">Microsoft.AspNet.WebApi.WebHost</a> NuGet package. </p> <h3 id="7145d32e9adb4b6883beae916fe25a3f"> Add a code file <a href="#7145d32e9adb4b6883beae916fe25a3f" title="permalink">#</a> </h3> <p> One of the disadvantages of creating an F# Web Project is that it's an unknown combination, so you can't add <em>anything</em> to the project: </p> <p> <img src="/content/binary/no-items-found-for-fsharp-web-project.png"> </p> <p> In order to work around this problem, I again hand-edited the .fsproj file, temporarily commenting out the ProjectTypeGuids element I previously added. Then I added an F# code file, and finally added the ProjectTypeGuids element again. </p> <h3 id="e0e4a955d6e54957ad3b727e543b9cf6"> Change the output folder <a href="#e0e4a955d6e54957ad3b727e543b9cf6" title="permalink">#</a> </h3> <p> The default output folder for the <em>debug</em> build is "bin\Debug". Although I'm not sure whether this is <em>strictly</em> necessary, I changed it to "bin" because that's how C# Web Projects work... </p> <h3 id="e91ab54b270e451689dd34a019b3829c"> Reference System.Web <a href="#e91ab54b270e451689dd34a019b3829c" title="permalink">#</a> </h3> <p> In order to be able to derive a Global class from System.Web.HttpApplication, I added a reference to System.Web. This is a BCL library, so I added it by using the old-fashioned <em>Add reference</em> menu item (instead of using NuGet). </p> <h3 id="a94fe5d3188042ad9ae0d6955f16fe20"> Add Global.asax <a href="#a94fe5d3188042ad9ae0d6955f16fe20" title="permalink">#</a> </h3> <p> Next, I added a Global.asax file with this content: </p> <p> <pre style="margin: 0px;"><span style="background: yellow;">&lt;%</span><span style="color: blue;">@</span> <span style="color: maroon;">Application</span> <span style="color: red;">Inherits</span><span style="color: blue;">="Ploeh.Samples.FebApi.Global"</span> <span style="background: yellow;">%&gt;</span></pre> </p> <p> The class Ploeh.Samples.FebApi.Global doesn't exist yet, so if you try to run the site at this stage, it's going to fail. </p> <h3 id="e7c9a44feca148baa030211cae28cadd"> Add a Global class <a href="#e7c9a44feca148baa030211cae28cadd" title="permalink">#</a> </h3> <p> In order to add the Ploeh.Samples.FebApi.Global class, I wrote the first F# code in the project: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">namespace</span> Ploeh.Samples.FebApi &nbsp; <span style="color: blue;">open</span> System &nbsp; <span style="color: blue;">type</span> Global() = &nbsp;&nbsp;&nbsp; <span style="color: blue;">inherit</span> System.Web.HttpApplication() &nbsp;&nbsp;&nbsp; <span style="color: blue;">member</span> this.Application_Start (sender : obj) (e : EventArgs) = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ()</pre> </p> <p> The site still can't run, but at least now it doesn't complain about a missing Global class. </p> <h3 id="79f6a9913c1f47b5bc777e202be9a6fe"> Add a Route <a href="#79f6a9913c1f47b5bc777e202be9a6fe" title="permalink">#</a> </h3> <p> The next step was to add a default Web API route: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">namespace</span> Ploeh.Samples.FebApi &nbsp; <span style="color: blue;">open</span> System <span style="color: blue;">open</span> System.Web.Http &nbsp; <span style="color: blue;">type</span> HttpRouteDefaults = { Controller : string; Id : obj } &nbsp; <span style="color: blue;">type</span> Global() = &nbsp;&nbsp;&nbsp; <span style="color: blue;">inherit</span> System.Web.HttpApplication() &nbsp;&nbsp;&nbsp; <span style="color: blue;">member</span> this.Application_Start (sender : obj) (e : EventArgs) = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GlobalConfiguration.Configuration.Routes.MapHttpRoute( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"DefaultAPI"</span>, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"{controller}/{id}"</span>, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { Controller = <span style="color: #a31515;">"Home"</span>; Id = RouteParameter.Optional }) |&gt; ignore</pre> </p> <p> At this point, you actually have an ASP.NET Web API site, because now, when you attempt to run the site, you get this error message: <blockquote>No HTTP resource was found that matches the request URI 'http://localhost:64176/'. No type was found that matches the controller named 'Home'.</blockquote> Which is great, because this is known territory. </p> <h3 id="fd9af0a5544f46818edc30bb0d67e6df"> Add HomeController <a href="#fd9af0a5544f46818edc30bb0d67e6df" title="permalink">#</a> </h3> <p> Adding the missing HomeController class is easy: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">type</span> HomeController() = &nbsp;&nbsp;&nbsp; <span style="color: blue;">inherit</span> ApiController()</pre> </p> <p> Of course, it doesn't do anything yet, so when you browse to the API, you get this (entirely expected) error message: <blockquote>The requested resource does not support http method 'GET'.</blockquote> This problem is easy to solve: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">type</span> HomeRendition() = &nbsp;&nbsp;&nbsp; [&lt;DefaultValue&gt;] <span style="color: blue;">val</span> <span style="color: blue;">mutable</span> Message : string &nbsp;&nbsp;&nbsp; [&lt;DefaultValue&gt;] <span style="color: blue;">val</span> <span style="color: blue;">mutable</span> Time : string &nbsp; <span style="color: blue;">type</span> HomeController() = &nbsp;&nbsp;&nbsp; <span style="color: blue;">inherit</span> ApiController() &nbsp;&nbsp;&nbsp; <span style="color: blue;">member</span> this.Get() = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.Request.CreateResponse( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HttpStatusCode.OK, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HomeRendition( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Message = <span style="color: #a31515;">"Hello from F#"</span>, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Time = DateTimeOffset.Now.ToString(<span style="color: #a31515;">"o"</span>)))</pre> </p> <p> Now, when browsing to the API, you get something like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">HomeRendition</span><span style="color: blue;"> </span><span style="color: red;">xmlns</span><span style="color: blue;">=</span>"<span style="color: blue;">http://schemas.datacontract.org/2004/07/Ploeh.Samples.FebApi</span>"<span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">Message</span><span style="color: blue;">&gt;</span>Hello from F#<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Message</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">Time</span><span style="color: blue;">&gt;</span>2013-08-23T10:26:40.8490741+02:00<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Time</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">HomeRendition</span><span style="color: blue;">&gt;</span></pre> </p> <p> Success, of a sort! </p> <p> This Web API now runs in IIS Express on my local machine. However, at this point, it still doesn't run in an Azure Web Site (which is something I also wanted to enable), but I'll cover how to do that in <a href="/2013/08/26/running-a-pure-f-web-api-on-azure-web-sites">a future post</a>. </p> <p> <strong>Update 2013.19.12:</strong> It turns out that it's possible to <a href="http://bloggemdano.blogspot.dk/2013/11/adding-new-items-to-pure-f-aspnet.html">hack the registry</a> to make it possible to add standard F# project items to the project. </p> <p> <strong>Update 2014.02.17:</strong> Since I wrote this article, <a href="http://blogs.msdn.com/b/fsharpteam/archive/2013/12/12/new-visual-f-templates-for-asp-net-web-api-mstest-and-nancy-now-available.aspx">new F# templates are now available for Visual Studio</a>, including <a href="http://bloggemdano.blogspot.dk/2013/12/a-new-f-aspnet-mvc-5-and-web-api-2.html">a template for F# ASP.NET MVC 5 and Web API 2 projects</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="7e7dff79b92040d5bf3eba7815652e15"> <div class="comment-author"><a href="http://scrambledbrains.net" name="comment-1">Mike McG</a> <a href="#7e7dff79b92040d5bf3eba7815652e15">#</a></div> <div class="comment-content"> <p>This is so awesome!</p> <p>One note: For adding a new file, I simply used Ctrl+N (i.e. File&gt;New) to create a file in the Miscellaneous Files meta solution folder. I saved the file in the physical project folder, then in Solution Explorer I dragged it's icon from the Miscellaneous Files folder onto the F# Project node. Voila! New code file. Not as easy as it should be, but easier than unloading the project to edit the fsproj file.</p> </div> <div class="comment-date">2013-08-23 17:10 UTC</div> </div> <div class="comment" id="cf5a14be675f4848aa282c7e72df0554"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cf5a14be675f4848aa282c7e72df0554">#</a></div> <div class="comment-content"> <p> Mike, that's a great tip! Thank you. </p> <p> Another option is to create the file in the file system (with Windows Explorer, or your preferred shell), and then add the existing file to the project. </p> </div> <div class="comment-date">2013-08-24 09:49 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Checking math homework with F# https://blog.ploeh.dk/2013/08/21/checking-math-homework-with-f 2013-08-21T20:36:00+00:00 Mark Seemann <div id="post"> <p> <em>Teaching my daughter F# while checking her math homework.</em> </p> <p> My daughter Linea is 10 years old, and I've been looking for ways to make programming relevant to her. Last year, I discovered that her math homework was beginning to include simple functions, although they weren't called that. Instead, they were verbal assignments like: "for each of the following numbers, multiply the number by 3, and subtract 2." Still, it got me thinking whether Functional Programming would be a relevant introduction to programming. </p> <p> In vacations, we've been doing a bit of F# programming, and I actually got so far as to help her implement <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataFizzBuzz">FizzBuzz</a> in F#. Still, I was struggling with coming up with a continuing set of small problems that would enable us to progress. </p> <p> Until yesterday, that is. Linea actually likes doing her math homework, but she always wants me to check it for her. This is something I'm already using some time at, but suddenly I realized that we could do it together - in F#! She would have to check her results by typing in each question, and learn F# as we go along. </p> <p> This makes F# relevant to her, and being the lazy programmer that I am, I'm at the same time trying to teach her a few shortcuts we can take here and there. Here's a scan of a small part of her homework: </p> <p> <img src="/content/binary/homework-small.jpg"> </p> <p> The Danish text basically says: <em>multiply the numbers</em>. As you can tell, Linea first did her homework the old-fashioned way, filling the numbers into the table. Then we sat down to check her work with F#. Here's a copy of the part of tonight's F# Interactive (FSI, a <a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a> for F#) session related to that table: </p> <p> <pre>&gt; let m9 x = 9 * x;; val m9 : x:int -&gt; int &gt; m9 12;; val it : int = 108 &gt; m9 20;; val it : int = 180 &gt; m9 22;; val it : int = 198 &gt; let m11 x = 11 * x;; val m11 : x:int -&gt; int &gt; let input = [9;12;20;22;30];; val input : int list = [9; 12; 20; 22; 30] &gt; Seq.map m11 input;; val it : seq&lt;int&gt; = seq [99; 132; 220; 242; ...] &gt; List.map m11 input;; val it : int list = [99; 132; 220; 242; 330] &gt; input |&gt; List.map m11;; val it : int list = [99; 132; 220; 242; 330] &gt; input |&gt; List.map m11 |&gt; List.sum;; val it : int = 1023 &gt; List.map m9 input;; val it : int list = [81; 108; 180; 198; 270] &gt; input |&gt; List.map m9;; val it : int list = [81; 108; 180; 198; 270] &gt; input |&gt; List.map m9 |&gt; List.sum;; val it : int = 837 &gt;</pre> </p> <p> As you can tell, Linea first created a function (m9) in order to multiply a number with 9. This is the function form I've taught her back when we were doing FizzBuzz. You could write it more succinctly as <code>let m9 = (*) 9</code>, but I didn't want to confuse her :) </p> <p> She evaluated the first row in the table using the m9 function one cell at a time, but when it came to the next row, I decided to teach her about <a href="http://msdn.microsoft.com/en-us/library/ee370378.aspx">List.map</a>. First, I had her create a list (input) of all the column head numbers from the table. Then I taught her to invoke the map function with the input and her m11 function. As you can see, first I had her use the imperative function call style she already knew, and then I had her use the pipe operator. That gave us a list with all the results for the last row of the table, and we could now compare the list with her homework to confirm that her results were correct. </p> <p> If you look at the table, you'll see that the last column is called <em>tjek</em> (check), and contains a pre-populated sum the pupil can use to check whether her homework is correct. I wanted Linea to use F# to calculate the sum to confirm that everything indeed adds up correctly, so I introduced her to <a href="http://msdn.microsoft.com/en-us/library/ee353634.aspx">List.sum</a>, and how she could further pipe her result list into the sum function, to get the sum. As you can see from the FSI session, she did that for both rows. </p> <p> Obviously, I helped her here and there, but she picked up some of the concepts quite easily, and was altogether happy about our session. She felt that she learned a bit of F#, and she could relate to the work we did because she likes math already. </p> <p> My purpose of posting this was to share the idea of using F# (or another programming language) to teach kids programming. While you could use other languages, I find F# a good fit because its syntax is close to the math syntax Linea learns in school, and she doesn't have to relate to a lot of curly brackets and parentheses. Additionally, the FSI makes this kind of ad hoc work easily approachable. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. LINQ versus the LSP https://blog.ploeh.dk/2013/07/20/linq-versus-the-lsp 2013-07-20T21:00:00+00:00 Mark Seemann <div id="post"> <p> <em>This post examines the conflict between Iterators, LINQ, and the Liskov Substitution Principle.</em> </p> <p> As a reaction to my <a href="/2013/07/08/defensive-coding">my previous post on Defensive Coding</a>, one of my readers sent me this question: </p> <blockquote> <p> "[One] very prominent scenario of defensive coding is used when the input argument is IEnumerable&lt;T&gt; </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Publisher</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Publisher(<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">Subscriber</span>&gt; subscribers) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// defensive copy -&gt; good or bad?</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subscribers = subscribers.ToArray(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">//&nbsp; &#8230;</span> }</pre> </p> <p> "You could argue: when the intention is to use IEnumerable&lt;T&gt;, the receiver shall not to believe this sequence is immutable. You could indicate an immutable sequence with ICollection for instance. In the example above, the caller might add a new subscriber silently to its own list and have it automatically injected into the 'publisher' (maybe that's the intention from the perspective of the caller). However, a defensive copy breaks this behavior because the injected sequence is now no longer under control of the caller. This shows how simple an implementation detail can change the behavior on the client. </p> <p> "The reason you often see code like this is because we like immutable objects and second because of the unknown performance impact of IEnumerable. Once you take a copy of the input you can predict the performance of your class otherwise you can't. </p> <p> "I tend to say it's 'bad' to take defensive copy (after reading many of your blog posts), but would be happy to hear your opinion on that." </p> </blockquote> <p> This question warrants an in-depth answer. </p> <h3 id="d2fdd54bc0f84d1598fd7bdfdf0fac05"> Encapsulation <a href="#d2fdd54bc0f84d1598fd7bdfdf0fac05" title="permalink">#</a> </h3> <p> IEnumerable&lt;T&gt; is one of the most misunderstood interfaces in .NET. It carries very few guarantees, and many method calls on it may actually break the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> (LSP). ToArray is one of them, because it assumes that the sequence produced by the Iterator is finite, <em>which it may not be</em>. Thus, if you invoke ToArray on an infinite Iterator, you will eventually get an exception. </p> <p> It doesn't really matter whether you call ToArray in the constructor, or in the class method(s) where you use the injected IEnumerable&lt;T&gt;. However, from a Fail First perspective, and in order to <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">protect the class' invariant</a>, if the class <em>requires</em> the sequence to be finite, you could argue that you should invoke ToArray (or ToList) in the constructor. However, this breaks <a href="https://vuscode.wordpress.com">Nikola Malovic</a>'s <a href="https://vuscode.wordpress.com/2009/10/16/inversion-of-control-single-responsibility-principle-and-nikola-s-laws-of-dependency-injection">4th law of IoC</a>: <a href="/2011/03/03/InjectionConstructorsshouldbesimple">constructors should do no work</a>. This should make you stop and ponder: if you <em>require</em> an array, you should state that requirement up front: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Publisher</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Publisher(<span style="color: #2b91af;">Subscriber</span>[] subscribers) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.subscribers = subscribers; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">//&nbsp; &#8230;</span> }</pre> </p> <p> Notice that instead of requesting IEnumerable&lt;T&gt;, this version requests an array and simply assigns the array to a private field. </p> <p> However, the problem is that an array isn't <em>quite</em> the same as an Iterator; the most profound difference is that the Publisher class is actually able to mutate the array by replacing elements within the array. This could be a problem if the array is shared with other clients. </p> <p> Another problem is that if the Publisher doesn't <em>need</em> the ability to mutate the array, it now breaks the <a href="http://en.wikipedia.org/wiki/Robustness_principle">Robustness Principle</a>, because a finite Iterator would have been <em>good enough</em> for its needs, but still, it puts an unwarranted demand on its clients. </p> <p> Asking for <a href="http://msdn.microsoft.com/en-us/library/92t2ye13.aspx">ICollection&lt;T&gt;</a>, as my reader suggests, is an even greater violation of the Robustness Principle, because that interface adds <em>seven</em> new methods on top of IEnumerable&lt;T&gt; - three of which are <em>only</em> about mutation. The rest of the methods have been made redundant by LINQ. </p> <h3 id="1fdea7347f3a48668994b00bb524c596"> LINQ and the LSP <a href="#1fdea7347f3a48668994b00bb524c596" title="permalink">#</a> </h3> <p> In a <a href="/2012/03/26/IQueryableTisTightCoupling">previous post, I've talked about the conflict between IQueryable and the LSP</a>, but even constraining the discussion to LINQ to Objects, it turns out that LINQ has lots of built-in LSP violations. </p> <p> Recall the essence of the LSP: <em>you should be able to pass any implementation of an interface to a client without changing the correctness of the system</em>. While 'correctness' is application-specific, the lowest common denominator must be that if a method works for one implementation, it mustn't throw exceptions for another implementation. However, consider these two implementations of IEnumerable&lt;string&gt;: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">new</span>[] { <span style="color: #a31515;">"foo"</span>, <span style="color: #a31515;">"bar"</span>, <span style="color: #a31515;">"baz"</span> };</pre> </p> <p> and this one: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">InfiniteStrings</span> : <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">string</span>&gt; { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IEnumerator</span>&lt;<span style="color: blue;">string</span>&gt; GetEnumerator() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">while</span> (<span style="color: blue;">true</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">yield</span> <span style="color: blue;">return</span> <span style="color: #a31515;">"foo"</span>; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; System.Collections.<span style="color: #2b91af;">IEnumerator</span> System.Collections.<span style="color: #2b91af;">IEnumerable</span>.GetEnumerator() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.GetEnumerator(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> As I've already discussed, invoking ToArray (or ToList) on these two implementations changes the correctness of the system, because the second implementation (the infinite Iterator) will cause an exception to be thrown. In fact, as far as I can tell, the only LSP-compliant LINQ methods are: <ul> <li>Any()</li> <li>AsEnumerable()</li> <li>Concat(IEnumerable&lt;T&gt;)</li> <li>DefaultIfEmpty()</li> <li>DefaultIfEmpty(T)</li> <li>Distinct (maybe...)</li> <li>Distinct(IEqualityComparer&lt;T&gt;) (maybe...)</li> <li>ElementAt(int)</li> <li>ElementAtOrDefault(int)</li> <li>First()</li> <li>FirstOrDefault()</li> <li>OfType&lt;TResult&gt;()</li> <li>Select(Func&lt;TSource, TResult&gt;)</li> <li>Select(Func&lt;TSource, int, TResult&gt;)</li> <li>SelectMany(Func&lt;TSource, IEnumerable&lt;TResult&gt;&gt;)</li> <li>SelectMany(Func&lt;TSource, int, IEnumerable&lt;TResult&gt;&gt;)</li> <li>SelectMany(Func&lt;TSource, IEnumerable&lt;TCollection&gt;&gt;, Func&lt;TSource, TCollection, TResult&gt;)</li> <li>SelectMany(Func&lt;TSource, int, IEnumerable&lt;TCollection&gt;&gt;, Func&lt;TSource, TCollection, TResult&gt;)</li> <li>Single()</li> <li>SingleOrDefault()</li> <li>Skip(int)</li> <li>Take(int)</li> <li>Where(Func&lt;TSource, bool&gt;)</li> <li>Where(Func&lt;TSource, int, bool&gt;)</li> <li>Zip(IEnumerable&lt;TSecond&gt;, Func&lt;TFirst, TSecond, TResult&gt;)</li> </ul> If you can get by with this subset of LINQ, you should be safe. If not, you're back at the choice of either accepting IEnumerable&lt;T&gt;, or requesting an array; break the LSP or break the Robustness Principle. </p> <p> This actually demonstrates a need for a 'Finite Iterator' interface, and I have to admit that, before researching for this article, I'd never heard about <a href="http://msdn.microsoft.com/en-us/library/hh881542.aspx">IReadOnlyCollection&lt;T&gt;</a>, but there it is; it's seems to be a new interface in .NET 4.5. I think I'll begin to use this interface some more! </p> <h3 id="47c6fadbc4b744edb352c0b46b41be78"> Summary <a href="#47c6fadbc4b744edb352c0b46b41be78" title="permalink">#</a> </h3> <p> The bottom line is that defensive copying of IEnumerable&lt;T&gt; into arrays should be avoided. If you can get by with the LSP-compliant subset of LINQ, then all is good (but consider writing a couple of unit tests that involve infinite Iterators). If you require a finite sequence and you're on .NET 4.5, require IReadOnlyCollection&lt;T&gt; as an argument instead of IEnumerable&lt;T&gt;. If you require a finite sequence and you're not on .NET 4.5, ask for an array as an argument (and consider adding some unit tests that verify that your implementation doesn't mutate the array). </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Defensive coding https://blog.ploeh.dk/2013/07/08/defensive-coding 2013-07-08T19:11:00+00:00 Mark Seemann <div id="post"> <p> <em>This post examines the advantages and disadvantages of defensive coding.</em> </p> <p> One of my readers, Barry Giles, recently wrote me and asked a question that I think is interesting enough to warrant a discussion: </p> <blockquote> <p> "Recently I came up against an interesting situation at work: I was getting a review and I had put defensive checks in – one for a null argument check against a constructor parameter, one for a null check on a property return value. I also had some assertions for private methods which I tend to use to state my assumptions. </p> <p> "It seems the prevailing mood in my team is to not have any of the checks and just let it fail. To be honest I’m struggling with this concept as I am so used to developing in this way and thought it was good practice. I’m pretty sure it’s in most tutorials and blog posts. </p> <p> "Can you advise on why it’s better to code defensively like this instead of just letting it fail and checking the stack trace please?" </p> </blockquote> <p> Actually, I don't think defensive coding necessarily is <em>better</em>; I also don't think it's <em>worse</em>. As usual, there are different forces in play, so the short answer is the usual: <em>it depends</em>. </p> <p> That answer is obviously useless unless you can answer the question: <em>on what does it depend?</em> In this article, I'll examine some of the forces at play. However, I'm not going to claim that this will be an exhaustive treatment of the subject. </p> <h3 id="4def89b80ce64243b0dbec2d51474ab4"> Letting it fail <a href="#4def89b80ce64243b0dbec2d51474ab4" title="permalink">#</a> </h3> <p> What is the value of defensive coding, if all you're going to do is to immediately throw an exception? Wouldn't it be just as good to let the code fail? Let's look at some code. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ProductDetailsController</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IUserRepository</span> userRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IProductRepository</span> productRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">ICurrencyExchange</span> exchange; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ProductDetailsController( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IUserRepository</span> userRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IProductRepository</span> productRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ICurrencyExchange</span> exchange) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository = userRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.productRepository = productRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange = exchange; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> GetProductDetails( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> userId, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> productId) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> user = <span style="color: blue;">this</span>.userRepository.Get(userId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> targetCurrency = user.PreferredCurrency; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> product = <span style="color: blue;">this</span>.productRepository.Get(productId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> convertedPrice = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange.Convert(product.UnitPrice, targetCurrency); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Name = product.Name, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Price = convertedPrice.ToString() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> In this simple ProductDetailsController, the GetProductDetails method produces a ProductDetailsViewModel instance by getting user and product information from injected Repositories, converting the product unit price to the user's desired currency, and returning data about the product for display purposes. For the sake of argument, let's focus only on null issues. How many things could go wrong in the GetProductDetails method? How many objects can be null? </p> <p> Quite a few, it turns out. Even decoupled from its dependencies, this small piece of code can throw a NullReferenceException in at least six different ways. Imagine that you receive an error report from your production system. The stack trace points to the ProductDetailsViewModel method, and the exception is a NullReferenceException. Which of the six possible nulls caused the error? </p> <p> By just letting the system fail, it's hard to answer that question. Keep in mind that this is even a demo example. Most production code I've seen has more than five to six lines of code, so looking for the cause of an error report can quickly become like looking for a needle in a haystack. </p> <p> Just letting the code fail isn't particularly helpful. Obviously, if you write very short methods (a practice I highly recommend), the problem is less pronounced, but the longer your methods are, the less professional I think 'just letting it fail' sounds. </p> <h3 id="1243a85d64164b689723bee4ea5efa1b"> Defensive coding to the rescue? <a href="#1243a85d64164b689723bee4ea5efa1b" title="permalink">#</a> </h3> <p> By adding explicit guards against null, you can throw more descriptive exception messages: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ProductDetailsController</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IUserRepository</span> userRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IProductRepository</span> productRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">ICurrencyExchange</span> exchange; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ProductDetailsController( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IUserRepository</span> userRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IProductRepository</span> productRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ICurrencyExchange</span> exchange) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (userRepository == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"userRepository"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (productRepository == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"productRepository"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (exchange == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"exchange"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository = userRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.productRepository = productRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange = exchange; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> GetProductDetails( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> userId, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> productId) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> user = <span style="color: blue;">this</span>.userRepository.Get(userId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (user == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"User was null."</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> targetCurrency = user.PreferredCurrency; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> product = <span style="color: blue;">this</span>.productRepository.Get(productId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (product == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Product was null."</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> convertedPrice = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange.Convert(product.UnitPrice, targetCurrency); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (convertedPrice == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Converted price was null."</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Name = product.Name, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Price = convertedPrice.ToString() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Is this better? From a troubleshooting perspective it is, because with this version of the code, if you get an error message from your production system, the exception message will tell you exactly which of the six null situations your program encountered. If I were in maintenance mode, I know which of the two versions I'd like to support. </p> <p> However, from a readability perspective, you're much worse off. The GetProductDetails method grew from five to eleven lines of code. Defensive coding more than doubled the line count! The flow through the method drowns in guard clauses, so it's less readable now. If you're a typical programmer, you read code much more than you write it, so a practice that makes it harder to read code should trigger your code smell sense. No wonder that many programmers consider defensive coding a bad practice. </p> <h3 id="b07be9e0f36d4d6ab9c382868e04dac4"> Robustness <a href="#b07be9e0f36d4d6ab9c382868e04dac4" title="permalink">#</a> </h3> <p> Is it possible to balance the forces at play here? Yes it is, but in order to understand how, you need to understand the root cause of the problem. The original code example isn't particularly complicated, but even so, there are so many ways it can fail. When it comes to null references, the cause is a <a href="http://en.wikipedia.org/wiki/Tony_Hoare#Quotations">language design mistake</a>, but in general, the question is whether or not you can trust input. The return values from invoking IUserRepository.Get is <a href="http://xunitpatterns.com/indirect%20input.html">(indirect) input</a> too. </p> <p> Depending on the environment in which your program is running, you may or may not be able to trust input. Consider, for a moment, the situation where your software is running <a href="/2012/12/18/RangersandZookeepers">in the wild</a>. It may be a reusable library or framework. In this case, you can't trust input at all. If fact, you may want to apply <a href="http://en.wikipedia.org/wiki/Robustness_principle">the robustness principle</a> and make sure that, not only are you going to be very defensive about input, but you're also going to be careful and nice about the output of your program. In other words, you don't want to pass null (or other evil values) to your collaborators. </p> <p> The sample code presented here may pass null to its dependencies, e.g. if <code>userId</code> is null, or (more subtly) if <code>user.PreferredCurrency</code> is null. Thus, to apply the robustness principle, you'd have to add even more defensive coding: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ProductDetailsController</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IUserRepository</span> userRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IProductRepository</span> productRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">ICurrencyExchange</span> exchange; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ProductDetailsController( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IUserRepository</span> userRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IProductRepository</span> productRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ICurrencyExchange</span> exchange) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (userRepository == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"userRepository"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (productRepository == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"productRepository"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (exchange == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"exchange"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository = userRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.productRepository = productRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange = exchange; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> GetProductDetails( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> userId, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> productId) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (userId == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"userId"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (productId == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"productId"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> user = <span style="color: blue;">this</span>.userRepository.Get(userId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (user == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"User was null."</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (user.PreferredCurrency == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Preferred currency was null."</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> targetCurrency = user.PreferredCurrency; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> product = <span style="color: blue;">this</span>.productRepository.Get(productId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (product == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Product was null."</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (product.Name == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Product name was null."</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (product.UnitPrice == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Unit price was null."</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> convertedPrice = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange.Convert(product.UnitPrice, targetCurrency); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (convertedPrice == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Converted price was null."</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Name = product.Name, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Price = convertedPrice.ToString() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This is clearly even more horrendous than the previous defensive programming example. Now you're not only defending yourself, but also standing up for your collaborators. Noble, but unreadable. </p> <p> Still, when I write wildlife code, this is basically what I do, although I'd tend to refactor my code so that <em>first</em> I'd collect and check all the input, and <em>then</em> I'd pass on that input to another class that performs the logic. </p> <h3 id="a6a7c8a73aec4db7917051ab467eed6b"> Protected enclosures <a href="#a6a7c8a73aec4db7917051ab467eed6b" title="permalink">#</a> </h3> <p> What if your code is a <a href="/2012/12/18/RangersandZookeepers">zoo animal</a>? What if your code is running in an environment, where all the collaborators are other classes also part of the same code base, written by you and your colleagues? If you can trust each other to follow some consistent rules, you could skip much of the defensive coding. </p> <p> In most of the teams I work with, I always suggest that we adopt the robustness principle. In practice, that means that null is <em>never</em> an acceptable return value. If a class member returns null, the bug is in that class, not in the consumer. Following that team rule, the code example can be reduced to this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ProductDetailsController</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IUserRepository</span> userRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IProductRepository</span> productRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">ICurrencyExchange</span> exchange; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ProductDetailsController( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IUserRepository</span> userRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IProductRepository</span> productRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ICurrencyExchange</span> exchange) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (userRepository == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"userRepository"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (productRepository == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"productRepository"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (exchange == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"exchange"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository = userRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.productRepository = productRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange = exchange; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> GetProductDetails( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> userId, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> productId) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (userId == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"userId"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (productId == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"productId"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> user = <span style="color: blue;">this</span>.userRepository.Get(userId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (user.PreferredCurrency == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Preferred currency was null."</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> targetCurrency = user.PreferredCurrency; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> product = <span style="color: blue;">this</span>.productRepository.Get(productId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (product.Name == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Product name was null."</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (product.UnitPrice == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Unit price was null."</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> convertedPrice = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange.Convert(product.UnitPrice, targetCurrency); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Name = product.Name, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Price = convertedPrice.ToString() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> That's better, but not quite good enough yet... but wait: readable properties are return values too, so you shouldn't have to check for those either: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ProductDetailsController</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IUserRepository</span> userRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IProductRepository</span> productRepository; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">ICurrencyExchange</span> exchange; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ProductDetailsController( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IUserRepository</span> userRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IProductRepository</span> productRepository, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ICurrencyExchange</span> exchange) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (userRepository == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"userRepository"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (productRepository == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"productRepository"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (exchange == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"exchange"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.userRepository = userRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.productRepository = productRepository; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange = exchange; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> GetProductDetails( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> userId, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> productId) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (userId == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"userId"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (productId == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"productId"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> user = <span style="color: blue;">this</span>.userRepository.Get(userId); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> targetCurrency = user.PreferredCurrency; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> product = <span style="color: blue;">this</span>.productRepository.Get(productId); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> convertedPrice = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.exchange.Convert(product.UnitPrice, targetCurrency); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ProductDetailsViewModel</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Name = product.Name, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Price = convertedPrice.ToString() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This is pretty good, because now you're almost back where you started. The only difference is the <a href="http://c2.com/cgi/wiki?GuardClause">Guard Clauses</a> at the beginning of each member. When following the robustness principle, most members tend to look like that. After a while, you get used to that, and you don't really see them any longer. I consider them a sort of preamble for each member. As a code reader, you can skip the Guard Clauses and concentrate on the program flow, without any interrupting defense checks interfering with the readability of the code. </p> <h3 id="6e1daa03dd7f422299eab920800af770"> Encapsulation <a href="#6e1daa03dd7f422299eab920800af770" title="permalink">#</a> </h3> <p> If it's an error to return null, then how does the User class, or the Product class, adhere to the robustness principle? In exactly the same way: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">User</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> preferredCurrency; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> User() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.preferredCurrency = <span style="color: #a31515;">"DKK"</span>; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> PreferredCurrency &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">this</span>.preferredCurrency; } &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">value</span> == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"value"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.preferredCurrency = <span style="color: blue;">value</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Notice how the User class <a href="/2011/05/26/CodeSmellAutomaticProperty">protects its invariants</a>. The PreferredCurrency property can never be null. This principle is also known by another name: it's called <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">encapsulation</a>. </p> <h3 id="54a2fd1349d64a0abf30f830818c1fe9"> Summary <a href="#54a2fd1349d64a0abf30f830818c1fe9" title="permalink">#</a> </h3> <p> As always, it helps to understand the forces working or your code base. You have to be much more defensive if your code is running in the wild, than if it's running in a protected environment. Still, I generally think that it's a fallacy if you believe that you can get by with writing sloppy code; <a href="/2012/12/18/ZookeepersmustbecomeRangers">we should all be Ranger programmers</a>. </p> <p> Unstructured defensive coding hurts readability; it's just another excuse for writing Spaghetti Code. Conversely, structured defensive coding is encapsulation. I know what I prefer. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. NDC 2013 session recordings https://blog.ploeh.dk/2013/06/27/ndc-2013-session-recordings 2013-06-27T14:47:00+00:00 Mark Seemann <div id="post"> <p> <em>NDC 2013 session recordings are now available, including mine.</em> </p> <p> The <a href="http://www.ndcoslo.com/">NDC 2013</a> conference is over, and most (if not all) of the session recordings <a href="http://vimeo.com/ndcoslo">are now available</a>. Specifically, both of the talks I gave were recorded and are now available for general viewing. <ul> <li> <a href="http://vimeo.com/68236489">Faking Homoiconicity in C# with graphs</a> </li> <li> <a href="http://vimeo.com/68378923">Big Object Graphs Up Front</a> </li> </ul> As a bonus, <a href="http://lostechies.com/jimmybogard/">Jimmy Bogard</a> gave a talk on <a href="http://vimeo.com/68390508">Holistic testing</a> with quite a bit of <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> content. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A heuristic for formatting code according to the AAA pattern https://blog.ploeh.dk/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern 2013-06-24T19:20:00+00:00 Mark Seemann <div id="post"> <p> <em>This article describes a rule of thumb for formatting unit tests.</em> </p> <p> The Arrange Act Assert (AAA) pattern is one of the most fundamental and important patterns for writing maintainable unit tests. It states that you should separate each test into three phases (Arrange, Act, and Assert). </p> <p> Like most other code, unit tests are read more than they are written, so it's important to make the tests readable. This article presents one way to make it easy for a test reader easily to distinguish the three AAA phases of a test method. </p> <h3 id="8757cb773c6c4c6abc6bea7e420bb8e7"> The way of AAA <a href="#8757cb773c6c4c6abc6bea7e420bb8e7" title="permalink">#</a> </h3> <p> The technique is simple: <ul> <li> As long as there are less than three lines of code in a test, they appear without any special formatting or white space. </li> <li> When a test contains more than three lines of code, you separate the three phases with a blank line. </li> <li> When a single phase contains so many lines of code that you'll need to divide it into subsections to make it readable, you should explicitly mark the beginning of each phase with a code comment. </li> </ul> This way avoids the use of comments until they are unavoidable; at that time, you should consider whether the need for a comment constitutes a code smell. </p> <h3 id="19e02fa0a7de45b18d41f87b6854f3fb"> Motivating example <a href="#19e02fa0a7de45b18d41f87b6854f3fb" title="permalink">#</a> </h3> <p> Many programmers use the AAA pattern by explicitly demarking each phase with a code comment: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> UseBasketPipelineOnExpensiveBasket() { &nbsp;&nbsp;&nbsp; <span style="color: green;">// Arrange</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> basket = <span style="color: blue;">new</span> <span style="color: #2b91af;">Basket</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Chocolate"</span>, 50, 3), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Gruy&#232;re"</span>, 45.5m, 1), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Barolo"</span>, 250, 2)); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CompositePipe</span>&lt;<span style="color: #2b91af;">Basket</span>&gt; pipeline = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketPipeline</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Act</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = pipeline.Pipe(basket); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Assert</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">Basket</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Chocolate"</span>, 50, 3), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Gruy&#232;re"</span>, 45.5m, 1), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Barolo"</span>, 250, 2), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">Discount</span>(34.775m), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">Vat</span>(165.18125m), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketTotal</span>(825.90625m)); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(expected, actual); }</pre> </p> <p> Notice the use of code comments to indicate the beginning of each of the three phases. </p> <p> Given an example like the above, this seems like a benign approach, but mandatory use of code comments starts to fall apart when tests are very simple. </p> <p> Consider this Structural Inspection test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsBasketElement() { &nbsp;&nbsp;&nbsp; <span style="color: green;">// Arrange</span> &nbsp;&nbsp;&nbsp; <span style="color: green;">// Act?</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">Vat</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Assert</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(sut); }</pre> </p> <p> Notice the question mark after the <code>// Act</code> comment. It seems that the writer of the test was unsure if the act of creating an instance of the System Under Test (SUT) constitutes the Act phase. </p> <p> You could just as well argue that creating the SUT is part of the Arrange phase: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsBasketElement() { &nbsp;&nbsp;&nbsp; <span style="color: green;">// Arrange</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">Vat</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Act</span> &nbsp;&nbsp;&nbsp; <span style="color: green;">// Assert</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(sut); }</pre> </p> <p> However, now the Act phase is empty. Clearly, using code comments to split two lines of code into three phases is not helpful to the reader. </p> <h3 id="46ca6398838d4904b116a509d2420fa5"> Three lines of code and less <a href="#46ca6398838d4904b116a509d2420fa5" title="permalink">#</a> </h3> <p> Here's a simpler alternative: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsBasketElement() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">Vat</span>(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(sut); }</pre> </p> <p> When there's only two lines of code, the test is so simple that you don't need help from code comments. If you wanted, you could even reduce that test to a single line of code, by inlining the <code>sut</code> variable: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsBasketElement() { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(<span style="color: blue;">new</span> <span style="color: #2b91af;">Vat</span>()); }</pre> </p> <p> Such a test is either a degenerate case of AAA where one or more phase is empty, or else it doesn't really fit into the AAA pattern at all. In these cases, code comments are only in the way, so it's better to omit them. </p> <p> Even if you have a test that you can properly divide into the three distinct AAA phases, you don't need comments or formatting if it's only three lines of code: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(<span style="color: #a31515;">""</span>, <span style="color: #a31515;">""</span>, 1, 1, 1, 1, <span style="color: blue;">true</span>)] [<span style="color: #2b91af;">InlineData</span>(<span style="color: #a31515;">"foo"</span>, <span style="color: #a31515;">""</span>, 1, 1, 1, 1, <span style="color: blue;">false</span>)] [<span style="color: #2b91af;">InlineData</span>(<span style="color: #a31515;">""</span>, <span style="color: #a31515;">"bar"</span>, 1, 1, 1, 1, <span style="color: blue;">false</span>)] [<span style="color: #2b91af;">InlineData</span>(<span style="color: #a31515;">"foo"</span>, <span style="color: #a31515;">"foo"</span>, 1, 1, 1, 1, <span style="color: blue;">true</span>)] [<span style="color: #2b91af;">InlineData</span>(<span style="color: #a31515;">"foo"</span>, <span style="color: #a31515;">"foo"</span>, 2, 1, 1, 1, <span style="color: blue;">false</span>)] [<span style="color: #2b91af;">InlineData</span>(<span style="color: #a31515;">"foo"</span>, <span style="color: #a31515;">"foo"</span>, 2, 2, 1, 1, <span style="color: blue;">true</span>)] [<span style="color: #2b91af;">InlineData</span>(<span style="color: #a31515;">"foo"</span>, <span style="color: #a31515;">"foo"</span>, 2, 2, 2, 1, <span style="color: blue;">false</span>)] [<span style="color: #2b91af;">InlineData</span>(<span style="color: #a31515;">"foo"</span>, <span style="color: #a31515;">"foo"</span>, 2, 2, 2, 2, <span style="color: blue;">true</span>)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> EqualsReturnsCorrectResult( &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> sutName, &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> otherName, &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> sutUnitPrice, &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> otherUnitPrice, &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> sutQuantity, &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> otherQuantity, &nbsp;&nbsp;&nbsp; <span style="color: blue;">bool</span> expected) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(sutName, sutUnitPrice, sutQuantity); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.Equals( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(otherName, otherUnitPrice, otherQuantity)); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(expected, actual); }</pre> </p> <p> Three lines of code, and three phases of AAA; I think it's obvious what goes where - even if this single test method captures eight different test cases. </p> <h3 id="77fd4d083b8541eab04a75783bd46719"> Simple tests with more than three lines of code <a href="#77fd4d083b8541eab04a75783bd46719" title="permalink">#</a> </h3> <p> When you have more than three lines of code, you'll need to help the reader identify what goes where. As long as you can keep it simple, I think that you accomplish this best with simple whitespace: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> UseBasketPipelineOnExpensiveBasket() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> basket = <span style="color: blue;">new</span> <span style="color: #2b91af;">Basket</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Chocolate"</span>, 50, 3), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Gruy&#232;re"</span>, 45.5m, 1), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Barolo"</span>, 250, 2)); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CompositePipe</span>&lt;<span style="color: #2b91af;">Basket</span>&gt; pipeline = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketPipeline</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = pipeline.Pipe(basket); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">Basket</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Chocolate"</span>, 50, 3), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Gruy&#232;re"</span>, 45.5m, 1), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItem</span>(<span style="color: #a31515;">"Barolo"</span>, 250, 2), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">Discount</span>(34.775m), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">Vat</span>(165.18125m), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketTotal</span>(825.90625m)); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(expected, actual); }</pre> </p> <p> This is the same test as in the motivating example, only with the comments removed. The use of whitespace makes it easy for you to identify three phases in the method, so comments are redundant. </p> <p> As long as you can express each phase without using whitespace <em>within</em> each phase, you can omit the comments. The only whitespace in the test marks the boundaries between each phase. </p> <h3 id="90a06ee1b1f84aa6bbe09819f03b13a5"> Complex tests requiring more whitespace <a href="#90a06ee1b1f84aa6bbe09819f03b13a5" title="permalink">#</a> </h3> <p> If your tests grow in complexity, you may need to divide the code into various sub-phases in order to keep it readable. When this happens, you'll have to resort to using code comments to demark the phases, because use of only whitespace would be ambiguous: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PipeReturnsCorrectResult() { &nbsp;&nbsp;&nbsp; <span style="color: green;">// Arrange</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> r = <span style="color: blue;">new</span> <span style="color: #2b91af;">MockRepository</span>(<span style="color: #2b91af;">MockBehavior</span>.Default) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DefaultValue = <span style="color: #2b91af;">DefaultValue</span>.Mock &nbsp;&nbsp;&nbsp; }; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> v1Stub = r.Create&lt;<span style="color: #2b91af;">IBasketVisitor</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> v2Stub = r.Create&lt;<span style="color: #2b91af;">IBasketVisitor</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> v3Stub = r.Create&lt;<span style="color: #2b91af;">IBasketVisitor</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> e1Stub = r.Create&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> e2Stub = r.Create&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(); &nbsp;&nbsp;&nbsp; e1Stub.Setup(e =&gt; e.Accept(v1Stub.Object)).Returns(v2Stub.Object); &nbsp;&nbsp;&nbsp; e2Stub.Setup(e =&gt; e.Accept(v2Stub.Object)).Returns(v3Stub.Object); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> newElements = <span style="color: blue;">new</span>[] &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r.Create&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;().Object, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r.Create&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;().Object, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r.Create&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;().Object, &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; v3Stub &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Setup(v =&gt; v.GetEnumerator()) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Returns(newElements.AsEnumerable().GetEnumerator()); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketVisitorPipe</span>(v1Stub.Object); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Act</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> basket = <span style="color: blue;">new</span> <span style="color: #2b91af;">Basket</span>(e1Stub.Object, e2Stub.Object); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Basket</span> actual = sut.Pipe(basket); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Assert</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.True(basket.Concat(newElements).SequenceEqual(actual)); }</pre> </p> <p> In this example, the Arrange phase is so complicated that I've had to divide it into various sections in order to make it just a bit more readable. Since I've had to use whitespace to indicate the various sections, I need another mechanism to indicate the three AAA phases. Code comments is an easy way to do this. </p> <p> As <a href="http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode">Tim Ottinger described back in 2006</a>, code comments are apologies for not making the code clear enough. A code comment is a code smell, because it means that the code itself isn't sufficiently self-documenting. This is also true in this case. </p> <p> Whenever I need to add code comments to indicate the three AAA phases, an alarm goes off in my head. Something is wrong; the test is too complex. It would be better if I could refactor either the test or the SUT to become simpler. </p> <p> When TDD’ing, I tend to accept the occasional complicated unit test method, but if I seem to be writing too many complicated unit tests, it's time to stop and think. </p> <h3 id="31360d48813a49beb6ecc9d6dbe56afd"> Summary <a href="#31360d48813a49beb6ecc9d6dbe56afd" title="permalink">#</a> </h3> <p> In <a href="http://amzn.to/VI81bP">Growing Object-Oriented Software, Guided by Tests</a>, one of the most consistent pieces of advice is that you should listen to your tests. If your tests are too hard to write; if your tests are too complicated, it's time to consider alternatives. </p> <p> How do you know when a test has become too complicated? If you need to added code comments to it, it probably is. </p> <p> <em> This article first appeared in the <a href="http://issuu.com/developermagazine/docs/developer_ndc_2013_web">The Developer No 2/2013</a>. It's reprinted here with kind permission. </em> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8bf71461766d414b91a2603be57c6918"> <div class="comment-author"><a href="https://www.linkedin.com/in/romainvasseur/">Romain Vasseur</a> <a href="#8bf71461766d414b91a2603be57c6918">#</a></div> <div class="comment-content"> <p> Currently reading your last book <b>Code That Fits in Your Head</b> (Lucky draw, thank you for writing!!) and find this section. I used several techniques to refactor those unbalanced structure. A recent one is to leverage <code>local function</code> to gather noise, top structure acting like <code>TOC</code> when bottom one provides insight for those interested. More or less like <code>template method</code> but keeping stuff internally. I find it particularly useful when such noise is <i>one-shot</i>. Extracting it into <code>private method</code> or <code>fixture</code> does not smell good to me. </p> <p> <pre>[Theory] [InlineData(194, 107, 37, "#C26B25")] [InlineData(66, 138, 245, "#428AF5")] public void Mapper(int r, int g, int b, string hex) { var (sut, source, expected) = Arrange(); var res = sut.Convert(source); Assert.Equal(expected, source); (Sut sut, Color1 color, Color2 color) Arrange() { // Do complex or verbose stuff eg: // Create C1 from hex // Create c2 from {r,g,b} // Create SUT return (s, c1, c2); } }</pre> </p> <p> I am used to apply the same pattern to process impure method: I provide a <code>local pure function</code> that I leverage above using instance filed, I/O, ... It is a good first step instead of right away create a private helper or equivalent. Then , if it apperas it can be reused, you only have to promote the <code>local function</code>. </p> </div> <div class="comment-date">2022-02-03 17:59 UTC</div> </div> <div class="comment" id="c9a14538f4154f71a5ae17a50cb5f753"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c9a14538f4154f71a5ae17a50cb5f753">#</a></div> <div class="comment-content"> <p> Romain, thank you for writing. Local functions can indeed be useful (also) in unit tests. The main use, as you imply, is as a cheaper alternative to the Template Method pattern. The main benefit, compared to private helper methods, is that you limit the scope of the method. </p> <p> On the other hand, a local function still involves more <a href="/2019/12/16/zone-of-ceremony">ceremony</a> than a lambda expression, which is often a better alternative. </p> <p> With regards to your particular example, do you gain anything from moving the Arrange phase down? </p> <p> Indeed, the three AAA phases may be more apparent. You're also following the principle of leading with the big overview, and then leaving the implementation details for later. I can never remember who originally described this principle for organising code, but I find it useful. (It's one the main challenges with F#, because of its single-pass compiler. That language feature, however, <a href="/2015/04/15/c-will-eventually-get-all-f-features-right">provides some other great benefits</a>.) </p> <p> Still, isn't this mostly symptomatic relief? I wouldn't mind doing something like this if I could think of nothing better, but I'd still view the need for a complex Arrange phase as being a code smell. Not a test smell, but as feedback that the System Under Test is too complicated. </p> </div> <div class="comment-date">2022-02-04 12:06 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Where to put unit tests https://blog.ploeh.dk/2013/06/17/where-to-put-unit-tests 2013-06-17T09:22:00+00:00 Mark Seemann <div id="post"> <p> <em>This article provides arguments for (and against) putting unit tests in libraries different from the production code.</em> </p> <p> One of my readers ask me "whether unit tests should be done in separate assemblies or if they should be done in [...] the production assemblies?" As he puts it, he "always took for granted that the test should go into separate assemblies," but is this really true, and if it is: what are the arguments? </p> <p> Despite the fundamental nature of this question, I haven't seen much explicit treatment of it, and to answer it, I had to pause and think. The key to the answer, I think, is to first understand <em>why</em> you write automated tests at all. (This way of thinking about questions and answers is closely related to the (Lean) technique of <a href="http://en.wikipedia.org/wiki/5_Whys">5 whys</a>.) </p> <p> As always, the short answer is that <em>it depends</em>, but that's not very helpful, so here are some common reasons. </p> <h3 id="8c1119b05daf4e1db8d2043895495ebd"> Regression testing <a href="#8c1119b05daf4e1db8d2043895495ebd" title="permalink">#</a> </h3> <p> In my experience, most people focus on the <em>quality control</em> aspect of automated testing. If your only purpose of having automated tests is to have a good <a href="http://en.wikipedia.org/wiki/Regression_testing">regression test</a> suite, then it seems that it doesn't really matter where the tests go. </p> <p> Still, even if that's your only reason for unit testing, I think putting the unit tests in a separate library is the best choice: <ul> <li> It gives you a clearer separation of concerns. If you put unit tests in your production library, it will blur the distinction between production code and test code. How do you know which code to test? How do you know what not to test? There are different ways to address these concerns (namespaces, TDD), but I think the <em>simplest</em> way to deal with such issues is to put the tests in a separate library. (This argument reminds me a little of <a href="http://en.wikipedia.org/wiki/Occam's_razor">Occam's razor</a>.) </li> <li> Putting unit tests in your production code will make your compiled libraries bigger. It will take (slightly) more time to load the libraries into memory, and they will take up more memory. This probably doesn't matter at all on a big server, but may be important if your production code runs on a (small) mobile device. Once again, it depends. </li> <li> The more code you put in your production software, the larger the potential attack surface becomes. Have you performed a security analysis of your unit tests? It's much easier to put the tests in a separate library that you never deploy to production, because it means that you don't have to waste valuable time and resources doing a security analysis of your test code. </li> </ul> </p> <p> In short, if you put unit tests in the same library as your production code, all that unit test <em>must</em> adhere to the same standards as your production code. Obviously, if you have no standards for your production code, this doesn't apply, but then you probably have bigger problems. </p> <h3 id="d39d4478d256490c88936f0cfd96cdfe"> API design feedback <a href="#d39d4478d256490c88936f0cfd96cdfe" title="permalink">#</a> </h3> <p> While I find regression testing useful in itself, I find Test-Driven Development (TDD) particularly valuable because it provides feedback about the API I'm building. In the spirit of <a href="http://amzn.to/VI81bP">GOOS</a>, if the test is difficult to write, it's probably because the API is difficult to use. You'll only get this feedback if you test against the public API of your code; the unit test is <a href="/2011/11/10/TDDimprovesreusability">the first client of your API</a>. </p> <p> However, if your unit tests reside in the same library as your production code, you can easily invoke internal classes and members from your unit tests. You simply lose the opportunity to get valuable feedback about the usability of your code. (Note that this argument also applies to the use of the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx">[InternalsVisibleTo] attribute</a>, which means that you should never use it.) </p> <p> For me, this reason alone is enough that I always prefer putting unit tests in separate libraries. </p> <h3 id="f723099dc8bc4059b31f435f3e2e1e05"> Shipping production code with tests <a href="#f723099dc8bc4059b31f435f3e2e1e05" title="permalink">#</a> </h3> <p> Although I think that there are strong arguments against putting unit tests in production code, there may also be reasons in favor. As <a href="http://stackoverflow.com/a/554732/126014">one answer on Stack Overflow</a> describes, shipping your production code with a test suite can be valuable as a self-diagnostics tool. This might be particularly valuable if you deploy your production code to heterogeneous environments, and you're unable to predict and test the configuration of all environments before you ship your code. </p> <h3 id="df26561d1944421180503e2e9f193d05"> Summary <a href="#df26561d1944421180503e2e9f193d05" title="permalink">#</a> </h3> <p> In the end, the answer depends on your context. If you understand why you (want to) have unit tests, you'll be able to answer the initial question: <em>should unit tests go in a separate library, or can they go in the same library as the production code?</em> In <em>mainstream scenarios</em> I will strongly recommend putting tests in a separate library, but you may have special circumstances where it makes sense to put the tests together with the production code. As long as you understand the pros and cons, you can make your own informed decision. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="bf054f135342493aa939fbeb7ce29443"> <div class="comment-author">Stefan Olofsson <a href="#bf054f135342493aa939fbeb7ce29443">#</a></div> <div class="comment-content">Hello Mark,<br> <br> Another big issue with having tests in your production code is that you will drag along depencencies to test libraries. You mention this in the second issue above but I think it would be a good idea to state it more explicitly. </div> <div class="comment-date">2013-06-17 14:28</div> </div> <div class="comment" id="5381530a7d9c4d4fabb15644aefe9a91"> <div class="comment-author">Anthony Leatherwood <a href="#5381530a7d9c4d4fabb15644aefe9a91">#</a></div> <div class="comment-content"> I had always put my unit tests in another assembly. But recently a colleague and I started to dislike the fact that the unit tests weren't in close enough proximity to the class they tested. As well you might not know tests even existed unless you went digging around in the solution. As a result, we've started putting our tests behind the cs file they are testing (like the code-behind of a winform). All you have to do is expand the cs to see the tests behind it. I know the reasons for not putting the tests in production are sound, but I have not regretted putting them behind the class once. As for the dependencies, if they aren't invoked, they are never loaded and don't even have to exist in the production environment. For us, this hasn't presented any problems. For what it's worth, we are "zookeepers" and not "rangers". If it were the other way around, it's possible we would reconsider. </div> <div class="comment-date">2013-06-19 17:28</div> </div> <div class="comment" id="48f77cee86584137a0d7a1de23a8e78f"> <div class="comment-author">Jeff Soper <a href="#48f77cee86584137a0d7a1de23a8e78f">#</a></div> <div class="comment-content"> <p>Could you share your opinion on bundling test projects with their target projects in source control repositories? I use Git, but I would imagine this is applicable in many source control implementations.</p> <p>I currently keep my tests in their own projects, as you generally recommend here, and also track them in their own Git repository, separately from the tested project. However, I'm finding that when I create a new branch to add a new feature in the project, I create the same branch in the test project. This is critical especially in situations where the new behavior necessarily causes existing tests to fail. If I forget to create a new branch in the test project to match the new branch in the project under test, it can sometimes lead to annoying clean-up work that was preventable.</p> <p>I am beginning to question why I'm not combining the test project and the tested project in the same Git repository, so that when I branch out on a new idea, I only need to do it once, and my tests are forever in sync with my tested code. I haven't come across a scenario where that would cause problems - can you think of any compelling reason not to keep the projects in the same source control repository?</p> </div> <div class="comment-date">2013-12-04 09:30</div> </div> <div class="comment" id="66e12b4d8dd94478ac747b04a7bc8323"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#66e12b4d8dd94478ac747b04a7bc8323">#</a></div> <div class="comment-content"> <p> Jeff, thank you for writing. </p> <p> If I were to exaggerate a bit, I would claim that I've used Test-Driven Development (TDD) longer than I've used source control systems, so it has never occurred to me to keep tests in a separate repository. It sounds really... inconvenient, but I've never tried it, so I don't <em>know</em> if it's a good or bad idea. Yet, in all that time, I've never had any problems keeping tests and production code in the same version control repository. </p> <p> With TDD, <a href="/2011/11/10/TDDimprovesreusability">the tests are such an integral part of the code base</a> that I wouldn't like to experiment with keeping them in a different repository. Additionally, when you use a test suite as a regression test suite, it's important that you version tests and production code together. This also means tagging them together. </p> <p> If you take a look at the <a href="https://github.com/ploeh">various code bases I maintain on GitHub</a>, you will also see that all of them include unit tests as part of each repository. </p> </div> <div class="comment-date">2013-12-04 06:28 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. REST lesson learned: Consider a self link on all resources https://blog.ploeh.dk/2013/05/03/rest-lesson-learned-consider-a-self-link-on-all-resources 2013-05-03T10:32:00+00:00 Mark Seemann <div id="post"> <p> <em>Add a self-link on RESTful resource.</em> </p> <p> This suggestion is part of my <a href="/2013/04/29/rest-lessons-learned">REST lessons learned</a> series of blog posts. Contrary to previous posts, this advice doesn't originate from a lesson learned the hard way, but is more of a gentle suggestion. </p> <p> One of the many advantages of a well-designed REST API is that it's '<a href="http://www.youtube.com/watch?v=b2F-DItXtZs">web-scale</a>'. The reason it's 'web-scale' is because it can leverage HTTP caching. </p> <p> HTTP caching is based on URLs. If two URLs are different, they represent different resources, and can't be cached as one. However, conceivably, a client could arrive at the same resource via a number of different URLs: <ul> <li>If a client follows redirect, it may not arrive at the URL it originally requested.</li> <li>If a client builds URLs from templates, it may order query string parameters in various ways.</li> </ul> If your API serves representations of the same resource for too many URLs, it could hurt cacheability. It may also confuse clients. </p> <p> Consider, as courtesy, adding a <a href="http://tools.ietf.org/html/rfc4287#section-4.2.7.2">self</a>-link to all resources. Adding a self link is as easy as this: </p> <p> <pre>&lt;product xmlns="http://fnaah.ploeh.dk/productcatalog/2013/05" xmlns:atom="http://www.w3.org/2005/Atom"&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/products/1337" rel="self" /&gt; &lt;atom:link href="http://catalog.api.ploeh.dk" rel="http://catalog.api.ploeh.dk/docs/rels/home" /&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/categories/chocolate" rel="http://catalog.api.ploeh.dk/docs/rels/category" /&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/categories/gourmet" rel="http://catalog.api.ploeh.dk/docs/rels/category" /&gt; &lt;name&gt;Fine Criollo dark chocolate&lt;/name&gt; &lt;price&gt;50 DKK&lt;/price&gt; &lt;weight&gt;100 g&lt;/weight&gt; &lt;/product&gt;</pre> </p> <p> Notice the link with the <em>rel</em> value of "self"; from its <em>href</em> value you now know that the canonical URL of that product resource is <code>http://catalog.api.ploeh.dk/products/1337</code>. </p> <p> Particularly when there are (technically) more than one URL that will serve the same content, do inform the client about the canonical URL. There's no way a client can tell which URL variation is the canonical URL. Only the API knows that. </p> <h3 id="8920ea29b1424d65892484c92989c036"> Redirected clients <a href="#8920ea29b1424d65892484c92989c036" title="permalink">#</a> </h3> <p> A <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a> RESTful API can evolve over time. As I <a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls">previously described</a>, you may start with URLs like <code>http://foo.ploeh.dk/orders/1234</code> only to realize that in a later version of the API, you'd rather want it to be <code>http://foo.ploeh.dk/customers/1234/orders</code>. </p> <p> As the <a href="http://amzn.to/YFnkRg">RESTful Web Services Cookbook</a> explains, you should keep <a href="http://www.w3.org/Provider/Style/URI.html">URLs cool</a>. In practice, that means that if you change the URLs of your API, you should at least leave a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2">301 (Moved Permanently)</a> at the old URL. </p> <p> If you imagine a mature API, this may have happened more than once, which means that a client arriving at one of the original URLs may be redirected several times. </p> <p> In your browser, you know that if redirects happen, the address bar will (normally) display the final URL at which you arrived. However, consider that REST clients are applications. It will be implementation-specific whether or not the client realizes that the value of the final address isn't the URL originally requested. </p> <p> As a courtesy to clients, do consider informing them of the final address at which they arrived. </p> <h3 id="433639decd564ff5825bc4986c650dbb"> URL variations <a href="#433639decd564ff5825bc4986c650dbb" title="permalink">#</a> </h3> <p> While the following scenario isn't applicable for level 3 RESTful APIs, level 1 and 2 services require clients to construct URLs from templates. As an example, such services may define a <em>product search</em> resource as <code>/products/search?q={query}&amp;p={page}</code>. To search for chocolate (and see the third (zero-indexed) page) you could construct the URL as <code>/products/search?q=chocolate&amp;p=2</code>. However, most platforms (e.g. the <a href="http://www.asp.net/web-api">ASP.NET Web API</a>) would handle <code>/products/search?p=2&amp;q=chocolate</code> in exactly the same way. The more query parameters you allow, the more permutations are possible. </p> <p> This is a well-known problem in search (and computer science); it's called <a href="http://en.wikipedia.org/wiki/Canonicalization">Canonicalization</a>. Only the API knows the canonical value of a given URL: do consider informing the client about this value. </p> <h3 id="26491fa3a4714bc1be7a0621a760feb2"> Summary <a href="#26491fa3a4714bc1be7a0621a760feb2" title="permalink">#</a> </h3> <p> Adding a self-link to each resource isn't much of a burden for the service, but could provide advantages for both the service and its clients. In most cases I'd expect the <a href="http://en.wikipedia.org/wiki/Return_on_investment">ROI</a> to be high, simply because the investment is low. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="130f9d8e3b334525aba6081198855a4b"> <div class="comment-author"><a href="http://testonsteroid.tumblr.com/">Ludovic Fleury</a> <a href="#130f9d8e3b334525aba6081198855a4b">#</a></div> <div class="comment-content">Why not using the Content-Location header for this? It the perfect candidate. As for the mutliple redirections scenario, the client would have know after each 301 what Location it targets? NB: the PR to comment is fun, yet it's very annoying.</div> <div class="comment-date">2014-02-13 22:45 UTC</div> </div> <div class="comment" id="c0f22448c2f14445b9ce7122fe40cf81"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c0f22448c2f14445b9ce7122fe40cf81">#</a></div> <div class="comment-content"> <p> The primary reason I didn't mention the Content-Location header here was that, frankly, I wasn't aware of it. Now that I've <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.14">read the specification</a>, I'll have to take your word for it :) You may be right, in which case there's no particular reason to prefer a self-link over a Content-Location header. </p> <p> When it comes following links, it's true that a protocol-near client would know the location after each redirect. The question is, if you're using an HTTP library, do you ever become aware of this? </p> <p> Consider a client, using a hypothetical HTTP client library: </p> <p> <pre>client.FollowRedirects = true; var response = client.Get("/foo");</pre> </p> <p> The response may actually be the result of various redirects, so that the final response is from "/bar", but unless the client is very careful, it may never notice this. </p> </div> <div class="comment-date">2014-02-14 16:43 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. REST lesson learned: Consider a home link on all resources https://blog.ploeh.dk/2013/05/02/rest-lesson-learned-consider-a-home-link-on-all-resources 2013-05-02T10:51:00+00:00 Mark Seemann <div id="post"> <p> <em>Add home links on RESTful resources.</em> </p> <p> This suggestion is part of my <a href="/2013/04/29/rest-lessons-learned">REST lessons learned</a> series of blog posts. Contrary to the previous posts, this advice doesn't originate from a lesson learned the hard way, but is more of a gentle suggestion. </p> <p> When designing a <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a> RESTful API, I've found that it's often helpful to think about design issues in terms of: <em>how would you design it if it was a web site?</em> On web sites, we don't ask users to construct URLs and enter them into the browser's address bar. On web sites, users follow links (and fill out forms). Many well-designed web sites are actually <a href="http://en.wikipedia.org/wiki/HATEOAS">HATEOAS</a> services, so it makes sense to learn from them when designing RESTful APIs. </p> <p> One almost universal principle for well-designed web sites is that they always have a 'home' link (usually in the top left corner). It makes sense to transfer this principle to RESTful APIs. </p> <p> All the RESTful APIs that I've helped design so far have had a single 'home' resource, which acts as a starting point for all clients. This is the only published URL for the entire API. From that starting page, clients must follow (semantic) links in order to accomplish their goals. </p> <p> As an example, here's a 'home' resource on a hypothetical product catalog API: </p> <p> <pre>&lt;home xmlns="http://fnaah.ploeh.dk/productcatalog/2013/05" xmlns:atom="http://www.w3.org/2005/Atom"&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/products/1234" rel="http://catalog.api.ploeh.dk/docs/rels/products/discounted" /&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/products/5678" rel="http://catalog.api.ploeh.dk/docs/rels/products/discounted" /&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/products/1337" rel="http://catalog.api.ploeh.dk/docs/rels/products/discounted" /&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/categories" rel="http://catalog.api.ploeh.dk/docs/rels/product/categories" /&gt; &lt;/home&gt;</pre> </p> <p> From this (rather sparse) 'home' resource, you can view each discounted product by following the <em>discounted</em> link, or you can browse the catalog by following the <em>categories</em> link. </p> <p> If you follow one of the <em>discounted</em> links, you get a new resource: </p> <p> <pre>&lt;product xmlns="http://fnaah.ploeh.dk/productcatalog/2013/05" xmlns:atom="http://www.w3.org/2005/Atom"&gt; &lt;atom:link href="http://catalog.api.ploeh.dk" rel="http://catalog.api.ploeh.dk/docs/rels/home" /&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/categories/chocolate" rel="http://catalog.api.ploeh.dk/docs/rels/category" /&gt; &lt;atom:link href="http://catalog.api.ploeh.dk/categories/gourmet" rel="http://catalog.api.ploeh.dk/docs/rels/category" /&gt; &lt;name&gt;Fine Criollo dark chocolate&lt;/name&gt; &lt;price&gt;50 DKK&lt;/price&gt; &lt;weight&gt;100 g&lt;/weight&gt; &lt;/product&gt;</pre> </p> <p> Notice that in addition to the two <em>category</em> links, the resource representation also contains a <em>home</em> link, enabling the client to go back to the 'home' resource. </p> <p> Having a home link on all resources is another courtesy to the client, just like <a href="/2013/04/30/rest-lesson-learned-avoid-204-responses">avoiding 204 responses</a> - in fact, if you can't think of anything else to return instead of 204 (No Content), at the very least, you could return a home link. </p> <p> In the above examples, I didn't <a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls">obfuscate the URLs</a>, but I would do that for a real REST API. The reason I didn't obfuscate the URLs here is to make the example easier to understand. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. REST lesson learned: Avoid hackable URLs https://blog.ploeh.dk/2013/05/01/rest-lesson-learned-avoid-hackable-urls 2013-05-01T10:29:00+00:00 Mark Seemann <div id="post"> <p> <em>Avoid hackable URLs if you are building a REST API.</em> </p> <p> This is a <a href="/2013/04/29/rest-lessons-learned">lesson about REST API design that I learned while building non-trivial REST APIs</a>. If you provide a full-on <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a> REST API, consider avoiding hackable URLs. </p> <h3 id="690f964e718a42f0b0745c0b2f7152bc"> Hackable URLs <a href="#690f964e718a42f0b0745c0b2f7152bc" title="permalink">#</a> </h3> <p> A <em>hackable URL</em> is a URL where there's a clear pattern or template for constructing the URL. As an example, if I present to you the URL <code>http://foo.ploeh.dk/products/1234</code>, it's easy to guess that this is a resource representing a product with the SKU of <em>1234</em>. If you know the SKU of another product, it's easy to 'hack' the URL to produce e.g. <code>http://foo.ploeh.dk/products/5678</code>. </p> <p> That's a really nice feature of your API if you are doing a <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 1 or 2</a> API, but for a <a href="http://en.wikipedia.org/wiki/HATEOAS">HATEOAS</a> API, this defies the purpose. </p> <h3 id="29f09d92de5742608a2977cc4c3d6516"> The great divide <a href="#29f09d92de5742608a2977cc4c3d6516" title="permalink">#</a> </h3> <p> Please notice that the shift from level 2 to level 3 RESTful APIs mark a fundamental shift in the way you should approach URL design. </p> <p> <img src="/content/binary/great-divide-between-richardson-level-2-and-3.png"> </p> <p> Hackable URLs are great for level 1 and 2 APIs because the way you (as a client) are told to construct URLs is by assembling them from templates. As <a href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179370.aspx">an example</a>, the Windows Azure REST APIs explicitly instruct you to construct the URL in a particular way: the URL to get BLOB container properties is <code>https://myaccount.blob.core.windows.net/mycontainer?restype=container</code>, where you should replace <em>myaccount</em> with your account name, and <em>mycontainer</em> with your container name. While code aesthetics are subjective, it's not even a particularly <a href="http://en.wikipedia.org/wiki/Clean_URL">clean URL</a>, but it's easy enough to produce. The URL template is part of the contract, which puts the Windows Azure API solidly at level 2 of the Richardson Maturity Model. If I were designing a level 1 or 2 API, I'd make sure to make URLs hackable, too. </p> <p> However, if you're building a level 3 API, hypermedia is king. Clients are expected to <em>follow links</em>. The addresses of resources are <em>not</em> published as having a particular template; instead, clients must follow semantic links in order to arrive at the desired resource(s). When hypermedia is the engine of application state, it's no good if the client can short-circuit the application flow by 'hacking' URLs. It may leave the application in an inconsistent state if it tries to do that. </p> <p> Hackable URLs are great for level 1 and 2 APIs, but counter-productive for level 3 APIs. </p> <h3 id="3a57655306344306b3c1ac5a001a505d"> Evolving URLs <a href="#3a57655306344306b3c1ac5a001a505d" title="permalink">#</a> </h3> <p> One of the main attractions of building a level 3 RESTful API is that it's easier to evolve. Exactly because URL templates are <em>not</em> part of the contract, you can decide to change the URL structure when evolving your API. </p> <p> Imagine that the first version of your API has an (internal) URL template like <code>/orders/{customerId}</code>, so that the example URL <code>http://foo.ploeh.dk/orders/1234</code> is the address of the order history for customer <em>1234</em>. However, in version 2 of your API, you realize that this way of thinking is still too RPC-like, and you'd rather prefer <code>/customers/{customerId}/orders</code>, e.g. <code>http://foo.ploeh.dk/customers/1234/orders</code>. </p> <p> With a level 3 RESTful API, you can change your internal URL templates, and as long as you keep providing links, clients following links will not notice the difference. However, if clients are 'hacking' your URLs, their applications may stop working if you change URL templates. </p> <h3 id="a8c7931812ae45b1abecb4ab1e175552"> Keep clients safe <a href="#a8c7931812ae45b1abecb4ab1e175552" title="permalink">#</a> </h3> <p> In the end, HATEOAS is about encapsulation: make it easy for the client to do the right thing, and make it hard for the client to do the wrong thing. Following links will make clients more robust, because they will be able to handle changes in the API. Making it easy for clients to follow links is one side of designing a good API, but the other side is important too: make it difficult for clients to <em>not</em> follow links: make it difficult for clients to 'hack' URLs. </p> <p> The services I've helped design so far are level 3 APIs, but they still used hackable URLs. One reason for that was that this is the default in the implementation platform we used (<a href="http://www.asp.net/web-api">ASP.NET Web API</a>); another reason was that I thought it would be easier for me and the rest of the development team if the URLs were human-readable. Today, I think this decision was a mistake. </p> <p> What's the harm of supplying human-readable URLs for a level 3 RESTful API? After all, if a client only follows links, the values of the URLs shouldn't matter. </p> <p> Indeed, <em>if</em> the client only follows links. However, clients are created by human developers, and humans often take the road of least resistance. While there are long-time benefits (robustness) from following links, it <em>is</em> more work in the short term. The API team and I repeatedly experienced that the developers consuming our APIs had 'hacked' our URLs; when we changed our URL templates, their clients broke and they complained. Even though we had tried to explicitly tell them that they <em>must</em> follow links, they didn't. While we <em>never</em> documented our URL templates, they were simply too easy to guess from pure extrapolation. </p> <h3 id="c5eaa6a7456e44ffbb4b6139fe35544f"> Opaque URLs <a href="#c5eaa6a7456e44ffbb4b6139fe35544f" title="permalink">#</a> </h3> <p> In the future, I plan to make URLs opaque when building level 3 APIs. Instead of <code>http://foo.ploeh.dk/customers/1234/orders</code>, I'm going to make it <code>http://foo.ploeh.dk/DC884298C70C41798ABE9052DC69CAEE</code>, and instead of <code>http://foo.ploeh.dk/products/2345</code>, I'm going to make it <code>http://foo.ploeh.dk/598CB0CAC30646E1BB768596BFE91F2C</code>, and so on. </p> <p> Obviously, that means that my API will have to maintain some sort of two-way lookup table that can map DC884298C70C41798ABE9052DC69CAEE to a request for customer <em>1234</em>'s orders, 598CB0CAC30646E1BB768596BFE91F2C to request for product <em>2345</em>, but that's trivial to implement. </p> <p> It puts a small burden on the server(s), but effectively stops client developers from shooting themselves in their feet. </p> <h3 id="36fdc650cfb94a3890bcc76d65126ae2"> Summary <a href="#36fdc650cfb94a3890bcc76d65126ae2" title="permalink">#</a> </h3> <p> Hackable URLs are a good idea if you are building a web site, or a level 1 or 2 REST API, but unless you know that all client developers are enthusiastic RESTafarians, consider producing opaque URLs for level 3 REST APIs. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="8ab08a62714148df99c9221240e0f597"> <div class="comment-author"> <a href="https://github.com/JontyMC">Jon Curtis</a> <a href="#8ab08a62714148df99c9221240e0f597">#</a></div> <div class="comment-content"> <p> I understand the benefits of the idea that clients must follow links, I just can't see how it would work in reality. Say you are building a website to display products and you are consuming a rest API, how do you support deep linking to individual products? OK, you can either use the same URL as the underlying API or encode it in, but what are you expected to do if the API changes it's links? Redirect the user to your home page? What if you want to display information from 2 or more resources on the same webpage? </p> </div> <div class="comment-date">2013-05-15 8:04 UTC</div> </div> <div class="comment" id="040e0e25a60a4ae4873c44af2c01725c"> <div class="comment-author"> <a href="/">Mark Seemann</a> <a href="#040e0e25a60a4ae4873c44af2c01725c">#</a></div> <div class="comment-content"> <p> First of all, keep in mind that while it can be helpful to think about REST design principles in terms of "how would I design this if it was a web site", a REST API is <em>not</em> a web site. </p> <p> You can do deep linking in your web site, but why would you want to do 'deep linking' for an API? These are two different concerns. </p> <p> It's very common to create a web site where each page calls many individual services. This can be done either from the web server (e.g. from ASP.NET or similar), or from the browser as AJAX calls. This is commonly known as <em>mash-up architecture</em>, because the GUI is really just a mash-up of service data. Amazon.com works that way. You can still deep link to a web page; it's the web page's responsibility to figure out which services to call with what parameters. </p> <p> That said, as described in the <a href="http://amzn.to/YFnkRg">RESTful Web Services Cookbook</a>, you should serve <a href="http://www.w3.org/Provider/Style/URI.html">cool URLs</a>, so if you ever decide to change your internal URI template, you should leave a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2">301 (Moved Permanently)</a> behind at the old URL. This would enable a client that once bookmarked a resource to follow the redirect to the new address. </p> </div> <div class="comment-date">2013-05-15 9:11 UTC</div> </div> <div class="comment" id="5c2ee152117c40e7bd37f3e1f003dabe"> <div class="comment-author"> <a href="https://github.com/JontyMC">Jon Curtis</a> <a href="#5c2ee152117c40e7bd37f3e1f003dabe">#</a></div> <div class="comment-content"> <p> When you say "it's the web page's responsibility to figure out which services to call", that's what I'm getting at, how would the client do that? </p> <p> I'm guessing one way would be to cache links followed and update them on 301s or "re-follow" on 404s. So, say you wanted a web page displaying "product/24", you might: </p> <ul> <li>GET the REST endpoint</li> <li>GET product catalogue URL from response</li> <li>GET product URL from response</li> <li>Cache the product URL against the website product URL</li> <li>Subsequent requests hit the cached URL</li> </ul> <p> Then if the product URL changes, if the response is 301, you simply update the cache. If the response is 404 then you'd redo the above steps. </p> <p> I'm just thinking this through, is the above a "standard" approach for creating rest clients? </p> </div> <div class="comment-date">2013-05-15 12:04 UTC</div> </div> <div class="comment" id="2189b527e13540daa9382f9db13ecb79"> <div class="comment-author"> <a href="/">Mark Seemann</a> <a href="#2189b527e13540daa9382f9db13ecb79">#</a></div> <div class="comment-content"> <p> That sounds like one way of doing it. I don't think there's a 'standard' way for creating RESTful clients. </p> <p> The sketch you paint sounds like a lot of work, and it <em>seems</em> that it would be easier if the client could simply assemble appropriate URLs from templates. That would indicate level 1 and 2 REST APIs, which might be perfectly fine if the only purpose of building the service is to support a GUI. However, what you get in exchange for the extra effort it takes to consume a level 3 API, is better decoupling. It's always going to be a trade-off. </p> <p> It's definitely going to be more work to build and consume a truly HATEOAS-based API, so you should only do it if it's going to provide a good return on investment. When would that be? One general scenario I can think of is when you're building a service, which is going to be consumed by multiple (unknown) clients. If you control the service, but not the clients, I'd say a level 3 API is very beneficial, because it enables you to evolve the API independently of the clients. Conversely, if the only purpose of building a service is to support a single client, it's probably going to be overkill. </p> </div> <div class="comment-date">2013-05-15 15:15 UTC</div> </div> <div class="comment" id="0716c6ea626247edbea745eb56af01ec"> <div class="comment-author"> <a href="https://github.com/redben">Reda Bendiar</a> <a href="#0716c6ea626247edbea745eb56af01ec">#</a></div> <div class="comment-content"> <p> The way I see it is that being HATEOAS compliant does not impose non-hackable URLs, but that URLs - even if they might look hackable (/1, /2...etc) - are not guaranteed to work, are not part of the contract and thus should not be relied uppon. In other words, a link is only guaranteed to work if you, the client, got it from a previous reponse, be it "hackable-looking" or not. </p> <p> So I think opaque URLs are just a way to inforce that contract. But don't we always here that "a good REST client should be well behaved" ? </p> </div> <div class="comment-date">2014-10-10 11:11 UTC</div> </div> <div class="comment" id="af28eac44f0142db86b1a914ddf21449"> <div class="comment-author"> <a href="/">Mark Seemann</a> <a href="#af28eac44f0142db86b1a914ddf21449">#</a></div> <div class="comment-content"> <p> Reda, thank you for writing. You're right, and if we could rely on all clients to be well-behaved, there'd be no problems. However, in my experience, client developers often don't read the documentation particularly thoroughly. Instead, they look at the returned data and start inferring the URL scheme from examples. I already wrote about this in this post: <blockquote>"The API team and I repeatedly experienced that the developers consuming our APIs had 'hacked' our URLs; when we changed our URL templates, their clients broke and they complained. Even though we had tried to explicitly tell them that they <em>must</em> follow links, they didn't."</blockquote> So, in this imperfect world, non-hackable URLs start to look attractive, because then the client developers have no choice but to follow the links. </p> </div> <div class="comment-date">2014-10-10 12:02 UTC</div> </div> <div class="comment" id="2f4695ce7e0a4a048ff89760ca2c2916"> <div class="comment-author"> <a href="http://kijanawoodard.com/">Kijana Woodard</a> <a href="#2f4695ce7e0a4a048ff89760ca2c2916">#</a></div> <div class="comment-content"> <p> I'd like to propose an alternate strategy to achieve the desired result while maintaining the ability to easily debug production issues at the client: only use the non-hackable urls for the "dev sandbox". </p> <p> Another inducement to better client behvior might be to have the getting started documentation use existing libraries for the chosen hypermedia media type. </p> <p> Finally, have the clients send a "developer [org] identifier". Find a way to probe clients for correctness. For instance, occasionally alter hrefs, but also support the "hackable" form. </p> <p> With this data, be proactive about informing the user that "we're planning a release and your app will probably break because you're not following links as expected". </p> <p> In the end, some people will still fail and be angry. With opaque urls, I would worry that users would have a hard time communicating production issues when looking at logs /fiddler / browser tools / etc. </p> </div> <div class="comment-date">2014-12-23 17:40 UTC</div> </div> <div class="comment" id="f9d2c7808ffc4f47b5c5b1c8995e7b81"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f9d2c7808ffc4f47b5c5b1c8995e7b81">#</a></div> <div class="comment-content"> <p> Kijana, thank you for writing. You suggest various good ideas that I'll have to keep in mind. Not all of them are universally applicable, but then I also don't think that my suggestion should be applied indiscriminately. </p> <p> Your first suggestion assumes that there <em>is</em> a dev sandbox; this may or may not be the case. The APIs with which I currently work have no dev sandboxes. </p> <p> The idea about using existing client libraries for the chosen hypermedia type is only possible if such libraries exist. The APIs I currently design use vendor media types, so client libraries only exist if we develop them ourselves. However, one of the important goals of RESTful services is to ensure interoperability, so we can't assume that clients are going to run on .NET, or Java, or Ruby, or whatever. For vendor media types, I don't think this is a viable option. </p> <p> Using a client identifier is an option, but in order to work well, there must be some way to correlate that identifier to a contact in the client's organisation. That's an option if you already have a mechanism in place where you only allow known clients to access your API. On the other hand, if you publish a truly scalable public API, you may not want to do that, as registration requirements tend to hurt adoption. The APIs I currently work with is a mix of both of those; some are publicly accessible, while others require a 'client key'. </p> <p> These are all interesting ideas, but ultimately, I'm not sure I understand your concern. Let's first establish that 'users' are other <em>programmers</em>. Why would a URL like <code>http://foo.ploeh.dk/DC884298C70C41798ABE9052DC69CAEE</code> be harder to communicate than <code>http://foo.ploeh.dk/customers/1234/orders</code>? Isn't it copy and paste in both cases? </p> <p> To be clear: I don't claim that obfuscated URLs don't make client developers' work more difficult; it does, compared to 'cheating' by hacking the URL schemes, instead of following links. Even for well-behaved client developers, another level of abstraction always makes things harder. On the other hand, this should also make clients more robust, so as always, it's a trade-off. </p> </div> <div class="comment-date">2015-01-22 08:55 UTC</div> </div> <div class="comment" id="424bd3f6199e422c8294e300d312ffb4"> <div class="comment-author"><a href="https://twitter.com/dkubb">Dan Kubb</a> <a href="#424bd3f6199e422c8294e300d312ffb4">#</a></div> <div class="comment-content"> <p> <blockquote>&ldquo;The API team and I repeatedly experienced that the developers consuming our APIs had 'hacked' our URLs; when we changed our URL templates, their clients broke and they complained. Even though we had tried to explicitly tell them that they <em>must</em> follow links, they didn't.&rdquo;</blockquote> </p> <p> I recently worked on an API for an iOS app and we ran into the same problem. It didn't matter how often I said "these URLs are probably going to change, don't hard-code them into the app" we still ran into issues. This was an internal team, I can only imagine how much more difficult it would be with an external team. </p> <p> Instead of hashing the URLs, we changed the server-side URL generation to HMAC the URL and append the signature onto the query string. Requests without a valid signature would return a 403 Forbidden response. The root of the domain is the only URL that doesn't require a signature, and it returns a json response with signed URLs for each of the "base" resources. </p> <p> We enabled this on a staging server and since all dev was happening on it, all the hard-coded URLs stopped functioning and were quickly removed from the application. It forced both us and the iOS developers to think more about navigation between resources, since it was impossible to get from one resource to another without valid URLs included in the json responses. I think it was probably also nicer for debugging than hashed URLs would be. </p> </div> <div class="comment-date">2015-04-11 09:26 UTC</div> </div> <div class="comment" id="29732957a881422ab7d4a38302b7bb5b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#29732957a881422ab7d4a38302b7bb5b">#</a></div> <div class="comment-content"> <p> Dan, thank you for sharing that great idea! That looks like a much better solution than my original suggestion of obscuring the URL, because the URL is still human-readable, and thus probably still easier to troubleshoot for developers. </p> <p> Your suggestion also doesn't require a lookup table. My suggested solution would require a lookup table in order to understand what each obscured URL actually <em>means</em>, and if you're running on multiple servers, that lookup table would have to be kept consistent across all servers, which can be hard to do in itself (you can use a database, but then you'd have a single point of failure). Your solution doesn't need any of that; it only requires that the HMAC key is the same on all servers. </p> <p> The only disadvantage that I can think of is that you may need to keep that HMAC key secret, particularly in internal projects, in order to prevent clients from (literally) hacking the URLs. </p> <p> Still, it sounds like your solution has more advantages, so I'm going to try that approach next time. Thank you for sharing! </p> </div> <div class="comment-date">2015-04-11 10:21 UTC</div> </div> <div class="comment" id="de1db0848da54b399d9af5d8005df0a8"> <div class="comment-author"><a href="https://twitter.com/dkubb">Dan Kubb</a> <a href="#de1db0848da54b399d9af5d8005df0a8">#</a></div> <div class="comment-content"> <p> <blockquote>&ldquo;The only disadvantage that I can think of is that you may need to keep that HMAC key secret, particularly in internal projects, in order to prevent clients from (literally) hacking the URLs.&rdquo;</blockquote> </p> <p> That was one concern of ours too. One approach we considered was including the git commit SHA as part of the HMAC secret so that every commit would invalidate existing URLs. We decided against it because we were doing Continous Delivery, so multiple times a day we were deploying to the server, and we didn't want the communication overhead of having to notify everyone on each commit. We wanted to be able to merge feature branches into master and just know the CI server was going to handle deployment; and that unless we got alerts from CI, Pingdom or New Relic, everything is working as expected. The last thing we wanted to do was babysit the build so we could tell everyone "ok, the new build has deployed to staging, URLs will break so restart any clients you are in the middle of testing". Also part of the reason we use REST (and json-api) was to decouple front and backend development, it seemed counterproductive to introduce coupling <em>back</em> into the process. </p> <p> An internal team could still re-implement this whole mechanism and sign their own URLs. We could either generate random secrets every day or have a simple tool that we can use to change the secret at-will (probably both). </p> <p> Even though it doesn't completely stop URL hard coding, another idea we had was to include an expiration timestamp in the query string and then HMAC the URL with the timestamp included. We would use the responses' Cache-Control max-age to calculate the time, which would allow the client to use any URLs from cached responses. If the client didn't implement proper cache invalidation the URLs would inexplicably break, thus having a nice side-effect of forcing the client to handle Cache-Control max-age properly. </p> </div> <div class="comment-date">2015-04-11 17:58 UTC</div> </div> <div class="comment" id="f440013aac7842ceb64c94868a500073"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f440013aac7842ceb64c94868a500073">#</a></div> <div class="comment-content"> <p> Dan, there are lots of interesting ideas there. What I assumed from your first comment is that you'd calculate the HMAC using asymmetric encryption, which would mean that unless clients have the server's encryption key, they wouldn't be able to recalculate the HMAC, and that would effectively prevent them from attempting to 'guess' URLs instead of following links. </p> <p> If an external client has the server's encryption key, you have a different sort of problem. </p> <p> However, for internal clients, developers may actually be able to find the key in your source control repository, build server, or wherever else you keep it (depending on the size of your organisation). You can solve this with security measures, like ACL-based security on the crypto key itself. It not hard, but it's something you may explicitly have to do. </p> <p> When it comes to tying the URL to cache invalidation, that makes me a bit uneasy. While RESTful clients should follow links, they are allowed to <em>bookmark</em> links. It's a fundamental tenet of proper web design (not only REST) that <a href="http://www.w3.org/Provider/Style/URI">cool URIs don't change</a>. A client should be allowed to keep a particular URL around <em>forever</em>, and a service following <a href="http://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a> should keep honouring requests for that URL. As the <a href="http://amzn.to/YFnkRg">RESTful Web Services Cookbook</a> explains, if you move the resource, you should at least return a 301 (Moved Permanently) "or, in rare cases," issue a 410 (Gone) response. </p> </div> <div class="comment-date">2015-04-12 11:35 UTC</div> </div> <div class="comment" id="32f5ba2d11f94ec6ad9338b70ea9bd62"> <div class="comment-author">Sandro Magi <a href="#32f5ba2d11f94ec6ad9338b70ea9bd62">#</a></div> <div class="comment-content"> <p>As Dan said, you can use an HMAC to protect certain parameters in a URL from tampering. I actually formalized this approach in a .NET library I call <a href="https://higherlogics-trac.sourcerepo.com/higherlogics_clavis/wiki/WikiStart">Clavis</a>, and which I first discussed in detail <a href="http://higherlogics.blogspot.ca/2014/01/clavis-rebooted-secure-type-safe-urls.html">here</a>.</p> <p>Clavis just provides a simple framework for declaring a resources parameter interface. Given that interface, Clavis provides a way to generate URLs when given a valid set of parameters, and also provides a way to safely parse values given a constructed URL. Arbitrary types can be specified as resource parameters, and there's very little boilerplate to convert to/from strings.</p> <p>Parameters are protected from tampering by default, but you can declare that some are unprotected, meaning the client can change them without triggering a server validation error. You want this in some cases, for instance to support GET forms, like a search.</p> </div> <div class="comment-date">2015-04-12 12:16 UTC</div> </div> <div class="comment" id="fdc20e7cf3e0496bade367ca741b7e58"> <div class="comment-author"><a href="http://ruben.verborgh.org">Ruben Verborgh</a> <a href="#fdc20e7cf3e0496bade367ca741b7e58">#</a></div> <div class="comment-content"> <p>Late to the party, but I wonder how you will create a hypermedia form that generates <code>http://foo.ploeh.dk/598CB0CAC30646E1BB768596BFE91F2C</code> rather than <code>http://foo.ploeh.dk/products/2345</code>.</p> <p>In the case of hyperlinks, I fully agree: linking to a product should not require an specifically structured identifier. However, hypermedia is more than links alone: what if the client wants to navigate to product with a given tracking number, or a list of people with a given first name?</p> <p>“Hackable URIs” are not a <em>necessity</em> for REST, but they are a potential/likely consequence of hypermedia forms. Such forms can be described with less expressive power if only simple string replacements are required instead of more complex transformations.</p> </div> <div class="comment-date">2016-02-25 11:20 UTC</div> </div> <div class="comment" id="e15484b5fc4c447e9601ee574a2c8452"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e15484b5fc4c447e9601ee574a2c8452">#</a></div> <div class="comment-content"> <p> Ruben, thank you for writing. This is a commonly occuring requirement, particularly when searching for particular resources, and I usually solve it with URI templates, as outlined in the <a href="http://amzn.to/YFnkRg">RESTful Web Services Cookbook</a>. Essentially, it involves providing URI templates like <code>http://foo.ploeh.dk/0D89C19343DA4ED985A492DA1A8CDC53/{term}</code>, and making sure that each template is served like a link, with a particular relationship type. For example: </p> <p> <pre><span style="color:blue;">&lt;</span><span style="color:#a31515;">home</span><span style="color:blue;">&nbsp;</span><span style="color:red;">xmlns</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://fnaah.ploeh.dk/productcatalog/2013/05</span>&quot;<span style="color:blue;">&gt;</span> <span style="color:blue;">&nbsp;&nbsp;&lt;</span><span style="color:#a31515;">link-template</span><span style="color:blue;">&nbsp;</span><span style="color:red;">href</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://foo.ploeh.dk/0D89C19343DA4ED985A492DA1A8CDC53/{term}</span>&quot; <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:red;">rel</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://catalog.api.ploeh.dk/docs/rels/product/search</span>&quot;<span style="color:blue;">&nbsp;/&gt;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">home</span><span style="color:blue;">&gt;</span></pre> </p> <p> It isn't perfect, but the best I've been able to come up with, and it works fairly well in practice. </p> </div> <div class="comment-date">2016-03-02 19:25 UTC</div> </div> <div class="comment" id="cde9b060dba24876a967a00379fc15eb"> <div class="comment-author"><a href="http://ruben.verborgh.org">Ruben Verborgh</a> <a href="#cde9b060dba24876a967a00379fc15eb">#</a></div> <div class="comment-content"> <p> Sure, URI templates are a means for creating hypermedia controls. But the point is that what they generate are hackable URIs. </p> <p> You still have a (semi-)hackable URI with <code>http://foo.ploeh.dk/0D89C19343DA4ED985A492DA1A8CDC53/{term}</code>. Even though the <code>/0D89C19343DA4ED985A492DA1A8CDC53/</code> part is non-manipulable, terms can still be changed at the end. This shows that hackable URIs cannot entirely be avoided if we want to keep hypermedia controls simple. What else are hackable URIs but underspecified, out-of-band hypermedia controls? </p> <p> An in-band IRI template makes the message follow the REST style by putting the control inside of the message, but it results in a hackable IRI. So, apparently, an HTTP REST API without hackable URIs is hard when there's more than just links. </p> </div> <div class="comment-date">2016-03-02 19:45 UTC</div> </div> <div class="comment" id="9fb5c6438480482c800f14f69e9b3c2d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9fb5c6438480482c800f14f69e9b3c2d">#</a></div> <div class="comment-content"> <p> Ruben, it's not that you can't avoid hackable URLs. URI templates are an optimisation that you can opt to apply, but you don't have to use them. As with so many other architecture decisions, it's a trade-off. </p> <p> When considering RESTful API design, it often helps to ask yourself: <em>how would a web site running in a browser work?</em> You don't have to copy web site mechanics one-to-one (for one, browsers utilise only GET and POST), but you can often get inspiration. </p> <p> A feature like <em>search</em> is, in my experience, one of the clearest cases for URI templates, so let's consider other designs. How does <em>search</em> work on a web site? It works by asking the user to type the search term into a form, and then submitting this form to the server. </p> <p> You can do the same with a RESTful design. When a client wishes to search, it'll have to perform an HTTP POST request with a well-formed request body containing the search term(s): </p> <p> <pre>POST /108B618F06644379879B9F2FEA1CAE92 HTTP/1.1 Content-Type: application/json Accept: application/json { "term": "chocolate" }</pre> </p> <p> The response can either contain the search hits, or a link to a resource containing the hits. </p> <p> Both options have potential drawbacks. If you return the search results in the response to the POST request, the client immediately receives the results, but you've lost the ability to cache them. </p> <p> <pre>HTTP/1.1 200 OK Content-Type: application/json { "term": "chocolate", "results": [{ "name": "Valrhona", "link": { "href": "http://example.com/B69AD9193F6E472AB75D8EBD97331E22", "rel": "product" } }, { "name": "Friis-Holm", "link": { "href": "http://example.com/3C4BEC72BFB54E1B8FADB16CCB3E7A67", "rel": "product" } }] }</pre> </p> <p> On the other hand, if the POST response only replies with a link to the search result, the search results themselves are cacheable, but now the client has to perform an extra request. </p> <p> <pre>HTTP/1.1 303 See Other Location: http://example.com/0D25692DE79B4AF9A73F128554A9119E</pre> </p> <p> In both cases, though, you'll need to document the valid representations that a client can POST to the search resource, which means that you need to <a href="/2015/06/22/rest-implies-content-negotiation">consider backwards compatibility if you want to change things</a>, and so on. In other words, you can 'protect' the URL schema of the search resource, but not the payload schema. </p> <p> If you're already exposing an API over HTTPS, caching is of marginal relevance anyway, so you may decide to use the first POST option outlined above. Sometimes, however, performance is more important than transport security. As an example, I once wrote (for a client) a publicly available music catalogue API. This API isn't protected, because the data isn't secret, and it has to be fast. Resources are cached for 6 hours, so if you're searching for something popular (say, <em>Rihanna</em>), odds are that you're going to get a response from an intermediary cache. URI templates work well in this scenario, but it's a deliberate performance optimisation. </p> </div> <div class="comment-date">2016-03-10 07:34 UTC</div> </div> <div class="comment" id="3208d3ec858e4b9496689b8d72b0f3f6"> <div class="comment-author"><a href="http://ruben.verborgh.org">Ruben Verborgh</a> <a href="#3208d3ec858e4b9496689b8d72b0f3f6">#</a></div> <div class="comment-content"> <p> One can always avoid hackable URLs with trickery, but would you say that leads to a cleaner solution? I agree that search is “one of the clearest cases for URI templates”, so why bother putting in an unneeded <code>POST</code> request just to avoid hackable URIs? I fail to see why this would be a better REST design, given that you loose the beneficial properties of the uniform interface constraints by overloading the <code>POST</code> method for what is essentially a read-only operation. The trade-off here is entirely non-technical: you trade the performance of the interface for the aesthetics of its URLs. </p> <p> Here's how a website would do it: it would have an HTML form for a <code>GET</code> request, which would naturally lead to a hackable URL. Because that's how HTML forms work: like URI templates, they result in hackable URLs. It's unavoidable. Hackable URLs are a direct consequence of common hypermedia controls, which are a necessity to realize the hypermedia constraint. You can swim against the current and make more difficult hypermedia controls, but to whose benefit? It's much easier to just accept that hackability is a side-effect of HTML forms and URI templates. </p> <p> Therefore, your thesis that one should avoid hackable URIs breaks down at this point. APIs with non-hackable URIs are inherently incompatible with URI templates—and why throw away this excellent means of affordance? After all, the most straightforward way to realize search functionality is with an in-band URI template. And URI templates lead to hackable URIs. Hence, hackable URIs occur in REST APIs. You can work around this, but it's quite artificial to do so. In essence, you're using the <code>POST</code> request to obfuscate the URI; having such a translation step for purely aesthetic motives complicates the API and sacrifices caching for no good reason. Hackable URIs are definitely the lesser evil of the two. </p> <p> So, would you avoid hackable URIs at any cost? Or can you agree that they indeed have a place if we like to keep using the hypermedia controls that have been standardized? </p> </div> <div class="comment-date">2016-03-10 23:50 UTC</div> </div> <div class="comment" id="43f1a970bc3749f383d077aecf5c7195"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#43f1a970bc3749f383d077aecf5c7195">#</a></div> <div class="comment-content"> <p> Ruben, my intent with this post was to highlight a problem I've experienced when publishing REST APIs, and that I've attempted to explain in the section titled <em>Keep clients safe</em>. I wish that problem didn't exist, but in my experience, it does. It most likely depends on a lot of factors, so if you don't have that problem, you can disregard the entire article. </p> <p> The issue isn't that I want to make URLs opaque. The real issue at stake is that I want clients to follow links, because it gives me options to evolve the API. Consider the example in <a href="/2014/01/20/rest-efficiency">this article</a>. If a client wants to see a user's messages, it should follow links. If, however, a client developer writes the code in such a way that it'll explicitly look for a <code>root &gt; users &gt; user details &gt; user messages</code> route, little is gained. </p> <p> A client looking for user messages should be implemented with a prioritised <em>rule set:</em> <ol> <li>Look for a <code>user-messages</code> link. If found, follow it. Done.</li> <li>Look for a <code>user-details</code> link. If found, follow it, and apply this rule list again from the top.</li> <li>Look for a <code>user</code> link. If found, follow it, and apply this rule list again from the top.</li> </ol> Actually, it's more complex than outlined here, because the rule set may have to dynamically change based on the context, but I hope it gets the main point across. </p> <p> If client developers can guess, and thereby 'hack', my service's URLs, I can't restructure my API. It's not really a question of aesthetics; it's not that I want to be able to <em>rename</em> URLs. The real issue is that sometimes, when developing software, you discover that you've modelled a concept the wrong way. Perhaps you've conflated two concepts that ought to be treated as separate resources. Perhaps you've create a false distinction, but now you realise that two concepts that you thought were separate, is really the same. Therefore, sometimes you want to split one type of resource into two, or more, or you want to combine two or more different types of resources into one. </p> <p> If clients follow links, you have a chance to pull off such radical restructuring. If clients have hard-coded URLs, you're out of luck. </p> </div> <div class="comment-date">2016-03-12 16:28 UTC</div> </div> <div class="comment" id="0efaa2e9cfc54586801d1f39b5a07386"> <div class="comment-author"><a href="https://au.linkedin.com/in/ashleyaitken">Ashley Aitken</a> <a href="#0efaa2e9cfc54586801d1f39b5a07386">#</a></div> <div class="comment-content"> <p> Thank you for an interesting post and discussion. I am no expert wrt REST but I have a comment (posed as a question) and a question. </p> <p> If client developers abuse transparent URLs (that are provided in a level 3 api) isn't the fact that the client stops working when the URLs change their problem? And sure, they can guess other URLs, just like in the browser we can guess other pages. Doesn't mean the application stage should let them access those pages etc. </p> <p> And now the question. Are there any libraries or tools that allow REST client applications to easily get a list of the links and "follow the links" (i.e. do an API call)? I can see a client having slots for possible / expected links, filling those that exist, and calling them. Is there any library to help this? </p> </div> <div class="comment-date">2016-06-23 15:18 UTC</div> </div> <div class="comment" id="98a9a27d3ada47948cc3b2ebf41ac348"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#98a9a27d3ada47948cc3b2ebf41ac348">#</a></div> <div class="comment-content"> <p> Ashley, thank you for writing. In the end, as you point out, it's the client's problem if they don't adhere to the contract. In some scenarios, that's the end of the story. In other scenarios (e.g. the ones I often find myself in), there's a major client that has a lot of clout, and if those developers find that you've moved their cheese, their default reaction is that it's your fault - not theirs. In such cases, it can be political to ensure that there's no doubt about the contract. </p> <p> I'm not aware of any libraries apart from <a href="https://msdn.microsoft.com/en-us/library/system.net.http.httpclient">HttpClient</a>. It doesn't do any of the things you ask about, because it's too low-level, but at least it enables you to build a sensible client API on top of it. </p> </div> <div class="comment-date">2016-06-23 21:05 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Herding Code podcast https://blog.ploeh.dk/2013/05/01/herding-code-podcast 2013-05-01T10:26:00+00:00 Mark Seemann <div id="post"> <p> <em>I'm on Herding Code.</em> </p> <p> At the Danish Developer Conference 2013 I had the pleasure of meeting <a href="http://weblogs.asp.net/jgalloway/">Jon Galloway</a>, who interviewed me for <a href="http://herdingcode.com/?p=539">a Herding Code podcast</a>. In this interview, we talk about <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>, <a href="/2013/03/11/listen-to-trivial-tests">testing (or not testing) trivial code</a>, as well as lots of the unit testing topics I also cover in <a href="https://blog.ploeh.dk/advanced-unit-testing">my Pluralsight course on Advanced Unit Testing</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. REST lesson learned: Avoid 204 responses https://blog.ploeh.dk/2013/04/30/rest-lesson-learned-avoid-204-responses 2013-04-30T10:46:00+00:00 Mark Seemann <div id="post"> <p> <em>Avoid 204 responses if you're building a HATEOAS application.</em> </p> <p> This is a <a href="/2013/04/29/rest-lessons-learned">lesson about REST API design that I learned while building non-trivial REST APIs</a>. In order to be as supportive of the client as possible, a REST API should not return 204 (No Content) responses. </p> <p> From the service's perspective, a 204 (No Content) response may be a perfectly valid response to a POST, PUT or DELETE request. Particularly, for a DELETE request it seems very appropriate, because what else can you say? </p> <p> However, from the perspective of a proper <a href="http://en.wikipedia.org/wiki/HATEOAS">HATEOAS</a>-aware client, a 204 response is problematic because there are no links to follow. When hypermedia acts as the engine of application state, when there are no links, there's no state. In other words, a 204 response throws away all application state. </p> <p> If a client encounters a 204 response, it can either give up, go to the entry point of the API, or go back to the previous resource it visited. Neither option is particularly good. </p> <p> Giving up is not a good option if there's still work to do. Essentially, this is equivalent to a crashing client. </p> <p> Going to the entry point of the API <em>may</em> allow the client to move on, doing what it was doing, but state may still be lost. </p> <p> Going back (the equivalent of using your browser's <em>back</em> button) may be the best option, but has a couple of problems: First, if the client just did a DELETE, the previous resource in the history may now be gone (it was just deleted). The client would have to go back twice to arrive at a proper resource. Second, while your browser has built-in history, a programmatic HTTP client probably hasn't. You could add that feature to your client, but it would require more work. Once more, it would require the <em>client</em> to maintain state, which means that you'd be moving state from hypermedia to the client. It's just not a HATEOAS-compliant approach. </p> <p> A good REST API should make it easy to be a client. While this is only a variation of <a href="http://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>, I also like to think of this in terms or courtesy. The service has a lot of information available to it, so it might as well be courteous and help the client by sharing appropriate pieces of information. </p> <p> Instead of a 204 (No Content) response, tell the client what it can do now. </p> <h3 id="fdf7840f0d1c4713aa305aa23868073a"> Responding to POST requests <a href="#fdf7840f0d1c4713aa305aa23868073a" title="permalink">#</a> </h3> <p> An HTTP POST request often represents some sort of <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command</a> - that is: an intent to produce side effects. If the service handles the request synchronously, it should return the result of invoking the Command. </p> <p> A common POST action is to create a new resource. At the very least, the REST API should return a 201 (Created) with a proper Location header. That's all described in detail in the <a href="http://amzn.to/YFnkRg">RESTful Web Services Cookbook</a>. </p> <p> Even if the API decides to handle the request asynchronously, it can provide one or more links. Actually, the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3">specification of 202 (Accepted)</a> says that the "entity returned with this response SHOULD include an indication of the request's current status and [...] a pointer to a status monitor". That sounds like a link to me. </p> <h3 id="6bbe22d7f8dc4d8a82e54c8efb78666f"> Responding to PUT requests <a href="#6bbe22d7f8dc4d8a82e54c8efb78666f" title="permalink">#</a> </h3> <p> An HTTP PUT request is often intended to update the state of a particular resource. Instead of returning 204 (No Content), the API should be courteous and return the new state of the resource. You may think that this is redundant because the client just transferred the state of the resource <em>to</em> the service, so why pay transmission costs to get the resource back <em>from</em> the service? </p> <p> Even if a client PUTs the entire state of a resource to the API, the API may still have information about the resource not available to the client. The most universal example is probably the resource's ETag. If the client wishes to do further processing on the resource, it's likely going to need the ETag value sooner or later. </p> <p> The representation of the resource may also include optional or denormalized data that the client may not have. If that data is optional, the client can make a PUT without it, but it might want that data to display to a user, so it would have to read the latest state of the resource regardless. </p> <h3 id="f3f9980667124587bf4a872cc2a2ea33"> Responding to DELETE requests <a href="#f3f9980667124587bf4a872cc2a2ea33" title="permalink">#</a> </h3> <p> A DELETE request represents the intent to delete a resource. Thus, if the service successfully handles a DELETE request, what else can it do than returning a 204 (No Content)? After all, the resource has just been removed. </p> <p> A resource is often a member of a collection, or otherwise 'owned' by a container. As an example, <code>http://foo.ploeh.dk/api/tags/rock</code> represents a "rock" <em>tag</em>, but another way of looking at it is that the <code>/rock</code> resource is contained within the <code>tags</code> container (which is itself a resource). This should be familiar to <a href="http://tools.ietf.org/html/rfc5023">Atom Pub</a> users. </p> <p> Imagine that you want to delete the <code>http://foo.ploeh.dk/api/tags/rock</code> resource. In order to accomplish that goal, you issue a DELETE request against it. If all your client gets back is a 204 (No Content), it's just lost its context. Where does it go from there? Unless you keep state on the client, you don't know where you came from. </p> <p> Instead of returning 204 (No Content), the API should be helpful and suggest places to go. In this example I think one obvious link to provide is to <code>http://foo.ploeh.dk/api/tags</code> - the container from which the client just deleted a resource. Perhaps the client wishes to delete more resources, so that would be a helpful link. </p> <h3 id="7c536a29005740e4a5bfe6525508cc43"> Responding to GET requests <a href="#7c536a29005740e4a5bfe6525508cc43" title="permalink">#</a> </h3> <p> Returning 204 (No Content) as a response to a GET request is a bit weird, but probably not <em>completely</em> unheard of. Such a resource would represent a flag or semaphore, where the <em>existence</em> of the resource (as opposed to a 404 (Not Found)) signifies something. However, consider being helpful to the client and at least provide a link it can use to move on. </p> <h3 id="e1c1be99c6744656b953b85bd8ba9e1c"> Summary <a href="#e1c1be99c6744656b953b85bd8ba9e1c" title="permalink">#</a> </h3> <p> Technically, responding with 204 (No Content) is perfectly valid, but if a REST API does that, its clients lose their current context. A service should be helpful and inform clients where they can go from the current resource. This will make client development easier, and only puts a small burden on the service. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="52dd9cc93ad2459c98b6019527bf47ee"> <div class="comment-author"><a href="http://xanderguzman.com">Xander Guzman</a> <a href="#52dd9cc93ad2459c98b6019527bf47ee">#</a></div> <div class="comment-content"> I'm going to have to disagree with your interpretation of HTTP 204 NO CONTENT and the DELETE method. The <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">HTTP/1.1 specification</a> clearly states <quote>The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.</quote> So you can still have a response body without entity information but contain meta information maintaining HATEOS. </div> <div class="comment-date">2015-04-02 17:24 UTC</div> </div> <div class="comment" id="5e06cd47e553409681d5001ae83b886a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5e06cd47e553409681d5001ae83b886a">#</a></div> <div class="comment-content"> <p> Xander, thank you for writing. If you read this article carefully, you may notice that it's not an exegesis of the HTTP specification, but a guideline to writing <em>pleasant and useful</em> RESTful services. </p> <p> The bottom line is that if you're a client developer trying to follow links, 204 responses are <em>annoying</em>, which isn't the same as to say that they're invalid (they're not). </p> <p> Could the service put links in the headers? Perhaps it could, but then it'd be forcing the client developer to do more work (by looking after links in two places). </p> <p> Forcing client developers to do more work scales poorly (in terms of effort, not performance). A service developer needs to do a particular piece of work <em>once</em>, after which it's available to all client developers (even if there are hundreds of clients). </p> <p> If a service doesn't provide a consistent API, every client developer has to duplicate the work the service developer could have done once. Every time that happens, you risk introducing bugs (on the client side). The end result is a more brittle system. </p> <p> In the end, this has nothing to do with HTTP, but with <a href="http://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. </p> </div> <div class="comment-date">2015-04-02 18:35 UTC</div> </div> <div class="comment" id="518872d326e3470eb2e7558a4740c606"> <div class="comment-author"><a href="http://tuespetre.github.io">Derek Gray</a> <a href="#518872d326e3470eb2e7558a4740c606">#</a></div> <div class="comment-content"> <p> Mark, I understand why you might say that about link headers, but not all media types are hypermedia (image/png, audio/mp4, video/ogg to name a few) whereas all HTTP header sections are. Given that, wouldn't it be wise for a client to always maintain awareness of protocol-level linking and, when a media type offers links, maintain awareness of that as well? </p> <p> Since you mentioned client/server development, it's worth considering that once a given domain (be it 'people', 'autos', 'nutrition', etc.) has become cemented with standard media types and link relations, there would probably be a community effort to develop client and server libraries and applications for varying languages and platforms so that each development team doesn't have to write and test their own implementations from scratch (see: HTML browsers and frameworks.) </p> <p> It makes sense that the 'World Wide Web' has become standard and RESTful; everyone is interested in the domain of general-purpose knowledge and entertainment. Unfortunately I think it will still be a while before we see many other, more specific domains become standardized to that same degree. </p> </div> <div class="comment-date">2015-06-12 13:50 UTC</div> </div> <div class="comment" id="2e0850a3bc2b4d9d9f2e2b0a165b6461"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2e0850a3bc2b4d9d9f2e2b0a165b6461">#</a></div> <div class="comment-content"> <p> Derek, thank you for writing. It's a good point that some media types like images, audio, etc. don't have a natural place to put meta-data like links. Conceivably, that might even include vendor-specific media types where, for some reason or other, links don't fit. </p> <p> In such cases, I think that links in headers sounds preferable to no links at all. In the presence of such media types, my argument against links in headers no longer holds. </p> <p> Once more, we've learned that no advice is independent of context. In the context of a RESTful API where such media types may occur, clients would have to be aware that links can appear in headers as well as in the body of any document. In such APIs, 204 responses may be perfectly fine. Here, the extra work of looking two places (both in the body, and in headers) is warranted. </p> <p> In contexts of APIs where such media types don't occur, clients have no particular reason to expect links in headers. In this case, I still maintain that 204 responses put an unwarranted burden on client developers. </p> <p> So far, I've only been involved with APIs without such special media types, so my initial advice was obviously coloured by my experience. Thus, I appreciate the alternative perspective. </p> </div> <div class="comment-date">2015-06-14 14:22 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. REST lesson learned: Avoid user-supplied data in URI segments https://blog.ploeh.dk/2013/04/29/rest-lesson-learned-avoid-user-supplied-data-in-uri-segments 2013-04-29T12:07:00+00:00 Mark Seemann <div id="post"> <p> <em>Be careful with user-supplied data in URI segments.</em> </p> <p> This is a <a href="/2013/04/29/rest-lessons-learned">lesson about design of REST APIs that I learned the hard way</a>: if you built service URIs from dynamic data, be careful with the data you allow in URI segments. </p> <p> Some HTTP frameworks (e.g. the <a href="http://www.asp.net/web-api">ASP.NET Web API</a>) let you define URI templates or routes that handle incoming requests - e.g. </p> <p> <pre>routeTemplate: "api/{controller}/{id}"</pre> </p> <p> which means that a request to <code>http://foo.ploeh.dk/api/fnaah/sgryt</code> would map the value "fnaah" to <em>controller</em>, and the value "sgryt" to <em>id</em>. </p> <p> If you are building a <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 2 or less</a> service, you may even publish your URI templates to consumers. If you are building a level 3 RESTful API, you may not be publishing your URI templates, but you can still use them internally. </p> <h3 id="454d275f4b2847a9887a54265c31cd6f"> Avoid user-supplied data in URI segments <a href="#454d275f4b2847a9887a54265c31cd6f" title="permalink">#</a> </h3> <p> Be careful with the source of data you allow to populate URI segments of your URI template. At one point, I was involved with designing a REST API that (among other things) included a 'tag cloud' feature. If you wanted to see the contents of a specific tag, you could navigate to it. </p> <p> Tags were all user-defined strings, and they had no internal ID, so our first attempt was to simply treat the <em>value</em> of the tag as the ID. That seemed reasonable, because we wanted the <em>tag</em> resource to list all the resources with that particular tag value. Thus, we modeled the URI template for tag resources like the above route template. </p> <p> That worked well for a URL like <code>http://foo.ploeh.dk/api/tags/rock</code> because it would simply match the value "rock" to the <em>id</em> variable, and we could then list all resources tagged with "rock". </p> <p> However, some user had defined a tag with the value of "sticky &amp; sweet" (notice the ampersand character), which meant that when you wanted to see all resources with this link, you would have to navigate to <code>http://foo.ploeh.dk/api/tags/sticky &amp; sweet</code>. However, that sort of URL is considered dangerous, and IIS will (<a href="http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx">by default</a>) refuse to handle it. </p> <p> Can you get around this by URL encoding the value? No, it's part of the <em>request path</em>, not part of any <em>query string</em>, so that's not going to work. The issue isn't that the URL is invalid, but that the server considers it to be dangerous. Even if you URL encode it, the server will decode it before handling it, and that would you leave you at square one. </p> <p> You can either change the URI template so that the URL instead becomes <code>http://foo.ploeh.dk/api/tags?id=sticky%20%26%20sweet</code>. This URL encodes the <em>query string</em> (the part of the URL that comes <em>after</em> the ?), but gives you an ugly URL. </p> <p> Another alternative is to be very strict about input validation, and only allow users to create values that are safe when used as URI segments. However, that's putting an unreasonable technical limitation on an application feature. If a user wants to tag a resource with "sticky &amp; sweet", the service should allow it. </p> <p> In the end, we used a third alternative: we assigned an internal ID to all tags and mapped back and forth so that the URL for the "sticky &amp; sweet" tag became <code>http://foo.ploeh.dk/api/tags/1234</code>. Yes: that makes it impossible to guess the URL, but we were building a <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a> RESTful API, so clients are expected to follow links - not guess the URL. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. REST lessons learned https://blog.ploeh.dk/2013/04/29/rest-lessons-learned 2013-04-29T00:00:00+00:00 Mark Seemann <div id="post"> <p> <em>This post provides an overview of some lessons I learned while bulding non-trivial REST APIs.</em> </p> <p> Last year I spent a good deal of the year designing and implementing a handful of non-trivial REST APIs for a customer of mine. During that process, I learned some small lessons about the design of RESTful systems that I haven't seen described elsewhere, and I want to share these lessons with you. </p> <p> In order to learn the concepts and philosphy behind REST, I think that <a href="http://amzn.to/UIQ4Mc">REST in Practice</a> is a great resource (pun intended), but when it comes to practical guidance, I find the <a href="http://amzn.to/YFnkRg">RESTful Web Services Cookbook</a> invaluable. It's full of useful and concrete tips and tricks for building RESTful APIs, but I don't remember reading about the following lessons, that I had to learn the hard way. </p> <p> There's so much hype and misrepresentation about REST that I have to point out that when I'm talking about REST, I mean full-on, <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a> REST, with resources, verbs, hypermedia controls and the works. </p> <p> Each of these lessons deserves a small article of its own, but here's an overview: </p> <p> <ul> <li><a href="/2013/04/29/rest-lesson-learned-avoid-user-supplied-data-in-uri-segments">Avoid user-supplied data in URI segments</a></li> <li><a href="/2013/04/30/rest-lesson-learned-avoid-204-responses">Avoid 204 responses</a></li> <li><a href="/2013/05/01/rest-lesson-learned-avoid-hackable-urls">Avoid hackable URLs</a></li> <li><a href="/2013/05/02/rest-lesson-learned-consider-a-home-link-on-all-resources">Consider a home link on all resources</a></li> <li><a href="/2013/05/03/rest-lesson-learned-consider-a-self-link-on-all-resources">Consider a self link on all resources</a></li> <li><a href="/2021/04/19/consider-including-identity-in-urls">Consider including identity in URLs</a></li> </ul> </p> <p> I hope you find these tips useful. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. How to change the Generate Property Stub refactoring code snippet in Visual Studio 2012 https://blog.ploeh.dk/2013/04/23/how-to-change-the-generate-property-stub-refactoring-code-snippet-in-visual-studio-2012 2013-04-23T14:00:00+00:00 Mark Seemann <div id="post"> <p> <em>This post describes how to change the code template for the 'Generate Property Stub' refactoring in Visual Studio 2012.</em> </p> <p> This is mostly a quick note for my own benefit (since I just spent half an hour chasing this down), but I'm posting it here on the blog, in case someone else might find it useful. Here's how to change the code snippet for the "Generate Property Stub" smart tag in Visual Studio 2012: </p> <p> <img src="/content/binary/generate-property-stub-smart-tag.png"> </p> <p> The template for this code is defined in </p> <p> <pre>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Snippets\1033 \Refactoring\GenerateProperty - Auto Property.snippet</pre> </p> <p> You can open this file and hand-edit it, but contrary to previous versions of Visual Studio, it seems that you have to restart Visual Studio before the change takes effect. </p> <p> If you do this, be sure that you know what you're doing; you'd be mucking around with the internals of your Visual Studio installation. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Advanced Unit Testing Pluralsight course https://blog.ploeh.dk/2013/04/15/advanced-unit-testing-pluralsight-course 2013-04-15T18:29:00+00:00 Mark Seemann <div id="post"> <p> Service announcement: my new <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/advanced-unit-testing">Advanced Unit Testing</a> is now available. Read more in <a href="http://blog.pluralsight.com/2013/04/15/new-course-advanced-unit-testing">Pluralsight's announcement</a>, or <a href="https://blog.ploeh.dk/advanced-unit-testing">go straight to the course</a>. </p> <p> If you don't already have a Pluralsight account, you can get a <a href="https://pluralsight.com/training/Subscribe/Step1?isTrial=True">free trial</a> of up to 200 minutes. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. FizzBuzz kata in Clojure https://blog.ploeh.dk/2013/04/11/fizzbuzz-kata-in-clojure 2013-04-11T06:20:00+00:00 Mark Seemann <div id="post"> <p> <em>This post describes my first experience with doing the FizzBuzz kata in Clojure.</em> </p> <p> After having looked at <a href="http://clojure.org/">Clojure</a> for some time, I finally had a bit of time to play with it, so I decided to do the <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataFizzBuzz">FizzBuzz kata</a> in Clojure. </p> <h3 id="f59b492d7404433fa7c87645f079e6f4"> Single fizzbuzz function <a href="#f59b492d7404433fa7c87645f079e6f4" title="permalink">#</a> </h3> <p> Clojure has a built-in testing framework, so I wrote a Parameterized Test for a single fizzbuzz function. Using TDD, I added test cases a little at a time, attempting to follow the <a href="http://cleancoder.posterous.com/the-transformation-priority-premise">Transformation Priority Premise</a>, but the end result is this: </p> <p> <pre>(ns fizzbuzz.core-test (:use clojure.test fizzbuzz.core)) (deftest fizzbuzz-test (are [expected i] (= expected (fizzbuzz i)) "1" 1 "2" 2 "Fizz" 3 "4" 4 "Buzz" 5 "Fizz" 6 "7" 7 "8" 8 "Fizz" 9 "Buzz" 10 "11" 11 "Fizz" 12 "13" 13 "14" 14 "FizzBuzz" 15 "FizzBuzz" 30))</pre> </p> <p> Clojure syntax is somewhat backwards from what I'm normally used to, but the <a href="http://richhickey.github.io/clojure/clojure.test-api.html#clojure.test/are">are</a> macro expands into a collection of tests that each evaluate whether the result of invoking the fizzbuzz function with <code>i</code> is equal to <code>expected</code>. </p> <p> Through a series of transformations of the SUT, I ended up with this implementation of the fizzbuzz function: </p> <p> <pre>(ns fizzbuzz.core) (defn fizzbuzz [i] (cond (= 0 (mod i 15)) "FizzBuzz" (= 0 (mod i 3)) "Fizz" (= 0 (mod i 5)) "Buzz" :else (str i)))</pre> </p> <p> This defines a function called <em>fizzbuzz</em> taking a single argument <code>i</code>. The <a href="http://richhickey.github.io/clojure/clojure.core-api.html#clojure.core/cond">cond</a> macro evaluates each test and returns the expression associated with the first test that evauluates to true. The first test checks if <code>i</code> is divisible with 15 and returns "FizzBuzz" if this is the case; the next test checks if <code>i</code> is divisible with 3 and returns "Fizz" if this is true, and so on. </p> <h3 id="dc83aff6725346a8a2aafd6ce49ba410"> Printing a range of fizzbuzz values <a href="#dc83aff6725346a8a2aafd6ce49ba410" title="permalink">#</a> </h3> <p> The task defined by the kata is to print all FizzBuzz values from 1 to 100, so the above function is't the final solution. The next step I took was to write a test that defines a version of the fizzbuzz function taking two parameters: </p> <p> <pre>(def acceptance-expected "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz") (deftest acceptance-test (is (= acceptance-expected (fizzbuzz 1 101))))</pre> </p> <p> I decided to define the <code>acceptance-expected</code> value outside of the test case itself, as I thought that made the test a bit more readable. The test case is defined by the <a href="http://richhickey.github.io/clojure/clojure.test-api.html#clojure.test/is">is</a> macro and states that the expected value is <code>acceptance-expected</code> and the actual value is the result of invoking the fizzbuzz function with <em>two</em> arguments: 1 as the (inclusive) start value, and 101 as the (exclusive) end value. The above code listing of fizzbuzz only accept one argument, but the new test case requires two arguments, so I added an overload to the function: </p> <p> <pre>(defn fizzbuzz ([i] (cond (= 0 (mod i 15)) "FizzBuzz" (= 0 (mod i 3)) "Fizz" (= 0 (mod i 5)) "Buzz" :else (str i))) ([start end] (apply str (interpose "\n" (map fizzbuzz (range start end))))))</pre> </p> <p> The previous implementation is still there, now contained within the overload taking a single argument <code>i</code>, but now there's also a new overload taking two arguments: <code>start</code> and <code>end</code>. </p> <p> This overload generates a sequence of integers from <code>start</code> to <code>end</code> using the <a href="http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/range">range</a> function. It then maps that sequence of integers into a sequence of strings by <a href="http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/map">mapping</a> each integer to a string with the fizzbuzz function. That gives you a sequence of strings such as <code>("1" "2" "Fizz" "4" "Buzz")</code>. </p> <p> In order to print all the FizzBuzz strings, I need to <a href="http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/interpose">interpose</a> a newline character between each string, which produces a new sequence of strings such as <code>("1" "\n" "2" "\n" "Fizz" "\n" "4" "\n" "Buzz")</code>. To concatenate all these strings, I <a href="http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/apply">apply</a> the <a href="http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/str">str</a> function to the sequence. </p> <h3 id="2eae1e308c5f401584d617a4f7a9cae8"> Printing FizzBuzz values from 1 to 100 <a href="#2eae1e308c5f401584d617a4f7a9cae8" title="permalink">#</a> </h3> <p> The requirements of the kata is to print all FizzBuzz values from 1 to 100, and the code already does this. However, I interpret the kata as requiring a single function that takes no parameters, so I added an acceptance test case: </p> <p> <pre>(deftest acceptance-test (is (= acceptance-expected (fizzbuzz 1 101))) (is (= acceptance-expected (fizzbuzz))))</pre> </p> <p> Notice the second test case in the last line of code that invokes the fizzbuzz function without any parameters. It's easily resolved by adding a third overload: </p> <p> <pre>(defn fizzbuzz ([] (fizzbuzz 1 101)) ([i] (cond (= 0 (mod i 15)) "FizzBuzz" (= 0 (mod i 3)) "Fizz" (= 0 (mod i 5)) "Buzz" :else (str i))) ([start end] (apply str (interpose "\n" (map fizzbuzz (range start end))))))</pre> </p> <p> As you can see, the first overload takes no parameters and simply invokes the previously desribed overload with the <code>start</code> and <code>end</code> arguments. </p> <p> FWIW, this entire solution is structurally similar to <a href="/2012/07/20/FizzBuzzkatainFstage1/">my implementation of FizzBuzz in F#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Ploeh blog syndication feed addresses https://blog.ploeh.dk/2013/04/09/ploeh-blog-syndication-feed-addresses 2013-04-09T07:53:00+00:00 Mark Seemann <div id="post"> <p> <em>Service announcement about syndication feed addresses for ploeh blog.</em> </p> <p> Now that Google Reader is closing down and a lot of my readers may want to migrate their <em>ploeh blog</em> subscription to another service, I think it's the right time for a quick post about ploeh blog's syndication feed addresses. </p> <p> When I <a href="/2013/03/03/moving-the-blog-to-jekyll">migrated my blog to Jekyll</a> I made it a priority to ensure a certain level of backwards compatibility. The permalinks for the old posts are still served, although they are now redirects. For that reason, I also left the old syndication feed addresses in place. Specifically, I left this old RSS feed address in place: <code>/SyndicationService.asmx/GetRss</code>. This feed address sort of works, but has issues. </p> <p> There are several problems with the 'legacy' feed address: <ul> <li>Due to the way Jekyll works, the address actually points to an index.html file. Since a Jekyll-powered site is a static site, I don't control the server, and thus I can't manipulate the HTTP headers for individual resources. HTML files are served with the "text/html" Content-Type, which doesn't fit the XML content. Some clients seem to dislike this.</li> <li>Google Reader has created some 'ghost' entries based on the feed. I wonder if it has anything to do with the faulty Content-Type.</li> <li>Other users have reported that their clients (e.g. Outlook) don't like the feed. Again, I suspect it's because of the faulty Content-Type. While I think those clients should follow <a href="http://en.wikipedia.org/wiki/Postel%27s_law">Postel's law</a>, they apparently don't.</li> <li>The address looks very implementation-specific.</li> </ul> </p> <p> However, ploeh blog has new, 'proper' syndication feed addresses, so if you're already in the process of migrating your subscriptions away from Google Reader, please take a moment to update your subscription. That 'legacy' RSS address may not stick around forever. </p> <p> The proper syndication feed addresses for ploeh blog are: <ul> <li><strong>ATOM:</strong> <a href="/atom.xml">/atom.xml</a></li> <li><strong>RSS:</strong> <a href="/rss.xml">/rss.xml</a></li> </ul> Both serve responses with the "text/xml" Content-Type. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. How to automatically populate properties with AutoMoq https://blog.ploeh.dk/2013/04/08/how-to-automatically-populate-properties-with-automoq 2013-04-08T05:55:00+00:00 Mark Seemann <div id="post"> <p> <em>This post explains how to automatically populate properties when using AutoFixture.AutoMoq.</em> </p> <p> In <a href="/2013/04/05/how-to-configure-automoq-to-set-up-all-properties">a previous blog post</a> I described how to configure <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer/">AutoFixture.AutoMoq</a> to set up all mock instances to have 'normal' property behavior. This enables you to assign and retrieve values from properties defined by interfaces, but still doesn't fill those properties with values. </p> <p> Apparently, <a href="https://autofixture.codeplex.com/workitem/4245">people want to do that</a>, so here's how to do it with the AutoMoq glue library. </p> <p> This solution builds upon the PropertiesPostprocessor described in my previous blog post. All you have to do is define a different Customization for <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> so that, instead of using the AutoMoqPropertiesCustomization described in the previous post, you'll need a variation: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">AutoPopulatedMoqPropertiesCustomization</span> : <span style="color: #2b91af;">ICustomization</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Customize(<span style="color: #2b91af;">IFixture</span> fixture) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fixture.Customizations.Add( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertiesPostprocessor</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MockPostprocessor</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MethodInvoker</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MockConstructorQuery</span>())))); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fixture.ResidueCollectors.Add( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">Postprocessor</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MockRelay</span>(), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AutoPropertiesCommand</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertiesOnlySpecification</span>()))); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PropertiesOnlySpecification</span> : <span style="color: #2b91af;">IRequestSpecification</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> IsSatisfiedBy(<span style="color: blue;">object</span> request) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> request <span style="color: blue;">is</span> <span style="color: #2b91af;">PropertyInfo</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The PropertiesPostprocessor assigned to the Fixture's Customizations has the same configuration as shown in AutoMoqPropertiesCustomization, but the object graph passed to the Fixture's ResidueCollectors is different. It's still a MockRelay, but now <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorated</a> with a Postprocessor instance, configured with an AutoPropertiesCommand instance, which is the class in AutoFixture responsible for implementing the AutoProperties feature. </p> <p> The only thing special about this configuration is that you need to pass a PropertiesOnlySpecification to the AutoPropertiesCommand instance. This is because, by default, AutoPropertiesCommand attempts to fill both properties and fields of a generated instance (we call that a <em>specimen</em>), but it turns out that when you ask <a href="https://github.com/Moq">Moq</a> to generate an instance of an interface, the generated type has a lot of public fields that you don't want to mess with. The PropertiesOnlySpecification class filters the population algorithm so that it only attempts to populate public properties. </p> <p> This test passes: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> AutoPopulatedProperties() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AutoPopulatedMoqPropertiesCustomization</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> h = fixture.Create&lt;<span style="color: #2b91af;">IHasProperties</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.NotEqual(<span style="color: blue;">default</span>(<span style="color: blue;">string</span>), h.Text); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.NotEqual(<span style="color: blue;">default</span>(<span style="color: blue;">int</span>), h.Number); }</pre> </p> <p> With the described AutoPopulatedMoqPropertiesCustomization, AutoFixture will populate all writable properties on interfaces generated by Moq. I still don't think this is a good idea, which is why it isn't the default behavior for AutoFixture, but as you can tell, it's not too hard to do. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. How to configure AutoMoq to set up all properties https://blog.ploeh.dk/2013/04/05/how-to-configure-automoq-to-set-up-all-properties 2013-04-05T15:13:00+00:00 Mark Seemann <div id="post"> <p> <em>This post explains how to configure AutoFixture.AutoMoq to setup all interface properties to behave like normal properties.</em> </p> <p> From time to time, people want the <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer/">AutoFixture.AutoMoq</a> <a href="/2013/03/11/auto-mocking-container/">Auto-Mocking Container</a> to set up all mock instances to have 'normal' property behavior. </p> <p> By default, <a href="https://github.com/Moq">Moq</a> doesn't implement any members of an interface. You have to explicitly configure the desired behavior using the Setup methods. This is also true for properties (which are really only special methods). </p> <p> Consider, as an example, this interface: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IHasProperties</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> Text { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> Number { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; } }</pre> </p> <p> Personally, I think such an interface design is a design smell, but that's not the issue here. </p> <h3 id="fb7863169e9144b39ef60af3074c7bf2"> Moq behavior <a href="#fb7863169e9144b39ef60af3074c7bf2" title="permalink">#</a> </h3> <p> The issue is that by default, Moq behaves like this: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> DefaultMoqDoesNotSetupProperties() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> h = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IHasProperties</span>&gt;().Object; &nbsp; &nbsp;&nbsp;&nbsp; h.Text = <span style="color: #a31515;">"foo"</span>; &nbsp;&nbsp;&nbsp; h.Number = 42; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(<span style="color: blue;">default</span>(<span style="color: blue;">string</span>), h.Text); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(<span style="color: blue;">default</span>(<span style="color: blue;">int</span>), h.Number); }</pre> </p> <p> As you can infer from this test, the IHasProperties instance completely ignores any attempt at assigning values to the properties. When you attempt to read the property values, Moq returns the default value for the property type - often null. </p> <p> You can configure each property individually, but you can also configure the Mock instance to implement all properties as thought they are normal, well-behaved properties: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> ManualSetupProperties() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> td = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IHasProperties</span>&gt;(); &nbsp;&nbsp;&nbsp; td.SetupAllProperties(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> h = td.Object; &nbsp; &nbsp;&nbsp;&nbsp; h.Text = <span style="color: #a31515;">"foo"</span>; &nbsp;&nbsp;&nbsp; h.Number = 42; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(<span style="color: #a31515;">"foo"</span>, h.Text); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(42, h.Number); }</pre> </p> <p> Notice the use of the SetupAllProperties method. So far, this is all about Moq, and really has nothing to do with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <h3 id="cd6afcb82dc2463f93a057eb4c997ddb"> AutoFixture.AutoMoq and interface property behavior <a href="#cd6afcb82dc2463f93a057eb4c997ddb" title="permalink">#</a> </h3> <p> Adhering to the <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment">Principle of least surprise</a>, the AutoFixture.AutoMoq glue library doesn't change this behavior: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> DefaultAutoMoqDoesNotSetupProperties() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize(<span style="color: blue;">new</span> <span style="color: #2b91af;">AutoMoqCustomization</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> h = fixture.Create&lt;<span style="color: #2b91af;">IHasProperties</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; h.Text = <span style="color: #a31515;">"foo"</span>; &nbsp;&nbsp;&nbsp; h.Number = 42; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(<span style="color: blue;">default</span>(<span style="color: blue;">string</span>), h.Text); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(<span style="color: blue;">default</span>(<span style="color: blue;">int</span>), h.Number); }</pre> </p> <p> However, it's easy to change the behavior in an ad hoc manner: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> ManualSetupPropertiesOnAutoMoq() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize(<span style="color: blue;">new</span> <span style="color: #2b91af;">AutoMoqCustomization</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> h = fixture.Create&lt;<span style="color: #2b91af;">IHasProperties</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Mock</span>.Get(h).SetupAllProperties(); &nbsp; &nbsp;&nbsp;&nbsp; h.Text = <span style="color: #a31515;">"foo"</span>; &nbsp;&nbsp;&nbsp; h.Number = 42; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(<span style="color: #a31515;">"foo"</span>, h.Text); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(42, h.Number); }</pre> </p> <p> As you can see, you can use the static Mock.Get method to get the underlying Mock&lt;IHasProperties&gt; and explicitly invoke the SetupAllProperties method. </p> <p> If you prefer AutoFixture to automate this for you, it's fairly easy to do. First, you'll need to write an ISpecimenBuilder that hooks into the Mock creation process and invokes the SetupAllProperties method: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PropertiesPostprocessor</span> : <span style="color: #2b91af;">ISpecimenBuilder</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">ISpecimenBuilder</span> builder; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> PropertiesPostprocessor(<span style="color: #2b91af;">ISpecimenBuilder</span> builder) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.builder = builder; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> Create(<span style="color: blue;">object</span> request, <span style="color: #2b91af;">ISpecimenContext</span> context) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">dynamic</span> s = <span style="color: blue;">this</span>.builder.Create(request, context); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (s <span style="color: blue;">is</span> <span style="color: #2b91af;">NoSpecimen</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> s; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s.SetupAllProperties(); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> s; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This class must <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorate</a> another ISpecimenBuilder, whose responsibility it is to create Mock&lt;T&gt; instances. The example code shown here is only a proof of concept, so it only does the bare minimum of defensive coding, assuming that if the object returned from the decorated ISpecimenBuilder is not a NoSpecimen, then it must be a Mock instance. In that case, it invokes SetupAllProperties on it. </p> <p> You'll need to compose this PropertiesPostprocessor class together with all the other building blocks of the AutoMoq glue library. Currently, there's no specific hook into AutoMoqCustomization that enables you to do that, but if you <a href="https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoMoq/AutoMoqCustomization.cs#L57">decompose AutoMoqCustomization</a>, you'll realize how easy it is to replicate the behavior while adding your new PropertiesPostprocessor class: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">AutoMoqPropertiesCustomization</span> : <span style="color: #2b91af;">ICustomization</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Customize(<span style="color: #2b91af;">IFixture</span> fixture) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fixture.Customizations.Add( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertiesPostprocessor</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MockPostprocessor</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MethodInvoker</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MockConstructorQuery</span>())))); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fixture.ResidueCollectors.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">MockRelay</span>()); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The only difference from the AutoMoqCustomization implementation is the addition of PropertiesPostprocessor decorating MockPostprocessor. </p> <p> This test now passes: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> AutoSetupProperties() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AutoMoqPropertiesCustomization</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> h = fixture.Create&lt;<span style="color: #2b91af;">IHasProperties</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; h.Text = <span style="color: #a31515;">"foo"</span>; &nbsp;&nbsp;&nbsp; h.Number = 42; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(<span style="color: #a31515;">"foo"</span>, h.Text); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(42, h.Number); }</pre> </p> <p> As you can see, the Mock object created by AutoFixture now implements properties as normal, well-behaved properties. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Structural Inspection https://blog.ploeh.dk/2013/04/04/structural-inspection 2013-04-04T07:50:00+00:00 Mark Seemann <div id="post"> <p> <em>One way to unit test a complex system is to test the behavior of each part and then verify that the structure of the composition is correct.</em> </p> <p> How do you unit test complex systems? How do you apply TDD to a complex system? This seems to be one of the most difficult questions related to unit testing. </p> <p> First, a little language lesson. <em>Complex</em> means that something is, by its very nature, composed of parts. <blockquote> "<a href="http://english.stackexchange.com/a/13445/39823">Complexity is intrinsic. Something is <em>complex</em> if it involves a lot of [metaphorical] moving parts even when considered as a Platonic ideal.</a>" </blockquote> You could say that <em>complexity</em> is the opposite of <em>unity</em>. It's not the same as <em>complicated</em>. <blockquote> "<a href="http://english.stackexchange.com/a/13445/39823">Complication is extrinsic. Something is <em>complicated</em> by external influences, or because of external influences.</a>" </blockquote> So how do you unit test complex systems? </p> <p> Traditional introductions to TDD tend to focus on simple systems, such as <ul> <li>Stack</li> <li>Fibonacci</li> <li>Prime factors</li> <li>Bowling game</li> <li>Word wrap</li> </ul> Common for these topics is that it's easy to explain the requirements and get started demonstrating how TDD works. It's what <a href="http://simpleprogrammer.com/">John Sonmez</a> calls <a href="http://simpleprogrammer.com/2010/12/12/back-to-basics-why-unit-testing-is-hard/">level 1 unit testing</a>. This is great if the purpose is to introduce the concept of TDD to programmers who are unfamiliar with the TDD <em>process</em>. However, the problem is that it doesn't really explain how to unit test complex systems (level 3 and up). </p> <p> A partial answer is provided by one of my favorite quotes from <a href="http://amzn.to/XBYukB">Design Patterns</a>: <blockquote> Favor object composition over class inheritance. </blockquote> If you can unit test the behavior each sub-component in isolation, then 'all' you need to do is to prove that those parts interact correctly. You can do this by a technique I call <em>Structural Inspection</em>. The idea is to inspect the structure of composed parts - a <a href="http://en.wikipedia.org/wiki/Facade_pattern">Facade</a>, if you will. If you know that two composed parts interact in a certain way, and you can also prove that the composed parts have the desired identity, you can prove that a complex system works correctly. </p> <p> Before I go on, I think it's important to point out that Structural Inspection is one among several approaches you can take to unit testing and TDD. Personally, I've had quite a few successes with this technique, but it tends to require a rather meticulous modus operandi, so it's not suitable in all cases. It works best against complex systems where you can't afford mistakes. The disadvantage is that you'll have to write a lot of tests and at times you may feel that you are moving very slowly, but the advantage is that the final system is not only <em>likely</em> to be correct, but it's <em>proven</em> to be correct. </p> <p> A note about the following example: it describes a way to unit test a shopping basket, including discounts, VAT, totals, etc. You may think that there are easier ways, and that the technique I describe is overkill. However, I've attempted to strike a balance between something that is sufficiently complex to make the examples realistic, yet still not so complex that it can fit in a single (although long) article. </p> <h3 id="62a31c0a869f46f6b571f046e3d1e927"> API design <a href="#62a31c0a869f46f6b571f046e3d1e927" title="permalink">#</a> </h3> <p> Structural Inspection is not only a unit testing technique; it's also an API design philosophy: <blockquote> What you compose, you can also expose. </blockquote> This means that if you compose objects by applying the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>, you can expose, as a property or field, what you just injected. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Discount</span> : <span style="color: #2b91af;">IBasketElement</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Discount(<span style="color: blue;">decimal</span> amount) &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> Amount { <span style="color: blue;">get</span>; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IBasketVisitor</span> Accept(<span style="color: #2b91af;">IBasketVisitor</span> visitor) }</pre> </p> <p> In the above example, the Discount class contains an amount (a <code>decimal</code> value), and implements the IBasketElement interface. The Accept method is the only member defined by that interface. The design philosophy of Structural Inspection is very simple: because it accepts an <code>amount</code> in its constructor, it should also expose that value as a public Amount property (<a href="/2013/03/11/listen-to-trivial-tests/">or field</a>). </p> <p> People unused to TDD often react in one of two ways: <ul> <li>"I don't like adding members only for testing"</li> <li>"It breaks encapsulation"</li> </ul> </p> <p> Well, if you follow TDD, all production code you write, you write as a response to a test you just wrote. <a href="/2011/11/10/TDDimprovesreusability/">The unit tests are the <em>first</em> client of the production API.</a> The important part is whether or not adding a member breaks encapsulation. Adding a property that exposes something that was passed into the constructor by a client, can hardly be said to break encapsulation. <a href="/2012/11/27/Encapsulationofproperties/">Exposing properties doesn't necessarily break encapsulation.</a> Even if you believe that encapsulation is 'information hiding', it makes no sense to hide data that was passed into the object instance by a third party. The object simply isn't the exclusive owner of that data. </p> <p> However, since the object (like the Discount class in the above example) already holds an instance of the data as a field, it might as well be courteous and share that information with its client. Otherwise, that would just put the onus on the client to remember the value it passed via the constructor. </p> <p> Ultimately, adding a property to a concrete class has <a href="/2011/02/28/Interfacesareaccessmodifiers/">no impact on the exposed interface</a>. Since a constructor is an implementation detail, by corollary Inspection Properties are also implementation details. While they don't break encapsulation, they are only part of the concrete class. At the interface level, they are invisible. <a href="/2011/10/25/SOLIDconcrete/">Interfaces are general, but concrete types are specific.</a> </p> <h3 id="ec0d148f24684b8b936414ed6dfdfa11"> Unit testing <a href="#ec0d148f24684b8b936414ed6dfdfa11" title="permalink">#</a> </h3> <p> The API design philosophy is easy to understand, but what does that mean for unit testing? The great thing is that it's possible to test each part of the whole in isolation, and then subsequently prove that parts correctly interact. </p> <p> Consider the Discount class. It's easy to <em>prove</em> that the <a href="/2012/08/31/ConcreteDependencies">injected amount</a> is properly exposed as a property: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(1)] [<span style="color: #2b91af;">InlineData</span>(2)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> AmountIsCorrect(<span style="color: blue;">int</span> expected) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">Discount</span>(expected); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.Amount; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(expected, actual); }</pre> </p> <p> Now, that's not particularly interesting in itself, but it's a small part of a larger whole. In the big picture, the concrete class is invisible, but the interfaces it implements are important. In this example, it must implement the IBasketElement interface. That's also easy to prove: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsBasketElement() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">Discount</span>(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(sut); }</pre> </p> <p> The above fact only states that Discount implements IBasketElement, but not that it <em>correctly</em> implements it. One last test verifies that this is, indeed, the case: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> AcceptReturnsCorrectResponse() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketVisitor</span>&gt;().Object; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">Discount</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> visitorStub = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketVisitor</span>&gt;(); &nbsp;&nbsp;&nbsp; visitorStub.Setup(v =&gt; v.Visit(sut)).Returns(expected); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.Accept(visitorStub.Object); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Same(expected, actual); }</pre> </p> <p> This test verifies that the Accept method is correctly implemented. It sets up <code>visitorStub</code> in such a way that it'll only return <code>expected</code> if <code>sut</code> is passed to the Visit method. The <code>actual</code> value returned by the Accept method must be the same as the <code>expected</code> value. </p> <p> Given these tests, the implementation must look something like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Discount</span> : <span style="color: #2b91af;">IBasketElement</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: blue;">decimal</span> amount; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Discount(<span style="color: blue;">decimal</span> amount) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.amount = amount; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IBasketVisitor</span> Accept(<span style="color: #2b91af;">IBasketVisitor</span> visitor) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> visitor.Visit(<span style="color: blue;">this</span>); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> Amount &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">this</span>.amount; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> With these tests in place, you can pretty much forget about the Discount class. It has appropriate concrete behavior and correctly implements the IBasketElement protocol. You can write similar tests for all other classes that implements IBasketElement. </p> <p> That may still seem trivial, but it begins to become interesting when you consider that a Basket is simply a collection of IBasketElement instances. That enables you to write this (Behavior Verification) test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> AcceptReturnsCorrectResult() { &nbsp;&nbsp;&nbsp; <span style="color: green;">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> v1 = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketVisitor</span>&gt;().Object; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> v2 = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketVisitor</span>&gt;().Object; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> v3 = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketVisitor</span>&gt;().Object; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> e1Stub = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> e2Stub = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketElement</span>&gt;(); &nbsp;&nbsp;&nbsp; e1Stub.Setup(e =&gt; e.Accept(v1)).Returns(v2); &nbsp;&nbsp;&nbsp; e2Stub.Setup(e =&gt; e.Accept(v2)).Returns(v3); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">Basket</span>(e1Stub.Object, e2Stub.Object); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.Accept(v1); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Same(v3, actual); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Teardown</span> }</pre> </p> <p> That may look difficult, but it simply states that when the Accept method is invoked with <code>v1</code>, it will pass <code>v1</code> to the first IBasketElement, and the return value will be <code>v2</code>, which is then passed to the second IBasketElement, which returns <code>v3</code>, which is the last result and thus expected to be returned by the Basket instance itself. This works for all Mock&lt;IBasketVisitor&gt;.Object instances, so according to the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>, it will work for any IBasketVisitor that conforms to the protocol. </p> <p> In other words: because you know that Discount correctly implements IBasketElement, you are guaranteed that <em>if</em> there's a Discount element in a basket, it <em>will</em> call any visitor's <code>Visit(Discount)</code> method. This puts you in a position to implement an IBasketVisitor to calculate the total for the basket. </p> <p> A BasketTotalVisitor must correctly implement all Visit methods defined by the IBasketVisitor interface and accumulate a total. However, whenever it encounters a Discount, it must subtract the discount from the total. Because BasketTotalVisitor is only a small part of a complex whole, this is easy to test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>] [<span style="color: #2b91af;">InlineData</span>(1, 1)] [<span style="color: #2b91af;">InlineData</span>(2, 1)] [<span style="color: #2b91af;">InlineData</span>(3, 2)] <span style="color: blue;">public</span> <span style="color: blue;">void</span> VisitDiscountReturnsCorrectResult( &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> initialTotal, &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> discount) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketTotalVisitor</span>(initialTotal); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.Visit(<span style="color: blue;">new</span> <span style="color: #2b91af;">Discount</span>(discount)); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> btv = <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">BasketTotalVisitor</span>&gt;(actual); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(initialTotal - discount, btv.Total); }</pre> </p> <p> Notice how this test uses Structural Inspection by verifying that the returned IBasketVisitor is, indeed, a BasketTotalVisitor instance, which has a Total property that the test verifies matches the expected value. It can only do that because the concrete BasketTotalVisitor class exposes the Total property. </p> <h3 id="34b3a597174c4bb1a08bd35e30a137de"> Verifying the whole <a href="#34b3a597174c4bb1a08bd35e30a137de" title="permalink">#</a> </h3> <p> You can keep on working like this: establishing that the behavior of each small part follows the correct protocols. Once all <a href="/2011/06/07/SOLIDCodeisnt/">fine-grained building blocks</a> are in place, the only thing left to do is to verify that they are correctly composed. This is where Structural Inspection is very powerful. This is how you can verify that a BasketPipeline will have the correct behavior: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutCorrectlyConvertsToPipe() { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CompositePipe</span>&lt;<span style="color: #2b91af;">Basket</span>&gt; sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketPipeline</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> visitors = sut &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Cast&lt;<span style="color: #2b91af;">BasketVisitorPipe</span>&gt;() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Select(bvp =&gt; bvp.Visitor); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> dv = <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">VolumeDiscountVisitor</span>&gt;(visitors.First()); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(500, dv.Threshold); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(.05m, dv.Rate); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> vv = <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">VatVisitor</span>&gt;(visitors.ElementAt(1)); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(.25m, vv.Rate); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> btv = <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">BasketTotalVisitor</span>&gt;(visitors.Last()); }</pre> </p> <p> This may look like a verbose test, but it's actually quite information-dense. </p> <p> First, the BasketPipeline is a CompositePipe&lt;Basket&gt;, and while I haven't shown that part in this article, I know from other unit tests that this class adheres to the Liskov Substitution Principle, so if the test can verify that its content is correct, the composed behavior must also be correct. </p> <p> In this test case, you can project all the contained pipes into the visitors they <a href="http://en.wikipedia.org/wiki/Adapter_pattern">adapt</a>. This is only possible because the concrete class BasketVisitorPipe exposes the concrete Visitor property. This is Structural Inspection in action. </p> <p> The BasketPipeline should be able to apply a volume discount, calculate VAT and the total. Because all the small parts have the correct behavior, you only need to verify that the BasketPipeline has the correct structure. </p> <p> A business rule states that a volume discount of five percent should be applied if the total value of all basket items is 500 or more. This means that the first visitor should be a VolumeDiscountVisitor, and its threshold should be 500 and the rate .05. You can use Structural Inspection to verify that this is the case. </p> <p> A legal requirement is that a VAT rate of 25 percent (real Danish VAT rate) should be applied to the value of the basket. This means that the next visitor should be a VatVisitor, and its rate should be .25 . </p> <p> Finally, the total price for the basket, including discounts and VAT should be calculated. This means that the last visitor should be a BasketTotalVisitor. </p> <p> The test verifies the <em>structure</em> of the API's Facade, and because the Facade has the correct structure and previous tests have proven that all the parts have the correct behavior, the Facade must have the desired higher-order behavior. </p> <p> In order to write this article, I TDD'ed all the code presented here (and more), and I deliberately didn't try to apply the BasketPipeline to some Basket instances until I had all the structure in place. Once I had that, I wrote a few <a href="/2012/06/27/FacadeTest/">Facade Tests</a> as sanity checks, and they all passed in the first attempt. </p> <h3 id="2c9e029216d549d88c2ecf0c041e6937"> Conclusion <a href="#2c9e029216d549d88c2ecf0c041e6937" title="permalink">#</a> </h3> <p> Structural Inspection is a useful technique when you need to unit test a complex system, and you need a high degree of confidence that everything works correctly. It's not the only option available to you. Another option is to use Triangulation with a large set of Facade Tests, but there may come a time where the number of test cases required to exercise all possible combinations of business rules becomes so large that Structural Inspection may be a better alternative. </p> <p> The code I wrote for this article contains 136 test cases, distributed over 88 test methods. It has 100 percent code coverage, and it covers 67 members over 11 classes and 3 interfaces. That's probably too enterprisey for a <em>simple</em> shopping basket calculation that only does volume discounts, VAT and total calculation. However, the way I've modeled this Basket API is quite extensible, so it may be quite well-suited for <em>complex</em> shopping baskets that might include various sorts of inclusive and exclusive discounts, VAT rates that are dependent on the category of goods in the basket, or that may be dependent on the shipping destination. Add to that calculation of shipping rates based on shipping origin and destination, as well as various characteristics (weight, volume, etc.) of the items in the basket, and Structural Inspection starts to look attractive. </p> <h3 id="d894c496b8fa45d385f30b941952b028"> Resources <a href="#d894c496b8fa45d385f30b941952b028" title="permalink">#</a> </h3> <p> For more details: <ul> <li> My Pluralsight course <a href="https://blog.ploeh.dk/advanced-unit-testing">Advanced Unit Testing</a> contains an entire 35 minute module on Structural Inspection. The course also comes with full source code. </li> <li> My NDC 2013 talk <a href="https://vimeo.com/68236489">Faking Homoiconicity in C# with graphs</a> approaches the same subject from a different angle. </li> </ul> </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="abf829340d0f462d9146be471c56bf26"> <div class="comment-author">Jin-Wook Chung <a href="#abf829340d0f462d9146be471c56bf26">#</a></div> <div class="comment-content"> <p> Thank you for sharing the great article. Recently I have been curious about how the structural instpection can work on funtional programming. Because funtion does not expose any properties to be inspected, I think that the only way to verify that whole thing correctly behaves is doing integration test. </p> <p> I was wondering whether you had a good approach about it. </p> </div> <div class="comment-date">2015-02-13 02:12 UTC</div> </div> <div class="comment" id="f40e5aeff7984b3cb4a6b55cdb4c35ab"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f40e5aeff7984b3cb4a6b55cdb4c35ab">#</a></div> <div class="comment-content"> <p> Thank you for writing. With functions, I've yet to find a good alternative to Structural Inspection, so I find myself writing more Integration Tests when I write F# code. On the other hand, because the F# type system is so much stronger than e.g. C#, I've also found that I can design data structures in such a way that <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable">illegal states become unrepresentable</a>, and that again means that I need to write fewer unit tests than I would have to with F#. </p> <p> Thus, even though I may have to write slightly more Integration Tests, I'm still quite happy with the trade-off. </p> </div> <div class="comment-date">2015-02-14 11:07 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Why trust tests? https://blog.ploeh.dk/2013/04/02/why-trust-tests 2013-04-02T07:40:00+00:00 Mark Seemann <div id="post"> <p> <em>Tests are trustworthy if they are simple and you've seen them fail.</em> </p> <p> When I started out with TDD some ten years ago, it was really hard to convince programmers that unit testing was valuable. It's better today, but much software is still produced without test coverage. One of the most common arguments for unit tests is to ask the question: "How do you know that your code works?" </p> <p> That's a darn good question, and one of the best answers is that you should cover the code with automated tests (unit tests). </p> <p> Okay, then: How do you know that your unit tests work? </p> <p> A test watches the correctness of the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, but <a href="http://en.wikipedia.org/wiki/Quis_custodiet_ipsos_custodes%3F">who watches the watchmen?</a> Should you write some more unit tests to test your unit tests? Is it going to be <a href="http://en.wikipedia.org/wiki/Turtles_all_the_way_down">turtles all the way down?</a> </p> <p> Obviously, that's not what we do. Then why do we trust unit tests? </p> <p> I think that there's two different reasons for that: <ul> <li>They are easy to review</li> <li>We've seen them fail</li> </ul> </p> <h3 id="56459bdf934c41a49c4287dbeaa19fa1"> Review <a href="#56459bdf934c41a49c4287dbeaa19fa1" title="permalink">#</a> </h3> <p> The reason that a lot of code fails to do what you think it does is because it's complicated. Sometimes there's also going to be subtle bugs, but I think that the most common problem is that as the code becomes more involved, it becomes more difficult for you to hold it in your brain. A simple <em>Hello World</em> application is easy to understand. Typical software isn't. </p> <p> Unit tests tend to be a lot simpler. First of all, <a href="http://xunitpatterns.com/Conditional%20Test%20Logic.html">unit tests should be deterministic</a>. This means that there should be a clear path through each test case. In other words, a unit test should have a <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic Complexity</a> of 1. </p> <p> If you follow the <a href="http://c2.com/cgi/wiki?ArrangeActAssert">AAA</a> or <a href="http://xunitpatterns.com/Four%20Phase%20Test.html">Four-Phase Test</a> patterns, keep the Cyclomatic Complexity at 1, keep the total line count down, and use good names, you should have a readable test case. Such test code is easy to review for correctness. If you are Pair Programming, you and your partner review the unit test code as you write it. If you use other review mechanisms (e.g. pull requests), the reviewer can review your unit test code. Even if you have no other person reviewing your code, keeping it simple helps you understand what it is that you do. Ultimately, it can't be <em>more</em> error prone than single-handedly writing production code without tests. </p> <p> Part of the reason why we trust unit tests is because, if written well, they are easy to review for correctness. </p> <h3 id="0cf7de4f7ad34833a9bd2c1e7d4d7093"> See the test fail <a href="#0cf7de4f7ad34833a9bd2c1e7d4d7093" title="permalink">#</a> </h3> <p> Deeply ingrained into proper TDD is the Red/Green/Refactor cycle. It's extremely important to see the test fail. Personally, I'm saved by this rule about once a week. I write a test and run it, and much to my surprise it turns out to be a <a href="http://xunitpatterns.com/false%20negative.html">false negative</a>. Although I've been doing TDD for some ten years, this still happens to me regularly, so I think it's one of those rigid rules you should always follow. Seeing the test fail verifies that the test indeed tests <em>something</em>. </p> <p> This is also the reason why, if you don't do TDD, but rather write tests after the fact, it becomes extremely important to follow <a href="http://agileinaflash.blogspot.dk/2009/02/writing-characterization-tests.html">a proper procedure for writing Characterization Tests</a>. </p> <p> Part of the reason that we trust unit tests is that we've seen them fail. It's like <a href="http://en.wikipedia.org/wiki/Double-entry_bookkeeping_system">double-entry bookkeeping</a>. The tests keep the SUT in place, and the SUT makes the tests pass. </p> <h3 id="b23efe23c1574f2b8ff85fb3c529ec9d"> Append-only tests <a href="#b23efe23c1574f2b8ff85fb3c529ec9d" title="permalink">#</a> </h3> <p> Both of the reasons I've provided are focused on the test as it's being created. At that time, you see it fail and you review it for correctness. As time goes by, you should end up with a test suite with lots of tests. Do you ever look at those tests? </p> <p> You trust them because they were correct <em>when they were created</em>. Are they still correct today? </p> <p> Well, they probably are if you never again touched them, but every time you modify a test case, you make it just a little less trustworthy. Similar in spirit to the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>, a test suite should be open for extension, but closed for modification. Trustworthy test suites are append-only. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Listen to trivial tests https://blog.ploeh.dk/2013/03/11/listen-to-trivial-tests 2013-03-11T13:57:00+00:00 Mark Seemann <div id="post"> <p> <em>Listen to your tests. If it's too much bother writing a test for a property, perhaps it's telling you that you don't need a property there.</em> </p> <p> A couple of days ago I published <a href="/2013/03/08/test-trivial-code/">a blog post</a> that caused some people to react, telling me that <a href="http://blog.markrendle.net/dont-unit-test-trivial-code/">I'm wrong</a>. That's most certainly a possibility, but what if I'm not? In this post I'd like to provide a bit of background on why I wrote that previous post, and where it may lead us. </p> <h3 id="e0d8ea0a4784415aa22f05507065cced"> Craftmanship <a href="#e0d8ea0a4784415aa22f05507065cced" title="permalink">#</a> </h3> <p> My blog post was a reaction to Robert C. Martin's post on <a href="http://blog.8thlight.com/uncle-bob/2013/03/06/ThePragmaticsOfTDD.html">The Pragmatics of TDD</a>. One of the qualities I've always appreciated of people like Robert C. Martin and <a href="http://martinfowler.com/">Martin Fowler</a> is that they consistently <em>explain</em> the reasoning behind their actions. The part of <em>The Pragmatics of TDD</em> that caused me to react was that it contains practices that aren't explained. </p> <p> Notice that Robert C. Martin takes time to explain why he doesn't unit test UI code (too much <em>fiddling</em> is involved). To me, that's a fully satisfactory explanation, so I didn't react against that at all. On the other hand, he states that he doesn't write tests against obviously trivial members, but no satisfactory explanation is given. </p> <p> Robert C. Martin has been programming for more than 40 years, and been doing TDD longer than me, so I have no doubt that this pragmatic approach works for him (as well as for many of the people who disagree with my previous post). However, if, for the sake of argument, we accept the <a href="http://en.wikipedia.org/wiki/Dreyfus_model_of_skill_acquisition">Dreyfus model of skill acquisition</a>, Robert C. Martin is clearly an <em>Expert</em>. He uses intuition and tacit knowledge to make appropriate decisions. Normally, as in <a href="http://amzn.to/XCJi9X">Clean Code</a>, he does a great job of making this tacit knowledge explicit, but in this case I found the explanation was missing, so I wanted to explore that question. </p> <p> Perhaps I was a bit high on laying out a logical argument. Yesterday, my wife (who's not at all a programmer) kindly read through the article and let me know that my language could have been less aggressive. It could, and I apologize if I offended anyone - particularly Robert C. Martin from whom I've learned so much. </p> <p> Most of all, though, I think I could have stated my motivation and context clearer. </p> <p> As I have previously stated on Twitter, I have come to the realization that I'm a Programmer more than I'm a Developer. While I love software, I love code more. If you ask my past employers and customers, they will tell you that I can get stuff done, too. However, I often code just to learn. This is an aspect of Software Craftmanship that is sometimes overlooked: Yes, we want to ship working software. However, we must always strive to become better at our craft, and that means practice. That means writing code for code's sake. That means obsessing over details. </p> <p> It also means thinking about aspects of programming that many people take for granted, such as properties. </p> <h3 id="83ef17b4359944a0b9079e9ad6571b6f"> Tests provide feedback <a href="#83ef17b4359944a0b9079e9ad6571b6f" title="permalink">#</a> </h3> <p> Several people attacked my previous post because they think it's not practical and that it puts TDD in a bad light. Actually, I think it puts TDD in a splendid light because it made me explicitly realize something that I've implicitly 'known' for a long time: we use way too many properties in our code. </p> <p> As always, we should take to heart the advice from <a href="http://amzn.to/VI81bP">GOOS</a>: <em>Listen to your tests.</em> </p> <p> If you think that it's ridiculous to write tests for <a href="/2011/05/26/CodeSmellAutomaticProperty">automatic properties</a>, then what is the feedback really about? </p> <p> Most of my critics present the choices like this: <table border="1"> <thead> <tr> <th></th> <th>Unit test</th> <th>No unit test</th> </tr> </thead> <tbody> <tr> <td>Property</td> <td></td> <td></td> </tr> </tbody> </table> Pick one of the boxes. </p> <p> However, I think the choice is more like this: <table border="1"> <thead> <tr> <th></th> <th>Unit test</th> <th>No unit test</th> </tr> </thead> <tbody> <tr> <td>Property</td> <td></td> <td></td> </tr> <tr> <td>No property</td> <td></td> <td></td> </tr> </tbody> </table> If you think that testing a property doesn't provide any value, then listen to the test: Does it even have to be a property? Couldn't a public field be equally good? </p> <p> In his post on the Pragmatics of TDD, Robert C. Martin states that getters and setters (properties) will be indirectly covered by other tests. This would also be true for public fields. This is even more pronounced in C# where the syntax for accessing a property is identical to the syntax for accessing a field. </p> <p> I agree with both Robert C. Martin and <a href="http://blog.markrendle.net/">Mark Rendle</a> that in a lot of cases, we don't really care about anything as nitty-gritty as a single property. What we <em>often</em> care about is the overall behavior of a system. This is also the approach I teach in my <a href="http://pluralsight.com">Pluralsight</a> course on <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test Driven Development</a>. You don't see me write tests of properties there. </p> <p> However, I made a mistake in that course, and another mistake in my previous post, and that was to design View Models with properties. Why are we even adding properties to View Models? The purpose of a View Model is to <em>render</em> in a particular way, either as UI, XML, JSON or something else. We care about the end result, not what the View Model class looks like. <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">At the boundaries, applications aren't object-oriented.</a> </p> <p> The JournalEntryModel from my Pluralsight course should really have look like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">JournalEntryModel</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTimeOffset</span> Time; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Distance; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">TimeSpan</span> Duration; }</pre> </p> <p> Notice that the class simply holds three public fields. This produces the same JSON as the version where those three members are properties. </p> <h3 id="b889c63a52e649939f87d3462f4a8116"> Summary <a href="#b889c63a52e649939f87d3462f4a8116" title="permalink">#</a> </h3> <p> There may still be cases where properties are appropriate. If, for example, instead of writing an end-to-end application, you are writing a reusable library, <a href="/2012/11/27/Encapsulationofproperties/">properties provide encapsulation</a>. In such cases, the property has a particular purpose, and it still makes sense to me to capture that behavior (yes: <em>behavior</em>, not data) in a regression test. </p> <p> However, most of the times properties aren't warranted, and you might as well use a public field - this is the <a href="http://en.wikipedia.org/wiki/You_aren't_gonna_need_it">YAGNI</a> principle applied. You don't have to test public fields because they can't possibly have any behavior. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="159c81a875fa466fade139d3b8523110"> <div class="comment-author"> <a href="http://gsscoder.github.com">Giacomo Stelluti Scala</a> <a href="#159c81a875fa466fade139d3b8523110">#</a></div> <div class="comment-content"> <p>In my opinion disciplines like <strong>TDD</strong> fall under a precise science and giving arbitrary rules to follow could be misleading.</p> <p>Totally agree with Mark Seemann, tests give us control. I dare add that also a bad wrote test give us information; for example it can put in evidence a code smell in the <i>system under test</i>.</p> <p>This discussion remind me a Stack Overflow question, <a href="http://stackoverflow.com/questions/153234/how-deep-are-your-unit-tests">How deep are your unit tests?</a>.</p> <p>It's interesting how Kent Beck start replying <i>I get paid for code that works, not for tests...</i> </p> <p>In conclusion writing good test code and applying <strong>TDD</strong> is finalized to prove the correctness of a system.</p> <p>It's a science; a lot of good literature (<i>as books quoted here</i>) and we can't rely on bunch of practical points.</p> <div class="comment-date">2013-03-11 18:01 UTC</div> </div> </div> <div class="comment" id="98b7776b22f54c07bb9e28bae9e31cd4"> <div class="comment-author"> <a href="http://blog.svick.org">Svick</a> <a href="#98b7776b22f54c07bb9e28bae9e31cd4">#</a></div> <div class="comment-content"> <p> I don't understand your line of reasoning. If I understand you correctly, you're saying auto-properties should be tested, because they might be later changed to properties with some behavior. And you're also saying that it might be a good idea to use public fields instead of auto-properties, because you don't have to test those. But public fields might also be changed to properties with behavior later. </p> <p> In other words, I don't really see the distinction between public fields and auto-properties. Either you should test both, or neither. But it doesn't make much sense to me to test auto-properties, but not public fields. </p> <p> Also, one thing you ommited is that auto-properties can have private setters. I think that's extremely useful and it's something public fields can't do. But, according to your reasoning, those should be tested too, using trivial tests. What does listening to those tests say? </p> <div class="comment-date">2013-04-01 12:40 UTC</div> </div> </div> <div class="comment" id="07ef0c4d919540249adbc2473be8ff4f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#07ef0c4d919540249adbc2473be8ff4f">#</a></div> <div class="comment-content"> Svick, if you change a public field into a property, it would be a breaking change, so there's a very real distinction there. </div> <div class="comment-date">2013-04-01 14:27 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Auto-mocking Container https://blog.ploeh.dk/2013/03/11/auto-mocking-container 2013-03-11T07:37:00+00:00 Mark Seemann <div id="post"> <p> <em>A unit test pattern</em> </p> <p> This article describes a unit test pattern called an Auto-mocking Container. It can be used as a solution to the problem: </p> <p class="text-center"> <em>How can unit tests be decoupled from Dependency Injection mechanics?</em> </p> <p class="text-center"> <strong>By using a heuristic composition engine to compose dynamic Test Doubles into the System Under Test</strong> </p> <p class="text-center"> <img src="/content/binary/auto-mocking-container-pattern-sequence-diagram.png" alt="Auto-mocking container pattern sequence diagram."> </p> <p> A major problem with unit tests is to make sure that they are robust in the face of a changing system. One of the most common problems programmers have with unit tests is the so-called <a href="http://xunitpatterns.com/Fragile%20Test.html">Fragile Test</a> smell. Every time you attempt to refactor your code, tests break. </p> <p> There are various reasons why that happens and the <em>Auto-mocking Container</em> pattern does not help in all cases, but a common reason that tests break is when you change the constructor of your System Under Test (SUT). In such cases, the use of an Auto-mocking Container can help. </p> <h3 id="e7c7aac866784817bd7a5fb71dc9929b"> How it works <a href="#e7c7aac866784817bd7a5fb71dc9929b" title="permalink">#</a> </h3> <p> In order to decouple a unit test from the mechanics of creating an instance of the SUT, the test code can repurpose a Dependency Injection (DI) Container to compose it. The DI Container must be customizable to the extent that it can automatically compose dynamic Test Doubles (mocks/stubs) into the SUT. </p> <p> It's important to notice that this technique is a pure unit testing concern. Even if you use an Auto-mocking Container in your unit test, you don't have to use a DI Container in your production code - or you can use a different DI Container than the one you've chosen to repurpose as an Auto-mocking Container. </p> <p> When a test case wants to exercise the SUT, it needs an instance of the SUT. Instead of creating the SUT directly by invoking its constructor, the test case uses an Auto-mocking Container to create the SUT instance. The Auto-mocking Container automatically supplies dynamic mocks in place of all the SUT's dependencies, freeing the test writer from explicitly dealing with this concern. </p> <p> This decouples each test case from the mechanics of how the SUT is created, making the test more robust. Even if the SUT's constructor is refactored, the test case remains unimpacted because the Auto-mocking Container dynamically deals with the changed constructor signature. </p> <h3 id="154f547d2b7f427cadf8322b3eba574f"> When to use it <a href="#154f547d2b7f427cadf8322b3eba574f" title="permalink">#</a> </h3> <p> Use an Auto-mocking Container when you are unit testing classes which utilize DI (particularly Constructor Injection). In a fully loosely coupled system, <a href="/2011/02/28/Interfacesareaccessmodifiers">constructors are implementation details</a>, which means that you may decide to change constructor signatures as part of refactoring. </p> <p> Particularly early in a code base's lifetime, the system and its internal design may be in flux. In order to enable refactoring, it's important to be able to change constructor signatures without worrying about breaking tests. In such cases, an Auto-mocking Container can be of great help. </p> <p> In well-established code bases, introducing an Auto-mocking Container is unlikely to be of much benefit, as the code is assumed to be more stable. </p> <p> Auto-mocking Containers are also less ideally suited for code-bases which rely more on a functional programming style with Value Objects and data flow algorithms, and less on DI. </p> <h3 id="f1bfdd7bd7d243e799da8d097e997062"> Implementation details <a href="#f1bfdd7bd7d243e799da8d097e997062" title="permalink">#</a> </h3> <p> Use an existing DI Container, but repurpose it as an Auto-mocking Container. Normal DI Containers don't serve dynamic mock objects by default, so you'll need to pick a DI Container which is sufficiently extensible to enable you to change its behavior as desired. </p> <p> In order to extend the DI Container with the ability to serve dynamic mock objects, you must also pick a suitable dynamic mock library. The Auto-mocking Container is nothing more than a 'Glue Library' that connects the behavior of the DI Container with the behavior of the dynamic mock library. </p> <p> Some open source projects exist that provide pre-packaged Glue Libraries as Auto-mocking Containers by combining two other libraries, but with good DI Containers and dynamic mock libraries, it's a trivial effort to produce the Auto-mocking Container as part of the unit test infrastructure. </p> <h3 id="5b915b5443b24279a9859cb57d27dbbf"> Motivating example <a href="#5b915b5443b24279a9859cb57d27dbbf" title="permalink">#</a> </h3> <p> In order to understand how unit tests can become tightly coupled to the construction mechanics of SUTs, imagine that you are developing a simple shopping basket web service using Test-Driven Development (TDD). </p> <p> To keep the example simple, imagine that the shopping basket is going to be a CRUD service exposed over HTTP. The framework you've chosen to use is based on the concept of a Controller that handles incoming requests and serves responses. In order to get started, you write the first (ice breaker) unit test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsController() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketController</span>(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IHttpController</span>&gt;(sut); }</pre> </p> <p> This is a straight-forward unit test which prompts you to create the BasketController class. </p> <p> The next thing you want to do is to enable clients to add new items to the basket. In order to do so, you write the next test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PostSendsCorrectEvent() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> channelMock = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">ICommandChannel</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketController</span>(channelMock.Object); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> item = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItemModel</span> { ProductId = 1234, Quantity = 3 }; &nbsp;&nbsp;&nbsp; sut.Post(item); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = item.AddToBasket(); &nbsp;&nbsp;&nbsp; channelMock.Verify(c =&gt; c.Send(expected)); }</pre> </p> <p> In order to make this test compile you need to add this constructor to the BasketController class: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: #2b91af;">ICommandChannel</span> channel; &nbsp; <span style="color: blue;">public</span> BasketController(<span style="color: #2b91af;">ICommandChannel</span> channel) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.channel = channel; }</pre> </p> <p> However, this breaks the first unit test, and you have to go back and fix the first unit test in order to make the test suite compile: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsController() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> channelDummy = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">ICommandChannel</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketController</span>(channelDummy.Object); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IHttpController</span>&gt;(sut); }</pre> </p> <p> In this example only a single unit test broke, but it gets progressively worse as you go along. </p> <p> Satisfied with your implementation so far, you now decide to implement a feature where the client of the service can read the basket. This is supported by the HTTP GET method, so you write this unit test to drive the feature into existence: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetReturnsCorrectResult() { &nbsp;&nbsp;&nbsp; <span style="color: green;">// Arrange&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> readerStub = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketReader</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketModel</span>(); &nbsp;&nbsp;&nbsp; readerStub.Setup(r =&gt; r.GetBasket()).Returns(expected); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> channelDummy = <span style="color: blue;">new</span> <span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">ICommandChannel</span>&gt;().Object; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketController</span>(channelDummy, readerStub.Object); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Act</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> response = sut.Get(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = response.Content.ReadAsAsync&lt;<span style="color: #2b91af;">BasketModel</span>&gt;().Result; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Assert</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(expected, actual); }</pre> </p> <p> This test introduces yet another dependency into the SUT, forcing you to change the BasketController constructor to this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: #2b91af;">ICommandChannel</span> channel; <span style="color: blue;">private</span> <span style="color: #2b91af;">IBasketReader</span> reader; &nbsp; <span style="color: blue;">public</span> BasketController(<span style="color: #2b91af;">ICommandChannel</span> channel, <span style="color: #2b91af;">IBasketReader</span> reader) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.channel = channel; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.reader = reader; }</pre> </p> <p> Alas, this breaks both previous unit tests and you have to revisit them and fix them before you can proceed. </p> <p> In this simple example, fixing a couple of unit tests in order to introduce a new dependency doesn't sound like much, but if you already have hundreds of tests, the prospect of breaking dozens of tests each time you wish to refactor by moving dependencies around can seriously hamper your productivity. </p> <h3 id="e9fbd9e4568e4113849c2b7097a75c34"> Refactoring notes <a href="#e9fbd9e4568e4113849c2b7097a75c34" title="permalink">#</a> </h3> <p> The problem is that the tests are too tightly coupled to the mechanics of how the SUT is constructed. The irony is that although you <em>somehow</em> need to create instances of the SUT, you shouldn't really care about <em>how</em> it happens. </p> <p> If you closely examine the tests in the motivating example, you will notice that the SUT is created in the Arrange phase of the test. This phase of the test is also called the Fixture Setup phase; it's where you put all the initialization code which is required before you can interact with the SUT. To be brutally honest, the code that goes into the Arrange phase is just a necessary evil. You should care only about the Act and Assert phases - after all, these tests don't test the constructors. In other words, what happens in the Arrange phase is mostly incidental, so it's unfortunate if that part of a test is holding you back from refactoring. You need a way to decouple the tests from the constructor signature, while still being able to manipulate the injected dynamic mock objects. </p> <p> There are various ways to achieve that goal. A common approach is to declare the SUT and its dependencies as class fields and compose them all as part of a test class' Implicit Setup . This can be an easy way to address the issue, but carries with it all the disadvantages of the <a href="http://xunitpatterns.com/Implicit%20Setup.html">Implicit Setup</a> pattern. Additionally, it can lead to an explosion of fields and low cohesion of the test class itself. </p> <p> Another approach is to build the SUT with a helper method. However, if the SUT has more than one dependency, you may need to create a lot of overloads of such helper methods, in order to manipulate only the dynamic mocks you may care about in a given test case. This tends to lead towards the <a href="http://martinfowler.com/bliki/ObjectMother.html">Object Mother</a> <a href="http://www.natpryce.com/articles/000714.html">(anti-)pattern</a>. </p> <p> A good alternative is an Auto-mocking Container to decouple the tests from the constructor signature of the SUT. </p> <h3 id="ab664442d63742df9c3af1b5130df94c"> Example: Castle Windsor as an Auto-mocking Container <a href="#ab664442d63742df9c3af1b5130df94c" title="permalink">#</a> </h3> <p> In this example you repurpose <a href="http://www.castleproject.org/">Castle Windsor</a> as an Auto-mocking Container. Castle Windsor is one among many DI Containers for .NET with a pretty good extensibility model. This can be used to turn the standard WindsorContainer into an Auto-mocking Container. In this case you will combine it with <a href="https://code.google.com/p/moq/">Moq</a> in order to automatically create dynamic mocks every time you need an instance of an interface. </p> <p> It only takes two small classes to make this happen. The first class is a so-called SubDependencyResolver that translates requests for an interface into a request for a mock of that interface: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">AutoMoqResolver</span> : <span style="color: #2b91af;">ISubDependencyResolver</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IKernel</span> kernel; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> AutoMoqResolver(<span style="color: #2b91af;">IKernel</span> kernel) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.kernel = kernel; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> CanResolve( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CreationContext</span> context, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ISubDependencyResolver</span> contextHandlerResolver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentModel</span> model, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DependencyModel</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> dependency.TargetType.IsInterface; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> Resolve( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CreationContext</span> context, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ISubDependencyResolver</span> contextHandlerResolver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentModel</span> model, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DependencyModel</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mockType = <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Mock</span>&lt;&gt;).MakeGenericType(dependency.TargetType); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> ((<span style="color: #2b91af;">Mock</span>)<span style="color: blue;">this</span>.kernel.Resolve(mockType)).Object; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The ISubDependencyResolver interface is a Castle Windsor extensibility point. It follows the Tester-Doer pattern, which means that the WindsorContainer will first call the CanResolve method, and subsequently only call the Resolve method if the return value from CanResolve was <code>true</code>. </p> <p> In this implementation you return <code>true</code> if and only if the requested dependency is an interface. When this is the case, you construct a generic type of Moq's Mock&lt;T&gt; class. For example, in the code above, if <code>dependency.TargetType</code> is the interface ICommandChannel, the <code>mockType</code> variable becomes the type Mock&lt;ICommandChannel&gt;. </p> <p> The next line of code asks the kernel to resolve that type (e.g. Mock&lt;ICommandChannel&gt;). The resolved instance is cast to Mock in order to access and return the value of its Object property. If you ask the kernel for an instance of Mock&lt;ICommandChannel&gt;, it'll return an instance of that type, and its Object property will then be an instance of ICommandChannel. This is the dynamic mock object you need in order to compose a SUT with automatic Test Doubles. </p> <p> The other class wires everything together: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShopFixture</span> : <span style="color: #2b91af;">IWindsorInstaller</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Install( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IWindsorContainer</span> container, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IConfigurationStore</span> store) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; container.Kernel.Resolver.AddSubResolver( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AutoMoqResolver</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; container.Kernel)); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .For(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Mock</span>&lt;&gt;))); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; container.Register(<span style="color: #2b91af;">Classes</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .FromAssemblyContaining&lt;Shop.<span style="color: #2b91af;">MvcApplication</span>&gt;() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Pick() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .WithServiceSelf() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .LifestyleTransient()); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This is an IWindsorInstaller, which is simply a way of packaging Castle Windsor configuration together in a class. In the first line you add the AutoMoqResolver. In the next line you register the open generic type Mock&lt;&gt;. This means that the WindsorContainer knows about any generic variation of Mock&lt;T&gt; you might want to create. Recall that AutoMockResolver asks the kernel to resolve an instance of Mock&lt;T&gt; (e.g. Mock&lt;ICommandChannel&gt;). This registration makes this possible. </p> <p> Finally, the ShopFixture scans through all public types in the SUT assembly and registers their concrete types. This is all it takes to turn Castle Windsor into an Auto-mocking Container. </p> <p> Once you've done that, you can start TDD'ing, and you should rarely (if ever) have to go back to the ShopFixture to tweak anything. </p> <p> The first test you write is equivalent to the first test in the previous motivating example: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> SutIsController() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> container = <span style="color: blue;">new</span> <span style="color: #2b91af;">WindsorContainer</span>().Install(<span style="color: blue;">new</span> <span style="color: #2b91af;">ShopFixture</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = container.Resolve&lt;<span style="color: #2b91af;">BasketController</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color: #2b91af;">IHttpController</span>&gt;(sut); }</pre> </p> <p> First you create the container and install the ShopFixture. This is going to be the first line of code in all of your tests. </p> <p> Next, you ask the container to resolve an instance of BasketController. At this point there's only a default constructor, so Auto-mocking hasn't even kicked in yet, but you get an instance of the SUT against which you can make your assertion. </p> <p> Now it's time to write the next test, equivalent to the second test in the motivating example: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PostSendsCorrectEvent() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> container = <span style="color: blue;">new</span> <span style="color: #2b91af;">WindsorContainer</span>().Install(<span style="color: blue;">new</span> <span style="color: #2b91af;">ShopFixture</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = container.Resolve&lt;<span style="color: #2b91af;">BasketController</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> item = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketItemModel</span> { ProductId = 1234, Quantity = 3 }; &nbsp;&nbsp;&nbsp; sut.Post(item); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = item.AddToBasket(); &nbsp;&nbsp;&nbsp; container &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Resolve&lt;<span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">ICommandChannel</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Verify(c =&gt; c.Send(expected)); }</pre> </p> <p> Here an interesting thing happens. Notice that the first two lines of code are the same as in the previous test. You don't need to define the mock object before creating the SUT. In fact, you can exercise the SUT without ever referencing the mock. </p> <p> In the last part of the test you need to verify that the SUT interacted with the mock as expected. At that time you can ask the container to resolve the mock and verify against it. This can be done in a single method call chain, so you don't even have to declare a variable for the mock. </p> <p> This is possible because the ShopFixture registers all Mock&lt;T&gt; instances with the so-called Singleton lifestyle. The Singleton lifestyle shouldn't be confused with the Singleton design pattern. It means that every time you ask a container instance for an instance of a type, you will get back the same instance. In Castle Windsor, this is the default lifestyle, so this code: </p> <p> <pre style="margin: 0px;">container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Mock</span>&lt;&gt;)));</pre> </p> <p> is equivalent to this: </p> <p> <pre style="margin: 0px;">container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Mock</span>&lt;&gt;)) &nbsp;&nbsp;&nbsp; .LifestyleSingleton());</pre> </p> <p> When you implement the Post method on BasketController by injecting ICommandChannel into its constructor, the test is going to pass. This is because when you ask the container to resolve an instance of BasketController, it will need to first resolve an instance of ICommandChannel. </p> <p> <img src="/content/binary/auto-mocking-windsor-container-sequence-diagram.png"> </p> <p> This is done by the AutoMoqResolver, which in turn asks the kernel for an instance of Mock&lt;ICommandChannel&gt;. The Object property is the dynamically generated instance of ICommandChannel, which is injected into the BasketController instance ultimately returned by the container. </p> <p> When the test case subsequently asks the container for an instance of Mock&lt;ICommandChannel&gt; the same instance is reused because it's configured with the Singleton lifestyle. </p> <p> More importantly, while the second test prompted you to change the constructor signature of BasketController, this change didn't break the existing test. </p> <p> You can now write the third test, equivalent to the third test in the motivating example: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetReturnsCorrectResult() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> container = <span style="color: blue;">new</span> <span style="color: #2b91af;">WindsorContainer</span>().Install(<span style="color: blue;">new</span> <span style="color: #2b91af;">ShopFixture</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = container.Resolve&lt;<span style="color: #2b91af;">BasketController</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasketModel</span>(); &nbsp;&nbsp;&nbsp; container.Resolve&lt;<span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IBasketReader</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Setup(r =&gt; r.GetBasket()) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Returns(expected);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> response = sut.Get(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = response.Content.ReadAsAsync&lt;<span style="color: #2b91af;">BasketModel</span>&gt;().Result; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(expected, actual); }</pre> </p> <p> Once more you change the constructor of BasketController, this time to inject an IBasketReader into it, but none of the existing tests break. </p> <p> This example demonstrated how to repurpose Castle Windsor as an Auto-mocking Container. It's important to realize that this is still a pure unit testing concern. It doesn't require you to use Castle Windsor in your production code. </p> <p> Other DI Containers can be repurposed as Auto-mocking Containers as well. <a href="http://stackoverflow.com/a/2462443/126014">One option is Autofac</a>, and while not strictly a DI Container, <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer">another option is AutoFixture</a>. </p> <p> <em>This article first appeared in the <a href="http://skillsmatter.com/go/open-source-journal">Open Source Journal</a> 2012 issue 4. It is reprinted here with kind permission.</em> </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="f1cabc86c9654d9fa86ffb9dbe0965a4"> <div class="comment-author"> <a href="http://megakemp.com">Enrico Campidoglio</a> <a href="#f1cabc86c9654d9fa86ffb9dbe0965a4">#</a></div> <div class="comment-content"> <p>First of all, thank you for a very informative article.</p> <p>I'd like to call out another example of an Auto Mocking Container that I've picked up recently and enjoyed working with. It's built into <a href="https://github.com/machine/machine.fakes">Machine.Fakes</a>, an <a href="https://github.com/machine/machine.specifications">MSpec</a> extension library that abstracts the details of different mocking libraries behind an expressive API.</p> <p>I rewrote the third test with MSpec and Machine.Fakes to demonstrate its usage:</p> <div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> <span style="color: #2b91af">When_sending_an_http_get_request</span> : <span style="color: #2b91af">WithSubject</span>&lt;<span style="color: #2b91af">BasketController</span>&gt; { <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #2b91af">BasketModel</span> expectedContent; <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #2b91af">HttpResponseMessage</span> response; <span style="color: #2b91af">Establish</span> context = () =&gt; { expected = <span style="color: #0000ff">new</span> <span style="color: #2b91af">BasketModel</span>(); The&lt;<span style="color: #2b91af">IBasketReader</span>&gt;() .WhenToldTo(r =&gt; r.GetBasket()) .Return(expectedContent); } <span style="color: #2b91af">Because</span> of = () =&gt; response = Subject.Get(); <span style="color: #2b91af">It</span> should_return_a_successful_http_response = () =&gt; response.IsSuccessfulStatusCode.ShouldBeTrue(); <span style="color: #2b91af">It</span> should_return_the_contents_of_the_basket = () =&gt; response.ReadContentAs&lt;<span style="color: #2b91af">BasketModel</span>&gt;().ShouldEqual(expectedContent); } </pre></div> <p>There are a couple of interesting points to highlight here. The <code>WithSubject&lt;T&gt;</code> class hides the details of composing the SUT, which is then exposed through the <code>Subject</code> property. It makes sure that any dependencies are automatically fulfilled with Test Doubles, created by delegating to a mocking library of choice. Those <em>singleton</em> instances can then be obtained from the test using the inherited <code>The&lt;T&gt;()</code> to setup stubs or make assertions.</p> <p>What I like about Machine.Fakes is that it gives me the advantages of using an Auto Mocking Container, while keeping the details of how the SUT is composed out of the test, thus allowing it to stay focused on the behavior to verify. </div> <div class="comment-date">2013-03-13 22:27 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Test trivial code https://blog.ploeh.dk/2013/03/08/test-trivial-code 2013-03-08T10:19:00+00:00 Mark Seemann <div id="post"> <p> <em>Even if code is trivial you should still test it.</em> </p> <p> A few days ago, Robert C. Martin posted a blog post on <a href="http://blog.8thlight.com/uncle-bob/2013/03/06/ThePragmaticsOfTDD.html">The Pragmatics of TDD</a>, where he explains that he doesn't test-drive <em>everything</em>. Some of the exceptions he give, such as not test-driving GUI code and true one-shot code, make sense to me, but there are two exceptions I think are <em>inconsistent</em>. </p> <p> Robert C. Martin states that he doesn't test-drive <ul> <li>getters and setters</li> <li>one-line functions</li> <li>functions that are obviously trivial</li> </ul> Actually, that really boils down to a single rule about not test-driving 'trivial' code, as getters and setters ('properties' for .NET programmers) also tend to be trivial. </p> <p> There are several problems with this exception that I'd like to point out: <ul> <li>It confuses cause and effect</li> <li>Trivial code may not stay trivial</li> <li>It's horrible advice for novices</li> </ul> Let me expand of each of these points. </p> <h3 id="5aaed4b041174eee85b697dc59526e21"> Causality <a href="#5aaed4b041174eee85b697dc59526e21" title="permalink">#</a> </h3> <p> The whole point of Test-<em>Driven</em> Development is that the tests <em>drive</em> the implementation. The test is the <em>cause</em> and the implementation is the <em>effect</em>. If you accept this premise, then how on Earth can you decide not to write a test because the implementation is going to be trivial? <em>You don't know that yet.</em> It's logically impossible. </p> <p> Robert C. Martin himself proposed the <a href="http://cleancoder.posterous.com/the-transformation-priority-premise">Transformation Priority Premise</a> (TPP), and one of the points here is that as you start out test-driving new code, you should strive to do so in small, formalized steps. The first step is extremely likely to leave you with a 'trivial' implementation, such as returning a constant. </p> <p> With the TPP, the only difference between trivial and non-trivial implementation code is how far into the TDD process you've come. So if 'trivial' is all you need, the only proper course is to write that single test that demonstrates that the trivial behavior works as expected. Then, according to the TPP, you'd be done. </p> <h3 id="03fe237e9c0e4247a4c112390066b588"> Encapsulation <a href="#03fe237e9c0e4247a4c112390066b588" title="permalink">#</a> </h3> <p> Robert C. Martin's exception about getters and setters is particularly confounding. If you consider a getter/setter (or .NET property) trivial, why even have it? Why not expose a public class field instead? </p> <p> There are good reasons why you shouldn't expose public class fields, and they are <a href="/2012/11/27/Encapsulationofproperties">all releated to encapsulation</a>. Data should be exposed via getters and setters because it gives you the option of changing the implementation in the future. </p> <p> What do we call the process of changing the implementation code without changing the behavior? </p> <p> This is called <em>refactoring</em>. How do we know that when we change the implementation code, we don't change the behavior? </p> <p> As <a href="http://martinfowler.com/">Martin Fowler</a> states in <a href="http://amzn.to/YPdQDf">Refactoring</a>, you <em>must</em> have a good test suite in place as a safety net, because otherwise you don't know if you broke something. </p> <p> Successful code lives a long time and evolves. What started out being trivial may not remain trivial, and you can't predict which trivial members will remain trivial years in the future. It's important to make sure that the trivial behavior remains correct when you start to add more complexity. A regression suite addresses this problem, but only if you actually test the trivial features. </p> <p> Robert C. Martin argues that the getters and setters are indirectly tested by other test cases, but while that may be true when you introduce the member, it may not stay true. Months later, those tests may be gone, leaving the member uncovered by tests. </p> <p> You can look at it like this: with TDD you may be applying the TPP, but for trivial members, the time span between the first and second transformation may be measured in months instead of minutes. </p> <h3 id="ac1c7ad592c24d0d8bbe802f973ff64d"> Learning <a href="#ac1c7ad592c24d0d8bbe802f973ff64d" title="permalink">#</a> </h3> <p> It's fine to be pragmatic, but I think that this 'rule' that you don't have to test-drive 'trivial' code is <a href="http://programmers.stackexchange.com/a/188188/19115">horrible advice for novices</a>. If you give someone learning TDD a 'way out', they will take it every time things become difficult. If you provide a 'way out', at least make the condition explicit and measurable. </p> <p> A fluffy condition that "you may be able to predict that the implementation will be trivial" isn't at all measurable. It's exactly this way of thinking that TDD attempts to address: you may think that you already know how the implementation is going to look, but letting tests drive the implementation, it often turns out that you'll be suprised. What you originally thought would work doesn't. </p> <h3 id="416a1d8b223d402e835e3389d2469e81"> Addressing the root cause <a href="#416a1d8b223d402e835e3389d2469e81" title="permalink">#</a> </h3> <p> Am I insisting that you should test-drive all getters and setters? Yes, indeed I am. </p> <p> But, you may say, it takes too much time? Ironically, Robert C. Martin has <a href="http://blog.8thlight.com/uncle-bob/2013/03/05/TheStartUpTrap.html">much to say</a> on exactly that subject: <blockquote>The only way to go fast, is to go well</blockquote> </p> <p> Even so, let's see what it would be like to apply the TPP to a property (Java programmers can keep on reading. A C# property is just syntactic suger for a getter and setter). </p> <p> Let's say that I have a DateViewModel class and I'd like it to have a Year property of the <a href="http://msdn.microsoft.com/en-us/library/system.int32.aspx">int</a> type. The first test is this: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetYearReturnsAssignedValue() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateViewModel</span>(); &nbsp;&nbsp;&nbsp; sut.Year = 2013; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(2013, sut.Year); }</pre> </p> <p> However, applying the Devil's Advocate technique, the correct implementation is this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">int</span> Year { &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> 2013; } &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { } }</pre> </p> <p> That's just by the TPP book, so I have to write another test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetYearReturnsAssignedValue2() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateViewModel</span>(); &nbsp;&nbsp;&nbsp; sut.Year = 2010; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(2010, sut.Year); }</pre> </p> <p> Together, these two tests prompt me to correctly implement the property: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">int</span> Year { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre> </p> <p> While those two tests could be refactored into a single <a href="http://xunitpatterns.com/Parameterized%20Test.html">Parameterized Test</a>, that's still <em>a lot of work</em>. Not only do you need to write two test cases for a single property, but you'd have to do <em>exactly the same</em> for every other property! </p> <p> Exactly the same, you say? And what are you? </p> <p> Ah, you're a programmer. What do programmers do when they have to do exactly the same over and over again? </p> <p> Well, if you make up inconsistent rules that enable you to skip out of doing things that hurt, then that's the wrong answer. If it hurts, do it more often. </p> <p> Programmers automate repeated tasks. You can do that when testing properties too. Here's how I've done that, using <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>, <span style="color: #2b91af;">AutoWebData</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> YearIsWritable(<span style="color: #2b91af;">WritablePropertyAssertion</span> a) { &nbsp;&nbsp;&nbsp; a.Verify(<span style="color: #2b91af;">Reflect</span>&lt;<span style="color: #2b91af;">DateViewModel</span>&gt;.GetProperty&lt;<span style="color: blue;">int</span>&gt;(sut =&gt; sut.Year)); }</pre> </p> <p> This is a declarative way of checking exactly the same behavior as the previous two tests, but in a single line of code. </p> <p> Root cause analysis is in order here. It seems as if the cost/benefit ratio of test-driving getters and setters is too high. However, I think that when Robert C. Martin stops there, it's because he considers the cost fixed, and the benefit is then too low. However, while the benefit may seem low, the cost doesn't have to be fixed. Lower the cost, and the cost/benefit ratio improves. This is why you should also test-drive getters and setters. </p> <p> <strong>Update 2014.01.07:</strong> As a follow-up to this article, <a href="http://weblogs.asp.net/jgalloway">Jon Galloway</a> interviewed me about this, and other subject, for <a href="http://herdingcode.com/herding-code-165-mark-seemann-on-autofixture-and-unit-testing">a Herding Code podcast</a>. </p> <p> <strong>Update, 2018.11.12:</strong> A <a href="/2018/11/12/what-to-test-and-not-to-test">new article</a> reflects on this one, and gives a more nuanced picture of my current thinking. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="985fd9d6cd3048b4b4be0e3362be9a82"> <div class="comment-author"> <a href="http://nikosbaxevanis.com/blog/">Nikos Baxevanis</a> <a href="#985fd9d6cd3048b4b4be0e3362be9a82">#</a></div> <div class="comment-content"> <p>Here is a concrete, pragmatic, <a href="https://github.com/ploeh/Albedo/issues/53">example</a> where this post really helped in finding a solution to a dilemma on whether (or not) we should test a (trivial) overriden <code>ToString</code> method's output. Mark Seemann explained why in this case (while the code is trivial) we should not test that particular method. Even though it is a very concrete example, I believe it will help the reader on making decisions about when, and whether it's worth (or not), to test trivial code.</p> </div> <div class="comment-date">2014-01-08 14:40 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Code readability hypotheses https://blog.ploeh.dk/2013/03/04/code-readability-hypotheses 2013-03-04T13:21:00+00:00 Mark Seemann <div id="post"> <p> <em>This post proposes a scientific experiment about code readability</em> </p> <p> Once in a while someone tries to drag me into one of many so-called 'religious' debates over code readability: <ul> <li>Tabs versus spaces?</li> <li>Where should the curly braces go?</li> <li>Should class members be prefixed (e.g. with an underscore)?</li> <li>Is the <code>this</code> C# keyword redundant?</li> <li>etc.</li> </ul> </p> <p> In most cases, the argument revolves around what is most readable. </p> <p> Let's look at the C# keyword <code>this</code> as an example. Many programmers feel that it's redundant because they can omit it without changing the program. The compiler doesn't care. However, as <a href="http://martinfowler.com/">Martin Fowler</a> wrote in <a href="http://amzn.to/YPdQDf">Refactoring</a>: "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Code is read much more than written, so I don't buy the argument about redundancy. </p> <p> There are lots of circumstances when code is read. Often, programmers read code from inside their <a href="http://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a>. This has been used as an argument against <a href="http://en.wikipedia.org/wiki/Hungarian_notation">Hungarian notation</a>. However, at other times, you may be reading code as part of a review process - e.g. on <a href="https://github.com/">GitHub</a> or <a href="http://www.codeplex.com/">CodePlex</a>. Or you may be reading code on a blog post, or in a book. </p> <p> Often I feel that the <code>this</code> keyword helps me because it provides information about the scope of a variable. However, I also preach that <a href="/2011/06/07/SOLIDCodeisnt/">code should consist of small classes with small methods</a>, and if you follow that principle, variable scoping should be easily visible on a single screen. </p> <p> Another argument against using the <code>this</code> keyword is that it's simply noise and that it makes it harder to read the program, simply because there's more text to read. Such a statement is <em>impossible to refute with logic</em>, because it depends on the person reading the code. </p> <p> Ultimately, the readability of code depends on circumstances and is highly subjective. For that reason, we can't arrive at a conclusion to any of those 'religious' debates by force of logic. However, what we <em>could</em> do, on the other hand, is to perform a set of scientific experiments to figure out what is most readable. </p> <h3 id="76938d9c6ed04de5bb80a92500f21be9"> A science experiment idea for future computer scientists <a href="#76938d9c6ed04de5bb80a92500f21be9" title="permalink">#</a> </h3> <p> Here's an idea for a computer science experiment: <ol> <li>Pick a 'religious' programming debate (e.g. whether or not the <code>this</code> keyword enhances or reduces readability).</li> <li>Form a hypothesis (e.g. predict that the <code>this</code> keyword enhances readability).</li> <li>Prepare a set of simple code listings, each with two alternatives (with and without <code>this</code>).</li> <li>Randomly select two groubs of test subjects.</li> <li>Ask each group to explain to you what the code does (what is the result of calling a method, etc.). One group gets one alternative set, and the other group gets the other set.</li> <li>Measure how quickly members of each group arrives at a correct conclusion.</li> <li>Compare results against original hypothesis.</li> </ol> <a rel="license" href="http://creativecommons.org/licenses/by/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by/3.0/88x31.png"></a><br><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">Code readability hypotheses</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="/2013/03/04/code-readability-hypotheses" property="cc:attributionName" rel="cc:attributionURL">Mark Seemann</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/deed.en_US">Creative Commons Attribution 3.0 Unported License</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="937bcbca71084c67b0cabcb3fbfc416f"> <div class="comment-author"> <a href="http://nikosbaxevanis.com">Nikos Baxevanis</a> <a href="#937bcbca71084c67b0cabcb3fbfc416f">#</a></div> <div class="comment-content"> <p> Totally agreed. </p> <p> I believe that these 'religious' debates is the result of overreliance to some popular Productivity Tools. If these tools never existed those debates would appear less often. </p> <p> Where possible, I use <code>this</code> and <code>base</code>. </p> <p> From a code reader's perspective, I also prefer <code>this</code> since I can immediately understand whether a member of a class is either a static member or an instance member. After all, it is possible to prefix a static member with <code>_</code> but not with <code>this</code>. </p> </div> <div class="comment-date">2013-03-04 14:04 UTC</div> </div> <div class="comment" id="8bc273327d644b7db35f25f425898855"> <div class="comment-author"> <a href="http://http://gsscoder.blogspot.it">Giacomo Stelluti Scala</a> <a href="#8bc273327d644b7db35f25f425898855">#</a></div> <div class="comment-content"> <p>The import thing that emerges from this interesting post is that readabiliy is a <strong>key feature</strong> of code.</p> <p>I'm a fun of this concept: <strong>code is meant to be read</strong>. A person able to program is not a programmer, like a person able to write is not a writer.</p> <p>The good compromise is common sense.</p> <p>The scenitifical experiment proposed could give use misleading informations, since the <i>human factor</i> is a problematic variable to put into the equation (not to mention <i>Heisenberg</i>); but I would be curious to see results.</p> <p>Coming back to the <code>this</code> keyword, my personal opinion is that it can be omitted, if your naming convention supplies a readable alternative (like the <code>_</code> underscore prefix before private instance members).</p> <div class="comment-date">2013-03-04 18:57 UTC</div> </div> </div> <div class="comment" id="6dc0864b9e9348c8bcce8dda49467f61"> <div class="comment-author"> <a href="http://blog.strobaek.org">Karsten Strøbæk</a> <a href="#6dc0864b9e9348c8bcce8dda49467f61">#</a></div> <div class="comment/content"> <p> I also agree that code should be readable. </p> <p> However, this being said, the proposed method for testing the hypothesis does not hold from a statistical point of view. </p> <p> Just to mentionen a few of the issues: <ol> <li>The two groups could have different leves of experience which would influence their 'ability' to read and understand the code</li> <li>Demografics such as age, gender, native language, education, ..., would also influence the result</li> <li>The individual members of the group could be biased towards one or the other, in the used example for or against the use of the <strong>this</strong> keyword</li> <li>Simply to compile large enough groups to ensure a low statistics error would be an issue</li> </ol> </p> <p> The word 'religious' is mentioned and this is the main problem. Different from science that is based on deduction, religion is based on beliefe (or faith) and precisely because of this cannot be tested or verified. </p> </div> <div class="comment-date">2013-03-12 12:00 UTC</div> </div> <div class="comment" id="97428363b8884cd884047293b00c760f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#97428363b8884cd884047293b00c760f">#</a></div> <div class="comment-content"> <p> Karsten, I think you're reading practical issues into my proposal. Would you agree that all but the last of your reservations could be addressed if one were to pick large enough groups of test subjects, without selection bias? </p> <p> That's what I meant when I wrote "Randomly select two groups of test subjects". </p> <p> If one were able to pick large enough groups of people, one should be able to control for variables such as demographics, experience, initial biases, and so on. </p> <p> Your last reservation, obviously, is fundamentally different. How many people would one need in order to be able to control for such variables in a statistically sound fashion? Probably thousands, if not tens of thousands, of programmers, including professional developers. This precludes the use of normal test subject populations of first-year students. </p> <p> Clearly, compiling such a group of test subjects would be a formidable undertaking. It'd be expensive. </p> <p> My original argument, however, was that I consider it possible to scientifically examine contentious topics of programming. I didn't claim that it was realistic that anyone would fund such research. </p> <p> To be clear, however, the main point of the entire article is that we can't reach any conclusions about these controversial issues by arguing about them. If we wish to settle them, we'll have to invest in scientific experiments. I have no illusions that there's any desire to invest the necessary funds and work into answering these questions, but I would love to be proven wrong. </p> </div> <div class="comment-date">2019-02-14 9:14 UTC</div> </div> <div class="comment" id="fef627676986467bbbee0f60cb38707d"> <div class="comment-author"><a href="https://www.clearlaunch.com/meet-the-team/">Arturo Hernandez</a> <a href="#fef627676986467bbbee0f60cb38707d">#</a></div> <div class="comment-content"> <p> There is a huge problem in the way that we evaluate software quality. Karsten, is naming many of the reasons that make it difficult. But given that it is difficult, The only rational way to solve it without relying as heavily credentials, personality and repetition. Is to establish guidelines and have multiple opionins/measurements of code samples. </p> <p> I think we overvalue experience in evaluating code. And in the contrary, there are real issues with expert bias, particularly of older programmers who have good credentials but are more financially and emotionally invested in a particular technology. In general, it is much more difficult to write good code, than to read it. Therefore, most programmers can read, understand and appreciate good code. While they may not have been able to write from scratch themselves. The only exception would be for true neophytes (which there may be ways to exclude). We also need to be leary about software qualities that are hard to grasp and evaluate. Unexplainable or unclear instinct can lead to bad outcomes. </p> <p> There is real oportunity here to more scientifically quantify aspects of code quality. I wish more people devoted time to it. </p> </div> <div class="comment-date">2019-02-14 17:36 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Outside-In TDD versus DDD https://blog.ploeh.dk/2013/03/04/outside-in-tdd-versus-ddd 2013-03-04T08:01:00+00:00 Mark Seemann <div id="post"> <p> <em>Is there a conflict between Outside-In TDD and DDD? I don't think so.</em> </p> <p> A reader of <a href="http://amzn.to/12p90MG">my book</a> recently asked me a question: </p> <blockquote> <p> "you are a DDD and TDD fan and at the same time you like to design your User Interfaces first. I have been reading about DDD and TDD for some time now and your approach confuses me, TDD is about writing tests first to lead the design, DDD is focusing on the domain yet you start with the UI." </p> </blockquote> <p> In my opinion there's no conflict between Outside-In TDD and DDD, but I must admit that I was a bit surprised by the question. It never occurred to me that there's an apparent conflict between these two approaches, but now that I was asked the question, I understand why one would think so. In other words, I think it's a good question that warrants a proper answer. </p> <p> Before discussing the synthesis of Outside-In TDD and DDD I think we need to examine each concept individually. </p> <h3 id="554b45c692db4e13bd2ce6246d776359"> DDD <a href="#554b45c692db4e13bd2ce6246d776359" title="permalink">#</a> </h3> <p> As far as I can tell, <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> is a horribly misunderstood book. By corollary, so is the DDD concept itself. The problem with the book is that it provides value and guidance along two axes: <ul> <li>Patterns and other coding advice</li> <li>Principles for analysis and modeling</li> </ul> The 'problem' is that all the patterns and coding concepts, such as <em>Aggregate Root</em>, <em>Specification</em>, <em>Factories</em>, <em>Value Objects</em>, <em>Entities</em>, <em>Services</em>, etc. are very useful. However, here's the catch: Many of these constructs are useful in themselves. They aren't really related to <em>Domain</em>-Driven Design. If Eric Evans had written a book called <em>Design Patterns for Supple Design</em>, there would have been much less confusion. He could have written <em>another</em> book with all the rest of the content and called that book "Domain-Driven Design," and less confusion would have ensued. </p> <p> Don't let the fact that all these patterns are described in a book called <em>Domain-Driven Design</em> trick you into thinking that they are intrinsically bound to DDD. Using the patterns don't make your code Domain-Driven, and you can do DDD <em>without</em> them. Obviously, you can also combine them. </p> <p> This provides a partial answer to the original question. Outside-In TDD doesn't conflict with the patterns described in <em>Domain-Driven Design</em>, just as it doesn't conflict with most other design patterns. </p> <p> That still leaves the 'true' DDD: the principles for analyzing and modeling the subject matter for a piece of software. If that is a design technique, and Outside-In TDD is also a design technique, isn't there a conflict? </p> <h3 id="f991203285474bdaa0c2410610359602"> Outside-In TDD <a href="#f991203285474bdaa0c2410610359602" title="permalink">#</a> </h3> <p> Outside-In TDD, as I describe it in <a href="https://blog.ploeh.dk/outside-in-tdd">my Pluralsight course</a>, isn't a design technique. <a href="/2010/12/22/TheTDDApostate">TDD isn't a design technique.</a> Granted, TDD provides fast <em>feedback</em> about the design you are implementing, but it's not a blind design technique. </p> <h3 id="971d051d776e4d83943cd96be86fc934"> Outside-In TDD and DDD <a href="#971d051d776e4d83943cd96be86fc934" title="permalink">#</a> </h3> <p> Once you realize that a major reason that Outside-In TDD and DDD seems to be at odds, is because of the false premise that TDD is a design technique, you should also realize that there isn't, after all, any conflict. There's no reason that you can't do both, but it doesn't mean you always should. <ul> <li>You can do Outside-In TDD without DDD. If you are building a CRUD application, DDD is probably going to be overkill. If so, skip it.</li> <li>You can do DDD without Outside-In TDD. If you are being paid to build or extract a true Domain Model, it makes sense to do so decoupled from any sort of application boundaries.</li> <li>You can combine Outside-In TDD with DDD, but I think this will often make best sense as an iterative approach. First you build the application. Then you realize that the business logic within the application is so complex that you'll need to extract a proper Domain Model in order to keep it maintainable.</li> </ul> In the end, remember that your employer/customer pays you to solve a particular problem with software, not to follow a particular software design principle. The reason I often start with the UI or perhaps a RESTful API is that this is the part of the application that the product owner cares about. </p> <p> In my Pluralsight course, I also discuss the <a href="http://martinfowler.com/bliki/TestPyramid.html">Test Pyramid</a>. While it makes sense to <em>begin</em> development at the boundary (e.g. UI) of an application, you shouldn't <em>stay</em> at the boundary. Most tests should still be unit tests. Once you reach the phase where you realize that you'll need to evolve a proper Domain Model, you should do that with unit tests, not boundary tests. Domain Models should be implemented decoupled from all boundaries (UI, I/O and persistence), so you can only directly test a Domain Model with unit tests. </p> <p> In the end, it's all a question of perspective. Outside-In TDD states that you should begin your TDD process at the boundary. In the beginning, it will focus on the externally visible behavior of the system, but as development progresses, other parts of the system must be dealt with. This is where DDD can be valuable. The lack of conflict eventually stems from the fact that Outside-In TDD and DDD operate on different levels of application architecture, with little overlap. </p> <p> In my experience, a lot of applications can be written with Outside-In TDD without a Domain Model ever having to evolve - and still be maintainable. These applications may still employ the 'Supple Design Patterns', but that doesn't mean that they are Domain-Driven. </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="ff50c0ba91244ce281be190cff323841"> <div class="comment-author"><a href="http://gorodinski.com">Lev Gorodinski</a> <a href="#ff50c0ba91244ce281be190cff323841">#</a></div> <div class="comment-content"><p>Thank for this insightful post. It inspired me to write a <a href="http://gorodinski.com/blog/2013/03/11/the-two-sides-of-domain-driven-design/">post</a> about the two sides of DDD.</p> <p>PS: IMO, commenting via pull-request is too burdensome. Have you considered Disqus?</p> </div> <div class="comment-date">2013-03-15 12:22 MST</div> </div> <div class="comment" id="e6b127c9408e42b29ff3b33d777b82fb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e6b127c9408e42b29ff3b33d777b82fb">#</a></div> <div class="comment-content"> <p> Lev, thank you for your comment. While I've considered Disqus and other alternatives, I <a href="/2013/03/03/moving-the-blog-to-jekyll">don't want to go in that direction</a>. Free services have a tendendcy to not remain free forever - Google Reader being a current case in point. </p> </div> <div class="comment-date">2013-03-16 08:36 GMT</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Moving the blog to Jekyll https://blog.ploeh.dk/2013/03/03/moving-the-blog-to-jekyll 2013-03-03T09:32:00+00:00 Mark Seemann <div id="post"> <p> ploeh blog <em>is sorta moving</em> </p> <p> For more than four years <em>ploeh blog</em> has been driven by <a href="http://www.dasblog.info/">dasBlog</a>, since I originally <a href="/2009/01/28/LivingInInterestingTimes.aspx">had to move away</a> from <a href="http://blogs.msdn.com/b/ploeh/">my original MSDN blog</a>. At the time, dasBlog seemed like a good choice for a variety of reasons, but not too long after, development seemed to stop. </p> <p> It's my hope that, as a reader, you haven't been too inconvenienced about that choice of blogging engine, but as the owner and author, I've grown increasingly frustrated with dasBlog. Yesterday, <em>ploeh blog</em> moved to a new platform and a new host. </p> <h3 id="d69625b14bf14c4a9e95b1b67fa3a2b8"> What's new? <a href="#d69625b14bf14c4a9e95b1b67fa3a2b8" title="permalink">#</a> </h3> <p> The new platform for <em>ploeh blog</em> is <a href="http://jekyllbootstrap.com/">Jekyll-Bootstrap</a> hosted on <a href="http://pages.github.com/">GitHub pages</a>. If you don't know what Jekyll-Bootstrap is, it's a parsing engine that creates a complete static web site from templates and data. This means that when you read this on the internet, you are reading a static HTML page served directly from a file. There's no application server involved. </p> <p> Since <em>ploeh blog</em> is now a static web site, I had to make a few shortcuts here and there. One example is that I can't have any forms on the web site, so I can't have a <em>contact me</em> form - but I didn't have that before either, so that's not really a problem. There are still plenty of ways in which you can <a href="/about/">contact me</a>. </p> <p> Another example is that the blog can't have a server-driven comment engine. As an alternative, Jekyll provides several options for including a third-party commenting service, such as <a href="http://disqus.com/">Disqus</a>, <a href="http://www.livefyre.com/">Livefyre</a>, <a href="http://intensedebate.com/">IntenseDebate</a>, etc. I could have done that too, but have chosen to try another alternative. </p> <p> There are several reasons why I prefer to avoid a third-party solution. First of all, in case you haven't noticed, I'm into this whole blogging thing for the long haul. I've been steadily blogging since January 2006, and have no plans to stop. Somehow, I expect to keep blogging for a longer period than I expect the average third-party content provider to last - or to keep their service free. </p> <p> Second, I think it's more proper to keep the comments with <em>ploeh blog</em>. These comments were originally submitted by their authors to <em>ploeh blog</em>, which implies that they trusted me to treat them with respect. If I somehow were to move all those comments to a third party, what would become of the copyright? </p> <p> Third, while it wasn't entirely trivial to merge the comments into each post, it looked like the simplest solution, compared to have to somehow migrate comments for some 200+ posts to a third party using an unknown (to me) API. </p> <p> What this all means is that I've decided to simply keep the comments as part of the body of the post. Comments, too, is static content. This also means that comments are included in syndication feeds - a side effect that I'm not entirely happy about, but for which I don't yet have a good solution. </p> <p> So, if comments are all static, does it mean that I no longer accept comments? No, I still accept comments. Just <a href="https://github.com/ploeh/ploeh.github.com#readme">send me a pull request</a>. Snarky, perhaps, but the advantage of that is that if you want, you'll have a lot more flexibility if you want to comment: you could include code, images, tables, lists, etc. The only checkpoint you'll have to get past is me :) </p> <p> Actually, you are welcome to send me pull requests for other enchancements as well, if you'd like. Did I spell a word incorrectly? Send me a pull request. Is a link broken? Send me a pull request. Do you know way more about web site design than I do and want to help? Send me a pull request :) Seriously, as you may have noticed, <em>ploeh blog</em> is currently using the default Twitter Bootstrap design. That's not particularly interesting to look at, but I hope that the most important part of the blog is the content, so I didn't use much energy on changing the look - it probably wouldn't have been to the better anyway. </p> <p> Since the content is the most important part of the blog, I've also gone to some lentgth to ensure that old permalinks to posts still work. If you encounter a link that no longer works, please let me know (or send me a pull request). </p> <p> Finally, as all RSS subscribers have probably noticed by now, switching the blog has caused some RSS hiccups. While the old feed address still works, the permalink URL scheme has changed, because I didn't think it made sense to have static files and URLs ending with .aspx. </p> <h3 id="e2b31775d2214086b89430ae90813c24"> What was wrong with dasBlog? <a href="#e2b31775d2214086b89430ae90813c24" title="permalink">#</a> </h3> <p> Originally, I chose dasBlog as a blogging engine because it had some attractive features. However, the main problem with it is that it's no longer being maintained. There seems to be no roadmap for it. </p> <p> Second, the commenting feature didn't work well. Many readers had trouble submitting comments, particularly if they wanted to include links or other HTML formatting. I also got a lot of comment spam. </p> <p> Finally, the authoring experience slowly degraded. The built-in editor only worked in Firefox, and while I don't mind having Firefox installed, it was a cause of concern that I could only edit with one out of three installed browsers. What if it also stopped working in Firefox? </p> <p> It was definitely time to move on, but whereto? </p> <h3 id="6171a59800864c71a566da60d1246e68"> Why Jekyll? <a href="#6171a59800864c71a566da60d1246e68" title="permalink">#</a> </h3> <p> Before I decided to go with Jekyll, I looked at various options for blogging sofware or services, including <a href="http://wordpress.com/">WordPress.com</a>, <a href="http://www.blogger.com">Blogger</a>, <a href="http://www.funnelweblog.com/">FunnelWeb</a>, <a href="http://www.orchardproject.net/">Orchard</a>, and others. However, none of them could meet my requirements: <ul> <li>Simplicity. Everything that required a relational database to host a blog was immediately dismissed. This is 2013. A RDBMS for a single user blog site? Seriously?</li> <li>Easy hosting. Requiring a RDBMS significantly constrains my deployment options. It also tends to make hosting much more expensive. Since January 2009 I've been hosting <em>ploeh blog</em> on my own server in my home, but I've become increasingly annoyed at having to operate my own server. This is, after all, 2013.</li> <li>Backwards compatible. The new blog should be able to handle the old blog's permalinks. Redirects were OK, but I couldn't just leave hundreds of external links to <em>ploeh blog</em> with a 404. That pretty much ruled out all hosted services.</li> <li>Forward compatible. This is the second time I change blog platform, and it's not going to be the last. I need a solution that enables me to move forward, taking all the existing content with me to a new service or platform.</li> </ul> </p> <p> I seriously toyed with writing my own blogging engine to meet all those requirements, but soon realized that Jekyll was a pretty good match. It's not perfect, but good enough for now. </p> </div> <hr> <div id="comments"> <h2 id="comments-header">Comments</h2> <div class="comment" id="7eaf50f7e9f149c7b6104605e6a6b350"> <div class="comment-author">Laurence <a href="#7eaf50f7e9f149c7b6104605e6a6b350">#</a></div> <div class="comment-content"> <p>While I like the move of the blog post to GitHub, the overhead of creating comments here is just too high. I like having some level of entry barrier to keep the quality up but this is more to the point of completely detracting additional useful information from being posted in comments.</p> <p>An option that comes to mind could be possibly using the Issue tracking system in GitHub for comments or something that gives access to the GitHub markdown features like the pages themselves.</p> </div> <div class="comment-date">2013-07-21 09:05 UST +10</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 3 https://blog.ploeh.dk/2013/03/01/AutoFixture3 2013-03-01T13:36:31+00:00 Mark Seemann <div id="post"> <p> <em>Announcing the general release of AutoFixture 3.</em> </p> <p> AutoFixture 3 has been under way in a long time, but after about of a month of limited beta/CTP/whatever trial, I've decided to release it as the 'official' current version of AutoFixture. Since AutoFixture uses <a href="http://semver.org/">Semantic Versioning</a>, the shift from 2.16.2 to 3.0.0 (actually, 3.0.1) signifies <em>breaking changes</em>. While we've made an effort to make the impact of these breaking changes as light as possible, it's worth noting that it's probably not a good idea upgrading to AutoFixture 3 five minutes before you are heading out of the door for the weekend. </p> <p> Read more about what's new and what has changed in the <a href="https://github.com/AutoFixture/AutoFixture/wiki/AutoFixture-3.0-Release-Notes">release notes</a>. </p> <p> Thank you to everyone who helped make this happen. There have been a lot of people contributing, providing feedback, asking intelligent questions, etc. but I particularly want to thank <a href="http://nikosbaxevanis.com/">Nikos Baxevanis</a> for all the work he's been putting into AutoFixture. Conversely, as is usual in such circumstances, all mistakes are my responsibility. </p> <h3 id="bb8a3c58237a4bf7aba39b27893d76a7"> What's new? <a href="#bb8a3c58237a4bf7aba39b27893d76a7" title="permalink">#</a> </h3> <p> When you read the release notes, you may find the amount of new features is quite unimpressive. This is because we've been continually releasing new features under the major version number of 2, in a sort of Continuous Delivery process. Because of the way Semantic Versioning works, breaking changes must be signaled by increasing the major version number, so while we could release new features as they became available, I thought it better to bundle up breaking changes into a bigger release; if not, we'd be looking at AutoFixture 7 or 8 now... And as I'm writing this, I notice that my installation of Chrome is on major version 25, so maybe it's just something I need to get over. </p> <h3 id="bc2e8433ba994b558398ad28ff40ed75"> Personal release notes <a href="#bc2e8433ba994b558398ad28ff40ed75" title="permalink">#</a> </h3> <p> One thing that the release notes don't explicitly mention, but I'd like to talk about a little is that AutoFixture 3 is running on an extensively rebuilt kernel. One of the visions I had for AutoFixture 3 was that the kernel structure should be immutable, and it should be as easy to take away functionality than it is to add custom functionality. Taking away functionality was really hard (or perhaps impossible) in AutoFixture 2, and that put a constraint on what you could do with AutoFixture. It also put a constraint on how opinionated its defaults could be. </p> <p> The AutoFixture 3 kernel makes it possible to take away functionality, as well as add new functionality. At the moment, you have to understand the underlying kernel to figure out how to do that, but in the future I plan to surface an API for that as well. </p> <p> In any case, I find it interesting that using <a href="/2012/01/03/SOLIDIsAppendonly.aspx">real SOLID</a> principles, I was able to develop the new kernel in parallel with the version 2 kernel. Partly because I was very conscious of not introducing breaking changes, but also because I follow the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>, I rarely had to touch existing code, but could build the new parts of the kernel in parallel. Even more interestingly, I think, is the fact that once that work was done, <em>replacing the kernel turned out to be a point release</em>. This happened late November 2012 - more than three months ago. If you've been using AutoFixture 2.14.1 or a more recent version, you've already been using the AutoFixture 3 kernel for some time. </p> <p> The reason I find this interesting is that sometimes casual readers of my blog express skepticism over whether the principles I talk about can be applied in 'realistic' settings. According to <a href="http://www.ohloh.net">Ohloh</a>, AutoFixture has a total of 197,935 lines of code, and I consider that a fair sized code base. I practice what I preach. QED. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with Autofixture exercise 8 https://blog.ploeh.dk/2013/02/18/StringCalculatorkatawithAutofixtureexercise8 2013-02-18T10:00:28+00:00 Mark Seemann <div id="post"> <p> This is the eighth post in <a href="/2013/02/06/StringCalculatorKataWithAutoFixture.aspx">a series of posts</a> about the <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a> done with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <p> This screencast implements the requirement of the kata's exercise 8 and 9. Although the kata has nine steps, it turned out that I slightly misinterpreted the requirements of exercise 8 and ended up implementing the requirements of exercise 9 as well as those of exercise 8 - or perhaps I was just gold plating. </p> <p> <iframe src="http://www.youtube.com/embed/J0eWzTfFJ0k" allowfullscreen="" frameborder="0" height="315" width="420"></iframe> </p> <p> If you liked this screencast, you may also like my <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with Autofixture exercise 7 https://blog.ploeh.dk/2013/02/15/StringCalculatorkatawithAutofixtureexercise7 2013-02-15T10:10:27+00:00 Mark Seemann <div id="post"> <p> This is the seventh post in <a href="/2013/02/06/StringCalculatorKataWithAutoFixture.aspx">a series of posts</a> about the <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a> done with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <p> This screencast implements the requirement of the kata's exercise 7. </p> <p> <iframe src="http://www.youtube.com/embed/7UeRRIABdDE" allowfullscreen="" frameborder="0" height="315" width="420"></iframe> </p> <p> <a href="/2013/02/18/StringCalculatorKataWithAutofixtureExercise8.aspx">Next exercise</a> </p> <p> If you liked this screencast, you may also like my <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with Autofixture exercise 6 https://blog.ploeh.dk/2013/02/14/StringCalculatorkatawithAutofixtureexercise6 2013-02-14T11:53:57+00:00 Mark Seemann <div id="post"> <p> This is the sixth post in <a href="/2013/02/06/StringCalculatorKataWithAutoFixture.aspx">a series of posts</a> about the <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a> done with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <p> This screencast implements the requirement of the kata's exercise 6. </p> <p> <iframe src="http://www.youtube.com/embed/QKF2mFAYhlM" allowfullscreen="" frameborder="0" height="315" width="420"></iframe> </p> <p> <a href="/2013/02/15/StringCalculatorKataWithAutofixtureExercise7.aspx">Next exercise</a> </p> <p> If you liked this screencast, you may also like my <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with Autofixture exercise 5 https://blog.ploeh.dk/2013/02/13/StringCalculatorkatawithAutofixtureexercise5 2013-02-13T12:13:01+00:00 Mark Seemann <div id="post"> <p> This is the fifth post in <a href="/2013/02/06/StringCalculatorKataWithAutoFixture.aspx">a series of posts</a> about the <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a> done with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <p> This screencast implements the requirement of the kata's exercise 5. </p> <p> <iframe src="http://www.youtube.com/embed/Mv3OGtxKuCg" allowfullscreen="" frameborder="0" height="315" width="420"></iframe> </p> <p> <a href="/2013/02/14/StringCalculatorKataWithAutofixtureExercise6.aspx">Next exercise</a> </p> <p> If you liked this screencast, you may also like my <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5c61cac3b63e461198b318f1027c7299"> <div class="comment-author">robi.y <a href="#5c61cac3b63e461198b318f1027c7299">#</a></div> <div class="comment-content">Hello and thanks for this awesome series.<br> It seems you assume that the int generator always returns non-negative numbers.<br> Is this true and by design? why?<br> Thanks</div> <div class="comment-date">2013-02-19 09:25 UTC</div> </div> <div class="comment" id="6c7bd82968b2496588a2b22d2d8972c3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6c7bd82968b2496588a2b22d2d8972c3">#</a></div> <div class="comment-content">Integers generated by AutoFixture have, by design, always been positive, although the algorithm has changed during the years.<br> <br> In AutoFixture 3, <a href="https://github.com/AutoFixture/AutoFixture/wiki/AutoFixture-3.0-Release-Notes#wiki-random-numbers">numbers are random</a>.<br> <br> In AutoFixture 2.2+, <a href="http://megakemp.com/2011/09/06/behavior-changes-in-autofixture-2-2-anonymous-numbers/">all numbers are generated by a strictly monotonically increasing sequence</a>.<br> <br> In AutoFixture prior to 2.2, <a href="/2009/04/03/CreatingNumbersWithAutoFixture.aspx">numbers were also generated as a rising sequence, but one sequence per number type</a>.<br> <br> Common for all is that they are 'small, positive integers'.</div> <div class="comment-date">2013-02-19 19:46 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with Autofixture exercise 4 https://blog.ploeh.dk/2013/02/12/StringCalculatorkatawithAutofixtureexercise4 2013-02-12T13:45:36+00:00 Mark Seemann <div id="post"> <p> This is the fourth post in <a href="/2013/02/06/StringCalculatorKataWithAutoFixture.aspx">a series of posts</a> about the <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a> done with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <p> This screencast implements the requirement of the kata's exercise 4. </p> <p> <iframe src="http://www.youtube.com/embed/kEWIwFfmsa4" allowfullscreen="" frameborder="0" height="315" width="420"></iframe> </p> <p> <a href="/2013/02/13/StringCalculatorKataWithAutofixtureExercise5.aspx">Next exercise</a> </p> <p> If you liked this screencast, you may also like my <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="44346435b37340c6bd267e1c34a6fa8d"> <div class="comment-author">Jiggaboo <a href="#44346435b37340c6bd267e1c34a6fa8d">#</a></div> <div class="comment-content">Wow, this test is so unreadable! I wouldn't allow it in my project.<br> Also I wonder what if you have error in summing? Error will show sth. like: Expected 34212, was 19857. How do you know what numbers where generated by int generator?</div> <div class="comment-date">2013-02-12 18:22 UTC</div> </div> <div class="comment" id="b9b801ae4a104e2db4015475e5a35acc"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b9b801ae4a104e2db4015475e5a35acc">#</a></div> <div class="comment-content">Undreadable? Well, my mother and my wife both find C# unreadable, while I don't (and I suspect you don't either). Other people find F# unreadable, while it's beginning to grow on me. Personally, I find Clojure unreadable, but I'm certain it's only a matter of time.<br> <br> Unit tests that leverage AutoFixture tends to be terser and more declarative in nature than more traditional unit tests, which tend to look more imperative. Like every other new thing, it takes some time getting used to.<br> <br> That said, there are things about the String Calculator kata that makes it a less than ideal fit for AutoFixture. The problem with the kata is that it's essentially just a bunch of more or less arbitrary rules (throw on negative numbers, ignore numbers bigger than 1000, support custom delimiter strings, etc.) There's no real domain being modelled here.<br> <br> If there had been a more proper domain, a refactoring phase would probably have prompted me to redesign the API and introduced a custom Delimiter Value Object that I could have requested in the test, instead of requesting a Generator. That would have made the test even terser, but also, IMO, more readable. However, I didn't want to do that in this screencast, as I was concerned it would be too much of a digression.</div> <div class="comment-date">2013-02-13 13:27 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with Autofixture exercise 3 https://blog.ploeh.dk/2013/02/11/StringCalculatorkatawithAutofixtureexercise3 2013-02-11T09:04:16+00:00 Mark Seemann <div id="post"> <p> This is the third post in <a href="/2013/02/06/StringCalculatorKataWithAutoFixture.aspx">a series of posts</a> about the <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a> done with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <p> This screencast implements the requirement of the kata's exercise 3. </p> <p> <iframe src="http://www.youtube.com/embed/RrY6DXfStoE" allowfullscreen="" frameborder="0" height="315" width="420"></iframe> </p> <p> <a href="/2013/02/12/StringCalculatorKataWithAutofixtureExercise4.aspx">Next exercise</a> </p> <p> If you liked this screencast, you may also like my <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with Autofixture exercise 2 https://blog.ploeh.dk/2013/02/07/StringCalculatorkatawithAutofixtureexercise2 2013-02-07T10:17:27+00:00 Mark Seemann <div id="post"> <p> This is the second post in <a href="/2013/02/06/StringCalculatorKataWithAutoFixture.aspx">a series of posts</a> about the <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a> done with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <p> This screencast implements the requirement of the kata's exercise 2. </p> <p> <iframe src="http://www.youtube.com/embed/m1_SDVlbf2M" allowfullscreen="" width="420" frameborder="0" height="315"></iframe> </p> <p> <a href="/2013/02/11/StringCalculatorKataWithAutofixtureExercise3.aspx">Next exercise</a> </p> <p> If you liked this screencast, you may also like my <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with AutoFixture exercise 1 https://blog.ploeh.dk/2013/02/06/StringCalculatorkatawithAutoFixtureexercise1 2013-02-06T14:05:30+00:00 Mark Seemann <div id="post"> <p> This is the first post in <a href="/2013/02/06/StringCalculatorKataWithAutoFixture.aspx">a series of posts</a> about the <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a> done with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>. </p> <p> This screencast sets up the Visual Studio projects and completes the first exercise of the kata. </p> <p> <iframe src="http://www.youtube.com/embed/9M3wkWAdnc4" allowfullscreen="" frameborder="0" height="315" width="420"></iframe> </p> <p> <a href="/2013/02/07/StringCalculatorKataWithAutofixtureExercise2.aspx">Next exercise</a> </p> <p> If you liked this screencast, you may also like my <a href="http://pluralsight.com">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4be73930ace9472c990366798e1e7930"> <div class="comment-author">Gary McLean Hall <a href="#4be73930ace9472c990366798e1e7930">#</a></div> <div class="comment-content">Any chance you could dive a bit deeper into the AutoDataAttribute subclass that you create? In the examples here and on a screencast of yours that I watched, you stop at customizing it for AutoMoq. Great examples though, really quick way of getting up to speed with AutoFixture.</div> <div class="comment-date">2013-02-15 21:37 UTC</div> </div> <div class="comment" id="141955eb0f76444586f59bb90557d8e5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#141955eb0f76444586f59bb90557d8e5">#</a></div> <div class="comment-content">Gary, does <a href="/2011/03/18/EncapsulatingAutoFixtureCustomizations.aspx">this</a> help?</div> <div class="comment-date">2013-02-16 08:31 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. String Calculator kata with AutoFixture https://blog.ploeh.dk/2013/02/06/StringCalculatorkatawithAutoFixture 2013-02-06T14:04:43+00:00 Mark Seemann <div id="post"> <p> <em>This post introduces the String Calculator kata done with AutoFixture.</em> </p> <p> A couple of weeks ago, at the Warm Crocodile conference in Copenhagen, <a href="http://osherove.com/">Roy Osherove</a> and I talked about <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> and his <a href="http://osherove.com/tdd-kata-1/">String Calculator kata</a>, and I decided to do the kata with AutoFixture 3 and make a series of screencasts out of it. </p> <p> This series makes no particular attempt at explaining what AutoFixture is, so you might want to first acquaint yourself with some basics, such as the theory of <a href="http://blogs.msdn.com/b/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous Variables</a>, <a href="http://xunitpatterns.com/Derived%20Value.html">Derived Values</a>, <a href="http://xunitpatterns.com/equivalence%20class.html">Equivalence Classes</a>, and <a href="/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained Non-Determinism</a>. It might also be a good idea to understand how <a href="/2010/10/08/AutoDataTheoriesWithAutoFixture.aspx">AutoFixture integrates with xUnit.net</a>. </p> <p> The following screencasts are available: </p> <ol> <li><a href="/2013/02/06/StringCalculatorKataWithAutoFixtureExercise1.aspx">Exercise 1</a></li><li><a href="/2013/02/07/StringCalculatorKataWithAutofixtureExercise2.aspx">Exercise 2</a></li><li><a href="/2013/02/11/StringCalculatorKataWithAutofixtureExercise3.aspx">Exercise 3</a></li><li><a href="/2013/02/12/StringCalculatorKataWithAutofixtureExercise4.aspx">Exercise 4</a></li><li><a href="/2013/02/13/StringCalculatorKataWithAutofixtureExercise5.aspx">Exercise 5</a></li><li><a href="/2013/02/14/StringCalculatorKataWithAutofixtureExercise6.aspx">Exercise 6</a></li><li><a href="/2013/02/15/StringCalculatorKataWithAutofixtureExercise7.aspx">Exercise 7</a></li><li><a href="/2013/02/18/StringCalculatorKataWithAutofixtureExercise8.aspx">Exercise 8</a><br></li> </ol> <p> As a general note, I didn't focus much on refactoring in this exercise, as I didn't feel the complexity of the solution required it.</p><p>If you like these screencasts, you may also like my <a href="https://blog.ploeh.dk/pluralsight-home">Pluralsight</a> course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>.<br> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="bd2e059a50cb4a31a70667c6323ff6ce"> <div class="comment-author"><a href="http://careers.stackoverflow.com/cristi">Cristian Lupascu</a> <a href="#bd2e059a50cb4a31a70667c6323ff6ce">#</a></div> <div class="comment-content"> Thank you for these screencasts and for taking the friction out of Unit Testing! I have one question: how can you run all tests so quickly? I also use TestDriven.Net and have a keyboard shortcut attached to <em>Run Tests</em>. However, this command is contextual and only runs the current method / methods of the current class. I'm not aware of a <em>Run all tests</em> command (or something similar). I see you running all tests in the project even when you're outside the test class. </div> <div class="comment-date">2013-07-31 14:43 UTC</div> </div> <div class="comment" id="de1b1acddfcf44eba12907c32771dad5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#de1b1acddfcf44eba12907c32771dad5">#</a></div> <div class="comment-content"> Hi Cristian, thank you for writing. The way I use <a href="http://testdriven.net">TestDriven.Net</a> is that I have a keyboard shortcut (Shift+Alt+q, simply because that combination was available, and adequately fits my hand) assigned to <em>TestDriven.NET.RunAllTestsInSolution</em>, which Jamie Cansdale courteously added upon my request; I don't exactly remember at which version that command was added, but I'm currently running TestDriven.Net 3.4 Professional build 2808. </div> <div class="comment-date">2013-07-31 18:26 UTC</div> </div> <div class="comment" id="7812fda9e9214c48bcdd6842ba3162ce"> <div class="comment-author"><a href="http://careers.stackoverflow.com/cristi">Cristian Lupascu</a> <a href="#7812fda9e9214c48bcdd6842ba3162ce">#</a></div> <div class="comment-content"> Just installed the latest TestDriven.Net and I also have the <em>RunAllTestsInSolution</em> option now. Thanks! This is very handy. </div> <div class="comment-date">2013-07-31 19:54 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Beware of Productivity Tools https://blog.ploeh.dk/2013/02/04/BewareofProductivityTools 2013-02-04T09:49:12+00:00 Mark Seemann <div id="post"> <p> <em>This article discusses developer productivity tools.</em> </p> <p> Once in a while I get into a heated discussion about the merits and demerits of <a href="http://www.jetbrains.com/resharper/">ReSharper</a>. As these discussions usually happen on Twitter, the 140 character limit isn't conducive to a nuanced debate. That's what I want to start here, not a rant. </p> <p> This is not going to be an attack on ReSharper. In fact, I don't have a stronger opinion on ReSharper than any other 'productivity tool', but I more often get dragged into discussions about ReSharper than, say, <a href="http://www.telerik.com/products/justcode.aspx">JustCode</a> or <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a>. I guess it's because more people feel passionate about ReSharper. </p> <p> In fact, I'm going to expand this discussion to a wider range of 'productivity tools', such as (but not limited to) </p> <ul> <li>ReSharper</li> <li>JustCode</li> <li>CodeRush</li> <li><a href="http://visualstudiogallery.msdn.microsoft.com/3a96a4dc-ba9c-4589-92c5-640e07332afd">Productivity Power Tools</a></li> <li>Visual Studio</li> </ul> <h3 id="0a2b3a7e3ce44149a15aa9c6082d2092"> Why are we even having this discussion? <a href="#0a2b3a7e3ce44149a15aa9c6082d2092" title="permalink">#</a> </h3> <p> The only 'productivity tool' I currently use is Visual Studio 2012, and even that makes me uneasy. That's just my personal preference, you might say, and there's a partial truth in that. However, I'm not writing this to defend myself. Rather, I'm writing because I think you need to be aware of the issues presented here. It might make you a better developer if I can get you to actively and consciously consider a choice you may have taken for granted. </p> <p> How do I even get dragged into these Twitter flame fests? Why do people even care whether or not I use a particular 'productivity tool'? First of all, I can't claim myself innocent of <a href="https://twitter.com/ploeh/status/276687083186319360">occasionally trolling</a> - I just get a kick out of yanking that particular chain. There's a reason for that, and it's not just to be mischievous. I want you to reflect on your choice of tools. Don't just drink the Cool Aid. </p><p> Still, there's a deeper, more rational reason why some people care what I do: I do give a lot of presentations about code, and during those presentations I write a lot of code. Whenever I give a talk where I code live, I always rehearse a lot and use specialized code snippets in order not to bore the audience with trivial coding. <a href="http://ndc2011.macsimum.no/mp4/Day3%20Friday/Track7%201500-1600.mp4">Here's an example</a> where during the talk, someone tweeted to complain that I didn't use ReSharper. However, the purpose of giving a talk about code isn't to produce the code in the fastest possible time. The purpose is to <em>teach</em> code. If I code too slowly, the audience may fall asleep, but if I go too fast, no one is going to learn anything. I'm just not convinced that in this particular case, the use of a 'productivity tool' is inherently better. </p> <h3 id="5ec2227f9b2e481a93b1efd41b880c9d"> Can you even live without this or that productivity tool? <a href="#5ec2227f9b2e481a93b1efd41b880c9d" title="permalink">#</a> </h3> <p> The most common reaction I get whenever people hear that I don't use their favorite 'productivity tool' is one of disbelief. </p> <blockquote> <ul> <li><a href="https://twitter.com/paulroho/status/276801779230973954">omg why? How can do without #resharper?</a></li> <li><a href="https://twitter.com/asbjornu/status/276735101591748608">You're seriously missing out. Visual Studio is completely useless without ReSharper, imho.</a></li> <li><a href="https://twitter.com/jeppec/status/276758517422120960">but how will you work then? Bare bone vs is soo poor compared to just about any ide</a></li> <li><a href="https://twitter.com/sapiensworks/status/276713901314408448">Without #resharper my productivity drops by 50%, I'm amazed that you can manage without it</a></li> <li><a href="https://twitter.com/hmemcpy/status/276678933649453056">I don't understand why you avoid ReSharper.</a></li> <li><a href="https://twitter.com/MelleKoning/status/276788532780077056">hmm.. R# is good for navigating around. Shortcuts to follow application flow, up and down. How do you do that without r#?</a></li> </ul> </blockquote> <p> What's my beef with productivity tools? It's much deeper than a dislike for any particular tool. <a href="http://www.charlespetzold.com/">Charles Petzold</a> already described his concern about Visual Studio in 2005 in a great talk titled <a href="http://www.charlespetzold.com/etc/doesvisualstudiorotthemind.html">Does Visual Studio Rot the Mind?</a>. It's a long read, but definitely worth your while. You should go read it now. </p> <p> In case you didn't want to take the time to read that article (but then: you're already reading <em>this</em> lengthy article), here's the gist of it: Via IntelliSense, code generation, Wizards and drag and drop, Visual Studio assists us, but it also pushes us towards writing (or not writing) code in a particular way. It railroads us. </p> <p> Does it make us more productive? I don't even know how to measure developer productivity, so I can't answer that. Do we <em>learn</em> while coding like that? Not much, I'd say. </p> <p> While Visual Studio is, in many ways, an impressive and extremely useful piece of software, it also concerns me that I'm so dependent on it. To learn new techniques, I try to follow what's going on outside the .NET/Microsoft ecosystem. <a href="http://clojure.org/">Clojure</a> looks like a very interesting language. <a href="http://www.erlang.org/">Erlang</a> seems to solve some hard problems in an easy way. <a href="http://storm-project.net/">Storm</a> seems to be way ahead of anything Microsoft can currently offer. <a href="http://www.ruby-lang.org/en/">Ruby</a> developers have claimed high productivity for years. <a href="http://git-scm.com/">Git</a> is a better source control system than anything Microsoft offers. </p> <p> However, I feel constrained by my reliance on Visual Studio. I want to learn and use those other technologies as well as .NET, so I'm certainly not looking for tools that will further strengthen my bond with Visual Studio. Using plain vanilla Visual Studio is the least I can do to broaden my horizons. </p> <h3 id="366bc0628827489d84a2c65d1881515d"> Productivity boosts <a href="#366bc0628827489d84a2c65d1881515d" title="permalink">#</a> </h3> <p> A common argument for a 'productivity tool' is that it makes you more productive. "<a href="https://twitter.com/sapiensworks/status/276713901314408448">Without #resharper my productivity drops by 50%, I'm amazed that you can manage without it</a>". That's an interesting statement. How do you even measure productivity? </p> <p> For the sake of argument, let's for a moment pretend that programmer productivity is measured by lines of code written. There's this myth going around that a professional programmer <a href="http://stackoverflow.com/q/966800/126014">only writes 10 lines of code per day</a>. This is probably not true, but even so, how many lines of code do you produce <em>on average</em> per day? 100? 200? Are you seriously going to claim that <a href="https://twitter.com/sapiensworks/status/276741827141451778">your productivity bottleneck is determined by how fast you can type?</a> Really? Then learn to type faster. </p> <p> Consider that most code is read a lot more than it's written. Code should be optimized for reading, not writing. Thus, productivity, if it can be measured at all, should be measured by how quickly programmers can read and understand a piece of code - not by how fast it can be written. </p> <p> Furthermore, if you believe that Pair Programming is one good and productive way to produce software, you must also realize that at every given moment, at least one person isn't typing at all. As Martin Fowler <a href="http://martinfowler.com/bliki/PairProgrammingMisconceptions.html">puts it</a>: "that [Pair-Programming halves the productivity of developers] would be true if the hardest part of programming was typing". In my experience, this is not the case. Thus, I'm not convinced that 'productivity tools' make anyone more productive. </p> <p> If you've ever looked beyond the Microsoft echo chamber in the last decade, you will have heard a particular group of developers boast unmatched productivity. Those would be <a href="http://rubyonrails.org/">Ruby on Rails</a> developers. Lately, it seems to me that many alpha geeks gravitate towards JavaScript (and particularly <a href="http://nodejs.org/">Node.js</a>). And what about <a href="http://www.python.org/">Python</a> or Clojure? In all cases it seems that the reason why cutting edge programmers are leaving .NET, in favor of other languages and platforms, is because of better productivity. What do these languages have in common? Well, the preferred development environment certainly isn't Visual Studio. These programmers 'get by' with <a href="http://www.vim.org/">Vim</a>, <a href="http://www.gnu.org/software/emacs/">Emacs</a>, <a href="http://www.sublimetext.com/">Sublime Text</a>, and many other editors. Apparently, it's possible to be 'crazy productive' without Visual Studio and a 'productivity tool'. </p> <h3 id="db8ed66907bd4890a830a7c9455bbaeb"> Railroading <a href="#db8ed66907bd4890a830a7c9455bbaeb" title="permalink">#</a> </h3> <p> As Charles Petzold points out in his excellent article, Visual Studio enforces a certain <em>bottom-up</em> style of programming that isn't particularly aligned with business needs. Visual Studio (with or without 'productivity tools') makes it hard (but not impossible) to do <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In development</a>. </p> <p> My feeling is that whenever a tool helps us in a certain way, it closes a lot of other doors on us. We may not even be aware of what we aren't being shown, but if we can shake off the helping hand, we may also be able to see other options. </p> <p> I don't mind being helped by a tool once in a while, but at other times, I'd rather make an informed decision by myself. At least I think it's important to realize that being helped means that decisions are being made for me. It's <em>not</em> a win-win situation. I may be able to finish a task quickly, but I lose the opportunity to learn. Not only that, but the more I rely on a tool for assistance, the more dependent do I become of it. The're a word for that. It's called <a href="http://en.wikipedia.org/wiki/Vendor_lock-in">Vendor lock-in</a>. </p> <h3 id="ee9138afb7c44a3ab59ffd1df5b76eff"> Final thoughts <a href="#ee9138afb7c44a3ab59ffd1df5b76eff" title="permalink">#</a> </h3> <p> All of this is highly subjective and personal. My personal style is to be very deliberate and patient. I go slowly in order to move fast. </p> <p> In order to demonstrate just how slowly I go, I recorded <a href="http://youtu.be/IfYHrARvTxo">half an hour of a TDD session</a>. There's nothing special about this TDD session. I didn't pick it to impress anyone. I didn't pick the 'best' from a pool of a dozen candidates. I just recorded how I work and uploaded it. I dare you to watch it all the way through. It will be boring. You will see much think time and long periods of inactivity. This is actually a typical depiction of how I work. Yet, somehow, I still manage to produce software in such a quality that people keep coming back to me to pay me to do more. </p> <p> If you watch just five minutes of that video, it should be clear to you that a 'productivity tool' wouldn't be of any help to me. It might not slow me down either, once I'd learned to use it, but it wouldn't make me more 'productive' either, so why should I bother with it? </p> <p> This is just <em>my</em> opinion of 'productivity tools.' It's not a one-size-fits-all judgment. If you feel that you benefit from using your favorite 'productivity tool' I'm not going to tell you to change your ways. Likewise, don't judge me because I don't use a particular tool. Some programmers that I really respect use ReSharper. I respect them not because of, but rather despite of, that. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="07df309643294a47b29ce65226ab312c"> <div class="comment-author">Martin <a href="#07df309643294a47b29ce65226ab312c">#</a></div> <div class="comment-content">In my limited experience, tools like ReSharper helped me very much when i was new to programming.<br> It helped me to see stupid mistakes, like possible Null references etc.<br> As I get more experienced I rely less on ReSharper and mainly use it out of convenience and I think I could do with out it.</div> <div class="comment-date">2013-02-04 11:42 UTC</div> </div> <div class="comment" id="f52b8ace60754816aeb653a65bc2a5ba"> <div class="comment-author"><a href="http://www.skov-boisen.dk">Simon Skov Boisen</a> <a href="#f52b8ace60754816aeb653a65bc2a5ba">#</a></div> <div class="comment-content">I like to say that Visual Studio would be an excellent IDE in if it wasn't so bad to write code in, the editor really isn't nearly as good as vim or emacs and there simply is way too much chrome with all the damn sidebars, bottom-bars, top-bars, popup-windows etc. etc.. That is why I use vim or emacs with evilmode whenever I don't have to write .NET.<br> <br> I don't use Resharper so much for productivity enhancement but rather to ease the cognitive requirement when navigating a big codebase. The fuzzyfind ability to go to type, symbol or file really makes it a lot easier on the brain because you don't have to remember exactly where to find the particular method that you remember part of the name of. <br> <br> Same functionality can of cause be had in vim and emacs but in a much more light-weight approach with small modules doing one thing great. I don't know what it is with Windows and the obsession with big monolithic applications (and tools) compared to unix which is much more about small well-integrated but pluggable modules.</div> <div class="comment-date">2013-02-04 11:49 UTC</div> </div> <div class="comment" id="ba86d6ab630649b49ebbf26d11a40f3b"> <div class="comment-author"><a href="http://whathecode.wordpress.com/">Steven Jeuris</a> <a href="#ba86d6ab630649b49ebbf26d11a40f3b">#</a></div> <div class="comment-content">While I do agree with probably the main part of your concerns, namely &quot;I want you to reflect on your choice of tools. Don't just drink the Cool Aid.&quot;, you argue against productivity tools by using a lot of generalizations.<br> <br> You mention it is hard to measure programmer productivity, yet you argue a lot from a very narrow view of programmer productivity, namely lines of code written.<br> <br> Productivity boosts<br> <br> &quot;Consider that most code is read a lot more than it's written. Code should be optimized for reading, not writing. Thus, productivity, if it can be measured at all, should be measured by how quickly programmers can read and understand a piece of code - not by how fast it can be written.&quot;<br> <br> Productivity tools not only focus on writing lines of code, but also aid exactly in faster navigation of the code, and optimized representations. This is exactly what one of the tweeters mentioned: &quot;hmm.. R# is good for navigating around. Shortcuts to follow application flow, up and down. How do you do that without r#?&quot; These features can also be found in Vim/Emacs. I haven't used these text editors myself, but if you read a bit about it they are simplistic, yet very powerful.<br> <br> Productivity Power Tools offers vertical tabs, allowing to have more tabs open at once, and even color-coding them. ReSharper allows more fine-grained color coding, e.g. extension methods can be given a different color allowing to more easily identify them. All of these enhancements have nothing to do with writing code.<br> <br> Railroading<br> <br> I think it's important to differentiate between different types of railroading here. I am not a big fan of &quot;automated processes&quot; either, e.g. code snippets or wizards. They indeed tend to close doors for you, preventing you from making more informed decisions. In fact I argued before code snippets actually promote bad design: http://whathecode.wordpress.com/2010/11/03/why-code-snippets-promote-bad-design/<br> <br> But what about the hint system of ReSharper? It makes you aware of potential problems in your code, or alternate approaches. That's what makes it even a good learning tool as Martin commented before, or can just remind you of things you didn't immediately think about (e.g. access to modified closures).<br> <br> To summarize, I think in order to have a more honest discussion about productivity tools, you should at least also look into where they can help you, instead of generalizing you don't like them because they have a few particular features you dislike.<br> <br> P.s.: I tried using some markup HTML, but none seems to work. :-(</div> <div class="comment-date">2013-02-04 15:33 UTC</div> </div> <div class="comment" id="9435bdda9e6c433da46f08f6ad5e9048"> <div class="comment-author">Eric <a href="#9435bdda9e6c433da46f08f6ad5e9048">#</a></div> <div class="comment-content">Just a quick comment about developer productivity here, as it relates to your article. I strongly suspect that the &quot;without #resharper my productivity drops by 50% ...&quot; comment has nothing to do with 'lines of code' or 'typing speed', as you imply. Rather, it's a comment on 'how fast can I get my code to look like I want it' (presumably to make the code more readable, understandable, and maintainable). This includes extracting methods, including new methods in interfaces, creating new classes and moving them to new files, referencing and 'using' external libraries, etc. <br> <br> All of these things can be tedious to do 'manually', and can be significantly less tedious with a productivity tool like resharper. As long as what *you* want the code to look like matches what *resharper* wants the code to look like, that is.</div> <div class="comment-date">2013-02-04 16:49 UTC</div> </div> <div class="comment" id="bbc5e3b097de4d3782ac6f90d15e36af"> <div class="comment-author"><a href="http://www.truewill.net/myblog/index.php">Bill Sorensen</a> <a href="#bbc5e3b097de4d3782ac6f90d15e36af">#</a></div> <div class="comment-content">I have a great deal of respect for you; I think on this topic, while you make valid points, your conclusion doesn't follow.<br> <br> When I worked through the book Seven Languages in Seven Weeks, the lack of IntelliSense was painful. I typically had a reference web page open all the time, and had to keep looking away from the code to look up a function. With experience, the time for this is reduced, but it's not eliminated. I remember the same sort of back-and-forth from my xBase days.<br> <br> I've seen you code on Pluralsight (an experience I'd recommend to others). You use the Visual Studio refactoring tools for outside-in development, writing a new method name and having the IDE create the stub. You also use NuGet extensively; I'd call that a major productivity tool.<br> <br> Martin Fowler wrote Refactoring thirteen years ago. I got the impression that he hoped much of it would be automated, and this has come to pass; computers are good at repetitive tasks. My IDE can Extract Method more quickly, accurately, and safely than I can.<br> <br> ReSharper has more and better refactorings than Visual Studio, in my opinion.<br> <br> Navigating complex code bases, including legacy code bases, is an area in which ReSharper shines. Anything that can help me understand the code is a boon.<br> <br> I agree that developers have to learn how to do these things by hand. After that, I don't feel that endlessly repeating the same routine tasks is a good use of my or my employer's time.</div> <div class="comment-date">2013-02-04 18:54 UTC</div> </div> <div class="comment" id="94bbc5e2d8a04b08b02d43e83d689e24"> <div class="comment-author">Nelson LaQuet <a href="#94bbc5e2d8a04b08b02d43e83d689e24">#</a></div> <div class="comment-content">You're comparing some sort of abstraction to R# - and I think that's your biggest mistake here. R# does not abstract or hide code at all - in fact, it's quite the opposite. My favorite features of R# are the keyboard shortcuts and code analysis tools. With R# in hand, I can navigate an entire class hierarchy, jump though methods, into (decompiled) methods and even *easily* navigate stack traces that I copied from an external source (such as ASP.net's yellow screen of death). I can do this almost as quickly as I can think, which removes almost all of the friction of the user interface so that it's just me, and _my_ code.<br> <br> The code generation of R# isn't the same thing as the code generation for, let's say, a WYSIWYG like Dreamweaver. It's not that R# will tell me &quot;I need a class here&quot; or &quot;this should be a field that is filled in by a ctor parameter.&quot; Instead, if _I_ decide I need a new class, or _I_ decide I want to pull up some public members of a class into an interface, I can do those tasks with a simple keystroke. Sometimes the code generation &quot;hints&quot; get a bit annoying - for example, although I am an avid LINQ user and functional programming fan, there are many times where the readability would suffer if I let R# turn a foreach loop into a single two-thousand character expression. But that choice is still up to me - and the times where R# does suggest I use code that ends up being cleaner than what I had wrote, I take advantage of that.<br> <br> So no, R# doesn't hide your code from you, it just removes some of the brain-to-code barrier that is present in all languages. Saying R# is hindering my understanding of code is like saying that you should always use C++ over C# because C# makes you more productive - or it's like suggesting that a digital painter who uses Photoshop and a drawing tablet doesn't still need to understand lighting and materials in the same way that a traditional painter would.<br> <br> R# isn't going to make you a better coder, but it will make you faster and make the experience of writing and navigating code much more pleasant.</div> <div class="comment-date">2013-02-04 23:33 UTC</div> </div> <div class="comment" id="450d574d85b34134a5e4785aceb1ad46"> <div class="comment-author">Giacomo Stelluti Scala <a href="#450d574d85b34134a5e4785aceb1ad46">#</a></div> <div class="comment-content">Hi Mark,<br> I'm agree with you that embracing Visual Studio and R# stimulates a sort of addiction that prevent developers to really deepen non-Microsoft technology (in sense of development, because .NET/C# are from long time standardized and can run also on *nix; but this is off-topic).<br> <br> The point is that tools (or productivity tools in this case) are neutral. They can be used to enforce theoretically correct techniques or to spread the code with &quot;code smells&quot;.<br> <br> So if a developer lacks solid (in all senses, acronym included) basis, he will produce a bad design with any tool he will use.<br> <br> Regards, Giacomo </div> <div class="comment-date">2013-02-05 09:00 UTC</div> </div> <div class="comment" id="44de31ae059f489e8d4a2b1ba65d4aa3"> <div class="comment-author"><a href="http://joseoncode.com">Jos&#233; Romaniello</a> <a href="#44de31ae059f489e8d4a2b1ba65d4aa3">#</a></div> <div class="comment-content">Another point to be added here is that .NET is TIED to Visual Studio, there is no way you can write c# outside Visual Studio and microsoft seems to put a lot of effort into this, for instance nuget worked only inside visual studio at the beginning.<br> <br> Antoher example is that in almost every other language your project match with a folder.. or some friendly configuration file (package.json whatever), but in .Net you have project files, like csproj which is not human readable/editable.. so you are forced to use this IDE. Visual studio is a gigantic IDE where you can do things like integration tests, managing a database and of course write some code.</div> <div class="comment-date">2013-02-05 10:57 UTC</div> </div> <div class="comment" id="880da3e5874e4c03afb78814bd7eef07"> <div class="comment-author">Jonathan Dickinson <a href="#880da3e5874e4c03afb78814bd7eef07">#</a></div> <div class="comment-content">Couldn't agree more. The only extensions that interact with code that I have installed are Productivity Power Tools (with only one or two of its more benign extensions enabled) and GhostDoc.<br> <br> People often give me uphill for that; I get where you are coming from.<br> <br> ReSharper or most other refactoring tools slow my IDE down to the point where its actually catching up with my typing (except CodeRush, it isn't *too* bad). That's a problem. When I hit a key I had better see it on the monitor, immediately. It has an effect from the 3000 LOC projects I work on here to the 50 000 LOC projects I work on here (to a much larger degree, obviously). Ever wondered why Visual Studio only comes with basic refactorings? Likely because the folks on the Windows team use Visual Studio and anything more elaborate would bog them down too much.<br> <br> I work in a VM so just throwing more hardware at the problem won't fix it, and I'll take the productivity boost of VMs over a refactoring tool any day.<br> <br> I guess ReSharper is why a lot of people are complaining about how slow VS2012 is, because for most people when they talk about Visual Studio they actually mean Visual Studio + ReSharper. It's probably why I don't have the same problem as them and personally think it's the fastest Visual Studio released; the ReSharper plugin must just be a little new/unoptimised at the moment.</div> <div class="comment-date">2013-02-06 08:27 UTC</div> </div> <div class="comment" id="e3a5df101cc14663ae425df0ab3d20ef"> <div class="comment-author">NNM <a href="#e3a5df101cc14663ae425df0ab3d20ef">#</a></div> <div class="comment-content">I agree with most of that.<br> And things like JustCode are a waste of time.<br> <br> But intellisense is the most wonderful thing ever! <br> The day this was added, my programming skills increased by a huge step. I could explore and discover all the libraries. A hobby of mine, is now to create empty winforms, add a button, a timer, and just start coding... &quot;Dim foo as System.... Hmm what am I gonna do today...? Let's explore this library..&quot;<br> Just to say, intellisense made me a better and MUCH faster coder. It's perfect for people like me who will never ever open a paper book again, and would rather learn by doing.</div> <div class="comment-date">2013-02-06 10:01 UTC</div> </div> <div class="comment" id="b0a8669c79f447f5b3fce9c418b116df"> <div class="comment-author">pelumi <a href="#b0a8669c79f447f5b3fce9c418b116df">#</a></div> <div class="comment-content">I use Visual Studio when I do .NET and I doubt if you can do anything meaningful in .NET without Visual Studio or some other .NET IDE, just think of it - Nuget, Intellisense, in-built Debugger, perfect handshake with Sql Server, the list is endless. But when i go outside MS domain and play with NodeJS, Django etc, Sublime Text 2 gladdens my heart. I think we should apply the right tool to the right problem and no real programmer should tie his apron to just one technology domain. </div> <div class="comment-date">2013-02-06 11:44 UTC</div> </div> <div class="comment" id="cd2b6d460aab4bc682fc4b6068bfee09"> <div class="comment-author">Gilles <a href="#cd2b6d460aab4bc682fc4b6068bfee09">#</a></div> <div class="comment-content">Regarding:<br> &gt; How do you even measure productivity? <br> <br> There are some standard to measure our productivity: <br> http://it-cisq.org/omg-adopts-automated-function-point-specification/ <br> <br> And some tools are able to automatically compute function point based on this standard.<br> <br> On my side, I'm much more efficient using Resharper, event to read code. <br> Some feature like 'Goto Implementation' really save time which can be used ... to implement something useful :) .<br> <br> Gilles<br> <br> </div> <div class="comment-date">2013-02-06 11:48 UTC</div> </div> <div class="comment" id="0566caa96f8b4874ab897f250951ade6"> <div class="comment-author">JohnB <a href="#0566caa96f8b4874ab897f250951ade6">#</a></div> <div class="comment-content">Someone talked about their dependence upon Intellisense; for me, this is the most aggravating part of VS. The number of times I hovered over a variable in a C++ dll module and nothing happens or it takes seemingly forever for something to appear. I felt like chucking it out of the window.</div> <div class="comment-date">2013-02-06 13:57 UTC</div> </div> <div class="comment" id="38d29ef343504ab498b521ec0244fbcb"> <div class="comment-author">Winston <a href="#38d29ef343504ab498b521ec0244fbcb">#</a></div> <div class="comment-content">I've never attempted to use a productivity tool that wasn't the opposite. I come from a pre-GUI, pre-Visual Studio, pre-Windows, pre-DOS world where programmers had to know where *all* of their files actually were. Because we not only had to edit them, we had to back them up. I programmed for Windows 2.x and 3.x without the &quot;convenience&quot; of a symbolic debugger because when compiled with the debug extensions, the code wouldn't fit in the base memory partition (remember those?). I already knew how to debug without such &quot;convenience&quot;. More than once I found problems my colleagues were stumped on with old fashioned debugging techniques. Including bugs in their symbolic debugger. For the past 10 years I've been more or less forced to use Visual Studio. At least in the C++ environment I know where it hides most of the definitions. My single biggest problem with it is accidentally messing up my screen layout and trying to figure out how it happened and how to get it back. Intellisense works great except when it doesn't. When I can type faster than my near state-of-the-art computer can file what I just typed, someone is wasting an awful lot of cycles, doing what I can't fathom. And no one has mentioned Microsoft's absolutely dreadful resource editor. Oh, the graphic part of it has improved - I used to type my dialog templates directly into the .rc file. More accurate and faster. Now I can edit the look, but its ability to botch resource IDs and duplicate them continues to this day, 24 years after I first cursed the Microsoft resource editor. I am to the point that any time I do much of anything with resources, I manually edit the resource ID file and check it for the duplicates that are sure to exist. Before my current marriage to Visual Studio, I worked in pure C and Unix for 13 years, using a text editor I wrote myself. In spite of its severe limitations, and paying actual money (hundreds of cups of coffee I'll never see again) for Other People's Editors, I stuck with my own. Couldn't stand any of them. Yes, there's something unbeatable about working in your own code base. Come to think of it, the only productivity tools I ever had that improved MY productivity are the ones I wrote myself. The idea that a team of geeks thousands of miles away know how I program, or how I ought to program, is laughable. </div> <div class="comment-date">2013-02-06 14:38 UTC</div> </div> <div class="comment" id="caa93c31215b451799947c69d0f181ad"> <div class="comment-author">Winston <a href="#caa93c31215b451799947c69d0f181ad">#</a></div> <div class="comment-content">BTW, what is Resharper? Never heard of it. Lucky me. </div> <div class="comment-date">2013-02-06 14:39 UTC</div> </div> <div class="comment" id="b8f25d5cc6364c429484c821825740b6"> <div class="comment-author">Shadow <a href="#b8f25d5cc6364c429484c821825740b6">#</a></div> <div class="comment-content">Of course, lets go back to horses, because airplanes made us lazy.</div> <div class="comment-date">2013-02-06 15:42 UTC</div> </div> <div class="comment" id="aa03c0ffad4e4b0793c719f3af16a649"> <div class="comment-author">Carlos L&#243;pez <a href="#aa03c0ffad4e4b0793c719f3af16a649">#</a></div> <div class="comment-content">When i started to work, i never knew about productivity tools. I always was dependant on the intellisense of the enviroment i was working on.<br> <br> Last year, I was hired on a private software developer company, which used R#, and since then, i have been loving it. But i dont let it be what defines my productivity. I learned a lot from the code snippets and suggestions it makes, and nowadays, i hardly recieve a suggestion from it, i had learnt mostly all of its trick to improve the readability of the code. To this days, i use it mostly to fill in code that we had designed on code snippets from our standards, but we can work withouth it just right<br> <br> Another productivity tool that i use along side R# is power tools, mostly for it search and localization of variables along the code. Selecting a variable and pressing ctrl+shit+up/down to navigate through the code is pretty awesome!</div> <div class="comment-date">2013-02-06 16:12 UTC</div> </div> <div class="comment" id="3ed1e069301f49a5800e04ce0b5a28e7"> <div class="comment-author">David Priebe <a href="#3ed1e069301f49a5800e04ce0b5a28e7">#</a></div> <div class="comment-content">In general I do agree that productivity tools can be a bit over blown. I've used text editors and IDEs and Visual Studio etc. The biggest thing I get from ReSharper and other tools on a daily basis is the elimination of repetitive tasks. Creating a property is easy. Type prop-tab and the code structure appears. Then I just have to dive in and do any customization I need. Do I want to convert the property to one backed by a variable? That's just a few key strokes away. Could I do it by typing in almost the same amount of time? Sure. I've never felt that I couldn't code without productivity tools but they do make some tasks less tedious.<br> <br> As far as learning about the language I'm using, I have to disagree with you there. I often use Intellisense to find available methods to call on a given object and the documentation on those methods gets displayed so it's convenient for me if I'm working with a framework or portion of .Net I'm unfamiliar with. I also found ReSharper's ability to refactor loops into Linq code to be very educational when I was starting out with Linq. It would condense loops to Linq when I couldn't see how to express it and would sometimes highlight methods that I did not realize existed, helping me understand what Linq could do and how to think about using it.<br> <br> In the end the basic truth is that your mileage will vary. What you get out of a tool will depend on how you approach it's use. Productivity tools can help explore existing code and how it works and it can help you learn how to use unfamiliar frameworks but you have to be thinking about what the tool is doing.</div> <div class="comment-date">2013-02-06 16:56 UTC</div> </div> <div class="comment" id="dd1b677362274883a7c6a2e42306dcc7"> <div class="comment-author">Kyle <a href="#dd1b677362274883a7c6a2e42306dcc7">#</a></div> <div class="comment-content">My take of the subject is... If you can't code properly without them then you shouldn't be using them. You should understand and be able to write what ReSharper is writing for you. <br> <br> IMO, the most important &quot;ility&quot; is READABILITY. These productivity tools write the most unreadability code I have ever seen. Your code should be written in a way that a Jr. Developer that knows a different language can read your code (without comments) at 2am half sleep.</div> <div class="comment-date">2013-02-06 17:23 UTC</div> </div> <div class="comment" id="3fb920852e564b6fba72269622d1f6a1"> <div class="comment-author">Davyd McColl <a href="#3fb920852e564b6fba72269622d1f6a1">#</a></div> <div class="comment-content">Having only worked the last 8 months (of over 12 years of dev) at a company where ReSharper is basically embedded in the culture, I can say this: I've seen far too many occasions of forced usage of the tool when a person could have just edited the text quicker. Personally, I use ViEmu too, because I'm very used to Vi-style editing and navigation. There's been many times when I could Vi myself to the place I want to be MUCH faster than a colleague with ReSharper.<br> <br> All of that being said, now that I have it, there are some conveniences, mainly the renaming stuff (yes, I know VS has that, but I'd really like a dialog that allowed the rename to happen instead of trying to find the fiddly little drop-down; I'm not a fan of ReSharper's inline renamer, largely because it doesn't play well with ViEmu, but I digress), &quot;find all usages&quot; (which is a little more honed than global find, but I still use the latter quite a bit) and &quot;Refactor method&quot; which, I have to admit, /when it gets it right/, can do the job faster than I can. The only problem with that is that I've found it to only be about 80% correct, producing really wonky refactors at other times -- but I can quickly undo and hand-craft as required. A colleague of mine tried to force me to rather re-write parts of code to &quot;help&quot; the refactor to take place -- I'm sure I don't have to point out how little sense that makes in the face of just refactoring by hand.<br> <br> And I do use the ReSharper test runner, because Ctrl-U,Ctrl-L is the quickest way I've found to re-run all tests before a checkin. Ctrl-U,Ctrl-R is also well handy for running the current test or fixture (scope dependent), and Ctrl-U,Ctrl-D is a quick way to step into a test with a debugger. I also prefer the ReSharper test runner UI over the NUnit one -- but that's a minor thing, really.<br> <br> At the end of the day though, I can quite easily live without these things. I don't think any of these tools are *perfect*, but what is? The parts of ReSharper which work for me shave seconds off of my work day here and there -- and that all adds up, so I appreciate them where then help. But I like to stick to the idea of a tool for a task: if it enhances my work without detracting from it, then it's useful. And if it doesn't work for you, don't use it. I could more easily part with ReSharper than ViEmu, to be totally honest.</div> <div class="comment-date">2013-02-06 18:46 UTC</div> </div> <div class="comment" id="6b3d8ac37fbe42ae976628a1e021c407"> <div class="comment-author">Mike <a href="#6b3d8ac37fbe42ae976628a1e021c407">#</a></div> <div class="comment-content">I've been programming since I got my first HP calculator back in 1980. Assembly code, compilers, Opus make scripts, line editors (even on a printer terminal!!). Back in the 90's, I used Multi-Edit for writing Clipper apps--what a fantastic macro language it had!! Now I'm coding mostly in C# with Visual Studio. I LOVE Intellisense--particularly for showing all the descriptions of the methods and parameters that I write with the /// &quot;self&quot;-documenter.<br> <br> I tried Resharper--for about one week. The myriad of options regarding what to flag as &quot;wrong&quot; coding style made my head spin. So I figured I'd use the defaults--BIG mistake. It seemed everything I was writing was wrong. I spent more time trying to &quot;fix&quot; (and research) my Resharper-flagged &quot;mistakes&quot; than actually writing real code. I happily uninstalled it.</div> <div class="comment-date">2013-02-06 18:50 UTC</div> </div> <div class="comment" id="145afd2384bf479fa5db70ac34d8b4c5"> <div class="comment-author"><a href="http://thinkskeptic.tumblr.com">Sebastian</a> <a href="#145afd2384bf479fa5db70ac34d8b4c5">#</a></div> <div class="comment-content">I've been programming for more then 19 years now, in the begining I used the ms-dos text editor, then notepad, dreamweaver, etc...<br> <br> It doesn't matter what you use, the problem comes when the editor/IDE does work you dont know about and dont understand well, then you are dependent since it does something you couldnt do without it, I like visual studio very much, especially the debugger and intellisense, but I dont need it to produce quality code, it just helps do things faster. or helps me learn new libraries without going through the reference.<br> <br> Ruby and Javascript are different since they are dynamic languages so the need for static typing and reference tracking isnt there as much. but those languages on the other hand are tougher to debug and I have seen a debugger for those languages as good as the visual studio one for .NET<br> <br> Sebastian</div> <div class="comment-date">2013-02-06 23:25 UTC</div> </div> <div class="comment" id="af73f67e12134092a5852bb16e8150ec"> <div class="comment-author">Vadim <a href="#af73f67e12134092a5852bb16e8150ec">#</a></div> <div class="comment-content">+1<br> I totally agree with the author. I've tried ReSharper and couple of other &quot;productivity tools&quot;, and found that they just distract me.<br> Software Engineer is paid for solving problems, not for writing code.</div> <div class="comment-date">2013-02-07 01:45 UTC</div> </div> <div class="comment" id="6d6242fb65234c87b536f81e1890c969"> <div class="comment-author">Gregory <a href="#6d6242fb65234c87b536f81e1890c969">#</a></div> <div class="comment-content">I recently came across your blog and this is the first time I comment. Really good job. Now about the post...<br> <br> My motto is: if you can deliver, I don't care how you did that. If I want you move my staff somewhere in a specified amount of time, I don't care whether you use a truck, a ferrari, or you did the job on foot. It is really whatever suit to any of us! You are not getting better by hand-made refactoring all the time, rather having a tool do that for you.<br> <br> This argument remind me back then, when linux had no decent (if at all) user interface but some people used that just to feel &quot;wizards&quot; :) Writing code is not a static thing. Tools, languages etc evolve. We have to evolve as well. Some will say &quot;I don't use Linq. Looks cheesy and I am an old schooler&quot;, &quot;I don't use .NET. I am a C++ guy that is used to have control of my resources&quot;. As we evolve, arguments evolve. Now, even me am using .net (yes was that guy that wanted to write compact c/c++ code with full control) and here we are talking about... productivity tools! :)<br> <br> I do not use any of these tools (apart from VS of course) not because I am a purist. I just haven't ever looked for it. Now that I saw what R# can do, I am really tempted! </div> <div class="comment-date">2013-02-23 21:56 UTC</div> </div> <div class="comment" id="cc6383c2b63140a09088f5ac67582fc1"> <div class="comment-author">Harshdeep Mehta <a href="#cc6383c2b63140a09088f5ac67582fc1">#</a></div> <div class="comment-content">Sorry to comment on old posst. In parts I do agree with you, but I dont agree with the point that Productivity Tools stops you from learning. <br> In fact, it does help you learn if you observ what change it has made to your code. I believe, after few observations you will start writing code in better manner so that you dont leverage such tools for same reason anymore. <br> I would say, such Productivity Tools help you make good code for you intentions, but it will not help you improve your intentions in most case. For example, if you are writing Foreach loop to get some value then it will suggest you to get it converted in LinQ but it will not help you to implement Parallel.Foreach which might improve performance in some cases by introducing complexity. So such tool helps you enhance code for your intentions but will not enhnace your intentions for better code. </div> <div class="comment-date">2017-07-07 08:35 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Outside-In Test-Driven Development Pluralsight course https://blog.ploeh.dk/2013/01/16/Outside-InTest-DrivenDevelopmentPluralsightcourse 2013-01-16T22:26:15+00:00 Mark Seemann <div id="post"> <p> In case you missed <a href="https://twitter.com/ploeh/status/291668143644999680">the Tweet</a>, <a href="https://plus.google.com/100509078856689101462/posts/4xgfFoVe1fc">the Google+ share</a> or <a href="http://blog.pluralsight.com/2013/01/16/new-course-outside-in-test-driven-development/">the announcement</a>, my first course for <a href="https://blog.ploeh.dk/pluralsight-home">Pluralsight</a>, called <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>, is now available. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e74abe3afc9945f592855361160b105e"> <div class="comment-author">DavidS <a href="#e74abe3afc9945f592855361160b105e">#</a></div> <div class="comment-content">Hey Mark,<br> <br> It is a very good course although some of the latter modules were a bit too quick for me. It must be due to my lack of experience. <br> <br> On another separate note, I know that you are going to be giving a course on Dependency injection in London with SkillsMatters. Will it be covering the material in Outside in TDD? Would it be possible for you to provide more details that what's on the site?<br> <br> TIA,<br> <br> David</div> <div class="comment-date">2013-02-03 17:47 UTC</div> </div> <div class="comment" id="ef4c8c88104b4c47812209c438bd5e51"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ef4c8c88104b4c47812209c438bd5e51">#</a></div> <div class="comment-content">Currently I don't have more information about the Skills Matter course than what's on their web site, but as it's a course about Dependency Injection, it's not going to cover TDD in particular. That's a different subject.</div> <div class="comment-date">2013-02-04 15:27 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Partial Type Name Role Hint https://blog.ploeh.dk/2013/01/11/PartialTypeNameRoleHint 2013-01-11T11:07:55+00:00 Mark Seemann <div id="post"> <p> <em>This article describes how object roles can be indicated by parts of a type name.</em> </p> <p> In my <a href="/2013/01/07/RoleHints.aspx">overview article on Role Hints</a> I described how making object roles explicit can help making code more object-oriented. One way code can convey information about the role played by an object is to let a part of a class' name convey that information. This is often very useful when using Convention over Configuration. </p> <p> While a class can have an elaborate and concise name, a part of that name can communicate a particular role. If the name is complex enough, it can hint at multiple roles. </p> <h3 id="9a6a38ae2dd94a58a9e2a25327281f47"> Example: Selecting a shipping Strategy <a href="#9a6a38ae2dd94a58a9e2a25327281f47" title="permalink">#</a> </h3> <p> As an example, consider the shipping Strategy selection problem from <a href="/2013/01/09/MetadataRoleHint.aspx">a previous post</a>. Does attribute-based metadata like this really add any value? </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">HandlesShippingMethod</span>(<span style="color: #2b91af;">ShippingMethod</span>.Express)] <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ExpressShippingCostCalculator</span> : <span style="color: #2b91af;">IBasketCalculator</span></pre> </p> <p> Notice how the term <em>Express</em> appears twice on two lines of code. You could successfully argue that the <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY principle</a> is being violated here. This becomes even more apparent when considering the <em>detached metadata</em> example. Here's a static way to populate the map (in F# just because I can): </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> map = Dictionary&lt;ShippingMethod, IBasketCalculator&gt;() map.Add(ShippingMethod.Standard, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;StandardShippingCostCalculator()) map.Add(ShippingMethod.Express, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ExpressShippingCostCalculator()) map.Add(ShippingMethod.PriceSaver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;PriceSaverShippingCostCalculator())</pre> </p> <p> This code snippet uses some slightly unorthodox (but still valid) formatting to highlight the problem. Instead of a single statement per shipping method, you could just as well write an algorithm that populates this map based on the first part of each calculator's name. Follow that train of thought to its logical conclusion, and you don't even need the map: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShippingCostCalculatorFactory</span> : <span style="color: #2b91af;">IShippingCostCalculatorFactory</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IBasketCalculator</span>&gt; candidates; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ShippingCostCalculatorFactory( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IBasketCalculator</span>&gt; candidates) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.candidates = candidates; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IBasketCalculator</span> GetCalculator(<span style="color: #2b91af;">ShippingMethod</span> shippingMethod) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> (<span style="color: blue;">from</span> c <span style="color: blue;">in</span> <span style="color: blue;">this</span>.candidates &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> t = c.GetType() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">where</span> t.Name.StartsWith(shippingMethod.ToString()) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">select</span> c).First(); &nbsp;&nbsp;&nbsp; } }</pre></p> <p> This implementation uses the start of the type name of each candidate as a Role Hint. The ExpressShippingCostCalculator already effectively indicates that it calculates shipping cost for the <em>Express</em> shipping method. </p> <p> This is an example of Convention over Configuration. Follow a simple naming convention, and things just work. This isn't an irreversible decision. If, in the future, you discover that you need a more elaborate selection algorithm, you can always modify the ShippingCostCalculatorFactory class (or, if you wish to adhere to the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>, add an alternative implementation of IShippingCostCalculatorFactory). </p> <h3 id="d00ea8554d5b4f07b1fd7e98dd76499b"> Example: ASP.NET MVC Controllers <a href="#d00ea8554d5b4f07b1fd7e98dd76499b" title="permalink">#</a> </h3> <p> The default routing algorithm in <a href="http://www.asp.net/mvc">ASP.NET MVC</a> works this way. An incoming request to /basket/1234 is handled by a BasketController instance, /product/3457 by a ProductController instance, and so on. </p> <p> The <a href="http://www.asp.net/web-api">ASP.NET Web API</a> works the same way, too. </p> <h3 id="1ce3ce12d6544cc5a8012bf0dcd4634f"> Summary <a href="#1ce3ce12d6544cc5a8012bf0dcd4634f" title="permalink">#</a> </h3> <p> Using a part of a type name as a Role Hint is very common when using Convention over Configuration. Many developers react strongly against this approach because they feel that the loss of type safety and the use of Reflection makes this a bit 'too magical' for their tastes. However, even when using attributes, you can easily forget to add an attribute to a class, so in the end you must rely on testing to be sure that everything works. The type safety of attributes is often an illusion. </p> <p> The great benefit of Convention over Configuration is that it significantly cuts down on the number of moving parts. It also 'forces' you (and your team) to write more consistent code, because the overall application is simply not going to work if you don't follow the conventions. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="7034012726344c4fa770e07b92ff4fc8"> <div class="comment-author"><a href="http://comfycoding.blogspot.com/">Illya Ivanov</a> <a href="#7034012726344c4fa770e07b92ff4fc8">#</a></div> <div class="comment-content">It's interesting how conventions can popup from places you wouldn't even expect them to appear. <br> I've used MVP pattern in Unity3D game development, where I had like MosterView and MonsterPresenter autowired by assembly scanning. As result I have IPresenter as input into View and IoC container that discover and inject correct Presenter implementation into View. I also wrote additional test, where I assert if every view has corresponding presenter, such that I would discover convention violation not at run-time, but at tests running step. Just reduces feedback time a little bit. <br> This idea came after watching your &quot;Conventions: Make your code consistent&quot; presentation. Thanks. </div> <div class="comment-date">2013-01-23 15:42 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Role Interface Role Hint https://blog.ploeh.dk/2013/01/10/RoleInterfaceRoleHint 2013-01-10T10:37:35+00:00 Mark Seemann <div id="post"> <p> <em>This article describes how object roles can be indicated by the use of Role Interfaces.</em> </p> <p> In my <a href="/2013/01/07/RoleHints.aspx">overview article on Role Hints</a> I described how making object roles explicit can help making code more object-oriented. One way code can convey information about the role played by an object is by implementing one or more <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a>. As the name implies, a Role Interface describes a role an object can play. Classes can implement more than one Role Interface. </p> <h3 id="5b4aa1d4a8da43a793129b3f773ba0b8"> Example: Selecting a shipping Strategy <a href="#5b4aa1d4a8da43a793129b3f773ba0b8" title="permalink">#</a> </h3> <p> As an example, consider the shipping Strategy selection problem from <a href="/2013/01/09/MetadataRoleHint.aspx">the previous post</a>. That example seemed to suffer from the <a href="http://c2.com/cgi/wiki?FeatureEnvySmell">Feature Envy smell</a> because the attribute had to expose the handled shipping method as a property in order to enable the selection mechanism to pick the right Strategy. </p> <p> Another alternative is to define a Role Interface for matching objects to shipping methods: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IHandleShippingMethod</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">bool</span> CanHandle(<span style="color: #2b91af;">ShippingMethod</span> shippingMethod); }</pre> </p> <p> A shipping cost calculator can implement the IHandleShippingMethod interface to participate in the selection process: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ExpressShippingCostCalculator</span> : &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IBasketCalculator</span>, <span style="color: #2b91af;">IHandleShippingMethod</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> CalculatePrice(<span style="color: #2b91af;">ShoppingBasket</span> basket) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">/* Perform some complicated price calculation based on the</span> <span style="color: green;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; * basket argument here. */</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> 1337; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> CanHandle(<span style="color: #2b91af;">ShippingMethod</span> shippingMethod) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> shippingMethod == <span style="color: #2b91af;">ShippingMethod</span>.Express; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> An ExpressShippingCostCalculator object can play one of two roles: It can calculate basket prices and it can handle basket calculations related to shipping methods. It doesn't have to expose as a property the shipping method it handles, which enables some more sophisticated scenarios like handling more than one shipping method, or handling a certain shipping method only if some other set of conditions are also met. </p> <p> You can implement the selection algorithm like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShippingCostCalculatorFactory</span> : <span style="color: #2b91af;">IShippingCostCalculatorFactory</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IBasketCalculator</span>&gt; candidates; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ShippingCostCalculatorFactory( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IBasketCalculator</span>&gt; candidates) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.candidates = candidates; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IBasketCalculator</span> GetCalculator(<span style="color: #2b91af;">ShippingMethod</span> shippingMethod) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> (<span style="color: blue;">from</span> c <span style="color: blue;">in</span> <span style="color: blue;">this</span>.candidates &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> handles = c <span style="color: blue;">as</span> <span style="color: #2b91af;">IHandleShippingMethod</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">where</span> handles != <span style="color: blue;">null</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; handles.CanHandle(shippingMethod) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">select</span> c).First(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Notice that because the implementation of CanHandle can be more sophisticated and conditional on the context, more than one of the candidates may be able to handle a given shipping method. This means that the order of the candidates matters. Instead of selecting a <a href="http://msdn.microsoft.com/en-us/library/bb155325.aspx">Single</a> item from the candidates, the implementation now selects the <a href="http://msdn.microsoft.com/en-us/library/bb291976.aspx">First</a>. This provides a fall-through mechanism where a preferred, but specialized candidate is asked before less preferred, general-purpose candidates. </p> <p> This particular definition of the IHandleShippingMethod interface suffers from the same tight coupling to the ShippingMethod enum as the <a href="/2013/01/09/MetadataRoleHint.aspx">previous example</a>. One fix may be to define the shipping method as a string, but you could still successfully argue that even implementing an interface such as IHandleShippingMethod in a Domain Model object mixes architectural concerns. Detached metadata might still be a better option. </p> <h3 id="db85e9ea5e59496fb509e6925947a0dc"> Summary <a href="#db85e9ea5e59496fb509e6925947a0dc" title="permalink">#</a> </h3> <p> As the name implies, a Role Interface can be used as a Role Hint. However, you must be wary of pulling in disconnected architectural concerns. Thus, while a class can implement several Role Interfaces, it should only implement interfaces defined in appropriate layers. (The word 'layer' here is used in a loose sense, but the same considerations apply for <a href="http://c2.com/cgi/wiki?PortsAndAdaptersArchitecture">Ports and Adapters</a>: don't mix Port types and Adapter types.) </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="82064da2bec44f3989a98208bcd4785f"> <div class="comment-author"><a href="http://blogs.msdn.com">Jeff Soper</a> <a href="#82064da2bec44f3989a98208bcd4785f">#</a></div> <div class="comment-content"> <p> Could you expand upon how you might implement the fall-through mechanism you mentioned in your IShippingCostCalculatorFactory implementation, where more than one candidate can handle the shippingMethod? </p> <p> How would you sort your IEnumerable&lt;IBasketCalculator&gt; candidates in GetCalculator() so that the candidate returned by First() is the one specifically meant to handle the ShippingMethod when one exists, and the default implementation when a specific one doesn't exist? </p> <p> I considered using FirstOrDefault(), then returning the default implementation if the result of the query was nothing, but my default IHandleShippingMethod implementation always returns True from CanHandle() - I don't know what other value it could return. </p> </div> <div class="comment-date">2013-08-14 15:37 UTC</div> </div> <div class="comment" id="fa4e59570dce4db68cb832b9bb94eac6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fa4e59570dce4db68cb832b9bb94eac6">#</a></div> <div class="comment-content"> <p> You could have super-specialized IBasketCalculator implementations, that (e.g.) are only active certain times of day, and you could have the ones I've shown here, and then you could have a fallback that implements IHandleShippingMethod.CanHandle by simply returning true no matter what the input is. If you put this fallback implementations <em>last</em> in the injected candidates, it's only going to picked up by the First method if no other candidate (before it) returns true from CanHandle. </p> <p> Thus, there no reason to sort the candidates within GetCalculator - in fact, it would be an error to do so. As I wrote above, "the order of the candidates matters." </p> </div> <div class="comment-date">2013-08-14 17:39 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Metadata Role Hint https://blog.ploeh.dk/2013/01/09/MetadataRoleHint 2013-01-09T10:42:20+00:00 Mark Seemann <div id="post"> <p> <em>This article describes how object roles can be indicated by metadata.</em> </p> <p> In my <a href="/2013/01/07/RoleHints.aspx">overview article on Role Hints</a> I described how making object roles explicit can help making code more object-oriented. One way code can convey information about the role played by an object is by leveraging metadata. In .NET that would often take the form of attributes, but you can also maintain the metadata in a separate data structure, such as a dictionary. </p> <p> Metadata can provide useful Role Hints when there are many potential objects to choose from. </p> <h3 id="1591e88e74914e83befe9e0b477c8c64"> Example: Selecting a shipping Strategy <a href="#1591e88e74914e83befe9e0b477c8c64" title="permalink">#</a> </h3> <p> Consider a web shop. When you take your shopping basket to checkout you are presented with a choice of shipping methods, e.g. <em>Standard</em>, <em>Express</em>, and <em>Price Saver</em>. Since this is a web application, the user's choice must be communicated to the application code as some sort of primitive type. In this example, assume an enum: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">enum</span> <span style="color: #2b91af;">ShippingMethod</span> { &nbsp;&nbsp;&nbsp; Standard = 0, &nbsp;&nbsp;&nbsp; Express, &nbsp;&nbsp;&nbsp; PriceSaver }</pre> </p> <p> Calculating the shipping cost for a basket may be a complex operation that involves the total weight and size of the basket, as well as the distance it has to travel. If the web shop has geographically distributed warehouses, it may be cheaper to ship from a warehouse closer to the customer. However, if the closest warehouse doesn't have all items in stock, there may be a better way to optimize profit. Again, that calculation likely depends on the shipping method chosen by the customer. Thus, a common solution is to select a shipping cost calculation <a href="http://en.wikipedia.org/wiki/Strategy_pattern">Strategy</a> based on the user's selection. A simplified example may look like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BasketCostCalculator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IShippingCostCalculatorFactory</span> shippingFactory; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> BasketCostCalculator( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IShippingCostCalculatorFactory</span> shippingCostCalculatorFactory) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.shippingFactory = shippingCostCalculatorFactory; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> CalculatePrice( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ShoppingBasket</span> basket, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ShippingMethod</span> shippingMethod) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> shippingCalculator = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.shippingFactory.GetCalculator(shippingMethod); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> shippingCalculator.CalculatePrice(basket) + basket.Total; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> A naïve attempt at an implementation of the IShippingCostCalculatorFactory may involve a switch statement: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">IBasketCalculator</span> GetCalculator(<span style="color: #2b91af;">ShippingMethod</span> shippingMethod) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">switch</span> (shippingMethod) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> <span style="color: #2b91af;">ShippingMethod</span>.Express: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ExpressShippingCostCalculator</span>(); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> <span style="color: #2b91af;">ShippingMethod</span>.PriceSaver: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">PriceSaverShippingCostCalculator</span>(); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> <span style="color: #2b91af;">ShippingMethod</span>.Standard: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">default</span>: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">StandardShippingCostCalculator</span>(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Now, before you pull <a href="http://www.amazon.com/gp/product/0201485672/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0201485672">Refactoring</a> at me and tell me to replace the enum with a polymorphic type, I must remind you that <a href="/2011/05/31/AtTheBoundariesApplicationsAreNotObjectOriented.aspx">at the boundaries, applications aren't object-oriented</a>. At some place, close to the application boundary, the application must translate an incoming primitive to a polymorphic type. That's the responsibility of something like the ShippingCostCalculatorFactory. </p> <p> There are several problems with the above implementation of IShippingCostCalculatorFactory. In the simplified example code, all three implementations of IBasketCalculator have default constructors, but that's not likely to be the case. Recall that calculating shipping cost involves complicated business rules. Not only are those classes likely to need all sorts of configuration data to determine price per weight range, etc. but they might even need to perform lookups against external system - such as getting a qoute from an external carrier. In other words, the ExpressShippingCostCalculator, PriceSaverShippingCostCalculator, and StandardShippingCostCalculator are unlikely to have default constructors. </p> <p> There are <a href="/2012/03/15/ImplementingAnAbstractFactory.aspx">various ways to implement such an Abstract Factory</a>, but none of them may fit perfectly. Another option is to associate metadata with each implementation. Using attributes for such purpose is the classic .NET solution: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">HandlesShippingMethodAttribute</span> : <span style="color: #2b91af;">Attribute</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">ShippingMethod</span> shippingMethod; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> HandlesShippingMethodAttribute(<span style="color: #2b91af;">ShippingMethod</span> shippingMethod) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.shippingMethod = shippingMethod; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ShippingMethod</span> ShippingMethod &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">this</span>.shippingMethod; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> You can now adorn each IBasketCalculator implementation with this attribute: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">HandlesShippingMethod</span>(<span style="color: #2b91af;">ShippingMethod</span>.Express)] <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ExpressShippingCostCalculator</span> : <span style="color: #2b91af;">IBasketCalculator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> CalculatePrice(<span style="color: #2b91af;">ShoppingBasket</span> basket) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">/* Perform some complicated price calculation based on the</span> <span style="color: green;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; * basket argument here. */</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> 1337; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Obviously, the other IBasketCalculator implementations get a similar attribute, only with a different ShippingMethod value. This effectively provides a hint about the role of each IBasketCalculator implementation. Not only is the ExpressShippingCostCalculator a <em>basket calculator</em>; it specifically handles the <em>Express</em> shipping method. </p> <p> You can now implement IShippingCostCalculatorFactory using these Role Hints: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShippingCostCalculatorFactory</span> : <span style="color: #2b91af;">IShippingCostCalculatorFactory</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IBasketCalculator</span>&gt; candidates; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ShippingCostCalculatorFactory( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IBasketCalculator</span>&gt; candidates) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.candidates = candidates; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IBasketCalculator</span> GetCalculator(<span style="color: #2b91af;">ShippingMethod</span> shippingMethod) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> (<span style="color: blue;">from</span> c <span style="color: blue;">in</span> <span style="color: blue;">this</span>.candidates &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> handledMethod = c &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .GetType() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .GetCustomAttributes&lt;<span style="color: #2b91af;">HandlesShippingMethodAttribute</span>&gt;() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .SingleOrDefault() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">where</span> handledMethod != <span style="color: blue;">null</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; handledMethod.ShippingMethod == shippingMethod &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">select</span> c).Single(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This implementation is created with a sequence of IBasketCalculator candidates and then selects the matching candidate upon each GetCalculator method call. (Notice that I decided that <em>candidates</em> was a better Role Hint than e.g. <em>calculators</em>.) To find a match, the method looks through the candidates and examines each candidate's [HandlesShippingMethod] attribute. </p> <p> You may think that this is horribly inefficient because it virtually guarantees that the majority of the injected candidates are never going to be used in this method call, but <a href="/2011/03/04/ComposeObjectGraphsWithConfidence.aspx">that's not a concern</a>. </p> <h3 id="b96f78e067de4e8abbdc982270baa366"> Example: detached metadata <a href="#b96f78e067de4e8abbdc982270baa366" title="permalink">#</a> </h3> <p> The use of attributes has been a long-standing design principle in .NET, but seems to me to offer a poor combination of tight coupling and <a href="/2011/05/25/DesignSmellPrimitiveObsession.aspx">Primitive Obsession</a>. To be fair, it looks like even in the BCL, the more modern APIs are less based on attributes, and more based on other alternatives. In other posts in this series on Role Hints I'll describe other ways to match objects, but even when working with metadata there are other alternatives. </p> <p> One alternative is to decouple the metadata from the type itself. There's no particular reason the metadata should be compiled into each type (and sometimes, if you don't own the type in question, you may not be able to do this). Instead, you can define the metadata in a simple map. Remember, 'metadata' simply means 'data about data'. </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShippingCostCalculatorFactory</span> : <span style="color: #2b91af;">IShippingCostCalculatorFactory</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IDictionary</span>&lt;<span style="color: #2b91af;">ShippingMethod</span>, <span style="color: #2b91af;">IBasketCalculator</span>&gt; map; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ShippingCostCalculatorFactory( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IDictionary</span>&lt;<span style="color: #2b91af;">ShippingMethod</span>, <span style="color: #2b91af;">IBasketCalculator</span>&gt; map) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.map = map; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IBasketCalculator</span> GetCalculator(<span style="color: #2b91af;">ShippingMethod</span> shippingMethod) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.map[shippingMethod]; &nbsp;&nbsp;&nbsp; } }</pre> <p> <p> That's a much simpler implementation than before, and only requires that you supply the appropriately populated dictionary in the application's <a href="/2011/07/28/CompositionRoot.aspx">Composition Root</a>. </p> <h3 id="77a4aeb4319543ec829edf68c4c0e4d2"> Summary <a href="#77a4aeb4319543ec829edf68c4c0e4d2" title="permalink">#</a> </h3> <p> Metadata, such as attributes, can be used to indicate the role played by an object. This is particularly useful when you have to select among several candidates that all have the same type. However, while the <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229042.aspx">Design Guidelines for Developing Class Libraries</a> still seems to favor the use of attributes for metadata, this isn't the most modern approach. One of the problems is that it is likely to tightly couple the resulting code. In the above example, the ShippingMethod enum is a pure boundary concern. It may be defined in the UI layer (or module) of the application, while you might prefer the shipping cost calculators to be implemented in the Domain Model (since they contain complicated business logic). However, using the ShippingMethod enum in an attribute and placing that attribute on each implementation couples the Domain Model to the user interface. </p> <p> One remedy for the problem of tight coupling is to express the selected shipping method as a more general type, such as a string or integer. Another is to use detached metadata, as in the second example. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5d24b12cec0e476a8b9aab3173f0f827"> <div class="comment-author">Phil Sandler <a href="#5d24b12cec0e476a8b9aab3173f0f827">#</a></div> <div class="comment-content">I know this post is about roles, but I think your example illustrates a question that I ask (and get asked) constantly about implementing factories using DI. I have gone around and around this issue and tried various approaches, including the map solution you provided. The attribute idea is new to me, and I will give it some consideration.<br> <br> Bottom line: I've landed on just using the container directly inside of these factories. I am completely on board with the idea of a single call to Resolve() on the container, but this is one case where I have decided it's &quot;easier&quot; to consciously violate that principle. I feel like it's a bit more explicit--developers at least know to look in the registration code to figure out which implementation will be returned. Using a map just creates one more layer that essentially accomplishes the same thing.<br> <br> A switch statement is even better and more explicit (as an aside, I don't know why the switch statement gets such a bad rap). But as you noted, a switch statement won't work if the calculator itself has dependencies that need resolution. In the past, I have actually injected the needed dependencies into the factory, and supplied them to the implementations via new().<br> <br> Anyway, I'm enjoying this series and getting some nice insights from it.<br> </div> <div class="comment-date">2013-01-09 15:33 UTC</div> </div> <div class="comment" id="8b7667d6bbbd4c4cb8404bd6e4e364ff"> <div class="comment-author">James Nail <a href="#8b7667d6bbbd4c4cb8404bd6e4e364ff">#</a></div> <div class="comment-content">On the topic of doing this via IoC, Autofac deals with metadata quite well. I've typically used things like named/keyed registrations for specific strategy implementations, but I can see where a developer dropped into the middle of the codebase could be confused amid the indirection.<br> So an attribute on the implementation class can serve two important purposes here -- first, it does indeed make the intended role of the class explicit, and second, it can give your IoC container registration hints, particularly helpful when using assembly scanning / convention-based registrations.</div> <div class="comment-date">2013-01-10 16:25 UTC</div> </div> <div class="comment" id="dd14acab568c4d49b148204cc213337e"> <div class="comment-author">Andrey K <a href="#dd14acab568c4d49b148204cc213337e">#</a></div> <div class="comment-content">Mark, big thanks for your &quot;january post&quot;!<br> <br> The detached metadata &quot;injecting mapping dictionary from composition root&quot; sample is great! <br> <br> In my practice I did such mapping-thing with inharitance and the result is a lot of factories all-over the code, that knows about DI-container (because of lazy-initialization with many dependencies inside each factory)<br> <br> With some modifications, like generic-parameters and lazy-initialization, injection dictionary over constructor inside such universal one-universal-factory-class really could be great solution for the most cases.</div> <div class="comment-date">2013-01-10 19:59 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. NSubstitute Auto-mocking with AutoFixture https://blog.ploeh.dk/2013/01/09/NSubstituteAuto-mockingwithAutoFixture 2013-01-09T07:18:24+00:00 Mark Seemann <div id="post"> <p> <em>This post announces the availability of the NSubstitute-based Auto-mocking extension for AutoFixture.</em> </p> <p> Almost two and a half years ago I added an <a href="/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx">Auto-mocking extension to AutoFixture, using Moq</a>. Since then, a couple of other Auto-mocking extensions have been added, and this Sunday I accepted a pull request for Auto-mocking with <a href="http://nsubstitute.github.com/">NSubstitute</a>, bringing the total number of Auto-mocking extensions for AutoFixture up to four: <ul> <li><a href="http://nuget.org/packages/AutoFixture.AutoMoq">AutoFixture.AutoMoq</a></li> <li><a href="http://nuget.org/packages/AutoFixture.AutoRhinoMocks">AutoFixture.AutoRhinoMocks</a></li> <li><a href="http://nuget.org/packages/AutoFixture.AutoFakeItEasy">AutoFixture.AutoFakeItEasy</a></li> <li><a href="http://nuget.org/packages/AutoFixture.AutoNSubstitute">AutoFixture.AutoNSubstitute</a></li> </ul> (listed both in the order of age and number of NuGet downloads.) </p> <p> Kudos to <a href="https://github.com/dhilgarth">Daniel Hilgarth</a> for creating this extension, and for offering a pull request of high quality! </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8898f922a88742c991f2cb6881afbf6e"> <div class="comment-author">Fxfighter <a href="#8898f922a88742c991f2cb6881afbf6e">#</a></div> <div class="comment-content">Yay, this is much appreciated!</div> <div class="comment-date">2013-01-09 07:28 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Argument Name Role Hint https://blog.ploeh.dk/2013/01/08/ArgumentNameRoleHint 2013-01-08T10:42:06+00:00 Mark Seemann <div id="post"> <p> <em>This article describes how object roles can by indicated by argument or variable names.</em> </p> <p> In my <a href="/2013/01/07/RoleHints.aspx">overview article on Role Hints</a> I described how making object roles explicit can help making code more object-oriented. One way code can convey information about the role played by an object is by proper naming of variables and method arguments. In many ways, this is the converse view of a <a href="/2013/01/07/TypeNameRoleHints.aspx">Type Name Role Hint</a>. </p> <p> To reiterate, the <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229042.aspx">Design Guidelines for Developing Class Libraries</a> provides <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229004.aspx">this rule:</a> </p> <blockquote> <p> Consider using names based on a parameter's meaning rather than names based on the parameter's type. </p> </blockquote> <p> As described in the post about <a href="/2013/01/07/TypeNameRoleHints.aspx">Type Name Role Hints</a>, this rule makes sense when the argument type is too generic to provide enough information about role played by an object. </p> <h3 id="736cdf5501994672ae89dcd2361114d4"> Example: unit test variables <a href="#736cdf5501994672ae89dcd2361114d4" title="permalink">#</a> </h3> <p> Previously I've described how <a href="http://blogs.msdn.com/b/ploeh/archive/2008/10/06/naming-sut-test-variables.aspx">explicitly naming unit test variables after their roles</a> clearly communicates to the <a href="http://xunitpatterns.com/test%20reader.html">Test Reader</a> the purpose of each variable. </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetUserNameFromProperSimpleWebTokenReturnsCorrectResult() { &nbsp;&nbsp;&nbsp; <span style="color: green;">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">SimpleWebTokenUserNameProjection</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> request = <span style="color: blue;">new</span> <span style="color: #2b91af;">HttpRequestMessage</span>(); &nbsp;&nbsp;&nbsp; request.Headers.Authorization = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AuthenticationHeaderValue</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"Bearer"</span>, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">SimpleWebToken</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">Claim</span>(<span style="color: #a31515;">"userName"</span>, <span style="color: #a31515;">"foo"</span>)).ToString()); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.GetUserName(request); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Equal(<span style="color: #a31515;">"foo"</span>, actual); &nbsp;&nbsp;&nbsp; <span style="color: green;">// Teardown</span> }</pre> </p> <p> Currently I prefer these well-known variable names in unit tests: </p> <ul> <li>sut</li> <li>expected</li> <li>actual</li> </ul> <p> Further variables can be named on a case-by-case basis, like the <em>request</em> variable in the above example. </p> <h3 id="4edbb967f99d43dca684e7419ebf3c3f"> Example: Selecting next Wizard Page <a href="#4edbb967f99d43dca684e7419ebf3c3f" title="permalink">#</a> </h3> <p> Consider a Wizard in a rich client, implemented using the <a href="http://en.wikipedia.org/wiki/Model_View_ViewModel">MVVM</a> pattern. A Wizard can be modeled as a 'Graph of Responsibility'. A simple example may look like this: </p> <p> <img src="/content/binary/wizard-graph.png" border="0"> </p> <p> This is a rather primitive Wizard where the start page asks you whether you want to proceed in a 'default' or 'custom' way: </p> <p> <img src="/content/binary/wizard-selection-dialog.png" border="0"> </p> <p> If you select <em>Default</em> and press Next, the Wizard will immediately proceed to the Progress step. If you select <em>Custom</em>, the Wizard will first show you the Custom step, where you can tweak your experience. Subsequently, when you press Next, the Progess step is shown. </p> <p> Imagine that each Wizard page must implement the IWizardPage interface: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IWizardPage</span> : <span style="color: #2b91af;">INotifyPropertyChanged</span> { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IWizardPage</span> Next { <span style="color: blue;">get</span>; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IWizardPage</span> Previous { <span style="color: blue;">get</span>; } }</pre> </p> <p> The Start page's View Model must wait for the user's selection and then serve the correct Next page. Using the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">DIP</a>, the StartWizardPageViewModel doesn't need to know about the concrete 'custom' and 'progress' steps: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IWizardPage</span> customPage; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IWizardPage</span> progressPage; <span style="color: blue;">private</span> <span style="color: blue;">bool</span> isCustomChecked; &nbsp; <span style="color: blue;">public</span> StartWizardPageViewModel( &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IWizardPage</span> progressPage, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IWizardPage</span> customPage) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.progressPage = progressPage; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.customPage = customPage; } &nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IWizardPage</span> Next { &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.isCustomChecked) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.customPage; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.progressPage; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Notice that the StartWizardPageViewModel depends on <em>two</em> different IWizardPage objects. In such a case, the interface name is insufficient to communicate the role of each dependency. Instead, the argument names <em>progressPage</em> and <em>customPage</em> are used to convey the role of each object. The role of the <em>customPage</em> is more specific than just being a Wizard page - it's the 'custom' page. </p> <h3 id="beacd4491e81489296c3f1016e999e78"> Example: Message Router <a href="#beacd4491e81489296c3f1016e999e78" title="permalink">#</a> </h3> <p> While you may not be building Wizard-based user interfaces with MVVM, I chose the previous example because the problem domain (that of modeling a Wizard UI) is something most of us can relate to. Another set of examples is much more general-purpose in nature, but may feel more abstract. </p> <p> Due to the multicore problem, asynchronous messaging architectures are becoming increasingly common - just consider the growing popularity of <a href="http://abdullin.com/cqrs/">CQRS</a>. In a <a href="http://www.eaipatterns.com/PipesAndFilters.html">Pipes and Filters</a> architecture, <a href="http://www.eaipatterns.com/MessageRouter.html">Message Routers</a> are central. Many variations of Message Routers presented in <a href="http://www.amazon.com/gp/product/0321200683/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321200683">Enterprise Integration Patterns</a> provide examples in C# where the alternative outbound channels are identified with Role Hints such as <em>outQueue1</em>, <em>outQueue2</em>, etc. See e.g. pages 83, 233, 246, etc. Due to copyright reasons, I'm not going to repeat them here, but here's a generic Message Router that does much the same: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ConditionalRouter</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IMessageSpecification</span>&lt;T&gt; specification; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IChannel</span>&lt;T&gt; firstChannel; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IChannel</span>&lt;T&gt; secondChannel; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ConditionalRouter( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IMessageSpecification</span>&lt;T&gt; specification, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IChannel</span>&lt;T&gt; firstChannel, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IChannel</span>&lt;T&gt; secondChannel) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.specification = specification; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.firstChannel = firstChannel; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.secondChannel = secondChannel; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Handle(T message) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.specification.IsSatisfiedBy(message)) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.firstChannel.Send(message); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.secondChannel.Send(message); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Once again, notice how the ConditionalRouter selects between the two roles of <em>firstChannel</em> and <em>secondChannel</em> based on the outcome of the <a href="http://en.wikipedia.org/wiki/Specification_pattern">Specification</a>. The constructor argument names carry (slightly) more information about the role of each channel than the interface name. </p> <h3 id="c3e859fd9ea74a71ae9ff9922f28b3ad"> Summary <a href="#c3e859fd9ea74a71ae9ff9922f28b3ad" title="permalink">#</a> </h3> <p> Parameter or variable names can be used to convey information about the role played by an object. This is especially helpful when the type of the object is very general (such as string, DateTime, int, etc.), but can also be used to select among alternative objects of the same type even when the type is specific enough to adhere to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility</a> and <a href="http://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation</a> principles. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Type Name Role Hints https://blog.ploeh.dk/2013/01/07/TypeNameRoleHints 2013-01-07T11:28:15+00:00 Mark Seemann <div id="post"> <p> <em>This article describes how object roles can by indicated by type names.</em> </p> <p> In my <a href="/2013/01/07/RoleHints.aspx">overview article on Role Hints</a> I described how making object roles explicit can help making code more object-oriented. When first hearing about the concept of object roles, a typical reaction is: <em>How is that different from the class name? Doesn't the class name communicate the purpose of the class?</em> </p> <p> Sometimes it does, so this is a fair question. However, there are certainly other situations where this isn't the case at all. </p> <p> Consider many primitive types: do the names <a href="http://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx">int</a> (or <a href="http://msdn.microsoft.com/en-us/library/system.int32.aspx">Int32</a>), <a href="http://msdn.microsoft.com/en-us/library/c8f5xwh7.aspx">bool</a> (or <a href="http://msdn.microsoft.com/en-us/library/system.boolean.aspx">Boolean</a>), <a href="http://msdn.microsoft.com/en-us/library/system.guid.aspx">Guid</a>, <a href="http://msdn.microsoft.com/en-us/library/system.datetime.aspx">DateTime</a>, <a href="http://msdn.microsoft.com/en-us/library/system.version.aspx">Version</a>, <a href="http://msdn.microsoft.com/en-us/library/362314fe.aspx">string</a>, etc. communicate anything about the roles played by instances? </p> <p> In most cases, such type names provide poor hints about the roles played by the instances. Most developers already implicitly know this, and the <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229042.aspx">Design Guidelines for Developing Class Libraries</a> also provides <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229004.aspx">this rule:</a> </p> <blockquote> <p> Consider using names based on a parameter's meaning rather than names based on the parameter's type. </p> </blockquote> <p> Most of us can probably agree that code like this would be hard to use correctly: </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> TryCreate(<span style="color: #2b91af;">Uri</span> u1, <span style="color: #2b91af;">Uri</span> u2, <span style="color: blue;">out</span> <span style="color: #2b91af;">Uri</span> u3)</pre> <p> <p> Which values should you use for <em>u1</em>? Which value for <em>u2</em>? </p> <p> Fortunately, <a href="http://msdn.microsoft.com/en-us/library/ay1kx93s.aspx">the actual signature</a> follows the Design Guidelines: </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> TryCreate(<span style="color: #2b91af;">Uri</span> baseUri, <span style="color: #2b91af;">Uri</span> relativeUri, <span style="color: blue;">out</span> <span style="color: #2b91af;">Uri</span> result)</pre> <p> <p> This is much better because the argument names communicate the roles the various Uri parameters play relative to each other. With the object roles provided by descriptive parameter names, the method signature is often all the documentation required to understand the proper intended use of the method. </p> <p> The Design Guidelines' rules sound almost universal. Are there cases when the name of a type is more informative than the argument or variable name? </p> <h3 id="35dfa9c6fb8f415e988002396cdb3008"> Example: Uri.IsBaseOf <a href="#35dfa9c6fb8f415e988002396cdb3008" title="permalink">#</a> </h3> <p> To stay with the <a href="http://msdn.microsoft.com/en-us/library/system.uri.aspx">Uri class</a> for a little while longer, consider the <a href="http://msdn.microsoft.com/en-us/library/system.uri.isbaseof.aspx">IsBaseOf</a> method: </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">bool</span> IsBaseOf(<span style="color: #2b91af;">Uri</span> uri)</pre> <p> <p> This method accepts <em>any</em> Uri instance as input parameter. The <em>uri</em> argument doesn't play any other role than being an Uri instance, so there's no reason that an API designer should go out of his or her way to come up with some artificial 'role' name for the parameter. In this example, the name of the type is sufficient information about the role played by the instance - or you could say that in this context the class itself and the role it plays conflates into the same name. </p> <h3 id="32be6ba2ade649be9990eada3342a688"> Example: MVC instances <a href="#32be6ba2ade649be9990eada3342a688" title="permalink">#</a> </h3> <p> If you've ever worked with <a href="http://www.asp.net/mvc">ASP.NET MVC</a> or <a href="http://www.asp.net/web-api">ASP.NET Web API</a> you may have noticed that rarely do you refer to Model, View or Controller instances with variables. Often, you just return a new model directly from the relevant Action Method: </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">ViewResult</span> Get() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> now = <span style="color: #2b91af;">DateTime</span>.Now; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> currentMonth = <span style="color: blue;">new</span> <span style="color: #2b91af;">Month</span>(now.Year, now.Month); &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.View(<span style="color: blue;">this</span>.reader.Query(currentMonth)); }</pre> <p> <p> In this example, notice how the model is implicitly created with a call to the reader's Query method. (However, you get a hint about the intermediary variables' roles from their names.) If we ever assign a model instance to a local variable before returning it with a call to the View method, we often tend to simply name that variable <em>model</em>. </p> <p> Furthermore, in ASP.NET MVC, do you ever create instances of Controllers or Views (except in unit tests)? Instances of Controllers and Views are created by the framework. Basically, the role each Controller and View plays is embodied in their class names - HomeController, BookingController, BasketController, BookingViewModel, etc. </p> <h3 id="be37da5205964bb3977bf67a8b9c65ef"> Example: Command Handler <a href="#be37da5205964bb3977bf67a8b9c65ef" title="permalink">#</a> </h3> <p> Consider a 'standard' <a href="http://abdullin.com/cqrs/">CQRS</a> implementation with a single Command Handler for each Command message type in the system. The Command Handler interface might be defined like this: </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">ICommandHandler</span>&lt;T&gt; { &nbsp;&nbsp;&nbsp; <span style="color: blue;">void</span> Execute(T command); }</pre> <p> <p> At this point, the type argument T could be literally any type, so the argument name <em>command</em> conveys the role of the object better than the type. However, once you look at a concrete implementation, the method signature materializes into something like this: </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">void</span> Execute(<span style="color: #2b91af;">RequestReservationCommand</span> command)</pre> <p> <p> In the concrete case, the type name (RequestReservationCommand) carries more information about the role than the argument name (command). </p> <h3 id="46c654f3d1be456db1c72b390aa9a556"> Example: Dependency Injection <a href="#46c654f3d1be456db1c72b390aa9a556" title="permalink">#</a> </h3> <p> With basic Dependency Injection, a common Role Hint is the type itself. </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">public</span> BasketController( &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IBasketService</span> basketService, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CurrencyProvider</span> currencyProvider) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (basketService == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"basketService"</span>); &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (currencyProvider == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"currencyProvider"</span>); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.basketService = basketService; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.currencyProvider = currencyProvider; }</pre> <p> <p> From the point of view of the BasketController, the type names of the IBasketService interface and the CurrencyProvider abstract base class carry all required information about the role played by these dependencies. You can tell this because the argument names simply echo the type names. In the complete system, there could conceivably be more than one implementation of IBasketService, but in the context of the BasketController, <em>some</em> IBasketService instance is all that is required. </p> <h3 id="6bf23b3514374b288b6c903a5462385d"> Summary <a href="#6bf23b3514374b288b6c903a5462385d" title="permalink">#</a> </h3> <p> The more generic a type is, the less information about role does the type name itself carry. Primitives and Value Objects such as integers, strings, Guids, Uris, etc. can be used in so many ways that you should strongly consider proper naming of arguments and variables to convey information about the role played by an object. This is what the Framework Design Guidelines suggest. However, as types become increasingly specific, their names carry more information about their roles. For very specific classes, the class name itself may carry all appropriate information about the object's intended role. As a rule of thumb, expect such classes to also adhere to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Role Hints https://blog.ploeh.dk/2013/01/07/RoleHints 2013-01-07T10:15:07+00:00 Mark Seemann <div id="post"> <p> <em>This article provides an overview of different ways to hint at the role an object is playing.</em> </p> <p> One of the interesting points in <a href="http://www.amazon.com/gp/product/0470684208/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0470684208">Lean Architecture</a> is that many so-called <em>object-oriented</em> languages aren't really object-oriented, but rather <em>class-oriented</em>. This is as true for C# as for Java. The code artifacts are classes (or interfaces, etc.); not objects. </p> <p> When asked to distinguish, most of us understand that objects are <em>instances</em> of classes, but since the languages are centered around classes, we sometimes forget this distinction and treat objects and classes as one and the same. When this happens, we run into some of the problems that <a href="http://www.udidahan.com">Udi Dahan</a> describes in his excellent talk <a href="http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan">Making Roles Explicit</a>. The solution is proposed in the same talk: make roles explicit. </p> <p> So, what's a role? you might ask. A role is the purpose of an object instance in a given context. (That sounds a bit like the (otherwise rather confusingly described) concept of <a href="http://en.wikipedia.org/wiki/Data,_context_and_interaction">DCI</a>.) An object can play more than one role during its lifetime, or a class can be instantiated in one of the roles it can play. Some objects can play only a single role. </p> <p> There are several ways to hint at the role played by an object: </p> <ul> <li><a href="/2013/01/07/TypeNameRoleHints.aspx">Type Name</a>. Use the name of the type (such as the class name) to hint at the role played by instances of the class. This seems to be mostly relevant for objects that can play only a single role.</li> <li><a href="/2013/01/08/ArgumentNameRoleHint.aspx">Argument Name</a>. Use the name of an argument (or variable) to hint at the role played by an instance.</li> <li><a href="/2013/01/09/MetadataRoleHint.aspx">Metadata</a>. Use data about code (such as attributes or marker interfaces) to hint at the role played by instances of the class.</li> <li><a href="/2013/01/10/RoleInterfaceRoleHint.aspx">Role Interfaces</a>. A class can implement several <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a>; clients can treat instances as the roles they require.</li> <li><a href="/2013/01/11/PartialTypeNameRoleHint.aspx">Partial Type Name</a>. Use part of the type name to hint at a role played by objects. This can be used to enable Convention over Configuration.</li> </ul> <p> Making the role of an object explicit can tip the balance in favor of true object-orientation instead of class-orientation. It will likely make your code easier to read, understand and maintain. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Zookeepers must become Rangers https://blog.ploeh.dk/2012/12/18/ZookeepersmustbecomeRangers 2012-12-18T15:08:31+00:00 Mark Seemann <div id="post"> <p> <em>For want of a nail the shoe was lost... This post explains why Zookeeper developers must become Ranger developers to escape the vicious cycle of impossible deadlines and low-quality code.</em> </p> <p> In <a href="/2012/12/18/RangersAndZookeepers.aspx">a previous article</a> I wrote about Ranger and Zookeper developers. The current article is going to make no sense to you if you haven't read the previous, but in brief, Rangers explicitly deal with versioning and forwards and backwards compatibility (henceforth called temporal compatibility) in order to produce workable code. </p> <p> While reading the previous article, you may have thought that you'd rather like to be a Ranger than a Zookeeper, simply because it sounds cooler. Well, I didn't pick those names by accident :) You don't want to be a Zookeeper developer. </p> <p> Zookeepers may be under the impression that, since they (or their organization) control the entire installation base of their code, they don't need to deal with the versioning aspect of software design. This is an illusion. It may seem like a little thing, but, through a chain reaction, the lack of versioning leads to impossible deadlines, slipping code quality, death marches and many unpleasant aspects of our otherwise wonderful vocation. </p> <p> In order to escape the vicious cycle of low-quality-code, death marches, and firefighting, Zookeepers need to explicitly deal with versioning of the software they produce, essentially turning themselves into Rangers. </p> <p> In the following, I will make two assumptions about the type of software I discuss here: </p> <ul> <li>It's impossible to predict all future feature requirements.</li> <li>Applications don't exist in a vacuum. They depend on other applications, and other applications depend on them.</li> </ul> <p> For the vast majority of Zoo Software, I believe these tenets to be true. </p> <p> In order to understand why versioning is so important (even for Zoo Software) it's important to first understand why Zookeepers consistently disregard it. </p> <h3 id="f80a7e69fb684acd97edc96a7d077828"> We don't need no steenking design guidelines <a href="#f80a7e69fb684acd97edc96a7d077828" title="permalink">#</a> </h3> <p> It's sometimes a bit surprising to me that (.NET) programmers are resistant to good design. There's this tome of knowledge originally published as the <a href="http://www.amazon.com/gp/product/0321545613/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321545613&amp;linkCode=as2&amp;tag=ploeh-20">Framework Design Guidelines</a> and later published online on MSDN as the <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229042.aspx">Design Guidelines for Developing Class Libraries</a>. Even if you don't care to read through it all, tools such as <a href="http://msdn.microsoft.com/en-us/library/3z0aeatx.aspx">Visual Studio Code Analysis</a> and <a href="http://www.microsoft.com/en-us/download/details.aspx?id=6544">FxCop</a> (which is free) encapsulate many of those guidelines and helps identify when your code diverges. </p> <p> In my experience, Zookeeper resistance against the Framework Design Guidelines is perfectly examplified by the <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229013.aspx">'rules' related to selecting between classes and interfaces</a>: </p> <ul> <li>Do favor defining classes over interfaces.</li> <li>Do use abstract (MustInherit in Visual Basic) classes instead of interfaces to decouple the contract from implementations.</li> </ul> <p> (For the record, I think this is horrible advice, but that's a discussion for another day.) </p> <p> The problem with a guideline like this is that most Zookeepers react to such advice by thinking: "This isn't relevant for the kind of code I write. I control the entire install base of my code, so it's not a problem for me to introduce a breaking change. That entire knowledge base of design guidelines is targeted at another type of developers. I need not care about it." </p> <p> Zookeepers fail to address versioning and temporal compatibility because they (incorrectly) assume that they can always schedule deployments of services and clients together in such a way that breaking changes never actually break anything. That may work as long as systems are monolithic and exist in a vacuum, but as soon as you start integrating systems, this disregard for versioning leads straight to hell. </p> <h3 id="2a59e3f609e2494c985de34fa4a4714d"> Example: an internal music catalog service <a href="#2a59e3f609e2494c985de34fa4a4714d" title="permalink">#</a> </h3> <p> Now that you understand why Zookeepers tend to ignore versioning it's time to understand where that attitude leads. This is best done with an example. </p> <p> Imagine a team tasked with building a music catalog service for internal use. In the first release of the service a track looks like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">track</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span>Recovery<span style="color: blue;">&lt;/</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">artist</span><span style="color: blue;">&gt;</span>Curve<span style="color: blue;">&lt;/</span><span style="color: #a31515;">artist</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">album</span><span style="color: blue;">&gt;</span>Come Clean<span style="color: blue;">&lt;/</span><span style="color: #a31515;">album</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">length</span><span style="color: blue;">&gt;</span>288<span style="color: blue;">&lt;/</span><span style="color: #a31515;">length</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">track</span><span style="color: blue;">&gt;</span></pre> </p> <p> This is obviously a naïve attempt, and a bit of planning could probably have prevented the following nasty particular surprise. However, this is just an illustrative example, and I'm sure you've found yourself in a situation where a new requirement took you entirely by surprise - no matter how much you tried to predict the future. </p> <p> After the team has released the first version of the music catalog service, it turns out that some tracks may be the result of collaborations, and thus have multiple artists. The track may also appear in multiple albums (such as compilations or greatest hits collections), or on no album at all. Thus, version 2 of a track will have to look like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">track</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span>Under Pressure<span style="color: blue;">&lt;/</span><span style="color: #a31515;">name</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">artists</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">artist</span><span style="color: blue;">&gt;</span>David Bowie<span style="color: blue;">&lt;/</span><span style="color: #a31515;">artist</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">artist</span><span style="color: blue;">&gt;</span>Queen<span style="color: blue;">&lt;/</span><span style="color: #a31515;">artist</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">artists</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">albums</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">album</span><span style="color: blue;">&gt;</span>Hot Space<span style="color: blue;">&lt;/</span><span style="color: #a31515;">album</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">album</span><span style="color: blue;">&gt;</span>Greatest Hits<span style="color: blue;">&lt;/</span><span style="color: #a31515;">album</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">album</span><span style="color: blue;">&gt;</span>The Singles: 1969-1993<span style="color: blue;">&lt;/</span><span style="color: #a31515;">album</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">albums</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">length</span><span style="color: blue;">&gt;</span>242<span style="color: blue;">&lt;/</span><span style="color: #a31515;">length</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">track</span><span style="color: blue;">&gt;</span></pre> </p> <p> This is obviously a breaking change, but since the team works in an organization where the entire installation base is under control, they work with the Enterprise Architecture team to schedule a release of this breaking change with all their clients. </p> <p> However, the service was made to support other systems, so this change must be coordinated with all the known clients. It turns out that three systems depend on the music catalog service. </p> <p> <img src="/content/binary/music-catalog-clients.png" border="0"> </p> <p> Each of these systems are being worked on by separate teams, each with individual deadlines. These teams are not planning to deploy new versions of their applications all on the same day. Their immediate schedules are already set, and they aren't aligned. </p> <p> <img src="/content/binary/immediate-schedule.png" border="0"> </p> <p> The Architecture team must negotiate future iterations with teams A, B, and C to line them up so that the music catalog team can release their new version on the same day. </p> <p> <img src="/content/binary/long-term-schedule.png" border="0"> </p> <p> This alignment may be months into the future - perhaps a significant fraction of a year. The music catalog team can't sit still while they wait for this date to arrive, so they work on other new features, most likely introducing other breaking changes along the way. </p> <p> Meanwhile, team A goes through three iterations. In the first two iterations, they know that they are going to deploy into a production environment with version 1 of the music catalog, so they are going to need a testing environment that looks like that. For the third iteration, they know that they are going to deploy into a production environment with version 2 of the music catalog, so they are going to have to change their testing environment to reflect that fact. </p> <p> Team B goes through four iterations that don't align with those of Team A. When Team A updates their testing environment to music catalog version 2, Team B is still working on an iteration where they must test against version 1. Thus, they must have their own testing environment. They can't share the testing environment with Team A. </p> <p> The same is true for Team C: it needs its own private testing environment. Notice that this is a testing environment that not only involves configuration and maintenance of the team's own application, but also of its dependency, the music catalog service. In order to provide realistic data for performance testing, each testing environment must also be maintained with representative sample data, and may have to run on production-like hardware, in production-like network topologies. Add software licences to the mix, and you may start to realize that such a testing environment is expensive. </p> <p> So far, the analysis has been unrealistically simplified. It turns out that the A, B, and C applications have other dependencies besides the music catalog service. </p> <p> <img src="/content/binary/more-dependencies.png" border="0"> </p> <p> Each of these dependencies are also moving, producing new features. Some of those new features also involve breaking changes. Once again, the Enterprise Architecture team must negotiate a coordinated deployment date to make sure nothing breaks. However, the organization have more applications than A, B, and C. Application D, for instance, doesn't depend on the music catalog service, but it shares a dependency with application A. Thus, Team D must take part in the coordination effort, making sure that they deploy their new version on the same day as everyone else. Meanwhile, they'll need yet another one of those expensive testing environments. </p> <p> I'm not making this up. I've had clients (financial institutions) that had hundreds of testing environments and big, coordinated deployments. Their applications didn't have two or three dependencies, but dozens of dependencies each - often on mainframe-based systems (<em>very</em> expensive). </p> <h3 id="a26d057c43284bd181e08dafa421ca79"> The rot of Big Bang Releases <a href="#a26d057c43284bd181e08dafa421ca79" title="permalink">#</a> </h3> <p> All this leads to Big Bang Releases. Once or twice each year, all Zoo Sofware is simultaneously deployed according to a meticulously laid plan. This date becomes an <em>unnegotiatable</em> deadline. No team can miss the deadline, because dozens of other teams directly or indirectly depend on each other. </p> <p> What happens when a Big Bang deadline grows nearer? What if you aren't ready to release? What if you just discovered a catastrophic bug? </p> <p> This is what happens: </p> <ul> <li>Teams work nights and weekends to meet deadlines. Members burn out, or get divorced, or decide to quit. More work is left for remaining team members. A vicious circle indeed.</li> <li>Software is deployed with known bugs. Often, it's simply not possible to address all bugs in time, because they are discovered too late.</li> <li>The team enters survival mode, building up technical debt. Unit tests are ignored. Dirty hacks are made. Code rots. In the end, the team deploys a version of the software where they don't really know what works and what doesn't work. This makes it harder to work on the next version, because the lack of test coverage means that they don't even <em>know</em> if they'll be introducing breaking changes. At the absence of knowledge it's better to assume the worst. Another vicious cycle is created.</li> </ul> <p> Remember: each team <em>must</em> deploy at the Big Bang Release, no matter how bad the quality is. It's not even possible to elect to skip a release all together, because the other teams have built <em>their</em> new versions with the assumption that your application's breaking changes will be deployed. In a Big Bang Release, there are only two options: Deploy everything, or deploy nothing. Deciding to deploy nothing may threaten the entire company's existence, so that decision is rarely made. </p> <p> Do you think I'm exaggerating? I have seen this happen more than once. In fact, I also strongly suspect that this sort of situation recently unfolded itself within a big international <a href="http://en.wikipedia.org/wiki/Independent_software_vendor">ISV</a> - at least, the symptoms are all here, for all to see :) </p> <p> All this because Zookeepers don't deal with temporal compatibility. </p> <h3 id="aea54c96932c4711b08570e695a29fe5"> Call to action <a href="#aea54c96932c4711b08570e695a29fe5" title="permalink">#</a> </h3> <p> Zookeepers must stop being Zookeepers and become Rangers. There's no excuse to not deal explicitly with versioning. You can't just introduce a breaking change in your code. The cost is often much larger than you think. The repercussions can be immense. Sometimes there's no way around breaking changes, but deal with them in tried-and-true ways. Introduce your new API side-by-side with the old API and deprecate the old API. Give clients a chance to move to the new API at their own pace. Monitor usage of your API to figure out when it's safe to completely remove the deprecated API. There are many well-described ways to deal with versioning and temporal compatibility. This is not an article about <em>how</em> to evolve an API, but rather a call to action: explicitly address versioning, or face the consequences! </p> <p> Looser coupling can help. What I've described here is simply a form of tight temporal coupling. Still, even with loosely coupled, asynchronous, messaging-based architectures, messages must still be explicitly versioned to avoid problems. </p> <p> Once an orginazation has moved entirely to a loosely coupled, explicitly versioned architecture where each team can use <a href="http://en.wikipedia.org/wiki/Continuous_delivery">Continuous Delivery</a>, they may find that they don't need that Enterprise Achitecture team at all :) </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="34bc51f475334080a8fd1826a2391846"> <div class="comment-author"><a href="http://wysnet.blogspot.com/">Jason Wyglendowski</a> <a href="#34bc51f475334080a8fd1826a2391846">#</a></div> <div class="comment-content">Interesting post. I agree with you the need for new terminology and ways to express the evolution of software development. This post reminds me how important it is to be mindful when we are producing software. I just recently heard a podcast (http://www.thersa.org/__data/assets/file/0006/999816/20121206NassimNicholasTaleb.mp3) that talks about fragility of systems I think you might find it interesting and I believe it complements the idea of making software robust to change. The speaker also has a good book also related http://www.amazon.com/Antifragile-Things-That-Disorder-ebook/dp/B0083DJWGO/ref=tmm_kin_title_0. <br> <br> Thanks for sharing,<br> <br> Jason</div> <div class="comment-date">2012-12-19 02:08 UTC</div> </div> <div class="comment" id="3dee2f2e389e4eefb195d4c51099f45f"> <div class="comment-author">Markus Zywitza <a href="#3dee2f2e389e4eefb195d4c51099f45f">#</a></div> <div class="comment-content">The difference between &quot;Rangers&quot; and &quot;Zookeepers&quot; is the same as between professional and unprofessional developers, independent of the installation base. The Agile movement, which is adopted among a lot of internal development teams, mandates practices like test-first, continuous integration, automated acceptance tests, instant feedback and so on.<br> <br> In such a case, one had to change an acceptance test if a service could not deal with legacy clients. A developer who does this or a manager who commands this, acts unprofessional.<br> <br> A team that calls itself agile and does not use the tools and techniques necessary for agile development, is just a bunch of cowboy coders, but neither &quot;Zookeepers&quot; nor &quot;Rangers&quot;.<br> </div> <div class="comment-date">2012-12-19 08:43 UTC</div> </div> <div class="comment" id="872e4be110eb4101a55108f01b7a6441"> <div class="comment-author">Markus <a href="#872e4be110eb4101a55108f01b7a6441">#</a></div> <div class="comment-content">Markus, I suspect you can adopt TDD+CS+Automation and still be a Zookeeper. All those things are boxes you can tick and still get your company into a mess. <br> <br> The point of the article (IMO) is that developers need to learn a new tool that you haven't listed - namely good internal version handling (and to start thinking of internal collaborators in the same way as external).<br> <br> A tricky thing for an organisation is to decide when make the move from (easy-to-write) temporally coupled code to discrete services (with increased versioning and infrastructure overheads). Most places with this problem have started off small, grow quickly by bolting bits onto the existing architecture, and by the time someone switched on enough to start changing the organisation takes charge, it is too late to make the switch easily. TBH most places I've worked where this is the root cause of the problems, the Enterprise Architect team can't see the wood for the trees.</div> <div class="comment-date">2012-12-19 14:28 UTC</div> </div> <div class="comment" id="39c0646f5af0449a97bd9dcf1f9b7064"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#39c0646f5af0449a97bd9dcf1f9b7064">#</a></div> <div class="comment-content">Markus Zywitza, the dictionary definition of &quot;professional&quot; simply mean that one gets paid to do a particular task. There are lots of professional programmers that don't use testing, etc. While I agree that it often seems ignorant or short-sighted to neglect to follow what you call &quot;agile practices&quot;, you can hardly say that it's unprofessional if the team gets paid to do it. Thus, we need a different terminology.<br> <br> However, the other Markus is spot on. The message of this article isn't that you should use TDD, Continuous Integration, etc. Lot's of people have already said that before me. The message is that even if you are an 'internal' developer, you must think explicitly about versioning and backwards compatibility.</div> <div class="comment-date">2012-12-26 21:44 UTC</div> </div> <div class="comment" id="97704c2562074407b2b3cced52bb56df"> <div class="comment-author"><a href="http://www.skov-boisen.dk">Simon Skov Boisen</a> <a href="#97704c2562074407b2b3cced52bb56df">#</a></div> <div class="comment-content">I think Markus the firsts definition of an professional developer were more in the line of how dear Uncle Bob defines it in The Clean Coder rather then the strict dictionary definition. A developer who takes responsibility for the code he writes by having tests, delivering on time, saying and sticking to a no and not agreeing to impossible schedules. </div> <div class="comment-date">2012-12-28 17:25 UTC</div> </div> <div class="comment" id="a8e36b629d144fe09c48ef43a6ccb9b9"> <div class="comment-author">Harry McIntyre <a href="#a8e36b629d144fe09c48ef43a6ccb9b9">#</a></div> <div class="comment-content">I think some copy-and-paste has led to some confusion. I'm actually Harry McIntyre, not another Markus! Apologies.</div> <div class="comment-date">2013-01-02 10:00 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Rangers and Zookeepers https://blog.ploeh.dk/2012/12/18/RangersandZookeepers 2012-12-18T08:10:23+00:00 Mark Seemann <div id="post"> <p> <em>This article discusses software that runs in the wild, versus software running in potentially controllable environments.</em> </p> <p> There are many perspectives on software development. One particular perspective that has always interested me is the distinction between software running 'in the wild' versus software running in potentially controllable environments (such as corporate networks, including DMZs). Whether it's one or the other has a substantial impact on how you write, release and maintain software. </p> <h3 id="b8c915b86f71498882ed88d03b87259a"> Historical perspective <a href="#b8c915b86f71498882ed88d03b87259a" title="permalink">#</a> </h3> <p> Software 'in the wild' is simply software where you don't know, or can't control, the install base. In the good old days (a few years ago) that typically meant software produced by <a href="http://en.wikipedia.org/wiki/Independent_software_vendor">ISVs</a> such as Microsoft, Oracle, SAS, SAP, Autodesk, Adobe etc. The software produced by such organizations were/are often purchased by license and deployed by enterprise operations organizations. Even for ISVs targeting end-users directly (such as personal tax software, single player games, etc.), the software was/is typically installed by the individual user, and the ISV had/has zero control of the deployment environment or schedule. </p> <p> The opposite of ISV Software has until recently been Enterprise Software. The problem with this term is that it has become almost derogatory, but I don't think that's entirely fair, because the forces working on this kind of software are very different from those working on ISV software. In this category I count specialized software made for specialized, internal purposes. It can be developed by in-house developers or a hired team, but the main characteristic is that the software is deployed in a potentially controllable environment (I originally wrote 'controlled environment,' but one reviewer interpreted this to indicate only software explicitly managed by process and tools such as Chef or Puppet). Even if there are several deployment environments such as testing, staging and production, and even if we are talking about Client/Server architectures with desktop clients deployed to enterprise desktops, an operations team should be able to keep track of all installations and schedule updates (if any). Often the original developers can work with operations to troubleshoot problems directly on the installed machine (or at least get logs). </p> <p> Note that I'm not counting software such as SAP, Microsoft Dynamics or Oracle as Enterprise Software, despite the fact that such software is <em>used</em> by enterprises. Enterprise software is software developed by the enterprise itself, for its own purposes. </p> <p> Enterprise Software can be a small system that sits in a corner of a corporate network, used by two employees every last weekday of the month. It can also be a massively scalable system build for a special occasion, such as the official site for a big sports tournament like the <a href="http://www.fifa.com/worldcup">FIFA World Cup</a> or the Summer Olympics. </p> <h3 id="e98e0304259f4c378cb2fcc387a43863"> Current perspective <a href="#e98e0304259f4c378cb2fcc387a43863" title="permalink">#</a> </h3> <p> The historical distinction of ISV versus Enterprise Development makes less and less sense today. First of all, with the shift of emphasis towards <a href="http://en.wikipedia.org/wiki/Software_as_a_service">SaaS</a>, traditional ISVs are moving into an area that looks a lot like Enterprise Development. With SaaS, vendors suddenly find themselves in a situation where they control the entire installation base (at least on the service side). Some of them are now figuring out that this enables them to iterate faster than before. <a href="http://www.dotnetrocks.com/default.aspx?showNum=827">Apparently</a> even such an Enterprisey-sounding service as the <a href="http://tfs.visualstudio.com/">Team Foundation Service</a> is now deploying new features several times a year. Expect that cadence to increase. </p> <p> On the other hand, the rising popularity of Open Source Software (OSS) suddenly puts a lot of OSS developers in the old position of ISV developers. OSS tends to run in the wild, and the developers have no control of the installation base or upgrade schedules. </p> <p> Oh, and do I even have to say 'Apps'? </p> <p> Thus, we need a better terminology. Developing, supporting and managing software in 'the wild' sounds a lot like the duties of a <a href="http://en.wikipedia.org/wiki/Park_ranger">Ranger</a>: you can put overall plans into motion to nudge the environment in a certain direction, and sometimes you encounter a particular specimen with which you can interact, but essentially you'll have to understand complex environmental dynamics and plan for the long term. </p> <p> If traditional ISV developers, as well as OSS programmers, are Rangers, then Enterprise and SaaS developers must be <a href="http://en.wikipedia.org/wiki/Zookeeper">Zookeepers</a>. They deal with a controlled environment, can keep an accurate tally, and execute detailed project plans at pre-planned schedules. </p> <p> OK, I admit that it sounds cooler to be a Ranger than a Zookeeper, but the metaphor makes sense to me :) </p> <p> As a corrollary, we can talk about Wildlife Software versus Zoo Software. </p> <h3 id="5b150caf65014840a0d7b5802dc11619"> Forces <a href="#5b150caf65014840a0d7b5802dc11619" title="permalink">#</a> </h3> <p> The forces working on Wildlife Software versus Zoo Software are very different: </p> <p> <table border="1"> <thead> <tr align="left" valign="top"> <th></th> <th>Advantages</th> <th>Disadvantages</th> </tr> </thead> <tbody> <tr align="left" valign="top"> <td>Wildlife Software</td> <td> Since you can't control the installation base, you have to make the software robust, well-tested, secure, easy (enough) to install, and documented. It should be well-instrumented and possible to troubleshoot without being the original developer. Once you release a version into the wild, it must be able to stand on its own, or it will die. This is a rather Darwinian environment, but the advantage is that the software that survives tends to have some attributes we often associate with high 'quality'. </td> <td> Traditionally, producing software with all these 'quality' attributes has been an expensive and slow endeavor. It also leads to conservatism, because every change must be carefully considered. Once released into the wild, a feature or behavior of a piece of software can't be changed (in that version). To wit: Microsoft has traditionally shipped new versions of Windows, Office, Visual Studio, the BCL etc. years apart. The BCL is peppered with sealed or internal classes, much to the chagrin of programmers everywhere. </td> </tr> <tr align="left" valign="top"> <td>Zoo Software</td> <td> The Product Owners of Zoo Software will expect their programmers to be able to iterate much faster, since the installation base is much smaller and well-known. There are fewer environment permutations to consider - e.g. you may know up front that the software should only have to be able to run on Windows Server 2008 R2 with a SQL Server 2008 R2 database. The entire deployment environment is also well-known, so there are many assumptions you can trust to hold. This indicates that you should be able to produce software in an 'agile' manner. Because the Zoo is a much less dangerous place, the software can be less robust (at least along some axes), which again indicates that it can be produced by a smaller team than corresponding Wildlife Software. This again helps keeping down cost. </td> <td> There are certain quality shortcuts that can be safely made with Zoo Software - e.g. never testing the software on Windows XP if you know it's never going to run on that OS. However, once a team under deadline pressure starts to make warranted shortcuts, it may begin making unwarranted shortcuts as well. Thus, we often experience Zoo Software that is poorly tested, is extremely difficult to deploy, is poorly documented and hard to operate. This, I believe, is why Enterprise Development today has such a negative ring to it. </td> </tr> </tbody> </table> </p> <p> Now that former ISVs are moving into Zoo Software via SaaS, it's going to be interesting to see what happens in this space. </p> <h3 id="b3f8d3ba8ade4babae7cec2e8ad428fb"> Conclusion <a href="#b3f8d3ba8ade4babae7cec2e8ad428fb" title="permalink">#</a> </h3> <p> Don't jump to conclusions about the advantages of either approach. This article is meant to be descriptive, first and foremost. This means that I'm describing the characteristics of Wildlife and Zoo Software as I most commonly encounter those types of software. I'm fully aware of initiatives such as <a href="http://en.wikipedia.org/wiki/DevOps">DevOps</a>, so I'm not saying that software <em>has</em> to be like I describe it - I'm just describing what I'm currently observing. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1cd2aec669754bada5ee161466e06d4c"> <div class="comment-author"><a href="http://nikosbaxevanis.com">Nikos Baxevanis</a> <a href="#1cd2aec669754bada5ee161466e06d4c">#</a></div> <div class="comment-content">Great article! One of the very interesting things to see what happens will be scalability. Since with SaaS we are paying for each virtual machine (VM) that is running, the service must scale or else we have to pay for another VM to handle the load.</div> <div class="comment-date">2012-12-18 10:30 UTC</div> </div> <div class="comment" id="a73b1ba9f2004cc6a913de1824f632e1"> <div class="comment-author"><a href="http://www.skov-boisen.dk">Simon Skov Boisen</a> <a href="#a73b1ba9f2004cc6a913de1824f632e1">#</a></div> <div class="comment-content">Great article! <br> <br> It's an interesting dynamic that while ISVs is moving into creating what can be thought of as Zoo Software due to the SaaS environment the fact that the client in a SaaS environment most often is browser-based means that they remain in Ranger-role on the client-side since the myriad of different browsers and browser-versions makes the new client-world as wild if not even wilder than a traditional desktop application environment.</div> <div class="comment-date">2012-12-18 12:34 UTC</div> </div> <div class="comment" id="083b8bfd7b9b4402881ea8ace3133fe1"> <div class="comment-author"><a href="http://twitter.com/yreynhout">Yves Reynhout</a> <a href="#083b8bfd7b9b4402881ea8ace3133fe1">#</a></div> <div class="comment-content">Given the lingo you introduce, as a Ranger there are many options that make it easy to deliver Zoo like software: e.g. creating an installer that has launch conditions requiring a particular OS, a particular database version. It's a bit of a misconception that all Rangers shrink wrap the entire package (with docs, vids, etc). As a Ranger delivering software for what is basically a Zoo, I can opt to have special trained cowboys that provide training, implementation guidance, or god forbid special customization services (SAP et al). Selling these extra services/consultancy to make up for poor quality is an entire business model in and by itself. I say this because there's more nuance to a Rangers (don't work in a Zoo so can't judge that) modus operandi. If I do any of these things, am I still a Ranger?</div> <div class="comment-date">2012-12-19 08:43 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Encapsulation of properties https://blog.ploeh.dk/2012/11/27/Encapsulationofproperties 2012-11-27T13:53:59+00:00 Mark Seemann <div id="post"> <p> <em>This post explains why Information Hiding isn't entirely the same thing as Encapsulation.</em> </p> <p> Despite being an old concept, <a href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">Encapsulation</a> is one of the most misunderstood principles of object-oriented programming. Particularly for C# and Visual Basic.NET developers, this concept is confusing because those languages have <a href="http://en.wikipedia.org/wiki/Property_(programming)">properties</a>. Other languages, including F#, have properties too, but it would be far from me to suggest that F# programmers are confused :) </p> <p> (As an aside, even in languages without formal properties, such as e.g. Java, you often see property-like constructs. In Java, for example, a pair of a getter and a setter method is sometimes informally referred to as a property. In the end, that's also how .NET properties are implemented. Thus, the present article can also be applied to Java and other 'property-less' languages.) </p> <p> In my experience, the two most important aspects of encapsulation are <em>Protection of Invariants</em> and <em>Information Hiding</em>. Earlier, I have had <a href="/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx">much to say about Protection of Invariants</a>, including the <a href="/2011/05/26/CodeSmellAutomaticProperty.aspx">invariants of properties</a>. In this post, I will instead focus on Information Hiding. </p> <p> With all that confusion (which I will get back to), you would think that Information Hiding is really hard to grasp. It's not - it's really simple, but I think that the <em>name</em> erects an effective psychological barrier to understanding. If instead we were to call it <em>Implementation Hiding</em>, I think most people would immediately grasp what it's all about. </p> <p> However, since Information Hiding has this misleading name, it becomes really difficult to understand what it means. Does it mean that all information in an object should be hidden from clients? How can we reconcile such a viewpoint with the fundamental concept that object-orientation is about <em>data</em> and behavior? Some people take the misunderstanding so far that they begin to evangelize against properties as a design principle. Granted, too heavy a reliance on properties leads to violations of the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a> as well as <a href="http://c2.com/cgi/wiki?FeatureEnvySmell">Feature Envy</a>, but without properties, how can a client ever know the state of a system? </p> <p> Direct field access isn't the solution, as this discloses data to an even larger degree than properties. Still, in the lack of better guidance, the question of Encapsulation often degenerates to the choice between fields and properties. Perhaps the most widely known and accepted .NET design guideline is that <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229057.aspx">data should be exposed via properties</a>. This again leads to the <a href="/2011/05/26/CodeSmellAutomaticProperty.aspx">redundant 'Automatic Property' language feature</a>. </p> <p> Tell me again: how is this </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre> </p> <p> better than this? </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">string</span> Name;</pre> </p> <p> The <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229042.aspx">Design Guidelines for Developing Class Libraries</a> isn't wrong. It's just important to understand why properties are preferred over fields, and <em>it has only partially to do with Encapsulation</em>. The real purpose of this guideline is to enable versioning of types. In other words: it's about backwards compatibility. </p> <p> An example demonstrating how properties enable code to evolve while maintaining backwards compatibility is in order. This example also demonstrates Implementation Hiding. </p> <h3 id="ede5704a9b5b4c68aff505885f22344d"> Example: a tennis game <a href="#ede5704a9b5b4c68aff505885f22344d" title="permalink">#</a> </h3> <p> The <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataTennis">Tennis kata</a> is one of my favorite TDD katas. Previously, I posted <a href="/2011/05/16/TennisKataWithImmutableTypesAndACyclomaticComplexityOf1.aspx">my own, very specific take</a> on it, but I've also, when teaching, asked groups to do it as an exercise. Often participants arrive at a solution not too far removed from this example: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Game</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> player1Score; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> player2Score; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> PointTo(<span style="color: #2b91af;">Player</span> player) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (player == <span style="color: #2b91af;">Player</span>.One) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player1Score &gt;= 30) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score += 10; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score += 15; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player2Score &gt;= 30) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player2Score += 10; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player2Score += 15; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Score &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player1Score == <span style="color: blue;">this</span>.player2Score &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score &gt;= 40) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Deuce"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player1Score &gt; 40 &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score == <span style="color: blue;">this</span>.player2Score + 10) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"AdvantagePlayerOne"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player2Score &gt; 40 &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player2Score == <span style="color: blue;">this</span>.player1Score + 10) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"AdvantagePlayerTwo"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player1Score &gt; 40 &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score &gt;= <span style="color: blue;">this</span>.player2Score + 20) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"GamePlayerOne"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player2Score &gt; 40) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"GamePlayerTwo"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> score1Word = ToWord(<span style="color: blue;">this</span>.player1Score); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> score2Word = ToWord(<span style="color: blue;">this</span>.player2Score); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (score1Word == score2Word) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> score1Word + <span style="color: #a31515;">"All"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> score1Word + score2Word; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> ToWord(<span style="color: blue;">int</span> score) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">switch</span> (score) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> 0: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Love"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> 15: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Fifteen"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> 30: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Thirty"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> 40: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Forty"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">default</span>: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentException</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"Unexpected score value."</span>, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"score"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Granted: there's a lot more going on here than just a single property, but I wanted to provide an example of enough complexity to demonstrate why Information Hiding is an important design principle. First of all, this Game class tips its hat to that other principle of Encapsulation by protecting its invariants. Notice that while the Score property is public, it's read-only. It wouldn't make much sense if the Game class allowed an external caller to assign a value to the property. </p> <p> When it comes to Information Hiding the Game class <em>hides the implementation details</em> by exposing a single Score property, but internally storing the state as two integers. It doesn't hide the state of the game, which is readily available via the Score property. </p> <p> The benefit of hiding the data and exposing the state as a property is that it enables you to vary the implementation independently from the public API. As a simple example, you may realize that you don't really need to keep the score in terms of 0, 15, 30, 40, etc. Actually, the scoring rules of a tennis game are very simple: in order to win, a player must win at least four points with at least two more points than the opponent. Once you realize this, you may decide to change the implementation to this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Game</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> player1Score; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> player2Score; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> PointTo(<span style="color: #2b91af;">Player</span> player) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (player == <span style="color: #2b91af;">Player</span>.One) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score += 1; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player2Score += 1; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Score &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player1Score == <span style="color: blue;">this</span>.player2Score &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score &gt;= 3) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Deuce"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player1Score &gt; 3 &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score == <span style="color: blue;">this</span>.player2Score + 1) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"AdvantagePlayerOne"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player2Score &gt; 3 &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player2Score == <span style="color: blue;">this</span>.player1Score + 1) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"AdvantagePlayerTwo"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player1Score &gt; 3 &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.player1Score &gt;= <span style="color: blue;">this</span>.player2Score + 2) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"GamePlayerOne"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.player2Score &gt; 3) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"GamePlayerTwo"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> score1Word = ToWord(<span style="color: blue;">this</span>.player1Score); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> score2Word = ToWord(<span style="color: blue;">this</span>.player2Score); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (score1Word == score2Word) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> score1Word + <span style="color: #a31515;">"All"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> score1Word + score2Word; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> ToWord(<span style="color: blue;">int</span> score) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">switch</span> (score) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> 0: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Love"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> 1: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Fifteen"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> 2: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Thirty"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> 3: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"Forty"</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">default</span>: &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentException</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"Unexpected score value."</span>, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"score"</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This is a true Refactoring because it modifies (actually simplifies) the internal implementation without changing the external API one bit. Good thing those integer fields were never publicly exposed. </p> <p> The tennis game scoring system is actually a <a href="http://en.wikipedia.org/wiki/Finite-state_machine">finite state machine</a> and from <a href="http://www.amazon.com/gp/product/0201633612/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0201633612&amp;linkCode=as2&amp;tag=ploeh-20">Design Patterns</a> we know that this can be effectively implemented using the <a href="http://en.wikipedia.org/wiki/State_pattern">State pattern</a>. Thus, a further refactoring could be to implement the Game class with a set of private or internal state classes. However, I will leave this as an exercise to the reader :) </p> <p> The progress towards the final score often seems to be an important aspect in sports reporting, so another possible future development of the Game class might be to enable it to not only report on its current state (as the Score property does), but also report on how it arrived at that state. This might prompt you to store the sequence of points as they were scored, and calculate past and current state based on the history of points. This would cause you to change the internal storage from two integers to a sequence of points scored. </p> <p> In both the above refactoring cases, you would be able to make the desired changes to the Game class because the implementation details are hidden. </p> <h3 id="8cfea4a423164328b7ce1cc5de7320d6"> Conclusion <a href="#8cfea4a423164328b7ce1cc5de7320d6" title="permalink">#</a> </h3> <p> Information Hiding isn't Data Hiding. <em>Implementation Hiding</em> would be a much better word. Properties expose <em>data</em> about the object while hiding the implementation details. This enables you to vary the implementation and the public API independently. Thus, the important benefit from Information Hiding is that it enables you to evolve your API in a backwards compatible fashion. It doesn't mean that you shouldn't expose any data at all, but since properties are just as much members of a class' public API as its methods, you must exercise the same care when designing properties as when designing methods.</p><p><b>Update (2012.12.02):</b> A reader correctly pointed out to me that there was a bug in the Game class. This bug has now been corrected.<br> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="32c4121159384ca6a550998ca3f83d90"> <div class="comment-author"><a href="http://whathecode.wordpress.com/">Steven Jeuris</a> <a href="#32c4121159384ca6a550998ca3f83d90">#</a></div> <div class="comment-content">I have read through several of your posts on encapsulation and information hiding and have also noticed throughout several discussions that information hiding is an often misunderstood/underestimated concept. Personally I think a lot of problems arise from ambiguous definitions, rather than the name &quot;information hiding&quot;. A particularly enlightening source for me was browsing through all the different definitions which have been formulated over the years, as compiled by Edward V. Berard.<br> <br> http://www.itmweb.com/essay550.htm<br> <br> In his conclusion he makes the following uncommon distinction: &quot;Abstraction, information hiding, and encapsulation are very different, but highly-related, concepts. One could argue that abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden. Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.&quot;<br> <br> Within a theoretical discussion, I agree with his conclusion that &quot;a stronger argument can be made for keeping the concepts, and thus the terms, distinct&quot;. A key thing to keep in mind here according to this definition encapsulating something doesn't necessarily mean it is hidden.<br> <br> When you refer to &quot;encapsulation&quot; you seem to refer to e.g. Rumbaugh's definition: &quot;Encapsulation (also information hiding) consists of separating the external aspects of an object which are accessible to other objects, from the internal implementation details of the object, which are hidden from other objects.&quot; who ironically doesn't seem to make a distinction between information hiding and encapsulation, highlighting the problem even more. This also corresponds to the first listed definition on the wikipedia article on encapsulation you link to, but not the second.<br> <br> 1. A language mechanism for restricting access to some of the object's components.<br> <br> 2. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.<br> <br> When hiding information we almost always (always?) encapsulate something, which is probably why many see them as the same thing.<br> <br> In a practical setting where we are just quickly trying to convey ideas things get even more problematic. When I refer to &quot;encapsulation&quot; I usually refer to information hiding, because that is generally my intention when encapsulating something. Interpreting it the other way around where encapsulation is seen solely as an OOP principle where state is placed within the context of a class with restricted access is more problematic when no distinction is made with information hiding. The many other ways in which information can be hidden are neglected.<br> <br> Keeping this warning in mind on the ambiguous definitions of encapsulation and information hiding, I wonder how you feel about my blog posts where I discuss information hiding beyond the scope of the class. To me this is a useful thing to do, for all the same reasons as information hiding in general is a good thing to do.<br> <br> In &quot;Improved encapsulation using lambdas&quot; I discuss how a piece of reusable code can be encapsulated within the scope of one function by using lambdas.<br> <br> http://whathecode.wordpress.com/2011/06/05/improved-encapsulation-using-lambdas/<br> <br> In &quot;Beyond private accessibility&quot; I discuss how variables used only within one function could potentially be hidden from other functions in a class.<br> <br> http://whathecode.wordpress.com/2011/06/13/beyond-private-accessibility/<br> <br> I'm glad I subscribed to this blog, you seem to talk about a lot of topics I'm truly interested in. :)</div> <div class="comment-date">2012-11-27 21:56 UTC</div> </div> <div class="comment" id="ed1688c6585340899843d385dd17c203"> <div class="comment-author"><a href="http://www.sapiensworks.com/blog">Mike</a> <a href="#ed1688c6585340899843d385dd17c203">#</a></div> <div class="comment-content">And speaking about encapsulation, I think that the score comparing logic should be ENCAPSULATED into its own PlayerScore class, something like [ https://gist.github.com/4159602 ](no link as the blog errors if i'm using html tags). So you can change anytime how the score is calculated and how many score points a tennis point really is. Also, it's testable and the Game object just manage where the points go, instead of also implementing scoring rules.</div> <div class="comment-date">2012-11-28 07:33 UTC</div> </div> <div class="comment" id="ee165b61b4e8463e8e62ea959ce301b1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ee165b61b4e8463e8e62ea959ce301b1">#</a></div> <div class="comment-content">Steven, thank you for your thoughtful comment. There are definitely many ways to think about encapsulation, and I don't think I have the only true definition. Basically I'm just trying to make sense of a lot of things that I know, from experience and intuition, are correct.<br> <br> The main purpose of this post is to drive a big stake through the notion that <em>properties = Encapsulation</em>.<br> <br> The lambda trick you describe is an additional way to scope a method - another is to realize that <a href="/2011/02/28/InterfacesAreAccessModifiers.aspx">interfaces can also be used for scoping</a>.</div> <div class="comment-date">2012-11-28 07:46 UTC</div> </div> <div class="comment" id="0255567a469740d3bdbc1abe063e253d"> <div class="comment-author"><a href="http://whathecode.wordpress.com/">Steven Jeuris</a> <a href="#0255567a469740d3bdbc1abe063e253d">#</a></div> <div class="comment-content">&quot;The main purpose of this post is to drive a big stake through the notion that properties = Encapsulation.&quot;<br> <br> While I agree 100% with the intent of that statement, I just wanted to warn you that according to some definitions, a property encapsulates a backing field, a getter, and a setter. Whether or not that getter and setter hide anything or are protected, is assumed by some to be an entirely different concept, namely information hiding.<br> <br> However using the 'mainstream' OOP definition of encapsulation you are absolutely right that a property with a public getter and setter (without any implementation logic inside) doesn't encapsulate anything as no access restrictions are imposed.<br> <br> And as per my cause, neither does moving something to the scope of the class with access restrictions provide the best possible information hiding in OOP. You are only hiding complexity from fellow programmers who will consume the class, but not for those who have to maintain it (or extend from it in the case of protected members).</div> <div class="comment-date">2012-11-28 09:39 UTC</div> </div> <div class="comment" id="2a3ae18bd284497d8e24bdb79bc5493e"> <div class="comment-author"><a href="http://whathecode.wordpress.com/">Steven Jeuris</a> <a href="#2a3ae18bd284497d8e24bdb79bc5493e">#</a></div> <div class="comment-content">@Mike<br> <br> You are probably right his example can be further encapsulated, but if you are taking the effort of encapsulating something I would at least try to make it reusable instead of making a specific 'PlayerScore' class.<br> <br> A score is basically a range of values which is a construct which is missing from C# and Java, but e.g. Ruby has. From experience I can tell you implementing it yourself is truly useful as you start seeing opportunities to use it everywhere: https://github.com/Whathecode/Framework-Class-Library-Extension/blob/master/Whathecode.System/Arithmetic/Range/Interval.cs<br> <br> Probably I would create a generic or abstract 'Score' class which is composed of a range and possibly some additional logic (Reset(), events, ...)<br> <br> As to moving the logic of the game (the score comparison) to the 'PlayerScore' class, I don't think I entirely agree. This is the concern of the 'Game' class and not the 'Score'. When separating these concerns one could use the 'Score' class in different types of games as well.</div> <div class="comment-date">2012-11-28 10:00 UTC</div> </div> <div class="comment" id="76ef3a817cee428ab55a688af4f4783f"> <div class="comment-author"><a href="http://www.skov-boisen.dk">Simon Skov Boisen</a> <a href="#76ef3a817cee428ab55a688af4f4783f">#</a></div> <div class="comment-content">@Steven Once in a while I use delegates, mainly Funcs to encapsulate utility functions inside private functions to get that additional encapsulation-level. <br> <br> However I feel that it's just not a very good fit in C# compared to functional languages like F# or Clojure where functions are truely first class and defining a function inside another function is fully supported with the same syntax as regular functions. What I really would like if C# simply would let you define functions inside functions with the usual syntax. <br> <br> Regarding performance I don't know how C# does it but I imagine it's similar to the F# inner function performance hit is negligible: http://stackoverflow.com/questions/7920234/what-are-the-performance-side-effects-of-defining-functions-inside-a-recursive-f<br> <br> Maybe it's better to encapsulate the private func as a public func in a nested private (static?) class and then have the inner function as a private function of that class.</div> <div class="comment-date">2012-11-29 05:59 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AppSettings convention for Castle Windsor https://blog.ploeh.dk/2012/11/07/AppSettingsconventionforCastleWindsor 2012-11-07T16:54:43+00:00 Mark Seemann <div id="post"> <p> <em>This post describes a generalized convention for Castle Windsor that handles AppSettings primitives.</em> </p> <p> In <a href="/2012/11/06/WhenToUseADIContainer.aspx">my previous post</a> I explained how Convention over Configuration is the preferred way to use a DI Container. Some readers asked to see some actual convention implementations (although I actually <a href="https://github.com/ploeh/Booking">linked to them</a> in the post). In fact, I've <a href="/2012/07/02/PrimitiveDependencies.aspx">previously showcased some simple conventions expressed with Castle Windsor's API.</a>. In this post I'm going to show you another convention, which is completely reusable. Feel free to copy and paste :) </p> <p> Most conventions are really easy to implement. Actually, sometimes it takes more effort to express the specification than it actually takes to implement it. </p> <p> This convention deals with <a href="/2012/07/02/PrimitiveDependencies.aspx">Primitive Dependencies</a>. In my original post on the topic I included an AppSettingsConvention class as part of the code listing, but that implementation was hard-coded to only deal with integers. This narrow convention can be generalized: </p> <blockquote> <p> The AppSettingsConvention should map AppSettings .config values into Primitive Dependencies. </p> <p> <ul> <li> If a class has a dependency, the name of the dependency is assumed to be the name of the constructor argument (or property, for that matter). If, for example, the name of a constructor argument is <em>top</em>, this is the name of the dependency. </li> <li> If there's an <em>appSettings</em> key with the same name in the .config, and if there's a known conversion from string to the type of the dependency, the .config value is converted and used. </li> </ul> </p> </blockquote> <h3 id="a12393778ce04dfa935a08da385847d8"> Example requirement: int top <a href="#a12393778ce04dfa935a08da385847d8" title="permalink">#</a> </h3> <p> Consider this constructor: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> DbChartReader(<span style="color: blue;">int</span> top, <span style="color: blue;">string</span> chartConnectionString)</pre> </p> <p> In this case the convention should look after an AppSettings key named <em>top</em> as well as check whether there's a known conversion from string to int (there is). Imagine that the .config file contains this XML fragment: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">appSettings</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">add</span><span style="color: blue;"> </span><span style="color: red;">key</span><span style="color: blue;">=</span>"<span style="color: blue;">top</span>"<span style="color: blue;"> </span><span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">40</span>"<span style="color: blue;"> /&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">appSettings</span><span style="color: blue;">&gt;</span></pre> </p> <p> The convention should read "40" from the .config file and convert it to an integer and inject 40 into a DbChartReader instance. </p> <h3 id="6cc094c201a346f084aa50a138f3263c"> Example requirement: Uri catalogTrackBaseUri <a href="#6cc094c201a346f084aa50a138f3263c" title="permalink">#</a> </h3> <p> Consider this constructor: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> CatalogApiTrackLinkFactory(<span style="color: #2b91af;">Uri</span> catalogTrackBaseUri)</pre> </p> <p> In this case the convention should look after an AppSettings key named <em>catalogTrackBaseUri</em> and check if there's a known conversion from string to Uri. Imagine that the .config file contains this XML fragment: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">appSettings</span><span style="color: blue;">&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">add</span><span style="color: blue;"> </span><span style="color: red;">key</span><span style="color: blue;">=</span>"<span style="color: blue;">catalogTrackBaseUri</span>"<span style="color: blue;"> </span><span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">http://www.ploeh.dk/foo/img/</span>"<span style="color: blue;">/&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">add</span><span style="color: blue;"> </span><span style="color: red;">key</span><span style="color: blue;">=</span>"<span style="color: blue;">foo</span>"<span style="color: blue;"> </span><span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">bar</span>"<span style="color: blue;">/&gt;</span> <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">add</span><span style="color: blue;"> </span><span style="color: red;">key</span><span style="color: blue;">=</span>"<span style="color: blue;">baz</span>"<span style="color: blue;"> </span><span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">42</span>"<span style="color: blue;">/&gt;</span> <span style="color: blue;">&lt;/</span><span style="color: #a31515;">appSettings</span><span style="color: blue;">&gt;</span></pre> </p> <p> The convention should read "http://www.ploeh.dk/foo/img/" from the .config file and convert it to a Uri instance. </p> <h3 id="3e740762c5df4534956aeb1f8aa31782"> Implementation <a href="#3e740762c5df4534956aeb1f8aa31782" title="permalink">#</a> </h3> <p> By now it should be clear what the conventions should do. With <a href="http://docs.castleproject.org/Windsor.MainPage.ashx">Castle Windsor</a> this is easily done by implementing an ISubDependencyResolver. Each method is a one-liner: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">AppSettingsConvention</span> : <span style="color: #2b91af;">ISubDependencyResolver</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> CanResolve( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CreationContext</span> context, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ISubDependencyResolver</span> contextHandlerResolver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentModel</span> model, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DependencyModel</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">ConfigurationManager</span>.AppSettings.AllKeys &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Contains(dependency.DependencyKey) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; <span style="color: #2b91af;">TypeDescriptor</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .GetConverter(dependency.TargetType) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .CanConvertFrom(<span style="color: blue;">typeof</span>(<span style="color: blue;">string</span>)); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> Resolve( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CreationContext</span> context, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ISubDependencyResolver</span> contextHandlerResolver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentModel</span> model, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DependencyModel</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">TypeDescriptor</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .GetConverter(dependency.TargetType) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .ConvertFrom( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ConfigurationManager</span>.AppSettings[dependency.DependencyKey]); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The ISubDependencyResolver interface is an example of the <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms229009.aspx">Tester-Doer pattern</a>. Only if the CanResolve method returns true is the Resolve method invoked. </p> <p> The CanResolve method performs two checks: <ul> <li>Is there an AppSettings key in the configuration which is equal to the name of the dependency?</li> <li>Is there a known conversion from string to the type of the dependency?</li> </ul> If both answers are true, then the CanResolve method returns true. </p> <p> The Resolve method simply reads the .config value and converts it to the appropriate type and returns it. </p> <p> Adding the convention to an IWindsorContainer instance is easy: </p> <p> <pre style="margin: 0px;">container.Kernel.Resolver.AddSubResolver( &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AppSettingsConvention</span>());&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> </p> <h3 id="c63cd302e8a14ddd839efe46688c6411"> Summary <a href="#c63cd302e8a14ddd839efe46688c6411" title="permalink">#</a> </h3> <p> The AppSettingsConvention is a completely reusable convention for Castle Windsor. With it, Primitive Dependencies are automatically wired the appropriate .config values if they are defined. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ff939d4f0ac841ce8fee74f132a36d91"> <div class="comment-author"><a href="http://kozmic.pl">Krzysztof Kozmic</a> <a href="#ff939d4f0ac841ce8fee74f132a36d91">#</a></div> <div class="comment-content">Actually IComponentModelContributor would be an even better place to put the logic than ISDR.<br> - it would handle all the type conversion for you<br> - the approach, since the dependency is set up as part of the ComponentModel is statically analysable, whereas ISDR works dynamically so your components that depend on values from config file would show up as &quot;Potentially misconfigured components&quot;.</div> <div class="comment-date">2012-11-07 21:19 UTC</div> </div> <div class="comment" id="5f1781fed00645c99677a19cf41401e7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5f1781fed00645c99677a19cf41401e7">#</a></div> <div class="comment-content">Krzysztof, if I try to implement an interface called &quot;IComponentModelContributor&quot; IntelliSense gives me nothing. Where is that interface defined? (I'm drawing a blank on Google too...)</div> <div class="comment-date">2012-11-07 21:29 UTC</div> </div> <div class="comment" id="c17882838e144068beef8a4ea6b3f8fb"> <div class="comment-author"><a href="http://kozmic.pl">Krzysztof Kozmic</a> <a href="#c17882838e144068beef8a4ea6b3f8fb">#</a></div> <div class="comment-content">a@http://docs.castleproject.org/Windsor.ComponentModel-construction-contributors.ashx@this thing</div> <div class="comment-date">2012-11-07 21:31 UTC</div> </div> <div class="comment" id="87d7f15041384621b1c7cd73997ca5fa"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#87d7f15041384621b1c7cd73997ca5fa">#</a></div> <div class="comment-content">How would you implement the above convention with that interface?</div> <div class="comment-date">2012-11-07 21:49 UTC</div> </div> <div class="comment" id="924a18be92194cdaa8424a5df27d4b77"> <div class="comment-author"><a href="http://kozmic.pl">Krzysztof Kozmic</a> <a href="#924a18be92194cdaa8424a5df27d4b77">#</a></div> <div class="comment-content">I guess that means a blogpost :)</div> <div class="comment-date">2012-11-07 21:51 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. When to use a DI Container https://blog.ploeh.dk/2012/11/06/WhentouseaDIContainer 2012-11-06T11:42:06+00:00 Mark Seemann <div id="post"> <p> <em>This post explains why a DI Container is useful with Convention over Configuration while Poor Man's DI might be a better fit for a more explicit Composition Root.</em> </p> <p> <strong>Note</strong> (2018-07-18): Since I wrote this article, I've <a href="/2014/06/10/pure-di">retired the term <em>Poor Man's DI</em> in favour of <em>Pure DI</em></a>. </p> <p> It seems to me that lately there's been a backlash against DI Containers among alpha geeks. Many of the software leaders that I myself learn from seem to dismiss the entire concept of a DI Container, claiming that it's too complex, too 'magical', that it isn't a good architectural pattern, or that the derived value doesn't warrant the 'cost' (most, if not all, DI Containers are open source, so they are free in a monetary sense, but there's always a cost in learning curve etc.). </p> <p> This must have caused <a href="http://kozmic.pl">Krzysztof Koźmic</a> to write <a href="http://kozmic.pl/2012/10/23/ioc-container-solves-a-problem-you-might-not-have-but-its-a-nice-problem-to-have/">a nice article about what sort of problem a DI Container solves</a>. I agree with the article, but want to provide a different perspective here. </p> <p> In short, it makes sense to me to illustrate the tradeoffs of Poor Man's DI versus DI Containers in a diagram like this:</p> <p><img src="/content/binary/usefulness-sophistication.png" alt="usefulness vs. sophistication" border="0" height="454" width="661"></p> <p> The point of the diagram is that Poor Man's DI can be valuable because it's simple, while a DI Container can be either valuable or pointless depending on how it's used. However, when used in a sufficiently sophisticated way I consider a DI Container to offer the best value/cost ratio. When people criticize DI Containers as being pointless I suspect that what really happened was that they gave up before they were out of the <a href="http://en.wikipedia.org/wiki/Hype_cycle">Trough of Disillusionment</a>. Had they continued to learn, they might have arrived at a new Plateau of Productivity. </p> <p> <table border="1"> <thead> <tr align="left" valign="top"> <th>DI style</th> <th>Advantages</th> <th>Disadvantages</th> </tr> </thead> <tbody> <tr align="left" valign="top"> <td>Poor Man's DI</td> <td> <ul> <li>Easy to learn</li> <li>Strongly typed</li> </ul> </td> <td> <ul> <li>High maintenance</li> </ul> </td> </tr> <tr align="left" valign="top"> <td>Explicit Register</td> <td> <br></td> <td> <ul> <li>Weakly typed</li> </ul> </td> </tr> <tr align="left" valign="top"> <td>Convention over Configuration</td> <td> <ul> <li>Low maintenance</li> </ul> </td> <td> <ul> <li>Hard to learn</li> <li>Weakly typed</li> </ul> </td> </tr> </tbody> </table> </p> <p> There are other, less important advantages and disadvantages of each approach, but here I'm focusing on three main axes that I consider important: </p> <p> <ul> <li>How easy is it to understand and learn?</li> <li>How soon will you get feedback if something is not right?</li> <li>How easy is it to maintain?</li> </ul> </p> <p> The major advantage of Poor Man's DI is that it's easy to learn. You don't have to learn the API of any DI Container (Unity, Autofac, Ninject, StructureMap, Castle Windsor, etc.) and while individual classes still use DI, once you find the <a href="/2011/07/28/CompositionRoot.aspx">Composition Root</a> it'll be evident what's going on and how object graphs are constructed. No 'magic' is involved. </p> <p> The second big advantage of Poor Man's DI is often overlooked: it's strongly typed. This is an advantage because <a href="/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx">it provides the fastest feedback about correctness that you can get</a>. However, strong typing cuts both ways because it also means that every time you refactor a constructor, you will break the Composition Root. If you are sharing a library (Domain Model, Utility, Data Access component, etc.) between more than one application (unit of deployment), you may have more than one Composition Root to maintain. How much of a burden this is depends on how often you refactor constructors, but I've seen projects where this happens several times each day (keep in mind that <a href="/2011/02/28/InterfacesAreAccessModifiers.aspx">constructor are implementation details</a>). </p> <p> If you use a DI Container, but explicitly Register each and every component using the container's API, you lose the rapid feedback from strong typing. On the other hand, the maintenance burden is also likely to drop because of <em>Auto-wiring</em>. Still, you'll need to register each new class or interface when you introduce them, and you (and your team) still has to learn the specific API of that container. In my opinion, you lose more advantages than you gain. </p> <p> Ultimately, if you can wield a DI Container in a sufficiently sophisticated way, you can use it to define a set of conventions. These conventions define a rule set that your code should adhere to, and as long as you stick to those rules, <em>things just work</em>. The container drops to the background, and you rarely need to touch it. Yes, this is hard to learn, and is still weakly typed, but if done right, it enables you to focus on code that adds value instead of infrastructure. An additional advantage is that it creates a positive feedback mechanism forcing a team to produce code that is consistent with the conventions. </p> <h3 id="0c19d443c1ad4fd6a4c7883dcc9abb3c"> Example: Poor Man's DI <a href="#0c19d443c1ad4fd6a4c7883dcc9abb3c" title="permalink">#</a> </h3> <p> The following example is part of my <a href="https://github.com/ploeh/Booking">Booking sample application</a>. It shows <a href="https://github.com/ploeh/Booking/blob/64b7b670fff9560d8947dd133ae54779d867a451/BookingDaemon/Program.cs">the state of the Ploeh.Samples.Booking.Daemon.Program class as it looks in the git tag <em>total-complexity</em> (git commit ID 64b7b670fff9560d8947dd133ae54779d867a451)</a>. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> queueDirectory = &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\Queue"</span>).CreateIfAbsent(); <span style="color: blue;">var</span> singleSourceOfTruthDirectory = &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\SSoT"</span>).CreateIfAbsent(); <span style="color: blue;">var</span> viewStoreDirectory = &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\ViewStore"</span>).CreateIfAbsent(); &nbsp; <span style="color: blue;">var</span> extension = <span style="color: #a31515;">"txt"</span>; &nbsp; <span style="color: blue;">var</span> fileDateStore = <span style="color: blue;">new</span> <span style="color: #2b91af;">FileDateStore</span>( &nbsp;&nbsp;&nbsp; singleSourceOfTruthDirectory, &nbsp;&nbsp;&nbsp; extension); &nbsp; <span style="color: blue;">var</span> quickenings = <span style="color: blue;">new</span> <span style="color: #2b91af;">IQuickening</span>[] { &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">RequestReservationCommand</span>.<span style="color: #2b91af;">Quickening</span>(), &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">ReservationAcceptedEvent</span>.<span style="color: #2b91af;">Quickening</span>(), &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">ReservationRejectedEvent</span>.<span style="color: #2b91af;">Quickening</span>(), &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">CapacityReservedEvent</span>.<span style="color: #2b91af;">Quickening</span>(), &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">SoldOutEvent</span>.<span style="color: #2b91af;">Quickening</span>() }; &nbsp; <span style="color: blue;">var</span> disposable = <span style="color: blue;">new</span> <span style="color: #2b91af;">CompositeDisposable</span>(); <span style="color: blue;">var</span> messageDispatcher = <span style="color: blue;">new</span> <span style="color: #2b91af;">Subject</span>&lt;<span style="color: blue;">object</span>&gt;(); disposable.Add( &nbsp;&nbsp;&nbsp; messageDispatcher.Subscribe( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">Dispatcher</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">CapacityGate</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonCapacityRepository</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fileDateStore, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fileDateStore, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; quickenings), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonChannel</span>&lt;<span style="color: #2b91af;">ReservationAcceptedEvent</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">FileQueueWriter</span>&lt;<span style="color: #2b91af;">ReservationAcceptedEvent</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; queueDirectory, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; extension)), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonChannel</span>&lt;<span style="color: #2b91af;">ReservationRejectedEvent</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">FileQueueWriter</span>&lt;<span style="color: #2b91af;">ReservationRejectedEvent</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; queueDirectory, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; extension)), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonChannel</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">FileQueueWriter</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; queueDirectory, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; extension)))))); disposable.Add( &nbsp;&nbsp;&nbsp; messageDispatcher.Subscribe( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">Dispatcher</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MonthViewUpdater</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">FileMonthViewStore</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; viewStoreDirectory, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; extension))))); &nbsp; <span style="color: blue;">var</span> q = <span style="color: blue;">new</span> <span style="color: #2b91af;">QueueConsumer</span>( &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">FileQueue</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; queueDirectory, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; extension), &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonStreamObserver</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; quickenings, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; messageDispatcher)); &nbsp; RunUntilStopped(q);</pre> </p> <p> Yes, that's a lot of code. I deliberately chose a non-trivial example to highlight just how much stuff there might be. You don't have to read and understand all of this code to appreciate that it might require a bit of maintenance. It's a big object graph, with some shared subgraphs, and since it uses the <em>new</em> keyword to create all the objects, every time you change a constructor signature, you'll need to update this code, because it's not going to compile until you do. </p> <p> Still, there's no 'magical' tool (read: DI Container) involved, so it's pretty easy to understand what's going on here. As <a href="http://dannorth.net/">Dan North</a> put it once I saw him endorse this technique: <em>'new' is the new 'new'</em> :) Once you see how Explicit Register looks, you may appreciate why. </p> <h3 id="1bf4c3cc11564bccb1b79faa67cdd47b"> Example: Explicit Register <a href="#1bf4c3cc11564bccb1b79faa67cdd47b" title="permalink">#</a> </h3> <p> The following example performs exactly the same work as the previous example, but now in a <a href="https://github.com/ploeh/Booking/blob/13fc576b729cdddd5ec53f1db907ec0a7d00836b/BookingDaemon/DaemonWindsorInstaller.cs">state (git tag: controllers-by-convention; commit ID: 13fc576b729cdddd5ec53f1db907ec0a7d00836b)</a> where it's being wired by <a href="http://docs.castleproject.org/Windsor.MainPage.ashx">Castle Windsor</a>. The name of this class is DaemonWindsorInstaller, and all components are explictly registered. Hang on to something. </p> <p> <pre style="margin: 0px;">container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">DirectoryInfo</span>&gt;() &nbsp;&nbsp;&nbsp; .UsingFactoryMethod(() =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\Queue"</span>).CreateIfAbsent()) &nbsp;&nbsp;&nbsp; .Named(<span style="color: #a31515;">"queueDirectory"</span>)); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">DirectoryInfo</span>&gt;() &nbsp;&nbsp;&nbsp; .UsingFactoryMethod(() =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\SSoT"</span>).CreateIfAbsent()) &nbsp;&nbsp;&nbsp; .Named(<span style="color: #a31515;">"ssotDirectory"</span>)); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">DirectoryInfo</span>&gt;() &nbsp;&nbsp;&nbsp; .UsingFactoryMethod(() =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\ViewStore"</span>).CreateIfAbsent()) &nbsp;&nbsp;&nbsp; .Named(<span style="color: #a31515;">"viewStoreDirectory"</span>));&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IQueue</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">FileQueue</span>&gt;() &nbsp;&nbsp;&nbsp; .DependsOn( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnComponent(<span style="color: #a31515;">"directory"</span>, <span style="color: #a31515;">"queueDirectory"</span>), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnValue(<span style="color: #a31515;">"extension"</span>, <span style="color: #a31515;">"txt"</span>))); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IStoreWriter</span>&lt;<span style="color: #2b91af;">DateTime</span>&gt;, <span style="color: #2b91af;">IStoreReader</span>&lt;<span style="color: #2b91af;">DateTime</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">FileDateStore</span>&gt;() &nbsp;&nbsp;&nbsp; .DependsOn( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnComponent(<span style="color: #a31515;">"directory"</span>, <span style="color: #a31515;">"ssotDirectory"</span>), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnValue(<span style="color: #a31515;">"extension"</span>, <span style="color: #a31515;">"txt"</span>))); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IStoreWriter</span>&lt;<span style="color: #2b91af;">ReservationAcceptedEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">FileQueueWriter</span>&lt;<span style="color: #2b91af;">ReservationAcceptedEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .DependsOn( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnComponent(<span style="color: #a31515;">"directory"</span>, <span style="color: #a31515;">"queueDirectory"</span>), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnValue(<span style="color: #a31515;">"extension"</span>, <span style="color: #a31515;">"txt"</span>))); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IStoreWriter</span>&lt;<span style="color: #2b91af;">ReservationRejectedEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">FileQueueWriter</span>&lt;<span style="color: #2b91af;">ReservationRejectedEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .DependsOn( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnComponent(<span style="color: #a31515;">"directory"</span>, <span style="color: #a31515;">"queueDirectory"</span>), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnValue(<span style="color: #a31515;">"extension"</span>, <span style="color: #a31515;">"txt"</span>))); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IStoreWriter</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">FileQueueWriter</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .DependsOn( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnComponent(<span style="color: #a31515;">"directory"</span>, <span style="color: #a31515;">"queueDirectory"</span>), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnValue(<span style="color: #a31515;">"extension"</span>, <span style="color: #a31515;">"txt"</span>))); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">ReservationAcceptedEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">JsonChannel</span>&lt;<span style="color: #2b91af;">ReservationAcceptedEvent</span>&gt;&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">ReservationRejectedEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">JsonChannel</span>&lt;<span style="color: #2b91af;">ReservationRejectedEvent</span>&gt;&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">JsonChannel</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;&gt;()); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">ICapacityRepository</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">JsonCapacityRepository</span>&gt;()); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IConsumer</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">CapacityGate</span>&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IConsumer</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">MonthViewUpdater</span>&gt;()); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">Dispatcher</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">Dispatcher</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;&gt;()); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">Stream</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">JsonStreamObserver</span>&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IObserver</span>&lt;<span style="color: #2b91af;">DateTime</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">FileMonthViewStore</span>&gt;() &nbsp;&nbsp;&nbsp; .DependsOn( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnComponent(<span style="color: #a31515;">"directory"</span>, <span style="color: #a31515;">"viewStoreDirectory"</span>), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnValue(<span style="color: #a31515;">"extension"</span>, <span style="color: #a31515;">"txt"</span>))); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IObserver</span>&lt;<span style="color: blue;">object</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .UsingFactoryMethod(k =&gt; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> messageDispatcher = <span style="color: blue;">new</span> <span style="color: #2b91af;">Subject</span>&lt;<span style="color: blue;">object</span>&gt;(); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; messageDispatcher.Subscribe(k.Resolve&lt;<span style="color: #2b91af;">Dispatcher</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;&gt;()); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; messageDispatcher.Subscribe(k.Resolve&lt;<span style="color: #2b91af;">Dispatcher</span>&lt;<span style="color: #2b91af;">SoldOutEvent</span>&gt;&gt;()); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> messageDispatcher; &nbsp;&nbsp;&nbsp; })); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IQuickening</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">RequestReservationCommand</span>.<span style="color: #2b91af;">Quickening</span>&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IQuickening</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">ReservationAcceptedEvent</span>.<span style="color: #2b91af;">Quickening</span>&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IQuickening</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">ReservationRejectedEvent</span>.<span style="color: #2b91af;">Quickening</span>&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IQuickening</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">CapacityReservedEvent</span>.<span style="color: #2b91af;">Quickening</span>&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IQuickening</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">SoldOutEvent</span>.<span style="color: #2b91af;">Quickening</span>&gt;()); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">QueueConsumer</span>&gt;()); &nbsp; container.Kernel.Resolver.AddSubResolver(<span style="color: blue;">new</span> <span style="color: #2b91af;">CollectionResolver</span>(container.Kernel));</pre> </p> <p> This is actually more verbose than before - almost double the size of the Poor Man's DI example. To add spite to injury, this is no longer strongly typed in the sense that you'll no longer get any compiler errors if you change something, but a change to your classes can easily lead to a runtime exception, since something may not be correctly configured. </p> <p> This example uses the Registration API of Castle Windsor, but imagine the horror if you were to use XML configuration instead. </p> <p> Other DI Containers have similar Registration APIs (apart from those that <em>only</em> support XML), so this problem isn't isolated to Castle Windsor only. It's inherent in the Explicit Register style. </p> <p> I can't claim to be an expert in Java, but all I've ever heard and seen of DI Containers in Java (Spring, Guice, Pico), they don't seem to have Registration APIs much more sophisticated than that. In fact, many of them still seem to be heavily focused on XML Registration. If that's the case, it's no wonder many software thought leaders (like Dan North with his <em>'new' is the new 'new'</em> line) dismiss DI Containers as being essentially pointless. If there weren't a more sophisticated option, I would tend to agree. </p> <h3 id="18bf234f55a7492cb9ef1bc87a3b9742"> Example: Convention over Configuration <a href="#18bf234f55a7492cb9ef1bc87a3b9742" title="permalink">#</a> </h3> <p> This is still the same example as before, but now <a href="https://github.com/ploeh/Booking/blob/0a7e6f246cacdbefc8f6933fc84b024774d02038/BookingDaemon/DaemonWindsorInstaller.cs">in a state (git tag: services-by-convention-in-daemon; git commit ID: 0a7e6f246cacdbefc8f6933fc84b024774d02038)</a> where almost the entire configuration is done by convention. </p> <p> <pre style="margin: 0px;">container.AddFacility&lt;<span style="color: #2b91af;">ConsumerConvention</span>&gt;(); &nbsp; container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IObserver</span>&lt;<span style="color: blue;">object</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">CompositeObserver</span>&lt;<span style="color: blue;">object</span>&gt;&gt;()); &nbsp; container.Register(<span style="color: #2b91af;">Classes</span> &nbsp;&nbsp;&nbsp; .FromAssemblyInDirectory(<span style="color: blue;">new</span> <span style="color: #2b91af;">AssemblyFilter</span>(<span style="color: #a31515;">"."</span>).FilterByName(an =&gt; an.Name.StartsWith(<span style="color: #a31515;">"Ploeh.Samples.Booking"</span>))) &nbsp;&nbsp;&nbsp; .Where(t =&gt; !(t.IsGenericType &amp;&amp; t.GetGenericTypeDefinition() == <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Dispatcher</span>&lt;&gt;))) &nbsp;&nbsp;&nbsp; .WithServiceAllInterfaces()); &nbsp; container.Kernel.Resolver.AddSubResolver(<span style="color: blue;">new</span> <span style="color: #2b91af;">ExtensionConvention</span>()); container.Kernel.Resolver.AddSubResolver(<span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryConvention</span>(container.Kernel)); container.Kernel.Resolver.AddSubResolver(<span style="color: blue;">new</span> <span style="color: #2b91af;">CollectionResolver</span>(container.Kernel)); &nbsp; <span style="color: blue;">#region</span> Manual configuration that requires maintenance container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">DirectoryInfo</span>&gt;() &nbsp;&nbsp;&nbsp; .UsingFactoryMethod(() =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\Queue"</span>).CreateIfAbsent()) &nbsp;&nbsp;&nbsp; .Named(<span style="color: #a31515;">"queueDirectory"</span>)); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">DirectoryInfo</span>&gt;() &nbsp;&nbsp;&nbsp; .UsingFactoryMethod(() =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\SSoT"</span>).CreateIfAbsent()) &nbsp;&nbsp;&nbsp; .Named(<span style="color: #a31515;">"ssotDirectory"</span>)); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">DirectoryInfo</span>&gt;() &nbsp;&nbsp;&nbsp; .UsingFactoryMethod(() =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">@"..\..\..\BookingWebUI\ViewStore"</span>).CreateIfAbsent()) &nbsp;&nbsp;&nbsp; .Named(<span style="color: #a31515;">"viewStoreDirectory"</span>)); <span style="color: blue;">#endregion</span></pre> </p> <p> It's pretty clear that this is a lot less verbose - and then I even left three explicit Register statements as a deliberate decision. Just because you decide to use Convention over Configuration doesn't mean that you have to stick to this principle 100 %. </p> <p> Compared to the previous example, this requires a lot less maintenance. While you are working with this code base, most of the time you can concentrate on adding new functionality to the software, and the conventions are just going to pick up your changes and new classes and interfaces. Personally, this is where I find the best tradeoff between the value provided by a DI Container versus the cost of figuring out how to implement the conventions. You should also keep in mind that once you've learned to use a particular DI Container like this, the cost goes down. </p> <h3 id="537bb32ea3b44263a98fcb039465aa5f"> Summary <a href="#537bb32ea3b44263a98fcb039465aa5f" title="permalink">#</a> </h3> <p> Using a DI Container to compose object graphs by convention presents an unparalled opportunity to push infrastructure code to the background. However, if you're not prepared to go all the way, Poor Man's DI may actually be a better option. Don't use a DI Container just to use one. Understand the value and cost associated with it, and always keep in mind that Poor Man's DI is a valid alternative. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c78e679fcb5c49b6adeb59b2f4af5381"> <div class="comment-author"><a href="http://www.cauthon.com">Darren Cauthon</a> <a href="#c78e679fcb5c49b6adeb59b2f4af5381">#</a></div> <div class="comment-content">I'd offer a different suggestion: Try writing a test for every line of code you write. Then tell me how Poor Man's DI works out for you.<br> <br> Testing usually seems to be the pressure release for not using an IoC container in a static language. Even if it's just a matter of not writing the tests for the overridden constructor, testing is usually thrown out. And in a world without tests, Poor Man's DI (or not DI at all) is often the &quot;simpler&quot; solution. Less lines of code, &quot;it just works,&quot; etc etc. There are lots of options when you only look at the implementation without concern about how one is to provide automated verification against regressions.<br> <br> If using TDD or even just &quot;testing,&quot; an IoC container is always the simpler solution. Unless, of course -- if you just switch to a language or framework that lets you do both. *cough* ruby *cough* python *cough* dynamic languages *cough*</div> <div class="comment-date">2012-11-06 17:00 UTC</div> </div> <div class="comment" id="e76b311de6674a339cac7083095c74fa"> <div class="comment-author">Daniel Hilgarth <a href="#e76b311de6674a339cac7083095c74fa">#</a></div> <div class="comment-content">Very good article, thank you!<br> <br> <br> Just to add another perspective:<br> <br> I created some extension methods in one of my core libraries that registers everything that ends with Service, Factory, Provider etc. Additionally, I created some extension methods for special areas like NHibernate or AutoMapping.<br> <br> With these extension methods and a project that adheres to these conventions, my composition roots are very short and need virtually no maintenance.<br> <br> I have successfully used this approach in several mid to big sized projects. It just works, I wouldn't want to work without a DI container anymore as it would cost so much more time.</div> <div class="comment-date">2012-11-06 19:53 UTC</div> </div> <div class="comment" id="a1031875ea2049dfa8c78f6bd2428f69"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a1031875ea2049dfa8c78f6bd2428f69">#</a></div> <div class="comment-content">Darren, thank you for your comment. How do you think TDD fits into this discussion? Which overloaded constructors? If you examine the git repo behind these samples, you should find that I didn't change the production code between the three examples. It's the same production code - the same classes, the same constructors, etc. - just wired in three different ways.</div> <div class="comment-date">2012-11-06 20:52 UTC</div> </div> <div class="comment" id="39980ec8761a4fa89b7dd752fbda1cdd"> <div class="comment-author"><a href="http://www.cauthon.com">Darren Cauthon</a> <a href="#39980ec8761a4fa89b7dd752fbda1cdd">#</a></div> <div class="comment-content">Every time I've heard of Poor Man's DI, it's always described the practice of creating two constructors: <br> <br> 1.) A constructor with no arguments. Dependencies are initialized in the constructor. This constructor can be used to instantiate the object like &quot;myThing = new MyThing();&quot; This is not using DI at all. <br> <br> 2.) A constructor with arguments for each dependency. Dependencies are passed in. This constructor is used for testing, since it actually uses DI.<br> <br> This &quot;Poor Man's DI&quot; is a concept because it's a cheap way to get DI into a class that may not have originally been written to support DI. In a way, it seems to give devs the best of both worlds. Users can still instantiate the class simply, but users can also test it. It sounds fine, but it has some issues because the class is still bound to its dependencies and because the implementation uses different code than the tests.<br> <br> Looking deeper at your example, I see that's not what your &quot;Poor Man's DI&quot; example is. Your way is fully testable, but I don't think its deserving of the extra &quot;Poor Man's DI&quot; moniker because it's just hand-rolled class instantiation. Or to put it another way: If your code is an example of &quot;Poor Man's DI,&quot; then wouldn't any DI that wasn't handled through an IoC container? You are just creating objects with code -- nothing special. (or wrong, either)<br> <br> If that's what &quot;Poor Man's DI&quot; means, there should probably be a new phrase to identify the practice that I've seen the phrase tied to -- as it's a &quot;special&quot; and unique practice. (Take that however you will. :) )</div> <div class="comment-date">2012-11-07 01:02 UTC</div> </div> <div class="comment" id="bc837668fa774099b29ed33ae301e151"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bc837668fa774099b29ed33ae301e151">#</a></div> <div class="comment-content">Darren, I'll refer you to <a href="http://stackoverflow.com/a/7102651/126014">this answer</a> for further details on the terminology choice. You do have a point, but I'm sticking to the terminology from <a href="http://amzn.to/12p90MG">my book</a>.</div> <div class="comment-date">2012-11-07 08:17 UTC</div> </div> <div class="comment" id="f498d27db66841a79155e9a825f51509"> <div class="comment-author"><a href="http://www.cauthon.com">Darren Cauthon</a> <a href="#f498d27db66841a79155e9a825f51509">#</a></div> <div class="comment-content">I guess people can call things whatever they want. I've seen &amp; heard many references to the two-constructor pattern as Poor Man's DI and for a long time, but this is the first time I've seen the phrase used in your way. It's also the basic first time I've seen basic class instantiation given a special name. <br> <br> Now that I think about it,the concept of &quot;Poor Man's DI&quot; and &quot;Bastard Injection&quot; seem to refer to different things. Given your definition, Poor Man's DI basically seems to mean that I don't use an IoC container. It's a concept defining the method in which the object is created. But Bastard Injection refers to what I think would be the more common use of &quot;Poor Man's DI,&quot; the practice of creating two constructors. It's a concept defining the method in which the class itself is written. I guess, then, it's possible for me to use Bastard Injection with Poor Man's DI, so long as I don't call the default constructor? <br> <br> As one more side note: I really don't like the name &quot;Bastard Injection&quot; due to the coarse language. I know it's an anti-pattern, but &quot;bastard&quot; is a word I'd never ever accept from myself or other developers in a professional setting, especially with a client. I just asked my wife, an public elementary school teacher and librarian, and she said that word would not be accepted in her class or at any school she's been at. I don't think it's helpful to give PG13-level words to programming concepts. :)</div> <div class="comment-date">2012-11-07 13:04 UTC</div> </div> <div class="comment" id="79a7065cc57b47acbc9683b61a86215f"> <div class="comment-author"><a href="http://www.truewill.net/myblog/index.php">Bill Sorensen</a> <a href="#79a7065cc57b47acbc9683b61a86215f">#</a></div> <div class="comment-content">Another thoughtful article; thank you!<br> <br> How do considerations of lifetime management factor in? I may want Singleton here, Transient there, etc. That would seem to favor Explicit Register.<br> <br> There's also the option of integration-testing the Composition Root to provide some type-checking.</div> <div class="comment-date">2012-11-09 18:51 UTC</div> </div> <div class="comment" id="30b3d1997173457fa8ccf44b7902f558"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#30b3d1997173457fa8ccf44b7902f558">#</a></div> <div class="comment-content">Bill, thanks for your comment.<br> <br> When it comes to lifetime management, there are answers on more than one level.<br> <br> On the pragmatic level, I've often found that in reality, most of my graphs tend to need to be Transient (or Per Graph) because some commonly used leaf node must be Transient (or Per Graph) for whatever reason. Once that happens, if most (say: more than 75%) of all objects are already Transient, does it really matter if a few more are also Transient? Yes, it could have been more efficient, but if you profile your app, you're most likely to discover that your bottleneck is somewhere else entirely.<br> <br> On a more explicit level, it would be possible to define a convention that picks up a hint about the desired lifetime from the type itself. You could for example call a service &quot;ThreadSafeFoo&quot; to hint that Singleton would be appropriate - or perhaps you could adorn it with a [ThreadSafe] attribute...<br> <br> Testing the container itself <a href="/2011/12/21/TestingContainerConfigurations.aspx">I don't find particularly useful</a>, but a set of smoke tests of the entire app can be helpful.</div> <div class="comment-date">2012-11-11 18:03 UTC</div> </div> <div class="comment" id="c4311b858daf4376ba01bb886460ad05"> <div class="comment-author"><a href="http://arialdomartini.wordpress.com">Arialdo Martini</a> <a href="#c4311b858daf4376ba01bb886460ad05">#</a></div> <div class="comment-content">Good points, Mark, original perspective.<br> I may be wrong, but reading your post I understand that the goal of a DI Container is to compose object graphs. This is undoubtedly true. Yet I think that this is just one of DI Containers' goals, and possibly not even the main one.<br><br> I'm sure you already know the amazing post <a href="http://nblumhardt.com/2011/01/an-autofac-lifetime-primer/">An Autofac Lifetime Primer</a> by Nicholas Blumhardt. It is about AutoFac, but it covers principles that are common to all the CI Containers.<br> Reading Nicholas post what I get is that a CI Container is a tool whose main goal is to manage resources lifetimes. Nicholas defines a resource as "<i>anything with acquisition and release phases in its lifecycle</i>". IoC Containers "<i>provide a good solution to the resource management problem</i>" and "<i>to do this, they need to take ownership of the disposable components that they create</i>". In other words, not only do DI Containers compose object graphs, but they also take care of the lifecycle of objects they created. Nicholas post is very detailed in explaining how and why a DI Container must track resources and guarantee that their disposal is properly managed.<br> This is an excerpt I find particurarly significant:<br><br> "[...] you need to find a strategy to ensure resources are disposed when they’re no longer required. The most widely-attempted one is based around the idea that whatever object acquires the resource should also release it. I pejoratively call it “ad-hoc” because it doesn’t work consistently. Eventually you’ll come up against one (and likely more) of the following issues:<br> <strong>Sharing</strong>: When multiple independent components share a resource, it is very hard to figure out when none of them requires it any more. Either a third party will have to know about all of the potential users of the resource, or the users will have to collaborate. Either way, things get hard fast.<br> <strong>Cascading Changes</strong>: Let’s say we have three components – A uses B which uses C. If no resources are involved, then no thought needs to be given to how resource ownership or release works. But, if the application changes so that C must now own a disposable resource, then both A and B will probably have to change to signal appropriately (via disposal) when that resource is no longer needed. The more components involved, the nastier this one is to unravel."<br><br> CI Containers solve these problems.<br> Poor Man (or Pure) CI solves the compose phase only. But the CI should also take care of resource disposal, or it would not provide any Unit of Work and possibly lead to memory leaks or NullPointerExceptions at runtime. What a basic Por Man implementation provides is just an Instance Per Dependency Scope (every request gets a new instance). With few modificatios, it could provide a Single Instance Scope (that is, a Singleton). But you might agree that managing nested scopes, shared dependencies, instances per web request and a proper disposal management with a Poor Man CI is all but a simple task.<br><br> So, I'm not sure the distinction between Poor Man and DI Containers is only a matter of Convention over Configuration. I got to the conclusion that the main goal of a DI Container is lifecycle management, much more than object graph composition.<br><br> What do you think? </div> <div class="comment-date">2015-08-18 7:06 UTC</div> </div> <div class="comment" id="59fbde8a85c74fad8f614dc470458eb9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#59fbde8a85c74fad8f614dc470458eb9">#</a></div> <div class="comment-content"> <p>Arialdo, thank you for writing.</p> <p> Like you, I used to think that lifetime management was an strong motivation to use a DI Container; there's an entire chapter about lifetime management in <a href="http://amzn.to/12p90MG">my book</a>. </p> <p> There may be cases where that's true, but these days I prefer the <a href="/2014/06/03/compile-time-lifetime-matching">the explicit lifetime matching I get from Pure DI</a>. </p> <p> While you can make lifetime management quite complicated, I prefer to keep it simple, so in practice, I only use the Singleton and Transient lifetime styles. Additionally, I prefer to design my components so that they aren't disposable. If I <em>must</em> use a disposable third-party object, my next priority would be to use a <a href="/2014/08/24/decoraptor">Decoraptor</a>, and add <a href="/2014/08/25/decommissioning-decoraptors">decommissioning support</a> if necessary. Only if none of that is possible will I begin to look at disposal from the Composition Root. </p> <p> Usually, when you only use Singleton and Transient, manual disposal from the Composition Root is easy. There's no practical reason to dispose of the Singletons, so you only need to dispose of the Transient objects. How you do that varies from framework to framework, but in ASP.NET Web API, for example, <a href="/2012/09/28/DependencyInjectionandLifetimeManagementwithASP.NETWebAPI">it's easy</a>. </p> </div> <div class="comment-date">2015-08-18 08:41 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency Injection in ASP.NET Web API with Castle Windsor https://blog.ploeh.dk/2012/10/03/DependencyInjectioninASP.NETWebAPIwithCastleWindsor 2012-10-03T03:45:22+00:00 Mark Seemann <div id="post"> <p> <em>This post describes how to compose Controllers with Castle Windsor in the ASP.NET Web API</em> </p> <p> In my <a href="/2012/09/28/DependencyInjectionAndLifetimeManagementWithASPNETWebAPI.aspx">previous post</a> I described how to use Dependency Injection (DI) in the <a href="http://www.asp.net/web-api">ASP.NET Web API</a> using Poor Man's DI. It explained the basic building blocks, including the relevant extensibility points in the Web API. Poor Man's DI can be an easy way to get started with DI and may be sufficient for a small code base, but for larger code bases you may want to adopt a more convention-based approach. Some DI Containers provide excellent support for Convention over Configuration. One of these is <a href="http://docs.castleproject.org/Windsor.MainPage.ashx">Castle Windsor</a>. </p> <h3 id="ad821840294543bab18b09fa3a7c9726"> Composition Root <a href="#ad821840294543bab18b09fa3a7c9726" title="permalink">#</a> </h3> <p> Instead of the PoorMansCompositionRoot from the example in the previous post, you can create an implementation of <a href="http://msdn.microsoft.com/en-us/library/system.web.http.dispatcher.ihttpcontrolleractivator.aspx">IHttpControllerActivator</a> that acts as an <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> over Castle Windsor: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">WindsorCompositionRoot</span> : <span style="color: #2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IWindsorContainer</span> container; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> WindsorCompositionRoot(<span style="color: #2b91af;">IWindsorContainer</span> container) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.container = container; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IHttpController</span> Create( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HttpRequestMessage</span> request, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HttpControllerDescriptor</span> controllerDescriptor, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span> controllerType) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> controller = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (<span style="color: #2b91af;">IHttpController</span>)<span style="color: blue;">this</span>.container.Resolve(controllerType); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; request.RegisterForDispose( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">Release</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; () =&gt; <span style="color: blue;">this</span>.container.Release(controller))); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> controller; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Release</span> : <span style="color: #2b91af;">IDisposable</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">Action</span> release; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Release(<span style="color: #2b91af;">Action</span> release) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.release = release; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Dispose() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.release(); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> That's pretty much all there is to it, but there are a few points of interest here. First of all, the class implements IHttpControllerActivator just like the previous PoorMansCompositionRoot. That's the extensibility point you need to implement in order to create Controller instances. However, instead of hard-coding knowledge of concrete Controller classes into the Create method, you delegate creation of the instance to an injected IWindsorContainer instance. However, before returning the IHttpController instance created by calling container.Resolve, you register that object graph for disposal. </p> <p> With Castle Windsor decommissioning is done by invoking the Release method on IWindsorContainer. The input into the Release method is the object graph originally created by IWindsorContainer.Resolve. That's the rule from the <a href="/2010/09/29/TheRegisterResolveReleasePattern.aspx">Register Resolve Release pattern</a>: What you Resolve you must also Release. This ensures that if the Resolve method created a disposable instance (even deep in the object graph), the Release method signals to the container that it can now safely dispose of it. You can read more about this subject in <a href="http://amzn.to/12p90MG">my book</a>. </p> <p> The <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessageextensions.registerfordispose.aspx">RegisterForDispose</a> method takes as a parameter an <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx">IDisposable</a> instance, and not a Release method, so you must wrap the call to the Release method in an IDisposable implementation. That's the little private Release class in the code example. It adapts an <a href="http://msdn.microsoft.com/en-us/library/system.action.aspx">Action delegate</a> into a class which implements IDisposable, invoking the code block when Dispose is invoked. The code block you pass into the constructor of the Release class is a closure around the outer variables <code>this.container</code> and <code>controller</code> so when the Dispose method is called, the container releases the controller (and the entire object graph beneath it). </p> <h3 id="7a580fe878214c54a4c49bd3772ab756"> Configuring the container <a href="#7a580fe878214c54a4c49bd3772ab756" title="permalink">#</a> </h3> <p> With the WindsorCompositionRoot class in place, all that's left is to set it all up in Global.asax. Since IWindsorContainer itself implements IDisposable, you should create and configure the container in the application's constructor so that you can dispose it when the application exits: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IWindsorContainer</span> container; &nbsp; <span style="color: blue;">public</span> WebApiApplication() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.container = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">WindsorContainer</span>().Install(<span style="color: blue;">new</span> <span style="color: #2b91af;">DependencyConventions</span>()); } &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> Dispose() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.container.Dispose(); &nbsp;&nbsp;&nbsp; <span style="color: blue;">base</span>.Dispose(); }</pre> </p> <p> Notice that you can configure the container with the Install method directly in the constructor. That's the Register phase of the Register Resolve Release pattern. </p> <p> In Application_Start you tell the ASP.NET Web API about your WindsorCompositionRoot instead of PoorMansCompositionRoot from the previous example: </p> <p> <pre style="margin: 0px;"><span style="color: #2b91af;">GlobalConfiguration</span>.Configuration.Services.Replace( &nbsp;&nbsp;&nbsp; <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IHttpControllerActivator</span>), &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">WindsorCompositionRoot</span>(<span style="color: blue;">this</span>.container));</pre> </p> <p> Notice that the <code>container</code> instance field is passed into the constructor of WindsorCompositionRoot, so that it can use the container instance to Resolve Controller instances. </p> <h3 id="032b0259bc3a42ad912d65876549b8ed"> Summary <a href="#032b0259bc3a42ad912d65876549b8ed" title="permalink">#</a> </h3> <p> Setting up DI in the ASP.NET Web API with a DI Container is easy, and it would work very similarly with other containers (not just Castle Windsor), although the Release mechanisms tend to be a bit different from container to container. You'll need to create an Adapter from IHttpControllerActivator to your container of choice and set it all up in the Global.asax. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="32792a0e61104313bf8f00e9357ed464"> <div class="comment-author">Mahesh <a href="#32792a0e61104313bf8f00e9357ed464">#</a></div> <div class="comment-content">Hi,<br> <br> I am getting an error -<br> <br> The type or namespace name 'DependencyConventions' could not be found (are you missing a using directive or an assembly reference?)<br> <br> I added Castle windsor via Nuget in VS 2012 Web Express.<br> <br> What's the problem?<br> <br> Thanks,<br> Mahesh.</div> <div class="comment-date">2012-10-14 10:49 UTC</div> </div> <div class="comment" id="32792a0e61104313bf8f00e9357ed464"> <div class="comment-author">Luis <a href="#32792a0e61104313bf8f00e9357ed464">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Not even remotely related to Web API, but I was wondering if a blog post about CQRS and DI in general was in the pipeline. Last time I posted I hadn't read your book, now that I have, I'm finding myself reading your blog posts like a book and I can't wait for the next. Great book by the way, can't recommend it enough, unless you're on some sort of diet.<br> <br> Luis</div> <div class="comment-date">2012-10-16 08:15 UTC</div> </div> <div class="comment" id="3af3ad29331e4134a15f878135058c58"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3af3ad29331e4134a15f878135058c58">#</a></div> <div class="comment-content">Thank you. Have you read <a href="http://msdn.microsoft.com/en-us/magazine/gg983487.aspx">my CQRS article</a>? For examples on using DI with CQRS, I'd like to suggest the <a href="https://github.com/ploeh/Booking">updated example code</a>.</div> <div class="comment-date">2012-10-16 11:59 UTC</div> </div> <div class="comment" id="11898626066b48d28abaedae4440aac0"> <div class="comment-author">Simon <a href="#11898626066b48d28abaedae4440aac0">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Does the above implementation also resolve normal MVC4 website controllers? If so is there any extra setup required in the Global.asax file? Prior to MVC4 I was using the ControllerFactory method described in your book but is this still the best way?<br> </div> <div class="comment-date">2012-10-17 08:12 UTC</div> </div> <div class="comment" id="6d3452f6338742e49a55ca4198050a0b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6d3452f6338742e49a55ca4198050a0b">#</a></div> <div class="comment-content">IHttpControllerActivator is a special Web API interface, as is IHttpController. MVC Controllers are not resolved with this API, but it's very similar to the approach outlined in my book.</div> <div class="comment-date">2012-10-17 12:30 UTC</div> </div> <div class="comment" id="4aad1d461bfc40229d2c683074ee93f1"> <div class="comment-author">Rema Manual <a href="#4aad1d461bfc40229d2c683074ee93f1">#</a></div> <div class="comment-content">Hi Mark,<br> <br> How about using Windsor/above technique for injecting dependencies into MVC 4 attributes? I am using customized Authorize and ExceptionFilter attributes and so far I have not found a nice, easy and clean way to inject dependencies into them?<br> <br> </div> <div class="comment-date">2012-10-20 12:58 UTC</div> </div> <div class="comment" id="ee16da9980cd4b2c9bf74f6a3df18463"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ee16da9980cd4b2c9bf74f6a3df18463">#</a></div> <div class="comment-content">You can't use the <em>above</em> technique for injecting anything into MVC 4 attributes, since they aren't Controllers. The only way to inject dependencies into MVC attributes is by Property Injection, and if you read section 4.2 of <a href="http://amzn.to/12p90MG">my book</a> you'll see that there are many issues with this pattern.<br> <br> A better approach is <a href="http://stackoverflow.com/a/7194467/126014">to use global filters with behaviour, and use only passive attributes</a>.</div> <div class="comment-date">2012-10-21 13:48 UTC</div> </div> <div class="comment" id="9467132e7c7f40e2afcd5db30f61ad36"> <div class="comment-author">Daniel Hilgarth <a href="#9467132e7c7f40e2afcd5db30f61ad36">#</a></div> <div class="comment-content">Thanks for your article. I adapted it to Autofac and I thought I'd share that code. <br> Autofac has the concept of LifetimeScopes. Using these, the code looks like the following:<br> <br> public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)<br> {<br> var scope = _container.BeginLifetimeScope();<br> var controller = (IHttpController)scope.Resolve(controllerType);<br> request.RegisterForDispose(scope);<br> return controller;<br> }<br> <br> If you want to register dependencies that are different for every request (like Hyprlinkr's RouteLinker), you can do this when beginning the lifetime scope:<br> <br> public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)<br> {<br> var scope = _container.BeginLifetimeScope(x =&gt; RegisterRequestDependantResources(x, request));<br> var controller = (IHttpController)scope.Resolve(controllerType);<br> request.RegisterForDispose(scope);<br> return controller;<br> }<br> <br> private static void RegisterRequestDependantResources(ContainerBuilder containerBuilder, HttpRequestMessage request)<br> {<br> containerBuilder.RegisterInstance(new RouteLinker(request));<br> containerBuilder.RegisterInstance(new ResourceLinkVerifier(request.GetConfiguration()));<br> }<br> <br> Sorry for the formatting, I have no idea how to format code here.</div> <div class="comment-date">2012-10-26 18:54 UTC</div> </div> <div class="comment" id="1fd7822d8d8b40be94d09cdb70590786"> <div class="comment-author">Alexander <a href="#1fd7822d8d8b40be94d09cdb70590786">#</a></div> <div class="comment-content">Hi Mark,<br> Nice article.<br> <br> As I understand WebApiApplication can be instantiated several times and disposed several times as well. This page(http://msdn.microsoft.com/en-gb/library/ms178473.aspx) says &quot;The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.&quot;<br> So as I understand your approach we can get into a situation using disposed context. <br> <br> What do you think about this?</div> <div class="comment-date">2012-11-20 02:32 UTC</div> </div><div class="comment"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#">#</a></div> <div class="comment-content">I've never experienced it to be a problem, so my guess is that in reality the documentation is off and there's only one instance of HttpApplication. Otherwise, the container should be disposed, and I've never seen that happen.</div> <div class="comment-date">2012-11-20 08:07 UTC</div> </div> <div class="comment" id="90e118589a394986b2bf7d3f8db87d5d"> <div class="comment-author">Alexander <a href="#90e118589a394986b2bf7d3f8db87d5d">#</a></div> <div class="comment-content">I've ended with static context in the HttpApplication. Create context in Application_Start and dispose Application_End. I think it's better for future once documentation become the reality:). For each AppDomain which possibly can be created we will have separate context.<br> Anyway your example is very useful for me.</div> <div class="comment-date">2012-11-20 09:38 UTC</div> </div> <div class="comment" id="481592c9d1b3496ab0276f3135b45c45"> <div class="comment-author">Chris <a href="#481592c9d1b3496ab0276f3135b45c45">#</a></div> <div class="comment-content">Is the constructor the correct place to initialise the container?<br> <br> As there are multiple instances of HttpApplication per application<br> <br> (If I put a breakpoint in the constructor it gets hit multiple times)<br> <br> As you can see by these articles, there is not a single instance of HttpApplication, but multiple<br> <br> http://lowleveldesign.wordpress.com/2011/07/20/global-asax-in-asp-net/<br> http://msdn.microsoft.com/en-us/library/a0xez8f2(v=vs.71).aspx <br> <br> wouldn't it be more appropriate to go in Application_Start?<br> <br> <br> </div> <div class="comment-date">2012-12-07 13:50 UTC</div> </div> <div class="comment" id="5ed7bbdb1d654afba786b405842c2c56"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5ed7bbdb1d654afba786b405842c2c56">#</a></div> <div class="comment-content">See previous comments.</div> <div class="comment-date">2012-12-09 19:36 UTC</div> </div> <div class="comment" id="369682befd4c486ba67e273bcca88bf6"> <div class="comment-author"><a href="http://www.redturtle.co.uk">Will Webster</a> <a href="#369682befd4c486ba67e273bcca88bf6">#</a></div> <div class="comment-content">Thanks for this post. Very useful. Not sure if I am missing something, but having the same issue as the first commentor (Mahesh). What should <em>DependencyConventions()</em> actually do?</div> <div class="comment-date">2013-04-12 16:01 UTC</div> </div> <div class="comment" id="e86238fe82c14454a5b85e1222842086"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e86238fe82c14454a5b85e1222842086">#</a></div> <div class="comment-content">Will, <em>DependencyConventions</em> is just a <a href="http://docs.castleproject.org/Default.aspx?Page=Installers&NS=Windsor&AspxAutoDetectCookieSupport=1">Windsor Installer</a>. I just called it <em>DependencyConventions</em> because <a href="/2012/11/06/WhentouseaDIContainer/">I prefer Convention over Configuration when using DI Containers</a>. In your own project, you'll need to define your own Windsor Installer. Alternatively, you can configure the container directly in the <em>WebApiApplication</em> constructor.</div> <div class="comment-date">2013-04-12 16:00 UTC</div> </div> <div class="comment" id="0343fdc08fd049b087e1eda9635eb2e5"> <div class="comment-author">Jeff Soper <a href="#0343fdc08fd049b087e1eda9635eb2e5">#</a></div> <div class="comment-content"> <p> I've been studying this article and some of your answers like <a href="http://stackoverflow.com/a/7194467/533958">this one</a> to StackOverflow questions pertaining to DI. It seems that the established community efforts to integrate popular IoC containers such as <a href="http://www.ninject.org">Ninject</a> are, at their core,<a href="https://github.com/ninject/ninject.web.mvc/blob/master/mvc3/src/Ninject.Web.Mvc/NinjectDependencyResolver.cs">implementations of IDependencyResolver</a>, rather than IHttpControllerActivator. </p> <p> Are these popular community efforts missing the 'access to context' trait of your solution, or are they simply accomplishing it another way? Are there any established projects, open-source or otherwise, that do what you propose, or is this still an untapped 'pull request' opportunity for projects like the Ninject MVC, etc? </p> </div> <div class="comment-date">2014-03-15 17:40 UTC</div> </div> <div class="comment" id="5085dbb7787f44d8ae235b5a15843eca"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5085dbb7787f44d8ae235b5a15843eca">#</a></div> <div class="comment-content"> <p> Jeff, thank you for writing. You are indeed correct that one of the many problems with the <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator anti-pattern</a> (and therefore <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">also IDependencyResolver</a>) is that the overall context is hidden. <a href="http://codebetter.com/glennblock/">Glenn Block</a> originally pointed that particular problem out to me. </p> <p> This is also the case with IDependencyResolver, because when GetService(Type) or GetServices(Type) is invoked, the only information the composition engine knows, is the requested type. Thus, resolving something that requires access to the HttpRequestMessage or one of its properties, is <em>impossible</em> with IDependencyResolver, but perfectly possible with IHttpControllerActivator. </p> <p> So, yes, I would definitely say that any DI Container that provides 'ASP.NET Web API integration' by implementing IDependencyResolver is missing out. In any case, these days <a href="/2012/11/06/WhentouseaDIContainer">I rarely use a DI Container</a>, so I don't consider it a big deal - and if I need to use a DI Container, I just need to add those few lines of code listed above in this blog post. </p> </div> <div class="comment-date">2014-03-15 18:28 UTC</div> </div> <div class="comment" id="514761f66eb4487aa541ff6b48b89142"> <div class="comment-author">Dmitry Goryunov <a href="#514761f66eb4487aa541ff6b48b89142">#</a></div> <div class="comment-content"> <p> Can't figure out, how is it better to organize my solution. </p> <p> There are, for example, three projects Cars.API, Cars.Core, and Cars.Data. API contains web-interface, Core contains all the abstractions, and Data communicates with DB. Data and API should depend on Core according to Dependency inversion principle. At this point everything seems to be clear, but then we implement our Composition Root in the API project, which makes it dependent on the Data project containing implementations of abstractions that are stored in Core project. Is it violation of Dependency inversion principle? </p> <p> P.S. thank you for your book and the articles you write. </p> </div> <div class="comment-date">2015-12-07 16:53 UTC</div> </div> <div class="comment" id="cee515aeff4540a59a96d30d80d0aff2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cee515aeff4540a59a96d30d80d0aff2">#</a></div> <div class="comment-content"> <p> Dmitry, thank you for writing. Does <a href="/2013/12/03/layers-onions-ports-adapters-its-all-the-same">this</a> or <a href="http://stackoverflow.com/a/9503612/126014">this</a> help? </p> </div> <div class="comment-date">2015-12-07 17:05 UTC</div> </div> <div class="comment" id="36345defe32d44dd9d96670f65bec1a5"> <div class="comment-author">Andrew G <a href="#36345defe32d44dd9d96670f65bec1a5">#</a></div> <div class="comment-content"> <p> In the Configuring the Container section, you are placing the Install inside the constructor. Whenever the application starts up or executes a request, the constructor seems to be called multiple times. In turn, the container will be created multiple times throughout its life time. Is that the point? Or should the container be moved into the Application_Start? Although the constructor is called multiple times, application start seems to be called once. The dispose doesnt seem to be called till the end as well. Is there something earlier in the lifecycle that would cause a need for the Register to be done in the constructor? </p> <p> I very much enjoy your book and your blog btw. great source of solid information! </p> </div> <div class="comment-date">2017-09-01 10:52 UTC</div> </div> <div class="comment" id="a5dd4df78d2843f8984ff1c330a6c464"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a5dd4df78d2843f8984ff1c330a6c464">#</a></div> <div class="comment-content"> <p> Andrew, thank you for writing. In general, I don't recall that this has ever been an issue, but see previous threads in the comments for this post. The question's come up before. </p> <p> I do, however, admit that I've never formally studied the question like <a href="/2010/05/17/ServiceHostFactorylifetime">I did with WCF</a>, so it's possible that I'm wrong on this particular issue. Also, details of the framework could have changed in the five years that's gone by since I wrote the article. </p> </div> <div class="comment-date">2017-09-02 15:33 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency Injection and Lifetime Management with ASP.NET Web API https://blog.ploeh.dk/2012/09/28/DependencyInjectionandLifetimeManagementwithASP.NETWebAPI 2012-09-28T03:56:21+00:00 Mark Seemann <div id="post"> <p> <em>This post describes how to properly use Dependency Injection in the ASP.NET Web API, including proper Lifetime Management.</em> </p> <p> The <a href="http://www.asp.net/web-api">ASP.NET Web API</a> supports Dependency Injection (DI), but the appropriate way to make it work is not the way it's <a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver">documented by Microsoft</a>. Even though the final version of <a href="http://msdn.microsoft.com/en-us/library/system.web.http.dependencies.idependencyresolver.aspx">IDependencyResolver</a> includes the notion of an <a href="http://msdn.microsoft.com/en-us/library/system.web.http.dependencies.idependencyscope.aspx">IDependencyScope</a>, and thus <em>seemingly</em> supports decommissioning (the release of IDisposable dependencies), it's not very useful. </p> <h3 id="ef92649c8c7c4f589fddf1cb0eda8e2a"> The problem with IDependencyResolver <a href="#ef92649c8c7c4f589fddf1cb0eda8e2a" title="permalink">#</a> </h3> <p> The main problem with IDependencyResolver is that <a href="/2010/11/01/PatternRecognitionAbstractFactoryOrServiceLocator.aspx">it's essentially a Service Locator</a>. There are many problems with the <a href="/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx">Service Locator anti-pattern</a>, but most of them I've already described elsewhere on this blog (and in <a href="http://amzn.to/12p90MG">my book</a>). One disadvantage of Service Locator that I haven't yet written so much about is that within each call to <a href="http://msdn.microsoft.com/en-us/library/system.web.http.dependencies.idependencyscope.getservice.aspx">GetService</a> there's no context at all. This is a general problem with the Service Locator anti-pattern, not just with IDependencyResolver. <a href="http://blogs.msdn.com/b/gblock/">Glenn Block</a> originally pointed this out to me. The problem is that in an implementation, all you're given is a <a href="http://msdn.microsoft.com/en-us/library/system.type.aspx">Type</a> instance and asked to return an object, but you're not informed about the context. You don't know how deep in a dependency graph you are, and if you're being asked to provide an instance of the same service multiple times, you don't know whether it's within the same HTTP request, or whether it's for multiple concurrent HTTP requests. </p> <p> In the ASP.NET Web API this issue is exacerbated by another design decision that the team made. Contrary to the IDependencyResolver design, I find this other decision highly appropriate. It's how <em>context</em> is modeled. In previous incarnations of web frameworks from Microsoft, we've had such abominations as <a href="http://msdn.microsoft.com/en-us/library/system.web.httpcontext.current.aspx">HttpContext.Current</a>, <a href="http://msdn.microsoft.com/en-us/library/system.web.httpcontextbase.aspx">HttpContextBase</a> and <a href="http://msdn.microsoft.com/en-us/library/system.web.httpcontextwrapper.aspx">HttpContextWrapper</a>. If you've ever tried to work with these interfaces, and particularly if you've tried to do TDD against any of these types, you know just how painful they are. That they are built around the <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton pattern</a> certainly doesn't help. </p> <p> The ASP.NET Web API does it differently, and that's very fortunate. Everything you need to know about the context is accessible through the <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessage.aspx">HttpRequestMessage</a> class. While you could argue that it's a bit of a <a href="http://en.wikipedia.org/wiki/God_object">God Object</a>, it's certainly a step in the right direction because at least it's a class you can instantiate within a unit test. No more nasty Singletons. </p> <p> This is good, but from the perspective of DI this makes IDependencyResolver close to useless. Imagine a situation where a dependency deep in the dependency graph need to know something about the context. What was the request URL? What was the base address (host name etc.) requested? How can you share dependency instances within a single request? To answer such questions, you must know about the context, and IDependencyResolver doesn't provide this information. In short, IDependencyResolver isn't the right hook to compose dependency graphs. Fortunately, the ASP.NET Web API has a better extensibility point for this purpose. </p> <h3 id="4219fc268003406097230fdf97f5d730"> Composition within context <a href="#4219fc268003406097230fdf97f5d730" title="permalink">#</a> </h3> <p> Because HttpRequestMessage provides the context you may need to compose dependency graphs, the best extensibility point is the extensibility point which provides an HttpRequestMessage every time a graph should be composed. This extensibility point is the <a href="http://msdn.microsoft.com/en-us/library/system.web.http.dispatcher.ihttpcontrolleractivator.aspx">IHttpControllerActivator</a> interface: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IHttpController</span> Create( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HttpRequestMessage</span> request, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HttpControllerDescriptor</span> controllerDescriptor, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span> controllerType); }</pre> </p> <p> As you can see, each time the Web API framework invokes this method, it will provide an HttpRequestMessage instance. This seems to be the best place to compose the dependency graph for each request. </p> <h3 id="433092fd8dcf4f569fe6181e161d9f38"> Example: Poor Man's DI <a href="#433092fd8dcf4f569fe6181e161d9f38" title="permalink">#</a> </h3> <p> As an example, consider a Controller with this constructor signature: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> RootController(<span style="color: #2b91af;">IStatusQuery</span> statusQuery)</pre> </p> <p> If this is the only Controller in your project, you can compose its dependency graph with a custom IHttpControllerActivator. This is easy to do: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PoorMansCompositionRoot</span> : <span style="color: #2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IHttpController</span> Create( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HttpRequestMessage</span> request, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HttpControllerDescriptor</span> controllerDescriptor, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span> controllerType) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (controllerType == <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">RootController</span>)) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">RootController</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">StatusQuery</span>()); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">null</span>; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The Create implementation contains a check on the supplied <code>controllerType</code> parameter, creating a RootController instance if the requested Controller type is RootController. It simply creates the (very shallow) dependency graph by injecting a new StatusQuery instance into a new RootController instance. If the requested Controller type is anything else than RootController, the method returns null. It seems to be a convention in the Web API that if you can't supply an instance, you should return null. (This isn't a design I'm fond of, but this time I'm only on the supplying side, and I can only pity the developers on the receiving side (the ASP.NET team) that they'll have to write all those null checks.) </p> <p> Some readers may think that it would be better to use a DI Container here, and that's certainly possible. In a future post I'll provide an example on how to do that. </p> <p> The new PoorMansCompositionRoot class must be registered with the Web API framework before it will work. This is done with a single line in Application_Start in Global.asax.cs: </p> <p> <pre style="margin: 0px;"><span style="color: #2b91af;">GlobalConfiguration</span>.Configuration.Services.Replace( &nbsp;&nbsp;&nbsp; <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IHttpControllerActivator</span>), &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">PoorMansCompositionRoot</span>());</pre> </p> <p> This replaces the default implementation that the framework provides with the PoorMansCompositionRoot instance. </p> <h3 id="c1bad31c96e04ed088fbee3c4b434123"> Decommissioning <a href="#c1bad31c96e04ed088fbee3c4b434123" title="permalink">#</a> </h3> <p> Implementing <a href="http://msdn.microsoft.com/en-us/library/hh944278.aspx">IHttpControllerActivator.Create</a> takes care of composing object graphs, but what about decommissioning? What if you have dependencies (deep within the dependency graph) implementing the <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx">IDisposable</a> interface? These must be disposed of after the request has ended (unless they are Singletons) - if not, you will have a resource leak at hand. However, there seems to be no Release hook in IHttpControllerActivator. On the other hand, there's a Release hook in IDependencyResolver, so is IDependencyResolver, after all, the right extensibility point? Must you trade off context for decommissioning, or can you have both? </p> <p> Fortunately you can have both, because there's a <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessageextensions.registerfordispose.aspx">RegisterForDispose</a> extension method hanging off HttpRequestMessage. It enables you to register all appropriate disposable instances for disposal after the request has completed. </p> <h3 id="a6229a4db3a34d88a9533b2f8c9cfeef"> Example: disposing of a disposable dependency <a href="#a6229a4db3a34d88a9533b2f8c9cfeef" title="permalink">#</a> </h3> <p> Imagine that, instead of the StatusQuery class from the previous example, you need to use a disposable implementation of IStatusQuery. Each instance must be disposed of after each request completes. In order to accomplish this goal, you can modify the PoorMansCompositionRoot implementation to this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PoorMansCompositionRoot</span> : <span style="color: #2b91af;">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">IHttpController</span> Create( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HttpRequestMessage</span> request, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HttpControllerDescriptor</span> controllerDescriptor, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span> controllerType) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (controllerType == <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">RootController</span>)) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> disposableQuery = <span style="color: blue;">new</span> <span style="color: #2b91af;">DisposableStatusQuery</span>(); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; request.RegisterForDispose(disposableQuery); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">RootController</span>(disposableQuery); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">null</span>; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Notice that the <code>disposableQuery</code> instance is passed to the RegisterForDispose method before it's injected into the RootController instance and the entire graph is returned from the method. After the request completes, DisposableStatusQuery.Dispose will be called. </p> <p> If you have a dependency which implements IDisposable, but should be shared across all requests, obviously you shouldn't register it for disposal. Such Singletons you can keep around and dispose of properly when the application exits gracefull (if that ever happens). </p> <h3 id="54882ebb57c142fab495dc800acfe3eb"> Summary <a href="#54882ebb57c142fab495dc800acfe3eb" title="permalink">#</a> </h3> <p> Proper DI and Lifetime Management with the ASP.NET Web API is easy once you know how to do it. It only requires a few lines of code. </p> <p> Stay away from the IDependencyResolver interface, which is close to useless. Instead, implement IHttpControllerActivator and use the RegisterForDispose method for decommissioning. </p> <p> In <a href="/2012/10/03/DependencyInjectionInASPNETWebAPIWithCastleWindsor.aspx">a future post</a> I will demonstrate how to use a DI Container instead of Poor Man's DI. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="fe3791848fe14168890ccd9857f13ddd"> <div class="comment-author"><a href="http://blog.gauffin.org">jgauffin</a> <a href="#fe3791848fe14168890ccd9857f13ddd">#</a></div> <div class="comment-content">IDependencyResolver (WebApi version) do support scoping. Look at the &quot;public IDependencyScope BeginScope()&quot; method.</div> <div class="comment-date">2012-09-28 12:24 UTC</div> </div> <div class="comment" id="96313a95acb1444d9bf77ebb43468d56"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#96313a95acb1444d9bf77ebb43468d56">#</a></div> <div class="comment-content">Yes, that's what I wrote. The problem is that it doesn't provide any context.</div> <div class="comment-date">2012-09-28 12:30 UTC</div> </div> <div class="comment" id="249801630d504d93a6792fc2d5b0a021"> <div class="comment-author"><a href="http://blog.gauffin.org">jgauffin</a> <a href="#249801630d504d93a6792fc2d5b0a021">#</a></div> <div class="comment-content">So what you're saying is that the DependencyResolver is bad because you *might* need to get more context information for the controller composition? Because I fail to see the advantage of your approach with the examples that you have given.<br> </div> <div class="comment-date">2012-09-28 18:17 UTC</div> </div> <div class="comment" id="beebd6d3f3664ca48500360e65c5ce14"> <div class="comment-author">Dave Bettin <a href="#beebd6d3f3664ca48500360e65c5ce14">#</a></div> <div class="comment-content">What changes would have you made to the DependencyResolver/Scope design to provide this context? Or would you ditch the design completely and start fresh?</div> <div class="comment-date">2012-09-28 20:40 UTC</div> </div> <div class="comment" id="f1999a50235f4902b689aa8229ac94a3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f1999a50235f4902b689aa8229ac94a3">#</a></div> <div class="comment-content">Dave, the IHttpControllerActivator/RegisterForDispose is a workable combo, but I'd preferred a release method on it, just like MVC's <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.icontrollerfactory.aspx">IControllerFactory</a>.</div> <div class="comment-date">2012-09-28 21:26 UTC</div> </div> <div class="comment" id="f7b8738c997647aca57e5bca19832c3d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f7b8738c997647aca57e5bca19832c3d">#</a></div> <div class="comment-content">Jonas, let's assume that you have a deeper dependency graph. Let's say that you have this Controller constructor: public MyController(IService1, IService2). Imagine, moreover, that you have implementations of each of these interfaces, with these constructors: public Service1(IFoo) and public Service2(IFoo).<br> <br> You have one implementation of IFoo, namely Foo, and for efficiency reasons, or perhaps because Foo is a Mediator, you'd like to share the same instance of Foo between Service1 and Service2. However, Foo isn't thread-safe, so you can only share the Foo instance within a single request. For each request, you want to use a single instance of Foo.<br> <br> This is the Per Graph lifestyle pattern (p. 259 in my book).<br> <br> That's not possible with IDependencyResolver, but it is with IHttpControllerActivator.</div> <div class="comment-date">2012-09-28 21:35 UTC</div> </div> <div class="comment" id="e4ccfcb425604026b52e82b8365ea934"> <div class="comment-author"><a href="https://github.com/seesharper">Bernhard Richter</a> <a href="#e4ccfcb425604026b52e82b8365ea934">#</a></div> <div class="comment-content">Hi Mark<br> <br> First of all I would like to mention that I just read your book and I enjoyed it very much.<br> Straight to the point with good simple examples and metaphors.<br> What I have noticed thought, from your book and from this blog post, is that you give handing <b>IDisposable</b> object a great deal of attention.<br> <br> I have written a lightweight service container over at Github (https://github.com/seesharper/LightInject/wiki/Getting-started) that tries to do the &quot;right thing&quot; with a minimal effort.<br> <br> Then I started to read about handling disposable services in your book and realized that this is actually quite a complicated thing to deal with.<br> <br> It also seems to be unclear how a service container actually should do this. The various container implementations has pretty much its own take on the disposable challenge where as Spring.Net for instance, does not seem to go very far on this topic. Yet it is one of the most popular DI frameworks out there. <br> <br> The question then remains, is automatic handling of disposable objects a necessity for any container or is a feature? <br> <br> If it is absolutely necessary, how complex does it need to be. I would rather not implement a whole ref-count system on top of the CLR :)<br> <br> Regards <br> <br> Bernhard Richter<br> <br> <br> </div> <div class="comment-date">2012-09-29 08:39 UTC</div> </div> <div class="comment" id="dd566ed7ed1e41288d65aa180f2bc06d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dd566ed7ed1e41288d65aa180f2bc06d">#</a></div> <div class="comment-content">Well, whether or not it's a necessity depends on who you ask.<br> <br> StructureMap, for example, has no decommissioning capability, and when you ask Jeremy Miller, it's by design. The reason for this is that <em>if you don't have</em> any disposable dependencies at all, it's just an overhead keeping track of all the instances created by the container. Garbage collection will ensure that resources are properly reclaimed.<br> <br> Containers that <em>do</em> keep track of the instances they created <em>will</em> leak unless you explicitly remember to Release what you Resolve. By that argument, Jeremy Miller considers StructureMap's design safer because in the majority case there will be no leaks. IMO, the downside of that is that <em>if</em> you have disposable dependencies, they <em>will</em> leak and there's nothing you can do about.<br> <br> On the other hand, with a container like Castle Windsor, it's important to Release what you Resolve, or you might have leaks. The advantage, however, is that you're guaranteed that everything <em>can</em> be properly disposed of.<br> <br> Thus, in the end, no matter the strategy, it all boils down to that the developers <em>using</em> the container must exercise discipline. However, they are two different kinds of discipline: Either <em>never</em> use disposable dependencies or <em>always</em> Release what you Resolve.</div> <div class="comment-date">2012-09-30 12:09 UTC</div> </div> <div class="comment" id="0a9b5e9b16e34bf8bff6082b86c10b55"> <div class="comment-author">Kirin Yao <a href="#0a9b5e9b16e34bf8bff6082b86c10b55">#</a></div> <div class="comment-content">So, it's also applicable to DI in ASP.NET MVC, isn't it? In IControllerActivator.Create method, RequestContext parameter provides the context for creating controller. </div> <div class="comment-date">2012-10-19 09:02 UTC</div> </div> <div class="comment" id="377f3d628dbd41a8ada9fd3f1d887e97"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#377f3d628dbd41a8ada9fd3f1d887e97">#</a></div> <div class="comment-content">Yes, this is described in detail in section 7.2 in <a href="http://amzn.to/12p90MG">my book</a>.</div> <div class="comment-date">2012-10-19 12:39 UTC</div> </div> <div class="comment" id="2838973fe84b4584b49f9c348beb4906"> <div class="comment-author">Ali <a href="#2838973fe84b4584b49f9c348beb4906">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Great article, thanks a lot! Loved your book as well :)<br> <br> I just have a little observation that returning null in the Create method causes a 404. Instead we could do the following to call the default implementation for other controllers:<br> <br> return new DefaultHttpControllerActivator().Create(request, controllerDescriptor, controllerType);</div> <div class="comment-date">2013-02-22 10:16 UTC</div> </div> <div class="comment" id="c14d55c9f03a42a1b6431fd318197441"> <div class="comment-author">ImaginaryDevelopment <a href="#c14d55c9f03a42a1b6431fd318197441">#</a></div> <div class="comment-content">Hi Mark,<br> <br> How would you handle db calls that need to be made to validate objects in say <code>[CustomValidationAttribute()]</code> tags<br> where you need the objects used to validate again in the actual web api action method? <br> <br> </div> <div class="comment-date">2014-08-28 14:52 UTC</div> </div> <div class="comment" id="81cec7d18b0c4f8584ec92888f1a3c92"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#81cec7d18b0c4f8584ec92888f1a3c92">#</a></div> <div class="comment-content"> <p> In general, I don't like attributes with behaviour, but prefer <a href="/2014/06/13/passive-attributes">passive attributes</a>. Still, that just moves the implementation to the Filter's ExecuteActionFilterAsync method, so that doesn't really address your question. </p> <p> If you need access to the actual objects created from the incoming request, you <em>probably</em> could pull it out of the HttpActionContext passed into ExecuteActionFilterAsync, but why bother? You can access the object from the Controller. </p> <p> A Filter attribute in ASP.NET Web API, to be useful, represents a Cross-Cutting Concern, such as security, logging, metering, caching, etc. Cross-Cutting Concerns are <em>cross-cutting</em> exactly because they are independent of the actual values or objects in use. This isn't the case for validation, which tends to be specific to particular types, so these are most likely better addressed by a Controller, or a Service invoked by the Controller, rather than by a custom attribute. </p> <p> Once you're in the Controller, if you need access to a database, you can inject a data access object (commonly referred to as a Repository) into the Controller. </p> </div> <div class="comment-date">2014-08-30 11:23 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Concrete Dependencies https://blog.ploeh.dk/2012/08/31/ConcreteDependencies 2012-08-31T20:37:37+00:00 Mark Seemann <div id="post"> <p> <em>Concrete classes can also be used as dependencies</em> </p> <p> Usually, when we think about Dependency Injection (DI), we tend to consider that only polymorphic types (interfaces or (abstract) base classes) can act as dependencies. However, in a previous blog post I described how <a href="/2012/07/02/PrimitiveDependencies.aspx">primitives can be used as dependencies</a>. A primitive is about as far removed from polymorphism as it can be, but there's a middle ground too. Sometimes 'normal' concrete classes with non-virtual members can be used as dependencies with to great effect. </p> <p> While the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> is voided by injecting a concrete type, there can be other good reasons to occasionaly do something like this. Consider how many times you've written an extension method to perform some recurring task. Sometimes it turns out that an extension method isn't the best way to encapsulate a common algorithm. It might start out simple enough, but then you realize that you need to provide the extension method with a control parameter in order to 'configure' it. This causes you to add more arguments to the extension method, or to add more overloads. Soon, you have something like the Object Mother (anti-)pattern on your hand. </p> <p> A concrete class can sometimes be a better way to encapsulate common algorithms in a way where the behavior can be tweaked or changed in one go. Sometimes the boundary can become blurred. In the previous post I examined constructor arguments such as strings and integers, but what about an <a href="http://msdn.microsoft.com/en-us/library/system.uri.aspx">Uri</a> instance? It might act as a base URI for creating absolute links from within a Controller. An Uri instance isn't really a primitive, although it basically just encapsulates something which is a string. It's an excellent example of the <a href="http://martinfowler.com/bliki/ValueObject.html">Value Object</a> pattern, providing a rich API for manipulating and querying URIs. </p> <p> It can be more complex that that. Consider <a href="https://github.com/ploeh/Hyprlinkr">Hyprlinkr</a> as an example. What it does is to produce URI links to other Controllers in an <a href="http://www.asp.net/web-api">ASP.NET Web API</a> service in a strongly typed way. It's not really a polymorphic dependency as such, although it <em>does</em> implement an interface. It's more like a reusable component which produces a determinstic result without side-effects. In Functional Programming terminology, it's comparable to a <em>pure function</em>. For a number of reasons, this is a prime candidate for a concrete dependency. </p> <p> Before I get to that, I want to show you what I mean when I talk about locally scoped methods, including extension methods and such. Then I want to talk about using the RouteLinker class (the main class in Hyprlinkr) as a classic polymorphic dependency, and why that doesn't really work either. Finally, I want to talk about why the best option is to treat RouteLinker as a concrete dependency. </p> <h3 id="d29f3e6122124d5fada85c07a9425395"> RouteLinker as a local variable <a href="#d29f3e6122124d5fada85c07a9425395" title="permalink">#</a> </h3> <p> While Hyprlinkr was always designed with DI in mind, you actually don't have to use DI to use it. From within an ApiController class, you can just create an instance like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> linker = <span style="color: blue;">new</span> <span style="color: #2b91af;">RouteLinker</span>(<span style="color: blue;">this</span>.Request);</pre> </p> <p> With this locally scoped variable you can start creating links to other resources: </p> <p> <pre style="margin: 0px;">Href = linker.GetUri&lt;<span style="color: #2b91af;">NoDIController</span>&gt;(r =&gt; r.Get(id)).ToString()</pre> </p> <p> That seems easy, so why make it hard than that? Well, it's easy as long as you have <em>only</em> a single, <a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api">default route</a> in your web service. As soon as you add more routes, you'll need to help Hyprlinkr a bit by providing a custom IRouteDispatcher. That goes as the second argument in a constructor overload: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> linker = <span style="color: blue;">new</span> <span style="color: #2b91af;">RouteLinker</span>(<span style="color: blue;">this</span>.Request, ??);</pre> </p> <p> The question is: how do you create an instance of the desired IRouteDispatcher? You could do it inline every time you want to create an instance of RouteLinker: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> linker = <span style="color: blue;">new</span> <span style="color: #2b91af;">RouteLinker</span>(<span style="color: blue;">this</span>.Request, <span style="color: blue;">new</span> <span style="color: #2b91af;">MyCustomRouteDispatcher</span>());</pre> </p> <p> However, that's starting to look less than DRY. This is where many people might consider creating an extension method which creates a RouteLinker instance from an HttpRequestMessage instance. Now what if you need to supply a configuration value to the custom route dispatcher? Should you pull it from app.config straight from within your extension method? Then what if you need to be able to vary that configuration value from a unit test? This could lead toward an unmaintainable mess quickly. Perhaps it would be better injecting the dependency after all... </p> <h3 id="9079a7201318443589daed43f71a8504"> IResourceLinker as a polymorphic dependency <a href="#9079a7201318443589daed43f71a8504" title="permalink">#</a> </h3> <p> The RouteLinker class actually implements an interface (IResourceLinker) so would it be worthwhile to inject it as a polymorphic interface? This is possible, but actually leads to more trouble. The problem is that due to its signature, it's damn hard to unit test. The interface looks like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IResourceLinker</span> { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Uri</span> GetUri&lt;T&gt;(<span style="color: #2b91af;">Expression</span>&lt;Action&lt;T&gt;&gt; method); }</pre> </p> <p> That may at first glance look innocuous, but is actually quite poisonous. The first issue is that it's impossible to define proper setups when using dynamic mocks. This is because of the Expression parameter. The problem is that while the following <a href="http://code.google.com/p/moq/">Moq</a> setup compiles, it can never work: </p> <p> <pre style="margin: 0px;">linkerStub &nbsp;&nbsp;&nbsp; .Setup(x =&gt; x.GetUri&lt;<span style="color: #2b91af;">ArtistController</span>&gt;(c =&gt; c.Get(artistId))) &nbsp;&nbsp;&nbsp; .Returns(uri);</pre> </p> <p> The problem is that the expression passed into the Setup method isn't the same as the expression used in the SUT. It may <em>look</em> like the same expression, but it's not. Most of the expression tree actually <em>is</em> the same, but the problem is the leaf of the tree. The leaf of the expression tree is the reference to the <em>artistId</em> variable. This is a test variable, while in the SUT it's a variable which is internal to the SUT. While the values of both variables are expected to be the same, the variables themselves aren't. </p> <p> It might be possible to write a custom equality comparer that picks expression trees apart in order to compare the values of leaf nodes, but that could become messy very quickly. </p> <p> The only option seems to define Setups like this: </p> <p> <pre style="margin: 0px;">linkerStub &nbsp;&nbsp;&nbsp; .Setup(x =&gt; x.GetUri(<span style="color: #2b91af;">It</span>.IsAny&lt;<span style="color: #2b91af;">Expression</span>&lt;Action&lt;<span style="color: #2b91af;">ArtistController</span>&gt;&gt;&gt;())) &nbsp;&nbsp;&nbsp; .Returns(uri);</pre> </p> <p> That sort of defies the purpose of a dynamic Test Double... </p> <p> That's not the only problem with the IResourceLinker interface. The other problem is the return type. Since Uri doesn't have a default constructor, it's necessary to tell Moq what to return when the GetUri method is called. While the default behavior of Moq is to return null if no matching Setups were found, I never allow null in my code, so I always change Moq's behavior to return something proper instead. However, this has the disadvantage that if there's no matching Setup when the SUT attempts to invoke the GetUri method, Moq will throw an exception because there's no default constructor for Uri and it doesn't know what else to return. </p> <p> This leads to <a href="http://xunitpatterns.com/fixture%20setup.html">Fixture Setup</a> code like this: </p> <p> <pre style="margin: 0px;">linkerStub &nbsp;&nbsp;&nbsp; .Setup(x =&gt; x.GetUri(<span style="color: #2b91af;">It</span>.IsAny&lt;<span style="color: #2b91af;">Expression</span>&lt;Action&lt;<span style="color: #2b91af;">ArtistController</span>&gt;&gt;&gt;())) &nbsp;&nbsp;&nbsp; .Returns(uri); linkerStub &nbsp;&nbsp;&nbsp; .Setup(x =&gt; x.GetUri(<span style="color: #2b91af;">It</span>.IsAny&lt;<span style="color: #2b91af;">Expression</span>&lt;Action&lt;<span style="color: #2b91af;">ArtistAlbumsController</span>&gt;&gt;&gt;())) &nbsp;&nbsp;&nbsp; .Returns(uri); linkerStub &nbsp;&nbsp;&nbsp; .Setup(x =&gt; x.GetUri(<span style="color: #2b91af;">It</span>.IsAny&lt;<span style="color: #2b91af;">Expression</span>&lt;Action&lt;<span style="color: #2b91af;">ArtistTracksController</span>&gt;&gt;&gt;())) &nbsp;&nbsp;&nbsp; .Returns(uri); linkerStub &nbsp;&nbsp;&nbsp; .Setup(x =&gt; x.GetUri(<span style="color: #2b91af;">It</span>.IsAny&lt;<span style="color: #2b91af;">Expression</span>&lt;Action&lt;<span style="color: #2b91af;">SimilarArtistsController</span>&gt;&gt;&gt;())) &nbsp;&nbsp;&nbsp; .Returns(uri);</pre> </p> <p> ...and that's just to prevent the unit test from crashing. Each and every unit test that hits the same method <em>must</em> have this Setup because the SUT method internally invokes the GetUri method four times with four different parameters. This is pure noise and isn't even part of the test case itself. The tests become very brittle. </p> <p> If only there was a better way... </p> <h3 id="dfef60d799824217aa427ed6af13049d"> RouteLinker as a concrete dependency <a href="#dfef60d799824217aa427ed6af13049d" title="permalink">#</a> </h3> <p> What would happen if you inject the concrete RouteLinker class into other classes? This might look like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">RouteLinker</span> linker; &nbsp; <span style="color: blue;">public</span> HomeController( &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">RouteLinker</span> linker) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.linker = linker; }</pre> </p> <p> Creating links from within the Controller is similar to before: </p> <p> <pre style="margin: 0px;">Href = <span style="color: blue;">this</span>.linker.GetUri&lt;<span style="color: #2b91af;">HomeController</span>&gt;(r =&gt; r.GetHome()).ToString(),</pre> </p> <p> What about unit testing? Well, since the GetUri method is strictly deterministic, given the same input, it will always produce the same output. Thus, from a unit test, you only have to ask the instance of RouteLinker injected into the SUT what it would return if invoked with a specific input. Then you can compare this expected output with the actual output. </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>, <span style="color: #2b91af;">AutoUserData</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> GetHomeReturnsResultWithCorrectSelfLink( &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Frozen</span>]<span style="color: #2b91af;">RouteLinker</span> linker, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">HomeController</span> sut) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> actual = sut.GetHome(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">AtomLinkModel</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Href = linker.GetUri&lt;<span style="color: #2b91af;">HomeController</span>&gt;(r =&gt; r.GetHome()).ToString(), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Rel = <span style="color: #a31515;">"self"</span> &nbsp;&nbsp;&nbsp; }.AsSource().OfLikeness&lt;<span style="color: #2b91af;">AtomLinkModel</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.True(actual.Links.Any(expected.Equals)); }</pre> </p> <p> In this test, you <a href="/2010/03/17/AutoFixtureFreeze.aspx">Freeze</a> the RouteLinker instance, which means that the linker variable is the same instance as the RouteLinker injected into the SUT. Next, you ask that RouteLinker instance what it would produce when invoked in a particular way, and since AtomLinkModel doesn't override Equals, you produce a <a href="/2012/06/22/ResemblanceAndLikeness.aspx">Likeness</a> from the AtomLinkModel and verify that the actual collection of links contains the expected link. </p> <p> That's much more precise than those horribly forgiving It.IsAny constraints. The other advantage is also that you don't have to care about Setups of methods you don't care about in a particular test case. The SUT can invoke the GetUri method as many times as it wants, with as many different arguments as it likes, and the test is never going to break because of that. Since the real implementation is injected, it always works without further Setup. </p> <p> Granted, strictly speaking these aren't unit tests any longer, but rather <a href="/2012/06/27/FacadeTest.aspx">Facade Tests</a>. </p> <p> This technique works because the GetUri method is deterministic and has no side-effects. Thus, it's very similar to Function Composition in Functional languages. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The order of AutoFixture Customizations matter https://blog.ploeh.dk/2012/07/31/TheorderofAutoFixtureCustomizationsmatter 2012-07-31T16:31:11+00:00 Mark Seemann <div id="post"> <p> <em>This post answers a FAQ about ordering of AutoFixture Customizations</em> </p> <p> With <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> you can <a href="/2011/03/18/EncapsulatingAutoFixtureCustomizations.aspx">encapsulate common Customizations</a> using the Customize method and the ICustomization interface. However, customizations may 'compete' for the same requests in the sense that more than one customization is able to handle a request. </p> <p> As an example, consider a request for something as basic as IEnumerable&lt;T&gt;. By default, AutoFixture can't create instances of IEnumerable&lt;T&gt;, but more than one customization can. </p> <p> As <a href="/2011/02/08/CreatingGeneralPopulatedListsWithAutoFixture.aspx">previously described</a> the MultipleCustomization handles requests for sequences just fine: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize(<span style="color: blue;">new</span> <span style="color: #2b91af;">MultipleCustomization</span>()); <span style="color: blue;">var</span> seq = fixture.CreateAnonymous&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">int</span>&gt;&gt;();</pre> </p> <p> However, the <a href="/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx">AutoMoqCustomization</a> can also (sort of) create sequences: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize(<span style="color: blue;">new</span> <span style="color: #2b91af;">AutoMoqCustomization</span>()); <span style="color: blue;">var</span> seq = fixture.CreateAnonymous&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">int</span>&gt;&gt;();</pre> </p> <p> However, in this case, the <em>implementation</em> of IEnumerable&lt;int&gt; is a dynamic proxy, so it's not much of a sequence after all. </p> <h3 id="ee2e5bad0ea04ae5b6507a9127f76577"> Mocking IEnumerable&lt;T&gt; <a href="#ee2e5bad0ea04ae5b6507a9127f76577" title="permalink">#</a> </h3> <p> Here I need to make a little digression on why that is, because this seems to confuse a lot of people. Consider what a dynamic mock object is: it's a dynamic proxy which implements an interface (or abstract base class). It doesn't have a lot of implemented <em>behavior</em>. Dynamic mocks do what we tell them through their configuration APIs (such as the Setup methods for Moq). If we don't tell them what to do, they must fall back on some sort of default implementation. When the AutoMoqCustomization is used, it sets Mock&lt;T&gt;.DefaultValue to DefaultValue.Mock, which means that the default behavior is to return a new dynamic proxy for reference types. </p> <p> Here's how an unconfigured dymamic proxy of IEnumerable&lt;T&gt; will behave: the interface only has two (very similar) methods: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">out</span> T&gt; : IEnumerable { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">IEnumerator</span>&lt;T&gt; GetEnumerator(); }</pre> </p> <p> Via IEnumerable the interface also defines the non-generic GetEnumerator method, but it's so similar to the generic GetEnumerator method that the following discussion applies for both. </p> <p> When you iterate over IEnumerable&lt;T&gt; using foreach, or when you use LINQ, the first thing that happens is that the GetEnumerator method is called. An unconfigured dynamic mock will respond by returning another dynamic proxy implementing IEnumerator&lt;T&gt;. This interface directly and indirectly defines these methods: </p> <p> <pre style="margin: 0px;">T Current { <span style="color: blue;">get</span>; } &nbsp; <span style="color: blue;">object</span> IEnumerator.Current { <span style="color: blue;">get</span>; } &nbsp; <span style="color: blue;">bool</span> MoveNext(); &nbsp; <span style="color: blue;">void</span> Reset(); &nbsp; <span style="color: blue;">void</span> Dispose();</pre> </p> <p> Iterating over a sequence will typically start by invoking the MoveNext method. Since the dynamic proxy is unconfigured, it has to fall back to default behavior. For booleans the default value is false, so the return value of a call to MoveNext would be false. This means that there are no more elements in the sequence. Iteration stops even before it begins. In effect, such an implementation would look like an empty sequence. </p> <p> OK, back to AutoFixture. </p> <h3 id="ee5f799b69dc4fc9b935784ee881ea67"> Ordering Customizations <a href="#ee5f799b69dc4fc9b935784ee881ea67" title="permalink">#</a> </h3> <p> Frequently I receive questions like this: </p> <blockquote> <p> "Creating lists with AutoFixture seems inconsistent. When MultipleCustomization comes before AutoMoqCustomization, lists are popuplated, but the other way around they are empty. Is this a bug?" </p> </blockquote> <p> No, this is by design. By now, you can probably figure out why. </p> <p> Still, lets look at the symptoms. Both of these tests pass: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnlyMultipleResolvingSequence() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize(<span style="color: blue;">new</span> <span style="color: #2b91af;">MultipleCustomization</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> seq = fixture.CreateAnonymous&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">int</span>&gt;&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.NotEmpty(seq); } &nbsp; [<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> OnlyAutoMoqResolvingSequence() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize(<span style="color: blue;">new</span> <span style="color: #2b91af;">AutoMoqCustomization</span>()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> seq = fixture.CreateAnonymous&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">int</span>&gt;&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Empty(seq); }</pre> </p> <p> Notice that in the first test, the sequence is <em>not</em> empty, whereas in the second test, the sequence <em>is</em> empty. This is because the MultipleCustomization produces a 'proper' sequence, while the AutoMoqCustomization produces a dynamic proxy of IEnumerable&lt;int&gt; as described above. At this point, this should hardly be surprising. </p> <p> The same obvervations can be made when both Customizations are in use: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> WrongOrderResolvingSequence() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">CompositeCustomization</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AutoMoqCustomization</span>(), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MultipleCustomization</span>())); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> seq = fixture.CreateAnonymous&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">int</span>&gt;&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.Empty(seq); } &nbsp; [<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> CorrectOrderResolvingSequnce() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> fixture = <span style="color: blue;">new</span> <span style="color: #2b91af;">Fixture</span>().Customize( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">CompositeCustomization</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">MultipleCustomization</span>(),&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AutoMoqCustomization</span>())); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> seq = fixture.CreateAnonymous&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">int</span>&gt;&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.NotEmpty(seq); }</pre> </p> <p> Both of these tests also pass. In the first test the sequence is empty, and in the second it contains elements. This is because the first Customization 'wins'. </p> <p> In general, a Customization may potentially be able to handle a lot of requests. For instance, the AutoMoqCustomization can handle all requests for interfaces and abstract base classes. Thus, multiple Customizations may be able to handle a request, so AutoFixture needs a conflict resolution strategy. That strategy is simply that the first Customization which can handle a request gets to do that, and the other Customizations are never invoked. You can use this feature to put specific Customizations in front of more catch-all Customizations. That's essentially what happens when you put MultipleCustomization in front of AutoMoqCustomization. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. FizzBuzz kata in F#: stage 2 https://blog.ploeh.dk/2012/07/25/FizzBuzzkatainF#stage2 2012-07-25T09:05:09+00:00 Mark Seemann <div id="post"> <p> In <a href="/2012/07/20/FizzBuzzKataInFStage1.aspx">my previous post</a> I walked you through stage 1 of the <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataFizzBuzz">FizzBuzz kata</a>. In this post I'll walk you through stage 2 of the kata, where new requirements are introduced (see <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataFizzBuzz">the kata itself</a> for details). This makes the implementation much more complex. </p> <h3 id="e5660b3586644618bef4f607c3f650b8"> Unit test <a href="#e5660b3586644618bef4f607c3f650b8" title="permalink">#</a> </h3> <p> In order to meet the new requirements, I first modified and expanded my existing test cases: </p> <p> <pre style="margin: 0px;">[&lt;Theory&gt;] [&lt;InlineData(1, <span style="color: maroon;">"1"</span>)&gt;] [&lt;InlineData(2, <span style="color: maroon;">"2"</span>)&gt;] [&lt;InlineData(3, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(4, <span style="color: maroon;">"4"</span>)&gt;] [&lt;InlineData(5, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(6, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(7, <span style="color: maroon;">"7"</span>)&gt;] [&lt;InlineData(8, <span style="color: maroon;">"8"</span>)&gt;] [&lt;InlineData(9, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(10, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(11, <span style="color: maroon;">"11"</span>)&gt;] [&lt;InlineData(12, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(13, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(14, <span style="color: maroon;">"14"</span>)&gt;] [&lt;InlineData(15, <span style="color: maroon;">"FizzBuzz"</span>)&gt;] [&lt;InlineData(16, <span style="color: maroon;">"16"</span>)&gt;] [&lt;InlineData(17, <span style="color: maroon;">"17"</span>)&gt;] [&lt;InlineData(18, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(19, <span style="color: maroon;">"19"</span>)&gt;] [&lt;InlineData(20, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(30, <span style="color: maroon;">"FizzBuzz"</span>)&gt;] [&lt;InlineData(31, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(32, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(33, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(34, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(35, <span style="color: maroon;">"FizzBuzz"</span>)&gt;] [&lt;InlineData(36, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(37, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(38, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(39, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(50, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(51, <span style="color: maroon;">"FizzBuzz"</span>)&gt;] [&lt;InlineData(52, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(53, <span style="color: maroon;">"FizzBuzz"</span>)&gt;] [&lt;InlineData(54, <span style="color: maroon;">"FizzBuzz"</span>)&gt;] [&lt;InlineData(55, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(56, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(57, <span style="color: maroon;">"FizzBuzz"</span>)&gt;] [&lt;InlineData(58, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(59, <span style="color: maroon;">"Buzz"</span>)&gt;] <span style="color: blue;">let</span> FizzBuzzReturnsCorrectResult number expected = &nbsp;&nbsp;&nbsp; number &nbsp;&nbsp;&nbsp; |&gt; FizzBuzz &nbsp;&nbsp;&nbsp; |&gt; should equal expected</pre> </p> <p> This is the same test code <a href="/2012/07/20/FizzBuzzKataInFStage1.aspx">as before</a>, only with new or modified test data. </p> <h3 id="38ad44a14542498eb42d2e8e92766564"> Implementation <a href="#38ad44a14542498eb42d2e8e92766564" title="permalink">#</a> </h3> <p> Compared with the stage 1 implementation, my implementation to meet the new requirements is much more complex. First, I'll post the entire code listing and then walk you through the details: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> FizzBuzz number = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> arithmeticFizzBuzz number = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> number % 3 = 0 <span style="color: blue;">then</span> <span style="color: blue;">yield</span> <span style="color: maroon;">"Fizz"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> number % 5 = 0 <span style="color: blue;">then</span> <span style="color: blue;">yield</span> <span style="color: maroon;">"Buzz"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> digitalFizzBuzz digit = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> digit = 3 <span style="color: blue;">then</span> <span style="color: blue;">yield</span> <span style="color: maroon;">"Fizz"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> digit = 5 <span style="color: blue;">then</span> <span style="color: blue;">yield</span> <span style="color: maroon;">"Buzz"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> <span style="color: blue;">rec</span> digitize number = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">yield</span> number % 10 &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> aTenth = number / 10 &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> aTenth &gt;= 1 <span style="color: blue;">then</span> <span style="color: blue;">yield!</span> digitize aTenth &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> arithmeticFizzBuzzes = number |&gt; arithmeticFizzBuzz &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> digitalFizzBuzzes = number &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; digitize &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.collect digitalFizzBuzz &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> fizzOrBuzz = arithmeticFizzBuzzes &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.append digitalFizzBuzzes &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.distinct &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.toArray &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Array.sort &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Array.rev &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; String.Concat &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> fizzOrBuzz = <span style="color: maroon;">""</span> &nbsp;&nbsp;&nbsp; <span style="color: blue;">then</span> number.ToString() &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> fizzOrBuzz</pre> </p> <p> First of all, you may wonder where the original implementation went. According to the requirements, the function must still 'Fizz' or 'Buzz' when a number is divisible by 3 or 5. This is handled by the nested <em>arithmeticFizzBuzz</em> function: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> arithmeticFizzBuzz number = &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> number % 3 = 0 <span style="color: blue;">then</span> <span style="color: blue;">yield</span> <span style="color: maroon;">"Fizz"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> number % 5 = 0 <span style="color: blue;">then</span> <span style="color: blue;">yield</span> <span style="color: maroon;">"Buzz"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> </p> <p> The <em>seq</em> symbol specifies a <a href="http://msdn.microsoft.com/en-us/library/dd233209.aspx">sequence expression</a>, which means that everything within the curly brackets is expected to produce parts of a sequence. It works a bit like the <a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx">yield</a> keyword in C#. </p> <p> Due to F#'s strong type inference, the type of the function is <em>int -&gt; seq&lt;string&gt;</em>, which means that it takes an integer as input and returns a sequence of strings. In C# an equivalent signature would be <em>IEnumerable&lt;string&gt; arithmeticFizzBuzz(int number)</em>. This function produces a sequence of strings depending on the input. </p> <p> <ul> <li>1 produces an empty sequence.</li> <li>2 produces an empty sequence.</li> <li>3 produces a sequence containing the single string "Fizz".</li> <li>4 produces an empty seqence.</li> <li>5 produces a sequence containing the single string "Buzz".</li> <li>6 produces a sequence containing the single string "Fizz".</li> <li>15 produces a sequence containing the strings "Fizz" and "Buzz" (in that order).</li> </ul> <p> That doesn't <em>quite</em> sound like the original requirements, but the trick will be to concatenate the strings. Thus, an empty sequence will be "", "Fizz" will be "Fizz", "Buzz" will be "Buzz", but "Fizz" and "Buzz" will become "FizzBuzz". </p> <p> The <em>digitalFizzBuzz</em> function works in much the same way, but expects only a single digit. </p> <p> </p> <pre style="margin: 0px;"><span style="color: blue;">let</span> digitalFizzBuzz digit = &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> digit = 3 <span style="color: blue;">then</span> <span style="color: blue;">yield</span> <span style="color: maroon;">"Fizz"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> digit = 5 <span style="color: blue;">then</span> <span style="color: blue;">yield</span> <span style="color: maroon;">"Buzz"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <p> <ul> <li>1 produces an empty sequence.</li> <li>2 produces an empty sequence.</li> <li>3 produces a sequence containing the single string "Fizz".</li> <li>4 produces an empty seqence.</li> <li>5 produces a sequence containing the single string "Buzz".</li> <li>6 produces an empty sequence.</li> </ul> <p> In order to be able to apply the new rule of Fizzing and Buzzing if a digit is 3 or 5, it's necessary to split a number into digits. This is done by the recursive <em>digitize</em> function: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> <span style="color: blue;">rec</span> digitize number = &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">yield</span> number % 10 &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> aTenth = number / 10 &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> aTenth &gt;= 1 <span style="color: blue;">then</span> <span style="color: blue;">yield!</span> digitize aTenth &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> </p> <p> This function works recursively by first yielding the rest of a division by 10, and then calling itself recursively with a tenth of the original number. Since the number is an integer, the division simply still produces an integer. The function produces a sequence of digits, but in a sort of backwards way. </p> <ul> <li>1 produces a sequence containing 1.</li> <li>2 produces a sequence containing 2.</li> <li>12 produces a sequence containing 2 followed by 1.</li> <li>23 produces a sequence containing 3 followed by 2.</li> <li>148 produces 8, 4, 1.</li> </ul> <p> This provides all the building blocks. To get the arithmetic (original) FizzBuzzes, the number is piped into the arithmeticFizzBuzz function: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> arithmeticFizzBuzzes = number |&gt; arithmeticFizzBuzz</pre> </p> <p> In order to get the digital (new) FizzBuzzes, the number is first piped into the digitize function, and the resulting sequence of digits is then piped to the digitalFizzBuzz function by way of the Seq.collection function. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> digitalFizzBuzzes = number &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; digitize &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.collect digitalFizzBuzz</pre> </p> <p> The Seq.collect function is a built-in function that takes a sequence of elements (in this case a sequence of digits) and for each element calls a method that produces a sequence of elements, and then concatenates all the produced sequences. As an example, consider the number 53. </p> <p> Calling digitize with the number 53 produces the sequence { 3; 5 }. Calling digitalFizzBuzz with 3 produces the sequence { "Fizz" } and calling digitalFizzBuzz with 5 produces { "Buzz" }. Seq.collect concatenates these <em>two</em> sequences to produce the <em>single</em> sequence { "Fizz"; "Buzz" }. </p> <p> Now we have two sequences of "Fizz" or "Buzz" strings - one produced by the old, arithmetic function, and one produced by the new, digital function. These two sequences can now be merged and ordered with the purpose of producing a single string: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> fizzOrBuzz = arithmeticFizzBuzzes &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.append digitalFizzBuzzes &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.distinct &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.toArray &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Array.sort &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Array.rev &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; String.Concat</pre> </p> <p> First, the Seq.append function simply concatenates the two sequences into a single sequence. This could potentially result in a sequence like this: { "Fizz"; "Buzz"; "Fizz" }. The Seq.distinct function gets rid of the duplicates, but the ordering may be wrong - the sequence may look like this: { "Buzz"; "Fizz" }. This can be fixed by sorting the sequence, but sorting alphabetically would always put "Buzz" before "Fizz" so it's also necessary to reverse the sequence. There's no function in the <a href="http://msdn.microsoft.com/en-us/library/ee353635.aspx">Seq module</a> which can reverse a sequence, so first the Seq.toArray function is used to convert the sequence to an array. After sorting and reversing the array, the result is one of four arrays: [], [ "Fizz" ], [ "Buzz" ], or [ "Fizz"; "Buzz" ]. The last step is to concatenate these string arrays to a single string using the <a href="http://msdn.microsoft.com/en-us/library/0wkb0y3w.aspx">String.Concat</a> BCL method. </p> <p> If there were no Fizzes or Buzzes, the string will be empty, in which case the number is converted to a string and returned; otherwise, the fizzOrBuzz string is returned. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">if</span> fizzOrBuzz = <span style="color: maroon;">""</span> <span style="color: blue;">then</span> number.ToString() <span style="color: blue;">else</span> fizzOrBuzz</pre> </p> <p> To print the FizzBuzz list for numbers from 1 to 100 the <a href="/2012/07/20/FizzBuzzKataInFStage1.aspx">same solution as before</a> can be used. </p> <p> What I like about Functional Programming is that data just flows through the function. There's not state and no mutation - only operations on sequences of data. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. FizzBuzz kata in F#: stage 1 https://blog.ploeh.dk/2012/07/20/FizzBuzzkatainF#stage1 2012-07-20T05:26:12+00:00 Mark Seemann <div id="post"> <p> In <a href="/2012/06/04/BankOCRKataInFUserStories3And4.aspx">previous posts</a> I've walked through the <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR">Bank OCR kata</a> in F#. In this post, I will do the same for the first stage of the very simple <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataFizzBuzz">FizzBuzz kata</a>. This is a very simple kata, so if you already know F#, there will be nothing new to see here. On the other hand, if you've yet to be exposed to F#, this is a good place to start - I'll attempt to walk you through the code assuming that you don't know F# yet. </p> <h3 id="3340b9506c2b47e19d6cca9cf927b5c2"> Unit test <a href="#3340b9506c2b47e19d6cca9cf927b5c2" title="permalink">#</a> </h3> <p> Since I developed the solution using Test-Driven Development, I started by writing a single Parameterized Test: </p> <p> <pre style="margin: 0px;">[&lt;Theory&gt;] [&lt;InlineData(1, <span style="color: maroon;">"1"</span>)&gt;] [&lt;InlineData(2, <span style="color: maroon;">"2"</span>)&gt;] [&lt;InlineData(3, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(4, <span style="color: maroon;">"4"</span>)&gt;] [&lt;InlineData(5, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(6, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(7, <span style="color: maroon;">"7"</span>)&gt;] [&lt;InlineData(8, <span style="color: maroon;">"8"</span>)&gt;] [&lt;InlineData(9, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(10, <span style="color: maroon;">"Buzz"</span>)&gt;] [&lt;InlineData(11, <span style="color: maroon;">"11"</span>)&gt;] [&lt;InlineData(12, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(13, <span style="color: maroon;">"13"</span>)&gt;] [&lt;InlineData(14, <span style="color: maroon;">"14"</span>)&gt;] [&lt;InlineData(15, <span style="color: maroon;">"FizzBuzz"</span>)&gt;] [&lt;InlineData(16, <span style="color: maroon;">"16"</span>)&gt;] [&lt;InlineData(17, <span style="color: maroon;">"17"</span>)&gt;] [&lt;InlineData(18, <span style="color: maroon;">"Fizz"</span>)&gt;] [&lt;InlineData(19, <span style="color: maroon;">"19"</span>)&gt;] [&lt;InlineData(20, <span style="color: maroon;">"Buzz"</span>)&gt;] <span style="color: blue;">let</span> FizzBuzzReturnsCorrectResult number expected = &nbsp;&nbsp;&nbsp; number &nbsp;&nbsp;&nbsp; |&gt; FizzBuzz &nbsp;&nbsp;&nbsp; |&gt; should equal expected</pre> </p> <p> This test uses <a href="http://xunit.codeplex.com/">xUnit.net</a> data theories to provide a set of test data in the form of an integer as input and an expected string. </p> <p> The <em>number</em> input variable is piped to the <em>FizzBuzz</em> function, using F#'s pipe operator |&gt;. This is just another way of writing </p> <p> <pre style="margin: 0px;">FizzBuzz number</pre> </p> <p> The pipe operator simply takes the data being piped and uses it as the last input parameter to the function being piped. In this case, the <em>number</em> integer variable is the data being piped, so it's used as the last input parameter to the <em>FizzBuzz</em> function, which only takes a single paramter. </p> <p> The result of invoking the <em>FizzBuzz</em> function is a string. This result is again piped to the <em>should</em> method, which is defined by the <a href="http://fsunit.codeplex.com/">FsUnit</a> module. The <em>should</em> method is an assertion function that takes three input parameters. The first two parameters are supplied as part of the function invokation as <em>equal expected</em>, but since the pipe operator is being used, the third and final parameter value is the result of invoking the <em>FizzBuzz</em> function. </p> <p> In all, the test states that when the <em>FizzBuzz</em> function is called with <em>number</em>, the result should be equal to the <em>expected</em> string. </p> <h3 id="aa25afac6e7b428a980b30d4d3f05aff"> Implementation <a href="#aa25afac6e7b428a980b30d4d3f05aff" title="permalink">#</a> </h3> <p> The FizzBuzz implementation is really simple: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> FizzBuzz number = &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> number <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; | i <span style="color: blue;">when</span> i % 3 = 0 &amp;&amp; i % 5 = 0 <span style="color: blue;">-&gt;</span> <span style="color: maroon;">"FizzBuzz"</span> &nbsp;&nbsp;&nbsp; | i <span style="color: blue;">when</span> i % 3 = 0 <span style="color: blue;">-&gt;</span> <span style="color: maroon;">"Fizz"</span> &nbsp;&nbsp;&nbsp; | i <span style="color: blue;">when</span> i % 5 = 0 <span style="color: blue;">-&gt;</span> <span style="color: maroon;">"Buzz"</span> &nbsp;&nbsp;&nbsp; | _ <span style="color: blue;">-&gt;</span> number.ToString()</pre> </p> <p> All it does is to use <a href="http://msdn.microsoft.com/en-us/library/dd547125.aspx">pattern matching</a> against the <em>number</em> input argument. In all cases except the last one, the value of <em>number</em> is matched against any number, but with a condition. The first condition is that the number should be divible by both 3 and 5. If this is the case, the result to the right of the -&gt; operator is returned from the function ("FizzBuzz"). </p> <p> The last line of the match block uses an underscore as the match pattern. This is a catch-all pattern that's being triggered if none of the other patterns are matched. In this case, the <em>number</em> input argument is converted to a string and returned. </p> <h3 id="d15c37e0cf5f4766b0ab84a636c7775b"> Printing all lines <a href="#d15c37e0cf5f4766b0ab84a636c7775b" title="permalink">#</a> </h3> <p> The kata requires me to print the output for all numbers from 1 to 100. The astute reader may have noticed that the FizzBuzz function doesn't do that - it only converts a single integer to a string. However, printing all numbers fizzed and buzzed is easy: </p> <p> <pre style="margin: 0px;">[1..100] |&gt; List.map FizzBuzz |&gt; List.reduce (sprintf <span style="color: maroon;">"%s\r\n%s"</span>)</pre> </p> <p> The first line defines a list of numbers from 1 to 100. The next line pipes this list of integers into the List.map function, which applies the FizzBuzz function to each integer. The output of this function call is another list of strings ["1"; "2"; "Fizz"; "4"; "Buzz"; etc.]. This list of strings is piped into the List.reduce function, which in this case uses the sprintf function to concatenate the strings and add a line break after each element, so that it formats correctly. </p> <p> The List.reduce function applies a function to pairwise elements in a list in order to produce a new element of the same type. Consider the beginning of the list of strings ["1"; "2"; "Fizz"; "4"; "Buzz"; etc.]. The List.reduce function starts with "1" and "2" and applies a function in order to produce a new string from those two strings. That function is the sprintf function, which is similar to the more well-known String.Format method in the BCL. In this case, the template is to take the two strings and insert a line break between them. Thus, when applied to "1" and "2", the result is </p> <p> <pre style="margin: 0px;"><span style="color: maroon;">1 2</span></pre> </p> <p> (notice the line break). Now, the List.reduce function takes that string and the next string in the list ("Fizz") and applies the funtion again, giving this result: </p> <p> <pre style="margin: 0px;"><span style="color: maroon;">1</span> <span style="color: maroon;">2</span> <span style="color: maroon;">Fizz</span></pre> </p> <p> It now takes this string and the next value ("4") and applies the sprintf function once more, etc. This is how the final list is being printed. </p> <p> In <a href="/2012/07/25/FizzBuzzKataInFStage2.aspx">a future post</a> I'll walk you through stage 2 of the kata. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="953495e565cc445ab27d38c1ed9a9ef1"> <div class="comment-author"><a href="http://www.skov-boisen.dk">Simon Skov Boisen</a> <a href="#953495e565cc445ab27d38c1ed9a9ef1">#</a></div> <div class="comment-content">One difference between sprintf and String.Format that is worth mentioning is that sprintf offers typesafety - it won't let you pass an integer to a %s since %s represents a string - it's a compile time error. More information available at http://msdn.microsoft.com/en-us/library/ee370560.aspx.</div> <div class="comment-date">2012-08-06 11:03 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Hyprlinkr https://blog.ploeh.dk/2012/07/18/Hyprlinkr 2012-07-18T09:46:48+00:00 Mark Seemann <div id="post"> <p>This post serves as an announcement that my latest open source project <a href="https://github.com/ploeh/Hyprlinkr">Hyprlinkr</a> is available. It's a very sharply focused helper library for the <a href="http://www.asp.net/web-api">ASP.NET Web API</a>, enabling you to create type-safe hyperlinks between resources in a RESTful API.</p> <p>It's basically a reusable package of <a href="/2012/04/17/HyperlinkingWithTheASPNETWebAPI.aspx">the RouteLinker class I previously presented as a spike</a>. The <a href="http://joseoncode.com/2011/03/18/wcf-web-api-strongly-typed-resource-linker/">original idea</a> wasn't mine, but <a href="http://joseoncode.com/">José F. Romaniello's</a> - I just took the idea and created a project out of it.</p> <p>The library is mostly useful if you build <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">level 3</a> RESTful APIs with the ASP.NET Web API.</p> <p>It's available <a href="https://nuget.org/packages/Hyprlinkr">as a NuGet package</a>.</p> <p>Apart from that, I'll refer you to <a href="https://github.com/ploeh/Hyprlinkr">the project site</a> for more information.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="033d321d5fd9493ea7de32473b748552"> <div class="comment-author"><a href="http://antix.co.uk">Anthony Johnston</a> <a href="#033d321d5fd9493ea7de32473b748552">#</a></div> <div class="comment-content">Thanks for this Mark, it will surely come in use<br> <br> The one thing I am struggling to reconcile, you must have some insight on, is the addition of the link properties to the model. <br> <br> I am thinking that hyperlinks should be a responsibility of the serializer and not the model...</div> <div class="comment-date">2012-07-18 11:23 UTC</div> </div> <div class="comment" id="58a0a0241e6143558a90d43336efc4fd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#58a0a0241e6143558a90d43336efc4fd">#</a></div> <div class="comment-content">In the cases I can think about right now, I'd say that the Controller (or one of its dependencies) must have the responsibility of adding the hyperlinks. It's an application concern to define what can be linked to. This isn't any different than adding &quot;&lt;a href&quot; links to HTML in a 'normal' web application.</div> <div class="comment-date">2012-07-18 11:45 UTC</div> </div> <div class="comment" id="609a5bde61a3454792c944a650695a97"> <div class="comment-author"><a href="http://joseoncode.com">Jose Romaniello</a> <a href="#609a5bde61a3454792c944a650695a97">#</a></div> <div class="comment-content">thanks for mentioning my article, I really like what you did.<br> <br> At the time I wrote that article i was trying different syntax to create a DSL to express a workflow, like the coffee workflow in &quot;Rest on practice&quot;.<br> <br> i think this is an step forward<br> <br> thanks</div> <div class="comment-date">2012-07-18 23:02 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Primitive Dependencies https://blog.ploeh.dk/2012/07/02/PrimitiveDependencies 2012-07-02T09:26:30+00:00 Mark Seemann <div id="post"> <p> <em>Primitives are also dependencies</em> </p> <p> There are tons of examples of how Dependency Injection (DI) can be used to decouple clients and services. When the subject is DI, the focus tends to be heavily on the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> (LSP), so most people think about dependencies as polymorphic types (interfaces or abstract base classes). Primitive types like strings and integers tend to be ignored or discouraged. It doesn't help that most DI Containers need extra help to deal with such values. </p> <p> Primitives are dependencies, too. It doesn't really matter whether or not they are polymorphic. In the end, a dependency is something that the client <em>depends</em> on - hence the name. It doesn't really matter whether the dependency is an interface, a class or a primitive type. In most object-oriented languages, everything is an object - even integers and booleans (although boxing occurs). </p> <p> There are several ways to inject dependencies into clients. <a href="http://amzn.to/12p90MG">My book</a> describes a set of patterns including Constructor Injection and Property Injection. It's important to keep in mind that ultimately, the reason why Constructor Injection should be your preferred DI pattern has nothing to do with polymorphism. It has to do with <a href="/2011/05/30/DesignSmellDefaultConstructor.aspx">protecting the invariants</a> of the class. </p> <p> Therefore, if the class in question <em>requires</em> a primitive value in order to work, that is a dependency too. Primitive constructor arguments can be mixed with polymorphic arguments. There's really no difference. </p> <h3 id="b2d8ed219c9f43e18d203d1f4f7ad7f2"> Example: a chart reader <a href="#b2d8ed219c9f43e18d203d1f4f7ad7f2" title="permalink">#</a> </h3> <p> Imagine that you're building a service which provides Top 40 music chart data. There's a ChartController which relies on an IChartReader: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ChartController</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">IChartReader</span> chartReader; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ChartController(<span style="color: #2b91af;">IChartReader</span> chartReader) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (chartReader == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"chartReader"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.chartReader = chartReader; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// ...</span> }</pre> </p> <p> One implementation of IChartReader is based on a database, so it requires a connection string (a primitive). It also requires a configuration value which establishes the size of the chart: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">DbChartReader</span> : <span style="color: #2b91af;">IChartReader</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: blue;">int</span> top; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: blue;">string</span> chartConnectionString; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> DbChartReader(<span style="color: blue;">int</span> top, <span style="color: blue;">string</span> chartConnectionString) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (top &lt;= 0) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentOutOfRangeException</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"top"</span>, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #a31515;">"Only positive numbers allowed."</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (chartConnectionString == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"chartConnectionString"</span>); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.top = top; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.chartConnectionString = chartConnectionString; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// ...</span> }</pre> </p> <p> When <em>top</em> has the value 40, the chart is a Top 40 chart; when the value is 10 it's a Top 10 chart; etc. </p> <h3 id="fc21e6a230564623a0b183c5fae8c929"> Unit testing <a href="#fc21e6a230564623a0b183c5fae8c929" title="permalink">#</a> </h3> <p> Obviously, a class like DbChartReader is easy to wire up in a unit test: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Fact</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> UnitTestingExample() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sut = <span style="color: blue;">new</span> <span style="color: #2b91af;">DbChartReader</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; top: 10, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; chartConnectionString: <span style="color: #a31515;">"localhost;foo;bar"</span>); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Act goes here...</span> &nbsp;&nbsp;&nbsp; <span style="color: green;">// Assert goes here...</span> }</pre> </p> <h3 id="a5f4de3155f946b88353fa93f38a80f3"> Hard-coded composition <a href="#a5f4de3155f946b88353fa93f38a80f3" title="permalink">#</a> </h3> <p> When it's time to bootstrap a complete application, one of the advantages of treating primitives as dependencies is that you have many options for how and where you define those values. At the beginning of an application's lifetime, the <a href="http://ayende.com/blog/3545/enabling-change-by-hard-coding-everything-the-smart-way">best option is often to hard-code some or all of the values</a>. This is as easy to do with primitive dependencies as with polymorphic dependencies: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> controller = <span style="color: blue;">new</span> <span style="color: #2b91af;">ChartController</span>( &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DbChartReader</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; top: 40, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; chartConnectionString: <span style="color: #a31515;">"foo"</span>));</pre> </p> <p> This code is part of the application's <a href="/2011/07/28/CompositionRoot.aspx">Composition Root</a>. </p> <h3 id="106907c1fedb4eed8a95f951651aa7af"> Configuration-based composition <a href="#106907c1fedb4eed8a95f951651aa7af" title="permalink">#</a> </h3> <p> If the time ever comes to move the arms of the <a href="http://mikehadlow.blogspot.co.uk/2012/05/configuration-complexity-clock.html">the Configuration Complexity Clock</a> towards using the configuration system, that's easy to do too: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">var</span> topString = <span style="color: #2b91af;">ConfigurationManager</span>.AppSettings[<span style="color: #a31515;">"top"</span>]; <span style="color: blue;">var</span> top = <span style="color: blue;">int</span>.Parse(topString); &nbsp; <span style="color: blue;">var</span> chartConnectionString = <span style="color: #2b91af;">ConfigurationManager</span> &nbsp;&nbsp;&nbsp; .ConnectionStrings[<span style="color: #a31515;">"chart"</span>].ConnectionString; &nbsp; <span style="color: blue;">var</span> controller = <span style="color: blue;">new</span> <span style="color: #2b91af;">ChartController</span>( &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">DbChartReader</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; top, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; chartConnectionString));</pre> </p> <p> This is still part of the Composition Root. </p> <h3 id="4e691babeb8949ed9d17215194a24dfd"> Wiring a DI Container with primitives <a href="#4e691babeb8949ed9d17215194a24dfd" title="permalink">#</a> </h3> <p> Most DI Containers need a little help with primitives. You can configure components with primitives, but you often need to be quite explicit about it. Here's an example of configuring <a href="http://www.castleproject.org/projects/windsor/">Castle Windsor</a>: </p> <p> <pre style="margin: 0px;">container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">ChartController</span>&gt;()); container.Register(<span style="color: #2b91af;">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af;">IChartReader</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af;">DbChartReader</span>&gt;() &nbsp;&nbsp;&nbsp; .DependsOn( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnAppSettingsValue(<span style="color: #a31515;">"top"</span>), &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Dependency</span>.OnValue&lt;<span style="color: blue;">string</span>&gt;( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ConfigurationManager</span>.ConnectionStrings[<span style="color: #a31515;">"chart"</span>] &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .ConnectionString)));</pre> </p> <p> This configures the ChartController type exactly like the previous example, but it's actually more complicated now, and you even lost the <a href="/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx">feedback from the compiler</a>. That's not good, but you can do better. </p> <h3 id="05e1457e2550439aabb6f6b3cc0e9f99"> Conventions for primitives <a href="#05e1457e2550439aabb6f6b3cc0e9f99" title="permalink">#</a> </h3> <p> A DI Container like Castle Windsor enables you define your own conventions. How about these conventions? </p> <ul> <li>If a dependency is a string and it ends with "ConnectionString", the part of the name <em>before</em> "ConnectionString" is the name of an entry in the app.config's <em>connectionStrings</em> element.</li> <li>If a dependency is a primitive (e.g. an integer) the name of the constructor argument is the key to the <em>appSettings</em> entry.</li> </ul> <p> That would be really nice because it means that you can keep on evolving you application by adding code, and it just works. Need a connection string to the 'artist database'? Just add a constructor argument called "artistConnectionString" and a corresponding <em>artist</em> connection string in your app.config. </p> <p> Here's how those conventions could be configured with Castle Windsor: </p> <p> <pre style="margin: 0px;">container.Register(<span style="color: #2b91af;">Classes</span> &nbsp;&nbsp;&nbsp; .FromAssemblyInDirectory(<span style="color: blue;">new</span> <span style="color: #2b91af;">AssemblyFilter</span>(<span style="color: #a31515;">"."</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .FilterByName(an =&gt; an.Name.StartsWith(<span style="color: #a31515;">"Ploeh"</span>))) &nbsp;&nbsp;&nbsp; .Pick() &nbsp;&nbsp;&nbsp; .WithServiceAllInterfaces()); &nbsp; container.Kernel.Resolver.AddSubResolver( &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">ConnectionStringConventions</span>()); container.Kernel.Resolver.AddSubResolver( &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">AppSettingsConvention</span>());&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> </p> <p> The Register call scans all appropriate assemblies in the application's root and registers all components according to the interfaces they implement, while the two sub-resolvers each implement one of the conventions described above. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ConnectionStringConventions</span> : <span style="color: #2b91af;">ISubDependencyResolver</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> CanResolve( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CreationContext</span> context, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ISubDependencyResolver</span> contextHandlerResolver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentModel</span> model, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DependencyModel</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> dependency.TargetType == <span style="color: blue;">typeof</span>(<span style="color: blue;">string</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; dependency.DependencyKey.EndsWith(<span style="color: #a31515;">"ConnectionString"</span>); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> Resolve( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CreationContext</span> context, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ISubDependencyResolver</span> contextHandlerResolver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentModel</span> model, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DependencyModel</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> name = dependency.DependencyKey.Replace(<span style="color: #a31515;">"ConnectionString"</span>, <span style="color: #a31515;">""</span>); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">ConfigurationManager</span>.ConnectionStrings[name].ConnectionString; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The CanResolve method ensures that the Resolve method is only invoked for string dependencies with names ending with "ConnectionString". If that's the case, the connection string is simply read from the app.config file according to the name. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">AppSettingsConvention</span> : <span style="color: #2b91af;">ISubDependencyResolver</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> CanResolve( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CreationContext</span> context, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ISubDependencyResolver</span> contextHandlerResolver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentModel</span> model, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DependencyModel</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> dependency.TargetType == <span style="color: blue;">typeof</span>(<span style="color: blue;">int</span>); <span style="color: green;">// or bool, Guid, etc.</span> &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> Resolve( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">CreationContext</span> context, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ISubDependencyResolver</span> contextHandlerResolver, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentModel</span> model, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DependencyModel</span> dependency) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> appSettingsKey = dependency.DependencyKey; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> s = <span style="color: #2b91af;">ConfigurationManager</span>.AppSettings[appSettingsKey]; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">Convert</span>.ChangeType(s, dependency.TargetType); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This other convention can be used to trigger on primitive dependencies. Since this is a bit of demo code, it only triggers on integers, but I'm sure you'll be able to figure out how to make it trigger on other types as well. </p> <p> Using convention-based techniques like these can turn a DI Container into a very powerful piece of infrastructure. It just sit there, and it just works, and rarely do you have to touch it. As long as all developers follow the conventions, things just work. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1ac0d095d92543929ecf1e63a09b262e"> <div class="comment-author"><a href="http://www.sqlhorror.com">Roopesh Shenoy</a> <a href="#1ac0d095d92543929ecf1e63a09b262e">#</a></div> <div class="comment-content">Convention over configuration - A bit magical, but Nice!</div> <div class="comment-date">2012-07-02 11:18 UTC</div> </div> <div class="comment" id="bfb472e33b7b4a65b3c5d1181b7ab0bb"> <div class="comment-author">Marcus <a href="#bfb472e33b7b4a65b3c5d1181b7ab0bb">#</a></div> <div class="comment-content">Great post!</div> <div class="comment-date">2012-07-04 11:42 UTC</div> </div> <div class="comment" id="4b2bcc92139343989f2b4c5266da384f"> <div class="comment-author">Gary McLean Hall <a href="#4b2bcc92139343989f2b4c5266da384f">#</a></div> <div class="comment-content">Do you know if Unity supports this sort of convention?</div> <div class="comment-date">2012-07-05 20:29 UTC</div> </div> <div class="comment" id="e05c1cdf1531454b8e30f21876246508"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e05c1cdf1531454b8e30f21876246508">#</a></div> <div class="comment-content">It's been almost two years since I last worked with Unity, so I don't know - I can't remember all the details of its API. However, I remember Unity as being quite extensible, actually, so I wouldn't be surprised if you could do something like this.<br> <br> Basically, Unity has very little in terms of Fluent APIs, convention over configuration, etc. but what it <em>does</em> have is a very open architecture. This means it's almost always possible to write a bit of Reflection code to configure Unity by convention.<br> <br> FWIW, there's a chapter in <a href="http://amzn.to/12p90MG">my book</a> about Unity and its extensibility mechanisms, but to be fair, it doesn't cover exactly this scenario.</div> <div class="comment-date">2012-07-06 06:16 UTC</div> </div> <div class="comment" id="51d54e020cb545de9fdc6479bc1938ed"> <div class="comment-author">Gary McLean Hall <a href="#51d54e020cb545de9fdc6479bc1938ed">#</a></div> <div class="comment-content">I haven't gotten that far in your book, yet ;)<br> <br> It appears it is possible, and that someone has created an extension for convention:<br> <br> http://aspiringcraftsman.com/2009/06/13/convention-based-registration-extension/<br> </div> <div class="comment-date">2012-07-06 14:34 UTC</div> </div> <div class="comment" id="9906f04639f84086a884e6a48a8a2f9c"> <div class="comment-author"><a href="http://acazsouza.com.br/">Acaz</a> <a href="#9906f04639f84086a884e6a48a8a2f9c">#</a></div> <div class="comment-content">It seems like a over-engineering to me. Make things simple like in &quot;Wiring a DI Container with primitives&quot; is the best option for me. The other options below seems confuse and unnecessary to me.</div> <div class="comment-date">2012-09-08 22:31 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Facade Test https://blog.ploeh.dk/2012/06/27/FacadeTest 2012-06-27T15:11:08+00:00 Mark Seemann <div id="post"> <p> <em>This post proposes the term </em>Facade Test<em> for an automated test which is more than a unit test, but not quite an integration test.</em> </p> <p> There are several definitions of what a unit test is, but the one that I personally prefer goes something like this: </p> <blockquote> <p> <em>A unit test is an automated test that tests a unit in isolation.</em> </p> </blockquote> <p> This is still a rather loose definition. What's a <em>unit?</em> What's does <em>isolation</em> mean? The answers to these questions seem to be a bit fluffy, but I think it's safe to say that a unit isn't bigger than a class. It's not a library (or 'assembly' in .NET terms). It's not all types in a namespace. In my opinion, a <em>unit</em> is a method... and a bit more. In OO, a method is coupled to it's defining class, which is the reason why I think of a unit as a bit more than a method. </p> <p> If that's a unit, then <em>isolation</em> means that you should be able to test the unit without using or relying on any other moving pieces of production code. </p> <p> Often, unit testing means that you have to decouple objects in order to achieve isolation. There's a few ways to do that, but <a href="http://amzn.to/12p90MG">Dependency Injection</a> is a very common approach. That often leads to heavy use of dynamic mocks and too many interfaces that exist exclusively to support unit testing. </p> <p> Sometimes, a good alternative is to write automated tests that exercise a larger group of classes. When all the collaborating classes are deterministic and implemented within the same library, this can be an effective approach. <a href="http://kozmic.pl/">Krzysztof Koźmic</a> describes that </p> <blockquote> <p> <em><a href="http://kozmic.pl/2011/02/28/unit-tests-are-overrated/">Windsor, has very few unit tests. Most tests there are (and we have close to 1000 of them) exercise entire container with no part stubbed out in simulations of real life scenarios</a></em> </p> </blockquote> <p> Another example is <a href="http://autofixture.codeplex.com/">AutoFixture</a>, which has hundreds of automated tests against the Fixture class. </p> <p> We need a name for tests like these. While we <em>could</em> call them Integration Tests, this label would lump them together with tests that exercise several libraries at once. </p> <p> To distinguish such tests from Unit Tests on the one side, and Integration Tests on the other side, I suggest the name <em>Facade Test</em> </p> <p> <img src="/content/binary/Windows-Live-Writer/Facade-Test_ED3A/Facade%20Test_thumb_1.png" alt="Facade Test"> </p> <p> The reason why I suggest the term Facade Test is that such automated tests tend to mostly target a small subset of classes that act as <a href="http://en.wikipedia.org/wiki/Facade_pattern">Facades</a> over many other classes in a library. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="90dbe10f930c40d2938abf288d0e5d4e"> <div class="comment-author">matthewr <a href="#90dbe10f930c40d2938abf288d0e5d4e">#</a></div> <div class="comment-content">I prefer Composite Tests or even Group Tests.</div> <div class="comment-date">2012-06-27 22:23 UTC</div> </div> <div class="comment" id="ba9b48daefc64b2da737a0cd4fcb1576"> <div class="comment-author">Bill Watson <a href="#ba9b48daefc64b2da737a0cd4fcb1576">#</a></div> <div class="comment-content">We break it down like this.<br> <br> <em>Unit test</em>: usually just one class but occasionally a couple of closely related classes, constructed and executed directly from the test code. Runs fast and no contact with the outside world.<br> <br> <em>Database</em> or <em>Comms test</em>: testing in isolation that a single class that interacts with something externally works. e.g. a class that accesses the database actually accesses the database, a class that contacts a remote server actual contacts a remote server (probably a special one we wrote for testing purposes).<br> <br> <em>Scenario test</em>: multiple classes, but with all external dependencies replaced with test doubles (e.g. no database access, no SOAP calls, no HTTP requests). Object graph is constructed via the same mechanism as the real object graph. So in some sense this tests the DI configuration. This is a form of integration test, but everything runs in memory so they are quick. Most of our tests are at this level.<br> <br> <em>Acceptance test</em>: full application running and tested with Selenium.<br> <br> And then there is manual testing.<br> </div> <div class="comment-date">2012-06-28 00:54 UTC</div> </div> <div class="comment" id="7c0723d53e4a415faeb15f5ad2001f72"> <div class="comment-author"><a href="http://www.neovolve.com">Rory Primrose</a> <a href="#7c0723d53e4a415faeb15f5ad2001f72">#</a></div> <div class="comment-content">I like the term component test for this level of scope. That being said, I still put it in a project suffixed with .UnitTests.</div> <div class="comment-date">2012-06-29 12:39 UTC</div> </div> <div class="comment" id="2a8be2bf1835439aa48a8e99d60eee4a"> <div class="comment-author"><a href="http://amirrajan.net">Amir Rajan</a> <a href="#2a8be2bf1835439aa48a8e99d60eee4a">#</a></div> <div class="comment-content">Why differentiate at all? I test is valuable or it isn't. A tests is fast or it isn't. Thoughts?</div> <div class="comment-date">2012-07-03 21:02 UTC</div> </div> <div class="comment" id="56f7bdaf4bcf449c819b245931453d71"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#56f7bdaf4bcf449c819b245931453d71">#</a></div> <div class="comment-content">The purpose is to have a pattern language for automated tests - just as with every other pattern language.<br> <br> &quot;Let's drive this through Facade Tests&quot; now means something else than &quot;Let's drive this through Unit Tests&quot;.</div> <div class="comment-date">2012-07-03 21:16 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Test-specific Equality versus Domain Equality https://blog.ploeh.dk/2012/06/22/Test-specificEqualityversusDomainEquality 2012-06-22T15:22:25+00:00 Mark Seemann <div id="post"> <p> As a reaction to my two <a href="/2012/06/21/TheResemblanceIdiom.aspx">previous</a> <a href="/2012/06/22/ResemblanceAndLikeness.aspx">blog posts</a>, Joe Miller asks on Twitter: </p> <p> <a href="https://twitter.com/lilshieste/status/216180824398241793">If a test needs to check a "special" type of equality, isn't that special equality actually part of the domain?</a> </p> <p> That's a fair question which I'd like to answer here. </p> <p> You should definitely strive towards having domain objects with strong equality semantics. Many things (including unit testing) becomes easier when domain objects override <a href="http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx">Equals</a> in a meaningful manner. By all means should you prefer that over any Resemblance or Likeness craziness. </p> <p> What is meaningful equality for a domain object? Personally, I find the guidance provided by <a href="http://www.amazon.com/gp/product/0321125215/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321125215">Domain-Driven Design</a> indispensable: </p> <ul> <li>If the domain object is an <em>Entity</em>, two objects are equal if their IDs are equal. No other properties should be compared.</li> <li>If the domain object is a <em>Value Object</em>, two object are equal if (all) their encapsulated data is equal.</li> <li>If the domain object is a <em>Service</em>, by default I'd say that the default reference equality is often the most correct (i.e. don't override Equals).</li> </ul> <p> Now consider the very common case of mapping layers, or the slightly related scenario of an Anti-corruption Layer. In such cases, the code translates Entities to and from other representations. How do we unit test such mapping code? </p> <p> If we were to rely on the domain object's built-in equality, we would only be testing that the <em>identity</em> of the Entity is as expected, but not whether or not the mapping code properly maps all the other data. In such cases we need <a href="http://xunitpatterns.com/test-specific%20equality.html">Test-specific Equality</a>, and that's exactly what Resemblances and Likenesses provide. </p> <p> In the <a href="/2012/06/21/TheResemblanceIdiom.aspx">previous example</a>, this is exactly what happens. The <a href="http://xunitpatterns.com/SUT.html">SUT</a> produces a new instance of the RequestReservationCommand: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">HttpPost</span>] <span style="color: blue;">public</span> <span style="color: #2b91af;">ViewResult</span> Post(<span style="color: #2b91af;">BookingViewModel</span> model) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.channel.Send(model.MakeReservation()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.View(<span style="color: #a31515;">"Receipt"</span>, model); }</pre> </p> <p> The MakeReservation method is implemented like this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">RequestReservationCommand</span> MakeReservation() { &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">RequestReservationCommand</span>( &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.Date, <span style="color: blue;">this</span>.Email, <span style="color: blue;">this</span>.Name, <span style="color: blue;">this</span>.Quantity); }</pre> </p> <p> Notice that nowhere does the code specify the identity of the RequestReservationCommand instance. This is done by the RequestReservationCommand constructor itself, because it's a domain rule that (unless deserialized) each command has a unique ID. </p> <p> Thus, from the unit test, you have absolutely no chance of knowing what the ID will be (it's a GUID). You could argue back and forth on whether the RequestReservationCommand class is an Entity or a Value Object, but in both cases, Domain Equality would involve comparing IDs, and those will <em>never</em> match. Therefore, the correct Test-specific Equality is to compare the values without the Id property. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="278aff46976d47ce81d2d9600658bb73"> <div class="comment-author"><a href="http://twitter.com/yreynhout">Yves Reynhout</a> <a href="#278aff46976d47ce81d2d9600658bb73">#</a></div> <div class="comment-content">As far as the &quot;Guid Generation&quot; goes, I like to control that part using the following code (optionally accompanied by some sort of scoping) in the bits where I'm interested in controlling it: <a href="http://pastie.org/4154395">some_code</a></div> <div class="comment-date">2012-06-26 13:33 UTC</div> </div> <div class="comment" id="db77aa7954dc450c9d71fc782f5f0e0e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#db77aa7954dc450c9d71fc782f5f0e0e">#</a></div> <div class="comment-content">Well, that's an Ambient Context (<a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a>, p. 118), and while it enables you to assign an expected Guid value from a unit test, from a <em>conceptual</em> perspective I don't think it's the correct solution for a test like this one.<br> <br> From a behavioral point of view, we don't really care about the value of the ID (the Guid). From other tests we know that a new instance of a command will always have a unique ID. It's part of the command's invariants. Thus, we <em>know</em> that the ID is going to be unique, so having to configure an Ambient Context is only going to add noise to a unit test.</div> <div class="comment-date">2012-06-26 16:09 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Resemblance and Likeness https://blog.ploeh.dk/2012/06/22/ResemblanceandLikeness 2012-06-22T12:35:04+00:00 Mark Seemann <div id="post"> <p> In a previous post I described the <a href="/2012/06/21/TheResemblanceIdiom.aspx">Resemblance idiom</a>. In this post I present a class that can be used to automate the process of creating a Resemblance. </p> <p> The Resemblance idiom enables you to write readable unit tests, but the disadvantage is that you will have to write (and maintain) quite a few test-specific Resemblance classes. If you're a Coding Neat Freak like me, you'll also have to override GetHashCode in every Resemblance, just because you're overriding Equals. If you're still looking for ways to torment yourself, you'll realize that the typical Resemblance implementation of Equals contains branching logic, so ideally, it should be unit tested too. (Yes, I <em>do</em> occasionally unit test my unit testing helper classes. <a href="http://autofixture.codeplex.com/">AutoFixture</a>, which is essentially one big unit test helper, is currently covered by some three thousand tests.) </p> <p> In that light, wouldn't it be nice if you were able to automate the process of defining Resemblance classes? With the Likeness class, you can. Likeness is currently bundled with AutoFixture, so you get it if you install the <a href="http://nuget.org/packages/AutoFixture">AutoFixture NuGet package</a> or if you download the AutoFixture zip file from <a href="http://autofixture.codeplex.com/">its CodePlex site</a>. The Likeness class can be found in an assembly named Ploeh.SemanticComparison. In the future, I plan on packaging this as a separate NuGet package, but currently, it's included with AutoFixture. </p> <h3 id="ea368b525c8f4d80b5deb95bc9436ef5"> Likeness <a href="#ea368b525c8f4d80b5deb95bc9436ef5" title="permalink">#</a> </h3> <p> Before I show you how to turn a Likeness into a Resemblance, I think a very short introduction to Likeness is in order. A couple of years ago <a href="/2010/06/29/IntroducingAutoFixtureLikeness.aspx">I introduced Likeness</a> on my blog, but I think a re-introduction is in order. In the context of the unit test from <a href="/2012/06/21/TheResemblanceIdiom.aspx">the previous blog post</a>, instead of manually writing a test like this: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>, <span style="color: #2b91af;">AutoWebData</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PostSendsOnChannel( &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Frozen</span>]<span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;&gt; channelMock, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingController</span> sut, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingViewModel</span> model) { &nbsp;&nbsp;&nbsp; sut.Post(model); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = model.MakeReservation(); &nbsp;&nbsp;&nbsp; channelMock.Verify(c =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; c.Send(<span style="color: #2b91af;">It</span>.Is&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;(cmd =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cmd.Date == expected.Date &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cmd.Email == expected.Email &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cmd.Name == expected.Name &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cmd.Quantity == expected.Quantity))); }</pre> </p> <p> Likeness will do it for you, using Reflection to match properties according to name and type. By default, Likeness compares <em>all</em> properties of the destination instance. This will not work in this test, because the RequestReservationCommand also includes an Id property which is a Guid which is never going to be the same across different instances: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> MakeReservationCommand( &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DateTime</span> date, <span style="color: blue;">string</span> email, <span style="color: blue;">string</span> name, <span style="color: blue;">int</span> quantity) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.date = date; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.email = email; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.name = name; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.quantity = quantity; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.id = <span style="color: #2b91af;">Guid</span>.NewGuid(); }</pre> </p> <p> Notice that the above test exludes the Id property. It's not very clear that this is going on, so a Test Reader may think that this is an accidental omission. With Likeness, this becomes explicit: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>, <span style="color: #2b91af;">AutoWebData</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PostSendsOnChannel( &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Frozen</span>]<span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">MakeReservationCommand</span>&gt;&gt; channelMock, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingController</span> sut, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingViewModel</span> model) { &nbsp;&nbsp;&nbsp; sut.Post(model); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = model.MakeReservation() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .AsSource().OfLikeness&lt;<span style="color: #2b91af;">MakeReservationCommand</span>&gt;() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Without(d =&gt; d.Id); &nbsp;&nbsp;&nbsp; channelMock.Verify(c =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; c.Send(<span style="color: #2b91af;">It</span>.Is&lt;<span style="color: #2b91af;">MakeReservationCommand</span>&gt;(x =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; expected.Equals(x)))); }</pre> </p> <p> Notice how the Without method explicitly states that the Id property should be excluded from the comparison. By convention, all other properties on the target type (MakeReservationCommand, which in this case is the same as the source type) must be equal to each other. </p> <p> The AsSource().OfLikeness extension method chain creates an instance of Likeness&lt;MakeReservationCommand,&nbsp;MakeReservationCommand&gt;, which isn't the same as an instance of MakeReservationCommand. Thus, you are still forced to use the slightly awkward It.Is syntax to express what equality means: </p> <p> <pre style="margin: 0px;">channelMock.Verify(c =&gt; &nbsp;&nbsp;&nbsp; c.Send(<span style="color: #2b91af;">It</span>.Is&lt;<span style="color: #2b91af;">MakeReservationCommand</span>&gt;(x =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; expected.Equals(x))));</pre> </p> <p> If a Likeness could be used as a Resemblance, the test would be more readable. </p> <h3 id="3ef18d3ec21c4a7caf027189809e8379"> Likeness as Resemblance <a href="#3ef18d3ec21c4a7caf027189809e8379" title="permalink">#</a> </h3> <p> The definition of the Likeness class is this: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Likeness</span>&lt;TSource, TDestination&gt; : <span style="color: #2b91af;">IEquatable</span>&lt;TDestination&gt;</pre> </p> <p> While it implements IEquatable&lt;TDestination&gt; it <em>isn't</em> a TDestination. That's the reason for the It.Is syntax above. </p> <p> Thanks to <a href="http://nikosbaxevanis.com/2012/02/20/dynamic-proxy-overriding-equals-in-autofixture-likeness/">awesome work by Nikos Baxevanis</a>, a Likeness instance can create a dynamic proxy of TDestination, as long as TDestination is non-sealed and doesn't seal the Equals method. The method is called CreateProxy: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> TDestination CreateProxy();</pre> </p> <p> This effectively turns the Likeness into a Resemblance by dynamically emitting a derived class that overrides Equals in the way that the Likeness instance (re)defines equality. With that, you can rewrite the unit test so that it's both readable and maintainable: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>, <span style="color: #2b91af;">AutoWebData</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PostSendsOnChannel( &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Frozen</span>]<span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;&gt; channelMock, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingController</span> sut, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingViewModel</span> model) { &nbsp;&nbsp;&nbsp; sut.Post(model); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = model.MakeReservation() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .AsSource().OfLikeness&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Without(d =&gt; d.Id) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .CreateProxy(); &nbsp;&nbsp;&nbsp; channelMock.Verify(c =&gt; c.Send(expected)); }</pre> </p> <p> The only difference in the definition of the <em>expected</em> variable is the additional call to CreateProxy. This changes the type of <em>expected</em> so that it's no longer Likeness&lt;MakeReservationCommand,&nbsp;MakeReservationCommand&gt;, but rather MakeReservationCommand (actually, a dynamically created sub-type of MakeReservationCommand). This enables you to write a readable assertion: </p> <p> <pre style="margin: 0px;">channelMock.Verify(c =&gt; c.Send(expected));</pre> </p> <p> This is exactly the same assertion as the assertion used with the Resemblance idiom described in the previous post. Thus, Likeness has now been turned into a Resemblance, and you no longer have to manually create and maintain concrete Resemblance classes. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ee275271932041a9933c6013ce260f79"> <div class="comment-author"><a href="http://www.skov-boisen.dk">Simon Skov Boisen</a> <a href="#ee275271932041a9933c6013ce260f79">#</a></div> <div class="comment-content">Great stuff - I took a look at the CreateProxy method and I must say that is some pretty hairy (and impressive) reflection and il.Emit code! How do one figure out exactly which IL needs to be emitted in order to create the dynamic proxy? Do you inspect the IL of a manually generated proxy and then use that as a guidance? (I realize you didn't write the code, but maybe you have some insight?) :)</div> <div class="comment-date">2012-07-05 07:43 UTC</div> </div> <div class="comment" id="6f2654b3ea9d406c9f974b5d57106a55"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6f2654b3ea9d406c9f974b5d57106a55">#</a></div> <div class="comment-content">Nah, I don't have any insights on that. Nikos Baxevanis wrote that, and I trust that he got it right (as he did with so much other crazy stuff). However, perhaps I can coax him to provide an answer here :)</div> <div class="comment-date">2012-07-05 08:03 UTC</div> </div> <div class="comment" id="d5aa44b9f8344189b3d3d6f44b121773"> <div class="comment-author"><a href="http://nikosbaxevanis.com">Nikos Baxevanis</a> <a href="#d5aa44b9f8344189b3d3d6f44b121773">#</a></div> <div class="comment-content">Hi Simon, definitely, a good starting point is to manually generate the code and then inspect the disassembled IL. Then, you can use the types in the System.Reflection.Emit namespace to create the (proxy) type. What this proxy generator supports, but usually others don't, is the ability to emit a proxy even for a type with no-default constructor. At that point, you can't manually generate and inspect the disassembled IL for all the possible combinations for (above three) number of arguments in the constructor. However, after loading the (third) constructor argument you keep loading additional arguments by referencing their index (lines 90-114 in the source, commit 8fff809). Finally, you need to create and return an instance of the newly created proxy. Since the proxy does not necessarily contains a default constructor there are some additional steps involved for discovering and choosing a compatible constructor with the source type. Definitely, there should be some points for improvement so if you have any suggestions there are more than welcome. :) Hope that helps.</div> <div class="comment-date">2012-07-05 11:24 UTC</div> </div> <div class="comment" id="a354776d21fa4c05aaf37cd5764baad3"> <div class="comment-author">James Nail <a href="#a354776d21fa4c05aaf37cd5764baad3">#</a></div> <div class="comment-content">Hi Mark,<br> Once again, you guys have done it. I've used foo.AsSource().OfLikeness() before, but the CreateProxy trick solved the exact problem I was having.<br> I'm continually impressed with the power you've packed into this relatively small tool (AutoFixture).</div> <div class="comment-date">2012-07-06 09:32 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Resemblance idiom https://blog.ploeh.dk/2012/06/21/TheResemblanceidiom 2012-06-21T08:42:17+00:00 Mark Seemann <div id="post"> <p> <em>In this post I describe a unit testing trick which can be used to make assertions more readable.</em> </p> <p> Many TDD or unit testing experts (e.g. <a href="http://osherove.com/">Roy Osherove</a>) give the advice that a unit test <a href="http://osherove.com/blog/2005/4/14/try-to-avoid-multiple-asserts-in-a-single-unit-test.html">should only contain a single assertion</a>. This is a pretty good piece of advice, but sometimes a <em>single assertion</em> is best interpreted as a <a href="http://osherove.com/blog/2011/2/9/multiple-mocks-asserts-and-hidden-results.html">single logical assertion</a>. </p> <p> When it comes to state-based testing, you can often write a <a href="http://xunitpatterns.com/Custom%20Assertion.html">Custom Assertion</a> to encapsulate multiple assertions into a single logical assertion. For interaction-based testing (i.e. using mock objects) this can be a bit harder. </p> <p> In order to write readable unit tests with single assertions, I sometimes use an idiom that I'll describe here. Since I've never seen it described anywhere else, I can't reasonably call it a design pattern, so I'll stick with the less universal term <em>idiom</em> - I call it the <em>Resemblance</em> idiom. In short, it introduces a test-specific override of an object's equality method. </p> <h3 id="b646cc3cd52642f1a837ae6a7cebe36c"> Motivating example <a href="#b646cc3cd52642f1a837ae6a7cebe36c" title="permalink">#</a> </h3> <p> Assume that you're TDD'ing this method: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">HttpPost</span>] <span style="color: blue;">public</span> <span style="color: #2b91af;">ViewResult</span> Post(<span style="color: #2b91af;">BookingViewModel</span> model) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.channel.Send(model.MakeReservation()); &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.View(<span style="color: #a31515;">"Receipt"</span>, model); }</pre> </p> <p> Obviously, when TDD'ing, the method isn't going to have any implementation before the test has been written, but for educational purposes, in this case I'm <em>showing</em> you the implementation before the test - I still <em>wrote</em> the tests first. This is the implementation you'd like to arrive at - particularly this line of code: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">this</span>.channel.Send(model.MakeReservation());</pre> </p> <p> The challenge here is that the MakeReservation method returns an instance of RequestReservationCommand, which is a data-carrying class (some would call it a <a href="http://en.wikipedia.org/wiki/Data_transfer_object">DTO</a>) that doesn't override <a href="http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx">Equals</a>. One option is to override Equals, but that would lead to <a href="http://xunitpatterns.com/Test%20Logic%20in%20Production.html#Equality%20Pollution">Equality Pollution</a>, since the equality semantics of RequestReservationCommand isn't going to match what you need in order to write the test. </p> <p> Using <a href="http://code.google.com/p/moq/">Moq</a>, your first take might look something like this: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>, <span style="color: #2b91af;">AutoWebData</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PostSendsOnChannel( &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Frozen</span>]<span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;&gt; channelMock, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingController</span> sut, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingViewModel</span> model) { &nbsp;&nbsp;&nbsp; sut.Post(model); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = model.MakeReservation(); &nbsp;&nbsp;&nbsp; channelMock.Verify(c =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; c.Send(<span style="color: #2b91af;">It</span>.Is&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;(cmd =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cmd.Date == expected.Date &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cmd.Email == expected.Email &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cmd.Name == expected.Name &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cmd.Quantity == expected.Quantity))); }</pre> </p> <p> This works, but is awkward. First of all, I'd prefer a more readable test than this. Secondly, what happens if, in the future, someone adds a new property to the RequestReservationCommand class? If you have a lot of tests like this one, should they all be updated to include the new property in the comparison? </p> <h3 id="634d187b49bb4fa9a0e32dfc84f50d43"> Attempted fix <a href="#634d187b49bb4fa9a0e32dfc84f50d43" title="permalink">#</a> </h3> <p> The first fix you might attempt involves extracting the comparison to a helper method like this one: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> Equals(<span style="color: #2b91af;">RequestReservationCommand</span> expected, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">RequestReservationCommand</span> actual) { &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> actual.Date == expected.Date &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; actual.Email == expected.Email &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; actual.Name == expected.Name &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; actual.Quantity == expected.Quantity; }</pre> </p> <p> This is better because at least it gives you a central method where you can manage which properties should be included in the comparison, but from a test invocation perspective it's still a bit unwieldy: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>, <span style="color: #2b91af;">AutoWebData</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PostSendsOnChannel( &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Frozen</span>]<span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;&gt; channelMock, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingController</span> sut, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingViewModel</span> model) { &nbsp;&nbsp;&nbsp; sut.Post(model); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = model.MakeReservation(); &nbsp;&nbsp;&nbsp; channelMock.Verify(c =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; c.Send(<span style="color: #2b91af;">It</span>.Is&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;(cmd =&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Equals(expected, cmd)))); }</pre> </p> <p> You're still stuck with the It.Is noise and a lambda expression. Being a static helper method, it's also not very object-oriented. Perhaps you don't care about that, but by not being object-oriented, it's also not very composable. This can come back to bite you if you want to compare complex object graphs, because it's dificult to compose static methods. There's a another way which provides a better alternative. </p> <h3 id="e2e8d18375ea48c69b4eeb5fe2f432f4"> Resemblance <a href="#e2e8d18375ea48c69b4eeb5fe2f432f4" title="permalink">#</a> </h3> <p> The trick is to recall that ultimately, most unit testing is about determining whether the actual outcome is <em>equal</em> to the expected outcome. Since the Equals method is virtual, you can simply create a test-specific child class that overrides the Equals method. This is what I call a Resemblance. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">RequestReservationCommandResemblance</span> : <span style="color: #2b91af;">RequestReservationCommand</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> RequestReservationCommandResemblance(<span style="color: #2b91af;">RequestReservationCommand</span> source) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; : <span style="color: blue;">base</span>(source.Date, source.Email, source.Name, source.Quantity) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">bool</span> Equals(<span style="color: blue;">object</span> obj) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> other = obj <span style="color: blue;">as</span> <span style="color: #2b91af;">RequestReservationCommand</span>; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (other != <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">object</span>.Equals(<span style="color: blue;">this</span>.Date, other.Date) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; <span style="color: blue;">object</span>.Equals(<span style="color: blue;">this</span>.Email, other.Email) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; <span style="color: blue;">object</span>.Equals(<span style="color: blue;">this</span>.Name, other.Name) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp; <span style="color: blue;">object</span>.Equals(<span style="color: blue;">this</span>.Quantity, other.Quantity); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">base</span>.Equals(obj); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">int</span> GetHashCode() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.Date.GetHashCode() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ^ <span style="color: blue;">this</span>.Email.GetHashCode() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ^ <span style="color: blue;">this</span>.Name.GetHashCode() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ^ <span style="color: blue;">this</span>.Quantity.GetHashCode(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Notice that the RequestReservationCommandResemblance class derives from RequestReservationCommand in order to override Equals (and GetHashCode for good measure). This looks a bit more complicated than the previous attempt at a fix, but it's much more object-oriented and composable, and most importantly: the test is much easier to read because almost all the noise can be removed: </p> <p> <pre style="margin: 0px;">[<span style="color: #2b91af;">Theory</span>, <span style="color: #2b91af;">AutoWebData</span>] <span style="color: blue;">public</span> <span style="color: blue;">void</span> PostSendsOnChannel( &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Frozen</span>]<span style="color: #2b91af;">Mock</span>&lt;<span style="color: #2b91af;">IChannel</span>&lt;<span style="color: #2b91af;">RequestReservationCommand</span>&gt;&gt; channelMock, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingController</span> sut, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BookingViewModel</span> model) { &nbsp;&nbsp;&nbsp; sut.Post(model); &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> expected = <span style="color: blue;">new</span> <span style="color: #2b91af;">RequestReservationCommandResemblance</span>(model.MakeReservation()); &nbsp;&nbsp;&nbsp; channelMock.Verify(c =&gt; c.Send(expected)); }</pre> </p> <p> Notice in particular how you're now able to state exactly <em>what</em> you care about, instead of <em>how</em> you care about it: </p> <p> <pre style="margin: 0px;">channelMock.Verify(c =&gt; c.Send(expected));</pre> </p> <p> You may still feel that the overhead of creating a new test-specific class just for this purpose doesn't quite justify the increased readability and composability, but stay tuned - <a href="/2012/06/22/ResemblanceAndLikeness.aspx">I have more tricks to show you</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="aeff573cca2941d4a4c3780a68d41470"> <div class="comment-author"><a href="http://vaskir.blogspot.com">Vasily Kirichenko</a> <a href="#aeff573cca2941d4a4c3780a68d41470">#</a></div> <div class="comment-content">Although this idiom allows changing the algorithm of value objects equality from one test case to another (however, introducing a bunch of derived classes may be a bit boring), it has very poor diagnostics characteristics (if a test fails, we can't say what property(s) is mismatched). <br> <br> &quot;One situation we want to avoid, however, is when we can’t diagnose a test<br> failure that has happened. The last thing we should have to do is crack open the<br> debugger and step through the tested code to find the point of disagreement.&quot;, GOOS</div> <div class="comment-date">2012-06-21 13:05 UTC</div> </div> <div class="comment" id="185d1e40af7a403c8c165d66417e8561"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#185d1e40af7a403c8c165d66417e8561">#</a></div> <div class="comment-content">There's that, but none of the options presented here solve that problem - all of them are going to throw an exception by the Verify method if the correct method call wasn't made.<br> <br> In GOOS they are better off because they use Hamcrest, and their mocking library understands Hamcrest matchers. That's not the situation in .NET (yet).<br> <br> The only way around this that I can think of is to call the Verify method four times - one for each property above. IMO that would be sacrificing either readability or maintainability to optimize for a failure scenario which should only occur rarely. I prefer to optimize for readability instead. YMMV.</div> <div class="comment-date">2012-06-21 15:09 UTC</div> </div> <div class="comment" id="6c035db0db0549efa8a1df7d1c6f4e22"> <div class="comment-author"><a href="http://blog.svick.org">svick</a> <a href="#6c035db0db0549efa8a1df7d1c6f4e22">#</a></div> <div class="comment-content">If you always want to compare all properties, wouldn't using Reflection solve the problem here?<br> <br> Or something like<br> <br> IEqualityComparer[RequestReservationCommand] CreateRequestReservationCommandComparer()<br> {<br> return CreateComparer[RequestReservationCommand](<br> c =&gt; c.Date, c =&gt; c.Email, c =&gt; c.Name, c =&gt; c.Quantity<br> );<br> }<br> <br> Although I'm not sure how composable that would be.<br> <br> BTW, I can't seem to post any comment containing the less than sign, it says:<br> <br> An error has been encountered while processing the page. We have logged the error condition and are working to correct the problem. We apologize for any inconvenience. </div> <div class="comment-date">2012-06-21 17:22 UTC</div> </div> <div class="comment" id="b235460f23614e54bc6c12de6821ccb5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b235460f23614e54bc6c12de6821ccb5">#</a></div> <div class="comment-content">As I wrote: stay tuned :)<br> <br> (and yes: the comment engine here is really crappy - sorry about that)</div> <div class="comment-date">2012-06-21 17:42 UTC</div> </div> <div class="comment" id="d099e8479f9845928a0ce0e1e784a79f"> <div class="comment-author">Daniel Hilgarth <a href="#d099e8479f9845928a0ce0e1e784a79f">#</a></div> <div class="comment-content">When using resemblance Assert.Equal is not working. You need to use Assert.IsTrue(expected.Equals(actual)) instead or make the resemblance class implement IEqualityComparer{T}.<br> Do you have a solution for that?</div> <div class="comment-date">2012-10-01 15:18 UTC</div> </div> <div class="comment" id="2339c2bd568b4637b5f56c861e5a54e8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2339c2bd568b4637b5f56c861e5a54e8">#</a></div> <div class="comment-content">I don't have a <em>direct</em> solution for that particular issue, but I find the Resemblance idiom most useful when dealing with Indirect Input and Output, as in the example above. In other words, it's mostly useful when dealing with Mock objects, because the <em>have</em> to deal exclusively with equality.<br> <br> When it comes to direct assertions, like Assert.Equal, I'd love it if we had something like Hamcrest Matchers in .NET (actually, there <em>is</em> a .NET port, but last time I looked, it seemed abandonded). The next best thing is often a custom IEqualityComparer, but that doesn't necessarily solve the pretty print error reporting need.<br> <br> Sometimes, it's nice to have both, so one can implement a custom IEqualityComparer and then use that class to also implement a Resemblance.<br> <br> FWIW, the Likeness class described in <a href="/2012/06/22/ResemblanceAndLikeness.aspx">the next blog post</a> is built that way. It also includes a ShouldEqual method as a very concrete way of providing more readable error reporting...</div> <div class="comment-date">2012-10-01 19:28 UTC</div> </div> <div class="comment" id="ec567095fb2b47a48c5438affae05cb3"> <div class="comment-author">Daniel Hilgarth <a href="#ec567095fb2b47a48c5438affae05cb3">#</a></div> <div class="comment-content">Thanks for the comment. Likeness couldn't be used in that particular case as the condition for equality was rather complex with nested properties. You can actually see it in the HttpActionContextResemblence in the pull request to Hyprlinkr: https://github.com/dhilgarth/Hyprlinkr/blob/de27a39477a747f56ae23fd8717bb8ef24c56bea/Hyprlinkr.UnitTest/HttpActionContextResemblance.cs<br> <br> I used the approach with an implementation of IEqualityComparer</div> <div class="comment-date">2012-10-02 13:02 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Bank OCR kata in F#: user stories 3 and 4 https://blog.ploeh.dk/2012/06/04/BankOCRkatainF#userstories3and4 2012-06-04T05:16:35+00:00 Mark Seemann <div id="post"> <p> In <a href="/2012/05/29/BankOCRkatainFuserstory1">previous</a> <a href="/2012/06/01/BankOCRkatainFuserstory2">posts</a>, I've walked through the first two user stories of the <a href="http://codingdojo.org/cgi-bin/index.pl?KataBankOCR">Bank OCR kata</a> in F#. In this post I'll walk through my implementation of user stories 3 and 4. </p> <p> The reason I'm collecting both user stories in a single blog post is that the requirements of user story 4 represent a change from user story 3, and since I didn't use a DVCS for this kata, I no longer have my implementation for user story 3. </p> <h3 id="adbf308af9674d5eba0b55622580d256"> The core unit test <a href="#adbf308af9674d5eba0b55622580d256" title="permalink">#</a> </h3> <p> As with the previous user stories, I started by writing a <a href="http://xunitpatterns.com/Parameterized%20Test.html">Parameterized Test</a>: </p> <p> <pre style="margin: 0px;">[&lt;Theory&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; | _| _||_||_ |_&nbsp;&nbsp; ||_||_|</span> <span style="color: maroon;">&nbsp; ||_&nbsp; _|&nbsp; | _||_|&nbsp; ||_| _|"</span>, <span style="color: maroon;">"123456789"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp;_||_||_ |_||_| _||_||_ |_ </span> <span style="color: maroon;">&nbsp;_|&nbsp; | _||_||_||_ |_||_| _|"</span>, <span style="color: maroon;">"345882865"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span> <span style="color: maroon;">&nbsp;_||_||_ |_||_| _||_|&nbsp; ||_|</span> <span style="color: maroon;">&nbsp;_|&nbsp; | _||_||_||_ |_|&nbsp; |&nbsp; |"</span>, <span style="color: maroon;">"345882814"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; | _| _||_||_ |_&nbsp;&nbsp; ||_||_|</span> <span style="color: maroon;">&nbsp; ||_&nbsp; _|&nbsp; | _||_|&nbsp; ||_||_|"</span>, <span style="color: maroon;">"123456789"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; | _| _||_||_ |_&nbsp;&nbsp; ||_||_|</span> <span style="color: maroon;">&nbsp; | _| _|&nbsp; | _||_|&nbsp; ||_||_|"</span>, <span style="color: maroon;">"133456788 AMB ['133456188', '193456788']"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; | _| _||_||_ |_&nbsp;&nbsp; ||_|| |</span> <span style="color: maroon;">&nbsp; | _| _|&nbsp; | _||_|&nbsp; ||_||_|"</span>, <span style="color: maroon;">"133456780 ERR"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span> <span style="color: maroon;">&nbsp;_||_||_ |_||_| _||_|&nbsp; ||_|</span> <span style="color: maroon;">&nbsp;_|| | _||_||_||_ |_|&nbsp; |&nbsp; |"</span>, <span style="color: maroon;">"345882814"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp; _&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _ </span> <span style="color: maroon;">|_||_&nbsp;&nbsp; |&nbsp; || | _| _&nbsp; _||_ </span> <span style="color: maroon;">|_||_|&nbsp; |&nbsp; ||_|&nbsp;&nbsp; |_| _||_|"</span>, <span style="color: maroon;">"86110??36 ILL"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span> <span style="color: maroon;">&nbsp;_||_||_ |_||_| _||_|&nbsp; ||_|</span> <span style="color: maroon;">&nbsp;_|&nbsp; | _||&nbsp; |_|&nbsp;&nbsp; |_|&nbsp; |&nbsp; |"</span>, <span style="color: maroon;">"345?8?814 ILL"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> &nbsp; <span style="color: maroon;">&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |</span> <span style="color: maroon;">&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |"</span>, <span style="color: maroon;">"711111111"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |</span> <span style="color: maroon;">&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |&nbsp; |"</span>, <span style="color: maroon;">"777777177"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp;_|| || || || || || || || |</span> <span style="color: maroon;">|_ |_||_||_||_||_||_||_||_|"</span>, <span style="color: maroon;">"200800000"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp;_| _| _| _| _| _| _| _| _|</span> <span style="color: maroon;">&nbsp;_| _| _| _| _| _| _| _| _|"</span>, <span style="color: maroon;">"333393333"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">|_||_||_||_||_||_||_||_||_|</span> <span style="color: maroon;">|_||_||_||_||_||_||_||_||_|"</span>, <span style="color: maroon;">"888888888 AMB ['888886888', '888888880', '888888988']"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">|_ |_ |_ |_ |_ |_ |_ |_ |_ </span> <span style="color: maroon;">&nbsp;_| _| _| _| _| _| _| _| _|"</span>, <span style="color: maroon;">"555555555 AMB ['559555555', '555655555']"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">|_ |_ |_ |_ |_ |_ |_ |_ |_ </span> <span style="color: maroon;">|_||_||_||_||_||_||_||_||_|\r\n"</span>, <span style="color: maroon;">"666666666 AMB ['686666666', '666566666']"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">|_||_||_||_||_||_||_||_||_|</span> <span style="color: maroon;">&nbsp;_| _| _| _| _| _| _| _| _|"</span>, <span style="color: maroon;">"999999999 AMB ['993999999', '999959999', '899999999']"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _ </span> <span style="color: maroon;">|_||_|| || ||_&nbsp;&nbsp; |&nbsp; |&nbsp; ||_ </span> <span style="color: maroon;">&nbsp; | _||_||_||_|&nbsp; |&nbsp; |&nbsp; | _|"</span>, <span style="color: maroon;">"490067715 AMB ['490067115', '490867715', '490067719']"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp;_| _| _||_||_ |_&nbsp;&nbsp; ||_||_|</span> <span style="color: maroon;">&nbsp; ||_&nbsp; _|&nbsp; | _||_|&nbsp; ||_| _|"</span>, <span style="color: maroon;">"123456789"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;_&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; </span> <span style="color: maroon;">| || || || || || || ||_&nbsp;&nbsp; |</span> <span style="color: maroon;">|_||_||_||_||_||_||_| _|&nbsp; |"</span>, <span style="color: maroon;">"000000051"</span>)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _ </span> <span style="color: maroon;">|_||_|| ||_||_&nbsp;&nbsp; |&nbsp; |&nbsp; | _ </span> <span style="color: maroon;">&nbsp; | _||_||_||_|&nbsp; |&nbsp; |&nbsp; | _|"</span>, <span style="color: maroon;">"490867715"</span>)&gt;] <span style="color: blue;">let</span> ParseToPrintoutReturnsCorrectResult entry expected = &nbsp;&nbsp;&nbsp; entry &nbsp;&nbsp;&nbsp; |&gt; ParseToPrintout &nbsp;&nbsp;&nbsp; |&gt; should equal expected</pre> </p> <p> Once again the test utilizes <a href="http://xunit.codeplex.com/">xUnit.net's</a> data theories feature to decouple the test code from the test data, as well as <a href="http://fsunit.codeplex.com/">FsUnit</a> for the assertion DSL. </p> <p> The test is really simple: it's 85 lines of data and three lines of code which state that the result of piping the <em>entry</em> argument to the ParseToPrintout function should be equal to the <em>expected</em> argument. </p> <h3 id="9fb1a1659503498b8a5767d538cb2f71"> Implementation <a href="#9fb1a1659503498b8a5767d538cb2f71" title="permalink">#</a> </h3> <p> The implementation may seem more daunting. At first, I'll post the function in its entirety, and afterwards I'll break it down and walk you through it. </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> ParseToPrintout entry = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> toPrintable opt = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> opt <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | Some(d) <span style="color: blue;">-&gt;</span> d.ToString() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | None <span style="color: blue;">-&gt;</span> <span style="color: maroon;">"?"</span> &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> formatEntry s = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; ParseToDigits &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.map toPrintable &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; String.Concat&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> isLegible potentialDigits = potentialDigits |&gt; Seq.forall Option.isSome &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> isValidAndLegible s = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> potentialDigits = s |&gt; ParseToDigits &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (potentialDigits |&gt; isLegible) &amp;&amp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (potentialDigits |&gt; skipNones |&gt; IsValid) &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> entry |&gt; isValidAndLegible &nbsp;&nbsp;&nbsp; <span style="color: blue;">then</span> entry |&gt; formatEntry &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> getCandidates chars = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> withAlternatives c = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> c <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | <span style="color: maroon;">' '</span> <span style="color: blue;">-&gt;</span> <span style="color: blue;">yield!</span> [ <span style="color: maroon;">'|'</span>; <span style="color: maroon;">'_'</span> ] &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | <span style="color: maroon;">'|'</span> | <span style="color: maroon;">'_'</span> <span style="color: blue;">-&gt;</span> <span style="color: blue;">yield</span> <span style="color: maroon;">' '</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | _ <span style="color: blue;">-&gt;</span> <span style="color: blue;">yield!</span> [] &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; chars &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.mapi (<span style="color: blue;">fun</span> i _ <span style="color: blue;">-&gt;</span> chars &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; ReplaceAt i withAlternatives) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.concat &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> validCandidates = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; entry.ToCharArray() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; getCandidates &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.map ToString &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.filter isValidAndLegible &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.toList &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> formatAlternatives alternatives = &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; alternatives &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.map formatEntry &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.map (sprintf <span style="color: maroon;">"'%s'"</span>) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; Seq.reduce (sprintf <span style="color: maroon;">"%s, %s"</span>) &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> validCandidates <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | [head] <span style="color: blue;">-&gt;</span> head |&gt; formatEntry &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | [] <span style="color: blue;">-&gt;</span> <span style="color: blue;">if</span> entry |&gt; ParseToDigits |&gt; isLegible &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">then</span> entry |&gt; formatEntry |&gt; sprintf <span style="color: maroon;">"%s ERR"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> entry |&gt; formatEntry |&gt; sprintf <span style="color: maroon;">"%s ILL"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | _ <span style="color: blue;">-&gt;</span> validCandidates &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; formatAlternatives &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; sprintf <span style="color: maroon;">"%s AMB [%s]"</span> (formatEntry entry)</pre> </p> <p> The first thing to notice is that the <em>ParseToPrintout</em> function is composed of quite a few helper functions. A thing that I currently don't like so much about F# is that it's necessary to define a function before it can be used, so all the helper functions must appear before the function that defines the general flow. </p> <p> It's possible to move the helper functions to other modules so that they don't clutter the implementation, but most of the functions defined here seem to me to be a very specialized part of the overall implementation. In this particular code base, I've been working under the assumption that it would be best to define a function in as narrow a scope as possible, but looking at the result, I don't think that was the right decision. In the future, I think I'll move more helper functions to a separate module. </p> <h3 id="1e3b3eef228e45dd87ff4457e35c5576"> Valid and legible <a href="#1e3b3eef228e45dd87ff4457e35c5576" title="permalink">#</a> </h3> <p> As a start, skim past the helper functions. The actual program flow of the function starts here: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">if</span> entry |&gt; isValidAndLegible <span style="color: blue;">then</span> entry |&gt; formatEntry</pre> </p> <p> That part of the if/then branch should be fairly easy to understand. If the entry is valid (according to the checksum function from user story 2) and legible, the result is formatted and returned. </p> <p> The isValidAndLegible function determines whether the entry is... well... valid and legible. The first thing it does is to pipe the string into the ParseToDigits function, which, <a href="/2012/05/29/BankOCRkatainFuserstory1">you'll recall</a>, returns a sequence of integer <a href="http://msdn.microsoft.com/en-us/library/dd233245.aspx">options</a>. </p> <p> To figure out whether those potential digits are legible it invokes the isLegible function, which I'll return to shortly. </p> <p> In order to figure out whether the potential digits are valid, it invokes the <a href="/2012/06/01/BankOCRkatainFuserstory2">IsValid method from user story 2</a>. However, in order to do so, it needs to convert the sequence of integer options to a sequence of integers. It does that by invoking the skipNones helper method, that I defined in a helper module: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> skipNones sequence = Seq.collect Option.toArray sequence</pre> </p> <p> This function takes a sequence of options and returns a sequence of values by skipping all the <a href="http://msdn.microsoft.com/en-us/library/ee340390.aspx">Nones</a>. It does that by turning each option into an array of 0 or 1 elements and then concatenate all these arrays together to form a new sequence. </p> <p> This array is piped into the IsValid function, which will only return true if there are exactly nine digits and the checksum is OK. </p> <p> The isLegible function is simpler because all it needs to do is to verify that all the options are <a href="http://msdn.microsoft.com/en-us/library/ee370301.aspx">Somes</a> (instead of Nones). </p> <h3 id="604b9be0d19941b88e7da8ade53fc897"> Formatting the entry <a href="#604b9be0d19941b88e7da8ade53fc897" title="permalink">#</a> </h3> <p> The formatEntry function pipes the entry into the ParseToDigits function, which returns a sequence of integer options. Each option is mapped into a string, replacing each None with a "?". </p> <p> Finally, the sequence of strings is concatenated into a single string by piping it to the built-in <a href="http://msdn.microsoft.com/en-us/library/dd784338.aspx">String.Concat</a> method. </p> <h3 id="b609f439524d4c2d9bfb294a4b97427f"> Finding candidates <a href="#b609f439524d4c2d9bfb294a4b97427f" title="permalink">#</a> </h3> <p> While formatting the entry is fairly easy if it's valid and legible, it suddenly becomes much harder to attempt to find other candidates if the entry seems invalid. What needs to be done in this case is to iterate through each and every character of the entry and try to replace <em>just a single character at a time</em> to see if it produces a better entry. </p> <p> According to user story 4, a blank space could be either a vertical pipe or and underscore, while an underscore and a pipe might instead be a blank space. This relationship can be formalized by the withAlternatives function: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> withAlternatives c = &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">match</span> c <span style="color: blue;">with</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | <span style="color: maroon;">' '</span> <span style="color: blue;">-&gt;</span> <span style="color: blue;">yield!</span> [ <span style="color: maroon;">'|'</span>; <span style="color: maroon;">'_'</span> ] &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | <span style="color: maroon;">'|'</span> | <span style="color: maroon;">'_'</span> <span style="color: blue;">-&gt;</span> <span style="color: blue;">yield</span> <span style="color: maroon;">' '</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | _ <span style="color: blue;">-&gt;</span> <span style="color: blue;">yield!</span> [] &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> </p> <p> This function takes as input a single character and returns a sequence of characters. If the input is ' ', the output is a list containing '|' and '_'. On the other hand, if the input is either '|' or '_', the output is a sequence with the single element ' '. For all other characters (such as line breaks), the output is an empty sequence, indicating that no replacement should be attempted. </p> <p> This defines the alternatives which can be fed into the ReplaceAt helper function: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> ReplaceAt index produceReplacements sequence = &nbsp;&nbsp;&nbsp; sequence &nbsp;&nbsp;&nbsp; |&gt; Seq.nth index &nbsp;&nbsp;&nbsp; |&gt; produceReplacements &nbsp;&nbsp;&nbsp; |&gt; Seq.map (<span style="color: blue;">fun</span> x <span style="color: blue;">-&gt;</span> sequence |&gt; ReplaceElementAt index x)</pre> </p> <p> In this case, the ReplaceAt function takes as input a sequence of characters (the entry string) and a function providing alternatives (withAlternatives) and produces a sequence of sequence of characters where only a single character has been replaced according to the alternatives. </p> <p> If that sounds difficult, perhaps these unit tests explain the behavior better: </p> <p> <pre style="margin: 0px;">[&lt;Fact&gt;] <span style="color: blue;">let</span> ProduceAllReplacementsAtReturnsCorrectResult1() = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> actual = ReplaceAt 0 (<span style="color: blue;">fun</span> c <span style="color: blue;">-&gt;</span> [ <span style="color: maroon;">'I'</span>; <span style="color: maroon;">'i'</span> ]) <span style="color: maroon;">"123"</span> &nbsp;&nbsp;&nbsp; actual |&gt; Seq.map ToString |&gt; should equalSequence [ <span style="color: maroon;">"I23"</span>; <span style="color: maroon;">"i23"</span> ] &nbsp; [&lt;Fact&gt;] <span style="color: blue;">let</span> ProduceAllReplacementsAtReturnsCorrectResult2() = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> actual = ReplaceAt 1 (<span style="color: blue;">fun</span> c <span style="color: blue;">-&gt;</span> [ <span style="color: maroon;">'V'</span> ]) <span style="color: maroon;">"153"</span> &nbsp;&nbsp;&nbsp; actual |&gt; Seq.map ToString |&gt; should equalSequence [ <span style="color: maroon;">"1V3"</span> ]</pre> </p> <p> The first unit tests replaces the first (zero-indexed) character in the input string "123" with two alternatives, 'I' and 'i', producing the output sequence of "I23" and "i23". </p> <p> The second test replaces the second (zero-indexed) character in the input string "153" with the single alternative 'V' to produce the output "1V3". </p> <p> The ReplaceAt function does this by first using the Seq.nth function to pick the nth character out of the sequence, and then pipe that character into the function that produces the alternatives. This produces a sequence of replacement characters, e.g. '|' and '_' as explained above. </p> <p> This sequence of replacement characters is then used to produce a sequence of strings where the element at the index is replaced with each of the replacement characters. In order to do that, the original input is piped into the ReplaceElementAt helper function: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> ReplaceElementAt index element sequence = &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> beforeIndex = sequence |&gt; Seq.take index &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> atIndex = element |&gt; Seq.singleton &nbsp;&nbsp;&nbsp; <span style="color: blue;">let</span> afterIndex = sequence |&gt; Seq.skip (index + 1) &nbsp;&nbsp;&nbsp; afterIndex |&gt; Seq.append atIndex |&gt; Seq.append beforeIndex</pre> </p> <p> This function takes a sequence of elements (e.g. a string) and returns another sequence (e.g. another string) by replacing the element at the specified <em>index</em> with the supplied <em>element</em>. </p> <p> All these helper functions can be combined to produce a sequence of candidate strings: </p> <p> <pre style="margin: 0px;">chars |&gt; Seq.mapi (<span style="color: blue;">fun</span> i _ <span style="color: blue;">-&gt;</span> chars &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; ReplaceAt i withAlternatives) |&gt; Seq.concat</pre> </p> <p> Of these candidates, a lot will be invalid, but filtering them to find only the valid candidates is fairly easy: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">let</span> validCandidates = &nbsp;&nbsp;&nbsp; entry.ToCharArray() &nbsp;&nbsp;&nbsp; |&gt; getCandidates &nbsp;&nbsp;&nbsp; |&gt; Seq.map ToString &nbsp;&nbsp;&nbsp; |&gt; Seq.filter isValidAndLegible &nbsp;&nbsp;&nbsp; |&gt; Seq.toList</pre> </p> <p> This final list of candidates is a <em>list</em> instead of a <em>sequence</em> because it enables me to use <a href="http://msdn.microsoft.com/en-us/library/dd547125.aspx">list pattern matching</a> to deal with the cases of a single, none, or multiple valid alternatives: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">match</span> validCandidates <span style="color: blue;">with</span> | [head] <span style="color: blue;">-&gt;</span> head |&gt; formatEntry | [] <span style="color: blue;">-&gt;</span> <span style="color: blue;">if</span> entry |&gt; ParseToDigits |&gt; isLegible &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">then</span> entry |&gt; formatEntry |&gt; sprintf <span style="color: maroon;">"%s ERR"</span> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span> entry |&gt; formatEntry |&gt; sprintf <span style="color: maroon;">"%s ILL"</span> | _ <span style="color: blue;">-&gt;</span> validCandidates &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; formatAlternatives &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&gt; sprintf <span style="color: maroon;">"%s AMB [%s]"</span> (formatEntry entry)</pre> </p> <p> If there's only a single valid alternative, the [head] match is triggered and the decomposed <em>head</em> variable is simply piped to the formatEntry function. </p> <p> If there's no valid alternative, the [] match is triggered, and the final formatting is then based on whether or not the entry is legible or not. </p> <p> Finally, if there are multiple candidates, each is formatted and appended as ambiguous alternatives to the original (invalid) input. </p> <p> In case you'd like to take an even closer look at the code, I've attached the entire solution as a zip file: <a href="/content/binary/KataBankOCR.zip">KataBankOCR.zip (299.62 KB)</a> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="7901ad9fcae549b5b9c2dec6cca360d0"> <div class="comment-author">Jonty <a href="#7901ad9fcae549b5b9c2dec6cca360d0">#</a></div> <div class="comment-content">After reading this post I decided to try out some F# and it is very interesting. I'm struggling to work out &quot;best practices&quot; though. It would be great to hear your thoughts on this and how to move from a C# mindset to F#. I'm thinking things like how SOLID principles apply and how to structure your code to keep it maintainable.</div> <div class="comment-date">2012-06-21 11:16 UTC</div> </div> <div class="comment" id="2a72ccfa8ed04c28be8ec48502375c52"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2a72ccfa8ed04c28be8ec48502375c52">#</a></div> <div class="comment-content">FWIW, I found the book <a href="http://www.amazon.com/gp/product/1933988924/ref=as_li_ss_tl?ie=UTF8&tag=ploeh-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1933988924">Real World Functional Programming: With Examples in F# and C#</a> an excellent introduction to functional concepts for object-oriented programmers.<br> <br> Soon, I'll write a small blog post on one way in which SOLID relates to Functional Programming.</div> <div class="comment-date">2012-06-21 12:24 UTC</div> </div> <div class="comment" id="a1cb17569eaf4ea4a394f0f9e6740ff7"> <div class="comment-author"><a href="http://www.skov-boisen.dk">Simon Skov Boisen</a> <a href="#a1cb17569eaf4ea4a394f0f9e6740ff7">#</a></div> <div class="comment-content">Excellent series of blogposts - how do you run your xunit tests? Do you run them with the xunit gui runner or using TestDriven.net?<br> <br> I second 'Real World Functional Programming: With Examples in F# and C#', it's a really great book!<br> <br> One very minor change to one of your helper-functions could be to use the backward pipe operator when combining the sequences in ReplaceElementAt, that way one doesn't need to have the somewhat confusing reverse append-order. Or simply use seq { } and you won't need the atIndex. See following gist for alternatives:<br> <br> <a href="https://gist.github.com/3049158">https://gist.github.com/3049158</a><br> <br> <br> </div> <div class="comment-date">2012-07-04 19:35 UTC</div> </div> <div class="comment" id="77a2bf115ba448fe82f99782b66c08a4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#77a2bf115ba448fe82f99782b66c08a4">#</a></div> <div class="comment-content">I use TestDriven.net to run the xUnit.net tests.<br> <br> Thanks for the alternatives for the ReplaceElementAt function. I was never happy with the readability of my implementation. I like the seq { } alternative best, so I've updated my own source code :)</div> <div class="comment-date">2012-07-04 19:42 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Bank OCR kata in F#: user story 2 https://blog.ploeh.dk/2012/06/01/BankOCRkatainF#userstory2 2012-06-01T05:54:27+00:00 Mark Seemann <div id="post"> <p> Following up on <a href="/2012/05/29/BankOCRkatainFuserstory1">my initial post</a> about the <a href="http://codingdojo.org/cgi-bin/index.pl?KataBankOCR">Bank OCR kata</a>, this post walks through the code required to implement user story 2, which is about calculating a checksum. </p> <p> The code I previously described already contains a function called ParseToDigits which returns a sequence of digits, so it seems reasonable to express the validation function as based on a sequence of digits as input. </p> <h3 id="8e63aee7863f460ab4ac18cf09bb2d42"> The core unit test <a href="#8e63aee7863f460ab4ac18cf09bb2d42" title="permalink">#</a> </h3> <p> To ensure that the behavior of the new <em>IsValid</em> function would be correct, I wrote this <a href="http://xunitpatterns.com/Parameterized%20Test.html">Parameterized Test</a>: </p> <p> <pre style="margin: 0px">[&lt;Theory&gt;] [&lt;InlineData(1, 2, 3, 4, 5, 6, 7, 8, 9, <span style="color: blue">true</span>)&gt;] [&lt;InlineData(3, 4, 5, 8, 8, 2, 8, 6, 5, <span style="color: blue">true</span>)&gt;] [&lt;InlineData(3, 4, 5, 8, 8, 2, 8, 1, 4, <span style="color: blue">true</span>)&gt;] [&lt;InlineData(7, 1, 5, 8, 8, 2, 8, 6, 4, <span style="color: blue">true</span>)&gt;] [&lt;InlineData(7, 4, 5, 8, 8, 2, 8, 6, 5, <span style="color: blue">false</span>)&gt;] [&lt;InlineData(7, 4, 5, 8, 8, 2, 8, 6, 9, <span style="color: blue">false</span>)&gt;] [&lt;InlineData(7, 4, 5, 8, 8, 2, 8, 6, 8, <span style="color: blue">false</span>)&gt;] [&lt;InlineData(1, 2, 3, 4, 5, 6, 7, 8, 8, <span style="color: blue">false</span>)&gt;] [&lt;InlineData(1, 3, 3, 4, 5, 6, 7, 8, 8, <span style="color: blue">false</span>)&gt;] [&lt;InlineData(1, 3, 3, 4, 5, 6, 7, 8, 0, <span style="color: blue">false</span>)&gt;] <span style="color: blue">let</span> IsValidReturnsCorrectResult d9 d8 d7 d6 d5 d4 d3 d2 d1 expected = &nbsp;&nbsp;&nbsp; seq { <span style="color: blue">yield!</span> [d9; d8; d7; d6; d5; d4; d3; d2; d1] } &nbsp;&nbsp;&nbsp; |&gt; IsValid &nbsp;&nbsp;&nbsp; |&gt; should equal expected</pre> </p> <p> As was the case for the previous tests, this test utilizes <a href="http://xunit.codeplex.com/">xUnit.net's</a> data theories feature to succinctly express a parameterized test, as well as <a href="http://fsunit.codeplex.com/">FsUnit</a> for the assertion DSL. </p> <p> Once again I'd like to point out how the use of pipes enables me to very compactly follow the Arrange Act Assert (AAA) test pattern. </p> <h3 id="7487c4323a044698883f4b18e0884793"> Implementation <a href="#7487c4323a044698883f4b18e0884793" title="permalink">#</a> </h3> <p> The IsValid function is very simple: </p> <p> <pre style="margin: 0px"><span style="color: blue">let</span> IsValid digits = &nbsp;&nbsp;&nbsp; <span style="color: blue">match</span> digits |&gt; Seq.toArray <span style="color: blue">with</span> &nbsp;&nbsp;&nbsp; | [| d9; d8; d7; d6; d5; d4; d3; d2; d1 |] <span style="color: blue">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (d1 + 2 * d2 + 3 * d3 + 4 * d4 + 5 * d5 + 6 * d6 + 7 * d7 + 8 * d8 + 9 * d9) % 11 = 0 &nbsp;&nbsp;&nbsp; | _ <span style="color: blue">-&gt;</span> <span style="color: blue">false</span></pre> </p> <p> The input sequence is converted to an array by piping it into the Seq.toArray function. The resulting array is then matched against an expected shape. </p> <p> If the array contains exactly 9 elements, each element is decomposed into a named variable (d9, d8, etc.) and passed to the correct checksum formula. The formula only returns true if the calculated result is zero; otherwise it returns false. </p> <p> If the input doesn't match the expected pattern, the return value is false. The following tests prove this: </p> <p> <pre style="margin: 0px">[&lt;Fact&gt;] <span style="color: blue">let</span> IsValidOfTooShortSequenceReturnsFalse() = &nbsp;&nbsp;&nbsp; seq { <span style="color: blue">yield!</span> [3; 4; 5; 8; 8; 2; 8; 6] } &nbsp;&nbsp;&nbsp; |&gt; IsValid &nbsp;&nbsp;&nbsp; |&gt; should equal <span style="color: blue">false</span> &nbsp; [&lt;Fact&gt;] <span style="color: blue">let</span> IsValidOfTooLongSequenceReturnsFalse() = &nbsp;&nbsp;&nbsp; seq { <span style="color: blue">yield!</span> [3; 4; 5; 8; 8; 2; 8; 6; 5; 7] } &nbsp;&nbsp;&nbsp; |&gt; IsValid &nbsp;&nbsp;&nbsp; |&gt; should equal <span style="color: blue">false</span></pre> </p> <p> In a <a href="/2012/06/04/BankOCRkatainFuserstories3and4">future post I will walk you through user stories 3 and 4</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e08de3fd03f8445ba3aec3b666a270e0"> <div class="comment-author"><a href="http://blog.svick.org">svick</a> <a href="#e08de3fd03f8445ba3aec3b666a270e0">#</a></div> <div class="comment-content">Why are you using `seq { yield! [d9; d8; d7; d6; d5; d4; d3; d2; d1] }` instead of just `[d9; d8; d7; d6; d5; d4; d3; d2; d1]`? It seems to me it's just a complicated way to write List.toSeq, which isn't even necessary here.</div> <div class="comment-date">2012-06-01 23:45 UTC</div> </div> <div class="comment" id="4efbeb6218e6416685c6b25759b05122"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4efbeb6218e6416685c6b25759b05122">#</a></div> <div class="comment-content">That's a TDD artifact more than an actual requirement. I wrote the test first, and wanted it to specify that the IsValid function should be able to take any sequence, and not only a list.<br> <br> Because of F# powerful type inference, that's what the function ends up doing anyway, but with TDD, it's the test that specifies the shape of the API, not the other way around.<br> <br> However, I could also have written [d9; d8; d7; d6; d5; d4; d3; d2; d1] |&gt; List.toSeq... There's no particular reason why I chose a sequence expression over that...</div> <div class="comment-date">2012-06-02 10:13 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Bank OCR kata in F#: user story 1 https://blog.ploeh.dk/2012/05/29/BankOCRkatainF#userstory1 2012-05-29T06:26:34+00:00 Mark Seemann <div id="post"> <p> In case my <a href="/2012/05/25/Designpatternsacrossparadigms">previous post</a> left readers in doubt about whether or not I like Functional Programming (FP), this post should make it clear that I (also) love FP. </p> <p> A couple of years ago I had the pleasure of performing a technical review of <a href="http://www.amazon.com/gp/product/1933988924/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1933988924">Real World Functional Programming</a>, which caused me to significantly shift my C# coding style towards a more functional style. </p> <p> Now I want to take the next step, so I've started doing katas in F#. In a series of blog posts, I'll share my experiences with learning F# as I go along. I'm sure that the F# gods will wince from my code, but that's OK - if any of my readers find this educational, my goal will be met. </p> <p> In this post I'll start out with the first use case of the <a href="http://codingdojo.org/cgi-bin/index.pl?KataBankOCR">Bank OCR kata</a>. </p> <h3 id="f7e2138e69b247a89d686aad614e9088"> The core unit test <a href="#f7e2138e69b247a89d686aad614e9088" title="permalink">#</a> </h3> <p> The first thing I wanted to do was write a couple of tests to demonstrate that I could parse the input. This <a href="http://xunitpatterns.com/Parameterized%20Test.html">Parameterized Test</a> uses <a href="http://xunit.codeplex.com/">xUnit.net</a> data theories and <a href="http://fsunit.codeplex.com/">FsUnit</a> for the assertion: </p> <p> <pre style="margin: 0px;">[&lt;Theory&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; | _| _||_||_ |_&nbsp;&nbsp; ||_||_|</span> <span style="color: maroon;">&nbsp; ||_&nbsp; _|&nbsp; | _||_|&nbsp; ||_| _|"</span>, 123456789)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; | _| _||_||_ |_&nbsp;&nbsp; ||_||_|</span> <span style="color: maroon;">&nbsp; ||_&nbsp; _|&nbsp; | _||_|&nbsp; ||_||_|"</span>, 123456788)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; | _| _||_||_ |_&nbsp;&nbsp; ||_||_|</span> <span style="color: maroon;">&nbsp; | _| _|&nbsp; | _||_|&nbsp; ||_||_|"</span>, 133456788)&gt;] [&lt;InlineData(<span style="color: maroon;">"</span> <span style="color: maroon;">&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp;&nbsp;&nbsp; _&nbsp; _&nbsp; _&nbsp; _&nbsp; _ </span> <span style="color: maroon;">&nbsp; | _| _||_||_ |_&nbsp;&nbsp; ||_|| |</span> <span style="color: maroon;">&nbsp; | _| _|&nbsp; | _||_|&nbsp; ||_||_|"</span>, 133456780)&gt;] <span style="color: blue;">let</span> ParseToNumberReturnsCorrectResult entry expected = &nbsp;&nbsp;&nbsp; entry &nbsp;&nbsp;&nbsp; |&gt; ParseToNumber &nbsp;&nbsp;&nbsp; |&gt; should equal expected</pre> </p> <p> One of the many nice features of F# is that it's possible to break string literals over multiple lines, so instead of having one wide, unreadable string with "\r\n" instead of line breaks, I could write the test cases directly in the test source code and keep them legible. </p> <p> In the test function body itself, I pipe the <em>entry</em> function argument into the ParseToNumber function using the pipeline operator |&gt;, so </p> <p> <pre style="margin: 0px">entry |&gt; ParseToNumber</pre> </p> <p> is equivalent to writing </p> <p> <pre style="margin: 0px">ParseToNumber entry</pre> </p> <p> However, by piping the input into the <a href="http://xunitpatterns.com/SUT.html">SUT</a> and the result further on to the assertion, I achieve a very dense test method where it's fairly clear that it follows the Arrange Act Assert (AAA) pattern. </p> <p> The assertion is expressed using FsUnit's F# adapter over xUnit.net. It should be clear that it states that the result (piped from the ParseToNumber function) should be equal to the <em>expected</em> function argument. </p> <h3 id="01b1cfaf283c41a197214eef4e3f17bb"> Implementation <a href="#01b1cfaf283c41a197214eef4e3f17bb" title="permalink">#</a> </h3> <p> Here's the body of the ParseToNumber function: </p> <p> <pre style="margin: 0px"><span style="color: blue">let</span> ParseToNumber entry = &nbsp;&nbsp;&nbsp; entry &nbsp;&nbsp;&nbsp; |&gt; ParseToDigits &nbsp;&nbsp;&nbsp; |&gt; Seq.map Option.get &nbsp;&nbsp;&nbsp; |&gt; Seq.reduce (<span style="color: blue">fun</span> x y <span style="color: blue">-&gt;</span> x * 10 + y)</pre> </p> <p> If you're used to read and write C# you may be wondering about the compactness of it all. Where are all the type declarations? </p> <p> F# is a strongly typed language, but it has very sophisticated type inferencing capabilities, so the compiler is able to infer that the signature is string - &gt; int, which (in this case) is read as <em>a function which takes a string as input and returns an integer</em>. The equivalent C# notation would be public int ParseToNumber(string entry). </p> <p> How does the F# compiler know this? </p> <p> The <em>entry</em> parameter is easy to infer because the function body invokes the ParseToDigits function, which takes a string as input. Therefore, <em>entry</em> must be a string as well. </p> <p> The output type is a little harder to understand, but basically it boils down to something like this: the ParseToDigits function returns a sequence (basically, an IEnumerable&lt;T&gt;) of integer options. The Seq.map function converts this to a sequence of integers, and the Seq.fold function aggregates that sequence into a single integer. </p> <p> Don't worry if you didn't understand all that yet - I'll walk you through the body of the function now. </p> <p> The <em>entry</em> argument is piped into the ParseToDigits function, which returns a sequence of integer options. An <a href="http://msdn.microsoft.com/en-us/library/dd233245.aspx">option</a> is a type that either has, or doesn't have, a value, so the next step is to unwrap all the values. This is done by piping the option sequence into the Seq.map function, which is equivalent to the LINQ <a href="http://msdn.microsoft.com/en-us/library/bb548891.aspx">Select</a> method. The Option.get method simply unwraps a single option. It will throw an exception if the value is None, but in this implementation, this is the desired behavior. </p> <p> The last thing to do is to aggregate the sequence of integers into a single number. The Seq.reduce method aggregates the sequence according to a given accumulator function. </p> <p> In F#, an inline code block is written with the <em>fun</em> keyword, but it's entirely equivalent to a C# code block, which would look like this: </p> <p> <pre style="margin: 0px">(x, y) =&gt; x * 10 + y</pre> </p> <p> This function basically interprets the sequence of integers as a stream of digits, so in order to arrive at the correct digital representation of the number, each accumulated result is multiplied by ten and added to the new digit. </p> <p> Obviously, all the real parsing takes place in the ParseToDigits function: </p> <p> <pre style="margin: 0px"><span style="color: blue">let</span> ParseToDigits (entry : string) = &nbsp;&nbsp;&nbsp; <span style="color: blue">let</span> toIntOption ocrDigit = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">match</span> ocrDigit |&gt; Seq.toArray <span style="color: blue">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>); (<span style="color: maroon">'_'</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">'_'</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>); (<span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">' '</span>) |] <span style="color: blue">-&gt;</span> Some 2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'_'</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 4 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>); (<span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 6 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">'_'</span>, <span style="color: maroon">' '</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 7 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>); (<span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 8 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [| (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">' '</span>); (<span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>, <span style="color: maroon">'_'</span>); (<span style="color: maroon">' '</span>, <span style="color: maroon">'|'</span>, <span style="color: maroon">'|'</span>) |] <span style="color: blue">-&gt;</span> Some 9 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | _ <span style="color: blue">-&gt;</span> None &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">let</span> lines = entry.Split([| <span style="color: maroon">"\r\n"</span> |], StringSplitOptions.RemoveEmptyEntries) &nbsp; &nbsp;&nbsp;&nbsp; Seq.zip3 lines.[0] lines.[1] lines.[2] &nbsp;&nbsp;&nbsp; |&gt; chunk 3 &nbsp;&nbsp;&nbsp; |&gt; Seq.map toIntOption</pre> </p> <p> That may look scary, but is actually not that bad. </p> <p> The function contains the nested function <em>toIntOption</em> which is local to the scope of ParseToDigits. One of the things I don't like that much about F# is that you have to declare and implement the <em>details</em> before the general flow, so you almost have to read the code backwards if you want the overview before diving into all the details. </p> <p> The first thing the ParseToDigit function <em>does</em> is to split the input into three lines according to the specification of the kata. It uses the standard .NET <a href="http://msdn.microsoft.com/en-us/library/tabh47cf.aspx">String.Split method</a> to do that. </p> <p> Next, it zips each of these three lines with each other. When you zip something, you take the first element of each sequence and create a tuple out of those three elements; then the second element of each sequence, and so forth. The result is a sequence of tuples, where each tuple represents a vertical slice down a digit. </p> <p> As an example, let's look at the digit 0: </p> <p> <pre style="margin: 0px"><span style="color: maroon"> _ </span> <span style="color: maroon">| |</span> <span style="color: maroon">|_|</span></pre> </p> <p> The first vertical slice contains the characters ' ', '|' and '|' so that results in the tuple (' ', '|', '|'). The next vertical slice corresponds to the tuple ('_', ' ', '_') and the third (' ', '|', '|'). </p> <p> With me so far? OK, good. </p> <p> The Seq.zip3 function produces a sequence of such tuples, but since the kata states that each digit is exactly three characters wide, it's necessary to divide that stream of tuples into chunks of three. In order to do that, I pipe the zipped sequence to the chunk function. </p> <p> <pre style="margin: 0px">Seq.zip3 lines.[0] lines.[1] lines.[2] |&gt; chunk 3</pre> </p> <p> I'll post the chunk function further down, but the result is a sequence of sequence of chars which can then be mapped with the toIntOption function. </p> <p> Now that I've explained how those tuples look, the toIntOption shouldn't be too hard to understand. Each <em>ocrDigit</em> argument is expected to be a sequence of three tuples, so after creating an array from the sequence, the function uses <a href="http://msdn.microsoft.com/en-us/library/dd547125.aspx">pattern matching</a> to match each tuple array to an option value. </p> <p> It simply matches an array of the tuples (' ', '|', '|'); ('_', ' ', '_'); (' ', '|', '|') to the number 0 (as explained above), and so on. </p> <p> If there's no match, it returns the option value None, instead of Some. </p> <p> Hang on, I'm almost done. The last part of the puzzle is the <em>chunk</em> function: </p> <p> <pre style="margin: 0px"><span style="color: blue">let</span> <span style="color: blue">rec</span> chunk size sequence = &nbsp;&nbsp;&nbsp; <span style="color: blue">let</span> skipOrEmpty size s = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> s |&gt; Seq.truncate size |&gt; Seq.length &gt;= size &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">then</span> s |&gt; Seq.skip size &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">else</span> Seq.empty &nbsp; &nbsp;&nbsp;&nbsp; seq { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">yield</span> sequence |&gt; Seq.truncate size &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">let</span> next = sequence |&gt; skipOrEmpty size &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> not (Seq.isEmpty next) <span style="color: blue">then</span> <span style="color: blue">yield!</span> chunk size next &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</pre> </p> <p> Just like the ParseToDigits function, the chunk function uses a nested function to do some of its work. </p> <p> First of all you may notice that the function declaration uses the <em>rec</em> keyword, which means that this is a recursive function. FP is all about recursion, so this is just something you'll have to get used to. </p> <p> The main part of the function consists of a sequence expression, denoted by the <em>seq</em> keyword. </p> <p> First the function returns the first <em>n</em> elements from the sequence, in order to return the first chunk. Next, it skips that chuck and if the sequence isn't empty it recursively calls itself with the rest of the sequence. </p> <p> The <em>skipOrEmpty</em> function is just a little helper function to work around the behavior of the Seq.skip function, which throws an exception if you ask it to skip some elements of a sequence which doesn't contain that many elements. </p> <p> That's it. The code is actually quite compact, but I just took a long time explaining it all in some detail. </p> <p> In a <a href="/2012/06/01/BankOCRkatainFuserstory2">future post</a> I'll walk through the other use cases of the Bank OCR kata.<br> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="10d243cbb12b4e5d9d4729ffda247261"> <div class="comment-author">Simon Skov Boisen <a href="#10d243cbb12b4e5d9d4729ffda247261">#</a></div> <div class="comment-content">Great post! I just love how succinct F# code is. I'm a bit worried about the language though. I'm not sure how much Microsoft will support it in the future - the F# 3.0 features seems very awesome - but the language is still lagging a lot of functional stuff like functors and polymorphic variants.</div> <div class="comment-date">2012-05-29 12:16 UTC</div> </div> <div class="comment" id="e0e39467f02d444682d5cea423cf6b0d"> <div class="comment-author"><a href="http://blog.svick.org">svick</a> <a href="#e0e39467f02d444682d5cea423cf6b0d">#</a></div> <div class="comment-content">I'm not sure about your implementation of chunk. I think all helper functions that take IEnumerable (a.k.a. seq in F#) should iterate over the source at most once. That's because it will be more efficient and, more importantly, some sequences can be iterated only once.<br> <br> Yeah, in a small example like this it doesn't really matter and readability is more important. But I think things like this should be mentioned, so that people are not surprised if they try to use the function in different contexts.</div> <div class="comment-date">2012-05-29 12:42 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Design patterns across paradigms https://blog.ploeh.dk/2012/05/25/Designpatternsacrossparadigms 2012-05-25T03:47:29+00:00 Mark Seemann <div id="post"> <p> <em>This blog post makes the banal observation that design patterns tend to be intimately coupled to the paradigm in which they belong.</em> </p> <p> It seems as though lately it has become hip to dismiss design patterns as a bit of a crutch to overcome limitations in Object-Oriented Design (OOD). One example originates from <a href="http://www.amazon.com/gp/product/1935182641/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1935182641">The Joy of Clojure</a>:</p> <blockquote> <p> "In this section, we'll attempt to dissuade you from viewing Clojure features as design patterns [...] and instead as an inherent nameless quality.</p> <p> "[...]the patterns described [in <a href="http://www.amazon.com/gp/product/0201633612/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0201633612">Design Patterns</a>] are aimed at patching deficiencies in popular object-oriented programming languages. This practical view of design patterns isn't directly relevant to Clojure, because in many ways the patterns are ever-present and are first-class citizens of the language itself." (page 303) </p> </blockquote> <p> From what little I've understood about Clojure, that seems like a reasonable assertion. </p> <p> Another example is a tweet by <a href="http://sw1nn.com/">Neal Swinnerton</a>: </p> <blockquote> <p> "<a href="https://twitter.com/sw1nn/status/205589588390064128">With functions that accept functions and/or return functions 400 of the 600 pages of design patterns can be burnt says @stilkov ‪#euroclojure</a>" </p> </blockquote> <p> and <a href="http://www.innoq.com/blog/st/">Stefan Tilkov</a> elaborates: </p> <blockquote> <p> "<a href="https://twitter.com/stilkov/status/205644501283373057">@ploeh @sw1nn @ptrelford many OO DPs only exist to replace missing FP features in the first place</a>" </p> </blockquote> <p> First of all: I completely agree. In fact, I find these statements rather uncontroversial. The only reason I'm writing this is because I see this sentiment popping up more and more (but perhaps I'm just suffering from the Baader-Meinhof phenomenon). </p> <h3 id="54f56ae2c9c448e8990d1bb9274c64ea"> Patterns for OOD <a href="#54f56ae2c9c448e8990d1bb9274c64ea" title="permalink">#</a> </h3> <p> Many 'traditional' design patterns describe how to solve problems which are complex when working with object-oriented languages. A lot of those problems are inherent in the object-oriented paradigm. </p> <p> Other paradigms may provide inherent solutions to those problems. As an example, Functional Programming (FP) includes the central concept of <em>Function Composition.</em> Functions can be composed according to signature. In OOD terminology you can say that the signature of a function <em>defines</em> its type. </p> <p> Most of the classic design patterns are based upon the idea of <em>programming to an interface instead of a concrete class</em>. In OOD, it's necessary to point this out as a piece of explicit advice because the default in OOD is to program against a concrete class. </p> <p> That's not the case in FP because functions can be composed as long as their signatures are compatible. Loose coupling is, so to speak, baked into the paradigm. </p> <p> Thus, it's true that many of the OOD patterns are irrelevant in an FP context. That doesn't mean that FP doesn't need patterns. </p> <h3 id="56a01fc01a8244d6a74d00c434f1d719"> Patterns for FP <a href="#56a01fc01a8244d6a74d00c434f1d719" title="permalink">#</a> </h3> <p> So if FP (or Clojure, for that matter) inherently address many of the shortcomings of OOD that give rise to patterns, does it mean that design patterns are redundant in FP? </p> <p> Hardly. This is reminiscent of the situation of a couple of years ago when Ruby on Rails developers were pointing fingers at everyone else because they had superior productivity, but now when they are being tasked with maintaining complex system, they are learning the hard way that <a href="http://martinfowler.com/eaaCatalog/activeRecord.html">Active Record</a> is an anti-pattern. Duh. </p> <p> FP has shortcomings as well, and patterns will emerge to address them. While FP has been around for a long time, it hasn't been as heavily used (and thus subjected to analysis) as OOD, so the patterns may not yet have been formulated yet, but if FP gains traction (and I believe it will), patterns <em>will</em> emerge. However, they will be <em>different</em> patterns. </p> <p> Once we have an extensive body of patterns for FP, we'll be able to state the equivalent of the introductory assertion: </p> <blockquote> <p> Most of the established FP patterns address shortcomings of FP. Using the FloopyDoopy paradigm makes most of them redundant. </p> </blockquote> <p> What would be shortcomings of FP? I don't know them all, but here's a couple of suggestions: </p> <h3 id="1bed928c4861410a95d581c7befebbe2"> Mutability <a href="#1bed928c4861410a95d581c7befebbe2" title="permalink">#</a> </h3> <p> Apart from calculation-intensive software, most software is actually all about mutating state: Take an order. Save to the database. Write a file. Send an email. Render a view. Print a document. Write to the console. Send a message... </p> <p> In FP they've come up with this clever concept of <a href="http://en.wikipedia.org/wiki/Monad_%28functional_programming%29">monads</a> to 'work around' the problem of mutating state. Yes, monads are very clever, but if they feel foreign in OOD it's because they're not required. Mutation is an inherent part of the OOD paradigm, and it very intuitively maps to the 'real world', where mutation happens all the time. Monads are cool, but not particularly intuitive. </p> <p> FP practitioners may not realize this, but a monad is a design pattern invented to address a shortcoming in 'pure' functional languages. </p> <h3 id="ebe4a8c5ba664c6fb5ea07c8b7e18555"> Discoverability <a href="#ebe4a8c5ba664c6fb5ea07c8b7e18555" title="permalink">#</a> </h3> <p> As <a href="http://trelford.com/blog/">Phil Trelford</a> kindly pointed out at <a href="http://gotocon.com/cph-2012/">GOTO Copenhagen 2012</a>, OOD is often characterized by 'dot-driven development.' What does that mean? </p> <p> It means that given a variable, we can often just enter ".", and our IDE is going to give us a list of methods we can call on the object: </p> <p> <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="IntelliSense" border="0" alt="IntelliSense" src="/content/binary/Windows-Live-Writer/Design-patterns-across-paradigms_F4A0/IntelliSense_3.png" width="231" height="178"> </p> <p> Since behavior is contained by each type, we can use patterns such as <a href="http://martinfowler.com/bliki/FluentInterface.html">Fluent Interface</a> to make it easy to learn a new API. While we can laugh at the term 'dot-driven development' it's hard to deny that it makes it very easy to learn. </p> <p> The API itself carries the information about how to use it, and what is possible. That's very <a href="http://www.amazon.com/gp/product/0132350882/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code</a>. Out-of-band documentation isn't required. </p> <p> I wouldn't even know how to address this shortcoming in FP, but I'm sure patterns will evolve. </p> <h3 id="4d3d112235a24fae9626b353532bfffd"> Different paradigms require different patterns <a href="#4d3d112235a24fae9626b353532bfffd" title="permalink">#</a> </h3> <p> All of this boils down to a totally banal observation: </p> <blockquote> <p> Most patterns address shortcomings specific to a paradigm </p> </blockquote> <p> Saying that most of the OOD patterns are redundant in FP is like saying that you don't need oven mittens to pour a glass of water. </p> <p> There might be a slight overlap, but I'd expect most patterns to be tightly coupled to the paradigm in which they were originally stated. </p> <p> <a href="/content/binary/Windows-Live-Writer/Design-patterns-across-paradigms_F4A0/patternsvenn_2.png"><img src="/content/binary/Windows-Live-Writer/Design-patterns-across-paradigms_F4A0/patternsvenn_thumb.png"></a> </p> <p> There might be a few patterns that are generally applicable. </p> <p> The bottom line is that FP isn't inherently better just because many of the OOD design patterns are redundant. Neither is OOD inherently better. They are <em>different</em>. That's all. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="3bcf51969a3e411a883f4da0288aa5c6"> <div class="comment-author">Martin <a href="#3bcf51969a3e411a883f4da0288aa5c6">#</a></div> <div class="comment-content">Good post, I entirely agree with the conclusion.<br> <br> Reg. mutatable state:<br> <br> &quot;<i>Mutation is an inherent part of the OOD paradigm, and it very intuitively maps to the 'real world', where mutation happens all the time.</i>&quot;<br> <br> As Rich Hickley also told us at Goto Copenhagen, above statement may not be entirely true. Current state changes, but that does not mean that previous state ceases to exist. Also, as software devs we are not necessarily interested in representing the real world, but rather a view of the world that suits our business domain. So the question becomes: does the OOD notion of mutable state suit our business domain, or is the FP model, where everything is an immutable value in time, a better fit?</div> <div class="comment-date">2012-05-25 12:42 UTC</div> </div> <div class="comment" id="b572e3d2fc6d4f0d9d790cc9ec331ad0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b572e3d2fc6d4f0d9d790cc9ec331ad0">#</a></div> <div class="comment-content">Martin, I agree that often the FP view is superior because it contains more information. What I said was that mutation is often more <em>intuitive</em>. Consider a kettle of boiling water: over time, the water in the kettle mutates - it becomes steam.<br> <br> While we are programmers, we are still human, and unless you get involved in pretty advanced theoretical physics, a practical and very precise world view is that objects change state over time.</div> <div class="comment-date">2012-05-25 13:07 UTC</div> </div> <div class="comment" id="bfebe3e04dce4537959da06d1d656114"> <div class="comment-author"><a href="http://byterot.blogspot.com">Ali</a> <a href="#bfebe3e04dce4537959da06d1d656114">#</a></div> <div class="comment-content">Nice post in a mostly neglected subject. I am really interested to understand such patterns.<br> <br> Alson, I see dot-driven development even more production in functional programming, or at least what I do as FP in C#. Function composition is nicely enhanced using static extensions over Func, Action and Task. I used this in one of my posts on comparing performance of object instantiation methods.</div> <div class="comment-date">2012-06-03 20:43 UTC</div> </div> <div class="comment" id="32fae4b99d8641258770262ee9bc45d9"> <div class="comment-author">anonymous coward <a href="#32fae4b99d8641258770262ee9bc45d9">#</a></div> <div class="comment-content">This <br> http://c2.com/cgi/wiki?AreDesignPatternsMissingLanguageFeatures<br> discussed the necessity of Pattern in FP. <br> The notion that pattern are not required in FP seems to be around for some time, <br> PaulGraham said &quot;Peter Norvig found that 16 of the 23 patterns in Design Patterns were 'invisible or simpler' in Lisp.&quot; http://www.norvig.com/design-patterns/</div> <div class="comment-date">2012-06-13 06:53 UTC</div> </div> <div class="comment" id="5a85425ac8fc473981786d66d2dc0711"> <div class="comment-author">Rant Antiblog <a href="#5a85425ac8fc473981786d66d2dc0711">#</a></div> <div class="comment-content">Very naive article, unfortunately written with a lot of conviction.<br> - Trying to combine OOD and pure FP is pointless and misses the point. Less is more.<br> - Design patterns are analogous to cooking recipes.<br> - Monads are not a &quot;design pattern invented to address a shortcoming in 'pure' functional languages&quot;.<br> </div> <div class="comment-date">2012-06-13 08:57 UTC</div> </div> <div class="comment" id="65e0fd5b456247f1b49ad4764c918c5b"> <div class="comment-author"><a href="http://gsscoder.github.com">Giacomo Stelluti Scala</a> <a href="#65e0fd5b456247f1b49ad4764c918c5b">#</a></div> <div class="comment-content"> <p> I came late to comment, but again I'm here due to an exchange of tweets with the author. I've pointed out a S.O. question titled <a href="http://stackoverflow.com/questions/5577054/solid-for-functional-programming">SOLID for functional programming</a> with a totally misleading answer (in my opinion, it's clear). </p> <p> In that case the answer lacks technical correctness, but in this post Mark Seemann points out a concept that is difficult to contradict.</p><p> Patterns are <i>abstraction</i> that provides <i>concrete</i> solutions (pardon the pun) to identifiable problems that present themselves in different shapes.<br> Tha fact that when we talk of patterns we talk of them in relation to <b>OO languages</b>, it just a contingency. </p> <p> I've to completely agree with the author; <b>functional languages</b> need patterns too and as more these become popular as more <b>functional patterns will arise</b>. </p> </div> <div class="comment-date">2013-17-03 09:19 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. TDD test suites should run in 10 seconds or less https://blog.ploeh.dk/2012/05/24/TDDtestsuitesshouldrunin10secondsorless 2012-05-24T15:20:48+00:00 Mark Seemann <div id="post"> <p> Most guidance about Test-Driven Development (TDD) will tell you that unit tests should be fast. Examples of such guidance can be found in <a href="http://agileinaflash.blogspot.com/2009/02/first.html">FIRST</a> and <a href="http://www.amazon.com/gp/product/0131495054/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0131495054">xUnit Test Patterns</a>. Rarely does it tell you <em>how</em> fast unit tests should be. </p> <p> 10 seconds for a unit test suite. Max. </p> <p> Here's why. </p> <p> When you follow the <a href="http://agileinaflash.blogspot.com/2009/02/red-green-refactor.html">Red/Green/Refactor</a> process, ideally you'd be running your unit test suite at least three times for each iteration: </p> <ol> <li>Red. <strong>Run</strong> the test suite.</li> <li>Green. <strong>Run</strong> the test suite.</li> <li>Refactor. <strong>Run</strong> the test suite.</li> </ol> <p> Each time you run the unit test suite, you're essentially blocked. You have to wait until the test run has completed until you get the result. </p> <p> During that wait time, it's important to keep focus. If the test run takes too long, your mind starts to wonder, and you'll suffer from context switching when you have to resume work. </p> <p> When does your mind start to wonder? After about 10 seconds. That number just keeps popping up when the topic turns to <a href="http://en.wikipedia.org/wiki/Attention_span">focused attention</a>. Obviously it's not a hard limit. Obviously there are individual and contextual variations. Still, it seems as though a 10 second short-term attention span is more or less hard-wired into the human brain. </p> <p> Thus, a unit test suite <em>used for TDD</em> should run in less than 10 seconds. If it's slower, you'll be less productive because you'll constantly lose focus. </p> <h3 id="5b93271a4bf44241a9bf56dd41383c12"> Implications <a href="#5b93271a4bf44241a9bf56dd41383c12" title="permalink">#</a> </h3> <p> The test suite you work with when you do TDD should execute in less than 10 seconds <em>on your machine</em>. If you have hundreds of tests, each test should be faster than 100 milliseconds. If you have thousands, each test should be faster than 10 milliseconds. You get the picture. </p> <p> That test suite doesn't need to be the same as is running on your CI server. It could be a subset of tests. That's OK. The TDD suite may just be part of your <a href="http://martinfowler.com/bliki/TestPyramid.html">Test Pyramid</a>. </p> <h3 id="d352378c76b94c90a12b05ec66312b16"> Selective test runs <a href="#d352378c76b94c90a12b05ec66312b16" title="permalink">#</a> </h3> <p> Many people work around the <em>Slow Tests</em> anti-pattern by only running one test at a time, or perhaps one test class. In my experience, this is not an optimal solution because it slows you down. Instead of just going </p> <ol> <li>Red</li> <li><strong>Run</strong></li> <li>Green</li> <li><strong>Run</strong></li> <li>Refactor</li> <li><strong>Run</strong></li> </ol> <p> you'd need to go </p> <ol> <li>Red</li> <li>Specifically instruct your Test Runner to run only the test you just wrote</li> <li>Green</li> <li>Decide which subset of tests may have been affected by the new test. This obviously involves the new test, but may include more tests.</li> <li>Run the tests you just selected</li> <li>Refactor</li> <li>Decide which subset of tests to run now</li> <li>Run the tests you just selected</li> </ol> <p> Obviously, that introduces friction into your process. Personally, I much prefer to have a fast test suite that I can run all the time at a key press. </p> <p> Still, there are tools available that promises to do this analysis for you. One of them are <a href="http://continuoustests.com/">Mighty Moose</a>, with which I've had varying degrees of success. Another similar approach is <a href="http://www.ncrunch.net/">NCrunch</a>. </p> <h3 id="e0b07145040b4b2b9fc4d7a2bd60fafe"> References? <a href="#e0b07145040b4b2b9fc4d7a2bd60fafe" title="permalink">#</a> </h3> <p> For a long time I've been wanting to write this article, but I've always felt held back by a lack of citations I could exhibit. While <a href="http://en.wikipedia.org/wiki/Attention_span">the Wikipedia link</a> does provide a bit of information, it's not that convincing in itself (and it also cites the time span as 8 seconds). </p> <p> However, that 10 second number just <em>keeps</em> popping up in all sorts of contexts, not all of them having anything to do with software development, so I decided to run with it. However, if some of my readers can provide me with better references, I'd be delighted. </p> <p> Or perhaps I'm just suffering from <a href="http://en.wikipedia.org/wiki/Confirmation_bias">confirmation bias</a>... </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="548630f487244e038f7045461d6fe7c5"> <div class="comment-author"><a href="http://binarysculpting.com">Per Dervall</a> <a href="#548630f487244e038f7045461d6fe7c5">#</a></div> <div class="comment-content">What are your thoughts on continuous test runners like NCrunch? The time for test running is effectively reduced to zero, but you no longer get the red/greeen cycle. Is the cycle crucial, or the notion of running test often the important bit?</div> <div class="comment-date">2012-05-24 17:52 UTC</div> </div> <div class="comment" id="8428d612787844c1a9aa523c210f3040"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8428d612787844c1a9aa523c210f3040">#</a></div> <div class="comment-content">In Agile we have this concept of <em>rapid feedback</em>. That a test runner runs in the background doesn't address the problem of Slow Tests. It's no good if you start writing more code and then five minutes later, your continuous test runner comes back and tells you that something you did five minutes ago caused the test suite to fail. That's not timely feedback.<br> <br> Some of that can be alleviated with a DVCS, but rapid feedback just makes things a lot easier.</div> <div class="comment-date">2012-05-24 18:39 UTC</div> </div> <div class="comment" id="e66ebdca70274c3ebeb56391724055ab"> <div class="comment-author">Vasily Kirichenko <a href="#e66ebdca70274c3ebeb56391724055ab">#</a></div> <div class="comment-content">&gt;&gt; Still, there are tools available that promises to do this analysis for you. One of them are <br> &gt;&gt; Mighty Moose, with which I've had varying degrees of success. Another similar approach is NCrunch.<br> <br> I tried both the tools in a real VS solution (contained about 180 projects, 20 of them are test ones). Mighty Moose was continuously throwing exceptions and I didn't manage to get it work at all. NCrunch could not compile some of projects (it uses Mono compiler) but works with some success with the remained ones. Yes, feedback is rather fast (2-5 secs instead of 10-20 using ReSharper test runner), but it unstable and I've returned to ReSharper back. </div> <div class="comment-date">2012-05-25 05:27 UTC</div> </div> <div class="comment" id="575b068aaa8946c281aa753267b8320a"> <div class="comment-author"><a href="http://agileinaflash.com/">Tim Ottinger</a> <a href="#575b068aaa8946c281aa753267b8320a">#</a></div> <div class="comment-content">As a practical matter, I've seen up to 45 seconds be _tolerable_, not ideal.</div> <div class="comment-date">2012-06-04 13:39 UTC</div> </div> <div class="comment" id="5664480df8d141e0abbb492fb10a4a66"> <div class="comment-author"><a href="http://microsoft.com">Shmoo</a> <a href="#5664480df8d141e0abbb492fb10a4a66">#</a></div> <div class="comment-content">I fully agree.<br> <br> TDD cannot be used for large applications.</div> <div class="comment-date">2012-06-22 06:43 UTC</div> </div> <div class="comment" id="7372aa6a7f344a1da3d17ec91bae0cbe"> <div class="comment-author"><a href="https://www.petermorlion.com/">Peter Morlion</a> <a href="#7372aa6a7f344a1da3d17ec91bae0cbe">#</a></div> <div class="comment-content"> <p>I was doing some research myself. From what I understand, the Wikipedia article you link to talks mainly about attention span in the context of someone doing a certain task and being disturbed. However, while we wait for our unit tests to finish, we're not doing anything. This is more similar to waiting for a website to download and render.</p> <p> The studies on "tolerable waiting time" are all over the map, but even old ones talk about 2 seconds. <a href="https://www.researchgate.net/publication/220893869_A_Study_on_Tolerable_Waiting_Time_How_Long_Are_Web_Users_Willing_to_Wait">This paper</a> mentions several studies, two of them from pre-internet days (scroll down to the graphics for a comparison table). This would mean that, ideally, we would need our tests to run in not 10 but 2 seconds! I say ideally, because this seams unrealistic to me at this moment (especially in projects where even compilation takes longer than 2 seconds). Maybe in the future, who knows.</p></div> <div class="comment-date">2018-09-26 14:35 UTC</div> </div> <div class="comment" id="b71215b8ea9648009c98b9b5948e70f5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b71215b8ea9648009c98b9b5948e70f5">#</a></div> <div class="comment-content"> <p> Peter, thank you for writing. I recall reading about the 10 second rule some months before I wrote the article, but that, when writing the article, I had trouble finding public reference material. Thank you for making the effort of researching this and sharing your findings. I don't dispute what you wrote, but here are some further thoughts on the topic: </p> <p> If the time limit really is two seconds, that's cause for some concern. I agree with you that it might be difficult to achieve that level of response time for a moderately-sized test suite. This does, however, heavily depend on various factors, the least of which isn't the language and platform. </p> <p> For instance, when working with a 'warm' C# code base, compilation time can be fast enough that you might actually be able to compile and run hundreds of tests within two seconds. On the other hand, just <em>compiling</em> a moderately-sized Haskell code base takes longer than two seconds on my machine (but then, once Haskell compiles, you don't need a lot of tests to verify that it works correctly). </p> <p> When working in interpreted languages, like SmallTalk (where TDD was originally <a href="https://www.quora.com/Why-does-Kent-Beck-refer-to-the-rediscovery-of-test-driven-development">rediscovered</a>), Ruby, or JavaScript, there's no compilation step, so tests start running immediately. Being interpreted, the test code may run slower than compiled code, but my limited experience with JavaScript is that it can still be fast enough. </p> </div> <div class="comment-date">2018-09-26 18:01 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Vendor Media Types With the ASP.NET Web API https://blog.ploeh.dk/2012/04/24/VendorMediaTypesWiththeASP.NETWebAPI 2012-04-24T11:45:41+00:00 Mark Seemann <div id="post"> <p> In RESTful services, media types (e.g. application/xml, application/json) are an important part of Content Negotiation (<em>conneg</em> in the jargon). This enables an API to provide multiple <em>representations</em> of the same resource. </p> <p> Apart from the standard media types such as application/xml, application/json, etc. an API can (and often should, IMO) expose its resources using specialized media types. These often take the form of vendor-specific media types, such as application/vnd.247e.catalog+xml or application/vnd.247e.album+json. </p> <p> In this article I'll present some initial findings I've made while investigating this in the <a href="http://www.asp.net/web-api">ASP.NET Web API</a> (beta). </p> <p> For an introduction to conneg with the Web API, see <a href="http://weblogs.asp.net/gunnarpeipman/default.aspx">Gunnar Peipman's ASP.NET blog</a>, particularly these two posts: </p> <ul> <li><a href="http://weblogs.asp.net/gunnarpeipman/archive/2012/04/19/asp-net-web-api-how-content-negotiation-works.aspx">ASP.NET Web API: How content negotiation works?</a></li> <li><a href="http://weblogs.asp.net/gunnarpeipman/archive/2012/04/20/asp-net-web-api-extending-content-negotiation-with-new-formats.aspx">ASP.NET Web API: Extending content negotiation with new formats</a></li> </ul> <h3 id="43f6e7325d374cbe8ec9b806913e30aa"> The Problem <a href="#43f6e7325d374cbe8ec9b806913e30aa" title="permalink">#</a> </h3> <p> In a particular RESTful API, I'd like to enable vendor-specific media types as well as the standard application/xml and application/json media types. </p> <p> More specifically, I'd like to add these media types to the API: </p> <ul> <li>application/vnd.247e.album+xml</li> <li>application/vnd.247e.artist+xml</li> <li>application/vnd.247e.catalog+xml</li> <li>application/vnd.247e.search-result+xml</li> <li>application/vnd.247e.track+xml</li> <li>application/vnd.247e.album+json</li> <li>application/vnd.247e.artist+json</li> <li>application/vnd.247e.catalog+json</li> <li>application/vnd.247e.search-result+json</li> <li>application/vnd.247e.track+json</li> </ul> <p> However, I can't just add all these media types to GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes or GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes. If I do that, each and every resource in the API would accept and (claim to) return all of those media types. That's not what I want. Rather, I want specific resources to accept and return specific media types. </p> <p> For example, if a resource (Controller) returns an instance of the SearchResult (Model) class, it should only accept the media types application/vnd.247e.search-result+xml, application/vnd.247e.search-result+json (as well as the standard application/xml and application/json media types). </p> <p> Likewise, a resource handling the Album (Model) class should accept and return application/vnd.247e.album+xml and application/vnd.247e.album+json, and so on. </p> <p> Figuring out how to enable such behavior took me a bit of fiddling (yes, <a href="http://fiddler2.com/fiddler2/">Fiddler</a> was involved). </p> <h3 id="edf8965786a24fbcbf0d59f16f891217"> The Solution <a href="#edf8965786a24fbcbf0d59f16f891217" title="permalink">#</a> </h3> <p> The Web API uses a polymorphic collection of <a href="http://msdn.microsoft.com/en-us/library/system.net.http.formatting.mediatypeformatter%28v=vs.108%29.aspx">MediaTypeFormatter</a> classes. These classes can be extended to be more specifically targeted at a specific Model class. </p> <p> For XML formatting, this can be done by deriving from the built-in XmlMediaTypeFormatter class: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">TypedXmlMediaTypeFormatter</span> : <span style="color: #2b91af">XmlMediaTypeFormatter</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Type</span> resourceType; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> TypedXmlMediaTypeFormatter(<span style="color: #2b91af">Type</span> resourceType, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MediaTypeHeaderValue</span> mediaType) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.resourceType = resourceType; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.SupportedMediaTypes.Clear(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.SupportedMediaTypes.Add(mediaType); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> CanReadType(<span style="color: #2b91af">Type</span> type) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.resourceType == type; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> CanWriteType(<span style="color: #2b91af">Type</span> type) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.resourceType == type; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The implementation is quite simple. In the constructor, it makes sure to clear out any existing supported media types and to add only the media type passed in via the constructor. </p> <p> The CanReadType and CanWriteType overrides only return true of the type parameter matches the type targeted by the particular TypedXmlMediaTypeFormatter instance. You could say that the TypedXmlMediaTypeFormatter provides a specific match between a media type and a resource Model class. </p> <p>The JSON formatter is similar:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">TypedJsonMediaTypeFormatter</span> : <span style="color: #2b91af">JsonMediaTypeFormatter</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Type</span> resourceType; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> TypedJsonMediaTypeFormatter(<span style="color: #2b91af">Type</span> resourceType, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MediaTypeHeaderValue</span> mediaType) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.resourceType = resourceType; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.SupportedMediaTypes.Clear(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.SupportedMediaTypes.Add(mediaType); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> CanReadType(<span style="color: #2b91af">Type</span> type) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.resourceType == type; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> CanWriteType(<span style="color: #2b91af">Type</span> type) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.resourceType == type; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The only difference from the TypedXmlMediaTypeFormatter class is that this one derives from JsonMediaTypeFormatter instead of XmlMediaTypeFormatter. </p> <p> With these two classes available, I can now register all the custom media types in Global.asax like this: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedXmlMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">Album</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.album+xml"</span>))); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedXmlMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">Artist</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.artist+xml"</span>))); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedXmlMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">Catalog</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.catalog+xml"</span>))); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedXmlMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">SearchResult</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.search-result+xml"</span>))); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedXmlMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">Track</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.track+xml"</span>))); &nbsp; <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedJsonMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">Album</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.album+json"</span>))); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedJsonMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">Artist</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.artist+json"</span>))); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedJsonMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">Catalog</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.catalog+json"</span>))); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedJsonMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">SearchResult</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.search-result+json"</span>))); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.Add( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">TypedJsonMediaTypeFormatter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">Track</span>), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"application/vnd.247e.track+json"</span>)));</pre> </p> <p> This is rather repetitive code, but I'll leave it as an exercise to the reader to write a set of conventions that appropriately register the correct media type for a Model class. </p> <h3 id="f3e2f7c5377949fb9ec945a06ff7b1a2"> Caveats <a href="#f3e2f7c5377949fb9ec945a06ff7b1a2" title="permalink">#</a> </h3> <p> Please be aware that I've only tested this with a read-only API. You may need to tweak this solution in order to also handle incoming data. </p> <p> As far as I can tell from the <a href="http://aspnetwebstack.codeplex.com/SourceControl/list/changesets">Web API source repository</a>, it seems as though there are some breaking changes in the pipeline in this area, so don't bet the farm on this particular solution. </p> <p> Lastly, it seems as though this solution doesn't correctly respect opt-out quality parameters in incoming Accept headers. As an example, if I request a 'Catalog' resource, but supply the following Accept header, I'd expect the response to be <code>406 (Not Acceptable)</code>. </p> <p> <pre>Accept: application/vnd.247e.search-result+xml; q=1, */*; q=0.0</pre> </p> <p> However, the result is that the service falls back to its default representation, which is <code>application/json</code>. Whether this is a problem with my approach or a bug in the Web API, I haven't investigated. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="487945daf8d24d99a3e6e79d5f7dd5fa"> <div class="comment-author"><a href="/2012/04/17/InjectingHttpControllerContextWi">Dmitry</a> <a href="#487945daf8d24d99a3e6e79d5f7dd5fa">#</a></div> <div class="comment-content">For those who are interested in returning 406 depending on client Accept header, there is an article about it and the my attempt to improve the code in it:<br> <br> http://pedroreys.com/2012/02/17/extending-asp-net-web-api-content-negotiation/<br> https://gist.github.com/2499672</div> <div class="comment-date">2012-05-03 02:10 UTC</div> </div> <div class="comment" id="e50060644507450a8d35ea90b0eba5ce"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e50060644507450a8d35ea90b0eba5ce">#</a></div> <div class="comment-content">FWIW, I <a href="http://aspnetwebstack.codeplex.com/workitem/93">submitted that particular issue as a bug</a>.</div> <div class="comment-date">2012-05-03 04:36 UTC</div> </div> <div class="comment" id="5d7198bac97747329381ede9e567d89e"> <div class="comment-author"><a href="http://byterot.blogspot.com">Ali</a> <a href="#5d7198bac97747329381ede9e567d89e">#</a></div> <div class="comment-content">According to <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP Spec</a>: &quot;Use of non-registered media types is discouraged&quot;<br> <br> <br> You might find <a href="http://byterot.blogspot.co.uk/2012/12/5-levels-of-media-type-rest-csds.html">this</a> useful. </div> <div class="comment-date">2012-12-09 14:37 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Wiring HttpControllerContext With Castle Windsor https://blog.ploeh.dk/2012/04/19/WiringHttpControllerContextWithCastleWindsor 2012-04-19T15:14:30+00:00 Mark Seemann <div id="post"> <p> In a previous post I demonstrated <a href="/2012/04/17/InjectingHttpControllerContextWiththeASP.NETWebAPI">how to wire up HttpControllerContext with Poor Man's DI</a>. In this article I'll show how to wire up HttpControllerContext with Castle Windsor. </p> <p> This turns out to be remarkably difficult, at least with the constraints I tend to set for myself: </p> <ul> <li> Readers of this blog may have an inkling that I have an absolute abhorrence of static state, so anything relying on that is out of the question. </li> <li> In addition to that, I also prefer to leverage the container as much as possible, so I'm not keen on duplicating a responsibility that really belongs to the container. </li> <li> No injecting the container into itself. That's just unnatural and lewd (without being fun). </li> <li> If possible, the solution should be thread-safe. </li> <li> The overall solution should still adhere to the <a href="/2010/09/29/TheRegisterResolveReleasepattern">Register Resolve Release pattern</a>, so registering a captured HttpControllerContext is a no-go. It's also unlikely to work, since you'd need to somehow scope each registered instance to its source request. </li> <li> That also rules out nested containers. </li> </ul> <p> OK, so given these constraints, how can an object graph like this one be created, only with Castle Windsor instead of Poor Man's DI? </p> <p> <pre style="margin: 0px"><span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">CatalogController</span>( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">RouteLinker</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; baseUri, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; controllerContext));</pre> </p> <p> Somehow, the HttpControllerContext instance must be captured and made available for further resolution of a CatalogController instance. </p> <h3 id="40d9f84da7c9497093a6dafea72eba2a"> Capturing the HttpControllerContext <a href="#40d9f84da7c9497093a6dafea72eba2a" title="permalink">#</a> </h3> <p> The first step is to capture the HttpControllerContext instance, as early as possible. From my previous post it should be reasonably clear that the best place to capture this instance is from within IHttpControllerActivator.Create. </p> <p> Here's the requirement: the HttpControllerContext instance must be captured and made available for further resolution of the object graph - preferably in a thread-safe manner. Ideally, it should be captured in a thread-safe object that can start out uninitialized, but then allow exactly one assignment of a value. </p> <p> At the time I first struggled with this, I was just finishing <a href="http://www.amazon.com/gp/product/1935182641/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1935182641">The Joy of Clojure</a>, so this sounded to me an awful lot like the description of a <a href="http://clojuredocs.org/clojure_core/clojure.core/promise">promise</a>. Crowdsourcing on Twitter turned up that the .NET equivalent of a promise is <a href="http://msdn.microsoft.com/en-us/library/dd449174.aspx">TaskCompletionSource&lt;T&gt;</a>. </p> <p> Creating a custom IHttpControllerActivator with an injected TaskCompletionSource&lt;HttpControllerContext&gt; sounds like a good approach. If the custom IHttpControllerActivator can be scoped to a specific request, that would be the solution then and there. </p> <p> However, as I've <a href="/2012/03/20/RobustDIWiththeASP.NETWebAPI">previously described</a>, the current incarnation of the ASP.NET Web API has the unfortunate behavior that all framework Services (such as IHttpControllerActivator) are resolved once and cached forever (effectively turning them into having the Singleton lifestyle, despite what you may attempt to configure in your container). </p> <blockquote> <p> With Dependency Injection, the common solution to bridge the gap between a long-lasting lifestyle and a shorter lifestyle is a factory. </p> </blockquote> <p> Thus, instead of injecting TaskCompletionSource&lt;HttpControllerContext&gt; into a custom IHttpControllerActivator, a Func&lt;TaskCompletionSource&lt;HttpControllerContext&gt;&gt; can be injected to bridge the lifestyle gap. </p> <p> One other thing: the custom IHttpControllerActivator is only required to capture the HttpControllerContext for further reference, so I don't want to reimplement all the functionality of DefaultHttpControllerActivator. This is the reason why the custom IHttpControllerActivator ends up being a <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a>: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ContextCapturingControllerActivator</span> : &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IHttpControllerActivator</span> activator; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">TaskCompletionSource</span>&lt;<span style="color: #2b91af">HttpControllerContext</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; promiseFactory; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> ContextCapturingControllerActivator( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">TaskCompletionSource</span>&lt;<span style="color: #2b91af">HttpControllerContext</span>&gt;&gt; promiseFactory, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IHttpControllerActivator</span> activator) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.activator = activator; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.promiseFactory = promiseFactory; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">IHttpController</span> Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">HttpControllerContext</span> controllerContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Type</span> controllerType) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.promiseFactory().SetResult(controllerContext); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.activator.Create(controllerContext, controllerType); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The ContextCapturingControllerActivator class simply Decorates another IHttpControllerActivator and does <em>one</em> thing before delegating the call to the inner implementation: it uses the factory to create a new instance of TaskCompletionSource&lt;HttpControllerContext&gt; and assigns the HttpControllerContext instance to that <em>promise</em>. </p> <h3 id="40ae32526cb247d7a0d4f6e92c97a5f5"> Scoping <a href="#40ae32526cb247d7a0d4f6e92c97a5f5" title="permalink">#</a> </h3> <p> Because the Web API is basically being an ass (I can write this here, because I'm gambling that the solitary reader making it this far is so desperate that he or she is not going to care about the swearing) by treating framework Services as Singletons, it doesn't matter how it's being registered: </p> <p> <pre style="margin: 0px">container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">IHttpControllerActivator</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">ContextCapturingControllerActivator</span>&gt;()); container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">IHttpControllerActivator</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">DefaultHttpControllerActivator</span>&gt;());</pre> </p> <p> Notice that because Castle Windsor is being such an awesome library that it implicitly understands the Decorator pattern, I can simple register both Decorator and Decoratee in an ordered sequence. </p> <p> The factory itself must also be registered as a Singleton (the default in Castle Windsor): </p> <p> <pre style="margin: 0px">container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">TaskCompletionSource</span>&lt;<span style="color: #2b91af">HttpControllerContext</span>&gt;&gt;&gt;() &nbsp;&nbsp;&nbsp; .AsFactory());</pre> </p> <p> Here, I'm taking advantage of Castle Windsor's <a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility.ashx">Typed Factory Facility</a>, so I'm simply asking it to treat a Func&lt;TaskCompletionSource&lt;HttpControllerContext&gt;&gt; as an <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a>. Doing that means that every time the delegate is being invoked, Castle Windsor will create an instance of TaskCompletionSource&lt;HttpControllerContext&gt; with the correct lifetime. </p> <p> This provides the bridge from Singleton lifestyle to PerWebRequest: </p> <p> <pre style="margin: 0px">container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">TaskCompletionSource</span>&lt;<span style="color: #2b91af">HttpControllerContext</span>&gt;&gt;() &nbsp;&nbsp;&nbsp; .LifestylePerWebRequest());</pre> </p> <p> Notice that TaskCompletionSource&lt;HttpControllerContext&gt; is registered with a PerWebRequest lifestyle, which means that every time the above delegate is invoked, it's going to create an instance which is scoped to the current request. This is exactly the desired behavior. </p> <h3 id="36c0398defb24c51b042ef9f50aad2d0"> Registering HttpControllerContext <a href="#36c0398defb24c51b042ef9f50aad2d0" title="permalink">#</a> </h3> <p> The only thing left is registering the HttpControllerContext class itself: </p> <p> <pre style="margin: 0px">container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">HttpControllerContext</span>&gt;() &nbsp;&nbsp;&nbsp; .UsingFactoryMethod(k =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k.Resolve&lt;<span style="color: #2b91af">TaskCompletionSource</span>&lt;<span style="color: #2b91af">HttpControllerContext</span>&gt;&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Task.Result) &nbsp;&nbsp;&nbsp; .LifestylePerWebRequest());</pre> </p> <p> This defines that HttpControllerContext instances are going to be resolved the following way: each time an HttpControllerContext instance is requested, the container is going to look up a TaskCompletionSource&lt;HttpControllerContext&gt; and return the result from that task. </p> <p> The TaskCompletionSource&lt;HttpControllerContext&gt; instance is scoped per web request and previously captured (as you may recall) by the ContextCapturingControllerActivator class. </p> <p> That's all (sic!) there's to it :) </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e84df5b5dd9c42839563a43551017640"> <div class="comment-author">Jon <a href="#e84df5b5dd9c42839563a43551017640">#</a></div> <div class="comment-content">This is awesome. Love the fact the solution contains so many different techniques to accomplish something that should be easy!</div> <div class="comment-date">2012-04-19 22:34 UTC</div> </div> <div class="comment" id="871b197dd94f487ea11c91e02acf5bf6"> <div class="comment-author">Thomas <a href="#871b197dd94f487ea11c91e02acf5bf6">#</a></div> <div class="comment-content">Mark, will .LifestylePerWebRequest() work with self hosting feature of Web API ? As far as I know .LifestylePerWebRequest() relies on the underlying HttpContext which is not available when self hosting.I haven't take enough time to investigate further but if you have any hints I'll be glad to hear from you.</div> <div class="comment-date">2012-04-24 14:24 UTC</div> </div> <div class="comment" id="a627fd73d1c4404091ae9b0134555931"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a627fd73d1c4404091ae9b0134555931">#</a></div> <div class="comment-content">Thomas, I haven't investigated that yet. Sorry.</div> <div class="comment-date">2012-04-25 06:23 UTC</div> </div> <div class="comment" id="95a38d6aaa664d9ea8862900b72b1d5e"> <div class="comment-author">ChrisCa <a href="#95a38d6aaa664d9ea8862900b72b1d5e">#</a></div> <div class="comment-content">Hi<br> This is a great article and is exactly what I need as I'm tring to use your Hyprlinkr library. However, I've come up against this problem:<br> http://stackoverflow.com/questions/12977743/asp-web-api-ioc-resolve-httprequestmessage<br> <br> any ideas?<br> </div> <div class="comment-date">2012-10-19 15:44 UTC</div> </div> <div class="comment" id="2a72a6c9c9644a179f58f6fd07faad6e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2a72a6c9c9644a179f58f6fd07faad6e">#</a></div> <div class="comment-content">The current blog post describes how to use Castle Windsor with a preview of the Web API. Since there were breaking changes between the preview and the RTM version, the approach described here no longer works. Please refer to <a href="/2012/10/03/DependencyInjectioninASP.NETWebAPIwithCastleWindsor">this blog post</a> for a description of how to make DI work with Castle Windsor in Web API RTM.</div> <div class="comment-date">2012-10-19 16:57 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Injecting HttpControllerContext With the ASP.NET Web API https://blog.ploeh.dk/2012/04/17/InjectingHttpControllerContextWiththeASP.NETWebAPI 2012-04-17T15:17:05+00:00 Mark Seemann <div id="post"> <p> The <a href="http://www.asp.net/web-api">ASP.NET Web API</a> (beta) defines a class called HttpControllerContext. As the name implies, it provides a context for a Controller. This article describes how to inject an instance of this class into a Service. </p> <h3 id="d873f97dad9d46cc878f421c7695944e"> The Problem <a href="#d873f97dad9d46cc878f421c7695944e" title="permalink">#</a> </h3> <p> A Service may need an instance of the HttpControllerContext class. For an example, see the <a href="/2012/04/17/HyperlinkingWiththeASP.NETWebAPI">RouteLinker class in my previous post</a>. A Controller, on the other hand, may depend on such a Service: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> CatalogController(<span style="color: #2b91af">IResourceLinker</span> resourceLinker)</pre> </p> <p> How can a CatalogController instance be wired up with an instance of RouteLinker, which again requires an instance of HttpControllerContext? In contrast to the existing ASP.NET MVC API, there's no easy way to read the current context. There's no HttpControllerContext.Current method or any other easy way (that I have found) to refer to an HttpControllerContext as part of the <a href="/2011/07/28/CompositionRoot">Composition Root</a>. </p> <p> True: it's easily available as a property on a Controller, but at the time of composition, there's no Controller instance (yet). A Controller instance is exactly what the Composition Root is attempting to create. This sounds like a circular reference problem. Fortunately, it's not. </p> <h3 id="9858af47267c4a6ba88875465ce8e7f1"> The Solution <a href="#9858af47267c4a6ba88875465ce8e7f1" title="permalink">#</a> </h3> <p> For Poor Man's DI, the solution is relatively simple. As <a href="/2012/03/20/RobustDIWiththeASP.NETWebAPI">I've previously described</a>, by default the responsibility of creating Controller instances is handled by an instance of IHttpControllerActivator. This is, essentially, the Composition Root (at least for all Controllers). </p> <p> The Create method of that interface takes exactly the HttpControllerContext required by RouteLinker - or, put differently: the framework will supply an instance every time it invokes the Create method. Thus, a custom IHttpControllerActivator solves the problem: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">PoorMansCompositionRoot</span> : <span style="color: #2b91af">IHttpControllerActivator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">IHttpController</span> Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">HttpControllerContext</span> controllerContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Type</span> controllerType) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (controllerType == <span style="color: blue">typeof</span>(<span style="color: #2b91af">CatalogController</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> url = <span style="color: #2b91af">HttpContext</span>.Current.Request.Url; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> baseUri = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">UriBuilder</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url.Scheme, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url.Host, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url.Port).Uri; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">CatalogController</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">RouteLinker</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; baseUri, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; controllerContext)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green">// Handle other types here...</span> &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The <em>controllerContext</em> parameter is simply passed on to the RouteLinker constructor. </p> <p> The only thing left is to register the PoorMansCompositionRoot with the ASP.NET Web API. This can be done in Global.asax by using the GlobalConfiguration.Configuration.ServiceResolver.SetResolver method, as described in <a href="/2012/03/20/RobustDIWiththeASP.NETWebAPI">my previous post</a>. Just resolve IHttpControllerActivator to an instance of PoorMansCompositionRoot. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="06a146e1788947d78caa7f4a53f8c4cc"> <div class="comment-author">mick delaney <a href="#06a146e1788947d78caa7f4a53f8c4cc">#</a></div> <div class="comment-content">this is a nightmare really... has there been a discussion on the web api forums? <br> http://forums.asp.net/1246.aspx/1?Web+API<br> i think i might create one and reference this article directly if thats ok?<br> <br> i upvoted your uservoice issue and there seems to be another one more generally on DI for asp.net http://aspnet.uservoice.com/forums/41199-general-asp-net/suggestions/487734-put-ioc-front-and-centre<br> <br> doesn't seem like they listen to anyone though.... <br> <br> <br> </div> <div class="comment-date">2012-04-20 09:35 UTC</div> </div> <div class="comment" id="17cd4daed3514747aeafb2860cf77f50"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#17cd4daed3514747aeafb2860cf77f50">#</a></div> <div class="comment-content">Yes, please go ahead :)</div> <div class="comment-date">2012-04-20 09:58 UTC</div> </div> <div class="comment" id="e9d38999b2a343a58d7ef426671a3b20"> <div class="comment-author">mick delaney <a href="#e9d38999b2a343a58d7ef426671a3b20">#</a></div> <div class="comment-content">i've started it off from here. <br> <br> will add some more examples to the thread over the weekend<br> <br> http://forums.asp.net/p/1795175/4943052.aspx/1?p=True&amp;t=634705120844896186</div> <div class="comment-date">2012-04-20 13:49 UTC</div> </div> <div class="comment" id="b1cbd40616964f8a866c8791c3c0df32"> <div class="comment-author"><a href="http://byterot.blogspot.com">Ali</a> <a href="#b1cbd40616964f8a866c8791c3c0df32">#</a></div> <div class="comment-content">It is an interesting problem.<br> <br> I would also like to look at it differently. ControllerContext is a volatile object with its lifetime spanning only a single method. It is true that we typically limit the lifetime of a controller to a single method but it can be different.<br> <br> One major difference here is that ControllerContext is not a classic dependency since it is added AFTER the object creation by the framework and not provided by the DI frameqork. As such I could resort to an Initialise method on my RouteLinker object, making it responsibility of controller to provide such volatile information.<br> </div> <div class="comment-date">2012-04-23 09:09 UTC</div> </div> <div class="comment" id="ae3356969c0344c79dd3e8761085eb10"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ae3356969c0344c79dd3e8761085eb10">#</a></div> <div class="comment-content">Ali, that's not an approach I like. First of all because an Initialize method would introduce a <a href="/2011/05/24/DesignSmellTemporalCoupling">Temporal Coupling</a>, but also because it would mean that one would potentially need to pass the HttpControllerContext through a lot of intermediary layers in order to invoke that Initialize method on a very low-level dependency. That's API pollution.</div> <div class="comment-date">2012-04-23 09:24 UTC</div> </div> <div class="comment" id="4488dddaafcc4e3d894ea19e6296addb"> <div class="comment-author"><a href="http://byterot.blogspot.com">Ali</a> <a href="#4488dddaafcc4e3d894ea19e6296addb">#</a></div> <div class="comment-content">Well, I know that you are an authority in the subject. So I probably need to go away, read your links and then have a think about it. Thanks anyway, and keep up the good work.</div> <div class="comment-date">2012-04-23 16:32 UTC</div> </div> <div class="comment" id="0c1382e8324f4c0a9b79fcba48f07b02"> <div class="comment-author"><a href="/2012/04/17/InjectingHttpControllerContextWi">Dmitry</a> <a href="#0c1382e8324f4c0a9b79fcba48f07b02">#</a></div> <div class="comment-content">I realize that this article is more general, but specifically for IResourceLinker, would it make more sense to just pass HttpControllerContext as a second parameter from controller action. What would be the downside of this approach?</div> <div class="comment-date">2012-05-02 14:03 UTC</div> </div> <div class="comment" id="d9d3e9d4fb5b42bfb0efca84ee6ed34c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d9d3e9d4fb5b42bfb0efca84ee6ed34c">#</a></div> <div class="comment-content">Dmitry, that would mean that you'd need to pass the parameter through each and every method through all intermediary layers (see also my answer above). This would seriously pollute the API.</div> <div class="comment-date">2012-05-02 14:10 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Hyperlinking With the ASP.NET Web API https://blog.ploeh.dk/2012/04/17/HyperlinkingWiththeASP.NETWebAPI 2012-04-17T14:46:42+00:00 Mark Seemann <div id="post"> <p> When creating resources with the <a href="http://www.asp.net/web-api">ASP.NET Web API</a> (beta) it's important to be able to create correct hyperlinks (you know, <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">if it doesn't have hyperlinks, it's not REST</a>). These hyperlinks may link to other resources in the same API, so it's important to keep the links consistent. A client following such a link should hit the desired resource. </p> <p> This post describes an refactoring-safe approach to creating hyperlinks using the Web API RouteCollection and Expressions. </p> <h3 id="e8d1b3bc70b442378806dbea1b0a902c"> The Problem <a href="#e8d1b3bc70b442378806dbea1b0a902c" title="permalink">#</a> </h3> <p> Obviously hyperlinks can be hard-coded, but since incoming requests are matched based on the Web API's RouteCollection, there's a risk that hard-coded links become disconnected from the API's incoming routes. In other words, hard-coding links is probably not a good idea. </p> <p> For reference, the default route in the Web API looks like this: </p> <p> <pre style="margin: 0px">routes.MapHttpRoute( &nbsp;&nbsp;&nbsp; name: <span style="color: #a31515">"DefaultApi"</span>, &nbsp;&nbsp;&nbsp; routeTemplate: <span style="color: #a31515">"{controller}/{id}"</span>, &nbsp;&nbsp;&nbsp; defaults: <span style="color: blue">new</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; controller = <span style="color: #a31515">"Catalog"</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id = <span style="color: #2b91af">RouteParameter</span>.Optional &nbsp;&nbsp;&nbsp; } );</pre> </p> <p> A sample action fitting that route might look like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: #2b91af">Artist</span> Get(<span style="color: blue">string</span> id)</pre> </p> <p> where the Get method is defined by the ArtistController class. </p> <h3 id="1f8d43fecacb40d6a5e07b7878aa7e76"> Desired Outcome <a href="#1f8d43fecacb40d6a5e07b7878aa7e76" title="permalink">#</a> </h3> <p> In order to provide a refactoring-safe way to create links to e.g. the artist resource, the <a href="http://joseoncode.com/2011/03/18/wcf-web-api-strongly-typed-resource-linker/">strongly typed Resource Linker</a> approach outlined by <a href="http://joseoncode.com/">José F. Romaniello</a> can be adopted. The IResourceLinker interface looks like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IResourceLinker</span> { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Uri</span> GetUri&lt;T&gt;(<span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Action</span>&lt;T&gt;&gt; method); }</pre> </p> <p> This makes it possible to create links like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> artist = <span style="color: blue">new</span> <span style="color: #2b91af">Artist</span> { &nbsp;&nbsp;&nbsp; Name = artistName, &nbsp;&nbsp;&nbsp; Links = <span style="color: blue">new</span>[] &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">Link</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Href = <span style="color: blue">this</span>.resourceLinker.GetUri&lt;<span style="color: #2b91af">ArtistController</span>&gt;(r =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.Get(artistsId)).ToString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Rel = <span style="color: #a31515">"self"</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; }, &nbsp;&nbsp;&nbsp; <span style="color: green">// More crap goes here...</span> };</pre> </p> <p> In this example, the <em>resourceLinker</em> field is an injected instance of IResourceLinker. </p> <p> Since the input to the GetUri method is an Expression, it's being checked at compile time. It's refactoring-safe because a refactoring tool will be able to e.g. change the name of the method call in the Expression if the name of the method changes. </p> <h3 id="dbfcc19b06074b28953632f2df12331c"> Example Implementation <a href="#dbfcc19b06074b28953632f2df12331c" title="permalink">#</a> </h3> <p> It's possible to implement IResourceLinker over a Web API RouteCollection. Here's an example implementation: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">RouteLinker</span> : <span style="color: #2b91af">IResourceLinker</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">Uri</span> baseUri; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">HttpControllerContext</span> ctx; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> RouteLinker(<span style="color: #2b91af">Uri</span> baseUri, <span style="color: #2b91af">HttpControllerContext</span> ctx) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.baseUri = baseUri; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.ctx = ctx; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">Uri</span> GetUri&lt;T&gt;(<span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Action</span>&lt;T&gt;&gt; method) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (method == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"method"</span>); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> methodCallExp = method.Body <span style="color: blue">as</span> <span style="color: #2b91af">MethodCallExpression</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (methodCallExp == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentException</span>(<span style="color: #a31515">"The expression's body must be a MethodCallExpression. The code block supplied should invoke a method.\nExample: x =&gt; x.Foo()."</span>, <span style="color: #a31515">"method"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> routeValues = methodCallExp.Method.GetParameters() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ToDictionary(p =&gt; p.Name, p =&gt; GetValue(methodCallExp, p)); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> controllerName = methodCallExp.Method.ReflectedType.Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ToLowerInvariant().Replace(<span style="color: #a31515">"controller"</span>, <span style="color: #a31515">""</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; routeValues.Add(<span style="color: #a31515">"controller"</span>, controllerName); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> relativeUri = <span style="color: blue">this</span>.ctx.Url.Route(<span style="color: #a31515">"DefaultApi"</span>, routeValues); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">Uri</span>(<span style="color: blue">this</span>.baseUri, relativeUri); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">object</span> GetValue(<span style="color: #2b91af">MethodCallExpression</span> methodCallExp, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ParameterInfo</span> p) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> arg = methodCallExp.Arguments[p.Position]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> lambda = <span style="color: #2b91af">Expression</span>.Lambda(arg); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> lambda.Compile().DynamicInvoke().ToString(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This isn't much different from José F. Romaniello's example, apart from the fact that it creates a dictionary of route values and then uses the UrlHelper.Route method to create a relative URI.</p> <p> Please note that this is just an example implementation. For instance, the call to the Route method supplies the hard-coded string "DefaultApi" to indicate which route (from Global.asax) to use. I'll leave it as an exercise for the interested reader to provide a generalization of this implementation. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e4d448ff7d93408ebce060b340ccbf10"> <div class="comment-author"><a href="http://gorodinski.com/">Lev Gorodinski</a> <a href="#e4d448ff7d93408ebce060b340ccbf10">#</a></div> <div class="comment-content">This is very helpful! I'm currently exploring a solution that injects links into representations via filter based on some sort of state machine for the resource, though I might be leading myself down a DSL path.</div> <div class="comment-date">2012-04-17 23:07 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. IQueryable is Tight Coupling https://blog.ploeh.dk/2012/03/26/IQueryableTisTightCoupling 2012-03-26T13:53:31+00:00 Mark Seemann <div id="post"> <p> <em>From time to time I encounter people who attempt to express an API in terms of IQueryable&lt;T&gt;. That's almost always a bad idea. In this post, I'll explain why.</em> </p> <p> In short, the <a href="http://msdn.microsoft.com/en-us/library/bb351562.aspx">IQueryable&lt;T&gt;</a> interface is one of the best examples of a <a href="http://martinfowler.com/bliki/HeaderInterface.html">Header Interface</a> that .NET has to offer. It's almost impossible to fully implement it. </p> <blockquote> <p> Please note that this post is about the problematic aspects of designing an API around the IQueryable&lt;T&gt; interface. It's not an attack on the interface itself, which has its place in the BCL. It's also not an attack on all the wonderful LINQ methods available on <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx">IEnumerable&lt;T&gt;</a>. </p> </blockquote> <p> You can say that IQueryable&lt;T&gt; is one big <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> (LSP)violation just waiting to happen. In the next two section, I will apply <a href="http://en.wikipedia.org/wiki/Postel%27s_law">Postel's law</a> to explain why that is. </p> <h3 id="13e0446089ab4c9fb40af3ee0d1a662d"> Consuming IQueryable&lt;T&gt; <a href="#13e0446089ab4c9fb40af3ee0d1a662d" title="permalink">#</a> </h3> <p> The first part of Postel's law applied to API design states that an API should be <em>liberal in what it accepts</em>. In other words, we are talking about input, so an API that consumes IQueryable&lt;T&gt; would take this generalized shape: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">IFoo</span> SomeMethod(<span style="color: #2b91af">IQueryable</span>&lt;<span style="color: #2b91af">Bar</span>&gt; q);</pre> </p> <p> Is that a <em>liberal</em> requirement? It most certainly is not. Such an interface <em>demands</em> of any caller that they must be able to supply an implementation of IQueryable&lt;Bar&gt;. According to the LSP we must be able to supply <em>any</em> implementation without changing the correctness of the program. That goes for both the implementer of IQueryable&lt;Bar&gt; as well as the implementation of SomeMethod. </p> <p> At this point it's important to keep in mind the <em>purpose</em> of IQueryable&lt;T&gt;: it's <a href="http://msdn.microsoft.com/en-us/library/bb351562.aspx">intended for implementation by query providers</a>. In other words, this isn't just some sequence of Bar instances which can be filtered and projected; no, this is a query <em>expression</em> which is intended to be <em>translated</em> into a query somewhere else - most often some dialect of SQL. </p> <p> That's quite a demand to put on the caller. </p> <p> It's certainly a powerful interface (or so it would seem), but is it really necessary? Does SomeMethod really need to be able to perform <em>arbitrarily complex</em> queries against a data source? </p> <p> In one recent discussion, it turns out that all the developer really wanted to do was to be able to <a href="https://github.com/ploeh/cqrs-journey-code/commit/7b912136b470b75f418eadae605669c3489f53f9">select based on a handful of simple criteria</a>. In another case, the developer only wanted to do simple paging. </p> <p> Such requirements could be modeled much simpler without making huge demands on the caller. In both cases, we could provide specialized <a href="http://martinfowler.com/eaaCatalog/queryObject.html">Query Objects</a> instead, or perhaps even simpler just a set of specialized queries: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">IFoo</span> FindById(<span style="color: blue">int</span> fooId); &nbsp; <span style="color: #2b91af">IFoo</span> FindByCorrelationId(<span style="color: blue">int</span> correlationId);</pre> </p> <p> Or, in the case of paging: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">IFoo</span>&gt; GetFoos(<span style="color: blue">int</span> page);</pre> </p> <p> This is certainly much more <em>liberal</em> in that it requires the caller to supply only the required information in order to implement the methods. Designing APIs in terms of <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a> instead of Header Interfaces makes the APIs much more flexible. This will enable you to respond to change. </p> <h3 id="f151149a2d8d473cbdd52eb31ac4fa71"> Exposing IQueryable&lt;T&gt; <a href="#f151149a2d8d473cbdd52eb31ac4fa71" title="permalink">#</a> </h3> <p> The other part of Postel's law states that an API should be <em>conservative in what it sends</em>. In other words, a method must guarantee that the data it returns conforms rigorously to the contract between caller and implementer. </p> <p> A method returning IQueryable&lt;T&gt; would take this generalized shape: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">IQueryable</span>&lt;<span style="color: #2b91af">Bar</span>&gt; GetBars();</pre> </p> <p> When designing APIs, a huge part of the contract is defined by the interface (or base class). Thus, the return type of a method specifies a conservative guarantee about the returned data. In the case of returning IQueryable&lt;Bar&gt; the method thus <em>guarantees</em> that it will return a complete implementation of IQueryable&lt;Bar&gt;. </p> <p> Is that conservative? </p> <p> Once again invoking the LSP, a consumer must be able to do <em>anything</em> allowed by IQueryable&lt;Bar&gt; without changing the correctness of the program. </p> <p> That's a big honking promise to make. </p> <p> Who can keep that promise? </p> <h3 id="053566a096be4268afaa042b34928a2c"> Current Implementations <a href="#053566a096be4268afaa042b34928a2c" title="permalink">#</a> </h3> <p> Implementing IQueryable&lt;T&gt; is a huge undertaking. If you don't believe me, just take a look at the official <a href="http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx">Building an IQueryable provider</a> series of blog posts. Even so, the interface is so flexible and expressive that with a single exception, it's always possible to write a query that a given provider can't translate. </p> <p> Have you ever worked with LINQ to Entities or another ORM and received a NotSupportedException? <a href="https://www.google.dk/search?q=linq+notsupportedexception">Lots of people have</a>. In fact, with a single exception, it's my firm belief that all existing implementations violate the LSP (in fact, I challenge my readers to refer me to a real, publicly available implementation of IQueryable&lt;T&gt; that can accept <em>any</em> expression I throw at it, and I'll ship a free copy of <a href="http://amzn.to/12p90MG">my book</a> to the first reader to do so). </p> <p> Furthermore, the subset of features that each implementation supports varies from query provider to query provider. An expression that can be translated by the Entity framework may not work with Microsoft's OData query provider. </p> <p> The only implementation that fully implements IQueryable&lt;T&gt; is the <a href="http://msdn.microsoft.com/en-us/library/cc190116.aspx">in-memory implementation</a> (and referring to this one does not earn you a free book). Ironically, this implementation can be considered a <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a> implementation and goes against the whole purpose of the IQueryable&lt;T&gt; interface exactly because it <em>doesn't</em> translate the expression to another language. </p> <h3 id="5e6961fc0c6c4b31a2701073559e9899"> Why This Matters <a href="#5e6961fc0c6c4b31a2701073559e9899" title="permalink">#</a> </h3> <p> You may think this is all a theoretical exercise, but it actually does matter. When writing <a href="http://www.amazon.com/gp/product/0132350882/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code</a>, it's important to design an API in such a way that it's clear what it does. </p> <p> An interface like this makes false guarantees: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IRepository</span> { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IQueryable</span>&lt;T&gt; Query&lt;T&gt;(); }</pre> </p> <p> According to the LSP and Postel's law, it would seem to guarantee that you can write <em>any</em> query expression (no matter how complex) against the returned instance, and it would always work. </p> <p> In practice, this is never going to happen. </p> <p> Programmers who define such interfaces invariably have a specific ORM in mind, and they implicitly tend to stay within the bounds they know are safe for that specific ORM. This is a leaky abstraction. </p> <p> If you have a specific ORM in mind, then be explicit about it. Don't hide it behind an interface. It creates the illusion that you can replace one implementation with another. In practice, that's impossible. Imagine attempting to provide an implementation over an Event Store. </p> <p> The cake is a lie. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e4688fbb6bb54495957e86733bd208ff"> <div class="comment-author"><a href="http://antix.co.uk">Anthony Johnston</a> <a href="#e4688fbb6bb54495957e86733bd208ff">#</a></div> <div class="comment-content">The differences between the different providers is even more subtle than NotSupportedException's<br> If you have the following query you get different results with the EF LINQ provider and the LINQ to Objects provider<br> <br> var r = from x in context.Xs select new { x.Y.Z }<br> <br> dependant on the type of join between the sets or if Y is null <br> <br> makes in-memory testing difficult<br> </div> <div class="comment-date">2012-03-26 17:24 UTC</div> </div> <div class="comment" id="34cf6c9fba8c4bc7a2be8e46166749de"> <div class="comment-author">Sergio Romero <a href="#34cf6c9fba8c4bc7a2be8e46166749de">#</a></div> <div class="comment-content">I will look for the implementation of IQueryable&lt;T&gt; that does not violate the principle but, what if I already own your book? :)<br> <br> P.S. Kudos by the way. I love every single piece of it.</div> <div class="comment-date">2012-03-26 20:12 UTC</div> </div> <div class="comment" id="84f7e9e4ea6f4d7eaced4729da20c076"> <div class="comment-author">Vytautas <a href="#84f7e9e4ea6f4d7eaced4729da20c076">#</a></div> <div class="comment-content">While swapping implementations from a specific ORM to an event store is a major undertaking, it is not very difficult to swap one persistence mechanism to another when you query persistent view models (which are usually found in the same type of applications where you can find an event store). They usually have automatic properties and (next to) no relations so queries on them are relatively simple to not throw many NotSupportedExceptions.<br> <br> Also, having a generic repository as an intermediate step between an ORM and a specific repository lets you unit test specific repositories and/or use them with multiple persistence mechanisms (which is sometimes desirable). Despite the leaks, this &quot;pattern&quot; does have some uses in my book when used VERY carefully (although it is misused in 99 cases out of 100 when you see it).</div> <div class="comment-date">2012-03-26 20:43 UTC</div> </div> <div class="comment" id="ae91630d64294bc09d174324022283e4"> <div class="comment-author"><a href="http://www.joncurtis.co.uk">Jon</a> <a href="#ae91630d64294bc09d174324022283e4">#</a></div> <div class="comment-content">With your suggested approach, don't you just end up with a large interface of different queries? If not, how do you decide to split them up? Wouldn't it be better to have a more general purpose IQuery &lt; T &gt;.Execute()?</div> <div class="comment-date">2012-03-26 21:17 UTC</div> </div> <div class="comment" id="a75aacc985fe4ec1959c812ab9437407"> <div class="comment-author"><a href="http://dev-frame.com">Amir</a> <a href="#a75aacc985fe4ec1959c812ab9437407">#</a></div> <div class="comment-content">I've created an IRepository interface which doesn't consume or expose any IQueryable object. But I've created a separate library (abstraction) on the top of this abstract IRepository and name it &quot;EntityRepository&quot; which is abstract, implements the IRepository and is dedicated to EF. EntityRepository and its derived types may consume or expose IQueryable.<br> Everybody who wants to implement the repositories using EF would use EntityRepository and everybody who wants to use other ORMs can create his own derived abstraction from IRepository.<br> <br> But the consumer codes in my library just use the IRepository inteface (Ex. A CRUD ViewModel base class).<br> <br> It it correct?</div> <div class="comment-date">2012-03-27 19:42 UTC</div> </div> <div class="comment" id="15ee85f171e74cc5bd541a9c59e8a850"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#15ee85f171e74cc5bd541a9c59e8a850">#</a></div> <div class="comment-content">Amir, that sounds correct. IQueryable is a useful tool, but it can be somewhat misleading as part of an API (interfaces or base classes). On concrete classes it's less misleading.</div> <div class="comment-date">2012-03-28 05:03 UTC</div> </div> <div class="comment" id="9abef321a485402b882940a7ac4b5732"> <div class="comment-author">Matt Warren <a href="#9abef321a485402b882940a7ac4b5732">#</a></div> <div class="comment-content">You should take a look at some of Bart De Smet's stuff, he talks about IQueryable and it's problems.<br> <br> See http://bartdesmet.net/blogs/bart/archive/2008/08/15/the-most-funny-interface-of-the-year-iqueryable-lt-t-gt.aspx and also take a look at the section 9min 30 secs into this talk http://channel9.msdn.com/Events/PDC/PDC10/FT10</div> <div class="comment-date">2012-03-28 10:47 UTC</div> </div> <div class="comment" id="c50defe8c0d447debbe8bff5fcb3de5d"> <div class="comment-author">Daniel Cazzulino <a href="#c50defe8c0d447debbe8bff5fcb3de5d">#</a></div> <div class="comment-content">-- IFoo SomeMethod(IQueryable{Bar} q);<br> <br> Who on Earth defines an API that *takes* an IQueryable? Nobody I know, and<br> I've never had the need to. So the first half of the post is kinda<br> irrelevant ;)<br> <br> -- Implementing IQueryable{T} is a huge undertaking.<br> <br> That's why NOBODY has to. The ORM you use will (THEY will take the huge<br> undertaking and they have, go look for EF and NHibernate and even RavenDB<br> who do provide such an API), as well as Linq to Objects for your testing<br> needs.<br> <br> So, the IRepository.Query{T} is doing *exactly* what its definition<br> promises: &quot;Mediates between the domain and data mapping layers using a<br> collection-like interface for accessing domain objects.&quot; (<br> http://martinfowler.com/eaaCatalog/repository.html)<br> <br> In the ORM-implemented case, it goes against the data mapping layer, in the<br> Linq to Objects case, it does directly over the collection of objects.<br> <br> It's the PERFECT abstraction to make code that goes against a DB/repository<br> unit-testable. (not that it frees you from having full integration<br> end-to-end tests that go against the *real* repository to make sure you're<br> not using any unsupported constructs against the in-memory version).<br> <br> IQueryable{T} got rid of an entire slew of useless interfaces to abstract<br> the real repository. We should wholeheartedly embrace it and move forward,<br> instead of longing for the days when we had to do all that manually ;)</div> <div class="comment-date">2012-03-28 14:19 UTC</div> </div> <div class="comment" id="0796c5195bec4c1bb920d0ed55ba2fb6"> <div class="comment-author">Daniel Cazzulino <a href="#0796c5195bec4c1bb920d0ed55ba2fb6">#</a></div> <div class="comment-content">-- Imagine attempting to provide an implementation over an Event Store.<br> <br> That's the thing, if this interface is used in the read side of a CQRS<br> system, there's no event store there. Views need flexible queryability. No?</div> <div class="comment-date">2012-03-28 14:20 UTC</div> </div> <div class="comment" id="7bfcf621bc4644e4949bb29455fdc981"> <div class="comment-author"><a href="http://www.liquidelectron.com">Josh Elster</a> <a href="#7bfcf621bc4644e4949bb29455fdc981">#</a></div> <div class="comment-content">I'm with kzu here -- I'm not really buying into your claim that IQueryable is a bad (leaky, etc) abstraction using the logic you provide. A method returning an IQueryable is *only* providing a guarantee that it describes a query over a set of objects. Nothing more. It gives no guarantees that the query can be executed, only what the query looks like. <br> <br> Saying this:<br> <br> public IQueryable{Customer} GetActiveCustomers() {<br> return CustomersDb.Where(x =&gt; x.IsActive).ToList().AsQueryable();<br> }<br> <br> tells consumers that the return value of the method is a representation of how the results will be materialized when the query is executed. Saying this, OTOH:<br> <br> public IEnumerable{Customer} GetActiveCustomers() {<br> return CustomersDb.Where(x =&gt; x.IsActive).ToList();<br> }<br> <br> explicitly tells consumers that you are returning the *results* of a query, and it's none of the consuming code's business to know how it got there. <br> <br> Materialization of those query results is what you seemed to be focused on, and that's not IQueryable's task. That's the task of the specific LINQ provider. IQueryable's job is only to *describe* queries, not actually execute them. </div> <div class="comment-date">2012-03-28 15:21 UTC</div> </div> <div class="comment" id="64daf95708824bfb85b252acbd50bf23"> <div class="comment-author">Ethan J. Brown <a href="#64daf95708824bfb85b252acbd50bf23">#</a></div> <div class="comment-content">Completely agree with Mark here - IQueryable in, for instance, a repository interface may seem like a natural fit, but in fact it's a very very bad idea.<br> <br> A repository interface should be explicit about the operations it provides over the set of data, and should not open the door to arbitrary querying that is not part of the applications overall design / architecture. YAGNI<br> <br> I know that this may fly in the face of those who are used to arbitrary late-bound SQL style query flexibility and code-gen'd ORMs, but this type of setup is not one that's conducive to performance or scaling in the long term. ORMs may be good to get something up and running without a lot of effort (or thought into how the data is used), but they rapidly show their cracks / leaks over time. This is why you see sites like StackOverflow as they reach scale adopt MicroORMs that are closer to the metal like Dapper instead of using EF, L2S or NHibernate. (Other great Micro ORMs are Massive, OrmLite, and PetaPoco to name just a few.)<br> <br> <br> Is it work to be explicit in your contracts around data access? Absolutely... but this is a better long-term design decision when you factor in testability and performance goals. How do you test the perf characteristics of an API that doesn't have well-defined operations? Well... you can't really.<br> <br> Also -- consider stores that are *not* SQL based as Mark alludes to. We're using Riak, where querying capabilities are limited in scope and there are performance trade-offs to certain types of operations on secondary indices. We accept this tradeoff b/c the operational characteristics of Riak are unparalleled and it has great latency characteristics when retrieving data. We need to be explicit about how we allow data to be retrieved in our data access layer because some things are a no-go in our underlying store (for instance, listing keys is extremely expensive at the moment in Riak).<br> <br> Bind yourself to IQueryable and you've handcuffed yourself. IQueryable is simply not a fit in a lot of cases, and in others it's total overkill... data access is not one size fits all. Embrace the polyglot!</div> <div class="comment-date">2012-03-29 12:09 UTC</div> </div> <div class="comment" id="11edd8b9d96143cbb61723055b7580fc"> <div class="comment-author">Daniel <a href="#11edd8b9d96143cbb61723055b7580fc">#</a></div> <div class="comment-content">Thanks for your blog post. One big problem with APIs that are based on IQueryable is testability, see this question for example: http://stackoverflow.com/questions/9921647/unit-testing-with-queries-defined-in-extension-methods.<br> <br> However, I don't yet see how to design an easy to use and read API with query objects. Maybe you could provide a repository implementation that works without IQueryable but is still readable and easy to use?</div> <div class="comment-date">2012-03-29 12:35 UTC</div> </div> <div class="comment" id="6a212a4f606e46d2aecab8394c8b585a"> <div class="comment-author"><a href="http://www.nikosbaxevanis.com/bonus-bits/">Nikos Baxevanis</a> <a href="#6a212a4f606e46d2aecab8394c8b585a">#</a></div> <div class="comment-content">Daniel, according to your blog post http://blogs.clariusconsulting.net/kzu/how-to-design-a-unit-testable-domain-model-with-entity-framework-code-first/ you seem to do quite a lot of work to make the code testable, even re-implementing the Include method. Claiming that IQueryable is the &quot;perfect&quot; abstraction here doesn't convince me.</div> <div class="comment-date">2012-03-29 12:39 UTC</div> </div> <div class="comment" id="83b0a245ef914137a875567033347b0a"> <div class="comment-author">Daniel <a href="#83b0a245ef914137a875567033347b0a">#</a></div> <div class="comment-content">What do you think about the API outlined in this post: http://stackoverflow.com/a/9943907/572644<br> </div> <div class="comment-date">2012-03-30 13:29 UTC</div> </div> <div class="comment" id="f5b698d8766749548300112b9a185801"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f5b698d8766749548300112b9a185801">#</a></div> <div class="comment-content">Looks good, Daniel. I'd love to hear your experience with this once you've tried it on for some time :)</div> <div class="comment-date">2012-03-30 14:15 UTC</div> </div> <div class="comment" id="13734aa2eab94db7bef24767be9491bf"> <div class="comment-author"><a href="http://josh.mouch.name">Joshua Mouch</a> <a href="#13734aa2eab94db7bef24767be9491bf">#</a></div> <div class="comment-content">I agree that it can be annoying when you try to materialize a IQueryable to find that something in the query is not supported by the referenced QueryProvider.<br> <br> However, exposing IQueryable in an API is so powerful that it's worth this minor annoyance.</div> <div class="comment-date">2012-03-30 19:35 UTC</div> </div> <div class="comment" id="8ea347912bc0450689a0fd8a21359fc9"> <div class="comment-author">Steven Pena <a href="#8ea347912bc0450689a0fd8a21359fc9">#</a></div> <div class="comment-content">Excellent points - couldn't agree more. Even when a method of IQueryable has been implemented, you can't even guarantee the same behavior. For example - look at &quot;Contains&quot;. The EF Linq provider uses %, which will match any case, while the LinqToObjects provider will match by case. I tend to use IQueryable in my DAL and surface up a more meaningful and controlled API through my Repositories. This prevents that leaky abstraction and allows me to control the behavior of that persistence layer (caching, eager loading of aggregates...)</div> <div class="comment-date">2012-03-31 12:46 UTC</div> </div> <div class="comment" id="6d4d043146684c92924248046a725fdf"> <div class="comment-author">Nathan Brown <a href="#6d4d043146684c92924248046a725fdf">#</a></div> <div class="comment-content">What does this say for things like ASP.Net Web API? It is asking you to expose your data through an IQueryable. I tried to use the OData stuff that was using IQueryable and could never get it to fully work with NHibernate because of specific unsupported query structures.<br> <br> I like the idea of the OData or Web API queries, but would rather they just expose the query parameters and let us build adapters from the query to the datastore as relevant.</div> <div class="comment-date">2012-03-31 18:24 UTC</div> </div> <div class="comment" id="62f54610507d46a281d4adae411b2b77"> <div class="comment-author"><a href="http://www.servicestack.net/mythz_blog/">Demis Bellot</a> <a href="#62f54610507d46a281d4adae411b2b77">#</a></div> <div class="comment-content">Completely agree with this. IQueryable/OData is an anti-pattern in the making - especially for external Web APIs, which is why it will never be an out-of-the-box option in ServiceStack: http://stackoverflow.com/questions/9577938/odata-with-servicestack<br> <br> The whole idea of web service interfaces is to expose a technology-agnostic interoperable API to the outside world.<br> <br> Exposing an IQueryable/OData endpoint effectively couples your services to using OData indefinitely as you wont be able to feasibly determine what 'query space' existing clients are binded to, i.e. what existing queries/tables/views/properties you need to freeze/support indefinitely. This is an issue when exposing any implementation at the surface area of your API, as it limits your ability to add your own custom logic on it, e.g. Authorization, Caching, Monitoring, Rate-Limiting, etc. And because OData is really slow, you'll hit performance/scaling problems with it early. The lack of control over the endpoint, means you're effectively heading for a rewrite: https://coldie.net/?tag=servicestack<br> <br> Lets see how feasible is would be to move off an oData provider implementation by looking at an existing query from Netflix's OData api:<br> <br> http://odata.netflix.com/Catalog/Titles?$filter=Type%20eq%20'Movie'%20and%20(Rating%20eq%20'G'%20or%20Rating%20eq%20'PG-13')<br> <br> This service is effectively now coupled to a Table/View called 'Titles' with a column called 'Type'.<br> <br> And how it would be naturally written if you weren't using OData:<br> <br> http://api.netflix.com/movies?ratings=G,PG-13<br> <br> Now if for whatever reason you need to replace the implementation of the existing service (e.g. a better technology platform has emerged, it runs too slow and you need to move this dataset over to a NoSQL/Full-TextIndexing-backed sln) how much effort would it take to replace the OData impl (which is effectively binded to an RDBMS schema and OData binary impl) to the more intuitive impl-agnostic query below? It's not impossible, but as it's prohibitively difficult to implement an OData API for a new technology, that rewriting + breaking existing clients would tend to be the preferred option. <br> <br> Letting internal implementations dictate the external facing url structure is a sure way to break existing clients when things need to change. This is why you should expose your services behind Cool URIs, i.e. logical permanent urls (that are unimpeded by implementation) that do not change, as you generally don't want to limit the technology choices of your services.<br> <br> It might be a good option if you want to deliver adhoc 'exploratory services' on it, but it's not something I'd ever want external clients binded to in a production system. And if I'm only going to limit its use to my local intranet what advantages does it have over just giving out a read-only connection string? which will allow others use with their favourite Sql Explorer, Reporting or any other tools that speaks SQL.</div> <div class="comment-date">2012-07-31 05:42 UTC</div> </div> <div class="comment" id="cf72d15ced014373bbc5f56f58a2ea55"> <div class="comment-author"><a href="http://clariusconsulting.net/kzu">Daniel Cazzulino</a> <a href="#cf72d15ced014373bbc5f56f58a2ea55">#</a></div> <div class="comment-content">@Nikos &quot;re-implementing the Include method&quot;: you mean that one-liner??<br> <br> The testable interface abstraction allows for one-liners in the ORM-bound implementation, as well as the testable fake. Doesn't get any simpler than that.<br> <br> That hardly seems like complicated stuff to me.</div> <div class="comment-date">2012-08-31 15:44 UTC</div> </div> <div class="comment" id="0d96cafb22e64e508275cf6c1479496e"> <div class="comment-author"><a href="http://clariusconsulting.net/kzu">Daniel Cazzulino</a> <a href="#0d96cafb22e64e508275cf6c1479496e">#</a></div> <div class="comment-content">@Demis +1. Exposing OData is a different thing altogether. Not totally convinced with that either myself.</div> <div class="comment-date">2012-08-31 15:46 UTC</div> </div> <div class="comment" id="051aaab4d69f4b28ad36e869a414fd0e"> <div class="comment-author">Ron <a href="#051aaab4d69f4b28ad36e869a414fd0e">#</a></div> <div class="comment-content">@Demis, couldn't you use a service pattern to address those issues?</div> <div class="comment-date">2013-02-21 16:49 UTC</div> </div> <div class="comment" id="1e3ce9a1ae9d4aa38d7d35d549a095cb"> <div class="comment-author"><a href="http://blog.majcica.com/">Mario Majcica</a> <a href="#1e3ce9a1ae9d4aa38d7d35d549a095cb">#</a></div> <div class="comment-content">Although this post is "oldish" I do think that is still not answered. I was <a href="http://blogs.msdn.com/b/mrtechnocal/archive/2014/03/16/asynchronous-repositories.aspx"> checking</a>a quite nice idea of UoW and Repository pattern and EF implementation proposal and got stuck again at IQueryable question. Recently I read a lot about this question and could not find anything that suits me perfectly. Is your opinion changed after more than two years time? What about passing a Expression&lt;Func&lt;T, bool&gt;&gt; predicate to our Find method? Thanks</div> <div class="comment-date">2014-08-06 17:46 UTC</div> </div> <div class="comment" id="98b12b729bbd4aaeb347ffe1b58ab352"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#98b12b729bbd4aaeb347ffe1b58ab352">#</a></div> <div class="comment-content"> <p> Mario, thank you for writing. It occasionally happens that I change my mind, as I learn new skills and gain more experience, but in this case, I haven't changed my mind. I indirectly <em>use</em> IQueryable&lt;T&gt; when I occasionally have to query a relational database with the Entity Framework or LINQ to SQL (which happens <em>extremely</em> rarely these days), but I never design my own interfaces around IQueryable&lt;T&gt;; my reasons for introducing an interface is normally to reduce coupling, but introducing any interface that exposes or depends on IQueryable&lt;T&gt; doesn't reduce coupling. </p> <p> In <a href="http://amzn.to/19W4JHk">Agile Principles, Patterns, and Practices in C#</a>, Robert C. Martin explains in chapter 11 that "clients [...] own the abstract interfaces" - in other words: the client, which invokes methods on the interface, should define the methods that the interface should expose, based on what it <em>needs</em> - not based on what an arbitrary implementation might be able to implement. This is the design philosophy I always follow. A client shouldn't have to need to be able to perform arbitrary queries against a data access layer. If it does, it's such a Leaky Abstraction anyway that the interface isn't going to help; in such cases, just let the client talk directly to the ORM or the ADO.NET objects. That's much easier to understand. </p> <p> The same argument also goes against designing interfaces around Expression&lt;Func&lt;T, bool&gt;&gt;. It may seem flexible, but the fact is that such an expression can be arbitrarily complex, so in practice, it's impossible to guarantee that you can translate <em>every</em> expression to all SQL dialects; and what about OData? Or queries against MongoDB? </p> <p> Still, I rarely use ORMs at all. Instead, I increasingly rely on simple abstractions like <a href="/2014/07/23/drain">Drain</a> when designing my systems. </p> </div> <div class="comment-date">2014-08-09 12:22 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Robust DI With the ASP.NET Web API https://blog.ploeh.dk/2012/03/20/RobustDIWiththeASP.NETWebAPI 2012-03-20T16:02:51+00:00 Mark Seemann <div id="post"> <blockquote> <strong>Note 2014-04-03 19:46 UTC:</strong> This post describes how to address various Dependency Injection issues with a Community Technical Preview (<strong>CTP</strong>) of ASP.NET Web API 1. Unless you're still using that 2012 CTP, it's no longer relevant. Instead, refer to <a href="/2012/09/28/DependencyInjectionandLifetimeManagementwithASP.NETWebAPI">my article about Dependency Injection with the final version of Web API 1</a> (which is also relevant for Web API 2). </blockquote> <p> Like the WCF Web API, the new <a href="http://www.asp.net/web-api">ASP.NET Web API</a> supports Dependency Injection (DI), but the approach is different and the resulting code you'll have to write is likely to be more complex. This post describes how to enable robust DI with the new Web API. Since this is based on the beta release, I hope that it will become easier in the final release. </p> <p> At first glance, enabling DI on an ASP.NET Web API <a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver">looks seductively simple</a>. As always, though, the devil is in the details. <a href="http://www.nikosbaxevanis.com/bonus-bits/">Nikos Baxevanis</a> has already provided a <a href="http://www.nikosbaxevanis.com/bonus-bits/2012/03/using-the-web-api-dependency-resolver-with-castle-windsor.html">more thorough description</a>, but it's even more tricky than that. </p> <h3 id="09913d085a9845ed86a8c33693d7b7b1"> Protocol <a href="#09913d085a9845ed86a8c33693d7b7b1" title="permalink">#</a> </h3> <p> To enable DI, all you have to do is to call the SetResolver method, right? It even has an overload that enables you to supply two code blocks instead of implementing an interface (although you can certainly also implement IDependencyResolver). Could it be any easier than that? </p> <p> Yes, it most certainly could. </p> <p> Imagine that you'd like to hook up your DI Container of choice. As a first attempt, you try something like this: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">GlobalConfiguration</span>.Configuration.ServiceResolver.SetResolver( &nbsp;&nbsp;&nbsp; t =&gt; <span style="color: blue">this</span>.container.Resolve(t), &nbsp;&nbsp;&nbsp; t =&gt; <span style="color: blue">this</span>.container.ResolveAll(t).Cast&lt;<span style="color: blue">object</span>&gt;());</pre> </p> <p> This compiles. Does it work? Yes, but in a rather scary manner. Although it satisfies the interface, it doesn't satisfy the <em>protocol</em> ("an interface describes whether two components will <em>fit</em> together, while a protocol describes whether they will <em>work</em> together." (<a href="http://www.amazon.com/gp/product/0321503627/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321503627">GOOS</a>, p. 58)). </p> <p> The protocol, in this case, is that if you (or rather the container) can't resolve the type, you should return null. What's even worse is that <em>if</em> your code throws an exception (<em>any</em> exception, apparently), DependencyResolver will suppress it. In case you didn't know, this is strongly frowned upon in the <a href="http://msdn.microsoft.com/en-us/library/ms229005.aspx">.NET Framework Design Guidelines</a>. </p> <p> Even so, the official introduction article instead chooses to play along with the protocol and explicitly handle any exceptions. Something along the lines of this ugly code: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">GlobalConfiguration</span>.Configuration.ServiceResolver.SetResolver( &nbsp;&nbsp;&nbsp; t =&gt; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">try</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.container.Resolve(t); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">catch</span> (<span style="color: #2b91af">ComponentNotFoundException</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; }, &nbsp;&nbsp;&nbsp; t =&gt; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">try</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.container.ResolveAll(t).Cast&lt;<span style="color: blue">object</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">catch</span> (<span style="color: #2b91af">ComponentNotFoundException</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: blue">object</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } );</pre> </p> <p> Notice how try/catch is used for control flow - another <a href="http://msdn.microsoft.com/en-us/library/ms229030.aspx">major no no in the .NET Framework Design Guidelines</a>. </p> <p> At least with a good DI Container, we can do something like this instead: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">GlobalConfiguration</span>.Configuration.ServiceResolver.SetResolver( &nbsp;&nbsp;&nbsp; t =&gt; <span style="color: blue">this</span>.container.Kernel.HasComponent(t) ? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.container.Resolve(t) : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">null</span>, &nbsp;&nbsp;&nbsp; t =&gt; <span style="color: blue">this</span>.container.ResolveAll(t).Cast&lt;<span style="color: blue">object</span>&gt;());</pre> </p> <p> Still, first impressions don't exactly inspire trust in the implementation... </p> <h3 id="2cf8da7831e8494e88ac76bc98e7dffb"> API Design Issues <a href="#2cf8da7831e8494e88ac76bc98e7dffb" title="permalink">#</a> </h3> <p> Next, I would like to direct your attention to the DependencyResolver API. At its core, it looks like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IDependencyResolver</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">object</span> GetService(<span style="color: #2b91af">Type</span> serviceType); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">object</span>&gt; GetServices(<span style="color: #2b91af">Type</span> serviceType); }</pre> </p> <p> It can create objects, but what about decommissioning? What if, deep in a dependency graph, a Controller contains an IDisposable object? This is not a particularly exotic scenario - it might be an instance of an Entity Framework <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx">ObjectContext</a>. While an ApiController itself implements IDisposable, it may not know that it contains an injected object graph with one or more IDisposable leaf nodes. </p> <blockquote> <p> It's a fundamental rule of DI that you must <a href="/2010/09/29/TheRegisterResolveReleasepattern">Release what you Resolve</a>. That's not possible with the DependencyResolver API. The result may be memory leaks. </p> </blockquote> <p> Fortunately, it turns out that there's a fix for this (at least for Controllers). Unfortunately, this workaround leverages another design problem with DependencyResolver. </p> <h3 id="d0c9e3c99cbb4d7e8c75fcbb51c5fa36"> Mixed Responsibilities <a href="#d0c9e3c99cbb4d7e8c75fcbb51c5fa36" title="permalink">#</a> </h3> <p> It turns out that when you wire a custom resolver up with the SetResolver method, the ASP.NET Web API will query your custom resolver (such as a DI Container) for not only your application classes, but also for its own infrastructure components. That surprised me a bit because of the mixed responsibility, but at least this is a useful hook. </p> <p> One of the first types the framework will ask for is an instance of IHttpControllerFactory, which looks like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IHttpControllerFactory</span> { &nbsp;&nbsp;&nbsp; IHttpController CreateController(HttpControllerContext controllerContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> controllerName); &nbsp;&nbsp;&nbsp; <span style="color: blue">void</span> ReleaseController(IHttpController controller); }</pre> </p> <p> Fortunately, this interface has a Release hook, so at least it's possible to release Controller instances, which is most important because there will be a lot of them (one per HTTP request). </p> <h3 id="d1bc7b40498e490698670e6606053c52"> Discoverability Issues <a href="#d1bc7b40498e490698670e6606053c52" title="permalink">#</a> </h3> <p> The IHttpControllerFactory looks a lot like the well-known ASP.NET MVC <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.icontrollerfactory%28v=vs.98%29.aspx">IControllerFactory</a> interface, but there are subtle differences. In ASP.NET MVC, there's a <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultcontrollerfactory%28v=vs.98%29.aspx">DefaultControllerFactory</a> with appropriate virtual methods one can overwrite (it follows the <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method</a> pattern). </p> <p> There's also a DefaultControllerFactory in the Web API, but unfortunately no Template Methods to override. While I <em>could</em> write an algorithm that maps from the <em>controllerName</em> parameter to a type which can be passed to a DI Container, I'd rather prefer to be able to reuse the implementation which the DefaultControllerFactory contains. </p> <p> In ASP.NET MVC, this is possible by overriding the <a href="http://msdn.microsoft.com/en-us/library/ee264052%28v=vs.98%29.aspx">GetControllerInstance</a> method, but it turns out that the Web API (beta) does this slightly differently. It favors composition over inheritance (which is actually a good thing, so kudos for that), so after mapping <em>controllerName</em> to a Type instance, it invokes an instance of the IHttpControllerActivator interface (did I hear anyone say "FactoryFactory?"). Very loosely coupled (good), but not very discoverable (not so good). It would have been more discoverable if DefaultControllerFactory had used Constructor Injection to get its dependency, rather than relying on the <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator</a> which DependencyResolver really is. </p> <p> However, this is only an issue if you need to hook into the Controller creation process, e.g. in order to capture the HttpControllerContext for further use. In normal scenarios, despite what Nikos Baxevanis describes in his blog post, you don't <em>have</em> to override or implement IHttpControllerFactory.CreateController. The DependencyResolver infrastructure will automatically invoke your GetService implementation (or the corresponding code block) whenever a Controller instance is required. </p> <h3 id="29adc9c1ebca4358a756f9850a7fd2fc"> Releasing Controllers <a href="#29adc9c1ebca4358a756f9850a7fd2fc" title="permalink">#</a> </h3> <p> The easiest way to make sure that all Controller instances are being properly released is to derive a class from DefaultControllerFactory and override the ReleaseController method: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ReleasingControllerFactory</span> : <span style="color: #2b91af">DefaultHttpControllerFactory</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Action</span>&lt;<span style="color: blue">object</span>&gt; release; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> ReleasingControllerFactory(<span style="color: #2b91af">Action</span>&lt;<span style="color: blue">object</span>&gt; releaseCallback, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">HttpConfiguration</span> configuration) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: blue">base</span>(configuration) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.release = releaseCallback; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> ReleaseController(<span style="color: #2b91af">IHttpController</span> controller) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.release(controller); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">base</span>.ReleaseController(controller); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Notice that it's not necessary to override the CreateController method, since the default implementation is good enough - it'll ask the DependencyResolver for an instance of IHttpControllerActivator, which will again ask the DependencyResolver for an instance of the Controller type, in the end invoking your custom GetObject implemention. </p> <p> To keep the above example generic, I just injected an Action&lt;object&gt; into ReleasingControllerFactory - I really don't wish to turn this into a discussion about the merits and demerits of various DI Containers. In any case, I'll leave it as an exercise to you to wire up your favorite DI Container so that the <em>releaseCallback</em> is actually a call to the container's Release method. </p> <h3 id="fea7abbbaa0249e294c89f95e1d5ee4a"> Lifetime Cycles of Infrastructure Components <a href="#fea7abbbaa0249e294c89f95e1d5ee4a" title="permalink">#</a> </h3> <p> Before I conclude, I'd like to point out another <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment">POLA</a> violation that hit me during my investigation. </p> <p> The ASP.NET Web API utilizes DependencyResolver to resolve its own infrastructure types (such as IHttpControllerFactory, IHttpControllerActivator, etc.). Any custom DependencyResolver you supply will also be queried for these types. However: </p> <blockquote> <p> When resolving infrastructure components, the Web API doesn't respect any custom lifecycle you may have defined for these components. </p> </blockquote> <p> At a certain point while I investigated this, I wanted to configure a custom IHttpControllerActivator to have a <em>Web Request Context</em> (<a href="http://amzn.to/12p90MG">my book</a>, section 8.3.4) - in other words, I wanted to create a new instance of IHttpControllerActivator for each incoming HTTP request. </p> <p> This is not possible. The framework queries a custom DependencyResolver for an infrastructure type, but <em>even when it receives an instance</em> (i.e. not null), it doesn't trust the DependencyResolver to efficiently manage the lifetime of that instance. Instead, it caches this instance for ever, and never asks for it again. This is, in my opinion, a mistaken responsibility, and I hope it will be corrected in the final release. </p> <h3 id="6da03656227045a7b521c4dded1f6183"> Concluding Thoughts <a href="#6da03656227045a7b521c4dded1f6183" title="permalink">#</a> </h3> <p> Wiring up the ASP.NET Web API with robust DI is possible, but much harder than it ought to be. Suggestions for improvements are: </p> <ul> <li>A Release hook in DependencyResolver.</li> <li>The framework itself should trust the DependencyResolver to efficiently manage lifetime of all objects it create.</li> </ul> <p> As I've described, there are other places were minor adjustments would be helpful, but these two suggestions are the most important ones. </p> <p> <strong>Update</strong> (2012.03.21): I've posted this feedback to the product group on <a href="http://aspnet.uservoice.com/forums/147201-asp-net-web-api/suggestions/2701848-clean-up-dependency-injection-support">uservoice</a> and <a href="https://connect.microsoft.com/VisualStudio/feedback/details/732337/clean-up-dependency-injection-support-in-asp-net-web-api">Connect</a> - if you agree, please visit those sites and vote up the issues. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="16e5c879f23e477fa7ad2f677e43535b"> <div class="comment-author"><a href="http://stormid.com">Chris Canal</a> <a href="#16e5c879f23e477fa7ad2f677e43535b">#</a></div> <div class="comment-content">&quot;A Release hook in DependencyResolver.&quot;<br> Won't happen. The release issue was highlighted during the RCs and Beta releases and the feedback from Brad Wilson was they were not going to add a release mechanism :(<br> <br> The same applies to the *Activators that where added (IViewPageActivator, etc.), they really need a release hook too</div> <div class="comment-date">2012-03-21 14:29 UTC</div> </div> <div class="comment" id="054c57718a1747b7970f90c8cfd14872"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#054c57718a1747b7970f90c8cfd14872">#</a></div> <div class="comment-content">Why not?</div> <div class="comment-date">2012-03-21 14:46 UTC</div> </div> <div class="comment" id="d8ca9df6c23248a3bc81ac95eae77276"> <div class="comment-author">Jaime Febres <a href="#d8ca9df6c23248a3bc81ac95eae77276">#</a></div> <div class="comment-content">That's the kind of problems when you use a framework that considers DI a second citizen and not really something that is bundled in the heart of it.<br> I know for some people is not an option whether use or not what MVC produces, if you are not one of them, I suggest you give a try to FubuMVC, a MVC framework built from the beginning with DI in mind.<br> It has its own shortcoming, but the advantages surpasses the problems. <br> The issue you had about not releasing disposable objects is simply non an issue in FubuMVC.<br> Kind regards,<br> Jaime.</div> <div class="comment-date">2012-03-21 15:34 UTC</div> </div> <div class="comment" id="1bc72232272a4f9d97bf406ccb9c8794"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1bc72232272a4f9d97bf406ccb9c8794">#</a></div> <div class="comment-content">Jaime, yes, I'm aware of FubuMVC, but have never tried it for a couple of reasons.<br> <br> First of all, right now I need a framework for building REST services, and not a web framework. FubuMVC might have gained REST features (such as conneg) since last I looked, but the ASP.NET Web API does have quite an attractive feature set for building REST APIs.<br> <br> Secondly, last time I discussed the issue of releasing components, Jeremy Miller was very much against it. StructureMap doesn't have a Release hook, so I wonder whether FubuMVC does...<br> <br> Personally, I'm not against trying other technologies, and I've previously looked a bit at OpenRasta and Nancy. However, many of my clients prefer Microsoft technologies.<br> <br> I must also admit to be somewhat taken aback by the direction Microsoft has taken here. The WCF Web API was really good, so I also felt it was my duty to provide feedback to Microsoft as well as the community about this.</div> <div class="comment-date">2012-03-21 16:02 UTC</div> </div> <div class="comment" id="6acad3226eea432d963eaee50dbd441b"> <div class="comment-author">Jaime Febres <a href="#6acad3226eea432d963eaee50dbd441b">#</a></div> <div class="comment-content">Hi Mark,<br> Yes, SM does not have a built-in release hook, I agree on that, but fubumvc uses something called &quot;NestedContainer&quot;.<br> <br> This nested container shares the same settings of your top level container (often StructureMap.ObjectFactory.Container), but with one added bonus, on disposal, also disposes any disposable dependency it resolved during the request lifetime.<br> <br> And all of this is controller by behaviors that wrap an action call at runtime (but configured at start-up), sorta a &quot;Russian Doll Model&quot; like fubu folks like to call to this.<br> So all these behaviors wrap each other, allowing you to effectively dispose resources.<br> To avoid giving wrong explanations, I better share a link which already does this in an effective way:<br> http://codebetter.com/jeremymiller/2011/01/09/fubumvcs-internal-runtime-the-russian-doll-model-and-how-it-compares-to-asp-net-mvc-and-openrasta/<br> <br> And by no means, don't take my words as a harsh opinion against MS MVC, it just a matter of preferences, if to people out there MS MVC or ASP.Net Web API solves their needs, I will not try to convince you otherwise.<br> <br> Kind regards,<br> Jaime.<br> <br> </div> <div class="comment-date">2012-03-21 16:42 UTC</div> </div> <div class="comment" id="adf844e1d0d64d71aca046ade2fb115f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#adf844e1d0d64d71aca046ade2fb115f">#</a></div> <div class="comment-content">Jaime, it's good feedback - I'm receiving it in the most positive way :)</div> <div class="comment-date">2012-03-21 16:45 UTC</div> </div> <div class="comment" id="1ad8272682e5441bb0a6d043ceb13222"> <div class="comment-author"><a href="http://lunarmedia.com">Anders Vindberg</a> <a href="#1ad8272682e5441bb0a6d043ceb13222">#</a></div> <div class="comment-content">Mark do you know if the Unity.WebAPI NuGet Package are releasing controllers in a proper manor?<br> <br> Im currently using it to register my repository following this guidance: <a href="http://www.devtrends.co.uk/blog/introducing-the-unity.webapi-nuget-package">http://www.devtrends.co.uk/blog/introducing-the-unity.webapi-nuget-package</a><br> <br> Thanks,<br> Vindberg.</div> <div class="comment-date">2012-03-23 08:05 UTC</div> </div> <div class="comment" id="880c99b4926f4cd7bb4b0a6f14190ad9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#880c99b4926f4cd7bb4b0a6f14190ad9">#</a></div> <div class="comment-content">Anders, no I don't know, but I would be slightly surprised if it does. Unity (2.0) has several issues related to proper decommissioning. In section 14.2 in <a href="http://amzn.to/12p90MG">my book</a> I describe those issues and how to overcome them.</div> <div class="comment-date">2012-03-23 08:34 UTC</div> </div> <div class="comment" id="c1b482dd42f74e3582d8ee98007b8cc5"> <div class="comment-author">thomas <a href="#c1b482dd42f74e3582d8ee98007b8cc5">#</a></div> <div class="comment-content">I've also experienced these problems that I descibed here : http://www.codedistillers.com/tja/2012/03/11/web-api-beta-and-castle-windsorwithout-idependencyresolver/. IDependencyResolver adds another layer of indirection and the API is not self describing about what parts you should implement if you want the &quot;Release&quot; method...</div> <div class="comment-date">2012-03-26 10:18 UTC</div> </div> <div class="comment" id="cd5252c8438b4d049fe5fe79aeb6b593"> <div class="comment-author">Chelsea Taylor <a href="#cd5252c8438b4d049fe5fe79aeb6b593">#</a></div> <div class="comment-content">@Anders - We are using Unity.WebAPI and it is certainly decommissioning components correctly in our projects. Internally it uses a child container per http request.</div> <div class="comment-date">2012-03-26 15:38 UTC</div> </div> <div class="comment" id="30ddc7b6ef7f4bffa2e6322a835c083a"> <div class="comment-author">Ethan J. Brown <a href="#30ddc7b6ef7f4bffa2e6322a835c083a">#</a></div> <div class="comment-content">I will take the opportunity here to plug ServiceStack for REST based .NET services. Hands down my framework of choice (after having used WCF for years). (Nancy is good too for other reasons, but ServiceStack has a more directly service oriented approach.)<br> <br> https://github.com/ServiceStack<br> <br> <br> The ServiceStack philosophy on IoC - https://github.com/ServiceStack/ServiceStack/wiki/The-IoC-container<br> <br> I'm sure you'll take issue with something in the stack, but keep in mind Demis iterates very quickly and the stack is a very pragmatic / results driven one. Unlike Microsoft, he's very willing to accept patches, etc where things may fall short of user reqs, so if you have feedback, he's always listening.</div> <div class="comment-date">2012-03-30 00:04 UTC</div> </div> <div class="comment" id="e5ffe60846644e158f1c0d772a2db339"> <div class="comment-author">Cristiano <a href="#e5ffe60846644e158f1c0d772a2db339">#</a></div> <div class="comment-content">Great post Mark, I'm 100% on your side.<br> Just beyond my understanding how you can design a support for IoC container with no decommissioning.<br> Hopefully MS will fix this issue/lackness before RTM</div> <div class="comment-date">2012-04-11 10:12 UTC</div> </div> <div class="comment" id="2251c196014140c3876cc516e2e4711b"> <div class="comment-author">JD <a href="#2251c196014140c3876cc516e2e4711b">#</a></div> <div class="comment-content">So I'm not really sure where this left off. I see it marked as &quot;resolved&quot; here: http://aspnetwebstack.codeplex.com/workitem/26<br> <br> But I don't see an ASP.NET MVC release number assigned to it. There is no .Release() method in the ASP.NET MVC 4 release candidate...safe to say they aren't resolving this until the next release?</div> <div class="comment-date">2012-06-05 18:20 UTC</div> </div> <div class="comment" id="36353fb98efe4f6f9f47080437bebab2"> <div class="comment-author">Chris Paynter <a href="#36353fb98efe4f6f9f47080437bebab2">#</a></div> <div class="comment-content">Hey Mark, I cannot find DefaultHttpControllerFactory, and Resharper is unable to resolve it automatically. Googling for a namespace or information regarding it's inclusion in the RC of MVC 4 is unfruitful. Has the name of the class been changed since you authored this post? <br> <br> I've found this article very useful in understanding the use of Windsor with Web API, and I am now wondering whether anything has changed in the RC of MVC 4 that would make any part of this method redundant? Would be great to get an update from you regarding the current status of the framework with regards to implementing an IOC with Web API.<br> <br> Many Thanks<br> <br> Chris</div> <div class="comment-date">2012-08-07 10:02 UTC</div> </div> <div class="comment" id="9cc12029ccdf4c1f8caa18036c94fe83"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9cc12029ccdf4c1f8caa18036c94fe83">#</a></div> <div class="comment-content">The class may very well have changed since the beta.<br> <br> In the future, I may add an update to this article, unless someone else beats me to it.</div> <div class="comment-date">2012-08-13 12:20 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Migrating from WCF Web API to ASP.NET Web API https://blog.ploeh.dk/2012/03/19/MigratingfromWCFWebAPItoASP.NETWebAPI 2012-03-19T22:24:47+00:00 Mark Seemann <div id="post"> <p> Now that <a href="http://wcf.codeplex.com/wikipage?title=WCF%20Web%20API%20is%20now%20ASP.NET%20Web%20API">the WCF Web API has ‘become' the ASP.NET Web API</a>, I've had to migrate a semi-complex code base from the old to the new framework. These are my notes from that process. </p> <h3 id="c3b82739064140b39475e9dfcfda38a6"> Migrating Project References <a href="#c3b82739064140b39475e9dfcfda38a6" title="permalink">#</a> </h3> <p> As far as I can tell, the ASP.NET Web API isn't just a port of the WCF Web API. At a cursory glance, it looks like a complete reimplementation. If it's a port of the old code, it's at least a rather radical one. The assemblies have completely different names, and so on. </p> <p> Both old and new project, however, are based on NuGet packages, so it wasn't particularly hard to change. </p> <p> To remove the old project references, I ran this NuGet command: </p> <p> <pre>Uninstall-Package webapi.all -RemoveDependencies</pre> </p> <p> followed by </p> <p> <pre>Install-Package aspnetwebapi</pre> </p> <p> to install the project references for the ASP.NET Web API. </p> <h3 id="92e50acada4a475abc0cb3e339d6b96e"> Rename Resources to Controllers <a href="#92e50acada4a475abc0cb3e339d6b96e" title="permalink">#</a> </h3> <p> In the WCF Web API, there was no naming convention for the various resource classes. In the quickstarts, they were sometimes called <em>Apis</em> (like ContactsApi), and I called mine <em>Resources</em> (like CatalogResource). Whatever your naming convention was, the easiest things is to find them all and rename them to end with <em>Controller</em> (e.g. CatalogController). </p> <p> AFAICT you <em>can</em> change the naming convention, but I didn't care enough to do so. </p> <h3 id="d06d90dce51a45c5a30ca6b36f485917"> Derive Controllers from ApiController <a href="#d06d90dce51a45c5a30ca6b36f485917" title="permalink">#</a> </h3> <p> Unless you care to manually implement IHttpController, each Controller should derive from ApiController: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">CatalogController</span> : <span style="color: #2b91af">ApiController</span></pre> </p> <h3 id="f66d5692cbca4089a9177a6de86fd230"> Remove Attributes <a href="#f66d5692cbca4089a9177a6de86fd230" title="permalink">#</a> </h3> <p> The WCF Web API uses the [WebGet] and [WebInvoke] attributes. The ASP.NET Web API, on the other hand, uses routes, so I removed all the attributes, including their UriTemplates: </p> <p> <pre style="margin: 0px"><span style="color: green">//[WebGet(UriTemplate = "/")]</span> <span style="color: blue">public</span> <span style="color: #2b91af">Catalog</span> GetRoot()</pre> </p> <h3 id="3f6538f1adb64d5f960467742280f905"> Add Routes <a href="#3f6538f1adb64d5f960467742280f905" title="permalink">#</a> </h3> <p> As a replacement for attributes and UriTemplates, I added HTTP routes: </p> <p> <pre style="margin: 0px">routes.MapHttpRoute( &nbsp;&nbsp;&nbsp; name: <span style="color: #a31515">"DefaultApi"</span>, &nbsp;&nbsp;&nbsp; routeTemplate: <span style="color: #a31515">"{controller}/{id}"</span>, &nbsp;&nbsp;&nbsp; defaults: <span style="color: blue">new</span> { controller = <span style="color: #a31515">"Catalog"</span>, id = <span style="color: #2b91af">RouteParameter</span>.Optional } );</pre> </p> <p> The MapHttpRoute method is an extension method defined in the System.Web.Http namespace, so I had to add a using directive for it. </p> <h3 id="35514705fd9448a9b461a9f5c4bd2982"> Composition <a href="#35514705fd9448a9b461a9f5c4bd2982" title="permalink">#</a> </h3> <p> Wiring up Controllers with Constructor Injection turned out to be rather painful. For a starting point, I used <a href="http://www.nikosbaxevanis.com/bonus-bits/">Nikos Baxevanis'</a> <a href="http://www.nikosbaxevanis.com/bonus-bits/2012/03/using-the-web-api-dependency-resolver-with-castle-windsor.html">guide</a>, but it turns out there are further subtleties which should be addressed (more about this later, but to prevent a stream of comments about the DependencyResolver API: yes, I know about that, but it's inadequate for a number of reasons). </p> <h3 id="69ae58d611af4f8dbbc9e72f89fc37f4"> Media Types <a href="#69ae58d611af4f8dbbc9e72f89fc37f4" title="permalink">#</a> </h3> <p> In the ASP.NET Web API application/json is now the default media type format if the client doesn't supply any Accept header. For the WCF Web API I had to resort to <a href="http://stackoverflow.com/questions/6779254/set-default-response-type-in-wcf-web-api/9002099#9002099">a hack to change the default</a>, so this was a pleasant surprise. </p> <p> It's still pretty easy to add more supported media types: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.XmlFormatter &nbsp;&nbsp;&nbsp; .SupportedMediaTypes.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>(<span style="color: #a31515">"application/vnd.247e.artist+xml"</span>)); <span style="color: #2b91af">GlobalConfiguration</span>.Configuration.Formatters.JsonFormatter &nbsp;&nbsp;&nbsp; .SupportedMediaTypes.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MediaTypeHeaderValue</span>(<span style="color: #a31515">"application/vnd.247e.artist+json"</span>));</pre> </p> <p> (Talk about a <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a> violation, BTW...) </p> <p> However, due to an over-reliance on global state, it's not so easy to figure out how one would go about mapping certain media types to only a single Controller. This was much easier in the WCF Web API because it was possible to assign a separate configuration instance to each Controller/Api/Resource/Service/Whatever... This, I've still to figure out how to do... </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="dc4a68b5b0cf4fd1989f3e50ccecb1df"> <div class="comment-author">Jonty <a href="#dc4a68b5b0cf4fd1989f3e50ccecb1df">#</a></div> <div class="comment-content">Why is the DependencyResolver API inadequate?</div> <div class="comment-date">2012-03-20 10:48 UTC</div> </div> <div class="comment" id="9368883a061c482c879862e471237ab7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9368883a061c482c879862e471237ab7">#</a></div> <div class="comment-content">Jonty, <a href="/2012/03/20/RobustDIWiththeASP.NETWebAPI">here's why</a>.</div> <div class="comment-date">2012-03-20 16:07 UTC</div> </div> <div class="comment" id="122eff0f8e0b4113adf533eeec0bfb4a"> <div class="comment-author">Cristiano <a href="#122eff0f8e0b4113adf533eeec0bfb4a">#</a></div> <div class="comment-content">Hopefully they will fix configuration issue too..<br> webapi prev6 seems much batter than current beta<br> http://code.msdn.microsoft.com/Contact-Manager-Web-API-0e8e373d/view/Discussions<br> see Daniel reply to my post</div> <div class="comment-date">2012-04-11 10:50 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Implementing an Abstract Factory https://blog.ploeh.dk/2012/03/15/ImplementinganAbstractFactory 2012-03-15T21:01:13+00:00 Mark Seemann <div id="post"> <p> <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a> is a tremendously useful pattern when used with Dependency Injection (DI). While I've <a href="http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern/2280289#2280289">repeatedly described</a> how it can be used to solve various problems in DI, apparently I've never described how to implement one. As a comment to <a href="/2010/01/20/EnablingDIforLazyComponents">an older blog post of mine</a>, <a href="http://blogs.codes-sources.com/tja/">Thomas Jaskula</a> asks how I'd implement the IOrderShipperFactory. </p> <p> To stay consistent with <a href="/2010/01/20/EnablingDIforLazyComponents">the old order shipper scenario</a>, this blog post outlines three alternative ways to implement the IOrderShipperFactory interface. </p> <p> To make it a bit more challenging, the implementation should create instances of the OrderShipper2 class, which itself has a dependency: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">OrderShipper2</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IOrderShipper</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IChannel</font></span> channel;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> OrderShipper2(<span style="color: "><font color="#2b91af">IChannel</font></span> channel)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (channel == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"channel"</font></span>);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.channel = channel;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Ship(<span style="color: "><font color="#2b91af">Order</font></span> order)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#008000">// Ship the order and</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#008000">// raise a domain event over this.channel</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> In order to be able to create an instance of OrderShipper2, any factory implementation must be able to supply an IChannel instance. </p> <h3 id="a9fbea155848471d9a0308c6f489ef20"> Manually Coded Factory <a href="#a9fbea155848471d9a0308c6f489ef20" title="permalink">#</a> </h3> <p> The first option is to manually wire up the OrderShipper2 instance within the factory: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">ManualOrderShipperFactory</font></span> :</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IOrderShipperFactory</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IChannel</font></span> channel;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> ManualOrderShipperFactory(<span style="color: "><font color="#2b91af">IChannel</font></span> channel)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (channel == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"channel"</font></span>);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.channel = channel;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#2b91af">IOrderShipper</font></span> Create()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">OrderShipper2</font></span>(<span style="color: "><font color="#0000ff">this</font></span>.channel);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This has the advantage that it's easy to understand. It can be unit tested and implemented in the same library that also contains OrderShipper2 itself. This means that any client of that library is supplied with a read-to-use implementation. </p> <p> The disadvantage of this approach is that if/when the constructor of OrderShipper2 changes, the ManualOrderShipperFactory class must also be corrected. Pragmatically, this may not be a big deal, but one could successfully argue that this violates the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>. </p> <h3 id="d31cce44c2ce48848295ee0b4cec72cf"> Container-based Factory <a href="#d31cce44c2ce48848295ee0b4cec72cf" title="permalink">#</a> </h3> <p> Another option is to make the implementation a thin <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> over a DI Container - in this example <a href="http://castleproject.org/container/index.html">Castle Windsor</a>: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">ContainerFactory</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IOrderShipperFactory</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#2b91af">IWindsorContainer</font></span> container;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> ContainerFactory(<span style="color: "><font color="#2b91af">IWindsorContainer</font></span> container)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (container == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"container"</font></span>);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.container = container;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#2b91af">IOrderShipper</font></span> Create()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.container.Resolve&lt;<span style="color: "><font color="#2b91af">IOrderShipper</font></span>&gt;();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> But wait! Isn't this an application of the <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator anti-pattern</a>? Not if this class is part of the <a href="/2011/07/28/CompositionRoot">Composition Root</a>. </p> <p> If this implementation was placed in the same library as OrderShipper2 itself, it would mean that the library would have a hard dependency on the container. In such a case, it would certainly be a Service Locator. </p> <p> However, when a Composition Root already references a container, it makes sense to place the ContainerFactory class there. This changes its <a href="/2011/08/25/ServiceLocatorrolesvs.mechanics">role</a> to the pure infrastructure component it really ought to be. This seems more SOLID, but the disadvantage is that there's no longer a ready-to-use implementation packaged together with the LazyOrderShipper2 class. All new clients must supply their own implementation. </p> <h3 id="38670a792e674c769e1c7927c533dd26"> Dynamic Proxy <a href="#38670a792e674c769e1c7927c533dd26" title="permalink">#</a> </h3> <p> The third option is to basically reduce the principle behind the container-based factory to its core. Why bother writing even a thin Adapter if one can be automatically generated. </p> <p> With Castle Windsor, the <a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx">Typed Factory Facility</a> makes this possible: </p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">container.AddFacility&lt;<span style="color: "><font color="#2b91af">TypedFactoryFacility</font></span>&gt;();</font> <font style="font-size: 10pt">container.Register(</font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Component</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .For&lt;<span style="color: "><font color="#2b91af">IOrderShipperFactory</font></span>&gt;()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .AsFactory());</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> factory =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; container.Resolve&lt;<span style="color: "><font color="#2b91af">IOrderShipperFactory</font></span>&gt;();</font></pre> </p> <p> There <em>is no longer</em> any code which implements IOrderShipperFactory. Instead, a class conceptually similar to the ContainerFactory class above is dynamically generated and emitted at runtime. </p> <p> While the code never materializes, conceptually, such a dynamically emitted implementation is still part of the Composition Root. </p> <p> This approach has the advantage that it's very DRY, but the disadvantages are similar to the container-based implementation above: there's no longer a ready-to-use implementation. There's also the additional disadvantage that out of the three alternative here outlined, the proxy-based implementation is the most difficult to understand. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="458b08214ae8486594381d2d59f0c910"> <div class="comment-author"><a href="http://szalapski.com">Patrick Szalapski</a> <a href="#458b08214ae8486594381d2d59f0c910">#</a></div> <div class="comment-content">So what is the advantage to having the manually-coded factory in this example at all? It violates the open/closed principle and provides a useless abstraction of the OrderShipper2 constructor. (Or did you just mention it because it is an example of what not to do?)</div> <div class="comment-date">2012-03-16 00:35 UTC</div> </div> <div class="comment" id="3853a3e4f61c43ed84bdd854ad124663"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3853a3e4f61c43ed84bdd854ad124663">#</a></div> <div class="comment-content">Patrick, I just explained what the advantages (and disadvantages) are...</div> <div class="comment-date">2012-03-16 04:47 UTC</div> </div> <div class="comment" id="f0272ff94e80426f91e0eea51d4268ca"> <div class="comment-author">Dmitriy <a href="#f0272ff94e80426f91e0eea51d4268ca">#</a></div> <div class="comment-content">Nice article, thanks.<br> But i have a question. What if object, created by factory, implements IDisposable? Where we should call Dispose()?<br> <br> Sorry for my English...</div> <div class="comment-date">2012-03-16 05:39 UTC</div> </div> <div class="comment" id="de9652328a5645ef8350b83bb2225d43"> <div class="comment-author"><a href="http://www.gibsnag.co.uk">Rob</a> <a href="#de9652328a5645ef8350b83bb2225d43">#</a></div> <div class="comment-content">Good article, I hadn't considered the idea that a composition root can effectively span across factories. I guess it makes sense since most (all?) DI containers have a module system for binding, which is effectively a means of separating out the composition root.</div> <div class="comment-date">2012-03-16 09:20 UTC</div> </div> <div class="comment" id="4e55621f86da4be08c2bcb8c12067ea9"> <div class="comment-author">Dmitriy <a href="#4e55621f86da4be08c2bcb8c12067ea9">#</a></div> <div class="comment-content">Sorry for offtopic. I have small bug report:<br> Often, when I open this blog from the google search results page, javascript function &quot;highlightWord&quot; hangs my Firefox. Probably too big cycle on DOM.</div> <div class="comment-date">2012-03-16 10:30 UTC</div> </div> <div class="comment" id="10b2a617d9e344d183d0a7297b25a0f7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#10b2a617d9e344d183d0a7297b25a0f7">#</a></div> <div class="comment-content">About disposal, I can only think of two options - both of which are leaky abstractions: Either let the returned interface also implement IDisposable, or add a Release method to the Abstract Factory.<br> <br> You can read more about this in chapter 6 in <a href="http://amzn.to/12p90MG">my book</a>.<br> <br> Regarding the bug report: thanks - I've noticed it too, but didn't know what to do about it...</div> <div class="comment-date">2012-03-16 12:52 UTC</div> </div> <div class="comment" id="d5a7a41218344c05bc88b6e4ee068b79"> <div class="comment-author">Thomas <a href="#d5a7a41218344c05bc88b6e4ee068b79">#</a></div> <div class="comment-content">Mark<br> <br> You have cleand up my doubts with this sentence &quot;But wait! Isn’t this an application of the Service Locator anti-pattern? Not if this class is part of the Composition Root.&quot; I was not just confortable about if it's well done or not.<br> <br> I use also Dynamic Proxy factory because it's a great feature.<br> <br> Thomas</div> <div class="comment-date">2012-03-16 14:22 UTC</div> </div> <div class="comment" id="a8c8452879b6479ea0d1ac4eccf3150d"> <div class="comment-author">Dmitriy <a href="#a8c8452879b6479ea0d1ac4eccf3150d">#</a></div> <div class="comment-content">I have read your book. :)<br> I thought about this &quot;life time&quot; problem. And I've got an idea.<br> <br> What if we let a concrete factory to implement the IDisposable interface?<br> In factory.Create method we will push every resolved service into the HashSet.<br> In factory.Dispose method we will call container.Release method for each object in HashSet.<br> Then we register our factory with a short life time, like Transitional.<br> So the container will release services, created by factory, as soon as possible.<br> <br> What do you think about it?<br> <br> About bug in javascript ... <br> Now the DOM visitor method &quot;highlightWord&quot; called for each word. It is very slow. And empty words, which passed into visitor, caused the creation of many empty SPAN elements. It is much slower.<br> I allowed myself to rewrite your functions... Just replace it with the following code.<br> <br> var googleSearchHighlight = function () {<br> if (!document.createElement) return;<br> ref = document.referrer;<br> if (ref.indexOf('?') == -1 || ref.indexOf('/') != -1) {<br> if (document.location.href.indexOf('PermaLink') != -1) {<br> if (ref.indexOf('SearchView.aspx') == -1) return;<br> }<br> else {<br> //Added by Scott Hanselman<br> ref = document.location.href;<br> if (ref.indexOf('?') == -1) return;<br> }<br> }<br> <br> //get all words<br> var allWords = [];<br> qs = ref.substr(ref.indexOf('?') + 1);<br> qsa = qs.split('&amp;');<br> for (i = 0; i &lt; qsa.length; i++) {<br> qsip = qsa[i].split('=');<br> if (qsip.length == 1) continue;<br> if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google, p= for Yahoo<br> words = decodeURIComponent(qsip[1].replace(/\+/g, ' ')).split(/\s+/);<br> for (w = 0; w &lt; words.length; w++) {<br> var word = words[w];<br> if (word.length)<br> allWords.push(word);<br> }<br> }<br> }<br> <br> //pass words into DOM visitor<br> if(allWords.length)<br> highlightWord(document.getElementsByTagName(&quot;body&quot;)[0], allWords);<br> }<br> <br> var highlightWord = function (node, allWords) {<br> // Iterate into this nodes childNodes<br> if (node.hasChildNodes) {<br> var hi_cn;<br> for (hi_cn = 0; hi_cn &lt; node.childNodes.length; hi_cn++) {<br> highlightWord(node.childNodes[hi_cn], allWords);<br> }<br> }<br> <br> // And do this node itself<br> if (node.nodeType == 3) { // text node<br> //do words iteration<br> for (var w = 0; w &lt; allWords.length; w++) {<br> var word = allWords[w];<br> if (!word.length)<br> continue;<br> tempNodeVal = node.nodeValue.toLowerCase();<br> tempWordVal = word.toLowerCase();<br> if (tempNodeVal.indexOf(tempWordVal) != -1) {<br> pn = node.parentNode;<br> if (pn &amp;&amp; pn.className != &quot;searchword&quot;) {<br> // word has not already been highlighted!<br> nv = node.nodeValue;<br> ni = tempNodeVal.indexOf(tempWordVal);<br> // Create a load of replacement nodes<br> before = document.createTextNode(nv.substr(0, ni));<br> docWordVal = nv.substr(ni, word.length);<br> after = document.createTextNode(nv.substr(ni + word.length));<br> hiwordtext = document.createTextNode(docWordVal);<br> hiword = document.createElement(&quot;span&quot;);<br> hiword.className = &quot;searchword&quot;;<br> hiword.appendChild(hiwordtext);<br> pn.insertBefore(before, node);<br> pn.insertBefore(hiword, node);<br> pn.insertBefore(after, node);<br> pn.removeChild(node);<br> }<br> }<br> }<br> }<br> }</div> <div class="comment-date">2012-03-16 16:26 UTC</div> </div> <div class="comment" id="e65154f9c3ec4f4387f941a739dc563e"> <div class="comment-author">Dmitriy <a href="#e65154f9c3ec4f4387f941a739dc563e">#</a></div> <div class="comment-content">Oops... formatting missed.<br> <br> I upload .js file here http://rghost.ru/37057487</div> <div class="comment-date">2012-03-16 17:08 UTC</div> </div> <div class="comment" id="9cecea2e1d174739b8cd6f4c53f1984e"> <div class="comment-author">Steve <a href="#9cecea2e1d174739b8cd6f4c53f1984e">#</a></div> <div class="comment-content">Does the manually coded factory really violate the OCP?<br> <br> Granted, if the constructor to OrderShipper2 changes, you MUST modify the abstract factory. However, isn't modifying the constructor of OrderShipper2 itself a violation of the OCP? If you are adding new dependencies, you are probably making a significant change.<br> <br> At that point, you would just create a new implementation of IOrderShipper.<br> <br> </div> <div class="comment-date">2012-03-16 19:03 UTC</div> </div> <div class="comment" id="f8c33804379649539a3bc43e61328c8d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f8c33804379649539a3bc43e61328c8d">#</a></div> <div class="comment-content">Good point, Steve. You're right... I hadn't thought it all the way through :)</div> <div class="comment-date">2012-03-18 14:03 UTC</div> </div> <div class="comment" id="892a39710625428eb16c73fb8ee90b47"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#892a39710625428eb16c73fb8ee90b47">#</a></div> <div class="comment-content">Dmitriy, if we let only the concrete factory implement IDisposable, then when should it be disposed? How can we deterministically signal that we are 'done' with it?<br> <br> (Thank you very much for your assistance with the javascript - I didn't even know which particular script was causing all that trouble. Unfortunately, the script itself is compiled into the dasBlog engine which hosts this blog, so I can't change it. However, I think I managed to switch it off... Yet another reason to find myself a new blog platform...)</div> <div class="comment-date">2012-03-18 18:25 UTC</div> </div> <div class="comment" id="779ccb71e8b9443088bf356aed07d649"> <div class="comment-author">Dmitriy <a href="#779ccb71e8b9443088bf356aed07d649">#</a></div> <div class="comment-content">I think we should have some sort of Scope to handle the life time ending. In request-based application it can be request. If we need smaller life time, we can write our own Scope, based on some application-specific events.<br> <br> Btw, another thing I think about is the abstract factory's responsibility. If factory consumer can create service with the factory.Create method, maybe we should let it to destroy object with the factory.Destroy method too? Piece of the life time management moves from one place to another. Factory consumer takes responsibility for the life time (defines new scope). For example we have ControllerFactory in ASP.NET MVC. It has the Release method for this. <br> In other words, why not to add the Release method into the abstract factory?</div> <div class="comment-date">2012-03-18 21:54 UTC</div> </div> <div class="comment" id="27ba1f918c04400c84e14b6d35954473"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#27ba1f918c04400c84e14b6d35954473">#</a></div> <div class="comment-content">Implicit scopes like an HTTP request can work pretty well when we can hook into the lifetime cycle of the scope itself. This is possible with HTTP requests, which is why many DI Containers can implicitly clean up when the HTTP request has been handled.<br> <br> However, what if we have a desktop application or a long-running batch job? How do we define an implicit scope then? One option might me to employ a timeout, but I'll have to think more about this... Not a bad idea, though :)</div> <div class="comment-date">2012-03-19 10:42 UTC</div> </div> <div class="comment" id="a36fc793c3d34473b7b96d584b82a2bf"> <div class="comment-author"><a href="http://stackoverflow.com/users/1275399/dmitriy-startsev">Dmitriy Startsev</a> <a href="#a36fc793c3d34473b7b96d584b82a2bf">#</a></div> <div class="comment-content">In multithreaded applications we can use a Thread as a smallest scope.<br> But for a single long-lived thread we have to invent something.<br> <br> It seems that there is no silver bullet in the management of a lifetime. At least without using Resolve-Release pattern in several places instead of one ...<br> <br> Can you write another book on this subject? :)</div> <div class="comment-date">2012-03-19 12:02 UTC</div> </div> <div class="comment" id="a9a3ac5c37834b7cba4bbe74096eefa1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a9a3ac5c37834b7cba4bbe74096eefa1">#</a></div> <div class="comment-content">Dmitriy, I still need to think more about this, but AFACT in order to use a scope for decommissioning, there must be some hook that we can attach to in order to clean up when the scope ends. I'm not aware of such a hook on a Thread, but I must admit that I rarely do low-level work with Threads.<br> <br> A Task&lt;T&gt;, on the other, provides us with the ability to attach a continuation, so that seems to me to be an appropriate candidate...</div> <div class="comment-date">2012-03-24 08:46 UTC</div> </div> <div class="comment" id="b237257aa0f7422ebca76da36d2c1e46"> <div class="comment-author">Hennadii Omelchenko <a href="#b237257aa0f7422ebca76da36d2c1e46">#</a></div> <div class="comment-content">Mark, how does Container-Based factory put up with Register-Resolve-Release pattern? For instance, if we have ten different factories, then we end up with at least ten 'Resolve' calls, and you state in your book that there must be only one, in composition root</div> <div class="comment-date">2012-12-23 14:31 UTC</div> </div> <div class="comment" id="ffa263c235194a0ba5d11108814c90ad"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ffa263c235194a0ba5d11108814c90ad">#</a></div> <div class="comment-content">You could create each factory instance using the &quot;new&quot; keyword and register the instances with the container.<br> <br> Or you could register the container with itself, enabling it to auto-wire itself.<br> <br> None of these options are particularly nice, which is why I instead tend to prefer one of the other options described above.</div> <div class="comment-date">2012-12-23 15:02 UTC</div> </div> <div class="comment" id="9744b93ab49b4d62bc7766f005d86f61"> <div class="comment-author">Julien Vulliet <a href="#9744b93ab49b4d62bc7766f005d86f61">#</a></div> <div class="comment-content">Interesting article, one thing I'm wondering (apart from many other questions which would likely require a post or two on their own as an answer), isn't finally using the dynamic proxy solution somehow a hidden dependency to your container? You don't have explicit reference, but if you swap container you might have an issue with this being implemented differently (one container might prefer func, another one interfaces....), so finally you're not able to swap container that easily?</div> <div class="comment-date">2014-02-28 22:58 UTC</div> </div> <div class="comment" id="7b0d1242fe824371a0dae29e0cae4365"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7b0d1242fe824371a0dae29e0cae4365">#</a></div> <div class="comment-content"> <p> Julien, thank you for your question. Using a Dynamic Proxy is an implementation detail. The consumer (in this case <a href="/2010/01/20/EnablingDIforLazyComponents">LazyOrderShipper2</a>) depends only on IOrderShipperFactory. </p> <p> Does using a Dynamic Proxy make it harder to swap containers? Yes, it does, because I'm only aware of one .NET DI Container that has this capability (although it's a couple of years ago since I last surveyed the .NET DI Container landscape). Therefore, if you start out with Castle Windsor, and then later on decide to exchange it for another DI Container, you would have to supply a manually coded implementation of IOrderShipperFactory. You could choose to implement either a Manually Coded Factory, or a Container-based Factory, as described in this article. It's rather trivial to do, so I would hardly call it blocking issue. </p> <p> The more you rely on specific features of a particular DI Container, the more work you'll have to perform to migrate. This is why I always recommend that you design your classes following normal, good design practices first, without any regard to how you'd use them with any particular DI Container. Only when you've designed your API should you figure out how to compose it with a DI Container (<a href="/2012/11/06/WhentouseaDIContainer">if at all</a>). </p> </div> <div class="comment-date">2014-03-01 14:47 UTC</div> </div> <div class="comment" id="32b5952f96ed4e66a3c6bb066df5421e"> <div class="comment-author">jam40jeff <a href="#32b5952f96ed4e66a3c6bb066df5421e">#</a></div> <div class="comment-content">Isn't the first option (manually coded factory carrying instance dependencies as factory dependencies) the only one which still allows for complete container verification? If that is the case, wouldn't that alone trump any other reasons not to use this option?</div> <div class="comment-date">2014-12-31 20:57 UTC</div> </div> <div class="comment" id="575c84ddee4b4860a416a60d35656e51"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#575c84ddee4b4860a416a60d35656e51">#</a></div> <div class="comment-content"> <p> Personally, <a href="/2011/12/21/TestingContainerConfigurations">I don't find container verification particularly valuable</a>, but then again, I mostly use <a href="/2014/06/10/pure-di">Pure DI</a> anyway. </p> </div> <div class="comment-date">2014-12-31 21:33 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Is Layering Worth the Mapping? https://blog.ploeh.dk/2012/02/09/IsLayeringWorththeMapping 2012-02-09T22:55:44+00:00 Mark Seemann <div id="post"> <p> <em>For years, layered application architecture has been a de-facto standard for loosely coupled architectures, but the question is: does the layering really provide benefit?</em> </p> <p> In theory, layering is a way to decouple concerns so that UI concerns or data access technologies don't pollute the domain model. However, this style of architecture seems to come at a pretty steep price: there's a lot of mapping going on between the layers. Is it really worth the price, or is it OK to define data structures that cut across the various layers? </p> <p> The short answer is that if you cut across the layers, it's no longer a layered application. However, the price of layering may still be too high. </p> <p> In this post I'll examine the problem and the proposed solution and demonstrate why none of them are particularly good. In the end, I'll provide a pointer going forward. </p> <h3 id="6d1f788b9ee245c4b12f56d88acd8631"> Proper Layering <a href="#6d1f788b9ee245c4b12f56d88acd8631" title="permalink">#</a> </h3> <p> To understand the problem with layering, I'll describe a fairly simple example. Assume that you are building a music service and you've been asked to render a top 10 list for the day. It would have to look something like this in a web page: </p> <p> <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Top10Tracks" border="0" alt="Top10Tracks" src="/content/binary/Windows-Live-Writer/04fbdab3357e_11E24/Top10Tracks_1.png" width="494" height="250"> </p> <p> As part of rendering the list, you must color the <em>Movement</em> values accordingly using CSS. </p> <p> A properly layered architecture would look something like this: </p> <p> <img src="/content/binary/Windows-Live-Writer/04fbdab3357e_11E24/ProperLayering_3.png"> </p> <p> Each layer defines some services and some data-carrying classes (<em>Entities</em>, if you want to stick with the <a href="http://www.amazon.com/gp/product/0321125215/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321125215">Domain-Driven Design</a> terminology). The Track class is defined by the Domain layer, while the TopTrackViewModel class is defined in the User Interface layer, and so on. If you are wondering about why Track is used to communicate both up and down, this is because the Domain layer should be the most decoupled layer, so the other layers exist to serve it. In fact, this is just a vertical representation of the Ports and Adapters architecture, with the Domain Model sitting in the center. </p> <p> This architecture is very decoupled, but comes at the cost of much mapping and seemingly redundant repetition. To demonstrate why that is, I'm going to show you some of the code. This is an ASP.NET MVC application, so the Controller is an obvious place to start: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">ViewResult</font></span> Index()</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> date = <span style="color: "><font color="#2b91af">DateTime</font></span>.Now.Date;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> topTracks = <span style="color: "><font color="#0000ff">this</font></span>.trackService.GetTopFor(date);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.View(topTracks);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This doesn't look so bad. It asks an ITrackService for the top tracks for the day and returns a list of TopTrackViewModel instances. This is the implementation of the track service: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#2b91af">TopTrackViewModel</font></span>&gt; GetTopFor(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">DateTime</font></span> date)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> today = <span style="color: "><font color="#2b91af">DateTime</font></span>.Now.Date;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> yesterDay = today - <span style="color: "><font color="#2b91af">TimeSpan</font></span>.FromDays(1);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> todaysTracks = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.repository.GetTopFor(today).ToList();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> yesterdaysTracks = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.repository.GetTopFor(yesterDay).ToList();</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> length = todaysTracks.Count;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> positions = <span style="color: "><font color="#2b91af">Enumerable</font></span>.Range(1, length);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">from</font></span> tt <span style="color: "><font color="#0000ff">in</font></span> todaysTracks.Zip(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; positions, (t, p) =&gt;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> { Position = p, Track = t })</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">let</font></span> yp = (<span style="color: "><font color="#0000ff">from</font></span> yt <span style="color: "><font color="#0000ff">in</font></span> yesterdaysTracks.Zip(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; positions, (t, p) =&gt; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#0000ff">new</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Position = p,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Track = t</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; })</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">where</font></span> yt.Track.Id == tt.Track.Id</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">select</font></span> yt.Position)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .DefaultIfEmpty(-1)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Single()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">let</font></span> cssClass = GetCssClass(tt.Position, yp)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">select</font></span> <span style="color: "><font color="#0000ff">new</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">TopTrackViewModel</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Position = tt.Position,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name = tt.Track.Name,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Artist = tt.Track.Artist,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CssClass = cssClass</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</font> <font style="font-size: 10pt">}</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">static</font></span> <span style="color: "><font color="#0000ff">string</font></span> GetCssClass(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">int</font></span> todaysPosition, <span style="color: "><font color="#0000ff">int</font></span> yesterdaysPosition)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (yesterdaysPosition &lt; 0)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#a31515">"new"</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (todaysPosition &lt; yesterdaysPosition)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#a31515">"up"</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (todaysPosition == yesterdaysPosition)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#a31515">"same"</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#a31515">"down"</font></span>;</font> <font style="font-size: 10pt">}</font></pre> </p> <p> While that looks fairly complex, there's really not a lot of mapping going on. Most of the work is spent getting the top 10 track for today and yesterday. For each position on today's top 10, the query finds the position of the same track on yesterday's top 10 and creates a TopTrackViewModel instance accordingly. </p> <p> Here's the only mapping code involved: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">select</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">new</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">TopTrackViewModel</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; Position = tt.Position,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; Name = tt.Track.Name,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; Artist = tt.Track.Artist,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; CssClass = cssClass</font> <font style="font-size: 10pt">};</font></pre> </p> <p> This maps from a Track (a Domain class) to a TopTrackViewModel (a UI class). </p> <p> This is the relevant implementation of the repository: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#2b91af">Track</font></span>&gt; GetTopFor(<span style="color: "><font color="#2b91af">DateTime</font></span> date)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> dbTracks = <span style="color: "><font color="#0000ff">this</font></span>.GetTopTracks(date);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">foreach</font></span> (<span style="color: "><font color="#0000ff">var</font></span> dbTrack <span style="color: "><font color="#0000ff">in</font></span> dbTracks)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">yield</font></span> <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Track</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTrack.Id,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTrack.Name,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTrack.Artist);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> You may be wondering about the translation from DbTrack to Track. In this case you can assume that the DbTrack class is a class representation of a database table, modeled along the lines of your favorite ORM. The Track class, on the other hand, is a proper object-oriented class which <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">protects its invariants</a>: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Track</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#0000ff">int</font></span> id;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">string</font></span> name;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">string</font></span> artist;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Track(<span style="color: "><font color="#0000ff">int</font></span> id, <span style="color: "><font color="#0000ff">string</font></span> name, <span style="color: "><font color="#0000ff">string</font></span> artist)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (name == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"name"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (artist == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"artist"</font></span>);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.id = id;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.name = name;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.artist = artist;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">int</font></span> Id</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.id; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">string</font></span> Name</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.name; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#0000ff">set</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (<span style="color: "><font color="#0000ff">value</font></span> == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"value"</font></span>);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.name = <span style="color: "><font color="#0000ff">value</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">string</font></span> Artist</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.artist; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#0000ff">set</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (<span style="color: "><font color="#0000ff">value</font></span> == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"value"</font></span>);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.artist = <span style="color: "><font color="#0000ff">value</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> No ORM I've encountered so far has been able to properly address such invariants - particularly the non-default constructor seems to be a showstopper. This is the reason a separate DbTrack class is required, even for ORMs with so-called POCO support. </p> <p> In summary, that's a lot of mapping. What would be involved if a new field is required in the top 10 table? Imagine that you are being asked to provide the release label as an extra column. </p> <ol> <li>A Label column must be added to the database schema and the DbTrack class.</li> <li>A Label property must be added to the Track class.</li> <li>The mapping from DbTrack to Track must be updated.</li> <li>A Label property must be added to the TopTrackViewModel class.</li> <li>The mapping from Track to TopTrackViewModel must be updated.</li> <li>The UI must be updated.</li> </ol> <p> That's a lot of work in order to add a single data element, and this is even a read-only scenario! Is it really worth it? </p> <h3 id="95a1e8388e02497da91a6bb87d2dd933"> Cross-Cutting Entities <a href="#95a1e8388e02497da91a6bb87d2dd933" title="permalink">#</a> </h3> <p> Is strict separation between layers really so important? What would happen if Entities were allowed to travel across all layers? Would that really be so bad? </p> <p> Such an architecture is often drawn like this: </p> <p> <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="CrossCuttingEntities" border="0" alt="CrossCuttingEntities" src="/content/binary/Windows-Live-Writer/04fbdab3357e_11E24/CrossCuttingEntities_3.png" width="441" height="480"> </p> <p> Now, a single Track class is allowed to travel from layer to layer in order to avoid mapping. The controller code hasn't really changed, although the model returned to the View is no longer a sequence of TopTrackViewModel, but simply a sequence of Track instances: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">ViewResult</font></span> Index()</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> date = <span style="color: "><font color="#2b91af">DateTime</font></span>.Now.Date;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> topTracks = <span style="color: "><font color="#0000ff">this</font></span>.trackService.GetTopFor(date);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.View(topTracks);</font><font style="font-size: 10pt">}</font></pre> </p> <p> The GetTopFor method also looks familiar: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#2b91af">Track</font></span>&gt; GetTopFor(<span style="color: "><font color="#2b91af">DateTime</font></span> date)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> today = <span style="color: "><font color="#2b91af">DateTime</font></span>.Now.Date;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> yesterDay = today - <span style="color: "><font color="#2b91af">TimeSpan</font></span>.FromDays(1);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> todaysTracks = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.repository.GetTopFor(today).ToList();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> yesterdaysTracks = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.repository.GetTopFor(yesterDay).ToList();</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> length = todaysTracks.Count;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> positions = <span style="color: "><font color="#2b91af">Enumerable</font></span>.Range(1, length);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">from</font></span> tt <span style="color: "><font color="#0000ff">in</font></span> todaysTracks.Zip(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; positions, (t, p) =&gt; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> { Position = p, Track = t })</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">let</font></span> yp = (<span style="color: "><font color="#0000ff">from</font></span> yt <span style="color: "><font color="#0000ff">in</font></span> yesterdaysTracks.Zip(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; positions, (t, p) =&gt;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#0000ff">new</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Position = p,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Track = t</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; })</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">where</font></span> yt.Track.Id == tt.Track.Id</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">select</font></span> yt.Position)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .DefaultIfEmpty(-1)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Single()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">let</font></span> cssClass = GetCssClass(tt.Position, yp)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">select</font></span> Enrich(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tt.Track, tt.Position, cssClass);</font> <font style="font-size: 10pt">}</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">static</font></span> <span style="color: "><font color="#0000ff">string</font></span> GetCssClass(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">int</font></span> todaysPosition, <span style="color: "><font color="#0000ff">int</font></span> yesterdaysPosition)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (yesterdaysPosition &lt; 0)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#a31515">"new"</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (todaysPosition &lt; yesterdaysPosition)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#a31515">"up"</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (todaysPosition == yesterdaysPosition)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#a31515">"same"</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#a31515">"down"</font></span>;</font> <font style="font-size: 10pt">}</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">static</font></span> <span style="color: "><font color="#2b91af">Track</font></span> Enrich(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Track</font></span> track, <span style="color: "><font color="#0000ff">int</font></span> position, <span style="color: "><font color="#0000ff">string</font></span> cssClass)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; track.Position = position;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; track.CssClass = cssClass;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> track;</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Whether or not much has been gained is likely to be a subjective assessment. While mapping is no longer taking place, it's still necessary to assign a CSS Class and Position to the track before handing it off to the View. This is the responsibility of the new Enrich method: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">static</font></span> <span style="color: "><font color="#2b91af">Track</font></span> Enrich(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Track</font></span> track, <span style="color: "><font color="#0000ff">int</font></span> position, <span style="color: "><font color="#0000ff">string</font></span> cssClass)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; track.Position = position;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; track.CssClass = cssClass;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> track;</font> <font style="font-size: 10pt">}</font></pre> </p> <p> If not much is gained at the UI layer, perhaps the data access layer has become simpler? This is, indeed, the case: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#2b91af">Track</font></span>&gt; GetTopFor(<span style="color: "><font color="#2b91af">DateTime</font></span> date)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.GetTopTracks(date);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> If, hypothetically, you were asked to add a label to the top 10 table it would be much simpler: </p> <ol> <li>A Label column must be added to the database schema and the Track class.</li> <li>The UI must be updated.</li> </ol> <p> This looks good. Are there any disadvantages? Yes, certainly. Consider the Track class: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Track</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">int</font></span> Id { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">string</font></span> Name { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">string</font></span> Artist { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">int</font></span> Position { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">string</font></span> CssClass { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> It also looks simpler than before, but this is actually not particularly beneficial, as it doesn't protect its invariants. In order to play nice with the ORM of your choice, it must have a <a href="/2011/05/30/DesignSmellDefaultConstructor">default constructor</a>. It also has <a href="/2011/05/26/CodeSmellAutomaticProperty">automatic properties</a>. However, most insidiously, it also somehow gained the Position and CssClass properties. </p> <p> What does the Position property imply outside of the context of a top 10 list? A position in relation to what? </p> <p> Even worse, why do we have a property called CssClass? CSS is a very web-specific technology so why is this property available to the Data Access and Domain layers? How does this fit if you are ever asked to build a native desktop app based on the Domain Model? Or a REST API? Or a batch job? </p> <p> When Entities are allowed to travel along layers, the layers basically collapse. UI concerns and data access concerns will inevitably be mixed up. You may think you have layers, but you don't. </p> <p> Is that such a bad thing, though? </p> <p> Perhaps not, but I think it's worth pointing out: </p> <blockquote> <p> The choice is whether or not you want to build a layered application. If you want layering, the separation must be strict. If it isn't, it's not a layered application. </p> </blockquote> <p> There may be great benefits to be gained from allowing Entities to travel from database to user interface. Much mapping cost goes away, but you must realize that then you're no longer building a layered application - now you're building a web application (or whichever other type of app you're initially building). </p> <h3 id="9172ef4da6a44dda8e3de573ae797739"> Further Thoughts <a href="#9172ef4da6a44dda8e3de573ae797739" title="permalink">#</a> </h3> <p> It's a common question: <em>how hard is it to add a new field to the user interface?</em> </p> <p> The underlying assumption is that the field must somehow originate from a corresponding database column. If this is the case, mapping seems to be in the way. </p> <p> However, if this is the major concern about the application you're currently building, it's a strong indication that you are building a CRUD application. If that's the case, <a href="http://msdn.microsoft.com/en-us/magazine/ee236415.aspx">you probably don't need a Domain Model at all</a>. Go ahead and let your ORM POCO classes travel up and down the stack, but don't bother creating layers: you'll be building a monolithic application no matter how hard you try not to. </p> <p> In the end it looks as though none of the options outlined in this article are particularly good. Strict layering leads to too much mapping, and no mapping leads to monolithic applications. Personally, I've certainly written quite a lot of strictly layered applications, and while the separation of concerns was good, I was never happy with the mapping overhead. </p> <p> At the heart of the problem is the CRUDy nature of many applications. In such applications, complex object graphs are traveling up and down the stack. The trick is to avoid complex object graphs. </p> <p> Move less data around and things are likely to become simpler. This is one of the many interesting promises of <a href="http://abdullin.com/cqrs">CQRS</a>, and particularly it's asymmetric approach to data. </p> <p> To be honest, I wish I had fully realized this when I started writing <a href="http://amzn.to/12p90MG">my book</a>, but when I finally realized what I'd implicitly felt for long, it was too late to change direction. Rest assured that nothing in the book is fundamentally flawed. The patterns etc. still hold, but the examples could have been cleaner if the sample applications had taken a CQRS-like direction instead of strict layering. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="894d3a87b6644e6cac91ea05ed2d11a0"> <div class="comment-author"><a href="http://moh-abed.com">Mohamed Abed</a> <a href="#894d3a87b6644e6cac91ea05ed2d11a0">#</a></div> <div class="comment-content">I have came to the same sequence and conclusions and in my recent application I had the following setup:<br> <br> - Domain layer with rich objects and protected variations<br> - Commands and Command handlers (write only) acting on the domain entities<br> - Mapping domain layer using NHibernate (I thought at first to start by creating a different data model because of the constraints of orm on the domain, but i managed to get rid of most of them - like having a default but private constructor, and mapping private fields instead of public properties ...)<br> <br> - Read model which consist of view models.<br> - Red model mapping layer which uses the orm to map view models to database directly (thus i got rid of mapping between view models and domains - but introduced a mapping between view models and database).<br> - Query objects to query the read model.<br> <br> at the end of application i came to almost the same conclusion of not being happy with the mappings.<br> <br> - I have to map domain model to database.<br> - I have to map between view models (read model) and database.<br> - I have to map between view models and commands.<br> - I have to map between commands and domain model.<br> - Having many fine grained commands is good in terms of defining strict behaviors and ubiquitous language but at the cost of maintainability, now when i introduce a new field most probably will have to modify a database field, domain model, view model, two data mappings for domain model and view model, 1 or more command and command handlers, and UI !<br> <br> So taking the practice of CQRS for me enriched the domain a little, simplified the operations and separated it from querying, but complicated the implementation and maintenance of the application.<br> <br> So I am still searching for a better approach to gain the rich domain model behavior and simplifying maintenance and eliminate mappings.</div> <div class="comment-date">2012-02-10 02:16 UTC</div> </div> <div class="comment" id="b9af9ba1ee634cc4a91669dc09089200"> <div class="comment-author">bitbonk <a href="#b9af9ba1ee634cc4a91669dc09089200">#</a></div> <div class="comment-content">Good article, thank you for that. <br> <br> I too have felt pain of a lot of useless mapping a lot times. But even for CRUD applications where the business rules do not change much, I still almost always take the pain and do the mapping because I have a deep sitting fear of monolthic apps and code rot. If I give my entities lots of different concerns (UI, persistence, domainlogic etc.), isn't this kind of code smell the culture medium for more code smell and code rot? When evolving my application and I am faced with a quirk in my code, I often only have two choices: Add a new quirk or remove the original quirk. It kind of seems that in the long run a rotten monolithic app that his hard to maintain and extend becomes almost inevitable if I give up SoC the way you described.<br> <br> Did I understand you correctly that you'd say it's OK to soften SoC a bit and let the entity travel across all layers for *some* apps? If yes, what kind of apps? Do you share my fear of inevitable code rot? How do you deal with this?</div> <div class="comment-date">2012-02-10 09:11 UTC</div> </div> <div class="comment" id="7d45bb22cb28472e853b3225b645194e"> <div class="comment-author">Jonty <a href="#7d45bb22cb28472e853b3225b645194e">#</a></div> <div class="comment-content">NHibernate allows protected constructors and setters, is that good enough? RavenDB allows private constructor and setters.</div> <div class="comment-date">2012-02-10 13:30 UTC</div> </div> <div class="comment" id="d8501f883f1b4f488aaa95cd836f4a45"> <div class="comment-author">Harry McIntyre <a href="#d8501f883f1b4f488aaa95cd836f4a45">#</a></div> <div class="comment-content">I think you might be interested to hear about the stack I use.<br> <br> I use either convention based NH mappings which make me almost forget it's there, or MemoryImage to reconstruct my object model (the boilerplate is reduced by wrapping my objects with proxies that intercept method calls and record them as a json Event Source). In both cases I can use a helper library which wraps my objects with proxies that help protect invariants.<br> <br> My web UI is built using a framework along the lines of the Naked Object a pattern*, which can be customised using ViewModels and custom views, and which enforces good REST design. This framework turns objects into pages and methods into ajax forms.<br> <br> I built all this stuff after getting frustrated with the problems you mention above, inspired in part by all the time I spent at Uni trying to make games (in which an in memory domain model is rendered to the screen by a disconnected rendering engine, and input collected via a separate mechanism and applied to that model). <br> <br> The long of the short of it is that I can add a field to the screen in seconds, and add a new feature in minutes. There are some trade offs, but it's great to feel like I am just writing an object model and leaving the rest to my frameworks. <br> <br> *not Naked Objects MVC, a DIY library thats not ready for the public yet.<br> <br> http://en.wikipedia.org/wiki/Naked_objects<br> https://github.com/mcintyre321/NhCodeFirst my mappings lib (but Fluent NH is probably fine)<br> http://martinfowler.com/bliki/MemoryImage.html<br> https://github.com/mcintyre321/Harden my invariant helper library</div> <div class="comment-date">2012-02-10 16:44 UTC</div> </div> <div class="comment" id="9abad7c0bbbb46868bf52db1ab3d69de"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9abad7c0bbbb46868bf52db1ab3d69de">#</a></div> <div class="comment-content">bitbonk, that wasn't what I said at all. On the contrary, the point of the post is that as soon as you soften separation of concerns just a bit, it becomes impossible to maintain that separation. It's an all-or-nothing proposition. Either you maintain strict separation, or you'd be building a monolithic application whether you know it or not.<br> <br> The only thing I'm saying is that it's better to build a monolithic application when you know and understand that this is what you are doing, than it is to be building a monolithic application while under the illusion that you are building a decoupled application.</div> <div class="comment-date">2012-02-10 16:46 UTC</div> </div> <div class="comment" id="efdab2b4581f4f868b2f6d70b32c6c68"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#efdab2b4581f4f868b2f6d70b32c6c68">#</a></div> <div class="comment-content">Jonty: I don't know... How does protected or private constructors or properties help protect invariants?</div> <div class="comment-date">2012-02-10 16:48 UTC</div> </div> <div class="comment" id="df27b3cbfcbf44cf985ea0e45e8f01b8"> <div class="comment-author">nelson <a href="#df27b3cbfcbf44cf985ea0e45e8f01b8">#</a></div> <div class="comment-content">I understand where you are coming from; but good architecture has always been at odds with RAD. You do make some good points, but as you certainly know, discipline in software design is very important. Many applications we build as &quot;one-offs&quot; are in production for years - and taking the RAD approach almost always results in software that everyone despises (especially the programmer left maintaining it). Proof: COBOL, Office VBA, Ruby on Rails. How many applications were built off of these platforms because it was easier and faster to get started, only to have the resulting codebase become something that doesn't work for anyone? We use C# and good design principles specifically to avoid this scenario. If I were to be in a position where I truly (absolutely truly) knew I wanted a CRUD app, I'd throw it together in Rails - then scrap that project once it grows too complex in favor of CQRS/C#/ASP.net MVC.<br> <br> If there's a point in there, it's that static languages (without designers) generally don't lend well to RAD, and if RAD is what you need, you have many popular options that aren't C#.<br> <br> Also, I have two problems with some of the sample code you wrote. Specifically, the Enrich method. First, a method signature such as:<br> <br> T Method(T obj, ...);<br> <br> Implies to consumers that the method is pure. Because this isn't the case, consumers will incorrectly use this method without the intention of modifying the original object. Second, if it is true that your ViewModels are POCOs consisting of your mapped properties AND a list of computed properties, perhaps you should instead use inheritance. This implementation is certainly SOLID:<br> <br> (sorry for the formatting)<br> <br> class Track <br> {<br> /* Mapped Properties */ <br> <br> public Track()<br> {<br> }<br> <br> protected Track(Track rhs)<br> {<br> Prop1 = rhs.Prop1;<br> Prop2 = rhs.Prop2;<br> Prop3 = rhs.Prop3;<br> }<br> }<br> <br> class TrackWebViewModel : Track <br> {<br> public string ClssClass { get; private set; } <br> <br> public TrackWebViewModel(Track track, string cssClass) : this(track)<br> {<br> CssClass = cssClass;<br> }<br> } <br> <br> let cssClass = GetCssClass(tt.Position, yp) select Enrich(tt.Track, tt.Position, cssClass)<br> <br> Simply becomes<br> <br> select new TrackWebViewModel(tt.Track, tt.Position, cssClass)</div> <div class="comment-date">2012-02-10 18:40 UTC</div> </div> <div class="comment" id="64687d3c0cc64654a3b1b6ad8e599827"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#64687d3c0cc64654a3b1b6ad8e599827">#</a></div> <div class="comment-content">nelson, I never claimed the code in this post was supposed to represent <em>good</em> code :) Agreed that the Enrich method violates CQS.<br> <br> While your suggested, inheritance-based solution seems cleaner, please note that it also introduces a mapping (in the copy constructor). Once you start doing this, there's almost always a composition-based solution which is at least as good as the inheritance-based solution.</div> <div class="comment-date">2012-02-10 18:54 UTC</div> </div> <div class="comment" id="b5880eb45ddd473bab8375be3f137845"> <div class="comment-author">nelson <a href="#b5880eb45ddd473bab8375be3f137845">#</a></div> <div class="comment-content">Oh, I certainly agree that inheritance should be avoided whenever possible. It's one of the most over (and improperly) used constructs in any OO language. I was simply writing an implementation that would work with your existing code with fewest changes as possible :) perhaps it was the maintainer in me talking, not the designer :p<br> <br> As far as the copy-ctor, yes, that is a mapping, but when you add a field, you just have to modify a file you're already modifying. This is a bit nicer than having to update mapping code halfway across your application. But you're right, composting a Track into a specific view model would be a nicer approach with fewer modifications required to add more view models and avoiding additional mapping.<br> <br> Though, I still am a bit concerned about the architecture as a whole. I understand in this post you were thinking out loud about a possible way to reduce complexity in certain types of applications, yet I'm wondering if you had considered using a platform designed for applications like this instead of using C#. Rails is quite handy when you need CRUD.</div> <div class="comment-date">2012-02-10 19:22 UTC</div> </div> <div class="comment" id="204701c9b5e946eba6bac849d7dca33f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#204701c9b5e946eba6bac849d7dca33f">#</a></div> <div class="comment-content">Well, I was mainly attempting to address the perception that a lot of people have that decoupling creates a lot of mapping and therefore isn't worth the effort (their sentiment, not mine). Unfortunately, I have too little experience with technology stacks outside of .NET to be able to say something authoritative about them, but that particular positioning of Rails matches what I've previously heard.</div> <div class="comment-date">2012-02-10 19:30 UTC</div> </div> <div class="comment" id="8918cd158f7d4eea967fa10aba6b46ab"> <div class="comment-author"><a href="http://www.sphereworks.dk">Anders Borum</a> <a href="#8918cd158f7d4eea967fa10aba6b46ab">#</a></div> <div class="comment-content">Hi Mark,<br> <br> thanks for the blog posts and DI book, which I'm currently reading over the weekend. I guess the scenario you're describing here is more or less commonplace, and as the team I'm working on are constantly bringing this subject up on the whiteboard, I thought I'd share a few thoughts on the mapping part. Letting the Track entity you're mentioning cross all layers is probably not a good idea.<br> <br> I agree that one should program to interfaces. Thus, any entity (be it an entity created via LINQ to SQL, EF, any other ORM/DAL or business object) should be modeled and exposed via an interface (or hierarchy thereof) from repositories or the like. With that in mind, here's what I think of mapping between entities. Popular ORM/DALs provide support for partial entities, external mapping files, POCOs, model-first etc.<br> <br> Take this simple example:<br> <br> // simplified pattern (deliberately not guarding args, and more)<br> interface IPageEntity { string Name { get; }}<br> class PageEntity : IPageEntity {}<br> class Page : IPage<br> {<br> private IPageEntity entity;<br> private IStrategy strategy;<br> <br> public Page(IPageEntity entity, IStrategy strategy)<br> {<br> this.entity = entity;<br> this.strategy = strategy;<br> }<br> <br> public string Name<br> {<br> get { return this.entity.Name; }<br> set { this.strategy.CopyOnWrite(ref this.entity).Name = value; }<br> }<br> }<br> <br> One of the benefits from using an ORM/DAL is that queries are strongly typed and results are projected to strongly typed entities (concrete or anonymous). They are prime candidates for embedding inside of business objects (BO) (as DTOs), as long as they implement the required interface for injected in the constructor. This is much more flexible (and clean) rather than taking a series of arguments, each corresponding to a property on the original entity. Instead of having the BO keeping private members mapped to the constructor args, and subsequently mapping public properties and methods to said members, you simply map to the entity injected in the BO.<br> <br> The benefit is much greater performance (less copying of args), a consistent design pattern and a layer of indirection in your BO that allows you to capture any potential changes to your entity (such as using the copy-on-write pattern that creates an exact copy of the entity, releasing it from a potential shared cache you've created by decorating the repository, from which the entity originally was retrieved).<br> <br> Again, in my example one of the goals were to make use of shared (cached) entities for high performance, while making sure that there are no side effects from changing a business object (seeing this from client code perspective).<br> <br> Code following the above pattern is just as testable as the code in your example (one could argue it's even easier to write tests, and maintenance is vastly reduced). Whenever I see BO constructors taking huge amounts of args I cry blood.<br> <br> In my experience, this allows a strictly layered application to thrive without the mapping overhead.<br> <br> Your thoughts?</div> <div class="comment-date">2012-02-11 13:37 UTC</div> </div> <div class="comment" id="6ac7515bf3e34521a6a16e0a2791823e"> <div class="comment-author"><a href="Http://www.planetgeek.ch">Daniel Marbach</a> <a href="#6ac7515bf3e34521a6a16e0a2791823e">#</a></div> <div class="comment-content">Hy<br> What do you propose in cases when you have a client server application with request/response over WCF and the domain model living on the server? Is there a better way than for example mapping from NH entities to DataContracts and from DataContracts to ViewModels?<br> <br> Thnaks for the post!</div> <div class="comment-date">2012-02-13 05:46 UTC</div> </div> <div class="comment" id="596aa54cbc8d4eb6a10fd98736bcb4e1"> <div class="comment-author"><a href="http://www.null-node.com">Dave Brue</a> <a href="#596aa54cbc8d4eb6a10fd98736bcb4e1">#</a></div> <div class="comment-content">I'm not certain the sample code you provided matches the first diagram... the diagram indicates that the Domain Layer only uses Track objects between the other layers. However you mention:<br> <br> &quot;... asks an ITrackService for the top tracks for the day and returns a list of <b>TopTrackViewModel</b> instances...&quot;<br> <br> The diagram really makes it look like the TrackService returns Track objects to the Controller and the Controller maps them into TopTrackViewModel objects. If that is the case... how would you communicate the Position and CssClass properties that the Domain Layer has computed between the two layers?<br> <br> Also, I noticed that ITrackService.GetTopFor(DateTime date) never uses the input parameter... should it really be more like ITrackService.GetTopForToday() instead?<br> <br> </div> <div class="comment-date">2012-02-13 19:10 UTC</div> </div> <div class="comment" id="f14590d0fb7f4aca9f196f4dd83cf7c4"> <div class="comment-author"><a href="http://www.dotnetcodegeeks.com/">Ilias Tsagklis</a> <a href="#f14590d0fb7f4aca9f196f4dd83cf7c4">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Awesome blog! Is there an email address I can contact you in private?</div> <div class="comment-date">2012-02-18 14:29 UTC</div> </div> <div class="comment" id="a8fadfa3bd3942018fe1f9af7c8220dd"> <div class="comment-author">Moss <a href="#a8fadfa3bd3942018fe1f9af7c8220dd">#</a></div> <div class="comment-content">What about using your domain objects in the data access layer because you're not using an ORM and doing things manually, so that at least one level of mapping can be done away with? Or is that something that would command the question, why would you do a silly thing like that (not using an ORM)?</div> <div class="comment-date">2012-02-18 17:03 UTC</div> </div> <div class="comment" id="bfc429f292254c96b6a2c858813483a1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bfc429f292254c96b6a2c858813483a1">#</a></div> <div class="comment-content">Anders, I've been down the road of modeling Entities or Value Objects as interfaces, and it rarely leads to anywhere productive. The problem is that you end up with a lot of interfaces that mainly consists of properties (instead of methods). One day I'll attempt to write a long and formal exposition on why this is problematic, but for now I'll just hint that it violates the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a> and leads to interface coupling as I quoted Jim Cooper for explaining in my <a href="/2012/02/02/LooseCouplingandtheBigPicture">previous blog post</a>. Basically the problem is that such interfaces tend to be <a href="http://martinfowler.com/bliki/HeaderInterface.html">Header Interfaces</a>.</div> <div class="comment-date">2012-02-20 00:23 UTC</div> </div> <div class="comment" id="71125987df924fc980ca52b70f82feaf"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#71125987df924fc980ca52b70f82feaf">#</a></div> <div class="comment-content">Daniel, it's really hard to answer that question without knowing <em>why</em> that web service exists in the first place. If the only reason it exists is to facilitate communication between client and server, you may not need a translation layer. That's basically how WCF RIA services work. However, that tightly couples the client to the service, and you can't deploy or version each independently of the other.<br> <br> If the service layer exists in order to decouple the service from the client, you <em>must</em> translate from the internal model to a service API. However, if this is the case, keep in mind that <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">at the service boundary, you are no longer dealing with <em>objects</em> at all</a>, so translation or mapping is conceptually very important. If you have .NET on both sides it's easy to lose sight of this, but imagine that either service or client is implemented in COBOL and you may begin to understand the importance of translation.<br> <br> So as always, it's a question of evaluating the advantages and disadvantages of each alternative in the concrete situation. There's no one correct answer.</div> <div class="comment-date">2012-02-20 00:37 UTC</div> </div> <div class="comment" id="a7fd9107ecc54cf88d31214b99dfb678"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a7fd9107ecc54cf88d31214b99dfb678">#</a></div> <div class="comment-content">Dave, well, yes, you are correct that the service in the code is actually part of the UI API. An error on my part, but I hope I still got the point of the blog post across. It doesn't change the conclusion in any way.</div> <div class="comment-date">2012-02-20 00:40 UTC</div> </div> <div class="comment" id="6293fd720afe4f2b91b45eb1fa715d72"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6293fd720afe4f2b91b45eb1fa715d72">#</a></div> <div class="comment-content">Ilias, you can write me at mark at seemann dot (my initials).</div> <div class="comment-date">2012-02-20 00:42 UTC</div> </div> <div class="comment" id="ec304d7794ce489aa9f0bee4cac0a27d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ec304d7794ce489aa9f0bee4cac0a27d">#</a></div> <div class="comment-content">Moss, that's one of the reason I find NoSQL (particularly Event Sourcing) very attractive. There need be no mapping at the data access level - only serialization.<br> <br> Personally, I'm beginning to consider ORMs the right solution to the wrong problem. A relational database is rarely the correct data store for a LoB application or your typical web site.</div> <div class="comment-date">2012-02-20 00:46 UTC</div> </div> <div class="comment" id="8f5f6cacce9443778006478d4e5da98f"> <div class="comment-author"><a href="http://flexdiary.blogspot.com">Amy</a> <a href="#8f5f6cacce9443778006478d4e5da98f">#</a></div> <div class="comment-content">Hi, Mark;<br> <br> Pardon me if I've gotten this wrong, because I'm approaching your post as an actionscript developer who is entirely self-taught. I don't know C#, but I like to read posts on good design from whatever language.<br> <br> How I might resolve this problem is to create a TrackDisplayModel Class that wraps the Track Class and provides the &quot;enriched&quot; methods of the position and cssClass. In AS, we have something called a Proxy Class that allows us to &quot;repeat&quot; methods on an inner object without writing a lot of code, so you could pass through the properties of the Track by making TrackDisplayModel extend Proxy. By doing this, I'd give up code completion and type safety, so I might not find that long term small cost worth the occasional larger cost of adding a field in more conventional ways.<br> <br> Does C# have a Class that is like Proxy that could be used in this way?</div> <div class="comment-date">2012-02-24 15:23 UTC</div> </div> <div class="comment" id="683aa836c8894b5cb1df473a87191c31"> <div class="comment-author"><a href="http://www.geekswithblogs.net/jboyer">Justin</a> <a href="#683aa836c8894b5cb1df473a87191c31">#</a></div> <div class="comment-content">Excellent post. I really enjoyed reading it and it brings up a good point. Development is a world of trade-offs. Could this issue be helped with document databases such as RavenDB? You could conceivably create a JSON string that could be passed through all layers since the DB understands it intrinsically. Not sure of mapping implications but maybe some relief could be had for some applications. Unfortunately, many applications just aren't the right kinds for document databases as the technology still needs to mature some. Might not work for straight CRUD apps but I don't want to get into a document DB/RDBMS debate. Just a thought.</div> <div class="comment-date">2012-03-02 15:22 UTC</div> </div> <div class="comment" id="5fa64924c7b345d88258420682b838d1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5fa64924c7b345d88258420682b838d1">#</a></div> <div class="comment-content">Amy, while I think you're describing a dynamic approach with that Proxy class, it sounds actually rather close to normal inheritance to me. Basically, in C#, when you inherit from another class, all the base class' members are carried forward into the derived class. From C# 4 and onward, we also have dynamic typing, which would enable us to forego compilation and type safety for dynamic dispatching.<br> <br> No matter how you'd achieve such things, I'm not to happy about doing this, as most of the variability axes we get from a multi-paradigmatic language such as C# is additive - it means that while it's fairly easy to <em>add</em> new capabilities to objects, it's actually rather hard to <em>remove</em> capabilities.<br> <br> When we map from e.g. a domain model to a UI model, we should consider that we must be able to do both: add as well as subtract behavior. There may be members in our domain model we don't want the UI model to use.<br> <br> Also, when the application enables the user to write as well as read data, we must be able to map both ways, so this is another reason why we must be able to add as well as remove members when traversing layers.<br> <br> I'm not saying that a dynamic approach isn't viable - in fact, I find it attractive in various scenarios. I'm just saying that simply exposing a lower-level API to upper layers by adding more members to it seems to me to go against the spirit of the Single Responsibility Principle. It would just be tight coupling on the semantic level.</div> <div class="comment-date">2012-03-12 20:41 UTC</div> </div> <div class="comment" id="977391b39ed345d38b6a90f562e299f3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#977391b39ed345d38b6a90f562e299f3">#</a></div> <div class="comment-content">Justin, if you pass JSON or any other type of untyped data up and down the layers, it would certainly remove the class coupling. The question is whether or not it would remove coupling in general. If you take a look at the <a href="/2012/02/02/LooseCouplingandtheBigPicture">Jim Cooper quote here</a>, he's talking about semantic coupling, and you'd still have that because each layer would be coupled to a specific structure of the data that travels up and down.<br> <br> You might also want to refer to my answer to Amy immediately above. The data structures the UI requires may be entirely different from the Domain Model, which again could be different from the data access model. The UI will often be an aggregate of data from many different data sources.<br> <br> In fact, I'd consider it a code smell if you could get by with the same data structure in all layers. If one can do that, one is most likely building a CRUD application, in which case a monolithic RAD application might actually be a better architectural choice.</div> <div class="comment-date">2012-03-12 20:48 UTC</div> </div> <div class="comment" id="43f27bd2f29c4e27883ac5aebcdef184"> <div class="comment-author">Matt <a href="#43f27bd2f29c4e27883ac5aebcdef184">#</a></div> <div class="comment-content">Mark,<br> <br> You've mentioned in your blogs (here and elsewhere) and on <a href="http://stackoverflow.com/a/9220361/452274">stackoverflow</a> simliar sentiments to your last comment:<br> <br> &lt;blockquote&gt;<br> I'd consider it a code smell if you could get by with the same data structure in all layers. If one can do that, one is most likely building a <b>CRUD</b> application, in which case a monolithic RAD application might actually be a better architectural choice.<br> &lt;/blockquote&gt;<br> <br> Most line of business (LoB) apps consist of CRUD operations. As such, when does an LoB app cross the line from being strictly &quot;CRUDy&quot; and jumps into the realm where where layering (or using CQRS) is preferable? Could you expand on your definition of <i>building a CRUD application</i>?<br> </div> <div class="comment-date">2012-04-16 16:31 UTC</div> </div> <div class="comment" id="70f6c8074f184f1caeff699ed3916515"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#70f6c8074f184f1caeff699ed3916515">#</a></div> <div class="comment-content">Matt, I don't think there's any hard rule for that. Consider all the unknowns that are before you when you embark on a new project: this is one of those unknowns. In part, I base such decisions on gut feeling - you may call it experience...<br> <br> What I think would be the correct thing, on the other hand, would be to make a conscious decision <i>as a team</i> on which way to go. If the team agrees that the project is most likely a CRUD application, then it can save itself the trouble of having all those layers. However, it's very important to stress to non-technical team members and stake holders (project managers etc.) that there's a trade-off here: <i>We've decided to go with RAD development. This will enables us to go fast as long as our assumption about a minimum of business logic holds true. On the other hand, if that assumption fails, we'll be going very slowly.</i> Put it in writing and make sure those people understand this.<br> <br> You can also put it another way: creating a correctly layered application is slow, but relatively safe. Assuming that you know what you're doing with layering (lots of people don't), developing a layered application removes much of that risk. Such an architecture is going to be able to handle a lot of the changes you'll throw at it, but it'll be slow going.<br> <br> HTH</div> <div class="comment-date">2012-04-16 19:02 UTC</div> </div> <div class="comment" id="9b8d6cdaae864e50807b319a1ae88981"> <div class="comment-author"><a href="http://www.sapiensworks.com/blog">Mike</a> <a href="#9b8d6cdaae864e50807b319a1ae88981">#</a></div> <div class="comment-content">My 2 euro cents: First of all, Track is DTO as it has no behaviour. IF all the domain objects are like this, then you're pretty much building an interface for the db and in this case everything is quite clear (CRUD).<br> <br> If youre 'referring to the case where there is actually a domain to model, then CQRS (the concept) is easily the answer. However, even in such app you'd have some parts which are CRUD. I simply like to use the appropriate tool for the job. For complex Aggregate Roots, Event Sourcing simplifies storage a lot. For 'dumb' objects, directly CRUD stuff. Of course, everything is done via a repository.<br> <br> Now, for complex domain I'd also use a message driven architecture so I'd have Domain events with handlers which will generate any read model required. I won't really have a mapping problem as event sourcing will solve it for complex objects while for simple objects at most I'll automap or use the domain entities directly .<br> </div> <div class="comment-date">2012-10-28 09:17 UTC</div> </div> <div class="comment" id="1f000bf1499844068c32edfa924df96a"> <div class="comment-author"><a href="http://www.ebenroux.co.za">Eben</a> <a href="#1f000bf1499844068c32edfa924df96a">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Nice blog!<br> <br> All the mapping stems from querying/reading the domain model. I have come to the conclusion that this is what leads to all kinds of problems such as:<br> <br> - lazy loading<br> - mapping<br> - fetching strategies<br> <br> The domain model has state but should *do* stuff. As you stated, the domain needs highly cohesive aggregates to get the object graph a short as possible. I do not use an ORM (I do my own mapping) so I do not have ORM specific classes.<br> <br> So I am 100% with you on the CQRS. There are different levels to doing CQRS and I think many folks shy away from it because they think there is only the all-or-nothing asynchronous messaging way to get to CQRS. Yet there are various approaches depending on the need, e.g.:-<br> <br> All within the same transaction scope with no messaging (100% consistent):<br> - store read data in same table (so different columns) &lt;-- probably the most typical option in this scenario<br> - store read data in separate table in the same database<br> - store read data in another table in another database<br> <br> Using eventual consistency via messaging / service bus (such as Shuttle - http://shuttle.codeplex.com/ | MassTransit | NServiceBus):<br> <br> - store read data in same table (so different columns)<br> - store read data in separate table in the same database<br> - store read data in another table in another database &lt;-- probably the most typical option in this scenario<br> <br> Using C# I would return the data from my query layer as a DataRow or DataTable and in the case of something more complex a DTO.<br> <br> Regards,<br> Eben</div> <div class="comment-date">2013-01-18 08:38 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Loose Coupling and the Big Picture https://blog.ploeh.dk/2012/02/02/LooseCouplingandtheBigPicture 2012-02-02T20:37:40+00:00 Mark Seemann <div id="post"> <p> A common criticism of loosely coupled code is that it's harder to understand. How do you see the big picture of an application when loose coupling is everywhere? When the entire code base has been programmed against interfaces instead of concrete classes, how do we understand how the objects are wired and how they interact? </p> <p> In this post, I'll provide answers on various levels, from high-level architecture over object-oriented principles to more nitty-gritty code. Before I do that, however, I'd like to pose a set of questions you should always be prepared to answer. </p> <h3 id="aae1c98a1e1141aca8570a665592b737"> Mu <a href="#aae1c98a1e1141aca8570a665592b737" title="permalink">#</a> </h3> <p> My first reaction to that sort of question is: <em>you say loosely coupled code is harder to understand. Harder than what?</em> </p> <p> If we are talking about a non-trivial application, odds are that it's going to take some time to understand the code base - whether or not it's loosely coupled. Agreed: understanding a loosely coupled code base takes some work, but so does understanding a tightly coupled code base. The question is whether it's <em>harder</em> to understand a loosely coupled code base? </p> <p> Imagine that I'm having a discussion about this subject with Mary Rowan from <a href="http://amzn.to/12p90MG">my book</a>. </p> <p> <strong>Mary:</strong> “Loosely coupled code is harder to understand.” </p> <p> <strong>Me:</strong> “Why do you think that is?” </p> <p> <strong>Mary:</strong> “It's very hard to navigate the code base because I always end up at an interface.” </p> <p> <strong>Me:</strong> “Why is that a problem?” </p> <p> <strong>Mary:</strong> “Because I don't know what the interface <em>does</em>.” </p> <p> At this point I'm very tempted to answer <a href="http://en.wikipedia.org/wiki/Mu_%28negative%29">Mu</a>. An interfaces doesn't <em>do</em> anything - that's the whole point of it. According to the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> (LSP), a consumer shouldn't have to care about what happens on the other side of the interface. </p> <p> However, developers used to tightly coupled code aren't used to think about services in this way. They are used to navigate the code base from consumer to service to understand how the two of them interact, and I will gladly admit this: in principle, that's <em>impossible</em> to do in a loosely coupled code base. I'll return to this subject in a little while, but first I want to discuss some strategies for understanding a loosely coupled code base. </p> <h3 id="315bc05f3ce94a71a69e2e855f458ff2"> Architecture and Documentation <a href="#315bc05f3ce94a71a69e2e855f458ff2" title="permalink">#</a> </h3> <p> Yes: documentation. Don't dismiss it. While I agree with Uncle Bob and like-minded spirits that the code <em>is</em> the documentation, a two-page document that outlines the Big Picture might save you from many hours of code archeology. </p> <p> The typical agile mindset is to minimize documentation because it tends to lose touch with the code base, but even so, it should be possible to maintain a two-page high-level document so that it stays up to date. Consider the alternative: if you have so much architectural churn that even a two-page overview regularly falls behind, then you're probably having a greater problem than understanding your loosely coupled code base. </p> <p> Maintaining such a document isn't adverse to the agile spirit. You'll find the same advice in <a href="http://www.amazon.com/gp/product/0470684208/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0470684208">Lean Architecture</a> (p. 127). Don't underestimate the value of such a document. </p> <h3 id="5e734db398324e208110405afd444f31"> See the Forest Instead of the Trees <a href="#5e734db398324e208110405afd444f31" title="permalink">#</a> </h3> <p> Understanding a loosely coupled code base typically tends to require a shift of mindset. </p> <p> Recall my discussion with Mary Rowan. The criticism of loose coupling is that it's difficult to understand which collaborators are being invoked. A developer like Mary Rowan is used to learn a code base by understanding all the myriad concrete details of it. In effect, while there may be ‘classes' around, there are <em>no</em> abstractions in place. In order to understand the behavior of a user interface element, it's necessary to also understand what's happening in the database - and everything in between. </p> <p> A loosely coupled code base shouldn't be like that. </p> <blockquote> <p> The entire purpose of loose coupling is that we should be able to reason about a part (a ‘unit', if you will) without understanding the whole. </p> </blockquote> <p> In a tightly coupled code base, it's often impossible to see the forest for the trees. Although we developers are good at relating to details, a tightly coupled code base requires us to be able to contain <em>the entire</em> code base in our heads in order to understand it. As the size of the code base grows, this becomes increasingly difficult. </p> <p> In a loosely coupled code base, on the other hand, it should be possible to understand smaller parts in isolation. However, I purposely wrote “should be”, because that's not always the case. Often, a so-called “loosely coupled” code base violates basic principles of object-oriented design. </p> <h3 id="aef9463c43b947c785ccdbc0403a177c"> RAP <a href="#aef9463c43b947c785ccdbc0403a177c" title="permalink">#</a> </h3> <p> The criticism that it's hard to see “what's on the other side of an interface” is, in my opinion, central. It betrays a mindset which is still tightly coupled. </p> <p> In many code bases there's often a single implementation of a given interface, so developers can be forgiven if they think about an interface as only a piece of friction that prevents them from reaching the concrete class on the other side. However, if that's the case with most of the interfaces in a code base, it signifies a violation of the <a href="http://codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle</a> (RAP) more than it signifies loose coupling. </p> <p> Jim Cooper, a reader of my book, put it quite eloquently <a href="http://www.manning-sandbox.com/thread.jspa?threadID=48257">on the book's forum</a>: </p> <blockquote> <p> “So many people think that using an interface magically decouples their code. It doesn't. It only changes the nature of the coupling. If you don't believe that, try changing a method signature in an interface - none of the code containing method references or any of the implementing classes will compile. I call that coupled” </p> </blockquote> <p> Refactoring tools aside, I completely agree with this statement. The RAP is a test we can use to verify whether or not an interface is truly reusable - what better test is there than to actually reuse your interfaces? </p> <p> The corollary of this discussion is that if a code base is massively violating the RAP then it's going to be hard to understand. It has all the disadvantages of loose coupling with few of the benefits. If that's the case, you would gain more benefit from making it either more tightly coupled or truly loosely coupled. </p> <p> What does “truly loosely coupled” mean? </p> <h3 id="b08181f07dd745ec8477e39a03c23098"> LSP <a href="#b08181f07dd745ec8477e39a03c23098" title="permalink">#</a> </h3> <p> According to the LSP a consumer <em>must not</em> concern itself with “what's on the other side of the interface”. It should be possible to replace any implementation with any other implementation of the same interface without changing the correctness of the program. </p> <p> This is why I previously said that in a truly loosely coupled code base, it isn't ‘hard' to understand “what's on the other side of the interface” - it's <em>impossible</em>. At design-time, there's <em>nothing</em> ‘behind' the interface. The interface is what you are programming against. It's all there is. </p> <p> Mary has been listening to all of this, and now she protests: </p> <p> <strong>Mary:</strong> “At run-time, there's going to be a concrete class behind the interface.” </p> <p> <strong>Me</strong> (being annoyingly pedantic): “Not quite. There's going to be an instance of a concrete class which the consumer invokes via the interface it implements.” </p> <p> <strong>Mary:</strong> “Yes, but I still need to know which concrete class is going to be invoked.” </p> <p> <strong>Me:</strong> “Why?” </p> <p> <strong>Mary:</strong> “Because otherwise I don't know what's going to happen when I invoke the method.” </p> <p> This type of answer often betrays a much more fundamental problem in a code base. </p> <h3 id="c6edfa2f2f884e388ffd1c9b15db0f3b"> CQS <a href="#c6edfa2f2f884e388ffd1c9b15db0f3b" title="permalink">#</a> </h3> <p> Now we are getting into the nitty-gritty details of class design. What would you expect that the following method does? </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">List</font></span>&lt;<span style="color: "><font color="#2b91af">Order</font></span>&gt; GetOpenOrders(<span style="color: "><font color="#2b91af">Customer</font></span> customer)</font></pre> </p> <p> The method name indicates that it gets open orders, and the signature seems to back it up. A single database query might be involved, since this looks a lot like a read-operation. A quick glance at the implementation seems to verify that first impression: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">List</font></span>&lt;<span style="color: "><font color="#2b91af">Order</font></span>&gt; GetOpenOrders(<span style="color: "><font color="#2b91af">Customer</font></span> customer)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> orders = GetOrders(customer);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">from</font></span> o <span style="color: "><font color="#0000ff">in</font></span> orders</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">where</font></span> o.Status == <span style="color: "><font color="#2b91af">OrderStatus</font></span>.Open</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">select</font></span> o).ToList();</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Is it safe to assume that this is a side-effect-free method call? As it turns out, this is far from the case in this particular code base: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">List</font></span>&lt;<span style="color: "><font color="#2b91af">Order</font></span>&gt; GetOrders(<span style="color: "><font color="#2b91af">Customer</font></span> customer)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> gw = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">CustomerGateway</font></span>(<span style="color: "><font color="#0000ff">this</font></span>.ConnectionString);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> orders = gw.GetOrders(customer);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; AuditOrders(orders);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; FixCustomer(gw, orders, customer);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> orders;</font> <font style="font-size: 10pt">}</font></pre> </p> <p> The devil is in the details. What does AuditOrders do? And what does FixCustomer do? One method at a time:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">void</font></span> AuditOrders(<span style="color: "><font color="#2b91af">List</font></span>&lt;<span style="color: "><font color="#2b91af">Order</font></span>&gt; orders)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> user = <span style="color: "><font color="#2b91af">Thread</font></span>.CurrentPrincipal.Identity.ToString();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> gw = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">OrderGateway</font></span>(<span style="color: "><font color="#0000ff">this</font></span>.ConnectionString);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">foreach</font></span> (<span style="color: "><font color="#0000ff">var</font></span> o <span style="color: "><font color="#0000ff">in</font></span> orders)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> clone = o.Clone();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> ar = <span style="color: "><font color="#0000ff">new</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">AuditRecord</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Time = <span style="color: "><font color="#2b91af">DateTime</font></span>.Now,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; User = user</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; clone.AuditTrail.Add(ar);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gw.Update(clone);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#008000">// We don't want the consumer to see the audit trail.</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; o.AuditTrail.Clear();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> OK, it turns out that this method actually makes a <em>copy</em> of <em>each and every</em> order and updates that copy, writing it back to the database in order to leave behind an audit trail. It also <em>mutates</em> each order before returning to the caller. Not only does this method result in an unexpected N+1 problem, it also <em>mutates its input</em>, and perhaps even more surprising, it's leaving the system in a state where the in-memory object is different from the database. This could lead to some pretty interesting bugs. </p> <p> Then what happens in the FixCustomer method? This: </p> <p> <pre style="margin: 0px"><span style="color: "><font style="font-size: 10pt" color="#008000">// Fixes the customer status field if there were orders</font></span> <span style="color: "><font style="font-size: 10pt" color="#008000">// added directly through the database.</font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">static</font></span> <span style="color: "><font color="#0000ff">void</font></span> FixCustomer(<span style="color: "><font color="#2b91af">CustomerGateway</font></span> gw,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">List</font></span>&lt;<span style="color: "><font color="#2b91af">Order</font></span>&gt; orders, <span style="color: "><font color="#2b91af">Customer</font></span> customer)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> total = orders.Sum(o =&gt; o.Total);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (customer.Status != <span style="color: "><font color="#2b91af">CustomerStatus</font></span>.Preferred</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; total &gt; PreferredThreshold)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; customer.Status = <span style="color: "><font color="#2b91af">CustomerStatus</font></span>.Preferred;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gw.Update(customer);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Another potential database write operation, as it turns out - complete with <a href="http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode">an apology</a>. Now that we've learned all about the details of the code, even the GetOpenOrders method is beginning to look suspect. The GetOrders method returns all orders, with the side effect that <em>all</em> orders were audited as having been read by the user, but the GetOpenOrders filters the output. In the end, it turns out that we can't even trust the audit trail. </p> <p> While I must apologize for this contrived example of a <a href="http://martinfowler.com/eaaCatalog/transactionScript.html">Transaction Script</a>, it's clear that when code looks like that, it's no wonder why developers think that it's necessary to contain the entire code base in their heads. When this is the case, interfaces are only in the way. </p> <p> However, this is not the fault of loose coupling, but rather a failure to adhere to the very fundamental principle of <a href="http://en.wikipedia.org/wiki/Command-query_separation">Command-Query Separation</a> (CQS). You should be able to tell from the method signature alone whether invoking the method will or will not have side-effects. This is one of the key messages from <a href="http://www.amazon.com/gp/product/0132350882/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code</a>: the method name and signature is an abstraction. You should be able to reason about the behavior of the method from its declaration. You shouldn't have to read the code to get an idea about what it does. </p> <blockquote> <p> Abstractions always hide details. Method declarations do too. The point is that you should be able to read just the method declaration in order to gain a basic understanding of what's going on. You can always return to the method's code later in order to understand detail, but reading the method declaration alone should provide the Big Picture. </p> </blockquote> <p> Strictly adhering to CQS goes a long way in enabling you to understand a loosely coupled code base. If you can reason about methods at a high level, you don't need to see “the other side of the interface” in order to understand the Big Picture. </p> <h3 id="3f5f04005d904107833d20390496dd64"> Stack Traces <a href="#3f5f04005d904107833d20390496dd64" title="permalink">#</a> </h3> <p> Still, even in a loosely coupled code base with great test coverage, integration issues arise. While each class works fine in isolation, when you integrate them, sometimes the interactions between them cause errors. This is often because of incorrect assumptions about the collaborators, which often indicates that the LSP was somehow violated. </p> <p> To understand why such errors occur, we need to understand which concrete classes are interacting. How do we do that in a loosely coupled code base? </p> <p> That's actually easy: look at the stack trace from your error report. If your error report doesn't include a stack trace, make sure that it's going to do that in the future. </p> <blockquote> <p> The stack trace is one of the most important troubleshooting tools in a loosely coupled code base, because it's going to tell you exactly which classes were interacting when an exception was thrown. </p> </blockquote> <p> Furthermore, if the code base also adheres to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> and the ideals from Clean Code, each method should be very small (under 15 lines of code). If that's the case, you can often understand the exact nature of the error from the stack trace and the error message alone. It shouldn't even be necessary to attach a debugger to understand the bug, but in a pinch, you can still do that. </p> <h3 id="0e694c07391944f2a4ba9c6f5d0201b9"> Tooling <a href="#0e694c07391944f2a4ba9c6f5d0201b9" title="permalink">#</a> </h3> <p> Returning to the original question, I often hear people advocating tools such as IDE add-ins which support navigation across interfaces. Such tools might provide a menu option which enables you to “go to implementation”. At this point it should be clear that such a tool is mainly going to be helpful in code bases that violate the RAP. </p> <p> (I'm not naming any particular tools here because I'm not interested in turning this post into a discussion about the merits of various specific tools.) </p> <h3 id="20b2d20f2c9d439b9bfefb761a64be5d"> Conclusion <a href="#20b2d20f2c9d439b9bfefb761a64be5d" title="permalink">#</a> </h3> <p> It's the responsibility of the loosely coupled code base to make sure that it's easy to understand the Big Picture and that it's easy to work with. In the end, that responsibility falls on the developers who <em>write</em> the code - not the developer who's trying to understand it. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e0302e4085c64bf5a2a54d4d837a15ad"> <div class="comment-author">Thomas <a href="#e0302e4085c64bf5a2a54d4d837a15ad">#</a></div> <div class="comment-content">Mark, another great blog post. What do you think about inroducing interfaces to enable unit testing. I we take into the consideration only the production code we could have a 1:1 mapping between interfaces and abstractions and thus violating the RAP principle. But from the point of view of unit testing it's very handy. Do you think that unit testing justify the RAP violation ?<br> <br> Thomas</div> <div class="comment-date">2012-02-02 22:56 UTC</div> </div> <div class="comment" id="da7eb398cc58473c8b8ed91051b53844"> <div class="comment-author">Thomas <a href="#da7eb398cc58473c8b8ed91051b53844">#</a></div> <div class="comment-content">We could also ask ourselves &quot;Why do we use abstraction in our code?&quot; Abstraction is a mean to isolate implementations like method bodies, so when we're implementing a MethodA which is dependend on some interface, from the MethodA perspective, we don't care about the implementation of that interface. In practice, this works pretty well and abstractions bring welcomed flexibility to make our code more refactorable, which leads to more maintainable program. The key is that if you need to know what implementation resides behind an abstraction, you are in trouble: not only there might be multiple different implementations but also, some of these implementations might be created or refactored in the future.</div> <div class="comment-date">2012-02-02 23:08 UTC</div> </div> <div class="comment" id="a366c7b2a3954e70bc2a9143c901ec40"> <div class="comment-author"><a href="http://javiercrespoalvez.com">Javi</a> <a href="#a366c7b2a3954e70bc2a9143c901ec40">#</a></div> <div class="comment-content">If you introduce interfaces to enable testability then you are not breaking the RAP IMHO. You can't really say that you have a 1:1 mapping as both the production code and your unit tests are consumers of your API. <br> <br> You can argue that the most important consumer is the production code and I definitely agree with you! But let's not underestimate the role and the importance of the automated test as this is the only consumer that should drive the design of your API.</div> <div class="comment-date">2012-02-03 16:30 UTC</div> </div> <div class="comment" id="1ee57b6b05194f4286485de84645a2f9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1ee57b6b05194f4286485de84645a2f9">#</a></div> <div class="comment-content">Thomas, Javi<br> <br> I'm a big proponent of the concept that, with TDD, unit tests are actually the <em>first</em> consumer of your code, and the final application only a close second. As such, it may seem compelling to state that you're never breaking the RAP if you do TDD. However, as a knee-jerk reaction, I just don't buy that argument, which made me think why that is the case...<br> <br> I haven't thought this completely through yet, but I think many (not all) unit tests pose a special case. A very specialized <a href="http://xunitpatterns.com/Test%20Double.html">Test Double</a> isn't really a piece of reuse as much as it's a simulation of the production environment. Add into this mix any dynamic mock library, and you have a tool where you can <em>simulate</em> anything.<br> <br> However, simulation isn't the same as reuse.<br> <br> I think a better test would be if you can write a robust and maintainable <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a>. If you can do that, the abstraction is most likely reuseable. If not, it probably isn't.</div> <div class="comment-date">2012-02-03 19:08 UTC</div> </div> <div class="comment" id="6c2590ac228a454981fa9fab8831e5b6"> <div class="comment-author">Thomas <a href="#6c2590ac228a454981fa9fab8831e5b6">#</a></div> <div class="comment-content">You're very exigent Mark ;) Puting aside TDD and Unit testing. How would you achieve loosely coupling between assemblies without an interface ? For example an MVC application, I prefer to be dependant on ISmthRepository in my controller than to reference the implementation of ISmthRepository which could be SqlSmthRepository. Even if I know that there will be only 1:1 implementation ?<br> <br> Thanks,<br> <br> Thomas</div> <div class="comment-date">2012-02-05 09:33 UTC</div> </div> <div class="comment" id="36d6d141057e43b7b3bbe48853ad67d4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#36d6d141057e43b7b3bbe48853ad67d4">#</a></div> <div class="comment-content">I'm not saying that you can't program to interfaces, but I'm saying that if you can't reuse those interfaces, it's time to take a good hard look at them. So if you know there's only ever going to be one implementation of ISmthRepository, what does that tell you about that interface?<br> <br> In any case, please refer back to the <a href="http://codemanship.co.uk/parlezuml/blog/?postid=934">original definition of the RAP</a>. It doesn't say that you aren't allowed to program against 1:1 interfaces - it just states that as long as it remains a 1:1 interface, you haven't proved that it's a generalization. Until that happens, it should be treated as suspect.<br> <br> However, the RAP states that we should discover generalizations <em>after the fact</em>, which implies that we'd always have to go through a stage where we have 1:1 interfaces. As part of the Refactoring step of Red/Green/Refactor, it should be our responsibility to merge interfaces, just as it's our responsibility to remove code duplication.</div> <div class="comment-date">2012-02-05 10:36 UTC</div> </div> <div class="comment" id="1f2e63afbf8240289dd04d1335d5c351"> <div class="comment-author">Justin <a href="#1f2e63afbf8240289dd04d1335d5c351">#</a></div> <div class="comment-content">I would like some clarification as some of these principles seem contradictory in nature. Allow me to explain:<br> 1. RAP says that using an interface that has only one implementation is unnecessary.<br> 2. Dependency inversion principle states that both client and service should depend on an abstraction.<br> 3. Tight coupling is discouraged and often defined as depending on a class (i.e. &quot;newing&quot; up a class for use).<br> <br> So in order to depend on an abstraction (I understand that &quot;abstraction&quot; does not necessarily mean interface all of the time), you need to create an interface and program to it. If you create an interface for this purpose but it only has one implementation than it is suspect under RAP. I understand also that RAP also refers to pointless interfaces such as &quot;IPerson&quot; that has a Person class as an implementation or &quot;IProduct&quot; that has one Product implementation.<br> <br> But how about a controller that needs a business service or a business service that needs a data service? I find it easier to build a business service in isolation from a data service or controller so I create an interface for the data services I need but don't create implementations. Then I just mock the interfaces to make sure that my business service works through unit testing. Then I move on to the next layer, making sure that the services then implement the interface defined by the business layer. Thoughts? Opinions?</div> <div class="comment-date">2012-02-05 19:11 UTC</div> </div> <div class="comment" id="486a59b0bbad46fbbfc5841f7b56878b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#486a59b0bbad46fbbfc5841f7b56878b">#</a></div> <div class="comment-content">Justin<br> <br> Remember that with TDD we should move through the three phases of Red, Green and Refactor. During the Refactoring phase we typically look towards eliminating duplication. We are (or at least should be) used to do this for code duplication. The only thing the RAP states is that we should extend that effort towards our interface designs.<br> <br> Please also refer to the other comment on this page.<br> <br> HTH</div> <div class="comment-date">2012-02-05 19:38 UTC</div> </div> <div class="comment" id="9ff5535b03974e048028337881cf47cb"> <div class="comment-author"><a href="http://simpleprogrammer.com">John Sonmez</a> <a href="#9ff5535b03974e048028337881cf47cb">#</a></div> <div class="comment-content">First of all, great post!<br> <br> I think one thing to consider in the whole loose coupling debate is granularity.<br> Not too many people talk about granularity, and without that aspect, I think it is impossible to really have enough context to say whether something is too tightly or loosely coupled. I wrote about the idea here: http://simpleprogrammer.com/2010/11/09/back-to-basics-cohesion-and-coupling-part-2/<br> <br> Essentially what I am saying is that some things should be coupled. We don't want to create unneeded abstractions, because they introduce complexity. The example I use is Enterprise FizzBuzz. At the same time, we should be striving to build the seams at the points of change which should align in a well designed system with responsibility.<br> <br> This is definitely a great topic though. Could talk about it all day.</div> <div class="comment-date">2012-03-02 14:44 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. SOLID is Append-only https://blog.ploeh.dk/2012/01/03/SOLIDisAppend-only 2012-01-03T14:43:47+00:00 Mark Seemann <div id="post"> <p> <a href="http://en.wikipedia.org/wiki/Solid_%28object-oriented_design%29">SOLID</a> is a set of principles that, if applied consistently, has some surprising effect on code. In a <a href="/2011/06/07/SOLIDCodeisnt">previous post</a> I provided a sketch of what it means to meticulously apply the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>. In this article I will describe what happens when you follow the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a> (OCP) to its logical conclusion. </p> <p> In case a refresher is required, the OCP states that a class should be open for extension, but <em>closed for modification</em>. It seems to me that people often forget the second part. What does it mean? </p> <p> It means that once implemented, you <em>shouldn't touch that piece of code ever again</em> (unless you need to correct a bug). </p> <p> Then how can new functionality be added to a code base? This is still possible through either inheritance or polymorphic recomposition. Since the L in SOLID signifies the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>, SOLID code tends to be based on loosely coupled code <em>composed</em> into an application through copious use of interfaces - basically, <a href="http://en.wikipedia.org/wiki/Strategy_pattern">Strategies</a> injected into other Strategies and so on (also due to <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>). In order to add functionality, you can create new implementations of these interfaces and redefine the application's <a href="/2011/07/28/CompositionRoot">Composition Root</a>. Perhaps you'd be wrapping existing functionality in a <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> or adding it to a <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composite</a>. </p> <p> Once in a while, you'll stop using an old implementation of an interface. Should you then delete this implementation? What would be the point? At a certain point in time, this implementation was valuable. Maybe it will become valuable again. Leaving it as an potential building block seems a better choice. </p> <p> Thus, if we think about working with code as a CRUD endeavor, SOLID code can be Created and Read, but never Updated or Deleted. In other words, true SOLID code is append-only code. </p> <h3 id="3c596a99eaf24b7c9165c5e577b4880a"> Example: Changing AutoFixture's Number Generation Algorithm <a href="#3c596a99eaf24b7c9165c5e577b4880a" title="permalink">#</a> </h3> <p> In early 2011 <a href="http://autofixture.codeplex.com/workitem/4223">an issue was reported</a> for <a href="http://autofixture.codeplex.com/">AutoFixture</a>: Anonymous numbers were created in monotonically increasing sequences, but with separate sequences for each number type: </p> <p> integers: 1, 2, 3, 4, 5, … </p> <p> decimals: 1.0, 2.0, 3.0, 4.0, 5.0, … </p> <p> and so on. However, the person reporting the issue thought it made more sense if all numbers shared a single sequence. After thinking about it a little while, I agreed. </p> <p> Because the AutoFixture code base is fairly SOLID we decided to leave the old implementations in place and implement the new behavior in new classes. </p> <p> The old behavior was composed from a set of ISpecimenBuilders. As an example, integers were generated by this class: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">Int32SequenceGenerator</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">ISpecimenBuilder</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">int</font></span> i;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">int</font></span> CreateAnonymous()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#2b91af">Interlocked</font></span>.Increment(<span style="color: "><font color="#0000ff">ref</font></span> <span style="color: "><font color="#0000ff">this</font></span>.i);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">object</font></span> Create(<span style="color: "><font color="#0000ff">object</font></span> request,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">ISpecimenContext</font></span> context)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (request != <span style="color: "><font color="#0000ff">typeof</font></span>(<span style="color: "><font color="#0000ff">int</font></span>))</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.CreateAnonymous();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Similar implementations generated decimals, floats, doubles, etc. Instead of modifying any of these classes, we left them in the code base and created a new ISpecimenBuilder that generates all numbers from a single sequence: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">NumericSequenceGenerator</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">ISpecimenBuilder</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">int</font></span> value;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">object</font></span> Create(<span style="color: "><font color="#0000ff">object</font></span> request,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">ISpecimenContext</font></span> context)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> type = request <span style="color: "><font color="#0000ff">as</font></span> <span style="color: "><font color="#2b91af">Type</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (type == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.CreateNumericSpecimen(type);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">object</font></span> CreateNumericSpecimen(<span style="color: "><font color="#2b91af">Type</font></span> request)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> typeCode = <span style="color: "><font color="#2b91af">Type</font></span>.GetTypeCode(request);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">switch</font></span> (typeCode)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.Byte:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">byte</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.Decimal:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">decimal</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.Double:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">double</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.Int16:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">short</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.Int32:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.Int64:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">long</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.SByte:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">sbyte</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.Single:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">float</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.UInt16:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">ushort</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.UInt32:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">uint</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> <span style="color: "><font color="#2b91af">TypeCode</font></span>.UInt64:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#0000ff">ulong</font></span>)<span style="color: "><font color="#0000ff">this</font></span>.GetNextNumber();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">default</font></span>:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">int</font></span> GetNextNumber()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#2b91af">Interlocked</font></span>.Increment(<span style="color: "><font color="#0000ff">ref</font></span> <span style="color: "><font color="#0000ff">this</font></span>.value);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Adding a new class in itself has no effect, so in order to recompose the default behavior of AutoFixture, we changed a class called DefaultPrimitiveBuilders by removing the old ISpecimenBuilders like Int32SequenceGenerator and instead adding NumericSequenceGenerator: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">yield</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">StringGenerator</font></span>(() =&gt; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Guid</font></span>.NewGuid());</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">yield</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ConstrainedStringGenerator</font></span>();</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">yield</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">StringSeedRelay</font></span>();</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">yield</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">NumericSequenceGenerator</font></span>();</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">yield</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">CharSequenceGenerator</font></span>();</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">yield</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">RangedNumberGenerator</font></span>();</font> <span style="color: "><font style="font-size: 10pt" color="#008000">// even more builders...</font></span></pre> </p> <p> NumericSequenceGenerator is the fourth class being yielded here. Before we added NumericSequenceGenerator, this class instead yielded Int32SequenceGenerator and similar classes. These were removed. </p> <p> The DefaultPrimitiveBuilders class is part of AutoFixture's default <a href="http://en.wikipedia.org/wiki/Facade_pattern">Facade</a> and is the closest we get to a Composition Root for the library. Recomposing this Facade enabled us to change the behavior of AutoFixture without modifying (other) existing classes. </p> <p> As <a href="http://megakemp.wordpress.com/">Enrico</a> (who implemented this change) points out, the beauty is that the <a href="http://megakemp.wordpress.com/2011/09/06/behavior-changes-in-autofixture-2-2-anonymous-numbers/">previous behavior is still in the box</a>, and all it takes is a single method call to bring it back: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>().Customize(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">NumericSequencePerTypeCustomization</font></span>());</font></pre> </p> <p> The only class we had to modify was the DefaultPrimitiveBuilders, which is where the object graph is composed. In applications this corresponds to the Composition Root, so even in the face of SOLID code, you still need to modify the Composition Root in order to recompose the application. However, use of a good DI Container and a strong set of conventions can do much to minimize the required editing of such a class. </p> <h3 id="973b689af072414a8d84a51989817096"> SOLID versus Refactoring <a href="#973b689af072414a8d84a51989817096" title="permalink">#</a> </h3> <p> SOLID is a goal I strive towards in the way I write code and design APIs, but I don't think I've ever written a significant code base which is perfectly SOLID. While I consider AutoFixture a ‘fairly' SOLID code base, it's not perfect, and I'm currently performing some design work in order to change some abstractions for version 3.0. This will require changing some of the existing types and thereby violating the OCP. </p> <p> It's worth noting that as long as you can stick with the OCP you can avoid introducing breaking changes. A breaking change is also an OCP violation, so adhering to the OCP is more than just an academic exercise - particularly if you write reusable libraries. </p> <p> Still, while none of my code is perfect and I occasionally have to refactor, I don't refactor much. By definition, refactoring means violating the OCP, and while I have nothing against refactoring code when it's required, I much prefer putting myself in a situation where it's rarely necessary in the first place. </p> <p> I've often been derided for my lack of use of <a href="http://www.jetbrains.com/resharper/">Resharper</a>. When replying that I have little use for Resharper because I write SOLID code and thus don't do much refactoring, I've been ridiculed for being totally clueless. People don't realize the intimate relationship between SOLID and refactoring. I hope this post has helped highlight that connection. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5a153192a94540878512e56ec09918a1"> <div class="comment-author">Jon Wingfield <a href="#5a153192a94540878512e56ec09918a1">#</a></div> <div class="comment-content">I've found that it's very difficult to accomplish OCP in practice, mostly because of evolutionary design. An example is having the foresight to know when the cohesion/coupling barrier has been crossed. Also, when one gains greater insight into a domain, refactoring is necessary (even crucial). but this violates OCP as well. I find that my designs are pretty crappy up front but evolve as patterns emerge. I prefer this to up-front engineering (except in some cases), because it yields a code base that is cohesive when appropriate, but also decoupled when appropriate.</div> <div class="comment-date">2012-01-03 16:09 UTC</div> </div> <div class="comment" id="e1509ca9cb804ba3a4769f426d676ed4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e1509ca9cb804ba3a4769f426d676ed4">#</a></div> <div class="comment-content">Agreed, OCP is <em>hard</em>.<br> <br> However, adhering to OCP doesn't indicate that you have to do BDUF. What it means is that once you get a 'rush of insight' (as <a href="http://www.amazon.com/gp/product/0321125215/ref=as_li_ss_tl?ie=UTF8&tag=ploeh-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0321125215">Domain-Driven Design</a> puts it) you don't <em>modify</em> existing classes. Instead, you introduce <em>new</em> classes to follow the new model.<br> <br> This may seem wasteful, but due to the <a href="/2011/06/07/SOLIDCodeisnt">very fine-grained nature</a> of SOLID code, it means that those classes that follow the old model (that you've just realized can be improved) are basically 'wrong' because they model the domain in the 'wrong' way. Re-implementing that part of the application's behavior while leaving the old code in place is typically more efficient because it's only going to be a very small part of the code base (again due to the granularity) and because you can do it in micro-iterations since you're not changing anything. Thus, dangerous 'big-bang' refactorings are avoided.<br> <br> In any case, I never said that SOLID is easy. What I'm saying is that SOLID code has certain characteristics, and if a code base doesn't exhibit those characteristics, it's not (perfectly) SOLID. In reality, I expect that there are very few code bases that can live up to what is essentially an <em>ideal</em> more than a practically attainable goal.</div> <div class="comment-date">2012-01-03 16:42 UTC</div> </div> <div class="comment" id="16b7797502894573b1ef56e3f387ab80"> <div class="comment-author">Jon Wingfield <a href="#16b7797502894573b1ef56e3f387ab80">#</a></div> <div class="comment-content">Not trying to spam your blog, but maybe OCP is better viewed as a means for enhancement, whereas refactoring is intended to improve the existing design. Sometimes when adding functionality, we realize that the existing design doesn't accomodate change very well, and thus we refactor (hopefully separately from implementing new functionality). I'm still a little uneasy about applying OCP when fixing bugs and especially design issues (which you already alleviated to).</div> <div class="comment-date">2012-01-03 17:33 UTC</div> </div> <div class="comment" id="895da9654bb543d49b587931fd7ada25"> <div class="comment-author"><a href="http://hadihariri.com">Hadi Hariri</a> <a href="#895da9654bb543d49b587931fd7ada25">#</a></div> <div class="comment-content">Mark,<br> <br> Irrelevant of my association with ReSharper, I think that refactoring (be it with or without tools) and SOLID design are not mutually exclusive. You are basing your argument mostly on the premise that we get things right the first and that is not always the case. Test Driven Development in itself for instance is about evolving design. <br> <br> As for ReSharper not being needed (or replace ReSharper with any other enhancing tool), I find it kind of amusing because it seems that there is some imaginary line that developers draw whereby what's included in Visual Studio is sufficient when it comes to refactoring. Everything else is superflous. That is of course until the next version of Visual Studio includes it. And that's if we think about these types of tools as refactoring only, which is so far from the truth. <br> <br> Btw, switch statement violates OCP and yes it doesn't change until it does change. I'd add that normally when I violate OCP I try and make sure the tests are in pace to let me know if something breaks. <br> </div> <div class="comment-date">2012-01-03 17:55 UTC</div> </div> <div class="comment" id="7c707889b3424d4592db82f1f2cafc8b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7c707889b3424d4592db82f1f2cafc8b">#</a></div> <div class="comment-content">Jon, that's well put.<br> <br> When it comes to fixing bugs, the OCP specifically states that it's OK to modify existing code, so you shouldn't be uneasy about that.</div> <div class="comment-date">2012-01-03 19:32 UTC</div> </div> <div class="comment" id="2ed4b4560bdd49398352c0cb2a8f0cee"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2ed4b4560bdd49398352c0cb2a8f0cee">#</a></div> <div class="comment-content">Hadi, I'd never claim that it's possible to get things right on the first attempt. Looking at AutoFixture (again), I had to do a lot of refactoring and redesign to arrive at version 2.0, which is 'fairly' SOLID. Still, I have more changes in store for version 3.0, which indicates to me that it's <em>still</em> not SOLID - although it's vastly better than before.<br> <br> Still, instead of refactoring, sometimes it makes more sense to leave the old stuff to atrophy and build the new, better API up around it. That's basically the <a href="http://www.martinfowler.com/bliki/StranglerApplication.html">Strangler pattern</a> applied at the code level.<br> <br> That said, there are some of the refactorings that ReSharper has that I'd <em>love</em> to have in my IDE. It's just that I think that already VS is too slow and heavy on my machine - even without ReSharper...</div> <div class="comment-date">2012-01-03 19:42 UTC</div> </div> <div class="comment" id="aea6b6e4669e4920a17bb441a7c149b4"> <div class="comment-author"><a href="http://http://jeff-sharp.blogspot.com/">Jeff Saunders</a> <a href="#aea6b6e4669e4920a17bb441a7c149b4">#</a></div> <div class="comment-content">You could build a whole set of tools around append-only programming; version control in particular would be a piece of cake. An editor that let you use a class or function as a starting point and then save an edited version as a new class or function (without affecting the existing version) would be quite helpful.<br> <br> I tried an extremely SOLID design for a prototype recently, with very few stable dependencies and leaning on the container for almost everything. I didn't quite adhere to OCP as you've described it here, but in retrospect it would have almost eliminated regressions. There was a lot of pushback to the design as soon as we got another developer on (just before we threw away the prototype), though.<br> <br> The usual complaints about being unable to see the big picture (due to container registrations being made mostly linearly) came through, and my choice to compose functions rather than objects certainly didn't help as it resulted in some quite foreign-looking code. I think tooling could have helped, but we've decided to stick to KISS and stabilise more dependencies for the upcoming releases.<br> <br> On the subject of tooling, I think something that's missing from DI tooling is a graphical designer containing a tree view of what your container will resolve at runtime, with markers for missing dependencies and such. A &quot;DIML&quot; file, perhaps, that generates a .diml.cs or .diml.vb when saved. Then you could have a find-and-replace-style feature for replacing dependencies, respecting OCP.</div> <div class="comment-date">2012-01-03 21:53 UTC</div> </div> <div class="comment" id="88bb890c0d77483ca21a8854462d272c"> <div class="comment-author">Andreas Triesch <a href="#88bb890c0d77483ca21a8854462d272c">#</a></div> <div class="comment-content">This might seem a bit like a newbie question (well, with regards to SOLID I am one) - does that mean one should mostly use 'protected virtual' methods as opposed to 'private', in case I need something just a little different? Or does the notion I might need to do so in a particular case rather imply my class might be too large (violating SRP) and I should try to achieve flexibility by breaking it up and composing my logic via DI instead of using inheritance?</div> <div class="comment-date">2012-01-03 22:11 UTC</div> </div> <div class="comment" id="f7c0547c9ed646cfb4f5cd96b43a8f00"> <div class="comment-author"><a href="http://geekswithblogs.net/thearchitectsnapkin/Default.aspx">Ralf Westphal</a> <a href="#f7c0547c9ed646cfb4f5cd96b43a8f00">#</a></div> <div class="comment-content">Let me support you, Mark, in not having much use for ReSharper.<br> <br> Event though I&#180;m using it, I&#180;m not using it much for refactoring. Out of all the refactorings I use maybe just 2-3 (rename, extract method, move class to separate file).<br> <br> My main use for ReSharper is as a test runner.<br> <br> So I agree: ReSharper is a tool for developers wrestling with tons of legacy code they need to refactor. But if your code base is clean... then the need for larger rearrangements is rare. &quot;Refactoring to deeper insight&quot; sometimes requires such rearrangements. But this too need not be that hard, if the functional units are fine grained.</div> <div class="comment-date">2012-01-04 10:15 UTC</div> </div> <div class="comment" id="aa9f46fc4bb147029a5d15b2f3e515c3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#aa9f46fc4bb147029a5d15b2f3e515c3">#</a></div> <div class="comment-content">Jeff, I think that the use of a DI Container and application of the OCP are perpendicular concerns.<br> <br> Andreas, the 'old' definition of OCP (Meyer's) is to use inheritance to extend an existing class. However, when we favor composition over inheritance, the default way to extend a class is to apply a Decorator to it.<br> <br> Ralf, I think you've nailed it. The reason why I've never felt much need for ReSharper is because I avoid legacy code like the plague. In fact, I've turned down job offers (that payed better than anything I've ever received) because it involved dealing with too much legacy code. If I were ever to deal substantially with legacy code, I might very well install ReSharper.</div> <div class="comment-date">2012-01-04 18:30 UTC</div> </div> <div class="comment" id="ef4f4c602c864f4ab4a4906318fdc22f"> <div class="comment-author"><a href="http://jeff-sharp.blogspot.com/">Jeff Saunders</a> <a href="#ef4f4c602c864f4ab4a4906318fdc22f">#</a></div> <div class="comment-content">Mark, you're right. I went off on a bit of a tangent! <br> <br> I guess the only relation that I was riffing off is that a great tool for writing and composing SOLID code would help with both.</div> <div class="comment-date">2012-01-04 19:32 UTC</div> </div> <div class="comment" id="f361dfcb531b45549b54868ca4e5cd21"> <div class="comment-author"><a href="https://twitter.com/#!/payman81">Payman</a> <a href="#f361dfcb531b45549b54868ca4e5cd21">#</a></div> <div class="comment-content">You said that you don't refactor often. Does that mean that you don't practice TDD? As I understand it, refactoring is an essential step in each TDD cycle. <br> Refactoring aside, it seems to me that TDD practice makes you violate OCP since you start with the simplest implementation and keep improving it (hence changing existing code) to make new tests pass.</div> <div class="comment-date">2012-03-17 08:51 UTC</div> </div> <div class="comment" id="6412f9f42ce748458855618ff63a1d7e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6412f9f42ce748458855618ff63a1d7e">#</a></div> <div class="comment-content">Payman, I do practice TDD, and I do refactor regularly as part of the Red/Green/Refactor cycle.<br> <br> What I meant (but perhaps did not explicitly state) was that once a piece of code is released to production, it changes status. That kind of code I don't often refactor.</div> <div class="comment-date">2012-03-18 14:02 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Testing Container Configurations https://blog.ploeh.dk/2011/12/21/TestingContainerConfigurations 2011-12-21T13:25:32+00:00 Mark Seemann <div id="post"> <p> Here's a question I often get: </p> <blockquote> <p> “Should I test my DI Container configuration?” </p> </blockquote> <p> The motivation for asking mostly seems to be that people want to know whether or not their applications are correctly wired. That makes sense. </p> <p> A related question I also often get is whether or not a particular container has a self-test feature? In this post I'll attempt to answer both questions. </p> <h3 id="9130110eee0c42ac88c93d0c0f0d2074"> Container Self-testing <a href="#9130110eee0c42ac88c93d0c0f0d2074" title="permalink">#</a> </h3> <p> Some DI Containers have a method you can invoke to make it perform a consistency check on itself. As an example, <a href="http://structuremap.net/structuremap/">StructureMap</a> has the AssertConfigurationIsValid method that, according to documentation does “a full environment test of the configuration of [the] container.” It will “try to create every configured instance [...]” </p> <p> Calling the method is <em>really</em> easy: </p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">container.AssertConfigurationIsValid();</font></pre> </p> <p> Such a self-test can often be an expensive operation (not only for StructureMap, but in general) because it's basically attempting to create an instance of each and every Service registered in the container. If the configuration is large, it's going to take some time, but it's still going to be faster than performing a manual test of the application. </p> <p> Two questions remain: Is it worth invoking this method? Why don't all containers have such a method? </p> <blockquote> <p> The quick answer is that such a method is close to worthless, which also explains why many containers don't include one. </p> </blockquote> <p> To understand the answer, consider the set of all components contained in the container in this figure: </p> <p> <a href="/content/binary/Windows-Live-Writer/Testing-Container-Configurations_84D9/containersets_2.png"><img src="/content/binary/Windows-Live-Writer/Testing-Container-Configurations_84D9/containersets_thumb.png"></a> </p> <p> The container contains the set of components IFoo, IBar, IBaz, Foo, Bar, Baz, and Qux so a self-test will try to create a single instance of each of these seven types. If all seven instances can be created, the test succeeds. </p> <p> All this accomplishes is to verify that the configuration is internally <em>consistent.</em> Even so, an application could require instances of the ICorge, Corge or Grault types which are completely external to the configuration, in which case resolution would fail. </p> <p> Even more subtly, resolution would also fail whenever the container is queried for an instance of IQux, since this interface isn't part of the configuration, even though it's related to the concrete Qux type which <em>is</em> registered in the container. A self-test only verifies that the concrete Qux class can be resolved, but it never attempts to create an instance of the IQux interface. </p> <p> In short, the fact that a container's configuration is internally consistent doesn't guarantee that all services required by an application can be served. </p> <p> Still, you may think that at least a self-test can constitute an early warning system: if the self-test fails, surely it must mean that the configuration is invalid? Unfortunately, that's not true either. </p> <p> If a container is being configured using Auto-registration/Convention over Configuration to scan one or more assemblies and register certain types contained within, chances are that ‘too many' types will end up being registered - particularly if one or more of these assemblies are reusable libraries (as opposed to application-specific assemblies). Often, the number of redundant types added is negligible, but they <em>may</em> make the configuration internally <em>inconsistent</em>. However, if the inconsistency only affects the redundant types, it doesn't matter. The container will still be able to resolve <em>everything</em> the current application requires. </p> <p> Thus, a container self-test method is worthless. </p> <p> Then how can the container configuration be tested? </p> <h3 id="95309a518aa74183bdf6e1b41b6c4b85"> Explicit Testing of Container Configuration <a href="#95309a518aa74183bdf6e1b41b6c4b85" title="permalink">#</a> </h3> <p> Since a container self-test doesn't achieve the desired goal, how can we ensure that an application can be composed correctly? </p> <p> One option is to write an automated integration test (not a unit test) for each service that the application requires. Still, if done manually, you run the risk of forgetting to write a test for a specific service. A better option is to come up with a convention so that you can identify <em>all</em> the services required by a specific application, and then write a convention-based test to verify that the container can resolve them all. </p> <p> Will this guarantee that the application can be correctly composed? </p> <p> No, it only guarantees that it can be composed - not that this composition is <em>correct.</em> </p> <p> Even when a composed instance can be created for each and every service, many things may still be wrong: </p> <ul> <li>Composition is just plain wrong: <ul> <li>Decorators may be missing</li> <li>Decorators may have been added in the wrong order</li> <li>The wrong services are injected into consumers (this is more likely to happen when you follow the <a href="http://codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle</a>, since there will be multiple concrete implementations of each interface)</li> </ul></li> <li>Configuration values like connection strings and such are incorrect - e.g. while a connection string is supplied to a constructor, it may not contain the correct values.</li> <li>Even if <em>everything</em> is correctly composed, the run-time environment may prevent the application from working. As an example, even if an injected connection string is correct, there may not be any connection to the database due to network or security misconfiguration.</li> </ul> <p> In short, a <a href="http://xunitpatterns.com/Layer%20Test.html">Subcutaneous Test</a> or full System Test is the only way to verify that <em>everything</em> is correctly wired. However, if you have good test coverage at the unit test level, a series of <a href="http://xunitpatterns.com/Smoke%20Test.html">Smoke Tests</a> is all that you need at the System Test level because in general you have good reason to believe that all behavior is correct. The remaining question is whether all this correct behavior can be correctly connected, and that tends to be an all-or-nothing proposition. </p> <h3 id="9e7ff4973fc44bffbc361afcf4a0b61c"> Conclusion <a href="#9e7ff4973fc44bffbc361afcf4a0b61c" title="permalink">#</a> </h3> <p> While it would be possible to write a set of convention-based integration tests to verify the configuration of a DI Container, the return of investment is too low since it doesn't remove the need for a set of Smoke Tests at the System Test level. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ada400a2d395482abf0eba4d2b111744"> <div class="comment-author">Graham <a href="#ada400a2d395482abf0eba4d2b111744">#</a></div> <div class="comment-content">While you're correct that integration tests are not required, they can still provide value if they pin-point the problem more quickly.<br> <br> A failing smoke test won't always tell you exactly what went wrong (while that's a failing of the test, it's not always that easy to fix). Rather than investigating, I'd prefer to have a failing integration test that means the smoke test won't even be run.<br> <br> I find the most value comes from tests that try and resolve the root of the object graph from the bootstrapped container, i.e. anything I (or a framework) explicitly try and resolve at runtime.<br> <br> Their being green doesn't necessarily mean the application is fine, but their being red means it's definitely broken. It is a duplication of testing, but so are the smoke tests and some of the unit tests (hopefully!).</div> <div class="comment-date">2011-12-22 11:18 UTC</div> </div> <div class="comment" id="1f55e95e42de4a8f932e68ae6ec56518"> <div class="comment-author">Jan <a href="#1f55e95e42de4a8f932e68ae6ec56518">#</a></div> <div class="comment-content">I am currently not quite sure whether an automated configuration test is really required or not.<br> <br> The correct wireing is already tested by the DI Container itself. An error prone configuration will be obvious in the first developer or at least user test.<br> So, is this kind of test overhead, useful or even necessary?<br> I probably wouldn't do these kind tests.</div> <div class="comment-date">2011-12-22 20:36 UTC</div> </div> <div class="comment" id="c504f28423fa4b2887c1241df5a9a0de"> <div class="comment-author">Mike Bridge <a href="#c504f28423fa4b2887c1241df5a9a0de">#</a></div> <div class="comment-content">In my application, I'd like to assert that my composition root will correctly instantiate a decorator in one situation and not in another. I managed to miswire this on my first attempt and have been trying to figure out how to write a simple test to assert that I'd specified the dependencies correctly.<br> <br> Would it be worthwhile strategy to unit test my container's configuration by mocking the container's resolver? I'd like to be able to run my registration on a container, then assert that the mocked resolver received the correct &quot;Resolve&quot; messages in the correct order. Currently I'm using validating this with an integration test, but I was thinking that this would be much simpler---if my container supports it.<br> <br> <br> <br> <br> <br> <br> <br> <br> </div> <div class="comment-date">2011-12-28 19:23 UTC</div> </div> <div class="comment" id="4e3f50d46d474552967dcfad05e460b1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4e3f50d46d474552967dcfad05e460b1">#</a></div> <div class="comment-content">That sounds like a brittle test, because instead of testing the 'what' you'd be testing the 'how'.</div> <div class="comment-date">2011-12-29 07:50 UTC</div> </div> <div class="comment" id="33c72917604e44cf96982d8a2103e4bb"> <div class="comment-author">Mike Bridge <a href="#33c72917604e44cf96982d8a2103e4bb">#</a></div> <div class="comment-content">Thanks, that's probably a correct assessment. I was intending it to be an interaction test which asserts &quot;given a certain input to my DI container initializer, my decorator should be created when the container instantiates my graph&quot;.<br> <br> I'll go back and think a bit more about how I can test the resulting behaviour instead of the implementation.</div> <div class="comment-date">2011-12-29 17:12 UTC</div> </div> <div class="comment" id="f19cce6f7efa4b328a25aee820364b4a"> <div class="comment-author"><a href="Http://www.planetgeek.ch">Daniel Marbach</a> <a href="#f19cce6f7efa4b328a25aee820364b4a">#</a></div> <div class="comment-content">Hy<br> Nice post! We have a set of automated developer acceptance tests which start the system in an machine.specifications test, execute business commands, shuts down the system and does business asserts on the external subsystems which are stubbed away depending on the feature under test. With that we have an implicit container test: the system boots and behave as expected. If someone adds new ccomponents for that feature which are not properly registered the tests will fail.<br> <br> Daniel</div> <div class="comment-date">2012-01-14 07:58 UTC</div> </div> <div class="comment" id="522069727e1340b4b18563070623aa0f"> <div class="comment-author">Thomas <a href="#522069727e1340b4b18563070623aa0f">#</a></div> <div class="comment-content">Hi Mark,<br> <br> While I agree on the principle one question came to my mind related to the first part. I don't understand why resolving IQux is an issue because even in runtime it's not required and not registered?<br> <br> Thanks<br> <br> Thomas</div> <div class="comment-date">2012-02-03 18:16 UTC</div> </div> <div class="comment" id="ee82511037f147efa198687f711f1e57"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ee82511037f147efa198687f711f1e57">#</a></div> <div class="comment-content">Thomas, the point of IQux is that, in the example, the container doesn't know about the IQux interface. A container self-test is only going to walk through all components to see if they can be resolved. However, such a test will never attempt to resolve IQux, since the container doesn't know about it.<br> <br> So, if an application code base requires IQux, the application is going to fail even when the container self-test succeeds.</div> <div class="comment-date">2012-02-03 19:30 UTC</div> </div> <div class="comment" id="4889300d878b490ab015492fb28c4edf"> <div class="comment-author">Thomas <a href="#4889300d878b490ab015492fb28c4edf">#</a></div> <div class="comment-content">Mark, I understand that. However even in the production scenario when a code base requires IQux at some point in the time (for example Baz requires IQux in the constructor), it should be registered in the container, otherwise it won't work. I think I should miss something.</div> <div class="comment-date">2012-02-05 11:10 UTC</div> </div> <div class="comment" id="610b2a029c124a02af55b08494835e3c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#610b2a029c124a02af55b08494835e3c">#</a></div> <div class="comment-content">Yes - that's why a container self-test is worthless. The container doesn't know about the requirements of you application. All it can do is to test whether or not it's <em>internally</em> consistent. It doesn't tell you anything about its ability to resolve the object graphs your application is going to need.</div> <div class="comment-date">2012-02-05 12:02 UTC</div> </div> <div class="comment" id="0b782a3dcec74f0aba1218c16398d24e"> <div class="comment-author">Rajkumar Srinivasan <a href="#0b782a3dcec74f0aba1218c16398d24e">#</a></div> <div class="comment-content">Mark, Great post. In order to avoid violation of <strong>Resused Abstraction Principle</strong>, I infer from some of your other posts that we need to provide null object implementation for all interfaces or abstractions. I am just trying to confirm if my inference is correct.</div> <div class="comment-date">2012-06-05 09:10 UTC</div> </div> <div class="comment" id="f196d5de2b7944469864d1902ac3c142"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f196d5de2b7944469864d1902ac3c142">#</a></div> <div class="comment-content">I'm not sure I can agree with that - that sounds a bit like Cargo Culting to me... The point of the RAP is that it tells us something about the degree of abstraction of our interfaces. If the sole purpose of adding a Null Object implementation is to adhere to the RAP, it may address the <em>mechanics</em> of the RAP, but not the <em>spirit</em>.</div> <div class="comment-date">2012-06-05 09:18 UTC</div> </div> <div class="comment" id="a78ea1ac7e0e43f8bf8d97bf35b76c41"> <div class="comment-author"><a href="http://qujck.com">Peter Parker</a> <a href="#a78ea1ac7e0e43f8bf8d97bf35b76c41">#</a></div> <div class="comment-content"> <p>You have made a very basic and primitive argument to sell a complex but feasible process as pointless: </p> <blockquote> <p>such a method is close to worthless</p> </blockquote> <p>and without a working example of a better way you explain why you are right. I am an avid reader of your posts and often reference them but IMO this particular argument is not well reasoned.</p> <p>Your opening argument explains why you may have an issue when using <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern/" target="_blank">the Service Locator anti-pattern</a>: </p> <blockquote> <p>an application could require instances of the ICorge, Corge or Grault types which are completely external to the configuration, in which case resolution would fail.</p> </blockquote> <p>Assertions such as the following would ideally be specified in a set of automated tests regardless of the method of composition </p> <blockquote> <p>Decorators may be missing</p> </blockquote> <p>&amp;</p> <blockquote> <p>Decorators may have been added in the wrong order</p> </blockquote> <p>And I fail to see how Pure DI would make the following into lesser issues </p> <blockquote> <p>Configuration values like connection strings and such are incorrect - e.g. while a connection string is supplied to a constructor, it may not contain the correct values</p> </blockquote> <p>&amp;</p> <blockquote> <p>the run-time environment may prevent the application from working</p> </blockquote> <hr> <p>My response was prompted by a statement made by you on <a href="http://stackoverflow.com/a/34195907">stackoverflow</a>. You are most highly regarded when it comes to .NET and DI and I feel statements such as <em>"Some people hope that you can ask a DI Container to self-diagnose, but you can't."</em> are very dangerous when they are out-of-date.</p> <p>Simple Injector will diagnose a number of issues beyond <em>"do I have a registration for X</em>". I'm not claiming that these tests alone are full-proof validation of your configuration but they are set of built in tests for issues that can occur in any configuration (including a <em>Pure</em> configuration) and these combined tests are far from worthless ...</p> <ul> <li> <strong>LifeStyle Mismatches</strong>: The component depends on a service with a lifestyle that is shorter than that of the component</li> <li> <strong>Short Circuited Dependencies</strong>: The component depends on an unregistered concrete type and this concrete type has a lifestyle that is different than the lifestyle of an explicitly registered type that uses this concrete type as its implementation</li> <li> <strong>Potential Single Responsibility Violations</strong>: The component depends on too many services</li> <li> <strong>Container-registered Type</strong>: A concrete type that was not registered explicitly and was not resolved using unregistered type resolution, but was created by the container using the default lifestyle</li> <li> <strong>Torn Lifestyle</strong>: Multiple registrations with the same lifestyle map to the same component</li> <li> <strong>Ambiguous Lifestyles</strong>: Multiple registrations with the different lifestyles map to the same component</li> <li> <strong>Disposable Transient Components</strong>: A registration has been made with the Transient lifestyle for a component that implements <code>IDisposable</code> </li> </ul> <p>Your claim that <em>"such a method is close to worthless"</em> may be true for the majority of the available .NET DI Containers but it does not take Simple Injector's <a href="https://simpleinjector.readthedocs.org/en/latest/diagnostics.html" target="_blank">diagnostic services</a> into consideration.</p></div> <div class="comment-date">2015-12-11 09:15 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Factory Overload https://blog.ploeh.dk/2011/12/19/FactoryOverload 2011-12-19T13:04:55+00:00 Mark Seemann <div id="post"> <p> Recently I received a question from <a href="http://kellabyte.com/">Kelly Sommers</a> about good ways to refactor away from Factory Overload. Basically, she's working in a code base where there's an explosion of <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factories</a> which seems to be counter-productive. In this post I'll take a look at the example problem and propose a set of alternatives. </p> <p> An <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a> (and its close relative Product Trader) can serve as a <a href="http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern/2280289#2280289">solution to various challenges</a> that come up when writing loosely coupled code (chapter 6 of <a href="http://amzn.to/12p90MG">my book</a> describes the most common scenarios). However, introducing an Abstract Factory may be a <a href="http://en.wikipedia.org/wiki/Leaky_abstraction">leaky abstraction</a>, so don't do it blindly. For example, an Abstract Factory is <a href="http://stackoverflow.com/questions/4648318/dependency-injection-new-instance-required-in-several-of-a-classes-methods/4650050#4650050">rarely the best approach to address lifetime management concerns</a>. In other words, the Abstract Factory has to make sense as a pure model element. </p> <p> That's not the case in the following example. </p> <h3 id="ec661c46307742d8950a821e9aae7ec1"> Problem Statement <a href="#ec661c46307742d8950a821e9aae7ec1" title="permalink">#</a> </h3> <p> The question centers around a code base that integrates with a database closely guarded by DBA police. Apparently, every single database access must happen through a set of <em>very</em> fine-grained stored procedures. </p> <p> For example, to update the first name of a user, a set of stored procedures exist to support this scenario, <em>depending on the context of the current application user:</em> </p> <table border="1" cellspacing="0" cellpadding="2" width="400"> <tbody> <tr> <td valign="top" width="135"><strong>User type</strong></td> <td valign="top" width="146"><strong>Stored procedure</strong></td> <td valign="top" width="117"><strong>Parameter name</strong></td> </tr> <tr> <td valign="top" width="135">Admin</td> <td valign="top" width="157">update_admin_firstname</td> <td valign="top" width="148">adminFirstName</td> </tr> <tr> <td valign="top" width="135">Guest</td> <td valign="top" width="157">update_guest_firstname</td> <td valign="top" width="148">guestFirstName</td> </tr> <tr> <td valign="top" width="135">Regular</td> <td valign="top" width="157">update_regular_firstname</td> <td valign="top" width="148">regularFirstName</td> </tr> <tr> <td valign="top" width="135">Restricted</td> <td valign="top" width="157">update_restricted_firstname</td> <td valign="top" width="148">restrictedFirstName</td> </tr> </tbody> </table> <p> As this table demonstrates, not only is there a stored procedure for each user context, but the parameter name differs as well. However, in this particular case it <em>seems</em> as though there's a pattern to the names. </p> <blockquote> <p> If this pattern is consistent, I think the easiest way to address these variations would be to algorithmically build the strings from a couple of templates. </p> </blockquote> <p> However, this is not the route taken by Kelly's team, so I assume that things are more complicated than that; apparently, a templated approach is not viable, so for the rest of&nbsp; this article I'm going to assume that it's necessary to write at least <em>some</em> code to address each case individually. </p> <p> The current solution that Kelly's team has implemented is to use an Abstract Factory (Product Trader) to translate the user type into an appropriate IUserFirstNameModifier instance. From the consuming code, it looks like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> modifier = factory.Create(<span style="color: "><font color="#2b91af">UserTypes</font></span>.Admin);</font> <font style="font-size: 10pt">modifier.Commit(<span style="color: "><font color="#a31515">"first"</font></span>);</font></pre> </p> <p> where the factory variable is an instance of the IUserFirstNameModifierFactory interface. This is certainly loosely coupled, but looks like a leaky abstraction. Why is a factory needed? It seems that its single responsibility is to translate a UserTypes instance (an enum) into an IUserFirstNameModifier. There's a code smell buried here - try to spot it before you read on :) </p> <h3 id="e30cbd11bc894a6aa9adb8d520b9d31d"> Proposed Solution <a href="#e30cbd11bc894a6aa9adb8d520b9d31d" title="permalink">#</a> </h3> <p> Kelly herself suggests an alternative involving a concrete Builder which can create instances of a single concrete UserFirstNameModifier with or without an implicit conversion: </p> <p> <pre style="margin: 0px"><span style="color: "><font style="font-size: 10pt" color="#008000">// Implicit conversion.</font></span> <span style="color: "><font color="#2b91af"><font style="font-size: 10pt">UserFirstNameModifier</font></font></span><font style="font-size: 10pt"> modifier1 = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; builder.WithUserType(<span style="color: "><font color="#2b91af">UserTypes</font></span>.Guest);</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font style="font-size: 10pt" color="#008000">// Without implicit conversion.</font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> modifier2 = builder</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .WithUserType(<span style="color: "><font color="#2b91af">UserTypes</font></span>.Restricted)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .Create();</font></pre> </p> <p> While this may seem to reduce the number of classes involved, it has several drawbacks: </p> <ul> <li> First of all, the Fluent Builder pattern implies that you can forgo invoking any of the WithXyz methods (WithUserType) and just accept all the default values encapsulated in the builder. This again implies that there's a default user type, which may or may not make sense in that particular domain. Looking at Kelly's code, UserTypes is an enum (and thus has a default value), so if WithUserType isn't invoked, the Create method defaults to UserTypes.Admin. That's a bit too implicit for my taste. </li> <li> Since all involved classes are now concrete, the proposed solution isn't extensibile (and by corollary hard to unit test). </li> <li> The builder is essentially a big switch statement. </li> </ul> <p> Both the current implementation and the proposed solution involves passing an enum as a method parameter to a different class. If you've read and memorized <a href="http://www.amazon.com/gp/product/0201485672/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0201485672">Refactoring</a> you should by now have recognized both a code smell and the remedy. </p> <h3 id="1be7e9d949bf4484a180040ee7a36dd8"> Alternative 1a: Make UserType Polymorphic <a href="#1be7e9d949bf4484a180040ee7a36dd8" title="permalink">#</a> </h3> <p> The code smell is <a href="http://c2.com/cgi/wiki?FeatureEnvySmell">Feature Envy</a> and a possible refactoring is to <a href="http://martinfowler.com/refactoring/catalog/replaceTypeCodeWithStateStrategy.html">replace the enum with a Strategy</a>. In order to do that, an IUserType interface is introduced: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IUserType</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IUserFirstNameModifer</font></span> CreateUserFirstNameModifier();</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Usage becomes as simple as this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> modifier = userType.CreateUserFirstNameModifier();</font></pre> </p> <p> Obviously, more methods can be added to IUserType to support other update operations, but care should be taken to avoid creating a <a href="http://martinfowler.com/bliki/HeaderInterface.html">Header Interface</a>. </p> <p> While this solution is much more object-oriented, I'm still not quite happy with it, because apparently, the context is a <a href="http://abdullin.com/cqrs">CQRS</a> style architecture. Since an update operation is essentially a Command, then why model the implementation along the lines of a Query? Both Abstract Factory and Factory Method patterns represent Queries, so it seems redundant in this case. It should be possible to apply the <a href="http://en.wikipedia.org/wiki/Hollywood_principle_%28computer_programming%29">Hollywood Principle</a> here. </p> <h3 id="981aa16606e04e259808cd61a78b32a3"> Alternative 1b: Tell, Don't Ask <a href="#981aa16606e04e259808cd61a78b32a3" title="permalink">#</a> </h3> <p> Why have the user type <em>return</em> an modifier? Why can't it perform the update itself? The IUserType interface should be changed to something like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IUserType</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> CommitUserFirtName(<span style="color: "><font color="#0000ff">string</font></span> firstName);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This makes it easier for the consumer to commit the user's first name because it can be done directly on the IUserType instance instead of first creating the modifier. </p> <p> It also makes it much easier to unit test the consumer because there's no longer a mix of Command and Queries within the same method. From <a href="http://www.amazon.com/gp/product/0321503627/ref=as_li_ss_tl?ie=UTF8&amp;tag=ploeh-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321503627">Growing Object-Oriented Software</a> we know that Queries should be modeled with <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a> and Commands with <a href="http://xunitpatterns.com/Mock%20Object.html">Mocks</a>, and if you've ever tried mixing the two you know that it's a sort of interaction that should be minimized. </p> <h3 id="50b8a5ff0b7b4483b699d22a3fd3d7b6"> Alternative 2a: Distinguish by Type <a href="#50b8a5ff0b7b4483b699d22a3fd3d7b6" title="permalink">#</a> </h3> <p> While I personally like alternative 1b best, it may not be practical in all situations, so it's always valuable to examine other alternatives. </p> <p> The root cause of the problem is that there's a lot of stored procedures. I want to reiterate that I still think that the absolutely easiest solution would be to generate a SqlCommand from a string template, but given that this article assumes that this isn't possible or desirable, it follows that code must be written for each stored procedure. </p> <p> Why not simply define an interface for each one? As an example, to update the user's first name in the context of being an ‘Admin' user, this <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interface</a> can be used: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IUserFirstNameAdminModifier</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Commit(<span style="color: "><font color="#0000ff">string</font></span> firstName);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Similar interfaces can be defined for the other user types, such as IUserFirstNameRestrictedModifier, IUserFirstNameGuestModifier and so on. </p> <p> This is a very simple solution; it's easy to implement, but risks violating the <a href="http://codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle</a> (RAP). </p> <h3 id="8912950cf00a4a32894b0957854b3cbc"> Alternative 2b: Distinguish by Generic Type <a href="#8912950cf00a4a32894b0957854b3cbc" title="permalink">#</a> </h3> <p> The problem with introducing interfaces like IUserFirstNameAdminModifier, IUserFirstNameRestrictedModifier, IUserFirstNameGuestModifier etc. is that they differ only by name. The Commit method is the same for all these interfaces, so this seems to violate the RAP. It'd be better to merge all these interfaces into a single interface, which is what Kelly's team is currently doing. However, the problem with this is that the type carries no information about the <em>role</em> that the modifier is playing. </p> <p> Another alternative is to turn the modifier interface into a generic interface like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> <span style="color: "><font color="#2b91af">IUserFirstNameModifier</font></span>&lt;T&gt; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">where</font></span> T : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IUserType</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Commit(<span style="color: "><font color="#0000ff">string</font></span> firstName);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> The IUserType is a <a href="http://en.wikipedia.org/wiki/Marker_interface_pattern">Marker Interface</a>, so .NET purists are not going to like this solution, since <a href="http://msdn.microsoft.com/en-us/library/ms229022.aspx">the .NET Type Design Guidelines recommend against using Marker Interfaces</a>. However, it's impossible to constrain a generic type argument against an attribute, so the party line solution is out of the question. </p> <p> This solution ensures that consumers can now have dependencies on IUserFirstNameModifier&lt;AdminUserType&gt;, IUserFirstNameModifier&lt;RestrictedUserType&gt;, etc. </p> <p> However, the need for a marker interface gives off another odeur. </p> <h3 id="9adc3ecda369415f8edb43e3d373ee78"> Alternative 3: Distinguish by Role <a href="#9adc3ecda369415f8edb43e3d373ee78" title="permalink">#</a> </h3> <p> The problem at the heart of alternative 2 is that it attempts to use the <em>type</em> of the interfaces as an indicator of the roles that Services play. It's seems that making the type distinct works against the RAP, but when the RAP is applied, the type becomes ambiguous. </p> <p> However, as <a href="http://www.tedneward.com/">Ted Neward</a> <a href="http://msdn.microsoft.com/en-us/magazine/hh205754.aspx">points out</a> in his excellent series on <a href="http://msdn.microsoft.com/en-us/magazine/ff955611.aspx">Multiparadigmatic .NET</a>, the type is only one axis of variability among many. Perhaps, in this case, it may be much easier to use the <em>name</em> of the dependency to communicate its role instead of the type. </p> <p> Given a single, ambiguous IUserFirstNameModifier interface (just as in the original problem statement), a consumer can distinguish between the various roles of modifiers by their names: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">partial</font></span> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">SomeConsumer</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IUserFirstNameModifier</font></span> adminModifier;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IUserFirstNameModifier</font></span> guestModifier;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> SomeConsumer(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IUserFirstNameModifier</font></span> adminModifier,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IUserFirstNameModifier</font></span> guestModifier)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.adminModifier = adminModifier;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.guestModifier = guestModifier;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> DoSomething()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (<span style="color: "><font color="#0000ff">this</font></span>.UseAdmin)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.adminModifier.Commit(<span style="color: "><font color="#a31515">"first"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#0000ff">else</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.guestModifier.Commit(<span style="color: "><font color="#a31515">"first"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Now it's entirely up to the <a href="/2011/07/28/CompositionRoot">Composition Root</a> to compose SomeConsumer with the correct modifiers, and while this can be done manually, it's an excellent case for a DI Container and a bit of Convention over Configuration. </p> <h3 id="413af579c2a14676949d7051b171ab19"> Conclusion <a href="#413af579c2a14676949d7051b171ab19" title="permalink">#</a> </h3> <p> I'm sure that if I'd spent more time analyzing the problem I could have come up with more alternatives, but this post is becoming long enough already. </p> <p> Of the alternatives I've suggested here, I prefer 1b or 3, depending on the exact requirements. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4c525e4f15874b939e6b9641c7073056"> <div class="comment-author"><a href="http://www.dotninjas.dk/">Jakob Christensen</a> <a href="#4c525e4f15874b939e6b9641c7073056">#</a></div> <div class="comment-content">Nice work, Mark.<br> <br> Personally I prefer 1b over 3. The if-statement in option 3 looks a bit suspicious to me as one day it might give rise to some maintenance if more user types have to be supported by the SomeConsumer type, so I am afraid it may violate the open/closed principle. Option 1b looks more straight forward to me.</div> <div class="comment-date">2011-12-21 12:48 UTC</div> </div> <div class="comment" id="56810551d1c14298b7b7f09b5d1b89f1"> <div class="comment-author">Travis <a href="#56810551d1c14298b7b7f09b5d1b89f1">#</a></div> <div class="comment-content">wow, those dba enforced sprocs and security measures are friction no developer should have to face<br> <br> sorry, way OT</div> <div class="comment-date">2011-12-21 17:52 UTC</div> </div> <div class="comment" id="855799439e1149bf9cecd0463626805c"> <div class="comment-author">Emanuel Pasat <a href="#855799439e1149bf9cecd0463626805c">#</a></div> <div class="comment-content">I have a similar situation and it might be a good ocassion to clarify it.<br> I'm trying to implement a visitor pattern over a list of visitees object, constructed with a factory.<br> <br> A visitee requires some external services, so, it might be resonable to have them injected already by IOC in the _factory (VisiteeFactory). isLast is an example of other contextual information, outside of dto, but required to create other visitee types.<br> Given that is a bounded context, how can I improve this design?<br> <br> Pseudocode follows:<br> <br> IVisiteeFactory <br> {<br> VisiteeAdapterBase Create(Dto dto, bool isLast);<br> }<br> <br> VisiteeFactory : IVisiteeFactory <br> {<br> ctor VisiteeFactory(IExternalService service)<br> <br> public VisiteeAdapterBase Create(Dto dto, bool isLast) <br> {<br> // lot of switches and ifs to determine the type of VisiteeAdapterBase<br> if (isLast &amp;&amp; dto.Type == &quot;1&quot;) {<br> return new Type1VisiteeAdapter(... dto props...);<br> }<br> <br> }<br> }<br> <br> ConsumerCtor(VisiteeFactory factory, List&lt;Dto&gt; dtoList) <br> {<br> // guards<br> _factory = factory;<br> _dtoList = dtoList;<br> }<br> ConsumerDoStuff<br> {<br> foreach (var dto in _dtoList) {<br> // isLast is additional logic, outside of dto<br> var visiteeAdapter = _factory.Create(dto, isLast);<br> visiteeAdapter.Accept(visitor);<br> }<br> }<br> <br> </div> <div class="comment-date">2011-12-22 10:06 UTC</div> </div> <div class="comment" id="763fbb08817a47feab22240aa2d95e48"> <div class="comment-author"><a href="http://twitter.com/BlackTigerX">Eber I</a> <a href="#763fbb08817a47feab22240aa2d95e48">#</a></div> <div class="comment-content">Too many classes/interfaces, why not use a simple lookup table?</div> <div class="comment-date">2012-01-09 06:48 UTC</div> </div> <div class="comment" id="2288abc1ac3e48ee9a3a1541e99a2ad2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2288abc1ac3e48ee9a3a1541e99a2ad2">#</a></div> <div class="comment-content">Eber, let me quoute myself from this particular post: &quot;I think the easiest way to address these variations would be to algorithmically build the strings from a couple of templates&quot;. A lookup table falls into that category, so I agree that such a thing would be easier if at all possible.<br> <br> The whole premise of the rest of the post is that for some reason, it's more complicated than that...</div> <div class="comment-date">2012-01-09 19:58 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Polymorphic Consistency https://blog.ploeh.dk/2011/12/07/PolymorphicConsistency 2011-12-07T08:40:21+00:00 Mark Seemann <div id="post"> <p> Asynchronous message passing combined with <a href="http://en.wikipedia.org/wiki/Eventual_consistency">eventual consistency</a> makes it possible to build very scalable systems. However, sometimes eventual consistency isn't appropriate in parts of the system, while it's acceptable in other parts. How can a consistent architecture be defined to fit both <a href="http://en.wikipedia.org/wiki/ACID">ACID</a> and eventual consistency? This article provides an answer. </p> <h3 id="33460895d21342978c927ea9edc00996"> The case of an online game <a href="#33460895d21342978c927ea9edc00996" title="permalink">#</a> </h3> <p> Last week I visited <a href="http://pixelpandemic.net/">Pixel Pandemic</a>, a company that produces browser-based <a href="http://en.wikipedia.org/wiki/Massively_multiplayer_online_role-playing_game">MMORPGs</a>. Since each game world has lots of players who can all potentially interact with each other, scalability is very important. </p> <p> In traditional line of business applications, eventual consistency is often an excellent fit because the application is a projection of the real world. My favorite example is an inventory system: it models what's going on in one or more physical warehouses, but the real world is the ultimate source of truth. A warehouse worker might accidentally drop and damage some of the goods, in which case the application must adjust <em>after the fact</em>. </p> <p> In other words, the information contained within line of business applications tend to lag after the real world. It's impossible to guarantee that the application is always consistent with the real world, so eventual consistency is a natural fit. </p> <p> That's not the case with an online game world. The game world itself is the source of truth, and it must be internally consistent at all times. As an example, in <a href="http://www.zombiepandemic.com/">Zombie Pandemic</a>, players fight against zombies and may take damage along the way. Players can heal themselves, but they would prefer (I gather) that the healing action takes place immediately, and not some time in the future where the character might be dead. Similarly, when a player hits a zombie, they'd prefer to apply the damage immediately. (However, I think that even here, eventual consistency might provide some interesting game mechanics, but that's another discussion.) </p> <p> While discussing these matters with the nice people in Pixel Pandemic, it turned out that while some parts of the game world have to be internally consistent, it's perfectly acceptable to use eventual consistency in other cases. One example is the game's high score table. While a single player should have a consistent view of his or her own score, it's acceptable if the high score table lags a bit. </p> <p> At this point it seemed clear that this particular online game could use an appropriate combination of ACID and eventual consistency, and I think this conclusion can be generalized. The question now becomes: how can a consistent architecture encompass both types of consistency? </p> <h3 id="0321c44ae9964bf28635e502f6b88968"> Problem statement <a href="#0321c44ae9964bf28635e502f6b88968" title="permalink">#</a> </h3> <p> With the above example scenario in mind the problem statement can be generalized: </p> <blockquote> <p> Given that an application should apply a mix of ACID and eventual consistency, how can a consistent architecture be defined? </p> </blockquote> <p> Keep in mind that ACID consistency implies that all writes to a transactional resource must take place as a blocking method call. This seems to be at odds with the concept of asynchronous message passing that works so well with eventual consistency. </p> <p> However, an application architecture where blocking ACID calls are fundamentally different than asynchronous message passing isn't really an architecture at all. Developers will have to decide up-front whether or not a given operation is or isn't synchronous, so the ‘architecture' offers little implementation guidance. The end result is likely to be a heterogeneous mix of Services, Repositories, Units of Work, Message Channels, etc. A uniform principle will be difficult to distill, and the whole thing threatens to devolve into <a href="http://en.wikipedia.org/wiki/Spaghetti_code">Spaghetti Code</a>. </p> <p> The solution turns out to be not at all difficult, but it requires that we invert our thinking a bit. Most of us tend to think about synchronous code first. When we think about code performing synchronous work it seems difficult (perhaps even impossible) to retrofit asynchrony to that model. On the other hand, the converse isn't true. </p> <blockquote> <p> Given an asynchronous API, it's trivial to provide a synchronous, blocking implementation. </p> </blockquote> <p> Adopting an architecture based on asynchronous message passing (the <a href="http://eaipatterns.com/PipesAndFilters.html">Pipes and Filters</a> architecture) enables both scenarios. Eventual consistency can be achieved by passing messages around on persistent queues, while ACID consistency can be achieved by handling a message in a blocking call that enlists a (potentially distributed) transaction. </p> <p> An example seems to be in order here. </p> <h3 id="f5762dcfcb794bf596e5cec161f32a04"> Example: keeping score <a href="#f5762dcfcb794bf596e5cec161f32a04" title="permalink">#</a> </h3> <p> In the online game world, each player accumulates a score based on his or her actions. From the perspective of the player, the score should always be consistent. When you defeat the zombie boss, you want to see the result in your score right away. That sounds an awful lot like the Player is an Aggregate Root and the score is part of that Entity. ACID consistency is warranted whenever the Player is updated. </p> <p> On the other hand, each time a score changes it may influence the high score table, but this doesn't need to be ACID consistent; eventual consistency is fine in this case. </p> <p> Once again, polymorphism comes to the rescue. </p> <p> Imagine that the application has a GameEngine class that handles updates in the game. Using an injected IChannel&lt;PointsChangedEvent&gt; it can update the score for a player as simple as this: </p> <p> <pre style="margin: 0px"><span style="color: "><font style="font-size: 10pt" color="#008000">/* Lots of other interesting things happen</font></span> <span style="color: "><font style="font-size: 10pt" color="#008000">&nbsp;&nbsp;&nbsp; * here, like calculating the new score... */</font></span> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> cmd =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ScoreChangedEvent</font></span>(<span style="color: "><font color="#0000ff">this</font></span>.playerId, score);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">this</font></font></span><font style="font-size: 10pt">.pointsChannel.Send(cmd);</font></pre> </p> <p> The Send method returns void, so it's a good example of a naturally asynchronous API. However, the implementation must do two things: </p> <ul> <li>Update the Player Aggregate Root in a transaction</li> <li>Update the high score table (eventually)</li> </ul> <p> That's two different types of consistency within the same method call. </p> <p> The first step to enable this is to employ the trusty old <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composite</a> design pattern: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">CompositeChannel</font></span>&lt;T&gt; : <span style="color: "><font color="#2b91af">IChannel</font></span>&lt;T&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#2b91af">IChannel</font></span>&lt;T&gt;&gt; channels;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> CompositeChannel(<span style="color: "><font color="#0000ff">params</font></span> <span style="color: "><font color="#2b91af">IChannel</font></span>&lt;T&gt;[] channels)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.channels = channels;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Send(T message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">foreach</font></span> (<span style="color: "><font color="#0000ff">var</font></span> c <span style="color: "><font color="#0000ff">in</font></span> <span style="color: "><font color="#0000ff">this</font></span>.channels)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c.Send(message);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> With a Composite channel it's possible to compose a polymorphic mix of IChannel&lt;T&gt; implementations, some blocking and some asynchronous. </p> <h3 id="91b7cc5b4724432db8ed4b45ddfa3c3c"> ACID write <a href="#91b7cc5b4724432db8ed4b45ddfa3c3c" title="permalink">#</a> </h3> <p> To update the Player Aggregate Root a simple <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> writes the event to a persistent data store. This could be a relational database, a document database, a REST resource or something else - it doesn't really matter exactly which technology is used. </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">PlayerStoreChannel</font></span> : </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IChannel</font></span>&lt;<span style="color: "><font color="#2b91af">ScoreChangedEvent</font></span>&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IPlayerStore</font></span> store;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> PlayerStoreChannel(<span style="color: "><font color="#2b91af">IPlayerStore</font></span> store)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.store = store;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Send(<span style="color: "><font color="#2b91af">ScoreChangedEvent</font></span> message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.store.Save(message.PlayerId, message);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> The important thing to realize is that the IPlayerStore.Save method will be a blocking method call - perhaps wrapped in a distributed transaction. This ensures that updates to the Player Aggregate Root always leave the data store in a consistent state. Either the operation succeeds or it fails during the method call itself. </p> <p> This takes care of the ACID consistent write, but the application must also update the high score table. </p> <h3 id="8ae92eadf22647c48ce79b9ddfc5e9c6"> Asynchronous write <a href="#8ae92eadf22647c48ce79b9ddfc5e9c6" title="permalink">#</a> </h3> <p> Since eventual consistency is acceptable for the high score table, the message can be transmitted over a persistent queue to be picked up by a background process. </p> <p> A generic class can server as an Adapter over an IQueue abstraction: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">QueueChannel</font></span>&lt;T&gt; : <span style="color: "><font color="#2b91af">IChannel</font></span>&lt;T&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IQueue</font></span> queue;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IMessageSerializer</font></span> serializer;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> QueueChannel(<span style="color: "><font color="#2b91af">IQueue</font></span> queue,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IMessageSerializer</font></span> serializer)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.queue = queue;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.serializer = serializer;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Send(T message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.queue.Enqueue(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.serializer.Serialize(message));</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Obvously, the Enqueue method is another void method. In the case of a persistent queue, it'll block while the message is being written to the queue, but that will tend to be a fast operation. </p> <h3 id="c5de80bdc7ea4d6eb614762bd739b41e"> Composing polymorphic consistency <a href="#c5de80bdc7ea4d6eb614762bd739b41e" title="permalink">#</a> </h3> <p> Now all the building blocks are available to compose both channel implementations into the GameEngine via the CompositeChannel. That might look like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> playerConnString = </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">ConfigurationManager</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .ConnectionStrings[<span style="color: "><font color="#a31515">"player"</font></span>].ConnectionString;</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> gameEngine = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">GameEngine</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">CompositeChannel</font></span>&lt;<span style="color: "><font color="#2b91af">ScoreChangedEvent</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">PlayerStoreChannel</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">DbPlayerStore</font></span>(playerConnString)),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">QueueChannel</font></span>&lt;<span style="color: "><font color="#2b91af">ScoreChangedEvent</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">PersistentQueue</font></span>(<span style="color: "><font color="#a31515">"messageQueue"</font></span>),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">JsonSerializer</font></span>())));</font></pre> </p> <p> When the Send method is invoked on the channel, it'll first invoke a blocking call that ensures ACID consistency for the Player, followed by asynchronous message passing for eventual consistency in other parts of the application. </p> <h3 id="201f4aec89174551ad353180979208d9"> Conclusion <a href="#201f4aec89174551ad353180979208d9" title="permalink">#</a> </h3> <p> Even when parts of an application must be implemented in a synchronous fashion to ensure ACID consistency, an architecture based on asynchronous message passing provides a flexible foundation that enables you to polymorphically mix both kinds of consistency in a single method call. From the perspective of the application layer, this provides a consistent and uniform architecture because all mutating actions are modeled as commands end events encapsulated in messages. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="fb04478cd0e0440fab53b583d558d536"> <div class="comment-author"><a href="http://pixelpandemic.net">Ebbe Brandstrup</a> <a href="#fb04478cd0e0440fab53b583d558d536">#</a></div> <div class="comment-content">Thanks a lot for mentioning us here at Pixel Pandemic and for your insights Mark! I very much agree with your conclusion and at this point we're discussing an architectural switch to what you're outlining here (a form of polymorphic consistency) and an event sourcing model for our persistent storage needs.<br> <br> We're working on ways to make as many aspects of our games as possible fit with an eventual consistency model by, e.g. simply by changing the way we communicate information about the virtual game world state to players (to put them in a frame of mind in which eventual consistency fits naturally with their perception of the game state).<br> <br> Looking very much forward to meeting with you again soon and discussing more details!</div> <div class="comment-date">2011-12-07 09:46 UTC</div> </div> <div class="comment" id="13b6b7307d46457f9c943fe78271abc2"> <div class="comment-author">Jake <a href="#13b6b7307d46457f9c943fe78271abc2">#</a></div> <div class="comment-content">Could you also use the async ctp stuff to do it all in a single command, so that you are not blocking while waiting for the persistant store to do I/O, and then when it calls back push the message onto the message queue then return.. If you were using something like async controllers in mvc 4 it would mean you could do something like registering a user which saves them to the database, then pass this event information onto the persistant queue so a backend could pick it up and send emails, and do other tasks that are longer to execute.<br> <br> await this.store.Save(message.PlayerId, message);<br> this.queue.Enqueue(this.serializer.Serialize(message));<br> <br> Keen to hear your thoughts<br> Jake</div> <div class="comment-date">2011-12-08 13:38 UTC</div> </div> <div class="comment" id="6f7d543ac520421c93f8cb9b3e34a63e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6f7d543ac520421c93f8cb9b3e34a63e">#</a></div> <div class="comment-content">Why not? You can combine the async functionality with the approach described above. It could make the application more efficient, since it would free up a thread while the first transaction is being completed.<br> <br> However, while the async CTP makes it easier to write asynchronous code, it doesn't help with blocking calls. It may be more efficient, but not necessarily faster. You can't know whether or not a transaction has committed until it actually happens, so you still need to wait for that before you decide whether or not to proceed.<br> <br> BTW, F# has had async support since its inception, so it's interesting to look towards what people are doing with that. Agents (the F# word for Actors) seem to fit into that model pretty well, and as far as I can tell, an Agent is simply an in-memory asynchronous worker process.</div> <div class="comment-date">2011-12-08 14:07 UTC</div> </div> <div class="comment" id="209750b1a38244e094d4bf7f87d46090"> <div class="comment-author"><a href="http://garymcleanhall.wordpress.com/">Gary McLean Hall</a> <a href="#209750b1a38244e094d4bf7f87d46090">#</a></div> <div class="comment-content">Hi Mark, firstly: great post.<br> <br> I do have a question, though. I can see how this works for all future commands, because they will all need to load the aggregate work to work on it and that will be ACID at all times. What I'm not sure about is how that translates to the query side of the coin - where the query is *not* the high-score table, but must be immediately available on-screen.<br> <br> Even if hypothetical, imagine the screen has a typical Heads-Up-Display of relevant information - stuff like 'ammo', 'health' and 'current score'. These are view concerns and will go down the query arm of the CQRS implementation. For example, the view-model backing this HUD could be stored in document storage for the player. This is, then, subject to eventual consistency and is not ACID, right? <br> <br> I'm clearly not 'getting' this bit of the puzzle at the moment, hopefully you can enlighten me.</div> <div class="comment-date">2011-12-14 21:23 UTC</div> </div> <div class="comment" id="5bd1e2365d05491da265a435cbcef43f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5bd1e2365d05491da265a435cbcef43f">#</a></div> <div class="comment-content">A HUD is exactly the sort of scenario that a must be implemented by a synchronous write. If you want to be sure that the persisted data is ACID consistent, it must be written as a synchronous, blocking operation. This means that once the query side comes along, it can simply read from the same persistent store because it's always up to date. That sort of persisted data isn't eventually consistent - it's atomically consistent.</div> <div class="comment-date">2011-12-21 08:08 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. TDD improves reusability https://blog.ploeh.dk/2011/11/10/TDDimprovesreusability 2011-11-10T16:55:10+00:00 Mark Seemann <div id="post"> <p>There's this meme going around that software <a href="http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/">reuse is a fallacy</a>. Bollocks! The <em>reuse is a fallacy meme</em> is a fallacy :) To be fair, I'm not claiming that everything can and should be reused, but my claim is that all code produced by Test-Driven Development (TDD) is being reused. Here's why:</p> <p>When tests are written first, they act as a kind of <a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>. Tests tease out the API of the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, as well as its behavior. In this point in the development process, the tests serve as a feedback mechanism. Only later, when the tests and the SUT stabilize, will the tests be repurposed (dare I say ‘reused'?) as regression tests. In other words: over time, tests written during TDD have more than one role:</p> <ol> <li>Feedback mechanism <li>Regression test</li></ol> <p>Each test plays one of these roles at a time, but not both.</p> <p>While the purpose of TDD is to evolve the SUT, the process produces two types of artifacts:</p> <ol> <li>Tests <li>Production code</li></ol> <p>Notice how the tests appear <em>before</em> the production code, which is an artifact of the test code.</p> <blockquote> <p>The unit tests are the <em>first</em> client of the production API.</p></blockquote> <p>When the production code is composed into an application, that application becomes the <em>second</em> client, so it <em>reuses</em> the API. This is a very beneficial effect of TDD, and probably one of the main reasons why TDD, if done correctly, produces code of high quality.</p> <p>A colleague once told me (when referring to scale-out) that the hardest step is to go from one server to two servers, and I've later found that principle to apply much more broadly. Generalizing from a single case to two distinct cases is often the hardest step, and it becomes much easier to generalize further from two to an arbitrary number of cases.</p> <p>This explains why TDD is such an efficient process. Apart from the beneficial side effect of producing a regression test suite, it also ensures that at the time the API goes into production, it's already being shared (or reused) between at least two distinct clients. If, at a later time, it becomes necessary to add a third client, the hard part is already done.</p> <p>TDD produces reusable code because the production application reuses the API which were realized by the tests.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e6e9f5bade0d4e08912eaa5b24da1760"> <div class="comment-author">Martin <a href="#e6e9f5bade0d4e08912eaa5b24da1760">#</a></div> <div class="comment-content">I am a junior developer and I am doing TDD for a small project right now and I can only agree. My code looks much better because i really use it instead of making assumptions how it should be used (ADD - Assumption Driven Development)</div> <div class="comment-date">2011-11-10 18:59 UTC</div> </div> <div class="comment" id="b599aa90fb2d499aa274a9c5a9d317b1"> <div class="comment-author"><a href="http://writesoft.wordpress.com">Karep</a> <a href="#b599aa90fb2d499aa274a9c5a9d317b1">#</a></div> <div class="comment-content">Hi all TDD fans.<br> If you are using NUnit for TDD you may find useful NUnit.Snippets NuGet package - &quot;almost every assert is only few keystrokes away&quot; TM ;)<br> http://writesoft.wordpress.com/2011/08/14/nunit-snippets/<br> http://nuget.org/List/Packages/NUnit.Snippets </div> <div class="comment-date">2011-11-11 12:03 UTC</div> </div> <div class="comment" id="0628c767bce948489080ae30c7ea93b0"> <div class="comment-author"><a href="http://www.udidahan.com">Udi Dahan</a> <a href="#0628c767bce948489080ae30c7ea93b0">#</a></div> <div class="comment-content">I think that you're equating *using* a class with reusing a class - the two aren't the same.</div> <div class="comment-date">2011-11-16 23:04 UTC</div> </div> <div class="comment" id="78466c1b528040a5b781958e0e960e4a"> <div class="comment-author">Simple <a href="#78466c1b528040a5b781958e0e960e4a">#</a></div> <div class="comment-content">Hello Mark!<br> <br> Do you use some test coverage software? <br> <br> Is there some free test coverage tools thats worth to use? )<br> <br> <br> <br> <br> </div> <div class="comment-date">2012-05-11 07:11 UTC</div> </div> <div class="comment" id="726e6004ea6c4870b2ec3a5e0162e97e"> <div class="comment-author">Simple <a href="#726e6004ea6c4870b2ec3a5e0162e97e">#</a></div> <div class="comment-content">Or if you dont know free tools - maybe some commercial tools - but not very expensive ))<br> <br> I have found for example &quot;dotcover&quot; from jetbrains - 140 its ok for the company )</div> <div class="comment-date">2012-05-11 09:13 UTC</div> </div> <div class="comment" id="a5628abcf451420fa9304ad4e275b209"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a5628abcf451420fa9304ad4e275b209">#</a></div> <div class="comment-content">I rarely use code coverage tools. Since I develop code almost exclusively with TDD, I know my coverage is good.<br> <br> I still occasionally use code coverage tools when I work in a team environment, so I have nothing against them. When I do, I just use the coverage tool which is built into Visual Studio. When used with the TestDriven.NET Visual Studio add-in it's quite friction-less.</div> <div class="comment-date">2012-05-11 18:56 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Independency https://blog.ploeh.dk/2011/11/08/Independency 2011-11-08T15:29:05+00:00 Mark Seemann <div id="post"> <p>Now that <a href="http://amzn.to/12p90MG">my book</a> about Dependency Injection is out, it's only fitting that I also invert my own dependencies by striking out as an independent consultant/advisor. In the future I'm hoping to combine my writing and speaking efforts, as well as my open source interests, with helping out clients write better code.</p> <p>If you'd like to get help with Dependency Injection or Test-Driven Development, SOLID, API design, application architecture or one of the other topics I regularly cover here on my blog, I'm available as a consultant worldwide.</p> <p>When it comes to Windows Azure, I'll be renewing my alliance with my penultimate employer <a href="http://www.commentor.dk/">Commentor</a>, so you can also hire me as part of larger deal with Commentor.</p> <p>In case you are wondering what happened to <a href="/2011/08/01/JoiningAppHarbor">my employment with AppHarbor</a>, I resigned from my position there because I couldn't make it work with all the other things I also would like to do. I still think <a href="http://appharbor.com">AppHarbor</a> is a very interesting project, and I wish my former employers the best of luck with their endeavor.</p> <p>This has been a message from the blog's sponsor (myself). Soon, regular content will resume.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4c0e927222da495d871ae9f454ed19b8"> <div class="comment-author"><a href="http://compositecode.com">Adron Hall</a> <a href="#4c0e927222da495d871ae9f454ed19b8">#</a></div> <div class="comment-content">Well shux, I was waiting on pins and needles for some magic unicorn stuff from ya! I hear ya though, gotta have that liberty. :) I'm often in the same situation.<br> <br> Best of luck to you, I'll be reading the blog as always.<br> <br> BTW - Got the physical book finally, even though I'm no newb of IoC and such, I'd have loved a solid read when I was learning about the options back in the day. ;)<br> <br> Cheers!</div> <div class="comment-date">2011-11-08 16:11 UTC</div> </div> <div class="comment" id="4f0bc473865d40cc95122f028e72dc3a"> <div class="comment-author"><a href="http://blog.strobaek.org">Karsten</a> <a href="#4f0bc473865d40cc95122f028e72dc3a">#</a></div> <div class="comment-content">Best of luck.<br> <br> As with all other endevours you set your mind to, you will for sure also excel as a free agent.</div> <div class="comment-date">2011-11-08 16:58 UTC</div> </div> <div class="comment" id="52e35411c7774242a965bdb290e4a2eb"> <div class="comment-author">Flemming Laugesen <a href="#52e35411c7774242a965bdb290e4a2eb">#</a></div> <div class="comment-content">Congratulation my friend - looking forward to take advances of you expertice :)<br> <br> Cheers,<br> Flemming</div> <div class="comment-date">2011-11-08 19:45 UTC</div> </div> <div class="comment" id="56cec1c8960b43dab4bf58c356e8bb44"> <div class="comment-author"><a href="http://horsdal.blogspot.com">Christian Horsdal</a> <a href="#56cec1c8960b43dab4bf58c356e8bb44">#</a></div> <div class="comment-content">Congratulations on your decision, and the very best of luck, I'm sure you'll have heaps of succes.</div> <div class="comment-date">2011-11-08 21:52 UTC</div> </div> <div class="comment" id="95ce4e4b469847cea860f8a778ed1b6a"> <div class="comment-author"><a href="http://codecontracts.info">David Allen</a> <a href="#95ce4e4b469847cea860f8a778ed1b6a">#</a></div> <div class="comment-content">I wish you the best with your new adventure. I cannot thank you enough for all I learned from your book on Dependency Injection. <br> Regards,<br> One of your Fans in USA,</div> <div class="comment-date">2011-11-09 03:08 UTC</div> </div> <div class="comment" id="05d451209f714c029c143fd420c6ff99"> <div class="comment-author"><a href="http://www.publicvoid.dk">S&#248;ren Spelling Lund</a> <a href="#05d451209f714c029c143fd420c6ff99">#</a></div> <div class="comment-content">Congrats! Best of luck.</div> <div class="comment-date">2011-11-09 09:00 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. SOLID concrete https://blog.ploeh.dk/2011/10/25/SOLIDconcrete 2011-10-25T15:01:15+00:00 Mark Seemann <div id="post"> <p><a href="https://twitter.com/#!/gregyoung">Greg Young</a> gave a talk at <a href="http://gotocon.com//aarhus-2011/">GOTO Aarhus 2011</a> titled <a href="http://gotocon.com/aarhus-2011/presentation/Developers%20have%20a%20mental%20disorder">Developers have a mental disorder</a>, which was (semi-)humorously meant, but still addressed some very real concerns about the cognitive biases of software developers as a group. While I have no intention to provide a complete resume of the talk, Greg said one thing that made me think a bit (more) about SOLID code. To paraphrase, it went something like this:</p> <blockquote> <p>Developers have a tendency to attempt to solve specific problems with general solutions. This leads to coupling and complexity. Instead of being general, code should be specific.</p></blockquote> <p>This sounds correct at first glance, but once again I think that SOLID code offers a solution. Due to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> each SOLID concrete (pardon the pun) class will tend to very specifically address a very narrow problem.</p> <p>Such a class may <em>implement</em> one (or more) general-purpose interface(s), but the concrete type is specific.</p> <p>The difference between the generality of an interface and the specificity of a concrete type becomes more and more apparent the better a code base applies the <a href="http://codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle</a>. This is best done by defining an API in terms of <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a>, which makes it possible to define a few core abstractions that apply very broadly, while implementations are very specific.</p> <p>As an example, consider <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s ISpecimenBuilder interface. This is a very central interface in AutoFixture (in fact, I don't even know just how many implementations it has, and I'm currently too lazy to count them). As an API, it has proven to be very generally useful, but each concrete implementation is still very specific, like the CurrentDateTimeGenerator shown here:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">CurrentDateTimeGenerator</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">ISpecimenBuilder</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">object</font></span> Create(<span style="color: "><font color="#0000ff">object</font></span> request, </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">ISpecimenContext</font></span> context)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (request != <span style="color: "><font color="#0000ff">typeof</font></span>(<span style="color: "><font color="#2b91af">DateTime</font></span>))</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#2b91af">DateTime</font></span>.Now;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>This is, literally, the entire implementation of the class. I hope we can agree that it's very specific.</p> <p>In my opinion, SOLID is a set of principles that can help us keep an API general while each implementation is very specific.</p> <p>In SOLID code all concrete types are specific.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="cde5d870de094deea32dfdb7d8fce8ee"> <div class="comment-author">nelson <a href="#cde5d870de094deea32dfdb7d8fce8ee">#</a></div> <div class="comment-content">I don't agree with the &quot;Reused Abstractions Principle&quot; article at all. Programming to interfaces provides many benefits even in cases where &quot;one interface, multiple implementations&quot; doesn't apply.<br> <br> For one, ctor injection for dependencies makes them explicit and increases readability of a particular class (you should be able to get a general idea of what a class does by looking at what dependencies it has in its ctor). However, if you're taking in more than a handful of dependencies, that is an indication that your class needs refactoring. Yes, you could accept dependencies in the form of concrete classes, but in such cases you are voiding the other benefits of using interfaces.<br> <br> As far as API writing goes, using interfaces with implementations that are internal is a way to guide a person though what is important in your API and what isn't. Offering up a bunch of instantiatable classes in an API adds to the mental overhead of learning your code - whereas only marking the &quot;entry point&quot; classes as public will guide people to what is important.<br> <br> Further, as far as API writing goes, there are many instances where Class A may have a concrete dependency on Class B, but you wish to hide the methods that Class A uses to talk to Class B. In this case, you may create an interface (Interface B) with all of the public methods that you wish to expose on Class B and have Class B implement them, then add your &quot;private&quot; methods as simple, non-virtual, methods on Class B itself. Class A will have a property of type Interface B, which simply returns a private field of type Class B. Class A can now invoke specific methods on Class B that aren't accessible though the public API using it's private reference to the concrete Class B.<br> <br> Finally, there are many instances where you want to expose only parts of a class to another class. Let's say that you have an event publisher. You would normally only want to expose the methods that have to do with publishing to other code, yet that same class may include facilities that allow you to register handler objects with it. Using interfaces, you can limit what other classes can and can't do when they accept your objects as dependencies.<br> <br> These are instances of what things you can do with interfaces that make them a useful construct on their own - but in addition to all of that, you get the ability to swap out implementations without changing code. I know that often times implementations are never swapped out in production (rather, the concrete classes themselves are changed), which is why I mention this last, but in the rare cases where it has to be done, interfaces make this scenario possible.<br> <br> My ultimate point is that interfaces don't always equal generality or abstraction. They are simply tools that we can use to make code explicit and readable, and allow us to have greater control over method/property accessibility.</div> <div class="comment-date">2011-10-25 18:15 UTC</div> </div> <div class="comment" id="dab31cabd8634b179ccc25b3c430e4ba"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dab31cabd8634b179ccc25b3c430e4ba">#</a></div> <div class="comment-content">The RAP fills the same type of role as unit testing/TDD: theoretically, it's possible to write testable code without writing a single unit test against it, but how do you know?<br> <br> It's the same thing with the RAP: how can you know that it's possible to exchange one implementation of an interface with another if you've never tried it? Keep in mind that Test Doubles don't count because you can always create a bogus implementation of any interface. You could have an interface that's one big leaky abstraction: even though it's an interface, you can never meaningfully replace that single implementation with any other meaningful production implementation.<br> <br> Also: using an interface alone doesn't guarantee the Liskov Substitution Principle. However, by following the RAP, you get a strong indication that you can, indeed, replace one implementation with another without changing the correctness of the code.</div> <div class="comment-date">2011-10-25 18:56 UTC</div> </div> <div class="comment" id="b9352b3d4adc41519d0a245361135912"> <div class="comment-author">nelson <a href="#b9352b3d4adc41519d0a245361135912">#</a></div> <div class="comment-content">That was my point, though. You can use interfaces as a tool to solve problems that have nothing directly to do with substituting implementations. I think people see this as the only usecase for the language construct, which is sad. These people then turn around and claim that you shouldn't be using interfaces at all, except for cases in which substituting implementation is the goal. I think this attitude disregards many other proper uses for the construct; the most important I think is being able to hide implementation details in the public API.<br> <br> If an interface does not satisfy RAP, it does not absolutely mean that interface is invalid. Take the Customer and CustomerImpl types specified in the linked article. Perhaps the Customer interface simply exposes a public, readonly, interface for querying information about a customer. The CustomerImpl class, instantiated and acted upon behind the scenes in the domain services, may specify specific details such as OR/mapping or other behavior that isn't intended to be accessible to client code. Although a slightly contrived example (I would prefer the query model to be event sourced, not mapped to a domain model mapped to an RDBMS), I think this use is valid and should not immediately be thrown out because it does not satisfy RAP.</div> <div class="comment-date">2011-10-25 20:15 UTC</div> </div> <div class="comment" id="1cbdcc70882c44ef9320ae6f4b0fde10"> <div class="comment-author"><a href="http://blog.martindoms.com">Martin Doms</a> <a href="#1cbdcc70882c44ef9320ae6f4b0fde10">#</a></div> <div class="comment-content">On his bio it says that Greg Young writes for Experts Exchange. Maybe he's the one with the mental disorder :P</div> <div class="comment-date">2011-10-26 04:16 UTC</div> </div> <div class="comment" id="8a0ae7172cad4a47b77df21e5223416e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8a0ae7172cad4a47b77df21e5223416e">#</a></div> <div class="comment-content">Nelson, I think we agree :) To me, the RAP is not an absolute rule, but just another guideline/metric. If <em>none</em> of my interfaces have multiple implementations, I start to worry about the quality of my abstractions, but I don't find it problematic if some of my interfaces have only a single implementation.<br> <br> Your discussion about interfaces fit well with the Interface Segregation Principle and the concept of Role Interfaces, and I've also previously described how <a href="/2011/02/28/Interfacesareaccessmodifiers">interfaces act as access modifiers</a>.</div> <div class="comment-date">2011-10-26 08:37 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Checking for exactly one item in a sequence using C# and F# https://blog.ploeh.dk/2011/10/11/CheckingforexactlyoneiteminasequenceusingC#andF# 2011-10-11T14:36:03+00:00 Mark Seemann <div id="post"> <p> Here's a programming issue that comes up from time to time. A method takes a sequence of items as input, like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">void</font></span> Route(<span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#0000ff">string</font></span>&gt; args)</font></pre> </p> <p> While the signature of the method may be given, the <em>implementation</em> may be concerned with finding out whether there is exactly <em>one</em> element in the sequence. (I'd argue that this would be a violation of the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>, but that's another discussion.) By corollary, we might also be interested in the result sets on each side of that single element: no elements and multiple elements. </p> <p> Let's assume that we're required to raise the appropriate event for each of these three cases. </p> <h3 id="1872fe4f3c8946f78cea804db4cd141d"> Naïve approach in C# <a href="#1872fe4f3c8946f78cea804db4cd141d" title="permalink">#</a> </h3> <p> A naïve implementation would be something like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">void</font></span> Route(<span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#0000ff">string</font></span>&gt; args)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> countCategory = args.Count();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">switch</font></span> (countCategory)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> 0:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.RaiseNoArgument();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> 1:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.RaiseSingleArgument(args.Single());</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">default</font></span>:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.RaiseMultipleArguments(args);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> However, the problem with that is that IEnumerable&lt;string&gt; carries no guarantee that the sequence will ever end. In fact, there's a whole category of implementations that keep iterating forever - these are called <a href="http://en.wikipedia.org/wiki/Generator_%28computer_science%29">Generators</a>. If you pass a Generator to the above implementation, it will never return because the Count method will block forever. </p> <h3 id="1afe15a859bb47a9a2f3fedf2ba6e0dd"> Robust implementation in C# <a href="#1afe15a859bb47a9a2f3fedf2ba6e0dd" title="permalink">#</a> </h3> <p> A better solution comes from the realization that we're only interested in knowing about which of the three categories the input matches: No elements, a single element, or multiple elements. The last case is covered if we find at least two elements. In other words, we don't have to read more than <em>at most two</em> elements to figure out the category. Here's a more robust solution: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">void</font></span> Route(<span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#0000ff">string</font></span>&gt; args)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> countCategory = args.Take(2).Count();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">switch</font></span> (countCategory)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> 0:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.RaiseNoArgument();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">case</font></span> 1:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.RaiseSingleArgument(args.Single());</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">default</font></span>:</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.RaiseMultipleArguments(args);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Notice the inclusion of the Take(2) method call, which is the only difference from the first attempt. This will give us <em>at most two</em> elements that we can then count with the Count method. </p> <p> While this is better, it still annoys me that it's necessary with a secondary LINQ call (to the Single method) to extract that single element. Not that it's particularly inefficient, but it still <em>feels</em> like I'm repeating myself here. </p> <p> (We could also have converted the Take(2) iterator into an array, which would have enabled us to query its Length property, as well as index into it to get the single value, but it basically amounts to the same work.) </p> <h3 id="c8fb08fba7704b58a7fc8c9c8734fef9"> Implementation in F# <a href="#c8fb08fba7704b58a7fc8c9c8734fef9" title="permalink">#</a> </h3> <p> In F# we can implement the same functionality in a much more compact manner, taking advantage of pattern matching against native F# lists: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">member</font></font></span><font style="font-size: 10pt"> this.Route args =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">let</font></span> atMostTwo = args |&gt; Seq.truncate 2 |&gt; Seq.toList</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">match</font></span> atMostTwo </font><span style="color: "><font style="font-size: 10pt" color="#0000ff">with</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; | [] <span style="color: "><font color="#0000ff">-&gt;</font></span> onNoArgument.Trigger(Unit.Default)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; | [arg] <span style="color: "><font color="#0000ff">-&gt;</font></span> onSingleArgument.Trigger(arg)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; | _ <span style="color: "><font color="#0000ff">-&gt;</font></span> onMultipleArguments.Trigger(args)</font></pre> </p> <p> The first thing happening here is that the input is being piped through a couple of functions. The truncate method does the same thing as the Take LINQ method does, and the toList method subsequently converts that sequence of at most two elements into a native F# list. </p> <p> The beautiful thing about native F# lists is that they support pattern matching, so instead of first figuring out in which category the input belongs, and then subsequently extract the data in the single element case, we can match and forward the element in a single statement. </p> <p> Why is this important? I don't know… it's just satisfying on an aesthetic level :) </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="22167824ba4f4e748dd24ec36631febf"> <div class="comment-author">a. <a href="#22167824ba4f4e748dd24ec36631febf">#</a></div> <div class="comment-content">string item = null;<br> int count = 0;<br> <br> foreach(var current in args)<br> {<br> item = current;<br> i ++;<br> <br> if (i == 2)<br> {<br> RaiseMultipleArguments(args);<br> return;<br> }<br> }<br> <br> if (i == 1)<br> this.RaiseSingleArgument(item);<br> else<br> RaiseNoArgument();</div> <div class="comment-date">2011-10-11 14:42 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Weakly-typed versus Strongly-typed Message Channels https://blog.ploeh.dk/2011/09/23/Weakly-typedversusStrongly-typedMessageChannels 2011-09-23T09:08:53+00:00 Mark Seemann <div id="post"> <p>Soon after I posted my previous blog post on <a href="/2011/09/19/MessageDispatchingwithoutServiceLocation">message dispatching without Service Location</a> I received an email from Jeff Saunders with some great observations. Jeff has been so kind to allow me to quote his email here on the blog, so here it is:</p> <blockquote> <p>“I enjoyed your latest blog post about message dispatching. I have to ask, though: why do we want weakly-typed messages? Why can't we just inject an appropriate IConsumer&lt;T&gt; into our services - they know which messages they're going to send or receive.</p> <p>“A really good example of this is ISubject&lt;T&gt; from Rx. It implements both IObserver&lt;T&gt; (a message consumer) and IObservable&lt;T&gt; (a message producer) and the default implementation Subject&lt;T&gt; routes messages directly from its IObserver side to its IObservable side. </p> <p>“We can use this with DI quite nicely - I have written an example in .NET Pad: <a href="http://dotnetpad.net/ViewPaste/woTkGk6_GEq3P9xTVEJYZg#c9,c26,">http://dotnetpad.net/ViewPaste/woTkGk6_GEq3P9xTVEJYZg#c9,c26,</a></p> <p>“The good thing about this is that we now have access to all of the standard LINQ query operators and the new ones added in Rx, so we can use a select query to map messages between layers, for instance.</p> <p>“This way we get all the benefits of a weakly-typed IChannel interface, with the added advantages of strong typing for our messages and composability using Rx.</p> <p>“One potential benefit of weak typing that could be raised is that we can have just a single implementation for IChannel, instead of an ISubject&lt;T&gt; for each message type. I don't think this is really a benefit, though, as we may want different propagation behaviour for each message type - there are other implementations of ISubject&lt;T&gt; that call consumers asynchronously, and we could pass any IObservable&lt;T&gt; or IObserver&lt;T&gt; into a service for testing purposes.”</p></blockquote> <p>These are great observations and I think that Rx holds much promise in this space. Basically you can say that in <a href="http://abdullin.com/cqrs">CQRS</a>-style architectures we're already pushing events (and commands) around, so why not build upon what the framework offers?</p> <p>Even if you find the <a href="http://msdn.microsoft.com/en-us/library/dd783449.aspx">IObserver&lt;T&gt;</a> interface a bit too clunky with its <a href="http://msdn.microsoft.com/en-us/library/dd782792.aspx">OnNext</a>, <a href="http://msdn.microsoft.com/en-us/library/dd781657.aspx">OnError</a> and <a href="http://msdn.microsoft.com/en-us/library/dd782982.aspx">OnCompleted</a> methods compared to the strongly typed IConsumer&lt;T&gt; interface, the question still remains: why do we want weakly-typed messages?</p> <p>We don't, necessarily. My previous post wasn't meant as a particular endorsement of a weakly typed messaging channel. It was more an observation that I've seen many variations of this IChannel interface:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IChannel</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Send&lt;T&gt;(T message);</font> <font style="font-size: 10pt">}</font></pre> </p> <p>The most important thing I wanted to point out was that while the generic type argument may create the illusion that this is a strongly typed method, this is all it is: an illusion. IChannel isn't strongly typed because you can invoke the Send method with <em>any</em> type of message - and the code will still compile. This is no different than the <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">mechanical distinction between a Service Locator and an Abstract Factory</a>.</p> <p>Thus, when defining a channel interface I normally prefer to make this explicit and instead model it like this:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IChannel</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Send(<span style="color: "><font color="#0000ff">object</font></span> message);</font> <font style="font-size: 10pt">}</font></pre> </p> <p>This achieves exactly the same and is more honest.</p> <p>Still, this doesn't really answer Jeff's question: is this preferable to one or more strongly typed IConsumer&lt;T&gt; dependencies?</p> <p>Any high-level application entry point that relies on a weakly typed IChannel can get by with a single IChannel dependency. This is flexible, but (just like with <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator</a>), it might hide that the client may have (or (d)evolve) too many responsibilities.</p> <p>If, instead, the client would rely on <a href="http://stackoverflow.com/questions/2420193/dependency-injection-constructor-madness/2420245#2420245">strongly typed dependencies it becomes much easier to see</a> if/when it violates the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>.</p> <p>In conclusion, I'd tend to prefer strongly typed <a href="http://www.eaipatterns.com/DatatypeChannel.html">Datatype Channels</a> instead of a single weakly typed channel, but one shouldn't underestimate the flexibility of a general-purpose channel either.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2ef3dbfcea1a41b0828a450b878536f4"> <div class="comment-author">Jeff <a href="#2ef3dbfcea1a41b0828a450b878536f4">#</a></div> <div class="comment-content">Thanks for the response, Mark! We are in full agreement.</div> <div class="comment-date">2011-09-23 09:20 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Message Dispatching without Service Location https://blog.ploeh.dk/2011/09/19/MessageDispatchingwithoutServiceLocation 2011-09-19T14:44:47+00:00 Mark Seemann <div id="post"> <p> Once upon a time I wrote a blog post about why <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator is an anti-pattern</a>, and ever since then, I occasionally receive rebuffs from people who agree with me in principle, but think that, still: in various special cases (the argument goes), Service Locator does have its uses. </p> <p> Most of these arguments actually stem from mistaking the <a href="/2011/08/25/ServiceLocatorrolesvs.mechanics">mechanics for the role of a Service Locator</a>. Still, once in a while a compelling argument seems to come my way. One of the <a href="http://smart421.wordpress.com/2011/08/12/to-iservicelocator-or-not/">most insistent arguments concerns message dispatching</a> - a pattern which is currently gaining in prominence due to the increasing popularity of <a href="http://abdullin.com/cqrs/">CQRS</a>, <a href="http://www.udidahan.com/2009/06/14/domain-events-salvation/">Domain Events</a> and kindred architectural styles. </p> <p> In this article I'll first provide a quick sketch of the scenario, followed by a typical implementation based on a ‘Service Locator', and then conclude by demonstrating why a Service Locator isn't necessary. </p> <h3 id="d44fdf738fa24e8f8c986075478955ea"> Scenario: Message Dispatching <a href="#d44fdf738fa24e8f8c986075478955ea" title="permalink">#</a> </h3> <p> Appropriate use of message dispatching internally in an application can significantly help decouple the code and make roles explicit. A common implementation utilizes a messaging interface like this one: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IChannel</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Send&lt;T&gt;(T message);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Personally, I find that the generic typing of the Send method is entirely redundant (not to mention heavily reminiscent of the <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">shape of a Service Locator</a>), but it's very common and not particularly important right now (but more about that later). </p> <p> An application might use the IChannel interface like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> registerUser = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">RegisterUserCommand</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Guid</font></span>.NewGuid(),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#a31515">"Jane Doe"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#a31515">"password"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#a31515">"jane@ploeh.dk"</font></span>);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">this</font></font></span><font style="font-size: 10pt">.channel.Send(registerUser);</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font style="font-size: 10pt" color="#008000">// ...</font></span> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> changeUserName = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ChangeUserNameCommand</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; registerUser.UserId,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#a31515">"Jane Ploeh"</font></span>);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">this</font></font></span><font style="font-size: 10pt">.channel.Send(changeUserName);</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font style="font-size: 10pt" color="#008000">// ...</font></span> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> resetPassword = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ResetPasswordCommand</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; registerUser.UserId);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">this</font></font></span><font style="font-size: 10pt">.channel.Send(resetPassword);</font></pre> </p> <p> Obviously, in this example, the channel variable is an injected instance of the IChannel interface. </p> <p> On the receiving end, these messages must be dispatched to appropriate consumers, which must all implement this interface: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> <span style="color: "><font color="#2b91af">IConsumer</font></span>&lt;T&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Consume(T message);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Thus, each of the command messages in the example have a corresponding consumer: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">RegisterUserConsumer</font></span> : <span style="color: "><font color="#2b91af">IConsumer</font></span>&lt;<span style="color: "><font color="#2b91af">RegisterUserCommand</font></span>&gt;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">ChangeUserNameConsumer</font></span> : <span style="color: "><font color="#2b91af">IConsumer</font></span>&lt;<span style="color: "><font color="#2b91af">ChangeUserNameCommand</font></span>&gt;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">ResetPasswordConsumer</font></span> : <span style="color: "><font color="#2b91af">IConsumer</font></span>&lt;<span style="color: "><font color="#2b91af">ResetPasswordCommand</font></span>&gt;</font></pre> </p> <p> This certainly <em>is</em> a very powerful pattern, so it's often used as an argument to prove that Service Locator is, after all, not an anti-pattern. </p> <h3 id="d0b4617c83c14670bfa810e4c3ed8ce6"> Message Dispatching using a DI Container <a href="#d0b4617c83c14670bfa810e4c3ed8ce6" title="permalink">#</a> </h3> <p> In order to implement IChannel it's necessary to match messages to their appropriate consumers. One easy way to do this is by employing a DI Container. Here's an example that uses <a href="http://autofac.org">Autofac</a> to implement IChannel, but any other container would do as well: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">AutofacChannel</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IChannel</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IComponentContext</font></span> container;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> AutofacChannel(<span style="color: "><font color="#2b91af">IComponentContext</font></span> container)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (container == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"container"</font></span>);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.container = container;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Send&lt;T&gt;(T message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> consumer = <span style="color: "><font color="#0000ff">this</font></span>.container.Resolve&lt;<span style="color: "><font color="#2b91af">IConsumer</font></span>&lt;T&gt;&gt;();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; consumer.Consume(message);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This class is an <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> from Autofac's IComponentContext interface to the IChannel interface. At this point I can always see the “Q.E.D.” around the corner: “look! Service Locator isn't an anti-pattern after all! I'd like to see you implement IChannel without a Service Locator.” </p> <p> While I'll do the latter in just a moment, I'd like to dwell on the DI Container-based implementation for a moment. </p> <ul> <li>Is it simple? Yes.</li> <li>Is it flexible? Yes, although it has shortcomings.</li> <li>Would I use it like this? Perhaps. It depends :)</li> <li>Is it the only way to implement IChannel? No - see the next section.</li> <li>Does it use a Service Locator? No.</li> </ul> <p> While AutofacChannel uses Autofac (a DI Container) to implement the functionality, it's not (necessarily) a Service Locator in action. This was the point I already tried to get across in my <a href="/2011/08/25/ServiceLocatorrolesvs.mechanics">previous post about the subject</a>: just because its mechanics look like Service Locator it doesn't mean that it <em>is</em> one. In my implementation, the AutofacChannel class is a piece of pure infrastructure code. I even made it a private nested class in my <a href="/2011/07/28/CompositionRoot">Composition Root</a> to underscore the point. The container is still not available to the application code, so is never used in the Service Locator <em>role</em>. </p> <p> One of the shortcomings about the above implementations is that it provides no fallback mechanism. What happens if the container can't resolve the matching consumer? Perhaps there isn't a consumer for the message. That's entirely possible because there are no safeguards in place to ensure that there's a consumer for every possibly message. </p> <p> The shape of the Send method enables the client to send any conceivable message type, and the code still compiles even if no consumer exists. That may look like a problem, but is actually an important insight into implementing an alternative IChannel class. </p> <h3 id="38191c1d7c89498a828142ca511b4274"> Message Dispatching using weakly typed matching <a href="#38191c1d7c89498a828142ca511b4274" title="permalink">#</a> </h3> <p> Consider the IChannel.Send method once again: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">void</font></font></span><font style="font-size: 10pt"> Send&lt;T&gt;(T message);</font></pre> </p> <p> Despite its generic signature it's important to realize that this is, in fact, a weakly typed method (at least when used with type inferencing, as in the above example). Equivalently to a bona fide Service Locator, it's possible for a developer to define a new class (Foo) and send it - and the code still compiles: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">this</font></font></span><font style="font-size: 10pt">.channel.Send(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Foo</font></span>());</font></pre> </p> <p> However, at run-time, this will fail because there's no matching consumer. Despite the generic signature of the Send method, it contains no type safety. This insight can be used to implement IChannel without a DI Container. </p> <blockquote> <p> Before I go on I should point out that I don't consider the following solution intrinsically superior to using a DI Container. However, readers of <a href="http://amzn.to/12p90MG">my book</a> will know that I consider it a very illuminating exercise to try to implement everything with Poor Man's DI once in a while. </p> <p> Using Poor Man's DI often helps unearth some important design elements of DI because it helps to think about solutions in terms of patterns and principles instead of in terms of technology. </p> <p> However, once I have arrived at an appropriate conclusion while considering Poor Man's DI, I still tend to prefer mapping it back to an implementation that involves a DI Container. </p> <p> Thus, the purpose of this section is first and foremost to outline how message dispatching can be implemented without relying on a Service Locator. </p> </blockquote> <p> While this alternative implementation isn't allowed to change any of the existing API, it's a pure implementation detail to encapsulate the insight about the weakly typed nature of IChannel into a similarly weakly typed consumer interface: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IConsumer</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Consume(<span style="color: "><font color="#0000ff">object</font></span> message);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Notice that this is a private nested interface of my Poor Man's DI Composition Root - it's a pure implementation detail. However, given this private interface, it's now possible to implement IChannel like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">PoorMansChannel</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IChannel</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#2b91af">IConsumer</font></span>&gt; consumers;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> PoorMansChannel(<span style="color: "><font color="#0000ff">params</font></span> <span style="color: "><font color="#2b91af">IConsumer</font></span>[] consumers)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.consumers = consumers;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Send&lt;T&gt;(T message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">foreach</font></span> (<span style="color: "><font color="#0000ff">var</font></span> c <span style="color: "><font color="#0000ff">in</font></span> <span style="color: "><font color="#0000ff">this</font></span>.consumers)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c.Consume(message);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Notice that this is another private nested type that belongs to the Composition Root. It loops though all injected consumers, so it's up to each consumer to decide whether or not to do anything about the message. </p> <p> A final private nested class bridges the generically typed world with the weakly typed world: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">Consumer</font></span>&lt;T&gt; : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IConsumer</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IConsumer</font></span>&lt;T&gt; consumer;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Consumer(<span style="color: "><font color="#2b91af">IConsumer</font></span>&lt;T&gt; consumer)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.consumer = consumer;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Consume(<span style="color: "><font color="#0000ff">object</font></span> message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (message <span style="color: "><font color="#0000ff">is</font></span> T)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.consumer.Consume((T)message);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This generic class is another Adapter - this time adapting the generic IConsumer&lt;T&gt; interface to the weakly typed (private) IConsumer interface. Notice that it only delegates the message to the adapted consumer if the type of the message matches the consumer. </p> <p> Each implementer of IConsumer&lt;T&gt; can be wrapped in the (private) Consumer&lt;T&gt; class and injected into the PoorMansChannel class: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> channel = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">PoorMansChannel</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Consumer</font></span>&lt;<span style="color: "><font color="#2b91af">ChangeUserNameCommand</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ChangeUserNameConsumer</font></span>(store)),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Consumer</font></span>&lt;<span style="color: "><font color="#2b91af">RegisterUserCommand</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">RegisterUserConsumer</font></span>(store)),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Consumer</font></span>&lt;<span style="color: "><font color="#2b91af">ResetPasswordCommand</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ResetPasswordConsumer</font></span>(store)));</font></pre> </p> <p> So there you have it: type-based message dispatching without a DI Container in sight. However, it would be easy to use convention-based configuration to scan an assembly and register all IConsumer&lt;T&gt; implementations and wrap them in Consumer&lt;T&gt; instances and use this list to compose a PoorMansChannel instance. However, I will leave this as an exercise to the reader (or a later blog post). </p> <h3 id="668c6127b4c24bd2a6bbb7120b4ab84d"> My claim still stands <a href="#668c6127b4c24bd2a6bbb7120b4ab84d" title="permalink">#</a> </h3> <p> In conclusion, I find that I can still defend my original claim: <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator is an anti-pattern</a>. </p> <p> That claim, by the way, is <a href="http://en.wikipedia.org/wiki/Falsifiability">falsifiable</a>, so I do appreciate that people take it seriously enough by attempting to disprove it. However, until now, I've yet to be presented with a scenario where I couldn't come up with a better solution that didn't involve a Service Locator. </p> <p> Keep in mind that a Service Locator is defined by the role it plays - not the shape of the API. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2f78b02498f5477db8b655a6ba22b7d5"> <div class="comment-author">SO User <a href="#2f78b02498f5477db8b655a6ba22b7d5">#</a></div> <div class="comment-content">I'm not clear on how you would send a command (or inject IChannel) from another class if AutofacChannel is private to your Composition Root.</div> <div class="comment-date">2011-09-19 19:37 UTC</div> </div> <div class="comment" id="43a6faa1888a4dd080aa76758713fe89"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#43a6faa1888a4dd080aa76758713fe89">#</a></div> <div class="comment-content">AutofacChannel (and PoorMansChannel) is a private class that implements a public interface (IChannel). Since IChannel is a public interface, it can be consumed by any class that needs it.</div> <div class="comment-date">2011-09-19 19:46 UTC</div> </div> <div class="comment" id="fc1b9e773d064952b7ae470301dba51b"> <div class="comment-author">Bob walsh <a href="#fc1b9e773d064952b7ae470301dba51b">#</a></div> <div class="comment-content">Hi Mark,<br> <br> I'm helping a .NET vendor improve their blog by finding respected developers who will contribute guest posts. Each post will include your byline, URL, book link (with your Amazon affiliate link) and a small honorarium. It can either be a new post or one of your popular older posts.<br> Being an author myself, I know that getting in front of new audiences boosts sales, generates consulting opportunities and in this case, a little cash. Would you be interested? If so, let me know and I'll set you up.<br> <br> Cheers,<br> <br> Bob Walsh </div> <div class="comment-date">2011-09-19 20:03 UTC</div> </div> <div class="comment" id="04b0354b94a7432481ba8eb08a946ab2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#04b0354b94a7432481ba8eb08a946ab2">#</a></div> <div class="comment-content">Thanks for contacting me about this. Yes, I'd like to discuss this further, but I think we should take this via e-mail so as to not tire all my other readers :) You can email me at mark(guess which sign goes here)seemann.ms.</div> <div class="comment-date">2011-09-19 20:10 UTC</div> </div> <div class="comment" id="a258b5b1bde34ea0b83c64963bdc91c9"> <div class="comment-author">Phil Sandler <a href="#a258b5b1bde34ea0b83c64963bdc91c9">#</a></div> <div class="comment-content">Hey Mark,<br> <br> Looking forward to getting your book later this month.<br> <br> I think it comes down to the definition of Service Locator. I'm not sure that the AutofacChannel example is much different than a common example I come up against, which is a ViewModel factory that more or less wraps a call to the container, and then gets injected as a IViewModelFactory into classes that need to create VMs. I don't feel that this is &quot;wrong,&quot; as I only allow this kind of thing in cases where more explicit injection is significantly more painful. However I do still think of it as Service Location, and it does violate the Three Calls Pattern. As long as I limit the number of places this is allowed and everyone is aware of them, I see little risk in doing it this way. Some might argue it's a slippery slope . . .</div> <div class="comment-date">2011-09-19 20:39 UTC</div> </div> <div class="comment" id="c1667cd7915b42eabbb55a4c59dec25f"> <div class="comment-author">Phil Sandler <a href="#c1667cd7915b42eabbb55a4c59dec25f">#</a></div> <div class="comment-content">Off-topic question: in the Domain Events post you linked (Udi's), he uses a static Dispatcher for events, which gets called directly from the AR. In the comments, you talk about favoring having the Dispatcher injected into the AR.<br> <br> Whether it is a static dependency or an injected instance, it seems unnatural to me to call a service directly from a domain object. I think I've seen you say the same thing, but I'm not sure in what context.<br> <br> Anyway, was wondering if you had any additional thoughts on the subject. I've been struggling with it for a while, and have settled (temporarily) on firing events outside of the domain object (i.e. call the domain.Method, then fire the event from the command handler).<br> </div> <div class="comment-date">2011-09-19 20:48 UTC</div> </div> <div class="comment" id="79e5758f193d43eba2ca18c5c2420591"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#79e5758f193d43eba2ca18c5c2420591">#</a></div> <div class="comment-content">Phil<br> <br> Thanks for writing.<br> <br> It's my experience that in MVVM one definitely needs some kind of factory to create ViewModels. However, there's no reason to define it as a Service Locator. A normal (generic) Abstract Factory is preferable, and achieves the same thing.<br> <br> Regarding the question about whether or not to raise Domain Events from within Entities: it bothered me for a while until I realized that when you move towards CQRS and Event Sourcing, Commands and Events become first-class citizens and this problem tends to go away because you can keep the logic about which commands raises which events decoupled from the Entities. In this architectural style, Entities are immutable, so nothing ever changes <em>within</em> them.<br> <br> In CQRS we have consumers that consume Commands and Events, and typically we have a single consumer which is responsible for receiving a Command and (upon validation) convert it to an Event. Such a consumer is a Service which can easily hold other Services, such as a channel upon which Events can be published.</div> <div class="comment-date">2011-09-20 07:48 UTC</div> </div> <div class="comment" id="762b68a9556b4b3cb1567d1451bf4e0e"> <div class="comment-author">Martin <a href="#762b68a9556b4b3cb1567d1451bf4e0e">#</a></div> <div class="comment-content">Hi Mark, just an off-topic comment: code samples in your posts are almost unreadable in google reader. Somehow there are lots of empty lines and indenting is wrong. Don't know if there is anything you can do about it?<br> Thanks.</div> <div class="comment-date">2011-09-20 14:41 UTC</div> </div> <div class="comment" id="69b824a751d24b399fdd116ef7327f8e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#69b824a751d24b399fdd116ef7327f8e">#</a></div> <div class="comment-content">Martin<br> <br> I had that problem previously, but I thought I fixed it months ago. From where I'm sitting, the code looks OK both in Google Reader on the Web, Google Reader on Android as well as FeedDemon. Can you share exactly where and how it looks unreadable?</div> <div class="comment-date">2011-09-21 06:23 UTC</div> </div> <div class="comment" id="f348dbd8f2994a80bfe9f93e67b73dd0"> <div class="comment-author">Martin <a href="#f348dbd8f2994a80bfe9f93e67b73dd0">#</a></div> <div class="comment-content">Hi Mark, I don't know if my previous post got lost or not. So again, here's a picture of how code snippets look like when I read them via google reader (in FF7, IE9, Chrome15):<br> <br> http://imgur.com/LvCfJ<br> <br> Thanks.</div> <div class="comment-date">2011-09-25 15:06 UTC</div> </div> <div class="comment" id="52de65b2de8845dd9a95c595e2e7b24d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#52de65b2de8845dd9a95c595e2e7b24d">#</a></div> <div class="comment-content">Martin, I agree that the screen shot looks strange, but across multiple machines and browsers I've been unable to reproduce it, so it's quite difficult for me to troubleshoot. Do you have any idea what might cause this problem?</div> <div class="comment-date">2011-09-26 10:48 UTC</div> </div> <div class="comment" id="7211a7c9c11a4bea875628a449136291"> <div class="comment-author">Martin <a href="#7211a7c9c11a4bea875628a449136291">#</a></div> <div class="comment-content">Update: I found the reason for the problem: it seems I was subscribed to the Atom feed (/SyndicationService.asmx/GetAtom). After unsubscribing and resubscribing to the Rss feed (/SyndicationService.asmx/GetRss), the code snippets look OK.<br> <br> Thanks.</div> <div class="comment-date">2011-09-26 13:15 UTC</div> </div> <div class="comment" id="d6f495ae8e5048f9a69716a71840ded0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d6f495ae8e5048f9a69716a71840ded0">#</a></div> <div class="comment-content">Good :) Thanks for the update.</div> <div class="comment-date">2011-09-26 14:20 UTC</div> </div> <div class="comment" id="9f7477459f0c4908be63689b2d754235"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#9f7477459f0c4908be63689b2d754235">#</a></div> <div class="comment-content">Hi Mark, Daniel again - sorry about not responding to your latest mails on the AutoNSubstitute fork - it's not dead on my side, I am just buried in work currently... I hope I can continue working on it in the next weeks.<br> Why I am posting:<br> I am currently designing the architecture of a new application and I want to design my domain models persistent ignorant but still use them directly in the NHibernate mapping to benefit from lazy loading and to not have near identical entity objects. One part of a PI domain model is that the models might rely on services to do their work which get injected using constructor injection. Now, NHibernate needs a default constructor by default but that can be changed (see: http://fabiomaulo.blogspot.com/2008/11/entities-behavior-injection.html). In the middle of this post there is a class implementation called ReflectionOptimizer that is responsible for creating the entities. It uses an injected container to receive an instance of the requested entity type or falls back to the default implementation of NHibernate if that type is unknown to the container.<br> Do you think this is using the container in a service locator role?<br> I think not, because a Poor Man's DI implementation of this class would get a list of factories, one for each supported entity and all of this is pure infrastructure.<br> The biggest benefit of changing the implementation in a way that it receives factories is that it fails fast: I am constructing all factories along with their dependencies in the composition root.<br> <br> <br> What is your view on this matter?</div> <div class="comment-date">2011-10-09 10:14 UTC</div> </div> <div class="comment" id="4765dbc2246f4df393b8fbf8844eefcb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4765dbc2246f4df393b8fbf8844eefcb">#</a></div> <div class="comment-content">Daniel, I think you reached the correct conclusion already. As you say, you could always create a Poor Man's implementation of that factory.<br> <br> It'd be particularly clean if you could inject an Abstract Factory into your NHibernate infrastructure and keep the container itself isolated to the Composition Root. In any case I agree that this sounds like pure infrastructure code, so it doesn't sound like Service Location.<br> <br> However, I'd <a href="http://stackoverflow.com/questions/4835046/why-not-use-an-ioc-container-to-resolve-dependencies-for-entities-business-object/4836790#4836790">think twice about injecting Services into Entities</a> - see also <a href="http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/">this post</a> from <a href="http://misko.hevery.com">Miško Hevery</a>.</div> <div class="comment-date">2011-10-11 16:12 UTC</div> </div> <div class="comment" id="ac86c7ec3d394ec897ce81fc2b822e42"> <div class="comment-author">Simple <a href="#ac86c7ec3d394ec897ce81fc2b822e42">#</a></div> <div class="comment-content">My questions are about Service Bus - I havent found any other place in your blog to ask it =)<br> <br> If Message Bus is used - how is about Layers? Should it be some kind of &quot;Superlayer&quot; (visible for all other layers?)<br> <br> How do you think - in which situation should Message Bus be involved? <br> <br> Should it better be implemented or is there some good products? (C#, not commerce licence)</div> <div class="comment-date">2012-05-04 08:29 UTC</div> </div> <div class="comment" id="e06b00080ed34e6c98966c52c12d9095"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e06b00080ed34e6c98966c52c12d9095">#</a></div> <div class="comment-content">The way we tend to think about messaging-based applications today (e.g. with CQRS, or Udi Dahan-style SOA), messages are only related to the domain models. Thus, any messaging gateway (like an IBus interface or similar) is only required by the domain model.<br> <br> It's not too hard to implement a message bus on top of a queue system, but it might be worth taking a look at NServiceBus or Rebus.</div> <div class="comment-date">2012-05-04 08:46 UTC</div> </div> <div class="comment" id="a1be1df8bf8c4e0cbe3b26e066cc8ca1"> <div class="comment-author"><a href="http://www.kenneth-truyers.net">Kenneth Truyers</a> <a href="#a1be1df8bf8c4e0cbe3b26e066cc8ca1">#</a></div> <div class="comment-content">Hi Mark, sorry about a very late question on this 2 year old post.<br> I have implemented this pattern before and everything is well when the return type is void.<br> So, for dispatching messages, this is a really usefull and flexible pattern<br><br> However, I was looking into implementing the same thing for a query dispatcher. The structure is similar, with the difference that your messages are queries and that the consumer actually returns a result.<br> I do have a working implementation but I cannot get type inference to work on my query dispatcher. That means that every time I call the query dispatcher I need to specify the return type and the query type<br> This may seem a bit abstract, but you can check out it this question on StackoverFlow: <a href="http://stackoverflow.com/questions/21958086/type-inference-with-interfaces-and-generic-constraints">type inference with interfaces and generic constraints</a>.<br> I'm aware that the way I'm doing it there is not possible with c#, but I was wondering if you'd see a pattern that would allow me to do that.<br> What is your view on this matter?<br> Many thanks! </div> <div class="comment-date">2014-02-23 04:41 UTC</div> </div> <div class="comment" id="010b29b554b444ec8340896567970aa8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#010b29b554b444ec8340896567970aa8">#</a></div> <div class="comment-content"> <p> Kenneth, thank you for writing. Your question reminded my of my <a href="/2013/01/07/RoleHints">series about Role Hints</a>, particularly <a href="/2013/01/09/MetadataRoleHint">Metadata</a>, <a href="/2013/01/10/RoleInterfaceRoleHint">Role Interface</a>, and <a href="/2013/01/11/PartialTypeNameRoleHint">Partial Type Name</a> Role Hints. The examples in those posts aren't generically typed, but I wonder if you might still find those patterns useful? </p> </div> <div class="comment-date">2014-02-23 08:11 UTC</div> </div> <div class="comment" id="dad603faf7ab4ed4ab78aae1e84fa46b"> <div class="comment-author"><a href="http://www.martijnburgers.net/">Martijn Burgers</a> <a href="#dad603faf7ab4ed4ab78aae1e84fa46b">#</a></div> <div class="comment-content"> <p>Hi Mark,<br> I am also using this pattern as an in-process mediator for commands, queries and events. My mediator (channel in your case) now uses a dependency resolver, which is a pure wrapper around a DI container and only contains resolve methods. </p> <p> I am now trying to refactor the dependency resolver away by creating separate factories for the command, query and event handlers (in your example this are the consumers). In my current code and also in yours and dozens of other implementations on the net don’t deal with releasing the handlers. My question is should this be a responsibility of the mediator (channel)? I think so because the mediator is the only place that knows about the existence of the handler. The problem I have with my answer is that the release of the handler is always called after a dispatch (send) even though the handler could be used again for sending another command of the same type during the same request (HTTP request for example). This implies that the handler’s lifetime is per HTTP request. </p> <p> Maybe I am thinking in the wrong direction but I would like to hear your opinion about the releasing handler’s problem.<br> Many thanks in advance,<br> Martijn Burgers </p> </div> <div class="comment-date">2014-07-12 08:50 UTC</div> </div> <div class="comment" id="571afec8e13b4f2899ea5cc4e46fa2bf"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#571afec8e13b4f2899ea5cc4e46fa2bf">#</a></div> <div class="comment-content"> <p> Martijn, thank you for writing. The golden rule for decommissioning is that the object responsible for composing the object graph should also be responsible for releasing it. That's what some DI Containers (Castle Windsor, Autofac, MEF) do - typically using a Release method. The reason for that is that only the Composer knows if there are any nodes in the object graph that should be disposed of, so only the Composer can properly decommission an object graph. </p> <p> You can also implement that Resolve/Release pattern using <a href="/2014/06/10/pure-di">Pure DI</a>. If you're writing a framework, <a href="/2014/05/19/di-friendly-framework">you may need to define an Abstract Factory with an associated Release method</a>, but otherwise, you can just release the graph when you're done with it. </p> <p> In the present article, you're correct that I haven't addressed the lifetime management aspect. The Composer here is the last code snippet that composes the PoorMansChannel object. As shown here, the entire object graph has the same lifetime as the PoorMansChannel object, but I don't describe whether or not it's a Singleton, Per Graph, Transient, or something else. However, the code that creates the PoorMansChannel object should also be the code that releases it, if that's required. </p> <p> In <a href="http://amzn.to/12p90MG">my book</a>'s chapter 8, I cover decommissioning in much more details, although I don't cover this particular example. I hope this answer helps; otherwise, please write again. </p> </div> <div class="comment-date">2014-07-12 14:35 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture goes Continuous Delivery with Semantic Versioning https://blog.ploeh.dk/2011/09/06/AutoFixturegoesContinuousDeliverywithSemanticVersioning 2011-09-06T20:34:42+00:00 Mark Seemann <div id="post"> <p>For the last couple of months I've been working on setting up <a href="http://autofixture.codeplex.com/">AutoFixture</a> for Continuous Delivery (thanks to the nice people at <a title="http://teamcity.codebetter.com/" href="http://teamcity.codebetter.com/">http://teamcity.codebetter.com/</a> for supplying the CI service) and I think I've finally succeeded. I've just pushed some code from my local Mercurial repository, and 5 minutes later the release is <a href="http://autofixture.codeplex.com/releases/view/72947">live on both the CodePlex site</a> as well as <a href="http://nuget.org/List/Search?searchTerm=autofixture">on the NuGet Gallery</a>.</p> <p>The plan for AutoFixture going forward is to maintain Continuous Delivery and switch the versioning scheme from ad hoc to <a href="http://semver.org/">Semantic Versioning</a>. This means that obviously you'll see releases much more often, and versions are going to be incremented much more often. Since the previous release the current release incidentally ended at version 2.2.44, but since the versioning scheme has now changed, you can expect to see 2.3, 2.4 etc. in rapid succession.</p> <p>While I've been mostly focused on setting up Continuous Delivery, <a href="http://www.nikosbaxevanis.com/bonus-bits/">Nikos Baxevanis</a> and <a href="http://megakemp.wordpress.com/">Enrico Campidoglio</a> have been busy writing new features:</p> <ul> <li><a href="http://megakemp.wordpress.com/2011/08/01/anonymous-delegates-in-autofixture/">Support for anonymous delegates</a> <li><a href="http://www.nikosbaxevanis.com/bonus-bits/2011/08/heuristics-for-static-factory-methods-in-autofixture.html">Heuristics for static factory methods</a> <li><a href="http://www.nikosbaxevanis.com/bonus-bits/2011/08/combining-xunit-data-theories.html">Inline AutoData Theories</a> <li><a href="http://megakemp.wordpress.com/2011/09/06/behavior-changes-in-autofixture-2-2-anonymous-numbers/">More anonymous numbers</a></li></ul> <p>Apart from these excellent contributions, other new features are</p> <ul> <li><a href="/2011/04/18/EnumerablesaredynamicalsoinAutoFixture">Added StableFiniteSequenceCustomization</a> <li>Added [FavorArrays], [FavorEnumerables] and [FavorLists] attributes to xUnit.net extension <li>Added a Generator&lt;T&gt; class <li>Added a completely new project/package called <em>Idioms</em>, which contains convention-based tests (more about this later) <li>Probably some other things I've forgotten about…</li></ul> <p>While you can expect to see version numbers to increase more rapidly and releases to occur more frequently, I'm also beginning to think about AutoFixture 3.0. This release will streamline some of the API in the root namespace, which, I'll admit, was always a bit haphazard. For those people who care, I have no plans to touch the API in the Ploeh.AutoFixture.Kernel namespace. AutoFixture 3.0 will mainly target the API contained in the Ploeh.AutoFixture namespace itself.</p> <p>Some of the changes I have in mind will hopefully make the default experience with AutoFixture more pleasant - I'm unofficially thinking about AutoFixture 3.0 as the ‘pit of success' release. It will also enable some of the various <a href="http://autofixture.codeplex.com/workitem/list/basic">outstanding feature requests</a>.</p> <p>Feedback is, as usual, appreciated.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Service Locator: roles vs. mechanics https://blog.ploeh.dk/2011/08/25/ServiceLocatorrolesvs.mechanics 2011-08-25T18:55:12+00:00 Mark Seemann <div id="post"> <p>It's time to take a step back from the whole debate about whether or not <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator</a> is, or isn't, an anti-pattern. It remains my strong belief that it's an anti-pattern, while others disagree. Although everyone is welcome to think differently than me, I've noticed that <a href="http://smart421.wordpress.com/2011/08/12/to-iservicelocator-or-not/">some of the arguments being put forth in defense of Service Locator seem very convincing</a>. However, I believe that in those cases we no longer talk about Service Locator, but something that looks an awful lot like it.</p> <p>Some APIs are easy to confuse with a ‘real' Service Locator. It probably doesn't help that last year I published an article on <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">how to tell the difference between a Service Locator and an Abstract Factory</a>. In this article I may have focused too much on the <em>mechanics</em> of Service Locator, but as <a href="http://lostechies.com/derickbailey/">Derick Bailey</a> was so kind to point out, this hides the <em>role</em> the API might play.</p> <p>To repeat that earlier post, a Service Locator looks like this:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IServiceLocator</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; T Create&lt;T&gt;(<span style="color: "><font color="#0000ff">object</font></span> context);</font> <font style="font-size: 10pt">}</font></pre> </p> <p>All Service Locators I've seen so far look like that, or some variation thereof, but that doesn't mean that the relationship is transitive. Just because an API <em>looks</em> like that it doesn't <em>automatically</em> means that it's a Service Locator.</p> <p>If it was, all DI containers would be Service Locators. As an example, here's Castle Windsor's Resolve method:</p> <p><pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> T Resolve&lt;T&gt;()</font></pre> </p> <p>Even <a href="http://autofixture.codeplex.com/">AutoFixture</a> has an API like that:</p> <p><pre style="margin: 0px"><span style="color: "><font color="#2b91af"><font style="font-size: 10pt">MyClass</font></font></span><font style="font-size: 10pt"> sut = fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">MyClass</font></span>&gt;();</font></pre></p> <p>It has never been my intention to denounce every single DI container available, as well as my own open source framework. Service Locator is ultimately not identified by the <em>mechanics</em> of its API, but by the <em>role</em> it plays.</p> <p>A DI container encapsulated in a <a href="/2011/07/28/CompositionRoot">Composition Root</a> is not a Service Locator - it's an <em>infrastructure</em> component.</p> <p>It <em>becomes</em> a Service Locator if used incorrectly: when <em>application</em> code (as opposed to infrastructure code) actively <em>queries</em> a service in order to be provided with required dependencies, then it has become a Service Locator.</p> <p>Service Locators are spread thinly and pervasively throughout a code base - <em>that</em> is just as much a defining characteristic.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8834a0037a644fe9a8c7e3786777bcd5"> <div class="comment-author">Jan <a href="#8834a0037a644fe9a8c7e3786777bcd5">#</a></div> <div class="comment-content">Sorry to come back to this pattern/anti-pattern discussion but when I get this right, a ServiceLocator per se is not an anti-pattern if used canny and wise.<br> The anti-pattern comes from a DI container used as a ServiceLocator?</div> <div class="comment-date">2011-08-26 08:53 UTC</div> </div> <div class="comment" id="6e8dded7d2b847708192efd549056038"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6e8dded7d2b847708192efd549056038">#</a></div> <div class="comment-content">Service Locator is, in my opinion, <em>always</em> an anti-pattern. However, a class with a Service Locator-like signature does not <em>in itself</em> constitute a Service Locator - it has to be <em>used</em> like a Service Locator to be one.<br> <br> A DI Container is not, in itself, a Service Locator, but it can be used like one. If you do that, it's an anti-pattern, but that doesn't mean that any use of a container constitutes an anti-pattern. When a container is used according to the <a href="/2010/09/29/TheRegisterResolveReleasepattern">Register Resolve Release</a> pattern from within a <a href="/2011/07/28/CompositionRoot">Composition Root</a> it's all good.</div> <div class="comment-date">2011-08-26 09:22 UTC</div> </div> <div class="comment" id="fc7087cabe21403fb373299a72959a5b"> <div class="comment-author">wcoenen <a href="#fc7087cabe21403fb373299a72959a5b">#</a></div> <div class="comment-content">I am confused by your use of the word &quot;transitive&quot; here (and also in your book on page 3). Don't you mean &quot;symmetric&quot; instead of &quot;transitive&quot;?</div> <div class="comment-date">2011-08-26 15:34 UTC</div> </div> <div class="comment" id="0c033354fb434efb99800df7744bf618"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0c033354fb434efb99800df7744bf618">#</a></div> <div class="comment-content">Yes, well, nobody's perfect :/ Thanks for pointing that out. My mistake. 'Symmetric' is the correct term - I'll see if I can manage to include that correction in the book. It's close, but I think it'll be possible to correct it.</div> <div class="comment-date">2011-08-28 19:41 UTC</div> </div> <div class="comment" id="b59daa5d66e44eecad27bcc85e9d9b28"> <div class="comment-author"><a href="http://nowhere.com">Nobody in particular</a> <a href="#b59daa5d66e44eecad27bcc85e9d9b28">#</a></div> <div class="comment-content">Hi, Mark,<br> <br> I pop by your blog intermittently: it's good stuff.<br> <br> For some reason, I always had a particular idea of the type of person you are; but for some other reason, I thought that this stemmed from something other than your (excellent) writings.<br> <br> I've just realised what it is.<br> <br> That photo of you, up there on the top right, your, &quot;Contact,&quot; photo.<br> <br> It looks like you have an ear-ring dangling from your left ear.</div> <div class="comment-date">2011-09-03 16:38 UTC</div> </div> <div class="comment" id="fe720322f5fa44d592d2337080dbc989"> <div class="comment-author">Tuan Nguyen <a href="#fe720322f5fa44d592d2337080dbc989">#</a></div> <div class="comment-content">Hi Mark,<br> <br> I have one concern about Composition Root. For WPF applications, you said that the Composition Root is at OnStartUp. So, if I want to compose the main window and all other windows (with their view models) in the app at once and one place only (I mean at OnStartUp). How could I do? Thanks in advance!</div> <div class="comment-date">2011-12-04 18:28 UTC</div> </div> <div class="comment" id="8b25bf6fee7549f08c90c6629960cab9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8b25bf6fee7549f08c90c6629960cab9">#</a></div> <div class="comment-content">Composing an object graph in WPF is no different than in any other application. In WPF, using the MVVM pattern, you shouldn't compose Windows but rather ViewModels. In the case where the ViewModels <a href="http://stackoverflow.com/questions/1943576/is-there-a-pattern-for-initializing-objects-created-via-a-di-container/1945023#1945023">rely on run-time values, you can use an Abstract Factory</a>.</div> <div class="comment-date">2011-12-05 10:46 UTC</div> </div> <div class="comment" id="e584670272284ab89a56ec55f2aad364"> <div class="comment-author">Tuan Nguyen <a href="#e584670272284ab89a56ec55f2aad364">#</a></div> <div class="comment-content">Thank you for your answer. I still have another question about your book. I am reading on Chapter 14 - Part 4. You said that Unity doesn't have built-in support to auto-registration. So, you suggested using reflection and Register method. I tried to apply your solution to my example with Unity 2.3 but it didn't work. Can you investigate where I am wrong? Following is the source code:<br> <br> namespace SimpleCSharpApp<br> {<br> class Program<br> {<br> static void Main(string[] args)<br> {<br> // resolve object using Unity<br> IUnityContainer container = new UnityContainer();<br> foreach (var t in typeof(Program).Assembly.GetExportedTypes())<br> {<br> if (typeof(IMessageWriter).IsAssignableFrom(t))<br> {<br> container.RegisterType(typeof(IMessageWriter), t, t.FullName);<br> }<br> }<br> container.Resolve&lt;Salutation&gt;().Exclaim();<br> Console.ReadLine();<br> } <br> }<br> <br> public class Salutation<br> {<br> private readonly IMessageWriter writer;<br> public Salutation(IMessageWriter writer)<br> {<br> this.writer = writer;<br> }<br> <br> public void Exclaim()<br> {<br> writer.Write(&quot;Hello DI!&quot;);<br> }<br> }<br> <br> public interface IMessageWriter<br> {<br> void Write(string message);<br> }<br> <br> public class ConsoleMessageWriter : IMessageWriter<br> {<br> public void Write(string message)<br> {<br> Console.WriteLine(message);<br> }<br> }<br> <br> }<br> </div> <div class="comment-date">2011-12-07 18:06 UTC</div> </div> <div class="comment" id="20401ed368a04dd494e335cc99f52623"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#20401ed368a04dd494e335cc99f52623">#</a></div> <div class="comment-content">Does the code download for the book work for you?</div> <div class="comment-date">2011-12-07 18:21 UTC</div> </div> <div class="comment" id="2b67f01ca4114da499a77ca401928338"> <div class="comment-author"><a href="http://google.com">Harold</a> <a href="#2b67f01ca4114da499a77ca401928338">#</a></div> <div class="comment-content">_, Doing some searching as well as noticed your website appears a bit all messed up during my K-meleon web browser. However luckily barely anybody uses it any longer however you might want to consider it.</div> <div class="comment-date">2012-04-03 11:49 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Joining AppHarbor https://blog.ploeh.dk/2011/08/01/JoiningAppHarbor 2011-08-01T14:03:10+00:00 Mark Seemann <div id="post"> <p>I'm pleased to announce that I'll be joining <a href="http://appharbor.com">AppHarbor</a> as a developer. With my long-standing interest in TDD and OOD as well as my more recent interests in open-source .NET software, distributed source control systems, Continuous Delivery etc. AppHarbor seems like a perfect match for me.</p> <p>Although AppHarbor is very attractive to me, this has been a difficult decision as <a href="http://www.commentor.dk/">Commentor</a> has been a great employer. However, despite great customers I just don't feel like consulting at the moment. Since Safewhere went out of business I've been writing much less code than I'd liked, so when presented with an opportunity to join such a congenial outfit as AppHarbor I had few doubts.</p> <p>I'll still be working out of Copenhagen, Denmark, and I also expect to keep up my usual community engagement at home as well as abroad.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="df5ec6783c3441c9b65edeeb301f4bad"> <div class="comment-author">Nikolaj Winnes <a href="#df5ec6783c3441c9b65edeeb301f4bad">#</a></div> <div class="comment-content">BRAVO! gratulerer</div> <div class="comment-date">2011-08-01 14:07 UTC</div> </div> <div class="comment" id="68261790a77a4d398cb4aece7c6432bc"> <div class="comment-author"><a href="http://beletsky.net">Alexander Beletsky</a> <a href="#68261790a77a4d398cb4aece7c6432bc">#</a></div> <div class="comment-content">Thats a great reenforcement for AppHarbor :) Good luck on new place! </div> <div class="comment-date">2011-08-01 15:26 UTC</div> </div> <div class="comment" id="ad8361f58f7048e89216bb6b04a50d61"> <div class="comment-author"><a href="http://martinsaspects.blogspot.com">Martin Rosen-Lidholm</a> <a href="#ad8361f58f7048e89216bb6b04a50d61">#</a></div> <div class="comment-content">Tillykke!</div> <div class="comment-date">2011-08-01 17:26 UTC</div> </div> <div class="comment" id="acd96388b03f444591db24926f5565bf"> <div class="comment-author"><a href="http://blogs.teamb.com/craigstuntz/">Craig Stuntz</a> <a href="#acd96388b03f444591db24926f5565bf">#</a></div> <div class="comment-content">Sounds good; have fun!</div> <div class="comment-date">2011-08-01 17:59 UTC</div> </div> <div class="comment" id="43a9b973bc61441da1f184f16f1e4bbc"> <div class="comment-author"><a href="http://cstrahan.com">Charles Strahan</a> <a href="#43a9b973bc61441da1f184f16f1e4bbc">#</a></div> <div class="comment-content">Congrats!</div> <div class="comment-date">2011-08-01 20:19 UTC</div> </div> <div class="comment" id="210728566628446ca3c8c545f0551036"> <div class="comment-author"><a href="http://nikosbaxevanis.com">Nikos Baxevanis</a> <a href="#210728566628446ca3c8c545f0551036">#</a></div> <div class="comment-content">Fantastic news! Congratulations!</div> <div class="comment-date">2011-08-02 06:43 UTC</div> </div> <div class="comment" id="c525c4f19bfd453a8509cd827507469d"> <div class="comment-author"><a href="http://compositecode.com">Adron Hall</a> <a href="#c525c4f19bfd453a8509cd827507469d">#</a></div> <div class="comment-content">Awesome!! Congratulations, I'm stoked for you &amp; the AppHarbor Team. Those guys rock, and you'll be adding even more oomph to a great team.<br> <br> Looking forward to the results of you guys working together! :)</div> <div class="comment-date">2011-08-13 23:38 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Composition Root https://blog.ploeh.dk/2011/07/28/CompositionRoot 2011-07-28T15:22:04+00:00 Mark Seemann <div id="post"> <p> In <a href="http://amzn.to/12p90MG">my book</a> I describe the Composition Root pattern in chapter 3. This post serves as a summary description of the pattern. </p> <p> The Constructor Injection pattern is easy to understand until a follow-up question comes up: </p> <blockquote> <p> Where should we compose object graphs? </p> </blockquote> <p> It's easy to understand that each class should require its dependencies through its constructor, but this pushes the responsibility of composing the classes with their dependencies <a href="http://www.natpryce.com/articles/000783.html">to a third party</a>. Where should that be? </p> <p> It seems to me that most people are eager to compose as early as possible, but the correct answer is: </p> <blockquote> <p> As close as possible to the application's entry point. </p> </blockquote> <p> This place is called the <em>Composition Root</em> of the application and defined like this: </p> <blockquote> <p> A Composition Root is a (preferably) unique location in an application where modules are composed together. </p> </blockquote> <p> This means that all the application code relies solely on Constructor Injection (or other injection patterns), but is <em>never composed</em>. Only at the entry point of the application is the <a href="/2011/03/04/Composeobjectgraphswithconfidence">entire object graph</a> finally composed. </p> <p> The appropriate entry point depends on the framework: </p> <ul> <li>In console applications it's the Main method</li> <li>In ASP.NET MVC applications it's global.asax and a custom IControllerFactory</li> <li>In WPF applications it's the Application.OnStartup method</li> <li>In WCF it's a custom ServiceHostFactory</li> <li>etc.</li> </ul> <p> (you can read more about framework-specific Composition Roots in chapter 7 of my book.) </p> <p> The Composition Root is an <em>application infrastructure component</em>. </p> <blockquote> <p> Only applications should have Composition Roots. Libraries and frameworks shouldn't. </p> </blockquote> <p> The Composition Root can be implemented with <del datetime="2019-01-30T11:59:12Z">Poor Man's DI</del> <ins><a href="/2014/06/10/pure-di">Pure DI</a></ins>, but is also the (only) appropriate place to use a DI Container. </p> <blockquote> <p> A DI Container should only be referenced from the Composition Root. All other modules should have no reference to the container. </p> </blockquote> <p> <del datetime="2019-01-30T11:59:12Z">Using a DI Container is often a good choice.</del> In that case it should be applied using the <a href="/2010/09/29/TheRegisterResolveReleasepattern">Register Resolve Release</a> pattern entirely from within the Composition Root. </p> <p> Read more in <a href="/dippp">Dependency Injection Principles, Practices, and Patterns</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ce7019a5117d4a638b7b4c1208867f43"> <div class="comment-author">David Martin <a href="#ce7019a5117d4a638b7b4c1208867f43">#</a></div> <div class="comment-content">Reading the pre-release and it's very good. As for the single composition root: what about complex apps with several deep features? These features could be considered by some to be mini-apps in and of themselves. Would it be appropriate to consider each feature to have its own composition root? I'm thinking of Unity's child containers here. Hopefully the question makes sense. As I use your book as ammo at work I come across the particular question. </div> <div class="comment-date">2011-08-02 03:24 UTC</div> </div> <div class="comment" id="bc6b70f521b844b788216a116d35d09b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bc6b70f521b844b788216a116d35d09b">#</a></div> <div class="comment-content">Each application/process requires only a single Composition Root. It doesn't matter how complex the application is.<br> <br> The earlier you compose, the more you limit your options, and there's simply no reason to do that.<br> <br> You may or may not find <a href="/2011/03/04/Composeobjectgraphswithconfidence">this article</a> helpful - otherwise, please write again :)</div> <div class="comment-date">2011-08-02 07:56 UTC</div> </div> <div class="comment" id="7a9a1473e32d42d6b560fbfa20162004"> <div class="comment-author">Erkan Durmaz <a href="#7a9a1473e32d42d6b560fbfa20162004">#</a></div> <div class="comment-content">Hi Mark,<br> <br> My question is quite parallel to David’s. How about we have a complex application that loads its modules dynamically on-demand, and these modules can consist of multiple assemblies. <br> <br> For example, a client application that has a page-based navigation. Different modules can be deployed and undeployed all the time. At design-time we don’t know all the types we wanna compose and we don’t have direct reference to them. <br> <br> Should we introduce some infrastructure code, like David suggested, to let the modules register their own types (e.g. services) to a child container, then we apply “resolve” on the loaded module and dispose the child container when we are done with the module?<br> <br> Looking forward to the final release of your book BTW :-)<br> <br> Thanks<br> </div> <div class="comment-date">2011-09-29 11:57 UTC</div> </div> <div class="comment" id="39c681c0ac2b4f3cace589c2f169e3d4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#39c681c0ac2b4f3cace589c2f169e3d4">#</a></div> <div class="comment-content">The problem with letting each module register their own types to any sort of container is that it tightly couples the modules (and <em>any</em> application in which you might want to use them) to a specific container. Even worse, if you ever need to consume a service provided by one module from another module, you'd have to couple these two to each other. Pretty soon, you'll end up with a tightly coupled mess.<br> <br> One option is to use whichever DI Container you'd like from the Composition Root, and use its XML capabilities to configure the modules into each particular application that needs them. That's always possible, but adds some overhead, and is rarely required.<br> <br> A better option is to simply drop all desired modules in an add-in folder and then use convention-based rules to scan each assembly. The most difficult part of that exercise is that you have to think explicitly about cardinality, but that's always an issue with add-in architectures. The MEF chapter of my book discusses cardinality a bit.</div> <div class="comment-date">2011-10-03 06:18 UTC</div> </div> <div class="comment" id="195cfbb70f0f4ba8b7d9d64f60a2ac8d"> <div class="comment-author"><a href="http://imaginarydevelopment.blogspot.com">Brandon Dimperio</a> <a href="#195cfbb70f0f4ba8b7d9d64f60a2ac8d">#</a></div> <div class="comment-content">I left your book at home, never mind it's here on my desk. I was going to ask what your composition root for asp.net webforms looked like, but here it is on pg 227.<br> <br> I think I'm headed in <br> UI(Asp.net) depends on Presentation layer and a pure interfaces/DTOs (with any serialization attributes/methods needed). so structure/schema of the 'model' lives in that domain-schema layer. domain model depends on the domain-schema layer, but is purely behaviors (methods/logic). In this way there's a sharing of schema/dto between service layer and presentation layer, with the clean separation of all business logic and only business logic living in the domain layer.So outside of the composition root, neither the UI, presentation layer, or resource access layers (public api -&gt; internal adapter-&gt; individual resource) can touch the domain methods or types. Requires we define an interface for the domain model classes I think? Cross cutting-concerns would live in their own assembly.<br> <br> Any thoughts?</div> <div class="comment-date">2011-10-21 16:09 UTC</div> </div> <div class="comment" id="28fc58fc632f426e8d83141dd1dbd981"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#28fc58fc632f426e8d83141dd1dbd981">#</a></div> <div class="comment-content">Why would your UI depend on DTOs? A Data Transfer Object plays the role of a boundary data carrier, just like View Model might do - they fit architecturally at the same place of application architecture: <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">at the boundary</a>. If you base your inner architecture upon DTOs you'll be headed in the direction of an <a href="http://martinfowler.com/bliki/AnemicDomainModel.html">Anemic Domain Model</a>, so that doesn't sound a particularly good idea to me...</div> <div class="comment-date">2011-10-23 09:05 UTC</div> </div> <div class="comment" id="73f9e8fdedba4883995bafbf9f95f576"> <div class="comment-author"><a href="http://imaginarydevelopment.blogspot.com">Brandon Dimperio</a> <a href="#73f9e8fdedba4883995bafbf9f95f576">#</a></div> <div class="comment-content">I'm only up to reading mid-way through your chapter on doing it right (and jumped to the asp.net webforms composition root after reading this blog post), but here is more of my reasoning.<br> <br> <i>The catch comes when you look at the behavior, and you realize that there is hardly any behavior on these objects, making them little more than bags of getters and setters. Indeed often these models come with design rules that say that you are not to put any domain logic in the the domain objects. </i><a href="http://martinfowler.com/bliki/AnemicDomainModel.html">AnemicDomain Models</a> - Martin Fowler<br> <br> <br> <i>It becomes a 'real' domain model when it contains all (or most) of the behaviour that makes up the business domain (note I'm emphasising business logic, not UI or other orthogonal concerns).</i> <a href="http://stackoverflow.com/questions/1805641/anemic-domain-model-versus-domain-model/1807593#1807593">Anemic Domain Models</a> - Stack overflow<br> <br> I don't feel that this is headed into an Anemic domain model as the domain model assembly would contain all behaviors specific to the domain.<br> <br> <i> Designs that share very little state or, even better, have no state at all tend to be less prone to hard to analyze bugs and easier to repurpose when requirements change.</i> <a href="http://www.colourcoding.net/blog/archive/2009/07/21/gang-of-four-anti-patterns.aspx">Blog article</a> - makes sense but i could be missing multiple boats here.<br> <br> The domain would have zero state, and would depend on interfaces not DTOs. This would make test-ability and analysis simple.<br> <br> Motivation for DTOs in the interfaces layer: presentation and persistence could all share the DTOs rather than having them duplicated in either place.<br> Barring that perceived gain, then the shared layer across all would just be interfaces.<br> <br> The UI could depend on interfaces only, but the same assembly would contain DTOs that the presentation layer and any persistence could share without having to be duplicated/rewritten. So the domain is completely persistence and presentation ignorant.</div> <div class="comment-date">2011-10-25 00:17 UTC</div> </div> <div class="comment" id="7ae20f60258a4ba1911c4f75a41f153a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7ae20f60258a4ba1911c4f75a41f153a">#</a></div> <div class="comment-content">It may just be that we use the terminology different, but according to <a href="http://www.amazon.com/gp/product/0321127420/ref=as_li_qf_sp_asin_tl?ie=UTF8&tag=ploeh-20&linkCode=as2&camp=217145&creative=399369&creativeASIN=0321127420">Patterns of Enterprise Application Architecture</a> &quot;a Data Transfer Object is one of those objects our mothers told us never to write. It's often little more than a bunch of fields and the getters and setters for them.&quot;<br> <br> The purpose of a DTO is &quot;to transfer multiple items of data between two processes in a single method call.&quot; That is (I take it) not what you are attempting to do here.<br> <br> Whether or not you implement the business logic in the same assembly as your DTOs has nothing to with avoiding an Anemic Domain Model. The behavior must be defined by the object that holds the data.</div> <div class="comment-date">2011-10-25 06:46 UTC</div> </div> <div class="comment" id="7c3de6cba1f448aba9a53f469fef2356"> <div class="comment-author">Jordan Morris <a href="#7c3de6cba1f448aba9a53f469fef2356">#</a></div> <div class="comment-content">I appreciate your exhortations to good architecture, but I am struggling to apply them in my situation.<br> <br> I am writing a WPF system tray application which has a primary constraint to keep lean, especially in terms of performance and memory usage. This constraint, along with the relatively low complexity of the application, means that I cannot justify the overhead of MVVM. In the absence of a prescribed architecture for building the object graph, I could set one up 'manually' in Application.OnStartup. Presumably it would mean instantiating all the Window objects (with dependences), which are utilised from time to time by the user.<br> <br> However, I have a problem with these Window instance sitting in memory, doing nothing, 90% of the time. It seems much more sensible to me to instantiate these Window objects in the events where the user asks for them. Yet, I will want to maintain the inversion of control, so how can I avoid accessing the DI container in multiple points in my app?</div> <div class="comment-date">2013-01-12 10:09 UTC</div> </div> <div class="comment" id="739139aaeaca402aa8fda056327513b0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#739139aaeaca402aa8fda056327513b0">#</a></div> <div class="comment-content">Jordan, would the <a href="/2011/03/04/Composeobjectgraphswithconfidence">Virtual Proxy pattern be of help here?</a><br> <br> You can use one of the solutions outlined in <a href="/2013/01/07/RoleHints">my recent series on Role Hints</a> to avoid referencing a container outside of the Composition Root.</div> <div class="comment-date">2013-01-12 21:17 UTC</div> </div> <div class="comment" id="714d47da37a947809874563a352ef4d3"> <div class="comment-author">Jordan Morris <a href="#714d47da37a947809874563a352ef4d3">#</a></div> <div class="comment-content">Hi Mark<br> <br> I wanted to post the following comment on your linked page, here<br> /2011/03/04/Composeobjectgraphswithconfidence<br> but I am encountering a page error on submission.<br> <br> The above article suggests a great framework for delayed construction of resources, however I have a different reason for wanting this than loading an assembly.<br> <br> In my scenario I have occasionally-needed Window objects with heavy GUIs, presenting lots of data. The GUI and data only need to be in memory while the Window is displayed. The lazy loading approach seems to apply, except it leaves me with a couple of questions.<br> <br> 1) When the window is closed again, I want to release the resources. How can I unload and reload it again?<br> <br> 2) What would this line look like when using an IoC like Ninject?<br> <br> &gt; new Lazy&lt;ISolo&lt;IMarker&gt;&gt;(() =&gt; new C1(<br> <br> An alternative I am considering is putting classes defining events in the object graph, instead of the windows themselves. These event classes could be constructed with instances of dependencies which will be used in the event methods to construct Windows when needed. The dependancies I am wanting to inject to maintain inversion of control are light-weight objects like persistance services which will exist as singletons in memory anyway (due to the way most IoC's work). Do you see any problem with this?<br> <br> Many thanks.</div> <div class="comment-date">2013-01-13 23:07 UTC</div> </div> <div class="comment" id="ab5e7abb85bc4f849a7a34a6fc68d3c5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ab5e7abb85bc4f849a7a34a6fc68d3c5">#</a></div> <div class="comment-content">I'll refer you to section 6.2 in <a href="http://amzn.to/12p90MG">my book</a> regarding lifetime management and the closing of resources. However, if things aren't disposable, you can also just let them go out of scope again and let the garbage collector take care of cleaning up.<br> <br> I can't help you with Ninject.<br> <br> </div> <div class="comment-date">2013-01-18 10:03 UTC</div> </div> <div class="comment" id="9b431d82da7c4e469495d2481a1e91ad"> <div class="comment-author">Allan Friis Hansen <a href="#9b431d82da7c4e469495d2481a1e91ad">#</a></div> <div class="comment-content"> I would like to touch and expand upon the subject David Martin raised regarding complex applications with deep features.<br> You make the point that only the composition root should register dependencies of various layers and components. And thereby only the project containing the composition root would have references to the DI container being used <br> <br> In a couple of projects I've worked on we have multiple applications and thereby composition root, being websites, service endpoint, console applications etc. giving us around 10 application endpoints that needs to have their own composition roots. But at the same time we work with a components based design which nicely encapsulates different aspects of the system. <br> <br> If I were to only do my registration code within the place of the composition roots, then applications using the same components would have to register exactly the same dependencies per compontent, leading to severe code duplication<br> At this point we've gone with a solution where every component has their own registration class which the composition root's then register, so basically out composition roots only compose another level of composition roots. I hope it makes sense!<br> <br> What I'm explaining here directly violates the principles you make in this post. Which I already had I sense it would. But at the same time I don't know how else to manage this situation effectively. Do you have any ideas for how to handle this type of situation? <br> </div> <div class="comment-date">2014-08-02 22:28 UTC</div> </div> <div class="comment" id="3c37390024b34873a298e918387c57bf"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3c37390024b34873a298e918387c57bf">#</a></div> <div class="comment-content"> <p> Allan, thank you for writing. In the end, you'll need to do what makes sense to you, and based on what you write, it may make sense to do what you do. Still, my first reaction is that I would probably tend to <a href="/2014/08/07/why-dry">worry less about duplication</a> than you seem to do. However, I don't know your particular project: neither how big your Composition Roots are, or how often they change. In other words, it's easy enough for me to say that a bit of duplication is okay, but that may not fit your reality at all. </p> <p> Another point is that, in my experience at least, even if you have different applications (web sites, services, console applications, etc.) as part of the same overall system, and they share code, the way they <em>should</em> be composed tend to diverge the longer they live. This is reminiscent of Mathias Verraes' point that <a href="http://verraes.net/2014/08/dry-is-about-knowledge/">duplication may be coincidental</a>, but that the duplicated code may deviate from each other in the future. This could also happen for such applications; e.g. at one point, you may wish to <a href="/2010/09/20/InstrumentationwithDecoratorsandInterceptors">instrument</a> some of your web site's dependencies, but not your batch job. </p> <p> My best advice is to build smaller systems, in order to keep complexity down, and then build more of them, rather than building big systems. Again, that advice may not be useful in your case... </p> <p> Another option, since you mention component-based design, is to <a href="/2012/11/06/WhentouseaDIContainer">move towards a convention-based approach</a>, so that you don't have to maintain those Composition Roots at all: just drop in the binaries, and let your DI Container take care of the rest. Take a look at the Managed Extensibility Framework (MEF) for inspiration. Still, while MEF exposes some nice ideas, and is a good source of inspiration, I would personally chose to do something like this with a proper DI Container that also supports run-time Interception and programmable Pointcuts. </p> <p> In the end, I think I understand your problem, but my overall reaction is that you seem to have a problem you shouldn't have, so my priority would be: <ol> <li>Remove the problem altogether.</li> <li>Live with it.</li> <li>If that's not possible, solve it with technology.</li> </ol> </p> </div> <div class="comment-date">2014-08-08 12:02 UTC</div> </div> <div class="comment" id="fb01283395284a36b98c6213d482367d"> <div class="comment-author"><a href="http://blog.majcica.com">Mario Majcica</a> <a href="#fb01283395284a36b98c6213d482367d">#</a></div> <div class="comment-content"> <p> It seems that I have a certan inclination for reopening <a href="/2012/03/26/IQueryableTisTightCoupling">"old"</a> posts. Still the may be considered evergreens! :) Now back on topic. </p> <p> When it comes to the well known frameworks as ASP.NET MVC, Web.API, WCF, it is quite clear on where and how to set our composition root. But what if we do not have a clear "entry point" to our code? Imagine that you are writing an SDK, laying down some classes that will be used by other developers. Now, you have a class that exposes the following constructor. <p> public MyClass(IWhatEver whatEver) </p> Consider also that who is going to use this classes has no idea about IWhatEver nor it should have. For make the usage of MyClass as simple as possible, we I should be able to instatiate MyClass via the parameterless constructor. I had the idea of making the constructor that is used for DI, internal, and the only publically available one to be the paramtereless constructor. Then fetch somehow the instances of my dependencies in the paramterless constructor. Now imagine that I have several of classes as MyClass and that I do have a common set of "services" that are injected inside of them. Now, my questions are. <ol type="1"> <li> Can I still have the single composition root? </li> <li> How do I trigger the composition? </li> <li> Does recreating the composition root per each "high level" class has a significant performance impact (considering a dependency tree of let's say 1000 objects). </li> <li> Is there a better way (pattern) to solve a problem like this? </li> </ol> Some of this questions may be slightly off topic, still the main question is, how to deal with the composition root in cases like this cases. Hoping you will make some clarity, I do thank you in advance. </div> <div class="comment-date">2014-09-18 07:17 UTC</div> </div> <div class="comment" id="e75096f63eac4349aa61314f55b80bd7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e75096f63eac4349aa61314f55b80bd7">#</a></div> <div class="comment-content"> <p> Mario, thank you for writing. If you're writing an SDK, you are writing either a library or a framework, so the Composition Root pattern isn't appropriate (as stated above). Instead, consider the guidelines for writing <a href="/2014/05/19/di-friendly-library">DI friendly libraries</a> or <a href="/2014/05/19/di-friendly-framework">DI friendly frameworks</a>. </p> <p> So, in order to answer your specific questions: </p> <p> <strong>1:</strong> Conceptually, there should only be a <em>single</em> Composition Root, but it belongs to the application, so as a library developer, then no: 'you' can't have any Composition Root. </p> <p> <strong>2:</strong> A library shouldn't trigger any composition, but <a href="/2014/05/19/di-friendly-library">as I explain in my guidelines for writing DI friendly libraries</a>, you can provide Facades to make the learning curve gentle. For frameworks, you may need to <a href="/2014/05/19/di-friendly-framework">provide appropriate Abstract Factories</a>. </p> <p> <strong>3:</strong> The <a href="/2011/03/04/Composeobjectgraphswithconfidence">performance impact of Composition shouldn't be a major concern</a>. However, the real problem of attempting to apply the Composition Root pattern where it doesn't apply is that <a href="http://stackoverflow.com/a/9503612/126014">it increases coupling</a> and takes away options that the client developer may want to utilize; e.g. how can a client developer <a href="/2010/09/20/InstrumentationwithDecoratorsandInterceptors">instrument a sub-graph with Decorators</a> if that sub-graph isn't available? </p> <p> <strong>4: </strong> There are better ways to design <a href="/2014/05/19/di-friendly-library">DI friendly libraries</a> and <a href="/2014/05/19/di-friendly-framework">DI friendly frameworks</a>. </p> </div> <div class="comment-date">2014-09-18 08:12 UTC</div> </div> <div class="comment" id="1c785ded6e254c41a874624b0e2da1ed"> <div class="comment-author"><a href="http://blog.majcica.com">Mario Majcica</a> <a href="#1c785ded6e254c41a874624b0e2da1ed">#</a></div> <div class="comment-content"> <p> Thank you for your quick and valuable replay. </p> <p> I read your posts and made some toughs about them. Still I am not convinced that what is described in that posts does tackle my problem. Even more I'm realizing that is less and less relevant to the topic of composition root. However, let me try describe a situation. </p> <p> I do plan to write an SDK (a library that will allow developers to interact with my application and use some of it's functionality). Consider that I'm exposing a class called FancyCalculator. FancyCalculater needs to get some information from the settings repository. Now, I have a dependency on ISettingsRepository implementation and it is injected via a constructor into my FancyCalculator. Nevertheless, who is going to use the FancyCalculator, doesn't know and he shouldn't know anything about ISettingsRepository and it's dependency tree. He is expected only to make an instance of FancyCalculator and call a method on it. I do have a single implementation of ISettingsRepository in form of SettingsRepository class. In this case, how do I get to create an instance of SettingsRepository once my FancyCalculator is created? </p> <p> A Factory? Service locator? Something else? </p> <p> Composition root in applications like MVC, WebAPI, etc, is a very nice and clean approach, but what about the situations when we do not have a clean single entry point to the application? </p> <p> Thank you again! </p> </div> <div class="comment-date">2014-09-18 12:57 UTC</div> </div> <div class="comment" id="47c25a0ef223427ea63b0f93f4ce2c8c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#47c25a0ef223427ea63b0f93f4ce2c8c">#</a></div> <div class="comment-content"> <p> Mario, thank you for writing again. As far as I understand, you basically have this scenario: </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;"><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FancyCalculator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ISettingsRepository</span>&nbsp;settingsRepository; }</pre> </p> <p> Then you say: "a dependency on ISettingsRepository implementation and it is injected via a constructor into my FancyCalculator". Okay, that means this: </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;"><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FancyCalculator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ISettingsRepository</span>&nbsp;settingsRepository; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;FancyCalculator(<span style="color:#2b91af;">ISettingsRepository</span>&nbsp;settingsRepository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(settingsRepository&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;settingsRepository&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.settingsRepository&nbsp;=&nbsp;settingsRepository; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> But then you say: "who is going to use the FancyCalculator, doesn't know and he shouldn't know anything about ISettingsRepository and it's dependency tree." </p> <p> That sounds to me like mutually exclusive constraints. Why are you injecting ISettingsRepository into FancyCalculator if you don't want to enable the user to supply any implementation of ISettingsRepository? If you don't want to allow that, the solution is easy: </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;"><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FancyCalculator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ISettingsRepository</span>&nbsp;settingsRepository; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;FancyCalculator() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.settingsRepository&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DefaultSettingsRepository</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If you want the best of both worlds, the solution is the Facade pattern I already described in my <a href="/2014/05/19/di-friendly-library">DI Friendly library</a> article. </p> </div> <div class="comment-date">2014-09-19 19:56 UTC</div> </div> <div class="comment" id="cdee2f3b0b8e435589753b9628f7c966"> <div class="comment-author"><a href="http://blog.majcica.com">Mario Majcica</a> <a href="#cdee2f3b0b8e435589753b9628f7c966">#</a></div> <div class="comment-content"> <p> Hi Mark, You got the example right. The reason of my choices are the following. I do not want user of my library to know about the dependencies for a couple of reason. First of all it needs to be as simple as possible to use. Second thing the dependencies are let's say "internal", so that code is loosely coupled and testable. He should not know even about them or get bothered at any point. Still I do not think it's wise to create instances of the dependencies in the parameterless constructor. Why? Well, I am concerned about maintainability. I would like a to somehow request the default instance of my dependency in the parameterless constructor and get it, so that I do have a single point in my sdk where teh default dependency is specified, and also that I do not need to handle the dependency tree. The fact is that I can't get what this something should be. I re-read the chapter 5 of your book this weekend to see if I can come up with something valid ideas. What I came up with is that I should use the parameterless constructor of each of my classes to handle it's own default dependencies. In this way, resolving the tree should be fine, and it is easy to maintain. Also to prevent user to see the constructors that to require the dependencies to be injected (and confuse it) I tough of declaring these constructors as internal. This are my words translated in code (easier to understand). </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;background:white;"><span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Program</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Main(<span style="color:blue;">string</span>[]&nbsp;args) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Using&nbsp;the&nbsp;SDK&nbsp;by&nbsp;others</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">FancyCalculator</span>&nbsp;calculator&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">FancyCalculator</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Console</span>.WriteLine(calculator.DoSomeThingFancy()); &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ISettingsRepository</span>&nbsp;{&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">ILogging</span>&nbsp;{&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Logging</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ILogging</span>&nbsp;{&nbsp;} <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">SettingsRepository</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ISettingsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ILogging</span>&nbsp;logging; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;SettingsRepository() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.logging&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Logging</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;SettingsRepository(<span style="color:#2b91af;">ILogging</span>&nbsp;logging) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(logging&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;logging&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.logging&nbsp;=&nbsp;logging; &nbsp;&nbsp;&nbsp;&nbsp;} } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FancyCalculator</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ISettingsRepository</span>&nbsp;settingsRepository; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;FancyCalculator() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.settingsRepository&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">SettingsRepository</span>(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">internal</span>&nbsp;FancyCalculator(<span style="color:#2b91af;">ISettingsRepository</span>&nbsp;settingsRepository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(settingsRepository&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;settingsRepository&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.settingsRepository&nbsp;=&nbsp;settingsRepository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;DoSomeThingFancy()&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;1;&nbsp;} }</pre> </p> <p> What do you think about a solution like this? What are a bad sides of this approach? Is there any pattern that encapsulates a practise like this? </p> </div> <div class="comment-date">2014-09-22 10:07 UTC</div> </div> <div class="comment" id="e038553290a7415c98c8f19735fc6b53"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e038553290a7415c98c8f19735fc6b53">#</a></div> <div class="comment-content"> <p> Mario, you don't want the user of your library to know about the dependencies, in order to make it easy to use the SDK. That's fine: this goal is easily achieved with one of those Facade patterns <a href="/2014/05/19/di-friendly-library">I've already described</a>. With either Constructor Chaining, or use of the Fluent Builder pattern, you can make it as easy to get started with FancyCalculator as possible: just invoke its parameterless constructor. </p> <p> What then, is your <em>motivation</em> for wanting to make the other constructors internal? What do you gain from doing that? </p> <p> Such code isn't loosely coupled, because only classes internal to the library can use those members. Thus, such a design violates the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>, because these classes aren't open for extension. </p> </div> <div class="comment-date">2014-09-13 6:48 UTC</div> </div> <div class="comment" id="f11302844e274d81adc147c431083980"> <div class="comment-author"><a href="http://rpajak.com">Robert Pajak</a> <a href="#f11302844e274d81adc147c431083980">#</a></div> <div class="comment-content"> <p> Dear Mark, <p> I am pretty sure that Mario uses internal code in order to be able to write unit tests. Probably the interfaces of the dependecies which he is hiding should also be internal. I think it is a good aproach because thanks to it he can safely refactor those interfaces, beacuse it is not a public API and also he can have unit tests. </p> <p> Yes - "Such code isn't loosely coupled, because only classes internal to the library can use those member", however I think that very often stable API is very important for libraries. </p> <p> Could you give any comments on that? </p> </div> <div class="comment-date">2014-09-25 20:10 UTC</div> </div> <div class="comment" id="8407c9cd2bd041af8a99234a371703e6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8407c9cd2bd041af8a99234a371703e6">#</a></div> <div class="comment-content"> <p> Robert, thank you for writing. Your guess sounds reasonable. Even assuming that this is the case, I find it important to preface my answer with the caution that since I don't know the entire context, my answer is, at best, based on mainstream scenarios I can think of. There may be contexts where this approach is, indeed, the best solution, but in my experience, this tends not to be the case. </p> <p> To me, the desire to keep an API stable leads to APIs that are so locked down that they are close to useless unless you happen to be so incredibly lucky that you're <em>right</em> on the path of a 'supported use case'. Over the years, I've worked with many object models in the .NET Base Class Library, where I've wanted it to do something a little out of the ordinary, only to find myself in a cul-de-sac of internal interfaces, sealed classes, or internal virtual methods. Many other people have had the same problems with .NET, which, I believe, has been a contributing factor causing so many of the brightest programmers to leave the platform for other, more open platforms and languages (Ruby, Clojure, JavaScript, Erlang, etc.). </p> <p> The .NET platform is getting much better in this regard, but it's still not nearly as good as it could be. Additionally, .NET is littered with failed technologies that turned out to be completely useless after all (Windows Workflow Foundation, Commerce Server, (early versions of) Entity Framework, etc.). </p> <p> .NET has suffered from a combination of fear of breaking backwards compatibility, combined with Big Design Up-Front (BDUF). The fact that (after all) it's still quite useful is a testament to the people who originally designed it; these people were (or are) some of most skilled people in the industry. Unless you're Anders Hejlsberg, Krzysztof Cwalina, Brad Abrams, or on a similar level, you can't expect to produce a useful and stable API if you take the conservative BDUF approach. </p> <p> Instead, what you can do is to <a href="/2011/11/10/TDDimprovesreusability">use TDD to explore the design of your API</a>. This doesn't guarantee that you'll end up with a <em>stable</em> API, but it does help to make the API as useful as possible. </p> <p> How do you ensure stability, then? One option is to realise that <a href="/2011/02/28/Interfacesareaccessmodifiers">you can use interfaces as access modifiers</a>. Thus, you can publish a library that mostly contains interfaces and a few high-level classes, and then add the public implementations of those interfaces in other libraries, and clearly document that these types are not guaranteed to remain stable. This option may be useful in some contexts, but if you're really exposing an API <a href="/2012/12/18/RangersandZookeepers">to the wild</a>, you probably need a more robust strategy. </p> <p> The best strategy I've been able to identify so far is to realise that you can't design a stable API up front. You <em>will</em> make mistakes, and you will need to deal with these design mistakes. One really effective strategy is to apply the <a href="http://www.martinfowler.com/bliki/StranglerApplication.html">Strangler pattern</a>: leave the design mistakes in, but add new, better APIs as you learn; <a href="/2012/01/03/SOLIDisAppend-only">SOLID is append-only</a>. In my <a href="https://blog.ploeh.dk/encapsulation-and-solid">Encapsulation and SOLID Pluralsight course</a>, I discuss this particular problem and approach in the <em>Append-Only</em> section of the <em>Liskov Substitution Principle</em> module. </p> </div> <div class="comment-date">2014-09-28 13:40 UTC</div> </div> <div class="comment" id="60709234fc3e42688890c011e01bc65f"> <div class="comment-author"><a href="http://rpajak.com">Robert Pajak</a> <a href="#60709234fc3e42688890c011e01bc65f">#</a></div> <div class="comment-content"> <p> Good point Mark! I really like your point of view, in general I agree with you. </p> <p> I will try to 'defend' the internal dependecies, beacuse it may help us explore all the possible reasonable usages of it. </p> <p> 1. Personlly I would hide my dependencies with internal access <b>when I would be quite certain that my design is bad</b>, but I do not have time to fix it, but I will do it for sure in probably near future. </p> <p> 2. Moreover if I would have unit tests without accessing to internal's (so not the case which I was writing in my previous comment), then I should be able to refactor the 'internals' without even touching the unit tests. This is why I see <b>internal depedencies as a refactoring terchnique</b> - especially when working with legacy code. </p> <p> These were some cases for Library/Framework. What about internal access in non-modular Application development that is consists of several projects (dll's)? Personally I try to keep everthing internal by default and only make public interfaces, entities, messages etc. for things that are used between the projects. Then I compose them in a dedicated project (which I name Bootstrapper) which is a composition root and has access to all internal types... If your books covers this topic - please just give a reference. I have not read your book so far, but it is in the queue :) </p> </div> <div class="comment-date">2014-09-28 18:12 UTC</div> </div> <div class="comment" id="759905d3d7a349f9820a945e00e9c1f8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#759905d3d7a349f9820a945e00e9c1f8">#</a></div> <div class="comment-content"> <p> Please note that I'm not saying that internal or private classes are always bad; all I'm saying is that unit testing internal classes (presumably using the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx">[InternalsVisibleTo]</a> attribute) is, in my opinion, not a good idea. </p> <p> You can always come up with some edge cases against a blanket statement like <em>never unit test internal classes</em>, but in the general case, I believe I've already outlined why I think it's a really poor idea to do so. </p> <p> Ultimately, I've never understood the need for the [InternalsVisibleTo] attribute. When you're applying it, you're basically making internal types public to select consumers, with all the coupling that implies. Why not make the types truly public then? </p> <p> As far as I can tell, the main motivation for not making types public is when the creators don't trust their code. If this is the case, making things internal isn't a <em>solution</em>, it's a <em>symptom</em>. Address the problem instead of trying to hide it. Make the types trustworthy by applying <a href="https://blog.ploeh.dk/encapsulation-and-solid">encapsulation</a>. </p> </div> <div class="comment-date">2014-09-29 11:59 UTC</div> </div> <div class="comment" id="9181cc2cd64f46e3b709044d1791eac4"> <div class="comment-author"><a href="http://rpajak.com">Robert Pajak</a> <a href="#9181cc2cd64f46e3b709044d1791eac4">#</a></div> <div class="comment-content"> <p> An example from my last days at work: I marked a class as internal that is wrapping some native library (driver for some hardware) using <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(v=vs.100).aspx">[DllImport]</a>. I want clients of my Library use my classes - not the DLL wrapper - this is why I hide it using internal. However I needed InternalVisibleTo so that I could write integration tests to see if the wrapper really works. </p> <p> Why making public when nobody outside is using it? YAGNI. I only expose when I know when its needed for the clients. Then for those that I have already exposed I need to have backward compability. The less I have exposed the more easily and safely I can refactor my design. And it is not about always bad design. New requirements frequently come in parallel with refactoring the design. This is why I like the Martin Fowler's idea of <a href="http://martinfowler.com/bliki/PublishedInterface.html">Published Interface</a> which is also mentioned in his <a href="http://martinfowler.com/bliki/AccessModifier.html">Access Modifier article</a>. </p> <p> Additionally I always had a feeling that making everything public can affect badly influence the software architecture. The more encaupsulated are the packages the better. And for me internal access is also a mean of encaupsulation at the packaging level. Three days ago <a href="http://www.codingthearchitecture.com/">Simon Brown</a> had a presentation named "Software Architecture vs Code" on <a href="http://devday.pl/">DevDay conference</a>. When he told something like "do not make public classes by default" people were applauding! </p> <p> My rules of a thumb for seting the class access modifiers are: <ul> <li>private - when it is a helper for a class where it is being nested</li> <li>internal - when it used only in the package</li> <li>public - when other packages needs to use it</li> <li>oh and, how I would love to have this Published Interface!</li> </ul> So for each package I can end up with having some public interfaces and classes (like messages and entities) and many internal interfaces and classes which are only seen by the unit tests and a bootstrapping package (if I have such!) It really helps when you have a project that has about 300k lines of code :) </p> </div> <div class="comment-date">2014-09-29 16:40 UTC</div> </div> <div class="comment" id="f25abd103769449a8dd66edc1aed318a"> <div class="comment-author"><a href="https://plus.google.com/108617450837535451203">Jo Takion</a> <a href="#f25abd103769449a8dd66edc1aed318a">#</a></div> <div class="comment-content"> <p> Thank to Mark Seemann for his very inspiring write about Composition Root design pattern, <a href="/2011/07/28/CompositionRoot">/2011/07/28/CompositionRoot/</a> </p> <p> I wrote my own JavaScript Dependency Injection Framework called Di-Ninja with these principles in mind <a href="https://github.com/di-ninja/di-ninja">https://github.com/di-ninja/di-ninja</a> </p> <p> As I know, is the only one in javascript that implement the Composition-Root design pattern and it's documentation could be another good example to demonstrate how it works. </p> <p> It work for both NodeJS and browser (with Webpack) </p> </div> <div class="comment-date">2018-01-04 08:22 UTC</div> </div> <div class="comment" id="35c0b5a5d9064a7ea06dea79e0350a5f"> <div class="comment-author"><a href="https://www.linkedin.com/in/lisber-pontes/">Lisber Pontes</a> <a href="#35c0b5a5d9064a7ea06dea79e0350a5f">#</a></div> <div class="comment-content"> <p> Thanks for this insightful post! </p> <p> I was just wondering, could we say then that using a Service Locator exclusively at the Composition root is not something bad, actually is perfectly fine? </p> <p> I'm asking this, since it's wide-spread the service locator 'anti-pattern', however, at the Composition root its usage is not a problem at all, right ? </p> <p> Thanks for the amazing blog! ;) </p> </div> <div class="comment-date">2022-01-24 9:25 UTC</div> </div> <div class="comment" id="a29ee88350874218a3991801eedbc0bb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a29ee88350874218a3991801eedbc0bb">#</a></div> <div class="comment-content"> <p> Lisber, thank you for writing. Does <a href="/2011/08/25/ServiceLocatorrolesvs.mechanics">this</a> answer your question? </p> </div> <div class="comment-date">2022-01-24 11:49 UTC</div> </div> <div class="comment" id="f2e74af7338144e79f48aa7c41f9f10c"> <div class="comment-author"><a href="https://www.linkedin.com/in/lisber-pontes/">Lisber Pontes</a> <a href="#f2e74af7338144e79f48aa7c41f9f10c">#</a></div> <div class="comment-content"> <p> Totally! </p> <p> The sum up at the end of that article is brilliant! </p> <i> It becomes a Service Locator if used incorrectly: when application code (as opposed to infrastructure code) actively queries a service in order to be provided with required dependencies, then it has become a Service Locator. </i> <p> Thank you for clarifying that. </p> </div> <div class="comment-date">2022-01-24 13:40 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. SOLID Code isn't https://blog.ploeh.dk/2011/06/07/SOLIDCodeisnt 2011-06-07T13:46:07+00:00 Mark Seemann <div id="post"> <p> Recently I had an interesting conversation with a developer at my current client, about how the <a href="http://en.wikipedia.org/wiki/Solid_%28object-oriented_design%29">SOLID</a> principles would impact their code base. The client wants to write SOLID code - who doesn't? It's a beautiful acronym that fully demonstrates the power of catchy terminology. </p> <p> However, when you start to outline what it actually <em>means</em> people become uneasy. At the point where the discussion became interesting, I had already sketched <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">my view on encapsulation</a>. However, the client's current code base is designed around validation at the perimeter. Most of the classes in the Domain Model are actually <a href="http://msdn.microsoft.com/en-us/library/7c5ka91b.aspx">internal</a> and implicitly trust input. </p> <p> We were actually discussing Test-Driven Development, and I had already told them that they should only test against the public API of their code base. The discussion went something like this (I'm hoping I'm not making my ‘opponent' sound dumb, because the real developer I talked to was anything but): </p> <p> <strong>Client:</strong> "That would mean that each and every class we expose must validate input!" </p> <p> <strong>Me:</strong> "Yes…?" </p> <p> <strong>Client:</strong> "That would be a lot of extra work." </p> <p> <strong>Me:</strong> "Would it? Why is that?" </p> <p> <strong>Client:</strong> "The input that we deal with consist of complex data structures, and we must validate that all values are present and correct." </p> <p> <strong>Me:</strong> "Assume that input is SOLID as well. This would mean that each input instance can be assumed to be in a valid state because that would be its own responsibility. Given that, what would validation really mean?" </p> <p> <strong>Client:</strong> "I'm not sure I understand what you mean…" </p> <p> <strong>Me:</strong> "Assuming that the input instance is a self-validating reference type, what could possibly go wrong?" </p> <p> <strong>Client:</strong> "The instance might be null…" </p> <p> <strong>Me:</strong> "Yes. Anything else?" </p> <p> <strong>Client:</strong> "Not that I can think of…" </p> <p> <strong>Me:</strong> "Me neither. This means that while you must add more code to implement proper encapsulation, it's really trivial code. It's just some Guard Clauses." </p> <p> <strong>Client:</strong> "But isn't it still gold plating?" </p> <p> <strong>Me:</strong> "Not really, because we are designing for <em>change</em> in the general sense. We know that we can't predict <em>specific</em> change, but I can guarantee you that change requests <em>will</em> occur. Instead of trying to predict specific changes and design variability in those specific places, we simply put interfaces around everything because the cost of doing so is really low. This means that when change does happen, we already have Seams in the right places." </p> <p> <strong>Client:</strong> "How does SOLID help with that?" </p> <p> <strong>Me:</strong> "A result of the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> is that each self-encapsulated class becomes really small, and there will be a lot of them." </p> <p> <strong>Client:</strong> "Lots of classes… I'm not sure I'm comfortable with that. Doesn't it make it much harder to find what you need?" </p> <p> <strong>Me:</strong> "I don't think so. Each class is very small, so although you have many of them, understanding what each one does is easy. In my experience this is a lot easier than trying to figure out what a big class with thousands of lines of code does. When you have few big classes, your object model might look something like this:" </p> <p> <img alt="Coarse-grained objects" src="/content/binary/Windows-Live-Writer/SOLID-Code-isnt_B434/largegrainedobjects_3.png"> </p> <p> "There's a few objects and they kind of fit together to form the overall picture. However, if you need to change something, you'll need to substantially change the shape of each of those objects. That's a lot of work, and this is why such an object design isn't particularly adaptable to change. </p> <p> "With SOLID, on the other hand, you have lots of small-grained objects which you can easily re-arrange to match new requirements:" </p> <p> <img alt="Fine-grained objects" src="/content/binary/Windows-Live-Writer/SOLID-Code-isnt_B434/finegrainedobjects_3.png"> </p> <p> And that's when it hit me: SOLID code isn't really solid at all. I'm not a material scientist, but to me a <a href="http://en.wikipedia.org/wiki/Solid">solid</a> indicates a rigid structure. In essence a structure where the particles are tightly locked to each other and can't easily move about. </p> <p> However, when thinking about SOLID code, it actually helps to think about it more like a <a href="http://en.wikipedia.org/wiki/Liquid">liquid</a> (although perhaps a rather viscous one). Each class has much more room to maneuver because it is small and fits together with other classes in many different ways. It's clear that when you <a href="http://xkcd.com/895/">push an analogy too far, it breaks apart</a>. </p> <p> Still, a closing anecdote is appropriate... </p> <p> My (then) three-year old son one day handed me a handful of <a href="http://en.wikipedia.org/wiki/Lego_Duplo">Duplo</a> bricks and asked me to build him a dragon. If you've ever tried to build anything out of Duplo you'll know that the ‘resolution' of the bricks is rather coarse-grained. Given that ‘a handful' for a three-year old isn't a lot of bricks, this was quite a challenge. Fortunately, I had an appreciative audience with quite a bit of imagination, so I was able to put the few bricks together in a way that satisfied my son. </p> <p> Still, building a dragon of comparable size out of <a href="http://en.wikipedia.org/wiki/Lego">Lego</a> bricks is much easier because the bricks have a much finer ‘resolution'. SOLID code is more comparable to Lego than Duplo. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5d420da2b1e240a19938b3c6832cf8a6"> <div class="comment-author"><a href="http://thedailywtf.com/">Alex Papadimoulis</a> <a href="#5d420da2b1e240a19938b3c6832cf8a6">#</a></div> <div class="comment-content">It's easier to use Lego bricks if your dinosaur is small, but if it's life sized - the cost and time using Lego instead of Duplo would be enormous. The same could be said about getting too granular with classes (IShippingOptionProviderFactory, anyone?) on a large project. Speaking of taking analogies too far... isn't building anything (real) out of Lego bricks a bad idea? I mean, I certainly wouldn't want to drive a Lego car or walk across a Lego bridge. </div> <div class="comment-date">2011-06-07 14:41 UTC</div> </div> <div class="comment" id="f332d90da5254299a761ed379585393e"> <div class="comment-author"><a href="http://beletsky.net">Alexander Beletsky</a> <a href="#f332d90da5254299a761ed379585393e">#</a></div> <div class="comment-content">Brilliant article :)</div> <div class="comment-date">2011-06-07 14:56 UTC</div> </div> <div class="comment" id="ca8421ac4dcb460aa55824777f20151d"> <div class="comment-author"><a href="http://geekswithblogs.net/theArchitectsNapkin">Ralf Westphal</a> <a href="#ca8421ac4dcb460aa55824777f20151d">#</a></div> <div class="comment-content">Looks like your thinking is in the line of the DDJ editorial:<br> http://bit.ly/ik3dCW <br> <br> Functional units of small size are key to evolvable code. This makes for a fine grained &quot;code sand&quot; which can be brought into ever changing shapes.<br> <br> The question however is: What is to become smaller? Classes or methods?<br> <br> I&#180;d say it&#180;s classes that need to be limited in size, maybe 50 to 100 LOC.<br> Classes are to be kept small because they are the &quot;blueprints&quot; of the smallest stateful runtime units of code which can be recombined: objects.<br> <br> However this leads to two problems:<br> <br> 1. It&#180;s difficult to think of a decomposition of the very common noun-classes into smaller classes. How to break up a PaymentService class into many smaller classes?<br> <br> 2. Small classes would lead to an explosion of dependencies between all of these classes. They would be hard to manage (even with dependency injection).<br> <br> That&#180;s two reasons why most developers will probably resists decreasing the size of their classes. They&#180;ll just keep methods small - but the effect of this will be limited. There&#180;s no limit to the size of a class; 1,000 LOC class can consist of 100 methods each 10 lines long.<br> <br> But the two problems go away if SOLID is applied in combination with a different view on object orientation.<br> <br> We just need to switch from nouns to verbs when thinking about domain logic classes (not data classes).<br> <br> If classes become functions (or maybe better behaviors) then there is no decomposition problem anymore. Behaviors can be as small as needed, maybe just 1 LOC.<br> <br> Also behaviors conceptually don&#180;t have any dependencies on each other (but maybe to an environment); sitting is not dependend on running even though running might &quot;be done&quot; after sitting.</div> <div class="comment-date">2011-06-09 10:53 UTC</div> </div> <div class="comment" id="f41619d4292947fb99f359b05e6fa527"> <div class="comment-author">Darin <a href="#f41619d4292947fb99f359b05e6fa527">#</a></div> <div class="comment-content">@Alex Papadimoulis<br> <br> &quot;I certainly wouldn't want to drive a Lego car or walk across a Lego bridge&quot;<br> <br> ROTFL<br> <br> Great quote! I've always liked the principles behind solid, but have always been a little turned off by some of the zealotry associated with it. Like most everything in life it's great &quot;in moderation&quot;.</div> <div class="comment-date">2011-06-09 12:15 UTC</div> </div> <div class="comment" id="6e98c86575bd498fb1ddbb03c64e73df"> <div class="comment-author"><a href="http://ted">Ted F.A. van Gaalen</a> <a href="#6e98c86575bd498fb1ddbb03c64e73df">#</a></div> <div class="comment-content">Hi Mark.<br> Thanks for your article.<br> You are describing a philosophy<br> which is almost standard practice<br> in Smalltalk since the 80's..<br> As you know, albeit desired, it is <br> however not always possible to<br> make small classes.<br> Recommended reading:<br> (also for OO in general)<br> &quot;Smalltalk, Objects and Design&quot;<br> by: Chamond Liu<br> ISBN: 1-8847777-27-9<br> Manning Publications Co, 1996.<br> Kind Regards<br> Ted<br> ..btw I am sure that there must be <br> a photo of yours whereon you look a bit happier?<br> <br> <br> </div> <div class="comment-date">2011-06-09 12:40 UTC</div> </div> <div class="comment" id="8dafbdf534d94246a25863e3285a3062"> <div class="comment-author">Bill Berger <a href="#8dafbdf534d94246a25863e3285a3062">#</a></div> <div class="comment-content">Nice blog. Good visual analogy.<br> <br> It's my experience that there is lots of discussion around the concept of abstraction and too little discussion around the use and practice of abstraction.<br> <br> I interview many software development candidates, and I always explore what I consider foundational concepts - one is abstraction. I find most developers / programmers / software engineers (/ whatever), cannot adequately communicate HOW they use abstraction to enable their code - not on an architectural level, not on a modular level, not on a class level, and not even on a simple functional level. This is a real indication of the state of software design and it is alarming to me.<br> <br> It's my belief that designing abstraction in software engineering is the most critical tool in software construction, but it is the least discussed.<br> </div> <div class="comment-date">2011-06-09 12:49 UTC</div> </div> <div class="comment" id="ba54b89b7de44a16ab43935daf1f2e4a"> <div class="comment-author"><a href="http://geekswithblogs.net/theArchitectsNapkin">Ralf Westphal</a> <a href="#ba54b89b7de44a16ab43935daf1f2e4a">#</a></div> <div class="comment-content">@Bill: I agree that abstraction is missing from many discussions (and from languages). Pars pro toto: in UML class diagrams abstraction (in the form of composition) cannot be distinguished from simple usage.<br> <br> So since there is no real expression of abstraction (except for inheritance) in code, it&#180;s neglected or left to personal style. Sad.</div> <div class="comment-date">2011-06-09 13:09 UTC</div> </div> <div class="comment" id="81c7a83ca6ec483c9aff55715ab130c1"> <div class="comment-author"><a href="http://sumosoftware.com">Mark Lauter</a> <a href="#81c7a83ca6ec483c9aff55715ab130c1">#</a></div> <div class="comment-content">Mark, thanks for this post. I am telling you that I had almost the exact same conversation with client late last year. At some point he says &quot;but it's SOOO many classes..&quot; Today reading your post I just had to laugh. Good times. :)</div> <div class="comment-date">2011-06-09 14:25 UTC</div> </div> <div class="comment" id="51d8aa21a5914064a0e1008a48235df6"> <div class="comment-author">Dave Boyle <a href="#51d8aa21a5914064a0e1008a48235df6">#</a></div> <div class="comment-content">"…put interfaces around everything because the cost of doing so is really low."<br> <br> I’m not convinced that the cost is indeed "really low", for two reasons.<br> <br> 1) Interfaces, before TDD became popular, imparted information to the maintainer of a type because they were used to indicate, for instance, that a given type was to be treated like a collection that could be enumerated or a type that could be compared with other types for the purposes of, say, sorting. When added reflexively as a means of inserting a level of indirection, the interface no longer imparts any information.<br> <br> 2) When navigating around a large code base where nearly all types are accessed via a level of indirection (courtesy of ubiquitous interfaces) my IDE – Visual Studio 2010 – struggles to answer common questions like "what code will be executed when SomeType.ReadHeader() is called?". Hitting F12 doesn’t take me to the definition of the method but rather takes me to the definition of the interface which, because of the point above, is of no value. ReSharper can sometimes find the method with a more advanced search but not always. The upshot of this is that code making heavy use of interfaces becomes much harder to statically analyse.<br> </div> <div class="comment-date">2011-06-09 14:50 UTC</div> </div> <div class="comment" id="fa41899483f14372b3d2131c5004397d"> <div class="comment-author">Bill Berger <a href="#fa41899483f14372b3d2131c5004397d">#</a></div> <div class="comment-content">@Ralf: &quot;So since there is no real expression of abstraction (except for inheritance) in code&quot;<br> <br> Agreed. That point is manifest in @Dave's subsequent post. So, how would the lack of direct language support for abstraction - other than inheritance - be solved? And how would code tracing / debugging interfaces and their implementations be handled?<br> <br> My gut tells me this is one reason (of several) that scripting-like languages have gained popularity in the recent past, and that we are being pointed in a more real-time, interpreted language direction due to the necessary dynamic nature of these constructs. Hm... {thinking}<br> <br> Love a blog that makes me think for more than a few seconds.</div> <div class="comment-date">2011-06-09 16:52 UTC</div> </div> <div class="comment" id="d09d630e2f2c4181b146e4a91ea02d49"> <div class="comment-author">P <a href="#d09d630e2f2c4181b146e4a91ea02d49">#</a></div> <div class="comment-content">As a client, looking at the second picture with lots of small parts, I see more complicated picture, therefore more maintenance, more support and more money to spend on it. As a consultant for that client, I see more dollar signs in my bank account...</div> <div class="comment-date">2011-06-09 22:14 UTC</div> </div> <div class="comment" id="6c10645e17aa4479b97c7b927fed2a16"> <div class="comment-author"><a href="http://geekswithblogs.net/theArchitectsNapkin">Ralf Westphal</a> <a href="#6c10645e17aa4479b97c7b927fed2a16">#</a></div> <div class="comment-content">@Bill: You ask<br> <br> <i>&quot;So, how would the lack of direct language support for abstraction - other than inheritance - be solved?&quot;</i><br> <br> and I don&#180;t know if we should hope for languages to offer support soon. We&#180;ve to live with what we have: Java, C#, C++, Ruby etc. (Scripting languages to me don&#180;t offer better support for abstraction.)<br> <br> Instead we need to figure out how to use whatever features these languages offer in a way to express abstraction - and I don&#180;t mean inheritance ;-)<br> <br> And that leads to a separation of mental model from technical features. We need to think in a certain way about our solutions - and then translate our mental models to language reality.<br> <br> Object orientation tried to unify mental model and code reality. But as all those maintenance nightmare projects show, this has not really worked out as nicely as expected. The combination of OO method (OOAD) and OO programming has not lived up to our expectations.<br> <br> Since we&#180;re pretty much stuck with OO languages we need to replace the other part of the pair, the method, I&#180;d say.</div> <div class="comment-date">2011-06-10 08:44 UTC</div> </div> <div class="comment" id="913d0012b717495eaf118d3b6880e2e0"> <div class="comment-author">Kenneth Siewers M&#248;ller <a href="#913d0012b717495eaf118d3b6880e2e0">#</a></div> <div class="comment-content">You really need a +1 :)<br> Very interesting article...</div> <div class="comment-date">2011-08-24 08:31 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. At the Boundaries, Applications are Not Object-Oriented https://blog.ploeh.dk/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented 2011-05-31T13:27:11+00:00 Mark Seemann <div id="post"> <p> My recent series of blog posts about <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a> generated a few responses (I would have been disappointed had this not been the case). Quite a few of these reactions relate to various serialization or translation technologies usually employed at application boundaries: Serialization, XML (de)hydration, UI validation, etc. Note that such translation happens not only at the perimeter of the application, but also at the persistence layer. ORMs are also a translation mechanism. </p> <p> Common to most of the comments is that lots of serialization technologies require the presence of a default constructor. As an example, the <a href="http://msdn.microsoft.com/en-us/library/182eeyhh.aspx">XmlSerializer</a> requires a default constructor and public writable properties. Most ORMs I've investigated seem to have the same kind of requirements. Windows Forms and WPF Controls (UI is also an application boundary) also must have default constructors. Doesn't that break encapsulation? Yes and no. </p> <h3 id="41e497528a1c42de8ba24e6418388810"> Objects at the Boundary <a href="#41e497528a1c42de8ba24e6418388810" title="permalink">#</a> </h3> <p> It certainly <em>would</em> break encapsulation if you were to expose your (domain) <em>objects</em> directly at the boundary. Consider a simple XML document like this one: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&lt;</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#a31515">name</font></span></font><span style="color: "><font style="font-size: 10pt" color="#0000ff">&gt;</font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&nbsp; &lt;</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#a31515">firstName</font></span><span style="color: "><font color="#0000ff">&gt;</font></span>Mark<span style="color: "><font color="#0000ff">&lt;/</font></span><span style="color: "><font color="#a31515">firstName</font></span></font><span style="color: "><font style="font-size: 10pt" color="#0000ff">&gt;</font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&nbsp; &lt;</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#a31515">lastName</font></span><span style="color: "><font color="#0000ff">&gt;</font></span>Seemann<span style="color: "><font color="#0000ff">&lt;/</font></span><span style="color: "><font color="#a31515">lastName</font></span></font><span style="color: "><font style="font-size: 10pt" color="#0000ff">&gt;</font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&lt;/</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#a31515">name</font></span></font><span style="color: "><font style="font-size: 10pt" color="#0000ff">&gt;</font></span></pre> </p> <p> Whether or not we have formal a contract (XSD), we might stipulate that both the firstName and lastName elements are <em>required</em>. However, despite such a contract, I can easily create a document that breaks it: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&lt;</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#a31515">name</font></span></font><span style="color: "><font style="font-size: 10pt" color="#0000ff">&gt;</font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&nbsp; &lt;</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#a31515">firstName</font></span><span style="color: "><font color="#0000ff">&gt;</font></span>Mark<span style="color: "><font color="#0000ff">&lt;/</font></span><span style="color: "><font color="#a31515">firstName</font></span></font><span style="color: "><font style="font-size: 10pt" color="#0000ff">&gt;</font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&lt;/</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#a31515">name</font></span></font><span style="color: "><font style="font-size: 10pt" color="#0000ff">&gt;</font></span></pre> </p> <p> We can't <em>enforce</em> the contract as there's no compilation step involved. We can <em>validate</em> input (and output), but that's a different matter. Exactly because there's no enforcement it's very easy to create malformed input. The same argument can be made for UI input forms and any sort of serialized byte sequence. This is why we must treat all input as suspect. </p> <p> This isn't a new observation at all. In <a href="http://www.martinfowler.com/books.html#eaa">Patterns of Enterprise Application Architecture</a>, <a href="http://www.martinfowler.com">Martin Fowler</a> described this as a <a href="http://martinfowler.com/eaaCatalog/dataTransferObject.html">Data Transfer Object</a> (DTO). However, despite the name we should realize that DTOs are not really objects at all. This is nothing new either. Back in 2004 Don Box formulated the <a href="http://msdn.microsoft.com/en-us/magazine/cc164026.aspx">Four Tenets of Service Orientation</a>. (Yes, I know that they are not in vogue any more and that people <a href="http://www.pluralsight-training.net/community/blogs/dbox/archive/2007/08/15/48232.aspx">wanted to retire them</a>, but some of them still make tons of sense.) Particularly the third tenet is germane to this particular discussion: </p> <blockquote> <p> Services share schema and contract, not class. </p> </blockquote> <p> Yes, and that means they are <em>not objects</em>. A DTO is a <em>representation</em> of such a piece of data <em>mapped into</em> an object-oriented language. That still doesn't make them objects in the sense of encapsulation. It would be impossible. Since all input is suspect, we can hardly enforce any invariants at all. </p> <p> Often, as <a href="http://blogs.teamb.com/craigstuntz/">Craig Stuntz</a> points out in <a href="/2011/05/27/DesignSmellRedundantRequiredAttribute">a comment to one of my previous posts</a>, even if the input is invalid, we want to capture what we <em>did</em> receive in order to present a proper error message (this argument also applies on machine-to-machine boundaries). This means that any DTO must have <em>very</em> weak invariants (if any at all). </p> <blockquote> <p> DTOs don't break encapsulation because they aren't objects at all. </p> </blockquote> <p> Don't be fooled by your tooling. The .NET framework very, very much wants you to treat DTOs as objects. Code generation ensues. </p> <p> However, the strong typing provided by such auto-generated classes gives a false sense of security. You may think that you get <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">rapid feedback from the compiler</a>, but there are many possible ways you can get run-time errors (most notably when you forget to update the auto-generated code based on new schema versions). </p> <p> An even more problematic result of representing input and output as objects is that it tricks lots of developers into dealing with them as though they represent the real object model. The result is invariably an <a href="http://www.martinfowler.com/bliki/AnemicDomainModel.html">anemic domain model</a>. </p> <p> More and more, this line of reasoning is leading me towards the conclusion that the DTO mental model that we have gotten used to over the last ten years is a dead end. </p> <h3 id="e5cfe90e1a6947439e0360d9b681f77a"> What Should Happen at the Boundary <a href="#e5cfe90e1a6947439e0360d9b681f77a" title="permalink">#</a> </h3> <p> Given that we write object-oriented code and that data at the boundary is anything but object-oriented, how do we deal with it? </p> <p> One option is to stick with what we already have. To bridge the gap we must then develop <em>translation layers</em> that can translate the DTOs to properly encapsulated domain objects. This is the route I take with the samples in <a href="http://amzn.to/12p90MG">my book</a>. However, this is a solution that more and more I'm beginning to think may not be the best. It has issues with maintainability. (Incidentally, that's the problem with writing a book: at the time you're done, you know so much more than you did when you started out… Not that I'm denouncing the book - it's just not perfect…) </p> <p> Another option is to stop treating data as objects and start treating it as the <em>structured data</em> that it really is. It would be really nice if our programming language had a separate concept of <em>structured data</em>… Interestingly, while C# has nothing of the kind, F# has tons of ways to model data structures without behavior. Perhaps that's a more honest approach to dealing with data… I will need to experiment more with this… </p> <p> A third option is to look towards dynamic types. In his article <a href="http://msdn.microsoft.com/en-us/magazine/ff796227.aspx">Cutting Edge: Expando Objects in C# 4.0</a>, Dino Esposito outlines a dynamic approach towards consuming structured data that shortcuts auto-generated code and provides a lightweight API to structured data. This also looks like a promising approach… It doesn't provide compile-time feedback, but that's only a false sense of security anyway. We must resort to <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">unit tests to get rapid feedback</a>, but we're all using TDD already, right? </p> <p> In summary, my entire series about encapsulation relates to <em>object-oriented programming</em>. Although there are lots of technologies available to represent boundary data as ‘objects', they are false objects. Even if we use an object-oriented language at the boundary, the code has nothing to do with object orientation. Thus, the Poka-yoke Design rules don't apply there. </p> <p> Now go back and reread this post, but replace ‘DTO' with ‘Entity' (or whatever your ORM calls its representation of a relational table row) and you should begin to see the contours of why ORMs are problematic. </p> <p> <ins datetime="2022-05-02"><strong>P.S. 2022-05-02.</strong> See also <a href="/2022/05/02/at-the-boundaries-applications-arent-functional">At the boundaries, applications aren't functional</a>.</ins> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2662e99c45e749c7b2b80911dcc0b7d4"> <div class="comment-author"><a href="http://marcgravell.blogspot.com">Marc Gravell</a> <a href="#2662e99c45e749c7b2b80911dcc0b7d4">#</a></div> <div class="comment-content">Absolutely. I spend a lot of time in this space, an while I enjoy using (and maintaining) tools to make this transition simple, whenever there isthe first hint of a problem I always advise &quot;add a dedicated DTO&quot;. A similar question (using interfaces in WCF messages) came up on stackoverflow last week. But ultimately, any boundary talks &quot;data&quot;, not behaviour. It also emphasises why BinaryFormatter (implementation-aware) sucks so much for ***any*** kind of data exchange.</div> <div class="comment-date">2011-05-31 14:53 UTC</div> </div> <div class="comment" id="80944acf26474381973e1a06d7aa020b"> <div class="comment-author">Phil Sandler <a href="#80944acf26474381973e1a06d7aa020b">#</a></div> <div class="comment-content">Arg, had a longer comment but it got eaten when I clicked Save Comment.<br> <br> Anyway the gist of it: great post, I commpletely agree that data objects aren't domain objects, and thinking of them as structured data is very liberating. Using Entities/DTOs/data objects as a composible part of a domain object is a nice way to create that separation which still allows you to (potentially directly) use ORM entities while avoiding having an anemic domain model. So, for example, CustomerDomain would/could contain a CustomerEntity, instead of trying to add behavior to CustomerEntity (which might be a generated class).</div> <div class="comment-date">2011-05-31 16:37 UTC</div> </div> <div class="comment" id="41b0b05eb4b340a485650ecacf23b279"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#41b0b05eb4b340a485650ecacf23b279">#</a></div> <div class="comment-content">Phil, when you write 'Entity' I immediately think about the definition provided in &quot;Domain-Driven Design&quot;. Those Entities are definitely Domain Objects which should have lots of behavior, but I'm getting the feeling that you're thinking about something else?</div> <div class="comment-date">2011-05-31 18:22 UTC</div> </div> <div class="comment" id="0a81e99a083c4b57824d8908f46ff098"> <div class="comment-author">Phil Sandler <a href="#0a81e99a083c4b57824d8908f46ff098">#</a></div> <div class="comment-content">Hey Mark,<br> <br> Yep, I meant &quot;entity&quot; more in the sense of how it is defined in some of the popular ORMs, like LLBLGen and Entity Framework. Essentially, an instance that corresponds (more or less) directly to a row in a table or view.</div> <div class="comment-date">2011-05-31 22:09 UTC</div> </div> <div class="comment" id="1f6279d40bd746ebb4d34da6d5b55b47"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1f6279d40bd746ebb4d34da6d5b55b47">#</a></div> <div class="comment-content">Yes, we might compose real domain objects from DTOs, but I'm not sure that is the best solution. When we map non-object-oriented structures to objects, they tend to drag along a lot of baggage which hurts OO modeling.<br> <br> For instance, generating DTOs from a database schema provides an entire static structure (relationships, etc.) that tend to constrain us when we subsequently attempt to define an object model. I rather prefer being able to work unconstrained and then subsequently figure out how to persist it.</div> <div class="comment-date">2011-06-01 05:44 UTC</div> </div> <div class="comment" id="0dda61272ac74a138e5a252fe6b44405"> <div class="comment-author"><a href="http://code.dortikum.net">Boyan Mihaylov</a> <a href="#0dda61272ac74a138e5a252fe6b44405">#</a></div> <div class="comment-content">I like this article. As far as I undertand you for every entity you have (a domain object) you need a separate DTO for different representations (XML, SQL, etc.). So you need a third object which translates every DTO into a specific entity. Am I correct? If so, isn't that process too complex?</div> <div class="comment-date">2011-06-01 08:29 UTC</div> </div> <div class="comment" id="fa88ba7b7b624e58bd7e03d0e2520be7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fa88ba7b7b624e58bd7e03d0e2520be7">#</a></div> <div class="comment-content">Yes, that's what I've been doing for the last couple of years, but the downside is the maintenance overhead. That's why I, in the last part of the post, discuss alternatives. Currently I think that using dynamic types looks most promising.<br> <br> Keep in mind that in most cases, a DTO is not an end-goal in itself. Rather, the end-goal is often the wire-representation of the DTO (XML, JSON, etc.). Wouldn't it be better if we could just skip the DTO altogether and use a bit of convention-based (and testable) dynamic code to make that translation directly?</div> <div class="comment-date">2011-06-01 08:41 UTC</div> </div> <div class="comment" id="bf95a4de9ff3487ba6e9f215513bff66"> <div class="comment-author">Florian H&#246;tzinger <a href="#bf95a4de9ff3487ba6e9f215513bff66">#</a></div> <div class="comment-content">I guess in WCF service scenarios there is really no other way than to create a DTO layer and compose our domain objects from those DTOs when passing them from/to the client. Since I'm doing a lot of Silverlight development at the moment, this came up quite early.</div> <div class="comment-date">2011-06-01 09:13 UTC</div> </div> <div class="comment" id="2b13ff621d5c422499b22c87dc2e4cb0"> <div class="comment-author"><a href="http://code.dortikum.net">Boyan Mihaylov</a> <a href="#2b13ff621d5c422499b22c87dc2e4cb0">#</a></div> <div class="comment-content">@Mark: Yes, that would be quite a better way. Here comes the great article, you've mentioned, about the ExpandoObject and the dynamic way of dealing with json, xml and other representations of data. However, in case of SQL at this moment you still need some DTOs I guess. Maybe if we have some wrapper over the traditional IDataReader or some other mechanism to access the data, it will be possible again.</div> <div class="comment-date">2011-06-01 10:13 UTC</div> </div> <div class="comment" id="363b69962b5c45169480642e9b6cb3a5"> <div class="comment-author">Phil Sandler <a href="#363b69962b5c45169480642e9b6cb3a5">#</a></div> <div class="comment-content">Hey Mark,<br> <br> It's a difficult problem to solve perfectly--every solution had downsides. I have gone down the road of mapping DTOs (or data entities) via a translation layer and that can be painful as well. As you said, the tradeoff of using data objects as a composable part of the Domain Object is that you can't model your domain with complete freedom (which DDD purists would likely see as non-negotiable). The upside is that you have the potential for less maintenance.<br> <br> </div> <div class="comment-date">2011-06-01 14:34 UTC</div> </div> <div class="comment" id="a0ba4c2b8f0845f9939e163a81174abc"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a0ba4c2b8f0845f9939e163a81174abc">#</a></div> <div class="comment-content">Florian, in WCF it <i>is</i> possible to drop down to the message-level and write directly to the message. Even so, I'm not sure I'd go that route, but it's good to know that the option exists.</div> <div class="comment-date">2011-06-01 18:43 UTC</div> </div> <div class="comment" id="8cefd775fb304f24b98fd5c77b9daac6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8cefd775fb304f24b98fd5c77b9daac6">#</a></div> <div class="comment-content">Boyan, I was specifically thinking about dropping down to IDataReader. For one example, please refer to Tomas Petricek's article <a href="http://tomasp.net/blog/dynamic-sql.aspx">Dynamic in F#: Reading data from SQL database</a>.</div> <div class="comment-date">2011-06-01 18:46 UTC</div> </div> <div class="comment" id="3542188e6e6c495aa6278a63138c9a91"> <div class="comment-author"><a href="http://stratasphere.blogspot.com/">hotsleeper</a> <a href="#3542188e6e6c495aa6278a63138c9a91">#</a></div> <div class="comment-content">Hi Mark, <br> <br> There is lots to argue about in this article (nothing &quot;wrong&quot;, just semantics). The main point I would like to pick up on is about your point &quot;What should happen at domain boundaries&quot;. <br> <br> I would contest that rather than &quot;translation&quot; layers, its semantically better to think of it as an &quot;interpretation object&quot;. When an app accepts input, there are very few assumptions that should be automatically attached to the unit of input. One of the fundamental ones and often the easiest to break is the assumption that the input is in any way valid, useful or complete. <br> <br> The semantic concept(abstraction) that is wrapped around the unit of input (the object that encapsulates the input data) need to have a rich interface that can convey abstractions such as &quot;Cannot interpret this input&quot;, &quot;Can partially interpret the input but it contains some rubbish&quot;, &quot;Input contains SQL injection attack&quot;, &quot;Input is poorly formed and has a missing .DTD file&quot; etc. <br> <br> My argument is that the semantic concept of &quot;translation&quot; while obviously closely related to &quot;interpretation&quot; is semantically a &quot;transformation process&quot;. A-&gt;B kind of idea, while &quot;interpretation&quot;, at least in my head, is not so deterministic and seeks to extract domain abstractions from the input that are then exposed to the application as high level abstractions rather than low level &quot;translated data/safe data/sanitized data&quot;. <br> <br> Thanks, <br> <br> hotsleeper<br> </div> <div class="comment-date">2011-06-02 02:37 UTC</div> </div> <div class="comment" id="2e0c0947b8e646bdb2b333a8fbf2615e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2e0c0947b8e646bdb2b333a8fbf2615e">#</a></div> <div class="comment-content">Makes sense. I mainly used the term 'translation layer' as I assumed that most people would then immediately know what I meant :)</div> <div class="comment-date">2011-06-02 06:51 UTC</div> </div> <div class="comment" id="3714fe353b414324a3916806e9f7ad2a"> <div class="comment-author">Arved Sandstrom <a href="#3714fe353b414324a3916806e9f7ad2a">#</a></div> <div class="comment-content">What's the processing error that is currently preventing comments? I can't get a usefully long one successfully submitted.</div> <div class="comment-date">2011-06-02 09:50 UTC</div> </div> <div class="comment" id="d80232d091674338b7aec332d7398b39"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d80232d091674338b7aec332d7398b39">#</a></div> <div class="comment-content">Sorry about that. This blog is running on <a href="http://dasblog.info">dasBlog</a> which was last updated two years ago. Migrating the blog to a better engine is on my to-do list, but it'll be a couple of months at least before I get there.</div> <div class="comment-date">2011-06-02 10:07 UTC</div> </div> <div class="comment" id="05127fbf1c99486fa460ae579ebae52d"> <div class="comment-author"><a href="http://blogs.teamb.com/craigstuntz">Craig Stuntz</a> <a href="#05127fbf1c99486fa460ae579ebae52d">#</a></div> <div class="comment-content">FWIW, the maintenance cost is effectively zero if the DTO types are codegened. <br> <br> Dynamic types are also an option -- anyone who has used Rails/ActiveRecord is familiar with their upsides and downsides. In short, maintaining them is free, but using them costs more. <br> <br> My preferred approach (for now) is to use EF entities as boundary objects for the DB. They cost almost nothing to maintain -- a couple of mouse clicks when we change DB schemata -- since we use &quot;Database First&quot; modeling for internal reasons. <br> <br> You only get into encapsulation trouble with this if you try to use the EF entities as business objects -- something many people do. My $0.02 is that it's usually wrong to put any kind of behavior on an EF entity.</div> <div class="comment-date">2011-06-04 15:51 UTC</div> </div> <div class="comment" id="d7b452de442a4254a866f16fd9aa3a55"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d7b452de442a4254a866f16fd9aa3a55">#</a></div> <div class="comment-content">I agree on the rule of never putting any behavior on an EF entity (although, still, I used to put a translation method on most of them: ToDomainObject(), but still...).<br> <br> My personal experience with EF was that even with code generation, it was far from frictionless, but YMMV...</div> <div class="comment-date">2011-06-04 21:13 UTC</div> </div> <div class="comment" id="845ae61af8164d6ab3fad9c874bc098e"> <div class="comment-author"><a href="http://www.mellekoning.nl">Melle Koning</a> <a href="#845ae61af8164d6ab3fad9c874bc098e">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Depends! If we're building a framework that is used by others then there is a big difference between the applications boundary to the &quot;outside world&quot; (DTO's) and persistence. <br> <br> A quote taken from NHDay - Loosely Coupled Complexity - CQRS (see minute 2:10) What is wrong with a design with DTO's? &quot;Nothing.&quot; End of presentation. But then continues into CQRS :)<br> <br> Especially the translation-layer when requests come into our application, and we try to rebuild the OO model to help save new objects with an ORM can be cumbersome. <br> <br> The point is that the database follows from the domain design, not other way round like EF or ORM class generators make you try to believe.<br> </div> <div class="comment-date">2011-06-05 11:18 UTC</div> </div> <div class="comment" id="72405fee69b84230b4cc99920b146e1b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#72405fee69b84230b4cc99920b146e1b">#</a></div> <div class="comment-content">The point being made here is that despite the name, DTOs aren't 'objects'. It's certainly possible to create a complex application entirely with DTOs - it's just not object-oriented.<br> <br> There may be nothing wrong with that, but if you decide that you need <em>object-orientation</em> to solve a business problem, you <em>must</em> transform DTOs into proper objects, because it's impossible to make OOD with DTOs.</div> <div class="comment-date">2011-06-05 11:45 UTC</div> </div> <div class="comment" id="6e4e961a660545dd8ad09c4cc3ad83a8"> <div class="comment-author"><a href="http://zaemis.blogspot.com">Timothy</a> <a href="#6e4e961a660545dd8ad09c4cc3ad83a8">#</a></div> <div class="comment-content">The &quot;everything is an ________&quot; mantra gets us in trouble every time, whether it's strings in TCL, objects in Ruby, etc. Objects are a programming construct in practice, and the problem is we're trying to treat data as objects... which it really isn't. I echo your sentiments to &quot;stop treating data as objects and start treating it as the structured data that it really is.&quot; </div> <div class="comment-date">2011-06-05 18:59 UTC</div> </div> <div class="comment" id="b090909dffc045acab9bb6c1a410d21e"> <div class="comment-author"><a href="http://geekswithblogs.net/theArchitectsNapkin">Ralf Westphal</a> <a href="#b090909dffc045acab9bb6c1a410d21e">#</a></div> <div class="comment-content">Great article!<br> <br> But why not think this further? What&#180;s a boundary? Is a boundary where data is exchanged between processes? Or processes on different machines? Processes implemented using different platforms? Or is a boundary where data moves between .NET AppDomains? Or between threads?<br> <br> The past year I&#180;ve felt great relieve by very strictly appyling this rule: data that moves around is, well, just data.<br> <br> That does not mean I&#180;m back to procedural programming. I appreciate the benefits of object oriented languages.<br> <br> But just because I can combine data and functions into one &quot;thing&quot;, I should not always do it.<br> <br> If data is state of a behavior, then data+function makes sense.<br> <br> But if data is pushed around between &quot;behaviroal objects&quot; then data should be just data (maybe spiced with some convenience functions).<br> <br> So what I&#180;ve come to doing is &quot;data flow design&quot; or &quot;behaviroal design&quot;. And that plays very nicely with async programming and distributing code across processes.</div> <div class="comment-date">2011-06-08 14:14 UTC</div> </div> <div class="comment" id="a8ecd80fa60747c5b10dff8ed7a25309"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a8ecd80fa60747c5b10dff8ed7a25309">#</a></div> <div class="comment-content">Yes, we can take it further, but I don't believe that one approach necessarily rules out the other. However, it depends on <em>how</em> you decide to pass around structured data.<br> <br> If you do it in a request/response style (even mapped into internal code), I'd say that you'd be doing procedural programming.<br> <br> However, if you do it as Commands, passing structured data to void methods, you'd basically be heading in the direction of <em>Pipes and Filters architecture</em>. <a href="/2011/03/22/CommandsareComposable">That's actually a very good place to be</a>.</div> <div class="comment-date">2011-06-09 09:47 UTC</div> </div> <div class="comment" id="e397b47542e24ea7b6236c53f45b6906"> <div class="comment-author"><a href="http://codyaray.com">Cody A. Ray</a> <a href="#e397b47542e24ea7b6236c53f45b6906">#</a></div> <div class="comment-content">Nice post (even though I'm a couple years late to it). <br> <blockquote>Wouldn't it be better if we could just skip the DTO altogether and use a bit of convention-based (and testable) dynamic code to make that translation directly?</blockquote> I think technology is finally starting to get there. Most serializers still want a default constructor, but some also provide mechanisms for constructor injection &mdash; making immutable domain models possible again. <br> Now we just need to be able to validate the input appropriately; you made a point about validation being "a different matter" above that I didn't quite understand. It seems to me that, as long as the input is valid, we can use it to create a proper model object. <br> And if we can automatically return abstract errors like @hotsleeper suggested, then I think we have a full solution. <br> My most recent take on this problem has been to use <a href="http://codyaray.com/2013/01/automatic-model-validation-using-jersey-jackson-and-hibernate-validator">a combination of Jersey, Jackson, and Hibernate Validator</a> to translate from on-the-wire JSON representation to valid domain object. <br> Is there something I'm missing with this approach? <div class="comment-date">2013-05-07 10:45 CDT</div> </div> </div> <div class="comment" id="d4ac92267e234e7ba4cee721fcb0481a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d4ac92267e234e7ba4cee721fcb0481a">#</a></div> <div class="comment-content"> <p> The "different matter" of interpretation is that you need to be able to interpret and validate the incoming data, before you turn it into a Domain Object. An encapsulated Domain Object should throw exceptions if you attempt to create invalid instances, so unless you want to catch exceptions and surface them at the boundary, you'll need to be able to validate the input <em>before</em> you create the Domain Object. </p> <p> There are various technologies where you can annotate your Domain Objects, in order to add such interpretation logic to them, but you should be aware that if you do that, you'd be conflating the interpretation logic with your Domain Model. Is that a problem? Only you can tell, but at least, you must be aware of this before you can make the decision. </p> <p> What happens if you need to interpret XML as well as JSON? What happens if you need to interpret two mutually exlusive versions (v1 and v2) of XML into the same object? What happens if you need to interpret YAML in addition to JSON and XML? Etc. </p> </div> <div class="comment-date">2013-05-08 06:15 UTC</div> </div> <div class="comment" id="a5a074422e434362b9f09263cf3f7ec1"> <div class="comment-author"><a href="mailto:parowoz@gmail.com">Dmitry</a> <a href="#a5a074422e434362b9f09263cf3f7ec1">#</a></div> <div class="comment-content"> <p> 5 years have passed after original post date, but smart thoughts never get old i think. </p> <p> I have a question, concerning <i>"Currently I think that using dynamic types looks most promising."</i><br> Are you still think like that? I assume yes. But is it even "ethical" to use amorphous, general-purpose objects, like that? <br> It's bad to populate objects with property injection, but it's ok to give them method they didn't have? When I'm looking at this Expando I just see something like: <br>- take JSON file with object graph <br>- deserialize to "DTO" graph <br>- give arbitrary DTOs methods, so they now domain objects <br>- seems legit </p> <p> Of cource, im exagerrating. But htat's why:<br> Right now we're starting a project. The only and last thing that bothers me now is exactly this: Client application receives JSON file with moderately large Scenes. While I'm satisfied with overal design I've achieved, there is this one "little" thing. As of now, i was planning to take this JSON, check it against schema (mb add some valitations later), "decompose" it to a number of DTOs and feed them to container in the process of object tree construction and for later use with Factories, that supply runtime objects with certain types and/or properties.<br> I don't like it, but I don't see a better way yet. </p> <p> For example, how do I set up references? Using json's method is ofcourse not an option. Make repository and inject it everywhere? Yet, I don't see it is being Service Locator, I for some reason suspect it's bad. Who will register everyone there, for example? Though, it can be nimble visitor in both cases (but it will demand the addition of specialized interface to all objects just for this concern). <br> And now I recalled about this Expando. And now i fell myself even worse: earlier I was just seeing not-so-good solutions, but in fact acceptable if treated responsibly and carefully. But this expando.. I see how much easier it may make everything, but I don't know how to allow myself to use it (tho the problem may disappear by itself, as we may fall under constrait of useing .NET 3.5). But if you say using it is ok, I will surely do so. That's because I know that I can trust your opinion more, that my own in this context of software design. </p> <p> All in all, I will be happy to hear your opinion on the matter of this article, but <i>n</i> years later. <b>What is the answer to questions raised here by article and comments</b>, ultimately? What transition from boundaries to inner OO paradise is the best in your opinion (and what is second, just in case)? <br>Also, concerning offtopic: can you give me a couple advices on setting up those references (that DI composition will not able to handle)? I think this will be needed only after object graph was builded, so may be do it in composition root right away during cunstruction? I was trying to do this pretty successfully, but i consider those method a "hack". <b> Is it bad to have repository with almost all business objects?</b> </p> </div> <div class="comment-date">2015-12-19 1:40 UTC</div> </div> <div class="comment" id="3beac16eb2df40b3ad5e9967fee7b064"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3beac16eb2df40b3ad5e9967fee7b064">#</a></div> <div class="comment-content"> <p> Dmitry, thank you for writing. This purpose of this post is mostly to help people realise that, as the title says, at the boundaries, applications aren't Object-Oriented (and neither are they Functional). Beyond that, the article is hardly prescriptive. While the <em>What Should Happen at the Boundary</em> section muses on ways to deal with input and output, I hope it doesn't suggest that there are any silver bullets that address these problems. </p> <p> It's a subject not easily addressed here in a comment. A new article wouldn't even do the topic justice; a book would be required to truly cover all the ins and outs. </p> <p> That said, I don't think I ever used the ExpandoObject-approach in practice, although I've used dynamic typing against JSON a lot with Integration Testing. Mostly, I use the <a href="http://fsharpforfunandprofit.com/posts/recipe-part2">Railway oriented programming</a> approach to input validation, but it doesn't address all the other issues with parsing, implementing <a href="http://martinfowler.com/bliki/TolerantReader.html">Tolerant Readers</a>, etc. </p> <p> <blockquote> "I know that I can trust your opinion more, that my own in this context of software design." </blockquote> I don't think you should. All advice is given in a specific context. Sometimes, that context is explicit, but usually it's implicit. I don't know your context (and I confess that I don't understand your more specific questions), so I can only give advice from my own (implicit) context. It may or may not fit your context. </p> <p> This is the reason I take pains to present <em>arguments</em> for my conclusion. While the arguments serve the purpose of trying to convince the reader, they also serve as documentation. From the arguments, the train of thought that leads to a particular conclusion should be clearer to the reader. If a reader disagrees with a premise, he or she will also disagree with the conclusion. That's OK; software development is not a one-size-fits-all activity. </p> </div> <div class="comment-date">2015-12-19 10:35 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Design Smell: Default Constructor https://blog.ploeh.dk/2011/05/30/DesignSmellDefaultConstructor 2011-05-30T13:02:02+00:00 Mark Seemann <div id="post"> <p> This post is the fifth in a series about <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a> - also known as <em>encapsulation</em>. </p> <p> Default constructors are code smells. There you have it. That probably sounds outrageous, but consider this: object-orientation is about <em>encapsulating</em> behavior <em>and data</em> into cohesive pieces of code (classes). Encapsulation means that the class should protect the integrity of the data it encapsulates. When data is required, it must often be supplied through a constructor. Conversely, a default constructor implies that no external data is <em>required</em>. That's a rather weak statement about the invariants of the class. </p> <blockquote> <p> Please be aware that this post represents a <em>smell</em>. This indicates that whenever a certain idiom or pattern (in this case a default constructor) is encountered in code it should trigger further investigation. </p> <p> As I will outline below, there are several scenarios where default constructors are perfectly fine, so the purpose of this blog post is not to thunder against default constructors. It's to provide food for thought. </p> </blockquote> <p> If you have read <a href="http://amzn.to/12p90MG">my book</a> you will know that Constructor Injection is the dominating DI pattern exactly because it statically advertises dependencies and protects the integrity of those dependencies by guaranteeing that an initialized consumer is always in a consistent state. This is fail-safe design because <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">the compiler can enforce the relationship, thus providing rapid feedback</a>. </p> <p> This principle extends far beyond DI. In a <a href="/2011/05/24/DesignSmellTemporalCoupling">previous post</a> I described how a constructor with arguments statically advertises that the argument is required: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">Fragrance</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IFragrance</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#0000ff">string</font></span> name;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Fragrance(<span style="color: "><font color="#0000ff">string</font></span> name)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (name == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"name"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.name = name;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">string</font></span> Spread()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.name;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> The Fragrance class protects the integrity of the name by requiring it through the constructor. Since this class requires the name to implement its behavior, requesting it through the constructor is the correct thing to do. A default constructor would not have been fail-safe, since it would introduce a <a href="/2011/05/24/DesignSmellTemporalCoupling">temporal coupling</a>. </p> <p> Consider that objects are supposed to be containers of behavior and data. Whenever an object contains data, the data must be encapsulated. In the (very common) case where no meaningful default value can be defined, the data must be provided via the constructor. Thus, default constructors might indicate that encapsulation is broken. </p> <h3 id="9d7a019a9a5e488e8c3d5764fcddb3c7"> When are Default Constructors OK? <a href="#9d7a019a9a5e488e8c3d5764fcddb3c7" title="permalink">#</a> </h3> <p> There are still scenarios where default constructors are in order (I'm sure there are more than those listed here). </p> <ul> <li> If a default constructor can assign meaningful default values to all contained fields a default constructor still protects the invariants of the class. As an example, the default constructor of <a href="http://msdn.microsoft.com/en-us/library/system.uribuilder.aspx">UriBuilder</a> initializes its internal values to a consistent set that will build the Uri http://localhost unless one or more of its properties are subsequently manipulated. You may agree or disagree with this default behavior, but it's consistent and so encapsulation is preserved. </li> <li> If a class contains no data obviously there is no data to protect. This may be a symptom of the <a href="http://c2.com/cgi/wiki?FeatureEnvySmell">Feature Envy</a> code smell, which is often evidenced by the class in question being a concrete class. <ul> <li>If such a class can be turned into a static class it's a certain sign of Feature Envy.</li> <li>If, on the other hand, the class implements an interface, it might be a sign that it actually represents pure behavior.</li> </ul> </li> </ul> <p> A class that represents pure behavior by implementing an interface is not necessarily a bad thing. This can be a very powerful construct. </p> <p> In summary, a default constructor should be a signal to stop and think about the invariants of the class in question. Does the default constructor sufficiently guarantee the integrity of the encapsulated data? If so, the default constructor is appropriate, but otherwise it's not. In my experience, default constructors tend to be the exception rather than the rule. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="199da0368def4713a7ad97c439fed480"> <div class="comment-author"><a href="http://Systemutvecklaren.blogspot.com">Per</a> <a href="#199da0368def4713a7ad97c439fed480">#</a></div> <div class="comment-content">Agreed.<br> My most common scenario for the default constructor is when some (probably reflection-based) framework requires it.<br> Don't like them, at all. There are always some dependencies, and excluding them from the ctor means you are introducing them in an uglier place...<br> <br> Good post!</div> <div class="comment-date">2011-05-30 16:24 UTC</div> </div> <div class="comment" id="b9e9cbb3427b41e6958b8259a00e28fc"> <div class="comment-author">martin <a href="#b9e9cbb3427b41e6958b8259a00e28fc">#</a></div> <div class="comment-content">So if I have a DTO class(only properties), all the properties should be provided in the contructor or the class should be contructed by some builder ?</div> <div class="comment-date">2011-05-30 20:06 UTC</div> </div> <div class="comment" id="08c81a2756a742a4954b3fbd50524a35"> <div class="comment-author"><a href="http://blog.bobcravens.com">Bob Cravens</a> <a href="#08c81a2756a742a4954b3fbd50524a35">#</a></div> <div class="comment-content">One caveat is the case of de- serialization.</div> <div class="comment-date">2011-05-31 00:32 UTC</div> </div> <div class="comment" id="a75e2a40abbc416182388049eee12c40"> <div class="comment-author"><a href="http://www.neovolve.com">Rory Primrose</a> <a href="#a75e2a40abbc416182388049eee12c40">#</a></div> <div class="comment-content">Loving this series Mark. Nice work :)</div> <div class="comment-date">2011-05-31 01:14 UTC</div> </div> <div class="comment" id="5914a0beaf57484fa3cbd88db5f1935b"> <div class="comment-author">Ken <a href="#5914a0beaf57484fa3cbd88db5f1935b">#</a></div> <div class="comment-content">I would like to point out if you are using expression blend you need a default constructor on your view otherwise Blend can't open it. Something like this. <br> <br> public ReturnPolicyManagementView(): base()<br> {<br> InitializeComponent();<br> }<br> public ReturnPolicyManagementView(ReturnPolicyManagementViewModel model) : this()<br> {<br> this.DataContext = model;<br> }<br> <br> You can of course get away with this because WPF bindings fail without causing an exception. </div> <div class="comment-date">2011-05-31 15:29 UTC</div> </div> <div class="comment" id="2b41040723fb431ab8ddbd5410b92bcd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2b41040723fb431ab8ddbd5410b92bcd">#</a></div> <div class="comment-content">Martin, Bob, Ken, I wrote <a href="/2011/05/24/DesignSmellTemporalCoupling">this postscript that essentially explains why such boundary code isn't object-oriented code</a>. At the boundaries, encapsulation doesn't apply.</div> <div class="comment-date">2011-05-31 18:17 UTC</div> </div> <div class="comment" id="06fd5f0e58b84cd281ea4225d6246b04"> <div class="comment-author"><a href="http://www.programmers-unlimited.com">Dustin Davis</a> <a href="#06fd5f0e58b84cd281ea4225d6246b04">#</a></div> <div class="comment-content">You have such a unique way of looking at code &amp; constructs, it's eye opening to say the least. I enjoy how you turn the widely accepted into a beast of evil :)<br> <br> Attacking default constructors is either mad or madly genius. </div> <div class="comment-date">2011-05-31 20:08 UTC</div> </div> <div class="comment" id="ba743709face4796a0aac779c2de5114"> <div class="comment-author"><a href="http://www.clear-lines.com/blog/">Mathias</a> <a href="#ba743709face4796a0aac779c2de5114">#</a></div> <div class="comment-content">Nice series. This post reminded me of a painful realization I made lately, which is that by design, a struct must have a parameterless default constructor. Does this make structs evil in your book, except for cases where the parameterless constructor makes sense?</div> <div class="comment-date">2011-06-05 07:39 UTC</div> </div> <div class="comment" id="4163ca984d8c40f69341a7723ced499f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4163ca984d8c40f69341a7723ced499f">#</a></div> <div class="comment-content">structs aren't evil - as with most other language constructs, it's a matter of understanding when and how to apply them. Basically, a struct is (or should be) a specialized representation of a number. Int32, Decimal, DateTime, TimeSpan etc. all fall into this category, and if you have a similar domain-specific concept in you code base, a struct could be a good representation as long as 0 is a valid value. A hypothetical Temperature type would be an excellent candidate.<br> <br> An example where a struct was inappropriately applied would by Guid, whose default value is Guid.Empty. Making Guid a struct adds no value because you'd always conceptually have to check whether you just received Guid.Empty. Had Guid been a reference type we could just have checked for null, but with this design choice we need to have special cases for handling exactly Guids. This means that we have to write special code that deals only with Guids, instead of code that just deals with any reference type.</div> <div class="comment-date">2011-06-05 08:02 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Design Smell: Redundant Required Attribute https://blog.ploeh.dk/2011/05/27/DesignSmellRedundantRequiredAttribute 2011-05-27T13:21:06+00:00 Mark Seemann <div id="post"> <p> This post is the fourth in a series about <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a> - also known as <em>encapsulation</em>. </p> <p> Recently I saw this apparently enthusiastic tweet reporting from some Microsoft technology event: </p> <blockquote> <p> <a href="https://twitter.com/#!/jennifermarsman/status/57850114424848385">[Required] attribute in code automatically creates a non-nullable entry in DB and validation in the webpage - nice […]</a> </p> </blockquote> <p> I imagine that it must look something like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Smell</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">Required</font></span>]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">int</font></span> Id { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Every time I see something like this I die a little inside. If you already read my previous posts it should by now be painfully clear why this breaks encapsulation. Despite the [Required] attribute there's <a href="/2011/05/24/DesignSmellTemporalCoupling">no guarantee that the Id property will ever be assigned a value</a>. The attribute is just a piece of garbage making a claim it can't back up. </p> <p> Code like that is not fail-safe. </p> <p> I understand that the attribute mentioned in the above tweet is intended to signal to some tool (probably EF) that the property must be mapped to a database schema as non-nullable, but it's still redundant. Attributes are not the correct way to make a statement about invariants. </p> <h3 id="19c9811ac7ea433c9d327d04b9845952"> Improved Design <a href="#19c9811ac7ea433c9d327d04b9845952" title="permalink">#</a> </h3> <p> The [Required] attribute is redundant because there's a much better way to state that a piece of data is required. This has been possible since .NET 1.0. Here's the Poka-yoke version of that same statement: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Fragrance</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#0000ff">int</font></span> id;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Fragrance(<span style="color: "><font color="#0000ff">int</font></span> id)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.id = id;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">int</font></span> Id</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.id; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This simple structural design ensures that the ID truly is required (and if the ID can only be positive a Guard Clause can be added). An instance of Fragrance can only be created with an ID. Since this is a structural construction, the <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">compiler can enforce the requirement, giving us rapid feedback</a>. </p> <p> I do realize that the [Required] attribute mentioned above is intended to address the challenge of mapping objects to relational data and rendering, but instead of closing the impedance mismatch gap, it widens it. Instead of introducing yet another redundant attribute the team should have made their tool understand simple idioms for encapsulation like the one above. </p> <p> This isn't at all hard to do. As an example, DI Containers thrive on structural information encoded into constructors (this is called <em>Auto-wiring</em>). The team behind the [Required] attribute could have done that as well. The [Required] attribute is a primitive and toxic hack. </p> <p> This is the major reason I never expect to use EF. It forces developers to break encapsulation, which is a principle upon which I refuse to compromise. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="10bbd17088de4776b463d000c6a8c87b"> <div class="comment-author"><a href="http://benfulton.net">Ben Fulton</a> <a href="#10bbd17088de4776b463d000c6a8c87b">#</a></div> <div class="comment-content">Seems like overkill if all you have is a piece of data that you want to display on a web page. Are you against using Entity Framework to fill Data Transfer Objects to be displayed?</div> <div class="comment-date">2011-05-27 13:59 UTC</div> </div> <div class="comment" id="53050530acff48579ba3bca019ea5222"> <div class="comment-author"><a href="http://www.garethelms.org">Gareth Elms</a> <a href="#53050530acff48579ba3bca019ea5222">#</a></div> <div class="comment-content">Agreed, I'd only use [Required] for validation on a view model</div> <div class="comment-date">2011-05-27 14:35 UTC</div> </div> <div class="comment" id="aaddaeb515734192bd54165d12747951"> <div class="comment-author"><a href="http://blogs.teamb.com/craigstuntz/">Craig Stuntz</a> <a href="#aaddaeb515734192bd54165d12747951">#</a></div> <div class="comment-content">You misstate the intended purpose of the attribute, because you cite a tweet instead of the documentation. <br> <br> It is true that most code-to-schema-mapping-tools, including EF code-first, Fluent NHibernate, and others, will infer non-nullability from [Required] if it happens to be present on a type which is actually nullable. Why shouldn't they? I realize it's cool for the hip kids to flame EF at every opportunity, but good grief, it didn't cause the tornadoes in Memphis, and it didn't create (nor does it &quot;require&quot;) [Required]...<br> <br> The documentation makes the intended purpose of the attribute clear:<br> <br> &quot;The RequiredAttribute attribute specifies that when a field on a form is validated, the field must contain a value. A validation exception is raised if the property is null, contains an empty string (&quot;&quot;), or contains only white-space characters.&quot;<br> <br> Your &quot;imagined&quot; code example is indeed redundant even in the case where someone is trying to create a non-nullable DB field instead of using the attribute for its intended purpose, because integer properties, being value types, are always mapped to non-nullable fields, with or without [Required]. One must use &quot;int?&quot; to get a nullable DB field; [Required] has no effect on &quot;int&quot; at all. (Indeed, the MVC framework, e.g., treats [Required] and value types exactly the same.)<br> <br> Now, let's consider the actual purpose of required, per the documentation, and see if it's redundant. Consider a view model with two &quot;required&quot; strings. The business requirement is that if either one or both of them do not contain a non-empty string, the web page should highlight the editors in red and display an appropriate message to the user. One could write a parameterized constructor and throw if they're not present, per your example, but you only get one chance to throw an exception, so you would have to handle three separate cases: The first field missing, the second field missing, or both fields missing. Better to use a validation framework which handles all of these cases for you -- which is exactly what [Required] is intended for.<br> <br> I would argue that such a parameterized constructor with one or more throws is the wrong design in this scenario, because view models must be able to cope with the task of re-displaying invalid data entered by the user so that the user can correct her mistakes. In contrast to a business object, it must be able to hold and report on invalid states. </div> <div class="comment-date">2011-05-27 15:19 UTC</div> </div> <div class="comment" id="9cb706e15fa84a93909c7345bb76b6b8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9cb706e15fa84a93909c7345bb76b6b8">#</a></div> <div class="comment-content">Ben, if all you have &quot;is a piece of data that you want to display on a web page&quot; you're most likely in the CRUD application scenario. There are cases where this is indeed the correct solution to a business problem. It's perfectly fine, but it has nothing to do with object-orientation.<br> <br> This whole series of posts about Poka-yoke Design are about encapsulation, which is an object-oriented principle. And yes: when it comes to OOD I'd never use EF (or any other ORM) if it requires me to break encapsulation.</div> <div class="comment-date">2011-05-28 21:29 UTC</div> </div> <div class="comment" id="5145eda4075040ed8509fac6442ef3ed"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5145eda4075040ed8509fac6442ef3ed">#</a></div> <div class="comment-content">Craig, I agree that when it comes to user input validation, using an attribute on one or more properties with very loose invariants is the best solution. In general, when at the boundary of an application, it's always best to assume that input can be invalid in all sorts of ways. This is true for UI input as well as automated input (XML, CSV, JSON, etc.). Thus, any 'object' modeling an application boundary data structure is essentially <em>not</em> encapsulated.<br> <br> However, it would be a grave mistake if we let UI concerns drive the design of our domain models. Thus, as soon as we know that we have a case of valid input data on our hands, we must translate it to a proper encapsulated object. From that point on (including db schema design) an attribute would certainly be redundant.<br> <br> BTW, don't read to much into the tweet. Perhaps I chose the wrong example, and it was never my intention to thunder particularly at EF. So far, all I've heard about NHibernate indicates to me that it has similar issues with regards to encapsulation.</div> <div class="comment-date">2011-05-28 21:42 UTC</div> </div> <div class="comment" id="1aed36ae10c6417d8735ef64aafc2f5f"> <div class="comment-author"><a href="http://blogs.teamb.com/craigstuntz/">Craig Stuntz</a> <a href="#1aed36ae10c6417d8735ef64aafc2f5f">#</a></div> <div class="comment-content">I like what you said in above: &quot;Thus, any 'object' modeling an application boundary data structure is essentially not encapsulated.&quot; That's an insight that many people miss. <br> <br> My personal take on this is that data service interfaces are boundary structures themselves, and should be BOs in the same way that view models shouldn't be BOs, either. </div> <div class="comment-date">2011-05-31 14:56 UTC</div> </div> <div class="comment" id="27ff6a4691564c988eda9ae5a7aa3dd4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#27ff6a4691564c988eda9ae5a7aa3dd4">#</a></div> <div class="comment-content">When you say 'BO', do you mean 'Boundary Object' or 'Business Object'? It seems to me that you might use both abbreviations in the same sentence... or I'm totally missing something :)</div> <div class="comment-date">2011-05-31 18:19 UTC</div> </div> <div class="comment" id="ee280010a17442d8a18034c7c84eb589"> <div class="comment-author"><a href="http://blogs.teamb.com/craigstuntz/">Craig Stuntz</a> <a href="#ee280010a17442d8a18034c7c84eb589">#</a></div> <div class="comment-content">Sorry, I meant business object, and &quot;should&quot; should have been &quot;shouldn't.&quot; Not the clearest sentence I ever wrote!</div> <div class="comment-date">2011-05-31 20:39 UTC</div> </div> <div class="comment" id="e29b28a709044c48b44991028d856f77"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e29b28a709044c48b44991028d856f77">#</a></div> <div class="comment-content">Gotcha! Agreed :)</div> <div class="comment-date">2011-05-31 20:40 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Code Smell: Automatic Property https://blog.ploeh.dk/2011/05/26/CodeSmellAutomaticProperty 2011-05-26T13:33:13+00:00 Mark Seemann <div id="post"> <p> This post is the third in a series about <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a> - also known as <em>encapsulation</em>. </p> <p> <a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx">Automatic properties</a> are one of the most redundant features of C#. I know that some people really love them, but they address a problem you shouldn't have in the first place. </p> <p> I totally agree that code like this looks redundant: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> name;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> Name</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.name; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">set</font></span> { <span style="color: "><font color="#0000ff">this</font></span>.name = <span style="color: "><font color="#0000ff">value</font></span>; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> However, the solution is not to write this instead: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> Name { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font></pre> </p> <p> The problem with the first code snippet isn't that it contains too much ceremony. The problem is that it breaks encapsulation. In fact </p> <blockquote> <p> "[…] getters and setters do not achieve encapsulation or information hiding: they are a language-legitimized way to violate them." </p> <p> James O. Coplien &amp; Gertrud Bjørnvig. <em>Lean Architecture</em>. Wiley. 2010. p. 134. </p> </blockquote> <p> While I personally think that properties do have their uses, I very rarely find use for <em>automatic</em> properties. They are never appropriate for reference types, and only rarely for value types. </p> <h3 id="1eaa405a60cd45e1bf9d7149e1ff0943"> Code Smell: Automatic Reference Type Property <a href="#1eaa405a60cd45e1bf9d7149e1ff0943" title="permalink">#</a> </h3> <p> First of all, let's consider the very large set of properties that expose a reference type. </p> <p> In the case of reference types, null is a possible value. However, when we think about Poka-yoke design, null is never an appropriate value because it leads to NullReferenceExceptions. The <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a> pattern provides a better alternative to deal with situations where a value might be undefined. </p> <p> In other words, an automatic property like the Name property above is never appropriate. The setter must have some kind of Guard Clause to protect it against null (and possibly other invalid values). Here's the most fundamental example: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> name;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> Name</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.name; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">set</font></span> </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (<span style="color: "><font color="#0000ff">value</font></span> == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"value"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.name = <span style="color: "><font color="#0000ff">value</font></span>; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> As an alternative, a Guard Clause could also check for null and provide a default Null Object in the cases where the assigned value is null: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> name;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> Name</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.name; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">set</font></span> </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (<span style="color: "><font color="#0000ff">value</font></span> == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.name = <span style="color: "><font color="#a31515">""</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.name = <span style="color: "><font color="#0000ff">value</font></span>; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> However, this implementation contains a <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment">POLA</a> violation because the getter sometimes returns a different value than what was assigned. It's possible to fix this problem by adding an associated boolean field indicating whether the name was assigned null so that null can be returned from the setter in this special case, but that leads to another code smell. </p> <h3 id="3490ddefdf424609aadd3de09e5e7702"> Code Smell: Automatic Value Type Property <a href="#3490ddefdf424609aadd3de09e5e7702" title="permalink">#</a> </h3> <p> If the type of the property is a value type, the case is less clear-cut because value types can't be null. This means that a Null Guard is never appropriate. However, directly consuming a value type may still be inappropriate. In fact, <a href="/2011/05/25/DesignSmellPrimitiveObsession">it's only appropriate if the class can meaningfully accept and handle any value of that type</a>. </p> <p> If, for example, the class can really only handle a certain subset of all possible values, a Guard Clause must be introduced. Consider this example: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">int</font></span> RetryCount { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font></pre> </p> <p> This property might be used to set the appropriate number or retries for a given operation. The problem with using an automatic property is that it's possible to assign a negative value to it, and that wouldn't make any sense. One possible remedy is to add a Guard Clause: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">int</font></span> retryCount;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">int</font></span> RetryCount</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.retryCount; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#0000ff">set</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (<span style="color: "><font color="#0000ff">value</font></span> &lt; 0)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentOutOfRangeException</font></span>();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.retryCount = <span style="color: "><font color="#0000ff">value</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> However, in many cases, exposing a primitive property is more likely to be a case of Primitive Obsession. </p> <h3 id="a70c0c92b4194d15bb972e6cb0867936"> Improved Design: Guard Clause <a href="#a70c0c92b4194d15bb972e6cb0867936" title="permalink">#</a> </h3> <p> As I described above, the most immediate fix for automatic properties is to properly implement the property with a Guard Clause. This ensures that the class' invariants are properly encapsulated. </p> <h3 id="a2f6245ecd5e4532a95a5d8c2e0b488a"> Improved Design: Value Object Property <a href="#a2f6245ecd5e4532a95a5d8c2e0b488a" title="permalink">#</a> </h3> <p> When the automatic property is a value type, a Guard Clause may still be in order. However, when the property is really a symptom of Primitive Obsession, a better alternative is to introduce a proper Value Object. </p> <p> Consider, as an example, this property: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">int</font></span> Temperature { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font></pre> </p> <p> This is bad design for a number of reasons. It doesn't communicate the unit of measure and allows unbounded values to be assigned. What happens if - 100 is assigned? If the unit of measure is Celcius it should succeed, although in the case when it's Kelvin, it should fail. No matter the unit of measure, attempting to assign int.MinValue should fail. </p> <p> A more robust design can be had if we introduce a new Temperature type and change the property to have that type. Apart from protection of invariants it would also encapsulate conversion between different temperature scales. </p> <p> However, if that Value Object is implemented as a reference type the situation is equivalent to the situation described above, and a Null Guard is necessary. Only in the case where the Value Object is implemented as a value type is an anonymous property appropriate. </p> <p> The bottom line is that automatic properties are rarely appropriate. In fact, they are only appropriate when the type of the property is a value type and all conceivable values are allowed. Since there <em>are</em> a few cases where automatic properties are appropriate their use can't be entirely dismissed, but it should be treated as warranting further investigation. It's a code smell, not an anti-pattern. </p> <p> On a different note properties also violate the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a>, but that's the topic of a future blog post... </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="67f99bbcd1a44a80a7799072fda43fa6"> <div class="comment-author">James Nail <a href="#67f99bbcd1a44a80a7799072fda43fa6">#</a></div> <div class="comment-content">Hi Mark,<br> I'm enjoying this series, and you can be sure I'll be sharing it with my teammates.<br> I wanted to point out a typo that might confuse readers, though -- you used the term &quot;anonymous properties&quot; instead of &quot;automatic properties&quot; toward the end of the article, which threw me off at first.<br> Anyway, keep up the great work!</div> <div class="comment-date">2011-05-26 14:07 UTC</div> </div> <div class="comment" id="92db359bb9c9456ea9f825d76b7ada93"> <div class="comment-author">Omer Katz <a href="#92db359bb9c9456ea9f825d76b7ada93">#</a></div> <div class="comment-content">What about NetworkStream's Connected property?<br> It has a private set and a public get and returns a boolean that indicates if the underlaying socket is connected.<br> Nothing wrong with that.<br> <br> By your logic, any .NET ORM is bad because it uses properties and violates the Law of Demeter.</div> <div class="comment-date">2011-05-26 14:09 UTC</div> </div> <div class="comment" id="992d909bd85042e7b4c3c500aa403b52"> <div class="comment-author"><a href="http://bentaylor.org/">Ben Taylor</a> <a href="#992d909bd85042e7b4c3c500aa403b52">#</a></div> <div class="comment-content">Isn't the code smell &quot;public setters&quot; rather than automatic properties? Making the set protected or private solves this issue. E.g.<br> <br> public decimal MyValue { get; private set; }<br> <br> With such usages it is legitimate for the guard code to be in a state mutating method, constructor validation etc<br> </div> <div class="comment-date">2011-05-26 14:15 UTC</div> </div> <div class="comment" id="1d9cbed86c64497690874cc709361c1e"> <div class="comment-author"><a href="http://alexhumphrey.me.uk">Alex Humphrey</a> <a href="#1d9cbed86c64497690874cc709361c1e">#</a></div> <div class="comment-content">Good points, and I completely agree with the { get; set; } kind of auto-implemented properties - in fact I don't think I've ever checked one in.<br> <br> However, I'm forever using the {get; private set; } kind of auto-implemented properties - what are your opinions on those? What about a hypothetical and confusingly named { get; readonly set; } auto-implemented property?</div> <div class="comment-date">2011-05-26 14:25 UTC</div> </div> <div class="comment" id="f914db2b9cd04bb7ab37e359ca04c267"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f914db2b9cd04bb7ab37e359ca04c267">#</a></div> <div class="comment-content">James, thanks for pointing that out. I've now corrected the error.</div> <div class="comment-date">2011-05-26 18:31 UTC</div> </div> <div class="comment" id="9690caccb74a4eb5911e72d3563a9e52"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9690caccb74a4eb5911e72d3563a9e52">#</a></div> <div class="comment-content">Omer, a boolean property falls into that rare case described during the discussion of value type properties. If the class exposing the property truly can accept any value that the type can possibly take, an automatic property is in order. A boolean value can only take one of two values, and both are legal. Thus, no Guard Clause is required. However, please notice that this scenario is not (or at least should not be) representative.<br> <br> Regarding ORMs you are now jumping ahead of me :) I'll come to that in a later blog post, but yes, I genuinely believe that the whole concept of an ORM (at least in any incarnations I've seen so far) is a fallacy.</div> <div class="comment-date">2011-05-26 18:39 UTC</div> </div> <div class="comment" id="9a08c5f0d8f74c75abe24bfd64648499"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9a08c5f0d8f74c75abe24bfd64648499">#</a></div> <div class="comment-content">Ben, you are correct that the issue is at least most prevalent when the setter is public. However, keep in mind that we should always treat protected as public, as anyone can always come by and derive from a class.<br> <br> For private setters it's not so clear-cut. In most cases when I have read-only properties it tends to be because the property is being supplied through the constructor and subsequently treated as immutable. When that is the case, I much prefer a backing field because it can be declared as readonly.<br> <br> In other words I rarely write code where the class itself can mutate the value of a read-only property, but I know that other people do. When this is the case, I could argue that I'd still prefer the setter to protect the invariants of the class, but that argument tends to become a little silly because with a backing field the class could always just mutate the value directly. In any case, if one has a class that needs that kind of protection from its own internals one probably has more serious problems :)<br> <br> Keep in mind that this whole discussion about encapsulation relates to the <i>public</i> API of classes. Obviously, once you start poking around in the internals of a class, you're already looking at the source code and thus past the encapsulation perimeter.</div> <div class="comment-date">2011-05-26 18:50 UTC</div> </div> <div class="comment" id="4bbef5aceaeb4097a936da7f3a22bb25"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4bbef5aceaeb4097a936da7f3a22bb25">#</a></div> <div class="comment-content">Alex, see my previous comment regarding { get; private set; }<br> <br> Regarding { get; readonly set; } I would probably use something like this assuming this was something that could only be used from the constructor, because I'd still be able to add the appropriate Guard Clause in the constructor before invoking the setter.</div> <div class="comment-date">2011-05-26 18:54 UTC</div> </div> <div class="comment" id="03c022c00b6f4900aeb486e8f5434846"> <div class="comment-author"><a href="http://code.dortikum.net">Boyan Mihaylov</a> <a href="#03c022c00b6f4900aeb486e8f5434846">#</a></div> <div class="comment-content">I totally agree with your post. However, there are some cases when you do not need to make checks for validity for reference types. If a class with such a property does not provide a default non-null value for it, then any other checks are redundant as the user may set or may not set a value to that property. This way it will still have its default value of NULL.<br> <br> Using properties with public setters in really tricky. When you have a class with properties and methods, users do not know which properties should be set before a method is called which will result in a run-time exception. That's why required parameters should be provided directly to constructors and methods.</div> <div class="comment-date">2011-05-27 07:52 UTC</div> </div> <div class="comment" id="60f7fa7c68534106863ff1232ea99ae6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#60f7fa7c68534106863ff1232ea99ae6">#</a></div> <div class="comment-content">If the value is optional a public setter is in order. However, allowing the field to be null is not, as it would require the rest of the class to have to check for null every time it uses the field. A Null Object is a better alternative, which again leads to the rule that a Null Guard is always appropriate for reference types. However, in certain cases, that Null Guard might not throw an exception, but rather replace the null with a Null Object.</div> <div class="comment-date">2011-05-29 08:55 UTC</div> </div> <div class="comment" id="2e9d5afd3d4a40c0a916e2653fd6fb66"> <div class="comment-author">Theo Andersen <a href="#2e9d5afd3d4a40c0a916e2653fd6fb66">#</a></div> <div class="comment-content">Hi Mark,<br> I really like your design smell series posts, and i have a few questions.<br> <br> I get and agree on your points about encapsulation and Automatic Properties, but as i see it a lot of frameworks forces you to break this encapsulation. An example is XML Serialization (ISerializable), which needs an empty constructor and then access to all the public properties to set them.<br> <br> Couldn't this lead to both the automatic property and temporal coupling smells, as you need to have basic properties so the framework can set them, plus an empty constructor which then breaks your temporal encapsulation? <br> To adhere to the serialization interface you're forced to open up your code (or create a DTO).<br> <br> </div> <div class="comment-date">2011-05-30 09:18 UTC</div> </div> <div class="comment" id="71e1287804644e9ba8aeed47c4513c6e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#71e1287804644e9ba8aeed47c4513c6e">#</a></div> <div class="comment-content">Theo, please see <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">my latest post</a> that touches on exactly that point.</div> <div class="comment-date">2011-05-31 18:11 UTC</div> </div> <div class="comment" id="2239435f6eee4edca6c0ec94d83fb5b5"> <div class="comment-author"><a href="http://cangencer.wordpress.com">Can Gencer</a> <a href="#2239435f6eee4edca6c0ec94d83fb5b5">#</a></div> <div class="comment-content">I wonder if there is a more elegant way to do this than creating guard clauses for every reference type property. It would have been nice to do this in a more declerative way. I posted a question on SO as well: http://stackoverflow.com/questions/6773189/auto-implemented-properties-with-non-null-check</div> <div class="comment-date">2011-07-21 16:38 UTC</div> </div> <div class="comment" id="ff30e7ed785c4758bb1f8554dc71bc5b"> <div class="comment-author">Ben Robinson <a href="#ff30e7ed785c4758bb1f8554dc71bc5b">#</a></div> <div class="comment-content">Your points are arguments against using automatic properties inappropriately and are far from general cases. There are perfectly valid reasons to allow the setting of properties to null (e.g. using ORM how would you set a property mapped DB field to null) i think your example of a setter that throws a null reference exception is a code smell itself unless you have a specific reason for disallowing it. Your example of the RetryCount is simply an inappropriate use of an automatic property not at all an argument against automatic properties. Your example of temperature is an example of inappropriate type and has nothing to do with automatic properties at all, with the correct type an automatic property would make perfect sense as you could build the validation into the Temperature class. Automatic properties as just shorthand for simple field backed properties and where simple field back properties are correct then so are automatic properties. Simple field backed properties with no validation are sometimes used when they shouldn't be, but that has nothing to do with automatic properties.</div> <div class="comment-date">2011-09-02 08:38 UTC</div> </div> <div class="comment" id="9bd5e41e51484b87bfc0847a7d172f35"> <div class="comment-author"><a href="http://blog.billyang.me">Bill</a> <a href="#9bd5e41e51484b87bfc0847a7d172f35">#</a></div> <div class="comment-content">I agree with Ben's last point, I believe automatic property(or simple field backed property) is just a way to deal with how C# compiler does assembly linking, and using simple properties allows minor modifications to them without recompiling all dependent assemblies. <br> <br> I somewhat agree to your Guard Clause argument, it's something I should pay more attention to instead of blindly implementing automatic properties. But I do have some doubts, in many cases, there is no proper default value, like Birth Date, Country, etc. In fact, I think that's the case most of the time, how would you deal with those situations?</div> <div class="comment-date">2012-01-10 17:04 UTC</div> </div> <div class="comment" id="2d04cf588f8540ddb1cc2d93012518e1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2d04cf588f8540ddb1cc2d93012518e1">#</a></div> <div class="comment-content">Bill, if there's no proper default value <a href="/2011/05/30/DesignSmellDefaultConstructor">the constructor should require that the caller supplies an initial value</a>.</div> <div class="comment-date">2012-01-15 16:40 UTC</div> </div> <div class="comment" id="2d04cf588f8540ddb1cc2d93012518e2"> <div class="comment-author">Juliano Leal Goncalves <a href="#2d04cf588f8540ddb1cc2d93012518e2">#</a></div> <div class="comment-content">Mark, have you considered revisiting this topic or adding some more info to this particular post after the introduction of automatic readonly properties? Most of what you discuss here is still applicable, but some of the newer constructs prevents a fair share of the problems associated with properties.</div> <div class="comment-date">2018-03-20 14:15 UTC</div> </div> <div class="comment" id="389e594137564a3bb5c9547807db3614"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#389e594137564a3bb5c9547807db3614">#</a></div> <div class="comment-content"> <p> Juliano, thank you for writing. You're right that after the publication of this post, C# got more features, at least one of which seems to relate to this article. When I write C# code today, I often use automatic read-only properties, combined with appropriate Guard Clauses in the constructor. I find that a useful language feature. </p> <p> That particular later language feature (automatically implemented read-only properties) is, however, only tangentially related to the topic of this post. Did you have some other language construct in mind? </p> </div> <div class="comment-date">2018-03-20 16:12 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Design Smell: Primitive Obsession https://blog.ploeh.dk/2011/05/25/DesignSmellPrimitiveObsession 2011-05-25T15:03:31+00:00 Mark Seemann <div id="post"> <p> This post is the second in a series about <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a> - also known as <em>encapsulation</em>. </p> <p> Many classes have a tendency to consume or expose primitive values like integers and strings. While such primitive types exist on any platform, they tend to lead to procedural code. Furthermore they often break encapsulation by allowing invalid values to be assigned. </p> <p> This problem has been addressed many times before. Years ago <a href="http://lostechies.com/jimmybogard/">Jimmy Bogard</a> provided an <a href="http://grabbagoft.blogspot.com/2007/12/dealing-with-primitive-obsession.html">excellent treatment of the issue, as well as guidance on how to resolve it</a>. In relation to <a href="http://autofixture.codeplex.com/">AutoFixture</a> I also <a href="/2009/05/01/DealingWithConstrainedInput">touched upon the subject some time ago</a>. As such, the current post is mostly a placeholder. </p> <p> However, it's worth noting that both Jimmy's and my own post address the concern that strings and integers do not sufficiently encapsulate the concepts of Zip codes and phone numbers. </p> <ul> <li> When a Zip code is represented as a string it's possible to assign values such as null, string.Emtpy, “foo”, very long strings, etc. Jimmy's ZipCode class encapsulates the concept by guaranteeing that an instance can only be successfully created with a correct value.</li> <li> When a Danish phone number is represented as an integer it's possible to assign values such as - 98, 0, int.MaxValue, etc. Once again the DanishPhoneNumber class from the above example encapsulates the concept by guaranteeing that an instance can only be successfully created with a correct value. </li> </ul> <p> Encapsulation is broken unless the concept represented by a primitive value can truly take <em>any</em> of the possible values of the primitive type. This is rarely the case. </p> <h3 id="3c26a431c4524627ad4d52abfd52e4d0"> Design Smell <a href="#3c26a431c4524627ad4d52abfd52e4d0" title="permalink">#</a> </h3> <p> A class consumes a primitive type. However, further analysis shows that not all possible values of the type are legal values. </p> <h3 id="fbbebe4b554444e488b876e622081320"> Improved Design <a href="#fbbebe4b554444e488b876e622081320" title="permalink">#</a> </h3> <p> <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">Encapsulate the primitive value in a Value Object that contains appropriate Guard Clauses etc.</a> to guarantee that only valid instances are possible. </p> <p> Primitives tend to not be fail-safe, but encapsulated Value Objects are. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8543cc2a574c4d099076600e398f7665"> <div class="comment-author">Jon Wingfield <a href="#8543cc2a574c4d099076600e398f7665">#</a></div> <div class="comment-content">What do you mean by this statement:<br> <br> Encapsulation is broken unless the concept represented by a primitive value can truly take any of the possible values of the primitive type. This is rarely the case.<br> <br> Should a Qty field that only takes positive integers now be represented by a separate class? You seem to be defining encapsulation as compile-safe or something.</div> <div class="comment-date">2011-05-25 15:52 UTC</div> </div> <div class="comment" id="53ac65924d7d4301b1f824c77394376c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#53ac65924d7d4301b1f824c77394376c">#</a></div> <div class="comment-content">I would very much like to be able to represent a quantity as a specialized class. Unfortunately <i>uint</i> is not a CLS-compliant type - otherwise I would use that one a lot.<br> <br> Keep in mind that I'm talking about a code smell. This means that whenever you encounter the smell it should make you stop and think. I'm not saying that using a primitive type is <i>always</i> wrong.<br> <br> With a quantity, and given the constraints of C#, unfortunately there isn't a lot we can do. While we could encapsulate it in a PositiveInteger struct, it wouldn't add much value from a feedback perspective since we can't make the compiler choke on a negative value.<br> <br> However, a hypothetical PositiveInteger struct would still add the value that it encapsulates the invariant in one place instead of spreading it out all over the code base.<br> <br> Still, a positive integer is such a basic concept that it never changes. This means that even if you spread Guard Clauses that check for negative values all over your code base, you may not have much of a maintenance burden, since the Guard will never change. Still, you might forget it in a couple of places.<br> <br> In any case I like the Temperature example above much better because it not only provides safety, but also encapsulates the concept as well as provides conversion logic etc.</div> <div class="comment-date">2011-05-25 18:03 UTC</div> </div> <div class="comment" id="72bd5d4c0c0440caa87806c996c0337e"> <div class="comment-author">Thomas <a href="#72bd5d4c0c0440caa87806c996c0337e">#</a></div> <div class="comment-content">I try whenever possible to design that way my api especially in the domain layer. However I run into some corner cases with RESTful web services when it's more convenient for the client to provide a simple primitive than the complex object. Then on the server side the check is done to know if the provided value is correct. Would you bother to enforce it that case?</div> <div class="comment-date">2011-05-31 22:38 UTC</div> </div> <div class="comment" id="878eeb355aab4e67a9501b3a645cdb97"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#878eeb355aab4e67a9501b3a645cdb97">#</a></div> <div class="comment-content">Thomas, did you see <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">my post on application boundaries</a>? I think it ought to answer your question :)</div> <div class="comment-date">2011-06-01 05:45 UTC</div> </div> <div class="comment" id="c1219b786a164e56972e48152b44e8de"> <div class="comment-author">Thomas <a href="#c1219b786a164e56972e48152b44e8de">#</a></div> <div class="comment-content">Not yet:) I'm catching up with the old one first. But I go to it:)</div> <div class="comment-date">2011-06-01 06:29 UTC</div> </div> <div class="comment" id="3913a200c19c43ad9d4263e6fd0b3fcc"> <div class="comment-author">Scott Peterson <a href="#3913a200c19c43ad9d4263e6fd0b3fcc">#</a></div> <div class="comment-content">Hello Mark:<br><br> I am revisiting an old post in hopes you can shed some light on Value objects and their limits. I am currently trying to implement a Password class, which seems like a perfect example of something that should not be handled as a simple string. I modelled my Password object based on examples you've provided, in that I pass in a string to a constructor and then run an IsValid method to see if the string meets our business rules for passwords (length, types of characters, etc.). That's fine as it is, and I have unit tests to make sure all is well. But there are more business rules to a password. I need to have a privately set DateCreated field, and I need to store the number of days the password is valid, while providing a function to see if the password is still valid based on the DateCreated and the number of days the password is valid. However, adding these things to my value object seems like I'm polluting it. Plus, I want to pass in the number of days valid when the object is created, so now I have two parameters, which causes problems if I want to have an explicit operator. I thought about creating a PasswordPrimitive class and then a Password class that inherits the PasswordPrimitive class, but that seems messy.<br><br> If you have any thoughts and/or guidance, I'd appreciate the input.<br><br> Regards,<br> Scott </div> <div class="comment-date">2015-02-26 21:04 UTC</div> </div> <div class="comment" id="fccc65320c04479aacad73aeb9c2826b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fccc65320c04479aacad73aeb9c2826b">#</a></div> <div class="comment-content"> <p> Scott, thank you for writing. A <a href="http://martinfowler.com/bliki/ValueObject.html">Value Object</a> can contain more than a single primitive value. The canonical Value Object example is <em>Money</em>, and you often see Money examples where a Money value is composed of an amount and a currency; one example in the literature is Kent Beck's implementation in <a href="http://bit.ly/tddbe">Test Driven Development: By Example</a>, which contains amount and currency. </p> <p> Thus, I don't see any intrinsic problem with your password class containing both a string (or a byte array?) and an expiration time. </p> <p> It's true that you can no longer have a lossless conversion from your Value Object to a primitive value, but that's not a requirement for it to be a Value Object. </p> <p> (BTW, I hope you don't <em>store</em> passwords, but only the hashes!) </p> </div> <div class="comment-date">2015-02-27 7:29 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Design Smell: Temporal Coupling https://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling 2011-05-24T14:00:42+00:00 Mark Seemann <div id="post"> <p> This post is the first in a series about <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a> - also known as <em>encapsulation</em>. </p> <p> A common problem in API design is <em>temporal coupling</em>, which occurs when there's an implicit relationship between two, or more, members of a class requiring clients to invoke one member before the other. This tightly couples the members in the temporal dimension. </p> <p> The archetypical example is the use of an Initialize method, although copious other examples can be found - even in the BCL. As an example, this usage of <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.endpointaddressbuilder.aspx">EndpointAddressBuilder</a> compiles, but fails at run-time: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> b = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">EndpointAddressBuilder</font></span>();</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> e = b.ToEndpointAddress();</font></pre> </p> <p> It turns out that at least an URI is required before an EndpointAddress can be created. The following code compiles <em>and</em> succeeds at run-time: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> b = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">EndpointAddressBuilder</font></span>();</font> <font style="font-size: 10pt">b.Uri = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">UriBuilder</font></span>().Uri;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> e = b.ToEndpointAddress();</font></pre> </p> <p> The API provides no hint that this is necessary, but there's a temporal coupling between the Uri property and the ToEndpointAddress method. </p> <p> In the rest of the post I will provide a more complete example, as well as a guideline to improve the API towards Poka-yoke Design. </p> <h3 id="fd475f53898648e690577880685efa9b"> Smell Example <a href="#fd475f53898648e690577880685efa9b" title="permalink">#</a> </h3> <p> This example describes a more abstract code smell, exhibited by the Smell class. The public API looks like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Smell</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Initialize(<span style="color: "><font color="#0000ff">string</font></span> name)</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">string</font></span> Spread()</font> <font style="font-size: 10pt">}</font></pre> </p> <p> Semantically the name of the Initialize method is obviously a clue, but on a structural level this API gives us no indication of temporal coupling. Thus, code like this compiles, but throws an exception at run-time: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> s = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Smell</font></span>();</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> n = s.Spread();</font></pre> </p> <p> It turns out that the Spread method throws an InvalidOperationException because the Smell has not been initialized with a name. The problem with the Smell class is that it doesn't properly protect its invariants. In other words, encapsulation is broken. </p> <p> To fix the issue the Initialize method must be invoked before the Spread method: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> sut = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Smell</font></span>();</font> <font style="font-size: 10pt">sut.Initialize(<span style="color: "><font color="#a31515">"Sulphur"</font></span>);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> n = sut.Spread();</font></pre> </p> <p> While it's possible to write unit tests that explore the behavior of the Smell class, it would be better if the design was improved to enable the <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">compiler to provide feedback</a>. </p> <h3 id="e56680c97a874558bfa3d8b6d3714df3"> Improvement: Constructor Injection <a href="#e56680c97a874558bfa3d8b6d3714df3" title="permalink">#</a> </h3> <p> Encapsulation (Poka-yoke style) requires that the class can never be in an inconsistent state. Since the name of the smell is required, a guarantee that it is always available must be built into the class. If no good default value is available, the name must be requested via the constructor: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">Fragrance</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IFragrance</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#0000ff">string</font></span> name;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Fragrance(<span style="color: "><font color="#0000ff">string</font></span> name)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (name == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"name"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.name = name;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">string</font></span> Spread()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.name;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This effectively guarantees that the name is always available in all instances of the class. There&nbsp; are also positive side effects: </p> <ul> <li>The cyclomatic complexity of the class has been reduced</li> <li>The class is now immutable, and thereby thread-safe</li> </ul> <p> However, there are times when the original version of the class implements an interface that causes the temporal coupling. It might have looked like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">ISmell</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Initialize(<span style="color: "><font color="#0000ff">string</font></span> name);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">string</font></span> Spread();</font> <font style="font-size: 10pt">}</font></pre> </p> <p> In many cases the injected value (name) is unknown until run-time, in which case straight use of the constructor seems prohibitive - after all, <a href="/2011/02/28/Interfacesareaccessmodifiers">the constructor is an implementation detail and not part of the loosely coupled API</a>. When programming against an interface it's not possible to invoke the constructor. </p> <p> There's a solution for that as well. </p> <h3 id="f3d6dc140244400c9d52aa1bdc9e79ed"> Improvement: Abstract Factory <a href="#f3d6dc140244400c9d52aa1bdc9e79ed" title="permalink">#</a> </h3> <p> To decouple the methods in the ISmell (ha ha) interface the Initialize method can be moved to a new interface. Instead of mutating the (inconsistent) state of a class, the Create method (formerly known as Initialize) returns a new instance of the IFragrance interface: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IFragranceFactory</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IFragrance</font></span> Create(<span style="color: "><font color="#0000ff">string</font></span> name);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> The implementation is straightforward: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">FragranceFactory</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IFragranceFactory</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#2b91af">IFragrance</font></span> Create(<span style="color: "><font color="#0000ff">string</font></span> name)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (name == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"name"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fragrance</font></span>(name);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This enables encapsulation because both the FragranceFactory and Fragrance classes protect their invariants. They can never be in an inconsistent state. A client previously interacting with the ISmell interface can use the IFragranceFactory/IFragrance combination to achieve the same funcionality: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> f = factory.Create(name);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> n = f.Spread();</font></pre> </p> <p> This is better because improper use of the API can now be detected by the compiler instead of at run-time. An interesting side-effect by moving towards a more statically declared interaction structure is that classes tend towards immutability. Immutable classes are automatically thread-safe, which is an increasingly important trait in the (relatively) new multi-core era. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4af1728a956944ac88a2fda1dac88723"> <div class="comment-author">Scott <a href="#4af1728a956944ac88a2fda1dac88723">#</a></div> <div class="comment-content">Great insight again in to core principles that often get over looked even by big names in software development.<br> <br> Looking forward to the rest of the series.</div> <div class="comment-date">2011-05-24 16:02 UTC</div> </div> <div class="comment" id="ecf9610bda0a4706a48fb4e349fc9135"> <div class="comment-author">juan agui <a href="#ecf9610bda0a4706a48fb4e349fc9135">#</a></div> <div class="comment-content">Have you ever considered renaming your blog to programming 101? It's amazing how most my colleagues are still writing procedural code and violate each and every OOP principle. Fortunately there are blogs like yours that explain brilliantly and concisely these topics. Keep up the good job!<br> <br> <br> </div> <div class="comment-date">2011-05-24 17:28 UTC</div> </div> <div class="comment" id="487c0b99e27d48ae98128efd1c87afe5"> <div class="comment-author"><a href="http://www.codefluententities.com">Simon Mourier</a> <a href="#487c0b99e27d48ae98128efd1c87afe5">#</a></div> <div class="comment-content">Although, I agree with the fact that temporal coupling must be avoided, when possible, I find the solution you propose quite overkill (as always with that dependency injection jazz, IMHO). With the initial design, at least, as a developer, the problem I had were simple to solve (ie: add the Initialize call). Now with DI, as a user of the Small API, I may have to deal with all sort of runtime problems, just because I didn't &quot;read&quot; the API, or the documentation properly :-) Plus, now I have 4 types instead of 1 loaded in my domain. A simple design may not be the most elegant, but it bears simple problems.</div> <div class="comment-date">2011-05-25 05:50 UTC</div> </div> <div class="comment" id="44cac9709a98406193e28ac41493a373"> <div class="comment-author"><a href="Http://szalapski.com">Patrick Szalapski</a> <a href="#44cac9709a98406193e28ac41493a373">#</a></div> <div class="comment-content">So, suppose I want my FragranceFactory to return a Fragrance that implements ISmell (as you have it above) instead of an IFragrance--perhaps because I can't change ISmell). I should just have FragranceFactory return initialized Fragrances wherein the Initialize method does nothing, right? This would accomplish all our goals, although ISmell would still be confusing to the consumer.</div> <div class="comment-date">2011-05-25 17:27 UTC</div> </div> <div class="comment" id="7228199ce72e4a90b78eb9e7b434a642"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7228199ce72e4a90b78eb9e7b434a642">#</a></div> <div class="comment-content">Simon, exactly the opposite is true. With the Fragrance API you <i>wouldn't</i> have to deal with any runtime problems, because as soon as you can compile, the design of the code makes it very likely that it's also going to work at run-time (except for null values, which you can't escape no matter what you do).<br> <br> Getting feedback sooner is betting than getting feedback later. I much prefer getting a compiler error over a run-time exception.<br> <br> The number of types is hardly a measure of the quality of the code, but if it is, the more the better. The Single Responsibility Principle favors many small classes over a few God Classes.<br> <br> However, even if you don't agree, then comparing one type against four doesn't make sense. In the absence of interfaces there'd be one class in each alternative (Smell versus Fragrance). When you add interfaces into the mix, you have two types (Smell and ISmell) in the temporally coupled API against four types in the safe API.</div> <div class="comment-date">2011-05-25 18:19 UTC</div> </div> <div class="comment" id="7ad9a58c77fc41daab8809e25477c52a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7ad9a58c77fc41daab8809e25477c52a">#</a></div> <div class="comment-content">Patrick, yes, that might be a possible variation, although perhaps a bit exotic... In which scenario would that be the case?</div> <div class="comment-date">2011-05-25 18:21 UTC</div> </div> <div class="comment" id="6bb29256e30d4faa931bf727f76a725f"> <div class="comment-author"><a href="http://www.codefluententities.com/">Simon Mourier</a> <a href="#6bb29256e30d4faa931bf727f76a725f">#</a></div> <div class="comment-content">Not sure to follow you on the number of types. Initially, I had 1 Smell class, and in then end I have 2 classes and 2 interfaces. Or maybe I missed something.<br> In a real world project, multiply classes by 2, 3, 4 or more (lets add some WCF interfaces to this!) is indeed an issue, unless you generate some or all of it.<br> I'm not saying what you propose can't be done, or is inelegant. It is exactly the opposite, it's clever and smart, and is the style of code that can be taught in classes or blogged about, but the generalization of these principles can lead to bloated code and huge assemblies if applied massively.<br> <br> About maintenability, I pretend your code is harder to extend. Initially, I had one class that I could easily extend and the JIT helps me a lot here. I can create a new Spread(arguments) methods easily without breaking any of my existing clients (and without changing the semantics coupling). It's less easy with interfaces, do I need one new ISmellxx interfaces for every new method? New types again? Classes are extensible, interfaces are carved in stone forever - in fact, most people *do* change their interfaces, but that's another story :-)</div> <div class="comment-date">2011-05-26 07:08 UTC</div> </div> <div class="comment" id="4c4465858810490b8d79593c91b6de08"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4c4465858810490b8d79593c91b6de08">#</a></div> <div class="comment-content">This blog post discusses two scenarios. The first scenario is that you have only the Smell class. In that scenario, you can replace the Smell class with the Fragrance class (minus the IFragrance interface). No new types are necessary, so you start out with one type and end up with one type.<br> <br> The second scenarios is when you have a Smell class implementing an ISmell interface. In that case you'd need to go from two to four types.<br> <br> When it comes to maintainability, I find that the SOLID principles are some of the best guidelines around. The Open/Closed Principle explicitly states that classes should be closed for modification, but open for extensibility. In other words, we extend code <i>only</i> by adding new classes.</div> <div class="comment-date">2011-05-26 18:24 UTC</div> </div> <div class="comment" id="32900a6921df4044922f537688969d85"> <div class="comment-author">_ikke_ <a href="#32900a6921df4044922f537688969d85">#</a></div> <div class="comment-content">Just to be sure: <br> <br> The factory class is allowed to create concrete instances without using a DI container? And any dependencies that the concrete class needs will have to be passed to the factory class?<br> <br> </div> <div class="comment-date">2011-05-27 10:49 UTC</div> </div> <div class="comment" id="8996443adaa7444f8a92f4bd3f68c46b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8996443adaa7444f8a92f4bd3f68c46b">#</a></div> <div class="comment-content">Use of a DI Container is IMO orthogonal to the issue.<br> <br> <em>Conceptually</em> it's best to think about any such factories as concrete classes. If they need to pass dependencies to the objects they create, they can do so by requesting them by Constructor Injection. In other words you are correct that &quot;any dependencies that the concrete class needs will have to be passed to the factory class&quot; (excluding those that are passed as parameters to the Create method).<br> <br> On a practical side, you can always consider implementing the factory as an infrastructure component based on a container. Essentially, the factory would be an Adapter over the container. However, if you do this, you must be sure to place the Adapter in the Composition Root of the application, as otherwise you risk that the container reference leaks into your application code. This means that the concrete class and the factory will end up being implemented in two different assemblies. If you use Castle Windsor, you can go all the way and not even implement the container at all because instead you can leverage the Typed Factory facility.</div> <div class="comment-date">2011-05-28 21:52 UTC</div> </div> <div class="comment" id="81e3f09846ac48ccb50f98c9a6a57de9"> <div class="comment-author">Mohammed <a href="#81e3f09846ac48ccb50f98c9a6a57de9">#</a></div> <div class="comment-content">All of our code is full of temporal couplings. You can not prevent it because we solve our problems in doing first step1, followed by step2, and so on and so forth. So I don't think you can create code where there is no temporal coupling. What you can do on the other hand, and what I think this blog should have addressed, is to always create valid objects. The design of the class Smell makes it possible creating the object of type Smell without a name. Right after you issued the keyword new() your object is invalid. Any method you call after that will operate on an invalid object (Initialize(...) tries to remedy that). Your solution on the other hand is the right one. </div> <div class="comment-date">2011-06-17 13:07 UTC</div> </div> <div class="comment" id="850226cae8cf44cd8c098b94d499b40a"> <div class="comment-author">Daniel Hilgarth <a href="#850226cae8cf44cd8c098b94d499b40a">#</a></div> <div class="comment-content">Mark, thanks for this and the other articles in this and other series.<br> I have one question however:<br> FragranceFactory.Create checks name for null. Why?<br> I see the following problems with it:<br> 1) Code duplication:<br> As Fragrance can be instantiated without the use of the factory, the null check can't be removed from the constructor of Fragrance.<br> 2) Knowledge of implementation details of Fragrance:<br> The FragranceFactory knows that Fragrance can't work with a name that is null. This is not implicit, this is explicit knowledge. Another implementation of IFragrance could simply assign a default value if null is passed. As FragranceFactory is coupled tightly to Fragrancy, that is not such a big problem. Still I think it is not correct<br> 3) No benefit whatsoever:<br> Adding the null check in the Create method brings no benefit at all. On the contrary: If we were to change the constructor of Fragrance to allow null and to assign a default value, but would forget to change FragranceFactory.Create accordingly, the semantics of the object creation would differ, depending on how the object is created.<br> <br> Can you please explain what I have missed and why you decided to put the null check into the Create method?</div> <div class="comment-date">2011-07-21 12:34 UTC</div> </div> <div class="comment" id="c74efa3ee62342e0becb1c1608cc41c4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c74efa3ee62342e0becb1c1608cc41c4">#</a></div> <div class="comment-content">Daniel, thank you for your insightful comment - you are completely correct.<br> <br> I just added the Guard Clause out of habit, but your arguments are valid and I will in the future be more careful with my Guard Clauses. It only makes sense that one should only have a Guard Clause if one <em>uses</em> the argument in question. That's not the case when it's just being passed on, so it should not be there.<br> <br> Thanks for pointing that out.</div> <div class="comment-date">2011-07-21 15:03 UTC</div> </div> <div class="comment" id="16d91206587c4487b99f2806195d2aa4"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#16d91206587c4487b99f2806195d2aa4">#</a></div> <div class="comment-content">Mark, thanks for your answer. I hope I didn't sound like I was nitpicking. I simply was a bit puzzled about it.</div> <div class="comment-date">2011-07-21 15:25 UTC</div> </div> <div class="comment" id="0b0d27ce4afb4e30830fdb672af0318d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0b0d27ce4afb4e30830fdb672af0318d">#</a></div> <div class="comment-content">Not at all - I'm happy that you pointed out something I hadn't properly considered. I learned something from it :)</div> <div class="comment-date">2011-07-21 15:28 UTC</div> </div> <div class="comment" id="772c918aae8141b1aedaa6ba443833ef"> <div class="comment-author"><a href="http://ludovic.chabant.com">Ludovic Chabant</a> <a href="#772c918aae8141b1aedaa6ba443833ef">#</a></div> <div class="comment-content">Hey Mark,<br> What about fluent interfaces? They completely rely on temporal coupling by design:<br> <br> var query = new QueryBuilder().WithField(&quot;dressing&quot;).NotNull().Containing(&quot;bacon&quot;);<br> <br> Do you consider this an edge case, or do you actually dislike fluent interfaces in general?</div> <div class="comment-date">2011-08-02 17:50 UTC</div> </div> <div class="comment" id="dbf5bdd19bcf4245993372089b5a0c4e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dbf5bdd19bcf4245993372089b5a0c4e">#</a></div> <div class="comment-content">Not at all (to both statements). A Fluent Interface doesn't rely on Temporal Coupling at all - I frequently implement Fluent APIs with immutable types.<br> <br> See <a href="/2009/08/06/AFluentInterfaceForTestingINotifyPropertyChanged">this blog post</a> for a very simple example. For a very complex example, see <a href="http://autofixture.codeplex.com/">AutoFixture's</a> Ploeh.AutoFixture.Dsl namespace, which is kicked off by the fixture.Build&lt;T&gt;() method.<br> <br> In any case, Temporal Coupling doesn't disallow mutability - it just states that if you have mutating methods, the order in which they are invoked (if at all) mustn't matter.</div> <div class="comment-date">2011-08-02 18:23 UTC</div> </div> <div class="comment" id="3ca6d4b0d3a84e03a12e07a93dfb6e48"> <div class="comment-author">Emanuel Pasat <a href="#3ca6d4b0d3a84e03a12e07a93dfb6e48">#</a></div> <div class="comment-content">Offtopic question: striving for immutability on the domain model is a good practice (or only value objects should be immutable)?<br> </div> <div class="comment-date">2011-09-16 14:32 UTC</div> </div> <div class="comment" id="598397785364473ea8dc01afb02a2c77"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#598397785364473ea8dc01afb02a2c77">#</a></div> <div class="comment-content">The most <em>common</em> approach is certainly to model Entities as mutable objects while Value Objects are kept immutable.<br> <br> When you use 'classic' CRUD-style Repositories or Unit of Work, that may also be the best match to that style of architecture. However, my more recent experience with CQRS and Event Sourcing seems to <em>indicate</em> to me that an immutable domain model begins to make a lot more sense. However, I've still to gain more experience with this before I can say anything with confidence.<br> <br> It has been my general experience over the last couple of years that the more immutable objects I can define, the easier the code is to work with and reason about.</div> <div class="comment-date">2011-09-16 19:18 UTC</div> </div> <div class="comment" id="a68604214365481f81cc8f020dabe08a"> <div class="comment-author">Luis <a href="#a68604214365481f81cc8f020dabe08a">#</a></div> <div class="comment-content">Quick noob question, why the IFragranceFactory interface? Your code factory.Create(&quot;name&quot;) which i imagine would be preceded by &quot;var factory = new FragranceFactory()&quot; does not use the interface, does it?</div> <div class="comment-date">2012-09-11 14:12 UTC</div> </div> <div class="comment" id="4cba71d472cd4ad780c6e2d3f4d580da"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4cba71d472cd4ad780c6e2d3f4d580da">#</a></div> <div class="comment-content">An IFragranceFactory instance can be injected into any client that needs it, freeing it from having to create the tightly coupled instance itself.<br> <br> The IFragranceFactory instance can be created in the <a href="/2011/07/28/CompositionRoot">Composition Root</a>, potentially far removed from any clients. At this point it could be created with the 'new' keyword, or by a DI Container.</div> <div class="comment-date">2012-09-11 15:43 UTC</div> </div> <div class="comment" id="74ca0bf2baee4b61af7a2ebc3cd7277e"> <div class="comment-author">Luis <a href="#74ca0bf2baee4b61af7a2ebc3cd7277e">#</a></div> <div class="comment-content">i came to this blog post with the specific intention of reading about temporal coupling, but after reading your reply to my message and feeling utterly confused i decided to go and read your book, maybe after reading it i'll actually understand what you're saying, and gather some knowledge about DI in the process, obviously this DI is a thing</div> <div class="comment-date">2012-09-21 12:45 UTC</div> </div> <div class="comment" id="6d8e821ce4224388a34aa4f7b96e61f4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6d8e821ce4224388a34aa4f7b96e61f4">#</a></div> <div class="comment-content">Sorry, I didn't mean to confuse you...</div> <div class="comment-date">2012-09-21 13:15 UTC</div> </div> <div class="comment" id="a6b57a365a014bf6a925c243e47ca7d8"> <div class="comment-author">James Wheeler <a href="#a6b57a365a014bf6a925c243e47ca7d8">#</a></div> <div class="comment-content">Hi Mark, this is an old post now but it's still very relevant for me. What about the case when initialization is a time consuming process?<br><br> For example, at my company we work with a lot of devices and we often end up writing initialization methods for some Type, let's call it 'FooDevice,' that provides functionality for that kind of device.<br><br> Let's say our <code>FooDevice</code> implements an interface very similar to <code>ISmell</code>:<br> <pre> IFooDevice { Task Initialize(string comPort); int DoSomething(); } </pre> This particular initialization method accepts as input parameter the COM port on which the device is connected to and through which we send serial commands to control the device.<br><br> So the initialization method may then create a serial port object for that port, configure the device in various ways (such as disabling physical buttons, since we're now controlling it programatically) and so forth.<br><br> The initialization may be a time consuming process that can potentially take in excess of a few seconds or longer.<br><br> Following your advice, we should apply a refactoring here. The constructor for <code>FooDevice</code> should accept the COM port instead. Though that seems to imply that initialization should be done in the constructor:<br> <pre> class FooDevice : IFooDevice { public FooDevice(string comPort) { // Initialize(comPort); } private void Initialize(string comPort) { /* ... */ } } </pre> Yet that becomes problematic, because now we've lost the ability to return a <code>Task</code>. But of course if we have our <code>IFooDeviceFactory</code>, it can provide the Task:<br> <pre> IFooDeviceFactory { Task&lt;IFooDevice&gt; Create(string comPort); } </pre> Now, if I understand your example correctly, then the whole purpose of providing an Abstract Factory is to satisify the requirement that the client be in control of when the object is created. Or else the client could just directly accept an <code>IFragrance</code> and no factory would be needed. Another client could feasibily skip the factory and invoke the <code>Fragrance</code> constructor directly.<br><br> But if we allow the same behavior with my above example, we're back in the same awkward situation with the constructor.<br><br> What can we do then? Should we make the constructor private, and instead provide a static factory method like the following?<br> <pre> class FooDevice : IFooDevice { private FooDevice() { } public static Task&lt;FooDevice&gt; Create(string comPort) { var device = new FooDevice(); await device.Initialize(string comPort); return device; } private Task Initialize(string comPort) { /* ... */ } } </pre> </div> <div class="comment-date">2021-06-07 22:22 UTC</div> </div> <div class="comment" id="71d75d5aec364d019383f0a02e6cb545"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#71d75d5aec364d019383f0a02e6cb545">#</a></div> <div class="comment-content"> <p> James, thank you for writing. Based on what you describe, I find the last option, the one with a static factory method, preferable. </p> <p> You may want to consider calling it something like <code>Connect</code> instead of <code>Create</code>, but then again, perhaps this isn't much of an improvement. The most important information is in the return type. Since the method returns a task, it's a strong signal that this could take some time. </p> <p> What about closing the connection? Is that a concern? If so, you may want to make the object implement <code>IDisposable</code>. These days, various code analysers will tell you to dispose of such an object once you're done with it. </p> <p> You write: <blockquote> <p> "the whole purpose of providing an Abstract Factory is to satisify the requirement that the client be in control of when the object is created" </p> </blockquote> Not quite. The main purpose of such a design is <a href="/encapsulation-and-solid">encapsulation</a>, guaranteeing that an object can never be in an invalid state. </p> </div> <div class="comment-date">2021-06-09 12:42 UTC</div> </div> <div class="comment" id="567093a01013414bb8af5df3c75dea4d"> <div class="comment-author">James Wheeler <a href="#567093a01013414bb8af5df3c75dea4d">#</a></div> <div class="comment-content"> <p> Thanks for the reponse Mark. I too would consider calling the static factory method <code>Connect</code> but I stuck with <code>Create</code> for the sake of consistency with the example. </p> <p> I'm not sure I follow your comment about encapsulation. With your example, <code>FragranceFactory</code> merely delegates its work to the <code>Fragrance</code> constructor--the <code>Fragrance</code> class already achieves encapsulation by requiring <code>name</code> as a constructor argument. </p> <p> If you had instead introduced a <code>SmellFactory</code> that behaved like the following, then I would agree it encapsulates the required initialization protocol: </p> <pre> class SmellFactory : ISmellFactory { ISmell Create(string name) { var smell = new Smell(); smell.Initialize(name); return smell; } } </pre> <p> So the only usage I can see for keeping a factory around <i>after</i> improving the design with <code>IFragrance</code> is to allow the client to lazily create an instance of <code>Fragrance</code> without tightly coupling the client via the constructor. Otherwise, an already valid instance of IFragrance could be passed to the client. </p> <p> Indeed, you preface the introduction of the Abstract Factory improvement with, "In many cases the injected value (name) is unknown until run-time..." </p> <p> As for disposal, yes, we almost always need to implement IDisposable, since the supplier's managed API for the device usually implements IDisposable as well. </p> <p> However, wouldn't using an Abstract Factory like this be undesirable? IDevice would need to implement IDisposable so that the client could invoke Dispose on the device instance created by the factory. But as you and Steven point out in your book on dependency injection, an abstraction that implements IDisposable is a leaky abstraction. Or would that not apply in this case? </p> </div> <div class="comment-date">2021-06-14 19:25 UTC</div> </div> <div class="comment" id="c024c0091bc14a4cab90035735ba0ee9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c024c0091bc14a4cab90035735ba0ee9">#</a></div> <div class="comment-content"> <p> James, thank you for writing. There are two separate concerns here: <ul> <li>Guaranteeing that an object is in a valid state (encapsulation)</li> <li>Enabling polymorphism</li> </ul> What <em>valid state</em> means is specific to an implementation; i.e. a concrete class. Thus, if <code>FooDevice</code> can only be initialised like shown above, that's an implementation detail. </p> <p> The other concern is polymorphism. Your original definition of the <code>IFooDevice</code> interface had two methods: <code>Initialize</code> and <code>DoSomething</code>. In order to retain both of these capabilities, you're going to need to put both of these functions somewhere. In order to address the <a href="https://en.wikipedia.org/wiki/Sequential_coupling">sequential coupling</a> implied by that version of the interface, you'll have to distribute those methods over two different polymoprhic types: </p> <p> <pre>IFooDeviceFactory { Task&lt;IFooDevice&gt; Create(string comPort); } IFooDevice { int DoSomething(); }</pre> </p> <p> This prevents programmer error. With the original design, a client developer could call <code>DoSomething</code> without first calling <code>Initialize</code>. With the new design, this is no longer possible. You can't call <code>DoSomething</code> unless you have an <code>IFooDevice</code> instance, and you can't get an <code>IFooDevice</code> instance unless you call the <code>Create</code> method. </p> <p> Is the factory required? Not necessarily. That depends on where the responsibility to create the object lies. If client code is the only code in possession of the <code>comPort</code> value, then you'll need the polymorphic factory in order to intialise a polymorphic object. On the other hand, if the <code>comPort</code> value is available to the application infrastructure (e.g. via a configuration value or environment variable) then you may not the the abstract factory. In that case, any client code that needs an <code>IFooDevice</code> object can receive it via the constructor, and the application's <a href="/2011/07/28/CompositionRoot">Composition Root</a> can create the object using the concrete <code>Create</code> method. </p> <p> As to the observation about interfaces inheriting from <code>IDisposable</code>, I still consider that a leaky abstraction. There's usually a way to avoid it, but it tends to depend on the details of the application architecture. Thus, it's difficult to give general advice on that topic without knowing more about the actual circumstances. </p> </div> <div class="comment-date">2021-06-17 10:50 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Poka-yoke Design: From Smell to Fragrance https://blog.ploeh.dk/2011/05/24/Poka-yokeDesignFromSmelltoFragrance 2011-05-24T13:57:39+00:00 Mark Seemann <div id="post"> <p>Encapsulation is one of the most misunderstood aspects of object-oriented programming. Most people seem to think that the related concept of <em>information hiding</em> simply means that private fields should be exposed by public properties (or getter/setter methods in languages that don't have native properties).</p> <p>Have you ever wondered what's the <em>real</em> benefit to be derived from code like the following?</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> name;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">string</font></span> Name</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.name; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">set</font></span> { <span style="color: "><font color="#0000ff">this</font></span>.name = <span style="color: "><font color="#0000ff">value</font></span>; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>This feels awfully much like redundant code to me (and <a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx">automatic properties</a> are not the answer - it's just a compiler trick that still creates private backing fields). No information is actually hidden. <a href="http://lostechies.com/derickbailey/">Derick Bailey</a> has a <a href="http://lostechies.com/derickbailey/2011/03/28/encapsulation-youre-doing-it-wrong/">good piece on why this view of encapsulation is too narrow</a>, so I'm not going to reiterate all his points here.</p> <p>So then what <em>is</em> encapsulation?</p> <p>The whole point of object-orientation is to produce cohesive pieces of code (classes) that solve given problems once and for all, so that programmers can use those classes without having to learn about the intricate details of the implementations.</p> <blockquote> <p>This is what encapsulation is all about: exposing a solution to a problem without requiring the consumer to fully understand the problem domain.</p></blockquote> <p>This is what all well-designed classes do.</p> <ul> <li>You don't have to know the intricate details of <a href="http://en.wikipedia.org/wiki/Tabular_Data_Stream">TDS</a> to use ADO.NET against SQL Server. <li>You don't have to know the intricate details of painting on the screen to use WPF or Windows Forms. <li>You don't have to know the intricate details of Reflection to use a DI Container. <li>You don't have to know how to efficiently sort a list in order to efficiently sort a list in .NET. <li>Etc.</li></ul> <p>What makes encapsulation so important is exactly this trait. The class must hide the information it encapsulates in order to protect it against ‘naïve' users. <a href="http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29">Wikipedia has this to say</a>:</p> <blockquote> <p>Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state.</p></blockquote> <p>Keep in mind that users are <em>expected</em> to not fully understand the internal implementation of a class. This makes it obvious what encapsulation is really about:</p> <blockquote> <p>Encapsulation is a fail-safe mechanism.</p></blockquote> <p>By corollary, encapsulation does <em>not</em> mean hiding complexity. Whenever complexity is hidden (<a href="/2011/04/27/Providerisnotapattern">as is the case for Providers</a>) feedback time increases. Rapid feedback is much preferred, so delaying feedback is not desirable if it can be avoided.</p> <blockquote> <p>Encapsulation is not about hiding complexity, but conversely exposing complexity in a fail-safe manner.</p></blockquote> <p>In Lean this is known as <a href="http://en.wikipedia.org/wiki/Poka-yoke">Poka-yoke</a>, so I find it only fitting to think about encapsulation as <em>Poka-yoke Design</em>: APIs that make it as hard as possible to do the wrong thing. Considering that <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">compilation is the cheapest feedback mechanism</a>, it's preferable to design APIs so that the code can only compile when classes are used correctly.</p> <p>In a series of blog posts I will look at various design smells that break encapsulation, as well as provide guidance on how to improve the design to make it safer, thus going from smell to fragrance.</p> <ol> <li><a href="/2011/05/24/DesignSmellTemporalCoupling">Design Smell: Temporal Coupling</a> <li><a href="/2011/05/25/DesignSmellPrimitiveObsession">Design Smell: Primitive Obsession</a> <li><a href="/2011/05/26/CodeSmellAutomaticProperty">Code Smell: Automatic Property</a> <li><a href="/2011/05/27/DesignSmellRedundantRequiredAttribute">Design Smell: Redundant Required Attribute</a> <li><a href="/2011/05/30/DesignSmellDefaultConstructor">Design Smell: Default Constructor</a></li> <li><a href="/2014/06/02/captive-dependency">DI Container smell: Captive Dependency</a></li> </ol> <p>Postscript: <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">At the Boundaries, Applications are Not Object-Oriented</a></p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="85754442e736438fa17d75fdbd86efa4"> <div class="comment-author"><a href="http://aberrantcoder.com">Josh</a> <a href="#85754442e736438fa17d75fdbd86efa4">#</a></div> <div class="comment-content">Perhaps it would be beneficial to elaborate on your focus here by describing when/where these &quot;smells&quot; are applicable. With the .NET community (as a whole) only recently coming on to topics like anemic models, I fear that they will take this advice and apply it to every scenario.<br> <br> That's not to say that this is not relevant information but surely you're not implying that's applicable to scenarios like messaging, RESTful APIs, and other circumstances that need easily serializable objecst? </div> <div class="comment-date">2011-05-30 15:03 UTC</div> </div> <div class="comment" id="17da00f1cb1b4af2bfbee08dd2ca53c5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#17da00f1cb1b4af2bfbee08dd2ca53c5">#</a></div> <div class="comment-content">Good point. I wrote a <a href="/2011/05/31/AttheBoundaries,ApplicationsareNotObject-Oriented">new post elaborating on when encapsulation and object-orientation are in order</a>.</div> <div class="comment-date">2011-05-31 18:13 UTC</div> </div> <div class="comment" id="327db0afde7149e6b0c52200f30653df"> <div class="comment-author">Kurt Guntheroth <a href="#327db0afde7149e6b0c52200f30653df">#</a></div> <div class="comment-content">You're still doing it wrong. Encapsulation means that, instead of exposing data, you expose an interface that exports exactly the operations and values that are defined for your class.<br> <br> For instance, if a string name is used as a sorting key, it isn't appropriate to change the name. You can make the name string public, but that implies that changing the name is a valid operation, and might lead to bugs later. Providing a const getter, but no setter for the name says &quot;You can't change this name&quot;.<br> <br> If you have a setter and a getter for a piece of data, it should be because the class needs to expose that data for the purpose of changing it. That happens a lot, and it shouldn't be viewed as unreasonable that you have a private data member and a setter/getter. It's not a waste of time or code. It's a clear contract with users of your class that you intend to provide these operations no matter how the class evolves.<br> <br> One important property of good encapsulation is that you are free to change the data representation of your class if the interface remains the same, and your changes will be limited to the methods of the class itself. Want to change from a 1-based count to a zero-based index? If you exposed a member called items, you're screwed. If you exposed a method called CountGet() you're ok. Just change CountGet()'s implementation from returning items to return items+1. </div> <div class="comment-date">2011-06-01 16:22 UTC</div> </div> <div class="comment" id="3335ededb1034c0bad04a321f1f8e62c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3335ededb1034c0bad04a321f1f8e62c">#</a></div> <div class="comment-content">One thing at a time... There's a reason I themed this series of blog posts around fail-safing. I agree that encapsulation involves more than just that, but I don't think I ever said anything else.</div> <div class="comment-date">2011-06-01 18:52 UTC</div> </div> <div class="comment" id="a67fe9b8983e47239b72ad509bd7f02f"> <div class="comment-author">Florian Fordermaier <a href="#a67fe9b8983e47239b72ad509bd7f02f">#</a></div> <div class="comment-content">Interesting post. A few years ago I did some research on the &quot;absence of proper encapsulation language features&quot; in C# and Java. It's an interesting approach to advance from smells to fragrance, pointing out code snippets that potentially violate encapsulation in some way. But - to be honest - this will help only those people that are even aware of these problems. <br> You mentioned the compiler as the first and cheapest feedback mechanism, so the target should be to achieve automation of the process of enforcing proper encapsulation with (a) a better compiler or (b) static analysis or (c) runtime functionality that can be applied minimally-invasive. <br> See C++'s const qualifier, an excellent example of a language feature to support proper encapsulation, this would allow for e.g. auto-properties with a getter/setter when making the setter const. Of course this will itself impact your design, but it offers a language integrated fail-safe mechanism for encapsulation. What do you think?<br> I also may have an additional smell for you, a violation of the law of demeter breaks encapsulation in most cases. </div> <div class="comment-date">2011-06-16 08:12 UTC</div> </div> <div class="comment" id="4d938e3a33c24cd8a4cd523035f30e91"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4d938e3a33c24cd8a4cd523035f30e91">#</a></div> <div class="comment-content">Florian, I agree with your statement about Law of Demeter, although Fowler calls it 'the occasionally useful suggestion of Demeter' (or some such thing).<br> <br> I don't have any comment on the C++ const qualifier, as I have no idea what it does...</div> <div class="comment-date">2011-06-18 18:00 UTC</div> </div> <div class="comment" id="f77bc652804f42dc80e94c6e8b9eba3c"> <div class="comment-author">Florian Fordermaier <a href="#f77bc652804f42dc80e94c6e8b9eba3c">#</a></div> <div class="comment-content">Mark,<br> <br> first of all I have to correct my first comment, auto properties with a const setter is definitely non-sense. <br> The C++ const qualifier is effectively a statically checked and compiler-enforced construct to syntactically express your objectives regarding &quot;permissions&quot; to change an object's state. <br> If I e.g. declare a class A with a const method. Every caller calling the const method knows, that wahtever the method itself does, it will definitely not change the state of the object - imagine you have a immutable 'this' in your const method. <br> The same holds for e.g. a parameter that is passed to a method. If the parameter is declared const, the compiler will enforce that the parameter (be it a value or reference type) will not be changed.<br> But the real problem with every object that is owned by another object and exposed in some way(e.g. property getter), is, that when I return a reference to it, the caller that received the reference can change this object's state without me knowing it - this breaks encapsulation. The const qualifier comes to the rescue, when I return a const reference, the caller cannot change the returned object (compiler-checked!). <br> <br> Although the const qualifier does not solve all the problems you mentioned in your blog, it can be of help. I actually only brought your attention to this C++ language construct to have an example in hand (I'm far away from a C++ expert) for what I meant with &quot;automation of the process of enforcing proper encapsulation&quot;. I'm still interested in your opinion regarding efforts on automation of these things...<br> <br> <br> </div> <div class="comment-date">2011-06-21 11:30 UTC</div> </div> <div class="comment" id="6ae0139a23d34f0880326e4507d84766"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6ae0139a23d34f0880326e4507d84766">#</a></div> <div class="comment-content">Sounds useful. Currently, Microsoft is making an effort along the same lines with Code Contracts, but although it's actually a released technology it still seems a bit immature to me.<br> <br> As I've been using TDD since 2003 I usually just codify my assumptions about invariants in the tests I write. A framework like Greg Young's Grensesnitt might also come in handy there.</div> <div class="comment-date">2011-06-21 19:03 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tennis Kata with immutable types and a cyclomatic complexity of 1 https://blog.ploeh.dk/2011/05/16/TennisKatawithimmutabletypesandacyclomaticcomplexityof1 2011-05-16T11:01:00+00:00 Mark Seemann <div id="post"> <p align="left">Recently I had the inclination to do the <a href="http://codingdojo.org/cgi-bin/wiki.pl?KataTennis">Tennis Kata</a> a couple of times. The first time I saw it I thought it wasn't terribly interesting as an exercise in C# development. It would basically just be an application of the <a href="http://en.wikipedia.org/wiki/State_pattern">State</a> pattern, so I decided to make it a bit more interesting. More or less by intuition I decided to give myself the following constraints:</p> <ul> <li> <div align="left">All types must be immutable</div> </li><li> <div align="left">All members must have a <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of 1</div></li></ul> <p align="left">Now that's more interesting :)</p> <p align="left">Given these constraints, what would be the correct approach? Given that this is a finite state machine with a fixed number of states, the <a href="http://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a> pattern will be a good match.</p> <p align="left">Each player's score can be modeled as a Value Object that can be one of these types:</p> <ul> <li> <div align="left">ZeroPoints</div> </li><li> <div align="left">FifteenPoints</div> </li><li> <div align="left">ThirtyPoints</div> </li><li> <div align="left">FortyPoints</div> </li><li> <div align="left">AdvantagePoint</div> </li><li> <div align="left">GamePoint</div></li></ul> <p align="left">All of these classes implement the IPoints interface:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">IPoints</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IPoints</font></span> Accept(<span style="color: "><font color="#2b91af">IPoints</font></span> visitor);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IPoints</font></span> LoseBall();</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IPoints</font></span> WinBall(<span style="color: "><font color="#2b91af">IPoints</font></span> opponentPoints);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IPoints</font></span> WinBall(<span style="color: "><font color="#2b91af">AdvantagePoint</font></span> opponentPoints);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IPoints</font></span> WinBall(<span style="color: "><font color="#2b91af">FortyPoints</font></span> opponentPoints);</font> <font style="font-size: 10pt">}</font></pre> </p> <p align="left">The interesting insight here is that until the opponent's score reaches FortyPoints nothing special happens. Those states can be effectively collapsed into the WinBall(IPoints) method. However, when the opponent either has FortyPoints or AdvantagePoint, special things happen, so IPoints has specialized methods for those cases. All implementations should use <a href="http://en.wikipedia.org/wiki/Double_dispatch">double dispatch</a> to invoke the correct overload of WinBall, so the Accept method must be implemented like this:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IPoints</font></span> Accept(<span style="color: "><font color="#2b91af">IPoints</font></span> visitor)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> visitor.WinBall(<span style="color: "><font color="#0000ff">this</font></span>);</font> <font style="font-size: 10pt">}</font></pre> </p> <p align="left">That's the core of the Visitor pattern in action. When the implementer of the Accept method is either FortyPoints or AdvantagePoint, the specialized overload will be invoked.</p> <p align="left">It's now possible to create a context around a pair of IPoints (called a Game) to implement a method to register that Player 1 won a ball:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">Game</font></span> PlayerOneWinsBall()</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> newPlayerOnePoints = <span style="color: "><font color="#0000ff">this</font></span>.PlayerTwoScore</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Accept(<span style="color: "><font color="#0000ff">this</font></span>.PlayerOneScore);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> newPlayerTwoPoints = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.PlayerTwoScore.LoseBall();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Game</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newPlayerOnePoints, newPlayerTwoPoints);</font> <font style="font-size: 10pt">}</font></pre> </p> <p align="left">A similar method for player two simply reverses the roles. (I'm currently reading <a href="http://www.amazon.com/Lean-Architecture-Agile-Software-Development/dp/0470684208">Lean Architecture</a>, but have yet to reach the chapter on <a href="http://en.wikipedia.org/wiki/Data,_Context_and_Interaction">DCI</a>. However, considering what I've already read about DCI, this seems to fit the bill pretty well… although I might be wrong on that account.)</p> <p align="left">The context calculates new scores for both players and returns the result as a new instance of the Game class. This keeps the Game and IPoints implementations immutable.</p> <p>The new score for the winner depends on the opponent's score, so the appropriate overload of WinBall should be invoked. The Visitor implementation makes it possible to pick the right overload without resorting to casts and <em>if</em> statements. As an example, the FortyPoints class implements the three WinBall overloads like this:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IPoints</font></span> WinBall(<span style="color: "><font color="#2b91af">IPoints</font></span> opponentPoints)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">GamePoint</font></span>();</font> <font style="font-size: 10pt">}</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IPoints</font></span> WinBall(<span style="color: "><font color="#2b91af">FortyPoints</font></span> opponentPoints)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">AdvantagePoint</font></span>();</font> <font style="font-size: 10pt">}</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IPoints</font></span> WinBall(<span style="color: "><font color="#2b91af">AdvantagePoint</font></span> opponentPoints)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>;</font> <font style="font-size: 10pt">}</font></pre> </p> <p>It's also important to correctly implement the LoseBall method. In most cases, losing a ball doesn't change the current state of the loser, in which case the implementation looks like this:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IPoints</font></span> LoseBall()</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>;</font> <font style="font-size: 10pt">}</font></pre> </p> <p>However, when the player has advantage and loses the ball, he or she loses the advantage, so for the AdvantagePoint class the implementation looks like this:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">IPoints</font></span> LoseBall()</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">FortyPoints</font></span>();</font> <font style="font-size: 10pt">}</font></pre> </p> <p>To keep things simple I decided to implicitly model <em>deuce</em> as both players having FortyPoints, so there's not explicit Deuce class. Thus, AdvantagePoint returns FortyPoints when losing the ball.</p> <p>Using the Visitor pattern it's possible to keep the cyclomatic complexity at 1. The code has no branches or loops. It's immutable to boot, so a game might look like this:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">Fact</font></span>]</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">void</font></span> PlayerOneWinsAfterHardFight()</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> game = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Game</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerOneWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerOneWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerOneWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerTwoWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerTwoWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerTwoWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerTwoWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerOneWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerOneWinsBall()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .PlayerOneWinsBall();</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Assert</font></span>.Equal(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">GamePoint</font></span>(), game.PlayerOneScore);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Assert</font></span>.Equal(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">FortyPoints</font></span>(), game.PlayerTwoScore);</font> <font style="font-size: 10pt">}</font></pre> </p> <p>In case you'd like to take a closer look at the code I'm attaching it to this post. It was driven completely by using the <a href="/2010/10/08/AutoDataTheorieswithAutoFixture">AutoFixture.Xunit extension</a>, so if you are interested in idiomatic <a href="http://autofixture.codeplex.com/">AutoFixture</a> code it's also a good example of that.</p><a href="/content/binary/TennisKata.zip">TennisKata.zip (3.09 MB)</a> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="085764f383f9496abbf7366a1e8ecc13"> <div class="comment-author"><a href="http://twitter.com/BlackTigerX">Eber Irigoyen</a> <a href="#085764f383f9496abbf7366a1e8ecc13">#</a></div> <div class="comment-content">It's a beautiful thing</div> <div class="comment-date">2011-05-17 14:59 UTC</div> </div> <div class="comment" id="9c1578b180b44b72bc3a7aedc92ad4d7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9c1578b180b44b72bc3a7aedc92ad4d7">#</a></div> <div class="comment-content">Thanks :)</div> <div class="comment-date">2011-05-17 15:02 UTC</div> </div> <div class="comment" id="50905f0a9f0b4dcd8b93a6375ca8ec59"> <div class="comment-author">Klaus Hebsgaard <a href="#50905f0a9f0b4dcd8b93a6375ca8ec59">#</a></div> <div class="comment-content">Funny to see, I believe I created thi kata once upon a time ...</div> <div class="comment-date">2011-05-19 15:31 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Generic unit testing with xUnit.net https://blog.ploeh.dk/2011/05/09/GenericunittestingwithxUnit.net 2011-05-09T12:37:17+00:00 Mark Seemann <div id="post"> <p>Generics in .NET are wonderful, but sometimes when doing Test-Driven Development against a generic class I've felt frustrated because I've been feeling that dropping down to the lowest common denominator and testing against, say, Foo&lt;object&gt; doesn't properly capture the variability inherent in generics. On the other hand, writing the same test for five different types of T have seemed too wasteful (not to mention boring) to bother with.</p> <p>That's until it occurred to me that in xUnit.net (and possibly other unit testing frameworks) I can define a generic test class. As an example, I wanted to test-drive a class with this class definition:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">Interval</font></span>&lt;T&gt;</font></pre> </p> <p>Instead of writing a set of tests against Interval&lt;object&gt; I rather wanted to write a set of tests against a representative set of T. This is so easy to do that I don't know why I haven't thought of it before. I simply declared the test class itself as a generic class:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">abstract</font></span> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">IntervalFacts</font></span>&lt;T&gt;</font></pre> </p> <p>The reason I declared the class as abstract is because that effectively prevents the test runner from trying to run the test methods directly on the class. That would fail because T is still open. However, it enabled me to write tests like this:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">Theory</font></span>, <span style="color: "><font color="#2b91af">AutoCatalogData</font></span>]</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">void</font></span> MinimumIsCorrect(<span style="color: "><font color="#2b91af">IComparable</font></span>&lt;T&gt; first, </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IComparable</font></span>&lt;T&gt; second)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> sut = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Interval</font></span>&lt;T&gt;(first, second);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IComparable</font></span>&lt;T&gt; result = sut.Minimum;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Assert</font></span>.Equal(result, first);</font> <font style="font-size: 10pt">}</font></pre> </p> <p>In this test, I also use <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s <a href="/2010/10/08/AutoDataTheorieswithAutoFixture">xUnit.net extension</a>, but that's completely optional. You might as well just write an old-fashioned unit test, but then you'll need a <a href="/2009/02/13/SUTFactory">SUT Factory</a> that can resolve generics. If you don't use AutoFixture any other (auto-mocking) container will do, and if you don't use one of those, you can define a <a href="http://en.wikipedia.org/wiki/Factory_method_pattern">Factory Method</a> that creates appropriate instances of T (and, in this particular case, instances of IComparable&lt;T&gt;).</p> <p>In the above example I used a [Theory], but I might as well have been using a [Fact] as long as I had a container or a Factory Method to create the appropriate instances.</p> <p>The above test doesn't execute in itself because the owning class is abstract. I needed to declare an appropriate set of constructed types for which I wanted the test to run. To do that, I defined the following test classes:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">DecimalIntervalFacts</font></span> : <span style="color: "><font color="#2b91af">IntervalFacts</font></span>&lt;<span style="color: "><font color="#0000ff">decimal</font></span>&gt; { }</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">StringIntervalFacts</font></span> : <span style="color: "><font color="#2b91af">IntervalFacts</font></span>&lt;<span style="color: "><font color="#0000ff">string</font></span>&gt; { }</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">DateTimeIntervalFacts</font></span> : <span style="color: "><font color="#2b91af">IntervalFacts</font></span>&lt;<span style="color: "><font color="#2b91af">DateTime</font></span>&gt; { }</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">TimSpanIntervalFacts</font></span> : <span style="color: "><font color="#2b91af">IntervalFacts</font></span>&lt;<span style="color: "><font color="#2b91af">TimeSpan</font></span>&gt; { }</font></pre> </p> <p>The only thing these classes do is to each pick a particular type for T. However, since they are concrete classes with test methods, the test runner will pick up the test cases and execute them. This means that the single unit test I wrote above is now being executed four times - one for each constructed type.</p> <p>It's even possible to specialize the generic class and add more methods to a single specialized class. As a sanity check I wanted to write a set of tests specifically against Interval&lt;int&gt;, so I added this class as well:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">Int32IntervalFacts</font></span> : <span style="color: "><font color="#2b91af">IntervalFacts</font></span>&lt;<span style="color: "><font color="#0000ff">int</font></span>&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">Theory</font></span>]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">InlineData</font></span>(0, 0, 0, <span style="color: "><font color="#0000ff">true</font></span>)]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">InlineData</font></span>(-1, 1, -1, <span style="color: "><font color="#0000ff">true</font></span>)]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">InlineData</font></span>(-1, 1, 0, <span style="color: "><font color="#0000ff">true</font></span>)]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">InlineData</font></span>(-1, 1, 1, <span style="color: "><font color="#0000ff">true</font></span>)]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">InlineData</font></span>(-1, 1, -2, <span style="color: "><font color="#0000ff">false</font></span>)]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">InlineData</font></span>(-1, 1, 2, <span style="color: "><font color="#0000ff">false</font></span>)]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Int32ContainsReturnsCorrectResult(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">int</font></span> minimum, <span style="color: "><font color="#0000ff">int</font></span> maximum, <span style="color: "><font color="#0000ff">int</font></span> value,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">bool</font></span> expectedResult)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> sut = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Interval</font></span>&lt;<span style="color: "><font color="#0000ff">int</font></span>&gt;(minimum, maximum);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> result = sut.Contains(value);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Assert</font></span>.Equal(expectedResult, result);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#008000">// More tests...</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>When added as an extra class in addition to the four ‘empty' concrete classes above, it now causes each generic method to be executed five times, whereas the above unit test is only executed for the Int32IntervalFacts class (on the other hand it's a parameterized test, so the method is actually executed six times).</p> <p>It's also possible to write parameterized tests in the generic test class itself:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">Theory</font></span>]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(-1, -1, <span style="color: "><font color="#0000ff">false</font></span>)]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(-1, 0, <span style="color: "><font color="#0000ff">true</font></span>)]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(-1, 1, <span style="color: "><font color="#0000ff">true</font></span>)]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(0, -1, <span style="color: "><font color="#0000ff">false</font></span>)]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(0, 0, <span style="color: "><font color="#0000ff">true</font></span>)]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(0, 1, <span style="color: "><font color="#0000ff">true</font></span>)]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(1, -1, <span style="color: "><font color="#0000ff">false</font></span>)]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(1, 0, <span style="color: "><font color="#0000ff">false</font></span>)]</font> <font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">InlineData</font></span>(1, 1, <span style="color: "><font color="#0000ff">false</font></span>)]</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">void</font></span> ContainsReturnsCorrectResult(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">int</font></span> minumResult, <span style="color: "><font color="#0000ff">int</font></span> maximumResult,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">bool</font></span> expectedResult)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#008000">// Test method body</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>Since this parameterized test in itself has 9 variations and is declared by IntervalFacts&lt;T&gt; which now has 5 constructed implementers, this single test method will be executed 9 x 5 = 45 times!</p> <p>Not that the number of executed tests in itself is any measure of the quality of the test, but I do appreciate the ability to write generic unit tests against generic types.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 2.1 https://blog.ploeh.dk/2011/05/02/AutoFixture2.1 2011-05-02T18:34:52+00:00 Mark Seemann <div id="post"> <p>Since I announced <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.1 beta 1 no issues have been reported, so I've now promoted the release to the official 2.1 release. This means that if you already downloaded AutoFixture 2.1 beta 1, you already have the 2.1 binaries. If not, you can head over to the <a href="http://autofixture.codeplex.com/releases/view/64686">release page</a> to get it.</p> <p>The NuGet packages will soon be available.</p> <p><strong>Update</strong> (2011.5.4): The NuGet packages are now available.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Windows Azure migration smell: SQL Server over-utilization https://blog.ploeh.dk/2011/05/02/WindowsAzuremigrationsmellSQLServerover-utilization 2011-05-02T12:23:49+00:00 Mark Seemann <div id="post"> <p>Recently I partook in a Windows Azure migration workshop, helping developers from existing development organizations port their applications to Windows Azure. Once more an old design smell popped up: <em>SQL Server over-utilization</em>. This ought to be old news to anyone with experience designing software on the Wintel stack, but apparently it bears repetition:</p> <blockquote> <p>Don't put logic in your database. SQL Server should be used only for persistent storage of data.</p></blockquote> <p>(Yes: this post <em>is</em> written in 2011…)</p> <p>Many years ago I heard that role described as a ‘bit bucket' - you put in data and pull it out again, and that's all you do. No fancy stored procedures or functions or triggers.</p> <p>Why wouldn't we want to use the database if we have one? Scalability is the answer. SQL Server doesn't scale horizontally. You can't add more servers to take the load off a database server (well, some of my old colleagues will argue that this is possible with Oracle, and that may be true, but with SQL Server it's impossible).</p> <p>Yes, we can jump through hoops like partitioning and splitting the database up into several smaller databases, but it still doesn't give us horizontal scalability. SQL Server is a bottleneck in any system in which it takes part.</p> <p>How is this relevant to Windows Azure? It's relevant for two important reasons:</p> <ul> <li>There's an upper size limit on SQL Azure. Currently that size limit is 50 GB, and while it's likely to grow in the future, there's going to be a ceiling for a long time. <li>You can't fine tune the hardware for performance. The server runs on virtual hardware.</li></ul> <p>Development organizations that rely heavily on the database for execution of logic often need expensive hardware and experienced DBAs to squeeze extra performance out of the database servers. Such people know that write-intensive/append-only tables work best with one type of RAID, while read-intensive tables are better hosted on other file groups on different disks with different RAID configurations.</p> <p>With SQL Azure you can just forget about all that.</p> <p>The bottom line is that there are fundamental rules for software development that you must follow if you want to be able to successfully migrate to Windows Azure. I previously described an <a href="/2010/10/14/WindowsAzureMigrationSanityCheck">even simpler sanity check you should perform</a>, but after that you should take a good look at your database.</p> <p>The best solution is if you can completely replace SQL Server with Azure's very scalable storage services, but those <a href="/2011/01/24/Scalabledoesntmeanfast">come with their own set of challenges</a>.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Feedback mechanisms and tradeoffs https://blog.ploeh.dk/2011/04/29/Feedbackmechanismsandtradeoffs 2011-04-29T13:02:14+00:00 Mark Seemann <div id="post"> <p> <em>Rapid feedback</em> is one of the cornerstones of agile development. The faster we can get feedback, the less it costs to correct errors. Unit testing is one of the ways to get feedback, but not the only one. Each way we can get feedback comes with its own advantages and disadvantages. </p> <p> In my experience there's a tradeoff between the cost of setting up a feedback mechanism and the level of confidence we can get from it. This is quite intuitive to most people, but I've rarely seen it explicitly described. The purpose of this post is to explicitly describe and relate those different mechanisms. </p> <h3 id="dcce16b851974ef7b7abd2be9c4214e2"> Compilation <a href="#dcce16b851974ef7b7abd2be9c4214e2" title="permalink">#</a> </h3> <p> In compiled languages, compilation provides the first level of feedback. I think a lot of people don't think about this as feedback as (for compiled languages) it's a necessary step before the code can be executed. </p> <p> The level of confidence may not be very high - we tend to laugh at statements like ‘if it compiles, it works'. However, the compiler still catches a lot of mistakes. Think about it: does your code always compile, or do you sometimes get compilation errors? Do you sometimes try to compile your code just to see if it's possible? I certainly do, and that's because the compiler is always available, and it's the first and fastest level of feedback we get. </p> <p> Code can be designed to take advantage of this verification step. Constructor Injection, for example, ensures that we can't create a new instance of a class without supplying it with its <em>required</em> dependencies. </p> <p> It's also interesting to note that anecdotal evidence seems to suggest that unit testing is more prevalent for interpreted languages. That makes a lot of sense, because as developers we need feedback as soon as possible, and if there's no compiler we must reach for the next level of feedback mechanism. </p> <h3 id="0a8a0827f14d430bb802e09085af967f"> Static code analysis <a href="#0a8a0827f14d430bb802e09085af967f" title="permalink">#</a> </h3> <p> The idea of static code analysis isn't specific to .NET, but in certain versions of Visual Studio we have Code Analysis (which is also available as FxCop). Static code analysis is a step up from compilation - not only is code checked for syntactical correctness, but it's also checked against a set of known anti-patterns and idioms. </p> <p> Static code analysis is encapsulated expert knowledge, yet surprisingly few people use it. I think part of the reason for this is because the ratio of time against confidence isn't as compelling as with other feedback mechanism. It takes some time to run the analysis, but like compilation the level of confidence gained from getting a clean result is not very high. </p> <p> Personally, I use static code analysis once in a while (e.g. before checking in code to the default branch), but not as often as other feedback mechanisms. Still, I think this type of feedback mechanism is under-utilized. </p> <h3 id="3f34b7661d584febade7dc802d78decf"> Unit testing <a href="#3f34b7661d584febade7dc802d78decf" title="permalink">#</a> </h3> <p> Running a unit test suite is one of the most efficient ways to gain rapid feedback. The execution time is often comparable to running static code analysis while the level of confidence is much higher. </p> <p> Obviously a successful unit test execution is no guarantee that the final application works as intended, but it's a much better indicator than the two previous mechanisms. When presented with the choice of using either static code analysis or unit tests, I prefer unit tests every time because the confidence level is much higher. </p> <p> If you have sufficiently high code coverage I'd say that a successful unit test suite is enough confidence to check in code and let continuous integration take care of the rest. </p> <blockquote> <p> Keep in mind that continuous integration and other types of automated builds are feedback mechanisms. If you institute rules where people are ‘punished' for checking in code that breaks the build, you effectively disable the automated build as a source of feedback. </p> </blockquote> <p> Thus, the rest of these feedback mechanisms should be used rarely (if at all) on developer work stations. </p> <h3 id="d0d460f264ef41e0b01a4981675c0386"> Integration testing <a href="#d0d460f264ef41e0b01a4981675c0386" title="permalink">#</a> </h3> <p> Integration testing comes in several sub-categories. You can integrate multiple classes from within the same library, or integrate multiple libraries while still using <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a> instead of out-of-process resources. At this level, the cost/confidence ratio is comparable to unit testing. </p> <h3 id="90f460b4f36a4a87b1cf2bfe18adc15c"> Subcutaneous testing <a href="#90f460b4f36a4a87b1cf2bfe18adc15c" title="permalink">#</a> </h3> <p> A more full level of integration testing comes when all resources are integrated, but the tests are still driven by code instead of through the UI. While this gives a degree of confidence that is close to what you can get from a full systems test, the cost of setting up the entire testing environment is <em>much</em> higher. In many cases I consider this the realm of a dedicated testing department, as it's often a full-time job to maintain the suite of automation tools that enable such tests - particularly if we are talking about distributed systems. </p> <h3 id="8906e9d008234b9bab176b40fd76ede6"> System testing <a href="#8906e9d008234b9bab176b40fd76ede6" title="permalink">#</a> </h3> <p> This is a test of the full system, often driven through the UI and supplemented with manual testing (such as exploratory testing). This provides the highest degree of confidence, but comes with a very high cost. It's often at this level we find formal acceptance tests. </p> <p> The time before we can get feedback at this level is often considerable. It may take days or even weeks or months. </p> <h3 id="1d76dfee6bdc4433b0fe6e7ce24ed7a8"> Understand the cost/confidence ratio <a href="#1d76dfee6bdc4433b0fe6e7ce24ed7a8" title="permalink">#</a> </h3> <p> The purpose of this post has been to talk about different ways we can get feedback when developing software. If we could get <em>instantaneous</em> feedback with <em>guaranteed</em> confidence it would be easy to choose, but as this is not the case we must understand the benefits and drawbacks of each mechanism. </p> <p> It's important to realize that there are many mechanisms, and that we can combine them in a staggered approach where we start with fast feedback (like the compiler) with a low degree of confidence and then move through successive stages that each provide a higher level of confidence. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="355037f6af7644a58dc88cb87d785d03"> <div class="comment-author">Markus Hj&#228;rne <a href="#355037f6af7644a58dc88cb87d785d03">#</a></div> <div class="comment-content">Hi Mark, good post. I just thought of one thing worth mentioning in this context: guarding public APIs with checks that throws exceptions in case they fail and guarding private methods with asserts has been a great source of good and fast feedback for me over the years.<br> <br> Regards,<br> Markus</div> <div class="comment-date">2011-05-04 20:46 UTC</div> </div> <div class="comment" id="6dc4e5d1bf2a40dd8ed59805b88a2af8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6dc4e5d1bf2a40dd8ed59805b88a2af8">#</a></div> <div class="comment-content">I totally agree, and I was (and am still) planning on writing something about that in a later post. This is essentially the Fail Fast principle, but as for feedback mechanisms, we don't discover these Guard Clauses before we invoke them.<br> <br> My strong preference is for writing unit tests that invoke these Guard Clauses to verify that they work, and that means that as far as feedback mechanisms go, they fall into the unit testing 'bucket'.</div> <div class="comment-date">2011-05-04 20:54 UTC</div> </div> <div class="comment" id="bae47385fd724c11b71442f3eeb7e28a"> <div class="comment-author"><a href="http://gogllundain.blogspot.co.uk/">GogLlundain</a> <a href="#bae47385fd724c11b71442f3eeb7e28a">#</a></div> <div class="comment-content">Your comments about compilation are interesting, I hadn't thought of it before but it is a level of feedback that I find as useful as you do.<br> <br> I wonder if you'd consider IntelliSense as an even earlier form of feedback though?<br> <br> Gog</div> <div class="comment-date">2012-06-25 09:20 UTC</div> </div> <div class="comment" id="8fa137df8ef842d98fc2ccf4c09b6e3a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8fa137df8ef842d98fc2ccf4c09b6e3a">#</a></div> <div class="comment-content">IntelliSense is, I think, more of a learning tool. It's not really <em>feedback</em> about what you <em>did</em> - it's more <em>information</em> about what you <em>can do</em>. It's certainly a productivity feature, although there are some people who argue that it does more harm than good.</div> <div class="comment-date">2012-06-25 10:50 UTC</div> </div> <div class="comment" id="3c270099afdf4c4386c4fc5af6b2c33a"> <div class="comment-author"><a href="https://codevergnugen.wordpress.com/">David Falkner</a> <a href="#3c270099afdf4c4386c4fc5af6b2c33a">#</a></div> <div class="comment-content">It might be worth considering a couple of additional feedback mechanisms at the opposite end of the spectrum, in the sense that they provide even later feedback, if only to underscore your points from the article with extreme counter-examples. <em>Run-time exceptions</em> are themselves a feedback mechanism. Most unnecessarily "stringly-typed" code could be considered to be pushing code away from the more rapid, desirable feedback mechanisms. (Primitive Obsession in general might be considered another tendency which pushes the design of code toward being run-time exception-prone.) <em>Logic erors</em> are flaws in code with which the code appears to execute successfully, until the program output is manually inspected. Just off the cuff, one typical cause might be the tendency in some projects to add guard clauses such as <em>if (methodArg==null) return;</em> which causes methods to silently and unexpectedly return without accomplishing the task that they were designed to do. Mathematical calculations performed without adequate test coverage might be another example.<br><br>We learned about 3 classes of errors in school: 1) Syntax, 2) Run-time, and 3) Logic (Unit testing and static code analysis weren't as much of a thing back then). It might seem odd to discuss types of feedback mechanisms that are undesirable by definition, until you stop to observe how common it is for the design of much code to push errors <em>towards</em> this undesirable side of the continuum. "To defeat your enemy you first must know him" or something like that.</div> <div class="comment-date">2016-09-11 00:11 UTC</div> </div> <div class="comment" id="556888cbfd2141569378d1b90abaa77e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#556888cbfd2141569378d1b90abaa77e">#</a></div> <div class="comment-content"> <p> David, thank you for making these feedback cases explicit. When I originally wrote this article, I implicitly had such alternative, common-place feedback mechanisms in mind, but I never explicitly spelled them out. While I should have done that, I can now thank you for doing it; your comment is a good addition to the original article. </p> </div> <div class="comment-date">2016-09-11 08:07 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Provider is not a pattern https://blog.ploeh.dk/2011/04/27/Providerisnotapattern 2011-04-27T12:14:52+00:00 Mark Seemann <div id="post"> <p> Developers exposed to ASP.NET are likely to be familiar with the so-called <a href="http://msdn.microsoft.com/en-us/library/ms972319.aspx">Provider pattern</a>. You see it a lot in that part of the BCL: <a href="http://msdn.microsoft.com/en-us/library/6b241xwt.aspx">Role Provider</a>, <a href="http://msdn.microsoft.com/en-us/library/sx3h274z.aspx">Membership Provider</a>, <a href="http://msdn.microsoft.com/en-us/library/014bec1k.aspx">Profile Provider</a>, etc. Lots of text has already been written about Providers, but the reason I want to add yet another blog post on the topic is because once in a while I get the question on how it relates to Dependency Injection (DI). </p> <p> Is Provider a proper way to do DI? </p> <p> No, it has nothing to do with DI, but as it tries to mimic loose coupling I can understand the confusion. </p> <p> First things first. Let's start with the name. Is it a pattern at all? Regular readers of this blog may get the impression that I'm fond of calling everything and the kitchen sink an anti-pattern. That's not true because I <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">only make that claim when I'm certain I can hold that position</a>, so I'm not going to denounce Provider as an anti-pattern. On the contrary I will make the claim that Provider is not a pattern at all. </p> <p> A design pattern is not <em>invented</em> - it's <em>discovered</em> as a repeated solution to a commonly recurring problem. Providers, on the other hand, were invented by Microsoft, and I've rarely seen them used outside their original scope. Secondly I'd also dispute that they solve anything. </p> <p> That aside, however, I want to explain why Provider is bad design: </p> <ul> <li>It uses the Constrained Construction anti-pattern</li> <li>It hides complexity</li> <li>It prevents proper lifetime management</li> <li>It's not testable</li> </ul> <p> In the rest of this post I will explain each point in detail, but before I do that we need an example to look at. The <a href="/2010/02/02/RefactoringtoAggregateServices">old OrderProcessor example</a> suffices, but instead of injecting IOrderValidator, IOrderCollector, and IOrderShipper this variation uses Providers to provide instances of the Services: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#2b91af">SuccessResult</font></span> Process(<span style="color: "><font color="#2b91af">Order</font></span> order)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IOrderValidator</font></span> validator = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">ValidatorProvider</font></span>.Validator;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">bool</font></span> isValid = validator.Validate(order);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (isValid)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">CollectorProvider</font></span>.Collector.Collect(order);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">ShipperProvider</font></span>.Shipper.Ship(order);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.CreateStatus(isValid);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> The ValidatorProvider uses the configuration system to create and return an instance of IOrderValidator: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">static</font></span> <span style="color: "><font color="#2b91af">IOrderValidator</font></span> Validator</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> section = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">OrderValidationConfigurationSection</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .GetSection();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> typeName = section.ValidatorTypeName;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> type = <span style="color: "><font color="#2b91af">Type</font></span>.GetType(typeName, <span style="color: "><font color="#0000ff">true</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> obj = <span style="color: "><font color="#2b91af">Activator</font></span>.CreateInstance(type);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">return</font></span> (<span style="color: "><font color="#2b91af">IOrderValidator</font></span>)obj;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> There are lots of details I omitted here. I could have saved the reference for later use instead of creating a new instance each time the property is accessed. In that case I would also have had to make the code thread-safe, so I decided to skip that complexity. The code could also be more defensive, but I'm sure you get the picture. </p> <p> The type name is defined in the app.config file like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&lt;</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#a31515">orderValidation</font></span></font><span style="color: "><font style="font-size: 10pt" color="#0000ff"> </font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&nbsp; </font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#ff0000">type</font></span><span style="color: "><font color="#0000ff">=</font></span>"</font><span style="color: "><font style="font-size: 10pt" color="#0000ff">Ploeh.Samples.OrderModel.UnitTest.TrueOrderValidator,</font></span> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ploeh.Samples.OrderModel.UnitTest</font></font></span><font style="font-size: 10pt">"</font><span style="color: "><font style="font-size: 10pt" color="#0000ff"> /&gt;</font></span></pre> </p> <p> Obviously, CollectorProvider and ShipperProvider follow the same… blueprint. </p> <p> This should be well-known to most .NET developers, so what's wrong with this model? </p> <h3 id="4c7b89305e744b22b9cadb8fd4f1d74e"> Constrained Construction <a href="#4c7b89305e744b22b9cadb8fd4f1d74e" title="permalink">#</a> </h3> <p> In <a href="http://amzn.to/12p90MG">my book</a>'s chapter on DI anti-patterns I describe the <em>Constrained Construction</em> anti-pattern. Basically it occurs every time there's an implicit constraint on the constructor of an implementer. In the case of Providers the constraint is that each implementer must have a default constructor. In the example the culprit is this line of code: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> obj = <span style="color: "><font color="#2b91af">Activator</font></span>.CreateInstance(type);</font></pre> </p> <p> This constrains any implementation of IOrderValidator to have a default constructor, which obviously means that the most fundamental DI pattern Constructor Injection is out of the question. </p> <p> Variations of the Provider idiom is to supply an Initialize method with a context, but this creates a temporal coupling while still not enabling us to inject arbitrary Services into our implementations. I'm not going to repeat six pages of detailed description of Constrained Construction here, but the bottom line is that you can't fix it - you have to refactor towards true DI - preferably Constructor Injection. </p> <h3 id="4542f0752d1c4cc2a9c4c58be4aa779a"> Hidden complexity <a href="#4542f0752d1c4cc2a9c4c58be4aa779a" title="permalink">#</a> </h3> <p> Providers hide the complexity of their implementations. This is not the same as <em>encapsulation</em>. Rather it's a dishonest API and the problem is that it just postpones the moment when you discover how complex the implementation really is. </p> <p> When you implement a client and use code like the following everything looks <em>deceptively</em> simple: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#2b91af"><font style="font-size: 10pt">IOrderValidator</font></font></span><font style="font-size: 10pt"> validator = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">ValidatorProvider</font></span>.Validator;</font></pre> </p> <p> However, if this is the only line of code you write it will fail, but you will not notice until run-time. Check back to the implementation of the Validator property if you need to refresh the implementation: there's a lot of things that can go wrong here: </p> <ul> <li>The appropriate configuration section is not available in the app.config file.</li> <li>The ValidatorTypeName is not provided, or is null, or is malformed.</li> <li>The ValidatorTypeName is correctly formed, but the type in question cannot be located by Fusion.</li> <li>The Type doesn't have a default constructor. This is one of the other problems of Constrained Construction: it can't be statically enforced because a constructor is not part of an <a href="/2011/02/28/Interfacesareaccessmodifiers">abstraction's API</a>.</li> <li>The created type doesn't implement IOrderValidator.</li> </ul> <p> I'm sure I even forgot a thing or two, but the above list is sufficient for me. None of these problems are caught by the compiler, so you don't discover these issues until you run an integration test. So much for rapid feedback. </p> <p> I don't like APIs that lie about their complexity. </p> <blockquote> <p> Hiding complexity does not make an API easier to use; it makes it harder. </p> </blockquote> <p> An API that hides necessary complexity makes it impossible to discover problems at compile time. It simply creates more friction. </p> <h3 id="f98fc4030f5a4ba995ac8d61b016fdf4"> Lifetime management issues <a href="#f98fc4030f5a4ba995ac8d61b016fdf4" title="permalink">#</a> </h3> <p> A Provider exerts too much control over the instances it creates. This is a variation of the <em>Control Freak</em> anti-pattern (also from my book). In the current implementation the Validator property totally violates the <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment">Principle of least surprise</a> since it returns a new instance every time you invoke the getter. I did this to keep the implementation simple (this is, after all, example code), but a more normal implementation would reuse the same instance every time. </p> <p> However, reusing the same instance every time may be problematic in a multi-threaded context (such as a web application) because you'll need to make sure that the implementation is thread-safe. Often, we'd much prefer to scope the lifetime of the Service to each HTTP request. </p> <p> HTTP request scoping <em>can</em> be built into the Provider, but then it would <em>only</em> work in web applications. That's not very flexible. </p> <p> What's even more problematic is that once we move away from the Singleton lifestyle (not to be confused with the Singleton design pattern) we may have a memory leak at hand, since the implementation may implement IDisposable. This can be solved by adding a Release method to each Provider, but now we are moving so far into DI Container territory that I find it far more reasonable to just use proper DI instead of trying to reinvent the wheel. </p> <p> Furthermore, the fact that each Provider owns the lifetime of the Service it controls makes it impossible to share resources. What if the implementation we want to use implements several <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a> each served up by a different Provider? We might want to use that common implementation to share or coordinate state across different Services, but that's not possible because we can't share an instance across multiple providers. </p> <p> Even if we configure all Providers with the same concrete class, each will instantiate and serve its own separate instance. </p> <h3 id="867767085f5646abb7ecf9921624ef25"> Testability <a href="#867767085f5646abb7ecf9921624ef25" title="permalink">#</a> </h3> <p> The Control Freak also impacts testability. Since a Provider creates instances of interfaces based on XML configuration and Activator.CreateInstance, there's no way to inject a dynamic mock. </p> <p> It <em>is</em> possible to use hard-coded <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a> such as <a href="http://xunitpatterns.com/Test%20Stub.html">Stubs</a> or <a href="http://xunitpatterns.com/Fake%20Object.html">Fakes</a> because we can configure the XML with their type names, but even a <a href="http://xunitpatterns.com/Test%20Spy.html">Spy</a> is problematic because we'll rarely have an object reference to the Test Double. </p> <p> In short, the Provider idiom is not a good approach to loose coupling. Although Microsoft uses it in some of their products, it only leads to problems, so there's no reason to mimic it. Instead, use Constructor Injection to create loosely coupled components and wire them in the application's <em>Composition Root</em> using the <a href="/2010/09/29/TheRegisterResolveReleasepattern">Register Resolve Release</a> pattern. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4536b696ef454ef7af0a526262f19def"> <div class="comment-author"><a href="http://mcdaniel.ws">Michael McDaniel</a> <a href="#4536b696ef454ef7af0a526262f19def">#</a></div> <div class="comment-content">I have to disagree with your statement that the provider is not a pattern at all. The provider (in .NET) is a specific implementation of the bridge pattern as defined in &quot;Design Patterns&quot; by Gamma, Helm, Johnson, and Vlissides. I believe that this description has been lost over the past 10 years as people in the industry have resorted to calling it the &quot;Provider Pattern.&quot; <br> <br> When used correctly as a bridge pattern, the provider does actually solve reoccuring problems very eloquently. It decouples the interface from the implementation so that the implementation details are hidden from the client. You can extend the implementation by building on existing implementations to reduce the complexity. Complexity is only introduced as a by-product of bad design. <br> <br> The notion that the provider interfers with testability is incorrect. Each individual implementation should be designed from the beginning to be testable. The bridge is not the entry point for testing the implementor. The bridge should only be responsible for forwarding client requests to its implementor. (Therefore the bridge should be testable as well.) I am not stating that there aren't providers out there that violate this priciple. I'm merely stating that providers which do this are poorly designed.</div> <div class="comment-date">2011-06-27 16:43 UTC</div> </div> <div class="comment" id="6c40ac58a0cf438a875b529eab24377c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6c40ac58a0cf438a875b529eab24377c">#</a></div> <div class="comment-content">I just reread the Bridge pattern and while I agree that Provider is a specialization of Bridge, I don't agree that this relationship goes the other way. If you read the description of Provider provided in the link above, you'll notice that it goes into very specific details on how a Provider should be implemented, including how it should be backed by the configuration system and that it must be created by clients by a static factory.<br> <br> This goes way beyond what the Bridge pattern describes, so I hardly think you can equate the two.<br> <br> Especially the part about being created by a static factory which can only read from the configuration system is testability poison.</div> <div class="comment-date">2011-06-27 19:25 UTC</div> </div> <div class="comment" id="2763bef05cdb4710812cd03521b702b3"> <div class="comment-author"><a href="http://candordeveloper.com/">Michael Lang</a> <a href="#2763bef05cdb4710812cd03521b702b3">#</a></div> <div class="comment-content">I disagree with your analysis. Provider model is a pattern.<br> <br> I realize this is a year late, but I just found your article a few days ago via a reference on scott hanselmans blog (in comments). Here is my rebuttal.<br> http://candordeveloper.com/2012/06/26/provider-model-is-a-solid-pattern/</div> <div class="comment-date">2012-06-29 11:31 UTC</div> </div> <div class="comment" id="e6732c1c9be54e6582afb5c4f59856e4"> <div class="comment-author"><a href="http://lukechavers.com">Luke Chavers</a> <a href="#e6732c1c9be54e6582afb5c4f59856e4">#</a></div> <div class="comment-content">Also going to throw in on the disagreement with your assertion:<br><br> "A design pattern is not invented - it's discovered as a repeated solution to a commonly recurring problem." <br><br> The design pattern concept does not provide an opinion on how they were arrived upon. I am always delighted when programming terminology gives me new insight into words. Kinda like "abstraction" -- I don't think I would even know the term if I were not a programmer, at least, not as well as I do.<br><br> However, programming has little to teach us about the words "design" and "pattern" that we do not already know. There is nothing mysterous or supernatural about them, even when combined. A pattern is anything that repeats in a predictable fashion.. and that's really all there is to it.<br><br> Whether or not the service provider is a good pattern, I am not sure, but I can be certain that it is, in fact, a pattern. Isn't the neuance you're describing subjective, anyway? Suppose that you invented it, and then I used it. Suppose also that a third developer "discovered" our usage. What then, does the universe unwind? My example is trivial, but scale it up and it makes a little more sense. <br><br> But, I do love your blog, thank you for the discussion. </div> <div class="comment-date">2014-11-11 9:55 UTC</div> </div> <div class="comment" id="7f23248f833946c2b824e5774539aa06"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7f23248f833946c2b824e5774539aa06">#</a></div> <div class="comment-content"> <p> Luke, thank you for writing. Obviously, you are free to have your own opinions, and interpretation of various words. However, if we want to be able to discuss our trade in a concise manner, we need as precise a vocabulary as possible. </p> <p> On this blog (and elsewhere), I strive to use a consistent and well-known jargon. Thus, when I use the term <em>design pattern</em>, I refer to it in a way that's compatible with the original description in <a href="http://amzn.to/XBYukB">Design Patterns</a>. In the introduction (on page 2), Gamma et al. write: <blockquote> "None of the design patterns in this book describes new or unproven designs. We have included only designs that <em>have been</em> applied <em>more than once</em> in <em>different systems</em>." </blockquote> (Emphasis mine.) The point is that, using the established vocabulary, a design pattern most certainly implies that the pattern is a result of parallel evolution, and subsequently discovered and described as a common solution to a particular type of problem. </p> <p> Although I grant that it's <em>partially</em> subjective, there's still an implicit value judgement in cataloguing a design pattern. While most patterns come with a set of known disadvantages, but those disadvantages tend to not outweigh the benefits. If they did, it wouldn't be a design pattern, but rather an anti-pattern. This, too, is a well-defined term: <blockquote> <a href="http://amzn.to/1gDifab">"a commonly occurring solution to a problem that generates decidedly negative consequences."</a> </blockquote> This definition differs from a 'proper' design pattern because a design pattern has mostly beneficial consequences. </p> </div> <div class="comment-date">2014-11-14 20:32 UTC</div> </div> <div class="comment" id="37c78e87b18746c8ae9909666c4140da"> <div class="comment-author"><a href="http://lukechavers.com">Luke Chavers</a> <a href="#37c78e87b18746c8ae9909666c4140da">#</a></div> <div class="comment-content"> <p>I appreciate your effort to maintain consistent jargon and termonology. I have had dozens of heated arguments with my co-workers about individual words in our interfaces, all the while they're staring back at me with a look that says "omg dude, its just a word." I gather that you and I have that common.</p> <p>Personally, I'm against the concept of jargon all together because it, by its nature, can only create confusion. By that I mean, if it is consistent with the English language, then its not jargon, its just English. Conversely, if it contradicts or conflicts with the English language, then its bad jargon anyway, and can only lead to confusion. I tend to err on the side of verbosity, and might have described the pattern as not being a "Beneficial Design Pattern".</p> <p>In reading the paragraph you quoted from, I did not get the impression that the author gave an opinion on the origin of a pattern as a contributor to its validity. I think he is simply trying to disclaim the notion that the patterns described within the book are original ideas, and effort was made to only include patterns that have been implemented in the wild, and by more than one implementor. i.e. I did not write a book about my own patterns, or the clever things my co-workers have done.</p> <blockquote> "So although these designs are not new, we capture them in a new and accessible way: as a catalog of design patterns having a consistent format." </blockquote> <p>The service pattern certainly has been implemented by more than one implementor, sometimes identified by a different name, sometimes in such a way that provides more benefit than otherwise, and very likely before Microsoft introduced it to the world at-large.</p> <p>My critique of your article was based solely on the language used and only because of provocativeness of your statement. It seemed intentionally unintuitive.</p> <p>With all of that said, though, I concede my point. I do that mostly because you obviously have a superior understanding of design patterns than I do (by multitudes). Your blog is one of resources that first got me interested in the study of design patterns, and I've learned a great deal from it. I think it would be rather foolish for me to try to "take you on", as I'd certainly be embarrassed.</p> <p>Besides that, I could not formulate an opinion on the supporting arguments you put forth as I reread the blog. I have not programmed in .NET in a very long time and I have trouble understanding if we're even referring to the same things. I fear that I am a bit out of my league.</p> <p>My [perhaps misguided] interpretation of the "Service Pattern" comes from places wholy unrelated to .NET. I've seen the term used to describe a pattern akin to the "Adapter Pattern", where the term "Adapter" is replaced by "Service", and "Adaptee" is replaced by "Provider".</p> <pre>Implementor &lt; Service &lt; Provider (or ServiceProvider)</pre> <p>i.e.</p> <pre>Model &lt; StorageService &lt; MySQLProvider</pre> <p>Thank you for your thoughtful response.</p> </div> <div class="comment-date">2014-11-15 3:10 UTC</div> </div> <div class="comment" id="17c029f35991491a89bf86b42a460a62"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#17c029f35991491a89bf86b42a460a62">#</a></div> <div class="comment-content"> <p> At the risk of coming across as a pedant, did you mean <em>Provider</em> when you wrote "Service Pattern"? At least, I'm not aware of any Service Pattern in the most commonly accepted pattern literature, but perhaps there's a book I should read. </p> <p> The Provider design isn't the same as the Adapter pattern. The difference is, among others, that Providers rely on the .NET configuration system, as well as the Constrained Construction anti-pattern. Particularly its reliance on the .NET configuration system is what excludes it from being a proper pattern, because you couldn't possibly discover it on a different platform. </p> <p> The Adapter pattern describes how to adapt one API to another API; while it's a useful pattern, it has a different concern than Provider. It also doesn't prescribe any particular method of initialization. </p> </div> <div class="comment-date">2014-11-15 20:53 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 2.1 beta 1 https://blog.ploeh.dk/2011/04/19/AutoFixture2.1beta1 2011-04-19T14:05:11+00:00 Mark Seemann <div id="post"> <p>It gives me great pleasure to announce that <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.1 beta 1 is now <a href="http://autofixture.codeplex.com/releases/view/64686">available for download</a>. Compared to version 2.0 this release mostly contains added features as well as a few bug fixes. The release page contains a list of the new features.</p> <p>The general availability of beta 1 of AutoFixture 2.1 marks the beginning of a trial period. If no new issues are reported within the next few weeks, a final version 2.1 will be released. If too many issues are reported, a new beta version may be necessary.</p> <p>Please <a href="http://autofixture.codeplex.com/workitem/list/basic">report any issues</a> you find.</p> <p>NuGet packages will follow the RTW version.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Constructor strategies for AutoFixture https://blog.ploeh.dk/2011/04/19/ConstructorstrategiesforAutoFixture 2011-04-19T06:59:19+00:00 Mark Seemann <div id="post"> <p>When <a href="http://autofixture.codeplex.com/">AutoFixture</a> creates complex objects it invokes the constructors of the types in question. When a class exposes more than one constructor, the <a href="/2009/03/24/HowAutoFixtureCreatesObjects">default behavior is to pick the constructor with the fewest number&nbsp; of arguments</a>. This is the exact opposite of most DI Containers that select the greediest constructor.</p> <p>For a DI Container it makes more sense to select the greediest constructor because that maximizes the chance that all dependencies are properly being auto-wired.</p> <p>The reason that AutoFixture's default behavior is different is that it maximizes the chance that an object graph can be created at all. If a convenience overload is available, this gives AutoFixture a better chance to compose the object graph.</p> <p align="left">This works well until a class comes along and introduces the wrong kind of ambiguity. Consider this case of <em>Bastard Injection</em> (<a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a>, section 5.2):</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Bastard</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IFoo</font></span> foo;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Bastard()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: "><font color="#0000ff">this</font></span>(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">DefaultFoo</font></span>())</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Bastard(<span style="color: "><font color="#2b91af">IFoo</font></span> foo)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (foo == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"foo"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.foo = foo;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#2b91af">IFoo</font></span> Foo</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.foo; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p align="left">By default AutoFixture will use the default constructor which means that the Foo will always be the instance of DefaultFoo owned by the Bastard class itself. This is problematic if we wish to <a href="/2010/03/27/Freezingmocks">inject and freeze a Test Double</a> because even if we freeze an IFoo instance, it will never be injected because the default constructor is being invoked.</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>();</font> <font style="font-size: 10pt">fixture.Register&lt;<span style="color: "><font color="#2b91af">IFoo</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">DummyFoo</font></span>&gt;);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> b = fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">Bastard</font></span>&gt;();</font> <span style="color: "><font color="#2b91af"><font style="font-size: 10pt">Assert</font></font></span><font style="font-size: 10pt">.IsAssignableFrom&lt;<span style="color: "><font color="#2b91af">DefaultFoo</font></span>&gt;(b.Foo);</font></pre> </p> <p align="left">As the above unit test demonstrates, even though the Register method call defines a mapping from IFoo to DummyFoo, the Foo property is an instance of DefaultFoo. The DummyFoo instance is never injected into the Bastard instance since the default constructor is used.</p> <p align="left">Your first reaction in such a case should be to get rid of all convenience constructors to make the class' constructor unambiguous. However, it's also possible to change AutoFixture's behavior.</p> <blockquote> <p align="left">The following is a description of a feature that will be a available in AutoFixture 2.1. It's not available in AutoFixture 2.0, but is already available in the code repository. Thus, if you can't wait for AutoFixture 2.1 you can download the source and build it.</p></blockquote> <p align="left">As always, AutoFixture's very extensible interface makes it possible to change this behavior. Constructor selection is guided by an interface called IConstructorQuery, and while ModestConstructorQuery is the default implementation, there's also an implementation called GreedyConstructorQuery.</p> <p align="left">To change the behavior specifically for the Bastard class the Fixture instance must be customized. The following unit test demonstrates how to do that.</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>();</font> <font style="font-size: 10pt">fixture.Customize&lt;<span style="color: "><font color="#2b91af">Bastard</font></span>&gt;(c =&gt; c.FromFactory(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">MethodInvoker</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">GreedyConstructorQuery</font></span>())));</font> <font style="font-size: 10pt">fixture.Register&lt;<span style="color: "><font color="#2b91af">IFoo</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">DummyFoo</font></span>&gt;);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> b = fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">Bastard</font></span>&gt;();</font> <span style="color: "><font color="#2b91af"><font style="font-size: 10pt">Assert</font></font></span><font style="font-size: 10pt">.IsAssignableFrom&lt;<span style="color: "><font color="#2b91af">DummyFoo</font></span>&gt;(b.Foo);</font></pre> </p> <p align="left">Notice that the only difference from before is an additional call to the Customize method where Bastard is modified to use greedy constructor selection. This changes the factory for the Bastard type, but not for any other types.</p> <p align="left">If you'd rather prefer to completely change the overall constructor selection behavior for AutoFixture you can add the GreedyConstructorQuery wrapped in a MethodInvoker to the Fixture's Customizations:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>();</font> <font style="font-size: 10pt">fixture.Customizations.Add(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">MethodInvoker</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">GreedyConstructorQuery</font></span>()));</font> <font style="font-size: 10pt">fixture.Register&lt;<span style="color: "><font color="#2b91af">IFoo</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">DummyFoo</font></span>&gt;);</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> b = fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">Bastard</font></span>&gt;();</font> <span style="color: "><font color="#2b91af"><font style="font-size: 10pt">Assert</font></font></span><font style="font-size: 10pt">.IsAssignableFrom&lt;<span style="color: "><font color="#2b91af">DummyFoo</font></span>&gt;(b.Foo);</font></pre> </p> <p align="left">In this unit test, the only change from the previous is that instead of assigning the GreedyConstructorQuery specifically to Bastard, it's now assigned as the new default strategy. All specimens created by AutoFixture will be created by invoking their most greedy constructor.</p> <p align="left">As always I find the default behavior more attractive, but the option to change behavior is there if you need it.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="3c4f94f81072462f8c17b2b7f9acc9c4"> <div class="comment-author"><a href="https://plus.google.com/+WestonMcNamee/about">Wes</a> <a href="#3c4f94f81072462f8c17b2b7f9acc9c4">#</a></div> <div class="comment-content">I seem to encountered a bug. It seems that the greedy constructor query & OmitAutoProperties don't play nice together when the constructor parameters fill in properties. <br>I've submitted an issue for this: <a href="https://github.com/AutoFixture/AutoFixture/issues/320">Issue#320</a> <br><pre class="csharp" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0600FF; font-weight: bold;">namespace</span> JustATest</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Ploeh.AutoFixture</span><span style="color: #008000;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Ploeh.AutoFixture.Kernel</span><span style="color: #008000;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Xunit</span><span style="color: #008000;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> JustATest <span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #008000;">&#91;</span>Fact<span style="color: #008000;">&#93;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> GreedyConstructorAndOmitAutoProps<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">var</span> fixture <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Fixture<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> fixture<span style="color: #008000;">.</span><span style="color: #0000FF;">Customize</span><span style="color: #008000;">&lt;</span>Foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c<span style="color: #008000;">.</span><span style="color: #0000FF;">FromFactory</span><span style="color: #008000;">&#40;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #008000;">new</span> MethodInvoker<span style="color: #008000;">&#40;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #008000;">new</span> GreedyConstructorQuery<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> fixture<span style="color: #008000;">.</span><span style="color: #0000FF;">Customize</span><span style="color: #008000;">&lt;</span>Foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c<span style="color: #008000;">.</span><span style="color: #0000FF;">OmitAutoProperties</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">var</span> foo <span style="color: #008000;">=</span> fixture<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&lt;</span>Foo<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">NotNull</span><span style="color: #008000;">&#40;</span>foo<span style="color: #008000;">.</span><span style="color: #0000FF;">myStr</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Foo <span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">public</span> Foo<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> myStr<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">myStr</span> <span style="color: #008000;">=</span> myStr<span style="color: #008000;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">public</span> Foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> myStr <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li></ol></pre> </div> <div class="comment-date">2014-10-17 6:48 UTC</div> </div> <div class="comment" id="8a6f6816e4f049c9b4b105f2f90e2be3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8a6f6816e4f049c9b4b105f2f90e2be3">#</a></div> <div class="comment-content"> <p> Wes, thank you for writing. Let's continue the discussion over at that <a href="https://github.com/AutoFixture/AutoFixture/issues/320">GitHub issue</a>. </p> </div> <div class="comment-date">2014-10-18 8:26 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Enumerables are dynamic - also in AutoFixture https://blog.ploeh.dk/2011/04/18/EnumerablesaredynamicalsoinAutoFixture 2011-04-18T13:22:29+00:00 Mark Seemann <div id="post"> <p>I just got a question about <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s way of dealing with enumerables, and since I suspect this is something that might surprise a few people I found it reasonable to answer in a blog post.</p> <p>In short, this unit test <em>succeeds:</em></p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>();</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> expected = fixture.CreateMany&lt;<span style="color: "><font color="#0000ff">string</font></span>&gt;();</font> <span style="color: "><font color="#2b91af"><font style="font-size: 10pt">Assert</font></font></span><font style="font-size: 10pt">.False(expected.SequenceEqual(expected));</font></pre> </p> <p>If this doesn't surprise you, then read the assertion again. The sequence is expected to <em>not</em> equal itself!</p> <p><em>This behavior is by design.</em></p> <p>(I've always wanted to write that.) The reason is that the return type of the CreateMany method is IEnumerable&lt;T&gt;, and that interface makes no guarantee that it will return the same sequence every time you iterate over it. The implementation may be a <a href="http://en.wikipedia.org/wiki/Generator_%28computer_science%29">Generator</a>.</p> <p>According to its principle of <a href="/2009/03/05/ConstrainedNon-Determinism">Constrained Non-determinism</a>, AutoFixture goes to great lengths to ensure that since IEnumerable&lt;T&gt; <em>might be</em> non-deterministic, it will be. This can be very useful in flushing out incorrect assumptions made by the <a href="http://xunitpatterns.com/SUT.html">SUT</a>.</p> <p align="left">This behavior is carried forward by the <a href="/2011/02/08/CreatinggeneralpopulatedlistswithAutoFixture">MultipleCustomization</a>. This unit test also succeeds:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .Customize(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">MultipleCustomization</font></span>());</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> expected =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#0000ff">string</font></span>&gt;&gt;();</font> <span style="color: "><font color="#2b91af"><font style="font-size: 10pt">Assert</font></font></span><font style="font-size: 10pt">.False(expected.SequenceEqual(expected));</font></pre> </p> <p>The behavior is the same because with the MultipleCustomization IEnumerable&lt;T&gt; acts as a <a href="http://nblumhardt.com/2010/01/the-relationship-zoo/">relationship type</a>. The class responsible for this behavior is called FiniteSequenceRelay, but AutoFixture 2.1 will also ship with an alternative StableFiniteSequenceRelay which wraps the sequence in a List&lt;T&gt;.</p> <p>To change the default behavior you can add the StableFiniteSequenceRelay to a Fixture's customizations:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>();</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> stableRelay = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">StableFiniteSequenceRelay</font></span>();</font> <font style="font-size: 10pt">fixture.Customizations.Add(stableRelay);</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> expected =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span style="color: "><font color="#0000ff">string</font></span>&gt;();</font> <span style="color: "><font color="#2b91af"><font style="font-size: 10pt">Assert</font></font></span><font style="font-size: 10pt">.True(expected.SequenceEqual(expected));</font></pre> </p> <p>The above unit test succeeds. Notice that the assertion now verifies that the sequence of strings is equal to itself.</p> <blockquote> <p>Just like MultipleCustomization the StableFiniteSequenceRelay class will be available in AutoFixture 2.1, but is not availale in 2.0.</p></blockquote> <p>As always, it's best to <a href="/2011/03/18/EncapsulatingAutoFixtureCustomizations">encapsulate this behavior change into a Customization</a>:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">StableFiniteSequenceCustomization</font></span> :</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">ICustomization</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Customize(<span style="color: "><font color="#2b91af">IFixture</font></span> fixture)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> stableRelay = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">StableFiniteSequenceRelay</font></span>();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.Customizations.Add(stableRelay);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>This makes it easier to bundle it with the MultipleCustomization if we want all the other benefits of conventions for multiples:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">StableMultipeCustomization</font></span> : </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">CompositeCustomization</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> StableMultipeCustomization()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: "><font color="#0000ff">base</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">StableFiniteSequenceCustomization</font></span>(),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">MultipleCustomization</font></span>())</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>With the StableMultipleCustomization the following unit test now passes:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .Customize(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">StableMultipeCustomization</font></span>());</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> expected =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#0000ff">string</font></span>&gt;&gt;();</font> <span style="color: "><font color="#2b91af"><font style="font-size: 10pt">Assert</font></font></span><font style="font-size: 10pt">.True(expected.SequenceEqual(expected));</font></pre> </p> <p>I still prefer the default behavior for its potential to act as an early warning system, but as I realize that this behavior may be problematic in some scenarios, the StableFiniteSequenceRelay provides an easy way to change that behavior.</p> <p><strong>Update (2011.8.10):</strong> StableFiniteSequenceCustomization is now part of AutoFixture and will be available in AutoFixture 2.2.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="3ac8582b21e84eb2b52f6c7e91f205a5"> <div class="comment-author">Gleb <a href="#3ac8582b21e84eb2b52f6c7e91f205a5">#</a></div> <div class="comment-content">hello Mark, first of all thanks for a great library! I've been looking for something like AutoFixture.<br> Can you please clarify, what do I need to do to make following scenario work:<br> I have two classes, Stock and StockMarket, which are in many2many relationship (each has collection of other one). I want to use AutoFixture with Fluent NHibernate persistence specification for tests, but exception tells me: Ploeh.AutoFixture.ObjectCreationException: AutoFixture was unable to create an instance of type System.RuntimeType because the traversed object graph contains a circular reference</div> <div class="comment-date">2012-02-27 18:18 UTC</div> </div> <div class="comment" id="65db86396a9c4e829355894bc63a76f4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#65db86396a9c4e829355894bc63a76f4">#</a></div> <div class="comment-content">Circular references are, IMO, a design smell, but since you are attempting to work with relational data, I guess you can't change the design.<br> <br> The easiest way to get around an issue like that is to customize the types to not fill in the properties in question. Something like this:<br> <br> fixture.Customize&lt;Stock&gt;(c =&gt; c.Without(s =&gt; s.StockMarket));<br> <br> fixture.Customize&lt;StockMarket&gt;(c =&gt; c.Without(sm =&gt; sm.Stock));</div> <div class="comment-date">2012-02-27 19:15 UTC</div> </div> <div class="comment" id="9cf28aaa101e4362b1ed9a551605d5ed"> <div class="comment-author">Gleb <a href="#9cf28aaa101e4362b1ed9a551605d5ed">#</a></div> <div class="comment-content">Thanks, I'll look into that</div> <div class="comment-date">2012-02-28 05:10 UTC</div> </div> <div class="comment" id="55b71fe84d1b4492a90842343c1bc00f"> <div class="comment-author">Diogo Castro <a href="#55b71fe84d1b4492a90842343c1bc00f">#</a></div> <div class="comment-content">Great blog post! I love the idea of making IEnumerable&lt;T&gt; non-deterministic. The fact that two traversals might yield different results is often overlooked by most people (me included). Luckily ReSharper reminds me when I'm iterating through an enumerable twice <em>within the same scope</em>, and having AutoFixture slap me across the face would be even better.<br> <br> It appears that the default behaviour has changed since this post was written nearly 4 years ago. The test now fails.<br> If I may ask, why the change of heart? </div> <div class="comment-date">2015-01-15 17:32 UTC</div> </div> <div class="comment" id="9f61a10cb1d742bfbfc886611f141a14"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9f61a10cb1d742bfbfc886611f141a14">#</a></div> <div class="comment-content"> <p> Diogo, thank you for writing. The reason for the changed behaviour was that the old behaviour caused too much confusion. Essentially, you can boild down the problem to the fact that if you compare 'two' IEnumerable&lt;T&gt; instances with ReferenceEquals, it could evaluate to true, and <em>still</em> contain different values when you start enumerating over them. </p> <p> In AutoFixture 3, we did some tweaks to make the default experience with AutoFixture more intuitive, i.e. aiming for the <em>Pit of Success</em>. Our experience was that dynamic enumerables constantly tripped up people, including ourselves! </p> <p> The old behaviour is still available as FiniteSequenceRelay, if you want to reinstate it. Just add a FiniteSequenceRelay instance to your Fixture's Customizations collection. </p> </div> <div class="comment-date">2015-01-15 19:11 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. MSDN Magazine article about CQRS on Windows Azure https://blog.ploeh.dk/2011/04/05/MSDNMagazinearticleaboutCQRSonWindowsAzure 2011-04-05T19:52:57+00:00 Mark Seemann <div id="post"> <p align="left">My latest MSDN Magazine article, this time about CQRS on Windows Azure, is now <a href="http://msdn.microsoft.com/en-us/magazine/gg983487.aspx">available at the April MSDN Magazine web site</a>.</p> <p align="left">It's mostly meant as an introduction to CQRS as well as containing some tips and tricks that are specific to applying CQRS on Windows Azure.</p> <p>As an added bonus the code sample download contains lots of idiomatic unit tests written with <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s <a href="/2010/10/08/AutoDataTheorieswithAutoFixture">xUnit.net extensions</a>, so if you'd like to see the result of my TDD work with AutoFixture, there's a complete code base to look at there.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="32e933a6374441ea8e837fc5e834f1e6"> <div class="comment-author">Clement <a href="#32e933a6374441ea8e837fc5e834f1e6">#</a></div> <div class="comment-content">Good article and also great sample code !</div> <div class="comment-date">2011-04-11 06:45 UTC</div> </div> <div class="comment" id="6de107dbb7924cdd9ddb5f77b41f452c"> <div class="comment-author"><a href="http://www.programmers-unlimited.com">Dustin Davis</a> <a href="#6de107dbb7924cdd9ddb5f77b41f452c">#</a></div> <div class="comment-content">Excellent article.</div> <div class="comment-date">2011-04-14 14:54 UTC</div> </div> <div class="comment" id="830b2ee3a12741dcb7dc4fb8a9cc0009"> <div class="comment-author"><a href="http://agilefromthegroundup.blogspot.com">Josh Gough</a> <a href="#830b2ee3a12741dcb7dc4fb8a9cc0009">#</a></div> <div class="comment-content">Really enjoying the article, Mark. I was racking my brain at lunch thinking about some of these issues, regarding safe modification of state, etc. Then, I found the article.<br> <br> I may end up doing things with Node and Redis, but still wanting to learn how to do things with Azure.</div> <div class="comment-date">2012-11-30 19:10 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Commands are Composable https://blog.ploeh.dk/2011/03/22/CommandsareComposable 2011-03-22T13:09:25+00:00 Mark Seemann <div id="post"> <p> A few months back I wrote a (somewhat theoretical) <a href="/2010/12/03/Towardsbetterabstractions">post on composable interfaces</a>. A major point of that post was that <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a>&nbsp; with a single <a href="http://en.wikipedia.org/wiki/Command_pattern">Command</a> method (i.e. a method that returns no value) is a very versatile category of abstraction. </p> <p> Some of my readers asked for examples, so in this post I will provide a few. Consider this interface that fits the above description: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Consume(T message);</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This is a very common type of interface you will tend to encounter a lot in distributed, message-based architectures such as <a href="http://abdullin.com/cqrs/">CQRS</a> or <a href="http://msdn.microsoft.com/en-us/architecture/aa699424">Udi Dahan's view of SOA</a>. Some people would call it a <em>message subscriber</em> instead... </p> <p> In the rest of this post I will examine how we can create compositions out of the IMessageConsumer&lt;T&gt; interface using (in order of significance) <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a>, <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a>, <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composite</a>, and other well-known programming constructs. </p> <h3 id="cf45059536e2425a8d50a6479262645f"> Decorator <a href="#cf45059536e2425a8d50a6479262645f" title="permalink">#</a> </h3> <p> Can we create a meaningful Decorator around the IMessageConsumer&lt;T&gt; interface? Yes, that's easy - <a href="/2010/04/07/DependencyInjectionisLooseCoupling">I've earlier provided various detailed examples of Decorators</a>, so I'm not going to repeat them here. </p> <blockquote> <p> I've yet to come up with an example of an interface that prevents us from applying a Decorator, so it's a valid <a href="http://en.wikipedia.org/wiki/Falsifiability">falsifiable</a> claim that we can always Decorate an interface. However, I have yet to prove that this is true, so until now we'll have to call it a <a href="http://en.wikipedia.org/wiki/Conjecture">conjecture</a>. </p> </blockquote> <p> However, since it's so easy to apply a Decorator to an interface, it's not a particularly valuable trait when evaluating the composability of an interface. </p> <h3 id="cdbec63e1e914762a0713e385faa22e3"> Null Object <a href="#cdbec63e1e914762a0713e385faa22e3" title="permalink">#</a> </h3> <p> It can be difficult to implement the Null Object pattern when the method(s) in question return a value, but for Commands it's easy: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">NullMessageConsumer</font></span>&lt;T&gt; : <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Consume(T message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> The implementation simply ignores the input and does nothing. </p> <blockquote> <p> Once again my lack of formal CS education prevents me from putting forth a formal proof, but I strongly suspect that it's always possibly to apply the Null Object pattern to a Command (keep in mind that <em>out</em> parameters count as output, so any interface with one or more of these are not Commands). </p> </blockquote> <p> It's often valuable to be able to use a Null Object, but the real benefit comes when we can compose various implementations together. </p> <h3 id="113a835866e3463ca3bc8cf72c1c89b0"> Composite <a href="#113a835866e3463ca3bc8cf72c1c89b0" title="permalink">#</a> </h3> <p> To be truly composable, an interface should make it possible to create various concrete implementations that each adhere to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> and then compose those together in a complex implementation. A Composite is a general-purpose implementation of this concept, and it's easy to create a Composite out of a Command: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">CompositeMessageConsumer</font></span>&lt;T&gt; : </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt;&gt; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; consumers;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> CompositeMessageConsumer(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">params</font></span> <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt;[] consumers)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (consumers == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"consumers"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.consumers = consumers;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#2b91af">IEnumerable</font></span>&lt;<span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt;&gt; Consumers</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">get</font></span> { <span style="color: "><font color="#0000ff">return</font></span> <span style="color: "><font color="#0000ff">this</font></span>.consumers; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; #region</font></font></span><font style="font-size: 10pt"> IMessageConsumer&lt;T&gt; Members</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Consume(T message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">foreach</font></span> (<span style="color: "><font color="#0000ff">var</font></span> consumer <span style="color: "><font color="#0000ff">in</font></span> <span style="color: "><font color="#0000ff">this</font></span>.Consumers)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; consumer.Consume(message);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span style="color: "><font style="font-size: 10pt" color="#0000ff">&nbsp;&nbsp;&nbsp; #endregion</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p> The implementation of the Consume method simply loops over each composed IMessageConsumer&lt;T&gt; and invokes its Consume method. </p> <p> I can, for example, implement a sequence of actions that will take place by composing the individual concrete implementations. First we have a guard that protects against invalid messages, followed by a consumer that writes the message to a persistent store, completed by a consumer that raises a Domain Event that the reservation request was accepted. </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> c = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">CompositeMessageConsumer</font></span>&lt;<span style="color: "><font color="#2b91af">MakeReservationCommand</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; guard, </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; writer, </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; acceptRaiser);</font></pre> </p> <p> Consumers following the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> will not notice the difference, as all they will see is an implementation of IMessageConsumer&lt;MakeReservationCommand&gt;. </p> <h3 id="2d09f747a3ce48ab8bf8528a8893580a"> More advanced programming constructs <a href="#2d09f747a3ce48ab8bf8528a8893580a" title="permalink">#</a> </h3> <p> The Composite pattern only describes a single, general way to compose implementations, but with a Command interface we can do more. As <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215">Domain-Driven Design</a> explains, a successful interface is often characterized by making it possible to apply well-known arithmetic or logical operators. As an example, in the case of the IMessageConsumer&lt;T&gt; interface, we can easily mimic the well-known ?! ternary operator from C#: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">ConditionalMessageConsumer</font></span>&lt;T&gt; : </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#2b91af">Func</font></span>&lt;T, <span style="color: "><font color="#0000ff">bool</font></span>&gt; condition;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt; first;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt; second;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> ConditionalMessageConsumer(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Func</font></span>&lt;T, <span style="color: "><font color="#0000ff">bool</font></span>&gt; condition, </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt; first, </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">IMessageConsumer</font></span>&lt;T&gt; second)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (condition == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"condition"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (first == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"first"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">if</font></span> (second == <span style="color: "><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">throw</font></span> <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ArgumentNullException</font></span>(<span style="color: "><font color="#a31515">"second"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.condition = condition;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.first = first;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.second = second;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">void</font></span> Consume(T message)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (<span style="color: "><font color="#0000ff">this</font></span>.condition(message) ? <span style="color: "><font color="#0000ff">this</font></span>.first : <span style="color: "><font color="#0000ff">this</font></span>.second)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Consume(message);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> This is more verbose than the ?! operator because C# doesn't allow us to define operator overloads for interfaces, but apart from that, it does exactly the same thing. Notice particularly that the Consume method uses the ?! operator to select among the two alternatives, and then subsequently invokes the Consume method on the selected consumer. </p> <p> We can use the ConditionalMessageConsumer to define branches in the consumption of messages. As an example, we can encapsulate the previous CompositeMessageConsumer&lt;MakeReservationCommand&gt; into a conditional branch like this: </p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> consumer = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">ConditionalMessageConsumer</font></span>&lt;<span style="color: "><font color="#2b91af">MakeReservationCommand</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; guard.HasCapacity, c, rejectRaiser);</font></pre> </p> <p> Notice that I use the method group syntax to supply the condition delegate. If the HasCapacity method returns true, the previous composite (c) is being invoked, but if the result is false we instead use a consumer that raises the Domain Event that the reservation request was rejected. </p> <h3 id="469f9b86d97743dc812937a124a31ddf"> Concluding thoughts <a href="#469f9b86d97743dc812937a124a31ddf" title="permalink">#</a> </h3> <p> Apart from the direct purpose of providing examples of the immensely powerful composition options a Command interface provides I want to point out a couple of things: </p> <ul> <li> Each of the design pattern implementations (Null Object, Composite - even Conditional) are generic. This is a strong testament to the power of this particular abstraction. </li> <li> The dynamic mocks I'm familiar with (<a href="http://code.google.com/p/moq/">Moq</a>, <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a>) will by default try to create Null Object implementations for interfaces without explicit setups. Since it's trivial to implement a Null Command, they just emit them by default. If you use an <a href="/2013/03/11/auto-mocking-container">Auto-mocking Container</a> with your unit tests, you can refactor to your heart's content, adding and removing dependencies as long as they are Command interfaces, and your testing infrastructure will just take care of things for you. It'll just work. </li> <li> Did you notice that even with these few building blocks we have implemented a large part of a sequential workflow engine? We can execute consumers in sequence as well as branch between different sequences. Obviously, more building blocks are needed to make a full-blown workflow engine, but not that many. I'll leave the rest as an exercise to the reader :) </li> </ul> <p> As I <a href="/2010/12/03/Towardsbetterabstractions">originally sketched</a>, a Command interface is the ultimate in composability. To illustrate, the application from where I took the above examples is a small application with 48 types (classes and interfaces) in the production code base (that is, excluding unit tests). Of these, 9 are implementations of the IMessageConsumer&lt;T&gt; interface. If we also count the interface itself, it accounts for more than 20 percent of the code base. According to the <a href="http://parlezuml.com/blog/?postid=934">Reused Abstractions Principle (RAP)</a> I consider this a very successful abstraction. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Encapsulating AutoFixture Customizations https://blog.ploeh.dk/2011/03/18/EncapsulatingAutoFixtureCustomizations 2011-03-18T12:51:08+00:00 Mark Seemann <div id="post"> <p><a href="http://autofixture.codeplex.com/">AutoFixture</a> is designed around the 80-20 principle. If your code is well-designed I'd expect a default instance of Fixture to be able to create specimens of your classes without too much trouble. There are cases where AutoFixture needs extra help:</p> <ul> <li>If a class consumes interfaces a default Fixture will not be able to create it. However, this is easily fixed through one of the <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer">AutoMocking Customizations</a>. <li>If an API has circular references, Fixture might enter an infinite recursion, but you can easily customize it to cut off one the references. <li>Some constructors may only accept arguments that don't fit with the default specimens created by Fixture. <a href="/2009/05/01/DealingWithConstrainedInput">There are ways to deal with that</a> as well.</li></ul> <p>I could keep on listing examples, but let's keep it at that. The key assumption underlying AutoFixture is that these special cases are a relatively small part of the overall API you want to test - preferably much less than 20 percent.</p> <p>Still, to address those special cases you'll need to customize a Fixture instance in some way, but you also want to keep your test code DRY.</p> <p>As the popularity of AutoFixture grows, I'm getting more and more glimpses of how people address this challenge: Some derive from Fixture, others create extension methods or other static methods, while others again wrap creation of Fixture instances in Factories or Builders.</p> <p>There really is no need to do that. AutoFixture has an idiomatic way to encapsulate Customizations. It's called… well… a <em>Customization</em>, which is just another way to say that there's an ICustomization interface that you can implement. This concept corresponds closely to the modularization APIs for several well-known DI Containers. Castle Windsor has <em>Installers</em>, StructureMap has <em>Registries</em> and Autofac has<em> Modules</em>.</p> <p>The ICustomization interface is simple and has a very gentle learning curve:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">interface</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">ICustomization</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">void</font></span> Customize(<span style="color: "><font color="#2b91af">IFixture</font></span> fixture);</font> <font style="font-size: 10pt">}</font></pre> </p> <p>Anything you can do with a Fixture instance you can also do with the IFixture instance passed to the Customize method, so this is the perfect place to encapsulate common Customizations to AutoFixture. Note that the AutoMocking extensions as well as several other optional behaviors for AutoFixture are already defined as Customizations.</p> <p>Using a Customization is also easy:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .Customize(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">DomainCustomization</font></span>());</font></pre> </p> <p>You just need to invoke the Customize method on the Fixture. That's no more difficult than calling a custom Factory or extension method - particularly if you also use a Visual Studio Code Snippet.</p> <p>When I start a new unit testing project, one of the first things I always do is to create a new ‘default' customization for that project. It usually doesn't take long before I need to tweak it a bit - if nothing else, then for adding the AutoMoqCustomization. To apply separation of concerns I encapsulate each Customization in its own class and compose them with a CompositeCustomization:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">DomainCustomization</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">CompositeCustomization</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> DomainCustomization()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: "><font color="#0000ff">base</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">AutoMoqCustomization</font></span>(),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">FuncCustomization</font></span>())</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>Whenever I need to make sweeping changes to my Fixtures I can simply modify DomainCustomization or one of the Customizations it composes.</p> <p>In fact, these days I rarely explicitly create new Fixture instances, but rather encapsulate them in a custom AutoDataAttribute like this:</p> <p> <pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> <span style="color: "><font color="#2b91af">AutoDomainDataAttribute</font></span> : </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">AutoDataAttribute</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> AutoDomainDataAttribute()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: "><font color="#0000ff">base</font></span>(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">Fixture</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Customize(<span style="color: "><font color="#0000ff">new</font></span> <span style="color: "><font color="#2b91af">DomainCustomization</font></span>()))</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>This means that I can reuse the DomainCustomization across normal, imperative unit tests as well as the <a href="/2010/10/08/AutoDataTheorieswithAutoFixture">declarative, xUnit.net-powered data theories I normally prefer</a>:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">[<span style="color: "><font color="#2b91af">Theory</font></span>, <span style="color: "><font color="#2b91af">AutoDomainData</font></span>]</font> <span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">void</font></span> CanReserveReturnsTrueWhenQuantityIsEqualToRemaining(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Capacity</font></span> sut, <span style="color: "><font color="#2b91af">Guid</font></span> id)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">var</font></span> result = sut.CanReserve(sut.Remaining, id);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#2b91af">Assert</font></span>.True(result);</font> <font style="font-size: 10pt">}</font></pre> </p> <p>Using Customizations to encapsulate your own specializations makes it easy to compose and manage them in an object-oriented fashion.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Resolving closed types with MEF https://blog.ploeh.dk/2011/03/14/ResolvingclosedtypeswithMEF 2011-03-14T20:49:11+00:00 Mark Seemann <div id="post"> <p>A while back I posed the challenge of <a href="/2010/12/24/ChallengeResolveclosedtypeswithMEF">resolving closed types with MEF</a>. I received some responses, but I also wanted to provide an alternative outline for a solution. In case you don't remember the problem statement, it revolved around using the <a href="http://mef.codeplex.com/">Managed Extensibility Framework</a> (MEF) to compose classes in those cases where it's impossible to annotate those classes with the MEF attributes. In the given example I want to compose the Mayonnaise class from EggYolk and OliveOil, but all three classes are sealed and cannot be recompiled.</p> <p>As I describe <a href="http://amzn.to/12p90MG">in my book</a>, a general solution to this type of problem is to create a sort of adapter the exports the closed type via a read-only attribute, like these EggYolkAdapter and MayonnaiseAdapter classes (the OliveOilAdapter looks just like the EggYolkAdapter):</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> </font><span><font style="font-size: 10pt" color="#2b91af">EggYolkAdapter</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> <span><font color="#2b91af">EggYolk</font></span> eggYolk;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> EggYolkAdapter()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.eggYolk = <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">EggYolk</font></span>();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span><font color="#2b91af">Export</font></span>]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">virtual</font></span> <span><font color="#2b91af">EggYolk</font></span> EggYolk</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">get</font></span> { <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">this</font></span>.eggYolk; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font> <font size="2"></font>&nbsp; <span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> </font><span><font style="font-size: 10pt" color="#2b91af">MayonnaiseAdapter</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> <span><font color="#2b91af">Mayonnaise</font></span> mayo;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span><font color="#2b91af">ImportingConstructor</font></span>]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> MayonnaiseAdapter(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">EggYolk</font></span> yolk, <span><font color="#2b91af">OliveOil</font></span> oil)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (yolk == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ArgumentNullException</font></span>(<span><font color="#a31515">"yolk"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (oil == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ArgumentNullException</font></span>(<span><font color="#a31515">"oil"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.mayo = <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">Mayonnaise</font></span>(yolk, oil);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span><font color="#2b91af">Export</font></span>]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">virtual</font></span> <span><font color="#2b91af">Mayonnaise</font></span> Mayonnaise</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">get</font></span> { <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">this</font></span>.mayo; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>Doing it like this is always possible, but if you have a lot of types that you need to compose, it becomes tedious having to define a lot of similar adapters. Fortunately, we can take it a step further and generalize the idea of a MEF adapter to a small set of generic classes.</p> <p>The EggYolkAdapter can be generalized as follows:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">MefAdapter</font></span>&lt;T&gt; <span><font color="#0000ff">where</font></span> T : <span><font color="#0000ff">new</font></span>()</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> T export;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> MefAdapter()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.export = <span><font color="#0000ff">new</font></span> T();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span><font color="#2b91af">Export</font></span>]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">virtual</font></span> T Export</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">get</font></span> { <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">this</font></span>.export; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>Notice that I've more or less just replaced the EggYolk class with a type argument (T). However, I also had to add the generic new() constraint, which is often quite restrictive. However, to support a type like Mayonnaise, I can create another, similar generic MEF adapter like this:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">MefAdapter</font></span>&lt;T1, T2, TResult&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> <span><font color="#0000ff">static</font></span> <span><font color="#2b91af">Func</font></span>&lt;T1, T2, TResult&gt; createExport =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">FuncFactory</font></span>.Create&lt;T1, T2, TResult&gt;();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> TResult export;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span><font color="#2b91af">ImportingConstructor</font></span>]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> MefAdapter(T1 arg1, T2 arg2)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.export = createExport(arg1, arg2);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span><font color="#2b91af">Export</font></span>]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">virtual</font></span> TResult Export</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">get</font></span> { <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">this</font></span>.export; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>The major difference from the simple MefAdapter&lt;T&gt; is that we need slightly more complicated code to invoke the constructor of TResult, which is expected to take two constructor arguments of types T1 and T2. This work is delegated to a FuncFactory that builds and compiles the appropriate delegate using an expression tree:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">internal</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">static</font></span> <span><font color="#2b91af">Func</font></span>&lt;T1, T2, TResult&gt;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; Create&lt;T1, T2, TResult&gt;()</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> arg1Exp =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">Expression</font></span>.Parameter(<span><font color="#0000ff">typeof</font></span>(T1), <span><font color="#a31515">"arg1"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> arg2Exp = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">Expression</font></span>.Parameter(<span><font color="#0000ff">typeof</font></span>(T2), <span><font color="#a31515">"arg2"</font></span>);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> ctorInfo = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">typeof</font></span>(TResult).GetConstructor(<span><font color="#0000ff">new</font></span>[]</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">typeof</font></span>(T1),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">typeof</font></span>(T2)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> ctorExp =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">Expression</font></span>.New(ctorInfo, arg1Exp, arg2Exp);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#2b91af">Expression</font></span>.Lambda&lt;<span><font color="#2b91af">Func</font></span>&lt;T1, T2, TResult&gt;&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ctorExp, arg1Exp, arg2Exp).Compile();</font> <font style="font-size: 10pt">}</font></pre> </p> <p>With a couple of MEF adapters, I can now compose a MEF Catalog <em>almost</em> like I can with a real DI Container, and resolve the Mayonnaise class:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> catalog = <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">TypeCatalog</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">MefAdapter</font></span>&lt;<span><font color="#2b91af">OliveOil</font></span>&gt;),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">MefAdapter</font></span>&lt;<span><font color="#2b91af">EggYolk</font></span>&gt;),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">MefAdapter</font></span>&lt;<span><font color="#2b91af">EggYolk</font></span>, <span><font color="#2b91af">OliveOil</font></span>, <span><font color="#2b91af">Mayonnaise</font></span>&gt;)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; );</font> <span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> container = <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">CompositionContainer</font></span>(catalog);</font> <font style="font-size: 10pt">&nbsp;</font> <span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> mayo = container.GetExportedValue&lt;<span><font color="#2b91af">Mayonnaise</font></span>&gt;();</font></pre> </p> <p>If you want to change the Creation Policy to NonShared, you can derive from the MefAdapter classes and annotate them with [PartCreationPolicy] attributes.</p> <p>I'd never voluntarily choose to use MEF like this, but if I was stuck with MEF in a project and had to use it like a DI Container, I'd do something like this.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="fa7edc58534b476f99d1a0f9eeb64514"> <div class="comment-author"><a href="http://blog.longle.net">Le</a> <a href="#fa7edc58534b476f99d1a0f9eeb64514">#</a></div> <div class="comment-content">Is there anyway we can enhance your MefAdapter to support keys as well? So that when the adapter is export's using the Export property we could associate a key to the export? With attributes I know the string has to be constant, didn't know if we could somehow assign this in runtime even though.</div> <div class="comment-date">2012-09-19 23:12 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Compose object graphs with confidence https://blog.ploeh.dk/2011/03/04/Composeobjectgraphswithconfidence 2011-03-04T11:15:10+00:00 Mark Seemann <div id="post"> <p> The main principle behind the <a href="/2010/09/29/TheRegisterResolveReleasepattern">Register Resolve Release</a> pattern is that loosely coupled object graphs should be composed as <em>a single action</em> in the entry point of the application (the Composition Root). For request-based applications (web sites and services), we use a variation where we compose <em>once</em> per request. </p> <p> It seems to me that a lot of people are apprehensive when they first hear about this concept. It may sound reasonable from an architectural point of view, but isn't it horribly inefficient? A well-known example of such a concern is <a href="http://jeffreypalermo.com/">Jeffrey Palermo</a>'s blog post <a href="http://jeffreypalermo.com/blog/constructor-over-injection-anti-pattern/">Constructor over-injection anti-pattern</a>. Is it really a good idea to compose a complete object graph in one go? What if we don't need part of the graph, or only need it later? Doesn't it adversely affect response times? </p> <p> Normally it doesn't, and if it does, there are elegant ways to address the issue. </p> <p> In the rest of this blog post I will expand on this topic. To keep the discussion as simple as possible, I'll restrict my analysis to object trees instead of full graphs. This is quite a reasonable simplification as we should strive to avoid circular dependencies, but even in the case of full graphs the arguments and techniques put forward below hold. </p> <p> Consider a simple tree composed of classes from three different assemblies: </p> <p> <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Tree" border="0" alt="Tree" src="/content/binary/Windows-Live-Writer/Compose-object-graphs-with-confidence_921A/Tree_1.png" width="373" height="131"> </p> <p> All the A classes (blue) are defined in the A assembly, B classes (green) in the B assembly, and the C1 class (red) in the C assembly. In code we create the tree with Constructor Injection like this: </p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> t =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">A1</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">A2</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">B1</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">B2</font></span>()),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">A3</font></span>()),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">C1</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">B3</font></span>()));</font></pre> </p> <p> Given the tree above, we can now address the most common concerns about composing object trees in one go. </p> <h3 id="6a86dd3f2cec442fa04f138575c9b76f"> Will it be slow? <a href="#6a86dd3f2cec442fa04f138575c9b76f" title="permalink">#</a> </h3> <p> Most likely not. Keep in mind that <a href="/2011/03/03/InjectionConstructorsshouldbesimple">Injection Constructors should be very simple</a>, so not a lot of work is going on during composition. Obviously just creating new object instances takes a bit of time in itself, but we create objects instances all the time in .NET code, and it's often a very fast operation. </p> <p> Even when using DI Containers, which perform a lot of (necessary) extra work when creating objects, <a href="http://www.codinginstinct.com/2008/05/ioc-container-benchmark-rerevisted.html">we can create tens of thousand trees per second</a>. Creation of objects simply isn't that big a deal. </p> <h3 id="299d1c9c3740480aa4b03198f02cac96"> But still: what about assembly loading? <a href="#299d1c9c3740480aa4b03198f02cac96" title="permalink">#</a> </h3> <p> I glossed over an important point in the above argument. While object creation is fast, it sometimes takes a bit of time to load an assembly. The tree above uses classes from three different assemblies, so to create the tree all three assemblies must be loaded. </p> <p> In many cases that's a performance hit you'll have to take because <em>you need those classes anyway</em>, but sometimes you might be concerned with taking this performance hit too early. However, I make the claim that in the vast majority of cases, this concern is irrelevant. </p> <p> In this particular context there are two different types of applications: Request-based applications (web) and all the rest (desktop apps, daemons, batch-jobs, etc.). </p> <h3 id="56cbf96c0e8a4e49a192c5662cd5f32b"> Request-based applications <a href="#56cbf96c0e8a4e49a192c5662cd5f32b" title="permalink">#</a> </h3> <p> For request-based applications such as web sites and REST services, an object tree must be composed for each request. However, all requests are served by the same AppDomain, so once an assembly is loaded, it sticks around to be available for all subsequent requests. Thus, the first few requests will suffer a performance penalty from having to load all assemblies, but after that there will be no performance impact. </p> <p> In short, in request-based applications, you can compose object trees with confidence. In only extremely rare cases should you have performance issues from composing the entire tree in one go. </p> <h3 id="037643845b3a45ddaab49501a0613c28"> Long-running applications <a href="#037643845b3a45ddaab49501a0613c28" title="permalink">#</a> </h3> <p> For long-running applications the entire object tree must be composed at start-up. For background services such as daemons and batch processes the start-up time probably doesn't matter much, but for desktop applications it can be of great importance. </p> <p> In some cases the application <em>requires</em> the entire tree to be immediately available, in which case there's not a lot you can do. Still, once all assemblies have been loaded, actually creating the tree will be very fast. </p> <p> In other cases an entire branch of the tree may not be immediately required. As an example, if the C1 node in the above graph isn't needed right away, we could improve start-up time if we could somehow defer creating that branch, because this would also defer loading of the entire C assembly. </p> <h3 id="4e924afb63eb4583a88e3a5bb9206ec4"> Deferred branches <a href="#4e924afb63eb4583a88e3a5bb9206ec4" title="permalink">#</a> </h3> <p> Since object creation is fast, the only case where it makes sense to defer loading of a branch is when creation of that branch causes an assembly to be loaded. If we can defer creation of such a branch, we can also defer loading of the assembly, thus improving the time it takes to compose the initial tree. </p> <p> Imagine that we wish to defer creation of the C1 branch of the above tree. It will prevent the C assembly from being loaded because that assembly is not used in any other place in the tree. However, it will not prevent the B assembly from being loaded, since that assembly is also being used by the A2 node. </p> <p> Still, in those rare situations where it makes sense to defer creation of a branch, we can make that cut into a part of the infrastructure of the tree. <a href="/2010/01/20/RebuttalConstructorover-injectionanti-pattern">I originally described this technique</a> as a reaction to the above mentioned post by Jeffrey Palermo, but here's a restatement in the current context. </p> <p> We can defer creating the C1 node by wrapping it in a lazy implementation of the same interface. The C1 node implements an interface called ISolo&lt;IMarker&gt;, so we can wrap it in a Virtual Proxy that defers creation of C1 until it's needed: </p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">LazySoloMarker</font></span> : <span><font color="#2b91af">ISolo</font></span>&lt;<span><font color="#2b91af">IMarker</font></span>&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> <span><font color="#2b91af">Lazy</font></span>&lt;<span><font color="#2b91af">ISolo</font></span>&lt;<span><font color="#2b91af">IMarker</font></span>&gt;&gt; lazy;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> LazySoloMarker(<span><font color="#2b91af">Lazy</font></span>&lt;<span><font color="#2b91af">ISolo</font></span>&lt;<span><font color="#2b91af">IMarker</font></span>&gt;&gt; lazy)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (lazy == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ArgumentNullException</font></span>(<span><font color="#a31515">"lazy"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.lazy = lazy;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span><font color="#0000ff"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; #region</font></font></span><font style="font-size: 10pt"> ISolo&lt;IMarker&gt; Members</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#2b91af">IMarker</font></span> Item</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">get</font></span> { <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">this</font></span>.lazy.Value.Item; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span><font style="font-size: 10pt" color="#0000ff">&nbsp;&nbsp;&nbsp; #endregion</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p> This Virtual Proxy takes a Lazy&lt;ISolo&lt;IMarker&gt;&gt; as input and defers to it to implement the members of the interface. This only causes the Value property to be created when it's first accessed - which may be long after the LazySoloMarker instance was created. </p> <p> The tree can now be composed like this: </p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> t =</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">A1</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">A2</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">B1</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">B2</font></span>()),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">A3</font></span>()),</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">LazySoloMarker</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">Lazy</font></span>&lt;<span><font color="#2b91af">ISolo</font></span>&lt;<span><font color="#2b91af">IMarker</font></span>&gt;&gt;(() =&gt; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">C1</font></span>(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">B3</font></span>()))));</font></pre> </p> <p> This retains all the original behavior of the original tree, but defers creation of the C1 node until it's needed for the first time. </p> <p> The bottom line is this: you can compose the entire object graph with confidence. It's not going to be a performance bottleneck. </p> <p> <strong>Update</strong> (2013-08-19 08:09 UTC): For a more detailed treatment of this topic, watch my NDC 2013 talk <a href="https://vimeo.com/68378923">Big Object Graphs Up Front</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8b428d29ea384696a410fb6ae14091e1"> <div class="comment-author"><a href="http://marcelbradea.com/">Marcel Bradea</a> <a href="#8b428d29ea384696a410fb6ae14091e1">#</a></div> <div class="comment-content">Hi there, great article and way to focus on the inherent differences between request-based and long-running applications.<br> <br> The viewpoint however seems skewed towards request-based apps and I think really trivializes the innate lazy exploration nature of object graphs in rich-client apps (let's not call them desktop, as mobile is the exact same scenario - only difference is resources and processing power are a lot more limited and thus our architectures have to be better designed to account for it).<br> <br> The main premise here is that in a rich-client app the trees you mention above should ALWAYS be lazily loaded, since the &quot;branches&quot; of the component tree are usually screens or their various sub-components (a dialog in some sub-screen for example). The branches are also more like &quot;chains&quot; as screens can have multiple follow-on screens that slowly load more content as you dive in deeper (while content you have visited higher up the chain may get unloaded - &quot;lazy unloading&quot; shall we say?). You never want to load screens that are not yet visible at app startup. In a desktop, fully-local app this results in a chuggy-performing or needlessly resource-wasteful app; in mobile it is simply impossible within the memory constraints. And of course... a majority of rich-client screens are hydrated with data pulled from the Internet and that data depends on either user input or server state (think accessing a venue listing on Yelp - you need the user to say which venue they want to view and the venue data is stored and updated server-side in a crowd-sourced manner not locally available to the user). So even if you had unlimited client-side memory you still couldn't pre-load the whole component object graph for the application up front.<br> <br> I completely agree with your Composition Root article (/2011/07/28/CompositionRoot) and I think it describes things very beautifully. But what it also blissfully points out is that rich-client apps are poorly suited to a Composition Root and thus Dependency Injection approach.<br> <br> DI does lazy-loading very poorly (just look at the amount of wrapper/plumbing you have to write for one component above). Now picture doing that for every screen in a mobile app (ours has ~30 screens and it's quite a trivial, minimal app for our problem domain). That's not to even mention whether you actually WANT to pull all the lifetime management into a single component - what could easily be seen as a break in Cohesion for logic belonging to the various sub-components. In web this doesn't really matter as the lifetime is usually all or nothing - it's either a singleton/per-request or per-instance. The request provides the full scoping needed for most processing. But in rich-client the lifetimes need to be finely managed based on screen navigation, user action and caching or memory constraints. Sub-components need to get loaded in and out dynamically in a lot more complex a manner than singletons or per-user-session. Again pulling this fine-tuned logic into a shared, centralized component is highly questionable - even if it was easily doable, which it's not.<br> <br> I won't go into alternatives here (perhaps that's the subject of a post I should write up), but service location and manual instantiation ends up being a much preferable approach in these kinds of long-running application scenarios. Testability can be achieved in other, possibly simpler ways (http://unitbox.codeplex.com) if that's the driving concern.<br> <br> Thus I think the key driving differentiator comes down to object graph composition: are you able to feasibly and desirably load the whole thing at once (such as in web scenarios) or not? In rich-client apps (desktop and mobile) this is a striking NO. You need components to load sub-components at will and not be bound and dependent on a centralized component (Composite Root) to do so. The alternative is passing a dependency container around to every component in the system so that the components can resolve sub-components using the container - but we all know how major an anti-pattern that is (oh Android!...)<br> <br> Would love to see the community start differentiating between the scenarios where DI makes sense and where it ends up being an anti-productive burden. And I think rich client apps is evidently one of those places.</div> <div class="comment-date">2012-10-18 05:11 UTC</div> </div> <div class="comment" id="24a9b25005094af0b603be0ac10bf780"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#24a9b25005094af0b603be0ac10bf780">#</a></div> <div class="comment-content">Hi Marcel<br> <br> Thank you for your insightful comment. When it comes to the analysis of the needs of desktop and mobile applications, I completely agree that many nodes of the object graph would need to be lazy for exactly the reasons you so clearly explain.<br> <br> However, I don't agree with your conclusions. Yes, there's a bit of plumbing involved with defining a Virtual Proxy over an expensive dependency, but I don't think it's particularly problematic issue. There's a number of reasons for that:<br> <br> - First of all, let's consider your example of an application with 30 screens. Obviously, you don't want to load all 30 screens up-front, but that doesn't mean that you'll need to write 30 custom Virtual Proxies. Hopefully, you have a single abstraction that loads screens, so that's only a single Virtual Proxy you'll need to write.<br> <br> - As you point out, you'll also want to postpone loading of data for each screen. I completely agree, but you don't need a Service Locator for this. The most common approach for this is to inject a strongly typed Query service (think: Repository) into your Controllers (or whatever you use to load data). This would essentially be a stateless service object without much (if any) read-only state, so even in a mobile app, I doubt it would take up much resources. Even so, you can still lazy-load it if you need to - but do measure before jumping to conclusions.<br> <br> - In the end, you may need to proxy more than a single service, but if you find yourself in a situation where you need to proxy 30+ services, that's more likely to indicate a violation of the <a href="http://codemanship.co.uk/parlezuml/blog/?postid=934">Reused Abstractions Principle</a> than a failure of DI and the Composition Root pattern.<br> <br> - Finally, while it may seem like an overhead to create the plumbing code, it's likely to be very robust. Once you've created a Virtual Proxy for an interface, the only reason it has to change is if you change the interface itself. If you stick to <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a> that shouldn't happen very often. Thus, while you may be concerned that creating Virtual Proxies will require extra effort, it'll abstract away an application concern that will tend to be very robust and have a very low maintenance overhead. I consider this far superior to the brittle approach of a Service Locator. In the end, you'll spend less time maintaining that code than if you go for a Service Locator. It's a classic case of a bigger up-front investment that pays huge dividends over time - just like TDD.</div> <div class="comment-date">2012-10-23 00:36 UTC</div> </div> <div class="comment" id="ca63c2ea83774457b83deaad4f535975"> <div class="comment-author"><a href="http://techyourchance.com/">Vasiliy Zukanov</a> <a href="#ca63c2ea83774457b83deaad4f535975">#</a></div> <div class="comment-content"> Hi Mark, <br> Just found your blog while doing additional research for a conference talk I'm about to give. The content here is pure gold! I'll make sure to read all of it. <br> Your response to Marcel above is exactly what I was thinking of when I read his comment. I'm a professional Android developer and I confirm that the scenario described in Marcel's comment is best handled in a way you suggested. <br> I would like to know your expert opinion on this statement of mine: "When using DI, if a requirement for lazy loading of an injected service(s) arises, it should be treated as 'code smell'. Most probably, this requirement is due to a service(s) that does too much work upon construction. If this service can be refactored, then you should favor refactoring over lazy loading. If the service can't be refactored (e.g. part of the framework), then you should check whether the client can be refactored in a way that eliminates a need for lazy loading. Use lazy loading of injected services only as a last resort in order to compensate for an unfortunate design that you can't change". <br> Does the above statement makes sense in your opinion? </div> <div class="comment-date">2017-03-19 05:11 UTC</div> </div> <div class="comment" id="2f6df1187d0648fc8d827e96aba10073"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2f6df1187d0648fc8d827e96aba10073">#</a></div> <div class="comment-content"> <p> Vasiliy, thank you for writing. That statement sounds reasonable. FWIW, objects that do too much work during construction violate Nikola Malovic's 4th law of IoC. Unfortunately, the original links to Nikola Malovic laws of IoC no longer point at the original material, but I described the fourth law in an article called <a href="/2011/03/03/InjectionConstructorsshouldbesimple">Injection Constructors should be simple</a>. </p> <p> If you want to give some examples of possible refactorings, you may want to take a look at the <a href="/2014/08/24/decoraptor">Decoraptor pattern</a>. Additionally, if you're dealing with a third-party component, you can often create an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> that behaves nicely during construction. </p> </div> <div class="comment-date">2017-03-19 11:53 UTC</div> </div> <div class="comment" id="83e70b2cee46462e9212f604a5bf4357"> <div class="comment-author"><a href="http://techyourchance.com">Vasiliy Zukanov</a> <a href="#83e70b2cee46462e9212f604a5bf4357">#</a></div> <div class="comment-content"> <p> Mark, thank you for such a quick response and additional information and references! I would vote for Decoraptor to be included in the next edition of GOF's book. </p> <p> BTW, I think I found Nikola'a article that you mentioned <a href="https://vuscode.wordpress.com/2009/10/16/inversion-of-control-single-responsibility-principle-and-nikola-s-laws-of-dependency-injection/">here</a>. </p> </div> <div class="comment-date">2017-03-19 13:30 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Injection Constructors should be simple https://blog.ploeh.dk/2011/03/03/InjectionConstructorsshouldbesimple 2011-03-03T14:18:54+00:00 Mark Seemann <div id="post"> <p>The Constructor Injection design pattern is a extremely useful way to implement loose coupling. It's easy to understand and implement, but sometime perhaps a bit misunderstood.</p> <p>The pattern itself is easily described through an example:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">private</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">readonly</font></span> <span><font color="#2b91af">ISpecimenBuilder</font></span> builder;</font> <font style="font-size: 10pt">&nbsp;</font> <span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> SpecimenContext(<span><font color="#2b91af">ISpecimenBuilder</font></span> builder)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (builder == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ArgumentNullException</font></span>(<span><font color="#a31515">"builder"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.builder = builder;</font> <font style="font-size: 10pt">}</font></pre> </p> <p>The SpecimenContext constructor <em>statically declares</em> that it <em>requires</em> an ISpecimenBuilder instance as an argument. To guarantee that the builder field is an invariant of the class, the constructor contains a Guard Clause before it assigns the builder parameter to the builder field. This pattern can be repeated for each constructor argument.</p> <p>It's important to understand that when using Constructor Injection the constructor should contain no additional logic.</p> <blockquote> <p>An Injection Constructor should do no more than receiving the dependencies.</p></blockquote> <p>This is simply a rephrasing of <a href="https://vuscode.wordpress.com/">Nikola Malovic</a>'s <a href="https://vuscode.wordpress.com/2009/10/16/inversion-of-control-single-responsibility-principle-and-nikola-s-laws-of-dependency-injection">4th law of IoC</a>. There are several reasons for this rule of thumb:</p> <ul> <li>When we compose applications with Constructor Injection we often create substantial object graphs, and we want to be able to create these graphs as efficiently as possible. This is Nikola's original argument. <li>In the odd (and not recommended) cases where you have circular dependencies, the injected dependencies may not yet be fully initialized, so an attempt to invoke their members at that time may result in an exception. This issue is similar to the <a href="http://blogs.msdn.com/b/brada/archive/2004/08/12/213951.aspx">issue of invoking virtual members from the constructor</a>. Conceptually, an injected dependency is equivalent to a virtual member. <li>With Constructor Injection, the constructor's responsibility is to demand and receive the dependencies. Thus, according to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> (SRP), it should not try to do something else as well. Some readers might argue that I'm misusing the SRP here, but I think I'm simply applying the underlying principle in a more granular context.</li></ul> <p>There's no reason to feel constrained by this rule, as in any case the <a href="/2011/02/28/Interfacesareaccessmodifiers">constructor is an implementation detail</a>. In loosely coupled code, the constructor is not part of the overall application API. When we consider the API at that level, we are still free to design the API as we'd like.</p> <p>Please notice that this rule is contextual: it applies to Services that use Constructor Injection. Entities and Value Objects tend not to use DI, so their constructors are covered by other rules.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4ad84966d4d7486da39808176d23877b"> <div class="comment-author"><a href="http://www.google.com/accounts/o8/id?id=AItOawljkaHpZNjDRgKsw3Yo4TZnChUqrEzJxjU">www.google.com/accounts/o8/id?id=AItOawljkaHpZNjDRgKsw3Yo4TZnChUqrEzJxjU</a> <a href="#4ad84966d4d7486da39808176d23877b">#</a></div> <div class="comment-content">Nice post, sometimes I find useful to have an IInitializable interface and instruct the container to call the initialize method after instantiation. What you thing about this?</div> <div class="comment-date">2011-03-03 15:01 UTC</div> </div> <div class="comment" id="4ddc9bb23f224c9f9c9c0bee7b1520a4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4ddc9bb23f224c9f9c9c0bee7b1520a4">#</a></div> <div class="comment-content">That's very rarely a good idea. The problem with an Initialize method is the same as with Property Injection (A.K.A. Setter Injection): it creates a temporal coupling between the Initialize method and all other members of the class. Unless you truly can invoke any other member of the class <em>without</em> first invoking the Initialize method, such API design is deceitful and will lead to run-time exceptions. It also becomes much harder to ensure that the object is always in a consistent state.<br> <br> Constructor Injection is a far superior pattern because is enforces that required dependencies will be present. Property Injection on the other hand implies that the dependency is optional, which is rarely the case.</div> <div class="comment-date">2011-03-03 16:04 UTC</div> </div> <div class="comment" id="0eb47596d1854fa791db4ee4abd6c5ac"> <div class="comment-author"><a href="http://mindinthewater.blogspot.com">wcoenen</a> <a href="#0eb47596d1854fa791db4ee4abd6c5ac">#</a></div> <div class="comment-content">What about wiring events in the constructor? For example:<br> <br> this.foo = foo;<br> this.foo.SomeEvent += HandleSomeEvent;</div> <div class="comment-date">2011-03-03 19:28 UTC</div> </div> <div class="comment" id="c0e0dce9cf9946208126d9e45d470cf4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c0e0dce9cf9946208126d9e45d470cf4">#</a></div> <div class="comment-content">When you look at what happens on the IL level, subscribing to an event is just another method call, so the same arguments as above still apply.<br> <br> Keep in mind, however, that the above constitutes a guideline. It's not an absolute truth. I rarely use events, but it happens from time to time, and I can think of at least one case where I've done just what you suggest. I also occasionally break the above rule in other ways, but I always pause and consider the implications and whether there's a better alternative - often there is.</div> <div class="comment-date">2011-03-03 19:50 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Interfaces are access modifiers https://blog.ploeh.dk/2011/02/28/Interfacesareaccessmodifiers 2011-02-28T13:19:04+00:00 Mark Seemann <div id="post"> <p align="left">.NET developers should be familiar with the <a href="http://msdn.microsoft.com/en-us/library/ms173121.aspx">standard access modifiers</a> (public, protected, internal, private). However, in loosely coupled code we can regard interface implementations as a fifth access modifier. This concept was originally introduced to me by <a href="http://www.udidahan.com/?blog=true">Udi Dahan</a> the only time I've had the pleasure of meeting him. That was many years ago and while I didn't <a href="/ref/stranger-in-a-strange-land">grok</a> it back then, I've subsequently come to appreciate it quite a lot.</p> <p align="left">Although I can't take credit for the idea, I've never seen it described, and it really deserves to be.</p> <p align="left">The basic idea is simple:</p> <blockquote> <p>If a consumer respects the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> (LSP), the only visible members are those belonging to the interface. Thus, the interface represents a dimension of visibility.</p></blockquote> <p>As an example, consider this simple interface from <a href="http://autofixture.codeplex.com/">AutoFixture</a>:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">interface</font></span> </font><span><font style="font-size: 10pt" color="#2b91af">ISpecimenContext</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">object</font></span> Resolve(<span><font color="#0000ff">object</font></span> request);</font> <font style="font-size: 10pt">}</font></pre> </p> <p>A well-behaved consumer can only invoke the Resolve method even though an implementation may have additional public members:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">SpecimenContext</font></span> : </font><span><font style="font-size: 10pt" color="#2b91af">ISpecimenContext</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> <span><font color="#2b91af">ISpecimenBuilder</font></span> builder;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> SpecimenContext(<span><font color="#2b91af">ISpecimenBuilder</font></span> builder)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (builder == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ArgumentNullException</font></span>(<span><font color="#a31515">"builder"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.builder = builder;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#2b91af">ISpecimenBuilder</font></span> Builder</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">get</font></span> { <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">this</font></span>.builder; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span><font color="#0000ff"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; #region</font></font></span><font style="font-size: 10pt"> ISpecimenContext Members</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">object</font></span> Resolve(<span><font color="#0000ff">object</font></span> request)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">this</font></span>.Builder.Create(request, <span><font color="#0000ff">this</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span><font style="font-size: 10pt" color="#0000ff">&nbsp;&nbsp;&nbsp; #endregion</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>Even though the SpecimenContext class defines the Builder property, as well as a public constructor, any consumer respecting the LSP will only see the Resolve method.</p> <p>In fact, the Builder property on the SpecimenContext class mostly exists to support unit testing because I sometimes need to assert that a given instance of SpecimenContext contains the expected ISpecimenBuilder. This doesn't break encapsulation since the Builder is exposed as a read-only property, and it more importantly <em>doesn't pollute the API</em>.</p> <p>To support unit testing (and whichever other clients might be interested in the encapsulated ISpecimenBuilder) we have a public property that follows all framework design guidelines. However, it's essentially an implementation detail, so it's not visible via the ISpecimenContext interface.</p> <p>When writing loosely coupled code, I've increasingly begun to see the <em>interfaces as the real API</em>. Most other (even public) members are pure implementation details. If the members are public, I still demand that they follow the framework design guidelines, but I don't consider them parts of the API. It's a very important distinction.</p> <blockquote> <p>The interfaces define the bulk of an application's API. Most other types and members are implementation details.</p></blockquote> <p>An important corollary is that <em>constructors are implementation details too</em>, since they can never by part of any interfaces.</p> <p>In that sense we can regard interfaces as a fifth access modifier - perhaps even the most important one.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="27cbc83049004889a41614c8b7757833"> <div class="comment-author"><a href="http://fredrikmork.myopenid.com">fredrikmork.myopenid.com</a> <a href="#27cbc83049004889a41614c8b7757833">#</a></div> <div class="comment-content">Taking this a step further, I wonder if it would make sense to prefer explicit interface implementations, separating the interface from any additional functionality in the type in a way so that you could not invoke Resolve on a SpecimenContext directly, but only on objects that are typed as ISpecimenContext? I am not sure whether I like that idea or not, but it would help enforce the logical separation between the interface and its implementation. </div> <div class="comment-date">2011-02-28 14:12 UTC</div> </div> <div class="comment" id="8d94d4fd6d414ee5a4747193ea54dc0f"> <div class="comment-author"><a href="http://www.google.com/accounts/o8/id?id=AItOawn5zIGlnVXcpwc9cVh-wtSvZN7wLcRQW5E">www.google.com/accounts/o8/id?id=AItOawn5zIGlnVXcpwc9cVh-wtSvZN7wLcRQW5E</a> <a href="#8d94d4fd6d414ee5a4747193ea54dc0f">#</a></div> <div class="comment-content">Mark,<br> <br> Thanks for this post.<br> <br> I have to disagree. <br> <br> Any public type or member becomes a part of your API. Consumer code will possibly link to them, they will become a part of your legacy, and you will need to provide backward-compatibility to any part of the public API.<br> <br> If you don't want a type, constructor or method to be part of the API, you have to make this it internal.<br> <br> Anyone is free to expose its entire API as a set of interfaces (as in COM), but the only thing that makes a semantic part of the API is whether it is public or not. Making a constructor public and then telling &quot;oh you know, you can't use it in your code, it's an implementation detail&quot; breaks the fundamental principles of object-oriented programming. <br> <br> Interfaces exist to allow for late binding between the consumer and the provider of a set of semantics. Visibility exits to segregate contractual semantics from implementations details. These concepts are related but actually orthogonal: you can have internal and public interfaces too.<br> <br> Bottom line: what makes your public API is the 'public' keyword. <br> <br> <br> -gael<br> <br> <br> </div> <div class="comment-date">2011-02-28 16:32 UTC</div> </div> <div class="comment" id="d3cab3df7c8041f8ab1144caf0e502b2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d3cab3df7c8041f8ab1144caf0e502b2">#</a></div> <div class="comment-content">I must admit that I haven't fully thought through whether or not interfaces ought to be explicit or implicit, but my gut feeling is that implicit implementations (as shown in the example) are fine.<br> <br> The thing is: when I unit test the concrete implementations I often tend to declare the System Under Test (SUT) as the concrete type because I want to <em>exercise</em> an interface member, but <em>verify</em> against a concrete member. If I were to use an explicit implementation this would require a cast in each test. If there was an indisputable benefit to be derived from explicit implementations I wouldn't consider this a valid argument, but in the absence of such, I'd tend towards making unit testing as easy as possible.<br> <br> I think the argument that explicit interfaces help enforce the logical separation is valid, but not by a sufficiently high degree...</div> <div class="comment-date">2011-02-28 20:49 UTC</div> </div> <div class="comment" id="0b3c6f67d7d1456da2fdf470ccff096d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0b3c6f67d7d1456da2fdf470ccff096d">#</a></div> <div class="comment-content">Gael<br> <br> Thank you for your comment.<br> <br> Please note that the context of the blog post is <em>loosely coupled code</em>. When composing classes using Dependency Injection (DI), consumers will never see anything else than the interface members. Thus, the API from which we compose an <em>application</em> contains mainly the interfaces. These are the moving parts from which we can define interaction.<br> <br> I agree that if you consider <em>only a single concrete type at a time</em>, all public (and protected) members are part of the API of that type, but that's not what I'm talking about. In DI it's implicitly discouraged to invoke public constructors of any Services because once you do that, you <em>tightly couple</em> a consumer to a specific implementation.</div> <div class="comment-date">2011-02-28 20:57 UTC</div> </div> <div class="comment" id="cc15e7f8a3cb4dd09adc3a92de425d30"> <div class="comment-author"><a href="http://bartelink.com">Ruben Bartelink</a> <a href="#cc15e7f8a3cb4dd09adc3a92de425d30">#</a></div> <div class="comment-content">I see from AutoMapper and the FDG references you're using the ILikePrefixes convention. I've been around the houses with it and have read most of the 'debates' on SO about it, but I keep coming back to the Growing Object Oriented Software Guided by Tests sidebar that says I is an antipattern and agreeing with it, even in .NET.<br> <br> Any comment on the above? Ever tried going I-less?<br> <br> Does it have any influence/overlap with your thoughts in the [excellent food-for-thought] article?</div> <div class="comment-date">2011-03-01 01:15 UTC</div> </div> <div class="comment" id="81c01cc696e848d6bccd5bca6e8526f2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#81c01cc696e848d6bccd5bca6e8526f2">#</a></div> <div class="comment-content">Ruben<br> <br> Thank you for writing.<br> <br> In my opinion, the most important goal for coding conventions is to reduce friction when reading (and writing) code. Thus, I generally try to write Clean Code, but another important guide is the <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment">POLA</a>. When it comes to the debate around the Hungarian <em>I</em> in interface names, I think that the POLA weighs heavier than the strictly logical argument against it.<br> <br> However illogical it is, (close to) 10 years of convention causes us to expect the <em>I</em> to be there; when it's not, it causes unnecessary friction.</div> <div class="comment-date">2011-03-01 08:42 UTC</div> </div> <div class="comment" id="e95ef463761b4181959e153cbbb335fd"> <div class="comment-author">Harry Dev <a href="#e95ef463761b4181959e153cbbb335fd">#</a></div> <div class="comment-content">If the &quot;Builder&quot; property is only used for testing why not use the &quot;internal&quot; access modifier instead, and allow the testing assembly access to this using the &quot;[assembly: InternalsVisibleTo(&quot;XXX.Test&quot;)]&quot; attribute? <br> <br> If the &quot;Builder&quot; property really is an implementation detail there should be no reason to expose it. Although, as you say it does not pollute much since it is a readonly property.</div> <div class="comment-date">2011-03-02 09:51 UTC</div> </div> <div class="comment" id="3d1acfdceccc4a90ac276e5dfc108b96"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3d1acfdceccc4a90ac276e5dfc108b96">#</a></div> <div class="comment-content">First and foremost I consider the InternalsVisibleTo feature an abomination. We should only unit test the public members of our code.<br> <br> As I wrote in another answer on this page, the Builder property is most certainly part of the public API of the concrete SpecimenContext class. It doesn't pollute the <em>class</em> in any way because it's an integral part of what the SpecimenContext class does.<br> <br> There's no reason to expect that the Builder property is used <em>only</em> for testing. It's true that it was driven into existence by TDD, but it makes sense as part of the class' API and is available to other potential consumers. In the rare cases that a third-party consumer wants to use the SpecimenContext directly, it can access the Builder property as well. It wouldn't be able to do that if the property was internal.<br> <br> However, the Builder property in no way belongs on the interface because that would be a leaky abstraction, so while it doesn't pollute the class, it would pollute the interface.</div> <div class="comment-date">2011-03-02 10:35 UTC</div> </div> <div class="comment" id="9c07f09d67fb4f1193bcd6037af7a55f"> <div class="comment-author"><a href="http://www.programmers-unlimited.com">Dustin Davis</a> <a href="#9c07f09d67fb4f1193bcd6037af7a55f">#</a></div> <div class="comment-content">Ruben, I have to agree with Mark. The I prefix is a convention that is expected and I doubt anyone really considers it hungarian notation (even though it fits the definition).<br> <br> If you're providing an API and there is a method expecting 'SomeType' most people will attempt to instantiate an instance of SomeType at which point they will receive red squiggles and will then have to do some investigation to determine what's going on. Even if it's only a matter of 5 seconds to figure it out, you've violated POLA, caused the developer to become confused because he now has to solve yet another problem loses momentum. There are many other potential confusing scenarios that can arise from not clearly marking an interface as such.<br> <br> No one expects to see strings prefixed with 'str' but clearly identifying an interface is expected. There many &quot;rules&quot; that should be followed but with all rules, there are exceptions. Most of them are for the sake of developers. It takes me no time to see any type and recognize it as an interface which I already know I can do this or I can do that, because it's prefixed with an I. but unless i'm already familiar with a framework/API that does not use the I convention, I would need to spend time learning and trial/error. Waste time.<br> <br> If for no other reason, then do it because Microsoft uses the I convention in the .NET framework and that is what .NET developers expect, even if it is &quot;incorrect&quot;.</div> <div class="comment-date">2011-03-02 16:29 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Creating general populated lists with AutoFixture https://blog.ploeh.dk/2011/02/08/CreatinggeneralpopulatedlistswithAutoFixture 2011-02-08T14:53:16+00:00 Mark Seemann <div id="post"> <p>In <a href="/2011/02/07/CreatingspecificpopulatedlistswithAutoFixture">my previous post</a> I described how to customize a Fixture instance to populate lists with items instead of returning empty lists. While it's pretty easy to do so, the drawback is that you have to do it explicitly for every type you want to influence. In this post I will follow up by describing how to enable some general conventions that simply populates all collections that the Fixture resolves.</p> <blockquote> <p>This post describes <a href="http://autofixture.codeplex.com/workitem/4199">a feature</a> that will be available in <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.1. It's not available in AutoFixture 2.0, but is already available in the code repository. Thus, if you can't wait for AutoFixture 2.1 you can download the source and built it.</p></blockquote> <p>Instead of having to create multiple customizations for IEnumerable&lt;int&gt;, IList&lt;int&gt;, List&lt;int&gt;, IEnumerable&lt;string&gt;, IList&lt;string&gt;, etc. you can simply enable these general conventions as easy as this:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">Fixture</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .Customize(<span><font color="#0000ff">new</font></span> <span><font color="#2b91af">MultipleCustomization</font></span>());</font></pre> </p> <p>Notice that enabling conventions for populating sequences and lists with ‘many' items is an optional customization that you must explicitly add.</p> <blockquote> <p>This feature must be explicitly enabled. There are several reasons for that:</p> <ul> <li>It would be a breaking change if AutoFixture suddenly started to behave like this by default. <li>The MultipleCustomization targets not only concrete types such as List&lt;T&gt; and Collection&lt;T&gt;, but also interfaces such as IEnumerable&lt;T&gt;, IList&lt;T&gt; etc. Thus, if you also use <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer">AutoFixture as an Auto-Mocking container</a>, I wanted to provide the ability to define which customization takes precedence.</li></ul></blockquote> <p>With that simple customization enabled, all requested IEnumerable&lt;T&gt; are now populated. The following will give us a finite, but populated list of integers:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> integers = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span><font color="#2b91af">IEnumerable</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;();</font></pre> </p> <p>This will give us a populated List&lt;int&gt;:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> list = fixture.CreateAnonymous&lt;<span><font color="#2b91af">List</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;();</font></pre> </p> <p>This will give us a populated Collection&lt;int&gt;:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> collection = </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span><font color="#2b91af">Collection</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;();</font></pre> </p> <p>As implied above, it also handles common list interfaces, so this gives us a populated IList&lt;T&gt;:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> list = fixture.CreateAnonymous&lt;<span><font color="#2b91af">IList</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;();</font></pre> </p> <p>The exact number of ‘many' is as always <a href="/2009/05/11/AnonymousSequencesWithAutoFixture">determined by the Fixture's RepeatCount</a>.</p> <p>As this code is still (at the time of publishing) in preview, I would love to get feedback on this feature.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Creating specific populated lists with AutoFixture https://blog.ploeh.dk/2011/02/07/CreatingspecificpopulatedlistswithAutoFixture 2011-02-07T19:49:26+00:00 Mark Seemann <div id="post"> <p>How do you get <a href="http://autofixture.codeplex.com/">AutoFixture</a> to create populated lists or sequences of items? Recently I seem to have been getting this question a lot, and luckily it's quite easy to answer.</p> <p>Let's first look at the standard AutoFixture behavior and API.</p> <p>You can ask AutoFixture to create an anonymous List like this:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> list = fixture.CreateAnonymous&lt;<span><font color="#2b91af">List</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;();</font></pre> </p> <p>Seen from AutoFixture's point of view, List&lt;int&gt; is just a class like any other. It has a default constructor, so AutoFixture just uses that and returns an instance. You get back an instance, no exceptions are thrown, but the list is empty. What if you'd rather want a populated list?</p> <p>There are many ways to go about this. A simple, low-level solution is to populate the list after creation:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">fixture.AddManyTo(list);</font></pre> </p> <p>However, you may instead prefer getting a populated list right away. This is also possible, but before we look at how to get there, I'd like to point out a feature that surprisingly few users notice. You can <a href="/2009/05/11/AnonymousSequencesWithAutoFixture">create many anonymous specimens at once</a>:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> integers = fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;();</font></pre> </p> <p>Armed with this knowledge, as well as the knowledge of <a href="/2010/04/06/MappingtypeswithAutoFixture">how to map types</a>, we can now create this customization to map IEnumerable&lt;int&gt; to CreateMany&lt;int&gt;:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;());</font></pre> </p> <p>The Register method is really a generic method, but since we have type inference, we don't have to write it out. However, since CreateMany&lt;int&gt;() returns IEnumerable&lt;int&gt;, this is the type we register. Thus, every time we subsequently resolve IEnumerable&lt;int&gt;, we will get back a populated sequence.</p> <p>Getting back to the original List&lt;int&gt; example, we can now customize it to a populated list like this:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList());</font></pre> </p> <p>Because the ToList() extension method returns List&lt;T&gt;, this call registers List&lt;int&gt; so that we will get back a populated list of integers every time the fixture resolves List&lt;int&gt;.</p> <p>What about other collection types that don't have a nice LINQ extension method? Personally, I never use Collection&lt;T&gt;, but if you wanted, you could customize it like this:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register(() =&gt;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">Collection</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList()));</font></pre> </p> <p>Since Collection&lt;T&gt; has a constructor overload that take IList&lt;T&gt; we can customize the type to use this specific overload and populate it with ‘many' items.</p> <p>Finally, we can combine all this to map from collection interfaces to populated lists. As an example, we can map from IList&lt;int&gt; to a populated List&lt;int&gt; like this:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">fixture.Register&lt;<span><font color="#2b91af">IList</font></span>&lt;<span><font color="#0000ff">int</font></span>&gt;&gt;(() =&gt; </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span><font color="#0000ff">int</font></span>&gt;().ToList());</font></pre> </p> <p>When we use the Register method to map types we can no longer rely on type inference. Instead, we must explicitly register IList&lt;int&gt; against a delegate that creates a populated List&lt;int&gt;. Because List&lt;int&gt; implements IList&lt;int&gt; this compiles. Whenever this fixture instance resolves IList&lt;int&gt; it will create a populated List&lt;int&gt;.</p> <p>All of this describes what you can do with the strongly typed API available in AutoFixture 2.0. It's easy and very flexible, but the only important drawback is that it's not general. All of the customizations in this post specifically address lists and sequences of integers, but not lists of any other type. What if you would like to expand this sort of behavior to any List&lt;T&gt;, IEnumerable&lt;T&gt; etc?</p> <p>Stay tuned, because in <a href="/2011/02/08/CreatinggeneralpopulatedlistswithAutoFixture">the next post I will describe how to do that</a>.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The BCL already has a Maybe monad https://blog.ploeh.dk/2011/02/04/TheBCLalreadyhasaMaybemonad 2011-02-04T13:11:34+00:00 Mark Seemann <div id="post"> <p>During the last couple of weeks I've been very interested in using a Maybe monad with <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s Kernel code, but although many examples can be found on the internet, they remain samples. <a href="http://abdullin.com/journal/2009/10/6/zen-development-practices-c-maybe-monad.html">Rinat Abdullin</a> and <a href="http://weblogs.asp.net/zowens/archive/2009/09/04/maybe-monad-my-c-version.aspx">Zack Owens</a> both posted samples, but I particularly like <a href="http://mikehadlow.blogspot.com/">Mike Hadlow</a>'s s<a href="http://mikehadlow.blogspot.com/2011/01/monads-in-c1-introduction.html">eries about Monads in C#</a> because he also explains how to <a href="http://mikehadlow.blogspot.com/2011/01/monads-in-c-5-maybe.html">use LINQ with monads such as the Maybe monad</a>.</p> <p>As I really wanted a Maybe monad for AutoFixture, I first thought about simply implementing it directly in the AutoFixture source. However, I found it too arbitrary to put such a general purpose programming construct into a specific library such as AutoFixture. My next thought was to create a small open source project just for that single purpose, but then I though about the problem a bit more…</p> <blockquote> <p>The BCL sort of already has a Maybe monad - you just need to recognize it as such.</p></blockquote> <p>What is a Maybe monad really? If you really distill it, it's just a type that either contains a value, or doesn't contain a value. In other words, it's a type that represents a particular range: a set with either zero or one items. That's just a special case of a more general range or collection, and we already have LINQ covering those constructs.</p> <p>Here it is: the Maybe monad from the BLC (encapsulated in a nice extension method):</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">static</font></span> <span><font color="#0000ff">class</font></span> </font><span><font style="font-size: 10pt" color="#2b91af">LightweightMaybe</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">static</font></span> <span><font color="#2b91af">IEnumerable</font></span>&lt;T&gt; Maybe&lt;T&gt;(<span><font color="#0000ff">this</font></span> T value)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">new</font></span>[] { value };</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>Obviously, this method returns a Maybe with a value, but we can just as easily represent Nothing with an empty array.</p> <p>With my ‘new' Maybe monad, I can now write code like this (where <em>request</em> is a System.Object instance):</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">return</font></font></span><font style="font-size: 10pt"> (<span><font color="#0000ff">from</font></span> t <span><font color="#0000ff">in</font></span> request.Maybe().OfType&lt;<span><font color="#2b91af">Type</font></span>&gt;()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">let</font></span> typeArguments = t.GetGenericArguments()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">where</font></span> typeArguments.Length == 1</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; <span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">IList</font></span>&lt;&gt;)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; == t.GetGenericTypeDefinition()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">select</font></span> context.Resolve(<span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">List</font></span>&lt;&gt;)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .MakeGenericType(typeArguments)))</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .DefaultIfEmpty(<span><font color="#0000ff">new</font></span> <span><font color="#2b91af">NoSpecimen</font></span>(request))</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .SingleOrDefault();</font></pre> </p> <p>You may think that this looks dense, but before that the code looked like this:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> type = request <span><font color="#0000ff">as</font></span> <span><font color="#2b91af">Type</font></span>;</font> <span><font color="#0000ff"><font style="font-size: 10pt">if</font></font></span><font style="font-size: 10pt"> (type == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">}</font> <font style="font-size: 10pt">&nbsp;</font> <span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> typeArguments = type.GetGenericArguments();</font> <span><font color="#0000ff"><font style="font-size: 10pt">if</font></font></span><font style="font-size: 10pt"> (typeArguments.Length != 1)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">}</font> <font style="font-size: 10pt">&nbsp;</font> <span><font color="#0000ff"><font style="font-size: 10pt">if</font></font></span><font style="font-size: 10pt"> (<span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">IList</font></span>&lt;&gt;) != </font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; type.GetGenericTypeDefinition())</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">}</font> <font style="font-size: 10pt">&nbsp;</font> <span><font color="#0000ff"><font style="font-size: 10pt">return</font></font></span><font style="font-size: 10pt"> context.Resolve(<span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">List</font></span>&lt;&gt;)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .MakeGenericType(typeArguments));</font></pre> </p> <p>Notice that in this more traditional approach involving Guard Clauses, I have to construct a new NoSpecimen object in three different places, thus violating the DRY principle. I like not having all those if/return blocks in the code.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2b6db9e415b74e1da34a8ca90a34068a"> <div class="comment-author"><a href="http://me.yahoo.com/a/ZmvSsbByu_fMjB5PvE2ZIcJEtIEwInc-">me.yahoo.com/a/ZmvSsbByu_fMjB5PvE2ZIcJEtIEwInc-</a> <a href="#2b6db9e415b74e1da34a8ca90a34068a">#</a></div> <div class="comment-content">That's a very neat idea. Now I think of it, it's a pattern you see used a lot in Haskell too.<br> <br> In your first code blog, rather than returning:<br> <br> return new[]{value};<br> <br> It would be nicer to do this:<br> <br> return Enumerable.Single(value);<br> <br> :)<br> </div> <div class="comment-date">2011-02-04 20:38 UTC</div> </div> <div class="comment" id="3443ddfc90ae48b3a905f36b6fc9d081"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3443ddfc90ae48b3a905f36b6fc9d081">#</a></div> <div class="comment-content">Yes, I believe the concept of a Maybe monad originates from Haskell or a similar language (but I can't remember the specific details).<br> <br> Using Enumerable.Single(value) will not work because it takes an IEnumerable&lt;T&gt; and returns a T. We want the exact opposite: take a T and return IEnumerable&lt;T&gt;.</div> <div class="comment-date">2011-02-04 21:33 UTC</div> </div> <div class="comment" id="c6fe01e45c9a430b92e047b816fe84bd"> <div class="comment-author"><a href="http://bartelink.com">Ruben Bartelink</a> <a href="#c6fe01e45c9a430b92e047b816fe84bd">#</a></div> <div class="comment-content">Cant say I know it inside out have never used DefaultIfEmpty IRL, but should the .SingleOrDefault be a .Single() or a [0] ? (I'd much favor a .Single() to be honest)<br> <br> @other commenter: Enumerable.Repeat(value,1) does the trick you want. I sometimes cruft up a .One helper method, but I believe there's a more accepted name for it in the excellent RealWorldFunctionalProgramming in C# and F# book I dont have to hand (The one that makes Mark's head hurt :D)</div> <div class="comment-date">2011-02-04 23:29 UTC</div> </div> <div class="comment" id="e4c549adc7ce4ca4b353ab94f9f8efd8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e4c549adc7ce4ca4b353ab94f9f8efd8">#</a></div> <div class="comment-content">Yes, you are right - Single() is enough. My mistake :)<br> <br> It's true that there are many ways to create an IEnumerable with a single element.</div> <div class="comment-date">2011-02-05 08:06 UTC</div> </div> <div class="comment" id="d823a6acb4f74a96b6cc365418430122"> <div class="comment-author"><a href="http://net-junkie.myopenid.com">net-junkie.myopenid.com</a> <a href="#d823a6acb4f74a96b6cc365418430122">#</a></div> <div class="comment-content">I agree that this functional approach is DRY, but I still find it a bit hard to follow. Instead, why not simply refactor the imperative code to a DRY, more intend revealing (but still imperative) version. This is what I propose: <br> <br> var type = request as Type;<br> <br> bool requestIsAType = type != null;<br> bool withOneGenericArgument = requestIsAType &amp;&amp; type.GetGenericArguments().Length == 1;<br> bool isAGenericList = requestIsAType &amp;&amp; type.GetGenericTypeDefinition() == typeof(IList&lt;&gt;);<br> <br> if (requestIsAType &amp;&amp; isAGenericList &amp;&amp; withOneGenericArgument)<br> {<br> return context.Resolve(typeof(List&lt;&gt;).MakeGenericType(type.GetGenericArguments()));<br> }<br> else<br> {<br> return new NoSpecimen(request);<br> }<br> <br> Doesn’t this just read like a functional spec? “When the request is a Type of a generic list with one generic generic argument, than … otherwise …”.<br> <br> Cheers</div> <div class="comment-date">2011-02-05 09:07 UTC</div> </div> <div class="comment" id="747b1b272c8a435ca728c26e6e9a2439"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#747b1b272c8a435ca728c26e6e9a2439">#</a></div> <div class="comment-content">Did you test that code? I'm pretty sure it has defects.<br> <br> If you call GetGenericTypeDefinition() on a type which is not generic, an exception will be thrown. This could happen if, for instance, I were to invoke the method with request = typeof(object).<br> <br> If you want to play around with this, just pull the AutoFixture source and revert to revision 391 and try it out on the ListRelay class. It has pretty comprehensive test coverage.</div> <div class="comment-date">2011-02-05 09:31 UTC</div> </div> <div class="comment" id="faaf5c9eb88848d5b912f7134bc3d793"> <div class="comment-author"><a href="http://net-junkie.myopenid.com"></a> <a href="#faaf5c9eb88848d5b912f7134bc3d793">#</a></div> <div class="comment-content">Did I test that code? Of course not! ;-) Just trying to prove what a bit of refactoring can do :-)</div> <div class="comment-date">2011-02-05 22:50 UTC</div> </div> <div class="comment" id="a2a3fe8831ae4d35aea6c64ca61f19f3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a2a3fe8831ae4d35aea6c64ca61f19f3">#</a></div> <div class="comment-content">Yes, but the point is that it's those little things that end up making a more procedural refactoring less than readable. In any case, 'readability' of code is highly subjective so obviously YMMV.</div> <div class="comment-date">2011-02-06 09:35 UTC</div> </div> <div class="comment" id="78f814812e2b4a4d8295ad9fd1ee4230"> <div class="comment-author">3P <a href="#78f814812e2b4a4d8295ad9fd1ee4230">#</a></div> <div class="comment-content">For me the LINQ version is almost unreadable. If it makes me more then few seconds to understand the code I think that code is not finished. Putting sth in one line is not &quot;Clean Code&quot; I think.</div> <div class="comment-date">2011-04-03 19:03 UTC</div> </div> <div class="comment" id="fbe066f14db844a393fdb59ff02678d2"> <div class="comment-author"><a href="http://gsscoder.github.com">Giacomo Stelluti Scala</a> <a href="#fbe066f14db844a393fdb59ff02678d2">#</a></div> <div class="comment-content">I came late to comment due to tweets exchange with author. But I want add that treating <code>null</code> as something that should semantically avoided in code, it's not only a matter of readability but also a symptom of good design. Totally agree with Mark Seemann that kindly supplied also this <code>Maybe</code> monad <a href="https://github.com/ploeh/Booking/blob/master/BookingDomainModel/Maybe.cs">implementation</a>. Excellent work.</div> <div class="comment-date">2013-03-06 08:18 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Scalable doesn't mean fast https://blog.ploeh.dk/2011/01/24/Scalabledoesntmeanfast 2011-01-24T12:03:16+00:00 Mark Seemann <div id="post"> <p>Recently I spent a couple of days with <a href="http://twitter.com/#!/tjementum">Thomas Jespersen</a> who's working towards a launch of <a href="http://www.spiir.dk/">spiir.dk</a> - on Windows Azure. The reason I got to talk to him was to see if I could help with some performance issues he had with Azure Table Storage.</p> <p>The scenario is really simple: the application needs to load all of a user's bank transactions into memory to enable pretty advanced sorting and filtering. That sounds like a lot, but really isn't more than approximately 200 kB of data retrieved through a <em>single</em> query - so: there are no 1+N problems in play here, but even so it originally took more than two seconds. That's a bit long to wait before you can even start rendering a web page.</p> <p>By tweaking his partitioning strategy and using parallel queries, Thomas managed to bring down the data retrieval time to approximately one second. Although stress testing indicated that this duration was very stable, even under load, it is still too slow. So we met to see what could be done. </p> <p>Thomas had done a great job tweaking the query, so I couldn't really suggest some sort of secret API that would make it run significantly faster. Basically, we have to deal with Azure storage being based on REST and that there are a lot of things about run-time behavior we cannot control. Apart from designing a proper partitioning strategy, we can't add indexes to Azure Table Storage.</p> <p>It was time to take a different approach.</p> <p>As far as I can tell, Windows Azure is designed to be <em>very</em> scalable. However, just because scalability implies that you can handle an insane amount of work within acceptable time frames, it doesn't mean that you can <em>extrapolate</em> it to mean that under a light load, everything will be lightning fast. That's not the case at all.</p> <blockquote> <p>Scalability means that performance characteristics remain <em>stable</em> from light to heavy load.</p></blockquote> <p>Consequently this means that if performance is adequate under heavy load, it will also be adequate under a light load. Azure Storage is first and foremost designed to be scalable, and as a second priority, as fast as possible.</p> <p>As Thomas discovered, Azure Table Storage isn't particularly fast.</p> <p>It may be a masochistic side of me that I'm not otherwise aware of, but I actually appreciate that. It makes us reassess our most basic assumptions.</p> <p>The data that Thomas needs to read isn't particularly dynamic, so what if we take a snapshot of it? In short, we loaded all of a user's data into memory and serialized it to Azure Blob Storage.</p> <blockquote> <p>Loading the same data from a binary serialized Blob took only 1/6 of the time it did to load it from Table Storage.</p></blockquote> <p>As it turns out, Thomas doesn't even need all the columns from the Table to populate the view, so we could even make the serialized Blob smaller yet.</p> <p>At this point, however, we now have two representations of the same data: The original data in Table Storage, and a <em>persistent cache</em> in Blob Storage. The remaining challenge is to figure out how to keep these in sync.</p> <blockquote> <p>This may seem like a hack, but is really represents a paradigm shift. Letting go of ACID opens up a lot of new opportunities.</p></blockquote> <p>Actually, I spend most of the next day trying to convince Thomas that <a href="http://abdullin.com/cqrs">CQRS</a> would be the best approach, or that we could at least pick up some of the techniques from asynchronous, messaging based architectures, but that's another story.</p> <p>The morale here is that on Azure, things may be slower than you are used to, but storage is (relatively) cheap, so denormalization can save you a lot of execution time.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2f22d6a79a6b495792d1c238a24e5f16"> <div class="comment-author"><a href="http://intellect.dk/">Jakob Andersen</a> <a href="#2f22d6a79a6b495792d1c238a24e5f16">#</a></div> <div class="comment-content">I don't see how this is different in azure vs. other solutions. It is basic knowledge that scalability introduces overhead in most simple cases to ensure more consistent response times under load, often this overhead is caused by a distribution in place of centralization, in your case regarding distributed storage. Maybe im missing the point but you seem to be stating the obvious like &quot;water travels downhill&quot; :-)<br> <br> Your comment about you letting go of ACID strikes me as odd. Basically you are just adding a cache in front of your table storage like people have done for ages so where in lies the paradigm shift in &quot;letting go of ACID&quot;, are you speaking to people who have never cached data? Even people not using a cache will let go of ACID in most cases because they keep stale data in objects in their application and in best case do optimistic concurrency checks and worst case just let the last-to-write-win.</div> <div class="comment-date">2011-01-24 20:35 UTC</div> </div> <div class="comment" id="adce4309c0c8488e943fd9719367ff1d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#adce4309c0c8488e943fd9719367ff1d">#</a></div> <div class="comment-content">Jakob, if you already knew all of those things, then this blog post wasn't meant for you. I knew them too, but apparently many people don't, so I wrote for their benefit. Trust me: for many developers, this is far from obvious.<br> <br> The same goes for the ACID comment. People don't seem to have too big of a problem with in-memory caches because somehow they know that these can't possibly be consistent. However, as soon as you start writing to a persistent store, you encounter knee-jerk reactions that all persisted data must be written and updated within transactions.<br> <br> I'm not disagreeing with you. This is the basic premise behind CQRS and other scalable architectures. It's just not particularly widely known yet.</div> <div class="comment-date">2011-01-24 20:46 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. My Christmas challenge has a winner https://blog.ploeh.dk/2011/01/01/MyChristmaschallengehasawinner 2011-01-01T13:53:33+00:00 Mark Seemann <div id="post"> <p>A week ago I concluded Microsoft Denmark's 2010 .NET Community Christmas Calendar with a challenge about <a href="/2010/12/24/ChallengeResolveclosedtypeswithMEF">resolving closed types with MEF</a>. As 2011 came around, the deadline ended, so it's now time to pick the winner.</p> <p>I didn't get a lot of entries, which can be interpreted in at least one (or more) of the following ways:</p> <ul> <li>The challenge was too difficult <li>The challenge wasn't interesting <li>The prize wasn't attractive <li>People had other things to do during the holidays</li></ul> <p>Whatever the reason, it made my task of picking a winner that much easier. The best Danish entry came from <a href="http://danielvg.dk/">Daniel Volder Guarnieri</a> who cheated a bit by partially hard-coding the composition of Mayonnaise into the ContainerBuilder. As I wrote in the original challenge, there are many ways to tackle the challenge, and one was to take the unit tests very literally :)</p> <p>However, honorable mention must go to <a href="http://code.dortikum.net/">Boyan Mihaylov</a> who participated just for the honor. He took a more general approach similar to <a href="http://fluentmef.codeplex.com/">Fluent MEF</a>. This involves implementing a completely new <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.primitives.composablepartcatalog.aspx">ComposablePartCatalog</a> with associated <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.primitives.composablepartdefinition.aspx">ComposablePartDefinition</a> and <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.primitives.composablepart.aspx">ComposablePart</a> implementations - not a trivial undertaking.</p> <p>Kudos to Boyan and congratulations to Daniel. My thanks for your submissions, and a happy new year to all my readers!</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="490d8a6620794643a610223a36f67f9e"> <div class="comment-author"><a href="http://www.google.com/accounts/o8/id?id=AItOawlKgebHvDp71PcE_ZSlSZGWox7QW4xy2jE">www.google.com/accounts/o8/id?id=AItOawlKgebHvDp71PcE_ZSlSZGWox7QW4xy2jE</a> <a href="#490d8a6620794643a610223a36f67f9e">#</a></div> <div class="comment-content">Could you post the solution?</div> <div class="comment-date">2011-01-01 21:00 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Challenge: Resolve closed types with MEF https://blog.ploeh.dk/2010/12/24/ChallengeResolveclosedtypeswithMEF 2010-12-24T09:29:06+00:00 Mark Seemann <div id="post"> <p> For my international reader, a bit of context is in order for this post: Microsoft Denmark sponsors a series of Christmas challenges known as the Microsoft Christmas Calendar. Different bloggers alternate hosting a coding challenge for the day, and Microsoft sponsors the prizes. </p> <p> Today I have the honor of hosting the last challenge for 2010. Contestants from the Danish .NET community have the opportunity to win a <a href="http://www.thinkgeek.com/homeoffice/kitchen/c2b1/">Molecular Gastronomy Starter Kit</a> - if any of my international readers feel like joining in, they are welcome, but (probably) not eligible for the prize. </p> <h3 id="4bf4ec32ba9a4aac9a4c02efe313d879"> The challenge <a href="#4bf4ec32ba9a4aac9a4c02efe313d879" title="permalink">#</a> </h3> <p> The <a href="http://mef.codeplex.com/">Managed Extensibility Framework</a> (MEF) enables us to compose applications by annotating types and members with [Import] and [Export] attributes, but what can you do when you have types without these attributes and you can't add the attributes? </p> <p> For example, how can we compose Mayonnaise from these three classes? </p> <p> <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">sealed</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">EggYolk</span> { } &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">sealed</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">OliveOil</span> { } &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">sealed</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Mayonnaise</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: rgb(43, 145, 175);">EggYolk</span> eggYolk; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: rgb(43, 145, 175);">OliveOil</span> oil; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> Mayonnaise(<span style="color: rgb(43, 145, 175);">EggYolk</span> eggYolk, <span style="color: rgb(43, 145, 175);">OliveOil</span> oil) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (eggYolk == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ArgumentNullException</span>(<span style="color: rgb(163, 21, 21);">"eggYolk"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (oil == <span style="color: blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ArgumentNullException</span>(<span style="color: rgb(163, 21, 21);">"oil"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.eggYolk = eggYolk; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.oil = oil; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">EggYolk</span> EggYolk &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">this</span>.eggYolk; } &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">OliveOil</span> Oil &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">this</span>.oil; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The challenge is to come up with a good solution to that problem. Here are the formal rules: </p> <ul> <li> The unit test suite at the end of this post must pass. </li> <li> You are not allowed to edit the unit tests. </li> <li> You are only allowed to add <em>one</em> (1) <em>using</em> directive to the unit test file to reference the namespace of your proposed solution. </li> <li> You must work from the Visual Studio 2010 solution attached to this post. Add a new project that contains your solution. Send me the solution in a .zip file to enter the contest. </li> <li> You are allowed to implement your solution in <em>any language</em> you would like as long as it compiles and runs from Visual Studio 2010 Premium. </li> <li> The winner is chosen by my subjective judgment, but I will emphasize clean code and design. A tip: attempt to get as good scores as possible from Visual Studio's <em>Code Analysis</em> and <em>Code Metrics</em>. Good scores does not guarantee that you win, but bad scores will most likely ensure that you don't.</li> <li> Since many of you are on Christmas vacation the deadline is this year. As long as you submit a solution in 2010 (Danish time) you're a contestant. </li> </ul> <p> There are lots of different ways to skin this cat, so I'm looking forward to your submissions to see all your creative solutions. </p> <p> This unit test suite is the specification: </p> <p> <pre style="margin: 0px;"><span style="color: blue;">using</span> System.ComponentModel.Composition.Hosting; <span style="color: blue;">using</span> Ploeh.Samples.MeffyXmas.MenuModel; <span style="color: blue;">using</span> Xunit; &nbsp; <span style="color: blue;">namespace</span> Ploeh.Samples.MeffyXmas.MefMenu.UnitTest { &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">ContainerBuilderFacts</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> DefaultContainerCorrectlyResolvesOliveOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> oil = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">OliveOil</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotNull(oil); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> DefaultContainerCorrectlyResolvesEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> yolk = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">EggYolk</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotNull(yolk); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> DefaultContainerCorrectlyResolvesMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotNull(mayo); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> DefaultContainerReturnsSingletonMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.Same(mayo1, mayo2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> WithTransientMayonnaiseReturnTransientMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotSame(mayo1, mayo2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> TransientMayonnaiseByDefaultContainsSingletonEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.Same(mayo1.EggYolk, mayo2.EggYolk); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> TransientMayonnaiseByDefaultContainsSingletonOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.Same(mayo1.Oil, mayo2.Oil); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> TransientMayonnaiseCanHaveTransientEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotSame(mayo1.EggYolk, mayo2.EggYolk); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> TransientMayonnaiseCanHaveSingletonOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.Same(mayo1.Oil, mayo2.Oil); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> TransientMayonnaiseCanHaveTransientOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotSame(mayo1.Oil, mayo2.Oil); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> TransientMayonnaiseCanHaveSingletonEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.Same(mayo1.EggYolk, mayo2.EggYolk); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> PureTransientMayonnaiseIsTransient() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotSame(mayo1, mayo2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> PureTransientMayonnaiseHasTransientEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotSame(mayo1.EggYolk, mayo2.EggYolk); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175);">Fact</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> PureTransientMayonnaiseHasTransientOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">CompositionContainer</span> container = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ContainerBuilder</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedMayonnaise() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedEggYolk() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .WithNonSharedOil() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Build(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo1 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> mayo2 = container.GetExportedValue&lt;<span style="color: rgb(43, 145, 175);">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Assert</span>.NotSame(mayo1.Oil, mayo2.Oil); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> ContainerBuilder is the class you must implement, so the unit tests don't compile until you make them. Meffy xmas! </p> <p> <img style="background-image: none; border-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" title="clip_image002" alt="clip_image002" src="/content/binary/Windows-Live-Writer/ResolveclosedtypeswithMEF_BD03/clip_image002_3.jpg" border="0" height="182" width="640"> </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The TDD Apostate https://blog.ploeh.dk/2010/12/22/TheTDDApostate 2010-12-22T13:57:56+00:00 Mark Seemann <div id="post"> <p> I've been doing Test-Driven Development since 2003. I still do, I still love it, and I still expect to be doing it in the future. Over the years, I've repeatedly returned to the discussion of whether TDD should be regarded as Test-Driven <em>Development</em> or Test-Driven <em>Design</em>. For a long time I've been of the conviction that TDD is both of those. Not so any longer. </p> <blockquote> <p> TDD is not a good design methodology. </p> </blockquote> <p> Over the years I've written tons of code with TDD. I've written code where tests <em>blindly drove</em> the design, and I've written code where the design was the result of a long period of deliberation, and the tests were only the manifestations of already well-formed ideas. </p> <p> I can safely say that the code where tests alone drove the design never turned out particularly well. Although it was testable and, after a fashion, ‘loosely coupled', it was still Spaghetti Code in the sense that it lacked overall consistency and <a href="/2010/12/03/Towardsbetterabstractions">good abstractions</a>. </p> <p> On the other hand, I'm immensely pleased with code like <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0, which was mostly the result of hours of careful contemplation riding my bike to and from work. It was still written test-first, but the design was well thought out in advance. </p> <p> This made me think: did I just fail (repeatedly) at Test-Driven Design, or is the overall concept a fallacy? </p> <p> That's a pretty hard question to answer; what constitutes good design? In the following, let's assume that the SOLID principles is a pretty good indicator of good design. If so, does test-first drive us towards SOLID design? </p> <h3 id="ce0ea9b8c65a41288b91a7edb4723162"> TDD versus the Single Responsibility Principle <a href="#ce0ea9b8c65a41288b91a7edb4723162" title="permalink">#</a> </h3> <p> Does TDD ensure the application of the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> (SRP)? This question is easy to answer and the answer is a resounding <em>NO!</em> Nothing prevents us from test-driving a God Class. I've seen many examples, and I've been guilty of it myself. </p> <p> Constructor Injection is a much better help because it <a href="/2010/02/02/RefactoringtoAggregateServices">makes SRP violations so painful</a>. </p> <p> The score so far: 0 points to TDD. </p> <h3 id="83d0e40aae54465c8a6cfd8f2192ce65"> TDD versus the Open/Closed Principle <a href="#83d0e40aae54465c8a6cfd8f2192ce65" title="permalink">#</a> </h3> <p> Does TDD ensure that we follow the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a> (OCP)? This is a bit harder to answer. I've previously argued that <a href="/2009/06/05/TestabilityIsReallyTheOpenClosedPrinciple">Testability is just another name for OCP</a>, so that would in itself imply that TDD drives OCP. However, the issue is more complex than that, because there are several different ways we can address the OCP: </p> <ul> <li>Inheritance</li> <li>Composition</li> </ul> <p> According to <a href="http://www.osherove.com/">Roy Osherove</a>'s book <a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_91">The Art of Unit Testing</a>, the <em>Extract and Override </em>technique is a common unit testing trick. Personally, I rarely use it, but if used it will indirectly drive us a bit towards OCP via inheritance. </p> <p> However, we all know that we should <em>favor composition over inheritance</em>, so does TDD drive us in that direction? As I alluded to previously, TDD does tend to drive us towards the use of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>, which we can view as one way to achieve OCP via composition. </p> <p> However, another favorite composition technique of mine is to <a href="/2010/04/07/DependencyInjectionisLooseCoupling">add functionality with a Decorator</a>. This is only possible if the original type implements an interface that can be decorated. It's possible to write a test that forces a <a href="http://xunitpatterns.com/SUT.html">SUT</a> to implement an interface, but TDD as a technique in itself does not drive us in that direction. </p> <p> Grudgingly, however, I must admit that TDD still scores half a point against OCP, for a total score so far of ½ point. </p> <h3 id="32bda8edf3ee4001bffccfb4ef6f9559"> TDD versus the Liskov Substitution Principle <a href="#32bda8edf3ee4001bffccfb4ef6f9559" title="permalink">#</a> </h3> <p> Does TDD drive us towards adhering to the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Princple</a> (LSP)? Perhaps, but probably not. </p> <p> Black box testing can't protect us against the SUT attempting to downcast its dependencies, but at least it doesn't particularly pull us in that direction either. When it comes to the SUT's treatment of a dependency, TDD pulls in neither direction. </p> <p> Can we test-drive interface implementations that inadvertently violate the LSP? Yes, easily. As I discussed in a previous post, the use of <a href="http://martinfowler.com/bliki/HeaderInterface.html">Header Interfaces</a> pulls us towards LSP violations. <a href="/2010/12/02/Interfacesarenotabstractions">The more members an interface has, the more likely are LSP violations</a>. </p> <p> TDD can definitely drive us towards Header Interfaces (although they tend to hurt in the long run). I've seen this happen numerous times, and I've been there myself. TDD doesn't properly encourage LSP adherence. </p> <p> The score this round: 0 points for TDD, for a running total of ½ point. </p> <h3 id="98d5376b27b44ae48403c34d8cabaa89"> TDD versus the Interface Segregation Principle <a href="#98d5376b27b44ae48403c34d8cabaa89" title="permalink">#</a> </h3> <p> Does TDD drive us towards the <a href="http://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> (ISP)? No. It's pretty easy to test-drive a SUT towards a Header Interface, just as we can test-drive towards a God Class. </p> <p> Another 0 points for TDD. The score is still ½ point to TDD. </p> <h3 id="d7df5f96cd1041628a385797f44b61fa"> TDD versus the Dependency Inversion Principle <a href="#d7df5f96cd1041628a385797f44b61fa" title="permalink">#</a> </h3> <p> Does TDD drive us towards the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> (DIP)? Yes, it does. </p> <p> The whole drive towards Testability - the ability to replace dependencies with Test Doubles - drives us exactly in the same direction as the DIP. </p> <p> Since we tend to mistake such mechanistic loose coupling with proper application design, this probably explains why we, for so long, have confused TDD with good design. However, although I view loose coupling as a prerequisite for good design, <a href="/2010/12/02/Interfacesarenotabstractions">it is by no means enough</a>. </p> <p> For those that still keep score, TDD scores 1 point against DIP, for a total of 1½ points. </p> <h3 id="de3fefa94602414eb59a89e43e221b63"> TDD does not ensure SOLID <a href="#de3fefa94602414eb59a89e43e221b63" title="permalink">#</a> </h3> <p> With 1½ out of 5 possible points I have stated my case. I am convinced that TDD itself does not drive us towards SOLID design. It's definitely possible to <em>use</em> test-first techniques to drive towards SOLID designs, but that will always be an extra effort that <em>supplements</em> TDD; it's not something that is inherently built into TDD. </p> <p> Obviously you could argue that SOLID in itself is not the end-all, be-all of proper API design. I would agree. However, based on my experience with TDD, I think the conclusion holds. TDD does not drive us towards good design. It is not a design technique. </p> <p> I still write code test-first because I find it more <em>productive</em>, but I make design decisions out of band. I'm a Test-Driven Design Apostate. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4106d2e12b464627a2dc29f5afec1e82"> <div class="comment-author"><a href="http://kevin-berridge.blogspot.com">Kevin Berridge</a> <a href="#4106d2e12b464627a2dc29f5afec1e82">#</a></div> <div class="comment-content">I think I agree with you. TDD has lots of benefits. But using TDD and only TDD to drive your design is silly. The application of the SOLID principles, being proactive about finding and correcting code smells, and keeping your &quot;domain logic&quot; explicit are much more effective methods for encouraging good design.<br> <br> I think some of TDD's primary benefits are:<br> - it raises the quality of your features (less bugs, simpler, more thought out)<br> - it helps support you as you refactor to improve your design<br> - it helps people work on existing code<br> <br> Thanks for the post, very thought provoking!<br> Kevin</div> <div class="comment-date">2010-12-22 22:07 UTC</div> </div> <div class="comment" id="e7e027e24b4a46be86ee0fea8d9d52fc"> <div class="comment-author">keith ray <a href="#e7e027e24b4a46be86ee0fea8d9d52fc">#</a></div> <div class="comment-content">Refactoring is a necessary step in TDD. Recognizing code smells, which are violations of SOLID principles (and other good design principles), and fixing them while the tests are green.<br> </div> <div class="comment-date">2010-12-23 00:55 UTC</div> </div> <div class="comment" id="fa2b675b370044a8bd2a0d63f847a846"> <div class="comment-author"><a href="https://blogs/msdn.com/elee">Eric Lee</a> <a href="#fa2b675b370044a8bd2a0d63f847a846">#</a></div> <div class="comment-content">I do think that TDD usually helps drive toward SRP. I suppose that if you have two completely orthogonal responsibilities in a class then that fact might not show up in your tests. But usually I find that I create classes that have two responsibilities that interact in some way and when I write tests to demonstrate how all of those responsibilities should work together I quickly end up with combinatorial explosions which make the tests extremely painful, i.e. when A is true and B is greater than 10 and C is not null and D contains E, then Foo should happen. When I see tests like that I know immediately that I've committed an SRP violation somewhere and I need to go pull some responsibilities out into separate classes, test those responsibilities in isolation, and then build a coordinator that consumes those abstracted components.<br> <br> I'd give at least half a point for helping with SRP, and probably a full point. Nevertheless, I'd agree with your larger idea that TDD isn't a license to turn your brain off. The TDD system is read-green-REFACTOR, and the refactor part means you need to engage your brain and apply design princples that will make your code stronger. The tests allow you to do that refactoring in relative safety.</div> <div class="comment-date">2010-12-23 02:18 UTC</div> </div> <div class="comment" id="dce98493ab894545ae847d162a1cb7aa"> <div class="comment-author">Kelly Anderson <a href="#dce98493ab894545ae847d162a1cb7aa">#</a></div> <div class="comment-content">TDD doesn't mean you can turn your brain off. When you're on your bicycle, you come up with ideas that can lead to refactoring your code towards a better design. Refactoring with tests is far superior to refactoring without tests. The biggest difficulty in my experience is when a major refactoring requires the changing of code and test at the same time. I always try to avoid that, but sometimes it's just inevitable. Lots of revision control submissions when doing that... <br> <br> The Design part of TDD is, imho all about designing your low level APIs. And they are designed for ease of use automatically because when you are writing your tests, you think, &quot;What's the easiest way to invoke the functionality I'm contemplating?&quot; As you have pointed out, ease of use is only one component of good design, so TDD doesn't design the code for you ALL BY ITSELF. <br> <br> Knowing the limitations of the methodology you are employing is critical to getting the most out of it. While you can ride your bicycle to New York, there are few situations where that is the most practical way of getting there.<br> <br> -Kelly</div> <div class="comment-date">2010-12-23 15:05 UTC</div> </div> <div class="comment" id="2be344bfec124d71bf474bfe981fb3fc"> <div class="comment-author"><a href="http://maxblog.bomzhi.de">Maxim Zaks</a> <a href="#2be344bfec124d71bf474bfe981fb3fc">#</a></div> <div class="comment-content">What is your opinion on this article?<br> http://cleancoder.posterous.com/the-transformation-priority-premise</div> <div class="comment-date">2010-12-23 15:14 UTC</div> </div> <div class="comment" id="f1ebbff5dbbc4971909b9c1498486d3f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f1ebbff5dbbc4971909b9c1498486d3f">#</a></div> <div class="comment-content">That article seems reasonable (and interesting), but I don't see it as being adverse to what I wrote. Uncle Bob writes about how you evolve an <em>implementation</em> of an API as an interaction between progressively complex tests and the code itself. As far as I can tell, at no point during that blog post does he <em>change</em> the API. In other words, the API design is given a priori.<br> <br> What I'm discussing here is whether or not you can use tests to <em>blindly</em> design APIs. That's a different perspective.</div> <div class="comment-date">2010-12-23 15:28 UTC</div> </div> <div class="comment" id="0f3b93976d484ede957bd5a3621c1b46"> <div class="comment-author"><a href="http://grumpyop.wordpress.com">Mike Woodhouse</a> <a href="#0f3b93976d484ede957bd5a3621c1b46">#</a></div> <div class="comment-content">&quot;What I'm discussing here is whether or not you can use tests to blindly design APIs&quot;<br> <br> Are there authoritative sources that assert that you can? Or non-authoritative ones? If so, could you cite them, which would help to put the piece into context. Otherwise, I'm just hearing &quot;red-green on its own doesn't produce good design&quot;, which I thought was pretty much obvious, given that the &quot;refactor&quot; step is where I've always expected the design part to happen, and the article boils down to &quot;doing it wrong doesn't work&quot;.<br> <br> ;-)</div> <div class="comment-date">2010-12-23 16:11 UTC</div> </div> <div class="comment" id="c74b4d1c7afa470c9c0660ff31d174a6"> <div class="comment-author">Marty Nelson <a href="#c74b4d1c7afa470c9c0660ff31d174a6">#</a></div> <div class="comment-content">IMO, SOLID is more like good construction (or craft) practices than design (maybe the ambiguity of &quot;design&quot; is part of the problem). TDD is driving the design as architecture: what are the needs, metaphors, principles, etc. driving the software. Just as a house may be brilliantly designed and meet the purpose of its occupants, we still need solid construction of the structure itself or the longevity is at risk.<br> <br> TDD /drives/ through priority and constraint. Good tests fix intent, not implementation, while only allowing implementations to emerge that meet the intent. By definition then, it should not be coupled to elements of construction (or design as you are calling it).<br> </div> <div class="comment-date">2010-12-23 18:00 UTC</div> </div> <div class="comment" id="1cdd4bb68df94e8b8f128018dc4719a4"> <div class="comment-author"><a href="http://cockneycoder.wordpress.com/">Isaac Abrahan</a> <a href="#1cdd4bb68df94e8b8f128018dc4719a4">#</a></div> <div class="comment-content">I found your post interesting and well written. Not sure I entirely agree though - it sounds as though &quot;blindly&quot; allowing TDD to shape your design means that you skip out the &quot;refactor&quot; part of the TDD cycle. This is no different to not doing refactoring whether using TDD or not, and therefore it's most likely that your code will not closely follow SOLID.<br> <br> I appreciate the point that you're making i.e. TDD != a SOLID design, but then I don't think it ever aspired to - that's why there was the &quot;refactor&quot; bit after getting your tests green.<br> <br> </div> <div class="comment-date">2010-12-24 14:10 UTC</div> </div> <div class="comment" id="f112479518774fb285fc114a36d9bbb6"> <div class="comment-author">Kevin Stevens <a href="#f112479518774fb285fc114a36d9bbb6">#</a></div> <div class="comment-content">I think the post falls for one of the biggest fallacies about TDD: that it makes a programmer a better designer of code. Nothing could be further from the truth. If you suck at design before using TDD, you will still suck once you adopt TDD as a practice. <br> <br> In short, in order to be good at design, learn to design. Then TDD will help you get to a good design faster.</div> <div class="comment-date">2010-12-24 23:50 UTC</div> </div> <div class="comment" id="29d0d1c6478545788e93b9121aa5a54b"> <div class="comment-author"><a href="http://ats.jobsket.com">Martin</a> <a href="#29d0d1c6478545788e93b9121aa5a54b">#</a></div> <div class="comment-content">I agree with you and most of the comments. The problem here is when someone tries to turn a development technique into a whole methodology. TDD is just a technique and I don't think it ever intended to be further than that. <br> <br> At the same time there I think it is clear that TDD can also drive the design of your class/program. But there is a quite big difference into turning that original &quot;can&quot; into a &quot;should&quot; or a &quot;must&quot;. And I don't know why there is so many people obssesed in change the original intention of TDD. <br> <br> Isaac for example in one of the commens makes a great point about refactoring and its impact of TDD. And that's the point of that &quot;can&quot;. But this doesn't mean that you should only rely on TDD to design your project. To me is a great technique that helps 1. to force your team to make tests, 2. to force your team to think on the stuff they are solving and 3. to pop up design flaws that we never thought of. <br> <br> </div> <div class="comment-date">2010-12-25 08:33 UTC</div> </div> <div class="comment" id="31bf45bd21db49b592d7752ea1ee27d7"> <div class="comment-author"><a href="http://twitter.com/BlackTigerX">Eber Irigoyen</a> <a href="#31bf45bd21db49b592d7752ea1ee27d7">#</a></div> <div class="comment-content">now we're getting somewhere xD, I don't do test first, my approach is more like &quot;Inteface based programming&quot;, I like to think the design through first using only interfaces, then start implementing, then writing the tests to merely check on the correctness, so far, that works for me really well</div> <div class="comment-date">2010-12-26 02:17 UTC</div> </div> <div class="comment" id="e8cb20d020504bdb83c6fdcd6ce16bb1"> <div class="comment-author"><a href="http://www.cauthon.com">Darren</a> <a href="#e8cb20d020504bdb83c6fdcd6ce16bb1">#</a></div> <div class="comment-content">You wrote:<br> <br> &quot;I’ve written code where tests blindly drove the design...&quot;<br> <br> I don't think this is true because it's not possible. Tests can't &quot;blindly&quot; drive any design, especially considering the fact that tests don't write themselves. It takes a human being, a programmer with ideas and plans for the software, to decide what tests to write and how to implement them. <br> <br> Now, there is a context in which a phrase like &quot;blindly drive&quot; is valid, and it's the TDD method. No matter how great or valid your ideas for your software might be, TDD demands that you prove the worth of your ideas one tiny step at a time. You write a simple test, then you implement it in the most simple manner. Then you repeat, then you repeat, and eventually you're left with software that may or may not match with what you had in your head. <br> <br> The method is &quot;blind&quot; to your ideas in that your implementation is focused on one tiny requirement, but it can't be blind to your ideas completely. What gave you the idea to write the test in the first place?<br> <br> When programmers start TDD for the first time, their software doesn't magically become pure examples of the SOLID principles. It takes a lot of practice to know what tests/questions to write, where to start the TDD practice, and how generally to keep things together. And even with lots of experience, it's still possible to mess things up. If I mess up during my practice of TDD, it's not fair to blame the practice of TDD any more than it's fair to blame an automobile manufacturer if someone drives their car off the road.<br> <br> I think that's kinda what you're doing when you say things like:<br> <br> &quot;Nothing prevents us from test-driving a God Class.&quot; <br> <br> Nothing prevents us from test-driving a God class? How about the fact that the tests will be hard to write, be unmaintainable, and will generally smell? Whenever a class takes on additional responsibilities, the tests for those responsibilities have to be &quot;mixed&quot; with the other tests. That fact that the programmer is going test-first will provide him with the earliest clue that the class is taking on too much, will cause him to *design* a way around the SRP violation by using a separate class. <br> <br> If TDD helps to provide so much evidence that a SRP violation is occurring, why go after it because it doesn't *force* the programmer to act based on that evidence?<br> <br> Making up a series of TDD &quot;versus&quot; the SOLID principles seems a little far-fetched to me. TDD isn't meant to be a replacement for the human brain. </div> <div class="comment-date">2010-12-26 22:50 UTC</div> </div> <div class="comment" id="dc98562cc2fe4d239ed0d79505c2feaf"> <div class="comment-author"><a href="http://blog.techniquesofdesign.com/">David Bernstein</a> <a href="#dc98562cc2fe4d239ed0d79505c2feaf">#</a></div> <div class="comment-content">Your post is very interesting and so are the comments from your readers. I’ve had a number of friends send me links to this post so I thought I’d address some of your points with more details but my feedback has grown too big to fit in a comment so I wrote a blog post on it. Please see my blog post at http://techniquesofdesign.com/2011/01/12/the-tdd-zealot/ and feel free to comment.<br> <br> I agree with many of the things you say in your post. Thank you for inspiring such fruitful conversations. <br> </div> <div class="comment-date">2011-01-13 06:23 UTC</div> </div> <div class="comment" id="267de08bd56b4c91a14220976dd93369"> <div class="comment-author">Philip Schwarz <a href="#267de08bd56b4c91a14220976dd93369">#</a></div> <div class="comment-content">Great post...what do you make of this http://groups.google.com/group/growing-object-oriented-software/browse_frm/thread/e0a41018c356c221</div> <div class="comment-date">2011-05-04 21:39 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. On Role Interfaces, the Reused Abstractions Principle and Service Locators https://blog.ploeh.dk/2010/12/18/OnRoleInterfaces,theReusedAbstractionsPrincipleandServiceLocators 2010-12-18T14:21:17+00:00 Mark Seemann <div id="post"> <p>As a comment to my previous post about <a href="/2010/12/02/Interfacesarenotabstractions">interfaces being no guarantee for abstractions</a>, Danny asks some interesting questions. In particular, his questions relate to <a href="http://www.udidahan.com/">Udi Dahan</a>'s presentation <a href="http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan">Intentions &amp; Interfaces: Making patterns concrete</a> (also known as <em>Making Roles Explicit</em>). Danny writes:</p> <blockquote> <p>it would seem that Udi recommends creating interfaces for each "role" the domain object plays and using a Service Locator to find the concrete implementation ... or in his case the concrete FetchingStrategy used to pull data back from his ORM. This sounds like his application would have many 1:1 abstractions.</p></blockquote> <p>Can this be true, or can we consolidate <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a> with the <a href="http://parlezuml.com/blog/?postid=934">Reused Abstractions Principle (RAP)</a> - preferably without resorting to a <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator</a>? Yes, of course we can.</p> <p>In Udi Dahan's talks, we see various examples where he queries a Service Locator for a Role Interface. If the Service Locator returns an instance he uses it; otherwise, he falls back to some sort of default behavior. Here is my interpretation of Udi Dahan's slides:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">void</font></span> Persist(<span><font color="#2b91af">Customer</font></span> entity)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> validator = <span><font color="#0000ff">this</font></span>.serviceLocator</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Get&lt;<span><font color="#2b91af">IValidator</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt;&gt;();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (validator != <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; validator.Validate(entity);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span><font style="font-size: 10pt" color="#008000">// Save entity in actual store</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>This is actually not very pretty object-oriented code, but I have Udi Dahan suspected of choosing this implementation to better communicate the essence of how to use Role Interfaces. However, a more proper implementation would have a default (or Null Object) implementation of the Role Interface, and then the special implementation.</p> <p>If we assume that a NullValidator exists, we can require that the Service Locator can always serve up a proper instance of IValidator&lt;Customer&gt;. This enables us to simplify the Persist method to something like this:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">void</font></span> Persist(<span><font color="#2b91af">Customer</font></span> entity)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> validator = <span><font color="#0000ff">this</font></span>.serviceLocator</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Get&lt;<span><font color="#2b91af">IValidator</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt;&gt;();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; validator.Validate(entity);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span><font style="font-size: 10pt" color="#008000">// Save entity in actual store</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>Either the Service Locator returns a specialized CustomerValidator, or it returns the NullValidator. In any case, this assumption enables us to leverage the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> and refactor the conditional logic to polymorphism.</p> <p>In other words: every single time we discover the need to extract a Role Interface, we should end up with <em>at least</em> two implementations: the Null Object and the <a href="http://martinfowler.com/eaaCatalog/specialCase.html">Special Case</a>. Thus the RAP is satisfied.</p> <p>As a last refactoring, we can also get rid of the Service Locator. Instead, we can use Constructor Injection to inject IValidator&lt;Customer&gt; directly into the Persistence class:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">CustomerPersistence</font></span> </font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> <span><font color="#2b91af">IValidator</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt; validator;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> CustomerPersistence(<span><font color="#2b91af">IValidator</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt; v)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (v == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ArgumentNullException</font></span>(<span><font color="#a31515">"..."</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.validator = v;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">void</font></span> Persist(<span><font color="#2b91af">Customer</font></span> entity)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.validator.Validate(entity);</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><span><font style="font-size: 10pt" color="#008000">// Save entity in actual store</font></span> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p>Thus, the use of Role Interfaces in no way hinges on using a Service Locator, and everything is good again :)</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="62cc5dc8f38a49aca264c61f590ae1f2"> <div class="comment-author"><a href="http://glasscasket.myopenid.com">Bill</a> <a href="#62cc5dc8f38a49aca264c61f590ae1f2">#</a></div> <div class="comment-content">Great article.<br> <br> However, I'm sure there's a good answer for this but wouldn't you run into the same issue when it came time to use the CustomerPersistence class? How would you instantiate CustomerPersistence with it's dependencies without relying on a service locator?<br> <br> Thank you.</div> <div class="comment-date">2011-02-10 00:50 UTC</div> </div> <div class="comment" id="dd1481ca99dd413fb5e3ebe261940169"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dd1481ca99dd413fb5e3ebe261940169">#</a></div> <div class="comment-content">Use Constructor Injection all the way and defer composition until the application's entry point. In <a href="http://amzn.to/12p90MG">my book</a> I call this the <b>Composition Root</b>.</div> <div class="comment-date">2011-02-10 06:04 UTC</div> </div> <div class="comment" id="49623a57284f4fdbb719879b96abb2f4"> <div class="comment-author"><a href="http://glasscasket.myopenid.com">Bill</a> <a href="#49623a57284f4fdbb719879b96abb2f4">#</a></div> <div class="comment-content">Thanks, Mark. I just bought the book. :D</div> <div class="comment-date">2011-02-10 22:19 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Towards better abstractions https://blog.ploeh.dk/2010/12/03/Towardsbetterabstractions 2010-12-03T13:19:48+00:00 Mark Seemann <div id="post"> <p> In my <a href="/2010/12/02/Interfacesarenotabstractions">previous post</a> I discussed why the use of interfaces doesn't <em>guarantee</em> that we work against good abstractions. In this post I will look at some guidelines that might be helpful in defining better abstractions. </p> <p> One important trait of a useful abstraction is that we can create many different implementations of it. This is the <a href="http://parlezuml.com/blog/?postid=934">Reused Abstractions Principle (RAP)</a>. This is particularly important because composition and separation of concerns often result in such reuse. Every time we use <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null Objects</a>, <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a> or <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composites</a>, we <a href="/2010/12/02/Interfacesarenotabstractions">reuse the same abstraction to compose an application from separate classes</a> that all adhere to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>. For example, Decorators are an excellent way to implement <a href="http://en.wikipedia.org/wiki/Cross-cutting_concern">Cross-Cutting Concerns</a>. </p> <p> The RAP gives us a way to <em>identify</em> good abstractions after the fact, but doesn't say much about the <em>traits</em> that make up a good, composable interface. </p> <p> On the other hand, I find that the <em>composability</em> of an interface is a pretty good indicator of its potential for reuse. While we can create Decorators from just about any interface, creating meaningful Null Objects or Composites are much harder. As we <a href="/2010/12/02/Interfacesarenotabstractions">previously saw</a>, bad abstractions often prevent us from implementing a meaningful Composite. </p> <blockquote> <p> Being able to implement a meaningful Composite is a good indication of a sound interface. </p> </blockquote> <p> This understanding is equivalent to the realization associated with the concept of <em>Closure of Operations</em> from <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215">Domain-Driven Design</a>. As soon as we achieve this, a lot of very intuitive, almost arithmetic-like APIs tend to follow. It becomes much easier to <em>compose</em> various instances of the abstraction. </p> <p> With Composite as an indicator of good abstractions, here are some guidelines that should enable us to define more useful interfaces. </p> <h3 id="261c9a3b383845bfae59fdf4a13ed64b"> ISP <a href="#261c9a3b383845bfae59fdf4a13ed64b" title="permalink">#</a> </h3> <p> The more members an interface has, the more difficult it is to create a Composite of it. Thus, the <a href="http://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> is a good guide, as it points us towards small interfaces. By extrapolation, the best interface would be an interface with a single member. </p> <p> That's a good start, but even such an interface could be problematic if it's a <a href="http://en.wikipedia.org/wiki/Leaky_abstraction">Leaky Abstraction</a> or a Shallow Interface. Still, let us assume that we aim for such <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interfaces</a> and move on to see what other guidelines are available to us. </p> <h3 id="d577137de88c43dbaedbbc6390b0c657"> Commands <a href="#d577137de88c43dbaedbbc6390b0c657" title="permalink">#</a> </h3> <p> <a href="http://en.wikipedia.org/wiki/Command_pattern">Commands</a>, and by extension any interface that consists of all void methods, are imminently composable. To implement a Null Object, just ignore the input and do nothing. To implement a Composite, just pass on the input to each contained instance. </p> <p> A Command is the epitome of the <a href="http://en.wikipedia.org/wiki/Hollywood_Principle">Hollywood Principle</a> because <em>telling</em> is the only thing you can do. There's no way to <em>ask</em> a Command about anything when the method returns void. Commands also guarantee the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a>, because there's no way you can ‘dot' across a void :) </p> <p> If a Command takes one or more input parameters, they must all stay clear of Shallow Interfaces and Leaky Abstractions. If these conditions are satisfied, a Command tends to be a very good abstraction. However, sometimes we just need return values. </p> <h3 id="8aaede06770846d3822f0d008fe9f4d9"> Closure of Operations <a href="#8aaede06770846d3822f0d008fe9f4d9" title="permalink">#</a> </h3> <p> We already briefly discussed Closure of Operations. In C# we can describe this concept as any method that fits this signature in some way: </p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">T DoIt(T x);</font></pre> </p> <p> An interface that returns the same type as the input type(s) exhibit Closure of Operations. There may be more than one input parameter as long as they are all of the same type. </p> <p> The interesting thing about Closure of Operations is that any interface with that quality is easily implemented as a Null Object (just return the input). A sort of Composite is often also possible because we can pass the input to each instance in the Composite and use some sort of aggregation or selection algorithm to return a result. </p> <p> Even if the return type doesn't easily lend itself towards aggregation, you can often implement a coalescing behavior with a Composite by returning the first non-null instance returned by the contained instances. </p> <p> Interfaces that exhibits Closure of Operations tend to be good abstractions, but it's not always possible to design APIs like that. </p> <h3 id="13f36c2d65b845b5bc320476619ea372"> Reduction of Input <a href="#13f36c2d65b845b5bc320476619ea372" title="permalink">#</a> </h3> <p> Sometimes we can keep some of the benefits from Closure of Operations even though a pure model isn't possible. Any method that returns a type that is a subset of the input types also tends to be composable. </p> <p> One variation is something like this: </p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">T1 DoIt(T1 x, T2 y, T3 z);</font></pre> </p> <p> In this sort of interface, the return type is the same as the first parameter. When creating Null Objects or Composites, we can generally just do as we did with pure Closure of Operations and ignore the other parameters. </p> <p> Another variation is a method like this: </p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">T1 DoIt(<span><font color="#2b91af">Foo</font></span>&lt;T1, T2, T3&gt; foo);</font></pre> </p> <p> where Foo is defined like this: </p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">Foo</font></span>&lt;T1, T2, T3&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> T1 X { <span><font color="#0000ff">get</font></span>; <span><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> T2 Y { <span><font color="#0000ff">get</font></span>; <span><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> T3 Z { <span><font color="#0000ff">get</font></span>; <span><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> In this case we can still reduce the input to create the output by simply selecting and returning foo.X and ignoring the other properties. </p> <p> Still, we may not always be able to define APIs such as these. </p> <h3 id="ed6816f35a064c02a4cd62f57db22dbf"> Composable return types <a href="#ed6816f35a064c02a4cd62f57db22dbf" title="permalink">#</a> </h3> <p> Sometimes (perhaps even most of the times) we can't mold our APIs into any of the above shapes because we inherently need to <em>map</em> one type into another type: </p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">T2 Map(T1 x);</font></pre> </p> <p> To keep such a method composable, we must then make sure that the output type itself is composable. This would allow us to implement a Composite by wrapping each return value from the contained instances into a Composite of the return type. </p> <p> Likewise, we could create a Null Object by returning another Null Object for the return type. </p> <p> In theory, we could repeat this design process to create a big chain of composable types, as long as the last type terminates the chain by fitting into one of the above shapes. However, this can quickly become unwieldy, so we should go to great efforts to make those chains as short as possible. </p> <p> It should be noted that every type that implements IEnumerable fits pretty well into this category. A Null Object is simply an empty sequence, and a Composite is simply a sequence with multiple items. Thus, interfaces that return enumerables tend to be good abstractions. </p> <h3 id="a14215f30fa94b08a677363bfea4ca38"> Conclusion <a href="#a14215f30fa94b08a677363bfea4ca38" title="permalink">#</a> </h3> <p> There are many well-known variations of good interface design. The above guiding principles looks only at a small, interrelated set. In fact, we can regard both Commands and Closure of Operations as degenerate cases of Reduction of Input. We should strive to create interfaces that directly fit into one of these categories, and when that isn't possible, at least interfaces that return types that fit into those categories. </p> <p> Keeping interfaces small and focused makes this possible in the first place. </p> <p> <strong>P.S.</strong> (added 2019-01-28) See my <a href="/2019/01/28/better-abstractions-revisited">retrospective article on the topic</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9aa9c9cd8e714e2f9aa94b71272736c3"> <div class="comment-author"><a href="http://joger.net">Johann Gerell</a> <a href="#9aa9c9cd8e714e2f9aa94b71272736c3">#</a></div> <div class="comment-content">Well thought through!</div> <div class="comment-date">2011-06-08 08:27 UTC</div> </div> <div class="comment" id="e5f4004d9f114c04a81fd42aaf38b7be"> <div class="comment-author">Orchun Kolcu <a href="#e5f4004d9f114c04a81fd42aaf38b7be">#</a></div> <div class="comment-content"> <p> I was able to follow you perfectly until the section "Composable return types", however I'd like to clarify how letting go of closure of operations affects composability. Certainly, if the return type isn't composable, you are SOL. However, conforming to the three previous shapes mentioned is only a (generally) sufficent but NOT necessary condition for composability, is that correct? I ask because you follow with "as long as the last type terminates the chain by fitting into one of the above shapes", and not just "... by being composable" so it muddies the waters a bit. </p> <p> My second question is about this chain itself - not clear on what it is. It's not inheritance and I thought about the chain formed formed by recursively called Composites in the parent Composite's tree structure, but then they wouldn't be conforming to the same interface. </p> <p> Also when you said other well-known variations of good interface design, were you talking about minimalism, RAP, rule/intent interfaces and ISP? I'm not aware of much more. </p> <p> Many thanks! </p> </div> <div class="comment-date">2018-12-07 15:57 UTC</div> </div> <div class="comment" id="c5d504ffdf79450ab1eae7dcb92656cd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c5d504ffdf79450ab1eae7dcb92656cd">#</a></div> <div class="comment-content"> <p> Orchun, thank you for writing. I intend to write a retrospective on this article, in the light of what I've subsequently figured out. I briefly <a href="/2018/05/17/composite-as-a-monoid-a-business-rules-example">discuss that connection in another article</a>, but I've yet to make a formal treatment out of it. Shortly told, all the 'shapes' identified here form <a href="/2017/10/06/monoids">monoids</a>, and <a href="/2018/03/12/composite-as-a-monoid">the Composite design pattern itself forms a monoid</a>. </p> <p> Stay tuned for a more detailed treatment of each of the above 'shapes'. </p> <p> Regarding good interface design, apart from minimalism, my primary concern is that an API does its utmost to communicate the invariants of the interaction; i.e. the pre- and postconditions involved. This is what Bertrand Meyer called <em>design by contract</em>, what I simply call <em>encapsulation</em>, and is related to what functional programmers refer to when they talk about being able to <em>reason about the code</em>. </p> </div> <div class="comment-date">2018-12-08 10:47 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Interfaces are not abstractions https://blog.ploeh.dk/2010/12/02/Interfacesarenotabstractions 2010-12-02T13:03:04+00:00 Mark Seemann <div id="post"> <p> One of the first sound bites from the beloved book <a href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/">Design Patterns</a> is this: </p> <blockquote> <p> <em>Program to an interface, not an implementation</em> </p> </blockquote> <p> It would seem that a corollary is that we can measure the quality of our code on the number of interfaces; the more, the better. However, that's not how it feels in reality when you are trying to figure out whether to use an IFooFactory, IFooPolicy, IFooPolicyFactory or perhaps even an IFooFactoryFactory. </p> <p> Do you extract interfaces from your classes to enable loose coupling? If so, you probably have a <a href="http://martinfowler.com/bliki/InterfaceImplementationPair.html">1:1 relationship between your interfaces and the concrete classes that implement them</a>. That's <a href="http://simpleprogrammer.com/2010/11/02/back-to-basics-what-is-an-interface/">probably not a good sign</a>, and violates the <a href="http://parlezuml.com/blog/?postid=934">Reused Abstractions Principle (RAP)</a>. I've been guilty of this and didn't like the result. </p> <blockquote> <p> Having only one implementation of a given interface is a code smell. </p> </blockquote> <p> Programming to an interface does not guarantee that we are coding against an abstraction. Interfaces are not abstractions. Why not? </p> <p> An interface is just a language construct. In essence, it's just a <em>shape</em>. It's like a power plug and socket. In Europe we use one kind, and the US uses another, but it's only <em>by convention</em> that we transmit 230V through European sockets and 110V through US sockets. Although plugs only fit in their respective sockets, nothing prevents us from sending 230V through a US plug/socket combination. </p> <p> <a href="http://blogs.msdn.com/b/kcwalina/">Krzysztof Cwalina</a> already pointed this out in 2004: <a href="http://blogs.msdn.com/b/kcwalina/archive/2004/10/24/246947.aspx">interfaces are not contracts</a>. If they aren't even contracts, then how can they be abstractions? </p> <p> Interfaces <em>can</em> be used as abstractions, but using an interface is in itself no guarantee that we are dealing with an abstraction. Rather, we have the following relationship between interfaces and abstractions: </p> <p> <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Abstraction and interface sets" border="0" alt="Abstractions, interfaces and their intersection" src="/content/binary/Windows-Live-Writer/Interfaces-are-not-abstractions_D282/image_5.png" width="240" height="135"> </p> <p> There are basically two sets: a set of abstractions and a set of interfaces. In the following we will discuss the set of interfaces that does <em>not</em> intersect the set of abstractions, saving the intersection for <a href="/2010/12/03/Towardsbetterabstractions">another blog post</a>. </p> <p> There are many ways an interface can turn out to be a poor abstraction. The following is an incomplete list: </p> <h3 id="6049f5eefff44d369b4c9c66209d713b"> LSP Violations <a href="#6049f5eefff44d369b4c9c66209d713b" title="permalink">#</a> </h3> <p> Violating the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a> is a pretty obvious sign that the interface in use is a poor abstraction. This may be most obvious when the consumer of the interface needs to downcast an instance to properly work with it. </p> <p> However, as <a href="http://www.objectmentor.com/omTeam/martin_r.html">Uncle Bob</a> <a href="http://www.objectmentor.com/resources/articles/lsp.pdf">points out</a>, even an interface as simple as this seemingly innocuous rectangle ‘abstraction' contains potential dangers: </p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">interface</font></span> </font><span><font style="font-size: 10pt" color="#2b91af">IRectangle</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">int</font></span> Width { <span><font color="#0000ff">get</font></span>; <span><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">int</font></span> Height { <span><font color="#0000ff">get</font></span>; <span><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> The issue becomes apparent when you attempt to let a Square class implement IRectangle. To protect the invariants of Square, you can't allow the Width and Height properties to differ. You have a couple of options, none of which are very good: </p> <ul> <li>Update both Width and Height to the same value when one of them are being written.</li> <li>Ignore the write operation when the caller attempts to assign an invalid value.</li> <li>Throw an exception when the caller attempts to assign a Width which is different from the Height (and vice versa).</li> </ul> <p> From the point of view of a consumer of the IRectangle interface, all of these options would at the very least violate the <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment">Principle of Least Astonishment</a>, and throwing exceptions would definitely cause the consumer to behave differently when consuming Square instances as opposed to ‘normal' rectangles. </p> <p> The problem stems from the fact that the operations have side effects. Invoking one operation changes the state of a seemingly unrelated piece of data. The more members we have, the greater the risk is, so the <a href="http://en.wikipedia.org/wiki/Interface_segregation_principle">Interface Segregation Principle</a> can, to a certain extent, help. </p> <h3 id="5003089a645f468bb76d44b61871bfdd"> Header Interfaces <a href="#5003089a645f468bb76d44b61871bfdd" title="permalink">#</a> </h3> <p> Since a higher number of members increases the risk of unexpected side effects and temporal coupling it should come as no surprise that interfaces mechanically extracted from <em>all</em> members of a concrete class are poor abstractions. </p> <p> As always, Visual Studio makes it very easy to do the wrong thing by offering the <em>Extract Interface</em> refactoring feature. </p> <p> We call such interfaces <a href="http://martinfowler.com/bliki/HeaderInterface.html">Header Interfaces</a> because they resemble C++ header files. They tend to simply state the same thing twice without apparent benefit. This is particularly true when you have only a single implementation, which tends to be very likely for interfaces with many members. </p> <h3 id="0b61e86ff9244be2ad6a9311f7a22ae9"> Shallow Interfaces <a href="#0b61e86ff9244be2ad6a9311f7a22ae9" title="permalink">#</a> </h3> <p> When you use the <em>Extract Interface</em> refactoring feature in Visual Studio, even if you don't extract every member, the resulting interface is <em>shallow</em> because it doesn't recursively extract interfaces from the concrete types exposed by the extracted members. </p> <p> An example I've seen more than once involves extracting an interface from a LINQ to SQL or LINQ to Entities context in order to define a Repository interface. As an example, here's an interface extracted from a very simple LINQ to Entities context: </p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">interface</font></span> </font><span><font style="font-size: 10pt" color="#2b91af">IPostingContext</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">void</font></span> AddToPostings(<span><font color="#2b91af">Posting</font></span> posting);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">ObjectSet</font></span>&lt;<span><font color="#2b91af">Posting</font></span>&gt; Postings { <span><font color="#0000ff">get</font></span>; }</font> <font style="font-size: 10pt">}</font></pre> </p> <p> At first glance this may look useful, but it isn't. Even though it's an interface, it's still tightly coupled to a specific object context. Not only does ObjectSet&lt;T&gt; reference the Entity Framework, but the Posting class is defined by a very specific, auto-generated Entity context. </p> <p> The interface may give you the impression of working against loosely coupled code, but you can't easily (if at all) implement a different IPostingContext with a radically different data access technology. You'll be stuck with this particular PostingContext. </p> <p> If you must extract an interface, you'll need to do it recursively. </p> <h3 id="3d22189b4ab349f59cd4aa01e349ecfb"> Leaky Abstractions <a href="#3d22189b4ab349f59cd4aa01e349ecfb" title="permalink">#</a> </h3> <p> Another way we can create problems for ourselves is when our interfaces leak implementation details. A good example can be found in the <a href="http://systemwrapper.codeplex.com/">SystemWrapper</a> project that provides extracted interfaces for various BCL types, such as <a href="http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx">System.IO.FileInfo</a>. Those interfaces may enable mocking, but we shouldn't expect to ever be able to create another implementation of SystemWrapper.IO.IFileInfoWrap. In other words, those interfaces aren't very useful. </p> <p> Another example is this attempt at defining a Repository interface: </p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">interface</font></span> </font><span><font style="font-size: 10pt" color="#2b91af">IFooRepository</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">string</font></span> ConnectionString { <span><font color="#0000ff">get</font></span>; <span><font color="#0000ff">set</font></span>; }</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span><font style="font-size: 10pt" color="#008000">// ...</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p> Exposing a ConnectionString property strongly indicates that the repository is implemented on top of a database; this knowledge leaks through. If we wanted to implement the repository based on a web service, we might be able to repurpose the ConnectionString property to a service URL, but it would be a hack at best - and how would we define security settings in that scenario? </p> <p> Exposing a <em>FileName</em> property on an interface that represents an abstract resource is another example of a <a href="http://en.wikipedia.org/wiki/Leaky_abstraction">Leaky Abstraction</a>. </p> <p> Leaky Abstractions like these are often difficult to reuse. As an example, it would be difficult to implement a <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composite</a> out of the above IFooRepository - how do you aggregate a ConnectionString? </p> <h3 id="08efe99a6f9d4d899783ce26a76cac39"> Conclusion <a href="#08efe99a6f9d4d899783ce26a76cac39" title="permalink">#</a> </h3> <p> In short, using interfaces in no way guarantees that we operate with appropriate abstractions. Thus, the proliferation of interfaces that typically follow from TDD or use of DI may not be the pure goodness we tend to believe. </p> <p> Creating good abstractions is difficult and requires skill. In a <a href="/2010/12/03/Towardsbetterabstractions">future post</a>, I'll look at some principles that we can use as guides. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c345a4a2bb2c47f09672931b05824eff"> <div class="comment-author">anonymous <a href="#c345a4a2bb2c47f09672931b05824eff">#</a></div> <div class="comment-content">How do you propose mocking/stubbing then?</div> <div class="comment-date">2010-12-02 16:21 UTC</div> </div> <div class="comment" id="f5ee980862304022afeef25f53f3385a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f5ee980862304022afeef25f53f3385a">#</a></div> <div class="comment-content">Good abstractions will still be interfaces (or base classes), so replacement with Test Doubles will still be possible.</div> <div class="comment-date">2010-12-02 16:24 UTC</div> </div> <div class="comment" id="dbb49e17edc5496cb6a7d1c17ac7e54f"> <div class="comment-author"><a href="http://twitter.com/BlackTigerX">Eber Irigoyen</a> <a href="#dbb49e17edc5496cb6a7d1c17ac7e54f">#</a></div> <div class="comment-content">*they also say that an abstract class is preferred over an interface, I am guilty of the one interface per class, so can't wait to see your point of view for creating abstractions</div> <div class="comment-date">2010-12-02 16:28 UTC</div> </div> <div class="comment" id="6333453d057442baa7c4a8ba394e4732"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6333453d057442baa7c4a8ba394e4732">#</a></div> <div class="comment-content">The whole discussion about abstract base classes versus interfaces is orthogonal to the point I'm trying to make, so I'm not going to go into that discussion in my next post.<br> <br> The reason why the Framework Design Guidelines favor abstract classes is related to keeping options open for future extensions to abstractions without breaking backwards compatibility. It makes tons of sense when you just <em>have</em> to respect backwards compatibility when adding new features. This is the case for big, commercial frameworks like the BCL. However, I'm beginning to suspect that this is kind of a pseudo-argument; it's really more an excuse for creating abstractions that don't adhere to the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>.<br> <br> On the other hand, it isn't that important if you control the entire code base in question. This is often the case for enterprise applications, where essentially you only have one customer and at most a handful of deployments.<br> <br> The problem with base classes is that they are a much more heavy-handed approach. Because you can only derive from a single base class, it becomes impossible to implement more than one <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interface</a> in the same class. Because of that constraint, I tend to prefer interfaces over base classes.</div> <div class="comment-date">2010-12-02 17:57 UTC</div> </div> <div class="comment" id="2fb6722ba7354319956ad1d1fc12b2b1"> <div class="comment-author"><a href="Http://khebbie.dk">Klaus hebsgaard</a> <a href="#2fb6722ba7354319956ad1d1fc12b2b1">#</a></div> <div class="comment-content">So in order to change implementation of a dependency in c# (ie. Throw in a test double) i have to use an interface (unless i use TypeMock or the like).<br> The way I understand your post is not that you want me to change this, which is also impossible in a statically typed language.<br> Do I understand you correctly that you are more talking about how I design these dependencies so they would be - well better designed?<br> So in a way, if I understand you correctly, the things you are saying her could be applied in fx. Ruby as well, since they are design principles and not language stuff?</div> <div class="comment-date">2010-12-02 20:53 UTC</div> </div> <div class="comment" id="28fae56ba2264c6393f53ac25c83c9d2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#28fae56ba2264c6393f53ac25c83c9d2">#</a></div> <div class="comment-content">Yes, the only thing I say is that an interface doesn't <em>guarantee</em> that you are using a good abstraction, but you can certainly <em>use</em> interfaces to model good abstractions - hence the Venn diagram.</div> <div class="comment-date">2010-12-02 20:59 UTC</div> </div> <div class="comment" id="9ef1a63acaba42aab4eb6e5c88c581c0"> <div class="comment-author">Daniel Gioulakis <a href="#9ef1a63acaba42aab4eb6e5c88c581c0">#</a></div> <div class="comment-content">Hi there Mark,<br> I've found this post to be quite an interesting read. At first, I was not in complete agreement, but as I took a step back and examined the way I write domain-driven code, I realized 99% of my code follows these concepts and rarely would I ever have a 1:1 relationship.<br> <br> Today I happened upon a good video from one of Udi Dahan's talks: &quot;Making Roles Explicit&quot;. It can be found here: http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan<br> <br> You can find some examples of his talk in practice here:<br> http://www.simonsegal.net/blog/2010/03/18/nfetchspec-code-entity-framework-repository-fetching-strategies-specifications-code-only-mapping-poco-and-making-roles-explicit/<br> <br> If you happen to have some time to watch the speech, I was curious to hear your opinion on the subject as it seems like it would violate the concepts of this blog post. I still haven't wrapped my head around it all, but it would seem that Udi recommends creating interfaces for each &quot;role&quot; the domain object plays and using a Service Locator to find the concrete implementation ... or in his case the concrete FetchingStrategy used to pull data back from his ORM. This sounds like his application would have many 1:1 abstractions. I am familiar with your stance on the Service Locator and look forward to your book coming out. :)<br> <br> Thanks,<br> Danny</div> <div class="comment-date">2010-12-17 20:02 UTC</div> </div> <div class="comment" id="538ca9f0a70b4f138aa691dc9ea84471"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#538ca9f0a70b4f138aa691dc9ea84471">#</a></div> <div class="comment-content">Hi Danny<br> <br> Thanks for your comments. If you manage to read your way through my <a href="/2010/12/03/Towardsbetterabstractions">follow-up post</a> you'll notice that I already discuss Role Interfaces there :)<br> <br> I actually prefer Role Interfaces over Header Interfaces, but I can understand why you ask the questions you do. In fact, I went ahead and wrote a <a href="/2010/12/18/OnRoleInterfaces,theReusedAbstractionsPrincipleandServiceLocators">new blog post</a> to answer them. HTH :)</div> <div class="comment-date">2010-12-18 14:27 UTC</div> </div> <div class="comment" id="860b030366e5433780b8732c7bd697ac"> <div class="comment-author">Emanuel Pasat <a href="#860b030366e5433780b8732c7bd697ac">#</a></div> <div class="comment-content">This blog post is asking for a quick real-time example (simple WCF or Azure example, let's say). <br> I assume you'll use same constructor injection with abstract factory but how will you avoid 1:1 mapping in these cases? Using the same trick with NullService and WcfService?<br> What is a good abstraction for a Wcf service or for a CloudQueueClient?<br> <br> Thanks in advance</div> <div class="comment-date">2010-12-24 11:23 UTC</div> </div> <div class="comment" id="7acc8f375f32464a98c1a498477f69ca"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7acc8f375f32464a98c1a498477f69ca">#</a></div> <div class="comment-content">I recently wrote an Azure application where I used these abstractions on top of Azure Queues:<br> <br> public interface IChannel { void Send(object message); }<br> <br> for sending messages, and this one to handle them:<br> <br> public interface IMessageConsumer&lt;T&gt; { void Consume(T message); }<br> <br> Since both are Commands (they return void) they compose excellently.</div> <div class="comment-date">2010-12-28 08:44 UTC</div> </div> <div class="comment" id="3827428ebdea486b8111deb6c95a7745"> <div class="comment-author">Emanuel Pasat <a href="#3827428ebdea486b8111deb6c95a7745">#</a></div> <div class="comment-content">Quote from RAP (http://parlezuml.com/blog/?postid=934) : <br> <br> &quot;If the only class that ever implements the Customer interface is CustomerImpl, you don't really have polymorphism and substitutability because there is nothing in practice to substitute at runtime. It's fake generality. All you have is indirection and code clutter, which just makes the code harder to understand.&quot;<br> <br> From there I thought that 1:1 mapping means one implementation to one interface.<br> From your example it seems that 1:1 mapping referes to not having all members of an implementation matching exactly all members of the interface.<br> <br> Which one is true?<br> Thanks again</div> <div class="comment-date">2010-12-29 08:14 UTC</div> </div> <div class="comment" id="2072b6e09728409796503e965f3ecb36"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#2072b6e09728409796503e965f3ecb36">#</a></div> <div class="comment-content">No, I meant exactly the same as the RAP post. Not implementing all members would indicate that an interface has more than one members. That would smell of a Header Interface, and I prefer Role Interface.<br> <br> What gave you that other impression?</div> <div class="comment-date">2010-12-29 12:12 UTC</div> </div> <div class="comment" id="fe911fb0bec842e1b1f53f1b35f1ab58"> <div class="comment-author">Emanuel Pasat <a href="#fe911fb0bec842e1b1f53f1b35f1ab58">#</a></div> <div class="comment-content">The fact that IChannel should have more implementations.<br> <br> But that's usually accomplished with decorators (we always need cross cutting concerns) and null objects, right? <br> </div> <div class="comment-date">2010-12-29 12:43 UTC</div> </div> <div class="comment" id="26d6583e39d041979eb9a98820620bb1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#26d6583e39d041979eb9a98820620bb1">#</a></div> <div class="comment-content">Yes, that's right, but a Decorator is also an implementation of an interface. As soon as you define your first Decorator, by implication you already have two implementations of the same type.<br> <br> With an interface like IChannel, a Composite also becomes possible, in the case that you would like to broadcast a message on multiple channels.</div> <div class="comment-date">2010-12-29 13:13 UTC</div> </div> <div class="comment" id="7a35e4227965442183c0887597e99041"> <div class="comment-author">Simple <a href="#7a35e4227965442183c0887597e99041">#</a></div> <div class="comment-content">Can you show please some examples where you use some kind of Test Doubles for Unit Tests without 1:1 Interface implementation? ))<br> <br> Thanks )<br> <br> <br> <br> <br> </div> <div class="comment-date">2012-04-21 12:57 UTC</div> </div> <div class="comment" id="45d1f54d1d3f4910a02072289855261f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#45d1f54d1d3f4910a02072289855261f">#</a></div> <div class="comment-content">For a framework example, see <a href="http://autofixture.codeplex.com/">AutoFixture</a>, which contains some 3000 unit tests - many of them with Test Doubles. Most (if not all) of the interfaces have several implementations, e.g. ISpecimenBuilder.<br> <br> For a more complete application, see <a href="https://github.com/ploeh/Booking">the Booking sample CQRS application</a>. It uses Moq for Test Doubles, and I very consciously wrote that code base with the RAP in mind.</div> <div class="comment-date">2012-04-22 06:09 UTC</div> </div> <div class="comment" id="1e1ce198ffa24c5cbc46f33758483539"> <div class="comment-author">Simple <a href="#1e1ce198ffa24c5cbc46f33758483539">#</a></div> <div class="comment-content">Maybe Ill better show some of my code later and ask - where the problem is =)<br> <br> <br> Do you have plans to publish some books about software development? Some kind of patterns explanation.. or best practices for .NET developers - with actual technologies? (not only DI =)) <br> <br> I think you can not only develop something but also explain how =) <br> <br> </div> <div class="comment-date">2012-04-23 17:42 UTC</div> </div> <div class="comment" id="379e357ff66d4b4ea958c25ab61c7c5f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#379e357ff66d4b4ea958c25ab61c7c5f">#</a></div> <div class="comment-content">Currently, I don't have any concrete plans for new books, but it's not unlikely that I'll write another book in the future.</div> <div class="comment-date">2012-04-23 18:12 UTC</div> </div> <div class="comment" id="cc405716f9392f7f2d784a29f0a0d609"> <div class="comment-author">Junlong <a href="#cc405716f9392f7f2d784a29f0a0d609">#</a></div> <div class="comment-content"> You mentioned at the beginning that "Having only one implementation of a given interface is a code smell.", but then you discussed why interfaces are not abstractions in some aspects. Would you mind answering why 1:1 relation between an interface and a implementation class, or posting the link if you have had another post discussing it? Thank you. </div> <div class="comment-date">2022-03-02 09:20 UTC</div> </div> <div class="comment" id="cf7d6ea7f09a4910a6ec41ffa029440f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cf7d6ea7f09a4910a6ec41ffa029440f">#</a></div> <div class="comment-content"> <p> Junlong, thank you for writing. This blog post already contains some links, of which I think that <a href="http://parlezuml.com/blog/?postid=934">Reused Abstractions Principle (RAP)</a> does the best job of describing the problem. </p> <p> For what it's worth, I also discuss what makes a good abstraction in my book <a href="/code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> </div> <div class="comment-date">2022-03-03 6:25 UTC</div> </div> <div class="comment" id="8b2f5e71d940c3a8f6e7b9c0a2d158e7"> <div class="comment-author">Alex Murari <a href="#8b2f5e71d940c3a8f6e7b9c0a2d158e7">#</a></div> <div class="comment-content"> <p> Mark, I think abstraction is (and always has been) an overloaded term. And it hurts our profession, because important concpets become less clear. </p> <p> Abstraction, in the words of Uncle Bob, is the <i>[...] amplification of the essential and the elimination of the irrelevant."</i> </p> <p> Divindig a big method in smaller ones is the perfect example of that phrase: we amplify the essential (a call to a new method with a good name indicating <b>what</b> it does) while eliminating the irrelevant (hiding the code [<b>how it does</b>] behind a new method [abstraction]). If we want to know the <i>hows</i>, we navigate to that method implementation. What = essential / How = irrelevant. </p> <p> This is completelly related to the concept of fractal architecture from your book, we just zoom in when we want. (Great book by the way, going to read it for the 3rd time). </p> <p> I think we confuse the <i>abstract</i> (concept) with <code>abstract</code> (keyword, opposite of concrete) and <i>interface</i> (concept, aka API) with <code>interface</code> (keyword). </p> <p> Interfaces (keyword) are always abstractions (concept), because they provide a list of methods/functions (API) for executing some logic that is abstracted (concept) behind it. The real questions are "are they <i>good</i> abstractions?", "Why are you using interfaces (keyword) for a single (1:1) implementation?" </p> <p> Bottom line: interfaces (keyword) are always abstractions (concept), but not always good ones. </p> <p> If you have the time, please write an article expanding on that line of reasoning. </p> </div> <div class="comment-date">2023-08-16 16:35 UTC</div> </div> <div class="comment" id="b69efb0445864a798543dcdf02df0b27"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b69efb0445864a798543dcdf02df0b27">#</a></div> <div class="comment-content"> <p> Alex, thank you for writing. There are several overloaded og vague terms in programming: <em>Client</em>, <em>service</em>, <em>unit test</em>, <em>mock</em>, <em>stub</em>, <em>encapsulation</em>, <em>API</em>... People can't even agree on how to define object-oriented or functional programming. </p> <p> I don't feel inclined to <a href="https://xkcd.com/927/">add to the confusion by adding my own definition to the mix</a>. </p> <p> As you've probably observed, I use Robert C. Martin's definition of abstraction in my book. According to that definition, you can easily define an <code>interface</code> that's not an abstraction: Just break the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>. There are plenty of examples of those around: Interface methods that return or take as parameters ORM objects, or <a href="/2014/08/11/cqs-versus-server-generated-ids">returning a database ID from a Create method</a>. Such interfaces don't eliminate the irrelevant. </p> </div> <div class="comment-date">2023-08-17 7:41 UTC</div> </div> <div class="comment" id="a27cde8d731442bf9e5af69c8107b3d2"> <div class="comment-author">Alex Murari <a href="#a27cde8d731442bf9e5af69c8107b3d2">#</a></div> <div class="comment-content"> <p> Mark, thanks for the response. There are two overloaded terms in programming that I consider the most important and the most misunderstood: <b>abstraction</b> and <b>encapsulation</b>. </p> <p> Abstraction is important because it's about managing complexity, encapsulation is important because it's about preserving data integrity. </p> <p> These are basic, fundamental (I can't stress this enough) concepts that enable the sustainable growth of a software project. I'm on the front-line here, and I know what a codebase that lacks these two concepts looks like. I think you do too. It ain't pretty. </p> <p> Oddly enough, these aren't taught correctly at all! In my (short) formal training at a university, abstraction was taught in terms of abstract classes/inheritance (OOP). Encapsulation as nothing more than using <i>getters</i> and <i>setters</i> (use auto-properties and you're encapsulated. Yay!). There were no mentions of <i>reducing complexity</i> or <i>preserving integrity</i> whatsoever. I dropped out after two years. The only thing I learned from my time at the university is that there is an immense gap between the industry and educational institutions. </p> <p> I'm mostly (if not completely) a self-taught programmer. I study a lot, and I'm always looking for new articles, books, etc. It took me ~7 years to find a coherent explanation of abstraction and encapsulation. </p> <p> Initially, I asked myself "am I the only one who didn't have such basic knowledge, even though being a programmer for almost 10 years?" Then I started asking around work colleagues for definitions of those terms: turns out I wasn't the only one. I can't say I was surprised. </p> <p> Maybe I am a fool for giving importance to such things as definitions and terminology of programming terms, but I can't see how we can move our profession towards a more engineering-focused one if we don't agree on (and teach correctly) basic concepts. Maybe I need <a href="https://www.goodreads.com/de/book/show/39644164">Adam Barr's time machine</a>. </p> </div> <div class="comment-date">2023-08-17 15:12 UTC</div> </div> <div class="comment" id="e809bf45125546338f512a91c4cf2240"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e809bf45125546338f512a91c4cf2240">#</a></div> <div class="comment-content"> <p> Alex, thank you for writing. I agree with you, and I don't think that you're a fool for considering these concepts fundamental. Over a decade of consulting, I ran into the same fundamental mistakes over and over again, which then became the main driver for writing <a href="/2021/06/14/new-book-code-that-fits-in-your-head">Code That Fits in Your Head</a>. </p> <p> It may be that university fails to teach these concepts adequately, but to be fair, if you consider that the main goal is to educate young people who may never have programmed before, maintainability may not be the first thing that you teach. After all, students should be able to walk before they can run. </p> <p> Are there better ways to teach? Possibly. I'm still pursuing that ideal, but I don't know if I'll find those better ways. </p> </div> <div class="comment-date">2023-08-20 9:26 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Integrating AutoFixture with ObjectHydrator https://blog.ploeh.dk/2010/11/22/IntegratingAutoFixturewithObjectHydrator 2010-11-22T06:42:37+00:00 Mark Seemann <div id="post"> <p>Back in the days of <a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.0 I occasionally got the feedback that although people liked the engine and its features, they didn't like the data it generated. I think they particularly <a href="http://twitter.com/#!/hakanforss/status/7607978333">didn't like</a> <a href="/2009/04/02/CreatingStringsWithAutoFixture">all the Guids</a>, but <a href="http://hakanforss.wordpress.com/">Håkon Forss</a> <a href="http://twitter.com/#!/hakanforss/status/7598441621">suggested</a> combining <a href="http://objecthydrator.codeplex.com/">Object Hydrator</a>'s data generator with AutoFixture.</p> <p>In fact, this suggestion made me realize that AutoFixture 1.0's engine wasn't extensible enough, which again prompted me to build AutoFixture 2.0. Now that AutoFixture 2.0 is out, what would be more fitting than to examine whether we can do what Håkon suggested?</p> <p>It turns out to be pretty easy to customize AutoFixture to use Object Hydrator's data generator. The main part is creating a custom ISpecimenBuilder that acts as an <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> of Object Hydrator:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">HydratorAdapter</font></span> : </font><span><font style="font-size: 10pt" color="#2b91af">ISpecimenBuilder</font></span> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> <span><font color="#2b91af">IMap</font></span> map;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> HydratorAdapter(<span><font color="#2b91af">IMap</font></span> map)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (map == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ArgumentNullException</font></span>(<span><font color="#a31515">"map"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.map = map;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span><font color="#0000ff"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; #region</font></font></span><font style="font-size: 10pt"> ISpecimenBuilder Members</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">object</font></span> Create(<span><font color="#0000ff">object</font></span> request,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">ISpecimenContext</font></span> context)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> pi = request <span><font color="#0000ff">as</font></span> <span><font color="#2b91af">PropertyInfo</font></span>;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (pi == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> ((!<span><font color="#0000ff">this</font></span>.map.Match(pi))</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; || (<span><font color="#0000ff">this</font></span>.map.Type != pi.PropertyType))</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">NoSpecimen</font></span>(request);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> <span><font color="#0000ff">this</font></span>.map.Mapping(pi).Generate();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span><font style="font-size: 10pt" color="#0000ff">&nbsp;&nbsp;&nbsp; #endregion</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>The IMap interface is defined by Object Hydrator, ISpecimenBuilder and NoSpecimen are AutoFixture types and the rest are BCL types.</p> <p>Each HydratorAdapter adapts a single IMap instance. The IMap interface only works with PropertyInfo instances, so the first thing to do is to examine the request to figure out whether it's a request for a PropertyInfo at all. If this is the case and the map matches the request, we ask it to generate a specimen for the property.</p> <p>To get all of Object Hydrator's maps into AutoFixture, we can now define this customization:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">ObjectHydratorCustomization</font></span> :</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span><font style="font-size: 10pt" color="#2b91af">ICustomization</font></span> <font style="font-size: 10pt">{</font> <span><font color="#0000ff"><font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; #region</font></font></span><font style="font-size: 10pt"> ICustomization Members</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> <span><font color="#0000ff">void</font></span> Customize(<span><font color="#2b91af">IFixture</font></span> fixture)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> builders = <span><font color="#0000ff">from</font></span> m <span><font color="#0000ff">in</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">DefaultTypeMap</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">select</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">HydratorAdapter</font></span>(m);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.Customizations.Add(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">CompositeSpecimenBuilder</font></span>(builders));</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <span><font style="font-size: 10pt" color="#0000ff">&nbsp;&nbsp;&nbsp; #endregion</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>The ObjectHydratorCustomization simply projects all maps from Object Hydrator's DefaultTypeMap into instances of HydratorAdapter and adds these as customizations to the fixture.</p> <p>This enables us to use Object Hydrator with any Fixture instance like this:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">Fixture</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .Customize(<span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ObjectHydratorCustomization</font></span>());</font></pre> </p> <p>To prove that this works, here's a dump of a Customer type created in this way:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Id"</font></span>: 1,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"FirstName"</font></span>: <span><font color="#a31515">"Raymond"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"LastName"</font></span>: <span><font color="#a31515">"Reeves"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Company"</font></span>: <span><font color="#a31515">"Carrys Candles"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Description"</font></span>: <span><font color="#a31515">"Lorem ipsum dolor sit"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Locations"</font></span>: 53,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"IncorporatedOn"</font></span>: <span><font color="#a31515">"\/Date(1376154940000+0200)\/"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Revenue"</font></span>: 33.57,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"WorkAddress"</font></span>: {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"AddressLine1"</font></span>: <span><font color="#a31515">"32373 BALL Lane"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"AddressLine2"</font></span>: <span><font color="#a31515">"29857 DEER PARK Dr."</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"City"</font></span>: <span><font color="#a31515">"fullerton"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"State"</font></span>: <span><font color="#a31515">"NM"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"PostalCode"</font></span>: <span><font color="#a31515">"27884"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"Country"</font></span>: </font><span><font style="font-size: 10pt" color="#a31515">"GI"</font></span> <font style="font-size: 10pt">&nbsp; },</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"HomeAddress"</font></span>: {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"AddressLine1"</font></span>: <span><font color="#a31515">"66377 NORTH STAR Pl."</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"AddressLine2"</font></span>: <span><font color="#a31515">"33406 MAY Dr."</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"City"</font></span>: <span><font color="#a31515">"miami"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"State"</font></span>: <span><font color="#a31515">"MD"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"PostalCode"</font></span>: <span><font color="#a31515">"18361"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"Country"</font></span>: </font><span><font style="font-size: 10pt" color="#a31515">"PH"</font></span> <font style="font-size: 10pt">&nbsp; },</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Addresses"</font></span>: [],</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"HomePhone"</font></span>: <span><font color="#a31515">"(388)538-1266"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Type"</font></span>: 0</font> <font style="font-size: 10pt">}</font></pre> </p> <p>Without Object Hydrator's data generators, this would have looked like this instead:</p> <p> <pre style="margin: 0px"><font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Id"</font></span>: 1,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"FirstName"</font></span>: <span><font color="#a31515">"FirstNamebf53cb4c-3aae-4963-bb0c-ad0219293736"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"LastName"</font></span>: <span><font color="#a31515">"LastName079f7ab2-d026-48c5-8cfb-76e0568d1d79"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Company"</font></span>: <span><font color="#a31515">"Company9ffe4640-2534-4ef7-b066-fb6bbe3a668c"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Description"</font></span>: <span><font color="#a31515">"Descriptionf5843974-b14b-4bce-b3cc-63ad6aaf3ab2"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Locations"</font></span>: 2,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"IncorporatedOn"</font></span>: <span><font color="#a31515">"\/Date(1290169587222+0100)\/"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Revenue"</font></span>: 1.0,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"WorkAddress"</font></span>: {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"AddressLine1"</font></span>: <span><font color="#a31515">"AddressLine1f4d50570-423e-4a74-8348-1c54402ffe48"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"AddressLine2"</font></span>: <span><font color="#a31515">"AddressLine2031fe3e2-40c1-4ec3-b445-e88c213457e9"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"City"</font></span>: <span><font color="#a31515">"Citycd33fce3-66bb-457d-8f99-98a16c0c5bf1"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"State"</font></span>: <span><font color="#a31515">"State40bebd6d-6073-4421-8a74-e910ff9d09e3"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"PostalCode"</font></span>: <span><font color="#a31515">"PostalCode1da93f22-799b-4f6b-a5ce-f4816f8bbb05"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"Country"</font></span>: </font><span><font style="font-size: 10pt" color="#a31515">"Countryfa2ad951-ce0c-42a4-ab55-c077b6e03f00"</font></span> <font style="font-size: 10pt">&nbsp; },</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"HomeAddress"</font></span>: {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"AddressLine1"</font></span>: <span><font color="#a31515">"AddressLine145cbffeb-d7a9-4778-b297-d010c30b7614"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"AddressLine2"</font></span>: <span><font color="#a31515">"AddressLine2e86d6476-5bdc-4940-a8ee-975bf3f65d49"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"City"</font></span>: <span><font color="#a31515">"City6ae3aab9-7c73-4768-ae7d-a6ea515c816a"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"State"</font></span>: <span><font color="#a31515">"State56de6222-fd84-46b0-ace0-c6098dbd0681"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"PostalCode"</font></span>: <span><font color="#a31515">"PostalCodeca1af9af-a97b-4966-b156-cfbebd6d5e38"</font></span>,</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#a31515">"Country"</font></span>: </font><span><font style="font-size: 10pt" color="#a31515">"Country6960eebe-fe6f-4b63-ad73-7ba6a2b95791"</font></span> <font style="font-size: 10pt">&nbsp; },</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Addresses"</font></span>: [],</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"HomePhone"</font></span>: <span><font color="#a31515">"HomePhone623f9d6f-febe-4c9f-87f8-e90d7e57eb46"</font></span>,</font> <font style="font-size: 10pt">&nbsp; <span><font color="#a31515">"Type"</font></span>: 0</font> <font style="font-size: 10pt">}</font></pre> </p> <p>One limitation of Object Hydrator is that it requires the classes to have default constructors. AutoFixture doesn't have that constraint, and to prove that I defined the only available Customer constructor like this:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> Customer(<span><font color="#0000ff">int</font></span> id)</font></pre> </p> <p>With AutoFixture, this is not a problem and the Customer instance is created as described above.</p> <p>With the extensibility model of AutoFixture 2.0 I am pleased to be able to verify that Håkon Forss (and others) can now have the best of both worlds :)</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="d688fb0c0e5849beb0e7a2b13e5771b1"> <div class="comment-author">Almir Begovic <a href="#d688fb0c0e5849beb0e7a2b13e5771b1">#</a></div> <div class="comment-content">It does not compile, I had to make a slight change to make it work:<br> <br> public class ObjectHydratorCustomization :<br> ICustomization<br> {<br> #region ICustomization Members<br> <br> public void Customize(IFixture fixture)<br> {<br> var builders= (from m in new DefaultTypeMap()<br> select new HydratorAdapter(m) as ISpecimenBuilder);<br> fixture.Customizations.Add(<br> new CompositeSpecimenBuilder(builders));<br> }<br> <br> #endregion<br> }<br> </div> <div class="comment-date">2012-02-16 14:38 UTC</div> </div> <div class="comment" id="9c3334a6c7be4d70b139e3418c8f72d9"> <div class="comment-author">Ryan Smith <a href="#9c3334a6c7be4d70b139e3418c8f72d9">#</a></div> <div class="comment-content">I moved ObjectHydrator from Codeplex to GitHub<br> <br> <a href="https://github.com/PrintsCharming/ObjectHydrator">Here it is</a>.<br> As always feel free to use any part you'd like.' </div> <div class="comment-date">2014-06-17 04:22 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Rhino Mocks-based auto-mocking with AutoFixture https://blog.ploeh.dk/2010/11/13/RhinoMocks-basedauto-mockingwithAutoFixture 2010-11-13T21:19:09+00:00 Mark Seemann <div id="post"> <p> <a href="http://autofixture.codeplex.com/">AutoFixture</a> now includes a <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a>-based auto-mocking feature similar to the <a href="http://code.google.com/p/moq/">Moq</a>-based auto-mocking customization <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer">previously described</a>. </p> <p> The developer of this great optional feature, the talented but discreet Mikkel has this to say: </p> <p> "The auto-mocking capabilities of AutoFixture now include auto-mocking using Rhino Mocks, completely along the same lines as the existing Moq-extension. Although it will not be a part of the .zip-distribution before the next official release, it can be built from the latest source code (November 13, 2010) which contains the relevant VS2008 solution: AutoRhinoMock.sln. It is built on Rhino.Mocks version 3.6.0.0. To use it, add a reference to Ploeh.AutoFixture.AutoRhinoMock.dll and customize the Fixture instance with: </p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"> fixture = <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">Fixture</font></span>()</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; .Customize(<span><font color="#0000ff">new</font></span> <span><font color="#2b91af">AutoRhinoMockCustomization</font></span>());</font></pre> </p> <p> "which automatically will result in mocked instances of requests for interfaces and abstract classes." </p> <p> I'm really happy to see the AutoFixture eco-system grow in this way, as it both demonstrates how AutoFixture gives you great flexibility and enables you to work with the tools you prefer. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Refactoring from Abstract Factory to Decorator https://blog.ploeh.dk/2010/11/01/RefactoringfromAbstractFactorytoDecorator 2010-11-01T21:19:06+00:00 Mark Seemann <div id="post"> <p><a href="http://twopagesahead.com/">Garth Kidd</a> was so nice to <a href="http://twitter.com/#!/garthk/status/29404349448">point out to me</a> that I hadn't needed stop where I did in my <a href="/2010/11/01/RefactoringfromServiceLocatortoAbstractFactory">previous post</a>, and he is, of course, correct. Taking a dependency on an <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a> that doesn't take any contextual information (i.e. has no method parameters) is often an indication of a <a href="http://en.wikipedia.org/wiki/Leaky_abstraction">Leaky Abstraction</a>. It indicates that the consumer has knowledge about the dependency's lifetime that it shouldn't have.</p> <p>We can remove this flaw by introducing a <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> of the IRepository&lt;T&gt; interface. Something like this should suffice:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">FoundRepository</font></span>&lt;T&gt; : <span><font color="#2b91af">IRepository</font></span>&lt;T&gt;</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span> <span><font color="#0000ff">readonly</font></span> <span><font color="#2b91af">IRepository</font></span>&lt;T&gt; repository;</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> FoundRepository(<span><font color="#2b91af">IRepositoryFinder</font></span>&lt;T&gt; finder)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (finder == <span><font color="#0000ff">null</font></span>)</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span> <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">ArgumentNullException</font></span>(<span><font color="#a31515">"finder"</font></span>);</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.repository = finder.FindRepository();</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; }</font> <font style="font-size: 10pt">&nbsp;</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span><font style="font-size: 10pt" color="#008000">/* Implement IRepository&lt;T&gt; by delegating to</font></span> <span><font style="font-size: 10pt" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp; * this.repository */</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>This means that we can change the implementation of MyServiceOperation to this:</p> <p> <pre style="margin: 0px"><span><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span><font color="#0000ff">void</font></span> MyServiceOperation(</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">IRepository</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt; repository)</font> <font style="font-size: 10pt">{</font> <font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; </font><span><font style="font-size: 10pt" color="#008000">// ...</font></span> <font style="font-size: 10pt">}</font></pre> </p> <p>This is much better, but this requires a couple of notes.</p> <p>First of all we should keep in mind that since FoundRepository creates and saves an instance of IRepository right away, we should control the lifetime of FoundRepository. In essense, the lifetime should be tied to the specific service operation. Two concurrent invocations of MyServiceOperation should each receive separate instances of FoundRepository.</p> <p>Many DI containers support Factory methods, so it may not even be necessary to implement FoundRepository explicitly. Rather, it would be possible to register IRepository&lt;T&gt; so that an instance is always created by invoking IRepositoryFinder&lt;T&gt;.FindRepository().</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="dc73811df8b242b5a68c15d3517c9d5f"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#dc73811df8b242b5a68c15d3517c9d5f">#</a></div> <div class="comment-content">Mark, doesn't this contradict the rule to make constructors that get dependencies injected simple? You can't really know what happens inside finder.FindRepository() - it might even access the database...<br> <br> I am asking, because I have a similar situation:<br> In the project I am currently refactoring, I created a lot of ConfiguredXYZ decorators that are modeled after this article.<br> They decorate classes, that have both dependencies and primitive types as ctor parameters. I use the decorator to read these primitive values from the configuration and to create an instance of the decorated class.<br> <br> Example:<br> <br> <br> public ConfiguredCache(IConfigService configService, ISomeOtherDependency dep)<br> {<br> var expiryTime = configService.Get&lt;DateTime&gt;(&quot;ExpiryTimeForCache&quot;);<br> _configuredCache = new ExpiringCache(expiryTime, dep);<br> }<br> <br> If IConfigService is implemented to read the database, I will hit the database in the constructor.<br> <br> What do you think about it? Wouldn't it be better to create the instance of the ExpiringCache only when someone really needs it by invoking a method on ConfiguredCache?</div> <div class="comment-date">2011-08-10 16:36 UTC</div> </div> <div class="comment" id="bbe20622c95745efb577733dd8acabea"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#bbe20622c95745efb577733dd8acabea">#</a></div> <div class="comment-content">Agreed, but this is just one step in a refactoring process. What I would subsequently tend to do would be to refactor once more and let the Composition Root invoke the factory, which means that the consumer can now take only the result of invoking the factory as a single dependency.<br> <br> You could also go the other way and perform a lazy evaluation of the dependency, but this is far more complicated to implement because it means that you'd be changing the state of the consumer. This again means that if you want to share the consumer as another dependency, you'll also need to think about thread safety.<br> <br> In most cases I'd regard lazy evaluation as a premature optimization. As I have explained in another blog post, we <a href="/2011/03/04/Composeobjectgraphswithconfidence">shouldn't worry about the cost of composing an object graph</a>.<br> <br> In your example, IConfigService isn't really a dependency because the ConfiguredCache class doesn't depend on it - it only use it to get an instance of ExpiringCache. Change the constructor by removing the IConfigService and instead require an ExpiringCache (or the interface it implements). Some third party can take care of wiring up the IConfigService for you.<br> <br> So what if it hits the database? It's probably going to do that sooner or later (meaning, typically, milliseconds or even seconds later) so does it matter? And will it happen often? A cache is only effective if it's shared, which means that we can share it with the Singleton lifetime. If we do that we only need to create the instance once per application, which again means that in the overall picture, that single database hit is insignificant.</div> <div class="comment-date">2011-08-10 17:45 UTC</div> </div> <div class="comment" id="3ebe852ee3eb44feb989fa9cdef7e81d"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#3ebe852ee3eb44feb989fa9cdef7e81d">#</a></div> <div class="comment-content">Thanks for your response.<br> Could you write a blog post about that refactoring step you would perform after you refactored to decorators?<br> <br> I agree with your points in general, but they don't solve the problem that lead to those decorators in the first place:<br> The configuration of ExpiringCache is complex - not in the sample code, but in the real code. That was the whole reason for introducing those ConfiguredXYZ decorators, because I didn't want to have that complex configuration in the composition root. If you wouldn't create these decorators, how would you do it then? Create an ICache factory that returns a new ExpiringCache? This factory again would need IConfigService and ISomeOtherDependency to work, so from the constructor it would be the same as with the decorator...<br> And how would you register this factory with the container while reserving auto-wiring? Would you use Autofac's RegisterAdapter to register from ICacheFactory to ICache using the Create method of ICacheFactory?<br> </div> <div class="comment-date">2011-08-15 13:33 UTC</div> </div> <div class="comment" id="4f41978d666b434eae1be30a9c4c7b1d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4f41978d666b434eae1be30a9c4c7b1d">#</a></div> <div class="comment-content">Why not perform the complex configuration in the Composition Root?</div> <div class="comment-date">2011-08-15 13:39 UTC</div> </div> <div class="comment" id="3bcee30f17ac487bb1df6e5f17ca7d9c"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#3bcee30f17ac487bb1df6e5f17ca7d9c">#</a></div> <div class="comment-content">Because I would like to keep it simple.<br> If I would perform this complex configuration in the Composition Root, it would become a monstrosity, because it would mean to put the other configurations in there, too.<br> I like my Composition Root &quot;to just work&quot;, so I work with conventions. Putting the configurations in there would make this impossible...</div> <div class="comment-date">2011-08-16 07:26 UTC</div> </div> <div class="comment" id="c920c444d23642baaa4aad3112809537"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c920c444d23642baaa4aad3112809537">#</a></div> <div class="comment-content">Why would it make it impossible?<br> <br> The code that reads configuration etc. needs to go somewhere. The Composition Root is the correct place because it makes the rest of your application code decoupled from your configuration source(s).<br> <br> There's nothing that prevents you from expressing conventions around configuration settings as well.</div> <div class="comment-date">2011-08-16 07:39 UTC</div> </div> <div class="comment" id="a9eef00c5e2e4cc393b6a6fdae4d020f"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#a9eef00c5e2e4cc393b6a6fdae4d020f">#</a></div> <div class="comment-content">I don't think I understand. If I would put this configuration code inside the composition root it would be several hundred lines long. Maybe even more. I seem to be missing something, because a composition root like this is a maintenance nightmare.</div> <div class="comment-date">2011-08-16 09:25 UTC</div> </div> <div class="comment" id="5744a07b37064eeb9b0e2bd079e82096"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5744a07b37064eeb9b0e2bd079e82096">#</a></div> <div class="comment-content">Would the configuration code be <em>longer</em> if you put it in the Composition Root? If that's the case, I can't for the life of me imagine why this would be, so please explain.</div> <div class="comment-date">2011-08-16 09:39 UTC</div> </div> <div class="comment" id="5ebd15db9931451cba0492216f81cef8"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#5ebd15db9931451cba0492216f81cef8">#</a></div> <div class="comment-content">No, it wouldn't be longer if I would put it in the Composition Root. That would be very odd, I agree.<br> But: the configuration code that currently is spread out in several classes would all be in the composition root. Note: &quot;the configuration code&quot; really consists of several classes that are not related to each other. One class configures the SAP object (which SAP server to use, username, password etc.), another configures the cache (see example above), yet another configures the Logger (which file to write to) etc.<br> It might well be a problem with the architecture per se, because I came to what I have now from an architecture that was centered around a service locator. All the lower levels now are very good, they get dependencies injected and work with them, but I can't figure out, where to put this configuration code.<br> Because, in my opinion, this configuration code is &quot;domain code&quot;. What I mean with this is that it requires special knowledge about the domain it is used in. For example, I don't think that the composition root should need to know about details of an SAP connection, just to configure the SapConnection class... This is more than just reading configuration values and passing them to a constructor. In the SAP example, it reads some values from the database and based on these values it makes some decisions and only after that, the object is created.<br> Having written it like this, it really sounds as if I should use Abstract Factories, but you objected to them in one of your earlier answers, which confuses me :-)</div> <div class="comment-date">2011-08-16 10:05 UTC</div> </div> <div class="comment" id="d5120db3e36d47f2979835773ebf85f2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d5120db3e36d47f2979835773ebf85f2">#</a></div> <div class="comment-content">The Composition Root doesn't have to be a single class. It's an architectural concept. It can have as many classes as you need as long as the stay together. Usually we implement the Composition Root in the same project as the application's entry point. This is where we wire everything together, so this particular place tends to not be (unit) testable. As such, we should go to great lengths to ensure that the entry point is a Humble Executable.<br> <br> Thus it follows that the Composition Root can be as large as necessary, but it must contain no logic. My personal rule of thumb is that all members must have a cyclomatic complexity of 1.<br> <br> Configuration isn't logic, but you may have application logic that depends on configuration values, so the trick is to separate the logic from the source of the values. The logic goes elsewhere, but the values are being dehydrated (or otherwise initialized) by the Composition Root.<br> <br> I would consider a SAP connection an infrastructure concern. If you base your domain logic on knowledge of infrastructure specifics you're making life hard for yourself.</div> <div class="comment-date">2011-08-16 10:40 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Refactoring from Service Locator to Abstract Factory https://blog.ploeh.dk/2010/11/01/RefactoringfromServiceLocatortoAbstractFactory 2010-11-01T19:43:24+00:00 Mark Seemann <div id="post"> <p> One of the readers of <a href="http://amzn.to/12p90MG">my book</a> recently asked me an interesting question that relates to the disadvantages of the <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator</a> anti-pattern. I found both the question and the potential solution so interesting that I would like to share it. </p> <p> In short, the reader's organization currently uses Service Locator in their code, but don't really see a way out of it. This post demonstrates how we can refactor from Service Locator to <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a>. Here's the original question: </p> <blockquote> <p> "We have been writing a WCF middle tier using DI </p> <p> "Our application talks to multiple databases.&nbsp; There is one Global database which contains Enterprise records, and each Enterprise has the connection string of a corresponding Enterprise database. </p> <p> "The trick is when we want to write a service which connects to an Enterprise database.&nbsp; The context for which enterprise we are dealing with is not available until one of the service methods is called, so what we do is this: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">void</font></span> MyServiceOperation(<br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">EnterpriseContext</font></span> context)<br>{<br>&nbsp;&nbsp;&nbsp; </font><font face="Consolas"><font color="#008000"><span>/* Get a Customer repository operating</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * in the given enterprise's context</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * (database) */</span></font><br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> customerRepository = <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.FindRepository&lt;<span><font color="#2b91af">Customer</font></span>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.EnterpriseId);<br>&nbsp;&nbsp;&nbsp; <span><font color="#008000">// ...</font></span><br>}</font></pre> </p> <p> "I'm not sure how, in this case, we can turn what we've got into a more pure DI system, since we have the dependency on the EnterpriseContext passed in to each service method.&nbsp; We are mocking and testing just fine, and seem reasonably well decoupled.&nbsp; Any ideas?" </p> </blockquote> <p> When we look at the FindRepository method we <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">quickly find that it's a Service Locator</a>. There are <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">many problems with Service Locator</a>, but the general issue is that the generic argument can be one of an unbounded set of types. </p> <p> The problem is that seen from the outside, the consuming type (MyService in the example) doesn't advertise its dependencies. In the example the dependency is a CustomerRepository, but you could later go into the implementation of MyServiceOperation and change the call to context.FindRepository&lt;Qux&gt;(context.EnterpriseId) and everything would still compile. However, at run-time, you'd likely get an exception. </p> <p> It would be much safer to use an Abstract Factory, but how do we get there from here, and will it be better? </p> <p> Let's see how we can do that. First, we'll have to make some assumptions on how EnterpriseContext works. In the following, I'll assume that it looks like this - warning: it's ugly, but that's the point, so don't give up reading just yet: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">class</font></span>&nbsp;<span><font color="#2b91af">EnterpriseContext</font></span><br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span>&nbsp;<span><font color="#0000ff">readonly</font></span>&nbsp;<span><font color="#0000ff">int</font></span> enterpriseId;<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span>&nbsp;<span><font color="#0000ff">readonly</font></span>&nbsp;<span><font color="#2b91af">IDictionary</font></span>&lt;<span><font color="#0000ff">int</font></span>, <span><font color="#0000ff">string</font></span>&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connectionStrings;<br> <br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span> EnterpriseContext(<span><font color="#0000ff">int</font></span> enterpriseId)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.enterpriseId = enterpriseId;<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.connectionStrings =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">new</font></span>&nbsp;<span><font color="#2b91af">Dictionary</font></span>&lt;<span><font color="#0000ff">int</font></span>, <span><font color="#0000ff">string</font></span>&gt;();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.connectionStrings[1] = <span><font color="#a31515">"Foo"</font></span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.connectionStrings[2] = <span><font color="#a31515">"Bar"</font></span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.connectionStrings[3] = <span><font color="#a31515">"Baz"</font></span>;<br>&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">virtual</font></span>&nbsp;<span><font color="#0000ff">int</font></span> EnterpriseId<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">get</font></span> { <span><font color="#0000ff">return</font></span>&nbsp;<span><font color="#0000ff">this</font></span>.enterpriseId; }<br>&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">virtual</font></span>&nbsp;<span><font color="#2b91af">IRepository</font></span>&lt;T&gt; FindRepository&lt;T&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">int</font></span> enterpriseId)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (<span><font color="#0000ff">typeof</font></span>(T) == <span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">Customer</font></span>))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> (<span><font color="#2b91af">IRepository</font></span>&lt;T&gt;)<span><font color="#0000ff">this</font></span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .FindCustomerRepository(enterpriseId);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (<span><font color="#0000ff">typeof</font></span>(T) == <span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">Campaign</font></span>))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> (<span><font color="#2b91af">IRepository</font></span>&lt;T&gt;)<span><font color="#0000ff">this</font></span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .FindCampaignRepository(enterpriseId);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">if</font></span> (<span><font color="#0000ff">typeof</font></span>(T) == <span><font color="#0000ff">typeof</font></span>(<span><font color="#2b91af">Product</font></span>))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span> (<span><font color="#2b91af">IRepository</font></span>&lt;T&gt;)<span><font color="#0000ff">this</font></span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .FindProductRepository(enterpriseId);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">throw</font></span>&nbsp;<span><font color="#0000ff">new</font></span>&nbsp;<span><font color="#2b91af">InvalidOperationException</font></span>(<span><font color="#a31515">"..."</font></span>);<br>&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span>&nbsp;<span><font color="#2b91af">IRepository</font></span>&lt;<span><font color="#2b91af">Campaign</font></span>&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FindCampaignRepository(<span><font color="#0000ff">int</font></span> enterpriseId)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> cs = <span><font color="#0000ff">this</font></span>.connectionStrings[enterpriseId];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span>&nbsp;<span><font color="#0000ff">new</font></span>&nbsp;<span><font color="#2b91af">CampaignRepository</font></span>(cs);<br>&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span>&nbsp;<span><font color="#2b91af">IRepository</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FindCustomerRepository(<span><font color="#0000ff">int</font></span> enterpriseId)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> cs = <span><font color="#0000ff">this</font></span>.connectionStrings[enterpriseId];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span>&nbsp;<span><font color="#0000ff">new</font></span>&nbsp;<span><font color="#2b91af">CustomerRepository</font></span>(cs);<br>&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">private</font></span>&nbsp;<span><font color="#2b91af">IRepository</font></span>&lt;<span><font color="#2b91af">Product</font></span>&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FindProductRepository(<span><font color="#0000ff">int</font></span> enterpriseId)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> cs = <span><font color="#0000ff">this</font></span>.connectionStrings[enterpriseId];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span>&nbsp;<span><font color="#0000ff">new</font></span>&nbsp;<span><font color="#2b91af">ProductRepository</font></span>(cs);<br>&nbsp;&nbsp;&nbsp; }<br>}</font></pre> </p> <p> That's pretty horrible, but that's exactly the point. Every time we need to add a new type of repository, we'll need to modify this class, so it's one big violation of the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>. </p> <blockquote> <p> I didn't implement EnterpriseContext with a DI Container on purpose. Yes: using a DI Container would make it <em>appear</em> less ugly, but it would only <em>hide</em> the design issue - not address it. I chose the above implementation to demonstrate just how ugly this sort of design really is. </p> </blockquote> <p> So, let's start refactoring. </p> <h3 id="a770ac87d7bc4a0d9ed726f9edac6dc9"> Step 1 <a href="#a770ac87d7bc4a0d9ed726f9edac6dc9" title="permalink">#</a> </h3> <p> We change <em>each</em> of the private finder methods to public methods. </p> <blockquote> <p> In this example, there are only three methods, but I realize that in a real system there might be many more. However, we'll end up with only a single interface and its implementation, so don't despair just yet. It'll turn out just fine. </p> </blockquote> <p> As a single example the FindCustomerRepository method is shown here: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#2b91af">IRepository</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt;<br>&nbsp;&nbsp;&nbsp; FindCustomerRepository(<span><font color="#0000ff">int</font></span> enterpriseId)<br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> cs = <span><font color="#0000ff">this</font></span>.connectionStrings[enterpriseId];<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span>&nbsp;<span><font color="#0000ff">new</font></span>&nbsp;<span><font color="#2b91af">CustomerRepository</font></span>(cs);<br>}</font></pre> </p> <p> For <em>each</em> of the methods we extract an interface, like this: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">interface</font></span>&nbsp;<span><font color="#2b91af">ICustomerRepositoryFinder</font></span><br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">int</font></span> EnterpriseId { <span><font color="#0000ff">get</font></span>; }<br> <br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">IRepository</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt; FindCustomerRepository(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">int</font></span> enterpriseId);<br>}</font></pre> </p> <p> We also include the EnterpriseId property because we'll need it soon. This is just an intermediary artifact which is not going to survive until the end. </p> <blockquote> <p> This is very reminiscent of the steps described by <a href="http://www.udidahan.com/">Udi Dahan</a> in his excellent talk <a href="http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan">Intentions &amp; Interfaces: Making patterns concrete</a>. We make the <em>roles</em> of finding repositories explicit. </p> </blockquote> <p> This leaves us with three distinct interfaces that EnterpriseContext can implement: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">class</font></span>&nbsp;<span><font color="#2b91af">EnterpriseContext</font></span> : <br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">ICampaignRepositoryFinder</font></span>,<br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">ICustomerRepositoryFinder</font></span>,<br>&nbsp;&nbsp;&nbsp; <span><font style="font-size: 12pt" color="#2b91af">IProductRepositoryFinder</font></span></font></pre> </p> <p> Until now, we haven't touched the service. </p> <h3 id="93bdf1ab0b7947768559ce2ca13d4b80"> Step 2 <a href="#93bdf1ab0b7947768559ce2ca13d4b80" title="permalink">#</a> </h3> <p> We can now change the implementation of MyServiceOperation to explicitly require only the role that it needs: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">void</font></span> MyServiceOperation(<br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">ICustomerRepositoryFinder</font></span> finder)<br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> customerRepository = <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finder.FindCustomerRepository(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finder.EnterpriseId);<br>}</font></pre> </p> <p> Since we now only consume the strongly typed role interfaces, we can now delete the original FindRepository&lt;T&gt; method from EnterpriseContext. </p> <h3 id="80bb90665ad64076a037f5e1e566fa2f"> Step 3 <a href="#80bb90665ad64076a037f5e1e566fa2f" title="permalink">#</a> </h3> <p> At this point, we're actually already done, since <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">ICustomerRepositoryFinder is an Abstract Factory</a>, but we can make the API even better. When we consider the implementation of MyServiceOperation, it should quickly become clear that there's a sort of local <a href="http://c2.com/cgi/wiki?FeatureEnvySmell">Feature Envy</a> in play. Why do we need to access finder.EnterpriseId to invoke finder.FindCustomerRepository? Shouldn't it rather be the finder's own responsibility to figure that out for us? </p> <p> Instead, let us change the implementation so that the method does not need the enterpriseId parameter: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#2b91af">IRepository</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt; FindCustomerRepository()<br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> cs = <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">this</font></span>.connectionStrings[<span><font color="#0000ff">this</font></span>.EnterpriseId];<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">return</font></span>&nbsp;<span><font color="#0000ff">new</font></span>&nbsp;<span><font color="#2b91af">CustomerRepository</font></span>(cs);<br>}</font></pre> </p> <p> Notice that the EnterpriseId can be accessed just as well from the implementation of the method itself. This change requires us to also change the interface: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">interface</font></span>&nbsp;<span><font color="#2b91af">ICustomerRepositoryFinder</font></span><br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">IRepository</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt; FindCustomerRepository();<br>}</font></pre> </p> <p> Notice that we removed the EnterpriseId property, as well as the enterpriseId parameter. The fact that there's an enterprise ID in play is now an implementation detail. </p> <p> MyServiceOperation now looks like this: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">void</font></span> MyServiceOperation(<br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">ICustomerRepositoryFinder</font></span> finder)<br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> customerRepository = <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finder.FindCustomerRepository();<br>}</font></pre> </p> <p> This takes care of the Feature Envy smell, but still leaves us with a lot of very similarly looking interfaces: ICampaignRepositoryFinder, ICustomerRepositoryFinder and IProductRepositoryFinder. </p> <h3 id="51476011fa5b42b3b2970cd1d9b0d97b"> Step 4 <a href="#51476011fa5b42b3b2970cd1d9b0d97b" title="permalink">#</a> </h3> <p> We can collapse all the very similar interfaces into a single generic interface: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">interface</font></span>&nbsp;<span><font color="#2b91af">IRepositoryFinder</font></span>&lt;T&gt;<br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">IRepository</font></span>&lt;T&gt; FindRepository();<br>}</font></pre> </p> <p> With that, MyServiceOperation now becomes: </p> <p> <pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">void</font></span> MyServiceOperation(<br>&nbsp;&nbsp;&nbsp; <span><font color="#2b91af">IRepositoryFinder</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt; finder)<br>{<br>&nbsp;&nbsp;&nbsp; <span><font color="#0000ff">var</font></span> customerRepository = <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finder.FindRepository();<br>}</font></pre> </p> <p> Now that we only have a single generic interface (which is still an Abstract Factory), we can seriously consider getting rid of all the very similarly looking implementations in EnterpriseContext and instead just create a single generic class. We now have a more explicit API that better communicates intent. </p> <p> How is this better? What if a method needs both an IRepository&lt;Customer&gt; and an IRepository&lt;Product&gt;? We'll now have to pass two parameters instead of one. </p> <p> Yes, but that's good because it explicitly calls to your attention exactly which collaborators are involved. With the original Service Locator, you might not notice the responsibility creep as you over time request more and more repositories from the EnterpriseContext. With Abstract Factories in play, violations of the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> (SRP) becomes much more obvious. </p> <blockquote> <p> Refactoring from Service Locator to Abstract Factories make it more painful to violate the SRP. </p> </blockquote> <p> You can always make roles explicit to get rid of Service Locators. This is likely to result in a more explicit design where doing the right thing feels more natural than doing the wrong thing. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Pattern Recognition: Abstract Factory or Service Locator? https://blog.ploeh.dk/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator 2010-11-01T12:31:53+00:00 Mark Seemann <div id="post"> <p>It's easy to confuse the <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a> pattern with the <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator</a> anti-pattern - particularly so when generics or contextual information is involved. However, it's really easy to distinguish between there two, and here's how!</p> <p>Here are both (anti-)patterns in condensed form opposite each other:</p> <table border="1" cellspacing="0" cellpadding="2" width="400"> <tbody> <tr> <td valign="top" width="200">Abstract Factory</td> <td valign="top" width="200">Service Locator</td></tr> <tr> <td valign="top" width="200"><pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">interface</font></span>&nbsp;<span><font color="#2b91af">IFactory</font></span>&lt;T&gt;<br>{<br>&nbsp;&nbsp;&nbsp; T Create(<span><font color="#0000ff">object</font></span> context);<br>}</font></pre></td> <td valign="top" width="200"><pre><font face="Consolas"><span><font color="#0000ff">public</font></span>&nbsp;<span><font color="#0000ff">interface</font></span>&nbsp;<span><font color="#2b91af">IServiceLocator</font></span><br>{<br>&nbsp;&nbsp;&nbsp; T Create&lt;T&gt;(<span><font color="#0000ff">object</font></span> context);<br>}</font></pre></td></tr></tbody></table> <p>For these examples I chose to demonstrate both as generic interfaces that take some kind of contextual information (<em>context</em>) as input.</p> <blockquote> <p>In this example the context can be any object, but we could also have considered a more strongly typed <em>context</em> parameter. Other variations include more than one method parameter, or, in the degenerate case, no parameters at all.</p></blockquote> <p align="left">Both interfaces have a simple Create method that returns the generic type T, so it's easy to confuse the two. However, even for generic types, it's easy to tell one from the other:</p> <p>An <strong>Abstract Factory</strong> is a generic type, and the return type of the Create method is determined by the type of the factory itself. In other words, a constructed type can only return instances of a single type.</p> <p>A <strong>Service Locator</strong>, on the other hand, is a non-generic interface with a generic method. The Create method of a single Service Locator can return instances of an infinite number of types.</p> <p>Even simpler:</p> <blockquote> <p>An <strong>Abstract Factory</strong> is a generic type with a non-generic Create method; a <strong>Service Locator</strong> is a non-generic type with a generic Create method.</p></blockquote> <p>The name of the method, the number of parameters, and other circumstances may vary. The types may not be generic, or may be base classes instead of interfaces, but at the heart of it, the question is whether you can ask for an arbitrary type from the service, or only a single, static type.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1b560e92cc374793996b6530dcdb5a91"> <div class="comment-author"><a href="http://derickbailey.lostechies.com">Derick Bailey</a> <a href="#1b560e92cc374793996b6530dcdb5a91">#</a></div> <div class="comment-content">mmm... i generally agree and it's an easy way to create a simple rule set for those learning the patterns. but i'm having a hard time with this one for a couple of reasons.<br> <br> 1) i've always viewed an IoC container (whether it's used for dependency injection or service location) as an amalgamation of design pattern, including the abstract factory (along with builders, registries, and others)<br> <br> 2) a design pattern is not always identified by implementation detail, but by intent. take a look at the Decorator and Proxy design patterns as implemented in C#, for example. There are, quite often and in the most basic of implementations, very few differences between these two patterns when it comes down to the implementation. the real difference is the intent of use<br> <br> i don't think you are necessarily wrong in what you've said. i do think there's much more room for a gray area in between these two patterns, though. a service locator is an abstract factory (along with many other things), and an abstract factory can be used as a service locator. so, while the two patterns are distinct in their intent, they are often blurred and indistinguishable in implementation.</div> <div class="comment-date">2010-11-01 13:08 UTC</div> </div> <div class="comment" id="8e10a970a64744e199d1a874ecb9a8fa"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8e10a970a64744e199d1a874ecb9a8fa">#</a></div> <div class="comment-content">Good point about design patterns not always being identifiable by implementation details. That's not <em>quite</em> what I said, either... We can distinguish between the two from differences in the structure of their public APIs. That's not the implementation, but rather the shape, of the type. Still, point taken.<br> <br> However, the whole point is that there are fundamental differences between Abstract Factory and Service Locator. One is good, the other is evil. Learning to tell them apart is important.</div> <div class="comment-date">2010-11-01 20:23 UTC</div> </div> <div class="comment" id="6acad330d759404caff78a6cb5fd5724"> <div class="comment-author"><a href="http://twitter.com/BlackTigerX">Eber Irigoyen</a> <a href="#6acad330d759404caff78a6cb5fd5724">#</a></div> <div class="comment-content">&quot;An Abstract Factory is a generic type with a non-generic Create method&quot;<br> is it? looks to me like the Create method IS generic, it just so happens to have a constraint</div> <div class="comment-date">2010-11-14 04:08 UTC</div> </div> <div class="comment" id="53fe557cfc9b41dbababb92828e0da78"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#53fe557cfc9b41dbababb92828e0da78">#</a></div> <div class="comment-content">True, but I'm sure you understand what is meant :)</div> <div class="comment-date">2010-11-14 08:10 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Convention-based Customizations with AutoFixture https://blog.ploeh.dk/2010/10/19/Convention-basedCustomizationswithAutoFixture 2010-10-19T19:46:53+00:00 Mark Seemann <div id="post"> <p>As previous posts have described, <a href="http://autofixture.codeplex.com/">AutoFixture</a> creates <a href="http://blogs.msdn.com/b/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous Variables</a> based on the notion of being able to always hit within a well-behaved <a href="http://xunitpatterns.com/equivalence%20class.html">Equivalence Class</a> for a given type. This works well a lot of the time because AutoFixture has some sensible defaults: <a href="/2009/04/03/CreatingNumbersWithAutoFixture">numbers are positive integers</a> and <a href="/2009/04/02/CreatingStringsWithAutoFixture">strings are GUIDs</a>.</p> <p>This last part only works as long as strings are nothing but opaque blobs to the consuming class. This is, however, not an unreasonable assumption. Consider classes that implement Entities such as Person or Address. Strings will often take the form of FirstName, LastName, Street, etc. In all such cases, the <em>value</em> of the string usually doesn't matter.</p> <p>However, there will always be cases where the value of a string has a special meaning of its own. It will often be best to let <a href="/2009/05/01/DealingWithConstrainedInput">AutoFixture guide us towards a better API design</a>, but this is not always possible. Sometimes there are rules that constrain the formatting of a string.</p> <p>As an example, consider a Money class with this constructor:</p> <p> <pre><span style="color: blue">public</span> Money(<span style="color: blue">decimal</span> amount, <span style="color: blue">string</span> currencyCode)<br>{<br>&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (currencyCode == <span style="color: blue">null</span>)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span>&nbsp;<span style="color: blue">new</span>&nbsp;<span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"..."</span>);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (!<span style="color: #2b91af">CurrencyCodes</span>.IsValid(currencyCode))<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span>&nbsp;<span style="color: blue">new</span>&nbsp;<span style="color: #2b91af">ArgumentException</span>(<span style="color: #a31515">"..."</span>);<br>&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.amount = amount;<br>&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.currencyCode = currencyCode;<br>}</pre> </p> <p>Notice that the constructor only allows properly formatted currency codes (such as e.g. “DKK”, “USD”, “AUD”, etc.) through, while other strings will throw an exception. AutoFixture's default behavior of creating GUIDs as strings is problematic, as the Money constructor will throw on a GUID.</p> <p>We could attempt to fix this by changing the way AutoFixture generates strings in general, but that may not be the best solution as it may interfere with other string values. It is, however, easy to do:</p> <p> <pre>fixture.Inject(<span style="color: #a31515">"DKK"</span>);<br></pre> </p> <p>This simply injects the “DKK” string into the fixture, causing all subsequent strings to have the same value. However, a hypothetical Pizza class with Name and Description properties in addition to a Price property of type Money will now look like this:</p> <p> <pre>{<br>&nbsp; "Name": "DKK",<br>&nbsp; "Price": {<br>&nbsp;&nbsp;&nbsp; "Amount": 1.0,<br>&nbsp;&nbsp;&nbsp; "CurrencyCode": "DKK"<br>&nbsp; },<br>&nbsp; "Description": "DKK"<br>}</pre> </p> <p>What we really want is to customize only the currency code. This is where the extremely customizable architecture of AutoFixture can help us. As the <a href="http://autofixture.codeplex.com/wikipage?title=Object%20Creation%20Architecture">documentation</a> explains, lots of different request will flow through the kernel's Chain of Responsibility to create a Money instance. To populate the two parameters of the Money constructor, two ParameterInfo requests will be issued - one for each parameter. We can take advantage of this to create a custom ISpecimenBuilder that only addresses string parameters with the name <em>currencyCode</em>.</p> <p> <pre><span style="color: blue">public</span>&nbsp;<span style="color: blue">class</span>&nbsp;<span style="color: #2b91af">CurrencyCodeSpecimenBuilder</span> : <br>&nbsp;&nbsp;&nbsp; ISpecimenBuilder<br>{<br>&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span>&nbsp;<span style="color: blue">object</span> Create(<span style="color: blue">object</span> request,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISpecimenContext context)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pi = request <span style="color: blue">as</span>&nbsp;<span style="color: #2b91af">ParameterInfo</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (pi == <span style="color: blue">null</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span>&nbsp;<span style="color: blue">new</span>&nbsp;<span style="color: #2b91af">NoSpecimen</span>(request);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (pi.ParameterType != <span style="color: blue">typeof</span>(<span style="color: blue">string</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; || pi.Name != <span style="color: #a31515">"currencyCode"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span>&nbsp;<span style="color: blue">new</span>&nbsp;<span style="color: #2b91af">NoSpecimen</span>(request);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span>&nbsp;<span style="color: #a31515">"DKK"</span>;<br>&nbsp;&nbsp;&nbsp; }<br>}</pre> </p> <p>It simply examines the request to determine whether this is something that it should address at all. Only if the request is a ParameterInfo representing a string parameter named <em>currencyCode</em> do we deal with it. In any other case we return NoSpecimen, which simply tells AutoFixture that it should ask another ISpecimenBuilder instead.</p> <p>Here we just return the hard-coded string “DKK”, but we could easily have expanded the example to use a more varied generation algorithm. I will leave that, as well as how to generalize this in other ways, as exercises to the reader.</p> <p>With CurrencyCodeSpecimenBuilder available, we can add it to the Fixture like this:</p> <p> <pre>fixture.Customizations.Add(<br>&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span>&nbsp;<span style="color: #2b91af">CurrencyCodeSpecimenBuilder</span>());</pre> </p> <p>With this customization added, a Pizza instance now looks like this:</p> <p> <pre>{<br>&nbsp; "Name": "namec6b7a923-ea78-4817-9e24-a6863a597645",<br>&nbsp; "Price": {<br>&nbsp;&nbsp;&nbsp; "Amount": 1.0,<br>&nbsp;&nbsp;&nbsp; "CurrencyCode": "DKK"<br>&nbsp; },<br>&nbsp; "Description": "Description63ef17d7-876d-46d8-af73-1ed91f83e699"<br>}</pre> </p> <p>Notice how only the currency code is affected while all other string values are created by the default algorithm.</p> <p>In a nutshell, a custom ISpecimenBuilder can be used to implement all sorts of custom conventions for AutoFixture. The one shown here applies the string “DKK” to all string parameters named <em>currencyCode</em>. This mean that the convention isn't necessarily constrained to the Money constructor, but applies to all ParamterInfo instances that fit the specification.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="157e6effcab146f89ae209559a728c78"> <div class="comment-author"><a href="https://plus.google.com/u/0/102573495357485359266/about">Jon</a> <a href="#157e6effcab146f89ae209559a728c78">#</a></div> <div class="comment-content">I know this post is a couple of years old, and maybe AutoFixture has changed since it was written. However, in the above example for the CurrencyCodeSpecimenBuilder, it shows testing the request object to see if it is a ParameterInfo. This never worked when I tried to use it, however changing ParameterInfo to PropertyInfo and pi.ParameterType to pi.PropertyType fixed it for me (meaning that it actually hit found the property I was attempting to match). I'm just mentioning this in case it is useful to someone else who stumbles across this blog post.<br> <br> Thanks</div> <div class="comment-date">2012-11-28 20:28 UTC</div> </div> <div class="comment" id="52a80c65d77b43429e80617193aa3125"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#52a80c65d77b43429e80617193aa3125">#</a></div> <div class="comment-content">Jon, thanks for your comment.<br> <br> The reason why the CurrencyCodeSpecimenBuilder is looking for a ParameterInfo instance is because the thing it's looking for is exactly the constructor <em>parameter</em> to the Money class.<br> <br> If you instead want to match on a <em>property</em>, PropertyInfo is indeed the correct request to look for.<br> <br> (and FieldInfo is used if you want to match on a public field...)</div> <div class="comment-date">2012-11-28 20:36 UTC</div> </div> <div class="comment" id="a7a3c06f688e411394561f3a2a5f9313"> <div class="comment-author">WesM <a href="#a7a3c06f688e411394561f3a2a5f9313">#</a></div> <div class="comment-content">What kinds of things can a "request" be?</div> <div class="comment-date">2014-03-18 04:28 UTC</div> </div> <div class="comment" id="3a9d8f085fd248549c3ab8af52e9c70c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3a9d8f085fd248549c3ab8af52e9c70c">#</a></div> <div class="comment-content"> <p> A request <a href="https://github.com/AutoFixture/AutoFixture/wiki/Object-Creation-Architecture#requests">can be anything</a>, but will often by a Type, ParameterInfo, or PropertyInfo. </p> </div> <div class="comment-date">2014-03-18 18:08 UTC</div> </div> <div class="comment" id="4e377b36c6d74c979efda0b35e9d7649"> <div class="comment-author">WesM <a href="#4e377b36c6d74c979efda0b35e9d7649">#</a></div> <div class="comment-content">How would I get a new fixturized object from within a specimen builder? Like Fixture.Create&lt;datetime&gt;().ToUniversalTime() or some other customization based on your current implementation of random values.</div> <div class="comment-date">2014-03-21 10:38 UTC</div> </div> <div class="comment" id="993a945c6003480abede39ee3b9c0946"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#993a945c6003480abede39ee3b9c0946">#</a></div> <div class="comment-content"> <p> You can <a href="https://github.com/AutoFixture/AutoFixture/wiki/Object-Creation-Architecture#the-specimen-context">use the <code>context</code></a> argument passed to the Create method to resolve other values; you only need to watch out for infinite recursions: you can't ask for an unconditional string if the Specimen Builder you're writing handles unconditional strings. </p> </div> <div class="comment-date">2014-03-22 8:41 UTC</div> </div> <div class="comment" id="f27afdc745054de3835a909cf57092e2"> <div class="comment-author">Jeff Soper <a href="#f27afdc745054de3835a909cf57092e2">#</a></div> <div class="comment-content"> <p> I've written several custom ISpecimenBuilder implementations similar to your example above, and I always wince when testing for the ParameterType.Name value (i.e., if(pi.Name == "myParamName"){...}). It seems like it makes a test that would use this implementation very brittle - no longer would I have freedom to change the name of the paramter to suit my asthetics, without relying on a refactoring tool (cough, cough, Resharper, cough, cough) to hopefully pickup on the string value in my test suite and prompt me to change it there as well. </p> <p> This makes me think that I shouldn't need to be doing this, and that a design refactoring of my SUT would be a better option. Care to comment on this observation? Is there a common scenario/design trap that illustrates a better way? Or am I already in dangerous design territory based on my need to create an ISpecimenBuilder in the first place? </p> </div> <div class="comment-date">2014-04-30 19:38 UTC</div> </div> <div class="comment" id="7b91c7ea5eb44bf9bcaad4f170005286"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7b91c7ea5eb44bf9bcaad4f170005286">#</a></div> <div class="comment-content"> <p> Jeff, thank you for writing. Your question warranted <a href="/2014/05/01/autofixture-conventions-with-albedo">a new blog post</a>; it may not answer all of your concerns, but hopefully some of them. Read it and let me know if you still have questions. </p> </div> <div class="comment-date">2014-05-02 8:23 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Windows Azure Migration Sanity Check https://blog.ploeh.dk/2010/10/14/WindowsAzureMigrationSanityCheck 2010-10-14T11:08:27+00:00 Mark Seemann <div id="post"> <p>Recently I attended a workshop where we attempted to migrate an existing web application to Windows Azure. It was a worthwhile workshop that left me with a few key takeaways related to migrating applications to Windows Azure.</p> <p>The first and most important point is so self-evident that I seriously considered not publishing it. However, apparently it wasn't self-evident to all workshop participants, so someone else might also benefit from this general advice:</p> <p><em>Before migrating to Windows Azure, make sure that the application scales to at least two normal servers.</em></p> <p>It's as simple as that - still, lots of web developers never consider this aspect of application architecture.</p> <p>Why is this important in relation to Azure? The <a href="http://www.microsoft.com/windowsazure/sla/">Windows Azure SLA</a> only applies if you deploy two or more instances, which makes sense since the hosting servers occasionally need to be patched etc.</p> <p>Unless you don't care about the SLA, your application must be able to ‘scale' to at least two servers. If it can't, fix this issue first, before attempting to migrate to Windows Azure. You can test this locally by simply installing your application on two different servers and put them behind a load balancer (you can use virtual machines if you don't have the hardware). Only if it works consistently in this configuration should you consider deploying to Azure.</p> <p>Here are the most common issues that may prevent the application from ‘scaling':</p> <ul> <li>Keeping state in memory. If you must use session state, use one of the out-of-process session state store providers. <li>Using the file system for persistent storage. The file system is local to each server.</li></ul> <p>Making sure that the application ‘scales' to at least two servers is such a simple sanity check that it should go without saying, but apparently it doesn't.</p> <p>Please note that I put ‘scaling' in quotes here. An application that runs on only two servers has yet to prove that it's truly scalable, but that's another story.</p> <p>Also note that this sanity check in no way guarantees that the application will run on Azure. However, if the check fails, it most likely will not.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c60ad1fd1ce14bac95d04fdd757f4562"> <div class="comment-author">Thomas <a href="#c60ad1fd1ce14bac95d04fdd757f4562">#</a></div> <div class="comment-content">Hello Mark,<br> <br> I would also add somme CCC like cash. you may consider to move from a local cache to distributed one to save some unwanted behaviour.<br> <br> </div> <div class="comment-date">2010-10-17 20:25 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 2.0 Released https://blog.ploeh.dk/2010/10/10/AutoFixture2.0Released 2010-10-10T18:42:33+00:00 Mark Seemann <div id="post"> <p>It gives me great pleasure to announce the release of <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0. This release fixes a few minor issues since <a href="/2010/08/09/AutoFixture2.0beta1">beta 1</a>, but apart from that there are no significant changes.</p> <p>This is a major release compared to AutoFixture 1.1, featuring a completely new kernel, as well as new features and extensibility points. The <a href="/2010/08/09/AutoFixture2.0beta1">beta 1 announcement sums up the changes</a>. Get it <a href="http://autofixture.codeplex.com/releases/view/48806">here</a>.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoData Theories with AutoFixture https://blog.ploeh.dk/2010/10/08/AutoDataTheorieswithAutoFixture 2010-10-08T08:48:07+00:00 Mark Seemann <div id="post"> <p><a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0 comes with a new extension for <a href="http://xunit.codeplex.com/">xUnit.net</a> <a href="http://blog.benhall.me.uk/2008/01/introduction-to-xunitnet-extensions.html">data theories</a>. For those of us using xUnit.net, it can help make our unit tests more succinct and declarative.</p> <blockquote> <p>AutoFixture's support for xUnit.net is implemented in a separate assembly. AutoFixture itself has no dependency on xUnit.net, and if you use another unit testing framework, you can just ignore the existence of the Ploeh.AutoFixture.Xunit assembly.</p></blockquote> <p>Let's go back and revisit <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer">the previous test</a> we wrote using AutoFixture and its auto-mocking extension:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">Fact</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> AddWillPipeMapCorrectly() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Customize(<span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>()); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> basket = fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> mapMock = fixture.Freeze&lt;<span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pizza = fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.Add(pizza); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add)); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>Notice how all of the Fixture Setup phase is only used to create various objects that will be used in the test. First we create the fixture object, and then we use it to create four other objects. That turns out to be a pretty common idiom when using AutoFixture, so it's worthwhile to reduce the clutter if possible.</p> <p>With xUnit.net's excellent extensibility features, we can. AutoFixture 2.0 now includes the AutoDataAttribute in a separate assembly. AutoDataAttribute derives from xUnit.net's DataAttribute (just like InlineDataAttribute), and while we can use it as is, it becomes really powerful if we combine it with auto-mocking like this:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">AutoMoqDataAttribute</span> : <span style="color: #2b91af">AutoDataAttribute</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> AutoMoqDataAttribute() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: blue">base</span>(<span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Customize(<span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>())) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>This is a custom attribute that combines AutoFixture's two optional extensions for auto-mocking and xUnit.net support. With the AutoMoqDataAttribute in place, we can now rewrite the above test like this:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">Theory</span>, <span style="color: #2b91af">AutoMoqData</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> AddWillPipeMapCorrectly([<span style="color: #2b91af">Frozen</span>]<span style="color: #2b91af">Basket</span> basket, &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af">Frozen</span>]<span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt; mapMock, <span style="color: #2b91af">PizzaPresenter</span> pizza, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">BasketPresenter</span> sut) { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.Add(pizza); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add)); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>The AutoDataAttribute simply uses a Fixture object to create the objects declared in the unit tests parameter list. Notice that the test is no longer a [Fact], but rather a [Theory].</p> <p>The only slightly tricky thing to notice is that when we declare a parameter object, it will automatically map to a call to the CreateAnonymous method for the parameter type. However, when we need to invoke the Freeze method, we can add the [Frozen] attribute in front of the parameter.</p> <p>The best part about data theories is that they don't prevent us from writing normal unit tests in the same Test Class, and this carries over to the [AutoData] attribute. We can use it when it's possible, but for those more complex tests where we need to interact with a Fixture instance, we can still write a normal [Fact].</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f2c15e6bd9fa49e4b0810fe88ab7e927"> <div class="comment-author"><a href="http://colinbowern.com">Colin Bowern</a> <a href="#f2c15e6bd9fa49e4b0810fe88ab7e927">#</a></div> <div class="comment-content">I'm poking around at the AutoDataAttribute and I might be missing something but in a simple test I'm getting an InvalidOperationException:<br> <br> System.InvalidOperationException : No data found for XunitSample.TestClass.TestMethod2<br> System.InvalidOperationException : No data found for XunitSample.TestClass.TestMethod1<br> <br> Did I miss something in terms of setup? Sample code here - https://gist.github.com/1920943</div> <div class="comment-date">2012-02-27 02:49 UTC</div> </div> <div class="comment" id="6283cdb9645f4b0eb8e56520e03ca377"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6283cdb9645f4b0eb8e56520e03ca377">#</a></div> <div class="comment-content">I just copied in the code from your gist and added the appropriate NuGet packages, followed by an Add-BindingRedirect, and everything works as intended. In other words, I can't reproduce the problem based on the information given here. I'd love to help, but I don't think this is the correct forum. Could you ask on Stack Overflow instead? If so, I'll take a look.</div> <div class="comment-date">2012-02-27 06:57 UTC</div> </div> <div class="comment" id="368200661720420683be7040fca7c262"> <div class="comment-author">Brian McCord <a href="#368200661720420683be7040fca7c262">#</a></div> <div class="comment-content">I am very impressed with this way of doing things, but I have one question. If you need to do setup on a mock to force certain return values, are you forced to go back to using a Fact and setting up the mock manually?</div> <div class="comment-date">2012-03-02 17:00 UTC</div> </div> <div class="comment" id="02c274e3cbb94deca8bce62a0aeeacdb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#02c274e3cbb94deca8bce62a0aeeacdb">#</a></div> <div class="comment-content">Absolutely, that's what the [Frozen] attribute is for. <a href="https://github.com/ploeh/Booking/blob/master/BookingDomainModel.UnitTest/CapacityGateTests.cs">Here's an example (second test).</a></div> <div class="comment-date">2012-03-03 19:42 UTC</div> </div> <div class="comment" id="18d470cd3bb74c47978411a4d26ae54c"> <div class="comment-author">Jeff Soper <a href="#18d470cd3bb74c47978411a4d26ae54c">#</a></div> <div class="comment-content"> <p> Forgive me if I missed this point in your blog, but it seems that the parameter order is critical when injecting objects marked with the [Frozen] attribute into your test, if the SUT is being injected with that frozen object. </p> <p> I was refactoring my tests to make use of AutoData like you've shown here, and was adding my [Frozen]Mock&lt;IMySutDependency&gt; parameter willy-nilly to the end of the test parameters, *after* the SUT parameter. I was freezing it so that I could verify the same mocked dependency that was injected into the SUT as part of my test, or so I thought... </p> <p> void MySutTest(MySut sut, [Frozen]Mock&lt;IMySutDependency&gt; frozen) </p> <p> After a brief episode of confusion and self-loathing, I moved the frozen mocked dependency in front of the SUT in the test parameter list. Skies parted, angels sang, test passed again. </p> <p> void MySutTest([Frozen]Mock&lt;IMySutDependency&gt; frozen, MySut sut) </p> </div> <div class="comment-date">2013-08-22 23:00 UTC</div> </div> <div class="comment" id="8176c5f3f9d542b3b29e5f3da4854d94"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8176c5f3f9d542b3b29e5f3da4854d94">#</a></div> <div class="comment-content"> <p> Jeff, thank you for your comment. As you have discovered, the order of test parameters matter when you apply those 'hint' attributes, like <code>[Frozen]</code>. This is by design, because it enables you to generate one or more unique values of a data type <em>before</em> you freeze it. This could sometimes be important, although it's not something I do a lot. Keep in mind that the [Frozen] attribute wasn't designed exclusively with mocks in mind; it doesn't know anything about mocks - it just freeze a Type. </p> <p> In general, all of those 'hint' attributes apply an ICustomization, and they apply each ICustomization in the order of the arguments to which they are applied. <a href="/2012/07/31/TheorderofAutoFixtureCustomizationsmatter">The order of AutoFixture Customizations matter</a>. </p> </div> <div class="comment-date">2013-08-24 12:39 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Register Resolve Release pattern https://blog.ploeh.dk/2010/09/29/TheRegisterResolveReleasepattern 2010-09-29T11:50:02+00:00 Mark Seemann <div id="post"> <p>The subject of Dependency Injection (DI) in general, and DI Containers specifically, suffers from horrible terminology that seems to confuse a lot of people. Newcomers to DI often think about DI Containers as a sort of <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a> on steroids. It's not. Nicholas Blumhardt already realized and <a href="http://blogs.msdn.com/b/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx">described this phenomenon</a> a couple of years ago.</p> <p>The core of the matter is that as developers, we are extremely accustomed to thinking about components and services in terms of <a href="http://en.wikipedia.org/wiki/Command-query_separation">queries instead of commands</a>. However, the <a href="http://en.wikipedia.org/wiki/Hollywood_Principle">Hollywood Principle</a> insists that we should embrace a <em>tell, don't ask</em> philosophy. We can apply this principles to DI Containers as well: <a href="/2010/08/30/Dontcallthecontainer;itllcallyou">Don't call the container; it'l call you</a>.</p> <p>This leads us to what <a href="http://kozmic.pl/">Krzysztof Koźmic</a> calls the <a href="http://kozmic.net/2010/06/20/how-i-use-inversion-of-control-containers">three calls pattern</a>. Basically it states that we should only do three things with a DI Container:</p> <ol> <li>Bootstrap the container <li>Resolve root components <li>Dispose this container</li></ol> <p>This is very sound advice and independently of Krzysztof I've been doing something similar for years - so perhaps the <em>pattern</em> label is actually in order here.</p> <p>However, I think that the pattern deserves a more catchy name, so in the spirit of the <a href="http://c2.com/cgi/wiki?ArrangeActAssert">Arrange Act Assert</a> (AAA) pattern for unit testing I propose that we name it the <strong>Register Resolve Release</strong> (RRR) pattern. The names originate with Castle Windsor terminology, where we:</p> <ol> <li>Register components with the container <li>Resolve root components <li>Release components from the container</li></ol> <p>Other containers also support the RRR pattern, but if we were to pick their terminology, it would rather be the Configure GetInstance Dispose (CGD) pattern (or something similar), and that's just not as catchy.</p> <p>We can rewrite a <a href="/2010/08/30/Dontcallthecontainer;itllcallyou">previous example</a> with Castle Windsor and annotate it with comments to call out where the three container phases occur:</p> <p> <pre style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args) { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">WindsorContainer</span>(); &nbsp;&nbsp;&nbsp; container.Kernel.Resolver.AddSubResolver( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">CollectionResolver</span>(container.Kernel)); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">// Register</span> &nbsp;&nbsp;&nbsp; container.Register( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IParser</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">WineInformationParser</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IParser</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">HelpParser</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IParseService</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">CoalescingParserSelector</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IWineRepository</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">SqlWineRepository</span>&gt;(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IMessageWriter</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">ConsoleMessageWriter</span>&gt;()); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">// Resolve</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> ps = container.Resolve&lt;<span style="color: #2b91af">IParseService</span>&gt;(); &nbsp;&nbsp;&nbsp; ps.Parse(args).CreateCommand().Execute(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">// Release</span> &nbsp;&nbsp;&nbsp; container.Release(ps); &nbsp;&nbsp;&nbsp; container.Dispose(); }</pre> </p> <p>Notice that <a href="http://kozmic.net/2010/08/27/must-i-release-everything-when-using-windsor/">in most cases, explicitly invoking the Release method isn't necessary</a>, but I included it here to make the pattern stand out.</p> <p>So there it is: the <em>Register Resolve Release</em> pattern.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9a94cbb8e8c14193ba6224d2673647b9"> <div class="comment-author">Harry Dev <a href="#9a94cbb8e8c14193ba6224d2673647b9">#</a></div> <div class="comment-content">I agree completely and think the Register Resolve Release (RRR) moniker is very good. You should think about creating a wikipedia or c2 entry for it to promote it ;)</div> <div class="comment-date">2010-09-29 12:21 UTC</div> </div> <div class="comment" id="5361a80168654a25b8d9ddba250c7c2e"> <div class="comment-author"><a href="http://nblumhardt.com">Nicholas Blumhardt</a> <a href="#5361a80168654a25b8d9ddba250c7c2e">#</a></div> <div class="comment-content">Nice coinage, gets a vote from me.</div> <div class="comment-date">2010-10-03 21:28 UTC</div> </div> <div class="comment" id="40120e635a1a4c20a17de91bd21a1620"> <div class="comment-author">Arnis L. <a href="#40120e635a1a4c20a17de91bd21a1620">#</a></div> <div class="comment-content">This realization took me few months. It truly is quite hard for newcomers.</div> <div class="comment-date">2010-10-03 22:42 UTC</div> </div> <div class="comment" id="438dfb8b78b64e648317c57be995943e"> <div class="comment-author"><a href="http://www.truewill.net/myblog/index.php">Bill Sorensen</a> <a href="#438dfb8b78b64e648317c57be995943e">#</a></div> <div class="comment-content">You could use a using block, too.</div> <div class="comment-date">2010-10-07 17:03 UTC</div> </div> <div class="comment" id="a36026f627e7450d8333658ee899bd32"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a36026f627e7450d8333658ee899bd32">#</a></div> <div class="comment-content">Yes, although the general pattern is a bit more subtle than this. In the example given, the call to the Release method is largely redundant. If we assume that this is the case, a using block disposes the container as well.<br> <br> However, a using block invokes Dispose, but not Release. Releasing an object graph is conceptually very different from disposing the container. However, in the degenerate case shown here, there's not a lot of difference, but in a server scenario where we use the container to resolve an object graph per request, we resolve and release many object graphs all the time. In such scenarios we only dispose the container when the application itself recycles, and even then, we may never be given notice that this happens.</div> <div class="comment-date">2010-10-07 21:09 UTC</div> </div> <div class="comment" id="589f178f37a345b4bcd05e998562a8e1"> <div class="comment-author"><a href="http://httt://thoughtresults.com">Saeed Neamati</a> <a href="#589f178f37a345b4bcd05e998562a8e1">#</a></div> <div class="comment-content">Great name, I like it. RRR is easy to remember and makes sense to me perfectly. Thanks.</div> <div class="comment-date">2012-06-09 06:45 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Instrumentation with Decorators and Interceptors https://blog.ploeh.dk/2010/09/20/InstrumentationwithDecoratorsandInterceptors 2010-09-20T19:18:21+00:00 Mark Seemann <div id="post"> <p>One of my readers recently <a href="http://www.manning-sandbox.com/thread.jspa?forumID=607&amp;threadID=39542">asked me an interesting question</a>. It relates to <a href="http://amzn.to/12p90MG">my book</a>'s chapter about Interception (chapter 9) and Decorators and how they can be used for instrumentation-like purposes.</p> <p>In an earlier blog post we saw how we can <a href="/2010/04/07/DependencyInjectionisLooseCoupling">use Decorators to implement Cross-Cutting Concerns</a>, but the question relates to how a set of Decorators can be used to log additional information about code execution, such as the time before and after a method is called, the name of the method and so on.</p> <p>A <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> can excellently address such a concern as well, as we will see here. Let us first define an IRegistrar interface and create an implementation like this:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ConsoleRegistrar</span> : <span style="color: #2b91af">IRegistrar</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Register(<span style="color: #2b91af">Guid</span> id, <span style="color: blue">string</span> text) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> now = <span style="color: #2b91af">DateTimeOffset</span>.Now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"{0}\t{1:s}.{2}\t{3}"</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id, now, now.Millisecond, text); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>Although this implementation ‘logs' to the Console, I'm sure you can imagine other implementations. The point is that given this interface, we can add all sorts of ambient information such as the thread ID, the name of the current principal, the current culture and whatnot, while the text string variable still gives us an option to log more information. If we want a more detailed API, we can just make it more detailed - after all, the IRegistrar interface is just an example.</p> <p>We now know how to register events, but are seemingly no nearer to instrumenting an application. How do we do that? Let us see how we can instrument the <a href="/2010/02/02/RefactoringtoAggregateServices">OrderProcessor class</a> that I have described several times in past posts.</p> <p>At the place I left off, the OrderProcessor class uses Constructor Injection all the way down. <del datetime="2017-08-25T09:20:05.2082308+02:00">Although I would normally prefer using a DI Container to auto-wire it, </del>here's a manual composition using <a href="/2014/06/10/pure-di">Pure DI</a> just to remind you of the general structure of the class and its dependencies:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">OrderProcessor</span>( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">OrderValidator</span>(), &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">OrderShipper</span>(), &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">OrderCollector</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">AccountsReceivable</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">RateExchange</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">UserContext</span>()));</pre> </p> <p>All the dependencies injected into the OrderProcessor instance implement interfaces on which OrderProcessor relies. This means that we can decorate each concrete dependency with an implementation that instruments it.</p> <p>Here's an example that instruments the IOrderProcessor interface itself:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">InstrumentedOrderProcessor</span> : <span style="color: #2b91af">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IOrderProcessor</span> orderProcessor; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IRegistrar</span> registrar; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> InstrumentedOrderProcessor( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IOrderProcessor</span> processor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IRegistrar</span> registrar) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (processor == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"processor"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (registrar == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"registrar"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.orderProcessor = processor; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.registrar = registrar; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IOrderProcessor Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">SuccessResult</span> Process(<span style="color: #2b91af">Order</span> order) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> correlationId = <span style="color: #2b91af">Guid</span>.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.registrar.Register(correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span>.Format(<span style="color: #a31515">"Process begins ({0})"</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.orderProcessor.GetType().Name)); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> result = <span style="color: blue">this</span>.orderProcessor.Process(order); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.registrar.Register(correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span>.Format(<span style="color: #a31515">"Process ends&nbsp;&nbsp; ({0})"</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.orderProcessor.GetType().Name)); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> result; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>That looks like quite a mouthful, but it's really quite simple - the cyclomatic complexity of the Process method is as low as it can be: <em>1</em>. We really just register the Process method call before and after invoking the decorated IOrderProcessor.</p> <p>Without changing anything else than the composition itself, we can now instrument the IOrderProcessor interface:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> registrar = <span style="color: blue">new</span> <span style="color: #2b91af">ConsoleRegistrar</span>(); <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">InstrumentedOrderProcessor</span>( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">OrderProcessor</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">OrderValidator</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">OrderShipper</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">OrderCollector</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">AccountsReceivable</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">RateExchange</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">UserContext</span>())), &nbsp;&nbsp;&nbsp; registrar);</pre> </p> <p>However, imagine implementing an InstrumentedXyz for every IXyz and compose the application with them. It's possible, but it's going to get old really fast - not to mention that it massively violates the DRY principle.</p> <p>Fortunately we can solve this issue with any DI Container that supports dynamic interception. Castle Windsor does, so let's see how that could work.</p> <p>Instead of implementing the same code ‘template' over and over again to instrument an interface, we can do it once and for all with an interceptor. Imagine that we delete the InstrumentedOrderProcessor; instead, we create this:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">InstrumentingInterceptor</span> : <span style="color: #2b91af">IInterceptor</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IRegistrar</span> registrar; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> InstrumentingInterceptor(<span style="color: #2b91af">IRegistrar</span> registrar) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (registrar == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"registrar"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.registrar = registrar; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IInterceptor Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Intercept(<span style="color: #2b91af">IInvocation</span> invocation) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> correlationId = <span style="color: #2b91af">Guid</span>.NewGuid(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.registrar.Register(correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span>.Format(<span style="color: #a31515">"{0} begins ({1})"</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invocation.Method.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invocation.TargetType.Name)); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invocation.Proceed(); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.registrar.Register(correlationId, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span>.Format(<span style="color: #a31515">"{0} ends&nbsp;&nbsp; ({1})"</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invocation.Method.Name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invocation.TargetType.Name)); &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>If you compare this to the Process method of InstrumentedOrderProcessor (that we don't need anymore), you should be able to see that they are very similar. In this version, we just use the <em>invocation</em> argument to retrieve information about the decorated method.</p> <p>We can now add InstrumentingInterceptor to a WindsorContainer and enable it for all appropriate components. When we do that and invoke the Process method on the resolved IOrderProcessor, we get a result like this:</p> <p> <pre style="margin: 0px">bbb9724e-0fad-4b06-9bb0-b8c1c460cded&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.744&nbsp;&nbsp;&nbsp; Process begins (OrderProcessor) 43349d42-a463-463b-8ddf-e569e3170c97&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.745&nbsp;&nbsp;&nbsp; Validate begins (TrueOrderValidator) 43349d42-a463-463b-8ddf-e569e3170c97&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.745&nbsp;&nbsp;&nbsp; Validate ends&nbsp;&nbsp; (TrueOrderValidator) 44fdccc8-f12d-4057-ae03-791225686504&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.746&nbsp;&nbsp;&nbsp; Collect begins (OrderCollector) 8bbb1a0c-6134-4652-a4af-cd8c0c7184a0&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.746&nbsp;&nbsp;&nbsp; GetCurrentUser begins (UserContext) 8bbb1a0c-6134-4652-a4af-cd8c0c7184a0&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.747&nbsp;&nbsp;&nbsp; GetCurrentUser ends&nbsp;&nbsp; (UserContext) d54359ff-8c32-487f-8728-b19ff0bf4942&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.747&nbsp;&nbsp;&nbsp; GetCurrentUser begins (UserContext) d54359ff-8c32-487f-8728-b19ff0bf4942&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.747&nbsp;&nbsp;&nbsp; GetCurrentUser ends&nbsp;&nbsp; (UserContext) c54c4506-23a8-4553-ba9a-066fc64252d2&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.748&nbsp;&nbsp;&nbsp; GetSelectedCurrency begins (UserContext) c54c4506-23a8-4553-ba9a-066fc64252d2&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.748&nbsp;&nbsp;&nbsp; GetSelectedCurrency ends&nbsp;&nbsp; (UserContext) b3dba76b-6b4e-44fa-aca5-52b2d8509db3&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.750&nbsp;&nbsp;&nbsp; Convert begins (RateExchange) b3dba76b-6b4e-44fa-aca5-52b2d8509db3&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.751&nbsp;&nbsp;&nbsp; Convert ends&nbsp;&nbsp; (RateExchange) e07765bd-fe07-4486-96f1-f74d77241343&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.751&nbsp;&nbsp;&nbsp; Collect begins (AccountsReceivable) e07765bd-fe07-4486-96f1-f74d77241343&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.752&nbsp;&nbsp;&nbsp; Collect ends&nbsp;&nbsp; (AccountsReceivable) 44fdccc8-f12d-4057-ae03-791225686504&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.752&nbsp;&nbsp;&nbsp; Collect ends&nbsp;&nbsp; (OrderCollector) 231055d3-4ebb-425d-8d69-fb9c85d9a860&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.752&nbsp;&nbsp;&nbsp; Ship begins (OrderShipper) 231055d3-4ebb-425d-8d69-fb9c85d9a860&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.753&nbsp;&nbsp;&nbsp; Ship ends&nbsp;&nbsp; (OrderShipper) bbb9724e-0fad-4b06-9bb0-b8c1c460cded&nbsp;&nbsp;&nbsp; 2010-09-20T21:01:16.753&nbsp;&nbsp;&nbsp; Process ends&nbsp;&nbsp; (OrderProcessor)</pre> </p> <p>Notice how we care easily see where and when method calls begin and end using the descriptive text as well as the correlation id. I will leave it as an exercise for the reader to come up with an API that provides better parsing options etc.</p> <p>As a final note it's worth pointing out that this way of instrumenting an application (or part of it) can be done following the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>. I never changed the original implementation of any of the components.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. My future is Azure https://blog.ploeh.dk/2010/09/13/MyfutureisAzure 2010-09-13T20:08:07+00:00 Mark Seemann <div id="post"> <p>As some of my readers may already know, my (previous) employer Safewhere went out of business in August, so I started looking for something new to do. I believe that I have now found it.</p> <p>October 1st I'll start in my new role as Technical Lead at <a href="http://www.commentor.dk/">Commentor</a>, where it will be my responsibility to establish us as the leading Windows Azure center of expertise in Denmark (and beyond?). That's quite a big mouthful for me, but also something that I'm very excited about.</p> <p>This means that besides working on real Windows Azure projects with Danish customers, I also anticipate doing a lot of blogging, speaking and writing about Azure in the future.</p> <p>What does this mean for my many other endeavors, like <a href="http://amzn.to/12p90MG">my book</a>, <a href="http://autofixture.codeplex.com/">AutoFixture</a> or just blogging and speaking about TDD and DI in general? These things are something that I've always been doing mainly in my spare time, and I intend to keep doing that. Perhaps there are even better opportunities for synergy in my new line of work, but only time will tell.</p> <p>I'm really thrilled to be given the opportunity to expand in a slightly different direction. It'll be hard, but I'm sure it's also going to be a blast!</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8d1651d5a4464ca18a84a056aaa0dadc"> <div class="comment-author"><a href="http://twitter.com/BlackTigerX">Eber Irigoyen</a> <a href="#8d1651d5a4464ca18a84a056aaa0dadc">#</a></div> <div class="comment-content">good for Azure! :)</div> <div class="comment-date">2010-09-13 21:49 UTC</div> </div> <div class="comment" id="3e08b3b6104047d189b4d1654d765255"> <div class="comment-author">Ren&#233; L&#248;hde <a href="#3e08b3b6104047d189b4d1654d765255">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Congrats - great news for both Commmentor, Azure and you.<br> I hope to do a ton of shared Azure projects with you in the near future.<br> <br> -Ren&#233;</div> <div class="comment-date">2010-09-14 08:57 UTC</div> </div> <div class="comment" id="5ff955186aad48ac823af7a6507ca660"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5ff955186aad48ac823af7a6507ca660">#</a></div> <div class="comment-content">Thanks. You know where to find me :)</div> <div class="comment-date">2010-09-14 09:23 UTC</div> </div> <div class="comment" id="3e471fe5f8014dd2930fc8995e0013eb"> <div class="comment-author"><a href="http://codecontracts.info">David Allen</a> <a href="#3e471fe5f8014dd2930fc8995e0013eb">#</a></div> <div class="comment-content">From what I know of you from your blog and your excellent book, Commentor is lucky to have you. I am almost finished with your book Dependency Injection, and I am VERY impressed with it. I have put much of the knowledge to use on my current projects, which have made them much more testable. </div> <div class="comment-date">2010-09-17 01:51 UTC</div> </div> <div class="comment" id="112ce2e85f784e87b0d57eb196a5672e"> <div class="comment-author"><a href="http://www.janusknudsen.dk">Janus Knudsen</a> <a href="#112ce2e85f784e87b0d57eb196a5672e">#</a></div> <div class="comment-content">Good luck with that Mark, I'm sure you need it with Azure :)<br> <br> Regards from a true believer in &quot;What can Azure do for you&quot;</div> <div class="comment-date">2010-09-18 19:33 UTC</div> </div> <div class="comment" id="3b18b298eac14bf781447569d2936d04"> <div class="comment-author">Thomas <a href="#3b18b298eac14bf781447569d2936d04">#</a></div> <div class="comment-content">Mark,<br> <br> Congrats on a new job. I will be interesting to read about Azure. Glad to see that you won't get rid of TDD and DI ;)<br> <br> Thomas</div> <div class="comment-date">2010-10-05 20:08 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Don't call the container; it'll call you https://blog.ploeh.dk/2010/08/30/Dontcallthecontainer;itllcallyou 2010-08-30T20:06:58+00:00 Mark Seemann <div id="post"> <p>There still seems to be some confusion about what is <em>Dependency Injection</em> (DI) and what is a <em>DI Container</em>, so in this post I will try to sort it out as explicitly as possible.</p> <blockquote> <p>DI is a set of principles and patterns that enable loose coupling.</p></blockquote> <p>That's it; nothing else. Remember that old quote from p. 18 of <a href="http://en.wikipedia.org/wiki/Design_Patterns_%28book%29">Design Patterns</a>?</p> <blockquote> <p><em>Program to an interface; not an implementation.</em></p></blockquote> <p>This is the concern that DI addresses. The most useful DI pattern is <em>Constructor Injection</em> where we inject dependencies into consumers via their constructors. No container is required to do this.</p> <p>The easiest way to build a DI-friendly application is to just use Constructor Injection all the way. Conversely, <em>an application does not automatically become loosely coupled when we use a DI Container</em>. Every time application code <em>queries</em> a container we have an instance of the <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator anti-pattern</a>. The corollary leads to this variation of the <a href="http://en.wikipedia.org/wiki/Hollywood_Principle">Hollywood Principle</a>:</p> <blockquote> <p>Don't call the container; it'll call you.</p></blockquote> <p>A DI Container is a fantastic tool. It's like a (motorized) mixer: you can whip cream by hand, but it's easier with a mixer. On the other hand, without the cream the mixer is nothing. The same is true for a DI Container: to really be valuable, your code must employ Constructor Injection so that the container can <em>auto-wire</em> dependencies.</p> <p>A well-designed application adheres to the Hollywood Principle for DI Containers: it doesn't call the container. On the other hand, we can use the container to compose the application - or we can do it the hard way; this is called <em>Poor Man's DI</em>. Here's an example that uses Poor Man's DI to compose a complete application graph in a console application:</p> <p> <pre style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args) { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> msgWriter = <span style="color: blue">new</span> <span style="color: #2b91af">ConsoleMessageWriter</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">CoalescingParserSelector</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">IParser</span>[] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">HelpParser</span>(msgWriter), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">WineInformationParser</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">SqlWineRepository</span>(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msgWriter) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Parse(args) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .CreateCommand() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Execute(); }</pre> </p> <p>Notice how the nested structure of all the dependencies gives you an almost visual idea about the graph. What we have here is Constructor Injection all the way in.</p> <p>CoalescingParserSelector's constructor takes an IEnumerable&lt;IParser&gt; as input. Both HelpParser and WineInformationParser requires an IMessageWriter, and WineInformationParser also an IWineRepository. We even pull in types from different assemblies because SqlWineRepository is defined in the SQL Server-based data access assembly.</p> <p>Another thing to notice is that the <em>msgWriter</em> variable is shared among two consumers. This is what a DI Container normally addresses with its ability to manage <em>component lifetime</em>. Although there's not a DI Container in sight, we could certainly benefit from one. Let's try to wire up the same graph using <a href="http://unity.codeplex.com/">Unity</a> (just for kicks):</p> <p> <pre style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args) { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">UnityContainer</span>(); &nbsp;&nbsp;&nbsp; container.RegisterType&lt;<span style="color: #2b91af">IParser</span>, <span style="color: #2b91af">WineInformationParser</span>&gt;(<span style="color: #a31515">"parser.info"</span>); &nbsp;&nbsp;&nbsp; container.RegisterType&lt;<span style="color: #2b91af">IParser</span>, <span style="color: #2b91af">HelpParser</span>&gt;(<span style="color: #a31515">"parser.help"</span>); &nbsp;&nbsp;&nbsp; container.RegisterType&lt;<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">IParser</span>&gt;, <span style="color: #2b91af">IParser</span>[]&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; container.RegisterType&lt;<span style="color: #2b91af">IParseService</span>, <span style="color: #2b91af">CoalescingParserSelector</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; container.RegisterType&lt;<span style="color: #2b91af">IWineRepository</span>, <span style="color: #2b91af">SqlWineRepository</span>&gt;(); &nbsp;&nbsp;&nbsp; container.RegisterType&lt;<span style="color: #2b91af">IMessageWriter</span>, <span style="color: #2b91af">ConsoleMessageWriter</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">ContainerControlledLifetimeManager</span>()); &nbsp; &nbsp;&nbsp;&nbsp; container.Resolve&lt;<span style="color: #2b91af">IParseService</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Parse(args) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .CreateCommand() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Execute(); &nbsp;&nbsp;&nbsp; container.Dispose(); }</pre> </p> <p>We are using Constructor Injection throughout, and most DI Containers (even Unity, but not <a href="http://mef.codeplex.com/">MEF</a>) natively understands that pattern. Consequently, this means that we can mostly just map interfaces to concrete types and the container will figure out the rest for us.</p> <p>Notice that I'm using the <a href="http://kozmic.pl/archive/2010/06/20/how-i-use-inversion-of-control-containers.aspx">Configure-Resolve-Release pattern</a> described by <a href="http://kozmic.pl">Krzysztof Koźmic</a>. First I configure the container, then I resolve<em> the entire object graph</em>, and lastly I dispose the container.</p> <p>The main part of the application's execution time will be spent within the Execute method, which is where all the real application code runs.</p> <p>In this example I wire up a console application, but it just as well might be any other type of application. In a web application we just do a resolve per web request instead.</p> <p>But wait! does that mean that we have to resolve the <em>entire</em> object graph of the application, even if we have dependencies that cannot be resolved at run-time? No, but that does not mean that you should pull from the container. <a href="http://kozmic.pl/archive/2010/06/22/how-i-use-inversion-of-control-containers-ndash-pulling-from.aspx">Pull from an Abstract Factory instead</a>.</p> <p>Another question that is likely to arise is: <em>what if I have dependencies that I rarely use? Must I wire these prematurely, even if they are expensive?</em> No, <a href="/2010/01/20/RebuttalConstructorover-injectionanti-pattern">you don't have to do that either</a>.</p> <p>In conclusion: there is never any reason to query the container. Use a container to compose your object graph, but don't rely on it by querying from it. Constructor Injection all the way enables most containers to auto-wire your application, and an <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory</a> can be a dependency too.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e6d75153ad2b4b0a819522a364808378"> <div class="comment-author"><a href="http://mindinthewater.blogspot.com">Wim Coenen</a> <a href="#e6d75153ad2b4b0a819522a364808378">#</a></div> <div class="comment-content">MEF also supports constructor injection natively. See [ImportingConstructor] in the MEF documentation section &quot;Declaring Imports&quot;.</div> <div class="comment-date">2010-09-01 16:29 UTC</div> </div> <div class="comment" id="9123d3fad10b4251ae871e2768f0198e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9123d3fad10b4251ae871e2768f0198e">#</a></div> <div class="comment-content">Yes, MEF supports it, but I chose my words pretty carefully. Any container that needs a special attribute to handle Constructor Injection can hardly be said to <em>understand</em> the pattern. I can't point MEF to a class that uses Constructor Injection and expect it to natively recognize it as such. MEF understands the [ImportingConstructor] attribute, but that's a different thing altogether.</div> <div class="comment-date">2010-09-01 17:34 UTC</div> </div> <div class="comment" id="a5292d1ebf52458e978c28979120c10f"> <div class="comment-author"><a href="http://www.deepelement.com">Todd Morrison</a> <a href="#a5292d1ebf52458e978c28979120c10f">#</a></div> <div class="comment-content">I have to agree with Mark.<br> <br> Having special constructors all over the code makes MEF kind of lame in comparison to .NET NInject or Java's CDI.<br> I am sure it works just fine, but seems a bit too &quot;Hand's on&quot; when there are a few good options that require less customization on host containers.</div> <div class="comment-date">2012-03-02 16:53 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Changing the behavior of AutoFixture auto-mocking with Moq https://blog.ploeh.dk/2010/08/25/ChangingthebehaviorofAutoFixtureauto-mockingwithMoq 2010-08-25T19:56:38+00:00 Mark Seemann <div id="post"> <p>One of my Twitter followers who appears to be using <a href="http://autofixture.codeplex.com/">AutoFixture</a> recently <a href="http://twitter.com/ZeroBugBounce/status/22007039917">asked me this</a>:</p> <blockquote> <p>So with the AutoMoqCustomization I feel like I should get Mocks with concrete types too (except the sut) - why am I wrong?</p></blockquote> <p>AutoFixture's extention for <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer">auto-mocking with Moq</a> was never meant as a general modification of behavior. Customizations <em>extend</em> the behavior of AutoFixture; they don't change it. There's a subtle difference. In any case, the auto-mocking customization was always meant as a fallback mechanism that would create Mocks for interfaces and abstract types because AutoFixture doesn't know how to deal with those.</p> <p>Apparently <a href="http://twitter.com/ZeroBugBounce">@ZeroBugBounce</a> also want concrete classes to be issued as Mock instances, which is not quite the same thing; AutoFixture already has a strategy for that (it's called ConstructorInvoker).</p> <p>Nevertheless I decided to spike a little on this to see if I could get it working. It turns out I needed to open some of the auto-mocking classes a bit for extensibility (always a good thing), so the following doesn't work with AutoFixture 2.0 beta 1, but will probably work with the RTW. Also please not that I'm reporting on a spike; I haven't thoroughly tested all edge cases.</p> <p>That said, the first thing we need to do is to remove AutoFixture's default ConstructorInvoker that invokes the constructor of concrete classes. This is possible with this constructor overload:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> Fixture(<span style="color: #2b91af">DefaultRelays</span> engineParts)</pre> </p> <p>This takes as input a DefaultRelays instance, which is more or less just an IEnumerable&lt;ISpecimenBuilder&gt; (the basic building block of AutoFixture). We need to replace that with a filter that removes the ConstructorInvoker. Here's a derived class that does that:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FilteringRelays</span> : <span style="color: #2b91af">DefaultEngineParts</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>, <span style="color: blue">bool</span>&gt; spec; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> FilteringRelays(<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>, <span style="color: blue">bool</span>&gt; specification) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (specification == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"specification"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.spec = specification; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: #2b91af">IEnumerator</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>&gt; GetEnumerator() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> enumerator = <span style="color: blue">base</span>.GetEnumerator(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">while</span> (enumerator.MoveNext()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (<span style="color: blue">this</span>.spec(enumerator.Current)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">yield</span> <span style="color: blue">return</span> enumerator.Current; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>DefaultEngineParts already derive from DefaultRelays, so this enables us to use the overloaded constructor to remove the ConstructorInvoker by using these filtered relays:</p> <p> <pre style="margin: 0px"><span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>, <span style="color: blue">bool</span>&gt; concreteFilter &nbsp;&nbsp;&nbsp; = sb =&gt; !(sb <span style="color: blue">is</span> <span style="color: #2b91af">ConstructorInvoker</span>); <span style="color: blue">var</span> relays = <span style="color: blue">new</span> <span style="color: #2b91af">FilteringRelays</span>(concreteFilter);</pre> </p> <p>The second thing we need to do is to tell the AutoMoqCustomization that it should Mock <em>all</em> types, not just interfaces and abstract classes. With the new (not in beta 1) overload of the constructor, we can now supply a <a href="http://en.wikipedia.org/wiki/Specification_pattern">Specification</a> that determines which types should be mocked.:</p> <p> <pre style="margin: 0px"><span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: blue">bool</span>&gt; mockSpec = t =&gt; <span style="color: blue">true</span>;</pre> </p> <p>We can now create the Fixture like this to get auto-mocking of all types:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(relays).Customize( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>(<span style="color: blue">new</span> <span style="color: #2b91af">MockRelay</span>(mockSpec)));</pre> </p> <p>With this Fixture instance, we can now create concrete classes that are mocked. Here's the full test that proves it:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">Fact</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> CreateAnonymousMockOfConcreteType() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">ISpecimenBuilder</span>, <span style="color: blue">bool</span>&gt; concreteFilter &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = sb =&gt; !(sb <span style="color: blue">is</span> <span style="color: #2b91af">ConstructorInvoker</span>); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> relays = <span style="color: blue">new</span> <span style="color: #2b91af">FilteringRelays</span>(concreteFilter); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: blue">bool</span>&gt; mockSpec = t =&gt; <span style="color: blue">true</span>; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(relays).Customize( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>(<span style="color: blue">new</span> <span style="color: #2b91af">MockRelay</span>(mockSpec))); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> foo = fixture.CreateAnonymous&lt;<span style="color: #2b91af">Foo</span>&gt;(); &nbsp;&nbsp;&nbsp; foo.DoIt(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fooTD = <span style="color: #2b91af">Mock</span>.Get(foo); &nbsp;&nbsp;&nbsp; fooTD.Verify(f =&gt; f.DoIt()); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>Foo is this concrete class:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Foo</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> DoIt() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>Finally, a word of caution: this is a <em>spike</em>. It's not fully tested and is bound to fail in certain cases: at least one case is when the type to be created is sealed. Since <a href="http://code.google.com/p/moq/">Moq</a> can't create a Mock of a sealed type, the above code will fail in that case. However, we can address this issue with some more sophisticated filters and Specifications. However, I will leave that up to the interested reader (or a later blog post).</p> <p>All in all I think this provides an excellent glimpse of the degree of extensibility that is built into AutoFixture 2.0's kernel.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture as an auto-mocking container https://blog.ploeh.dk/2010/08/19/AutoFixtureasanauto-mockingcontainer 2010-08-19T19:25:50+00:00 Mark Seemann <div id="post"> <p>The new internal architecture of <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0 enables some interesting features. One of these is that it becomes easy to extend AutoFixture to become an <a href="/2013/03/11/auto-mocking-container">auto-mocking container</a>.</p> <p>Since I personally use <a href="http://code.google.com/p/moq/">Moq</a>, the AutoFixture 2.0 .zip file includes a new assembly called Ploeh.AutoFixture.AutoMoq that includes an auto-mocking extension that uses Moq for Test Doubles.</p> <blockquote> <p>Please note that AutoFixture in itself has no dependency on Moq. If you don't want to use Moq, you can just ignore the Ploeh.AutoFixture.AutoMoq assembly.</p> <p>Auto-mocking with AutoFixture does not have to use Moq. Although it only ships with Moq support, it is possible to write an auto-mocking extension for a different dynamic mock library.</p></blockquote> <p>To use it, you must first add a reference to Ploeh.AutoFixture.AutoMoq. You can now create your Fixture instance like this:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>() &nbsp;&nbsp;&nbsp; .Customize(<span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>());</pre> </p> <p>What this does is that it adds a fallback mechanism to the fixture. If a type falls through the normal engine without being handled, the auto-mocking extension will check whether it is a request for an interface or abstract class. If this is so, it will <em>relay</em> the request to a request for a Mock of the same type.</p> <p>A different part of the extension handles requests for Mocks, which ensures that the Mock will be created and returned.</p> <p>Splitting up auto-mocking into a relay and a creational strategy for Mock objects proper also means that we can directly request a Mock if we would like that. Even better, we can use the built-in <a href="/2010/03/26/Moreaboutfrozenpizza">Freeze support</a> to freeze a Mock, and it will also automatically freeze the auto-mocked instance as well (because the relay will ask for a Mock that turns out to be frozen).</p> <p>Returning to the <a href="/2010/03/26/Moreaboutfrozenpizza">original frozen pizza example</a>, we can now rewrite it like this:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">Fact</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> AddWillPipeMapCorrectly() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Customize(<span style="color: blue">new</span> <span style="color: #2b91af">AutoMoqCustomization</span>()); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> basket = fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> mapMock = fixture.Freeze&lt;<span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pizza = fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.Add(pizza); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add)); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>Notice that we can simply freeze Mock&lt;IPizzaMap&gt; which also automatically freeze the IPizzaMap instance as well. When we later create the <a href="http://xunitpatterns.com/SUT.html">SUT</a> by requesting an anonymous BasketPresenter, IPizzaMap is already frozen in the fixture, so the correct instance will be injected into the SUT.</p> <p>This is similar to the behavior of the <a href="/2010/03/27/Freezingmocks">custom FreezeMoq extension method</a> I previously described, but now this feature is baked in.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5d3ac3e7ed574067a0f689c42505f0dd"> <div class="comment-author"><a href="http://codecontracts.info">David Allen</a> <a href="#5d3ac3e7ed574067a0f689c42505f0dd">#</a></div> <div class="comment-content">How would you compare and contrast AutoFixture with <a href="http://code.google.com/p/moq-contrib/wiki/Automocking">UnityAutoMockContainer</a>? I started using UnityAutoMockContainer and have been very pleased with it so far. The main contribution it makes is that it reduces the friction of TDD by mocking all dependencies so I do not have to. Then I can extract them using syntax like this<br> <b><br> Mock&lt;IFoo&gt; mockFoo = _container.GetMock&lt;IFoo&gt;();<br> </b><br> <br> It appears that both AutoFixture and UnityAutoMocker can be used in a similar fashion. Though it appears that AutoFixture has a broader feature set (the ability to create anonymous value types.) Whereas, one can create anonymous reference types by simply asking theUnityAutoMockContainer to resolve the interface or concrete type.<br> <br> I have only been using UnityAutoMockContainer for a week or two. So when I stumbled upon your most excellent book Dependency Injection in .Net, I discovered AutoFixture and was considering whether to use it instead. I welcome your perspective. By the way, that book on DI you are writing is great! Nice work. <br> <br> </div> <div class="comment-date">2010-09-09 10:34 UTC</div> </div> <div class="comment" id="a3cf1bff1948469bb17d19f87cdcee42"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a3cf1bff1948469bb17d19f87cdcee42">#</a></div> <div class="comment-content">Hi David<br> <br> Thanks for writing.<br> <br> As a disclaimer I should add that I have no experience with UnityAutoMockContainer, but that I'm quite familiar with Unity itself, as well as the general concept of an auto-mocking container, which (IIRC) is something that predates Unity. At the very least there are many auto-mocking containers available, and you can often easily extend an existing DI Container to add auto-mocking capabilities (see e.g. <a href="http://stackoverflow.com/questions/2462340/automockcontainer-with-support-for-automocking-class-instances/2462443#2462443">this simplified example</a>).<br> <br> This is more or less also the case with AutoFixture. It started out as something completely different (namely as a <a href="http://www.natpryce.com/articles/000714.html">Test Data Builder</a>) and then evolved. At a time it became natural to also add auto-mocking capabilities to it.<br> <br> You could say that the same is the case with Unity: it's a DI Container and was never envisioned as an auto-mocking container. However, since Unity is extensible, it is possible to add auto-mocking to it as well (just as the Autofac example above).<br> <br> This means that UnityAutoMockContainer and AutoFixture approach the concept of auto-mocking from two different directions. I can't really compare their auto-mocking capabilities as I don't know UnityAutoMockContainer well enough, but I can offer this on a more general level:<br> <br> AutoFixture shares a lot of similarities with DI Containers (Unity included). It supports <b>auto-wiring</b> and it can be configured to create instances in lots of interesting ways. However, since the focus is different, it does some things better and some things not as well as a DI Container.<br> <br> AutoFixture has more appropriate handling of primitive types. Normally a DI Container is not going to serve you a sequence of unique strings or numbers, whereas AutoFixture does. This was really the original idea that started it all.<br> <br> Most DI Containers are not going to try to populate writable properties, but AutoFixture does.<br> <br> AutoFixture also has a more granular concept of what constitutes a request. For all DI Containers I know, a request to resolve something is always based on a Type. AutoFixture, on the other hand, enable us to make arbitrary requests, which means that we can treat a request for a ParameterInfo differently than a request for a PropertyInfo (or a Type) even if they share the same Type. This sometimes comes in handy.<br> <br> On the other hand AutoFixture is weaker when it comes to lifetime management. A Fixture is never expected to exist for more than a single test case, so it makes no sense to model any other lifestyle than <b>Transient</b> and <b>Singleton</b>. AutoFixture can do that, but nothing more. It has no Seam for implementing custom lifestyles and it does not offer Per Thread or Per HttpRequest lifestyles. It doesn't have to, because it's not a DI Container.<br> <br> In short I prefer AutoFixture for TDD because it's a more focused tool than any DI Container. On the other hand it means that there's one more new API to learn, although that's not an issue for me personally :)</div> <div class="comment-date">2010-09-09 11:04 UTC</div> </div> <div class="comment" id="82db40971d164cc8a59ffc412f620483"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#82db40971d164cc8a59ffc412f620483">#</a></div> <div class="comment-content">Hi Mark,<br> <br> I just started to play with AutoFixture and its auto-mocking support. As far as I can see, the auto-mocking extensions return mocks without functionality, especially, without any data in the properties.<br> Example:<br> <b>var anonymousParent = fixture.CreateAnonymous&lt;ComplexParent&gt;();</b><br> This example from the cheat sheet would return a filled fixture:<br> ComplexParent:<br> -Child: ComplexChild<br> --Name: string: &quot;namef70b67ff-05d3-4498-95c9-de74e1aa0c3c&quot;<br> --Number: int: 1 <br> <br> <br> When using the AutoMoqCustomization with an interface IComplexParent with one property Child: ComplexChild, the result is strange:<br> 1) Child is of type ComplexChildProxySomeGuid, i.e. it is mocked, although it is a concrete class<br> 2) Child.Number is 0<br> 3) Child.Name is null<br> <br> <br> I think this is inconsistent. Is it a bug or intentional? If it is intentional, could you please explain?<br> <br> Thanks,<br> <br> Daniel</div> <div class="comment-date">2011-09-08 09:57 UTC</div> </div> <div class="comment" id="ba638d17b0844e6aae9405311e713477"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ba638d17b0844e6aae9405311e713477">#</a></div> <div class="comment-content">Yes, this is mostly intentional - mostly because even if AutoFixture would attempt to assign data to properties, you wouldn't get any result out of it. As an illustration, try using Moq without AutoFixture to create a new instance of IComplexParent and assign a property to it. It's not going to remember the property value!<br> <br> This is, in my opinion, the correct design choice made by the Moq designers: an interface specifies only the shape of members - not behavior.<br> <br> However, with Moq, you can turn on 'normal' property behavior by invoking the SetupAllProperties on the Mock instance. The AutoMoqCustomization doesn't do that, but it's certainly possible to use the underlying types used to implement it to compose a different AutoMoqCustomization that also invokes SetupAllProperties.<br> <br> In any case, this is only half of the solution, because it would only enable the Mock instance to get 'normal' property behavior. The other half would then be to make the AutoMoqCustomization automatically fill those properties. This is possible by wrapping the ISpecimenBuilder instance responsible for creating the actual interface instance in a PostProcessor. The following discussions may shed a little more light on these matters: <a href="http://autofixture.codeplex.com/workitem/4234">Default Greedy constructor behavior breaks filling</a> and <a href="http://autofixture.codeplex.com/discussions/222358">Filling sub properties</a>.<br> <br> In short, I don't consider it a bug, but I do respect that other people may want different base behavior than I find appropriate. It's definitely possible to tweak AutoFixture to do what you want it to do, but perhaps a bit more involved than it ought to be. AutoFixture 3.0 should streamline the customization API somewhat, I hope.<br> <br> As a side note, I consider properties in interfaces a design smell, so that's probably also why I've never had any issues with the current behavior.</div> <div class="comment-date">2011-09-08 10:51 UTC</div> </div> <div class="comment" id="8398e5e6d03e4ffda1751914831381d8"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#8398e5e6d03e4ffda1751914831381d8">#</a></div> <div class="comment-content">Thanks for your response. As I am using NSubstitute, I created an auto-mocking customization for it. I used the RhinoMocks customization as a template and changed it to support NSubstitute. I also implemented it the way I expected AutoMoq to behave, i.e. with filled properties.<br> Please see the fork I created: https://hg01.codeplex.com/forks/dhilgarth/autonsubstitute.<br> If you are interested, I can send a pull request.</div> <div class="comment-date">2011-09-08 13:28 UTC</div> </div> <div class="comment" id="5d9de0a263e54f16bb50c8fb7ee5bd10"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#5d9de0a263e54f16bb50c8fb7ee5bd10">#</a></div> <div class="comment-content">I think it is better to have AutoFixture behave consistent. When I request an anonymous instance, I expect its properties to be filled. I don't care if it's a concrete class I get or a mock because I asked for an anonymous instance of an interface. For me as a user it's not intuitive that they behave differently.<br> That's why I implemented it that way in AutoNSubstitute.<br> <br> <br> BTW, AutoMoq is in itself inconsistent: As I wrote in my original question, the property Child is not null but it contains a mock of ComplexChild. I understand your reasons for saying the properties should be null if a mock is returned, but then it should apply consistently to all properties.<br> <br> Maybe it is a good idea to let the user of the customization choose what behavior he wants?</div> <div class="comment-date">2011-09-08 14:19 UTC</div> </div> <div class="comment" id="6cb7294fcca940d585cecfee0c824e14"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6cb7294fcca940d585cecfee0c824e14">#</a></div> <div class="comment-content">Yes, I agree that consistency is desirable :)<br> <br> As I mentioned, it's not something I've ever given a great deal of thought because I almost never define properties on interfaces, but I agree that it might be more consistent, so I've created a <a href="http://autofixture.codeplex.com/workitem/4245">work item for it</a> - you could go and vote for it if you'd like :)<br> <br> Regarding your observation about the Child property, that's once more the behavior we get from Moq... The current implementation of AutoMoq basically just hands all mocking concerns off to Moq and doesn't deal with them any further.<br> <br> Regarding your fork for NSubstitute I only had a brief look at it, but please do send a pull request - then we'll take it from there :)<br> <br> Thank you for your interest.</div> <div class="comment-date">2011-09-08 14:37 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 2.0 beta 1 https://blog.ploeh.dk/2010/08/09/AutoFixture2.0beta1 2010-08-09T11:32:06+00:00 Mark Seemann <div id="post"> <p> It gives me great pleasure to announce that <a href="http://autofixture.codeplex.com/">AutoFixture</a> 2.0 beta 1 is now <a href="http://autofixture.codeplex.com/releases/view/50304">available for download</a>. Compared to version 1.1 AutoFixture 2.0 is implemented using a new and much more open and extensible engine that enables many interesting scenarios. </p> <p> Despite the new engine and the vastly increased potential, the focus on version 2.0 has been to ensure that the current, known feature set was preserved. </p> <h3 id="8da4f271bde546cdbf9991400adfa7d6"> What's new? <a href="#8da4f271bde546cdbf9991400adfa7d6" title="permalink">#</a> </h3> <p> While AutoFixture 2.0 introduces new features, this release is first and foremost an upgrade to a completely new internal architecture, so the number of new releases is limited. Never the less, the following new features are available in version 2.0: </p> <ul> <li><a href="http://autofixture.codeplex.com/workitem/1744">Support for enums</a></li> <li>Full <a href="http://autofixture.codeplex.com/workitem/4199">support of arrays</a></li> <li>Optional <a href="http://autofixture.codeplex.com/workitem/1913">tracing</a></li> <li>Improved extensibility</li> <li>Optional auto-mocking with <a href="http://code.google.com/p/moq/">Moq</a> (implemented as a separate, optional assembly)</li> <li>Optional extensions for <a href="http://xunit.codeplex.com/">xUnit.net</a> (implemented as a separate, optional assembly)</li> </ul> <p> There are still <a href="http://autofixture.codeplex.com/workitem/list/basic">open feature requests</a> for AutoFixture, so now that the new engine is in place I can again focus on implementing some of the features that were too difficult to address with the old engine. </p> <h3 id="2960d86aa1e345a9b090244e50ad675f"> Breaking changes <a href="#2960d86aa1e345a9b090244e50ad675f" title="permalink">#</a> </h3> <p> Version 2.0 introduces some breaking changes, although the fundamental API remains recognizable. </p> <ul> <li> A lot of the original methods on Fixture are now extension methods on IFixture (a new interface). The methods include CreateAnonymous&lt;T&gt;, CreateMany&lt;T&gt; and many others, so you will need to include a using directive for Ploeh.AutoFixture to compile the unit test code. </li> <li> CreateMany&lt;T&gt; still returns IEnumerable&lt;T&gt;, but the return value is much more consistently deferred. This means that in almost all cases you should instantly stabilize the sequence by converting it to a List&lt;T&gt;, an array or similar. </li> <li> Several methods are now deprecated in favor of new methods with better names. </li> </ul> <p> A few methods were also removed, but only those that were already deprecated in version 1.1. </p> <h3 id="991099f6397648ceab0140f2b368a74b"> Roadmap <a href="#991099f6397648ceab0140f2b368a74b" title="permalink">#</a> </h3> <p> The general availability of beta 1 of AutoFixture 2.0 marks the beginning of a trial period. If no new issues are reported within the next few weeks, a final version 2.0 will be released. If too many issues are reported, a new beta version may be necessary. </p> <p> Please <a href="http://autofixture.codeplex.com/workitem/list/basic">report any issues</a> you find. </p> <p> After the release of the final version 2.0 my plan is currently to focus on the <a href="http://autofixture.codeplex.com/workitem/1650">Idioms project</a>, although there are many other new features that might warrant my attention. </p> <p> Blog posts about the new features will also follow soon. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e53a498b60e24034a969eb6a28270d64"> <div class="comment-author">Arnis L. <a href="#e53a498b60e24034a969eb6a28270d64">#</a></div> <div class="comment-content">One thing i somehow can't understand - when exactly there's a need for such a framework as AutoFixture?<br> <br> P.s. Just don't read it wrong - I didn't say it's useless. Problem is with me. :)</div> <div class="comment-date">2010-08-13 19:00 UTC</div> </div> <div class="comment" id="17236816af614dc398fccccc68434356"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#17236816af614dc398fccccc68434356">#</a></div> <div class="comment-content">That's a reasonable question. The short answer is provided by AutoFixture's tag line: <em>Write maintainable unit tests, faster.</em><br> <br> For a more elaborate answer, you may want to read <a href="/2009/03/22/AnnouncingAutoFixture">this</a>, <a href="/2009/05/15/AutoFixtureAsFixtureObject">this</a> and <a href="/2009/07/01/AutoFixtureAsTestDataBuilder">this</a>. Also: stay tuned for more content.</div> <div class="comment-date">2010-08-15 19:32 UTC</div> </div> <div class="comment" id="0cfda707593744e684d8299e00010cdf"> <div class="comment-author">Arnis L. <a href="#0cfda707593744e684d8299e00010cdf">#</a></div> <div class="comment-content">Using it for awhile. Really nice tool. Less to write and think about. :)</div> <div class="comment-date">2010-08-29 17:57 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. StructureMap PerRequest vs. Unique lifetimes https://blog.ploeh.dk/2010/07/20/StructureMapPerRequestvs.Uniquelifetimes 2010-07-20T20:42:53+00:00 Mark Seemann <div id="post"> <p><a href="http://structuremap.github.com/structuremap/">StructureMap</a> offers several different lifetimes, among these two known as <em>PerRequest</em> and <em>Unique</em> respectively. Recently I found myself wondering what was the difference between those two, but <a href="http://twitter.com/jeremydmiller/statuses/18865954515">a</a> <a href="http://twitter.com/jeremydmiller/statuses/18865987309">little</a> <a href="http://twitter.com/jeremydmiller/statuses/18866005435">help</a> from <a href="http://codebetter.com/blogs/jeremy.miller/">Jeremy Miller</a> put me on the right track.</p> <p>In short, <em>Unique</em> is equivalent to what <a href="http://castleproject.org/container/index.html">Castle Windsor</a> calls <em>Transient:</em> every time an instance of a type is needed, a new instance is created. Even if we need the same service multiple times in the same resolved graph, multiple instances are created.</p> <p><em>PerRequest</em>, on the other hand, is a bit special. Each type can be viewed as a <em>Singleton</em> within a single call to GetInstance, but as <em>Transient</em> across different invocations of GetInstance. In other words, the same instance will be shared within a resolved object graph, but if we resolve the same root type once more, we will get a new shared instance - a <em>Singleton</em> local to that graph.</p> <p>Here are some unit tests I wrote to verify this behavior (recall that PerRequest is StructureMap's default lifestyle):</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">Fact</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> ResolveServicesWithSameUniqueDependency() { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">Container</span>(); &nbsp;&nbsp;&nbsp; container.Configure(x =&gt; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> unique = <span style="color: blue">new</span> <span style="color: #2b91af">UniquePerRequestLifecycle</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">IIngredient</span>&gt;().LifecycleIs(unique) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Use&lt;<span style="color: #2b91af">Shrimp</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">OliveOil</span>&gt;().LifecycleIs(unique); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">EggYolk</span>&gt;().LifecycleIs(unique); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">Vinegar</span>&gt;().LifecycleIs(unique); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">IIngredient</span>&gt;().LifecycleIs(unique) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Use&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">IIngredient</span>&gt;().LifecycleIs(unique) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Use&lt;<span style="color: #2b91af">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">Course</span>&gt;().LifecycleIs(unique); &nbsp;&nbsp;&nbsp; }); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> c1 = container.GetInstance&lt;<span style="color: #2b91af">Course</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> c2 = container.GetInstance&lt;<span style="color: #2b91af">Course</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.NotSame( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.Ingredients.OfType&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;().Single().Oil, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.Ingredients.OfType&lt;<span style="color: #2b91af">Mayonnaise</span>&gt;().Single().Oil); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.NotSame( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.Ingredients.OfType&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;().Single().Oil, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.Ingredients.OfType&lt;<span style="color: #2b91af">Mayonnaise</span>&gt;().Single().Oil); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.NotSame( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.Ingredients.OfType&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;().Single().Oil, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.Ingredients.OfType&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;().Single().Oil); } &nbsp; [<span style="color: #2b91af">Fact</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> ResolveServicesWithSamePerRequestDependency() { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">Container</span>(); &nbsp;&nbsp;&nbsp; container.Configure(x =&gt; &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">IIngredient</span>&gt;().Use&lt;<span style="color: #2b91af">Shrimp</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">OliveOil</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">EggYolk</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">Vinegar</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">IIngredient</span>&gt;().Use&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.For&lt;<span style="color: #2b91af">IIngredient</span>&gt;().Use&lt;<span style="color: #2b91af">Mayonnaise</span>&gt;(); &nbsp;&nbsp;&nbsp; }); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> c1 = container.GetInstance&lt;<span style="color: #2b91af">Course</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> c2 = container.GetInstance&lt;<span style="color: #2b91af">Course</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.Same( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.Ingredients.OfType&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;().Single().Oil, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.Ingredients.OfType&lt;<span style="color: #2b91af">Mayonnaise</span>&gt;().Single().Oil); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.Same( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.Ingredients.OfType&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;().Single().Oil, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.Ingredients.OfType&lt;<span style="color: #2b91af">Mayonnaise</span>&gt;().Single().Oil); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.NotSame( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.Ingredients.OfType&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;().Single().Oil, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.Ingredients.OfType&lt;<span style="color: #2b91af">Vinaigrette</span>&gt;().Single().Oil); }</pre> </p> <p>Notice that in both cases, the OliveOil instances are different across two independently resolved graphs (<em>c1</em> and <em>c2</em>).</p> <p>However, within each graph, the same OliveOil instance is shared in the <em>PerRequest</em> configuration, whereas they are different in the <em>Unique</em> configuration.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2e5f7f3d7e5c461488f8882af60f6048"> <div class="comment-author">Freddy Hansen <a href="#2e5f7f3d7e5c461488f8882af60f6048">#</a></div> <div class="comment-content">Thank you, I had missed the subtle difference here and your post saved me :-)</div> <div class="comment-date">2012-10-23 18:58 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Domain Objects and IDataErrorInfo https://blog.ploeh.dk/2010/07/12/DomainObjectsandIDataErrorInfo 2010-07-12T12:58:16+00:00 Mark Seemann <div id="post"> <p>Occasionally I get a question about whether it is reasonable or advisable to let domain objects implement IDataErrorInfo. In summary, my answer is that it's not so much a question about whether it's a leaky abstraction or not, but rather whether it makes sense at all. To me, it doesn't.</p> <p>Let us first consider the <em>essence</em> of the concept underlying IDataErrorInfo: It provides information about the validity of an object. More specifically, it provides error information when an object is in an <em>invalid</em> state.</p> <p>This is really the crux of the matter. Domain Objects should be designed so that they cannot be put into invalid states. They should guarantee their invariants.</p> <p>Let us return to the good old <a href="/2009/05/01/DealingWithConstrainedInput">DanishPhoneNumber example</a>. Instead of accepting or representing a Danish phone number as a string or integer, we model it as a Value Object that encapsulates the appropriate domain logic.</p> <p>More specifically, the class' constructor guarantees that you can't create an invalid instance:</p> <p> <pre style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">int</span> number; &nbsp; <span style="color: blue">public</span> DanishPhoneNumber(<span style="color: blue">int</span> number) { &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> ((number &lt; 112) || &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (number &gt; 99999999)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"number"</span>); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.number = number; }</pre> </p> <p>Notice that the Guard Clause guarantees that you can't create an instance with an invalid number, and the readonly keyword guarantees that you can't change the value afterwards. Immutable types make it easier to protect a type's invariants, but it is also possible with mutable types - you just need to place proper Guards in public setters and other mutators, as well as in the constructor.</p> <p>In any case, whenever a Domain Object guarantees its invariants according to the correct domain logic it makes no sense for it to implement IDataErrorInfo; if it did, the implementation would be trivial, because there would never be an error to report.</p> <p>Does this mean that IDataErrorInfo is a redundant interface? Not at all, but it is important to realize that it's an Application Boundary concern instead of a Domain concern. At Application Boundaries, data entry errors will happen, and we must be able to cope with them appropriately; we don't want the application to crash by passing unvalidated data to DanishPhoneNumber's constructor.</p> <p>Does this mean that we should duplicate domain logic at the Application Boundary? That should not be necessary. At first, we can apply a simple refactoring to the DanishPhoneNumber constructor:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> DanishPhoneNumber(<span style="color: blue">int</span> number) { &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (!<span style="color: #2b91af">DanishPhoneNumber</span>.IsValid(number)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"number"</span>); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.number = number; } &nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">bool</span> IsValid(<span style="color: blue">int</span> number) { &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> (112 &lt;= number) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; (number &lt;= 99999999); }</pre> </p> <p>We now have a public IsValid method we can use to implement an IDataErrorInfo at the Application Boundary. Next steps might be to add a TryParse method.</p> <p>IDataErrorInfo implementations are often related to input forms in user interfaces. Instead of crashing the application or closing the form, we want to provide appropriate error messages to the user. We can use the Domain Object to provide validation logic, but the concern is completely different: we want the form to stay open until valid data has been entered. Not until all data is valid do we allow the creation of a Domain Object from that data.</p> <p>In short, if you feel tempted to add IDataErrorInfo to a Domain Class, consider whether you aren't about to violate the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>. In my opinion, this is the case, and you would be better off reconsidering the design.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="001ce0e74cb84a8aba327934d035217f"> <div class="comment-author">onof <a href="#001ce0e74cb84a8aba327934d035217f">#</a></div> <div class="comment-content">I agree. <br> <br> Too often i see domain objects implementing a lot of validation code. I think that most of validation logic must be out of domain objects.</div> <div class="comment-date">2010-07-12 15:05 UTC</div> </div> <div class="comment" id="5323229af3c14442b854aa61053d8688"> <div class="comment-author">Arnis L <a href="#5323229af3c14442b854aa61053d8688">#</a></div> <div class="comment-content">People are struggling with understanding what they are validating, where they put their validation but i kind a think that nature of validity itself is often forgotten, unexplored or misunderstood.<br> <br> DanishPhoneNumber value can't be less than 112. In reality we are modeling - such a phone number just does not exist. So it makes sense to disallow existence of such an object and throw an error immediately.<br> <br> But there might be cases when domain contains temporary domain object invalidity from specific viewpoint/s.<br> <br> Consider good old cargo shipment domain from Blue book. Shipment object is invalid and shouldn't be shipped if there's no cargo to ship and yet such a shipment can exist because it's planned out gradually. In these kind of situations - it might make sense to use IDataErrorInfo interface.</div> <div class="comment-date">2010-07-13 13:44 UTC</div> </div> <div class="comment" id="5fe35026bbaa4640b2269815655c814d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5fe35026bbaa4640b2269815655c814d">#</a></div> <div class="comment-content">I would prefer to disagree :)<br> <br> We must keep in mind that we are not modeling the <em>real world</em>, but rather the <em>business logic</em> that addresses the real world. In your example, that would be represented by a proper domain object that models that a shipment is still in the planning stage. Let's call this object PlannedShipment.<br> <br> According to the domain model, PlannedShipment has its own invariants that it must protect, and the point still remains: PlannedShipment itself cannot be in an invalid state. However, PlannedShipment can't be shipped because it has not yet been promoted to a 'proper' Shipment. Such an API is safer because it makes it impossible to introduce errors of the kind where the code attempts to ship an invalid Shipment.</div> <div class="comment-date">2010-07-13 14:58 UTC</div> </div> <div class="comment" id="578f4ac64baf4b33ae6f9096f493c9ec"> <div class="comment-author"><a href="http://www.cuttingedge.it/blogs/steven">Steven</a> <a href="#578f4ac64baf4b33ae6f9096f493c9ec">#</a></div> <div class="comment-content">I think it is a very interesting thought to make domain objects immutable. However, I’m very curious about the practical implications of this. For instance: are all your domain objects immutable? Do you create them by using constructors with many arguments (because some domain objects tend to have many properties? It gets really awkward when constructors have many (say more than 5) arguments. How do you deal with this? Which O/RM tool(s) are you using for this immutability and how are you achieving this. Some O/RM tools will probably be a bad pick in trying to implement this. How are you dealing with updating existing entities? Creating a new entity with the same id seems rather awkward and doesn't seem to communicate its intent very well IMO. I love to see some examples.<br> <br> Thanks</div> <div class="comment-date">2010-07-13 20:08 UTC</div> </div> <div class="comment" id="9e363bd01614449bbd48daedaf2c2b4d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9e363bd01614449bbd48daedaf2c2b4d">#</a></div> <div class="comment-content">I never said that all Domain Objects should be immutable - I'm using the terminology from <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215">Domain-Driven Design</a> that distinguishes between <b>Entities</b> and <b>Value Objects</b>.<br> <br> A Value Object benefits very much from being immutable, so I always design them that way, but that doesn't mean that I make Entities immutable as well. I usually don't, although I'm sometimes toying with that idea.<br> <br> In any case, if you have more than 4 or 5 fields in a class (no matter if you fill them through a constructor or via property setters), you most likely have a new class somewhere in there waiting to be set free. <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">Clean Code</a> makes a pretty good case of this. Once again, too many primitives in an Entity is a smell that the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> is violated.</div> <div class="comment-date">2010-07-14 09:06 UTC</div> </div> <div class="comment" id="be98f1ec09134431bb92e87aafdf4551"> <div class="comment-author"><a href="http://www.janusknudsen.dk">Janus007</a> <a href="#be98f1ec09134431bb92e87aafdf4551">#</a></div> <div class="comment-content">It's very uncommon that i disagree with you, but...<br> <br> With your phone number example in mind, the validation should imho never be encapsulated in the domain object, but belong to a separate validation. When you put validation inside the constructor you will eventually break the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/closed principle</a>. Of course we have to validate for null input if they will break the functionality, but I would never integrate range check etc. into the class it self. <br> </div> <div class="comment-date">2010-07-15 20:28 UTC</div> </div> <div class="comment" id="16aacbed1f6642938f68fe0df54124be"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#16aacbed1f6642938f68fe0df54124be">#</a></div> <div class="comment-content">You'll have to define the range check <em>somewhere</em>. If you put it in an external class, I could repeat your argument there: &quot;your DanishPhoneNumberRangeValidator isn't open for extensibility.&quot; Value Objects are intrinsically rather atomic, and not particularly composable, in scope.<br> <br> However, consumers of those Value Objects need not be. While I didn't show it, DanishPhoneNumber could implement IPhoneNumber and all clients consume the interface. That would make DanishPhoneNumber a leaf of a composition while still keeping the architecture open for extensibility.<br> <br> The point is to define each type so that their states are always <em>consistent</em>. Note that for input gatherers, invalid data is considered <em>consistent</em> in the scope of input gathering. That's where IDataErrorInfo makes sense :)</div> <div class="comment-date">2010-07-15 21:13 UTC</div> </div> <div class="comment" id="9abe60c400ee47dbb75296ce0758db8c"> <div class="comment-author">Arnis L <a href="#9abe60c400ee47dbb75296ce0758db8c">#</a></div> <div class="comment-content">In short - I just wanted to mention nature of validity itself.<br> <br> Second thing I wanted to emphasize is about Your sentence of making sense - we should focus on technology we are using not only to be able to express ourselves, but to be aware (!) of how we are doing it and be able to adjust that.<br> <br> Patterns, OOP, .NET, POCO and whatnot are tools only. IDataErrorInfo is a tool too. Therefore - if it feels natural to use it to express our domain model (while it's suboptimal cause of arguments You mentioned), there is nothing wrong with using it per se. An agreement that our domain model objects (in contrast to reality) can be invalid if it simplifies things greatly (think ActiveRecord) is a tool too.</div> <div class="comment-date">2010-07-20 08:51 UTC</div> </div> <div class="comment" id="437f72ad101b4568a4e9e1ca76b42079"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#437f72ad101b4568a4e9e1ca76b42079">#</a></div> <div class="comment-content">I think we can often construct examples where the opposite of our current stance makes sense. Still, I like all rules like the above because they should first and foremost make us <em>stop and think</em> about what we are doing. Once we've done that, we can forge ahead knowing that we made a conscious decision - no matter what we chose.<br> <br> To me, internal consistency and the SRP is so important that I would feel more comfortable having IDataErrorInfo outside of domain objects, but there are no absolutes :)</div> <div class="comment-date">2010-07-20 09:35 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Introducing AutoFixture Likeness https://blog.ploeh.dk/2010/06/29/IntroducingAutoFixtureLikeness 2010-06-29T06:39:30+00:00 Mark Seemann <div id="post"> <p>The <a href="/2010/04/06/MappingtypeswithAutoFixture">last time I presented a sample</a> of an <a href="http://autofixture.codeplex.com/">AutoFixture</a>-based unit test, I purposely glossed over the state-based verification that asserted that the resulting state of the <em>basket</em> variable was that the appropriate Pizza was added:</p> <p> <pre style="margin: 0px"><span style="color: #2b91af">Assert</span>.IsTrue(basket.Pizze.Any(p =&gt; &nbsp;&nbsp;&nbsp; p.Name == pizza.Name), <span style="color: #a31515">"Basket has added pizza."</span>);</pre> </p> <p>The main issue with this assertion is that the implied equality expression is rather weak: we consider a PizzaPresenter instance to be equal to a Pizza instance if their Name properties match.</p> <p>What if they have other properties (like Size) that don't match? If this is the case, the test would be a <a href="http://xunitpatterns.com/false%20negative.html">false negative</a>. A match would be found in the Pizze collection, but the instances would not truly represent the same pizza.</p> <p>How do we resolve this conundrum without introducing <a href="http://xunitpatterns.com/Test%20Logic%20in%20Production.html#Equality%20Pollution">equality pollution</a>? AutoFixture offers one option in the form of the generic Likeness&lt;TSource, TDestination&gt; class. This class offers convention-based <a href="http://xunitpatterns.com/test-specific%20equality.html">test-specific equality</a> mapping from TSource to TDestination and overriding the Equals method.</p> <p>One of the ways we can use it is by a convenience extension method. This unit test is a refactoring of the test from the previous post, but now using Likeness:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> AddWillAddToBasket_Likeness() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; fixture.Register&lt;<span style="color: #2b91af">IPizzaMap</span>, <span style="color: #2b91af">PizzaMap</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> basket = fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pizza = fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> expectedPizza = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pizza.AsSource().OfLikeness&lt;<span style="color: #2b91af">Pizza</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.Add(pizza); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.IsTrue(basket.Pizze.Any(expectedPizza.Equals)); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>Notice how the Likeness instance is created with the AsSource() extension method. The <em>pizza</em> instance (of type PizzaPresenter) is the source of the Likeness, whereas the Pizza domain model type is the destination. The <em>expectedPizza</em> instance is of type Likeness&lt;PizzaPresenter, Pizza&gt;.</p> <p>The Likeness class overrides Equals with a convention-based comparison: if two properties have the same name and type, they are equal if their values are equal. All public properties on the destination must have equal properties on the source.</p> <p>This allows me to specify the Equals method as the predicate for the Any method in the assertion:</p> <p> <pre style="margin: 0px"><span style="color: #2b91af">Assert</span>.IsTrue(basket.Pizze.Any(expectedPizza.Equals));</pre> </p> <p>When the Any method evalues the Pizze collection, it executes the Equals method on Likeness, resulting in a convention-based comparison of all public properties and fields on the two instances.</p> <p>It's possible to customize the comparison to override the behavior for certain properties, but I will leave that to later posts. This post only scratches the surface of what Likeness can do.</p> <p>To use Likeness, you must add a reference to the Ploeh.SemanticComparison assembly. You can create a new instance using the public constructor, but to use the AsSource extension method, you will need to add a using directive:</p> <p> <pre style="margin: 0px"><span style="color: blue">using</span> Ploeh.SemanticComparison.Fluent;</pre> </p> </div> <div id="comments"> <hr> <h2 id="comments-header">Comments</h2> <div class="comment" id="6a94053ac837434fa551f4f8376829c7"> <div class="comment-author">DavidS <a href="#6a94053ac837434fa551f4f8376829c7">#</a></div> <div class="comment-content"> Hi Mark,<br> <p> In your example, you are only comparing one property and I know that you can test many properties as well. </p> <p> Now it is my understanding that given many properties if <em>any</em> property doesn't match, then you'll get a test failure. My question is how to output a message pinpointing which property is causing the test to fail. </p> <p> On another note, maybe you could ask Adam Ralph how he's integrated the comment section on his blog, which I believe is using the same platform as you are. http://adamralph.com/2013/01/09/blog-post-excerpts-a-new-solution/ </p> </div> <div class="comment-date">2013-04-18 12:40 UTC</div> </div> <div class="comment" id="8693d386283f4fe982da3939e9b257b3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8693d386283f4fe982da3939e9b257b3">#</a></div> <div class="comment-content"> <p> David, if you want to get more detailed feedback on which properties don't match, you can use <code>expected.ShouldEqual(actual);</code> </p> </div> <div class="comment-date">2013-04-18 21:33 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Upcoming talks spring 2010 https://blog.ploeh.dk/2010/05/23/Upcomingtalksspring2010 2010-05-23T16:11:37+00:00 Mark Seemann <div id="post"> <p>In the next couple of weeks I will be giving a couple of talks in Copenhagen.</p> <p>At <a href="http://communityday.in/copenhagen/Home/Agenda">Community Day 2010</a> I will be giving two talks on respectively Dependency Injection and TDD.</p> <p>In early June I will be giving a <a href="http://www.eventbrite.com/event/660219735">repeat of my previous CNUG TDD talk</a>.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Sneak view at Castle's WCF Facility https://blog.ploeh.dk/2010/05/18/SneakviewatCastlesWCFFacility 2010-05-18T05:27:56+00:00 Mark Seemann <div id="post"> <p>One of Castle Windsor's facilities addresses wiring up of WCF services. So far, the sparse documentation for the WCF Facility seems to indicate <a href="http://www.castleproject.org/container/facilities/trunk/wcf/index.html">that you have to configure your container in a global.asax</a>. That's not much to my liking. First of all, it reeks of ASP.NET, and secondly, it's not going to work if you expose WCF over protocols other than HTTP.</p> <p>However, now that we know that <a href="/2010/05/17/ServiceHostFactorylifetime">a custom ServiceHostFactory is effectively a Singleton</a>, a much better alternative is to derive from the WCF Facility's DefaultServiceHost class:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FooServiceHostFactory</span> : &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">DefaultServiceHostFactory</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> FooServiceHostFactory() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: blue">base</span>(<span style="color: #2b91af">FooServiceHostFactory</span>.CreateKernel()) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: #2b91af">IKernel</span> CreateKernel() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">WindsorContainer</span>(); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.AddFacility&lt;<span style="color: #2b91af">WcfFacility</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">FooService</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .LifeStyle.Transient); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">IBar</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">Bar</span>&gt;()); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> container.Kernel; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>Although it feels a little odd to create a container and then not really use it, but only its Kernel property, this works like a charm. It correctly wires up this FooService:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FooService</span> : <span style="color: #2b91af">IFooService</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IBar</span> bar; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> FooService(<span style="color: #2b91af">IBar</span> bar) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (bar == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"bar"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.bar = bar; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IFooService Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">string</span> Foo() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.bar.Baz; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>However, instead of the static CreateKernel method that creates the IKernel instance, <a href="http://groups.google.com/group/castle-project-users/browse_thread/thread/5401a7189f8295af/2dabe6cbb3c63488?q=">I suggest that the WCF Facility utilizes the Factory Method pattern</a>. As the WCF Facility has not yet been released, perhaps there's still time for that change.</p> <p>In any case, the WCF Facility saves you from writing a lot of infrastructure code if you would like to wire your WCF services with Castle Windsor.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="a241cb444dbb41c1a13301c88ce480a8"> <div class="comment-author"><a href="http://kozmic.pl">Krzysztof Koźmic</a> <a href="#a241cb444dbb41c1a13301c88ce480a8">#</a></div> <div class="comment-content">There's a plan to provide Facility's base ServiceHostFactory that you could use out of the box with very minimal anmount if XML config in your web.config/app.config file:<br> <br> &lt;installers&gt;<br> &lt;install assembly=&quot;Ploeh.AssemblyContainingWindsorInstallers&quot;/&gt;<br> &lt;/ installers&gt;<br> <br> See http://stw.castleproject.org/Windsor.Registering-Installers.ashx<br> <br> Alternatively you could override protected IWindsorInstaller GetInstallers(); method and configure the container entirely in code.<br> <br> Thoughts?</div> <div class="comment-date">2010-05-18 08:24 UTC</div> </div> <div class="comment" id="b2643b6c2b1f4c7e86a48669637240da"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b2643b6c2b1f4c7e86a48669637240da">#</a></div> <div class="comment-content">That would definitely address most cases, including the one I'm currently looking at, although I'm not sure I understand your last sentence. Did you mean that I can implement a custom IWindsorInstaller? If so, that makes a lot of sense.<br> <br> For the edge cases where adding an Installer isn't enough, I'd still prefer a Factory Method hook in DefaultServiceHostFactory, but being able to specify an Installer in web.config will likely address 90 percent (or more) of scenarios.<br> <br> Looks good!</div> <div class="comment-date">2010-05-18 08:38 UTC</div> </div> <div class="comment" id="03bae55e8d1c447590c831bf9822b4ae"> <div class="comment-author"><a href="http://kozmic.pl">Krzysztof Koźmic</a> <a href="#03bae55e8d1c447590c831bf9822b4ae">#</a></div> <div class="comment-content">In both cases you will need to provide your own implementation of IWindsorInstaller that will provide services and configuration to the container. The difference between the two approaches is as follows:<br> <br> If you use Facility's default SH Factory it will look into your web.config for informations about how to configure itself.<br> ALternativelty you can provide your own subclass of the SH Factory, override its protected IWindsorInstaller[] GetInstallers(); method and then you'll be able to configure the container without using config file.</div> <div class="comment-date">2010-05-18 08:45 UTC</div> </div> <div class="comment" id="d0dc0d5aa720445da5413b9b7cdd30e2"> <div class="comment-author"><a href="http://kozmic.pl">Krzysztof Koźmic</a> <a href="#d0dc0d5aa720445da5413b9b7cdd30e2">#</a></div> <div class="comment-content">Why would you prefer factory method instead of just pointing to installers? What do you need to do that the other approach can't address?</div> <div class="comment-date">2010-05-18 08:47 UTC</div> </div> <div class="comment" id="8a58a9bcc2164ed9bb0285cb805ba16e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8a58a9bcc2164ed9bb0285cb805ba16e">#</a></div> <div class="comment-content">Thanks for your explanation. I think that sounds very promising. As far as I can tell, this is still in the future, right? I don't see any GetInstallers() on DefaultServiceHostFactory right now (based on the code I downloaded and compiled last week).<br> <br> Don't get me wrong on the Factory Method thing. I don't expect to need it often (if at all), but I just think it would be a good OCP thing to do... Off the top of my head, I can't remember whether there are things you can do to a container that you can't do from an Installer. Maybe there aren't...</div> <div class="comment-date">2010-05-18 09:05 UTC</div> </div> <div class="comment" id="7c581ccf11a14b06ade6b9ebd588531b"> <div class="comment-author"><a href="http://kozmic.pl">Krzysztof Koźmic</a> <a href="#7c581ccf11a14b06ade6b9ebd588531b">#</a></div> <div class="comment-content">you're right, GetInstallers() is not there yet, (same as specifying installers in xml is part of trunk, not v2.1).<br> <br> I do intend to leave the door open with factory method for people who can't use xml even if it's so minimal (or feel sick when even thinking about xml), but as you said - there really isn't anything you couldn't do from installer so I imagine it would be for emergency cases only.</div> <div class="comment-date">2010-05-18 09:15 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. ServiceHostFactory lifetime https://blog.ploeh.dk/2010/05/17/ServiceHostFactorylifetime 2010-05-17T05:42:33+00:00 Mark Seemann <div id="post"> <p>For a while I've been wondering about the lifetime behavior of custom <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.servicehostfactory.aspx">ServiceHostFactory</a> classes hosted in IIS. Does IIS create an instance per request? Or a single instance to handle all requests?</p> <p>I decided to find out, so I wrote a little test service. The conclusion seems to be that there is only a single instance that servers as a factory for all requests. This is very fortunate, since it gives us an excellent place to host a DI Container. The container can then manage the lifetime of all components, including Singletons that will live for the duration of the process.</p> <p>If you are curious how I arrived at this conclusion, here's the code I wrote. I started out with this custom ServiceHostFactory:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">PocServiceHostFactory</span> : <span style="color: #2b91af">ServiceHostFactory</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">int</span> number = 1; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> PocServiceHostFactory() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Interlocked</span>.Increment( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">ref</span> <span style="color: #2b91af">PocServiceHostFactory</span>.number); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: #2b91af">ServiceHost</span> CreateServiceHost( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Type</span> serviceType, <span style="color: #2b91af">Uri</span>[] baseAddresses) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">PocServiceHost</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">PocServiceHostFactory</span>.number, serviceType, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; baseAddresses); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>The idea is that every time a new instance of ServiceHostFactory is created, the static number is incremented.</p> <p>The PocServiceHostFactory just forwards the number to the PocServiceHost:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">PocServiceHost</span> : <span style="color: #2b91af">ServiceHost</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> PocServiceHost(<span style="color: blue">int</span> number, <span style="color: #2b91af">Type</span> serviceType, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Uri</span>[] baseAddresses) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color: blue">base</span>(serviceType, baseAddresses) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">foreach</span> (<span style="color: blue">var</span> cd <span style="color: blue">in</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.ImplementedContracts.Values) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cd.Behaviors.Add( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">NumberServiceInstanceProvider</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>The PocServiceHost just forwards the number to the NumberServiceInstanceProvider:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">NumberServiceInstanceProvider</span> : &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IInstanceProvider</span>, <span style="color: #2b91af">IContractBehavior</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">int</span> number; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> NumberServiceInstanceProvider(<span style="color: blue">int</span> number) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.number = number; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IInstanceProvider Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">object</span> GetInstance( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">InstanceContext</span> instanceContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Message</span> message) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.GetInstance(instanceContext); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">object</span> GetInstance( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">InstanceContext</span> instanceContext) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">NumberService</span>(<span style="color: blue">this</span>.number); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> ReleaseInstance( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">InstanceContext</span> instanceContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">object</span> instance) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IContractBehavior Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> AddBindingParameters( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ContractDescription</span> contractDescription, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ServiceEndpoint</span> endpoint, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">BindingParameterCollection</span> bindingParameters) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> ApplyClientBehavior( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ContractDescription</span> contractDescription, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ServiceEndpoint</span> endpoint, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ClientRuntime</span> clientRuntime) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> ApplyDispatchBehavior( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ContractDescription</span> contractDescription, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ServiceEndpoint</span> endpoint, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">DispatchRuntime</span> dispatchRuntime) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dispatchRuntime.InstanceProvider = <span style="color: blue">this</span>; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Validate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ContractDescription</span> contractDescription, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ServiceEndpoint</span> endpoint) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>The relevant part of NumberServiceInstanceProvider is the GetInstanceMethod that simply forwards the number to the NumberService:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">NumberService</span> : <span style="color: #2b91af">INumberService</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">int</span> number; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> NumberService(<span style="color: blue">int</span> number) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.number = number; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> INumberService Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">int</span> GetNumber() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.number; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>As you can see, NumberService simply returns the injected number.</p> <p>The experiment is now to host NumberService in IIS using PocServiceHostFactory. If there is only one ServiceHostFactory per application process, we would expect that the same number (2) is returned every time we invoke the GetNumber operation. If, on the other hand, a new instance of ServiceHostFactory is created per request, we would expect the number to increase for every request.</p> <p>To test this I spun up a few instances of WcfTestClient.exe and invoked the operation. It consistently returns <em>2</em> across multiple clients and multiple requests. This supports the hypothesis that there is only one ServiceHostFactory per service process.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="72c772ae704f402d9721cca0d3e2e117"> <div class="comment-author">onof <a href="#72c772ae704f402d9721cca0d3e2e117">#</a></div> <div class="comment-content">There's a project on codeplex<br> <br> http://containerservicehost.codeplex.com/documentation<br> </div> <div class="comment-date">2010-05-17 07:10 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fun with literal extensions and Ambient Context https://blog.ploeh.dk/2010/04/27/FunwithliteralextensionsandAmbientContext 2010-04-27T04:24:25+00:00 Mark Seemann <div id="post"> <p><a href="http://amzn.to/12p90MG">My book</a> contains a section on the <a href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx">Ambient Context</a> pattern that uses a TimeProvider as an example. It's used like this:</p> <p> <pre style="margin: 0px"><span style="color: blue">this</span>.closedAt = <span style="color: #2b91af">TimeProvider</span>.Current.UtcNow;</pre> </p> <p>Yesterday I was TDDing a state machine that consumes TimeProvider and needed to freeze and advance time at different places in the test. Always on the lookout for making unit tests more readable, I decided to have a little fun with literal extensions and TimeProvider. I ended up with this test:</p> <p> <pre style="margin: 0px"><span style="color: green">// Fixture setup</span> <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">WcfFixture</span>(); &nbsp; <span style="color: #2b91af">DateTime</span>.Now.Freeze(); &nbsp; fixture.Register(1.Minutes()); <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">CircuitBreaker</span>&gt;(); sut.PutInOpenState(); &nbsp; 2.Minutes().Pass(); <span style="color: green">// Exercise system</span> sut.Guard(); <span style="color: green">// Verify outcome</span> <span style="color: #2b91af">Assert</span>.IsInstanceOfType(sut.State, &nbsp;&nbsp;&nbsp; <span style="color: blue">typeof</span>(<span style="color: #2b91af">HalfOpenCircuitState</span>)); <span style="color: green">// Teardown</span></pre> </p> <p>There are several items of note. Imagine that we can freeze time!</p> <p> <pre style="margin: 0px"><span style="color: #2b91af">DateTime</span>.Now.Freeze();</pre> </p> <p>With the TimeProvider and an extension method, we can:</p> <p> <pre style="margin: 0px"><span style="color: blue">internal</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Freeze(<span style="color: blue">this</span> <span style="color: #2b91af">DateTime</span> dt) { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> timeProviderStub = <span style="color: blue">new</span> <span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">TimeProvider</span>&gt;(); &nbsp;&nbsp;&nbsp; timeProviderStub.SetupGet(tp =&gt; tp.UtcNow).Returns(dt); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">TimeProvider</span>.Current = timeProviderStub.Object; }</pre> </p> <p>This effectively sets up the TimeProvider to always return the same time.</p> <p>Later in the test I state that 2 minutes pass:</p> <p> <pre style="margin: 0px">2.Minutes().Pass();</pre> </p> <p>I particularly like the grammatically correct English. This is accomplished with a combination of a literal extension and changing the state of TimeProvider.</p> <p>First, the literal extension:</p> <p> <pre style="margin: 0px"><span style="color: blue">internal</span> <span style="color: blue">static</span> <span style="color: #2b91af">TimeSpan</span> Minutes(<span style="color: blue">this</span> <span style="color: blue">int</span> m) { &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: #2b91af">TimeSpan</span>.FromMinutes(m); }</pre> </p> <p>Given the TimeSpan returned from the Minutes method, I can now invoke the Pass extension method:</p> <p> <pre style="margin: 0px"><span style="color: blue">internal</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Pass(<span style="color: blue">this</span> <span style="color: #2b91af">TimeSpan</span> ts) { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> previousTime = <span style="color: #2b91af">TimeProvider</span>.Current.UtcNow; &nbsp;&nbsp;&nbsp; (previousTime + ts).Freeze(); }</pre> </p> <p>Note that I just add the TimeSpan to the current time and invoke the Freeze extension method with the new value.</p> <p>Last, but not least, I should point out that the PutInOpenState method isn't some smelly test-specific method on the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, but rather yet another extension method.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Changing Windsor lifestyles after the fact https://blog.ploeh.dk/2010/04/26/ChangingWindsorlifestylesafterthefact 2010-04-26T05:09:42+00:00 Mark Seemann <div id="post"> <p>I recently had the need to change the lifestyles of all components in a WindsorContainer (read on to the end if you want to know why). This turned out to be amazingly simple to do.</p> <p>The problem was this: I had numerous components registered in a WindsorContainer, some of them as Singletons, some as Transients and yet again some as PerWebRequest. Configuration was even defined in numerous IWindsorInstallers, including some distributed XML files. I now needed to spin up a second container with the same configuration as the first one, <em>except</em> that the lifestyles should be all Singletons across the board.</p> <p>This can be easily accomplished by implementing a custom IContributeComponentModelConstruction type. Here's a simple example:</p> <p>Consider this IWindsorInstaller:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FooInstaller</span> : <span style="color: #2b91af">IWindsorInstaller</span> { <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IWindsorInstaller Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Install(<span style="color: #2b91af">IWindsorContainer</span> container, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IConfigurationStore</span> store) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">IFoo</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">Foo</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .LifeStyle.Transient); &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>The important point to notice is that it registers the lifestyle as Transient. In other words, this container will always return new Foo instances:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">WindsorContainer</span>(); container.Install(<span style="color: blue">new</span> <span style="color: #2b91af">FooInstaller</span>());</pre> </p> <p>We can override this behavior by adding this custom IContributeComponentModelConstruction:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">SingletonEqualizer</span> : &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IContributeComponentModelConstruction</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> ProcessModel(<span style="color: #2b91af">IKernel</span> kernel, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ComponentModel</span> model) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; model.LifestyleType = <span style="color: #2b91af">LifestyleType</span>.Singleton; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>In this very simple example, I always set the lifestyle type to the same value, but obviously we can write as complex code in the ProcessModel method as we would like. We can now configure the container like this:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">WindsorContainer</span>(); container.Kernel.ComponentModelBuilder &nbsp;&nbsp;&nbsp; .AddContributor(<span style="color: blue">new</span> <span style="color: #2b91af">SingletonEqualizer</span>()); container.Install(<span style="color: blue">new</span> <span style="color: #2b91af">FooInstaller</span>());</pre> </p> <p>With this configuration we will now get the same instance of Foo every time we Resolve IFoo.</p> <p>We did I need this? Because my application is a web application and I'm using the PerWebRequest lifestyle in a number of places. However, I needed to spin up a second container that would compose object hierarchies for a background process. This background process needs the same component configuration as the rest of the application, but can't use the PerWebRequest lifestyle as there will be no web request available to the background process. Hence the need to change lifestyles across the board.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Why I'm migrating from MSTest to xUnit.net https://blog.ploeh.dk/2010/04/26/WhyImmigratingfromMSTesttoxUnit.net 2010-04-26T04:30:49+00:00 Mark Seemann <div id="post"> <p>About a month ago I decided to migrate from MSTest to <a href="http://xunit.codeplex.com/wikipage">xUnit.net</a>, and while I am still in the process, I haven't regretted it yet, and I don't expect to. <a href="http://autofixture.codeplex.com/">AutoFixture</a> has already moved over, and I'm slowly migrating all the sample code for <a href="http://amzn.to/12p90MG">my book</a>.</p> <p>Recently I was asked <em>why</em>, which prompted me to write this post.</p> <p>I'm not moving away from MSTest for one single reason. It's rather like lots of small reasons.</p> <p>When I originally started out with TDD, I used nUnit - it was more or less the only unit testing framework available for .NET at the time. When MSTest came, the change was natural, since I worked for Microsoft at the time. This is not the case anymore, but it still took me most of a year to finally abandon MSTest.</p> <p>There was one thing that really made me cling to MSTest, and that was the IDE integration, but over time, I started to realize that this was the <em>only</em> reason, and even that was getting flaky.</p> <p>When I started to think about all the things that left me dissatisfied, making the decision was easy:</p> <ul> <li>First of all, MSTest isn't extensible, but xUnit.net is. In xUnit.net, I can extend the Fact or Theory attributes (and I intent to), while in MSTest, I will have to play with the cards I've been dealt. I think I could live with all the other issues if I could just have this one, but no. <li>MSTest has no support for parameterized test. xUnit.net does (via the Theory attribute). <li>MSTest has no Assert.Throws, although I <a href="https://connect.microsoft.com/VisualStudio/feedback/details/381288/assert-throws-for-mstest">requested this feature a long time ago</a>. Now Visual Studio 2010 is out, but Assert.Throws is still nowhere in sight. <li>MSTest has no x64 support. Tests always run as x86. Usually it's no big deal, but sometimes it's a really big deal. <li>In MSTest, to write unit tests, you must create a special <em>Unit Test Project</em>, and those are only available for C# and VB.net. Good luck trying to write unit tests in a more exotic .NET language (like F# on Visual Studio 2008). xUnit.net doesn't have this problem. <li>MSTest uses Test Lists and .vsmdi files to maintain test lists. Why? I don't care, I just want to execute my tests, and the .vsmdi files are in the way. This is particularly bad when you use TFS, but I'm also moving away from TFS, so that wouldn't have continued to be that much of an issue. Still: try having more than one .sln file with unit tests in the same folder, and watch funny things happen because they need to share the same .vsmdi file. <li>I suppose it's because of the .vsmdi files, but sometimes I get a Test run error if I delete a test and run the tests immediately after. That's a false positive, if anyone cares. <li>MSTest gives special treatment to its own AssertionException, which gets nice formatting in the Test Results window. All other exceptions (like verification exceptions thrown by Moq or Rhino Mocks are rendered near-intelligible because MSTest thinks it's very important to report the fully qualified name of the exception before its message. Most of the time, you have to open the Test Details window to see the exception message. <li>Last, but not least, I often get cryptic exception messages like this one: <em>Column 'id_column, runid_column' is constrained to be unique.&nbsp; Value '8c84fa94-04c1-424b-9868-57a2d4851a1d, d7471c5e-522f-43d3-b2c5-8f5cab55af0e' is already present.</em> This appears in a very annoying modal MessageBox, but clicking OK and retrying usually works, although sometimes it even takes two or three attempts before I can get past this error.</li></ul> <p>It not one big thing, it's just a lot of small, but very annoying things. After three iterations (VS2005, VS2008 and now VS2010) these issue have still to be addressed, and I got tired of waiting.</p> <p>So far, I can only say that I have none of these problems with xUnit.net and the IDE integration provided by <a href="http://www.testdriven.net/">TestDriven.NET</a>. It's just a much smoother experience with much less friction.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0e2c269f7b5a4319a1d7466831e6543d"> <div class="comment-author"><a href="http://twitter.com/adrianomm">Adriano Machado</a> <a href="#0e2c269f7b5a4319a1d7466831e6543d">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Interesting post about moving away from &quot;out-of-the-box&quot; Microsoft tools. I've made this move about a year ago, and I can't regret about it.<br> <br> Another point that you mentioned in your post and that really caught my attention was the fact that you are also moving away from TFS. Since I'm starting my own startup here, the budget is really short and we are looking for cheaper alternatives to TFS.<br> <br> Here, we really like Mercurial HG and we are basing out SCM on it. However, I'm having difficulty finding tools for bug and feature tracking. Can you share with me in which direction you are moving away from TFS?</div> <div class="comment-date">2010-04-26 13:59 UTC</div> </div> <div class="comment" id="e35980410b9a4f639290448c4a2facda"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e35980410b9a4f639290448c4a2facda">#</a></div> <div class="comment-content">Personally, I also use Hg for SCM.<br> <br> In Safewhere, we are currenlty trying out AgileZen for work item tracking. For AutoFixture, I just use the tools provided with CodePlex.</div> <div class="comment-date">2010-04-26 16:32 UTC</div> </div> <div class="comment" id="18a30eea8e2549d4b7a381e9a2dc639d"> <div class="comment-author"><a href="http://www.clear-lines.com/blog">Mathias</a> <a href="#18a30eea8e2549d4b7a381e9a2dc639d">#</a></div> <div class="comment-content">I started with NUnit, and gave a quick shot at MSTest, but never made the transition, because I couldn't see any upside. Could you comment a bit on why you picked xUnit over NUnit? I haven't tried it yet, but from what I saw in the docs, the syntax is pretty interesting; first time in a while that I see a framework which seems to re-think unit testing, rather than improve on JUnit.</div> <div class="comment-date">2010-04-26 21:15 UTC</div> </div> <div class="comment" id="9ad65aa0e1cc49498e726dfbe9f9eff9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9ad65aa0e1cc49498e726dfbe9f9eff9">#</a></div> <div class="comment-content">There are two main reasons that I prefer xUnit.net over NUnit, but both may be due to ignorance about NUnit on my part. The last time I did serious work with NUnit must have been back in 2005.<br> <br> One reason is that xUnit.net has a pretty good extensibility story, and as I do have some plans in that direction, that's a pretty big issue for me. Last time I checked, the extensibility story for NUnit didn't match xUnit.net.<br> <br> NUnit has a design bug when it comes to Fixture management, because it creates only a single instance of a test class and invokes all the test methods on that instance. This may have been fixed since the last time I looked, but in any case, I better like xUnit.net's philosphy of using the test class' constructor for Fixture Setup (if any) and implementing IDisposable for Fixture Teardown.<br> <br> As I said, both items are based on somewhat dated knowledge on my part, so none of them may apply anymore.</div> <div class="comment-date">2010-04-26 21:26 UTC</div> </div> <div class="comment" id="500f34aeab514f11923d01ebf5f4e1ad"> <div class="comment-author"><a href="http://humblecoder.co.uk">Will</a> <a href="#500f34aeab514f11923d01ebf5f4e1ad">#</a></div> <div class="comment-content">Thanks for writing this up, the tooling built into VS for MSTest makes it very attractive but it still gets a lot of hate. Nice to know why :)</div> <div class="comment-date">2010-05-11 10:36 UTC</div> </div> <div class="comment" id="0c506630675a48f2906a55fece0152a0"> <div class="comment-author"><a href="http://imaginarydevelopment.blogspot.com">Brandon Dimperio</a> <a href="#0c506630675a48f2906a55fece0152a0">#</a></div> <div class="comment-content">Is the MSTest code going to also be available somewhere? I'd hate to have to constantly be translating for code you already have available for us. - twitter @MaslowJax</div> <div class="comment-date">2010-06-19 16:46 UTC</div> </div> <div class="comment" id="c7cc170562d34607a7e2323231f7ef79"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c7cc170562d34607a7e2323231f7ef79">#</a></div> <div class="comment-content">I'm not sure exactly to which MSTest code you are referring, but in general I don't plan to change the existing MSTest code I've posted here on the blog. However, new tests are likely to appear with xUnit.net. In any case, when it comes to unit testing examples I don't think the differences are all that important. In most cases it's just a question of differently named attributes and slightly different Assert syntax...</div> <div class="comment-date">2010-06-20 06:39 UTC</div> </div> <div class="comment" id="7954f1393b6f4f0583bfc3767b8eed73"> <div class="comment-author">Fabricio <a href="#7954f1393b6f4f0583bfc3767b8eed73">#</a></div> <div class="comment-content">MSTest support parametrized tests using Pex</div> <div class="comment-date">2012-03-27 15:17 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 1.1 https://blog.ploeh.dk/2010/04/10/AutoFixture1.1 2010-04-10T10:25:23+00:00 Mark Seemann <div id="post"> <p><a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.1 is now available on the CodePlex site! Compared to the Release Candidate, there are no changes.</p> <p>The <a href="http://autofixture.codeplex.com/releases/view/43351">1.1 release page</a> has more details about this particular release, but essentially this is the RC promoted to release status.</p> <p>Release 1.1 is an interim release that addresses a few issues that appeared since the release of version 1.0. Work continues on AutoFixture 2.0 in parallel.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency Injection is Loose Coupling https://blog.ploeh.dk/2010/04/07/DependencyInjectionisLooseCoupling 2010-04-07T19:49:11+00:00 Mark Seemann <div id="post"> <p>It seems to me that I've lately encountered a particular mindset towards Dependency Injection (DI). People seem to think that it's only really good for replacing one data access implementation with another. Once you get to that point, you know that the following argument isn't far behind:</p> <blockquote> <p>“That's all well and good, but we know for certain that we will <em>never</em> exchange [insert name of RDBMS here] with anything else in this application.”</p></blockquote> <p>Apart from the hubris of making such a bold statement about the future of any software endeavor, such a statement reveals the narrow view on DI that its only purpose is for replacing data access components - and perhaps for unit testing.</p> <p>Those are relevant reasons for using DI, but they are only <em>some</em> of the reasons. Let's briefly revisit why we employ DI.</p> <p>We use DI to enable loose coupling.</p> <p>DI is only a means to an end. Even if you <em>never</em> intend to replace your database and even if you never want to write a single unit test, DI still offers benefits in form of a more maintainable code base. The loose coupling gives you better separation of concerns because it allows you to apply the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a>.</p> <p>Example coming right up:</p> <p>Imagine that we need to implement a PrécisViewModel class with a TopSellers property that returns an IEnumerable&lt;string&gt;. To implement this class, we have a data access component. Let's use the ubiquitous Repository pattern and define IProductRepository to see where that leads us:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IProductRepository</span> { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Product</span>&gt; SelectTopSellers(); }</pre> </p> <p>We can now implement PrécisViewModel like this:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">PrécisViewModel</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IProductRepository</span> repository; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> PrécisViewModel(<span style="color: #2b91af">IProductRepository</span> repository) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (repository == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"repository"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.repository = repository; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; TopSellers &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">get</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> topSellers = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.repository.SelectTopSellers(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">from</span> p <span style="color: blue">in</span> topSellers &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">select</span> p.Name; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>Nothing fancy is going on here. It's just straight Constructor Injection at work.</p> <p>Obviously, we can now implement and use a SQL Server-based repository:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> repository = <span style="color: blue">new</span> <span style="color: #2b91af">SqlProductRepository</span>(); <span style="color: blue">var</span> vm = <span style="color: blue">new</span> <span style="color: #2b91af">PrécisViewModel</span>(repository);</pre> </p> <p>So what does all this loose coupling buy us? It doesn't seem to help us a lot.</p> <p>The real benefit is not yet apparent, but it should become more obvious when we start adding requirements. Let's start with some caching. It turns out that the SelectTopSellers implementation is slow, so we would like to add some caching somewhere.</p> <p>Where should we add this caching functionality? Without loose coupling, we would more or less be constrained to adding it to either PrécisViewModel or SqlProductRepository, but both have issues:</p> <ul> <li>First of all we would be violating the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> (SRP) in both cases. <li>If we implement caching in PrécisViewModel, other consumers of the SelectTopSellers would not benefit from it. <li>If we implement caching in SqlProductRepository, it wouldn't be available for any other IProductRepository implementations.</li></ul> <p>Since the premise for this post is that we will <em>never</em> use any other database than SQL Server, implementing caching directly in SqlProductRepository sounds like the correct choice, but we would still be violating the SRP, and thus making our code more difficult to maintain.</p> <p>A better solution is to introduce a caching <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a> like this one:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">CachingProductRepository</span> : <span style="color: #2b91af">IProductRepository</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ICache</span> cache; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IProductRepository</span> repository; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> CachingProductRepository( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IProductRepository</span> repository, <span style="color: #2b91af">ICache</span> cache) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (repository == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"repository"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (cache == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"cache"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.cache = cache; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.repository = repository; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IProductRepository Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Product</span>&gt; SelectTopSellers() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.cache &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Retrieve&lt;<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Product</span>&gt;&gt;(<span style="color: #a31515">"topSellers"</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.repository.SelectTopSellers); &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>For completeness sake is here the definition of ICache:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ICache</span> { &nbsp;&nbsp;&nbsp; T Retrieve&lt;T&gt;(<span style="color: blue">string</span> key, <span style="color: #2b91af">Func</span>&lt;T&gt; readThrough); }</pre> </p> <p>The point is that CachingProductRepository <em>extends</em> any IProductRepository we provide to it (including SqlProductRepository) without modifying it. Thus, we have satisfied both the OCP and the SRP.</p> <p>Just to drive home the point, let us assume that we also wish to record execution times for various methods for purposes of SLA compliance. We can do this by introducing yet another Decorator:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">PerformanceMeasuringProductRepository</span> : &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IProductRepository</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IProductRepository</span> repository; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IStopwatch</span> stopwatch; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> PerformanceMeasuringProductRepository( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IProductRepository</span> repository, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IStopwatch</span> stopwatch) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (repository == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"repository"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (stopwatch == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"stopwatch"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.repository = repository; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.stopwatch = stopwatch; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IProductRepository Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Product</span>&gt; SelectTopSellers() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> timer = <span style="color: blue">this</span>.stopwatch &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .StartMeasuring(<span style="color: #a31515">"SelectTopSellers"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> topSellers = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.repository.SelectTopSellers(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.StopMeasuring(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> topSellers; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>Once again, we modified neither SqlProductRepository nor CachingProductRepository to introduce this new feature. We can implement security and auditing features by following the same principle.</p> <p>To me, this is what loose coupling (and DI) is all about. That we can also replace data access components and unit test using dynamic mocks are very fortunate side effects, but the loose coupling is valuable in itself because it enables us to write more maintainable code.</p> <p>We don't even need a DI Container to wire up all these repositories (although it <del datetime="2017-08-25T16:30:25.1593924+02:00">sure would</del> could be helpful). Here's how we can do it with <a href="/2014/06/10/pure-di">Pure DI</a>:</p> <p> <pre style="margin: 0px"><span style="color: #2b91af">IProductRepository</span> repository = &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">PerformanceMeasuringProductRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">CachingProductRepository</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">SqlProductRepository</span>(), <span style="color: blue">new</span> <span style="color: #2b91af">Cache</span>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">RealStopwatch</span>() &nbsp;&nbsp;&nbsp; ); <span style="color: blue">var</span> vm = <span style="color: blue">new</span> <span style="color: #2b91af">PrécisViewModel</span>(repository);</pre> </p> <p>The next time someone on your team claims that you don't need DI because the choice of RDBMS is fixed, you can tell them that it's irrelevant. The choice is between DI and Spaghetti Code.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="3a28cda060a34b699f23e4e125684adf"> <div class="comment-author">Arnis L <a href="#3a28cda060a34b699f23e4e125684adf">#</a></div> <div class="comment-content">That was marvelous post. Never thought about this kind of approach.<br> <br> Btw, i never figured out if there is anything why service locator isn't anti pattern. :)</div> <div class="comment-date">2010-04-08 11:27 UTC</div> </div> <div class="comment" id="48ef6bf4a7f7475ba330d7d98cd37b57"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#48ef6bf4a7f7475ba330d7d98cd37b57">#</a></div> <div class="comment-content">Thanks :)<br> <br> I'm not sure I understand your comment regarding Service Locator. It <em>is</em> an anti-pattern :)<br> <br> No, seriously, I never expected the entire world to just accept my word as gospel, and there are many people who disagree on this point. Did you have something specific in mind?</div> <div class="comment-date">2010-04-08 11:37 UTC</div> </div> <div class="comment" id="fc05cb4b00ab41ef93cd77357d40dfcb"> <div class="comment-author"><a href="http://mindinthewater.blogspot.com">wcoenen</a> <a href="#fc05cb4b00ab41ef93cd77357d40dfcb">#</a></div> <div class="comment-content">I recently listened to a short <a href="http://weblog.savanne.be/files/fosdem-mono-2010/monotorrent.odp">talk</a> by the MonoTorrent author at FOSDEM 2010. His presentation included an explanation of how (after running into maintenance hell first) he had separated the different concerns in his bittorrent <a href="http://anonsvn.mono-project.com/viewvc/trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/PiecePicking/">piece picking code</a> by implementing it as a series of decorators.<br> <br> For me the interesting thing about the talk was that apparently this &quot;separation of concerns&quot; thing had been an important enough discovery for him that it warranted the use of half the presentation time to explain, with the other half being spent talking about the dangers of multi-threading :-)</div> <div class="comment-date">2010-04-09 07:24 UTC</div> </div> <div class="comment" id="41c12f17c90b4920ba1c3ddf81487c45"> <div class="comment-author">Kshitij <a href="#41c12f17c90b4920ba1c3ddf81487c45">#</a></div> <div class="comment-content">Love, the blog spot. thanks for showing DI in action.</div> <div class="comment-date">2010-04-19 00:01 UTC</div> </div> <div class="comment" id="4f09d3f18dea4b77b79413c0bd1e4ba9"> <div class="comment-author"><a href="http://www.clear-lines.com/blog">Mathias</a> <a href="#4f09d3f18dea4b77b79413c0bd1e4ba9">#</a></div> <div class="comment-content">Totally off-topic comment, but I believe this is the first time I witness C# code with acute accents :) Do you really use accents in your code?</div> <div class="comment-date">2010-04-26 21:20 UTC</div> </div> <div class="comment" id="f377f87c8084486e9798f318d6f35aab"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f377f87c8084486e9798f318d6f35aab">#</a></div> <div class="comment-content">He he, no, normally I don't, but sometimes when writing sample code I like taking advantage of the fact that C# is based on Unicode. Somewhere here, I also have a sample that uses Danish characters (&#230;, &#248; or &#229;), but I can't remember which post it was :)</div> <div class="comment-date">2010-04-26 21:30 UTC</div> </div> <div class="comment" id="34ba5dd3a6414b5185e6aa5f3372d4e5"> <div class="comment-author"><a href="http://twitter.com/samuelpearson">Sam</a> <a href="#34ba5dd3a6414b5185e6aa5f3372d4e5">#</a></div> <div class="comment-content">Hey Mark,<br> A little off-topic, but how'd you implement Cache to force evaluation if it gets passed a Func&lt;IEnumerable&lt;Something&gt;&gt;? Else it will just cache the query.</div> <div class="comment-date">2010-10-08 22:17 UTC</div> </div> <div class="comment" id="5c34e9409b8a4f51916aa506da924f58"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5c34e9409b8a4f51916aa506da924f58">#</a></div> <div class="comment-content">Yes, you are right. <em>Perhaps</em> it will just cache the query - it actually depends on what the concrete implementation is. It may be an array or List&lt;T&gt;, in which case there is no issue.<br> <br> However, we could always specialize the implementation of the cache so that if T was IEnumerable, we'd invoke ToList() on it before caching the result.</div> <div class="comment-date">2010-10-09 07:04 UTC</div> </div> <div class="comment" id="682db8f92fdc41f4886e135b670db4c0"> <div class="comment-author"><a href="http://www.google.com/accounts/o8/id?id=AItOawmoyiZWGtGH4B-5d0X_KnFsJqRSqFK8p2k">www.google.com/accounts/o8/id?id=AItOawmoyiZWGtGH4B-5d0X_KnFsJqRSqFK8p2k</a> <a href="#682db8f92fdc41f4886e135b670db4c0">#</a></div> <div class="comment-content">Geat post, this shows clearly how you can chain functionality without violation OCP and SRP</div> <div class="comment-date">2011-04-07 08:34 UTC</div> </div> <div class="comment" id="ab9941987c7e4ddc8a77cfed2eae9563"> <div class="comment-author">Tom Stickel <a href="#ab9941987c7e4ddc8a77cfed2eae9563">#</a></div> <div class="comment-content">Awesome as usual. Once I drank in the Mark Seemann punch, I'm addicted to following how to do DI properly. <br> Thanks Mark. Any books from you scheduled for this year or the next?<br> <br> </div> <div class="comment-date">2012-01-15 19:04 UTC</div> </div> <div class="comment" id="53c7c4159eeb4b7e9ccb5927f4ebeccc"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#53c7c4159eeb4b7e9ccb5927f4ebeccc">#</a></div> <div class="comment-content">Thanks, Tom. No new book scheduled right now :)</div> <div class="comment-date">2012-01-15 19:33 UTC</div> </div> <div class="comment" id="4dce4fbc3c30437fa95d3098bcc2aed8"> <div class="comment-author">Alex <a href="#4dce4fbc3c30437fa95d3098bcc2aed8">#</a></div> <div class="comment-content">Hi Mark!<br> <br> What if IProductRepository has 15 methods but only one method should be cached?<br> <br> Or what if I don't need always the cache? So I have a ProductService that needs a IProductRepository. For 5 cases the ProducrtService would need the CachingProductRepository and for the rest the standard ProductRepository?</div> <div class="comment-date">2012-09-12 11:46 UTC</div> </div> <div class="comment" id="7e1897429ca44c929f3edc9c242501fe"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7e1897429ca44c929f3edc9c242501fe">#</a></div> <div class="comment-content">If you have 15 methods and only one should be cached, you can still cache the one method with a Decorator. The remaining 14 methods on that Decorator can be implemented as pure delegation.<br> <br> However, if you have this scenario, could it be that the interface violates the Interface Segregation Principle?</div> <div class="comment-date">2012-09-12 11:56 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Mapping types with AutoFixture https://blog.ploeh.dk/2010/04/06/MappingtypeswithAutoFixture 2010-04-06T05:22:32+00:00 Mark Seemann <div id="post"> <p>In my <a href="/2010/03/27/Freezingmocks">previous</a> <a href="/2010/03/26/Moreaboutfrozenpizza">posts</a> I demonstrated interaction-based unit tests that verify that a pizza is correctly being added to a shopping basket. An alternative is a state-based test where we examine the contents of the shopping basket after exercising the <a href="http://xunitpatterns.com/SUT.html">SUT</a>. Here's an initial attempt:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> AddWillAddToBasket() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; fixture.Register&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaMap</span>&gt;); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> basket = fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pizza = fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.Add(pizza); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.IsTrue(basket.Pizze.Any(p =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p.Name == pizza.Name), <span style="color: #a31515">"Basket has added pizza."</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>In this case the assertion examines the Pizze collection (you did know that the plural of <em>pizza</em> is <em>pizze</em>, right?) of the <a href="/2010/03/17/AutoFixtureFreeze">frozen Basket</a> to verify that it contains the added pizza.</p> <p>The tricky part is that the Pizze property is a collection of Pizza instances, and not PizzaPresenter instances. The injected IPizzaMap instance is responsible for mapping from PizzaPresenter to Pizza, but since we are rewriting this as a state-based test, I thought it would also be interesting to write the test without using <a href="http://code.google.com/p/moq/">Moq</a>. Instead, we can use the real implementation of IPizzaMap, but this means that we must instruct <a href="http://autofixture.codeplex.com/">AutoFixture</a> to map from the abstract IPizzaMap to the concrete PizzaMap.</p> <p>We see that happening in this line of code:</p> <p> <pre style="margin: 0px">fixture.Register&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;( &nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaMap</span>&gt;);</pre> </p> <p>Notice the method group syntax: we pass in a delegate to the CreateAnonymous method, which means that every time the fixture is asked to create an IPizzaMap instance, it invokes CreateAnonymous&lt;PIzzaMap&gt;() and uses the result.</p> <p>This is, obviously, a general-purpose way in which we can map compatible types, so we can write an extension method like this one:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Register&lt;TAbstract, TConcrete&gt;( &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span> <span style="color: #2b91af">Fixture</span> fixture) <span style="color: blue">where</span> TConcrete : TAbstract { &nbsp;&nbsp;&nbsp; fixture.Register&lt;TAbstract&gt;(() =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;TConcrete&gt;()); }</pre> </p> <p>(I'm slightly undecided on the name of this method. <em>Map</em> might be a better name, but I just like the equivalence to some common DI Containers and their Register methods.) Armed with this Register overload, we can now rewrite the previous Register statement like this:</p> <p> <pre style="margin: 0px">fixture.Register&lt;<span style="color: #2b91af">IPizzaMap</span>, <span style="color: #2b91af">PizzaMap</span>&gt;();</pre> </p> <p>It's the same amount of code lines, but I find it slightly more succinct and communicative.</p> <p>The real point of this blog post, however, is that you can map abstract types to concrete types, and that you can always write extension methods to encapsulate your own AutoFixture idioms.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="6fc4061421b3438ab31eca27514eee6f"> <div class="comment-author"><a href="http://nikosbaxevanis.com/">Nikos Baxevanis</a> <a href="#6fc4061421b3438ab31eca27514eee6f">#</a></div> <div class="comment-content">I also prefer Register (instead of Map). Because most of the times it will be like that: registering an interface to an implementation.</div> <div class="comment-date">2011-03-13 10:06 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 1.1 RC1 https://blog.ploeh.dk/2010/04/02/AutoFixture1.1RC1 2010-04-02T06:44:27+00:00 Mark Seemann <div id="post"> <p><a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.1 Release Candidate 1 is now available on the CodePlex site.</p> <p>Users are encouraged to evaluate this RC and submit feedback. If no bugs or issues are reported within the next week, we will promote RC1 to version 1.1.</p> <p>The <a href="http://autofixture.codeplex.com/releases/view/42969">release page</a> has more details about this particular release.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Freezing mocks https://blog.ploeh.dk/2010/03/27/Freezingmocks 2010-03-27T13:27:02+00:00 Mark Seemann <div id="post"> <p>My <a href="/2010/03/26/Moreaboutfrozenpizza">previous post</a> about <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s Freeze functionality included this little piece of code that I didn't discuss:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> mapMock = <span style="color: blue">new</span> <span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;(); fixture.Register(mapMock.Object);</pre> </p> <p>In case you were wondering, this is <a href="http://code.google.com/p/moq/">Moq</a> interacting with AutoFixture. Here we create a new <a href="http://xunitpatterns.com/Test%20Double.html">Test Double</a> and register it with the fixture. This is very similar to AutoFixture's built-in Freeze functionality, with the difference that we register an IPizzaMap instance, which isn't the same as the Mock&lt;IPizzaMap&gt; instance.</p> <p>It would be nice if we could simply freeze a Test Double emitted by Moq, but unfortunately we can't directly use the Freeze method, since Freeze&lt;Mock&lt;IPizzaMap&gt;&gt;() would freeze a Mock&lt;IPizzaMap&gt;, but not IPizzaMap itself. On the other hand, Freeze&lt;IPizzaMap&gt;() wouldn't work because we haven't told the fixture how to create IPizzaMap instances, but even if we had, we wouldn't have a Mock&lt;IPizzaMap&gt; against which we could call Verify.</p> <p>On the other hand, it's trivial to write an extension method to Fixture:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">Mock</span>&lt;T&gt; FreezeMoq&lt;T&gt;(<span style="color: blue">this</span> <span style="color: #2b91af">Fixture</span> fixture) &nbsp;&nbsp;&nbsp; <span style="color: blue">where</span> T : <span style="color: blue">class</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> td = <span style="color: blue">new</span> <span style="color: #2b91af">Mock</span>&lt;T&gt;(); &nbsp;&nbsp;&nbsp; fixture.Register(td.Object); &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> td; }</pre> </p> <p>I chose to call the method FreezeMoq to indicate its affinity with Moq.</p> <p>We can now rewrite the unit test from the previous post like this:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> AddWillPipeMapCorrectly_FreezeMoq() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> basket = fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> mapMock = fixture.FreezeMoq&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pizza = fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.Add(pizza); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add)); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>You may think that saving only a single line of code may not be that big a deal, but if you also need to perform Setups on the Mock, or if you have several different Mocks to configure, you may appreciate the encapsulation. I know I sure do.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="d88490509df1464aa551bba47bcfe8a9"> <div class="comment-author"><a href="http://mindinthewater.blogspot.com">Wim Coenen</a> <a href="#d88490509df1464aa551bba47bcfe8a9">#</a></div> <div class="comment-content">I am still not sure which problem is solved by a SUT factory like autofixture.<br> <br> You mentioned that adding an indirection between the tests and the SUT constructor helps with refactoring. But if I add a dependency to the SUT, I will still have to add calls to &quot;fixture.Register&quot; to fix my tests. And if I remove a dependency, then my tests will still work but the setup code will accumulate unnecessary cruft. It might be preferable to get a compiler error about a constructor argument which no longer exists.<br> <br> My own approach for minimizing the impact of refactorings on tests is to just store the SUT and mocks as fields of the test class, and create them in the TestInitialize/SetUp method. That way there is only one place were the constructor is called.</div> <div class="comment-date">2010-03-27 20:24 UTC</div> </div> <div class="comment" id="1351328ab0aa4656aba56b14ff4d9f05"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1351328ab0aa4656aba56b14ff4d9f05">#</a></div> <div class="comment-content">Wim, thank you for writing.<br> <br> Setting up your Test Fixture by populating fields on the test class is a common approach. However, I prefer not to do this, as it binds us very hard to the <a href="http://xunitpatterns.com/Testcase%20Class%20per%20Fixture.html">Testcase Class per Fixture</a> pattern. Although it may make sense in some cases, it requires us to add new test classes every time we need to vary the Test Fixture even the slightest, or we will end up with a <a href="http://xunitpatterns.com/Obscure%20Test.html#General Fixture">General Fixture</a>, which again leads to <a href="http://xunitpatterns.com/Obscure%20Test.html">Obscure Tests</a>.<br> <br> In my opinion, this leads to an explosion of test classes, and unless you are very disciplined, it becomes very difficult to figure out where to add a new test. This approach generates too much friction.<br> <br> Even without AutoFixture, a SUT Factory is superior because it's not tied to a single test class, and if desirable, you can vary it with overloads.<br> <br> The added benefit of AutoFixture is that its heuristic approach lets you concentrate on only the important aspects of a particular test case. Ideally, AutoFixture takes care of everything else by figuring out which values to supply for all those parameters you didn't explicitly supply.<br> <br> However, I can certainly understand your concern about unnecessary cruft. If we need a long sequence of fixture.Register calls to register dependencies then we certainly only introduce another level of maintainance hell. This leads us into an area I have yet to discuss, but I also use AutoFixture as an <b>auto-mocking container</b>.<br> <br> This means that I never explicitly setup mocks for all the dependencies needed by a SUT unless I actually need to configure it. AutoFixture will simply analyze the SUT's constructor and ask Moq (or potentially any another dymamic mock) to provide an instance. This approach works really well, but I have yet to blog about it because the AutoFixture API that supports automocking has not yet solidified. However, for hints on how to do this with the current version, see <a href="http://autofixture.codeplex.com/Thread/View.aspx?ThreadId=74061">this discussion</a>.</div> <div class="comment-date">2010-03-28 09:02 UTC</div> </div> <div class="comment" id="39d3bd9e8d0842de9edf40fab6be4acd"> <div class="comment-author">Wes <a href="#39d3bd9e8d0842de9edf40fab6be4acd">#</a></div> <div class="comment-content">I think there is a problem with the code sample you provided. I looked at the source code and did a small test. <br>I think the following is what you meant (or maybe this article i <br><pre>// does not work var td = new Mock(); fixture.Register(td.Object); // should work fixture.Inject(td.Object); fixture.Register(() => td.object);</pre></div> <div class="comment-date">2014-05-14 14:24 UTC</div> </div> <div class="comment" id="1651b7ec41d444988727dcd329c500f3"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1651b7ec41d444988727dcd329c500f3">#</a></div> <div class="comment-content"> <p> Wes, thank you for writing. You are indeed correct that this particular overload of the Register method no longer exists, and Inject is the correct method to use. See <a href="http://stackoverflow.com/a/18172472/126014">this Stack Overflow answer</a> for more details. </p> </div> <div class="comment-date">2014-05-15 09:33 UTC</div> </div> <div class="comment" id="fe84f9525fae436ca437e16743fe7e47"> <div class="comment-author">Tommy Vernieri <a href="#fe84f9525fae436ca437e16743fe7e47">#</a></div> <div class="comment-content"> <p> This article is out of date. Readers wishing to use AutoFixture with Moq should read <a href="/2010/08/19/AutoFixtureasanauto-mockingcontainer/">AutoFixture as an auto-mocking container</a>. </p> </div> <div class="comment-date">2018-08-27 02:21 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. More about frozen pizza https://blog.ploeh.dk/2010/03/26/Moreaboutfrozenpizza 2010-03-26T22:01:42+00:00 Mark Seemann <div id="post"> <p>In my <a href="/2010/03/17/AutoFixtureFreeze">previous blog post</a>, I introduced <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s Freeze feature, but the example didn't fully demonstrate the power of the concept. In this blog post, we will turn up the heat on the frozen pizza a notch.</p> <p>The following unit test exercises the BasketPresenter class, which is simply a wrapper around a Basket instance (we're doing a pizza online shop, if you were wondering). In true TDD style, I'll start with the unit test, but I'll post the BasketPresenter class later for reference.</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> AddWillPipeMapCorrectly() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> basket = fixture.Freeze&lt;<span style="color: #2b91af">Basket</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> mapMock = <span style="color: blue">new</span> <span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IPizzaMap</span>&gt;(); &nbsp;&nbsp;&nbsp; fixture.Register(mapMock.Object); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> pizza = fixture.CreateAnonymous&lt;<span style="color: #2b91af">PizzaPresenter</span>&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">BasketPresenter</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.Add(pizza); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; mapMock.Verify(m =&gt; m.Pipe(pizza, basket.Add)); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>The interesting thing in the above unit test is that we Freeze a Basket instance in the fixture. We do this because we know that the BasketPresenter <em>somehow</em> wraps a Basket instance, but we trust the Fixture class to figure it out for us. By telling the fixture instance to Freeze the Basket we know that it will reuse the same Basket instance throughout the entire test case. That includes the call to CreateAnonymous&lt;BasketPresenter&gt;.</p> <p>This means that we can use the frozen basket instance in the Verify call because we know that the same instance will have been reused by the fixture, and thus wrapped by the <a href="http://xunitpatterns.com/SUT.html">SUT</a>.</p> <p>When you stop to think about this on a more theoretical level, it fortunately makes a lot of sense. AutoFixture's terminology is based upon the excellent book <a href="http://xunitpatterns.com/">xUnit Test Patterns</a>, and a Fixture instance pretty much corresponds to the concept of a <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a>. This means that freezing an instance simply means that a particular instance is constant throughout that particular fixture. Every time we ask for an instance of that class, we get back the same frozen instance.</p> <p>In DI Container terminology, we just changed the Basket type's lifetime behavior from Transient to Singleton.</p> <p>For reference, here's the BasketPresenter class we're testing:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">BasketPresenter</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Basket</span> basket; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IPizzaMap</span> map; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> BasketPresenter(<span style="color: #2b91af">Basket</span> basket, <span style="color: #2b91af">IPizzaMap</span> map) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (basket == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"basket"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (map == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"map"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.basket = basket; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.map = map; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Add(<span style="color: #2b91af">PizzaPresenter</span> presenter) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.map.Pipe(presenter, <span style="color: blue">this</span>.basket.Add); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>If you are wondering about why this is interesting at all, and why we don't just pass in a Basket through the BasketPresenter's constructor, it's because we are using AutoFixture as a <a href="/2009/02/13/SUTFactory">SUT Factory</a>. We want to be able to refactor BasketPresenter (and in this case particularly its constructor) without breaking a lot of existing tests. The level of indirection provided by AutoFixture gives us just that ability because we never directly invoke the constructor.</p> <p>Coming up: <a href="/2010/03/27/Freezingmocks">more fun with the Freeze concept</a>!</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="88f95c23540f48068c817cab15efe576"> <div class="comment-author"><a href="http://LancerKind.com">Lancer Kind</a> <a href="#88f95c23540f48068c817cab15efe576">#</a></div> <div class="comment-content">Interesting, interesting!<br> It's not clear from the example how using AutoFixture for DI keeps our tests testing object behavior better than setting up dependencies through the constructor or setters. I think it will suffer the same problems as we're examining private data. It has one benefit in that our public APIs remain pristine from adding in special-ctors/Setters for DI.<br> <br> But hey, I'm going to play with this and see what happens. Thanks for the code sample!</div> <div class="comment-date">2012-09-04 15:26 UTC</div> </div> <div class="comment" id="d75ed6af40304230bd69997f2c39d662"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d75ed6af40304230bd69997f2c39d662">#</a></div> <div class="comment-content">I have an upcoming article on how to decouple tests from constructor signatures, so stay tuned.</div> <div class="comment-date">2012-09-04 19:05 UTC</div> </div> <div class="comment" id="27dff34b4fde4a59b8aa9b49fa724234"> <div class="comment-author"><a href="http://LancerKind.com">Lancer Kind</a> <a href="#27dff34b4fde4a59b8aa9b49fa724234">#</a></div> <div class="comment-content">One of the biggest complaints that architects have about TDD is allowing developers to redesign classes to allow for dependency injection. Even without strong change controls from top down, it would be convenient to be able to do DI without having to design for DI. (These cases come up with those doing Test Last rather than Test First/TDD.) Unfortunately this tool chain doesn't help this problem. Power Mock (Java: http://java.dzone.com/articles/using-powermock-mock) reprograms the class loader so this can be done without designing for DI. Perhaps a later release of this tool could as well. As of now, if I have to redesigning for DI[1], I don't need these other things as Moq already gives me this value.<br> <br> Am I missing something?<br> ==&gt;Lancer---<br> http://ConfessionsOfAnAgileCoach.blogspot.com<br> <br> <br> [1]<br> [code]private readonly Basket basket;<br> private readonly IPizzaMap map;<br> public BasketPresenter(Basket basket, IPizzaMap map) // &lt;-- redesign for DI<br> [/code]</div> <div class="comment-date">2012-09-18 15:49 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture Freeze https://blog.ploeh.dk/2010/03/17/AutoFixtureFreeze 2010-03-17T21:54:53+00:00 Mark Seemann <div id="post"> <p>One of the important points of <a href="http://autofixture.codeplex.com/">AutoFixture</a> is to hide away all the boring details that you don't care about when you are writing a unit test, but that the compiler seems to insist upon. One of these details is how you create a new instance of your <a href="http://xunitpatterns.com/SUT.html">SUT</a>.</p> <p>Every time you create an instance of your SUT using its constructor, you make it more difficult to refactor that constructor. This is particularly true when it comes to Constructor Injection because you often need to define a <a href="http://xunitpatterns.com/Test%20Double.html">Test Double</a> in each unit test, but even for primitive types, it's more maintenance-friendly to use a <a href="/2009/02/13/SUTFactory">SUT Factory</a>.</p> <p>AutoFixture is a SUT Factory, so we can use it to create instances of our SUTs. However, how do we correlate constructor parameters with variables in the test when we will not use the constructor directly?</p> <p>This is where the Freeze method comes in handy, but let's first examine how to do it with the core API methods <a href="/2009/04/02/CreatingStringsWithAutoFixture">CreateAnonymous</a> and <a href="/2009/04/23/DealingWithTypesWithoutPublicConstructors">Register</a>.</p> <p>Imagine that we want to write a unit test for a Pizza class that takes a name in its constructor and exposes that name as a property. We can write this test like this:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> NameIsCorrect() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> expectedName = fixture.CreateAnonymous(<span style="color: #a31515">"Name"</span>); &nbsp;&nbsp;&nbsp; fixture.Register(expectedName); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">Pizza</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.Name; &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual(expectedName, result, <span style="color: #a31515">"Name"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>The important lines are these two:</p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> expectedName = fixture.CreateAnonymous(<span style="color: #a31515">"Name"</span>); fixture.Register(expectedName);</pre> </p> <p>What's going on here is that we create a new string, and then we subsequently Register this string so that <em>every time</em> the fixture instance is asked to create a string, it will return this particular string. This also means that when we ask AutoFixture to create an instance of Pizza, it will use that string as the constructor parameter.</p> <p>It turned out that we used this coding idiom so much that we decided to encapsulate it in a convenience method. After some debate we arrived at the name Freeze, because we essentially freeze a single anonymous variable in the fixture, bypassing the default algorithm for creating new instances. Incidentally, this is one of very few methods in AutoFixture that breaks CQS, but although that bugs me a little, the Freeze concept has turned out to be so powerful that I live with it.</p> <p>Here is the same test rewritten to use the Freeze method:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> NameIsCorrect_Freeze() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> expectedName = fixture.Freeze(<span style="color: #a31515">"Name"</span>); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">Pizza</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.Name; &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual(expectedName, result, <span style="color: #a31515">"Name"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>In this example, we only save a single line of code, but apart from that, the test also becomes a little more communicative because it explicitly calls out that this particular string is frozen.</p> <p>However, this is still a pretty lame example, but while I intend to follow up with a more complex example, I wanted to introduce the concept gently.</p> <p>For completeness sake, here's the Pizza class:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Pizza</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">string</span> name; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> Pizza(<span style="color: blue">string</span> name) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (name == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"name"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.name = name; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">string</span> Name &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">get</span> { <span style="color: blue">return</span> <span style="color: blue">this</span>.name; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>As you can see, the test simply verifies that the constructor parameter is echoed by the Name property, and the Freeze method makes this more explicit while we still enjoy the indirection of not invoking the constructor directly.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="aec0c28ac69345b7b613cc85afa19ac2"> <div class="comment-author">DavidS <a href="#aec0c28ac69345b7b613cc85afa19ac2">#</a></div> <div class="comment-content">Hi,<br> <br> I am trying to understand how, in this particular example, AutoFixture makes the set up impervious to changes in the constructor. <br> <br> Say that for whatever reason the Pizza constructor takes another parameter e.g.<br> <br> public Pizza(string name, price decimal)<br> <br> Then surely, we'd have to update the test given. Am I missing something?</div> <div class="comment-date">2012-12-05 22:51 UTC</div> </div> <div class="comment" id="cfe4a7db41404587a1b511ba5b15e5e6"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cfe4a7db41404587a1b511ba5b15e5e6">#</a></div> <div class="comment-content">The CreateAnonymous method reflects over the constructor to figure out which arguments are required. If you add a decimal to the constructor, it's just going to pick that up and supply an anonymous value of decimal. No update of the test is required.<br> <br> Try it out :)</div> <div class="comment-date">2012-12-06 08:45 UTC</div> </div> <div class="comment" id="470c13de1edb4845aba76705755f9943"> <div class="comment-author">DavidS <a href="#470c13de1edb4845aba76705755f9943">#</a></div> <div class="comment-content">Hey Mark,<br> <br> Thanks for the prompt reply. Now, I think I understand what's going. Effectively, I was not appreciating that the purpose of &quot;Freezing&quot; was to have the string parameter &quot;Name&quot; &quot;frozen&quot; so that the assertion could be made against a known value. <br> <br> But your explanation has clarified the issue. Thanks very much. </div> <div class="comment-date">2012-12-06 13:37 UTC</div> </div> <div class="comment" id="8033e0960f09480c937aed0bb3168148"> <div class="comment-author">WesM <a href="#8033e0960f09480c937aed0bb3168148">#</a></div> <div class="comment-content">I&#39;m curious about how to have different customizations for the same type of object. Example: <br> <pre><span style='color:#800000; font-weight:bold;'>new</span> Pizza<span style='color:#808030;'>(</span><span style='color:#800000; font-weight:bold;'>string</span> fancyName<span style='color:#808030;'>,</span> <span style='color:#800000; font-weight:bold;'>string</span> boringName<span style='color:#808030;'>)</span></pre> </div> <div class="comment-date">2014-03-18 03:46 UTC</div> </div> <div class="comment" id="4d4fb9c184db44ecb99009e62ab1be00"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4d4fb9c184db44ecb99009e62ab1be00">#</a></div> <div class="comment-content"> <p> WesM, thank you for writing. Perhaps you'll find <a href="/2010/10/19/Convention-basedCustomizationswithAutoFixture">this article</a> helpful. </p> </div> <div class="comment-date">2014-03-18 07:33 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. CNUG TDD talk https://blog.ploeh.dk/2010/02/08/CNUGTDDtalk 2010-02-08T19:15:41+00:00 Mark Seemann <div id="post"> <p>As part of the Copenhagen .NET User Group (CNUG) winter and early autumn schedule, I'll be giving a talk on (slightly advanced) TDD.</p> <p>The talk will be in Danish and takes place April 15th, 2010. More details and sign-up <a href="http://www.eventbrite.com/event/567027996/">here</a>.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Service Locator is an Anti-Pattern https://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern 2010-02-03T21:49:39+00:00 Mark Seemann <div id="post"> <p> Service Locator is a well-known pattern, and since it was <a href="http://martinfowler.com/articles/injection.html">described by Martin Fowler</a>, it must be good, right? </p> <p> No, it's actually an <strong>anti-pattern</strong> and should be avoided. </p> <p> Let's examine why this is so. In short, the problem with Service Locator is that it hides a class' dependencies, causing run-time errors instead of compile-time errors, as well as making the code more difficult to maintain because it becomes unclear when you would be introducing a breaking change. </p> <h3 id="367d9875cdde42d797600de9d98ba573"> OrderProcessor example <a href="#367d9875cdde42d797600de9d98ba573" title="permalink">#</a> </h3> <p> As an example, let's pick a hot topic in DI these days: an OrderProcessor. To process an order, the OrderProcessor must validate the order and ship it if valid. Here's an example using a static Service Locator: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">OrderProcessor</span> : <span style="color: #2b91af">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Process(<span style="color: #2b91af">Order</span> order) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> validator = <span style="color: #2b91af">Locator</span>.Resolve&lt;<span style="color: #2b91af">IOrderValidator</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (validator.Validate(order)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> shipper = <span style="color: #2b91af">Locator</span>.Resolve&lt;<span style="color: #2b91af">IOrderShipper</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shipper.Ship(order); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The Service Locator is used as a replacement for the <em>new</em> operator. It looks like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">Locator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">static</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">object</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; services = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">object</span>&gt;&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Register&lt;T&gt;(<span style="color: #2b91af">Func</span>&lt;T&gt; resolver) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Locator</span>.services[<span style="color: blue">typeof</span>(T)] = () =&gt; resolver(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> T Resolve&lt;T&gt;() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> (T)<span style="color: #2b91af">Locator</span>.services[<span style="color: blue">typeof</span>(T)](); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Reset() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Locator</span>.services.Clear(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> We can configure the Locator using the Register method. A ‘real' Service Locator implementation would be much more advanced than this, but this example captures the gist of it. </p> <p> This is flexible and extensible, and it even supports replacing services with Test Doubles, as we will see shortly. </p> <p> Given that, then what could be the problem? </p> <h3 id="40d3f71708ef40b79e5eedc34c73748b"> API usage issues <a href="#40d3f71708ef40b79e5eedc34c73748b" title="permalink">#</a> </h3> <p> Let's assume for a moment that we are simply consumers of the OrderProcessor class. We didn't write it ourselves, it was given to us in an assembly by a third party, and we have yet to look at it in Reflector. </p> <p> This is what we get from IntelliSense in Visual Studio: </p> <p> <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="/content/binary/WindowsLiveWriter/ServiceLocatorisanAntiPattern_F652/image_3.png" width="317" height="52"> </p> <p> Okay, so the class has a default constructor. That means I can simply create a new instance of it and invoke the Process method right away: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> order = <span style="color: blue">new</span> <span style="color: #2b91af">Order</span>(); <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">OrderProcessor</span>(); sut.Process(order);</pre> </p> <p> Alas, running this code surprisingly throws a KeyNotFoundException because the IOrderValidator was never registered with Locator. This is not only surprising, it may be quite baffling if we don't have access to the source code. </p> <p> By perusing the source code (or using Reflector) or consulting the documentation (ick!) we may finally discover that we need to register an IOrderValidator instance with Locator (a completely unrelated static class) before this will work. </p> <p> In a unit test test, this can be done like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> validatorStub = <span style="color: blue">new</span> <span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IOrderValidator</span>&gt;(); validatorStub.Setup(v =&gt; v.Validate(order)).Returns(<span style="color: blue">false</span>); <span style="color: #2b91af">Locator</span>.Register(() =&gt; validatorStub.Object);</pre> </p> <p> What is even more annoying is that because the Locator's internal store is static, we need to invoke the Reset method after each unit test, but granted: that is mainly a unit testing issue. </p> <p> All in all, however, we can't reasonably claim that this sort of API provides a positive developer experience. </p> <h3 id="90e00e721c2a45589a746a5253810667"> Maintenance issues <a href="#90e00e721c2a45589a746a5253810667" title="permalink">#</a> </h3> <p> While this use of Service Locator is problematic from the consumer's point of view, what seems easy soon becomes an issue for the maintenance developer as well. </p> <p> Let's say that we need to expand the behavior of OrderProcessor to also invoke the <a href="/2010/02/02/RefactoringtoAggregateServices">IOrderCollector.Collect method</a>. This is easily done, or is it? </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> Process(<span style="color: #2b91af">Order</span> order) { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> validator = <span style="color: #2b91af">Locator</span>.Resolve&lt;<span style="color: #2b91af">IOrderValidator</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (validator.Validate(order)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> collector = <span style="color: #2b91af">Locator</span>.Resolve&lt;<span style="color: #2b91af">IOrderCollector</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; collector.Collect(order); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> shipper = <span style="color: #2b91af">Locator</span>.Resolve&lt;<span style="color: #2b91af">IOrderShipper</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shipper.Ship(order); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> From a pure mechanistic point of view, that was easy - we simply added a new call to Locator.Resolve and invoke IOrderCollector.Collect. </p> <p> Was this a breaking change? </p> <p> This can be surprisingly hard to answer. It certainly compiled fine, but broke one of my unit tests. What happens in a production application? The IOrderCollector interface may already be registered with the Service Locator because it is already in use by other components, in which case it will work without a hitch. On the other hand, this may not be the case. </p> <p> The bottom line is that it becomes a lot harder to tell whether you are introducing a breaking change or not. You need to understand the <em>entire</em> application in which the Service Locator is being used, and the compiler is not going to help you. </p> <h3 id="2d1fc2185a204b81914a7e92bc8866f8"> Variation: Concrete Service Locator <a href="#2d1fc2185a204b81914a7e92bc8866f8" title="permalink">#</a> </h3> <p> Can we fix these issues in some way? </p> <p> One variation commonly encountered is to make the Service Locator a concrete class, used like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> Process(<span style="color: #2b91af">Order</span> order) { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> locator = <span style="color: blue">new</span> <span style="color: #2b91af">Locator</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> validator = locator.Resolve&lt;<span style="color: #2b91af">IOrderValidator</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (validator.Validate(order)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> shipper = locator.Resolve&lt;<span style="color: #2b91af">IOrderShipper</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shipper.Ship(order); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> However, to be configured, it still needs a static in-memory store: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Locator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">static</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">object</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; services = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">object</span>&gt;&gt;(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Register&lt;T&gt;(<span style="color: #2b91af">Func</span>&lt;T&gt; resolver) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Locator</span>.services[<span style="color: blue">typeof</span>(T)] = () =&gt; resolver(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> T Resolve&lt;T&gt;() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> (T)<span style="color: #2b91af">Locator</span>.services[<span style="color: blue">typeof</span>(T)](); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Reset() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Locator</span>.services.Clear(); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> In other words: there's no structural difference between the concrete Service Locator and the static Service Locator we already reviewed. It has the same issues and solves nothing. </p> <h3 id="c74efcff64084de68a0cc3ee0a9a21e5"> Variation: Abstract Service Locator <a href="#c74efcff64084de68a0cc3ee0a9a21e5" title="permalink">#</a> </h3> <p> A different variation seems more in line with true DI: the Service Locator is a concrete class implementing an interface. </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IServiceLocator</span> { &nbsp;&nbsp;&nbsp; T Resolve&lt;T&gt;(); } &nbsp; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Locator</span> : <span style="color: #2b91af">IServiceLocator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">object</span>&gt;&gt; services; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> Locator() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.services = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">object</span>&gt;&gt;(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Register&lt;T&gt;(<span style="color: #2b91af">Func</span>&lt;T&gt; resolver) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.services[<span style="color: blue">typeof</span>(T)] = () =&gt; resolver(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> T Resolve&lt;T&gt;() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> (T)<span style="color: blue">this</span>.services[<span style="color: blue">typeof</span>(T)](); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> With this variation it becomes necessary to inject the Service Locator into the consumer. <strong>Constructor Injection</strong> is always a good choice for injecting dependencies, so OrderProcessor morphs into this implementation: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">OrderProcessor</span> : <span style="color: #2b91af">IOrderProcessor</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IServiceLocator</span> locator; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> OrderProcessor(<span style="color: #2b91af">IServiceLocator</span> locator) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (locator == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"locator"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.locator = locator; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Process(<span style="color: #2b91af">Order</span> order) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> validator = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.locator.Resolve&lt;<span style="color: #2b91af">IOrderValidator</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (validator.Validate(order)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> shipper = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.locator.Resolve&lt;<span style="color: #2b91af">IOrderShipper</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shipper.Ship(order); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Is this good, then? </p> <p> From a developer perspective, we now get a bit of help from IntelliSense: </p> <p> <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="/content/binary/WindowsLiveWriter/ServiceLocatorisanAntiPattern_F652/image_6.png" width="474" height="47"> </p> <p> What does this tell us? Nothing much, really. Okay, so OrderProcessor needs a ServiceLocator - that's a bit more information than before, but it still doesn't tell us <em>which services</em> are needed. The following code compiles, but crashes with the same KeyNotFoundException as before: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> order = <span style="color: blue">new</span> <span style="color: #2b91af">Order</span>(); <span style="color: blue">var</span> locator = <span style="color: blue">new</span> <span style="color: #2b91af">Locator</span>(); <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">OrderProcessor</span>(locator); sut.Process(order);</pre> </p> <p> From the maintenance developer's point of view, things don't improve much either. We still get no help if we need to add a new dependency: is it a breaking change or not? Just as hard to tell as before. </p> <h3 id="6a2f69cc19bb4e51a4c98f59e74d2bf9"> Summary <a href="#6a2f69cc19bb4e51a4c98f59e74d2bf9" title="permalink">#</a> </h3> <p> The problem with using a Service Locator isn't that you take a dependency on a particular Service Locator implementation (although that may be a problem as well), but that it's a bona-fide <strong>anti-pattern</strong>. It will give consumers of your API a horrible developer experience, and it will make your life as a maintenance developer worse because you will need to use considerable amounts of brain power to grasp the implications of every change you make. </p> <p> The compiler can offer both consumers and producers so much help when <strong>Constructor Injection</strong> is used, but none of that assistance is available for APIs that rely on Service Locator. </p> <p> You can read more about DI patterns and anti-patterns in <a href="http://amzn.to/12p90MG">my book</a>. </p> <p> <strong>Update 2014-05-20:</strong> Another way to explain the negative aspects of Service Locator is that <a href="/2014/05/15/service-locator-violates-solid">it violates SOLID</a>. </p> <p> <strong>Update 2015-10-26:</strong> The fundamental problem with Service Locator is that <a href="/2015/10/26/service-locator-violates-encapsulation">it violates encapsulation</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e89cd2ec7a294e3ea9daf86cbd2ec4ba"> <div class="comment-author"><a href="http://www.janusknudsen.dk">Janus</a> <a href="#e89cd2ec7a294e3ea9daf86cbd2ec4ba">#</a></div> <div class="comment-content">I couldn't agree more on this :), thank you for different examples.<br> <br> I would like to hear your opinion, what do you think about a service locator that operates with dependency injection? So when you call the service locator it returns the requested type that was specified in the di wiring?<br> For instance.. many DI framework have trouble working without constructors as in .aspx, .asmx, .svc, how would you solve such scenario?</div> <div class="comment-date">2010-02-03 23:17 UTC</div> </div> <div class="comment" id="91a0d7c93d704d29a9190dc0d5e5bd5b"> <div class="comment-author">FZelle <a href="#91a0d7c93d704d29a9190dc0d5e5bd5b">#</a></div> <div class="comment-content">Sure, if you don't understand how something was meant to be used you can missuse everything.<br> A hammer is only aprobiate when you need to hit on something.<br> <br> Servicelocator is only useable if you need to create dynamic objects inside your class,<br> otherwise it really is an antipattern.<br> <br> The right thing to do is use Injection where the dependency tree is fixed and ServiceLocator<br> where you are sure you need to dynamicaly create something new ( in a factory for ex. ).<br> </div> <div class="comment-date">2010-02-04 09:54 UTC</div> </div> <div class="comment" id="c9c7aeeea45b498a8f03b4049073add7"> <div class="comment-author"><a href="http://www.humblecoder.co.uk">Will</a> <a href="#c9c7aeeea45b498a8f03b4049073add7">#</a></div> <div class="comment-content">I always struggle to do IoC without service locator for the exact reasons you stated, how do you get rid of all pesky little new operators. Much of the advice I've read is that passing around a container is a bad idea.<br> <br> Lately, I've been using AutoFac more and more which, I feel, helps with these type of problems by using delegate factories. Be interested to hear your thoughts on if this is a good compromise?<br> <br> BTW, nice pimp of your book, I might buy it if it's all this thought provoking :)<br> <br> </div> <div class="comment-date">2010-02-04 10:00 UTC</div> </div> <div class="comment" id="e70a961bf5164464b0cd822972d4ac7a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e70a961bf5164464b0cd822972d4ac7a">#</a></div> <div class="comment-content">It's true that frameworks such as ASP.NET, PowerShell and the MMC SDK (but <em>not</em> WCF) are inherently DI-unfriendly because they insist on managing the lifetime of important objects. ASP.NET Page objects are the most well-known example.<br> <br> In such cases you really have no recourse but to move the <b>Composition Root</b> into each object (e.g. Page) and let your DI Container wire up your dependencies from there. This may look like the Service Locator anti-pattern, but it isn't because you still keep container usage at an absolute minimum. The difference is that the 'absolute minimum' in this case is inside the constructor or Initialize method of each and every object (e.g. Page) created by the framework (e.g. ASP.NET).<br> <br> At that point, we need to ask our DI Container to wire up the entire object graph for further use. This also means that we should treat the object where we do this as a <b>Humble Object</b> whose only responsibility is to act as an Adapter between the framework and our own code that uses proper DI patterns.<br> <br> In ASP.NET, we can pull the container from the Application object so that we can have a single, shared container that can track long-lived (i.e. Singleton-scoped) dependencies without having to resort to a static container. This is by far the best option because there will be no 'virtual new operator' available further down the call stack - you can't call Locator.Resolve&lt;IMyDependency&gt;() because there will be no static container.<br> <br> The Hollywood principle still applies to the rest of the application: <b>Don't call the container, it'll call you</b>. Instead of bootsrapping the application in one go from its entry point (as we can do in ASP.NET MVC) we need to bootstrap each created object individually, but <em>from there on down</em>, there will be no Service Locator available.</div> <div class="comment-date">2010-02-04 10:18 UTC</div> </div> <div class="comment" id="ead76277d65a4f95b24b6bd37a670176"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ead76277d65a4f95b24b6bd37a670176">#</a></div> <div class="comment-content">@FZelle: I don't agree that Service Locator is ever appropriate. The standard solution if you need short-lived or dynamically created objects is to use an injected <b>Abstract Factory</b>.</div> <div class="comment-date">2010-02-04 10:32 UTC</div> </div> <div class="comment" id="88198a7033d64303970fe3a97ba4d364"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#88198a7033d64303970fe3a97ba4d364">#</a></div> <div class="comment-content">@Will: Autofac is just one among several DI Containers. I'm not yet that familiar with it, but if Delegate Factories are comparable to Windsor's Typed Factory Facility, it sounds like a good approach. In general <b>Constructor Injection</b> is preferrable, but there are many cases where you need a short-lived object.<br> <br> This is where <b>Abstract Factories</b> bridge the gap. What is better about an Abstract Factory compared to a Service Locator is that it is strongly typed: you can't just ask it for any dependency, but only for instances of specific types.</div> <div class="comment-date">2010-02-04 10:46 UTC</div> </div> <div class="comment" id="a652f07710194366bc8122102099259e"> <div class="comment-author">Huy Nguyen <a href="#a652f07710194366bc8122102099259e">#</a></div> <div class="comment-content">I just cannot agree. The problem with IntelliSense or Compiler doesn't make sense to reject the use of service locator. IMHO, the only problem for the misuse here is that the developer (or maintenance developer) does not know what service locator is and how it works. What about using Convention Over Configuration that we don't need to include the configuration file but specifying all the contracts and default implementations then initialize them at bootstrapper. Also, if you think the error is only found at runtime, we can do unit test to make sure all the dependencies are configured correctly before the component is delivered.</div> <div class="comment-date">2010-02-05 04:03 UTC</div> </div> <div class="comment" id="74aa0871eb7c4b3594247b1d7cd82689"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#74aa0871eb7c4b3594247b1d7cd82689">#</a></div> <div class="comment-content">Huy Nguyen<br> <br> Thank you for your comment.<br> <br> Missing IntelliSense and compiler support are just two symptoms of a poorly modeled object model. You are in your right to disagree, but I consider good API design to be as explicit as possible and adhere to the <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment">Principle of Least Astonishment</a>. It really has nothing to do with whether I, as a developer, understand the Service Locator (anti-)pattern or not.<br> <br> A good API should follow design principles for reusable libraries. It doesn't really matter whether you are building a true reusable library, or just a single application for internal use. If you want it to be maintainable, it must behave in a non-surprising and consistent manner, and not require you to have intimate knowledge of the inner workings of each class. This is one of the driving forces behind <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215">Domain-Driven Design</a>, as well as such principles as <a href="http://en.wikipedia.org/wiki/Command-query_separation">Command-Query Separation</a>.<br> <br> Any class that internally uses a Service Locator isn't Clean Code because it doesn't communicate its intent or requirements.<br> <br> Unit tests may help, but it would be symptomatic treatment instead of driving to the core of the problem. The benefit of unit testing is that it provides us with faster feedback than integration testing or systems testing would do. That's all fine, but the compiler provides even faster feedback, so why should I resort to unit testing if the compiler can give me feedback sooner?</div> <div class="comment-date">2010-02-05 10:13 UTC</div> </div> <div class="comment" id="5e6a25819b154a6390a91c6e125d7a73"> <div class="comment-author">Arnis L. <a href="#5e6a25819b154a6390a91c6e125d7a73">#</a></div> <div class="comment-content">It's not an anti-pattern.<br> <br> Actually - `anti-pattern` is not an antonym to `pattern`. Pattern does NOT say that something is cool and good per se in every imaginable situation.<br> <br> Conclusion:<br> <br> while these are definitely correct and good points you make and everyone should be aware of them, we return back to those boring phrases like =&gt;<br> <br> &quot;use right tool for the job&quot;<br> <br> and<br> <br> &quot;it depends...&quot;</div> <div class="comment-date">2010-02-06 21:19 UTC</div> </div> <div class="comment" id="9edcc7ae5db14b708f6ec4e3927d94c2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9edcc7ae5db14b708f6ec4e3927d94c2">#</a></div> <div class="comment-content">Arnis L.<br> <br> Thank you for writing.<br> <br> At the risk of taking the discussion in the wrong direction, &quot;AntiPatterns&quot; [Brown et al., Wiley, 1998] define an anti-pattern as a &quot;commonly ocurring solution to a problem that generates decidedly negative consequences&quot; (p. 7). According to that definition, Service Locator is an anti-pattern.<br> <br> The reason I insist on protracting this semantic debate is that I have yet to see a valid case <em>for</em> Service Locator. There may be occasions where we need to invoke container.Resolve from deeper in the application than we would ideally have liked (the ASP.NET case described above in point), but that isn't the Service Locator anti-pattern in play, but rather a set of distributed <b>Composition Roots</b>.<br> <br> To me, Service Locator is when you use the Resolve method as a sort of 'virtual new operator', and that is just never the right tool for the job.</div> <div class="comment-date">2010-02-06 21:45 UTC</div> </div> <div class="comment" id="d961c1f2745147cd853bb9a007669bfe"> <div class="comment-author">Arnis L. <a href="#d961c1f2745147cd853bb9a007669bfe">#</a></div> <div class="comment-content">People don't act accordingly to mentioned definition. That's their nature. Anyway - direction is surely wrong.<br> What's interesting - i started to feel like you - can't find reason for service locator.<br> Good thing is - you gave a great push. At least - for me. Currently - clearing out confusion about IoC in my head.<br> Bad thing is - learning process is still in progress and i haven't found feeling that 'i know' to wholeheartedly agree with you.<br> Must confess that I've used related tools and techniques without necessary knowledge. :)</div> <div class="comment-date">2010-02-06 23:11 UTC</div> </div> <div class="comment" id="6b0d77fe52804d0e893a166925611d4d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6b0d77fe52804d0e893a166925611d4d">#</a></div> <div class="comment-content">To be fair, I must admit that I'm puposefully being rigid and unrelenting in my tone to get the point across :)<br> <br> Here's an interesting confession: There are a few places in Safewhere's production code where we call Resolve() pretty deep within the bowels of the application. You could well argue that we apply Service Locator there. The point, however, is that I still don't consider those few applications valid, but rather as failures on my part to correctly model our abstractions. One day, I still hope to get them right so I can get rid of those last pieces, and I actually managed to remove one dirty part of that particular API just this week.<br> <br> We are all only fallible humans, but that doesn't stop me from striving towards perfection :)</div> <div class="comment-date">2010-02-06 23:25 UTC</div> </div> <div class="comment" id="95c2ac0c11d3400397be355f20b9bea1"> <div class="comment-author">FZelle <a href="#95c2ac0c11d3400397be355f20b9bea1">#</a></div> <div class="comment-content">@Mark:<br> And that abstract Factory is useing new xyz()?<br> No that Factory than needs the Servicelocator.<br> </div> <div class="comment-date">2010-02-08 09:27 UTC</div> </div> <div class="comment" id="630a3a03a64b4c72a8e9943ba237cf8e"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#630a3a03a64b4c72a8e9943ba237cf8e">#</a></div> <div class="comment-content">@FZelle: The Abstract Factory needs no dependencies since it's an abstraction (interface/abstract base class).<br> <br> Concrete implementations of an Abstract Factory may very well need specific dependencies, which it can request via <b>Constructor Injection</b> - just like any other service.<br> <br> The DI Container will then wire up the concrete factory with its dependencies just like it wires up any other service. No Service Locator is ever needed.<br> <br> <a href="http://stackoverflow.com/questions/1926826/cant-combine-factory-di/1927167#1927167">Here's</a> just one example demonstrating what I mean.</div> <div class="comment-date">2010-02-08 14:25 UTC</div> </div> <div class="comment" id="49c65fb5622b404fa1ae100ce647f81b"> <div class="comment-author">Andrei Alecu <a href="#49c65fb5622b404fa1ae100ce647f81b">#</a></div> <div class="comment-content">In some advanced usage scenarios, dependencies are not always set in stone and known at design time, or even at application startup time. They could even change several times throughout the run-time of the application, so, having a service locator which can manage this reconfiguration aspect from a single place can be preferred. <br> <br> This is similar to an abstract factory pattern, but it's much more feasible to implement when you have a ton of dependencies.<br> <br> Additionally, if you consider a service oriented architecture where the services are well known and are always guaranteed to exist, but you just don't know where (could be on a separate server via WCF), then the service locator pattern solves a lot of problems.<br> <br> Passing a ton of dependencies through dependency injection every time leads to unreadable code in my opinion. If by convention, the service is guaranteed to always exist, then Service Locator is a valuable pattern.<br> <br> I would consider this to be an application of the Convention over Configuration paradigm.<br> <br> However, it is a pattern that should only be used where it makes sense, I can see how abusing it would create more problems than it solves.</div> <div class="comment-date">2010-05-11 14:14 UTC</div> </div> <div class="comment" id="00385d856b3d4c3a9d7d7704cdfb4502"> <div class="comment-author"><a href="http://kozmic.pl">Krzysztof Kozmic</a> <a href="#00385d856b3d4c3a9d7d7704cdfb4502">#</a></div> <div class="comment-content">@Andrei Alecu<br> <br> Modern IoC containers let you resolve dependencies at runtime without using service locator. Windsor for example has several places where you can plug in to provide the dependencies, DynamicParameters method or OnCreated are just two examples from the top of my head. More generic solutions like handler selectors or subdependencyresolvers also exist.<br> <br> In cases where you do need to trigger resolution of a dependency from the call site many containers provide autogenerated abstract factories (Windsor has TypedFactoryFacility which creates interface based factories and in trunk (v2.5) also delegate based factories).<br> <br> So in my opinion using SL is just an excuse for being lazy. The only place where I'd use it (temporarily) is when migrating old, legacy code that can't be easily changed, as an intermediate step.</div> <div class="comment-date">2010-06-11 03:52 UTC</div> </div> <div class="comment" id="663c34d6d38f44e0a2b33695f5f436e9"> <div class="comment-author">Iran Hutchinson <a href="#663c34d6d38f44e0a2b33695f5f436e9">#</a></div> <div class="comment-content">Just came across this discussion. My opinion is whether you use ServiceLocator or not depends on your application requirements. What if you have to run inside or outside of a container environment? A service locator does make that situation much easier. What about SOA [implementation, not theory :) ]? SOA generally involves some service registry and service lookup. You can definitely look at that as a ServiceLocator implementation. There are good SOA design that have great merit, especially in a distributed environment where services are not guaranteed to be local. <br> <br> The strength or weakness of your choice and use of patterns depends on how they meet your requirements. There is no one golden way. Dependency Injections is not a cure all nor was service locator when it became widely used. I use both and many others.<br> <br> And how many patterns can you fit into the definition of &quot;Anti-Pattern&quot; if it is misused / abused?</div> <div class="comment-date">2010-07-21 23:54 UTC</div> </div> <div class="comment" id="c5cc61315a4c47b8929b90e2aa865737"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c5cc61315a4c47b8929b90e2aa865737">#</a></div> <div class="comment-content">What do you mean by running &quot;inside or outside of a container environment&quot;? If you design your software appropriately, the presence of a DI container has no impact on the overall dependency usage. A DI container should resolve the appropriate object graph and get out of the way. This is what I call the <b>Hollywood Principle for DI Containers</b>: <em>Don't call the container - it will call you</em>.<br> <br> In my opinion the SOA discussion is completely orthogonal to the discussion about the Service Locator (anti-)pattern. We shouldn't be misled by the similarity of names. The <em>services</em> located by a Service Locator are object services that may or may not encapsulate or represent an external resource. That external resource may be a web service, but could be something entirely different. When we discuss the Service Locator (anti-)pattern, we discuss it in the context of object-oriented design. Whether or not the underlying implementation is a web service is irrelevant at that level.<br> <br> That said, I'm fully aware that many SOA environments work with a service registry. UDDI was the past attempt at making this work, while today we have protocols such as WS-Discovery. These can still be considered implementations of 'design patterns', but they certainly operate on a different architectural level. They have nothing to do with the Service Locator (anti-)pattern.<br> <br> Nothing prevents us from combining proper Dependency Injection with service registries. They are unrelated because they operate at different levels, so they can be combined with exact the same flexibility as data access and UI technologies.<br> <br> If we go back to the (original?) definition of the term <em>anti-pattern</em> from <a href="http://www.amazon.com/AntiPatterns-Refactoring-Software-Architectures-Projects/dp/0471197130">the book</a>, an anti-pattern is characterized by a &quot;commonly occurring solution to a problem that generates decidedly negative consequences&quot;. By that definition, Service Locator is an anti-pattern - even if you can come up with niche scenarios where it makes sense. So far, I've never been presented with a case for Service Locator where I haven't been able to come up with a better design that uses proper DI.</div> <div class="comment-date">2010-07-24 12:29 UTC</div> </div> <div class="comment" id="3600452291b9432fbb3549f36999b073"> <div class="comment-author"><a href="http://karlshifflett.wordpress.com/">Karl Shifflett</a> <a href="#3600452291b9432fbb3549f36999b073">#</a></div> <div class="comment-content">Sauce b&#233;arnaise is a great way to start the book.<br> <br> I first learned how to prepare sauce b&#233;arnaise when I started making Sole Wellington.<br> <br> Reading book now, nice.<br> <br> Cheers,<br> <br> Karl</div> <div class="comment-date">2010-07-31 17:04 UTC</div> </div> <div class="comment" id="198b00bb699042fe9891c59d00da5190"> <div class="comment-author">Iran Hutchinson <a href="#198b00bb699042fe9891c59d00da5190">#</a></div> <div class="comment-content">I have multi-language / platform responsibilities which include .NET and Java Prior to DI being popular in Java many of us had requirements to run inside container (EJB, Spring, Pico, etc.) and outside of containers (just what was in the Virtual Machine libraries [prior to JDK version 1.5]). There was no DI available for free (unless you wrote it yourself). This is true still today in a number of environments.<br> <br> If I go directly the posted definition of anti-pattern, which most can agree with, I can put a number of patterns that are not always anti-patterns in this category. I tend to emphasize proper pattern usage. Patterns are just tools that can be correctly or incorrectly. I have seen design-by-contract (interface pattern) used in a manner that fits the anti-pattern definition but I don't consider it an anti-pattern. I consider the use of pattern poor or inappropriate for that situation.<br> <br> I consider SOA != Web services + UDDI + etc,. Web services and its relative technology stack can be used to implement an SOA. Therefore, from my perspective it is relative to this discussion and not orthogonal. In fact we had a discussion relative to these to these concepts last week in one of my current projects. The goal of that discussion was the abstraction of service implementation + location in a high-performance low-latency SOA design.<br> <br> I am not a proponent of Service Locator or any other pattern. I us it and others with/without DI where appropriate. The perceived / actual negative impact of a pattern usage in one scenario does not apply to all scenarios. There are definitely patterns where one can get a majority agreement on them being anti-patterns. I just don't think Service Locator is one. However, the pursuit of try to qualify the validity of a pattern's usage or existence does generally lead to other solutions or ideas. Which I am a proponent of.<br> <br> Cheers,<br> <br> Iran<br> </div> <div class="comment-date">2010-08-01 00:42 UTC</div> </div> <div class="comment" id="2801bf3f368441cea3dfd1c31d94a751"> <div class="comment-author">Al <a href="#2801bf3f368441cea3dfd1c31d94a751">#</a></div> <div class="comment-content"> You have missed totally the point with service locator pattern. Probably you have done this intentionally, just to gain attention to try to sell a bunch of books. All you complaints can be equally applied to most DI implementations. In fact, many DI frameworks (Spring and Seam came into my mind), use a service locator internally and even let you to use it directly.<br> <br> Service locator and DI, have essentially the same main downsides, and the only real difference is whether you prefer to retrieve the component from your code of to have it injected externally. In fact, and at least in the Java implementations i have worked with, you get less compiler support with DI variants, and also have a harder time to debug because of faulty injections. I am still amazed to see people arguing about how you get a dependency with a particular service locator implementation, when you a) can have a light wrapper to decouple from it and b) you get the same dependency with your particular DI implementation, you still have to tell de container what, how and when you need the injection, and thats a change too.<br> <br> With you service locator example, you have only proved that it is also possible to write a crappy service locator implementation. And your moans are about that, not the pattern.</div> <div class="comment-date">2010-09-10 08:57 UTC</div> </div> <div class="comment" id="b2fd0a48c7ea4a2fae238baab643140d"> <div class="comment-author">Phil <a href="#b2fd0a48c7ea4a2fae238baab643140d">#</a></div> <div class="comment-content">Sure, Service locator is not the best way to implement kind of IoC. Your argument about hidden dependencies is certainly a good point.<br> <br> Though, how many developers with good will and at least, average knowledge are trying to use IoC just to fail to understand how to set up their root components and bootstrap correctly their dependencies?<br> <br> Now, imagine those who understand well those principles and techniques, trying to explain, guide, support an entire team of developers about those concepts...I guess you see (or have seen) like me those astonished developers faces trying to understand and not mess this magic behind dependency injection.<br> <br> Ok, I'll admit it is not so hard. But still, my point is it is much harder to understand how those dependencies are injected and how not to mess and miss the point than using a simple Service locator instead of a &quot;new&quot; keyword.<br> <br> And as someone else mention before, it is mandatory to use a Service locator with good convention-over-configuration to have fun with it. But it is the same with Dependency injection.<br> <br> I see service locator as a first step for a team into IoC. After the concept and benefits are known by the team, I think it is much easier to turn the ship towards the Dependency injection final destination.<br> <br> Good article and back and forth discussions here<br> Thanks<br> Phil</div> <div class="comment-date">2011-05-09 16:01 UTC</div> </div> <div class="comment" id="446904dd2439401a880ab14ebc789b62"> <div class="comment-author">Alex Brina <a href="#446904dd2439401a880ab14ebc789b62">#</a></div> <div class="comment-content">What every article I read fail to show is how to use DI without some sort of container, or factory or locator or whatever is the name of the &quot;thing&quot; responsible for objects instantiation. It's no big deal moving all &quot;new&quot; keywords out of my &quot;OrderProcessor&quot; class, preparing them for injection via constructor or setter methods, but what about the calling object? Is it now responsible for &quot;injecting&quot; the dependencies? Should it instantiate them and pass to &quot;OrderProcessor&quot;? where is the value of this thing I may call &quot;Inversion of Dependency&quot;? Please show me where dependencies come from? Should the calling object call the DIC to instantiate &quot;OrderProcessor&quot; fulfilling its dependencies based on some configuration? No, because it is masking its dependency on &quot;OrderProcessor&quot;, your calling object should have received the &quot;OrderProcessor&quot; beforehand. Allright, but now it's the calling object of the object who's responsible for passing all this chain of dependencies. I just don't get it, and I would love to understand this DI without &quot;Container&quot; thing!</div> <div class="comment-date">2011-05-28 01:03 UTC</div> </div> <div class="comment" id="59c69dbe0d1742b89665c332ccbb1bf4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#59c69dbe0d1742b89665c332ccbb1bf4">#</a></div> <div class="comment-content">With DI, all &quot;new&quot; keywords are being pushed to the entry point of the application. This is what is called the <i>Composition Root</i>. In <a href="/2010/04/07/DependencyInjectionisLooseCoupling">this example</a> I use Poor Man's DI towards the end of the post.<br> <br> FWIW the first nine chapters of <a href="http://amzn.to/12p90MG">my book</a> discuss DI mainly in the context of Poor Man's DI (that is, without containers).</div> <div class="comment-date">2011-05-28 21:20 UTC</div> </div> <div class="comment" id="c7c709cf10bf49cb94567771e1a5ac06"> <div class="comment-author">Alex Brina <a href="#c7c709cf10bf49cb94567771e1a5ac06">#</a></div> <div class="comment-content">Hello Mark,<br> <br> thanks for you answer, I was subconsciously avoiding this concept of Composition Root, although I didn't know why. But today I came to some code that shed some light on it, and the driving force against using the entry point for dependecies instantiation was my understanding of &quot;encapsulation&quot;. As I see it, some dependencies should not be known outside the object. The code I mentioned, for instance, is a class named &quot;Syncronizer&quot; which defines a sequency of &quot;Steps&quot; (each step is an object), the sequence and steps are defined internally and it is the Syncronizer's sole responsibility, chosen steps are &quot;instantiated&quot; internally (say 10 of 30 available), The entry point isn't aware of which Steps would be needed. AS I see, there is something in DI that goes against &quot;encapsulation&quot;. Would love to know your thoughts about it.<br> <br> Alex Brina<br> </div> <div class="comment-date">2011-06-01 15:35 UTC</div> </div> <div class="comment" id="1a7cf81941004f739cbd7881b711501f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1a7cf81941004f739cbd7881b711501f">#</a></div> <div class="comment-content">DI doesn't go against encapsulation, which is one of <a href="/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">the most misunderstood concepts in OOP</a>.</div> <div class="comment-date">2011-06-01 18:49 UTC</div> </div> <div class="comment" id="92bdb37ed56543e29862c6ffaafd19ee"> <div class="comment-author">Patrick Sears <a href="#92bdb37ed56543e29862c6ffaafd19ee">#</a></div> <div class="comment-content">Mark - I love this post. I'm diving into IoC and planning to use the Unity framework and one of the first and immediate problems I ran into was the need to resolve types deep within the object hierarchy. So of course, first thing I did was search for common ways to do this.<br> <br> 1. Passing around the container was obviously the wrong approach<br> 2. A global, static container also &quot;felt&quot; like the wrong approach... the business code shouldn't need to know that much about the application domain.<br> 3. A very common solution around the web is the ServiceLocator pattern, but - the reason I continued looking is that pattern just &quot;felt&quot; wrong to me in an intuitive sense - it's essentially no better than (2) and as you say, the container begins to invade the business logic. Now, the entire architecture has to know about the container - which completely defeats the purpose of DI and IoC.<br> <br> So then I stumbled upon your post and it summed up EXACTLY why those approaches &quot;felt&quot; wrong - and also helped me re-orient my thinking with regard to where to create the containers. <br> <br> I don't NEED a single container for the whole application, I only need them within the scope where they're required. If I have a type that is only ever instantiated 4 levels deep in the object hierarchy, it makes no difference whether I register the type at the root of the application or scoped to the code where that type is relevant. Thus, in order to prevent the container from invading the application in the ways you describe, it makes perfect sense to create the container at a lower level (what you call the Composition Root?). <br> <br> Really like your blog. I appreciate the dedication to design principles that seem so easily thrown away when pragmatism requires.</div> <div class="comment-date">2011-07-11 08:14 UTC</div> </div> <div class="comment" id="386b19c27fed41ebad1092910078f0a1"> <div class="comment-author">Danil <a href="#386b19c27fed41ebad1092910078f0a1">#</a></div> <div class="comment-content">I constantly get this when attempting to post a comment:<br> <br> An error has been encountered while processing the page. We have logged the error condition and are working to correct the problem. We apologize for any inconvenience.<br> <br> Page rendered at Wednesday, July 13, 2011 8:02:11 AM (Romance Daylight Time, UTC+02:00)</div> <div class="comment-date">2011-07-13 08:03 UTC</div> </div> <div class="comment" id="619d2a3c06ad4e798ab44db3865a76fa"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#619d2a3c06ad4e798ab44db3865a76fa">#</a></div> <div class="comment-content">Yes, the software hosting this blog is crap, and migration is on my to-do list. The error usually has something to do with the use of angle brackets.<br> <br> Sorry about the inconvenience.</div> <div class="comment-date">2011-07-13 09:44 UTC</div> </div> <div class="comment" id="19b97169f4734afe860d0ea3de4a3a3e"> <div class="comment-author"><a href="http://garymcleanhall.wordpress.com/">Gary McLean Hall</a> <a href="#19b97169f4734afe860d0ea3de4a3a3e">#</a></div> <div class="comment-content">Hi Mark, <br> <br> Apologies for the necromancy, but I googled for 'Service Locator anti pattern' and this post is one of the top-five hits.<br> <br> I totally agree that Service Locator is an anti-pattern. It's the use of the static class/property that makes it so insidious. It's an example of a Skyhook (http://garymcleanhall.wordpress.com/2011/07/24/skyhooks-vs-cranes/) as opposed to DI, which is a Crane. <br> <br> Service Locator seems to be preferred because the alternative is sometimes to write a constructor to inject half a dozen (or more) dependencies, ie: it smells like the class has too many responsibilities. </div> <div class="comment-date">2011-10-05 00:20 UTC</div> </div> <div class="comment" id="3fdb968bcafd40879f7d584542773b05"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3fdb968bcafd40879f7d584542773b05">#</a></div> <div class="comment-content">Agreed. The important thing is to realize that <a href="/2010/01/20/RebuttalConstructorover-injectionanti-pattern">the smell comes not from Constructor Injection, but from violating the Single Responsibility Principle</a>.</div> <div class="comment-date">2011-10-05 09:39 UTC</div> </div> <div class="comment" id="ae0228e67fcc498086f602ba73ad22c6"> <div class="comment-author">Mohammed <a href="#ae0228e67fcc498086f602ba73ad22c6">#</a></div> <div class="comment-content">A google Tech Talk that addressed pretty much the same issue in 2008 can be seen here:<br> http://googletesting.blogspot.com/2008/11/clean-code-talks-dependency-injection.html<br> <br> </div> <div class="comment-date">2011-10-08 16:19 UTC</div> </div> <div class="comment" id="f19df2a0aa514f7f8a9071ccd6722ab8"> <div class="comment-author">KT <a href="#f19df2a0aa514f7f8a9071ccd6722ab8">#</a></div> <div class="comment-content">I actually don't see a problem with the user of the service locator. In the case of both IOC and the service locator, you have the same problems. The only question is how much indirection you want shown the client of the service.<br> <br> API Usage Issue:<br> In the case of both IOC and Service locator, there is an independent class that is responsible for making sure that the appopriate service (IOrderValidator or IOrderShipper) is found by the client. The only difference is whether the client automatically receives the information or if the client makes an explicit call to a 3rd party class (Locator) to find it. It is more a semantic argument than anything else. <br> <br> If the configuration of the IOC is done incorrectly, you will also receive an exception. The problem is with the management of the configuration as opposed to the service locator itself.<br> <br> Maintenance Issues:<br> Same problem as above. This is a problem with configuration of the service as opposed to the way in which IOC or service locator is used.<br> <br> With both service locator and IOC, someone is going to have to understand all the dependencies in order to configure the services correctly and ensure that the correct service is returned when called upon. This configuration is part of the setup of the service contract. Whether or not this configuration is done through an IOC container or through a service locator seems immaterial.</div> <div class="comment-date">2011-10-17 19:00 UTC</div> </div> <div class="comment" id="3c6022aac8bc48ffb207ef87306a6de3"> <div class="comment-author">Vlad <a href="#3c6022aac8bc48ffb207ef87306a6de3">#</a></div> <div class="comment-content">=== Service Locator is NOT an anti-pattern ===<br> <br> Sorry, I cannot agree with author’s statements from practical standpoint. If you discuss pure theory and your team has unlimited resources for your projects - you can use all kind of nice patterns that require more work and give some advantage.<br> <br> Let's compare advantages and disadvantages and not just talk about pros on one side.<br> <br> I talk from practical large scale software Dev experience when your product is not one-man show but you have mature product with a lot of components, several teams, and engineers coming and leaving. Is it what we all try to get to? If you are talking about one-man show project or small-highly-skilled team project and you work on v1 or v1.1 of your product - sure you can use any pattern and dependency injection through constructor parameters would probably work for you.<br> <br> ===========================<br> <br> == Service Locator advantages ==<br> <br> 1. Much smaller production code. No need to declare variables, parameters to pass dependencies, properties for stateful objects, etc.<br> <br> Every line of code/symbol has associated Dev/QA/support cost. Smaller code is easier to understand and update.<br> <br> 2. Much smaller unit test code. No need to instantiate and pass dependencies - you can set all common dependencies in TestSetup and reset them in TestCleanup. Again, every line of code/symbol has associated cost.<br> <br> 3. Much easier to introduce dependencies. Yes, this is an advantage, because you need to write much less code for this if you use Service Locator. And again every line of code....<br> <br> Imagine you need to add SQL performance logger deep in the code and of course you want all your DB access methods use the same logger instance no matter how you get there from business logic layer. You have to change all stack of callers if you do not use Service Locator.<br> <br> And if you have to change any caller because of your method change -this can be a huge problem in mature product. There will be no way you can justify expenses for a new release of the calling component to you management.<br> <br> == Service Locator disadvantages ==<br> <br> 1. Introduced dependencies are not captured at compile time.<br> <br> How to fix this? The answer is - test your product (with automatic integration or manual tests). But you have to test only changed functionality to make sure that feature that uses new introduced dependency works correctly. You need this anyway and this is already in the plan/budget for your project/change anyway.<br> <br> Did I miss anything?<br> <br> ===========================<br> <br> We’ve tried to use dependency injection and switch to Service Locator turned out to be such a huge relief <br> that we would never even consider switching back.<br> <br> == My conclusion ==<br> <br> If you are working on long-term project and you have to implement cost-efficient solution, maintainable by the team with various skill levels - Service Locator is significant cost saver for you with small to none down sides.<br> <br> If quality is your first priority and budget/time-to-market is not an issue and you have skilled team - do not use Service Locator and rely as much as possible on compile time checks.<br> </div> <div class="comment-date">2011-10-28 09:47 UTC</div> </div> <div class="comment" id="76b94b9383864faab15452f1b5a369df"> <div class="comment-author"><a href="http://arialdomartini.wordpress.com">Arialdo Martini</a> <a href="#76b94b9383864faab15452f1b5a369df">#</a></div> <div class="comment-content">I couldn't agree more.<br> <br> We could say the container itself should not be a dependency, and hance should not be passed around, as stated in this StackOverflow question.<br> http://stackoverflow.com/questions/4806918/should-i-be-passing-a-unity-container-in-to-my-dependencies<br> <br> To me, Service Locator is the exact opposite of Inversion of Control.<br> <br> http://stackoverflow.com/questions/2386487/is-it-better-to-create-a-singleton-to-access-unity-container-or-pass-it-through is another great StackOverflow question about this topic (no surpsise the correct answer is by Mark Seemann himself.</div> <div class="comment-date">2011-11-24 13:59 UTC</div> </div> <div class="comment" id="a4090e0506eb485daaae5890e632be0d"> <div class="comment-author">Barley N. Hopps <a href="#a4090e0506eb485daaae5890e632be0d">#</a></div> <div class="comment-content">The problem with your argument is that you state &quot;I don't agree that Service Locator is ever appropriate&quot;, then provide one contrived example where Service Locator is implemented poorly. All you have proven is Service Locator might not be the best pattern to solve this problem (although you don't prove that either by defining a better solution). It seems that you started with the conclusion and derived the premise.</div> <div class="comment-date">2011-12-15 22:02 UTC</div> </div> <div class="comment" id="2e0eb798b48f46af909269932505b8df"> <div class="comment-author"><a href="http://www.marisic.net">Chris Marisic</a> <a href="#2e0eb798b48f46af909269932505b8df">#</a></div> <div class="comment-content">I vehemently disagree with your premise that service locator itself is an anti-pattern. The service locator is an invaluable pattern when it is needed, and it fulfills roles that there are no other solutions to, other than poor implementations of the service locator pattern.<br> <br> Do people apply it in less then optimal scenarios? Absolutely. Does that make a pattern, an anti-pattern? Absolutely not.</div> <div class="comment-date">2012-01-31 13:54 UTC</div> </div> <div class="comment" id="08f40ad8d4be4d60bfe019ad7626bcd8"> <div class="comment-author">Zach <a href="#08f40ad8d4be4d60bfe019ad7626bcd8">#</a></div> <div class="comment-content">Hi!<br> (I've had to remove angle brackets so I hope the code still makes sense.. where you see a T think generics T with angle brackets:))<br> <br> All you are doing with your proposal is turning the abstract factory into an &quot;anti-pattern&quot;. Why go through the creation of the factory only to create a new level of indirection just to (essentiall)resolve 1 object that needs resolution in 1 one. I know you can reuse this everywhere. Why not create an object registry interface eg IMyObjectRegistry <br> eg: <br> public interface IMyObjectRegistry { T get_for T ();} <br> <br> public class MyObjectRegistry : IMyObjectRegistry {<br> public T get_for T (){<br> return Resolver.Get T ();<br> } <br> }<br> <br> <br> <br> IMyObjectRegistry get passed around. You could also further constrain these to only work with certain types. <br> You register the &quot;Registry&quot; with the DI container. Now all objects have a way of finding dependencies with the ServiceLocator. Its still very testable you can Mock/Stub/Fake IMyObjectRegistry. You don't even need a DI implementation in your tests.<br> <br> <br> </div> <div class="comment-date">2012-02-06 10:26 UTC</div> </div> <div class="comment" id="63e435d425a64f6a8dd0148a1692a279"> <div class="comment-author">Adrian <a href="#63e435d425a64f6a8dd0148a1692a279">#</a></div> <div class="comment-content">I'm using the service locator pattern in my Zend Framework (ZF) based PHP application. The upcoming ZF2 seems to promote DI and I'm thinking about refactoring with constructor DI and a DI container. However, there's a proposal for DI-enabled service locators (http://framework.zend.com/wiki/display/ZFDEV2/Proposal+for+ServiceLocator+and+DependencyInjector). Doesn't this completely go against the spirit of IoC/DI? Would you consider a DI-enabled service locator an anti-pattern, too?</div> <div class="comment-date">2012-02-20 16:23 UTC</div> </div> <div class="comment" id="8e17aad3bdaf4d65bc70a0472ee782a7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8e17aad3bdaf4d65bc70a0472ee782a7">#</a></div> <div class="comment-content">In my opinion, there's no such thing as a &quot;DI-enabled Service Locator&quot;. <a href="http://www.infoq.com/articles/Succeeding-Dependency-Injection">Service Locator and DI are two mutually exclusive concepts</a>.</div> <div class="comment-date">2012-02-22 15:56 UTC</div> </div> <div class="comment" id="9b5c78edf8ac4b8cbb011faeeccaf2a9"> <div class="comment-author">Grasp <a href="#9b5c78edf8ac4b8cbb011faeeccaf2a9">#</a></div> <div class="comment-content">Did not read the everyone's comments, but dont agree with the post.<br> <br> It is the Service Locator's responsibilty to RESOLVE the interface implementation, if the interface is not registered, instead of doing NOTHING which bubble's KeyNotFoundException up, it should raise its own exception to notify consumer that the interface has NOT been registered yet.</div> <div class="comment-date">2012-03-07 23:38 UTC</div> </div> <div class="comment" id="5f03fcf649dc4f949b835bff7d8901dc"> <div class="comment-author">Justin <a href="#5f03fcf649dc4f949b835bff7d8901dc">#</a></div> <div class="comment-content">@Grasp <br> <br> I think you fail to understand the argument against ServiceLocator. No one is debating what ServiceLocator's responiblities are.<br> This problem with ServiceLocator is the problems/headaches it introduces. The biggest one being that it hides a type's dependencies. <br> It's difficult to write unit tests for a type when it isn't clear from the type's contract what type of dependencies it has. This can make writing unit tests extremely painful and discouraging. Secondly, it make detecting breaking changes a PITA. That fact that your unit test fails because some hidden dependency hasn't been resolved is a side-effect of ServiceLocator.<br> <br> Unfortunately, when some developers hear the word 'pattern' they automatically assume it's good/best practice. This is hardly the case with ServiceLocator. A pattern that makes your life harder or encourages bad design practices is an anti-pattern.</div> <div class="comment-date">2012-03-12 14:03 UTC</div> </div> <div class="comment" id="e4b5ff77bd0b41e19ba6bd1f674734ec"> <div class="comment-author">Hannes Kochni&#223; <a href="#e4b5ff77bd0b41e19ba6bd1f674734ec">#</a></div> <div class="comment-content">I know the arguments so far, but there is _one_ issue that comes up constantly, and even some TDD veterans agree that SL is ok: a Logger. I'm not always in &quot;register the service locator with the framework&quot; MVC wonderland, and AOP doesn't cover it, I don't want to log only on exceptions or method boundaries. Assuming I understand the capabilities of AOP frameworks like PostSharp correctly (I know IL weaving / dynamic proxy is a big difference, but anyway).<br> <br> So your normal answer is &quot;abstract factory&quot;, but that still would mean an additional parameter. Should I inject an ILogger or ILoggerAbstractFactory into EVERY class? That.. would be insane.<br> <br> So what's your solution there?</div> <div class="comment-date">2012-05-10 22:05 UTC</div> </div> <div class="comment" id="966048d0404a4ae59a308de140a51f43"> <div class="comment-author">Hannes Kochni&#223; <a href="#966048d0404a4ae59a308de140a51f43">#</a></div> <div class="comment-content">just to change the tone of my post: &quot;insane&quot; is too harsh. :)<br> <br> As you well argue, &quot;expressing intention&quot; in design is amongst the main points of DI. But ILogger is an cross-cutting concern, showing intention to log in a class is 0% Info to anyone (as is tracing). I know these are the most used examples for AOP, but I repeat myself: AOP doesn't solve logging additional/debug info, just exception logging. Or I'm misunderstanding AOP capabilities.</div> <div class="comment-date">2012-05-10 22:09 UTC</div> </div> <div class="comment" id="a5d5868ba9fc406fbda16fa5dce54ec8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a5d5868ba9fc406fbda16fa5dce54ec8">#</a></div> <div class="comment-content">Yes, logging is a cross-cutting concern, which is why I'd strongly recommend not injecting it all. <a href="http://stackoverflow.com/questions/7905110/logging-aspect-oriented-programming-and-dependency-injection-trying-to-make/7906547#7906547">AOP is much preferred</a>. It addresses fine-grained instrumentation too, with the caveat that (as you imply) you can only log at method boundaries. However, before you dismiss this as a solution, consider this: why is that not enough? If you need to log from within a single method, it most like means that this method is too big.</div> <div class="comment-date">2012-05-11 03:55 UTC</div> </div> <div class="comment" id="9ce1a65482bf4676919bfb617afdbb7f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9ce1a65482bf4676919bfb617afdbb7f">#</a></div> <div class="comment-content">BTW, even if you're not convinced by the arguments above, or if you have legacy code with long methods, Service Locator is not a good solution. If you don't like to inject an Abstract Factory, you could instead use an Ambient Context. That's not my first choice, but still way better than a Service Locator.</div> <div class="comment-date">2012-05-11 04:04 UTC</div> </div> <div class="comment" id="17d87a45717b480fbfc5a98307c5f29c"> <div class="comment-author">Dima <a href="#17d87a45717b480fbfc5a98307c5f29c">#</a></div> <div class="comment-content">Hi Mark! I read your post about Ambient Context, but it doesn't look like full-fledged pattern. It has two problems: <br> 1) it doesn't know its scope (app domain, thread, call context, or custom context)<br> 2) it doesn't know when to dispose itself. Both problems come from the fact that it's the root application that should care about those problems. <br> Again it comes down to Service Locator.</div> <div class="comment-date">2012-05-14 18:24 UTC</div> </div> <div class="comment" id="7a9e7d962e924950b129514fb8ddebb1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7a9e7d962e924950b129514fb8ddebb1">#</a></div> <div class="comment-content">Which post?<br> <br> Did you read the Ambient Context pattern description in <a href="http://amzn.to/12p90MG">my book</a>?<br> <br> Most loggers already come with an Ambient Context implementation. Logger.Log(&quot;foo&quot;) is, essentially, an Ambient Context. It's true that it's not particularly clear when an Ambient Context should dispose itself, but then again: why would you need to dispose of something which is a cross-cutting concern? I'd prefer such a thing to stick around (for performance reasons).<br> <br> Still, if you don't like Ambient Context (I don't), you can always use an Abstract Factory to get your ILogger. Service Locator is not required.</div> <div class="comment-date">2012-05-14 18:33 UTC</div> </div> <div class="comment" id="5b7bb9f0c9614728bb0353f29c6e4d79"> <div class="comment-author">Dima <a href="#5b7bb9f0c9614728bb0353f29c6e4d79">#</a></div> <div class="comment-content">I read about them in your book and in your old msdn blog.<br> Dispose problems come with asp.net doing threads reuse. Though CallContext can be used as workaround, it feels a bit hacky.<br> It's easier with loggers as they usually can be just static. But if I need something to be bound to a HttpContext or a session?<br> <br> Abstract factory doesn't solve the problem this all started with: extra reference in a constructor. IoC looks very nice when my classes have parameters they really need to do their work. But secondary parameters like ILogger or ILoggerFactory don't make much sense. If I went this way I would prefer something like IInfrastructure and let it have all ILogger properties and other cross cutting stuff.<br> <br> I still don't feel satisfied with all solutions. I don't like that SL hides contracts, but I also don't like that DI makes them over complicated.</div> <div class="comment-date">2012-05-14 19:28 UTC</div> </div> <div class="comment" id="436f2f2902c7432cbf4a97d5e32eb64f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#436f2f2902c7432cbf4a97d5e32eb64f">#</a></div> <div class="comment-content">Oh, that blog post... I'd completely forgotten about that one.<br> <br> I agree that injecting an ILoggerFactory could be considered as parameter pollution of the constructor, but remember that (IMO) we are only talking about this as a solution when dealing with legacy code. Properly factored code should have methods that are short enough that a Decorator or Around Advice should be more than enough for logging and instrumentation purposes.<br> <br> So once again: Service Locator is never a solution for anything. The solution is to refactor the code so that you don't need to inject an ILogger at all.</div> <div class="comment-date">2012-05-14 20:03 UTC</div> </div> <div class="comment" id="a556cacdbbae42228acd9b92f0c5f45d"> <div class="comment-author">Tarriq Ferrose Khan <a href="#a556cacdbbae42228acd9b92f0c5f45d">#</a></div> <div class="comment-content">Dear Mark, <br> <br> am desparately in need of a help, I tried to post my complete content (with code) but it was always throwing an error, Have sent you an email, request you to please review and share your thoughts.<br> <br> Thanks,<br> Tarriq </div> <div class="comment-date">2012-06-30 01:31 UTC</div> </div> <div class="comment" id="f989242940b848a78536263750464444"> <div class="comment-author">Vaseem <a href="#f989242940b848a78536263750464444">#</a></div> <div class="comment-content">Did you mention anything about how to do DI over web forms in your book ?</div> <div class="comment-date">2012-07-28 10:13 UTC</div> </div> <div class="comment" id="9deea1953de64e3693ec462d4fa4b0b8"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#9deea1953de64e3693ec462d4fa4b0b8">#</a></div> <div class="comment-content">That's section 7.5, but to be fair, it's only 6 pages, so it's not a major part of the book in any way.</div> <div class="comment-date">2012-07-28 10:24 UTC</div> </div> <div class="comment" id="704ce60a92f24452b98a363cce560040"> <div class="comment-author">Michael Bui <a href="#704ce60a92f24452b98a363cce560040">#</a></div> <div class="comment-content">It appears that this is more an argument about developing using Interfaces versus using concrete implementation. It is clear if you change an interface you will have dependencies that you will affect. In general, it can be assumed that breaking interfaces (changing) them is not a good practice. I think you can resolve your issues by not breaking the interfaces but defined new ones and having the implementation use the new version. This is some of the core concepts underlying component programming. It isnt an issue about the service locator, it seeks to achieve to solve its own problem. The problem here is about how your deal with changes to behavior or structure on a already published contract. In using service locator, knowning your implemenation dependency is a must. Try solution wide refactoring tools.</div> <div class="comment-date">2012-10-11 17:49 UTC</div> </div> <div class="comment" id="6f424b9f1e294548b75b5baa907de3d2"> <div class="comment-author">Michael <a href="#6f424b9f1e294548b75b5baa907de3d2">#</a></div> <div class="comment-content">So, we've got code 1:<br> <br> public class MyService<br> {<br> public MyService (IRepository1 repository, IRepository2 repositor2)<br> ...<br> public void DoStuff ()<br> {<br> repository.Find...<br> repository2.QueryAllWithCriteria...<br> ...<br> }<br> }<br> <br> And code 2:<br> <br> public class MyService<br> {<br> public void DoStuff ()<br> {<br> // used square brakets instead of angle<br> // cause of comment posting error<br> ServiceLocator.Resolve[IRepository1].Find...<br> ServiceLocator.Resolve[IRepository2].QueryAllWithCriteria...<br> ...<br> }<br> }<br> <br> Since when injecting dependencies (code 1) is helping understand better on on which code MyService really depends compared to code 2? I mean real world repositories usually have a lot of different methods. Just because you pass it around does not help user understand which method inside that repository the service use. For example, if I'm going to write unit test it won't help my at all - unless I got some really simple/generic repository I can't just mock all repository methods just to test my service. So I still end up looking into the code or catching first run test failures to see what I need to mock inside that repository. And no - SRP has nothing to do with it, unless you plan to build separate repository for every single query/operation.<br> <br> Why then I should clutter my client code with pointless stuff?<br> <br> For example, I develop a library for internal usage between many projects. It exports some services, command and queries. It does not work with some external repositories - so why should I make all users to pass my repositories to my methods? I could just make them call one single bootstrapper static method during app startup which will register all my necessary components for my library to work.<br> <br> Service locator is ideal thing for allowing loose coupling of internal components (for ability to unit test them).</div> <div class="comment-date">2012-11-26 09:54 UTC</div> </div> <div class="comment" id="1eba6fe334b34cecb851b0aa89a45798"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1eba6fe334b34cecb851b0aa89a45798">#</a></div> <div class="comment-content">It's no so much the SRP as it's the ISP which is in play here. I'd strongly recommend building a <a href="http://martinfowler.com/bliki/RoleInterface.html">Role Interface</a> for each query or command instead of the sort of <a href="http://martinfowler.com/bliki/HeaderInterface.html">Header Interface</a> you seem to prefer.</div> <div class="comment-date">2012-11-26 10:01 UTC</div> </div> <div class="comment" id="91c948d58f3d4d9ab71ed88c82be9c64"> <div class="comment-author">Michael <a href="#91c948d58f3d4d9ab71ed88c82be9c64">#</a></div> <div class="comment-content">Hm.. Isn't that means that I'll end up with separate role interface for each method inside repository?<br> <br> <br> And the second question - why do client (consumer of my library) have to deal with my internals? I mean why does he had to provide my service my repositories?<br> <br> I think the whole misconception and negativism toward service locator comes from trying to use it everywhere and thus leaking it outside of your domain. Look at those many good libraries - they don't ask us to provide instances of their internals. In order to use NHibernate you have to configure it once and not feeding every session creation bunch of interface instances which it will use. NHibernate will use configuration to get instance of internal object when he needs to - that's basically the service locator with different name.<br> <br> I think that real anti-patterns are:<br> 1. Usage of constructor or property injection for external interfaces of your domain, that is expecting user to provide instances of interfaces that your service need in order to operate. Though it's good to provide a way for user to override some part implementaion (like NHibernate, MVC does).<br> <br> 2. Usage of service locator for external resources: if your service mean to operate on some external user-provided source you should never expect it to be provided via service locator - use constructor/properties/parameters injection instead.<br> </div> <div class="comment-date">2012-11-26 10:50 UTC</div> </div> <div class="comment" id="c6eca9aef44642a19b06422a84f779db"> <div class="comment-author">Mansoor Omrani <a href="#c6eca9aef44642a19b06422a84f779db">#</a></div> <div class="comment-content">I'm agree with the problems you mentioned for the Service Locator pattern. But I think this pattern can be a last resort in scenarios where you have a deployed component on which many clients are depending. In such a case modifying constructor or methods signatures will break those dependent apps, whereas service locator provides -at least- a little inversion of control. But in redesign scenarios or when are designing from the ground up I agree that service locator is a bad choice.<br> <br> I also suggest a way to amend service locator pattern to overcome dependency opacity in the clients of a locator. I apologize for the code might be a little long. But I'm sure you'll instantly get the idea behind it with in a glance.<br> <br> best regards<br> <br> p.s.: I wasn't able to submit the comment because the blog-engine takes angle brackets as xss attack. So I've changed all &quot;lower than&quot; and &quot;greater than&quot; characters into &quot;dollar sign&quot; ($) and &quot;pound sign&quot; (#) respectively. If you perform a replace-all in the code against these characters in reverse direction, the code turns back to normal.<br> <br> public interface IDependency1 { }<br> public interface IDependency2 { }<br> <br> public interface IClient<br> {<br> KeyValuePair$Type, bool#[] GetDependencies();<br> bool IsReady();<br> }<br> public interface IServiceLocator<br> {<br> void Register$T#(Func$T# resolver);<br> T Resolve$T#();<br> bool Contains$T#();<br> }<br> public class ServiceLocator: IServiceLocator<br> {<br> private readonly Dictionary$Type, Func$object## services = new Dictionary$Type, Func$object##();<br> public void Register$T#(Func$T# resolver)<br> {<br> services[typeof(T)] = () =# resolver();<br> }<br> public T Resolve$T#()<br> {<br> return (T)services[typeof(T)]();<br> }<br> public bool Contains$T#()<br> {<br> return services.Keys.Contains(typeof(T));<br> }<br> }<br> public class MyService: IClient<br> {<br> private bool hasIDependency1;<br> private bool hasIDependency2;<br> <br> public MyService() { }<br> public MyService(IServiceLocator servicelocator, bool checkDependencies = false)<br> {<br> hasIDependency1 = servicelocator.Contains$IDependency1#();<br> hasIDependency2 = servicelocator.Contains$IDependency2#();<br> <br> if (checkDependencies)<br> {<br> if (!hasIDependency1)<br> throw new ArgumentException(&quot;ServiceLocator doesn't resolve dependency: IDependency1.&quot;);<br> if (!hasIDependency2)<br> throw new ArgumentException(&quot;ServiceLocator doesn't resolve dependency: IDependency2.&quot;);<br> }<br> }<br> public KeyValuePair$Type, bool#[] GetDependencies()<br> {<br> var result = new KeyValuePair$Type, bool#[]<br> {<br> new KeyValuePair$Type, bool#(typeof(IDependency1),hasIDependency1),<br> new KeyValuePair$Type, bool#(typeof(IDependency2),hasIDependency2)<br> };<br> return result;<br> }<br> public bool IsReady()<br> {<br> return hasIDependency1 &amp;&amp; hasIDependency2;<br> }<br> }<br> public class Test<br> {<br> public static void Main()<br> {<br> var ms = new MyService();<br> <br> Console.WriteLine(&quot;Dependencies of 'MyService':&quot;);<br> Console.WriteLine(&quot; Dependency Resolved&quot;);<br> Console.WriteLine(&quot;---------------------------------&quot;);<br> <br> foreach(var item in ms.GetDependencies())<br> {<br> Console.WriteLine(item.Key + &quot; &quot; + item.Value);<br> }<br> <br> Console.ReadKey();<br> }<br> }<br> /*<br> Dependencies of 'MyService':<br> Dependency Resolved<br> ---------------------------------<br> Dummy.IDependency1 False<br> Dummy.IDependency2 False<br> */</div> <div class="comment-date">2013-01-24 08:19 UTC</div> </div> <div class="comment" id="c14ae5d56ee24dabbc682cc257256ff2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c14ae5d56ee24dabbc682cc257256ff2">#</a></div> <div class="comment-content">Mansoor, what you describe is already covered in the original post under the heading <em>Abstract Service Locator</em>.</div> <div class="comment-date">2013-01-27 10:55 UTC</div> </div> <div class="comment" id="c80ed1730cba43308cf8dfedb509dce0"> <div class="comment-author">Mansoor Omrani <a href="#c80ed1730cba43308cf8dfedb509dce0">#</a></div> <div class="comment-content">I disagree respectfully. My point is not about dependency injection. I suggested to &quot;amend&quot; service locator pattern. In fact based on your explanation in &quot;Abstract Service Locator&quot; which I'm agree with ...<br> <br> [blockquote cite=&quot;Scott&quot;]<br> ...<br> What does this tell us? Nothing much, really. Okay, so OrderProcessor needs a ServiceLocator – that’s a bit more information than before, but it still doesn’t tell us which services are needed.<br> [/blockquote]<br> <br> ... I suggested a solution by which a class can &quot;report&quot; if the given locator &quot;contains&quot; its required dependencies or not. So this way the OrderProcessor can tell us exactly which services are needed.<br> </div> <div class="comment-date">2013-01-28 09:49 UTC</div> </div> <div class="comment" id="614c207878ab4a099dafd08630f21f54"> <div class="comment-author"><a href="http://www.refactorthis.net">Buddy James</a> <a href="#614c207878ab4a099dafd08630f21f54">#</a></div> <div class="comment-content">Mark,<br> <br> I agree with you completely. I've read this article and others like it concerning the Service Locator Anti-pattern. I've also read your book on dependency injection (I read it in a night, great book!). There is one topic that I would love to get your input on and that's the way in which Microsoft's Prism passes the Unity container around to each module for registering it's dependencies. For those who aren't familiar with Prism, it's a library from the Microsoft patterns and practices camp and it facilitates writing loosely coupled applications.<br> In prism your application is made up of independent modules that implement the IModule interface. Modules can be configured fluidly in code, defined in the application configuration file, and even discovered on demand from the file system. The IModule implementation allows for passing the IUnityContainer used throughout the application in each module's constructor to allow the module to register it's dependencies. Though I love the flexibility of the Prism Modules, I can't help but feel dirty about the way that the container is passed around sporadically to each module. I as a developer don't know what the developer of the next module to be initialized may do to the container. To me, this mystery aspect of the way that the container is used is strikingly similar to the problems of the Service locator anti-pattern. I feel that I need an &quot;intimate knowledge&quot; of the other modules if I want to be sure about the container. I'm curious about your thoughts on this subject considering you wrote the book :)<br> Thanks for your time, and keep up the great work!<br> Buddy James </div> <div class="comment-date">2013-01-30 20:10 UTC</div> </div> <div class="comment" id="84a397c551764c0b8c38a1c15dd61c69"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#84a397c551764c0b8c38a1c15dd61c69">#</a></div> <div class="comment-content">Mansoor, in which way does your proposed amendment of Service Locator change the feedback mechanism? While you propose a more explicit API for querying a Service Locator, it doesn't change the fundamental situation. Even without an explicit query mechanism, you could wrap your Service Locator calls in a try/catch block or check for null return values... so even without an explicit query mechanism, you can get the exact same information. Now you have that information at run-time. What will you do with it?<br> <br> One major problem with Service Locator is that it doesn't provide feedback until at run-time. <a href="/2011/04/29/Feedbackmechanismsandtradeoffs">It's much better if we can get feedback already at compile time</a>, and that's possible with Dependency Injection. It 'reports' on its requirements at compile time. It also enables tools (such as DI Containers) to walk up to the type information and start querying about its dependencies.<br> <br> The proposed fix for Service Locator doesn't change any of that. What benefits does it provide?</div> <div class="comment-date">2013-02-04 15:38 UTC</div> </div> <div class="comment" id="0715d69a3508427fb359999a36739148"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0715d69a3508427fb359999a36739148">#</a></div> <div class="comment-content">Buddy, I think my opinion about passing the container around is sufficiently covered in the subsection of this post entitled <em>Variation: Abstract Service Locator</em>. That's exactly what it describes :)</div> <div class="comment-date">2013-02-04 15:43 UTC</div> </div> <div class="comment" id="b0d35b8017b442709f7bf28c46266b7a"> <div class="comment-author">Bruce Pierson <a href="#b0d35b8017b442709f7bf28c46266b7a">#</a></div> <div class="comment-content">Interesting. I've changed my service locator to use &quot;reasonable defaults&quot;, like this:<br> <br> --In IIocAdapter:<br> <br> T TryGet[T]( T defaultInstance, bool register = true );<br> <br> --In AutofacIoc implementation:<br> <br> public T TryGet[T]( T defaultInstance, bool register = true )<br> {<br> var instance = TryGet[T]();<br> if( null == instance )<br> {<br> instance = defaultInstance;<br> if( null != instance &amp;&amp; register )<br> {<br> ContainerBuilder builder = new ContainerBuilder();<br> builder.RegisterType( instance.GetType() ).As( typeof( T ) );<br> builder.Update( m_Service );<br> }<br> }<br> <br> return instance;<br> }<br> <br> --And in the static Ioc &quot;Service locator&quot;:<br> <br> public static T TryGet[T]( T defaultInstance, bool register = true )<br> {<br> if( IsInitialized() )<br> return Adapter.TryGet[T]( defaultInstance, register );<br> <br> return defaultInstance;<br> }<br> <br> --From code:<br> <br> public virtual IRuleContainer GetRuleContainer()<br> {<br> if( null == m_RuleContainer )<br> {<br> lock( LOCK )<br> {<br> if( null == m_RuleContainer )<br> m_RuleContainer = Ioc.TryGet[IRuleContainer]( new DefaultRuleContainer() );<br> }<br> }<br> <br> return m_RuleContainer;<br> }<br> <br> Very flexible. I can use IoC or inheritance.<br> <br> I love it. It lets me create good default behavior that can be very simply replaced. It also makes the principle of &quot;prefer containment over inheritance&quot; much easier to achieve. I guess sometimes we need to use &quot;anti-patterns&quot; to enhance the implementation of other patterns.</div> <div class="comment-date">2013-02-07 20:03 UTC</div> </div> <div class="comment" id="599ced4b3c27450982e177a37a924cae"> <div class="comment-author">Mansoor Omrani <a href="#599ced4b3c27450982e177a37a924cae">#</a></div> <div class="comment-content">I got the point and I am convinced. :)<br> <br> So I conclude it this way:<br> <br> Inability to report on compile-time is one other deficiency to Service Locator pattern among its other deficiencies.<br> <br> While my suggestion may ease finding the requirements of the holder class a little, easier than a try-and-error approach such as using a try/catch, but the need to &quot;execute&quot; the app and obtaining the information at runtime is never removable.<br> <br> Perhaps the only benefit for adding query API to a service locator is in those scenarios where you are extending an app whose structure, libraries and API is already fixed (perhaps worked by a previous team) and you can't modify those classes and method signatures.<br> <br> Thank you for time on reviewing my code.</div> <div class="comment-date">2013-02-08 04:29 UTC</div> </div> <div class="comment" id="af0a8e888d59440f9160110bc2ad1841"> <div class="comment-author"><a href="http://www.refactorthis.net">Buddy James</a> <a href="#af0a8e888d59440f9160110bc2ad1841">#</a></div> <div class="comment-content">@Mark I've read the entire article, and I don't think that the subsection that you specify does cover my question. First and foremost, I'm 100% with why you would not want to use the service locator anti-pattern. I agree that it's bad and shouldn't be used. My question is regarding Prism. In the Prism library, the Unity container is passed around as modules initialize.. So that way each module can be decoupled from the other modules, and they can each register their required dependencies. This process *most of the time* occurs at the composition root and you proceed to use Resolve only once to resolve your start up Window (for WPF) or whatever you are starting with. So at the point of resolve.. you call it once.. you call it at the composition root.. and the one call to resolve should resolve your entire &quot;tree of dependencies&quot; or graph so to speak.. This part is correct.. the part that is incorrect.. or rather, I'm fickled about, is the fact that you are passing 1 container around to multiple modules that have no knowledge of what the others may.. or may not have registered.. So with Unity.. this makes it possible for your to register a dependency, only to have the dependency overwritten by a module that someone else wrote that you aren't aware of. This may not happen frequently, however, it's still possible and that's where I have a problem :) . I'm wondering what would be some better alternatives to allow multiple modules developed by different developers to register dependencies and to then resolve all dependencies at the app start up or &quot;composition root&quot;, without passing one unity container around. You could have a container for each module.. however.. you wouldn't have a single resolve that &quot;unravels&quot; your entire object graph. Any ideas anyone?? Thanks for the response!</div> <div class="comment-date">2013-02-15 08:40 UTC</div> </div> <div class="comment" id="1139790b4718456eb660e44b9a51b739"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1139790b4718456eb660e44b9a51b739">#</a></div> <div class="comment-content">Buddy, thanks for clarifying. While I've never looked at Prism, I think I understand how it works from what you've described. It's not that uncommon a 'pattern', but I can't particularly endorse it, as I think that it tightly couples a framework to a particular container (in this case Unity), and in any case there are better, less invasive alternatives.<br> <br> The way MEF works comes to mind, but you can do the same as MEF does with convention-based wiring with any DI Container worth its salt. However, I think I need to do a write-up of this one day...<br> <br> The fundamental thing to be aware of, however, is that if you are building an application with a true add-in architecture, you'll need to define your interfaces in such a way that any dependency can be implemented by zero to many classes. The cardinality of dependencies in an add-in architecture is never 1, it's always 0+. Doing this elegantly addresses the issue of 'what if one module overwrites the other?' They mustn't be allowed to do that. All modules have equal priority, and so must their dependencies. This also makes a lot of sense if you think about what an add-in is: usually, it's just a file you drop in a folder somewhere, so there's no explicit order or priority implied.</div> <div class="comment-date">2013-02-15 09:53 UTC</div> </div> <div class="comment" id="ac7a263e0c984f6fa9ac48b6a5044b99"> <div class="comment-author"><a href="http://www.refactorthis.net">Buddy James</a> <a href="#ac7a263e0c984f6fa9ac48b6a5044b99">#</a></div> <div class="comment-content">Mark, Thanks for the input. Just to be clear, Prism also supports a MEF bootstrapper to load the modules as well so you aren't forced to use Unity.. it's just a popular option. Anyway, it's been a pleasure and I loved your book. I'd be honored if you would stop by my blog and check out my articles on Unity and DI . it's refactorthis.net Thanks again!</div> <div class="comment-date">2013-02-16 05:23 UTC</div> </div> <div class="comment" id="4a013b1027164a6d84e422f4d67f5380"> <div class="comment-author">Danyil <a href="#4a013b1027164a6d84e422f4d67f5380">#</a></div> <div class="comment-content">Hi, Mark! What do you think of the <a href="http://sonymathew.blogspot.nl/2009/11/context-ioc-revisited-i-wrote-about.html">Context IoC</a> pattern? Would you consider it distinct from the Service Locator?</div> <div class="comment-date">2015-10-07 14:54 UTC</div> </div> <div class="comment" id="e100f6dc7eae4c238bdf569fb38fd9b0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e100f6dc7eae4c238bdf569fb38fd9b0">#</a></div> <div class="comment-content"> <p> Danyil, thank you for writing. Context IoC, as described in that article, <a href="/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator">isn't service location</a>, because each context injected is only a limited, well-known interface. A Service Locator, on the other hand, exposes <a href="/2014/05/15/service-locator-violates-solid">an infinite set of services</a>. </p> <p> That said, how does Context IoC solve anything that <a href="/2014/06/10/pure-di">Pure DI</a> doesn't solve in a simpler way? </p> </div> <div class="comment-date">2015-10-07 15:37 UTC</div> </div> <div class="comment" id="facadcd5608d4341a4bfa035f0ace47c"> <div class="comment-author">Danyil <a href="#facadcd5608d4341a4bfa035f0ace47c">#</a></div> <div class="comment-content"> <p>To give my question above some context, I've run into online discussion threads where the participants equated the two, which I thought unfairly ignored the Context IoC pattern's better static guarantees. I am glad to hear you disagree with them on the matter of classification.</p> <p>As for Pure DI, I will have to investigate it further. Thanks for your reply!</p> </div> <div class="comment-date">2015-10-07 16:55 UTC</div> </div> <div class="comment" id="1738fcab3ff9475bb70626ad8db7d629"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1738fcab3ff9475bb70626ad8db7d629">#</a></div> <div class="comment-content"> <p> Danyil, thank you for writing. That Pure DI link may not give you the most succinct picture of what I had in mind. Using the blue/red example from the Context IoC article, you can rewrite MyService much simpler using Constructor Injection: </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;"><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MyService</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ISomeService</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ICommonService</span>&nbsp;blue; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ICommonService</span>&nbsp;red; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MyService(<span style="color:#2b91af;">ICommonService</span>&nbsp;blue,&nbsp;<span style="color:#2b91af;">ICommonService</span>&nbsp;red) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(blue&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(blue)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(red&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(red)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.blue&nbsp;=&nbsp;blue; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.red&nbsp;=&nbsp;red; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Members&nbsp;that&nbsp;DO&nbsp;something&nbsp;can&nbsp;go&nbsp;here...</span> }</pre> </p> <p> If you want to add a <em>green</em> dependency to BlueService, you can do that in the same manner: </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;"><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">BlueService</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ICommonService</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">ICommonService</span>&nbsp;green; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;BlueService(<span style="color:#2b91af;">ICommonService</span>&nbsp;green) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.green&nbsp;=&nbsp;green; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Members&nbsp;that&nbsp;DO&nbsp;something&nbsp;can&nbsp;go&nbsp;here...</span> }</pre> </p> <p> You can <a href="/2011/03/04/Composeobjectgraphswithconfidence">compose desired object graphs</a> in your <a href="/2011/07/28/CompositionRoot">Composition Root</a>: </p> <p> <pre style="font-family:Consolas;font-size:13;color:black;"><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">ISomeService</span>&nbsp;Main() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MyService</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">BlueService</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GreenService</span>()), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">RedService</span>()); }</pre> </p> <p> Notice how the shape of the object graph is visibly present due to the (standard) indentation. That makes it easy to gauge the depth and complexity of most object graphs. </p> <p> Does ContextIoC solve anything that this doesn't address in a simpler, less convoluted way with fewer moving parts? </p> </div> <div class="comment-date">2015-10-08 12:06 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Refactoring to Aggregate Services https://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices 2010-02-02T20:56:44+00:00 Mark Seemann <div id="post"> <p>In a <a href="http://jeffreypalermo.com/blog/constructor-over-injection-smell-ndash-follow-up/">follow-up</a> to his <a href="http://jeffreypalermo.com/blog/constructor-over-injection-anti-pattern/">earlier post on Constructor Over-Injection</a>, <a href="http://jeffreypalermo.com/">Jeffrey Palermo</a> changes his stance on Constructor Over-Injection from <em>anti-pattern</em> to the more palatable <em>code smell</em>. In this post I introduce the concept of a <strong>Facade Service</strong> and outline a refactoring that addresses this code smell.</p> <p>If I should extract a core message from Jeffrey Palermo's blog post it would be that it's a code smell if you have a class that takes too many dependencies in its constructor.</p> <p>I can only agree, but only so far as it's a code smell. However, it has nothing to do with DI in general or <strong>Constructor Injection</strong> specifically. Rather, it's a smell that indicates a violation of the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> (SRP). Let's review the example constructor:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> OrderProcessor(<span style="color: #2b91af">IOrderValidator</span> validator, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IOrderShipper</span> shipper, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IAccountsReceivable</span> receivable, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IRateExchange</span> exchange, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IUserContext</span> userContext)</pre> </p> <p>In this version, I even added IOrderShipper back in <a href="/2010/01/20/RebuttalConstructorover-injectionanti-pattern">as I described in my earlier post</a>. Surely, five constructor parameters are too many.</p> <p><em><strong>Constructor Injection</strong> makes SRP violations glaringly obvious.</em></p> <p>What's not to like? My personal threshold lies somewhere around 3-4 constructor parameters, so whenever I hit three, I start to consider if I could perhaps aggregate some of the dependencies into a new type.</p> <p>I call such a type a <strong>Facade Service</strong>. It's closely related to <a href="http://www.refactoring.com/catalog/introduceParameterObject.html">Parameter Objects</a>, but the main difference is that a <strong>Parameter Object</strong> only moves the parameters to a common root, while a <strong>Facade Service</strong> hides the aggregate behavior behind a new abstraction. While the <strong>Facade Service</strong> may start its life as a result of a pure mechanistic refactoring, it often turns out that the extracted behavior represents a Domain Concept in its own right. Congratulations: you've just move a little closer to adhering to the SRP!</p> <p>Let's look at Jeffrey Palermo's OrderProcessor example. The core implementation of the class is reproduced here (recall that in my version, IOrderShipper is also an injected dependency):</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: #2b91af">SuccessResult</span> Process(<span style="color: #2b91af">Order</span> order) { &nbsp;&nbsp;&nbsp; <span style="color: blue">bool</span> isValid = _validator.Validate(order); &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (isValid) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Collect(order); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _shipper.Ship(order); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> CreateStatus(isValid); } &nbsp; <span style="color: blue">private</span> <span style="color: blue">void</span> Collect(<span style="color: #2b91af">Order</span> order) { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">User</span> user = _userContext.GetCurrentUser(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Price</span> price = order.GetPrice(_exchange, _userContext); &nbsp;&nbsp;&nbsp; _receivable.Collect(user, price); }</pre> </p> <p>If you examine the code it should quickly become apparent that the Collect method encapsulates a cluster of dependencies: IAccountsReceivable, IRateExchange and IUserContext. In this case it's pretty obvious because they are already encapsulated in a single private method. In real production code, you may need to perform a series of internal refactorings before a pattern starts to emerge and you can extract an interface that aggregates several dependencies.</p> <p>Now that we have identified the cluster of dependencies, we can extract an interface that closely resembles the Collect method:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IOrderCollector</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">void</span> Collect(<span style="color: #2b91af">Order</span> order); }</pre> </p> <p>In lieu of a better name, I simply chose to call it IOrderCollector, but what's interesting about extracting <strong>Facade Services</strong> is that over time, they often turn out to be previously <em>implicit</em> Domain Concepts that we have now dragged out in the open and made <em>explicit</em>.</p> <p>We can now inject IOrderCollector into OrderProcessor and change the implementation of the private Collect method:</p> <p> <pre style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">void</span> Collect(<span style="color: #2b91af">Order</span> order) { &nbsp;&nbsp;&nbsp; _collector.Collect(order); }</pre> </p> <p>Next, we can remove the redundant dependencies, leaving us with this constructor:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> OrderProcessor(<span style="color: #2b91af">IOrderValidator</span> validator, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IOrderShipper</span> shipper, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IOrderCollector</span> collector)</pre> </p> <p>With three constructor parameters it's much more acceptable, but we can always consider repeating the procedure and extract a new <strong>Facade Service</strong> that aggregates IOrderShipper and IOrderCollector.</p> <p>The original behavior from the Collect method is still required, but is now implemented in the OrderCollector class:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">OrderCollector</span> : <span style="color: #2b91af">IOrderCollector</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IUserContext</span> _userContext; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IRateExchange</span> _exchange; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IAccountsReceivable</span> _receivable; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> OrderCollector(<span style="color: #2b91af">IAccountsReceivable</span> receivable, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IRateExchange</span> exchange, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IUserContext</span> userContext) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _receivable = receivable; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _exchange = exchange; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _userContext = userContext; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IOrderCollector Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Collect(<span style="color: #2b91af">Order</span> order) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">User</span> user = _userContext.GetCurrentUser(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Price</span> price = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order.GetPrice(_exchange, _userContext); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _receivable.Collect(user, price); &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>Here's another class with three constructor parameters, which falls within the reasonable range. However, once again, we can begin to consider whether the interaction between IUserContext and the Order could be better modeled.</p> <p>In outline form, the <em>Introduce Facade Service</em> refactoring follows these steps:</p> <ol> <li>Analyze how dependencies interact to identify clusters of behavior. </li><li>Extract an interface from these clusters. </li><li>Copy the original implementation to a class that implements the new interface. </li><li>Inject the new interface into the consumer. </li><li>Replace the original implementation with a call the new dependency. </li><li>Remove the redundant dependencies. </li><li>Rinse and repeat :)</li></ol> <p>The beauty of <strong>Facade Services</strong> is that we can keep wrapping one <strong>Facade Service</strong> in new <strong>Facade Services</strong> to define more and more coarse-grained building blocks as we get closer and closer to the application boundary.</p> <p>Keeping each class and its dependencies to simple interactions also makes it much easier to unit test all of them because none of them do anything particularly complex.</p> <p>Adhering strictly to <strong>Constructor Injection</strong> makes it easy to see when one violates the SRP and should refactor to an <strong>Facade Service</strong>.</p><p><b>Update (2011.04.10):</b> In <a href="http://amzn.to/12p90MG">my book</a> I've changed the name of this concept to <b>Facade Service</b> as it more clearly communicates the relationship with the Facade pattern.</p><p><b>Last modified (2011.08.23):</b> Changed all references to <i>Aggregate Service</i> (the old name of the concept) to <i>Facade Service.</i><br></p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5342631e9c6d4fd080eed4c6c72a9d5e"> <div class="comment-author">Arnis L. <a href="#5342631e9c6d4fd080eed4c6c72a9d5e">#</a></div> <div class="comment-content">Had to review project of co-workers. Found exactly this anti-pattern and said - this is wrong but currently have no comments about how to fix that.<br> <br> Now i have. Thanks. :)</div> <div class="comment-date">2010-02-11 08:32 UTC</div> </div> <div class="comment" id="3edb3111af084a7bb3be60bf99c2e886"> <div class="comment-author">Sam <a href="#3edb3111af084a7bb3be60bf99c2e886">#</a></div> <div class="comment-content">Great post. Hell, great blog. Thank you for sharing :)</div> <div class="comment-date">2010-04-09 15:29 UTC</div> </div> <div class="comment" id="6831cbef3aef44a3ab18e99a852a820a"> <div class="comment-author">Carl R <a href="#6831cbef3aef44a3ab18e99a852a820a">#</a></div> <div class="comment-content">I really like how you write!<br> <br> I also just read Service locator is an anti pattern and I must say your posts are eye openers.</div> <div class="comment-date">2011-05-03 19:24 UTC</div> </div> <div class="comment" id="332ebffa07564c53a110cda1b0b73385"> <div class="comment-author"><a href="http://stackoverflow.com/users/572644/daniel-hilgarth">Daniel Hilgarth</a> <a href="#332ebffa07564c53a110cda1b0b73385">#</a></div> <div class="comment-content">(Looks like my last comment didn't make it...)<br> I have a question about that.<br> RAP states that you should have more than one implementation per interface.<br> How do you do this with Aggregate Services? In most cases, you will have only one, especially when refactoring...</div> <div class="comment-date">2011-07-29 14:14 UTC</div> </div> <div class="comment" id="ca94b645e9b34400a1797be4457777b9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ca94b645e9b34400a1797be4457777b9">#</a></div> <div class="comment-content">The Reused Abstractions Principle says that we should <em>strive</em> towards having more than one implementation of the same interface, and that we should consider 1:1 interfaces as code smells. That doesn't mean that 1:1 interfaces suddenly have become illegal - only that they warrant more investigation.<br> <br> Even though I find the RAP extremely useful as a guide it doesn't mean that I ferociously stamp out every occurrence of 1:1 interfaces I find in my code bases. For example, when refactoring to Facade Services, I still think that this refactoring alone improves a code base. If that means that we (perhaps temporarily) end up with a 1:1 interface, I wouldn't be that concerned.<br> <br> Still, in <a href="/2010/12/03/Towardsbetterabstractions">this blog post</a> I outline what really is a ranking of composability patterns. More blog posts are going to follow on that topic, but I can already now reveal that <a href="/2011/03/22/CommandsareComposable">the most composable (and thus most reusable) pattern is the Command pattern</a>. Thus, the more Commands you have, the easier it will become to adhere to the RAP.<br> <br> I believe that this is the underlying reason why so many people are reporting that CQRS are helping them to effectively deal with complexity.<br> <br> A Facade Service can easily be expressed as a Command, so there's no conflict between Facade Services and the RAP.</div> <div class="comment-date">2011-07-29 14:31 UTC</div> </div> <div class="comment" id="98fc18d333c645ad93ab7a8b686a5a73"> <div class="comment-author">NightOwl888 <a href="#98fc18d333c645ad93ab7a8b686a5a73">#</a></div> <div class="comment-content">Ok, either I have done something wrong, or I am interpreting what you are saying wrong. I have a business object - that is, an object that represents a domain entity. The object is responsible for maintaining state about the entity and references to its child entities, and as such it requires several services to 1) instantiate the internal state (collections and dictionaries) 2) plugins to provide alternate implementations of business logic, and 3) factories to provide abstract references to global resources (ambient context).<br> <br> I started with 9 constructor parameters and got it down to 6 so far, but I am having trouble now because none of the services are grouped together in blocks like in this article and they don't seem all that related. Here is my constructor before:<br> <br> public SiteMap(<br> ISiteMapBuilder siteMapBuilder,<br> IControllerTypeResolver controllerTypeResolver,<br> IActionMethodParameterResolver actionMethodParameterResolver,<br> IHttpContextFactory httpContextFactory,<br> IAclModule aclModule,<br> ISiteMapNodeCollectionFactory siteMapNodeCollectionFactory,<br> IGenericDictionaryFactory genericDictionaryFactory,<br> IUrlPath urlPath,<br> RouteCollection routes<br> )<br> <br> And here it is now:<br> <br> <br> public SiteMap(<br> ISiteMapBuilder siteMapBuilder,<br> IMvcResolver mvcResolver,<br> IMvcContextFactory mvcContextFactory,<br> IAclModule aclModule,<br> ISiteMapChildStateFactory siteMapChildStateFactory,<br> IUrlPath urlPath<br> )<br> <br> I renamed IHttpContextFactory because it has several types of context related (controller context, request context, http context) creation methods. I then simply added the routes to that factory so it can provide access to the global routes collection.<br> <br> I created a new facade service that implements (and cascades to the original services) the methods from both IControllerTypeResolver and IActionMethodParameterResolver named IMvcResolver.<br> <br> Then I created another facade ISiteMapChildStateFactory that implements and cascades the methods to ISiteMapNodeCollectionFactory and IGenericDictionaryFactory. This one feels a bit like a kludge - the only thing these have in common are that they are used to instantiate the object's internal collections. I feel like this is a step in the wrong direction.<br> <br> Ironically, the entry point of the application is not much of a problem with only 5 dependencies and still room to divide up responsibilities, it is the business entity objects I am having problems with.<br> <br> I am really having trouble finding more ways to combine these dependencies in logical ways. But perhaps I misunderstood - are you even talking about refactoring business entity objects here, or just command services that do not maintain their own state other than what is required to perform the action?<br> <br> </div> <div class="comment-date">2013-02-24 18:55 UTC</div> </div> <div class="comment" id="fb792319a41043bc8dfee9d492894185"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fb792319a41043bc8dfee9d492894185">#</a></div> <div class="comment-content">You and your team are probably the only people who know what the SiteMap class and its dependencies do, so I can't tell you how to group them better. Could the SiteMap be taking on too many responsibilities?</div> <div class="comment-date">2013-02-24 20:21 UTC</div> </div> <div class="comment" id="e06dafaf41a241a58d8074e983c26d8d"> <div class="comment-author">NightOwl888 <a href="#e06dafaf41a241a58d8074e983c26d8d">#</a></div> <div class="comment-content">The SiteMap is only responsible for maintaining its internal state, and it provides accessor methods that other objects use to indirectly manage the state and get information about its current state. Some of these accessor methods rely on services, but none of them rely on 2 or more services.<br> <br> However, the IMvcResolver is only here to sync up the object's lifetime with the lifetime of the SiteMap - it is actually only used by external services, but it makes sense to create a single instance of it that services all of the SiteMap's node objects than to inject it directly into the nodes (not to mention, the situation with the node constructors is even worse). There can be more than one SiteMap instance in the application and the MvcResolver has an internal cache that needs to be in sync with the SiteMap's lifetime in case there are cache name collisions from one SiteMap to the next.<br> <br> I can also tell you that ISiteMapBuilder, IMvcResolver, and IAclModule are all there to support alternate implementations (plugins) of logic. When we've run out of ways to group by domain concepts, in your opinion would it be wise to create a ISiteMapPluginService even though they are technically unrelated plugins? I think that could make sense if each service interface is exposed as a property of the ISiteMapPluginService rather than cascading the calls. Or would it make more sense to group by technology, say group all of the MVC services together?<br> <br> </div> <div class="comment-date">2013-02-24 22:45 UTC</div> </div> <div class="comment" id="428369ecdbf1484d912d175848c8f542"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#428369ecdbf1484d912d175848c8f542">#</a></div> <div class="comment-content">If &quot;accessor methods rely on services, but none of them rely on 2 or more services&quot; it sounds like the SiteMap class has poor cohesion, which again is a sign that it should be split into multiple smaller classes.</div> <div class="comment-date">2013-02-25 07:20 UTC</div> </div> <div class="comment" id="ea96a661bf494f3db59d5660a4ff1a53"> <div class="comment-author">NightOwl888 <a href="#ea96a661bf494f3db59d5660a4ff1a53">#</a></div> <div class="comment-content">Poor cohesion? I thought it was a sign that I pulled out all of the possible facade services already - I considered it a good thing until you mentioned otherwise.<br> <br> I started out with some 4000 lines of code and have whittled it down to 700 lines (including documentation and curly brackets). I am considering what you are saying about splitting this into more classes, but looking at the public interface and seeing all that is there are 1) add, remove, clear, find, and get methods that manage the internal state, 2) methods that pass child node object to another service to process or analyze it in some way or 3) are simple boolean or string properties that pertain to the whole object graph.<br> <br> I can see potentially moving the find methods to an ISiteMapNodeFinder, but this would add constructor parameters, not remove them. I suppose I could potentially create a class that wraps the 4 internal dictionaries along with the accessor methods into a service, which would move the responsibility of instantiating the dictionaries to that service's constructor.<br> <br> Anyway, I ended up taking the path I mentioned - just creating a ISiteMapPluginProvider that exposes service interfaces directly on its interface. Crude, but effective. It ended up making both the SiteMap class and the SiteMapNode class have very similar constructors, so I consider it a win.</div> <div class="comment-date">2013-02-25 14:10 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 1.0 https://blog.ploeh.dk/2010/01/27/AutoFixture1.0 2010-01-27T22:54:58+00:00 Mark Seemann <div id="post"> <p><a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.0 is now available on the CodePlex site! Compared to Release Candidate 2 there are no changes.</p> <p>The <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=38630">1.0 release page</a> has more details about this particular release, but essentially this is RC2 promoted to release status.</p> <p>It's been almost a year since I started development on AutoFixture and I must say that it has been an exciting and fulfilling experience! The API has evolved, but has turned out to be surprisingly flexible, yet robust. I even had some positive surprises along the way as it dawned on me that I could do new fancy things I hadn't originally considered.</p> <p>If you use the Likeness feature (of which I have yet to write), you will run into <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=524901">this bug in Visual Studio</a>. The bug is only in IntelliSense, so any code using Likeness will compile and work just fine.</p> <p>While this release marks the end of AutoFixture's initial days, it also marks the beginning of AutoFixture 2.0. I already have lots of plans for making it even more extensible and powerful, as well as plans for utility libraries that integrate with, say, Moq or Rhino Mocks. It's going to be an exciting new voyage!</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ef6ec1a5b1af4e8ebde00159343ece07"> <div class="comment-author">Adrian Hara <a href="#ef6ec1a5b1af4e8ebde00159343ece07">#</a></div> <div class="comment-content">Having gone through all the posts in the AutoFixture category, as well as the zero-friction TDD posts (first), all i can say is (paraphrasing http://en.wikipedia.org/wiki/Kenny_Bania) &quot;This is Gold! Gold!&quot;. Thanks not only for the tools but for some amazing blog posts!<br> <br> </div> <div class="comment-date">2010-02-08 10:39 UTC</div> </div> <div class="comment" id="6db04f1eba2a442db92cabd2bcab0953"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6db04f1eba2a442db92cabd2bcab0953">#</a></div> <div class="comment-content">Thanks for those kind words. They are much appreciated :)</div> <div class="comment-date">2010-02-08 14:31 UTC</div> </div> <div class="comment" id="8c7ee94a08b14ba4ae606553c7f904d2"> <div class="comment-author">Ruddy lee <a href="#8c7ee94a08b14ba4ae606553c7f904d2">#</a></div> <div class="comment-content">Good stuff!<br> <br> Why called Ploeh?<br> <br> I'm a speaker in MS Taiwan, try to know more about Ploeh.AutoFixture.dll<br> <br> </div> <div class="comment-date">2010-03-07 02:51 UTC</div> </div> <div class="comment" id="a498bdfd8cfb42c886aec46ed6d5f4db"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a498bdfd8cfb42c886aec46ed6d5f4db">#</a></div> <div class="comment-content">Currently, the best place to learn about AutoFixture is this very blog. Apart from that, trying it out on some small, but real software project is always always the best way to truly learn a new technology.<br> <br> If you have specific questions about AutoFixture, the best place to ask them would be in the <a href="http://autofixture.codeplex.com/Thread/List.aspx">AutoFixture discussion forum</a>.<br> <br> HTH</div> <div class="comment-date">2010-03-07 09:17 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. What's so dangerous about a DI attribute? https://blog.ploeh.dk/2010/01/27/What'ssodangerousaboutaDIattribute 2010-01-27T19:36:34+00:00 Mark Seemann <div id="post"> <p> In a reaction to <a href="http://blog.objectmentor.com/articles/category/uncle-bobs-blatherings">Uncle Bob</a>'s recent post on <a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion">Dependency Injection Inversion</a>, <a href="http://blog.cgdecker.com/">Colin Decker</a> writes that he <a href="http://blog.cgdecker.com/2010/01/whats-issue-with-inject.html">doesn't consider the use of the single Guice @Inject annotation particularly problematic</a>. As I read it, the central argument is that </p> <blockquote> <p> annotations are not code. By themselves, they do nothing. </p> </blockquote> <p> I'll have to take that at face value, but if we translate this reasoning to .NET it certainly holds true. Attributes don't do anything by themselves. </p> <p> I'm not aware of any DI Container for .NET that <em>requires</em> us to sprinkle attributes all over our code to work (I don't consider <a href="http://mef.codeplex.com/">MEF</a> a DI Container), but for the sake of argument, let's assume that such a container exists (let's call it <em><a href="http://en.wikipedia.org/wiki/Hypodermic_needle">Needle</a></em>). Would it be so bad if we had to liberally apply the Needle [Inject] attribute in large parts of our code bases? </p> <p> Colin suggests <em>no</em>. As usual, my position is that it depends, but in most cases I would consider it bad. </p> <p> If Needle is implemented like most libraries, InjectAttribute is just one of many types that make up the entire API. Other types would include NeedleContainer and its associated classes. </p> <p> Java annotations may work differently, but in .NET we need to reference a library to apply one of its attributes. To apply the [Inject] attribute, we would have to reference Needle, and herein lies the problem. </p> <p> Once Needle is referenced, it becomes much easier for a junior developer to accidentally start directly using other parts of the Needle API. Particularly he or she may start using Needle as a Service Locator. When that happens, Needle is no longer a passive participant of the code, but a very active one, and it becomes much harder to separate the code from the Container. </p> <p> To paraphrase Uncle Bob: <em>I don't want to write a Needle application.</em> </p> <p> We can't even protect ourselves from accidental usage by writing a <a href="http://blogs.msdn.com/gblock/archive/2008/05/05/prismshouldnotreferenceunity.aspx">convention-based unit test</a> that fails if Needle is referenced by our code, because it <em>must</em> be referenced for the [Inject] attribute to be applied. </p> <p> The point is that the attribute drags in a reference to the entire container, which in my opinion is a bad thing. </p> <p> So when would it be less problematic? </p> <p> If Needle was implemented in such a way that InjectAttribute was defined in an assembly that only contains that one type, and the rest of Needle was implemented in a different assembly, the attribute wouldn't drag the rest of the container along. </p> <p> Whether this whole analysis makes sense at all in Java, and whether Guice is implemented like that, I can't say, but in most cases I would consider even a single attribute to be unacceptable pollution of my code base. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="2a9b212418074af6896416f1f756a35d"> <div class="comment-author"><a href="http://publicobject.com">Jesse Wilson</a> <a href="#2a9b212418074af6896416f1f756a35d">#</a></div> <div class="comment-content">In addition to its own annotations, Guice supports the atinject API. This minimal 6-class package contains only the stuff you need to import in your application code and nothing you don't. These are standard annotations supported by Guice and other Java dependency injection frameworks.<br> http://code.google.com/p/atinject/<br> http://atinject.googlecode.com/svn/tags/1/javadoc/javax/inject/package-summary.html</div> <div class="comment-date">2010-01-28 03:08 UTC</div> </div> <div class="comment" id="c7ca5ccf14dc4fe7a6e9e0a467fb645f"> <div class="comment-author"><a href="http://www.cuttingedge.it/blogs/steven">Steven</a> <a href="#c7ca5ccf14dc4fe7a6e9e0a467fb645f">#</a></div> <div class="comment-content">Ninject does it's injection magic using an [Inject] attribute. I agree with you that sprinkling your code base with those attributes is problematic. Not only because of the direct dependency, but this switching IoC frameworks becomes much harder. The <a href="commonservicelocator.codeplex.com">Common Service Locator</a> project defines an interface for IoC implementations. This allows you to swap implementations. However, when your code is polluted with those attribute declarations, switching isn't easy anymore.</div> <div class="comment-date">2010-02-04 15:09 UTC</div> </div> <div class="comment" id="e4591e8099ad4a8d86c3abbeacae6825"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e4591e8099ad4a8d86c3abbeacae6825">#</a></div> <div class="comment-content">I'm no expert in Ninject, but last time I checked, the [Inject] attribute was <a href="http://ninject.codeplex.com/wikipage?title=Modules%20and%20the%20Kernel&referringTitle=Activation%20Behaviors">optional</a>.</div> <div class="comment-date">2010-02-04 15:41 UTC</div> </div> <div class="comment" id="0a35e96e3e9c49dd9c5e76fcad332ed1"> <div class="comment-author"><a href="https://github.com/babakks">babakks</a> <a href="#0a35e96e3e9c49dd9c5e76fcad332ed1">#</a></div> <div class="comment-content">Sorry to bother, but I'd like to know your opinion about TypeScript DIC containers. Since TS transpiles code into JS, no type information is available at runtime. Therefore, DICs need developers to decorate their classes with the container's special decorators, which is obviously different among various containers. However, there is no other choice for containers. As a suggestion, TS could define a built-in decorator that unifies DIC usages. I've created a suggestion on GitHub (See it <a href="https://github.com/Microsoft/TypeScript/issues/26840">here</a>). I'd love to know your opinion on this issue and on my suggestion. Is there a better way?</div> <div class="comment-date">2018-09-03 5:48 UTC</div> </div> <div class="comment" id="17ac5ef20dc5465180e5b1b66b78d8e4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#17ac5ef20dc5465180e5b1b66b78d8e4">#</a></div> <div class="comment-content"> <p> babakks, thank you for writing. While I'm aware of TypeScript and the overall design philosphy behind it, I've never written any TypeScript code, so I'm not really the right person to ask. As a general observation, though, I recommend <a href="/2014/06/10/pure-di">Pure DI</a>. Unless you have some rare and exotic requirements, <a href="/2012/11/06/WhentouseaDIContainer">a DI Container is almost never the best choice</a>. DI Containers tend to create more problems than they solve. </p> <p> In TypeScript, can't you compose object graphs by simply writing the necessary code? That's what I do in C#, at least... </p> </div> <div class="comment-date">2018-09-04 6:01 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. IWindsorInstaller https://blog.ploeh.dk/2010/01/26/IWindsorInstaller 2010-01-26T19:24:51+00:00 Mark Seemann <div id="post"> <p>Reacting to my <a href="/2010/01/25/DependencyInjectionInversionin.NET">previous post</a>, <a href="http://kozmic.pl/Default.aspx">Krzysztof Koźmic</a> was so kind to point out to me that I really should be using an IWindsorInstaller instead of writing the registration code in a static helper method (it <em>did</em> make me cringe a bit).</p> <p>As it turns out, IWindsorInstaller is not a particularly well-described feature of <a href="http://castleproject.org/container/index.html">Castle Windsor</a>, so here's a quick introduction. Fortunately, it is very easy to understand.</p> <p>The idea is simply to package configuration code in reusable modules (just like the Guice modules from <a href="https://sites.google.com/site/unclebobconsultingllc/home/articles/dependency-injection-inversion">Uncle Bob's post</a>).</p> <p>Refactoring the bootstrap code from my previous post, I can now move all the container configuration code into a reusable module:</p> <p><pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">BillingContainerInstaller</span> : <span style="color: #2b91af">IWindsorInstaller</span> { <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IWindsorInstaller Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Install(<span style="color: #2b91af">IWindsorContainer</span> container, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IConfigurationStore</span> store) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.AddComponent&lt;<span style="color: #2b91af">TransactionLog</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">DatabaseTransactionLog</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.AddComponent&lt;<span style="color: #2b91af">CreditCardProcessor</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyCreditCardProcessor</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.AddComponent&lt;<span style="color: #2b91af">BillingService</span>&gt;(); &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre></p> <p>While I was at it, I also changed from the fluent registration API to the generic registration methods as I didn't really need the full API, but I could have left it as it was.</p> <p>BillingContainerInstaller implements IWindsorInstaller, and I can now configure my container instance like this:</p> <p><pre><span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">WindsorContainer</span>(); container.Install(<span style="color: blue">new</span> <span style="color: #2b91af">BillingContainerInstaller</span>());</pre></p> <p>The Install method takes a parameter array of IWindsorInstaller instances, so you can pass as many as you'd like.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5e1f8aecb22e4497b8c2d50fbd7b3bf2"> <div class="comment-author">Thomas <a href="#5e1f8aecb22e4497b8c2d50fbd7b3bf2">#</a></div> <div class="comment-content">It seemes that this feature is something like Registry in StructureMap?</div> <div class="comment-date">2010-01-26 21:35 UTC</div> </div> <div class="comment" id="1a4e94e2bf2d40c7a7123c93a362e086"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1a4e94e2bf2d40c7a7123c93a362e086">#</a></div> <div class="comment-content">I have to admit that I'm not yet entirely up to speed on all the different DI Containers out there, but it's a common feature that several DI Containers have.</div> <div class="comment-date">2010-01-26 22:04 UTC</div> </div> <div class="comment" id="78ad38195f5849359558c6d798c67ac6"> <div class="comment-author"><a href="http://blog.strobaek.org">Karsten Str&#248;b&#230;k</a> <a href="#78ad38195f5849359558c6d798c67ac6">#</a></div> <div class="comment-content">This may be trivial, but remember to implement a ReleasePolicy when using transient objects. Otherwise you will use an unheard amount of RAM to hold all instances created, because .Release() will not be called on each one of them.</div> <div class="comment-date">2010-01-27 07:14 UTC</div> </div> <div class="comment" id="cb9750ed230b47b893ddf152e7363832"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cb9750ed230b47b893ddf152e7363832">#</a></div> <div class="comment-content">Thanks for pointing that out - did you have <a href="http://davybrion.com/blog/2008/12/the-importance-of-releasing-your-components-through-windsor/">this particular blog post</a> in mind?<br> <br> I'll have to investigate whether that still holds true. If so, I would be inclined to consider it a bug on Windsor's part, but it may be by design, influenced by considerations I simply have yet to realize.</div> <div class="comment-date">2010-01-27 08:17 UTC</div> </div> <div class="comment" id="d8f1736fa6d14a108044d3ec72cdc432"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d8f1736fa6d14a108044d3ec72cdc432">#</a></div> <div class="comment-content">Windsor committer <a href="http://kozmic.pl/Default.aspx">Krzysztof Koźmic</a> was so kind to investigate this, and he left a comment at <a href="http://davybrion.com/blog/2008/12/the-importance-of-releasing-your-components-through-windsor/">the blog post</a> to the effect that this was a bug in Windsor. It is now fixed in Windsor 2.1, so having dependencies implement IDisposable should be enough.</div> <div class="comment-date">2010-01-27 09:02 UTC</div> </div> <div class="comment" id="5476350e649240c9ba53a39cbbf0e247"> <div class="comment-author">urkurk <a href="#5476350e649240c9ba53a39cbbf0e247">#</a></div> <div class="comment-content">hi mark,<br> <br> was wondering where i could find a sample application that uses castle windsor and asp.net webforms?</div> <div class="comment-date">2010-04-04 09:52 UTC</div> </div> <div class="comment" id="12f10a23754240c5b2a25d81a325bcee"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#12f10a23754240c5b2a25d81a325bcee">#</a></div> <div class="comment-content">Hi urkurk<br> <br> Thank you for writing. Unfortunately, I'm not aware of such a sample application. Have you tried asking on the <a href="http://groups.google.com/group/castle-project-users">Castle Windsor forum</a>?</div> <div class="comment-date">2010-04-04 14:49 UTC</div> </div> <div class="comment" id="a02d597e748842cc9455d767749b5ed9"> <div class="comment-author"><a href="http://twitter.com/superbecio">Superbecio</a> <a href="#a02d597e748842cc9455d767749b5ed9">#</a></div> <div class="comment-content">The system is not modular when you have to explicitly instanciate the BillingContainerInstaller on your code and pass the instance to the install method, I haven't investigate on the IWindsorInstaller yet, but I'm hoping that Windsor during the normal registration process of components canintercept those that implement that interface, and automatically call the Install method.<br> <br> This sounds very similar to the startable facility though, with the important difference that in this case you have some more arguments.</div> <div class="comment-date">2010-09-22 09:25 UTC</div> </div> <div class="comment" id="af1014eda29b454fa24d7f7d592d44b2"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#af1014eda29b454fa24d7f7d592d44b2">#</a></div> <div class="comment-content">AFAIR the new version of Castle Windsor (2.5) enables you to specify installers in app.config.</div> <div class="comment-date">2010-09-22 09:47 UTC</div> </div> <div class="comment" id="399ded0af76a42ba95c6d9305305f658"> <div class="comment-author">Sasireka <a href="#399ded0af76a42ba95c6d9305305f658">#</a></div> <div class="comment-content">Hi,<br> <br> I am new to castle windsor. Could you please tell me the difference between container.AddComponent(...) &amp; container.Register(component.For&lt;...&gt;..).<br> <br> When we need to use AddComponent() / Register()?<br> <br> Thanks in advance !</div> <div class="comment-date">2011-08-11 09:39 UTC</div> </div> <div class="comment" id="3c2a5711bc0f4098bbf02e92a2e99c5f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3c2a5711bc0f4098bbf02e92a2e99c5f">#</a></div> <div class="comment-content">AFAIK AddComponent is a legacy method and will be removed in future versions of Castle Windsor, but that I didn't know when I wrote that specific example :$</div> <div class="comment-date">2011-08-11 09:44 UTC</div> </div> <div class="comment" id="8825a76b73574852a986ba2b1c19be36"> <div class="comment-author">Sasireka <a href="#8825a76b73574852a986ba2b1c19be36">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Thank you for your answer. I have an another clarification. What are the advantages offer the castle windsor framework rather than using the normal coding.<br> <br> </div> <div class="comment-date">2011-08-12 05:34 UTC</div> </div> <div class="comment" id="093642162cd64b828604d6c7f180e2c4"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#093642162cd64b828604d6c7f180e2c4">#</a></div> <div class="comment-content">The <a href="http://stackoverflow.com/questions/5667801/arguments-against-inversion-of-control-containers/5668093#5668093">same advantage</a> as any other DI Container.<br> <br> In addition to that, Castle Windsor (and Unity and Spring.NET) also supports <a href="/2010/09/20/InstrumentationwithDecoratorsandInterceptors">interception</a>.</div> <div class="comment-date">2011-08-12 06:44 UTC</div> </div> <div class="comment" id="20d779d99f2441d9b0c6b5c2f966e1a2"> <div class="comment-author">sasireka <a href="#20d779d99f2441d9b0c6b5c2f966e1a2">#</a></div> <div class="comment-content">Hi Mark,<br> <br> I need another help about castle windsor.<br> <br> Could you please tell about how to check whether the particular assembly registered or not in the castle container.<br> <br> Actually i am using the below code for register the assembly. i need to avoid the registering process, if it was already registered. <br> <br> container.Register(AllTypes.FromAssemblyNamed(pAssemblyName).Pick().WithService.DefaultInterface()); <br> Thanks in advance.</div> <div class="comment-date">2011-09-19 09:43 UTC</div> </div> <div class="comment" id="8de89fbf61d642bd8ef6d9d667134465"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#8de89fbf61d642bd8ef6d9d667134465">#</a></div> <div class="comment-content">I can't recall off the top of my head - may I suggest that you ask on <a href="http://stackoverflow.com/">Stack Overflow</a>?</div> <div class="comment-date">2011-09-19 10:12 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency Injection Inversion in .NET https://blog.ploeh.dk/2010/01/25/DependencyInjectionInversionin.NET 2010-01-25T20:48:27+00:00 Mark Seemann <div id="post"> <p>About a week ago <a href="http://blog.objectmentor.com/articles/category/uncle-bobs-blatherings">Uncle Bob</a> published a post on <a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion">Dependency Injection Inversion</a> that caused quite a stir in the tiny part of the .NET community I usually pretend to hang out with. Twitter was alive with much debate, but <a href="http://ayende.com/Blog/Default.aspx">Ayende</a> seems to <a href="http://ayende.com/Blog/archive/2010/01/22/rejecting-dependency-injection-inversion.aspx">sum up</a> the .NET DI community's sentiment pretty well:</p> <blockquote> <p>if this is a typical example of IoC usage in the Java world, then [Uncle Bob] should peek over the fence to see how IoC is commonly implemented in the .Net space</p></blockquote> <p>Despite having initially left a more or less positive note to Uncle Bob's post, after having re-read it carefully, I am beginning to think the same, but instead of just <em>telling</em> everyone how much greener the grass is on the .NET side, let me <em>show</em> you.</p> <p>First of all, let's translate Uncle Bob's BillingService to C#:</p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">BillingService</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">CreditCardProcessor</span> processor; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">TransactionLog</span> transactionLog; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> BillingService(<span style="color: #2b91af">CreditCardProcessor</span> processor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">TransactionLog</span> transactionLog) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (processor == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"processor"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (transactionLog == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"transactionLog"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.processor = processor; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.transactionLog = transactionLog; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> ProcessCharge(<span style="color: blue">int</span> amount, <span style="color: blue">string</span> id) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> approval = <span style="color: blue">this</span>.processor.Approve(amount, id); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.transactionLog.Log(<span style="color: blue">string</span>.Format( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Transaction by {0} for {1} {2}"</span>, id, amount, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.GetApprovalCode(approval))); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">string</span> GetApprovalCode(<span style="color: blue">bool</span> approval) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> approval ? <span style="color: #a31515">"approved"</span> : <span style="color: #a31515">"denied"</span>; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p>It's nice how easy it is to translate Java code to C#, but apart from casing and other minor deviations, let's focus on the main difference. I've added Guard Clauses to protect the injected dependencies against null values as I consider this an essential and required part of <strong>Constructor Injection</strong> - I think Uncle Bob should have added those as well, but he might have omitted them for brevity.</p> <p>If you disregard the Guard Clauses, the C# version is a logical line of code shorter than the Java version because it has no DI attribute like Guice's @Inject.</p> <p>Does this mean that we can't do DI with the C# version of BillingService? Uncle Bob seems to imply that we can do <em>Dependency Inversion</em>, but not <em>Dependency Injection</em> - or is it the other way around? I can't really make head or tails of that part of the post…</p> <p>The interesting part is that in .NET, <em>there's no difference!</em> We can use DI Containers with the BillingService without sprinkling DI attributes all over our code base. The BillingService class has no reference to any DI Container.</p> <p>It does, however, use the central DI pattern <strong>Constructor Injection</strong>. .NET DI Containers know all about this pattern, and with .NET's static type system they know all they need to know to wire dependencies up correctly. (I thought that Java had a static type system as well, but perhaps I am mistaken.) The .NET DI Containers will figure it out for you - you don't have to explicitly tell them how to invoke a constructor with two parameters.</p> <p>We can write an entire application by using <strong>Constructor Injection</strong> and stacking dependencies <em>without ever referencing a container!</em></p> <p>Like the Lean concept of the <em>Last Responsible Moment</em>, we can wait until the application's entry point to decide how we will wire up the dependencies.</p> <p>As Uncle Bob suggests, we can use <strong>Poor Man's DI</strong> and manually create the dependencies directly in Main, but as <a href="http://ayende.com/Blog/archive/2010/01/22/rejecting-dependency-injection-inversion.aspx">Ayende correctly observes</a>, that only looks like an attractive alternative because the example is so simple. For complex dependency graphs, a DI Container is a much better choice.</p> <p>With the C# version of BillingService, which DI Container must we select?</p> <p>It doesn't matter: we can choose whichever one we would like because we have been following patterns instead of using a framework.</p> <p>Here's an example of an implementation of Main using <a href="http://castleproject.org/">Castle Windsor</a>:</p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args) { &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> container = <span style="color: blue">new</span> <span style="color: #2b91af">WindsorContainer</span>(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Program</span>.Configure(container); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> billingService = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.Resolve&lt;<span style="color: #2b91af">BillingService</span>&gt;(); &nbsp;&nbsp;&nbsp; billingService.ProcessCharge(<span style="color: #a52a2a">2034</span>, <span style="color: #a31515">"Bob"</span>); }</pre> </p> <p>This looks a lot like Uncle Bob's first Guice example, but instead of injecting a BillingModule into the container, we can configure it inline or in a helper method:</p> <p> <pre><span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Configure(<span style="color: #2b91af">WindsorContainer</span> container) { &nbsp;&nbsp;&nbsp; container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">TransactionLog</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">DatabaseTransactionLog</span>&gt;()); &nbsp;&nbsp;&nbsp; container.Register(<span style="color: #2b91af">Component</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .For&lt;<span style="color: #2b91af">CreditCardProcessor</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">MyCreditCardProcessor</span>&gt;()); &nbsp;&nbsp;&nbsp; container.Register(<span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">BillingService</span>&gt;()); }</pre> </p> <p>This corresponds more or less to the Guice-specific BillingModule, although Windsor also requires us to register the concrete BillingService as a component (this last step varies a bit from DI Container to DI Container - it is, for example, redundant in <a href="http://www.codeplex.com/unity">Unity</a>).</p> <p>Imagine that in the future we want to rewire this program to use a different DI Container. The only piece of code we need to change is this <strong>Composition Root</strong>. We need to change the container declaration and configuration and then we are ready to use a different DI Container.</p> <p>The bottom line is that Uncle Bob's <em>Dependency Injection Inversion</em> is redundant in .NET. Just use a few well-known design patterns and principles and you can write entire applications with DI-friendly, DI-agnostic code bases.</p> <p>I recently posted a <a href="http://stackoverflow.com/questions/2045904/dependency-inject-di-friendly-library/2047657#2047657">first take on guidelines for writing DI-agnostic code</a>. I plan to evolve these guiding principles and make them a part of <a href="http://www.manning.com/seemann">my upcoming book</a>.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="562eddf6551844a0b1ebc56783fad88f"> <div class="comment-author"><a href="http://blog.cgdecker.com">Colin Decker</a> <a href="#562eddf6551844a0b1ebc56783fad88f">#</a></div> <div class="comment-content">I don't agree with your implication that Uncle Bob's views are justified in the context of Java's DI frameworks... they are not. He appears to understand neither how DI frameworks in general are supposed to be used nor all the advantages they provide. That's where the real problem lies, not in the differences between .NET and Java frameworks.<br> <br> The *only* difference between the example you give here and how Guice is used is that in Guice you'd annotate the constructor with @Inject, and I don't really buy there being any significant disadvantage to doing so in most cases. I posted in a bit more detail why I don't think the use of @Inject is a problem on my <a href="http://blog.cgdecker.com/2010/01/whats-issue-with-inject.html">blog</a> yesterday.</div> <div class="comment-date">2010-01-26 22:18 UTC</div> </div> <div class="comment" id="94df3d6f55e6400a9c89b88d9ff5c628"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#94df3d6f55e6400a9c89b88d9ff5c628">#</a></div> <div class="comment-content">Thanks for your comment.<br> <br> If my previous post left the impression that I find Uncle Bob's views justified in the context of Java's DI Containers, that was accidental: I have no opinion on whether or not this is the case as I know too little about the detailed mechanics of Java and the DI Containers available there to have an opinion either for or against.<br> <br> Whether or not a single annotation constitutes a problem or not ended up warranting <a href="/2010/01/27/What'ssodangerousaboutaDIattribute">an entire new blog post</a> in response :)</div> <div class="comment-date">2010-01-27 19:49 UTC</div> </div> <div class="comment" id="7ebd36951333488db5078a349c4ddfb3"> <div class="comment-author">Ricardo Lacerda Castelo Branco <a href="#7ebd36951333488db5078a349c4ddfb3">#</a></div> <div class="comment-content">Mark, <br> <br> I think your code still has the same problem that &quot;Uncle Bob&quot; was trying to solve. If you want a new instance of BillingService from deep in the bowels of the application, you need to reference the container! So, you are creating a dependency on WindsorContainer. <br> <br> Maybe the solution would be program to an interface instead of depend on a concrete container.<br> <br> What do you think?</div> <div class="comment-date">2010-02-01 02:24 UTC</div> </div> <div class="comment" id="777170d4891049e4ae97ef558daa4a87"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#777170d4891049e4ae97ef558daa4a87">#</a></div> <div class="comment-content">That is actually a different issue, but I agree that if we need to take a dependency on WindsorContainer or any other particular DI Container, we haven't solved the problem.<br> <br> However, explaining how to deal with that will require another blog post - watch this space the next couple of days.</div> <div class="comment-date">2010-02-01 05:47 UTC</div> </div> <div class="comment" id="675fb58873fd43188a25b55ce2e2af35"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#675fb58873fd43188a25b55ce2e2af35">#</a></div> <div class="comment-content">How do we deal with the issue if we need BillingService deep in the bowels of the application?<br> <br> Well, take a dependency on BillingService using standard <b>Constructor Injection</b>. Does this mean that we should have to pass an instance of BillingService through all constructors on the way down? No, rather not.<br> <br> The answer lies in <a href="/2010/02/02/RefactoringtoAggregateServices">Aggregate Services</a>. Instead of taking dependencies with the only purpose of passing them on to other dependencies, we can define a more coarse-grained service that encapsulates the desired behavior. This is exactly the kind of scenario where DI Containers excel, because they are able to auto-wire complex object graphs based entirely on their knowledge of which dependencies are required by each concrete type. They can do that when you use <b>Constructor Injection</b> because this knowledge is statically encoded in the type.<br> <br> Then what if you need BillingService <em>both</em> deep in the bowels of the application <em>as well as</em> near the surface? In most cases I would say that this is a design smell that indicates that the granularity of BillingService is poorly defined. A dependency should be either a fine-grained 'leaf' dependency, or an Aggregate Service - not both.</div> <div class="comment-date">2010-02-03 10:03 UTC</div> </div> <div class="comment" id="1c25f3af91c54021b1d9d384dad8d786"> <div class="comment-author"><a href="http://www.google.com/accounts/o8/id?id=AItOawnweLjwm87YGbIMob3fwisgmDawAWBgk9U">www.google.com/accounts/o8/id?id=AItOawnweLjwm87YGbIMob3fwisgmDawAWBgk9U</a> <a href="#1c25f3af91c54021b1d9d384dad8d786">#</a></div> <div class="comment-content">Thank You for your great contributions to the community</div> <div class="comment-date">2011-02-06 11:39 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 1.0 RC2 https://blog.ploeh.dk/2010/01/20/AutoFixture1.0RC2 2010-01-20T22:59:39+00:00 Mark Seemann <div id="post"> <p><a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.0 Release Candidate 2 is now available on the CodePlex site! Compared to Release Candidate 1 there are very few changes, but the test period uncovered the need for a few extra methods on a recent addition to the library. RC2 contains these additional methods.</p> <p>This resets the clock for the Release Candidate trial period. Key users have a chance to veto this release until a week from now. If no-one complains within that period, we will promote RC2 to version 1.0.</p> <p>The <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=39161">RC2 release page</a> has more details about this particular release.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Enabling DI for Lazy Components https://blog.ploeh.dk/2010/01/20/EnablingDIforLazyComponents 2010-01-20T18:08:36+00:00 Mark Seemann <div id="post"> <p>My <a href="/2010/01/20/RebuttalConstructorover-injectionanti-pattern">previous post</a> led to this comment by <a href="http://haacked.com/">Phil Haack</a>:</p> <blockquote> <p>Your LazyOrderShipper directly instantiates an OrderShipper. What about the dependencies that OrderShipper might require? What if those dependencies are costly?</p></blockquote> <p>I didn't want to make my original example more complex than necessary to get the point across, so I admit that I made it a bit simpler than I might have liked. However, the issue is easily solved by enabling DI for the LazyOrderShipper itself.</p> <p>As always, when the dependency's lifetime may be shorter than the consumer, the solution is to inject (via the constructor!) an Abstract Factory, as this modification of LazyOrderShipper shows:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">LazyOrderShipper2</span> : <span style="color: #2b91af">IOrderShipper</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IOrderShipperFactory</span> factory; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">IOrderShipper</span> shipper; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> LazyOrderShipper2(<span style="color: #2b91af">IOrderShipperFactory</span> factory) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (factory == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"factory"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.factory = factory; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IOrderShipper Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Ship(<span style="color: #2b91af">Order</span> order) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (<span style="color: blue">this</span>.shipper == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.shipper = <span style="color: blue">this</span>.factory.Create(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.shipper.Ship(order); &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>But, doesn't that reintroduce the OrderShipperFactory that I earlier claimed was a bad design?</p> <p>No, it doesn't, because this IOrderShipperFactory doesn't rely on static configuration. The other point is that while we do have an IOrderShipperFactory, the original design of OrderProcessor is unchanged (and thus blissfully unaware of the existence of this Abstract Factory).</p> <p>The lifetime of the various dependencies is completely decoupled from the components themselves, and this is as it should be with DI.</p> <p>This version of LazyOrderShipper is more reusable because it doesn't rely on any particular implementation of OrderShipper - it can Lazily create any IOrderShipper.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="16d644a8a1f147a58f8f187a84798c5f"> <div class="comment-author"><a href="http://twitter.com/zsepi">Peter Zsoldos</a> <a href="#16d644a8a1f147a58f8f187a84798c5f">#</a></div> <div class="comment-content">We could indicate optional dependencies by using Func&lt;TResult&gt; - http://msdn.microsoft.com/en-us/library/bb534960.aspx - delegates for the given constructor parameter. The given DI framework could be configured to resolve any Func&lt;TResult&gt; as x =&gt; Container.Resolve&lt;TResult&gt;()<br> <br> This way the lazy/optional nature of the parameter is obvious to clients of the class, and there is no need to generate lazy implementation classes manually.<br> <br> Note: I haven't had any practical experience using DI frameworks, so the above might not be possible at all :)</div> <div class="comment-date">2010-01-20 20:44 UTC</div> </div> <div class="comment" id="87569c020ba842cfa70f3b8f360a0b45"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#87569c020ba842cfa70f3b8f360a0b45">#</a></div> <div class="comment-content">Yes, Func&lt;T&gt; is sometimes a viable option. In general, I consider <a href="/2009/05/28/DelegatesAreAnonymousInterfaces">delegates to be anonymous interfaces</a>, so Func&lt;T&gt; is really just an Abstract Factory. In other words, IOrderShipperFactory is functionally equivalent to Func&lt;IOrderShipper&gt;.<br> <br> I had a period where I used a lot of delegates as injected dependencies, but I have more or less abandonded that approach again. While it technically works fine, it makes unit testing a bit harder because it's harder to test that a given object contains a specific type of Strategy if it's just a Func&lt;T&gt; or similar.<br> <br> In any case, I'm mostly familiar with Castle Windsor at the moment. Although I have yet to try it out, I think the new <a href="http://kozmic.pl/archive/2009/12/24/castle-typed-factory-facility-reborn.aspx">Typed Factory Facility</a> fits the bill very well - with that, we would never have to code a real implementation of IOrderShipperFactory because Windsor would be able to dynamically emit one for us.</div> <div class="comment-date">2010-01-20 21:56 UTC</div> </div> <div class="comment" id="20ec6acbf99647338e456ab99c09f512"> <div class="comment-author"><a href="http://twitter.com/zsepi">Peter Zsoldos</a> <a href="#20ec6acbf99647338e456ab99c09f512">#</a></div> <div class="comment-content">I have the feeling I did not set the context. Let me do that, and tell me if I the issues you raised still hold - both of them are important!<br> <br> What I meant to propose is that we change Jeffrey Palermo's original example like the below:<br> - private readonly IOrderShipper _shipper;<br> + private readonly Func&lt; IOrderShipper &gt; _shipperFactory;<br> <br> <br> - public OrderProcessor(IOrderValidator validator, IOrderShipper shipper)<br> + public OrderProcessor(IOrderValidator validator, Func&lt; IOrderShipper &gt; shipperFactory)<br> <br> - _shipper = shipper;<br> + _shipperFactory = shipperFactory;<br> <br> - _shipper.Ship(order);<br> + _shipperFactory().Ship(order);<br> <br> The change to the tests should be straightforward as well,<br> <br> - new OrderProcessor(validator, mockShipper) <br> + new OrderProcessor(validator, () =&gt; mockShipper) <br> </div> <div class="comment-date">2010-01-20 22:29 UTC</div> </div> <div class="comment" id="2f91faf306c6443f8cee6679663b92bb"> <div class="comment-author"><a href="http://nblumhardt.com">Nicholas Blumhardt</a> <a href="#2f91faf306c6443f8cee6679663b92bb">#</a></div> <div class="comment-content">I'm surprised no one has mentioned .NET 4's Lazy&lt;T&gt;.<br> <br> To communicate intent, it's clearer than Func&lt;T&gt;:<br> <br> <em>public UsesOrderShipper(<strong>Lazy&lt;IOrderShipper&gt;</strong> orderShipper)</em><br> <br> There's a <a href="http://nblumhardt.com/2009/12/lazing-around-with-autofac2/">more complete example</a> using Lazy&lt;T&gt; with Autofac.<br> <br> Cheers,<br> Nick</div> <div class="comment-date">2010-01-21 10:59 UTC</div> </div> <div class="comment" id="1b65adff33ee4a208a6c0752bdc8b4e5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1b65adff33ee4a208a6c0752bdc8b4e5">#</a></div> <div class="comment-content">To be fair, Alwin mentioned it over on Jeffrey Palermo's original post before I posted my response.<br> <br> That would definitely be an option as well, but I rather wanted to show the route involving absolutely no redesign of the original OrderProcess, and I couldn't do that purely with Lazy&lt;IOrderShipper&gt;. The most important point I wanted to make is that you can solve this problem using basic tools available since .NET 1.0.<br> <br> It would, however, make a lot of sense to implement LazyOrderShipper by injecting Lazy&lt;IOrderShipper&gt; into it instead of inventing a new IOrderShipperFactory interface.</div> <div class="comment-date">2010-01-21 17:47 UTC</div> </div> <div class="comment" id="5dfc9cb47b24430b8cb3dfb370ad87b9"> <div class="comment-author"><a href="http://ctrl-shift-b.com">Derek Greer</a> <a href="#5dfc9cb47b24430b8cb3dfb370ad87b9">#</a></div> <div class="comment-content">I like the Func&amp;lt;T&amp;gt; and Lazy&amp;lt;T&amp;gt; solutions for addressing any real performance issues, but based upon the original example I still submit that the cleanest approach would be to just register the type with a singleton lifestyle to begin with. After the first two valid orders, it's more efficient.</div> <div class="comment-date">2010-01-22 15:25 UTC</div> </div> <div class="comment" id="a7ca59ef789b416db20962a8e7af7f6f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#a7ca59ef789b416db20962a8e7af7f6f">#</a></div> <div class="comment-content">@Derek Greer: Aggreed, and that was also my initial point in my previous post.</div> <div class="comment-date">2010-01-22 23:54 UTC</div> </div> <div class="comment" id="3563325048f340799ddc907f768e4c7b"> <div class="comment-author">Thomas <a href="#3563325048f340799ddc907f768e4c7b">#</a></div> <div class="comment-content">Mark,<br> <br> While it's easy to get it work with Typed Factory Facility and Castle, how do you implement the factory :<br> <br> - without static configuration ?<br> - without passing the container in ?<br> <br> Or I missed something ?<br> <br> Thanks,<br> <br> Thomas</div> <div class="comment-date">2012-03-14 21:52 UTC</div> </div> <div class="comment" id="3fa1c2f9ba624adfa478d833c43f5b1c"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3fa1c2f9ba624adfa478d833c43f5b1c">#</a></div> <div class="comment-content">Thomas, who said anything about a DI Container or Castle Windsor for that matter?</div> <div class="comment-date">2012-03-15 06:47 UTC</div> </div> <div class="comment" id="0fe28cd7d10c4760b8ed449e9f19703a"> <div class="comment-author">Thomas <a href="#0fe28cd7d10c4760b8ed449e9f19703a">#</a></div> <div class="comment-content">Mark,<br> <br> I was refering to your first comment. If I have no problem with the pattern I would like to know how you would do from the implementation point of view.<br> <br> Thanks,<br> <br> Thomas</div> <div class="comment-date">2012-03-15 08:50 UTC</div> </div> <div class="comment" id="32a7707c8e6e4e9399a52f65ad51c800"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#32a7707c8e6e4e9399a52f65ad51c800">#</a></div> <div class="comment-content">Ah, sorry... I'm not sure I entirely understand the question. With Windsor's Typed Factory Facility, you'd reqister the IOrderShipperFactory as being auto-generated by Windsor. I can't recall the exact syntax for this right now, but that registration would happen as part of the Registration phase of RRR.</div> <div class="comment-date">2012-03-15 09:26 UTC</div> </div> <div class="comment" id="7cd2add33aab4cebabb701005aaf41d2"> <div class="comment-author">Thomas <a href="#7cd2add33aab4cebabb701005aaf41d2">#</a></div> <div class="comment-content">Mark,<br> <br> With Windsor there is no problem as TypedFactoryFacility provides implementation on the fly. However if you take another container you have to provide the implementation of IOrderShipperFactory on your own. Now the question is. How my implementation of the factory will pull the IOrderShipper implementation from the container ? I see two choices : <br> <br> - configure staticaly (like Jeffrey did in his post)<br> - pass the container into the factory that it could resolve IOrderShipper.<br> - third choice that I don't know :)<br> <br> I hope it's clearer now. Let me know if it doesn't make sense.<br> <br> Thanks,<br> <br> Thomas</div> <div class="comment-date">2012-03-15 10:04 UTC</div> </div> <div class="comment" id="c4e54ed2b298423bad0a70e71d123234"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c4e54ed2b298423bad0a70e71d123234">#</a></div> <div class="comment-content">Thomas, I wrote a <a href="/2012/03/15/ImplementinganAbstractFactory">new blog post</a> to answer your question.<br> <br> HTH</div> <div class="comment-date">2012-03-15 21:04 UTC</div> </div> <div class="comment" id="011599cfbf994c3b985a1588d72cc613"> <div class="comment-author">Thomas <a href="#011599cfbf994c3b985a1588d72cc613">#</a></div> <div class="comment-content">Thanks Mark, I go to read it :)</div> <div class="comment-date">2012-03-16 14:11 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Rebuttal: Constructor over-injection anti-pattern https://blog.ploeh.dk/2010/01/20/RebuttalConstructorover-injectionanti-pattern 2010-01-20T16:28:03+00:00 Mark Seemann <div id="post"> <p><a href="http://jeffreypalermo.com/">Jeffrey Palermo</a> recently posted a blog post titled <a href="http://jeffreypalermo.com/blog/constructor-over-injection-anti-pattern/">Constructor over-injection anti-pattern</a> - go read his post first if you want to be able to follow my arguments.</p> <p>His point seems to be that Constructor Injection can be an anti-pattern if applied too much, particularly if a consumer doesn't need a particular dependency in the majority of cases.</p> <p>The problem is illustrated in this little code snippet:</p> <p> <pre style="margin: 0px"><span style="color: blue">bool</span> isValid = _validator.Validate(order);&nbsp; <span style="color: blue">if</span> (isValid) { &nbsp;&nbsp;&nbsp; _shipper.Ship(order); }</pre> </p> <p>If the Validate method returns false often, the shipper dependency is never needed. </p> <p>This, he argues, can lead to inefficiencies if the dependency is costly to create. It's not a good thing to require a costly dependency if you are not going to use it in a lot of cases.</p> <p>That sounds like a reasonable statement, but is it really? And is the proposed solution a good solution?</p> <p><em>No, this isn't a reasonable statement, and the proposed solution isn't a good solution.</em></p> <p>It would seem like there's a problem with Constructor Injection, but in reality the problem is that it is being used incorrectly and in too constrained a way.</p> <p>The proposed solution is problematic because it involves tightly coupling the code to OrderShipperFactory. This is more or less a specialized application of the Service Locator anti-pattern.</p> <p>Consumers of OrderProcessor have no static type information to warn them that they need to configure the OrderShipperFactory.CreationClosure static member - a completely unrelated type. This may technically work, but creates a very developer-unfriendly API. IntelliSense isn't going to be of much help here, because when you want to create an instance of OrderProcessor, it's not going to remind you that you need to statically configure OrderShipperFactory first. Enter lots of run-time exceptions.</p> <p>Another issue is that he allows a <em>concrete</em> implementation of an interface to <em>change the design</em> of the OrderProcessor class - that's hardly in the spirit of the Liskov Substitution Principle. I consider this a strong design smell.</p> <p>One of the commenters (Alwin) suggests instead injecting an IOrderShipperFactory. While this is a better option, it still suffers from letting a concrete implementation influence the design, but there's a better solution.</p> <p>First of all we should realize that the whole case is a bit construed because although the IOrderShipper implementation may be expensive to create, there's no need to create a new instance for every OrderProcessor. Instead, we can use the so-called Singleton lifetime style where we <em>share or reuse</em> a single IOrderShipper instance between multiple OrderProcessor instances.</p> <p>The beauty of this is that we can wait making that decision until we wire up the actual dependencies. If we have implementations of IOrderShipper that are inexpensive to create, we may still decide to create a new instance every time.</p> <p>There may still be a corner case where a shared instance doesn't work for a particular implementation (perhaps because it's not thread-safe). In such cases, we can use Lazy loading to create a LazyOrderShipper like this (for clarity I've omitted making this implementation thread-safe, but that would be trivial to do):</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">LazyOrderShipper</span> : <span style="color: #2b91af">IOrderShipper</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">OrderShipper</span> shipper; &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IOrderShipper Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Ship(<span style="color: #2b91af">Order</span> order) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (<span style="color: blue">this</span>.shipper == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.shipper = <span style="color: blue">new</span> <span style="color: #2b91af">OrderShipper</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.shipper.Ship(order); &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p>Notice that this implementation of IOrderShipper only creates the expensive OrderShipper instance when it needs it.</p> <p>Instead of directly injecting the expensive OrderShipper instance directly into OrderProcessor, we wrap it in the LazyOrderShipper class and inject that instead. The following test proves the point:</p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> OrderProcessorIsFast() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> stopwatch = <span style="color: blue">new</span> <span style="color: #2b91af">Stopwatch</span>(); &nbsp;&nbsp;&nbsp; stopwatch.Start(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> order = <span style="color: blue">new</span> <span style="color: #2b91af">Order</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> validator = <span style="color: blue">new</span> <span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">IOrderValidator</span>&gt;(); &nbsp;&nbsp;&nbsp; validator.Setup(v =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; v.Validate(order)).Returns(<span style="color: blue">false</span>); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> shipper = <span style="color: blue">new</span> <span style="color: #2b91af">LazyOrderShipper</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">OrderProcessor</span>(validator.Object, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shipper); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.Process(order); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; stopwatch.Stop(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.IsTrue(stopwatch.Elapsed &lt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">TimeSpan</span>.FromMilliseconds(777)); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Console</span>.WriteLine(stopwatch.Elapsed); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>This test is <em>significantly faster</em> than 777 milliseconds because the OrderShipper never comes into play. In fact, the stopwatch instance reports that the elapsed time was around 3 ms!</p> <p>The bottom line is that Constructor Injection is <em>not</em> an anti-pattern. On the contrary, it is the <em>most powerful DI pattern</em> available, and you should think twice before deviating from it.</p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="30d5f1ecf81d43e0b135c1251b84ce7b"> <div class="comment-author"><a href="http://www.nerdfurio.us">Bryan Gertonson</a> <a href="#30d5f1ecf81d43e0b135c1251b84ce7b">#</a></div> <div class="comment-content">Thanks for publishing this response. When I read the post yesterday evening, I had a lot of thoughts I was chewing on, and you tackled most of them. I couldn't tell if the &quot;smell&quot; was that the dependency was only sometimes needed, or that the dependency was only sometimes needed, and was expensive to create.<br> <br> It seemed like the need to create a set of factories and have them make calls into the IOC container directly was a bigger smell. And the fact that the OrderShipper would be comming from a factory setup somewhere else, making code maintenance more difficult, and the application more prone to errors.<br> <br> Decorating the implementation with a lazy load implementation is very clever. I hadn't thought of that solution, but the singleton lifecycle had.<br> <br> Well done.</div> <div class="comment-date">2010-01-20 16:50 UTC</div> </div> <div class="comment" id="a49925d874e8459c9aa220cc6c8cf5d7"> <div class="comment-author"><a href="http://haacked.com/">Haacked</a> <a href="#a49925d874e8459c9aa220cc6c8cf5d7">#</a></div> <div class="comment-content">I like this approach, but you've also made a contrived example. Your LazyOrderShipper directly instantiates an OrderShipper. What about the dependencies that OrderShipper might require? What if those dependencies are costly? Is it lazy turtles all the way down?</div> <div class="comment-date">2010-01-20 17:14 UTC</div> </div> <div class="comment" id="f719f75ba07f486fa1c2e1ac8717a0a9"> <div class="comment-author"><a href="Http://jeffreypalermo.com">Jeffrey Palermo</a> <a href="#f719f75ba07f486fa1c2e1ac8717a0a9">#</a></div> <div class="comment-content">Constructor over-injection is the problem. I rather like constructor injection</div> <div class="comment-date">2010-01-20 17:20 UTC</div> </div> <div class="comment" id="4e4ef2cbcbbb45d78319017ad6ed8eba"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#4e4ef2cbcbbb45d78319017ad6ed8eba">#</a></div> <div class="comment-content">Thank you all for your comments.<br> <br> The point raised by Phil Haack is valid, so I've addressed it in <a href="/2010/01/20/EnablingDIforLazyComponents">a second blog post</a>.</div> <div class="comment-date">2010-01-20 18:09 UTC</div> </div> <div class="comment" id="1500ca47aa3743529b2dad7ee9beb502"> <div class="comment-author">SP <a href="#1500ca47aa3743529b2dad7ee9beb502">#</a></div> <div class="comment-content">Maybe I'm missing something, but why isn't LazyOrderShipper thread safe as it stands? Your shipper member variable isn't static.</div> <div class="comment-date">2010-01-20 18:44 UTC</div> </div> <div class="comment" id="6003d1d8ec1847efa16bd18a609dd346"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#6003d1d8ec1847efa16bd18a609dd346">#</a></div> <div class="comment-content">Why isn't LazyOrderShipper thread-safe as it is?<br> <br> When I wrote that, my thinking was that if the Ship method is hit by two or more threads simultaneously, more than one thread may evaluate the member field to null and start creating a new instance of OrderShipper. At least one of those instance will created, but never permanently assigned, since another thread may overwrite the value.<br> <br> In this case, it may not lead to any exceptions (that I can think of), but it's inefficient use of resources.<br> <br> However, every time I post something about threading on this blog, someone will point out my errors, so I'm looking forward to learn which mistake I made this time around :)<br> <br> I'm no threading expert, in case you were wondering :)</div> <div class="comment-date">2010-01-20 19:00 UTC</div> </div> <div class="comment" id="9c049beb60154dc9ac11d36ee4798ada"> <div class="comment-author">SP <a href="#9c049beb60154dc9ac11d36ee4798ada">#</a></div> <div class="comment-content">As yes, your right -- I was thinking the calling code could lock against the object itself, but you would never know when LazyOrderShipper was writing to the property. (I'm not threading guru by any means, either).<br> <br> Ok, back to DI... :-)</div> <div class="comment-date">2010-01-20 19:35 UTC</div> </div> <div class="comment" id="11fe930f8b2548d2969ab4515f5a7b69"> <div class="comment-author">Thomas <a href="#11fe930f8b2548d2969ab4515f5a7b69">#</a></div> <div class="comment-content">Great post as well as your book</div> <div class="comment-date">2010-01-20 20:21 UTC</div> </div> <div class="comment" id="b0792ae98ff44c7ea3e70ac49863739b"> <div class="comment-author"><a href="http://www.aquabirdconsulting.com">Khalid Abuhakmeh</a> <a href="#b0792ae98ff44c7ea3e70ac49863739b">#</a></div> <div class="comment-content">This would be a good solution if the problem wasn't so contrived. I've never run into a scenario where it would take me seven seconds to instantiate an object. It might take me 7 seconds to execute a method, but never instantiate the object itself.<br> <br> My point is: It is like arguing that my unicorn needs a better saddle, and that saddle will let me ride it better.<br> <br> I commend you anyways for your effort.</div> <div class="comment-date">2010-01-22 21:52 UTC</div> </div> <div class="comment" id="ad8531800a9544f389143f90b066c766"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#ad8531800a9544f389143f90b066c766">#</a></div> <div class="comment-content">I agree, but I just couldn't let the claim that Constructor Injection is an anti-pattern go by without reacting.</div> <div class="comment-date">2010-01-22 23:56 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture 1.0 RC1 https://blog.ploeh.dk/2010/01/13/AutoFixture1.0RC1 2010-01-13T22:48:13+00:00 Mark Seemann <div id="post"> <p><a href="http://autofixture.codeplex.com/">AutoFixture</a> 1.0 Release Candidate 1 is now available on the CodePlex site! AutoFixture is now almost ten months old and has seen extensive use internally in <a href="http://www.safewhere.net/">Safewhere</a> during most of this time. It has proven to be very stable, expressive and generally a delight to use.</p> <p>If all goes well, the Release Candidate period will be short. Key users have a chance to veto the this version, but if no-one complains within a week from now, we will promote RC1 to version 1.0.</p> <p>The <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=38665">RC1 release page</a> has more detail about this particular release.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Anonymous Get https://blog.ploeh.dk/2010/01/04/AnonymousGet 2010-01-04T20:53:24+00:00 Mark Seemann <div id="post"> <p>In a <a href="/2009/11/26/AnonymousDo">previous post</a> I described how <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s Do method can let you invoke commands on your <a href="http://xunitpatterns.com/SUT.html">SUT</a> with <a href="http://xunitpatterns.com/Dummy%20Object.html">Dummy</a> values for the parameters you don't care about.</p> <p>The Get method is the equivalent method you can use when the member you are invoking returns a value. In other words: if you want to call a method on your SUT to get a value, but you don't want the hassle of coming up with values you don't care about, you can let the Get method supply those values for you.</p> <p>In today's example I will demonstrate a unit test that verifies the behavior of a custom ASP.NET MVC ModelBinder. If you don't know anything about ASP.NET MVC it doesn't really matter. The point is that a ModelBinder must implement the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.imodelbinder.aspx">IModelBinder</a> interface that defines a single method:</p> <p> <pre><span style="color: blue">object</span> BindModel(<span style="color: #2b91af">ControllerContext</span> controllerContext, &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ModelBindingContext</span> bindingContext);</pre> </p> <p>In many cases we don't care about one or the other of these parameters, but we still need to supply them when unit testing.</p> <p>The example is a bit more complex than some of my other sample code, but once in a while I like to provide you with slightly more realistic AutoFixture examples. Still, it's only 10 lines of code, but it looks like a lot more because I have wrapped several of the lines so that the code is still readable on small screens.</p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> BindModelWillReturnCorrectResult() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; fixture.Customize&lt;<span style="color: #2b91af">ControllerContext</span>&gt;(ob =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ob.OmitAutoProperties()); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> value = fixture.CreateAnonymous(<span style="color: #a31515">"Value"</span>); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> bindingContext = <span style="color: blue">new</span> <span style="color: #2b91af">ModelBindingContext</span>(); &nbsp;&nbsp;&nbsp; bindingContext.ValueProvider = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">ValueProviderResult</span>&gt;(); &nbsp;&nbsp;&nbsp; bindingContext.ValueProvider[<span style="color: #a31515">"MyValue"</span>] = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">ValueProviderResult</span>(value, value, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">CultureInfo</span>.CurrentCulture); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> expectedResult = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: blue">string</span>(value.Reverse().ToArray()); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyModelBinder</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> result = fixture.Get((<span style="color: #2b91af">ControllerContext</span> cc) =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sut.BindModel(cc, bindingContext)); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual(expectedResult, result, <span style="color: #a31515">"BindModel"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p>The first part simply creates the Fixture object and <a href="/2009/09/22/CustomizingAType'sBuilderWithAutoFixture">customizes</a> it to <a href="/2009/07/23/DisablingAutoPropertiesInAutoFixture">disable AutoProperties</a> for all ControllerContext instances (otherwise we would have to set up a lot of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a> for such properties as HttpContext, RequestContext etc.).</p> <p>The next part of the test sets up a ModelBindingContext instance that will be used to exercise the SUT. In this test case, the <em>bindingContext</em> parameter of the BindModel method is important, so I explicitly set that up. On the other hand, I don't care about the <em>controllerContext</em> parameter in this test case, so I ask the Get method to take care of that for me:</p> <p> <pre><span style="color: blue">var</span> result = fixture.Get((<span style="color: #2b91af">ControllerContext</span> cc) =&gt; &nbsp;&nbsp;&nbsp; sut.BindModel(cc, bindingContext));</pre> </p> <p>The Get method creates a Dummy value for the ControllerContext, whereas I can still use the outer variable <em>bindingContext</em> to call the BindModel method. The return value of the BindModel method is returned to the <em>result</em> variable by the Get method.</p> <p>Like the Do methods, the Get methods are generic methods. The one invoked in this example has this signature:</p> <p> <pre><span style="color: blue">public</span> TResult Get&lt;T, TResult&gt;(<span style="color: #2b91af">Func</span>&lt;T, TResult&gt; function);</pre> </p> <p>There are also overloads that works with the versions of the Func delegate that takes two, three and four parameters.</p> <p>As the Do methods, the Get methods are convenience methods that let you concentrate on writing the test code you care about while it takes care of all the rest. You could have written a slightly more complex version that didn't use Get but instead used the CreateAnonymous method to create an Anonymous Variable for the ControllerContext, but this way is slightly more succinct.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. MEF TechTalk with me https://blog.ploeh.dk/2009/12/20/MEFTechTalkwithme 2009-12-20T19:56:33+00:00 Mark Seemann <div id="post"> <p> I'll be doing a TechTalk on the Managed Extensibility Framework and Dependency Injection at Microsoft Denmark January 20th 2010. </p> <p> The talk will be in Danish. <a href="http://msdn.microsoft.com/da-dk/dd796175.aspx">Details and sign-up here</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Wiring ASP.NET MVC Error Handling with Castle Windsor https://blog.ploeh.dk/2009/12/14/WiringASP.NETMVCErrorHandlingwithCastleWindsor 2009-12-14T06:59:32+00:00 Mark Seemann <div id="post"> <p>In my previous posts I discussed <a href="/2009/12/01/GlobalErrorHandlinginASP.NETMVC">how to enable global error handling in ASP.NET MVC</a> and <a href="/2009/12/07/LoggingExceptionFilter">how to inject a logging interface into the error handler</a>. In these posts, I simplified things a bit to get my points across.</p> <p>In production we don't use a custom ErrorHandlingControllerFactory to configure all Controllers with error handling, nor do we instantiate IExceptionFilters manually. What I described was the Poor Man's Dependency Injection (DI) approach, which I find most appropriate when describing DI concepts.</p> <p>However, we really use <a href="http://castleproject.org/container/index.html">Castle Windsor</a> currently, so the wiring looks a bit different although it's still exactly the same thing that's going on. Neither ErrorHandlingActionInvoker nor LoggingExceptionFilter are any different than I have already described, but for completeness I wanted to share a bit of our Windsor code.</p> <p>This is how we really wire our Controllers:</p> <p> <pre style="margin: 0px">container.Register(<span style="color: #2b91af">AllTypes</span> &nbsp;&nbsp;&nbsp; .FromAssemblyContaining(representativeControllerType) &nbsp;&nbsp;&nbsp; .BasedOn&lt;<span style="color: #2b91af">Controller</span>&gt;() &nbsp;&nbsp;&nbsp; .ConfigureFor&lt;<span style="color: #2b91af">Controller</span>&gt;(reg =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reg.LifeStyle.PerWebRequest.ServiceOverrides( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> { ActionInvoker = <span style="color: blue">typeof</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ErrorHandlingControllerActionInvoker</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .FullName })));</pre> </p> <p>Most of this statement simply instructs Windsor to scan all types in the specified assembly for Controller implementations and register them. The interesting part is the ServiceOverrides method call that uses Windsor's rather excentric syntax for defining that the ActionInvoker property should be wired up with an instance of the component registered as ErrorHandlingControllerActionInvoker.</p> <p>Since ErrorHandlingControllerActionInvoker itself expects an IExceptionFilter instance we need to configure at least one of these as well. Instead of one, however, we have two:</p> <p> <pre style="margin: 0px">container.Register(<span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IExceptionFilter</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">LoggingExceptionFilter</span>&gt;()); container.Register(<span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IExceptionFilter</span>&gt;() &nbsp;&nbsp;&nbsp; .ImplementedBy&lt;<span style="color: #2b91af">HandleErrorAttribute</span>&gt;());</pre> </p> <p>This is Windsor's elegant way of registering <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a>: you simply register the Decorator before the decorated type, and it'll Auto-Wire everything for you.</p> <p>Finally, we use a ControllerFactory very similar to the WindsorControllerFactory from the <a href="http://www.codeplex.com/MVCContrib">MVC Contrib</a> project.</p> <p>To reiterate: You don't have to use Castle Windsor to enable global error handling or logging in ASP.NET MVC. You can code it by hand as I've demonstrated in my previous posts, or you can use some other DI Container. The purpose of this post was simply to demonstrate one way to take it to the next level.</p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. LoggingExceptionFilter https://blog.ploeh.dk/2009/12/07/LoggingExceptionFilter 2009-12-07T06:20:27+00:00 Mark Seemann <div id="post"> <p> In a previous post I described <a href="/2009/12/01/GlobalErrorHandlinginASP.NETMVC">how to enable global error handling in ASP.NET MVC applications</a>. Although I spent some time talking about the importance of DRY, another major motivation for me was to enable Dependency Injection (DI) with exception handling so that I could log stack traces before letting ASP.NET MVC handle the exception. </p> <p> With the ErrorHandlingControllerActionInvoker I described, we can inject any IExceptionFilter implementation. As an example I used HandleErrorAttribute, but obviously that doesn't log anything. Once again, it would be tempting to derive from HandleErrorAttribute and override its OnException method, but staying true to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> as well as the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open/Closed Principle</a> I rather prefer a <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</a>: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">LoggingExceptionFilter</span> : <span style="color: #2b91af">IExceptionFilter</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IExceptionFilter</span> filter; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ILogWriter</span> logWriter; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> LoggingExceptionFilter(<span style="color: #2b91af">IExceptionFilter</span> filter, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ILogWriter</span> logWriter) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (filter == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"filter"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (logWriter == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"logWriter"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.filter = filter; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.logWriter = logWriter; &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #region</span> IExceptionFilter Members &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> OnException(<span style="color: #2b91af">ExceptionContext</span> filterContext) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.logWriter.WriteError(filterContext.Exception); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.filter.OnException(filterContext); &nbsp;&nbsp;&nbsp; } &nbsp; <span style="color: blue">&nbsp;&nbsp;&nbsp; #endregion</span> }</pre> </p> <p> The LoggingExceptionFilter shown above is unabridged production code. This is all it takes to bridge the gap between IExceptionFilter and some ILogWriter interface (replace with the logging framework of your choice). Notice how it simply logs the error and then passes on exception handling to the decorated IExceptionFilter. </p> <p> Currently we use HandleErrorAttribute as the decorated filter so that behavior stays as expected.</p> <p> <pre style="margin: 0px">c.ActionInvoker = &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">ErrorHandlingControllerActionInvoker</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">LoggingExceptionFilter</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">HandleErrorAttribute</span>(), logWriter));</pre> </p> <p> This is not too different from before, except that a LoggingExceptionFilter now decorates the HandleErrorAttribute instance. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Building and assigning arrays with AutoFixture https://blog.ploeh.dk/2009/12/05/BuildingandassigningarrayswithAutoFixture 2009-12-05T00:41:45+00:00 Mark Seemann <div id="post"> <p> A reader asked me how <a href="http://autofixture.codeplex.com/">AutoFixture</a> can deal with arrays as fields on a class. More specifically, given a class like MyClassA, how can AutoFixture assign a proper array with initialized values to Items? </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyClassA</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClassB</span>[] Items; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClassC</span> C; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClassD</span> D; }</pre> </p> <p> Ignoring the bad practice of publicly exposing fields, the main problem is that AutoFixture has no inherent understanding of arrays, so if we try to create a new instance of MyClassA by invoking the <a href="/2009/03/24/HowAutoFixtureCreatesObjects">CreateAnonymous</a> method, we would end up with Items being an array of nulls. </p> <p> Obviously we want a populated array instead. There are at least a couple of ways to reach this goal. </p> <p> The simplest is just to create it and modify it afterwards: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> mc = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClassA</span>&gt;(); mc.Items = fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray();</pre> </p> <p> Although the CreateAnomymous method will assign an unitialized array, we immediately overwrite the value with an initialized array. The <a href="/2009/05/11/AnonymousSequencesWithAutoFixture">CreateMany</a> method returns an IEnumerable&lt;MyClassB&gt; on which we can use the ToArray extension method to create an array. </p> <p> The next option is to do almost the same thing, but as a single operation: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> mc = fixture.Build&lt;<span style="color: #2b91af">MyClassA</span>&gt;() &nbsp;&nbsp;&nbsp; .With(x =&gt; x.Items, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray()) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> Besides the different syntax and the lower semicolon count, the biggest difference is that in this case the Items field is never assigned an unitialized array because <a href="/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture">the With method ensures that the specified value is assigned immediately</a>. </p> <p> If you get tired of writing this Builder sequence every time you want to create a new MyClassA instance, you can <a href="/2009/09/22/CustomizingAType'sBuilderWithAutoFixture">Customize the Fixture</a>: </p> <p> <pre style="margin: 0px">fixture.Customize&lt;<span style="color: #2b91af">MyClassA</span>&gt;(ob =&gt; &nbsp;&nbsp;&nbsp; ob.With(x =&gt; x.Items, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateMany&lt;<span style="color: #2b91af">MyClassB</span>&gt;().ToArray()));</pre> </p> <p> With this customization, every time we invoke </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> mc = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClassA</span>&gt;();</pre> </p> <p> we will get an instance of MyClassA with a properly initialized Items array. </p> <p> I hope you find one or more of these methods useful. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="89c48a6a5ec1427c8509a71c51eebe8d"> <div class="comment-author">Murali <a href="#89c48a6a5ec1427c8509a71c51eebe8d">#</a></div> <div class="comment-content">Thanks Mark for your very quick answer to my question. <br> <br> Murali</div> <div class="comment-date">2009-12-07 14:33 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Global Error Handling in ASP.NET MVC https://blog.ploeh.dk/2009/12/01/GlobalErrorHandlinginASP.NETMVC 2009-12-01T06:37:01+00:00 Mark Seemann <div id="post"> <p> ASP.NET MVC comes with an error-handling feature that enables you to show nice error Views to your users instead of the dreaded Yellow Screen of Death. The most prominently described technique involves attributing either you Controller or a single Controller action, like we see in the AccountController created by the Visual Studio project template. </p> <p> <pre>[<span style="color: #2b91af">HandleError</span>] <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">AccountController</span> : <span style="color: #2b91af">Controller</span> { }</pre> </p> <p> That sure is easy, but I don't like it for a number of reasons: </p> <ul> <li> Even though you can derive from HandleErrorAttribute, it's impossible to inject any dependencies into Attributes because they must be fully constructed at compile-time. That makes it really difficult to log errors to an interface. </li> <li> It violates the DRY principle. Although it can be applied at the Controller level, I still must remember to attribute all of my Controllers with the HandleErrorAttribute. </li> </ul> <p> Another approach is to override Controller.OnException. That solves the DI problem, but not the DRY problem. </p> <p> Attentive readers may now point out that I can define a base Controller that implements the proper error handling, and require that all my Controllers derive from this base Controller, but that doesn't satisfy me: </p> <p> First of all, it still violates the DRY principle. Whether I have to remember to apply a [HandleError] attribute or derive from a : MyBaseController amounts to the same thing. I (and all my team members) have to remember this always. It's unnecessary project friction. </p> <p> The second thing that bugs me about this approach is that I really do <em>favor composition over inheritance</em>. Error handling is a <em>cross-cutting concern</em>, so why should all my Controllers have to derive from a base controller to enable this? That is absurd. </p> <p> Fortunately, ASP.NET MVC offers a third way. </p> <p> The key lies in the Controller base class and how it deals with IExceptionFilters (which HandleErrorAttribute implements). When a Controller action is invoked, this actually happens through an IActionInvoker. The default ControllerActionInvoker is responsible for gathering the list of IExceptionFilters, so the first thing we can do is to derive from that and override its GetFilters method: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ErrorHandlingActionInvoker</span> : &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ControllerActionInvoker</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IExceptionFilter</span> filter; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> ErrorHandlingActionInvoker( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IExceptionFilter</span> filter) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (filter == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"filter"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.filter = filter; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: #2b91af">FilterInfo</span> GetFilters( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ControllerContext</span> controllerContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ActionDescriptor</span> actionDescriptor) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> filterInfo = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">base</span>.GetFilters(controllerContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; actionDescriptor); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filterInfo.ExceptionFilters.Add(<span style="color: blue">this</span>.filter); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> filterInfo; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Here I'm simply injecting an IExceptionFilter instance into this class, so I'm not tightly binding it to any particular implementation - it will work with any IExceptionFilter we give it, so it simply serves the purpose of adding the filter at the right stage so that it can deal with otherwise unhandled exceptions. It's pure infrastructure code. </p> <p> Notice that I first invoke base.GetFilters and then add the injected filter to the returned list of filters. This allows the normal ASP.NET MVC IExceptionFilters to attempt to deal with the error first. </p> <p> This ErrorHandlingActionInvoker is only valuable if we can assign it to each and every Controller in the application. This can be done using a custom ControllerFactory: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ErrorHandlingControllerFactory</span> : &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">DefaultControllerFactory</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: #2b91af">IController</span> CreateController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">RequestContext</span> requestContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> controllerName) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> controller = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">base</span>.CreateController(requestContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; controllerName); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> c = controller <span style="color: blue">as</span> <span style="color: #2b91af">Controller</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (c != <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c.ActionInvoker = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">ErrorHandlingActionInvoker</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">HandleErrorAttribute</span>()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> controller; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> Notice that I'm simply deriving from the DefaultControllerFactory and overriding the CreateController method to assign the ErrorHandlingActionInvoker to the created Controller. </p> <p> In this example, I have chosen to inject the HandleErrorAttribute into the ErrorHandlingActionInvoker instance. This seems weird, but it's the HandleErrorAttribute that implements the default error handling logic in ASP.NET MVC, and since it implements IExceptionFilter, it works like a charm. </p> <p> The only thing left to do is to wire up the custom ErrorHandlingControllerFactory in global.asax like this: </p> <p> <pre><span style="color: #2b91af">ControllerBuilder</span>.Current.SetControllerFactory( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">ErrorHandlingControllerFactory</span>());</pre> </p> <p> Now any Controller action that throws an exception is gracefully handled by the injected IExceptionFilter. Since the filter is added as the last in the list of ExceptionFilters, any normal [HandleError] attribute and Controller.OnException override gets a chance to deal with the exception first, so this doesn't even change behavior of existing code. </p> <p> If you are trying this out on your own development machine, remember that you must modify your web.config like so: </p> <p> <pre><span style="color: blue">&lt;</span><span style="color: #a31515">customErrors</span><span style="color: blue"> </span><span style="color: red">mode</span><span style="color: blue">=</span>"<span style="color: blue">On</span>"<span style="color: blue"> /&gt;</span></pre> </p> <p> If you run in the RemoteOnly mode, you will still see the Yellow Screen of Death on your local box. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c447b504e69d4cfbbfad11381033fb3d"> <div class="comment-author"><a href="http://erikzaadi.com">Erik</a> <a href="#c447b504e69d4cfbbfad11381033fb3d">#</a></div> <div class="comment-content">Very nice article..<br> </div> <div class="comment-date">2009-12-04 16:25 UTC</div> </div> <div class="comment" id="af75c1fa728043abb288279f281ba63e"> <div class="comment-author"><a href="http://eduncan911.com">Eric Duncan</a> <a href="#af75c1fa728043abb288279f281ba63e">#</a></div> <div class="comment-content">Excellent! I like this for the same reason of DRY as well! I've implemented this in our project now as well!<br> <br> One question, just to confirm your dependency injection concern: During your CreateController() override, I would have to obtain an instance of ErrorHandlingActionInvoker() from my IoC container, right?<br> <br> Feels kind of dirty doing it there. Well, it would feel even dirtier if i had to reference a property from the controller to get an instance of what I want.<br> <br> Just wondering what you ended up with for your dependency injection pattern of this error handler.<br> <br> </div> <div class="comment-date">2010-01-30 00:23 UTC</div> </div> <div class="comment" id="0f6180e42b8d469ea2fec0938fe19ef9"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#0f6180e42b8d469ea2fec0938fe19ef9">#</a></div> <div class="comment-content">You are correct that in our production applications, we don't manually request the ErrorHandlingActionInvoker from the container and assign it to the property. Rather, we simply configure the container to automatically set the ActionInvoker property as part of wiring up any Controller.<br> <br> <a href="/2009/12/14/WiringASP.NETMVCErrorHandlingwithCastleWindsor">This follow-up post</a> provides an example.</div> <div class="comment-date">2010-01-30 09:27 UTC</div> </div> <div class="comment" id="266b0ae675bb40c4891b9d28673f7db1"> <div class="comment-author"><a href="http://jvance.com">Jarrett Vance</a> <a href="#266b0ae675bb40c4891b9d28673f7db1">#</a></div> <div class="comment-content">I've tried extending the view model to have some of my custom data needed by master page and I keep getting empty result. Any ideas?</div> <div class="comment-date">2010-03-15 21:42 UTC</div> </div> <div class="comment" id="1b0e0e78f8c14364a9af6bfab5de7995"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#1b0e0e78f8c14364a9af6bfab5de7995">#</a></div> <div class="comment-content">I'm not sure I understand how your question relates particularly to this blog post, so I'll need more details than that to be able to say anything meaningful.<br> <br> May I suggest asking your question on <a href="http://stackoverflow.com/">Stack Overflow</a>? That site has good facilities for asking development-related questions, including syntax highlighting. You are welcome to point me to any questions you may have, and I'll take a look and answer if I have anything to add.</div> <div class="comment-date">2010-03-15 22:17 UTC</div> </div> <div class="comment" id="793dbabab45349c086eb7cc6a52181fe"> <div class="comment-author">petrux <a href="#793dbabab45349c086eb7cc6a52181fe">#</a></div> <div class="comment-content">Great post!!! I was wondering... is it possible to handle the authentication issue the same way? I mean: is it possible to handle it dealing with controllers? Or is it a bad practice and shall I deal with this stuff in the service/busines layer?<br> <br> Bye bye and... sorry for my broken English! :-)<br> petrux from Italy</div> <div class="comment-date">2011-03-01 08:56 UTC</div> </div> <div class="comment" id="3d57949f2ba44986a8c2817350a91d43"> <div class="comment-author">RonJ <a href="#3d57949f2ba44986a8c2817350a91d43">#</a></div> <div class="comment-content">Mark, Your code works up to a point. When an exception occurs in a controller's HttpGet method the method SystemError is reached. However, when an exception occurs in a controller's HttpPost method the exception &quot;A public action method 'SystemError' was not found on controller 'Cmrs_Web.Controllers.CalmErrorController'.&quot; is raised on the line of code &quot;httpHandler.ProcessRequest(this.Context);&quot; in global.asa.cs<br> <br> Here is the global.asa.cs code:<br> protected void Application_Error(object sender, EventArgs e)<br> {<br> // Log the exception to CALM<br> Guid errorLogNumber = new Guid();<br> var error = Server.GetLastError();<br> string logMessage = Calm.Logging.Utilities.FormatException(error);<br> Response.Clear();<br> Server.ClearError();<br> if (Logger.IsFatalOn)<br> {<br> errorLogNumber = Logger.Fatal(error);<br> }<br> <br> // Put CALM Error Id into session so CalmError controller can get it for displaying in view<br> Session.Add(&quot;CalmErrorId&quot;, errorLogNumber.ToString());<br> <br> // Display Error View<br> string path = Request.Path;<br> this.Context.RewritePath(&quot;~/CalmError/SystemError&quot;);<br> IHttpHandler httpHandler = new MvcHttpHandler();<br> httpHandler.ProcessRequest(this.Context);<br> this.Context.RewritePath(path, false);<br> }<br> <br> Here is simplified code for the Controller (device is a simple object with 6 properties - forceErr I manually set to false/true then ran a Debug session)<br> [HttpGet]<br> public ActionResult CreateManualAsset()<br> {<br> bool forceErr = false;<br> if (forceErr)<br> {<br> throw new Exception(&quot;forced error in CreateManualAsset post&quot;);<br> }<br> return this.View(&quot;Manual Asset&quot;);<br> }<br> <br> [HttpPost]<br> public ActionResult CreateManualAsset(Device device)<br> {<br> bool forceErr = false;<br> if (forceErr)<br> {<br> throw new Exception(&quot;forced error in CreateManualAsset post&quot;);<br> }<br> return this.View(&quot;Manual Asset&quot;);<br> }</div> <div class="comment-date">2012-07-27 18:53 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Anonymous Do https://blog.ploeh.dk/2009/11/26/AnonymousDo 2009-11-26T21:23:46+00:00 Mark Seemann <div id="post"> <p> In unit testing an important step is to exercise the <a href="http://xunitpatterns.com/SUT.html">SUT</a>. The member you want to invoke is often a method that takes one or more parameters, but in some test cases you don't care about the values of those parameters - you just want to invoke the method. </p> <p> You can always make up one or more <a href="http://xunitpatterns.com/Dummy%20Object.html">Dummy</a> parameters and pass them to the method in question, but you could also use one of <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s Do convenience methods. There are several overloads that all take delegates that specify the action in question while providing you with Dummies of any parameters you don't care about. </p> <p> A good example is WPF's <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx">ICommand</a> interface. The most prevalent method is the Execute method that takes a single <em>parameter</em> parameter: </p> <p> <pre><span style="color: blue">void</span> Execute(<span style="color: blue">object</span> parameter);</pre> </p> <p> Most of the time we don't really care about the parameter because we only care that the command was invoked. However, we still need to supply a value for the parameter when we unit test our ICommand implementations. Obviously, we could just pass in a new System.Object every time, but why not let AutoFixture take care of that for us? </p> <p> (You may think that new'ing up a System.Object is something you can easily do yourself, but imagine other APIs that require much more complex input parameters, and you should begin to see the potential.) </p> <p> Here's a state-based unit test that verifies that the Message property of the MyViewModel class has the correct value after the FooCommand has been invoked: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> FooWillUpdateMessage() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyViewModel</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; fixture.Do((<span style="color: blue">object</span> parameter) =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sut.FooCommand.Execute(parameter)); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual(<span style="color: #a31515">"Foo"</span>, sut.Message, <span style="color: #a31515">"Message"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> Notice how the Do method takes an Action&lt;object&gt; to specify the method to invoke. AutoFixture automatically supplies an instance for the <em>parameter</em> parameter using the same engine to create <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous variables</a> that it uses for everything else. </p> <p> The Do method in question is really a generic method with this signature: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">void</span> Do&lt;T&gt;(<span style="color: #2b91af">Action</span>&lt;T&gt; action);</pre> </p> <p> There are also overloads that take two, three or four input parameters, corresponding to the available Action types available in the BCL. </p> <p> These methods are simply convenience methods that allow you to express your test code more succinctly than you would otherwise have been able to do. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dependency Injection Podcast with me https://blog.ploeh.dk/2009/11/17/DependencyInjectionPodcastwithme 2009-11-17T18:58:47+00:00 Mark Seemann <div id="post"> <p> <a href="http://danielfrost.dk/">Daniel Frost</a> has published a podcast where he discusses Dependency Injection with me. It's about half an hour long and in Danish. <a href="http://danielfrost.dk/post/Frosts-Podcast-Show-7-e28093-Dependency-Injections-med-Mark-Seemann.aspx">Hear it here</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Using Castle Windsor's PerWebRequest lifestyle with ASP.NET MVC on IIS7 https://blog.ploeh.dk/2009/11/17/UsingCastleWindsor'sPerWebRequestlifestylewithASP.NETMVConIIS7 2009-11-17T12:44:37+00:00 Mark Seemann <div id="post"> <p> When using <a href="http://castleproject.org/container/index.html">Castle Windsor</a> in web applications you would want to register many of your components with a lifestyle that is associated with a single request. This is the purpose of the PerWebRequest lifestyle. </p> <p> If you try that with ASP.NET MVC on IIS7, you are likely to receive the following error message: </p> <blockquote> <p> Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule <br>Add '&lt;add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel" /&gt;' to the &lt;httpModules&gt; section on your web.config. </p> </blockquote> <p> Unfortunately, following the instructions in the error message doesn't help. There's a <a href="http://groups.google.com/group/castle-project-users/browse_thread/thread/d44d96f4b548611e/1c33a54539f8abf7">discussion about this issue</a> on the Castle Project Users forum, but the gist of it is that if you don't need to <em>resolve</em> components during application startup, this shouldn't be an issue, and indeed it isn't - it seems to be something else. </p> <p> In my case I seem to have solved the problem by registering the HTTP module in configuration/system.webServer/modules instead of configuration/system.web/httpModules. </p> <p> Although I haven't had the opportunity to dive into the technical details to understand <em>why</em> this works, this seems to solve the problem on both Windows Vista and Windows Server 2008. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ee085cd6864b4e32b00d6d7f2a940436"> <div class="comment-author"><a href="http://eduncan911.com">Eric Duncan</a> <a href="#ee085cd6864b4e32b00d6d7f2a940436">#</a></div> <div class="comment-content">It is because IIS 7 running under Integration Pipeline(the default, and prefered mode) requires all HttpHandlers and HttpModules to be registered under:<br> <br> /configuration/system.webServer/modules <br> <br> It ignores the other HttpHandler and HttpModules defined in system.web.<br> <br> If you have a website running under IIS6, or you have downgraded your IIS7 to run in &quot;Classic .NET&quot; pipeline mode, that's when you use /configuration/system.web/httpModules - as it's the OLD way of adding them.<br> <br> IIS7 will even take your old web.config from IIS6 and 'upgrade it' by adding in the system.webServer/modules and /handlers section, and will copy over the older definitions. That is, if you use the IIS Manager to upgrade it. ;)<br> </div> <div class="comment-date">2010-01-14 22:04 UTC</div> </div> <div class="comment" id="abf10d56bc4d43faaeaea11158aeda79"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#abf10d56bc4d43faaeaea11158aeda79">#</a></div> <div class="comment-content">Thanks for the explanation :)</div> <div class="comment-date">2010-01-14 22:23 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Creating length-constrained strings with AutoFixture https://blog.ploeh.dk/2009/11/07/Creatinglength-constrainedstringswithAutoFixture 2009-11-07T19:56:05+00:00 Mark Seemann <div id="post"> <p> The <a href="http://autofixture.codeplex.com/WorkItem/View.aspx?WorkItemId=2987">following feature suggestion</a> was recently posted in the <a href="http://autofixture.codeplex.com/">AutoFixture</a>&nbsp;<a href="http://autofixture.codeplex.com/WorkItem/List.aspx">Issue Tracker</a> board: </p> <blockquote> <p> "We're using AutoFixture to create random rows of data in our DB. A lot of times though, it creates strings that are too long for the database columns. It would be nice if the .With&lt;string&gt; method had an overload that took in a min/max length. We want the random data, but capped at a limit. </p> <p> "fixture.Build&lt;MyObject&gt;.With(x = x.MyString, 0, 100); </p> <p> "As an aside, this is for a project that uses Nhibernate and Fluent Nhibernate, which has these lengths already defined. I would be nice if AutoFixture could automatically pick up on that somehow." </p> </blockquote> <p> I think such an feature is an excellent idea, but I don't think I will include in AutoFixture. Why not? </p> <p> So far, I have kept the AutoFixture API pretty clean and very generic, and it is my belief that this is one of the main reasons it is so expressive and flexible. There are no methods that only work on specific types (such as strings), and I am reluctant to introduce them now. </p> <p> In the last six months, I have identified a lot of specialized usage idioms that I would love to package into a reusable library, but I think that they will pollute the core AutoFixture API, so I'm going to put those in one or more optional 'add-on' libraries. </p> <p> The ability to define strings that are constrained on length could be one such feature, but rather than wait for a helper library, I will show you have you can implement such a method yourself. </p> <p> The first thing we need is a method that can create anonymous strings given length constraints. One possible implementation is this ConstrainedStringGenerator class: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ConstrainedStringGenerator</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">int</span> minimumLength; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">int</span> maximumLength; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> ConstrainedStringGenerator(<span style="color: blue">int</span> minimumLength, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> maximumLength) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (maximumLength &lt; 0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"..."</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (minimumLength &gt; maximumLength) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"..."</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.minimumLength = minimumLength; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.maximumLength = maximumLength; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">string</span> CreateaAnonymous(<span style="color: blue">string</span> seed) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> s = <span style="color: blue">string</span>.Empty; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">while</span> (s.Length &lt; <span style="color: blue">this</span>.minimumLength) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s += <span style="color: #2b91af">GuidStringGenerator</span>.CreateAnonymous(seed); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (s.Length &gt; <span style="color: blue">this</span>.maximumLength) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s = s.Substring(0, <span style="color: blue">this</span>.maximumLength); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> s; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The CreateAnonymous method uses AutoFixture's GuidStringGenerator class to create anonymous strings of the required length. For this implementation I chose a basic algorithm, but I'm sure you can create one that is more sophisticated if you need it. </p> <p> The next thing we need to do is to implement the desired With method. That can be done with an extension method works on ObjectBuilder&lt;T&gt;: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">ObjectBuilderExtension</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">ObjectBuilder</span>&lt;T&gt; With&lt;T&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span> <span style="color: #2b91af">ObjectBuilder</span>&lt;T&gt; ob, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;T, <span style="color: blue">string</span>&gt;&gt; propertyPicker, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> minimumLength, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> maximumLength) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> me = (<span style="color: #2b91af">MemberExpression</span>)propertyPicker.Body; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> name = me.Member.Name; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> generator = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">ConstrainedStringGenerator</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minimumLength, maximumLength); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> value = generator.CreateaAnonymous(name); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> ob.With(propertyPicker, value); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The method takes the same input as ObjectBuilder&lt;T&gt;'s With method, plus the two integers that constrain the length. Note that the propertyPicker expression has been constrained to deal only with strings. </p> <p> In this implementation, we use the ConstrainedStringGenerator class to generate the desired value for the property, where after we can use the existing With method to assign the value to the property in question. </p> <p> This now allows us to write Build statements like the one originally requested: </p> <p> <pre><span style="color: blue">var</span> mc = fixture.Build&lt;<span style="color: #2b91af">MyClass</span>&gt;() &nbsp;&nbsp;&nbsp; .With(x =&gt; x.SomeText, 0, 100) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> The other part of the request, regarding NHibernate integration, I will leave to the interested reader - mostly because I have never used NHibernate, so I have no clue how to do it. I would, however, love to see a blog post with that addition. </p> <p> This entire example is now part of the AutoFixture test suite, so if you are interested in looking at it in more detail, you can get it from the AutoFixture source code (available at CodePlex). </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="7811bca8456640879a27ff35efa14f51"> <div class="comment-author">Troy <a href="#7811bca8456640879a27ff35efa14f51">#</a></div> <div class="comment-content">I see that you have added a ConstrainedStringGenerator class to AutoFixture now but I haven't found any example of how to use it in its new form. What I'm doing currently is providing an ICustomization implementation for domain data that takes max field lengths into consideration. My first question is, is this the right place to do that? If so, how do I reliably get the ISpecimenContext to pass to the Create method? Right now I am creating a SpecimenContext instance myself but again I'm not sure where I should be getting my ISpecimenBuilder instance to pass to it. The 2 leading candidates right now are the Engine property of a Fixture instance that I pass in the constructor as a ISpecimenBuilder or create a StringGenerator instance. If the StringGenerator is what I should use though what should I pass to it in its constructor? Here is an example in case that isn't clear.<br> <br> fixture.Customize&lt;NwCategory&gt;<br> (<br> x =&gt; x<br> .Without(m =&gt; m.CategoryID)<br> .Without(m =&gt; m.ProductSet)<br> .With(m =&gt; m.CategoryName, (String)stringGenerator.Create(new ConstrainedStringRequest(NwCategory.MaxLength_CategoryName), new SpecimenContext(this.SpecimenBuilder)))<br> );</div> <div class="comment-date">2012-04-26 14:32 UTC</div> </div> <div class="comment" id="fb8ca75d3d234dc2bf9e867fa6cf9cb0"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#fb8ca75d3d234dc2bf9e867fa6cf9cb0">#</a></div> <div class="comment-content">IIRC the ConstrainedStringGenerator class was added to <a href="http://www.nikosbaxevanis.com/bonus-bits/2011/09/stringlengthattribute-support-in-autofixture.html">support the [StringLength] attribute</a>. Does this do what you'd like it to do?<br> <br> There's also a <a href="http://stackoverflow.com/q/10125199/126014">recent discussion about the subject on Stack Overflow</a>.<br> <br> Finally, perhaps you'll find <a href="/2010/10/19/Convention-basedCustomizationswithAutoFixture">this post</a> helpful.<br> <br> If none of these links answer your question, please feel free to ask again. However, Stack Overflow or the AutoFixture discussion list might be better forums, as they are better targeted at Q&amp;A.<br> <br> HTH</div> <div class="comment-date">2012-04-27 18:43 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture beta 1 https://blog.ploeh.dk/2009/10/31/AutoFixturebeta1 2009-10-31T08:59:43+00:00 Mark Seemann <div id="post"> <p> <a href="http://autofixture.codeplex.com/">AutoFixture</a> beta 1 is now available on the CodePlex site! We have been using AutoFixture quite intensively in <a href="http://www.safewhere.net">Safewhere</a> for almost half a year now, and the core of it has turned out to be stable and much more powerful than I originally imagined. </p> <p> During that period, I have discovered and fixed a few bugs, but the most positive experience has been how extended usage has inspired us to come up with numerous ideas to new cool features. Some of these features are already implemented in the current release, and the rest are <a href="http://autofixture.codeplex.com/WorkItem/List.aspx">listed on the AutoFixture site</a>. </p> <p> The <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29097">beta 1 release page</a> has more details about this particular release. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Anonymous With https://blog.ploeh.dk/2009/10/26/AnonymousWith 2009-10-26T20:26:49+00:00 Mark Seemann <div id="post"> <p> A few months ago I described how you can use <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s <a href="/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture">With method</a> to assign property values as part of building up an anonymous variable. In that scenario, the With method is used to explicitly assign a particular, pre-defined value to a property. </p> <p> There's another overload of the With method that <em>doesn't</em> take an explicit value, but rather uses AutoFixture to create an anonymous value for the property. So what's the deal with that if AutoFixture's default behavior is to assign anonymous values to all writable properties? </p> <p> In short it's an opt-in mechanism that only makes sense if you decide to opt out of the AutoProperties features. </p> <p> As always, let's look at an example. This time, I've decided to show you a slightly more realistic example so that you can get an idea of how AutoFixture can be used to aid in unit testing. This also means that the example is going to be a little more complex than usual, but it's still simple. </p> <p> Imagine that we wish to test that an ASP.NET MVC Controller Action returns the correct result. More specifically, we wish to test that the Profile method on MyController returns a ViewResult with the correct Model (the current user's name, to make things interesing). </p> <p> Here's the entire test. It may look more complicated than it is - it's really only 10 lines of code, but I had to break them up to prevent unpleasant wrapping if your screen is narrow. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> ProfileWillReturnResultWithCorrectUserName() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; fixture.Customize&lt;<span style="color: #2b91af">ControllerContext</span>&gt;(ob =&gt; ob &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .OmitAutoProperties() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .With(cc =&gt; cc.HttpContext)); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> expectedUserName = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.CreateAnonymous(<span style="color: #a31515">"UserName"</span>); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> httpCtxStub = <span style="color: blue">new</span> <span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">HttpContextBase</span>&gt;(); &nbsp;&nbsp;&nbsp; httpCtxStub.SetupGet(x =&gt; x.User).Returns( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">GenericPrincipal</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">GenericIdentity</span>(expectedUserName), <span style="color: blue">null</span>)); &nbsp;&nbsp;&nbsp; fixture.Register(httpCtxStub.Object); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.Build&lt;<span style="color: #2b91af">MyController</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .OmitAutoProperties() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .With(c =&gt; c.ControllerContext) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .CreateAnonymous(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ViewResult</span> result = sut.Profile(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> actual = result.ViewData.Model; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual(expectedUserName, actual, <span style="color: #a31515">"User"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> Apart from AutoFixture, I'm also making use of <a href="http://code.google.com/p/moq/">Moq</a> to stub out HttpContextBase. </p> <p> You can see the Anonymous With method in two different places: in the call to <a href="/2009/09/22/CustomizingAType'sBuilderWithAutoFixture">Customize</a> and when the <a href="http://xunitpatterns.com/SUT.html">SUT</a> is being built. In both cases you can see that the call to With follows a call to OmitAutoProperties. In other words: we are telling AutoFixture that we don't want any of the writable properties to be assigned a value <em>except</em> the one we identify. </p> <p> Let me highlight some parts of the test. </p> <p> <pre>fixture.Customize&lt;<span style="color: #2b91af">ControllerContext</span>&gt;(ob =&gt; ob &nbsp;&nbsp;&nbsp; .OmitAutoProperties() &nbsp;&nbsp;&nbsp; .With(cc =&gt; cc.HttpContext));</pre> </p> <p> This line of code instructs AutoFixture to always create a ControllerContext in a particular way: I don't want to use AutoProperties here, because ControllerContext has a lot of writable properties of abstract types, and that would require me to set up a lot of Test Doubles if I had to assign values to all of those. It's much easier to simply opt out of this mechanism. However, I <em>would</em> like to have the HttpContext property assigned, but I don't care about the value in this statement, so the With method simply states that AutoFixture should assign a value according to whatever rule it has for creating instances of HttpContextBase. </p> <p> I can now set up a Stub that populates the User property of HttpContextBase: </p> <p> <pre><span style="color: blue">var</span> httpCtxStub = <span style="color: blue">new</span> <span style="color: #2b91af">Mock</span>&lt;<span style="color: #2b91af">HttpContextBase</span>&gt;(); httpCtxStub.SetupGet(x =&gt; x.User).Returns( &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">GenericPrincipal</span>( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">GenericIdentity</span>(expectedUserName), <span style="color: blue">null</span>)); fixture.Register(httpCtxStub.Object);</pre> </p> <p> This is registered with the fixture instance which closes the loop to the previous customization. </p> <p> I can now create an instance of my SUT. Once more, I don't want to have to set up a lot of irrelevant properties on MyController, so I opt out of AutoProperties and then explicitly opt in on the ControllerContext. This will cause AutoFixture to automatically populate the ControllerContext with the HttpContext Stub: </p> <p> <pre><span style="color: blue">var</span> sut = fixture.Build&lt;<span style="color: #2b91af">MyController</span>&gt;() &nbsp;&nbsp;&nbsp; .OmitAutoProperties() &nbsp;&nbsp;&nbsp; .With(c =&gt; c.ControllerContext) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> For completeness' sake, here's the MyController class that I am testing: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyController</span> : <span style="color: #2b91af">Controller</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">ViewResult</span> Profile() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">object</span> userName = <span style="color: blue">this</span>.User.Identity.Name; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.View(userName); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> This test may seem complex, but it really accomplishes a lot in only 10 lines of code, considering that ASP.NET MVC Controllers with a working HttpContext are not particularly trivial to set up. </p> <p> In summary, this With method overload lets you opt in on one or more explicitly identified properties when you have otherwise decided to omit AutoProperties. As all the other Builder methods, this method can also be chained. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. WCF Podcast with me https://blog.ploeh.dk/2009/10/24/WCFPodcastwithme 2009-10-24T01:52:18+00:00 Mark Seemann <div id="post"> <p> <a href="http://danielfrost.dk/">Daniel Frost</a> has published a podcast where he discusses WCF with me. It's about half an hour and in Danish. <a href="http://danielfrost.dk/post/Frosts-Podcast-Show-4-e28093-Mark-Seemann-om-WCF.aspx">Hear it here</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Writing a book https://blog.ploeh.dk/2009/10/05/Writingabook 2009-10-05T18:13:50+00:00 Mark Seemann <div id="post"> <p> For the last few months I've been writing a book for Manning tentatively titled <em>Dependency Injection in .NET</em>. The <a href="http://www.manning.com/seemann/">page about the book</a> is now live at the <a href="http://www.manning.com/">Manning</a> web site where you can read more about it and, if you would like, purchase an Early Access edition and read the chapters as they are being written. </p> <p> If you have ever wanted to learn about Dependency Injection (DI) related to .NET, here's your chance! </p> <p> At the moment I'm about a third of the way into the book, so there's still some way to go, but I hope to be done with it in 2010. </p> <p> If you decide to purchase an Early Access edition, I'd love to receive your feedback in the online forum. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="6b81b9772c6f47edb7cadec244432a96"> <div class="comment-author"><a href="http://grantpalin.com">Grant Palin</a> <a href="#6b81b9772c6f47edb7cadec244432a96">#</a></div> <div class="comment-content">Ohh, I could have used this book weeks ago! I've been trying to wrap my head around DI/IoC for some time, and in fact am working on a blog post on the subject. It seems complex on the surface, but is less so once you really get into it!<br> <br> Purchased the MEAP, looking forward to the results!</div> <div class="comment-date">2009-10-07 03:01 UTC</div> </div> <div class="comment" id="cec6c496cc40461e87518a59ac44d5bd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#cec6c496cc40461e87518a59ac44d5bd">#</a></div> <div class="comment-content">I hope that you get a lot out of it! If parts of the contents strike you as odd or you think that something is missing, please let me know. The <a href="http://www.manning-sandbox.com/forum.jspa?forumID=607">Author Online</a> forum is the preferred place, but you can drop me a line in any way you prefer.</div> <div class="comment-date">2009-10-13 11:00 UTC</div> </div> <div class="comment" id="4624db46c6ce425688bf06fc5a361cf6"> <div class="comment-author">Bill Campbell <a href="#4624db46c6ce425688bf06fc5a361cf6">#</a></div> <div class="comment-content">Hey Mark,<br> <br> Got the MEAP of your book and am enjoying it. I was wondering if you are going to make any of the sample code from the book available? That would be awesome.<br> <br> thanks,<br> Bill</div> <div class="comment-date">2009-10-21 12:32 UTC</div> </div> <div class="comment" id="d49e6bed8db94a0ca7983fe11284589b"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d49e6bed8db94a0ca7983fe11284589b">#</a></div> <div class="comment-content">Yes, that is definitely the plan. Manning does have the code, but I don't know when they plan on making it available. It's still a little rough around the edges, but if you would like to get it now, send me an email (use the small envelope icon in the Contact section area) and I'll zip it up for you.</div> <div class="comment-date">2009-10-21 12:55 UTC</div> </div> <div class="comment" id="f467d58dc1fb492783bcc0a3c8b7c954"> <div class="comment-author">Scott Peterson <a href="#f467d58dc1fb492783bcc0a3c8b7c954">#</a></div> <div class="comment-content">Hello Mark,<br> <br> While your book is now a couple of years old, I purchased an e-version of it and got a lot out of it. I saw immediate uses for interceptors in an application I am evaluating (for a re-write) at a new company, but I ran into some trouble.<br> In a copy of your ProductManagementClient code, in a MenuItem_Click event, I added code to purposefully throw an InvalidOperationException, one of the exception types listed in the ErrorHandlingInterceptor, just to see the interceptor in action. I then tried to step through the code, but I never got to the Interceptor; I always just see the standard Visual Studio exception dialog in Debug mode.<br> Am I approaching this incorrectly? <br><br> Thanks for all that you do for the .NET community; it has really been a big help to me.<br><br> Scott </div> <div class="comment-date">2013-05-23 18:31 UTC</div> </div> <div class="comment" id="5d5bce5259ed4dd59e4a0924ef04d191"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5d5bce5259ed4dd59e4a0924ef04d191">#</a></div> <div class="comment-content"> <p> Scott, thanks for writing. The Visual Studio behaviour you describe fits my own experience: stepping <em>into</em> something Decorated with a dynamic interceptor doesn't really work. While I'm no expert in how the Visual Studio debugger works, I think it's because there's some auto-generated code (the dynamic interceptor(s)) between the call site and your code on the other side. Visual Studio doesn't have the source code for the dynamic interceptor, so it generally gives up when you try to do that. </p> <p> However, if you set a breakpoint on the other side (behind the interceptors), you will still hit that breakpoint when you press F5. This should enable you to still 'see' the interceptor(s) in action, using the debugger. IIRC, you can also set breakpoints in your own interceptor code, and those should be hit as well. </p> <p> As I wrote, I'm no expert in the Visual Studio debugger, but I do know that it has lots of options you can tweak. I wouldn't be surprised if there was an option that would enable you to skip over the interceptor code and into the code Decorated by the interceptor, but I don't know how to do that. </p> </div> <div class="comment-date">2013-05-23 19:07 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. SOLID or COLDS? https://blog.ploeh.dk/2009/09/29/SOLIDorCOLDS 2009-09-29T19:38:42+00:00 Mark Seemann <div id="post"> <p> The SOLID principles of OOD as <a href="http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">originally put forth by Robert C. Martin</a> make for such a catchy acronym, although they seem to originally have been spelled SOLDI. </p> <p> In any case I've lately been thinking a bit about these principles and it seems to me that the Single Responsibility Principle (SRP) and the Interface Segregation Principle (ISP) seem to be very much related. In essence you could say that the ISP is simply SRP applied to interfaces. </p> <p> The notion underlying both is that a type should deal with only a single concept. Whether that applies to the public API or the internal implementation is less relevant because a corollary to the Liskov Substitution Principle (LSP) and Dependency Inversion Principle (DIP) is that we shouldn't really care about the internals (unless we are actually implementing, that is). </p> <p> The API is what matters. </p> <p> Although I do understand the subtle differences between SRP and ISP I think they are so closely related that one of them is really redundant. We can remove the ISP and still have a fairly good acronym: SOLD (although SOLID is still better). </p> <p> There's one principle that I think is missing from this set: The principle about Command/Query Separation (CQS). In my opinion, this is a very important principle that should be highlighted more than is currently the case. </p> <p> If we add CQS to SOLD, we are left with some less attractive acronyms: </p> <ul> <li>SCOLD</li> <li>COLDS</li> <li>CLODS</li> </ul> <p> Not nearly as confidence-inspiring acronyms as SOLID, but nonetheless, I'm striving to write COLDS code. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Customizing A Type's Builder With AutoFixture https://blog.ploeh.dk/2009/09/22/CustomizingAType'sBuilderWithAutoFixture 2009-09-22T14:53:48+00:00 Mark Seemann <div id="post"> <p> In the <a href="/2009/08/25/DoRedux">previous post</a> on <a href="http://autofixture.codeplex.com/">AutoFixture</a>, I demonstrated how it's possible to use a customized Builder to perform complex initialization when requesting an instance of a particular type. To recap, this was the solution I described: </p> <p> <pre><span style="color: blue">var</span> mc = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;(); <span style="color: blue">var</span> mvm = fixture.Build&lt;<span style="color: #2b91af">MyViewModel</span>&gt;() &nbsp;&nbsp;&nbsp; .Do(x =&gt; x.AvailableItems.Add(mc)) &nbsp;&nbsp;&nbsp; .With(x =&gt; x.SelectedItem, mc) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> This code first creates an anonymous instance of MyClass that can be added to MyViewModel. It then initializes a Builder for a specific instance of MyViewModel, instructing it to </p> <ol> <li>add the anonymous MyClass instance to the list of AvailableItems</li> <li>assign the same instance to the SelectedItem property</li> </ol> <p> While this works splendidly, it can get tiresome to write the same customization over and over again if you need to create multiple instances of the same type. It also violate the DRY principle. </p> <p> When this is the case, you can alternatively register a customized Builder pipeline for the type in question (in this case MyViewModel). This is done with the Customize method: </p> <p> <pre><span style="color: blue">var</span> mc = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;(); fixture.Customize&lt;<span style="color: #2b91af">MyViewModel</span>&gt;(ob =&gt; ob &nbsp;&nbsp;&nbsp; .Do(x =&gt; x.AvailableItems.Add(mc)) &nbsp;&nbsp;&nbsp; .With(x =&gt; x.SelectedItem, mc));</pre> </p> <p> The Customize method takes as input a function that provides an initial ObjectBuilder as input, and returns a new, customized ObjectBuilder as output. This function is registered with the type, so that each time an anonymous instance of the type is requested, the customized ObjectBuilder will be used to create the instance. </p> <p> In the example, I customize the supplied ObjectBuilder (<em>ob</em>) in exactly the same way as before, but instead of invoking CreateAnonymous, I simply return the customized ObjectBuilder to the Fixture instance. It then saves this customized ObjectBuilder for later use. </p> <p> With this customization, what before failed now succeeds: </p> <p> <pre><span style="color: blue">var</span> mvm = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyViewModel</span>&gt;();</pre> </p> <p> The Customize method is the core method for customizing AutoFixture. Most other customization methods (like Register) are simply convenience methods that wraps Customize. It is a very powerful method that can be used to define some very specific Builder algorithms for particular types. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="393f8bbad33f4ee18e61f1e66bcf86f0"> <div class="comment-author">Scott Peterson <a href="#393f8bbad33f4ee18e61f1e66bcf86f0">#</a></div> <div class="comment-content"> <p>I posted this to CodePlex without properly thinking it through, but am now posting here which should be closer to where it belongs.</p> <p>I have the following class that I am trying to test: </p> <!-- HTML generated using hilite.me --> <div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">License</span> { <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">virtual</span> DateTime DatePurchased { <span style="color: #008800; font-weight: bold">get</span>; <span style="color: #008800; font-weight: bold">set</span>; } <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">virtual</span> <span style="color: #333399; font-weight: bold">int</span> LicenseDuration { <span style="color: #008800; font-weight: bold">get</span>; <span style="color: #008800; font-weight: bold">set</span>; } <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">virtual</span> EnumDatePeriod PeriodType { <span style="color: #008800; font-weight: bold">get</span>; <span style="color: #008800; font-weight: bold">set</span>; } <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">virtual</span> <span style="color: #333399; font-weight: bold">bool</span> InUse { <span style="color: #008800; font-weight: bold">get</span>; <span style="color: #008800; font-weight: bold">set</span>; } <span style="color: #008800; font-weight: bold">public</span> DateTime <span style="color: #0066BB; font-weight: bold">ExpirationDate</span>() { ILicenseCalculator calculator = CreateLicenseDurationBuilder(PeriodType); <span style="color: #008800; font-weight: bold">return</span> calculator.CalculateLicenseDuration(DatePurchased, LicenseDuration); } <span style="color: #008800; font-weight: bold">public</span> ILicenseCalculator <span style="color: #0066BB; font-weight: bold">CreateLicenseDurationBuilder</span>(EnumDatePeriod datePeriod) { Dictionary&lt;EnumDatePeriod, ILicenseCalculator&gt; calculators = <span style="color: #008800; font-weight: bold">new</span> Dictionary&lt;EnumDatePeriod, ILicenseCalculator&gt;(); calculators.Add(EnumDatePeriod.Year, <span style="color: #008800; font-weight: bold">new</span> YearLicenseCalculator()); calculators.Add(EnumDatePeriod.Month, <span style="color: #008800; font-weight: bold">new</span> MonthLicenseCalculator()); calculators.Add(EnumDatePeriod.Week, <span style="color: #008800; font-weight: bold">new</span> WeekLicenseCalculator()); calculators.Add(EnumDatePeriod.Day, <span style="color: #008800; font-weight: bold">new</span> DayLicenseCalculator()); <span style="color: #008800; font-weight: bold">return</span> calculators[datePeriod]; } } </pre></div> <p>Specifically, I want to see if I have a license that was purchased more than X time periods ago, the expiration date calculates properly. For example, I have a test that is set up to use a 6 month license, with a purchase date from seven months ago. I am trying to force a certain date, so I got into customizing, and set up my test as follows:</p> <!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #0000CC">[Fact]</span> <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">CanCalculateExpirationDateOnExpiredMonthlyLicense</span>() { Fixture fixture = <span style="color: #008800; font-weight: bold">new</span> Fixture(); fixture.Customize&lt;License&gt;(x =&gt; x .With(b =&gt; b.DatePurchased == DateTime.Now.AddMonths(-<span style="color: #6600EE; font-weight: bold">7</span>))); <span style="color: #333399; font-weight: bold">var</span> sut = fixture.Create&lt;License&gt;(); } </pre></div> <p>I realize that this isn't a complete test, but I ran it just to see what feedback I'd get, to make certain I was customizing properly. Looks like I'm not, because I get an error: </p> <pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>Result Message: System.ArgumentException : The expression's Body is not a MemberExpression. Most likely this is because it does not represent access to a property or field. Parameter name: propertyPicker </code></pre> <p>Where have I gone wrong? </p> <p>Thanks for your help.</p> </div> <div class="comment-date">2015-02-26 21:39 UTC</div> </div> <div class="comment" id="81bad5efb58046d2accdfeed5dcf170a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#81bad5efb58046d2accdfeed5dcf170a">#</a></div> <div class="comment-content"> <p> Scott, thank you for writing. In your expression <code>b.DatePurchased == DateTime.Now.AddMonths(-7)</code>, what would you say the <code>==</code> does? </p> </div> <div class="comment-date">2015-02-27 7:35 UTC</div> </div> <div class="comment" id="a4ebb76a63e1435aafffbde5b1613815"> <div class="comment-author">Scott Peterson <a href="#a4ebb76a63e1435aafffbde5b1613815">#</a></div> <div class="comment-content"> <p> Mark, I really must apologize; my post above was not meant to be published. I worked on this a long time ago and I must have committed it to my local repository. I am very aware of the difference between <code>=</code> and <code>==</code>. This must have been a question I had when I first started looking at AutoFixture (and working with Git). Again, my apologies.<br><br> Scott </p> </div> <div class="comment-date">2015-03-01 19:41 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture .8.6 Released https://blog.ploeh.dk/2009/09/21/AutoFixture.8.6Released 2009-09-21T17:37:36+00:00 Mark Seemann <div id="post"> <p> Yesterday I released version .8.6 of <a href="http://autofixture.codeplex.com/">AutoFixture</a>. It is a minor release that simply adds some new features. </p> <p> There are some minor breaking changes (documented on the <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=33256">release page</a>), but they only affect supporting classes and don't touch on any of the code examples I have so far published. In other words, if you are using AutoFixture's fluent interface, your code should still compile. </p> <p> Please go ahead and download it and use it. As always, comments and questions are welcome, either here or in the <a href="http://autofixture.codeplex.com/Thread/List.aspx">forum</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Self-updating AJAX links with ASP.NET MVC https://blog.ploeh.dk/2009/09/07/Self-updatingAJAXlinkswithASP.NETMVC 2009-09-07T18:14:39+00:00 Mark Seemann <div id="post"> <p> How can you make an AJAX link that updates itself in ASP.NET MVC? My colleague Mikkel and I recently had that problem and we couldn't find any guidance on this topic, so now that we have a solution, I thought I'd share it. </p> <p> The problem is simple: We needed a link that invoked some server side code and updated the text of the link itself based on the result of the operation. Here is a simplified example: </p> <p> <a href="/content/binary/WindowsLiveWriter/SelfupdatingAJAXlinkswithASP.NETMVC_791E/image_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="/content/binary/WindowsLiveWriter/SelfupdatingAJAXlinkswithASP.NETMVC_791E/image_thumb.png" width="267" height="75"></a> </p> <p> Each time you click the link, it should invoke a Controller Action and return a new number that should appear as the link text. </p> <p> This is pretty simple to implement once you know how. The first thing to realize is that the link and all the AJAX stuff must be placed in a user control. The only thing that needs to go into the containing page is the containing element itself: </p> <p> <pre><span style="color: blue">&lt;</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;</span>Self-updating AJAX link<span style="color: blue">&lt;/</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;</span> Click the link to update the number: <span style="color: blue">&lt;</span><span style="color: #a31515">span</span> <span style="color: red">id</span><span style="color: blue">="thespan"&gt;</span> &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> <span style="color: blue">this</span>.Html.RenderPartial(<span style="color: #a31515">"NumberAjaxUserControl"</span>); <span style="background: #ffee62">%&gt;</span> <span style="color: blue">&lt;/</span><span style="color: #a31515">span</span><span style="color: blue">&gt;</span></pre> </p> <p> Notice the <em>id</em> of the span element - this same <em>id</em> will be referenced from the user control. </p> <p> To bootstrap this view, the Controller Action for the page contains code that assigns an initial value to the number (in this case <em>1</em>): </p> <p> <pre><span style="color: blue">public</span> <span style="color: #2b91af">ActionResult</span> Index() { &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.ViewData[<span style="color: #a31515">"number"</span>] = 1.ToString(); &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.View(); }</pre> </p> <p> To keep the example simple, I simply add the number to the ViewData dictionary, but in any production implementation, I would opt to use a strongly typed ViewModel instead. </p> <p> The NumberAjaxUserControl itself only contains the definition of the AJAX link: </p> <p> <pre><span style="background: #ffee62">&lt;%</span><span style="color: blue">@</span> <span style="color: #a31515">Control</span> <span style="color: red">Language</span><span style="color: blue">="C#"</span> <span style="color: red">Inherits</span><span style="color: blue">="System.Web.Mvc.ViewUserControl"</span> <span style="background: #ffee62">%&gt;</span> <span style="background: #ffee62">&lt;%</span><span style="color: blue">@</span> <span style="color: #a31515">Import</span> <span style="color: red">Namespace</span><span style="color: blue">="System.Web.Mvc.Ajax"</span> <span style="background: #ffee62">%&gt;</span> <span style="background: #ffee62">&lt;%</span><span style="color: blue">=</span> <span style="color: blue">this</span>.Ajax.ActionLink((<span style="color: blue">string</span>)<span style="color: blue">this</span>.ViewData[<span style="color: #a31515">"number"</span>], &nbsp;&nbsp;&nbsp; <span style="color: #a31515">"GetNext"</span>, &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> { number = <span style="color: blue">this</span>.ViewData[<span style="color: #a31515">"number"</span>] }, &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">AjaxOptions</span> { UpdateTargetId = <span style="color: #a31515">"thespan"</span> })<span style="background: #ffee62">%&gt;</span></pre> </p> <p> The first parameter to the ActionLink method is simply the current number to render as the link text. Since I'm using the untyped ViewData dictionary for this example, I need to cast it to a string. </p> <p> The next parameter ("GetNext") indicates the Controller Action to invoke when the link is clicked - I will cover that shortly. </p> <p> The third parameter is a Route Value that specifies that the parameter <em>number</em> with the correct value will be supplied to the GetNext Controller Action. It uses the number stored in ViewData. </p> <p> The last parameter indicates the <em>id</em> of the element to update. Recall from before that this name was "thespan". </p> <p> The only missing piece now is the GetNext Controller Action: </p> <p> <pre><span style="color: blue">public</span> <span style="color: #2b91af">PartialViewResult</span> GetNext(<span style="color: blue">int</span> number) { &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.ViewData[<span style="color: #a31515">"number"</span>] = (number + 1).ToString(); &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">this</span>.PartialView(<span style="color: #a31515">"NumberAjaxUserControl"</span>); }</pre> </p> <p> In this example I simply chose to increment the number by one, but I'm sure you can imagine that this method could just as well perform a database lookup or something similar. </p> <p> Notice that the method returns a PartialViewResult that uses the same user control that I used to bootstrap the <em>thespan</em> element. This means that every time the link is clicked, the GetNext method is updated, and the exact same user control is used to render the content that dynamically replaces the original content of the element. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="27aca810b6924481a5d4f0136a395650"> <div class="comment-author">anonymous <a href="#27aca810b6924481a5d4f0136a395650">#</a></div> <div class="comment-content">I kind of prefer the jQuery approach, it is much less intrusive: you render your link as usual and then you use the &quot;live&quot; function:<br> <br> $(&quot;a#some_link&quot;).live('click', function(){ $('#thespan').load(this.href); });<br> <br> This will automatically watch for DOM updates of the anchor tag and if it detects any it will automatically rebind the click function.</div> <div class="comment-date">2009-09-08 07:00 UTC</div> </div> <div class="comment" id="db6d87c942a84cdf821a1634101868b7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#db6d87c942a84cdf821a1634101868b7">#</a></div> <div class="comment-content">Thank you for your comment<br> <br> It looks pretty simple. The following questions are entirely based on my total (and very deplorable) lack of JQuery knowledge:<br> <br> Will this allow me to query the server with particular parameter values (the <i>number</i> parameter in my example)?<br> Will it allow me to update the link with the result from the server?<br> <br> From your snippet, I can't really see where it is specified which URL is requested, what are the query parameters, how the returned result is used to update the link text, etc.</div> <div class="comment-date">2009-09-08 08:16 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture .8.5 Released https://blog.ploeh.dk/2009/09/02/AutoFixture.8.5Released 2009-09-02T20:21:13+00:00 Mark Seemann <div id="post"> <p> It gives me great pleasure to announce that I have just release version .8.5 of <a href="http://autofixture.codeplex.com/">AutoFixture</a>. It is a minor release (hence the numbering) that mainly contains a lot of convenience overloads to already existing methods. There is also a single bug fix. </p> <p> There are two breaking changes (documented on the <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=31587">release page</a>), but they are minor and I do not expect them to cause problems. Only one of these even remotely affects any part of the API I have already discussed here, and that relates to what kind of exception is being thrown <a href="/2009/04/23/DealingWithTypesWithoutPublicConstructors">when AutoFixture is unable to create an instance of the requested type</a>. </p> <p> Please go ahead and download it and use it heavily :) As always, comments and questions are welcome. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Do Redux https://blog.ploeh.dk/2009/08/25/DoRedux 2009-08-25T18:27:39+00:00 Mark Seemann <div id="post"> <p> Soon after I posted <a href="/2009/06/09/CallingMethodsWhileBuildingAnonymousVariablesWithAutoFixture">my post on the AutoFixture Custom Builder's Do method</a>, a much better example occurred to me, so let's revisit this feature in light of a more reasonable context. </p> <p> When I write WPF code, I always use the <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx">MVVM pattern</a>. When I need to create a Master/Detail View, I usually model it so that my View Model has a list of available items, and a property that returns the currently selected item. In this way, I can bind the current Detail View to the currently selected item purely through the View Model. </p> <p> Such a View Model might look like this: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyViewModel</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">MyClass</span>&gt; availableItems; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">MyClass</span> selectedItem; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> MyViewModel() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.availableItems = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">MyClass</span>&gt;(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">ICollection</span>&lt;<span style="color: #2b91af">MyClass</span>&gt; AvailableItems &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">get</span> { <span style="color: blue">return</span> <span style="color: blue">this</span>.availableItems; } &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">MyClass</span> SelectedItem &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">get</span> { <span style="color: blue">return</span> <span style="color: blue">this</span>.selectedItem; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (!<span style="color: blue">this</span>.availableItems.Contains(<span style="color: blue">value</span>)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentException</span>(<span style="color: #a31515">"..."</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.selectedItem = <span style="color: blue">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The main point of interest is that if you attempt to set SelectedItem to an instance that's not contained in the list of available items, an exception will be thrown. That's reasonable behavior, since we want the user to select only from the available items. </p> <p> By default, <a href="http://autofixture.codeplex.com/">AutoFixture</a> works by assigning an Anonymous Value to all writable properties. Since these values are auto-generated, the value AutoFixture is going to assign to SelectedItem will be a new instance of MyClass, and thus not one of the available items. In other words, this will throw an exception: </p> <p> <pre><span style="color: blue">var</span> mvm = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyViewModel</span>&gt;();</pre> </p> <p> There are several solutions to this situation, depending on the scenario. If you need an instance with SelectedItem correctly set to a non-null value, you can use the Do method like this: </p> <p> <pre><span style="color: blue">var</span> mc = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;(); <span style="color: blue">var</span> mvm = fixture.Build&lt;<span style="color: #2b91af">MyViewModel</span>&gt;() &nbsp;&nbsp;&nbsp; .Do(x =&gt; x.AvailableItems.Add(mc)) &nbsp;&nbsp;&nbsp; .With(x =&gt; x.SelectedItem, mc) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> This first creates an anonymous instance of MyClass, adds it to AvailableItems as part of a customized Builder pipeline and subsequently assigns it to SelectedItem. </p> <p> Another option is to skip assigning only the SelectedItem property. This is a good option if you don't need that value in a particular test. You can use the <a href="/2009/08/17/OmittingOnlyCertainPropertiesWithAutoFixture">Without</a> method to do that: </p> <p> <pre><span style="color: blue">var</span> mvm = fixture.Build&lt;<span style="color: #2b91af">MyViewModel</span>&gt;() &nbsp;&nbsp;&nbsp; .Without(s =&gt; s.SelectedItem) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> This will assign a value to all other writable properties of MyViewModel (if it had had any), except the SelectedItem property. In this case, the value of SelectedItem will be null, since it is being ignored. </p> <p> Finally you can simply choose to omit all AutoProperties using the <a href="/2009/07/23/DisablingAutoPropertiesInAutoFixture">OmitAutoProperties</a> method: </p> <p> <pre><span style="color: blue">var</span> mvm = fixture.Build&lt;<span style="color: #2b91af">MyViewModel</span>&gt;() &nbsp;&nbsp;&nbsp; .OmitAutoProperties() &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> In this scenario, only MyViewModel's constructor is being executed, while all writable properties are being ignored. </p> <p> As you can see, AutoFixture offers great flexibility in providing specialized custom Builders that fit almost any situation. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="99255f48283d4a1baa50b413f4168e11"> <div class="comment-author">Murali <a href="#99255f48283d4a1baa50b413f4168e11">#</a></div> <div class="comment-content">Hi Mark,<br> <br> Can you please help in creating the class which has an array of other types. With Autofixture , the array size is default to 2 null tems.<br> <br> For eg:<br> <br> Here is the my class definition:<br> <br> class MyClassA<br> {<br> <br> public MyClassB[] items;<br> public MyClassC c;<br> public MyClassD d; <br> }<br> <br> class MyclassB<br> {<br> public int x;<br> public string y;<br> <br> }<br> when i use Autofixture for creating MyClass c,d are created and b array with 2 items but with each item null instead of intantiated MyclassB objects. How do i get an Myclass with MyclassB array .<br> Thanks for your help in advance. <br> <br> Murali</div> <div class="comment-date">2009-12-04 22:00 UTC</div> </div> <div class="comment" id="de0101b3d3814af2b6a2d7430b25a49f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#de0101b3d3814af2b6a2d7430b25a49f">#</a></div> <div class="comment-content">Hi Murali<br> <br> Thank you for your question! It prompted me to write a new blog post that provides possible solutions to your question: <a href="/2009/12/05/BuildingandassigningarrayswithAutoFixture">Building and assigning arrays with AutoFixture</a>.<br> <br> I hope it answers your question. If not then please write again.</div> <div class="comment-date">2009-12-05 00:45 UTC</div> </div> <div class="comment" id="808713b1515648868f98fb25a4f24bf1"> <div class="comment-author">Simple <a href="#808713b1515648868f98fb25a4f24bf1">#</a></div> <div class="comment-content">Hi Mark,<br> <br> which MVVM Frameworks do you prefer? Or if you dont use Framerworks - which one can you recommend?</div> <div class="comment-date">2012-05-10 13:22 UTC</div> </div> <div class="comment" id="5e5f14e228984f089d0a409dc23685da"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#5e5f14e228984f089d0a409dc23685da">#</a></div> <div class="comment-content">FWIW, I've found <a href="http://knockoutjs.com/">Knockout.js</a> pretty decent so far, but that's probably not what you had in mind?<br> <br> For WPF, I don't think an additional framework is warranted for MVVM; for Silverlight, I have no opinion.</div> <div class="comment-date">2012-05-10 15:02 UTC</div> </div> <div class="comment" id="4b487c9ab60b4550ab424e61faf4de2a"> <div class="comment-author">Simple <a href="#4b487c9ab60b4550ab424e61faf4de2a">#</a></div> <div class="comment-content">Yes you are right - my question was about MVVM Framework for WPF.. )<br> <br> </div> <div class="comment-date">2012-05-10 20:16 UTC</div> </div> <div class="comment" id="62629c4aa2994a5db204fd6069d7ddb8"> <div class="comment-author">Simple <a href="#62629c4aa2994a5db204fd6069d7ddb8">#</a></div> <div class="comment-content">Hello again Mark!<br> <br> Do you use some kind of GUI-Tests? To simulate users cliks etc..<br> <br> </div> <div class="comment-date">2012-05-14 07:09 UTC</div> </div> <div class="comment" id="e825d6c3a16e45698116542300e9bfdd"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e825d6c3a16e45698116542300e9bfdd">#</a></div> <div class="comment-content">No, <a href="http://martinfowler.com/bliki/TestPyramid.html">they're too brittle</a>.</div> <div class="comment-date">2012-05-14 08:23 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Omitting Only Certain Properties With AutoFixture https://blog.ploeh.dk/2009/08/17/OmittingOnlyCertainPropertiesWithAutoFixture 2009-08-17T19:33:40+00:00 Mark Seemann <div id="post"> <p> The default behavior of <a href="http://autofixture.codeplex.com/">AutoFixture</a> is to create an Anonymous Variable by assigning a value to all writable properties of the created instance. This is great in many scenarios, but not so much in others. You can <a href="/2009/07/23/DisablingAutoPropertiesInAutoFixture">disable this behavior by using the OmitAutoProperties method</a>, but sometimes, you <em>would</em> like most of the writable properties set, except one or two. </p> <p>Consider this simple Person class:</p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Person</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">Person</span> spouse; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">DateTime</span> BirthDay { <span style="color: blue">get</span>; <span style="color: blue">set</span>; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: #2b91af">Person</span> Spouse &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">get</span> { <span style="color: blue">return</span> <span style="color: blue">this</span>.spouse; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.spouse = <span style="color: blue">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (<span style="color: blue">value</span> != <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">value</span>.spouse = <span style="color: blue">this</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The main trouble with this class, seen from AutoFixture's perspective, is the circular reference exposed by the Spouse property. When AutoFixture attempts to create an anonymous instance of Person, it will create anonymous values for all writable properties, and that includes the Spouse property, so it attempts to create a new instance of the Person class and assign values to all public properties, including the Spouse property, etc. </p> <p> In other words, this line of code throws a StackOverflowException: </p> <p> <pre><span style="color: blue">var</span> person = fixture.CreateAnonymous&lt;<span style="color: #2b91af">Person</span>&gt;();</pre> </p> <p> If you would still like to have anonymous values assigned to Name and BirthDay, you can use the Without method: </p> <p> <pre><span style="color: blue">var</span> person = fixture.Build&lt;<span style="color: #2b91af">Person</span>&gt;() &nbsp;&nbsp;&nbsp; .Without(p =&gt; p.Spouse) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> This will give you an anonymous instance of the Person class, but with the Spouse property still with its default value of null. </p> <p> Several calls to Without can be chained if you want to skip more than one property. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A Fluent Interface For Testing INotifyPropertyChanged https://blog.ploeh.dk/2009/08/06/AFluentInterfaceForTestingINotifyPropertyChanged 2009-08-06T17:58:36+00:00 Mark Seemann <div id="post"> <p> If you are doing Rich UI, <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx">INotifyPropertyChanged</a> is a pretty important interface. This is as true for WPF as it was for Windows Forms. Consisting solely of an event, it's <a href="http://blogs.msdn.com/ploeh/archive/2006/01/19/TestingEventsUsingAnonymousMethods.aspx">not any harder to unit test than other events</a>. </p> <p> You can certainly write each test manually like the following. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> ChangingMyPropertyWillRaiseNotifyEvent_Classic() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">bool</span> eventWasRaised = <span style="color: blue">false</span>; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; sut.PropertyChanged += (sender, e) =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (e.PropertyName == <span style="color: #a31515">"MyProperty"</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eventWasRaised = <span style="color: blue">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; sut.MyProperty = <span style="color: #a31515">"Some new value"</span>; &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.IsTrue(eventWasRaised, <span style="color: #a31515">"Event was raised"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> Even for a one-off test, this one has a few problems. From an <a href="http://xunitpatterns.com/"><em>xUnit Test Patterns</em></a> point of view, there's the issue that the test contains conditional logic, but that aside, the main problem is that if you have a lot of properties, writing all these very similar tests become old hat very soon. </p> <p> To make testing INotifyPropertyChanged events easier, I created a simple fluent interface that allows me to write the same test like this: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> ChangingMyPropertyWillRaiseNotifyEvent_Fluent() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system and verify outcome</span> &nbsp;&nbsp;&nbsp; sut.ShouldNotifyOn(s =&gt; s.MyProperty) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .When(s =&gt; s.MyProperty = <span style="color: #a31515">"Some new value"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> You simply state for which property you want to verify the event when a certain operation is invoked. This is certainly more concise and intention-revealing than the previous test. </p> <p> If you have interdependent properties, you can specify than an event was raised when another property was modified. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> ChangingMyPropertyWillRaiseNotifyForDerived() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system and verify outcome</span> &nbsp;&nbsp;&nbsp; sut.ShouldNotifyOn(s =&gt; s.MyDerivedProperty) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .When(s =&gt; s.MyProperty = <span style="color: #a31515">"Some new value"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> The When method takes any Action&lt;T&gt;, so you can also invoke methods, use Closures and what not. </p> <p> There's also a <em>ShouldNotNotifyOn</em> method to verify that an event was <em>not</em> raised when a particular operation was invoked. </p> <p> This fluent interface is implemented with an extension method on INotifyPropertyChanged, combined with a custom class that performs the verification. Here are the extension methods: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">NotifyPropertyChanged</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShouldNotifyOn&lt;T, TProperty&gt;(<span style="color: blue">this</span> T owner, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;T, TProperty&gt;&gt; propertyPicker) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">where</span> T : <span style="color: #2b91af">INotifyPropertyChanged</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: #2b91af">NotifyPropertyChanged</span>.CreateExpectation(owner, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; propertyPicker, <span style="color: blue">true</span>); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShouldNotNotifyOn&lt;T, TProperty&gt;(<span style="color: blue">this</span> T owner, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;T, TProperty&gt;&gt; propertyPicker) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">where</span> T : <span style="color: #2b91af">INotifyPropertyChanged</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: #2b91af">NotifyPropertyChanged</span>.CreateExpectation(owner, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; propertyPicker, <span style="color: blue">false</span>); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CreateExpectation&lt;T, TProperty&gt;(T owner, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;T, TProperty&gt;&gt; pickProperty, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">bool</span> eventExpected) <span style="color: blue">where</span> T : <span style="color: #2b91af">INotifyPropertyChanged</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> propertyName = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((<span style="color: #2b91af">MemberExpression</span>)pickProperty.Body).Member.Name; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt;(owner, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; propertyName, eventExpected); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> And here's the NotifyExpectation class returned by both extension methods: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">NotifyExpectation</span>&lt;T&gt; &nbsp;&nbsp;&nbsp; <span style="color: blue">where</span> T : <span style="color: #2b91af">INotifyPropertyChanged</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> T owner; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">string</span> propertyName; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">bool</span> eventExpected; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> NotifyExpectation(T owner, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> propertyName, <span style="color: blue">bool</span> eventExpected) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.owner = owner; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.propertyName = propertyName; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.eventExpected = eventExpected; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> When(<span style="color: #2b91af">Action</span>&lt;T&gt; action) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">bool</span> eventWasRaised = <span style="color: blue">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.owner.PropertyChanged += (sender, e) =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (e.PropertyName == <span style="color: blue">this</span>.propertyName) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eventWasRaised = <span style="color: blue">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; action(<span style="color: blue">this</span>.owner); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">bool</span>&gt;(<span style="color: blue">this</span>.eventExpected, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eventWasRaised, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"PropertyChanged on {0}"</span>, <span style="color: blue">this</span>.propertyName); &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> You can replace the Assertion with one that matches your test framework of choice (this one was written for MSTest). </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="f82e4f32be7945319cd0e73bebdbe699"> <div class="comment-author">PeteB <a href="#f82e4f32be7945319cd0e73bebdbe699">#</a></div> <div class="comment-content">I'm never played with modifying/creating a fluent interface; how can this be extended to check for multiple NotifyProperty events (and NOT events)?<br> <br> e.g.<br> sut.ShouldNotifyOn(s =&gt; s.MyProperty).AndOn(s =&gt; s.MyDependentProperty).AndNotOn(s =&gt; s.MyIndependentProperty)<br> .When(s =&gt; s.MyProperty = &quot;Some new value&quot;);<br> </div> <div class="comment-date">2012-08-06 11:39 UTC</div> </div> <div class="comment" id="7d6fba18ed8e49fea8b5f4dd9fe0a2ec"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7d6fba18ed8e49fea8b5f4dd9fe0a2ec">#</a></div> <div class="comment-content">Would that add more value than three individual tests?</div> <div class="comment-date">2012-08-06 11:52 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Disabling AutoProperties In AutoFixture https://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture 2009-07-23T14:54:45+00:00 Mark Seemann <div id="post"> <p> Since <a href="http://autofixture.codeplex.com/">AutoFixture</a> <a href="/2009/07/01/AutoFixtureAsTestDataBuilder">is a Test Data Builder</a>, one of its most important tasks is to build up graphs of fully populated, yet semantically correct, strongly typed objects. As such, its default behavior is to assign a value to every writable property in the object graph. </p> <p> While this is sometimes the desired behavior, at other times it is not. </p> <p> This is particularly the case when you want to test that a newly created object has a property of a particular value. When you want to test the default value of a writable property, AutoFixture's AutoProperty feature is very much in the way. </p> <p> Let's consider as an example a piece of software that deals with vehicle registration. By default, a vehicle should have four wheels, since this is the most common occurrence. Although I always practice TDD, I'll start by showing you the Vehicle class to illustrate what I mean. </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Vehicle</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> Vehicle() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Wheels = 4; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">int</span> Wheels { <span style="color: blue">get</span>; <span style="color: blue">set</span>; } }</pre> </p> <p> Here's a test that ensures that the default number of wheels is <em>4</em> - or does it? </p> <p> In fact the assertion fails because the actual value is <em>1</em>, not <em>4</em>. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> AnonymousVehicleHasWheelsAssignedByFixture() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">Vehicle</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> result = sut.Wheels; &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(4, result, <span style="color: #a31515">"Wheels"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> Why does the test fail when the value of Wheels is set to <em>4</em> in the constructor? It fails because AutoFixture is designed to create <em>populated</em> test data, so it assigns a value to every writable property. Wheels is a writable property, so AutoFixture assigns an integer value to it using its <a href="/2009/04/03/CreatingNumbersWithAutoFixture">default algorithm for creating anonymous numbers</a>. Since no other numbers are being created during this test, the number assigned to Wheels is <em>1</em>. This is AutoFixture's AutoProperties feature in effect. </p> <p> When you want to test constructor logic, or otherwise wish to disable the AutoProperties feature, you can use a <a href="/2009/05/26/TheAutoFixtureBuilder">customized Builder</a> with the OmitAutoProperties method: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> VehicleWithoutAutoPropertiesWillHaveFourWheels() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sut = fixture.Build&lt;<span style="color: #2b91af">Vehicle</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .OmitAutoProperties() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .CreateAnonymous(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> result = sut.Wheels; &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(4, result, <span style="color: #a31515">"Wheels"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> The OmitAutoProperties method instructs AutoFixture to skip assigning automatic Anonymous Values to all writable properties in the object graph. Any properties specifically assigned by the <a href="/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture">With method</a> will still be assigned. </p> <p> The test using OmitAutoProperties succeeds. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. DataTemplating In ASP.NET MVC https://blog.ploeh.dk/2009/07/16/DataTemplatingInASP.NETMVC 2009-07-16T19:33:48+00:00 Mark Seemann <div id="post"> <p> The expressiveness of WPF is amazing. I particularly like the databinding and templating features. The ability to selectively render an object based on its type is very strong. </p> <p> When I recently began working with ASP.NET MVC (which I like so far), I quickly ran into a scenario where I would have liked to have WPF's DataTemplates at my disposal. Maybe it's just because I've become used to WPF, but I missed the feature and set out to find out if something similar is possible in ASP.NET MVC. </p> <p> Before we dive into that, I'd like to present the 'problem' in WPF terms, but the underlying View Model that I want to expose will be shared between both solutions. </p> <p> <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="PresentationModel" border="0" alt="PresentationModel" src="/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/PresentationModel_3.png" width="619" height="629"> </p> <p> The main point is that the <em>Items</em> property exposes a polymorphic list. While all items in this list share a common property (Name), they are otherwise different; one contains a piece of Text, one contains a Color, and one is a complex item that contains child items. </p> <p> When I render this list, I want each item to render according to its type. </p> <p> In WPF, this is fairly easy to accomplish with DataTemplates: </p> <p> <pre><span style="color: blue">&lt;</span><span style="color: #a31515">ListBox.Resources</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: red"> DataType</span><span style="color: blue">="{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type</span><span style="color: red"> pm</span><span style="color: blue">:</span><span style="color: red">NamedTextItem</span><span style="color: blue">}"&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">TextBlock</span><span style="color: red"> Text</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Name}"</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> FontWeight</span><span style="color: blue">="bold" /&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">TextBlock</span><span style="color: red"> Text</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Text}" /&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: red"> DataType</span><span style="color: blue">="{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type</span><span style="color: red"> pm</span><span style="color: blue">:</span><span style="color: red">NamedColorItem</span><span style="color: blue">}"&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">TextBlock</span><span style="color: red"> Text</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Name}"</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> FontWeight</span><span style="color: blue">="bold" /&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">Ellipse</span><span style="color: red"> Height</span><span style="color: blue">="25"</span><span style="color: red"> Width</span><span style="color: blue">="25"</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> HorizontalAlignment</span><span style="color: blue">="Left"</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> Fill</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Brush}"</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> Stroke</span><span style="color: blue">="DarkGray" /&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">DataTemplate</span><span style="color: red"> DataType</span><span style="color: blue">="{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type</span><span style="color: red"> pm</span><span style="color: blue">:</span><span style="color: red">NamedComplexItem</span><span style="color: blue">}"&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">TextBlock</span><span style="color: red"> Text</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Name}"</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> FontWeight</span><span style="color: blue">="bold" /&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">ListBox</span><span style="color: red"> ItemsSource</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Children}"</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> BorderThickness</span><span style="color: blue">="0"&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">ListBox.Resources</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">DataTemplate</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> DataType</span><span style="color: blue">="{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type</span><span style="color: red"> pm</span><span style="color: blue">:</span><span style="color: red">ChildItem</span><span style="color: blue">}"&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;</span><span style="color: #a31515">TextBlock</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: red"> Text</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Text}" /&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">ListBox.Resources</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">ListBox</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">StackPanel</span><span style="color: blue">&gt;</span> <span style="color: #a31515">&nbsp;&nbsp;&nbsp; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">DataTemplate</span><span style="color: blue">&gt;</span> <span style="color: blue">&lt;/</span><span style="color: #a31515">ListBox.Resources</span><span style="color: blue">&gt;</span></pre> </p> <p> Each DataTemplate is contained within a ListBox. When the ListBox binds to each item in the <em>Items</em> list, it automatically picks the correct template for the item. </p> <p> The result is something like this: </p> <p> <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WpfApp" border="0" alt="WpfApp" src="/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/WpfApp_5.png" width="167" height="333"> </p> <p> The NamedTextItem is rendered as a box containing the Name and the Text on two separate lines; the NamedColorItem is rendered as a box containing the Name and a circle filled with the Color defined by the item; and the NamedComplexItem is rendered as a box with the Name and each child of the Children list. </p> <p> This is all implemented declaratively without a single line of imperative UI code. </p> <p> Is it possible to do the same in ASP.NET MVC? </p> <p> To my knowledge (but please correct me if I'm wrong), ASP.NET MVC has no explicit concept of a DataTemplate, so we will have to mimic it. The following describes the best I've been able to come up with so far. </p> <p> In ASP.NET MVC, there's no declarative databinding, so we will need to loop through the list of items. My View page derives from ViewPage&lt;MyViewModel&gt;, so I can write </p> <p> <pre><span style="background: #ffee62">&lt;%</span> <span style="color: blue">foreach</span> (<span style="color: blue">var</span> item <span style="color: blue">in</span> <span style="color: blue">this</span>.Model.Items) &nbsp;&nbsp; { <span style="background: #ffee62">%&gt;</span> &nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">div</span> <span style="color: red">class</span><span style="color: blue">="ploeh"&gt;</span> &nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> <span style="color: green">// Render each item </span><span style="background: #ffee62">%&gt;</span> &nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span> <span style="background: #ffee62">&lt;%</span> } <span style="background: #ffee62">%&gt;</span></pre> </p> <p>The challenge is to figure out how to render each item according to its own template.</p> <p>To define the templates, I create a UserControl for each item. The NamedTextItemUserControl derives from ViewUserControl&lt;NamedTextItem&gt;, which gives me a strongly typed Model:</p> <p> <pre><span style="color: blue">&lt;</span><span style="color: #a31515">div</span><span style="color: blue">&gt;&lt;</span><span style="color: #a31515">strong</span><span style="color: blue">&gt;</span><span style="background: #ffee62">&lt;%</span><span style="color: blue">=</span> <span style="color: blue">this</span>.Model.Name <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">strong</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span> <span style="color: blue">&lt;</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span><span style="background: #ffee62">&lt;%</span><span style="color: blue">=</span> <span style="color: blue">this</span>.Model.Text <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre> </p> <p> The other two UserControls are implemented similarly. </p> <p> A UserControl can be rendered using the RenderPartial extension method, so the only thing left is to select the correct UserControl name for each item. It would be nice to be able to do this in markup, like WPF, but I'm not aware of any way that is possible. </p> <p> I will have to resort to code, but we can at least strive for code that is as declarative in style as possible. </p> <p> First, I need to define the map from type to UserControl: </p> <p> <pre><span style="background: #ffee62">&lt;%</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> dataTemplates = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: blue">string</span>&gt;(); &nbsp;&nbsp;&nbsp; dataTemplates[<span style="color: blue">typeof</span>(<span style="color: #2b91af">NamedTextItem</span>)] = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"NamedTextItemUserControl"</span>; &nbsp;&nbsp;&nbsp; dataTemplates[<span style="color: blue">typeof</span>(<span style="color: #2b91af">NamedColorItem</span>)] = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"NamedColorItemUserControl"</span>; &nbsp;&nbsp;&nbsp; dataTemplates[<span style="color: blue">typeof</span>(<span style="color: #2b91af">NamedComplexItem</span>)] = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"NamedComplexItemUserControl"</span>; <span style="background: #ffee62">%&gt;</span></pre> </p> <p> Next, I can use this map to render each item correctly: </p> <p> <pre><span style="background: #ffee62">&lt;%</span> <span style="color: blue">foreach</span> (<span style="color: blue">var</span> item <span style="color: blue">in</span> <span style="color: blue">this</span>.Model.Items) &nbsp;&nbsp; { <span style="background: #ffee62">%&gt;</span> &nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">div</span> <span style="color: red">class</span><span style="color: blue">="ploeh"&gt;</span> &nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> <span style="color: green">// Render each item </span><span style="background: #ffee62">%&gt;</span> &nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> <span style="color: blue">this</span>.Html.RenderPartial(dataTemplates[item.GetType()], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; item); <span style="background: #ffee62">%&gt;</span> &nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span> <span style="background: #ffee62">&lt;%</span> } <span style="background: #ffee62">%&gt;</span></pre> </p> <p> This is definitely less pretty than with WPF, but if you overlook the aesthetics and focus on the structure of the code, it's darn close to markup. The Cyclomatic Complexity of the page is only <em>2</em>, and that's even because of the <em>foreach</em> statement that we need in any case. </p> <p> The resulting page looks like this: </p> <p> <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="AspNetMvcApp" border="0" alt="AspNetMvcApp" src="/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/AspNetMvcApp_1.png" width="156" height="246"> </p> <p> My HTML skills aren't good enough to draw circles with markup, so I had to replace them with blocks, but apart from that, the result is pretty much the same. </p> <p> A potential improvement on this technique could be to embed the knowledge of the UserControl into each item. ASP.NET MVC Controllers already know of Views in an abstract sense, so letting the View Model know about a UserControl (identified as a string) may be conceptually sound. </p> <p> The advantage would be that we could get rid of the Dictionary in the ViewPage and instead let the item itself tell us the name of the UserControl that should be used to render it. </p> <p> The disadvantage would be that we lose some flexibility. It would then require a recompilation of the application if we wanted to render an item using a different UserControl. </p> <p> The technique outlined here represents an explorative work in progress, so comments are welcome. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4ff68661171a42d9994b66845f146129"> <div class="comment-author">David C <a href="#4ff68661171a42d9994b66845f146129">#</a></div> <div class="comment-content">In MVC, data templates are really either Editor or Display templates. In your Shared Views Folder, you can create an EditorTemplates folder and a DisplayTemplates folder. These are snippets which can be either ASP or Razor syntax. By default they run on convention. So if you create a String.cshtml, then anytime you do a Html.EditorFor(x=&gt;x.SomeModelVariableThatIsAString) it will automatically use your template for it. If you only need to display it, not edit it, you can do Html.DisplayFor(x=&gt;x.SomeModelVariableThatIsAString). Assuming you have a String.cshtml in your DisplayTemplates folder as well. You can also optionally specify a specific template, such as Html.DisplayFor(x=&gt;x.SomeModelVariableThatIsAString,&quot;PrettyListItem&quot;), and it will look for PrettyListItem.cshtml as the template to render your &lt;li&gt;. Generally you would simply add a css class name to the markup in that instance of the template &lt;li class=&quot;purty&quot;&gt;, and do all your styling in CSS, which is of course optimal for Web development.<br> <br> Here is a fairly detailed introduction to using Templates in MVC. It is a bit old but the concepts still hold.<br> http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html<br> <br> BTW: Your custom implementation was pretty tight, not bad at all :)</div> <div class="comment-date">2012-09-05 19:38 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture .8.4 And Roadmap https://blog.ploeh.dk/2009/07/11/AutoFixture.8.4AndRoadmap 2009-07-11T20:35:34+00:00 Mark Seemann <div id="post"> <p> A couple of days ago I released <a href="http://autofixture.codeplex.com/">AutoFixture</a> .8.4. It contains a few small feature additions and a bug fix. You can get it at the <a href="http://autofixture.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29096">AutoFixture CodePlex site</a> as usual. </p> <p> You may have noticed that I'm frequently releasing incremental versions these days. This is because we have begun using AutoFixture for writing production software at <a href="http://www.safewhere.net/">Safewhere</a>. So far, it has been a pleasure to use AutoFixture in my daily work, and watch colleagues pick it up and run with it. </p> <p> Such intensive usage obviously uncovers missing features as well as a few bugs, which is the driving force behind the frequent releases. I've been happy to observe that so far, there have been only a few bugs, and the general API is very expressive and useful. </p> <p> After we ship the first product written with AutoFixture, I plan to upgrade it to version .9 (beta). That should hopefully happen in the autumn of 2009. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture As Test Data Builder https://blog.ploeh.dk/2009/07/01/AutoFixtureAsTestDataBuilder 2009-07-01T06:19:00+00:00 Mark Seemann <div id="post"> <p> Some time ago, my good friend Martin Gildenpfennig from <a href="http://www.ative.dk/">Ative</a> made me aware that <a href="http://autofixture.codeplex.com/">AutoFixture</a> (among other things) is an generic implementation of the <a href="http://www.natpryce.com/articles/000714.html">Test Data Builder</a> pattern, and indeed it is. </p> <p> In the original Gang of Four definition of a Design Pattern, several people must independently have arrived at the same general solution to a given problem before we can call a coding idiom a true Pattern. In this spirit, the Test Data Builder pattern is on the verge of becoming a true Design Pattern, since I came up with AutoFixture without having heard about Test Data Builder :) </p> <p> The problem is the same: How do we create semantically correct test objects in a manner that is flexible and yet hides away irrelevant complexity? </p> <p> Like others before me, I tried the Object Mother (anti?)pattern and found it lacking. To me the answer was AutoFixture, a library that heuristically builds object graphs using Reflection and built-in rules for specific types. </p> <p> Although my original approach was different, you can certainly use AutoFixture as a generic Test Data Builder. </p> <p> To demonstrate this, let's work with this (oversimplified) Order object model: </p> <p> <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="/content/binary/WindowsLiveWriter/AutoFixtureAsTestDataBuilder_1371F/image_3.png" width="478" height="457"> </p> <p> Assuming that we have an instance of the Fixture class (called <em>fixture</em>), we can create a new instance of the Order class with a ShippingAddress in Denmark: </p> <p> <pre><span style="color: blue">var</span> order = fixture.Build&lt;<span style="color: #2b91af">Order</span>&gt;() &nbsp;&nbsp;&nbsp; .With(o =&gt; o.ShippingAddress, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.Build&lt;<span style="color: #2b91af">Address</span>&gt;() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .With(a =&gt; a.Country, <span style="color: #a31515">"Denmark"</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .CreateAnonymous()) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> While this works and follows the Test Data Builder pattern, I find this more concise and readable: </p> <p> <pre><span style="color: blue">var</span> order = fixture.CreateAnonymous&lt;<span style="color: #2b91af">Order</span>&gt;(); order.ShippingAddress.Country = <span style="color: #a31515">"Denmark"</span>;</pre> </p> <p> The result is the same. </p> <p> We can also add anonymous order lines to the order using this fluent interface: </p> <p> <pre><span style="color: blue">var</span> order = fixture.Build&lt;<span style="color: #2b91af">Order</span>&gt;() &nbsp;&nbsp;&nbsp; .Do(o =&gt; fixture.AddManyTo(o.OrderLines)) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> but again, I find it easier to simply let AutoFixture create a fully anonymous Order instance, and then afterwards modify the relevant parts: </p> <p> <pre><span style="color: blue">var</span> order = fixture.CreateAnonymous&lt;<span style="color: #2b91af">Order</span>&gt;(); fixture.AddManyTo(order.OrderLines);</pre> </p> <p> Whether you prefer one style over the other, AutoFixture supports them both. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Custom Tokens Over Non-HTTP Transports https://blog.ploeh.dk/2009/06/22/CustomTokensOverNon-HTTPTransports 2009-06-22T19:48:42+00:00 Mark Seemann <div id="post"> <p> About a year ago, one of my readers <a href="http://blogs.msdn.com/ploeh/archive/2008/05/01/DeclarativeUseOfCustomSecurityTokenParameters.aspx#8575741">asked me about how to make custom tokens work over TPC</a> in WCF. Here's the question again: </p> <p> "i'm trying to implement the CustomToken over tcp. The original used the SymmetricSecurityBindingElement and the transport http, this works fine, but when i change URI's and and the transport, it gives an error saying: </p> <p> "Binding 'CustomBinding' doesn't support creating any channel types. This often indicates that the BindingElements in a CustomBinding have been stacked incorrectly or in the wrong order. A Transport is required at the bottom of the stack. The recommended order for BindingElements is: TransactionFlow, ReliableSession, Security, CompositeDuplex, OneWay, StreamSecurity, MessageEncoding, Transport." </p> <p> As it turns out, this seems to be a general issue with more transports than just TCP - at least, I've seen the exact same behavior for the Named Pipes transport. </p> <p> When I originally received the question, it seemed that no-one knew the answer, and neither did I. Now, about a year later, I've managed to find a solution, and it's really simple. </p> <p> If you build up your CustomBinding in code, all you need to do is set the <a href="http://msdn.microsoft.com/en-us/library/microsoft.servicebus.channels.connectionorientedtransportbindingelement.transfermode.aspx">TransferMode</a> property to <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.transfermode.aspx">Streamed</a>: </p> <p> <pre><span style="color: blue">var</span> pipeTransport = <span style="color: blue">new</span> <span style="color: #2b91af">NamedPipeTransportBindingElement</span>(); pipeTransport.TransferMode = <span style="color: #2b91af">TransferMode</span>.Streamed;</pre> </p> <p> In this example, I'm setting the property on a Named Pipe transport, but you can do exactly the same with a TCP transport. </p> <p> Although I wasn't able to find any documentation to that effect, experimentation seems to indicate that you can also set the property in a .config file (at least, it works on my computer): </p> <p> <pre><span style="color: blue">&lt;</span><span style="color: #a31515">namedPipeTransport</span><span style="color: blue"> </span><span style="color: red">transferMode</span><span style="color: blue">=</span>"<span style="color: blue">Streamed</span>"<span style="color: blue"> /&gt;</span></pre> </p> <p> I will not claim that I fully understand this fix/workaround, or that it applies in every situation, but I hope that it might prove helpful to some of my readers some day. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture .8.3 Released https://blog.ploeh.dk/2009/06/20/AutoFixture.8.3Released 2009-06-20T08:34:30+00:00 Mark Seemann <div id="post"> <p> It was only earlier this week that I released <a href="http://autofixture.codeplex.com/">AutoFixture</a> .8.2, but now I'm releasing version .8.3 - not that there was anything wrong with .8.2 (that I know of), but I had some time to implement new features, and I wanted to properly release those. </p> <p> In the future I will blog about these new features (along with all the other AutoFixture features I haven't introduced yet). </p> <p> Get it at the <a href="http://autofixture.codeplex.com/">AutoFixture CodePlex site</a> as usual. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture .8.2 Released https://blog.ploeh.dk/2009/06/18/AutoFixture.8.2Released 2009-06-18T19:11:39+00:00 Mark Seemann <div id="post"> <p> Yesterday I created a new release (.8.2) of <a href="http://autofixture.codeplex.com/">AutoFixture</a>; this time with a new feature that I recently discovered that I needed, and about which I will blog later. </p> <p> There are no breaking changes and no known bugs. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Calling Methods While Building Anonymous Variables With AutoFixture https://blog.ploeh.dk/2009/06/09/CallingMethodsWhileBuildingAnonymousVariablesWithAutoFixture 2009-06-09T17:50:54+00:00 Mark Seemann <div id="post"> <p> Previously, we saw how you can <a href="/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture">set property values while building</a> <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">anonymous variables</a> with <a href="http://autofixture.codeplex.com/">AutoFixture</a>. While I insinuated that I consider this a somewhat atypical scenario, we can now safely venture forth to the truly exotic :) </p> <p> Imagine that you want to build an anonymous instance of a type that requires you to call a certain method before you can start assigning properties. It's difficult to come up with a reasonable example of this, but although I consider it quite bad design, I've seen interfaces that include an Initialize method that must be called before any other member. In our example, let's call this interface IBadDesign. </p> <p> Let's say that we have a class called SomeImp that implements IBadDesign. Here's the relevant part of the class: </p> <p> <pre><span style="color: blue">#region</span> IBadDesign Members &nbsp; <span style="color: blue">public</span> <span style="color: blue">string</span> Message { &nbsp;&nbsp;&nbsp; <span style="color: blue">get</span> { <span style="color: blue">return</span> <span style="color: blue">this</span>.message; } &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (<span style="color: blue">this</span>.mc == <span style="color: blue">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">InvalidOperationException</span>(<span style="color: #a31515">"..."</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.message = <span style="color: blue">value</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.transformedMessage = <span style="color: blue">this</span>.mc.DoStuff(<span style="color: blue">value</span>); &nbsp;&nbsp;&nbsp; } } &nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Initialize(<span style="color: #2b91af">MyClass</span> mc) { &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.mc = mc; } &nbsp; <span style="color: blue">#endregion</span></pre> </p> <p> This is a rather ridiculous example, but I couldn't think of a better one. The main point here is that given a brand-new instance of SomeImp, this usage is invalid: </p> <p> <pre>something.Message = <span style="color: #a31515">"Ploeh"</span>;</pre> </p> <p> While the above code snippet will throw an InvalidOperationException, this will work: </p> <p> <pre>something.Initialize(<span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>()); something.Message = <span style="color: #a31515">"Ploeh"</span>;</pre> </p> <p> The problem is that by default, AutoFixture ignores methods and only assigns properties, which means that this is also going to throw: </p> <p> <pre><span style="color: blue">var</span> imp = fixture.CreateAnonymous&lt;<span style="color: #2b91af">SomeImp</span>&gt;();</pre> </p> <p> As we saw with properties, we can use the Build method to customize how the type is being created. Properties can be set with the With method, while methods can be invoked with the Do method, so this is all it takes: </p> <p> <pre><span style="color: blue">var</span> imp = fixture.Build&lt;<span style="color: #2b91af">SomeImp</span>&gt;() &nbsp;&nbsp;&nbsp; .Do(s =&gt; s.Initialize(<span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>())) &nbsp;&nbsp;&nbsp; .CreateAnonymous();&nbsp;&nbsp;&nbsp; </pre> </p> <p> We don't have to explicitly set the Message property, as AutoFixture is going to do this automatically, and all implicit assignments happen after explicit actions defined by With or Do (and in case you were wondering, you can mix With and Do, and ObjectBuilder&lt;T&gt; will preserve the ordering). </p> <p> In most cases, having to use the Do method probably constitutes a design smell of the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, but the method is there if you need it. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tweet https://blog.ploeh.dk/2009/06/05/Tweet 2009-06-05T19:12:49+00:00 Mark Seemann <div id="post"> <p> Lately, several people have implied to me that I should consider start using <a href="http://twitter.com">Twitter</a>, so <a href="http://twitter.com/ploeh">here I am</a>, with only a vague idea about what to do with it... </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Testability Is Really The Open/Closed Principle https://blog.ploeh.dk/2009/06/05/TestabilityIsReallyTheOpenClosedPrinciple 2009-06-05T07:56:19+00:00 Mark Seemann <div id="post"> <p> When I talk with people about TDD and unit testing, the discussion often moves into the area of Testability - that is, the software's susceptibility to unit testing. A couple of years back, <a href="http://weblogs.asp.net/rosherove/default.aspx">Roy</a> even discussed the seemingly <a href="http://weblogs.asp.net/rosherove/archive/2007/02/25/why-you-should-think-about-ootp-object-oriented-testable-programming.aspx">opposable forces of Object-Oriented Design and Testability</a>. </p> <p> Lately, it has been occurring to me that there really isn't any conflict. Encapsulation is important because it manifests expert knowledge so that other developers can effectively leverage that knowledge, and it does so in a way that minimizes misuse. </p> <p> However, too much encapsulation goes against the Open/Closed Principle (that states that objects should be open for extension, but closed for modification). From a Testability perspective, the Open/Closed Principle pulls object-oriented design in the desired direction. Equivalently, done correctly, making your API Testable is simply opening it up for extensibility. </p> <p> As an example, consider a simple <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx">WPF ViewModel</a> class called MainWindowViewModel. This class has an <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx">ICommand</a> property that, when invoked, should show a message box. Showing a message box is good example of breaking testability, because if the <a href="http://xunitpatterns.com/SUT.html">SUT</a> were to show a message box, it would be very hard to automatically verify and we wouldn't have fully automated tests. </p> <p> For this reason, we need to introduce an abstraction that basically models an action with a string as input. <a href="/2009/05/28/DelegatesAreAnonymousInterfaces">Although we could define an interface for that, an Action&lt;string&gt; fits the bill perfectly</a>. </p> <p> To enable that feature, I decide to use Constructor Injection to inject that abstraction into the MainWindowViewModel class: </p> <p> <pre><span style="color: blue">public</span> MainWindowViewModel(<span style="color: #2b91af">Action</span>&lt;<span style="color: blue">string</span>&gt; notify) { &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.ButtonCommand = <span style="color: blue">new</span> <span style="color: #2b91af">RelayCommand</span>(p =&gt; &nbsp;&nbsp;&nbsp; { notify(<span style="color: #a31515">"Button was clicked!"</span>); }); }</pre> </p> <p> When I recently did that at a public talk I gave, one member of the audience initially reacted by assuming that I was now introducing test-specific code into my SUT, but that's not the case. </p> <p> What I'm really doing here is opening the MainWindowViewModel class for extensibility. It can still be used with message boxes: </p> <p> <pre><span style="color: blue">var</span> vm = <span style="color: blue">new</span> <span style="color: #2b91af">MainWindowViewModel</span>(s =&gt; <span style="color: #2b91af">MessageBox</span>.Show(s));</pre> </p> <p> but now we also have the option of notifying by sending off an email; writing to a database; or whatever else we can think of. </p> <p> It just so happens that one of the things we can do instead of showing a message box, is unit testing by passing in a <a href="http://xunitpatterns.com/Test%20Double.html">Test Double</a>. </p> <p> <pre><span style="color: green">// Fixture setup</span> <span style="color: blue">var</span> mockNotify = &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MockRepository</span>.GenerateMock&lt;<span style="color: #2b91af">Action</span>&lt;<span style="color: blue">string</span>&gt;&gt;(); mockNotify.Expect(a =&gt; a(<span style="color: #a31515">"Button was clicked!"</span>)); &nbsp; <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MainWindowViewModel</span>(mockNotify); <span style="color: green">// Exercise system</span> sut.ButtonCommand.Execute(<span style="color: blue">new</span> <span style="color: blue">object</span>()); <span style="color: green">// Verify outcome</span> mockNotify.VerifyAllExpectations(); <span style="color: green">// Teardown</span></pre> </p> <p> Once again, TDD has lead to better design. In this case it prompted me to open the class for extensibility. There really isn't a need for Testability as a specific concept; the Open/Closed Principle should be enough to drive us in the right direction. </p> <p> Pragmatically, that's not the case, so we use TDD to drive us towards the Open/Closed Principle, but I think it's important to note that we are not only doing this to enable testing: We are creating a better and more flexible API at the same time. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture Cheat Sheet https://blog.ploeh.dk/2009/06/04/AutoFixtureCheatSheet 2009-06-04T21:15:08+00:00 Mark Seemann <div id="post"> <p> To make it a bit easier to get started with <a href="http://autofixture.codeplex.com/">AutoFixture</a> without having to trawl through all my blog posts, I've added a <a href="http://autofixture.codeplex.com/Wiki/View.aspx?title=CheatSheet">Cheat Sheet</a> over at the AutoFixture CodePlex site. </p> <p> As I add more posts on AutoFixture, I'll update the Cheat Sheet with the essentials. Please let me know if you think something's missing. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Setting Property Values While Building Anonymous Variables With AutoFixture https://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture 2009-06-01T12:35:18+00:00 Mark Seemann <div id="post"> <p> In my previous post I described <a href="/2009/05/26/TheAutoFixtureBuilder">how the Build method can be used to customize how a single anonymous variable is created</a>. </p> <p> A common customization is to set a property value during creation. In most cases, this can simply be done <em>after</em> the anonymous variable has been created (so the following is not an AutoFixture customization): </p> <p> <pre><span style="color: blue">var</span> mc = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;(); mc.MyText = <span style="color: #a31515">"Ploeh"</span>;</pre> </p> <p> By default, <a href="http://autofixture.codeplex.com/">AutoFixture</a> assigns anonymous values to all writable properties, but since they are writable, you can always explicitly give them different values if you care. </p> <p> However, there are situations when a property is writable, but can't take just any value of its type. Sometimes this is a sign that you should reconsider your API, as I've <a href="/2009/05/01/DealingWithConstrainedInput">previously described</a>, but it may also be a legitimate situation. </p> <p> Consider a Filter class that has Min and Max properties. To be semantically correct, the Min property must be less than or equal to the Max property. Each property is implemented like this: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">int</span> Min { &nbsp;&nbsp;&nbsp; <span style="color: blue">get</span> { <span style="color: blue">return</span> <span style="color: blue">this</span>.min; } &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (<span style="color: blue">value</span> &gt; <span style="color: blue">this</span>.Max) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"value"</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.min = <span style="color: blue">value</span>; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> When you ask AutoFixture to create an instance of the Filter class, it will throw an exception because it's attempting to set the Min property after the Max property, and the <a href="/2009/04/03/CreatingNumbersWithAutoFixture">default algorithm for numbers</a> is to return numbers in an increasing sequence. (In this example, the Min property is being assigned a value <em>after</em> the Max property, but AutoFixture has no contract that states that the order in which properties are assigned is guaranteed.) In other words, this throws an exception: </p> <p> <pre><span style="color: blue">var</span> f = fixture.CreateAnonymous&lt;<span style="color: #2b91af">Filter</span>&gt;();</pre> </p> <p> To solve this problem, we will have to customize the assignment of the Min and Max properties, <em>before</em> we ask AutoFixture to create an instance of the Filter class. Here's how to do that: </p> <p> <pre><span style="color: blue">int</span> min = fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;(); <span style="color: blue">int</span> max = min + 1; <span style="color: blue">var</span> f = fixture.Build&lt;<span style="color: #2b91af">Filter</span>&gt;() &nbsp;&nbsp;&nbsp; .With(s =&gt; s.Max, max) &nbsp;&nbsp;&nbsp; .With(s =&gt; s.Min, min) &nbsp;&nbsp;&nbsp; .CreateAnonymous();</pre> </p> <p> The With method lets you specify an expression that identifies a property, as well as the value that should be assigned to that property. When you do that, AutoFixture will never attempt to assign an anonymous value to that property, but will instead use the value you specified. </p> <p> In most cases, just creating a truly anonymous instance and subsequently explicitly assigning any significant values is easier, but using the Build method with one or more calls to the With method gives you the power to override any property assignments before the instance is created. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Delegates Are Anonymous Interfaces https://blog.ploeh.dk/2009/05/28/DelegatesAreAnonymousInterfaces 2009-05-28T20:19:04+00:00 Mark Seemann <div id="post"> <p> This is really nothing new, but I don't think I've explicitly stated this before: It makes a lot of sense to view delegates as anonymous one-method interfaces. </p> <p> Many people liken delegates to function pointers. While that's probably correct (I wouldn't really know), it's not a very object-oriented view to take - at least not when we are dealing with managed code. To me, it makes more sense to view delegates as anonymous one-method interfaces. </p> <p> Lets consider a simple example. As always, we have the ubiquitous MyClass with its DoStuff method. In this example, DoStuff takes as input an abstraction that takes a string as input and returns an integer - let's imagine that this is some kind of Strategy (notice the capital <em>S</em> - I'm talking about the design pattern, here). </p> <p> In traditional object-oriented design, we could solve this by introducing the IMyInterface type: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IMyInterface</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> DoIt(<span style="color: blue">string</span> message); }</pre> </p> <p>The implementation of DoStuff is simply:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">string</span> DoStuff(<span style="color: #2b91af">IMyInterface</span> strategy) { &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> strategy.DoIt(<span style="color: #a31515">"Ploeh"</span>).ToString(); }</pre> </p> <p>Hardly rocket science…</p> <p>However, defining a completely new interface just to do this is not really necessary, since we could just as well have implemented DoStuff with a Func&lt;string, int&gt;:</p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">string</span> DoStuff(<span style="color: #2b91af">Func</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">int</span>&gt; strategy) { &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> strategy(<span style="color: #a31515">"Ploeh"</span>).ToString(); }</pre> </p> <p> This not only frees us from defining a new interface, but also from implementing that interface to use the DoStuff method. Instead, we can simply pass a lambda expression: </p> <p> <pre style="margin: 0px"><span style="color: blue">string</span> result = sut.DoStuff(s =&gt; s.Count());</pre> </p> <p> What's most amazing is that <a href="http://ayende.com/projects/rhino-mocks.aspx">RhinoMocks</a> understands and treats delegates just like other abstract types, so that we can write the following to treat it as a mock: </p> <p> <pre style="margin: 0px"><span style="color: green">// Fixture setup</span> <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">int</span>&gt; mock = &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MockRepository</span>.GenerateMock&lt;<span style="color: #2b91af">Func</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">int</span>&gt;&gt;(); mock.Expect(f =&gt; f(<span style="color: #a31515">"Ploeh"</span>)).Return(42); <span style="color: blue">var</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); <span style="color: green">// Exercise system</span> <span style="color: blue">string</span> result = sut.DoStuff(mock); <span style="color: green">// Verify outcome</span> mock.VerifyAllExpectations(); <span style="color: green">// Teardown</span></pre> </p> <p> Whenever possible, I prefer to model my APIs with delegates instead of one-method interfaces, since it gives me greater flexibility and less infrastructure code. </p> <p> Obviously, this technique only works as long as you only need to abstract a single method. As soon as your abstraction needs a second method, you will need to introduce a proper interface or, preferably, an abstract base class. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="72e7549bc19b4f7d825c42e426362694"> <div class="comment-author">jonnie savell <a href="#72e7549bc19b4f7d825c42e426362694">#</a></div> <div class="comment-content">&quot;While that's probably correct (I wouldn't really know), it's not a very object-oriented view to take.&quot;<br> <br> We shouldn't believe that delegates are unlike a function pointer just because the latter is not object-oriented. The shoe ... fits. Furthermore, I would argue that an anonymous one-method interfaces is not a first-class object-oriented concept; we can describe it with words, but I doubt that you will find any of the non-.NET literature talking about such a thing. Well ... I will grant that mention might be made under a description of the command pattern.<br> <br> &quot;Obviously, this technique only works as long as you only need to abstract a single method.&quot;<br> <br> Yes. Then we are in trouble and we didn't even swim that far from shore.<br> <br> What was the problem? We focussed too much on a method and we ignored the interface. An interface defines the contract of which the method is only a part. The contract is identified by the name of the interface. There is no contract defined by method signatures. &quot;Takes an int and a double and returns a string&quot; doesn't mean anything.<br> <br> In summary, focussing on the method is every bit as dirty as ... function pointers.<br> <br> Sincerely,<br> jonnie savell</div> <div class="comment-date">2010-04-07 07:47 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The AutoFixture Builder https://blog.ploeh.dk/2009/05/26/TheAutoFixtureBuilder 2009-05-26T21:30:35+00:00 Mark Seemann <div id="post"> <p> Until now, I've shown you how you can make wholesale adjustments or customizations to an entire Fixture instance, effectively changing the way it creates all instances of a particular type. </p> <p> In some scenarios, you'd rather want to customize how a single instance is created without influencing other instances of the same type. For this purpose, <a href="http://autofixture.codeplex.com/">AutoFixture</a> includes a class called ObjectBuilder&lt;T&gt; that can be used to do exactly that. </p> <p> The easiest way to get an instance of this class is by calling Build on a Fixture instance. This will give you an instance of ObjectBuilder&lt;T&gt; that you can use to customize the build steps. When you are done, CreateAnonymous returns the built instance. </p> <p> <pre><span style="color: blue">var</span> mc = fixture.Build&lt;<span style="color: #2b91af">MyClass</span>&gt;().CreateAnonymous();</pre> </p> <p> This particular example doesn't define any customizations, so it's equivalent to </p> <p> <pre><span style="color: blue">var</span> mc = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre> </p> <p> In fact, Fixture.CreateAnonymous is little more than a convenience method wrapping an ObjectBuilder (there's a few extra differences, but that's a topic for another post). </p> <p> It's worth noting that the object specified by the type parameter to the Build method is first created when you call CreateAnonymous. </p> <p> In future posts I'll demonstrate how to use the Build method to customize individual <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">anonymous variables</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. SyncOrchestrator.Synchronize() Throws COMException When Unit Testing https://blog.ploeh.dk/2009/05/21/SyncOrchestrator.Synchronize()ThrowsCOMExceptionWhenUnitTesting 2009-05-21T18:54:01+00:00 Mark Seemann <div id="post"> <p> This post describes a particular problem I ran into when working with the Microsoft Sync Framework. Since I found a solution, I'm sharing it here to help others. If you are not having this particular problem, it's quite safe to skip reading the rest of the post :) </p> <p> While developing a SyncProvider, I wanted to create and execute a series of <a href="http://blogs.msdn.com/ploeh/archive/2006/08/27/727211.aspx">Integration Tests</a> to drive my development effort. In order to do that, I wrote a simple test that simply created a SyncOrchestrator instance and invoked its Synchronize method. </p> <p> Running this test gave me this error message: </p> <p> “Microsoft.Synchronization.SyncException: Retrieving the COM class factory for component with CLSID {A7B3B4EE-925C-4D6C-B007-A4A6A0B09143} failed due to the following error: 80040154. ---&gt;&nbsp; System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {A7B3B4EE-925C-4D6C-B007-A4A6A0B09143} failed due to the following error: 80040154.” </p> <p> It's not often I see a COMException these days, so I was initially baffled. Since the Sync Framework also has an unmanaged API, this is really not surprising, but that didn't help me solve my problem. </p> <p> What was even weirder was that when I tried running the same code in my application, this exception was <em>not</em> being thrown. </p> <p> It took me a couple of hours to figure out what the problem was. </p> <p> Here's a little hint: I'm running Windows Vista x64. </p> <p> No: The issue is not that I'm running Vista :) </p> <p> Even on x64, Visual Studio runs as a 32-bit process, and so does MSTest. Since my code was compiled to <em>Any CPU</em>, the application itself was running in a 64-bit process, whereas my unit test was running in a 32-bit process. </p> <p> I tried changing my build output to x86, and now the application started throwing the same exception as the unit test did. </p> <p> In other words: When running in a 64-bit process, everything worked as intended. When running in a 32-bit process, a COMException was thrown. </p> <p> As it turned out, I had only installed the 64-bit version of the Sync Framework, and even though the SDK seems to contain builds for the other architectures as well, the COM Server wasn't properly registered for 32-bit use. </p> <p> To resolve this issue, I downloaded and installed the x86 version of the Sync Framework as well, and the problem went away. </p> <p> If you are having the same problem, I hope this post helps you resolve it. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="886907632d75456fbf9f4987c1b64090"> <div class="comment-author">Reginald Henderson <a href="#886907632d75456fbf9f4987c1b64090">#</a></div> <div class="comment-content">PERFECT. This is exactly what I needed!!! Thanks dude.</div> <div class="comment-date">2009-07-10 12:46 UTC</div> </div> <div class="comment" id="7c70659d8b654c64a43730e023ad1976"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7c70659d8b654c64a43730e023ad1976">#</a></div> <div class="comment-content">Hi Reginal<br> <br> Thank you for your message - I am happy that my post was helpful to you.</div> <div class="comment-date">2009-07-10 19:23 UTC</div> </div> <div class="comment" id="e14439d3b8d64921a9ce01f668181ff0"> <div class="comment-author"><a href="http://sjbdeveloper.blogspot.com">Scott Baldwin</a> <a href="#e14439d3b8d64921a9ce01f668181ff0">#</a></div> <div class="comment-content">Thanks for this, I stumbled on this one today. My issue was simply that it was still compiling for &quot;Any CPU&quot;. I simply changed it to x86, and there were no problems.</div> <div class="comment-date">2010-03-07 02:45 UTC</div> </div> <div class="comment" id="e6cf9fce0f6a40179c752701a9fcd147"> <div class="comment-author">Bernhard <a href="#e6cf9fce0f6a40179c752701a9fcd147">#</a></div> <div class="comment-content">Thanks, that did the job!</div> <div class="comment-date">2011-09-09 07:49 UTC</div> </div> <div class="comment" id="5df7085bff534edba76405c492f49cab"> <div class="comment-author">Michael N <a href="#5df7085bff534edba76405c492f49cab">#</a></div> <div class="comment-content">Thank you for the information; this worked for me!<br> <br> I was trying to find out why SharePoint Workspace did not work on my workstation. It was failing with the &quot;Class not found&quot; 80040154 error, and I found your article with a Google search on the CLSID. Installing Microsoft Sync Framework 1.0 SP1 fixed the problem, and I would never have known where to look without your article.</div> <div class="comment-date">2011-09-14 11:25 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture .8.1 Released https://blog.ploeh.dk/2009/05/17/AutoFixture.8.1Released 2009-05-17T07:48:36+00:00 Mark Seemann <div id="post"> <p> Today I've created a new release (.8.1 for lack of a better version number) of <a href="http://autofixture.codeplex.com/">AutoFixture</a>. While it contains some breaking changes, they all relate to features that I have yet to cover here on the blog - in other words: All the examples that I've posted so far are still valid. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. AutoFixture As Fixture Object https://blog.ploeh.dk/2009/05/15/AutoFixtureAsFixtureObject 2009-05-15T05:34:00+00:00 Mark Seemann <div id="post"> <p> Dear reader, I hope you are still with me! </p> <p> After eight posts of <a href="http://autofixture.codeplex.com/">AutoFixture</a> feature walkthroughs, I can't blame you for wondering why this tool might even be relevant to you. In this post, we'll finally begin to look at how AutoFixture can help you towards <a href="/2009/01/28/Zero-FrictionTDD">Zero-Friction TDD</a>! </p> <p> In an earlier post, I described how the <a href="/2009/03/16/FixtureObject">Fixture Object</a> pattern can help you greatly reduce the amount of test code that you have to write. Since AutoFixture was designed to act as a general-purpose Fixture Object, it can help you reduce the amount of test code even further, letting you focus on <a href="/2009/03/10/Specification-DrivenDevelopment">specifying the behavior</a> of your <a href="http://xunitpatterns.com/SUT.html">SUT</a>. </p> <p> In that former post, the original example was this complex test that I will repeat in it's entirety for your benefit (or horror): </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_Naïve() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing1 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 3, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 1"</span> &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing2 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 6, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 2"</span> &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing3 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 3"</span> &nbsp;&nbsp;&nbsp; }; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = <span style="color: blue">new</span>[] { thing1, thing2, thing3 }. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select(t =&gt; t.Number).Sum(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(); &nbsp;&nbsp;&nbsp; fake.AddThing(thing1); &nbsp;&nbsp;&nbsp; fake.AddThing(thing2); &nbsp;&nbsp;&nbsp; fake.AddThing(thing3); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(fake); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> This test consists of 18 lines of code. </p> <p> Using the Fixture Object pattern, I was able to cut that down to 7 lines of code, which is a 61% improvement (however, the downside was an additional 19 lines of (reusable) code for MyClassFixture, so the benefit can only be reaped when you have multiple tests leveraged by the same Fixture Object. This was all covered in the <a href="/2009/03/16/FixtureObject">former post, to which I will refer you</a>). </p> <p> With AutoFixture, we can do much better. Here's a one-off rewrite of the unit test using AutoFixture: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_AutoFixture() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Fixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(); &nbsp;&nbsp;&nbsp; fixture.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(() =&gt; fake); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> things = fixture.CreateMany&lt;<span style="color: #2b91af">Thing</span>&gt;().ToList(); &nbsp;&nbsp;&nbsp; things.ForEach(t =&gt; fake.AddThing(t)); &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = things.Select(t =&gt; t.Number).Sum(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> In this test, I <a href="/2009/04/23/DealingWithTypesWithoutPublicConstructors">map the concrete fake instance to the IMyInterface type</a> in the fixture object, and then use its ability to <a href="/2009/05/11/AnonymousSequencesWithAutoFixture">create many anonymous instances with one method call</a>. Before exercising the SUT, I also use the fixture instance as a <a href="/2009/02/13/SUTFactory">SUT Factory</a>. </p> <p> Apart from AutoFixture (and FakeMyInterface, which is invariant for all variations, and thus kept out of the comparison), this test stands alone, but still manages to reduce the number of code lines to 10 lines - a 44% improvement! In my book, that's already a significant gain in productivity and maintainability, but we can do better! </p> <p> If we need to test MyClass repeatedly in similar ways, we can move the common code to a Fixture Object based on AutoFixture, and the test can be refactored to this: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_DerivedFixture() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClassFixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">MyClassFixture</span>(); &nbsp;&nbsp;&nbsp; fixture.AddManyTo(fixture.Things); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.Things.Select(t =&gt; t.Number).Sum(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> Now we are back at 7 lines of code, which is on par with the original Fixture Object-based test, but now MyClassFixture is reduced to 8 lines of code: </p> <p> <pre><span style="color: blue">internal</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyClassFixture</span> : <span style="color: #2b91af">Fixture</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> MyClassFixture() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Things = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Thing</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(() =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Things.ToList().ForEach(t =&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fake.AddThing(t)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> fake; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Thing</span>&gt; Things { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; } }</pre> </p> <p> Notice how I've moved the IMyInterface-to-FakeMyInterface mapping to MyClassFixture. Whenever it's asked to create a new instance of IMyInterface, MyClassFixture makes sure to add all the Thing instances to the fake before returning it. </p> <p> Compared to the former Fixture Object of 19 lines, that's another 58% improvement. Considering some of the APIs I encounter in my daily work, the above example is even rather simple. The more complex and demanding your SUT's API is, the greater the gain from using AutoFixture will be, since it's going to figure out much of the routine stuff for you. </p> <p> With this post, I hope I have given you a taste of the power that AutoFixture provides. It allows you to focus on specifying the behavior of your SUT, while taking care of all the infrastructure tedium that tends to get in the way. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Anonymous Sequences With AutoFixture https://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture 2009-05-11T20:25:42+00:00 Mark Seemann <div id="post"> <p> When writing unit tests you often need to deal with sequences and collections, populating lists with anonymous data as part of setting up a <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a>. </p> <p> This is easy to do with <a href="http://autofixture.codeplex.com/">AutoFixture</a>. While you can obviously create a simple loop and call CreateAnonymous from within the loop, AutoFixture provides some convenient methods for working with sequences. </p> <p> Equivalent to the CreateAnonymous method, the Fixture class also includes the CreateMany method that creates a sequence of <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">anonymous variables</a>. <em>CreateManyAnonymous</em> might have been a more concise and consistent name for the method, but I felt that this was a bit too verbose. </p> <p> This will create an IEnumerable&lt;string&gt;: </p> <p> <pre style="margin: 0px"><span style="color: #2b91af">Fixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); <span style="color: blue">var</span> strings = fixture.CreateMany&lt;<span style="color: blue">string</span>&gt;();</pre> </p> <p> Obviously, you can create sequences of whatever type you want, as long as <a href="/2009/03/24/HowAutoFixtureCreatesObjects">AutoFixture can figure out how to create instances of the type</a>: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> myInstances = fixture.CreateMany&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre> </p> <p> Being able to create sequences of anonymous data is nice, but sometimes you need to add multiple anonymous items to an existing list (particularly if that list is a read-only property of your <a href="http://xunitpatterns.com/SUT.html">SUT</a>). </p> <p> To support that scenario, the Fixture class also has the AddManyTo method that can be used like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> list = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">MyClass</span>&gt;(); fixture.AddManyTo(list);</pre> </p> <p> This simply creates many anonymous MyClass instances and adds them all to the list. Once more, <em>AddManyAnonymousTo</em> might have been a more precise name, but again I chose a less verbose alternative. </p> <p> If you want more control over how the instances are created, a more explicit overload of AddManyTo gives you that. </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> list = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;(); <span style="color: blue">var</span> r = <span style="color: blue">new</span> <span style="color: #2b91af">Random</span>(); fixture.AddManyTo(list, () =&gt; r.Next());</pre> </p> <p> The above examples adds many random numbers to the list of integers, since the second parameters is a Func&lt;T&gt; used to create the instances. </p> <p> By default, these methods all create 3 anonymous variables when called, since <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">3 is a good equivalent for <em>many</em></a>. If you want a different number of instances to be created, you can modify the RepeatCount property. </p> <p> <pre style="margin: 0px">fixture.RepeatCount = 10; <span style="color: blue">var</span> sequence = fixture.CreateMany&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre> </p> <p> The above example will create an IEnumerable&lt;MyClass&gt; with 10 anonymous MyClass instances, while this will add 7 anonymous instances to the list variable: </p> <p> <pre style="margin: 0px"><span style="color: blue">var</span> list = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">MyClass</span>&gt;(); fixture.RepeatCount = 7; fixture.AddManyTo(list);</pre> </p> <p> AutoFixture provides some convenient methods for creating and managing collections of anonymous data. While it may seem simple (and it is), in a future post I will demonstrate how it can save you quit a bit of infrastructure code, and enable you to write unit tests that are shorter, more concise and more maintainable. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Managing Loosely Coupled Projects https://blog.ploeh.dk/2009/05/05/ManagingLooselyCoupledProjects 2009-05-05T18:54:11+00:00 Mark Seemann <div id="post"> <p> <a href="http://www.udidahan.com/?blog=true">Udi</a> recently posted an <a href="http://www.udidahan.com/2009/05/03/projects-assemblies-and-namespaces-oh-my/">article on managing loose coupling</a> in Visual Studio. While I completely agree, this is a topic that deserves more detailed treatment. In particular, I'd like to expand on this statement: </p> <p> "In fact, each component could theoretically have its own solution" </p> <p> This is really the crux of the matter, although in practical terms, you'd typically need at least a couple of projects per component. In special cases, a component may truly be a stand-alone component, requiring no other dependencies than what is already in the BCL (in fact, <a href="http://autofixture.codeplex.com/">AutoFixture</a> is just such a component), but most components of more complex software have dependencies. </p> <p> Even when you are programming against interfaces (which you should be), these interfaces will normally be defined in other projects. </p> <p> <a href="/content/binary/WindowsLiveWriter/ManagingLooselyCoupledProject_12017/PragmaticMinimalSolution_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="PragmaticMinimalSolution" border="0" alt="PragmaticMinimalSolution" src="/content/binary/WindowsLiveWriter/ManagingLooselyCoupledProject_12017/PragmaticMinimalSolution_thumb.png" width="640" height="364"></a> </p> <p> A component may even use multiple interfaces, since it may be implementing some, but consuming others, and these interfaces may be defined in different projects. This is particularly the case with Adapters. </p> <p> Finally, you should have at least one unit test project that targets your component. </p> <p> In essence, while the exact number of projects you need will vary, it should stay small. In the figure above, we end up with five projects, but there's also quite a few abstractions being pulled in. </p> <p> As a rule of thumb I'd say that if you <em>can't</em> create an .sln file that contains less than ten projects to work on any component, you should seriously consider your decoupling strategy. </p> <p> You may <em>choose</em> to work with more than ten projects in a solution, but it should always be possible to create a solution to work with a single component, and it should drag only few dependencies along. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dealing With Constrained Input https://blog.ploeh.dk/2009/05/01/DealingWithConstrainedInput 2009-05-01T03:56:00+00:00 Mark Seemann <div id="post"> <p> As a response to my description of <a href="/2009/03/24/HowAutoFixtureCreatesObjects">how AutoFixture creates objects</a>, Klaus asked: </p> <p> “[What] if the constructor of ComplexChild imposes some kind of restriction on its parameter? If, for example, instead of the "name" parameter, it would take a "phoneNumber" parameter (as a string), and do some format checking?” </p> <p> Now that we have covered some of the basic features of <a href="http://autofixture.codeplex.com/">AutoFixture</a>, it's time to properly answer this excellent question. </p> <p> For simplicity's sake, let's assume that the phone number in question is a Danish phone number: This is pretty good for example code, since a Danish phone number is essentially just an 8-digit number. It can have white space and an optional country code (+45), but strip that away, and it's just an 8-digit number. However, there are exceptions, since the emergency number is 112 (equivalent to the American 911), and other 3-digit special numbers exist as well. </p> <p> With that in mind, let's look at a simple Contact class that contains a contact's name and Danish phone number. The constructor might look like this: </p> <p> <pre><span style="color: blue">public</span> Contact(<span style="color: blue">string</span> name, <span style="color: blue">string</span> phoneNumber) { &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Name = name; &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.PhoneNumber = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Contact</span>.ParsePhoneNumber(phoneNumber); }</pre> </p> <p> The static ParsePhoneNumber method strips away white space and optional country code and parses the normalized string to a number. This fits the scenario laid out in Klaus' question. </p> <p> So what happens when we ask AutoFixture to create an instance of Contact? It will Reflect over Contact's constructor and create two new anonymous string instances - one for name, and one for phoneNumber. As <a href="/2009/04/02/CreatingStringsWithAutoFixture">previously described</a>, each string will be created as a Guid prepended with a named hint - in this case the argument name. Thus, the phoneNumber argument will get a value like "phoneNumberfa432351-1563-4769-842c-7588af32a056", which will cause the ParsePhoneNumber method to throw an exception. </p> <p> How do we deal with that? </p> <p> The most obvious fix is to <a href="/2009/04/27/ReplacingAutoFixturesDefaultAlgorithms">modify AutoFixture's algorithm for generating strings</a>. Here an initial attempt: </p> <p> <pre>fixture.Register&lt;<span style="color: blue">string</span>&gt;(() =&gt; <span style="color: #a31515">"112"</span>);</pre> </p> <p> This will simply cause <em>all</em> generated strings to be "112", including the Contact instance's Name property. In unit testing, this may not be a problem in itself, since, from an API perspective, the name could in principle be any string. </p> <p> However, if the Contact class also had an Email property that was parsed and verified from a string argument, we'd be in trouble, since "112" is not a valid email address. </p> <p> We can't easily modify the string generation algorithm to fit the requirements for both a Danish telephone number and an email address. </p> <p> Should we then conclude that AutoFixture isn't really useful after all? </p> <p> On the contrary, this is a hint to us that the Contact class' API could be better. If an automated tool can't figure out how to generate correct input, how can we expect other developers to do it? </p> <p> Although humans can make leaps of intuition, an API should still go to great lengths to protect its users from making mistakes. Asking for an unbounded string and then expecting it to be in a particular format may not always be the best option available. </p> <p> In our particular case, the Value Object pattern offers a better alternative. Our first version of the DanishPhoneNumber class simply takes an integer as a constructor argument: </p> <p> <pre><span style="color: blue">public</span> DanishPhoneNumber(<span style="color: blue">int</span> number) { &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.number = number; }</pre> </p> <p> If we still need to parse strings (e.g. from user input), we could add a static Parse, or even a TryParse, method and test that method in isolation without involving the Contact class. </p> <p> This neatly solves our original issue with AutoFixture, since it will now create a new instance of DanishPhoneNumber as part of the creation process when we ask for an anonymous Contact instance. </p> <p> The only remaining issue is that by default, the number fed into the DanishPhoneNumber instance is likely to be considerably less than 112 - actually, <a href="/2009/04/03/CreatingNumbersWithAutoFixture">if no other Int32 instances are created, it will be <em>1</em></a>. </p> <p> This will be a problem if we modify the DanishPhoneNumber constructor to look like this: </p> <p> <pre><span style="color: blue">public</span> DanishPhoneNumber(<span style="color: blue">int</span> number) { &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> ((number &lt; 112) || &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (number &gt; 99999999)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentOutOfRangeException</span>(<span style="color: #a31515">"number"</span>); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.number = number; }</pre> </p> <p> Unless a unit test has already caused AutFixture to previously create 111 other integers (highly unlikely), CreateAnonymous&lt;Contact&gt; is going to throw an exception. </p> <p> This is easy to fix. Once again, the most obvious fix is to modify the creation algorithm for integers. </p> <p> <pre>fixture.Register&lt;<span style="color: blue">int</span>&gt;(() =&gt; 12345678);</pre> </p> <p> However, this will cause that particular instance of Fixture to return 12345678 <em>every</em> time you ask it to create an anonymous integer. Depending on the scenario, this may or may not be a problem. </p> <p> A more targeted solution is to <a href="/2009/04/23/DealingWithTypesWithoutPublicConstructors">specifically address the algorithm</a> for generating DanishPhoneNumber instances: </p> <p> <pre>fixture.Register&lt;<span style="color: blue">int</span>, <span style="color: #2b91af">DanishPhoneNumber</span>&gt;(i =&gt; &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">DanishPhoneNumber</span>(i + 112));</pre> </p> <p> Here, I've even used the Register overload that automatically provides an anonymous integer to feed into the DanishPhoneNumber constructor, so all I have to do is ensure that the number falls into the proper range. Adding 112 (the minimum) neatly does the trick. </p> <p> If you don't like the hard-coded value of 112 in the test, you can use that to further drive the design. In this case, we can add a MinValue to DanishPhoneNumber: </p> <p> <pre>fixture.Register&lt;<span style="color: blue">int</span>, <span style="color: #2b91af">DanishPhoneNumber</span>&gt;(i =&gt; &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">DanishPhoneNumber</span>(i + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">DanishPhoneNumber</span>.MinValue));</pre> </p> <p> Obvously, MinValue will also be used in DanishPhoneNumber's constructor to define the lower limit of the Guard Clause. </p> <p> In my opinion, a good API should guide the user and make it difficult to make mistakes. In many ways, you can view AutoFixture as an exceptionally dim user of your API. This is the reason I really enjoyed receiving Klaus' original question: Like other TDD practices, AutoFixture drives better design. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Speaking at 7N IT Conference 2009 https://blog.ploeh.dk/2009/04/28/Speakingat7NITConference2009 2009-04-28T21:02:32+00:00 Mark Seemann <div id="post"> <p> At May 12 2009 I'll be speaking at 7N's <em>IT Konference 2009</em> (in Danish, so that's no spelling error). You can read the program <a href="http://www.7n.com/dk/Downloads/test2009.pdf">here</a>. </p> <p> The topic of my talk will be TDD patterns and terminology, so I'll discuss Fixtures, Stubs, Mocks and the like. As always, <a href="http://xunitpatterns.com/">xUnit Test Patterns</a> will form the basis of my vocabulary. </p> <p> Of the other speakers, I'm particularly looking forward to hear my good friend Martin Gildenpfennig from <a href="http://community.ative.dk/blogs/">Ative</a> speak! </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Replacing AutoFixture's Default Algorithms https://blog.ploeh.dk/2009/04/27/ReplacingAutoFixturesDefaultAlgorithms 2009-04-27T17:42:07+00:00 Mark Seemann <div id="post"> <p> Several times in my previous <a href="http://autofixture.codeplex.com/">AutoFixture</a> posts, I've insinuated that you can change the algorithms used for creating strings, numbers and so on, if you don't like the defaults. </p> <p> One way you can do this is by simply using the <a href="/2009/04/23/DealingWithTypesWithoutPublicConstructors">Register method that I introduced in my previous post</a>. Let's say that you want to replace the string algorithm to simply return a specific string: </p> <p> <pre>fixture.Register&lt;<span style="color: blue">string</span>&gt;(() =&gt; <span style="color: #a31515">"ploeh"</span>);</pre> </p> <p> No matter how many times you'll call CreateAnonymous&lt;string&gt; on that particular fixture object, it will always return <em>ploeh</em>. </p> <p> The Register method is really only a type-safe convenience method that wraps access to the TypeMappings property. TypeMappings is just a Dictionary of types mapped to functions. By default, the Fixture class has a set of pre-defined TypeMappings for primitive types such as strings, numbers and booleans, so you could access the function used to generate strings by indexing into this Dictionary with the System.String type. </p> <p> Equivalent to the above example, you could alternatively replace the string algorithm like this: </p> <p> <pre>fixture.TypeMappings[<span style="color: blue">typeof</span>(<span style="color: blue">string</span>)] = s =&gt; <span style="color: #a31515">"fnaah"</span>;</pre> </p> <p> Instead of using the Register method, I here assign a lambda expression directly to the key identified by the System.String type. This is what the Register method does, so the result is exactly the same. </p> <p> However, you may have noticed that by accessing TypeMappings directly, the signature of the function is different. The Register method takes a Func&lt;T&gt;, whereas the TypeMappings Dictionary expects a Func&lt;object, object&gt;. As you can see, the Register method is more type-safe, but the TypeMappings Dictionary gives you a chance to utilize the optional seed that one of the CreateAnonymous overloads takes. </p> <p> You could, for example, do this: </p> <p> <pre>fixture.TypeMappings[<span style="color: blue">typeof</span>(<span style="color: blue">string</span>)] = s =&gt; &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span>.Format((<span style="color: blue">string</span>)s, <span style="color: blue">new</span> <span style="color: #2b91af">Random</span>().Next(100));</pre> </p> <p> Although this particular algorithm has a built-in weakness (can you spot it?), we can now use the seed to provide a format string, like this: </p> <p> <pre><span style="color: blue">string</span> result = fixture.CreateAnonymous(<span style="color: #a31515">"Risk: {0}%"</span>);</pre> </p> <p> which will yield a result like <em>Risk: 32%</em>. </p> <p> When I designed the extensibility mechanism for AutoFixture, I seriously considered defining an interface that all TypeMappings had to implement, but I ended up preferring a Func&lt;object, object&gt; instead, since this allows you to redefine a particular algorithm inline in a test by using an anonymous delegate or lambda expression, and you can also reuse an existing algorithm, as long as it fits the signature. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4dcd8a90ba404c86bbb4bf5d9d4ed040"> <div class="comment-author">Paul <a href="#4dcd8a90ba404c86bbb4bf5d9d4ed040">#</a></div> <div class="comment-content">Hello, I am looking at Autofixture now, but cannot find TypeMappings. They were removed from the library?</div> <div class="comment-date">2012-07-25 07:05 UTC</div> </div> <div class="comment" id="476abe7665cd4d0bbcd52f5bfc6404f5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#476abe7665cd4d0bbcd52f5bfc6404f5">#</a></div> <div class="comment-content">Yes, TypeMappings were removed in AutoFixture 2.0, which has a much more flexible extensibility model. Ultimately, <a href="/2010/10/19/Convention-basedCustomizationswithAutoFixture">a custom ISpecimenBuilder</a> can do everything you may want to do.</div> <div class="comment-date">2012-07-25 08:37 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Dealing With Types Without Public Constructors https://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors 2009-04-23T19:51:42+00:00 Mark Seemann <div id="post"> <p> Now that I've described how <a href="http://autofixture.codeplex.com/">AutoFixture</a> <a href="/2009/03/24/HowAutoFixtureCreatesObjects">creates objects</a>, it's time to look at a bit more advanced scenario. As you may recall, AutoFixture creates an object graph based on the public constructors of the types contained in that object graph. </p> <p> That's all well and good when all involved types have public constructors, but what happens when this is not the case? </p> <p> Imagine that the MyClass constructor has this signature: </p> <p> <pre><span style="color: blue">public</span> MyClass(<span style="color: #2b91af">IMyInterface</span> mi)</pre> </p> <p> Since IMyInterface is an interface it has no public constructors, so this will not work: </p> <p> <pre><span style="color: #2b91af">Fixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); <span style="color: #2b91af">MyClass</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre> </p> <p> The second line of code will throw an <strike>ArgumentException</strike> ObjectCreationException* stating that “AutoFixture was unable to create an instance of type Ploeh.QualityTools.AutoFixtureDocumentationTest.Intermediate.IMyInterface, since it has no public constructor.” Not terribly surprising, actually. </p> <p> To resolve this issue, the Register method allows you to specify a custom function that creates an object of the requested type. In the case of IMyInterface, that would be a Func&lt;IMyInterface&gt;: </p> <p> <pre><span style="color: #2b91af">Fixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); fixture.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(() =&gt; &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>()); <span style="color: #2b91af">MyClass</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre> </p> <p> Here, I use a lambda expression to register the FakeMyInterface type, so that every time that particular Fixture instance is asked to create an instance of IMyInterface, it will invoke the lambda expression, and thus return an instance of FakeMyInterface (which obviously implements IMyInterface). </p> <p> The Register method simply lets you map types without public constructors to concrete types created by a function you specify. </p> <p> A more advanced scenario arises if you wish to use a specific text with this FakeMyInterface constructor overload: </p> <p> <pre><span style="color: blue">public</span> FakeMyInterface(<span style="color: blue">int</span> number, <span style="color: blue">string</span> text)</pre> </p> <p> Obviously, you can do it manually like this: </p> <p> <pre><span style="color: #2b91af">Fixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); <span style="color: blue">int</span> anonymousNumber = fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;(); <span style="color: blue">string</span> knownText = <span style="color: #a31515">"This text is not anonymous"</span>; fixture.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(() =&gt; &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(anonymousNumber, knownText));</pre> </p> <p> Here, I simply use the fixture object to create an anonymous number, while the knownText variable is explicitly assigned a value, and then both are used as outer variables in the Register function. </p> <p> This is, however, a common scenario, so the Register method has some convenience overloads that will supply anonymous input parameters for you to use or throw away as you like. This means that I can rewrite the above example to this: </p> <p> <pre><span style="color: #2b91af">Fixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); <span style="color: blue">string</span> knownText = <span style="color: #a31515">"This text is not anonymous"</span>; fixture.Register&lt;<span style="color: blue">int</span>, <span style="color: blue">string</span>, <span style="color: #2b91af">IMyInterface</span>&gt;((i, s) =&gt; &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(i, knownText));</pre> </p> <p> Compared to the previous example, I save a line of code. The drawback is that I have to explicitly specify the type parameters to the Register method. In my book, that's a pretty good tradeoff, as it removes a line of irrelevant code, and allows the test to focus on the relevant parts. </p> <p> Notice that this Register overload creates both an anonymous integer and an anonymous string, but since I don't want to use the anonymous string (s), I just ignore it and use the knownText variable instead. </p> <p> <strong>*Edit (2009-09-02): </strong>Changed the name of the Exception type to correctly reflect a breaking change introduced in <a href="/2009/09/02/AutoFixture.8.5Released">AutoFixture .8.5</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Creating Booleans With AutoFixture https://blog.ploeh.dk/2009/04/19/CreatingBooleansWithAutoFixture 2009-04-19T17:40:36+00:00 Mark Seemann <div id="post"> <p> (Just back after 14 days in Morocco, I'll pick up where I left…) </p> <p> The last group of built-in special creation algorithms for <a href="http://autofixture.codeplex.com/">AutoFixture</a>, besides <a href="/2009/04/02/CreatingStringsWithAutoFixture">strings</a> and <a href="/2009/04/03/CreatingNumbersWithAutoFixture">numbers</a>, concerns Booleans. </p> <p> The default algorithm is to alternate between true and false, starting with true; i.e. the first time you invoke </p> <p> <pre>fixture.CreateAnonymous&lt;<span style="color: blue">bool</span>&gt;();</pre> </p> <p> it will return <em>true</em>, the next time <em>false</em>, then <em>true</em> again, etc. </p> <p> The reason I chose this algorithm was because I wanted to ensure that the first time AutoFixture creates an anonymous Boolean, it will return true, which is different than the default (false, in case you were in doubt). This gives us better assurance that a given constructor argument or property is being assigned a real value instead of the default. </p> <p> Like with numbers, using the overload that takes a seed has no effect, and the seed is simply ignored. </p> <p> <pre>fixture.CreateAnonymous(<span style="color: blue">true</span>);</pre> </p> <p> In other words, the above method is still going to return <em>false</em> every second time, so it doesn't really make sense to use this overload at all for Booleans (or numbers). </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Creating Numbers With AutoFixture https://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture 2009-04-03T21:07:13+00:00 Mark Seemann <div id="post"> <p> <a href="/2009/04/02/CreatingStringsWithAutoFixture">Previously</a>, we saw how <a href="http://autofixture.codeplex.com/">AutoFixture</a> creates strings. In this post, I'll explain how it creates numbers. Once again, the algorithm that I'll explain here is the default algorithm, but if you don't like it, you can replace it with something else. </p> <p> It's very simple: Numbers are returned in the ordered sequence of natural numbers (1, 2, 3, 4, 5, …). The first time you call </p> <p> <pre>fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;();</pre> </p> <p> the returned number will be <em>1</em>, the second time <em>2</em>, etc. </p> <p> The reason I chose that particular algorithm is because it creates numbers that we, as humans, find… well… natural! </p> <p> A lot of the domains we model work with natural numbers, and even if you write an API where negative numbers are allowed, it's fairly unlikely that positive numbers will <em>not</em> be allowed. Thus, in most cases, small positive integers tend to be ‘nice' values in most APIs - and recall that when we do TDD, we focus on the Happy Path, so it's important to pick values that take us down that path. </p> <p> Using the overload that takes a seed, like this: </p> <p> <pre>fixture.CreateAnonymous(42);</pre> </p> <p> has no effect - the seed (in this case <em>42</em>) is simply ignored, so if you call this after first calling the parameterless overload twice, the return number is going to be <em>3</em>. </p> <p> Each number type, however, has its own sequence, so even if you've been creating a few Int32 instances like above, </p> <p> <pre>fixture.CreateAnonymous&lt;<span style="color: blue">decimal</span>&gt;();</pre> </p> <p> will return <em>1</em>. </p> <p> The following number types all work that way: </p> <ul> <li>Byte</li> <li>Decimal</li> <li>Double</li> <li>Int16</li> <li>Int32</li> <li>Int64</li> <li>SByte</li> <li>Single</li> <li>UInt16</li> <li>UInt32</li> <li>UInt64</li> </ul> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b8f7ad573c594131ad0b607d2fdbd9a5"> <div class="comment-author">florin <a href="#b8f7ad573c594131ad0b607d2fdbd9a5">#</a></div> <div class="comment-content">Having them sequence ordered sounds like there will not be so &quot;non-deterministic&quot;. Wouldn't a random number be more appropriate as it would allow testing of different cases each time. Sure it would not be reproducible but at least you will get to know that there might be something wrong with the code under test.</div> <div class="comment-date">2010-05-21 10:29 UTC</div> </div> <div class="comment" id="3c74ab5681c24e9381e72d9525637d7d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#3c74ab5681c24e9381e72d9525637d7d">#</a></div> <div class="comment-content">The idea about constrained non-determinism is that although test values are essentially unknown, two consecutive test runs <i>must</i> traverse the same branch of source code each time. In some SUTs different integer values may cause different branches to be exectured for each test run, so I found it safer to use a deterministic sequence.<br> <br> For complex objects (say: a class with two writable properties of the same number type) the <i>ordering</i> of assignment is undefined even though the sequence of numbers is deterministic. In other words, if you have the properties Number1 and Number2, you know one gets the value 1 and the other gets the value 2, but you don't know which one gets what (it's probably deterministic anyway because Reflection is likely to always return properties in the same order, but AutoFixture does nothing to explicitly order properties and fields).<br> <br> Another similar strategy could be to use random numbers from a constant seed. This would give you another stable sequence of numbers.<br> <br> If none of the above is a concern (numbers do not influence the execution path) you can also choose to use pure random numbers.<br> <br> In any case you can use the Register method to override the default behavior for a given type, so it's entirely possible to set it up with random numbers instead of a rising sequence.</div> <div class="comment-date">2010-05-21 12:33 UTC</div> </div> <div class="comment" id="db77cb8896ec4e268649e9e3f6b2db0e"> <div class="comment-author"><a href="http://www.google.com/accounts/o8/id?id=AItOawlmJFdkTl0xYKp-zAx1ZfZCPP7X22Emwyk">www.google.com/accounts/o8/id?id=AItOawlmJFdkTl0xYKp-zAx1ZfZCPP7X22Emwyk</a> <a href="#db77cb8896ec4e268649e9e3f6b2db0e">#</a></div> <div class="comment-content">If you offer an overload for the constructor that takes a seed, when/how is it used? As you say, it does not work in this case.</div> <div class="comment-date">2011-03-05 23:16 UTC</div> </div> <div class="comment" id="c6084f8502864f4daac8b1cb0f137bfb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#c6084f8502864f4daac8b1cb0f137bfb">#</a></div> <div class="comment-content">Let's say that you want to change the integer algorithm to return the seed. This can be done like this:<br> <br> fixture.Customize&lt;int&gt;(c =&gt; c.FromSeed(i =&gt; i));</div> <div class="comment-date">2011-03-06 04:24 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Creating Strings With AutoFixture https://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture 2009-04-02T05:29:35+00:00 Mark Seemann <div id="post"> <p> As <a href="/2009/03/24/HowAutoFixtureCreatesObjects">previously hinted</a>, <a href="http://autofixture.codeplex.com/">AutoFixture</a> creates primitive types like strings, numbers, etc. using special algorithms. </p> <p> In this post, I'll describe the <em>default</em> algorithm for strings. If you don't like this particular algorithm, you can replace it with your own. </p> <p> If you don't care about the created string at all, you can just create it like this: </p> <p> <pre><span style="color: blue">string</span> anonymousText = &nbsp;&nbsp;&nbsp; fixture.CreateAnonymous&lt;<span style="color: blue">string</span>&gt;();</pre> </p> <p> The algorithm is simply to create a new Guid and convert it to a string, so that anonymousText will have a value like “f5cdf6b1-a473-410f-95f3-f427f7abb0c7”. Obviously, you don't know exactly which value will be returned, but that's the whole point of <a href="/2009/03/05/ConstrainedNon-Determinism">Constrained Non-Determinism</a>. </p> <p> When I create string values as <a href="/2009/03/11/ExplicitExpectations">Explicit Expectations</a>, I prefer that the Assert failure message contains some sort of hint for me, so I can instead provide a hint to the CreateAnonymous method: </p> <p> <pre><span style="color: blue">string</span> anonymousName = fixture.CreateAnonymous(<span style="color: #a31515">"Name"</span>);</pre> </p> <p> This overload is still generic, but since I provide a string as input, type inferencing takes care of the rest. </p> <p> This is still going to create a Guid, but will now prepend the hint, giving a string like “Name30a35da1-d681-441b-9db3-77ff51728b58”. </p> <p> Now, when my test fails, I'll get an error message equivalent to </p> <p> "Assert.AreEqual failed. Expected:&lt;Namef2b1f55b-e9dc-4aac-a1ab-128dc80d3b71&gt;. Actual:&lt;ploeh&gt;. Boo hiss" </p> <p> which I find marginally more informative than if the hint hadn't been there. </p> <p> In a future post, I'll explain how you can replace this algorithm with something else. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. How AutoFixture Creates Objects https://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects 2009-03-24T20:22:49+00:00 Mark Seemann <div id="post"> <p> <a href="http://autofixture.codeplex.com/">AutoFixture</a> creates <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous Variables</a>, but you'd probably like to know <em>how</em> it does it. This post explains how. </p> <p> As we <a href="/2009/03/22/AnnouncingAutoFixture">previously saw</a>, the CreateAnonymous method can create a new instance of a type known to it only from its type parameter: </p> <p> <pre><span style="color: #2b91af">MyClass</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre> </p> <p> AutoFixture was never compiled with any knowledge of the MyClass type, so it obviously uses Reflection to create the instance. That's hardly surprising in itself. </p> <p> In the case of MyClass, it has a default constructor, so creating an instance is as simple as it can be, but what happens if we instead ask for a more complex instance? </p> <p> As an example, the ComplexParent type has this constructor: </p> <p> <pre><span style="color: blue">public</span> ComplexParent(<span style="color: #2b91af">ComplexChild</span> child)</pre> </p> <p> ComplexChild, however, has two constructors: </p> <p> <pre><span style="color: blue">public</span> ComplexChild(<span style="color: blue">string</span> name)</pre> </p> <p> and </p> <p> <pre><span style="color: blue">public</span> ComplexChild(<span style="color: blue">string</span> name, <span style="color: blue">int</span> number)</pre> </p> <p> So what happens when we ask AutoFixture to create an instance of ComplexParent? </p> <p> ComplexParent only has a single public constructor, so AutoFixture doesn't have any other choice than picking that. This means that it must now create an anonymous instance of ComplexChild. </p> <p> Fortunately, AutoFixture's raison d'être is creating objects, so creating an instance of ComplexChild isn't a big deal; the only thing it needs to figure out is which constructor to pick. When multiple public constructors are available, it always picks the one with the fewest number of arguments - in this case ComplexChild(string). </p> <p> Obviously, it then needs to create an anonymous string value. For primitive types like strings, numbers and booleans, AutoFixture has custom algorithms for value creation. Since I'll cover those mechanisms later, suffice it to say that <a href="/2009/03/05/ConstrainedNon-Determinism">Constrained Non-Determinism</a> is used to create an anonymous string. </p> <p> At this point, AutoFixture has all the information it needs, and it can now return a properly initialized instance of ComplexParent. </p> <p> This ability to create instances of almost arbitrarily complex types is a real time-saver: That, more than the ability to create single strings or numbers, was the reason I originally created AutoFixture, since I got tired of initializing complex object graphs just to satisfy some API that the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Test Fixture</a> requires. </p> <p> It also has the additional advantage that it hides all the irrelevant object creation code that the Test Fixture needs, but which isn't relevant for the test at hand. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="ce11cd3a8c3d4955aba72764cebbf2d8"> <div class="comment-author"><a href="http://www.bonisimo.dk">Klaus Byskov Hoffmann</a> <a href="#ce11cd3a8c3d4955aba72764cebbf2d8">#</a></div> <div class="comment-content">That sounds pretty neat. But what if the constructor of ComplexChild imposes some kind of restriction on its parameter? If, for example, instead of the &quot;name&quot; parameter, it would take a &quot;phoneNumber&quot; parameter (as a string), and do some format checking?</div> <div class="comment-date">2009-03-25 09:17 UTC</div> </div> <div class="comment" id="86976f7349e94336b5852ae80796ef01"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#86976f7349e94336b5852ae80796ef01">#</a></div> <div class="comment-content">Hi Klaus<br> <br> Thank you for your question! It is an excellent question, particularly because it gives me the opportunity to touch on both some of the more advanced features of AutoFixture, as well as to demonstrate how TDD drives good design.<br> <br> However, because of that, I'd also like to cover a bit more ground on some of AutoFixture's features before I dive into the details of my answer. In other words, I'd like to post a few more entries that will act as prerequisites before I post my answer to your particular question, so stay tuned, and I'll get to that in due time :)</div> <div class="comment-date">2009-03-25 13:07 UTC</div> </div> <div class="comment" id="097054743e384c868918b4502fb03c5c"> <div class="comment-author">florin <a href="#097054743e384c868918b4502fb03c5c">#</a></div> <div class="comment-content">What if I have two constructors with the same number of parameters, how will it decide on the right one?</div> <div class="comment-date">2010-05-21 10:51 UTC</div> </div> <div class="comment" id="59acfa2c75f84e28b42961e9886e108a"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#59acfa2c75f84e28b42961e9886e108a">#</a></div> <div class="comment-content">It doesn't, so this is a situation best avoided. Essentially AutoFixture orders the constructors according to length, but in case of equal lengths it just picks the first one. Which constructor is the first is up to Reflection to decide, so this is not specified.<br> <br> This sounds like a serious limitation on AutoFixture's part, but in practice I've found that it rarely matters. In the few cases where a SUT has overloaded constructors, it is essential to the design that none of them puts the SUT into an invalid state, so when you need an <i>anonymous instance</i> it shouldn't matter which constructor is used. This is really what AutoFixture is all about.<br> <br> That said, in AutoFixture 2.0 it will be possible to register custom types that can use other heuristics to pick constructors.</div> <div class="comment-date">2010-05-21 12:41 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Joining Safewhere https://blog.ploeh.dk/2009/03/23/JoiningSafewhere 2009-03-23T19:03:38+00:00 Mark Seemann <div id="post"> <p> Today I spent my first working day at <a href="http://www.safewhere.net/">Safewhere</a>, where I'll be working as a Senior Software Engineer. In the last couple of years, Safewhere and I have crossed paths a couple of times, and each time they always left me with the impression of a very professional and congenial ISV, so I'm super-excited to be joining! </p> <p> Expect the occasional Geneva or Federation-related blog post to intermingle with the usual TDD stuff in the future :) </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="8d89259477d2417eaa8b44e02dea4d96"> <div class="comment-author"><a href="http://zianet.dk/blog">Kristian Kristensen</a> <a href="#8d89259477d2417eaa8b44e02dea4d96">#</a></div> <div class="comment-content">Congratulations on the new job :-)</div> <div class="comment-date">2009-03-24 15:14 UTC</div> </div> <div class="comment" id="74508982d3c045a88a309449e6b88ea7"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#74508982d3c045a88a309449e6b88ea7">#</a></div> <div class="comment-content">Thanks :)</div> <div class="comment-date">2009-03-25 07:32 UTC</div> </div> <div class="comment" id="473cf1a88a3546a09661cdbf34cb27a9"> <div class="comment-author">Mark R&#248;nn <a href="#473cf1a88a3546a09661cdbf34cb27a9">#</a></div> <div class="comment-content">Hi Mark. Great news - congratulations and all the best on your new job :-)</div> <div class="comment-date">2009-04-15 08:31 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Announcing: AutoFixture https://blog.ploeh.dk/2009/03/22/AnnouncingAutoFixture 2009-03-22T06:50:54+00:00 Mark Seemann <div id="post"> <p> It gives me great pleasure to announce my latest project: <a href="http://autofixture.codeplex.com/">AutoFixture</a>! </p> <p> What is AutoFixture? </p> <p> In essence, AutoFixture is a library that creates <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous Variables</a> for you when you write unit tests. The intention is that it should enhance your productivity when you do Test-Driven Development - as it has done mine. </p> <p> Instead of using mental resources on creating Anonymous Variables, AutoFixture can do it for you. By default, it uses <a href="/2009/03/05/ConstrainedNon-Determinism">Constrained Non-Determinism</a>, but you can configure it to behave differently if you wish. </p> <p> Here's a very basic example: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> IntroductoryTest() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Fixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">Fixture</span>(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedNumber = fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.Echo(expectedNumber); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedNumber, result, <span style="color: #a31515">"Echo"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> The Fixture class is your main entry point to AutoFixture. You can use it as is, customize it, or derive from it as you please; it makes a great base class for a <a href="/2009/03/16/FixtureObject">Fixture Object</a>. </p> <p> The <em>expectedNumber</em> variable may be an <a href="/2009/03/11/ExplicitExpectations">Explicit Expectation</a>, but its value is Anonymous, so instead of coming up with a number ourselves, we can let the CreateAnonymous&lt;T&gt; method do it for us. </p> <p> This method can create instances of most CLR types as long as they have a public constructor (it uses Reflection), but for many primitive types (like Int32), it has specific, customizable algorithms for creating values using Constrained Non-Determinism. </p> <p> When creating the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, we can also use Fixture as an excellent <a href="/2009/02/13/SUTFactory">SUT Factory</a>. Since it will do whatever it can to create an instance of the type you ask for, it is pretty robust if you decide to refactor the SUT's constructor. </p> <p> The above example only hints at what AutoFixture can do. Since the example is very simple, it may be hard to immediately understand its value, so in future posts I will expand on specific AutoFixture features and principles. </p> <p> Getting started with AutoFixture is as simple as downloading it from <a href="http://autofixture.codeplex.com/">CodePlex</a> and referencing the assembly in Visual Studio. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="1f33092d92f8416f8ce270a5c9e8b648"> <div class="comment-author"><a href="http://blog.dotnetwiki.org">Peli</a> <a href="#1f33092d92f8416f8ce270a5c9e8b648">#</a></div> <div class="comment-content">Hi Mark,<br> <br> You could also hook up Pex to AutoFixture (http://research.microsoft.com/pex) and let Pex generate inputs for your tests. Pex provides an API to generate values, similar to your CreateAnonymous method so this should be a piece of cake ;)<br> <br> Cheers,<br> Peli</div> <div class="comment-date">2009-05-01 15:53 UTC</div> </div> <div class="comment" id="dd900b1217d94a4b80a155d7d4b704c1"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#dd900b1217d94a4b80a155d7d4b704c1">#</a></div> <div class="comment-content">Hi Peli<br> <br> Thank you for your comment.<br> <br> Pex is a very interesting piece of technology that I'm very excited about. As I see it, Pex and AutoFixture are complementary technologies. While I've dealt briefly with this topic in the <a href="http://autofixture.codeplex.com/Wiki/View.aspx?title=FAQ">AutoFixture FAQ</a>, it bears repeating here: AutoFixture addresses a different scenario than Pex.<br> <br> Roughly speaking, Pex is a Quality Assurance (QA) tool - it's main purpose is to break the application, in the time-honored tradition of QA testing. AutoFixture, on the other hand, is a TDD utility library. It's main purpose is to drive development of features along the happy path.<br> <br> In a full development cycle, I'd use AutoFixture as part of my TDD effort to develop the desired feature. Subsequently, when my API is fairly stable, I'd let Pex loose on it to test its robustness before I ship.<br> <br> Some day, before version 1.0, I should really let Pex loose on AutoFixture :)</div> <div class="comment-date">2009-05-01 21:05 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Fixture Object https://blog.ploeh.dk/2009/03/16/FixtureObject 2009-03-16T20:13:29+00:00 Mark Seemann <div id="post"> <p> (A <a href="/2009/01/28/Zero-FrictionTDD">Zero-Friction TDD</a> post) </p> <p> For a simple API, setting up the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a> may be as simple as creating a new instance of the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, and possibly any <a href="/2009/03/11/ExplicitExpectations">Expected</a> or <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous Variables</a>. On the other hand, for a complex API, setting up the fixture may require quite a bit of (potentially repetitive) code. </p> <p> Since the DRY principle also applies to test code, it quickly becomes necessary to create test-specific helper methods and other <a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20Encapsulation%20Method">SUT API Encapsulation</a>, and I've found that instead of creating a more or less unplanned set of disconnected helper methods, it's much cleaner (and, not to mention, much more object-oriented) to create a single object that represents the Fixture, and attach the helper methods to this object. </p> <p> Let's look at an example. </p> <p> Here's a unit test with a complex <a href="http://xunitpatterns.com/fixture%20setup.html">Fixture Setup</a>: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_Naïve() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing1 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 3, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 1"</span> &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing2 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 6, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 2"</span> &nbsp;&nbsp;&nbsp; }; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing3 = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Number = 1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text = <span style="color: #a31515">"Anonymous text 3"</span> &nbsp;&nbsp;&nbsp; }; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = <span style="color: blue">new</span>[] { thing1, thing2, thing3 }. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select(t =&gt; t.Number).Sum(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(); &nbsp;&nbsp;&nbsp; fake.AddThing(thing1); &nbsp;&nbsp;&nbsp; fake.AddThing(thing2); &nbsp;&nbsp;&nbsp; fake.AddThing(thing3); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(fake); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> If this was a truly one-off test and you know with certainty that there's going to be no other tests just remotely similar to this one, just hard-coding the entire Fixture Setup inline is in order, but as soon as the need for similar tests arises, this approach leads to repetitive code, and hence unmaintainable tests. </p> <p> The more repetitive code that can be delegated to helper methods the better. A common refactoring of the previous test might then look something like this: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_Helpers() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing1 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing2 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> thing3 = <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span>[] things = <span style="color: blue">new</span>[] { thing1, thing2, thing3 }; &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = things.Select(t =&gt; t.Number).Sum(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClassTest</span>.AddThingsToMyInterface(fake, things); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(fake); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> While this is better, the helper methods are static methods, so it's necessary to pass too much state around. The array of Things and the fake is both needed in the test itself, as well as in the AddThingsToMyInterface helper method. </p> <p> By moving and refactoring the helper methods to a new class that represents the Fixture, the test code becomes both more reusable and more readable. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> NumberSumIsCorrect_FixtureObject() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClassFixture</span> fixture = <span style="color: blue">new</span> <span style="color: #2b91af">MyClassFixture</span>(); &nbsp;&nbsp;&nbsp; fixture.AddAnonymousThings(); &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> expectedSum = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixture.Things.Select(t =&gt; t.Number).Sum(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = fixture.CreateSut(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.CalculateSumOfThings(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"Sum of things"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> The MyClassFixture instance now holds the state of the Fixture, so there's much less need to pass around as much data as before. The set of Things is now contained within the Fixture object itself, and the fake has totally disappeared from the test; it's still present, but now encapsulated within MyClassFixture. </p> <p> <pre><span style="color: blue">internal</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyClassFixture</span> { &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> MyClassFixture() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Things = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Thing</span>&gt;(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">FakeMyInterface</span> Fake { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Thing</span>&gt; Things { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: blue">void</span> AddAnonymousThings() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> many = 3; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">for</span> (<span style="color: blue">int</span> i = 0; i &lt; many; i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> t = <span style="color: blue">this</span>.CreateAnonymousThing(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Things.Add(t); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">this</span>.Fake.AddThing(t); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: #2b91af">MyClass</span> CreateSut() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(<span style="color: blue">this</span>.Fake); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">Thing</span> CreateAnonymousThing() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> t = <span style="color: blue">new</span> <span style="color: #2b91af">Thing</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.Number = <span style="color: blue">this</span>.Things.Count + 1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.Text = <span style="color: #2b91af">Guid</span>.NewGuid().ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> t; &nbsp;&nbsp;&nbsp; } }</pre> </p> <p> The CreateAnonymousThing method uses <a href="/2009/03/05/ConstrainedNon-Determinism">Constrained Non-Determinism</a> to create unique Thing instances. The AddAnonymousThings method uses <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">3 as an equivalence of many</a>, and the CreateSut method acts as a <a href="/2009/02/13/SUTFactory">SUT Factory</a>. </p> <p> This is both more reusable and more expressive than a collection of disjointed static helper methods. </p> <p> Whenever I begin to feel that setting up a Test Fixture is becoming too cumbersome, Fixture Object is the first pattern I consider. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Explicit Expectations https://blog.ploeh.dk/2009/03/11/ExplicitExpectations 2009-03-11T19:54:38+00:00 Mark Seemann <div id="post"> <p> In previous <a href="/2009/01/28/Zero-FrictionTDD">Zero-Friction TDD</a> posts, I've discussed <a href="http://blogs.msdn.com/ploeh/archive/2008/10/06/naming-sut-test-variables.aspx">naming SUT</a> and <a href="http://blogs.msdn.com/ploeh/archive/2008/11/14/naming-direct-output-variables.aspx">Direct Output variables</a>, as well as the importance of <a href="/2009/03/03/DerivedValuesEnsureExecutableSpecification">explicitly describing the relationship between input and expected results</a>. </p> <p> Everything you can do to help the <a href="http://xunitpatterns.com/test%20reader.html">Test Reader</a> understand what's going on increases the quality of the test. Having a naming convention for expectations help in that regard, and it also helps coming up with variable names, thus saving yourself a bit of mental context switching. </p> <p> My naming convention is to always prefix my expectation variables with the term <em>expected</em>. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> DoStuffWillReturnMessage() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> expectedResult = <span style="color: #a31515">"ploeh"</span>; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.DoStuff(expectedResult); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"DoStuff"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> In a test like this, the relationship between the input and output is straightforward, but in other cases it may be more complicated. Since tests should explicitly state the relationship between input and output, it may sometimes be necessary to reproduce parts of the <a href="http://xunitpatterns.com/SUT.html">SUT</a>'s behavior in the test to specify this association. </p> <p> Do I really recommend duplicating the SUT's code in the test? Isn't this a violation of the DRY principle? And do I really think that embedding complex code in a test is a good idea? </p> <p> No, no, and no. </p> <p> What I really mean is best illustrated with an example. Imagine that you want to write an extension method that converts a string to PascalCase. There are several different rules that must be applied to such an algorithm, such as </p> <ul> <li>Convert the first letter in a word to upper case</li> <li>Convert the remaining letters in the word to lower case</li> <li>Remove white space</li> </ul> <p> The real algorithm would need to split the string into words along white space boundaries, then loop through this list and perform the conversion for each word, and finally concatenate all the words. However, I don't think you should reproduce this algorithm in any single test. </p> <p> What you can do instead is to split this behavior into several tests that each test a small part of this specification, carefully avoiding any control flow language features (such as <em>if, switch, for, </em>etc.). </p> <p> One such test might look like this: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> ToPascalCaseWillConvertFirstLetterToUpper() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> anonymousText = <span style="color: #a31515">"pLOeh"</span>; &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> expectedLetter = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; anonymousText.First().ToString().ToUpper(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; anonymousText.ToPascalCase(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedLetter, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result.First().ToString(), <span style="color: #a31515">"ToPascalCase"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> While the complete implementation of ToPascalCase is more complex, I've extracted a tiny bit of the specification and simulated just that for the special case where there's only one word. Granted, there's a lot of method calls, but I expect that these have already been thoroughly tested, so I use them with confidence. The cyclomatic complexity of the test is minimal. </p> <p> As an aside, note that I'm using LINQ queries to get the first letter of the string, instead of Substring(0, 1), since I find that the LINQ methods much better communicate intent. </p> <p> I use a similar naming convention for unexpected values, imaginatively prefixing my variables with the term <em>unexpected</em>. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> CreateThingWillCreateThingWithCorrectGuid() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Guid</span> unexpectedId = <span style="color: #2b91af">Guid</span>.Empty; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Thing</span> result = sut.CreateThing(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreNotEqual&lt;<span style="color: #2b91af">Guid</span>&gt;(unexpectedId, result.Id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"CreateThing"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> Having a naming convention for expected values not only increases your productivity when writing tests, but also increases test maintainability. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4d156a8ebb274bb6a86a62357631aefc"> <div class="comment-author"><a href="https://github.com/tiesmaster">Thijs Brobbel</a> <a href="#4d156a8ebb274bb6a86a62357631aefc">#</a></div> <div class="comment-content">Hi,<br> <br> I've read this post a while back, and it was a really helpful way for me to see you suggestion how to still use the "explicit relationship" between input, and output, but preventing yourself from reproducing the implementation by breaking the various parts of the algorithm apart.<br> I've used that already in plenty of situations, where I would have written a single unit test which was testing multiple scenario's, which could be split up in various isolated unit tests.<br> However, the other day, I needed to write a view model property that combines 2 properties of that view model in a particular format (see below for the code). I also tried to use this approach, but I couldn't find a way to do that. How would you break that apart in different unit tests?<br> public string NameAndType { get { return string.Format("{0} [{1}]", Name, ContracteeType); } } </div> <div class="comment-date">2015-02-03 15:33 UTC</div> </div> <div class="comment" id="b62a043d3b4e4c74a618ae4d615e10e5"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#b62a043d3b4e4c74a618ae4d615e10e5">#</a></div> <div class="comment-content"> <p> Like this? </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;Foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Foo&nbsp;[Bar]&quot;</span>)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;Baz&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Qux&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;Baz&nbsp;[Qux]&quot;</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;NameAndTypeReturnsCorrectResult( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;contracteeType, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;expected) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;sut&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MyClass</span>(name,&nbsp;contracteeType); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.NameAndType; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(expected,&nbsp;actual); }</pre> </p> <p> I'm not sure I understand the question... </p> </div> <div class="comment-date">2015-02-03 17:03 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Specification-Driven Development https://blog.ploeh.dk/2009/03/10/Specification-DrivenDevelopment 2009-03-10T21:04:38+00:00 Mark Seemann <div id="post"> <p> In the last couple of years, there's been a lot of debate in the community on the philosophy behind TDD and where to put the emphasis - even to the point of debating whether the acronym stands for Test-Driven <em>Development</em> or Test-Driven <em>Design</em>. </p> <p> Other people don't like the emphasis on <em>tests</em>, since that makes TDD sound like a Testing discipline, and not a Development discipline. Instead, they prefer terms like <em>Example-Driven Design/Development</em> (EDD) or even <em>Design by Example</em> (DbE). </p> <p> This view seems to me to be particularly prevalent in Microsoft, where there's a rather sharp distinction between developers and testers (job titles to the contrary) - I guess that's one of the reasons why <a href="http://www.codeplex.com/xunit">xUnit.net</a> (a project initiated by Microsoft employees) uses the attribute <em>Fact</em> instead of <em>Test</em> or <em>TestMethod</em>. </p> <p> For people used to SCRUM or other agile methodologies, this distinction is more blurred, and they also seem to accept the <em>T</em> in TDD more willingly. </p> <p> However, the adherents of EDD claim that the mere presence of the word <em>test</em> make some developers block any further input and stop listening. They may be right in that. </p> <p> They also claim that the <em>tests</em> in TDD/EDD are nothing more than accidental artifacts of the development <em>process</em>, and hence argue that we shouldn't call them <em>tests</em> at all. However, if that's true, <a href="http://ayende.com/Blog/archive/2007/10/19/Test-Once-The-other-side-of-continuous-integration.aspx">this little story</a> related by <a href="http://ayende.com/Blog/Default.aspx">Ayende</a> must be an example of EDD in its purest form :) </p> <p> To me, the tests are <em>also</em> important. Since 2003 I've been practicing TDD, and while I love how it helps me arrive at better design, I also savor the safety net that my suite of tests gives me. The tests that I write during TDD define the <em>behavior</em> of the software. In many cases, I'd even claim that such a regression test suite is more valuable than a Quality Assurance (QA) regression test suite - after all, a QA suite may catch some edge cases, but they don't focus on the intended <em>behavior</em> of the system, but often more on how to break it - but I digress… </p> <p> My recent posts on <a href="/2009/03/03/DerivedValuesEnsureExecutableSpecification">Executable Specification</a> and <a href="/2009/03/05/ConstrainedNon-Determinism">Constrained Non-Determinism</a> help explain my current stance in this debate: In my opinion, EDD fails to establish a relationship by not providing <a href="http://xunitpatterns.com/Derived%20Value.html">Derived Values</a>. After all, what does a test like the following specify? </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> InvertWillReverseText_Naïve() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.Invert(<span style="color: #a31515">"ploeh"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(<span style="color: #a31515">"heolp"</span>, result, <span style="color: #a31515">"Invert"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> How would you implement the Invert method? Here's one possible implementation: </p> <p> <pre><span style="color: blue">return</span> <span style="color: #a31515">"heolp"</span>;</pre> </p> <p> Obviously, you could now write a new test that gives a <em>second</em> example of input and outcome and force me to write a more sophisticated algorithm. However, with only two examples, I might still be tempted to write a switch statement with some hard-coded return values until you've written so many ‘examples' that you've coerced me into writing the more general (and correct) algorithm. </p> <p> Such an approach I find inefficient. </p> <p> Instead, by using Constrained Non-Determinism to force myself to define Derived Values, each test fully <em>specifies</em> the desired behavior. It doesn't provide <em>examples</em>. It provides the <em>specification</em>, and instead of having to write several similar examples to coerce a general algorithm to emerge, I can usually nail it in a single test. </p> <p> This approach could be styled Specification-Driven Development, and that's how I've been writing code for the last year or so. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="455ce3584a464c0d83fe6f4382a08389"> <div class="comment-author"><a href="http://blog.kjempekjekt.com">Torbj&#248;rn Mar&#248;</a> <a href="#455ce3584a464c0d83fe6f4382a08389">#</a></div> <div class="comment-content">Just a comment... Remember the pattern in TDD: Red, Green, Refactor. You're not supposed to write a second test in order to force you to remove the hardcoded return &quot;heolp&quot;; implementation. You're supposed to remove duplication during the refactoring stage. The constant &quot;heolp&quot; is duplicated in the test and in the implementation code, and according to Kent Beck this is your main focus during refactoring; to eliminate duplication. </div> <div class="comment-date">2009-03-28 12:28 UTC</div> </div> <div class="comment" id="f811204040314d3f9868a00fb24a844d"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#f811204040314d3f9868a00fb24a844d">#</a></div> <div class="comment-content">Hi Torbj&#248;rn<br> <br> Thank you for your comment.<br> <br> First of all: I'm a big believer in the Red/Green/Refactor cycle, so we're totally on the same page there!<br> <br> As the test in the example stands, the constant &quot;heolp&quot; is most certainly duplicated across test and implementation code, until the point when I decide to implement the Invert method correctly. So far I can only agree, but partially because my example is so simple: Once again I fall into the trap that my example is too simple, but had I made it more complex, I'd have lost my reader(s) long ago.<br> <br> One or few 'examples' would be enough to clue anyone on to the fact that the intended algorithm of the Invert method is to invert the input. It's fairly obvious.<br> <br> Imagine, instead, that we're testing a hypothetical CalculateRocketTrajectory method. Yes, we're attempting to implement a Domain Model over rocket science! Unless you <i>are</i> a rocket scientist, it's likely that it's going to take a pretty big amount of 'examples' before you can figure out what is the underlying algorithm. It would be much more efficient if each test could simply <i>specify</i> the relationship between input and output.<br> <br> Yes, that would lead to a bit of duplication across the test code and the implementation code, but I prefer that to the alternative. In my experience, most software development isn't about algorithm development anyway; it's about API design, Domain Modeling, handling corner cases, moving and transforming data, etc.<br> <br> This is not to say that I am right and you are wrong. Here, I'm mainly trying to describe what motivates me to work in a certain way, so please feel free to keep the discussion going!</div> <div class="comment-date">2009-03-28 13:55 UTC</div> </div> <div class="comment" id="a6af88ebee704361b5e2b79a5a2e72e0"> <div class="comment-author"><a href="http://blog.kjempekjekt.com">Torbj&#248;rn Mar&#248;</a> <a href="#a6af88ebee704361b5e2b79a5a2e72e0">#</a></div> <div class="comment-content">I expect you have much more actual experience with TDD (or EDD or DbE or BDD) then myself. Since I just read Beck's TDD book and had it fresh in mind, I just wanted to give my comment, for your readers mostly, since I felt that part of the pattern wasn't represented in your post.<br> <br> Keep up the nice posts.., enjoying your blog!</div> <div class="comment-date">2009-03-28 18:01 UTC</div> </div> <div class="comment" id="7819402aa4414a89bfe2b23b5707c42f"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#7819402aa4414a89bfe2b23b5707c42f">#</a></div> <div class="comment-content">Hi Torbj&#248;rn<br> <br> Thanks again. You point was valid and well taken.<br> <br> One of the reasons I find EDD ineffecient stems from my experience with The XP Game, which is enjoyable, but a bit too much on the back-and-forth side for my tastes.<br> <br> YMMV, so I'm not trying to dictate what anyone should do - I'm just trying to demonstrate that there are more than one way to do TDD.</div> <div class="comment-date">2009-03-28 19:54 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Constrained Non-Determinism https://blog.ploeh.dk/2009/03/05/ConstrainedNon-Determinism 2009-03-05T20:23:05+00:00 Mark Seemann <div id="post"> <p> This may turn out to be the most controversial of my <a href="/2009/01/28/Zero-FrictionTDD">Zero-Friction TDD</a> posts so far, as it supposedly goes against conventional wisdom. However, I have found this approach to be really powerful since I began using it about a year ago. </p> <p> In my previous post, I explained how <a href="/2009/03/03/DerivedValuesEnsureExecutableSpecification">Derived Values help ensure that tests act as Executable Specification</a>. In short, a test should clearly specify the relationship between input and outcome, as this test does: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> InvertWillReverseText() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> anonymousText = <span style="color: #a31515">"ploeh"</span>; &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> expectedResult = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: blue">string</span>(anonymousText.Reverse().ToArray()); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.Invert(anonymousText); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"DoWork"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> However, it is very tempting to just hardcode the expected value. Consistently using <a href="/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;url=http%3a%2f%2fxunitpatterns.com%2fDerived%2520Value.html">Derived Values</a> to establish the relationship between input and outcome requires discipline. </p> <p> To help myself enforce this discipline, I use well-defined, but essentially random, input, because when the input is random, I don't know the value at design time, and hence, it is impossible for me to accidentally hard-code any assertions. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> InvertWillReverseText_Cnd() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> anonymousText = <span style="color: #2b91af">Guid</span>.NewGuid().ToString(); &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> expectedResult = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: blue">string</span>(anonymousText.Reverse().ToArray()); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.Invert(anonymousText); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"DoWork"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> For strings, I prefer Guids, as the above example demonstrates. For numbers, I often just use the sequence of natural numbers (i.e. <em>1, 2, 3, 4, 5...</em>). For booleans, I often use an alternating sequence (i.e. <em>true, false, true, false...</em>). </p> <p> While this technique causes input to become non-deterministic, I always pick the non-deterministic value-generating algorithm in such a way that it creates 'nice' values; I call this principle <strong>Constrained Non-Determinism</strong>. Values are carefully generated to stay far away from any boundary conditions that may cause the <a href="/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;url=http%3a%2f%2fxunitpatterns.com%2fSUT.html">SUT</a> to behave differently in each test run. </p> <p> Conventional unit testing wisdom dictates that unit tests should be deterministic, so how can I possibly endorse this technique? </p> <p> To understand this, it's important to know <em>why</em> the rule about deterministic unit tests exist. It exists because we want to be certain that each time we execute a test suite, we verify the exact same behavior as we did the last time (given that no tests changed). Since we also use test suites as regression tests, it's important that we can be confident that each and every test run verifies the exact same <em>specification</em>. </p> <p> Constrained Non-Determinism doesn't invalidate that goal, because the algorithm that generates the values must be carefully picked to always create values that stay within the input's <a href="http://xunitpatterns.com/equivalence%20class.html">Equivalence Class</a>. </p> <p> In a surprisingly large set of APIs, strings, for example, are treated as opaque values that don't influence behavior in themselves. Many enterprise applications mostly store and read data from persistent data stores, and the value of a string in itself is often inconsequential from the point of view of the code's execution path. Data stores may have constraints on the length of strings, so Constrained Non-Determinism dictates that you should pick the generating algorithm so that the string length always stays within (or exceeds, if that's what you want to test) the constraint. Guid.ToString always returns a string with the length of 36, which is fine for a large number of scenarios. </p> <p> Note that Constrained Non-Determinism is only relevant for <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous Variables</a>. For input where the value holds a particular meaning in the context of the SUT, you will still need to hand-pick values as always. E.g. if the input is expected to be an XML string conforming to a particular schema, a Guid string makes no sense. </p> <p> A secondary benefit of Constrained Non-Determinism is that you don't have to pause to come up with values for Anonymous Variables when you are writing the test. </p> <p> While this advice may be controversial, I can only recommend it - I've been using this technique for about a year now, and have only become more fond of it as I have gained more experience with it. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="5e8834cfaf124d8ba01d362d945c5794"> <div class="comment-author"><a href="http://www.carlj.ca">Carl J</a> <a href="#5e8834cfaf124d8ba01d362d945c5794">#</a></div> <div class="comment-content">Hey Phoeh, this is @CarlJ (if you didn't guess).<br> <br> One suggestion/tip when using random values to test, is to output the random value when the test fails. With randomness, you may come across that 1 in 100 value that breaks the test, but you won't have any idea of what it was, which makes it a bit hard to replicate and fix. In my Unit Test project, I have a common method that you pass the value to and it will print it to the output screen when it fails.<br> <br> As for my question on Twitter. I'm dealing with a really large database (700Gbs), which returns an infinite number of combinations of data through stored procs. There are no INSERT/UPDATE/DELETEs (that's done in a totally different project). What I want to test is how the code handles the data that is returned from the procs, which is based on a user's selection from multiple radio buttons, and drop down boxes (single and multiple selection). <br> <br> This data that they select from, comes from the database too, which we have little control over if it's valid or not.<br> <br> So my question on Twitter was, should I create a method(s) that generate random parameters (which come from the DB) that the proc accepts? What I'm testing is not if the data itself is valid, but whether the code handles some weird anomalies? Or should I just use parameters that I know return valid data? <br> <br> I've already experimented with creating a proc that generates random parameters from the data within the database, and using those values in my test. Amazingly, it's found a lot of issues that I was able to go back and fix.<br> <br> The reason why I ask, is because I've heard that this goes beyond &quot;Unit Testing&quot;.<br> <br> Thanks,<br> Carl J </div> <div class="comment-date">2010-10-08 14:52 UTC</div> </div> <div class="comment" id="eeb5eb41c03344d39460fd8a29a82d86"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#eeb5eb41c03344d39460fd8a29a82d86">#</a></div> <div class="comment-content">It sounds to me like <a href="http://research.microsoft.com/en-us/projects/pex/">Pex</a> would fit your scenario very well. Basically, that would allow you to write a Pex test that creates deterministic test cases for each and every code path through the SUT. I do realize that data comes from the database, but it's still input. In testing terminology, we call that <em>Indirect Input</em>.<br> <br> If you can't inject a Test Double that provides all the different data combinations, you should be able to use Moles for that part of the task.</div> <div class="comment-date">2010-10-08 15:57 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Derived Values Ensure Executable Specification https://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification 2009-03-03T20:01:29+00:00 Mark Seemann <div id="post"> <p> In this <a href="/2009/01/28/Zero-FrictionTDD">Zero-Friction TDD</a> post, I'd like to take a detour around the concept of tests as <a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Executable%20Specification">Executable Specification</a>. </p> <p> An important aspect of test maintainability is readability. Tests should act both as Executable Specification as well as <a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Tests%20as%20Documentation">documentation</a>, which puts a lot of responsibility on the test. </p> <p> One facet of test readability is to make the relationship between the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a>, the <a href="http://xunitpatterns.com/SUT.html">SUT</a> and the verification as easy to understand as possible. In other words, it should be clear to the <a href="http://xunitpatterns.com/test%20reader.html">Test Reader</a> what is being asserted, and why. </p> <p> Consider a test like this one: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> InvertWillReverseText_Naïve() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.Invert(<span style="color: #a31515">"ploeh"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(<span style="color: #a31515">"heolp"</span>, result, <span style="color: #a31515">"DoWork"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> Since this test is so simple, I expect that you can easily figure out that it implies that the Invert method should simply reverse its input argument, but one of the reasons this seems to be evident is because of the proximity of the two strings, as well as the test's name. </p> <p> In a test of a more complex API, this may not be quite as evident. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> DoItWillReturnCorrectResult_Naïve() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">int</span> result = sut.DoIt(<span style="color: #a31515">"ploeh"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(42, result, <span style="color: #a31515">"DoIt"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> In this test, there's no apparent relationship between the input (<em>ploeh</em>) and the output (<em>42</em>). Whatever the algorithm is behind the DoIt method, it's completely opaque to the Test Reader, and the test fails in its role as specification and documentation. </p> <p> Returning to the first example, it would be better if the relationship between input and output was explicitly described: </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> InvertWillReverseText() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> anonymousText = <span style="color: #a31515">"ploeh"</span>; &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> expectedResult = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: blue">string</span>(anonymousText.Reverse().ToArray()); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.Invert(anonymousText); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"DoWork"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> In this case, the input and expected outcome are clearly related, and we call the expectedResult variable a <a href="http://xunitpatterns.com/Derived%20Value.html">Derived Value</a>, since we explicitly derive the expected result from the input. </p> <p> Note that I'm not asking you to re-implement the whole algorithm in the test, but only to establish a relationship. One of the main rules of thumb of unit testing is that a test should never contain conditional branches, so there must be at least one test case per path though the SUT. </p> <p> In the example, the Invert method actually looks like this: </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">string</span> Invert(<span style="color: blue">string</span> message) { &nbsp;&nbsp;&nbsp; <span style="color: blue">double</span> d; &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (<span style="color: blue">double</span>.TryParse(message, <span style="color: blue">out</span> d)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> (1d / d).ToString(); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: blue">string</span>(message.Reverse().ToArray()); }</pre> </p> <p> Note that the above test only reproduces that part of the algorithm that corresponds to the <a href="http://xunitpatterns.com/equivalence%20class.html">Equivalence Class</a> defined by the input, whereas the branch that is triggered by a number string can be tested by another test case that doesn't specify string reversion. </p> <p> <pre>[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> InvertWillInvertNumber() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">double</span> anonymousNumber = 10; &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> numberText = anonymousNumber.ToString(); &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> expectedResult = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (1d / anonymousNumber).ToString(); &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> result = sut.Invert(numberText); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult, result, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #a31515">"DoWork"</span>); &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> In this way, we can break down the test cases to individual Executable Specifications that define the expected behavior for each Equivalence Class. </p> <p> While such tests more clearly provide both specification and documentation, it requires discipline to write tests in this way. Particularly when the algorithm is so simple as is the case here, it's very tempting to just hard-code the values directly into the assertion. </p> <p> In a future post, I'll explain <a href="/2009/03/05/ConstrainedNon-Determinism">how we can force ourselves to do the right thing per default</a>. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Updating Detached Entities With LINQ To Entities https://blog.ploeh.dk/2009/02/22/UpdatingDetachedEntitiesWithLINQToEntities 2009-02-22T20:45:36+00:00 Mark Seemann <div id="post"> <p> When working with the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx">ObjectContext</a> in <a href="http://msdn.microsoft.com/en-us/library/bb386964.aspx">LINQ To Entities</a>, a lot of operations are easily performed as long as you work with the same ObjectContext instance: You can retrieve entities from storage by selecting them; update or delete these entities and create new entities, and the ObjectContext will keep track of all this for you, so the changes are correctly applied to the store when you call <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.savechanges.aspx">SaveChanges</a>. </p> <p> This is all well and good, but not particularly useful when you start working with layered applications. In this case, LINQ To Entities is just a persistence technology that you (or someone else) decided to use to implement the Data Access Layer. A few years ago, I tended to implement my Data Access Components in straight ADO.NET; and a lot of people prefer <a href="http://nhforge.org/Default.aspx">NHibernate</a> or similar tools - but I digress… </p> <p> When LINQ To Entities is just an implementation detail of a service, lifetime management becomes important, so it is commonly recommended that any ObjectContext instance is instantiated when needed and disposed immediately after use. </p> <p> This means that you will have a lot of detached entities in your system. Entities are likely to be returned to the calling code as interface, and when updating, a client will simply pass a reference to <em>some</em> implementation of that interface. </p> <p> <pre><span style="color: blue">public</span> <span style="color: blue">void</span> CompleteAtSource(<span style="color: #2b91af">IRecord</span> record)</pre> </p> <p> Since we should always follow the Liskov Substitution Principle, we should not even try to cast the interface to an entity. Instead, we must populate a new instance of the entity in question with the correct data and save it. </p> <p> That's not hard, but since we are creating a new instance of an entity that represents data that is already in the database, we must attach it to the ObjectContext so that it can start tracking it again. </p> <p> Now we are getting to the heat of the matter, because this is done with the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.attachto.aspx">AttachTo</a> method, which is woefully inadequately documented. </p> <p> At first, I couldn't get it to work, and it wasn't very apparent to me what I did wrong, so although the answer is very simple, this post might save you a bit of time. </p> <p> This was my first attempt: </p> <p> <pre><span style="color: blue">using</span> (<span style="color: #2b91af">MessageEntities</span> store = &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MessageEntities</span>(<span style="color: blue">this</span>.connectionString)) { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Message</span> m = <span style="color: blue">new</span> <span style="color: #2b91af">Message</span>(); &nbsp;&nbsp;&nbsp; m.Id = record.Id; &nbsp;&nbsp;&nbsp; m.InputReference = record.InputReference; &nbsp;&nbsp;&nbsp; m.State = 2; &nbsp;&nbsp;&nbsp; m.Text = record.Text; &nbsp; &nbsp;&nbsp;&nbsp; store.AttachTo(<span style="color: #a31515">"Messages"</span>, m); &nbsp; &nbsp;&nbsp;&nbsp; store.SaveChanges(); }</pre> </p> <p> I find this approach very intuitive: Build the entity from the input parameter's data, attach it to the store and save the changes. Unfortunately, this approach is wrong. </p> <p> What happens is that when you invoke AttachTo, the state of the entity becomes <a href="http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx">Unchanged</a>, and thus, not updated. </p> <p> The solution is so simple that I'm surprised it took me so long to arrive at it: Simply call AttachTo right after setting the Id property: </p> <p> <pre><span style="color: blue">using</span> (<span style="color: #2b91af">MessageEntities</span> store = &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MessageEntities</span>(<span style="color: blue">this</span>.connectionString)) { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Message</span> m = <span style="color: blue">new</span> <span style="color: #2b91af">Message</span>(); &nbsp;&nbsp;&nbsp; m.Id = record.Id; &nbsp; &nbsp;&nbsp;&nbsp; store.AttachTo(<span style="color: #a31515">"Messages"</span>, m); &nbsp; &nbsp;&nbsp;&nbsp; m.InputReference = record.InputReference; &nbsp;&nbsp;&nbsp; m.State = 2; &nbsp;&nbsp;&nbsp; m.Text = record.Text; &nbsp; &nbsp;&nbsp;&nbsp; store.SaveChanges(); }</pre> </p> <p> You can't invoke AttachTo <em>before</em> adding the Id, since this method requires that the entity has a populated <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.entityobject.entitykey.aspx">EntityKey</a> before it can be attached, but as soon as you begin updating properties <em>after</em> the call to AttachTo, the entity's state changes to Modified, and SaveChanges now updates the data in the database. </p> <p> That you have to follow this specific sequence when re-attaching data to the ObjectContext is poorly documented and not enforced by the API, so I thought I'd share this in case it would save someone else a bit of time. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="e321b7d0b92d416b9871a5c635b54c6d"> <div class="comment-author"><a href="http://www.dotninjas.dk">Jakob Christensen</a> <a href="#e321b7d0b92d416b9871a5c635b54c6d">#</a></div> <div class="comment-content">Luckily, the ADO.NET team implemented the attach logic correctly in the Entity framework as opposed to Linq to Sql. </div> <div class="comment-date">2009-03-10 12:39 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. SUT Factory https://blog.ploeh.dk/2009/02/13/SUTFactory 2009-02-13T07:56:21+00:00 Mark Seemann <div id="post"> <p> In my <a href="/2009/01/28/Zero-FrictionTDD">Zero-Friction TDD</a> series, I focus on establishing a set of good habits that can potentially make you more productive while writing tests TDD style. While being able to quickly write good tests is important, this is not the only quality on which you should focus. </p> <p> Maintainability, not only of your production code, but also of your test code, is important, and the DRY principle is just as applicable here. </p> <p> Consider a test like this: </p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> SomeTestUsingConstructorToCreateSut() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// ...</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// ...</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> Such a test represents an anti-pattern you can easily fall victim to. The main item of interest here is that I create the <a href="http://xunitpatterns.com/SUT.html">SUT</a> using its constructor. You could say that I have hard-coded this particular constructor usage into my test. </p> <p> This is not a problem if there's only one test of MyClass, but once you have many, this starts to become a drag on your ability to refactor your code. </p> <p> Imagine that you want to change the constructor of MyClass from the default constructor to one that takes a dependency, like this: </p> <p> <pre style="margin: 0px"><span style="color: blue">public</span> MyClass(<span style="color: #2b91af">IMyInterface</span> dependency)</pre> </p> <p> If you have many (in this case, not <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">three</a>, but dozens) tests using the default constructor, this simple change will force you to visit all these tests and modify them to be able to compile again. </p> <p> If, instead, we use a factory to create the SUT in each test, there's a single place where we can go and update the creation logic. </p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> SomeTestUsingFactoryToCreateSut() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">MyClass</span> sut = <span style="color: #2b91af">MyClassFactory</span>.Create(); &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// ...</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// ...</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> The MyClassFactory class is a test-specific helper class (more formally, it's part of our <a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation">SUT API Encapsulation</a>) that is part of the unit test project. Using this factory, we only need to modify the Create method to implement the constructor change. </p> <p> <pre style="margin: 0px"><span style="color: blue">internal</span> <span style="color: blue">static</span> <span style="color: #2b91af">MyClass</span> Create() { &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> fake = <span style="color: blue">new</span> <span style="color: #2b91af">FakeMyInterface</span>(); &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(fake); }</pre> </p> <p> Instead of having to modify many individual tests to support the signature change of the constructor, there's now one central place where we can go and do that. This pattern supports refactoring much better, so consider making this a habit of yours. </p> <p> One exception to this rule concerns tests that explicitly deal with the constructor, such as this one: </p> <p> <pre style="margin: 0px">[<span style="color: #2b91af">ExpectedException</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">ArgumentNullException</span>))] [<span style="color: #2b91af">TestMethod</span>] <span style="color: blue">public</span> <span style="color: blue">void</span> CreateWithNullMyInterfaceWillThrow() { &nbsp;&nbsp;&nbsp; <span style="color: green">// Fixture setup</span> &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">IMyInterface</span> nullMyInterface = <span style="color: blue">null</span>; &nbsp;&nbsp;&nbsp; <span style="color: green">// Exercise system</span> &nbsp;&nbsp;&nbsp; <span style="color: blue">new</span> <span style="color: #2b91af">MyClass</span>(nullMyInterface); &nbsp;&nbsp;&nbsp; <span style="color: green">// Verify outcome (expected exception)</span> &nbsp;&nbsp;&nbsp; <span style="color: green">// Teardown</span> }</pre> </p> <p> In a case like this, where you explicitly want to deal with the constructor in an anomalous way, I consider it reasonable to deviate from the rule of using a factory to create the SUT. Although this may result in a need to fix the SUT creation logic in more than one place, instead of only in the factory itself, it's likely to be constrained to a few places instead of dozens or more, since normally, you will only have a handful of these explicit constructor tests. </p> <p> Compared to my Zero-Friction TDD tips and tricks, this particular advice has the potential to marginally slow you down. However, this investments pays off when you want to refactor your SUT's constructor, and remember that you can always just write the <a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx">call to the factory and move on without implementing it right away</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="7cfd05fbae4548a9a5bc963bfdfc8d34"> <div class="comment-author"><a href="http://community.ative.dk/blogs/">Martin Gildenpfennig</a> <a href="#7cfd05fbae4548a9a5bc963bfdfc8d34">#</a></div> <div class="comment-content">Another approach is to let the test fixture hold the SUT in a field and then instantiate it in the test initialize method. This way all tests will have access to a default instance of the SUT, without cluttering the test itself with details of how it was created. Since dependencies will also be created in test init, your tests will also be able to access the stubs or mocks for verification.<br> <br> </div> <div class="comment-date">2009-02-16 19:20 UTC</div> </div> <div class="comment" id="e094a085b6544cea9c314e456e85d4fb"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#e094a085b6544cea9c314e456e85d4fb">#</a></div> <div class="comment-content">Hi Martin<br> <br> Thank you for your comment.<br> <br> While you are right that technically, this is another option, I don't like to use Implicit Setup, since it doesn't clearly communicate intent. If you have dozens of test cases in a single Test Class, the Setup may not be very apparant; in essence, it's clouding the state of the Fixture, since it's not readily visible (it may be in a completely different secion of the file).<br> <br> Another reason I don't like this approach is that it tightly couples the Test Class to the Fixture, and it makes it harder to vary the Fixture within the same Test Class.<br> <br> Explicitly setting up the Fixture provides a greater degree of flexibility, since you can always overload the SUT Factory to create the SUT in different ways.</div> <div class="comment-date">2009-02-17 13:09 UTC</div> </div> <div class="comment" id="44da848fa4ec4ca69fdafb8253c83d83"> <div class="comment-author"><a href="http://blog.rajsoftware.com">Raj Aththanayake</a> <a href="#44da848fa4ec4ca69fdafb8253c83d83">#</a></div> <div class="comment-content">I like this pattern. What's your opinion on doing the same thing within the method decorated with [TestIntialize] attr (MS Test), or even in constructor (i.e xUnit)? It achieves the same result (or may be even better) IMO.</div> <div class="comment-date">2011-05-24 10:54 UTC</div> </div> <div class="comment" id="898678fcac7e4e288040507106f4e009"> <div class="comment-author"><a href="/">Mark Seemann</a> <a href="#898678fcac7e4e288040507106f4e009">#</a></div> <div class="comment-content">Raj, the problem with this approach is that in order to be maintainable you would need to adopt the <a href="http://xunitpatterns.com/Testcase%20Class%20per%20Fixture.html">Testcase Class per Fixture</a> pattern, because if you don't the test class will eventually suffer from low cohesion. However, most people (myself included) tend to find this pattern counter-intuitive and rather prefer <a href="http://xunitpatterns.com/Testcase%20Class%20per%20Class.html">Testcase Class per Class</a>.</div> <div class="comment-date">2011-05-24 19:07 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Zero-Friction TDD https://blog.ploeh.dk/2009/01/28/Zero-FrictionTDD 2009-01-28T14:46:44+00:00 Mark Seemann <div id="post"> <p> In my original post on <a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/zero-friction-tdd">Zero-Friction TDD</a>, I continually updated the list of posts in the series, so that there would always be a central 'table of contents' for this topic. </p> <p> Since <a href="/2009/01/28/LivingInInterestingTimes">I'll lose the ability to keep editing any of my previous postings</a> on the old <a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/">ploeh blog</a>, the present post now contains the most updated list of Zero-Friction TDD articles: </p> <ul> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/naming-sut-test-variables">Naming SUT Test Variables</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/naming-direct-output-variables">Naming Direct Output Variables</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/anonymous-variables">Anonymous Variables</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/ignore-irrelevant-return-values">Ignore Irrelevant Return Values</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/testmethod-code-snippet">testmethod Code Snippet</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/3-is-many">3 Is Many</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/why-use-areequalt">Why Use AreEqual&lt;T&gt;?</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/assert-messages-are-not-optional">Assert Messages Are Not Optional</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone">Use The Generate Method Stub Smart Tag To Stay In The Zone</a></li> <li><a href="https://docs.microsoft.com/en-us/archive/blogs/ploeh/test-driven-properties">Test-Driven Properties</a></li> <li><a href="/2009/02/13/SUTFactory">SUT Factory</a></li> <li><a href="/2009/03/03/DerivedValuesEnsureExecutableSpecification">Derived Values Ensure Executable Specification</a></li> <li><a href="/2009/03/05/ConstrainedNon-Determinism">Constrained Non-Determinism</a></li> <li><a href="/2009/03/11/ExplicitExpectations">Explicit Expectations</a></li> <li><a href="/2009/03/16/FixtureObject">Fixture Object</a></li> <li><a href="/2009/05/15/AutoFixtureAsFixtureObject">AutoFixture As Fixture Object</a></li> </ul> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Living In Interesting Times https://blog.ploeh.dk/2009/01/28/LivingInInterestingTimes 2009-01-28T09:03:29+00:00 Mark Seemann <div id="post"> <p> As readers of <a href="http://blogs.msdn.com/ploeh/">my old MSDN blog</a> will know, <em>ploeh blog</em> is moving to this new site. </p> <p> Responding to the current financial crisis, Microsoft is cutting costs and laying off 1400 employees. During that process, the entire <a href="http://blogs.msdn.com/dynamicsmobile/default.aspx">Microsoft Dynamics Mobile Team</a> is being disbanded, which is very sad, since it was a very nice place to work. The team spirit was great, and we were really committed to agile development methodologies, but all good things must end... </p> <p> Currently, I can't even begin to guess what the future looks like for me, although to regular readers of my blog I can state that I sincerely intend to keep writing as I always have. If you subscribed to my old blog, then please subscribe here instead. The blog is moving because the old blog belongs to Microsoft, and only employees can post, so I'll soon be writing my last post on the old blog. </p> <p> Until now, it's always been Microsoft's policy to retain the old blogs, even when the original authors leave the company, so while I will not be able to post to the old blog, I expect the old posts to be around for a long time yet. </p> <p> Professionally, I don't know what I will do now. If I can find new employment in these times, I may simply decide to take on new challenges with a new employer. However, I'm also considering free-lancing for a while: Coding, mentoring, lecturing, writing... </p> <p> If you are in the position where you think you can use my services, whether for full-time employment or just a few days, please let me know. Keep in mind that I'm based in Copenhagen, Denmark, and while I can certainly travel after work, I cannot permanently move due to family obligations. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="c0b2e0bd500a4d5c8407c60bf11bff69"> <div class="comment-author"><a href="http://ebersys.blogspot.com">Eber Irigoyen</a> <a href="#c0b2e0bd500a4d5c8407c60bf11bff69">#</a></div> <div class="comment-content"> <p> I'm very sorry to hear that; but you are a very smart person so I'm sure you'll have no problem finding a new job </p> <p> I wish you the best </p> </div> <div class="comment-date">2009-02-11 22:42 UTC</div> </div> <div class="comment" id="dbe1ac567c5543138b09d6f9f2653871"> <div class="comment-author"><a href="http://www.it-practice.dk">Simon B. Jensen</a> <a href="#dbe1ac567c5543138b09d6f9f2653871">#</a></div> <div class="comment-content"> <p> We're currently looking for senior developers with MS competencies. So have a look at our website and send me your CV, if you're interested. </p> <p> Regards </p> <p> Simon B. Jensen<br> Head of Development<br> IT Practice </p> </div> <div class="comment-date">2009-02-12 13:25 UTC</div> </div> <div class="comment" id="6ba0bb0a36394501a446b9575474c1a3"> <div class="comment-author"><a href="http://blogs.msdn.com">Brian Lambert</a> <a href="#6ba0bb0a36394501a446b9575474c1a3">#</a></div> <div class="comment-content"> <p> Mark, </p> <p> You're a good man. A software genius. The fact that you got laid off is a crime. I would hire you any day, and I wish you the best. Friend me on Facebook. brianlambert@gmail.com. </p> <p> All my best, </p> <p> Brian </p> </div> <div class="comment-date">2009-02-24 05:34 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>.